2 * Block driver for the QCOW version 2 format
4 * Copyright (c) 2004-2006 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 #include "qemu-common.h"
25 #include "block/block_int.h"
26 #include "qemu/module.h"
28 #include "block/qcow2.h"
29 #include "qemu/error-report.h"
30 #include "qapi/qmp/qerror.h"
31 #include "qapi/qmp/qbool.h"
32 #include "qapi/util.h"
33 #include "qapi/qmp/types.h"
34 #include "qapi-event.h"
36 #include "qemu/option_int.h"
39 Differences with QCOW:
41 - Support for multiple incremental snapshots.
42 - Memory management by reference counts.
43 - Clusters which have a reference count of one have the bit
44 QCOW_OFLAG_COPIED to optimize write performance.
45 - Size of compressed clusters is stored in sectors to reduce bit usage
46 in the cluster offsets.
47 - Support for storing additional data (such as the VM state) in the
49 - If a backing store is used, the cluster size is not constrained
50 (could be backported to QCOW).
51 - L2 tables have always a size of one cluster.
58 } QEMU_PACKED QCowExtension
;
60 #define QCOW2_EXT_MAGIC_END 0
61 #define QCOW2_EXT_MAGIC_BACKING_FORMAT 0xE2792ACA
62 #define QCOW2_EXT_MAGIC_FEATURE_TABLE 0x6803f857
64 static int qcow2_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
66 const QCowHeader
*cow_header
= (const void *)buf
;
68 if (buf_size
>= sizeof(QCowHeader
) &&
69 be32_to_cpu(cow_header
->magic
) == QCOW_MAGIC
&&
70 be32_to_cpu(cow_header
->version
) >= 2)
78 * read qcow2 extension and fill bs
79 * start reading from start_offset
80 * finish reading upon magic of value 0 or when end_offset reached
81 * unknown magic is skipped (future extension this version knows nothing about)
82 * return 0 upon success, non-0 otherwise
84 static int qcow2_read_extensions(BlockDriverState
*bs
, uint64_t start_offset
,
85 uint64_t end_offset
, void **p_feature_table
,
88 BDRVQcow2State
*s
= bs
->opaque
;
94 printf("qcow2_read_extensions: start=%ld end=%ld\n", start_offset
, end_offset
);
96 offset
= start_offset
;
97 while (offset
< end_offset
) {
101 if (offset
> s
->cluster_size
)
102 printf("qcow2_read_extension: suspicious offset %lu\n", offset
);
104 printf("attempting to read extended header in offset %lu\n", offset
);
107 ret
= bdrv_pread(bs
->file
, offset
, &ext
, sizeof(ext
));
109 error_setg_errno(errp
, -ret
, "qcow2_read_extension: ERROR: "
110 "pread fail from offset %" PRIu64
, offset
);
113 be32_to_cpus(&ext
.magic
);
114 be32_to_cpus(&ext
.len
);
115 offset
+= sizeof(ext
);
117 printf("ext.magic = 0x%x\n", ext
.magic
);
119 if (offset
> end_offset
|| ext
.len
> end_offset
- offset
) {
120 error_setg(errp
, "Header extension too large");
125 case QCOW2_EXT_MAGIC_END
:
128 case QCOW2_EXT_MAGIC_BACKING_FORMAT
:
129 if (ext
.len
>= sizeof(bs
->backing_format
)) {
130 error_setg(errp
, "ERROR: ext_backing_format: len=%" PRIu32
131 " too large (>=%zu)", ext
.len
,
132 sizeof(bs
->backing_format
));
135 ret
= bdrv_pread(bs
->file
, offset
, bs
->backing_format
, ext
.len
);
137 error_setg_errno(errp
, -ret
, "ERROR: ext_backing_format: "
138 "Could not read format name");
141 bs
->backing_format
[ext
.len
] = '\0';
142 s
->image_backing_format
= g_strdup(bs
->backing_format
);
144 printf("Qcow2: Got format extension %s\n", bs
->backing_format
);
148 case QCOW2_EXT_MAGIC_FEATURE_TABLE
:
149 if (p_feature_table
!= NULL
) {
150 void* feature_table
= g_malloc0(ext
.len
+ 2 * sizeof(Qcow2Feature
));
151 ret
= bdrv_pread(bs
->file
, offset
, feature_table
, ext
.len
);
153 error_setg_errno(errp
, -ret
, "ERROR: ext_feature_table: "
154 "Could not read table");
158 *p_feature_table
= feature_table
;
163 /* unknown magic - save it in case we need to rewrite the header */
165 Qcow2UnknownHeaderExtension
*uext
;
167 uext
= g_malloc0(sizeof(*uext
) + ext
.len
);
168 uext
->magic
= ext
.magic
;
170 QLIST_INSERT_HEAD(&s
->unknown_header_ext
, uext
, next
);
172 ret
= bdrv_pread(bs
->file
, offset
, uext
->data
, uext
->len
);
174 error_setg_errno(errp
, -ret
, "ERROR: unknown extension: "
175 "Could not read data");
182 offset
+= ((ext
.len
+ 7) & ~7);
188 static void cleanup_unknown_header_ext(BlockDriverState
*bs
)
190 BDRVQcow2State
*s
= bs
->opaque
;
191 Qcow2UnknownHeaderExtension
*uext
, *next
;
193 QLIST_FOREACH_SAFE(uext
, &s
->unknown_header_ext
, next
, next
) {
194 QLIST_REMOVE(uext
, next
);
199 static void GCC_FMT_ATTR(3, 4) report_unsupported(BlockDriverState
*bs
,
200 Error
**errp
, const char *fmt
, ...)
206 vsnprintf(msg
, sizeof(msg
), fmt
, ap
);
209 error_setg(errp
, QERR_UNKNOWN_BLOCK_FORMAT_FEATURE
,
210 bdrv_get_device_or_node_name(bs
), "qcow2", msg
);
213 static void report_unsupported_feature(BlockDriverState
*bs
,
214 Error
**errp
, Qcow2Feature
*table
, uint64_t mask
)
216 char *features
= g_strdup("");
219 while (table
&& table
->name
[0] != '\0') {
220 if (table
->type
== QCOW2_FEAT_TYPE_INCOMPATIBLE
) {
221 if (mask
& (1ULL << table
->bit
)) {
223 features
= g_strdup_printf("%s%s%.46s", old
, *old
? ", " : "",
226 mask
&= ~(1ULL << table
->bit
);
234 features
= g_strdup_printf("%s%sUnknown incompatible feature: %" PRIx64
,
235 old
, *old
? ", " : "", mask
);
239 report_unsupported(bs
, errp
, "%s", features
);
244 * Sets the dirty bit and flushes afterwards if necessary.
246 * The incompatible_features bit is only set if the image file header was
247 * updated successfully. Therefore it is not required to check the return
248 * value of this function.
250 int qcow2_mark_dirty(BlockDriverState
*bs
)
252 BDRVQcow2State
*s
= bs
->opaque
;
256 assert(s
->qcow_version
>= 3);
258 if (s
->incompatible_features
& QCOW2_INCOMPAT_DIRTY
) {
259 return 0; /* already dirty */
262 val
= cpu_to_be64(s
->incompatible_features
| QCOW2_INCOMPAT_DIRTY
);
263 ret
= bdrv_pwrite(bs
->file
, offsetof(QCowHeader
, incompatible_features
),
268 ret
= bdrv_flush(bs
->file
);
273 /* Only treat image as dirty if the header was updated successfully */
274 s
->incompatible_features
|= QCOW2_INCOMPAT_DIRTY
;
279 * Clears the dirty bit and flushes before if necessary. Only call this
280 * function when there are no pending requests, it does not guard against
281 * concurrent requests dirtying the image.
283 static int qcow2_mark_clean(BlockDriverState
*bs
)
285 BDRVQcow2State
*s
= bs
->opaque
;
287 if (s
->incompatible_features
& QCOW2_INCOMPAT_DIRTY
) {
290 s
->incompatible_features
&= ~QCOW2_INCOMPAT_DIRTY
;
292 ret
= bdrv_flush(bs
);
297 return qcow2_update_header(bs
);
303 * Marks the image as corrupt.
305 int qcow2_mark_corrupt(BlockDriverState
*bs
)
307 BDRVQcow2State
*s
= bs
->opaque
;
309 s
->incompatible_features
|= QCOW2_INCOMPAT_CORRUPT
;
310 return qcow2_update_header(bs
);
314 * Marks the image as consistent, i.e., unsets the corrupt bit, and flushes
315 * before if necessary.
317 int qcow2_mark_consistent(BlockDriverState
*bs
)
319 BDRVQcow2State
*s
= bs
->opaque
;
321 if (s
->incompatible_features
& QCOW2_INCOMPAT_CORRUPT
) {
322 int ret
= bdrv_flush(bs
);
327 s
->incompatible_features
&= ~QCOW2_INCOMPAT_CORRUPT
;
328 return qcow2_update_header(bs
);
333 static int qcow2_check(BlockDriverState
*bs
, BdrvCheckResult
*result
,
336 int ret
= qcow2_check_refcounts(bs
, result
, fix
);
341 if (fix
&& result
->check_errors
== 0 && result
->corruptions
== 0) {
342 ret
= qcow2_mark_clean(bs
);
346 return qcow2_mark_consistent(bs
);
351 static int validate_table_offset(BlockDriverState
*bs
, uint64_t offset
,
352 uint64_t entries
, size_t entry_len
)
354 BDRVQcow2State
*s
= bs
->opaque
;
357 /* Use signed INT64_MAX as the maximum even for uint64_t header fields,
358 * because values will be passed to qemu functions taking int64_t. */
359 if (entries
> INT64_MAX
/ entry_len
) {
363 size
= entries
* entry_len
;
365 if (INT64_MAX
- size
< offset
) {
369 /* Tables must be cluster aligned */
370 if (offset
& (s
->cluster_size
- 1)) {
377 static QemuOptsList qcow2_runtime_opts
= {
379 .head
= QTAILQ_HEAD_INITIALIZER(qcow2_runtime_opts
.head
),
382 .name
= QCOW2_OPT_LAZY_REFCOUNTS
,
383 .type
= QEMU_OPT_BOOL
,
384 .help
= "Postpone refcount updates",
387 .name
= QCOW2_OPT_DISCARD_REQUEST
,
388 .type
= QEMU_OPT_BOOL
,
389 .help
= "Pass guest discard requests to the layer below",
392 .name
= QCOW2_OPT_DISCARD_SNAPSHOT
,
393 .type
= QEMU_OPT_BOOL
,
394 .help
= "Generate discard requests when snapshot related space "
398 .name
= QCOW2_OPT_DISCARD_OTHER
,
399 .type
= QEMU_OPT_BOOL
,
400 .help
= "Generate discard requests when other clusters are freed",
403 .name
= QCOW2_OPT_OVERLAP
,
404 .type
= QEMU_OPT_STRING
,
405 .help
= "Selects which overlap checks to perform from a range of "
406 "templates (none, constant, cached, all)",
409 .name
= QCOW2_OPT_OVERLAP_TEMPLATE
,
410 .type
= QEMU_OPT_STRING
,
411 .help
= "Selects which overlap checks to perform from a range of "
412 "templates (none, constant, cached, all)",
415 .name
= QCOW2_OPT_OVERLAP_MAIN_HEADER
,
416 .type
= QEMU_OPT_BOOL
,
417 .help
= "Check for unintended writes into the main qcow2 header",
420 .name
= QCOW2_OPT_OVERLAP_ACTIVE_L1
,
421 .type
= QEMU_OPT_BOOL
,
422 .help
= "Check for unintended writes into the active L1 table",
425 .name
= QCOW2_OPT_OVERLAP_ACTIVE_L2
,
426 .type
= QEMU_OPT_BOOL
,
427 .help
= "Check for unintended writes into an active L2 table",
430 .name
= QCOW2_OPT_OVERLAP_REFCOUNT_TABLE
,
431 .type
= QEMU_OPT_BOOL
,
432 .help
= "Check for unintended writes into the refcount table",
435 .name
= QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK
,
436 .type
= QEMU_OPT_BOOL
,
437 .help
= "Check for unintended writes into a refcount block",
440 .name
= QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE
,
441 .type
= QEMU_OPT_BOOL
,
442 .help
= "Check for unintended writes into the snapshot table",
445 .name
= QCOW2_OPT_OVERLAP_INACTIVE_L1
,
446 .type
= QEMU_OPT_BOOL
,
447 .help
= "Check for unintended writes into an inactive L1 table",
450 .name
= QCOW2_OPT_OVERLAP_INACTIVE_L2
,
451 .type
= QEMU_OPT_BOOL
,
452 .help
= "Check for unintended writes into an inactive L2 table",
455 .name
= QCOW2_OPT_CACHE_SIZE
,
456 .type
= QEMU_OPT_SIZE
,
457 .help
= "Maximum combined metadata (L2 tables and refcount blocks) "
461 .name
= QCOW2_OPT_L2_CACHE_SIZE
,
462 .type
= QEMU_OPT_SIZE
,
463 .help
= "Maximum L2 table cache size",
466 .name
= QCOW2_OPT_REFCOUNT_CACHE_SIZE
,
467 .type
= QEMU_OPT_SIZE
,
468 .help
= "Maximum refcount block cache size",
471 .name
= QCOW2_OPT_CACHE_CLEAN_INTERVAL
,
472 .type
= QEMU_OPT_NUMBER
,
473 .help
= "Clean unused cache entries after this time (in seconds)",
475 { /* end of list */ }
479 static const char *overlap_bool_option_names
[QCOW2_OL_MAX_BITNR
] = {
480 [QCOW2_OL_MAIN_HEADER_BITNR
] = QCOW2_OPT_OVERLAP_MAIN_HEADER
,
481 [QCOW2_OL_ACTIVE_L1_BITNR
] = QCOW2_OPT_OVERLAP_ACTIVE_L1
,
482 [QCOW2_OL_ACTIVE_L2_BITNR
] = QCOW2_OPT_OVERLAP_ACTIVE_L2
,
483 [QCOW2_OL_REFCOUNT_TABLE_BITNR
] = QCOW2_OPT_OVERLAP_REFCOUNT_TABLE
,
484 [QCOW2_OL_REFCOUNT_BLOCK_BITNR
] = QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK
,
485 [QCOW2_OL_SNAPSHOT_TABLE_BITNR
] = QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE
,
486 [QCOW2_OL_INACTIVE_L1_BITNR
] = QCOW2_OPT_OVERLAP_INACTIVE_L1
,
487 [QCOW2_OL_INACTIVE_L2_BITNR
] = QCOW2_OPT_OVERLAP_INACTIVE_L2
,
490 static void cache_clean_timer_cb(void *opaque
)
492 BlockDriverState
*bs
= opaque
;
493 BDRVQcow2State
*s
= bs
->opaque
;
494 qcow2_cache_clean_unused(bs
, s
->l2_table_cache
);
495 qcow2_cache_clean_unused(bs
, s
->refcount_block_cache
);
496 timer_mod(s
->cache_clean_timer
, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
) +
497 (int64_t) s
->cache_clean_interval
* 1000);
500 static void cache_clean_timer_init(BlockDriverState
*bs
, AioContext
*context
)
502 BDRVQcow2State
*s
= bs
->opaque
;
503 if (s
->cache_clean_interval
> 0) {
504 s
->cache_clean_timer
= aio_timer_new(context
, QEMU_CLOCK_VIRTUAL
,
505 SCALE_MS
, cache_clean_timer_cb
,
507 timer_mod(s
->cache_clean_timer
, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
) +
508 (int64_t) s
->cache_clean_interval
* 1000);
512 static void cache_clean_timer_del(BlockDriverState
*bs
)
514 BDRVQcow2State
*s
= bs
->opaque
;
515 if (s
->cache_clean_timer
) {
516 timer_del(s
->cache_clean_timer
);
517 timer_free(s
->cache_clean_timer
);
518 s
->cache_clean_timer
= NULL
;
522 static void qcow2_detach_aio_context(BlockDriverState
*bs
)
524 cache_clean_timer_del(bs
);
527 static void qcow2_attach_aio_context(BlockDriverState
*bs
,
528 AioContext
*new_context
)
530 cache_clean_timer_init(bs
, new_context
);
533 static void read_cache_sizes(BlockDriverState
*bs
, QemuOpts
*opts
,
534 uint64_t *l2_cache_size
,
535 uint64_t *refcount_cache_size
, Error
**errp
)
537 BDRVQcow2State
*s
= bs
->opaque
;
538 uint64_t combined_cache_size
;
539 bool l2_cache_size_set
, refcount_cache_size_set
, combined_cache_size_set
;
541 combined_cache_size_set
= qemu_opt_get(opts
, QCOW2_OPT_CACHE_SIZE
);
542 l2_cache_size_set
= qemu_opt_get(opts
, QCOW2_OPT_L2_CACHE_SIZE
);
543 refcount_cache_size_set
= qemu_opt_get(opts
, QCOW2_OPT_REFCOUNT_CACHE_SIZE
);
545 combined_cache_size
= qemu_opt_get_size(opts
, QCOW2_OPT_CACHE_SIZE
, 0);
546 *l2_cache_size
= qemu_opt_get_size(opts
, QCOW2_OPT_L2_CACHE_SIZE
, 0);
547 *refcount_cache_size
= qemu_opt_get_size(opts
,
548 QCOW2_OPT_REFCOUNT_CACHE_SIZE
, 0);
550 if (combined_cache_size_set
) {
551 if (l2_cache_size_set
&& refcount_cache_size_set
) {
552 error_setg(errp
, QCOW2_OPT_CACHE_SIZE
", " QCOW2_OPT_L2_CACHE_SIZE
553 " and " QCOW2_OPT_REFCOUNT_CACHE_SIZE
" may not be set "
556 } else if (*l2_cache_size
> combined_cache_size
) {
557 error_setg(errp
, QCOW2_OPT_L2_CACHE_SIZE
" may not exceed "
558 QCOW2_OPT_CACHE_SIZE
);
560 } else if (*refcount_cache_size
> combined_cache_size
) {
561 error_setg(errp
, QCOW2_OPT_REFCOUNT_CACHE_SIZE
" may not exceed "
562 QCOW2_OPT_CACHE_SIZE
);
566 if (l2_cache_size_set
) {
567 *refcount_cache_size
= combined_cache_size
- *l2_cache_size
;
568 } else if (refcount_cache_size_set
) {
569 *l2_cache_size
= combined_cache_size
- *refcount_cache_size
;
571 *refcount_cache_size
= combined_cache_size
572 / (DEFAULT_L2_REFCOUNT_SIZE_RATIO
+ 1);
573 *l2_cache_size
= combined_cache_size
- *refcount_cache_size
;
576 if (!l2_cache_size_set
&& !refcount_cache_size_set
) {
577 *l2_cache_size
= MAX(DEFAULT_L2_CACHE_BYTE_SIZE
,
578 (uint64_t)DEFAULT_L2_CACHE_CLUSTERS
580 *refcount_cache_size
= *l2_cache_size
581 / DEFAULT_L2_REFCOUNT_SIZE_RATIO
;
582 } else if (!l2_cache_size_set
) {
583 *l2_cache_size
= *refcount_cache_size
584 * DEFAULT_L2_REFCOUNT_SIZE_RATIO
;
585 } else if (!refcount_cache_size_set
) {
586 *refcount_cache_size
= *l2_cache_size
587 / DEFAULT_L2_REFCOUNT_SIZE_RATIO
;
592 static int qcow2_update_options(BlockDriverState
*bs
, QemuOpts
*opts
,
593 int flags
, Error
**errp
)
595 BDRVQcow2State
*s
= bs
->opaque
;
596 const char *opt_overlap_check
, *opt_overlap_check_template
;
597 int overlap_check_template
= 0;
601 s
->use_lazy_refcounts
= qemu_opt_get_bool(opts
, QCOW2_OPT_LAZY_REFCOUNTS
,
602 (s
->compatible_features
& QCOW2_COMPAT_LAZY_REFCOUNTS
));
604 s
->discard_passthrough
[QCOW2_DISCARD_NEVER
] = false;
605 s
->discard_passthrough
[QCOW2_DISCARD_ALWAYS
] = true;
606 s
->discard_passthrough
[QCOW2_DISCARD_REQUEST
] =
607 qemu_opt_get_bool(opts
, QCOW2_OPT_DISCARD_REQUEST
,
608 flags
& BDRV_O_UNMAP
);
609 s
->discard_passthrough
[QCOW2_DISCARD_SNAPSHOT
] =
610 qemu_opt_get_bool(opts
, QCOW2_OPT_DISCARD_SNAPSHOT
, true);
611 s
->discard_passthrough
[QCOW2_DISCARD_OTHER
] =
612 qemu_opt_get_bool(opts
, QCOW2_OPT_DISCARD_OTHER
, false);
614 opt_overlap_check
= qemu_opt_get(opts
, QCOW2_OPT_OVERLAP
);
615 opt_overlap_check_template
= qemu_opt_get(opts
, QCOW2_OPT_OVERLAP_TEMPLATE
);
616 if (opt_overlap_check_template
&& opt_overlap_check
&&
617 strcmp(opt_overlap_check_template
, opt_overlap_check
))
619 error_setg(errp
, "Conflicting values for qcow2 options '"
620 QCOW2_OPT_OVERLAP
"' ('%s') and '" QCOW2_OPT_OVERLAP_TEMPLATE
621 "' ('%s')", opt_overlap_check
, opt_overlap_check_template
);
625 if (!opt_overlap_check
) {
626 opt_overlap_check
= opt_overlap_check_template
?: "cached";
629 if (!strcmp(opt_overlap_check
, "none")) {
630 overlap_check_template
= 0;
631 } else if (!strcmp(opt_overlap_check
, "constant")) {
632 overlap_check_template
= QCOW2_OL_CONSTANT
;
633 } else if (!strcmp(opt_overlap_check
, "cached")) {
634 overlap_check_template
= QCOW2_OL_CACHED
;
635 } else if (!strcmp(opt_overlap_check
, "all")) {
636 overlap_check_template
= QCOW2_OL_ALL
;
638 error_setg(errp
, "Unsupported value '%s' for qcow2 option "
639 "'overlap-check'. Allowed are any of the following: "
640 "none, constant, cached, all", opt_overlap_check
);
645 s
->overlap_check
= 0;
646 for (i
= 0; i
< QCOW2_OL_MAX_BITNR
; i
++) {
647 /* overlap-check defines a template bitmask, but every flag may be
648 * overwritten through the associated boolean option */
650 qemu_opt_get_bool(opts
, overlap_bool_option_names
[i
],
651 overlap_check_template
& (1 << i
)) << i
;
654 if (s
->use_lazy_refcounts
&& s
->qcow_version
< 3) {
655 error_setg(errp
, "Lazy refcounts require a qcow2 image with at least "
656 "qemu 1.1 compatibility level");
666 static int qcow2_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
669 BDRVQcow2State
*s
= bs
->opaque
;
673 QemuOpts
*opts
= NULL
;
674 Error
*local_err
= NULL
;
676 uint64_t l1_vm_state_index
;
677 uint64_t l2_cache_size
, refcount_cache_size
;
678 uint64_t cache_clean_interval
;
680 ret
= bdrv_pread(bs
->file
, 0, &header
, sizeof(header
));
682 error_setg_errno(errp
, -ret
, "Could not read qcow2 header");
685 be32_to_cpus(&header
.magic
);
686 be32_to_cpus(&header
.version
);
687 be64_to_cpus(&header
.backing_file_offset
);
688 be32_to_cpus(&header
.backing_file_size
);
689 be64_to_cpus(&header
.size
);
690 be32_to_cpus(&header
.cluster_bits
);
691 be32_to_cpus(&header
.crypt_method
);
692 be64_to_cpus(&header
.l1_table_offset
);
693 be32_to_cpus(&header
.l1_size
);
694 be64_to_cpus(&header
.refcount_table_offset
);
695 be32_to_cpus(&header
.refcount_table_clusters
);
696 be64_to_cpus(&header
.snapshots_offset
);
697 be32_to_cpus(&header
.nb_snapshots
);
699 if (header
.magic
!= QCOW_MAGIC
) {
700 error_setg(errp
, "Image is not in qcow2 format");
704 if (header
.version
< 2 || header
.version
> 3) {
705 report_unsupported(bs
, errp
, "QCOW version %" PRIu32
, header
.version
);
710 s
->qcow_version
= header
.version
;
712 /* Initialise cluster size */
713 if (header
.cluster_bits
< MIN_CLUSTER_BITS
||
714 header
.cluster_bits
> MAX_CLUSTER_BITS
) {
715 error_setg(errp
, "Unsupported cluster size: 2^%" PRIu32
,
716 header
.cluster_bits
);
721 s
->cluster_bits
= header
.cluster_bits
;
722 s
->cluster_size
= 1 << s
->cluster_bits
;
723 s
->cluster_sectors
= 1 << (s
->cluster_bits
- 9);
725 /* Initialise version 3 header fields */
726 if (header
.version
== 2) {
727 header
.incompatible_features
= 0;
728 header
.compatible_features
= 0;
729 header
.autoclear_features
= 0;
730 header
.refcount_order
= 4;
731 header
.header_length
= 72;
733 be64_to_cpus(&header
.incompatible_features
);
734 be64_to_cpus(&header
.compatible_features
);
735 be64_to_cpus(&header
.autoclear_features
);
736 be32_to_cpus(&header
.refcount_order
);
737 be32_to_cpus(&header
.header_length
);
739 if (header
.header_length
< 104) {
740 error_setg(errp
, "qcow2 header too short");
746 if (header
.header_length
> s
->cluster_size
) {
747 error_setg(errp
, "qcow2 header exceeds cluster size");
752 if (header
.header_length
> sizeof(header
)) {
753 s
->unknown_header_fields_size
= header
.header_length
- sizeof(header
);
754 s
->unknown_header_fields
= g_malloc(s
->unknown_header_fields_size
);
755 ret
= bdrv_pread(bs
->file
, sizeof(header
), s
->unknown_header_fields
,
756 s
->unknown_header_fields_size
);
758 error_setg_errno(errp
, -ret
, "Could not read unknown qcow2 header "
764 if (header
.backing_file_offset
> s
->cluster_size
) {
765 error_setg(errp
, "Invalid backing file offset");
770 if (header
.backing_file_offset
) {
771 ext_end
= header
.backing_file_offset
;
773 ext_end
= 1 << header
.cluster_bits
;
776 /* Handle feature bits */
777 s
->incompatible_features
= header
.incompatible_features
;
778 s
->compatible_features
= header
.compatible_features
;
779 s
->autoclear_features
= header
.autoclear_features
;
781 if (s
->incompatible_features
& ~QCOW2_INCOMPAT_MASK
) {
782 void *feature_table
= NULL
;
783 qcow2_read_extensions(bs
, header
.header_length
, ext_end
,
784 &feature_table
, NULL
);
785 report_unsupported_feature(bs
, errp
, feature_table
,
786 s
->incompatible_features
&
787 ~QCOW2_INCOMPAT_MASK
);
789 g_free(feature_table
);
793 if (s
->incompatible_features
& QCOW2_INCOMPAT_CORRUPT
) {
794 /* Corrupt images may not be written to unless they are being repaired
796 if ((flags
& BDRV_O_RDWR
) && !(flags
& BDRV_O_CHECK
)) {
797 error_setg(errp
, "qcow2: Image is corrupt; cannot be opened "
804 /* Check support for various header values */
805 if (header
.refcount_order
> 6) {
806 error_setg(errp
, "Reference count entry width too large; may not "
811 s
->refcount_order
= header
.refcount_order
;
812 s
->refcount_bits
= 1 << s
->refcount_order
;
813 s
->refcount_max
= UINT64_C(1) << (s
->refcount_bits
- 1);
814 s
->refcount_max
+= s
->refcount_max
- 1;
816 if (header
.crypt_method
> QCOW_CRYPT_AES
) {
817 error_setg(errp
, "Unsupported encryption method: %" PRIu32
,
818 header
.crypt_method
);
822 if (!qcrypto_cipher_supports(QCRYPTO_CIPHER_ALG_AES_128
)) {
823 error_setg(errp
, "AES cipher not available");
827 s
->crypt_method_header
= header
.crypt_method
;
828 if (s
->crypt_method_header
) {
832 s
->l2_bits
= s
->cluster_bits
- 3; /* L2 is always one cluster */
833 s
->l2_size
= 1 << s
->l2_bits
;
834 /* 2^(s->refcount_order - 3) is the refcount width in bytes */
835 s
->refcount_block_bits
= s
->cluster_bits
- (s
->refcount_order
- 3);
836 s
->refcount_block_size
= 1 << s
->refcount_block_bits
;
837 bs
->total_sectors
= header
.size
/ 512;
838 s
->csize_shift
= (62 - (s
->cluster_bits
- 8));
839 s
->csize_mask
= (1 << (s
->cluster_bits
- 8)) - 1;
840 s
->cluster_offset_mask
= (1LL << s
->csize_shift
) - 1;
842 s
->refcount_table_offset
= header
.refcount_table_offset
;
843 s
->refcount_table_size
=
844 header
.refcount_table_clusters
<< (s
->cluster_bits
- 3);
846 if (header
.refcount_table_clusters
> qcow2_max_refcount_clusters(s
)) {
847 error_setg(errp
, "Reference count table too large");
852 ret
= validate_table_offset(bs
, s
->refcount_table_offset
,
853 s
->refcount_table_size
, sizeof(uint64_t));
855 error_setg(errp
, "Invalid reference count table offset");
859 /* Snapshot table offset/length */
860 if (header
.nb_snapshots
> QCOW_MAX_SNAPSHOTS
) {
861 error_setg(errp
, "Too many snapshots");
866 ret
= validate_table_offset(bs
, header
.snapshots_offset
,
868 sizeof(QCowSnapshotHeader
));
870 error_setg(errp
, "Invalid snapshot table offset");
874 /* read the level 1 table */
875 if (header
.l1_size
> QCOW_MAX_L1_SIZE
/ sizeof(uint64_t)) {
876 error_setg(errp
, "Active L1 table too large");
880 s
->l1_size
= header
.l1_size
;
882 l1_vm_state_index
= size_to_l1(s
, header
.size
);
883 if (l1_vm_state_index
> INT_MAX
) {
884 error_setg(errp
, "Image is too big");
888 s
->l1_vm_state_index
= l1_vm_state_index
;
890 /* the L1 table must contain at least enough entries to put
892 if (s
->l1_size
< s
->l1_vm_state_index
) {
893 error_setg(errp
, "L1 table is too small");
898 ret
= validate_table_offset(bs
, header
.l1_table_offset
,
899 header
.l1_size
, sizeof(uint64_t));
901 error_setg(errp
, "Invalid L1 table offset");
904 s
->l1_table_offset
= header
.l1_table_offset
;
907 if (s
->l1_size
> 0) {
908 s
->l1_table
= qemu_try_blockalign(bs
->file
,
909 align_offset(s
->l1_size
* sizeof(uint64_t), 512));
910 if (s
->l1_table
== NULL
) {
911 error_setg(errp
, "Could not allocate L1 table");
915 ret
= bdrv_pread(bs
->file
, s
->l1_table_offset
, s
->l1_table
,
916 s
->l1_size
* sizeof(uint64_t));
918 error_setg_errno(errp
, -ret
, "Could not read L1 table");
921 for(i
= 0;i
< s
->l1_size
; i
++) {
922 be64_to_cpus(&s
->l1_table
[i
]);
926 /* get L2 table/refcount block cache size from command line options */
927 opts
= qemu_opts_create(&qcow2_runtime_opts
, NULL
, 0, &error_abort
);
928 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
930 error_propagate(errp
, local_err
);
935 read_cache_sizes(bs
, opts
, &l2_cache_size
, &refcount_cache_size
,
938 error_propagate(errp
, local_err
);
943 l2_cache_size
/= s
->cluster_size
;
944 if (l2_cache_size
< MIN_L2_CACHE_SIZE
) {
945 l2_cache_size
= MIN_L2_CACHE_SIZE
;
947 if (l2_cache_size
> INT_MAX
) {
948 error_setg(errp
, "L2 cache size too big");
953 refcount_cache_size
/= s
->cluster_size
;
954 if (refcount_cache_size
< MIN_REFCOUNT_CACHE_SIZE
) {
955 refcount_cache_size
= MIN_REFCOUNT_CACHE_SIZE
;
957 if (refcount_cache_size
> INT_MAX
) {
958 error_setg(errp
, "Refcount cache size too big");
963 /* alloc L2 table/refcount block cache */
964 s
->l2_table_cache
= qcow2_cache_create(bs
, l2_cache_size
);
965 s
->refcount_block_cache
= qcow2_cache_create(bs
, refcount_cache_size
);
966 if (s
->l2_table_cache
== NULL
|| s
->refcount_block_cache
== NULL
) {
967 error_setg(errp
, "Could not allocate metadata caches");
972 cache_clean_interval
=
973 qemu_opt_get_number(opts
, QCOW2_OPT_CACHE_CLEAN_INTERVAL
, 0);
974 if (cache_clean_interval
> UINT_MAX
) {
975 error_setg(errp
, "Cache clean interval too big");
979 s
->cache_clean_interval
= cache_clean_interval
;
980 cache_clean_timer_init(bs
, bdrv_get_aio_context(bs
));
982 /* Enable lazy_refcounts according to image and command line options */
983 ret
= qcow2_update_options(bs
, opts
, flags
, errp
);
991 s
->cluster_cache
= g_malloc(s
->cluster_size
);
992 /* one more sector for decompressed data alignment */
993 s
->cluster_data
= qemu_try_blockalign(bs
->file
, QCOW_MAX_CRYPT_CLUSTERS
994 * s
->cluster_size
+ 512);
995 if (s
->cluster_data
== NULL
) {
996 error_setg(errp
, "Could not allocate temporary cluster buffer");
1001 s
->cluster_cache_offset
= -1;
1004 ret
= qcow2_refcount_init(bs
);
1006 error_setg_errno(errp
, -ret
, "Could not initialize refcount handling");
1010 QLIST_INIT(&s
->cluster_allocs
);
1011 QTAILQ_INIT(&s
->discards
);
1013 /* read qcow2 extensions */
1014 if (qcow2_read_extensions(bs
, header
.header_length
, ext_end
, NULL
,
1016 error_propagate(errp
, local_err
);
1021 /* read the backing file name */
1022 if (header
.backing_file_offset
!= 0) {
1023 len
= header
.backing_file_size
;
1024 if (len
> MIN(1023, s
->cluster_size
- header
.backing_file_offset
) ||
1025 len
>= sizeof(bs
->backing_file
)) {
1026 error_setg(errp
, "Backing file name too long");
1030 ret
= bdrv_pread(bs
->file
, header
.backing_file_offset
,
1031 bs
->backing_file
, len
);
1033 error_setg_errno(errp
, -ret
, "Could not read backing file name");
1036 bs
->backing_file
[len
] = '\0';
1037 s
->image_backing_file
= g_strdup(bs
->backing_file
);
1040 /* Internal snapshots */
1041 s
->snapshots_offset
= header
.snapshots_offset
;
1042 s
->nb_snapshots
= header
.nb_snapshots
;
1044 ret
= qcow2_read_snapshots(bs
);
1046 error_setg_errno(errp
, -ret
, "Could not read snapshots");
1050 /* Clear unknown autoclear feature bits */
1051 if (!bs
->read_only
&& !(flags
& BDRV_O_INCOMING
) && s
->autoclear_features
) {
1052 s
->autoclear_features
= 0;
1053 ret
= qcow2_update_header(bs
);
1055 error_setg_errno(errp
, -ret
, "Could not update qcow2 header");
1060 /* Initialise locks */
1061 qemu_co_mutex_init(&s
->lock
);
1063 /* Repair image if dirty */
1064 if (!(flags
& (BDRV_O_CHECK
| BDRV_O_INCOMING
)) && !bs
->read_only
&&
1065 (s
->incompatible_features
& QCOW2_INCOMPAT_DIRTY
)) {
1066 BdrvCheckResult result
= {0};
1068 ret
= qcow2_check(bs
, &result
, BDRV_FIX_ERRORS
| BDRV_FIX_LEAKS
);
1070 error_setg_errno(errp
, -ret
, "Could not repair dirty image");
1077 BdrvCheckResult result
= {0};
1078 qcow2_check_refcounts(bs
, &result
, 0);
1084 qemu_opts_del(opts
);
1085 g_free(s
->unknown_header_fields
);
1086 cleanup_unknown_header_ext(bs
);
1087 qcow2_free_snapshots(bs
);
1088 qcow2_refcount_close(bs
);
1089 qemu_vfree(s
->l1_table
);
1090 /* else pre-write overlap checks in cache_destroy may crash */
1092 cache_clean_timer_del(bs
);
1093 if (s
->l2_table_cache
) {
1094 qcow2_cache_destroy(bs
, s
->l2_table_cache
);
1096 if (s
->refcount_block_cache
) {
1097 qcow2_cache_destroy(bs
, s
->refcount_block_cache
);
1099 g_free(s
->cluster_cache
);
1100 qemu_vfree(s
->cluster_data
);
1104 static void qcow2_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
1106 BDRVQcow2State
*s
= bs
->opaque
;
1108 bs
->bl
.write_zeroes_alignment
= s
->cluster_sectors
;
1111 static int qcow2_set_key(BlockDriverState
*bs
, const char *key
)
1113 BDRVQcow2State
*s
= bs
->opaque
;
1118 memset(keybuf
, 0, 16);
1122 /* XXX: we could compress the chars to 7 bits to increase
1124 for(i
= 0;i
< len
;i
++) {
1127 assert(bs
->encrypted
);
1129 qcrypto_cipher_free(s
->cipher
);
1130 s
->cipher
= qcrypto_cipher_new(
1131 QCRYPTO_CIPHER_ALG_AES_128
,
1132 QCRYPTO_CIPHER_MODE_CBC
,
1133 keybuf
, G_N_ELEMENTS(keybuf
),
1137 /* XXX would be nice if errors in this method could
1138 * be properly propagate to the caller. Would need
1139 * the bdrv_set_key() API signature to be fixed. */
1146 /* We have no actual commit/abort logic for qcow2, but we need to write out any
1147 * unwritten data if we reopen read-only. */
1148 static int qcow2_reopen_prepare(BDRVReopenState
*state
,
1149 BlockReopenQueue
*queue
, Error
**errp
)
1153 if ((state
->flags
& BDRV_O_RDWR
) == 0) {
1154 ret
= bdrv_flush(state
->bs
);
1159 ret
= qcow2_mark_clean(state
->bs
);
1168 static int64_t coroutine_fn
qcow2_co_get_block_status(BlockDriverState
*bs
,
1169 int64_t sector_num
, int nb_sectors
, int *pnum
)
1171 BDRVQcow2State
*s
= bs
->opaque
;
1172 uint64_t cluster_offset
;
1173 int index_in_cluster
, ret
;
1177 qemu_co_mutex_lock(&s
->lock
);
1178 ret
= qcow2_get_cluster_offset(bs
, sector_num
<< 9, pnum
, &cluster_offset
);
1179 qemu_co_mutex_unlock(&s
->lock
);
1184 if (cluster_offset
!= 0 && ret
!= QCOW2_CLUSTER_COMPRESSED
&&
1186 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
1187 cluster_offset
|= (index_in_cluster
<< BDRV_SECTOR_BITS
);
1188 status
|= BDRV_BLOCK_OFFSET_VALID
| cluster_offset
;
1190 if (ret
== QCOW2_CLUSTER_ZERO
) {
1191 status
|= BDRV_BLOCK_ZERO
;
1192 } else if (ret
!= QCOW2_CLUSTER_UNALLOCATED
) {
1193 status
|= BDRV_BLOCK_DATA
;
1198 /* handle reading after the end of the backing file */
1199 int qcow2_backing_read1(BlockDriverState
*bs
, QEMUIOVector
*qiov
,
1200 int64_t sector_num
, int nb_sectors
)
1203 if ((sector_num
+ nb_sectors
) <= bs
->total_sectors
)
1205 if (sector_num
>= bs
->total_sectors
)
1208 n1
= bs
->total_sectors
- sector_num
;
1210 qemu_iovec_memset(qiov
, 512 * n1
, 0, 512 * (nb_sectors
- n1
));
1215 static coroutine_fn
int qcow2_co_readv(BlockDriverState
*bs
, int64_t sector_num
,
1216 int remaining_sectors
, QEMUIOVector
*qiov
)
1218 BDRVQcow2State
*s
= bs
->opaque
;
1219 int index_in_cluster
, n1
;
1221 int cur_nr_sectors
; /* number of sectors in current iteration */
1222 uint64_t cluster_offset
= 0;
1223 uint64_t bytes_done
= 0;
1224 QEMUIOVector hd_qiov
;
1225 uint8_t *cluster_data
= NULL
;
1227 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
1229 qemu_co_mutex_lock(&s
->lock
);
1231 while (remaining_sectors
!= 0) {
1233 /* prepare next request */
1234 cur_nr_sectors
= remaining_sectors
;
1236 cur_nr_sectors
= MIN(cur_nr_sectors
,
1237 QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
);
1240 ret
= qcow2_get_cluster_offset(bs
, sector_num
<< 9,
1241 &cur_nr_sectors
, &cluster_offset
);
1246 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
1248 qemu_iovec_reset(&hd_qiov
);
1249 qemu_iovec_concat(&hd_qiov
, qiov
, bytes_done
,
1250 cur_nr_sectors
* 512);
1253 case QCOW2_CLUSTER_UNALLOCATED
:
1255 if (bs
->backing_hd
) {
1256 /* read from the base image */
1257 n1
= qcow2_backing_read1(bs
->backing_hd
, &hd_qiov
,
1258 sector_num
, cur_nr_sectors
);
1260 QEMUIOVector local_qiov
;
1262 qemu_iovec_init(&local_qiov
, hd_qiov
.niov
);
1263 qemu_iovec_concat(&local_qiov
, &hd_qiov
, 0,
1264 n1
* BDRV_SECTOR_SIZE
);
1266 BLKDBG_EVENT(bs
->file
, BLKDBG_READ_BACKING_AIO
);
1267 qemu_co_mutex_unlock(&s
->lock
);
1268 ret
= bdrv_co_readv(bs
->backing_hd
, sector_num
,
1270 qemu_co_mutex_lock(&s
->lock
);
1272 qemu_iovec_destroy(&local_qiov
);
1279 /* Note: in this case, no need to wait */
1280 qemu_iovec_memset(&hd_qiov
, 0, 0, 512 * cur_nr_sectors
);
1284 case QCOW2_CLUSTER_ZERO
:
1285 qemu_iovec_memset(&hd_qiov
, 0, 0, 512 * cur_nr_sectors
);
1288 case QCOW2_CLUSTER_COMPRESSED
:
1289 /* add AIO support for compressed blocks ? */
1290 ret
= qcow2_decompress_cluster(bs
, cluster_offset
);
1295 qemu_iovec_from_buf(&hd_qiov
, 0,
1296 s
->cluster_cache
+ index_in_cluster
* 512,
1297 512 * cur_nr_sectors
);
1300 case QCOW2_CLUSTER_NORMAL
:
1301 if ((cluster_offset
& 511) != 0) {
1306 if (bs
->encrypted
) {
1310 * For encrypted images, read everything into a temporary
1311 * contiguous buffer on which the AES functions can work.
1313 if (!cluster_data
) {
1315 qemu_try_blockalign(bs
->file
, QCOW_MAX_CRYPT_CLUSTERS
1317 if (cluster_data
== NULL
) {
1323 assert(cur_nr_sectors
<=
1324 QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
);
1325 qemu_iovec_reset(&hd_qiov
);
1326 qemu_iovec_add(&hd_qiov
, cluster_data
,
1327 512 * cur_nr_sectors
);
1330 BLKDBG_EVENT(bs
->file
, BLKDBG_READ_AIO
);
1331 qemu_co_mutex_unlock(&s
->lock
);
1332 ret
= bdrv_co_readv(bs
->file
,
1333 (cluster_offset
>> 9) + index_in_cluster
,
1334 cur_nr_sectors
, &hd_qiov
);
1335 qemu_co_mutex_lock(&s
->lock
);
1339 if (bs
->encrypted
) {
1342 if (qcow2_encrypt_sectors(s
, sector_num
, cluster_data
,
1343 cluster_data
, cur_nr_sectors
, false,
1349 qemu_iovec_from_buf(qiov
, bytes_done
,
1350 cluster_data
, 512 * cur_nr_sectors
);
1355 g_assert_not_reached();
1360 remaining_sectors
-= cur_nr_sectors
;
1361 sector_num
+= cur_nr_sectors
;
1362 bytes_done
+= cur_nr_sectors
* 512;
1367 qemu_co_mutex_unlock(&s
->lock
);
1369 qemu_iovec_destroy(&hd_qiov
);
1370 qemu_vfree(cluster_data
);
1375 static coroutine_fn
int qcow2_co_writev(BlockDriverState
*bs
,
1377 int remaining_sectors
,
1380 BDRVQcow2State
*s
= bs
->opaque
;
1381 int index_in_cluster
;
1383 int cur_nr_sectors
; /* number of sectors in current iteration */
1384 uint64_t cluster_offset
;
1385 QEMUIOVector hd_qiov
;
1386 uint64_t bytes_done
= 0;
1387 uint8_t *cluster_data
= NULL
;
1388 QCowL2Meta
*l2meta
= NULL
;
1390 trace_qcow2_writev_start_req(qemu_coroutine_self(), sector_num
,
1393 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
1395 s
->cluster_cache_offset
= -1; /* disable compressed cache */
1397 qemu_co_mutex_lock(&s
->lock
);
1399 while (remaining_sectors
!= 0) {
1403 trace_qcow2_writev_start_part(qemu_coroutine_self());
1404 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
1405 cur_nr_sectors
= remaining_sectors
;
1406 if (bs
->encrypted
&&
1408 QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
- index_in_cluster
) {
1410 QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
- index_in_cluster
;
1413 ret
= qcow2_alloc_cluster_offset(bs
, sector_num
<< 9,
1414 &cur_nr_sectors
, &cluster_offset
, &l2meta
);
1419 assert((cluster_offset
& 511) == 0);
1421 qemu_iovec_reset(&hd_qiov
);
1422 qemu_iovec_concat(&hd_qiov
, qiov
, bytes_done
,
1423 cur_nr_sectors
* 512);
1425 if (bs
->encrypted
) {
1428 if (!cluster_data
) {
1429 cluster_data
= qemu_try_blockalign(bs
->file
,
1430 QCOW_MAX_CRYPT_CLUSTERS
1432 if (cluster_data
== NULL
) {
1438 assert(hd_qiov
.size
<=
1439 QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_size
);
1440 qemu_iovec_to_buf(&hd_qiov
, 0, cluster_data
, hd_qiov
.size
);
1442 if (qcow2_encrypt_sectors(s
, sector_num
, cluster_data
,
1443 cluster_data
, cur_nr_sectors
,
1450 qemu_iovec_reset(&hd_qiov
);
1451 qemu_iovec_add(&hd_qiov
, cluster_data
,
1452 cur_nr_sectors
* 512);
1455 ret
= qcow2_pre_write_overlap_check(bs
, 0,
1456 cluster_offset
+ index_in_cluster
* BDRV_SECTOR_SIZE
,
1457 cur_nr_sectors
* BDRV_SECTOR_SIZE
);
1462 qemu_co_mutex_unlock(&s
->lock
);
1463 BLKDBG_EVENT(bs
->file
, BLKDBG_WRITE_AIO
);
1464 trace_qcow2_writev_data(qemu_coroutine_self(),
1465 (cluster_offset
>> 9) + index_in_cluster
);
1466 ret
= bdrv_co_writev(bs
->file
,
1467 (cluster_offset
>> 9) + index_in_cluster
,
1468 cur_nr_sectors
, &hd_qiov
);
1469 qemu_co_mutex_lock(&s
->lock
);
1474 while (l2meta
!= NULL
) {
1477 ret
= qcow2_alloc_cluster_link_l2(bs
, l2meta
);
1482 /* Take the request off the list of running requests */
1483 if (l2meta
->nb_clusters
!= 0) {
1484 QLIST_REMOVE(l2meta
, next_in_flight
);
1487 qemu_co_queue_restart_all(&l2meta
->dependent_requests
);
1489 next
= l2meta
->next
;
1494 remaining_sectors
-= cur_nr_sectors
;
1495 sector_num
+= cur_nr_sectors
;
1496 bytes_done
+= cur_nr_sectors
* 512;
1497 trace_qcow2_writev_done_part(qemu_coroutine_self(), cur_nr_sectors
);
1502 qemu_co_mutex_unlock(&s
->lock
);
1504 while (l2meta
!= NULL
) {
1507 if (l2meta
->nb_clusters
!= 0) {
1508 QLIST_REMOVE(l2meta
, next_in_flight
);
1510 qemu_co_queue_restart_all(&l2meta
->dependent_requests
);
1512 next
= l2meta
->next
;
1517 qemu_iovec_destroy(&hd_qiov
);
1518 qemu_vfree(cluster_data
);
1519 trace_qcow2_writev_done_req(qemu_coroutine_self(), ret
);
1524 static void qcow2_close(BlockDriverState
*bs
)
1526 BDRVQcow2State
*s
= bs
->opaque
;
1527 qemu_vfree(s
->l1_table
);
1528 /* else pre-write overlap checks in cache_destroy may crash */
1531 if (!(bs
->open_flags
& BDRV_O_INCOMING
)) {
1534 ret1
= qcow2_cache_flush(bs
, s
->l2_table_cache
);
1535 ret2
= qcow2_cache_flush(bs
, s
->refcount_block_cache
);
1538 error_report("Failed to flush the L2 table cache: %s",
1542 error_report("Failed to flush the refcount block cache: %s",
1546 if (!ret1
&& !ret2
) {
1547 qcow2_mark_clean(bs
);
1551 cache_clean_timer_del(bs
);
1552 qcow2_cache_destroy(bs
, s
->l2_table_cache
);
1553 qcow2_cache_destroy(bs
, s
->refcount_block_cache
);
1555 qcrypto_cipher_free(s
->cipher
);
1558 g_free(s
->unknown_header_fields
);
1559 cleanup_unknown_header_ext(bs
);
1561 g_free(s
->image_backing_file
);
1562 g_free(s
->image_backing_format
);
1564 g_free(s
->cluster_cache
);
1565 qemu_vfree(s
->cluster_data
);
1566 qcow2_refcount_close(bs
);
1567 qcow2_free_snapshots(bs
);
1570 static void qcow2_invalidate_cache(BlockDriverState
*bs
, Error
**errp
)
1572 BDRVQcow2State
*s
= bs
->opaque
;
1573 int flags
= s
->flags
;
1574 QCryptoCipher
*cipher
= NULL
;
1576 Error
*local_err
= NULL
;
1580 * Backing files are read-only which makes all of their metadata immutable,
1581 * that means we don't have to worry about reopening them here.
1589 bdrv_invalidate_cache(bs
->file
, &local_err
);
1591 error_propagate(errp
, local_err
);
1595 memset(s
, 0, sizeof(BDRVQcow2State
));
1596 options
= qdict_clone_shallow(bs
->options
);
1598 ret
= qcow2_open(bs
, options
, flags
, &local_err
);
1601 error_setg(errp
, "Could not reopen qcow2 layer: %s",
1602 error_get_pretty(local_err
));
1603 error_free(local_err
);
1605 } else if (ret
< 0) {
1606 error_setg_errno(errp
, -ret
, "Could not reopen qcow2 layer");
1613 static size_t header_ext_add(char *buf
, uint32_t magic
, const void *s
,
1614 size_t len
, size_t buflen
)
1616 QCowExtension
*ext_backing_fmt
= (QCowExtension
*) buf
;
1617 size_t ext_len
= sizeof(QCowExtension
) + ((len
+ 7) & ~7);
1619 if (buflen
< ext_len
) {
1623 *ext_backing_fmt
= (QCowExtension
) {
1624 .magic
= cpu_to_be32(magic
),
1625 .len
= cpu_to_be32(len
),
1627 memcpy(buf
+ sizeof(QCowExtension
), s
, len
);
1633 * Updates the qcow2 header, including the variable length parts of it, i.e.
1634 * the backing file name and all extensions. qcow2 was not designed to allow
1635 * such changes, so if we run out of space (we can only use the first cluster)
1636 * this function may fail.
1638 * Returns 0 on success, -errno in error cases.
1640 int qcow2_update_header(BlockDriverState
*bs
)
1642 BDRVQcow2State
*s
= bs
->opaque
;
1645 size_t buflen
= s
->cluster_size
;
1647 uint64_t total_size
;
1648 uint32_t refcount_table_clusters
;
1649 size_t header_length
;
1650 Qcow2UnknownHeaderExtension
*uext
;
1652 buf
= qemu_blockalign(bs
, buflen
);
1654 /* Header structure */
1655 header
= (QCowHeader
*) buf
;
1657 if (buflen
< sizeof(*header
)) {
1662 header_length
= sizeof(*header
) + s
->unknown_header_fields_size
;
1663 total_size
= bs
->total_sectors
* BDRV_SECTOR_SIZE
;
1664 refcount_table_clusters
= s
->refcount_table_size
>> (s
->cluster_bits
- 3);
1666 *header
= (QCowHeader
) {
1667 /* Version 2 fields */
1668 .magic
= cpu_to_be32(QCOW_MAGIC
),
1669 .version
= cpu_to_be32(s
->qcow_version
),
1670 .backing_file_offset
= 0,
1671 .backing_file_size
= 0,
1672 .cluster_bits
= cpu_to_be32(s
->cluster_bits
),
1673 .size
= cpu_to_be64(total_size
),
1674 .crypt_method
= cpu_to_be32(s
->crypt_method_header
),
1675 .l1_size
= cpu_to_be32(s
->l1_size
),
1676 .l1_table_offset
= cpu_to_be64(s
->l1_table_offset
),
1677 .refcount_table_offset
= cpu_to_be64(s
->refcount_table_offset
),
1678 .refcount_table_clusters
= cpu_to_be32(refcount_table_clusters
),
1679 .nb_snapshots
= cpu_to_be32(s
->nb_snapshots
),
1680 .snapshots_offset
= cpu_to_be64(s
->snapshots_offset
),
1682 /* Version 3 fields */
1683 .incompatible_features
= cpu_to_be64(s
->incompatible_features
),
1684 .compatible_features
= cpu_to_be64(s
->compatible_features
),
1685 .autoclear_features
= cpu_to_be64(s
->autoclear_features
),
1686 .refcount_order
= cpu_to_be32(s
->refcount_order
),
1687 .header_length
= cpu_to_be32(header_length
),
1690 /* For older versions, write a shorter header */
1691 switch (s
->qcow_version
) {
1693 ret
= offsetof(QCowHeader
, incompatible_features
);
1696 ret
= sizeof(*header
);
1705 memset(buf
, 0, buflen
);
1707 /* Preserve any unknown field in the header */
1708 if (s
->unknown_header_fields_size
) {
1709 if (buflen
< s
->unknown_header_fields_size
) {
1714 memcpy(buf
, s
->unknown_header_fields
, s
->unknown_header_fields_size
);
1715 buf
+= s
->unknown_header_fields_size
;
1716 buflen
-= s
->unknown_header_fields_size
;
1719 /* Backing file format header extension */
1720 if (s
->image_backing_format
) {
1721 ret
= header_ext_add(buf
, QCOW2_EXT_MAGIC_BACKING_FORMAT
,
1722 s
->image_backing_format
,
1723 strlen(s
->image_backing_format
),
1734 Qcow2Feature features
[] = {
1736 .type
= QCOW2_FEAT_TYPE_INCOMPATIBLE
,
1737 .bit
= QCOW2_INCOMPAT_DIRTY_BITNR
,
1738 .name
= "dirty bit",
1741 .type
= QCOW2_FEAT_TYPE_INCOMPATIBLE
,
1742 .bit
= QCOW2_INCOMPAT_CORRUPT_BITNR
,
1743 .name
= "corrupt bit",
1746 .type
= QCOW2_FEAT_TYPE_COMPATIBLE
,
1747 .bit
= QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR
,
1748 .name
= "lazy refcounts",
1752 ret
= header_ext_add(buf
, QCOW2_EXT_MAGIC_FEATURE_TABLE
,
1753 features
, sizeof(features
), buflen
);
1760 /* Keep unknown header extensions */
1761 QLIST_FOREACH(uext
, &s
->unknown_header_ext
, next
) {
1762 ret
= header_ext_add(buf
, uext
->magic
, uext
->data
, uext
->len
, buflen
);
1771 /* End of header extensions */
1772 ret
= header_ext_add(buf
, QCOW2_EXT_MAGIC_END
, NULL
, 0, buflen
);
1780 /* Backing file name */
1781 if (s
->image_backing_file
) {
1782 size_t backing_file_len
= strlen(s
->image_backing_file
);
1784 if (buflen
< backing_file_len
) {
1789 /* Using strncpy is ok here, since buf is not NUL-terminated. */
1790 strncpy(buf
, s
->image_backing_file
, buflen
);
1792 header
->backing_file_offset
= cpu_to_be64(buf
- ((char*) header
));
1793 header
->backing_file_size
= cpu_to_be32(backing_file_len
);
1796 /* Write the new header */
1797 ret
= bdrv_pwrite(bs
->file
, 0, header
, s
->cluster_size
);
1808 static int qcow2_change_backing_file(BlockDriverState
*bs
,
1809 const char *backing_file
, const char *backing_fmt
)
1811 BDRVQcow2State
*s
= bs
->opaque
;
1813 pstrcpy(bs
->backing_file
, sizeof(bs
->backing_file
), backing_file
?: "");
1814 pstrcpy(bs
->backing_format
, sizeof(bs
->backing_format
), backing_fmt
?: "");
1816 g_free(s
->image_backing_file
);
1817 g_free(s
->image_backing_format
);
1819 s
->image_backing_file
= backing_file
? g_strdup(bs
->backing_file
) : NULL
;
1820 s
->image_backing_format
= backing_fmt
? g_strdup(bs
->backing_format
) : NULL
;
1822 return qcow2_update_header(bs
);
1825 static int preallocate(BlockDriverState
*bs
)
1827 uint64_t nb_sectors
;
1829 uint64_t host_offset
= 0;
1834 nb_sectors
= bdrv_nb_sectors(bs
);
1837 while (nb_sectors
) {
1838 num
= MIN(nb_sectors
, INT_MAX
>> BDRV_SECTOR_BITS
);
1839 ret
= qcow2_alloc_cluster_offset(bs
, offset
, &num
,
1840 &host_offset
, &meta
);
1846 QCowL2Meta
*next
= meta
->next
;
1848 ret
= qcow2_alloc_cluster_link_l2(bs
, meta
);
1850 qcow2_free_any_clusters(bs
, meta
->alloc_offset
,
1851 meta
->nb_clusters
, QCOW2_DISCARD_NEVER
);
1855 /* There are no dependent requests, but we need to remove our
1856 * request from the list of in-flight requests */
1857 QLIST_REMOVE(meta
, next_in_flight
);
1863 /* TODO Preallocate data if requested */
1866 offset
+= num
<< BDRV_SECTOR_BITS
;
1870 * It is expected that the image file is large enough to actually contain
1871 * all of the allocated clusters (otherwise we get failing reads after
1872 * EOF). Extend the image to the last allocated sector.
1874 if (host_offset
!= 0) {
1875 uint8_t buf
[BDRV_SECTOR_SIZE
];
1876 memset(buf
, 0, BDRV_SECTOR_SIZE
);
1877 ret
= bdrv_write(bs
->file
, (host_offset
>> BDRV_SECTOR_BITS
) + num
- 1,
1887 static int qcow2_create2(const char *filename
, int64_t total_size
,
1888 const char *backing_file
, const char *backing_format
,
1889 int flags
, size_t cluster_size
, PreallocMode prealloc
,
1890 QemuOpts
*opts
, int version
, int refcount_order
,
1896 /* Calculate cluster_bits */
1897 cluster_bits
= ctz32(cluster_size
);
1898 if (cluster_bits
< MIN_CLUSTER_BITS
|| cluster_bits
> MAX_CLUSTER_BITS
||
1899 (1 << cluster_bits
) != cluster_size
)
1901 error_setg(errp
, "Cluster size must be a power of two between %d and "
1902 "%dk", 1 << MIN_CLUSTER_BITS
, 1 << (MAX_CLUSTER_BITS
- 10));
1907 * Open the image file and write a minimal qcow2 header.
1909 * We keep things simple and start with a zero-sized image. We also
1910 * do without refcount blocks or a L1 table for now. We'll fix the
1911 * inconsistency later.
1913 * We do need a refcount table because growing the refcount table means
1914 * allocating two new refcount blocks - the seconds of which would be at
1915 * 2 GB for 64k clusters, and we don't want to have a 2 GB initial file
1916 * size for any qcow2 image.
1918 BlockDriverState
* bs
;
1920 uint64_t* refcount_table
;
1921 Error
*local_err
= NULL
;
1924 if (prealloc
== PREALLOC_MODE_FULL
|| prealloc
== PREALLOC_MODE_FALLOC
) {
1925 /* Note: The following calculation does not need to be exact; if it is a
1926 * bit off, either some bytes will be "leaked" (which is fine) or we
1927 * will need to increase the file size by some bytes (which is fine,
1928 * too, as long as the bulk is allocated here). Therefore, using
1929 * floating point arithmetic is fine. */
1930 int64_t meta_size
= 0;
1931 uint64_t nreftablee
, nrefblocke
, nl1e
, nl2e
;
1932 int64_t aligned_total_size
= align_offset(total_size
, cluster_size
);
1933 int refblock_bits
, refblock_size
;
1934 /* refcount entry size in bytes */
1935 double rces
= (1 << refcount_order
) / 8.;
1937 /* see qcow2_open() */
1938 refblock_bits
= cluster_bits
- (refcount_order
- 3);
1939 refblock_size
= 1 << refblock_bits
;
1941 /* header: 1 cluster */
1942 meta_size
+= cluster_size
;
1944 /* total size of L2 tables */
1945 nl2e
= aligned_total_size
/ cluster_size
;
1946 nl2e
= align_offset(nl2e
, cluster_size
/ sizeof(uint64_t));
1947 meta_size
+= nl2e
* sizeof(uint64_t);
1949 /* total size of L1 tables */
1950 nl1e
= nl2e
* sizeof(uint64_t) / cluster_size
;
1951 nl1e
= align_offset(nl1e
, cluster_size
/ sizeof(uint64_t));
1952 meta_size
+= nl1e
* sizeof(uint64_t);
1954 /* total size of refcount blocks
1956 * note: every host cluster is reference-counted, including metadata
1957 * (even refcount blocks are recursively included).
1959 * a = total_size (this is the guest disk size)
1960 * m = meta size not including refcount blocks and refcount tables
1962 * y1 = number of refcount blocks entries
1963 * y2 = meta size including everything
1964 * rces = refcount entry size in bytes
1967 * y2 = y1 * rces + y1 * rces * sizeof(u64) / c + m
1969 * y1 = (a + m) / (c - rces - rces * sizeof(u64) / c)
1971 nrefblocke
= (aligned_total_size
+ meta_size
+ cluster_size
)
1972 / (cluster_size
- rces
- rces
* sizeof(uint64_t)
1974 meta_size
+= DIV_ROUND_UP(nrefblocke
, refblock_size
) * cluster_size
;
1976 /* total size of refcount tables */
1977 nreftablee
= nrefblocke
/ refblock_size
;
1978 nreftablee
= align_offset(nreftablee
, cluster_size
/ sizeof(uint64_t));
1979 meta_size
+= nreftablee
* sizeof(uint64_t);
1981 qemu_opt_set_number(opts
, BLOCK_OPT_SIZE
,
1982 aligned_total_size
+ meta_size
, &error_abort
);
1983 qemu_opt_set(opts
, BLOCK_OPT_PREALLOC
, PreallocMode_lookup
[prealloc
],
1987 ret
= bdrv_create_file(filename
, opts
, &local_err
);
1989 error_propagate(errp
, local_err
);
1994 ret
= bdrv_open(&bs
, filename
, NULL
, NULL
, BDRV_O_RDWR
| BDRV_O_PROTOCOL
,
1997 error_propagate(errp
, local_err
);
2001 /* Write the header */
2002 QEMU_BUILD_BUG_ON((1 << MIN_CLUSTER_BITS
) < sizeof(*header
));
2003 header
= g_malloc0(cluster_size
);
2004 *header
= (QCowHeader
) {
2005 .magic
= cpu_to_be32(QCOW_MAGIC
),
2006 .version
= cpu_to_be32(version
),
2007 .cluster_bits
= cpu_to_be32(cluster_bits
),
2008 .size
= cpu_to_be64(0),
2009 .l1_table_offset
= cpu_to_be64(0),
2010 .l1_size
= cpu_to_be32(0),
2011 .refcount_table_offset
= cpu_to_be64(cluster_size
),
2012 .refcount_table_clusters
= cpu_to_be32(1),
2013 .refcount_order
= cpu_to_be32(refcount_order
),
2014 .header_length
= cpu_to_be32(sizeof(*header
)),
2017 if (flags
& BLOCK_FLAG_ENCRYPT
) {
2018 header
->crypt_method
= cpu_to_be32(QCOW_CRYPT_AES
);
2020 header
->crypt_method
= cpu_to_be32(QCOW_CRYPT_NONE
);
2023 if (flags
& BLOCK_FLAG_LAZY_REFCOUNTS
) {
2024 header
->compatible_features
|=
2025 cpu_to_be64(QCOW2_COMPAT_LAZY_REFCOUNTS
);
2028 ret
= bdrv_pwrite(bs
, 0, header
, cluster_size
);
2031 error_setg_errno(errp
, -ret
, "Could not write qcow2 header");
2035 /* Write a refcount table with one refcount block */
2036 refcount_table
= g_malloc0(2 * cluster_size
);
2037 refcount_table
[0] = cpu_to_be64(2 * cluster_size
);
2038 ret
= bdrv_pwrite(bs
, cluster_size
, refcount_table
, 2 * cluster_size
);
2039 g_free(refcount_table
);
2042 error_setg_errno(errp
, -ret
, "Could not write refcount table");
2050 * And now open the image and make it consistent first (i.e. increase the
2051 * refcount of the cluster that is occupied by the header and the refcount
2054 options
= qdict_new();
2055 qdict_put(options
, "driver", qstring_from_str("qcow2"));
2056 ret
= bdrv_open(&bs
, filename
, NULL
, options
,
2057 BDRV_O_RDWR
| BDRV_O_CACHE_WB
| BDRV_O_NO_FLUSH
,
2060 error_propagate(errp
, local_err
);
2064 ret
= qcow2_alloc_clusters(bs
, 3 * cluster_size
);
2066 error_setg_errno(errp
, -ret
, "Could not allocate clusters for qcow2 "
2067 "header and refcount table");
2070 } else if (ret
!= 0) {
2071 error_report("Huh, first cluster in empty image is already in use?");
2075 /* Okay, now that we have a valid image, let's give it the right size */
2076 ret
= bdrv_truncate(bs
, total_size
);
2078 error_setg_errno(errp
, -ret
, "Could not resize image");
2082 /* Want a backing file? There you go.*/
2084 ret
= bdrv_change_backing_file(bs
, backing_file
, backing_format
);
2086 error_setg_errno(errp
, -ret
, "Could not assign backing file '%s' "
2087 "with format '%s'", backing_file
, backing_format
);
2092 /* And if we're supposed to preallocate metadata, do that now */
2093 if (prealloc
!= PREALLOC_MODE_OFF
) {
2094 BDRVQcow2State
*s
= bs
->opaque
;
2095 qemu_co_mutex_lock(&s
->lock
);
2096 ret
= preallocate(bs
);
2097 qemu_co_mutex_unlock(&s
->lock
);
2099 error_setg_errno(errp
, -ret
, "Could not preallocate metadata");
2107 /* Reopen the image without BDRV_O_NO_FLUSH to flush it before returning */
2108 options
= qdict_new();
2109 qdict_put(options
, "driver", qstring_from_str("qcow2"));
2110 ret
= bdrv_open(&bs
, filename
, NULL
, options
,
2111 BDRV_O_RDWR
| BDRV_O_CACHE_WB
| BDRV_O_NO_BACKING
,
2114 error_propagate(errp
, local_err
);
2126 static int qcow2_create(const char *filename
, QemuOpts
*opts
, Error
**errp
)
2128 char *backing_file
= NULL
;
2129 char *backing_fmt
= NULL
;
2133 size_t cluster_size
= DEFAULT_CLUSTER_SIZE
;
2134 PreallocMode prealloc
;
2136 uint64_t refcount_bits
= 16;
2138 Error
*local_err
= NULL
;
2141 /* Read out options */
2142 size
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
2144 backing_file
= qemu_opt_get_del(opts
, BLOCK_OPT_BACKING_FILE
);
2145 backing_fmt
= qemu_opt_get_del(opts
, BLOCK_OPT_BACKING_FMT
);
2146 if (qemu_opt_get_bool_del(opts
, BLOCK_OPT_ENCRYPT
, false)) {
2147 flags
|= BLOCK_FLAG_ENCRYPT
;
2149 cluster_size
= qemu_opt_get_size_del(opts
, BLOCK_OPT_CLUSTER_SIZE
,
2150 DEFAULT_CLUSTER_SIZE
);
2151 buf
= qemu_opt_get_del(opts
, BLOCK_OPT_PREALLOC
);
2152 prealloc
= qapi_enum_parse(PreallocMode_lookup
, buf
,
2153 PREALLOC_MODE_MAX
, PREALLOC_MODE_OFF
,
2156 error_propagate(errp
, local_err
);
2161 buf
= qemu_opt_get_del(opts
, BLOCK_OPT_COMPAT_LEVEL
);
2163 /* keep the default */
2164 } else if (!strcmp(buf
, "0.10")) {
2166 } else if (!strcmp(buf
, "1.1")) {
2169 error_setg(errp
, "Invalid compatibility level: '%s'", buf
);
2174 if (qemu_opt_get_bool_del(opts
, BLOCK_OPT_LAZY_REFCOUNTS
, false)) {
2175 flags
|= BLOCK_FLAG_LAZY_REFCOUNTS
;
2178 if (backing_file
&& prealloc
!= PREALLOC_MODE_OFF
) {
2179 error_setg(errp
, "Backing file and preallocation cannot be used at "
2185 if (version
< 3 && (flags
& BLOCK_FLAG_LAZY_REFCOUNTS
)) {
2186 error_setg(errp
, "Lazy refcounts only supported with compatibility "
2187 "level 1.1 and above (use compat=1.1 or greater)");
2192 refcount_bits
= qemu_opt_get_number_del(opts
, BLOCK_OPT_REFCOUNT_BITS
,
2194 if (refcount_bits
> 64 || !is_power_of_2(refcount_bits
)) {
2195 error_setg(errp
, "Refcount width must be a power of two and may not "
2201 if (version
< 3 && refcount_bits
!= 16) {
2202 error_setg(errp
, "Different refcount widths than 16 bits require "
2203 "compatibility level 1.1 or above (use compat=1.1 or "
2209 refcount_order
= ctz32(refcount_bits
);
2211 ret
= qcow2_create2(filename
, size
, backing_file
, backing_fmt
, flags
,
2212 cluster_size
, prealloc
, opts
, version
, refcount_order
,
2215 error_propagate(errp
, local_err
);
2219 g_free(backing_file
);
2220 g_free(backing_fmt
);
2225 static coroutine_fn
int qcow2_co_write_zeroes(BlockDriverState
*bs
,
2226 int64_t sector_num
, int nb_sectors
, BdrvRequestFlags flags
)
2229 BDRVQcow2State
*s
= bs
->opaque
;
2231 /* Emulate misaligned zero writes */
2232 if (sector_num
% s
->cluster_sectors
|| nb_sectors
% s
->cluster_sectors
) {
2236 /* Whatever is left can use real zero clusters */
2237 qemu_co_mutex_lock(&s
->lock
);
2238 ret
= qcow2_zero_clusters(bs
, sector_num
<< BDRV_SECTOR_BITS
,
2240 qemu_co_mutex_unlock(&s
->lock
);
2245 static coroutine_fn
int qcow2_co_discard(BlockDriverState
*bs
,
2246 int64_t sector_num
, int nb_sectors
)
2249 BDRVQcow2State
*s
= bs
->opaque
;
2251 qemu_co_mutex_lock(&s
->lock
);
2252 ret
= qcow2_discard_clusters(bs
, sector_num
<< BDRV_SECTOR_BITS
,
2253 nb_sectors
, QCOW2_DISCARD_REQUEST
, false);
2254 qemu_co_mutex_unlock(&s
->lock
);
2258 static int qcow2_truncate(BlockDriverState
*bs
, int64_t offset
)
2260 BDRVQcow2State
*s
= bs
->opaque
;
2261 int64_t new_l1_size
;
2265 error_report("The new size must be a multiple of 512");
2269 /* cannot proceed if image has snapshots */
2270 if (s
->nb_snapshots
) {
2271 error_report("Can't resize an image which has snapshots");
2275 /* shrinking is currently not supported */
2276 if (offset
< bs
->total_sectors
* 512) {
2277 error_report("qcow2 doesn't support shrinking images yet");
2281 new_l1_size
= size_to_l1(s
, offset
);
2282 ret
= qcow2_grow_l1_table(bs
, new_l1_size
, true);
2287 /* write updated header.size */
2288 offset
= cpu_to_be64(offset
);
2289 ret
= bdrv_pwrite_sync(bs
->file
, offsetof(QCowHeader
, size
),
2290 &offset
, sizeof(uint64_t));
2295 s
->l1_vm_state_index
= new_l1_size
;
2299 /* XXX: put compressed sectors first, then all the cluster aligned
2300 tables to avoid losing bytes in alignment */
2301 static int qcow2_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
2302 const uint8_t *buf
, int nb_sectors
)
2304 BDRVQcow2State
*s
= bs
->opaque
;
2308 uint64_t cluster_offset
;
2310 if (nb_sectors
== 0) {
2311 /* align end of file to a sector boundary to ease reading with
2312 sector based I/Os */
2313 cluster_offset
= bdrv_getlength(bs
->file
);
2314 return bdrv_truncate(bs
->file
, cluster_offset
);
2317 if (nb_sectors
!= s
->cluster_sectors
) {
2320 /* Zero-pad last write if image size is not cluster aligned */
2321 if (sector_num
+ nb_sectors
== bs
->total_sectors
&&
2322 nb_sectors
< s
->cluster_sectors
) {
2323 uint8_t *pad_buf
= qemu_blockalign(bs
, s
->cluster_size
);
2324 memset(pad_buf
, 0, s
->cluster_size
);
2325 memcpy(pad_buf
, buf
, nb_sectors
* BDRV_SECTOR_SIZE
);
2326 ret
= qcow2_write_compressed(bs
, sector_num
,
2327 pad_buf
, s
->cluster_sectors
);
2328 qemu_vfree(pad_buf
);
2333 out_buf
= g_malloc(s
->cluster_size
+ (s
->cluster_size
/ 1000) + 128);
2335 /* best compression, small window, no zlib header */
2336 memset(&strm
, 0, sizeof(strm
));
2337 ret
= deflateInit2(&strm
, Z_DEFAULT_COMPRESSION
,
2339 9, Z_DEFAULT_STRATEGY
);
2345 strm
.avail_in
= s
->cluster_size
;
2346 strm
.next_in
= (uint8_t *)buf
;
2347 strm
.avail_out
= s
->cluster_size
;
2348 strm
.next_out
= out_buf
;
2350 ret
= deflate(&strm
, Z_FINISH
);
2351 if (ret
!= Z_STREAM_END
&& ret
!= Z_OK
) {
2356 out_len
= strm
.next_out
- out_buf
;
2360 if (ret
!= Z_STREAM_END
|| out_len
>= s
->cluster_size
) {
2361 /* could not compress: write normal cluster */
2362 ret
= bdrv_write(bs
, sector_num
, buf
, s
->cluster_sectors
);
2367 cluster_offset
= qcow2_alloc_compressed_cluster_offset(bs
,
2368 sector_num
<< 9, out_len
);
2369 if (!cluster_offset
) {
2373 cluster_offset
&= s
->cluster_offset_mask
;
2375 ret
= qcow2_pre_write_overlap_check(bs
, 0, cluster_offset
, out_len
);
2380 BLKDBG_EVENT(bs
->file
, BLKDBG_WRITE_COMPRESSED
);
2381 ret
= bdrv_pwrite(bs
->file
, cluster_offset
, out_buf
, out_len
);
2393 static int make_completely_empty(BlockDriverState
*bs
)
2395 BDRVQcow2State
*s
= bs
->opaque
;
2396 int ret
, l1_clusters
;
2398 uint64_t *new_reftable
= NULL
;
2399 uint64_t rt_entry
, l1_size2
;
2402 uint64_t reftable_offset
;
2403 uint32_t reftable_clusters
;
2404 } QEMU_PACKED l1_ofs_rt_ofs_cls
;
2406 ret
= qcow2_cache_empty(bs
, s
->l2_table_cache
);
2411 ret
= qcow2_cache_empty(bs
, s
->refcount_block_cache
);
2416 /* Refcounts will be broken utterly */
2417 ret
= qcow2_mark_dirty(bs
);
2422 BLKDBG_EVENT(bs
->file
, BLKDBG_L1_UPDATE
);
2424 l1_clusters
= DIV_ROUND_UP(s
->l1_size
, s
->cluster_size
/ sizeof(uint64_t));
2425 l1_size2
= (uint64_t)s
->l1_size
* sizeof(uint64_t);
2427 /* After this call, neither the in-memory nor the on-disk refcount
2428 * information accurately describe the actual references */
2430 ret
= bdrv_write_zeroes(bs
->file
, s
->l1_table_offset
/ BDRV_SECTOR_SIZE
,
2431 l1_clusters
* s
->cluster_sectors
, 0);
2433 goto fail_broken_refcounts
;
2435 memset(s
->l1_table
, 0, l1_size2
);
2437 BLKDBG_EVENT(bs
->file
, BLKDBG_EMPTY_IMAGE_PREPARE
);
2439 /* Overwrite enough clusters at the beginning of the sectors to place
2440 * the refcount table, a refcount block and the L1 table in; this may
2441 * overwrite parts of the existing refcount and L1 table, which is not
2442 * an issue because the dirty flag is set, complete data loss is in fact
2443 * desired and partial data loss is consequently fine as well */
2444 ret
= bdrv_write_zeroes(bs
->file
, s
->cluster_size
/ BDRV_SECTOR_SIZE
,
2445 (2 + l1_clusters
) * s
->cluster_size
/
2446 BDRV_SECTOR_SIZE
, 0);
2447 /* This call (even if it failed overall) may have overwritten on-disk
2448 * refcount structures; in that case, the in-memory refcount information
2449 * will probably differ from the on-disk information which makes the BDS
2452 goto fail_broken_refcounts
;
2455 BLKDBG_EVENT(bs
->file
, BLKDBG_L1_UPDATE
);
2456 BLKDBG_EVENT(bs
->file
, BLKDBG_REFTABLE_UPDATE
);
2458 /* "Create" an empty reftable (one cluster) directly after the image
2459 * header and an empty L1 table three clusters after the image header;
2460 * the cluster between those two will be used as the first refblock */
2461 cpu_to_be64w(&l1_ofs_rt_ofs_cls
.l1_offset
, 3 * s
->cluster_size
);
2462 cpu_to_be64w(&l1_ofs_rt_ofs_cls
.reftable_offset
, s
->cluster_size
);
2463 cpu_to_be32w(&l1_ofs_rt_ofs_cls
.reftable_clusters
, 1);
2464 ret
= bdrv_pwrite_sync(bs
->file
, offsetof(QCowHeader
, l1_table_offset
),
2465 &l1_ofs_rt_ofs_cls
, sizeof(l1_ofs_rt_ofs_cls
));
2467 goto fail_broken_refcounts
;
2470 s
->l1_table_offset
= 3 * s
->cluster_size
;
2472 new_reftable
= g_try_new0(uint64_t, s
->cluster_size
/ sizeof(uint64_t));
2473 if (!new_reftable
) {
2475 goto fail_broken_refcounts
;
2478 s
->refcount_table_offset
= s
->cluster_size
;
2479 s
->refcount_table_size
= s
->cluster_size
/ sizeof(uint64_t);
2481 g_free(s
->refcount_table
);
2482 s
->refcount_table
= new_reftable
;
2483 new_reftable
= NULL
;
2485 /* Now the in-memory refcount information again corresponds to the on-disk
2486 * information (reftable is empty and no refblocks (the refblock cache is
2487 * empty)); however, this means some clusters (e.g. the image header) are
2488 * referenced, but not refcounted, but the normal qcow2 code assumes that
2489 * the in-memory information is always correct */
2491 BLKDBG_EVENT(bs
->file
, BLKDBG_REFBLOCK_ALLOC
);
2493 /* Enter the first refblock into the reftable */
2494 rt_entry
= cpu_to_be64(2 * s
->cluster_size
);
2495 ret
= bdrv_pwrite_sync(bs
->file
, s
->cluster_size
,
2496 &rt_entry
, sizeof(rt_entry
));
2498 goto fail_broken_refcounts
;
2500 s
->refcount_table
[0] = 2 * s
->cluster_size
;
2502 s
->free_cluster_index
= 0;
2503 assert(3 + l1_clusters
<= s
->refcount_block_size
);
2504 offset
= qcow2_alloc_clusters(bs
, 3 * s
->cluster_size
+ l1_size2
);
2507 goto fail_broken_refcounts
;
2508 } else if (offset
> 0) {
2509 error_report("First cluster in emptied image is in use");
2513 /* Now finally the in-memory information corresponds to the on-disk
2514 * structures and is correct */
2515 ret
= qcow2_mark_clean(bs
);
2520 ret
= bdrv_truncate(bs
->file
, (3 + l1_clusters
) * s
->cluster_size
);
2527 fail_broken_refcounts
:
2528 /* The BDS is unusable at this point. If we wanted to make it usable, we
2529 * would have to call qcow2_refcount_close(), qcow2_refcount_init(),
2530 * qcow2_check_refcounts(), qcow2_refcount_close() and qcow2_refcount_init()
2531 * again. However, because the functions which could have caused this error
2532 * path to be taken are used by those functions as well, it's very likely
2533 * that that sequence will fail as well. Therefore, just eject the BDS. */
2537 g_free(new_reftable
);
2541 static int qcow2_make_empty(BlockDriverState
*bs
)
2543 BDRVQcow2State
*s
= bs
->opaque
;
2544 uint64_t start_sector
;
2545 int sector_step
= INT_MAX
/ BDRV_SECTOR_SIZE
;
2546 int l1_clusters
, ret
= 0;
2548 l1_clusters
= DIV_ROUND_UP(s
->l1_size
, s
->cluster_size
/ sizeof(uint64_t));
2550 if (s
->qcow_version
>= 3 && !s
->snapshots
&&
2551 3 + l1_clusters
<= s
->refcount_block_size
) {
2552 /* The following function only works for qcow2 v3 images (it requires
2553 * the dirty flag) and only as long as there are no snapshots (because
2554 * it completely empties the image). Furthermore, the L1 table and three
2555 * additional clusters (image header, refcount table, one refcount
2556 * block) have to fit inside one refcount block. */
2557 return make_completely_empty(bs
);
2560 /* This fallback code simply discards every active cluster; this is slow,
2561 * but works in all cases */
2562 for (start_sector
= 0; start_sector
< bs
->total_sectors
;
2563 start_sector
+= sector_step
)
2565 /* As this function is generally used after committing an external
2566 * snapshot, QCOW2_DISCARD_SNAPSHOT seems appropriate. Also, the
2567 * default action for this kind of discard is to pass the discard,
2568 * which will ideally result in an actually smaller image file, as
2569 * is probably desired. */
2570 ret
= qcow2_discard_clusters(bs
, start_sector
* BDRV_SECTOR_SIZE
,
2572 bs
->total_sectors
- start_sector
),
2573 QCOW2_DISCARD_SNAPSHOT
, true);
2582 static coroutine_fn
int qcow2_co_flush_to_os(BlockDriverState
*bs
)
2584 BDRVQcow2State
*s
= bs
->opaque
;
2587 qemu_co_mutex_lock(&s
->lock
);
2588 ret
= qcow2_cache_flush(bs
, s
->l2_table_cache
);
2590 qemu_co_mutex_unlock(&s
->lock
);
2594 if (qcow2_need_accurate_refcounts(s
)) {
2595 ret
= qcow2_cache_flush(bs
, s
->refcount_block_cache
);
2597 qemu_co_mutex_unlock(&s
->lock
);
2601 qemu_co_mutex_unlock(&s
->lock
);
2606 static int qcow2_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
2608 BDRVQcow2State
*s
= bs
->opaque
;
2609 bdi
->unallocated_blocks_are_zero
= true;
2610 bdi
->can_write_zeroes_with_unmap
= (s
->qcow_version
>= 3);
2611 bdi
->cluster_size
= s
->cluster_size
;
2612 bdi
->vm_state_offset
= qcow2_vm_state_offset(s
);
2616 static ImageInfoSpecific
*qcow2_get_specific_info(BlockDriverState
*bs
)
2618 BDRVQcow2State
*s
= bs
->opaque
;
2619 ImageInfoSpecific
*spec_info
= g_new(ImageInfoSpecific
, 1);
2621 *spec_info
= (ImageInfoSpecific
){
2622 .kind
= IMAGE_INFO_SPECIFIC_KIND_QCOW2
,
2624 .qcow2
= g_new(ImageInfoSpecificQCow2
, 1),
2627 if (s
->qcow_version
== 2) {
2628 *spec_info
->qcow2
= (ImageInfoSpecificQCow2
){
2629 .compat
= g_strdup("0.10"),
2630 .refcount_bits
= s
->refcount_bits
,
2632 } else if (s
->qcow_version
== 3) {
2633 *spec_info
->qcow2
= (ImageInfoSpecificQCow2
){
2634 .compat
= g_strdup("1.1"),
2635 .lazy_refcounts
= s
->compatible_features
&
2636 QCOW2_COMPAT_LAZY_REFCOUNTS
,
2637 .has_lazy_refcounts
= true,
2638 .corrupt
= s
->incompatible_features
&
2639 QCOW2_INCOMPAT_CORRUPT
,
2640 .has_corrupt
= true,
2641 .refcount_bits
= s
->refcount_bits
,
2649 static void dump_refcounts(BlockDriverState
*bs
)
2651 BDRVQcow2State
*s
= bs
->opaque
;
2652 int64_t nb_clusters
, k
, k1
, size
;
2655 size
= bdrv_getlength(bs
->file
);
2656 nb_clusters
= size_to_clusters(s
, size
);
2657 for(k
= 0; k
< nb_clusters
;) {
2659 refcount
= get_refcount(bs
, k
);
2661 while (k
< nb_clusters
&& get_refcount(bs
, k
) == refcount
)
2663 printf("%" PRId64
": refcount=%d nb=%" PRId64
"\n", k
, refcount
,
2669 static int qcow2_save_vmstate(BlockDriverState
*bs
, QEMUIOVector
*qiov
,
2672 BDRVQcow2State
*s
= bs
->opaque
;
2673 int64_t total_sectors
= bs
->total_sectors
;
2674 bool zero_beyond_eof
= bs
->zero_beyond_eof
;
2677 BLKDBG_EVENT(bs
->file
, BLKDBG_VMSTATE_SAVE
);
2678 bs
->zero_beyond_eof
= false;
2679 ret
= bdrv_pwritev(bs
, qcow2_vm_state_offset(s
) + pos
, qiov
);
2680 bs
->zero_beyond_eof
= zero_beyond_eof
;
2682 /* bdrv_co_do_writev will have increased the total_sectors value to include
2683 * the VM state - the VM state is however not an actual part of the block
2684 * device, therefore, we need to restore the old value. */
2685 bs
->total_sectors
= total_sectors
;
2690 static int qcow2_load_vmstate(BlockDriverState
*bs
, uint8_t *buf
,
2691 int64_t pos
, int size
)
2693 BDRVQcow2State
*s
= bs
->opaque
;
2694 bool zero_beyond_eof
= bs
->zero_beyond_eof
;
2697 BLKDBG_EVENT(bs
->file
, BLKDBG_VMSTATE_LOAD
);
2698 bs
->zero_beyond_eof
= false;
2699 ret
= bdrv_pread(bs
, qcow2_vm_state_offset(s
) + pos
, buf
, size
);
2700 bs
->zero_beyond_eof
= zero_beyond_eof
;
2706 * Downgrades an image's version. To achieve this, any incompatible features
2707 * have to be removed.
2709 static int qcow2_downgrade(BlockDriverState
*bs
, int target_version
,
2710 BlockDriverAmendStatusCB
*status_cb
)
2712 BDRVQcow2State
*s
= bs
->opaque
;
2713 int current_version
= s
->qcow_version
;
2716 if (target_version
== current_version
) {
2718 } else if (target_version
> current_version
) {
2720 } else if (target_version
!= 2) {
2724 if (s
->refcount_order
!= 4) {
2725 /* we would have to convert the image to a refcount_order == 4 image
2726 * here; however, since qemu (at the time of writing this) does not
2727 * support anything different than 4 anyway, there is no point in doing
2728 * so right now; however, we should error out (if qemu supports this in
2729 * the future and this code has not been adapted) */
2730 error_report("qcow2_downgrade: Image refcount orders other than 4 are "
2731 "currently not supported.");
2735 /* clear incompatible features */
2736 if (s
->incompatible_features
& QCOW2_INCOMPAT_DIRTY
) {
2737 ret
= qcow2_mark_clean(bs
);
2743 /* with QCOW2_INCOMPAT_CORRUPT, it is pretty much impossible to get here in
2744 * the first place; if that happens nonetheless, returning -ENOTSUP is the
2745 * best thing to do anyway */
2747 if (s
->incompatible_features
) {
2751 /* since we can ignore compatible features, we can set them to 0 as well */
2752 s
->compatible_features
= 0;
2753 /* if lazy refcounts have been used, they have already been fixed through
2754 * clearing the dirty flag */
2756 /* clearing autoclear features is trivial */
2757 s
->autoclear_features
= 0;
2759 ret
= qcow2_expand_zero_clusters(bs
, status_cb
);
2764 s
->qcow_version
= target_version
;
2765 ret
= qcow2_update_header(bs
);
2767 s
->qcow_version
= current_version
;
2773 static int qcow2_amend_options(BlockDriverState
*bs
, QemuOpts
*opts
,
2774 BlockDriverAmendStatusCB
*status_cb
)
2776 BDRVQcow2State
*s
= bs
->opaque
;
2777 int old_version
= s
->qcow_version
, new_version
= old_version
;
2778 uint64_t new_size
= 0;
2779 const char *backing_file
= NULL
, *backing_format
= NULL
;
2780 bool lazy_refcounts
= s
->use_lazy_refcounts
;
2781 const char *compat
= NULL
;
2782 uint64_t cluster_size
= s
->cluster_size
;
2785 QemuOptDesc
*desc
= opts
->list
->desc
;
2787 while (desc
&& desc
->name
) {
2788 if (!qemu_opt_find(opts
, desc
->name
)) {
2789 /* only change explicitly defined options */
2794 if (!strcmp(desc
->name
, BLOCK_OPT_COMPAT_LEVEL
)) {
2795 compat
= qemu_opt_get(opts
, BLOCK_OPT_COMPAT_LEVEL
);
2797 /* preserve default */
2798 } else if (!strcmp(compat
, "0.10")) {
2800 } else if (!strcmp(compat
, "1.1")) {
2803 fprintf(stderr
, "Unknown compatibility level %s.\n", compat
);
2806 } else if (!strcmp(desc
->name
, BLOCK_OPT_PREALLOC
)) {
2807 fprintf(stderr
, "Cannot change preallocation mode.\n");
2809 } else if (!strcmp(desc
->name
, BLOCK_OPT_SIZE
)) {
2810 new_size
= qemu_opt_get_size(opts
, BLOCK_OPT_SIZE
, 0);
2811 } else if (!strcmp(desc
->name
, BLOCK_OPT_BACKING_FILE
)) {
2812 backing_file
= qemu_opt_get(opts
, BLOCK_OPT_BACKING_FILE
);
2813 } else if (!strcmp(desc
->name
, BLOCK_OPT_BACKING_FMT
)) {
2814 backing_format
= qemu_opt_get(opts
, BLOCK_OPT_BACKING_FMT
);
2815 } else if (!strcmp(desc
->name
, BLOCK_OPT_ENCRYPT
)) {
2816 encrypt
= qemu_opt_get_bool(opts
, BLOCK_OPT_ENCRYPT
,
2819 if (encrypt
!= !!s
->cipher
) {
2820 fprintf(stderr
, "Changing the encryption flag is not "
2824 } else if (!strcmp(desc
->name
, BLOCK_OPT_CLUSTER_SIZE
)) {
2825 cluster_size
= qemu_opt_get_size(opts
, BLOCK_OPT_CLUSTER_SIZE
,
2827 if (cluster_size
!= s
->cluster_size
) {
2828 fprintf(stderr
, "Changing the cluster size is not "
2832 } else if (!strcmp(desc
->name
, BLOCK_OPT_LAZY_REFCOUNTS
)) {
2833 lazy_refcounts
= qemu_opt_get_bool(opts
, BLOCK_OPT_LAZY_REFCOUNTS
,
2835 } else if (!strcmp(desc
->name
, BLOCK_OPT_REFCOUNT_BITS
)) {
2836 error_report("Cannot change refcount entry width");
2839 /* if this assertion fails, this probably means a new option was
2840 * added without having it covered here */
2847 if (new_version
!= old_version
) {
2848 if (new_version
> old_version
) {
2850 s
->qcow_version
= new_version
;
2851 ret
= qcow2_update_header(bs
);
2853 s
->qcow_version
= old_version
;
2857 ret
= qcow2_downgrade(bs
, new_version
, status_cb
);
2864 if (backing_file
|| backing_format
) {
2865 ret
= qcow2_change_backing_file(bs
,
2866 backing_file
?: s
->image_backing_file
,
2867 backing_format
?: s
->image_backing_format
);
2873 if (s
->use_lazy_refcounts
!= lazy_refcounts
) {
2874 if (lazy_refcounts
) {
2875 if (s
->qcow_version
< 3) {
2876 fprintf(stderr
, "Lazy refcounts only supported with compatibility "
2877 "level 1.1 and above (use compat=1.1 or greater)\n");
2880 s
->compatible_features
|= QCOW2_COMPAT_LAZY_REFCOUNTS
;
2881 ret
= qcow2_update_header(bs
);
2883 s
->compatible_features
&= ~QCOW2_COMPAT_LAZY_REFCOUNTS
;
2886 s
->use_lazy_refcounts
= true;
2888 /* make image clean first */
2889 ret
= qcow2_mark_clean(bs
);
2893 /* now disallow lazy refcounts */
2894 s
->compatible_features
&= ~QCOW2_COMPAT_LAZY_REFCOUNTS
;
2895 ret
= qcow2_update_header(bs
);
2897 s
->compatible_features
|= QCOW2_COMPAT_LAZY_REFCOUNTS
;
2900 s
->use_lazy_refcounts
= false;
2905 ret
= bdrv_truncate(bs
, new_size
);
2915 * If offset or size are negative, respectively, they will not be included in
2916 * the BLOCK_IMAGE_CORRUPTED event emitted.
2917 * fatal will be ignored for read-only BDS; corruptions found there will always
2918 * be considered non-fatal.
2920 void qcow2_signal_corruption(BlockDriverState
*bs
, bool fatal
, int64_t offset
,
2921 int64_t size
, const char *message_format
, ...)
2923 BDRVQcow2State
*s
= bs
->opaque
;
2924 const char *node_name
;
2928 fatal
= fatal
&& !bs
->read_only
;
2930 if (s
->signaled_corruption
&&
2931 (!fatal
|| (s
->incompatible_features
& QCOW2_INCOMPAT_CORRUPT
)))
2936 va_start(ap
, message_format
);
2937 message
= g_strdup_vprintf(message_format
, ap
);
2941 fprintf(stderr
, "qcow2: Marking image as corrupt: %s; further "
2942 "corruption events will be suppressed\n", message
);
2944 fprintf(stderr
, "qcow2: Image is corrupt: %s; further non-fatal "
2945 "corruption events will be suppressed\n", message
);
2948 node_name
= bdrv_get_node_name(bs
);
2949 qapi_event_send_block_image_corrupted(bdrv_get_device_name(bs
),
2950 *node_name
!= '\0', node_name
,
2951 message
, offset
>= 0, offset
,
2953 fatal
, &error_abort
);
2957 qcow2_mark_corrupt(bs
);
2958 bs
->drv
= NULL
; /* make BDS unusable */
2961 s
->signaled_corruption
= true;
2964 static QemuOptsList qcow2_create_opts
= {
2965 .name
= "qcow2-create-opts",
2966 .head
= QTAILQ_HEAD_INITIALIZER(qcow2_create_opts
.head
),
2969 .name
= BLOCK_OPT_SIZE
,
2970 .type
= QEMU_OPT_SIZE
,
2971 .help
= "Virtual disk size"
2974 .name
= BLOCK_OPT_COMPAT_LEVEL
,
2975 .type
= QEMU_OPT_STRING
,
2976 .help
= "Compatibility level (0.10 or 1.1)"
2979 .name
= BLOCK_OPT_BACKING_FILE
,
2980 .type
= QEMU_OPT_STRING
,
2981 .help
= "File name of a base image"
2984 .name
= BLOCK_OPT_BACKING_FMT
,
2985 .type
= QEMU_OPT_STRING
,
2986 .help
= "Image format of the base image"
2989 .name
= BLOCK_OPT_ENCRYPT
,
2990 .type
= QEMU_OPT_BOOL
,
2991 .help
= "Encrypt the image",
2992 .def_value_str
= "off"
2995 .name
= BLOCK_OPT_CLUSTER_SIZE
,
2996 .type
= QEMU_OPT_SIZE
,
2997 .help
= "qcow2 cluster size",
2998 .def_value_str
= stringify(DEFAULT_CLUSTER_SIZE
)
3001 .name
= BLOCK_OPT_PREALLOC
,
3002 .type
= QEMU_OPT_STRING
,
3003 .help
= "Preallocation mode (allowed values: off, metadata, "
3007 .name
= BLOCK_OPT_LAZY_REFCOUNTS
,
3008 .type
= QEMU_OPT_BOOL
,
3009 .help
= "Postpone refcount updates",
3010 .def_value_str
= "off"
3013 .name
= BLOCK_OPT_REFCOUNT_BITS
,
3014 .type
= QEMU_OPT_NUMBER
,
3015 .help
= "Width of a reference count entry in bits",
3016 .def_value_str
= "16"
3018 { /* end of list */ }
3022 BlockDriver bdrv_qcow2
= {
3023 .format_name
= "qcow2",
3024 .instance_size
= sizeof(BDRVQcow2State
),
3025 .bdrv_probe
= qcow2_probe
,
3026 .bdrv_open
= qcow2_open
,
3027 .bdrv_close
= qcow2_close
,
3028 .bdrv_reopen_prepare
= qcow2_reopen_prepare
,
3029 .bdrv_create
= qcow2_create
,
3030 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
3031 .bdrv_co_get_block_status
= qcow2_co_get_block_status
,
3032 .bdrv_set_key
= qcow2_set_key
,
3034 .bdrv_co_readv
= qcow2_co_readv
,
3035 .bdrv_co_writev
= qcow2_co_writev
,
3036 .bdrv_co_flush_to_os
= qcow2_co_flush_to_os
,
3038 .bdrv_co_write_zeroes
= qcow2_co_write_zeroes
,
3039 .bdrv_co_discard
= qcow2_co_discard
,
3040 .bdrv_truncate
= qcow2_truncate
,
3041 .bdrv_write_compressed
= qcow2_write_compressed
,
3042 .bdrv_make_empty
= qcow2_make_empty
,
3044 .bdrv_snapshot_create
= qcow2_snapshot_create
,
3045 .bdrv_snapshot_goto
= qcow2_snapshot_goto
,
3046 .bdrv_snapshot_delete
= qcow2_snapshot_delete
,
3047 .bdrv_snapshot_list
= qcow2_snapshot_list
,
3048 .bdrv_snapshot_load_tmp
= qcow2_snapshot_load_tmp
,
3049 .bdrv_get_info
= qcow2_get_info
,
3050 .bdrv_get_specific_info
= qcow2_get_specific_info
,
3052 .bdrv_save_vmstate
= qcow2_save_vmstate
,
3053 .bdrv_load_vmstate
= qcow2_load_vmstate
,
3055 .supports_backing
= true,
3056 .bdrv_change_backing_file
= qcow2_change_backing_file
,
3058 .bdrv_refresh_limits
= qcow2_refresh_limits
,
3059 .bdrv_invalidate_cache
= qcow2_invalidate_cache
,
3061 .create_opts
= &qcow2_create_opts
,
3062 .bdrv_check
= qcow2_check
,
3063 .bdrv_amend_options
= qcow2_amend_options
,
3065 .bdrv_detach_aio_context
= qcow2_detach_aio_context
,
3066 .bdrv_attach_aio_context
= qcow2_attach_aio_context
,
3069 static void bdrv_qcow2_init(void)
3071 bdrv_register(&bdrv_qcow2
);
3074 block_init(bdrv_qcow2_init
);