qcow2: add option to clean unused cache entries after some time
[qemu/rayw.git] / block / qcow2.c
blobea34ae2da5e52dfc84c73a1ec4b7f779567bcbb7
1 /*
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
22 * THE SOFTWARE.
24 #include "qemu-common.h"
25 #include "block/block_int.h"
26 #include "qemu/module.h"
27 #include <zlib.h>
28 #include "block/qcow2.h"
29 #include "qemu/error-report.h"
30 #include "qapi/qmp/qerror.h"
31 #include "qapi/qmp/qbool.h"
32 #include "qapi/util.h"
33 #include "qapi/qmp/types.h"
34 #include "qapi-event.h"
35 #include "trace.h"
36 #include "qemu/option_int.h"
39 Differences with QCOW:
41 - Support for multiple incremental snapshots.
42 - Memory management by reference counts.
43 - Clusters which have a reference count of one have the bit
44 QCOW_OFLAG_COPIED to optimize write performance.
45 - Size of compressed clusters is stored in sectors to reduce bit usage
46 in the cluster offsets.
47 - Support for storing additional data (such as the VM state) in the
48 snapshots.
49 - If a backing store is used, the cluster size is not constrained
50 (could be backported to QCOW).
51 - L2 tables have always a size of one cluster.
55 typedef struct {
56 uint32_t magic;
57 uint32_t len;
58 } QEMU_PACKED QCowExtension;
60 #define QCOW2_EXT_MAGIC_END 0
61 #define QCOW2_EXT_MAGIC_BACKING_FORMAT 0xE2792ACA
62 #define QCOW2_EXT_MAGIC_FEATURE_TABLE 0x6803f857
64 static int qcow2_probe(const uint8_t *buf, int buf_size, const char *filename)
66 const QCowHeader *cow_header = (const void *)buf;
68 if (buf_size >= sizeof(QCowHeader) &&
69 be32_to_cpu(cow_header->magic) == QCOW_MAGIC &&
70 be32_to_cpu(cow_header->version) >= 2)
71 return 100;
72 else
73 return 0;
77 /*
78 * read qcow2 extension and fill bs
79 * start reading from start_offset
80 * finish reading upon magic of value 0 or when end_offset reached
81 * unknown magic is skipped (future extension this version knows nothing about)
82 * return 0 upon success, non-0 otherwise
84 static int qcow2_read_extensions(BlockDriverState *bs, uint64_t start_offset,
85 uint64_t end_offset, void **p_feature_table,
86 Error **errp)
88 BDRVQcowState *s = bs->opaque;
89 QCowExtension ext;
90 uint64_t offset;
91 int ret;
93 #ifdef DEBUG_EXT
94 printf("qcow2_read_extensions: start=%ld end=%ld\n", start_offset, end_offset);
95 #endif
96 offset = start_offset;
97 while (offset < end_offset) {
99 #ifdef DEBUG_EXT
100 /* Sanity check */
101 if (offset > s->cluster_size)
102 printf("qcow2_read_extension: suspicious offset %lu\n", offset);
104 printf("attempting to read extended header in offset %lu\n", offset);
105 #endif
107 ret = bdrv_pread(bs->file, offset, &ext, sizeof(ext));
108 if (ret < 0) {
109 error_setg_errno(errp, -ret, "qcow2_read_extension: ERROR: "
110 "pread fail from offset %" PRIu64, offset);
111 return 1;
113 be32_to_cpus(&ext.magic);
114 be32_to_cpus(&ext.len);
115 offset += sizeof(ext);
116 #ifdef DEBUG_EXT
117 printf("ext.magic = 0x%x\n", ext.magic);
118 #endif
119 if (offset > end_offset || ext.len > end_offset - offset) {
120 error_setg(errp, "Header extension too large");
121 return -EINVAL;
124 switch (ext.magic) {
125 case QCOW2_EXT_MAGIC_END:
126 return 0;
128 case QCOW2_EXT_MAGIC_BACKING_FORMAT:
129 if (ext.len >= sizeof(bs->backing_format)) {
130 error_setg(errp, "ERROR: ext_backing_format: len=%" PRIu32
131 " too large (>=%zu)", ext.len,
132 sizeof(bs->backing_format));
133 return 2;
135 ret = bdrv_pread(bs->file, offset, bs->backing_format, ext.len);
136 if (ret < 0) {
137 error_setg_errno(errp, -ret, "ERROR: ext_backing_format: "
138 "Could not read format name");
139 return 3;
141 bs->backing_format[ext.len] = '\0';
142 s->image_backing_format = g_strdup(bs->backing_format);
143 #ifdef DEBUG_EXT
144 printf("Qcow2: Got format extension %s\n", bs->backing_format);
145 #endif
146 break;
148 case QCOW2_EXT_MAGIC_FEATURE_TABLE:
149 if (p_feature_table != NULL) {
150 void* feature_table = g_malloc0(ext.len + 2 * sizeof(Qcow2Feature));
151 ret = bdrv_pread(bs->file, offset , feature_table, ext.len);
152 if (ret < 0) {
153 error_setg_errno(errp, -ret, "ERROR: ext_feature_table: "
154 "Could not read table");
155 return ret;
158 *p_feature_table = feature_table;
160 break;
162 default:
163 /* unknown magic - save it in case we need to rewrite the header */
165 Qcow2UnknownHeaderExtension *uext;
167 uext = g_malloc0(sizeof(*uext) + ext.len);
168 uext->magic = ext.magic;
169 uext->len = ext.len;
170 QLIST_INSERT_HEAD(&s->unknown_header_ext, uext, next);
172 ret = bdrv_pread(bs->file, offset , uext->data, uext->len);
173 if (ret < 0) {
174 error_setg_errno(errp, -ret, "ERROR: unknown extension: "
175 "Could not read data");
176 return ret;
179 break;
182 offset += ((ext.len + 7) & ~7);
185 return 0;
188 static void cleanup_unknown_header_ext(BlockDriverState *bs)
190 BDRVQcowState *s = bs->opaque;
191 Qcow2UnknownHeaderExtension *uext, *next;
193 QLIST_FOREACH_SAFE(uext, &s->unknown_header_ext, next, next) {
194 QLIST_REMOVE(uext, next);
195 g_free(uext);
199 static void GCC_FMT_ATTR(3, 4) report_unsupported(BlockDriverState *bs,
200 Error **errp, const char *fmt, ...)
202 char msg[64];
203 va_list ap;
205 va_start(ap, fmt);
206 vsnprintf(msg, sizeof(msg), fmt, ap);
207 va_end(ap);
209 error_setg(errp, QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,
210 bdrv_get_device_or_node_name(bs), "qcow2", msg);
213 static void report_unsupported_feature(BlockDriverState *bs,
214 Error **errp, Qcow2Feature *table, uint64_t mask)
216 char *features = g_strdup("");
217 char *old;
219 while (table && table->name[0] != '\0') {
220 if (table->type == QCOW2_FEAT_TYPE_INCOMPATIBLE) {
221 if (mask & (1ULL << table->bit)) {
222 old = features;
223 features = g_strdup_printf("%s%s%.46s", old, *old ? ", " : "",
224 table->name);
225 g_free(old);
226 mask &= ~(1ULL << table->bit);
229 table++;
232 if (mask) {
233 old = features;
234 features = g_strdup_printf("%s%sUnknown incompatible feature: %" PRIx64,
235 old, *old ? ", " : "", mask);
236 g_free(old);
239 report_unsupported(bs, errp, "%s", features);
240 g_free(features);
244 * Sets the dirty bit and flushes afterwards if necessary.
246 * The incompatible_features bit is only set if the image file header was
247 * updated successfully. Therefore it is not required to check the return
248 * value of this function.
250 int qcow2_mark_dirty(BlockDriverState *bs)
252 BDRVQcowState *s = bs->opaque;
253 uint64_t val;
254 int ret;
256 assert(s->qcow_version >= 3);
258 if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) {
259 return 0; /* already dirty */
262 val = cpu_to_be64(s->incompatible_features | QCOW2_INCOMPAT_DIRTY);
263 ret = bdrv_pwrite(bs->file, offsetof(QCowHeader, incompatible_features),
264 &val, sizeof(val));
265 if (ret < 0) {
266 return ret;
268 ret = bdrv_flush(bs->file);
269 if (ret < 0) {
270 return ret;
273 /* Only treat image as dirty if the header was updated successfully */
274 s->incompatible_features |= QCOW2_INCOMPAT_DIRTY;
275 return 0;
279 * Clears the dirty bit and flushes before if necessary. Only call this
280 * function when there are no pending requests, it does not guard against
281 * concurrent requests dirtying the image.
283 static int qcow2_mark_clean(BlockDriverState *bs)
285 BDRVQcowState *s = bs->opaque;
287 if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) {
288 int ret;
290 s->incompatible_features &= ~QCOW2_INCOMPAT_DIRTY;
292 ret = bdrv_flush(bs);
293 if (ret < 0) {
294 return ret;
297 return qcow2_update_header(bs);
299 return 0;
303 * Marks the image as corrupt.
305 int qcow2_mark_corrupt(BlockDriverState *bs)
307 BDRVQcowState *s = bs->opaque;
309 s->incompatible_features |= QCOW2_INCOMPAT_CORRUPT;
310 return qcow2_update_header(bs);
314 * Marks the image as consistent, i.e., unsets the corrupt bit, and flushes
315 * before if necessary.
317 int qcow2_mark_consistent(BlockDriverState *bs)
319 BDRVQcowState *s = bs->opaque;
321 if (s->incompatible_features & QCOW2_INCOMPAT_CORRUPT) {
322 int ret = bdrv_flush(bs);
323 if (ret < 0) {
324 return ret;
327 s->incompatible_features &= ~QCOW2_INCOMPAT_CORRUPT;
328 return qcow2_update_header(bs);
330 return 0;
333 static int qcow2_check(BlockDriverState *bs, BdrvCheckResult *result,
334 BdrvCheckMode fix)
336 int ret = qcow2_check_refcounts(bs, result, fix);
337 if (ret < 0) {
338 return ret;
341 if (fix && result->check_errors == 0 && result->corruptions == 0) {
342 ret = qcow2_mark_clean(bs);
343 if (ret < 0) {
344 return ret;
346 return qcow2_mark_consistent(bs);
348 return ret;
351 static int validate_table_offset(BlockDriverState *bs, uint64_t offset,
352 uint64_t entries, size_t entry_len)
354 BDRVQcowState *s = bs->opaque;
355 uint64_t size;
357 /* Use signed INT64_MAX as the maximum even for uint64_t header fields,
358 * because values will be passed to qemu functions taking int64_t. */
359 if (entries > INT64_MAX / entry_len) {
360 return -EINVAL;
363 size = entries * entry_len;
365 if (INT64_MAX - size < offset) {
366 return -EINVAL;
369 /* Tables must be cluster aligned */
370 if (offset & (s->cluster_size - 1)) {
371 return -EINVAL;
374 return 0;
377 static QemuOptsList qcow2_runtime_opts = {
378 .name = "qcow2",
379 .head = QTAILQ_HEAD_INITIALIZER(qcow2_runtime_opts.head),
380 .desc = {
382 .name = QCOW2_OPT_LAZY_REFCOUNTS,
383 .type = QEMU_OPT_BOOL,
384 .help = "Postpone refcount updates",
387 .name = QCOW2_OPT_DISCARD_REQUEST,
388 .type = QEMU_OPT_BOOL,
389 .help = "Pass guest discard requests to the layer below",
392 .name = QCOW2_OPT_DISCARD_SNAPSHOT,
393 .type = QEMU_OPT_BOOL,
394 .help = "Generate discard requests when snapshot related space "
395 "is freed",
398 .name = QCOW2_OPT_DISCARD_OTHER,
399 .type = QEMU_OPT_BOOL,
400 .help = "Generate discard requests when other clusters are freed",
403 .name = QCOW2_OPT_OVERLAP,
404 .type = QEMU_OPT_STRING,
405 .help = "Selects which overlap checks to perform from a range of "
406 "templates (none, constant, cached, all)",
409 .name = QCOW2_OPT_OVERLAP_TEMPLATE,
410 .type = QEMU_OPT_STRING,
411 .help = "Selects which overlap checks to perform from a range of "
412 "templates (none, constant, cached, all)",
415 .name = QCOW2_OPT_OVERLAP_MAIN_HEADER,
416 .type = QEMU_OPT_BOOL,
417 .help = "Check for unintended writes into the main qcow2 header",
420 .name = QCOW2_OPT_OVERLAP_ACTIVE_L1,
421 .type = QEMU_OPT_BOOL,
422 .help = "Check for unintended writes into the active L1 table",
425 .name = QCOW2_OPT_OVERLAP_ACTIVE_L2,
426 .type = QEMU_OPT_BOOL,
427 .help = "Check for unintended writes into an active L2 table",
430 .name = QCOW2_OPT_OVERLAP_REFCOUNT_TABLE,
431 .type = QEMU_OPT_BOOL,
432 .help = "Check for unintended writes into the refcount table",
435 .name = QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK,
436 .type = QEMU_OPT_BOOL,
437 .help = "Check for unintended writes into a refcount block",
440 .name = QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE,
441 .type = QEMU_OPT_BOOL,
442 .help = "Check for unintended writes into the snapshot table",
445 .name = QCOW2_OPT_OVERLAP_INACTIVE_L1,
446 .type = QEMU_OPT_BOOL,
447 .help = "Check for unintended writes into an inactive L1 table",
450 .name = QCOW2_OPT_OVERLAP_INACTIVE_L2,
451 .type = QEMU_OPT_BOOL,
452 .help = "Check for unintended writes into an inactive L2 table",
455 .name = QCOW2_OPT_CACHE_SIZE,
456 .type = QEMU_OPT_SIZE,
457 .help = "Maximum combined metadata (L2 tables and refcount blocks) "
458 "cache size",
461 .name = QCOW2_OPT_L2_CACHE_SIZE,
462 .type = QEMU_OPT_SIZE,
463 .help = "Maximum L2 table cache size",
466 .name = QCOW2_OPT_REFCOUNT_CACHE_SIZE,
467 .type = QEMU_OPT_SIZE,
468 .help = "Maximum refcount block cache size",
471 .name = QCOW2_OPT_CACHE_CLEAN_INTERVAL,
472 .type = QEMU_OPT_NUMBER,
473 .help = "Clean unused cache entries after this time (in seconds)",
475 { /* end of list */ }
479 static const char *overlap_bool_option_names[QCOW2_OL_MAX_BITNR] = {
480 [QCOW2_OL_MAIN_HEADER_BITNR] = QCOW2_OPT_OVERLAP_MAIN_HEADER,
481 [QCOW2_OL_ACTIVE_L1_BITNR] = QCOW2_OPT_OVERLAP_ACTIVE_L1,
482 [QCOW2_OL_ACTIVE_L2_BITNR] = QCOW2_OPT_OVERLAP_ACTIVE_L2,
483 [QCOW2_OL_REFCOUNT_TABLE_BITNR] = QCOW2_OPT_OVERLAP_REFCOUNT_TABLE,
484 [QCOW2_OL_REFCOUNT_BLOCK_BITNR] = QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK,
485 [QCOW2_OL_SNAPSHOT_TABLE_BITNR] = QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE,
486 [QCOW2_OL_INACTIVE_L1_BITNR] = QCOW2_OPT_OVERLAP_INACTIVE_L1,
487 [QCOW2_OL_INACTIVE_L2_BITNR] = QCOW2_OPT_OVERLAP_INACTIVE_L2,
490 static void cache_clean_timer_cb(void *opaque)
492 BlockDriverState *bs = opaque;
493 BDRVQcowState *s = bs->opaque;
494 qcow2_cache_clean_unused(bs, s->l2_table_cache);
495 qcow2_cache_clean_unused(bs, s->refcount_block_cache);
496 timer_mod(s->cache_clean_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
497 (int64_t) s->cache_clean_interval * 1000);
500 static void cache_clean_timer_init(BlockDriverState *bs, AioContext *context)
502 BDRVQcowState *s = bs->opaque;
503 if (s->cache_clean_interval > 0) {
504 s->cache_clean_timer = aio_timer_new(context, QEMU_CLOCK_VIRTUAL,
505 SCALE_MS, cache_clean_timer_cb,
506 bs);
507 timer_mod(s->cache_clean_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
508 (int64_t) s->cache_clean_interval * 1000);
512 static void cache_clean_timer_del(BlockDriverState *bs)
514 BDRVQcowState *s = bs->opaque;
515 if (s->cache_clean_timer) {
516 timer_del(s->cache_clean_timer);
517 timer_free(s->cache_clean_timer);
518 s->cache_clean_timer = NULL;
522 static void qcow2_detach_aio_context(BlockDriverState *bs)
524 cache_clean_timer_del(bs);
527 static void qcow2_attach_aio_context(BlockDriverState *bs,
528 AioContext *new_context)
530 cache_clean_timer_init(bs, new_context);
533 static void read_cache_sizes(BlockDriverState *bs, QemuOpts *opts,
534 uint64_t *l2_cache_size,
535 uint64_t *refcount_cache_size, Error **errp)
537 BDRVQcowState *s = bs->opaque;
538 uint64_t combined_cache_size;
539 bool l2_cache_size_set, refcount_cache_size_set, combined_cache_size_set;
541 combined_cache_size_set = qemu_opt_get(opts, QCOW2_OPT_CACHE_SIZE);
542 l2_cache_size_set = qemu_opt_get(opts, QCOW2_OPT_L2_CACHE_SIZE);
543 refcount_cache_size_set = qemu_opt_get(opts, QCOW2_OPT_REFCOUNT_CACHE_SIZE);
545 combined_cache_size = qemu_opt_get_size(opts, QCOW2_OPT_CACHE_SIZE, 0);
546 *l2_cache_size = qemu_opt_get_size(opts, QCOW2_OPT_L2_CACHE_SIZE, 0);
547 *refcount_cache_size = qemu_opt_get_size(opts,
548 QCOW2_OPT_REFCOUNT_CACHE_SIZE, 0);
550 if (combined_cache_size_set) {
551 if (l2_cache_size_set && refcount_cache_size_set) {
552 error_setg(errp, QCOW2_OPT_CACHE_SIZE ", " QCOW2_OPT_L2_CACHE_SIZE
553 " and " QCOW2_OPT_REFCOUNT_CACHE_SIZE " may not be set "
554 "the same time");
555 return;
556 } else if (*l2_cache_size > combined_cache_size) {
557 error_setg(errp, QCOW2_OPT_L2_CACHE_SIZE " may not exceed "
558 QCOW2_OPT_CACHE_SIZE);
559 return;
560 } else if (*refcount_cache_size > combined_cache_size) {
561 error_setg(errp, QCOW2_OPT_REFCOUNT_CACHE_SIZE " may not exceed "
562 QCOW2_OPT_CACHE_SIZE);
563 return;
566 if (l2_cache_size_set) {
567 *refcount_cache_size = combined_cache_size - *l2_cache_size;
568 } else if (refcount_cache_size_set) {
569 *l2_cache_size = combined_cache_size - *refcount_cache_size;
570 } else {
571 *refcount_cache_size = combined_cache_size
572 / (DEFAULT_L2_REFCOUNT_SIZE_RATIO + 1);
573 *l2_cache_size = combined_cache_size - *refcount_cache_size;
575 } else {
576 if (!l2_cache_size_set && !refcount_cache_size_set) {
577 *l2_cache_size = MAX(DEFAULT_L2_CACHE_BYTE_SIZE,
578 (uint64_t)DEFAULT_L2_CACHE_CLUSTERS
579 * s->cluster_size);
580 *refcount_cache_size = *l2_cache_size
581 / DEFAULT_L2_REFCOUNT_SIZE_RATIO;
582 } else if (!l2_cache_size_set) {
583 *l2_cache_size = *refcount_cache_size
584 * DEFAULT_L2_REFCOUNT_SIZE_RATIO;
585 } else if (!refcount_cache_size_set) {
586 *refcount_cache_size = *l2_cache_size
587 / DEFAULT_L2_REFCOUNT_SIZE_RATIO;
592 static int qcow2_open(BlockDriverState *bs, QDict *options, int flags,
593 Error **errp)
595 BDRVQcowState *s = bs->opaque;
596 unsigned int len, i;
597 int ret = 0;
598 QCowHeader header;
599 QemuOpts *opts = NULL;
600 Error *local_err = NULL;
601 uint64_t ext_end;
602 uint64_t l1_vm_state_index;
603 const char *opt_overlap_check, *opt_overlap_check_template;
604 int overlap_check_template = 0;
605 uint64_t l2_cache_size, refcount_cache_size;
606 uint64_t cache_clean_interval;
608 ret = bdrv_pread(bs->file, 0, &header, sizeof(header));
609 if (ret < 0) {
610 error_setg_errno(errp, -ret, "Could not read qcow2 header");
611 goto fail;
613 be32_to_cpus(&header.magic);
614 be32_to_cpus(&header.version);
615 be64_to_cpus(&header.backing_file_offset);
616 be32_to_cpus(&header.backing_file_size);
617 be64_to_cpus(&header.size);
618 be32_to_cpus(&header.cluster_bits);
619 be32_to_cpus(&header.crypt_method);
620 be64_to_cpus(&header.l1_table_offset);
621 be32_to_cpus(&header.l1_size);
622 be64_to_cpus(&header.refcount_table_offset);
623 be32_to_cpus(&header.refcount_table_clusters);
624 be64_to_cpus(&header.snapshots_offset);
625 be32_to_cpus(&header.nb_snapshots);
627 if (header.magic != QCOW_MAGIC) {
628 error_setg(errp, "Image is not in qcow2 format");
629 ret = -EINVAL;
630 goto fail;
632 if (header.version < 2 || header.version > 3) {
633 report_unsupported(bs, errp, "QCOW version %" PRIu32, header.version);
634 ret = -ENOTSUP;
635 goto fail;
638 s->qcow_version = header.version;
640 /* Initialise cluster size */
641 if (header.cluster_bits < MIN_CLUSTER_BITS ||
642 header.cluster_bits > MAX_CLUSTER_BITS) {
643 error_setg(errp, "Unsupported cluster size: 2^%" PRIu32,
644 header.cluster_bits);
645 ret = -EINVAL;
646 goto fail;
649 s->cluster_bits = header.cluster_bits;
650 s->cluster_size = 1 << s->cluster_bits;
651 s->cluster_sectors = 1 << (s->cluster_bits - 9);
653 /* Initialise version 3 header fields */
654 if (header.version == 2) {
655 header.incompatible_features = 0;
656 header.compatible_features = 0;
657 header.autoclear_features = 0;
658 header.refcount_order = 4;
659 header.header_length = 72;
660 } else {
661 be64_to_cpus(&header.incompatible_features);
662 be64_to_cpus(&header.compatible_features);
663 be64_to_cpus(&header.autoclear_features);
664 be32_to_cpus(&header.refcount_order);
665 be32_to_cpus(&header.header_length);
667 if (header.header_length < 104) {
668 error_setg(errp, "qcow2 header too short");
669 ret = -EINVAL;
670 goto fail;
674 if (header.header_length > s->cluster_size) {
675 error_setg(errp, "qcow2 header exceeds cluster size");
676 ret = -EINVAL;
677 goto fail;
680 if (header.header_length > sizeof(header)) {
681 s->unknown_header_fields_size = header.header_length - sizeof(header);
682 s->unknown_header_fields = g_malloc(s->unknown_header_fields_size);
683 ret = bdrv_pread(bs->file, sizeof(header), s->unknown_header_fields,
684 s->unknown_header_fields_size);
685 if (ret < 0) {
686 error_setg_errno(errp, -ret, "Could not read unknown qcow2 header "
687 "fields");
688 goto fail;
692 if (header.backing_file_offset > s->cluster_size) {
693 error_setg(errp, "Invalid backing file offset");
694 ret = -EINVAL;
695 goto fail;
698 if (header.backing_file_offset) {
699 ext_end = header.backing_file_offset;
700 } else {
701 ext_end = 1 << header.cluster_bits;
704 /* Handle feature bits */
705 s->incompatible_features = header.incompatible_features;
706 s->compatible_features = header.compatible_features;
707 s->autoclear_features = header.autoclear_features;
709 if (s->incompatible_features & ~QCOW2_INCOMPAT_MASK) {
710 void *feature_table = NULL;
711 qcow2_read_extensions(bs, header.header_length, ext_end,
712 &feature_table, NULL);
713 report_unsupported_feature(bs, errp, feature_table,
714 s->incompatible_features &
715 ~QCOW2_INCOMPAT_MASK);
716 ret = -ENOTSUP;
717 g_free(feature_table);
718 goto fail;
721 if (s->incompatible_features & QCOW2_INCOMPAT_CORRUPT) {
722 /* Corrupt images may not be written to unless they are being repaired
724 if ((flags & BDRV_O_RDWR) && !(flags & BDRV_O_CHECK)) {
725 error_setg(errp, "qcow2: Image is corrupt; cannot be opened "
726 "read/write");
727 ret = -EACCES;
728 goto fail;
732 /* Check support for various header values */
733 if (header.refcount_order > 6) {
734 error_setg(errp, "Reference count entry width too large; may not "
735 "exceed 64 bits");
736 ret = -EINVAL;
737 goto fail;
739 s->refcount_order = header.refcount_order;
740 s->refcount_bits = 1 << s->refcount_order;
741 s->refcount_max = UINT64_C(1) << (s->refcount_bits - 1);
742 s->refcount_max += s->refcount_max - 1;
744 if (header.crypt_method > QCOW_CRYPT_AES) {
745 error_setg(errp, "Unsupported encryption method: %" PRIu32,
746 header.crypt_method);
747 ret = -EINVAL;
748 goto fail;
750 if (!qcrypto_cipher_supports(QCRYPTO_CIPHER_ALG_AES_128)) {
751 error_setg(errp, "AES cipher not available");
752 ret = -EINVAL;
753 goto fail;
755 s->crypt_method_header = header.crypt_method;
756 if (s->crypt_method_header) {
757 bs->encrypted = 1;
760 s->l2_bits = s->cluster_bits - 3; /* L2 is always one cluster */
761 s->l2_size = 1 << s->l2_bits;
762 /* 2^(s->refcount_order - 3) is the refcount width in bytes */
763 s->refcount_block_bits = s->cluster_bits - (s->refcount_order - 3);
764 s->refcount_block_size = 1 << s->refcount_block_bits;
765 bs->total_sectors = header.size / 512;
766 s->csize_shift = (62 - (s->cluster_bits - 8));
767 s->csize_mask = (1 << (s->cluster_bits - 8)) - 1;
768 s->cluster_offset_mask = (1LL << s->csize_shift) - 1;
770 s->refcount_table_offset = header.refcount_table_offset;
771 s->refcount_table_size =
772 header.refcount_table_clusters << (s->cluster_bits - 3);
774 if (header.refcount_table_clusters > qcow2_max_refcount_clusters(s)) {
775 error_setg(errp, "Reference count table too large");
776 ret = -EINVAL;
777 goto fail;
780 ret = validate_table_offset(bs, s->refcount_table_offset,
781 s->refcount_table_size, sizeof(uint64_t));
782 if (ret < 0) {
783 error_setg(errp, "Invalid reference count table offset");
784 goto fail;
787 /* Snapshot table offset/length */
788 if (header.nb_snapshots > QCOW_MAX_SNAPSHOTS) {
789 error_setg(errp, "Too many snapshots");
790 ret = -EINVAL;
791 goto fail;
794 ret = validate_table_offset(bs, header.snapshots_offset,
795 header.nb_snapshots,
796 sizeof(QCowSnapshotHeader));
797 if (ret < 0) {
798 error_setg(errp, "Invalid snapshot table offset");
799 goto fail;
802 /* read the level 1 table */
803 if (header.l1_size > QCOW_MAX_L1_SIZE / sizeof(uint64_t)) {
804 error_setg(errp, "Active L1 table too large");
805 ret = -EFBIG;
806 goto fail;
808 s->l1_size = header.l1_size;
810 l1_vm_state_index = size_to_l1(s, header.size);
811 if (l1_vm_state_index > INT_MAX) {
812 error_setg(errp, "Image is too big");
813 ret = -EFBIG;
814 goto fail;
816 s->l1_vm_state_index = l1_vm_state_index;
818 /* the L1 table must contain at least enough entries to put
819 header.size bytes */
820 if (s->l1_size < s->l1_vm_state_index) {
821 error_setg(errp, "L1 table is too small");
822 ret = -EINVAL;
823 goto fail;
826 ret = validate_table_offset(bs, header.l1_table_offset,
827 header.l1_size, sizeof(uint64_t));
828 if (ret < 0) {
829 error_setg(errp, "Invalid L1 table offset");
830 goto fail;
832 s->l1_table_offset = header.l1_table_offset;
835 if (s->l1_size > 0) {
836 s->l1_table = qemu_try_blockalign(bs->file,
837 align_offset(s->l1_size * sizeof(uint64_t), 512));
838 if (s->l1_table == NULL) {
839 error_setg(errp, "Could not allocate L1 table");
840 ret = -ENOMEM;
841 goto fail;
843 ret = bdrv_pread(bs->file, s->l1_table_offset, s->l1_table,
844 s->l1_size * sizeof(uint64_t));
845 if (ret < 0) {
846 error_setg_errno(errp, -ret, "Could not read L1 table");
847 goto fail;
849 for(i = 0;i < s->l1_size; i++) {
850 be64_to_cpus(&s->l1_table[i]);
854 /* get L2 table/refcount block cache size from command line options */
855 opts = qemu_opts_create(&qcow2_runtime_opts, NULL, 0, &error_abort);
856 qemu_opts_absorb_qdict(opts, options, &local_err);
857 if (local_err) {
858 error_propagate(errp, local_err);
859 ret = -EINVAL;
860 goto fail;
863 read_cache_sizes(bs, opts, &l2_cache_size, &refcount_cache_size,
864 &local_err);
865 if (local_err) {
866 error_propagate(errp, local_err);
867 ret = -EINVAL;
868 goto fail;
871 l2_cache_size /= s->cluster_size;
872 if (l2_cache_size < MIN_L2_CACHE_SIZE) {
873 l2_cache_size = MIN_L2_CACHE_SIZE;
875 if (l2_cache_size > INT_MAX) {
876 error_setg(errp, "L2 cache size too big");
877 ret = -EINVAL;
878 goto fail;
881 refcount_cache_size /= s->cluster_size;
882 if (refcount_cache_size < MIN_REFCOUNT_CACHE_SIZE) {
883 refcount_cache_size = MIN_REFCOUNT_CACHE_SIZE;
885 if (refcount_cache_size > INT_MAX) {
886 error_setg(errp, "Refcount cache size too big");
887 ret = -EINVAL;
888 goto fail;
891 /* alloc L2 table/refcount block cache */
892 s->l2_table_cache = qcow2_cache_create(bs, l2_cache_size);
893 s->refcount_block_cache = qcow2_cache_create(bs, refcount_cache_size);
894 if (s->l2_table_cache == NULL || s->refcount_block_cache == NULL) {
895 error_setg(errp, "Could not allocate metadata caches");
896 ret = -ENOMEM;
897 goto fail;
900 cache_clean_interval =
901 qemu_opt_get_number(opts, QCOW2_OPT_CACHE_CLEAN_INTERVAL, 0);
902 if (cache_clean_interval > UINT_MAX) {
903 error_setg(errp, "Cache clean interval too big");
904 ret = -EINVAL;
905 goto fail;
907 s->cache_clean_interval = cache_clean_interval;
908 cache_clean_timer_init(bs, bdrv_get_aio_context(bs));
910 s->cluster_cache = g_malloc(s->cluster_size);
911 /* one more sector for decompressed data alignment */
912 s->cluster_data = qemu_try_blockalign(bs->file, QCOW_MAX_CRYPT_CLUSTERS
913 * s->cluster_size + 512);
914 if (s->cluster_data == NULL) {
915 error_setg(errp, "Could not allocate temporary cluster buffer");
916 ret = -ENOMEM;
917 goto fail;
920 s->cluster_cache_offset = -1;
921 s->flags = flags;
923 ret = qcow2_refcount_init(bs);
924 if (ret != 0) {
925 error_setg_errno(errp, -ret, "Could not initialize refcount handling");
926 goto fail;
929 QLIST_INIT(&s->cluster_allocs);
930 QTAILQ_INIT(&s->discards);
932 /* read qcow2 extensions */
933 if (qcow2_read_extensions(bs, header.header_length, ext_end, NULL,
934 &local_err)) {
935 error_propagate(errp, local_err);
936 ret = -EINVAL;
937 goto fail;
940 /* read the backing file name */
941 if (header.backing_file_offset != 0) {
942 len = header.backing_file_size;
943 if (len > MIN(1023, s->cluster_size - header.backing_file_offset) ||
944 len >= sizeof(bs->backing_file)) {
945 error_setg(errp, "Backing file name too long");
946 ret = -EINVAL;
947 goto fail;
949 ret = bdrv_pread(bs->file, header.backing_file_offset,
950 bs->backing_file, len);
951 if (ret < 0) {
952 error_setg_errno(errp, -ret, "Could not read backing file name");
953 goto fail;
955 bs->backing_file[len] = '\0';
956 s->image_backing_file = g_strdup(bs->backing_file);
959 /* Internal snapshots */
960 s->snapshots_offset = header.snapshots_offset;
961 s->nb_snapshots = header.nb_snapshots;
963 ret = qcow2_read_snapshots(bs);
964 if (ret < 0) {
965 error_setg_errno(errp, -ret, "Could not read snapshots");
966 goto fail;
969 /* Clear unknown autoclear feature bits */
970 if (!bs->read_only && !(flags & BDRV_O_INCOMING) && s->autoclear_features) {
971 s->autoclear_features = 0;
972 ret = qcow2_update_header(bs);
973 if (ret < 0) {
974 error_setg_errno(errp, -ret, "Could not update qcow2 header");
975 goto fail;
979 /* Initialise locks */
980 qemu_co_mutex_init(&s->lock);
982 /* Repair image if dirty */
983 if (!(flags & (BDRV_O_CHECK | BDRV_O_INCOMING)) && !bs->read_only &&
984 (s->incompatible_features & QCOW2_INCOMPAT_DIRTY)) {
985 BdrvCheckResult result = {0};
987 ret = qcow2_check(bs, &result, BDRV_FIX_ERRORS | BDRV_FIX_LEAKS);
988 if (ret < 0) {
989 error_setg_errno(errp, -ret, "Could not repair dirty image");
990 goto fail;
994 /* Enable lazy_refcounts according to image and command line options */
995 s->use_lazy_refcounts = qemu_opt_get_bool(opts, QCOW2_OPT_LAZY_REFCOUNTS,
996 (s->compatible_features & QCOW2_COMPAT_LAZY_REFCOUNTS));
998 s->discard_passthrough[QCOW2_DISCARD_NEVER] = false;
999 s->discard_passthrough[QCOW2_DISCARD_ALWAYS] = true;
1000 s->discard_passthrough[QCOW2_DISCARD_REQUEST] =
1001 qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_REQUEST,
1002 flags & BDRV_O_UNMAP);
1003 s->discard_passthrough[QCOW2_DISCARD_SNAPSHOT] =
1004 qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_SNAPSHOT, true);
1005 s->discard_passthrough[QCOW2_DISCARD_OTHER] =
1006 qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_OTHER, false);
1008 opt_overlap_check = qemu_opt_get(opts, QCOW2_OPT_OVERLAP);
1009 opt_overlap_check_template = qemu_opt_get(opts, QCOW2_OPT_OVERLAP_TEMPLATE);
1010 if (opt_overlap_check_template && opt_overlap_check &&
1011 strcmp(opt_overlap_check_template, opt_overlap_check))
1013 error_setg(errp, "Conflicting values for qcow2 options '"
1014 QCOW2_OPT_OVERLAP "' ('%s') and '" QCOW2_OPT_OVERLAP_TEMPLATE
1015 "' ('%s')", opt_overlap_check, opt_overlap_check_template);
1016 ret = -EINVAL;
1017 goto fail;
1019 if (!opt_overlap_check) {
1020 opt_overlap_check = opt_overlap_check_template ?: "cached";
1023 if (!strcmp(opt_overlap_check, "none")) {
1024 overlap_check_template = 0;
1025 } else if (!strcmp(opt_overlap_check, "constant")) {
1026 overlap_check_template = QCOW2_OL_CONSTANT;
1027 } else if (!strcmp(opt_overlap_check, "cached")) {
1028 overlap_check_template = QCOW2_OL_CACHED;
1029 } else if (!strcmp(opt_overlap_check, "all")) {
1030 overlap_check_template = QCOW2_OL_ALL;
1031 } else {
1032 error_setg(errp, "Unsupported value '%s' for qcow2 option "
1033 "'overlap-check'. Allowed are either of the following: "
1034 "none, constant, cached, all", opt_overlap_check);
1035 ret = -EINVAL;
1036 goto fail;
1039 s->overlap_check = 0;
1040 for (i = 0; i < QCOW2_OL_MAX_BITNR; i++) {
1041 /* overlap-check defines a template bitmask, but every flag may be
1042 * overwritten through the associated boolean option */
1043 s->overlap_check |=
1044 qemu_opt_get_bool(opts, overlap_bool_option_names[i],
1045 overlap_check_template & (1 << i)) << i;
1048 qemu_opts_del(opts);
1049 opts = NULL;
1051 if (s->use_lazy_refcounts && s->qcow_version < 3) {
1052 error_setg(errp, "Lazy refcounts require a qcow2 image with at least "
1053 "qemu 1.1 compatibility level");
1054 ret = -EINVAL;
1055 goto fail;
1058 #ifdef DEBUG_ALLOC
1060 BdrvCheckResult result = {0};
1061 qcow2_check_refcounts(bs, &result, 0);
1063 #endif
1064 return ret;
1066 fail:
1067 qemu_opts_del(opts);
1068 g_free(s->unknown_header_fields);
1069 cleanup_unknown_header_ext(bs);
1070 qcow2_free_snapshots(bs);
1071 qcow2_refcount_close(bs);
1072 qemu_vfree(s->l1_table);
1073 /* else pre-write overlap checks in cache_destroy may crash */
1074 s->l1_table = NULL;
1075 cache_clean_timer_del(bs);
1076 if (s->l2_table_cache) {
1077 qcow2_cache_destroy(bs, s->l2_table_cache);
1079 if (s->refcount_block_cache) {
1080 qcow2_cache_destroy(bs, s->refcount_block_cache);
1082 g_free(s->cluster_cache);
1083 qemu_vfree(s->cluster_data);
1084 return ret;
1087 static void qcow2_refresh_limits(BlockDriverState *bs, Error **errp)
1089 BDRVQcowState *s = bs->opaque;
1091 bs->bl.write_zeroes_alignment = s->cluster_sectors;
1094 static int qcow2_set_key(BlockDriverState *bs, const char *key)
1096 BDRVQcowState *s = bs->opaque;
1097 uint8_t keybuf[16];
1098 int len, i;
1099 Error *err = NULL;
1101 memset(keybuf, 0, 16);
1102 len = strlen(key);
1103 if (len > 16)
1104 len = 16;
1105 /* XXX: we could compress the chars to 7 bits to increase
1106 entropy */
1107 for(i = 0;i < len;i++) {
1108 keybuf[i] = key[i];
1110 assert(bs->encrypted);
1112 qcrypto_cipher_free(s->cipher);
1113 s->cipher = qcrypto_cipher_new(
1114 QCRYPTO_CIPHER_ALG_AES_128,
1115 QCRYPTO_CIPHER_MODE_CBC,
1116 keybuf, G_N_ELEMENTS(keybuf),
1117 &err);
1119 if (!s->cipher) {
1120 /* XXX would be nice if errors in this method could
1121 * be properly propagate to the caller. Would need
1122 * the bdrv_set_key() API signature to be fixed. */
1123 error_free(err);
1124 return -1;
1126 return 0;
1129 /* We have no actual commit/abort logic for qcow2, but we need to write out any
1130 * unwritten data if we reopen read-only. */
1131 static int qcow2_reopen_prepare(BDRVReopenState *state,
1132 BlockReopenQueue *queue, Error **errp)
1134 int ret;
1136 if ((state->flags & BDRV_O_RDWR) == 0) {
1137 ret = bdrv_flush(state->bs);
1138 if (ret < 0) {
1139 return ret;
1142 ret = qcow2_mark_clean(state->bs);
1143 if (ret < 0) {
1144 return ret;
1148 return 0;
1151 static int64_t coroutine_fn qcow2_co_get_block_status(BlockDriverState *bs,
1152 int64_t sector_num, int nb_sectors, int *pnum)
1154 BDRVQcowState *s = bs->opaque;
1155 uint64_t cluster_offset;
1156 int index_in_cluster, ret;
1157 int64_t status = 0;
1159 *pnum = nb_sectors;
1160 qemu_co_mutex_lock(&s->lock);
1161 ret = qcow2_get_cluster_offset(bs, sector_num << 9, pnum, &cluster_offset);
1162 qemu_co_mutex_unlock(&s->lock);
1163 if (ret < 0) {
1164 return ret;
1167 if (cluster_offset != 0 && ret != QCOW2_CLUSTER_COMPRESSED &&
1168 !s->cipher) {
1169 index_in_cluster = sector_num & (s->cluster_sectors - 1);
1170 cluster_offset |= (index_in_cluster << BDRV_SECTOR_BITS);
1171 status |= BDRV_BLOCK_OFFSET_VALID | cluster_offset;
1173 if (ret == QCOW2_CLUSTER_ZERO) {
1174 status |= BDRV_BLOCK_ZERO;
1175 } else if (ret != QCOW2_CLUSTER_UNALLOCATED) {
1176 status |= BDRV_BLOCK_DATA;
1178 return status;
1181 /* handle reading after the end of the backing file */
1182 int qcow2_backing_read1(BlockDriverState *bs, QEMUIOVector *qiov,
1183 int64_t sector_num, int nb_sectors)
1185 int n1;
1186 if ((sector_num + nb_sectors) <= bs->total_sectors)
1187 return nb_sectors;
1188 if (sector_num >= bs->total_sectors)
1189 n1 = 0;
1190 else
1191 n1 = bs->total_sectors - sector_num;
1193 qemu_iovec_memset(qiov, 512 * n1, 0, 512 * (nb_sectors - n1));
1195 return n1;
1198 static coroutine_fn int qcow2_co_readv(BlockDriverState *bs, int64_t sector_num,
1199 int remaining_sectors, QEMUIOVector *qiov)
1201 BDRVQcowState *s = bs->opaque;
1202 int index_in_cluster, n1;
1203 int ret;
1204 int cur_nr_sectors; /* number of sectors in current iteration */
1205 uint64_t cluster_offset = 0;
1206 uint64_t bytes_done = 0;
1207 QEMUIOVector hd_qiov;
1208 uint8_t *cluster_data = NULL;
1210 qemu_iovec_init(&hd_qiov, qiov->niov);
1212 qemu_co_mutex_lock(&s->lock);
1214 while (remaining_sectors != 0) {
1216 /* prepare next request */
1217 cur_nr_sectors = remaining_sectors;
1218 if (s->cipher) {
1219 cur_nr_sectors = MIN(cur_nr_sectors,
1220 QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors);
1223 ret = qcow2_get_cluster_offset(bs, sector_num << 9,
1224 &cur_nr_sectors, &cluster_offset);
1225 if (ret < 0) {
1226 goto fail;
1229 index_in_cluster = sector_num & (s->cluster_sectors - 1);
1231 qemu_iovec_reset(&hd_qiov);
1232 qemu_iovec_concat(&hd_qiov, qiov, bytes_done,
1233 cur_nr_sectors * 512);
1235 switch (ret) {
1236 case QCOW2_CLUSTER_UNALLOCATED:
1238 if (bs->backing_hd) {
1239 /* read from the base image */
1240 n1 = qcow2_backing_read1(bs->backing_hd, &hd_qiov,
1241 sector_num, cur_nr_sectors);
1242 if (n1 > 0) {
1243 QEMUIOVector local_qiov;
1245 qemu_iovec_init(&local_qiov, hd_qiov.niov);
1246 qemu_iovec_concat(&local_qiov, &hd_qiov, 0,
1247 n1 * BDRV_SECTOR_SIZE);
1249 BLKDBG_EVENT(bs->file, BLKDBG_READ_BACKING_AIO);
1250 qemu_co_mutex_unlock(&s->lock);
1251 ret = bdrv_co_readv(bs->backing_hd, sector_num,
1252 n1, &local_qiov);
1253 qemu_co_mutex_lock(&s->lock);
1255 qemu_iovec_destroy(&local_qiov);
1257 if (ret < 0) {
1258 goto fail;
1261 } else {
1262 /* Note: in this case, no need to wait */
1263 qemu_iovec_memset(&hd_qiov, 0, 0, 512 * cur_nr_sectors);
1265 break;
1267 case QCOW2_CLUSTER_ZERO:
1268 qemu_iovec_memset(&hd_qiov, 0, 0, 512 * cur_nr_sectors);
1269 break;
1271 case QCOW2_CLUSTER_COMPRESSED:
1272 /* add AIO support for compressed blocks ? */
1273 ret = qcow2_decompress_cluster(bs, cluster_offset);
1274 if (ret < 0) {
1275 goto fail;
1278 qemu_iovec_from_buf(&hd_qiov, 0,
1279 s->cluster_cache + index_in_cluster * 512,
1280 512 * cur_nr_sectors);
1281 break;
1283 case QCOW2_CLUSTER_NORMAL:
1284 if ((cluster_offset & 511) != 0) {
1285 ret = -EIO;
1286 goto fail;
1289 if (bs->encrypted) {
1290 assert(s->cipher);
1293 * For encrypted images, read everything into a temporary
1294 * contiguous buffer on which the AES functions can work.
1296 if (!cluster_data) {
1297 cluster_data =
1298 qemu_try_blockalign(bs->file, QCOW_MAX_CRYPT_CLUSTERS
1299 * s->cluster_size);
1300 if (cluster_data == NULL) {
1301 ret = -ENOMEM;
1302 goto fail;
1306 assert(cur_nr_sectors <=
1307 QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors);
1308 qemu_iovec_reset(&hd_qiov);
1309 qemu_iovec_add(&hd_qiov, cluster_data,
1310 512 * cur_nr_sectors);
1313 BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO);
1314 qemu_co_mutex_unlock(&s->lock);
1315 ret = bdrv_co_readv(bs->file,
1316 (cluster_offset >> 9) + index_in_cluster,
1317 cur_nr_sectors, &hd_qiov);
1318 qemu_co_mutex_lock(&s->lock);
1319 if (ret < 0) {
1320 goto fail;
1322 if (bs->encrypted) {
1323 assert(s->cipher);
1324 Error *err = NULL;
1325 if (qcow2_encrypt_sectors(s, sector_num, cluster_data,
1326 cluster_data, cur_nr_sectors, false,
1327 &err) < 0) {
1328 error_free(err);
1329 ret = -EIO;
1330 goto fail;
1332 qemu_iovec_from_buf(qiov, bytes_done,
1333 cluster_data, 512 * cur_nr_sectors);
1335 break;
1337 default:
1338 g_assert_not_reached();
1339 ret = -EIO;
1340 goto fail;
1343 remaining_sectors -= cur_nr_sectors;
1344 sector_num += cur_nr_sectors;
1345 bytes_done += cur_nr_sectors * 512;
1347 ret = 0;
1349 fail:
1350 qemu_co_mutex_unlock(&s->lock);
1352 qemu_iovec_destroy(&hd_qiov);
1353 qemu_vfree(cluster_data);
1355 return ret;
1358 static coroutine_fn int qcow2_co_writev(BlockDriverState *bs,
1359 int64_t sector_num,
1360 int remaining_sectors,
1361 QEMUIOVector *qiov)
1363 BDRVQcowState *s = bs->opaque;
1364 int index_in_cluster;
1365 int ret;
1366 int cur_nr_sectors; /* number of sectors in current iteration */
1367 uint64_t cluster_offset;
1368 QEMUIOVector hd_qiov;
1369 uint64_t bytes_done = 0;
1370 uint8_t *cluster_data = NULL;
1371 QCowL2Meta *l2meta = NULL;
1373 trace_qcow2_writev_start_req(qemu_coroutine_self(), sector_num,
1374 remaining_sectors);
1376 qemu_iovec_init(&hd_qiov, qiov->niov);
1378 s->cluster_cache_offset = -1; /* disable compressed cache */
1380 qemu_co_mutex_lock(&s->lock);
1382 while (remaining_sectors != 0) {
1384 l2meta = NULL;
1386 trace_qcow2_writev_start_part(qemu_coroutine_self());
1387 index_in_cluster = sector_num & (s->cluster_sectors - 1);
1388 cur_nr_sectors = remaining_sectors;
1389 if (bs->encrypted &&
1390 cur_nr_sectors >
1391 QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors - index_in_cluster) {
1392 cur_nr_sectors =
1393 QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors - index_in_cluster;
1396 ret = qcow2_alloc_cluster_offset(bs, sector_num << 9,
1397 &cur_nr_sectors, &cluster_offset, &l2meta);
1398 if (ret < 0) {
1399 goto fail;
1402 assert((cluster_offset & 511) == 0);
1404 qemu_iovec_reset(&hd_qiov);
1405 qemu_iovec_concat(&hd_qiov, qiov, bytes_done,
1406 cur_nr_sectors * 512);
1408 if (bs->encrypted) {
1409 Error *err = NULL;
1410 assert(s->cipher);
1411 if (!cluster_data) {
1412 cluster_data = qemu_try_blockalign(bs->file,
1413 QCOW_MAX_CRYPT_CLUSTERS
1414 * s->cluster_size);
1415 if (cluster_data == NULL) {
1416 ret = -ENOMEM;
1417 goto fail;
1421 assert(hd_qiov.size <=
1422 QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size);
1423 qemu_iovec_to_buf(&hd_qiov, 0, cluster_data, hd_qiov.size);
1425 if (qcow2_encrypt_sectors(s, sector_num, cluster_data,
1426 cluster_data, cur_nr_sectors,
1427 true, &err) < 0) {
1428 error_free(err);
1429 ret = -EIO;
1430 goto fail;
1433 qemu_iovec_reset(&hd_qiov);
1434 qemu_iovec_add(&hd_qiov, cluster_data,
1435 cur_nr_sectors * 512);
1438 ret = qcow2_pre_write_overlap_check(bs, 0,
1439 cluster_offset + index_in_cluster * BDRV_SECTOR_SIZE,
1440 cur_nr_sectors * BDRV_SECTOR_SIZE);
1441 if (ret < 0) {
1442 goto fail;
1445 qemu_co_mutex_unlock(&s->lock);
1446 BLKDBG_EVENT(bs->file, BLKDBG_WRITE_AIO);
1447 trace_qcow2_writev_data(qemu_coroutine_self(),
1448 (cluster_offset >> 9) + index_in_cluster);
1449 ret = bdrv_co_writev(bs->file,
1450 (cluster_offset >> 9) + index_in_cluster,
1451 cur_nr_sectors, &hd_qiov);
1452 qemu_co_mutex_lock(&s->lock);
1453 if (ret < 0) {
1454 goto fail;
1457 while (l2meta != NULL) {
1458 QCowL2Meta *next;
1460 ret = qcow2_alloc_cluster_link_l2(bs, l2meta);
1461 if (ret < 0) {
1462 goto fail;
1465 /* Take the request off the list of running requests */
1466 if (l2meta->nb_clusters != 0) {
1467 QLIST_REMOVE(l2meta, next_in_flight);
1470 qemu_co_queue_restart_all(&l2meta->dependent_requests);
1472 next = l2meta->next;
1473 g_free(l2meta);
1474 l2meta = next;
1477 remaining_sectors -= cur_nr_sectors;
1478 sector_num += cur_nr_sectors;
1479 bytes_done += cur_nr_sectors * 512;
1480 trace_qcow2_writev_done_part(qemu_coroutine_self(), cur_nr_sectors);
1482 ret = 0;
1484 fail:
1485 qemu_co_mutex_unlock(&s->lock);
1487 while (l2meta != NULL) {
1488 QCowL2Meta *next;
1490 if (l2meta->nb_clusters != 0) {
1491 QLIST_REMOVE(l2meta, next_in_flight);
1493 qemu_co_queue_restart_all(&l2meta->dependent_requests);
1495 next = l2meta->next;
1496 g_free(l2meta);
1497 l2meta = next;
1500 qemu_iovec_destroy(&hd_qiov);
1501 qemu_vfree(cluster_data);
1502 trace_qcow2_writev_done_req(qemu_coroutine_self(), ret);
1504 return ret;
1507 static void qcow2_close(BlockDriverState *bs)
1509 BDRVQcowState *s = bs->opaque;
1510 qemu_vfree(s->l1_table);
1511 /* else pre-write overlap checks in cache_destroy may crash */
1512 s->l1_table = NULL;
1514 if (!(bs->open_flags & BDRV_O_INCOMING)) {
1515 int ret1, ret2;
1517 ret1 = qcow2_cache_flush(bs, s->l2_table_cache);
1518 ret2 = qcow2_cache_flush(bs, s->refcount_block_cache);
1520 if (ret1) {
1521 error_report("Failed to flush the L2 table cache: %s",
1522 strerror(-ret1));
1524 if (ret2) {
1525 error_report("Failed to flush the refcount block cache: %s",
1526 strerror(-ret2));
1529 if (!ret1 && !ret2) {
1530 qcow2_mark_clean(bs);
1534 cache_clean_timer_del(bs);
1535 qcow2_cache_destroy(bs, s->l2_table_cache);
1536 qcow2_cache_destroy(bs, s->refcount_block_cache);
1538 qcrypto_cipher_free(s->cipher);
1539 s->cipher = NULL;
1541 g_free(s->unknown_header_fields);
1542 cleanup_unknown_header_ext(bs);
1544 g_free(s->image_backing_file);
1545 g_free(s->image_backing_format);
1547 g_free(s->cluster_cache);
1548 qemu_vfree(s->cluster_data);
1549 qcow2_refcount_close(bs);
1550 qcow2_free_snapshots(bs);
1553 static void qcow2_invalidate_cache(BlockDriverState *bs, Error **errp)
1555 BDRVQcowState *s = bs->opaque;
1556 int flags = s->flags;
1557 QCryptoCipher *cipher = NULL;
1558 QDict *options;
1559 Error *local_err = NULL;
1560 int ret;
1563 * Backing files are read-only which makes all of their metadata immutable,
1564 * that means we don't have to worry about reopening them here.
1567 cipher = s->cipher;
1568 s->cipher = NULL;
1570 qcow2_close(bs);
1572 bdrv_invalidate_cache(bs->file, &local_err);
1573 if (local_err) {
1574 error_propagate(errp, local_err);
1575 return;
1578 memset(s, 0, sizeof(BDRVQcowState));
1579 options = qdict_clone_shallow(bs->options);
1581 ret = qcow2_open(bs, options, flags, &local_err);
1582 QDECREF(options);
1583 if (local_err) {
1584 error_setg(errp, "Could not reopen qcow2 layer: %s",
1585 error_get_pretty(local_err));
1586 error_free(local_err);
1587 return;
1588 } else if (ret < 0) {
1589 error_setg_errno(errp, -ret, "Could not reopen qcow2 layer");
1590 return;
1593 s->cipher = cipher;
1596 static size_t header_ext_add(char *buf, uint32_t magic, const void *s,
1597 size_t len, size_t buflen)
1599 QCowExtension *ext_backing_fmt = (QCowExtension*) buf;
1600 size_t ext_len = sizeof(QCowExtension) + ((len + 7) & ~7);
1602 if (buflen < ext_len) {
1603 return -ENOSPC;
1606 *ext_backing_fmt = (QCowExtension) {
1607 .magic = cpu_to_be32(magic),
1608 .len = cpu_to_be32(len),
1610 memcpy(buf + sizeof(QCowExtension), s, len);
1612 return ext_len;
1616 * Updates the qcow2 header, including the variable length parts of it, i.e.
1617 * the backing file name and all extensions. qcow2 was not designed to allow
1618 * such changes, so if we run out of space (we can only use the first cluster)
1619 * this function may fail.
1621 * Returns 0 on success, -errno in error cases.
1623 int qcow2_update_header(BlockDriverState *bs)
1625 BDRVQcowState *s = bs->opaque;
1626 QCowHeader *header;
1627 char *buf;
1628 size_t buflen = s->cluster_size;
1629 int ret;
1630 uint64_t total_size;
1631 uint32_t refcount_table_clusters;
1632 size_t header_length;
1633 Qcow2UnknownHeaderExtension *uext;
1635 buf = qemu_blockalign(bs, buflen);
1637 /* Header structure */
1638 header = (QCowHeader*) buf;
1640 if (buflen < sizeof(*header)) {
1641 ret = -ENOSPC;
1642 goto fail;
1645 header_length = sizeof(*header) + s->unknown_header_fields_size;
1646 total_size = bs->total_sectors * BDRV_SECTOR_SIZE;
1647 refcount_table_clusters = s->refcount_table_size >> (s->cluster_bits - 3);
1649 *header = (QCowHeader) {
1650 /* Version 2 fields */
1651 .magic = cpu_to_be32(QCOW_MAGIC),
1652 .version = cpu_to_be32(s->qcow_version),
1653 .backing_file_offset = 0,
1654 .backing_file_size = 0,
1655 .cluster_bits = cpu_to_be32(s->cluster_bits),
1656 .size = cpu_to_be64(total_size),
1657 .crypt_method = cpu_to_be32(s->crypt_method_header),
1658 .l1_size = cpu_to_be32(s->l1_size),
1659 .l1_table_offset = cpu_to_be64(s->l1_table_offset),
1660 .refcount_table_offset = cpu_to_be64(s->refcount_table_offset),
1661 .refcount_table_clusters = cpu_to_be32(refcount_table_clusters),
1662 .nb_snapshots = cpu_to_be32(s->nb_snapshots),
1663 .snapshots_offset = cpu_to_be64(s->snapshots_offset),
1665 /* Version 3 fields */
1666 .incompatible_features = cpu_to_be64(s->incompatible_features),
1667 .compatible_features = cpu_to_be64(s->compatible_features),
1668 .autoclear_features = cpu_to_be64(s->autoclear_features),
1669 .refcount_order = cpu_to_be32(s->refcount_order),
1670 .header_length = cpu_to_be32(header_length),
1673 /* For older versions, write a shorter header */
1674 switch (s->qcow_version) {
1675 case 2:
1676 ret = offsetof(QCowHeader, incompatible_features);
1677 break;
1678 case 3:
1679 ret = sizeof(*header);
1680 break;
1681 default:
1682 ret = -EINVAL;
1683 goto fail;
1686 buf += ret;
1687 buflen -= ret;
1688 memset(buf, 0, buflen);
1690 /* Preserve any unknown field in the header */
1691 if (s->unknown_header_fields_size) {
1692 if (buflen < s->unknown_header_fields_size) {
1693 ret = -ENOSPC;
1694 goto fail;
1697 memcpy(buf, s->unknown_header_fields, s->unknown_header_fields_size);
1698 buf += s->unknown_header_fields_size;
1699 buflen -= s->unknown_header_fields_size;
1702 /* Backing file format header extension */
1703 if (s->image_backing_format) {
1704 ret = header_ext_add(buf, QCOW2_EXT_MAGIC_BACKING_FORMAT,
1705 s->image_backing_format,
1706 strlen(s->image_backing_format),
1707 buflen);
1708 if (ret < 0) {
1709 goto fail;
1712 buf += ret;
1713 buflen -= ret;
1716 /* Feature table */
1717 Qcow2Feature features[] = {
1719 .type = QCOW2_FEAT_TYPE_INCOMPATIBLE,
1720 .bit = QCOW2_INCOMPAT_DIRTY_BITNR,
1721 .name = "dirty bit",
1724 .type = QCOW2_FEAT_TYPE_INCOMPATIBLE,
1725 .bit = QCOW2_INCOMPAT_CORRUPT_BITNR,
1726 .name = "corrupt bit",
1729 .type = QCOW2_FEAT_TYPE_COMPATIBLE,
1730 .bit = QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR,
1731 .name = "lazy refcounts",
1735 ret = header_ext_add(buf, QCOW2_EXT_MAGIC_FEATURE_TABLE,
1736 features, sizeof(features), buflen);
1737 if (ret < 0) {
1738 goto fail;
1740 buf += ret;
1741 buflen -= ret;
1743 /* Keep unknown header extensions */
1744 QLIST_FOREACH(uext, &s->unknown_header_ext, next) {
1745 ret = header_ext_add(buf, uext->magic, uext->data, uext->len, buflen);
1746 if (ret < 0) {
1747 goto fail;
1750 buf += ret;
1751 buflen -= ret;
1754 /* End of header extensions */
1755 ret = header_ext_add(buf, QCOW2_EXT_MAGIC_END, NULL, 0, buflen);
1756 if (ret < 0) {
1757 goto fail;
1760 buf += ret;
1761 buflen -= ret;
1763 /* Backing file name */
1764 if (s->image_backing_file) {
1765 size_t backing_file_len = strlen(s->image_backing_file);
1767 if (buflen < backing_file_len) {
1768 ret = -ENOSPC;
1769 goto fail;
1772 /* Using strncpy is ok here, since buf is not NUL-terminated. */
1773 strncpy(buf, s->image_backing_file, buflen);
1775 header->backing_file_offset = cpu_to_be64(buf - ((char*) header));
1776 header->backing_file_size = cpu_to_be32(backing_file_len);
1779 /* Write the new header */
1780 ret = bdrv_pwrite(bs->file, 0, header, s->cluster_size);
1781 if (ret < 0) {
1782 goto fail;
1785 ret = 0;
1786 fail:
1787 qemu_vfree(header);
1788 return ret;
1791 static int qcow2_change_backing_file(BlockDriverState *bs,
1792 const char *backing_file, const char *backing_fmt)
1794 BDRVQcowState *s = bs->opaque;
1796 pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: "");
1797 pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: "");
1799 g_free(s->image_backing_file);
1800 g_free(s->image_backing_format);
1802 s->image_backing_file = backing_file ? g_strdup(bs->backing_file) : NULL;
1803 s->image_backing_format = backing_fmt ? g_strdup(bs->backing_format) : NULL;
1805 return qcow2_update_header(bs);
1808 static int preallocate(BlockDriverState *bs)
1810 uint64_t nb_sectors;
1811 uint64_t offset;
1812 uint64_t host_offset = 0;
1813 int num;
1814 int ret;
1815 QCowL2Meta *meta;
1817 nb_sectors = bdrv_nb_sectors(bs);
1818 offset = 0;
1820 while (nb_sectors) {
1821 num = MIN(nb_sectors, INT_MAX >> BDRV_SECTOR_BITS);
1822 ret = qcow2_alloc_cluster_offset(bs, offset, &num,
1823 &host_offset, &meta);
1824 if (ret < 0) {
1825 return ret;
1828 while (meta) {
1829 QCowL2Meta *next = meta->next;
1831 ret = qcow2_alloc_cluster_link_l2(bs, meta);
1832 if (ret < 0) {
1833 qcow2_free_any_clusters(bs, meta->alloc_offset,
1834 meta->nb_clusters, QCOW2_DISCARD_NEVER);
1835 return ret;
1838 /* There are no dependent requests, but we need to remove our
1839 * request from the list of in-flight requests */
1840 QLIST_REMOVE(meta, next_in_flight);
1842 g_free(meta);
1843 meta = next;
1846 /* TODO Preallocate data if requested */
1848 nb_sectors -= num;
1849 offset += num << BDRV_SECTOR_BITS;
1853 * It is expected that the image file is large enough to actually contain
1854 * all of the allocated clusters (otherwise we get failing reads after
1855 * EOF). Extend the image to the last allocated sector.
1857 if (host_offset != 0) {
1858 uint8_t buf[BDRV_SECTOR_SIZE];
1859 memset(buf, 0, BDRV_SECTOR_SIZE);
1860 ret = bdrv_write(bs->file, (host_offset >> BDRV_SECTOR_BITS) + num - 1,
1861 buf, 1);
1862 if (ret < 0) {
1863 return ret;
1867 return 0;
1870 static int qcow2_create2(const char *filename, int64_t total_size,
1871 const char *backing_file, const char *backing_format,
1872 int flags, size_t cluster_size, PreallocMode prealloc,
1873 QemuOpts *opts, int version, int refcount_order,
1874 Error **errp)
1876 /* Calculate cluster_bits */
1877 int cluster_bits;
1878 cluster_bits = ctz32(cluster_size);
1879 if (cluster_bits < MIN_CLUSTER_BITS || cluster_bits > MAX_CLUSTER_BITS ||
1880 (1 << cluster_bits) != cluster_size)
1882 error_setg(errp, "Cluster size must be a power of two between %d and "
1883 "%dk", 1 << MIN_CLUSTER_BITS, 1 << (MAX_CLUSTER_BITS - 10));
1884 return -EINVAL;
1888 * Open the image file and write a minimal qcow2 header.
1890 * We keep things simple and start with a zero-sized image. We also
1891 * do without refcount blocks or a L1 table for now. We'll fix the
1892 * inconsistency later.
1894 * We do need a refcount table because growing the refcount table means
1895 * allocating two new refcount blocks - the seconds of which would be at
1896 * 2 GB for 64k clusters, and we don't want to have a 2 GB initial file
1897 * size for any qcow2 image.
1899 BlockDriverState* bs;
1900 QCowHeader *header;
1901 uint64_t* refcount_table;
1902 Error *local_err = NULL;
1903 int ret;
1905 if (prealloc == PREALLOC_MODE_FULL || prealloc == PREALLOC_MODE_FALLOC) {
1906 /* Note: The following calculation does not need to be exact; if it is a
1907 * bit off, either some bytes will be "leaked" (which is fine) or we
1908 * will need to increase the file size by some bytes (which is fine,
1909 * too, as long as the bulk is allocated here). Therefore, using
1910 * floating point arithmetic is fine. */
1911 int64_t meta_size = 0;
1912 uint64_t nreftablee, nrefblocke, nl1e, nl2e;
1913 int64_t aligned_total_size = align_offset(total_size, cluster_size);
1914 int refblock_bits, refblock_size;
1915 /* refcount entry size in bytes */
1916 double rces = (1 << refcount_order) / 8.;
1918 /* see qcow2_open() */
1919 refblock_bits = cluster_bits - (refcount_order - 3);
1920 refblock_size = 1 << refblock_bits;
1922 /* header: 1 cluster */
1923 meta_size += cluster_size;
1925 /* total size of L2 tables */
1926 nl2e = aligned_total_size / cluster_size;
1927 nl2e = align_offset(nl2e, cluster_size / sizeof(uint64_t));
1928 meta_size += nl2e * sizeof(uint64_t);
1930 /* total size of L1 tables */
1931 nl1e = nl2e * sizeof(uint64_t) / cluster_size;
1932 nl1e = align_offset(nl1e, cluster_size / sizeof(uint64_t));
1933 meta_size += nl1e * sizeof(uint64_t);
1935 /* total size of refcount blocks
1937 * note: every host cluster is reference-counted, including metadata
1938 * (even refcount blocks are recursively included).
1939 * Let:
1940 * a = total_size (this is the guest disk size)
1941 * m = meta size not including refcount blocks and refcount tables
1942 * c = cluster size
1943 * y1 = number of refcount blocks entries
1944 * y2 = meta size including everything
1945 * rces = refcount entry size in bytes
1946 * then,
1947 * y1 = (y2 + a)/c
1948 * y2 = y1 * rces + y1 * rces * sizeof(u64) / c + m
1949 * we can get y1:
1950 * y1 = (a + m) / (c - rces - rces * sizeof(u64) / c)
1952 nrefblocke = (aligned_total_size + meta_size + cluster_size)
1953 / (cluster_size - rces - rces * sizeof(uint64_t)
1954 / cluster_size);
1955 meta_size += DIV_ROUND_UP(nrefblocke, refblock_size) * cluster_size;
1957 /* total size of refcount tables */
1958 nreftablee = nrefblocke / refblock_size;
1959 nreftablee = align_offset(nreftablee, cluster_size / sizeof(uint64_t));
1960 meta_size += nreftablee * sizeof(uint64_t);
1962 qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
1963 aligned_total_size + meta_size, &error_abort);
1964 qemu_opt_set(opts, BLOCK_OPT_PREALLOC, PreallocMode_lookup[prealloc],
1965 &error_abort);
1968 ret = bdrv_create_file(filename, opts, &local_err);
1969 if (ret < 0) {
1970 error_propagate(errp, local_err);
1971 return ret;
1974 bs = NULL;
1975 ret = bdrv_open(&bs, filename, NULL, NULL, BDRV_O_RDWR | BDRV_O_PROTOCOL,
1976 NULL, &local_err);
1977 if (ret < 0) {
1978 error_propagate(errp, local_err);
1979 return ret;
1982 /* Write the header */
1983 QEMU_BUILD_BUG_ON((1 << MIN_CLUSTER_BITS) < sizeof(*header));
1984 header = g_malloc0(cluster_size);
1985 *header = (QCowHeader) {
1986 .magic = cpu_to_be32(QCOW_MAGIC),
1987 .version = cpu_to_be32(version),
1988 .cluster_bits = cpu_to_be32(cluster_bits),
1989 .size = cpu_to_be64(0),
1990 .l1_table_offset = cpu_to_be64(0),
1991 .l1_size = cpu_to_be32(0),
1992 .refcount_table_offset = cpu_to_be64(cluster_size),
1993 .refcount_table_clusters = cpu_to_be32(1),
1994 .refcount_order = cpu_to_be32(refcount_order),
1995 .header_length = cpu_to_be32(sizeof(*header)),
1998 if (flags & BLOCK_FLAG_ENCRYPT) {
1999 header->crypt_method = cpu_to_be32(QCOW_CRYPT_AES);
2000 } else {
2001 header->crypt_method = cpu_to_be32(QCOW_CRYPT_NONE);
2004 if (flags & BLOCK_FLAG_LAZY_REFCOUNTS) {
2005 header->compatible_features |=
2006 cpu_to_be64(QCOW2_COMPAT_LAZY_REFCOUNTS);
2009 ret = bdrv_pwrite(bs, 0, header, cluster_size);
2010 g_free(header);
2011 if (ret < 0) {
2012 error_setg_errno(errp, -ret, "Could not write qcow2 header");
2013 goto out;
2016 /* Write a refcount table with one refcount block */
2017 refcount_table = g_malloc0(2 * cluster_size);
2018 refcount_table[0] = cpu_to_be64(2 * cluster_size);
2019 ret = bdrv_pwrite(bs, cluster_size, refcount_table, 2 * cluster_size);
2020 g_free(refcount_table);
2022 if (ret < 0) {
2023 error_setg_errno(errp, -ret, "Could not write refcount table");
2024 goto out;
2027 bdrv_unref(bs);
2028 bs = NULL;
2031 * And now open the image and make it consistent first (i.e. increase the
2032 * refcount of the cluster that is occupied by the header and the refcount
2033 * table)
2035 ret = bdrv_open(&bs, filename, NULL, NULL,
2036 BDRV_O_RDWR | BDRV_O_CACHE_WB | BDRV_O_NO_FLUSH,
2037 &bdrv_qcow2, &local_err);
2038 if (ret < 0) {
2039 error_propagate(errp, local_err);
2040 goto out;
2043 ret = qcow2_alloc_clusters(bs, 3 * cluster_size);
2044 if (ret < 0) {
2045 error_setg_errno(errp, -ret, "Could not allocate clusters for qcow2 "
2046 "header and refcount table");
2047 goto out;
2049 } else if (ret != 0) {
2050 error_report("Huh, first cluster in empty image is already in use?");
2051 abort();
2054 /* Okay, now that we have a valid image, let's give it the right size */
2055 ret = bdrv_truncate(bs, total_size);
2056 if (ret < 0) {
2057 error_setg_errno(errp, -ret, "Could not resize image");
2058 goto out;
2061 /* Want a backing file? There you go.*/
2062 if (backing_file) {
2063 ret = bdrv_change_backing_file(bs, backing_file, backing_format);
2064 if (ret < 0) {
2065 error_setg_errno(errp, -ret, "Could not assign backing file '%s' "
2066 "with format '%s'", backing_file, backing_format);
2067 goto out;
2071 /* And if we're supposed to preallocate metadata, do that now */
2072 if (prealloc != PREALLOC_MODE_OFF) {
2073 BDRVQcowState *s = bs->opaque;
2074 qemu_co_mutex_lock(&s->lock);
2075 ret = preallocate(bs);
2076 qemu_co_mutex_unlock(&s->lock);
2077 if (ret < 0) {
2078 error_setg_errno(errp, -ret, "Could not preallocate metadata");
2079 goto out;
2083 bdrv_unref(bs);
2084 bs = NULL;
2086 /* Reopen the image without BDRV_O_NO_FLUSH to flush it before returning */
2087 ret = bdrv_open(&bs, filename, NULL, NULL,
2088 BDRV_O_RDWR | BDRV_O_CACHE_WB | BDRV_O_NO_BACKING,
2089 &bdrv_qcow2, &local_err);
2090 if (local_err) {
2091 error_propagate(errp, local_err);
2092 goto out;
2095 ret = 0;
2096 out:
2097 if (bs) {
2098 bdrv_unref(bs);
2100 return ret;
2103 static int qcow2_create(const char *filename, QemuOpts *opts, Error **errp)
2105 char *backing_file = NULL;
2106 char *backing_fmt = NULL;
2107 char *buf = NULL;
2108 uint64_t size = 0;
2109 int flags = 0;
2110 size_t cluster_size = DEFAULT_CLUSTER_SIZE;
2111 PreallocMode prealloc;
2112 int version = 3;
2113 uint64_t refcount_bits = 16;
2114 int refcount_order;
2115 Error *local_err = NULL;
2116 int ret;
2118 /* Read out options */
2119 size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
2120 BDRV_SECTOR_SIZE);
2121 backing_file = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE);
2122 backing_fmt = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FMT);
2123 if (qemu_opt_get_bool_del(opts, BLOCK_OPT_ENCRYPT, false)) {
2124 flags |= BLOCK_FLAG_ENCRYPT;
2126 cluster_size = qemu_opt_get_size_del(opts, BLOCK_OPT_CLUSTER_SIZE,
2127 DEFAULT_CLUSTER_SIZE);
2128 buf = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC);
2129 prealloc = qapi_enum_parse(PreallocMode_lookup, buf,
2130 PREALLOC_MODE_MAX, PREALLOC_MODE_OFF,
2131 &local_err);
2132 if (local_err) {
2133 error_propagate(errp, local_err);
2134 ret = -EINVAL;
2135 goto finish;
2137 g_free(buf);
2138 buf = qemu_opt_get_del(opts, BLOCK_OPT_COMPAT_LEVEL);
2139 if (!buf) {
2140 /* keep the default */
2141 } else if (!strcmp(buf, "0.10")) {
2142 version = 2;
2143 } else if (!strcmp(buf, "1.1")) {
2144 version = 3;
2145 } else {
2146 error_setg(errp, "Invalid compatibility level: '%s'", buf);
2147 ret = -EINVAL;
2148 goto finish;
2151 if (qemu_opt_get_bool_del(opts, BLOCK_OPT_LAZY_REFCOUNTS, false)) {
2152 flags |= BLOCK_FLAG_LAZY_REFCOUNTS;
2155 if (backing_file && prealloc != PREALLOC_MODE_OFF) {
2156 error_setg(errp, "Backing file and preallocation cannot be used at "
2157 "the same time");
2158 ret = -EINVAL;
2159 goto finish;
2162 if (version < 3 && (flags & BLOCK_FLAG_LAZY_REFCOUNTS)) {
2163 error_setg(errp, "Lazy refcounts only supported with compatibility "
2164 "level 1.1 and above (use compat=1.1 or greater)");
2165 ret = -EINVAL;
2166 goto finish;
2169 refcount_bits = qemu_opt_get_number_del(opts, BLOCK_OPT_REFCOUNT_BITS,
2170 refcount_bits);
2171 if (refcount_bits > 64 || !is_power_of_2(refcount_bits)) {
2172 error_setg(errp, "Refcount width must be a power of two and may not "
2173 "exceed 64 bits");
2174 ret = -EINVAL;
2175 goto finish;
2178 if (version < 3 && refcount_bits != 16) {
2179 error_setg(errp, "Different refcount widths than 16 bits require "
2180 "compatibility level 1.1 or above (use compat=1.1 or "
2181 "greater)");
2182 ret = -EINVAL;
2183 goto finish;
2186 refcount_order = ctz32(refcount_bits);
2188 ret = qcow2_create2(filename, size, backing_file, backing_fmt, flags,
2189 cluster_size, prealloc, opts, version, refcount_order,
2190 &local_err);
2191 if (local_err) {
2192 error_propagate(errp, local_err);
2195 finish:
2196 g_free(backing_file);
2197 g_free(backing_fmt);
2198 g_free(buf);
2199 return ret;
2202 static coroutine_fn int qcow2_co_write_zeroes(BlockDriverState *bs,
2203 int64_t sector_num, int nb_sectors, BdrvRequestFlags flags)
2205 int ret;
2206 BDRVQcowState *s = bs->opaque;
2208 /* Emulate misaligned zero writes */
2209 if (sector_num % s->cluster_sectors || nb_sectors % s->cluster_sectors) {
2210 return -ENOTSUP;
2213 /* Whatever is left can use real zero clusters */
2214 qemu_co_mutex_lock(&s->lock);
2215 ret = qcow2_zero_clusters(bs, sector_num << BDRV_SECTOR_BITS,
2216 nb_sectors);
2217 qemu_co_mutex_unlock(&s->lock);
2219 return ret;
2222 static coroutine_fn int qcow2_co_discard(BlockDriverState *bs,
2223 int64_t sector_num, int nb_sectors)
2225 int ret;
2226 BDRVQcowState *s = bs->opaque;
2228 qemu_co_mutex_lock(&s->lock);
2229 ret = qcow2_discard_clusters(bs, sector_num << BDRV_SECTOR_BITS,
2230 nb_sectors, QCOW2_DISCARD_REQUEST, false);
2231 qemu_co_mutex_unlock(&s->lock);
2232 return ret;
2235 static int qcow2_truncate(BlockDriverState *bs, int64_t offset)
2237 BDRVQcowState *s = bs->opaque;
2238 int64_t new_l1_size;
2239 int ret;
2241 if (offset & 511) {
2242 error_report("The new size must be a multiple of 512");
2243 return -EINVAL;
2246 /* cannot proceed if image has snapshots */
2247 if (s->nb_snapshots) {
2248 error_report("Can't resize an image which has snapshots");
2249 return -ENOTSUP;
2252 /* shrinking is currently not supported */
2253 if (offset < bs->total_sectors * 512) {
2254 error_report("qcow2 doesn't support shrinking images yet");
2255 return -ENOTSUP;
2258 new_l1_size = size_to_l1(s, offset);
2259 ret = qcow2_grow_l1_table(bs, new_l1_size, true);
2260 if (ret < 0) {
2261 return ret;
2264 /* write updated header.size */
2265 offset = cpu_to_be64(offset);
2266 ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, size),
2267 &offset, sizeof(uint64_t));
2268 if (ret < 0) {
2269 return ret;
2272 s->l1_vm_state_index = new_l1_size;
2273 return 0;
2276 /* XXX: put compressed sectors first, then all the cluster aligned
2277 tables to avoid losing bytes in alignment */
2278 static int qcow2_write_compressed(BlockDriverState *bs, int64_t sector_num,
2279 const uint8_t *buf, int nb_sectors)
2281 BDRVQcowState *s = bs->opaque;
2282 z_stream strm;
2283 int ret, out_len;
2284 uint8_t *out_buf;
2285 uint64_t cluster_offset;
2287 if (nb_sectors == 0) {
2288 /* align end of file to a sector boundary to ease reading with
2289 sector based I/Os */
2290 cluster_offset = bdrv_getlength(bs->file);
2291 return bdrv_truncate(bs->file, cluster_offset);
2294 if (nb_sectors != s->cluster_sectors) {
2295 ret = -EINVAL;
2297 /* Zero-pad last write if image size is not cluster aligned */
2298 if (sector_num + nb_sectors == bs->total_sectors &&
2299 nb_sectors < s->cluster_sectors) {
2300 uint8_t *pad_buf = qemu_blockalign(bs, s->cluster_size);
2301 memset(pad_buf, 0, s->cluster_size);
2302 memcpy(pad_buf, buf, nb_sectors * BDRV_SECTOR_SIZE);
2303 ret = qcow2_write_compressed(bs, sector_num,
2304 pad_buf, s->cluster_sectors);
2305 qemu_vfree(pad_buf);
2307 return ret;
2310 out_buf = g_malloc(s->cluster_size + (s->cluster_size / 1000) + 128);
2312 /* best compression, small window, no zlib header */
2313 memset(&strm, 0, sizeof(strm));
2314 ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION,
2315 Z_DEFLATED, -12,
2316 9, Z_DEFAULT_STRATEGY);
2317 if (ret != 0) {
2318 ret = -EINVAL;
2319 goto fail;
2322 strm.avail_in = s->cluster_size;
2323 strm.next_in = (uint8_t *)buf;
2324 strm.avail_out = s->cluster_size;
2325 strm.next_out = out_buf;
2327 ret = deflate(&strm, Z_FINISH);
2328 if (ret != Z_STREAM_END && ret != Z_OK) {
2329 deflateEnd(&strm);
2330 ret = -EINVAL;
2331 goto fail;
2333 out_len = strm.next_out - out_buf;
2335 deflateEnd(&strm);
2337 if (ret != Z_STREAM_END || out_len >= s->cluster_size) {
2338 /* could not compress: write normal cluster */
2339 ret = bdrv_write(bs, sector_num, buf, s->cluster_sectors);
2340 if (ret < 0) {
2341 goto fail;
2343 } else {
2344 cluster_offset = qcow2_alloc_compressed_cluster_offset(bs,
2345 sector_num << 9, out_len);
2346 if (!cluster_offset) {
2347 ret = -EIO;
2348 goto fail;
2350 cluster_offset &= s->cluster_offset_mask;
2352 ret = qcow2_pre_write_overlap_check(bs, 0, cluster_offset, out_len);
2353 if (ret < 0) {
2354 goto fail;
2357 BLKDBG_EVENT(bs->file, BLKDBG_WRITE_COMPRESSED);
2358 ret = bdrv_pwrite(bs->file, cluster_offset, out_buf, out_len);
2359 if (ret < 0) {
2360 goto fail;
2364 ret = 0;
2365 fail:
2366 g_free(out_buf);
2367 return ret;
2370 static int make_completely_empty(BlockDriverState *bs)
2372 BDRVQcowState *s = bs->opaque;
2373 int ret, l1_clusters;
2374 int64_t offset;
2375 uint64_t *new_reftable = NULL;
2376 uint64_t rt_entry, l1_size2;
2377 struct {
2378 uint64_t l1_offset;
2379 uint64_t reftable_offset;
2380 uint32_t reftable_clusters;
2381 } QEMU_PACKED l1_ofs_rt_ofs_cls;
2383 ret = qcow2_cache_empty(bs, s->l2_table_cache);
2384 if (ret < 0) {
2385 goto fail;
2388 ret = qcow2_cache_empty(bs, s->refcount_block_cache);
2389 if (ret < 0) {
2390 goto fail;
2393 /* Refcounts will be broken utterly */
2394 ret = qcow2_mark_dirty(bs);
2395 if (ret < 0) {
2396 goto fail;
2399 BLKDBG_EVENT(bs->file, BLKDBG_L1_UPDATE);
2401 l1_clusters = DIV_ROUND_UP(s->l1_size, s->cluster_size / sizeof(uint64_t));
2402 l1_size2 = (uint64_t)s->l1_size * sizeof(uint64_t);
2404 /* After this call, neither the in-memory nor the on-disk refcount
2405 * information accurately describe the actual references */
2407 ret = bdrv_write_zeroes(bs->file, s->l1_table_offset / BDRV_SECTOR_SIZE,
2408 l1_clusters * s->cluster_sectors, 0);
2409 if (ret < 0) {
2410 goto fail_broken_refcounts;
2412 memset(s->l1_table, 0, l1_size2);
2414 BLKDBG_EVENT(bs->file, BLKDBG_EMPTY_IMAGE_PREPARE);
2416 /* Overwrite enough clusters at the beginning of the sectors to place
2417 * the refcount table, a refcount block and the L1 table in; this may
2418 * overwrite parts of the existing refcount and L1 table, which is not
2419 * an issue because the dirty flag is set, complete data loss is in fact
2420 * desired and partial data loss is consequently fine as well */
2421 ret = bdrv_write_zeroes(bs->file, s->cluster_size / BDRV_SECTOR_SIZE,
2422 (2 + l1_clusters) * s->cluster_size /
2423 BDRV_SECTOR_SIZE, 0);
2424 /* This call (even if it failed overall) may have overwritten on-disk
2425 * refcount structures; in that case, the in-memory refcount information
2426 * will probably differ from the on-disk information which makes the BDS
2427 * unusable */
2428 if (ret < 0) {
2429 goto fail_broken_refcounts;
2432 BLKDBG_EVENT(bs->file, BLKDBG_L1_UPDATE);
2433 BLKDBG_EVENT(bs->file, BLKDBG_REFTABLE_UPDATE);
2435 /* "Create" an empty reftable (one cluster) directly after the image
2436 * header and an empty L1 table three clusters after the image header;
2437 * the cluster between those two will be used as the first refblock */
2438 cpu_to_be64w(&l1_ofs_rt_ofs_cls.l1_offset, 3 * s->cluster_size);
2439 cpu_to_be64w(&l1_ofs_rt_ofs_cls.reftable_offset, s->cluster_size);
2440 cpu_to_be32w(&l1_ofs_rt_ofs_cls.reftable_clusters, 1);
2441 ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, l1_table_offset),
2442 &l1_ofs_rt_ofs_cls, sizeof(l1_ofs_rt_ofs_cls));
2443 if (ret < 0) {
2444 goto fail_broken_refcounts;
2447 s->l1_table_offset = 3 * s->cluster_size;
2449 new_reftable = g_try_new0(uint64_t, s->cluster_size / sizeof(uint64_t));
2450 if (!new_reftable) {
2451 ret = -ENOMEM;
2452 goto fail_broken_refcounts;
2455 s->refcount_table_offset = s->cluster_size;
2456 s->refcount_table_size = s->cluster_size / sizeof(uint64_t);
2458 g_free(s->refcount_table);
2459 s->refcount_table = new_reftable;
2460 new_reftable = NULL;
2462 /* Now the in-memory refcount information again corresponds to the on-disk
2463 * information (reftable is empty and no refblocks (the refblock cache is
2464 * empty)); however, this means some clusters (e.g. the image header) are
2465 * referenced, but not refcounted, but the normal qcow2 code assumes that
2466 * the in-memory information is always correct */
2468 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC);
2470 /* Enter the first refblock into the reftable */
2471 rt_entry = cpu_to_be64(2 * s->cluster_size);
2472 ret = bdrv_pwrite_sync(bs->file, s->cluster_size,
2473 &rt_entry, sizeof(rt_entry));
2474 if (ret < 0) {
2475 goto fail_broken_refcounts;
2477 s->refcount_table[0] = 2 * s->cluster_size;
2479 s->free_cluster_index = 0;
2480 assert(3 + l1_clusters <= s->refcount_block_size);
2481 offset = qcow2_alloc_clusters(bs, 3 * s->cluster_size + l1_size2);
2482 if (offset < 0) {
2483 ret = offset;
2484 goto fail_broken_refcounts;
2485 } else if (offset > 0) {
2486 error_report("First cluster in emptied image is in use");
2487 abort();
2490 /* Now finally the in-memory information corresponds to the on-disk
2491 * structures and is correct */
2492 ret = qcow2_mark_clean(bs);
2493 if (ret < 0) {
2494 goto fail;
2497 ret = bdrv_truncate(bs->file, (3 + l1_clusters) * s->cluster_size);
2498 if (ret < 0) {
2499 goto fail;
2502 return 0;
2504 fail_broken_refcounts:
2505 /* The BDS is unusable at this point. If we wanted to make it usable, we
2506 * would have to call qcow2_refcount_close(), qcow2_refcount_init(),
2507 * qcow2_check_refcounts(), qcow2_refcount_close() and qcow2_refcount_init()
2508 * again. However, because the functions which could have caused this error
2509 * path to be taken are used by those functions as well, it's very likely
2510 * that that sequence will fail as well. Therefore, just eject the BDS. */
2511 bs->drv = NULL;
2513 fail:
2514 g_free(new_reftable);
2515 return ret;
2518 static int qcow2_make_empty(BlockDriverState *bs)
2520 BDRVQcowState *s = bs->opaque;
2521 uint64_t start_sector;
2522 int sector_step = INT_MAX / BDRV_SECTOR_SIZE;
2523 int l1_clusters, ret = 0;
2525 l1_clusters = DIV_ROUND_UP(s->l1_size, s->cluster_size / sizeof(uint64_t));
2527 if (s->qcow_version >= 3 && !s->snapshots &&
2528 3 + l1_clusters <= s->refcount_block_size) {
2529 /* The following function only works for qcow2 v3 images (it requires
2530 * the dirty flag) and only as long as there are no snapshots (because
2531 * it completely empties the image). Furthermore, the L1 table and three
2532 * additional clusters (image header, refcount table, one refcount
2533 * block) have to fit inside one refcount block. */
2534 return make_completely_empty(bs);
2537 /* This fallback code simply discards every active cluster; this is slow,
2538 * but works in all cases */
2539 for (start_sector = 0; start_sector < bs->total_sectors;
2540 start_sector += sector_step)
2542 /* As this function is generally used after committing an external
2543 * snapshot, QCOW2_DISCARD_SNAPSHOT seems appropriate. Also, the
2544 * default action for this kind of discard is to pass the discard,
2545 * which will ideally result in an actually smaller image file, as
2546 * is probably desired. */
2547 ret = qcow2_discard_clusters(bs, start_sector * BDRV_SECTOR_SIZE,
2548 MIN(sector_step,
2549 bs->total_sectors - start_sector),
2550 QCOW2_DISCARD_SNAPSHOT, true);
2551 if (ret < 0) {
2552 break;
2556 return ret;
2559 static coroutine_fn int qcow2_co_flush_to_os(BlockDriverState *bs)
2561 BDRVQcowState *s = bs->opaque;
2562 int ret;
2564 qemu_co_mutex_lock(&s->lock);
2565 ret = qcow2_cache_flush(bs, s->l2_table_cache);
2566 if (ret < 0) {
2567 qemu_co_mutex_unlock(&s->lock);
2568 return ret;
2571 if (qcow2_need_accurate_refcounts(s)) {
2572 ret = qcow2_cache_flush(bs, s->refcount_block_cache);
2573 if (ret < 0) {
2574 qemu_co_mutex_unlock(&s->lock);
2575 return ret;
2578 qemu_co_mutex_unlock(&s->lock);
2580 return 0;
2583 static int qcow2_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
2585 BDRVQcowState *s = bs->opaque;
2586 bdi->unallocated_blocks_are_zero = true;
2587 bdi->can_write_zeroes_with_unmap = (s->qcow_version >= 3);
2588 bdi->cluster_size = s->cluster_size;
2589 bdi->vm_state_offset = qcow2_vm_state_offset(s);
2590 return 0;
2593 static ImageInfoSpecific *qcow2_get_specific_info(BlockDriverState *bs)
2595 BDRVQcowState *s = bs->opaque;
2596 ImageInfoSpecific *spec_info = g_new(ImageInfoSpecific, 1);
2598 *spec_info = (ImageInfoSpecific){
2599 .kind = IMAGE_INFO_SPECIFIC_KIND_QCOW2,
2601 .qcow2 = g_new(ImageInfoSpecificQCow2, 1),
2604 if (s->qcow_version == 2) {
2605 *spec_info->qcow2 = (ImageInfoSpecificQCow2){
2606 .compat = g_strdup("0.10"),
2607 .refcount_bits = s->refcount_bits,
2609 } else if (s->qcow_version == 3) {
2610 *spec_info->qcow2 = (ImageInfoSpecificQCow2){
2611 .compat = g_strdup("1.1"),
2612 .lazy_refcounts = s->compatible_features &
2613 QCOW2_COMPAT_LAZY_REFCOUNTS,
2614 .has_lazy_refcounts = true,
2615 .corrupt = s->incompatible_features &
2616 QCOW2_INCOMPAT_CORRUPT,
2617 .has_corrupt = true,
2618 .refcount_bits = s->refcount_bits,
2622 return spec_info;
2625 #if 0
2626 static void dump_refcounts(BlockDriverState *bs)
2628 BDRVQcowState *s = bs->opaque;
2629 int64_t nb_clusters, k, k1, size;
2630 int refcount;
2632 size = bdrv_getlength(bs->file);
2633 nb_clusters = size_to_clusters(s, size);
2634 for(k = 0; k < nb_clusters;) {
2635 k1 = k;
2636 refcount = get_refcount(bs, k);
2637 k++;
2638 while (k < nb_clusters && get_refcount(bs, k) == refcount)
2639 k++;
2640 printf("%" PRId64 ": refcount=%d nb=%" PRId64 "\n", k, refcount,
2641 k - k1);
2644 #endif
2646 static int qcow2_save_vmstate(BlockDriverState *bs, QEMUIOVector *qiov,
2647 int64_t pos)
2649 BDRVQcowState *s = bs->opaque;
2650 int64_t total_sectors = bs->total_sectors;
2651 bool zero_beyond_eof = bs->zero_beyond_eof;
2652 int ret;
2654 BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_SAVE);
2655 bs->zero_beyond_eof = false;
2656 ret = bdrv_pwritev(bs, qcow2_vm_state_offset(s) + pos, qiov);
2657 bs->zero_beyond_eof = zero_beyond_eof;
2659 /* bdrv_co_do_writev will have increased the total_sectors value to include
2660 * the VM state - the VM state is however not an actual part of the block
2661 * device, therefore, we need to restore the old value. */
2662 bs->total_sectors = total_sectors;
2664 return ret;
2667 static int qcow2_load_vmstate(BlockDriverState *bs, uint8_t *buf,
2668 int64_t pos, int size)
2670 BDRVQcowState *s = bs->opaque;
2671 bool zero_beyond_eof = bs->zero_beyond_eof;
2672 int ret;
2674 BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_LOAD);
2675 bs->zero_beyond_eof = false;
2676 ret = bdrv_pread(bs, qcow2_vm_state_offset(s) + pos, buf, size);
2677 bs->zero_beyond_eof = zero_beyond_eof;
2679 return ret;
2683 * Downgrades an image's version. To achieve this, any incompatible features
2684 * have to be removed.
2686 static int qcow2_downgrade(BlockDriverState *bs, int target_version,
2687 BlockDriverAmendStatusCB *status_cb)
2689 BDRVQcowState *s = bs->opaque;
2690 int current_version = s->qcow_version;
2691 int ret;
2693 if (target_version == current_version) {
2694 return 0;
2695 } else if (target_version > current_version) {
2696 return -EINVAL;
2697 } else if (target_version != 2) {
2698 return -EINVAL;
2701 if (s->refcount_order != 4) {
2702 /* we would have to convert the image to a refcount_order == 4 image
2703 * here; however, since qemu (at the time of writing this) does not
2704 * support anything different than 4 anyway, there is no point in doing
2705 * so right now; however, we should error out (if qemu supports this in
2706 * the future and this code has not been adapted) */
2707 error_report("qcow2_downgrade: Image refcount orders other than 4 are "
2708 "currently not supported.");
2709 return -ENOTSUP;
2712 /* clear incompatible features */
2713 if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) {
2714 ret = qcow2_mark_clean(bs);
2715 if (ret < 0) {
2716 return ret;
2720 /* with QCOW2_INCOMPAT_CORRUPT, it is pretty much impossible to get here in
2721 * the first place; if that happens nonetheless, returning -ENOTSUP is the
2722 * best thing to do anyway */
2724 if (s->incompatible_features) {
2725 return -ENOTSUP;
2728 /* since we can ignore compatible features, we can set them to 0 as well */
2729 s->compatible_features = 0;
2730 /* if lazy refcounts have been used, they have already been fixed through
2731 * clearing the dirty flag */
2733 /* clearing autoclear features is trivial */
2734 s->autoclear_features = 0;
2736 ret = qcow2_expand_zero_clusters(bs, status_cb);
2737 if (ret < 0) {
2738 return ret;
2741 s->qcow_version = target_version;
2742 ret = qcow2_update_header(bs);
2743 if (ret < 0) {
2744 s->qcow_version = current_version;
2745 return ret;
2747 return 0;
2750 static int qcow2_amend_options(BlockDriverState *bs, QemuOpts *opts,
2751 BlockDriverAmendStatusCB *status_cb)
2753 BDRVQcowState *s = bs->opaque;
2754 int old_version = s->qcow_version, new_version = old_version;
2755 uint64_t new_size = 0;
2756 const char *backing_file = NULL, *backing_format = NULL;
2757 bool lazy_refcounts = s->use_lazy_refcounts;
2758 const char *compat = NULL;
2759 uint64_t cluster_size = s->cluster_size;
2760 bool encrypt;
2761 int ret;
2762 QemuOptDesc *desc = opts->list->desc;
2764 while (desc && desc->name) {
2765 if (!qemu_opt_find(opts, desc->name)) {
2766 /* only change explicitly defined options */
2767 desc++;
2768 continue;
2771 if (!strcmp(desc->name, BLOCK_OPT_COMPAT_LEVEL)) {
2772 compat = qemu_opt_get(opts, BLOCK_OPT_COMPAT_LEVEL);
2773 if (!compat) {
2774 /* preserve default */
2775 } else if (!strcmp(compat, "0.10")) {
2776 new_version = 2;
2777 } else if (!strcmp(compat, "1.1")) {
2778 new_version = 3;
2779 } else {
2780 fprintf(stderr, "Unknown compatibility level %s.\n", compat);
2781 return -EINVAL;
2783 } else if (!strcmp(desc->name, BLOCK_OPT_PREALLOC)) {
2784 fprintf(stderr, "Cannot change preallocation mode.\n");
2785 return -ENOTSUP;
2786 } else if (!strcmp(desc->name, BLOCK_OPT_SIZE)) {
2787 new_size = qemu_opt_get_size(opts, BLOCK_OPT_SIZE, 0);
2788 } else if (!strcmp(desc->name, BLOCK_OPT_BACKING_FILE)) {
2789 backing_file = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
2790 } else if (!strcmp(desc->name, BLOCK_OPT_BACKING_FMT)) {
2791 backing_format = qemu_opt_get(opts, BLOCK_OPT_BACKING_FMT);
2792 } else if (!strcmp(desc->name, BLOCK_OPT_ENCRYPT)) {
2793 encrypt = qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT,
2794 !!s->cipher);
2796 if (encrypt != !!s->cipher) {
2797 fprintf(stderr, "Changing the encryption flag is not "
2798 "supported.\n");
2799 return -ENOTSUP;
2801 } else if (!strcmp(desc->name, BLOCK_OPT_CLUSTER_SIZE)) {
2802 cluster_size = qemu_opt_get_size(opts, BLOCK_OPT_CLUSTER_SIZE,
2803 cluster_size);
2804 if (cluster_size != s->cluster_size) {
2805 fprintf(stderr, "Changing the cluster size is not "
2806 "supported.\n");
2807 return -ENOTSUP;
2809 } else if (!strcmp(desc->name, BLOCK_OPT_LAZY_REFCOUNTS)) {
2810 lazy_refcounts = qemu_opt_get_bool(opts, BLOCK_OPT_LAZY_REFCOUNTS,
2811 lazy_refcounts);
2812 } else if (!strcmp(desc->name, BLOCK_OPT_REFCOUNT_BITS)) {
2813 error_report("Cannot change refcount entry width");
2814 return -ENOTSUP;
2815 } else {
2816 /* if this assertion fails, this probably means a new option was
2817 * added without having it covered here */
2818 assert(false);
2821 desc++;
2824 if (new_version != old_version) {
2825 if (new_version > old_version) {
2826 /* Upgrade */
2827 s->qcow_version = new_version;
2828 ret = qcow2_update_header(bs);
2829 if (ret < 0) {
2830 s->qcow_version = old_version;
2831 return ret;
2833 } else {
2834 ret = qcow2_downgrade(bs, new_version, status_cb);
2835 if (ret < 0) {
2836 return ret;
2841 if (backing_file || backing_format) {
2842 ret = qcow2_change_backing_file(bs,
2843 backing_file ?: s->image_backing_file,
2844 backing_format ?: s->image_backing_format);
2845 if (ret < 0) {
2846 return ret;
2850 if (s->use_lazy_refcounts != lazy_refcounts) {
2851 if (lazy_refcounts) {
2852 if (s->qcow_version < 3) {
2853 fprintf(stderr, "Lazy refcounts only supported with compatibility "
2854 "level 1.1 and above (use compat=1.1 or greater)\n");
2855 return -EINVAL;
2857 s->compatible_features |= QCOW2_COMPAT_LAZY_REFCOUNTS;
2858 ret = qcow2_update_header(bs);
2859 if (ret < 0) {
2860 s->compatible_features &= ~QCOW2_COMPAT_LAZY_REFCOUNTS;
2861 return ret;
2863 s->use_lazy_refcounts = true;
2864 } else {
2865 /* make image clean first */
2866 ret = qcow2_mark_clean(bs);
2867 if (ret < 0) {
2868 return ret;
2870 /* now disallow lazy refcounts */
2871 s->compatible_features &= ~QCOW2_COMPAT_LAZY_REFCOUNTS;
2872 ret = qcow2_update_header(bs);
2873 if (ret < 0) {
2874 s->compatible_features |= QCOW2_COMPAT_LAZY_REFCOUNTS;
2875 return ret;
2877 s->use_lazy_refcounts = false;
2881 if (new_size) {
2882 ret = bdrv_truncate(bs, new_size);
2883 if (ret < 0) {
2884 return ret;
2888 return 0;
2892 * If offset or size are negative, respectively, they will not be included in
2893 * the BLOCK_IMAGE_CORRUPTED event emitted.
2894 * fatal will be ignored for read-only BDS; corruptions found there will always
2895 * be considered non-fatal.
2897 void qcow2_signal_corruption(BlockDriverState *bs, bool fatal, int64_t offset,
2898 int64_t size, const char *message_format, ...)
2900 BDRVQcowState *s = bs->opaque;
2901 const char *node_name;
2902 char *message;
2903 va_list ap;
2905 fatal = fatal && !bs->read_only;
2907 if (s->signaled_corruption &&
2908 (!fatal || (s->incompatible_features & QCOW2_INCOMPAT_CORRUPT)))
2910 return;
2913 va_start(ap, message_format);
2914 message = g_strdup_vprintf(message_format, ap);
2915 va_end(ap);
2917 if (fatal) {
2918 fprintf(stderr, "qcow2: Marking image as corrupt: %s; further "
2919 "corruption events will be suppressed\n", message);
2920 } else {
2921 fprintf(stderr, "qcow2: Image is corrupt: %s; further non-fatal "
2922 "corruption events will be suppressed\n", message);
2925 node_name = bdrv_get_node_name(bs);
2926 qapi_event_send_block_image_corrupted(bdrv_get_device_name(bs),
2927 *node_name != '\0', node_name,
2928 message, offset >= 0, offset,
2929 size >= 0, size,
2930 fatal, &error_abort);
2931 g_free(message);
2933 if (fatal) {
2934 qcow2_mark_corrupt(bs);
2935 bs->drv = NULL; /* make BDS unusable */
2938 s->signaled_corruption = true;
2941 static QemuOptsList qcow2_create_opts = {
2942 .name = "qcow2-create-opts",
2943 .head = QTAILQ_HEAD_INITIALIZER(qcow2_create_opts.head),
2944 .desc = {
2946 .name = BLOCK_OPT_SIZE,
2947 .type = QEMU_OPT_SIZE,
2948 .help = "Virtual disk size"
2951 .name = BLOCK_OPT_COMPAT_LEVEL,
2952 .type = QEMU_OPT_STRING,
2953 .help = "Compatibility level (0.10 or 1.1)"
2956 .name = BLOCK_OPT_BACKING_FILE,
2957 .type = QEMU_OPT_STRING,
2958 .help = "File name of a base image"
2961 .name = BLOCK_OPT_BACKING_FMT,
2962 .type = QEMU_OPT_STRING,
2963 .help = "Image format of the base image"
2966 .name = BLOCK_OPT_ENCRYPT,
2967 .type = QEMU_OPT_BOOL,
2968 .help = "Encrypt the image",
2969 .def_value_str = "off"
2972 .name = BLOCK_OPT_CLUSTER_SIZE,
2973 .type = QEMU_OPT_SIZE,
2974 .help = "qcow2 cluster size",
2975 .def_value_str = stringify(DEFAULT_CLUSTER_SIZE)
2978 .name = BLOCK_OPT_PREALLOC,
2979 .type = QEMU_OPT_STRING,
2980 .help = "Preallocation mode (allowed values: off, metadata, "
2981 "falloc, full)"
2984 .name = BLOCK_OPT_LAZY_REFCOUNTS,
2985 .type = QEMU_OPT_BOOL,
2986 .help = "Postpone refcount updates",
2987 .def_value_str = "off"
2990 .name = BLOCK_OPT_REFCOUNT_BITS,
2991 .type = QEMU_OPT_NUMBER,
2992 .help = "Width of a reference count entry in bits",
2993 .def_value_str = "16"
2995 { /* end of list */ }
2999 BlockDriver bdrv_qcow2 = {
3000 .format_name = "qcow2",
3001 .instance_size = sizeof(BDRVQcowState),
3002 .bdrv_probe = qcow2_probe,
3003 .bdrv_open = qcow2_open,
3004 .bdrv_close = qcow2_close,
3005 .bdrv_reopen_prepare = qcow2_reopen_prepare,
3006 .bdrv_create = qcow2_create,
3007 .bdrv_has_zero_init = bdrv_has_zero_init_1,
3008 .bdrv_co_get_block_status = qcow2_co_get_block_status,
3009 .bdrv_set_key = qcow2_set_key,
3011 .bdrv_co_readv = qcow2_co_readv,
3012 .bdrv_co_writev = qcow2_co_writev,
3013 .bdrv_co_flush_to_os = qcow2_co_flush_to_os,
3015 .bdrv_co_write_zeroes = qcow2_co_write_zeroes,
3016 .bdrv_co_discard = qcow2_co_discard,
3017 .bdrv_truncate = qcow2_truncate,
3018 .bdrv_write_compressed = qcow2_write_compressed,
3019 .bdrv_make_empty = qcow2_make_empty,
3021 .bdrv_snapshot_create = qcow2_snapshot_create,
3022 .bdrv_snapshot_goto = qcow2_snapshot_goto,
3023 .bdrv_snapshot_delete = qcow2_snapshot_delete,
3024 .bdrv_snapshot_list = qcow2_snapshot_list,
3025 .bdrv_snapshot_load_tmp = qcow2_snapshot_load_tmp,
3026 .bdrv_get_info = qcow2_get_info,
3027 .bdrv_get_specific_info = qcow2_get_specific_info,
3029 .bdrv_save_vmstate = qcow2_save_vmstate,
3030 .bdrv_load_vmstate = qcow2_load_vmstate,
3032 .supports_backing = true,
3033 .bdrv_change_backing_file = qcow2_change_backing_file,
3035 .bdrv_refresh_limits = qcow2_refresh_limits,
3036 .bdrv_invalidate_cache = qcow2_invalidate_cache,
3038 .create_opts = &qcow2_create_opts,
3039 .bdrv_check = qcow2_check,
3040 .bdrv_amend_options = qcow2_amend_options,
3042 .bdrv_detach_aio_context = qcow2_detach_aio_context,
3043 .bdrv_attach_aio_context = qcow2_attach_aio_context,
3046 static void bdrv_qcow2_init(void)
3048 bdrv_register(&bdrv_qcow2);
3051 block_init(bdrv_qcow2_init);