2 * Block driver for the QCOW version 2 format
4 * Copyright (c) 2004-2006 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 #include "qemu/osdep.h"
25 #include "block/block_int.h"
26 #include "sysemu/block-backend.h"
27 #include "qemu/module.h"
29 #include "block/qcow2.h"
30 #include "qemu/error-report.h"
31 #include "qapi/qmp/qerror.h"
32 #include "qapi/qmp/qbool.h"
33 #include "qapi/util.h"
34 #include "qapi/qmp/types.h"
35 #include "qapi-event.h"
37 #include "qemu/option_int.h"
38 #include "qemu/cutils.h"
39 #include "qemu/bswap.h"
42 Differences with QCOW:
44 - Support for multiple incremental snapshots.
45 - Memory management by reference counts.
46 - Clusters which have a reference count of one have the bit
47 QCOW_OFLAG_COPIED to optimize write performance.
48 - Size of compressed clusters is stored in sectors to reduce bit usage
49 in the cluster offsets.
50 - Support for storing additional data (such as the VM state) in the
52 - If a backing store is used, the cluster size is not constrained
53 (could be backported to QCOW).
54 - L2 tables have always a size of one cluster.
61 } QEMU_PACKED QCowExtension
;
63 #define QCOW2_EXT_MAGIC_END 0
64 #define QCOW2_EXT_MAGIC_BACKING_FORMAT 0xE2792ACA
65 #define QCOW2_EXT_MAGIC_FEATURE_TABLE 0x6803f857
67 static int qcow2_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
69 const QCowHeader
*cow_header
= (const void *)buf
;
71 if (buf_size
>= sizeof(QCowHeader
) &&
72 be32_to_cpu(cow_header
->magic
) == QCOW_MAGIC
&&
73 be32_to_cpu(cow_header
->version
) >= 2)
81 * read qcow2 extension and fill bs
82 * start reading from start_offset
83 * finish reading upon magic of value 0 or when end_offset reached
84 * unknown magic is skipped (future extension this version knows nothing about)
85 * return 0 upon success, non-0 otherwise
87 static int qcow2_read_extensions(BlockDriverState
*bs
, uint64_t start_offset
,
88 uint64_t end_offset
, void **p_feature_table
,
91 BDRVQcow2State
*s
= bs
->opaque
;
97 printf("qcow2_read_extensions: start=%ld end=%ld\n", start_offset
, end_offset
);
99 offset
= start_offset
;
100 while (offset
< end_offset
) {
104 if (offset
> s
->cluster_size
)
105 printf("qcow2_read_extension: suspicious offset %lu\n", offset
);
107 printf("attempting to read extended header in offset %lu\n", offset
);
110 ret
= bdrv_pread(bs
->file
->bs
, offset
, &ext
, sizeof(ext
));
112 error_setg_errno(errp
, -ret
, "qcow2_read_extension: ERROR: "
113 "pread fail from offset %" PRIu64
, offset
);
116 be32_to_cpus(&ext
.magic
);
117 be32_to_cpus(&ext
.len
);
118 offset
+= sizeof(ext
);
120 printf("ext.magic = 0x%x\n", ext
.magic
);
122 if (offset
> end_offset
|| ext
.len
> end_offset
- offset
) {
123 error_setg(errp
, "Header extension too large");
128 case QCOW2_EXT_MAGIC_END
:
131 case QCOW2_EXT_MAGIC_BACKING_FORMAT
:
132 if (ext
.len
>= sizeof(bs
->backing_format
)) {
133 error_setg(errp
, "ERROR: ext_backing_format: len=%" PRIu32
134 " too large (>=%zu)", ext
.len
,
135 sizeof(bs
->backing_format
));
138 ret
= bdrv_pread(bs
->file
->bs
, offset
, bs
->backing_format
, ext
.len
);
140 error_setg_errno(errp
, -ret
, "ERROR: ext_backing_format: "
141 "Could not read format name");
144 bs
->backing_format
[ext
.len
] = '\0';
145 s
->image_backing_format
= g_strdup(bs
->backing_format
);
147 printf("Qcow2: Got format extension %s\n", bs
->backing_format
);
151 case QCOW2_EXT_MAGIC_FEATURE_TABLE
:
152 if (p_feature_table
!= NULL
) {
153 void* feature_table
= g_malloc0(ext
.len
+ 2 * sizeof(Qcow2Feature
));
154 ret
= bdrv_pread(bs
->file
->bs
, offset
, feature_table
, ext
.len
);
156 error_setg_errno(errp
, -ret
, "ERROR: ext_feature_table: "
157 "Could not read table");
161 *p_feature_table
= feature_table
;
166 /* unknown magic - save it in case we need to rewrite the header */
168 Qcow2UnknownHeaderExtension
*uext
;
170 uext
= g_malloc0(sizeof(*uext
) + ext
.len
);
171 uext
->magic
= ext
.magic
;
173 QLIST_INSERT_HEAD(&s
->unknown_header_ext
, uext
, next
);
175 ret
= bdrv_pread(bs
->file
->bs
, offset
, uext
->data
, uext
->len
);
177 error_setg_errno(errp
, -ret
, "ERROR: unknown extension: "
178 "Could not read data");
185 offset
+= ((ext
.len
+ 7) & ~7);
191 static void cleanup_unknown_header_ext(BlockDriverState
*bs
)
193 BDRVQcow2State
*s
= bs
->opaque
;
194 Qcow2UnknownHeaderExtension
*uext
, *next
;
196 QLIST_FOREACH_SAFE(uext
, &s
->unknown_header_ext
, next
, next
) {
197 QLIST_REMOVE(uext
, next
);
202 static void report_unsupported_feature(Error
**errp
, Qcow2Feature
*table
,
205 char *features
= g_strdup("");
208 while (table
&& table
->name
[0] != '\0') {
209 if (table
->type
== QCOW2_FEAT_TYPE_INCOMPATIBLE
) {
210 if (mask
& (1ULL << table
->bit
)) {
212 features
= g_strdup_printf("%s%s%.46s", old
, *old
? ", " : "",
215 mask
&= ~(1ULL << table
->bit
);
223 features
= g_strdup_printf("%s%sUnknown incompatible feature: %" PRIx64
,
224 old
, *old
? ", " : "", mask
);
228 error_setg(errp
, "Unsupported qcow2 feature(s): %s", features
);
233 * Sets the dirty bit and flushes afterwards if necessary.
235 * The incompatible_features bit is only set if the image file header was
236 * updated successfully. Therefore it is not required to check the return
237 * value of this function.
239 int qcow2_mark_dirty(BlockDriverState
*bs
)
241 BDRVQcow2State
*s
= bs
->opaque
;
245 assert(s
->qcow_version
>= 3);
247 if (s
->incompatible_features
& QCOW2_INCOMPAT_DIRTY
) {
248 return 0; /* already dirty */
251 val
= cpu_to_be64(s
->incompatible_features
| QCOW2_INCOMPAT_DIRTY
);
252 ret
= bdrv_pwrite(bs
->file
->bs
, offsetof(QCowHeader
, incompatible_features
),
257 ret
= bdrv_flush(bs
->file
->bs
);
262 /* Only treat image as dirty if the header was updated successfully */
263 s
->incompatible_features
|= QCOW2_INCOMPAT_DIRTY
;
268 * Clears the dirty bit and flushes before if necessary. Only call this
269 * function when there are no pending requests, it does not guard against
270 * concurrent requests dirtying the image.
272 static int qcow2_mark_clean(BlockDriverState
*bs
)
274 BDRVQcow2State
*s
= bs
->opaque
;
276 if (s
->incompatible_features
& QCOW2_INCOMPAT_DIRTY
) {
279 s
->incompatible_features
&= ~QCOW2_INCOMPAT_DIRTY
;
281 ret
= bdrv_flush(bs
);
286 return qcow2_update_header(bs
);
292 * Marks the image as corrupt.
294 int qcow2_mark_corrupt(BlockDriverState
*bs
)
296 BDRVQcow2State
*s
= bs
->opaque
;
298 s
->incompatible_features
|= QCOW2_INCOMPAT_CORRUPT
;
299 return qcow2_update_header(bs
);
303 * Marks the image as consistent, i.e., unsets the corrupt bit, and flushes
304 * before if necessary.
306 int qcow2_mark_consistent(BlockDriverState
*bs
)
308 BDRVQcow2State
*s
= bs
->opaque
;
310 if (s
->incompatible_features
& QCOW2_INCOMPAT_CORRUPT
) {
311 int ret
= bdrv_flush(bs
);
316 s
->incompatible_features
&= ~QCOW2_INCOMPAT_CORRUPT
;
317 return qcow2_update_header(bs
);
322 static int qcow2_check(BlockDriverState
*bs
, BdrvCheckResult
*result
,
325 int ret
= qcow2_check_refcounts(bs
, result
, fix
);
330 if (fix
&& result
->check_errors
== 0 && result
->corruptions
== 0) {
331 ret
= qcow2_mark_clean(bs
);
335 return qcow2_mark_consistent(bs
);
340 static int validate_table_offset(BlockDriverState
*bs
, uint64_t offset
,
341 uint64_t entries
, size_t entry_len
)
343 BDRVQcow2State
*s
= bs
->opaque
;
346 /* Use signed INT64_MAX as the maximum even for uint64_t header fields,
347 * because values will be passed to qemu functions taking int64_t. */
348 if (entries
> INT64_MAX
/ entry_len
) {
352 size
= entries
* entry_len
;
354 if (INT64_MAX
- size
< offset
) {
358 /* Tables must be cluster aligned */
359 if (offset
& (s
->cluster_size
- 1)) {
366 static QemuOptsList qcow2_runtime_opts
= {
368 .head
= QTAILQ_HEAD_INITIALIZER(qcow2_runtime_opts
.head
),
371 .name
= QCOW2_OPT_LAZY_REFCOUNTS
,
372 .type
= QEMU_OPT_BOOL
,
373 .help
= "Postpone refcount updates",
376 .name
= QCOW2_OPT_DISCARD_REQUEST
,
377 .type
= QEMU_OPT_BOOL
,
378 .help
= "Pass guest discard requests to the layer below",
381 .name
= QCOW2_OPT_DISCARD_SNAPSHOT
,
382 .type
= QEMU_OPT_BOOL
,
383 .help
= "Generate discard requests when snapshot related space "
387 .name
= QCOW2_OPT_DISCARD_OTHER
,
388 .type
= QEMU_OPT_BOOL
,
389 .help
= "Generate discard requests when other clusters are freed",
392 .name
= QCOW2_OPT_OVERLAP
,
393 .type
= QEMU_OPT_STRING
,
394 .help
= "Selects which overlap checks to perform from a range of "
395 "templates (none, constant, cached, all)",
398 .name
= QCOW2_OPT_OVERLAP_TEMPLATE
,
399 .type
= QEMU_OPT_STRING
,
400 .help
= "Selects which overlap checks to perform from a range of "
401 "templates (none, constant, cached, all)",
404 .name
= QCOW2_OPT_OVERLAP_MAIN_HEADER
,
405 .type
= QEMU_OPT_BOOL
,
406 .help
= "Check for unintended writes into the main qcow2 header",
409 .name
= QCOW2_OPT_OVERLAP_ACTIVE_L1
,
410 .type
= QEMU_OPT_BOOL
,
411 .help
= "Check for unintended writes into the active L1 table",
414 .name
= QCOW2_OPT_OVERLAP_ACTIVE_L2
,
415 .type
= QEMU_OPT_BOOL
,
416 .help
= "Check for unintended writes into an active L2 table",
419 .name
= QCOW2_OPT_OVERLAP_REFCOUNT_TABLE
,
420 .type
= QEMU_OPT_BOOL
,
421 .help
= "Check for unintended writes into the refcount table",
424 .name
= QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK
,
425 .type
= QEMU_OPT_BOOL
,
426 .help
= "Check for unintended writes into a refcount block",
429 .name
= QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE
,
430 .type
= QEMU_OPT_BOOL
,
431 .help
= "Check for unintended writes into the snapshot table",
434 .name
= QCOW2_OPT_OVERLAP_INACTIVE_L1
,
435 .type
= QEMU_OPT_BOOL
,
436 .help
= "Check for unintended writes into an inactive L1 table",
439 .name
= QCOW2_OPT_OVERLAP_INACTIVE_L2
,
440 .type
= QEMU_OPT_BOOL
,
441 .help
= "Check for unintended writes into an inactive L2 table",
444 .name
= QCOW2_OPT_CACHE_SIZE
,
445 .type
= QEMU_OPT_SIZE
,
446 .help
= "Maximum combined metadata (L2 tables and refcount blocks) "
450 .name
= QCOW2_OPT_L2_CACHE_SIZE
,
451 .type
= QEMU_OPT_SIZE
,
452 .help
= "Maximum L2 table cache size",
455 .name
= QCOW2_OPT_REFCOUNT_CACHE_SIZE
,
456 .type
= QEMU_OPT_SIZE
,
457 .help
= "Maximum refcount block cache size",
460 .name
= QCOW2_OPT_CACHE_CLEAN_INTERVAL
,
461 .type
= QEMU_OPT_NUMBER
,
462 .help
= "Clean unused cache entries after this time (in seconds)",
464 { /* end of list */ }
468 static const char *overlap_bool_option_names
[QCOW2_OL_MAX_BITNR
] = {
469 [QCOW2_OL_MAIN_HEADER_BITNR
] = QCOW2_OPT_OVERLAP_MAIN_HEADER
,
470 [QCOW2_OL_ACTIVE_L1_BITNR
] = QCOW2_OPT_OVERLAP_ACTIVE_L1
,
471 [QCOW2_OL_ACTIVE_L2_BITNR
] = QCOW2_OPT_OVERLAP_ACTIVE_L2
,
472 [QCOW2_OL_REFCOUNT_TABLE_BITNR
] = QCOW2_OPT_OVERLAP_REFCOUNT_TABLE
,
473 [QCOW2_OL_REFCOUNT_BLOCK_BITNR
] = QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK
,
474 [QCOW2_OL_SNAPSHOT_TABLE_BITNR
] = QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE
,
475 [QCOW2_OL_INACTIVE_L1_BITNR
] = QCOW2_OPT_OVERLAP_INACTIVE_L1
,
476 [QCOW2_OL_INACTIVE_L2_BITNR
] = QCOW2_OPT_OVERLAP_INACTIVE_L2
,
479 static void cache_clean_timer_cb(void *opaque
)
481 BlockDriverState
*bs
= opaque
;
482 BDRVQcow2State
*s
= bs
->opaque
;
483 qcow2_cache_clean_unused(bs
, s
->l2_table_cache
);
484 qcow2_cache_clean_unused(bs
, s
->refcount_block_cache
);
485 timer_mod(s
->cache_clean_timer
, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
) +
486 (int64_t) s
->cache_clean_interval
* 1000);
489 static void cache_clean_timer_init(BlockDriverState
*bs
, AioContext
*context
)
491 BDRVQcow2State
*s
= bs
->opaque
;
492 if (s
->cache_clean_interval
> 0) {
493 s
->cache_clean_timer
= aio_timer_new(context
, QEMU_CLOCK_VIRTUAL
,
494 SCALE_MS
, cache_clean_timer_cb
,
496 timer_mod(s
->cache_clean_timer
, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
) +
497 (int64_t) s
->cache_clean_interval
* 1000);
501 static void cache_clean_timer_del(BlockDriverState
*bs
)
503 BDRVQcow2State
*s
= bs
->opaque
;
504 if (s
->cache_clean_timer
) {
505 timer_del(s
->cache_clean_timer
);
506 timer_free(s
->cache_clean_timer
);
507 s
->cache_clean_timer
= NULL
;
511 static void qcow2_detach_aio_context(BlockDriverState
*bs
)
513 cache_clean_timer_del(bs
);
516 static void qcow2_attach_aio_context(BlockDriverState
*bs
,
517 AioContext
*new_context
)
519 cache_clean_timer_init(bs
, new_context
);
522 static void read_cache_sizes(BlockDriverState
*bs
, QemuOpts
*opts
,
523 uint64_t *l2_cache_size
,
524 uint64_t *refcount_cache_size
, Error
**errp
)
526 BDRVQcow2State
*s
= bs
->opaque
;
527 uint64_t combined_cache_size
;
528 bool l2_cache_size_set
, refcount_cache_size_set
, combined_cache_size_set
;
530 combined_cache_size_set
= qemu_opt_get(opts
, QCOW2_OPT_CACHE_SIZE
);
531 l2_cache_size_set
= qemu_opt_get(opts
, QCOW2_OPT_L2_CACHE_SIZE
);
532 refcount_cache_size_set
= qemu_opt_get(opts
, QCOW2_OPT_REFCOUNT_CACHE_SIZE
);
534 combined_cache_size
= qemu_opt_get_size(opts
, QCOW2_OPT_CACHE_SIZE
, 0);
535 *l2_cache_size
= qemu_opt_get_size(opts
, QCOW2_OPT_L2_CACHE_SIZE
, 0);
536 *refcount_cache_size
= qemu_opt_get_size(opts
,
537 QCOW2_OPT_REFCOUNT_CACHE_SIZE
, 0);
539 if (combined_cache_size_set
) {
540 if (l2_cache_size_set
&& refcount_cache_size_set
) {
541 error_setg(errp
, QCOW2_OPT_CACHE_SIZE
", " QCOW2_OPT_L2_CACHE_SIZE
542 " and " QCOW2_OPT_REFCOUNT_CACHE_SIZE
" may not be set "
545 } else if (*l2_cache_size
> combined_cache_size
) {
546 error_setg(errp
, QCOW2_OPT_L2_CACHE_SIZE
" may not exceed "
547 QCOW2_OPT_CACHE_SIZE
);
549 } else if (*refcount_cache_size
> combined_cache_size
) {
550 error_setg(errp
, QCOW2_OPT_REFCOUNT_CACHE_SIZE
" may not exceed "
551 QCOW2_OPT_CACHE_SIZE
);
555 if (l2_cache_size_set
) {
556 *refcount_cache_size
= combined_cache_size
- *l2_cache_size
;
557 } else if (refcount_cache_size_set
) {
558 *l2_cache_size
= combined_cache_size
- *refcount_cache_size
;
560 *refcount_cache_size
= combined_cache_size
561 / (DEFAULT_L2_REFCOUNT_SIZE_RATIO
+ 1);
562 *l2_cache_size
= combined_cache_size
- *refcount_cache_size
;
565 if (!l2_cache_size_set
&& !refcount_cache_size_set
) {
566 *l2_cache_size
= MAX(DEFAULT_L2_CACHE_BYTE_SIZE
,
567 (uint64_t)DEFAULT_L2_CACHE_CLUSTERS
569 *refcount_cache_size
= *l2_cache_size
570 / DEFAULT_L2_REFCOUNT_SIZE_RATIO
;
571 } else if (!l2_cache_size_set
) {
572 *l2_cache_size
= *refcount_cache_size
573 * DEFAULT_L2_REFCOUNT_SIZE_RATIO
;
574 } else if (!refcount_cache_size_set
) {
575 *refcount_cache_size
= *l2_cache_size
576 / DEFAULT_L2_REFCOUNT_SIZE_RATIO
;
581 typedef struct Qcow2ReopenState
{
582 Qcow2Cache
*l2_table_cache
;
583 Qcow2Cache
*refcount_block_cache
;
584 bool use_lazy_refcounts
;
586 bool discard_passthrough
[QCOW2_DISCARD_MAX
];
587 uint64_t cache_clean_interval
;
590 static int qcow2_update_options_prepare(BlockDriverState
*bs
,
592 QDict
*options
, int flags
,
595 BDRVQcow2State
*s
= bs
->opaque
;
596 QemuOpts
*opts
= NULL
;
597 const char *opt_overlap_check
, *opt_overlap_check_template
;
598 int overlap_check_template
= 0;
599 uint64_t l2_cache_size
, refcount_cache_size
;
601 Error
*local_err
= NULL
;
604 opts
= qemu_opts_create(&qcow2_runtime_opts
, NULL
, 0, &error_abort
);
605 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
607 error_propagate(errp
, local_err
);
612 /* get L2 table/refcount block cache size from command line options */
613 read_cache_sizes(bs
, opts
, &l2_cache_size
, &refcount_cache_size
,
616 error_propagate(errp
, local_err
);
621 l2_cache_size
/= s
->cluster_size
;
622 if (l2_cache_size
< MIN_L2_CACHE_SIZE
) {
623 l2_cache_size
= MIN_L2_CACHE_SIZE
;
625 if (l2_cache_size
> INT_MAX
) {
626 error_setg(errp
, "L2 cache size too big");
631 refcount_cache_size
/= s
->cluster_size
;
632 if (refcount_cache_size
< MIN_REFCOUNT_CACHE_SIZE
) {
633 refcount_cache_size
= MIN_REFCOUNT_CACHE_SIZE
;
635 if (refcount_cache_size
> INT_MAX
) {
636 error_setg(errp
, "Refcount cache size too big");
641 /* alloc new L2 table/refcount block cache, flush old one */
642 if (s
->l2_table_cache
) {
643 ret
= qcow2_cache_flush(bs
, s
->l2_table_cache
);
645 error_setg_errno(errp
, -ret
, "Failed to flush the L2 table cache");
650 if (s
->refcount_block_cache
) {
651 ret
= qcow2_cache_flush(bs
, s
->refcount_block_cache
);
653 error_setg_errno(errp
, -ret
,
654 "Failed to flush the refcount block cache");
659 r
->l2_table_cache
= qcow2_cache_create(bs
, l2_cache_size
);
660 r
->refcount_block_cache
= qcow2_cache_create(bs
, refcount_cache_size
);
661 if (r
->l2_table_cache
== NULL
|| r
->refcount_block_cache
== NULL
) {
662 error_setg(errp
, "Could not allocate metadata caches");
667 /* New interval for cache cleanup timer */
668 r
->cache_clean_interval
=
669 qemu_opt_get_number(opts
, QCOW2_OPT_CACHE_CLEAN_INTERVAL
,
670 s
->cache_clean_interval
);
671 if (r
->cache_clean_interval
> UINT_MAX
) {
672 error_setg(errp
, "Cache clean interval too big");
677 /* lazy-refcounts; flush if going from enabled to disabled */
678 r
->use_lazy_refcounts
= qemu_opt_get_bool(opts
, QCOW2_OPT_LAZY_REFCOUNTS
,
679 (s
->compatible_features
& QCOW2_COMPAT_LAZY_REFCOUNTS
));
680 if (r
->use_lazy_refcounts
&& s
->qcow_version
< 3) {
681 error_setg(errp
, "Lazy refcounts require a qcow2 image with at least "
682 "qemu 1.1 compatibility level");
687 if (s
->use_lazy_refcounts
&& !r
->use_lazy_refcounts
) {
688 ret
= qcow2_mark_clean(bs
);
690 error_setg_errno(errp
, -ret
, "Failed to disable lazy refcounts");
695 /* Overlap check options */
696 opt_overlap_check
= qemu_opt_get(opts
, QCOW2_OPT_OVERLAP
);
697 opt_overlap_check_template
= qemu_opt_get(opts
, QCOW2_OPT_OVERLAP_TEMPLATE
);
698 if (opt_overlap_check_template
&& opt_overlap_check
&&
699 strcmp(opt_overlap_check_template
, opt_overlap_check
))
701 error_setg(errp
, "Conflicting values for qcow2 options '"
702 QCOW2_OPT_OVERLAP
"' ('%s') and '" QCOW2_OPT_OVERLAP_TEMPLATE
703 "' ('%s')", opt_overlap_check
, opt_overlap_check_template
);
707 if (!opt_overlap_check
) {
708 opt_overlap_check
= opt_overlap_check_template
?: "cached";
711 if (!strcmp(opt_overlap_check
, "none")) {
712 overlap_check_template
= 0;
713 } else if (!strcmp(opt_overlap_check
, "constant")) {
714 overlap_check_template
= QCOW2_OL_CONSTANT
;
715 } else if (!strcmp(opt_overlap_check
, "cached")) {
716 overlap_check_template
= QCOW2_OL_CACHED
;
717 } else if (!strcmp(opt_overlap_check
, "all")) {
718 overlap_check_template
= QCOW2_OL_ALL
;
720 error_setg(errp
, "Unsupported value '%s' for qcow2 option "
721 "'overlap-check'. Allowed are any of the following: "
722 "none, constant, cached, all", opt_overlap_check
);
727 r
->overlap_check
= 0;
728 for (i
= 0; i
< QCOW2_OL_MAX_BITNR
; i
++) {
729 /* overlap-check defines a template bitmask, but every flag may be
730 * overwritten through the associated boolean option */
732 qemu_opt_get_bool(opts
, overlap_bool_option_names
[i
],
733 overlap_check_template
& (1 << i
)) << i
;
736 r
->discard_passthrough
[QCOW2_DISCARD_NEVER
] = false;
737 r
->discard_passthrough
[QCOW2_DISCARD_ALWAYS
] = true;
738 r
->discard_passthrough
[QCOW2_DISCARD_REQUEST
] =
739 qemu_opt_get_bool(opts
, QCOW2_OPT_DISCARD_REQUEST
,
740 flags
& BDRV_O_UNMAP
);
741 r
->discard_passthrough
[QCOW2_DISCARD_SNAPSHOT
] =
742 qemu_opt_get_bool(opts
, QCOW2_OPT_DISCARD_SNAPSHOT
, true);
743 r
->discard_passthrough
[QCOW2_DISCARD_OTHER
] =
744 qemu_opt_get_bool(opts
, QCOW2_OPT_DISCARD_OTHER
, false);
753 static void qcow2_update_options_commit(BlockDriverState
*bs
,
756 BDRVQcow2State
*s
= bs
->opaque
;
759 if (s
->l2_table_cache
) {
760 qcow2_cache_destroy(bs
, s
->l2_table_cache
);
762 if (s
->refcount_block_cache
) {
763 qcow2_cache_destroy(bs
, s
->refcount_block_cache
);
765 s
->l2_table_cache
= r
->l2_table_cache
;
766 s
->refcount_block_cache
= r
->refcount_block_cache
;
768 s
->overlap_check
= r
->overlap_check
;
769 s
->use_lazy_refcounts
= r
->use_lazy_refcounts
;
771 for (i
= 0; i
< QCOW2_DISCARD_MAX
; i
++) {
772 s
->discard_passthrough
[i
] = r
->discard_passthrough
[i
];
775 if (s
->cache_clean_interval
!= r
->cache_clean_interval
) {
776 cache_clean_timer_del(bs
);
777 s
->cache_clean_interval
= r
->cache_clean_interval
;
778 cache_clean_timer_init(bs
, bdrv_get_aio_context(bs
));
782 static void qcow2_update_options_abort(BlockDriverState
*bs
,
785 if (r
->l2_table_cache
) {
786 qcow2_cache_destroy(bs
, r
->l2_table_cache
);
788 if (r
->refcount_block_cache
) {
789 qcow2_cache_destroy(bs
, r
->refcount_block_cache
);
793 static int qcow2_update_options(BlockDriverState
*bs
, QDict
*options
,
794 int flags
, Error
**errp
)
796 Qcow2ReopenState r
= {};
799 ret
= qcow2_update_options_prepare(bs
, &r
, options
, flags
, errp
);
801 qcow2_update_options_commit(bs
, &r
);
803 qcow2_update_options_abort(bs
, &r
);
809 static int qcow2_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
812 BDRVQcow2State
*s
= bs
->opaque
;
816 Error
*local_err
= NULL
;
818 uint64_t l1_vm_state_index
;
820 ret
= bdrv_pread(bs
->file
->bs
, 0, &header
, sizeof(header
));
822 error_setg_errno(errp
, -ret
, "Could not read qcow2 header");
825 be32_to_cpus(&header
.magic
);
826 be32_to_cpus(&header
.version
);
827 be64_to_cpus(&header
.backing_file_offset
);
828 be32_to_cpus(&header
.backing_file_size
);
829 be64_to_cpus(&header
.size
);
830 be32_to_cpus(&header
.cluster_bits
);
831 be32_to_cpus(&header
.crypt_method
);
832 be64_to_cpus(&header
.l1_table_offset
);
833 be32_to_cpus(&header
.l1_size
);
834 be64_to_cpus(&header
.refcount_table_offset
);
835 be32_to_cpus(&header
.refcount_table_clusters
);
836 be64_to_cpus(&header
.snapshots_offset
);
837 be32_to_cpus(&header
.nb_snapshots
);
839 if (header
.magic
!= QCOW_MAGIC
) {
840 error_setg(errp
, "Image is not in qcow2 format");
844 if (header
.version
< 2 || header
.version
> 3) {
845 error_setg(errp
, "Unsupported qcow2 version %" PRIu32
, header
.version
);
850 s
->qcow_version
= header
.version
;
852 /* Initialise cluster size */
853 if (header
.cluster_bits
< MIN_CLUSTER_BITS
||
854 header
.cluster_bits
> MAX_CLUSTER_BITS
) {
855 error_setg(errp
, "Unsupported cluster size: 2^%" PRIu32
,
856 header
.cluster_bits
);
861 s
->cluster_bits
= header
.cluster_bits
;
862 s
->cluster_size
= 1 << s
->cluster_bits
;
863 s
->cluster_sectors
= 1 << (s
->cluster_bits
- 9);
865 /* Initialise version 3 header fields */
866 if (header
.version
== 2) {
867 header
.incompatible_features
= 0;
868 header
.compatible_features
= 0;
869 header
.autoclear_features
= 0;
870 header
.refcount_order
= 4;
871 header
.header_length
= 72;
873 be64_to_cpus(&header
.incompatible_features
);
874 be64_to_cpus(&header
.compatible_features
);
875 be64_to_cpus(&header
.autoclear_features
);
876 be32_to_cpus(&header
.refcount_order
);
877 be32_to_cpus(&header
.header_length
);
879 if (header
.header_length
< 104) {
880 error_setg(errp
, "qcow2 header too short");
886 if (header
.header_length
> s
->cluster_size
) {
887 error_setg(errp
, "qcow2 header exceeds cluster size");
892 if (header
.header_length
> sizeof(header
)) {
893 s
->unknown_header_fields_size
= header
.header_length
- sizeof(header
);
894 s
->unknown_header_fields
= g_malloc(s
->unknown_header_fields_size
);
895 ret
= bdrv_pread(bs
->file
->bs
, sizeof(header
), s
->unknown_header_fields
,
896 s
->unknown_header_fields_size
);
898 error_setg_errno(errp
, -ret
, "Could not read unknown qcow2 header "
904 if (header
.backing_file_offset
> s
->cluster_size
) {
905 error_setg(errp
, "Invalid backing file offset");
910 if (header
.backing_file_offset
) {
911 ext_end
= header
.backing_file_offset
;
913 ext_end
= 1 << header
.cluster_bits
;
916 /* Handle feature bits */
917 s
->incompatible_features
= header
.incompatible_features
;
918 s
->compatible_features
= header
.compatible_features
;
919 s
->autoclear_features
= header
.autoclear_features
;
921 if (s
->incompatible_features
& ~QCOW2_INCOMPAT_MASK
) {
922 void *feature_table
= NULL
;
923 qcow2_read_extensions(bs
, header
.header_length
, ext_end
,
924 &feature_table
, NULL
);
925 report_unsupported_feature(errp
, feature_table
,
926 s
->incompatible_features
&
927 ~QCOW2_INCOMPAT_MASK
);
929 g_free(feature_table
);
933 if (s
->incompatible_features
& QCOW2_INCOMPAT_CORRUPT
) {
934 /* Corrupt images may not be written to unless they are being repaired
936 if ((flags
& BDRV_O_RDWR
) && !(flags
& BDRV_O_CHECK
)) {
937 error_setg(errp
, "qcow2: Image is corrupt; cannot be opened "
944 /* Check support for various header values */
945 if (header
.refcount_order
> 6) {
946 error_setg(errp
, "Reference count entry width too large; may not "
951 s
->refcount_order
= header
.refcount_order
;
952 s
->refcount_bits
= 1 << s
->refcount_order
;
953 s
->refcount_max
= UINT64_C(1) << (s
->refcount_bits
- 1);
954 s
->refcount_max
+= s
->refcount_max
- 1;
956 if (header
.crypt_method
> QCOW_CRYPT_AES
) {
957 error_setg(errp
, "Unsupported encryption method: %" PRIu32
,
958 header
.crypt_method
);
962 if (!qcrypto_cipher_supports(QCRYPTO_CIPHER_ALG_AES_128
)) {
963 error_setg(errp
, "AES cipher not available");
967 s
->crypt_method_header
= header
.crypt_method
;
968 if (s
->crypt_method_header
) {
969 if (bdrv_uses_whitelist() &&
970 s
->crypt_method_header
== QCOW_CRYPT_AES
) {
971 error_report("qcow2 built-in AES encryption is deprecated");
972 error_printf("Support for it will be removed in a future release.\n"
973 "You can use 'qemu-img convert' to switch to an\n"
974 "unencrypted qcow2 image, or a LUKS raw image.\n");
980 s
->l2_bits
= s
->cluster_bits
- 3; /* L2 is always one cluster */
981 s
->l2_size
= 1 << s
->l2_bits
;
982 /* 2^(s->refcount_order - 3) is the refcount width in bytes */
983 s
->refcount_block_bits
= s
->cluster_bits
- (s
->refcount_order
- 3);
984 s
->refcount_block_size
= 1 << s
->refcount_block_bits
;
985 bs
->total_sectors
= header
.size
/ 512;
986 s
->csize_shift
= (62 - (s
->cluster_bits
- 8));
987 s
->csize_mask
= (1 << (s
->cluster_bits
- 8)) - 1;
988 s
->cluster_offset_mask
= (1LL << s
->csize_shift
) - 1;
990 s
->refcount_table_offset
= header
.refcount_table_offset
;
991 s
->refcount_table_size
=
992 header
.refcount_table_clusters
<< (s
->cluster_bits
- 3);
994 if (header
.refcount_table_clusters
> qcow2_max_refcount_clusters(s
)) {
995 error_setg(errp
, "Reference count table too large");
1000 ret
= validate_table_offset(bs
, s
->refcount_table_offset
,
1001 s
->refcount_table_size
, sizeof(uint64_t));
1003 error_setg(errp
, "Invalid reference count table offset");
1007 /* Snapshot table offset/length */
1008 if (header
.nb_snapshots
> QCOW_MAX_SNAPSHOTS
) {
1009 error_setg(errp
, "Too many snapshots");
1014 ret
= validate_table_offset(bs
, header
.snapshots_offset
,
1015 header
.nb_snapshots
,
1016 sizeof(QCowSnapshotHeader
));
1018 error_setg(errp
, "Invalid snapshot table offset");
1022 /* read the level 1 table */
1023 if (header
.l1_size
> QCOW_MAX_L1_SIZE
/ sizeof(uint64_t)) {
1024 error_setg(errp
, "Active L1 table too large");
1028 s
->l1_size
= header
.l1_size
;
1030 l1_vm_state_index
= size_to_l1(s
, header
.size
);
1031 if (l1_vm_state_index
> INT_MAX
) {
1032 error_setg(errp
, "Image is too big");
1036 s
->l1_vm_state_index
= l1_vm_state_index
;
1038 /* the L1 table must contain at least enough entries to put
1039 header.size bytes */
1040 if (s
->l1_size
< s
->l1_vm_state_index
) {
1041 error_setg(errp
, "L1 table is too small");
1046 ret
= validate_table_offset(bs
, header
.l1_table_offset
,
1047 header
.l1_size
, sizeof(uint64_t));
1049 error_setg(errp
, "Invalid L1 table offset");
1052 s
->l1_table_offset
= header
.l1_table_offset
;
1055 if (s
->l1_size
> 0) {
1056 s
->l1_table
= qemu_try_blockalign(bs
->file
->bs
,
1057 align_offset(s
->l1_size
* sizeof(uint64_t), 512));
1058 if (s
->l1_table
== NULL
) {
1059 error_setg(errp
, "Could not allocate L1 table");
1063 ret
= bdrv_pread(bs
->file
->bs
, s
->l1_table_offset
, s
->l1_table
,
1064 s
->l1_size
* sizeof(uint64_t));
1066 error_setg_errno(errp
, -ret
, "Could not read L1 table");
1069 for(i
= 0;i
< s
->l1_size
; i
++) {
1070 be64_to_cpus(&s
->l1_table
[i
]);
1074 /* Parse driver-specific options */
1075 ret
= qcow2_update_options(bs
, options
, flags
, errp
);
1080 s
->cluster_cache
= g_malloc(s
->cluster_size
);
1081 /* one more sector for decompressed data alignment */
1082 s
->cluster_data
= qemu_try_blockalign(bs
->file
->bs
, QCOW_MAX_CRYPT_CLUSTERS
1083 * s
->cluster_size
+ 512);
1084 if (s
->cluster_data
== NULL
) {
1085 error_setg(errp
, "Could not allocate temporary cluster buffer");
1090 s
->cluster_cache_offset
= -1;
1093 ret
= qcow2_refcount_init(bs
);
1095 error_setg_errno(errp
, -ret
, "Could not initialize refcount handling");
1099 QLIST_INIT(&s
->cluster_allocs
);
1100 QTAILQ_INIT(&s
->discards
);
1102 /* read qcow2 extensions */
1103 if (qcow2_read_extensions(bs
, header
.header_length
, ext_end
, NULL
,
1105 error_propagate(errp
, local_err
);
1110 /* read the backing file name */
1111 if (header
.backing_file_offset
!= 0) {
1112 len
= header
.backing_file_size
;
1113 if (len
> MIN(1023, s
->cluster_size
- header
.backing_file_offset
) ||
1114 len
>= sizeof(bs
->backing_file
)) {
1115 error_setg(errp
, "Backing file name too long");
1119 ret
= bdrv_pread(bs
->file
->bs
, header
.backing_file_offset
,
1120 bs
->backing_file
, len
);
1122 error_setg_errno(errp
, -ret
, "Could not read backing file name");
1125 bs
->backing_file
[len
] = '\0';
1126 s
->image_backing_file
= g_strdup(bs
->backing_file
);
1129 /* Internal snapshots */
1130 s
->snapshots_offset
= header
.snapshots_offset
;
1131 s
->nb_snapshots
= header
.nb_snapshots
;
1133 ret
= qcow2_read_snapshots(bs
);
1135 error_setg_errno(errp
, -ret
, "Could not read snapshots");
1139 /* Clear unknown autoclear feature bits */
1140 if (!bs
->read_only
&& !(flags
& BDRV_O_INACTIVE
) && s
->autoclear_features
) {
1141 s
->autoclear_features
= 0;
1142 ret
= qcow2_update_header(bs
);
1144 error_setg_errno(errp
, -ret
, "Could not update qcow2 header");
1149 /* Initialise locks */
1150 qemu_co_mutex_init(&s
->lock
);
1152 /* Repair image if dirty */
1153 if (!(flags
& (BDRV_O_CHECK
| BDRV_O_INACTIVE
)) && !bs
->read_only
&&
1154 (s
->incompatible_features
& QCOW2_INCOMPAT_DIRTY
)) {
1155 BdrvCheckResult result
= {0};
1157 ret
= qcow2_check(bs
, &result
, BDRV_FIX_ERRORS
| BDRV_FIX_LEAKS
);
1159 error_setg_errno(errp
, -ret
, "Could not repair dirty image");
1166 BdrvCheckResult result
= {0};
1167 qcow2_check_refcounts(bs
, &result
, 0);
1173 g_free(s
->unknown_header_fields
);
1174 cleanup_unknown_header_ext(bs
);
1175 qcow2_free_snapshots(bs
);
1176 qcow2_refcount_close(bs
);
1177 qemu_vfree(s
->l1_table
);
1178 /* else pre-write overlap checks in cache_destroy may crash */
1180 cache_clean_timer_del(bs
);
1181 if (s
->l2_table_cache
) {
1182 qcow2_cache_destroy(bs
, s
->l2_table_cache
);
1184 if (s
->refcount_block_cache
) {
1185 qcow2_cache_destroy(bs
, s
->refcount_block_cache
);
1187 g_free(s
->cluster_cache
);
1188 qemu_vfree(s
->cluster_data
);
1192 static void qcow2_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
1194 BDRVQcow2State
*s
= bs
->opaque
;
1196 bs
->bl
.write_zeroes_alignment
= s
->cluster_sectors
;
1199 static int qcow2_set_key(BlockDriverState
*bs
, const char *key
)
1201 BDRVQcow2State
*s
= bs
->opaque
;
1206 memset(keybuf
, 0, 16);
1210 /* XXX: we could compress the chars to 7 bits to increase
1212 for(i
= 0;i
< len
;i
++) {
1215 assert(bs
->encrypted
);
1217 qcrypto_cipher_free(s
->cipher
);
1218 s
->cipher
= qcrypto_cipher_new(
1219 QCRYPTO_CIPHER_ALG_AES_128
,
1220 QCRYPTO_CIPHER_MODE_CBC
,
1221 keybuf
, G_N_ELEMENTS(keybuf
),
1225 /* XXX would be nice if errors in this method could
1226 * be properly propagate to the caller. Would need
1227 * the bdrv_set_key() API signature to be fixed. */
1234 static int qcow2_reopen_prepare(BDRVReopenState
*state
,
1235 BlockReopenQueue
*queue
, Error
**errp
)
1237 Qcow2ReopenState
*r
;
1240 r
= g_new0(Qcow2ReopenState
, 1);
1243 ret
= qcow2_update_options_prepare(state
->bs
, r
, state
->options
,
1244 state
->flags
, errp
);
1249 /* We need to write out any unwritten data if we reopen read-only. */
1250 if ((state
->flags
& BDRV_O_RDWR
) == 0) {
1251 ret
= bdrv_flush(state
->bs
);
1256 ret
= qcow2_mark_clean(state
->bs
);
1265 qcow2_update_options_abort(state
->bs
, r
);
1270 static void qcow2_reopen_commit(BDRVReopenState
*state
)
1272 qcow2_update_options_commit(state
->bs
, state
->opaque
);
1273 g_free(state
->opaque
);
1276 static void qcow2_reopen_abort(BDRVReopenState
*state
)
1278 qcow2_update_options_abort(state
->bs
, state
->opaque
);
1279 g_free(state
->opaque
);
1282 static void qcow2_join_options(QDict
*options
, QDict
*old_options
)
1284 bool has_new_overlap_template
=
1285 qdict_haskey(options
, QCOW2_OPT_OVERLAP
) ||
1286 qdict_haskey(options
, QCOW2_OPT_OVERLAP_TEMPLATE
);
1287 bool has_new_total_cache_size
=
1288 qdict_haskey(options
, QCOW2_OPT_CACHE_SIZE
);
1289 bool has_all_cache_options
;
1291 /* New overlap template overrides all old overlap options */
1292 if (has_new_overlap_template
) {
1293 qdict_del(old_options
, QCOW2_OPT_OVERLAP
);
1294 qdict_del(old_options
, QCOW2_OPT_OVERLAP_TEMPLATE
);
1295 qdict_del(old_options
, QCOW2_OPT_OVERLAP_MAIN_HEADER
);
1296 qdict_del(old_options
, QCOW2_OPT_OVERLAP_ACTIVE_L1
);
1297 qdict_del(old_options
, QCOW2_OPT_OVERLAP_ACTIVE_L2
);
1298 qdict_del(old_options
, QCOW2_OPT_OVERLAP_REFCOUNT_TABLE
);
1299 qdict_del(old_options
, QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK
);
1300 qdict_del(old_options
, QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE
);
1301 qdict_del(old_options
, QCOW2_OPT_OVERLAP_INACTIVE_L1
);
1302 qdict_del(old_options
, QCOW2_OPT_OVERLAP_INACTIVE_L2
);
1305 /* New total cache size overrides all old options */
1306 if (qdict_haskey(options
, QCOW2_OPT_CACHE_SIZE
)) {
1307 qdict_del(old_options
, QCOW2_OPT_L2_CACHE_SIZE
);
1308 qdict_del(old_options
, QCOW2_OPT_REFCOUNT_CACHE_SIZE
);
1311 qdict_join(options
, old_options
, false);
1314 * If after merging all cache size options are set, an old total size is
1315 * overwritten. Do keep all options, however, if all three are new. The
1316 * resulting error message is what we want to happen.
1318 has_all_cache_options
=
1319 qdict_haskey(options
, QCOW2_OPT_CACHE_SIZE
) ||
1320 qdict_haskey(options
, QCOW2_OPT_L2_CACHE_SIZE
) ||
1321 qdict_haskey(options
, QCOW2_OPT_REFCOUNT_CACHE_SIZE
);
1323 if (has_all_cache_options
&& !has_new_total_cache_size
) {
1324 qdict_del(options
, QCOW2_OPT_CACHE_SIZE
);
1328 static int64_t coroutine_fn
qcow2_co_get_block_status(BlockDriverState
*bs
,
1329 int64_t sector_num
, int nb_sectors
, int *pnum
, BlockDriverState
**file
)
1331 BDRVQcow2State
*s
= bs
->opaque
;
1332 uint64_t cluster_offset
;
1333 int index_in_cluster
, ret
;
1337 qemu_co_mutex_lock(&s
->lock
);
1338 ret
= qcow2_get_cluster_offset(bs
, sector_num
<< 9, pnum
, &cluster_offset
);
1339 qemu_co_mutex_unlock(&s
->lock
);
1344 if (cluster_offset
!= 0 && ret
!= QCOW2_CLUSTER_COMPRESSED
&&
1346 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
1347 cluster_offset
|= (index_in_cluster
<< BDRV_SECTOR_BITS
);
1348 *file
= bs
->file
->bs
;
1349 status
|= BDRV_BLOCK_OFFSET_VALID
| cluster_offset
;
1351 if (ret
== QCOW2_CLUSTER_ZERO
) {
1352 status
|= BDRV_BLOCK_ZERO
;
1353 } else if (ret
!= QCOW2_CLUSTER_UNALLOCATED
) {
1354 status
|= BDRV_BLOCK_DATA
;
1359 /* handle reading after the end of the backing file */
1360 int qcow2_backing_read1(BlockDriverState
*bs
, QEMUIOVector
*qiov
,
1361 int64_t sector_num
, int nb_sectors
)
1364 if ((sector_num
+ nb_sectors
) <= bs
->total_sectors
)
1366 if (sector_num
>= bs
->total_sectors
)
1369 n1
= bs
->total_sectors
- sector_num
;
1371 qemu_iovec_memset(qiov
, 512 * n1
, 0, 512 * (nb_sectors
- n1
));
1376 static coroutine_fn
int qcow2_co_readv(BlockDriverState
*bs
, int64_t sector_num
,
1377 int remaining_sectors
, QEMUIOVector
*qiov
)
1379 BDRVQcow2State
*s
= bs
->opaque
;
1380 int index_in_cluster
, n1
;
1382 int cur_nr_sectors
; /* number of sectors in current iteration */
1383 uint64_t cluster_offset
= 0;
1384 uint64_t bytes_done
= 0;
1385 QEMUIOVector hd_qiov
;
1386 uint8_t *cluster_data
= NULL
;
1388 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
1390 qemu_co_mutex_lock(&s
->lock
);
1392 while (remaining_sectors
!= 0) {
1394 /* prepare next request */
1395 cur_nr_sectors
= remaining_sectors
;
1397 cur_nr_sectors
= MIN(cur_nr_sectors
,
1398 QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
);
1401 ret
= qcow2_get_cluster_offset(bs
, sector_num
<< 9,
1402 &cur_nr_sectors
, &cluster_offset
);
1407 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
1409 qemu_iovec_reset(&hd_qiov
);
1410 qemu_iovec_concat(&hd_qiov
, qiov
, bytes_done
,
1411 cur_nr_sectors
* 512);
1414 case QCOW2_CLUSTER_UNALLOCATED
:
1417 /* read from the base image */
1418 n1
= qcow2_backing_read1(bs
->backing
->bs
, &hd_qiov
,
1419 sector_num
, cur_nr_sectors
);
1421 QEMUIOVector local_qiov
;
1423 qemu_iovec_init(&local_qiov
, hd_qiov
.niov
);
1424 qemu_iovec_concat(&local_qiov
, &hd_qiov
, 0,
1425 n1
* BDRV_SECTOR_SIZE
);
1427 BLKDBG_EVENT(bs
->file
, BLKDBG_READ_BACKING_AIO
);
1428 qemu_co_mutex_unlock(&s
->lock
);
1429 ret
= bdrv_co_readv(bs
->backing
->bs
, sector_num
,
1431 qemu_co_mutex_lock(&s
->lock
);
1433 qemu_iovec_destroy(&local_qiov
);
1440 /* Note: in this case, no need to wait */
1441 qemu_iovec_memset(&hd_qiov
, 0, 0, 512 * cur_nr_sectors
);
1445 case QCOW2_CLUSTER_ZERO
:
1446 qemu_iovec_memset(&hd_qiov
, 0, 0, 512 * cur_nr_sectors
);
1449 case QCOW2_CLUSTER_COMPRESSED
:
1450 /* add AIO support for compressed blocks ? */
1451 ret
= qcow2_decompress_cluster(bs
, cluster_offset
);
1456 qemu_iovec_from_buf(&hd_qiov
, 0,
1457 s
->cluster_cache
+ index_in_cluster
* 512,
1458 512 * cur_nr_sectors
);
1461 case QCOW2_CLUSTER_NORMAL
:
1462 if ((cluster_offset
& 511) != 0) {
1467 if (bs
->encrypted
) {
1471 * For encrypted images, read everything into a temporary
1472 * contiguous buffer on which the AES functions can work.
1474 if (!cluster_data
) {
1476 qemu_try_blockalign(bs
->file
->bs
,
1477 QCOW_MAX_CRYPT_CLUSTERS
1479 if (cluster_data
== NULL
) {
1485 assert(cur_nr_sectors
<=
1486 QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
);
1487 qemu_iovec_reset(&hd_qiov
);
1488 qemu_iovec_add(&hd_qiov
, cluster_data
,
1489 512 * cur_nr_sectors
);
1492 BLKDBG_EVENT(bs
->file
, BLKDBG_READ_AIO
);
1493 qemu_co_mutex_unlock(&s
->lock
);
1494 ret
= bdrv_co_readv(bs
->file
->bs
,
1495 (cluster_offset
>> 9) + index_in_cluster
,
1496 cur_nr_sectors
, &hd_qiov
);
1497 qemu_co_mutex_lock(&s
->lock
);
1501 if (bs
->encrypted
) {
1504 if (qcow2_encrypt_sectors(s
, sector_num
, cluster_data
,
1505 cluster_data
, cur_nr_sectors
, false,
1511 qemu_iovec_from_buf(qiov
, bytes_done
,
1512 cluster_data
, 512 * cur_nr_sectors
);
1517 g_assert_not_reached();
1522 remaining_sectors
-= cur_nr_sectors
;
1523 sector_num
+= cur_nr_sectors
;
1524 bytes_done
+= cur_nr_sectors
* 512;
1529 qemu_co_mutex_unlock(&s
->lock
);
1531 qemu_iovec_destroy(&hd_qiov
);
1532 qemu_vfree(cluster_data
);
1537 static coroutine_fn
int qcow2_co_writev(BlockDriverState
*bs
,
1539 int remaining_sectors
,
1542 BDRVQcow2State
*s
= bs
->opaque
;
1543 int index_in_cluster
;
1545 int cur_nr_sectors
; /* number of sectors in current iteration */
1546 uint64_t cluster_offset
;
1547 QEMUIOVector hd_qiov
;
1548 uint64_t bytes_done
= 0;
1549 uint8_t *cluster_data
= NULL
;
1550 QCowL2Meta
*l2meta
= NULL
;
1552 trace_qcow2_writev_start_req(qemu_coroutine_self(), sector_num
,
1555 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
1557 s
->cluster_cache_offset
= -1; /* disable compressed cache */
1559 qemu_co_mutex_lock(&s
->lock
);
1561 while (remaining_sectors
!= 0) {
1565 trace_qcow2_writev_start_part(qemu_coroutine_self());
1566 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
1567 cur_nr_sectors
= remaining_sectors
;
1568 if (bs
->encrypted
&&
1570 QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
- index_in_cluster
) {
1572 QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
- index_in_cluster
;
1575 ret
= qcow2_alloc_cluster_offset(bs
, sector_num
<< 9,
1576 &cur_nr_sectors
, &cluster_offset
, &l2meta
);
1581 assert((cluster_offset
& 511) == 0);
1583 qemu_iovec_reset(&hd_qiov
);
1584 qemu_iovec_concat(&hd_qiov
, qiov
, bytes_done
,
1585 cur_nr_sectors
* 512);
1587 if (bs
->encrypted
) {
1590 if (!cluster_data
) {
1591 cluster_data
= qemu_try_blockalign(bs
->file
->bs
,
1592 QCOW_MAX_CRYPT_CLUSTERS
1594 if (cluster_data
== NULL
) {
1600 assert(hd_qiov
.size
<=
1601 QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_size
);
1602 qemu_iovec_to_buf(&hd_qiov
, 0, cluster_data
, hd_qiov
.size
);
1604 if (qcow2_encrypt_sectors(s
, sector_num
, cluster_data
,
1605 cluster_data
, cur_nr_sectors
,
1612 qemu_iovec_reset(&hd_qiov
);
1613 qemu_iovec_add(&hd_qiov
, cluster_data
,
1614 cur_nr_sectors
* 512);
1617 ret
= qcow2_pre_write_overlap_check(bs
, 0,
1618 cluster_offset
+ index_in_cluster
* BDRV_SECTOR_SIZE
,
1619 cur_nr_sectors
* BDRV_SECTOR_SIZE
);
1624 qemu_co_mutex_unlock(&s
->lock
);
1625 BLKDBG_EVENT(bs
->file
, BLKDBG_WRITE_AIO
);
1626 trace_qcow2_writev_data(qemu_coroutine_self(),
1627 (cluster_offset
>> 9) + index_in_cluster
);
1628 ret
= bdrv_co_writev(bs
->file
->bs
,
1629 (cluster_offset
>> 9) + index_in_cluster
,
1630 cur_nr_sectors
, &hd_qiov
);
1631 qemu_co_mutex_lock(&s
->lock
);
1636 while (l2meta
!= NULL
) {
1639 ret
= qcow2_alloc_cluster_link_l2(bs
, l2meta
);
1644 /* Take the request off the list of running requests */
1645 if (l2meta
->nb_clusters
!= 0) {
1646 QLIST_REMOVE(l2meta
, next_in_flight
);
1649 qemu_co_queue_restart_all(&l2meta
->dependent_requests
);
1651 next
= l2meta
->next
;
1656 remaining_sectors
-= cur_nr_sectors
;
1657 sector_num
+= cur_nr_sectors
;
1658 bytes_done
+= cur_nr_sectors
* 512;
1659 trace_qcow2_writev_done_part(qemu_coroutine_self(), cur_nr_sectors
);
1664 qemu_co_mutex_unlock(&s
->lock
);
1666 while (l2meta
!= NULL
) {
1669 if (l2meta
->nb_clusters
!= 0) {
1670 QLIST_REMOVE(l2meta
, next_in_flight
);
1672 qemu_co_queue_restart_all(&l2meta
->dependent_requests
);
1674 next
= l2meta
->next
;
1679 qemu_iovec_destroy(&hd_qiov
);
1680 qemu_vfree(cluster_data
);
1681 trace_qcow2_writev_done_req(qemu_coroutine_self(), ret
);
1686 static int qcow2_inactivate(BlockDriverState
*bs
)
1688 BDRVQcow2State
*s
= bs
->opaque
;
1689 int ret
, result
= 0;
1691 ret
= qcow2_cache_flush(bs
, s
->l2_table_cache
);
1694 error_report("Failed to flush the L2 table cache: %s",
1698 ret
= qcow2_cache_flush(bs
, s
->refcount_block_cache
);
1701 error_report("Failed to flush the refcount block cache: %s",
1706 qcow2_mark_clean(bs
);
1712 static void qcow2_close(BlockDriverState
*bs
)
1714 BDRVQcow2State
*s
= bs
->opaque
;
1715 qemu_vfree(s
->l1_table
);
1716 /* else pre-write overlap checks in cache_destroy may crash */
1719 if (!(s
->flags
& BDRV_O_INACTIVE
)) {
1720 qcow2_inactivate(bs
);
1723 cache_clean_timer_del(bs
);
1724 qcow2_cache_destroy(bs
, s
->l2_table_cache
);
1725 qcow2_cache_destroy(bs
, s
->refcount_block_cache
);
1727 qcrypto_cipher_free(s
->cipher
);
1730 g_free(s
->unknown_header_fields
);
1731 cleanup_unknown_header_ext(bs
);
1733 g_free(s
->image_backing_file
);
1734 g_free(s
->image_backing_format
);
1736 g_free(s
->cluster_cache
);
1737 qemu_vfree(s
->cluster_data
);
1738 qcow2_refcount_close(bs
);
1739 qcow2_free_snapshots(bs
);
1742 static void qcow2_invalidate_cache(BlockDriverState
*bs
, Error
**errp
)
1744 BDRVQcow2State
*s
= bs
->opaque
;
1745 int flags
= s
->flags
;
1746 QCryptoCipher
*cipher
= NULL
;
1748 Error
*local_err
= NULL
;
1752 * Backing files are read-only which makes all of their metadata immutable,
1753 * that means we don't have to worry about reopening them here.
1761 memset(s
, 0, sizeof(BDRVQcow2State
));
1762 options
= qdict_clone_shallow(bs
->options
);
1764 flags
&= ~BDRV_O_INACTIVE
;
1765 ret
= qcow2_open(bs
, options
, flags
, &local_err
);
1768 error_propagate(errp
, local_err
);
1769 error_prepend(errp
, "Could not reopen qcow2 layer: ");
1772 } else if (ret
< 0) {
1773 error_setg_errno(errp
, -ret
, "Could not reopen qcow2 layer");
1781 static size_t header_ext_add(char *buf
, uint32_t magic
, const void *s
,
1782 size_t len
, size_t buflen
)
1784 QCowExtension
*ext_backing_fmt
= (QCowExtension
*) buf
;
1785 size_t ext_len
= sizeof(QCowExtension
) + ((len
+ 7) & ~7);
1787 if (buflen
< ext_len
) {
1791 *ext_backing_fmt
= (QCowExtension
) {
1792 .magic
= cpu_to_be32(magic
),
1793 .len
= cpu_to_be32(len
),
1795 memcpy(buf
+ sizeof(QCowExtension
), s
, len
);
1801 * Updates the qcow2 header, including the variable length parts of it, i.e.
1802 * the backing file name and all extensions. qcow2 was not designed to allow
1803 * such changes, so if we run out of space (we can only use the first cluster)
1804 * this function may fail.
1806 * Returns 0 on success, -errno in error cases.
1808 int qcow2_update_header(BlockDriverState
*bs
)
1810 BDRVQcow2State
*s
= bs
->opaque
;
1813 size_t buflen
= s
->cluster_size
;
1815 uint64_t total_size
;
1816 uint32_t refcount_table_clusters
;
1817 size_t header_length
;
1818 Qcow2UnknownHeaderExtension
*uext
;
1820 buf
= qemu_blockalign(bs
, buflen
);
1822 /* Header structure */
1823 header
= (QCowHeader
*) buf
;
1825 if (buflen
< sizeof(*header
)) {
1830 header_length
= sizeof(*header
) + s
->unknown_header_fields_size
;
1831 total_size
= bs
->total_sectors
* BDRV_SECTOR_SIZE
;
1832 refcount_table_clusters
= s
->refcount_table_size
>> (s
->cluster_bits
- 3);
1834 *header
= (QCowHeader
) {
1835 /* Version 2 fields */
1836 .magic
= cpu_to_be32(QCOW_MAGIC
),
1837 .version
= cpu_to_be32(s
->qcow_version
),
1838 .backing_file_offset
= 0,
1839 .backing_file_size
= 0,
1840 .cluster_bits
= cpu_to_be32(s
->cluster_bits
),
1841 .size
= cpu_to_be64(total_size
),
1842 .crypt_method
= cpu_to_be32(s
->crypt_method_header
),
1843 .l1_size
= cpu_to_be32(s
->l1_size
),
1844 .l1_table_offset
= cpu_to_be64(s
->l1_table_offset
),
1845 .refcount_table_offset
= cpu_to_be64(s
->refcount_table_offset
),
1846 .refcount_table_clusters
= cpu_to_be32(refcount_table_clusters
),
1847 .nb_snapshots
= cpu_to_be32(s
->nb_snapshots
),
1848 .snapshots_offset
= cpu_to_be64(s
->snapshots_offset
),
1850 /* Version 3 fields */
1851 .incompatible_features
= cpu_to_be64(s
->incompatible_features
),
1852 .compatible_features
= cpu_to_be64(s
->compatible_features
),
1853 .autoclear_features
= cpu_to_be64(s
->autoclear_features
),
1854 .refcount_order
= cpu_to_be32(s
->refcount_order
),
1855 .header_length
= cpu_to_be32(header_length
),
1858 /* For older versions, write a shorter header */
1859 switch (s
->qcow_version
) {
1861 ret
= offsetof(QCowHeader
, incompatible_features
);
1864 ret
= sizeof(*header
);
1873 memset(buf
, 0, buflen
);
1875 /* Preserve any unknown field in the header */
1876 if (s
->unknown_header_fields_size
) {
1877 if (buflen
< s
->unknown_header_fields_size
) {
1882 memcpy(buf
, s
->unknown_header_fields
, s
->unknown_header_fields_size
);
1883 buf
+= s
->unknown_header_fields_size
;
1884 buflen
-= s
->unknown_header_fields_size
;
1887 /* Backing file format header extension */
1888 if (s
->image_backing_format
) {
1889 ret
= header_ext_add(buf
, QCOW2_EXT_MAGIC_BACKING_FORMAT
,
1890 s
->image_backing_format
,
1891 strlen(s
->image_backing_format
),
1902 if (s
->qcow_version
>= 3) {
1903 Qcow2Feature features
[] = {
1905 .type
= QCOW2_FEAT_TYPE_INCOMPATIBLE
,
1906 .bit
= QCOW2_INCOMPAT_DIRTY_BITNR
,
1907 .name
= "dirty bit",
1910 .type
= QCOW2_FEAT_TYPE_INCOMPATIBLE
,
1911 .bit
= QCOW2_INCOMPAT_CORRUPT_BITNR
,
1912 .name
= "corrupt bit",
1915 .type
= QCOW2_FEAT_TYPE_COMPATIBLE
,
1916 .bit
= QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR
,
1917 .name
= "lazy refcounts",
1921 ret
= header_ext_add(buf
, QCOW2_EXT_MAGIC_FEATURE_TABLE
,
1922 features
, sizeof(features
), buflen
);
1930 /* Keep unknown header extensions */
1931 QLIST_FOREACH(uext
, &s
->unknown_header_ext
, next
) {
1932 ret
= header_ext_add(buf
, uext
->magic
, uext
->data
, uext
->len
, buflen
);
1941 /* End of header extensions */
1942 ret
= header_ext_add(buf
, QCOW2_EXT_MAGIC_END
, NULL
, 0, buflen
);
1950 /* Backing file name */
1951 if (s
->image_backing_file
) {
1952 size_t backing_file_len
= strlen(s
->image_backing_file
);
1954 if (buflen
< backing_file_len
) {
1959 /* Using strncpy is ok here, since buf is not NUL-terminated. */
1960 strncpy(buf
, s
->image_backing_file
, buflen
);
1962 header
->backing_file_offset
= cpu_to_be64(buf
- ((char*) header
));
1963 header
->backing_file_size
= cpu_to_be32(backing_file_len
);
1966 /* Write the new header */
1967 ret
= bdrv_pwrite(bs
->file
->bs
, 0, header
, s
->cluster_size
);
1978 static int qcow2_change_backing_file(BlockDriverState
*bs
,
1979 const char *backing_file
, const char *backing_fmt
)
1981 BDRVQcow2State
*s
= bs
->opaque
;
1983 if (backing_file
&& strlen(backing_file
) > 1023) {
1987 pstrcpy(bs
->backing_file
, sizeof(bs
->backing_file
), backing_file
?: "");
1988 pstrcpy(bs
->backing_format
, sizeof(bs
->backing_format
), backing_fmt
?: "");
1990 g_free(s
->image_backing_file
);
1991 g_free(s
->image_backing_format
);
1993 s
->image_backing_file
= backing_file
? g_strdup(bs
->backing_file
) : NULL
;
1994 s
->image_backing_format
= backing_fmt
? g_strdup(bs
->backing_format
) : NULL
;
1996 return qcow2_update_header(bs
);
1999 static int preallocate(BlockDriverState
*bs
)
2001 uint64_t nb_sectors
;
2003 uint64_t host_offset
= 0;
2008 nb_sectors
= bdrv_nb_sectors(bs
);
2011 while (nb_sectors
) {
2012 num
= MIN(nb_sectors
, INT_MAX
>> BDRV_SECTOR_BITS
);
2013 ret
= qcow2_alloc_cluster_offset(bs
, offset
, &num
,
2014 &host_offset
, &meta
);
2020 QCowL2Meta
*next
= meta
->next
;
2022 ret
= qcow2_alloc_cluster_link_l2(bs
, meta
);
2024 qcow2_free_any_clusters(bs
, meta
->alloc_offset
,
2025 meta
->nb_clusters
, QCOW2_DISCARD_NEVER
);
2029 /* There are no dependent requests, but we need to remove our
2030 * request from the list of in-flight requests */
2031 QLIST_REMOVE(meta
, next_in_flight
);
2037 /* TODO Preallocate data if requested */
2040 offset
+= num
<< BDRV_SECTOR_BITS
;
2044 * It is expected that the image file is large enough to actually contain
2045 * all of the allocated clusters (otherwise we get failing reads after
2046 * EOF). Extend the image to the last allocated sector.
2048 if (host_offset
!= 0) {
2049 uint8_t buf
[BDRV_SECTOR_SIZE
];
2050 memset(buf
, 0, BDRV_SECTOR_SIZE
);
2051 ret
= bdrv_write(bs
->file
->bs
,
2052 (host_offset
>> BDRV_SECTOR_BITS
) + num
- 1,
2062 static int qcow2_create2(const char *filename
, int64_t total_size
,
2063 const char *backing_file
, const char *backing_format
,
2064 int flags
, size_t cluster_size
, PreallocMode prealloc
,
2065 QemuOpts
*opts
, int version
, int refcount_order
,
2071 /* Calculate cluster_bits */
2072 cluster_bits
= ctz32(cluster_size
);
2073 if (cluster_bits
< MIN_CLUSTER_BITS
|| cluster_bits
> MAX_CLUSTER_BITS
||
2074 (1 << cluster_bits
) != cluster_size
)
2076 error_setg(errp
, "Cluster size must be a power of two between %d and "
2077 "%dk", 1 << MIN_CLUSTER_BITS
, 1 << (MAX_CLUSTER_BITS
- 10));
2082 * Open the image file and write a minimal qcow2 header.
2084 * We keep things simple and start with a zero-sized image. We also
2085 * do without refcount blocks or a L1 table for now. We'll fix the
2086 * inconsistency later.
2088 * We do need a refcount table because growing the refcount table means
2089 * allocating two new refcount blocks - the seconds of which would be at
2090 * 2 GB for 64k clusters, and we don't want to have a 2 GB initial file
2091 * size for any qcow2 image.
2095 uint64_t* refcount_table
;
2096 Error
*local_err
= NULL
;
2099 if (prealloc
== PREALLOC_MODE_FULL
|| prealloc
== PREALLOC_MODE_FALLOC
) {
2100 /* Note: The following calculation does not need to be exact; if it is a
2101 * bit off, either some bytes will be "leaked" (which is fine) or we
2102 * will need to increase the file size by some bytes (which is fine,
2103 * too, as long as the bulk is allocated here). Therefore, using
2104 * floating point arithmetic is fine. */
2105 int64_t meta_size
= 0;
2106 uint64_t nreftablee
, nrefblocke
, nl1e
, nl2e
;
2107 int64_t aligned_total_size
= align_offset(total_size
, cluster_size
);
2108 int refblock_bits
, refblock_size
;
2109 /* refcount entry size in bytes */
2110 double rces
= (1 << refcount_order
) / 8.;
2112 /* see qcow2_open() */
2113 refblock_bits
= cluster_bits
- (refcount_order
- 3);
2114 refblock_size
= 1 << refblock_bits
;
2116 /* header: 1 cluster */
2117 meta_size
+= cluster_size
;
2119 /* total size of L2 tables */
2120 nl2e
= aligned_total_size
/ cluster_size
;
2121 nl2e
= align_offset(nl2e
, cluster_size
/ sizeof(uint64_t));
2122 meta_size
+= nl2e
* sizeof(uint64_t);
2124 /* total size of L1 tables */
2125 nl1e
= nl2e
* sizeof(uint64_t) / cluster_size
;
2126 nl1e
= align_offset(nl1e
, cluster_size
/ sizeof(uint64_t));
2127 meta_size
+= nl1e
* sizeof(uint64_t);
2129 /* total size of refcount blocks
2131 * note: every host cluster is reference-counted, including metadata
2132 * (even refcount blocks are recursively included).
2134 * a = total_size (this is the guest disk size)
2135 * m = meta size not including refcount blocks and refcount tables
2137 * y1 = number of refcount blocks entries
2138 * y2 = meta size including everything
2139 * rces = refcount entry size in bytes
2142 * y2 = y1 * rces + y1 * rces * sizeof(u64) / c + m
2144 * y1 = (a + m) / (c - rces - rces * sizeof(u64) / c)
2146 nrefblocke
= (aligned_total_size
+ meta_size
+ cluster_size
)
2147 / (cluster_size
- rces
- rces
* sizeof(uint64_t)
2149 meta_size
+= DIV_ROUND_UP(nrefblocke
, refblock_size
) * cluster_size
;
2151 /* total size of refcount tables */
2152 nreftablee
= nrefblocke
/ refblock_size
;
2153 nreftablee
= align_offset(nreftablee
, cluster_size
/ sizeof(uint64_t));
2154 meta_size
+= nreftablee
* sizeof(uint64_t);
2156 qemu_opt_set_number(opts
, BLOCK_OPT_SIZE
,
2157 aligned_total_size
+ meta_size
, &error_abort
);
2158 qemu_opt_set(opts
, BLOCK_OPT_PREALLOC
, PreallocMode_lookup
[prealloc
],
2162 ret
= bdrv_create_file(filename
, opts
, &local_err
);
2164 error_propagate(errp
, local_err
);
2168 blk
= blk_new_open(filename
, NULL
, NULL
,
2169 BDRV_O_RDWR
| BDRV_O_PROTOCOL
, &local_err
);
2171 error_propagate(errp
, local_err
);
2175 blk_set_allow_write_beyond_eof(blk
, true);
2177 /* Write the header */
2178 QEMU_BUILD_BUG_ON((1 << MIN_CLUSTER_BITS
) < sizeof(*header
));
2179 header
= g_malloc0(cluster_size
);
2180 *header
= (QCowHeader
) {
2181 .magic
= cpu_to_be32(QCOW_MAGIC
),
2182 .version
= cpu_to_be32(version
),
2183 .cluster_bits
= cpu_to_be32(cluster_bits
),
2184 .size
= cpu_to_be64(0),
2185 .l1_table_offset
= cpu_to_be64(0),
2186 .l1_size
= cpu_to_be32(0),
2187 .refcount_table_offset
= cpu_to_be64(cluster_size
),
2188 .refcount_table_clusters
= cpu_to_be32(1),
2189 .refcount_order
= cpu_to_be32(refcount_order
),
2190 .header_length
= cpu_to_be32(sizeof(*header
)),
2193 if (flags
& BLOCK_FLAG_ENCRYPT
) {
2194 header
->crypt_method
= cpu_to_be32(QCOW_CRYPT_AES
);
2196 header
->crypt_method
= cpu_to_be32(QCOW_CRYPT_NONE
);
2199 if (flags
& BLOCK_FLAG_LAZY_REFCOUNTS
) {
2200 header
->compatible_features
|=
2201 cpu_to_be64(QCOW2_COMPAT_LAZY_REFCOUNTS
);
2204 ret
= blk_pwrite(blk
, 0, header
, cluster_size
, 0);
2207 error_setg_errno(errp
, -ret
, "Could not write qcow2 header");
2211 /* Write a refcount table with one refcount block */
2212 refcount_table
= g_malloc0(2 * cluster_size
);
2213 refcount_table
[0] = cpu_to_be64(2 * cluster_size
);
2214 ret
= blk_pwrite(blk
, cluster_size
, refcount_table
, 2 * cluster_size
, 0);
2215 g_free(refcount_table
);
2218 error_setg_errno(errp
, -ret
, "Could not write refcount table");
2226 * And now open the image and make it consistent first (i.e. increase the
2227 * refcount of the cluster that is occupied by the header and the refcount
2230 options
= qdict_new();
2231 qdict_put(options
, "driver", qstring_from_str("qcow2"));
2232 blk
= blk_new_open(filename
, NULL
, options
,
2233 BDRV_O_RDWR
| BDRV_O_NO_FLUSH
, &local_err
);
2235 error_propagate(errp
, local_err
);
2240 ret
= qcow2_alloc_clusters(blk_bs(blk
), 3 * cluster_size
);
2242 error_setg_errno(errp
, -ret
, "Could not allocate clusters for qcow2 "
2243 "header and refcount table");
2246 } else if (ret
!= 0) {
2247 error_report("Huh, first cluster in empty image is already in use?");
2251 /* Create a full header (including things like feature table) */
2252 ret
= qcow2_update_header(blk_bs(blk
));
2254 error_setg_errno(errp
, -ret
, "Could not update qcow2 header");
2258 /* Okay, now that we have a valid image, let's give it the right size */
2259 ret
= blk_truncate(blk
, total_size
);
2261 error_setg_errno(errp
, -ret
, "Could not resize image");
2265 /* Want a backing file? There you go.*/
2267 ret
= bdrv_change_backing_file(blk_bs(blk
), backing_file
, backing_format
);
2269 error_setg_errno(errp
, -ret
, "Could not assign backing file '%s' "
2270 "with format '%s'", backing_file
, backing_format
);
2275 /* And if we're supposed to preallocate metadata, do that now */
2276 if (prealloc
!= PREALLOC_MODE_OFF
) {
2277 BDRVQcow2State
*s
= blk_bs(blk
)->opaque
;
2278 qemu_co_mutex_lock(&s
->lock
);
2279 ret
= preallocate(blk_bs(blk
));
2280 qemu_co_mutex_unlock(&s
->lock
);
2282 error_setg_errno(errp
, -ret
, "Could not preallocate metadata");
2290 /* Reopen the image without BDRV_O_NO_FLUSH to flush it before returning */
2291 options
= qdict_new();
2292 qdict_put(options
, "driver", qstring_from_str("qcow2"));
2293 blk
= blk_new_open(filename
, NULL
, options
,
2294 BDRV_O_RDWR
| BDRV_O_NO_BACKING
, &local_err
);
2296 error_propagate(errp
, local_err
);
2309 static int qcow2_create(const char *filename
, QemuOpts
*opts
, Error
**errp
)
2311 char *backing_file
= NULL
;
2312 char *backing_fmt
= NULL
;
2316 size_t cluster_size
= DEFAULT_CLUSTER_SIZE
;
2317 PreallocMode prealloc
;
2319 uint64_t refcount_bits
= 16;
2321 Error
*local_err
= NULL
;
2324 /* Read out options */
2325 size
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
2327 backing_file
= qemu_opt_get_del(opts
, BLOCK_OPT_BACKING_FILE
);
2328 backing_fmt
= qemu_opt_get_del(opts
, BLOCK_OPT_BACKING_FMT
);
2329 if (qemu_opt_get_bool_del(opts
, BLOCK_OPT_ENCRYPT
, false)) {
2330 flags
|= BLOCK_FLAG_ENCRYPT
;
2332 cluster_size
= qemu_opt_get_size_del(opts
, BLOCK_OPT_CLUSTER_SIZE
,
2333 DEFAULT_CLUSTER_SIZE
);
2334 buf
= qemu_opt_get_del(opts
, BLOCK_OPT_PREALLOC
);
2335 prealloc
= qapi_enum_parse(PreallocMode_lookup
, buf
,
2336 PREALLOC_MODE__MAX
, PREALLOC_MODE_OFF
,
2339 error_propagate(errp
, local_err
);
2344 buf
= qemu_opt_get_del(opts
, BLOCK_OPT_COMPAT_LEVEL
);
2346 /* keep the default */
2347 } else if (!strcmp(buf
, "0.10")) {
2349 } else if (!strcmp(buf
, "1.1")) {
2352 error_setg(errp
, "Invalid compatibility level: '%s'", buf
);
2357 if (qemu_opt_get_bool_del(opts
, BLOCK_OPT_LAZY_REFCOUNTS
, false)) {
2358 flags
|= BLOCK_FLAG_LAZY_REFCOUNTS
;
2361 if (backing_file
&& prealloc
!= PREALLOC_MODE_OFF
) {
2362 error_setg(errp
, "Backing file and preallocation cannot be used at "
2368 if (version
< 3 && (flags
& BLOCK_FLAG_LAZY_REFCOUNTS
)) {
2369 error_setg(errp
, "Lazy refcounts only supported with compatibility "
2370 "level 1.1 and above (use compat=1.1 or greater)");
2375 refcount_bits
= qemu_opt_get_number_del(opts
, BLOCK_OPT_REFCOUNT_BITS
,
2377 if (refcount_bits
> 64 || !is_power_of_2(refcount_bits
)) {
2378 error_setg(errp
, "Refcount width must be a power of two and may not "
2384 if (version
< 3 && refcount_bits
!= 16) {
2385 error_setg(errp
, "Different refcount widths than 16 bits require "
2386 "compatibility level 1.1 or above (use compat=1.1 or "
2392 refcount_order
= ctz32(refcount_bits
);
2394 ret
= qcow2_create2(filename
, size
, backing_file
, backing_fmt
, flags
,
2395 cluster_size
, prealloc
, opts
, version
, refcount_order
,
2398 error_propagate(errp
, local_err
);
2402 g_free(backing_file
);
2403 g_free(backing_fmt
);
2409 static bool is_zero_cluster(BlockDriverState
*bs
, int64_t start
)
2411 BDRVQcow2State
*s
= bs
->opaque
;
2413 BlockDriverState
*file
;
2414 int64_t res
= bdrv_get_block_status_above(bs
, NULL
, start
,
2415 s
->cluster_sectors
, &nr
, &file
);
2416 return res
>= 0 && (res
& BDRV_BLOCK_ZERO
) && nr
== s
->cluster_sectors
;
2419 static bool is_zero_cluster_top_locked(BlockDriverState
*bs
, int64_t start
)
2421 BDRVQcow2State
*s
= bs
->opaque
;
2422 int nr
= s
->cluster_sectors
;
2426 ret
= qcow2_get_cluster_offset(bs
, start
<< BDRV_SECTOR_BITS
, &nr
, &off
);
2427 assert(nr
== s
->cluster_sectors
);
2428 return ret
== QCOW2_CLUSTER_UNALLOCATED
|| ret
== QCOW2_CLUSTER_ZERO
;
2431 static coroutine_fn
int qcow2_co_write_zeroes(BlockDriverState
*bs
,
2432 int64_t sector_num
, int nb_sectors
, BdrvRequestFlags flags
)
2435 BDRVQcow2State
*s
= bs
->opaque
;
2437 int head
= sector_num
% s
->cluster_sectors
;
2438 int tail
= (sector_num
+ nb_sectors
) % s
->cluster_sectors
;
2440 if (head
!= 0 || tail
!= 0) {
2441 int64_t cl_end
= -1;
2447 nb_sectors
+= s
->cluster_sectors
- tail
;
2450 if (!is_zero_cluster(bs
, sector_num
)) {
2454 if (nb_sectors
> s
->cluster_sectors
) {
2455 /* Technically the request can cover 2 clusters, f.e. 4k write
2456 at s->cluster_sectors - 2k offset. One of these cluster can
2457 be zeroed, one unallocated */
2458 cl_end
= sector_num
+ nb_sectors
- s
->cluster_sectors
;
2459 if (!is_zero_cluster(bs
, cl_end
)) {
2464 qemu_co_mutex_lock(&s
->lock
);
2465 /* We can have new write after previous check */
2466 if (!is_zero_cluster_top_locked(bs
, sector_num
) ||
2467 (cl_end
> 0 && !is_zero_cluster_top_locked(bs
, cl_end
))) {
2468 qemu_co_mutex_unlock(&s
->lock
);
2472 qemu_co_mutex_lock(&s
->lock
);
2475 /* Whatever is left can use real zero clusters */
2476 ret
= qcow2_zero_clusters(bs
, sector_num
<< BDRV_SECTOR_BITS
, nb_sectors
);
2477 qemu_co_mutex_unlock(&s
->lock
);
2482 static coroutine_fn
int qcow2_co_discard(BlockDriverState
*bs
,
2483 int64_t sector_num
, int nb_sectors
)
2486 BDRVQcow2State
*s
= bs
->opaque
;
2488 qemu_co_mutex_lock(&s
->lock
);
2489 ret
= qcow2_discard_clusters(bs
, sector_num
<< BDRV_SECTOR_BITS
,
2490 nb_sectors
, QCOW2_DISCARD_REQUEST
, false);
2491 qemu_co_mutex_unlock(&s
->lock
);
2495 static int qcow2_truncate(BlockDriverState
*bs
, int64_t offset
)
2497 BDRVQcow2State
*s
= bs
->opaque
;
2498 int64_t new_l1_size
;
2502 error_report("The new size must be a multiple of 512");
2506 /* cannot proceed if image has snapshots */
2507 if (s
->nb_snapshots
) {
2508 error_report("Can't resize an image which has snapshots");
2512 /* shrinking is currently not supported */
2513 if (offset
< bs
->total_sectors
* 512) {
2514 error_report("qcow2 doesn't support shrinking images yet");
2518 new_l1_size
= size_to_l1(s
, offset
);
2519 ret
= qcow2_grow_l1_table(bs
, new_l1_size
, true);
2524 /* write updated header.size */
2525 offset
= cpu_to_be64(offset
);
2526 ret
= bdrv_pwrite_sync(bs
->file
->bs
, offsetof(QCowHeader
, size
),
2527 &offset
, sizeof(uint64_t));
2532 s
->l1_vm_state_index
= new_l1_size
;
2536 /* XXX: put compressed sectors first, then all the cluster aligned
2537 tables to avoid losing bytes in alignment */
2538 static int qcow2_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
2539 const uint8_t *buf
, int nb_sectors
)
2541 BDRVQcow2State
*s
= bs
->opaque
;
2545 uint64_t cluster_offset
;
2547 if (nb_sectors
== 0) {
2548 /* align end of file to a sector boundary to ease reading with
2549 sector based I/Os */
2550 cluster_offset
= bdrv_getlength(bs
->file
->bs
);
2551 return bdrv_truncate(bs
->file
->bs
, cluster_offset
);
2554 if (nb_sectors
!= s
->cluster_sectors
) {
2557 /* Zero-pad last write if image size is not cluster aligned */
2558 if (sector_num
+ nb_sectors
== bs
->total_sectors
&&
2559 nb_sectors
< s
->cluster_sectors
) {
2560 uint8_t *pad_buf
= qemu_blockalign(bs
, s
->cluster_size
);
2561 memset(pad_buf
, 0, s
->cluster_size
);
2562 memcpy(pad_buf
, buf
, nb_sectors
* BDRV_SECTOR_SIZE
);
2563 ret
= qcow2_write_compressed(bs
, sector_num
,
2564 pad_buf
, s
->cluster_sectors
);
2565 qemu_vfree(pad_buf
);
2570 out_buf
= g_malloc(s
->cluster_size
+ (s
->cluster_size
/ 1000) + 128);
2572 /* best compression, small window, no zlib header */
2573 memset(&strm
, 0, sizeof(strm
));
2574 ret
= deflateInit2(&strm
, Z_DEFAULT_COMPRESSION
,
2576 9, Z_DEFAULT_STRATEGY
);
2582 strm
.avail_in
= s
->cluster_size
;
2583 strm
.next_in
= (uint8_t *)buf
;
2584 strm
.avail_out
= s
->cluster_size
;
2585 strm
.next_out
= out_buf
;
2587 ret
= deflate(&strm
, Z_FINISH
);
2588 if (ret
!= Z_STREAM_END
&& ret
!= Z_OK
) {
2593 out_len
= strm
.next_out
- out_buf
;
2597 if (ret
!= Z_STREAM_END
|| out_len
>= s
->cluster_size
) {
2598 /* could not compress: write normal cluster */
2599 ret
= bdrv_write(bs
, sector_num
, buf
, s
->cluster_sectors
);
2604 cluster_offset
= qcow2_alloc_compressed_cluster_offset(bs
,
2605 sector_num
<< 9, out_len
);
2606 if (!cluster_offset
) {
2610 cluster_offset
&= s
->cluster_offset_mask
;
2612 ret
= qcow2_pre_write_overlap_check(bs
, 0, cluster_offset
, out_len
);
2617 BLKDBG_EVENT(bs
->file
, BLKDBG_WRITE_COMPRESSED
);
2618 ret
= bdrv_pwrite(bs
->file
->bs
, cluster_offset
, out_buf
, out_len
);
2630 static int make_completely_empty(BlockDriverState
*bs
)
2632 BDRVQcow2State
*s
= bs
->opaque
;
2633 int ret
, l1_clusters
;
2635 uint64_t *new_reftable
= NULL
;
2636 uint64_t rt_entry
, l1_size2
;
2639 uint64_t reftable_offset
;
2640 uint32_t reftable_clusters
;
2641 } QEMU_PACKED l1_ofs_rt_ofs_cls
;
2643 ret
= qcow2_cache_empty(bs
, s
->l2_table_cache
);
2648 ret
= qcow2_cache_empty(bs
, s
->refcount_block_cache
);
2653 /* Refcounts will be broken utterly */
2654 ret
= qcow2_mark_dirty(bs
);
2659 BLKDBG_EVENT(bs
->file
, BLKDBG_L1_UPDATE
);
2661 l1_clusters
= DIV_ROUND_UP(s
->l1_size
, s
->cluster_size
/ sizeof(uint64_t));
2662 l1_size2
= (uint64_t)s
->l1_size
* sizeof(uint64_t);
2664 /* After this call, neither the in-memory nor the on-disk refcount
2665 * information accurately describe the actual references */
2667 ret
= bdrv_write_zeroes(bs
->file
->bs
, s
->l1_table_offset
/ BDRV_SECTOR_SIZE
,
2668 l1_clusters
* s
->cluster_sectors
, 0);
2670 goto fail_broken_refcounts
;
2672 memset(s
->l1_table
, 0, l1_size2
);
2674 BLKDBG_EVENT(bs
->file
, BLKDBG_EMPTY_IMAGE_PREPARE
);
2676 /* Overwrite enough clusters at the beginning of the sectors to place
2677 * the refcount table, a refcount block and the L1 table in; this may
2678 * overwrite parts of the existing refcount and L1 table, which is not
2679 * an issue because the dirty flag is set, complete data loss is in fact
2680 * desired and partial data loss is consequently fine as well */
2681 ret
= bdrv_write_zeroes(bs
->file
->bs
, s
->cluster_size
/ BDRV_SECTOR_SIZE
,
2682 (2 + l1_clusters
) * s
->cluster_size
/
2683 BDRV_SECTOR_SIZE
, 0);
2684 /* This call (even if it failed overall) may have overwritten on-disk
2685 * refcount structures; in that case, the in-memory refcount information
2686 * will probably differ from the on-disk information which makes the BDS
2689 goto fail_broken_refcounts
;
2692 BLKDBG_EVENT(bs
->file
, BLKDBG_L1_UPDATE
);
2693 BLKDBG_EVENT(bs
->file
, BLKDBG_REFTABLE_UPDATE
);
2695 /* "Create" an empty reftable (one cluster) directly after the image
2696 * header and an empty L1 table three clusters after the image header;
2697 * the cluster between those two will be used as the first refblock */
2698 cpu_to_be64w(&l1_ofs_rt_ofs_cls
.l1_offset
, 3 * s
->cluster_size
);
2699 cpu_to_be64w(&l1_ofs_rt_ofs_cls
.reftable_offset
, s
->cluster_size
);
2700 cpu_to_be32w(&l1_ofs_rt_ofs_cls
.reftable_clusters
, 1);
2701 ret
= bdrv_pwrite_sync(bs
->file
->bs
, offsetof(QCowHeader
, l1_table_offset
),
2702 &l1_ofs_rt_ofs_cls
, sizeof(l1_ofs_rt_ofs_cls
));
2704 goto fail_broken_refcounts
;
2707 s
->l1_table_offset
= 3 * s
->cluster_size
;
2709 new_reftable
= g_try_new0(uint64_t, s
->cluster_size
/ sizeof(uint64_t));
2710 if (!new_reftable
) {
2712 goto fail_broken_refcounts
;
2715 s
->refcount_table_offset
= s
->cluster_size
;
2716 s
->refcount_table_size
= s
->cluster_size
/ sizeof(uint64_t);
2718 g_free(s
->refcount_table
);
2719 s
->refcount_table
= new_reftable
;
2720 new_reftable
= NULL
;
2722 /* Now the in-memory refcount information again corresponds to the on-disk
2723 * information (reftable is empty and no refblocks (the refblock cache is
2724 * empty)); however, this means some clusters (e.g. the image header) are
2725 * referenced, but not refcounted, but the normal qcow2 code assumes that
2726 * the in-memory information is always correct */
2728 BLKDBG_EVENT(bs
->file
, BLKDBG_REFBLOCK_ALLOC
);
2730 /* Enter the first refblock into the reftable */
2731 rt_entry
= cpu_to_be64(2 * s
->cluster_size
);
2732 ret
= bdrv_pwrite_sync(bs
->file
->bs
, s
->cluster_size
,
2733 &rt_entry
, sizeof(rt_entry
));
2735 goto fail_broken_refcounts
;
2737 s
->refcount_table
[0] = 2 * s
->cluster_size
;
2739 s
->free_cluster_index
= 0;
2740 assert(3 + l1_clusters
<= s
->refcount_block_size
);
2741 offset
= qcow2_alloc_clusters(bs
, 3 * s
->cluster_size
+ l1_size2
);
2744 goto fail_broken_refcounts
;
2745 } else if (offset
> 0) {
2746 error_report("First cluster in emptied image is in use");
2750 /* Now finally the in-memory information corresponds to the on-disk
2751 * structures and is correct */
2752 ret
= qcow2_mark_clean(bs
);
2757 ret
= bdrv_truncate(bs
->file
->bs
, (3 + l1_clusters
) * s
->cluster_size
);
2764 fail_broken_refcounts
:
2765 /* The BDS is unusable at this point. If we wanted to make it usable, we
2766 * would have to call qcow2_refcount_close(), qcow2_refcount_init(),
2767 * qcow2_check_refcounts(), qcow2_refcount_close() and qcow2_refcount_init()
2768 * again. However, because the functions which could have caused this error
2769 * path to be taken are used by those functions as well, it's very likely
2770 * that that sequence will fail as well. Therefore, just eject the BDS. */
2774 g_free(new_reftable
);
2778 static int qcow2_make_empty(BlockDriverState
*bs
)
2780 BDRVQcow2State
*s
= bs
->opaque
;
2781 uint64_t start_sector
;
2782 int sector_step
= INT_MAX
/ BDRV_SECTOR_SIZE
;
2783 int l1_clusters
, ret
= 0;
2785 l1_clusters
= DIV_ROUND_UP(s
->l1_size
, s
->cluster_size
/ sizeof(uint64_t));
2787 if (s
->qcow_version
>= 3 && !s
->snapshots
&&
2788 3 + l1_clusters
<= s
->refcount_block_size
) {
2789 /* The following function only works for qcow2 v3 images (it requires
2790 * the dirty flag) and only as long as there are no snapshots (because
2791 * it completely empties the image). Furthermore, the L1 table and three
2792 * additional clusters (image header, refcount table, one refcount
2793 * block) have to fit inside one refcount block. */
2794 return make_completely_empty(bs
);
2797 /* This fallback code simply discards every active cluster; this is slow,
2798 * but works in all cases */
2799 for (start_sector
= 0; start_sector
< bs
->total_sectors
;
2800 start_sector
+= sector_step
)
2802 /* As this function is generally used after committing an external
2803 * snapshot, QCOW2_DISCARD_SNAPSHOT seems appropriate. Also, the
2804 * default action for this kind of discard is to pass the discard,
2805 * which will ideally result in an actually smaller image file, as
2806 * is probably desired. */
2807 ret
= qcow2_discard_clusters(bs
, start_sector
* BDRV_SECTOR_SIZE
,
2809 bs
->total_sectors
- start_sector
),
2810 QCOW2_DISCARD_SNAPSHOT
, true);
2819 static coroutine_fn
int qcow2_co_flush_to_os(BlockDriverState
*bs
)
2821 BDRVQcow2State
*s
= bs
->opaque
;
2824 qemu_co_mutex_lock(&s
->lock
);
2825 ret
= qcow2_cache_flush(bs
, s
->l2_table_cache
);
2827 qemu_co_mutex_unlock(&s
->lock
);
2831 if (qcow2_need_accurate_refcounts(s
)) {
2832 ret
= qcow2_cache_flush(bs
, s
->refcount_block_cache
);
2834 qemu_co_mutex_unlock(&s
->lock
);
2838 qemu_co_mutex_unlock(&s
->lock
);
2843 static int qcow2_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
2845 BDRVQcow2State
*s
= bs
->opaque
;
2846 bdi
->unallocated_blocks_are_zero
= true;
2847 bdi
->can_write_zeroes_with_unmap
= (s
->qcow_version
>= 3);
2848 bdi
->cluster_size
= s
->cluster_size
;
2849 bdi
->vm_state_offset
= qcow2_vm_state_offset(s
);
2853 static ImageInfoSpecific
*qcow2_get_specific_info(BlockDriverState
*bs
)
2855 BDRVQcow2State
*s
= bs
->opaque
;
2856 ImageInfoSpecific
*spec_info
= g_new(ImageInfoSpecific
, 1);
2858 *spec_info
= (ImageInfoSpecific
){
2859 .type
= IMAGE_INFO_SPECIFIC_KIND_QCOW2
,
2860 .u
.qcow2
.data
= g_new(ImageInfoSpecificQCow2
, 1),
2862 if (s
->qcow_version
== 2) {
2863 *spec_info
->u
.qcow2
.data
= (ImageInfoSpecificQCow2
){
2864 .compat
= g_strdup("0.10"),
2865 .refcount_bits
= s
->refcount_bits
,
2867 } else if (s
->qcow_version
== 3) {
2868 *spec_info
->u
.qcow2
.data
= (ImageInfoSpecificQCow2
){
2869 .compat
= g_strdup("1.1"),
2870 .lazy_refcounts
= s
->compatible_features
&
2871 QCOW2_COMPAT_LAZY_REFCOUNTS
,
2872 .has_lazy_refcounts
= true,
2873 .corrupt
= s
->incompatible_features
&
2874 QCOW2_INCOMPAT_CORRUPT
,
2875 .has_corrupt
= true,
2876 .refcount_bits
= s
->refcount_bits
,
2879 /* if this assertion fails, this probably means a new version was
2880 * added without having it covered here */
2888 static void dump_refcounts(BlockDriverState
*bs
)
2890 BDRVQcow2State
*s
= bs
->opaque
;
2891 int64_t nb_clusters
, k
, k1
, size
;
2894 size
= bdrv_getlength(bs
->file
->bs
);
2895 nb_clusters
= size_to_clusters(s
, size
);
2896 for(k
= 0; k
< nb_clusters
;) {
2898 refcount
= get_refcount(bs
, k
);
2900 while (k
< nb_clusters
&& get_refcount(bs
, k
) == refcount
)
2902 printf("%" PRId64
": refcount=%d nb=%" PRId64
"\n", k
, refcount
,
2908 static int qcow2_save_vmstate(BlockDriverState
*bs
, QEMUIOVector
*qiov
,
2911 BDRVQcow2State
*s
= bs
->opaque
;
2912 int64_t total_sectors
= bs
->total_sectors
;
2913 bool zero_beyond_eof
= bs
->zero_beyond_eof
;
2916 BLKDBG_EVENT(bs
->file
, BLKDBG_VMSTATE_SAVE
);
2917 bs
->zero_beyond_eof
= false;
2918 ret
= bdrv_pwritev(bs
, qcow2_vm_state_offset(s
) + pos
, qiov
);
2919 bs
->zero_beyond_eof
= zero_beyond_eof
;
2921 /* bdrv_co_do_writev will have increased the total_sectors value to include
2922 * the VM state - the VM state is however not an actual part of the block
2923 * device, therefore, we need to restore the old value. */
2924 bs
->total_sectors
= total_sectors
;
2929 static int qcow2_load_vmstate(BlockDriverState
*bs
, uint8_t *buf
,
2930 int64_t pos
, int size
)
2932 BDRVQcow2State
*s
= bs
->opaque
;
2933 bool zero_beyond_eof
= bs
->zero_beyond_eof
;
2936 BLKDBG_EVENT(bs
->file
, BLKDBG_VMSTATE_LOAD
);
2937 bs
->zero_beyond_eof
= false;
2938 ret
= bdrv_pread(bs
, qcow2_vm_state_offset(s
) + pos
, buf
, size
);
2939 bs
->zero_beyond_eof
= zero_beyond_eof
;
2945 * Downgrades an image's version. To achieve this, any incompatible features
2946 * have to be removed.
2948 static int qcow2_downgrade(BlockDriverState
*bs
, int target_version
,
2949 BlockDriverAmendStatusCB
*status_cb
, void *cb_opaque
)
2951 BDRVQcow2State
*s
= bs
->opaque
;
2952 int current_version
= s
->qcow_version
;
2955 if (target_version
== current_version
) {
2957 } else if (target_version
> current_version
) {
2959 } else if (target_version
!= 2) {
2963 if (s
->refcount_order
!= 4) {
2964 error_report("compat=0.10 requires refcount_bits=16");
2968 /* clear incompatible features */
2969 if (s
->incompatible_features
& QCOW2_INCOMPAT_DIRTY
) {
2970 ret
= qcow2_mark_clean(bs
);
2976 /* with QCOW2_INCOMPAT_CORRUPT, it is pretty much impossible to get here in
2977 * the first place; if that happens nonetheless, returning -ENOTSUP is the
2978 * best thing to do anyway */
2980 if (s
->incompatible_features
) {
2984 /* since we can ignore compatible features, we can set them to 0 as well */
2985 s
->compatible_features
= 0;
2986 /* if lazy refcounts have been used, they have already been fixed through
2987 * clearing the dirty flag */
2989 /* clearing autoclear features is trivial */
2990 s
->autoclear_features
= 0;
2992 ret
= qcow2_expand_zero_clusters(bs
, status_cb
, cb_opaque
);
2997 s
->qcow_version
= target_version
;
2998 ret
= qcow2_update_header(bs
);
3000 s
->qcow_version
= current_version
;
3006 typedef enum Qcow2AmendOperation
{
3007 /* This is the value Qcow2AmendHelperCBInfo::last_operation will be
3008 * statically initialized to so that the helper CB can discern the first
3009 * invocation from an operation change */
3010 QCOW2_NO_OPERATION
= 0,
3012 QCOW2_CHANGING_REFCOUNT_ORDER
,
3014 } Qcow2AmendOperation
;
3016 typedef struct Qcow2AmendHelperCBInfo
{
3017 /* The code coordinating the amend operations should only modify
3018 * these four fields; the rest will be managed by the CB */
3019 BlockDriverAmendStatusCB
*original_status_cb
;
3020 void *original_cb_opaque
;
3022 Qcow2AmendOperation current_operation
;
3024 /* Total number of operations to perform (only set once) */
3025 int total_operations
;
3027 /* The following fields are managed by the CB */
3029 /* Number of operations completed */
3030 int operations_completed
;
3032 /* Cumulative offset of all completed operations */
3033 int64_t offset_completed
;
3035 Qcow2AmendOperation last_operation
;
3036 int64_t last_work_size
;
3037 } Qcow2AmendHelperCBInfo
;
3039 static void qcow2_amend_helper_cb(BlockDriverState
*bs
,
3040 int64_t operation_offset
,
3041 int64_t operation_work_size
, void *opaque
)
3043 Qcow2AmendHelperCBInfo
*info
= opaque
;
3044 int64_t current_work_size
;
3045 int64_t projected_work_size
;
3047 if (info
->current_operation
!= info
->last_operation
) {
3048 if (info
->last_operation
!= QCOW2_NO_OPERATION
) {
3049 info
->offset_completed
+= info
->last_work_size
;
3050 info
->operations_completed
++;
3053 info
->last_operation
= info
->current_operation
;
3056 assert(info
->total_operations
> 0);
3057 assert(info
->operations_completed
< info
->total_operations
);
3059 info
->last_work_size
= operation_work_size
;
3061 current_work_size
= info
->offset_completed
+ operation_work_size
;
3063 /* current_work_size is the total work size for (operations_completed + 1)
3064 * operations (which includes this one), so multiply it by the number of
3065 * operations not covered and divide it by the number of operations
3066 * covered to get a projection for the operations not covered */
3067 projected_work_size
= current_work_size
* (info
->total_operations
-
3068 info
->operations_completed
- 1)
3069 / (info
->operations_completed
+ 1);
3071 info
->original_status_cb(bs
, info
->offset_completed
+ operation_offset
,
3072 current_work_size
+ projected_work_size
,
3073 info
->original_cb_opaque
);
3076 static int qcow2_amend_options(BlockDriverState
*bs
, QemuOpts
*opts
,
3077 BlockDriverAmendStatusCB
*status_cb
,
3080 BDRVQcow2State
*s
= bs
->opaque
;
3081 int old_version
= s
->qcow_version
, new_version
= old_version
;
3082 uint64_t new_size
= 0;
3083 const char *backing_file
= NULL
, *backing_format
= NULL
;
3084 bool lazy_refcounts
= s
->use_lazy_refcounts
;
3085 const char *compat
= NULL
;
3086 uint64_t cluster_size
= s
->cluster_size
;
3088 int refcount_bits
= s
->refcount_bits
;
3090 QemuOptDesc
*desc
= opts
->list
->desc
;
3091 Qcow2AmendHelperCBInfo helper_cb_info
;
3093 while (desc
&& desc
->name
) {
3094 if (!qemu_opt_find(opts
, desc
->name
)) {
3095 /* only change explicitly defined options */
3100 if (!strcmp(desc
->name
, BLOCK_OPT_COMPAT_LEVEL
)) {
3101 compat
= qemu_opt_get(opts
, BLOCK_OPT_COMPAT_LEVEL
);
3103 /* preserve default */
3104 } else if (!strcmp(compat
, "0.10")) {
3106 } else if (!strcmp(compat
, "1.1")) {
3109 error_report("Unknown compatibility level %s", compat
);
3112 } else if (!strcmp(desc
->name
, BLOCK_OPT_PREALLOC
)) {
3113 error_report("Cannot change preallocation mode");
3115 } else if (!strcmp(desc
->name
, BLOCK_OPT_SIZE
)) {
3116 new_size
= qemu_opt_get_size(opts
, BLOCK_OPT_SIZE
, 0);
3117 } else if (!strcmp(desc
->name
, BLOCK_OPT_BACKING_FILE
)) {
3118 backing_file
= qemu_opt_get(opts
, BLOCK_OPT_BACKING_FILE
);
3119 } else if (!strcmp(desc
->name
, BLOCK_OPT_BACKING_FMT
)) {
3120 backing_format
= qemu_opt_get(opts
, BLOCK_OPT_BACKING_FMT
);
3121 } else if (!strcmp(desc
->name
, BLOCK_OPT_ENCRYPT
)) {
3122 encrypt
= qemu_opt_get_bool(opts
, BLOCK_OPT_ENCRYPT
,
3125 if (encrypt
!= !!s
->cipher
) {
3126 error_report("Changing the encryption flag is not supported");
3129 } else if (!strcmp(desc
->name
, BLOCK_OPT_CLUSTER_SIZE
)) {
3130 cluster_size
= qemu_opt_get_size(opts
, BLOCK_OPT_CLUSTER_SIZE
,
3132 if (cluster_size
!= s
->cluster_size
) {
3133 error_report("Changing the cluster size is not supported");
3136 } else if (!strcmp(desc
->name
, BLOCK_OPT_LAZY_REFCOUNTS
)) {
3137 lazy_refcounts
= qemu_opt_get_bool(opts
, BLOCK_OPT_LAZY_REFCOUNTS
,
3139 } else if (!strcmp(desc
->name
, BLOCK_OPT_REFCOUNT_BITS
)) {
3140 refcount_bits
= qemu_opt_get_number(opts
, BLOCK_OPT_REFCOUNT_BITS
,
3143 if (refcount_bits
<= 0 || refcount_bits
> 64 ||
3144 !is_power_of_2(refcount_bits
))
3146 error_report("Refcount width must be a power of two and may "
3147 "not exceed 64 bits");
3151 /* if this point is reached, this probably means a new option was
3152 * added without having it covered here */
3159 helper_cb_info
= (Qcow2AmendHelperCBInfo
){
3160 .original_status_cb
= status_cb
,
3161 .original_cb_opaque
= cb_opaque
,
3162 .total_operations
= (new_version
< old_version
)
3163 + (s
->refcount_bits
!= refcount_bits
)
3166 /* Upgrade first (some features may require compat=1.1) */
3167 if (new_version
> old_version
) {
3168 s
->qcow_version
= new_version
;
3169 ret
= qcow2_update_header(bs
);
3171 s
->qcow_version
= old_version
;
3176 if (s
->refcount_bits
!= refcount_bits
) {
3177 int refcount_order
= ctz32(refcount_bits
);
3178 Error
*local_error
= NULL
;
3180 if (new_version
< 3 && refcount_bits
!= 16) {
3181 error_report("Different refcount widths than 16 bits require "
3182 "compatibility level 1.1 or above (use compat=1.1 or "
3187 helper_cb_info
.current_operation
= QCOW2_CHANGING_REFCOUNT_ORDER
;
3188 ret
= qcow2_change_refcount_order(bs
, refcount_order
,
3189 &qcow2_amend_helper_cb
,
3190 &helper_cb_info
, &local_error
);
3192 error_report_err(local_error
);
3197 if (backing_file
|| backing_format
) {
3198 ret
= qcow2_change_backing_file(bs
,
3199 backing_file
?: s
->image_backing_file
,
3200 backing_format
?: s
->image_backing_format
);
3206 if (s
->use_lazy_refcounts
!= lazy_refcounts
) {
3207 if (lazy_refcounts
) {
3208 if (new_version
< 3) {
3209 error_report("Lazy refcounts only supported with compatibility "
3210 "level 1.1 and above (use compat=1.1 or greater)");
3213 s
->compatible_features
|= QCOW2_COMPAT_LAZY_REFCOUNTS
;
3214 ret
= qcow2_update_header(bs
);
3216 s
->compatible_features
&= ~QCOW2_COMPAT_LAZY_REFCOUNTS
;
3219 s
->use_lazy_refcounts
= true;
3221 /* make image clean first */
3222 ret
= qcow2_mark_clean(bs
);
3226 /* now disallow lazy refcounts */
3227 s
->compatible_features
&= ~QCOW2_COMPAT_LAZY_REFCOUNTS
;
3228 ret
= qcow2_update_header(bs
);
3230 s
->compatible_features
|= QCOW2_COMPAT_LAZY_REFCOUNTS
;
3233 s
->use_lazy_refcounts
= false;
3238 ret
= bdrv_truncate(bs
, new_size
);
3244 /* Downgrade last (so unsupported features can be removed before) */
3245 if (new_version
< old_version
) {
3246 helper_cb_info
.current_operation
= QCOW2_DOWNGRADING
;
3247 ret
= qcow2_downgrade(bs
, new_version
, &qcow2_amend_helper_cb
,
3258 * If offset or size are negative, respectively, they will not be included in
3259 * the BLOCK_IMAGE_CORRUPTED event emitted.
3260 * fatal will be ignored for read-only BDS; corruptions found there will always
3261 * be considered non-fatal.
3263 void qcow2_signal_corruption(BlockDriverState
*bs
, bool fatal
, int64_t offset
,
3264 int64_t size
, const char *message_format
, ...)
3266 BDRVQcow2State
*s
= bs
->opaque
;
3267 const char *node_name
;
3271 fatal
= fatal
&& !bs
->read_only
;
3273 if (s
->signaled_corruption
&&
3274 (!fatal
|| (s
->incompatible_features
& QCOW2_INCOMPAT_CORRUPT
)))
3279 va_start(ap
, message_format
);
3280 message
= g_strdup_vprintf(message_format
, ap
);
3284 fprintf(stderr
, "qcow2: Marking image as corrupt: %s; further "
3285 "corruption events will be suppressed\n", message
);
3287 fprintf(stderr
, "qcow2: Image is corrupt: %s; further non-fatal "
3288 "corruption events will be suppressed\n", message
);
3291 node_name
= bdrv_get_node_name(bs
);
3292 qapi_event_send_block_image_corrupted(bdrv_get_device_name(bs
),
3293 *node_name
!= '\0', node_name
,
3294 message
, offset
>= 0, offset
,
3296 fatal
, &error_abort
);
3300 qcow2_mark_corrupt(bs
);
3301 bs
->drv
= NULL
; /* make BDS unusable */
3304 s
->signaled_corruption
= true;
3307 static QemuOptsList qcow2_create_opts
= {
3308 .name
= "qcow2-create-opts",
3309 .head
= QTAILQ_HEAD_INITIALIZER(qcow2_create_opts
.head
),
3312 .name
= BLOCK_OPT_SIZE
,
3313 .type
= QEMU_OPT_SIZE
,
3314 .help
= "Virtual disk size"
3317 .name
= BLOCK_OPT_COMPAT_LEVEL
,
3318 .type
= QEMU_OPT_STRING
,
3319 .help
= "Compatibility level (0.10 or 1.1)"
3322 .name
= BLOCK_OPT_BACKING_FILE
,
3323 .type
= QEMU_OPT_STRING
,
3324 .help
= "File name of a base image"
3327 .name
= BLOCK_OPT_BACKING_FMT
,
3328 .type
= QEMU_OPT_STRING
,
3329 .help
= "Image format of the base image"
3332 .name
= BLOCK_OPT_ENCRYPT
,
3333 .type
= QEMU_OPT_BOOL
,
3334 .help
= "Encrypt the image",
3335 .def_value_str
= "off"
3338 .name
= BLOCK_OPT_CLUSTER_SIZE
,
3339 .type
= QEMU_OPT_SIZE
,
3340 .help
= "qcow2 cluster size",
3341 .def_value_str
= stringify(DEFAULT_CLUSTER_SIZE
)
3344 .name
= BLOCK_OPT_PREALLOC
,
3345 .type
= QEMU_OPT_STRING
,
3346 .help
= "Preallocation mode (allowed values: off, metadata, "
3350 .name
= BLOCK_OPT_LAZY_REFCOUNTS
,
3351 .type
= QEMU_OPT_BOOL
,
3352 .help
= "Postpone refcount updates",
3353 .def_value_str
= "off"
3356 .name
= BLOCK_OPT_REFCOUNT_BITS
,
3357 .type
= QEMU_OPT_NUMBER
,
3358 .help
= "Width of a reference count entry in bits",
3359 .def_value_str
= "16"
3361 { /* end of list */ }
3365 BlockDriver bdrv_qcow2
= {
3366 .format_name
= "qcow2",
3367 .instance_size
= sizeof(BDRVQcow2State
),
3368 .bdrv_probe
= qcow2_probe
,
3369 .bdrv_open
= qcow2_open
,
3370 .bdrv_close
= qcow2_close
,
3371 .bdrv_reopen_prepare
= qcow2_reopen_prepare
,
3372 .bdrv_reopen_commit
= qcow2_reopen_commit
,
3373 .bdrv_reopen_abort
= qcow2_reopen_abort
,
3374 .bdrv_join_options
= qcow2_join_options
,
3375 .bdrv_create
= qcow2_create
,
3376 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
3377 .bdrv_co_get_block_status
= qcow2_co_get_block_status
,
3378 .bdrv_set_key
= qcow2_set_key
,
3380 .bdrv_co_readv
= qcow2_co_readv
,
3381 .bdrv_co_writev
= qcow2_co_writev
,
3382 .bdrv_co_flush_to_os
= qcow2_co_flush_to_os
,
3384 .bdrv_co_write_zeroes
= qcow2_co_write_zeroes
,
3385 .bdrv_co_discard
= qcow2_co_discard
,
3386 .bdrv_truncate
= qcow2_truncate
,
3387 .bdrv_write_compressed
= qcow2_write_compressed
,
3388 .bdrv_make_empty
= qcow2_make_empty
,
3390 .bdrv_snapshot_create
= qcow2_snapshot_create
,
3391 .bdrv_snapshot_goto
= qcow2_snapshot_goto
,
3392 .bdrv_snapshot_delete
= qcow2_snapshot_delete
,
3393 .bdrv_snapshot_list
= qcow2_snapshot_list
,
3394 .bdrv_snapshot_load_tmp
= qcow2_snapshot_load_tmp
,
3395 .bdrv_get_info
= qcow2_get_info
,
3396 .bdrv_get_specific_info
= qcow2_get_specific_info
,
3398 .bdrv_save_vmstate
= qcow2_save_vmstate
,
3399 .bdrv_load_vmstate
= qcow2_load_vmstate
,
3401 .supports_backing
= true,
3402 .bdrv_change_backing_file
= qcow2_change_backing_file
,
3404 .bdrv_refresh_limits
= qcow2_refresh_limits
,
3405 .bdrv_invalidate_cache
= qcow2_invalidate_cache
,
3406 .bdrv_inactivate
= qcow2_inactivate
,
3408 .create_opts
= &qcow2_create_opts
,
3409 .bdrv_check
= qcow2_check
,
3410 .bdrv_amend_options
= qcow2_amend_options
,
3412 .bdrv_detach_aio_context
= qcow2_detach_aio_context
,
3413 .bdrv_attach_aio_context
= qcow2_attach_aio_context
,
3416 static void bdrv_qcow2_init(void)
3418 bdrv_register(&bdrv_qcow2
);
3421 block_init(bdrv_qcow2_init
);