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"
44 #include "block/aio_task.h"
47 Differences with QCOW:
49 - Support for multiple incremental snapshots.
50 - Memory management by reference counts.
51 - Clusters which have a reference count of one have the bit
52 QCOW_OFLAG_COPIED to optimize write performance.
53 - Size of compressed clusters is stored in sectors to reduce bit usage
54 in the cluster offsets.
55 - Support for storing additional data (such as the VM state) in the
57 - If a backing store is used, the cluster size is not constrained
58 (could be backported to QCOW).
59 - L2 tables have always a size of one cluster.
66 } QEMU_PACKED QCowExtension
;
68 #define QCOW2_EXT_MAGIC_END 0
69 #define QCOW2_EXT_MAGIC_BACKING_FORMAT 0xE2792ACA
70 #define QCOW2_EXT_MAGIC_FEATURE_TABLE 0x6803f857
71 #define QCOW2_EXT_MAGIC_CRYPTO_HEADER 0x0537be77
72 #define QCOW2_EXT_MAGIC_BITMAPS 0x23852875
73 #define QCOW2_EXT_MAGIC_DATA_FILE 0x44415441
75 static int coroutine_fn
76 qcow2_co_preadv_compressed(BlockDriverState
*bs
,
77 uint64_t file_cluster_offset
,
83 static int qcow2_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
85 const QCowHeader
*cow_header
= (const void *)buf
;
87 if (buf_size
>= sizeof(QCowHeader
) &&
88 be32_to_cpu(cow_header
->magic
) == QCOW_MAGIC
&&
89 be32_to_cpu(cow_header
->version
) >= 2)
96 static ssize_t
qcow2_crypto_hdr_read_func(QCryptoBlock
*block
, size_t offset
,
97 uint8_t *buf
, size_t buflen
,
98 void *opaque
, Error
**errp
)
100 BlockDriverState
*bs
= opaque
;
101 BDRVQcow2State
*s
= bs
->opaque
;
104 if ((offset
+ buflen
) > s
->crypto_header
.length
) {
105 error_setg(errp
, "Request for data outside of extension header");
109 ret
= bdrv_pread(bs
->file
,
110 s
->crypto_header
.offset
+ offset
, buf
, buflen
);
112 error_setg_errno(errp
, -ret
, "Could not read encryption header");
119 static ssize_t
qcow2_crypto_hdr_init_func(QCryptoBlock
*block
, size_t headerlen
,
120 void *opaque
, Error
**errp
)
122 BlockDriverState
*bs
= opaque
;
123 BDRVQcow2State
*s
= bs
->opaque
;
127 ret
= qcow2_alloc_clusters(bs
, headerlen
);
129 error_setg_errno(errp
, -ret
,
130 "Cannot allocate cluster for LUKS header size %zu",
135 s
->crypto_header
.length
= headerlen
;
136 s
->crypto_header
.offset
= ret
;
138 /* Zero fill remaining space in cluster so it has predictable
139 * content in case of future spec changes */
140 clusterlen
= size_to_clusters(s
, headerlen
) * s
->cluster_size
;
141 assert(qcow2_pre_write_overlap_check(bs
, 0, ret
, clusterlen
, false) == 0);
142 ret
= bdrv_pwrite_zeroes(bs
->file
,
144 clusterlen
- headerlen
, 0);
146 error_setg_errno(errp
, -ret
, "Could not zero fill encryption header");
154 static ssize_t
qcow2_crypto_hdr_write_func(QCryptoBlock
*block
, size_t offset
,
155 const uint8_t *buf
, size_t buflen
,
156 void *opaque
, Error
**errp
)
158 BlockDriverState
*bs
= opaque
;
159 BDRVQcow2State
*s
= bs
->opaque
;
162 if ((offset
+ buflen
) > s
->crypto_header
.length
) {
163 error_setg(errp
, "Request for data outside of extension header");
167 ret
= bdrv_pwrite(bs
->file
,
168 s
->crypto_header
.offset
+ offset
, buf
, buflen
);
170 error_setg_errno(errp
, -ret
, "Could not read encryption header");
178 * read qcow2 extension and fill bs
179 * start reading from start_offset
180 * finish reading upon magic of value 0 or when end_offset reached
181 * unknown magic is skipped (future extension this version knows nothing about)
182 * return 0 upon success, non-0 otherwise
184 static int qcow2_read_extensions(BlockDriverState
*bs
, uint64_t start_offset
,
185 uint64_t end_offset
, void **p_feature_table
,
186 int flags
, bool *need_update_header
,
189 BDRVQcow2State
*s
= bs
->opaque
;
193 Qcow2BitmapHeaderExt bitmaps_ext
;
195 if (need_update_header
!= NULL
) {
196 *need_update_header
= false;
200 printf("qcow2_read_extensions: start=%ld end=%ld\n", start_offset
, end_offset
);
202 offset
= start_offset
;
203 while (offset
< end_offset
) {
207 if (offset
> s
->cluster_size
)
208 printf("qcow2_read_extension: suspicious offset %lu\n", offset
);
210 printf("attempting to read extended header in offset %lu\n", offset
);
213 ret
= bdrv_pread(bs
->file
, offset
, &ext
, sizeof(ext
));
215 error_setg_errno(errp
, -ret
, "qcow2_read_extension: ERROR: "
216 "pread fail from offset %" PRIu64
, offset
);
219 ext
.magic
= be32_to_cpu(ext
.magic
);
220 ext
.len
= be32_to_cpu(ext
.len
);
221 offset
+= sizeof(ext
);
223 printf("ext.magic = 0x%x\n", ext
.magic
);
225 if (offset
> end_offset
|| ext
.len
> end_offset
- offset
) {
226 error_setg(errp
, "Header extension too large");
231 case QCOW2_EXT_MAGIC_END
:
234 case QCOW2_EXT_MAGIC_BACKING_FORMAT
:
235 if (ext
.len
>= sizeof(bs
->backing_format
)) {
236 error_setg(errp
, "ERROR: ext_backing_format: len=%" PRIu32
237 " too large (>=%zu)", ext
.len
,
238 sizeof(bs
->backing_format
));
241 ret
= bdrv_pread(bs
->file
, offset
, bs
->backing_format
, ext
.len
);
243 error_setg_errno(errp
, -ret
, "ERROR: ext_backing_format: "
244 "Could not read format name");
247 bs
->backing_format
[ext
.len
] = '\0';
248 s
->image_backing_format
= g_strdup(bs
->backing_format
);
250 printf("Qcow2: Got format extension %s\n", bs
->backing_format
);
254 case QCOW2_EXT_MAGIC_FEATURE_TABLE
:
255 if (p_feature_table
!= NULL
) {
256 void* feature_table
= g_malloc0(ext
.len
+ 2 * sizeof(Qcow2Feature
));
257 ret
= bdrv_pread(bs
->file
, offset
, feature_table
, ext
.len
);
259 error_setg_errno(errp
, -ret
, "ERROR: ext_feature_table: "
260 "Could not read table");
264 *p_feature_table
= feature_table
;
268 case QCOW2_EXT_MAGIC_CRYPTO_HEADER
: {
269 unsigned int cflags
= 0;
270 if (s
->crypt_method_header
!= QCOW_CRYPT_LUKS
) {
271 error_setg(errp
, "CRYPTO header extension only "
272 "expected with LUKS encryption method");
275 if (ext
.len
!= sizeof(Qcow2CryptoHeaderExtension
)) {
276 error_setg(errp
, "CRYPTO header extension size %u, "
277 "but expected size %zu", ext
.len
,
278 sizeof(Qcow2CryptoHeaderExtension
));
282 ret
= bdrv_pread(bs
->file
, offset
, &s
->crypto_header
, ext
.len
);
284 error_setg_errno(errp
, -ret
,
285 "Unable to read CRYPTO header extension");
288 s
->crypto_header
.offset
= be64_to_cpu(s
->crypto_header
.offset
);
289 s
->crypto_header
.length
= be64_to_cpu(s
->crypto_header
.length
);
291 if ((s
->crypto_header
.offset
% s
->cluster_size
) != 0) {
292 error_setg(errp
, "Encryption header offset '%" PRIu64
"' is "
293 "not a multiple of cluster size '%u'",
294 s
->crypto_header
.offset
, s
->cluster_size
);
298 if (flags
& BDRV_O_NO_IO
) {
299 cflags
|= QCRYPTO_BLOCK_OPEN_NO_IO
;
301 s
->crypto
= qcrypto_block_open(s
->crypto_opts
, "encrypt.",
302 qcow2_crypto_hdr_read_func
,
303 bs
, cflags
, QCOW2_MAX_THREADS
, errp
);
309 case QCOW2_EXT_MAGIC_BITMAPS
:
310 if (ext
.len
!= sizeof(bitmaps_ext
)) {
311 error_setg_errno(errp
, -ret
, "bitmaps_ext: "
312 "Invalid extension length");
316 if (!(s
->autoclear_features
& QCOW2_AUTOCLEAR_BITMAPS
)) {
317 if (s
->qcow_version
< 3) {
318 /* Let's be a bit more specific */
319 warn_report("This qcow2 v2 image contains bitmaps, but "
320 "they may have been modified by a program "
321 "without persistent bitmap support; so now "
322 "they must all be considered inconsistent");
324 warn_report("a program lacking bitmap support "
325 "modified this file, so all bitmaps are now "
326 "considered inconsistent");
328 error_printf("Some clusters may be leaked, "
329 "run 'qemu-img check -r' on the image "
331 if (need_update_header
!= NULL
) {
332 /* Updating is needed to drop invalid bitmap extension. */
333 *need_update_header
= true;
338 ret
= bdrv_pread(bs
->file
, offset
, &bitmaps_ext
, ext
.len
);
340 error_setg_errno(errp
, -ret
, "bitmaps_ext: "
341 "Could not read ext header");
345 if (bitmaps_ext
.reserved32
!= 0) {
346 error_setg_errno(errp
, -ret
, "bitmaps_ext: "
347 "Reserved field is not zero");
351 bitmaps_ext
.nb_bitmaps
= be32_to_cpu(bitmaps_ext
.nb_bitmaps
);
352 bitmaps_ext
.bitmap_directory_size
=
353 be64_to_cpu(bitmaps_ext
.bitmap_directory_size
);
354 bitmaps_ext
.bitmap_directory_offset
=
355 be64_to_cpu(bitmaps_ext
.bitmap_directory_offset
);
357 if (bitmaps_ext
.nb_bitmaps
> QCOW2_MAX_BITMAPS
) {
359 "bitmaps_ext: Image has %" PRIu32
" bitmaps, "
360 "exceeding the QEMU supported maximum of %d",
361 bitmaps_ext
.nb_bitmaps
, QCOW2_MAX_BITMAPS
);
365 if (bitmaps_ext
.nb_bitmaps
== 0) {
366 error_setg(errp
, "found bitmaps extension with zero bitmaps");
370 if (offset_into_cluster(s
, bitmaps_ext
.bitmap_directory_offset
)) {
371 error_setg(errp
, "bitmaps_ext: "
372 "invalid bitmap directory offset");
376 if (bitmaps_ext
.bitmap_directory_size
>
377 QCOW2_MAX_BITMAP_DIRECTORY_SIZE
) {
378 error_setg(errp
, "bitmaps_ext: "
379 "bitmap directory size (%" PRIu64
") exceeds "
380 "the maximum supported size (%d)",
381 bitmaps_ext
.bitmap_directory_size
,
382 QCOW2_MAX_BITMAP_DIRECTORY_SIZE
);
386 s
->nb_bitmaps
= bitmaps_ext
.nb_bitmaps
;
387 s
->bitmap_directory_offset
=
388 bitmaps_ext
.bitmap_directory_offset
;
389 s
->bitmap_directory_size
=
390 bitmaps_ext
.bitmap_directory_size
;
393 printf("Qcow2: Got bitmaps extension: "
394 "offset=%" PRIu64
" nb_bitmaps=%" PRIu32
"\n",
395 s
->bitmap_directory_offset
, s
->nb_bitmaps
);
399 case QCOW2_EXT_MAGIC_DATA_FILE
:
401 s
->image_data_file
= g_malloc0(ext
.len
+ 1);
402 ret
= bdrv_pread(bs
->file
, offset
, s
->image_data_file
, ext
.len
);
404 error_setg_errno(errp
, -ret
,
405 "ERROR: Could not read data file name");
409 printf("Qcow2: Got external data file %s\n", s
->image_data_file
);
415 /* unknown magic - save it in case we need to rewrite the header */
416 /* If you add a new feature, make sure to also update the fast
417 * path of qcow2_make_empty() to deal with it. */
419 Qcow2UnknownHeaderExtension
*uext
;
421 uext
= g_malloc0(sizeof(*uext
) + ext
.len
);
422 uext
->magic
= ext
.magic
;
424 QLIST_INSERT_HEAD(&s
->unknown_header_ext
, uext
, next
);
426 ret
= bdrv_pread(bs
->file
, offset
, uext
->data
, uext
->len
);
428 error_setg_errno(errp
, -ret
, "ERROR: unknown extension: "
429 "Could not read data");
436 offset
+= ((ext
.len
+ 7) & ~7);
442 static void cleanup_unknown_header_ext(BlockDriverState
*bs
)
444 BDRVQcow2State
*s
= bs
->opaque
;
445 Qcow2UnknownHeaderExtension
*uext
, *next
;
447 QLIST_FOREACH_SAFE(uext
, &s
->unknown_header_ext
, next
, next
) {
448 QLIST_REMOVE(uext
, next
);
453 static void report_unsupported_feature(Error
**errp
, Qcow2Feature
*table
,
456 char *features
= g_strdup("");
459 while (table
&& table
->name
[0] != '\0') {
460 if (table
->type
== QCOW2_FEAT_TYPE_INCOMPATIBLE
) {
461 if (mask
& (1ULL << table
->bit
)) {
463 features
= g_strdup_printf("%s%s%.46s", old
, *old
? ", " : "",
466 mask
&= ~(1ULL << table
->bit
);
474 features
= g_strdup_printf("%s%sUnknown incompatible feature: %" PRIx64
,
475 old
, *old
? ", " : "", mask
);
479 error_setg(errp
, "Unsupported qcow2 feature(s): %s", features
);
484 * Sets the dirty bit and flushes afterwards if necessary.
486 * The incompatible_features bit is only set if the image file header was
487 * updated successfully. Therefore it is not required to check the return
488 * value of this function.
490 int qcow2_mark_dirty(BlockDriverState
*bs
)
492 BDRVQcow2State
*s
= bs
->opaque
;
496 assert(s
->qcow_version
>= 3);
498 if (s
->incompatible_features
& QCOW2_INCOMPAT_DIRTY
) {
499 return 0; /* already dirty */
502 val
= cpu_to_be64(s
->incompatible_features
| QCOW2_INCOMPAT_DIRTY
);
503 ret
= bdrv_pwrite(bs
->file
, offsetof(QCowHeader
, incompatible_features
),
508 ret
= bdrv_flush(bs
->file
->bs
);
513 /* Only treat image as dirty if the header was updated successfully */
514 s
->incompatible_features
|= QCOW2_INCOMPAT_DIRTY
;
519 * Clears the dirty bit and flushes before if necessary. Only call this
520 * function when there are no pending requests, it does not guard against
521 * concurrent requests dirtying the image.
523 static int qcow2_mark_clean(BlockDriverState
*bs
)
525 BDRVQcow2State
*s
= bs
->opaque
;
527 if (s
->incompatible_features
& QCOW2_INCOMPAT_DIRTY
) {
530 s
->incompatible_features
&= ~QCOW2_INCOMPAT_DIRTY
;
532 ret
= qcow2_flush_caches(bs
);
537 return qcow2_update_header(bs
);
543 * Marks the image as corrupt.
545 int qcow2_mark_corrupt(BlockDriverState
*bs
)
547 BDRVQcow2State
*s
= bs
->opaque
;
549 s
->incompatible_features
|= QCOW2_INCOMPAT_CORRUPT
;
550 return qcow2_update_header(bs
);
554 * Marks the image as consistent, i.e., unsets the corrupt bit, and flushes
555 * before if necessary.
557 int qcow2_mark_consistent(BlockDriverState
*bs
)
559 BDRVQcow2State
*s
= bs
->opaque
;
561 if (s
->incompatible_features
& QCOW2_INCOMPAT_CORRUPT
) {
562 int ret
= qcow2_flush_caches(bs
);
567 s
->incompatible_features
&= ~QCOW2_INCOMPAT_CORRUPT
;
568 return qcow2_update_header(bs
);
573 static void qcow2_add_check_result(BdrvCheckResult
*out
,
574 const BdrvCheckResult
*src
,
575 bool set_allocation_info
)
577 out
->corruptions
+= src
->corruptions
;
578 out
->leaks
+= src
->leaks
;
579 out
->check_errors
+= src
->check_errors
;
580 out
->corruptions_fixed
+= src
->corruptions_fixed
;
581 out
->leaks_fixed
+= src
->leaks_fixed
;
583 if (set_allocation_info
) {
584 out
->image_end_offset
= src
->image_end_offset
;
589 static int coroutine_fn
qcow2_co_check_locked(BlockDriverState
*bs
,
590 BdrvCheckResult
*result
,
593 BdrvCheckResult snapshot_res
= {};
594 BdrvCheckResult refcount_res
= {};
597 memset(result
, 0, sizeof(*result
));
599 ret
= qcow2_check_read_snapshot_table(bs
, &snapshot_res
, fix
);
601 qcow2_add_check_result(result
, &snapshot_res
, false);
605 ret
= qcow2_check_refcounts(bs
, &refcount_res
, fix
);
606 qcow2_add_check_result(result
, &refcount_res
, true);
608 qcow2_add_check_result(result
, &snapshot_res
, false);
612 ret
= qcow2_check_fix_snapshot_table(bs
, &snapshot_res
, fix
);
613 qcow2_add_check_result(result
, &snapshot_res
, false);
618 if (fix
&& result
->check_errors
== 0 && result
->corruptions
== 0) {
619 ret
= qcow2_mark_clean(bs
);
623 return qcow2_mark_consistent(bs
);
628 static int coroutine_fn
qcow2_co_check(BlockDriverState
*bs
,
629 BdrvCheckResult
*result
,
632 BDRVQcow2State
*s
= bs
->opaque
;
635 qemu_co_mutex_lock(&s
->lock
);
636 ret
= qcow2_co_check_locked(bs
, result
, fix
);
637 qemu_co_mutex_unlock(&s
->lock
);
641 int qcow2_validate_table(BlockDriverState
*bs
, uint64_t offset
,
642 uint64_t entries
, size_t entry_len
,
643 int64_t max_size_bytes
, const char *table_name
,
646 BDRVQcow2State
*s
= bs
->opaque
;
648 if (entries
> max_size_bytes
/ entry_len
) {
649 error_setg(errp
, "%s too large", table_name
);
653 /* Use signed INT64_MAX as the maximum even for uint64_t header fields,
654 * because values will be passed to qemu functions taking int64_t. */
655 if ((INT64_MAX
- entries
* entry_len
< offset
) ||
656 (offset_into_cluster(s
, offset
) != 0)) {
657 error_setg(errp
, "%s offset invalid", table_name
);
664 static const char *const mutable_opts
[] = {
665 QCOW2_OPT_LAZY_REFCOUNTS
,
666 QCOW2_OPT_DISCARD_REQUEST
,
667 QCOW2_OPT_DISCARD_SNAPSHOT
,
668 QCOW2_OPT_DISCARD_OTHER
,
670 QCOW2_OPT_OVERLAP_TEMPLATE
,
671 QCOW2_OPT_OVERLAP_MAIN_HEADER
,
672 QCOW2_OPT_OVERLAP_ACTIVE_L1
,
673 QCOW2_OPT_OVERLAP_ACTIVE_L2
,
674 QCOW2_OPT_OVERLAP_REFCOUNT_TABLE
,
675 QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK
,
676 QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE
,
677 QCOW2_OPT_OVERLAP_INACTIVE_L1
,
678 QCOW2_OPT_OVERLAP_INACTIVE_L2
,
679 QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY
,
680 QCOW2_OPT_CACHE_SIZE
,
681 QCOW2_OPT_L2_CACHE_SIZE
,
682 QCOW2_OPT_L2_CACHE_ENTRY_SIZE
,
683 QCOW2_OPT_REFCOUNT_CACHE_SIZE
,
684 QCOW2_OPT_CACHE_CLEAN_INTERVAL
,
688 static QemuOptsList qcow2_runtime_opts
= {
690 .head
= QTAILQ_HEAD_INITIALIZER(qcow2_runtime_opts
.head
),
693 .name
= QCOW2_OPT_LAZY_REFCOUNTS
,
694 .type
= QEMU_OPT_BOOL
,
695 .help
= "Postpone refcount updates",
698 .name
= QCOW2_OPT_DISCARD_REQUEST
,
699 .type
= QEMU_OPT_BOOL
,
700 .help
= "Pass guest discard requests to the layer below",
703 .name
= QCOW2_OPT_DISCARD_SNAPSHOT
,
704 .type
= QEMU_OPT_BOOL
,
705 .help
= "Generate discard requests when snapshot related space "
709 .name
= QCOW2_OPT_DISCARD_OTHER
,
710 .type
= QEMU_OPT_BOOL
,
711 .help
= "Generate discard requests when other clusters are freed",
714 .name
= QCOW2_OPT_OVERLAP
,
715 .type
= QEMU_OPT_STRING
,
716 .help
= "Selects which overlap checks to perform from a range of "
717 "templates (none, constant, cached, all)",
720 .name
= QCOW2_OPT_OVERLAP_TEMPLATE
,
721 .type
= QEMU_OPT_STRING
,
722 .help
= "Selects which overlap checks to perform from a range of "
723 "templates (none, constant, cached, all)",
726 .name
= QCOW2_OPT_OVERLAP_MAIN_HEADER
,
727 .type
= QEMU_OPT_BOOL
,
728 .help
= "Check for unintended writes into the main qcow2 header",
731 .name
= QCOW2_OPT_OVERLAP_ACTIVE_L1
,
732 .type
= QEMU_OPT_BOOL
,
733 .help
= "Check for unintended writes into the active L1 table",
736 .name
= QCOW2_OPT_OVERLAP_ACTIVE_L2
,
737 .type
= QEMU_OPT_BOOL
,
738 .help
= "Check for unintended writes into an active L2 table",
741 .name
= QCOW2_OPT_OVERLAP_REFCOUNT_TABLE
,
742 .type
= QEMU_OPT_BOOL
,
743 .help
= "Check for unintended writes into the refcount table",
746 .name
= QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK
,
747 .type
= QEMU_OPT_BOOL
,
748 .help
= "Check for unintended writes into a refcount block",
751 .name
= QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE
,
752 .type
= QEMU_OPT_BOOL
,
753 .help
= "Check for unintended writes into the snapshot table",
756 .name
= QCOW2_OPT_OVERLAP_INACTIVE_L1
,
757 .type
= QEMU_OPT_BOOL
,
758 .help
= "Check for unintended writes into an inactive L1 table",
761 .name
= QCOW2_OPT_OVERLAP_INACTIVE_L2
,
762 .type
= QEMU_OPT_BOOL
,
763 .help
= "Check for unintended writes into an inactive L2 table",
766 .name
= QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY
,
767 .type
= QEMU_OPT_BOOL
,
768 .help
= "Check for unintended writes into the bitmap directory",
771 .name
= QCOW2_OPT_CACHE_SIZE
,
772 .type
= QEMU_OPT_SIZE
,
773 .help
= "Maximum combined metadata (L2 tables and refcount blocks) "
777 .name
= QCOW2_OPT_L2_CACHE_SIZE
,
778 .type
= QEMU_OPT_SIZE
,
779 .help
= "Maximum L2 table cache size",
782 .name
= QCOW2_OPT_L2_CACHE_ENTRY_SIZE
,
783 .type
= QEMU_OPT_SIZE
,
784 .help
= "Size of each entry in the L2 cache",
787 .name
= QCOW2_OPT_REFCOUNT_CACHE_SIZE
,
788 .type
= QEMU_OPT_SIZE
,
789 .help
= "Maximum refcount block cache size",
792 .name
= QCOW2_OPT_CACHE_CLEAN_INTERVAL
,
793 .type
= QEMU_OPT_NUMBER
,
794 .help
= "Clean unused cache entries after this time (in seconds)",
796 BLOCK_CRYPTO_OPT_DEF_KEY_SECRET("encrypt.",
797 "ID of secret providing qcow2 AES key or LUKS passphrase"),
798 { /* end of list */ }
802 static const char *overlap_bool_option_names
[QCOW2_OL_MAX_BITNR
] = {
803 [QCOW2_OL_MAIN_HEADER_BITNR
] = QCOW2_OPT_OVERLAP_MAIN_HEADER
,
804 [QCOW2_OL_ACTIVE_L1_BITNR
] = QCOW2_OPT_OVERLAP_ACTIVE_L1
,
805 [QCOW2_OL_ACTIVE_L2_BITNR
] = QCOW2_OPT_OVERLAP_ACTIVE_L2
,
806 [QCOW2_OL_REFCOUNT_TABLE_BITNR
] = QCOW2_OPT_OVERLAP_REFCOUNT_TABLE
,
807 [QCOW2_OL_REFCOUNT_BLOCK_BITNR
] = QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK
,
808 [QCOW2_OL_SNAPSHOT_TABLE_BITNR
] = QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE
,
809 [QCOW2_OL_INACTIVE_L1_BITNR
] = QCOW2_OPT_OVERLAP_INACTIVE_L1
,
810 [QCOW2_OL_INACTIVE_L2_BITNR
] = QCOW2_OPT_OVERLAP_INACTIVE_L2
,
811 [QCOW2_OL_BITMAP_DIRECTORY_BITNR
] = QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY
,
814 static void cache_clean_timer_cb(void *opaque
)
816 BlockDriverState
*bs
= opaque
;
817 BDRVQcow2State
*s
= bs
->opaque
;
818 qcow2_cache_clean_unused(s
->l2_table_cache
);
819 qcow2_cache_clean_unused(s
->refcount_block_cache
);
820 timer_mod(s
->cache_clean_timer
, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
) +
821 (int64_t) s
->cache_clean_interval
* 1000);
824 static void cache_clean_timer_init(BlockDriverState
*bs
, AioContext
*context
)
826 BDRVQcow2State
*s
= bs
->opaque
;
827 if (s
->cache_clean_interval
> 0) {
828 s
->cache_clean_timer
= aio_timer_new(context
, QEMU_CLOCK_VIRTUAL
,
829 SCALE_MS
, cache_clean_timer_cb
,
831 timer_mod(s
->cache_clean_timer
, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
) +
832 (int64_t) s
->cache_clean_interval
* 1000);
836 static void cache_clean_timer_del(BlockDriverState
*bs
)
838 BDRVQcow2State
*s
= bs
->opaque
;
839 if (s
->cache_clean_timer
) {
840 timer_del(s
->cache_clean_timer
);
841 timer_free(s
->cache_clean_timer
);
842 s
->cache_clean_timer
= NULL
;
846 static void qcow2_detach_aio_context(BlockDriverState
*bs
)
848 cache_clean_timer_del(bs
);
851 static void qcow2_attach_aio_context(BlockDriverState
*bs
,
852 AioContext
*new_context
)
854 cache_clean_timer_init(bs
, new_context
);
857 static void read_cache_sizes(BlockDriverState
*bs
, QemuOpts
*opts
,
858 uint64_t *l2_cache_size
,
859 uint64_t *l2_cache_entry_size
,
860 uint64_t *refcount_cache_size
, Error
**errp
)
862 BDRVQcow2State
*s
= bs
->opaque
;
863 uint64_t combined_cache_size
, l2_cache_max_setting
;
864 bool l2_cache_size_set
, refcount_cache_size_set
, combined_cache_size_set
;
865 bool l2_cache_entry_size_set
;
866 int min_refcount_cache
= MIN_REFCOUNT_CACHE_SIZE
* s
->cluster_size
;
867 uint64_t virtual_disk_size
= bs
->total_sectors
* BDRV_SECTOR_SIZE
;
868 uint64_t max_l2_entries
= DIV_ROUND_UP(virtual_disk_size
, s
->cluster_size
);
869 /* An L2 table is always one cluster in size so the max cache size
870 * should be a multiple of the cluster size. */
871 uint64_t max_l2_cache
= ROUND_UP(max_l2_entries
* sizeof(uint64_t),
874 combined_cache_size_set
= qemu_opt_get(opts
, QCOW2_OPT_CACHE_SIZE
);
875 l2_cache_size_set
= qemu_opt_get(opts
, QCOW2_OPT_L2_CACHE_SIZE
);
876 refcount_cache_size_set
= qemu_opt_get(opts
, QCOW2_OPT_REFCOUNT_CACHE_SIZE
);
877 l2_cache_entry_size_set
= qemu_opt_get(opts
, QCOW2_OPT_L2_CACHE_ENTRY_SIZE
);
879 combined_cache_size
= qemu_opt_get_size(opts
, QCOW2_OPT_CACHE_SIZE
, 0);
880 l2_cache_max_setting
= qemu_opt_get_size(opts
, QCOW2_OPT_L2_CACHE_SIZE
,
881 DEFAULT_L2_CACHE_MAX_SIZE
);
882 *refcount_cache_size
= qemu_opt_get_size(opts
,
883 QCOW2_OPT_REFCOUNT_CACHE_SIZE
, 0);
885 *l2_cache_entry_size
= qemu_opt_get_size(
886 opts
, QCOW2_OPT_L2_CACHE_ENTRY_SIZE
, s
->cluster_size
);
888 *l2_cache_size
= MIN(max_l2_cache
, l2_cache_max_setting
);
890 if (combined_cache_size_set
) {
891 if (l2_cache_size_set
&& refcount_cache_size_set
) {
892 error_setg(errp
, QCOW2_OPT_CACHE_SIZE
", " QCOW2_OPT_L2_CACHE_SIZE
893 " and " QCOW2_OPT_REFCOUNT_CACHE_SIZE
" may not be set "
896 } else if (l2_cache_size_set
&&
897 (l2_cache_max_setting
> combined_cache_size
)) {
898 error_setg(errp
, QCOW2_OPT_L2_CACHE_SIZE
" may not exceed "
899 QCOW2_OPT_CACHE_SIZE
);
901 } else if (*refcount_cache_size
> combined_cache_size
) {
902 error_setg(errp
, QCOW2_OPT_REFCOUNT_CACHE_SIZE
" may not exceed "
903 QCOW2_OPT_CACHE_SIZE
);
907 if (l2_cache_size_set
) {
908 *refcount_cache_size
= combined_cache_size
- *l2_cache_size
;
909 } else if (refcount_cache_size_set
) {
910 *l2_cache_size
= combined_cache_size
- *refcount_cache_size
;
912 /* Assign as much memory as possible to the L2 cache, and
913 * use the remainder for the refcount cache */
914 if (combined_cache_size
>= max_l2_cache
+ min_refcount_cache
) {
915 *l2_cache_size
= max_l2_cache
;
916 *refcount_cache_size
= combined_cache_size
- *l2_cache_size
;
918 *refcount_cache_size
=
919 MIN(combined_cache_size
, min_refcount_cache
);
920 *l2_cache_size
= combined_cache_size
- *refcount_cache_size
;
926 * If the L2 cache is not enough to cover the whole disk then
927 * default to 4KB entries. Smaller entries reduce the cost of
928 * loads and evictions and increase I/O performance.
930 if (*l2_cache_size
< max_l2_cache
&& !l2_cache_entry_size_set
) {
931 *l2_cache_entry_size
= MIN(s
->cluster_size
, 4096);
934 /* l2_cache_size and refcount_cache_size are ensured to have at least
935 * their minimum values in qcow2_update_options_prepare() */
937 if (*l2_cache_entry_size
< (1 << MIN_CLUSTER_BITS
) ||
938 *l2_cache_entry_size
> s
->cluster_size
||
939 !is_power_of_2(*l2_cache_entry_size
)) {
940 error_setg(errp
, "L2 cache entry size must be a power of two "
941 "between %d and the cluster size (%d)",
942 1 << MIN_CLUSTER_BITS
, s
->cluster_size
);
947 typedef struct Qcow2ReopenState
{
948 Qcow2Cache
*l2_table_cache
;
949 Qcow2Cache
*refcount_block_cache
;
950 int l2_slice_size
; /* Number of entries in a slice of the L2 table */
951 bool use_lazy_refcounts
;
953 bool discard_passthrough
[QCOW2_DISCARD_MAX
];
954 uint64_t cache_clean_interval
;
955 QCryptoBlockOpenOptions
*crypto_opts
; /* Disk encryption runtime options */
958 static int qcow2_update_options_prepare(BlockDriverState
*bs
,
960 QDict
*options
, int flags
,
963 BDRVQcow2State
*s
= bs
->opaque
;
964 QemuOpts
*opts
= NULL
;
965 const char *opt_overlap_check
, *opt_overlap_check_template
;
966 int overlap_check_template
= 0;
967 uint64_t l2_cache_size
, l2_cache_entry_size
, refcount_cache_size
;
969 const char *encryptfmt
;
970 QDict
*encryptopts
= NULL
;
971 Error
*local_err
= NULL
;
974 qdict_extract_subqdict(options
, &encryptopts
, "encrypt.");
975 encryptfmt
= qdict_get_try_str(encryptopts
, "format");
977 opts
= qemu_opts_create(&qcow2_runtime_opts
, NULL
, 0, &error_abort
);
978 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
980 error_propagate(errp
, local_err
);
985 /* get L2 table/refcount block cache size from command line options */
986 read_cache_sizes(bs
, opts
, &l2_cache_size
, &l2_cache_entry_size
,
987 &refcount_cache_size
, &local_err
);
989 error_propagate(errp
, local_err
);
994 l2_cache_size
/= l2_cache_entry_size
;
995 if (l2_cache_size
< MIN_L2_CACHE_SIZE
) {
996 l2_cache_size
= MIN_L2_CACHE_SIZE
;
998 if (l2_cache_size
> INT_MAX
) {
999 error_setg(errp
, "L2 cache size too big");
1004 refcount_cache_size
/= s
->cluster_size
;
1005 if (refcount_cache_size
< MIN_REFCOUNT_CACHE_SIZE
) {
1006 refcount_cache_size
= MIN_REFCOUNT_CACHE_SIZE
;
1008 if (refcount_cache_size
> INT_MAX
) {
1009 error_setg(errp
, "Refcount cache size too big");
1014 /* alloc new L2 table/refcount block cache, flush old one */
1015 if (s
->l2_table_cache
) {
1016 ret
= qcow2_cache_flush(bs
, s
->l2_table_cache
);
1018 error_setg_errno(errp
, -ret
, "Failed to flush the L2 table cache");
1023 if (s
->refcount_block_cache
) {
1024 ret
= qcow2_cache_flush(bs
, s
->refcount_block_cache
);
1026 error_setg_errno(errp
, -ret
,
1027 "Failed to flush the refcount block cache");
1032 r
->l2_slice_size
= l2_cache_entry_size
/ sizeof(uint64_t);
1033 r
->l2_table_cache
= qcow2_cache_create(bs
, l2_cache_size
,
1034 l2_cache_entry_size
);
1035 r
->refcount_block_cache
= qcow2_cache_create(bs
, refcount_cache_size
,
1037 if (r
->l2_table_cache
== NULL
|| r
->refcount_block_cache
== NULL
) {
1038 error_setg(errp
, "Could not allocate metadata caches");
1043 /* New interval for cache cleanup timer */
1044 r
->cache_clean_interval
=
1045 qemu_opt_get_number(opts
, QCOW2_OPT_CACHE_CLEAN_INTERVAL
,
1046 DEFAULT_CACHE_CLEAN_INTERVAL
);
1047 #ifndef CONFIG_LINUX
1048 if (r
->cache_clean_interval
!= 0) {
1049 error_setg(errp
, QCOW2_OPT_CACHE_CLEAN_INTERVAL
1050 " not supported on this host");
1055 if (r
->cache_clean_interval
> UINT_MAX
) {
1056 error_setg(errp
, "Cache clean interval too big");
1061 /* lazy-refcounts; flush if going from enabled to disabled */
1062 r
->use_lazy_refcounts
= qemu_opt_get_bool(opts
, QCOW2_OPT_LAZY_REFCOUNTS
,
1063 (s
->compatible_features
& QCOW2_COMPAT_LAZY_REFCOUNTS
));
1064 if (r
->use_lazy_refcounts
&& s
->qcow_version
< 3) {
1065 error_setg(errp
, "Lazy refcounts require a qcow2 image with at least "
1066 "qemu 1.1 compatibility level");
1071 if (s
->use_lazy_refcounts
&& !r
->use_lazy_refcounts
) {
1072 ret
= qcow2_mark_clean(bs
);
1074 error_setg_errno(errp
, -ret
, "Failed to disable lazy refcounts");
1079 /* Overlap check options */
1080 opt_overlap_check
= qemu_opt_get(opts
, QCOW2_OPT_OVERLAP
);
1081 opt_overlap_check_template
= qemu_opt_get(opts
, QCOW2_OPT_OVERLAP_TEMPLATE
);
1082 if (opt_overlap_check_template
&& opt_overlap_check
&&
1083 strcmp(opt_overlap_check_template
, opt_overlap_check
))
1085 error_setg(errp
, "Conflicting values for qcow2 options '"
1086 QCOW2_OPT_OVERLAP
"' ('%s') and '" QCOW2_OPT_OVERLAP_TEMPLATE
1087 "' ('%s')", opt_overlap_check
, opt_overlap_check_template
);
1091 if (!opt_overlap_check
) {
1092 opt_overlap_check
= opt_overlap_check_template
?: "cached";
1095 if (!strcmp(opt_overlap_check
, "none")) {
1096 overlap_check_template
= 0;
1097 } else if (!strcmp(opt_overlap_check
, "constant")) {
1098 overlap_check_template
= QCOW2_OL_CONSTANT
;
1099 } else if (!strcmp(opt_overlap_check
, "cached")) {
1100 overlap_check_template
= QCOW2_OL_CACHED
;
1101 } else if (!strcmp(opt_overlap_check
, "all")) {
1102 overlap_check_template
= QCOW2_OL_ALL
;
1104 error_setg(errp
, "Unsupported value '%s' for qcow2 option "
1105 "'overlap-check'. Allowed are any of the following: "
1106 "none, constant, cached, all", opt_overlap_check
);
1111 r
->overlap_check
= 0;
1112 for (i
= 0; i
< QCOW2_OL_MAX_BITNR
; i
++) {
1113 /* overlap-check defines a template bitmask, but every flag may be
1114 * overwritten through the associated boolean option */
1116 qemu_opt_get_bool(opts
, overlap_bool_option_names
[i
],
1117 overlap_check_template
& (1 << i
)) << i
;
1120 r
->discard_passthrough
[QCOW2_DISCARD_NEVER
] = false;
1121 r
->discard_passthrough
[QCOW2_DISCARD_ALWAYS
] = true;
1122 r
->discard_passthrough
[QCOW2_DISCARD_REQUEST
] =
1123 qemu_opt_get_bool(opts
, QCOW2_OPT_DISCARD_REQUEST
,
1124 flags
& BDRV_O_UNMAP
);
1125 r
->discard_passthrough
[QCOW2_DISCARD_SNAPSHOT
] =
1126 qemu_opt_get_bool(opts
, QCOW2_OPT_DISCARD_SNAPSHOT
, true);
1127 r
->discard_passthrough
[QCOW2_DISCARD_OTHER
] =
1128 qemu_opt_get_bool(opts
, QCOW2_OPT_DISCARD_OTHER
, false);
1130 switch (s
->crypt_method_header
) {
1131 case QCOW_CRYPT_NONE
:
1133 error_setg(errp
, "No encryption in image header, but options "
1134 "specified format '%s'", encryptfmt
);
1140 case QCOW_CRYPT_AES
:
1141 if (encryptfmt
&& !g_str_equal(encryptfmt
, "aes")) {
1143 "Header reported 'aes' encryption format but "
1144 "options specify '%s'", encryptfmt
);
1148 qdict_put_str(encryptopts
, "format", "qcow");
1149 r
->crypto_opts
= block_crypto_open_opts_init(encryptopts
, errp
);
1152 case QCOW_CRYPT_LUKS
:
1153 if (encryptfmt
&& !g_str_equal(encryptfmt
, "luks")) {
1155 "Header reported 'luks' encryption format but "
1156 "options specify '%s'", encryptfmt
);
1160 qdict_put_str(encryptopts
, "format", "luks");
1161 r
->crypto_opts
= block_crypto_open_opts_init(encryptopts
, errp
);
1165 error_setg(errp
, "Unsupported encryption method %d",
1166 s
->crypt_method_header
);
1169 if (s
->crypt_method_header
!= QCOW_CRYPT_NONE
&& !r
->crypto_opts
) {
1176 qobject_unref(encryptopts
);
1177 qemu_opts_del(opts
);
1182 static void qcow2_update_options_commit(BlockDriverState
*bs
,
1183 Qcow2ReopenState
*r
)
1185 BDRVQcow2State
*s
= bs
->opaque
;
1188 if (s
->l2_table_cache
) {
1189 qcow2_cache_destroy(s
->l2_table_cache
);
1191 if (s
->refcount_block_cache
) {
1192 qcow2_cache_destroy(s
->refcount_block_cache
);
1194 s
->l2_table_cache
= r
->l2_table_cache
;
1195 s
->refcount_block_cache
= r
->refcount_block_cache
;
1196 s
->l2_slice_size
= r
->l2_slice_size
;
1198 s
->overlap_check
= r
->overlap_check
;
1199 s
->use_lazy_refcounts
= r
->use_lazy_refcounts
;
1201 for (i
= 0; i
< QCOW2_DISCARD_MAX
; i
++) {
1202 s
->discard_passthrough
[i
] = r
->discard_passthrough
[i
];
1205 if (s
->cache_clean_interval
!= r
->cache_clean_interval
) {
1206 cache_clean_timer_del(bs
);
1207 s
->cache_clean_interval
= r
->cache_clean_interval
;
1208 cache_clean_timer_init(bs
, bdrv_get_aio_context(bs
));
1211 qapi_free_QCryptoBlockOpenOptions(s
->crypto_opts
);
1212 s
->crypto_opts
= r
->crypto_opts
;
1215 static void qcow2_update_options_abort(BlockDriverState
*bs
,
1216 Qcow2ReopenState
*r
)
1218 if (r
->l2_table_cache
) {
1219 qcow2_cache_destroy(r
->l2_table_cache
);
1221 if (r
->refcount_block_cache
) {
1222 qcow2_cache_destroy(r
->refcount_block_cache
);
1224 qapi_free_QCryptoBlockOpenOptions(r
->crypto_opts
);
1227 static int qcow2_update_options(BlockDriverState
*bs
, QDict
*options
,
1228 int flags
, Error
**errp
)
1230 Qcow2ReopenState r
= {};
1233 ret
= qcow2_update_options_prepare(bs
, &r
, options
, flags
, errp
);
1235 qcow2_update_options_commit(bs
, &r
);
1237 qcow2_update_options_abort(bs
, &r
);
1243 /* Called with s->lock held. */
1244 static int coroutine_fn
qcow2_do_open(BlockDriverState
*bs
, QDict
*options
,
1245 int flags
, Error
**errp
)
1247 BDRVQcow2State
*s
= bs
->opaque
;
1248 unsigned int len
, i
;
1251 Error
*local_err
= NULL
;
1253 uint64_t l1_vm_state_index
;
1254 bool update_header
= false;
1256 ret
= bdrv_pread(bs
->file
, 0, &header
, sizeof(header
));
1258 error_setg_errno(errp
, -ret
, "Could not read qcow2 header");
1261 header
.magic
= be32_to_cpu(header
.magic
);
1262 header
.version
= be32_to_cpu(header
.version
);
1263 header
.backing_file_offset
= be64_to_cpu(header
.backing_file_offset
);
1264 header
.backing_file_size
= be32_to_cpu(header
.backing_file_size
);
1265 header
.size
= be64_to_cpu(header
.size
);
1266 header
.cluster_bits
= be32_to_cpu(header
.cluster_bits
);
1267 header
.crypt_method
= be32_to_cpu(header
.crypt_method
);
1268 header
.l1_table_offset
= be64_to_cpu(header
.l1_table_offset
);
1269 header
.l1_size
= be32_to_cpu(header
.l1_size
);
1270 header
.refcount_table_offset
= be64_to_cpu(header
.refcount_table_offset
);
1271 header
.refcount_table_clusters
=
1272 be32_to_cpu(header
.refcount_table_clusters
);
1273 header
.snapshots_offset
= be64_to_cpu(header
.snapshots_offset
);
1274 header
.nb_snapshots
= be32_to_cpu(header
.nb_snapshots
);
1276 if (header
.magic
!= QCOW_MAGIC
) {
1277 error_setg(errp
, "Image is not in qcow2 format");
1281 if (header
.version
< 2 || header
.version
> 3) {
1282 error_setg(errp
, "Unsupported qcow2 version %" PRIu32
, header
.version
);
1287 s
->qcow_version
= header
.version
;
1289 /* Initialise cluster size */
1290 if (header
.cluster_bits
< MIN_CLUSTER_BITS
||
1291 header
.cluster_bits
> MAX_CLUSTER_BITS
) {
1292 error_setg(errp
, "Unsupported cluster size: 2^%" PRIu32
,
1293 header
.cluster_bits
);
1298 s
->cluster_bits
= header
.cluster_bits
;
1299 s
->cluster_size
= 1 << s
->cluster_bits
;
1301 /* Initialise version 3 header fields */
1302 if (header
.version
== 2) {
1303 header
.incompatible_features
= 0;
1304 header
.compatible_features
= 0;
1305 header
.autoclear_features
= 0;
1306 header
.refcount_order
= 4;
1307 header
.header_length
= 72;
1309 header
.incompatible_features
=
1310 be64_to_cpu(header
.incompatible_features
);
1311 header
.compatible_features
= be64_to_cpu(header
.compatible_features
);
1312 header
.autoclear_features
= be64_to_cpu(header
.autoclear_features
);
1313 header
.refcount_order
= be32_to_cpu(header
.refcount_order
);
1314 header
.header_length
= be32_to_cpu(header
.header_length
);
1316 if (header
.header_length
< 104) {
1317 error_setg(errp
, "qcow2 header too short");
1323 if (header
.header_length
> s
->cluster_size
) {
1324 error_setg(errp
, "qcow2 header exceeds cluster size");
1329 if (header
.header_length
> sizeof(header
)) {
1330 s
->unknown_header_fields_size
= header
.header_length
- sizeof(header
);
1331 s
->unknown_header_fields
= g_malloc(s
->unknown_header_fields_size
);
1332 ret
= bdrv_pread(bs
->file
, sizeof(header
), s
->unknown_header_fields
,
1333 s
->unknown_header_fields_size
);
1335 error_setg_errno(errp
, -ret
, "Could not read unknown qcow2 header "
1341 if (header
.backing_file_offset
> s
->cluster_size
) {
1342 error_setg(errp
, "Invalid backing file offset");
1347 if (header
.backing_file_offset
) {
1348 ext_end
= header
.backing_file_offset
;
1350 ext_end
= 1 << header
.cluster_bits
;
1353 /* Handle feature bits */
1354 s
->incompatible_features
= header
.incompatible_features
;
1355 s
->compatible_features
= header
.compatible_features
;
1356 s
->autoclear_features
= header
.autoclear_features
;
1358 if (s
->incompatible_features
& ~QCOW2_INCOMPAT_MASK
) {
1359 void *feature_table
= NULL
;
1360 qcow2_read_extensions(bs
, header
.header_length
, ext_end
,
1361 &feature_table
, flags
, NULL
, NULL
);
1362 report_unsupported_feature(errp
, feature_table
,
1363 s
->incompatible_features
&
1364 ~QCOW2_INCOMPAT_MASK
);
1366 g_free(feature_table
);
1370 if (s
->incompatible_features
& QCOW2_INCOMPAT_CORRUPT
) {
1371 /* Corrupt images may not be written to unless they are being repaired
1373 if ((flags
& BDRV_O_RDWR
) && !(flags
& BDRV_O_CHECK
)) {
1374 error_setg(errp
, "qcow2: Image is corrupt; cannot be opened "
1381 /* Check support for various header values */
1382 if (header
.refcount_order
> 6) {
1383 error_setg(errp
, "Reference count entry width too large; may not "
1388 s
->refcount_order
= header
.refcount_order
;
1389 s
->refcount_bits
= 1 << s
->refcount_order
;
1390 s
->refcount_max
= UINT64_C(1) << (s
->refcount_bits
- 1);
1391 s
->refcount_max
+= s
->refcount_max
- 1;
1393 s
->crypt_method_header
= header
.crypt_method
;
1394 if (s
->crypt_method_header
) {
1395 if (bdrv_uses_whitelist() &&
1396 s
->crypt_method_header
== QCOW_CRYPT_AES
) {
1398 "Use of AES-CBC encrypted qcow2 images is no longer "
1399 "supported in system emulators");
1400 error_append_hint(errp
,
1401 "You can use 'qemu-img convert' to convert your "
1402 "image to an alternative supported format, such "
1403 "as unencrypted qcow2, or raw with the LUKS "
1404 "format instead.\n");
1409 if (s
->crypt_method_header
== QCOW_CRYPT_AES
) {
1410 s
->crypt_physical_offset
= false;
1412 /* Assuming LUKS and any future crypt methods we
1413 * add will all use physical offsets, due to the
1414 * fact that the alternative is insecure... */
1415 s
->crypt_physical_offset
= true;
1418 bs
->encrypted
= true;
1421 s
->l2_bits
= s
->cluster_bits
- 3; /* L2 is always one cluster */
1422 s
->l2_size
= 1 << s
->l2_bits
;
1423 /* 2^(s->refcount_order - 3) is the refcount width in bytes */
1424 s
->refcount_block_bits
= s
->cluster_bits
- (s
->refcount_order
- 3);
1425 s
->refcount_block_size
= 1 << s
->refcount_block_bits
;
1426 bs
->total_sectors
= header
.size
/ BDRV_SECTOR_SIZE
;
1427 s
->csize_shift
= (62 - (s
->cluster_bits
- 8));
1428 s
->csize_mask
= (1 << (s
->cluster_bits
- 8)) - 1;
1429 s
->cluster_offset_mask
= (1LL << s
->csize_shift
) - 1;
1431 s
->refcount_table_offset
= header
.refcount_table_offset
;
1432 s
->refcount_table_size
=
1433 header
.refcount_table_clusters
<< (s
->cluster_bits
- 3);
1435 if (header
.refcount_table_clusters
== 0 && !(flags
& BDRV_O_CHECK
)) {
1436 error_setg(errp
, "Image does not contain a reference count table");
1441 ret
= qcow2_validate_table(bs
, s
->refcount_table_offset
,
1442 header
.refcount_table_clusters
,
1443 s
->cluster_size
, QCOW_MAX_REFTABLE_SIZE
,
1444 "Reference count table", errp
);
1449 if (!(flags
& BDRV_O_CHECK
)) {
1451 * The total size in bytes of the snapshot table is checked in
1452 * qcow2_read_snapshots() because the size of each snapshot is
1453 * variable and we don't know it yet.
1454 * Here we only check the offset and number of snapshots.
1456 ret
= qcow2_validate_table(bs
, header
.snapshots_offset
,
1457 header
.nb_snapshots
,
1458 sizeof(QCowSnapshotHeader
),
1459 sizeof(QCowSnapshotHeader
) *
1461 "Snapshot table", errp
);
1467 /* read the level 1 table */
1468 ret
= qcow2_validate_table(bs
, header
.l1_table_offset
,
1469 header
.l1_size
, sizeof(uint64_t),
1470 QCOW_MAX_L1_SIZE
, "Active L1 table", errp
);
1474 s
->l1_size
= header
.l1_size
;
1475 s
->l1_table_offset
= header
.l1_table_offset
;
1477 l1_vm_state_index
= size_to_l1(s
, header
.size
);
1478 if (l1_vm_state_index
> INT_MAX
) {
1479 error_setg(errp
, "Image is too big");
1483 s
->l1_vm_state_index
= l1_vm_state_index
;
1485 /* the L1 table must contain at least enough entries to put
1486 header.size bytes */
1487 if (s
->l1_size
< s
->l1_vm_state_index
) {
1488 error_setg(errp
, "L1 table is too small");
1493 if (s
->l1_size
> 0) {
1494 s
->l1_table
= qemu_try_blockalign(bs
->file
->bs
,
1495 ROUND_UP(s
->l1_size
* sizeof(uint64_t), 512));
1496 if (s
->l1_table
== NULL
) {
1497 error_setg(errp
, "Could not allocate L1 table");
1501 ret
= bdrv_pread(bs
->file
, s
->l1_table_offset
, s
->l1_table
,
1502 s
->l1_size
* sizeof(uint64_t));
1504 error_setg_errno(errp
, -ret
, "Could not read L1 table");
1507 for(i
= 0;i
< s
->l1_size
; i
++) {
1508 s
->l1_table
[i
] = be64_to_cpu(s
->l1_table
[i
]);
1512 /* Parse driver-specific options */
1513 ret
= qcow2_update_options(bs
, options
, flags
, errp
);
1520 ret
= qcow2_refcount_init(bs
);
1522 error_setg_errno(errp
, -ret
, "Could not initialize refcount handling");
1526 QLIST_INIT(&s
->cluster_allocs
);
1527 QTAILQ_INIT(&s
->discards
);
1529 /* read qcow2 extensions */
1530 if (qcow2_read_extensions(bs
, header
.header_length
, ext_end
, NULL
,
1531 flags
, &update_header
, &local_err
)) {
1532 error_propagate(errp
, local_err
);
1537 /* Open external data file */
1538 s
->data_file
= bdrv_open_child(NULL
, options
, "data-file", bs
, &child_file
,
1541 error_propagate(errp
, local_err
);
1546 if (s
->incompatible_features
& QCOW2_INCOMPAT_DATA_FILE
) {
1547 if (!s
->data_file
&& s
->image_data_file
) {
1548 s
->data_file
= bdrv_open_child(s
->image_data_file
, options
,
1549 "data-file", bs
, &child_file
,
1551 if (!s
->data_file
) {
1556 if (!s
->data_file
) {
1557 error_setg(errp
, "'data-file' is required for this image");
1563 error_setg(errp
, "'data-file' can only be set for images with an "
1564 "external data file");
1569 s
->data_file
= bs
->file
;
1571 if (data_file_is_raw(bs
)) {
1572 error_setg(errp
, "data-file-raw requires a data file");
1578 /* qcow2_read_extension may have set up the crypto context
1579 * if the crypt method needs a header region, some methods
1580 * don't need header extensions, so must check here
1582 if (s
->crypt_method_header
&& !s
->crypto
) {
1583 if (s
->crypt_method_header
== QCOW_CRYPT_AES
) {
1584 unsigned int cflags
= 0;
1585 if (flags
& BDRV_O_NO_IO
) {
1586 cflags
|= QCRYPTO_BLOCK_OPEN_NO_IO
;
1588 s
->crypto
= qcrypto_block_open(s
->crypto_opts
, "encrypt.",
1590 QCOW2_MAX_THREADS
, errp
);
1595 } else if (!(flags
& BDRV_O_NO_IO
)) {
1596 error_setg(errp
, "Missing CRYPTO header for crypt method %d",
1597 s
->crypt_method_header
);
1603 /* read the backing file name */
1604 if (header
.backing_file_offset
!= 0) {
1605 len
= header
.backing_file_size
;
1606 if (len
> MIN(1023, s
->cluster_size
- header
.backing_file_offset
) ||
1607 len
>= sizeof(bs
->backing_file
)) {
1608 error_setg(errp
, "Backing file name too long");
1612 ret
= bdrv_pread(bs
->file
, header
.backing_file_offset
,
1613 bs
->auto_backing_file
, len
);
1615 error_setg_errno(errp
, -ret
, "Could not read backing file name");
1618 bs
->auto_backing_file
[len
] = '\0';
1619 pstrcpy(bs
->backing_file
, sizeof(bs
->backing_file
),
1620 bs
->auto_backing_file
);
1621 s
->image_backing_file
= g_strdup(bs
->auto_backing_file
);
1625 * Internal snapshots; skip reading them in check mode, because
1626 * we do not need them then, and we do not want to abort because
1627 * of a broken table.
1629 if (!(flags
& BDRV_O_CHECK
)) {
1630 s
->snapshots_offset
= header
.snapshots_offset
;
1631 s
->nb_snapshots
= header
.nb_snapshots
;
1633 ret
= qcow2_read_snapshots(bs
, errp
);
1639 /* Clear unknown autoclear feature bits */
1640 update_header
|= s
->autoclear_features
& ~QCOW2_AUTOCLEAR_MASK
;
1642 update_header
&& !bs
->read_only
&& !(flags
& BDRV_O_INACTIVE
);
1643 if (update_header
) {
1644 s
->autoclear_features
&= QCOW2_AUTOCLEAR_MASK
;
1647 /* == Handle persistent dirty bitmaps ==
1649 * We want load dirty bitmaps in three cases:
1651 * 1. Normal open of the disk in active mode, not related to invalidation
1654 * 2. Invalidation of the target vm after pre-copy phase of migration, if
1655 * bitmaps are _not_ migrating through migration channel, i.e.
1656 * 'dirty-bitmaps' capability is disabled.
1658 * 3. Invalidation of source vm after failed or canceled migration.
1659 * This is a very interesting case. There are two possible types of
1662 * A. Stored on inactivation and removed. They should be loaded from the
1665 * B. Not stored: not-persistent bitmaps and bitmaps, migrated through
1666 * the migration channel (with dirty-bitmaps capability).
1668 * On the other hand, there are two possible sub-cases:
1670 * 3.1 disk was changed by somebody else while were inactive. In this
1671 * case all in-RAM dirty bitmaps (both persistent and not) are
1672 * definitely invalid. And we don't have any method to determine
1675 * Simple and safe thing is to just drop all the bitmaps of type B on
1676 * inactivation. But in this case we lose bitmaps in valid 4.2 case.
1678 * On the other hand, resuming source vm, if disk was already changed
1679 * is a bad thing anyway: not only bitmaps, the whole vm state is
1680 * out of sync with disk.
1682 * This means, that user or management tool, who for some reason
1683 * decided to resume source vm, after disk was already changed by
1684 * target vm, should at least drop all dirty bitmaps by hand.
1686 * So, we can ignore this case for now, but TODO: "generation"
1687 * extension for qcow2, to determine, that image was changed after
1688 * last inactivation. And if it is changed, we will drop (or at least
1689 * mark as 'invalid' all the bitmaps of type B, both persistent
1692 * 3.2 disk was _not_ changed while were inactive. Bitmaps may be saved
1693 * to disk ('dirty-bitmaps' capability disabled), or not saved
1694 * ('dirty-bitmaps' capability enabled), but we don't need to care
1695 * of: let's load bitmaps as always: stored bitmaps will be loaded,
1696 * and not stored has flag IN_USE=1 in the image and will be skipped
1699 * One remaining possible case when we don't want load bitmaps:
1701 * 4. Open disk in inactive mode in target vm (bitmaps are migrating or
1702 * will be loaded on invalidation, no needs try loading them before)
1705 if (!(bdrv_get_flags(bs
) & BDRV_O_INACTIVE
)) {
1706 /* It's case 1, 2 or 3.2. Or 3.1 which is BUG in management layer. */
1707 bool header_updated
= qcow2_load_dirty_bitmaps(bs
, &local_err
);
1708 if (local_err
!= NULL
) {
1709 error_propagate(errp
, local_err
);
1714 update_header
= update_header
&& !header_updated
;
1717 if (update_header
) {
1718 ret
= qcow2_update_header(bs
);
1720 error_setg_errno(errp
, -ret
, "Could not update qcow2 header");
1725 bs
->supported_zero_flags
= header
.version
>= 3 ?
1726 BDRV_REQ_MAY_UNMAP
| BDRV_REQ_NO_FALLBACK
: 0;
1728 /* Repair image if dirty */
1729 if (!(flags
& (BDRV_O_CHECK
| BDRV_O_INACTIVE
)) && !bs
->read_only
&&
1730 (s
->incompatible_features
& QCOW2_INCOMPAT_DIRTY
)) {
1731 BdrvCheckResult result
= {0};
1733 ret
= qcow2_co_check_locked(bs
, &result
,
1734 BDRV_FIX_ERRORS
| BDRV_FIX_LEAKS
);
1735 if (ret
< 0 || result
.check_errors
) {
1739 error_setg_errno(errp
, -ret
, "Could not repair dirty image");
1746 BdrvCheckResult result
= {0};
1747 qcow2_check_refcounts(bs
, &result
, 0);
1751 qemu_co_queue_init(&s
->thread_task_queue
);
1756 g_free(s
->image_data_file
);
1757 if (has_data_file(bs
)) {
1758 bdrv_unref_child(bs
, s
->data_file
);
1760 g_free(s
->unknown_header_fields
);
1761 cleanup_unknown_header_ext(bs
);
1762 qcow2_free_snapshots(bs
);
1763 qcow2_refcount_close(bs
);
1764 qemu_vfree(s
->l1_table
);
1765 /* else pre-write overlap checks in cache_destroy may crash */
1767 cache_clean_timer_del(bs
);
1768 if (s
->l2_table_cache
) {
1769 qcow2_cache_destroy(s
->l2_table_cache
);
1771 if (s
->refcount_block_cache
) {
1772 qcow2_cache_destroy(s
->refcount_block_cache
);
1774 qcrypto_block_free(s
->crypto
);
1775 qapi_free_QCryptoBlockOpenOptions(s
->crypto_opts
);
1779 typedef struct QCow2OpenCo
{
1780 BlockDriverState
*bs
;
1787 static void coroutine_fn
qcow2_open_entry(void *opaque
)
1789 QCow2OpenCo
*qoc
= opaque
;
1790 BDRVQcow2State
*s
= qoc
->bs
->opaque
;
1792 qemu_co_mutex_lock(&s
->lock
);
1793 qoc
->ret
= qcow2_do_open(qoc
->bs
, qoc
->options
, qoc
->flags
, qoc
->errp
);
1794 qemu_co_mutex_unlock(&s
->lock
);
1797 static int qcow2_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
1800 BDRVQcow2State
*s
= bs
->opaque
;
1809 bs
->file
= bdrv_open_child(NULL
, options
, "file", bs
, &child_file
,
1815 /* Initialise locks */
1816 qemu_co_mutex_init(&s
->lock
);
1818 if (qemu_in_coroutine()) {
1819 /* From bdrv_co_create. */
1820 qcow2_open_entry(&qoc
);
1822 assert(qemu_get_current_aio_context() == qemu_get_aio_context());
1823 qemu_coroutine_enter(qemu_coroutine_create(qcow2_open_entry
, &qoc
));
1824 BDRV_POLL_WHILE(bs
, qoc
.ret
== -EINPROGRESS
);
1829 static void qcow2_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
1831 BDRVQcow2State
*s
= bs
->opaque
;
1833 if (bs
->encrypted
) {
1834 /* Encryption works on a sector granularity */
1835 bs
->bl
.request_alignment
= qcrypto_block_get_sector_size(s
->crypto
);
1837 bs
->bl
.pwrite_zeroes_alignment
= s
->cluster_size
;
1838 bs
->bl
.pdiscard_alignment
= s
->cluster_size
;
1841 static int qcow2_reopen_prepare(BDRVReopenState
*state
,
1842 BlockReopenQueue
*queue
, Error
**errp
)
1844 Qcow2ReopenState
*r
;
1847 r
= g_new0(Qcow2ReopenState
, 1);
1850 ret
= qcow2_update_options_prepare(state
->bs
, r
, state
->options
,
1851 state
->flags
, errp
);
1856 /* We need to write out any unwritten data if we reopen read-only. */
1857 if ((state
->flags
& BDRV_O_RDWR
) == 0) {
1858 ret
= qcow2_reopen_bitmaps_ro(state
->bs
, errp
);
1863 ret
= bdrv_flush(state
->bs
);
1868 ret
= qcow2_mark_clean(state
->bs
);
1877 qcow2_update_options_abort(state
->bs
, r
);
1882 static void qcow2_reopen_commit(BDRVReopenState
*state
)
1884 qcow2_update_options_commit(state
->bs
, state
->opaque
);
1885 if (state
->flags
& BDRV_O_RDWR
) {
1886 Error
*local_err
= NULL
;
1888 if (qcow2_reopen_bitmaps_rw(state
->bs
, &local_err
) < 0) {
1890 * This is not fatal, bitmaps just left read-only, so all following
1891 * writes will fail. User can remove read-only bitmaps to unblock
1892 * writes or retry reopen.
1894 error_reportf_err(local_err
,
1895 "%s: Failed to make dirty bitmaps writable: ",
1896 bdrv_get_node_name(state
->bs
));
1899 g_free(state
->opaque
);
1902 static void qcow2_reopen_abort(BDRVReopenState
*state
)
1904 qcow2_update_options_abort(state
->bs
, state
->opaque
);
1905 g_free(state
->opaque
);
1908 static void qcow2_join_options(QDict
*options
, QDict
*old_options
)
1910 bool has_new_overlap_template
=
1911 qdict_haskey(options
, QCOW2_OPT_OVERLAP
) ||
1912 qdict_haskey(options
, QCOW2_OPT_OVERLAP_TEMPLATE
);
1913 bool has_new_total_cache_size
=
1914 qdict_haskey(options
, QCOW2_OPT_CACHE_SIZE
);
1915 bool has_all_cache_options
;
1917 /* New overlap template overrides all old overlap options */
1918 if (has_new_overlap_template
) {
1919 qdict_del(old_options
, QCOW2_OPT_OVERLAP
);
1920 qdict_del(old_options
, QCOW2_OPT_OVERLAP_TEMPLATE
);
1921 qdict_del(old_options
, QCOW2_OPT_OVERLAP_MAIN_HEADER
);
1922 qdict_del(old_options
, QCOW2_OPT_OVERLAP_ACTIVE_L1
);
1923 qdict_del(old_options
, QCOW2_OPT_OVERLAP_ACTIVE_L2
);
1924 qdict_del(old_options
, QCOW2_OPT_OVERLAP_REFCOUNT_TABLE
);
1925 qdict_del(old_options
, QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK
);
1926 qdict_del(old_options
, QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE
);
1927 qdict_del(old_options
, QCOW2_OPT_OVERLAP_INACTIVE_L1
);
1928 qdict_del(old_options
, QCOW2_OPT_OVERLAP_INACTIVE_L2
);
1931 /* New total cache size overrides all old options */
1932 if (qdict_haskey(options
, QCOW2_OPT_CACHE_SIZE
)) {
1933 qdict_del(old_options
, QCOW2_OPT_L2_CACHE_SIZE
);
1934 qdict_del(old_options
, QCOW2_OPT_REFCOUNT_CACHE_SIZE
);
1937 qdict_join(options
, old_options
, false);
1940 * If after merging all cache size options are set, an old total size is
1941 * overwritten. Do keep all options, however, if all three are new. The
1942 * resulting error message is what we want to happen.
1944 has_all_cache_options
=
1945 qdict_haskey(options
, QCOW2_OPT_CACHE_SIZE
) ||
1946 qdict_haskey(options
, QCOW2_OPT_L2_CACHE_SIZE
) ||
1947 qdict_haskey(options
, QCOW2_OPT_REFCOUNT_CACHE_SIZE
);
1949 if (has_all_cache_options
&& !has_new_total_cache_size
) {
1950 qdict_del(options
, QCOW2_OPT_CACHE_SIZE
);
1954 static int coroutine_fn
qcow2_co_block_status(BlockDriverState
*bs
,
1956 int64_t offset
, int64_t count
,
1957 int64_t *pnum
, int64_t *map
,
1958 BlockDriverState
**file
)
1960 BDRVQcow2State
*s
= bs
->opaque
;
1961 uint64_t cluster_offset
;
1963 int ret
, status
= 0;
1965 qemu_co_mutex_lock(&s
->lock
);
1967 if (!s
->metadata_preallocation_checked
) {
1968 ret
= qcow2_detect_metadata_preallocation(bs
);
1969 s
->metadata_preallocation
= (ret
== 1);
1970 s
->metadata_preallocation_checked
= true;
1973 bytes
= MIN(INT_MAX
, count
);
1974 ret
= qcow2_get_cluster_offset(bs
, offset
, &bytes
, &cluster_offset
);
1975 qemu_co_mutex_unlock(&s
->lock
);
1982 if ((ret
== QCOW2_CLUSTER_NORMAL
|| ret
== QCOW2_CLUSTER_ZERO_ALLOC
) &&
1984 *map
= cluster_offset
| offset_into_cluster(s
, offset
);
1985 *file
= s
->data_file
->bs
;
1986 status
|= BDRV_BLOCK_OFFSET_VALID
;
1988 if (ret
== QCOW2_CLUSTER_ZERO_PLAIN
|| ret
== QCOW2_CLUSTER_ZERO_ALLOC
) {
1989 status
|= BDRV_BLOCK_ZERO
;
1990 } else if (ret
!= QCOW2_CLUSTER_UNALLOCATED
) {
1991 status
|= BDRV_BLOCK_DATA
;
1993 if (s
->metadata_preallocation
&& (status
& BDRV_BLOCK_DATA
) &&
1994 (status
& BDRV_BLOCK_OFFSET_VALID
))
1996 status
|= BDRV_BLOCK_RECURSE
;
2001 static coroutine_fn
int qcow2_handle_l2meta(BlockDriverState
*bs
,
2002 QCowL2Meta
**pl2meta
,
2006 QCowL2Meta
*l2meta
= *pl2meta
;
2008 while (l2meta
!= NULL
) {
2012 ret
= qcow2_alloc_cluster_link_l2(bs
, l2meta
);
2017 qcow2_alloc_cluster_abort(bs
, l2meta
);
2020 /* Take the request off the list of running requests */
2021 if (l2meta
->nb_clusters
!= 0) {
2022 QLIST_REMOVE(l2meta
, next_in_flight
);
2025 qemu_co_queue_restart_all(&l2meta
->dependent_requests
);
2027 next
= l2meta
->next
;
2036 static coroutine_fn
int
2037 qcow2_co_preadv_encrypted(BlockDriverState
*bs
,
2038 uint64_t file_cluster_offset
,
2042 uint64_t qiov_offset
)
2045 BDRVQcow2State
*s
= bs
->opaque
;
2048 assert(bs
->encrypted
&& s
->crypto
);
2049 assert(bytes
<= QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_size
);
2052 * For encrypted images, read everything into a temporary
2053 * contiguous buffer on which the AES functions can work.
2054 * Also, decryption in a separate buffer is better as it
2055 * prevents the guest from learning information about the
2056 * encrypted nature of the virtual disk.
2059 buf
= qemu_try_blockalign(s
->data_file
->bs
, bytes
);
2064 BLKDBG_EVENT(bs
->file
, BLKDBG_READ_AIO
);
2065 ret
= bdrv_co_pread(s
->data_file
,
2066 file_cluster_offset
+ offset_into_cluster(s
, offset
),
2072 assert(QEMU_IS_ALIGNED(offset
, BDRV_SECTOR_SIZE
));
2073 assert(QEMU_IS_ALIGNED(bytes
, BDRV_SECTOR_SIZE
));
2074 if (qcow2_co_decrypt(bs
,
2075 file_cluster_offset
+ offset_into_cluster(s
, offset
),
2076 offset
, buf
, bytes
) < 0)
2081 qemu_iovec_from_buf(qiov
, qiov_offset
, buf
, bytes
);
2089 typedef struct Qcow2AioTask
{
2092 BlockDriverState
*bs
;
2093 QCow2ClusterType cluster_type
; /* only for read */
2094 uint64_t file_cluster_offset
;
2098 uint64_t qiov_offset
;
2099 QCowL2Meta
*l2meta
; /* only for write */
2102 static coroutine_fn
int qcow2_co_preadv_task_entry(AioTask
*task
);
2103 static coroutine_fn
int qcow2_add_task(BlockDriverState
*bs
,
2106 QCow2ClusterType cluster_type
,
2107 uint64_t file_cluster_offset
,
2114 Qcow2AioTask local_task
;
2115 Qcow2AioTask
*task
= pool
? g_new(Qcow2AioTask
, 1) : &local_task
;
2117 *task
= (Qcow2AioTask
) {
2120 .cluster_type
= cluster_type
,
2122 .file_cluster_offset
= file_cluster_offset
,
2125 .qiov_offset
= qiov_offset
,
2129 trace_qcow2_add_task(qemu_coroutine_self(), bs
, pool
,
2130 func
== qcow2_co_preadv_task_entry
? "read" : "write",
2131 cluster_type
, file_cluster_offset
, offset
, bytes
,
2135 return func(&task
->task
);
2138 aio_task_pool_start_task(pool
, &task
->task
);
2143 static coroutine_fn
int qcow2_co_preadv_task(BlockDriverState
*bs
,
2144 QCow2ClusterType cluster_type
,
2145 uint64_t file_cluster_offset
,
2146 uint64_t offset
, uint64_t bytes
,
2150 BDRVQcow2State
*s
= bs
->opaque
;
2151 int offset_in_cluster
= offset_into_cluster(s
, offset
);
2153 switch (cluster_type
) {
2154 case QCOW2_CLUSTER_ZERO_PLAIN
:
2155 case QCOW2_CLUSTER_ZERO_ALLOC
:
2156 /* Both zero types are handled in qcow2_co_preadv_part */
2157 g_assert_not_reached();
2159 case QCOW2_CLUSTER_UNALLOCATED
:
2160 assert(bs
->backing
); /* otherwise handled in qcow2_co_preadv_part */
2162 BLKDBG_EVENT(bs
->file
, BLKDBG_READ_BACKING_AIO
);
2163 return bdrv_co_preadv_part(bs
->backing
, offset
, bytes
,
2164 qiov
, qiov_offset
, 0);
2166 case QCOW2_CLUSTER_COMPRESSED
:
2167 return qcow2_co_preadv_compressed(bs
, file_cluster_offset
,
2168 offset
, bytes
, qiov
, qiov_offset
);
2170 case QCOW2_CLUSTER_NORMAL
:
2171 if ((file_cluster_offset
& 511) != 0) {
2175 if (bs
->encrypted
) {
2176 return qcow2_co_preadv_encrypted(bs
, file_cluster_offset
,
2177 offset
, bytes
, qiov
, qiov_offset
);
2180 BLKDBG_EVENT(bs
->file
, BLKDBG_READ_AIO
);
2181 return bdrv_co_preadv_part(s
->data_file
,
2182 file_cluster_offset
+ offset_in_cluster
,
2183 bytes
, qiov
, qiov_offset
, 0);
2186 g_assert_not_reached();
2189 g_assert_not_reached();
2192 static coroutine_fn
int qcow2_co_preadv_task_entry(AioTask
*task
)
2194 Qcow2AioTask
*t
= container_of(task
, Qcow2AioTask
, task
);
2198 return qcow2_co_preadv_task(t
->bs
, t
->cluster_type
, t
->file_cluster_offset
,
2199 t
->offset
, t
->bytes
, t
->qiov
, t
->qiov_offset
);
2202 static coroutine_fn
int qcow2_co_preadv_part(BlockDriverState
*bs
,
2203 uint64_t offset
, uint64_t bytes
,
2205 size_t qiov_offset
, int flags
)
2207 BDRVQcow2State
*s
= bs
->opaque
;
2209 unsigned int cur_bytes
; /* number of bytes in current iteration */
2210 uint64_t cluster_offset
= 0;
2211 AioTaskPool
*aio
= NULL
;
2213 while (bytes
!= 0 && aio_task_pool_status(aio
) == 0) {
2214 /* prepare next request */
2215 cur_bytes
= MIN(bytes
, INT_MAX
);
2217 cur_bytes
= MIN(cur_bytes
,
2218 QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_size
);
2221 qemu_co_mutex_lock(&s
->lock
);
2222 ret
= qcow2_get_cluster_offset(bs
, offset
, &cur_bytes
, &cluster_offset
);
2223 qemu_co_mutex_unlock(&s
->lock
);
2228 if (ret
== QCOW2_CLUSTER_ZERO_PLAIN
||
2229 ret
== QCOW2_CLUSTER_ZERO_ALLOC
||
2230 (ret
== QCOW2_CLUSTER_UNALLOCATED
&& !bs
->backing
))
2232 qemu_iovec_memset(qiov
, qiov_offset
, 0, cur_bytes
);
2234 if (!aio
&& cur_bytes
!= bytes
) {
2235 aio
= aio_task_pool_new(QCOW2_MAX_WORKERS
);
2237 ret
= qcow2_add_task(bs
, aio
, qcow2_co_preadv_task_entry
, ret
,
2238 cluster_offset
, offset
, cur_bytes
,
2239 qiov
, qiov_offset
, NULL
);
2246 offset
+= cur_bytes
;
2247 qiov_offset
+= cur_bytes
;
2252 aio_task_pool_wait_all(aio
);
2254 ret
= aio_task_pool_status(aio
);
2262 /* Check if it's possible to merge a write request with the writing of
2263 * the data from the COW regions */
2264 static bool merge_cow(uint64_t offset
, unsigned bytes
,
2265 QEMUIOVector
*qiov
, size_t qiov_offset
,
2270 for (m
= l2meta
; m
!= NULL
; m
= m
->next
) {
2271 /* If both COW regions are empty then there's nothing to merge */
2272 if (m
->cow_start
.nb_bytes
== 0 && m
->cow_end
.nb_bytes
== 0) {
2276 /* If COW regions are handled already, skip this too */
2281 /* The data (middle) region must be immediately after the
2283 if (l2meta_cow_start(m
) + m
->cow_start
.nb_bytes
!= offset
) {
2287 /* The end region must be immediately after the data (middle)
2289 if (m
->offset
+ m
->cow_end
.offset
!= offset
+ bytes
) {
2293 /* Make sure that adding both COW regions to the QEMUIOVector
2294 * does not exceed IOV_MAX */
2295 if (qemu_iovec_subvec_niov(qiov
, qiov_offset
, bytes
) > IOV_MAX
- 2) {
2299 m
->data_qiov
= qiov
;
2300 m
->data_qiov_offset
= qiov_offset
;
2307 static bool is_unallocated(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
)
2311 (!bdrv_is_allocated_above(bs
, NULL
, false, offset
, bytes
, &nr
) &&
2315 static bool is_zero_cow(BlockDriverState
*bs
, QCowL2Meta
*m
)
2318 * This check is designed for optimization shortcut so it must be
2320 * Instead of is_zero(), use is_unallocated() as it is faster (but not
2321 * as accurate and can result in false negatives).
2323 return is_unallocated(bs
, m
->offset
+ m
->cow_start
.offset
,
2324 m
->cow_start
.nb_bytes
) &&
2325 is_unallocated(bs
, m
->offset
+ m
->cow_end
.offset
,
2326 m
->cow_end
.nb_bytes
);
2329 static int handle_alloc_space(BlockDriverState
*bs
, QCowL2Meta
*l2meta
)
2331 BDRVQcow2State
*s
= bs
->opaque
;
2334 if (!(s
->data_file
->bs
->supported_zero_flags
& BDRV_REQ_NO_FALLBACK
)) {
2338 if (bs
->encrypted
) {
2342 for (m
= l2meta
; m
!= NULL
; m
= m
->next
) {
2345 if (!m
->cow_start
.nb_bytes
&& !m
->cow_end
.nb_bytes
) {
2349 if (!is_zero_cow(bs
, m
)) {
2354 * instead of writing zero COW buffers,
2355 * efficiently zero out the whole clusters
2358 ret
= qcow2_pre_write_overlap_check(bs
, 0, m
->alloc_offset
,
2359 m
->nb_clusters
* s
->cluster_size
,
2365 BLKDBG_EVENT(bs
->file
, BLKDBG_CLUSTER_ALLOC_SPACE
);
2366 ret
= bdrv_co_pwrite_zeroes(s
->data_file
, m
->alloc_offset
,
2367 m
->nb_clusters
* s
->cluster_size
,
2368 BDRV_REQ_NO_FALLBACK
);
2370 if (ret
!= -ENOTSUP
&& ret
!= -EAGAIN
) {
2376 trace_qcow2_skip_cow(qemu_coroutine_self(), m
->offset
, m
->nb_clusters
);
2383 * qcow2_co_pwritev_task
2384 * Called with s->lock unlocked
2385 * l2meta - if not NULL, qcow2_co_pwritev_task() will consume it. Caller must
2386 * not use it somehow after qcow2_co_pwritev_task() call
2388 static coroutine_fn
int qcow2_co_pwritev_task(BlockDriverState
*bs
,
2389 uint64_t file_cluster_offset
,
2390 uint64_t offset
, uint64_t bytes
,
2392 uint64_t qiov_offset
,
2396 BDRVQcow2State
*s
= bs
->opaque
;
2397 void *crypt_buf
= NULL
;
2398 int offset_in_cluster
= offset_into_cluster(s
, offset
);
2399 QEMUIOVector encrypted_qiov
;
2401 if (bs
->encrypted
) {
2403 assert(bytes
<= QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_size
);
2404 crypt_buf
= qemu_try_blockalign(bs
->file
->bs
, bytes
);
2405 if (crypt_buf
== NULL
) {
2409 qemu_iovec_to_buf(qiov
, qiov_offset
, crypt_buf
, bytes
);
2411 if (qcow2_co_encrypt(bs
, file_cluster_offset
+ offset_in_cluster
,
2412 offset
, crypt_buf
, bytes
) < 0)
2418 qemu_iovec_init_buf(&encrypted_qiov
, crypt_buf
, bytes
);
2419 qiov
= &encrypted_qiov
;
2423 /* Try to efficiently initialize the physical space with zeroes */
2424 ret
= handle_alloc_space(bs
, l2meta
);
2430 * If we need to do COW, check if it's possible to merge the
2431 * writing of the guest data together with that of the COW regions.
2432 * If it's not possible (or not necessary) then write the
2435 if (!merge_cow(offset
, bytes
, qiov
, qiov_offset
, l2meta
)) {
2436 BLKDBG_EVENT(bs
->file
, BLKDBG_WRITE_AIO
);
2437 trace_qcow2_writev_data(qemu_coroutine_self(),
2438 file_cluster_offset
+ offset_in_cluster
);
2439 ret
= bdrv_co_pwritev_part(s
->data_file
,
2440 file_cluster_offset
+ offset_in_cluster
,
2441 bytes
, qiov
, qiov_offset
, 0);
2447 qemu_co_mutex_lock(&s
->lock
);
2449 ret
= qcow2_handle_l2meta(bs
, &l2meta
, true);
2453 qemu_co_mutex_lock(&s
->lock
);
2456 qcow2_handle_l2meta(bs
, &l2meta
, false);
2457 qemu_co_mutex_unlock(&s
->lock
);
2459 qemu_vfree(crypt_buf
);
2464 static coroutine_fn
int qcow2_co_pwritev_task_entry(AioTask
*task
)
2466 Qcow2AioTask
*t
= container_of(task
, Qcow2AioTask
, task
);
2468 assert(!t
->cluster_type
);
2470 return qcow2_co_pwritev_task(t
->bs
, t
->file_cluster_offset
,
2471 t
->offset
, t
->bytes
, t
->qiov
, t
->qiov_offset
,
2475 static coroutine_fn
int qcow2_co_pwritev_part(
2476 BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
2477 QEMUIOVector
*qiov
, size_t qiov_offset
, int flags
)
2479 BDRVQcow2State
*s
= bs
->opaque
;
2480 int offset_in_cluster
;
2482 unsigned int cur_bytes
; /* number of sectors in current iteration */
2483 uint64_t cluster_offset
;
2484 QCowL2Meta
*l2meta
= NULL
;
2485 AioTaskPool
*aio
= NULL
;
2487 trace_qcow2_writev_start_req(qemu_coroutine_self(), offset
, bytes
);
2489 while (bytes
!= 0 && aio_task_pool_status(aio
) == 0) {
2493 trace_qcow2_writev_start_part(qemu_coroutine_self());
2494 offset_in_cluster
= offset_into_cluster(s
, offset
);
2495 cur_bytes
= MIN(bytes
, INT_MAX
);
2496 if (bs
->encrypted
) {
2497 cur_bytes
= MIN(cur_bytes
,
2498 QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_size
2499 - offset_in_cluster
);
2502 qemu_co_mutex_lock(&s
->lock
);
2504 ret
= qcow2_alloc_cluster_offset(bs
, offset
, &cur_bytes
,
2505 &cluster_offset
, &l2meta
);
2510 assert((cluster_offset
& 511) == 0);
2512 ret
= qcow2_pre_write_overlap_check(bs
, 0,
2513 cluster_offset
+ offset_in_cluster
,
2519 qemu_co_mutex_unlock(&s
->lock
);
2521 if (!aio
&& cur_bytes
!= bytes
) {
2522 aio
= aio_task_pool_new(QCOW2_MAX_WORKERS
);
2524 ret
= qcow2_add_task(bs
, aio
, qcow2_co_pwritev_task_entry
, 0,
2525 cluster_offset
, offset
, cur_bytes
,
2526 qiov
, qiov_offset
, l2meta
);
2527 l2meta
= NULL
; /* l2meta is consumed by qcow2_co_pwritev_task() */
2533 offset
+= cur_bytes
;
2534 qiov_offset
+= cur_bytes
;
2535 trace_qcow2_writev_done_part(qemu_coroutine_self(), cur_bytes
);
2539 qemu_co_mutex_lock(&s
->lock
);
2542 qcow2_handle_l2meta(bs
, &l2meta
, false);
2544 qemu_co_mutex_unlock(&s
->lock
);
2548 aio_task_pool_wait_all(aio
);
2550 ret
= aio_task_pool_status(aio
);
2555 trace_qcow2_writev_done_req(qemu_coroutine_self(), ret
);
2560 static int qcow2_inactivate(BlockDriverState
*bs
)
2562 BDRVQcow2State
*s
= bs
->opaque
;
2563 int ret
, result
= 0;
2564 Error
*local_err
= NULL
;
2566 qcow2_store_persistent_dirty_bitmaps(bs
, true, &local_err
);
2567 if (local_err
!= NULL
) {
2569 error_reportf_err(local_err
, "Lost persistent bitmaps during "
2570 "inactivation of node '%s': ",
2571 bdrv_get_device_or_node_name(bs
));
2574 ret
= qcow2_cache_flush(bs
, s
->l2_table_cache
);
2577 error_report("Failed to flush the L2 table cache: %s",
2581 ret
= qcow2_cache_flush(bs
, s
->refcount_block_cache
);
2584 error_report("Failed to flush the refcount block cache: %s",
2589 qcow2_mark_clean(bs
);
2595 static void qcow2_close(BlockDriverState
*bs
)
2597 BDRVQcow2State
*s
= bs
->opaque
;
2598 qemu_vfree(s
->l1_table
);
2599 /* else pre-write overlap checks in cache_destroy may crash */
2602 if (!(s
->flags
& BDRV_O_INACTIVE
)) {
2603 qcow2_inactivate(bs
);
2606 cache_clean_timer_del(bs
);
2607 qcow2_cache_destroy(s
->l2_table_cache
);
2608 qcow2_cache_destroy(s
->refcount_block_cache
);
2610 qcrypto_block_free(s
->crypto
);
2613 g_free(s
->unknown_header_fields
);
2614 cleanup_unknown_header_ext(bs
);
2616 g_free(s
->image_data_file
);
2617 g_free(s
->image_backing_file
);
2618 g_free(s
->image_backing_format
);
2620 if (has_data_file(bs
)) {
2621 bdrv_unref_child(bs
, s
->data_file
);
2624 qcow2_refcount_close(bs
);
2625 qcow2_free_snapshots(bs
);
2628 static void coroutine_fn
qcow2_co_invalidate_cache(BlockDriverState
*bs
,
2631 BDRVQcow2State
*s
= bs
->opaque
;
2632 int flags
= s
->flags
;
2633 QCryptoBlock
*crypto
= NULL
;
2635 Error
*local_err
= NULL
;
2639 * Backing files are read-only which makes all of their metadata immutable,
2640 * that means we don't have to worry about reopening them here.
2648 memset(s
, 0, sizeof(BDRVQcow2State
));
2649 options
= qdict_clone_shallow(bs
->options
);
2651 flags
&= ~BDRV_O_INACTIVE
;
2652 qemu_co_mutex_lock(&s
->lock
);
2653 ret
= qcow2_do_open(bs
, options
, flags
, &local_err
);
2654 qemu_co_mutex_unlock(&s
->lock
);
2655 qobject_unref(options
);
2657 error_propagate_prepend(errp
, local_err
,
2658 "Could not reopen qcow2 layer: ");
2661 } else if (ret
< 0) {
2662 error_setg_errno(errp
, -ret
, "Could not reopen qcow2 layer");
2670 static size_t header_ext_add(char *buf
, uint32_t magic
, const void *s
,
2671 size_t len
, size_t buflen
)
2673 QCowExtension
*ext_backing_fmt
= (QCowExtension
*) buf
;
2674 size_t ext_len
= sizeof(QCowExtension
) + ((len
+ 7) & ~7);
2676 if (buflen
< ext_len
) {
2680 *ext_backing_fmt
= (QCowExtension
) {
2681 .magic
= cpu_to_be32(magic
),
2682 .len
= cpu_to_be32(len
),
2686 memcpy(buf
+ sizeof(QCowExtension
), s
, len
);
2693 * Updates the qcow2 header, including the variable length parts of it, i.e.
2694 * the backing file name and all extensions. qcow2 was not designed to allow
2695 * such changes, so if we run out of space (we can only use the first cluster)
2696 * this function may fail.
2698 * Returns 0 on success, -errno in error cases.
2700 int qcow2_update_header(BlockDriverState
*bs
)
2702 BDRVQcow2State
*s
= bs
->opaque
;
2705 size_t buflen
= s
->cluster_size
;
2707 uint64_t total_size
;
2708 uint32_t refcount_table_clusters
;
2709 size_t header_length
;
2710 Qcow2UnknownHeaderExtension
*uext
;
2712 buf
= qemu_blockalign(bs
, buflen
);
2714 /* Header structure */
2715 header
= (QCowHeader
*) buf
;
2717 if (buflen
< sizeof(*header
)) {
2722 header_length
= sizeof(*header
) + s
->unknown_header_fields_size
;
2723 total_size
= bs
->total_sectors
* BDRV_SECTOR_SIZE
;
2724 refcount_table_clusters
= s
->refcount_table_size
>> (s
->cluster_bits
- 3);
2726 *header
= (QCowHeader
) {
2727 /* Version 2 fields */
2728 .magic
= cpu_to_be32(QCOW_MAGIC
),
2729 .version
= cpu_to_be32(s
->qcow_version
),
2730 .backing_file_offset
= 0,
2731 .backing_file_size
= 0,
2732 .cluster_bits
= cpu_to_be32(s
->cluster_bits
),
2733 .size
= cpu_to_be64(total_size
),
2734 .crypt_method
= cpu_to_be32(s
->crypt_method_header
),
2735 .l1_size
= cpu_to_be32(s
->l1_size
),
2736 .l1_table_offset
= cpu_to_be64(s
->l1_table_offset
),
2737 .refcount_table_offset
= cpu_to_be64(s
->refcount_table_offset
),
2738 .refcount_table_clusters
= cpu_to_be32(refcount_table_clusters
),
2739 .nb_snapshots
= cpu_to_be32(s
->nb_snapshots
),
2740 .snapshots_offset
= cpu_to_be64(s
->snapshots_offset
),
2742 /* Version 3 fields */
2743 .incompatible_features
= cpu_to_be64(s
->incompatible_features
),
2744 .compatible_features
= cpu_to_be64(s
->compatible_features
),
2745 .autoclear_features
= cpu_to_be64(s
->autoclear_features
),
2746 .refcount_order
= cpu_to_be32(s
->refcount_order
),
2747 .header_length
= cpu_to_be32(header_length
),
2750 /* For older versions, write a shorter header */
2751 switch (s
->qcow_version
) {
2753 ret
= offsetof(QCowHeader
, incompatible_features
);
2756 ret
= sizeof(*header
);
2765 memset(buf
, 0, buflen
);
2767 /* Preserve any unknown field in the header */
2768 if (s
->unknown_header_fields_size
) {
2769 if (buflen
< s
->unknown_header_fields_size
) {
2774 memcpy(buf
, s
->unknown_header_fields
, s
->unknown_header_fields_size
);
2775 buf
+= s
->unknown_header_fields_size
;
2776 buflen
-= s
->unknown_header_fields_size
;
2779 /* Backing file format header extension */
2780 if (s
->image_backing_format
) {
2781 ret
= header_ext_add(buf
, QCOW2_EXT_MAGIC_BACKING_FORMAT
,
2782 s
->image_backing_format
,
2783 strlen(s
->image_backing_format
),
2793 /* External data file header extension */
2794 if (has_data_file(bs
) && s
->image_data_file
) {
2795 ret
= header_ext_add(buf
, QCOW2_EXT_MAGIC_DATA_FILE
,
2796 s
->image_data_file
, strlen(s
->image_data_file
),
2806 /* Full disk encryption header pointer extension */
2807 if (s
->crypto_header
.offset
!= 0) {
2808 s
->crypto_header
.offset
= cpu_to_be64(s
->crypto_header
.offset
);
2809 s
->crypto_header
.length
= cpu_to_be64(s
->crypto_header
.length
);
2810 ret
= header_ext_add(buf
, QCOW2_EXT_MAGIC_CRYPTO_HEADER
,
2811 &s
->crypto_header
, sizeof(s
->crypto_header
),
2813 s
->crypto_header
.offset
= be64_to_cpu(s
->crypto_header
.offset
);
2814 s
->crypto_header
.length
= be64_to_cpu(s
->crypto_header
.length
);
2823 if (s
->qcow_version
>= 3) {
2824 Qcow2Feature features
[] = {
2826 .type
= QCOW2_FEAT_TYPE_INCOMPATIBLE
,
2827 .bit
= QCOW2_INCOMPAT_DIRTY_BITNR
,
2828 .name
= "dirty bit",
2831 .type
= QCOW2_FEAT_TYPE_INCOMPATIBLE
,
2832 .bit
= QCOW2_INCOMPAT_CORRUPT_BITNR
,
2833 .name
= "corrupt bit",
2836 .type
= QCOW2_FEAT_TYPE_INCOMPATIBLE
,
2837 .bit
= QCOW2_INCOMPAT_DATA_FILE_BITNR
,
2838 .name
= "external data file",
2841 .type
= QCOW2_FEAT_TYPE_COMPATIBLE
,
2842 .bit
= QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR
,
2843 .name
= "lazy refcounts",
2847 ret
= header_ext_add(buf
, QCOW2_EXT_MAGIC_FEATURE_TABLE
,
2848 features
, sizeof(features
), buflen
);
2856 /* Bitmap extension */
2857 if (s
->nb_bitmaps
> 0) {
2858 Qcow2BitmapHeaderExt bitmaps_header
= {
2859 .nb_bitmaps
= cpu_to_be32(s
->nb_bitmaps
),
2860 .bitmap_directory_size
=
2861 cpu_to_be64(s
->bitmap_directory_size
),
2862 .bitmap_directory_offset
=
2863 cpu_to_be64(s
->bitmap_directory_offset
)
2865 ret
= header_ext_add(buf
, QCOW2_EXT_MAGIC_BITMAPS
,
2866 &bitmaps_header
, sizeof(bitmaps_header
),
2875 /* Keep unknown header extensions */
2876 QLIST_FOREACH(uext
, &s
->unknown_header_ext
, next
) {
2877 ret
= header_ext_add(buf
, uext
->magic
, uext
->data
, uext
->len
, buflen
);
2886 /* End of header extensions */
2887 ret
= header_ext_add(buf
, QCOW2_EXT_MAGIC_END
, NULL
, 0, buflen
);
2895 /* Backing file name */
2896 if (s
->image_backing_file
) {
2897 size_t backing_file_len
= strlen(s
->image_backing_file
);
2899 if (buflen
< backing_file_len
) {
2904 /* Using strncpy is ok here, since buf is not NUL-terminated. */
2905 strncpy(buf
, s
->image_backing_file
, buflen
);
2907 header
->backing_file_offset
= cpu_to_be64(buf
- ((char*) header
));
2908 header
->backing_file_size
= cpu_to_be32(backing_file_len
);
2911 /* Write the new header */
2912 ret
= bdrv_pwrite(bs
->file
, 0, header
, s
->cluster_size
);
2923 static int qcow2_change_backing_file(BlockDriverState
*bs
,
2924 const char *backing_file
, const char *backing_fmt
)
2926 BDRVQcow2State
*s
= bs
->opaque
;
2928 /* Adding a backing file means that the external data file alone won't be
2929 * enough to make sense of the content */
2930 if (backing_file
&& data_file_is_raw(bs
)) {
2934 if (backing_file
&& strlen(backing_file
) > 1023) {
2938 pstrcpy(bs
->auto_backing_file
, sizeof(bs
->auto_backing_file
),
2939 backing_file
?: "");
2940 pstrcpy(bs
->backing_file
, sizeof(bs
->backing_file
), backing_file
?: "");
2941 pstrcpy(bs
->backing_format
, sizeof(bs
->backing_format
), backing_fmt
?: "");
2943 g_free(s
->image_backing_file
);
2944 g_free(s
->image_backing_format
);
2946 s
->image_backing_file
= backing_file
? g_strdup(bs
->backing_file
) : NULL
;
2947 s
->image_backing_format
= backing_fmt
? g_strdup(bs
->backing_format
) : NULL
;
2949 return qcow2_update_header(bs
);
2952 static int qcow2_crypt_method_from_format(const char *encryptfmt
)
2954 if (g_str_equal(encryptfmt
, "luks")) {
2955 return QCOW_CRYPT_LUKS
;
2956 } else if (g_str_equal(encryptfmt
, "aes")) {
2957 return QCOW_CRYPT_AES
;
2963 static int qcow2_set_up_encryption(BlockDriverState
*bs
,
2964 QCryptoBlockCreateOptions
*cryptoopts
,
2967 BDRVQcow2State
*s
= bs
->opaque
;
2968 QCryptoBlock
*crypto
= NULL
;
2971 switch (cryptoopts
->format
) {
2972 case Q_CRYPTO_BLOCK_FORMAT_LUKS
:
2973 fmt
= QCOW_CRYPT_LUKS
;
2975 case Q_CRYPTO_BLOCK_FORMAT_QCOW
:
2976 fmt
= QCOW_CRYPT_AES
;
2979 error_setg(errp
, "Crypto format not supported in qcow2");
2983 s
->crypt_method_header
= fmt
;
2985 crypto
= qcrypto_block_create(cryptoopts
, "encrypt.",
2986 qcow2_crypto_hdr_init_func
,
2987 qcow2_crypto_hdr_write_func
,
2993 ret
= qcow2_update_header(bs
);
2995 error_setg_errno(errp
, -ret
, "Could not write encryption header");
3001 qcrypto_block_free(crypto
);
3006 * Preallocates metadata structures for data clusters between @offset (in the
3007 * guest disk) and @new_length (which is thus generally the new guest disk
3010 * Returns: 0 on success, -errno on failure.
3012 static int coroutine_fn
preallocate_co(BlockDriverState
*bs
, uint64_t offset
,
3013 uint64_t new_length
, PreallocMode mode
,
3016 BDRVQcow2State
*s
= bs
->opaque
;
3018 uint64_t host_offset
= 0;
3019 int64_t file_length
;
3020 unsigned int cur_bytes
;
3024 assert(offset
<= new_length
);
3025 bytes
= new_length
- offset
;
3028 cur_bytes
= MIN(bytes
, QEMU_ALIGN_DOWN(INT_MAX
, s
->cluster_size
));
3029 ret
= qcow2_alloc_cluster_offset(bs
, offset
, &cur_bytes
,
3030 &host_offset
, &meta
);
3032 error_setg_errno(errp
, -ret
, "Allocating clusters failed");
3037 QCowL2Meta
*next
= meta
->next
;
3039 ret
= qcow2_alloc_cluster_link_l2(bs
, meta
);
3041 error_setg_errno(errp
, -ret
, "Mapping clusters failed");
3042 qcow2_free_any_clusters(bs
, meta
->alloc_offset
,
3043 meta
->nb_clusters
, QCOW2_DISCARD_NEVER
);
3047 /* There are no dependent requests, but we need to remove our
3048 * request from the list of in-flight requests */
3049 QLIST_REMOVE(meta
, next_in_flight
);
3055 /* TODO Preallocate data if requested */
3058 offset
+= cur_bytes
;
3062 * It is expected that the image file is large enough to actually contain
3063 * all of the allocated clusters (otherwise we get failing reads after
3064 * EOF). Extend the image to the last allocated sector.
3066 file_length
= bdrv_getlength(s
->data_file
->bs
);
3067 if (file_length
< 0) {
3068 error_setg_errno(errp
, -file_length
, "Could not get file size");
3072 if (host_offset
+ cur_bytes
> file_length
) {
3073 if (mode
== PREALLOC_MODE_METADATA
) {
3074 mode
= PREALLOC_MODE_OFF
;
3076 ret
= bdrv_co_truncate(s
->data_file
, host_offset
+ cur_bytes
, false,
3086 /* qcow2_refcount_metadata_size:
3087 * @clusters: number of clusters to refcount (including data and L1/L2 tables)
3088 * @cluster_size: size of a cluster, in bytes
3089 * @refcount_order: refcount bits power-of-2 exponent
3090 * @generous_increase: allow for the refcount table to be 1.5x as large as it
3093 * Returns: Number of bytes required for refcount blocks and table metadata.
3095 int64_t qcow2_refcount_metadata_size(int64_t clusters
, size_t cluster_size
,
3096 int refcount_order
, bool generous_increase
,
3097 uint64_t *refblock_count
)
3100 * Every host cluster is reference-counted, including metadata (even
3101 * refcount metadata is recursively included).
3103 * An accurate formula for the size of refcount metadata size is difficult
3104 * to derive. An easier method of calculation is finding the fixed point
3105 * where no further refcount blocks or table clusters are required to
3106 * reference count every cluster.
3108 int64_t blocks_per_table_cluster
= cluster_size
/ sizeof(uint64_t);
3109 int64_t refcounts_per_block
= cluster_size
* 8 / (1 << refcount_order
);
3110 int64_t table
= 0; /* number of refcount table clusters */
3111 int64_t blocks
= 0; /* number of refcount block clusters */
3117 blocks
= DIV_ROUND_UP(clusters
+ table
+ blocks
, refcounts_per_block
);
3118 table
= DIV_ROUND_UP(blocks
, blocks_per_table_cluster
);
3119 n
= clusters
+ blocks
+ table
;
3121 if (n
== last
&& generous_increase
) {
3122 clusters
+= DIV_ROUND_UP(table
, 2);
3123 n
= 0; /* force another loop */
3124 generous_increase
= false;
3126 } while (n
!= last
);
3128 if (refblock_count
) {
3129 *refblock_count
= blocks
;
3132 return (blocks
+ table
) * cluster_size
;
3136 * qcow2_calc_prealloc_size:
3137 * @total_size: virtual disk size in bytes
3138 * @cluster_size: cluster size in bytes
3139 * @refcount_order: refcount bits power-of-2 exponent
3141 * Returns: Total number of bytes required for the fully allocated image
3142 * (including metadata).
3144 static int64_t qcow2_calc_prealloc_size(int64_t total_size
,
3145 size_t cluster_size
,
3148 int64_t meta_size
= 0;
3149 uint64_t nl1e
, nl2e
;
3150 int64_t aligned_total_size
= ROUND_UP(total_size
, cluster_size
);
3152 /* header: 1 cluster */
3153 meta_size
+= cluster_size
;
3155 /* total size of L2 tables */
3156 nl2e
= aligned_total_size
/ cluster_size
;
3157 nl2e
= ROUND_UP(nl2e
, cluster_size
/ sizeof(uint64_t));
3158 meta_size
+= nl2e
* sizeof(uint64_t);
3160 /* total size of L1 tables */
3161 nl1e
= nl2e
* sizeof(uint64_t) / cluster_size
;
3162 nl1e
= ROUND_UP(nl1e
, cluster_size
/ sizeof(uint64_t));
3163 meta_size
+= nl1e
* sizeof(uint64_t);
3165 /* total size of refcount table and blocks */
3166 meta_size
+= qcow2_refcount_metadata_size(
3167 (meta_size
+ aligned_total_size
) / cluster_size
,
3168 cluster_size
, refcount_order
, false, NULL
);
3170 return meta_size
+ aligned_total_size
;
3173 static bool validate_cluster_size(size_t cluster_size
, Error
**errp
)
3175 int cluster_bits
= ctz32(cluster_size
);
3176 if (cluster_bits
< MIN_CLUSTER_BITS
|| cluster_bits
> MAX_CLUSTER_BITS
||
3177 (1 << cluster_bits
) != cluster_size
)
3179 error_setg(errp
, "Cluster size must be a power of two between %d and "
3180 "%dk", 1 << MIN_CLUSTER_BITS
, 1 << (MAX_CLUSTER_BITS
- 10));
3186 static size_t qcow2_opt_get_cluster_size_del(QemuOpts
*opts
, Error
**errp
)
3188 size_t cluster_size
;
3190 cluster_size
= qemu_opt_get_size_del(opts
, BLOCK_OPT_CLUSTER_SIZE
,
3191 DEFAULT_CLUSTER_SIZE
);
3192 if (!validate_cluster_size(cluster_size
, errp
)) {
3195 return cluster_size
;
3198 static int qcow2_opt_get_version_del(QemuOpts
*opts
, Error
**errp
)
3203 buf
= qemu_opt_get_del(opts
, BLOCK_OPT_COMPAT_LEVEL
);
3205 ret
= 3; /* default */
3206 } else if (!strcmp(buf
, "0.10")) {
3208 } else if (!strcmp(buf
, "1.1")) {
3211 error_setg(errp
, "Invalid compatibility level: '%s'", buf
);
3218 static uint64_t qcow2_opt_get_refcount_bits_del(QemuOpts
*opts
, int version
,
3221 uint64_t refcount_bits
;
3223 refcount_bits
= qemu_opt_get_number_del(opts
, BLOCK_OPT_REFCOUNT_BITS
, 16);
3224 if (refcount_bits
> 64 || !is_power_of_2(refcount_bits
)) {
3225 error_setg(errp
, "Refcount width must be a power of two and may not "
3230 if (version
< 3 && refcount_bits
!= 16) {
3231 error_setg(errp
, "Different refcount widths than 16 bits require "
3232 "compatibility level 1.1 or above (use compat=1.1 or "
3237 return refcount_bits
;
3240 static int coroutine_fn
3241 qcow2_co_create(BlockdevCreateOptions
*create_options
, Error
**errp
)
3243 BlockdevCreateOptionsQcow2
*qcow2_opts
;
3247 * Open the image file and write a minimal qcow2 header.
3249 * We keep things simple and start with a zero-sized image. We also
3250 * do without refcount blocks or a L1 table for now. We'll fix the
3251 * inconsistency later.
3253 * We do need a refcount table because growing the refcount table means
3254 * allocating two new refcount blocks - the seconds of which would be at
3255 * 2 GB for 64k clusters, and we don't want to have a 2 GB initial file
3256 * size for any qcow2 image.
3258 BlockBackend
*blk
= NULL
;
3259 BlockDriverState
*bs
= NULL
;
3260 BlockDriverState
*data_bs
= NULL
;
3262 size_t cluster_size
;
3265 uint64_t* refcount_table
;
3266 Error
*local_err
= NULL
;
3269 assert(create_options
->driver
== BLOCKDEV_DRIVER_QCOW2
);
3270 qcow2_opts
= &create_options
->u
.qcow2
;
3272 bs
= bdrv_open_blockdev_ref(qcow2_opts
->file
, errp
);
3277 /* Validate options and set default values */
3278 if (!QEMU_IS_ALIGNED(qcow2_opts
->size
, BDRV_SECTOR_SIZE
)) {
3279 error_setg(errp
, "Image size must be a multiple of 512 bytes");
3284 if (qcow2_opts
->has_version
) {
3285 switch (qcow2_opts
->version
) {
3286 case BLOCKDEV_QCOW2_VERSION_V2
:
3289 case BLOCKDEV_QCOW2_VERSION_V3
:
3293 g_assert_not_reached();
3299 if (qcow2_opts
->has_cluster_size
) {
3300 cluster_size
= qcow2_opts
->cluster_size
;
3302 cluster_size
= DEFAULT_CLUSTER_SIZE
;
3305 if (!validate_cluster_size(cluster_size
, errp
)) {
3310 if (!qcow2_opts
->has_preallocation
) {
3311 qcow2_opts
->preallocation
= PREALLOC_MODE_OFF
;
3313 if (qcow2_opts
->has_backing_file
&&
3314 qcow2_opts
->preallocation
!= PREALLOC_MODE_OFF
)
3316 error_setg(errp
, "Backing file and preallocation cannot be used at "
3321 if (qcow2_opts
->has_backing_fmt
&& !qcow2_opts
->has_backing_file
) {
3322 error_setg(errp
, "Backing format cannot be used without backing file");
3327 if (!qcow2_opts
->has_lazy_refcounts
) {
3328 qcow2_opts
->lazy_refcounts
= false;
3330 if (version
< 3 && qcow2_opts
->lazy_refcounts
) {
3331 error_setg(errp
, "Lazy refcounts only supported with compatibility "
3332 "level 1.1 and above (use version=v3 or greater)");
3337 if (!qcow2_opts
->has_refcount_bits
) {
3338 qcow2_opts
->refcount_bits
= 16;
3340 if (qcow2_opts
->refcount_bits
> 64 ||
3341 !is_power_of_2(qcow2_opts
->refcount_bits
))
3343 error_setg(errp
, "Refcount width must be a power of two and may not "
3348 if (version
< 3 && qcow2_opts
->refcount_bits
!= 16) {
3349 error_setg(errp
, "Different refcount widths than 16 bits require "
3350 "compatibility level 1.1 or above (use version=v3 or "
3355 refcount_order
= ctz32(qcow2_opts
->refcount_bits
);
3357 if (qcow2_opts
->data_file_raw
&& !qcow2_opts
->data_file
) {
3358 error_setg(errp
, "data-file-raw requires data-file");
3362 if (qcow2_opts
->data_file_raw
&& qcow2_opts
->has_backing_file
) {
3363 error_setg(errp
, "Backing file and data-file-raw cannot be used at "
3369 if (qcow2_opts
->data_file
) {
3371 error_setg(errp
, "External data files are only supported with "
3372 "compatibility level 1.1 and above (use version=v3 or "
3377 data_bs
= bdrv_open_blockdev_ref(qcow2_opts
->data_file
, errp
);
3378 if (data_bs
== NULL
) {
3384 /* Create BlockBackend to write to the image */
3385 blk
= blk_new(bdrv_get_aio_context(bs
),
3386 BLK_PERM_WRITE
| BLK_PERM_RESIZE
, BLK_PERM_ALL
);
3387 ret
= blk_insert_bs(blk
, bs
, errp
);
3391 blk_set_allow_write_beyond_eof(blk
, true);
3393 /* Write the header */
3394 QEMU_BUILD_BUG_ON((1 << MIN_CLUSTER_BITS
) < sizeof(*header
));
3395 header
= g_malloc0(cluster_size
);
3396 *header
= (QCowHeader
) {
3397 .magic
= cpu_to_be32(QCOW_MAGIC
),
3398 .version
= cpu_to_be32(version
),
3399 .cluster_bits
= cpu_to_be32(ctz32(cluster_size
)),
3400 .size
= cpu_to_be64(0),
3401 .l1_table_offset
= cpu_to_be64(0),
3402 .l1_size
= cpu_to_be32(0),
3403 .refcount_table_offset
= cpu_to_be64(cluster_size
),
3404 .refcount_table_clusters
= cpu_to_be32(1),
3405 .refcount_order
= cpu_to_be32(refcount_order
),
3406 .header_length
= cpu_to_be32(sizeof(*header
)),
3409 /* We'll update this to correct value later */
3410 header
->crypt_method
= cpu_to_be32(QCOW_CRYPT_NONE
);
3412 if (qcow2_opts
->lazy_refcounts
) {
3413 header
->compatible_features
|=
3414 cpu_to_be64(QCOW2_COMPAT_LAZY_REFCOUNTS
);
3417 header
->incompatible_features
|=
3418 cpu_to_be64(QCOW2_INCOMPAT_DATA_FILE
);
3420 if (qcow2_opts
->data_file_raw
) {
3421 header
->autoclear_features
|=
3422 cpu_to_be64(QCOW2_AUTOCLEAR_DATA_FILE_RAW
);
3425 ret
= blk_pwrite(blk
, 0, header
, cluster_size
, 0);
3428 error_setg_errno(errp
, -ret
, "Could not write qcow2 header");
3432 /* Write a refcount table with one refcount block */
3433 refcount_table
= g_malloc0(2 * cluster_size
);
3434 refcount_table
[0] = cpu_to_be64(2 * cluster_size
);
3435 ret
= blk_pwrite(blk
, cluster_size
, refcount_table
, 2 * cluster_size
, 0);
3436 g_free(refcount_table
);
3439 error_setg_errno(errp
, -ret
, "Could not write refcount table");
3447 * And now open the image and make it consistent first (i.e. increase the
3448 * refcount of the cluster that is occupied by the header and the refcount
3451 options
= qdict_new();
3452 qdict_put_str(options
, "driver", "qcow2");
3453 qdict_put_str(options
, "file", bs
->node_name
);
3455 qdict_put_str(options
, "data-file", data_bs
->node_name
);
3457 blk
= blk_new_open(NULL
, NULL
, options
,
3458 BDRV_O_RDWR
| BDRV_O_RESIZE
| BDRV_O_NO_FLUSH
,
3461 error_propagate(errp
, local_err
);
3466 ret
= qcow2_alloc_clusters(blk_bs(blk
), 3 * cluster_size
);
3468 error_setg_errno(errp
, -ret
, "Could not allocate clusters for qcow2 "
3469 "header and refcount table");
3472 } else if (ret
!= 0) {
3473 error_report("Huh, first cluster in empty image is already in use?");
3477 /* Set the external data file if necessary */
3479 BDRVQcow2State
*s
= blk_bs(blk
)->opaque
;
3480 s
->image_data_file
= g_strdup(data_bs
->filename
);
3483 /* Create a full header (including things like feature table) */
3484 ret
= qcow2_update_header(blk_bs(blk
));
3486 error_setg_errno(errp
, -ret
, "Could not update qcow2 header");
3490 /* Okay, now that we have a valid image, let's give it the right size */
3491 ret
= blk_truncate(blk
, qcow2_opts
->size
, false, qcow2_opts
->preallocation
,
3494 error_prepend(errp
, "Could not resize image: ");
3498 /* Want a backing file? There you go.*/
3499 if (qcow2_opts
->has_backing_file
) {
3500 const char *backing_format
= NULL
;
3502 if (qcow2_opts
->has_backing_fmt
) {
3503 backing_format
= BlockdevDriver_str(qcow2_opts
->backing_fmt
);
3506 ret
= bdrv_change_backing_file(blk_bs(blk
), qcow2_opts
->backing_file
,
3509 error_setg_errno(errp
, -ret
, "Could not assign backing file '%s' "
3510 "with format '%s'", qcow2_opts
->backing_file
,
3516 /* Want encryption? There you go. */
3517 if (qcow2_opts
->has_encrypt
) {
3518 ret
= qcow2_set_up_encryption(blk_bs(blk
), qcow2_opts
->encrypt
, errp
);
3527 /* Reopen the image without BDRV_O_NO_FLUSH to flush it before returning.
3528 * Using BDRV_O_NO_IO, since encryption is now setup we don't want to
3529 * have to setup decryption context. We're not doing any I/O on the top
3530 * level BlockDriverState, only lower layers, where BDRV_O_NO_IO does
3533 options
= qdict_new();
3534 qdict_put_str(options
, "driver", "qcow2");
3535 qdict_put_str(options
, "file", bs
->node_name
);
3537 qdict_put_str(options
, "data-file", data_bs
->node_name
);
3539 blk
= blk_new_open(NULL
, NULL
, options
,
3540 BDRV_O_RDWR
| BDRV_O_NO_BACKING
| BDRV_O_NO_IO
,
3543 error_propagate(errp
, local_err
);
3552 bdrv_unref(data_bs
);
3556 static int coroutine_fn
qcow2_co_create_opts(const char *filename
, QemuOpts
*opts
,
3559 BlockdevCreateOptions
*create_options
= NULL
;
3562 BlockDriverState
*bs
= NULL
;
3563 BlockDriverState
*data_bs
= NULL
;
3564 Error
*local_err
= NULL
;
3568 /* Only the keyval visitor supports the dotted syntax needed for
3569 * encryption, so go through a QDict before getting a QAPI type. Ignore
3570 * options meant for the protocol layer so that the visitor doesn't
3572 qdict
= qemu_opts_to_qdict_filtered(opts
, NULL
, bdrv_qcow2
.create_opts
,
3575 /* Handle encryption options */
3576 val
= qdict_get_try_str(qdict
, BLOCK_OPT_ENCRYPT
);
3577 if (val
&& !strcmp(val
, "on")) {
3578 qdict_put_str(qdict
, BLOCK_OPT_ENCRYPT
, "qcow");
3579 } else if (val
&& !strcmp(val
, "off")) {
3580 qdict_del(qdict
, BLOCK_OPT_ENCRYPT
);
3583 val
= qdict_get_try_str(qdict
, BLOCK_OPT_ENCRYPT_FORMAT
);
3584 if (val
&& !strcmp(val
, "aes")) {
3585 qdict_put_str(qdict
, BLOCK_OPT_ENCRYPT_FORMAT
, "qcow");
3588 /* Convert compat=0.10/1.1 into compat=v2/v3, to be renamed into
3589 * version=v2/v3 below. */
3590 val
= qdict_get_try_str(qdict
, BLOCK_OPT_COMPAT_LEVEL
);
3591 if (val
&& !strcmp(val
, "0.10")) {
3592 qdict_put_str(qdict
, BLOCK_OPT_COMPAT_LEVEL
, "v2");
3593 } else if (val
&& !strcmp(val
, "1.1")) {
3594 qdict_put_str(qdict
, BLOCK_OPT_COMPAT_LEVEL
, "v3");
3597 /* Change legacy command line options into QMP ones */
3598 static const QDictRenames opt_renames
[] = {
3599 { BLOCK_OPT_BACKING_FILE
, "backing-file" },
3600 { BLOCK_OPT_BACKING_FMT
, "backing-fmt" },
3601 { BLOCK_OPT_CLUSTER_SIZE
, "cluster-size" },
3602 { BLOCK_OPT_LAZY_REFCOUNTS
, "lazy-refcounts" },
3603 { BLOCK_OPT_REFCOUNT_BITS
, "refcount-bits" },
3604 { BLOCK_OPT_ENCRYPT
, BLOCK_OPT_ENCRYPT_FORMAT
},
3605 { BLOCK_OPT_COMPAT_LEVEL
, "version" },
3606 { BLOCK_OPT_DATA_FILE_RAW
, "data-file-raw" },
3610 if (!qdict_rename_keys(qdict
, opt_renames
, errp
)) {
3615 /* Create and open the file (protocol layer) */
3616 ret
= bdrv_create_file(filename
, opts
, errp
);
3621 bs
= bdrv_open(filename
, NULL
, NULL
,
3622 BDRV_O_RDWR
| BDRV_O_RESIZE
| BDRV_O_PROTOCOL
, errp
);
3628 /* Create and open an external data file (protocol layer) */
3629 val
= qdict_get_try_str(qdict
, BLOCK_OPT_DATA_FILE
);
3631 ret
= bdrv_create_file(val
, opts
, errp
);
3636 data_bs
= bdrv_open(val
, NULL
, NULL
,
3637 BDRV_O_RDWR
| BDRV_O_RESIZE
| BDRV_O_PROTOCOL
,
3639 if (data_bs
== NULL
) {
3644 qdict_del(qdict
, BLOCK_OPT_DATA_FILE
);
3645 qdict_put_str(qdict
, "data-file", data_bs
->node_name
);
3648 /* Set 'driver' and 'node' options */
3649 qdict_put_str(qdict
, "driver", "qcow2");
3650 qdict_put_str(qdict
, "file", bs
->node_name
);
3652 /* Now get the QAPI type BlockdevCreateOptions */
3653 v
= qobject_input_visitor_new_flat_confused(qdict
, errp
);
3659 visit_type_BlockdevCreateOptions(v
, NULL
, &create_options
, &local_err
);
3663 error_propagate(errp
, local_err
);
3668 /* Silently round up size */
3669 create_options
->u
.qcow2
.size
= ROUND_UP(create_options
->u
.qcow2
.size
,
3672 /* Create the qcow2 image (format layer) */
3673 ret
= qcow2_co_create(create_options
, errp
);
3680 qobject_unref(qdict
);
3682 bdrv_unref(data_bs
);
3683 qapi_free_BlockdevCreateOptions(create_options
);
3688 static bool is_zero(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
)
3693 /* Clamp to image length, before checking status of underlying sectors */
3694 if (offset
+ bytes
> bs
->total_sectors
* BDRV_SECTOR_SIZE
) {
3695 bytes
= bs
->total_sectors
* BDRV_SECTOR_SIZE
- offset
;
3701 res
= bdrv_block_status_above(bs
, NULL
, offset
, bytes
, &nr
, NULL
, NULL
);
3702 return res
>= 0 && (res
& BDRV_BLOCK_ZERO
) && nr
== bytes
;
3705 static coroutine_fn
int qcow2_co_pwrite_zeroes(BlockDriverState
*bs
,
3706 int64_t offset
, int bytes
, BdrvRequestFlags flags
)
3709 BDRVQcow2State
*s
= bs
->opaque
;
3711 uint32_t head
= offset
% s
->cluster_size
;
3712 uint32_t tail
= (offset
+ bytes
) % s
->cluster_size
;
3714 trace_qcow2_pwrite_zeroes_start_req(qemu_coroutine_self(), offset
, bytes
);
3715 if (offset
+ bytes
== bs
->total_sectors
* BDRV_SECTOR_SIZE
) {
3723 assert(head
+ bytes
<= s
->cluster_size
);
3725 /* check whether remainder of cluster already reads as zero */
3726 if (!(is_zero(bs
, offset
- head
, head
) &&
3727 is_zero(bs
, offset
+ bytes
,
3728 tail
? s
->cluster_size
- tail
: 0))) {
3732 qemu_co_mutex_lock(&s
->lock
);
3733 /* We can have new write after previous check */
3734 offset
= QEMU_ALIGN_DOWN(offset
, s
->cluster_size
);
3735 bytes
= s
->cluster_size
;
3736 nr
= s
->cluster_size
;
3737 ret
= qcow2_get_cluster_offset(bs
, offset
, &nr
, &off
);
3738 if (ret
!= QCOW2_CLUSTER_UNALLOCATED
&&
3739 ret
!= QCOW2_CLUSTER_ZERO_PLAIN
&&
3740 ret
!= QCOW2_CLUSTER_ZERO_ALLOC
) {
3741 qemu_co_mutex_unlock(&s
->lock
);
3745 qemu_co_mutex_lock(&s
->lock
);
3748 trace_qcow2_pwrite_zeroes(qemu_coroutine_self(), offset
, bytes
);
3750 /* Whatever is left can use real zero clusters */
3751 ret
= qcow2_cluster_zeroize(bs
, offset
, bytes
, flags
);
3752 qemu_co_mutex_unlock(&s
->lock
);
3757 static coroutine_fn
int qcow2_co_pdiscard(BlockDriverState
*bs
,
3758 int64_t offset
, int bytes
)
3761 BDRVQcow2State
*s
= bs
->opaque
;
3763 if (!QEMU_IS_ALIGNED(offset
| bytes
, s
->cluster_size
)) {
3764 assert(bytes
< s
->cluster_size
);
3765 /* Ignore partial clusters, except for the special case of the
3766 * complete partial cluster at the end of an unaligned file */
3767 if (!QEMU_IS_ALIGNED(offset
, s
->cluster_size
) ||
3768 offset
+ bytes
!= bs
->total_sectors
* BDRV_SECTOR_SIZE
) {
3773 qemu_co_mutex_lock(&s
->lock
);
3774 ret
= qcow2_cluster_discard(bs
, offset
, bytes
, QCOW2_DISCARD_REQUEST
,
3776 qemu_co_mutex_unlock(&s
->lock
);
3780 static int coroutine_fn
3781 qcow2_co_copy_range_from(BlockDriverState
*bs
,
3782 BdrvChild
*src
, uint64_t src_offset
,
3783 BdrvChild
*dst
, uint64_t dst_offset
,
3784 uint64_t bytes
, BdrvRequestFlags read_flags
,
3785 BdrvRequestFlags write_flags
)
3787 BDRVQcow2State
*s
= bs
->opaque
;
3789 unsigned int cur_bytes
; /* number of bytes in current iteration */
3790 BdrvChild
*child
= NULL
;
3791 BdrvRequestFlags cur_write_flags
;
3793 assert(!bs
->encrypted
);
3794 qemu_co_mutex_lock(&s
->lock
);
3796 while (bytes
!= 0) {
3797 uint64_t copy_offset
= 0;
3798 /* prepare next request */
3799 cur_bytes
= MIN(bytes
, INT_MAX
);
3800 cur_write_flags
= write_flags
;
3802 ret
= qcow2_get_cluster_offset(bs
, src_offset
, &cur_bytes
, ©_offset
);
3808 case QCOW2_CLUSTER_UNALLOCATED
:
3809 if (bs
->backing
&& bs
->backing
->bs
) {
3810 int64_t backing_length
= bdrv_getlength(bs
->backing
->bs
);
3811 if (src_offset
>= backing_length
) {
3812 cur_write_flags
|= BDRV_REQ_ZERO_WRITE
;
3814 child
= bs
->backing
;
3815 cur_bytes
= MIN(cur_bytes
, backing_length
- src_offset
);
3816 copy_offset
= src_offset
;
3819 cur_write_flags
|= BDRV_REQ_ZERO_WRITE
;
3823 case QCOW2_CLUSTER_ZERO_PLAIN
:
3824 case QCOW2_CLUSTER_ZERO_ALLOC
:
3825 cur_write_flags
|= BDRV_REQ_ZERO_WRITE
;
3828 case QCOW2_CLUSTER_COMPRESSED
:
3832 case QCOW2_CLUSTER_NORMAL
:
3833 child
= s
->data_file
;
3834 copy_offset
+= offset_into_cluster(s
, src_offset
);
3835 if ((copy_offset
& 511) != 0) {
3844 qemu_co_mutex_unlock(&s
->lock
);
3845 ret
= bdrv_co_copy_range_from(child
,
3848 cur_bytes
, read_flags
, cur_write_flags
);
3849 qemu_co_mutex_lock(&s
->lock
);
3855 src_offset
+= cur_bytes
;
3856 dst_offset
+= cur_bytes
;
3861 qemu_co_mutex_unlock(&s
->lock
);
3865 static int coroutine_fn
3866 qcow2_co_copy_range_to(BlockDriverState
*bs
,
3867 BdrvChild
*src
, uint64_t src_offset
,
3868 BdrvChild
*dst
, uint64_t dst_offset
,
3869 uint64_t bytes
, BdrvRequestFlags read_flags
,
3870 BdrvRequestFlags write_flags
)
3872 BDRVQcow2State
*s
= bs
->opaque
;
3873 int offset_in_cluster
;
3875 unsigned int cur_bytes
; /* number of sectors in current iteration */
3876 uint64_t cluster_offset
;
3877 QCowL2Meta
*l2meta
= NULL
;
3879 assert(!bs
->encrypted
);
3881 qemu_co_mutex_lock(&s
->lock
);
3883 while (bytes
!= 0) {
3887 offset_in_cluster
= offset_into_cluster(s
, dst_offset
);
3888 cur_bytes
= MIN(bytes
, INT_MAX
);
3891 * If src->bs == dst->bs, we could simply copy by incrementing
3892 * the refcnt, without copying user data.
3893 * Or if src->bs == dst->bs->backing->bs, we could copy by discarding. */
3894 ret
= qcow2_alloc_cluster_offset(bs
, dst_offset
, &cur_bytes
,
3895 &cluster_offset
, &l2meta
);
3900 assert((cluster_offset
& 511) == 0);
3902 ret
= qcow2_pre_write_overlap_check(bs
, 0,
3903 cluster_offset
+ offset_in_cluster
, cur_bytes
, true);
3908 qemu_co_mutex_unlock(&s
->lock
);
3909 ret
= bdrv_co_copy_range_to(src
, src_offset
,
3911 cluster_offset
+ offset_in_cluster
,
3912 cur_bytes
, read_flags
, write_flags
);
3913 qemu_co_mutex_lock(&s
->lock
);
3918 ret
= qcow2_handle_l2meta(bs
, &l2meta
, true);
3924 src_offset
+= cur_bytes
;
3925 dst_offset
+= cur_bytes
;
3930 qcow2_handle_l2meta(bs
, &l2meta
, false);
3932 qemu_co_mutex_unlock(&s
->lock
);
3934 trace_qcow2_writev_done_req(qemu_coroutine_self(), ret
);
3939 static int coroutine_fn
qcow2_co_truncate(BlockDriverState
*bs
, int64_t offset
,
3940 bool exact
, PreallocMode prealloc
,
3943 BDRVQcow2State
*s
= bs
->opaque
;
3944 uint64_t old_length
;
3945 int64_t new_l1_size
;
3949 if (prealloc
!= PREALLOC_MODE_OFF
&& prealloc
!= PREALLOC_MODE_METADATA
&&
3950 prealloc
!= PREALLOC_MODE_FALLOC
&& prealloc
!= PREALLOC_MODE_FULL
)
3952 error_setg(errp
, "Unsupported preallocation mode '%s'",
3953 PreallocMode_str(prealloc
));
3958 error_setg(errp
, "The new size must be a multiple of 512");
3962 qemu_co_mutex_lock(&s
->lock
);
3964 /* cannot proceed if image has snapshots */
3965 if (s
->nb_snapshots
) {
3966 error_setg(errp
, "Can't resize an image which has snapshots");
3971 /* cannot proceed if image has bitmaps */
3972 if (qcow2_truncate_bitmaps_check(bs
, errp
)) {
3977 old_length
= bs
->total_sectors
* BDRV_SECTOR_SIZE
;
3978 new_l1_size
= size_to_l1(s
, offset
);
3980 if (offset
< old_length
) {
3981 int64_t last_cluster
, old_file_size
;
3982 if (prealloc
!= PREALLOC_MODE_OFF
) {
3984 "Preallocation can't be used for shrinking an image");
3989 ret
= qcow2_cluster_discard(bs
, ROUND_UP(offset
, s
->cluster_size
),
3990 old_length
- ROUND_UP(offset
,
3992 QCOW2_DISCARD_ALWAYS
, true);
3994 error_setg_errno(errp
, -ret
, "Failed to discard cropped clusters");
3998 ret
= qcow2_shrink_l1_table(bs
, new_l1_size
);
4000 error_setg_errno(errp
, -ret
,
4001 "Failed to reduce the number of L2 tables");
4005 ret
= qcow2_shrink_reftable(bs
);
4007 error_setg_errno(errp
, -ret
,
4008 "Failed to discard unused refblocks");
4012 old_file_size
= bdrv_getlength(bs
->file
->bs
);
4013 if (old_file_size
< 0) {
4014 error_setg_errno(errp
, -old_file_size
,
4015 "Failed to inquire current file length");
4016 ret
= old_file_size
;
4019 last_cluster
= qcow2_get_last_cluster(bs
, old_file_size
);
4020 if (last_cluster
< 0) {
4021 error_setg_errno(errp
, -last_cluster
,
4022 "Failed to find the last cluster");
4026 if ((last_cluster
+ 1) * s
->cluster_size
< old_file_size
) {
4027 Error
*local_err
= NULL
;
4030 * Do not pass @exact here: It will not help the user if
4031 * we get an error here just because they wanted to shrink
4032 * their qcow2 image (on a block device) with qemu-img.
4033 * (And on the qcow2 layer, the @exact requirement is
4034 * always fulfilled, so there is no need to pass it on.)
4036 bdrv_co_truncate(bs
->file
, (last_cluster
+ 1) * s
->cluster_size
,
4037 false, PREALLOC_MODE_OFF
, &local_err
);
4039 warn_reportf_err(local_err
,
4040 "Failed to truncate the tail of the image: ");
4044 ret
= qcow2_grow_l1_table(bs
, new_l1_size
, true);
4046 error_setg_errno(errp
, -ret
, "Failed to grow the L1 table");
4052 case PREALLOC_MODE_OFF
:
4053 if (has_data_file(bs
)) {
4055 * If the caller wants an exact resize, the external data
4056 * file should be resized to the exact target size, too,
4057 * so we pass @exact here.
4059 ret
= bdrv_co_truncate(s
->data_file
, offset
, exact
, prealloc
, errp
);
4066 case PREALLOC_MODE_METADATA
:
4067 ret
= preallocate_co(bs
, old_length
, offset
, prealloc
, errp
);
4073 case PREALLOC_MODE_FALLOC
:
4074 case PREALLOC_MODE_FULL
:
4076 int64_t allocation_start
, host_offset
, guest_offset
;
4077 int64_t clusters_allocated
;
4078 int64_t old_file_size
, new_file_size
;
4079 uint64_t nb_new_data_clusters
, nb_new_l2_tables
;
4081 /* With a data file, preallocation means just allocating the metadata
4082 * and forwarding the truncate request to the data file */
4083 if (has_data_file(bs
)) {
4084 ret
= preallocate_co(bs
, old_length
, offset
, prealloc
, errp
);
4091 old_file_size
= bdrv_getlength(bs
->file
->bs
);
4092 if (old_file_size
< 0) {
4093 error_setg_errno(errp
, -old_file_size
,
4094 "Failed to inquire current file length");
4095 ret
= old_file_size
;
4098 old_file_size
= ROUND_UP(old_file_size
, s
->cluster_size
);
4100 nb_new_data_clusters
= DIV_ROUND_UP(offset
- old_length
,
4103 /* This is an overestimation; we will not actually allocate space for
4104 * these in the file but just make sure the new refcount structures are
4105 * able to cover them so we will not have to allocate new refblocks
4106 * while entering the data blocks in the potentially new L2 tables.
4107 * (We do not actually care where the L2 tables are placed. Maybe they
4108 * are already allocated or they can be placed somewhere before
4109 * @old_file_size. It does not matter because they will be fully
4110 * allocated automatically, so they do not need to be covered by the
4111 * preallocation. All that matters is that we will not have to allocate
4112 * new refcount structures for them.) */
4113 nb_new_l2_tables
= DIV_ROUND_UP(nb_new_data_clusters
,
4114 s
->cluster_size
/ sizeof(uint64_t));
4115 /* The cluster range may not be aligned to L2 boundaries, so add one L2
4116 * table for a potential head/tail */
4119 allocation_start
= qcow2_refcount_area(bs
, old_file_size
,
4120 nb_new_data_clusters
+
4123 if (allocation_start
< 0) {
4124 error_setg_errno(errp
, -allocation_start
,
4125 "Failed to resize refcount structures");
4126 ret
= allocation_start
;
4130 clusters_allocated
= qcow2_alloc_clusters_at(bs
, allocation_start
,
4131 nb_new_data_clusters
);
4132 if (clusters_allocated
< 0) {
4133 error_setg_errno(errp
, -clusters_allocated
,
4134 "Failed to allocate data clusters");
4135 ret
= clusters_allocated
;
4139 assert(clusters_allocated
== nb_new_data_clusters
);
4141 /* Allocate the data area */
4142 new_file_size
= allocation_start
+
4143 nb_new_data_clusters
* s
->cluster_size
;
4144 /* Image file grows, so @exact does not matter */
4145 ret
= bdrv_co_truncate(bs
->file
, new_file_size
, false, prealloc
, errp
);
4147 error_prepend(errp
, "Failed to resize underlying file: ");
4148 qcow2_free_clusters(bs
, allocation_start
,
4149 nb_new_data_clusters
* s
->cluster_size
,
4150 QCOW2_DISCARD_OTHER
);
4154 /* Create the necessary L2 entries */
4155 host_offset
= allocation_start
;
4156 guest_offset
= old_length
;
4157 while (nb_new_data_clusters
) {
4158 int64_t nb_clusters
= MIN(
4159 nb_new_data_clusters
,
4160 s
->l2_slice_size
- offset_to_l2_slice_index(s
, guest_offset
));
4161 QCowL2Meta allocation
= {
4162 .offset
= guest_offset
,
4163 .alloc_offset
= host_offset
,
4164 .nb_clusters
= nb_clusters
,
4166 qemu_co_queue_init(&allocation
.dependent_requests
);
4168 ret
= qcow2_alloc_cluster_link_l2(bs
, &allocation
);
4170 error_setg_errno(errp
, -ret
, "Failed to update L2 tables");
4171 qcow2_free_clusters(bs
, host_offset
,
4172 nb_new_data_clusters
* s
->cluster_size
,
4173 QCOW2_DISCARD_OTHER
);
4177 guest_offset
+= nb_clusters
* s
->cluster_size
;
4178 host_offset
+= nb_clusters
* s
->cluster_size
;
4179 nb_new_data_clusters
-= nb_clusters
;
4185 g_assert_not_reached();
4188 if (prealloc
!= PREALLOC_MODE_OFF
) {
4189 /* Flush metadata before actually changing the image size */
4190 ret
= qcow2_write_caches(bs
);
4192 error_setg_errno(errp
, -ret
,
4193 "Failed to flush the preallocated area to disk");
4198 bs
->total_sectors
= offset
/ BDRV_SECTOR_SIZE
;
4200 /* write updated header.size */
4201 offset
= cpu_to_be64(offset
);
4202 ret
= bdrv_pwrite_sync(bs
->file
, offsetof(QCowHeader
, size
),
4203 &offset
, sizeof(uint64_t));
4205 error_setg_errno(errp
, -ret
, "Failed to update the image size");
4209 s
->l1_vm_state_index
= new_l1_size
;
4211 /* Update cache sizes */
4212 options
= qdict_clone_shallow(bs
->options
);
4213 ret
= qcow2_update_options(bs
, options
, s
->flags
, errp
);
4214 qobject_unref(options
);
4220 qemu_co_mutex_unlock(&s
->lock
);
4224 static coroutine_fn
int
4225 qcow2_co_pwritev_compressed_task(BlockDriverState
*bs
,
4226 uint64_t offset
, uint64_t bytes
,
4227 QEMUIOVector
*qiov
, size_t qiov_offset
)
4229 BDRVQcow2State
*s
= bs
->opaque
;
4232 uint8_t *buf
, *out_buf
;
4233 uint64_t cluster_offset
;
4235 assert(bytes
== s
->cluster_size
|| (bytes
< s
->cluster_size
&&
4236 (offset
+ bytes
== bs
->total_sectors
<< BDRV_SECTOR_BITS
)));
4238 buf
= qemu_blockalign(bs
, s
->cluster_size
);
4239 if (bytes
< s
->cluster_size
) {
4240 /* Zero-pad last write if image size is not cluster aligned */
4241 memset(buf
+ bytes
, 0, s
->cluster_size
- bytes
);
4243 qemu_iovec_to_buf(qiov
, qiov_offset
, buf
, bytes
);
4245 out_buf
= g_malloc(s
->cluster_size
);
4247 out_len
= qcow2_co_compress(bs
, out_buf
, s
->cluster_size
- 1,
4248 buf
, s
->cluster_size
);
4249 if (out_len
== -ENOMEM
) {
4250 /* could not compress: write normal cluster */
4251 ret
= qcow2_co_pwritev_part(bs
, offset
, bytes
, qiov
, qiov_offset
, 0);
4256 } else if (out_len
< 0) {
4261 qemu_co_mutex_lock(&s
->lock
);
4262 ret
= qcow2_alloc_compressed_cluster_offset(bs
, offset
, out_len
,
4265 qemu_co_mutex_unlock(&s
->lock
);
4269 ret
= qcow2_pre_write_overlap_check(bs
, 0, cluster_offset
, out_len
, true);
4270 qemu_co_mutex_unlock(&s
->lock
);
4275 BLKDBG_EVENT(s
->data_file
, BLKDBG_WRITE_COMPRESSED
);
4276 ret
= bdrv_co_pwrite(s
->data_file
, cluster_offset
, out_len
, out_buf
, 0);
4288 static coroutine_fn
int qcow2_co_pwritev_compressed_task_entry(AioTask
*task
)
4290 Qcow2AioTask
*t
= container_of(task
, Qcow2AioTask
, task
);
4292 assert(!t
->cluster_type
&& !t
->l2meta
);
4294 return qcow2_co_pwritev_compressed_task(t
->bs
, t
->offset
, t
->bytes
, t
->qiov
,
4299 * XXX: put compressed sectors first, then all the cluster aligned
4300 * tables to avoid losing bytes in alignment
4302 static coroutine_fn
int
4303 qcow2_co_pwritev_compressed_part(BlockDriverState
*bs
,
4304 uint64_t offset
, uint64_t bytes
,
4305 QEMUIOVector
*qiov
, size_t qiov_offset
)
4307 BDRVQcow2State
*s
= bs
->opaque
;
4308 AioTaskPool
*aio
= NULL
;
4311 if (has_data_file(bs
)) {
4317 * align end of file to a sector boundary to ease reading with
4320 int64_t len
= bdrv_getlength(bs
->file
->bs
);
4324 return bdrv_co_truncate(bs
->file
, len
, false, PREALLOC_MODE_OFF
, NULL
);
4327 if (offset_into_cluster(s
, offset
)) {
4331 while (bytes
&& aio_task_pool_status(aio
) == 0) {
4332 uint64_t chunk_size
= MIN(bytes
, s
->cluster_size
);
4334 if (!aio
&& chunk_size
!= bytes
) {
4335 aio
= aio_task_pool_new(QCOW2_MAX_WORKERS
);
4338 ret
= qcow2_add_task(bs
, aio
, qcow2_co_pwritev_compressed_task_entry
,
4339 0, 0, offset
, chunk_size
, qiov
, qiov_offset
, NULL
);
4343 qiov_offset
+= chunk_size
;
4344 offset
+= chunk_size
;
4345 bytes
-= chunk_size
;
4349 aio_task_pool_wait_all(aio
);
4351 ret
= aio_task_pool_status(aio
);
4359 static int coroutine_fn
4360 qcow2_co_preadv_compressed(BlockDriverState
*bs
,
4361 uint64_t file_cluster_offset
,
4367 BDRVQcow2State
*s
= bs
->opaque
;
4368 int ret
= 0, csize
, nb_csectors
;
4370 uint8_t *buf
, *out_buf
;
4371 int offset_in_cluster
= offset_into_cluster(s
, offset
);
4373 coffset
= file_cluster_offset
& s
->cluster_offset_mask
;
4374 nb_csectors
= ((file_cluster_offset
>> s
->csize_shift
) & s
->csize_mask
) + 1;
4375 csize
= nb_csectors
* QCOW2_COMPRESSED_SECTOR_SIZE
-
4376 (coffset
& ~QCOW2_COMPRESSED_SECTOR_MASK
);
4378 buf
= g_try_malloc(csize
);
4383 out_buf
= qemu_blockalign(bs
, s
->cluster_size
);
4385 BLKDBG_EVENT(bs
->file
, BLKDBG_READ_COMPRESSED
);
4386 ret
= bdrv_co_pread(bs
->file
, coffset
, csize
, buf
, 0);
4391 if (qcow2_co_decompress(bs
, out_buf
, s
->cluster_size
, buf
, csize
) < 0) {
4396 qemu_iovec_from_buf(qiov
, qiov_offset
, out_buf
+ offset_in_cluster
, bytes
);
4399 qemu_vfree(out_buf
);
4405 static int make_completely_empty(BlockDriverState
*bs
)
4407 BDRVQcow2State
*s
= bs
->opaque
;
4408 Error
*local_err
= NULL
;
4409 int ret
, l1_clusters
;
4411 uint64_t *new_reftable
= NULL
;
4412 uint64_t rt_entry
, l1_size2
;
4415 uint64_t reftable_offset
;
4416 uint32_t reftable_clusters
;
4417 } QEMU_PACKED l1_ofs_rt_ofs_cls
;
4419 ret
= qcow2_cache_empty(bs
, s
->l2_table_cache
);
4424 ret
= qcow2_cache_empty(bs
, s
->refcount_block_cache
);
4429 /* Refcounts will be broken utterly */
4430 ret
= qcow2_mark_dirty(bs
);
4435 BLKDBG_EVENT(bs
->file
, BLKDBG_L1_UPDATE
);
4437 l1_clusters
= DIV_ROUND_UP(s
->l1_size
, s
->cluster_size
/ sizeof(uint64_t));
4438 l1_size2
= (uint64_t)s
->l1_size
* sizeof(uint64_t);
4440 /* After this call, neither the in-memory nor the on-disk refcount
4441 * information accurately describe the actual references */
4443 ret
= bdrv_pwrite_zeroes(bs
->file
, s
->l1_table_offset
,
4444 l1_clusters
* s
->cluster_size
, 0);
4446 goto fail_broken_refcounts
;
4448 memset(s
->l1_table
, 0, l1_size2
);
4450 BLKDBG_EVENT(bs
->file
, BLKDBG_EMPTY_IMAGE_PREPARE
);
4452 /* Overwrite enough clusters at the beginning of the sectors to place
4453 * the refcount table, a refcount block and the L1 table in; this may
4454 * overwrite parts of the existing refcount and L1 table, which is not
4455 * an issue because the dirty flag is set, complete data loss is in fact
4456 * desired and partial data loss is consequently fine as well */
4457 ret
= bdrv_pwrite_zeroes(bs
->file
, s
->cluster_size
,
4458 (2 + l1_clusters
) * s
->cluster_size
, 0);
4459 /* This call (even if it failed overall) may have overwritten on-disk
4460 * refcount structures; in that case, the in-memory refcount information
4461 * will probably differ from the on-disk information which makes the BDS
4464 goto fail_broken_refcounts
;
4467 BLKDBG_EVENT(bs
->file
, BLKDBG_L1_UPDATE
);
4468 BLKDBG_EVENT(bs
->file
, BLKDBG_REFTABLE_UPDATE
);
4470 /* "Create" an empty reftable (one cluster) directly after the image
4471 * header and an empty L1 table three clusters after the image header;
4472 * the cluster between those two will be used as the first refblock */
4473 l1_ofs_rt_ofs_cls
.l1_offset
= cpu_to_be64(3 * s
->cluster_size
);
4474 l1_ofs_rt_ofs_cls
.reftable_offset
= cpu_to_be64(s
->cluster_size
);
4475 l1_ofs_rt_ofs_cls
.reftable_clusters
= cpu_to_be32(1);
4476 ret
= bdrv_pwrite_sync(bs
->file
, offsetof(QCowHeader
, l1_table_offset
),
4477 &l1_ofs_rt_ofs_cls
, sizeof(l1_ofs_rt_ofs_cls
));
4479 goto fail_broken_refcounts
;
4482 s
->l1_table_offset
= 3 * s
->cluster_size
;
4484 new_reftable
= g_try_new0(uint64_t, s
->cluster_size
/ sizeof(uint64_t));
4485 if (!new_reftable
) {
4487 goto fail_broken_refcounts
;
4490 s
->refcount_table_offset
= s
->cluster_size
;
4491 s
->refcount_table_size
= s
->cluster_size
/ sizeof(uint64_t);
4492 s
->max_refcount_table_index
= 0;
4494 g_free(s
->refcount_table
);
4495 s
->refcount_table
= new_reftable
;
4496 new_reftable
= NULL
;
4498 /* Now the in-memory refcount information again corresponds to the on-disk
4499 * information (reftable is empty and no refblocks (the refblock cache is
4500 * empty)); however, this means some clusters (e.g. the image header) are
4501 * referenced, but not refcounted, but the normal qcow2 code assumes that
4502 * the in-memory information is always correct */
4504 BLKDBG_EVENT(bs
->file
, BLKDBG_REFBLOCK_ALLOC
);
4506 /* Enter the first refblock into the reftable */
4507 rt_entry
= cpu_to_be64(2 * s
->cluster_size
);
4508 ret
= bdrv_pwrite_sync(bs
->file
, s
->cluster_size
,
4509 &rt_entry
, sizeof(rt_entry
));
4511 goto fail_broken_refcounts
;
4513 s
->refcount_table
[0] = 2 * s
->cluster_size
;
4515 s
->free_cluster_index
= 0;
4516 assert(3 + l1_clusters
<= s
->refcount_block_size
);
4517 offset
= qcow2_alloc_clusters(bs
, 3 * s
->cluster_size
+ l1_size2
);
4520 goto fail_broken_refcounts
;
4521 } else if (offset
> 0) {
4522 error_report("First cluster in emptied image is in use");
4526 /* Now finally the in-memory information corresponds to the on-disk
4527 * structures and is correct */
4528 ret
= qcow2_mark_clean(bs
);
4533 ret
= bdrv_truncate(bs
->file
, (3 + l1_clusters
) * s
->cluster_size
, false,
4534 PREALLOC_MODE_OFF
, &local_err
);
4536 error_report_err(local_err
);
4542 fail_broken_refcounts
:
4543 /* The BDS is unusable at this point. If we wanted to make it usable, we
4544 * would have to call qcow2_refcount_close(), qcow2_refcount_init(),
4545 * qcow2_check_refcounts(), qcow2_refcount_close() and qcow2_refcount_init()
4546 * again. However, because the functions which could have caused this error
4547 * path to be taken are used by those functions as well, it's very likely
4548 * that that sequence will fail as well. Therefore, just eject the BDS. */
4552 g_free(new_reftable
);
4556 static int qcow2_make_empty(BlockDriverState
*bs
)
4558 BDRVQcow2State
*s
= bs
->opaque
;
4559 uint64_t offset
, end_offset
;
4560 int step
= QEMU_ALIGN_DOWN(INT_MAX
, s
->cluster_size
);
4561 int l1_clusters
, ret
= 0;
4563 l1_clusters
= DIV_ROUND_UP(s
->l1_size
, s
->cluster_size
/ sizeof(uint64_t));
4565 if (s
->qcow_version
>= 3 && !s
->snapshots
&& !s
->nb_bitmaps
&&
4566 3 + l1_clusters
<= s
->refcount_block_size
&&
4567 s
->crypt_method_header
!= QCOW_CRYPT_LUKS
&&
4568 !has_data_file(bs
)) {
4569 /* The following function only works for qcow2 v3 images (it
4570 * requires the dirty flag) and only as long as there are no
4571 * features that reserve extra clusters (such as snapshots,
4572 * LUKS header, or persistent bitmaps), because it completely
4573 * empties the image. Furthermore, the L1 table and three
4574 * additional clusters (image header, refcount table, one
4575 * refcount block) have to fit inside one refcount block. It
4576 * only resets the image file, i.e. does not work with an
4577 * external data file. */
4578 return make_completely_empty(bs
);
4581 /* This fallback code simply discards every active cluster; this is slow,
4582 * but works in all cases */
4583 end_offset
= bs
->total_sectors
* BDRV_SECTOR_SIZE
;
4584 for (offset
= 0; offset
< end_offset
; offset
+= step
) {
4585 /* As this function is generally used after committing an external
4586 * snapshot, QCOW2_DISCARD_SNAPSHOT seems appropriate. Also, the
4587 * default action for this kind of discard is to pass the discard,
4588 * which will ideally result in an actually smaller image file, as
4589 * is probably desired. */
4590 ret
= qcow2_cluster_discard(bs
, offset
, MIN(step
, end_offset
- offset
),
4591 QCOW2_DISCARD_SNAPSHOT
, true);
4600 static coroutine_fn
int qcow2_co_flush_to_os(BlockDriverState
*bs
)
4602 BDRVQcow2State
*s
= bs
->opaque
;
4605 qemu_co_mutex_lock(&s
->lock
);
4606 ret
= qcow2_write_caches(bs
);
4607 qemu_co_mutex_unlock(&s
->lock
);
4612 static ssize_t
qcow2_measure_crypto_hdr_init_func(QCryptoBlock
*block
,
4613 size_t headerlen
, void *opaque
, Error
**errp
)
4615 size_t *headerlenp
= opaque
;
4617 /* Stash away the payload size */
4618 *headerlenp
= headerlen
;
4622 static ssize_t
qcow2_measure_crypto_hdr_write_func(QCryptoBlock
*block
,
4623 size_t offset
, const uint8_t *buf
, size_t buflen
,
4624 void *opaque
, Error
**errp
)
4626 /* Discard the bytes, we're not actually writing to an image */
4630 /* Determine the number of bytes for the LUKS payload */
4631 static bool qcow2_measure_luks_headerlen(QemuOpts
*opts
, size_t *len
,
4635 QDict
*cryptoopts_qdict
;
4636 QCryptoBlockCreateOptions
*cryptoopts
;
4637 QCryptoBlock
*crypto
;
4639 /* Extract "encrypt." options into a qdict */
4640 opts_qdict
= qemu_opts_to_qdict(opts
, NULL
);
4641 qdict_extract_subqdict(opts_qdict
, &cryptoopts_qdict
, "encrypt.");
4642 qobject_unref(opts_qdict
);
4644 /* Build QCryptoBlockCreateOptions object from qdict */
4645 qdict_put_str(cryptoopts_qdict
, "format", "luks");
4646 cryptoopts
= block_crypto_create_opts_init(cryptoopts_qdict
, errp
);
4647 qobject_unref(cryptoopts_qdict
);
4652 /* Fake LUKS creation in order to determine the payload size */
4653 crypto
= qcrypto_block_create(cryptoopts
, "encrypt.",
4654 qcow2_measure_crypto_hdr_init_func
,
4655 qcow2_measure_crypto_hdr_write_func
,
4657 qapi_free_QCryptoBlockCreateOptions(cryptoopts
);
4662 qcrypto_block_free(crypto
);
4666 static BlockMeasureInfo
*qcow2_measure(QemuOpts
*opts
, BlockDriverState
*in_bs
,
4669 Error
*local_err
= NULL
;
4670 BlockMeasureInfo
*info
;
4671 uint64_t required
= 0; /* bytes that contribute to required size */
4672 uint64_t virtual_size
; /* disk size as seen by guest */
4673 uint64_t refcount_bits
;
4675 uint64_t luks_payload_size
= 0;
4676 size_t cluster_size
;
4679 PreallocMode prealloc
;
4680 bool has_backing_file
;
4683 /* Parse image creation options */
4684 cluster_size
= qcow2_opt_get_cluster_size_del(opts
, &local_err
);
4689 version
= qcow2_opt_get_version_del(opts
, &local_err
);
4694 refcount_bits
= qcow2_opt_get_refcount_bits_del(opts
, version
, &local_err
);
4699 optstr
= qemu_opt_get_del(opts
, BLOCK_OPT_PREALLOC
);
4700 prealloc
= qapi_enum_parse(&PreallocMode_lookup
, optstr
,
4701 PREALLOC_MODE_OFF
, &local_err
);
4707 optstr
= qemu_opt_get_del(opts
, BLOCK_OPT_BACKING_FILE
);
4708 has_backing_file
= !!optstr
;
4711 optstr
= qemu_opt_get_del(opts
, BLOCK_OPT_ENCRYPT_FORMAT
);
4712 has_luks
= optstr
&& strcmp(optstr
, "luks") == 0;
4718 if (!qcow2_measure_luks_headerlen(opts
, &headerlen
, &local_err
)) {
4722 luks_payload_size
= ROUND_UP(headerlen
, cluster_size
);
4725 virtual_size
= qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0);
4726 virtual_size
= ROUND_UP(virtual_size
, cluster_size
);
4728 /* Check that virtual disk size is valid */
4729 l2_tables
= DIV_ROUND_UP(virtual_size
/ cluster_size
,
4730 cluster_size
/ sizeof(uint64_t));
4731 if (l2_tables
* sizeof(uint64_t) > QCOW_MAX_L1_SIZE
) {
4732 error_setg(&local_err
, "The image size is too large "
4733 "(try using a larger cluster size)");
4737 /* Account for input image */
4739 int64_t ssize
= bdrv_getlength(in_bs
);
4741 error_setg_errno(&local_err
, -ssize
,
4742 "Unable to get image virtual_size");
4746 virtual_size
= ROUND_UP(ssize
, cluster_size
);
4748 if (has_backing_file
) {
4749 /* We don't how much of the backing chain is shared by the input
4750 * image and the new image file. In the worst case the new image's
4751 * backing file has nothing in common with the input image. Be
4752 * conservative and assume all clusters need to be written.
4754 required
= virtual_size
;
4759 for (offset
= 0; offset
< ssize
; offset
+= pnum
) {
4762 ret
= bdrv_block_status_above(in_bs
, NULL
, offset
,
4763 ssize
- offset
, &pnum
, NULL
,
4766 error_setg_errno(&local_err
, -ret
,
4767 "Unable to get block status");
4771 if (ret
& BDRV_BLOCK_ZERO
) {
4772 /* Skip zero regions (safe with no backing file) */
4773 } else if ((ret
& (BDRV_BLOCK_DATA
| BDRV_BLOCK_ALLOCATED
)) ==
4774 (BDRV_BLOCK_DATA
| BDRV_BLOCK_ALLOCATED
)) {
4775 /* Extend pnum to end of cluster for next iteration */
4776 pnum
= ROUND_UP(offset
+ pnum
, cluster_size
) - offset
;
4778 /* Count clusters we've seen */
4779 required
+= offset
% cluster_size
+ pnum
;
4785 /* Take into account preallocation. Nothing special is needed for
4786 * PREALLOC_MODE_METADATA since metadata is always counted.
4788 if (prealloc
== PREALLOC_MODE_FULL
|| prealloc
== PREALLOC_MODE_FALLOC
) {
4789 required
= virtual_size
;
4792 info
= g_new(BlockMeasureInfo
, 1);
4793 info
->fully_allocated
=
4794 qcow2_calc_prealloc_size(virtual_size
, cluster_size
,
4795 ctz32(refcount_bits
)) + luks_payload_size
;
4797 /* Remove data clusters that are not required. This overestimates the
4798 * required size because metadata needed for the fully allocated file is
4801 info
->required
= info
->fully_allocated
- virtual_size
+ required
;
4805 error_propagate(errp
, local_err
);
4809 static int qcow2_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
4811 BDRVQcow2State
*s
= bs
->opaque
;
4812 bdi
->unallocated_blocks_are_zero
= true;
4813 bdi
->cluster_size
= s
->cluster_size
;
4814 bdi
->vm_state_offset
= qcow2_vm_state_offset(s
);
4818 static ImageInfoSpecific
*qcow2_get_specific_info(BlockDriverState
*bs
,
4821 BDRVQcow2State
*s
= bs
->opaque
;
4822 ImageInfoSpecific
*spec_info
;
4823 QCryptoBlockInfo
*encrypt_info
= NULL
;
4824 Error
*local_err
= NULL
;
4826 if (s
->crypto
!= NULL
) {
4827 encrypt_info
= qcrypto_block_get_info(s
->crypto
, &local_err
);
4829 error_propagate(errp
, local_err
);
4834 spec_info
= g_new(ImageInfoSpecific
, 1);
4835 *spec_info
= (ImageInfoSpecific
){
4836 .type
= IMAGE_INFO_SPECIFIC_KIND_QCOW2
,
4837 .u
.qcow2
.data
= g_new0(ImageInfoSpecificQCow2
, 1),
4839 if (s
->qcow_version
== 2) {
4840 *spec_info
->u
.qcow2
.data
= (ImageInfoSpecificQCow2
){
4841 .compat
= g_strdup("0.10"),
4842 .refcount_bits
= s
->refcount_bits
,
4844 } else if (s
->qcow_version
== 3) {
4845 Qcow2BitmapInfoList
*bitmaps
;
4846 bitmaps
= qcow2_get_bitmap_info_list(bs
, &local_err
);
4848 error_propagate(errp
, local_err
);
4849 qapi_free_ImageInfoSpecific(spec_info
);
4852 *spec_info
->u
.qcow2
.data
= (ImageInfoSpecificQCow2
){
4853 .compat
= g_strdup("1.1"),
4854 .lazy_refcounts
= s
->compatible_features
&
4855 QCOW2_COMPAT_LAZY_REFCOUNTS
,
4856 .has_lazy_refcounts
= true,
4857 .corrupt
= s
->incompatible_features
&
4858 QCOW2_INCOMPAT_CORRUPT
,
4859 .has_corrupt
= true,
4860 .refcount_bits
= s
->refcount_bits
,
4861 .has_bitmaps
= !!bitmaps
,
4863 .has_data_file
= !!s
->image_data_file
,
4864 .data_file
= g_strdup(s
->image_data_file
),
4865 .has_data_file_raw
= has_data_file(bs
),
4866 .data_file_raw
= data_file_is_raw(bs
),
4869 /* if this assertion fails, this probably means a new version was
4870 * added without having it covered here */
4875 ImageInfoSpecificQCow2Encryption
*qencrypt
=
4876 g_new(ImageInfoSpecificQCow2Encryption
, 1);
4877 switch (encrypt_info
->format
) {
4878 case Q_CRYPTO_BLOCK_FORMAT_QCOW
:
4879 qencrypt
->format
= BLOCKDEV_QCOW2_ENCRYPTION_FORMAT_AES
;
4881 case Q_CRYPTO_BLOCK_FORMAT_LUKS
:
4882 qencrypt
->format
= BLOCKDEV_QCOW2_ENCRYPTION_FORMAT_LUKS
;
4883 qencrypt
->u
.luks
= encrypt_info
->u
.luks
;
4888 /* Since we did shallow copy above, erase any pointers
4889 * in the original info */
4890 memset(&encrypt_info
->u
, 0, sizeof(encrypt_info
->u
));
4891 qapi_free_QCryptoBlockInfo(encrypt_info
);
4893 spec_info
->u
.qcow2
.data
->has_encrypt
= true;
4894 spec_info
->u
.qcow2
.data
->encrypt
= qencrypt
;
4900 static int qcow2_has_zero_init(BlockDriverState
*bs
)
4902 BDRVQcow2State
*s
= bs
->opaque
;
4905 if (qemu_in_coroutine()) {
4906 qemu_co_mutex_lock(&s
->lock
);
4909 * Check preallocation status: Preallocated images have all L2
4910 * tables allocated, nonpreallocated images have none. It is
4911 * therefore enough to check the first one.
4913 preallocated
= s
->l1_size
> 0 && s
->l1_table
[0] != 0;
4914 if (qemu_in_coroutine()) {
4915 qemu_co_mutex_unlock(&s
->lock
);
4918 if (!preallocated
) {
4920 } else if (bs
->encrypted
) {
4923 return bdrv_has_zero_init(s
->data_file
->bs
);
4927 static int qcow2_save_vmstate(BlockDriverState
*bs
, QEMUIOVector
*qiov
,
4930 BDRVQcow2State
*s
= bs
->opaque
;
4932 BLKDBG_EVENT(bs
->file
, BLKDBG_VMSTATE_SAVE
);
4933 return bs
->drv
->bdrv_co_pwritev_part(bs
, qcow2_vm_state_offset(s
) + pos
,
4934 qiov
->size
, qiov
, 0, 0);
4937 static int qcow2_load_vmstate(BlockDriverState
*bs
, QEMUIOVector
*qiov
,
4940 BDRVQcow2State
*s
= bs
->opaque
;
4942 BLKDBG_EVENT(bs
->file
, BLKDBG_VMSTATE_LOAD
);
4943 return bs
->drv
->bdrv_co_preadv_part(bs
, qcow2_vm_state_offset(s
) + pos
,
4944 qiov
->size
, qiov
, 0, 0);
4948 * Downgrades an image's version. To achieve this, any incompatible features
4949 * have to be removed.
4951 static int qcow2_downgrade(BlockDriverState
*bs
, int target_version
,
4952 BlockDriverAmendStatusCB
*status_cb
, void *cb_opaque
,
4955 BDRVQcow2State
*s
= bs
->opaque
;
4956 int current_version
= s
->qcow_version
;
4959 /* This is qcow2_downgrade(), not qcow2_upgrade() */
4960 assert(target_version
< current_version
);
4962 /* There are no other versions (now) that you can downgrade to */
4963 assert(target_version
== 2);
4965 if (s
->refcount_order
!= 4) {
4966 error_setg(errp
, "compat=0.10 requires refcount_bits=16");
4970 if (has_data_file(bs
)) {
4971 error_setg(errp
, "Cannot downgrade an image with a data file");
4975 /* clear incompatible features */
4976 if (s
->incompatible_features
& QCOW2_INCOMPAT_DIRTY
) {
4977 ret
= qcow2_mark_clean(bs
);
4979 error_setg_errno(errp
, -ret
, "Failed to make the image clean");
4984 /* with QCOW2_INCOMPAT_CORRUPT, it is pretty much impossible to get here in
4985 * the first place; if that happens nonetheless, returning -ENOTSUP is the
4986 * best thing to do anyway */
4988 if (s
->incompatible_features
) {
4989 error_setg(errp
, "Cannot downgrade an image with incompatible features "
4990 "%#" PRIx64
" set", s
->incompatible_features
);
4994 /* since we can ignore compatible features, we can set them to 0 as well */
4995 s
->compatible_features
= 0;
4996 /* if lazy refcounts have been used, they have already been fixed through
4997 * clearing the dirty flag */
4999 /* clearing autoclear features is trivial */
5000 s
->autoclear_features
= 0;
5002 ret
= qcow2_expand_zero_clusters(bs
, status_cb
, cb_opaque
);
5004 error_setg_errno(errp
, -ret
, "Failed to turn zero into data clusters");
5008 s
->qcow_version
= target_version
;
5009 ret
= qcow2_update_header(bs
);
5011 s
->qcow_version
= current_version
;
5012 error_setg_errno(errp
, -ret
, "Failed to update the image header");
5019 * Upgrades an image's version. While newer versions encompass all
5020 * features of older versions, some things may have to be presented
5023 static int qcow2_upgrade(BlockDriverState
*bs
, int target_version
,
5024 BlockDriverAmendStatusCB
*status_cb
, void *cb_opaque
,
5027 BDRVQcow2State
*s
= bs
->opaque
;
5028 bool need_snapshot_update
;
5029 int current_version
= s
->qcow_version
;
5033 /* This is qcow2_upgrade(), not qcow2_downgrade() */
5034 assert(target_version
> current_version
);
5036 /* There are no other versions (yet) that you can upgrade to */
5037 assert(target_version
== 3);
5039 status_cb(bs
, 0, 2, cb_opaque
);
5042 * In v2, snapshots do not need to have extra data. v3 requires
5043 * the 64-bit VM state size and the virtual disk size to be
5045 * qcow2_write_snapshots() will always write the list in the
5046 * v3-compliant format.
5048 need_snapshot_update
= false;
5049 for (i
= 0; i
< s
->nb_snapshots
; i
++) {
5050 if (s
->snapshots
[i
].extra_data_size
<
5051 sizeof_field(QCowSnapshotExtraData
, vm_state_size_large
) +
5052 sizeof_field(QCowSnapshotExtraData
, disk_size
))
5054 need_snapshot_update
= true;
5058 if (need_snapshot_update
) {
5059 ret
= qcow2_write_snapshots(bs
);
5061 error_setg_errno(errp
, -ret
, "Failed to update the snapshot table");
5065 status_cb(bs
, 1, 2, cb_opaque
);
5067 s
->qcow_version
= target_version
;
5068 ret
= qcow2_update_header(bs
);
5070 s
->qcow_version
= current_version
;
5071 error_setg_errno(errp
, -ret
, "Failed to update the image header");
5074 status_cb(bs
, 2, 2, cb_opaque
);
5079 typedef enum Qcow2AmendOperation
{
5080 /* This is the value Qcow2AmendHelperCBInfo::last_operation will be
5081 * statically initialized to so that the helper CB can discern the first
5082 * invocation from an operation change */
5083 QCOW2_NO_OPERATION
= 0,
5086 QCOW2_CHANGING_REFCOUNT_ORDER
,
5088 } Qcow2AmendOperation
;
5090 typedef struct Qcow2AmendHelperCBInfo
{
5091 /* The code coordinating the amend operations should only modify
5092 * these four fields; the rest will be managed by the CB */
5093 BlockDriverAmendStatusCB
*original_status_cb
;
5094 void *original_cb_opaque
;
5096 Qcow2AmendOperation current_operation
;
5098 /* Total number of operations to perform (only set once) */
5099 int total_operations
;
5101 /* The following fields are managed by the CB */
5103 /* Number of operations completed */
5104 int operations_completed
;
5106 /* Cumulative offset of all completed operations */
5107 int64_t offset_completed
;
5109 Qcow2AmendOperation last_operation
;
5110 int64_t last_work_size
;
5111 } Qcow2AmendHelperCBInfo
;
5113 static void qcow2_amend_helper_cb(BlockDriverState
*bs
,
5114 int64_t operation_offset
,
5115 int64_t operation_work_size
, void *opaque
)
5117 Qcow2AmendHelperCBInfo
*info
= opaque
;
5118 int64_t current_work_size
;
5119 int64_t projected_work_size
;
5121 if (info
->current_operation
!= info
->last_operation
) {
5122 if (info
->last_operation
!= QCOW2_NO_OPERATION
) {
5123 info
->offset_completed
+= info
->last_work_size
;
5124 info
->operations_completed
++;
5127 info
->last_operation
= info
->current_operation
;
5130 assert(info
->total_operations
> 0);
5131 assert(info
->operations_completed
< info
->total_operations
);
5133 info
->last_work_size
= operation_work_size
;
5135 current_work_size
= info
->offset_completed
+ operation_work_size
;
5137 /* current_work_size is the total work size for (operations_completed + 1)
5138 * operations (which includes this one), so multiply it by the number of
5139 * operations not covered and divide it by the number of operations
5140 * covered to get a projection for the operations not covered */
5141 projected_work_size
= current_work_size
* (info
->total_operations
-
5142 info
->operations_completed
- 1)
5143 / (info
->operations_completed
+ 1);
5145 info
->original_status_cb(bs
, info
->offset_completed
+ operation_offset
,
5146 current_work_size
+ projected_work_size
,
5147 info
->original_cb_opaque
);
5150 static int qcow2_amend_options(BlockDriverState
*bs
, QemuOpts
*opts
,
5151 BlockDriverAmendStatusCB
*status_cb
,
5155 BDRVQcow2State
*s
= bs
->opaque
;
5156 int old_version
= s
->qcow_version
, new_version
= old_version
;
5157 uint64_t new_size
= 0;
5158 const char *backing_file
= NULL
, *backing_format
= NULL
, *data_file
= NULL
;
5159 bool lazy_refcounts
= s
->use_lazy_refcounts
;
5160 bool data_file_raw
= data_file_is_raw(bs
);
5161 const char *compat
= NULL
;
5162 uint64_t cluster_size
= s
->cluster_size
;
5165 int refcount_bits
= s
->refcount_bits
;
5167 QemuOptDesc
*desc
= opts
->list
->desc
;
5168 Qcow2AmendHelperCBInfo helper_cb_info
;
5170 while (desc
&& desc
->name
) {
5171 if (!qemu_opt_find(opts
, desc
->name
)) {
5172 /* only change explicitly defined options */
5177 if (!strcmp(desc
->name
, BLOCK_OPT_COMPAT_LEVEL
)) {
5178 compat
= qemu_opt_get(opts
, BLOCK_OPT_COMPAT_LEVEL
);
5180 /* preserve default */
5181 } else if (!strcmp(compat
, "0.10") || !strcmp(compat
, "v2")) {
5183 } else if (!strcmp(compat
, "1.1") || !strcmp(compat
, "v3")) {
5186 error_setg(errp
, "Unknown compatibility level %s", compat
);
5189 } else if (!strcmp(desc
->name
, BLOCK_OPT_PREALLOC
)) {
5190 error_setg(errp
, "Cannot change preallocation mode");
5192 } else if (!strcmp(desc
->name
, BLOCK_OPT_SIZE
)) {
5193 new_size
= qemu_opt_get_size(opts
, BLOCK_OPT_SIZE
, 0);
5194 } else if (!strcmp(desc
->name
, BLOCK_OPT_BACKING_FILE
)) {
5195 backing_file
= qemu_opt_get(opts
, BLOCK_OPT_BACKING_FILE
);
5196 } else if (!strcmp(desc
->name
, BLOCK_OPT_BACKING_FMT
)) {
5197 backing_format
= qemu_opt_get(opts
, BLOCK_OPT_BACKING_FMT
);
5198 } else if (!strcmp(desc
->name
, BLOCK_OPT_ENCRYPT
)) {
5199 encrypt
= qemu_opt_get_bool(opts
, BLOCK_OPT_ENCRYPT
,
5202 if (encrypt
!= !!s
->crypto
) {
5204 "Changing the encryption flag is not supported");
5207 } else if (!strcmp(desc
->name
, BLOCK_OPT_ENCRYPT_FORMAT
)) {
5208 encformat
= qcow2_crypt_method_from_format(
5209 qemu_opt_get(opts
, BLOCK_OPT_ENCRYPT_FORMAT
));
5211 if (encformat
!= s
->crypt_method_header
) {
5213 "Changing the encryption format is not supported");
5216 } else if (g_str_has_prefix(desc
->name
, "encrypt.")) {
5218 "Changing the encryption parameters is not supported");
5220 } else if (!strcmp(desc
->name
, BLOCK_OPT_CLUSTER_SIZE
)) {
5221 cluster_size
= qemu_opt_get_size(opts
, BLOCK_OPT_CLUSTER_SIZE
,
5223 if (cluster_size
!= s
->cluster_size
) {
5224 error_setg(errp
, "Changing the cluster size is not supported");
5227 } else if (!strcmp(desc
->name
, BLOCK_OPT_LAZY_REFCOUNTS
)) {
5228 lazy_refcounts
= qemu_opt_get_bool(opts
, BLOCK_OPT_LAZY_REFCOUNTS
,
5230 } else if (!strcmp(desc
->name
, BLOCK_OPT_REFCOUNT_BITS
)) {
5231 refcount_bits
= qemu_opt_get_number(opts
, BLOCK_OPT_REFCOUNT_BITS
,
5234 if (refcount_bits
<= 0 || refcount_bits
> 64 ||
5235 !is_power_of_2(refcount_bits
))
5237 error_setg(errp
, "Refcount width must be a power of two and "
5238 "may not exceed 64 bits");
5241 } else if (!strcmp(desc
->name
, BLOCK_OPT_DATA_FILE
)) {
5242 data_file
= qemu_opt_get(opts
, BLOCK_OPT_DATA_FILE
);
5243 if (data_file
&& !has_data_file(bs
)) {
5244 error_setg(errp
, "data-file can only be set for images that "
5245 "use an external data file");
5248 } else if (!strcmp(desc
->name
, BLOCK_OPT_DATA_FILE_RAW
)) {
5249 data_file_raw
= qemu_opt_get_bool(opts
, BLOCK_OPT_DATA_FILE_RAW
,
5251 if (data_file_raw
&& !data_file_is_raw(bs
)) {
5252 error_setg(errp
, "data-file-raw cannot be set on existing "
5257 /* if this point is reached, this probably means a new option was
5258 * added without having it covered here */
5265 helper_cb_info
= (Qcow2AmendHelperCBInfo
){
5266 .original_status_cb
= status_cb
,
5267 .original_cb_opaque
= cb_opaque
,
5268 .total_operations
= (new_version
!= old_version
)
5269 + (s
->refcount_bits
!= refcount_bits
)
5272 /* Upgrade first (some features may require compat=1.1) */
5273 if (new_version
> old_version
) {
5274 helper_cb_info
.current_operation
= QCOW2_UPGRADING
;
5275 ret
= qcow2_upgrade(bs
, new_version
, &qcow2_amend_helper_cb
,
5276 &helper_cb_info
, errp
);
5282 if (s
->refcount_bits
!= refcount_bits
) {
5283 int refcount_order
= ctz32(refcount_bits
);
5285 if (new_version
< 3 && refcount_bits
!= 16) {
5286 error_setg(errp
, "Refcount widths other than 16 bits require "
5287 "compatibility level 1.1 or above (use compat=1.1 or "
5292 helper_cb_info
.current_operation
= QCOW2_CHANGING_REFCOUNT_ORDER
;
5293 ret
= qcow2_change_refcount_order(bs
, refcount_order
,
5294 &qcow2_amend_helper_cb
,
5295 &helper_cb_info
, errp
);
5301 /* data-file-raw blocks backing files, so clear it first if requested */
5302 if (data_file_raw
) {
5303 s
->autoclear_features
|= QCOW2_AUTOCLEAR_DATA_FILE_RAW
;
5305 s
->autoclear_features
&= ~QCOW2_AUTOCLEAR_DATA_FILE_RAW
;
5309 g_free(s
->image_data_file
);
5310 s
->image_data_file
= *data_file
? g_strdup(data_file
) : NULL
;
5313 ret
= qcow2_update_header(bs
);
5315 error_setg_errno(errp
, -ret
, "Failed to update the image header");
5319 if (backing_file
|| backing_format
) {
5320 ret
= qcow2_change_backing_file(bs
,
5321 backing_file
?: s
->image_backing_file
,
5322 backing_format
?: s
->image_backing_format
);
5324 error_setg_errno(errp
, -ret
, "Failed to change the backing file");
5329 if (s
->use_lazy_refcounts
!= lazy_refcounts
) {
5330 if (lazy_refcounts
) {
5331 if (new_version
< 3) {
5332 error_setg(errp
, "Lazy refcounts only supported with "
5333 "compatibility level 1.1 and above (use compat=1.1 "
5337 s
->compatible_features
|= QCOW2_COMPAT_LAZY_REFCOUNTS
;
5338 ret
= qcow2_update_header(bs
);
5340 s
->compatible_features
&= ~QCOW2_COMPAT_LAZY_REFCOUNTS
;
5341 error_setg_errno(errp
, -ret
, "Failed to update the image header");
5344 s
->use_lazy_refcounts
= true;
5346 /* make image clean first */
5347 ret
= qcow2_mark_clean(bs
);
5349 error_setg_errno(errp
, -ret
, "Failed to make the image clean");
5352 /* now disallow lazy refcounts */
5353 s
->compatible_features
&= ~QCOW2_COMPAT_LAZY_REFCOUNTS
;
5354 ret
= qcow2_update_header(bs
);
5356 s
->compatible_features
|= QCOW2_COMPAT_LAZY_REFCOUNTS
;
5357 error_setg_errno(errp
, -ret
, "Failed to update the image header");
5360 s
->use_lazy_refcounts
= false;
5365 BlockBackend
*blk
= blk_new(bdrv_get_aio_context(bs
),
5366 BLK_PERM_RESIZE
, BLK_PERM_ALL
);
5367 ret
= blk_insert_bs(blk
, bs
, errp
);
5374 * Amending image options should ensure that the image has
5375 * exactly the given new values, so pass exact=true here.
5377 ret
= blk_truncate(blk
, new_size
, true, PREALLOC_MODE_OFF
, errp
);
5384 /* Downgrade last (so unsupported features can be removed before) */
5385 if (new_version
< old_version
) {
5386 helper_cb_info
.current_operation
= QCOW2_DOWNGRADING
;
5387 ret
= qcow2_downgrade(bs
, new_version
, &qcow2_amend_helper_cb
,
5388 &helper_cb_info
, errp
);
5398 * If offset or size are negative, respectively, they will not be included in
5399 * the BLOCK_IMAGE_CORRUPTED event emitted.
5400 * fatal will be ignored for read-only BDS; corruptions found there will always
5401 * be considered non-fatal.
5403 void qcow2_signal_corruption(BlockDriverState
*bs
, bool fatal
, int64_t offset
,
5404 int64_t size
, const char *message_format
, ...)
5406 BDRVQcow2State
*s
= bs
->opaque
;
5407 const char *node_name
;
5411 fatal
= fatal
&& bdrv_is_writable(bs
);
5413 if (s
->signaled_corruption
&&
5414 (!fatal
|| (s
->incompatible_features
& QCOW2_INCOMPAT_CORRUPT
)))
5419 va_start(ap
, message_format
);
5420 message
= g_strdup_vprintf(message_format
, ap
);
5424 fprintf(stderr
, "qcow2: Marking image as corrupt: %s; further "
5425 "corruption events will be suppressed\n", message
);
5427 fprintf(stderr
, "qcow2: Image is corrupt: %s; further non-fatal "
5428 "corruption events will be suppressed\n", message
);
5431 node_name
= bdrv_get_node_name(bs
);
5432 qapi_event_send_block_image_corrupted(bdrv_get_device_name(bs
),
5433 *node_name
!= '\0', node_name
,
5434 message
, offset
>= 0, offset
,
5440 qcow2_mark_corrupt(bs
);
5441 bs
->drv
= NULL
; /* make BDS unusable */
5444 s
->signaled_corruption
= true;
5447 static QemuOptsList qcow2_create_opts
= {
5448 .name
= "qcow2-create-opts",
5449 .head
= QTAILQ_HEAD_INITIALIZER(qcow2_create_opts
.head
),
5452 .name
= BLOCK_OPT_SIZE
,
5453 .type
= QEMU_OPT_SIZE
,
5454 .help
= "Virtual disk size"
5457 .name
= BLOCK_OPT_COMPAT_LEVEL
,
5458 .type
= QEMU_OPT_STRING
,
5459 .help
= "Compatibility level (v2 [0.10] or v3 [1.1])"
5462 .name
= BLOCK_OPT_BACKING_FILE
,
5463 .type
= QEMU_OPT_STRING
,
5464 .help
= "File name of a base image"
5467 .name
= BLOCK_OPT_BACKING_FMT
,
5468 .type
= QEMU_OPT_STRING
,
5469 .help
= "Image format of the base image"
5472 .name
= BLOCK_OPT_DATA_FILE
,
5473 .type
= QEMU_OPT_STRING
,
5474 .help
= "File name of an external data file"
5477 .name
= BLOCK_OPT_DATA_FILE_RAW
,
5478 .type
= QEMU_OPT_BOOL
,
5479 .help
= "The external data file must stay valid as a raw image"
5482 .name
= BLOCK_OPT_ENCRYPT
,
5483 .type
= QEMU_OPT_BOOL
,
5484 .help
= "Encrypt the image with format 'aes'. (Deprecated "
5485 "in favor of " BLOCK_OPT_ENCRYPT_FORMAT
"=aes)",
5488 .name
= BLOCK_OPT_ENCRYPT_FORMAT
,
5489 .type
= QEMU_OPT_STRING
,
5490 .help
= "Encrypt the image, format choices: 'aes', 'luks'",
5492 BLOCK_CRYPTO_OPT_DEF_KEY_SECRET("encrypt.",
5493 "ID of secret providing qcow AES key or LUKS passphrase"),
5494 BLOCK_CRYPTO_OPT_DEF_LUKS_CIPHER_ALG("encrypt."),
5495 BLOCK_CRYPTO_OPT_DEF_LUKS_CIPHER_MODE("encrypt."),
5496 BLOCK_CRYPTO_OPT_DEF_LUKS_IVGEN_ALG("encrypt."),
5497 BLOCK_CRYPTO_OPT_DEF_LUKS_IVGEN_HASH_ALG("encrypt."),
5498 BLOCK_CRYPTO_OPT_DEF_LUKS_HASH_ALG("encrypt."),
5499 BLOCK_CRYPTO_OPT_DEF_LUKS_ITER_TIME("encrypt."),
5501 .name
= BLOCK_OPT_CLUSTER_SIZE
,
5502 .type
= QEMU_OPT_SIZE
,
5503 .help
= "qcow2 cluster size",
5504 .def_value_str
= stringify(DEFAULT_CLUSTER_SIZE
)
5507 .name
= BLOCK_OPT_PREALLOC
,
5508 .type
= QEMU_OPT_STRING
,
5509 .help
= "Preallocation mode (allowed values: off, metadata, "
5513 .name
= BLOCK_OPT_LAZY_REFCOUNTS
,
5514 .type
= QEMU_OPT_BOOL
,
5515 .help
= "Postpone refcount updates",
5516 .def_value_str
= "off"
5519 .name
= BLOCK_OPT_REFCOUNT_BITS
,
5520 .type
= QEMU_OPT_NUMBER
,
5521 .help
= "Width of a reference count entry in bits",
5522 .def_value_str
= "16"
5524 { /* end of list */ }
5528 static const char *const qcow2_strong_runtime_opts
[] = {
5529 "encrypt." BLOCK_CRYPTO_OPT_QCOW_KEY_SECRET
,
5534 BlockDriver bdrv_qcow2
= {
5535 .format_name
= "qcow2",
5536 .instance_size
= sizeof(BDRVQcow2State
),
5537 .bdrv_probe
= qcow2_probe
,
5538 .bdrv_open
= qcow2_open
,
5539 .bdrv_close
= qcow2_close
,
5540 .bdrv_reopen_prepare
= qcow2_reopen_prepare
,
5541 .bdrv_reopen_commit
= qcow2_reopen_commit
,
5542 .bdrv_reopen_abort
= qcow2_reopen_abort
,
5543 .bdrv_join_options
= qcow2_join_options
,
5544 .bdrv_child_perm
= bdrv_format_default_perms
,
5545 .bdrv_co_create_opts
= qcow2_co_create_opts
,
5546 .bdrv_co_create
= qcow2_co_create
,
5547 .bdrv_has_zero_init
= qcow2_has_zero_init
,
5548 .bdrv_has_zero_init_truncate
= bdrv_has_zero_init_1
,
5549 .bdrv_co_block_status
= qcow2_co_block_status
,
5551 .bdrv_co_preadv_part
= qcow2_co_preadv_part
,
5552 .bdrv_co_pwritev_part
= qcow2_co_pwritev_part
,
5553 .bdrv_co_flush_to_os
= qcow2_co_flush_to_os
,
5555 .bdrv_co_pwrite_zeroes
= qcow2_co_pwrite_zeroes
,
5556 .bdrv_co_pdiscard
= qcow2_co_pdiscard
,
5557 .bdrv_co_copy_range_from
= qcow2_co_copy_range_from
,
5558 .bdrv_co_copy_range_to
= qcow2_co_copy_range_to
,
5559 .bdrv_co_truncate
= qcow2_co_truncate
,
5560 .bdrv_co_pwritev_compressed_part
= qcow2_co_pwritev_compressed_part
,
5561 .bdrv_make_empty
= qcow2_make_empty
,
5563 .bdrv_snapshot_create
= qcow2_snapshot_create
,
5564 .bdrv_snapshot_goto
= qcow2_snapshot_goto
,
5565 .bdrv_snapshot_delete
= qcow2_snapshot_delete
,
5566 .bdrv_snapshot_list
= qcow2_snapshot_list
,
5567 .bdrv_snapshot_load_tmp
= qcow2_snapshot_load_tmp
,
5568 .bdrv_measure
= qcow2_measure
,
5569 .bdrv_get_info
= qcow2_get_info
,
5570 .bdrv_get_specific_info
= qcow2_get_specific_info
,
5572 .bdrv_save_vmstate
= qcow2_save_vmstate
,
5573 .bdrv_load_vmstate
= qcow2_load_vmstate
,
5575 .supports_backing
= true,
5576 .bdrv_change_backing_file
= qcow2_change_backing_file
,
5578 .bdrv_refresh_limits
= qcow2_refresh_limits
,
5579 .bdrv_co_invalidate_cache
= qcow2_co_invalidate_cache
,
5580 .bdrv_inactivate
= qcow2_inactivate
,
5582 .create_opts
= &qcow2_create_opts
,
5583 .strong_runtime_opts
= qcow2_strong_runtime_opts
,
5584 .mutable_opts
= mutable_opts
,
5585 .bdrv_co_check
= qcow2_co_check
,
5586 .bdrv_amend_options
= qcow2_amend_options
,
5588 .bdrv_detach_aio_context
= qcow2_detach_aio_context
,
5589 .bdrv_attach_aio_context
= qcow2_attach_aio_context
,
5591 .bdrv_co_can_store_new_dirty_bitmap
= qcow2_co_can_store_new_dirty_bitmap
,
5592 .bdrv_co_remove_persistent_dirty_bitmap
=
5593 qcow2_co_remove_persistent_dirty_bitmap
,
5596 static void bdrv_qcow2_init(void)
5598 bdrv_register(&bdrv_qcow2
);
5601 block_init(bdrv_qcow2_init
);