Statistics
| Branch: | Revision:

root / block / qcow2.c @ dc7588c1

History | View | Annotate | Download (51.9 kB)

1 585f8587 bellard
/*
2 585f8587 bellard
 * Block driver for the QCOW version 2 format
3 5fafdf24 ths
 *
4 585f8587 bellard
 * Copyright (c) 2004-2006 Fabrice Bellard
5 5fafdf24 ths
 *
6 585f8587 bellard
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 585f8587 bellard
 * of this software and associated documentation files (the "Software"), to deal
8 585f8587 bellard
 * in the Software without restriction, including without limitation the rights
9 585f8587 bellard
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 585f8587 bellard
 * copies of the Software, and to permit persons to whom the Software is
11 585f8587 bellard
 * furnished to do so, subject to the following conditions:
12 585f8587 bellard
 *
13 585f8587 bellard
 * The above copyright notice and this permission notice shall be included in
14 585f8587 bellard
 * all copies or substantial portions of the Software.
15 585f8587 bellard
 *
16 585f8587 bellard
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 585f8587 bellard
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 585f8587 bellard
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 585f8587 bellard
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 585f8587 bellard
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 585f8587 bellard
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 585f8587 bellard
 * THE SOFTWARE.
23 585f8587 bellard
 */
24 faf07963 pbrook
#include "qemu-common.h"
25 737e150e Paolo Bonzini
#include "block/block_int.h"
26 1de7afc9 Paolo Bonzini
#include "qemu/module.h"
27 585f8587 bellard
#include <zlib.h>
28 753d9b82 Aurelien Jarno
#include "qemu/aes.h"
29 f7d0fe02 Kevin Wolf
#include "block/qcow2.h"
30 1de7afc9 Paolo Bonzini
#include "qemu/error-report.h"
31 7b1b5d19 Paolo Bonzini
#include "qapi/qmp/qerror.h"
32 acdfb480 Kevin Wolf
#include "qapi/qmp/qbool.h"
33 3cce16f4 Kevin Wolf
#include "trace.h"
34 585f8587 bellard
35 585f8587 bellard
/*
36 585f8587 bellard
  Differences with QCOW:
37 585f8587 bellard

38 585f8587 bellard
  - Support for multiple incremental snapshots.
39 585f8587 bellard
  - Memory management by reference counts.
40 585f8587 bellard
  - Clusters which have a reference count of one have the bit
41 585f8587 bellard
    QCOW_OFLAG_COPIED to optimize write performance.
42 5fafdf24 ths
  - Size of compressed clusters is stored in sectors to reduce bit usage
43 585f8587 bellard
    in the cluster offsets.
44 585f8587 bellard
  - Support for storing additional data (such as the VM state) in the
45 3b46e624 ths
    snapshots.
46 585f8587 bellard
  - If a backing store is used, the cluster size is not constrained
47 585f8587 bellard
    (could be backported to QCOW).
48 585f8587 bellard
  - L2 tables have always a size of one cluster.
49 585f8587 bellard
*/
50 585f8587 bellard
51 9b80ddf3 aliguori
52 9b80ddf3 aliguori
typedef struct {
53 9b80ddf3 aliguori
    uint32_t magic;
54 9b80ddf3 aliguori
    uint32_t len;
55 9b80ddf3 aliguori
} QCowExtension;
56 21d82ac9 Jeff Cody
57 7c80ab3f Jes Sorensen
#define  QCOW2_EXT_MAGIC_END 0
58 7c80ab3f Jes Sorensen
#define  QCOW2_EXT_MAGIC_BACKING_FORMAT 0xE2792ACA
59 cfcc4c62 Kevin Wolf
#define  QCOW2_EXT_MAGIC_FEATURE_TABLE 0x6803f857
60 9b80ddf3 aliguori
61 7c80ab3f Jes Sorensen
static int qcow2_probe(const uint8_t *buf, int buf_size, const char *filename)
62 585f8587 bellard
{
63 585f8587 bellard
    const QCowHeader *cow_header = (const void *)buf;
64 3b46e624 ths
65 585f8587 bellard
    if (buf_size >= sizeof(QCowHeader) &&
66 585f8587 bellard
        be32_to_cpu(cow_header->magic) == QCOW_MAGIC &&
67 6744cbab Kevin Wolf
        be32_to_cpu(cow_header->version) >= 2)
68 585f8587 bellard
        return 100;
69 585f8587 bellard
    else
70 585f8587 bellard
        return 0;
71 585f8587 bellard
}
72 585f8587 bellard
73 9b80ddf3 aliguori
74 9b80ddf3 aliguori
/* 
75 9b80ddf3 aliguori
 * read qcow2 extension and fill bs
76 9b80ddf3 aliguori
 * start reading from start_offset
77 9b80ddf3 aliguori
 * finish reading upon magic of value 0 or when end_offset reached
78 9b80ddf3 aliguori
 * unknown magic is skipped (future extension this version knows nothing about)
79 9b80ddf3 aliguori
 * return 0 upon success, non-0 otherwise
80 9b80ddf3 aliguori
 */
81 7c80ab3f Jes Sorensen
static int qcow2_read_extensions(BlockDriverState *bs, uint64_t start_offset,
82 cfcc4c62 Kevin Wolf
                                 uint64_t end_offset, void **p_feature_table)
83 9b80ddf3 aliguori
{
84 75bab85c Kevin Wolf
    BDRVQcowState *s = bs->opaque;
85 9b80ddf3 aliguori
    QCowExtension ext;
86 9b80ddf3 aliguori
    uint64_t offset;
87 75bab85c Kevin Wolf
    int ret;
88 9b80ddf3 aliguori
89 9b80ddf3 aliguori
#ifdef DEBUG_EXT
90 7c80ab3f Jes Sorensen
    printf("qcow2_read_extensions: start=%ld end=%ld\n", start_offset, end_offset);
91 9b80ddf3 aliguori
#endif
92 9b80ddf3 aliguori
    offset = start_offset;
93 9b80ddf3 aliguori
    while (offset < end_offset) {
94 9b80ddf3 aliguori
95 9b80ddf3 aliguori
#ifdef DEBUG_EXT
96 9b80ddf3 aliguori
        /* Sanity check */
97 9b80ddf3 aliguori
        if (offset > s->cluster_size)
98 7c80ab3f Jes Sorensen
            printf("qcow2_read_extension: suspicious offset %lu\n", offset);
99 9b80ddf3 aliguori
100 9b2260cb Dong Xu Wang
        printf("attempting to read extended header in offset %lu\n", offset);
101 9b80ddf3 aliguori
#endif
102 9b80ddf3 aliguori
103 66f82cee Kevin Wolf
        if (bdrv_pread(bs->file, offset, &ext, sizeof(ext)) != sizeof(ext)) {
104 7c80ab3f Jes Sorensen
            fprintf(stderr, "qcow2_read_extension: ERROR: "
105 0bfcd599 Blue Swirl
                    "pread fail from offset %" PRIu64 "\n",
106 0bfcd599 Blue Swirl
                    offset);
107 9b80ddf3 aliguori
            return 1;
108 9b80ddf3 aliguori
        }
109 9b80ddf3 aliguori
        be32_to_cpus(&ext.magic);
110 9b80ddf3 aliguori
        be32_to_cpus(&ext.len);
111 9b80ddf3 aliguori
        offset += sizeof(ext);
112 9b80ddf3 aliguori
#ifdef DEBUG_EXT
113 9b80ddf3 aliguori
        printf("ext.magic = 0x%x\n", ext.magic);
114 9b80ddf3 aliguori
#endif
115 64ca6aee Kevin Wolf
        if (ext.len > end_offset - offset) {
116 64ca6aee Kevin Wolf
            error_report("Header extension too large");
117 64ca6aee Kevin Wolf
            return -EINVAL;
118 64ca6aee Kevin Wolf
        }
119 64ca6aee Kevin Wolf
120 9b80ddf3 aliguori
        switch (ext.magic) {
121 7c80ab3f Jes Sorensen
        case QCOW2_EXT_MAGIC_END:
122 9b80ddf3 aliguori
            return 0;
123 f965509c aliguori
124 7c80ab3f Jes Sorensen
        case QCOW2_EXT_MAGIC_BACKING_FORMAT:
125 f965509c aliguori
            if (ext.len >= sizeof(bs->backing_format)) {
126 f965509c aliguori
                fprintf(stderr, "ERROR: ext_backing_format: len=%u too large"
127 4c978075 aliguori
                        " (>=%zu)\n",
128 f965509c aliguori
                        ext.len, sizeof(bs->backing_format));
129 f965509c aliguori
                return 2;
130 f965509c aliguori
            }
131 66f82cee Kevin Wolf
            if (bdrv_pread(bs->file, offset , bs->backing_format,
132 f965509c aliguori
                           ext.len) != ext.len)
133 f965509c aliguori
                return 3;
134 f965509c aliguori
            bs->backing_format[ext.len] = '\0';
135 f965509c aliguori
#ifdef DEBUG_EXT
136 f965509c aliguori
            printf("Qcow2: Got format extension %s\n", bs->backing_format);
137 f965509c aliguori
#endif
138 f965509c aliguori
            break;
139 f965509c aliguori
140 cfcc4c62 Kevin Wolf
        case QCOW2_EXT_MAGIC_FEATURE_TABLE:
141 cfcc4c62 Kevin Wolf
            if (p_feature_table != NULL) {
142 cfcc4c62 Kevin Wolf
                void* feature_table = g_malloc0(ext.len + 2 * sizeof(Qcow2Feature));
143 cfcc4c62 Kevin Wolf
                ret = bdrv_pread(bs->file, offset , feature_table, ext.len);
144 cfcc4c62 Kevin Wolf
                if (ret < 0) {
145 cfcc4c62 Kevin Wolf
                    return ret;
146 cfcc4c62 Kevin Wolf
                }
147 cfcc4c62 Kevin Wolf
148 cfcc4c62 Kevin Wolf
                *p_feature_table = feature_table;
149 cfcc4c62 Kevin Wolf
            }
150 cfcc4c62 Kevin Wolf
            break;
151 cfcc4c62 Kevin Wolf
152 9b80ddf3 aliguori
        default:
153 75bab85c Kevin Wolf
            /* unknown magic - save it in case we need to rewrite the header */
154 75bab85c Kevin Wolf
            {
155 75bab85c Kevin Wolf
                Qcow2UnknownHeaderExtension *uext;
156 75bab85c Kevin Wolf
157 75bab85c Kevin Wolf
                uext = g_malloc0(sizeof(*uext)  + ext.len);
158 75bab85c Kevin Wolf
                uext->magic = ext.magic;
159 75bab85c Kevin Wolf
                uext->len = ext.len;
160 75bab85c Kevin Wolf
                QLIST_INSERT_HEAD(&s->unknown_header_ext, uext, next);
161 75bab85c Kevin Wolf
162 75bab85c Kevin Wolf
                ret = bdrv_pread(bs->file, offset , uext->data, uext->len);
163 75bab85c Kevin Wolf
                if (ret < 0) {
164 75bab85c Kevin Wolf
                    return ret;
165 75bab85c Kevin Wolf
                }
166 75bab85c Kevin Wolf
            }
167 9b80ddf3 aliguori
            break;
168 9b80ddf3 aliguori
        }
169 fd29b4bb Kevin Wolf
170 fd29b4bb Kevin Wolf
        offset += ((ext.len + 7) & ~7);
171 9b80ddf3 aliguori
    }
172 9b80ddf3 aliguori
173 9b80ddf3 aliguori
    return 0;
174 9b80ddf3 aliguori
}
175 9b80ddf3 aliguori
176 75bab85c Kevin Wolf
static void cleanup_unknown_header_ext(BlockDriverState *bs)
177 75bab85c Kevin Wolf
{
178 75bab85c Kevin Wolf
    BDRVQcowState *s = bs->opaque;
179 75bab85c Kevin Wolf
    Qcow2UnknownHeaderExtension *uext, *next;
180 75bab85c Kevin Wolf
181 75bab85c Kevin Wolf
    QLIST_FOREACH_SAFE(uext, &s->unknown_header_ext, next, next) {
182 75bab85c Kevin Wolf
        QLIST_REMOVE(uext, next);
183 75bab85c Kevin Wolf
        g_free(uext);
184 75bab85c Kevin Wolf
    }
185 75bab85c Kevin Wolf
}
186 9b80ddf3 aliguori
187 b9531b6e Stefan Weil
static void GCC_FMT_ATTR(2, 3) report_unsupported(BlockDriverState *bs,
188 b9531b6e Stefan Weil
    const char *fmt, ...)
189 6744cbab Kevin Wolf
{
190 6744cbab Kevin Wolf
    char msg[64];
191 6744cbab Kevin Wolf
    va_list ap;
192 6744cbab Kevin Wolf
193 6744cbab Kevin Wolf
    va_start(ap, fmt);
194 6744cbab Kevin Wolf
    vsnprintf(msg, sizeof(msg), fmt, ap);
195 6744cbab Kevin Wolf
    va_end(ap);
196 6744cbab Kevin Wolf
197 6744cbab Kevin Wolf
    qerror_report(QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,
198 6744cbab Kevin Wolf
        bs->device_name, "qcow2", msg);
199 6744cbab Kevin Wolf
}
200 6744cbab Kevin Wolf
201 cfcc4c62 Kevin Wolf
static void report_unsupported_feature(BlockDriverState *bs,
202 cfcc4c62 Kevin Wolf
    Qcow2Feature *table, uint64_t mask)
203 cfcc4c62 Kevin Wolf
{
204 cfcc4c62 Kevin Wolf
    while (table && table->name[0] != '\0') {
205 cfcc4c62 Kevin Wolf
        if (table->type == QCOW2_FEAT_TYPE_INCOMPATIBLE) {
206 cfcc4c62 Kevin Wolf
            if (mask & (1 << table->bit)) {
207 cfcc4c62 Kevin Wolf
                report_unsupported(bs, "%.46s",table->name);
208 cfcc4c62 Kevin Wolf
                mask &= ~(1 << table->bit);
209 cfcc4c62 Kevin Wolf
            }
210 cfcc4c62 Kevin Wolf
        }
211 cfcc4c62 Kevin Wolf
        table++;
212 cfcc4c62 Kevin Wolf
    }
213 cfcc4c62 Kevin Wolf
214 cfcc4c62 Kevin Wolf
    if (mask) {
215 cfcc4c62 Kevin Wolf
        report_unsupported(bs, "Unknown incompatible feature: %" PRIx64, mask);
216 cfcc4c62 Kevin Wolf
    }
217 cfcc4c62 Kevin Wolf
}
218 cfcc4c62 Kevin Wolf
219 c61d0004 Stefan Hajnoczi
/*
220 bfe8043e Stefan Hajnoczi
 * Sets the dirty bit and flushes afterwards if necessary.
221 bfe8043e Stefan Hajnoczi
 *
222 bfe8043e Stefan Hajnoczi
 * The incompatible_features bit is only set if the image file header was
223 bfe8043e Stefan Hajnoczi
 * updated successfully.  Therefore it is not required to check the return
224 bfe8043e Stefan Hajnoczi
 * value of this function.
225 bfe8043e Stefan Hajnoczi
 */
226 280d3735 Kevin Wolf
int qcow2_mark_dirty(BlockDriverState *bs)
227 bfe8043e Stefan Hajnoczi
{
228 bfe8043e Stefan Hajnoczi
    BDRVQcowState *s = bs->opaque;
229 bfe8043e Stefan Hajnoczi
    uint64_t val;
230 bfe8043e Stefan Hajnoczi
    int ret;
231 bfe8043e Stefan Hajnoczi
232 bfe8043e Stefan Hajnoczi
    assert(s->qcow_version >= 3);
233 bfe8043e Stefan Hajnoczi
234 bfe8043e Stefan Hajnoczi
    if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) {
235 bfe8043e Stefan Hajnoczi
        return 0; /* already dirty */
236 bfe8043e Stefan Hajnoczi
    }
237 bfe8043e Stefan Hajnoczi
238 bfe8043e Stefan Hajnoczi
    val = cpu_to_be64(s->incompatible_features | QCOW2_INCOMPAT_DIRTY);
239 bfe8043e Stefan Hajnoczi
    ret = bdrv_pwrite(bs->file, offsetof(QCowHeader, incompatible_features),
240 bfe8043e Stefan Hajnoczi
                      &val, sizeof(val));
241 bfe8043e Stefan Hajnoczi
    if (ret < 0) {
242 bfe8043e Stefan Hajnoczi
        return ret;
243 bfe8043e Stefan Hajnoczi
    }
244 bfe8043e Stefan Hajnoczi
    ret = bdrv_flush(bs->file);
245 bfe8043e Stefan Hajnoczi
    if (ret < 0) {
246 bfe8043e Stefan Hajnoczi
        return ret;
247 bfe8043e Stefan Hajnoczi
    }
248 bfe8043e Stefan Hajnoczi
249 bfe8043e Stefan Hajnoczi
    /* Only treat image as dirty if the header was updated successfully */
250 bfe8043e Stefan Hajnoczi
    s->incompatible_features |= QCOW2_INCOMPAT_DIRTY;
251 bfe8043e Stefan Hajnoczi
    return 0;
252 bfe8043e Stefan Hajnoczi
}
253 bfe8043e Stefan Hajnoczi
254 bfe8043e Stefan Hajnoczi
/*
255 c61d0004 Stefan Hajnoczi
 * Clears the dirty bit and flushes before if necessary.  Only call this
256 c61d0004 Stefan Hajnoczi
 * function when there are no pending requests, it does not guard against
257 c61d0004 Stefan Hajnoczi
 * concurrent requests dirtying the image.
258 c61d0004 Stefan Hajnoczi
 */
259 c61d0004 Stefan Hajnoczi
static int qcow2_mark_clean(BlockDriverState *bs)
260 c61d0004 Stefan Hajnoczi
{
261 c61d0004 Stefan Hajnoczi
    BDRVQcowState *s = bs->opaque;
262 c61d0004 Stefan Hajnoczi
263 c61d0004 Stefan Hajnoczi
    if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) {
264 c61d0004 Stefan Hajnoczi
        int ret = bdrv_flush(bs);
265 c61d0004 Stefan Hajnoczi
        if (ret < 0) {
266 c61d0004 Stefan Hajnoczi
            return ret;
267 c61d0004 Stefan Hajnoczi
        }
268 c61d0004 Stefan Hajnoczi
269 c61d0004 Stefan Hajnoczi
        s->incompatible_features &= ~QCOW2_INCOMPAT_DIRTY;
270 c61d0004 Stefan Hajnoczi
        return qcow2_update_header(bs);
271 c61d0004 Stefan Hajnoczi
    }
272 c61d0004 Stefan Hajnoczi
    return 0;
273 c61d0004 Stefan Hajnoczi
}
274 c61d0004 Stefan Hajnoczi
275 acbe5982 Stefan Hajnoczi
static int qcow2_check(BlockDriverState *bs, BdrvCheckResult *result,
276 acbe5982 Stefan Hajnoczi
                       BdrvCheckMode fix)
277 acbe5982 Stefan Hajnoczi
{
278 acbe5982 Stefan Hajnoczi
    int ret = qcow2_check_refcounts(bs, result, fix);
279 acbe5982 Stefan Hajnoczi
    if (ret < 0) {
280 acbe5982 Stefan Hajnoczi
        return ret;
281 acbe5982 Stefan Hajnoczi
    }
282 acbe5982 Stefan Hajnoczi
283 acbe5982 Stefan Hajnoczi
    if (fix && result->check_errors == 0 && result->corruptions == 0) {
284 acbe5982 Stefan Hajnoczi
        return qcow2_mark_clean(bs);
285 acbe5982 Stefan Hajnoczi
    }
286 acbe5982 Stefan Hajnoczi
    return ret;
287 acbe5982 Stefan Hajnoczi
}
288 acbe5982 Stefan Hajnoczi
289 74c4510a Kevin Wolf
static QemuOptsList qcow2_runtime_opts = {
290 74c4510a Kevin Wolf
    .name = "qcow2",
291 74c4510a Kevin Wolf
    .head = QTAILQ_HEAD_INITIALIZER(qcow2_runtime_opts.head),
292 74c4510a Kevin Wolf
    .desc = {
293 74c4510a Kevin Wolf
        {
294 74c4510a Kevin Wolf
            .name = "lazy_refcounts",
295 74c4510a Kevin Wolf
            .type = QEMU_OPT_BOOL,
296 74c4510a Kevin Wolf
            .help = "Postpone refcount updates",
297 74c4510a Kevin Wolf
        },
298 74c4510a Kevin Wolf
        { /* end of list */ }
299 74c4510a Kevin Wolf
    },
300 74c4510a Kevin Wolf
};
301 74c4510a Kevin Wolf
302 1a86938f Kevin Wolf
static int qcow2_open(BlockDriverState *bs, QDict *options, int flags)
303 585f8587 bellard
{
304 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
305 6d85a57e Jes Sorensen
    int len, i, ret = 0;
306 585f8587 bellard
    QCowHeader header;
307 74c4510a Kevin Wolf
    QemuOpts *opts;
308 74c4510a Kevin Wolf
    Error *local_err = NULL;
309 9b80ddf3 aliguori
    uint64_t ext_end;
310 585f8587 bellard
311 6d85a57e Jes Sorensen
    ret = bdrv_pread(bs->file, 0, &header, sizeof(header));
312 6d85a57e Jes Sorensen
    if (ret < 0) {
313 585f8587 bellard
        goto fail;
314 6d85a57e Jes Sorensen
    }
315 585f8587 bellard
    be32_to_cpus(&header.magic);
316 585f8587 bellard
    be32_to_cpus(&header.version);
317 585f8587 bellard
    be64_to_cpus(&header.backing_file_offset);
318 585f8587 bellard
    be32_to_cpus(&header.backing_file_size);
319 585f8587 bellard
    be64_to_cpus(&header.size);
320 585f8587 bellard
    be32_to_cpus(&header.cluster_bits);
321 585f8587 bellard
    be32_to_cpus(&header.crypt_method);
322 585f8587 bellard
    be64_to_cpus(&header.l1_table_offset);
323 585f8587 bellard
    be32_to_cpus(&header.l1_size);
324 585f8587 bellard
    be64_to_cpus(&header.refcount_table_offset);
325 585f8587 bellard
    be32_to_cpus(&header.refcount_table_clusters);
326 585f8587 bellard
    be64_to_cpus(&header.snapshots_offset);
327 585f8587 bellard
    be32_to_cpus(&header.nb_snapshots);
328 3b46e624 ths
329 e8cdcec1 Kevin Wolf
    if (header.magic != QCOW_MAGIC) {
330 15bac0d5 Stefan Weil
        ret = -EMEDIUMTYPE;
331 585f8587 bellard
        goto fail;
332 6d85a57e Jes Sorensen
    }
333 6744cbab Kevin Wolf
    if (header.version < 2 || header.version > 3) {
334 6744cbab Kevin Wolf
        report_unsupported(bs, "QCOW version %d", header.version);
335 6744cbab Kevin Wolf
        ret = -ENOTSUP;
336 6744cbab Kevin Wolf
        goto fail;
337 6744cbab Kevin Wolf
    }
338 6744cbab Kevin Wolf
339 6744cbab Kevin Wolf
    s->qcow_version = header.version;
340 6744cbab Kevin Wolf
341 6744cbab Kevin Wolf
    /* Initialise version 3 header fields */
342 6744cbab Kevin Wolf
    if (header.version == 2) {
343 6744cbab Kevin Wolf
        header.incompatible_features    = 0;
344 6744cbab Kevin Wolf
        header.compatible_features      = 0;
345 6744cbab Kevin Wolf
        header.autoclear_features       = 0;
346 6744cbab Kevin Wolf
        header.refcount_order           = 4;
347 6744cbab Kevin Wolf
        header.header_length            = 72;
348 6744cbab Kevin Wolf
    } else {
349 6744cbab Kevin Wolf
        be64_to_cpus(&header.incompatible_features);
350 6744cbab Kevin Wolf
        be64_to_cpus(&header.compatible_features);
351 6744cbab Kevin Wolf
        be64_to_cpus(&header.autoclear_features);
352 6744cbab Kevin Wolf
        be32_to_cpus(&header.refcount_order);
353 6744cbab Kevin Wolf
        be32_to_cpus(&header.header_length);
354 6744cbab Kevin Wolf
    }
355 6744cbab Kevin Wolf
356 6744cbab Kevin Wolf
    if (header.header_length > sizeof(header)) {
357 6744cbab Kevin Wolf
        s->unknown_header_fields_size = header.header_length - sizeof(header);
358 6744cbab Kevin Wolf
        s->unknown_header_fields = g_malloc(s->unknown_header_fields_size);
359 6744cbab Kevin Wolf
        ret = bdrv_pread(bs->file, sizeof(header), s->unknown_header_fields,
360 6744cbab Kevin Wolf
                         s->unknown_header_fields_size);
361 6744cbab Kevin Wolf
        if (ret < 0) {
362 6744cbab Kevin Wolf
            goto fail;
363 6744cbab Kevin Wolf
        }
364 6744cbab Kevin Wolf
    }
365 6744cbab Kevin Wolf
366 cfcc4c62 Kevin Wolf
    if (header.backing_file_offset) {
367 cfcc4c62 Kevin Wolf
        ext_end = header.backing_file_offset;
368 cfcc4c62 Kevin Wolf
    } else {
369 cfcc4c62 Kevin Wolf
        ext_end = 1 << header.cluster_bits;
370 cfcc4c62 Kevin Wolf
    }
371 cfcc4c62 Kevin Wolf
372 6744cbab Kevin Wolf
    /* Handle feature bits */
373 6744cbab Kevin Wolf
    s->incompatible_features    = header.incompatible_features;
374 6744cbab Kevin Wolf
    s->compatible_features      = header.compatible_features;
375 6744cbab Kevin Wolf
    s->autoclear_features       = header.autoclear_features;
376 6744cbab Kevin Wolf
377 c61d0004 Stefan Hajnoczi
    if (s->incompatible_features & ~QCOW2_INCOMPAT_MASK) {
378 cfcc4c62 Kevin Wolf
        void *feature_table = NULL;
379 cfcc4c62 Kevin Wolf
        qcow2_read_extensions(bs, header.header_length, ext_end,
380 cfcc4c62 Kevin Wolf
                              &feature_table);
381 cfcc4c62 Kevin Wolf
        report_unsupported_feature(bs, feature_table,
382 c61d0004 Stefan Hajnoczi
                                   s->incompatible_features &
383 c61d0004 Stefan Hajnoczi
                                   ~QCOW2_INCOMPAT_MASK);
384 6744cbab Kevin Wolf
        ret = -ENOTSUP;
385 6744cbab Kevin Wolf
        goto fail;
386 6744cbab Kevin Wolf
    }
387 6744cbab Kevin Wolf
388 6744cbab Kevin Wolf
    /* Check support for various header values */
389 6744cbab Kevin Wolf
    if (header.refcount_order != 4) {
390 6744cbab Kevin Wolf
        report_unsupported(bs, "%d bit reference counts",
391 6744cbab Kevin Wolf
                           1 << header.refcount_order);
392 e8cdcec1 Kevin Wolf
        ret = -ENOTSUP;
393 e8cdcec1 Kevin Wolf
        goto fail;
394 e8cdcec1 Kevin Wolf
    }
395 6744cbab Kevin Wolf
396 d191d12d Stefan Weil
    if (header.cluster_bits < MIN_CLUSTER_BITS ||
397 6d85a57e Jes Sorensen
        header.cluster_bits > MAX_CLUSTER_BITS) {
398 6d85a57e Jes Sorensen
        ret = -EINVAL;
399 585f8587 bellard
        goto fail;
400 6d85a57e Jes Sorensen
    }
401 6d85a57e Jes Sorensen
    if (header.crypt_method > QCOW_CRYPT_AES) {
402 6d85a57e Jes Sorensen
        ret = -EINVAL;
403 585f8587 bellard
        goto fail;
404 6d85a57e Jes Sorensen
    }
405 585f8587 bellard
    s->crypt_method_header = header.crypt_method;
406 6d85a57e Jes Sorensen
    if (s->crypt_method_header) {
407 585f8587 bellard
        bs->encrypted = 1;
408 6d85a57e Jes Sorensen
    }
409 585f8587 bellard
    s->cluster_bits = header.cluster_bits;
410 585f8587 bellard
    s->cluster_size = 1 << s->cluster_bits;
411 585f8587 bellard
    s->cluster_sectors = 1 << (s->cluster_bits - 9);
412 585f8587 bellard
    s->l2_bits = s->cluster_bits - 3; /* L2 is always one cluster */
413 585f8587 bellard
    s->l2_size = 1 << s->l2_bits;
414 585f8587 bellard
    bs->total_sectors = header.size / 512;
415 585f8587 bellard
    s->csize_shift = (62 - (s->cluster_bits - 8));
416 585f8587 bellard
    s->csize_mask = (1 << (s->cluster_bits - 8)) - 1;
417 585f8587 bellard
    s->cluster_offset_mask = (1LL << s->csize_shift) - 1;
418 585f8587 bellard
    s->refcount_table_offset = header.refcount_table_offset;
419 5fafdf24 ths
    s->refcount_table_size =
420 585f8587 bellard
        header.refcount_table_clusters << (s->cluster_bits - 3);
421 585f8587 bellard
422 585f8587 bellard
    s->snapshots_offset = header.snapshots_offset;
423 585f8587 bellard
    s->nb_snapshots = header.nb_snapshots;
424 585f8587 bellard
425 585f8587 bellard
    /* read the level 1 table */
426 585f8587 bellard
    s->l1_size = header.l1_size;
427 419b19d9 Stefan Hajnoczi
    s->l1_vm_state_index = size_to_l1(s, header.size);
428 585f8587 bellard
    /* the L1 table must contain at least enough entries to put
429 585f8587 bellard
       header.size bytes */
430 6d85a57e Jes Sorensen
    if (s->l1_size < s->l1_vm_state_index) {
431 6d85a57e Jes Sorensen
        ret = -EINVAL;
432 585f8587 bellard
        goto fail;
433 6d85a57e Jes Sorensen
    }
434 585f8587 bellard
    s->l1_table_offset = header.l1_table_offset;
435 d191d12d Stefan Weil
    if (s->l1_size > 0) {
436 7267c094 Anthony Liguori
        s->l1_table = g_malloc0(
437 d191d12d Stefan Weil
            align_offset(s->l1_size * sizeof(uint64_t), 512));
438 6d85a57e Jes Sorensen
        ret = bdrv_pread(bs->file, s->l1_table_offset, s->l1_table,
439 6d85a57e Jes Sorensen
                         s->l1_size * sizeof(uint64_t));
440 6d85a57e Jes Sorensen
        if (ret < 0) {
441 d191d12d Stefan Weil
            goto fail;
442 6d85a57e Jes Sorensen
        }
443 d191d12d Stefan Weil
        for(i = 0;i < s->l1_size; i++) {
444 d191d12d Stefan Weil
            be64_to_cpus(&s->l1_table[i]);
445 d191d12d Stefan Weil
        }
446 585f8587 bellard
    }
447 29c1a730 Kevin Wolf
448 29c1a730 Kevin Wolf
    /* alloc L2 table/refcount block cache */
449 6af4e9ea Paolo Bonzini
    s->l2_table_cache = qcow2_cache_create(bs, L2_CACHE_SIZE);
450 6af4e9ea Paolo Bonzini
    s->refcount_block_cache = qcow2_cache_create(bs, REFCOUNT_CACHE_SIZE);
451 29c1a730 Kevin Wolf
452 7267c094 Anthony Liguori
    s->cluster_cache = g_malloc(s->cluster_size);
453 585f8587 bellard
    /* one more sector for decompressed data alignment */
454 dea43a65 Frediano Ziglio
    s->cluster_data = qemu_blockalign(bs, QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size
455 095a9c58 aliguori
                                  + 512);
456 585f8587 bellard
    s->cluster_cache_offset = -1;
457 06d9260f Anthony Liguori
    s->flags = flags;
458 3b46e624 ths
459 6d85a57e Jes Sorensen
    ret = qcow2_refcount_init(bs);
460 6d85a57e Jes Sorensen
    if (ret != 0) {
461 585f8587 bellard
        goto fail;
462 6d85a57e Jes Sorensen
    }
463 585f8587 bellard
464 72cf2d4f Blue Swirl
    QLIST_INIT(&s->cluster_allocs);
465 f214978a Kevin Wolf
466 9b80ddf3 aliguori
    /* read qcow2 extensions */
467 cfcc4c62 Kevin Wolf
    if (qcow2_read_extensions(bs, header.header_length, ext_end, NULL)) {
468 6d85a57e Jes Sorensen
        ret = -EINVAL;
469 9b80ddf3 aliguori
        goto fail;
470 6d85a57e Jes Sorensen
    }
471 9b80ddf3 aliguori
472 585f8587 bellard
    /* read the backing file name */
473 585f8587 bellard
    if (header.backing_file_offset != 0) {
474 585f8587 bellard
        len = header.backing_file_size;
475 6d85a57e Jes Sorensen
        if (len > 1023) {
476 585f8587 bellard
            len = 1023;
477 6d85a57e Jes Sorensen
        }
478 6d85a57e Jes Sorensen
        ret = bdrv_pread(bs->file, header.backing_file_offset,
479 6d85a57e Jes Sorensen
                         bs->backing_file, len);
480 6d85a57e Jes Sorensen
        if (ret < 0) {
481 585f8587 bellard
            goto fail;
482 6d85a57e Jes Sorensen
        }
483 585f8587 bellard
        bs->backing_file[len] = '\0';
484 585f8587 bellard
    }
485 42deb29f Kevin Wolf
486 42deb29f Kevin Wolf
    ret = qcow2_read_snapshots(bs);
487 42deb29f Kevin Wolf
    if (ret < 0) {
488 585f8587 bellard
        goto fail;
489 6d85a57e Jes Sorensen
    }
490 585f8587 bellard
491 af7b708d Stefan Hajnoczi
    /* Clear unknown autoclear feature bits */
492 af7b708d Stefan Hajnoczi
    if (!bs->read_only && s->autoclear_features != 0) {
493 af7b708d Stefan Hajnoczi
        s->autoclear_features = 0;
494 af7b708d Stefan Hajnoczi
        ret = qcow2_update_header(bs);
495 af7b708d Stefan Hajnoczi
        if (ret < 0) {
496 af7b708d Stefan Hajnoczi
            goto fail;
497 af7b708d Stefan Hajnoczi
        }
498 af7b708d Stefan Hajnoczi
    }
499 af7b708d Stefan Hajnoczi
500 68d100e9 Kevin Wolf
    /* Initialise locks */
501 68d100e9 Kevin Wolf
    qemu_co_mutex_init(&s->lock);
502 68d100e9 Kevin Wolf
503 c61d0004 Stefan Hajnoczi
    /* Repair image if dirty */
504 058f8f16 Stefan Hajnoczi
    if (!(flags & BDRV_O_CHECK) && !bs->read_only &&
505 058f8f16 Stefan Hajnoczi
        (s->incompatible_features & QCOW2_INCOMPAT_DIRTY)) {
506 c61d0004 Stefan Hajnoczi
        BdrvCheckResult result = {0};
507 c61d0004 Stefan Hajnoczi
508 acbe5982 Stefan Hajnoczi
        ret = qcow2_check(bs, &result, BDRV_FIX_ERRORS);
509 c61d0004 Stefan Hajnoczi
        if (ret < 0) {
510 c61d0004 Stefan Hajnoczi
            goto fail;
511 c61d0004 Stefan Hajnoczi
        }
512 c61d0004 Stefan Hajnoczi
    }
513 c61d0004 Stefan Hajnoczi
514 74c4510a Kevin Wolf
    /* Enable lazy_refcounts according to image and command line options */
515 74c4510a Kevin Wolf
    opts = qemu_opts_create_nofail(&qcow2_runtime_opts);
516 74c4510a Kevin Wolf
    qemu_opts_absorb_qdict(opts, options, &local_err);
517 74c4510a Kevin Wolf
    if (error_is_set(&local_err)) {
518 74c4510a Kevin Wolf
        qerror_report_err(local_err);
519 74c4510a Kevin Wolf
        error_free(local_err);
520 74c4510a Kevin Wolf
        ret = -EINVAL;
521 74c4510a Kevin Wolf
        goto fail;
522 74c4510a Kevin Wolf
    }
523 74c4510a Kevin Wolf
524 acdfb480 Kevin Wolf
    s->use_lazy_refcounts = qemu_opt_get_bool(opts, QCOW2_OPT_LAZY_REFCOUNTS,
525 74c4510a Kevin Wolf
        (s->compatible_features & QCOW2_COMPAT_LAZY_REFCOUNTS));
526 74c4510a Kevin Wolf
527 74c4510a Kevin Wolf
    qemu_opts_del(opts);
528 74c4510a Kevin Wolf
529 74c4510a Kevin Wolf
    if (s->use_lazy_refcounts && s->qcow_version < 3) {
530 74c4510a Kevin Wolf
        qerror_report(ERROR_CLASS_GENERIC_ERROR, "Lazy refcounts require "
531 74c4510a Kevin Wolf
            "a qcow2 image with at least qemu 1.1 compatibility level");
532 74c4510a Kevin Wolf
        ret = -EINVAL;
533 74c4510a Kevin Wolf
        goto fail;
534 74c4510a Kevin Wolf
    }
535 74c4510a Kevin Wolf
536 585f8587 bellard
#ifdef DEBUG_ALLOC
537 6cbc3031 Philipp Hahn
    {
538 6cbc3031 Philipp Hahn
        BdrvCheckResult result = {0};
539 b35278f7 Stefan Hajnoczi
        qcow2_check_refcounts(bs, &result, 0);
540 6cbc3031 Philipp Hahn
    }
541 585f8587 bellard
#endif
542 6d85a57e Jes Sorensen
    return ret;
543 585f8587 bellard
544 585f8587 bellard
 fail:
545 6744cbab Kevin Wolf
    g_free(s->unknown_header_fields);
546 75bab85c Kevin Wolf
    cleanup_unknown_header_ext(bs);
547 ed6ccf0f Kevin Wolf
    qcow2_free_snapshots(bs);
548 ed6ccf0f Kevin Wolf
    qcow2_refcount_close(bs);
549 7267c094 Anthony Liguori
    g_free(s->l1_table);
550 29c1a730 Kevin Wolf
    if (s->l2_table_cache) {
551 29c1a730 Kevin Wolf
        qcow2_cache_destroy(bs, s->l2_table_cache);
552 29c1a730 Kevin Wolf
    }
553 7267c094 Anthony Liguori
    g_free(s->cluster_cache);
554 dea43a65 Frediano Ziglio
    qemu_vfree(s->cluster_data);
555 6d85a57e Jes Sorensen
    return ret;
556 585f8587 bellard
}
557 585f8587 bellard
558 7c80ab3f Jes Sorensen
static int qcow2_set_key(BlockDriverState *bs, const char *key)
559 585f8587 bellard
{
560 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
561 585f8587 bellard
    uint8_t keybuf[16];
562 585f8587 bellard
    int len, i;
563 3b46e624 ths
564 585f8587 bellard
    memset(keybuf, 0, 16);
565 585f8587 bellard
    len = strlen(key);
566 585f8587 bellard
    if (len > 16)
567 585f8587 bellard
        len = 16;
568 585f8587 bellard
    /* XXX: we could compress the chars to 7 bits to increase
569 585f8587 bellard
       entropy */
570 585f8587 bellard
    for(i = 0;i < len;i++) {
571 585f8587 bellard
        keybuf[i] = key[i];
572 585f8587 bellard
    }
573 585f8587 bellard
    s->crypt_method = s->crypt_method_header;
574 585f8587 bellard
575 585f8587 bellard
    if (AES_set_encrypt_key(keybuf, 128, &s->aes_encrypt_key) != 0)
576 585f8587 bellard
        return -1;
577 585f8587 bellard
    if (AES_set_decrypt_key(keybuf, 128, &s->aes_decrypt_key) != 0)
578 585f8587 bellard
        return -1;
579 585f8587 bellard
#if 0
580 585f8587 bellard
    /* test */
581 585f8587 bellard
    {
582 585f8587 bellard
        uint8_t in[16];
583 585f8587 bellard
        uint8_t out[16];
584 585f8587 bellard
        uint8_t tmp[16];
585 585f8587 bellard
        for(i=0;i<16;i++)
586 585f8587 bellard
            in[i] = i;
587 585f8587 bellard
        AES_encrypt(in, tmp, &s->aes_encrypt_key);
588 585f8587 bellard
        AES_decrypt(tmp, out, &s->aes_decrypt_key);
589 585f8587 bellard
        for(i = 0; i < 16; i++)
590 585f8587 bellard
            printf(" %02x", tmp[i]);
591 585f8587 bellard
        printf("\n");
592 585f8587 bellard
        for(i = 0; i < 16; i++)
593 585f8587 bellard
            printf(" %02x", out[i]);
594 585f8587 bellard
        printf("\n");
595 585f8587 bellard
    }
596 585f8587 bellard
#endif
597 585f8587 bellard
    return 0;
598 585f8587 bellard
}
599 585f8587 bellard
600 21d82ac9 Jeff Cody
/* We have nothing to do for QCOW2 reopen, stubs just return
601 21d82ac9 Jeff Cody
 * success */
602 21d82ac9 Jeff Cody
static int qcow2_reopen_prepare(BDRVReopenState *state,
603 21d82ac9 Jeff Cody
                                BlockReopenQueue *queue, Error **errp)
604 21d82ac9 Jeff Cody
{
605 21d82ac9 Jeff Cody
    return 0;
606 21d82ac9 Jeff Cody
}
607 21d82ac9 Jeff Cody
608 f8a2e5e3 Stefan Hajnoczi
static int coroutine_fn qcow2_co_is_allocated(BlockDriverState *bs,
609 f8a2e5e3 Stefan Hajnoczi
        int64_t sector_num, int nb_sectors, int *pnum)
610 585f8587 bellard
{
611 f8a2e5e3 Stefan Hajnoczi
    BDRVQcowState *s = bs->opaque;
612 585f8587 bellard
    uint64_t cluster_offset;
613 1c46efaa Kevin Wolf
    int ret;
614 585f8587 bellard
615 095a9c58 aliguori
    *pnum = nb_sectors;
616 f8a2e5e3 Stefan Hajnoczi
    /* FIXME We can get errors here, but the bdrv_co_is_allocated interface
617 f8a2e5e3 Stefan Hajnoczi
     * can't pass them on today */
618 f8a2e5e3 Stefan Hajnoczi
    qemu_co_mutex_lock(&s->lock);
619 1c46efaa Kevin Wolf
    ret = qcow2_get_cluster_offset(bs, sector_num << 9, pnum, &cluster_offset);
620 f8a2e5e3 Stefan Hajnoczi
    qemu_co_mutex_unlock(&s->lock);
621 1c46efaa Kevin Wolf
    if (ret < 0) {
622 1c46efaa Kevin Wolf
        *pnum = 0;
623 1c46efaa Kevin Wolf
    }
624 095a9c58 aliguori
625 381b487d Paolo Bonzini
    return (cluster_offset != 0) || (ret == QCOW2_CLUSTER_ZERO);
626 585f8587 bellard
}
627 585f8587 bellard
628 a9465922 bellard
/* handle reading after the end of the backing file */
629 bd28f835 Kevin Wolf
int qcow2_backing_read1(BlockDriverState *bs, QEMUIOVector *qiov,
630 bd28f835 Kevin Wolf
                  int64_t sector_num, int nb_sectors)
631 a9465922 bellard
{
632 a9465922 bellard
    int n1;
633 a9465922 bellard
    if ((sector_num + nb_sectors) <= bs->total_sectors)
634 a9465922 bellard
        return nb_sectors;
635 a9465922 bellard
    if (sector_num >= bs->total_sectors)
636 a9465922 bellard
        n1 = 0;
637 a9465922 bellard
    else
638 a9465922 bellard
        n1 = bs->total_sectors - sector_num;
639 bd28f835 Kevin Wolf
640 3d9b4925 Michael Tokarev
    qemu_iovec_memset(qiov, 512 * n1, 0, 512 * (nb_sectors - n1));
641 bd28f835 Kevin Wolf
642 a9465922 bellard
    return n1;
643 a9465922 bellard
}
644 a9465922 bellard
645 a968168c Dong Xu Wang
static coroutine_fn int qcow2_co_readv(BlockDriverState *bs, int64_t sector_num,
646 3fc48d09 Frediano Ziglio
                          int remaining_sectors, QEMUIOVector *qiov)
647 585f8587 bellard
{
648 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
649 a9465922 bellard
    int index_in_cluster, n1;
650 68d100e9 Kevin Wolf
    int ret;
651 faf575c1 Frediano Ziglio
    int cur_nr_sectors; /* number of sectors in current iteration */
652 c2bdd990 Frediano Ziglio
    uint64_t cluster_offset = 0;
653 3fc48d09 Frediano Ziglio
    uint64_t bytes_done = 0;
654 3fc48d09 Frediano Ziglio
    QEMUIOVector hd_qiov;
655 3fc48d09 Frediano Ziglio
    uint8_t *cluster_data = NULL;
656 585f8587 bellard
657 3fc48d09 Frediano Ziglio
    qemu_iovec_init(&hd_qiov, qiov->niov);
658 3fc48d09 Frediano Ziglio
659 3fc48d09 Frediano Ziglio
    qemu_co_mutex_lock(&s->lock);
660 3fc48d09 Frediano Ziglio
661 3fc48d09 Frediano Ziglio
    while (remaining_sectors != 0) {
662 bd28f835 Kevin Wolf
663 5ebaa27e Frediano Ziglio
        /* prepare next request */
664 3fc48d09 Frediano Ziglio
        cur_nr_sectors = remaining_sectors;
665 5ebaa27e Frediano Ziglio
        if (s->crypt_method) {
666 5ebaa27e Frediano Ziglio
            cur_nr_sectors = MIN(cur_nr_sectors,
667 5ebaa27e Frediano Ziglio
                QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors);
668 585f8587 bellard
        }
669 5ebaa27e Frediano Ziglio
670 3fc48d09 Frediano Ziglio
        ret = qcow2_get_cluster_offset(bs, sector_num << 9,
671 5ebaa27e Frediano Ziglio
            &cur_nr_sectors, &cluster_offset);
672 8af36488 Kevin Wolf
        if (ret < 0) {
673 3fc48d09 Frediano Ziglio
            goto fail;
674 8af36488 Kevin Wolf
        }
675 bd28f835 Kevin Wolf
676 3fc48d09 Frediano Ziglio
        index_in_cluster = sector_num & (s->cluster_sectors - 1);
677 c87c0672 aliguori
678 3fc48d09 Frediano Ziglio
        qemu_iovec_reset(&hd_qiov);
679 1b093c48 Michael Tokarev
        qemu_iovec_concat(&hd_qiov, qiov, bytes_done,
680 5ebaa27e Frediano Ziglio
            cur_nr_sectors * 512);
681 5ebaa27e Frediano Ziglio
682 68d000a3 Kevin Wolf
        switch (ret) {
683 68d000a3 Kevin Wolf
        case QCOW2_CLUSTER_UNALLOCATED:
684 5ebaa27e Frediano Ziglio
685 5ebaa27e Frediano Ziglio
            if (bs->backing_hd) {
686 5ebaa27e Frediano Ziglio
                /* read from the base image */
687 3fc48d09 Frediano Ziglio
                n1 = qcow2_backing_read1(bs->backing_hd, &hd_qiov,
688 3fc48d09 Frediano Ziglio
                    sector_num, cur_nr_sectors);
689 5ebaa27e Frediano Ziglio
                if (n1 > 0) {
690 5ebaa27e Frediano Ziglio
                    BLKDBG_EVENT(bs->file, BLKDBG_READ_BACKING_AIO);
691 5ebaa27e Frediano Ziglio
                    qemu_co_mutex_unlock(&s->lock);
692 3fc48d09 Frediano Ziglio
                    ret = bdrv_co_readv(bs->backing_hd, sector_num,
693 3fc48d09 Frediano Ziglio
                                        n1, &hd_qiov);
694 5ebaa27e Frediano Ziglio
                    qemu_co_mutex_lock(&s->lock);
695 5ebaa27e Frediano Ziglio
                    if (ret < 0) {
696 3fc48d09 Frediano Ziglio
                        goto fail;
697 5ebaa27e Frediano Ziglio
                    }
698 5ebaa27e Frediano Ziglio
                }
699 5ebaa27e Frediano Ziglio
            } else {
700 5ebaa27e Frediano Ziglio
                /* Note: in this case, no need to wait */
701 3d9b4925 Michael Tokarev
                qemu_iovec_memset(&hd_qiov, 0, 0, 512 * cur_nr_sectors);
702 5ebaa27e Frediano Ziglio
            }
703 68d000a3 Kevin Wolf
            break;
704 68d000a3 Kevin Wolf
705 6377af48 Kevin Wolf
        case QCOW2_CLUSTER_ZERO:
706 3d9b4925 Michael Tokarev
            qemu_iovec_memset(&hd_qiov, 0, 0, 512 * cur_nr_sectors);
707 6377af48 Kevin Wolf
            break;
708 6377af48 Kevin Wolf
709 68d000a3 Kevin Wolf
        case QCOW2_CLUSTER_COMPRESSED:
710 5ebaa27e Frediano Ziglio
            /* add AIO support for compressed blocks ? */
711 5ebaa27e Frediano Ziglio
            ret = qcow2_decompress_cluster(bs, cluster_offset);
712 5ebaa27e Frediano Ziglio
            if (ret < 0) {
713 3fc48d09 Frediano Ziglio
                goto fail;
714 bd28f835 Kevin Wolf
            }
715 bd28f835 Kevin Wolf
716 03396148 Michael Tokarev
            qemu_iovec_from_buf(&hd_qiov, 0,
717 5ebaa27e Frediano Ziglio
                s->cluster_cache + index_in_cluster * 512,
718 faf575c1 Frediano Ziglio
                512 * cur_nr_sectors);
719 68d000a3 Kevin Wolf
            break;
720 68d000a3 Kevin Wolf
721 68d000a3 Kevin Wolf
        case QCOW2_CLUSTER_NORMAL:
722 5ebaa27e Frediano Ziglio
            if ((cluster_offset & 511) != 0) {
723 3fc48d09 Frediano Ziglio
                ret = -EIO;
724 3fc48d09 Frediano Ziglio
                goto fail;
725 5ebaa27e Frediano Ziglio
            }
726 bd28f835 Kevin Wolf
727 5ebaa27e Frediano Ziglio
            if (s->crypt_method) {
728 5ebaa27e Frediano Ziglio
                /*
729 5ebaa27e Frediano Ziglio
                 * For encrypted images, read everything into a temporary
730 5ebaa27e Frediano Ziglio
                 * contiguous buffer on which the AES functions can work.
731 5ebaa27e Frediano Ziglio
                 */
732 3fc48d09 Frediano Ziglio
                if (!cluster_data) {
733 3fc48d09 Frediano Ziglio
                    cluster_data =
734 dea43a65 Frediano Ziglio
                        qemu_blockalign(bs, QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size);
735 5ebaa27e Frediano Ziglio
                }
736 5ebaa27e Frediano Ziglio
737 5ebaa27e Frediano Ziglio
                assert(cur_nr_sectors <=
738 5ebaa27e Frediano Ziglio
                    QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors);
739 3fc48d09 Frediano Ziglio
                qemu_iovec_reset(&hd_qiov);
740 3fc48d09 Frediano Ziglio
                qemu_iovec_add(&hd_qiov, cluster_data,
741 5ebaa27e Frediano Ziglio
                    512 * cur_nr_sectors);
742 5ebaa27e Frediano Ziglio
            }
743 5ebaa27e Frediano Ziglio
744 5ebaa27e Frediano Ziglio
            BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO);
745 5ebaa27e Frediano Ziglio
            qemu_co_mutex_unlock(&s->lock);
746 5ebaa27e Frediano Ziglio
            ret = bdrv_co_readv(bs->file,
747 5ebaa27e Frediano Ziglio
                                (cluster_offset >> 9) + index_in_cluster,
748 3fc48d09 Frediano Ziglio
                                cur_nr_sectors, &hd_qiov);
749 5ebaa27e Frediano Ziglio
            qemu_co_mutex_lock(&s->lock);
750 5ebaa27e Frediano Ziglio
            if (ret < 0) {
751 3fc48d09 Frediano Ziglio
                goto fail;
752 5ebaa27e Frediano Ziglio
            }
753 5ebaa27e Frediano Ziglio
            if (s->crypt_method) {
754 3fc48d09 Frediano Ziglio
                qcow2_encrypt_sectors(s, sector_num,  cluster_data,
755 3fc48d09 Frediano Ziglio
                    cluster_data, cur_nr_sectors, 0, &s->aes_decrypt_key);
756 03396148 Michael Tokarev
                qemu_iovec_from_buf(qiov, bytes_done,
757 03396148 Michael Tokarev
                    cluster_data, 512 * cur_nr_sectors);
758 5ebaa27e Frediano Ziglio
            }
759 68d000a3 Kevin Wolf
            break;
760 68d000a3 Kevin Wolf
761 68d000a3 Kevin Wolf
        default:
762 68d000a3 Kevin Wolf
            g_assert_not_reached();
763 68d000a3 Kevin Wolf
            ret = -EIO;
764 68d000a3 Kevin Wolf
            goto fail;
765 faf575c1 Frediano Ziglio
        }
766 f141eafe aliguori
767 3fc48d09 Frediano Ziglio
        remaining_sectors -= cur_nr_sectors;
768 3fc48d09 Frediano Ziglio
        sector_num += cur_nr_sectors;
769 3fc48d09 Frediano Ziglio
        bytes_done += cur_nr_sectors * 512;
770 5ebaa27e Frediano Ziglio
    }
771 3fc48d09 Frediano Ziglio
    ret = 0;
772 faf575c1 Frediano Ziglio
773 3fc48d09 Frediano Ziglio
fail:
774 68d100e9 Kevin Wolf
    qemu_co_mutex_unlock(&s->lock);
775 42496d62 Kevin Wolf
776 3fc48d09 Frediano Ziglio
    qemu_iovec_destroy(&hd_qiov);
777 dea43a65 Frediano Ziglio
    qemu_vfree(cluster_data);
778 68d100e9 Kevin Wolf
779 68d100e9 Kevin Wolf
    return ret;
780 585f8587 bellard
}
781 585f8587 bellard
782 a968168c Dong Xu Wang
static coroutine_fn int qcow2_co_writev(BlockDriverState *bs,
783 3fc48d09 Frediano Ziglio
                           int64_t sector_num,
784 3fc48d09 Frediano Ziglio
                           int remaining_sectors,
785 3fc48d09 Frediano Ziglio
                           QEMUIOVector *qiov)
786 585f8587 bellard
{
787 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
788 585f8587 bellard
    int index_in_cluster;
789 095a9c58 aliguori
    int n_end;
790 68d100e9 Kevin Wolf
    int ret;
791 faf575c1 Frediano Ziglio
    int cur_nr_sectors; /* number of sectors in current iteration */
792 c2bdd990 Frediano Ziglio
    uint64_t cluster_offset;
793 3fc48d09 Frediano Ziglio
    QEMUIOVector hd_qiov;
794 3fc48d09 Frediano Ziglio
    uint64_t bytes_done = 0;
795 3fc48d09 Frediano Ziglio
    uint8_t *cluster_data = NULL;
796 8d2497c3 Kevin Wolf
    QCowL2Meta *l2meta = NULL;
797 c2271403 Frediano Ziglio
798 3cce16f4 Kevin Wolf
    trace_qcow2_writev_start_req(qemu_coroutine_self(), sector_num,
799 3cce16f4 Kevin Wolf
                                 remaining_sectors);
800 3cce16f4 Kevin Wolf
801 3fc48d09 Frediano Ziglio
    qemu_iovec_init(&hd_qiov, qiov->niov);
802 3fc48d09 Frediano Ziglio
803 3fc48d09 Frediano Ziglio
    s->cluster_cache_offset = -1; /* disable compressed cache */
804 3b46e624 ths
805 3fc48d09 Frediano Ziglio
    qemu_co_mutex_lock(&s->lock);
806 3fc48d09 Frediano Ziglio
807 3fc48d09 Frediano Ziglio
    while (remaining_sectors != 0) {
808 3fc48d09 Frediano Ziglio
809 f50f88b9 Kevin Wolf
        l2meta = NULL;
810 cf5c1a23 Kevin Wolf
811 3cce16f4 Kevin Wolf
        trace_qcow2_writev_start_part(qemu_coroutine_self());
812 3fc48d09 Frediano Ziglio
        index_in_cluster = sector_num & (s->cluster_sectors - 1);
813 3fc48d09 Frediano Ziglio
        n_end = index_in_cluster + remaining_sectors;
814 5ebaa27e Frediano Ziglio
        if (s->crypt_method &&
815 5ebaa27e Frediano Ziglio
            n_end > QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors) {
816 5ebaa27e Frediano Ziglio
            n_end = QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors;
817 5ebaa27e Frediano Ziglio
        }
818 095a9c58 aliguori
819 3fc48d09 Frediano Ziglio
        ret = qcow2_alloc_cluster_offset(bs, sector_num << 9,
820 f50f88b9 Kevin Wolf
            index_in_cluster, n_end, &cur_nr_sectors, &cluster_offset, &l2meta);
821 5ebaa27e Frediano Ziglio
        if (ret < 0) {
822 3fc48d09 Frediano Ziglio
            goto fail;
823 5ebaa27e Frediano Ziglio
        }
824 148da7ea Kevin Wolf
825 5ebaa27e Frediano Ziglio
        assert((cluster_offset & 511) == 0);
826 148da7ea Kevin Wolf
827 3fc48d09 Frediano Ziglio
        qemu_iovec_reset(&hd_qiov);
828 1b093c48 Michael Tokarev
        qemu_iovec_concat(&hd_qiov, qiov, bytes_done,
829 5ebaa27e Frediano Ziglio
            cur_nr_sectors * 512);
830 6f5f060b Kevin Wolf
831 5ebaa27e Frediano Ziglio
        if (s->crypt_method) {
832 3fc48d09 Frediano Ziglio
            if (!cluster_data) {
833 dea43a65 Frediano Ziglio
                cluster_data = qemu_blockalign(bs, QCOW_MAX_CRYPT_CLUSTERS *
834 5ebaa27e Frediano Ziglio
                                                 s->cluster_size);
835 5ebaa27e Frediano Ziglio
            }
836 6f5f060b Kevin Wolf
837 3fc48d09 Frediano Ziglio
            assert(hd_qiov.size <=
838 5ebaa27e Frediano Ziglio
                   QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size);
839 d5e6b161 Michael Tokarev
            qemu_iovec_to_buf(&hd_qiov, 0, cluster_data, hd_qiov.size);
840 6f5f060b Kevin Wolf
841 3fc48d09 Frediano Ziglio
            qcow2_encrypt_sectors(s, sector_num, cluster_data,
842 3fc48d09 Frediano Ziglio
                cluster_data, cur_nr_sectors, 1, &s->aes_encrypt_key);
843 6f5f060b Kevin Wolf
844 3fc48d09 Frediano Ziglio
            qemu_iovec_reset(&hd_qiov);
845 3fc48d09 Frediano Ziglio
            qemu_iovec_add(&hd_qiov, cluster_data,
846 5ebaa27e Frediano Ziglio
                cur_nr_sectors * 512);
847 5ebaa27e Frediano Ziglio
        }
848 6f5f060b Kevin Wolf
849 5ebaa27e Frediano Ziglio
        qemu_co_mutex_unlock(&s->lock);
850 67a7a0eb Kevin Wolf
        BLKDBG_EVENT(bs->file, BLKDBG_WRITE_AIO);
851 3cce16f4 Kevin Wolf
        trace_qcow2_writev_data(qemu_coroutine_self(),
852 3cce16f4 Kevin Wolf
                                (cluster_offset >> 9) + index_in_cluster);
853 5ebaa27e Frediano Ziglio
        ret = bdrv_co_writev(bs->file,
854 5ebaa27e Frediano Ziglio
                             (cluster_offset >> 9) + index_in_cluster,
855 3fc48d09 Frediano Ziglio
                             cur_nr_sectors, &hd_qiov);
856 5ebaa27e Frediano Ziglio
        qemu_co_mutex_lock(&s->lock);
857 5ebaa27e Frediano Ziglio
        if (ret < 0) {
858 3fc48d09 Frediano Ziglio
            goto fail;
859 5ebaa27e Frediano Ziglio
        }
860 f141eafe aliguori
861 88c6588c Kevin Wolf
        while (l2meta != NULL) {
862 88c6588c Kevin Wolf
            QCowL2Meta *next;
863 88c6588c Kevin Wolf
864 f50f88b9 Kevin Wolf
            ret = qcow2_alloc_cluster_link_l2(bs, l2meta);
865 f50f88b9 Kevin Wolf
            if (ret < 0) {
866 f50f88b9 Kevin Wolf
                goto fail;
867 f50f88b9 Kevin Wolf
            }
868 faf575c1 Frediano Ziglio
869 4e95314e Kevin Wolf
            /* Take the request off the list of running requests */
870 4e95314e Kevin Wolf
            if (l2meta->nb_clusters != 0) {
871 4e95314e Kevin Wolf
                QLIST_REMOVE(l2meta, next_in_flight);
872 4e95314e Kevin Wolf
            }
873 4e95314e Kevin Wolf
874 4e95314e Kevin Wolf
            qemu_co_queue_restart_all(&l2meta->dependent_requests);
875 4e95314e Kevin Wolf
876 88c6588c Kevin Wolf
            next = l2meta->next;
877 f50f88b9 Kevin Wolf
            g_free(l2meta);
878 88c6588c Kevin Wolf
            l2meta = next;
879 f50f88b9 Kevin Wolf
        }
880 0fa9131a Kevin Wolf
881 3fc48d09 Frediano Ziglio
        remaining_sectors -= cur_nr_sectors;
882 3fc48d09 Frediano Ziglio
        sector_num += cur_nr_sectors;
883 3fc48d09 Frediano Ziglio
        bytes_done += cur_nr_sectors * 512;
884 3cce16f4 Kevin Wolf
        trace_qcow2_writev_done_part(qemu_coroutine_self(), cur_nr_sectors);
885 5ebaa27e Frediano Ziglio
    }
886 3fc48d09 Frediano Ziglio
    ret = 0;
887 faf575c1 Frediano Ziglio
888 3fc48d09 Frediano Ziglio
fail:
889 4e95314e Kevin Wolf
    qemu_co_mutex_unlock(&s->lock);
890 4e95314e Kevin Wolf
891 88c6588c Kevin Wolf
    while (l2meta != NULL) {
892 88c6588c Kevin Wolf
        QCowL2Meta *next;
893 88c6588c Kevin Wolf
894 4e95314e Kevin Wolf
        if (l2meta->nb_clusters != 0) {
895 4e95314e Kevin Wolf
            QLIST_REMOVE(l2meta, next_in_flight);
896 4e95314e Kevin Wolf
        }
897 4e95314e Kevin Wolf
        qemu_co_queue_restart_all(&l2meta->dependent_requests);
898 88c6588c Kevin Wolf
899 88c6588c Kevin Wolf
        next = l2meta->next;
900 cf5c1a23 Kevin Wolf
        g_free(l2meta);
901 88c6588c Kevin Wolf
        l2meta = next;
902 cf5c1a23 Kevin Wolf
    }
903 0fa9131a Kevin Wolf
904 3fc48d09 Frediano Ziglio
    qemu_iovec_destroy(&hd_qiov);
905 dea43a65 Frediano Ziglio
    qemu_vfree(cluster_data);
906 3cce16f4 Kevin Wolf
    trace_qcow2_writev_done_req(qemu_coroutine_self(), ret);
907 42496d62 Kevin Wolf
908 68d100e9 Kevin Wolf
    return ret;
909 585f8587 bellard
}
910 585f8587 bellard
911 7c80ab3f Jes Sorensen
static void qcow2_close(BlockDriverState *bs)
912 585f8587 bellard
{
913 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
914 7267c094 Anthony Liguori
    g_free(s->l1_table);
915 29c1a730 Kevin Wolf
916 29c1a730 Kevin Wolf
    qcow2_cache_flush(bs, s->l2_table_cache);
917 29c1a730 Kevin Wolf
    qcow2_cache_flush(bs, s->refcount_block_cache);
918 29c1a730 Kevin Wolf
919 c61d0004 Stefan Hajnoczi
    qcow2_mark_clean(bs);
920 c61d0004 Stefan Hajnoczi
921 29c1a730 Kevin Wolf
    qcow2_cache_destroy(bs, s->l2_table_cache);
922 29c1a730 Kevin Wolf
    qcow2_cache_destroy(bs, s->refcount_block_cache);
923 29c1a730 Kevin Wolf
924 6744cbab Kevin Wolf
    g_free(s->unknown_header_fields);
925 75bab85c Kevin Wolf
    cleanup_unknown_header_ext(bs);
926 6744cbab Kevin Wolf
927 7267c094 Anthony Liguori
    g_free(s->cluster_cache);
928 dea43a65 Frediano Ziglio
    qemu_vfree(s->cluster_data);
929 ed6ccf0f Kevin Wolf
    qcow2_refcount_close(bs);
930 28c1202b Li Zhi Hui
    qcow2_free_snapshots(bs);
931 585f8587 bellard
}
932 585f8587 bellard
933 06d9260f Anthony Liguori
static void qcow2_invalidate_cache(BlockDriverState *bs)
934 06d9260f Anthony Liguori
{
935 06d9260f Anthony Liguori
    BDRVQcowState *s = bs->opaque;
936 06d9260f Anthony Liguori
    int flags = s->flags;
937 06d9260f Anthony Liguori
    AES_KEY aes_encrypt_key;
938 06d9260f Anthony Liguori
    AES_KEY aes_decrypt_key;
939 06d9260f Anthony Liguori
    uint32_t crypt_method = 0;
940 acdfb480 Kevin Wolf
    QDict *options;
941 06d9260f Anthony Liguori
942 06d9260f Anthony Liguori
    /*
943 06d9260f Anthony Liguori
     * Backing files are read-only which makes all of their metadata immutable,
944 06d9260f Anthony Liguori
     * that means we don't have to worry about reopening them here.
945 06d9260f Anthony Liguori
     */
946 06d9260f Anthony Liguori
947 06d9260f Anthony Liguori
    if (s->crypt_method) {
948 06d9260f Anthony Liguori
        crypt_method = s->crypt_method;
949 06d9260f Anthony Liguori
        memcpy(&aes_encrypt_key, &s->aes_encrypt_key, sizeof(aes_encrypt_key));
950 06d9260f Anthony Liguori
        memcpy(&aes_decrypt_key, &s->aes_decrypt_key, sizeof(aes_decrypt_key));
951 06d9260f Anthony Liguori
    }
952 06d9260f Anthony Liguori
953 06d9260f Anthony Liguori
    qcow2_close(bs);
954 06d9260f Anthony Liguori
955 acdfb480 Kevin Wolf
    options = qdict_new();
956 acdfb480 Kevin Wolf
    qdict_put(options, QCOW2_OPT_LAZY_REFCOUNTS,
957 acdfb480 Kevin Wolf
              qbool_from_int(s->use_lazy_refcounts));
958 acdfb480 Kevin Wolf
959 06d9260f Anthony Liguori
    memset(s, 0, sizeof(BDRVQcowState));
960 acdfb480 Kevin Wolf
    qcow2_open(bs, options, flags);
961 acdfb480 Kevin Wolf
962 acdfb480 Kevin Wolf
    QDECREF(options);
963 06d9260f Anthony Liguori
964 06d9260f Anthony Liguori
    if (crypt_method) {
965 06d9260f Anthony Liguori
        s->crypt_method = crypt_method;
966 06d9260f Anthony Liguori
        memcpy(&s->aes_encrypt_key, &aes_encrypt_key, sizeof(aes_encrypt_key));
967 06d9260f Anthony Liguori
        memcpy(&s->aes_decrypt_key, &aes_decrypt_key, sizeof(aes_decrypt_key));
968 06d9260f Anthony Liguori
    }
969 06d9260f Anthony Liguori
}
970 06d9260f Anthony Liguori
971 e24e49e6 Kevin Wolf
static size_t header_ext_add(char *buf, uint32_t magic, const void *s,
972 e24e49e6 Kevin Wolf
    size_t len, size_t buflen)
973 e24e49e6 Kevin Wolf
{
974 e24e49e6 Kevin Wolf
    QCowExtension *ext_backing_fmt = (QCowExtension*) buf;
975 e24e49e6 Kevin Wolf
    size_t ext_len = sizeof(QCowExtension) + ((len + 7) & ~7);
976 e24e49e6 Kevin Wolf
977 e24e49e6 Kevin Wolf
    if (buflen < ext_len) {
978 e24e49e6 Kevin Wolf
        return -ENOSPC;
979 e24e49e6 Kevin Wolf
    }
980 e24e49e6 Kevin Wolf
981 e24e49e6 Kevin Wolf
    *ext_backing_fmt = (QCowExtension) {
982 e24e49e6 Kevin Wolf
        .magic  = cpu_to_be32(magic),
983 e24e49e6 Kevin Wolf
        .len    = cpu_to_be32(len),
984 e24e49e6 Kevin Wolf
    };
985 e24e49e6 Kevin Wolf
    memcpy(buf + sizeof(QCowExtension), s, len);
986 e24e49e6 Kevin Wolf
987 e24e49e6 Kevin Wolf
    return ext_len;
988 e24e49e6 Kevin Wolf
}
989 e24e49e6 Kevin Wolf
990 756e6736 Kevin Wolf
/*
991 e24e49e6 Kevin Wolf
 * Updates the qcow2 header, including the variable length parts of it, i.e.
992 e24e49e6 Kevin Wolf
 * the backing file name and all extensions. qcow2 was not designed to allow
993 e24e49e6 Kevin Wolf
 * such changes, so if we run out of space (we can only use the first cluster)
994 e24e49e6 Kevin Wolf
 * this function may fail.
995 756e6736 Kevin Wolf
 *
996 756e6736 Kevin Wolf
 * Returns 0 on success, -errno in error cases.
997 756e6736 Kevin Wolf
 */
998 e24e49e6 Kevin Wolf
int qcow2_update_header(BlockDriverState *bs)
999 756e6736 Kevin Wolf
{
1000 756e6736 Kevin Wolf
    BDRVQcowState *s = bs->opaque;
1001 e24e49e6 Kevin Wolf
    QCowHeader *header;
1002 e24e49e6 Kevin Wolf
    char *buf;
1003 e24e49e6 Kevin Wolf
    size_t buflen = s->cluster_size;
1004 756e6736 Kevin Wolf
    int ret;
1005 e24e49e6 Kevin Wolf
    uint64_t total_size;
1006 e24e49e6 Kevin Wolf
    uint32_t refcount_table_clusters;
1007 6744cbab Kevin Wolf
    size_t header_length;
1008 75bab85c Kevin Wolf
    Qcow2UnknownHeaderExtension *uext;
1009 756e6736 Kevin Wolf
1010 e24e49e6 Kevin Wolf
    buf = qemu_blockalign(bs, buflen);
1011 756e6736 Kevin Wolf
1012 e24e49e6 Kevin Wolf
    /* Header structure */
1013 e24e49e6 Kevin Wolf
    header = (QCowHeader*) buf;
1014 756e6736 Kevin Wolf
1015 e24e49e6 Kevin Wolf
    if (buflen < sizeof(*header)) {
1016 e24e49e6 Kevin Wolf
        ret = -ENOSPC;
1017 e24e49e6 Kevin Wolf
        goto fail;
1018 756e6736 Kevin Wolf
    }
1019 756e6736 Kevin Wolf
1020 6744cbab Kevin Wolf
    header_length = sizeof(*header) + s->unknown_header_fields_size;
1021 e24e49e6 Kevin Wolf
    total_size = bs->total_sectors * BDRV_SECTOR_SIZE;
1022 e24e49e6 Kevin Wolf
    refcount_table_clusters = s->refcount_table_size >> (s->cluster_bits - 3);
1023 e24e49e6 Kevin Wolf
1024 e24e49e6 Kevin Wolf
    *header = (QCowHeader) {
1025 6744cbab Kevin Wolf
        /* Version 2 fields */
1026 e24e49e6 Kevin Wolf
        .magic                  = cpu_to_be32(QCOW_MAGIC),
1027 6744cbab Kevin Wolf
        .version                = cpu_to_be32(s->qcow_version),
1028 e24e49e6 Kevin Wolf
        .backing_file_offset    = 0,
1029 e24e49e6 Kevin Wolf
        .backing_file_size      = 0,
1030 e24e49e6 Kevin Wolf
        .cluster_bits           = cpu_to_be32(s->cluster_bits),
1031 e24e49e6 Kevin Wolf
        .size                   = cpu_to_be64(total_size),
1032 e24e49e6 Kevin Wolf
        .crypt_method           = cpu_to_be32(s->crypt_method_header),
1033 e24e49e6 Kevin Wolf
        .l1_size                = cpu_to_be32(s->l1_size),
1034 e24e49e6 Kevin Wolf
        .l1_table_offset        = cpu_to_be64(s->l1_table_offset),
1035 e24e49e6 Kevin Wolf
        .refcount_table_offset  = cpu_to_be64(s->refcount_table_offset),
1036 e24e49e6 Kevin Wolf
        .refcount_table_clusters = cpu_to_be32(refcount_table_clusters),
1037 e24e49e6 Kevin Wolf
        .nb_snapshots           = cpu_to_be32(s->nb_snapshots),
1038 e24e49e6 Kevin Wolf
        .snapshots_offset       = cpu_to_be64(s->snapshots_offset),
1039 6744cbab Kevin Wolf
1040 6744cbab Kevin Wolf
        /* Version 3 fields */
1041 6744cbab Kevin Wolf
        .incompatible_features  = cpu_to_be64(s->incompatible_features),
1042 6744cbab Kevin Wolf
        .compatible_features    = cpu_to_be64(s->compatible_features),
1043 6744cbab Kevin Wolf
        .autoclear_features     = cpu_to_be64(s->autoclear_features),
1044 6744cbab Kevin Wolf
        .refcount_order         = cpu_to_be32(3 + REFCOUNT_SHIFT),
1045 6744cbab Kevin Wolf
        .header_length          = cpu_to_be32(header_length),
1046 e24e49e6 Kevin Wolf
    };
1047 756e6736 Kevin Wolf
1048 6744cbab Kevin Wolf
    /* For older versions, write a shorter header */
1049 6744cbab Kevin Wolf
    switch (s->qcow_version) {
1050 6744cbab Kevin Wolf
    case 2:
1051 6744cbab Kevin Wolf
        ret = offsetof(QCowHeader, incompatible_features);
1052 6744cbab Kevin Wolf
        break;
1053 6744cbab Kevin Wolf
    case 3:
1054 6744cbab Kevin Wolf
        ret = sizeof(*header);
1055 6744cbab Kevin Wolf
        break;
1056 6744cbab Kevin Wolf
    default:
1057 b6c14762 Jim Meyering
        ret = -EINVAL;
1058 b6c14762 Jim Meyering
        goto fail;
1059 6744cbab Kevin Wolf
    }
1060 6744cbab Kevin Wolf
1061 6744cbab Kevin Wolf
    buf += ret;
1062 6744cbab Kevin Wolf
    buflen -= ret;
1063 6744cbab Kevin Wolf
    memset(buf, 0, buflen);
1064 6744cbab Kevin Wolf
1065 6744cbab Kevin Wolf
    /* Preserve any unknown field in the header */
1066 6744cbab Kevin Wolf
    if (s->unknown_header_fields_size) {
1067 6744cbab Kevin Wolf
        if (buflen < s->unknown_header_fields_size) {
1068 6744cbab Kevin Wolf
            ret = -ENOSPC;
1069 6744cbab Kevin Wolf
            goto fail;
1070 6744cbab Kevin Wolf
        }
1071 6744cbab Kevin Wolf
1072 6744cbab Kevin Wolf
        memcpy(buf, s->unknown_header_fields, s->unknown_header_fields_size);
1073 6744cbab Kevin Wolf
        buf += s->unknown_header_fields_size;
1074 6744cbab Kevin Wolf
        buflen -= s->unknown_header_fields_size;
1075 6744cbab Kevin Wolf
    }
1076 756e6736 Kevin Wolf
1077 e24e49e6 Kevin Wolf
    /* Backing file format header extension */
1078 e24e49e6 Kevin Wolf
    if (*bs->backing_format) {
1079 e24e49e6 Kevin Wolf
        ret = header_ext_add(buf, QCOW2_EXT_MAGIC_BACKING_FORMAT,
1080 e24e49e6 Kevin Wolf
                             bs->backing_format, strlen(bs->backing_format),
1081 e24e49e6 Kevin Wolf
                             buflen);
1082 e24e49e6 Kevin Wolf
        if (ret < 0) {
1083 e24e49e6 Kevin Wolf
            goto fail;
1084 756e6736 Kevin Wolf
        }
1085 756e6736 Kevin Wolf
1086 e24e49e6 Kevin Wolf
        buf += ret;
1087 e24e49e6 Kevin Wolf
        buflen -= ret;
1088 756e6736 Kevin Wolf
    }
1089 756e6736 Kevin Wolf
1090 cfcc4c62 Kevin Wolf
    /* Feature table */
1091 cfcc4c62 Kevin Wolf
    Qcow2Feature features[] = {
1092 c61d0004 Stefan Hajnoczi
        {
1093 c61d0004 Stefan Hajnoczi
            .type = QCOW2_FEAT_TYPE_INCOMPATIBLE,
1094 c61d0004 Stefan Hajnoczi
            .bit  = QCOW2_INCOMPAT_DIRTY_BITNR,
1095 c61d0004 Stefan Hajnoczi
            .name = "dirty bit",
1096 c61d0004 Stefan Hajnoczi
        },
1097 bfe8043e Stefan Hajnoczi
        {
1098 bfe8043e Stefan Hajnoczi
            .type = QCOW2_FEAT_TYPE_COMPATIBLE,
1099 bfe8043e Stefan Hajnoczi
            .bit  = QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR,
1100 bfe8043e Stefan Hajnoczi
            .name = "lazy refcounts",
1101 bfe8043e Stefan Hajnoczi
        },
1102 cfcc4c62 Kevin Wolf
    };
1103 cfcc4c62 Kevin Wolf
1104 cfcc4c62 Kevin Wolf
    ret = header_ext_add(buf, QCOW2_EXT_MAGIC_FEATURE_TABLE,
1105 cfcc4c62 Kevin Wolf
                         features, sizeof(features), buflen);
1106 cfcc4c62 Kevin Wolf
    if (ret < 0) {
1107 cfcc4c62 Kevin Wolf
        goto fail;
1108 cfcc4c62 Kevin Wolf
    }
1109 cfcc4c62 Kevin Wolf
    buf += ret;
1110 cfcc4c62 Kevin Wolf
    buflen -= ret;
1111 cfcc4c62 Kevin Wolf
1112 75bab85c Kevin Wolf
    /* Keep unknown header extensions */
1113 75bab85c Kevin Wolf
    QLIST_FOREACH(uext, &s->unknown_header_ext, next) {
1114 75bab85c Kevin Wolf
        ret = header_ext_add(buf, uext->magic, uext->data, uext->len, buflen);
1115 75bab85c Kevin Wolf
        if (ret < 0) {
1116 75bab85c Kevin Wolf
            goto fail;
1117 75bab85c Kevin Wolf
        }
1118 75bab85c Kevin Wolf
1119 75bab85c Kevin Wolf
        buf += ret;
1120 75bab85c Kevin Wolf
        buflen -= ret;
1121 75bab85c Kevin Wolf
    }
1122 75bab85c Kevin Wolf
1123 e24e49e6 Kevin Wolf
    /* End of header extensions */
1124 e24e49e6 Kevin Wolf
    ret = header_ext_add(buf, QCOW2_EXT_MAGIC_END, NULL, 0, buflen);
1125 756e6736 Kevin Wolf
    if (ret < 0) {
1126 756e6736 Kevin Wolf
        goto fail;
1127 756e6736 Kevin Wolf
    }
1128 756e6736 Kevin Wolf
1129 e24e49e6 Kevin Wolf
    buf += ret;
1130 e24e49e6 Kevin Wolf
    buflen -= ret;
1131 756e6736 Kevin Wolf
1132 e24e49e6 Kevin Wolf
    /* Backing file name */
1133 e24e49e6 Kevin Wolf
    if (*bs->backing_file) {
1134 e24e49e6 Kevin Wolf
        size_t backing_file_len = strlen(bs->backing_file);
1135 e24e49e6 Kevin Wolf
1136 e24e49e6 Kevin Wolf
        if (buflen < backing_file_len) {
1137 e24e49e6 Kevin Wolf
            ret = -ENOSPC;
1138 e24e49e6 Kevin Wolf
            goto fail;
1139 e24e49e6 Kevin Wolf
        }
1140 e24e49e6 Kevin Wolf
1141 00ea1881 Jim Meyering
        /* Using strncpy is ok here, since buf is not NUL-terminated. */
1142 e24e49e6 Kevin Wolf
        strncpy(buf, bs->backing_file, buflen);
1143 e24e49e6 Kevin Wolf
1144 e24e49e6 Kevin Wolf
        header->backing_file_offset = cpu_to_be64(buf - ((char*) header));
1145 e24e49e6 Kevin Wolf
        header->backing_file_size   = cpu_to_be32(backing_file_len);
1146 756e6736 Kevin Wolf
    }
1147 756e6736 Kevin Wolf
1148 e24e49e6 Kevin Wolf
    /* Write the new header */
1149 e24e49e6 Kevin Wolf
    ret = bdrv_pwrite(bs->file, 0, header, s->cluster_size);
1150 756e6736 Kevin Wolf
    if (ret < 0) {
1151 756e6736 Kevin Wolf
        goto fail;
1152 756e6736 Kevin Wolf
    }
1153 756e6736 Kevin Wolf
1154 756e6736 Kevin Wolf
    ret = 0;
1155 756e6736 Kevin Wolf
fail:
1156 e24e49e6 Kevin Wolf
    qemu_vfree(header);
1157 756e6736 Kevin Wolf
    return ret;
1158 756e6736 Kevin Wolf
}
1159 756e6736 Kevin Wolf
1160 756e6736 Kevin Wolf
static int qcow2_change_backing_file(BlockDriverState *bs,
1161 756e6736 Kevin Wolf
    const char *backing_file, const char *backing_fmt)
1162 756e6736 Kevin Wolf
{
1163 e24e49e6 Kevin Wolf
    pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: "");
1164 e24e49e6 Kevin Wolf
    pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: "");
1165 e24e49e6 Kevin Wolf
1166 e24e49e6 Kevin Wolf
    return qcow2_update_header(bs);
1167 756e6736 Kevin Wolf
}
1168 756e6736 Kevin Wolf
1169 a35e1c17 Kevin Wolf
static int preallocate(BlockDriverState *bs)
1170 a35e1c17 Kevin Wolf
{
1171 a35e1c17 Kevin Wolf
    uint64_t nb_sectors;
1172 a35e1c17 Kevin Wolf
    uint64_t offset;
1173 060bee89 Kevin Wolf
    uint64_t host_offset = 0;
1174 a35e1c17 Kevin Wolf
    int num;
1175 148da7ea Kevin Wolf
    int ret;
1176 f50f88b9 Kevin Wolf
    QCowL2Meta *meta;
1177 a35e1c17 Kevin Wolf
1178 a35e1c17 Kevin Wolf
    nb_sectors = bdrv_getlength(bs) >> 9;
1179 a35e1c17 Kevin Wolf
    offset = 0;
1180 a35e1c17 Kevin Wolf
1181 a35e1c17 Kevin Wolf
    while (nb_sectors) {
1182 a35e1c17 Kevin Wolf
        num = MIN(nb_sectors, INT_MAX >> 9);
1183 060bee89 Kevin Wolf
        ret = qcow2_alloc_cluster_offset(bs, offset, 0, num, &num,
1184 060bee89 Kevin Wolf
                                         &host_offset, &meta);
1185 148da7ea Kevin Wolf
        if (ret < 0) {
1186 19dbcbf7 Kevin Wolf
            return ret;
1187 a35e1c17 Kevin Wolf
        }
1188 a35e1c17 Kevin Wolf
1189 f50f88b9 Kevin Wolf
        ret = qcow2_alloc_cluster_link_l2(bs, meta);
1190 19dbcbf7 Kevin Wolf
        if (ret < 0) {
1191 f50f88b9 Kevin Wolf
            qcow2_free_any_clusters(bs, meta->alloc_offset, meta->nb_clusters);
1192 19dbcbf7 Kevin Wolf
            return ret;
1193 a35e1c17 Kevin Wolf
        }
1194 a35e1c17 Kevin Wolf
1195 f214978a Kevin Wolf
        /* There are no dependent requests, but we need to remove our request
1196 f214978a Kevin Wolf
         * from the list of in-flight requests */
1197 f50f88b9 Kevin Wolf
        if (meta != NULL) {
1198 4e95314e Kevin Wolf
            QLIST_REMOVE(meta, next_in_flight);
1199 f50f88b9 Kevin Wolf
        }
1200 f214978a Kevin Wolf
1201 a35e1c17 Kevin Wolf
        /* TODO Preallocate data if requested */
1202 a35e1c17 Kevin Wolf
1203 a35e1c17 Kevin Wolf
        nb_sectors -= num;
1204 a35e1c17 Kevin Wolf
        offset += num << 9;
1205 a35e1c17 Kevin Wolf
    }
1206 a35e1c17 Kevin Wolf
1207 a35e1c17 Kevin Wolf
    /*
1208 a35e1c17 Kevin Wolf
     * It is expected that the image file is large enough to actually contain
1209 a35e1c17 Kevin Wolf
     * all of the allocated clusters (otherwise we get failing reads after
1210 a35e1c17 Kevin Wolf
     * EOF). Extend the image to the last allocated sector.
1211 a35e1c17 Kevin Wolf
     */
1212 060bee89 Kevin Wolf
    if (host_offset != 0) {
1213 ea80b906 Kevin Wolf
        uint8_t buf[512];
1214 ea80b906 Kevin Wolf
        memset(buf, 0, 512);
1215 060bee89 Kevin Wolf
        ret = bdrv_write(bs->file, (host_offset >> 9) + num - 1, buf, 1);
1216 19dbcbf7 Kevin Wolf
        if (ret < 0) {
1217 19dbcbf7 Kevin Wolf
            return ret;
1218 19dbcbf7 Kevin Wolf
        }
1219 a35e1c17 Kevin Wolf
    }
1220 a35e1c17 Kevin Wolf
1221 a35e1c17 Kevin Wolf
    return 0;
1222 a35e1c17 Kevin Wolf
}
1223 a35e1c17 Kevin Wolf
1224 7c80ab3f Jes Sorensen
static int qcow2_create2(const char *filename, int64_t total_size,
1225 7c80ab3f Jes Sorensen
                         const char *backing_file, const char *backing_format,
1226 7c80ab3f Jes Sorensen
                         int flags, size_t cluster_size, int prealloc,
1227 6744cbab Kevin Wolf
                         QEMUOptionParameter *options, int version)
1228 a9420734 Kevin Wolf
{
1229 9b2260cb Dong Xu Wang
    /* Calculate cluster_bits */
1230 a9420734 Kevin Wolf
    int cluster_bits;
1231 a9420734 Kevin Wolf
    cluster_bits = ffs(cluster_size) - 1;
1232 a9420734 Kevin Wolf
    if (cluster_bits < MIN_CLUSTER_BITS || cluster_bits > MAX_CLUSTER_BITS ||
1233 a9420734 Kevin Wolf
        (1 << cluster_bits) != cluster_size)
1234 a9420734 Kevin Wolf
    {
1235 a9420734 Kevin Wolf
        error_report(
1236 6daf194d Markus Armbruster
            "Cluster size must be a power of two between %d and %dk",
1237 a9420734 Kevin Wolf
            1 << MIN_CLUSTER_BITS, 1 << (MAX_CLUSTER_BITS - 10));
1238 a9420734 Kevin Wolf
        return -EINVAL;
1239 a9420734 Kevin Wolf
    }
1240 a9420734 Kevin Wolf
1241 a9420734 Kevin Wolf
    /*
1242 a9420734 Kevin Wolf
     * Open the image file and write a minimal qcow2 header.
1243 a9420734 Kevin Wolf
     *
1244 a9420734 Kevin Wolf
     * We keep things simple and start with a zero-sized image. We also
1245 a9420734 Kevin Wolf
     * do without refcount blocks or a L1 table for now. We'll fix the
1246 a9420734 Kevin Wolf
     * inconsistency later.
1247 a9420734 Kevin Wolf
     *
1248 a9420734 Kevin Wolf
     * We do need a refcount table because growing the refcount table means
1249 a9420734 Kevin Wolf
     * allocating two new refcount blocks - the seconds of which would be at
1250 a9420734 Kevin Wolf
     * 2 GB for 64k clusters, and we don't want to have a 2 GB initial file
1251 a9420734 Kevin Wolf
     * size for any qcow2 image.
1252 a9420734 Kevin Wolf
     */
1253 a9420734 Kevin Wolf
    BlockDriverState* bs;
1254 a9420734 Kevin Wolf
    QCowHeader header;
1255 a9420734 Kevin Wolf
    uint8_t* refcount_table;
1256 a9420734 Kevin Wolf
    int ret;
1257 a9420734 Kevin Wolf
1258 a9420734 Kevin Wolf
    ret = bdrv_create_file(filename, options);
1259 a9420734 Kevin Wolf
    if (ret < 0) {
1260 a9420734 Kevin Wolf
        return ret;
1261 a9420734 Kevin Wolf
    }
1262 a9420734 Kevin Wolf
1263 787e4a85 Kevin Wolf
    ret = bdrv_file_open(&bs, filename, NULL, BDRV_O_RDWR);
1264 a9420734 Kevin Wolf
    if (ret < 0) {
1265 a9420734 Kevin Wolf
        return ret;
1266 a9420734 Kevin Wolf
    }
1267 a9420734 Kevin Wolf
1268 a9420734 Kevin Wolf
    /* Write the header */
1269 a9420734 Kevin Wolf
    memset(&header, 0, sizeof(header));
1270 a9420734 Kevin Wolf
    header.magic = cpu_to_be32(QCOW_MAGIC);
1271 6744cbab Kevin Wolf
    header.version = cpu_to_be32(version);
1272 a9420734 Kevin Wolf
    header.cluster_bits = cpu_to_be32(cluster_bits);
1273 a9420734 Kevin Wolf
    header.size = cpu_to_be64(0);
1274 a9420734 Kevin Wolf
    header.l1_table_offset = cpu_to_be64(0);
1275 a9420734 Kevin Wolf
    header.l1_size = cpu_to_be32(0);
1276 a9420734 Kevin Wolf
    header.refcount_table_offset = cpu_to_be64(cluster_size);
1277 a9420734 Kevin Wolf
    header.refcount_table_clusters = cpu_to_be32(1);
1278 6744cbab Kevin Wolf
    header.refcount_order = cpu_to_be32(3 + REFCOUNT_SHIFT);
1279 6744cbab Kevin Wolf
    header.header_length = cpu_to_be32(sizeof(header));
1280 a9420734 Kevin Wolf
1281 a9420734 Kevin Wolf
    if (flags & BLOCK_FLAG_ENCRYPT) {
1282 a9420734 Kevin Wolf
        header.crypt_method = cpu_to_be32(QCOW_CRYPT_AES);
1283 a9420734 Kevin Wolf
    } else {
1284 a9420734 Kevin Wolf
        header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE);
1285 a9420734 Kevin Wolf
    }
1286 a9420734 Kevin Wolf
1287 bfe8043e Stefan Hajnoczi
    if (flags & BLOCK_FLAG_LAZY_REFCOUNTS) {
1288 bfe8043e Stefan Hajnoczi
        header.compatible_features |=
1289 bfe8043e Stefan Hajnoczi
            cpu_to_be64(QCOW2_COMPAT_LAZY_REFCOUNTS);
1290 bfe8043e Stefan Hajnoczi
    }
1291 bfe8043e Stefan Hajnoczi
1292 a9420734 Kevin Wolf
    ret = bdrv_pwrite(bs, 0, &header, sizeof(header));
1293 a9420734 Kevin Wolf
    if (ret < 0) {
1294 a9420734 Kevin Wolf
        goto out;
1295 a9420734 Kevin Wolf
    }
1296 a9420734 Kevin Wolf
1297 a9420734 Kevin Wolf
    /* Write an empty refcount table */
1298 7267c094 Anthony Liguori
    refcount_table = g_malloc0(cluster_size);
1299 a9420734 Kevin Wolf
    ret = bdrv_pwrite(bs, cluster_size, refcount_table, cluster_size);
1300 7267c094 Anthony Liguori
    g_free(refcount_table);
1301 a9420734 Kevin Wolf
1302 a9420734 Kevin Wolf
    if (ret < 0) {
1303 a9420734 Kevin Wolf
        goto out;
1304 a9420734 Kevin Wolf
    }
1305 a9420734 Kevin Wolf
1306 a9420734 Kevin Wolf
    bdrv_close(bs);
1307 a9420734 Kevin Wolf
1308 a9420734 Kevin Wolf
    /*
1309 a9420734 Kevin Wolf
     * And now open the image and make it consistent first (i.e. increase the
1310 a9420734 Kevin Wolf
     * refcount of the cluster that is occupied by the header and the refcount
1311 a9420734 Kevin Wolf
     * table)
1312 a9420734 Kevin Wolf
     */
1313 a9420734 Kevin Wolf
    BlockDriver* drv = bdrv_find_format("qcow2");
1314 a9420734 Kevin Wolf
    assert(drv != NULL);
1315 de9c0cec Kevin Wolf
    ret = bdrv_open(bs, filename, NULL,
1316 e1a7107f Kevin Wolf
        BDRV_O_RDWR | BDRV_O_CACHE_WB | BDRV_O_NO_FLUSH, drv);
1317 a9420734 Kevin Wolf
    if (ret < 0) {
1318 a9420734 Kevin Wolf
        goto out;
1319 a9420734 Kevin Wolf
    }
1320 a9420734 Kevin Wolf
1321 a9420734 Kevin Wolf
    ret = qcow2_alloc_clusters(bs, 2 * cluster_size);
1322 a9420734 Kevin Wolf
    if (ret < 0) {
1323 a9420734 Kevin Wolf
        goto out;
1324 a9420734 Kevin Wolf
1325 a9420734 Kevin Wolf
    } else if (ret != 0) {
1326 a9420734 Kevin Wolf
        error_report("Huh, first cluster in empty image is already in use?");
1327 a9420734 Kevin Wolf
        abort();
1328 a9420734 Kevin Wolf
    }
1329 a9420734 Kevin Wolf
1330 a9420734 Kevin Wolf
    /* Okay, now that we have a valid image, let's give it the right size */
1331 a9420734 Kevin Wolf
    ret = bdrv_truncate(bs, total_size * BDRV_SECTOR_SIZE);
1332 a9420734 Kevin Wolf
    if (ret < 0) {
1333 a9420734 Kevin Wolf
        goto out;
1334 a9420734 Kevin Wolf
    }
1335 a9420734 Kevin Wolf
1336 a9420734 Kevin Wolf
    /* Want a backing file? There you go.*/
1337 a9420734 Kevin Wolf
    if (backing_file) {
1338 a9420734 Kevin Wolf
        ret = bdrv_change_backing_file(bs, backing_file, backing_format);
1339 a9420734 Kevin Wolf
        if (ret < 0) {
1340 a9420734 Kevin Wolf
            goto out;
1341 a9420734 Kevin Wolf
        }
1342 a9420734 Kevin Wolf
    }
1343 a9420734 Kevin Wolf
1344 a9420734 Kevin Wolf
    /* And if we're supposed to preallocate metadata, do that now */
1345 a9420734 Kevin Wolf
    if (prealloc) {
1346 15552c4a Zhi Yong Wu
        BDRVQcowState *s = bs->opaque;
1347 15552c4a Zhi Yong Wu
        qemu_co_mutex_lock(&s->lock);
1348 a9420734 Kevin Wolf
        ret = preallocate(bs);
1349 15552c4a Zhi Yong Wu
        qemu_co_mutex_unlock(&s->lock);
1350 a9420734 Kevin Wolf
        if (ret < 0) {
1351 a9420734 Kevin Wolf
            goto out;
1352 a9420734 Kevin Wolf
        }
1353 a9420734 Kevin Wolf
    }
1354 a9420734 Kevin Wolf
1355 a9420734 Kevin Wolf
    ret = 0;
1356 a9420734 Kevin Wolf
out:
1357 a9420734 Kevin Wolf
    bdrv_delete(bs);
1358 a9420734 Kevin Wolf
    return ret;
1359 a9420734 Kevin Wolf
}
1360 de5f3f40 Kevin Wolf
1361 7c80ab3f Jes Sorensen
static int qcow2_create(const char *filename, QEMUOptionParameter *options)
1362 de5f3f40 Kevin Wolf
{
1363 de5f3f40 Kevin Wolf
    const char *backing_file = NULL;
1364 de5f3f40 Kevin Wolf
    const char *backing_fmt = NULL;
1365 de5f3f40 Kevin Wolf
    uint64_t sectors = 0;
1366 de5f3f40 Kevin Wolf
    int flags = 0;
1367 99cce9fa Kevin Wolf
    size_t cluster_size = DEFAULT_CLUSTER_SIZE;
1368 de5f3f40 Kevin Wolf
    int prealloc = 0;
1369 6744cbab Kevin Wolf
    int version = 2;
1370 de5f3f40 Kevin Wolf
1371 de5f3f40 Kevin Wolf
    /* Read out options */
1372 de5f3f40 Kevin Wolf
    while (options && options->name) {
1373 de5f3f40 Kevin Wolf
        if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
1374 de5f3f40 Kevin Wolf
            sectors = options->value.n / 512;
1375 de5f3f40 Kevin Wolf
        } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) {
1376 de5f3f40 Kevin Wolf
            backing_file = options->value.s;
1377 de5f3f40 Kevin Wolf
        } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FMT)) {
1378 de5f3f40 Kevin Wolf
            backing_fmt = options->value.s;
1379 de5f3f40 Kevin Wolf
        } else if (!strcmp(options->name, BLOCK_OPT_ENCRYPT)) {
1380 de5f3f40 Kevin Wolf
            flags |= options->value.n ? BLOCK_FLAG_ENCRYPT : 0;
1381 de5f3f40 Kevin Wolf
        } else if (!strcmp(options->name, BLOCK_OPT_CLUSTER_SIZE)) {
1382 de5f3f40 Kevin Wolf
            if (options->value.n) {
1383 de5f3f40 Kevin Wolf
                cluster_size = options->value.n;
1384 de5f3f40 Kevin Wolf
            }
1385 de5f3f40 Kevin Wolf
        } else if (!strcmp(options->name, BLOCK_OPT_PREALLOC)) {
1386 de5f3f40 Kevin Wolf
            if (!options->value.s || !strcmp(options->value.s, "off")) {
1387 de5f3f40 Kevin Wolf
                prealloc = 0;
1388 de5f3f40 Kevin Wolf
            } else if (!strcmp(options->value.s, "metadata")) {
1389 de5f3f40 Kevin Wolf
                prealloc = 1;
1390 de5f3f40 Kevin Wolf
            } else {
1391 de5f3f40 Kevin Wolf
                fprintf(stderr, "Invalid preallocation mode: '%s'\n",
1392 de5f3f40 Kevin Wolf
                    options->value.s);
1393 de5f3f40 Kevin Wolf
                return -EINVAL;
1394 de5f3f40 Kevin Wolf
            }
1395 6744cbab Kevin Wolf
        } else if (!strcmp(options->name, BLOCK_OPT_COMPAT_LEVEL)) {
1396 6744cbab Kevin Wolf
            if (!options->value.s || !strcmp(options->value.s, "0.10")) {
1397 6744cbab Kevin Wolf
                version = 2;
1398 6744cbab Kevin Wolf
            } else if (!strcmp(options->value.s, "1.1")) {
1399 6744cbab Kevin Wolf
                version = 3;
1400 6744cbab Kevin Wolf
            } else {
1401 6744cbab Kevin Wolf
                fprintf(stderr, "Invalid compatibility level: '%s'\n",
1402 6744cbab Kevin Wolf
                    options->value.s);
1403 6744cbab Kevin Wolf
                return -EINVAL;
1404 6744cbab Kevin Wolf
            }
1405 bfe8043e Stefan Hajnoczi
        } else if (!strcmp(options->name, BLOCK_OPT_LAZY_REFCOUNTS)) {
1406 bfe8043e Stefan Hajnoczi
            flags |= options->value.n ? BLOCK_FLAG_LAZY_REFCOUNTS : 0;
1407 de5f3f40 Kevin Wolf
        }
1408 de5f3f40 Kevin Wolf
        options++;
1409 de5f3f40 Kevin Wolf
    }
1410 de5f3f40 Kevin Wolf
1411 de5f3f40 Kevin Wolf
    if (backing_file && prealloc) {
1412 de5f3f40 Kevin Wolf
        fprintf(stderr, "Backing file and preallocation cannot be used at "
1413 de5f3f40 Kevin Wolf
            "the same time\n");
1414 de5f3f40 Kevin Wolf
        return -EINVAL;
1415 de5f3f40 Kevin Wolf
    }
1416 de5f3f40 Kevin Wolf
1417 bfe8043e Stefan Hajnoczi
    if (version < 3 && (flags & BLOCK_FLAG_LAZY_REFCOUNTS)) {
1418 bfe8043e Stefan Hajnoczi
        fprintf(stderr, "Lazy refcounts only supported with compatibility "
1419 bfe8043e Stefan Hajnoczi
                "level 1.1 and above (use compat=1.1 or greater)\n");
1420 bfe8043e Stefan Hajnoczi
        return -EINVAL;
1421 bfe8043e Stefan Hajnoczi
    }
1422 bfe8043e Stefan Hajnoczi
1423 7c80ab3f Jes Sorensen
    return qcow2_create2(filename, sectors, backing_file, backing_fmt, flags,
1424 6744cbab Kevin Wolf
                         cluster_size, prealloc, options, version);
1425 de5f3f40 Kevin Wolf
}
1426 de5f3f40 Kevin Wolf
1427 7c80ab3f Jes Sorensen
static int qcow2_make_empty(BlockDriverState *bs)
1428 20d97356 Blue Swirl
{
1429 20d97356 Blue Swirl
#if 0
1430 20d97356 Blue Swirl
    /* XXX: not correct */
1431 20d97356 Blue Swirl
    BDRVQcowState *s = bs->opaque;
1432 20d97356 Blue Swirl
    uint32_t l1_length = s->l1_size * sizeof(uint64_t);
1433 20d97356 Blue Swirl
    int ret;
1434 20d97356 Blue Swirl

1435 20d97356 Blue Swirl
    memset(s->l1_table, 0, l1_length);
1436 66f82cee Kevin Wolf
    if (bdrv_pwrite(bs->file, s->l1_table_offset, s->l1_table, l1_length) < 0)
1437 20d97356 Blue Swirl
        return -1;
1438 66f82cee Kevin Wolf
    ret = bdrv_truncate(bs->file, s->l1_table_offset + l1_length);
1439 20d97356 Blue Swirl
    if (ret < 0)
1440 20d97356 Blue Swirl
        return ret;
1441 20d97356 Blue Swirl

1442 20d97356 Blue Swirl
    l2_cache_reset(bs);
1443 20d97356 Blue Swirl
#endif
1444 20d97356 Blue Swirl
    return 0;
1445 20d97356 Blue Swirl
}
1446 20d97356 Blue Swirl
1447 621f0589 Kevin Wolf
static coroutine_fn int qcow2_co_write_zeroes(BlockDriverState *bs,
1448 621f0589 Kevin Wolf
    int64_t sector_num, int nb_sectors)
1449 621f0589 Kevin Wolf
{
1450 621f0589 Kevin Wolf
    int ret;
1451 621f0589 Kevin Wolf
    BDRVQcowState *s = bs->opaque;
1452 621f0589 Kevin Wolf
1453 621f0589 Kevin Wolf
    /* Emulate misaligned zero writes */
1454 621f0589 Kevin Wolf
    if (sector_num % s->cluster_sectors || nb_sectors % s->cluster_sectors) {
1455 621f0589 Kevin Wolf
        return -ENOTSUP;
1456 621f0589 Kevin Wolf
    }
1457 621f0589 Kevin Wolf
1458 621f0589 Kevin Wolf
    /* Whatever is left can use real zero clusters */
1459 621f0589 Kevin Wolf
    qemu_co_mutex_lock(&s->lock);
1460 621f0589 Kevin Wolf
    ret = qcow2_zero_clusters(bs, sector_num << BDRV_SECTOR_BITS,
1461 621f0589 Kevin Wolf
        nb_sectors);
1462 621f0589 Kevin Wolf
    qemu_co_mutex_unlock(&s->lock);
1463 621f0589 Kevin Wolf
1464 621f0589 Kevin Wolf
    return ret;
1465 621f0589 Kevin Wolf
}
1466 621f0589 Kevin Wolf
1467 6db39ae2 Paolo Bonzini
static coroutine_fn int qcow2_co_discard(BlockDriverState *bs,
1468 6db39ae2 Paolo Bonzini
    int64_t sector_num, int nb_sectors)
1469 5ea929e3 Kevin Wolf
{
1470 6db39ae2 Paolo Bonzini
    int ret;
1471 6db39ae2 Paolo Bonzini
    BDRVQcowState *s = bs->opaque;
1472 6db39ae2 Paolo Bonzini
1473 6db39ae2 Paolo Bonzini
    qemu_co_mutex_lock(&s->lock);
1474 6db39ae2 Paolo Bonzini
    ret = qcow2_discard_clusters(bs, sector_num << BDRV_SECTOR_BITS,
1475 5ea929e3 Kevin Wolf
        nb_sectors);
1476 6db39ae2 Paolo Bonzini
    qemu_co_mutex_unlock(&s->lock);
1477 6db39ae2 Paolo Bonzini
    return ret;
1478 5ea929e3 Kevin Wolf
}
1479 5ea929e3 Kevin Wolf
1480 419b19d9 Stefan Hajnoczi
static int qcow2_truncate(BlockDriverState *bs, int64_t offset)
1481 419b19d9 Stefan Hajnoczi
{
1482 419b19d9 Stefan Hajnoczi
    BDRVQcowState *s = bs->opaque;
1483 419b19d9 Stefan Hajnoczi
    int ret, new_l1_size;
1484 419b19d9 Stefan Hajnoczi
1485 419b19d9 Stefan Hajnoczi
    if (offset & 511) {
1486 259b2173 Kevin Wolf
        error_report("The new size must be a multiple of 512");
1487 419b19d9 Stefan Hajnoczi
        return -EINVAL;
1488 419b19d9 Stefan Hajnoczi
    }
1489 419b19d9 Stefan Hajnoczi
1490 419b19d9 Stefan Hajnoczi
    /* cannot proceed if image has snapshots */
1491 419b19d9 Stefan Hajnoczi
    if (s->nb_snapshots) {
1492 259b2173 Kevin Wolf
        error_report("Can't resize an image which has snapshots");
1493 419b19d9 Stefan Hajnoczi
        return -ENOTSUP;
1494 419b19d9 Stefan Hajnoczi
    }
1495 419b19d9 Stefan Hajnoczi
1496 419b19d9 Stefan Hajnoczi
    /* shrinking is currently not supported */
1497 419b19d9 Stefan Hajnoczi
    if (offset < bs->total_sectors * 512) {
1498 259b2173 Kevin Wolf
        error_report("qcow2 doesn't support shrinking images yet");
1499 419b19d9 Stefan Hajnoczi
        return -ENOTSUP;
1500 419b19d9 Stefan Hajnoczi
    }
1501 419b19d9 Stefan Hajnoczi
1502 419b19d9 Stefan Hajnoczi
    new_l1_size = size_to_l1(s, offset);
1503 72893756 Stefan Hajnoczi
    ret = qcow2_grow_l1_table(bs, new_l1_size, true);
1504 419b19d9 Stefan Hajnoczi
    if (ret < 0) {
1505 419b19d9 Stefan Hajnoczi
        return ret;
1506 419b19d9 Stefan Hajnoczi
    }
1507 419b19d9 Stefan Hajnoczi
1508 419b19d9 Stefan Hajnoczi
    /* write updated header.size */
1509 419b19d9 Stefan Hajnoczi
    offset = cpu_to_be64(offset);
1510 8b3b7206 Kevin Wolf
    ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, size),
1511 8b3b7206 Kevin Wolf
                           &offset, sizeof(uint64_t));
1512 419b19d9 Stefan Hajnoczi
    if (ret < 0) {
1513 419b19d9 Stefan Hajnoczi
        return ret;
1514 419b19d9 Stefan Hajnoczi
    }
1515 419b19d9 Stefan Hajnoczi
1516 419b19d9 Stefan Hajnoczi
    s->l1_vm_state_index = new_l1_size;
1517 419b19d9 Stefan Hajnoczi
    return 0;
1518 419b19d9 Stefan Hajnoczi
}
1519 419b19d9 Stefan Hajnoczi
1520 20d97356 Blue Swirl
/* XXX: put compressed sectors first, then all the cluster aligned
1521 20d97356 Blue Swirl
   tables to avoid losing bytes in alignment */
1522 7c80ab3f Jes Sorensen
static int qcow2_write_compressed(BlockDriverState *bs, int64_t sector_num,
1523 7c80ab3f Jes Sorensen
                                  const uint8_t *buf, int nb_sectors)
1524 20d97356 Blue Swirl
{
1525 20d97356 Blue Swirl
    BDRVQcowState *s = bs->opaque;
1526 20d97356 Blue Swirl
    z_stream strm;
1527 20d97356 Blue Swirl
    int ret, out_len;
1528 20d97356 Blue Swirl
    uint8_t *out_buf;
1529 20d97356 Blue Swirl
    uint64_t cluster_offset;
1530 20d97356 Blue Swirl
1531 20d97356 Blue Swirl
    if (nb_sectors == 0) {
1532 20d97356 Blue Swirl
        /* align end of file to a sector boundary to ease reading with
1533 20d97356 Blue Swirl
           sector based I/Os */
1534 66f82cee Kevin Wolf
        cluster_offset = bdrv_getlength(bs->file);
1535 20d97356 Blue Swirl
        cluster_offset = (cluster_offset + 511) & ~511;
1536 66f82cee Kevin Wolf
        bdrv_truncate(bs->file, cluster_offset);
1537 20d97356 Blue Swirl
        return 0;
1538 20d97356 Blue Swirl
    }
1539 20d97356 Blue Swirl
1540 20d97356 Blue Swirl
    if (nb_sectors != s->cluster_sectors)
1541 20d97356 Blue Swirl
        return -EINVAL;
1542 20d97356 Blue Swirl
1543 7267c094 Anthony Liguori
    out_buf = g_malloc(s->cluster_size + (s->cluster_size / 1000) + 128);
1544 20d97356 Blue Swirl
1545 20d97356 Blue Swirl
    /* best compression, small window, no zlib header */
1546 20d97356 Blue Swirl
    memset(&strm, 0, sizeof(strm));
1547 20d97356 Blue Swirl
    ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION,
1548 20d97356 Blue Swirl
                       Z_DEFLATED, -12,
1549 20d97356 Blue Swirl
                       9, Z_DEFAULT_STRATEGY);
1550 20d97356 Blue Swirl
    if (ret != 0) {
1551 8f1efd00 Kevin Wolf
        ret = -EINVAL;
1552 8f1efd00 Kevin Wolf
        goto fail;
1553 20d97356 Blue Swirl
    }
1554 20d97356 Blue Swirl
1555 20d97356 Blue Swirl
    strm.avail_in = s->cluster_size;
1556 20d97356 Blue Swirl
    strm.next_in = (uint8_t *)buf;
1557 20d97356 Blue Swirl
    strm.avail_out = s->cluster_size;
1558 20d97356 Blue Swirl
    strm.next_out = out_buf;
1559 20d97356 Blue Swirl
1560 20d97356 Blue Swirl
    ret = deflate(&strm, Z_FINISH);
1561 20d97356 Blue Swirl
    if (ret != Z_STREAM_END && ret != Z_OK) {
1562 20d97356 Blue Swirl
        deflateEnd(&strm);
1563 8f1efd00 Kevin Wolf
        ret = -EINVAL;
1564 8f1efd00 Kevin Wolf
        goto fail;
1565 20d97356 Blue Swirl
    }
1566 20d97356 Blue Swirl
    out_len = strm.next_out - out_buf;
1567 20d97356 Blue Swirl
1568 20d97356 Blue Swirl
    deflateEnd(&strm);
1569 20d97356 Blue Swirl
1570 20d97356 Blue Swirl
    if (ret != Z_STREAM_END || out_len >= s->cluster_size) {
1571 20d97356 Blue Swirl
        /* could not compress: write normal cluster */
1572 8f1efd00 Kevin Wolf
        ret = bdrv_write(bs, sector_num, buf, s->cluster_sectors);
1573 8f1efd00 Kevin Wolf
        if (ret < 0) {
1574 8f1efd00 Kevin Wolf
            goto fail;
1575 8f1efd00 Kevin Wolf
        }
1576 20d97356 Blue Swirl
    } else {
1577 20d97356 Blue Swirl
        cluster_offset = qcow2_alloc_compressed_cluster_offset(bs,
1578 20d97356 Blue Swirl
            sector_num << 9, out_len);
1579 8f1efd00 Kevin Wolf
        if (!cluster_offset) {
1580 8f1efd00 Kevin Wolf
            ret = -EIO;
1581 8f1efd00 Kevin Wolf
            goto fail;
1582 8f1efd00 Kevin Wolf
        }
1583 20d97356 Blue Swirl
        cluster_offset &= s->cluster_offset_mask;
1584 66f82cee Kevin Wolf
        BLKDBG_EVENT(bs->file, BLKDBG_WRITE_COMPRESSED);
1585 8f1efd00 Kevin Wolf
        ret = bdrv_pwrite(bs->file, cluster_offset, out_buf, out_len);
1586 8f1efd00 Kevin Wolf
        if (ret < 0) {
1587 8f1efd00 Kevin Wolf
            goto fail;
1588 20d97356 Blue Swirl
        }
1589 20d97356 Blue Swirl
    }
1590 20d97356 Blue Swirl
1591 8f1efd00 Kevin Wolf
    ret = 0;
1592 8f1efd00 Kevin Wolf
fail:
1593 7267c094 Anthony Liguori
    g_free(out_buf);
1594 8f1efd00 Kevin Wolf
    return ret;
1595 20d97356 Blue Swirl
}
1596 20d97356 Blue Swirl
1597 a968168c Dong Xu Wang
static coroutine_fn int qcow2_co_flush_to_os(BlockDriverState *bs)
1598 20d97356 Blue Swirl
{
1599 29c1a730 Kevin Wolf
    BDRVQcowState *s = bs->opaque;
1600 29c1a730 Kevin Wolf
    int ret;
1601 29c1a730 Kevin Wolf
1602 8b94ff85 Paolo Bonzini
    qemu_co_mutex_lock(&s->lock);
1603 29c1a730 Kevin Wolf
    ret = qcow2_cache_flush(bs, s->l2_table_cache);
1604 29c1a730 Kevin Wolf
    if (ret < 0) {
1605 c95de7e2 Dong Xu Wang
        qemu_co_mutex_unlock(&s->lock);
1606 8b94ff85 Paolo Bonzini
        return ret;
1607 29c1a730 Kevin Wolf
    }
1608 29c1a730 Kevin Wolf
1609 bfe8043e Stefan Hajnoczi
    if (qcow2_need_accurate_refcounts(s)) {
1610 bfe8043e Stefan Hajnoczi
        ret = qcow2_cache_flush(bs, s->refcount_block_cache);
1611 bfe8043e Stefan Hajnoczi
        if (ret < 0) {
1612 bfe8043e Stefan Hajnoczi
            qemu_co_mutex_unlock(&s->lock);
1613 bfe8043e Stefan Hajnoczi
            return ret;
1614 bfe8043e Stefan Hajnoczi
        }
1615 29c1a730 Kevin Wolf
    }
1616 8b94ff85 Paolo Bonzini
    qemu_co_mutex_unlock(&s->lock);
1617 29c1a730 Kevin Wolf
1618 eb489bb1 Kevin Wolf
    return 0;
1619 eb489bb1 Kevin Wolf
}
1620 eb489bb1 Kevin Wolf
1621 7c80ab3f Jes Sorensen
static int64_t qcow2_vm_state_offset(BDRVQcowState *s)
1622 20d97356 Blue Swirl
{
1623 20d97356 Blue Swirl
        return (int64_t)s->l1_vm_state_index << (s->cluster_bits + s->l2_bits);
1624 20d97356 Blue Swirl
}
1625 20d97356 Blue Swirl
1626 7c80ab3f Jes Sorensen
static int qcow2_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1627 20d97356 Blue Swirl
{
1628 20d97356 Blue Swirl
    BDRVQcowState *s = bs->opaque;
1629 20d97356 Blue Swirl
    bdi->cluster_size = s->cluster_size;
1630 7c80ab3f Jes Sorensen
    bdi->vm_state_offset = qcow2_vm_state_offset(s);
1631 20d97356 Blue Swirl
    return 0;
1632 20d97356 Blue Swirl
}
1633 20d97356 Blue Swirl
1634 20d97356 Blue Swirl
#if 0
1635 20d97356 Blue Swirl
static void dump_refcounts(BlockDriverState *bs)
1636 20d97356 Blue Swirl
{
1637 20d97356 Blue Swirl
    BDRVQcowState *s = bs->opaque;
1638 20d97356 Blue Swirl
    int64_t nb_clusters, k, k1, size;
1639 20d97356 Blue Swirl
    int refcount;
1640 20d97356 Blue Swirl

1641 66f82cee Kevin Wolf
    size = bdrv_getlength(bs->file);
1642 20d97356 Blue Swirl
    nb_clusters = size_to_clusters(s, size);
1643 20d97356 Blue Swirl
    for(k = 0; k < nb_clusters;) {
1644 20d97356 Blue Swirl
        k1 = k;
1645 20d97356 Blue Swirl
        refcount = get_refcount(bs, k);
1646 20d97356 Blue Swirl
        k++;
1647 20d97356 Blue Swirl
        while (k < nb_clusters && get_refcount(bs, k) == refcount)
1648 20d97356 Blue Swirl
            k++;
1649 0bfcd599 Blue Swirl
        printf("%" PRId64 ": refcount=%d nb=%" PRId64 "\n", k, refcount,
1650 0bfcd599 Blue Swirl
               k - k1);
1651 20d97356 Blue Swirl
    }
1652 20d97356 Blue Swirl
}
1653 20d97356 Blue Swirl
#endif
1654 20d97356 Blue Swirl
1655 cf8074b3 Kevin Wolf
static int qcow2_save_vmstate(BlockDriverState *bs, QEMUIOVector *qiov,
1656 cf8074b3 Kevin Wolf
                              int64_t pos)
1657 20d97356 Blue Swirl
{
1658 20d97356 Blue Swirl
    BDRVQcowState *s = bs->opaque;
1659 20d97356 Blue Swirl
    int growable = bs->growable;
1660 20d97356 Blue Swirl
    int ret;
1661 20d97356 Blue Swirl
1662 66f82cee Kevin Wolf
    BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_SAVE);
1663 20d97356 Blue Swirl
    bs->growable = 1;
1664 8d3b1a2d Kevin Wolf
    ret = bdrv_pwritev(bs, qcow2_vm_state_offset(s) + pos, qiov);
1665 20d97356 Blue Swirl
    bs->growable = growable;
1666 20d97356 Blue Swirl
1667 20d97356 Blue Swirl
    return ret;
1668 20d97356 Blue Swirl
}
1669 20d97356 Blue Swirl
1670 7c80ab3f Jes Sorensen
static int qcow2_load_vmstate(BlockDriverState *bs, uint8_t *buf,
1671 7c80ab3f Jes Sorensen
                              int64_t pos, int size)
1672 20d97356 Blue Swirl
{
1673 20d97356 Blue Swirl
    BDRVQcowState *s = bs->opaque;
1674 20d97356 Blue Swirl
    int growable = bs->growable;
1675 20d97356 Blue Swirl
    int ret;
1676 20d97356 Blue Swirl
1677 66f82cee Kevin Wolf
    BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_LOAD);
1678 20d97356 Blue Swirl
    bs->growable = 1;
1679 7c80ab3f Jes Sorensen
    ret = bdrv_pread(bs, qcow2_vm_state_offset(s) + pos, buf, size);
1680 20d97356 Blue Swirl
    bs->growable = growable;
1681 20d97356 Blue Swirl
1682 20d97356 Blue Swirl
    return ret;
1683 20d97356 Blue Swirl
}
1684 20d97356 Blue Swirl
1685 7c80ab3f Jes Sorensen
static QEMUOptionParameter qcow2_create_options[] = {
1686 20d97356 Blue Swirl
    {
1687 20d97356 Blue Swirl
        .name = BLOCK_OPT_SIZE,
1688 20d97356 Blue Swirl
        .type = OPT_SIZE,
1689 20d97356 Blue Swirl
        .help = "Virtual disk size"
1690 20d97356 Blue Swirl
    },
1691 20d97356 Blue Swirl
    {
1692 6744cbab Kevin Wolf
        .name = BLOCK_OPT_COMPAT_LEVEL,
1693 6744cbab Kevin Wolf
        .type = OPT_STRING,
1694 6744cbab Kevin Wolf
        .help = "Compatibility level (0.10 or 1.1)"
1695 6744cbab Kevin Wolf
    },
1696 6744cbab Kevin Wolf
    {
1697 20d97356 Blue Swirl
        .name = BLOCK_OPT_BACKING_FILE,
1698 20d97356 Blue Swirl
        .type = OPT_STRING,
1699 20d97356 Blue Swirl
        .help = "File name of a base image"
1700 20d97356 Blue Swirl
    },
1701 20d97356 Blue Swirl
    {
1702 20d97356 Blue Swirl
        .name = BLOCK_OPT_BACKING_FMT,
1703 20d97356 Blue Swirl
        .type = OPT_STRING,
1704 20d97356 Blue Swirl
        .help = "Image format of the base image"
1705 20d97356 Blue Swirl
    },
1706 20d97356 Blue Swirl
    {
1707 20d97356 Blue Swirl
        .name = BLOCK_OPT_ENCRYPT,
1708 20d97356 Blue Swirl
        .type = OPT_FLAG,
1709 20d97356 Blue Swirl
        .help = "Encrypt the image"
1710 20d97356 Blue Swirl
    },
1711 20d97356 Blue Swirl
    {
1712 20d97356 Blue Swirl
        .name = BLOCK_OPT_CLUSTER_SIZE,
1713 20d97356 Blue Swirl
        .type = OPT_SIZE,
1714 99cce9fa Kevin Wolf
        .help = "qcow2 cluster size",
1715 99cce9fa Kevin Wolf
        .value = { .n = DEFAULT_CLUSTER_SIZE },
1716 20d97356 Blue Swirl
    },
1717 20d97356 Blue Swirl
    {
1718 20d97356 Blue Swirl
        .name = BLOCK_OPT_PREALLOC,
1719 20d97356 Blue Swirl
        .type = OPT_STRING,
1720 20d97356 Blue Swirl
        .help = "Preallocation mode (allowed values: off, metadata)"
1721 20d97356 Blue Swirl
    },
1722 bfe8043e Stefan Hajnoczi
    {
1723 bfe8043e Stefan Hajnoczi
        .name = BLOCK_OPT_LAZY_REFCOUNTS,
1724 bfe8043e Stefan Hajnoczi
        .type = OPT_FLAG,
1725 bfe8043e Stefan Hajnoczi
        .help = "Postpone refcount updates",
1726 bfe8043e Stefan Hajnoczi
    },
1727 20d97356 Blue Swirl
    { NULL }
1728 20d97356 Blue Swirl
};
1729 20d97356 Blue Swirl
1730 20d97356 Blue Swirl
static BlockDriver bdrv_qcow2 = {
1731 7c80ab3f Jes Sorensen
    .format_name        = "qcow2",
1732 7c80ab3f Jes Sorensen
    .instance_size      = sizeof(BDRVQcowState),
1733 7c80ab3f Jes Sorensen
    .bdrv_probe         = qcow2_probe,
1734 7c80ab3f Jes Sorensen
    .bdrv_open          = qcow2_open,
1735 7c80ab3f Jes Sorensen
    .bdrv_close         = qcow2_close,
1736 21d82ac9 Jeff Cody
    .bdrv_reopen_prepare  = qcow2_reopen_prepare,
1737 7c80ab3f Jes Sorensen
    .bdrv_create        = qcow2_create,
1738 f8a2e5e3 Stefan Hajnoczi
    .bdrv_co_is_allocated = qcow2_co_is_allocated,
1739 7c80ab3f Jes Sorensen
    .bdrv_set_key       = qcow2_set_key,
1740 7c80ab3f Jes Sorensen
    .bdrv_make_empty    = qcow2_make_empty,
1741 7c80ab3f Jes Sorensen
1742 c68b89ac Kevin Wolf
    .bdrv_co_readv          = qcow2_co_readv,
1743 c68b89ac Kevin Wolf
    .bdrv_co_writev         = qcow2_co_writev,
1744 eb489bb1 Kevin Wolf
    .bdrv_co_flush_to_os    = qcow2_co_flush_to_os,
1745 419b19d9 Stefan Hajnoczi
1746 621f0589 Kevin Wolf
    .bdrv_co_write_zeroes   = qcow2_co_write_zeroes,
1747 6db39ae2 Paolo Bonzini
    .bdrv_co_discard        = qcow2_co_discard,
1748 419b19d9 Stefan Hajnoczi
    .bdrv_truncate          = qcow2_truncate,
1749 7c80ab3f Jes Sorensen
    .bdrv_write_compressed  = qcow2_write_compressed,
1750 20d97356 Blue Swirl
1751 20d97356 Blue Swirl
    .bdrv_snapshot_create   = qcow2_snapshot_create,
1752 20d97356 Blue Swirl
    .bdrv_snapshot_goto     = qcow2_snapshot_goto,
1753 20d97356 Blue Swirl
    .bdrv_snapshot_delete   = qcow2_snapshot_delete,
1754 20d97356 Blue Swirl
    .bdrv_snapshot_list     = qcow2_snapshot_list,
1755 51ef6727 edison
    .bdrv_snapshot_load_tmp     = qcow2_snapshot_load_tmp,
1756 7c80ab3f Jes Sorensen
    .bdrv_get_info      = qcow2_get_info,
1757 20d97356 Blue Swirl
1758 7c80ab3f Jes Sorensen
    .bdrv_save_vmstate    = qcow2_save_vmstate,
1759 7c80ab3f Jes Sorensen
    .bdrv_load_vmstate    = qcow2_load_vmstate,
1760 20d97356 Blue Swirl
1761 20d97356 Blue Swirl
    .bdrv_change_backing_file   = qcow2_change_backing_file,
1762 20d97356 Blue Swirl
1763 06d9260f Anthony Liguori
    .bdrv_invalidate_cache      = qcow2_invalidate_cache,
1764 06d9260f Anthony Liguori
1765 7c80ab3f Jes Sorensen
    .create_options = qcow2_create_options,
1766 7c80ab3f Jes Sorensen
    .bdrv_check = qcow2_check,
1767 20d97356 Blue Swirl
};
1768 20d97356 Blue Swirl
1769 5efa9d5a Anthony Liguori
static void bdrv_qcow2_init(void)
1770 5efa9d5a Anthony Liguori
{
1771 5efa9d5a Anthony Liguori
    bdrv_register(&bdrv_qcow2);
1772 5efa9d5a Anthony Liguori
}
1773 5efa9d5a Anthony Liguori
1774 5efa9d5a Anthony Liguori
block_init(bdrv_qcow2_init);