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
25 #include "qemu/osdep.h"
27 #include "block/qdict.h"
28 #include "sysemu/block-backend.h"
29 #include "qemu/main-loop.h"
30 #include "qemu/module.h"
32 #include "qemu/error-report.h"
33 #include "qapi/error.h"
34 #include "qapi/qapi-events-block-core.h"
35 #include "qapi/qmp/qdict.h"
36 #include "qapi/qmp/qstring.h"
38 #include "qemu/option_int.h"
39 #include "qemu/cutils.h"
40 #include "qemu/bswap.h"
41 #include "qapi/qobject-input-visitor.h"
42 #include "qapi/qapi-visit-block-core.h"
46 Differences with QCOW:
48 - Support for multiple incremental snapshots.
49 - Memory management by reference counts.
50 - Clusters which have a reference count of one have the bit
51 QCOW_OFLAG_COPIED to optimize write performance.
52 - Size of compressed clusters is stored in sectors to reduce bit usage
53 in the cluster offsets.
54 - Support for storing additional data (such as the VM state) in the
56 - If a backing store is used, the cluster size is not constrained
57 (could be backported to QCOW).
58 - L2 tables have always a size of one cluster.
65 } QEMU_PACKED QCowExtension
;
67 #define QCOW2_EXT_MAGIC_END 0
68 #define QCOW2_EXT_MAGIC_BACKING_FORMAT 0xE2792ACA
69 #define QCOW2_EXT_MAGIC_FEATURE_TABLE 0x6803f857
70 #define QCOW2_EXT_MAGIC_CRYPTO_HEADER 0x0537be77
71 #define QCOW2_EXT_MAGIC_BITMAPS 0x23852875
72 #define QCOW2_EXT_MAGIC_DATA_FILE 0x44415441
74 static int coroutine_fn
75 qcow2_co_preadv_compressed(BlockDriverState
*bs
,
76 uint64_t file_cluster_offset
,
81 static int qcow2_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
83 const QCowHeader
*cow_header
= (const void *)buf
;
85 if (buf_size
>= sizeof(QCowHeader
) &&
86 be32_to_cpu(cow_header
->magic
) == QCOW_MAGIC
&&
87 be32_to_cpu(cow_header
->version
) >= 2)
94 static ssize_t
qcow2_crypto_hdr_read_func(QCryptoBlock
*block
, size_t offset
,
95 uint8_t *buf
, size_t buflen
,
96 void *opaque
, Error
**errp
)
98 BlockDriverState
*bs
= opaque
;
99 BDRVQcow2State
*s
= bs
->opaque
;
102 if ((offset
+ buflen
) > s
->crypto_header
.length
) {
103 error_setg(errp
, "Request for data outside of extension header");
107 ret
= bdrv_pread(bs
->file
,
108 s
->crypto_header
.offset
+ offset
, buf
, buflen
);
110 error_setg_errno(errp
, -ret
, "Could not read encryption header");
117 static ssize_t
qcow2_crypto_hdr_init_func(QCryptoBlock
*block
, size_t headerlen
,
118 void *opaque
, Error
**errp
)
120 BlockDriverState
*bs
= opaque
;
121 BDRVQcow2State
*s
= bs
->opaque
;
125 ret
= qcow2_alloc_clusters(bs
, headerlen
);
127 error_setg_errno(errp
, -ret
,
128 "Cannot allocate cluster for LUKS header size %zu",
133 s
->crypto_header
.length
= headerlen
;
134 s
->crypto_header
.offset
= ret
;
136 /* Zero fill remaining space in cluster so it has predictable
137 * content in case of future spec changes */
138 clusterlen
= size_to_clusters(s
, headerlen
) * s
->cluster_size
;
139 assert(qcow2_pre_write_overlap_check(bs
, 0, ret
, clusterlen
, false) == 0);
140 ret
= bdrv_pwrite_zeroes(bs
->file
,
142 clusterlen
- headerlen
, 0);
144 error_setg_errno(errp
, -ret
, "Could not zero fill encryption header");
152 static ssize_t
qcow2_crypto_hdr_write_func(QCryptoBlock
*block
, size_t offset
,
153 const uint8_t *buf
, size_t buflen
,
154 void *opaque
, Error
**errp
)
156 BlockDriverState
*bs
= opaque
;
157 BDRVQcow2State
*s
= bs
->opaque
;
160 if ((offset
+ buflen
) > s
->crypto_header
.length
) {
161 error_setg(errp
, "Request for data outside of extension header");
165 ret
= bdrv_pwrite(bs
->file
,
166 s
->crypto_header
.offset
+ offset
, buf
, buflen
);
168 error_setg_errno(errp
, -ret
, "Could not read encryption header");
176 * read qcow2 extension and fill bs
177 * start reading from start_offset
178 * finish reading upon magic of value 0 or when end_offset reached
179 * unknown magic is skipped (future extension this version knows nothing about)
180 * return 0 upon success, non-0 otherwise
182 static int qcow2_read_extensions(BlockDriverState
*bs
, uint64_t start_offset
,
183 uint64_t end_offset
, void **p_feature_table
,
184 int flags
, bool *need_update_header
,
187 BDRVQcow2State
*s
= bs
->opaque
;
191 Qcow2BitmapHeaderExt bitmaps_ext
;
193 if (need_update_header
!= NULL
) {
194 *need_update_header
= false;
198 printf("qcow2_read_extensions: start=%ld end=%ld\n", start_offset
, end_offset
);
200 offset
= start_offset
;
201 while (offset
< end_offset
) {
205 if (offset
> s
->cluster_size
)
206 printf("qcow2_read_extension: suspicious offset %lu\n", offset
);
208 printf("attempting to read extended header in offset %lu\n", offset
);
211 ret
= bdrv_pread(bs
->file
, offset
, &ext
, sizeof(ext
));
213 error_setg_errno(errp
, -ret
, "qcow2_read_extension: ERROR: "
214 "pread fail from offset %" PRIu64
, offset
);
217 ext
.magic
= be32_to_cpu(ext
.magic
);
218 ext
.len
= be32_to_cpu(ext
.len
);
219 offset
+= sizeof(ext
);
221 printf("ext.magic = 0x%x\n", ext
.magic
);
223 if (offset
> end_offset
|| ext
.len
> end_offset
- offset
) {
224 error_setg(errp
, "Header extension too large");
229 case QCOW2_EXT_MAGIC_END
:
232 case QCOW2_EXT_MAGIC_BACKING_FORMAT
:
233 if (ext
.len
>= sizeof(bs
->backing_format
)) {
234 error_setg(errp
, "ERROR: ext_backing_format: len=%" PRIu32
235 " too large (>=%zu)", ext
.len
,
236 sizeof(bs
->backing_format
));
239 ret
= bdrv_pread(bs
->file
, offset
, bs
->backing_format
, ext
.len
);
241 error_setg_errno(errp
, -ret
, "ERROR: ext_backing_format: "
242 "Could not read format name");
245 bs
->backing_format
[ext
.len
] = '\0';
246 s
->image_backing_format
= g_strdup(bs
->backing_format
);
248 printf("Qcow2: Got format extension %s\n", bs
->backing_format
);
252 case QCOW2_EXT_MAGIC_FEATURE_TABLE
:
253 if (p_feature_table
!= NULL
) {
254 void* feature_table
= g_malloc0(ext
.len
+ 2 * sizeof(Qcow2Feature
));
255 ret
= bdrv_pread(bs
->file
, offset
, feature_table
, ext
.len
);
257 error_setg_errno(errp
, -ret
, "ERROR: ext_feature_table: "
258 "Could not read table");
262 *p_feature_table
= feature_table
;
266 case QCOW2_EXT_MAGIC_CRYPTO_HEADER
: {
267 unsigned int cflags
= 0;
268 if (s
->crypt_method_header
!= QCOW_CRYPT_LUKS
) {
269 error_setg(errp
, "CRYPTO header extension only "
270 "expected with LUKS encryption method");
273 if (ext
.len
!= sizeof(Qcow2CryptoHeaderExtension
)) {
274 error_setg(errp
, "CRYPTO header extension size %u, "
275 "but expected size %zu", ext
.len
,
276 sizeof(Qcow2CryptoHeaderExtension
));
280 ret
= bdrv_pread(bs
->file
, offset
, &s
->crypto_header
, ext
.len
);
282 error_setg_errno(errp
, -ret
,
283 "Unable to read CRYPTO header extension");
286 s
->crypto_header
.offset
= be64_to_cpu(s
->crypto_header
.offset
);
287 s
->crypto_header
.length
= be64_to_cpu(s
->crypto_header
.length
);
289 if ((s
->crypto_header
.offset
% s
->cluster_size
) != 0) {
290 error_setg(errp
, "Encryption header offset '%" PRIu64
"' is "
291 "not a multiple of cluster size '%u'",
292 s
->crypto_header
.offset
, s
->cluster_size
);
296 if (flags
& BDRV_O_NO_IO
) {
297 cflags
|= QCRYPTO_BLOCK_OPEN_NO_IO
;
299 s
->crypto
= qcrypto_block_open(s
->crypto_opts
, "encrypt.",
300 qcow2_crypto_hdr_read_func
,
301 bs
, cflags
, QCOW2_MAX_THREADS
, errp
);
307 case QCOW2_EXT_MAGIC_BITMAPS
:
308 if (ext
.len
!= sizeof(bitmaps_ext
)) {
309 error_setg_errno(errp
, -ret
, "bitmaps_ext: "
310 "Invalid extension length");
314 if (!(s
->autoclear_features
& QCOW2_AUTOCLEAR_BITMAPS
)) {
315 if (s
->qcow_version
< 3) {
316 /* Let's be a bit more specific */
317 warn_report("This qcow2 v2 image contains bitmaps, but "
318 "they may have been modified by a program "
319 "without persistent bitmap support; so now "
320 "they must all be considered inconsistent");
322 warn_report("a program lacking bitmap support "
323 "modified this file, so all bitmaps are now "
324 "considered inconsistent");
326 error_printf("Some clusters may be leaked, "
327 "run 'qemu-img check -r' on the image "
329 if (need_update_header
!= NULL
) {
330 /* Updating is needed to drop invalid bitmap extension. */
331 *need_update_header
= true;
336 ret
= bdrv_pread(bs
->file
, offset
, &bitmaps_ext
, ext
.len
);
338 error_setg_errno(errp
, -ret
, "bitmaps_ext: "
339 "Could not read ext header");
343 if (bitmaps_ext
.reserved32
!= 0) {
344 error_setg_errno(errp
, -ret
, "bitmaps_ext: "
345 "Reserved field is not zero");
349 bitmaps_ext
.nb_bitmaps
= be32_to_cpu(bitmaps_ext
.nb_bitmaps
);
350 bitmaps_ext
.bitmap_directory_size
=
351 be64_to_cpu(bitmaps_ext
.bitmap_directory_size
);
352 bitmaps_ext
.bitmap_directory_offset
=
353 be64_to_cpu(bitmaps_ext
.bitmap_directory_offset
);
355 if (bitmaps_ext
.nb_bitmaps
> QCOW2_MAX_BITMAPS
) {
357 "bitmaps_ext: Image has %" PRIu32
" bitmaps, "
358 "exceeding the QEMU supported maximum of %d",
359 bitmaps_ext
.nb_bitmaps
, QCOW2_MAX_BITMAPS
);
363 if (bitmaps_ext
.nb_bitmaps
== 0) {
364 error_setg(errp
, "found bitmaps extension with zero bitmaps");
368 if (bitmaps_ext
.bitmap_directory_offset
& (s
->cluster_size
- 1)) {
369 error_setg(errp
, "bitmaps_ext: "
370 "invalid bitmap directory offset");
374 if (bitmaps_ext
.bitmap_directory_size
>
375 QCOW2_MAX_BITMAP_DIRECTORY_SIZE
) {
376 error_setg(errp
, "bitmaps_ext: "
377 "bitmap directory size (%" PRIu64
") exceeds "
378 "the maximum supported size (%d)",
379 bitmaps_ext
.bitmap_directory_size
,
380 QCOW2_MAX_BITMAP_DIRECTORY_SIZE
);
384 s
->nb_bitmaps
= bitmaps_ext
.nb_bitmaps
;
385 s
->bitmap_directory_offset
=
386 bitmaps_ext
.bitmap_directory_offset
;
387 s
->bitmap_directory_size
=
388 bitmaps_ext
.bitmap_directory_size
;
391 printf("Qcow2: Got bitmaps extension: "
392 "offset=%" PRIu64
" nb_bitmaps=%" PRIu32
"\n",
393 s
->bitmap_directory_offset
, s
->nb_bitmaps
);
397 case QCOW2_EXT_MAGIC_DATA_FILE
:
399 s
->image_data_file
= g_malloc0(ext
.len
+ 1);
400 ret
= bdrv_pread(bs
->file
, offset
, s
->image_data_file
, ext
.len
);
402 error_setg_errno(errp
, -ret
,
403 "ERROR: Could not read data file name");
407 printf("Qcow2: Got external data file %s\n", s
->image_data_file
);
413 /* unknown magic - save it in case we need to rewrite the header */
414 /* If you add a new feature, make sure to also update the fast
415 * path of qcow2_make_empty() to deal with it. */
417 Qcow2UnknownHeaderExtension
*uext
;
419 uext
= g_malloc0(sizeof(*uext
) + ext
.len
);
420 uext
->magic
= ext
.magic
;
422 QLIST_INSERT_HEAD(&s
->unknown_header_ext
, uext
, next
);
424 ret
= bdrv_pread(bs
->file
, offset
, uext
->data
, uext
->len
);
426 error_setg_errno(errp
, -ret
, "ERROR: unknown extension: "
427 "Could not read data");
434 offset
+= ((ext
.len
+ 7) & ~7);
440 static void cleanup_unknown_header_ext(BlockDriverState
*bs
)
442 BDRVQcow2State
*s
= bs
->opaque
;
443 Qcow2UnknownHeaderExtension
*uext
, *next
;
445 QLIST_FOREACH_SAFE(uext
, &s
->unknown_header_ext
, next
, next
) {
446 QLIST_REMOVE(uext
, next
);
451 static void report_unsupported_feature(Error
**errp
, Qcow2Feature
*table
,
454 char *features
= g_strdup("");
457 while (table
&& table
->name
[0] != '\0') {
458 if (table
->type
== QCOW2_FEAT_TYPE_INCOMPATIBLE
) {
459 if (mask
& (1ULL << table
->bit
)) {
461 features
= g_strdup_printf("%s%s%.46s", old
, *old
? ", " : "",
464 mask
&= ~(1ULL << table
->bit
);
472 features
= g_strdup_printf("%s%sUnknown incompatible feature: %" PRIx64
,
473 old
, *old
? ", " : "", mask
);
477 error_setg(errp
, "Unsupported qcow2 feature(s): %s", features
);
482 * Sets the dirty bit and flushes afterwards if necessary.
484 * The incompatible_features bit is only set if the image file header was
485 * updated successfully. Therefore it is not required to check the return
486 * value of this function.
488 int qcow2_mark_dirty(BlockDriverState
*bs
)
490 BDRVQcow2State
*s
= bs
->opaque
;
494 assert(s
->qcow_version
>= 3);
496 if (s
->incompatible_features
& QCOW2_INCOMPAT_DIRTY
) {
497 return 0; /* already dirty */
500 val
= cpu_to_be64(s
->incompatible_features
| QCOW2_INCOMPAT_DIRTY
);
501 ret
= bdrv_pwrite(bs
->file
, offsetof(QCowHeader
, incompatible_features
),
506 ret
= bdrv_flush(bs
->file
->bs
);
511 /* Only treat image as dirty if the header was updated successfully */
512 s
->incompatible_features
|= QCOW2_INCOMPAT_DIRTY
;
517 * Clears the dirty bit and flushes before if necessary. Only call this
518 * function when there are no pending requests, it does not guard against
519 * concurrent requests dirtying the image.
521 static int qcow2_mark_clean(BlockDriverState
*bs
)
523 BDRVQcow2State
*s
= bs
->opaque
;
525 if (s
->incompatible_features
& QCOW2_INCOMPAT_DIRTY
) {
528 s
->incompatible_features
&= ~QCOW2_INCOMPAT_DIRTY
;
530 ret
= qcow2_flush_caches(bs
);
535 return qcow2_update_header(bs
);
541 * Marks the image as corrupt.
543 int qcow2_mark_corrupt(BlockDriverState
*bs
)
545 BDRVQcow2State
*s
= bs
->opaque
;
547 s
->incompatible_features
|= QCOW2_INCOMPAT_CORRUPT
;
548 return qcow2_update_header(bs
);
552 * Marks the image as consistent, i.e., unsets the corrupt bit, and flushes
553 * before if necessary.
555 int qcow2_mark_consistent(BlockDriverState
*bs
)
557 BDRVQcow2State
*s
= bs
->opaque
;
559 if (s
->incompatible_features
& QCOW2_INCOMPAT_CORRUPT
) {
560 int ret
= qcow2_flush_caches(bs
);
565 s
->incompatible_features
&= ~QCOW2_INCOMPAT_CORRUPT
;
566 return qcow2_update_header(bs
);
571 static int coroutine_fn
qcow2_co_check_locked(BlockDriverState
*bs
,
572 BdrvCheckResult
*result
,
575 int ret
= qcow2_check_refcounts(bs
, result
, fix
);
580 if (fix
&& result
->check_errors
== 0 && result
->corruptions
== 0) {
581 ret
= qcow2_mark_clean(bs
);
585 return qcow2_mark_consistent(bs
);
590 static int coroutine_fn
qcow2_co_check(BlockDriverState
*bs
,
591 BdrvCheckResult
*result
,
594 BDRVQcow2State
*s
= bs
->opaque
;
597 qemu_co_mutex_lock(&s
->lock
);
598 ret
= qcow2_co_check_locked(bs
, result
, fix
);
599 qemu_co_mutex_unlock(&s
->lock
);
603 int qcow2_validate_table(BlockDriverState
*bs
, uint64_t offset
,
604 uint64_t entries
, size_t entry_len
,
605 int64_t max_size_bytes
, const char *table_name
,
608 BDRVQcow2State
*s
= bs
->opaque
;
610 if (entries
> max_size_bytes
/ entry_len
) {
611 error_setg(errp
, "%s too large", table_name
);
615 /* Use signed INT64_MAX as the maximum even for uint64_t header fields,
616 * because values will be passed to qemu functions taking int64_t. */
617 if ((INT64_MAX
- entries
* entry_len
< offset
) ||
618 (offset_into_cluster(s
, offset
) != 0)) {
619 error_setg(errp
, "%s offset invalid", table_name
);
626 static const char *const mutable_opts
[] = {
627 QCOW2_OPT_LAZY_REFCOUNTS
,
628 QCOW2_OPT_DISCARD_REQUEST
,
629 QCOW2_OPT_DISCARD_SNAPSHOT
,
630 QCOW2_OPT_DISCARD_OTHER
,
632 QCOW2_OPT_OVERLAP_TEMPLATE
,
633 QCOW2_OPT_OVERLAP_MAIN_HEADER
,
634 QCOW2_OPT_OVERLAP_ACTIVE_L1
,
635 QCOW2_OPT_OVERLAP_ACTIVE_L2
,
636 QCOW2_OPT_OVERLAP_REFCOUNT_TABLE
,
637 QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK
,
638 QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE
,
639 QCOW2_OPT_OVERLAP_INACTIVE_L1
,
640 QCOW2_OPT_OVERLAP_INACTIVE_L2
,
641 QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY
,
642 QCOW2_OPT_CACHE_SIZE
,
643 QCOW2_OPT_L2_CACHE_SIZE
,
644 QCOW2_OPT_L2_CACHE_ENTRY_SIZE
,
645 QCOW2_OPT_REFCOUNT_CACHE_SIZE
,
646 QCOW2_OPT_CACHE_CLEAN_INTERVAL
,
650 static QemuOptsList qcow2_runtime_opts
= {
652 .head
= QTAILQ_HEAD_INITIALIZER(qcow2_runtime_opts
.head
),
655 .name
= QCOW2_OPT_LAZY_REFCOUNTS
,
656 .type
= QEMU_OPT_BOOL
,
657 .help
= "Postpone refcount updates",
660 .name
= QCOW2_OPT_DISCARD_REQUEST
,
661 .type
= QEMU_OPT_BOOL
,
662 .help
= "Pass guest discard requests to the layer below",
665 .name
= QCOW2_OPT_DISCARD_SNAPSHOT
,
666 .type
= QEMU_OPT_BOOL
,
667 .help
= "Generate discard requests when snapshot related space "
671 .name
= QCOW2_OPT_DISCARD_OTHER
,
672 .type
= QEMU_OPT_BOOL
,
673 .help
= "Generate discard requests when other clusters are freed",
676 .name
= QCOW2_OPT_OVERLAP
,
677 .type
= QEMU_OPT_STRING
,
678 .help
= "Selects which overlap checks to perform from a range of "
679 "templates (none, constant, cached, all)",
682 .name
= QCOW2_OPT_OVERLAP_TEMPLATE
,
683 .type
= QEMU_OPT_STRING
,
684 .help
= "Selects which overlap checks to perform from a range of "
685 "templates (none, constant, cached, all)",
688 .name
= QCOW2_OPT_OVERLAP_MAIN_HEADER
,
689 .type
= QEMU_OPT_BOOL
,
690 .help
= "Check for unintended writes into the main qcow2 header",
693 .name
= QCOW2_OPT_OVERLAP_ACTIVE_L1
,
694 .type
= QEMU_OPT_BOOL
,
695 .help
= "Check for unintended writes into the active L1 table",
698 .name
= QCOW2_OPT_OVERLAP_ACTIVE_L2
,
699 .type
= QEMU_OPT_BOOL
,
700 .help
= "Check for unintended writes into an active L2 table",
703 .name
= QCOW2_OPT_OVERLAP_REFCOUNT_TABLE
,
704 .type
= QEMU_OPT_BOOL
,
705 .help
= "Check for unintended writes into the refcount table",
708 .name
= QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK
,
709 .type
= QEMU_OPT_BOOL
,
710 .help
= "Check for unintended writes into a refcount block",
713 .name
= QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE
,
714 .type
= QEMU_OPT_BOOL
,
715 .help
= "Check for unintended writes into the snapshot table",
718 .name
= QCOW2_OPT_OVERLAP_INACTIVE_L1
,
719 .type
= QEMU_OPT_BOOL
,
720 .help
= "Check for unintended writes into an inactive L1 table",
723 .name
= QCOW2_OPT_OVERLAP_INACTIVE_L2
,
724 .type
= QEMU_OPT_BOOL
,
725 .help
= "Check for unintended writes into an inactive L2 table",
728 .name
= QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY
,
729 .type
= QEMU_OPT_BOOL
,
730 .help
= "Check for unintended writes into the bitmap directory",
733 .name
= QCOW2_OPT_CACHE_SIZE
,
734 .type
= QEMU_OPT_SIZE
,
735 .help
= "Maximum combined metadata (L2 tables and refcount blocks) "
739 .name
= QCOW2_OPT_L2_CACHE_SIZE
,
740 .type
= QEMU_OPT_SIZE
,
741 .help
= "Maximum L2 table cache size",
744 .name
= QCOW2_OPT_L2_CACHE_ENTRY_SIZE
,
745 .type
= QEMU_OPT_SIZE
,
746 .help
= "Size of each entry in the L2 cache",
749 .name
= QCOW2_OPT_REFCOUNT_CACHE_SIZE
,
750 .type
= QEMU_OPT_SIZE
,
751 .help
= "Maximum refcount block cache size",
754 .name
= QCOW2_OPT_CACHE_CLEAN_INTERVAL
,
755 .type
= QEMU_OPT_NUMBER
,
756 .help
= "Clean unused cache entries after this time (in seconds)",
758 BLOCK_CRYPTO_OPT_DEF_KEY_SECRET("encrypt.",
759 "ID of secret providing qcow2 AES key or LUKS passphrase"),
760 { /* end of list */ }
764 static const char *overlap_bool_option_names
[QCOW2_OL_MAX_BITNR
] = {
765 [QCOW2_OL_MAIN_HEADER_BITNR
] = QCOW2_OPT_OVERLAP_MAIN_HEADER
,
766 [QCOW2_OL_ACTIVE_L1_BITNR
] = QCOW2_OPT_OVERLAP_ACTIVE_L1
,
767 [QCOW2_OL_ACTIVE_L2_BITNR
] = QCOW2_OPT_OVERLAP_ACTIVE_L2
,
768 [QCOW2_OL_REFCOUNT_TABLE_BITNR
] = QCOW2_OPT_OVERLAP_REFCOUNT_TABLE
,
769 [QCOW2_OL_REFCOUNT_BLOCK_BITNR
] = QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK
,
770 [QCOW2_OL_SNAPSHOT_TABLE_BITNR
] = QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE
,
771 [QCOW2_OL_INACTIVE_L1_BITNR
] = QCOW2_OPT_OVERLAP_INACTIVE_L1
,
772 [QCOW2_OL_INACTIVE_L2_BITNR
] = QCOW2_OPT_OVERLAP_INACTIVE_L2
,
773 [QCOW2_OL_BITMAP_DIRECTORY_BITNR
] = QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY
,
776 static void cache_clean_timer_cb(void *opaque
)
778 BlockDriverState
*bs
= opaque
;
779 BDRVQcow2State
*s
= bs
->opaque
;
780 qcow2_cache_clean_unused(s
->l2_table_cache
);
781 qcow2_cache_clean_unused(s
->refcount_block_cache
);
782 timer_mod(s
->cache_clean_timer
, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
) +
783 (int64_t) s
->cache_clean_interval
* 1000);
786 static void cache_clean_timer_init(BlockDriverState
*bs
, AioContext
*context
)
788 BDRVQcow2State
*s
= bs
->opaque
;
789 if (s
->cache_clean_interval
> 0) {
790 s
->cache_clean_timer
= aio_timer_new(context
, QEMU_CLOCK_VIRTUAL
,
791 SCALE_MS
, cache_clean_timer_cb
,
793 timer_mod(s
->cache_clean_timer
, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
) +
794 (int64_t) s
->cache_clean_interval
* 1000);
798 static void cache_clean_timer_del(BlockDriverState
*bs
)
800 BDRVQcow2State
*s
= bs
->opaque
;
801 if (s
->cache_clean_timer
) {
802 timer_del(s
->cache_clean_timer
);
803 timer_free(s
->cache_clean_timer
);
804 s
->cache_clean_timer
= NULL
;
808 static void qcow2_detach_aio_context(BlockDriverState
*bs
)
810 cache_clean_timer_del(bs
);
813 static void qcow2_attach_aio_context(BlockDriverState
*bs
,
814 AioContext
*new_context
)
816 cache_clean_timer_init(bs
, new_context
);
819 static void read_cache_sizes(BlockDriverState
*bs
, QemuOpts
*opts
,
820 uint64_t *l2_cache_size
,
821 uint64_t *l2_cache_entry_size
,
822 uint64_t *refcount_cache_size
, Error
**errp
)
824 BDRVQcow2State
*s
= bs
->opaque
;
825 uint64_t combined_cache_size
, l2_cache_max_setting
;
826 bool l2_cache_size_set
, refcount_cache_size_set
, combined_cache_size_set
;
827 bool l2_cache_entry_size_set
;
828 int min_refcount_cache
= MIN_REFCOUNT_CACHE_SIZE
* s
->cluster_size
;
829 uint64_t virtual_disk_size
= bs
->total_sectors
* BDRV_SECTOR_SIZE
;
830 uint64_t max_l2_cache
= virtual_disk_size
/ (s
->cluster_size
/ 8);
832 combined_cache_size_set
= qemu_opt_get(opts
, QCOW2_OPT_CACHE_SIZE
);
833 l2_cache_size_set
= qemu_opt_get(opts
, QCOW2_OPT_L2_CACHE_SIZE
);
834 refcount_cache_size_set
= qemu_opt_get(opts
, QCOW2_OPT_REFCOUNT_CACHE_SIZE
);
835 l2_cache_entry_size_set
= qemu_opt_get(opts
, QCOW2_OPT_L2_CACHE_ENTRY_SIZE
);
837 combined_cache_size
= qemu_opt_get_size(opts
, QCOW2_OPT_CACHE_SIZE
, 0);
838 l2_cache_max_setting
= qemu_opt_get_size(opts
, QCOW2_OPT_L2_CACHE_SIZE
,
839 DEFAULT_L2_CACHE_MAX_SIZE
);
840 *refcount_cache_size
= qemu_opt_get_size(opts
,
841 QCOW2_OPT_REFCOUNT_CACHE_SIZE
, 0);
843 *l2_cache_entry_size
= qemu_opt_get_size(
844 opts
, QCOW2_OPT_L2_CACHE_ENTRY_SIZE
, s
->cluster_size
);
846 *l2_cache_size
= MIN(max_l2_cache
, l2_cache_max_setting
);
848 if (combined_cache_size_set
) {
849 if (l2_cache_size_set
&& refcount_cache_size_set
) {
850 error_setg(errp
, QCOW2_OPT_CACHE_SIZE
", " QCOW2_OPT_L2_CACHE_SIZE
851 " and " QCOW2_OPT_REFCOUNT_CACHE_SIZE
" may not be set "
854 } else if (l2_cache_size_set
&&
855 (l2_cache_max_setting
> combined_cache_size
)) {
856 error_setg(errp
, QCOW2_OPT_L2_CACHE_SIZE
" may not exceed "
857 QCOW2_OPT_CACHE_SIZE
);
859 } else if (*refcount_cache_size
> combined_cache_size
) {
860 error_setg(errp
, QCOW2_OPT_REFCOUNT_CACHE_SIZE
" may not exceed "
861 QCOW2_OPT_CACHE_SIZE
);
865 if (l2_cache_size_set
) {
866 *refcount_cache_size
= combined_cache_size
- *l2_cache_size
;
867 } else if (refcount_cache_size_set
) {
868 *l2_cache_size
= combined_cache_size
- *refcount_cache_size
;
870 /* Assign as much memory as possible to the L2 cache, and
871 * use the remainder for the refcount cache */
872 if (combined_cache_size
>= max_l2_cache
+ min_refcount_cache
) {
873 *l2_cache_size
= max_l2_cache
;
874 *refcount_cache_size
= combined_cache_size
- *l2_cache_size
;
876 *refcount_cache_size
=
877 MIN(combined_cache_size
, min_refcount_cache
);
878 *l2_cache_size
= combined_cache_size
- *refcount_cache_size
;
884 * If the L2 cache is not enough to cover the whole disk then
885 * default to 4KB entries. Smaller entries reduce the cost of
886 * loads and evictions and increase I/O performance.
888 if (*l2_cache_size
< max_l2_cache
&& !l2_cache_entry_size_set
) {
889 *l2_cache_entry_size
= MIN(s
->cluster_size
, 4096);
892 /* l2_cache_size and refcount_cache_size are ensured to have at least
893 * their minimum values in qcow2_update_options_prepare() */
895 if (*l2_cache_entry_size
< (1 << MIN_CLUSTER_BITS
) ||
896 *l2_cache_entry_size
> s
->cluster_size
||
897 !is_power_of_2(*l2_cache_entry_size
)) {
898 error_setg(errp
, "L2 cache entry size must be a power of two "
899 "between %d and the cluster size (%d)",
900 1 << MIN_CLUSTER_BITS
, s
->cluster_size
);
905 typedef struct Qcow2ReopenState
{
906 Qcow2Cache
*l2_table_cache
;
907 Qcow2Cache
*refcount_block_cache
;
908 int l2_slice_size
; /* Number of entries in a slice of the L2 table */
909 bool use_lazy_refcounts
;
911 bool discard_passthrough
[QCOW2_DISCARD_MAX
];
912 uint64_t cache_clean_interval
;
913 QCryptoBlockOpenOptions
*crypto_opts
; /* Disk encryption runtime options */
916 static int qcow2_update_options_prepare(BlockDriverState
*bs
,
918 QDict
*options
, int flags
,
921 BDRVQcow2State
*s
= bs
->opaque
;
922 QemuOpts
*opts
= NULL
;
923 const char *opt_overlap_check
, *opt_overlap_check_template
;
924 int overlap_check_template
= 0;
925 uint64_t l2_cache_size
, l2_cache_entry_size
, refcount_cache_size
;
927 const char *encryptfmt
;
928 QDict
*encryptopts
= NULL
;
929 Error
*local_err
= NULL
;
932 qdict_extract_subqdict(options
, &encryptopts
, "encrypt.");
933 encryptfmt
= qdict_get_try_str(encryptopts
, "format");
935 opts
= qemu_opts_create(&qcow2_runtime_opts
, NULL
, 0, &error_abort
);
936 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
938 error_propagate(errp
, local_err
);
943 /* get L2 table/refcount block cache size from command line options */
944 read_cache_sizes(bs
, opts
, &l2_cache_size
, &l2_cache_entry_size
,
945 &refcount_cache_size
, &local_err
);
947 error_propagate(errp
, local_err
);
952 l2_cache_size
/= l2_cache_entry_size
;
953 if (l2_cache_size
< MIN_L2_CACHE_SIZE
) {
954 l2_cache_size
= MIN_L2_CACHE_SIZE
;
956 if (l2_cache_size
> INT_MAX
) {
957 error_setg(errp
, "L2 cache size too big");
962 refcount_cache_size
/= s
->cluster_size
;
963 if (refcount_cache_size
< MIN_REFCOUNT_CACHE_SIZE
) {
964 refcount_cache_size
= MIN_REFCOUNT_CACHE_SIZE
;
966 if (refcount_cache_size
> INT_MAX
) {
967 error_setg(errp
, "Refcount cache size too big");
972 /* alloc new L2 table/refcount block cache, flush old one */
973 if (s
->l2_table_cache
) {
974 ret
= qcow2_cache_flush(bs
, s
->l2_table_cache
);
976 error_setg_errno(errp
, -ret
, "Failed to flush the L2 table cache");
981 if (s
->refcount_block_cache
) {
982 ret
= qcow2_cache_flush(bs
, s
->refcount_block_cache
);
984 error_setg_errno(errp
, -ret
,
985 "Failed to flush the refcount block cache");
990 r
->l2_slice_size
= l2_cache_entry_size
/ sizeof(uint64_t);
991 r
->l2_table_cache
= qcow2_cache_create(bs
, l2_cache_size
,
992 l2_cache_entry_size
);
993 r
->refcount_block_cache
= qcow2_cache_create(bs
, refcount_cache_size
,
995 if (r
->l2_table_cache
== NULL
|| r
->refcount_block_cache
== NULL
) {
996 error_setg(errp
, "Could not allocate metadata caches");
1001 /* New interval for cache cleanup timer */
1002 r
->cache_clean_interval
=
1003 qemu_opt_get_number(opts
, QCOW2_OPT_CACHE_CLEAN_INTERVAL
,
1004 DEFAULT_CACHE_CLEAN_INTERVAL
);
1005 #ifndef CONFIG_LINUX
1006 if (r
->cache_clean_interval
!= 0) {
1007 error_setg(errp
, QCOW2_OPT_CACHE_CLEAN_INTERVAL
1008 " not supported on this host");
1013 if (r
->cache_clean_interval
> UINT_MAX
) {
1014 error_setg(errp
, "Cache clean interval too big");
1019 /* lazy-refcounts; flush if going from enabled to disabled */
1020 r
->use_lazy_refcounts
= qemu_opt_get_bool(opts
, QCOW2_OPT_LAZY_REFCOUNTS
,
1021 (s
->compatible_features
& QCOW2_COMPAT_LAZY_REFCOUNTS
));
1022 if (r
->use_lazy_refcounts
&& s
->qcow_version
< 3) {
1023 error_setg(errp
, "Lazy refcounts require a qcow2 image with at least "
1024 "qemu 1.1 compatibility level");
1029 if (s
->use_lazy_refcounts
&& !r
->use_lazy_refcounts
) {
1030 ret
= qcow2_mark_clean(bs
);
1032 error_setg_errno(errp
, -ret
, "Failed to disable lazy refcounts");
1037 /* Overlap check options */
1038 opt_overlap_check
= qemu_opt_get(opts
, QCOW2_OPT_OVERLAP
);
1039 opt_overlap_check_template
= qemu_opt_get(opts
, QCOW2_OPT_OVERLAP_TEMPLATE
);
1040 if (opt_overlap_check_template
&& opt_overlap_check
&&
1041 strcmp(opt_overlap_check_template
, opt_overlap_check
))
1043 error_setg(errp
, "Conflicting values for qcow2 options '"
1044 QCOW2_OPT_OVERLAP
"' ('%s') and '" QCOW2_OPT_OVERLAP_TEMPLATE
1045 "' ('%s')", opt_overlap_check
, opt_overlap_check_template
);
1049 if (!opt_overlap_check
) {
1050 opt_overlap_check
= opt_overlap_check_template
?: "cached";
1053 if (!strcmp(opt_overlap_check
, "none")) {
1054 overlap_check_template
= 0;
1055 } else if (!strcmp(opt_overlap_check
, "constant")) {
1056 overlap_check_template
= QCOW2_OL_CONSTANT
;
1057 } else if (!strcmp(opt_overlap_check
, "cached")) {
1058 overlap_check_template
= QCOW2_OL_CACHED
;
1059 } else if (!strcmp(opt_overlap_check
, "all")) {
1060 overlap_check_template
= QCOW2_OL_ALL
;
1062 error_setg(errp
, "Unsupported value '%s' for qcow2 option "
1063 "'overlap-check'. Allowed are any of the following: "
1064 "none, constant, cached, all", opt_overlap_check
);
1069 r
->overlap_check
= 0;
1070 for (i
= 0; i
< QCOW2_OL_MAX_BITNR
; i
++) {
1071 /* overlap-check defines a template bitmask, but every flag may be
1072 * overwritten through the associated boolean option */
1074 qemu_opt_get_bool(opts
, overlap_bool_option_names
[i
],
1075 overlap_check_template
& (1 << i
)) << i
;
1078 r
->discard_passthrough
[QCOW2_DISCARD_NEVER
] = false;
1079 r
->discard_passthrough
[QCOW2_DISCARD_ALWAYS
] = true;
1080 r
->discard_passthrough
[QCOW2_DISCARD_REQUEST
] =
1081 qemu_opt_get_bool(opts
, QCOW2_OPT_DISCARD_REQUEST
,
1082 flags
& BDRV_O_UNMAP
);
1083 r
->discard_passthrough
[QCOW2_DISCARD_SNAPSHOT
] =
1084 qemu_opt_get_bool(opts
, QCOW2_OPT_DISCARD_SNAPSHOT
, true);
1085 r
->discard_passthrough
[QCOW2_DISCARD_OTHER
] =
1086 qemu_opt_get_bool(opts
, QCOW2_OPT_DISCARD_OTHER
, false);
1088 switch (s
->crypt_method_header
) {
1089 case QCOW_CRYPT_NONE
:
1091 error_setg(errp
, "No encryption in image header, but options "
1092 "specified format '%s'", encryptfmt
);
1098 case QCOW_CRYPT_AES
:
1099 if (encryptfmt
&& !g_str_equal(encryptfmt
, "aes")) {
1101 "Header reported 'aes' encryption format but "
1102 "options specify '%s'", encryptfmt
);
1106 qdict_put_str(encryptopts
, "format", "qcow");
1107 r
->crypto_opts
= block_crypto_open_opts_init(encryptopts
, errp
);
1110 case QCOW_CRYPT_LUKS
:
1111 if (encryptfmt
&& !g_str_equal(encryptfmt
, "luks")) {
1113 "Header reported 'luks' encryption format but "
1114 "options specify '%s'", encryptfmt
);
1118 qdict_put_str(encryptopts
, "format", "luks");
1119 r
->crypto_opts
= block_crypto_open_opts_init(encryptopts
, errp
);
1123 error_setg(errp
, "Unsupported encryption method %d",
1124 s
->crypt_method_header
);
1127 if (s
->crypt_method_header
!= QCOW_CRYPT_NONE
&& !r
->crypto_opts
) {
1134 qobject_unref(encryptopts
);
1135 qemu_opts_del(opts
);
1140 static void qcow2_update_options_commit(BlockDriverState
*bs
,
1141 Qcow2ReopenState
*r
)
1143 BDRVQcow2State
*s
= bs
->opaque
;
1146 if (s
->l2_table_cache
) {
1147 qcow2_cache_destroy(s
->l2_table_cache
);
1149 if (s
->refcount_block_cache
) {
1150 qcow2_cache_destroy(s
->refcount_block_cache
);
1152 s
->l2_table_cache
= r
->l2_table_cache
;
1153 s
->refcount_block_cache
= r
->refcount_block_cache
;
1154 s
->l2_slice_size
= r
->l2_slice_size
;
1156 s
->overlap_check
= r
->overlap_check
;
1157 s
->use_lazy_refcounts
= r
->use_lazy_refcounts
;
1159 for (i
= 0; i
< QCOW2_DISCARD_MAX
; i
++) {
1160 s
->discard_passthrough
[i
] = r
->discard_passthrough
[i
];
1163 if (s
->cache_clean_interval
!= r
->cache_clean_interval
) {
1164 cache_clean_timer_del(bs
);
1165 s
->cache_clean_interval
= r
->cache_clean_interval
;
1166 cache_clean_timer_init(bs
, bdrv_get_aio_context(bs
));
1169 qapi_free_QCryptoBlockOpenOptions(s
->crypto_opts
);
1170 s
->crypto_opts
= r
->crypto_opts
;
1173 static void qcow2_update_options_abort(BlockDriverState
*bs
,
1174 Qcow2ReopenState
*r
)
1176 if (r
->l2_table_cache
) {
1177 qcow2_cache_destroy(r
->l2_table_cache
);
1179 if (r
->refcount_block_cache
) {
1180 qcow2_cache_destroy(r
->refcount_block_cache
);
1182 qapi_free_QCryptoBlockOpenOptions(r
->crypto_opts
);
1185 static int qcow2_update_options(BlockDriverState
*bs
, QDict
*options
,
1186 int flags
, Error
**errp
)
1188 Qcow2ReopenState r
= {};
1191 ret
= qcow2_update_options_prepare(bs
, &r
, options
, flags
, errp
);
1193 qcow2_update_options_commit(bs
, &r
);
1195 qcow2_update_options_abort(bs
, &r
);
1201 /* Called with s->lock held. */
1202 static int coroutine_fn
qcow2_do_open(BlockDriverState
*bs
, QDict
*options
,
1203 int flags
, Error
**errp
)
1205 BDRVQcow2State
*s
= bs
->opaque
;
1206 unsigned int len
, i
;
1209 Error
*local_err
= NULL
;
1211 uint64_t l1_vm_state_index
;
1212 bool update_header
= false;
1214 ret
= bdrv_pread(bs
->file
, 0, &header
, sizeof(header
));
1216 error_setg_errno(errp
, -ret
, "Could not read qcow2 header");
1219 header
.magic
= be32_to_cpu(header
.magic
);
1220 header
.version
= be32_to_cpu(header
.version
);
1221 header
.backing_file_offset
= be64_to_cpu(header
.backing_file_offset
);
1222 header
.backing_file_size
= be32_to_cpu(header
.backing_file_size
);
1223 header
.size
= be64_to_cpu(header
.size
);
1224 header
.cluster_bits
= be32_to_cpu(header
.cluster_bits
);
1225 header
.crypt_method
= be32_to_cpu(header
.crypt_method
);
1226 header
.l1_table_offset
= be64_to_cpu(header
.l1_table_offset
);
1227 header
.l1_size
= be32_to_cpu(header
.l1_size
);
1228 header
.refcount_table_offset
= be64_to_cpu(header
.refcount_table_offset
);
1229 header
.refcount_table_clusters
=
1230 be32_to_cpu(header
.refcount_table_clusters
);
1231 header
.snapshots_offset
= be64_to_cpu(header
.snapshots_offset
);
1232 header
.nb_snapshots
= be32_to_cpu(header
.nb_snapshots
);
1234 if (header
.magic
!= QCOW_MAGIC
) {
1235 error_setg(errp
, "Image is not in qcow2 format");
1239 if (header
.version
< 2 || header
.version
> 3) {
1240 error_setg(errp
, "Unsupported qcow2 version %" PRIu32
, header
.version
);
1245 s
->qcow_version
= header
.version
;
1247 /* Initialise cluster size */
1248 if (header
.cluster_bits
< MIN_CLUSTER_BITS
||
1249 header
.cluster_bits
> MAX_CLUSTER_BITS
) {
1250 error_setg(errp
, "Unsupported cluster size: 2^%" PRIu32
,
1251 header
.cluster_bits
);
1256 s
->cluster_bits
= header
.cluster_bits
;
1257 s
->cluster_size
= 1 << s
->cluster_bits
;
1259 /* Initialise version 3 header fields */
1260 if (header
.version
== 2) {
1261 header
.incompatible_features
= 0;
1262 header
.compatible_features
= 0;
1263 header
.autoclear_features
= 0;
1264 header
.refcount_order
= 4;
1265 header
.header_length
= 72;
1267 header
.incompatible_features
=
1268 be64_to_cpu(header
.incompatible_features
);
1269 header
.compatible_features
= be64_to_cpu(header
.compatible_features
);
1270 header
.autoclear_features
= be64_to_cpu(header
.autoclear_features
);
1271 header
.refcount_order
= be32_to_cpu(header
.refcount_order
);
1272 header
.header_length
= be32_to_cpu(header
.header_length
);
1274 if (header
.header_length
< 104) {
1275 error_setg(errp
, "qcow2 header too short");
1281 if (header
.header_length
> s
->cluster_size
) {
1282 error_setg(errp
, "qcow2 header exceeds cluster size");
1287 if (header
.header_length
> sizeof(header
)) {
1288 s
->unknown_header_fields_size
= header
.header_length
- sizeof(header
);
1289 s
->unknown_header_fields
= g_malloc(s
->unknown_header_fields_size
);
1290 ret
= bdrv_pread(bs
->file
, sizeof(header
), s
->unknown_header_fields
,
1291 s
->unknown_header_fields_size
);
1293 error_setg_errno(errp
, -ret
, "Could not read unknown qcow2 header "
1299 if (header
.backing_file_offset
> s
->cluster_size
) {
1300 error_setg(errp
, "Invalid backing file offset");
1305 if (header
.backing_file_offset
) {
1306 ext_end
= header
.backing_file_offset
;
1308 ext_end
= 1 << header
.cluster_bits
;
1311 /* Handle feature bits */
1312 s
->incompatible_features
= header
.incompatible_features
;
1313 s
->compatible_features
= header
.compatible_features
;
1314 s
->autoclear_features
= header
.autoclear_features
;
1316 if (s
->incompatible_features
& ~QCOW2_INCOMPAT_MASK
) {
1317 void *feature_table
= NULL
;
1318 qcow2_read_extensions(bs
, header
.header_length
, ext_end
,
1319 &feature_table
, flags
, NULL
, NULL
);
1320 report_unsupported_feature(errp
, feature_table
,
1321 s
->incompatible_features
&
1322 ~QCOW2_INCOMPAT_MASK
);
1324 g_free(feature_table
);
1328 if (s
->incompatible_features
& QCOW2_INCOMPAT_CORRUPT
) {
1329 /* Corrupt images may not be written to unless they are being repaired
1331 if ((flags
& BDRV_O_RDWR
) && !(flags
& BDRV_O_CHECK
)) {
1332 error_setg(errp
, "qcow2: Image is corrupt; cannot be opened "
1339 /* Check support for various header values */
1340 if (header
.refcount_order
> 6) {
1341 error_setg(errp
, "Reference count entry width too large; may not "
1346 s
->refcount_order
= header
.refcount_order
;
1347 s
->refcount_bits
= 1 << s
->refcount_order
;
1348 s
->refcount_max
= UINT64_C(1) << (s
->refcount_bits
- 1);
1349 s
->refcount_max
+= s
->refcount_max
- 1;
1351 s
->crypt_method_header
= header
.crypt_method
;
1352 if (s
->crypt_method_header
) {
1353 if (bdrv_uses_whitelist() &&
1354 s
->crypt_method_header
== QCOW_CRYPT_AES
) {
1356 "Use of AES-CBC encrypted qcow2 images is no longer "
1357 "supported in system emulators");
1358 error_append_hint(errp
,
1359 "You can use 'qemu-img convert' to convert your "
1360 "image to an alternative supported format, such "
1361 "as unencrypted qcow2, or raw with the LUKS "
1362 "format instead.\n");
1367 if (s
->crypt_method_header
== QCOW_CRYPT_AES
) {
1368 s
->crypt_physical_offset
= false;
1370 /* Assuming LUKS and any future crypt methods we
1371 * add will all use physical offsets, due to the
1372 * fact that the alternative is insecure... */
1373 s
->crypt_physical_offset
= true;
1376 bs
->encrypted
= true;
1379 s
->l2_bits
= s
->cluster_bits
- 3; /* L2 is always one cluster */
1380 s
->l2_size
= 1 << s
->l2_bits
;
1381 /* 2^(s->refcount_order - 3) is the refcount width in bytes */
1382 s
->refcount_block_bits
= s
->cluster_bits
- (s
->refcount_order
- 3);
1383 s
->refcount_block_size
= 1 << s
->refcount_block_bits
;
1384 bs
->total_sectors
= header
.size
/ BDRV_SECTOR_SIZE
;
1385 s
->csize_shift
= (62 - (s
->cluster_bits
- 8));
1386 s
->csize_mask
= (1 << (s
->cluster_bits
- 8)) - 1;
1387 s
->cluster_offset_mask
= (1LL << s
->csize_shift
) - 1;
1389 s
->refcount_table_offset
= header
.refcount_table_offset
;
1390 s
->refcount_table_size
=
1391 header
.refcount_table_clusters
<< (s
->cluster_bits
- 3);
1393 if (header
.refcount_table_clusters
== 0 && !(flags
& BDRV_O_CHECK
)) {
1394 error_setg(errp
, "Image does not contain a reference count table");
1399 ret
= qcow2_validate_table(bs
, s
->refcount_table_offset
,
1400 header
.refcount_table_clusters
,
1401 s
->cluster_size
, QCOW_MAX_REFTABLE_SIZE
,
1402 "Reference count table", errp
);
1407 /* The total size in bytes of the snapshot table is checked in
1408 * qcow2_read_snapshots() because the size of each snapshot is
1409 * variable and we don't know it yet.
1410 * Here we only check the offset and number of snapshots. */
1411 ret
= qcow2_validate_table(bs
, header
.snapshots_offset
,
1412 header
.nb_snapshots
,
1413 sizeof(QCowSnapshotHeader
),
1414 sizeof(QCowSnapshotHeader
) * QCOW_MAX_SNAPSHOTS
,
1415 "Snapshot table", errp
);
1420 /* read the level 1 table */
1421 ret
= qcow2_validate_table(bs
, header
.l1_table_offset
,
1422 header
.l1_size
, sizeof(uint64_t),
1423 QCOW_MAX_L1_SIZE
, "Active L1 table", errp
);
1427 s
->l1_size
= header
.l1_size
;
1428 s
->l1_table_offset
= header
.l1_table_offset
;
1430 l1_vm_state_index
= size_to_l1(s
, header
.size
);
1431 if (l1_vm_state_index
> INT_MAX
) {
1432 error_setg(errp
, "Image is too big");
1436 s
->l1_vm_state_index
= l1_vm_state_index
;
1438 /* the L1 table must contain at least enough entries to put
1439 header.size bytes */
1440 if (s
->l1_size
< s
->l1_vm_state_index
) {
1441 error_setg(errp
, "L1 table is too small");
1446 if (s
->l1_size
> 0) {
1447 s
->l1_table
= qemu_try_blockalign(bs
->file
->bs
,
1448 ROUND_UP(s
->l1_size
* sizeof(uint64_t), 512));
1449 if (s
->l1_table
== NULL
) {
1450 error_setg(errp
, "Could not allocate L1 table");
1454 ret
= bdrv_pread(bs
->file
, s
->l1_table_offset
, s
->l1_table
,
1455 s
->l1_size
* sizeof(uint64_t));
1457 error_setg_errno(errp
, -ret
, "Could not read L1 table");
1460 for(i
= 0;i
< s
->l1_size
; i
++) {
1461 s
->l1_table
[i
] = be64_to_cpu(s
->l1_table
[i
]);
1465 /* Parse driver-specific options */
1466 ret
= qcow2_update_options(bs
, options
, flags
, errp
);
1473 ret
= qcow2_refcount_init(bs
);
1475 error_setg_errno(errp
, -ret
, "Could not initialize refcount handling");
1479 QLIST_INIT(&s
->cluster_allocs
);
1480 QTAILQ_INIT(&s
->discards
);
1482 /* read qcow2 extensions */
1483 if (qcow2_read_extensions(bs
, header
.header_length
, ext_end
, NULL
,
1484 flags
, &update_header
, &local_err
)) {
1485 error_propagate(errp
, local_err
);
1490 /* Open external data file */
1491 s
->data_file
= bdrv_open_child(NULL
, options
, "data-file", bs
, &child_file
,
1494 error_propagate(errp
, local_err
);
1499 if (s
->incompatible_features
& QCOW2_INCOMPAT_DATA_FILE
) {
1500 if (!s
->data_file
&& s
->image_data_file
) {
1501 s
->data_file
= bdrv_open_child(s
->image_data_file
, options
,
1502 "data-file", bs
, &child_file
,
1504 if (!s
->data_file
) {
1509 if (!s
->data_file
) {
1510 error_setg(errp
, "'data-file' is required for this image");
1516 error_setg(errp
, "'data-file' can only be set for images with an "
1517 "external data file");
1522 s
->data_file
= bs
->file
;
1524 if (data_file_is_raw(bs
)) {
1525 error_setg(errp
, "data-file-raw requires a data file");
1531 /* qcow2_read_extension may have set up the crypto context
1532 * if the crypt method needs a header region, some methods
1533 * don't need header extensions, so must check here
1535 if (s
->crypt_method_header
&& !s
->crypto
) {
1536 if (s
->crypt_method_header
== QCOW_CRYPT_AES
) {
1537 unsigned int cflags
= 0;
1538 if (flags
& BDRV_O_NO_IO
) {
1539 cflags
|= QCRYPTO_BLOCK_OPEN_NO_IO
;
1541 s
->crypto
= qcrypto_block_open(s
->crypto_opts
, "encrypt.",
1543 QCOW2_MAX_THREADS
, errp
);
1548 } else if (!(flags
& BDRV_O_NO_IO
)) {
1549 error_setg(errp
, "Missing CRYPTO header for crypt method %d",
1550 s
->crypt_method_header
);
1556 /* read the backing file name */
1557 if (header
.backing_file_offset
!= 0) {
1558 len
= header
.backing_file_size
;
1559 if (len
> MIN(1023, s
->cluster_size
- header
.backing_file_offset
) ||
1560 len
>= sizeof(bs
->backing_file
)) {
1561 error_setg(errp
, "Backing file name too long");
1565 ret
= bdrv_pread(bs
->file
, header
.backing_file_offset
,
1566 bs
->auto_backing_file
, len
);
1568 error_setg_errno(errp
, -ret
, "Could not read backing file name");
1571 bs
->auto_backing_file
[len
] = '\0';
1572 pstrcpy(bs
->backing_file
, sizeof(bs
->backing_file
),
1573 bs
->auto_backing_file
);
1574 s
->image_backing_file
= g_strdup(bs
->auto_backing_file
);
1577 /* Internal snapshots */
1578 s
->snapshots_offset
= header
.snapshots_offset
;
1579 s
->nb_snapshots
= header
.nb_snapshots
;
1581 ret
= qcow2_read_snapshots(bs
);
1583 error_setg_errno(errp
, -ret
, "Could not read snapshots");
1587 /* Clear unknown autoclear feature bits */
1588 update_header
|= s
->autoclear_features
& ~QCOW2_AUTOCLEAR_MASK
;
1590 update_header
&& !bs
->read_only
&& !(flags
& BDRV_O_INACTIVE
);
1591 if (update_header
) {
1592 s
->autoclear_features
&= QCOW2_AUTOCLEAR_MASK
;
1595 /* == Handle persistent dirty bitmaps ==
1597 * We want load dirty bitmaps in three cases:
1599 * 1. Normal open of the disk in active mode, not related to invalidation
1602 * 2. Invalidation of the target vm after pre-copy phase of migration, if
1603 * bitmaps are _not_ migrating through migration channel, i.e.
1604 * 'dirty-bitmaps' capability is disabled.
1606 * 3. Invalidation of source vm after failed or canceled migration.
1607 * This is a very interesting case. There are two possible types of
1610 * A. Stored on inactivation and removed. They should be loaded from the
1613 * B. Not stored: not-persistent bitmaps and bitmaps, migrated through
1614 * the migration channel (with dirty-bitmaps capability).
1616 * On the other hand, there are two possible sub-cases:
1618 * 3.1 disk was changed by somebody else while were inactive. In this
1619 * case all in-RAM dirty bitmaps (both persistent and not) are
1620 * definitely invalid. And we don't have any method to determine
1623 * Simple and safe thing is to just drop all the bitmaps of type B on
1624 * inactivation. But in this case we lose bitmaps in valid 4.2 case.
1626 * On the other hand, resuming source vm, if disk was already changed
1627 * is a bad thing anyway: not only bitmaps, the whole vm state is
1628 * out of sync with disk.
1630 * This means, that user or management tool, who for some reason
1631 * decided to resume source vm, after disk was already changed by
1632 * target vm, should at least drop all dirty bitmaps by hand.
1634 * So, we can ignore this case for now, but TODO: "generation"
1635 * extension for qcow2, to determine, that image was changed after
1636 * last inactivation. And if it is changed, we will drop (or at least
1637 * mark as 'invalid' all the bitmaps of type B, both persistent
1640 * 3.2 disk was _not_ changed while were inactive. Bitmaps may be saved
1641 * to disk ('dirty-bitmaps' capability disabled), or not saved
1642 * ('dirty-bitmaps' capability enabled), but we don't need to care
1643 * of: let's load bitmaps as always: stored bitmaps will be loaded,
1644 * and not stored has flag IN_USE=1 in the image and will be skipped
1647 * One remaining possible case when we don't want load bitmaps:
1649 * 4. Open disk in inactive mode in target vm (bitmaps are migrating or
1650 * will be loaded on invalidation, no needs try loading them before)
1653 if (!(bdrv_get_flags(bs
) & BDRV_O_INACTIVE
)) {
1654 /* It's case 1, 2 or 3.2. Or 3.1 which is BUG in management layer. */
1655 bool header_updated
= qcow2_load_dirty_bitmaps(bs
, &local_err
);
1657 update_header
= update_header
&& !header_updated
;
1659 if (local_err
!= NULL
) {
1660 error_propagate(errp
, local_err
);
1665 if (update_header
) {
1666 ret
= qcow2_update_header(bs
);
1668 error_setg_errno(errp
, -ret
, "Could not update qcow2 header");
1673 bs
->supported_zero_flags
= header
.version
>= 3 ? BDRV_REQ_MAY_UNMAP
: 0;
1675 /* Repair image if dirty */
1676 if (!(flags
& (BDRV_O_CHECK
| BDRV_O_INACTIVE
)) && !bs
->read_only
&&
1677 (s
->incompatible_features
& QCOW2_INCOMPAT_DIRTY
)) {
1678 BdrvCheckResult result
= {0};
1680 ret
= qcow2_co_check_locked(bs
, &result
,
1681 BDRV_FIX_ERRORS
| BDRV_FIX_LEAKS
);
1682 if (ret
< 0 || result
.check_errors
) {
1686 error_setg_errno(errp
, -ret
, "Could not repair dirty image");
1693 BdrvCheckResult result
= {0};
1694 qcow2_check_refcounts(bs
, &result
, 0);
1698 qemu_co_queue_init(&s
->thread_task_queue
);
1703 g_free(s
->image_data_file
);
1704 if (has_data_file(bs
)) {
1705 bdrv_unref_child(bs
, s
->data_file
);
1707 g_free(s
->unknown_header_fields
);
1708 cleanup_unknown_header_ext(bs
);
1709 qcow2_free_snapshots(bs
);
1710 qcow2_refcount_close(bs
);
1711 qemu_vfree(s
->l1_table
);
1712 /* else pre-write overlap checks in cache_destroy may crash */
1714 cache_clean_timer_del(bs
);
1715 if (s
->l2_table_cache
) {
1716 qcow2_cache_destroy(s
->l2_table_cache
);
1718 if (s
->refcount_block_cache
) {
1719 qcow2_cache_destroy(s
->refcount_block_cache
);
1721 qcrypto_block_free(s
->crypto
);
1722 qapi_free_QCryptoBlockOpenOptions(s
->crypto_opts
);
1726 typedef struct QCow2OpenCo
{
1727 BlockDriverState
*bs
;
1734 static void coroutine_fn
qcow2_open_entry(void *opaque
)
1736 QCow2OpenCo
*qoc
= opaque
;
1737 BDRVQcow2State
*s
= qoc
->bs
->opaque
;
1739 qemu_co_mutex_lock(&s
->lock
);
1740 qoc
->ret
= qcow2_do_open(qoc
->bs
, qoc
->options
, qoc
->flags
, qoc
->errp
);
1741 qemu_co_mutex_unlock(&s
->lock
);
1744 static int qcow2_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
1747 BDRVQcow2State
*s
= bs
->opaque
;
1756 bs
->file
= bdrv_open_child(NULL
, options
, "file", bs
, &child_file
,
1762 /* Initialise locks */
1763 qemu_co_mutex_init(&s
->lock
);
1765 if (qemu_in_coroutine()) {
1766 /* From bdrv_co_create. */
1767 qcow2_open_entry(&qoc
);
1769 assert(qemu_get_current_aio_context() == qemu_get_aio_context());
1770 qemu_coroutine_enter(qemu_coroutine_create(qcow2_open_entry
, &qoc
));
1771 BDRV_POLL_WHILE(bs
, qoc
.ret
== -EINPROGRESS
);
1776 static void qcow2_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
1778 BDRVQcow2State
*s
= bs
->opaque
;
1780 if (bs
->encrypted
) {
1781 /* Encryption works on a sector granularity */
1782 bs
->bl
.request_alignment
= qcrypto_block_get_sector_size(s
->crypto
);
1784 bs
->bl
.pwrite_zeroes_alignment
= s
->cluster_size
;
1785 bs
->bl
.pdiscard_alignment
= s
->cluster_size
;
1788 static int qcow2_reopen_prepare(BDRVReopenState
*state
,
1789 BlockReopenQueue
*queue
, Error
**errp
)
1791 Qcow2ReopenState
*r
;
1794 r
= g_new0(Qcow2ReopenState
, 1);
1797 ret
= qcow2_update_options_prepare(state
->bs
, r
, state
->options
,
1798 state
->flags
, errp
);
1803 /* We need to write out any unwritten data if we reopen read-only. */
1804 if ((state
->flags
& BDRV_O_RDWR
) == 0) {
1805 ret
= qcow2_reopen_bitmaps_ro(state
->bs
, errp
);
1810 ret
= bdrv_flush(state
->bs
);
1815 ret
= qcow2_mark_clean(state
->bs
);
1824 qcow2_update_options_abort(state
->bs
, r
);
1829 static void qcow2_reopen_commit(BDRVReopenState
*state
)
1831 qcow2_update_options_commit(state
->bs
, state
->opaque
);
1832 g_free(state
->opaque
);
1835 static void qcow2_reopen_abort(BDRVReopenState
*state
)
1837 qcow2_update_options_abort(state
->bs
, state
->opaque
);
1838 g_free(state
->opaque
);
1841 static void qcow2_join_options(QDict
*options
, QDict
*old_options
)
1843 bool has_new_overlap_template
=
1844 qdict_haskey(options
, QCOW2_OPT_OVERLAP
) ||
1845 qdict_haskey(options
, QCOW2_OPT_OVERLAP_TEMPLATE
);
1846 bool has_new_total_cache_size
=
1847 qdict_haskey(options
, QCOW2_OPT_CACHE_SIZE
);
1848 bool has_all_cache_options
;
1850 /* New overlap template overrides all old overlap options */
1851 if (has_new_overlap_template
) {
1852 qdict_del(old_options
, QCOW2_OPT_OVERLAP
);
1853 qdict_del(old_options
, QCOW2_OPT_OVERLAP_TEMPLATE
);
1854 qdict_del(old_options
, QCOW2_OPT_OVERLAP_MAIN_HEADER
);
1855 qdict_del(old_options
, QCOW2_OPT_OVERLAP_ACTIVE_L1
);
1856 qdict_del(old_options
, QCOW2_OPT_OVERLAP_ACTIVE_L2
);
1857 qdict_del(old_options
, QCOW2_OPT_OVERLAP_REFCOUNT_TABLE
);
1858 qdict_del(old_options
, QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK
);
1859 qdict_del(old_options
, QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE
);
1860 qdict_del(old_options
, QCOW2_OPT_OVERLAP_INACTIVE_L1
);
1861 qdict_del(old_options
, QCOW2_OPT_OVERLAP_INACTIVE_L2
);
1864 /* New total cache size overrides all old options */
1865 if (qdict_haskey(options
, QCOW2_OPT_CACHE_SIZE
)) {
1866 qdict_del(old_options
, QCOW2_OPT_L2_CACHE_SIZE
);
1867 qdict_del(old_options
, QCOW2_OPT_REFCOUNT_CACHE_SIZE
);
1870 qdict_join(options
, old_options
, false);
1873 * If after merging all cache size options are set, an old total size is
1874 * overwritten. Do keep all options, however, if all three are new. The
1875 * resulting error message is what we want to happen.
1877 has_all_cache_options
=
1878 qdict_haskey(options
, QCOW2_OPT_CACHE_SIZE
) ||
1879 qdict_haskey(options
, QCOW2_OPT_L2_CACHE_SIZE
) ||
1880 qdict_haskey(options
, QCOW2_OPT_REFCOUNT_CACHE_SIZE
);
1882 if (has_all_cache_options
&& !has_new_total_cache_size
) {
1883 qdict_del(options
, QCOW2_OPT_CACHE_SIZE
);
1887 static int coroutine_fn
qcow2_co_block_status(BlockDriverState
*bs
,
1889 int64_t offset
, int64_t count
,
1890 int64_t *pnum
, int64_t *map
,
1891 BlockDriverState
**file
)
1893 BDRVQcow2State
*s
= bs
->opaque
;
1894 uint64_t cluster_offset
;
1895 int index_in_cluster
, ret
;
1899 if (!s
->metadata_preallocation_checked
) {
1900 ret
= qcow2_detect_metadata_preallocation(bs
);
1901 s
->metadata_preallocation
= (ret
== 1);
1902 s
->metadata_preallocation_checked
= true;
1905 bytes
= MIN(INT_MAX
, count
);
1906 qemu_co_mutex_lock(&s
->lock
);
1907 ret
= qcow2_get_cluster_offset(bs
, offset
, &bytes
, &cluster_offset
);
1908 qemu_co_mutex_unlock(&s
->lock
);
1915 if ((ret
== QCOW2_CLUSTER_NORMAL
|| ret
== QCOW2_CLUSTER_ZERO_ALLOC
) &&
1917 index_in_cluster
= offset
& (s
->cluster_size
- 1);
1918 *map
= cluster_offset
| index_in_cluster
;
1919 *file
= s
->data_file
->bs
;
1920 status
|= BDRV_BLOCK_OFFSET_VALID
;
1922 if (ret
== QCOW2_CLUSTER_ZERO_PLAIN
|| ret
== QCOW2_CLUSTER_ZERO_ALLOC
) {
1923 status
|= BDRV_BLOCK_ZERO
;
1924 } else if (ret
!= QCOW2_CLUSTER_UNALLOCATED
) {
1925 status
|= BDRV_BLOCK_DATA
;
1927 if (s
->metadata_preallocation
&& (status
& BDRV_BLOCK_DATA
) &&
1928 (status
& BDRV_BLOCK_OFFSET_VALID
))
1930 status
|= BDRV_BLOCK_RECURSE
;
1935 static coroutine_fn
int qcow2_handle_l2meta(BlockDriverState
*bs
,
1936 QCowL2Meta
**pl2meta
,
1940 QCowL2Meta
*l2meta
= *pl2meta
;
1942 while (l2meta
!= NULL
) {
1946 ret
= qcow2_alloc_cluster_link_l2(bs
, l2meta
);
1951 qcow2_alloc_cluster_abort(bs
, l2meta
);
1954 /* Take the request off the list of running requests */
1955 if (l2meta
->nb_clusters
!= 0) {
1956 QLIST_REMOVE(l2meta
, next_in_flight
);
1959 qemu_co_queue_restart_all(&l2meta
->dependent_requests
);
1961 next
= l2meta
->next
;
1970 static coroutine_fn
int qcow2_co_preadv(BlockDriverState
*bs
, uint64_t offset
,
1971 uint64_t bytes
, QEMUIOVector
*qiov
,
1974 BDRVQcow2State
*s
= bs
->opaque
;
1975 int offset_in_cluster
;
1977 unsigned int cur_bytes
; /* number of bytes in current iteration */
1978 uint64_t cluster_offset
= 0;
1979 uint64_t bytes_done
= 0;
1980 QEMUIOVector hd_qiov
;
1981 uint8_t *cluster_data
= NULL
;
1983 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
1985 while (bytes
!= 0) {
1987 /* prepare next request */
1988 cur_bytes
= MIN(bytes
, INT_MAX
);
1990 cur_bytes
= MIN(cur_bytes
,
1991 QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_size
);
1994 qemu_co_mutex_lock(&s
->lock
);
1995 ret
= qcow2_get_cluster_offset(bs
, offset
, &cur_bytes
, &cluster_offset
);
1996 qemu_co_mutex_unlock(&s
->lock
);
2001 offset_in_cluster
= offset_into_cluster(s
, offset
);
2003 qemu_iovec_reset(&hd_qiov
);
2004 qemu_iovec_concat(&hd_qiov
, qiov
, bytes_done
, cur_bytes
);
2007 case QCOW2_CLUSTER_UNALLOCATED
:
2010 BLKDBG_EVENT(bs
->file
, BLKDBG_READ_BACKING_AIO
);
2011 ret
= bdrv_co_preadv(bs
->backing
, offset
, cur_bytes
,
2017 /* Note: in this case, no need to wait */
2018 qemu_iovec_memset(&hd_qiov
, 0, 0, cur_bytes
);
2022 case QCOW2_CLUSTER_ZERO_PLAIN
:
2023 case QCOW2_CLUSTER_ZERO_ALLOC
:
2024 qemu_iovec_memset(&hd_qiov
, 0, 0, cur_bytes
);
2027 case QCOW2_CLUSTER_COMPRESSED
:
2028 ret
= qcow2_co_preadv_compressed(bs
, cluster_offset
,
2037 case QCOW2_CLUSTER_NORMAL
:
2038 if ((cluster_offset
& 511) != 0) {
2043 if (bs
->encrypted
) {
2047 * For encrypted images, read everything into a temporary
2048 * contiguous buffer on which the AES functions can work.
2050 if (!cluster_data
) {
2052 qemu_try_blockalign(s
->data_file
->bs
,
2053 QCOW_MAX_CRYPT_CLUSTERS
2055 if (cluster_data
== NULL
) {
2061 assert(cur_bytes
<= QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_size
);
2062 qemu_iovec_reset(&hd_qiov
);
2063 qemu_iovec_add(&hd_qiov
, cluster_data
, cur_bytes
);
2066 BLKDBG_EVENT(bs
->file
, BLKDBG_READ_AIO
);
2067 ret
= bdrv_co_preadv(s
->data_file
,
2068 cluster_offset
+ offset_in_cluster
,
2069 cur_bytes
, &hd_qiov
, 0);
2073 if (bs
->encrypted
) {
2075 assert((offset
& (BDRV_SECTOR_SIZE
- 1)) == 0);
2076 assert((cur_bytes
& (BDRV_SECTOR_SIZE
- 1)) == 0);
2077 if (qcow2_co_decrypt(bs
, cluster_offset
, offset
,
2078 cluster_data
, cur_bytes
) < 0) {
2082 qemu_iovec_from_buf(qiov
, bytes_done
, cluster_data
, cur_bytes
);
2087 g_assert_not_reached();
2093 offset
+= cur_bytes
;
2094 bytes_done
+= cur_bytes
;
2099 qemu_iovec_destroy(&hd_qiov
);
2100 qemu_vfree(cluster_data
);
2105 /* Check if it's possible to merge a write request with the writing of
2106 * the data from the COW regions */
2107 static bool merge_cow(uint64_t offset
, unsigned bytes
,
2108 QEMUIOVector
*hd_qiov
, QCowL2Meta
*l2meta
)
2112 for (m
= l2meta
; m
!= NULL
; m
= m
->next
) {
2113 /* If both COW regions are empty then there's nothing to merge */
2114 if (m
->cow_start
.nb_bytes
== 0 && m
->cow_end
.nb_bytes
== 0) {
2118 /* If COW regions are handled already, skip this too */
2123 /* The data (middle) region must be immediately after the
2125 if (l2meta_cow_start(m
) + m
->cow_start
.nb_bytes
!= offset
) {
2129 /* The end region must be immediately after the data (middle)
2131 if (m
->offset
+ m
->cow_end
.offset
!= offset
+ bytes
) {
2135 /* Make sure that adding both COW regions to the QEMUIOVector
2136 * does not exceed IOV_MAX */
2137 if (hd_qiov
->niov
> IOV_MAX
- 2) {
2141 m
->data_qiov
= hd_qiov
;
2148 static bool is_unallocated(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
)
2152 (!bdrv_is_allocated_above(bs
, NULL
, false, offset
, bytes
, &nr
) &&
2156 static bool is_zero_cow(BlockDriverState
*bs
, QCowL2Meta
*m
)
2159 * This check is designed for optimization shortcut so it must be
2161 * Instead of is_zero(), use is_unallocated() as it is faster (but not
2162 * as accurate and can result in false negatives).
2164 return is_unallocated(bs
, m
->offset
+ m
->cow_start
.offset
,
2165 m
->cow_start
.nb_bytes
) &&
2166 is_unallocated(bs
, m
->offset
+ m
->cow_end
.offset
,
2167 m
->cow_end
.nb_bytes
);
2170 static int handle_alloc_space(BlockDriverState
*bs
, QCowL2Meta
*l2meta
)
2172 BDRVQcow2State
*s
= bs
->opaque
;
2175 if (!(s
->data_file
->bs
->supported_zero_flags
& BDRV_REQ_NO_FALLBACK
)) {
2179 if (bs
->encrypted
) {
2183 for (m
= l2meta
; m
!= NULL
; m
= m
->next
) {
2186 if (!m
->cow_start
.nb_bytes
&& !m
->cow_end
.nb_bytes
) {
2190 if (!is_zero_cow(bs
, m
)) {
2195 * instead of writing zero COW buffers,
2196 * efficiently zero out the whole clusters
2199 ret
= qcow2_pre_write_overlap_check(bs
, 0, m
->alloc_offset
,
2200 m
->nb_clusters
* s
->cluster_size
,
2206 BLKDBG_EVENT(bs
->file
, BLKDBG_CLUSTER_ALLOC_SPACE
);
2207 ret
= bdrv_co_pwrite_zeroes(s
->data_file
, m
->alloc_offset
,
2208 m
->nb_clusters
* s
->cluster_size
,
2209 BDRV_REQ_NO_FALLBACK
);
2211 if (ret
!= -ENOTSUP
&& ret
!= -EAGAIN
) {
2217 trace_qcow2_skip_cow(qemu_coroutine_self(), m
->offset
, m
->nb_clusters
);
2223 static coroutine_fn
int qcow2_co_pwritev(BlockDriverState
*bs
, uint64_t offset
,
2224 uint64_t bytes
, QEMUIOVector
*qiov
,
2227 BDRVQcow2State
*s
= bs
->opaque
;
2228 int offset_in_cluster
;
2230 unsigned int cur_bytes
; /* number of sectors in current iteration */
2231 uint64_t cluster_offset
;
2232 QEMUIOVector hd_qiov
;
2233 uint64_t bytes_done
= 0;
2234 uint8_t *cluster_data
= NULL
;
2235 QCowL2Meta
*l2meta
= NULL
;
2237 trace_qcow2_writev_start_req(qemu_coroutine_self(), offset
, bytes
);
2239 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
2241 qemu_co_mutex_lock(&s
->lock
);
2243 while (bytes
!= 0) {
2247 trace_qcow2_writev_start_part(qemu_coroutine_self());
2248 offset_in_cluster
= offset_into_cluster(s
, offset
);
2249 cur_bytes
= MIN(bytes
, INT_MAX
);
2250 if (bs
->encrypted
) {
2251 cur_bytes
= MIN(cur_bytes
,
2252 QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_size
2253 - offset_in_cluster
);
2256 ret
= qcow2_alloc_cluster_offset(bs
, offset
, &cur_bytes
,
2257 &cluster_offset
, &l2meta
);
2262 assert((cluster_offset
& 511) == 0);
2264 ret
= qcow2_pre_write_overlap_check(bs
, 0,
2265 cluster_offset
+ offset_in_cluster
,
2271 qemu_co_mutex_unlock(&s
->lock
);
2273 qemu_iovec_reset(&hd_qiov
);
2274 qemu_iovec_concat(&hd_qiov
, qiov
, bytes_done
, cur_bytes
);
2276 if (bs
->encrypted
) {
2278 if (!cluster_data
) {
2279 cluster_data
= qemu_try_blockalign(bs
->file
->bs
,
2280 QCOW_MAX_CRYPT_CLUSTERS
2282 if (cluster_data
== NULL
) {
2288 assert(hd_qiov
.size
<=
2289 QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_size
);
2290 qemu_iovec_to_buf(&hd_qiov
, 0, cluster_data
, hd_qiov
.size
);
2292 if (qcow2_co_encrypt(bs
, cluster_offset
, offset
,
2293 cluster_data
, cur_bytes
) < 0) {
2298 qemu_iovec_reset(&hd_qiov
);
2299 qemu_iovec_add(&hd_qiov
, cluster_data
, cur_bytes
);
2302 /* Try to efficiently initialize the physical space with zeroes */
2303 ret
= handle_alloc_space(bs
, l2meta
);
2308 /* If we need to do COW, check if it's possible to merge the
2309 * writing of the guest data together with that of the COW regions.
2310 * If it's not possible (or not necessary) then write the
2311 * guest data now. */
2312 if (!merge_cow(offset
, cur_bytes
, &hd_qiov
, l2meta
)) {
2313 BLKDBG_EVENT(bs
->file
, BLKDBG_WRITE_AIO
);
2314 trace_qcow2_writev_data(qemu_coroutine_self(),
2315 cluster_offset
+ offset_in_cluster
);
2316 ret
= bdrv_co_pwritev(s
->data_file
,
2317 cluster_offset
+ offset_in_cluster
,
2318 cur_bytes
, &hd_qiov
, 0);
2324 qemu_co_mutex_lock(&s
->lock
);
2326 ret
= qcow2_handle_l2meta(bs
, &l2meta
, true);
2332 offset
+= cur_bytes
;
2333 bytes_done
+= cur_bytes
;
2334 trace_qcow2_writev_done_part(qemu_coroutine_self(), cur_bytes
);
2340 qemu_co_mutex_lock(&s
->lock
);
2343 qcow2_handle_l2meta(bs
, &l2meta
, false);
2345 qemu_co_mutex_unlock(&s
->lock
);
2347 qemu_iovec_destroy(&hd_qiov
);
2348 qemu_vfree(cluster_data
);
2349 trace_qcow2_writev_done_req(qemu_coroutine_self(), ret
);
2354 static int qcow2_inactivate(BlockDriverState
*bs
)
2356 BDRVQcow2State
*s
= bs
->opaque
;
2357 int ret
, result
= 0;
2358 Error
*local_err
= NULL
;
2360 qcow2_store_persistent_dirty_bitmaps(bs
, &local_err
);
2361 if (local_err
!= NULL
) {
2363 error_reportf_err(local_err
, "Lost persistent bitmaps during "
2364 "inactivation of node '%s': ",
2365 bdrv_get_device_or_node_name(bs
));
2368 ret
= qcow2_cache_flush(bs
, s
->l2_table_cache
);
2371 error_report("Failed to flush the L2 table cache: %s",
2375 ret
= qcow2_cache_flush(bs
, s
->refcount_block_cache
);
2378 error_report("Failed to flush the refcount block cache: %s",
2383 qcow2_mark_clean(bs
);
2389 static void qcow2_close(BlockDriverState
*bs
)
2391 BDRVQcow2State
*s
= bs
->opaque
;
2392 qemu_vfree(s
->l1_table
);
2393 /* else pre-write overlap checks in cache_destroy may crash */
2396 if (!(s
->flags
& BDRV_O_INACTIVE
)) {
2397 qcow2_inactivate(bs
);
2400 cache_clean_timer_del(bs
);
2401 qcow2_cache_destroy(s
->l2_table_cache
);
2402 qcow2_cache_destroy(s
->refcount_block_cache
);
2404 qcrypto_block_free(s
->crypto
);
2407 g_free(s
->unknown_header_fields
);
2408 cleanup_unknown_header_ext(bs
);
2410 g_free(s
->image_data_file
);
2411 g_free(s
->image_backing_file
);
2412 g_free(s
->image_backing_format
);
2414 if (has_data_file(bs
)) {
2415 bdrv_unref_child(bs
, s
->data_file
);
2418 qcow2_refcount_close(bs
);
2419 qcow2_free_snapshots(bs
);
2422 static void coroutine_fn
qcow2_co_invalidate_cache(BlockDriverState
*bs
,
2425 BDRVQcow2State
*s
= bs
->opaque
;
2426 int flags
= s
->flags
;
2427 QCryptoBlock
*crypto
= NULL
;
2429 Error
*local_err
= NULL
;
2433 * Backing files are read-only which makes all of their metadata immutable,
2434 * that means we don't have to worry about reopening them here.
2442 memset(s
, 0, sizeof(BDRVQcow2State
));
2443 options
= qdict_clone_shallow(bs
->options
);
2445 flags
&= ~BDRV_O_INACTIVE
;
2446 qemu_co_mutex_lock(&s
->lock
);
2447 ret
= qcow2_do_open(bs
, options
, flags
, &local_err
);
2448 qemu_co_mutex_unlock(&s
->lock
);
2449 qobject_unref(options
);
2451 error_propagate_prepend(errp
, local_err
,
2452 "Could not reopen qcow2 layer: ");
2455 } else if (ret
< 0) {
2456 error_setg_errno(errp
, -ret
, "Could not reopen qcow2 layer");
2464 static size_t header_ext_add(char *buf
, uint32_t magic
, const void *s
,
2465 size_t len
, size_t buflen
)
2467 QCowExtension
*ext_backing_fmt
= (QCowExtension
*) buf
;
2468 size_t ext_len
= sizeof(QCowExtension
) + ((len
+ 7) & ~7);
2470 if (buflen
< ext_len
) {
2474 *ext_backing_fmt
= (QCowExtension
) {
2475 .magic
= cpu_to_be32(magic
),
2476 .len
= cpu_to_be32(len
),
2480 memcpy(buf
+ sizeof(QCowExtension
), s
, len
);
2487 * Updates the qcow2 header, including the variable length parts of it, i.e.
2488 * the backing file name and all extensions. qcow2 was not designed to allow
2489 * such changes, so if we run out of space (we can only use the first cluster)
2490 * this function may fail.
2492 * Returns 0 on success, -errno in error cases.
2494 int qcow2_update_header(BlockDriverState
*bs
)
2496 BDRVQcow2State
*s
= bs
->opaque
;
2499 size_t buflen
= s
->cluster_size
;
2501 uint64_t total_size
;
2502 uint32_t refcount_table_clusters
;
2503 size_t header_length
;
2504 Qcow2UnknownHeaderExtension
*uext
;
2506 buf
= qemu_blockalign(bs
, buflen
);
2508 /* Header structure */
2509 header
= (QCowHeader
*) buf
;
2511 if (buflen
< sizeof(*header
)) {
2516 header_length
= sizeof(*header
) + s
->unknown_header_fields_size
;
2517 total_size
= bs
->total_sectors
* BDRV_SECTOR_SIZE
;
2518 refcount_table_clusters
= s
->refcount_table_size
>> (s
->cluster_bits
- 3);
2520 *header
= (QCowHeader
) {
2521 /* Version 2 fields */
2522 .magic
= cpu_to_be32(QCOW_MAGIC
),
2523 .version
= cpu_to_be32(s
->qcow_version
),
2524 .backing_file_offset
= 0,
2525 .backing_file_size
= 0,
2526 .cluster_bits
= cpu_to_be32(s
->cluster_bits
),
2527 .size
= cpu_to_be64(total_size
),
2528 .crypt_method
= cpu_to_be32(s
->crypt_method_header
),
2529 .l1_size
= cpu_to_be32(s
->l1_size
),
2530 .l1_table_offset
= cpu_to_be64(s
->l1_table_offset
),
2531 .refcount_table_offset
= cpu_to_be64(s
->refcount_table_offset
),
2532 .refcount_table_clusters
= cpu_to_be32(refcount_table_clusters
),
2533 .nb_snapshots
= cpu_to_be32(s
->nb_snapshots
),
2534 .snapshots_offset
= cpu_to_be64(s
->snapshots_offset
),
2536 /* Version 3 fields */
2537 .incompatible_features
= cpu_to_be64(s
->incompatible_features
),
2538 .compatible_features
= cpu_to_be64(s
->compatible_features
),
2539 .autoclear_features
= cpu_to_be64(s
->autoclear_features
),
2540 .refcount_order
= cpu_to_be32(s
->refcount_order
),
2541 .header_length
= cpu_to_be32(header_length
),
2544 /* For older versions, write a shorter header */
2545 switch (s
->qcow_version
) {
2547 ret
= offsetof(QCowHeader
, incompatible_features
);
2550 ret
= sizeof(*header
);
2559 memset(buf
, 0, buflen
);
2561 /* Preserve any unknown field in the header */
2562 if (s
->unknown_header_fields_size
) {
2563 if (buflen
< s
->unknown_header_fields_size
) {
2568 memcpy(buf
, s
->unknown_header_fields
, s
->unknown_header_fields_size
);
2569 buf
+= s
->unknown_header_fields_size
;
2570 buflen
-= s
->unknown_header_fields_size
;
2573 /* Backing file format header extension */
2574 if (s
->image_backing_format
) {
2575 ret
= header_ext_add(buf
, QCOW2_EXT_MAGIC_BACKING_FORMAT
,
2576 s
->image_backing_format
,
2577 strlen(s
->image_backing_format
),
2587 /* External data file header extension */
2588 if (has_data_file(bs
) && s
->image_data_file
) {
2589 ret
= header_ext_add(buf
, QCOW2_EXT_MAGIC_DATA_FILE
,
2590 s
->image_data_file
, strlen(s
->image_data_file
),
2600 /* Full disk encryption header pointer extension */
2601 if (s
->crypto_header
.offset
!= 0) {
2602 s
->crypto_header
.offset
= cpu_to_be64(s
->crypto_header
.offset
);
2603 s
->crypto_header
.length
= cpu_to_be64(s
->crypto_header
.length
);
2604 ret
= header_ext_add(buf
, QCOW2_EXT_MAGIC_CRYPTO_HEADER
,
2605 &s
->crypto_header
, sizeof(s
->crypto_header
),
2607 s
->crypto_header
.offset
= be64_to_cpu(s
->crypto_header
.offset
);
2608 s
->crypto_header
.length
= be64_to_cpu(s
->crypto_header
.length
);
2617 if (s
->qcow_version
>= 3) {
2618 Qcow2Feature features
[] = {
2620 .type
= QCOW2_FEAT_TYPE_INCOMPATIBLE
,
2621 .bit
= QCOW2_INCOMPAT_DIRTY_BITNR
,
2622 .name
= "dirty bit",
2625 .type
= QCOW2_FEAT_TYPE_INCOMPATIBLE
,
2626 .bit
= QCOW2_INCOMPAT_CORRUPT_BITNR
,
2627 .name
= "corrupt bit",
2630 .type
= QCOW2_FEAT_TYPE_INCOMPATIBLE
,
2631 .bit
= QCOW2_INCOMPAT_DATA_FILE_BITNR
,
2632 .name
= "external data file",
2635 .type
= QCOW2_FEAT_TYPE_COMPATIBLE
,
2636 .bit
= QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR
,
2637 .name
= "lazy refcounts",
2641 ret
= header_ext_add(buf
, QCOW2_EXT_MAGIC_FEATURE_TABLE
,
2642 features
, sizeof(features
), buflen
);
2650 /* Bitmap extension */
2651 if (s
->nb_bitmaps
> 0) {
2652 Qcow2BitmapHeaderExt bitmaps_header
= {
2653 .nb_bitmaps
= cpu_to_be32(s
->nb_bitmaps
),
2654 .bitmap_directory_size
=
2655 cpu_to_be64(s
->bitmap_directory_size
),
2656 .bitmap_directory_offset
=
2657 cpu_to_be64(s
->bitmap_directory_offset
)
2659 ret
= header_ext_add(buf
, QCOW2_EXT_MAGIC_BITMAPS
,
2660 &bitmaps_header
, sizeof(bitmaps_header
),
2669 /* Keep unknown header extensions */
2670 QLIST_FOREACH(uext
, &s
->unknown_header_ext
, next
) {
2671 ret
= header_ext_add(buf
, uext
->magic
, uext
->data
, uext
->len
, buflen
);
2680 /* End of header extensions */
2681 ret
= header_ext_add(buf
, QCOW2_EXT_MAGIC_END
, NULL
, 0, buflen
);
2689 /* Backing file name */
2690 if (s
->image_backing_file
) {
2691 size_t backing_file_len
= strlen(s
->image_backing_file
);
2693 if (buflen
< backing_file_len
) {
2698 /* Using strncpy is ok here, since buf is not NUL-terminated. */
2699 strncpy(buf
, s
->image_backing_file
, buflen
);
2701 header
->backing_file_offset
= cpu_to_be64(buf
- ((char*) header
));
2702 header
->backing_file_size
= cpu_to_be32(backing_file_len
);
2705 /* Write the new header */
2706 ret
= bdrv_pwrite(bs
->file
, 0, header
, s
->cluster_size
);
2717 static int qcow2_change_backing_file(BlockDriverState
*bs
,
2718 const char *backing_file
, const char *backing_fmt
)
2720 BDRVQcow2State
*s
= bs
->opaque
;
2722 /* Adding a backing file means that the external data file alone won't be
2723 * enough to make sense of the content */
2724 if (backing_file
&& data_file_is_raw(bs
)) {
2728 if (backing_file
&& strlen(backing_file
) > 1023) {
2732 pstrcpy(bs
->auto_backing_file
, sizeof(bs
->auto_backing_file
),
2733 backing_file
?: "");
2734 pstrcpy(bs
->backing_file
, sizeof(bs
->backing_file
), backing_file
?: "");
2735 pstrcpy(bs
->backing_format
, sizeof(bs
->backing_format
), backing_fmt
?: "");
2737 g_free(s
->image_backing_file
);
2738 g_free(s
->image_backing_format
);
2740 s
->image_backing_file
= backing_file
? g_strdup(bs
->backing_file
) : NULL
;
2741 s
->image_backing_format
= backing_fmt
? g_strdup(bs
->backing_format
) : NULL
;
2743 return qcow2_update_header(bs
);
2746 static int qcow2_crypt_method_from_format(const char *encryptfmt
)
2748 if (g_str_equal(encryptfmt
, "luks")) {
2749 return QCOW_CRYPT_LUKS
;
2750 } else if (g_str_equal(encryptfmt
, "aes")) {
2751 return QCOW_CRYPT_AES
;
2757 static int qcow2_set_up_encryption(BlockDriverState
*bs
,
2758 QCryptoBlockCreateOptions
*cryptoopts
,
2761 BDRVQcow2State
*s
= bs
->opaque
;
2762 QCryptoBlock
*crypto
= NULL
;
2765 switch (cryptoopts
->format
) {
2766 case Q_CRYPTO_BLOCK_FORMAT_LUKS
:
2767 fmt
= QCOW_CRYPT_LUKS
;
2769 case Q_CRYPTO_BLOCK_FORMAT_QCOW
:
2770 fmt
= QCOW_CRYPT_AES
;
2773 error_setg(errp
, "Crypto format not supported in qcow2");
2777 s
->crypt_method_header
= fmt
;
2779 crypto
= qcrypto_block_create(cryptoopts
, "encrypt.",
2780 qcow2_crypto_hdr_init_func
,
2781 qcow2_crypto_hdr_write_func
,
2787 ret
= qcow2_update_header(bs
);
2789 error_setg_errno(errp
, -ret
, "Could not write encryption header");
2795 qcrypto_block_free(crypto
);
2800 * Preallocates metadata structures for data clusters between @offset (in the
2801 * guest disk) and @new_length (which is thus generally the new guest disk
2804 * Returns: 0 on success, -errno on failure.
2806 static int coroutine_fn
preallocate_co(BlockDriverState
*bs
, uint64_t offset
,
2807 uint64_t new_length
, PreallocMode mode
,
2810 BDRVQcow2State
*s
= bs
->opaque
;
2812 uint64_t host_offset
= 0;
2813 int64_t file_length
;
2814 unsigned int cur_bytes
;
2818 assert(offset
<= new_length
);
2819 bytes
= new_length
- offset
;
2822 cur_bytes
= MIN(bytes
, QEMU_ALIGN_DOWN(INT_MAX
, s
->cluster_size
));
2823 ret
= qcow2_alloc_cluster_offset(bs
, offset
, &cur_bytes
,
2824 &host_offset
, &meta
);
2826 error_setg_errno(errp
, -ret
, "Allocating clusters failed");
2831 QCowL2Meta
*next
= meta
->next
;
2833 ret
= qcow2_alloc_cluster_link_l2(bs
, meta
);
2835 error_setg_errno(errp
, -ret
, "Mapping clusters failed");
2836 qcow2_free_any_clusters(bs
, meta
->alloc_offset
,
2837 meta
->nb_clusters
, QCOW2_DISCARD_NEVER
);
2841 /* There are no dependent requests, but we need to remove our
2842 * request from the list of in-flight requests */
2843 QLIST_REMOVE(meta
, next_in_flight
);
2849 /* TODO Preallocate data if requested */
2852 offset
+= cur_bytes
;
2856 * It is expected that the image file is large enough to actually contain
2857 * all of the allocated clusters (otherwise we get failing reads after
2858 * EOF). Extend the image to the last allocated sector.
2860 file_length
= bdrv_getlength(s
->data_file
->bs
);
2861 if (file_length
< 0) {
2862 error_setg_errno(errp
, -file_length
, "Could not get file size");
2866 if (host_offset
+ cur_bytes
> file_length
) {
2867 if (mode
== PREALLOC_MODE_METADATA
) {
2868 mode
= PREALLOC_MODE_OFF
;
2870 ret
= bdrv_co_truncate(s
->data_file
, host_offset
+ cur_bytes
, mode
,
2880 /* qcow2_refcount_metadata_size:
2881 * @clusters: number of clusters to refcount (including data and L1/L2 tables)
2882 * @cluster_size: size of a cluster, in bytes
2883 * @refcount_order: refcount bits power-of-2 exponent
2884 * @generous_increase: allow for the refcount table to be 1.5x as large as it
2887 * Returns: Number of bytes required for refcount blocks and table metadata.
2889 int64_t qcow2_refcount_metadata_size(int64_t clusters
, size_t cluster_size
,
2890 int refcount_order
, bool generous_increase
,
2891 uint64_t *refblock_count
)
2894 * Every host cluster is reference-counted, including metadata (even
2895 * refcount metadata is recursively included).
2897 * An accurate formula for the size of refcount metadata size is difficult
2898 * to derive. An easier method of calculation is finding the fixed point
2899 * where no further refcount blocks or table clusters are required to
2900 * reference count every cluster.
2902 int64_t blocks_per_table_cluster
= cluster_size
/ sizeof(uint64_t);
2903 int64_t refcounts_per_block
= cluster_size
* 8 / (1 << refcount_order
);
2904 int64_t table
= 0; /* number of refcount table clusters */
2905 int64_t blocks
= 0; /* number of refcount block clusters */
2911 blocks
= DIV_ROUND_UP(clusters
+ table
+ blocks
, refcounts_per_block
);
2912 table
= DIV_ROUND_UP(blocks
, blocks_per_table_cluster
);
2913 n
= clusters
+ blocks
+ table
;
2915 if (n
== last
&& generous_increase
) {
2916 clusters
+= DIV_ROUND_UP(table
, 2);
2917 n
= 0; /* force another loop */
2918 generous_increase
= false;
2920 } while (n
!= last
);
2922 if (refblock_count
) {
2923 *refblock_count
= blocks
;
2926 return (blocks
+ table
) * cluster_size
;
2930 * qcow2_calc_prealloc_size:
2931 * @total_size: virtual disk size in bytes
2932 * @cluster_size: cluster size in bytes
2933 * @refcount_order: refcount bits power-of-2 exponent
2935 * Returns: Total number of bytes required for the fully allocated image
2936 * (including metadata).
2938 static int64_t qcow2_calc_prealloc_size(int64_t total_size
,
2939 size_t cluster_size
,
2942 int64_t meta_size
= 0;
2943 uint64_t nl1e
, nl2e
;
2944 int64_t aligned_total_size
= ROUND_UP(total_size
, cluster_size
);
2946 /* header: 1 cluster */
2947 meta_size
+= cluster_size
;
2949 /* total size of L2 tables */
2950 nl2e
= aligned_total_size
/ cluster_size
;
2951 nl2e
= ROUND_UP(nl2e
, cluster_size
/ sizeof(uint64_t));
2952 meta_size
+= nl2e
* sizeof(uint64_t);
2954 /* total size of L1 tables */
2955 nl1e
= nl2e
* sizeof(uint64_t) / cluster_size
;
2956 nl1e
= ROUND_UP(nl1e
, cluster_size
/ sizeof(uint64_t));
2957 meta_size
+= nl1e
* sizeof(uint64_t);
2959 /* total size of refcount table and blocks */
2960 meta_size
+= qcow2_refcount_metadata_size(
2961 (meta_size
+ aligned_total_size
) / cluster_size
,
2962 cluster_size
, refcount_order
, false, NULL
);
2964 return meta_size
+ aligned_total_size
;
2967 static bool validate_cluster_size(size_t cluster_size
, Error
**errp
)
2969 int cluster_bits
= ctz32(cluster_size
);
2970 if (cluster_bits
< MIN_CLUSTER_BITS
|| cluster_bits
> MAX_CLUSTER_BITS
||
2971 (1 << cluster_bits
) != cluster_size
)
2973 error_setg(errp
, "Cluster size must be a power of two between %d and "
2974 "%dk", 1 << MIN_CLUSTER_BITS
, 1 << (MAX_CLUSTER_BITS
- 10));
2980 static size_t qcow2_opt_get_cluster_size_del(QemuOpts
*opts
, Error
**errp
)
2982 size_t cluster_size
;
2984 cluster_size
= qemu_opt_get_size_del(opts
, BLOCK_OPT_CLUSTER_SIZE
,
2985 DEFAULT_CLUSTER_SIZE
);
2986 if (!validate_cluster_size(cluster_size
, errp
)) {
2989 return cluster_size
;
2992 static int qcow2_opt_get_version_del(QemuOpts
*opts
, Error
**errp
)
2997 buf
= qemu_opt_get_del(opts
, BLOCK_OPT_COMPAT_LEVEL
);
2999 ret
= 3; /* default */
3000 } else if (!strcmp(buf
, "0.10")) {
3002 } else if (!strcmp(buf
, "1.1")) {
3005 error_setg(errp
, "Invalid compatibility level: '%s'", buf
);
3012 static uint64_t qcow2_opt_get_refcount_bits_del(QemuOpts
*opts
, int version
,
3015 uint64_t refcount_bits
;
3017 refcount_bits
= qemu_opt_get_number_del(opts
, BLOCK_OPT_REFCOUNT_BITS
, 16);
3018 if (refcount_bits
> 64 || !is_power_of_2(refcount_bits
)) {
3019 error_setg(errp
, "Refcount width must be a power of two and may not "
3024 if (version
< 3 && refcount_bits
!= 16) {
3025 error_setg(errp
, "Different refcount widths than 16 bits require "
3026 "compatibility level 1.1 or above (use compat=1.1 or "
3031 return refcount_bits
;
3034 static int coroutine_fn
3035 qcow2_co_create(BlockdevCreateOptions
*create_options
, Error
**errp
)
3037 BlockdevCreateOptionsQcow2
*qcow2_opts
;
3041 * Open the image file and write a minimal qcow2 header.
3043 * We keep things simple and start with a zero-sized image. We also
3044 * do without refcount blocks or a L1 table for now. We'll fix the
3045 * inconsistency later.
3047 * We do need a refcount table because growing the refcount table means
3048 * allocating two new refcount blocks - the seconds of which would be at
3049 * 2 GB for 64k clusters, and we don't want to have a 2 GB initial file
3050 * size for any qcow2 image.
3052 BlockBackend
*blk
= NULL
;
3053 BlockDriverState
*bs
= NULL
;
3054 BlockDriverState
*data_bs
= NULL
;
3056 size_t cluster_size
;
3059 uint64_t* refcount_table
;
3060 Error
*local_err
= NULL
;
3063 assert(create_options
->driver
== BLOCKDEV_DRIVER_QCOW2
);
3064 qcow2_opts
= &create_options
->u
.qcow2
;
3066 bs
= bdrv_open_blockdev_ref(qcow2_opts
->file
, errp
);
3071 /* Validate options and set default values */
3072 if (!QEMU_IS_ALIGNED(qcow2_opts
->size
, BDRV_SECTOR_SIZE
)) {
3073 error_setg(errp
, "Image size must be a multiple of 512 bytes");
3078 if (qcow2_opts
->has_version
) {
3079 switch (qcow2_opts
->version
) {
3080 case BLOCKDEV_QCOW2_VERSION_V2
:
3083 case BLOCKDEV_QCOW2_VERSION_V3
:
3087 g_assert_not_reached();
3093 if (qcow2_opts
->has_cluster_size
) {
3094 cluster_size
= qcow2_opts
->cluster_size
;
3096 cluster_size
= DEFAULT_CLUSTER_SIZE
;
3099 if (!validate_cluster_size(cluster_size
, errp
)) {
3104 if (!qcow2_opts
->has_preallocation
) {
3105 qcow2_opts
->preallocation
= PREALLOC_MODE_OFF
;
3107 if (qcow2_opts
->has_backing_file
&&
3108 qcow2_opts
->preallocation
!= PREALLOC_MODE_OFF
)
3110 error_setg(errp
, "Backing file and preallocation cannot be used at "
3115 if (qcow2_opts
->has_backing_fmt
&& !qcow2_opts
->has_backing_file
) {
3116 error_setg(errp
, "Backing format cannot be used without backing file");
3121 if (!qcow2_opts
->has_lazy_refcounts
) {
3122 qcow2_opts
->lazy_refcounts
= false;
3124 if (version
< 3 && qcow2_opts
->lazy_refcounts
) {
3125 error_setg(errp
, "Lazy refcounts only supported with compatibility "
3126 "level 1.1 and above (use version=v3 or greater)");
3131 if (!qcow2_opts
->has_refcount_bits
) {
3132 qcow2_opts
->refcount_bits
= 16;
3134 if (qcow2_opts
->refcount_bits
> 64 ||
3135 !is_power_of_2(qcow2_opts
->refcount_bits
))
3137 error_setg(errp
, "Refcount width must be a power of two and may not "
3142 if (version
< 3 && qcow2_opts
->refcount_bits
!= 16) {
3143 error_setg(errp
, "Different refcount widths than 16 bits require "
3144 "compatibility level 1.1 or above (use version=v3 or "
3149 refcount_order
= ctz32(qcow2_opts
->refcount_bits
);
3151 if (qcow2_opts
->data_file_raw
&& !qcow2_opts
->data_file
) {
3152 error_setg(errp
, "data-file-raw requires data-file");
3156 if (qcow2_opts
->data_file_raw
&& qcow2_opts
->has_backing_file
) {
3157 error_setg(errp
, "Backing file and data-file-raw cannot be used at "
3163 if (qcow2_opts
->data_file
) {
3165 error_setg(errp
, "External data files are only supported with "
3166 "compatibility level 1.1 and above (use version=v3 or "
3171 data_bs
= bdrv_open_blockdev_ref(qcow2_opts
->data_file
, errp
);
3172 if (data_bs
== NULL
) {
3178 /* Create BlockBackend to write to the image */
3179 blk
= blk_new(bdrv_get_aio_context(bs
),
3180 BLK_PERM_WRITE
| BLK_PERM_RESIZE
, BLK_PERM_ALL
);
3181 ret
= blk_insert_bs(blk
, bs
, errp
);
3185 blk_set_allow_write_beyond_eof(blk
, true);
3187 /* Clear the protocol layer and preallocate it if necessary */
3188 ret
= blk_truncate(blk
, 0, PREALLOC_MODE_OFF
, errp
);
3193 /* Write the header */
3194 QEMU_BUILD_BUG_ON((1 << MIN_CLUSTER_BITS
) < sizeof(*header
));
3195 header
= g_malloc0(cluster_size
);
3196 *header
= (QCowHeader
) {
3197 .magic
= cpu_to_be32(QCOW_MAGIC
),
3198 .version
= cpu_to_be32(version
),
3199 .cluster_bits
= cpu_to_be32(ctz32(cluster_size
)),
3200 .size
= cpu_to_be64(0),
3201 .l1_table_offset
= cpu_to_be64(0),
3202 .l1_size
= cpu_to_be32(0),
3203 .refcount_table_offset
= cpu_to_be64(cluster_size
),
3204 .refcount_table_clusters
= cpu_to_be32(1),
3205 .refcount_order
= cpu_to_be32(refcount_order
),
3206 .header_length
= cpu_to_be32(sizeof(*header
)),
3209 /* We'll update this to correct value later */
3210 header
->crypt_method
= cpu_to_be32(QCOW_CRYPT_NONE
);
3212 if (qcow2_opts
->lazy_refcounts
) {
3213 header
->compatible_features
|=
3214 cpu_to_be64(QCOW2_COMPAT_LAZY_REFCOUNTS
);
3217 header
->incompatible_features
|=
3218 cpu_to_be64(QCOW2_INCOMPAT_DATA_FILE
);
3220 if (qcow2_opts
->data_file_raw
) {
3221 header
->autoclear_features
|=
3222 cpu_to_be64(QCOW2_AUTOCLEAR_DATA_FILE_RAW
);
3225 ret
= blk_pwrite(blk
, 0, header
, cluster_size
, 0);
3228 error_setg_errno(errp
, -ret
, "Could not write qcow2 header");
3232 /* Write a refcount table with one refcount block */
3233 refcount_table
= g_malloc0(2 * cluster_size
);
3234 refcount_table
[0] = cpu_to_be64(2 * cluster_size
);
3235 ret
= blk_pwrite(blk
, cluster_size
, refcount_table
, 2 * cluster_size
, 0);
3236 g_free(refcount_table
);
3239 error_setg_errno(errp
, -ret
, "Could not write refcount table");
3247 * And now open the image and make it consistent first (i.e. increase the
3248 * refcount of the cluster that is occupied by the header and the refcount
3251 options
= qdict_new();
3252 qdict_put_str(options
, "driver", "qcow2");
3253 qdict_put_str(options
, "file", bs
->node_name
);
3255 qdict_put_str(options
, "data-file", data_bs
->node_name
);
3257 blk
= blk_new_open(NULL
, NULL
, options
,
3258 BDRV_O_RDWR
| BDRV_O_RESIZE
| BDRV_O_NO_FLUSH
,
3261 error_propagate(errp
, local_err
);
3266 ret
= qcow2_alloc_clusters(blk_bs(blk
), 3 * cluster_size
);
3268 error_setg_errno(errp
, -ret
, "Could not allocate clusters for qcow2 "
3269 "header and refcount table");
3272 } else if (ret
!= 0) {
3273 error_report("Huh, first cluster in empty image is already in use?");
3277 /* Set the external data file if necessary */
3279 BDRVQcow2State
*s
= blk_bs(blk
)->opaque
;
3280 s
->image_data_file
= g_strdup(data_bs
->filename
);
3283 /* Create a full header (including things like feature table) */
3284 ret
= qcow2_update_header(blk_bs(blk
));
3286 error_setg_errno(errp
, -ret
, "Could not update qcow2 header");
3290 /* Okay, now that we have a valid image, let's give it the right size */
3291 ret
= blk_truncate(blk
, qcow2_opts
->size
, qcow2_opts
->preallocation
, errp
);
3293 error_prepend(errp
, "Could not resize image: ");
3297 /* Want a backing file? There you go.*/
3298 if (qcow2_opts
->has_backing_file
) {
3299 const char *backing_format
= NULL
;
3301 if (qcow2_opts
->has_backing_fmt
) {
3302 backing_format
= BlockdevDriver_str(qcow2_opts
->backing_fmt
);
3305 ret
= bdrv_change_backing_file(blk_bs(blk
), qcow2_opts
->backing_file
,
3308 error_setg_errno(errp
, -ret
, "Could not assign backing file '%s' "
3309 "with format '%s'", qcow2_opts
->backing_file
,
3315 /* Want encryption? There you go. */
3316 if (qcow2_opts
->has_encrypt
) {
3317 ret
= qcow2_set_up_encryption(blk_bs(blk
), qcow2_opts
->encrypt
, errp
);
3326 /* Reopen the image without BDRV_O_NO_FLUSH to flush it before returning.
3327 * Using BDRV_O_NO_IO, since encryption is now setup we don't want to
3328 * have to setup decryption context. We're not doing any I/O on the top
3329 * level BlockDriverState, only lower layers, where BDRV_O_NO_IO does
3332 options
= qdict_new();
3333 qdict_put_str(options
, "driver", "qcow2");
3334 qdict_put_str(options
, "file", bs
->node_name
);
3336 qdict_put_str(options
, "data-file", data_bs
->node_name
);
3338 blk
= blk_new_open(NULL
, NULL
, options
,
3339 BDRV_O_RDWR
| BDRV_O_NO_BACKING
| BDRV_O_NO_IO
,
3342 error_propagate(errp
, local_err
);
3351 bdrv_unref(data_bs
);
3355 static int coroutine_fn
qcow2_co_create_opts(const char *filename
, QemuOpts
*opts
,
3358 BlockdevCreateOptions
*create_options
= NULL
;
3361 BlockDriverState
*bs
= NULL
;
3362 BlockDriverState
*data_bs
= NULL
;
3363 Error
*local_err
= NULL
;
3367 /* Only the keyval visitor supports the dotted syntax needed for
3368 * encryption, so go through a QDict before getting a QAPI type. Ignore
3369 * options meant for the protocol layer so that the visitor doesn't
3371 qdict
= qemu_opts_to_qdict_filtered(opts
, NULL
, bdrv_qcow2
.create_opts
,
3374 /* Handle encryption options */
3375 val
= qdict_get_try_str(qdict
, BLOCK_OPT_ENCRYPT
);
3376 if (val
&& !strcmp(val
, "on")) {
3377 qdict_put_str(qdict
, BLOCK_OPT_ENCRYPT
, "qcow");
3378 } else if (val
&& !strcmp(val
, "off")) {
3379 qdict_del(qdict
, BLOCK_OPT_ENCRYPT
);
3382 val
= qdict_get_try_str(qdict
, BLOCK_OPT_ENCRYPT_FORMAT
);
3383 if (val
&& !strcmp(val
, "aes")) {
3384 qdict_put_str(qdict
, BLOCK_OPT_ENCRYPT_FORMAT
, "qcow");
3387 /* Convert compat=0.10/1.1 into compat=v2/v3, to be renamed into
3388 * version=v2/v3 below. */
3389 val
= qdict_get_try_str(qdict
, BLOCK_OPT_COMPAT_LEVEL
);
3390 if (val
&& !strcmp(val
, "0.10")) {
3391 qdict_put_str(qdict
, BLOCK_OPT_COMPAT_LEVEL
, "v2");
3392 } else if (val
&& !strcmp(val
, "1.1")) {
3393 qdict_put_str(qdict
, BLOCK_OPT_COMPAT_LEVEL
, "v3");
3396 /* Change legacy command line options into QMP ones */
3397 static const QDictRenames opt_renames
[] = {
3398 { BLOCK_OPT_BACKING_FILE
, "backing-file" },
3399 { BLOCK_OPT_BACKING_FMT
, "backing-fmt" },
3400 { BLOCK_OPT_CLUSTER_SIZE
, "cluster-size" },
3401 { BLOCK_OPT_LAZY_REFCOUNTS
, "lazy-refcounts" },
3402 { BLOCK_OPT_REFCOUNT_BITS
, "refcount-bits" },
3403 { BLOCK_OPT_ENCRYPT
, BLOCK_OPT_ENCRYPT_FORMAT
},
3404 { BLOCK_OPT_COMPAT_LEVEL
, "version" },
3405 { BLOCK_OPT_DATA_FILE_RAW
, "data-file-raw" },
3409 if (!qdict_rename_keys(qdict
, opt_renames
, errp
)) {
3414 /* Create and open the file (protocol layer) */
3415 ret
= bdrv_create_file(filename
, opts
, errp
);
3420 bs
= bdrv_open(filename
, NULL
, NULL
,
3421 BDRV_O_RDWR
| BDRV_O_RESIZE
| BDRV_O_PROTOCOL
, errp
);
3427 /* Create and open an external data file (protocol layer) */
3428 val
= qdict_get_try_str(qdict
, BLOCK_OPT_DATA_FILE
);
3430 ret
= bdrv_create_file(val
, opts
, errp
);
3435 data_bs
= bdrv_open(val
, NULL
, NULL
,
3436 BDRV_O_RDWR
| BDRV_O_RESIZE
| BDRV_O_PROTOCOL
,
3438 if (data_bs
== NULL
) {
3443 qdict_del(qdict
, BLOCK_OPT_DATA_FILE
);
3444 qdict_put_str(qdict
, "data-file", data_bs
->node_name
);
3447 /* Set 'driver' and 'node' options */
3448 qdict_put_str(qdict
, "driver", "qcow2");
3449 qdict_put_str(qdict
, "file", bs
->node_name
);
3451 /* Now get the QAPI type BlockdevCreateOptions */
3452 v
= qobject_input_visitor_new_flat_confused(qdict
, errp
);
3458 visit_type_BlockdevCreateOptions(v
, NULL
, &create_options
, &local_err
);
3462 error_propagate(errp
, local_err
);
3467 /* Silently round up size */
3468 create_options
->u
.qcow2
.size
= ROUND_UP(create_options
->u
.qcow2
.size
,
3471 /* Create the qcow2 image (format layer) */
3472 ret
= qcow2_co_create(create_options
, errp
);
3479 qobject_unref(qdict
);
3481 bdrv_unref(data_bs
);
3482 qapi_free_BlockdevCreateOptions(create_options
);
3487 static bool is_zero(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
)
3492 /* Clamp to image length, before checking status of underlying sectors */
3493 if (offset
+ bytes
> bs
->total_sectors
* BDRV_SECTOR_SIZE
) {
3494 bytes
= bs
->total_sectors
* BDRV_SECTOR_SIZE
- offset
;
3500 res
= bdrv_block_status_above(bs
, NULL
, offset
, bytes
, &nr
, NULL
, NULL
);
3501 return res
>= 0 && (res
& BDRV_BLOCK_ZERO
) && nr
== bytes
;
3504 static coroutine_fn
int qcow2_co_pwrite_zeroes(BlockDriverState
*bs
,
3505 int64_t offset
, int bytes
, BdrvRequestFlags flags
)
3508 BDRVQcow2State
*s
= bs
->opaque
;
3510 uint32_t head
= offset
% s
->cluster_size
;
3511 uint32_t tail
= (offset
+ bytes
) % s
->cluster_size
;
3513 trace_qcow2_pwrite_zeroes_start_req(qemu_coroutine_self(), offset
, bytes
);
3514 if (offset
+ bytes
== bs
->total_sectors
* BDRV_SECTOR_SIZE
) {
3522 assert(head
+ bytes
<= s
->cluster_size
);
3524 /* check whether remainder of cluster already reads as zero */
3525 if (!(is_zero(bs
, offset
- head
, head
) &&
3526 is_zero(bs
, offset
+ bytes
,
3527 tail
? s
->cluster_size
- tail
: 0))) {
3531 qemu_co_mutex_lock(&s
->lock
);
3532 /* We can have new write after previous check */
3533 offset
= QEMU_ALIGN_DOWN(offset
, s
->cluster_size
);
3534 bytes
= s
->cluster_size
;
3535 nr
= s
->cluster_size
;
3536 ret
= qcow2_get_cluster_offset(bs
, offset
, &nr
, &off
);
3537 if (ret
!= QCOW2_CLUSTER_UNALLOCATED
&&
3538 ret
!= QCOW2_CLUSTER_ZERO_PLAIN
&&
3539 ret
!= QCOW2_CLUSTER_ZERO_ALLOC
) {
3540 qemu_co_mutex_unlock(&s
->lock
);
3544 qemu_co_mutex_lock(&s
->lock
);
3547 trace_qcow2_pwrite_zeroes(qemu_coroutine_self(), offset
, bytes
);
3549 /* Whatever is left can use real zero clusters */
3550 ret
= qcow2_cluster_zeroize(bs
, offset
, bytes
, flags
);
3551 qemu_co_mutex_unlock(&s
->lock
);
3556 static coroutine_fn
int qcow2_co_pdiscard(BlockDriverState
*bs
,
3557 int64_t offset
, int bytes
)
3560 BDRVQcow2State
*s
= bs
->opaque
;
3562 if (!QEMU_IS_ALIGNED(offset
| bytes
, s
->cluster_size
)) {
3563 assert(bytes
< s
->cluster_size
);
3564 /* Ignore partial clusters, except for the special case of the
3565 * complete partial cluster at the end of an unaligned file */
3566 if (!QEMU_IS_ALIGNED(offset
, s
->cluster_size
) ||
3567 offset
+ bytes
!= bs
->total_sectors
* BDRV_SECTOR_SIZE
) {
3572 qemu_co_mutex_lock(&s
->lock
);
3573 ret
= qcow2_cluster_discard(bs
, offset
, bytes
, QCOW2_DISCARD_REQUEST
,
3575 qemu_co_mutex_unlock(&s
->lock
);
3579 static int coroutine_fn
3580 qcow2_co_copy_range_from(BlockDriverState
*bs
,
3581 BdrvChild
*src
, uint64_t src_offset
,
3582 BdrvChild
*dst
, uint64_t dst_offset
,
3583 uint64_t bytes
, BdrvRequestFlags read_flags
,
3584 BdrvRequestFlags write_flags
)
3586 BDRVQcow2State
*s
= bs
->opaque
;
3588 unsigned int cur_bytes
; /* number of bytes in current iteration */
3589 BdrvChild
*child
= NULL
;
3590 BdrvRequestFlags cur_write_flags
;
3592 assert(!bs
->encrypted
);
3593 qemu_co_mutex_lock(&s
->lock
);
3595 while (bytes
!= 0) {
3596 uint64_t copy_offset
= 0;
3597 /* prepare next request */
3598 cur_bytes
= MIN(bytes
, INT_MAX
);
3599 cur_write_flags
= write_flags
;
3601 ret
= qcow2_get_cluster_offset(bs
, src_offset
, &cur_bytes
, ©_offset
);
3607 case QCOW2_CLUSTER_UNALLOCATED
:
3608 if (bs
->backing
&& bs
->backing
->bs
) {
3609 int64_t backing_length
= bdrv_getlength(bs
->backing
->bs
);
3610 if (src_offset
>= backing_length
) {
3611 cur_write_flags
|= BDRV_REQ_ZERO_WRITE
;
3613 child
= bs
->backing
;
3614 cur_bytes
= MIN(cur_bytes
, backing_length
- src_offset
);
3615 copy_offset
= src_offset
;
3618 cur_write_flags
|= BDRV_REQ_ZERO_WRITE
;
3622 case QCOW2_CLUSTER_ZERO_PLAIN
:
3623 case QCOW2_CLUSTER_ZERO_ALLOC
:
3624 cur_write_flags
|= BDRV_REQ_ZERO_WRITE
;
3627 case QCOW2_CLUSTER_COMPRESSED
:
3631 case QCOW2_CLUSTER_NORMAL
:
3632 child
= s
->data_file
;
3633 copy_offset
+= offset_into_cluster(s
, src_offset
);
3634 if ((copy_offset
& 511) != 0) {
3643 qemu_co_mutex_unlock(&s
->lock
);
3644 ret
= bdrv_co_copy_range_from(child
,
3647 cur_bytes
, read_flags
, cur_write_flags
);
3648 qemu_co_mutex_lock(&s
->lock
);
3654 src_offset
+= cur_bytes
;
3655 dst_offset
+= cur_bytes
;
3660 qemu_co_mutex_unlock(&s
->lock
);
3664 static int coroutine_fn
3665 qcow2_co_copy_range_to(BlockDriverState
*bs
,
3666 BdrvChild
*src
, uint64_t src_offset
,
3667 BdrvChild
*dst
, uint64_t dst_offset
,
3668 uint64_t bytes
, BdrvRequestFlags read_flags
,
3669 BdrvRequestFlags write_flags
)
3671 BDRVQcow2State
*s
= bs
->opaque
;
3672 int offset_in_cluster
;
3674 unsigned int cur_bytes
; /* number of sectors in current iteration */
3675 uint64_t cluster_offset
;
3676 QCowL2Meta
*l2meta
= NULL
;
3678 assert(!bs
->encrypted
);
3680 qemu_co_mutex_lock(&s
->lock
);
3682 while (bytes
!= 0) {
3686 offset_in_cluster
= offset_into_cluster(s
, dst_offset
);
3687 cur_bytes
= MIN(bytes
, INT_MAX
);
3690 * If src->bs == dst->bs, we could simply copy by incrementing
3691 * the refcnt, without copying user data.
3692 * Or if src->bs == dst->bs->backing->bs, we could copy by discarding. */
3693 ret
= qcow2_alloc_cluster_offset(bs
, dst_offset
, &cur_bytes
,
3694 &cluster_offset
, &l2meta
);
3699 assert((cluster_offset
& 511) == 0);
3701 ret
= qcow2_pre_write_overlap_check(bs
, 0,
3702 cluster_offset
+ offset_in_cluster
, cur_bytes
, true);
3707 qemu_co_mutex_unlock(&s
->lock
);
3708 ret
= bdrv_co_copy_range_to(src
, src_offset
,
3710 cluster_offset
+ offset_in_cluster
,
3711 cur_bytes
, read_flags
, write_flags
);
3712 qemu_co_mutex_lock(&s
->lock
);
3717 ret
= qcow2_handle_l2meta(bs
, &l2meta
, true);
3723 src_offset
+= cur_bytes
;
3724 dst_offset
+= cur_bytes
;
3729 qcow2_handle_l2meta(bs
, &l2meta
, false);
3731 qemu_co_mutex_unlock(&s
->lock
);
3733 trace_qcow2_writev_done_req(qemu_coroutine_self(), ret
);
3738 static int coroutine_fn
qcow2_co_truncate(BlockDriverState
*bs
, int64_t offset
,
3739 PreallocMode prealloc
, Error
**errp
)
3741 BDRVQcow2State
*s
= bs
->opaque
;
3742 uint64_t old_length
;
3743 int64_t new_l1_size
;
3747 if (prealloc
!= PREALLOC_MODE_OFF
&& prealloc
!= PREALLOC_MODE_METADATA
&&
3748 prealloc
!= PREALLOC_MODE_FALLOC
&& prealloc
!= PREALLOC_MODE_FULL
)
3750 error_setg(errp
, "Unsupported preallocation mode '%s'",
3751 PreallocMode_str(prealloc
));
3756 error_setg(errp
, "The new size must be a multiple of 512");
3760 qemu_co_mutex_lock(&s
->lock
);
3762 /* cannot proceed if image has snapshots */
3763 if (s
->nb_snapshots
) {
3764 error_setg(errp
, "Can't resize an image which has snapshots");
3769 /* cannot proceed if image has bitmaps */
3770 if (qcow2_truncate_bitmaps_check(bs
, errp
)) {
3775 old_length
= bs
->total_sectors
* BDRV_SECTOR_SIZE
;
3776 new_l1_size
= size_to_l1(s
, offset
);
3778 if (offset
< old_length
) {
3779 int64_t last_cluster
, old_file_size
;
3780 if (prealloc
!= PREALLOC_MODE_OFF
) {
3782 "Preallocation can't be used for shrinking an image");
3787 ret
= qcow2_cluster_discard(bs
, ROUND_UP(offset
, s
->cluster_size
),
3788 old_length
- ROUND_UP(offset
,
3790 QCOW2_DISCARD_ALWAYS
, true);
3792 error_setg_errno(errp
, -ret
, "Failed to discard cropped clusters");
3796 ret
= qcow2_shrink_l1_table(bs
, new_l1_size
);
3798 error_setg_errno(errp
, -ret
,
3799 "Failed to reduce the number of L2 tables");
3803 ret
= qcow2_shrink_reftable(bs
);
3805 error_setg_errno(errp
, -ret
,
3806 "Failed to discard unused refblocks");
3810 old_file_size
= bdrv_getlength(bs
->file
->bs
);
3811 if (old_file_size
< 0) {
3812 error_setg_errno(errp
, -old_file_size
,
3813 "Failed to inquire current file length");
3814 ret
= old_file_size
;
3817 last_cluster
= qcow2_get_last_cluster(bs
, old_file_size
);
3818 if (last_cluster
< 0) {
3819 error_setg_errno(errp
, -last_cluster
,
3820 "Failed to find the last cluster");
3824 if ((last_cluster
+ 1) * s
->cluster_size
< old_file_size
) {
3825 Error
*local_err
= NULL
;
3827 bdrv_co_truncate(bs
->file
, (last_cluster
+ 1) * s
->cluster_size
,
3828 PREALLOC_MODE_OFF
, &local_err
);
3830 warn_reportf_err(local_err
,
3831 "Failed to truncate the tail of the image: ");
3835 ret
= qcow2_grow_l1_table(bs
, new_l1_size
, true);
3837 error_setg_errno(errp
, -ret
, "Failed to grow the L1 table");
3843 case PREALLOC_MODE_OFF
:
3844 if (has_data_file(bs
)) {
3845 ret
= bdrv_co_truncate(s
->data_file
, offset
, prealloc
, errp
);
3852 case PREALLOC_MODE_METADATA
:
3853 ret
= preallocate_co(bs
, old_length
, offset
, prealloc
, errp
);
3859 case PREALLOC_MODE_FALLOC
:
3860 case PREALLOC_MODE_FULL
:
3862 int64_t allocation_start
, host_offset
, guest_offset
;
3863 int64_t clusters_allocated
;
3864 int64_t old_file_size
, new_file_size
;
3865 uint64_t nb_new_data_clusters
, nb_new_l2_tables
;
3867 /* With a data file, preallocation means just allocating the metadata
3868 * and forwarding the truncate request to the data file */
3869 if (has_data_file(bs
)) {
3870 ret
= preallocate_co(bs
, old_length
, offset
, prealloc
, errp
);
3877 old_file_size
= bdrv_getlength(bs
->file
->bs
);
3878 if (old_file_size
< 0) {
3879 error_setg_errno(errp
, -old_file_size
,
3880 "Failed to inquire current file length");
3881 ret
= old_file_size
;
3884 old_file_size
= ROUND_UP(old_file_size
, s
->cluster_size
);
3886 nb_new_data_clusters
= DIV_ROUND_UP(offset
- old_length
,
3889 /* This is an overestimation; we will not actually allocate space for
3890 * these in the file but just make sure the new refcount structures are
3891 * able to cover them so we will not have to allocate new refblocks
3892 * while entering the data blocks in the potentially new L2 tables.
3893 * (We do not actually care where the L2 tables are placed. Maybe they
3894 * are already allocated or they can be placed somewhere before
3895 * @old_file_size. It does not matter because they will be fully
3896 * allocated automatically, so they do not need to be covered by the
3897 * preallocation. All that matters is that we will not have to allocate
3898 * new refcount structures for them.) */
3899 nb_new_l2_tables
= DIV_ROUND_UP(nb_new_data_clusters
,
3900 s
->cluster_size
/ sizeof(uint64_t));
3901 /* The cluster range may not be aligned to L2 boundaries, so add one L2
3902 * table for a potential head/tail */
3905 allocation_start
= qcow2_refcount_area(bs
, old_file_size
,
3906 nb_new_data_clusters
+
3909 if (allocation_start
< 0) {
3910 error_setg_errno(errp
, -allocation_start
,
3911 "Failed to resize refcount structures");
3912 ret
= allocation_start
;
3916 clusters_allocated
= qcow2_alloc_clusters_at(bs
, allocation_start
,
3917 nb_new_data_clusters
);
3918 if (clusters_allocated
< 0) {
3919 error_setg_errno(errp
, -clusters_allocated
,
3920 "Failed to allocate data clusters");
3921 ret
= clusters_allocated
;
3925 assert(clusters_allocated
== nb_new_data_clusters
);
3927 /* Allocate the data area */
3928 new_file_size
= allocation_start
+
3929 nb_new_data_clusters
* s
->cluster_size
;
3930 ret
= bdrv_co_truncate(bs
->file
, new_file_size
, prealloc
, errp
);
3932 error_prepend(errp
, "Failed to resize underlying file: ");
3933 qcow2_free_clusters(bs
, allocation_start
,
3934 nb_new_data_clusters
* s
->cluster_size
,
3935 QCOW2_DISCARD_OTHER
);
3939 /* Create the necessary L2 entries */
3940 host_offset
= allocation_start
;
3941 guest_offset
= old_length
;
3942 while (nb_new_data_clusters
) {
3943 int64_t nb_clusters
= MIN(
3944 nb_new_data_clusters
,
3945 s
->l2_slice_size
- offset_to_l2_slice_index(s
, guest_offset
));
3946 QCowL2Meta allocation
= {
3947 .offset
= guest_offset
,
3948 .alloc_offset
= host_offset
,
3949 .nb_clusters
= nb_clusters
,
3951 qemu_co_queue_init(&allocation
.dependent_requests
);
3953 ret
= qcow2_alloc_cluster_link_l2(bs
, &allocation
);
3955 error_setg_errno(errp
, -ret
, "Failed to update L2 tables");
3956 qcow2_free_clusters(bs
, host_offset
,
3957 nb_new_data_clusters
* s
->cluster_size
,
3958 QCOW2_DISCARD_OTHER
);
3962 guest_offset
+= nb_clusters
* s
->cluster_size
;
3963 host_offset
+= nb_clusters
* s
->cluster_size
;
3964 nb_new_data_clusters
-= nb_clusters
;
3970 g_assert_not_reached();
3973 if (prealloc
!= PREALLOC_MODE_OFF
) {
3974 /* Flush metadata before actually changing the image size */
3975 ret
= qcow2_write_caches(bs
);
3977 error_setg_errno(errp
, -ret
,
3978 "Failed to flush the preallocated area to disk");
3983 bs
->total_sectors
= offset
/ BDRV_SECTOR_SIZE
;
3985 /* write updated header.size */
3986 offset
= cpu_to_be64(offset
);
3987 ret
= bdrv_pwrite_sync(bs
->file
, offsetof(QCowHeader
, size
),
3988 &offset
, sizeof(uint64_t));
3990 error_setg_errno(errp
, -ret
, "Failed to update the image size");
3994 s
->l1_vm_state_index
= new_l1_size
;
3996 /* Update cache sizes */
3997 options
= qdict_clone_shallow(bs
->options
);
3998 ret
= qcow2_update_options(bs
, options
, s
->flags
, errp
);
3999 qobject_unref(options
);
4005 qemu_co_mutex_unlock(&s
->lock
);
4009 /* XXX: put compressed sectors first, then all the cluster aligned
4010 tables to avoid losing bytes in alignment */
4011 static coroutine_fn
int
4012 qcow2_co_pwritev_compressed(BlockDriverState
*bs
, uint64_t offset
,
4013 uint64_t bytes
, QEMUIOVector
*qiov
)
4015 BDRVQcow2State
*s
= bs
->opaque
;
4018 uint8_t *buf
, *out_buf
;
4019 uint64_t cluster_offset
;
4021 if (has_data_file(bs
)) {
4026 /* align end of file to a sector boundary to ease reading with
4027 sector based I/Os */
4028 int64_t len
= bdrv_getlength(bs
->file
->bs
);
4032 return bdrv_co_truncate(bs
->file
, len
, PREALLOC_MODE_OFF
, NULL
);
4035 if (offset_into_cluster(s
, offset
)) {
4039 buf
= qemu_blockalign(bs
, s
->cluster_size
);
4040 if (bytes
!= s
->cluster_size
) {
4041 if (bytes
> s
->cluster_size
||
4042 offset
+ bytes
!= bs
->total_sectors
<< BDRV_SECTOR_BITS
)
4047 /* Zero-pad last write if image size is not cluster aligned */
4048 memset(buf
+ bytes
, 0, s
->cluster_size
- bytes
);
4050 qemu_iovec_to_buf(qiov
, 0, buf
, bytes
);
4052 out_buf
= g_malloc(s
->cluster_size
);
4054 out_len
= qcow2_co_compress(bs
, out_buf
, s
->cluster_size
- 1,
4055 buf
, s
->cluster_size
);
4056 if (out_len
== -ENOMEM
) {
4057 /* could not compress: write normal cluster */
4058 ret
= qcow2_co_pwritev(bs
, offset
, bytes
, qiov
, 0);
4063 } else if (out_len
< 0) {
4068 qemu_co_mutex_lock(&s
->lock
);
4069 ret
= qcow2_alloc_compressed_cluster_offset(bs
, offset
, out_len
,
4072 qemu_co_mutex_unlock(&s
->lock
);
4076 ret
= qcow2_pre_write_overlap_check(bs
, 0, cluster_offset
, out_len
, true);
4077 qemu_co_mutex_unlock(&s
->lock
);
4082 BLKDBG_EVENT(s
->data_file
, BLKDBG_WRITE_COMPRESSED
);
4083 ret
= bdrv_co_pwrite(s
->data_file
, cluster_offset
, out_len
, out_buf
, 0);
4095 static int coroutine_fn
4096 qcow2_co_preadv_compressed(BlockDriverState
*bs
,
4097 uint64_t file_cluster_offset
,
4102 BDRVQcow2State
*s
= bs
->opaque
;
4103 int ret
= 0, csize
, nb_csectors
;
4105 uint8_t *buf
, *out_buf
;
4106 int offset_in_cluster
= offset_into_cluster(s
, offset
);
4108 coffset
= file_cluster_offset
& s
->cluster_offset_mask
;
4109 nb_csectors
= ((file_cluster_offset
>> s
->csize_shift
) & s
->csize_mask
) + 1;
4110 csize
= nb_csectors
* QCOW2_COMPRESSED_SECTOR_SIZE
-
4111 (coffset
& ~QCOW2_COMPRESSED_SECTOR_MASK
);
4113 buf
= g_try_malloc(csize
);
4118 out_buf
= qemu_blockalign(bs
, s
->cluster_size
);
4120 BLKDBG_EVENT(bs
->file
, BLKDBG_READ_COMPRESSED
);
4121 ret
= bdrv_co_pread(bs
->file
, coffset
, csize
, buf
, 0);
4126 if (qcow2_co_decompress(bs
, out_buf
, s
->cluster_size
, buf
, csize
) < 0) {
4131 qemu_iovec_from_buf(qiov
, 0, out_buf
+ offset_in_cluster
, bytes
);
4134 qemu_vfree(out_buf
);
4140 static int make_completely_empty(BlockDriverState
*bs
)
4142 BDRVQcow2State
*s
= bs
->opaque
;
4143 Error
*local_err
= NULL
;
4144 int ret
, l1_clusters
;
4146 uint64_t *new_reftable
= NULL
;
4147 uint64_t rt_entry
, l1_size2
;
4150 uint64_t reftable_offset
;
4151 uint32_t reftable_clusters
;
4152 } QEMU_PACKED l1_ofs_rt_ofs_cls
;
4154 ret
= qcow2_cache_empty(bs
, s
->l2_table_cache
);
4159 ret
= qcow2_cache_empty(bs
, s
->refcount_block_cache
);
4164 /* Refcounts will be broken utterly */
4165 ret
= qcow2_mark_dirty(bs
);
4170 BLKDBG_EVENT(bs
->file
, BLKDBG_L1_UPDATE
);
4172 l1_clusters
= DIV_ROUND_UP(s
->l1_size
, s
->cluster_size
/ sizeof(uint64_t));
4173 l1_size2
= (uint64_t)s
->l1_size
* sizeof(uint64_t);
4175 /* After this call, neither the in-memory nor the on-disk refcount
4176 * information accurately describe the actual references */
4178 ret
= bdrv_pwrite_zeroes(bs
->file
, s
->l1_table_offset
,
4179 l1_clusters
* s
->cluster_size
, 0);
4181 goto fail_broken_refcounts
;
4183 memset(s
->l1_table
, 0, l1_size2
);
4185 BLKDBG_EVENT(bs
->file
, BLKDBG_EMPTY_IMAGE_PREPARE
);
4187 /* Overwrite enough clusters at the beginning of the sectors to place
4188 * the refcount table, a refcount block and the L1 table in; this may
4189 * overwrite parts of the existing refcount and L1 table, which is not
4190 * an issue because the dirty flag is set, complete data loss is in fact
4191 * desired and partial data loss is consequently fine as well */
4192 ret
= bdrv_pwrite_zeroes(bs
->file
, s
->cluster_size
,
4193 (2 + l1_clusters
) * s
->cluster_size
, 0);
4194 /* This call (even if it failed overall) may have overwritten on-disk
4195 * refcount structures; in that case, the in-memory refcount information
4196 * will probably differ from the on-disk information which makes the BDS
4199 goto fail_broken_refcounts
;
4202 BLKDBG_EVENT(bs
->file
, BLKDBG_L1_UPDATE
);
4203 BLKDBG_EVENT(bs
->file
, BLKDBG_REFTABLE_UPDATE
);
4205 /* "Create" an empty reftable (one cluster) directly after the image
4206 * header and an empty L1 table three clusters after the image header;
4207 * the cluster between those two will be used as the first refblock */
4208 l1_ofs_rt_ofs_cls
.l1_offset
= cpu_to_be64(3 * s
->cluster_size
);
4209 l1_ofs_rt_ofs_cls
.reftable_offset
= cpu_to_be64(s
->cluster_size
);
4210 l1_ofs_rt_ofs_cls
.reftable_clusters
= cpu_to_be32(1);
4211 ret
= bdrv_pwrite_sync(bs
->file
, offsetof(QCowHeader
, l1_table_offset
),
4212 &l1_ofs_rt_ofs_cls
, sizeof(l1_ofs_rt_ofs_cls
));
4214 goto fail_broken_refcounts
;
4217 s
->l1_table_offset
= 3 * s
->cluster_size
;
4219 new_reftable
= g_try_new0(uint64_t, s
->cluster_size
/ sizeof(uint64_t));
4220 if (!new_reftable
) {
4222 goto fail_broken_refcounts
;
4225 s
->refcount_table_offset
= s
->cluster_size
;
4226 s
->refcount_table_size
= s
->cluster_size
/ sizeof(uint64_t);
4227 s
->max_refcount_table_index
= 0;
4229 g_free(s
->refcount_table
);
4230 s
->refcount_table
= new_reftable
;
4231 new_reftable
= NULL
;
4233 /* Now the in-memory refcount information again corresponds to the on-disk
4234 * information (reftable is empty and no refblocks (the refblock cache is
4235 * empty)); however, this means some clusters (e.g. the image header) are
4236 * referenced, but not refcounted, but the normal qcow2 code assumes that
4237 * the in-memory information is always correct */
4239 BLKDBG_EVENT(bs
->file
, BLKDBG_REFBLOCK_ALLOC
);
4241 /* Enter the first refblock into the reftable */
4242 rt_entry
= cpu_to_be64(2 * s
->cluster_size
);
4243 ret
= bdrv_pwrite_sync(bs
->file
, s
->cluster_size
,
4244 &rt_entry
, sizeof(rt_entry
));
4246 goto fail_broken_refcounts
;
4248 s
->refcount_table
[0] = 2 * s
->cluster_size
;
4250 s
->free_cluster_index
= 0;
4251 assert(3 + l1_clusters
<= s
->refcount_block_size
);
4252 offset
= qcow2_alloc_clusters(bs
, 3 * s
->cluster_size
+ l1_size2
);
4255 goto fail_broken_refcounts
;
4256 } else if (offset
> 0) {
4257 error_report("First cluster in emptied image is in use");
4261 /* Now finally the in-memory information corresponds to the on-disk
4262 * structures and is correct */
4263 ret
= qcow2_mark_clean(bs
);
4268 ret
= bdrv_truncate(bs
->file
, (3 + l1_clusters
) * s
->cluster_size
,
4269 PREALLOC_MODE_OFF
, &local_err
);
4271 error_report_err(local_err
);
4277 fail_broken_refcounts
:
4278 /* The BDS is unusable at this point. If we wanted to make it usable, we
4279 * would have to call qcow2_refcount_close(), qcow2_refcount_init(),
4280 * qcow2_check_refcounts(), qcow2_refcount_close() and qcow2_refcount_init()
4281 * again. However, because the functions which could have caused this error
4282 * path to be taken are used by those functions as well, it's very likely
4283 * that that sequence will fail as well. Therefore, just eject the BDS. */
4287 g_free(new_reftable
);
4291 static int qcow2_make_empty(BlockDriverState
*bs
)
4293 BDRVQcow2State
*s
= bs
->opaque
;
4294 uint64_t offset
, end_offset
;
4295 int step
= QEMU_ALIGN_DOWN(INT_MAX
, s
->cluster_size
);
4296 int l1_clusters
, ret
= 0;
4298 l1_clusters
= DIV_ROUND_UP(s
->l1_size
, s
->cluster_size
/ sizeof(uint64_t));
4300 if (s
->qcow_version
>= 3 && !s
->snapshots
&& !s
->nb_bitmaps
&&
4301 3 + l1_clusters
<= s
->refcount_block_size
&&
4302 s
->crypt_method_header
!= QCOW_CRYPT_LUKS
&&
4303 !has_data_file(bs
)) {
4304 /* The following function only works for qcow2 v3 images (it
4305 * requires the dirty flag) and only as long as there are no
4306 * features that reserve extra clusters (such as snapshots,
4307 * LUKS header, or persistent bitmaps), because it completely
4308 * empties the image. Furthermore, the L1 table and three
4309 * additional clusters (image header, refcount table, one
4310 * refcount block) have to fit inside one refcount block. It
4311 * only resets the image file, i.e. does not work with an
4312 * external data file. */
4313 return make_completely_empty(bs
);
4316 /* This fallback code simply discards every active cluster; this is slow,
4317 * but works in all cases */
4318 end_offset
= bs
->total_sectors
* BDRV_SECTOR_SIZE
;
4319 for (offset
= 0; offset
< end_offset
; offset
+= step
) {
4320 /* As this function is generally used after committing an external
4321 * snapshot, QCOW2_DISCARD_SNAPSHOT seems appropriate. Also, the
4322 * default action for this kind of discard is to pass the discard,
4323 * which will ideally result in an actually smaller image file, as
4324 * is probably desired. */
4325 ret
= qcow2_cluster_discard(bs
, offset
, MIN(step
, end_offset
- offset
),
4326 QCOW2_DISCARD_SNAPSHOT
, true);
4335 static coroutine_fn
int qcow2_co_flush_to_os(BlockDriverState
*bs
)
4337 BDRVQcow2State
*s
= bs
->opaque
;
4340 qemu_co_mutex_lock(&s
->lock
);
4341 ret
= qcow2_write_caches(bs
);
4342 qemu_co_mutex_unlock(&s
->lock
);
4347 static ssize_t
qcow2_measure_crypto_hdr_init_func(QCryptoBlock
*block
,
4348 size_t headerlen
, void *opaque
, Error
**errp
)
4350 size_t *headerlenp
= opaque
;
4352 /* Stash away the payload size */
4353 *headerlenp
= headerlen
;
4357 static ssize_t
qcow2_measure_crypto_hdr_write_func(QCryptoBlock
*block
,
4358 size_t offset
, const uint8_t *buf
, size_t buflen
,
4359 void *opaque
, Error
**errp
)
4361 /* Discard the bytes, we're not actually writing to an image */
4365 /* Determine the number of bytes for the LUKS payload */
4366 static bool qcow2_measure_luks_headerlen(QemuOpts
*opts
, size_t *len
,
4370 QDict
*cryptoopts_qdict
;
4371 QCryptoBlockCreateOptions
*cryptoopts
;
4372 QCryptoBlock
*crypto
;
4374 /* Extract "encrypt." options into a qdict */
4375 opts_qdict
= qemu_opts_to_qdict(opts
, NULL
);
4376 qdict_extract_subqdict(opts_qdict
, &cryptoopts_qdict
, "encrypt.");
4377 qobject_unref(opts_qdict
);
4379 /* Build QCryptoBlockCreateOptions object from qdict */
4380 qdict_put_str(cryptoopts_qdict
, "format", "luks");
4381 cryptoopts
= block_crypto_create_opts_init(cryptoopts_qdict
, errp
);
4382 qobject_unref(cryptoopts_qdict
);
4387 /* Fake LUKS creation in order to determine the payload size */
4388 crypto
= qcrypto_block_create(cryptoopts
, "encrypt.",
4389 qcow2_measure_crypto_hdr_init_func
,
4390 qcow2_measure_crypto_hdr_write_func
,
4392 qapi_free_QCryptoBlockCreateOptions(cryptoopts
);
4397 qcrypto_block_free(crypto
);
4401 static BlockMeasureInfo
*qcow2_measure(QemuOpts
*opts
, BlockDriverState
*in_bs
,
4404 Error
*local_err
= NULL
;
4405 BlockMeasureInfo
*info
;
4406 uint64_t required
= 0; /* bytes that contribute to required size */
4407 uint64_t virtual_size
; /* disk size as seen by guest */
4408 uint64_t refcount_bits
;
4410 uint64_t luks_payload_size
= 0;
4411 size_t cluster_size
;
4414 PreallocMode prealloc
;
4415 bool has_backing_file
;
4418 /* Parse image creation options */
4419 cluster_size
= qcow2_opt_get_cluster_size_del(opts
, &local_err
);
4424 version
= qcow2_opt_get_version_del(opts
, &local_err
);
4429 refcount_bits
= qcow2_opt_get_refcount_bits_del(opts
, version
, &local_err
);
4434 optstr
= qemu_opt_get_del(opts
, BLOCK_OPT_PREALLOC
);
4435 prealloc
= qapi_enum_parse(&PreallocMode_lookup
, optstr
,
4436 PREALLOC_MODE_OFF
, &local_err
);
4442 optstr
= qemu_opt_get_del(opts
, BLOCK_OPT_BACKING_FILE
);
4443 has_backing_file
= !!optstr
;
4446 optstr
= qemu_opt_get_del(opts
, BLOCK_OPT_ENCRYPT_FORMAT
);
4447 has_luks
= optstr
&& strcmp(optstr
, "luks") == 0;
4453 if (!qcow2_measure_luks_headerlen(opts
, &headerlen
, &local_err
)) {
4457 luks_payload_size
= ROUND_UP(headerlen
, cluster_size
);
4460 virtual_size
= qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0);
4461 virtual_size
= ROUND_UP(virtual_size
, cluster_size
);
4463 /* Check that virtual disk size is valid */
4464 l2_tables
= DIV_ROUND_UP(virtual_size
/ cluster_size
,
4465 cluster_size
/ sizeof(uint64_t));
4466 if (l2_tables
* sizeof(uint64_t) > QCOW_MAX_L1_SIZE
) {
4467 error_setg(&local_err
, "The image size is too large "
4468 "(try using a larger cluster size)");
4472 /* Account for input image */
4474 int64_t ssize
= bdrv_getlength(in_bs
);
4476 error_setg_errno(&local_err
, -ssize
,
4477 "Unable to get image virtual_size");
4481 virtual_size
= ROUND_UP(ssize
, cluster_size
);
4483 if (has_backing_file
) {
4484 /* We don't how much of the backing chain is shared by the input
4485 * image and the new image file. In the worst case the new image's
4486 * backing file has nothing in common with the input image. Be
4487 * conservative and assume all clusters need to be written.
4489 required
= virtual_size
;
4494 for (offset
= 0; offset
< ssize
; offset
+= pnum
) {
4497 ret
= bdrv_block_status_above(in_bs
, NULL
, offset
,
4498 ssize
- offset
, &pnum
, NULL
,
4501 error_setg_errno(&local_err
, -ret
,
4502 "Unable to get block status");
4506 if (ret
& BDRV_BLOCK_ZERO
) {
4507 /* Skip zero regions (safe with no backing file) */
4508 } else if ((ret
& (BDRV_BLOCK_DATA
| BDRV_BLOCK_ALLOCATED
)) ==
4509 (BDRV_BLOCK_DATA
| BDRV_BLOCK_ALLOCATED
)) {
4510 /* Extend pnum to end of cluster for next iteration */
4511 pnum
= ROUND_UP(offset
+ pnum
, cluster_size
) - offset
;
4513 /* Count clusters we've seen */
4514 required
+= offset
% cluster_size
+ pnum
;
4520 /* Take into account preallocation. Nothing special is needed for
4521 * PREALLOC_MODE_METADATA since metadata is always counted.
4523 if (prealloc
== PREALLOC_MODE_FULL
|| prealloc
== PREALLOC_MODE_FALLOC
) {
4524 required
= virtual_size
;
4527 info
= g_new(BlockMeasureInfo
, 1);
4528 info
->fully_allocated
=
4529 qcow2_calc_prealloc_size(virtual_size
, cluster_size
,
4530 ctz32(refcount_bits
)) + luks_payload_size
;
4532 /* Remove data clusters that are not required. This overestimates the
4533 * required size because metadata needed for the fully allocated file is
4536 info
->required
= info
->fully_allocated
- virtual_size
+ required
;
4540 error_propagate(errp
, local_err
);
4544 static int qcow2_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
4546 BDRVQcow2State
*s
= bs
->opaque
;
4547 bdi
->unallocated_blocks_are_zero
= true;
4548 bdi
->cluster_size
= s
->cluster_size
;
4549 bdi
->vm_state_offset
= qcow2_vm_state_offset(s
);
4553 static ImageInfoSpecific
*qcow2_get_specific_info(BlockDriverState
*bs
,
4556 BDRVQcow2State
*s
= bs
->opaque
;
4557 ImageInfoSpecific
*spec_info
;
4558 QCryptoBlockInfo
*encrypt_info
= NULL
;
4559 Error
*local_err
= NULL
;
4561 if (s
->crypto
!= NULL
) {
4562 encrypt_info
= qcrypto_block_get_info(s
->crypto
, &local_err
);
4564 error_propagate(errp
, local_err
);
4569 spec_info
= g_new(ImageInfoSpecific
, 1);
4570 *spec_info
= (ImageInfoSpecific
){
4571 .type
= IMAGE_INFO_SPECIFIC_KIND_QCOW2
,
4572 .u
.qcow2
.data
= g_new0(ImageInfoSpecificQCow2
, 1),
4574 if (s
->qcow_version
== 2) {
4575 *spec_info
->u
.qcow2
.data
= (ImageInfoSpecificQCow2
){
4576 .compat
= g_strdup("0.10"),
4577 .refcount_bits
= s
->refcount_bits
,
4579 } else if (s
->qcow_version
== 3) {
4580 Qcow2BitmapInfoList
*bitmaps
;
4581 bitmaps
= qcow2_get_bitmap_info_list(bs
, &local_err
);
4583 error_propagate(errp
, local_err
);
4584 qapi_free_ImageInfoSpecific(spec_info
);
4587 *spec_info
->u
.qcow2
.data
= (ImageInfoSpecificQCow2
){
4588 .compat
= g_strdup("1.1"),
4589 .lazy_refcounts
= s
->compatible_features
&
4590 QCOW2_COMPAT_LAZY_REFCOUNTS
,
4591 .has_lazy_refcounts
= true,
4592 .corrupt
= s
->incompatible_features
&
4593 QCOW2_INCOMPAT_CORRUPT
,
4594 .has_corrupt
= true,
4595 .refcount_bits
= s
->refcount_bits
,
4596 .has_bitmaps
= !!bitmaps
,
4598 .has_data_file
= !!s
->image_data_file
,
4599 .data_file
= g_strdup(s
->image_data_file
),
4600 .has_data_file_raw
= has_data_file(bs
),
4601 .data_file_raw
= data_file_is_raw(bs
),
4604 /* if this assertion fails, this probably means a new version was
4605 * added without having it covered here */
4610 ImageInfoSpecificQCow2Encryption
*qencrypt
=
4611 g_new(ImageInfoSpecificQCow2Encryption
, 1);
4612 switch (encrypt_info
->format
) {
4613 case Q_CRYPTO_BLOCK_FORMAT_QCOW
:
4614 qencrypt
->format
= BLOCKDEV_QCOW2_ENCRYPTION_FORMAT_AES
;
4616 case Q_CRYPTO_BLOCK_FORMAT_LUKS
:
4617 qencrypt
->format
= BLOCKDEV_QCOW2_ENCRYPTION_FORMAT_LUKS
;
4618 qencrypt
->u
.luks
= encrypt_info
->u
.luks
;
4623 /* Since we did shallow copy above, erase any pointers
4624 * in the original info */
4625 memset(&encrypt_info
->u
, 0, sizeof(encrypt_info
->u
));
4626 qapi_free_QCryptoBlockInfo(encrypt_info
);
4628 spec_info
->u
.qcow2
.data
->has_encrypt
= true;
4629 spec_info
->u
.qcow2
.data
->encrypt
= qencrypt
;
4635 static int qcow2_save_vmstate(BlockDriverState
*bs
, QEMUIOVector
*qiov
,
4638 BDRVQcow2State
*s
= bs
->opaque
;
4640 BLKDBG_EVENT(bs
->file
, BLKDBG_VMSTATE_SAVE
);
4641 return bs
->drv
->bdrv_co_pwritev(bs
, qcow2_vm_state_offset(s
) + pos
,
4642 qiov
->size
, qiov
, 0);
4645 static int qcow2_load_vmstate(BlockDriverState
*bs
, QEMUIOVector
*qiov
,
4648 BDRVQcow2State
*s
= bs
->opaque
;
4650 BLKDBG_EVENT(bs
->file
, BLKDBG_VMSTATE_LOAD
);
4651 return bs
->drv
->bdrv_co_preadv(bs
, qcow2_vm_state_offset(s
) + pos
,
4652 qiov
->size
, qiov
, 0);
4656 * Downgrades an image's version. To achieve this, any incompatible features
4657 * have to be removed.
4659 static int qcow2_downgrade(BlockDriverState
*bs
, int target_version
,
4660 BlockDriverAmendStatusCB
*status_cb
, void *cb_opaque
,
4663 BDRVQcow2State
*s
= bs
->opaque
;
4664 int current_version
= s
->qcow_version
;
4667 /* This is qcow2_downgrade(), not qcow2_upgrade() */
4668 assert(target_version
< current_version
);
4670 /* There are no other versions (now) that you can downgrade to */
4671 assert(target_version
== 2);
4673 if (s
->refcount_order
!= 4) {
4674 error_setg(errp
, "compat=0.10 requires refcount_bits=16");
4678 if (has_data_file(bs
)) {
4679 error_setg(errp
, "Cannot downgrade an image with a data file");
4683 /* clear incompatible features */
4684 if (s
->incompatible_features
& QCOW2_INCOMPAT_DIRTY
) {
4685 ret
= qcow2_mark_clean(bs
);
4687 error_setg_errno(errp
, -ret
, "Failed to make the image clean");
4692 /* with QCOW2_INCOMPAT_CORRUPT, it is pretty much impossible to get here in
4693 * the first place; if that happens nonetheless, returning -ENOTSUP is the
4694 * best thing to do anyway */
4696 if (s
->incompatible_features
) {
4697 error_setg(errp
, "Cannot downgrade an image with incompatible features "
4698 "%#" PRIx64
" set", s
->incompatible_features
);
4702 /* since we can ignore compatible features, we can set them to 0 as well */
4703 s
->compatible_features
= 0;
4704 /* if lazy refcounts have been used, they have already been fixed through
4705 * clearing the dirty flag */
4707 /* clearing autoclear features is trivial */
4708 s
->autoclear_features
= 0;
4710 ret
= qcow2_expand_zero_clusters(bs
, status_cb
, cb_opaque
);
4712 error_setg_errno(errp
, -ret
, "Failed to turn zero into data clusters");
4716 s
->qcow_version
= target_version
;
4717 ret
= qcow2_update_header(bs
);
4719 s
->qcow_version
= current_version
;
4720 error_setg_errno(errp
, -ret
, "Failed to update the image header");
4726 typedef enum Qcow2AmendOperation
{
4727 /* This is the value Qcow2AmendHelperCBInfo::last_operation will be
4728 * statically initialized to so that the helper CB can discern the first
4729 * invocation from an operation change */
4730 QCOW2_NO_OPERATION
= 0,
4732 QCOW2_CHANGING_REFCOUNT_ORDER
,
4734 } Qcow2AmendOperation
;
4736 typedef struct Qcow2AmendHelperCBInfo
{
4737 /* The code coordinating the amend operations should only modify
4738 * these four fields; the rest will be managed by the CB */
4739 BlockDriverAmendStatusCB
*original_status_cb
;
4740 void *original_cb_opaque
;
4742 Qcow2AmendOperation current_operation
;
4744 /* Total number of operations to perform (only set once) */
4745 int total_operations
;
4747 /* The following fields are managed by the CB */
4749 /* Number of operations completed */
4750 int operations_completed
;
4752 /* Cumulative offset of all completed operations */
4753 int64_t offset_completed
;
4755 Qcow2AmendOperation last_operation
;
4756 int64_t last_work_size
;
4757 } Qcow2AmendHelperCBInfo
;
4759 static void qcow2_amend_helper_cb(BlockDriverState
*bs
,
4760 int64_t operation_offset
,
4761 int64_t operation_work_size
, void *opaque
)
4763 Qcow2AmendHelperCBInfo
*info
= opaque
;
4764 int64_t current_work_size
;
4765 int64_t projected_work_size
;
4767 if (info
->current_operation
!= info
->last_operation
) {
4768 if (info
->last_operation
!= QCOW2_NO_OPERATION
) {
4769 info
->offset_completed
+= info
->last_work_size
;
4770 info
->operations_completed
++;
4773 info
->last_operation
= info
->current_operation
;
4776 assert(info
->total_operations
> 0);
4777 assert(info
->operations_completed
< info
->total_operations
);
4779 info
->last_work_size
= operation_work_size
;
4781 current_work_size
= info
->offset_completed
+ operation_work_size
;
4783 /* current_work_size is the total work size for (operations_completed + 1)
4784 * operations (which includes this one), so multiply it by the number of
4785 * operations not covered and divide it by the number of operations
4786 * covered to get a projection for the operations not covered */
4787 projected_work_size
= current_work_size
* (info
->total_operations
-
4788 info
->operations_completed
- 1)
4789 / (info
->operations_completed
+ 1);
4791 info
->original_status_cb(bs
, info
->offset_completed
+ operation_offset
,
4792 current_work_size
+ projected_work_size
,
4793 info
->original_cb_opaque
);
4796 static int qcow2_amend_options(BlockDriverState
*bs
, QemuOpts
*opts
,
4797 BlockDriverAmendStatusCB
*status_cb
,
4801 BDRVQcow2State
*s
= bs
->opaque
;
4802 int old_version
= s
->qcow_version
, new_version
= old_version
;
4803 uint64_t new_size
= 0;
4804 const char *backing_file
= NULL
, *backing_format
= NULL
, *data_file
= NULL
;
4805 bool lazy_refcounts
= s
->use_lazy_refcounts
;
4806 bool data_file_raw
= data_file_is_raw(bs
);
4807 const char *compat
= NULL
;
4808 uint64_t cluster_size
= s
->cluster_size
;
4811 int refcount_bits
= s
->refcount_bits
;
4813 QemuOptDesc
*desc
= opts
->list
->desc
;
4814 Qcow2AmendHelperCBInfo helper_cb_info
;
4816 while (desc
&& desc
->name
) {
4817 if (!qemu_opt_find(opts
, desc
->name
)) {
4818 /* only change explicitly defined options */
4823 if (!strcmp(desc
->name
, BLOCK_OPT_COMPAT_LEVEL
)) {
4824 compat
= qemu_opt_get(opts
, BLOCK_OPT_COMPAT_LEVEL
);
4826 /* preserve default */
4827 } else if (!strcmp(compat
, "0.10") || !strcmp(compat
, "v2")) {
4829 } else if (!strcmp(compat
, "1.1") || !strcmp(compat
, "v3")) {
4832 error_setg(errp
, "Unknown compatibility level %s", compat
);
4835 } else if (!strcmp(desc
->name
, BLOCK_OPT_PREALLOC
)) {
4836 error_setg(errp
, "Cannot change preallocation mode");
4838 } else if (!strcmp(desc
->name
, BLOCK_OPT_SIZE
)) {
4839 new_size
= qemu_opt_get_size(opts
, BLOCK_OPT_SIZE
, 0);
4840 } else if (!strcmp(desc
->name
, BLOCK_OPT_BACKING_FILE
)) {
4841 backing_file
= qemu_opt_get(opts
, BLOCK_OPT_BACKING_FILE
);
4842 } else if (!strcmp(desc
->name
, BLOCK_OPT_BACKING_FMT
)) {
4843 backing_format
= qemu_opt_get(opts
, BLOCK_OPT_BACKING_FMT
);
4844 } else if (!strcmp(desc
->name
, BLOCK_OPT_ENCRYPT
)) {
4845 encrypt
= qemu_opt_get_bool(opts
, BLOCK_OPT_ENCRYPT
,
4848 if (encrypt
!= !!s
->crypto
) {
4850 "Changing the encryption flag is not supported");
4853 } else if (!strcmp(desc
->name
, BLOCK_OPT_ENCRYPT_FORMAT
)) {
4854 encformat
= qcow2_crypt_method_from_format(
4855 qemu_opt_get(opts
, BLOCK_OPT_ENCRYPT_FORMAT
));
4857 if (encformat
!= s
->crypt_method_header
) {
4859 "Changing the encryption format is not supported");
4862 } else if (g_str_has_prefix(desc
->name
, "encrypt.")) {
4864 "Changing the encryption parameters is not supported");
4866 } else if (!strcmp(desc
->name
, BLOCK_OPT_CLUSTER_SIZE
)) {
4867 cluster_size
= qemu_opt_get_size(opts
, BLOCK_OPT_CLUSTER_SIZE
,
4869 if (cluster_size
!= s
->cluster_size
) {
4870 error_setg(errp
, "Changing the cluster size is not supported");
4873 } else if (!strcmp(desc
->name
, BLOCK_OPT_LAZY_REFCOUNTS
)) {
4874 lazy_refcounts
= qemu_opt_get_bool(opts
, BLOCK_OPT_LAZY_REFCOUNTS
,
4876 } else if (!strcmp(desc
->name
, BLOCK_OPT_REFCOUNT_BITS
)) {
4877 refcount_bits
= qemu_opt_get_number(opts
, BLOCK_OPT_REFCOUNT_BITS
,
4880 if (refcount_bits
<= 0 || refcount_bits
> 64 ||
4881 !is_power_of_2(refcount_bits
))
4883 error_setg(errp
, "Refcount width must be a power of two and "
4884 "may not exceed 64 bits");
4887 } else if (!strcmp(desc
->name
, BLOCK_OPT_DATA_FILE
)) {
4888 data_file
= qemu_opt_get(opts
, BLOCK_OPT_DATA_FILE
);
4889 if (data_file
&& !has_data_file(bs
)) {
4890 error_setg(errp
, "data-file can only be set for images that "
4891 "use an external data file");
4894 } else if (!strcmp(desc
->name
, BLOCK_OPT_DATA_FILE_RAW
)) {
4895 data_file_raw
= qemu_opt_get_bool(opts
, BLOCK_OPT_DATA_FILE_RAW
,
4897 if (data_file_raw
&& !data_file_is_raw(bs
)) {
4898 error_setg(errp
, "data-file-raw cannot be set on existing "
4903 /* if this point is reached, this probably means a new option was
4904 * added without having it covered here */
4911 helper_cb_info
= (Qcow2AmendHelperCBInfo
){
4912 .original_status_cb
= status_cb
,
4913 .original_cb_opaque
= cb_opaque
,
4914 .total_operations
= (new_version
< old_version
)
4915 + (s
->refcount_bits
!= refcount_bits
)
4918 /* Upgrade first (some features may require compat=1.1) */
4919 if (new_version
> old_version
) {
4920 s
->qcow_version
= new_version
;
4921 ret
= qcow2_update_header(bs
);
4923 s
->qcow_version
= old_version
;
4924 error_setg_errno(errp
, -ret
, "Failed to update the image header");
4929 if (s
->refcount_bits
!= refcount_bits
) {
4930 int refcount_order
= ctz32(refcount_bits
);
4932 if (new_version
< 3 && refcount_bits
!= 16) {
4933 error_setg(errp
, "Refcount widths other than 16 bits require "
4934 "compatibility level 1.1 or above (use compat=1.1 or "
4939 helper_cb_info
.current_operation
= QCOW2_CHANGING_REFCOUNT_ORDER
;
4940 ret
= qcow2_change_refcount_order(bs
, refcount_order
,
4941 &qcow2_amend_helper_cb
,
4942 &helper_cb_info
, errp
);
4948 /* data-file-raw blocks backing files, so clear it first if requested */
4949 if (data_file_raw
) {
4950 s
->autoclear_features
|= QCOW2_AUTOCLEAR_DATA_FILE_RAW
;
4952 s
->autoclear_features
&= ~QCOW2_AUTOCLEAR_DATA_FILE_RAW
;
4956 g_free(s
->image_data_file
);
4957 s
->image_data_file
= *data_file
? g_strdup(data_file
) : NULL
;
4960 ret
= qcow2_update_header(bs
);
4962 error_setg_errno(errp
, -ret
, "Failed to update the image header");
4966 if (backing_file
|| backing_format
) {
4967 ret
= qcow2_change_backing_file(bs
,
4968 backing_file
?: s
->image_backing_file
,
4969 backing_format
?: s
->image_backing_format
);
4971 error_setg_errno(errp
, -ret
, "Failed to change the backing file");
4976 if (s
->use_lazy_refcounts
!= lazy_refcounts
) {
4977 if (lazy_refcounts
) {
4978 if (new_version
< 3) {
4979 error_setg(errp
, "Lazy refcounts only supported with "
4980 "compatibility level 1.1 and above (use compat=1.1 "
4984 s
->compatible_features
|= QCOW2_COMPAT_LAZY_REFCOUNTS
;
4985 ret
= qcow2_update_header(bs
);
4987 s
->compatible_features
&= ~QCOW2_COMPAT_LAZY_REFCOUNTS
;
4988 error_setg_errno(errp
, -ret
, "Failed to update the image header");
4991 s
->use_lazy_refcounts
= true;
4993 /* make image clean first */
4994 ret
= qcow2_mark_clean(bs
);
4996 error_setg_errno(errp
, -ret
, "Failed to make the image clean");
4999 /* now disallow lazy refcounts */
5000 s
->compatible_features
&= ~QCOW2_COMPAT_LAZY_REFCOUNTS
;
5001 ret
= qcow2_update_header(bs
);
5003 s
->compatible_features
|= QCOW2_COMPAT_LAZY_REFCOUNTS
;
5004 error_setg_errno(errp
, -ret
, "Failed to update the image header");
5007 s
->use_lazy_refcounts
= false;
5012 BlockBackend
*blk
= blk_new(bdrv_get_aio_context(bs
),
5013 BLK_PERM_RESIZE
, BLK_PERM_ALL
);
5014 ret
= blk_insert_bs(blk
, bs
, errp
);
5020 ret
= blk_truncate(blk
, new_size
, PREALLOC_MODE_OFF
, errp
);
5027 /* Downgrade last (so unsupported features can be removed before) */
5028 if (new_version
< old_version
) {
5029 helper_cb_info
.current_operation
= QCOW2_DOWNGRADING
;
5030 ret
= qcow2_downgrade(bs
, new_version
, &qcow2_amend_helper_cb
,
5031 &helper_cb_info
, errp
);
5041 * If offset or size are negative, respectively, they will not be included in
5042 * the BLOCK_IMAGE_CORRUPTED event emitted.
5043 * fatal will be ignored for read-only BDS; corruptions found there will always
5044 * be considered non-fatal.
5046 void qcow2_signal_corruption(BlockDriverState
*bs
, bool fatal
, int64_t offset
,
5047 int64_t size
, const char *message_format
, ...)
5049 BDRVQcow2State
*s
= bs
->opaque
;
5050 const char *node_name
;
5054 fatal
= fatal
&& bdrv_is_writable(bs
);
5056 if (s
->signaled_corruption
&&
5057 (!fatal
|| (s
->incompatible_features
& QCOW2_INCOMPAT_CORRUPT
)))
5062 va_start(ap
, message_format
);
5063 message
= g_strdup_vprintf(message_format
, ap
);
5067 fprintf(stderr
, "qcow2: Marking image as corrupt: %s; further "
5068 "corruption events will be suppressed\n", message
);
5070 fprintf(stderr
, "qcow2: Image is corrupt: %s; further non-fatal "
5071 "corruption events will be suppressed\n", message
);
5074 node_name
= bdrv_get_node_name(bs
);
5075 qapi_event_send_block_image_corrupted(bdrv_get_device_name(bs
),
5076 *node_name
!= '\0', node_name
,
5077 message
, offset
>= 0, offset
,
5083 qcow2_mark_corrupt(bs
);
5084 bs
->drv
= NULL
; /* make BDS unusable */
5087 s
->signaled_corruption
= true;
5090 static QemuOptsList qcow2_create_opts
= {
5091 .name
= "qcow2-create-opts",
5092 .head
= QTAILQ_HEAD_INITIALIZER(qcow2_create_opts
.head
),
5095 .name
= BLOCK_OPT_SIZE
,
5096 .type
= QEMU_OPT_SIZE
,
5097 .help
= "Virtual disk size"
5100 .name
= BLOCK_OPT_COMPAT_LEVEL
,
5101 .type
= QEMU_OPT_STRING
,
5102 .help
= "Compatibility level (v2 [0.10] or v3 [1.1])"
5105 .name
= BLOCK_OPT_BACKING_FILE
,
5106 .type
= QEMU_OPT_STRING
,
5107 .help
= "File name of a base image"
5110 .name
= BLOCK_OPT_BACKING_FMT
,
5111 .type
= QEMU_OPT_STRING
,
5112 .help
= "Image format of the base image"
5115 .name
= BLOCK_OPT_DATA_FILE
,
5116 .type
= QEMU_OPT_STRING
,
5117 .help
= "File name of an external data file"
5120 .name
= BLOCK_OPT_DATA_FILE_RAW
,
5121 .type
= QEMU_OPT_BOOL
,
5122 .help
= "The external data file must stay valid as a raw image"
5125 .name
= BLOCK_OPT_ENCRYPT
,
5126 .type
= QEMU_OPT_BOOL
,
5127 .help
= "Encrypt the image with format 'aes'. (Deprecated "
5128 "in favor of " BLOCK_OPT_ENCRYPT_FORMAT
"=aes)",
5131 .name
= BLOCK_OPT_ENCRYPT_FORMAT
,
5132 .type
= QEMU_OPT_STRING
,
5133 .help
= "Encrypt the image, format choices: 'aes', 'luks'",
5135 BLOCK_CRYPTO_OPT_DEF_KEY_SECRET("encrypt.",
5136 "ID of secret providing qcow AES key or LUKS passphrase"),
5137 BLOCK_CRYPTO_OPT_DEF_LUKS_CIPHER_ALG("encrypt."),
5138 BLOCK_CRYPTO_OPT_DEF_LUKS_CIPHER_MODE("encrypt."),
5139 BLOCK_CRYPTO_OPT_DEF_LUKS_IVGEN_ALG("encrypt."),
5140 BLOCK_CRYPTO_OPT_DEF_LUKS_IVGEN_HASH_ALG("encrypt."),
5141 BLOCK_CRYPTO_OPT_DEF_LUKS_HASH_ALG("encrypt."),
5142 BLOCK_CRYPTO_OPT_DEF_LUKS_ITER_TIME("encrypt."),
5144 .name
= BLOCK_OPT_CLUSTER_SIZE
,
5145 .type
= QEMU_OPT_SIZE
,
5146 .help
= "qcow2 cluster size",
5147 .def_value_str
= stringify(DEFAULT_CLUSTER_SIZE
)
5150 .name
= BLOCK_OPT_PREALLOC
,
5151 .type
= QEMU_OPT_STRING
,
5152 .help
= "Preallocation mode (allowed values: off, metadata, "
5156 .name
= BLOCK_OPT_LAZY_REFCOUNTS
,
5157 .type
= QEMU_OPT_BOOL
,
5158 .help
= "Postpone refcount updates",
5159 .def_value_str
= "off"
5162 .name
= BLOCK_OPT_REFCOUNT_BITS
,
5163 .type
= QEMU_OPT_NUMBER
,
5164 .help
= "Width of a reference count entry in bits",
5165 .def_value_str
= "16"
5167 { /* end of list */ }
5171 static const char *const qcow2_strong_runtime_opts
[] = {
5172 "encrypt." BLOCK_CRYPTO_OPT_QCOW_KEY_SECRET
,
5177 BlockDriver bdrv_qcow2
= {
5178 .format_name
= "qcow2",
5179 .instance_size
= sizeof(BDRVQcow2State
),
5180 .bdrv_probe
= qcow2_probe
,
5181 .bdrv_open
= qcow2_open
,
5182 .bdrv_close
= qcow2_close
,
5183 .bdrv_reopen_prepare
= qcow2_reopen_prepare
,
5184 .bdrv_reopen_commit
= qcow2_reopen_commit
,
5185 .bdrv_reopen_abort
= qcow2_reopen_abort
,
5186 .bdrv_join_options
= qcow2_join_options
,
5187 .bdrv_child_perm
= bdrv_format_default_perms
,
5188 .bdrv_co_create_opts
= qcow2_co_create_opts
,
5189 .bdrv_co_create
= qcow2_co_create
,
5190 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
5191 .bdrv_co_block_status
= qcow2_co_block_status
,
5193 .bdrv_co_preadv
= qcow2_co_preadv
,
5194 .bdrv_co_pwritev
= qcow2_co_pwritev
,
5195 .bdrv_co_flush_to_os
= qcow2_co_flush_to_os
,
5197 .bdrv_co_pwrite_zeroes
= qcow2_co_pwrite_zeroes
,
5198 .bdrv_co_pdiscard
= qcow2_co_pdiscard
,
5199 .bdrv_co_copy_range_from
= qcow2_co_copy_range_from
,
5200 .bdrv_co_copy_range_to
= qcow2_co_copy_range_to
,
5201 .bdrv_co_truncate
= qcow2_co_truncate
,
5202 .bdrv_co_pwritev_compressed
= qcow2_co_pwritev_compressed
,
5203 .bdrv_make_empty
= qcow2_make_empty
,
5205 .bdrv_snapshot_create
= qcow2_snapshot_create
,
5206 .bdrv_snapshot_goto
= qcow2_snapshot_goto
,
5207 .bdrv_snapshot_delete
= qcow2_snapshot_delete
,
5208 .bdrv_snapshot_list
= qcow2_snapshot_list
,
5209 .bdrv_snapshot_load_tmp
= qcow2_snapshot_load_tmp
,
5210 .bdrv_measure
= qcow2_measure
,
5211 .bdrv_get_info
= qcow2_get_info
,
5212 .bdrv_get_specific_info
= qcow2_get_specific_info
,
5214 .bdrv_save_vmstate
= qcow2_save_vmstate
,
5215 .bdrv_load_vmstate
= qcow2_load_vmstate
,
5217 .supports_backing
= true,
5218 .bdrv_change_backing_file
= qcow2_change_backing_file
,
5220 .bdrv_refresh_limits
= qcow2_refresh_limits
,
5221 .bdrv_co_invalidate_cache
= qcow2_co_invalidate_cache
,
5222 .bdrv_inactivate
= qcow2_inactivate
,
5224 .create_opts
= &qcow2_create_opts
,
5225 .strong_runtime_opts
= qcow2_strong_runtime_opts
,
5226 .mutable_opts
= mutable_opts
,
5227 .bdrv_co_check
= qcow2_co_check
,
5228 .bdrv_amend_options
= qcow2_amend_options
,
5230 .bdrv_detach_aio_context
= qcow2_detach_aio_context
,
5231 .bdrv_attach_aio_context
= qcow2_attach_aio_context
,
5233 .bdrv_reopen_bitmaps_rw
= qcow2_reopen_bitmaps_rw
,
5234 .bdrv_can_store_new_dirty_bitmap
= qcow2_can_store_new_dirty_bitmap
,
5235 .bdrv_remove_persistent_dirty_bitmap
= qcow2_remove_persistent_dirty_bitmap
,
5238 static void bdrv_qcow2_init(void)
5240 bdrv_register(&bdrv_qcow2
);
5243 block_init(bdrv_qcow2_init
);