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 QemuOptsList qcow2_runtime_opts
= {
334 .head
= QTAILQ_HEAD_INITIALIZER(qcow2_runtime_opts
.head
),
337 .name
= QCOW2_OPT_LAZY_REFCOUNTS
,
338 .type
= QEMU_OPT_BOOL
,
339 .help
= "Postpone refcount updates",
342 .name
= QCOW2_OPT_DISCARD_REQUEST
,
343 .type
= QEMU_OPT_BOOL
,
344 .help
= "Pass guest discard requests to the layer below",
347 .name
= QCOW2_OPT_DISCARD_SNAPSHOT
,
348 .type
= QEMU_OPT_BOOL
,
349 .help
= "Generate discard requests when snapshot related space "
353 .name
= QCOW2_OPT_DISCARD_OTHER
,
354 .type
= QEMU_OPT_BOOL
,
355 .help
= "Generate discard requests when other clusters are freed",
358 .name
= QCOW2_OPT_OVERLAP
,
359 .type
= QEMU_OPT_STRING
,
360 .help
= "Selects which overlap checks to perform from a range of "
361 "templates (none, constant, cached, all)",
364 .name
= QCOW2_OPT_OVERLAP_MAIN_HEADER
,
365 .type
= QEMU_OPT_BOOL
,
366 .help
= "Check for unintended writes into the main qcow2 header",
369 .name
= QCOW2_OPT_OVERLAP_ACTIVE_L1
,
370 .type
= QEMU_OPT_BOOL
,
371 .help
= "Check for unintended writes into the active L1 table",
374 .name
= QCOW2_OPT_OVERLAP_ACTIVE_L2
,
375 .type
= QEMU_OPT_BOOL
,
376 .help
= "Check for unintended writes into an active L2 table",
379 .name
= QCOW2_OPT_OVERLAP_REFCOUNT_TABLE
,
380 .type
= QEMU_OPT_BOOL
,
381 .help
= "Check for unintended writes into the refcount table",
384 .name
= QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK
,
385 .type
= QEMU_OPT_BOOL
,
386 .help
= "Check for unintended writes into a refcount block",
389 .name
= QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE
,
390 .type
= QEMU_OPT_BOOL
,
391 .help
= "Check for unintended writes into the snapshot table",
394 .name
= QCOW2_OPT_OVERLAP_INACTIVE_L1
,
395 .type
= QEMU_OPT_BOOL
,
396 .help
= "Check for unintended writes into an inactive L1 table",
399 .name
= QCOW2_OPT_OVERLAP_INACTIVE_L2
,
400 .type
= QEMU_OPT_BOOL
,
401 .help
= "Check for unintended writes into an inactive L2 table",
403 { /* end of list */ }
407 static const char *overlap_bool_option_names
[QCOW2_OL_MAX_BITNR
] = {
408 [QCOW2_OL_MAIN_HEADER_BITNR
] = QCOW2_OPT_OVERLAP_MAIN_HEADER
,
409 [QCOW2_OL_ACTIVE_L1_BITNR
] = QCOW2_OPT_OVERLAP_ACTIVE_L1
,
410 [QCOW2_OL_ACTIVE_L2_BITNR
] = QCOW2_OPT_OVERLAP_ACTIVE_L2
,
411 [QCOW2_OL_REFCOUNT_TABLE_BITNR
] = QCOW2_OPT_OVERLAP_REFCOUNT_TABLE
,
412 [QCOW2_OL_REFCOUNT_BLOCK_BITNR
] = QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK
,
413 [QCOW2_OL_SNAPSHOT_TABLE_BITNR
] = QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE
,
414 [QCOW2_OL_INACTIVE_L1_BITNR
] = QCOW2_OPT_OVERLAP_INACTIVE_L1
,
415 [QCOW2_OL_INACTIVE_L2_BITNR
] = QCOW2_OPT_OVERLAP_INACTIVE_L2
,
418 static int qcow2_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
421 BDRVQcowState
*s
= bs
->opaque
;
425 Error
*local_err
= NULL
;
427 uint64_t l1_vm_state_index
;
428 const char *opt_overlap_check
;
429 int overlap_check_template
= 0;
431 ret
= bdrv_pread(bs
->file
, 0, &header
, sizeof(header
));
433 error_setg_errno(errp
, -ret
, "Could not read qcow2 header");
436 be32_to_cpus(&header
.magic
);
437 be32_to_cpus(&header
.version
);
438 be64_to_cpus(&header
.backing_file_offset
);
439 be32_to_cpus(&header
.backing_file_size
);
440 be64_to_cpus(&header
.size
);
441 be32_to_cpus(&header
.cluster_bits
);
442 be32_to_cpus(&header
.crypt_method
);
443 be64_to_cpus(&header
.l1_table_offset
);
444 be32_to_cpus(&header
.l1_size
);
445 be64_to_cpus(&header
.refcount_table_offset
);
446 be32_to_cpus(&header
.refcount_table_clusters
);
447 be64_to_cpus(&header
.snapshots_offset
);
448 be32_to_cpus(&header
.nb_snapshots
);
450 if (header
.magic
!= QCOW_MAGIC
) {
451 error_setg(errp
, "Image is not in qcow2 format");
455 if (header
.version
< 2 || header
.version
> 3) {
456 report_unsupported(bs
, errp
, "QCOW version %d", header
.version
);
461 s
->qcow_version
= header
.version
;
463 /* Initialise version 3 header fields */
464 if (header
.version
== 2) {
465 header
.incompatible_features
= 0;
466 header
.compatible_features
= 0;
467 header
.autoclear_features
= 0;
468 header
.refcount_order
= 4;
469 header
.header_length
= 72;
471 be64_to_cpus(&header
.incompatible_features
);
472 be64_to_cpus(&header
.compatible_features
);
473 be64_to_cpus(&header
.autoclear_features
);
474 be32_to_cpus(&header
.refcount_order
);
475 be32_to_cpus(&header
.header_length
);
478 if (header
.header_length
> sizeof(header
)) {
479 s
->unknown_header_fields_size
= header
.header_length
- sizeof(header
);
480 s
->unknown_header_fields
= g_malloc(s
->unknown_header_fields_size
);
481 ret
= bdrv_pread(bs
->file
, sizeof(header
), s
->unknown_header_fields
,
482 s
->unknown_header_fields_size
);
484 error_setg_errno(errp
, -ret
, "Could not read unknown qcow2 header "
490 if (header
.backing_file_offset
) {
491 ext_end
= header
.backing_file_offset
;
493 ext_end
= 1 << header
.cluster_bits
;
496 /* Handle feature bits */
497 s
->incompatible_features
= header
.incompatible_features
;
498 s
->compatible_features
= header
.compatible_features
;
499 s
->autoclear_features
= header
.autoclear_features
;
501 if (s
->incompatible_features
& ~QCOW2_INCOMPAT_MASK
) {
502 void *feature_table
= NULL
;
503 qcow2_read_extensions(bs
, header
.header_length
, ext_end
,
504 &feature_table
, NULL
);
505 report_unsupported_feature(bs
, errp
, feature_table
,
506 s
->incompatible_features
&
507 ~QCOW2_INCOMPAT_MASK
);
512 if (s
->incompatible_features
& QCOW2_INCOMPAT_CORRUPT
) {
513 /* Corrupt images may not be written to unless they are being repaired
515 if ((flags
& BDRV_O_RDWR
) && !(flags
& BDRV_O_CHECK
)) {
516 error_setg(errp
, "qcow2: Image is corrupt; cannot be opened "
523 /* Check support for various header values */
524 if (header
.refcount_order
!= 4) {
525 report_unsupported(bs
, errp
, "%d bit reference counts",
526 1 << header
.refcount_order
);
530 s
->refcount_order
= header
.refcount_order
;
532 if (header
.cluster_bits
< MIN_CLUSTER_BITS
||
533 header
.cluster_bits
> MAX_CLUSTER_BITS
) {
534 error_setg(errp
, "Unsupported cluster size: 2^%i", header
.cluster_bits
);
538 if (header
.crypt_method
> QCOW_CRYPT_AES
) {
539 error_setg(errp
, "Unsupported encryption method: %i",
540 header
.crypt_method
);
544 s
->crypt_method_header
= header
.crypt_method
;
545 if (s
->crypt_method_header
) {
548 s
->cluster_bits
= header
.cluster_bits
;
549 s
->cluster_size
= 1 << s
->cluster_bits
;
550 s
->cluster_sectors
= 1 << (s
->cluster_bits
- 9);
551 s
->l2_bits
= s
->cluster_bits
- 3; /* L2 is always one cluster */
552 s
->l2_size
= 1 << s
->l2_bits
;
553 bs
->total_sectors
= header
.size
/ 512;
554 s
->csize_shift
= (62 - (s
->cluster_bits
- 8));
555 s
->csize_mask
= (1 << (s
->cluster_bits
- 8)) - 1;
556 s
->cluster_offset_mask
= (1LL << s
->csize_shift
) - 1;
557 s
->refcount_table_offset
= header
.refcount_table_offset
;
558 s
->refcount_table_size
=
559 header
.refcount_table_clusters
<< (s
->cluster_bits
- 3);
561 s
->snapshots_offset
= header
.snapshots_offset
;
562 s
->nb_snapshots
= header
.nb_snapshots
;
564 /* read the level 1 table */
565 s
->l1_size
= header
.l1_size
;
567 l1_vm_state_index
= size_to_l1(s
, header
.size
);
568 if (l1_vm_state_index
> INT_MAX
) {
569 error_setg(errp
, "Image is too big");
573 s
->l1_vm_state_index
= l1_vm_state_index
;
575 /* the L1 table must contain at least enough entries to put
577 if (s
->l1_size
< s
->l1_vm_state_index
) {
578 error_setg(errp
, "L1 table is too small");
582 s
->l1_table_offset
= header
.l1_table_offset
;
583 if (s
->l1_size
> 0) {
584 s
->l1_table
= g_malloc0(
585 align_offset(s
->l1_size
* sizeof(uint64_t), 512));
586 ret
= bdrv_pread(bs
->file
, s
->l1_table_offset
, s
->l1_table
,
587 s
->l1_size
* sizeof(uint64_t));
589 error_setg_errno(errp
, -ret
, "Could not read L1 table");
592 for(i
= 0;i
< s
->l1_size
; i
++) {
593 be64_to_cpus(&s
->l1_table
[i
]);
597 /* alloc L2 table/refcount block cache */
598 s
->l2_table_cache
= qcow2_cache_create(bs
, L2_CACHE_SIZE
);
599 s
->refcount_block_cache
= qcow2_cache_create(bs
, REFCOUNT_CACHE_SIZE
);
601 s
->cluster_cache
= g_malloc(s
->cluster_size
);
602 /* one more sector for decompressed data alignment */
603 s
->cluster_data
= qemu_blockalign(bs
, QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_size
605 s
->cluster_cache_offset
= -1;
608 ret
= qcow2_refcount_init(bs
);
610 error_setg_errno(errp
, -ret
, "Could not initialize refcount handling");
614 QLIST_INIT(&s
->cluster_allocs
);
615 QTAILQ_INIT(&s
->discards
);
617 /* read qcow2 extensions */
618 if (qcow2_read_extensions(bs
, header
.header_length
, ext_end
, NULL
,
620 error_propagate(errp
, local_err
);
625 /* read the backing file name */
626 if (header
.backing_file_offset
!= 0) {
627 len
= header
.backing_file_size
;
631 ret
= bdrv_pread(bs
->file
, header
.backing_file_offset
,
632 bs
->backing_file
, len
);
634 error_setg_errno(errp
, -ret
, "Could not read backing file name");
637 bs
->backing_file
[len
] = '\0';
640 ret
= qcow2_read_snapshots(bs
);
642 error_setg_errno(errp
, -ret
, "Could not read snapshots");
646 /* Clear unknown autoclear feature bits */
647 if (!bs
->read_only
&& !(flags
& BDRV_O_INCOMING
) && s
->autoclear_features
) {
648 s
->autoclear_features
= 0;
649 ret
= qcow2_update_header(bs
);
651 error_setg_errno(errp
, -ret
, "Could not update qcow2 header");
656 /* Initialise locks */
657 qemu_co_mutex_init(&s
->lock
);
659 /* Repair image if dirty */
660 if (!(flags
& (BDRV_O_CHECK
| BDRV_O_INCOMING
)) && !bs
->read_only
&&
661 (s
->incompatible_features
& QCOW2_INCOMPAT_DIRTY
)) {
662 BdrvCheckResult result
= {0};
664 ret
= qcow2_check(bs
, &result
, BDRV_FIX_ERRORS
);
666 error_setg_errno(errp
, -ret
, "Could not repair dirty image");
671 /* Enable lazy_refcounts according to image and command line options */
672 opts
= qemu_opts_create(&qcow2_runtime_opts
, NULL
, 0, &error_abort
);
673 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
675 error_propagate(errp
, local_err
);
680 s
->use_lazy_refcounts
= qemu_opt_get_bool(opts
, QCOW2_OPT_LAZY_REFCOUNTS
,
681 (s
->compatible_features
& QCOW2_COMPAT_LAZY_REFCOUNTS
));
683 s
->discard_passthrough
[QCOW2_DISCARD_NEVER
] = false;
684 s
->discard_passthrough
[QCOW2_DISCARD_ALWAYS
] = true;
685 s
->discard_passthrough
[QCOW2_DISCARD_REQUEST
] =
686 qemu_opt_get_bool(opts
, QCOW2_OPT_DISCARD_REQUEST
,
687 flags
& BDRV_O_UNMAP
);
688 s
->discard_passthrough
[QCOW2_DISCARD_SNAPSHOT
] =
689 qemu_opt_get_bool(opts
, QCOW2_OPT_DISCARD_SNAPSHOT
, true);
690 s
->discard_passthrough
[QCOW2_DISCARD_OTHER
] =
691 qemu_opt_get_bool(opts
, QCOW2_OPT_DISCARD_OTHER
, false);
693 opt_overlap_check
= qemu_opt_get(opts
, "overlap-check") ?: "cached";
694 if (!strcmp(opt_overlap_check
, "none")) {
695 overlap_check_template
= 0;
696 } else if (!strcmp(opt_overlap_check
, "constant")) {
697 overlap_check_template
= QCOW2_OL_CONSTANT
;
698 } else if (!strcmp(opt_overlap_check
, "cached")) {
699 overlap_check_template
= QCOW2_OL_CACHED
;
700 } else if (!strcmp(opt_overlap_check
, "all")) {
701 overlap_check_template
= QCOW2_OL_ALL
;
703 error_setg(errp
, "Unsupported value '%s' for qcow2 option "
704 "'overlap-check'. Allowed are either of the following: "
705 "none, constant, cached, all", opt_overlap_check
);
711 s
->overlap_check
= 0;
712 for (i
= 0; i
< QCOW2_OL_MAX_BITNR
; i
++) {
713 /* overlap-check defines a template bitmask, but every flag may be
714 * overwritten through the associated boolean option */
716 qemu_opt_get_bool(opts
, overlap_bool_option_names
[i
],
717 overlap_check_template
& (1 << i
)) << i
;
722 if (s
->use_lazy_refcounts
&& s
->qcow_version
< 3) {
723 error_setg(errp
, "Lazy refcounts require a qcow2 image with at least "
724 "qemu 1.1 compatibility level");
731 BdrvCheckResult result
= {0};
732 qcow2_check_refcounts(bs
, &result
, 0);
738 g_free(s
->unknown_header_fields
);
739 cleanup_unknown_header_ext(bs
);
740 qcow2_free_snapshots(bs
);
741 qcow2_refcount_close(bs
);
743 /* else pre-write overlap checks in cache_destroy may crash */
745 if (s
->l2_table_cache
) {
746 qcow2_cache_destroy(bs
, s
->l2_table_cache
);
748 g_free(s
->cluster_cache
);
749 qemu_vfree(s
->cluster_data
);
753 static int qcow2_refresh_limits(BlockDriverState
*bs
)
755 BDRVQcowState
*s
= bs
->opaque
;
757 bs
->bl
.write_zeroes_alignment
= s
->cluster_sectors
;
762 static int qcow2_set_key(BlockDriverState
*bs
, const char *key
)
764 BDRVQcowState
*s
= bs
->opaque
;
768 memset(keybuf
, 0, 16);
772 /* XXX: we could compress the chars to 7 bits to increase
774 for(i
= 0;i
< len
;i
++) {
777 s
->crypt_method
= s
->crypt_method_header
;
779 if (AES_set_encrypt_key(keybuf
, 128, &s
->aes_encrypt_key
) != 0)
781 if (AES_set_decrypt_key(keybuf
, 128, &s
->aes_decrypt_key
) != 0)
791 AES_encrypt(in
, tmp
, &s
->aes_encrypt_key
);
792 AES_decrypt(tmp
, out
, &s
->aes_decrypt_key
);
793 for(i
= 0; i
< 16; i
++)
794 printf(" %02x", tmp
[i
]);
796 for(i
= 0; i
< 16; i
++)
797 printf(" %02x", out
[i
]);
804 /* We have nothing to do for QCOW2 reopen, stubs just return
806 static int qcow2_reopen_prepare(BDRVReopenState
*state
,
807 BlockReopenQueue
*queue
, Error
**errp
)
812 static int64_t coroutine_fn
qcow2_co_get_block_status(BlockDriverState
*bs
,
813 int64_t sector_num
, int nb_sectors
, int *pnum
)
815 BDRVQcowState
*s
= bs
->opaque
;
816 uint64_t cluster_offset
;
817 int index_in_cluster
, ret
;
821 qemu_co_mutex_lock(&s
->lock
);
822 ret
= qcow2_get_cluster_offset(bs
, sector_num
<< 9, pnum
, &cluster_offset
);
823 qemu_co_mutex_unlock(&s
->lock
);
828 if (cluster_offset
!= 0 && ret
!= QCOW2_CLUSTER_COMPRESSED
&&
830 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
831 cluster_offset
|= (index_in_cluster
<< BDRV_SECTOR_BITS
);
832 status
|= BDRV_BLOCK_OFFSET_VALID
| cluster_offset
;
834 if (ret
== QCOW2_CLUSTER_ZERO
) {
835 status
|= BDRV_BLOCK_ZERO
;
836 } else if (ret
!= QCOW2_CLUSTER_UNALLOCATED
) {
837 status
|= BDRV_BLOCK_DATA
;
842 /* handle reading after the end of the backing file */
843 int qcow2_backing_read1(BlockDriverState
*bs
, QEMUIOVector
*qiov
,
844 int64_t sector_num
, int nb_sectors
)
847 if ((sector_num
+ nb_sectors
) <= bs
->total_sectors
)
849 if (sector_num
>= bs
->total_sectors
)
852 n1
= bs
->total_sectors
- sector_num
;
854 qemu_iovec_memset(qiov
, 512 * n1
, 0, 512 * (nb_sectors
- n1
));
859 static coroutine_fn
int qcow2_co_readv(BlockDriverState
*bs
, int64_t sector_num
,
860 int remaining_sectors
, QEMUIOVector
*qiov
)
862 BDRVQcowState
*s
= bs
->opaque
;
863 int index_in_cluster
, n1
;
865 int cur_nr_sectors
; /* number of sectors in current iteration */
866 uint64_t cluster_offset
= 0;
867 uint64_t bytes_done
= 0;
868 QEMUIOVector hd_qiov
;
869 uint8_t *cluster_data
= NULL
;
871 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
873 qemu_co_mutex_lock(&s
->lock
);
875 while (remaining_sectors
!= 0) {
877 /* prepare next request */
878 cur_nr_sectors
= remaining_sectors
;
879 if (s
->crypt_method
) {
880 cur_nr_sectors
= MIN(cur_nr_sectors
,
881 QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
);
884 ret
= qcow2_get_cluster_offset(bs
, sector_num
<< 9,
885 &cur_nr_sectors
, &cluster_offset
);
890 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
892 qemu_iovec_reset(&hd_qiov
);
893 qemu_iovec_concat(&hd_qiov
, qiov
, bytes_done
,
894 cur_nr_sectors
* 512);
897 case QCOW2_CLUSTER_UNALLOCATED
:
899 if (bs
->backing_hd
) {
900 /* read from the base image */
901 n1
= qcow2_backing_read1(bs
->backing_hd
, &hd_qiov
,
902 sector_num
, cur_nr_sectors
);
904 BLKDBG_EVENT(bs
->file
, BLKDBG_READ_BACKING_AIO
);
905 qemu_co_mutex_unlock(&s
->lock
);
906 ret
= bdrv_co_readv(bs
->backing_hd
, sector_num
,
908 qemu_co_mutex_lock(&s
->lock
);
914 /* Note: in this case, no need to wait */
915 qemu_iovec_memset(&hd_qiov
, 0, 0, 512 * cur_nr_sectors
);
919 case QCOW2_CLUSTER_ZERO
:
920 qemu_iovec_memset(&hd_qiov
, 0, 0, 512 * cur_nr_sectors
);
923 case QCOW2_CLUSTER_COMPRESSED
:
924 /* add AIO support for compressed blocks ? */
925 ret
= qcow2_decompress_cluster(bs
, cluster_offset
);
930 qemu_iovec_from_buf(&hd_qiov
, 0,
931 s
->cluster_cache
+ index_in_cluster
* 512,
932 512 * cur_nr_sectors
);
935 case QCOW2_CLUSTER_NORMAL
:
936 if ((cluster_offset
& 511) != 0) {
941 if (s
->crypt_method
) {
943 * For encrypted images, read everything into a temporary
944 * contiguous buffer on which the AES functions can work.
948 qemu_blockalign(bs
, QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_size
);
951 assert(cur_nr_sectors
<=
952 QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
);
953 qemu_iovec_reset(&hd_qiov
);
954 qemu_iovec_add(&hd_qiov
, cluster_data
,
955 512 * cur_nr_sectors
);
958 BLKDBG_EVENT(bs
->file
, BLKDBG_READ_AIO
);
959 qemu_co_mutex_unlock(&s
->lock
);
960 ret
= bdrv_co_readv(bs
->file
,
961 (cluster_offset
>> 9) + index_in_cluster
,
962 cur_nr_sectors
, &hd_qiov
);
963 qemu_co_mutex_lock(&s
->lock
);
967 if (s
->crypt_method
) {
968 qcow2_encrypt_sectors(s
, sector_num
, cluster_data
,
969 cluster_data
, cur_nr_sectors
, 0, &s
->aes_decrypt_key
);
970 qemu_iovec_from_buf(qiov
, bytes_done
,
971 cluster_data
, 512 * cur_nr_sectors
);
976 g_assert_not_reached();
981 remaining_sectors
-= cur_nr_sectors
;
982 sector_num
+= cur_nr_sectors
;
983 bytes_done
+= cur_nr_sectors
* 512;
988 qemu_co_mutex_unlock(&s
->lock
);
990 qemu_iovec_destroy(&hd_qiov
);
991 qemu_vfree(cluster_data
);
996 static coroutine_fn
int qcow2_co_writev(BlockDriverState
*bs
,
998 int remaining_sectors
,
1001 BDRVQcowState
*s
= bs
->opaque
;
1002 int index_in_cluster
;
1004 int cur_nr_sectors
; /* number of sectors in current iteration */
1005 uint64_t cluster_offset
;
1006 QEMUIOVector hd_qiov
;
1007 uint64_t bytes_done
= 0;
1008 uint8_t *cluster_data
= NULL
;
1009 QCowL2Meta
*l2meta
= NULL
;
1011 trace_qcow2_writev_start_req(qemu_coroutine_self(), sector_num
,
1014 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
1016 s
->cluster_cache_offset
= -1; /* disable compressed cache */
1018 qemu_co_mutex_lock(&s
->lock
);
1020 while (remaining_sectors
!= 0) {
1024 trace_qcow2_writev_start_part(qemu_coroutine_self());
1025 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
1026 cur_nr_sectors
= remaining_sectors
;
1027 if (s
->crypt_method
&&
1029 QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
- index_in_cluster
) {
1031 QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
- index_in_cluster
;
1034 ret
= qcow2_alloc_cluster_offset(bs
, sector_num
<< 9,
1035 &cur_nr_sectors
, &cluster_offset
, &l2meta
);
1040 assert((cluster_offset
& 511) == 0);
1042 qemu_iovec_reset(&hd_qiov
);
1043 qemu_iovec_concat(&hd_qiov
, qiov
, bytes_done
,
1044 cur_nr_sectors
* 512);
1046 if (s
->crypt_method
) {
1047 if (!cluster_data
) {
1048 cluster_data
= qemu_blockalign(bs
, QCOW_MAX_CRYPT_CLUSTERS
*
1052 assert(hd_qiov
.size
<=
1053 QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_size
);
1054 qemu_iovec_to_buf(&hd_qiov
, 0, cluster_data
, hd_qiov
.size
);
1056 qcow2_encrypt_sectors(s
, sector_num
, cluster_data
,
1057 cluster_data
, cur_nr_sectors
, 1, &s
->aes_encrypt_key
);
1059 qemu_iovec_reset(&hd_qiov
);
1060 qemu_iovec_add(&hd_qiov
, cluster_data
,
1061 cur_nr_sectors
* 512);
1064 ret
= qcow2_pre_write_overlap_check(bs
, 0,
1065 cluster_offset
+ index_in_cluster
* BDRV_SECTOR_SIZE
,
1066 cur_nr_sectors
* BDRV_SECTOR_SIZE
);
1071 qemu_co_mutex_unlock(&s
->lock
);
1072 BLKDBG_EVENT(bs
->file
, BLKDBG_WRITE_AIO
);
1073 trace_qcow2_writev_data(qemu_coroutine_self(),
1074 (cluster_offset
>> 9) + index_in_cluster
);
1075 ret
= bdrv_co_writev(bs
->file
,
1076 (cluster_offset
>> 9) + index_in_cluster
,
1077 cur_nr_sectors
, &hd_qiov
);
1078 qemu_co_mutex_lock(&s
->lock
);
1083 while (l2meta
!= NULL
) {
1086 ret
= qcow2_alloc_cluster_link_l2(bs
, l2meta
);
1091 /* Take the request off the list of running requests */
1092 if (l2meta
->nb_clusters
!= 0) {
1093 QLIST_REMOVE(l2meta
, next_in_flight
);
1096 qemu_co_queue_restart_all(&l2meta
->dependent_requests
);
1098 next
= l2meta
->next
;
1103 remaining_sectors
-= cur_nr_sectors
;
1104 sector_num
+= cur_nr_sectors
;
1105 bytes_done
+= cur_nr_sectors
* 512;
1106 trace_qcow2_writev_done_part(qemu_coroutine_self(), cur_nr_sectors
);
1111 qemu_co_mutex_unlock(&s
->lock
);
1113 while (l2meta
!= NULL
) {
1116 if (l2meta
->nb_clusters
!= 0) {
1117 QLIST_REMOVE(l2meta
, next_in_flight
);
1119 qemu_co_queue_restart_all(&l2meta
->dependent_requests
);
1121 next
= l2meta
->next
;
1126 qemu_iovec_destroy(&hd_qiov
);
1127 qemu_vfree(cluster_data
);
1128 trace_qcow2_writev_done_req(qemu_coroutine_self(), ret
);
1133 static void qcow2_close(BlockDriverState
*bs
)
1135 BDRVQcowState
*s
= bs
->opaque
;
1136 g_free(s
->l1_table
);
1137 /* else pre-write overlap checks in cache_destroy may crash */
1140 if (!(bs
->open_flags
& BDRV_O_INCOMING
)) {
1141 qcow2_cache_flush(bs
, s
->l2_table_cache
);
1142 qcow2_cache_flush(bs
, s
->refcount_block_cache
);
1144 qcow2_mark_clean(bs
);
1147 qcow2_cache_destroy(bs
, s
->l2_table_cache
);
1148 qcow2_cache_destroy(bs
, s
->refcount_block_cache
);
1150 g_free(s
->unknown_header_fields
);
1151 cleanup_unknown_header_ext(bs
);
1153 g_free(s
->cluster_cache
);
1154 qemu_vfree(s
->cluster_data
);
1155 qcow2_refcount_close(bs
);
1156 qcow2_free_snapshots(bs
);
1159 static void qcow2_invalidate_cache(BlockDriverState
*bs
, Error
**errp
)
1161 BDRVQcowState
*s
= bs
->opaque
;
1162 int flags
= s
->flags
;
1163 AES_KEY aes_encrypt_key
;
1164 AES_KEY aes_decrypt_key
;
1165 uint32_t crypt_method
= 0;
1167 Error
*local_err
= NULL
;
1171 * Backing files are read-only which makes all of their metadata immutable,
1172 * that means we don't have to worry about reopening them here.
1175 if (s
->crypt_method
) {
1176 crypt_method
= s
->crypt_method
;
1177 memcpy(&aes_encrypt_key
, &s
->aes_encrypt_key
, sizeof(aes_encrypt_key
));
1178 memcpy(&aes_decrypt_key
, &s
->aes_decrypt_key
, sizeof(aes_decrypt_key
));
1183 bdrv_invalidate_cache(bs
->file
, &local_err
);
1185 error_propagate(errp
, local_err
);
1189 memset(s
, 0, sizeof(BDRVQcowState
));
1190 options
= qdict_clone_shallow(bs
->options
);
1192 ret
= qcow2_open(bs
, options
, flags
, &local_err
);
1194 error_setg(errp
, "Could not reopen qcow2 layer: %s",
1195 error_get_pretty(local_err
));
1196 error_free(local_err
);
1198 } else if (ret
< 0) {
1199 error_setg_errno(errp
, -ret
, "Could not reopen qcow2 layer");
1206 s
->crypt_method
= crypt_method
;
1207 memcpy(&s
->aes_encrypt_key
, &aes_encrypt_key
, sizeof(aes_encrypt_key
));
1208 memcpy(&s
->aes_decrypt_key
, &aes_decrypt_key
, sizeof(aes_decrypt_key
));
1212 static size_t header_ext_add(char *buf
, uint32_t magic
, const void *s
,
1213 size_t len
, size_t buflen
)
1215 QCowExtension
*ext_backing_fmt
= (QCowExtension
*) buf
;
1216 size_t ext_len
= sizeof(QCowExtension
) + ((len
+ 7) & ~7);
1218 if (buflen
< ext_len
) {
1222 *ext_backing_fmt
= (QCowExtension
) {
1223 .magic
= cpu_to_be32(magic
),
1224 .len
= cpu_to_be32(len
),
1226 memcpy(buf
+ sizeof(QCowExtension
), s
, len
);
1232 * Updates the qcow2 header, including the variable length parts of it, i.e.
1233 * the backing file name and all extensions. qcow2 was not designed to allow
1234 * such changes, so if we run out of space (we can only use the first cluster)
1235 * this function may fail.
1237 * Returns 0 on success, -errno in error cases.
1239 int qcow2_update_header(BlockDriverState
*bs
)
1241 BDRVQcowState
*s
= bs
->opaque
;
1244 size_t buflen
= s
->cluster_size
;
1246 uint64_t total_size
;
1247 uint32_t refcount_table_clusters
;
1248 size_t header_length
;
1249 Qcow2UnknownHeaderExtension
*uext
;
1251 buf
= qemu_blockalign(bs
, buflen
);
1253 /* Header structure */
1254 header
= (QCowHeader
*) buf
;
1256 if (buflen
< sizeof(*header
)) {
1261 header_length
= sizeof(*header
) + s
->unknown_header_fields_size
;
1262 total_size
= bs
->total_sectors
* BDRV_SECTOR_SIZE
;
1263 refcount_table_clusters
= s
->refcount_table_size
>> (s
->cluster_bits
- 3);
1265 *header
= (QCowHeader
) {
1266 /* Version 2 fields */
1267 .magic
= cpu_to_be32(QCOW_MAGIC
),
1268 .version
= cpu_to_be32(s
->qcow_version
),
1269 .backing_file_offset
= 0,
1270 .backing_file_size
= 0,
1271 .cluster_bits
= cpu_to_be32(s
->cluster_bits
),
1272 .size
= cpu_to_be64(total_size
),
1273 .crypt_method
= cpu_to_be32(s
->crypt_method_header
),
1274 .l1_size
= cpu_to_be32(s
->l1_size
),
1275 .l1_table_offset
= cpu_to_be64(s
->l1_table_offset
),
1276 .refcount_table_offset
= cpu_to_be64(s
->refcount_table_offset
),
1277 .refcount_table_clusters
= cpu_to_be32(refcount_table_clusters
),
1278 .nb_snapshots
= cpu_to_be32(s
->nb_snapshots
),
1279 .snapshots_offset
= cpu_to_be64(s
->snapshots_offset
),
1281 /* Version 3 fields */
1282 .incompatible_features
= cpu_to_be64(s
->incompatible_features
),
1283 .compatible_features
= cpu_to_be64(s
->compatible_features
),
1284 .autoclear_features
= cpu_to_be64(s
->autoclear_features
),
1285 .refcount_order
= cpu_to_be32(s
->refcount_order
),
1286 .header_length
= cpu_to_be32(header_length
),
1289 /* For older versions, write a shorter header */
1290 switch (s
->qcow_version
) {
1292 ret
= offsetof(QCowHeader
, incompatible_features
);
1295 ret
= sizeof(*header
);
1304 memset(buf
, 0, buflen
);
1306 /* Preserve any unknown field in the header */
1307 if (s
->unknown_header_fields_size
) {
1308 if (buflen
< s
->unknown_header_fields_size
) {
1313 memcpy(buf
, s
->unknown_header_fields
, s
->unknown_header_fields_size
);
1314 buf
+= s
->unknown_header_fields_size
;
1315 buflen
-= s
->unknown_header_fields_size
;
1318 /* Backing file format header extension */
1319 if (*bs
->backing_format
) {
1320 ret
= header_ext_add(buf
, QCOW2_EXT_MAGIC_BACKING_FORMAT
,
1321 bs
->backing_format
, strlen(bs
->backing_format
),
1332 Qcow2Feature features
[] = {
1334 .type
= QCOW2_FEAT_TYPE_INCOMPATIBLE
,
1335 .bit
= QCOW2_INCOMPAT_DIRTY_BITNR
,
1336 .name
= "dirty bit",
1339 .type
= QCOW2_FEAT_TYPE_INCOMPATIBLE
,
1340 .bit
= QCOW2_INCOMPAT_CORRUPT_BITNR
,
1341 .name
= "corrupt bit",
1344 .type
= QCOW2_FEAT_TYPE_COMPATIBLE
,
1345 .bit
= QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR
,
1346 .name
= "lazy refcounts",
1350 ret
= header_ext_add(buf
, QCOW2_EXT_MAGIC_FEATURE_TABLE
,
1351 features
, sizeof(features
), buflen
);
1358 /* Keep unknown header extensions */
1359 QLIST_FOREACH(uext
, &s
->unknown_header_ext
, next
) {
1360 ret
= header_ext_add(buf
, uext
->magic
, uext
->data
, uext
->len
, buflen
);
1369 /* End of header extensions */
1370 ret
= header_ext_add(buf
, QCOW2_EXT_MAGIC_END
, NULL
, 0, buflen
);
1378 /* Backing file name */
1379 if (*bs
->backing_file
) {
1380 size_t backing_file_len
= strlen(bs
->backing_file
);
1382 if (buflen
< backing_file_len
) {
1387 /* Using strncpy is ok here, since buf is not NUL-terminated. */
1388 strncpy(buf
, bs
->backing_file
, buflen
);
1390 header
->backing_file_offset
= cpu_to_be64(buf
- ((char*) header
));
1391 header
->backing_file_size
= cpu_to_be32(backing_file_len
);
1394 /* Write the new header */
1395 ret
= bdrv_pwrite(bs
->file
, 0, header
, s
->cluster_size
);
1406 static int qcow2_change_backing_file(BlockDriverState
*bs
,
1407 const char *backing_file
, const char *backing_fmt
)
1409 pstrcpy(bs
->backing_file
, sizeof(bs
->backing_file
), backing_file
?: "");
1410 pstrcpy(bs
->backing_format
, sizeof(bs
->backing_format
), backing_fmt
?: "");
1412 return qcow2_update_header(bs
);
1415 static int preallocate(BlockDriverState
*bs
)
1417 uint64_t nb_sectors
;
1419 uint64_t host_offset
= 0;
1424 nb_sectors
= bdrv_getlength(bs
) >> BDRV_SECTOR_BITS
;
1427 while (nb_sectors
) {
1428 num
= MIN(nb_sectors
, INT_MAX
>> BDRV_SECTOR_BITS
);
1429 ret
= qcow2_alloc_cluster_offset(bs
, offset
, &num
,
1430 &host_offset
, &meta
);
1436 ret
= qcow2_alloc_cluster_link_l2(bs
, meta
);
1438 qcow2_free_any_clusters(bs
, meta
->alloc_offset
,
1439 meta
->nb_clusters
, QCOW2_DISCARD_NEVER
);
1443 /* There are no dependent requests, but we need to remove our
1444 * request from the list of in-flight requests */
1445 QLIST_REMOVE(meta
, next_in_flight
);
1448 /* TODO Preallocate data if requested */
1451 offset
+= num
<< BDRV_SECTOR_BITS
;
1455 * It is expected that the image file is large enough to actually contain
1456 * all of the allocated clusters (otherwise we get failing reads after
1457 * EOF). Extend the image to the last allocated sector.
1459 if (host_offset
!= 0) {
1460 uint8_t buf
[BDRV_SECTOR_SIZE
];
1461 memset(buf
, 0, BDRV_SECTOR_SIZE
);
1462 ret
= bdrv_write(bs
->file
, (host_offset
>> BDRV_SECTOR_BITS
) + num
- 1,
1472 static int qcow2_create2(const char *filename
, int64_t total_size
,
1473 const char *backing_file
, const char *backing_format
,
1474 int flags
, size_t cluster_size
, int prealloc
,
1475 QEMUOptionParameter
*options
, int version
,
1478 /* Calculate cluster_bits */
1480 cluster_bits
= ffs(cluster_size
) - 1;
1481 if (cluster_bits
< MIN_CLUSTER_BITS
|| cluster_bits
> MAX_CLUSTER_BITS
||
1482 (1 << cluster_bits
) != cluster_size
)
1484 error_setg(errp
, "Cluster size must be a power of two between %d and "
1485 "%dk", 1 << MIN_CLUSTER_BITS
, 1 << (MAX_CLUSTER_BITS
- 10));
1490 * Open the image file and write a minimal qcow2 header.
1492 * We keep things simple and start with a zero-sized image. We also
1493 * do without refcount blocks or a L1 table for now. We'll fix the
1494 * inconsistency later.
1496 * We do need a refcount table because growing the refcount table means
1497 * allocating two new refcount blocks - the seconds of which would be at
1498 * 2 GB for 64k clusters, and we don't want to have a 2 GB initial file
1499 * size for any qcow2 image.
1501 BlockDriverState
* bs
;
1503 uint8_t* refcount_table
;
1504 Error
*local_err
= NULL
;
1507 ret
= bdrv_create_file(filename
, options
, &local_err
);
1509 error_propagate(errp
, local_err
);
1514 ret
= bdrv_open(&bs
, filename
, NULL
, NULL
, BDRV_O_RDWR
| BDRV_O_PROTOCOL
,
1517 error_propagate(errp
, local_err
);
1521 /* Write the header */
1522 QEMU_BUILD_BUG_ON((1 << MIN_CLUSTER_BITS
) < sizeof(*header
));
1523 header
= g_malloc0(cluster_size
);
1524 *header
= (QCowHeader
) {
1525 .magic
= cpu_to_be32(QCOW_MAGIC
),
1526 .version
= cpu_to_be32(version
),
1527 .cluster_bits
= cpu_to_be32(cluster_bits
),
1528 .size
= cpu_to_be64(0),
1529 .l1_table_offset
= cpu_to_be64(0),
1530 .l1_size
= cpu_to_be32(0),
1531 .refcount_table_offset
= cpu_to_be64(cluster_size
),
1532 .refcount_table_clusters
= cpu_to_be32(1),
1533 .refcount_order
= cpu_to_be32(3 + REFCOUNT_SHIFT
),
1534 .header_length
= cpu_to_be32(sizeof(*header
)),
1537 if (flags
& BLOCK_FLAG_ENCRYPT
) {
1538 header
->crypt_method
= cpu_to_be32(QCOW_CRYPT_AES
);
1540 header
->crypt_method
= cpu_to_be32(QCOW_CRYPT_NONE
);
1543 if (flags
& BLOCK_FLAG_LAZY_REFCOUNTS
) {
1544 header
->compatible_features
|=
1545 cpu_to_be64(QCOW2_COMPAT_LAZY_REFCOUNTS
);
1548 ret
= bdrv_pwrite(bs
, 0, header
, cluster_size
);
1551 error_setg_errno(errp
, -ret
, "Could not write qcow2 header");
1555 /* Write an empty refcount table */
1556 refcount_table
= g_malloc0(cluster_size
);
1557 ret
= bdrv_pwrite(bs
, cluster_size
, refcount_table
, cluster_size
);
1558 g_free(refcount_table
);
1561 error_setg_errno(errp
, -ret
, "Could not write refcount table");
1569 * And now open the image and make it consistent first (i.e. increase the
1570 * refcount of the cluster that is occupied by the header and the refcount
1573 BlockDriver
* drv
= bdrv_find_format("qcow2");
1574 assert(drv
!= NULL
);
1575 ret
= bdrv_open(&bs
, filename
, NULL
, NULL
,
1576 BDRV_O_RDWR
| BDRV_O_CACHE_WB
| BDRV_O_NO_FLUSH
, drv
, &local_err
);
1578 error_propagate(errp
, local_err
);
1582 ret
= qcow2_alloc_clusters(bs
, 2 * cluster_size
);
1584 error_setg_errno(errp
, -ret
, "Could not allocate clusters for qcow2 "
1585 "header and refcount table");
1588 } else if (ret
!= 0) {
1589 error_report("Huh, first cluster in empty image is already in use?");
1593 /* Okay, now that we have a valid image, let's give it the right size */
1594 ret
= bdrv_truncate(bs
, total_size
* BDRV_SECTOR_SIZE
);
1596 error_setg_errno(errp
, -ret
, "Could not resize image");
1600 /* Want a backing file? There you go.*/
1602 ret
= bdrv_change_backing_file(bs
, backing_file
, backing_format
);
1604 error_setg_errno(errp
, -ret
, "Could not assign backing file '%s' "
1605 "with format '%s'", backing_file
, backing_format
);
1610 /* And if we're supposed to preallocate metadata, do that now */
1612 BDRVQcowState
*s
= bs
->opaque
;
1613 qemu_co_mutex_lock(&s
->lock
);
1614 ret
= preallocate(bs
);
1615 qemu_co_mutex_unlock(&s
->lock
);
1617 error_setg_errno(errp
, -ret
, "Could not preallocate metadata");
1625 /* Reopen the image without BDRV_O_NO_FLUSH to flush it before returning */
1626 ret
= bdrv_open(&bs
, filename
, NULL
, NULL
,
1627 BDRV_O_RDWR
| BDRV_O_CACHE_WB
| BDRV_O_NO_BACKING
,
1630 error_propagate(errp
, local_err
);
1642 static int qcow2_create(const char *filename
, QEMUOptionParameter
*options
,
1645 const char *backing_file
= NULL
;
1646 const char *backing_fmt
= NULL
;
1647 uint64_t sectors
= 0;
1649 size_t cluster_size
= DEFAULT_CLUSTER_SIZE
;
1652 Error
*local_err
= NULL
;
1655 /* Read out options */
1656 while (options
&& options
->name
) {
1657 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
1658 sectors
= options
->value
.n
/ 512;
1659 } else if (!strcmp(options
->name
, BLOCK_OPT_BACKING_FILE
)) {
1660 backing_file
= options
->value
.s
;
1661 } else if (!strcmp(options
->name
, BLOCK_OPT_BACKING_FMT
)) {
1662 backing_fmt
= options
->value
.s
;
1663 } else if (!strcmp(options
->name
, BLOCK_OPT_ENCRYPT
)) {
1664 flags
|= options
->value
.n
? BLOCK_FLAG_ENCRYPT
: 0;
1665 } else if (!strcmp(options
->name
, BLOCK_OPT_CLUSTER_SIZE
)) {
1666 if (options
->value
.n
) {
1667 cluster_size
= options
->value
.n
;
1669 } else if (!strcmp(options
->name
, BLOCK_OPT_PREALLOC
)) {
1670 if (!options
->value
.s
|| !strcmp(options
->value
.s
, "off")) {
1672 } else if (!strcmp(options
->value
.s
, "metadata")) {
1675 error_setg(errp
, "Invalid preallocation mode: '%s'",
1679 } else if (!strcmp(options
->name
, BLOCK_OPT_COMPAT_LEVEL
)) {
1680 if (!options
->value
.s
) {
1681 /* keep the default */
1682 } else if (!strcmp(options
->value
.s
, "0.10")) {
1684 } else if (!strcmp(options
->value
.s
, "1.1")) {
1687 error_setg(errp
, "Invalid compatibility level: '%s'",
1691 } else if (!strcmp(options
->name
, BLOCK_OPT_LAZY_REFCOUNTS
)) {
1692 flags
|= options
->value
.n
? BLOCK_FLAG_LAZY_REFCOUNTS
: 0;
1697 if (backing_file
&& prealloc
) {
1698 error_setg(errp
, "Backing file and preallocation cannot be used at "
1703 if (version
< 3 && (flags
& BLOCK_FLAG_LAZY_REFCOUNTS
)) {
1704 error_setg(errp
, "Lazy refcounts only supported with compatibility "
1705 "level 1.1 and above (use compat=1.1 or greater)");
1709 ret
= qcow2_create2(filename
, sectors
, backing_file
, backing_fmt
, flags
,
1710 cluster_size
, prealloc
, options
, version
, &local_err
);
1712 error_propagate(errp
, local_err
);
1717 static coroutine_fn
int qcow2_co_write_zeroes(BlockDriverState
*bs
,
1718 int64_t sector_num
, int nb_sectors
, BdrvRequestFlags flags
)
1721 BDRVQcowState
*s
= bs
->opaque
;
1723 /* Emulate misaligned zero writes */
1724 if (sector_num
% s
->cluster_sectors
|| nb_sectors
% s
->cluster_sectors
) {
1728 /* Whatever is left can use real zero clusters */
1729 qemu_co_mutex_lock(&s
->lock
);
1730 ret
= qcow2_zero_clusters(bs
, sector_num
<< BDRV_SECTOR_BITS
,
1732 qemu_co_mutex_unlock(&s
->lock
);
1737 static coroutine_fn
int qcow2_co_discard(BlockDriverState
*bs
,
1738 int64_t sector_num
, int nb_sectors
)
1741 BDRVQcowState
*s
= bs
->opaque
;
1743 qemu_co_mutex_lock(&s
->lock
);
1744 ret
= qcow2_discard_clusters(bs
, sector_num
<< BDRV_SECTOR_BITS
,
1745 nb_sectors
, QCOW2_DISCARD_REQUEST
);
1746 qemu_co_mutex_unlock(&s
->lock
);
1750 static int qcow2_truncate(BlockDriverState
*bs
, int64_t offset
)
1752 BDRVQcowState
*s
= bs
->opaque
;
1753 int64_t new_l1_size
;
1757 error_report("The new size must be a multiple of 512");
1761 /* cannot proceed if image has snapshots */
1762 if (s
->nb_snapshots
) {
1763 error_report("Can't resize an image which has snapshots");
1767 /* shrinking is currently not supported */
1768 if (offset
< bs
->total_sectors
* 512) {
1769 error_report("qcow2 doesn't support shrinking images yet");
1773 new_l1_size
= size_to_l1(s
, offset
);
1774 ret
= qcow2_grow_l1_table(bs
, new_l1_size
, true);
1779 /* write updated header.size */
1780 offset
= cpu_to_be64(offset
);
1781 ret
= bdrv_pwrite_sync(bs
->file
, offsetof(QCowHeader
, size
),
1782 &offset
, sizeof(uint64_t));
1787 s
->l1_vm_state_index
= new_l1_size
;
1791 /* XXX: put compressed sectors first, then all the cluster aligned
1792 tables to avoid losing bytes in alignment */
1793 static int qcow2_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
1794 const uint8_t *buf
, int nb_sectors
)
1796 BDRVQcowState
*s
= bs
->opaque
;
1800 uint64_t cluster_offset
;
1802 if (nb_sectors
== 0) {
1803 /* align end of file to a sector boundary to ease reading with
1804 sector based I/Os */
1805 cluster_offset
= bdrv_getlength(bs
->file
);
1806 cluster_offset
= (cluster_offset
+ 511) & ~511;
1807 bdrv_truncate(bs
->file
, cluster_offset
);
1811 if (nb_sectors
!= s
->cluster_sectors
) {
1814 /* Zero-pad last write if image size is not cluster aligned */
1815 if (sector_num
+ nb_sectors
== bs
->total_sectors
&&
1816 nb_sectors
< s
->cluster_sectors
) {
1817 uint8_t *pad_buf
= qemu_blockalign(bs
, s
->cluster_size
);
1818 memset(pad_buf
, 0, s
->cluster_size
);
1819 memcpy(pad_buf
, buf
, nb_sectors
* BDRV_SECTOR_SIZE
);
1820 ret
= qcow2_write_compressed(bs
, sector_num
,
1821 pad_buf
, s
->cluster_sectors
);
1822 qemu_vfree(pad_buf
);
1827 out_buf
= g_malloc(s
->cluster_size
+ (s
->cluster_size
/ 1000) + 128);
1829 /* best compression, small window, no zlib header */
1830 memset(&strm
, 0, sizeof(strm
));
1831 ret
= deflateInit2(&strm
, Z_DEFAULT_COMPRESSION
,
1833 9, Z_DEFAULT_STRATEGY
);
1839 strm
.avail_in
= s
->cluster_size
;
1840 strm
.next_in
= (uint8_t *)buf
;
1841 strm
.avail_out
= s
->cluster_size
;
1842 strm
.next_out
= out_buf
;
1844 ret
= deflate(&strm
, Z_FINISH
);
1845 if (ret
!= Z_STREAM_END
&& ret
!= Z_OK
) {
1850 out_len
= strm
.next_out
- out_buf
;
1854 if (ret
!= Z_STREAM_END
|| out_len
>= s
->cluster_size
) {
1855 /* could not compress: write normal cluster */
1856 ret
= bdrv_write(bs
, sector_num
, buf
, s
->cluster_sectors
);
1861 cluster_offset
= qcow2_alloc_compressed_cluster_offset(bs
,
1862 sector_num
<< 9, out_len
);
1863 if (!cluster_offset
) {
1867 cluster_offset
&= s
->cluster_offset_mask
;
1869 ret
= qcow2_pre_write_overlap_check(bs
, 0, cluster_offset
, out_len
);
1874 BLKDBG_EVENT(bs
->file
, BLKDBG_WRITE_COMPRESSED
);
1875 ret
= bdrv_pwrite(bs
->file
, cluster_offset
, out_buf
, out_len
);
1887 static coroutine_fn
int qcow2_co_flush_to_os(BlockDriverState
*bs
)
1889 BDRVQcowState
*s
= bs
->opaque
;
1892 qemu_co_mutex_lock(&s
->lock
);
1893 ret
= qcow2_cache_flush(bs
, s
->l2_table_cache
);
1895 qemu_co_mutex_unlock(&s
->lock
);
1899 if (qcow2_need_accurate_refcounts(s
)) {
1900 ret
= qcow2_cache_flush(bs
, s
->refcount_block_cache
);
1902 qemu_co_mutex_unlock(&s
->lock
);
1906 qemu_co_mutex_unlock(&s
->lock
);
1911 static int qcow2_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
1913 BDRVQcowState
*s
= bs
->opaque
;
1914 bdi
->unallocated_blocks_are_zero
= true;
1915 bdi
->can_write_zeroes_with_unmap
= (s
->qcow_version
>= 3);
1916 bdi
->cluster_size
= s
->cluster_size
;
1917 bdi
->vm_state_offset
= qcow2_vm_state_offset(s
);
1921 static ImageInfoSpecific
*qcow2_get_specific_info(BlockDriverState
*bs
)
1923 BDRVQcowState
*s
= bs
->opaque
;
1924 ImageInfoSpecific
*spec_info
= g_new(ImageInfoSpecific
, 1);
1926 *spec_info
= (ImageInfoSpecific
){
1927 .kind
= IMAGE_INFO_SPECIFIC_KIND_QCOW2
,
1929 .qcow2
= g_new(ImageInfoSpecificQCow2
, 1),
1932 if (s
->qcow_version
== 2) {
1933 *spec_info
->qcow2
= (ImageInfoSpecificQCow2
){
1934 .compat
= g_strdup("0.10"),
1936 } else if (s
->qcow_version
== 3) {
1937 *spec_info
->qcow2
= (ImageInfoSpecificQCow2
){
1938 .compat
= g_strdup("1.1"),
1939 .lazy_refcounts
= s
->compatible_features
&
1940 QCOW2_COMPAT_LAZY_REFCOUNTS
,
1941 .has_lazy_refcounts
= true,
1949 static void dump_refcounts(BlockDriverState
*bs
)
1951 BDRVQcowState
*s
= bs
->opaque
;
1952 int64_t nb_clusters
, k
, k1
, size
;
1955 size
= bdrv_getlength(bs
->file
);
1956 nb_clusters
= size_to_clusters(s
, size
);
1957 for(k
= 0; k
< nb_clusters
;) {
1959 refcount
= get_refcount(bs
, k
);
1961 while (k
< nb_clusters
&& get_refcount(bs
, k
) == refcount
)
1963 printf("%" PRId64
": refcount=%d nb=%" PRId64
"\n", k
, refcount
,
1969 static int qcow2_save_vmstate(BlockDriverState
*bs
, QEMUIOVector
*qiov
,
1972 BDRVQcowState
*s
= bs
->opaque
;
1973 int64_t total_sectors
= bs
->total_sectors
;
1974 int growable
= bs
->growable
;
1975 bool zero_beyond_eof
= bs
->zero_beyond_eof
;
1978 BLKDBG_EVENT(bs
->file
, BLKDBG_VMSTATE_SAVE
);
1980 bs
->zero_beyond_eof
= false;
1981 ret
= bdrv_pwritev(bs
, qcow2_vm_state_offset(s
) + pos
, qiov
);
1982 bs
->growable
= growable
;
1983 bs
->zero_beyond_eof
= zero_beyond_eof
;
1985 /* bdrv_co_do_writev will have increased the total_sectors value to include
1986 * the VM state - the VM state is however not an actual part of the block
1987 * device, therefore, we need to restore the old value. */
1988 bs
->total_sectors
= total_sectors
;
1993 static int qcow2_load_vmstate(BlockDriverState
*bs
, uint8_t *buf
,
1994 int64_t pos
, int size
)
1996 BDRVQcowState
*s
= bs
->opaque
;
1997 int growable
= bs
->growable
;
1998 bool zero_beyond_eof
= bs
->zero_beyond_eof
;
2001 BLKDBG_EVENT(bs
->file
, BLKDBG_VMSTATE_LOAD
);
2003 bs
->zero_beyond_eof
= false;
2004 ret
= bdrv_pread(bs
, qcow2_vm_state_offset(s
) + pos
, buf
, size
);
2005 bs
->growable
= growable
;
2006 bs
->zero_beyond_eof
= zero_beyond_eof
;
2012 * Downgrades an image's version. To achieve this, any incompatible features
2013 * have to be removed.
2015 static int qcow2_downgrade(BlockDriverState
*bs
, int target_version
)
2017 BDRVQcowState
*s
= bs
->opaque
;
2018 int current_version
= s
->qcow_version
;
2021 if (target_version
== current_version
) {
2023 } else if (target_version
> current_version
) {
2025 } else if (target_version
!= 2) {
2029 if (s
->refcount_order
!= 4) {
2030 /* we would have to convert the image to a refcount_order == 4 image
2031 * here; however, since qemu (at the time of writing this) does not
2032 * support anything different than 4 anyway, there is no point in doing
2033 * so right now; however, we should error out (if qemu supports this in
2034 * the future and this code has not been adapted) */
2035 error_report("qcow2_downgrade: Image refcount orders other than 4 are "
2036 "currently not supported.");
2040 /* clear incompatible features */
2041 if (s
->incompatible_features
& QCOW2_INCOMPAT_DIRTY
) {
2042 ret
= qcow2_mark_clean(bs
);
2048 /* with QCOW2_INCOMPAT_CORRUPT, it is pretty much impossible to get here in
2049 * the first place; if that happens nonetheless, returning -ENOTSUP is the
2050 * best thing to do anyway */
2052 if (s
->incompatible_features
) {
2056 /* since we can ignore compatible features, we can set them to 0 as well */
2057 s
->compatible_features
= 0;
2058 /* if lazy refcounts have been used, they have already been fixed through
2059 * clearing the dirty flag */
2061 /* clearing autoclear features is trivial */
2062 s
->autoclear_features
= 0;
2064 ret
= qcow2_expand_zero_clusters(bs
);
2069 s
->qcow_version
= target_version
;
2070 ret
= qcow2_update_header(bs
);
2072 s
->qcow_version
= current_version
;
2078 static int qcow2_amend_options(BlockDriverState
*bs
,
2079 QEMUOptionParameter
*options
)
2081 BDRVQcowState
*s
= bs
->opaque
;
2082 int old_version
= s
->qcow_version
, new_version
= old_version
;
2083 uint64_t new_size
= 0;
2084 const char *backing_file
= NULL
, *backing_format
= NULL
;
2085 bool lazy_refcounts
= s
->use_lazy_refcounts
;
2089 for (i
= 0; options
[i
].name
; i
++)
2091 if (!options
[i
].assigned
) {
2092 /* only change explicitly defined options */
2096 if (!strcmp(options
[i
].name
, "compat")) {
2097 if (!options
[i
].value
.s
) {
2098 /* preserve default */
2099 } else if (!strcmp(options
[i
].value
.s
, "0.10")) {
2101 } else if (!strcmp(options
[i
].value
.s
, "1.1")) {
2104 fprintf(stderr
, "Unknown compatibility level %s.\n",
2105 options
[i
].value
.s
);
2108 } else if (!strcmp(options
[i
].name
, "preallocation")) {
2109 fprintf(stderr
, "Cannot change preallocation mode.\n");
2111 } else if (!strcmp(options
[i
].name
, "size")) {
2112 new_size
= options
[i
].value
.n
;
2113 } else if (!strcmp(options
[i
].name
, "backing_file")) {
2114 backing_file
= options
[i
].value
.s
;
2115 } else if (!strcmp(options
[i
].name
, "backing_fmt")) {
2116 backing_format
= options
[i
].value
.s
;
2117 } else if (!strcmp(options
[i
].name
, "encryption")) {
2118 if ((options
[i
].value
.n
!= !!s
->crypt_method
)) {
2119 fprintf(stderr
, "Changing the encryption flag is not "
2123 } else if (!strcmp(options
[i
].name
, "cluster_size")) {
2124 if (options
[i
].value
.n
!= s
->cluster_size
) {
2125 fprintf(stderr
, "Changing the cluster size is not "
2129 } else if (!strcmp(options
[i
].name
, "lazy_refcounts")) {
2130 lazy_refcounts
= options
[i
].value
.n
;
2132 /* if this assertion fails, this probably means a new option was
2133 * added without having it covered here */
2138 if (new_version
!= old_version
) {
2139 if (new_version
> old_version
) {
2141 s
->qcow_version
= new_version
;
2142 ret
= qcow2_update_header(bs
);
2144 s
->qcow_version
= old_version
;
2148 ret
= qcow2_downgrade(bs
, new_version
);
2155 if (backing_file
|| backing_format
) {
2156 ret
= qcow2_change_backing_file(bs
, backing_file
?: bs
->backing_file
,
2157 backing_format
?: bs
->backing_format
);
2163 if (s
->use_lazy_refcounts
!= lazy_refcounts
) {
2164 if (lazy_refcounts
) {
2165 if (s
->qcow_version
< 3) {
2166 fprintf(stderr
, "Lazy refcounts only supported with compatibility "
2167 "level 1.1 and above (use compat=1.1 or greater)\n");
2170 s
->compatible_features
|= QCOW2_COMPAT_LAZY_REFCOUNTS
;
2171 ret
= qcow2_update_header(bs
);
2173 s
->compatible_features
&= ~QCOW2_COMPAT_LAZY_REFCOUNTS
;
2176 s
->use_lazy_refcounts
= true;
2178 /* make image clean first */
2179 ret
= qcow2_mark_clean(bs
);
2183 /* now disallow lazy refcounts */
2184 s
->compatible_features
&= ~QCOW2_COMPAT_LAZY_REFCOUNTS
;
2185 ret
= qcow2_update_header(bs
);
2187 s
->compatible_features
|= QCOW2_COMPAT_LAZY_REFCOUNTS
;
2190 s
->use_lazy_refcounts
= false;
2195 ret
= bdrv_truncate(bs
, new_size
);
2204 static QEMUOptionParameter qcow2_create_options
[] = {
2206 .name
= BLOCK_OPT_SIZE
,
2208 .help
= "Virtual disk size"
2211 .name
= BLOCK_OPT_COMPAT_LEVEL
,
2213 .help
= "Compatibility level (0.10 or 1.1)"
2216 .name
= BLOCK_OPT_BACKING_FILE
,
2218 .help
= "File name of a base image"
2221 .name
= BLOCK_OPT_BACKING_FMT
,
2223 .help
= "Image format of the base image"
2226 .name
= BLOCK_OPT_ENCRYPT
,
2228 .help
= "Encrypt the image"
2231 .name
= BLOCK_OPT_CLUSTER_SIZE
,
2233 .help
= "qcow2 cluster size",
2234 .value
= { .n
= DEFAULT_CLUSTER_SIZE
},
2237 .name
= BLOCK_OPT_PREALLOC
,
2239 .help
= "Preallocation mode (allowed values: off, metadata)"
2242 .name
= BLOCK_OPT_LAZY_REFCOUNTS
,
2244 .help
= "Postpone refcount updates",
2249 static BlockDriver bdrv_qcow2
= {
2250 .format_name
= "qcow2",
2251 .instance_size
= sizeof(BDRVQcowState
),
2252 .bdrv_probe
= qcow2_probe
,
2253 .bdrv_open
= qcow2_open
,
2254 .bdrv_close
= qcow2_close
,
2255 .bdrv_reopen_prepare
= qcow2_reopen_prepare
,
2256 .bdrv_create
= qcow2_create
,
2257 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
2258 .bdrv_co_get_block_status
= qcow2_co_get_block_status
,
2259 .bdrv_set_key
= qcow2_set_key
,
2261 .bdrv_co_readv
= qcow2_co_readv
,
2262 .bdrv_co_writev
= qcow2_co_writev
,
2263 .bdrv_co_flush_to_os
= qcow2_co_flush_to_os
,
2265 .bdrv_co_write_zeroes
= qcow2_co_write_zeroes
,
2266 .bdrv_co_discard
= qcow2_co_discard
,
2267 .bdrv_truncate
= qcow2_truncate
,
2268 .bdrv_write_compressed
= qcow2_write_compressed
,
2270 .bdrv_snapshot_create
= qcow2_snapshot_create
,
2271 .bdrv_snapshot_goto
= qcow2_snapshot_goto
,
2272 .bdrv_snapshot_delete
= qcow2_snapshot_delete
,
2273 .bdrv_snapshot_list
= qcow2_snapshot_list
,
2274 .bdrv_snapshot_load_tmp
= qcow2_snapshot_load_tmp
,
2275 .bdrv_get_info
= qcow2_get_info
,
2276 .bdrv_get_specific_info
= qcow2_get_specific_info
,
2278 .bdrv_save_vmstate
= qcow2_save_vmstate
,
2279 .bdrv_load_vmstate
= qcow2_load_vmstate
,
2281 .bdrv_change_backing_file
= qcow2_change_backing_file
,
2283 .bdrv_refresh_limits
= qcow2_refresh_limits
,
2284 .bdrv_invalidate_cache
= qcow2_invalidate_cache
,
2286 .create_options
= qcow2_create_options
,
2287 .bdrv_check
= qcow2_check
,
2288 .bdrv_amend_options
= qcow2_amend_options
,
2291 static void bdrv_qcow2_init(void)
2293 bdrv_register(&bdrv_qcow2
);
2296 block_init(bdrv_qcow2_init
);