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"
29 #include "block/qcow2.h"
30 #include "qemu/error-report.h"
31 #include "qapi/qmp/qerror.h"
32 #include "qapi/qmp/qbool.h"
36 Differences with QCOW:
38 - Support for multiple incremental snapshots.
39 - Memory management by reference counts.
40 - Clusters which have a reference count of one have the bit
41 QCOW_OFLAG_COPIED to optimize write performance.
42 - Size of compressed clusters is stored in sectors to reduce bit usage
43 in the cluster offsets.
44 - Support for storing additional data (such as the VM state) in the
46 - If a backing store is used, the cluster size is not constrained
47 (could be backported to QCOW).
48 - L2 tables have always a size of one cluster.
55 } QEMU_PACKED QCowExtension
;
57 #define QCOW2_EXT_MAGIC_END 0
58 #define QCOW2_EXT_MAGIC_BACKING_FORMAT 0xE2792ACA
59 #define QCOW2_EXT_MAGIC_FEATURE_TABLE 0x6803f857
61 static int qcow2_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
63 const QCowHeader
*cow_header
= (const void *)buf
;
65 if (buf_size
>= sizeof(QCowHeader
) &&
66 be32_to_cpu(cow_header
->magic
) == QCOW_MAGIC
&&
67 be32_to_cpu(cow_header
->version
) >= 2)
75 * read qcow2 extension and fill bs
76 * start reading from start_offset
77 * finish reading upon magic of value 0 or when end_offset reached
78 * unknown magic is skipped (future extension this version knows nothing about)
79 * return 0 upon success, non-0 otherwise
81 static int qcow2_read_extensions(BlockDriverState
*bs
, uint64_t start_offset
,
82 uint64_t end_offset
, void **p_feature_table
,
85 BDRVQcowState
*s
= bs
->opaque
;
91 printf("qcow2_read_extensions: start=%ld end=%ld\n", start_offset
, end_offset
);
93 offset
= start_offset
;
94 while (offset
< end_offset
) {
98 if (offset
> s
->cluster_size
)
99 printf("qcow2_read_extension: suspicious offset %lu\n", offset
);
101 printf("attempting to read extended header in offset %lu\n", offset
);
104 ret
= bdrv_pread(bs
->file
, offset
, &ext
, sizeof(ext
));
106 error_setg_errno(errp
, -ret
, "qcow2_read_extension: ERROR: "
107 "pread fail from offset %" PRIu64
, offset
);
110 be32_to_cpus(&ext
.magic
);
111 be32_to_cpus(&ext
.len
);
112 offset
+= sizeof(ext
);
114 printf("ext.magic = 0x%x\n", ext
.magic
);
116 if (ext
.len
> end_offset
- offset
) {
117 error_setg(errp
, "Header extension too large");
122 case QCOW2_EXT_MAGIC_END
:
125 case QCOW2_EXT_MAGIC_BACKING_FORMAT
:
126 if (ext
.len
>= sizeof(bs
->backing_format
)) {
127 error_setg(errp
, "ERROR: ext_backing_format: len=%u too large"
128 " (>=%zu)", ext
.len
, sizeof(bs
->backing_format
));
131 ret
= bdrv_pread(bs
->file
, offset
, bs
->backing_format
, ext
.len
);
133 error_setg_errno(errp
, -ret
, "ERROR: ext_backing_format: "
134 "Could not read format name");
137 bs
->backing_format
[ext
.len
] = '\0';
139 printf("Qcow2: Got format extension %s\n", bs
->backing_format
);
143 case QCOW2_EXT_MAGIC_FEATURE_TABLE
:
144 if (p_feature_table
!= NULL
) {
145 void* feature_table
= g_malloc0(ext
.len
+ 2 * sizeof(Qcow2Feature
));
146 ret
= bdrv_pread(bs
->file
, offset
, feature_table
, ext
.len
);
148 error_setg_errno(errp
, -ret
, "ERROR: ext_feature_table: "
149 "Could not read table");
153 *p_feature_table
= feature_table
;
158 /* unknown magic - save it in case we need to rewrite the header */
160 Qcow2UnknownHeaderExtension
*uext
;
162 uext
= g_malloc0(sizeof(*uext
) + ext
.len
);
163 uext
->magic
= ext
.magic
;
165 QLIST_INSERT_HEAD(&s
->unknown_header_ext
, uext
, next
);
167 ret
= bdrv_pread(bs
->file
, offset
, uext
->data
, uext
->len
);
169 error_setg_errno(errp
, -ret
, "ERROR: unknown extension: "
170 "Could not read data");
177 offset
+= ((ext
.len
+ 7) & ~7);
183 static void cleanup_unknown_header_ext(BlockDriverState
*bs
)
185 BDRVQcowState
*s
= bs
->opaque
;
186 Qcow2UnknownHeaderExtension
*uext
, *next
;
188 QLIST_FOREACH_SAFE(uext
, &s
->unknown_header_ext
, next
, next
) {
189 QLIST_REMOVE(uext
, next
);
194 static void GCC_FMT_ATTR(3, 4) report_unsupported(BlockDriverState
*bs
,
195 Error
**errp
, const char *fmt
, ...)
201 vsnprintf(msg
, sizeof(msg
), fmt
, ap
);
204 error_set(errp
, QERR_UNKNOWN_BLOCK_FORMAT_FEATURE
, bs
->device_name
, "qcow2",
208 static void report_unsupported_feature(BlockDriverState
*bs
,
209 Error
**errp
, Qcow2Feature
*table
, uint64_t mask
)
211 while (table
&& table
->name
[0] != '\0') {
212 if (table
->type
== QCOW2_FEAT_TYPE_INCOMPATIBLE
) {
213 if (mask
& (1 << table
->bit
)) {
214 report_unsupported(bs
, errp
, "%.46s", table
->name
);
215 mask
&= ~(1 << table
->bit
);
222 report_unsupported(bs
, errp
, "Unknown incompatible feature: %" PRIx64
,
228 * Sets the dirty bit and flushes afterwards if necessary.
230 * The incompatible_features bit is only set if the image file header was
231 * updated successfully. Therefore it is not required to check the return
232 * value of this function.
234 int qcow2_mark_dirty(BlockDriverState
*bs
)
236 BDRVQcowState
*s
= bs
->opaque
;
240 assert(s
->qcow_version
>= 3);
242 if (s
->incompatible_features
& QCOW2_INCOMPAT_DIRTY
) {
243 return 0; /* already dirty */
246 val
= cpu_to_be64(s
->incompatible_features
| QCOW2_INCOMPAT_DIRTY
);
247 ret
= bdrv_pwrite(bs
->file
, offsetof(QCowHeader
, incompatible_features
),
252 ret
= bdrv_flush(bs
->file
);
257 /* Only treat image as dirty if the header was updated successfully */
258 s
->incompatible_features
|= QCOW2_INCOMPAT_DIRTY
;
263 * Clears the dirty bit and flushes before if necessary. Only call this
264 * function when there are no pending requests, it does not guard against
265 * concurrent requests dirtying the image.
267 static int qcow2_mark_clean(BlockDriverState
*bs
)
269 BDRVQcowState
*s
= bs
->opaque
;
271 if (s
->incompatible_features
& QCOW2_INCOMPAT_DIRTY
) {
272 int ret
= bdrv_flush(bs
);
277 s
->incompatible_features
&= ~QCOW2_INCOMPAT_DIRTY
;
278 return qcow2_update_header(bs
);
284 * Marks the image as corrupt.
286 int qcow2_mark_corrupt(BlockDriverState
*bs
)
288 BDRVQcowState
*s
= bs
->opaque
;
290 s
->incompatible_features
|= QCOW2_INCOMPAT_CORRUPT
;
291 return qcow2_update_header(bs
);
295 * Marks the image as consistent, i.e., unsets the corrupt bit, and flushes
296 * before if necessary.
298 int qcow2_mark_consistent(BlockDriverState
*bs
)
300 BDRVQcowState
*s
= bs
->opaque
;
302 if (s
->incompatible_features
& QCOW2_INCOMPAT_CORRUPT
) {
303 int ret
= bdrv_flush(bs
);
308 s
->incompatible_features
&= ~QCOW2_INCOMPAT_CORRUPT
;
309 return qcow2_update_header(bs
);
314 static int qcow2_check(BlockDriverState
*bs
, BdrvCheckResult
*result
,
317 int ret
= qcow2_check_refcounts(bs
, result
, fix
);
322 if (fix
&& result
->check_errors
== 0 && result
->corruptions
== 0) {
323 ret
= qcow2_mark_clean(bs
);
327 return qcow2_mark_consistent(bs
);
332 static int validate_table_offset(BlockDriverState
*bs
, uint64_t offset
,
333 uint64_t entries
, size_t entry_len
)
335 BDRVQcowState
*s
= bs
->opaque
;
338 /* Use signed INT64_MAX as the maximum even for uint64_t header fields,
339 * because values will be passed to qemu functions taking int64_t. */
340 if (entries
> INT64_MAX
/ entry_len
) {
344 size
= entries
* entry_len
;
346 if (INT64_MAX
- size
< offset
) {
350 /* Tables must be cluster aligned */
351 if (offset
& (s
->cluster_size
- 1)) {
358 static QemuOptsList qcow2_runtime_opts
= {
360 .head
= QTAILQ_HEAD_INITIALIZER(qcow2_runtime_opts
.head
),
363 .name
= QCOW2_OPT_LAZY_REFCOUNTS
,
364 .type
= QEMU_OPT_BOOL
,
365 .help
= "Postpone refcount updates",
368 .name
= QCOW2_OPT_DISCARD_REQUEST
,
369 .type
= QEMU_OPT_BOOL
,
370 .help
= "Pass guest discard requests to the layer below",
373 .name
= QCOW2_OPT_DISCARD_SNAPSHOT
,
374 .type
= QEMU_OPT_BOOL
,
375 .help
= "Generate discard requests when snapshot related space "
379 .name
= QCOW2_OPT_DISCARD_OTHER
,
380 .type
= QEMU_OPT_BOOL
,
381 .help
= "Generate discard requests when other clusters are freed",
384 .name
= QCOW2_OPT_OVERLAP
,
385 .type
= QEMU_OPT_STRING
,
386 .help
= "Selects which overlap checks to perform from a range of "
387 "templates (none, constant, cached, all)",
390 .name
= QCOW2_OPT_OVERLAP_MAIN_HEADER
,
391 .type
= QEMU_OPT_BOOL
,
392 .help
= "Check for unintended writes into the main qcow2 header",
395 .name
= QCOW2_OPT_OVERLAP_ACTIVE_L1
,
396 .type
= QEMU_OPT_BOOL
,
397 .help
= "Check for unintended writes into the active L1 table",
400 .name
= QCOW2_OPT_OVERLAP_ACTIVE_L2
,
401 .type
= QEMU_OPT_BOOL
,
402 .help
= "Check for unintended writes into an active L2 table",
405 .name
= QCOW2_OPT_OVERLAP_REFCOUNT_TABLE
,
406 .type
= QEMU_OPT_BOOL
,
407 .help
= "Check for unintended writes into the refcount table",
410 .name
= QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK
,
411 .type
= QEMU_OPT_BOOL
,
412 .help
= "Check for unintended writes into a refcount block",
415 .name
= QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE
,
416 .type
= QEMU_OPT_BOOL
,
417 .help
= "Check for unintended writes into the snapshot table",
420 .name
= QCOW2_OPT_OVERLAP_INACTIVE_L1
,
421 .type
= QEMU_OPT_BOOL
,
422 .help
= "Check for unintended writes into an inactive L1 table",
425 .name
= QCOW2_OPT_OVERLAP_INACTIVE_L2
,
426 .type
= QEMU_OPT_BOOL
,
427 .help
= "Check for unintended writes into an inactive L2 table",
429 { /* end of list */ }
433 static const char *overlap_bool_option_names
[QCOW2_OL_MAX_BITNR
] = {
434 [QCOW2_OL_MAIN_HEADER_BITNR
] = QCOW2_OPT_OVERLAP_MAIN_HEADER
,
435 [QCOW2_OL_ACTIVE_L1_BITNR
] = QCOW2_OPT_OVERLAP_ACTIVE_L1
,
436 [QCOW2_OL_ACTIVE_L2_BITNR
] = QCOW2_OPT_OVERLAP_ACTIVE_L2
,
437 [QCOW2_OL_REFCOUNT_TABLE_BITNR
] = QCOW2_OPT_OVERLAP_REFCOUNT_TABLE
,
438 [QCOW2_OL_REFCOUNT_BLOCK_BITNR
] = QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK
,
439 [QCOW2_OL_SNAPSHOT_TABLE_BITNR
] = QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE
,
440 [QCOW2_OL_INACTIVE_L1_BITNR
] = QCOW2_OPT_OVERLAP_INACTIVE_L1
,
441 [QCOW2_OL_INACTIVE_L2_BITNR
] = QCOW2_OPT_OVERLAP_INACTIVE_L2
,
444 static int qcow2_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
447 BDRVQcowState
*s
= bs
->opaque
;
451 Error
*local_err
= NULL
;
453 uint64_t l1_vm_state_index
;
454 const char *opt_overlap_check
;
455 int overlap_check_template
= 0;
457 ret
= bdrv_pread(bs
->file
, 0, &header
, sizeof(header
));
459 error_setg_errno(errp
, -ret
, "Could not read qcow2 header");
462 be32_to_cpus(&header
.magic
);
463 be32_to_cpus(&header
.version
);
464 be64_to_cpus(&header
.backing_file_offset
);
465 be32_to_cpus(&header
.backing_file_size
);
466 be64_to_cpus(&header
.size
);
467 be32_to_cpus(&header
.cluster_bits
);
468 be32_to_cpus(&header
.crypt_method
);
469 be64_to_cpus(&header
.l1_table_offset
);
470 be32_to_cpus(&header
.l1_size
);
471 be64_to_cpus(&header
.refcount_table_offset
);
472 be32_to_cpus(&header
.refcount_table_clusters
);
473 be64_to_cpus(&header
.snapshots_offset
);
474 be32_to_cpus(&header
.nb_snapshots
);
476 if (header
.magic
!= QCOW_MAGIC
) {
477 error_setg(errp
, "Image is not in qcow2 format");
481 if (header
.version
< 2 || header
.version
> 3) {
482 report_unsupported(bs
, errp
, "QCOW version %d", header
.version
);
487 s
->qcow_version
= header
.version
;
489 /* Initialise cluster size */
490 if (header
.cluster_bits
< MIN_CLUSTER_BITS
||
491 header
.cluster_bits
> MAX_CLUSTER_BITS
) {
492 error_setg(errp
, "Unsupported cluster size: 2^%i", header
.cluster_bits
);
497 s
->cluster_bits
= header
.cluster_bits
;
498 s
->cluster_size
= 1 << s
->cluster_bits
;
499 s
->cluster_sectors
= 1 << (s
->cluster_bits
- 9);
501 /* Initialise version 3 header fields */
502 if (header
.version
== 2) {
503 header
.incompatible_features
= 0;
504 header
.compatible_features
= 0;
505 header
.autoclear_features
= 0;
506 header
.refcount_order
= 4;
507 header
.header_length
= 72;
509 be64_to_cpus(&header
.incompatible_features
);
510 be64_to_cpus(&header
.compatible_features
);
511 be64_to_cpus(&header
.autoclear_features
);
512 be32_to_cpus(&header
.refcount_order
);
513 be32_to_cpus(&header
.header_length
);
515 if (header
.header_length
< 104) {
516 error_setg(errp
, "qcow2 header too short");
522 if (header
.header_length
> s
->cluster_size
) {
523 error_setg(errp
, "qcow2 header exceeds cluster size");
528 if (header
.header_length
> sizeof(header
)) {
529 s
->unknown_header_fields_size
= header
.header_length
- sizeof(header
);
530 s
->unknown_header_fields
= g_malloc(s
->unknown_header_fields_size
);
531 ret
= bdrv_pread(bs
->file
, sizeof(header
), s
->unknown_header_fields
,
532 s
->unknown_header_fields_size
);
534 error_setg_errno(errp
, -ret
, "Could not read unknown qcow2 header "
540 if (header
.backing_file_offset
> s
->cluster_size
) {
541 error_setg(errp
, "Invalid backing file offset");
546 if (header
.backing_file_offset
) {
547 ext_end
= header
.backing_file_offset
;
549 ext_end
= 1 << header
.cluster_bits
;
552 /* Handle feature bits */
553 s
->incompatible_features
= header
.incompatible_features
;
554 s
->compatible_features
= header
.compatible_features
;
555 s
->autoclear_features
= header
.autoclear_features
;
557 if (s
->incompatible_features
& ~QCOW2_INCOMPAT_MASK
) {
558 void *feature_table
= NULL
;
559 qcow2_read_extensions(bs
, header
.header_length
, ext_end
,
560 &feature_table
, NULL
);
561 report_unsupported_feature(bs
, errp
, feature_table
,
562 s
->incompatible_features
&
563 ~QCOW2_INCOMPAT_MASK
);
565 g_free(feature_table
);
569 if (s
->incompatible_features
& QCOW2_INCOMPAT_CORRUPT
) {
570 /* Corrupt images may not be written to unless they are being repaired
572 if ((flags
& BDRV_O_RDWR
) && !(flags
& BDRV_O_CHECK
)) {
573 error_setg(errp
, "qcow2: Image is corrupt; cannot be opened "
580 /* Check support for various header values */
581 if (header
.refcount_order
!= 4) {
582 report_unsupported(bs
, errp
, "%d bit reference counts",
583 1 << header
.refcount_order
);
587 s
->refcount_order
= header
.refcount_order
;
589 if (header
.crypt_method
> QCOW_CRYPT_AES
) {
590 error_setg(errp
, "Unsupported encryption method: %i",
591 header
.crypt_method
);
595 s
->crypt_method_header
= header
.crypt_method
;
596 if (s
->crypt_method_header
) {
600 s
->l2_bits
= s
->cluster_bits
- 3; /* L2 is always one cluster */
601 s
->l2_size
= 1 << s
->l2_bits
;
602 bs
->total_sectors
= header
.size
/ 512;
603 s
->csize_shift
= (62 - (s
->cluster_bits
- 8));
604 s
->csize_mask
= (1 << (s
->cluster_bits
- 8)) - 1;
605 s
->cluster_offset_mask
= (1LL << s
->csize_shift
) - 1;
607 s
->refcount_table_offset
= header
.refcount_table_offset
;
608 s
->refcount_table_size
=
609 header
.refcount_table_clusters
<< (s
->cluster_bits
- 3);
611 if (header
.refcount_table_clusters
> (0x800000 >> s
->cluster_bits
)) {
612 /* 8 MB refcount table is enough for 2 PB images at 64k cluster size
613 * (128 GB for 512 byte clusters, 2 EB for 2 MB clusters) */
614 error_setg(errp
, "Reference count table too large");
619 ret
= validate_table_offset(bs
, s
->refcount_table_offset
,
620 s
->refcount_table_size
, sizeof(uint64_t));
622 error_setg(errp
, "Invalid reference count table offset");
626 /* Snapshot table offset/length */
627 if (header
.nb_snapshots
> QCOW_MAX_SNAPSHOTS
) {
628 error_setg(errp
, "Too many snapshots");
633 ret
= validate_table_offset(bs
, header
.snapshots_offset
,
635 sizeof(QCowSnapshotHeader
));
637 error_setg(errp
, "Invalid snapshot table offset");
641 s
->snapshots_offset
= header
.snapshots_offset
;
642 s
->nb_snapshots
= header
.nb_snapshots
;
644 /* read the level 1 table */
645 if (header
.l1_size
> 0x2000000) {
646 /* 32 MB L1 table is enough for 2 PB images at 64k cluster size
647 * (128 GB for 512 byte clusters, 2 EB for 2 MB clusters) */
648 error_setg(errp
, "Active L1 table too large");
652 s
->l1_size
= header
.l1_size
;
654 l1_vm_state_index
= size_to_l1(s
, header
.size
);
655 if (l1_vm_state_index
> INT_MAX
) {
656 error_setg(errp
, "Image is too big");
660 s
->l1_vm_state_index
= l1_vm_state_index
;
662 /* the L1 table must contain at least enough entries to put
664 if (s
->l1_size
< s
->l1_vm_state_index
) {
665 error_setg(errp
, "L1 table is too small");
670 ret
= validate_table_offset(bs
, header
.l1_table_offset
,
671 header
.l1_size
, sizeof(uint64_t));
673 error_setg(errp
, "Invalid L1 table offset");
676 s
->l1_table_offset
= header
.l1_table_offset
;
679 if (s
->l1_size
> 0) {
680 s
->l1_table
= g_malloc0(
681 align_offset(s
->l1_size
* sizeof(uint64_t), 512));
682 ret
= bdrv_pread(bs
->file
, s
->l1_table_offset
, s
->l1_table
,
683 s
->l1_size
* sizeof(uint64_t));
685 error_setg_errno(errp
, -ret
, "Could not read L1 table");
688 for(i
= 0;i
< s
->l1_size
; i
++) {
689 be64_to_cpus(&s
->l1_table
[i
]);
693 /* alloc L2 table/refcount block cache */
694 s
->l2_table_cache
= qcow2_cache_create(bs
, L2_CACHE_SIZE
);
695 s
->refcount_block_cache
= qcow2_cache_create(bs
, REFCOUNT_CACHE_SIZE
);
697 s
->cluster_cache
= g_malloc(s
->cluster_size
);
698 /* one more sector for decompressed data alignment */
699 s
->cluster_data
= qemu_blockalign(bs
, QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_size
701 s
->cluster_cache_offset
= -1;
704 ret
= qcow2_refcount_init(bs
);
706 error_setg_errno(errp
, -ret
, "Could not initialize refcount handling");
710 QLIST_INIT(&s
->cluster_allocs
);
711 QTAILQ_INIT(&s
->discards
);
713 /* read qcow2 extensions */
714 if (qcow2_read_extensions(bs
, header
.header_length
, ext_end
, NULL
,
716 error_propagate(errp
, local_err
);
721 /* read the backing file name */
722 if (header
.backing_file_offset
!= 0) {
723 len
= header
.backing_file_size
;
727 ret
= bdrv_pread(bs
->file
, header
.backing_file_offset
,
728 bs
->backing_file
, len
);
730 error_setg_errno(errp
, -ret
, "Could not read backing file name");
733 bs
->backing_file
[len
] = '\0';
736 ret
= qcow2_read_snapshots(bs
);
738 error_setg_errno(errp
, -ret
, "Could not read snapshots");
742 /* Clear unknown autoclear feature bits */
743 if (!bs
->read_only
&& !(flags
& BDRV_O_INCOMING
) && s
->autoclear_features
) {
744 s
->autoclear_features
= 0;
745 ret
= qcow2_update_header(bs
);
747 error_setg_errno(errp
, -ret
, "Could not update qcow2 header");
752 /* Initialise locks */
753 qemu_co_mutex_init(&s
->lock
);
755 /* Repair image if dirty */
756 if (!(flags
& (BDRV_O_CHECK
| BDRV_O_INCOMING
)) && !bs
->read_only
&&
757 (s
->incompatible_features
& QCOW2_INCOMPAT_DIRTY
)) {
758 BdrvCheckResult result
= {0};
760 ret
= qcow2_check(bs
, &result
, BDRV_FIX_ERRORS
);
762 error_setg_errno(errp
, -ret
, "Could not repair dirty image");
767 /* Enable lazy_refcounts according to image and command line options */
768 opts
= qemu_opts_create(&qcow2_runtime_opts
, NULL
, 0, &error_abort
);
769 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
771 error_propagate(errp
, local_err
);
776 s
->use_lazy_refcounts
= qemu_opt_get_bool(opts
, QCOW2_OPT_LAZY_REFCOUNTS
,
777 (s
->compatible_features
& QCOW2_COMPAT_LAZY_REFCOUNTS
));
779 s
->discard_passthrough
[QCOW2_DISCARD_NEVER
] = false;
780 s
->discard_passthrough
[QCOW2_DISCARD_ALWAYS
] = true;
781 s
->discard_passthrough
[QCOW2_DISCARD_REQUEST
] =
782 qemu_opt_get_bool(opts
, QCOW2_OPT_DISCARD_REQUEST
,
783 flags
& BDRV_O_UNMAP
);
784 s
->discard_passthrough
[QCOW2_DISCARD_SNAPSHOT
] =
785 qemu_opt_get_bool(opts
, QCOW2_OPT_DISCARD_SNAPSHOT
, true);
786 s
->discard_passthrough
[QCOW2_DISCARD_OTHER
] =
787 qemu_opt_get_bool(opts
, QCOW2_OPT_DISCARD_OTHER
, false);
789 opt_overlap_check
= qemu_opt_get(opts
, "overlap-check") ?: "cached";
790 if (!strcmp(opt_overlap_check
, "none")) {
791 overlap_check_template
= 0;
792 } else if (!strcmp(opt_overlap_check
, "constant")) {
793 overlap_check_template
= QCOW2_OL_CONSTANT
;
794 } else if (!strcmp(opt_overlap_check
, "cached")) {
795 overlap_check_template
= QCOW2_OL_CACHED
;
796 } else if (!strcmp(opt_overlap_check
, "all")) {
797 overlap_check_template
= QCOW2_OL_ALL
;
799 error_setg(errp
, "Unsupported value '%s' for qcow2 option "
800 "'overlap-check'. Allowed are either of the following: "
801 "none, constant, cached, all", opt_overlap_check
);
807 s
->overlap_check
= 0;
808 for (i
= 0; i
< QCOW2_OL_MAX_BITNR
; i
++) {
809 /* overlap-check defines a template bitmask, but every flag may be
810 * overwritten through the associated boolean option */
812 qemu_opt_get_bool(opts
, overlap_bool_option_names
[i
],
813 overlap_check_template
& (1 << i
)) << i
;
818 if (s
->use_lazy_refcounts
&& s
->qcow_version
< 3) {
819 error_setg(errp
, "Lazy refcounts require a qcow2 image with at least "
820 "qemu 1.1 compatibility level");
827 BdrvCheckResult result
= {0};
828 qcow2_check_refcounts(bs
, &result
, 0);
834 g_free(s
->unknown_header_fields
);
835 cleanup_unknown_header_ext(bs
);
836 qcow2_free_snapshots(bs
);
837 qcow2_refcount_close(bs
);
839 /* else pre-write overlap checks in cache_destroy may crash */
841 if (s
->l2_table_cache
) {
842 qcow2_cache_destroy(bs
, s
->l2_table_cache
);
844 if (s
->refcount_block_cache
) {
845 qcow2_cache_destroy(bs
, s
->refcount_block_cache
);
847 g_free(s
->cluster_cache
);
848 qemu_vfree(s
->cluster_data
);
852 static int qcow2_refresh_limits(BlockDriverState
*bs
)
854 BDRVQcowState
*s
= bs
->opaque
;
856 bs
->bl
.write_zeroes_alignment
= s
->cluster_sectors
;
861 static int qcow2_set_key(BlockDriverState
*bs
, const char *key
)
863 BDRVQcowState
*s
= bs
->opaque
;
867 memset(keybuf
, 0, 16);
871 /* XXX: we could compress the chars to 7 bits to increase
873 for(i
= 0;i
< len
;i
++) {
876 s
->crypt_method
= s
->crypt_method_header
;
878 if (AES_set_encrypt_key(keybuf
, 128, &s
->aes_encrypt_key
) != 0)
880 if (AES_set_decrypt_key(keybuf
, 128, &s
->aes_decrypt_key
) != 0)
890 AES_encrypt(in
, tmp
, &s
->aes_encrypt_key
);
891 AES_decrypt(tmp
, out
, &s
->aes_decrypt_key
);
892 for(i
= 0; i
< 16; i
++)
893 printf(" %02x", tmp
[i
]);
895 for(i
= 0; i
< 16; i
++)
896 printf(" %02x", out
[i
]);
903 /* We have nothing to do for QCOW2 reopen, stubs just return
905 static int qcow2_reopen_prepare(BDRVReopenState
*state
,
906 BlockReopenQueue
*queue
, Error
**errp
)
911 static int64_t coroutine_fn
qcow2_co_get_block_status(BlockDriverState
*bs
,
912 int64_t sector_num
, int nb_sectors
, int *pnum
)
914 BDRVQcowState
*s
= bs
->opaque
;
915 uint64_t cluster_offset
;
916 int index_in_cluster
, ret
;
920 qemu_co_mutex_lock(&s
->lock
);
921 ret
= qcow2_get_cluster_offset(bs
, sector_num
<< 9, pnum
, &cluster_offset
);
922 qemu_co_mutex_unlock(&s
->lock
);
927 if (cluster_offset
!= 0 && ret
!= QCOW2_CLUSTER_COMPRESSED
&&
929 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
930 cluster_offset
|= (index_in_cluster
<< BDRV_SECTOR_BITS
);
931 status
|= BDRV_BLOCK_OFFSET_VALID
| cluster_offset
;
933 if (ret
== QCOW2_CLUSTER_ZERO
) {
934 status
|= BDRV_BLOCK_ZERO
;
935 } else if (ret
!= QCOW2_CLUSTER_UNALLOCATED
) {
936 status
|= BDRV_BLOCK_DATA
;
941 /* handle reading after the end of the backing file */
942 int qcow2_backing_read1(BlockDriverState
*bs
, QEMUIOVector
*qiov
,
943 int64_t sector_num
, int nb_sectors
)
946 if ((sector_num
+ nb_sectors
) <= bs
->total_sectors
)
948 if (sector_num
>= bs
->total_sectors
)
951 n1
= bs
->total_sectors
- sector_num
;
953 qemu_iovec_memset(qiov
, 512 * n1
, 0, 512 * (nb_sectors
- n1
));
958 static coroutine_fn
int qcow2_co_readv(BlockDriverState
*bs
, int64_t sector_num
,
959 int remaining_sectors
, QEMUIOVector
*qiov
)
961 BDRVQcowState
*s
= bs
->opaque
;
962 int index_in_cluster
, n1
;
964 int cur_nr_sectors
; /* number of sectors in current iteration */
965 uint64_t cluster_offset
= 0;
966 uint64_t bytes_done
= 0;
967 QEMUIOVector hd_qiov
;
968 uint8_t *cluster_data
= NULL
;
970 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
972 qemu_co_mutex_lock(&s
->lock
);
974 while (remaining_sectors
!= 0) {
976 /* prepare next request */
977 cur_nr_sectors
= remaining_sectors
;
978 if (s
->crypt_method
) {
979 cur_nr_sectors
= MIN(cur_nr_sectors
,
980 QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
);
983 ret
= qcow2_get_cluster_offset(bs
, sector_num
<< 9,
984 &cur_nr_sectors
, &cluster_offset
);
989 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
991 qemu_iovec_reset(&hd_qiov
);
992 qemu_iovec_concat(&hd_qiov
, qiov
, bytes_done
,
993 cur_nr_sectors
* 512);
996 case QCOW2_CLUSTER_UNALLOCATED
:
998 if (bs
->backing_hd
) {
999 /* read from the base image */
1000 n1
= qcow2_backing_read1(bs
->backing_hd
, &hd_qiov
,
1001 sector_num
, cur_nr_sectors
);
1003 BLKDBG_EVENT(bs
->file
, BLKDBG_READ_BACKING_AIO
);
1004 qemu_co_mutex_unlock(&s
->lock
);
1005 ret
= bdrv_co_readv(bs
->backing_hd
, sector_num
,
1007 qemu_co_mutex_lock(&s
->lock
);
1013 /* Note: in this case, no need to wait */
1014 qemu_iovec_memset(&hd_qiov
, 0, 0, 512 * cur_nr_sectors
);
1018 case QCOW2_CLUSTER_ZERO
:
1019 qemu_iovec_memset(&hd_qiov
, 0, 0, 512 * cur_nr_sectors
);
1022 case QCOW2_CLUSTER_COMPRESSED
:
1023 /* add AIO support for compressed blocks ? */
1024 ret
= qcow2_decompress_cluster(bs
, cluster_offset
);
1029 qemu_iovec_from_buf(&hd_qiov
, 0,
1030 s
->cluster_cache
+ index_in_cluster
* 512,
1031 512 * cur_nr_sectors
);
1034 case QCOW2_CLUSTER_NORMAL
:
1035 if ((cluster_offset
& 511) != 0) {
1040 if (s
->crypt_method
) {
1042 * For encrypted images, read everything into a temporary
1043 * contiguous buffer on which the AES functions can work.
1045 if (!cluster_data
) {
1047 qemu_blockalign(bs
, QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_size
);
1050 assert(cur_nr_sectors
<=
1051 QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
);
1052 qemu_iovec_reset(&hd_qiov
);
1053 qemu_iovec_add(&hd_qiov
, cluster_data
,
1054 512 * cur_nr_sectors
);
1057 BLKDBG_EVENT(bs
->file
, BLKDBG_READ_AIO
);
1058 qemu_co_mutex_unlock(&s
->lock
);
1059 ret
= bdrv_co_readv(bs
->file
,
1060 (cluster_offset
>> 9) + index_in_cluster
,
1061 cur_nr_sectors
, &hd_qiov
);
1062 qemu_co_mutex_lock(&s
->lock
);
1066 if (s
->crypt_method
) {
1067 qcow2_encrypt_sectors(s
, sector_num
, cluster_data
,
1068 cluster_data
, cur_nr_sectors
, 0, &s
->aes_decrypt_key
);
1069 qemu_iovec_from_buf(qiov
, bytes_done
,
1070 cluster_data
, 512 * cur_nr_sectors
);
1075 g_assert_not_reached();
1080 remaining_sectors
-= cur_nr_sectors
;
1081 sector_num
+= cur_nr_sectors
;
1082 bytes_done
+= cur_nr_sectors
* 512;
1087 qemu_co_mutex_unlock(&s
->lock
);
1089 qemu_iovec_destroy(&hd_qiov
);
1090 qemu_vfree(cluster_data
);
1095 static coroutine_fn
int qcow2_co_writev(BlockDriverState
*bs
,
1097 int remaining_sectors
,
1100 BDRVQcowState
*s
= bs
->opaque
;
1101 int index_in_cluster
;
1103 int cur_nr_sectors
; /* number of sectors in current iteration */
1104 uint64_t cluster_offset
;
1105 QEMUIOVector hd_qiov
;
1106 uint64_t bytes_done
= 0;
1107 uint8_t *cluster_data
= NULL
;
1108 QCowL2Meta
*l2meta
= NULL
;
1110 trace_qcow2_writev_start_req(qemu_coroutine_self(), sector_num
,
1113 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
1115 s
->cluster_cache_offset
= -1; /* disable compressed cache */
1117 qemu_co_mutex_lock(&s
->lock
);
1119 while (remaining_sectors
!= 0) {
1123 trace_qcow2_writev_start_part(qemu_coroutine_self());
1124 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
1125 cur_nr_sectors
= remaining_sectors
;
1126 if (s
->crypt_method
&&
1128 QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
- index_in_cluster
) {
1130 QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
- index_in_cluster
;
1133 ret
= qcow2_alloc_cluster_offset(bs
, sector_num
<< 9,
1134 &cur_nr_sectors
, &cluster_offset
, &l2meta
);
1139 assert((cluster_offset
& 511) == 0);
1141 qemu_iovec_reset(&hd_qiov
);
1142 qemu_iovec_concat(&hd_qiov
, qiov
, bytes_done
,
1143 cur_nr_sectors
* 512);
1145 if (s
->crypt_method
) {
1146 if (!cluster_data
) {
1147 cluster_data
= qemu_blockalign(bs
, QCOW_MAX_CRYPT_CLUSTERS
*
1151 assert(hd_qiov
.size
<=
1152 QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_size
);
1153 qemu_iovec_to_buf(&hd_qiov
, 0, cluster_data
, hd_qiov
.size
);
1155 qcow2_encrypt_sectors(s
, sector_num
, cluster_data
,
1156 cluster_data
, cur_nr_sectors
, 1, &s
->aes_encrypt_key
);
1158 qemu_iovec_reset(&hd_qiov
);
1159 qemu_iovec_add(&hd_qiov
, cluster_data
,
1160 cur_nr_sectors
* 512);
1163 ret
= qcow2_pre_write_overlap_check(bs
, 0,
1164 cluster_offset
+ index_in_cluster
* BDRV_SECTOR_SIZE
,
1165 cur_nr_sectors
* BDRV_SECTOR_SIZE
);
1170 qemu_co_mutex_unlock(&s
->lock
);
1171 BLKDBG_EVENT(bs
->file
, BLKDBG_WRITE_AIO
);
1172 trace_qcow2_writev_data(qemu_coroutine_self(),
1173 (cluster_offset
>> 9) + index_in_cluster
);
1174 ret
= bdrv_co_writev(bs
->file
,
1175 (cluster_offset
>> 9) + index_in_cluster
,
1176 cur_nr_sectors
, &hd_qiov
);
1177 qemu_co_mutex_lock(&s
->lock
);
1182 while (l2meta
!= NULL
) {
1185 ret
= qcow2_alloc_cluster_link_l2(bs
, l2meta
);
1190 /* Take the request off the list of running requests */
1191 if (l2meta
->nb_clusters
!= 0) {
1192 QLIST_REMOVE(l2meta
, next_in_flight
);
1195 qemu_co_queue_restart_all(&l2meta
->dependent_requests
);
1197 next
= l2meta
->next
;
1202 remaining_sectors
-= cur_nr_sectors
;
1203 sector_num
+= cur_nr_sectors
;
1204 bytes_done
+= cur_nr_sectors
* 512;
1205 trace_qcow2_writev_done_part(qemu_coroutine_self(), cur_nr_sectors
);
1210 qemu_co_mutex_unlock(&s
->lock
);
1212 while (l2meta
!= NULL
) {
1215 if (l2meta
->nb_clusters
!= 0) {
1216 QLIST_REMOVE(l2meta
, next_in_flight
);
1218 qemu_co_queue_restart_all(&l2meta
->dependent_requests
);
1220 next
= l2meta
->next
;
1225 qemu_iovec_destroy(&hd_qiov
);
1226 qemu_vfree(cluster_data
);
1227 trace_qcow2_writev_done_req(qemu_coroutine_self(), ret
);
1232 static void qcow2_close(BlockDriverState
*bs
)
1234 BDRVQcowState
*s
= bs
->opaque
;
1235 g_free(s
->l1_table
);
1236 /* else pre-write overlap checks in cache_destroy may crash */
1239 if (!(bs
->open_flags
& BDRV_O_INCOMING
)) {
1240 qcow2_cache_flush(bs
, s
->l2_table_cache
);
1241 qcow2_cache_flush(bs
, s
->refcount_block_cache
);
1243 qcow2_mark_clean(bs
);
1246 qcow2_cache_destroy(bs
, s
->l2_table_cache
);
1247 qcow2_cache_destroy(bs
, s
->refcount_block_cache
);
1249 g_free(s
->unknown_header_fields
);
1250 cleanup_unknown_header_ext(bs
);
1252 g_free(s
->cluster_cache
);
1253 qemu_vfree(s
->cluster_data
);
1254 qcow2_refcount_close(bs
);
1255 qcow2_free_snapshots(bs
);
1258 static void qcow2_invalidate_cache(BlockDriverState
*bs
, Error
**errp
)
1260 BDRVQcowState
*s
= bs
->opaque
;
1261 int flags
= s
->flags
;
1262 AES_KEY aes_encrypt_key
;
1263 AES_KEY aes_decrypt_key
;
1264 uint32_t crypt_method
= 0;
1266 Error
*local_err
= NULL
;
1270 * Backing files are read-only which makes all of their metadata immutable,
1271 * that means we don't have to worry about reopening them here.
1274 if (s
->crypt_method
) {
1275 crypt_method
= s
->crypt_method
;
1276 memcpy(&aes_encrypt_key
, &s
->aes_encrypt_key
, sizeof(aes_encrypt_key
));
1277 memcpy(&aes_decrypt_key
, &s
->aes_decrypt_key
, sizeof(aes_decrypt_key
));
1282 bdrv_invalidate_cache(bs
->file
, &local_err
);
1284 error_propagate(errp
, local_err
);
1288 memset(s
, 0, sizeof(BDRVQcowState
));
1289 options
= qdict_clone_shallow(bs
->options
);
1291 ret
= qcow2_open(bs
, options
, flags
, &local_err
);
1293 error_setg(errp
, "Could not reopen qcow2 layer: %s",
1294 error_get_pretty(local_err
));
1295 error_free(local_err
);
1297 } else if (ret
< 0) {
1298 error_setg_errno(errp
, -ret
, "Could not reopen qcow2 layer");
1305 s
->crypt_method
= crypt_method
;
1306 memcpy(&s
->aes_encrypt_key
, &aes_encrypt_key
, sizeof(aes_encrypt_key
));
1307 memcpy(&s
->aes_decrypt_key
, &aes_decrypt_key
, sizeof(aes_decrypt_key
));
1311 static size_t header_ext_add(char *buf
, uint32_t magic
, const void *s
,
1312 size_t len
, size_t buflen
)
1314 QCowExtension
*ext_backing_fmt
= (QCowExtension
*) buf
;
1315 size_t ext_len
= sizeof(QCowExtension
) + ((len
+ 7) & ~7);
1317 if (buflen
< ext_len
) {
1321 *ext_backing_fmt
= (QCowExtension
) {
1322 .magic
= cpu_to_be32(magic
),
1323 .len
= cpu_to_be32(len
),
1325 memcpy(buf
+ sizeof(QCowExtension
), s
, len
);
1331 * Updates the qcow2 header, including the variable length parts of it, i.e.
1332 * the backing file name and all extensions. qcow2 was not designed to allow
1333 * such changes, so if we run out of space (we can only use the first cluster)
1334 * this function may fail.
1336 * Returns 0 on success, -errno in error cases.
1338 int qcow2_update_header(BlockDriverState
*bs
)
1340 BDRVQcowState
*s
= bs
->opaque
;
1343 size_t buflen
= s
->cluster_size
;
1345 uint64_t total_size
;
1346 uint32_t refcount_table_clusters
;
1347 size_t header_length
;
1348 Qcow2UnknownHeaderExtension
*uext
;
1350 buf
= qemu_blockalign(bs
, buflen
);
1352 /* Header structure */
1353 header
= (QCowHeader
*) buf
;
1355 if (buflen
< sizeof(*header
)) {
1360 header_length
= sizeof(*header
) + s
->unknown_header_fields_size
;
1361 total_size
= bs
->total_sectors
* BDRV_SECTOR_SIZE
;
1362 refcount_table_clusters
= s
->refcount_table_size
>> (s
->cluster_bits
- 3);
1364 *header
= (QCowHeader
) {
1365 /* Version 2 fields */
1366 .magic
= cpu_to_be32(QCOW_MAGIC
),
1367 .version
= cpu_to_be32(s
->qcow_version
),
1368 .backing_file_offset
= 0,
1369 .backing_file_size
= 0,
1370 .cluster_bits
= cpu_to_be32(s
->cluster_bits
),
1371 .size
= cpu_to_be64(total_size
),
1372 .crypt_method
= cpu_to_be32(s
->crypt_method_header
),
1373 .l1_size
= cpu_to_be32(s
->l1_size
),
1374 .l1_table_offset
= cpu_to_be64(s
->l1_table_offset
),
1375 .refcount_table_offset
= cpu_to_be64(s
->refcount_table_offset
),
1376 .refcount_table_clusters
= cpu_to_be32(refcount_table_clusters
),
1377 .nb_snapshots
= cpu_to_be32(s
->nb_snapshots
),
1378 .snapshots_offset
= cpu_to_be64(s
->snapshots_offset
),
1380 /* Version 3 fields */
1381 .incompatible_features
= cpu_to_be64(s
->incompatible_features
),
1382 .compatible_features
= cpu_to_be64(s
->compatible_features
),
1383 .autoclear_features
= cpu_to_be64(s
->autoclear_features
),
1384 .refcount_order
= cpu_to_be32(s
->refcount_order
),
1385 .header_length
= cpu_to_be32(header_length
),
1388 /* For older versions, write a shorter header */
1389 switch (s
->qcow_version
) {
1391 ret
= offsetof(QCowHeader
, incompatible_features
);
1394 ret
= sizeof(*header
);
1403 memset(buf
, 0, buflen
);
1405 /* Preserve any unknown field in the header */
1406 if (s
->unknown_header_fields_size
) {
1407 if (buflen
< s
->unknown_header_fields_size
) {
1412 memcpy(buf
, s
->unknown_header_fields
, s
->unknown_header_fields_size
);
1413 buf
+= s
->unknown_header_fields_size
;
1414 buflen
-= s
->unknown_header_fields_size
;
1417 /* Backing file format header extension */
1418 if (*bs
->backing_format
) {
1419 ret
= header_ext_add(buf
, QCOW2_EXT_MAGIC_BACKING_FORMAT
,
1420 bs
->backing_format
, strlen(bs
->backing_format
),
1431 Qcow2Feature features
[] = {
1433 .type
= QCOW2_FEAT_TYPE_INCOMPATIBLE
,
1434 .bit
= QCOW2_INCOMPAT_DIRTY_BITNR
,
1435 .name
= "dirty bit",
1438 .type
= QCOW2_FEAT_TYPE_INCOMPATIBLE
,
1439 .bit
= QCOW2_INCOMPAT_CORRUPT_BITNR
,
1440 .name
= "corrupt bit",
1443 .type
= QCOW2_FEAT_TYPE_COMPATIBLE
,
1444 .bit
= QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR
,
1445 .name
= "lazy refcounts",
1449 ret
= header_ext_add(buf
, QCOW2_EXT_MAGIC_FEATURE_TABLE
,
1450 features
, sizeof(features
), buflen
);
1457 /* Keep unknown header extensions */
1458 QLIST_FOREACH(uext
, &s
->unknown_header_ext
, next
) {
1459 ret
= header_ext_add(buf
, uext
->magic
, uext
->data
, uext
->len
, buflen
);
1468 /* End of header extensions */
1469 ret
= header_ext_add(buf
, QCOW2_EXT_MAGIC_END
, NULL
, 0, buflen
);
1477 /* Backing file name */
1478 if (*bs
->backing_file
) {
1479 size_t backing_file_len
= strlen(bs
->backing_file
);
1481 if (buflen
< backing_file_len
) {
1486 /* Using strncpy is ok here, since buf is not NUL-terminated. */
1487 strncpy(buf
, bs
->backing_file
, buflen
);
1489 header
->backing_file_offset
= cpu_to_be64(buf
- ((char*) header
));
1490 header
->backing_file_size
= cpu_to_be32(backing_file_len
);
1493 /* Write the new header */
1494 ret
= bdrv_pwrite(bs
->file
, 0, header
, s
->cluster_size
);
1505 static int qcow2_change_backing_file(BlockDriverState
*bs
,
1506 const char *backing_file
, const char *backing_fmt
)
1508 pstrcpy(bs
->backing_file
, sizeof(bs
->backing_file
), backing_file
?: "");
1509 pstrcpy(bs
->backing_format
, sizeof(bs
->backing_format
), backing_fmt
?: "");
1511 return qcow2_update_header(bs
);
1514 static int preallocate(BlockDriverState
*bs
)
1516 uint64_t nb_sectors
;
1518 uint64_t host_offset
= 0;
1523 nb_sectors
= bdrv_getlength(bs
) >> BDRV_SECTOR_BITS
;
1526 while (nb_sectors
) {
1527 num
= MIN(nb_sectors
, INT_MAX
>> BDRV_SECTOR_BITS
);
1528 ret
= qcow2_alloc_cluster_offset(bs
, offset
, &num
,
1529 &host_offset
, &meta
);
1535 ret
= qcow2_alloc_cluster_link_l2(bs
, meta
);
1537 qcow2_free_any_clusters(bs
, meta
->alloc_offset
,
1538 meta
->nb_clusters
, QCOW2_DISCARD_NEVER
);
1542 /* There are no dependent requests, but we need to remove our
1543 * request from the list of in-flight requests */
1544 QLIST_REMOVE(meta
, next_in_flight
);
1547 /* TODO Preallocate data if requested */
1550 offset
+= num
<< BDRV_SECTOR_BITS
;
1554 * It is expected that the image file is large enough to actually contain
1555 * all of the allocated clusters (otherwise we get failing reads after
1556 * EOF). Extend the image to the last allocated sector.
1558 if (host_offset
!= 0) {
1559 uint8_t buf
[BDRV_SECTOR_SIZE
];
1560 memset(buf
, 0, BDRV_SECTOR_SIZE
);
1561 ret
= bdrv_write(bs
->file
, (host_offset
>> BDRV_SECTOR_BITS
) + num
- 1,
1571 static int qcow2_create2(const char *filename
, int64_t total_size
,
1572 const char *backing_file
, const char *backing_format
,
1573 int flags
, size_t cluster_size
, int prealloc
,
1574 QEMUOptionParameter
*options
, int version
,
1577 /* Calculate cluster_bits */
1579 cluster_bits
= ffs(cluster_size
) - 1;
1580 if (cluster_bits
< MIN_CLUSTER_BITS
|| cluster_bits
> MAX_CLUSTER_BITS
||
1581 (1 << cluster_bits
) != cluster_size
)
1583 error_setg(errp
, "Cluster size must be a power of two between %d and "
1584 "%dk", 1 << MIN_CLUSTER_BITS
, 1 << (MAX_CLUSTER_BITS
- 10));
1589 * Open the image file and write a minimal qcow2 header.
1591 * We keep things simple and start with a zero-sized image. We also
1592 * do without refcount blocks or a L1 table for now. We'll fix the
1593 * inconsistency later.
1595 * We do need a refcount table because growing the refcount table means
1596 * allocating two new refcount blocks - the seconds of which would be at
1597 * 2 GB for 64k clusters, and we don't want to have a 2 GB initial file
1598 * size for any qcow2 image.
1600 BlockDriverState
* bs
;
1602 uint8_t* refcount_table
;
1603 Error
*local_err
= NULL
;
1606 ret
= bdrv_create_file(filename
, options
, &local_err
);
1608 error_propagate(errp
, local_err
);
1613 ret
= bdrv_open(&bs
, filename
, NULL
, NULL
, BDRV_O_RDWR
| BDRV_O_PROTOCOL
,
1616 error_propagate(errp
, local_err
);
1620 /* Write the header */
1621 QEMU_BUILD_BUG_ON((1 << MIN_CLUSTER_BITS
) < sizeof(*header
));
1622 header
= g_malloc0(cluster_size
);
1623 *header
= (QCowHeader
) {
1624 .magic
= cpu_to_be32(QCOW_MAGIC
),
1625 .version
= cpu_to_be32(version
),
1626 .cluster_bits
= cpu_to_be32(cluster_bits
),
1627 .size
= cpu_to_be64(0),
1628 .l1_table_offset
= cpu_to_be64(0),
1629 .l1_size
= cpu_to_be32(0),
1630 .refcount_table_offset
= cpu_to_be64(cluster_size
),
1631 .refcount_table_clusters
= cpu_to_be32(1),
1632 .refcount_order
= cpu_to_be32(3 + REFCOUNT_SHIFT
),
1633 .header_length
= cpu_to_be32(sizeof(*header
)),
1636 if (flags
& BLOCK_FLAG_ENCRYPT
) {
1637 header
->crypt_method
= cpu_to_be32(QCOW_CRYPT_AES
);
1639 header
->crypt_method
= cpu_to_be32(QCOW_CRYPT_NONE
);
1642 if (flags
& BLOCK_FLAG_LAZY_REFCOUNTS
) {
1643 header
->compatible_features
|=
1644 cpu_to_be64(QCOW2_COMPAT_LAZY_REFCOUNTS
);
1647 ret
= bdrv_pwrite(bs
, 0, header
, cluster_size
);
1650 error_setg_errno(errp
, -ret
, "Could not write qcow2 header");
1654 /* Write an empty refcount table */
1655 refcount_table
= g_malloc0(cluster_size
);
1656 ret
= bdrv_pwrite(bs
, cluster_size
, refcount_table
, cluster_size
);
1657 g_free(refcount_table
);
1660 error_setg_errno(errp
, -ret
, "Could not write refcount table");
1668 * And now open the image and make it consistent first (i.e. increase the
1669 * refcount of the cluster that is occupied by the header and the refcount
1672 BlockDriver
* drv
= bdrv_find_format("qcow2");
1673 assert(drv
!= NULL
);
1674 ret
= bdrv_open(&bs
, filename
, NULL
, NULL
,
1675 BDRV_O_RDWR
| BDRV_O_CACHE_WB
| BDRV_O_NO_FLUSH
, drv
, &local_err
);
1677 error_propagate(errp
, local_err
);
1681 ret
= qcow2_alloc_clusters(bs
, 2 * cluster_size
);
1683 error_setg_errno(errp
, -ret
, "Could not allocate clusters for qcow2 "
1684 "header and refcount table");
1687 } else if (ret
!= 0) {
1688 error_report("Huh, first cluster in empty image is already in use?");
1692 /* Okay, now that we have a valid image, let's give it the right size */
1693 ret
= bdrv_truncate(bs
, total_size
* BDRV_SECTOR_SIZE
);
1695 error_setg_errno(errp
, -ret
, "Could not resize image");
1699 /* Want a backing file? There you go.*/
1701 ret
= bdrv_change_backing_file(bs
, backing_file
, backing_format
);
1703 error_setg_errno(errp
, -ret
, "Could not assign backing file '%s' "
1704 "with format '%s'", backing_file
, backing_format
);
1709 /* And if we're supposed to preallocate metadata, do that now */
1711 BDRVQcowState
*s
= bs
->opaque
;
1712 qemu_co_mutex_lock(&s
->lock
);
1713 ret
= preallocate(bs
);
1714 qemu_co_mutex_unlock(&s
->lock
);
1716 error_setg_errno(errp
, -ret
, "Could not preallocate metadata");
1724 /* Reopen the image without BDRV_O_NO_FLUSH to flush it before returning */
1725 ret
= bdrv_open(&bs
, filename
, NULL
, NULL
,
1726 BDRV_O_RDWR
| BDRV_O_CACHE_WB
| BDRV_O_NO_BACKING
,
1729 error_propagate(errp
, local_err
);
1741 static int qcow2_create(const char *filename
, QEMUOptionParameter
*options
,
1744 const char *backing_file
= NULL
;
1745 const char *backing_fmt
= NULL
;
1746 uint64_t sectors
= 0;
1748 size_t cluster_size
= DEFAULT_CLUSTER_SIZE
;
1751 Error
*local_err
= NULL
;
1754 /* Read out options */
1755 while (options
&& options
->name
) {
1756 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
1757 sectors
= options
->value
.n
/ 512;
1758 } else if (!strcmp(options
->name
, BLOCK_OPT_BACKING_FILE
)) {
1759 backing_file
= options
->value
.s
;
1760 } else if (!strcmp(options
->name
, BLOCK_OPT_BACKING_FMT
)) {
1761 backing_fmt
= options
->value
.s
;
1762 } else if (!strcmp(options
->name
, BLOCK_OPT_ENCRYPT
)) {
1763 flags
|= options
->value
.n
? BLOCK_FLAG_ENCRYPT
: 0;
1764 } else if (!strcmp(options
->name
, BLOCK_OPT_CLUSTER_SIZE
)) {
1765 if (options
->value
.n
) {
1766 cluster_size
= options
->value
.n
;
1768 } else if (!strcmp(options
->name
, BLOCK_OPT_PREALLOC
)) {
1769 if (!options
->value
.s
|| !strcmp(options
->value
.s
, "off")) {
1771 } else if (!strcmp(options
->value
.s
, "metadata")) {
1774 error_setg(errp
, "Invalid preallocation mode: '%s'",
1778 } else if (!strcmp(options
->name
, BLOCK_OPT_COMPAT_LEVEL
)) {
1779 if (!options
->value
.s
) {
1780 /* keep the default */
1781 } else if (!strcmp(options
->value
.s
, "0.10")) {
1783 } else if (!strcmp(options
->value
.s
, "1.1")) {
1786 error_setg(errp
, "Invalid compatibility level: '%s'",
1790 } else if (!strcmp(options
->name
, BLOCK_OPT_LAZY_REFCOUNTS
)) {
1791 flags
|= options
->value
.n
? BLOCK_FLAG_LAZY_REFCOUNTS
: 0;
1796 if (backing_file
&& prealloc
) {
1797 error_setg(errp
, "Backing file and preallocation cannot be used at "
1802 if (version
< 3 && (flags
& BLOCK_FLAG_LAZY_REFCOUNTS
)) {
1803 error_setg(errp
, "Lazy refcounts only supported with compatibility "
1804 "level 1.1 and above (use compat=1.1 or greater)");
1808 ret
= qcow2_create2(filename
, sectors
, backing_file
, backing_fmt
, flags
,
1809 cluster_size
, prealloc
, options
, version
, &local_err
);
1811 error_propagate(errp
, local_err
);
1816 static coroutine_fn
int qcow2_co_write_zeroes(BlockDriverState
*bs
,
1817 int64_t sector_num
, int nb_sectors
, BdrvRequestFlags flags
)
1820 BDRVQcowState
*s
= bs
->opaque
;
1822 /* Emulate misaligned zero writes */
1823 if (sector_num
% s
->cluster_sectors
|| nb_sectors
% s
->cluster_sectors
) {
1827 /* Whatever is left can use real zero clusters */
1828 qemu_co_mutex_lock(&s
->lock
);
1829 ret
= qcow2_zero_clusters(bs
, sector_num
<< BDRV_SECTOR_BITS
,
1831 qemu_co_mutex_unlock(&s
->lock
);
1836 static coroutine_fn
int qcow2_co_discard(BlockDriverState
*bs
,
1837 int64_t sector_num
, int nb_sectors
)
1840 BDRVQcowState
*s
= bs
->opaque
;
1842 qemu_co_mutex_lock(&s
->lock
);
1843 ret
= qcow2_discard_clusters(bs
, sector_num
<< BDRV_SECTOR_BITS
,
1844 nb_sectors
, QCOW2_DISCARD_REQUEST
);
1845 qemu_co_mutex_unlock(&s
->lock
);
1849 static int qcow2_truncate(BlockDriverState
*bs
, int64_t offset
)
1851 BDRVQcowState
*s
= bs
->opaque
;
1852 int64_t new_l1_size
;
1856 error_report("The new size must be a multiple of 512");
1860 /* cannot proceed if image has snapshots */
1861 if (s
->nb_snapshots
) {
1862 error_report("Can't resize an image which has snapshots");
1866 /* shrinking is currently not supported */
1867 if (offset
< bs
->total_sectors
* 512) {
1868 error_report("qcow2 doesn't support shrinking images yet");
1872 new_l1_size
= size_to_l1(s
, offset
);
1873 ret
= qcow2_grow_l1_table(bs
, new_l1_size
, true);
1878 /* write updated header.size */
1879 offset
= cpu_to_be64(offset
);
1880 ret
= bdrv_pwrite_sync(bs
->file
, offsetof(QCowHeader
, size
),
1881 &offset
, sizeof(uint64_t));
1886 s
->l1_vm_state_index
= new_l1_size
;
1890 /* XXX: put compressed sectors first, then all the cluster aligned
1891 tables to avoid losing bytes in alignment */
1892 static int qcow2_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
1893 const uint8_t *buf
, int nb_sectors
)
1895 BDRVQcowState
*s
= bs
->opaque
;
1899 uint64_t cluster_offset
;
1901 if (nb_sectors
== 0) {
1902 /* align end of file to a sector boundary to ease reading with
1903 sector based I/Os */
1904 cluster_offset
= bdrv_getlength(bs
->file
);
1905 cluster_offset
= (cluster_offset
+ 511) & ~511;
1906 bdrv_truncate(bs
->file
, cluster_offset
);
1910 if (nb_sectors
!= s
->cluster_sectors
) {
1913 /* Zero-pad last write if image size is not cluster aligned */
1914 if (sector_num
+ nb_sectors
== bs
->total_sectors
&&
1915 nb_sectors
< s
->cluster_sectors
) {
1916 uint8_t *pad_buf
= qemu_blockalign(bs
, s
->cluster_size
);
1917 memset(pad_buf
, 0, s
->cluster_size
);
1918 memcpy(pad_buf
, buf
, nb_sectors
* BDRV_SECTOR_SIZE
);
1919 ret
= qcow2_write_compressed(bs
, sector_num
,
1920 pad_buf
, s
->cluster_sectors
);
1921 qemu_vfree(pad_buf
);
1926 out_buf
= g_malloc(s
->cluster_size
+ (s
->cluster_size
/ 1000) + 128);
1928 /* best compression, small window, no zlib header */
1929 memset(&strm
, 0, sizeof(strm
));
1930 ret
= deflateInit2(&strm
, Z_DEFAULT_COMPRESSION
,
1932 9, Z_DEFAULT_STRATEGY
);
1938 strm
.avail_in
= s
->cluster_size
;
1939 strm
.next_in
= (uint8_t *)buf
;
1940 strm
.avail_out
= s
->cluster_size
;
1941 strm
.next_out
= out_buf
;
1943 ret
= deflate(&strm
, Z_FINISH
);
1944 if (ret
!= Z_STREAM_END
&& ret
!= Z_OK
) {
1949 out_len
= strm
.next_out
- out_buf
;
1953 if (ret
!= Z_STREAM_END
|| out_len
>= s
->cluster_size
) {
1954 /* could not compress: write normal cluster */
1955 ret
= bdrv_write(bs
, sector_num
, buf
, s
->cluster_sectors
);
1960 cluster_offset
= qcow2_alloc_compressed_cluster_offset(bs
,
1961 sector_num
<< 9, out_len
);
1962 if (!cluster_offset
) {
1966 cluster_offset
&= s
->cluster_offset_mask
;
1968 ret
= qcow2_pre_write_overlap_check(bs
, 0, cluster_offset
, out_len
);
1973 BLKDBG_EVENT(bs
->file
, BLKDBG_WRITE_COMPRESSED
);
1974 ret
= bdrv_pwrite(bs
->file
, cluster_offset
, out_buf
, out_len
);
1986 static coroutine_fn
int qcow2_co_flush_to_os(BlockDriverState
*bs
)
1988 BDRVQcowState
*s
= bs
->opaque
;
1991 qemu_co_mutex_lock(&s
->lock
);
1992 ret
= qcow2_cache_flush(bs
, s
->l2_table_cache
);
1994 qemu_co_mutex_unlock(&s
->lock
);
1998 if (qcow2_need_accurate_refcounts(s
)) {
1999 ret
= qcow2_cache_flush(bs
, s
->refcount_block_cache
);
2001 qemu_co_mutex_unlock(&s
->lock
);
2005 qemu_co_mutex_unlock(&s
->lock
);
2010 static int qcow2_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
2012 BDRVQcowState
*s
= bs
->opaque
;
2013 bdi
->unallocated_blocks_are_zero
= true;
2014 bdi
->can_write_zeroes_with_unmap
= (s
->qcow_version
>= 3);
2015 bdi
->cluster_size
= s
->cluster_size
;
2016 bdi
->vm_state_offset
= qcow2_vm_state_offset(s
);
2020 static ImageInfoSpecific
*qcow2_get_specific_info(BlockDriverState
*bs
)
2022 BDRVQcowState
*s
= bs
->opaque
;
2023 ImageInfoSpecific
*spec_info
= g_new(ImageInfoSpecific
, 1);
2025 *spec_info
= (ImageInfoSpecific
){
2026 .kind
= IMAGE_INFO_SPECIFIC_KIND_QCOW2
,
2028 .qcow2
= g_new(ImageInfoSpecificQCow2
, 1),
2031 if (s
->qcow_version
== 2) {
2032 *spec_info
->qcow2
= (ImageInfoSpecificQCow2
){
2033 .compat
= g_strdup("0.10"),
2035 } else if (s
->qcow_version
== 3) {
2036 *spec_info
->qcow2
= (ImageInfoSpecificQCow2
){
2037 .compat
= g_strdup("1.1"),
2038 .lazy_refcounts
= s
->compatible_features
&
2039 QCOW2_COMPAT_LAZY_REFCOUNTS
,
2040 .has_lazy_refcounts
= true,
2048 static void dump_refcounts(BlockDriverState
*bs
)
2050 BDRVQcowState
*s
= bs
->opaque
;
2051 int64_t nb_clusters
, k
, k1
, size
;
2054 size
= bdrv_getlength(bs
->file
);
2055 nb_clusters
= size_to_clusters(s
, size
);
2056 for(k
= 0; k
< nb_clusters
;) {
2058 refcount
= get_refcount(bs
, k
);
2060 while (k
< nb_clusters
&& get_refcount(bs
, k
) == refcount
)
2062 printf("%" PRId64
": refcount=%d nb=%" PRId64
"\n", k
, refcount
,
2068 static int qcow2_save_vmstate(BlockDriverState
*bs
, QEMUIOVector
*qiov
,
2071 BDRVQcowState
*s
= bs
->opaque
;
2072 int64_t total_sectors
= bs
->total_sectors
;
2073 int growable
= bs
->growable
;
2074 bool zero_beyond_eof
= bs
->zero_beyond_eof
;
2077 BLKDBG_EVENT(bs
->file
, BLKDBG_VMSTATE_SAVE
);
2079 bs
->zero_beyond_eof
= false;
2080 ret
= bdrv_pwritev(bs
, qcow2_vm_state_offset(s
) + pos
, qiov
);
2081 bs
->growable
= growable
;
2082 bs
->zero_beyond_eof
= zero_beyond_eof
;
2084 /* bdrv_co_do_writev will have increased the total_sectors value to include
2085 * the VM state - the VM state is however not an actual part of the block
2086 * device, therefore, we need to restore the old value. */
2087 bs
->total_sectors
= total_sectors
;
2092 static int qcow2_load_vmstate(BlockDriverState
*bs
, uint8_t *buf
,
2093 int64_t pos
, int size
)
2095 BDRVQcowState
*s
= bs
->opaque
;
2096 int growable
= bs
->growable
;
2097 bool zero_beyond_eof
= bs
->zero_beyond_eof
;
2100 BLKDBG_EVENT(bs
->file
, BLKDBG_VMSTATE_LOAD
);
2102 bs
->zero_beyond_eof
= false;
2103 ret
= bdrv_pread(bs
, qcow2_vm_state_offset(s
) + pos
, buf
, size
);
2104 bs
->growable
= growable
;
2105 bs
->zero_beyond_eof
= zero_beyond_eof
;
2111 * Downgrades an image's version. To achieve this, any incompatible features
2112 * have to be removed.
2114 static int qcow2_downgrade(BlockDriverState
*bs
, int target_version
)
2116 BDRVQcowState
*s
= bs
->opaque
;
2117 int current_version
= s
->qcow_version
;
2120 if (target_version
== current_version
) {
2122 } else if (target_version
> current_version
) {
2124 } else if (target_version
!= 2) {
2128 if (s
->refcount_order
!= 4) {
2129 /* we would have to convert the image to a refcount_order == 4 image
2130 * here; however, since qemu (at the time of writing this) does not
2131 * support anything different than 4 anyway, there is no point in doing
2132 * so right now; however, we should error out (if qemu supports this in
2133 * the future and this code has not been adapted) */
2134 error_report("qcow2_downgrade: Image refcount orders other than 4 are "
2135 "currently not supported.");
2139 /* clear incompatible features */
2140 if (s
->incompatible_features
& QCOW2_INCOMPAT_DIRTY
) {
2141 ret
= qcow2_mark_clean(bs
);
2147 /* with QCOW2_INCOMPAT_CORRUPT, it is pretty much impossible to get here in
2148 * the first place; if that happens nonetheless, returning -ENOTSUP is the
2149 * best thing to do anyway */
2151 if (s
->incompatible_features
) {
2155 /* since we can ignore compatible features, we can set them to 0 as well */
2156 s
->compatible_features
= 0;
2157 /* if lazy refcounts have been used, they have already been fixed through
2158 * clearing the dirty flag */
2160 /* clearing autoclear features is trivial */
2161 s
->autoclear_features
= 0;
2163 ret
= qcow2_expand_zero_clusters(bs
);
2168 s
->qcow_version
= target_version
;
2169 ret
= qcow2_update_header(bs
);
2171 s
->qcow_version
= current_version
;
2177 static int qcow2_amend_options(BlockDriverState
*bs
,
2178 QEMUOptionParameter
*options
)
2180 BDRVQcowState
*s
= bs
->opaque
;
2181 int old_version
= s
->qcow_version
, new_version
= old_version
;
2182 uint64_t new_size
= 0;
2183 const char *backing_file
= NULL
, *backing_format
= NULL
;
2184 bool lazy_refcounts
= s
->use_lazy_refcounts
;
2188 for (i
= 0; options
[i
].name
; i
++)
2190 if (!options
[i
].assigned
) {
2191 /* only change explicitly defined options */
2195 if (!strcmp(options
[i
].name
, "compat")) {
2196 if (!options
[i
].value
.s
) {
2197 /* preserve default */
2198 } else if (!strcmp(options
[i
].value
.s
, "0.10")) {
2200 } else if (!strcmp(options
[i
].value
.s
, "1.1")) {
2203 fprintf(stderr
, "Unknown compatibility level %s.\n",
2204 options
[i
].value
.s
);
2207 } else if (!strcmp(options
[i
].name
, "preallocation")) {
2208 fprintf(stderr
, "Cannot change preallocation mode.\n");
2210 } else if (!strcmp(options
[i
].name
, "size")) {
2211 new_size
= options
[i
].value
.n
;
2212 } else if (!strcmp(options
[i
].name
, "backing_file")) {
2213 backing_file
= options
[i
].value
.s
;
2214 } else if (!strcmp(options
[i
].name
, "backing_fmt")) {
2215 backing_format
= options
[i
].value
.s
;
2216 } else if (!strcmp(options
[i
].name
, "encryption")) {
2217 if ((options
[i
].value
.n
!= !!s
->crypt_method
)) {
2218 fprintf(stderr
, "Changing the encryption flag is not "
2222 } else if (!strcmp(options
[i
].name
, "cluster_size")) {
2223 if (options
[i
].value
.n
!= s
->cluster_size
) {
2224 fprintf(stderr
, "Changing the cluster size is not "
2228 } else if (!strcmp(options
[i
].name
, "lazy_refcounts")) {
2229 lazy_refcounts
= options
[i
].value
.n
;
2231 /* if this assertion fails, this probably means a new option was
2232 * added without having it covered here */
2237 if (new_version
!= old_version
) {
2238 if (new_version
> old_version
) {
2240 s
->qcow_version
= new_version
;
2241 ret
= qcow2_update_header(bs
);
2243 s
->qcow_version
= old_version
;
2247 ret
= qcow2_downgrade(bs
, new_version
);
2254 if (backing_file
|| backing_format
) {
2255 ret
= qcow2_change_backing_file(bs
, backing_file
?: bs
->backing_file
,
2256 backing_format
?: bs
->backing_format
);
2262 if (s
->use_lazy_refcounts
!= lazy_refcounts
) {
2263 if (lazy_refcounts
) {
2264 if (s
->qcow_version
< 3) {
2265 fprintf(stderr
, "Lazy refcounts only supported with compatibility "
2266 "level 1.1 and above (use compat=1.1 or greater)\n");
2269 s
->compatible_features
|= QCOW2_COMPAT_LAZY_REFCOUNTS
;
2270 ret
= qcow2_update_header(bs
);
2272 s
->compatible_features
&= ~QCOW2_COMPAT_LAZY_REFCOUNTS
;
2275 s
->use_lazy_refcounts
= true;
2277 /* make image clean first */
2278 ret
= qcow2_mark_clean(bs
);
2282 /* now disallow lazy refcounts */
2283 s
->compatible_features
&= ~QCOW2_COMPAT_LAZY_REFCOUNTS
;
2284 ret
= qcow2_update_header(bs
);
2286 s
->compatible_features
|= QCOW2_COMPAT_LAZY_REFCOUNTS
;
2289 s
->use_lazy_refcounts
= false;
2294 ret
= bdrv_truncate(bs
, new_size
);
2303 static QEMUOptionParameter qcow2_create_options
[] = {
2305 .name
= BLOCK_OPT_SIZE
,
2307 .help
= "Virtual disk size"
2310 .name
= BLOCK_OPT_COMPAT_LEVEL
,
2312 .help
= "Compatibility level (0.10 or 1.1)"
2315 .name
= BLOCK_OPT_BACKING_FILE
,
2317 .help
= "File name of a base image"
2320 .name
= BLOCK_OPT_BACKING_FMT
,
2322 .help
= "Image format of the base image"
2325 .name
= BLOCK_OPT_ENCRYPT
,
2327 .help
= "Encrypt the image"
2330 .name
= BLOCK_OPT_CLUSTER_SIZE
,
2332 .help
= "qcow2 cluster size",
2333 .value
= { .n
= DEFAULT_CLUSTER_SIZE
},
2336 .name
= BLOCK_OPT_PREALLOC
,
2338 .help
= "Preallocation mode (allowed values: off, metadata)"
2341 .name
= BLOCK_OPT_LAZY_REFCOUNTS
,
2343 .help
= "Postpone refcount updates",
2348 static BlockDriver bdrv_qcow2
= {
2349 .format_name
= "qcow2",
2350 .instance_size
= sizeof(BDRVQcowState
),
2351 .bdrv_probe
= qcow2_probe
,
2352 .bdrv_open
= qcow2_open
,
2353 .bdrv_close
= qcow2_close
,
2354 .bdrv_reopen_prepare
= qcow2_reopen_prepare
,
2355 .bdrv_create
= qcow2_create
,
2356 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
2357 .bdrv_co_get_block_status
= qcow2_co_get_block_status
,
2358 .bdrv_set_key
= qcow2_set_key
,
2360 .bdrv_co_readv
= qcow2_co_readv
,
2361 .bdrv_co_writev
= qcow2_co_writev
,
2362 .bdrv_co_flush_to_os
= qcow2_co_flush_to_os
,
2364 .bdrv_co_write_zeroes
= qcow2_co_write_zeroes
,
2365 .bdrv_co_discard
= qcow2_co_discard
,
2366 .bdrv_truncate
= qcow2_truncate
,
2367 .bdrv_write_compressed
= qcow2_write_compressed
,
2369 .bdrv_snapshot_create
= qcow2_snapshot_create
,
2370 .bdrv_snapshot_goto
= qcow2_snapshot_goto
,
2371 .bdrv_snapshot_delete
= qcow2_snapshot_delete
,
2372 .bdrv_snapshot_list
= qcow2_snapshot_list
,
2373 .bdrv_snapshot_load_tmp
= qcow2_snapshot_load_tmp
,
2374 .bdrv_get_info
= qcow2_get_info
,
2375 .bdrv_get_specific_info
= qcow2_get_specific_info
,
2377 .bdrv_save_vmstate
= qcow2_save_vmstate
,
2378 .bdrv_load_vmstate
= qcow2_load_vmstate
,
2380 .bdrv_change_backing_file
= qcow2_change_backing_file
,
2382 .bdrv_refresh_limits
= qcow2_refresh_limits
,
2383 .bdrv_invalidate_cache
= qcow2_invalidate_cache
,
2385 .create_options
= qcow2_create_options
,
2386 .bdrv_check
= qcow2_check
,
2387 .bdrv_amend_options
= qcow2_amend_options
,
2390 static void bdrv_qcow2_init(void)
2392 bdrv_register(&bdrv_qcow2
);
2395 block_init(bdrv_qcow2_init
);