]> git.proxmox.com Git - qemu.git/blame - block/qcow2.c
Merge remote-tracking branch 'jliu/or32' into staging
[qemu.git] / block / qcow2.c
CommitLineData
585f8587
FB
1/*
2 * Block driver for the QCOW version 2 format
5fafdf24 3 *
585f8587 4 * Copyright (c) 2004-2006 Fabrice Bellard
5fafdf24 5 *
585f8587
FB
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
faf07963 24#include "qemu-common.h"
737e150e 25#include "block/block_int.h"
1de7afc9 26#include "qemu/module.h"
585f8587 27#include <zlib.h>
753d9b82 28#include "qemu/aes.h"
f7d0fe02 29#include "block/qcow2.h"
1de7afc9 30#include "qemu/error-report.h"
7b1b5d19 31#include "qapi/qmp/qerror.h"
acdfb480 32#include "qapi/qmp/qbool.h"
3cce16f4 33#include "trace.h"
585f8587
FB
34
35/*
36 Differences with QCOW:
37
38 - Support for multiple incremental snapshots.
39 - Memory management by reference counts.
40 - Clusters which have a reference count of one have the bit
41 QCOW_OFLAG_COPIED to optimize write performance.
5fafdf24 42 - Size of compressed clusters is stored in sectors to reduce bit usage
585f8587
FB
43 in the cluster offsets.
44 - Support for storing additional data (such as the VM state) in the
3b46e624 45 snapshots.
585f8587
FB
46 - If a backing store is used, the cluster size is not constrained
47 (could be backported to QCOW).
48 - L2 tables have always a size of one cluster.
49*/
50
9b80ddf3
AL
51
52typedef struct {
53 uint32_t magic;
54 uint32_t len;
c4217f64 55} QEMU_PACKED QCowExtension;
21d82ac9 56
7c80ab3f
JS
57#define QCOW2_EXT_MAGIC_END 0
58#define QCOW2_EXT_MAGIC_BACKING_FORMAT 0xE2792ACA
cfcc4c62 59#define QCOW2_EXT_MAGIC_FEATURE_TABLE 0x6803f857
9b80ddf3 60
7c80ab3f 61static int qcow2_probe(const uint8_t *buf, int buf_size, const char *filename)
585f8587
FB
62{
63 const QCowHeader *cow_header = (const void *)buf;
3b46e624 64
585f8587
FB
65 if (buf_size >= sizeof(QCowHeader) &&
66 be32_to_cpu(cow_header->magic) == QCOW_MAGIC &&
6744cbab 67 be32_to_cpu(cow_header->version) >= 2)
585f8587
FB
68 return 100;
69 else
70 return 0;
71}
72
9b80ddf3
AL
73
74/*
75 * read qcow2 extension and fill bs
76 * start reading from start_offset
77 * finish reading upon magic of value 0 or when end_offset reached
78 * unknown magic is skipped (future extension this version knows nothing about)
79 * return 0 upon success, non-0 otherwise
80 */
7c80ab3f 81static int qcow2_read_extensions(BlockDriverState *bs, uint64_t start_offset,
3ef6c40a
MR
82 uint64_t end_offset, void **p_feature_table,
83 Error **errp)
9b80ddf3 84{
75bab85c 85 BDRVQcowState *s = bs->opaque;
9b80ddf3
AL
86 QCowExtension ext;
87 uint64_t offset;
75bab85c 88 int ret;
9b80ddf3
AL
89
90#ifdef DEBUG_EXT
7c80ab3f 91 printf("qcow2_read_extensions: start=%ld end=%ld\n", start_offset, end_offset);
9b80ddf3
AL
92#endif
93 offset = start_offset;
94 while (offset < end_offset) {
95
96#ifdef DEBUG_EXT
97 /* Sanity check */
98 if (offset > s->cluster_size)
7c80ab3f 99 printf("qcow2_read_extension: suspicious offset %lu\n", offset);
9b80ddf3 100
9b2260cb 101 printf("attempting to read extended header in offset %lu\n", offset);
9b80ddf3
AL
102#endif
103
3ef6c40a
MR
104 ret = bdrv_pread(bs->file, offset, &ext, sizeof(ext));
105 if (ret < 0) {
106 error_setg_errno(errp, -ret, "qcow2_read_extension: ERROR: "
107 "pread fail from offset %" PRIu64, offset);
9b80ddf3
AL
108 return 1;
109 }
110 be32_to_cpus(&ext.magic);
111 be32_to_cpus(&ext.len);
112 offset += sizeof(ext);
113#ifdef DEBUG_EXT
114 printf("ext.magic = 0x%x\n", ext.magic);
115#endif
64ca6aee 116 if (ext.len > end_offset - offset) {
3ef6c40a 117 error_setg(errp, "Header extension too large");
64ca6aee
KW
118 return -EINVAL;
119 }
120
9b80ddf3 121 switch (ext.magic) {
7c80ab3f 122 case QCOW2_EXT_MAGIC_END:
9b80ddf3 123 return 0;
f965509c 124
7c80ab3f 125 case QCOW2_EXT_MAGIC_BACKING_FORMAT:
f965509c 126 if (ext.len >= sizeof(bs->backing_format)) {
3ef6c40a
MR
127 error_setg(errp, "ERROR: ext_backing_format: len=%u too large"
128 " (>=%zu)", ext.len, sizeof(bs->backing_format));
f965509c
AL
129 return 2;
130 }
3ef6c40a
MR
131 ret = bdrv_pread(bs->file, offset, bs->backing_format, ext.len);
132 if (ret < 0) {
133 error_setg_errno(errp, -ret, "ERROR: ext_backing_format: "
134 "Could not read format name");
f965509c 135 return 3;
3ef6c40a 136 }
f965509c
AL
137 bs->backing_format[ext.len] = '\0';
138#ifdef DEBUG_EXT
139 printf("Qcow2: Got format extension %s\n", bs->backing_format);
140#endif
f965509c
AL
141 break;
142
cfcc4c62
KW
143 case QCOW2_EXT_MAGIC_FEATURE_TABLE:
144 if (p_feature_table != NULL) {
145 void* feature_table = g_malloc0(ext.len + 2 * sizeof(Qcow2Feature));
146 ret = bdrv_pread(bs->file, offset , feature_table, ext.len);
147 if (ret < 0) {
3ef6c40a
MR
148 error_setg_errno(errp, -ret, "ERROR: ext_feature_table: "
149 "Could not read table");
cfcc4c62
KW
150 return ret;
151 }
152
153 *p_feature_table = feature_table;
154 }
155 break;
156
9b80ddf3 157 default:
75bab85c
KW
158 /* unknown magic - save it in case we need to rewrite the header */
159 {
160 Qcow2UnknownHeaderExtension *uext;
161
162 uext = g_malloc0(sizeof(*uext) + ext.len);
163 uext->magic = ext.magic;
164 uext->len = ext.len;
165 QLIST_INSERT_HEAD(&s->unknown_header_ext, uext, next);
166
167 ret = bdrv_pread(bs->file, offset , uext->data, uext->len);
168 if (ret < 0) {
3ef6c40a
MR
169 error_setg_errno(errp, -ret, "ERROR: unknown extension: "
170 "Could not read data");
75bab85c
KW
171 return ret;
172 }
75bab85c 173 }
9b80ddf3
AL
174 break;
175 }
fd29b4bb
KW
176
177 offset += ((ext.len + 7) & ~7);
9b80ddf3
AL
178 }
179
180 return 0;
181}
182
75bab85c
KW
183static void cleanup_unknown_header_ext(BlockDriverState *bs)
184{
185 BDRVQcowState *s = bs->opaque;
186 Qcow2UnknownHeaderExtension *uext, *next;
187
188 QLIST_FOREACH_SAFE(uext, &s->unknown_header_ext, next, next) {
189 QLIST_REMOVE(uext, next);
190 g_free(uext);
191 }
192}
9b80ddf3 193
3ef6c40a
MR
194static void GCC_FMT_ATTR(3, 4) report_unsupported(BlockDriverState *bs,
195 Error **errp, const char *fmt, ...)
6744cbab
KW
196{
197 char msg[64];
198 va_list ap;
199
200 va_start(ap, fmt);
201 vsnprintf(msg, sizeof(msg), fmt, ap);
202 va_end(ap);
203
3ef6c40a
MR
204 error_set(errp, QERR_UNKNOWN_BLOCK_FORMAT_FEATURE, bs->device_name, "qcow2",
205 msg);
6744cbab
KW
206}
207
cfcc4c62 208static void report_unsupported_feature(BlockDriverState *bs,
3ef6c40a 209 Error **errp, Qcow2Feature *table, uint64_t mask)
cfcc4c62
KW
210{
211 while (table && table->name[0] != '\0') {
212 if (table->type == QCOW2_FEAT_TYPE_INCOMPATIBLE) {
213 if (mask & (1 << table->bit)) {
3ef6c40a 214 report_unsupported(bs, errp, "%.46s", table->name);
cfcc4c62
KW
215 mask &= ~(1 << table->bit);
216 }
217 }
218 table++;
219 }
220
221 if (mask) {
3ef6c40a
MR
222 report_unsupported(bs, errp, "Unknown incompatible feature: %" PRIx64,
223 mask);
cfcc4c62
KW
224 }
225}
226
bfe8043e
SH
227/*
228 * Sets the dirty bit and flushes afterwards if necessary.
229 *
230 * The incompatible_features bit is only set if the image file header was
231 * updated successfully. Therefore it is not required to check the return
232 * value of this function.
233 */
280d3735 234int qcow2_mark_dirty(BlockDriverState *bs)
bfe8043e
SH
235{
236 BDRVQcowState *s = bs->opaque;
237 uint64_t val;
238 int ret;
239
240 assert(s->qcow_version >= 3);
241
242 if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) {
243 return 0; /* already dirty */
244 }
245
246 val = cpu_to_be64(s->incompatible_features | QCOW2_INCOMPAT_DIRTY);
247 ret = bdrv_pwrite(bs->file, offsetof(QCowHeader, incompatible_features),
248 &val, sizeof(val));
249 if (ret < 0) {
250 return ret;
251 }
252 ret = bdrv_flush(bs->file);
253 if (ret < 0) {
254 return ret;
255 }
256
257 /* Only treat image as dirty if the header was updated successfully */
258 s->incompatible_features |= QCOW2_INCOMPAT_DIRTY;
259 return 0;
260}
261
c61d0004
SH
262/*
263 * Clears the dirty bit and flushes before if necessary. Only call this
264 * function when there are no pending requests, it does not guard against
265 * concurrent requests dirtying the image.
266 */
267static int qcow2_mark_clean(BlockDriverState *bs)
268{
269 BDRVQcowState *s = bs->opaque;
270
271 if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) {
272 int ret = bdrv_flush(bs);
273 if (ret < 0) {
274 return ret;
275 }
276
277 s->incompatible_features &= ~QCOW2_INCOMPAT_DIRTY;
278 return qcow2_update_header(bs);
279 }
280 return 0;
281}
282
69c98726
MR
283/*
284 * Marks the image as corrupt.
285 */
286int qcow2_mark_corrupt(BlockDriverState *bs)
287{
288 BDRVQcowState *s = bs->opaque;
289
290 s->incompatible_features |= QCOW2_INCOMPAT_CORRUPT;
291 return qcow2_update_header(bs);
292}
293
294/*
295 * Marks the image as consistent, i.e., unsets the corrupt bit, and flushes
296 * before if necessary.
297 */
298int qcow2_mark_consistent(BlockDriverState *bs)
299{
300 BDRVQcowState *s = bs->opaque;
301
302 if (s->incompatible_features & QCOW2_INCOMPAT_CORRUPT) {
303 int ret = bdrv_flush(bs);
304 if (ret < 0) {
305 return ret;
306 }
307
308 s->incompatible_features &= ~QCOW2_INCOMPAT_CORRUPT;
309 return qcow2_update_header(bs);
310 }
311 return 0;
312}
313
acbe5982
SH
314static int qcow2_check(BlockDriverState *bs, BdrvCheckResult *result,
315 BdrvCheckMode fix)
316{
317 int ret = qcow2_check_refcounts(bs, result, fix);
318 if (ret < 0) {
319 return ret;
320 }
321
322 if (fix && result->check_errors == 0 && result->corruptions == 0) {
24530f3e
MR
323 ret = qcow2_mark_clean(bs);
324 if (ret < 0) {
325 return ret;
326 }
327 return qcow2_mark_consistent(bs);
acbe5982
SH
328 }
329 return ret;
330}
331
74c4510a
KW
332static QemuOptsList qcow2_runtime_opts = {
333 .name = "qcow2",
334 .head = QTAILQ_HEAD_INITIALIZER(qcow2_runtime_opts.head),
335 .desc = {
336 {
64aa99d3 337 .name = QCOW2_OPT_LAZY_REFCOUNTS,
74c4510a
KW
338 .type = QEMU_OPT_BOOL,
339 .help = "Postpone refcount updates",
340 },
67af674e
KW
341 {
342 .name = QCOW2_OPT_DISCARD_REQUEST,
343 .type = QEMU_OPT_BOOL,
344 .help = "Pass guest discard requests to the layer below",
345 },
346 {
347 .name = QCOW2_OPT_DISCARD_SNAPSHOT,
348 .type = QEMU_OPT_BOOL,
349 .help = "Generate discard requests when snapshot related space "
350 "is freed",
351 },
352 {
353 .name = QCOW2_OPT_DISCARD_OTHER,
354 .type = QEMU_OPT_BOOL,
355 .help = "Generate discard requests when other clusters are freed",
356 },
05de7e86
MR
357 {
358 .name = QCOW2_OPT_OVERLAP,
359 .type = QEMU_OPT_STRING,
360 .help = "Selects which overlap checks to perform from a range of "
361 "templates (none, constant, cached, all)",
362 },
363 {
364 .name = QCOW2_OPT_OVERLAP_MAIN_HEADER,
365 .type = QEMU_OPT_BOOL,
366 .help = "Check for unintended writes into the main qcow2 header",
367 },
368 {
369 .name = QCOW2_OPT_OVERLAP_ACTIVE_L1,
370 .type = QEMU_OPT_BOOL,
371 .help = "Check for unintended writes into the active L1 table",
372 },
373 {
374 .name = QCOW2_OPT_OVERLAP_ACTIVE_L2,
375 .type = QEMU_OPT_BOOL,
376 .help = "Check for unintended writes into an active L2 table",
377 },
378 {
379 .name = QCOW2_OPT_OVERLAP_REFCOUNT_TABLE,
380 .type = QEMU_OPT_BOOL,
381 .help = "Check for unintended writes into the refcount table",
382 },
383 {
384 .name = QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK,
385 .type = QEMU_OPT_BOOL,
386 .help = "Check for unintended writes into a refcount block",
387 },
388 {
389 .name = QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE,
390 .type = QEMU_OPT_BOOL,
391 .help = "Check for unintended writes into the snapshot table",
392 },
393 {
394 .name = QCOW2_OPT_OVERLAP_INACTIVE_L1,
395 .type = QEMU_OPT_BOOL,
396 .help = "Check for unintended writes into an inactive L1 table",
397 },
398 {
399 .name = QCOW2_OPT_OVERLAP_INACTIVE_L2,
400 .type = QEMU_OPT_BOOL,
401 .help = "Check for unintended writes into an inactive L2 table",
402 },
74c4510a
KW
403 { /* end of list */ }
404 },
405};
406
4092e99d
MR
407static const char *overlap_bool_option_names[QCOW2_OL_MAX_BITNR] = {
408 [QCOW2_OL_MAIN_HEADER_BITNR] = QCOW2_OPT_OVERLAP_MAIN_HEADER,
409 [QCOW2_OL_ACTIVE_L1_BITNR] = QCOW2_OPT_OVERLAP_ACTIVE_L1,
410 [QCOW2_OL_ACTIVE_L2_BITNR] = QCOW2_OPT_OVERLAP_ACTIVE_L2,
411 [QCOW2_OL_REFCOUNT_TABLE_BITNR] = QCOW2_OPT_OVERLAP_REFCOUNT_TABLE,
412 [QCOW2_OL_REFCOUNT_BLOCK_BITNR] = QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK,
413 [QCOW2_OL_SNAPSHOT_TABLE_BITNR] = QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE,
414 [QCOW2_OL_INACTIVE_L1_BITNR] = QCOW2_OPT_OVERLAP_INACTIVE_L1,
415 [QCOW2_OL_INACTIVE_L2_BITNR] = QCOW2_OPT_OVERLAP_INACTIVE_L2,
416};
417
015a1036
MR
418static int qcow2_open(BlockDriverState *bs, QDict *options, int flags,
419 Error **errp)
585f8587
FB
420{
421 BDRVQcowState *s = bs->opaque;
6d85a57e 422 int len, i, ret = 0;
585f8587 423 QCowHeader header;
74c4510a
KW
424 QemuOpts *opts;
425 Error *local_err = NULL;
9b80ddf3 426 uint64_t ext_end;
2cf7cfa1 427 uint64_t l1_vm_state_index;
1fa5cc83
MR
428 const char *opt_overlap_check;
429 int overlap_check_template = 0;
585f8587 430
6d85a57e
JS
431 ret = bdrv_pread(bs->file, 0, &header, sizeof(header));
432 if (ret < 0) {
3ef6c40a 433 error_setg_errno(errp, -ret, "Could not read qcow2 header");
585f8587 434 goto fail;
6d85a57e 435 }
585f8587
FB
436 be32_to_cpus(&header.magic);
437 be32_to_cpus(&header.version);
438 be64_to_cpus(&header.backing_file_offset);
439 be32_to_cpus(&header.backing_file_size);
440 be64_to_cpus(&header.size);
441 be32_to_cpus(&header.cluster_bits);
442 be32_to_cpus(&header.crypt_method);
443 be64_to_cpus(&header.l1_table_offset);
444 be32_to_cpus(&header.l1_size);
445 be64_to_cpus(&header.refcount_table_offset);
446 be32_to_cpus(&header.refcount_table_clusters);
447 be64_to_cpus(&header.snapshots_offset);
448 be32_to_cpus(&header.nb_snapshots);
3b46e624 449
e8cdcec1 450 if (header.magic != QCOW_MAGIC) {
3ef6c40a 451 error_setg(errp, "Image is not in qcow2 format");
15bac0d5 452 ret = -EMEDIUMTYPE;
585f8587 453 goto fail;
6d85a57e 454 }
6744cbab 455 if (header.version < 2 || header.version > 3) {
3ef6c40a 456 report_unsupported(bs, errp, "QCOW version %d", header.version);
6744cbab
KW
457 ret = -ENOTSUP;
458 goto fail;
459 }
460
461 s->qcow_version = header.version;
462
463 /* Initialise version 3 header fields */
464 if (header.version == 2) {
465 header.incompatible_features = 0;
466 header.compatible_features = 0;
467 header.autoclear_features = 0;
468 header.refcount_order = 4;
469 header.header_length = 72;
470 } else {
471 be64_to_cpus(&header.incompatible_features);
472 be64_to_cpus(&header.compatible_features);
473 be64_to_cpus(&header.autoclear_features);
474 be32_to_cpus(&header.refcount_order);
475 be32_to_cpus(&header.header_length);
476 }
477
478 if (header.header_length > sizeof(header)) {
479 s->unknown_header_fields_size = header.header_length - sizeof(header);
480 s->unknown_header_fields = g_malloc(s->unknown_header_fields_size);
481 ret = bdrv_pread(bs->file, sizeof(header), s->unknown_header_fields,
482 s->unknown_header_fields_size);
483 if (ret < 0) {
3ef6c40a
MR
484 error_setg_errno(errp, -ret, "Could not read unknown qcow2 header "
485 "fields");
6744cbab
KW
486 goto fail;
487 }
488 }
489
cfcc4c62
KW
490 if (header.backing_file_offset) {
491 ext_end = header.backing_file_offset;
492 } else {
493 ext_end = 1 << header.cluster_bits;
494 }
495
6744cbab
KW
496 /* Handle feature bits */
497 s->incompatible_features = header.incompatible_features;
498 s->compatible_features = header.compatible_features;
499 s->autoclear_features = header.autoclear_features;
500
c61d0004 501 if (s->incompatible_features & ~QCOW2_INCOMPAT_MASK) {
cfcc4c62
KW
502 void *feature_table = NULL;
503 qcow2_read_extensions(bs, header.header_length, ext_end,
3ef6c40a
MR
504 &feature_table, NULL);
505 report_unsupported_feature(bs, errp, feature_table,
c61d0004
SH
506 s->incompatible_features &
507 ~QCOW2_INCOMPAT_MASK);
6744cbab
KW
508 ret = -ENOTSUP;
509 goto fail;
510 }
511
69c98726
MR
512 if (s->incompatible_features & QCOW2_INCOMPAT_CORRUPT) {
513 /* Corrupt images may not be written to unless they are being repaired
514 */
515 if ((flags & BDRV_O_RDWR) && !(flags & BDRV_O_CHECK)) {
3ef6c40a
MR
516 error_setg(errp, "qcow2: Image is corrupt; cannot be opened "
517 "read/write");
69c98726
MR
518 ret = -EACCES;
519 goto fail;
520 }
521 }
522
6744cbab
KW
523 /* Check support for various header values */
524 if (header.refcount_order != 4) {
3ef6c40a 525 report_unsupported(bs, errp, "%d bit reference counts",
6744cbab 526 1 << header.refcount_order);
e8cdcec1
KW
527 ret = -ENOTSUP;
528 goto fail;
529 }
b6481f37 530 s->refcount_order = header.refcount_order;
6744cbab 531
d191d12d 532 if (header.cluster_bits < MIN_CLUSTER_BITS ||
6d85a57e 533 header.cluster_bits > MAX_CLUSTER_BITS) {
3ef6c40a 534 error_setg(errp, "Unsupported cluster size: 2^%i", header.cluster_bits);
6d85a57e 535 ret = -EINVAL;
585f8587 536 goto fail;
6d85a57e
JS
537 }
538 if (header.crypt_method > QCOW_CRYPT_AES) {
3ef6c40a
MR
539 error_setg(errp, "Unsupported encryption method: %i",
540 header.crypt_method);
6d85a57e 541 ret = -EINVAL;
585f8587 542 goto fail;
6d85a57e 543 }
585f8587 544 s->crypt_method_header = header.crypt_method;
6d85a57e 545 if (s->crypt_method_header) {
585f8587 546 bs->encrypted = 1;
6d85a57e 547 }
585f8587
FB
548 s->cluster_bits = header.cluster_bits;
549 s->cluster_size = 1 << s->cluster_bits;
550 s->cluster_sectors = 1 << (s->cluster_bits - 9);
551 s->l2_bits = s->cluster_bits - 3; /* L2 is always one cluster */
552 s->l2_size = 1 << s->l2_bits;
553 bs->total_sectors = header.size / 512;
554 s->csize_shift = (62 - (s->cluster_bits - 8));
555 s->csize_mask = (1 << (s->cluster_bits - 8)) - 1;
556 s->cluster_offset_mask = (1LL << s->csize_shift) - 1;
557 s->refcount_table_offset = header.refcount_table_offset;
5fafdf24 558 s->refcount_table_size =
585f8587
FB
559 header.refcount_table_clusters << (s->cluster_bits - 3);
560
561 s->snapshots_offset = header.snapshots_offset;
562 s->nb_snapshots = header.nb_snapshots;
563
564 /* read the level 1 table */
565 s->l1_size = header.l1_size;
2cf7cfa1
KW
566
567 l1_vm_state_index = size_to_l1(s, header.size);
568 if (l1_vm_state_index > INT_MAX) {
3ef6c40a 569 error_setg(errp, "Image is too big");
2cf7cfa1
KW
570 ret = -EFBIG;
571 goto fail;
572 }
573 s->l1_vm_state_index = l1_vm_state_index;
574
585f8587
FB
575 /* the L1 table must contain at least enough entries to put
576 header.size bytes */
6d85a57e 577 if (s->l1_size < s->l1_vm_state_index) {
3ef6c40a 578 error_setg(errp, "L1 table is too small");
6d85a57e 579 ret = -EINVAL;
585f8587 580 goto fail;
6d85a57e 581 }
585f8587 582 s->l1_table_offset = header.l1_table_offset;
d191d12d 583 if (s->l1_size > 0) {
7267c094 584 s->l1_table = g_malloc0(
d191d12d 585 align_offset(s->l1_size * sizeof(uint64_t), 512));
6d85a57e
JS
586 ret = bdrv_pread(bs->file, s->l1_table_offset, s->l1_table,
587 s->l1_size * sizeof(uint64_t));
588 if (ret < 0) {
3ef6c40a 589 error_setg_errno(errp, -ret, "Could not read L1 table");
d191d12d 590 goto fail;
6d85a57e 591 }
d191d12d
SW
592 for(i = 0;i < s->l1_size; i++) {
593 be64_to_cpus(&s->l1_table[i]);
594 }
585f8587 595 }
29c1a730
KW
596
597 /* alloc L2 table/refcount block cache */
6af4e9ea
PB
598 s->l2_table_cache = qcow2_cache_create(bs, L2_CACHE_SIZE);
599 s->refcount_block_cache = qcow2_cache_create(bs, REFCOUNT_CACHE_SIZE);
29c1a730 600
7267c094 601 s->cluster_cache = g_malloc(s->cluster_size);
585f8587 602 /* one more sector for decompressed data alignment */
dea43a65 603 s->cluster_data = qemu_blockalign(bs, QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size
095a9c58 604 + 512);
585f8587 605 s->cluster_cache_offset = -1;
06d9260f 606 s->flags = flags;
3b46e624 607
6d85a57e
JS
608 ret = qcow2_refcount_init(bs);
609 if (ret != 0) {
3ef6c40a 610 error_setg_errno(errp, -ret, "Could not initialize refcount handling");
585f8587 611 goto fail;
6d85a57e 612 }
585f8587 613
72cf2d4f 614 QLIST_INIT(&s->cluster_allocs);
0b919fae 615 QTAILQ_INIT(&s->discards);
f214978a 616
9b80ddf3 617 /* read qcow2 extensions */
3ef6c40a
MR
618 if (qcow2_read_extensions(bs, header.header_length, ext_end, NULL,
619 &local_err)) {
620 error_propagate(errp, local_err);
6d85a57e 621 ret = -EINVAL;
9b80ddf3 622 goto fail;
6d85a57e 623 }
9b80ddf3 624
585f8587
FB
625 /* read the backing file name */
626 if (header.backing_file_offset != 0) {
627 len = header.backing_file_size;
6d85a57e 628 if (len > 1023) {
585f8587 629 len = 1023;
6d85a57e
JS
630 }
631 ret = bdrv_pread(bs->file, header.backing_file_offset,
632 bs->backing_file, len);
633 if (ret < 0) {
3ef6c40a 634 error_setg_errno(errp, -ret, "Could not read backing file name");
585f8587 635 goto fail;
6d85a57e 636 }
585f8587
FB
637 bs->backing_file[len] = '\0';
638 }
42deb29f
KW
639
640 ret = qcow2_read_snapshots(bs);
641 if (ret < 0) {
3ef6c40a 642 error_setg_errno(errp, -ret, "Could not read snapshots");
585f8587 643 goto fail;
6d85a57e 644 }
585f8587 645
af7b708d
SH
646 /* Clear unknown autoclear feature bits */
647 if (!bs->read_only && s->autoclear_features != 0) {
648 s->autoclear_features = 0;
649 ret = qcow2_update_header(bs);
650 if (ret < 0) {
3ef6c40a 651 error_setg_errno(errp, -ret, "Could not update qcow2 header");
af7b708d
SH
652 goto fail;
653 }
654 }
655
68d100e9
KW
656 /* Initialise locks */
657 qemu_co_mutex_init(&s->lock);
658
c61d0004 659 /* Repair image if dirty */
058f8f16
SH
660 if (!(flags & BDRV_O_CHECK) && !bs->read_only &&
661 (s->incompatible_features & QCOW2_INCOMPAT_DIRTY)) {
c61d0004
SH
662 BdrvCheckResult result = {0};
663
acbe5982 664 ret = qcow2_check(bs, &result, BDRV_FIX_ERRORS);
c61d0004 665 if (ret < 0) {
3ef6c40a 666 error_setg_errno(errp, -ret, "Could not repair dirty image");
c61d0004
SH
667 goto fail;
668 }
669 }
670
74c4510a
KW
671 /* Enable lazy_refcounts according to image and command line options */
672 opts = qemu_opts_create_nofail(&qcow2_runtime_opts);
673 qemu_opts_absorb_qdict(opts, options, &local_err);
674 if (error_is_set(&local_err)) {
3ef6c40a 675 error_propagate(errp, local_err);
74c4510a
KW
676 ret = -EINVAL;
677 goto fail;
678 }
679
acdfb480 680 s->use_lazy_refcounts = qemu_opt_get_bool(opts, QCOW2_OPT_LAZY_REFCOUNTS,
74c4510a
KW
681 (s->compatible_features & QCOW2_COMPAT_LAZY_REFCOUNTS));
682
67af674e
KW
683 s->discard_passthrough[QCOW2_DISCARD_NEVER] = false;
684 s->discard_passthrough[QCOW2_DISCARD_ALWAYS] = true;
685 s->discard_passthrough[QCOW2_DISCARD_REQUEST] =
686 qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_REQUEST,
687 flags & BDRV_O_UNMAP);
688 s->discard_passthrough[QCOW2_DISCARD_SNAPSHOT] =
689 qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_SNAPSHOT, true);
690 s->discard_passthrough[QCOW2_DISCARD_OTHER] =
691 qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_OTHER, false);
692
1fa5cc83
MR
693 opt_overlap_check = qemu_opt_get(opts, "overlap-check") ?: "cached";
694 if (!strcmp(opt_overlap_check, "none")) {
695 overlap_check_template = 0;
696 } else if (!strcmp(opt_overlap_check, "constant")) {
697 overlap_check_template = QCOW2_OL_CONSTANT;
698 } else if (!strcmp(opt_overlap_check, "cached")) {
699 overlap_check_template = QCOW2_OL_CACHED;
700 } else if (!strcmp(opt_overlap_check, "all")) {
701 overlap_check_template = QCOW2_OL_ALL;
702 } else {
703 error_setg(errp, "Unsupported value '%s' for qcow2 option "
704 "'overlap-check'. Allowed are either of the following: "
705 "none, constant, cached, all", opt_overlap_check);
706 qemu_opts_del(opts);
707 ret = -EINVAL;
708 goto fail;
709 }
710
711 s->overlap_check = 0;
712 for (i = 0; i < QCOW2_OL_MAX_BITNR; i++) {
713 /* overlap-check defines a template bitmask, but every flag may be
714 * overwritten through the associated boolean option */
715 s->overlap_check |=
716 qemu_opt_get_bool(opts, overlap_bool_option_names[i],
717 overlap_check_template & (1 << i)) << i;
718 }
3e355390 719
74c4510a
KW
720 qemu_opts_del(opts);
721
722 if (s->use_lazy_refcounts && s->qcow_version < 3) {
3ef6c40a
MR
723 error_setg(errp, "Lazy refcounts require a qcow2 image with at least "
724 "qemu 1.1 compatibility level");
74c4510a
KW
725 ret = -EINVAL;
726 goto fail;
727 }
728
585f8587 729#ifdef DEBUG_ALLOC
6cbc3031
PH
730 {
731 BdrvCheckResult result = {0};
b35278f7 732 qcow2_check_refcounts(bs, &result, 0);
6cbc3031 733 }
585f8587 734#endif
6d85a57e 735 return ret;
585f8587
FB
736
737 fail:
6744cbab 738 g_free(s->unknown_header_fields);
75bab85c 739 cleanup_unknown_header_ext(bs);
ed6ccf0f
KW
740 qcow2_free_snapshots(bs);
741 qcow2_refcount_close(bs);
7267c094 742 g_free(s->l1_table);
cf93980e
MR
743 /* else pre-write overlap checks in cache_destroy may crash */
744 s->l1_table = NULL;
29c1a730
KW
745 if (s->l2_table_cache) {
746 qcow2_cache_destroy(bs, s->l2_table_cache);
747 }
7267c094 748 g_free(s->cluster_cache);
dea43a65 749 qemu_vfree(s->cluster_data);
6d85a57e 750 return ret;
585f8587
FB
751}
752
7c80ab3f 753static int qcow2_set_key(BlockDriverState *bs, const char *key)
585f8587
FB
754{
755 BDRVQcowState *s = bs->opaque;
756 uint8_t keybuf[16];
757 int len, i;
3b46e624 758
585f8587
FB
759 memset(keybuf, 0, 16);
760 len = strlen(key);
761 if (len > 16)
762 len = 16;
763 /* XXX: we could compress the chars to 7 bits to increase
764 entropy */
765 for(i = 0;i < len;i++) {
766 keybuf[i] = key[i];
767 }
768 s->crypt_method = s->crypt_method_header;
769
770 if (AES_set_encrypt_key(keybuf, 128, &s->aes_encrypt_key) != 0)
771 return -1;
772 if (AES_set_decrypt_key(keybuf, 128, &s->aes_decrypt_key) != 0)
773 return -1;
774#if 0
775 /* test */
776 {
777 uint8_t in[16];
778 uint8_t out[16];
779 uint8_t tmp[16];
780 for(i=0;i<16;i++)
781 in[i] = i;
782 AES_encrypt(in, tmp, &s->aes_encrypt_key);
783 AES_decrypt(tmp, out, &s->aes_decrypt_key);
784 for(i = 0; i < 16; i++)
785 printf(" %02x", tmp[i]);
786 printf("\n");
787 for(i = 0; i < 16; i++)
788 printf(" %02x", out[i]);
789 printf("\n");
790 }
791#endif
792 return 0;
793}
794
21d82ac9
JC
795/* We have nothing to do for QCOW2 reopen, stubs just return
796 * success */
797static int qcow2_reopen_prepare(BDRVReopenState *state,
798 BlockReopenQueue *queue, Error **errp)
799{
800 return 0;
801}
802
b6b8a333 803static int64_t coroutine_fn qcow2_co_get_block_status(BlockDriverState *bs,
f8a2e5e3 804 int64_t sector_num, int nb_sectors, int *pnum)
585f8587 805{
f8a2e5e3 806 BDRVQcowState *s = bs->opaque;
585f8587 807 uint64_t cluster_offset;
4bc74be9
PB
808 int index_in_cluster, ret;
809 int64_t status = 0;
585f8587 810
095a9c58 811 *pnum = nb_sectors;
f8a2e5e3 812 qemu_co_mutex_lock(&s->lock);
1c46efaa 813 ret = qcow2_get_cluster_offset(bs, sector_num << 9, pnum, &cluster_offset);
f8a2e5e3 814 qemu_co_mutex_unlock(&s->lock);
1c46efaa 815 if (ret < 0) {
d663640c 816 return ret;
1c46efaa 817 }
095a9c58 818
4bc74be9
PB
819 if (cluster_offset != 0 && ret != QCOW2_CLUSTER_COMPRESSED &&
820 !s->crypt_method) {
821 index_in_cluster = sector_num & (s->cluster_sectors - 1);
822 cluster_offset |= (index_in_cluster << BDRV_SECTOR_BITS);
823 status |= BDRV_BLOCK_OFFSET_VALID | cluster_offset;
824 }
825 if (ret == QCOW2_CLUSTER_ZERO) {
826 status |= BDRV_BLOCK_ZERO;
827 } else if (ret != QCOW2_CLUSTER_UNALLOCATED) {
828 status |= BDRV_BLOCK_DATA;
829 }
830 return status;
585f8587
FB
831}
832
a9465922 833/* handle reading after the end of the backing file */
bd28f835
KW
834int qcow2_backing_read1(BlockDriverState *bs, QEMUIOVector *qiov,
835 int64_t sector_num, int nb_sectors)
a9465922
FB
836{
837 int n1;
838 if ((sector_num + nb_sectors) <= bs->total_sectors)
839 return nb_sectors;
840 if (sector_num >= bs->total_sectors)
841 n1 = 0;
842 else
843 n1 = bs->total_sectors - sector_num;
bd28f835 844
3d9b4925 845 qemu_iovec_memset(qiov, 512 * n1, 0, 512 * (nb_sectors - n1));
bd28f835 846
a9465922
FB
847 return n1;
848}
849
a968168c 850static coroutine_fn int qcow2_co_readv(BlockDriverState *bs, int64_t sector_num,
3fc48d09 851 int remaining_sectors, QEMUIOVector *qiov)
585f8587 852{
585f8587 853 BDRVQcowState *s = bs->opaque;
a9465922 854 int index_in_cluster, n1;
68d100e9 855 int ret;
faf575c1 856 int cur_nr_sectors; /* number of sectors in current iteration */
c2bdd990 857 uint64_t cluster_offset = 0;
3fc48d09
FZ
858 uint64_t bytes_done = 0;
859 QEMUIOVector hd_qiov;
860 uint8_t *cluster_data = NULL;
585f8587 861
3fc48d09
FZ
862 qemu_iovec_init(&hd_qiov, qiov->niov);
863
864 qemu_co_mutex_lock(&s->lock);
865
866 while (remaining_sectors != 0) {
bd28f835 867
5ebaa27e 868 /* prepare next request */
3fc48d09 869 cur_nr_sectors = remaining_sectors;
5ebaa27e
FZ
870 if (s->crypt_method) {
871 cur_nr_sectors = MIN(cur_nr_sectors,
872 QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors);
585f8587 873 }
5ebaa27e 874
3fc48d09 875 ret = qcow2_get_cluster_offset(bs, sector_num << 9,
5ebaa27e 876 &cur_nr_sectors, &cluster_offset);
8af36488 877 if (ret < 0) {
3fc48d09 878 goto fail;
8af36488 879 }
bd28f835 880
3fc48d09 881 index_in_cluster = sector_num & (s->cluster_sectors - 1);
c87c0672 882
3fc48d09 883 qemu_iovec_reset(&hd_qiov);
1b093c48 884 qemu_iovec_concat(&hd_qiov, qiov, bytes_done,
5ebaa27e
FZ
885 cur_nr_sectors * 512);
886
68d000a3
KW
887 switch (ret) {
888 case QCOW2_CLUSTER_UNALLOCATED:
5ebaa27e
FZ
889
890 if (bs->backing_hd) {
891 /* read from the base image */
3fc48d09
FZ
892 n1 = qcow2_backing_read1(bs->backing_hd, &hd_qiov,
893 sector_num, cur_nr_sectors);
5ebaa27e
FZ
894 if (n1 > 0) {
895 BLKDBG_EVENT(bs->file, BLKDBG_READ_BACKING_AIO);
896 qemu_co_mutex_unlock(&s->lock);
3fc48d09
FZ
897 ret = bdrv_co_readv(bs->backing_hd, sector_num,
898 n1, &hd_qiov);
5ebaa27e
FZ
899 qemu_co_mutex_lock(&s->lock);
900 if (ret < 0) {
3fc48d09 901 goto fail;
5ebaa27e
FZ
902 }
903 }
904 } else {
905 /* Note: in this case, no need to wait */
3d9b4925 906 qemu_iovec_memset(&hd_qiov, 0, 0, 512 * cur_nr_sectors);
5ebaa27e 907 }
68d000a3
KW
908 break;
909
6377af48 910 case QCOW2_CLUSTER_ZERO:
3d9b4925 911 qemu_iovec_memset(&hd_qiov, 0, 0, 512 * cur_nr_sectors);
6377af48
KW
912 break;
913
68d000a3 914 case QCOW2_CLUSTER_COMPRESSED:
5ebaa27e
FZ
915 /* add AIO support for compressed blocks ? */
916 ret = qcow2_decompress_cluster(bs, cluster_offset);
917 if (ret < 0) {
3fc48d09 918 goto fail;
bd28f835
KW
919 }
920
03396148 921 qemu_iovec_from_buf(&hd_qiov, 0,
5ebaa27e 922 s->cluster_cache + index_in_cluster * 512,
faf575c1 923 512 * cur_nr_sectors);
68d000a3
KW
924 break;
925
926 case QCOW2_CLUSTER_NORMAL:
5ebaa27e 927 if ((cluster_offset & 511) != 0) {
3fc48d09
FZ
928 ret = -EIO;
929 goto fail;
5ebaa27e 930 }
bd28f835 931
5ebaa27e
FZ
932 if (s->crypt_method) {
933 /*
934 * For encrypted images, read everything into a temporary
935 * contiguous buffer on which the AES functions can work.
936 */
3fc48d09
FZ
937 if (!cluster_data) {
938 cluster_data =
dea43a65 939 qemu_blockalign(bs, QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size);
5ebaa27e
FZ
940 }
941
942 assert(cur_nr_sectors <=
943 QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors);
3fc48d09
FZ
944 qemu_iovec_reset(&hd_qiov);
945 qemu_iovec_add(&hd_qiov, cluster_data,
5ebaa27e
FZ
946 512 * cur_nr_sectors);
947 }
948
949 BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO);
950 qemu_co_mutex_unlock(&s->lock);
951 ret = bdrv_co_readv(bs->file,
952 (cluster_offset >> 9) + index_in_cluster,
3fc48d09 953 cur_nr_sectors, &hd_qiov);
5ebaa27e
FZ
954 qemu_co_mutex_lock(&s->lock);
955 if (ret < 0) {
3fc48d09 956 goto fail;
5ebaa27e
FZ
957 }
958 if (s->crypt_method) {
3fc48d09
FZ
959 qcow2_encrypt_sectors(s, sector_num, cluster_data,
960 cluster_data, cur_nr_sectors, 0, &s->aes_decrypt_key);
03396148
MT
961 qemu_iovec_from_buf(qiov, bytes_done,
962 cluster_data, 512 * cur_nr_sectors);
5ebaa27e 963 }
68d000a3
KW
964 break;
965
966 default:
967 g_assert_not_reached();
968 ret = -EIO;
969 goto fail;
faf575c1 970 }
f141eafe 971
3fc48d09
FZ
972 remaining_sectors -= cur_nr_sectors;
973 sector_num += cur_nr_sectors;
974 bytes_done += cur_nr_sectors * 512;
5ebaa27e 975 }
3fc48d09 976 ret = 0;
faf575c1 977
3fc48d09 978fail:
68d100e9 979 qemu_co_mutex_unlock(&s->lock);
42496d62 980
3fc48d09 981 qemu_iovec_destroy(&hd_qiov);
dea43a65 982 qemu_vfree(cluster_data);
68d100e9
KW
983
984 return ret;
585f8587
FB
985}
986
a968168c 987static coroutine_fn int qcow2_co_writev(BlockDriverState *bs,
3fc48d09
FZ
988 int64_t sector_num,
989 int remaining_sectors,
990 QEMUIOVector *qiov)
585f8587 991{
585f8587 992 BDRVQcowState *s = bs->opaque;
585f8587 993 int index_in_cluster;
095a9c58 994 int n_end;
68d100e9 995 int ret;
faf575c1 996 int cur_nr_sectors; /* number of sectors in current iteration */
c2bdd990 997 uint64_t cluster_offset;
3fc48d09
FZ
998 QEMUIOVector hd_qiov;
999 uint64_t bytes_done = 0;
1000 uint8_t *cluster_data = NULL;
8d2497c3 1001 QCowL2Meta *l2meta = NULL;
c2271403 1002
3cce16f4
KW
1003 trace_qcow2_writev_start_req(qemu_coroutine_self(), sector_num,
1004 remaining_sectors);
1005
3fc48d09
FZ
1006 qemu_iovec_init(&hd_qiov, qiov->niov);
1007
1008 s->cluster_cache_offset = -1; /* disable compressed cache */
3b46e624 1009
3fc48d09
FZ
1010 qemu_co_mutex_lock(&s->lock);
1011
1012 while (remaining_sectors != 0) {
1013
f50f88b9 1014 l2meta = NULL;
cf5c1a23 1015
3cce16f4 1016 trace_qcow2_writev_start_part(qemu_coroutine_self());
3fc48d09
FZ
1017 index_in_cluster = sector_num & (s->cluster_sectors - 1);
1018 n_end = index_in_cluster + remaining_sectors;
5ebaa27e
FZ
1019 if (s->crypt_method &&
1020 n_end > QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors) {
1021 n_end = QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors;
1022 }
095a9c58 1023
3fc48d09 1024 ret = qcow2_alloc_cluster_offset(bs, sector_num << 9,
f50f88b9 1025 index_in_cluster, n_end, &cur_nr_sectors, &cluster_offset, &l2meta);
5ebaa27e 1026 if (ret < 0) {
3fc48d09 1027 goto fail;
5ebaa27e 1028 }
148da7ea 1029
5ebaa27e 1030 assert((cluster_offset & 511) == 0);
148da7ea 1031
3fc48d09 1032 qemu_iovec_reset(&hd_qiov);
1b093c48 1033 qemu_iovec_concat(&hd_qiov, qiov, bytes_done,
5ebaa27e 1034 cur_nr_sectors * 512);
6f5f060b 1035
5ebaa27e 1036 if (s->crypt_method) {
3fc48d09 1037 if (!cluster_data) {
dea43a65 1038 cluster_data = qemu_blockalign(bs, QCOW_MAX_CRYPT_CLUSTERS *
5ebaa27e
FZ
1039 s->cluster_size);
1040 }
6f5f060b 1041
3fc48d09 1042 assert(hd_qiov.size <=
5ebaa27e 1043 QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size);
d5e6b161 1044 qemu_iovec_to_buf(&hd_qiov, 0, cluster_data, hd_qiov.size);
6f5f060b 1045
3fc48d09
FZ
1046 qcow2_encrypt_sectors(s, sector_num, cluster_data,
1047 cluster_data, cur_nr_sectors, 1, &s->aes_encrypt_key);
6f5f060b 1048
3fc48d09
FZ
1049 qemu_iovec_reset(&hd_qiov);
1050 qemu_iovec_add(&hd_qiov, cluster_data,
5ebaa27e
FZ
1051 cur_nr_sectors * 512);
1052 }
6f5f060b 1053
231bb267 1054 ret = qcow2_pre_write_overlap_check(bs, 0,
cf93980e
MR
1055 cluster_offset + index_in_cluster * BDRV_SECTOR_SIZE,
1056 cur_nr_sectors * BDRV_SECTOR_SIZE);
1057 if (ret < 0) {
1058 goto fail;
1059 }
1060
5ebaa27e 1061 qemu_co_mutex_unlock(&s->lock);
67a7a0eb 1062 BLKDBG_EVENT(bs->file, BLKDBG_WRITE_AIO);
3cce16f4
KW
1063 trace_qcow2_writev_data(qemu_coroutine_self(),
1064 (cluster_offset >> 9) + index_in_cluster);
5ebaa27e
FZ
1065 ret = bdrv_co_writev(bs->file,
1066 (cluster_offset >> 9) + index_in_cluster,
3fc48d09 1067 cur_nr_sectors, &hd_qiov);
5ebaa27e
FZ
1068 qemu_co_mutex_lock(&s->lock);
1069 if (ret < 0) {
3fc48d09 1070 goto fail;
5ebaa27e 1071 }
f141eafe 1072
88c6588c
KW
1073 while (l2meta != NULL) {
1074 QCowL2Meta *next;
1075
f50f88b9
KW
1076 ret = qcow2_alloc_cluster_link_l2(bs, l2meta);
1077 if (ret < 0) {
1078 goto fail;
1079 }
faf575c1 1080
4e95314e
KW
1081 /* Take the request off the list of running requests */
1082 if (l2meta->nb_clusters != 0) {
1083 QLIST_REMOVE(l2meta, next_in_flight);
1084 }
1085
4e95314e 1086 qemu_co_queue_restart_all(&l2meta->dependent_requests);
4e95314e 1087
88c6588c 1088 next = l2meta->next;
f50f88b9 1089 g_free(l2meta);
88c6588c 1090 l2meta = next;
f50f88b9 1091 }
0fa9131a 1092
3fc48d09
FZ
1093 remaining_sectors -= cur_nr_sectors;
1094 sector_num += cur_nr_sectors;
1095 bytes_done += cur_nr_sectors * 512;
3cce16f4 1096 trace_qcow2_writev_done_part(qemu_coroutine_self(), cur_nr_sectors);
5ebaa27e 1097 }
3fc48d09 1098 ret = 0;
faf575c1 1099
3fc48d09 1100fail:
4e95314e
KW
1101 qemu_co_mutex_unlock(&s->lock);
1102
88c6588c
KW
1103 while (l2meta != NULL) {
1104 QCowL2Meta *next;
1105
4e95314e
KW
1106 if (l2meta->nb_clusters != 0) {
1107 QLIST_REMOVE(l2meta, next_in_flight);
1108 }
1109 qemu_co_queue_restart_all(&l2meta->dependent_requests);
88c6588c
KW
1110
1111 next = l2meta->next;
cf5c1a23 1112 g_free(l2meta);
88c6588c 1113 l2meta = next;
cf5c1a23 1114 }
0fa9131a 1115
3fc48d09 1116 qemu_iovec_destroy(&hd_qiov);
dea43a65 1117 qemu_vfree(cluster_data);
3cce16f4 1118 trace_qcow2_writev_done_req(qemu_coroutine_self(), ret);
42496d62 1119
68d100e9 1120 return ret;
585f8587
FB
1121}
1122
7c80ab3f 1123static void qcow2_close(BlockDriverState *bs)
585f8587
FB
1124{
1125 BDRVQcowState *s = bs->opaque;
7267c094 1126 g_free(s->l1_table);
cf93980e
MR
1127 /* else pre-write overlap checks in cache_destroy may crash */
1128 s->l1_table = NULL;
29c1a730
KW
1129
1130 qcow2_cache_flush(bs, s->l2_table_cache);
1131 qcow2_cache_flush(bs, s->refcount_block_cache);
1132
c61d0004
SH
1133 qcow2_mark_clean(bs);
1134
29c1a730
KW
1135 qcow2_cache_destroy(bs, s->l2_table_cache);
1136 qcow2_cache_destroy(bs, s->refcount_block_cache);
1137
6744cbab 1138 g_free(s->unknown_header_fields);
75bab85c 1139 cleanup_unknown_header_ext(bs);
6744cbab 1140
7267c094 1141 g_free(s->cluster_cache);
dea43a65 1142 qemu_vfree(s->cluster_data);
ed6ccf0f 1143 qcow2_refcount_close(bs);
28c1202b 1144 qcow2_free_snapshots(bs);
585f8587
FB
1145}
1146
06d9260f
AL
1147static void qcow2_invalidate_cache(BlockDriverState *bs)
1148{
1149 BDRVQcowState *s = bs->opaque;
1150 int flags = s->flags;
1151 AES_KEY aes_encrypt_key;
1152 AES_KEY aes_decrypt_key;
1153 uint32_t crypt_method = 0;
acdfb480 1154 QDict *options;
06d9260f
AL
1155
1156 /*
1157 * Backing files are read-only which makes all of their metadata immutable,
1158 * that means we don't have to worry about reopening them here.
1159 */
1160
1161 if (s->crypt_method) {
1162 crypt_method = s->crypt_method;
1163 memcpy(&aes_encrypt_key, &s->aes_encrypt_key, sizeof(aes_encrypt_key));
1164 memcpy(&aes_decrypt_key, &s->aes_decrypt_key, sizeof(aes_decrypt_key));
1165 }
1166
1167 qcow2_close(bs);
1168
acdfb480
KW
1169 options = qdict_new();
1170 qdict_put(options, QCOW2_OPT_LAZY_REFCOUNTS,
1171 qbool_from_int(s->use_lazy_refcounts));
1172
06d9260f 1173 memset(s, 0, sizeof(BDRVQcowState));
015a1036 1174 qcow2_open(bs, options, flags, NULL);
acdfb480
KW
1175
1176 QDECREF(options);
06d9260f
AL
1177
1178 if (crypt_method) {
1179 s->crypt_method = crypt_method;
1180 memcpy(&s->aes_encrypt_key, &aes_encrypt_key, sizeof(aes_encrypt_key));
1181 memcpy(&s->aes_decrypt_key, &aes_decrypt_key, sizeof(aes_decrypt_key));
1182 }
1183}
1184
e24e49e6
KW
1185static size_t header_ext_add(char *buf, uint32_t magic, const void *s,
1186 size_t len, size_t buflen)
1187{
1188 QCowExtension *ext_backing_fmt = (QCowExtension*) buf;
1189 size_t ext_len = sizeof(QCowExtension) + ((len + 7) & ~7);
1190
1191 if (buflen < ext_len) {
1192 return -ENOSPC;
1193 }
1194
1195 *ext_backing_fmt = (QCowExtension) {
1196 .magic = cpu_to_be32(magic),
1197 .len = cpu_to_be32(len),
1198 };
1199 memcpy(buf + sizeof(QCowExtension), s, len);
1200
1201 return ext_len;
1202}
1203
756e6736 1204/*
e24e49e6
KW
1205 * Updates the qcow2 header, including the variable length parts of it, i.e.
1206 * the backing file name and all extensions. qcow2 was not designed to allow
1207 * such changes, so if we run out of space (we can only use the first cluster)
1208 * this function may fail.
756e6736
KW
1209 *
1210 * Returns 0 on success, -errno in error cases.
1211 */
e24e49e6 1212int qcow2_update_header(BlockDriverState *bs)
756e6736 1213{
756e6736 1214 BDRVQcowState *s = bs->opaque;
e24e49e6
KW
1215 QCowHeader *header;
1216 char *buf;
1217 size_t buflen = s->cluster_size;
756e6736 1218 int ret;
e24e49e6
KW
1219 uint64_t total_size;
1220 uint32_t refcount_table_clusters;
6744cbab 1221 size_t header_length;
75bab85c 1222 Qcow2UnknownHeaderExtension *uext;
756e6736 1223
e24e49e6 1224 buf = qemu_blockalign(bs, buflen);
756e6736 1225
e24e49e6
KW
1226 /* Header structure */
1227 header = (QCowHeader*) buf;
756e6736 1228
e24e49e6
KW
1229 if (buflen < sizeof(*header)) {
1230 ret = -ENOSPC;
1231 goto fail;
756e6736
KW
1232 }
1233
6744cbab 1234 header_length = sizeof(*header) + s->unknown_header_fields_size;
e24e49e6
KW
1235 total_size = bs->total_sectors * BDRV_SECTOR_SIZE;
1236 refcount_table_clusters = s->refcount_table_size >> (s->cluster_bits - 3);
1237
1238 *header = (QCowHeader) {
6744cbab 1239 /* Version 2 fields */
e24e49e6 1240 .magic = cpu_to_be32(QCOW_MAGIC),
6744cbab 1241 .version = cpu_to_be32(s->qcow_version),
e24e49e6
KW
1242 .backing_file_offset = 0,
1243 .backing_file_size = 0,
1244 .cluster_bits = cpu_to_be32(s->cluster_bits),
1245 .size = cpu_to_be64(total_size),
1246 .crypt_method = cpu_to_be32(s->crypt_method_header),
1247 .l1_size = cpu_to_be32(s->l1_size),
1248 .l1_table_offset = cpu_to_be64(s->l1_table_offset),
1249 .refcount_table_offset = cpu_to_be64(s->refcount_table_offset),
1250 .refcount_table_clusters = cpu_to_be32(refcount_table_clusters),
1251 .nb_snapshots = cpu_to_be32(s->nb_snapshots),
1252 .snapshots_offset = cpu_to_be64(s->snapshots_offset),
6744cbab
KW
1253
1254 /* Version 3 fields */
1255 .incompatible_features = cpu_to_be64(s->incompatible_features),
1256 .compatible_features = cpu_to_be64(s->compatible_features),
1257 .autoclear_features = cpu_to_be64(s->autoclear_features),
b6481f37 1258 .refcount_order = cpu_to_be32(s->refcount_order),
6744cbab 1259 .header_length = cpu_to_be32(header_length),
e24e49e6 1260 };
756e6736 1261
6744cbab
KW
1262 /* For older versions, write a shorter header */
1263 switch (s->qcow_version) {
1264 case 2:
1265 ret = offsetof(QCowHeader, incompatible_features);
1266 break;
1267 case 3:
1268 ret = sizeof(*header);
1269 break;
1270 default:
b6c14762
JM
1271 ret = -EINVAL;
1272 goto fail;
6744cbab
KW
1273 }
1274
1275 buf += ret;
1276 buflen -= ret;
1277 memset(buf, 0, buflen);
1278
1279 /* Preserve any unknown field in the header */
1280 if (s->unknown_header_fields_size) {
1281 if (buflen < s->unknown_header_fields_size) {
1282 ret = -ENOSPC;
1283 goto fail;
1284 }
1285
1286 memcpy(buf, s->unknown_header_fields, s->unknown_header_fields_size);
1287 buf += s->unknown_header_fields_size;
1288 buflen -= s->unknown_header_fields_size;
1289 }
756e6736 1290
e24e49e6
KW
1291 /* Backing file format header extension */
1292 if (*bs->backing_format) {
1293 ret = header_ext_add(buf, QCOW2_EXT_MAGIC_BACKING_FORMAT,
1294 bs->backing_format, strlen(bs->backing_format),
1295 buflen);
1296 if (ret < 0) {
1297 goto fail;
756e6736
KW
1298 }
1299
e24e49e6
KW
1300 buf += ret;
1301 buflen -= ret;
756e6736
KW
1302 }
1303
cfcc4c62
KW
1304 /* Feature table */
1305 Qcow2Feature features[] = {
c61d0004
SH
1306 {
1307 .type = QCOW2_FEAT_TYPE_INCOMPATIBLE,
1308 .bit = QCOW2_INCOMPAT_DIRTY_BITNR,
1309 .name = "dirty bit",
1310 },
69c98726
MR
1311 {
1312 .type = QCOW2_FEAT_TYPE_INCOMPATIBLE,
1313 .bit = QCOW2_INCOMPAT_CORRUPT_BITNR,
1314 .name = "corrupt bit",
1315 },
bfe8043e
SH
1316 {
1317 .type = QCOW2_FEAT_TYPE_COMPATIBLE,
1318 .bit = QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR,
1319 .name = "lazy refcounts",
1320 },
cfcc4c62
KW
1321 };
1322
1323 ret = header_ext_add(buf, QCOW2_EXT_MAGIC_FEATURE_TABLE,
1324 features, sizeof(features), buflen);
1325 if (ret < 0) {
1326 goto fail;
1327 }
1328 buf += ret;
1329 buflen -= ret;
1330
75bab85c
KW
1331 /* Keep unknown header extensions */
1332 QLIST_FOREACH(uext, &s->unknown_header_ext, next) {
1333 ret = header_ext_add(buf, uext->magic, uext->data, uext->len, buflen);
1334 if (ret < 0) {
1335 goto fail;
1336 }
1337
1338 buf += ret;
1339 buflen -= ret;
1340 }
1341
e24e49e6
KW
1342 /* End of header extensions */
1343 ret = header_ext_add(buf, QCOW2_EXT_MAGIC_END, NULL, 0, buflen);
756e6736
KW
1344 if (ret < 0) {
1345 goto fail;
1346 }
1347
e24e49e6
KW
1348 buf += ret;
1349 buflen -= ret;
756e6736 1350
e24e49e6
KW
1351 /* Backing file name */
1352 if (*bs->backing_file) {
1353 size_t backing_file_len = strlen(bs->backing_file);
1354
1355 if (buflen < backing_file_len) {
1356 ret = -ENOSPC;
1357 goto fail;
1358 }
1359
00ea1881 1360 /* Using strncpy is ok here, since buf is not NUL-terminated. */
e24e49e6
KW
1361 strncpy(buf, bs->backing_file, buflen);
1362
1363 header->backing_file_offset = cpu_to_be64(buf - ((char*) header));
1364 header->backing_file_size = cpu_to_be32(backing_file_len);
756e6736
KW
1365 }
1366
e24e49e6
KW
1367 /* Write the new header */
1368 ret = bdrv_pwrite(bs->file, 0, header, s->cluster_size);
756e6736
KW
1369 if (ret < 0) {
1370 goto fail;
1371 }
1372
1373 ret = 0;
1374fail:
e24e49e6 1375 qemu_vfree(header);
756e6736
KW
1376 return ret;
1377}
1378
1379static int qcow2_change_backing_file(BlockDriverState *bs,
1380 const char *backing_file, const char *backing_fmt)
1381{
e24e49e6
KW
1382 pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: "");
1383 pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: "");
1384
1385 return qcow2_update_header(bs);
756e6736
KW
1386}
1387
a35e1c17
KW
1388static int preallocate(BlockDriverState *bs)
1389{
a35e1c17
KW
1390 uint64_t nb_sectors;
1391 uint64_t offset;
060bee89 1392 uint64_t host_offset = 0;
a35e1c17 1393 int num;
148da7ea 1394 int ret;
f50f88b9 1395 QCowL2Meta *meta;
a35e1c17
KW
1396
1397 nb_sectors = bdrv_getlength(bs) >> 9;
1398 offset = 0;
1399
1400 while (nb_sectors) {
1401 num = MIN(nb_sectors, INT_MAX >> 9);
060bee89
KW
1402 ret = qcow2_alloc_cluster_offset(bs, offset, 0, num, &num,
1403 &host_offset, &meta);
148da7ea 1404 if (ret < 0) {
19dbcbf7 1405 return ret;
a35e1c17
KW
1406 }
1407
f50f88b9 1408 ret = qcow2_alloc_cluster_link_l2(bs, meta);
19dbcbf7 1409 if (ret < 0) {
6cfcb9b8
KW
1410 qcow2_free_any_clusters(bs, meta->alloc_offset, meta->nb_clusters,
1411 QCOW2_DISCARD_NEVER);
19dbcbf7 1412 return ret;
a35e1c17
KW
1413 }
1414
f214978a
KW
1415 /* There are no dependent requests, but we need to remove our request
1416 * from the list of in-flight requests */
f50f88b9 1417 if (meta != NULL) {
4e95314e 1418 QLIST_REMOVE(meta, next_in_flight);
f50f88b9 1419 }
f214978a 1420
a35e1c17
KW
1421 /* TODO Preallocate data if requested */
1422
1423 nb_sectors -= num;
1424 offset += num << 9;
1425 }
1426
1427 /*
1428 * It is expected that the image file is large enough to actually contain
1429 * all of the allocated clusters (otherwise we get failing reads after
1430 * EOF). Extend the image to the last allocated sector.
1431 */
060bee89 1432 if (host_offset != 0) {
ea80b906
KW
1433 uint8_t buf[512];
1434 memset(buf, 0, 512);
060bee89 1435 ret = bdrv_write(bs->file, (host_offset >> 9) + num - 1, buf, 1);
19dbcbf7
KW
1436 if (ret < 0) {
1437 return ret;
1438 }
a35e1c17
KW
1439 }
1440
1441 return 0;
1442}
1443
7c80ab3f
JS
1444static int qcow2_create2(const char *filename, int64_t total_size,
1445 const char *backing_file, const char *backing_format,
1446 int flags, size_t cluster_size, int prealloc,
3ef6c40a
MR
1447 QEMUOptionParameter *options, int version,
1448 Error **errp)
a9420734 1449{
9b2260cb 1450 /* Calculate cluster_bits */
a9420734
KW
1451 int cluster_bits;
1452 cluster_bits = ffs(cluster_size) - 1;
1453 if (cluster_bits < MIN_CLUSTER_BITS || cluster_bits > MAX_CLUSTER_BITS ||
1454 (1 << cluster_bits) != cluster_size)
1455 {
3ef6c40a
MR
1456 error_setg(errp, "Cluster size must be a power of two between %d and "
1457 "%dk", 1 << MIN_CLUSTER_BITS, 1 << (MAX_CLUSTER_BITS - 10));
a9420734
KW
1458 return -EINVAL;
1459 }
1460
1461 /*
1462 * Open the image file and write a minimal qcow2 header.
1463 *
1464 * We keep things simple and start with a zero-sized image. We also
1465 * do without refcount blocks or a L1 table for now. We'll fix the
1466 * inconsistency later.
1467 *
1468 * We do need a refcount table because growing the refcount table means
1469 * allocating two new refcount blocks - the seconds of which would be at
1470 * 2 GB for 64k clusters, and we don't want to have a 2 GB initial file
1471 * size for any qcow2 image.
1472 */
1473 BlockDriverState* bs;
1474 QCowHeader header;
1475 uint8_t* refcount_table;
3ef6c40a 1476 Error *local_err = NULL;
a9420734
KW
1477 int ret;
1478
3ef6c40a 1479 ret = bdrv_create_file(filename, options, &local_err);
a9420734 1480 if (ret < 0) {
3ef6c40a 1481 error_propagate(errp, local_err);
a9420734
KW
1482 return ret;
1483 }
1484
3ef6c40a 1485 ret = bdrv_file_open(&bs, filename, NULL, BDRV_O_RDWR, &local_err);
a9420734 1486 if (ret < 0) {
3ef6c40a 1487 error_propagate(errp, local_err);
a9420734
KW
1488 return ret;
1489 }
1490
1491 /* Write the header */
1492 memset(&header, 0, sizeof(header));
1493 header.magic = cpu_to_be32(QCOW_MAGIC);
6744cbab 1494 header.version = cpu_to_be32(version);
a9420734
KW
1495 header.cluster_bits = cpu_to_be32(cluster_bits);
1496 header.size = cpu_to_be64(0);
1497 header.l1_table_offset = cpu_to_be64(0);
1498 header.l1_size = cpu_to_be32(0);
1499 header.refcount_table_offset = cpu_to_be64(cluster_size);
1500 header.refcount_table_clusters = cpu_to_be32(1);
6744cbab
KW
1501 header.refcount_order = cpu_to_be32(3 + REFCOUNT_SHIFT);
1502 header.header_length = cpu_to_be32(sizeof(header));
a9420734
KW
1503
1504 if (flags & BLOCK_FLAG_ENCRYPT) {
1505 header.crypt_method = cpu_to_be32(QCOW_CRYPT_AES);
1506 } else {
1507 header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE);
1508 }
1509
bfe8043e
SH
1510 if (flags & BLOCK_FLAG_LAZY_REFCOUNTS) {
1511 header.compatible_features |=
1512 cpu_to_be64(QCOW2_COMPAT_LAZY_REFCOUNTS);
1513 }
1514
a9420734
KW
1515 ret = bdrv_pwrite(bs, 0, &header, sizeof(header));
1516 if (ret < 0) {
3ef6c40a 1517 error_setg_errno(errp, -ret, "Could not write qcow2 header");
a9420734
KW
1518 goto out;
1519 }
1520
1521 /* Write an empty refcount table */
7267c094 1522 refcount_table = g_malloc0(cluster_size);
a9420734 1523 ret = bdrv_pwrite(bs, cluster_size, refcount_table, cluster_size);
7267c094 1524 g_free(refcount_table);
a9420734
KW
1525
1526 if (ret < 0) {
3ef6c40a 1527 error_setg_errno(errp, -ret, "Could not write refcount table");
a9420734
KW
1528 goto out;
1529 }
1530
1531 bdrv_close(bs);
1532
1533 /*
1534 * And now open the image and make it consistent first (i.e. increase the
1535 * refcount of the cluster that is occupied by the header and the refcount
1536 * table)
1537 */
1538 BlockDriver* drv = bdrv_find_format("qcow2");
1539 assert(drv != NULL);
de9c0cec 1540 ret = bdrv_open(bs, filename, NULL,
3ef6c40a 1541 BDRV_O_RDWR | BDRV_O_CACHE_WB | BDRV_O_NO_FLUSH, drv, &local_err);
a9420734 1542 if (ret < 0) {
3ef6c40a 1543 error_propagate(errp, local_err);
a9420734
KW
1544 goto out;
1545 }
1546
1547 ret = qcow2_alloc_clusters(bs, 2 * cluster_size);
1548 if (ret < 0) {
3ef6c40a
MR
1549 error_setg_errno(errp, -ret, "Could not allocate clusters for qcow2 "
1550 "header and refcount table");
a9420734
KW
1551 goto out;
1552
1553 } else if (ret != 0) {
1554 error_report("Huh, first cluster in empty image is already in use?");
1555 abort();
1556 }
1557
1558 /* Okay, now that we have a valid image, let's give it the right size */
1559 ret = bdrv_truncate(bs, total_size * BDRV_SECTOR_SIZE);
1560 if (ret < 0) {
3ef6c40a 1561 error_setg_errno(errp, -ret, "Could not resize image");
a9420734
KW
1562 goto out;
1563 }
1564
1565 /* Want a backing file? There you go.*/
1566 if (backing_file) {
1567 ret = bdrv_change_backing_file(bs, backing_file, backing_format);
1568 if (ret < 0) {
3ef6c40a
MR
1569 error_setg_errno(errp, -ret, "Could not assign backing file '%s' "
1570 "with format '%s'", backing_file, backing_format);
a9420734
KW
1571 goto out;
1572 }
1573 }
1574
1575 /* And if we're supposed to preallocate metadata, do that now */
1576 if (prealloc) {
15552c4a
ZYW
1577 BDRVQcowState *s = bs->opaque;
1578 qemu_co_mutex_lock(&s->lock);
a9420734 1579 ret = preallocate(bs);
15552c4a 1580 qemu_co_mutex_unlock(&s->lock);
a9420734 1581 if (ret < 0) {
3ef6c40a 1582 error_setg_errno(errp, -ret, "Could not preallocate metadata");
a9420734
KW
1583 goto out;
1584 }
1585 }
1586
1587 ret = 0;
1588out:
4f6fd349 1589 bdrv_unref(bs);
a9420734
KW
1590 return ret;
1591}
de5f3f40 1592
d5124c00
MR
1593static int qcow2_create(const char *filename, QEMUOptionParameter *options,
1594 Error **errp)
de5f3f40
KW
1595{
1596 const char *backing_file = NULL;
1597 const char *backing_fmt = NULL;
1598 uint64_t sectors = 0;
1599 int flags = 0;
99cce9fa 1600 size_t cluster_size = DEFAULT_CLUSTER_SIZE;
de5f3f40 1601 int prealloc = 0;
8ad1898c 1602 int version = 3;
3ef6c40a
MR
1603 Error *local_err = NULL;
1604 int ret;
de5f3f40
KW
1605
1606 /* Read out options */
1607 while (options && options->name) {
1608 if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
1609 sectors = options->value.n / 512;
1610 } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) {
1611 backing_file = options->value.s;
1612 } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FMT)) {
1613 backing_fmt = options->value.s;
1614 } else if (!strcmp(options->name, BLOCK_OPT_ENCRYPT)) {
1615 flags |= options->value.n ? BLOCK_FLAG_ENCRYPT : 0;
1616 } else if (!strcmp(options->name, BLOCK_OPT_CLUSTER_SIZE)) {
1617 if (options->value.n) {
1618 cluster_size = options->value.n;
1619 }
1620 } else if (!strcmp(options->name, BLOCK_OPT_PREALLOC)) {
1621 if (!options->value.s || !strcmp(options->value.s, "off")) {
1622 prealloc = 0;
1623 } else if (!strcmp(options->value.s, "metadata")) {
1624 prealloc = 1;
1625 } else {
3ef6c40a
MR
1626 error_setg(errp, "Invalid preallocation mode: '%s'",
1627 options->value.s);
de5f3f40
KW
1628 return -EINVAL;
1629 }
6744cbab 1630 } else if (!strcmp(options->name, BLOCK_OPT_COMPAT_LEVEL)) {
9117b477
KW
1631 if (!options->value.s) {
1632 /* keep the default */
1633 } else if (!strcmp(options->value.s, "0.10")) {
6744cbab
KW
1634 version = 2;
1635 } else if (!strcmp(options->value.s, "1.1")) {
1636 version = 3;
1637 } else {
3ef6c40a
MR
1638 error_setg(errp, "Invalid compatibility level: '%s'",
1639 options->value.s);
6744cbab
KW
1640 return -EINVAL;
1641 }
bfe8043e
SH
1642 } else if (!strcmp(options->name, BLOCK_OPT_LAZY_REFCOUNTS)) {
1643 flags |= options->value.n ? BLOCK_FLAG_LAZY_REFCOUNTS : 0;
de5f3f40
KW
1644 }
1645 options++;
1646 }
1647
1648 if (backing_file && prealloc) {
3ef6c40a
MR
1649 error_setg(errp, "Backing file and preallocation cannot be used at "
1650 "the same time");
de5f3f40
KW
1651 return -EINVAL;
1652 }
1653
bfe8043e 1654 if (version < 3 && (flags & BLOCK_FLAG_LAZY_REFCOUNTS)) {
3ef6c40a
MR
1655 error_setg(errp, "Lazy refcounts only supported with compatibility "
1656 "level 1.1 and above (use compat=1.1 or greater)");
bfe8043e
SH
1657 return -EINVAL;
1658 }
1659
3ef6c40a
MR
1660 ret = qcow2_create2(filename, sectors, backing_file, backing_fmt, flags,
1661 cluster_size, prealloc, options, version, &local_err);
1662 if (error_is_set(&local_err)) {
1663 error_propagate(errp, local_err);
1664 }
1665 return ret;
de5f3f40
KW
1666}
1667
7c80ab3f 1668static int qcow2_make_empty(BlockDriverState *bs)
20d97356
BS
1669{
1670#if 0
1671 /* XXX: not correct */
1672 BDRVQcowState *s = bs->opaque;
1673 uint32_t l1_length = s->l1_size * sizeof(uint64_t);
1674 int ret;
1675
1676 memset(s->l1_table, 0, l1_length);
66f82cee 1677 if (bdrv_pwrite(bs->file, s->l1_table_offset, s->l1_table, l1_length) < 0)
20d97356 1678 return -1;
66f82cee 1679 ret = bdrv_truncate(bs->file, s->l1_table_offset + l1_length);
20d97356
BS
1680 if (ret < 0)
1681 return ret;
1682
1683 l2_cache_reset(bs);
1684#endif
1685 return 0;
1686}
1687
621f0589
KW
1688static coroutine_fn int qcow2_co_write_zeroes(BlockDriverState *bs,
1689 int64_t sector_num, int nb_sectors)
1690{
1691 int ret;
1692 BDRVQcowState *s = bs->opaque;
1693
1694 /* Emulate misaligned zero writes */
1695 if (sector_num % s->cluster_sectors || nb_sectors % s->cluster_sectors) {
1696 return -ENOTSUP;
1697 }
1698
1699 /* Whatever is left can use real zero clusters */
1700 qemu_co_mutex_lock(&s->lock);
1701 ret = qcow2_zero_clusters(bs, sector_num << BDRV_SECTOR_BITS,
1702 nb_sectors);
1703 qemu_co_mutex_unlock(&s->lock);
1704
1705 return ret;
1706}
1707
6db39ae2
PB
1708static coroutine_fn int qcow2_co_discard(BlockDriverState *bs,
1709 int64_t sector_num, int nb_sectors)
5ea929e3 1710{
6db39ae2
PB
1711 int ret;
1712 BDRVQcowState *s = bs->opaque;
1713
1714 qemu_co_mutex_lock(&s->lock);
1715 ret = qcow2_discard_clusters(bs, sector_num << BDRV_SECTOR_BITS,
670df5e3 1716 nb_sectors, QCOW2_DISCARD_REQUEST);
6db39ae2
PB
1717 qemu_co_mutex_unlock(&s->lock);
1718 return ret;
5ea929e3
KW
1719}
1720
419b19d9
SH
1721static int qcow2_truncate(BlockDriverState *bs, int64_t offset)
1722{
1723 BDRVQcowState *s = bs->opaque;
2cf7cfa1
KW
1724 int64_t new_l1_size;
1725 int ret;
419b19d9
SH
1726
1727 if (offset & 511) {
259b2173 1728 error_report("The new size must be a multiple of 512");
419b19d9
SH
1729 return -EINVAL;
1730 }
1731
1732 /* cannot proceed if image has snapshots */
1733 if (s->nb_snapshots) {
259b2173 1734 error_report("Can't resize an image which has snapshots");
419b19d9
SH
1735 return -ENOTSUP;
1736 }
1737
1738 /* shrinking is currently not supported */
1739 if (offset < bs->total_sectors * 512) {
259b2173 1740 error_report("qcow2 doesn't support shrinking images yet");
419b19d9
SH
1741 return -ENOTSUP;
1742 }
1743
1744 new_l1_size = size_to_l1(s, offset);
72893756 1745 ret = qcow2_grow_l1_table(bs, new_l1_size, true);
419b19d9
SH
1746 if (ret < 0) {
1747 return ret;
1748 }
1749
1750 /* write updated header.size */
1751 offset = cpu_to_be64(offset);
8b3b7206
KW
1752 ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, size),
1753 &offset, sizeof(uint64_t));
419b19d9
SH
1754 if (ret < 0) {
1755 return ret;
1756 }
1757
1758 s->l1_vm_state_index = new_l1_size;
1759 return 0;
1760}
1761
20d97356
BS
1762/* XXX: put compressed sectors first, then all the cluster aligned
1763 tables to avoid losing bytes in alignment */
7c80ab3f
JS
1764static int qcow2_write_compressed(BlockDriverState *bs, int64_t sector_num,
1765 const uint8_t *buf, int nb_sectors)
20d97356
BS
1766{
1767 BDRVQcowState *s = bs->opaque;
1768 z_stream strm;
1769 int ret, out_len;
1770 uint8_t *out_buf;
1771 uint64_t cluster_offset;
1772
1773 if (nb_sectors == 0) {
1774 /* align end of file to a sector boundary to ease reading with
1775 sector based I/Os */
66f82cee 1776 cluster_offset = bdrv_getlength(bs->file);
20d97356 1777 cluster_offset = (cluster_offset + 511) & ~511;
66f82cee 1778 bdrv_truncate(bs->file, cluster_offset);
20d97356
BS
1779 return 0;
1780 }
1781
f4d38bef
SH
1782 if (nb_sectors != s->cluster_sectors) {
1783 ret = -EINVAL;
1784
1785 /* Zero-pad last write if image size is not cluster aligned */
1786 if (sector_num + nb_sectors == bs->total_sectors &&
1787 nb_sectors < s->cluster_sectors) {
1788 uint8_t *pad_buf = qemu_blockalign(bs, s->cluster_size);
1789 memset(pad_buf, 0, s->cluster_size);
1790 memcpy(pad_buf, buf, nb_sectors * BDRV_SECTOR_SIZE);
1791 ret = qcow2_write_compressed(bs, sector_num,
1792 pad_buf, s->cluster_sectors);
1793 qemu_vfree(pad_buf);
1794 }
1795 return ret;
1796 }
20d97356 1797
7267c094 1798 out_buf = g_malloc(s->cluster_size + (s->cluster_size / 1000) + 128);
20d97356
BS
1799
1800 /* best compression, small window, no zlib header */
1801 memset(&strm, 0, sizeof(strm));
1802 ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION,
1803 Z_DEFLATED, -12,
1804 9, Z_DEFAULT_STRATEGY);
1805 if (ret != 0) {
8f1efd00
KW
1806 ret = -EINVAL;
1807 goto fail;
20d97356
BS
1808 }
1809
1810 strm.avail_in = s->cluster_size;
1811 strm.next_in = (uint8_t *)buf;
1812 strm.avail_out = s->cluster_size;
1813 strm.next_out = out_buf;
1814
1815 ret = deflate(&strm, Z_FINISH);
1816 if (ret != Z_STREAM_END && ret != Z_OK) {
20d97356 1817 deflateEnd(&strm);
8f1efd00
KW
1818 ret = -EINVAL;
1819 goto fail;
20d97356
BS
1820 }
1821 out_len = strm.next_out - out_buf;
1822
1823 deflateEnd(&strm);
1824
1825 if (ret != Z_STREAM_END || out_len >= s->cluster_size) {
1826 /* could not compress: write normal cluster */
8f1efd00
KW
1827 ret = bdrv_write(bs, sector_num, buf, s->cluster_sectors);
1828 if (ret < 0) {
1829 goto fail;
1830 }
20d97356
BS
1831 } else {
1832 cluster_offset = qcow2_alloc_compressed_cluster_offset(bs,
1833 sector_num << 9, out_len);
8f1efd00
KW
1834 if (!cluster_offset) {
1835 ret = -EIO;
1836 goto fail;
1837 }
20d97356 1838 cluster_offset &= s->cluster_offset_mask;
cf93980e 1839
231bb267 1840 ret = qcow2_pre_write_overlap_check(bs, 0, cluster_offset, out_len);
cf93980e
MR
1841 if (ret < 0) {
1842 goto fail;
1843 }
1844
66f82cee 1845 BLKDBG_EVENT(bs->file, BLKDBG_WRITE_COMPRESSED);
8f1efd00
KW
1846 ret = bdrv_pwrite(bs->file, cluster_offset, out_buf, out_len);
1847 if (ret < 0) {
1848 goto fail;
20d97356
BS
1849 }
1850 }
1851
8f1efd00
KW
1852 ret = 0;
1853fail:
7267c094 1854 g_free(out_buf);
8f1efd00 1855 return ret;
20d97356
BS
1856}
1857
a968168c 1858static coroutine_fn int qcow2_co_flush_to_os(BlockDriverState *bs)
20d97356 1859{
29c1a730
KW
1860 BDRVQcowState *s = bs->opaque;
1861 int ret;
1862
8b94ff85 1863 qemu_co_mutex_lock(&s->lock);
29c1a730
KW
1864 ret = qcow2_cache_flush(bs, s->l2_table_cache);
1865 if (ret < 0) {
c95de7e2 1866 qemu_co_mutex_unlock(&s->lock);
8b94ff85 1867 return ret;
29c1a730
KW
1868 }
1869
bfe8043e
SH
1870 if (qcow2_need_accurate_refcounts(s)) {
1871 ret = qcow2_cache_flush(bs, s->refcount_block_cache);
1872 if (ret < 0) {
1873 qemu_co_mutex_unlock(&s->lock);
1874 return ret;
1875 }
29c1a730 1876 }
8b94ff85 1877 qemu_co_mutex_unlock(&s->lock);
29c1a730 1878
eb489bb1
KW
1879 return 0;
1880}
1881
7c80ab3f 1882static int qcow2_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
20d97356
BS
1883{
1884 BDRVQcowState *s = bs->opaque;
1885 bdi->cluster_size = s->cluster_size;
7c80ab3f 1886 bdi->vm_state_offset = qcow2_vm_state_offset(s);
20d97356
BS
1887 return 0;
1888}
1889
37764dfb
MR
1890static ImageInfoSpecific *qcow2_get_specific_info(BlockDriverState *bs)
1891{
1892 BDRVQcowState *s = bs->opaque;
1893 ImageInfoSpecific *spec_info = g_new(ImageInfoSpecific, 1);
1894
1895 *spec_info = (ImageInfoSpecific){
1896 .kind = IMAGE_INFO_SPECIFIC_KIND_QCOW2,
1897 {
1898 .qcow2 = g_new(ImageInfoSpecificQCow2, 1),
1899 },
1900 };
1901 if (s->qcow_version == 2) {
1902 *spec_info->qcow2 = (ImageInfoSpecificQCow2){
1903 .compat = g_strdup("0.10"),
1904 };
1905 } else if (s->qcow_version == 3) {
1906 *spec_info->qcow2 = (ImageInfoSpecificQCow2){
1907 .compat = g_strdup("1.1"),
1908 .lazy_refcounts = s->compatible_features &
1909 QCOW2_COMPAT_LAZY_REFCOUNTS,
1910 .has_lazy_refcounts = true,
1911 };
1912 }
1913
1914 return spec_info;
1915}
1916
20d97356
BS
1917#if 0
1918static void dump_refcounts(BlockDriverState *bs)
1919{
1920 BDRVQcowState *s = bs->opaque;
1921 int64_t nb_clusters, k, k1, size;
1922 int refcount;
1923
66f82cee 1924 size = bdrv_getlength(bs->file);
20d97356
BS
1925 nb_clusters = size_to_clusters(s, size);
1926 for(k = 0; k < nb_clusters;) {
1927 k1 = k;
1928 refcount = get_refcount(bs, k);
1929 k++;
1930 while (k < nb_clusters && get_refcount(bs, k) == refcount)
1931 k++;
0bfcd599
BS
1932 printf("%" PRId64 ": refcount=%d nb=%" PRId64 "\n", k, refcount,
1933 k - k1);
20d97356
BS
1934 }
1935}
1936#endif
1937
cf8074b3
KW
1938static int qcow2_save_vmstate(BlockDriverState *bs, QEMUIOVector *qiov,
1939 int64_t pos)
20d97356
BS
1940{
1941 BDRVQcowState *s = bs->opaque;
1942 int growable = bs->growable;
1943 int ret;
1944
66f82cee 1945 BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_SAVE);
20d97356 1946 bs->growable = 1;
8d3b1a2d 1947 ret = bdrv_pwritev(bs, qcow2_vm_state_offset(s) + pos, qiov);
20d97356
BS
1948 bs->growable = growable;
1949
1950 return ret;
1951}
1952
7c80ab3f
JS
1953static int qcow2_load_vmstate(BlockDriverState *bs, uint8_t *buf,
1954 int64_t pos, int size)
20d97356
BS
1955{
1956 BDRVQcowState *s = bs->opaque;
1957 int growable = bs->growable;
0d51b4de 1958 bool zero_beyond_eof = bs->zero_beyond_eof;
20d97356
BS
1959 int ret;
1960
66f82cee 1961 BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_LOAD);
20d97356 1962 bs->growable = 1;
0d51b4de 1963 bs->zero_beyond_eof = false;
7c80ab3f 1964 ret = bdrv_pread(bs, qcow2_vm_state_offset(s) + pos, buf, size);
20d97356 1965 bs->growable = growable;
0d51b4de 1966 bs->zero_beyond_eof = zero_beyond_eof;
20d97356
BS
1967
1968 return ret;
1969}
1970
9296b3ed
MR
1971/*
1972 * Downgrades an image's version. To achieve this, any incompatible features
1973 * have to be removed.
1974 */
1975static int qcow2_downgrade(BlockDriverState *bs, int target_version)
1976{
1977 BDRVQcowState *s = bs->opaque;
1978 int current_version = s->qcow_version;
1979 int ret;
1980
1981 if (target_version == current_version) {
1982 return 0;
1983 } else if (target_version > current_version) {
1984 return -EINVAL;
1985 } else if (target_version != 2) {
1986 return -EINVAL;
1987 }
1988
1989 if (s->refcount_order != 4) {
1990 /* we would have to convert the image to a refcount_order == 4 image
1991 * here; however, since qemu (at the time of writing this) does not
1992 * support anything different than 4 anyway, there is no point in doing
1993 * so right now; however, we should error out (if qemu supports this in
1994 * the future and this code has not been adapted) */
9e3f0892 1995 error_report("qcow2_downgrade: Image refcount orders other than 4 are "
9296b3ed
MR
1996 "currently not supported.");
1997 return -ENOTSUP;
1998 }
1999
2000 /* clear incompatible features */
2001 if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) {
2002 ret = qcow2_mark_clean(bs);
2003 if (ret < 0) {
2004 return ret;
2005 }
2006 }
2007
2008 /* with QCOW2_INCOMPAT_CORRUPT, it is pretty much impossible to get here in
2009 * the first place; if that happens nonetheless, returning -ENOTSUP is the
2010 * best thing to do anyway */
2011
2012 if (s->incompatible_features) {
2013 return -ENOTSUP;
2014 }
2015
2016 /* since we can ignore compatible features, we can set them to 0 as well */
2017 s->compatible_features = 0;
2018 /* if lazy refcounts have been used, they have already been fixed through
2019 * clearing the dirty flag */
2020
2021 /* clearing autoclear features is trivial */
2022 s->autoclear_features = 0;
2023
2024 ret = qcow2_expand_zero_clusters(bs);
2025 if (ret < 0) {
2026 return ret;
2027 }
2028
2029 s->qcow_version = target_version;
2030 ret = qcow2_update_header(bs);
2031 if (ret < 0) {
2032 s->qcow_version = current_version;
2033 return ret;
2034 }
2035 return 0;
2036}
2037
2038static int qcow2_amend_options(BlockDriverState *bs,
2039 QEMUOptionParameter *options)
2040{
2041 BDRVQcowState *s = bs->opaque;
2042 int old_version = s->qcow_version, new_version = old_version;
2043 uint64_t new_size = 0;
2044 const char *backing_file = NULL, *backing_format = NULL;
2045 bool lazy_refcounts = s->use_lazy_refcounts;
2046 int ret;
2047 int i;
2048
2049 for (i = 0; options[i].name; i++)
2050 {
2051 if (!options[i].assigned) {
2052 /* only change explicitly defined options */
2053 continue;
2054 }
2055
2056 if (!strcmp(options[i].name, "compat")) {
2057 if (!options[i].value.s) {
2058 /* preserve default */
2059 } else if (!strcmp(options[i].value.s, "0.10")) {
2060 new_version = 2;
2061 } else if (!strcmp(options[i].value.s, "1.1")) {
2062 new_version = 3;
2063 } else {
2064 fprintf(stderr, "Unknown compatibility level %s.\n",
2065 options[i].value.s);
2066 return -EINVAL;
2067 }
2068 } else if (!strcmp(options[i].name, "preallocation")) {
2069 fprintf(stderr, "Cannot change preallocation mode.\n");
2070 return -ENOTSUP;
2071 } else if (!strcmp(options[i].name, "size")) {
2072 new_size = options[i].value.n;
2073 } else if (!strcmp(options[i].name, "backing_file")) {
2074 backing_file = options[i].value.s;
2075 } else if (!strcmp(options[i].name, "backing_fmt")) {
2076 backing_format = options[i].value.s;
2077 } else if (!strcmp(options[i].name, "encryption")) {
2078 if ((options[i].value.n != !!s->crypt_method)) {
2079 fprintf(stderr, "Changing the encryption flag is not "
2080 "supported.\n");
2081 return -ENOTSUP;
2082 }
2083 } else if (!strcmp(options[i].name, "cluster_size")) {
2084 if (options[i].value.n != s->cluster_size) {
2085 fprintf(stderr, "Changing the cluster size is not "
2086 "supported.\n");
2087 return -ENOTSUP;
2088 }
2089 } else if (!strcmp(options[i].name, "lazy_refcounts")) {
2090 lazy_refcounts = options[i].value.n;
2091 } else {
2092 /* if this assertion fails, this probably means a new option was
2093 * added without having it covered here */
2094 assert(false);
2095 }
2096 }
2097
2098 if (new_version != old_version) {
2099 if (new_version > old_version) {
2100 /* Upgrade */
2101 s->qcow_version = new_version;
2102 ret = qcow2_update_header(bs);
2103 if (ret < 0) {
2104 s->qcow_version = old_version;
2105 return ret;
2106 }
2107 } else {
2108 ret = qcow2_downgrade(bs, new_version);
2109 if (ret < 0) {
2110 return ret;
2111 }
2112 }
2113 }
2114
2115 if (backing_file || backing_format) {
2116 ret = qcow2_change_backing_file(bs, backing_file ?: bs->backing_file,
2117 backing_format ?: bs->backing_format);
2118 if (ret < 0) {
2119 return ret;
2120 }
2121 }
2122
2123 if (s->use_lazy_refcounts != lazy_refcounts) {
2124 if (lazy_refcounts) {
2125 if (s->qcow_version < 3) {
2126 fprintf(stderr, "Lazy refcounts only supported with compatibility "
2127 "level 1.1 and above (use compat=1.1 or greater)\n");
2128 return -EINVAL;
2129 }
2130 s->compatible_features |= QCOW2_COMPAT_LAZY_REFCOUNTS;
2131 ret = qcow2_update_header(bs);
2132 if (ret < 0) {
2133 s->compatible_features &= ~QCOW2_COMPAT_LAZY_REFCOUNTS;
2134 return ret;
2135 }
2136 s->use_lazy_refcounts = true;
2137 } else {
2138 /* make image clean first */
2139 ret = qcow2_mark_clean(bs);
2140 if (ret < 0) {
2141 return ret;
2142 }
2143 /* now disallow lazy refcounts */
2144 s->compatible_features &= ~QCOW2_COMPAT_LAZY_REFCOUNTS;
2145 ret = qcow2_update_header(bs);
2146 if (ret < 0) {
2147 s->compatible_features |= QCOW2_COMPAT_LAZY_REFCOUNTS;
2148 return ret;
2149 }
2150 s->use_lazy_refcounts = false;
2151 }
2152 }
2153
2154 if (new_size) {
2155 ret = bdrv_truncate(bs, new_size);
2156 if (ret < 0) {
2157 return ret;
2158 }
2159 }
2160
2161 return 0;
2162}
2163
7c80ab3f 2164static QEMUOptionParameter qcow2_create_options[] = {
20d97356
BS
2165 {
2166 .name = BLOCK_OPT_SIZE,
2167 .type = OPT_SIZE,
2168 .help = "Virtual disk size"
2169 },
6744cbab
KW
2170 {
2171 .name = BLOCK_OPT_COMPAT_LEVEL,
2172 .type = OPT_STRING,
2173 .help = "Compatibility level (0.10 or 1.1)"
2174 },
20d97356
BS
2175 {
2176 .name = BLOCK_OPT_BACKING_FILE,
2177 .type = OPT_STRING,
2178 .help = "File name of a base image"
2179 },
2180 {
2181 .name = BLOCK_OPT_BACKING_FMT,
2182 .type = OPT_STRING,
2183 .help = "Image format of the base image"
2184 },
2185 {
2186 .name = BLOCK_OPT_ENCRYPT,
2187 .type = OPT_FLAG,
2188 .help = "Encrypt the image"
2189 },
2190 {
2191 .name = BLOCK_OPT_CLUSTER_SIZE,
2192 .type = OPT_SIZE,
99cce9fa
KW
2193 .help = "qcow2 cluster size",
2194 .value = { .n = DEFAULT_CLUSTER_SIZE },
20d97356
BS
2195 },
2196 {
2197 .name = BLOCK_OPT_PREALLOC,
2198 .type = OPT_STRING,
2199 .help = "Preallocation mode (allowed values: off, metadata)"
2200 },
bfe8043e
SH
2201 {
2202 .name = BLOCK_OPT_LAZY_REFCOUNTS,
2203 .type = OPT_FLAG,
2204 .help = "Postpone refcount updates",
2205 },
20d97356
BS
2206 { NULL }
2207};
2208
2209static BlockDriver bdrv_qcow2 = {
7c80ab3f
JS
2210 .format_name = "qcow2",
2211 .instance_size = sizeof(BDRVQcowState),
2212 .bdrv_probe = qcow2_probe,
2213 .bdrv_open = qcow2_open,
2214 .bdrv_close = qcow2_close,
21d82ac9 2215 .bdrv_reopen_prepare = qcow2_reopen_prepare,
7c80ab3f 2216 .bdrv_create = qcow2_create,
3ac21627 2217 .bdrv_has_zero_init = bdrv_has_zero_init_1,
b6b8a333 2218 .bdrv_co_get_block_status = qcow2_co_get_block_status,
7c80ab3f
JS
2219 .bdrv_set_key = qcow2_set_key,
2220 .bdrv_make_empty = qcow2_make_empty,
2221
c68b89ac
KW
2222 .bdrv_co_readv = qcow2_co_readv,
2223 .bdrv_co_writev = qcow2_co_writev,
eb489bb1 2224 .bdrv_co_flush_to_os = qcow2_co_flush_to_os,
419b19d9 2225
621f0589 2226 .bdrv_co_write_zeroes = qcow2_co_write_zeroes,
6db39ae2 2227 .bdrv_co_discard = qcow2_co_discard,
419b19d9 2228 .bdrv_truncate = qcow2_truncate,
7c80ab3f 2229 .bdrv_write_compressed = qcow2_write_compressed,
20d97356
BS
2230
2231 .bdrv_snapshot_create = qcow2_snapshot_create,
2232 .bdrv_snapshot_goto = qcow2_snapshot_goto,
2233 .bdrv_snapshot_delete = qcow2_snapshot_delete,
2234 .bdrv_snapshot_list = qcow2_snapshot_list,
51ef6727 2235 .bdrv_snapshot_load_tmp = qcow2_snapshot_load_tmp,
7c80ab3f 2236 .bdrv_get_info = qcow2_get_info,
37764dfb 2237 .bdrv_get_specific_info = qcow2_get_specific_info,
20d97356 2238
7c80ab3f
JS
2239 .bdrv_save_vmstate = qcow2_save_vmstate,
2240 .bdrv_load_vmstate = qcow2_load_vmstate,
20d97356
BS
2241
2242 .bdrv_change_backing_file = qcow2_change_backing_file,
2243
06d9260f
AL
2244 .bdrv_invalidate_cache = qcow2_invalidate_cache,
2245
7c80ab3f
JS
2246 .create_options = qcow2_create_options,
2247 .bdrv_check = qcow2_check,
9296b3ed 2248 .bdrv_amend_options = qcow2_amend_options,
20d97356
BS
2249};
2250
5efa9d5a
AL
2251static void bdrv_qcow2_init(void)
2252{
2253 bdrv_register(&bdrv_qcow2);
2254}
2255
2256block_init(bdrv_qcow2_init);