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=%" PRIu32
128 " too large (>=%zu)", ext
.len
,
129 sizeof(bs
->backing_format
));
132 ret
= bdrv_pread(bs
->file
, offset
, bs
->backing_format
, ext
.len
);
134 error_setg_errno(errp
, -ret
, "ERROR: ext_backing_format: "
135 "Could not read format name");
138 bs
->backing_format
[ext
.len
] = '\0';
140 printf("Qcow2: Got format extension %s\n", bs
->backing_format
);
144 case QCOW2_EXT_MAGIC_FEATURE_TABLE
:
145 if (p_feature_table
!= NULL
) {
146 void* feature_table
= g_malloc0(ext
.len
+ 2 * sizeof(Qcow2Feature
));
147 ret
= bdrv_pread(bs
->file
, offset
, feature_table
, ext
.len
);
149 error_setg_errno(errp
, -ret
, "ERROR: ext_feature_table: "
150 "Could not read table");
154 *p_feature_table
= feature_table
;
159 /* unknown magic - save it in case we need to rewrite the header */
161 Qcow2UnknownHeaderExtension
*uext
;
163 uext
= g_malloc0(sizeof(*uext
) + ext
.len
);
164 uext
->magic
= ext
.magic
;
166 QLIST_INSERT_HEAD(&s
->unknown_header_ext
, uext
, next
);
168 ret
= bdrv_pread(bs
->file
, offset
, uext
->data
, uext
->len
);
170 error_setg_errno(errp
, -ret
, "ERROR: unknown extension: "
171 "Could not read data");
178 offset
+= ((ext
.len
+ 7) & ~7);
184 static void cleanup_unknown_header_ext(BlockDriverState
*bs
)
186 BDRVQcowState
*s
= bs
->opaque
;
187 Qcow2UnknownHeaderExtension
*uext
, *next
;
189 QLIST_FOREACH_SAFE(uext
, &s
->unknown_header_ext
, next
, next
) {
190 QLIST_REMOVE(uext
, next
);
195 static void GCC_FMT_ATTR(3, 4) report_unsupported(BlockDriverState
*bs
,
196 Error
**errp
, const char *fmt
, ...)
202 vsnprintf(msg
, sizeof(msg
), fmt
, ap
);
205 error_set(errp
, QERR_UNKNOWN_BLOCK_FORMAT_FEATURE
, bs
->device_name
, "qcow2",
209 static void report_unsupported_feature(BlockDriverState
*bs
,
210 Error
**errp
, Qcow2Feature
*table
, uint64_t mask
)
212 while (table
&& table
->name
[0] != '\0') {
213 if (table
->type
== QCOW2_FEAT_TYPE_INCOMPATIBLE
) {
214 if (mask
& (1 << table
->bit
)) {
215 report_unsupported(bs
, errp
, "%.46s", table
->name
);
216 mask
&= ~(1 << table
->bit
);
223 report_unsupported(bs
, errp
, "Unknown incompatible feature: %" PRIx64
,
229 * Sets the dirty bit and flushes afterwards if necessary.
231 * The incompatible_features bit is only set if the image file header was
232 * updated successfully. Therefore it is not required to check the return
233 * value of this function.
235 int qcow2_mark_dirty(BlockDriverState
*bs
)
237 BDRVQcowState
*s
= bs
->opaque
;
241 assert(s
->qcow_version
>= 3);
243 if (s
->incompatible_features
& QCOW2_INCOMPAT_DIRTY
) {
244 return 0; /* already dirty */
247 val
= cpu_to_be64(s
->incompatible_features
| QCOW2_INCOMPAT_DIRTY
);
248 ret
= bdrv_pwrite(bs
->file
, offsetof(QCowHeader
, incompatible_features
),
253 ret
= bdrv_flush(bs
->file
);
258 /* Only treat image as dirty if the header was updated successfully */
259 s
->incompatible_features
|= QCOW2_INCOMPAT_DIRTY
;
264 * Clears the dirty bit and flushes before if necessary. Only call this
265 * function when there are no pending requests, it does not guard against
266 * concurrent requests dirtying the image.
268 static int qcow2_mark_clean(BlockDriverState
*bs
)
270 BDRVQcowState
*s
= bs
->opaque
;
272 if (s
->incompatible_features
& QCOW2_INCOMPAT_DIRTY
) {
275 s
->incompatible_features
&= ~QCOW2_INCOMPAT_DIRTY
;
277 ret
= bdrv_flush(bs
);
282 return qcow2_update_header(bs
);
288 * Marks the image as corrupt.
290 int qcow2_mark_corrupt(BlockDriverState
*bs
)
292 BDRVQcowState
*s
= bs
->opaque
;
294 s
->incompatible_features
|= QCOW2_INCOMPAT_CORRUPT
;
295 return qcow2_update_header(bs
);
299 * Marks the image as consistent, i.e., unsets the corrupt bit, and flushes
300 * before if necessary.
302 int qcow2_mark_consistent(BlockDriverState
*bs
)
304 BDRVQcowState
*s
= bs
->opaque
;
306 if (s
->incompatible_features
& QCOW2_INCOMPAT_CORRUPT
) {
307 int ret
= bdrv_flush(bs
);
312 s
->incompatible_features
&= ~QCOW2_INCOMPAT_CORRUPT
;
313 return qcow2_update_header(bs
);
318 static int qcow2_check(BlockDriverState
*bs
, BdrvCheckResult
*result
,
321 int ret
= qcow2_check_refcounts(bs
, result
, fix
);
326 if (fix
&& result
->check_errors
== 0 && result
->corruptions
== 0) {
327 ret
= qcow2_mark_clean(bs
);
331 return qcow2_mark_consistent(bs
);
336 static int validate_table_offset(BlockDriverState
*bs
, uint64_t offset
,
337 uint64_t entries
, size_t entry_len
)
339 BDRVQcowState
*s
= bs
->opaque
;
342 /* Use signed INT64_MAX as the maximum even for uint64_t header fields,
343 * because values will be passed to qemu functions taking int64_t. */
344 if (entries
> INT64_MAX
/ entry_len
) {
348 size
= entries
* entry_len
;
350 if (INT64_MAX
- size
< offset
) {
354 /* Tables must be cluster aligned */
355 if (offset
& (s
->cluster_size
- 1)) {
362 static QemuOptsList qcow2_runtime_opts
= {
364 .head
= QTAILQ_HEAD_INITIALIZER(qcow2_runtime_opts
.head
),
367 .name
= QCOW2_OPT_LAZY_REFCOUNTS
,
368 .type
= QEMU_OPT_BOOL
,
369 .help
= "Postpone refcount updates",
372 .name
= QCOW2_OPT_DISCARD_REQUEST
,
373 .type
= QEMU_OPT_BOOL
,
374 .help
= "Pass guest discard requests to the layer below",
377 .name
= QCOW2_OPT_DISCARD_SNAPSHOT
,
378 .type
= QEMU_OPT_BOOL
,
379 .help
= "Generate discard requests when snapshot related space "
383 .name
= QCOW2_OPT_DISCARD_OTHER
,
384 .type
= QEMU_OPT_BOOL
,
385 .help
= "Generate discard requests when other clusters are freed",
388 .name
= QCOW2_OPT_OVERLAP
,
389 .type
= QEMU_OPT_STRING
,
390 .help
= "Selects which overlap checks to perform from a range of "
391 "templates (none, constant, cached, all)",
394 .name
= QCOW2_OPT_OVERLAP_MAIN_HEADER
,
395 .type
= QEMU_OPT_BOOL
,
396 .help
= "Check for unintended writes into the main qcow2 header",
399 .name
= QCOW2_OPT_OVERLAP_ACTIVE_L1
,
400 .type
= QEMU_OPT_BOOL
,
401 .help
= "Check for unintended writes into the active L1 table",
404 .name
= QCOW2_OPT_OVERLAP_ACTIVE_L2
,
405 .type
= QEMU_OPT_BOOL
,
406 .help
= "Check for unintended writes into an active L2 table",
409 .name
= QCOW2_OPT_OVERLAP_REFCOUNT_TABLE
,
410 .type
= QEMU_OPT_BOOL
,
411 .help
= "Check for unintended writes into the refcount table",
414 .name
= QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK
,
415 .type
= QEMU_OPT_BOOL
,
416 .help
= "Check for unintended writes into a refcount block",
419 .name
= QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE
,
420 .type
= QEMU_OPT_BOOL
,
421 .help
= "Check for unintended writes into the snapshot table",
424 .name
= QCOW2_OPT_OVERLAP_INACTIVE_L1
,
425 .type
= QEMU_OPT_BOOL
,
426 .help
= "Check for unintended writes into an inactive L1 table",
429 .name
= QCOW2_OPT_OVERLAP_INACTIVE_L2
,
430 .type
= QEMU_OPT_BOOL
,
431 .help
= "Check for unintended writes into an inactive L2 table",
433 { /* end of list */ }
437 static const char *overlap_bool_option_names
[QCOW2_OL_MAX_BITNR
] = {
438 [QCOW2_OL_MAIN_HEADER_BITNR
] = QCOW2_OPT_OVERLAP_MAIN_HEADER
,
439 [QCOW2_OL_ACTIVE_L1_BITNR
] = QCOW2_OPT_OVERLAP_ACTIVE_L1
,
440 [QCOW2_OL_ACTIVE_L2_BITNR
] = QCOW2_OPT_OVERLAP_ACTIVE_L2
,
441 [QCOW2_OL_REFCOUNT_TABLE_BITNR
] = QCOW2_OPT_OVERLAP_REFCOUNT_TABLE
,
442 [QCOW2_OL_REFCOUNT_BLOCK_BITNR
] = QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK
,
443 [QCOW2_OL_SNAPSHOT_TABLE_BITNR
] = QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE
,
444 [QCOW2_OL_INACTIVE_L1_BITNR
] = QCOW2_OPT_OVERLAP_INACTIVE_L1
,
445 [QCOW2_OL_INACTIVE_L2_BITNR
] = QCOW2_OPT_OVERLAP_INACTIVE_L2
,
448 static int qcow2_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
451 BDRVQcowState
*s
= bs
->opaque
;
456 Error
*local_err
= NULL
;
458 uint64_t l1_vm_state_index
;
459 const char *opt_overlap_check
;
460 int overlap_check_template
= 0;
462 ret
= bdrv_pread(bs
->file
, 0, &header
, sizeof(header
));
464 error_setg_errno(errp
, -ret
, "Could not read qcow2 header");
467 be32_to_cpus(&header
.magic
);
468 be32_to_cpus(&header
.version
);
469 be64_to_cpus(&header
.backing_file_offset
);
470 be32_to_cpus(&header
.backing_file_size
);
471 be64_to_cpus(&header
.size
);
472 be32_to_cpus(&header
.cluster_bits
);
473 be32_to_cpus(&header
.crypt_method
);
474 be64_to_cpus(&header
.l1_table_offset
);
475 be32_to_cpus(&header
.l1_size
);
476 be64_to_cpus(&header
.refcount_table_offset
);
477 be32_to_cpus(&header
.refcount_table_clusters
);
478 be64_to_cpus(&header
.snapshots_offset
);
479 be32_to_cpus(&header
.nb_snapshots
);
481 if (header
.magic
!= QCOW_MAGIC
) {
482 error_setg(errp
, "Image is not in qcow2 format");
486 if (header
.version
< 2 || header
.version
> 3) {
487 report_unsupported(bs
, errp
, "QCOW version %" PRIu32
, header
.version
);
492 s
->qcow_version
= header
.version
;
494 /* Initialise cluster size */
495 if (header
.cluster_bits
< MIN_CLUSTER_BITS
||
496 header
.cluster_bits
> MAX_CLUSTER_BITS
) {
497 error_setg(errp
, "Unsupported cluster size: 2^%" PRIu32
,
498 header
.cluster_bits
);
503 s
->cluster_bits
= header
.cluster_bits
;
504 s
->cluster_size
= 1 << s
->cluster_bits
;
505 s
->cluster_sectors
= 1 << (s
->cluster_bits
- 9);
507 /* Initialise version 3 header fields */
508 if (header
.version
== 2) {
509 header
.incompatible_features
= 0;
510 header
.compatible_features
= 0;
511 header
.autoclear_features
= 0;
512 header
.refcount_order
= 4;
513 header
.header_length
= 72;
515 be64_to_cpus(&header
.incompatible_features
);
516 be64_to_cpus(&header
.compatible_features
);
517 be64_to_cpus(&header
.autoclear_features
);
518 be32_to_cpus(&header
.refcount_order
);
519 be32_to_cpus(&header
.header_length
);
521 if (header
.header_length
< 104) {
522 error_setg(errp
, "qcow2 header too short");
528 if (header
.header_length
> s
->cluster_size
) {
529 error_setg(errp
, "qcow2 header exceeds cluster size");
534 if (header
.header_length
> sizeof(header
)) {
535 s
->unknown_header_fields_size
= header
.header_length
- sizeof(header
);
536 s
->unknown_header_fields
= g_malloc(s
->unknown_header_fields_size
);
537 ret
= bdrv_pread(bs
->file
, sizeof(header
), s
->unknown_header_fields
,
538 s
->unknown_header_fields_size
);
540 error_setg_errno(errp
, -ret
, "Could not read unknown qcow2 header "
546 if (header
.backing_file_offset
> s
->cluster_size
) {
547 error_setg(errp
, "Invalid backing file offset");
552 if (header
.backing_file_offset
) {
553 ext_end
= header
.backing_file_offset
;
555 ext_end
= 1 << header
.cluster_bits
;
558 /* Handle feature bits */
559 s
->incompatible_features
= header
.incompatible_features
;
560 s
->compatible_features
= header
.compatible_features
;
561 s
->autoclear_features
= header
.autoclear_features
;
563 if (s
->incompatible_features
& ~QCOW2_INCOMPAT_MASK
) {
564 void *feature_table
= NULL
;
565 qcow2_read_extensions(bs
, header
.header_length
, ext_end
,
566 &feature_table
, NULL
);
567 report_unsupported_feature(bs
, errp
, feature_table
,
568 s
->incompatible_features
&
569 ~QCOW2_INCOMPAT_MASK
);
571 g_free(feature_table
);
575 if (s
->incompatible_features
& QCOW2_INCOMPAT_CORRUPT
) {
576 /* Corrupt images may not be written to unless they are being repaired
578 if ((flags
& BDRV_O_RDWR
) && !(flags
& BDRV_O_CHECK
)) {
579 error_setg(errp
, "qcow2: Image is corrupt; cannot be opened "
586 /* Check support for various header values */
587 if (header
.refcount_order
!= 4) {
588 report_unsupported(bs
, errp
, "%d bit reference counts",
589 1 << header
.refcount_order
);
593 s
->refcount_order
= header
.refcount_order
;
595 if (header
.crypt_method
> QCOW_CRYPT_AES
) {
596 error_setg(errp
, "Unsupported encryption method: %" PRIu32
,
597 header
.crypt_method
);
601 s
->crypt_method_header
= header
.crypt_method
;
602 if (s
->crypt_method_header
) {
606 s
->l2_bits
= s
->cluster_bits
- 3; /* L2 is always one cluster */
607 s
->l2_size
= 1 << s
->l2_bits
;
608 bs
->total_sectors
= header
.size
/ 512;
609 s
->csize_shift
= (62 - (s
->cluster_bits
- 8));
610 s
->csize_mask
= (1 << (s
->cluster_bits
- 8)) - 1;
611 s
->cluster_offset_mask
= (1LL << s
->csize_shift
) - 1;
613 s
->refcount_table_offset
= header
.refcount_table_offset
;
614 s
->refcount_table_size
=
615 header
.refcount_table_clusters
<< (s
->cluster_bits
- 3);
617 if (header
.refcount_table_clusters
> qcow2_max_refcount_clusters(s
)) {
618 error_setg(errp
, "Reference count table too large");
623 ret
= validate_table_offset(bs
, s
->refcount_table_offset
,
624 s
->refcount_table_size
, sizeof(uint64_t));
626 error_setg(errp
, "Invalid reference count table offset");
630 /* Snapshot table offset/length */
631 if (header
.nb_snapshots
> QCOW_MAX_SNAPSHOTS
) {
632 error_setg(errp
, "Too many snapshots");
637 ret
= validate_table_offset(bs
, header
.snapshots_offset
,
639 sizeof(QCowSnapshotHeader
));
641 error_setg(errp
, "Invalid snapshot table offset");
645 /* read the level 1 table */
646 if (header
.l1_size
> QCOW_MAX_L1_SIZE
) {
647 error_setg(errp
, "Active L1 table too large");
651 s
->l1_size
= header
.l1_size
;
653 l1_vm_state_index
= size_to_l1(s
, header
.size
);
654 if (l1_vm_state_index
> INT_MAX
) {
655 error_setg(errp
, "Image is too big");
659 s
->l1_vm_state_index
= l1_vm_state_index
;
661 /* the L1 table must contain at least enough entries to put
663 if (s
->l1_size
< s
->l1_vm_state_index
) {
664 error_setg(errp
, "L1 table is too small");
669 ret
= validate_table_offset(bs
, header
.l1_table_offset
,
670 header
.l1_size
, sizeof(uint64_t));
672 error_setg(errp
, "Invalid L1 table offset");
675 s
->l1_table_offset
= header
.l1_table_offset
;
678 if (s
->l1_size
> 0) {
679 s
->l1_table
= g_malloc0(
680 align_offset(s
->l1_size
* sizeof(uint64_t), 512));
681 ret
= bdrv_pread(bs
->file
, s
->l1_table_offset
, s
->l1_table
,
682 s
->l1_size
* sizeof(uint64_t));
684 error_setg_errno(errp
, -ret
, "Could not read L1 table");
687 for(i
= 0;i
< s
->l1_size
; i
++) {
688 be64_to_cpus(&s
->l1_table
[i
]);
692 /* alloc L2 table/refcount block cache */
693 s
->l2_table_cache
= qcow2_cache_create(bs
, L2_CACHE_SIZE
);
694 s
->refcount_block_cache
= qcow2_cache_create(bs
, REFCOUNT_CACHE_SIZE
);
696 s
->cluster_cache
= g_malloc(s
->cluster_size
);
697 /* one more sector for decompressed data alignment */
698 s
->cluster_data
= qemu_blockalign(bs
, QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_size
700 s
->cluster_cache_offset
= -1;
703 ret
= qcow2_refcount_init(bs
);
705 error_setg_errno(errp
, -ret
, "Could not initialize refcount handling");
709 QLIST_INIT(&s
->cluster_allocs
);
710 QTAILQ_INIT(&s
->discards
);
712 /* read qcow2 extensions */
713 if (qcow2_read_extensions(bs
, header
.header_length
, ext_end
, NULL
,
715 error_propagate(errp
, local_err
);
720 /* read the backing file name */
721 if (header
.backing_file_offset
!= 0) {
722 len
= header
.backing_file_size
;
723 if (len
> MIN(1023, s
->cluster_size
- header
.backing_file_offset
)) {
724 error_setg(errp
, "Backing file name too long");
728 ret
= bdrv_pread(bs
->file
, header
.backing_file_offset
,
729 bs
->backing_file
, len
);
731 error_setg_errno(errp
, -ret
, "Could not read backing file name");
734 bs
->backing_file
[len
] = '\0';
737 /* Internal snapshots */
738 s
->snapshots_offset
= header
.snapshots_offset
;
739 s
->nb_snapshots
= header
.nb_snapshots
;
741 ret
= qcow2_read_snapshots(bs
);
743 error_setg_errno(errp
, -ret
, "Could not read snapshots");
747 /* Clear unknown autoclear feature bits */
748 if (!bs
->read_only
&& !(flags
& BDRV_O_INCOMING
) && s
->autoclear_features
) {
749 s
->autoclear_features
= 0;
750 ret
= qcow2_update_header(bs
);
752 error_setg_errno(errp
, -ret
, "Could not update qcow2 header");
757 /* Initialise locks */
758 qemu_co_mutex_init(&s
->lock
);
760 /* Repair image if dirty */
761 if (!(flags
& (BDRV_O_CHECK
| BDRV_O_INCOMING
)) && !bs
->read_only
&&
762 (s
->incompatible_features
& QCOW2_INCOMPAT_DIRTY
)) {
763 BdrvCheckResult result
= {0};
765 ret
= qcow2_check(bs
, &result
, BDRV_FIX_ERRORS
);
767 error_setg_errno(errp
, -ret
, "Could not repair dirty image");
772 /* Enable lazy_refcounts according to image and command line options */
773 opts
= qemu_opts_create(&qcow2_runtime_opts
, NULL
, 0, &error_abort
);
774 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
776 error_propagate(errp
, local_err
);
781 s
->use_lazy_refcounts
= qemu_opt_get_bool(opts
, QCOW2_OPT_LAZY_REFCOUNTS
,
782 (s
->compatible_features
& QCOW2_COMPAT_LAZY_REFCOUNTS
));
784 s
->discard_passthrough
[QCOW2_DISCARD_NEVER
] = false;
785 s
->discard_passthrough
[QCOW2_DISCARD_ALWAYS
] = true;
786 s
->discard_passthrough
[QCOW2_DISCARD_REQUEST
] =
787 qemu_opt_get_bool(opts
, QCOW2_OPT_DISCARD_REQUEST
,
788 flags
& BDRV_O_UNMAP
);
789 s
->discard_passthrough
[QCOW2_DISCARD_SNAPSHOT
] =
790 qemu_opt_get_bool(opts
, QCOW2_OPT_DISCARD_SNAPSHOT
, true);
791 s
->discard_passthrough
[QCOW2_DISCARD_OTHER
] =
792 qemu_opt_get_bool(opts
, QCOW2_OPT_DISCARD_OTHER
, false);
794 opt_overlap_check
= qemu_opt_get(opts
, "overlap-check") ?: "cached";
795 if (!strcmp(opt_overlap_check
, "none")) {
796 overlap_check_template
= 0;
797 } else if (!strcmp(opt_overlap_check
, "constant")) {
798 overlap_check_template
= QCOW2_OL_CONSTANT
;
799 } else if (!strcmp(opt_overlap_check
, "cached")) {
800 overlap_check_template
= QCOW2_OL_CACHED
;
801 } else if (!strcmp(opt_overlap_check
, "all")) {
802 overlap_check_template
= QCOW2_OL_ALL
;
804 error_setg(errp
, "Unsupported value '%s' for qcow2 option "
805 "'overlap-check'. Allowed are either of the following: "
806 "none, constant, cached, all", opt_overlap_check
);
812 s
->overlap_check
= 0;
813 for (i
= 0; i
< QCOW2_OL_MAX_BITNR
; i
++) {
814 /* overlap-check defines a template bitmask, but every flag may be
815 * overwritten through the associated boolean option */
817 qemu_opt_get_bool(opts
, overlap_bool_option_names
[i
],
818 overlap_check_template
& (1 << i
)) << i
;
823 if (s
->use_lazy_refcounts
&& s
->qcow_version
< 3) {
824 error_setg(errp
, "Lazy refcounts require a qcow2 image with at least "
825 "qemu 1.1 compatibility level");
832 BdrvCheckResult result
= {0};
833 qcow2_check_refcounts(bs
, &result
, 0);
839 g_free(s
->unknown_header_fields
);
840 cleanup_unknown_header_ext(bs
);
841 qcow2_free_snapshots(bs
);
842 qcow2_refcount_close(bs
);
844 /* else pre-write overlap checks in cache_destroy may crash */
846 if (s
->l2_table_cache
) {
847 qcow2_cache_destroy(bs
, s
->l2_table_cache
);
849 if (s
->refcount_block_cache
) {
850 qcow2_cache_destroy(bs
, s
->refcount_block_cache
);
852 g_free(s
->cluster_cache
);
853 qemu_vfree(s
->cluster_data
);
857 static int qcow2_refresh_limits(BlockDriverState
*bs
)
859 BDRVQcowState
*s
= bs
->opaque
;
861 bs
->bl
.write_zeroes_alignment
= s
->cluster_sectors
;
866 static int qcow2_set_key(BlockDriverState
*bs
, const char *key
)
868 BDRVQcowState
*s
= bs
->opaque
;
872 memset(keybuf
, 0, 16);
876 /* XXX: we could compress the chars to 7 bits to increase
878 for(i
= 0;i
< len
;i
++) {
881 s
->crypt_method
= s
->crypt_method_header
;
883 if (AES_set_encrypt_key(keybuf
, 128, &s
->aes_encrypt_key
) != 0)
885 if (AES_set_decrypt_key(keybuf
, 128, &s
->aes_decrypt_key
) != 0)
895 AES_encrypt(in
, tmp
, &s
->aes_encrypt_key
);
896 AES_decrypt(tmp
, out
, &s
->aes_decrypt_key
);
897 for(i
= 0; i
< 16; i
++)
898 printf(" %02x", tmp
[i
]);
900 for(i
= 0; i
< 16; i
++)
901 printf(" %02x", out
[i
]);
908 /* We have no actual commit/abort logic for qcow2, but we need to write out any
909 * unwritten data if we reopen read-only. */
910 static int qcow2_reopen_prepare(BDRVReopenState
*state
,
911 BlockReopenQueue
*queue
, Error
**errp
)
915 if ((state
->flags
& BDRV_O_RDWR
) == 0) {
916 ret
= bdrv_flush(state
->bs
);
921 ret
= qcow2_mark_clean(state
->bs
);
930 static int64_t coroutine_fn
qcow2_co_get_block_status(BlockDriverState
*bs
,
931 int64_t sector_num
, int nb_sectors
, int *pnum
)
933 BDRVQcowState
*s
= bs
->opaque
;
934 uint64_t cluster_offset
;
935 int index_in_cluster
, ret
;
939 qemu_co_mutex_lock(&s
->lock
);
940 ret
= qcow2_get_cluster_offset(bs
, sector_num
<< 9, pnum
, &cluster_offset
);
941 qemu_co_mutex_unlock(&s
->lock
);
946 if (cluster_offset
!= 0 && ret
!= QCOW2_CLUSTER_COMPRESSED
&&
948 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
949 cluster_offset
|= (index_in_cluster
<< BDRV_SECTOR_BITS
);
950 status
|= BDRV_BLOCK_OFFSET_VALID
| cluster_offset
;
952 if (ret
== QCOW2_CLUSTER_ZERO
) {
953 status
|= BDRV_BLOCK_ZERO
;
954 } else if (ret
!= QCOW2_CLUSTER_UNALLOCATED
) {
955 status
|= BDRV_BLOCK_DATA
;
960 /* handle reading after the end of the backing file */
961 int qcow2_backing_read1(BlockDriverState
*bs
, QEMUIOVector
*qiov
,
962 int64_t sector_num
, int nb_sectors
)
965 if ((sector_num
+ nb_sectors
) <= bs
->total_sectors
)
967 if (sector_num
>= bs
->total_sectors
)
970 n1
= bs
->total_sectors
- sector_num
;
972 qemu_iovec_memset(qiov
, 512 * n1
, 0, 512 * (nb_sectors
- n1
));
977 static coroutine_fn
int qcow2_co_readv(BlockDriverState
*bs
, int64_t sector_num
,
978 int remaining_sectors
, QEMUIOVector
*qiov
)
980 BDRVQcowState
*s
= bs
->opaque
;
981 int index_in_cluster
, n1
;
983 int cur_nr_sectors
; /* number of sectors in current iteration */
984 uint64_t cluster_offset
= 0;
985 uint64_t bytes_done
= 0;
986 QEMUIOVector hd_qiov
;
987 uint8_t *cluster_data
= NULL
;
989 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
991 qemu_co_mutex_lock(&s
->lock
);
993 while (remaining_sectors
!= 0) {
995 /* prepare next request */
996 cur_nr_sectors
= remaining_sectors
;
997 if (s
->crypt_method
) {
998 cur_nr_sectors
= MIN(cur_nr_sectors
,
999 QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
);
1002 ret
= qcow2_get_cluster_offset(bs
, sector_num
<< 9,
1003 &cur_nr_sectors
, &cluster_offset
);
1008 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
1010 qemu_iovec_reset(&hd_qiov
);
1011 qemu_iovec_concat(&hd_qiov
, qiov
, bytes_done
,
1012 cur_nr_sectors
* 512);
1015 case QCOW2_CLUSTER_UNALLOCATED
:
1017 if (bs
->backing_hd
) {
1018 /* read from the base image */
1019 n1
= qcow2_backing_read1(bs
->backing_hd
, &hd_qiov
,
1020 sector_num
, cur_nr_sectors
);
1022 BLKDBG_EVENT(bs
->file
, BLKDBG_READ_BACKING_AIO
);
1023 qemu_co_mutex_unlock(&s
->lock
);
1024 ret
= bdrv_co_readv(bs
->backing_hd
, sector_num
,
1026 qemu_co_mutex_lock(&s
->lock
);
1032 /* Note: in this case, no need to wait */
1033 qemu_iovec_memset(&hd_qiov
, 0, 0, 512 * cur_nr_sectors
);
1037 case QCOW2_CLUSTER_ZERO
:
1038 qemu_iovec_memset(&hd_qiov
, 0, 0, 512 * cur_nr_sectors
);
1041 case QCOW2_CLUSTER_COMPRESSED
:
1042 /* add AIO support for compressed blocks ? */
1043 ret
= qcow2_decompress_cluster(bs
, cluster_offset
);
1048 qemu_iovec_from_buf(&hd_qiov
, 0,
1049 s
->cluster_cache
+ index_in_cluster
* 512,
1050 512 * cur_nr_sectors
);
1053 case QCOW2_CLUSTER_NORMAL
:
1054 if ((cluster_offset
& 511) != 0) {
1059 if (s
->crypt_method
) {
1061 * For encrypted images, read everything into a temporary
1062 * contiguous buffer on which the AES functions can work.
1064 if (!cluster_data
) {
1066 qemu_blockalign(bs
, QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_size
);
1069 assert(cur_nr_sectors
<=
1070 QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
);
1071 qemu_iovec_reset(&hd_qiov
);
1072 qemu_iovec_add(&hd_qiov
, cluster_data
,
1073 512 * cur_nr_sectors
);
1076 BLKDBG_EVENT(bs
->file
, BLKDBG_READ_AIO
);
1077 qemu_co_mutex_unlock(&s
->lock
);
1078 ret
= bdrv_co_readv(bs
->file
,
1079 (cluster_offset
>> 9) + index_in_cluster
,
1080 cur_nr_sectors
, &hd_qiov
);
1081 qemu_co_mutex_lock(&s
->lock
);
1085 if (s
->crypt_method
) {
1086 qcow2_encrypt_sectors(s
, sector_num
, cluster_data
,
1087 cluster_data
, cur_nr_sectors
, 0, &s
->aes_decrypt_key
);
1088 qemu_iovec_from_buf(qiov
, bytes_done
,
1089 cluster_data
, 512 * cur_nr_sectors
);
1094 g_assert_not_reached();
1099 remaining_sectors
-= cur_nr_sectors
;
1100 sector_num
+= cur_nr_sectors
;
1101 bytes_done
+= cur_nr_sectors
* 512;
1106 qemu_co_mutex_unlock(&s
->lock
);
1108 qemu_iovec_destroy(&hd_qiov
);
1109 qemu_vfree(cluster_data
);
1114 static coroutine_fn
int qcow2_co_writev(BlockDriverState
*bs
,
1116 int remaining_sectors
,
1119 BDRVQcowState
*s
= bs
->opaque
;
1120 int index_in_cluster
;
1122 int cur_nr_sectors
; /* number of sectors in current iteration */
1123 uint64_t cluster_offset
;
1124 QEMUIOVector hd_qiov
;
1125 uint64_t bytes_done
= 0;
1126 uint8_t *cluster_data
= NULL
;
1127 QCowL2Meta
*l2meta
= NULL
;
1129 trace_qcow2_writev_start_req(qemu_coroutine_self(), sector_num
,
1132 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
1134 s
->cluster_cache_offset
= -1; /* disable compressed cache */
1136 qemu_co_mutex_lock(&s
->lock
);
1138 while (remaining_sectors
!= 0) {
1142 trace_qcow2_writev_start_part(qemu_coroutine_self());
1143 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
1144 cur_nr_sectors
= remaining_sectors
;
1145 if (s
->crypt_method
&&
1147 QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
- index_in_cluster
) {
1149 QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
- index_in_cluster
;
1152 ret
= qcow2_alloc_cluster_offset(bs
, sector_num
<< 9,
1153 &cur_nr_sectors
, &cluster_offset
, &l2meta
);
1158 assert((cluster_offset
& 511) == 0);
1160 qemu_iovec_reset(&hd_qiov
);
1161 qemu_iovec_concat(&hd_qiov
, qiov
, bytes_done
,
1162 cur_nr_sectors
* 512);
1164 if (s
->crypt_method
) {
1165 if (!cluster_data
) {
1166 cluster_data
= qemu_blockalign(bs
, QCOW_MAX_CRYPT_CLUSTERS
*
1170 assert(hd_qiov
.size
<=
1171 QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_size
);
1172 qemu_iovec_to_buf(&hd_qiov
, 0, cluster_data
, hd_qiov
.size
);
1174 qcow2_encrypt_sectors(s
, sector_num
, cluster_data
,
1175 cluster_data
, cur_nr_sectors
, 1, &s
->aes_encrypt_key
);
1177 qemu_iovec_reset(&hd_qiov
);
1178 qemu_iovec_add(&hd_qiov
, cluster_data
,
1179 cur_nr_sectors
* 512);
1182 ret
= qcow2_pre_write_overlap_check(bs
, 0,
1183 cluster_offset
+ index_in_cluster
* BDRV_SECTOR_SIZE
,
1184 cur_nr_sectors
* BDRV_SECTOR_SIZE
);
1189 qemu_co_mutex_unlock(&s
->lock
);
1190 BLKDBG_EVENT(bs
->file
, BLKDBG_WRITE_AIO
);
1191 trace_qcow2_writev_data(qemu_coroutine_self(),
1192 (cluster_offset
>> 9) + index_in_cluster
);
1193 ret
= bdrv_co_writev(bs
->file
,
1194 (cluster_offset
>> 9) + index_in_cluster
,
1195 cur_nr_sectors
, &hd_qiov
);
1196 qemu_co_mutex_lock(&s
->lock
);
1201 while (l2meta
!= NULL
) {
1204 ret
= qcow2_alloc_cluster_link_l2(bs
, l2meta
);
1209 /* Take the request off the list of running requests */
1210 if (l2meta
->nb_clusters
!= 0) {
1211 QLIST_REMOVE(l2meta
, next_in_flight
);
1214 qemu_co_queue_restart_all(&l2meta
->dependent_requests
);
1216 next
= l2meta
->next
;
1221 remaining_sectors
-= cur_nr_sectors
;
1222 sector_num
+= cur_nr_sectors
;
1223 bytes_done
+= cur_nr_sectors
* 512;
1224 trace_qcow2_writev_done_part(qemu_coroutine_self(), cur_nr_sectors
);
1229 qemu_co_mutex_unlock(&s
->lock
);
1231 while (l2meta
!= NULL
) {
1234 if (l2meta
->nb_clusters
!= 0) {
1235 QLIST_REMOVE(l2meta
, next_in_flight
);
1237 qemu_co_queue_restart_all(&l2meta
->dependent_requests
);
1239 next
= l2meta
->next
;
1244 qemu_iovec_destroy(&hd_qiov
);
1245 qemu_vfree(cluster_data
);
1246 trace_qcow2_writev_done_req(qemu_coroutine_self(), ret
);
1251 static void qcow2_close(BlockDriverState
*bs
)
1253 BDRVQcowState
*s
= bs
->opaque
;
1254 g_free(s
->l1_table
);
1255 /* else pre-write overlap checks in cache_destroy may crash */
1258 if (!(bs
->open_flags
& BDRV_O_INCOMING
)) {
1259 qcow2_cache_flush(bs
, s
->l2_table_cache
);
1260 qcow2_cache_flush(bs
, s
->refcount_block_cache
);
1262 qcow2_mark_clean(bs
);
1265 qcow2_cache_destroy(bs
, s
->l2_table_cache
);
1266 qcow2_cache_destroy(bs
, s
->refcount_block_cache
);
1268 g_free(s
->unknown_header_fields
);
1269 cleanup_unknown_header_ext(bs
);
1271 g_free(s
->cluster_cache
);
1272 qemu_vfree(s
->cluster_data
);
1273 qcow2_refcount_close(bs
);
1274 qcow2_free_snapshots(bs
);
1277 static void qcow2_invalidate_cache(BlockDriverState
*bs
, Error
**errp
)
1279 BDRVQcowState
*s
= bs
->opaque
;
1280 int flags
= s
->flags
;
1281 AES_KEY aes_encrypt_key
;
1282 AES_KEY aes_decrypt_key
;
1283 uint32_t crypt_method
= 0;
1285 Error
*local_err
= NULL
;
1289 * Backing files are read-only which makes all of their metadata immutable,
1290 * that means we don't have to worry about reopening them here.
1293 if (s
->crypt_method
) {
1294 crypt_method
= s
->crypt_method
;
1295 memcpy(&aes_encrypt_key
, &s
->aes_encrypt_key
, sizeof(aes_encrypt_key
));
1296 memcpy(&aes_decrypt_key
, &s
->aes_decrypt_key
, sizeof(aes_decrypt_key
));
1301 bdrv_invalidate_cache(bs
->file
, &local_err
);
1303 error_propagate(errp
, local_err
);
1307 memset(s
, 0, sizeof(BDRVQcowState
));
1308 options
= qdict_clone_shallow(bs
->options
);
1310 ret
= qcow2_open(bs
, options
, flags
, &local_err
);
1313 error_setg(errp
, "Could not reopen qcow2 layer: %s",
1314 error_get_pretty(local_err
));
1315 error_free(local_err
);
1317 } else if (ret
< 0) {
1318 error_setg_errno(errp
, -ret
, "Could not reopen qcow2 layer");
1323 s
->crypt_method
= crypt_method
;
1324 memcpy(&s
->aes_encrypt_key
, &aes_encrypt_key
, sizeof(aes_encrypt_key
));
1325 memcpy(&s
->aes_decrypt_key
, &aes_decrypt_key
, sizeof(aes_decrypt_key
));
1329 static size_t header_ext_add(char *buf
, uint32_t magic
, const void *s
,
1330 size_t len
, size_t buflen
)
1332 QCowExtension
*ext_backing_fmt
= (QCowExtension
*) buf
;
1333 size_t ext_len
= sizeof(QCowExtension
) + ((len
+ 7) & ~7);
1335 if (buflen
< ext_len
) {
1339 *ext_backing_fmt
= (QCowExtension
) {
1340 .magic
= cpu_to_be32(magic
),
1341 .len
= cpu_to_be32(len
),
1343 memcpy(buf
+ sizeof(QCowExtension
), s
, len
);
1349 * Updates the qcow2 header, including the variable length parts of it, i.e.
1350 * the backing file name and all extensions. qcow2 was not designed to allow
1351 * such changes, so if we run out of space (we can only use the first cluster)
1352 * this function may fail.
1354 * Returns 0 on success, -errno in error cases.
1356 int qcow2_update_header(BlockDriverState
*bs
)
1358 BDRVQcowState
*s
= bs
->opaque
;
1361 size_t buflen
= s
->cluster_size
;
1363 uint64_t total_size
;
1364 uint32_t refcount_table_clusters
;
1365 size_t header_length
;
1366 Qcow2UnknownHeaderExtension
*uext
;
1368 buf
= qemu_blockalign(bs
, buflen
);
1370 /* Header structure */
1371 header
= (QCowHeader
*) buf
;
1373 if (buflen
< sizeof(*header
)) {
1378 header_length
= sizeof(*header
) + s
->unknown_header_fields_size
;
1379 total_size
= bs
->total_sectors
* BDRV_SECTOR_SIZE
;
1380 refcount_table_clusters
= s
->refcount_table_size
>> (s
->cluster_bits
- 3);
1382 *header
= (QCowHeader
) {
1383 /* Version 2 fields */
1384 .magic
= cpu_to_be32(QCOW_MAGIC
),
1385 .version
= cpu_to_be32(s
->qcow_version
),
1386 .backing_file_offset
= 0,
1387 .backing_file_size
= 0,
1388 .cluster_bits
= cpu_to_be32(s
->cluster_bits
),
1389 .size
= cpu_to_be64(total_size
),
1390 .crypt_method
= cpu_to_be32(s
->crypt_method_header
),
1391 .l1_size
= cpu_to_be32(s
->l1_size
),
1392 .l1_table_offset
= cpu_to_be64(s
->l1_table_offset
),
1393 .refcount_table_offset
= cpu_to_be64(s
->refcount_table_offset
),
1394 .refcount_table_clusters
= cpu_to_be32(refcount_table_clusters
),
1395 .nb_snapshots
= cpu_to_be32(s
->nb_snapshots
),
1396 .snapshots_offset
= cpu_to_be64(s
->snapshots_offset
),
1398 /* Version 3 fields */
1399 .incompatible_features
= cpu_to_be64(s
->incompatible_features
),
1400 .compatible_features
= cpu_to_be64(s
->compatible_features
),
1401 .autoclear_features
= cpu_to_be64(s
->autoclear_features
),
1402 .refcount_order
= cpu_to_be32(s
->refcount_order
),
1403 .header_length
= cpu_to_be32(header_length
),
1406 /* For older versions, write a shorter header */
1407 switch (s
->qcow_version
) {
1409 ret
= offsetof(QCowHeader
, incompatible_features
);
1412 ret
= sizeof(*header
);
1421 memset(buf
, 0, buflen
);
1423 /* Preserve any unknown field in the header */
1424 if (s
->unknown_header_fields_size
) {
1425 if (buflen
< s
->unknown_header_fields_size
) {
1430 memcpy(buf
, s
->unknown_header_fields
, s
->unknown_header_fields_size
);
1431 buf
+= s
->unknown_header_fields_size
;
1432 buflen
-= s
->unknown_header_fields_size
;
1435 /* Backing file format header extension */
1436 if (*bs
->backing_format
) {
1437 ret
= header_ext_add(buf
, QCOW2_EXT_MAGIC_BACKING_FORMAT
,
1438 bs
->backing_format
, strlen(bs
->backing_format
),
1449 Qcow2Feature features
[] = {
1451 .type
= QCOW2_FEAT_TYPE_INCOMPATIBLE
,
1452 .bit
= QCOW2_INCOMPAT_DIRTY_BITNR
,
1453 .name
= "dirty bit",
1456 .type
= QCOW2_FEAT_TYPE_INCOMPATIBLE
,
1457 .bit
= QCOW2_INCOMPAT_CORRUPT_BITNR
,
1458 .name
= "corrupt bit",
1461 .type
= QCOW2_FEAT_TYPE_COMPATIBLE
,
1462 .bit
= QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR
,
1463 .name
= "lazy refcounts",
1467 ret
= header_ext_add(buf
, QCOW2_EXT_MAGIC_FEATURE_TABLE
,
1468 features
, sizeof(features
), buflen
);
1475 /* Keep unknown header extensions */
1476 QLIST_FOREACH(uext
, &s
->unknown_header_ext
, next
) {
1477 ret
= header_ext_add(buf
, uext
->magic
, uext
->data
, uext
->len
, buflen
);
1486 /* End of header extensions */
1487 ret
= header_ext_add(buf
, QCOW2_EXT_MAGIC_END
, NULL
, 0, buflen
);
1495 /* Backing file name */
1496 if (*bs
->backing_file
) {
1497 size_t backing_file_len
= strlen(bs
->backing_file
);
1499 if (buflen
< backing_file_len
) {
1504 /* Using strncpy is ok here, since buf is not NUL-terminated. */
1505 strncpy(buf
, bs
->backing_file
, buflen
);
1507 header
->backing_file_offset
= cpu_to_be64(buf
- ((char*) header
));
1508 header
->backing_file_size
= cpu_to_be32(backing_file_len
);
1511 /* Write the new header */
1512 ret
= bdrv_pwrite(bs
->file
, 0, header
, s
->cluster_size
);
1523 static int qcow2_change_backing_file(BlockDriverState
*bs
,
1524 const char *backing_file
, const char *backing_fmt
)
1526 pstrcpy(bs
->backing_file
, sizeof(bs
->backing_file
), backing_file
?: "");
1527 pstrcpy(bs
->backing_format
, sizeof(bs
->backing_format
), backing_fmt
?: "");
1529 return qcow2_update_header(bs
);
1532 static int preallocate(BlockDriverState
*bs
)
1534 uint64_t nb_sectors
;
1536 uint64_t host_offset
= 0;
1541 nb_sectors
= bdrv_getlength(bs
) >> BDRV_SECTOR_BITS
;
1544 while (nb_sectors
) {
1545 num
= MIN(nb_sectors
, INT_MAX
>> BDRV_SECTOR_BITS
);
1546 ret
= qcow2_alloc_cluster_offset(bs
, offset
, &num
,
1547 &host_offset
, &meta
);
1553 QCowL2Meta
*next
= meta
->next
;
1555 ret
= qcow2_alloc_cluster_link_l2(bs
, meta
);
1557 qcow2_free_any_clusters(bs
, meta
->alloc_offset
,
1558 meta
->nb_clusters
, QCOW2_DISCARD_NEVER
);
1562 /* There are no dependent requests, but we need to remove our
1563 * request from the list of in-flight requests */
1564 QLIST_REMOVE(meta
, next_in_flight
);
1570 /* TODO Preallocate data if requested */
1573 offset
+= num
<< BDRV_SECTOR_BITS
;
1577 * It is expected that the image file is large enough to actually contain
1578 * all of the allocated clusters (otherwise we get failing reads after
1579 * EOF). Extend the image to the last allocated sector.
1581 if (host_offset
!= 0) {
1582 uint8_t buf
[BDRV_SECTOR_SIZE
];
1583 memset(buf
, 0, BDRV_SECTOR_SIZE
);
1584 ret
= bdrv_write(bs
->file
, (host_offset
>> BDRV_SECTOR_BITS
) + num
- 1,
1594 static int qcow2_create2(const char *filename
, int64_t total_size
,
1595 const char *backing_file
, const char *backing_format
,
1596 int flags
, size_t cluster_size
, int prealloc
,
1597 QEMUOptionParameter
*options
, int version
,
1600 /* Calculate cluster_bits */
1602 cluster_bits
= ffs(cluster_size
) - 1;
1603 if (cluster_bits
< MIN_CLUSTER_BITS
|| cluster_bits
> MAX_CLUSTER_BITS
||
1604 (1 << cluster_bits
) != cluster_size
)
1606 error_setg(errp
, "Cluster size must be a power of two between %d and "
1607 "%dk", 1 << MIN_CLUSTER_BITS
, 1 << (MAX_CLUSTER_BITS
- 10));
1612 * Open the image file and write a minimal qcow2 header.
1614 * We keep things simple and start with a zero-sized image. We also
1615 * do without refcount blocks or a L1 table for now. We'll fix the
1616 * inconsistency later.
1618 * We do need a refcount table because growing the refcount table means
1619 * allocating two new refcount blocks - the seconds of which would be at
1620 * 2 GB for 64k clusters, and we don't want to have a 2 GB initial file
1621 * size for any qcow2 image.
1623 BlockDriverState
* bs
;
1625 uint64_t* refcount_table
;
1626 Error
*local_err
= NULL
;
1629 ret
= bdrv_create_file(filename
, options
, &local_err
);
1631 error_propagate(errp
, local_err
);
1636 ret
= bdrv_open(&bs
, filename
, NULL
, NULL
, BDRV_O_RDWR
| BDRV_O_PROTOCOL
,
1639 error_propagate(errp
, local_err
);
1643 /* Write the header */
1644 QEMU_BUILD_BUG_ON((1 << MIN_CLUSTER_BITS
) < sizeof(*header
));
1645 header
= g_malloc0(cluster_size
);
1646 *header
= (QCowHeader
) {
1647 .magic
= cpu_to_be32(QCOW_MAGIC
),
1648 .version
= cpu_to_be32(version
),
1649 .cluster_bits
= cpu_to_be32(cluster_bits
),
1650 .size
= cpu_to_be64(0),
1651 .l1_table_offset
= cpu_to_be64(0),
1652 .l1_size
= cpu_to_be32(0),
1653 .refcount_table_offset
= cpu_to_be64(cluster_size
),
1654 .refcount_table_clusters
= cpu_to_be32(1),
1655 .refcount_order
= cpu_to_be32(3 + REFCOUNT_SHIFT
),
1656 .header_length
= cpu_to_be32(sizeof(*header
)),
1659 if (flags
& BLOCK_FLAG_ENCRYPT
) {
1660 header
->crypt_method
= cpu_to_be32(QCOW_CRYPT_AES
);
1662 header
->crypt_method
= cpu_to_be32(QCOW_CRYPT_NONE
);
1665 if (flags
& BLOCK_FLAG_LAZY_REFCOUNTS
) {
1666 header
->compatible_features
|=
1667 cpu_to_be64(QCOW2_COMPAT_LAZY_REFCOUNTS
);
1670 ret
= bdrv_pwrite(bs
, 0, header
, cluster_size
);
1673 error_setg_errno(errp
, -ret
, "Could not write qcow2 header");
1677 /* Write a refcount table with one refcount block */
1678 refcount_table
= g_malloc0(2 * cluster_size
);
1679 refcount_table
[0] = cpu_to_be64(2 * cluster_size
);
1680 ret
= bdrv_pwrite(bs
, cluster_size
, refcount_table
, 2 * cluster_size
);
1681 g_free(refcount_table
);
1684 error_setg_errno(errp
, -ret
, "Could not write refcount table");
1692 * And now open the image and make it consistent first (i.e. increase the
1693 * refcount of the cluster that is occupied by the header and the refcount
1696 BlockDriver
* drv
= bdrv_find_format("qcow2");
1697 assert(drv
!= NULL
);
1698 ret
= bdrv_open(&bs
, filename
, NULL
, NULL
,
1699 BDRV_O_RDWR
| BDRV_O_CACHE_WB
| BDRV_O_NO_FLUSH
, drv
, &local_err
);
1701 error_propagate(errp
, local_err
);
1705 ret
= qcow2_alloc_clusters(bs
, 3 * cluster_size
);
1707 error_setg_errno(errp
, -ret
, "Could not allocate clusters for qcow2 "
1708 "header and refcount table");
1711 } else if (ret
!= 0) {
1712 error_report("Huh, first cluster in empty image is already in use?");
1716 /* Okay, now that we have a valid image, let's give it the right size */
1717 ret
= bdrv_truncate(bs
, total_size
* BDRV_SECTOR_SIZE
);
1719 error_setg_errno(errp
, -ret
, "Could not resize image");
1723 /* Want a backing file? There you go.*/
1725 ret
= bdrv_change_backing_file(bs
, backing_file
, backing_format
);
1727 error_setg_errno(errp
, -ret
, "Could not assign backing file '%s' "
1728 "with format '%s'", backing_file
, backing_format
);
1733 /* And if we're supposed to preallocate metadata, do that now */
1735 BDRVQcowState
*s
= bs
->opaque
;
1736 qemu_co_mutex_lock(&s
->lock
);
1737 ret
= preallocate(bs
);
1738 qemu_co_mutex_unlock(&s
->lock
);
1740 error_setg_errno(errp
, -ret
, "Could not preallocate metadata");
1748 /* Reopen the image without BDRV_O_NO_FLUSH to flush it before returning */
1749 ret
= bdrv_open(&bs
, filename
, NULL
, NULL
,
1750 BDRV_O_RDWR
| BDRV_O_CACHE_WB
| BDRV_O_NO_BACKING
,
1753 error_propagate(errp
, local_err
);
1765 static int qcow2_create(const char *filename
, QEMUOptionParameter
*options
,
1768 const char *backing_file
= NULL
;
1769 const char *backing_fmt
= NULL
;
1770 uint64_t sectors
= 0;
1772 size_t cluster_size
= DEFAULT_CLUSTER_SIZE
;
1775 Error
*local_err
= NULL
;
1778 /* Read out options */
1779 while (options
&& options
->name
) {
1780 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
1781 sectors
= options
->value
.n
/ 512;
1782 } else if (!strcmp(options
->name
, BLOCK_OPT_BACKING_FILE
)) {
1783 backing_file
= options
->value
.s
;
1784 } else if (!strcmp(options
->name
, BLOCK_OPT_BACKING_FMT
)) {
1785 backing_fmt
= options
->value
.s
;
1786 } else if (!strcmp(options
->name
, BLOCK_OPT_ENCRYPT
)) {
1787 flags
|= options
->value
.n
? BLOCK_FLAG_ENCRYPT
: 0;
1788 } else if (!strcmp(options
->name
, BLOCK_OPT_CLUSTER_SIZE
)) {
1789 if (options
->value
.n
) {
1790 cluster_size
= options
->value
.n
;
1792 } else if (!strcmp(options
->name
, BLOCK_OPT_PREALLOC
)) {
1793 if (!options
->value
.s
|| !strcmp(options
->value
.s
, "off")) {
1795 } else if (!strcmp(options
->value
.s
, "metadata")) {
1798 error_setg(errp
, "Invalid preallocation mode: '%s'",
1802 } else if (!strcmp(options
->name
, BLOCK_OPT_COMPAT_LEVEL
)) {
1803 if (!options
->value
.s
) {
1804 /* keep the default */
1805 } else if (!strcmp(options
->value
.s
, "0.10")) {
1807 } else if (!strcmp(options
->value
.s
, "1.1")) {
1810 error_setg(errp
, "Invalid compatibility level: '%s'",
1814 } else if (!strcmp(options
->name
, BLOCK_OPT_LAZY_REFCOUNTS
)) {
1815 flags
|= options
->value
.n
? BLOCK_FLAG_LAZY_REFCOUNTS
: 0;
1820 if (backing_file
&& prealloc
) {
1821 error_setg(errp
, "Backing file and preallocation cannot be used at "
1826 if (version
< 3 && (flags
& BLOCK_FLAG_LAZY_REFCOUNTS
)) {
1827 error_setg(errp
, "Lazy refcounts only supported with compatibility "
1828 "level 1.1 and above (use compat=1.1 or greater)");
1832 ret
= qcow2_create2(filename
, sectors
, backing_file
, backing_fmt
, flags
,
1833 cluster_size
, prealloc
, options
, version
, &local_err
);
1835 error_propagate(errp
, local_err
);
1840 static coroutine_fn
int qcow2_co_write_zeroes(BlockDriverState
*bs
,
1841 int64_t sector_num
, int nb_sectors
, BdrvRequestFlags flags
)
1844 BDRVQcowState
*s
= bs
->opaque
;
1846 /* Emulate misaligned zero writes */
1847 if (sector_num
% s
->cluster_sectors
|| nb_sectors
% s
->cluster_sectors
) {
1851 /* Whatever is left can use real zero clusters */
1852 qemu_co_mutex_lock(&s
->lock
);
1853 ret
= qcow2_zero_clusters(bs
, sector_num
<< BDRV_SECTOR_BITS
,
1855 qemu_co_mutex_unlock(&s
->lock
);
1860 static coroutine_fn
int qcow2_co_discard(BlockDriverState
*bs
,
1861 int64_t sector_num
, int nb_sectors
)
1864 BDRVQcowState
*s
= bs
->opaque
;
1866 qemu_co_mutex_lock(&s
->lock
);
1867 ret
= qcow2_discard_clusters(bs
, sector_num
<< BDRV_SECTOR_BITS
,
1868 nb_sectors
, QCOW2_DISCARD_REQUEST
);
1869 qemu_co_mutex_unlock(&s
->lock
);
1873 static int qcow2_truncate(BlockDriverState
*bs
, int64_t offset
)
1875 BDRVQcowState
*s
= bs
->opaque
;
1876 int64_t new_l1_size
;
1880 error_report("The new size must be a multiple of 512");
1884 /* cannot proceed if image has snapshots */
1885 if (s
->nb_snapshots
) {
1886 error_report("Can't resize an image which has snapshots");
1890 /* shrinking is currently not supported */
1891 if (offset
< bs
->total_sectors
* 512) {
1892 error_report("qcow2 doesn't support shrinking images yet");
1896 new_l1_size
= size_to_l1(s
, offset
);
1897 ret
= qcow2_grow_l1_table(bs
, new_l1_size
, true);
1902 /* write updated header.size */
1903 offset
= cpu_to_be64(offset
);
1904 ret
= bdrv_pwrite_sync(bs
->file
, offsetof(QCowHeader
, size
),
1905 &offset
, sizeof(uint64_t));
1910 s
->l1_vm_state_index
= new_l1_size
;
1914 /* XXX: put compressed sectors first, then all the cluster aligned
1915 tables to avoid losing bytes in alignment */
1916 static int qcow2_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
1917 const uint8_t *buf
, int nb_sectors
)
1919 BDRVQcowState
*s
= bs
->opaque
;
1923 uint64_t cluster_offset
;
1925 if (nb_sectors
== 0) {
1926 /* align end of file to a sector boundary to ease reading with
1927 sector based I/Os */
1928 cluster_offset
= bdrv_getlength(bs
->file
);
1929 cluster_offset
= (cluster_offset
+ 511) & ~511;
1930 bdrv_truncate(bs
->file
, cluster_offset
);
1934 if (nb_sectors
!= s
->cluster_sectors
) {
1937 /* Zero-pad last write if image size is not cluster aligned */
1938 if (sector_num
+ nb_sectors
== bs
->total_sectors
&&
1939 nb_sectors
< s
->cluster_sectors
) {
1940 uint8_t *pad_buf
= qemu_blockalign(bs
, s
->cluster_size
);
1941 memset(pad_buf
, 0, s
->cluster_size
);
1942 memcpy(pad_buf
, buf
, nb_sectors
* BDRV_SECTOR_SIZE
);
1943 ret
= qcow2_write_compressed(bs
, sector_num
,
1944 pad_buf
, s
->cluster_sectors
);
1945 qemu_vfree(pad_buf
);
1950 out_buf
= g_malloc(s
->cluster_size
+ (s
->cluster_size
/ 1000) + 128);
1952 /* best compression, small window, no zlib header */
1953 memset(&strm
, 0, sizeof(strm
));
1954 ret
= deflateInit2(&strm
, Z_DEFAULT_COMPRESSION
,
1956 9, Z_DEFAULT_STRATEGY
);
1962 strm
.avail_in
= s
->cluster_size
;
1963 strm
.next_in
= (uint8_t *)buf
;
1964 strm
.avail_out
= s
->cluster_size
;
1965 strm
.next_out
= out_buf
;
1967 ret
= deflate(&strm
, Z_FINISH
);
1968 if (ret
!= Z_STREAM_END
&& ret
!= Z_OK
) {
1973 out_len
= strm
.next_out
- out_buf
;
1977 if (ret
!= Z_STREAM_END
|| out_len
>= s
->cluster_size
) {
1978 /* could not compress: write normal cluster */
1979 ret
= bdrv_write(bs
, sector_num
, buf
, s
->cluster_sectors
);
1984 cluster_offset
= qcow2_alloc_compressed_cluster_offset(bs
,
1985 sector_num
<< 9, out_len
);
1986 if (!cluster_offset
) {
1990 cluster_offset
&= s
->cluster_offset_mask
;
1992 ret
= qcow2_pre_write_overlap_check(bs
, 0, cluster_offset
, out_len
);
1997 BLKDBG_EVENT(bs
->file
, BLKDBG_WRITE_COMPRESSED
);
1998 ret
= bdrv_pwrite(bs
->file
, cluster_offset
, out_buf
, out_len
);
2010 static coroutine_fn
int qcow2_co_flush_to_os(BlockDriverState
*bs
)
2012 BDRVQcowState
*s
= bs
->opaque
;
2015 qemu_co_mutex_lock(&s
->lock
);
2016 ret
= qcow2_cache_flush(bs
, s
->l2_table_cache
);
2018 qemu_co_mutex_unlock(&s
->lock
);
2022 if (qcow2_need_accurate_refcounts(s
)) {
2023 ret
= qcow2_cache_flush(bs
, s
->refcount_block_cache
);
2025 qemu_co_mutex_unlock(&s
->lock
);
2029 qemu_co_mutex_unlock(&s
->lock
);
2034 static int qcow2_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
2036 BDRVQcowState
*s
= bs
->opaque
;
2037 bdi
->unallocated_blocks_are_zero
= true;
2038 bdi
->can_write_zeroes_with_unmap
= (s
->qcow_version
>= 3);
2039 bdi
->cluster_size
= s
->cluster_size
;
2040 bdi
->vm_state_offset
= qcow2_vm_state_offset(s
);
2044 static ImageInfoSpecific
*qcow2_get_specific_info(BlockDriverState
*bs
)
2046 BDRVQcowState
*s
= bs
->opaque
;
2047 ImageInfoSpecific
*spec_info
= g_new(ImageInfoSpecific
, 1);
2049 *spec_info
= (ImageInfoSpecific
){
2050 .kind
= IMAGE_INFO_SPECIFIC_KIND_QCOW2
,
2052 .qcow2
= g_new(ImageInfoSpecificQCow2
, 1),
2055 if (s
->qcow_version
== 2) {
2056 *spec_info
->qcow2
= (ImageInfoSpecificQCow2
){
2057 .compat
= g_strdup("0.10"),
2059 } else if (s
->qcow_version
== 3) {
2060 *spec_info
->qcow2
= (ImageInfoSpecificQCow2
){
2061 .compat
= g_strdup("1.1"),
2062 .lazy_refcounts
= s
->compatible_features
&
2063 QCOW2_COMPAT_LAZY_REFCOUNTS
,
2064 .has_lazy_refcounts
= true,
2072 static void dump_refcounts(BlockDriverState
*bs
)
2074 BDRVQcowState
*s
= bs
->opaque
;
2075 int64_t nb_clusters
, k
, k1
, size
;
2078 size
= bdrv_getlength(bs
->file
);
2079 nb_clusters
= size_to_clusters(s
, size
);
2080 for(k
= 0; k
< nb_clusters
;) {
2082 refcount
= get_refcount(bs
, k
);
2084 while (k
< nb_clusters
&& get_refcount(bs
, k
) == refcount
)
2086 printf("%" PRId64
": refcount=%d nb=%" PRId64
"\n", k
, refcount
,
2092 static int qcow2_save_vmstate(BlockDriverState
*bs
, QEMUIOVector
*qiov
,
2095 BDRVQcowState
*s
= bs
->opaque
;
2096 int64_t total_sectors
= bs
->total_sectors
;
2097 int growable
= bs
->growable
;
2098 bool zero_beyond_eof
= bs
->zero_beyond_eof
;
2101 BLKDBG_EVENT(bs
->file
, BLKDBG_VMSTATE_SAVE
);
2103 bs
->zero_beyond_eof
= false;
2104 ret
= bdrv_pwritev(bs
, qcow2_vm_state_offset(s
) + pos
, qiov
);
2105 bs
->growable
= growable
;
2106 bs
->zero_beyond_eof
= zero_beyond_eof
;
2108 /* bdrv_co_do_writev will have increased the total_sectors value to include
2109 * the VM state - the VM state is however not an actual part of the block
2110 * device, therefore, we need to restore the old value. */
2111 bs
->total_sectors
= total_sectors
;
2116 static int qcow2_load_vmstate(BlockDriverState
*bs
, uint8_t *buf
,
2117 int64_t pos
, int size
)
2119 BDRVQcowState
*s
= bs
->opaque
;
2120 int growable
= bs
->growable
;
2121 bool zero_beyond_eof
= bs
->zero_beyond_eof
;
2124 BLKDBG_EVENT(bs
->file
, BLKDBG_VMSTATE_LOAD
);
2126 bs
->zero_beyond_eof
= false;
2127 ret
= bdrv_pread(bs
, qcow2_vm_state_offset(s
) + pos
, buf
, size
);
2128 bs
->growable
= growable
;
2129 bs
->zero_beyond_eof
= zero_beyond_eof
;
2135 * Downgrades an image's version. To achieve this, any incompatible features
2136 * have to be removed.
2138 static int qcow2_downgrade(BlockDriverState
*bs
, int target_version
)
2140 BDRVQcowState
*s
= bs
->opaque
;
2141 int current_version
= s
->qcow_version
;
2144 if (target_version
== current_version
) {
2146 } else if (target_version
> current_version
) {
2148 } else if (target_version
!= 2) {
2152 if (s
->refcount_order
!= 4) {
2153 /* we would have to convert the image to a refcount_order == 4 image
2154 * here; however, since qemu (at the time of writing this) does not
2155 * support anything different than 4 anyway, there is no point in doing
2156 * so right now; however, we should error out (if qemu supports this in
2157 * the future and this code has not been adapted) */
2158 error_report("qcow2_downgrade: Image refcount orders other than 4 are "
2159 "currently not supported.");
2163 /* clear incompatible features */
2164 if (s
->incompatible_features
& QCOW2_INCOMPAT_DIRTY
) {
2165 ret
= qcow2_mark_clean(bs
);
2171 /* with QCOW2_INCOMPAT_CORRUPT, it is pretty much impossible to get here in
2172 * the first place; if that happens nonetheless, returning -ENOTSUP is the
2173 * best thing to do anyway */
2175 if (s
->incompatible_features
) {
2179 /* since we can ignore compatible features, we can set them to 0 as well */
2180 s
->compatible_features
= 0;
2181 /* if lazy refcounts have been used, they have already been fixed through
2182 * clearing the dirty flag */
2184 /* clearing autoclear features is trivial */
2185 s
->autoclear_features
= 0;
2187 ret
= qcow2_expand_zero_clusters(bs
);
2192 s
->qcow_version
= target_version
;
2193 ret
= qcow2_update_header(bs
);
2195 s
->qcow_version
= current_version
;
2201 static int qcow2_amend_options(BlockDriverState
*bs
,
2202 QEMUOptionParameter
*options
)
2204 BDRVQcowState
*s
= bs
->opaque
;
2205 int old_version
= s
->qcow_version
, new_version
= old_version
;
2206 uint64_t new_size
= 0;
2207 const char *backing_file
= NULL
, *backing_format
= NULL
;
2208 bool lazy_refcounts
= s
->use_lazy_refcounts
;
2212 for (i
= 0; options
[i
].name
; i
++)
2214 if (!options
[i
].assigned
) {
2215 /* only change explicitly defined options */
2219 if (!strcmp(options
[i
].name
, "compat")) {
2220 if (!options
[i
].value
.s
) {
2221 /* preserve default */
2222 } else if (!strcmp(options
[i
].value
.s
, "0.10")) {
2224 } else if (!strcmp(options
[i
].value
.s
, "1.1")) {
2227 fprintf(stderr
, "Unknown compatibility level %s.\n",
2228 options
[i
].value
.s
);
2231 } else if (!strcmp(options
[i
].name
, "preallocation")) {
2232 fprintf(stderr
, "Cannot change preallocation mode.\n");
2234 } else if (!strcmp(options
[i
].name
, "size")) {
2235 new_size
= options
[i
].value
.n
;
2236 } else if (!strcmp(options
[i
].name
, "backing_file")) {
2237 backing_file
= options
[i
].value
.s
;
2238 } else if (!strcmp(options
[i
].name
, "backing_fmt")) {
2239 backing_format
= options
[i
].value
.s
;
2240 } else if (!strcmp(options
[i
].name
, "encryption")) {
2241 if ((options
[i
].value
.n
!= !!s
->crypt_method
)) {
2242 fprintf(stderr
, "Changing the encryption flag is not "
2246 } else if (!strcmp(options
[i
].name
, "cluster_size")) {
2247 if (options
[i
].value
.n
!= s
->cluster_size
) {
2248 fprintf(stderr
, "Changing the cluster size is not "
2252 } else if (!strcmp(options
[i
].name
, "lazy_refcounts")) {
2253 lazy_refcounts
= options
[i
].value
.n
;
2255 /* if this assertion fails, this probably means a new option was
2256 * added without having it covered here */
2261 if (new_version
!= old_version
) {
2262 if (new_version
> old_version
) {
2264 s
->qcow_version
= new_version
;
2265 ret
= qcow2_update_header(bs
);
2267 s
->qcow_version
= old_version
;
2271 ret
= qcow2_downgrade(bs
, new_version
);
2278 if (backing_file
|| backing_format
) {
2279 ret
= qcow2_change_backing_file(bs
, backing_file
?: bs
->backing_file
,
2280 backing_format
?: bs
->backing_format
);
2286 if (s
->use_lazy_refcounts
!= lazy_refcounts
) {
2287 if (lazy_refcounts
) {
2288 if (s
->qcow_version
< 3) {
2289 fprintf(stderr
, "Lazy refcounts only supported with compatibility "
2290 "level 1.1 and above (use compat=1.1 or greater)\n");
2293 s
->compatible_features
|= QCOW2_COMPAT_LAZY_REFCOUNTS
;
2294 ret
= qcow2_update_header(bs
);
2296 s
->compatible_features
&= ~QCOW2_COMPAT_LAZY_REFCOUNTS
;
2299 s
->use_lazy_refcounts
= true;
2301 /* make image clean first */
2302 ret
= qcow2_mark_clean(bs
);
2306 /* now disallow lazy refcounts */
2307 s
->compatible_features
&= ~QCOW2_COMPAT_LAZY_REFCOUNTS
;
2308 ret
= qcow2_update_header(bs
);
2310 s
->compatible_features
|= QCOW2_COMPAT_LAZY_REFCOUNTS
;
2313 s
->use_lazy_refcounts
= false;
2318 ret
= bdrv_truncate(bs
, new_size
);
2327 static QEMUOptionParameter qcow2_create_options
[] = {
2329 .name
= BLOCK_OPT_SIZE
,
2331 .help
= "Virtual disk size"
2334 .name
= BLOCK_OPT_COMPAT_LEVEL
,
2336 .help
= "Compatibility level (0.10 or 1.1)"
2339 .name
= BLOCK_OPT_BACKING_FILE
,
2341 .help
= "File name of a base image"
2344 .name
= BLOCK_OPT_BACKING_FMT
,
2346 .help
= "Image format of the base image"
2349 .name
= BLOCK_OPT_ENCRYPT
,
2351 .help
= "Encrypt the image"
2354 .name
= BLOCK_OPT_CLUSTER_SIZE
,
2356 .help
= "qcow2 cluster size",
2357 .value
= { .n
= DEFAULT_CLUSTER_SIZE
},
2360 .name
= BLOCK_OPT_PREALLOC
,
2362 .help
= "Preallocation mode (allowed values: off, metadata)"
2365 .name
= BLOCK_OPT_LAZY_REFCOUNTS
,
2367 .help
= "Postpone refcount updates",
2372 static BlockDriver bdrv_qcow2
= {
2373 .format_name
= "qcow2",
2374 .instance_size
= sizeof(BDRVQcowState
),
2375 .bdrv_probe
= qcow2_probe
,
2376 .bdrv_open
= qcow2_open
,
2377 .bdrv_close
= qcow2_close
,
2378 .bdrv_reopen_prepare
= qcow2_reopen_prepare
,
2379 .bdrv_create
= qcow2_create
,
2380 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
2381 .bdrv_co_get_block_status
= qcow2_co_get_block_status
,
2382 .bdrv_set_key
= qcow2_set_key
,
2384 .bdrv_co_readv
= qcow2_co_readv
,
2385 .bdrv_co_writev
= qcow2_co_writev
,
2386 .bdrv_co_flush_to_os
= qcow2_co_flush_to_os
,
2388 .bdrv_co_write_zeroes
= qcow2_co_write_zeroes
,
2389 .bdrv_co_discard
= qcow2_co_discard
,
2390 .bdrv_truncate
= qcow2_truncate
,
2391 .bdrv_write_compressed
= qcow2_write_compressed
,
2393 .bdrv_snapshot_create
= qcow2_snapshot_create
,
2394 .bdrv_snapshot_goto
= qcow2_snapshot_goto
,
2395 .bdrv_snapshot_delete
= qcow2_snapshot_delete
,
2396 .bdrv_snapshot_list
= qcow2_snapshot_list
,
2397 .bdrv_snapshot_load_tmp
= qcow2_snapshot_load_tmp
,
2398 .bdrv_get_info
= qcow2_get_info
,
2399 .bdrv_get_specific_info
= qcow2_get_specific_info
,
2401 .bdrv_save_vmstate
= qcow2_save_vmstate
,
2402 .bdrv_load_vmstate
= qcow2_load_vmstate
,
2404 .bdrv_change_backing_file
= qcow2_change_backing_file
,
2406 .bdrv_refresh_limits
= qcow2_refresh_limits
,
2407 .bdrv_invalidate_cache
= qcow2_invalidate_cache
,
2409 .create_options
= qcow2_create_options
,
2410 .bdrv_check
= qcow2_check
,
2411 .bdrv_amend_options
= qcow2_amend_options
,
2414 static void bdrv_qcow2_init(void)
2416 bdrv_register(&bdrv_qcow2
);
2419 block_init(bdrv_qcow2_init
);