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
25 #include "qemu/osdep.h"
30 #include "block/block_int.h"
31 #include "block/qdict.h"
32 #include "sysemu/block-backend.h"
33 #include "qemu/module.h"
35 #include "qemu/error-report.h"
36 #include "qapi/error.h"
37 #include "qapi/qapi-events-block-core.h"
38 #include "qapi/qmp/qdict.h"
39 #include "qapi/qmp/qstring.h"
41 #include "qemu/option_int.h"
42 #include "qemu/cutils.h"
43 #include "qemu/bswap.h"
44 #include "qapi/qobject-input-visitor.h"
45 #include "qapi/qapi-visit-block-core.h"
47 #include "block/thread-pool.h"
50 Differences with QCOW:
52 - Support for multiple incremental snapshots.
53 - Memory management by reference counts.
54 - Clusters which have a reference count of one have the bit
55 QCOW_OFLAG_COPIED to optimize write performance.
56 - Size of compressed clusters is stored in sectors to reduce bit usage
57 in the cluster offsets.
58 - Support for storing additional data (such as the VM state) in the
60 - If a backing store is used, the cluster size is not constrained
61 (could be backported to QCOW).
62 - L2 tables have always a size of one cluster.
69 } QEMU_PACKED QCowExtension;
71 #define QCOW2_EXT_MAGIC_END 0
72 #define QCOW2_EXT_MAGIC_BACKING_FORMAT 0xE2792ACA
73 #define QCOW2_EXT_MAGIC_FEATURE_TABLE 0x6803f857
74 #define QCOW2_EXT_MAGIC_CRYPTO_HEADER 0x0537be77
75 #define QCOW2_EXT_MAGIC_BITMAPS 0x23852875
77 static int qcow2_probe(const uint8_t *buf, int buf_size, const char *filename)
79 const QCowHeader *cow_header = (const void *)buf;
81 if (buf_size >= sizeof(QCowHeader) &&
82 be32_to_cpu(cow_header->magic) == QCOW_MAGIC &&
83 be32_to_cpu(cow_header->version) >= 2)
90 static ssize_t qcow2_crypto_hdr_read_func(QCryptoBlock *block, size_t offset,
91 uint8_t *buf, size_t buflen,
92 void *opaque, Error **errp)
94 BlockDriverState *bs = opaque;
95 BDRVQcow2State *s = bs->opaque;
98 if ((offset + buflen) > s->crypto_header.length) {
99 error_setg(errp, "Request for data outside of extension header");
103 ret = bdrv_pread(bs->file,
104 s->crypto_header.offset + offset, buf, buflen);
106 error_setg_errno(errp, -ret, "Could not read encryption header");
113 static ssize_t qcow2_crypto_hdr_init_func(QCryptoBlock *block, size_t headerlen,
114 void *opaque, Error **errp)
116 BlockDriverState *bs = opaque;
117 BDRVQcow2State *s = bs->opaque;
121 ret = qcow2_alloc_clusters(bs, headerlen);
123 error_setg_errno(errp, -ret,
124 "Cannot allocate cluster for LUKS header size %zu",
129 s->crypto_header.length = headerlen;
130 s->crypto_header.offset = ret;
132 /* Zero fill remaining space in cluster so it has predictable
133 * content in case of future spec changes */
134 clusterlen = size_to_clusters(s, headerlen) * s->cluster_size;
135 assert(qcow2_pre_write_overlap_check(bs, 0, ret, clusterlen) == 0);
136 ret = bdrv_pwrite_zeroes(bs->file,
138 clusterlen - headerlen, 0);
140 error_setg_errno(errp, -ret, "Could not zero fill encryption header");
148 static ssize_t qcow2_crypto_hdr_write_func(QCryptoBlock *block, size_t offset,
149 const uint8_t *buf, size_t buflen,
150 void *opaque, Error **errp)
152 BlockDriverState *bs = opaque;
153 BDRVQcow2State *s = bs->opaque;
156 if ((offset + buflen) > s->crypto_header.length) {
157 error_setg(errp, "Request for data outside of extension header");
161 ret = bdrv_pwrite(bs->file,
162 s->crypto_header.offset + offset, buf, buflen);
164 error_setg_errno(errp, -ret, "Could not read encryption header");
172 * read qcow2 extension and fill bs
173 * start reading from start_offset
174 * finish reading upon magic of value 0 or when end_offset reached
175 * unknown magic is skipped (future extension this version knows nothing about)
176 * return 0 upon success, non-0 otherwise
178 static int qcow2_read_extensions(BlockDriverState *bs, uint64_t start_offset,
179 uint64_t end_offset, void **p_feature_table,
180 int flags, bool *need_update_header,
183 BDRVQcow2State *s = bs->opaque;
187 Qcow2BitmapHeaderExt bitmaps_ext;
189 if (need_update_header != NULL) {
190 *need_update_header = false;
194 printf("qcow2_read_extensions: start=%ld end=%ld\n", start_offset, end_offset);
196 offset = start_offset;
197 while (offset < end_offset) {
201 if (offset > s->cluster_size)
202 printf("qcow2_read_extension: suspicious offset %lu\n", offset);
204 printf("attempting to read extended header in offset %lu\n", offset);
207 ret = bdrv_pread(bs->file, offset, &ext, sizeof(ext));
209 error_setg_errno(errp, -ret, "qcow2_read_extension: ERROR: "
210 "pread fail from offset %" PRIu64, offset);
213 be32_to_cpus(&ext.magic);
214 be32_to_cpus(&ext.len);
215 offset += sizeof(ext);
217 printf("ext.magic = 0x%x\n", ext.magic);
219 if (offset > end_offset || ext.len > end_offset - offset) {
220 error_setg(errp, "Header extension too large");
225 case QCOW2_EXT_MAGIC_END:
228 case QCOW2_EXT_MAGIC_BACKING_FORMAT:
229 if (ext.len >= sizeof(bs->backing_format)) {
230 error_setg(errp, "ERROR: ext_backing_format: len=%" PRIu32
231 " too large (>=%zu)", ext.len,
232 sizeof(bs->backing_format));
235 ret = bdrv_pread(bs->file, offset, bs->backing_format, ext.len);
237 error_setg_errno(errp, -ret, "ERROR: ext_backing_format: "
238 "Could not read format name");
241 bs->backing_format[ext.len] = '\0';
242 s->image_backing_format = g_strdup(bs->backing_format);
244 printf("Qcow2: Got format extension %s\n", bs->backing_format);
248 case QCOW2_EXT_MAGIC_FEATURE_TABLE:
249 if (p_feature_table != NULL) {
250 void* feature_table = g_malloc0(ext.len + 2 * sizeof(Qcow2Feature));
251 ret = bdrv_pread(bs->file, offset , feature_table, ext.len);
253 error_setg_errno(errp, -ret, "ERROR: ext_feature_table: "
254 "Could not read table");
258 *p_feature_table = feature_table;
262 case QCOW2_EXT_MAGIC_CRYPTO_HEADER: {
263 unsigned int cflags = 0;
264 if (s->crypt_method_header != QCOW_CRYPT_LUKS) {
265 error_setg(errp, "CRYPTO header extension only "
266 "expected with LUKS encryption method");
269 if (ext.len != sizeof(Qcow2CryptoHeaderExtension)) {
270 error_setg(errp, "CRYPTO header extension size %u, "
271 "but expected size %zu", ext.len,
272 sizeof(Qcow2CryptoHeaderExtension));
276 ret = bdrv_pread(bs->file, offset, &s->crypto_header, ext.len);
278 error_setg_errno(errp, -ret,
279 "Unable to read CRYPTO header extension");
282 be64_to_cpus(&s->crypto_header.offset);
283 be64_to_cpus(&s->crypto_header.length);
285 if ((s->crypto_header.offset % s->cluster_size) != 0) {
286 error_setg(errp, "Encryption header offset '%" PRIu64 "' is "
287 "not a multiple of cluster size '%u'",
288 s->crypto_header.offset, s->cluster_size);
292 if (flags & BDRV_O_NO_IO) {
293 cflags |= QCRYPTO_BLOCK_OPEN_NO_IO;
295 s->crypto = qcrypto_block_open(s->crypto_opts, "encrypt.",
296 qcow2_crypto_hdr_read_func,
303 case QCOW2_EXT_MAGIC_BITMAPS:
304 if (ext.len != sizeof(bitmaps_ext)) {
305 error_setg_errno(errp, -ret, "bitmaps_ext: "
306 "Invalid extension length");
310 if (!(s->autoclear_features & QCOW2_AUTOCLEAR_BITMAPS)) {
311 if (s->qcow_version < 3) {
312 /* Let's be a bit more specific */
313 warn_report("This qcow2 v2 image contains bitmaps, but "
314 "they may have been modified by a program "
315 "without persistent bitmap support; so now "
316 "they must all be considered inconsistent");
318 warn_report("a program lacking bitmap support "
319 "modified this file, so all bitmaps are now "
320 "considered inconsistent");
322 error_printf("Some clusters may be leaked, "
323 "run 'qemu-img check -r' on the image "
325 if (need_update_header != NULL) {
326 /* Updating is needed to drop invalid bitmap extension. */
327 *need_update_header = true;
332 ret = bdrv_pread(bs->file, offset, &bitmaps_ext, ext.len);
334 error_setg_errno(errp, -ret, "bitmaps_ext: "
335 "Could not read ext header");
339 if (bitmaps_ext.reserved32 != 0) {
340 error_setg_errno(errp, -ret, "bitmaps_ext: "
341 "Reserved field is not zero");
345 be32_to_cpus(&bitmaps_ext.nb_bitmaps);
346 be64_to_cpus(&bitmaps_ext.bitmap_directory_size);
347 be64_to_cpus(&bitmaps_ext.bitmap_directory_offset);
349 if (bitmaps_ext.nb_bitmaps > QCOW2_MAX_BITMAPS) {
351 "bitmaps_ext: Image has %" PRIu32 " bitmaps, "
352 "exceeding the QEMU supported maximum of %d",
353 bitmaps_ext.nb_bitmaps, QCOW2_MAX_BITMAPS);
357 if (bitmaps_ext.nb_bitmaps == 0) {
358 error_setg(errp, "found bitmaps extension with zero bitmaps");
362 if (bitmaps_ext.bitmap_directory_offset & (s->cluster_size - 1)) {
363 error_setg(errp, "bitmaps_ext: "
364 "invalid bitmap directory offset");
368 if (bitmaps_ext.bitmap_directory_size >
369 QCOW2_MAX_BITMAP_DIRECTORY_SIZE) {
370 error_setg(errp, "bitmaps_ext: "
371 "bitmap directory size (%" PRIu64 ") exceeds "
372 "the maximum supported size (%d)",
373 bitmaps_ext.bitmap_directory_size,
374 QCOW2_MAX_BITMAP_DIRECTORY_SIZE);
378 s->nb_bitmaps = bitmaps_ext.nb_bitmaps;
379 s->bitmap_directory_offset =
380 bitmaps_ext.bitmap_directory_offset;
381 s->bitmap_directory_size =
382 bitmaps_ext.bitmap_directory_size;
385 printf("Qcow2: Got bitmaps extension: "
386 "offset=%" PRIu64 " nb_bitmaps=%" PRIu32 "\n",
387 s->bitmap_directory_offset, s->nb_bitmaps);
392 /* unknown magic - save it in case we need to rewrite the header */
393 /* If you add a new feature, make sure to also update the fast
394 * path of qcow2_make_empty() to deal with it. */
396 Qcow2UnknownHeaderExtension *uext;
398 uext = g_malloc0(sizeof(*uext) + ext.len);
399 uext->magic = ext.magic;
401 QLIST_INSERT_HEAD(&s->unknown_header_ext, uext, next);
403 ret = bdrv_pread(bs->file, offset , uext->data, uext->len);
405 error_setg_errno(errp, -ret, "ERROR: unknown extension: "
406 "Could not read data");
413 offset += ((ext.len + 7) & ~7);
419 static void cleanup_unknown_header_ext(BlockDriverState *bs)
421 BDRVQcow2State *s = bs->opaque;
422 Qcow2UnknownHeaderExtension *uext, *next;
424 QLIST_FOREACH_SAFE(uext, &s->unknown_header_ext, next, next) {
425 QLIST_REMOVE(uext, next);
430 static void report_unsupported_feature(Error **errp, Qcow2Feature *table,
433 char *features = g_strdup("");
436 while (table && table->name[0] != '\0') {
437 if (table->type == QCOW2_FEAT_TYPE_INCOMPATIBLE) {
438 if (mask & (1ULL << table->bit)) {
440 features = g_strdup_printf("%s%s%.46s", old, *old ? ", " : "",
443 mask &= ~(1ULL << table->bit);
451 features = g_strdup_printf("%s%sUnknown incompatible feature: %" PRIx64,
452 old, *old ? ", " : "", mask);
456 error_setg(errp, "Unsupported qcow2 feature(s): %s", features);
461 * Sets the dirty bit and flushes afterwards if necessary.
463 * The incompatible_features bit is only set if the image file header was
464 * updated successfully. Therefore it is not required to check the return
465 * value of this function.
467 int qcow2_mark_dirty(BlockDriverState *bs)
469 BDRVQcow2State *s = bs->opaque;
473 assert(s->qcow_version >= 3);
475 if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) {
476 return 0; /* already dirty */
479 val = cpu_to_be64(s->incompatible_features | QCOW2_INCOMPAT_DIRTY);
480 ret = bdrv_pwrite(bs->file, offsetof(QCowHeader, incompatible_features),
485 ret = bdrv_flush(bs->file->bs);
490 /* Only treat image as dirty if the header was updated successfully */
491 s->incompatible_features |= QCOW2_INCOMPAT_DIRTY;
496 * Clears the dirty bit and flushes before if necessary. Only call this
497 * function when there are no pending requests, it does not guard against
498 * concurrent requests dirtying the image.
500 static int qcow2_mark_clean(BlockDriverState *bs)
502 BDRVQcow2State *s = bs->opaque;
504 if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) {
507 s->incompatible_features &= ~QCOW2_INCOMPAT_DIRTY;
509 ret = qcow2_flush_caches(bs);
514 return qcow2_update_header(bs);
520 * Marks the image as corrupt.
522 int qcow2_mark_corrupt(BlockDriverState *bs)
524 BDRVQcow2State *s = bs->opaque;
526 s->incompatible_features |= QCOW2_INCOMPAT_CORRUPT;
527 return qcow2_update_header(bs);
531 * Marks the image as consistent, i.e., unsets the corrupt bit, and flushes
532 * before if necessary.
534 int qcow2_mark_consistent(BlockDriverState *bs)
536 BDRVQcow2State *s = bs->opaque;
538 if (s->incompatible_features & QCOW2_INCOMPAT_CORRUPT) {
539 int ret = qcow2_flush_caches(bs);
544 s->incompatible_features &= ~QCOW2_INCOMPAT_CORRUPT;
545 return qcow2_update_header(bs);
550 static int coroutine_fn qcow2_co_check_locked(BlockDriverState *bs,
551 BdrvCheckResult *result,
554 int ret = qcow2_check_refcounts(bs, result, fix);
559 if (fix && result->check_errors == 0 && result->corruptions == 0) {
560 ret = qcow2_mark_clean(bs);
564 return qcow2_mark_consistent(bs);
569 static int coroutine_fn qcow2_co_check(BlockDriverState *bs,
570 BdrvCheckResult *result,
573 BDRVQcow2State *s = bs->opaque;
576 qemu_co_mutex_lock(&s->lock);
577 ret = qcow2_co_check_locked(bs, result, fix);
578 qemu_co_mutex_unlock(&s->lock);
582 int qcow2_validate_table(BlockDriverState *bs, uint64_t offset,
583 uint64_t entries, size_t entry_len,
584 int64_t max_size_bytes, const char *table_name,
587 BDRVQcow2State *s = bs->opaque;
589 if (entries > max_size_bytes / entry_len) {
590 error_setg(errp, "%s too large", table_name);
594 /* Use signed INT64_MAX as the maximum even for uint64_t header fields,
595 * because values will be passed to qemu functions taking int64_t. */
596 if ((INT64_MAX - entries * entry_len < offset) ||
597 (offset_into_cluster(s, offset) != 0)) {
598 error_setg(errp, "%s offset invalid", table_name);
605 static QemuOptsList qcow2_runtime_opts = {
607 .head = QTAILQ_HEAD_INITIALIZER(qcow2_runtime_opts.head),
610 .name = QCOW2_OPT_LAZY_REFCOUNTS,
611 .type = QEMU_OPT_BOOL,
612 .help = "Postpone refcount updates",
615 .name = QCOW2_OPT_DISCARD_REQUEST,
616 .type = QEMU_OPT_BOOL,
617 .help = "Pass guest discard requests to the layer below",
620 .name = QCOW2_OPT_DISCARD_SNAPSHOT,
621 .type = QEMU_OPT_BOOL,
622 .help = "Generate discard requests when snapshot related space "
626 .name = QCOW2_OPT_DISCARD_OTHER,
627 .type = QEMU_OPT_BOOL,
628 .help = "Generate discard requests when other clusters are freed",
631 .name = QCOW2_OPT_OVERLAP,
632 .type = QEMU_OPT_STRING,
633 .help = "Selects which overlap checks to perform from a range of "
634 "templates (none, constant, cached, all)",
637 .name = QCOW2_OPT_OVERLAP_TEMPLATE,
638 .type = QEMU_OPT_STRING,
639 .help = "Selects which overlap checks to perform from a range of "
640 "templates (none, constant, cached, all)",
643 .name = QCOW2_OPT_OVERLAP_MAIN_HEADER,
644 .type = QEMU_OPT_BOOL,
645 .help = "Check for unintended writes into the main qcow2 header",
648 .name = QCOW2_OPT_OVERLAP_ACTIVE_L1,
649 .type = QEMU_OPT_BOOL,
650 .help = "Check for unintended writes into the active L1 table",
653 .name = QCOW2_OPT_OVERLAP_ACTIVE_L2,
654 .type = QEMU_OPT_BOOL,
655 .help = "Check for unintended writes into an active L2 table",
658 .name = QCOW2_OPT_OVERLAP_REFCOUNT_TABLE,
659 .type = QEMU_OPT_BOOL,
660 .help = "Check for unintended writes into the refcount table",
663 .name = QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK,
664 .type = QEMU_OPT_BOOL,
665 .help = "Check for unintended writes into a refcount block",
668 .name = QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE,
669 .type = QEMU_OPT_BOOL,
670 .help = "Check for unintended writes into the snapshot table",
673 .name = QCOW2_OPT_OVERLAP_INACTIVE_L1,
674 .type = QEMU_OPT_BOOL,
675 .help = "Check for unintended writes into an inactive L1 table",
678 .name = QCOW2_OPT_OVERLAP_INACTIVE_L2,
679 .type = QEMU_OPT_BOOL,
680 .help = "Check for unintended writes into an inactive L2 table",
683 .name = QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY,
684 .type = QEMU_OPT_BOOL,
685 .help = "Check for unintended writes into the bitmap directory",
688 .name = QCOW2_OPT_CACHE_SIZE,
689 .type = QEMU_OPT_SIZE,
690 .help = "Maximum combined metadata (L2 tables and refcount blocks) "
694 .name = QCOW2_OPT_L2_CACHE_SIZE,
695 .type = QEMU_OPT_SIZE,
696 .help = "Maximum L2 table cache size",
699 .name = QCOW2_OPT_L2_CACHE_ENTRY_SIZE,
700 .type = QEMU_OPT_SIZE,
701 .help = "Size of each entry in the L2 cache",
704 .name = QCOW2_OPT_REFCOUNT_CACHE_SIZE,
705 .type = QEMU_OPT_SIZE,
706 .help = "Maximum refcount block cache size",
709 .name = QCOW2_OPT_CACHE_CLEAN_INTERVAL,
710 .type = QEMU_OPT_NUMBER,
711 .help = "Clean unused cache entries after this time (in seconds)",
713 BLOCK_CRYPTO_OPT_DEF_KEY_SECRET("encrypt.",
714 "ID of secret providing qcow2 AES key or LUKS passphrase"),
715 { /* end of list */ }
719 static const char *overlap_bool_option_names[QCOW2_OL_MAX_BITNR] = {
720 [QCOW2_OL_MAIN_HEADER_BITNR] = QCOW2_OPT_OVERLAP_MAIN_HEADER,
721 [QCOW2_OL_ACTIVE_L1_BITNR] = QCOW2_OPT_OVERLAP_ACTIVE_L1,
722 [QCOW2_OL_ACTIVE_L2_BITNR] = QCOW2_OPT_OVERLAP_ACTIVE_L2,
723 [QCOW2_OL_REFCOUNT_TABLE_BITNR] = QCOW2_OPT_OVERLAP_REFCOUNT_TABLE,
724 [QCOW2_OL_REFCOUNT_BLOCK_BITNR] = QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK,
725 [QCOW2_OL_SNAPSHOT_TABLE_BITNR] = QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE,
726 [QCOW2_OL_INACTIVE_L1_BITNR] = QCOW2_OPT_OVERLAP_INACTIVE_L1,
727 [QCOW2_OL_INACTIVE_L2_BITNR] = QCOW2_OPT_OVERLAP_INACTIVE_L2,
728 [QCOW2_OL_BITMAP_DIRECTORY_BITNR] = QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY,
731 static void cache_clean_timer_cb(void *opaque)
733 BlockDriverState *bs = opaque;
734 BDRVQcow2State *s = bs->opaque;
735 qcow2_cache_clean_unused(s->l2_table_cache);
736 qcow2_cache_clean_unused(s->refcount_block_cache);
737 timer_mod(s->cache_clean_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
738 (int64_t) s->cache_clean_interval * 1000);
741 static void cache_clean_timer_init(BlockDriverState *bs, AioContext *context)
743 BDRVQcow2State *s = bs->opaque;
744 if (s->cache_clean_interval > 0) {
745 s->cache_clean_timer = aio_timer_new(context, QEMU_CLOCK_VIRTUAL,
746 SCALE_MS, cache_clean_timer_cb,
748 timer_mod(s->cache_clean_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
749 (int64_t) s->cache_clean_interval * 1000);
753 static void cache_clean_timer_del(BlockDriverState *bs)
755 BDRVQcow2State *s = bs->opaque;
756 if (s->cache_clean_timer) {
757 timer_del(s->cache_clean_timer);
758 timer_free(s->cache_clean_timer);
759 s->cache_clean_timer = NULL;
763 static void qcow2_detach_aio_context(BlockDriverState *bs)
765 cache_clean_timer_del(bs);
768 static void qcow2_attach_aio_context(BlockDriverState *bs,
769 AioContext *new_context)
771 cache_clean_timer_init(bs, new_context);
774 static void read_cache_sizes(BlockDriverState *bs, QemuOpts *opts,
775 uint64_t *l2_cache_size,
776 uint64_t *l2_cache_entry_size,
777 uint64_t *refcount_cache_size, Error **errp)
779 BDRVQcow2State *s = bs->opaque;
780 uint64_t combined_cache_size, l2_cache_max_setting;
781 bool l2_cache_size_set, refcount_cache_size_set, combined_cache_size_set;
782 int min_refcount_cache = MIN_REFCOUNT_CACHE_SIZE * s->cluster_size;
783 uint64_t virtual_disk_size = bs->total_sectors * BDRV_SECTOR_SIZE;
784 uint64_t max_l2_cache = virtual_disk_size / (s->cluster_size / 8);
786 combined_cache_size_set = qemu_opt_get(opts, QCOW2_OPT_CACHE_SIZE);
787 l2_cache_size_set = qemu_opt_get(opts, QCOW2_OPT_L2_CACHE_SIZE);
788 refcount_cache_size_set = qemu_opt_get(opts, QCOW2_OPT_REFCOUNT_CACHE_SIZE);
790 combined_cache_size = qemu_opt_get_size(opts, QCOW2_OPT_CACHE_SIZE, 0);
791 l2_cache_max_setting = qemu_opt_get_size(opts, QCOW2_OPT_L2_CACHE_SIZE,
792 DEFAULT_L2_CACHE_MAX_SIZE);
793 *refcount_cache_size = qemu_opt_get_size(opts,
794 QCOW2_OPT_REFCOUNT_CACHE_SIZE, 0);
796 *l2_cache_entry_size = qemu_opt_get_size(
797 opts, QCOW2_OPT_L2_CACHE_ENTRY_SIZE, s->cluster_size);
799 *l2_cache_size = MIN(max_l2_cache, l2_cache_max_setting);
801 if (combined_cache_size_set) {
802 if (l2_cache_size_set && refcount_cache_size_set) {
803 error_setg(errp, QCOW2_OPT_CACHE_SIZE ", " QCOW2_OPT_L2_CACHE_SIZE
804 " and " QCOW2_OPT_REFCOUNT_CACHE_SIZE " may not be set "
807 } else if (l2_cache_size_set &&
808 (l2_cache_max_setting > combined_cache_size)) {
809 error_setg(errp, QCOW2_OPT_L2_CACHE_SIZE " may not exceed "
810 QCOW2_OPT_CACHE_SIZE);
812 } else if (*refcount_cache_size > combined_cache_size) {
813 error_setg(errp, QCOW2_OPT_REFCOUNT_CACHE_SIZE " may not exceed "
814 QCOW2_OPT_CACHE_SIZE);
818 if (l2_cache_size_set) {
819 *refcount_cache_size = combined_cache_size - *l2_cache_size;
820 } else if (refcount_cache_size_set) {
821 *l2_cache_size = combined_cache_size - *refcount_cache_size;
823 /* Assign as much memory as possible to the L2 cache, and
824 * use the remainder for the refcount cache */
825 if (combined_cache_size >= max_l2_cache + min_refcount_cache) {
826 *l2_cache_size = max_l2_cache;
827 *refcount_cache_size = combined_cache_size - *l2_cache_size;
829 *refcount_cache_size =
830 MIN(combined_cache_size, min_refcount_cache);
831 *l2_cache_size = combined_cache_size - *refcount_cache_size;
835 /* l2_cache_size and refcount_cache_size are ensured to have at least
836 * their minimum values in qcow2_update_options_prepare() */
838 if (*l2_cache_entry_size < (1 << MIN_CLUSTER_BITS) ||
839 *l2_cache_entry_size > s->cluster_size ||
840 !is_power_of_2(*l2_cache_entry_size)) {
841 error_setg(errp, "L2 cache entry size must be a power of two "
842 "between %d and the cluster size (%d)",
843 1 << MIN_CLUSTER_BITS, s->cluster_size);
848 typedef struct Qcow2ReopenState {
849 Qcow2Cache *l2_table_cache;
850 Qcow2Cache *refcount_block_cache;
851 int l2_slice_size; /* Number of entries in a slice of the L2 table */
852 bool use_lazy_refcounts;
854 bool discard_passthrough[QCOW2_DISCARD_MAX];
855 uint64_t cache_clean_interval;
856 QCryptoBlockOpenOptions *crypto_opts; /* Disk encryption runtime options */
859 static int qcow2_update_options_prepare(BlockDriverState *bs,
861 QDict *options, int flags,
864 BDRVQcow2State *s = bs->opaque;
865 QemuOpts *opts = NULL;
866 const char *opt_overlap_check, *opt_overlap_check_template;
867 int overlap_check_template = 0;
868 uint64_t l2_cache_size, l2_cache_entry_size, refcount_cache_size;
870 const char *encryptfmt;
871 QDict *encryptopts = NULL;
872 Error *local_err = NULL;
875 qdict_extract_subqdict(options, &encryptopts, "encrypt.");
876 encryptfmt = qdict_get_try_str(encryptopts, "format");
878 opts = qemu_opts_create(&qcow2_runtime_opts, NULL, 0, &error_abort);
879 qemu_opts_absorb_qdict(opts, options, &local_err);
881 error_propagate(errp, local_err);
886 /* get L2 table/refcount block cache size from command line options */
887 read_cache_sizes(bs, opts, &l2_cache_size, &l2_cache_entry_size,
888 &refcount_cache_size, &local_err);
890 error_propagate(errp, local_err);
895 l2_cache_size /= l2_cache_entry_size;
896 if (l2_cache_size < MIN_L2_CACHE_SIZE) {
897 l2_cache_size = MIN_L2_CACHE_SIZE;
899 if (l2_cache_size > INT_MAX) {
900 error_setg(errp, "L2 cache size too big");
905 refcount_cache_size /= s->cluster_size;
906 if (refcount_cache_size < MIN_REFCOUNT_CACHE_SIZE) {
907 refcount_cache_size = MIN_REFCOUNT_CACHE_SIZE;
909 if (refcount_cache_size > INT_MAX) {
910 error_setg(errp, "Refcount cache size too big");
915 /* alloc new L2 table/refcount block cache, flush old one */
916 if (s->l2_table_cache) {
917 ret = qcow2_cache_flush(bs, s->l2_table_cache);
919 error_setg_errno(errp, -ret, "Failed to flush the L2 table cache");
924 if (s->refcount_block_cache) {
925 ret = qcow2_cache_flush(bs, s->refcount_block_cache);
927 error_setg_errno(errp, -ret,
928 "Failed to flush the refcount block cache");
933 r->l2_slice_size = l2_cache_entry_size / sizeof(uint64_t);
934 r->l2_table_cache = qcow2_cache_create(bs, l2_cache_size,
935 l2_cache_entry_size);
936 r->refcount_block_cache = qcow2_cache_create(bs, refcount_cache_size,
938 if (r->l2_table_cache == NULL || r->refcount_block_cache == NULL) {
939 error_setg(errp, "Could not allocate metadata caches");
944 /* New interval for cache cleanup timer */
945 r->cache_clean_interval =
946 qemu_opt_get_number(opts, QCOW2_OPT_CACHE_CLEAN_INTERVAL,
947 s->cache_clean_interval);
949 if (r->cache_clean_interval != 0) {
950 error_setg(errp, QCOW2_OPT_CACHE_CLEAN_INTERVAL
951 " not supported on this host");
956 if (r->cache_clean_interval > UINT_MAX) {
957 error_setg(errp, "Cache clean interval too big");
962 /* lazy-refcounts; flush if going from enabled to disabled */
963 r->use_lazy_refcounts = qemu_opt_get_bool(opts, QCOW2_OPT_LAZY_REFCOUNTS,
964 (s->compatible_features & QCOW2_COMPAT_LAZY_REFCOUNTS));
965 if (r->use_lazy_refcounts && s->qcow_version < 3) {
966 error_setg(errp, "Lazy refcounts require a qcow2 image with at least "
967 "qemu 1.1 compatibility level");
972 if (s->use_lazy_refcounts && !r->use_lazy_refcounts) {
973 ret = qcow2_mark_clean(bs);
975 error_setg_errno(errp, -ret, "Failed to disable lazy refcounts");
980 /* Overlap check options */
981 opt_overlap_check = qemu_opt_get(opts, QCOW2_OPT_OVERLAP);
982 opt_overlap_check_template = qemu_opt_get(opts, QCOW2_OPT_OVERLAP_TEMPLATE);
983 if (opt_overlap_check_template && opt_overlap_check &&
984 strcmp(opt_overlap_check_template, opt_overlap_check))
986 error_setg(errp, "Conflicting values for qcow2 options '"
987 QCOW2_OPT_OVERLAP "' ('%s') and '" QCOW2_OPT_OVERLAP_TEMPLATE
988 "' ('%s')", opt_overlap_check, opt_overlap_check_template);
992 if (!opt_overlap_check) {
993 opt_overlap_check = opt_overlap_check_template ?: "cached";
996 if (!strcmp(opt_overlap_check, "none")) {
997 overlap_check_template = 0;
998 } else if (!strcmp(opt_overlap_check, "constant")) {
999 overlap_check_template = QCOW2_OL_CONSTANT;
1000 } else if (!strcmp(opt_overlap_check, "cached")) {
1001 overlap_check_template = QCOW2_OL_CACHED;
1002 } else if (!strcmp(opt_overlap_check, "all")) {
1003 overlap_check_template = QCOW2_OL_ALL;
1005 error_setg(errp, "Unsupported value '%s' for qcow2 option "
1006 "'overlap-check'. Allowed are any of the following: "
1007 "none, constant, cached, all", opt_overlap_check);
1012 r->overlap_check = 0;
1013 for (i = 0; i < QCOW2_OL_MAX_BITNR; i++) {
1014 /* overlap-check defines a template bitmask, but every flag may be
1015 * overwritten through the associated boolean option */
1017 qemu_opt_get_bool(opts, overlap_bool_option_names[i],
1018 overlap_check_template & (1 << i)) << i;
1021 r->discard_passthrough[QCOW2_DISCARD_NEVER] = false;
1022 r->discard_passthrough[QCOW2_DISCARD_ALWAYS] = true;
1023 r->discard_passthrough[QCOW2_DISCARD_REQUEST] =
1024 qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_REQUEST,
1025 flags & BDRV_O_UNMAP);
1026 r->discard_passthrough[QCOW2_DISCARD_SNAPSHOT] =
1027 qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_SNAPSHOT, true);
1028 r->discard_passthrough[QCOW2_DISCARD_OTHER] =
1029 qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_OTHER, false);
1031 switch (s->crypt_method_header) {
1032 case QCOW_CRYPT_NONE:
1034 error_setg(errp, "No encryption in image header, but options "
1035 "specified format '%s'", encryptfmt);
1041 case QCOW_CRYPT_AES:
1042 if (encryptfmt && !g_str_equal(encryptfmt, "aes")) {
1044 "Header reported 'aes' encryption format but "
1045 "options specify '%s'", encryptfmt);
1049 qdict_put_str(encryptopts, "format", "qcow");
1050 r->crypto_opts = block_crypto_open_opts_init(encryptopts, errp);
1053 case QCOW_CRYPT_LUKS:
1054 if (encryptfmt && !g_str_equal(encryptfmt, "luks")) {
1056 "Header reported 'luks' encryption format but "
1057 "options specify '%s'", encryptfmt);
1061 qdict_put_str(encryptopts, "format", "luks");
1062 r->crypto_opts = block_crypto_open_opts_init(encryptopts, errp);
1066 error_setg(errp, "Unsupported encryption method %d",
1067 s->crypt_method_header);
1070 if (s->crypt_method_header != QCOW_CRYPT_NONE && !r->crypto_opts) {
1077 qobject_unref(encryptopts);
1078 qemu_opts_del(opts);
1083 static void qcow2_update_options_commit(BlockDriverState *bs,
1084 Qcow2ReopenState *r)
1086 BDRVQcow2State *s = bs->opaque;
1089 if (s->l2_table_cache) {
1090 qcow2_cache_destroy(s->l2_table_cache);
1092 if (s->refcount_block_cache) {
1093 qcow2_cache_destroy(s->refcount_block_cache);
1095 s->l2_table_cache = r->l2_table_cache;
1096 s->refcount_block_cache = r->refcount_block_cache;
1097 s->l2_slice_size = r->l2_slice_size;
1099 s->overlap_check = r->overlap_check;
1100 s->use_lazy_refcounts = r->use_lazy_refcounts;
1102 for (i = 0; i < QCOW2_DISCARD_MAX; i++) {
1103 s->discard_passthrough[i] = r->discard_passthrough[i];
1106 if (s->cache_clean_interval != r->cache_clean_interval) {
1107 cache_clean_timer_del(bs);
1108 s->cache_clean_interval = r->cache_clean_interval;
1109 cache_clean_timer_init(bs, bdrv_get_aio_context(bs));
1112 qapi_free_QCryptoBlockOpenOptions(s->crypto_opts);
1113 s->crypto_opts = r->crypto_opts;
1116 static void qcow2_update_options_abort(BlockDriverState *bs,
1117 Qcow2ReopenState *r)
1119 if (r->l2_table_cache) {
1120 qcow2_cache_destroy(r->l2_table_cache);
1122 if (r->refcount_block_cache) {
1123 qcow2_cache_destroy(r->refcount_block_cache);
1125 qapi_free_QCryptoBlockOpenOptions(r->crypto_opts);
1128 static int qcow2_update_options(BlockDriverState *bs, QDict *options,
1129 int flags, Error **errp)
1131 Qcow2ReopenState r = {};
1134 ret = qcow2_update_options_prepare(bs, &r, options, flags, errp);
1136 qcow2_update_options_commit(bs, &r);
1138 qcow2_update_options_abort(bs, &r);
1144 /* Called with s->lock held. */
1145 static int coroutine_fn qcow2_do_open(BlockDriverState *bs, QDict *options,
1146 int flags, Error **errp)
1148 BDRVQcow2State *s = bs->opaque;
1149 unsigned int len, i;
1152 Error *local_err = NULL;
1154 uint64_t l1_vm_state_index;
1155 bool update_header = false;
1156 bool header_updated = false;
1158 ret = bdrv_pread(bs->file, 0, &header, sizeof(header));
1160 error_setg_errno(errp, -ret, "Could not read qcow2 header");
1163 be32_to_cpus(&header.magic);
1164 be32_to_cpus(&header.version);
1165 be64_to_cpus(&header.backing_file_offset);
1166 be32_to_cpus(&header.backing_file_size);
1167 be64_to_cpus(&header.size);
1168 be32_to_cpus(&header.cluster_bits);
1169 be32_to_cpus(&header.crypt_method);
1170 be64_to_cpus(&header.l1_table_offset);
1171 be32_to_cpus(&header.l1_size);
1172 be64_to_cpus(&header.refcount_table_offset);
1173 be32_to_cpus(&header.refcount_table_clusters);
1174 be64_to_cpus(&header.snapshots_offset);
1175 be32_to_cpus(&header.nb_snapshots);
1177 if (header.magic != QCOW_MAGIC) {
1178 error_setg(errp, "Image is not in qcow2 format");
1182 if (header.version < 2 || header.version > 3) {
1183 error_setg(errp, "Unsupported qcow2 version %" PRIu32, header.version);
1188 s->qcow_version = header.version;
1190 /* Initialise cluster size */
1191 if (header.cluster_bits < MIN_CLUSTER_BITS ||
1192 header.cluster_bits > MAX_CLUSTER_BITS) {
1193 error_setg(errp, "Unsupported cluster size: 2^%" PRIu32,
1194 header.cluster_bits);
1199 s->cluster_bits = header.cluster_bits;
1200 s->cluster_size = 1 << s->cluster_bits;
1201 s->cluster_sectors = 1 << (s->cluster_bits - BDRV_SECTOR_BITS);
1203 /* Initialise version 3 header fields */
1204 if (header.version == 2) {
1205 header.incompatible_features = 0;
1206 header.compatible_features = 0;
1207 header.autoclear_features = 0;
1208 header.refcount_order = 4;
1209 header.header_length = 72;
1211 be64_to_cpus(&header.incompatible_features);
1212 be64_to_cpus(&header.compatible_features);
1213 be64_to_cpus(&header.autoclear_features);
1214 be32_to_cpus(&header.refcount_order);
1215 be32_to_cpus(&header.header_length);
1217 if (header.header_length < 104) {
1218 error_setg(errp, "qcow2 header too short");
1224 if (header.header_length > s->cluster_size) {
1225 error_setg(errp, "qcow2 header exceeds cluster size");
1230 if (header.header_length > sizeof(header)) {
1231 s->unknown_header_fields_size = header.header_length - sizeof(header);
1232 s->unknown_header_fields = g_malloc(s->unknown_header_fields_size);
1233 ret = bdrv_pread(bs->file, sizeof(header), s->unknown_header_fields,
1234 s->unknown_header_fields_size);
1236 error_setg_errno(errp, -ret, "Could not read unknown qcow2 header "
1242 if (header.backing_file_offset > s->cluster_size) {
1243 error_setg(errp, "Invalid backing file offset");
1248 if (header.backing_file_offset) {
1249 ext_end = header.backing_file_offset;
1251 ext_end = 1 << header.cluster_bits;
1254 /* Handle feature bits */
1255 s->incompatible_features = header.incompatible_features;
1256 s->compatible_features = header.compatible_features;
1257 s->autoclear_features = header.autoclear_features;
1259 if (s->incompatible_features & ~QCOW2_INCOMPAT_MASK) {
1260 void *feature_table = NULL;
1261 qcow2_read_extensions(bs, header.header_length, ext_end,
1262 &feature_table, flags, NULL, NULL);
1263 report_unsupported_feature(errp, feature_table,
1264 s->incompatible_features &
1265 ~QCOW2_INCOMPAT_MASK);
1267 g_free(feature_table);
1271 if (s->incompatible_features & QCOW2_INCOMPAT_CORRUPT) {
1272 /* Corrupt images may not be written to unless they are being repaired
1274 if ((flags & BDRV_O_RDWR) && !(flags & BDRV_O_CHECK)) {
1275 error_setg(errp, "qcow2: Image is corrupt; cannot be opened "
1282 /* Check support for various header values */
1283 if (header.refcount_order > 6) {
1284 error_setg(errp, "Reference count entry width too large; may not "
1289 s->refcount_order = header.refcount_order;
1290 s->refcount_bits = 1 << s->refcount_order;
1291 s->refcount_max = UINT64_C(1) << (s->refcount_bits - 1);
1292 s->refcount_max += s->refcount_max - 1;
1294 s->crypt_method_header = header.crypt_method;
1295 if (s->crypt_method_header) {
1296 if (bdrv_uses_whitelist() &&
1297 s->crypt_method_header == QCOW_CRYPT_AES) {
1299 "Use of AES-CBC encrypted qcow2 images is no longer "
1300 "supported in system emulators");
1301 error_append_hint(errp,
1302 "You can use 'qemu-img convert' to convert your "
1303 "image to an alternative supported format, such "
1304 "as unencrypted qcow2, or raw with the LUKS "
1305 "format instead.\n");
1310 if (s->crypt_method_header == QCOW_CRYPT_AES) {
1311 s->crypt_physical_offset = false;
1313 /* Assuming LUKS and any future crypt methods we
1314 * add will all use physical offsets, due to the
1315 * fact that the alternative is insecure... */
1316 s->crypt_physical_offset = true;
1319 bs->encrypted = true;
1322 s->l2_bits = s->cluster_bits - 3; /* L2 is always one cluster */
1323 s->l2_size = 1 << s->l2_bits;
1324 /* 2^(s->refcount_order - 3) is the refcount width in bytes */
1325 s->refcount_block_bits = s->cluster_bits - (s->refcount_order - 3);
1326 s->refcount_block_size = 1 << s->refcount_block_bits;
1327 bs->total_sectors = header.size / 512;
1328 s->csize_shift = (62 - (s->cluster_bits - 8));
1329 s->csize_mask = (1 << (s->cluster_bits - 8)) - 1;
1330 s->cluster_offset_mask = (1LL << s->csize_shift) - 1;
1332 s->refcount_table_offset = header.refcount_table_offset;
1333 s->refcount_table_size =
1334 header.refcount_table_clusters << (s->cluster_bits - 3);
1336 if (header.refcount_table_clusters == 0 && !(flags & BDRV_O_CHECK)) {
1337 error_setg(errp, "Image does not contain a reference count table");
1342 ret = qcow2_validate_table(bs, s->refcount_table_offset,
1343 header.refcount_table_clusters,
1344 s->cluster_size, QCOW_MAX_REFTABLE_SIZE,
1345 "Reference count table", errp);
1350 /* The total size in bytes of the snapshot table is checked in
1351 * qcow2_read_snapshots() because the size of each snapshot is
1352 * variable and we don't know it yet.
1353 * Here we only check the offset and number of snapshots. */
1354 ret = qcow2_validate_table(bs, header.snapshots_offset,
1355 header.nb_snapshots,
1356 sizeof(QCowSnapshotHeader),
1357 sizeof(QCowSnapshotHeader) * QCOW_MAX_SNAPSHOTS,
1358 "Snapshot table", errp);
1363 /* read the level 1 table */
1364 ret = qcow2_validate_table(bs, header.l1_table_offset,
1365 header.l1_size, sizeof(uint64_t),
1366 QCOW_MAX_L1_SIZE, "Active L1 table", errp);
1370 s->l1_size = header.l1_size;
1371 s->l1_table_offset = header.l1_table_offset;
1373 l1_vm_state_index = size_to_l1(s, header.size);
1374 if (l1_vm_state_index > INT_MAX) {
1375 error_setg(errp, "Image is too big");
1379 s->l1_vm_state_index = l1_vm_state_index;
1381 /* the L1 table must contain at least enough entries to put
1382 header.size bytes */
1383 if (s->l1_size < s->l1_vm_state_index) {
1384 error_setg(errp, "L1 table is too small");
1389 if (s->l1_size > 0) {
1390 s->l1_table = qemu_try_blockalign(bs->file->bs,
1391 ROUND_UP(s->l1_size * sizeof(uint64_t), 512));
1392 if (s->l1_table == NULL) {
1393 error_setg(errp, "Could not allocate L1 table");
1397 ret = bdrv_pread(bs->file, s->l1_table_offset, s->l1_table,
1398 s->l1_size * sizeof(uint64_t));
1400 error_setg_errno(errp, -ret, "Could not read L1 table");
1403 for(i = 0;i < s->l1_size; i++) {
1404 be64_to_cpus(&s->l1_table[i]);
1408 /* Parse driver-specific options */
1409 ret = qcow2_update_options(bs, options, flags, errp);
1414 s->cluster_cache_offset = -1;
1417 ret = qcow2_refcount_init(bs);
1419 error_setg_errno(errp, -ret, "Could not initialize refcount handling");
1423 QLIST_INIT(&s->cluster_allocs);
1424 QTAILQ_INIT(&s->discards);
1426 /* read qcow2 extensions */
1427 if (qcow2_read_extensions(bs, header.header_length, ext_end, NULL,
1428 flags, &update_header, &local_err)) {
1429 error_propagate(errp, local_err);
1434 /* qcow2_read_extension may have set up the crypto context
1435 * if the crypt method needs a header region, some methods
1436 * don't need header extensions, so must check here
1438 if (s->crypt_method_header && !s->crypto) {
1439 if (s->crypt_method_header == QCOW_CRYPT_AES) {
1440 unsigned int cflags = 0;
1441 if (flags & BDRV_O_NO_IO) {
1442 cflags |= QCRYPTO_BLOCK_OPEN_NO_IO;
1444 s->crypto = qcrypto_block_open(s->crypto_opts, "encrypt.",
1445 NULL, NULL, cflags, errp);
1450 } else if (!(flags & BDRV_O_NO_IO)) {
1451 error_setg(errp, "Missing CRYPTO header for crypt method %d",
1452 s->crypt_method_header);
1458 /* read the backing file name */
1459 if (header.backing_file_offset != 0) {
1460 len = header.backing_file_size;
1461 if (len > MIN(1023, s->cluster_size - header.backing_file_offset) ||
1462 len >= sizeof(bs->backing_file)) {
1463 error_setg(errp, "Backing file name too long");
1467 ret = bdrv_pread(bs->file, header.backing_file_offset,
1468 bs->backing_file, len);
1470 error_setg_errno(errp, -ret, "Could not read backing file name");
1473 bs->backing_file[len] = '\0';
1474 s->image_backing_file = g_strdup(bs->backing_file);
1477 /* Internal snapshots */
1478 s->snapshots_offset = header.snapshots_offset;
1479 s->nb_snapshots = header.nb_snapshots;
1481 ret = qcow2_read_snapshots(bs);
1483 error_setg_errno(errp, -ret, "Could not read snapshots");
1487 /* Clear unknown autoclear feature bits */
1488 update_header |= s->autoclear_features & ~QCOW2_AUTOCLEAR_MASK;
1490 update_header && !bs->read_only && !(flags & BDRV_O_INACTIVE);
1491 if (update_header) {
1492 s->autoclear_features &= QCOW2_AUTOCLEAR_MASK;
1495 if (s->dirty_bitmaps_loaded) {
1496 /* It's some kind of reopen. There are no known cases where we need to
1497 * reload bitmaps in such a situation, so it's safer to skip them.
1499 * Moreover, if we have some readonly bitmaps and we are reopening for
1500 * rw we should reopen bitmaps correspondingly.
1502 if (bdrv_has_readonly_bitmaps(bs) &&
1503 !bdrv_is_read_only(bs) && !(bdrv_get_flags(bs) & BDRV_O_INACTIVE))
1505 qcow2_reopen_bitmaps_rw_hint(bs, &header_updated, &local_err);
1508 header_updated = qcow2_load_dirty_bitmaps(bs, &local_err);
1509 s->dirty_bitmaps_loaded = true;
1511 update_header = update_header && !header_updated;
1512 if (local_err != NULL) {
1513 error_propagate(errp, local_err);
1518 if (update_header) {
1519 ret = qcow2_update_header(bs);
1521 error_setg_errno(errp, -ret, "Could not update qcow2 header");
1526 bs->supported_zero_flags = header.version >= 3 ? BDRV_REQ_MAY_UNMAP : 0;
1528 /* Repair image if dirty */
1529 if (!(flags & (BDRV_O_CHECK | BDRV_O_INACTIVE)) && !bs->read_only &&
1530 (s->incompatible_features & QCOW2_INCOMPAT_DIRTY)) {
1531 BdrvCheckResult result = {0};
1533 ret = qcow2_co_check_locked(bs, &result,
1534 BDRV_FIX_ERRORS | BDRV_FIX_LEAKS);
1535 if (ret < 0 || result.check_errors) {
1539 error_setg_errno(errp, -ret, "Could not repair dirty image");
1546 BdrvCheckResult result = {0};
1547 qcow2_check_refcounts(bs, &result, 0);
1551 qemu_co_queue_init(&s->compress_wait_queue);
1556 g_free(s->unknown_header_fields);
1557 cleanup_unknown_header_ext(bs);
1558 qcow2_free_snapshots(bs);
1559 qcow2_refcount_close(bs);
1560 qemu_vfree(s->l1_table);
1561 /* else pre-write overlap checks in cache_destroy may crash */
1563 cache_clean_timer_del(bs);
1564 if (s->l2_table_cache) {
1565 qcow2_cache_destroy(s->l2_table_cache);
1567 if (s->refcount_block_cache) {
1568 qcow2_cache_destroy(s->refcount_block_cache);
1570 qcrypto_block_free(s->crypto);
1571 qapi_free_QCryptoBlockOpenOptions(s->crypto_opts);
1575 typedef struct QCow2OpenCo {
1576 BlockDriverState *bs;
1583 static void coroutine_fn qcow2_open_entry(void *opaque)
1585 QCow2OpenCo *qoc = opaque;
1586 BDRVQcow2State *s = qoc->bs->opaque;
1588 qemu_co_mutex_lock(&s->lock);
1589 qoc->ret = qcow2_do_open(qoc->bs, qoc->options, qoc->flags, qoc->errp);
1590 qemu_co_mutex_unlock(&s->lock);
1593 static int qcow2_open(BlockDriverState *bs, QDict *options, int flags,
1596 BDRVQcow2State *s = bs->opaque;
1605 bs->file = bdrv_open_child(NULL, options, "file", bs, &child_file,
1611 /* Initialise locks */
1612 qemu_co_mutex_init(&s->lock);
1614 if (qemu_in_coroutine()) {
1615 /* From bdrv_co_create. */
1616 qcow2_open_entry(&qoc);
1618 qemu_coroutine_enter(qemu_coroutine_create(qcow2_open_entry, &qoc));
1619 BDRV_POLL_WHILE(bs, qoc.ret == -EINPROGRESS);
1624 static void qcow2_refresh_limits(BlockDriverState *bs, Error **errp)
1626 BDRVQcow2State *s = bs->opaque;
1628 if (bs->encrypted) {
1629 /* Encryption works on a sector granularity */
1630 bs->bl.request_alignment = BDRV_SECTOR_SIZE;
1632 bs->bl.pwrite_zeroes_alignment = s->cluster_size;
1633 bs->bl.pdiscard_alignment = s->cluster_size;
1636 static int qcow2_reopen_prepare(BDRVReopenState *state,
1637 BlockReopenQueue *queue, Error **errp)
1639 Qcow2ReopenState *r;
1642 r = g_new0(Qcow2ReopenState, 1);
1645 ret = qcow2_update_options_prepare(state->bs, r, state->options,
1646 state->flags, errp);
1651 /* We need to write out any unwritten data if we reopen read-only. */
1652 if ((state->flags & BDRV_O_RDWR) == 0) {
1653 ret = qcow2_reopen_bitmaps_ro(state->bs, errp);
1658 ret = bdrv_flush(state->bs);
1663 ret = qcow2_mark_clean(state->bs);
1672 qcow2_update_options_abort(state->bs, r);
1677 static void qcow2_reopen_commit(BDRVReopenState *state)
1679 qcow2_update_options_commit(state->bs, state->opaque);
1680 g_free(state->opaque);
1683 static void qcow2_reopen_abort(BDRVReopenState *state)
1685 qcow2_update_options_abort(state->bs, state->opaque);
1686 g_free(state->opaque);
1689 static void qcow2_join_options(QDict *options, QDict *old_options)
1691 bool has_new_overlap_template =
1692 qdict_haskey(options, QCOW2_OPT_OVERLAP) ||
1693 qdict_haskey(options, QCOW2_OPT_OVERLAP_TEMPLATE);
1694 bool has_new_total_cache_size =
1695 qdict_haskey(options, QCOW2_OPT_CACHE_SIZE);
1696 bool has_all_cache_options;
1698 /* New overlap template overrides all old overlap options */
1699 if (has_new_overlap_template) {
1700 qdict_del(old_options, QCOW2_OPT_OVERLAP);
1701 qdict_del(old_options, QCOW2_OPT_OVERLAP_TEMPLATE);
1702 qdict_del(old_options, QCOW2_OPT_OVERLAP_MAIN_HEADER);
1703 qdict_del(old_options, QCOW2_OPT_OVERLAP_ACTIVE_L1);
1704 qdict_del(old_options, QCOW2_OPT_OVERLAP_ACTIVE_L2);
1705 qdict_del(old_options, QCOW2_OPT_OVERLAP_REFCOUNT_TABLE);
1706 qdict_del(old_options, QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK);
1707 qdict_del(old_options, QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE);
1708 qdict_del(old_options, QCOW2_OPT_OVERLAP_INACTIVE_L1);
1709 qdict_del(old_options, QCOW2_OPT_OVERLAP_INACTIVE_L2);
1712 /* New total cache size overrides all old options */
1713 if (qdict_haskey(options, QCOW2_OPT_CACHE_SIZE)) {
1714 qdict_del(old_options, QCOW2_OPT_L2_CACHE_SIZE);
1715 qdict_del(old_options, QCOW2_OPT_REFCOUNT_CACHE_SIZE);
1718 qdict_join(options, old_options, false);
1721 * If after merging all cache size options are set, an old total size is
1722 * overwritten. Do keep all options, however, if all three are new. The
1723 * resulting error message is what we want to happen.
1725 has_all_cache_options =
1726 qdict_haskey(options, QCOW2_OPT_CACHE_SIZE) ||
1727 qdict_haskey(options, QCOW2_OPT_L2_CACHE_SIZE) ||
1728 qdict_haskey(options, QCOW2_OPT_REFCOUNT_CACHE_SIZE);
1730 if (has_all_cache_options && !has_new_total_cache_size) {
1731 qdict_del(options, QCOW2_OPT_CACHE_SIZE);
1735 static int coroutine_fn qcow2_co_block_status(BlockDriverState *bs,
1737 int64_t offset, int64_t count,
1738 int64_t *pnum, int64_t *map,
1739 BlockDriverState **file)
1741 BDRVQcow2State *s = bs->opaque;
1742 uint64_t cluster_offset;
1743 int index_in_cluster, ret;
1747 bytes = MIN(INT_MAX, count);
1748 qemu_co_mutex_lock(&s->lock);
1749 ret = qcow2_get_cluster_offset(bs, offset, &bytes, &cluster_offset);
1750 qemu_co_mutex_unlock(&s->lock);
1757 if (cluster_offset != 0 && ret != QCOW2_CLUSTER_COMPRESSED &&
1759 index_in_cluster = offset & (s->cluster_size - 1);
1760 *map = cluster_offset | index_in_cluster;
1761 *file = bs->file->bs;
1762 status |= BDRV_BLOCK_OFFSET_VALID;
1764 if (ret == QCOW2_CLUSTER_ZERO_PLAIN || ret == QCOW2_CLUSTER_ZERO_ALLOC) {
1765 status |= BDRV_BLOCK_ZERO;
1766 } else if (ret != QCOW2_CLUSTER_UNALLOCATED) {
1767 status |= BDRV_BLOCK_DATA;
1772 static coroutine_fn int qcow2_handle_l2meta(BlockDriverState *bs,
1773 QCowL2Meta **pl2meta,
1777 QCowL2Meta *l2meta = *pl2meta;
1779 while (l2meta != NULL) {
1783 ret = qcow2_alloc_cluster_link_l2(bs, l2meta);
1788 qcow2_alloc_cluster_abort(bs, l2meta);
1791 /* Take the request off the list of running requests */
1792 if (l2meta->nb_clusters != 0) {
1793 QLIST_REMOVE(l2meta, next_in_flight);
1796 qemu_co_queue_restart_all(&l2meta->dependent_requests);
1798 next = l2meta->next;
1807 static coroutine_fn int qcow2_co_preadv(BlockDriverState *bs, uint64_t offset,
1808 uint64_t bytes, QEMUIOVector *qiov,
1811 BDRVQcow2State *s = bs->opaque;
1812 int offset_in_cluster;
1814 unsigned int cur_bytes; /* number of bytes in current iteration */
1815 uint64_t cluster_offset = 0;
1816 uint64_t bytes_done = 0;
1817 QEMUIOVector hd_qiov;
1818 uint8_t *cluster_data = NULL;
1820 qemu_iovec_init(&hd_qiov, qiov->niov);
1822 qemu_co_mutex_lock(&s->lock);
1824 while (bytes != 0) {
1826 /* prepare next request */
1827 cur_bytes = MIN(bytes, INT_MAX);
1829 cur_bytes = MIN(cur_bytes,
1830 QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size);
1833 ret = qcow2_get_cluster_offset(bs, offset, &cur_bytes, &cluster_offset);
1838 offset_in_cluster = offset_into_cluster(s, offset);
1840 qemu_iovec_reset(&hd_qiov);
1841 qemu_iovec_concat(&hd_qiov, qiov, bytes_done, cur_bytes);
1844 case QCOW2_CLUSTER_UNALLOCATED:
1847 BLKDBG_EVENT(bs->file, BLKDBG_READ_BACKING_AIO);
1848 qemu_co_mutex_unlock(&s->lock);
1849 ret = bdrv_co_preadv(bs->backing, offset, cur_bytes,
1851 qemu_co_mutex_lock(&s->lock);
1856 /* Note: in this case, no need to wait */
1857 qemu_iovec_memset(&hd_qiov, 0, 0, cur_bytes);
1861 case QCOW2_CLUSTER_ZERO_PLAIN:
1862 case QCOW2_CLUSTER_ZERO_ALLOC:
1863 qemu_iovec_memset(&hd_qiov, 0, 0, cur_bytes);
1866 case QCOW2_CLUSTER_COMPRESSED:
1867 /* add AIO support for compressed blocks ? */
1868 ret = qcow2_decompress_cluster(bs, cluster_offset);
1873 qemu_iovec_from_buf(&hd_qiov, 0,
1874 s->cluster_cache + offset_in_cluster,
1878 case QCOW2_CLUSTER_NORMAL:
1879 if ((cluster_offset & 511) != 0) {
1884 if (bs->encrypted) {
1888 * For encrypted images, read everything into a temporary
1889 * contiguous buffer on which the AES functions can work.
1891 if (!cluster_data) {
1893 qemu_try_blockalign(bs->file->bs,
1894 QCOW_MAX_CRYPT_CLUSTERS
1896 if (cluster_data == NULL) {
1902 assert(cur_bytes <= QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size);
1903 qemu_iovec_reset(&hd_qiov);
1904 qemu_iovec_add(&hd_qiov, cluster_data, cur_bytes);
1907 BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO);
1908 qemu_co_mutex_unlock(&s->lock);
1909 ret = bdrv_co_preadv(bs->file,
1910 cluster_offset + offset_in_cluster,
1911 cur_bytes, &hd_qiov, 0);
1912 qemu_co_mutex_lock(&s->lock);
1916 if (bs->encrypted) {
1918 assert((offset & (BDRV_SECTOR_SIZE - 1)) == 0);
1919 assert((cur_bytes & (BDRV_SECTOR_SIZE - 1)) == 0);
1920 if (qcrypto_block_decrypt(s->crypto,
1921 (s->crypt_physical_offset ?
1922 cluster_offset + offset_in_cluster :
1930 qemu_iovec_from_buf(qiov, bytes_done, cluster_data, cur_bytes);
1935 g_assert_not_reached();
1941 offset += cur_bytes;
1942 bytes_done += cur_bytes;
1947 qemu_co_mutex_unlock(&s->lock);
1949 qemu_iovec_destroy(&hd_qiov);
1950 qemu_vfree(cluster_data);
1955 /* Check if it's possible to merge a write request with the writing of
1956 * the data from the COW regions */
1957 static bool merge_cow(uint64_t offset, unsigned bytes,
1958 QEMUIOVector *hd_qiov, QCowL2Meta *l2meta)
1962 for (m = l2meta; m != NULL; m = m->next) {
1963 /* If both COW regions are empty then there's nothing to merge */
1964 if (m->cow_start.nb_bytes == 0 && m->cow_end.nb_bytes == 0) {
1968 /* The data (middle) region must be immediately after the
1970 if (l2meta_cow_start(m) + m->cow_start.nb_bytes != offset) {
1974 /* The end region must be immediately after the data (middle)
1976 if (m->offset + m->cow_end.offset != offset + bytes) {
1980 /* Make sure that adding both COW regions to the QEMUIOVector
1981 * does not exceed IOV_MAX */
1982 if (hd_qiov->niov > IOV_MAX - 2) {
1986 m->data_qiov = hd_qiov;
1993 static coroutine_fn int qcow2_co_pwritev(BlockDriverState *bs, uint64_t offset,
1994 uint64_t bytes, QEMUIOVector *qiov,
1997 BDRVQcow2State *s = bs->opaque;
1998 int offset_in_cluster;
2000 unsigned int cur_bytes; /* number of sectors in current iteration */
2001 uint64_t cluster_offset;
2002 QEMUIOVector hd_qiov;
2003 uint64_t bytes_done = 0;
2004 uint8_t *cluster_data = NULL;
2005 QCowL2Meta *l2meta = NULL;
2007 trace_qcow2_writev_start_req(qemu_coroutine_self(), offset, bytes);
2009 qemu_iovec_init(&hd_qiov, qiov->niov);
2011 s->cluster_cache_offset = -1; /* disable compressed cache */
2013 qemu_co_mutex_lock(&s->lock);
2015 while (bytes != 0) {
2019 trace_qcow2_writev_start_part(qemu_coroutine_self());
2020 offset_in_cluster = offset_into_cluster(s, offset);
2021 cur_bytes = MIN(bytes, INT_MAX);
2022 if (bs->encrypted) {
2023 cur_bytes = MIN(cur_bytes,
2024 QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size
2025 - offset_in_cluster);
2028 ret = qcow2_alloc_cluster_offset(bs, offset, &cur_bytes,
2029 &cluster_offset, &l2meta);
2034 assert((cluster_offset & 511) == 0);
2036 qemu_iovec_reset(&hd_qiov);
2037 qemu_iovec_concat(&hd_qiov, qiov, bytes_done, cur_bytes);
2039 if (bs->encrypted) {
2041 if (!cluster_data) {
2042 cluster_data = qemu_try_blockalign(bs->file->bs,
2043 QCOW_MAX_CRYPT_CLUSTERS
2045 if (cluster_data == NULL) {
2051 assert(hd_qiov.size <=
2052 QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size);
2053 qemu_iovec_to_buf(&hd_qiov, 0, cluster_data, hd_qiov.size);
2055 if (qcrypto_block_encrypt(s->crypto,
2056 (s->crypt_physical_offset ?
2057 cluster_offset + offset_in_cluster :
2060 cur_bytes, NULL) < 0) {
2065 qemu_iovec_reset(&hd_qiov);
2066 qemu_iovec_add(&hd_qiov, cluster_data, cur_bytes);
2069 ret = qcow2_pre_write_overlap_check(bs, 0,
2070 cluster_offset + offset_in_cluster, cur_bytes);
2075 /* If we need to do COW, check if it's possible to merge the
2076 * writing of the guest data together with that of the COW regions.
2077 * If it's not possible (or not necessary) then write the
2078 * guest data now. */
2079 if (!merge_cow(offset, cur_bytes, &hd_qiov, l2meta)) {
2080 qemu_co_mutex_unlock(&s->lock);
2081 BLKDBG_EVENT(bs->file, BLKDBG_WRITE_AIO);
2082 trace_qcow2_writev_data(qemu_coroutine_self(),
2083 cluster_offset + offset_in_cluster);
2084 ret = bdrv_co_pwritev(bs->file,
2085 cluster_offset + offset_in_cluster,
2086 cur_bytes, &hd_qiov, 0);
2087 qemu_co_mutex_lock(&s->lock);
2093 ret = qcow2_handle_l2meta(bs, &l2meta, true);
2099 offset += cur_bytes;
2100 bytes_done += cur_bytes;
2101 trace_qcow2_writev_done_part(qemu_coroutine_self(), cur_bytes);
2106 qcow2_handle_l2meta(bs, &l2meta, false);
2108 qemu_co_mutex_unlock(&s->lock);
2110 qemu_iovec_destroy(&hd_qiov);
2111 qemu_vfree(cluster_data);
2112 trace_qcow2_writev_done_req(qemu_coroutine_self(), ret);
2117 static int qcow2_inactivate(BlockDriverState *bs)
2119 BDRVQcow2State *s = bs->opaque;
2120 int ret, result = 0;
2121 Error *local_err = NULL;
2123 qcow2_store_persistent_dirty_bitmaps(bs, &local_err);
2124 if (local_err != NULL) {
2126 error_report_err(local_err);
2127 error_report("Persistent bitmaps are lost for node '%s'",
2128 bdrv_get_device_or_node_name(bs));
2131 ret = qcow2_cache_flush(bs, s->l2_table_cache);
2134 error_report("Failed to flush the L2 table cache: %s",
2138 ret = qcow2_cache_flush(bs, s->refcount_block_cache);
2141 error_report("Failed to flush the refcount block cache: %s",
2146 qcow2_mark_clean(bs);
2152 static void qcow2_close(BlockDriverState *bs)
2154 BDRVQcow2State *s = bs->opaque;
2155 qemu_vfree(s->l1_table);
2156 /* else pre-write overlap checks in cache_destroy may crash */
2159 if (!(s->flags & BDRV_O_INACTIVE)) {
2160 qcow2_inactivate(bs);
2163 cache_clean_timer_del(bs);
2164 qcow2_cache_destroy(s->l2_table_cache);
2165 qcow2_cache_destroy(s->refcount_block_cache);
2167 qcrypto_block_free(s->crypto);
2170 g_free(s->unknown_header_fields);
2171 cleanup_unknown_header_ext(bs);
2173 g_free(s->image_backing_file);
2174 g_free(s->image_backing_format);
2176 g_free(s->cluster_cache);
2177 qemu_vfree(s->cluster_data);
2178 qcow2_refcount_close(bs);
2179 qcow2_free_snapshots(bs);
2182 static void coroutine_fn qcow2_co_invalidate_cache(BlockDriverState *bs,
2185 BDRVQcow2State *s = bs->opaque;
2186 int flags = s->flags;
2187 QCryptoBlock *crypto = NULL;
2189 Error *local_err = NULL;
2193 * Backing files are read-only which makes all of their metadata immutable,
2194 * that means we don't have to worry about reopening them here.
2202 memset(s, 0, sizeof(BDRVQcow2State));
2203 options = qdict_clone_shallow(bs->options);
2205 flags &= ~BDRV_O_INACTIVE;
2206 qemu_co_mutex_lock(&s->lock);
2207 ret = qcow2_do_open(bs, options, flags, &local_err);
2208 qemu_co_mutex_unlock(&s->lock);
2209 qobject_unref(options);
2211 error_propagate(errp, local_err);
2212 error_prepend(errp, "Could not reopen qcow2 layer: ");
2215 } else if (ret < 0) {
2216 error_setg_errno(errp, -ret, "Could not reopen qcow2 layer");
2224 static size_t header_ext_add(char *buf, uint32_t magic, const void *s,
2225 size_t len, size_t buflen)
2227 QCowExtension *ext_backing_fmt = (QCowExtension*) buf;
2228 size_t ext_len = sizeof(QCowExtension) + ((len + 7) & ~7);
2230 if (buflen < ext_len) {
2234 *ext_backing_fmt = (QCowExtension) {
2235 .magic = cpu_to_be32(magic),
2236 .len = cpu_to_be32(len),
2240 memcpy(buf + sizeof(QCowExtension), s, len);
2247 * Updates the qcow2 header, including the variable length parts of it, i.e.
2248 * the backing file name and all extensions. qcow2 was not designed to allow
2249 * such changes, so if we run out of space (we can only use the first cluster)
2250 * this function may fail.
2252 * Returns 0 on success, -errno in error cases.
2254 int qcow2_update_header(BlockDriverState *bs)
2256 BDRVQcow2State *s = bs->opaque;
2259 size_t buflen = s->cluster_size;
2261 uint64_t total_size;
2262 uint32_t refcount_table_clusters;
2263 size_t header_length;
2264 Qcow2UnknownHeaderExtension *uext;
2266 buf = qemu_blockalign(bs, buflen);
2268 /* Header structure */
2269 header = (QCowHeader*) buf;
2271 if (buflen < sizeof(*header)) {
2276 header_length = sizeof(*header) + s->unknown_header_fields_size;
2277 total_size = bs->total_sectors * BDRV_SECTOR_SIZE;
2278 refcount_table_clusters = s->refcount_table_size >> (s->cluster_bits - 3);
2280 *header = (QCowHeader) {
2281 /* Version 2 fields */
2282 .magic = cpu_to_be32(QCOW_MAGIC),
2283 .version = cpu_to_be32(s->qcow_version),
2284 .backing_file_offset = 0,
2285 .backing_file_size = 0,
2286 .cluster_bits = cpu_to_be32(s->cluster_bits),
2287 .size = cpu_to_be64(total_size),
2288 .crypt_method = cpu_to_be32(s->crypt_method_header),
2289 .l1_size = cpu_to_be32(s->l1_size),
2290 .l1_table_offset = cpu_to_be64(s->l1_table_offset),
2291 .refcount_table_offset = cpu_to_be64(s->refcount_table_offset),
2292 .refcount_table_clusters = cpu_to_be32(refcount_table_clusters),
2293 .nb_snapshots = cpu_to_be32(s->nb_snapshots),
2294 .snapshots_offset = cpu_to_be64(s->snapshots_offset),
2296 /* Version 3 fields */
2297 .incompatible_features = cpu_to_be64(s->incompatible_features),
2298 .compatible_features = cpu_to_be64(s->compatible_features),
2299 .autoclear_features = cpu_to_be64(s->autoclear_features),
2300 .refcount_order = cpu_to_be32(s->refcount_order),
2301 .header_length = cpu_to_be32(header_length),
2304 /* For older versions, write a shorter header */
2305 switch (s->qcow_version) {
2307 ret = offsetof(QCowHeader, incompatible_features);
2310 ret = sizeof(*header);
2319 memset(buf, 0, buflen);
2321 /* Preserve any unknown field in the header */
2322 if (s->unknown_header_fields_size) {
2323 if (buflen < s->unknown_header_fields_size) {
2328 memcpy(buf, s->unknown_header_fields, s->unknown_header_fields_size);
2329 buf += s->unknown_header_fields_size;
2330 buflen -= s->unknown_header_fields_size;
2333 /* Backing file format header extension */
2334 if (s->image_backing_format) {
2335 ret = header_ext_add(buf, QCOW2_EXT_MAGIC_BACKING_FORMAT,
2336 s->image_backing_format,
2337 strlen(s->image_backing_format),
2347 /* Full disk encryption header pointer extension */
2348 if (s->crypto_header.offset != 0) {
2349 cpu_to_be64s(&s->crypto_header.offset);
2350 cpu_to_be64s(&s->crypto_header.length);
2351 ret = header_ext_add(buf, QCOW2_EXT_MAGIC_CRYPTO_HEADER,
2352 &s->crypto_header, sizeof(s->crypto_header),
2354 be64_to_cpus(&s->crypto_header.offset);
2355 be64_to_cpus(&s->crypto_header.length);
2364 if (s->qcow_version >= 3) {
2365 Qcow2Feature features[] = {
2367 .type = QCOW2_FEAT_TYPE_INCOMPATIBLE,
2368 .bit = QCOW2_INCOMPAT_DIRTY_BITNR,
2369 .name = "dirty bit",
2372 .type = QCOW2_FEAT_TYPE_INCOMPATIBLE,
2373 .bit = QCOW2_INCOMPAT_CORRUPT_BITNR,
2374 .name = "corrupt bit",
2377 .type = QCOW2_FEAT_TYPE_COMPATIBLE,
2378 .bit = QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR,
2379 .name = "lazy refcounts",
2383 ret = header_ext_add(buf, QCOW2_EXT_MAGIC_FEATURE_TABLE,
2384 features, sizeof(features), buflen);
2392 /* Bitmap extension */
2393 if (s->nb_bitmaps > 0) {
2394 Qcow2BitmapHeaderExt bitmaps_header = {
2395 .nb_bitmaps = cpu_to_be32(s->nb_bitmaps),
2396 .bitmap_directory_size =
2397 cpu_to_be64(s->bitmap_directory_size),
2398 .bitmap_directory_offset =
2399 cpu_to_be64(s->bitmap_directory_offset)
2401 ret = header_ext_add(buf, QCOW2_EXT_MAGIC_BITMAPS,
2402 &bitmaps_header, sizeof(bitmaps_header),
2411 /* Keep unknown header extensions */
2412 QLIST_FOREACH(uext, &s->unknown_header_ext, next) {
2413 ret = header_ext_add(buf, uext->magic, uext->data, uext->len, buflen);
2422 /* End of header extensions */
2423 ret = header_ext_add(buf, QCOW2_EXT_MAGIC_END, NULL, 0, buflen);
2431 /* Backing file name */
2432 if (s->image_backing_file) {
2433 size_t backing_file_len = strlen(s->image_backing_file);
2435 if (buflen < backing_file_len) {
2440 /* Using strncpy is ok here, since buf is not NUL-terminated. */
2441 strncpy(buf, s->image_backing_file, buflen);
2443 header->backing_file_offset = cpu_to_be64(buf - ((char*) header));
2444 header->backing_file_size = cpu_to_be32(backing_file_len);
2447 /* Write the new header */
2448 ret = bdrv_pwrite(bs->file, 0, header, s->cluster_size);
2459 static int qcow2_change_backing_file(BlockDriverState *bs,
2460 const char *backing_file, const char *backing_fmt)
2462 BDRVQcow2State *s = bs->opaque;
2464 if (backing_file && strlen(backing_file) > 1023) {
2468 pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: "");
2469 pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: "");
2471 g_free(s->image_backing_file);
2472 g_free(s->image_backing_format);
2474 s->image_backing_file = backing_file ? g_strdup(bs->backing_file) : NULL;
2475 s->image_backing_format = backing_fmt ? g_strdup(bs->backing_format) : NULL;
2477 return qcow2_update_header(bs);
2480 static int qcow2_crypt_method_from_format(const char *encryptfmt)
2482 if (g_str_equal(encryptfmt, "luks")) {
2483 return QCOW_CRYPT_LUKS;
2484 } else if (g_str_equal(encryptfmt, "aes")) {
2485 return QCOW_CRYPT_AES;
2491 static int qcow2_set_up_encryption(BlockDriverState *bs,
2492 QCryptoBlockCreateOptions *cryptoopts,
2495 BDRVQcow2State *s = bs->opaque;
2496 QCryptoBlock *crypto = NULL;
2499 switch (cryptoopts->format) {
2500 case Q_CRYPTO_BLOCK_FORMAT_LUKS:
2501 fmt = QCOW_CRYPT_LUKS;
2503 case Q_CRYPTO_BLOCK_FORMAT_QCOW:
2504 fmt = QCOW_CRYPT_AES;
2507 error_setg(errp, "Crypto format not supported in qcow2");
2511 s->crypt_method_header = fmt;
2513 crypto = qcrypto_block_create(cryptoopts, "encrypt.",
2514 qcow2_crypto_hdr_init_func,
2515 qcow2_crypto_hdr_write_func,
2521 ret = qcow2_update_header(bs);
2523 error_setg_errno(errp, -ret, "Could not write encryption header");
2529 qcrypto_block_free(crypto);
2534 * Preallocates metadata structures for data clusters between @offset (in the
2535 * guest disk) and @new_length (which is thus generally the new guest disk
2538 * Returns: 0 on success, -errno on failure.
2540 static int coroutine_fn preallocate_co(BlockDriverState *bs, uint64_t offset,
2541 uint64_t new_length)
2544 uint64_t host_offset = 0;
2545 unsigned int cur_bytes;
2549 assert(offset <= new_length);
2550 bytes = new_length - offset;
2553 cur_bytes = MIN(bytes, INT_MAX);
2554 ret = qcow2_alloc_cluster_offset(bs, offset, &cur_bytes,
2555 &host_offset, &meta);
2561 QCowL2Meta *next = meta->next;
2563 ret = qcow2_alloc_cluster_link_l2(bs, meta);
2565 qcow2_free_any_clusters(bs, meta->alloc_offset,
2566 meta->nb_clusters, QCOW2_DISCARD_NEVER);
2570 /* There are no dependent requests, but we need to remove our
2571 * request from the list of in-flight requests */
2572 QLIST_REMOVE(meta, next_in_flight);
2578 /* TODO Preallocate data if requested */
2581 offset += cur_bytes;
2585 * It is expected that the image file is large enough to actually contain
2586 * all of the allocated clusters (otherwise we get failing reads after
2587 * EOF). Extend the image to the last allocated sector.
2589 if (host_offset != 0) {
2591 ret = bdrv_pwrite(bs->file, (host_offset + cur_bytes) - 1,
2601 /* qcow2_refcount_metadata_size:
2602 * @clusters: number of clusters to refcount (including data and L1/L2 tables)
2603 * @cluster_size: size of a cluster, in bytes
2604 * @refcount_order: refcount bits power-of-2 exponent
2605 * @generous_increase: allow for the refcount table to be 1.5x as large as it
2608 * Returns: Number of bytes required for refcount blocks and table metadata.
2610 int64_t qcow2_refcount_metadata_size(int64_t clusters, size_t cluster_size,
2611 int refcount_order, bool generous_increase,
2612 uint64_t *refblock_count)
2615 * Every host cluster is reference-counted, including metadata (even
2616 * refcount metadata is recursively included).
2618 * An accurate formula for the size of refcount metadata size is difficult
2619 * to derive. An easier method of calculation is finding the fixed point
2620 * where no further refcount blocks or table clusters are required to
2621 * reference count every cluster.
2623 int64_t blocks_per_table_cluster = cluster_size / sizeof(uint64_t);
2624 int64_t refcounts_per_block = cluster_size * 8 / (1 << refcount_order);
2625 int64_t table = 0; /* number of refcount table clusters */
2626 int64_t blocks = 0; /* number of refcount block clusters */
2632 blocks = DIV_ROUND_UP(clusters + table + blocks, refcounts_per_block);
2633 table = DIV_ROUND_UP(blocks, blocks_per_table_cluster);
2634 n = clusters + blocks + table;
2636 if (n == last && generous_increase) {
2637 clusters += DIV_ROUND_UP(table, 2);
2638 n = 0; /* force another loop */
2639 generous_increase = false;
2641 } while (n != last);
2643 if (refblock_count) {
2644 *refblock_count = blocks;
2647 return (blocks + table) * cluster_size;
2651 * qcow2_calc_prealloc_size:
2652 * @total_size: virtual disk size in bytes
2653 * @cluster_size: cluster size in bytes
2654 * @refcount_order: refcount bits power-of-2 exponent
2656 * Returns: Total number of bytes required for the fully allocated image
2657 * (including metadata).
2659 static int64_t qcow2_calc_prealloc_size(int64_t total_size,
2660 size_t cluster_size,
2663 int64_t meta_size = 0;
2664 uint64_t nl1e, nl2e;
2665 int64_t aligned_total_size = ROUND_UP(total_size, cluster_size);
2667 /* header: 1 cluster */
2668 meta_size += cluster_size;
2670 /* total size of L2 tables */
2671 nl2e = aligned_total_size / cluster_size;
2672 nl2e = ROUND_UP(nl2e, cluster_size / sizeof(uint64_t));
2673 meta_size += nl2e * sizeof(uint64_t);
2675 /* total size of L1 tables */
2676 nl1e = nl2e * sizeof(uint64_t) / cluster_size;
2677 nl1e = ROUND_UP(nl1e, cluster_size / sizeof(uint64_t));
2678 meta_size += nl1e * sizeof(uint64_t);
2680 /* total size of refcount table and blocks */
2681 meta_size += qcow2_refcount_metadata_size(
2682 (meta_size + aligned_total_size) / cluster_size,
2683 cluster_size, refcount_order, false, NULL);
2685 return meta_size + aligned_total_size;
2688 static bool validate_cluster_size(size_t cluster_size, Error **errp)
2690 int cluster_bits = ctz32(cluster_size);
2691 if (cluster_bits < MIN_CLUSTER_BITS || cluster_bits > MAX_CLUSTER_BITS ||
2692 (1 << cluster_bits) != cluster_size)
2694 error_setg(errp, "Cluster size must be a power of two between %d and "
2695 "%dk", 1 << MIN_CLUSTER_BITS, 1 << (MAX_CLUSTER_BITS - 10));
2701 static size_t qcow2_opt_get_cluster_size_del(QemuOpts *opts, Error **errp)
2703 size_t cluster_size;
2705 cluster_size = qemu_opt_get_size_del(opts, BLOCK_OPT_CLUSTER_SIZE,
2706 DEFAULT_CLUSTER_SIZE);
2707 if (!validate_cluster_size(cluster_size, errp)) {
2710 return cluster_size;
2713 static int qcow2_opt_get_version_del(QemuOpts *opts, Error **errp)
2718 buf = qemu_opt_get_del(opts, BLOCK_OPT_COMPAT_LEVEL);
2720 ret = 3; /* default */
2721 } else if (!strcmp(buf, "0.10")) {
2723 } else if (!strcmp(buf, "1.1")) {
2726 error_setg(errp, "Invalid compatibility level: '%s'", buf);
2733 static uint64_t qcow2_opt_get_refcount_bits_del(QemuOpts *opts, int version,
2736 uint64_t refcount_bits;
2738 refcount_bits = qemu_opt_get_number_del(opts, BLOCK_OPT_REFCOUNT_BITS, 16);
2739 if (refcount_bits > 64 || !is_power_of_2(refcount_bits)) {
2740 error_setg(errp, "Refcount width must be a power of two and may not "
2745 if (version < 3 && refcount_bits != 16) {
2746 error_setg(errp, "Different refcount widths than 16 bits require "
2747 "compatibility level 1.1 or above (use compat=1.1 or "
2752 return refcount_bits;
2755 static int coroutine_fn
2756 qcow2_co_create(BlockdevCreateOptions *create_options, Error **errp)
2758 BlockdevCreateOptionsQcow2 *qcow2_opts;
2762 * Open the image file and write a minimal qcow2 header.
2764 * We keep things simple and start with a zero-sized image. We also
2765 * do without refcount blocks or a L1 table for now. We'll fix the
2766 * inconsistency later.
2768 * We do need a refcount table because growing the refcount table means
2769 * allocating two new refcount blocks - the seconds of which would be at
2770 * 2 GB for 64k clusters, and we don't want to have a 2 GB initial file
2771 * size for any qcow2 image.
2773 BlockBackend *blk = NULL;
2774 BlockDriverState *bs = NULL;
2776 size_t cluster_size;
2779 uint64_t* refcount_table;
2780 Error *local_err = NULL;
2783 assert(create_options->driver == BLOCKDEV_DRIVER_QCOW2);
2784 qcow2_opts = &create_options->u.qcow2;
2786 bs = bdrv_open_blockdev_ref(qcow2_opts->file, errp);
2791 /* Validate options and set default values */
2792 if (!QEMU_IS_ALIGNED(qcow2_opts->size, BDRV_SECTOR_SIZE)) {
2793 error_setg(errp, "Image size must be a multiple of 512 bytes");
2798 if (qcow2_opts->has_version) {
2799 switch (qcow2_opts->version) {
2800 case BLOCKDEV_QCOW2_VERSION_V2:
2803 case BLOCKDEV_QCOW2_VERSION_V3:
2807 g_assert_not_reached();
2813 if (qcow2_opts->has_cluster_size) {
2814 cluster_size = qcow2_opts->cluster_size;
2816 cluster_size = DEFAULT_CLUSTER_SIZE;
2819 if (!validate_cluster_size(cluster_size, errp)) {
2824 if (!qcow2_opts->has_preallocation) {
2825 qcow2_opts->preallocation = PREALLOC_MODE_OFF;
2827 if (qcow2_opts->has_backing_file &&
2828 qcow2_opts->preallocation != PREALLOC_MODE_OFF)
2830 error_setg(errp, "Backing file and preallocation cannot be used at "
2835 if (qcow2_opts->has_backing_fmt && !qcow2_opts->has_backing_file) {
2836 error_setg(errp, "Backing format cannot be used without backing file");
2841 if (!qcow2_opts->has_lazy_refcounts) {
2842 qcow2_opts->lazy_refcounts = false;
2844 if (version < 3 && qcow2_opts->lazy_refcounts) {
2845 error_setg(errp, "Lazy refcounts only supported with compatibility "
2846 "level 1.1 and above (use version=v3 or greater)");
2851 if (!qcow2_opts->has_refcount_bits) {
2852 qcow2_opts->refcount_bits = 16;
2854 if (qcow2_opts->refcount_bits > 64 ||
2855 !is_power_of_2(qcow2_opts->refcount_bits))
2857 error_setg(errp, "Refcount width must be a power of two and may not "
2862 if (version < 3 && qcow2_opts->refcount_bits != 16) {
2863 error_setg(errp, "Different refcount widths than 16 bits require "
2864 "compatibility level 1.1 or above (use version=v3 or "
2869 refcount_order = ctz32(qcow2_opts->refcount_bits);
2872 /* Create BlockBackend to write to the image */
2873 blk = blk_new(BLK_PERM_WRITE | BLK_PERM_RESIZE, BLK_PERM_ALL);
2874 ret = blk_insert_bs(blk, bs, errp);
2878 blk_set_allow_write_beyond_eof(blk, true);
2880 /* Clear the protocol layer and preallocate it if necessary */
2881 ret = blk_truncate(blk, 0, PREALLOC_MODE_OFF, errp);
2886 if (qcow2_opts->preallocation == PREALLOC_MODE_FULL ||
2887 qcow2_opts->preallocation == PREALLOC_MODE_FALLOC)
2889 int64_t prealloc_size =
2890 qcow2_calc_prealloc_size(qcow2_opts->size, cluster_size,
2893 ret = blk_truncate(blk, prealloc_size, qcow2_opts->preallocation, errp);
2899 /* Write the header */
2900 QEMU_BUILD_BUG_ON((1 << MIN_CLUSTER_BITS) < sizeof(*header));
2901 header = g_malloc0(cluster_size);
2902 *header = (QCowHeader) {
2903 .magic = cpu_to_be32(QCOW_MAGIC),
2904 .version = cpu_to_be32(version),
2905 .cluster_bits = cpu_to_be32(ctz32(cluster_size)),
2906 .size = cpu_to_be64(0),
2907 .l1_table_offset = cpu_to_be64(0),
2908 .l1_size = cpu_to_be32(0),
2909 .refcount_table_offset = cpu_to_be64(cluster_size),
2910 .refcount_table_clusters = cpu_to_be32(1),
2911 .refcount_order = cpu_to_be32(refcount_order),
2912 .header_length = cpu_to_be32(sizeof(*header)),
2915 /* We'll update this to correct value later */
2916 header->crypt_method = cpu_to_be32(QCOW_CRYPT_NONE);
2918 if (qcow2_opts->lazy_refcounts) {
2919 header->compatible_features |=
2920 cpu_to_be64(QCOW2_COMPAT_LAZY_REFCOUNTS);
2923 ret = blk_pwrite(blk, 0, header, cluster_size, 0);
2926 error_setg_errno(errp, -ret, "Could not write qcow2 header");
2930 /* Write a refcount table with one refcount block */
2931 refcount_table = g_malloc0(2 * cluster_size);
2932 refcount_table[0] = cpu_to_be64(2 * cluster_size);
2933 ret = blk_pwrite(blk, cluster_size, refcount_table, 2 * cluster_size, 0);
2934 g_free(refcount_table);
2937 error_setg_errno(errp, -ret, "Could not write refcount table");
2945 * And now open the image and make it consistent first (i.e. increase the
2946 * refcount of the cluster that is occupied by the header and the refcount
2949 options = qdict_new();
2950 qdict_put_str(options, "driver", "qcow2");
2951 qdict_put_str(options, "file", bs->node_name);
2952 blk = blk_new_open(NULL, NULL, options,
2953 BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_NO_FLUSH,
2956 error_propagate(errp, local_err);
2961 ret = qcow2_alloc_clusters(blk_bs(blk), 3 * cluster_size);
2963 error_setg_errno(errp, -ret, "Could not allocate clusters for qcow2 "
2964 "header and refcount table");
2967 } else if (ret != 0) {
2968 error_report("Huh, first cluster in empty image is already in use?");
2972 /* Create a full header (including things like feature table) */
2973 ret = qcow2_update_header(blk_bs(blk));
2975 error_setg_errno(errp, -ret, "Could not update qcow2 header");
2979 /* Okay, now that we have a valid image, let's give it the right size */
2980 ret = blk_truncate(blk, qcow2_opts->size, PREALLOC_MODE_OFF, errp);
2982 error_prepend(errp, "Could not resize image: ");
2986 /* Want a backing file? There you go.*/
2987 if (qcow2_opts->has_backing_file) {
2988 const char *backing_format = NULL;
2990 if (qcow2_opts->has_backing_fmt) {
2991 backing_format = BlockdevDriver_str(qcow2_opts->backing_fmt);
2994 ret = bdrv_change_backing_file(blk_bs(blk), qcow2_opts->backing_file,
2997 error_setg_errno(errp, -ret, "Could not assign backing file '%s' "
2998 "with format '%s'", qcow2_opts->backing_file,
3004 /* Want encryption? There you go. */
3005 if (qcow2_opts->has_encrypt) {
3006 ret = qcow2_set_up_encryption(blk_bs(blk), qcow2_opts->encrypt, errp);
3012 /* And if we're supposed to preallocate metadata, do that now */
3013 if (qcow2_opts->preallocation != PREALLOC_MODE_OFF) {
3014 BDRVQcow2State *s = blk_bs(blk)->opaque;
3015 qemu_co_mutex_lock(&s->lock);
3016 ret = preallocate_co(blk_bs(blk), 0, qcow2_opts->size);
3017 qemu_co_mutex_unlock(&s->lock);
3020 error_setg_errno(errp, -ret, "Could not preallocate metadata");
3028 /* Reopen the image without BDRV_O_NO_FLUSH to flush it before returning.
3029 * Using BDRV_O_NO_IO, since encryption is now setup we don't want to
3030 * have to setup decryption context. We're not doing any I/O on the top
3031 * level BlockDriverState, only lower layers, where BDRV_O_NO_IO does
3034 options = qdict_new();
3035 qdict_put_str(options, "driver", "qcow2");
3036 qdict_put_str(options, "file", bs->node_name);
3037 blk = blk_new_open(NULL, NULL, options,
3038 BDRV_O_RDWR | BDRV_O_NO_BACKING | BDRV_O_NO_IO,
3041 error_propagate(errp, local_err);
3053 static int coroutine_fn qcow2_co_create_opts(const char *filename, QemuOpts *opts,
3056 BlockdevCreateOptions *create_options = NULL;
3059 BlockDriverState *bs = NULL;
3060 Error *local_err = NULL;
3064 /* Only the keyval visitor supports the dotted syntax needed for
3065 * encryption, so go through a QDict before getting a QAPI type. Ignore
3066 * options meant for the protocol layer so that the visitor doesn't
3068 qdict = qemu_opts_to_qdict_filtered(opts, NULL, bdrv_qcow2.create_opts,
3071 /* Handle encryption options */
3072 val = qdict_get_try_str(qdict, BLOCK_OPT_ENCRYPT);
3073 if (val && !strcmp(val, "on")) {
3074 qdict_put_str(qdict, BLOCK_OPT_ENCRYPT, "qcow");
3075 } else if (val && !strcmp(val, "off")) {
3076 qdict_del(qdict, BLOCK_OPT_ENCRYPT);
3079 val = qdict_get_try_str(qdict, BLOCK_OPT_ENCRYPT_FORMAT);
3080 if (val && !strcmp(val, "aes")) {
3081 qdict_put_str(qdict, BLOCK_OPT_ENCRYPT_FORMAT, "qcow");
3084 /* Convert compat=0.10/1.1 into compat=v2/v3, to be renamed into
3085 * version=v2/v3 below. */
3086 val = qdict_get_try_str(qdict, BLOCK_OPT_COMPAT_LEVEL);
3087 if (val && !strcmp(val, "0.10")) {
3088 qdict_put_str(qdict, BLOCK_OPT_COMPAT_LEVEL, "v2");
3089 } else if (val && !strcmp(val, "1.1")) {
3090 qdict_put_str(qdict, BLOCK_OPT_COMPAT_LEVEL, "v3");
3093 /* Change legacy command line options into QMP ones */
3094 static const QDictRenames opt_renames[] = {
3095 { BLOCK_OPT_BACKING_FILE, "backing-file" },
3096 { BLOCK_OPT_BACKING_FMT, "backing-fmt" },
3097 { BLOCK_OPT_CLUSTER_SIZE, "cluster-size" },
3098 { BLOCK_OPT_LAZY_REFCOUNTS, "lazy-refcounts" },
3099 { BLOCK_OPT_REFCOUNT_BITS, "refcount-bits" },
3100 { BLOCK_OPT_ENCRYPT, BLOCK_OPT_ENCRYPT_FORMAT },
3101 { BLOCK_OPT_COMPAT_LEVEL, "version" },
3105 if (!qdict_rename_keys(qdict, opt_renames, errp)) {
3110 /* Create and open the file (protocol layer) */
3111 ret = bdrv_create_file(filename, opts, errp);
3116 bs = bdrv_open(filename, NULL, NULL,
3117 BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, errp);
3123 /* Set 'driver' and 'node' options */
3124 qdict_put_str(qdict, "driver", "qcow2");
3125 qdict_put_str(qdict, "file", bs->node_name);
3127 /* Now get the QAPI type BlockdevCreateOptions */
3128 v = qobject_input_visitor_new_flat_confused(qdict, errp);
3134 visit_type_BlockdevCreateOptions(v, NULL, &create_options, &local_err);
3138 error_propagate(errp, local_err);
3143 /* Silently round up size */
3144 create_options->u.qcow2.size = ROUND_UP(create_options->u.qcow2.size,
3147 /* Create the qcow2 image (format layer) */
3148 ret = qcow2_co_create(create_options, errp);
3155 qobject_unref(qdict);
3157 qapi_free_BlockdevCreateOptions(create_options);
3162 static bool is_zero(BlockDriverState *bs, int64_t offset, int64_t bytes)
3167 /* Clamp to image length, before checking status of underlying sectors */
3168 if (offset + bytes > bs->total_sectors * BDRV_SECTOR_SIZE) {
3169 bytes = bs->total_sectors * BDRV_SECTOR_SIZE - offset;
3175 res = bdrv_block_status_above(bs, NULL, offset, bytes, &nr, NULL, NULL);
3176 return res >= 0 && (res & BDRV_BLOCK_ZERO) && nr == bytes;
3179 static coroutine_fn int qcow2_co_pwrite_zeroes(BlockDriverState *bs,
3180 int64_t offset, int bytes, BdrvRequestFlags flags)
3183 BDRVQcow2State *s = bs->opaque;
3185 uint32_t head = offset % s->cluster_size;
3186 uint32_t tail = (offset + bytes) % s->cluster_size;
3188 trace_qcow2_pwrite_zeroes_start_req(qemu_coroutine_self(), offset, bytes);
3189 if (offset + bytes == bs->total_sectors * BDRV_SECTOR_SIZE) {
3197 assert(head + bytes <= s->cluster_size);
3199 /* check whether remainder of cluster already reads as zero */
3200 if (!(is_zero(bs, offset - head, head) &&
3201 is_zero(bs, offset + bytes,
3202 tail ? s->cluster_size - tail : 0))) {
3206 qemu_co_mutex_lock(&s->lock);
3207 /* We can have new write after previous check */
3208 offset = QEMU_ALIGN_DOWN(offset, s->cluster_size);
3209 bytes = s->cluster_size;
3210 nr = s->cluster_size;
3211 ret = qcow2_get_cluster_offset(bs, offset, &nr, &off);
3212 if (ret != QCOW2_CLUSTER_UNALLOCATED &&
3213 ret != QCOW2_CLUSTER_ZERO_PLAIN &&
3214 ret != QCOW2_CLUSTER_ZERO_ALLOC) {
3215 qemu_co_mutex_unlock(&s->lock);
3219 qemu_co_mutex_lock(&s->lock);
3222 trace_qcow2_pwrite_zeroes(qemu_coroutine_self(), offset, bytes);
3224 /* Whatever is left can use real zero clusters */
3225 ret = qcow2_cluster_zeroize(bs, offset, bytes, flags);
3226 qemu_co_mutex_unlock(&s->lock);
3231 static coroutine_fn int qcow2_co_pdiscard(BlockDriverState *bs,
3232 int64_t offset, int bytes)
3235 BDRVQcow2State *s = bs->opaque;
3237 if (!QEMU_IS_ALIGNED(offset | bytes, s->cluster_size)) {
3238 assert(bytes < s->cluster_size);
3239 /* Ignore partial clusters, except for the special case of the
3240 * complete partial cluster at the end of an unaligned file */
3241 if (!QEMU_IS_ALIGNED(offset, s->cluster_size) ||
3242 offset + bytes != bs->total_sectors * BDRV_SECTOR_SIZE) {
3247 qemu_co_mutex_lock(&s->lock);
3248 ret = qcow2_cluster_discard(bs, offset, bytes, QCOW2_DISCARD_REQUEST,
3250 qemu_co_mutex_unlock(&s->lock);
3254 static int coroutine_fn
3255 qcow2_co_copy_range_from(BlockDriverState *bs,
3256 BdrvChild *src, uint64_t src_offset,
3257 BdrvChild *dst, uint64_t dst_offset,
3258 uint64_t bytes, BdrvRequestFlags read_flags,
3259 BdrvRequestFlags write_flags)
3261 BDRVQcow2State *s = bs->opaque;
3263 unsigned int cur_bytes; /* number of bytes in current iteration */
3264 BdrvChild *child = NULL;
3265 BdrvRequestFlags cur_write_flags;
3267 assert(!bs->encrypted);
3268 qemu_co_mutex_lock(&s->lock);
3270 while (bytes != 0) {
3271 uint64_t copy_offset = 0;
3272 /* prepare next request */
3273 cur_bytes = MIN(bytes, INT_MAX);
3274 cur_write_flags = write_flags;
3276 ret = qcow2_get_cluster_offset(bs, src_offset, &cur_bytes, ©_offset);
3282 case QCOW2_CLUSTER_UNALLOCATED:
3283 if (bs->backing && bs->backing->bs) {
3284 int64_t backing_length = bdrv_getlength(bs->backing->bs);
3285 if (src_offset >= backing_length) {
3286 cur_write_flags |= BDRV_REQ_ZERO_WRITE;
3288 child = bs->backing;
3289 cur_bytes = MIN(cur_bytes, backing_length - src_offset);
3290 copy_offset = src_offset;
3293 cur_write_flags |= BDRV_REQ_ZERO_WRITE;
3297 case QCOW2_CLUSTER_ZERO_PLAIN:
3298 case QCOW2_CLUSTER_ZERO_ALLOC:
3299 cur_write_flags |= BDRV_REQ_ZERO_WRITE;
3302 case QCOW2_CLUSTER_COMPRESSED:
3306 case QCOW2_CLUSTER_NORMAL:
3308 copy_offset += offset_into_cluster(s, src_offset);
3309 if ((copy_offset & 511) != 0) {
3318 qemu_co_mutex_unlock(&s->lock);
3319 ret = bdrv_co_copy_range_from(child,
3322 cur_bytes, read_flags, cur_write_flags);
3323 qemu_co_mutex_lock(&s->lock);
3329 src_offset += cur_bytes;
3330 dst_offset += cur_bytes;
3335 qemu_co_mutex_unlock(&s->lock);
3339 static int coroutine_fn
3340 qcow2_co_copy_range_to(BlockDriverState *bs,
3341 BdrvChild *src, uint64_t src_offset,
3342 BdrvChild *dst, uint64_t dst_offset,
3343 uint64_t bytes, BdrvRequestFlags read_flags,
3344 BdrvRequestFlags write_flags)
3346 BDRVQcow2State *s = bs->opaque;
3347 int offset_in_cluster;
3349 unsigned int cur_bytes; /* number of sectors in current iteration */
3350 uint64_t cluster_offset;
3351 QCowL2Meta *l2meta = NULL;
3353 assert(!bs->encrypted);
3354 s->cluster_cache_offset = -1; /* disable compressed cache */
3356 qemu_co_mutex_lock(&s->lock);
3358 while (bytes != 0) {
3362 offset_in_cluster = offset_into_cluster(s, dst_offset);
3363 cur_bytes = MIN(bytes, INT_MAX);
3366 * If src->bs == dst->bs, we could simply copy by incrementing
3367 * the refcnt, without copying user data.
3368 * Or if src->bs == dst->bs->backing->bs, we could copy by discarding. */
3369 ret = qcow2_alloc_cluster_offset(bs, dst_offset, &cur_bytes,
3370 &cluster_offset, &l2meta);
3375 assert((cluster_offset & 511) == 0);
3377 ret = qcow2_pre_write_overlap_check(bs, 0,
3378 cluster_offset + offset_in_cluster, cur_bytes);
3383 qemu_co_mutex_unlock(&s->lock);
3384 ret = bdrv_co_copy_range_to(src, src_offset,
3386 cluster_offset + offset_in_cluster,
3387 cur_bytes, read_flags, write_flags);
3388 qemu_co_mutex_lock(&s->lock);
3393 ret = qcow2_handle_l2meta(bs, &l2meta, true);
3399 src_offset += cur_bytes;
3400 dst_offset += cur_bytes;
3405 qcow2_handle_l2meta(bs, &l2meta, false);
3407 qemu_co_mutex_unlock(&s->lock);
3409 trace_qcow2_writev_done_req(qemu_coroutine_self(), ret);
3414 static int coroutine_fn qcow2_co_truncate(BlockDriverState *bs, int64_t offset,
3415 PreallocMode prealloc, Error **errp)
3417 BDRVQcow2State *s = bs->opaque;
3418 uint64_t old_length;
3419 int64_t new_l1_size;
3422 if (prealloc != PREALLOC_MODE_OFF && prealloc != PREALLOC_MODE_METADATA &&
3423 prealloc != PREALLOC_MODE_FALLOC && prealloc != PREALLOC_MODE_FULL)
3425 error_setg(errp, "Unsupported preallocation mode '%s'",
3426 PreallocMode_str(prealloc));
3431 error_setg(errp, "The new size must be a multiple of 512");
3435 qemu_co_mutex_lock(&s->lock);
3437 /* cannot proceed if image has snapshots */
3438 if (s->nb_snapshots) {
3439 error_setg(errp, "Can't resize an image which has snapshots");
3444 /* cannot proceed if image has bitmaps */
3445 if (s->nb_bitmaps) {
3446 /* TODO: resize bitmaps in the image */
3447 error_setg(errp, "Can't resize an image which has bitmaps");
3452 old_length = bs->total_sectors * 512;
3453 new_l1_size = size_to_l1(s, offset);
3455 if (offset < old_length) {
3456 int64_t last_cluster, old_file_size;
3457 if (prealloc != PREALLOC_MODE_OFF) {
3459 "Preallocation can't be used for shrinking an image");
3464 ret = qcow2_cluster_discard(bs, ROUND_UP(offset, s->cluster_size),
3465 old_length - ROUND_UP(offset,
3467 QCOW2_DISCARD_ALWAYS, true);
3469 error_setg_errno(errp, -ret, "Failed to discard cropped clusters");
3473 ret = qcow2_shrink_l1_table(bs, new_l1_size);
3475 error_setg_errno(errp, -ret,
3476 "Failed to reduce the number of L2 tables");
3480 ret = qcow2_shrink_reftable(bs);
3482 error_setg_errno(errp, -ret,
3483 "Failed to discard unused refblocks");
3487 old_file_size = bdrv_getlength(bs->file->bs);
3488 if (old_file_size < 0) {
3489 error_setg_errno(errp, -old_file_size,
3490 "Failed to inquire current file length");
3491 ret = old_file_size;
3494 last_cluster = qcow2_get_last_cluster(bs, old_file_size);
3495 if (last_cluster < 0) {
3496 error_setg_errno(errp, -last_cluster,
3497 "Failed to find the last cluster");
3501 if ((last_cluster + 1) * s->cluster_size < old_file_size) {
3502 Error *local_err = NULL;
3504 bdrv_co_truncate(bs->file, (last_cluster + 1) * s->cluster_size,
3505 PREALLOC_MODE_OFF, &local_err);
3507 warn_reportf_err(local_err,
3508 "Failed to truncate the tail of the image: ");
3512 ret = qcow2_grow_l1_table(bs, new_l1_size, true);
3514 error_setg_errno(errp, -ret, "Failed to grow the L1 table");
3520 case PREALLOC_MODE_OFF:
3523 case PREALLOC_MODE_METADATA:
3524 ret = preallocate_co(bs, old_length, offset);
3526 error_setg_errno(errp, -ret, "Preallocation failed");
3531 case PREALLOC_MODE_FALLOC:
3532 case PREALLOC_MODE_FULL:
3534 int64_t allocation_start, host_offset, guest_offset;
3535 int64_t clusters_allocated;
3536 int64_t old_file_size, new_file_size;
3537 uint64_t nb_new_data_clusters, nb_new_l2_tables;
3539 old_file_size = bdrv_getlength(bs->file->bs);
3540 if (old_file_size < 0) {
3541 error_setg_errno(errp, -old_file_size,
3542 "Failed to inquire current file length");
3543 ret = old_file_size;
3546 old_file_size = ROUND_UP(old_file_size, s->cluster_size);
3548 nb_new_data_clusters = DIV_ROUND_UP(offset - old_length,
3551 /* This is an overestimation; we will not actually allocate space for
3552 * these in the file but just make sure the new refcount structures are
3553 * able to cover them so we will not have to allocate new refblocks
3554 * while entering the data blocks in the potentially new L2 tables.
3555 * (We do not actually care where the L2 tables are placed. Maybe they
3556 * are already allocated or they can be placed somewhere before
3557 * @old_file_size. It does not matter because they will be fully
3558 * allocated automatically, so they do not need to be covered by the
3559 * preallocation. All that matters is that we will not have to allocate
3560 * new refcount structures for them.) */
3561 nb_new_l2_tables = DIV_ROUND_UP(nb_new_data_clusters,
3562 s->cluster_size / sizeof(uint64_t));
3563 /* The cluster range may not be aligned to L2 boundaries, so add one L2
3564 * table for a potential head/tail */
3567 allocation_start = qcow2_refcount_area(bs, old_file_size,
3568 nb_new_data_clusters +
3571 if (allocation_start < 0) {
3572 error_setg_errno(errp, -allocation_start,
3573 "Failed to resize refcount structures");
3574 ret = allocation_start;
3578 clusters_allocated = qcow2_alloc_clusters_at(bs, allocation_start,
3579 nb_new_data_clusters);
3580 if (clusters_allocated < 0) {
3581 error_setg_errno(errp, -clusters_allocated,
3582 "Failed to allocate data clusters");
3583 ret = clusters_allocated;
3587 assert(clusters_allocated == nb_new_data_clusters);
3589 /* Allocate the data area */
3590 new_file_size = allocation_start +
3591 nb_new_data_clusters * s->cluster_size;
3592 ret = bdrv_co_truncate(bs->file, new_file_size, prealloc, errp);
3594 error_prepend(errp, "Failed to resize underlying file: ");
3595 qcow2_free_clusters(bs, allocation_start,
3596 nb_new_data_clusters * s->cluster_size,
3597 QCOW2_DISCARD_OTHER);
3601 /* Create the necessary L2 entries */
3602 host_offset = allocation_start;
3603 guest_offset = old_length;
3604 while (nb_new_data_clusters) {
3605 int64_t nb_clusters = MIN(
3606 nb_new_data_clusters,
3607 s->l2_slice_size - offset_to_l2_slice_index(s, guest_offset));
3608 QCowL2Meta allocation = {
3609 .offset = guest_offset,
3610 .alloc_offset = host_offset,
3611 .nb_clusters = nb_clusters,
3613 qemu_co_queue_init(&allocation.dependent_requests);
3615 ret = qcow2_alloc_cluster_link_l2(bs, &allocation);
3617 error_setg_errno(errp, -ret, "Failed to update L2 tables");
3618 qcow2_free_clusters(bs, host_offset,
3619 nb_new_data_clusters * s->cluster_size,
3620 QCOW2_DISCARD_OTHER);
3624 guest_offset += nb_clusters * s->cluster_size;
3625 host_offset += nb_clusters * s->cluster_size;
3626 nb_new_data_clusters -= nb_clusters;
3632 g_assert_not_reached();
3635 if (prealloc != PREALLOC_MODE_OFF) {
3636 /* Flush metadata before actually changing the image size */
3637 ret = qcow2_write_caches(bs);
3639 error_setg_errno(errp, -ret,
3640 "Failed to flush the preallocated area to disk");
3645 /* write updated header.size */
3646 offset = cpu_to_be64(offset);
3647 ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, size),
3648 &offset, sizeof(uint64_t));
3650 error_setg_errno(errp, -ret, "Failed to update the image size");
3654 s->l1_vm_state_index = new_l1_size;
3657 qemu_co_mutex_unlock(&s->lock);
3664 * @dest - destination buffer, at least of @size-1 bytes
3665 * @src - source buffer, @size bytes
3667 * Returns: compressed size on success
3668 * -1 if compression is inefficient
3669 * -2 on any other error
3671 static ssize_t qcow2_compress(void *dest, const void *src, size_t size)
3676 /* best compression, small window, no zlib header */
3677 memset(&strm, 0, sizeof(strm));
3678 ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
3679 -12, 9, Z_DEFAULT_STRATEGY);
3684 /* strm.next_in is not const in old zlib versions, such as those used on
3685 * OpenBSD/NetBSD, so cast the const away */
3686 strm.avail_in = size;
3687 strm.next_in = (void *) src;
3688 strm.avail_out = size - 1;
3689 strm.next_out = dest;
3691 ret = deflate(&strm, Z_FINISH);
3692 if (ret == Z_STREAM_END) {
3693 ret = size - 1 - strm.avail_out;
3695 ret = (ret == Z_OK ? -1 : -2);
3703 #define MAX_COMPRESS_THREADS 4
3705 typedef struct Qcow2CompressData {
3710 } Qcow2CompressData;
3712 static int qcow2_compress_pool_func(void *opaque)
3714 Qcow2CompressData *data = opaque;
3716 data->ret = qcow2_compress(data->dest, data->src, data->size);
3721 static void qcow2_compress_complete(void *opaque, int ret)
3723 qemu_coroutine_enter(opaque);
3726 /* See qcow2_compress definition for parameters description */
3727 static ssize_t qcow2_co_compress(BlockDriverState *bs,
3728 void *dest, const void *src, size_t size)
3730 BDRVQcow2State *s = bs->opaque;
3732 ThreadPool *pool = aio_get_thread_pool(bdrv_get_aio_context(bs));
3733 Qcow2CompressData arg = {
3739 while (s->nb_compress_threads >= MAX_COMPRESS_THREADS) {
3740 qemu_co_queue_wait(&s->compress_wait_queue, NULL);
3743 s->nb_compress_threads++;
3744 acb = thread_pool_submit_aio(pool, qcow2_compress_pool_func, &arg,
3745 qcow2_compress_complete,
3746 qemu_coroutine_self());
3749 s->nb_compress_threads--;
3752 qemu_coroutine_yield();
3753 s->nb_compress_threads--;
3754 qemu_co_queue_next(&s->compress_wait_queue);
3759 /* XXX: put compressed sectors first, then all the cluster aligned
3760 tables to avoid losing bytes in alignment */
3761 static coroutine_fn int
3762 qcow2_co_pwritev_compressed(BlockDriverState *bs, uint64_t offset,
3763 uint64_t bytes, QEMUIOVector *qiov)
3765 BDRVQcow2State *s = bs->opaque;
3766 QEMUIOVector hd_qiov;
3770 uint8_t *buf, *out_buf;
3771 int64_t cluster_offset;
3774 /* align end of file to a sector boundary to ease reading with
3775 sector based I/Os */
3776 cluster_offset = bdrv_getlength(bs->file->bs);
3777 if (cluster_offset < 0) {
3778 return cluster_offset;
3780 return bdrv_co_truncate(bs->file, cluster_offset, PREALLOC_MODE_OFF,
3784 if (offset_into_cluster(s, offset)) {
3788 buf = qemu_blockalign(bs, s->cluster_size);
3789 if (bytes != s->cluster_size) {
3790 if (bytes > s->cluster_size ||
3791 offset + bytes != bs->total_sectors << BDRV_SECTOR_BITS)
3796 /* Zero-pad last write if image size is not cluster aligned */
3797 memset(buf + bytes, 0, s->cluster_size - bytes);
3799 qemu_iovec_to_buf(qiov, 0, buf, bytes);
3801 out_buf = g_malloc(s->cluster_size);
3803 out_len = qcow2_co_compress(bs, out_buf, buf, s->cluster_size);
3804 if (out_len == -2) {
3807 } else if (out_len == -1) {
3808 /* could not compress: write normal cluster */
3809 ret = qcow2_co_pwritev(bs, offset, bytes, qiov, 0);
3816 qemu_co_mutex_lock(&s->lock);
3818 qcow2_alloc_compressed_cluster_offset(bs, offset, out_len);
3819 if (!cluster_offset) {
3820 qemu_co_mutex_unlock(&s->lock);
3824 cluster_offset &= s->cluster_offset_mask;
3826 ret = qcow2_pre_write_overlap_check(bs, 0, cluster_offset, out_len);
3827 qemu_co_mutex_unlock(&s->lock);
3832 iov = (struct iovec) {
3833 .iov_base = out_buf,
3836 qemu_iovec_init_external(&hd_qiov, &iov, 1);
3838 BLKDBG_EVENT(bs->file, BLKDBG_WRITE_COMPRESSED);
3839 ret = bdrv_co_pwritev(bs->file, cluster_offset, out_len, &hd_qiov, 0);
3851 static int make_completely_empty(BlockDriverState *bs)
3853 BDRVQcow2State *s = bs->opaque;
3854 Error *local_err = NULL;
3855 int ret, l1_clusters;
3857 uint64_t *new_reftable = NULL;
3858 uint64_t rt_entry, l1_size2;
3861 uint64_t reftable_offset;
3862 uint32_t reftable_clusters;
3863 } QEMU_PACKED l1_ofs_rt_ofs_cls;
3865 ret = qcow2_cache_empty(bs, s->l2_table_cache);
3870 ret = qcow2_cache_empty(bs, s->refcount_block_cache);
3875 /* Refcounts will be broken utterly */
3876 ret = qcow2_mark_dirty(bs);
3881 BLKDBG_EVENT(bs->file, BLKDBG_L1_UPDATE);
3883 l1_clusters = DIV_ROUND_UP(s->l1_size, s->cluster_size / sizeof(uint64_t));
3884 l1_size2 = (uint64_t)s->l1_size * sizeof(uint64_t);
3886 /* After this call, neither the in-memory nor the on-disk refcount
3887 * information accurately describe the actual references */
3889 ret = bdrv_pwrite_zeroes(bs->file, s->l1_table_offset,
3890 l1_clusters * s->cluster_size, 0);
3892 goto fail_broken_refcounts;
3894 memset(s->l1_table, 0, l1_size2);
3896 BLKDBG_EVENT(bs->file, BLKDBG_EMPTY_IMAGE_PREPARE);
3898 /* Overwrite enough clusters at the beginning of the sectors to place
3899 * the refcount table, a refcount block and the L1 table in; this may
3900 * overwrite parts of the existing refcount and L1 table, which is not
3901 * an issue because the dirty flag is set, complete data loss is in fact
3902 * desired and partial data loss is consequently fine as well */
3903 ret = bdrv_pwrite_zeroes(bs->file, s->cluster_size,
3904 (2 + l1_clusters) * s->cluster_size, 0);
3905 /* This call (even if it failed overall) may have overwritten on-disk
3906 * refcount structures; in that case, the in-memory refcount information
3907 * will probably differ from the on-disk information which makes the BDS
3910 goto fail_broken_refcounts;
3913 BLKDBG_EVENT(bs->file, BLKDBG_L1_UPDATE);
3914 BLKDBG_EVENT(bs->file, BLKDBG_REFTABLE_UPDATE);
3916 /* "Create" an empty reftable (one cluster) directly after the image
3917 * header and an empty L1 table three clusters after the image header;
3918 * the cluster between those two will be used as the first refblock */
3919 l1_ofs_rt_ofs_cls.l1_offset = cpu_to_be64(3 * s->cluster_size);
3920 l1_ofs_rt_ofs_cls.reftable_offset = cpu_to_be64(s->cluster_size);
3921 l1_ofs_rt_ofs_cls.reftable_clusters = cpu_to_be32(1);
3922 ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, l1_table_offset),
3923 &l1_ofs_rt_ofs_cls, sizeof(l1_ofs_rt_ofs_cls));
3925 goto fail_broken_refcounts;
3928 s->l1_table_offset = 3 * s->cluster_size;
3930 new_reftable = g_try_new0(uint64_t, s->cluster_size / sizeof(uint64_t));
3931 if (!new_reftable) {
3933 goto fail_broken_refcounts;
3936 s->refcount_table_offset = s->cluster_size;
3937 s->refcount_table_size = s->cluster_size / sizeof(uint64_t);
3938 s->max_refcount_table_index = 0;
3940 g_free(s->refcount_table);
3941 s->refcount_table = new_reftable;
3942 new_reftable = NULL;
3944 /* Now the in-memory refcount information again corresponds to the on-disk
3945 * information (reftable is empty and no refblocks (the refblock cache is
3946 * empty)); however, this means some clusters (e.g. the image header) are
3947 * referenced, but not refcounted, but the normal qcow2 code assumes that
3948 * the in-memory information is always correct */
3950 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC);
3952 /* Enter the first refblock into the reftable */
3953 rt_entry = cpu_to_be64(2 * s->cluster_size);
3954 ret = bdrv_pwrite_sync(bs->file, s->cluster_size,
3955 &rt_entry, sizeof(rt_entry));
3957 goto fail_broken_refcounts;
3959 s->refcount_table[0] = 2 * s->cluster_size;
3961 s->free_cluster_index = 0;
3962 assert(3 + l1_clusters <= s->refcount_block_size);
3963 offset = qcow2_alloc_clusters(bs, 3 * s->cluster_size + l1_size2);
3966 goto fail_broken_refcounts;
3967 } else if (offset > 0) {
3968 error_report("First cluster in emptied image is in use");
3972 /* Now finally the in-memory information corresponds to the on-disk
3973 * structures and is correct */
3974 ret = qcow2_mark_clean(bs);
3979 ret = bdrv_truncate(bs->file, (3 + l1_clusters) * s->cluster_size,
3980 PREALLOC_MODE_OFF, &local_err);
3982 error_report_err(local_err);
3988 fail_broken_refcounts:
3989 /* The BDS is unusable at this point. If we wanted to make it usable, we
3990 * would have to call qcow2_refcount_close(), qcow2_refcount_init(),
3991 * qcow2_check_refcounts(), qcow2_refcount_close() and qcow2_refcount_init()
3992 * again. However, because the functions which could have caused this error
3993 * path to be taken are used by those functions as well, it's very likely
3994 * that that sequence will fail as well. Therefore, just eject the BDS. */
3998 g_free(new_reftable);
4002 static int qcow2_make_empty(BlockDriverState *bs)
4004 BDRVQcow2State *s = bs->opaque;
4005 uint64_t offset, end_offset;
4006 int step = QEMU_ALIGN_DOWN(INT_MAX, s->cluster_size);
4007 int l1_clusters, ret = 0;
4009 l1_clusters = DIV_ROUND_UP(s->l1_size, s->cluster_size / sizeof(uint64_t));
4011 if (s->qcow_version >= 3 && !s->snapshots && !s->nb_bitmaps &&
4012 3 + l1_clusters <= s->refcount_block_size &&
4013 s->crypt_method_header != QCOW_CRYPT_LUKS) {
4014 /* The following function only works for qcow2 v3 images (it
4015 * requires the dirty flag) and only as long as there are no
4016 * features that reserve extra clusters (such as snapshots,
4017 * LUKS header, or persistent bitmaps), because it completely
4018 * empties the image. Furthermore, the L1 table and three
4019 * additional clusters (image header, refcount table, one
4020 * refcount block) have to fit inside one refcount block. */
4021 return make_completely_empty(bs);
4024 /* This fallback code simply discards every active cluster; this is slow,
4025 * but works in all cases */
4026 end_offset = bs->total_sectors * BDRV_SECTOR_SIZE;
4027 for (offset = 0; offset < end_offset; offset += step) {
4028 /* As this function is generally used after committing an external
4029 * snapshot, QCOW2_DISCARD_SNAPSHOT seems appropriate. Also, the
4030 * default action for this kind of discard is to pass the discard,
4031 * which will ideally result in an actually smaller image file, as
4032 * is probably desired. */
4033 ret = qcow2_cluster_discard(bs, offset, MIN(step, end_offset - offset),
4034 QCOW2_DISCARD_SNAPSHOT, true);
4043 static coroutine_fn int qcow2_co_flush_to_os(BlockDriverState *bs)
4045 BDRVQcow2State *s = bs->opaque;
4048 qemu_co_mutex_lock(&s->lock);
4049 ret = qcow2_write_caches(bs);
4050 qemu_co_mutex_unlock(&s->lock);
4055 static BlockMeasureInfo *qcow2_measure(QemuOpts *opts, BlockDriverState *in_bs,
4058 Error *local_err = NULL;
4059 BlockMeasureInfo *info;
4060 uint64_t required = 0; /* bytes that contribute to required size */
4061 uint64_t virtual_size; /* disk size as seen by guest */
4062 uint64_t refcount_bits;
4064 size_t cluster_size;
4067 PreallocMode prealloc;
4068 bool has_backing_file;
4070 /* Parse image creation options */
4071 cluster_size = qcow2_opt_get_cluster_size_del(opts, &local_err);
4076 version = qcow2_opt_get_version_del(opts, &local_err);
4081 refcount_bits = qcow2_opt_get_refcount_bits_del(opts, version, &local_err);
4086 optstr = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC);
4087 prealloc = qapi_enum_parse(&PreallocMode_lookup, optstr,
4088 PREALLOC_MODE_OFF, &local_err);
4094 optstr = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE);
4095 has_backing_file = !!optstr;
4098 virtual_size = qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0);
4099 virtual_size = ROUND_UP(virtual_size, cluster_size);
4101 /* Check that virtual disk size is valid */
4102 l2_tables = DIV_ROUND_UP(virtual_size / cluster_size,
4103 cluster_size / sizeof(uint64_t));
4104 if (l2_tables * sizeof(uint64_t) > QCOW_MAX_L1_SIZE) {
4105 error_setg(&local_err, "The image size is too large "
4106 "(try using a larger cluster size)");
4110 /* Account for input image */
4112 int64_t ssize = bdrv_getlength(in_bs);
4114 error_setg_errno(&local_err, -ssize,
4115 "Unable to get image virtual_size");
4119 virtual_size = ROUND_UP(ssize, cluster_size);
4121 if (has_backing_file) {
4122 /* We don't how much of the backing chain is shared by the input
4123 * image and the new image file. In the worst case the new image's
4124 * backing file has nothing in common with the input image. Be
4125 * conservative and assume all clusters need to be written.
4127 required = virtual_size;
4132 for (offset = 0; offset < ssize; offset += pnum) {
4135 ret = bdrv_block_status_above(in_bs, NULL, offset,
4136 ssize - offset, &pnum, NULL,
4139 error_setg_errno(&local_err, -ret,
4140 "Unable to get block status");
4144 if (ret & BDRV_BLOCK_ZERO) {
4145 /* Skip zero regions (safe with no backing file) */
4146 } else if ((ret & (BDRV_BLOCK_DATA | BDRV_BLOCK_ALLOCATED)) ==
4147 (BDRV_BLOCK_DATA | BDRV_BLOCK_ALLOCATED)) {
4148 /* Extend pnum to end of cluster for next iteration */
4149 pnum = ROUND_UP(offset + pnum, cluster_size) - offset;
4151 /* Count clusters we've seen */
4152 required += offset % cluster_size + pnum;
4158 /* Take into account preallocation. Nothing special is needed for
4159 * PREALLOC_MODE_METADATA since metadata is always counted.
4161 if (prealloc == PREALLOC_MODE_FULL || prealloc == PREALLOC_MODE_FALLOC) {
4162 required = virtual_size;
4165 info = g_new(BlockMeasureInfo, 1);
4166 info->fully_allocated =
4167 qcow2_calc_prealloc_size(virtual_size, cluster_size,
4168 ctz32(refcount_bits));
4170 /* Remove data clusters that are not required. This overestimates the
4171 * required size because metadata needed for the fully allocated file is
4174 info->required = info->fully_allocated - virtual_size + required;
4178 error_propagate(errp, local_err);
4182 static int qcow2_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
4184 BDRVQcow2State *s = bs->opaque;
4185 bdi->unallocated_blocks_are_zero = true;
4186 bdi->cluster_size = s->cluster_size;
4187 bdi->vm_state_offset = qcow2_vm_state_offset(s);
4191 static ImageInfoSpecific *qcow2_get_specific_info(BlockDriverState *bs)
4193 BDRVQcow2State *s = bs->opaque;
4194 ImageInfoSpecific *spec_info;
4195 QCryptoBlockInfo *encrypt_info = NULL;
4197 if (s->crypto != NULL) {
4198 encrypt_info = qcrypto_block_get_info(s->crypto, &error_abort);
4201 spec_info = g_new(ImageInfoSpecific, 1);
4202 *spec_info = (ImageInfoSpecific){
4203 .type = IMAGE_INFO_SPECIFIC_KIND_QCOW2,
4204 .u.qcow2.data = g_new(ImageInfoSpecificQCow2, 1),
4206 if (s->qcow_version == 2) {
4207 *spec_info->u.qcow2.data = (ImageInfoSpecificQCow2){
4208 .compat = g_strdup("0.10"),
4209 .refcount_bits = s->refcount_bits,
4211 } else if (s->qcow_version == 3) {
4212 *spec_info->u.qcow2.data = (ImageInfoSpecificQCow2){
4213 .compat = g_strdup("1.1"),
4214 .lazy_refcounts = s->compatible_features &
4215 QCOW2_COMPAT_LAZY_REFCOUNTS,
4216 .has_lazy_refcounts = true,
4217 .corrupt = s->incompatible_features &
4218 QCOW2_INCOMPAT_CORRUPT,
4219 .has_corrupt = true,
4220 .refcount_bits = s->refcount_bits,
4223 /* if this assertion fails, this probably means a new version was
4224 * added without having it covered here */
4229 ImageInfoSpecificQCow2Encryption *qencrypt =
4230 g_new(ImageInfoSpecificQCow2Encryption, 1);
4231 switch (encrypt_info->format) {
4232 case Q_CRYPTO_BLOCK_FORMAT_QCOW:
4233 qencrypt->format = BLOCKDEV_QCOW2_ENCRYPTION_FORMAT_AES;
4235 case Q_CRYPTO_BLOCK_FORMAT_LUKS:
4236 qencrypt->format = BLOCKDEV_QCOW2_ENCRYPTION_FORMAT_LUKS;
4237 qencrypt->u.luks = encrypt_info->u.luks;
4242 /* Since we did shallow copy above, erase any pointers
4243 * in the original info */
4244 memset(&encrypt_info->u, 0, sizeof(encrypt_info->u));
4245 qapi_free_QCryptoBlockInfo(encrypt_info);
4247 spec_info->u.qcow2.data->has_encrypt = true;
4248 spec_info->u.qcow2.data->encrypt = qencrypt;
4254 static int qcow2_save_vmstate(BlockDriverState *bs, QEMUIOVector *qiov,
4257 BDRVQcow2State *s = bs->opaque;
4259 BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_SAVE);
4260 return bs->drv->bdrv_co_pwritev(bs, qcow2_vm_state_offset(s) + pos,
4261 qiov->size, qiov, 0);
4264 static int qcow2_load_vmstate(BlockDriverState *bs, QEMUIOVector *qiov,
4267 BDRVQcow2State *s = bs->opaque;
4269 BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_LOAD);
4270 return bs->drv->bdrv_co_preadv(bs, qcow2_vm_state_offset(s) + pos,
4271 qiov->size, qiov, 0);
4275 * Downgrades an image's version. To achieve this, any incompatible features
4276 * have to be removed.
4278 static int qcow2_downgrade(BlockDriverState *bs, int target_version,
4279 BlockDriverAmendStatusCB *status_cb, void *cb_opaque,
4282 BDRVQcow2State *s = bs->opaque;
4283 int current_version = s->qcow_version;
4286 /* This is qcow2_downgrade(), not qcow2_upgrade() */
4287 assert(target_version < current_version);
4289 /* There are no other versions (now) that you can downgrade to */
4290 assert(target_version == 2);
4292 if (s->refcount_order != 4) {
4293 error_setg(errp, "compat=0.10 requires refcount_bits=16");
4297 /* clear incompatible features */
4298 if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) {
4299 ret = qcow2_mark_clean(bs);
4301 error_setg_errno(errp, -ret, "Failed to make the image clean");
4306 /* with QCOW2_INCOMPAT_CORRUPT, it is pretty much impossible to get here in
4307 * the first place; if that happens nonetheless, returning -ENOTSUP is the
4308 * best thing to do anyway */
4310 if (s->incompatible_features) {
4311 error_setg(errp, "Cannot downgrade an image with incompatible features "
4312 "%#" PRIx64 " set", s->incompatible_features);
4316 /* since we can ignore compatible features, we can set them to 0 as well */
4317 s->compatible_features = 0;
4318 /* if lazy refcounts have been used, they have already been fixed through
4319 * clearing the dirty flag */
4321 /* clearing autoclear features is trivial */
4322 s->autoclear_features = 0;
4324 ret = qcow2_expand_zero_clusters(bs, status_cb, cb_opaque);
4326 error_setg_errno(errp, -ret, "Failed to turn zero into data clusters");
4330 s->qcow_version = target_version;
4331 ret = qcow2_update_header(bs);
4333 s->qcow_version = current_version;
4334 error_setg_errno(errp, -ret, "Failed to update the image header");
4340 typedef enum Qcow2AmendOperation {
4341 /* This is the value Qcow2AmendHelperCBInfo::last_operation will be
4342 * statically initialized to so that the helper CB can discern the first
4343 * invocation from an operation change */
4344 QCOW2_NO_OPERATION = 0,
4346 QCOW2_CHANGING_REFCOUNT_ORDER,
4348 } Qcow2AmendOperation;
4350 typedef struct Qcow2AmendHelperCBInfo {
4351 /* The code coordinating the amend operations should only modify
4352 * these four fields; the rest will be managed by the CB */
4353 BlockDriverAmendStatusCB *original_status_cb;
4354 void *original_cb_opaque;
4356 Qcow2AmendOperation current_operation;
4358 /* Total number of operations to perform (only set once) */
4359 int total_operations;
4361 /* The following fields are managed by the CB */
4363 /* Number of operations completed */
4364 int operations_completed;
4366 /* Cumulative offset of all completed operations */
4367 int64_t offset_completed;
4369 Qcow2AmendOperation last_operation;
4370 int64_t last_work_size;
4371 } Qcow2AmendHelperCBInfo;
4373 static void qcow2_amend_helper_cb(BlockDriverState *bs,
4374 int64_t operation_offset,
4375 int64_t operation_work_size, void *opaque)
4377 Qcow2AmendHelperCBInfo *info = opaque;
4378 int64_t current_work_size;
4379 int64_t projected_work_size;
4381 if (info->current_operation != info->last_operation) {
4382 if (info->last_operation != QCOW2_NO_OPERATION) {
4383 info->offset_completed += info->last_work_size;
4384 info->operations_completed++;
4387 info->last_operation = info->current_operation;
4390 assert(info->total_operations > 0);
4391 assert(info->operations_completed < info->total_operations);
4393 info->last_work_size = operation_work_size;
4395 current_work_size = info->offset_completed + operation_work_size;
4397 /* current_work_size is the total work size for (operations_completed + 1)
4398 * operations (which includes this one), so multiply it by the number of
4399 * operations not covered and divide it by the number of operations
4400 * covered to get a projection for the operations not covered */
4401 projected_work_size = current_work_size * (info->total_operations -
4402 info->operations_completed - 1)
4403 / (info->operations_completed + 1);
4405 info->original_status_cb(bs, info->offset_completed + operation_offset,
4406 current_work_size + projected_work_size,
4407 info->original_cb_opaque);
4410 static int qcow2_amend_options(BlockDriverState *bs, QemuOpts *opts,
4411 BlockDriverAmendStatusCB *status_cb,
4415 BDRVQcow2State *s = bs->opaque;
4416 int old_version = s->qcow_version, new_version = old_version;
4417 uint64_t new_size = 0;
4418 const char *backing_file = NULL, *backing_format = NULL;
4419 bool lazy_refcounts = s->use_lazy_refcounts;
4420 const char *compat = NULL;
4421 uint64_t cluster_size = s->cluster_size;
4424 int refcount_bits = s->refcount_bits;
4426 QemuOptDesc *desc = opts->list->desc;
4427 Qcow2AmendHelperCBInfo helper_cb_info;
4429 while (desc && desc->name) {
4430 if (!qemu_opt_find(opts, desc->name)) {
4431 /* only change explicitly defined options */
4436 if (!strcmp(desc->name, BLOCK_OPT_COMPAT_LEVEL)) {
4437 compat = qemu_opt_get(opts, BLOCK_OPT_COMPAT_LEVEL);
4439 /* preserve default */
4440 } else if (!strcmp(compat, "0.10")) {
4442 } else if (!strcmp(compat, "1.1")) {
4445 error_setg(errp, "Unknown compatibility level %s", compat);
4448 } else if (!strcmp(desc->name, BLOCK_OPT_PREALLOC)) {
4449 error_setg(errp, "Cannot change preallocation mode");
4451 } else if (!strcmp(desc->name, BLOCK_OPT_SIZE)) {
4452 new_size = qemu_opt_get_size(opts, BLOCK_OPT_SIZE, 0);
4453 } else if (!strcmp(desc->name, BLOCK_OPT_BACKING_FILE)) {
4454 backing_file = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
4455 } else if (!strcmp(desc->name, BLOCK_OPT_BACKING_FMT)) {
4456 backing_format = qemu_opt_get(opts, BLOCK_OPT_BACKING_FMT);
4457 } else if (!strcmp(desc->name, BLOCK_OPT_ENCRYPT)) {
4458 encrypt = qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT,
4461 if (encrypt != !!s->crypto) {
4463 "Changing the encryption flag is not supported");
4466 } else if (!strcmp(desc->name, BLOCK_OPT_ENCRYPT_FORMAT)) {
4467 encformat = qcow2_crypt_method_from_format(
4468 qemu_opt_get(opts, BLOCK_OPT_ENCRYPT_FORMAT));
4470 if (encformat != s->crypt_method_header) {
4472 "Changing the encryption format is not supported");
4475 } else if (g_str_has_prefix(desc->name, "encrypt.")) {
4477 "Changing the encryption parameters is not supported");
4479 } else if (!strcmp(desc->name, BLOCK_OPT_CLUSTER_SIZE)) {
4480 cluster_size = qemu_opt_get_size(opts, BLOCK_OPT_CLUSTER_SIZE,
4482 if (cluster_size != s->cluster_size) {
4483 error_setg(errp, "Changing the cluster size is not supported");
4486 } else if (!strcmp(desc->name, BLOCK_OPT_LAZY_REFCOUNTS)) {
4487 lazy_refcounts = qemu_opt_get_bool(opts, BLOCK_OPT_LAZY_REFCOUNTS,
4489 } else if (!strcmp(desc->name, BLOCK_OPT_REFCOUNT_BITS)) {
4490 refcount_bits = qemu_opt_get_number(opts, BLOCK_OPT_REFCOUNT_BITS,
4493 if (refcount_bits <= 0 || refcount_bits > 64 ||
4494 !is_power_of_2(refcount_bits))
4496 error_setg(errp, "Refcount width must be a power of two and "
4497 "may not exceed 64 bits");
4501 /* if this point is reached, this probably means a new option was
4502 * added without having it covered here */
4509 helper_cb_info = (Qcow2AmendHelperCBInfo){
4510 .original_status_cb = status_cb,
4511 .original_cb_opaque = cb_opaque,
4512 .total_operations = (new_version < old_version)
4513 + (s->refcount_bits != refcount_bits)
4516 /* Upgrade first (some features may require compat=1.1) */
4517 if (new_version > old_version) {
4518 s->qcow_version = new_version;
4519 ret = qcow2_update_header(bs);
4521 s->qcow_version = old_version;
4522 error_setg_errno(errp, -ret, "Failed to update the image header");
4527 if (s->refcount_bits != refcount_bits) {
4528 int refcount_order = ctz32(refcount_bits);
4530 if (new_version < 3 && refcount_bits != 16) {
4531 error_setg(errp, "Refcount widths other than 16 bits require "
4532 "compatibility level 1.1 or above (use compat=1.1 or "
4537 helper_cb_info.current_operation = QCOW2_CHANGING_REFCOUNT_ORDER;
4538 ret = qcow2_change_refcount_order(bs, refcount_order,
4539 &qcow2_amend_helper_cb,
4540 &helper_cb_info, errp);
4546 if (backing_file || backing_format) {
4547 ret = qcow2_change_backing_file(bs,
4548 backing_file ?: s->image_backing_file,
4549 backing_format ?: s->image_backing_format);
4551 error_setg_errno(errp, -ret, "Failed to change the backing file");
4556 if (s->use_lazy_refcounts != lazy_refcounts) {
4557 if (lazy_refcounts) {
4558 if (new_version < 3) {
4559 error_setg(errp, "Lazy refcounts only supported with "
4560 "compatibility level 1.1 and above (use compat=1.1 "
4564 s->compatible_features |= QCOW2_COMPAT_LAZY_REFCOUNTS;
4565 ret = qcow2_update_header(bs);
4567 s->compatible_features &= ~QCOW2_COMPAT_LAZY_REFCOUNTS;
4568 error_setg_errno(errp, -ret, "Failed to update the image header");
4571 s->use_lazy_refcounts = true;
4573 /* make image clean first */
4574 ret = qcow2_mark_clean(bs);
4576 error_setg_errno(errp, -ret, "Failed to make the image clean");
4579 /* now disallow lazy refcounts */
4580 s->compatible_features &= ~QCOW2_COMPAT_LAZY_REFCOUNTS;
4581 ret = qcow2_update_header(bs);
4583 s->compatible_features |= QCOW2_COMPAT_LAZY_REFCOUNTS;
4584 error_setg_errno(errp, -ret, "Failed to update the image header");
4587 s->use_lazy_refcounts = false;
4592 BlockBackend *blk = blk_new(BLK_PERM_RESIZE, BLK_PERM_ALL);
4593 ret = blk_insert_bs(blk, bs, errp);
4599 ret = blk_truncate(blk, new_size, PREALLOC_MODE_OFF, errp);
4606 /* Downgrade last (so unsupported features can be removed before) */
4607 if (new_version < old_version) {
4608 helper_cb_info.current_operation = QCOW2_DOWNGRADING;
4609 ret = qcow2_downgrade(bs, new_version, &qcow2_amend_helper_cb,
4610 &helper_cb_info, errp);
4620 * If offset or size are negative, respectively, they will not be included in
4621 * the BLOCK_IMAGE_CORRUPTED event emitted.
4622 * fatal will be ignored for read-only BDS; corruptions found there will always
4623 * be considered non-fatal.
4625 void qcow2_signal_corruption(BlockDriverState *bs, bool fatal, int64_t offset,
4626 int64_t size, const char *message_format, ...)
4628 BDRVQcow2State *s = bs->opaque;
4629 const char *node_name;
4633 fatal = fatal && bdrv_is_writable(bs);
4635 if (s->signaled_corruption &&
4636 (!fatal || (s->incompatible_features & QCOW2_INCOMPAT_CORRUPT)))
4641 va_start(ap, message_format);
4642 message = g_strdup_vprintf(message_format, ap);
4646 fprintf(stderr, "qcow2: Marking image as corrupt: %s; further "
4647 "corruption events will be suppressed\n", message);
4649 fprintf(stderr, "qcow2: Image is corrupt: %s; further non-fatal "
4650 "corruption events will be suppressed\n", message);
4653 node_name = bdrv_get_node_name(bs);
4654 qapi_event_send_block_image_corrupted(bdrv_get_device_name(bs),
4655 *node_name != '\0', node_name,
4656 message, offset >= 0, offset,
4662 qcow2_mark_corrupt(bs);
4663 bs->drv = NULL; /* make BDS unusable */
4666 s->signaled_corruption = true;
4669 static QemuOptsList qcow2_create_opts = {
4670 .name = "qcow2-create-opts",
4671 .head = QTAILQ_HEAD_INITIALIZER(qcow2_create_opts.head),
4674 .name = BLOCK_OPT_SIZE,
4675 .type = QEMU_OPT_SIZE,
4676 .help = "Virtual disk size"
4679 .name = BLOCK_OPT_COMPAT_LEVEL,
4680 .type = QEMU_OPT_STRING,
4681 .help = "Compatibility level (0.10 or 1.1)"
4684 .name = BLOCK_OPT_BACKING_FILE,
4685 .type = QEMU_OPT_STRING,
4686 .help = "File name of a base image"
4689 .name = BLOCK_OPT_BACKING_FMT,
4690 .type = QEMU_OPT_STRING,
4691 .help = "Image format of the base image"
4694 .name = BLOCK_OPT_ENCRYPT,
4695 .type = QEMU_OPT_BOOL,
4696 .help = "Encrypt the image with format 'aes'. (Deprecated "
4697 "in favor of " BLOCK_OPT_ENCRYPT_FORMAT "=aes)",
4700 .name = BLOCK_OPT_ENCRYPT_FORMAT,
4701 .type = QEMU_OPT_STRING,
4702 .help = "Encrypt the image, format choices: 'aes', 'luks'",
4704 BLOCK_CRYPTO_OPT_DEF_KEY_SECRET("encrypt.",
4705 "ID of secret providing qcow AES key or LUKS passphrase"),
4706 BLOCK_CRYPTO_OPT_DEF_LUKS_CIPHER_ALG("encrypt."),
4707 BLOCK_CRYPTO_OPT_DEF_LUKS_CIPHER_MODE("encrypt."),
4708 BLOCK_CRYPTO_OPT_DEF_LUKS_IVGEN_ALG("encrypt."),
4709 BLOCK_CRYPTO_OPT_DEF_LUKS_IVGEN_HASH_ALG("encrypt."),
4710 BLOCK_CRYPTO_OPT_DEF_LUKS_HASH_ALG("encrypt."),
4711 BLOCK_CRYPTO_OPT_DEF_LUKS_ITER_TIME("encrypt."),
4713 .name = BLOCK_OPT_CLUSTER_SIZE,
4714 .type = QEMU_OPT_SIZE,
4715 .help = "qcow2 cluster size",
4716 .def_value_str = stringify(DEFAULT_CLUSTER_SIZE)
4719 .name = BLOCK_OPT_PREALLOC,
4720 .type = QEMU_OPT_STRING,
4721 .help = "Preallocation mode (allowed values: off, metadata, "
4725 .name = BLOCK_OPT_LAZY_REFCOUNTS,
4726 .type = QEMU_OPT_BOOL,
4727 .help = "Postpone refcount updates",
4728 .def_value_str = "off"
4731 .name = BLOCK_OPT_REFCOUNT_BITS,
4732 .type = QEMU_OPT_NUMBER,
4733 .help = "Width of a reference count entry in bits",
4734 .def_value_str = "16"
4736 { /* end of list */ }
4740 BlockDriver bdrv_qcow2 = {
4741 .format_name = "qcow2",
4742 .instance_size = sizeof(BDRVQcow2State),
4743 .bdrv_probe = qcow2_probe,
4744 .bdrv_open = qcow2_open,
4745 .bdrv_close = qcow2_close,
4746 .bdrv_reopen_prepare = qcow2_reopen_prepare,
4747 .bdrv_reopen_commit = qcow2_reopen_commit,
4748 .bdrv_reopen_abort = qcow2_reopen_abort,
4749 .bdrv_join_options = qcow2_join_options,
4750 .bdrv_child_perm = bdrv_format_default_perms,
4751 .bdrv_co_create_opts = qcow2_co_create_opts,
4752 .bdrv_co_create = qcow2_co_create,
4753 .bdrv_has_zero_init = bdrv_has_zero_init_1,
4754 .bdrv_co_block_status = qcow2_co_block_status,
4756 .bdrv_co_preadv = qcow2_co_preadv,
4757 .bdrv_co_pwritev = qcow2_co_pwritev,
4758 .bdrv_co_flush_to_os = qcow2_co_flush_to_os,
4760 .bdrv_co_pwrite_zeroes = qcow2_co_pwrite_zeroes,
4761 .bdrv_co_pdiscard = qcow2_co_pdiscard,
4762 .bdrv_co_copy_range_from = qcow2_co_copy_range_from,
4763 .bdrv_co_copy_range_to = qcow2_co_copy_range_to,
4764 .bdrv_co_truncate = qcow2_co_truncate,
4765 .bdrv_co_pwritev_compressed = qcow2_co_pwritev_compressed,
4766 .bdrv_make_empty = qcow2_make_empty,
4768 .bdrv_snapshot_create = qcow2_snapshot_create,
4769 .bdrv_snapshot_goto = qcow2_snapshot_goto,
4770 .bdrv_snapshot_delete = qcow2_snapshot_delete,
4771 .bdrv_snapshot_list = qcow2_snapshot_list,
4772 .bdrv_snapshot_load_tmp = qcow2_snapshot_load_tmp,
4773 .bdrv_measure = qcow2_measure,
4774 .bdrv_get_info = qcow2_get_info,
4775 .bdrv_get_specific_info = qcow2_get_specific_info,
4777 .bdrv_save_vmstate = qcow2_save_vmstate,
4778 .bdrv_load_vmstate = qcow2_load_vmstate,
4780 .supports_backing = true,
4781 .bdrv_change_backing_file = qcow2_change_backing_file,
4783 .bdrv_refresh_limits = qcow2_refresh_limits,
4784 .bdrv_co_invalidate_cache = qcow2_co_invalidate_cache,
4785 .bdrv_inactivate = qcow2_inactivate,
4787 .create_opts = &qcow2_create_opts,
4788 .bdrv_co_check = qcow2_co_check,
4789 .bdrv_amend_options = qcow2_amend_options,
4791 .bdrv_detach_aio_context = qcow2_detach_aio_context,
4792 .bdrv_attach_aio_context = qcow2_attach_aio_context,
4794 .bdrv_reopen_bitmaps_rw = qcow2_reopen_bitmaps_rw,
4795 .bdrv_can_store_new_dirty_bitmap = qcow2_can_store_new_dirty_bitmap,
4796 .bdrv_remove_persistent_dirty_bitmap = qcow2_remove_persistent_dirty_bitmap,
4799 static void bdrv_qcow2_init(void)
4801 bdrv_register(&bdrv_qcow2);
4804 block_init(bdrv_qcow2_init);