2 * Block driver for the QCOW version 2 format
4 * Copyright (c) 2004-2006 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
24 #include "qemu-common.h"
25 #include "block/block_int.h"
26 #include "qemu/module.h"
28 #include "block/qcow2.h"
29 #include "qemu/error-report.h"
30 #include "qapi/qmp/qerror.h"
31 #include "qapi/qmp/qbool.h"
32 #include "qapi/util.h"
33 #include "qapi/qmp/types.h"
34 #include "qapi-event.h"
36 #include "qemu/option_int.h"
39 Differences with QCOW:
41 - Support for multiple incremental snapshots.
42 - Memory management by reference counts.
43 - Clusters which have a reference count of one have the bit
44 QCOW_OFLAG_COPIED to optimize write performance.
45 - Size of compressed clusters is stored in sectors to reduce bit usage
46 in the cluster offsets.
47 - Support for storing additional data (such as the VM state) in the
49 - If a backing store is used, the cluster size is not constrained
50 (could be backported to QCOW).
51 - L2 tables have always a size of one cluster.
58 } QEMU_PACKED QCowExtension;
60 #define QCOW2_EXT_MAGIC_END 0
61 #define QCOW2_EXT_MAGIC_BACKING_FORMAT 0xE2792ACA
62 #define QCOW2_EXT_MAGIC_FEATURE_TABLE 0x6803f857
64 static int qcow2_probe(const uint8_t *buf, int buf_size, const char *filename)
66 const QCowHeader *cow_header = (const void *)buf;
68 if (buf_size >= sizeof(QCowHeader) &&
69 be32_to_cpu(cow_header->magic) == QCOW_MAGIC &&
70 be32_to_cpu(cow_header->version) >= 2)
78 * read qcow2 extension and fill bs
79 * start reading from start_offset
80 * finish reading upon magic of value 0 or when end_offset reached
81 * unknown magic is skipped (future extension this version knows nothing about)
82 * return 0 upon success, non-0 otherwise
84 static int qcow2_read_extensions(BlockDriverState *bs, uint64_t start_offset,
85 uint64_t end_offset, void **p_feature_table,
88 BDRVQcow2State *s = bs->opaque;
94 printf("qcow2_read_extensions: start=%ld end=%ld\n", start_offset, end_offset);
96 offset = start_offset;
97 while (offset < end_offset) {
101 if (offset > s->cluster_size)
102 printf("qcow2_read_extension: suspicious offset %lu\n", offset);
104 printf("attempting to read extended header in offset %lu\n", offset);
107 ret = bdrv_pread(bs->file, offset, &ext, sizeof(ext));
109 error_setg_errno(errp, -ret, "qcow2_read_extension: ERROR: "
110 "pread fail from offset %" PRIu64, offset);
113 be32_to_cpus(&ext.magic);
114 be32_to_cpus(&ext.len);
115 offset += sizeof(ext);
117 printf("ext.magic = 0x%x\n", ext.magic);
119 if (offset > end_offset || ext.len > end_offset - offset) {
120 error_setg(errp, "Header extension too large");
125 case QCOW2_EXT_MAGIC_END:
128 case QCOW2_EXT_MAGIC_BACKING_FORMAT:
129 if (ext.len >= sizeof(bs->backing_format)) {
130 error_setg(errp, "ERROR: ext_backing_format: len=%" PRIu32
131 " too large (>=%zu)", ext.len,
132 sizeof(bs->backing_format));
135 ret = bdrv_pread(bs->file, offset, bs->backing_format, ext.len);
137 error_setg_errno(errp, -ret, "ERROR: ext_backing_format: "
138 "Could not read format name");
141 bs->backing_format[ext.len] = '\0';
142 s->image_backing_format = g_strdup(bs->backing_format);
144 printf("Qcow2: Got format extension %s\n", bs->backing_format);
148 case QCOW2_EXT_MAGIC_FEATURE_TABLE:
149 if (p_feature_table != NULL) {
150 void* feature_table = g_malloc0(ext.len + 2 * sizeof(Qcow2Feature));
151 ret = bdrv_pread(bs->file, offset , feature_table, ext.len);
153 error_setg_errno(errp, -ret, "ERROR: ext_feature_table: "
154 "Could not read table");
158 *p_feature_table = feature_table;
163 /* unknown magic - save it in case we need to rewrite the header */
165 Qcow2UnknownHeaderExtension *uext;
167 uext = g_malloc0(sizeof(*uext) + ext.len);
168 uext->magic = ext.magic;
170 QLIST_INSERT_HEAD(&s->unknown_header_ext, uext, next);
172 ret = bdrv_pread(bs->file, offset , uext->data, uext->len);
174 error_setg_errno(errp, -ret, "ERROR: unknown extension: "
175 "Could not read data");
182 offset += ((ext.len + 7) & ~7);
188 static void cleanup_unknown_header_ext(BlockDriverState *bs)
190 BDRVQcow2State *s = bs->opaque;
191 Qcow2UnknownHeaderExtension *uext, *next;
193 QLIST_FOREACH_SAFE(uext, &s->unknown_header_ext, next, next) {
194 QLIST_REMOVE(uext, next);
199 static void GCC_FMT_ATTR(3, 4) report_unsupported(BlockDriverState *bs,
200 Error **errp, const char *fmt, ...)
206 vsnprintf(msg, sizeof(msg), fmt, ap);
209 error_setg(errp, QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,
210 bdrv_get_device_or_node_name(bs), "qcow2", msg);
213 static void report_unsupported_feature(BlockDriverState *bs,
214 Error **errp, Qcow2Feature *table, uint64_t mask)
216 char *features = g_strdup("");
219 while (table && table->name[0] != '\0') {
220 if (table->type == QCOW2_FEAT_TYPE_INCOMPATIBLE) {
221 if (mask & (1ULL << table->bit)) {
223 features = g_strdup_printf("%s%s%.46s", old, *old ? ", " : "",
226 mask &= ~(1ULL << table->bit);
234 features = g_strdup_printf("%s%sUnknown incompatible feature: %" PRIx64,
235 old, *old ? ", " : "", mask);
239 report_unsupported(bs, errp, "%s", features);
244 * Sets the dirty bit and flushes afterwards if necessary.
246 * The incompatible_features bit is only set if the image file header was
247 * updated successfully. Therefore it is not required to check the return
248 * value of this function.
250 int qcow2_mark_dirty(BlockDriverState *bs)
252 BDRVQcow2State *s = bs->opaque;
256 assert(s->qcow_version >= 3);
258 if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) {
259 return 0; /* already dirty */
262 val = cpu_to_be64(s->incompatible_features | QCOW2_INCOMPAT_DIRTY);
263 ret = bdrv_pwrite(bs->file, offsetof(QCowHeader, incompatible_features),
268 ret = bdrv_flush(bs->file);
273 /* Only treat image as dirty if the header was updated successfully */
274 s->incompatible_features |= QCOW2_INCOMPAT_DIRTY;
279 * Clears the dirty bit and flushes before if necessary. Only call this
280 * function when there are no pending requests, it does not guard against
281 * concurrent requests dirtying the image.
283 static int qcow2_mark_clean(BlockDriverState *bs)
285 BDRVQcow2State *s = bs->opaque;
287 if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) {
290 s->incompatible_features &= ~QCOW2_INCOMPAT_DIRTY;
292 ret = bdrv_flush(bs);
297 return qcow2_update_header(bs);
303 * Marks the image as corrupt.
305 int qcow2_mark_corrupt(BlockDriverState *bs)
307 BDRVQcow2State *s = bs->opaque;
309 s->incompatible_features |= QCOW2_INCOMPAT_CORRUPT;
310 return qcow2_update_header(bs);
314 * Marks the image as consistent, i.e., unsets the corrupt bit, and flushes
315 * before if necessary.
317 int qcow2_mark_consistent(BlockDriverState *bs)
319 BDRVQcow2State *s = bs->opaque;
321 if (s->incompatible_features & QCOW2_INCOMPAT_CORRUPT) {
322 int ret = bdrv_flush(bs);
327 s->incompatible_features &= ~QCOW2_INCOMPAT_CORRUPT;
328 return qcow2_update_header(bs);
333 static int qcow2_check(BlockDriverState *bs, BdrvCheckResult *result,
336 int ret = qcow2_check_refcounts(bs, result, fix);
341 if (fix && result->check_errors == 0 && result->corruptions == 0) {
342 ret = qcow2_mark_clean(bs);
346 return qcow2_mark_consistent(bs);
351 static int validate_table_offset(BlockDriverState *bs, uint64_t offset,
352 uint64_t entries, size_t entry_len)
354 BDRVQcow2State *s = bs->opaque;
357 /* Use signed INT64_MAX as the maximum even for uint64_t header fields,
358 * because values will be passed to qemu functions taking int64_t. */
359 if (entries > INT64_MAX / entry_len) {
363 size = entries * entry_len;
365 if (INT64_MAX - size < offset) {
369 /* Tables must be cluster aligned */
370 if (offset & (s->cluster_size - 1)) {
377 static QemuOptsList qcow2_runtime_opts = {
379 .head = QTAILQ_HEAD_INITIALIZER(qcow2_runtime_opts.head),
382 .name = QCOW2_OPT_LAZY_REFCOUNTS,
383 .type = QEMU_OPT_BOOL,
384 .help = "Postpone refcount updates",
387 .name = QCOW2_OPT_DISCARD_REQUEST,
388 .type = QEMU_OPT_BOOL,
389 .help = "Pass guest discard requests to the layer below",
392 .name = QCOW2_OPT_DISCARD_SNAPSHOT,
393 .type = QEMU_OPT_BOOL,
394 .help = "Generate discard requests when snapshot related space "
398 .name = QCOW2_OPT_DISCARD_OTHER,
399 .type = QEMU_OPT_BOOL,
400 .help = "Generate discard requests when other clusters are freed",
403 .name = QCOW2_OPT_OVERLAP,
404 .type = QEMU_OPT_STRING,
405 .help = "Selects which overlap checks to perform from a range of "
406 "templates (none, constant, cached, all)",
409 .name = QCOW2_OPT_OVERLAP_TEMPLATE,
410 .type = QEMU_OPT_STRING,
411 .help = "Selects which overlap checks to perform from a range of "
412 "templates (none, constant, cached, all)",
415 .name = QCOW2_OPT_OVERLAP_MAIN_HEADER,
416 .type = QEMU_OPT_BOOL,
417 .help = "Check for unintended writes into the main qcow2 header",
420 .name = QCOW2_OPT_OVERLAP_ACTIVE_L1,
421 .type = QEMU_OPT_BOOL,
422 .help = "Check for unintended writes into the active L1 table",
425 .name = QCOW2_OPT_OVERLAP_ACTIVE_L2,
426 .type = QEMU_OPT_BOOL,
427 .help = "Check for unintended writes into an active L2 table",
430 .name = QCOW2_OPT_OVERLAP_REFCOUNT_TABLE,
431 .type = QEMU_OPT_BOOL,
432 .help = "Check for unintended writes into the refcount table",
435 .name = QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK,
436 .type = QEMU_OPT_BOOL,
437 .help = "Check for unintended writes into a refcount block",
440 .name = QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE,
441 .type = QEMU_OPT_BOOL,
442 .help = "Check for unintended writes into the snapshot table",
445 .name = QCOW2_OPT_OVERLAP_INACTIVE_L1,
446 .type = QEMU_OPT_BOOL,
447 .help = "Check for unintended writes into an inactive L1 table",
450 .name = QCOW2_OPT_OVERLAP_INACTIVE_L2,
451 .type = QEMU_OPT_BOOL,
452 .help = "Check for unintended writes into an inactive L2 table",
455 .name = QCOW2_OPT_CACHE_SIZE,
456 .type = QEMU_OPT_SIZE,
457 .help = "Maximum combined metadata (L2 tables and refcount blocks) "
461 .name = QCOW2_OPT_L2_CACHE_SIZE,
462 .type = QEMU_OPT_SIZE,
463 .help = "Maximum L2 table cache size",
466 .name = QCOW2_OPT_REFCOUNT_CACHE_SIZE,
467 .type = QEMU_OPT_SIZE,
468 .help = "Maximum refcount block cache size",
471 .name = QCOW2_OPT_CACHE_CLEAN_INTERVAL,
472 .type = QEMU_OPT_NUMBER,
473 .help = "Clean unused cache entries after this time (in seconds)",
475 { /* end of list */ }
479 static const char *overlap_bool_option_names[QCOW2_OL_MAX_BITNR] = {
480 [QCOW2_OL_MAIN_HEADER_BITNR] = QCOW2_OPT_OVERLAP_MAIN_HEADER,
481 [QCOW2_OL_ACTIVE_L1_BITNR] = QCOW2_OPT_OVERLAP_ACTIVE_L1,
482 [QCOW2_OL_ACTIVE_L2_BITNR] = QCOW2_OPT_OVERLAP_ACTIVE_L2,
483 [QCOW2_OL_REFCOUNT_TABLE_BITNR] = QCOW2_OPT_OVERLAP_REFCOUNT_TABLE,
484 [QCOW2_OL_REFCOUNT_BLOCK_BITNR] = QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK,
485 [QCOW2_OL_SNAPSHOT_TABLE_BITNR] = QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE,
486 [QCOW2_OL_INACTIVE_L1_BITNR] = QCOW2_OPT_OVERLAP_INACTIVE_L1,
487 [QCOW2_OL_INACTIVE_L2_BITNR] = QCOW2_OPT_OVERLAP_INACTIVE_L2,
490 static void cache_clean_timer_cb(void *opaque)
492 BlockDriverState *bs = opaque;
493 BDRVQcow2State *s = bs->opaque;
494 qcow2_cache_clean_unused(bs, s->l2_table_cache);
495 qcow2_cache_clean_unused(bs, s->refcount_block_cache);
496 timer_mod(s->cache_clean_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
497 (int64_t) s->cache_clean_interval * 1000);
500 static void cache_clean_timer_init(BlockDriverState *bs, AioContext *context)
502 BDRVQcow2State *s = bs->opaque;
503 if (s->cache_clean_interval > 0) {
504 s->cache_clean_timer = aio_timer_new(context, QEMU_CLOCK_VIRTUAL,
505 SCALE_MS, cache_clean_timer_cb,
507 timer_mod(s->cache_clean_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
508 (int64_t) s->cache_clean_interval * 1000);
512 static void cache_clean_timer_del(BlockDriverState *bs)
514 BDRVQcow2State *s = bs->opaque;
515 if (s->cache_clean_timer) {
516 timer_del(s->cache_clean_timer);
517 timer_free(s->cache_clean_timer);
518 s->cache_clean_timer = NULL;
522 static void qcow2_detach_aio_context(BlockDriverState *bs)
524 cache_clean_timer_del(bs);
527 static void qcow2_attach_aio_context(BlockDriverState *bs,
528 AioContext *new_context)
530 cache_clean_timer_init(bs, new_context);
533 static void read_cache_sizes(BlockDriverState *bs, QemuOpts *opts,
534 uint64_t *l2_cache_size,
535 uint64_t *refcount_cache_size, Error **errp)
537 BDRVQcow2State *s = bs->opaque;
538 uint64_t combined_cache_size;
539 bool l2_cache_size_set, refcount_cache_size_set, combined_cache_size_set;
541 combined_cache_size_set = qemu_opt_get(opts, QCOW2_OPT_CACHE_SIZE);
542 l2_cache_size_set = qemu_opt_get(opts, QCOW2_OPT_L2_CACHE_SIZE);
543 refcount_cache_size_set = qemu_opt_get(opts, QCOW2_OPT_REFCOUNT_CACHE_SIZE);
545 combined_cache_size = qemu_opt_get_size(opts, QCOW2_OPT_CACHE_SIZE, 0);
546 *l2_cache_size = qemu_opt_get_size(opts, QCOW2_OPT_L2_CACHE_SIZE, 0);
547 *refcount_cache_size = qemu_opt_get_size(opts,
548 QCOW2_OPT_REFCOUNT_CACHE_SIZE, 0);
550 if (combined_cache_size_set) {
551 if (l2_cache_size_set && refcount_cache_size_set) {
552 error_setg(errp, QCOW2_OPT_CACHE_SIZE ", " QCOW2_OPT_L2_CACHE_SIZE
553 " and " QCOW2_OPT_REFCOUNT_CACHE_SIZE " may not be set "
556 } else if (*l2_cache_size > combined_cache_size) {
557 error_setg(errp, QCOW2_OPT_L2_CACHE_SIZE " may not exceed "
558 QCOW2_OPT_CACHE_SIZE);
560 } else if (*refcount_cache_size > combined_cache_size) {
561 error_setg(errp, QCOW2_OPT_REFCOUNT_CACHE_SIZE " may not exceed "
562 QCOW2_OPT_CACHE_SIZE);
566 if (l2_cache_size_set) {
567 *refcount_cache_size = combined_cache_size - *l2_cache_size;
568 } else if (refcount_cache_size_set) {
569 *l2_cache_size = combined_cache_size - *refcount_cache_size;
571 *refcount_cache_size = combined_cache_size
572 / (DEFAULT_L2_REFCOUNT_SIZE_RATIO + 1);
573 *l2_cache_size = combined_cache_size - *refcount_cache_size;
576 if (!l2_cache_size_set && !refcount_cache_size_set) {
577 *l2_cache_size = MAX(DEFAULT_L2_CACHE_BYTE_SIZE,
578 (uint64_t)DEFAULT_L2_CACHE_CLUSTERS
580 *refcount_cache_size = *l2_cache_size
581 / DEFAULT_L2_REFCOUNT_SIZE_RATIO;
582 } else if (!l2_cache_size_set) {
583 *l2_cache_size = *refcount_cache_size
584 * DEFAULT_L2_REFCOUNT_SIZE_RATIO;
585 } else if (!refcount_cache_size_set) {
586 *refcount_cache_size = *l2_cache_size
587 / DEFAULT_L2_REFCOUNT_SIZE_RATIO;
592 static int qcow2_update_options(BlockDriverState *bs, QDict *options,
593 int flags, Error **errp)
595 BDRVQcow2State *s = bs->opaque;
596 QemuOpts *opts = NULL;
597 const char *opt_overlap_check, *opt_overlap_check_template;
598 int overlap_check_template = 0;
599 uint64_t l2_cache_size, refcount_cache_size;
600 uint64_t cache_clean_interval;
602 Error *local_err = NULL;
605 opts = qemu_opts_create(&qcow2_runtime_opts, NULL, 0, &error_abort);
606 qemu_opts_absorb_qdict(opts, options, &local_err);
608 error_propagate(errp, local_err);
613 /* get L2 table/refcount block cache size from command line options */
614 read_cache_sizes(bs, opts, &l2_cache_size, &refcount_cache_size,
617 error_propagate(errp, local_err);
622 l2_cache_size /= s->cluster_size;
623 if (l2_cache_size < MIN_L2_CACHE_SIZE) {
624 l2_cache_size = MIN_L2_CACHE_SIZE;
626 if (l2_cache_size > INT_MAX) {
627 error_setg(errp, "L2 cache size too big");
632 refcount_cache_size /= s->cluster_size;
633 if (refcount_cache_size < MIN_REFCOUNT_CACHE_SIZE) {
634 refcount_cache_size = MIN_REFCOUNT_CACHE_SIZE;
636 if (refcount_cache_size > INT_MAX) {
637 error_setg(errp, "Refcount cache size too big");
642 /* alloc L2 table/refcount block cache */
643 s->l2_table_cache = qcow2_cache_create(bs, l2_cache_size);
644 s->refcount_block_cache = qcow2_cache_create(bs, refcount_cache_size);
645 if (s->l2_table_cache == NULL || s->refcount_block_cache == NULL) {
646 error_setg(errp, "Could not allocate metadata caches");
651 /* New interval for cache cleanup timer */
652 cache_clean_interval =
653 qemu_opt_get_number(opts, QCOW2_OPT_CACHE_CLEAN_INTERVAL, 0);
654 if (cache_clean_interval > UINT_MAX) {
655 error_setg(errp, "Cache clean interval too big");
659 s->cache_clean_interval = cache_clean_interval;
660 cache_clean_timer_init(bs, bdrv_get_aio_context(bs));
662 /* Enable lazy_refcounts according to image and command line options */
663 s->use_lazy_refcounts = qemu_opt_get_bool(opts, QCOW2_OPT_LAZY_REFCOUNTS,
664 (s->compatible_features & QCOW2_COMPAT_LAZY_REFCOUNTS));
666 s->discard_passthrough[QCOW2_DISCARD_NEVER] = false;
667 s->discard_passthrough[QCOW2_DISCARD_ALWAYS] = true;
668 s->discard_passthrough[QCOW2_DISCARD_REQUEST] =
669 qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_REQUEST,
670 flags & BDRV_O_UNMAP);
671 s->discard_passthrough[QCOW2_DISCARD_SNAPSHOT] =
672 qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_SNAPSHOT, true);
673 s->discard_passthrough[QCOW2_DISCARD_OTHER] =
674 qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_OTHER, false);
676 opt_overlap_check = qemu_opt_get(opts, QCOW2_OPT_OVERLAP);
677 opt_overlap_check_template = qemu_opt_get(opts, QCOW2_OPT_OVERLAP_TEMPLATE);
678 if (opt_overlap_check_template && opt_overlap_check &&
679 strcmp(opt_overlap_check_template, opt_overlap_check))
681 error_setg(errp, "Conflicting values for qcow2 options '"
682 QCOW2_OPT_OVERLAP "' ('%s') and '" QCOW2_OPT_OVERLAP_TEMPLATE
683 "' ('%s')", opt_overlap_check, opt_overlap_check_template);
687 if (!opt_overlap_check) {
688 opt_overlap_check = opt_overlap_check_template ?: "cached";
691 if (!strcmp(opt_overlap_check, "none")) {
692 overlap_check_template = 0;
693 } else if (!strcmp(opt_overlap_check, "constant")) {
694 overlap_check_template = QCOW2_OL_CONSTANT;
695 } else if (!strcmp(opt_overlap_check, "cached")) {
696 overlap_check_template = QCOW2_OL_CACHED;
697 } else if (!strcmp(opt_overlap_check, "all")) {
698 overlap_check_template = QCOW2_OL_ALL;
700 error_setg(errp, "Unsupported value '%s' for qcow2 option "
701 "'overlap-check'. Allowed are any of the following: "
702 "none, constant, cached, all", opt_overlap_check);
707 s->overlap_check = 0;
708 for (i = 0; i < QCOW2_OL_MAX_BITNR; i++) {
709 /* overlap-check defines a template bitmask, but every flag may be
710 * overwritten through the associated boolean option */
712 qemu_opt_get_bool(opts, overlap_bool_option_names[i],
713 overlap_check_template & (1 << i)) << i;
716 if (s->use_lazy_refcounts && s->qcow_version < 3) {
717 error_setg(errp, "Lazy refcounts require a qcow2 image with at least "
718 "qemu 1.1 compatibility level");
731 static int qcow2_open(BlockDriverState *bs, QDict *options, int flags,
734 BDRVQcow2State *s = bs->opaque;
738 Error *local_err = NULL;
740 uint64_t l1_vm_state_index;
742 ret = bdrv_pread(bs->file, 0, &header, sizeof(header));
744 error_setg_errno(errp, -ret, "Could not read qcow2 header");
747 be32_to_cpus(&header.magic);
748 be32_to_cpus(&header.version);
749 be64_to_cpus(&header.backing_file_offset);
750 be32_to_cpus(&header.backing_file_size);
751 be64_to_cpus(&header.size);
752 be32_to_cpus(&header.cluster_bits);
753 be32_to_cpus(&header.crypt_method);
754 be64_to_cpus(&header.l1_table_offset);
755 be32_to_cpus(&header.l1_size);
756 be64_to_cpus(&header.refcount_table_offset);
757 be32_to_cpus(&header.refcount_table_clusters);
758 be64_to_cpus(&header.snapshots_offset);
759 be32_to_cpus(&header.nb_snapshots);
761 if (header.magic != QCOW_MAGIC) {
762 error_setg(errp, "Image is not in qcow2 format");
766 if (header.version < 2 || header.version > 3) {
767 report_unsupported(bs, errp, "QCOW version %" PRIu32, header.version);
772 s->qcow_version = header.version;
774 /* Initialise cluster size */
775 if (header.cluster_bits < MIN_CLUSTER_BITS ||
776 header.cluster_bits > MAX_CLUSTER_BITS) {
777 error_setg(errp, "Unsupported cluster size: 2^%" PRIu32,
778 header.cluster_bits);
783 s->cluster_bits = header.cluster_bits;
784 s->cluster_size = 1 << s->cluster_bits;
785 s->cluster_sectors = 1 << (s->cluster_bits - 9);
787 /* Initialise version 3 header fields */
788 if (header.version == 2) {
789 header.incompatible_features = 0;
790 header.compatible_features = 0;
791 header.autoclear_features = 0;
792 header.refcount_order = 4;
793 header.header_length = 72;
795 be64_to_cpus(&header.incompatible_features);
796 be64_to_cpus(&header.compatible_features);
797 be64_to_cpus(&header.autoclear_features);
798 be32_to_cpus(&header.refcount_order);
799 be32_to_cpus(&header.header_length);
801 if (header.header_length < 104) {
802 error_setg(errp, "qcow2 header too short");
808 if (header.header_length > s->cluster_size) {
809 error_setg(errp, "qcow2 header exceeds cluster size");
814 if (header.header_length > sizeof(header)) {
815 s->unknown_header_fields_size = header.header_length - sizeof(header);
816 s->unknown_header_fields = g_malloc(s->unknown_header_fields_size);
817 ret = bdrv_pread(bs->file, sizeof(header), s->unknown_header_fields,
818 s->unknown_header_fields_size);
820 error_setg_errno(errp, -ret, "Could not read unknown qcow2 header "
826 if (header.backing_file_offset > s->cluster_size) {
827 error_setg(errp, "Invalid backing file offset");
832 if (header.backing_file_offset) {
833 ext_end = header.backing_file_offset;
835 ext_end = 1 << header.cluster_bits;
838 /* Handle feature bits */
839 s->incompatible_features = header.incompatible_features;
840 s->compatible_features = header.compatible_features;
841 s->autoclear_features = header.autoclear_features;
843 if (s->incompatible_features & ~QCOW2_INCOMPAT_MASK) {
844 void *feature_table = NULL;
845 qcow2_read_extensions(bs, header.header_length, ext_end,
846 &feature_table, NULL);
847 report_unsupported_feature(bs, errp, feature_table,
848 s->incompatible_features &
849 ~QCOW2_INCOMPAT_MASK);
851 g_free(feature_table);
855 if (s->incompatible_features & QCOW2_INCOMPAT_CORRUPT) {
856 /* Corrupt images may not be written to unless they are being repaired
858 if ((flags & BDRV_O_RDWR) && !(flags & BDRV_O_CHECK)) {
859 error_setg(errp, "qcow2: Image is corrupt; cannot be opened "
866 /* Check support for various header values */
867 if (header.refcount_order > 6) {
868 error_setg(errp, "Reference count entry width too large; may not "
873 s->refcount_order = header.refcount_order;
874 s->refcount_bits = 1 << s->refcount_order;
875 s->refcount_max = UINT64_C(1) << (s->refcount_bits - 1);
876 s->refcount_max += s->refcount_max - 1;
878 if (header.crypt_method > QCOW_CRYPT_AES) {
879 error_setg(errp, "Unsupported encryption method: %" PRIu32,
880 header.crypt_method);
884 if (!qcrypto_cipher_supports(QCRYPTO_CIPHER_ALG_AES_128)) {
885 error_setg(errp, "AES cipher not available");
889 s->crypt_method_header = header.crypt_method;
890 if (s->crypt_method_header) {
894 s->l2_bits = s->cluster_bits - 3; /* L2 is always one cluster */
895 s->l2_size = 1 << s->l2_bits;
896 /* 2^(s->refcount_order - 3) is the refcount width in bytes */
897 s->refcount_block_bits = s->cluster_bits - (s->refcount_order - 3);
898 s->refcount_block_size = 1 << s->refcount_block_bits;
899 bs->total_sectors = header.size / 512;
900 s->csize_shift = (62 - (s->cluster_bits - 8));
901 s->csize_mask = (1 << (s->cluster_bits - 8)) - 1;
902 s->cluster_offset_mask = (1LL << s->csize_shift) - 1;
904 s->refcount_table_offset = header.refcount_table_offset;
905 s->refcount_table_size =
906 header.refcount_table_clusters << (s->cluster_bits - 3);
908 if (header.refcount_table_clusters > qcow2_max_refcount_clusters(s)) {
909 error_setg(errp, "Reference count table too large");
914 ret = validate_table_offset(bs, s->refcount_table_offset,
915 s->refcount_table_size, sizeof(uint64_t));
917 error_setg(errp, "Invalid reference count table offset");
921 /* Snapshot table offset/length */
922 if (header.nb_snapshots > QCOW_MAX_SNAPSHOTS) {
923 error_setg(errp, "Too many snapshots");
928 ret = validate_table_offset(bs, header.snapshots_offset,
930 sizeof(QCowSnapshotHeader));
932 error_setg(errp, "Invalid snapshot table offset");
936 /* read the level 1 table */
937 if (header.l1_size > QCOW_MAX_L1_SIZE / sizeof(uint64_t)) {
938 error_setg(errp, "Active L1 table too large");
942 s->l1_size = header.l1_size;
944 l1_vm_state_index = size_to_l1(s, header.size);
945 if (l1_vm_state_index > INT_MAX) {
946 error_setg(errp, "Image is too big");
950 s->l1_vm_state_index = l1_vm_state_index;
952 /* the L1 table must contain at least enough entries to put
954 if (s->l1_size < s->l1_vm_state_index) {
955 error_setg(errp, "L1 table is too small");
960 ret = validate_table_offset(bs, header.l1_table_offset,
961 header.l1_size, sizeof(uint64_t));
963 error_setg(errp, "Invalid L1 table offset");
966 s->l1_table_offset = header.l1_table_offset;
969 if (s->l1_size > 0) {
970 s->l1_table = qemu_try_blockalign(bs->file,
971 align_offset(s->l1_size * sizeof(uint64_t), 512));
972 if (s->l1_table == NULL) {
973 error_setg(errp, "Could not allocate L1 table");
977 ret = bdrv_pread(bs->file, s->l1_table_offset, s->l1_table,
978 s->l1_size * sizeof(uint64_t));
980 error_setg_errno(errp, -ret, "Could not read L1 table");
983 for(i = 0;i < s->l1_size; i++) {
984 be64_to_cpus(&s->l1_table[i]);
988 /* Parse driver-specific options */
989 ret = qcow2_update_options(bs, options, flags, errp);
994 s->cluster_cache = g_malloc(s->cluster_size);
995 /* one more sector for decompressed data alignment */
996 s->cluster_data = qemu_try_blockalign(bs->file, QCOW_MAX_CRYPT_CLUSTERS
997 * s->cluster_size + 512);
998 if (s->cluster_data == NULL) {
999 error_setg(errp, "Could not allocate temporary cluster buffer");
1004 s->cluster_cache_offset = -1;
1007 ret = qcow2_refcount_init(bs);
1009 error_setg_errno(errp, -ret, "Could not initialize refcount handling");
1013 QLIST_INIT(&s->cluster_allocs);
1014 QTAILQ_INIT(&s->discards);
1016 /* read qcow2 extensions */
1017 if (qcow2_read_extensions(bs, header.header_length, ext_end, NULL,
1019 error_propagate(errp, local_err);
1024 /* read the backing file name */
1025 if (header.backing_file_offset != 0) {
1026 len = header.backing_file_size;
1027 if (len > MIN(1023, s->cluster_size - header.backing_file_offset) ||
1028 len >= sizeof(bs->backing_file)) {
1029 error_setg(errp, "Backing file name too long");
1033 ret = bdrv_pread(bs->file, header.backing_file_offset,
1034 bs->backing_file, len);
1036 error_setg_errno(errp, -ret, "Could not read backing file name");
1039 bs->backing_file[len] = '\0';
1040 s->image_backing_file = g_strdup(bs->backing_file);
1043 /* Internal snapshots */
1044 s->snapshots_offset = header.snapshots_offset;
1045 s->nb_snapshots = header.nb_snapshots;
1047 ret = qcow2_read_snapshots(bs);
1049 error_setg_errno(errp, -ret, "Could not read snapshots");
1053 /* Clear unknown autoclear feature bits */
1054 if (!bs->read_only && !(flags & BDRV_O_INCOMING) && s->autoclear_features) {
1055 s->autoclear_features = 0;
1056 ret = qcow2_update_header(bs);
1058 error_setg_errno(errp, -ret, "Could not update qcow2 header");
1063 /* Initialise locks */
1064 qemu_co_mutex_init(&s->lock);
1066 /* Repair image if dirty */
1067 if (!(flags & (BDRV_O_CHECK | BDRV_O_INCOMING)) && !bs->read_only &&
1068 (s->incompatible_features & QCOW2_INCOMPAT_DIRTY)) {
1069 BdrvCheckResult result = {0};
1071 ret = qcow2_check(bs, &result, BDRV_FIX_ERRORS | BDRV_FIX_LEAKS);
1073 error_setg_errno(errp, -ret, "Could not repair dirty image");
1080 BdrvCheckResult result = {0};
1081 qcow2_check_refcounts(bs, &result, 0);
1087 g_free(s->unknown_header_fields);
1088 cleanup_unknown_header_ext(bs);
1089 qcow2_free_snapshots(bs);
1090 qcow2_refcount_close(bs);
1091 qemu_vfree(s->l1_table);
1092 /* else pre-write overlap checks in cache_destroy may crash */
1094 cache_clean_timer_del(bs);
1095 if (s->l2_table_cache) {
1096 qcow2_cache_destroy(bs, s->l2_table_cache);
1098 if (s->refcount_block_cache) {
1099 qcow2_cache_destroy(bs, s->refcount_block_cache);
1101 g_free(s->cluster_cache);
1102 qemu_vfree(s->cluster_data);
1106 static void qcow2_refresh_limits(BlockDriverState *bs, Error **errp)
1108 BDRVQcow2State *s = bs->opaque;
1110 bs->bl.write_zeroes_alignment = s->cluster_sectors;
1113 static int qcow2_set_key(BlockDriverState *bs, const char *key)
1115 BDRVQcow2State *s = bs->opaque;
1120 memset(keybuf, 0, 16);
1124 /* XXX: we could compress the chars to 7 bits to increase
1126 for(i = 0;i < len;i++) {
1129 assert(bs->encrypted);
1131 qcrypto_cipher_free(s->cipher);
1132 s->cipher = qcrypto_cipher_new(
1133 QCRYPTO_CIPHER_ALG_AES_128,
1134 QCRYPTO_CIPHER_MODE_CBC,
1135 keybuf, G_N_ELEMENTS(keybuf),
1139 /* XXX would be nice if errors in this method could
1140 * be properly propagate to the caller. Would need
1141 * the bdrv_set_key() API signature to be fixed. */
1148 /* We have no actual commit/abort logic for qcow2, but we need to write out any
1149 * unwritten data if we reopen read-only. */
1150 static int qcow2_reopen_prepare(BDRVReopenState *state,
1151 BlockReopenQueue *queue, Error **errp)
1155 if ((state->flags & BDRV_O_RDWR) == 0) {
1156 ret = bdrv_flush(state->bs);
1161 ret = qcow2_mark_clean(state->bs);
1170 static int64_t coroutine_fn qcow2_co_get_block_status(BlockDriverState *bs,
1171 int64_t sector_num, int nb_sectors, int *pnum)
1173 BDRVQcow2State *s = bs->opaque;
1174 uint64_t cluster_offset;
1175 int index_in_cluster, ret;
1179 qemu_co_mutex_lock(&s->lock);
1180 ret = qcow2_get_cluster_offset(bs, sector_num << 9, pnum, &cluster_offset);
1181 qemu_co_mutex_unlock(&s->lock);
1186 if (cluster_offset != 0 && ret != QCOW2_CLUSTER_COMPRESSED &&
1188 index_in_cluster = sector_num & (s->cluster_sectors - 1);
1189 cluster_offset |= (index_in_cluster << BDRV_SECTOR_BITS);
1190 status |= BDRV_BLOCK_OFFSET_VALID | cluster_offset;
1192 if (ret == QCOW2_CLUSTER_ZERO) {
1193 status |= BDRV_BLOCK_ZERO;
1194 } else if (ret != QCOW2_CLUSTER_UNALLOCATED) {
1195 status |= BDRV_BLOCK_DATA;
1200 /* handle reading after the end of the backing file */
1201 int qcow2_backing_read1(BlockDriverState *bs, QEMUIOVector *qiov,
1202 int64_t sector_num, int nb_sectors)
1205 if ((sector_num + nb_sectors) <= bs->total_sectors)
1207 if (sector_num >= bs->total_sectors)
1210 n1 = bs->total_sectors - sector_num;
1212 qemu_iovec_memset(qiov, 512 * n1, 0, 512 * (nb_sectors - n1));
1217 static coroutine_fn int qcow2_co_readv(BlockDriverState *bs, int64_t sector_num,
1218 int remaining_sectors, QEMUIOVector *qiov)
1220 BDRVQcow2State *s = bs->opaque;
1221 int index_in_cluster, n1;
1223 int cur_nr_sectors; /* number of sectors in current iteration */
1224 uint64_t cluster_offset = 0;
1225 uint64_t bytes_done = 0;
1226 QEMUIOVector hd_qiov;
1227 uint8_t *cluster_data = NULL;
1229 qemu_iovec_init(&hd_qiov, qiov->niov);
1231 qemu_co_mutex_lock(&s->lock);
1233 while (remaining_sectors != 0) {
1235 /* prepare next request */
1236 cur_nr_sectors = remaining_sectors;
1238 cur_nr_sectors = MIN(cur_nr_sectors,
1239 QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors);
1242 ret = qcow2_get_cluster_offset(bs, sector_num << 9,
1243 &cur_nr_sectors, &cluster_offset);
1248 index_in_cluster = sector_num & (s->cluster_sectors - 1);
1250 qemu_iovec_reset(&hd_qiov);
1251 qemu_iovec_concat(&hd_qiov, qiov, bytes_done,
1252 cur_nr_sectors * 512);
1255 case QCOW2_CLUSTER_UNALLOCATED:
1257 if (bs->backing_hd) {
1258 /* read from the base image */
1259 n1 = qcow2_backing_read1(bs->backing_hd, &hd_qiov,
1260 sector_num, cur_nr_sectors);
1262 QEMUIOVector local_qiov;
1264 qemu_iovec_init(&local_qiov, hd_qiov.niov);
1265 qemu_iovec_concat(&local_qiov, &hd_qiov, 0,
1266 n1 * BDRV_SECTOR_SIZE);
1268 BLKDBG_EVENT(bs->file, BLKDBG_READ_BACKING_AIO);
1269 qemu_co_mutex_unlock(&s->lock);
1270 ret = bdrv_co_readv(bs->backing_hd, sector_num,
1272 qemu_co_mutex_lock(&s->lock);
1274 qemu_iovec_destroy(&local_qiov);
1281 /* Note: in this case, no need to wait */
1282 qemu_iovec_memset(&hd_qiov, 0, 0, 512 * cur_nr_sectors);
1286 case QCOW2_CLUSTER_ZERO:
1287 qemu_iovec_memset(&hd_qiov, 0, 0, 512 * cur_nr_sectors);
1290 case QCOW2_CLUSTER_COMPRESSED:
1291 /* add AIO support for compressed blocks ? */
1292 ret = qcow2_decompress_cluster(bs, cluster_offset);
1297 qemu_iovec_from_buf(&hd_qiov, 0,
1298 s->cluster_cache + index_in_cluster * 512,
1299 512 * cur_nr_sectors);
1302 case QCOW2_CLUSTER_NORMAL:
1303 if ((cluster_offset & 511) != 0) {
1308 if (bs->encrypted) {
1312 * For encrypted images, read everything into a temporary
1313 * contiguous buffer on which the AES functions can work.
1315 if (!cluster_data) {
1317 qemu_try_blockalign(bs->file, QCOW_MAX_CRYPT_CLUSTERS
1319 if (cluster_data == NULL) {
1325 assert(cur_nr_sectors <=
1326 QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors);
1327 qemu_iovec_reset(&hd_qiov);
1328 qemu_iovec_add(&hd_qiov, cluster_data,
1329 512 * cur_nr_sectors);
1332 BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO);
1333 qemu_co_mutex_unlock(&s->lock);
1334 ret = bdrv_co_readv(bs->file,
1335 (cluster_offset >> 9) + index_in_cluster,
1336 cur_nr_sectors, &hd_qiov);
1337 qemu_co_mutex_lock(&s->lock);
1341 if (bs->encrypted) {
1344 if (qcow2_encrypt_sectors(s, sector_num, cluster_data,
1345 cluster_data, cur_nr_sectors, false,
1351 qemu_iovec_from_buf(qiov, bytes_done,
1352 cluster_data, 512 * cur_nr_sectors);
1357 g_assert_not_reached();
1362 remaining_sectors -= cur_nr_sectors;
1363 sector_num += cur_nr_sectors;
1364 bytes_done += cur_nr_sectors * 512;
1369 qemu_co_mutex_unlock(&s->lock);
1371 qemu_iovec_destroy(&hd_qiov);
1372 qemu_vfree(cluster_data);
1377 static coroutine_fn int qcow2_co_writev(BlockDriverState *bs,
1379 int remaining_sectors,
1382 BDRVQcow2State *s = bs->opaque;
1383 int index_in_cluster;
1385 int cur_nr_sectors; /* number of sectors in current iteration */
1386 uint64_t cluster_offset;
1387 QEMUIOVector hd_qiov;
1388 uint64_t bytes_done = 0;
1389 uint8_t *cluster_data = NULL;
1390 QCowL2Meta *l2meta = NULL;
1392 trace_qcow2_writev_start_req(qemu_coroutine_self(), sector_num,
1395 qemu_iovec_init(&hd_qiov, qiov->niov);
1397 s->cluster_cache_offset = -1; /* disable compressed cache */
1399 qemu_co_mutex_lock(&s->lock);
1401 while (remaining_sectors != 0) {
1405 trace_qcow2_writev_start_part(qemu_coroutine_self());
1406 index_in_cluster = sector_num & (s->cluster_sectors - 1);
1407 cur_nr_sectors = remaining_sectors;
1408 if (bs->encrypted &&
1410 QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors - index_in_cluster) {
1412 QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors - index_in_cluster;
1415 ret = qcow2_alloc_cluster_offset(bs, sector_num << 9,
1416 &cur_nr_sectors, &cluster_offset, &l2meta);
1421 assert((cluster_offset & 511) == 0);
1423 qemu_iovec_reset(&hd_qiov);
1424 qemu_iovec_concat(&hd_qiov, qiov, bytes_done,
1425 cur_nr_sectors * 512);
1427 if (bs->encrypted) {
1430 if (!cluster_data) {
1431 cluster_data = qemu_try_blockalign(bs->file,
1432 QCOW_MAX_CRYPT_CLUSTERS
1434 if (cluster_data == NULL) {
1440 assert(hd_qiov.size <=
1441 QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size);
1442 qemu_iovec_to_buf(&hd_qiov, 0, cluster_data, hd_qiov.size);
1444 if (qcow2_encrypt_sectors(s, sector_num, cluster_data,
1445 cluster_data, cur_nr_sectors,
1452 qemu_iovec_reset(&hd_qiov);
1453 qemu_iovec_add(&hd_qiov, cluster_data,
1454 cur_nr_sectors * 512);
1457 ret = qcow2_pre_write_overlap_check(bs, 0,
1458 cluster_offset + index_in_cluster * BDRV_SECTOR_SIZE,
1459 cur_nr_sectors * BDRV_SECTOR_SIZE);
1464 qemu_co_mutex_unlock(&s->lock);
1465 BLKDBG_EVENT(bs->file, BLKDBG_WRITE_AIO);
1466 trace_qcow2_writev_data(qemu_coroutine_self(),
1467 (cluster_offset >> 9) + index_in_cluster);
1468 ret = bdrv_co_writev(bs->file,
1469 (cluster_offset >> 9) + index_in_cluster,
1470 cur_nr_sectors, &hd_qiov);
1471 qemu_co_mutex_lock(&s->lock);
1476 while (l2meta != NULL) {
1479 ret = qcow2_alloc_cluster_link_l2(bs, l2meta);
1484 /* Take the request off the list of running requests */
1485 if (l2meta->nb_clusters != 0) {
1486 QLIST_REMOVE(l2meta, next_in_flight);
1489 qemu_co_queue_restart_all(&l2meta->dependent_requests);
1491 next = l2meta->next;
1496 remaining_sectors -= cur_nr_sectors;
1497 sector_num += cur_nr_sectors;
1498 bytes_done += cur_nr_sectors * 512;
1499 trace_qcow2_writev_done_part(qemu_coroutine_self(), cur_nr_sectors);
1504 qemu_co_mutex_unlock(&s->lock);
1506 while (l2meta != NULL) {
1509 if (l2meta->nb_clusters != 0) {
1510 QLIST_REMOVE(l2meta, next_in_flight);
1512 qemu_co_queue_restart_all(&l2meta->dependent_requests);
1514 next = l2meta->next;
1519 qemu_iovec_destroy(&hd_qiov);
1520 qemu_vfree(cluster_data);
1521 trace_qcow2_writev_done_req(qemu_coroutine_self(), ret);
1526 static void qcow2_close(BlockDriverState *bs)
1528 BDRVQcow2State *s = bs->opaque;
1529 qemu_vfree(s->l1_table);
1530 /* else pre-write overlap checks in cache_destroy may crash */
1533 if (!(bs->open_flags & BDRV_O_INCOMING)) {
1536 ret1 = qcow2_cache_flush(bs, s->l2_table_cache);
1537 ret2 = qcow2_cache_flush(bs, s->refcount_block_cache);
1540 error_report("Failed to flush the L2 table cache: %s",
1544 error_report("Failed to flush the refcount block cache: %s",
1548 if (!ret1 && !ret2) {
1549 qcow2_mark_clean(bs);
1553 cache_clean_timer_del(bs);
1554 qcow2_cache_destroy(bs, s->l2_table_cache);
1555 qcow2_cache_destroy(bs, s->refcount_block_cache);
1557 qcrypto_cipher_free(s->cipher);
1560 g_free(s->unknown_header_fields);
1561 cleanup_unknown_header_ext(bs);
1563 g_free(s->image_backing_file);
1564 g_free(s->image_backing_format);
1566 g_free(s->cluster_cache);
1567 qemu_vfree(s->cluster_data);
1568 qcow2_refcount_close(bs);
1569 qcow2_free_snapshots(bs);
1572 static void qcow2_invalidate_cache(BlockDriverState *bs, Error **errp)
1574 BDRVQcow2State *s = bs->opaque;
1575 int flags = s->flags;
1576 QCryptoCipher *cipher = NULL;
1578 Error *local_err = NULL;
1582 * Backing files are read-only which makes all of their metadata immutable,
1583 * that means we don't have to worry about reopening them here.
1591 bdrv_invalidate_cache(bs->file, &local_err);
1593 error_propagate(errp, local_err);
1597 memset(s, 0, sizeof(BDRVQcow2State));
1598 options = qdict_clone_shallow(bs->options);
1600 ret = qcow2_open(bs, options, flags, &local_err);
1603 error_setg(errp, "Could not reopen qcow2 layer: %s",
1604 error_get_pretty(local_err));
1605 error_free(local_err);
1607 } else if (ret < 0) {
1608 error_setg_errno(errp, -ret, "Could not reopen qcow2 layer");
1615 static size_t header_ext_add(char *buf, uint32_t magic, const void *s,
1616 size_t len, size_t buflen)
1618 QCowExtension *ext_backing_fmt = (QCowExtension*) buf;
1619 size_t ext_len = sizeof(QCowExtension) + ((len + 7) & ~7);
1621 if (buflen < ext_len) {
1625 *ext_backing_fmt = (QCowExtension) {
1626 .magic = cpu_to_be32(magic),
1627 .len = cpu_to_be32(len),
1629 memcpy(buf + sizeof(QCowExtension), s, len);
1635 * Updates the qcow2 header, including the variable length parts of it, i.e.
1636 * the backing file name and all extensions. qcow2 was not designed to allow
1637 * such changes, so if we run out of space (we can only use the first cluster)
1638 * this function may fail.
1640 * Returns 0 on success, -errno in error cases.
1642 int qcow2_update_header(BlockDriverState *bs)
1644 BDRVQcow2State *s = bs->opaque;
1647 size_t buflen = s->cluster_size;
1649 uint64_t total_size;
1650 uint32_t refcount_table_clusters;
1651 size_t header_length;
1652 Qcow2UnknownHeaderExtension *uext;
1654 buf = qemu_blockalign(bs, buflen);
1656 /* Header structure */
1657 header = (QCowHeader*) buf;
1659 if (buflen < sizeof(*header)) {
1664 header_length = sizeof(*header) + s->unknown_header_fields_size;
1665 total_size = bs->total_sectors * BDRV_SECTOR_SIZE;
1666 refcount_table_clusters = s->refcount_table_size >> (s->cluster_bits - 3);
1668 *header = (QCowHeader) {
1669 /* Version 2 fields */
1670 .magic = cpu_to_be32(QCOW_MAGIC),
1671 .version = cpu_to_be32(s->qcow_version),
1672 .backing_file_offset = 0,
1673 .backing_file_size = 0,
1674 .cluster_bits = cpu_to_be32(s->cluster_bits),
1675 .size = cpu_to_be64(total_size),
1676 .crypt_method = cpu_to_be32(s->crypt_method_header),
1677 .l1_size = cpu_to_be32(s->l1_size),
1678 .l1_table_offset = cpu_to_be64(s->l1_table_offset),
1679 .refcount_table_offset = cpu_to_be64(s->refcount_table_offset),
1680 .refcount_table_clusters = cpu_to_be32(refcount_table_clusters),
1681 .nb_snapshots = cpu_to_be32(s->nb_snapshots),
1682 .snapshots_offset = cpu_to_be64(s->snapshots_offset),
1684 /* Version 3 fields */
1685 .incompatible_features = cpu_to_be64(s->incompatible_features),
1686 .compatible_features = cpu_to_be64(s->compatible_features),
1687 .autoclear_features = cpu_to_be64(s->autoclear_features),
1688 .refcount_order = cpu_to_be32(s->refcount_order),
1689 .header_length = cpu_to_be32(header_length),
1692 /* For older versions, write a shorter header */
1693 switch (s->qcow_version) {
1695 ret = offsetof(QCowHeader, incompatible_features);
1698 ret = sizeof(*header);
1707 memset(buf, 0, buflen);
1709 /* Preserve any unknown field in the header */
1710 if (s->unknown_header_fields_size) {
1711 if (buflen < s->unknown_header_fields_size) {
1716 memcpy(buf, s->unknown_header_fields, s->unknown_header_fields_size);
1717 buf += s->unknown_header_fields_size;
1718 buflen -= s->unknown_header_fields_size;
1721 /* Backing file format header extension */
1722 if (s->image_backing_format) {
1723 ret = header_ext_add(buf, QCOW2_EXT_MAGIC_BACKING_FORMAT,
1724 s->image_backing_format,
1725 strlen(s->image_backing_format),
1736 Qcow2Feature features[] = {
1738 .type = QCOW2_FEAT_TYPE_INCOMPATIBLE,
1739 .bit = QCOW2_INCOMPAT_DIRTY_BITNR,
1740 .name = "dirty bit",
1743 .type = QCOW2_FEAT_TYPE_INCOMPATIBLE,
1744 .bit = QCOW2_INCOMPAT_CORRUPT_BITNR,
1745 .name = "corrupt bit",
1748 .type = QCOW2_FEAT_TYPE_COMPATIBLE,
1749 .bit = QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR,
1750 .name = "lazy refcounts",
1754 ret = header_ext_add(buf, QCOW2_EXT_MAGIC_FEATURE_TABLE,
1755 features, sizeof(features), buflen);
1762 /* Keep unknown header extensions */
1763 QLIST_FOREACH(uext, &s->unknown_header_ext, next) {
1764 ret = header_ext_add(buf, uext->magic, uext->data, uext->len, buflen);
1773 /* End of header extensions */
1774 ret = header_ext_add(buf, QCOW2_EXT_MAGIC_END, NULL, 0, buflen);
1782 /* Backing file name */
1783 if (s->image_backing_file) {
1784 size_t backing_file_len = strlen(s->image_backing_file);
1786 if (buflen < backing_file_len) {
1791 /* Using strncpy is ok here, since buf is not NUL-terminated. */
1792 strncpy(buf, s->image_backing_file, buflen);
1794 header->backing_file_offset = cpu_to_be64(buf - ((char*) header));
1795 header->backing_file_size = cpu_to_be32(backing_file_len);
1798 /* Write the new header */
1799 ret = bdrv_pwrite(bs->file, 0, header, s->cluster_size);
1810 static int qcow2_change_backing_file(BlockDriverState *bs,
1811 const char *backing_file, const char *backing_fmt)
1813 BDRVQcow2State *s = bs->opaque;
1815 pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: "");
1816 pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: "");
1818 g_free(s->image_backing_file);
1819 g_free(s->image_backing_format);
1821 s->image_backing_file = backing_file ? g_strdup(bs->backing_file) : NULL;
1822 s->image_backing_format = backing_fmt ? g_strdup(bs->backing_format) : NULL;
1824 return qcow2_update_header(bs);
1827 static int preallocate(BlockDriverState *bs)
1829 uint64_t nb_sectors;
1831 uint64_t host_offset = 0;
1836 nb_sectors = bdrv_nb_sectors(bs);
1839 while (nb_sectors) {
1840 num = MIN(nb_sectors, INT_MAX >> BDRV_SECTOR_BITS);
1841 ret = qcow2_alloc_cluster_offset(bs, offset, &num,
1842 &host_offset, &meta);
1848 QCowL2Meta *next = meta->next;
1850 ret = qcow2_alloc_cluster_link_l2(bs, meta);
1852 qcow2_free_any_clusters(bs, meta->alloc_offset,
1853 meta->nb_clusters, QCOW2_DISCARD_NEVER);
1857 /* There are no dependent requests, but we need to remove our
1858 * request from the list of in-flight requests */
1859 QLIST_REMOVE(meta, next_in_flight);
1865 /* TODO Preallocate data if requested */
1868 offset += num << BDRV_SECTOR_BITS;
1872 * It is expected that the image file is large enough to actually contain
1873 * all of the allocated clusters (otherwise we get failing reads after
1874 * EOF). Extend the image to the last allocated sector.
1876 if (host_offset != 0) {
1877 uint8_t buf[BDRV_SECTOR_SIZE];
1878 memset(buf, 0, BDRV_SECTOR_SIZE);
1879 ret = bdrv_write(bs->file, (host_offset >> BDRV_SECTOR_BITS) + num - 1,
1889 static int qcow2_create2(const char *filename, int64_t total_size,
1890 const char *backing_file, const char *backing_format,
1891 int flags, size_t cluster_size, PreallocMode prealloc,
1892 QemuOpts *opts, int version, int refcount_order,
1898 /* Calculate cluster_bits */
1899 cluster_bits = ctz32(cluster_size);
1900 if (cluster_bits < MIN_CLUSTER_BITS || cluster_bits > MAX_CLUSTER_BITS ||
1901 (1 << cluster_bits) != cluster_size)
1903 error_setg(errp, "Cluster size must be a power of two between %d and "
1904 "%dk", 1 << MIN_CLUSTER_BITS, 1 << (MAX_CLUSTER_BITS - 10));
1909 * Open the image file and write a minimal qcow2 header.
1911 * We keep things simple and start with a zero-sized image. We also
1912 * do without refcount blocks or a L1 table for now. We'll fix the
1913 * inconsistency later.
1915 * We do need a refcount table because growing the refcount table means
1916 * allocating two new refcount blocks - the seconds of which would be at
1917 * 2 GB for 64k clusters, and we don't want to have a 2 GB initial file
1918 * size for any qcow2 image.
1920 BlockDriverState* bs;
1922 uint64_t* refcount_table;
1923 Error *local_err = NULL;
1926 if (prealloc == PREALLOC_MODE_FULL || prealloc == PREALLOC_MODE_FALLOC) {
1927 /* Note: The following calculation does not need to be exact; if it is a
1928 * bit off, either some bytes will be "leaked" (which is fine) or we
1929 * will need to increase the file size by some bytes (which is fine,
1930 * too, as long as the bulk is allocated here). Therefore, using
1931 * floating point arithmetic is fine. */
1932 int64_t meta_size = 0;
1933 uint64_t nreftablee, nrefblocke, nl1e, nl2e;
1934 int64_t aligned_total_size = align_offset(total_size, cluster_size);
1935 int refblock_bits, refblock_size;
1936 /* refcount entry size in bytes */
1937 double rces = (1 << refcount_order) / 8.;
1939 /* see qcow2_open() */
1940 refblock_bits = cluster_bits - (refcount_order - 3);
1941 refblock_size = 1 << refblock_bits;
1943 /* header: 1 cluster */
1944 meta_size += cluster_size;
1946 /* total size of L2 tables */
1947 nl2e = aligned_total_size / cluster_size;
1948 nl2e = align_offset(nl2e, cluster_size / sizeof(uint64_t));
1949 meta_size += nl2e * sizeof(uint64_t);
1951 /* total size of L1 tables */
1952 nl1e = nl2e * sizeof(uint64_t) / cluster_size;
1953 nl1e = align_offset(nl1e, cluster_size / sizeof(uint64_t));
1954 meta_size += nl1e * sizeof(uint64_t);
1956 /* total size of refcount blocks
1958 * note: every host cluster is reference-counted, including metadata
1959 * (even refcount blocks are recursively included).
1961 * a = total_size (this is the guest disk size)
1962 * m = meta size not including refcount blocks and refcount tables
1964 * y1 = number of refcount blocks entries
1965 * y2 = meta size including everything
1966 * rces = refcount entry size in bytes
1969 * y2 = y1 * rces + y1 * rces * sizeof(u64) / c + m
1971 * y1 = (a + m) / (c - rces - rces * sizeof(u64) / c)
1973 nrefblocke = (aligned_total_size + meta_size + cluster_size)
1974 / (cluster_size - rces - rces * sizeof(uint64_t)
1976 meta_size += DIV_ROUND_UP(nrefblocke, refblock_size) * cluster_size;
1978 /* total size of refcount tables */
1979 nreftablee = nrefblocke / refblock_size;
1980 nreftablee = align_offset(nreftablee, cluster_size / sizeof(uint64_t));
1981 meta_size += nreftablee * sizeof(uint64_t);
1983 qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
1984 aligned_total_size + meta_size, &error_abort);
1985 qemu_opt_set(opts, BLOCK_OPT_PREALLOC, PreallocMode_lookup[prealloc],
1989 ret = bdrv_create_file(filename, opts, &local_err);
1991 error_propagate(errp, local_err);
1996 ret = bdrv_open(&bs, filename, NULL, NULL, BDRV_O_RDWR | BDRV_O_PROTOCOL,
1999 error_propagate(errp, local_err);
2003 /* Write the header */
2004 QEMU_BUILD_BUG_ON((1 << MIN_CLUSTER_BITS) < sizeof(*header));
2005 header = g_malloc0(cluster_size);
2006 *header = (QCowHeader) {
2007 .magic = cpu_to_be32(QCOW_MAGIC),
2008 .version = cpu_to_be32(version),
2009 .cluster_bits = cpu_to_be32(cluster_bits),
2010 .size = cpu_to_be64(0),
2011 .l1_table_offset = cpu_to_be64(0),
2012 .l1_size = cpu_to_be32(0),
2013 .refcount_table_offset = cpu_to_be64(cluster_size),
2014 .refcount_table_clusters = cpu_to_be32(1),
2015 .refcount_order = cpu_to_be32(refcount_order),
2016 .header_length = cpu_to_be32(sizeof(*header)),
2019 if (flags & BLOCK_FLAG_ENCRYPT) {
2020 header->crypt_method = cpu_to_be32(QCOW_CRYPT_AES);
2022 header->crypt_method = cpu_to_be32(QCOW_CRYPT_NONE);
2025 if (flags & BLOCK_FLAG_LAZY_REFCOUNTS) {
2026 header->compatible_features |=
2027 cpu_to_be64(QCOW2_COMPAT_LAZY_REFCOUNTS);
2030 ret = bdrv_pwrite(bs, 0, header, cluster_size);
2033 error_setg_errno(errp, -ret, "Could not write qcow2 header");
2037 /* Write a refcount table with one refcount block */
2038 refcount_table = g_malloc0(2 * cluster_size);
2039 refcount_table[0] = cpu_to_be64(2 * cluster_size);
2040 ret = bdrv_pwrite(bs, cluster_size, refcount_table, 2 * cluster_size);
2041 g_free(refcount_table);
2044 error_setg_errno(errp, -ret, "Could not write refcount table");
2052 * And now open the image and make it consistent first (i.e. increase the
2053 * refcount of the cluster that is occupied by the header and the refcount
2056 options = qdict_new();
2057 qdict_put(options, "driver", qstring_from_str("qcow2"));
2058 ret = bdrv_open(&bs, filename, NULL, options,
2059 BDRV_O_RDWR | BDRV_O_CACHE_WB | BDRV_O_NO_FLUSH,
2062 error_propagate(errp, local_err);
2066 ret = qcow2_alloc_clusters(bs, 3 * cluster_size);
2068 error_setg_errno(errp, -ret, "Could not allocate clusters for qcow2 "
2069 "header and refcount table");
2072 } else if (ret != 0) {
2073 error_report("Huh, first cluster in empty image is already in use?");
2077 /* Okay, now that we have a valid image, let's give it the right size */
2078 ret = bdrv_truncate(bs, total_size);
2080 error_setg_errno(errp, -ret, "Could not resize image");
2084 /* Want a backing file? There you go.*/
2086 ret = bdrv_change_backing_file(bs, backing_file, backing_format);
2088 error_setg_errno(errp, -ret, "Could not assign backing file '%s' "
2089 "with format '%s'", backing_file, backing_format);
2094 /* And if we're supposed to preallocate metadata, do that now */
2095 if (prealloc != PREALLOC_MODE_OFF) {
2096 BDRVQcow2State *s = bs->opaque;
2097 qemu_co_mutex_lock(&s->lock);
2098 ret = preallocate(bs);
2099 qemu_co_mutex_unlock(&s->lock);
2101 error_setg_errno(errp, -ret, "Could not preallocate metadata");
2109 /* Reopen the image without BDRV_O_NO_FLUSH to flush it before returning */
2110 options = qdict_new();
2111 qdict_put(options, "driver", qstring_from_str("qcow2"));
2112 ret = bdrv_open(&bs, filename, NULL, options,
2113 BDRV_O_RDWR | BDRV_O_CACHE_WB | BDRV_O_NO_BACKING,
2116 error_propagate(errp, local_err);
2128 static int qcow2_create(const char *filename, QemuOpts *opts, Error **errp)
2130 char *backing_file = NULL;
2131 char *backing_fmt = NULL;
2135 size_t cluster_size = DEFAULT_CLUSTER_SIZE;
2136 PreallocMode prealloc;
2138 uint64_t refcount_bits = 16;
2140 Error *local_err = NULL;
2143 /* Read out options */
2144 size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
2146 backing_file = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE);
2147 backing_fmt = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FMT);
2148 if (qemu_opt_get_bool_del(opts, BLOCK_OPT_ENCRYPT, false)) {
2149 flags |= BLOCK_FLAG_ENCRYPT;
2151 cluster_size = qemu_opt_get_size_del(opts, BLOCK_OPT_CLUSTER_SIZE,
2152 DEFAULT_CLUSTER_SIZE);
2153 buf = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC);
2154 prealloc = qapi_enum_parse(PreallocMode_lookup, buf,
2155 PREALLOC_MODE_MAX, PREALLOC_MODE_OFF,
2158 error_propagate(errp, local_err);
2163 buf = qemu_opt_get_del(opts, BLOCK_OPT_COMPAT_LEVEL);
2165 /* keep the default */
2166 } else if (!strcmp(buf, "0.10")) {
2168 } else if (!strcmp(buf, "1.1")) {
2171 error_setg(errp, "Invalid compatibility level: '%s'", buf);
2176 if (qemu_opt_get_bool_del(opts, BLOCK_OPT_LAZY_REFCOUNTS, false)) {
2177 flags |= BLOCK_FLAG_LAZY_REFCOUNTS;
2180 if (backing_file && prealloc != PREALLOC_MODE_OFF) {
2181 error_setg(errp, "Backing file and preallocation cannot be used at "
2187 if (version < 3 && (flags & BLOCK_FLAG_LAZY_REFCOUNTS)) {
2188 error_setg(errp, "Lazy refcounts only supported with compatibility "
2189 "level 1.1 and above (use compat=1.1 or greater)");
2194 refcount_bits = qemu_opt_get_number_del(opts, BLOCK_OPT_REFCOUNT_BITS,
2196 if (refcount_bits > 64 || !is_power_of_2(refcount_bits)) {
2197 error_setg(errp, "Refcount width must be a power of two and may not "
2203 if (version < 3 && refcount_bits != 16) {
2204 error_setg(errp, "Different refcount widths than 16 bits require "
2205 "compatibility level 1.1 or above (use compat=1.1 or "
2211 refcount_order = ctz32(refcount_bits);
2213 ret = qcow2_create2(filename, size, backing_file, backing_fmt, flags,
2214 cluster_size, prealloc, opts, version, refcount_order,
2217 error_propagate(errp, local_err);
2221 g_free(backing_file);
2222 g_free(backing_fmt);
2227 static coroutine_fn int qcow2_co_write_zeroes(BlockDriverState *bs,
2228 int64_t sector_num, int nb_sectors, BdrvRequestFlags flags)
2231 BDRVQcow2State *s = bs->opaque;
2233 /* Emulate misaligned zero writes */
2234 if (sector_num % s->cluster_sectors || nb_sectors % s->cluster_sectors) {
2238 /* Whatever is left can use real zero clusters */
2239 qemu_co_mutex_lock(&s->lock);
2240 ret = qcow2_zero_clusters(bs, sector_num << BDRV_SECTOR_BITS,
2242 qemu_co_mutex_unlock(&s->lock);
2247 static coroutine_fn int qcow2_co_discard(BlockDriverState *bs,
2248 int64_t sector_num, int nb_sectors)
2251 BDRVQcow2State *s = bs->opaque;
2253 qemu_co_mutex_lock(&s->lock);
2254 ret = qcow2_discard_clusters(bs, sector_num << BDRV_SECTOR_BITS,
2255 nb_sectors, QCOW2_DISCARD_REQUEST, false);
2256 qemu_co_mutex_unlock(&s->lock);
2260 static int qcow2_truncate(BlockDriverState *bs, int64_t offset)
2262 BDRVQcow2State *s = bs->opaque;
2263 int64_t new_l1_size;
2267 error_report("The new size must be a multiple of 512");
2271 /* cannot proceed if image has snapshots */
2272 if (s->nb_snapshots) {
2273 error_report("Can't resize an image which has snapshots");
2277 /* shrinking is currently not supported */
2278 if (offset < bs->total_sectors * 512) {
2279 error_report("qcow2 doesn't support shrinking images yet");
2283 new_l1_size = size_to_l1(s, offset);
2284 ret = qcow2_grow_l1_table(bs, new_l1_size, true);
2289 /* write updated header.size */
2290 offset = cpu_to_be64(offset);
2291 ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, size),
2292 &offset, sizeof(uint64_t));
2297 s->l1_vm_state_index = new_l1_size;
2301 /* XXX: put compressed sectors first, then all the cluster aligned
2302 tables to avoid losing bytes in alignment */
2303 static int qcow2_write_compressed(BlockDriverState *bs, int64_t sector_num,
2304 const uint8_t *buf, int nb_sectors)
2306 BDRVQcow2State *s = bs->opaque;
2310 uint64_t cluster_offset;
2312 if (nb_sectors == 0) {
2313 /* align end of file to a sector boundary to ease reading with
2314 sector based I/Os */
2315 cluster_offset = bdrv_getlength(bs->file);
2316 return bdrv_truncate(bs->file, cluster_offset);
2319 if (nb_sectors != s->cluster_sectors) {
2322 /* Zero-pad last write if image size is not cluster aligned */
2323 if (sector_num + nb_sectors == bs->total_sectors &&
2324 nb_sectors < s->cluster_sectors) {
2325 uint8_t *pad_buf = qemu_blockalign(bs, s->cluster_size);
2326 memset(pad_buf, 0, s->cluster_size);
2327 memcpy(pad_buf, buf, nb_sectors * BDRV_SECTOR_SIZE);
2328 ret = qcow2_write_compressed(bs, sector_num,
2329 pad_buf, s->cluster_sectors);
2330 qemu_vfree(pad_buf);
2335 out_buf = g_malloc(s->cluster_size + (s->cluster_size / 1000) + 128);
2337 /* best compression, small window, no zlib header */
2338 memset(&strm, 0, sizeof(strm));
2339 ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION,
2341 9, Z_DEFAULT_STRATEGY);
2347 strm.avail_in = s->cluster_size;
2348 strm.next_in = (uint8_t *)buf;
2349 strm.avail_out = s->cluster_size;
2350 strm.next_out = out_buf;
2352 ret = deflate(&strm, Z_FINISH);
2353 if (ret != Z_STREAM_END && ret != Z_OK) {
2358 out_len = strm.next_out - out_buf;
2362 if (ret != Z_STREAM_END || out_len >= s->cluster_size) {
2363 /* could not compress: write normal cluster */
2364 ret = bdrv_write(bs, sector_num, buf, s->cluster_sectors);
2369 cluster_offset = qcow2_alloc_compressed_cluster_offset(bs,
2370 sector_num << 9, out_len);
2371 if (!cluster_offset) {
2375 cluster_offset &= s->cluster_offset_mask;
2377 ret = qcow2_pre_write_overlap_check(bs, 0, cluster_offset, out_len);
2382 BLKDBG_EVENT(bs->file, BLKDBG_WRITE_COMPRESSED);
2383 ret = bdrv_pwrite(bs->file, cluster_offset, out_buf, out_len);
2395 static int make_completely_empty(BlockDriverState *bs)
2397 BDRVQcow2State *s = bs->opaque;
2398 int ret, l1_clusters;
2400 uint64_t *new_reftable = NULL;
2401 uint64_t rt_entry, l1_size2;
2404 uint64_t reftable_offset;
2405 uint32_t reftable_clusters;
2406 } QEMU_PACKED l1_ofs_rt_ofs_cls;
2408 ret = qcow2_cache_empty(bs, s->l2_table_cache);
2413 ret = qcow2_cache_empty(bs, s->refcount_block_cache);
2418 /* Refcounts will be broken utterly */
2419 ret = qcow2_mark_dirty(bs);
2424 BLKDBG_EVENT(bs->file, BLKDBG_L1_UPDATE);
2426 l1_clusters = DIV_ROUND_UP(s->l1_size, s->cluster_size / sizeof(uint64_t));
2427 l1_size2 = (uint64_t)s->l1_size * sizeof(uint64_t);
2429 /* After this call, neither the in-memory nor the on-disk refcount
2430 * information accurately describe the actual references */
2432 ret = bdrv_write_zeroes(bs->file, s->l1_table_offset / BDRV_SECTOR_SIZE,
2433 l1_clusters * s->cluster_sectors, 0);
2435 goto fail_broken_refcounts;
2437 memset(s->l1_table, 0, l1_size2);
2439 BLKDBG_EVENT(bs->file, BLKDBG_EMPTY_IMAGE_PREPARE);
2441 /* Overwrite enough clusters at the beginning of the sectors to place
2442 * the refcount table, a refcount block and the L1 table in; this may
2443 * overwrite parts of the existing refcount and L1 table, which is not
2444 * an issue because the dirty flag is set, complete data loss is in fact
2445 * desired and partial data loss is consequently fine as well */
2446 ret = bdrv_write_zeroes(bs->file, s->cluster_size / BDRV_SECTOR_SIZE,
2447 (2 + l1_clusters) * s->cluster_size /
2448 BDRV_SECTOR_SIZE, 0);
2449 /* This call (even if it failed overall) may have overwritten on-disk
2450 * refcount structures; in that case, the in-memory refcount information
2451 * will probably differ from the on-disk information which makes the BDS
2454 goto fail_broken_refcounts;
2457 BLKDBG_EVENT(bs->file, BLKDBG_L1_UPDATE);
2458 BLKDBG_EVENT(bs->file, BLKDBG_REFTABLE_UPDATE);
2460 /* "Create" an empty reftable (one cluster) directly after the image
2461 * header and an empty L1 table three clusters after the image header;
2462 * the cluster between those two will be used as the first refblock */
2463 cpu_to_be64w(&l1_ofs_rt_ofs_cls.l1_offset, 3 * s->cluster_size);
2464 cpu_to_be64w(&l1_ofs_rt_ofs_cls.reftable_offset, s->cluster_size);
2465 cpu_to_be32w(&l1_ofs_rt_ofs_cls.reftable_clusters, 1);
2466 ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, l1_table_offset),
2467 &l1_ofs_rt_ofs_cls, sizeof(l1_ofs_rt_ofs_cls));
2469 goto fail_broken_refcounts;
2472 s->l1_table_offset = 3 * s->cluster_size;
2474 new_reftable = g_try_new0(uint64_t, s->cluster_size / sizeof(uint64_t));
2475 if (!new_reftable) {
2477 goto fail_broken_refcounts;
2480 s->refcount_table_offset = s->cluster_size;
2481 s->refcount_table_size = s->cluster_size / sizeof(uint64_t);
2483 g_free(s->refcount_table);
2484 s->refcount_table = new_reftable;
2485 new_reftable = NULL;
2487 /* Now the in-memory refcount information again corresponds to the on-disk
2488 * information (reftable is empty and no refblocks (the refblock cache is
2489 * empty)); however, this means some clusters (e.g. the image header) are
2490 * referenced, but not refcounted, but the normal qcow2 code assumes that
2491 * the in-memory information is always correct */
2493 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC);
2495 /* Enter the first refblock into the reftable */
2496 rt_entry = cpu_to_be64(2 * s->cluster_size);
2497 ret = bdrv_pwrite_sync(bs->file, s->cluster_size,
2498 &rt_entry, sizeof(rt_entry));
2500 goto fail_broken_refcounts;
2502 s->refcount_table[0] = 2 * s->cluster_size;
2504 s->free_cluster_index = 0;
2505 assert(3 + l1_clusters <= s->refcount_block_size);
2506 offset = qcow2_alloc_clusters(bs, 3 * s->cluster_size + l1_size2);
2509 goto fail_broken_refcounts;
2510 } else if (offset > 0) {
2511 error_report("First cluster in emptied image is in use");
2515 /* Now finally the in-memory information corresponds to the on-disk
2516 * structures and is correct */
2517 ret = qcow2_mark_clean(bs);
2522 ret = bdrv_truncate(bs->file, (3 + l1_clusters) * s->cluster_size);
2529 fail_broken_refcounts:
2530 /* The BDS is unusable at this point. If we wanted to make it usable, we
2531 * would have to call qcow2_refcount_close(), qcow2_refcount_init(),
2532 * qcow2_check_refcounts(), qcow2_refcount_close() and qcow2_refcount_init()
2533 * again. However, because the functions which could have caused this error
2534 * path to be taken are used by those functions as well, it's very likely
2535 * that that sequence will fail as well. Therefore, just eject the BDS. */
2539 g_free(new_reftable);
2543 static int qcow2_make_empty(BlockDriverState *bs)
2545 BDRVQcow2State *s = bs->opaque;
2546 uint64_t start_sector;
2547 int sector_step = INT_MAX / BDRV_SECTOR_SIZE;
2548 int l1_clusters, ret = 0;
2550 l1_clusters = DIV_ROUND_UP(s->l1_size, s->cluster_size / sizeof(uint64_t));
2552 if (s->qcow_version >= 3 && !s->snapshots &&
2553 3 + l1_clusters <= s->refcount_block_size) {
2554 /* The following function only works for qcow2 v3 images (it requires
2555 * the dirty flag) and only as long as there are no snapshots (because
2556 * it completely empties the image). Furthermore, the L1 table and three
2557 * additional clusters (image header, refcount table, one refcount
2558 * block) have to fit inside one refcount block. */
2559 return make_completely_empty(bs);
2562 /* This fallback code simply discards every active cluster; this is slow,
2563 * but works in all cases */
2564 for (start_sector = 0; start_sector < bs->total_sectors;
2565 start_sector += sector_step)
2567 /* As this function is generally used after committing an external
2568 * snapshot, QCOW2_DISCARD_SNAPSHOT seems appropriate. Also, the
2569 * default action for this kind of discard is to pass the discard,
2570 * which will ideally result in an actually smaller image file, as
2571 * is probably desired. */
2572 ret = qcow2_discard_clusters(bs, start_sector * BDRV_SECTOR_SIZE,
2574 bs->total_sectors - start_sector),
2575 QCOW2_DISCARD_SNAPSHOT, true);
2584 static coroutine_fn int qcow2_co_flush_to_os(BlockDriverState *bs)
2586 BDRVQcow2State *s = bs->opaque;
2589 qemu_co_mutex_lock(&s->lock);
2590 ret = qcow2_cache_flush(bs, s->l2_table_cache);
2592 qemu_co_mutex_unlock(&s->lock);
2596 if (qcow2_need_accurate_refcounts(s)) {
2597 ret = qcow2_cache_flush(bs, s->refcount_block_cache);
2599 qemu_co_mutex_unlock(&s->lock);
2603 qemu_co_mutex_unlock(&s->lock);
2608 static int qcow2_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
2610 BDRVQcow2State *s = bs->opaque;
2611 bdi->unallocated_blocks_are_zero = true;
2612 bdi->can_write_zeroes_with_unmap = (s->qcow_version >= 3);
2613 bdi->cluster_size = s->cluster_size;
2614 bdi->vm_state_offset = qcow2_vm_state_offset(s);
2618 static ImageInfoSpecific *qcow2_get_specific_info(BlockDriverState *bs)
2620 BDRVQcow2State *s = bs->opaque;
2621 ImageInfoSpecific *spec_info = g_new(ImageInfoSpecific, 1);
2623 *spec_info = (ImageInfoSpecific){
2624 .kind = IMAGE_INFO_SPECIFIC_KIND_QCOW2,
2626 .qcow2 = g_new(ImageInfoSpecificQCow2, 1),
2629 if (s->qcow_version == 2) {
2630 *spec_info->qcow2 = (ImageInfoSpecificQCow2){
2631 .compat = g_strdup("0.10"),
2632 .refcount_bits = s->refcount_bits,
2634 } else if (s->qcow_version == 3) {
2635 *spec_info->qcow2 = (ImageInfoSpecificQCow2){
2636 .compat = g_strdup("1.1"),
2637 .lazy_refcounts = s->compatible_features &
2638 QCOW2_COMPAT_LAZY_REFCOUNTS,
2639 .has_lazy_refcounts = true,
2640 .corrupt = s->incompatible_features &
2641 QCOW2_INCOMPAT_CORRUPT,
2642 .has_corrupt = true,
2643 .refcount_bits = s->refcount_bits,
2651 static void dump_refcounts(BlockDriverState *bs)
2653 BDRVQcow2State *s = bs->opaque;
2654 int64_t nb_clusters, k, k1, size;
2657 size = bdrv_getlength(bs->file);
2658 nb_clusters = size_to_clusters(s, size);
2659 for(k = 0; k < nb_clusters;) {
2661 refcount = get_refcount(bs, k);
2663 while (k < nb_clusters && get_refcount(bs, k) == refcount)
2665 printf("%" PRId64 ": refcount=%d nb=%" PRId64 "\n", k, refcount,
2671 static int qcow2_save_vmstate(BlockDriverState *bs, QEMUIOVector *qiov,
2674 BDRVQcow2State *s = bs->opaque;
2675 int64_t total_sectors = bs->total_sectors;
2676 bool zero_beyond_eof = bs->zero_beyond_eof;
2679 BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_SAVE);
2680 bs->zero_beyond_eof = false;
2681 ret = bdrv_pwritev(bs, qcow2_vm_state_offset(s) + pos, qiov);
2682 bs->zero_beyond_eof = zero_beyond_eof;
2684 /* bdrv_co_do_writev will have increased the total_sectors value to include
2685 * the VM state - the VM state is however not an actual part of the block
2686 * device, therefore, we need to restore the old value. */
2687 bs->total_sectors = total_sectors;
2692 static int qcow2_load_vmstate(BlockDriverState *bs, uint8_t *buf,
2693 int64_t pos, int size)
2695 BDRVQcow2State *s = bs->opaque;
2696 bool zero_beyond_eof = bs->zero_beyond_eof;
2699 BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_LOAD);
2700 bs->zero_beyond_eof = false;
2701 ret = bdrv_pread(bs, qcow2_vm_state_offset(s) + pos, buf, size);
2702 bs->zero_beyond_eof = zero_beyond_eof;
2708 * Downgrades an image's version. To achieve this, any incompatible features
2709 * have to be removed.
2711 static int qcow2_downgrade(BlockDriverState *bs, int target_version,
2712 BlockDriverAmendStatusCB *status_cb)
2714 BDRVQcow2State *s = bs->opaque;
2715 int current_version = s->qcow_version;
2718 if (target_version == current_version) {
2720 } else if (target_version > current_version) {
2722 } else if (target_version != 2) {
2726 if (s->refcount_order != 4) {
2727 /* we would have to convert the image to a refcount_order == 4 image
2728 * here; however, since qemu (at the time of writing this) does not
2729 * support anything different than 4 anyway, there is no point in doing
2730 * so right now; however, we should error out (if qemu supports this in
2731 * the future and this code has not been adapted) */
2732 error_report("qcow2_downgrade: Image refcount orders other than 4 are "
2733 "currently not supported.");
2737 /* clear incompatible features */
2738 if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) {
2739 ret = qcow2_mark_clean(bs);
2745 /* with QCOW2_INCOMPAT_CORRUPT, it is pretty much impossible to get here in
2746 * the first place; if that happens nonetheless, returning -ENOTSUP is the
2747 * best thing to do anyway */
2749 if (s->incompatible_features) {
2753 /* since we can ignore compatible features, we can set them to 0 as well */
2754 s->compatible_features = 0;
2755 /* if lazy refcounts have been used, they have already been fixed through
2756 * clearing the dirty flag */
2758 /* clearing autoclear features is trivial */
2759 s->autoclear_features = 0;
2761 ret = qcow2_expand_zero_clusters(bs, status_cb);
2766 s->qcow_version = target_version;
2767 ret = qcow2_update_header(bs);
2769 s->qcow_version = current_version;
2775 static int qcow2_amend_options(BlockDriverState *bs, QemuOpts *opts,
2776 BlockDriverAmendStatusCB *status_cb)
2778 BDRVQcow2State *s = bs->opaque;
2779 int old_version = s->qcow_version, new_version = old_version;
2780 uint64_t new_size = 0;
2781 const char *backing_file = NULL, *backing_format = NULL;
2782 bool lazy_refcounts = s->use_lazy_refcounts;
2783 const char *compat = NULL;
2784 uint64_t cluster_size = s->cluster_size;
2787 QemuOptDesc *desc = opts->list->desc;
2789 while (desc && desc->name) {
2790 if (!qemu_opt_find(opts, desc->name)) {
2791 /* only change explicitly defined options */
2796 if (!strcmp(desc->name, BLOCK_OPT_COMPAT_LEVEL)) {
2797 compat = qemu_opt_get(opts, BLOCK_OPT_COMPAT_LEVEL);
2799 /* preserve default */
2800 } else if (!strcmp(compat, "0.10")) {
2802 } else if (!strcmp(compat, "1.1")) {
2805 fprintf(stderr, "Unknown compatibility level %s.\n", compat);
2808 } else if (!strcmp(desc->name, BLOCK_OPT_PREALLOC)) {
2809 fprintf(stderr, "Cannot change preallocation mode.\n");
2811 } else if (!strcmp(desc->name, BLOCK_OPT_SIZE)) {
2812 new_size = qemu_opt_get_size(opts, BLOCK_OPT_SIZE, 0);
2813 } else if (!strcmp(desc->name, BLOCK_OPT_BACKING_FILE)) {
2814 backing_file = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
2815 } else if (!strcmp(desc->name, BLOCK_OPT_BACKING_FMT)) {
2816 backing_format = qemu_opt_get(opts, BLOCK_OPT_BACKING_FMT);
2817 } else if (!strcmp(desc->name, BLOCK_OPT_ENCRYPT)) {
2818 encrypt = qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT,
2821 if (encrypt != !!s->cipher) {
2822 fprintf(stderr, "Changing the encryption flag is not "
2826 } else if (!strcmp(desc->name, BLOCK_OPT_CLUSTER_SIZE)) {
2827 cluster_size = qemu_opt_get_size(opts, BLOCK_OPT_CLUSTER_SIZE,
2829 if (cluster_size != s->cluster_size) {
2830 fprintf(stderr, "Changing the cluster size is not "
2834 } else if (!strcmp(desc->name, BLOCK_OPT_LAZY_REFCOUNTS)) {
2835 lazy_refcounts = qemu_opt_get_bool(opts, BLOCK_OPT_LAZY_REFCOUNTS,
2837 } else if (!strcmp(desc->name, BLOCK_OPT_REFCOUNT_BITS)) {
2838 error_report("Cannot change refcount entry width");
2841 /* if this assertion fails, this probably means a new option was
2842 * added without having it covered here */
2849 if (new_version != old_version) {
2850 if (new_version > old_version) {
2852 s->qcow_version = new_version;
2853 ret = qcow2_update_header(bs);
2855 s->qcow_version = old_version;
2859 ret = qcow2_downgrade(bs, new_version, status_cb);
2866 if (backing_file || backing_format) {
2867 ret = qcow2_change_backing_file(bs,
2868 backing_file ?: s->image_backing_file,
2869 backing_format ?: s->image_backing_format);
2875 if (s->use_lazy_refcounts != lazy_refcounts) {
2876 if (lazy_refcounts) {
2877 if (s->qcow_version < 3) {
2878 fprintf(stderr, "Lazy refcounts only supported with compatibility "
2879 "level 1.1 and above (use compat=1.1 or greater)\n");
2882 s->compatible_features |= QCOW2_COMPAT_LAZY_REFCOUNTS;
2883 ret = qcow2_update_header(bs);
2885 s->compatible_features &= ~QCOW2_COMPAT_LAZY_REFCOUNTS;
2888 s->use_lazy_refcounts = true;
2890 /* make image clean first */
2891 ret = qcow2_mark_clean(bs);
2895 /* now disallow lazy refcounts */
2896 s->compatible_features &= ~QCOW2_COMPAT_LAZY_REFCOUNTS;
2897 ret = qcow2_update_header(bs);
2899 s->compatible_features |= QCOW2_COMPAT_LAZY_REFCOUNTS;
2902 s->use_lazy_refcounts = false;
2907 ret = bdrv_truncate(bs, new_size);
2917 * If offset or size are negative, respectively, they will not be included in
2918 * the BLOCK_IMAGE_CORRUPTED event emitted.
2919 * fatal will be ignored for read-only BDS; corruptions found there will always
2920 * be considered non-fatal.
2922 void qcow2_signal_corruption(BlockDriverState *bs, bool fatal, int64_t offset,
2923 int64_t size, const char *message_format, ...)
2925 BDRVQcow2State *s = bs->opaque;
2926 const char *node_name;
2930 fatal = fatal && !bs->read_only;
2932 if (s->signaled_corruption &&
2933 (!fatal || (s->incompatible_features & QCOW2_INCOMPAT_CORRUPT)))
2938 va_start(ap, message_format);
2939 message = g_strdup_vprintf(message_format, ap);
2943 fprintf(stderr, "qcow2: Marking image as corrupt: %s; further "
2944 "corruption events will be suppressed\n", message);
2946 fprintf(stderr, "qcow2: Image is corrupt: %s; further non-fatal "
2947 "corruption events will be suppressed\n", message);
2950 node_name = bdrv_get_node_name(bs);
2951 qapi_event_send_block_image_corrupted(bdrv_get_device_name(bs),
2952 *node_name != '\0', node_name,
2953 message, offset >= 0, offset,
2955 fatal, &error_abort);
2959 qcow2_mark_corrupt(bs);
2960 bs->drv = NULL; /* make BDS unusable */
2963 s->signaled_corruption = true;
2966 static QemuOptsList qcow2_create_opts = {
2967 .name = "qcow2-create-opts",
2968 .head = QTAILQ_HEAD_INITIALIZER(qcow2_create_opts.head),
2971 .name = BLOCK_OPT_SIZE,
2972 .type = QEMU_OPT_SIZE,
2973 .help = "Virtual disk size"
2976 .name = BLOCK_OPT_COMPAT_LEVEL,
2977 .type = QEMU_OPT_STRING,
2978 .help = "Compatibility level (0.10 or 1.1)"
2981 .name = BLOCK_OPT_BACKING_FILE,
2982 .type = QEMU_OPT_STRING,
2983 .help = "File name of a base image"
2986 .name = BLOCK_OPT_BACKING_FMT,
2987 .type = QEMU_OPT_STRING,
2988 .help = "Image format of the base image"
2991 .name = BLOCK_OPT_ENCRYPT,
2992 .type = QEMU_OPT_BOOL,
2993 .help = "Encrypt the image",
2994 .def_value_str = "off"
2997 .name = BLOCK_OPT_CLUSTER_SIZE,
2998 .type = QEMU_OPT_SIZE,
2999 .help = "qcow2 cluster size",
3000 .def_value_str = stringify(DEFAULT_CLUSTER_SIZE)
3003 .name = BLOCK_OPT_PREALLOC,
3004 .type = QEMU_OPT_STRING,
3005 .help = "Preallocation mode (allowed values: off, metadata, "
3009 .name = BLOCK_OPT_LAZY_REFCOUNTS,
3010 .type = QEMU_OPT_BOOL,
3011 .help = "Postpone refcount updates",
3012 .def_value_str = "off"
3015 .name = BLOCK_OPT_REFCOUNT_BITS,
3016 .type = QEMU_OPT_NUMBER,
3017 .help = "Width of a reference count entry in bits",
3018 .def_value_str = "16"
3020 { /* end of list */ }
3024 BlockDriver bdrv_qcow2 = {
3025 .format_name = "qcow2",
3026 .instance_size = sizeof(BDRVQcow2State),
3027 .bdrv_probe = qcow2_probe,
3028 .bdrv_open = qcow2_open,
3029 .bdrv_close = qcow2_close,
3030 .bdrv_reopen_prepare = qcow2_reopen_prepare,
3031 .bdrv_create = qcow2_create,
3032 .bdrv_has_zero_init = bdrv_has_zero_init_1,
3033 .bdrv_co_get_block_status = qcow2_co_get_block_status,
3034 .bdrv_set_key = qcow2_set_key,
3036 .bdrv_co_readv = qcow2_co_readv,
3037 .bdrv_co_writev = qcow2_co_writev,
3038 .bdrv_co_flush_to_os = qcow2_co_flush_to_os,
3040 .bdrv_co_write_zeroes = qcow2_co_write_zeroes,
3041 .bdrv_co_discard = qcow2_co_discard,
3042 .bdrv_truncate = qcow2_truncate,
3043 .bdrv_write_compressed = qcow2_write_compressed,
3044 .bdrv_make_empty = qcow2_make_empty,
3046 .bdrv_snapshot_create = qcow2_snapshot_create,
3047 .bdrv_snapshot_goto = qcow2_snapshot_goto,
3048 .bdrv_snapshot_delete = qcow2_snapshot_delete,
3049 .bdrv_snapshot_list = qcow2_snapshot_list,
3050 .bdrv_snapshot_load_tmp = qcow2_snapshot_load_tmp,
3051 .bdrv_get_info = qcow2_get_info,
3052 .bdrv_get_specific_info = qcow2_get_specific_info,
3054 .bdrv_save_vmstate = qcow2_save_vmstate,
3055 .bdrv_load_vmstate = qcow2_load_vmstate,
3057 .supports_backing = true,
3058 .bdrv_change_backing_file = qcow2_change_backing_file,
3060 .bdrv_refresh_limits = qcow2_refresh_limits,
3061 .bdrv_invalidate_cache = qcow2_invalidate_cache,
3063 .create_opts = &qcow2_create_opts,
3064 .bdrv_check = qcow2_check,
3065 .bdrv_amend_options = qcow2_amend_options,
3067 .bdrv_detach_aio_context = qcow2_detach_aio_context,
3068 .bdrv_attach_aio_context = qcow2_attach_aio_context,
3071 static void bdrv_qcow2_init(void)
3073 bdrv_register(&bdrv_qcow2);
3076 block_init(bdrv_qcow2_init);