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 "qemu/memalign.h"
42 #include "qapi/qobject-input-visitor.h"
43 #include "qapi/qapi-visit-block-core.h"
45 #include "block/aio_task.h"
48 Differences with QCOW:
50 - Support for multiple incremental snapshots.
51 - Memory management by reference counts.
52 - Clusters which have a reference count of one have the bit
53 QCOW_OFLAG_COPIED to optimize write performance.
54 - Size of compressed clusters is stored in sectors to reduce bit usage
55 in the cluster offsets.
56 - Support for storing additional data (such as the VM state) in the
58 - If a backing store is used, the cluster size is not constrained
59 (could be backported to QCOW).
60 - L2 tables have always a size of one cluster.
67 } QEMU_PACKED QCowExtension
;
69 #define QCOW2_EXT_MAGIC_END 0
70 #define QCOW2_EXT_MAGIC_BACKING_FORMAT 0xe2792aca
71 #define QCOW2_EXT_MAGIC_FEATURE_TABLE 0x6803f857
72 #define QCOW2_EXT_MAGIC_CRYPTO_HEADER 0x0537be77
73 #define QCOW2_EXT_MAGIC_BITMAPS 0x23852875
74 #define QCOW2_EXT_MAGIC_DATA_FILE 0x44415441
76 static int coroutine_fn
77 qcow2_co_preadv_compressed(BlockDriverState
*bs
,
84 static int qcow2_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
86 const QCowHeader
*cow_header
= (const void *)buf
;
88 if (buf_size
>= sizeof(QCowHeader
) &&
89 be32_to_cpu(cow_header
->magic
) == QCOW_MAGIC
&&
90 be32_to_cpu(cow_header
->version
) >= 2)
97 static int qcow2_crypto_hdr_read_func(QCryptoBlock
*block
, size_t offset
,
98 uint8_t *buf
, size_t buflen
,
99 void *opaque
, Error
**errp
)
101 BlockDriverState
*bs
= opaque
;
102 BDRVQcow2State
*s
= bs
->opaque
;
105 if ((offset
+ buflen
) > s
->crypto_header
.length
) {
106 error_setg(errp
, "Request for data outside of extension header");
110 ret
= bdrv_pread(bs
->file
, s
->crypto_header
.offset
+ offset
, buflen
, buf
,
113 error_setg_errno(errp
, -ret
, "Could not read encryption header");
120 static int qcow2_crypto_hdr_init_func(QCryptoBlock
*block
, size_t headerlen
,
121 void *opaque
, Error
**errp
)
123 BlockDriverState
*bs
= opaque
;
124 BDRVQcow2State
*s
= bs
->opaque
;
128 ret
= qcow2_alloc_clusters(bs
, headerlen
);
130 error_setg_errno(errp
, -ret
,
131 "Cannot allocate cluster for LUKS header size %zu",
136 s
->crypto_header
.length
= headerlen
;
137 s
->crypto_header
.offset
= ret
;
140 * Zero fill all space in cluster so it has predictable
141 * content, as we may not initialize some regions of the
142 * header (eg only 1 out of 8 key slots will be initialized)
144 clusterlen
= size_to_clusters(s
, headerlen
) * s
->cluster_size
;
145 assert(qcow2_pre_write_overlap_check(bs
, 0, ret
, clusterlen
, false) == 0);
146 ret
= bdrv_pwrite_zeroes(bs
->file
,
150 error_setg_errno(errp
, -ret
, "Could not zero fill encryption header");
158 static int qcow2_crypto_hdr_write_func(QCryptoBlock
*block
, size_t offset
,
159 const uint8_t *buf
, size_t buflen
,
160 void *opaque
, Error
**errp
)
162 BlockDriverState
*bs
= opaque
;
163 BDRVQcow2State
*s
= bs
->opaque
;
166 if ((offset
+ buflen
) > s
->crypto_header
.length
) {
167 error_setg(errp
, "Request for data outside of extension header");
171 ret
= bdrv_pwrite(bs
->file
, s
->crypto_header
.offset
+ offset
, buflen
, buf
,
174 error_setg_errno(errp
, -ret
, "Could not read encryption header");
181 qcow2_extract_crypto_opts(QemuOpts
*opts
, const char *fmt
, Error
**errp
)
183 QDict
*cryptoopts_qdict
;
186 /* Extract "encrypt." options into a qdict */
187 opts_qdict
= qemu_opts_to_qdict(opts
, NULL
);
188 qdict_extract_subqdict(opts_qdict
, &cryptoopts_qdict
, "encrypt.");
189 qobject_unref(opts_qdict
);
190 qdict_put_str(cryptoopts_qdict
, "format", fmt
);
191 return cryptoopts_qdict
;
195 * read qcow2 extension and fill bs
196 * start reading from start_offset
197 * finish reading upon magic of value 0 or when end_offset reached
198 * unknown magic is skipped (future extension this version knows nothing about)
199 * return 0 upon success, non-0 otherwise
201 static int qcow2_read_extensions(BlockDriverState
*bs
, uint64_t start_offset
,
202 uint64_t end_offset
, void **p_feature_table
,
203 int flags
, bool *need_update_header
,
206 BDRVQcow2State
*s
= bs
->opaque
;
210 Qcow2BitmapHeaderExt bitmaps_ext
;
212 if (need_update_header
!= NULL
) {
213 *need_update_header
= false;
217 printf("qcow2_read_extensions: start=%ld end=%ld\n", start_offset
, end_offset
);
219 offset
= start_offset
;
220 while (offset
< end_offset
) {
224 if (offset
> s
->cluster_size
)
225 printf("qcow2_read_extension: suspicious offset %lu\n", offset
);
227 printf("attempting to read extended header in offset %lu\n", offset
);
230 ret
= bdrv_pread(bs
->file
, offset
, sizeof(ext
), &ext
, 0);
232 error_setg_errno(errp
, -ret
, "qcow2_read_extension: ERROR: "
233 "pread fail from offset %" PRIu64
, offset
);
236 ext
.magic
= be32_to_cpu(ext
.magic
);
237 ext
.len
= be32_to_cpu(ext
.len
);
238 offset
+= sizeof(ext
);
240 printf("ext.magic = 0x%x\n", ext
.magic
);
242 if (offset
> end_offset
|| ext
.len
> end_offset
- offset
) {
243 error_setg(errp
, "Header extension too large");
248 case QCOW2_EXT_MAGIC_END
:
251 case QCOW2_EXT_MAGIC_BACKING_FORMAT
:
252 if (ext
.len
>= sizeof(bs
->backing_format
)) {
253 error_setg(errp
, "ERROR: ext_backing_format: len=%" PRIu32
254 " too large (>=%zu)", ext
.len
,
255 sizeof(bs
->backing_format
));
258 ret
= bdrv_pread(bs
->file
, offset
, ext
.len
, bs
->backing_format
, 0);
260 error_setg_errno(errp
, -ret
, "ERROR: ext_backing_format: "
261 "Could not read format name");
264 bs
->backing_format
[ext
.len
] = '\0';
265 s
->image_backing_format
= g_strdup(bs
->backing_format
);
267 printf("Qcow2: Got format extension %s\n", bs
->backing_format
);
271 case QCOW2_EXT_MAGIC_FEATURE_TABLE
:
272 if (p_feature_table
!= NULL
) {
273 void *feature_table
= g_malloc0(ext
.len
+ 2 * sizeof(Qcow2Feature
));
274 ret
= bdrv_pread(bs
->file
, offset
, ext
.len
, feature_table
, 0);
276 error_setg_errno(errp
, -ret
, "ERROR: ext_feature_table: "
277 "Could not read table");
281 *p_feature_table
= feature_table
;
285 case QCOW2_EXT_MAGIC_CRYPTO_HEADER
: {
286 unsigned int cflags
= 0;
287 if (s
->crypt_method_header
!= QCOW_CRYPT_LUKS
) {
288 error_setg(errp
, "CRYPTO header extension only "
289 "expected with LUKS encryption method");
292 if (ext
.len
!= sizeof(Qcow2CryptoHeaderExtension
)) {
293 error_setg(errp
, "CRYPTO header extension size %u, "
294 "but expected size %zu", ext
.len
,
295 sizeof(Qcow2CryptoHeaderExtension
));
299 ret
= bdrv_pread(bs
->file
, offset
, ext
.len
, &s
->crypto_header
, 0);
301 error_setg_errno(errp
, -ret
,
302 "Unable to read CRYPTO header extension");
305 s
->crypto_header
.offset
= be64_to_cpu(s
->crypto_header
.offset
);
306 s
->crypto_header
.length
= be64_to_cpu(s
->crypto_header
.length
);
308 if ((s
->crypto_header
.offset
% s
->cluster_size
) != 0) {
309 error_setg(errp
, "Encryption header offset '%" PRIu64
"' is "
310 "not a multiple of cluster size '%u'",
311 s
->crypto_header
.offset
, s
->cluster_size
);
315 if (flags
& BDRV_O_NO_IO
) {
316 cflags
|= QCRYPTO_BLOCK_OPEN_NO_IO
;
318 s
->crypto
= qcrypto_block_open(s
->crypto_opts
, "encrypt.",
319 qcow2_crypto_hdr_read_func
,
320 bs
, cflags
, QCOW2_MAX_THREADS
, errp
);
326 case QCOW2_EXT_MAGIC_BITMAPS
:
327 if (ext
.len
!= sizeof(bitmaps_ext
)) {
328 error_setg_errno(errp
, -ret
, "bitmaps_ext: "
329 "Invalid extension length");
333 if (!(s
->autoclear_features
& QCOW2_AUTOCLEAR_BITMAPS
)) {
334 if (s
->qcow_version
< 3) {
335 /* Let's be a bit more specific */
336 warn_report("This qcow2 v2 image contains bitmaps, but "
337 "they may have been modified by a program "
338 "without persistent bitmap support; so now "
339 "they must all be considered inconsistent");
341 warn_report("a program lacking bitmap support "
342 "modified this file, so all bitmaps are now "
343 "considered inconsistent");
345 error_printf("Some clusters may be leaked, "
346 "run 'qemu-img check -r' on the image "
348 if (need_update_header
!= NULL
) {
349 /* Updating is needed to drop invalid bitmap extension. */
350 *need_update_header
= true;
355 ret
= bdrv_pread(bs
->file
, offset
, ext
.len
, &bitmaps_ext
, 0);
357 error_setg_errno(errp
, -ret
, "bitmaps_ext: "
358 "Could not read ext header");
362 if (bitmaps_ext
.reserved32
!= 0) {
363 error_setg_errno(errp
, -ret
, "bitmaps_ext: "
364 "Reserved field is not zero");
368 bitmaps_ext
.nb_bitmaps
= be32_to_cpu(bitmaps_ext
.nb_bitmaps
);
369 bitmaps_ext
.bitmap_directory_size
=
370 be64_to_cpu(bitmaps_ext
.bitmap_directory_size
);
371 bitmaps_ext
.bitmap_directory_offset
=
372 be64_to_cpu(bitmaps_ext
.bitmap_directory_offset
);
374 if (bitmaps_ext
.nb_bitmaps
> QCOW2_MAX_BITMAPS
) {
376 "bitmaps_ext: Image has %" PRIu32
" bitmaps, "
377 "exceeding the QEMU supported maximum of %d",
378 bitmaps_ext
.nb_bitmaps
, QCOW2_MAX_BITMAPS
);
382 if (bitmaps_ext
.nb_bitmaps
== 0) {
383 error_setg(errp
, "found bitmaps extension with zero bitmaps");
387 if (offset_into_cluster(s
, bitmaps_ext
.bitmap_directory_offset
)) {
388 error_setg(errp
, "bitmaps_ext: "
389 "invalid bitmap directory offset");
393 if (bitmaps_ext
.bitmap_directory_size
>
394 QCOW2_MAX_BITMAP_DIRECTORY_SIZE
) {
395 error_setg(errp
, "bitmaps_ext: "
396 "bitmap directory size (%" PRIu64
") exceeds "
397 "the maximum supported size (%d)",
398 bitmaps_ext
.bitmap_directory_size
,
399 QCOW2_MAX_BITMAP_DIRECTORY_SIZE
);
403 s
->nb_bitmaps
= bitmaps_ext
.nb_bitmaps
;
404 s
->bitmap_directory_offset
=
405 bitmaps_ext
.bitmap_directory_offset
;
406 s
->bitmap_directory_size
=
407 bitmaps_ext
.bitmap_directory_size
;
410 printf("Qcow2: Got bitmaps extension: "
411 "offset=%" PRIu64
" nb_bitmaps=%" PRIu32
"\n",
412 s
->bitmap_directory_offset
, s
->nb_bitmaps
);
416 case QCOW2_EXT_MAGIC_DATA_FILE
:
418 s
->image_data_file
= g_malloc0(ext
.len
+ 1);
419 ret
= bdrv_pread(bs
->file
, offset
, ext
.len
, s
->image_data_file
, 0);
421 error_setg_errno(errp
, -ret
,
422 "ERROR: Could not read data file name");
426 printf("Qcow2: Got external data file %s\n", s
->image_data_file
);
432 /* unknown magic - save it in case we need to rewrite the header */
433 /* If you add a new feature, make sure to also update the fast
434 * path of qcow2_make_empty() to deal with it. */
436 Qcow2UnknownHeaderExtension
*uext
;
438 uext
= g_malloc0(sizeof(*uext
) + ext
.len
);
439 uext
->magic
= ext
.magic
;
441 QLIST_INSERT_HEAD(&s
->unknown_header_ext
, uext
, next
);
443 ret
= bdrv_pread(bs
->file
, offset
, uext
->len
, uext
->data
, 0);
445 error_setg_errno(errp
, -ret
, "ERROR: unknown extension: "
446 "Could not read data");
453 offset
+= ((ext
.len
+ 7) & ~7);
459 static void cleanup_unknown_header_ext(BlockDriverState
*bs
)
461 BDRVQcow2State
*s
= bs
->opaque
;
462 Qcow2UnknownHeaderExtension
*uext
, *next
;
464 QLIST_FOREACH_SAFE(uext
, &s
->unknown_header_ext
, next
, next
) {
465 QLIST_REMOVE(uext
, next
);
470 static void report_unsupported_feature(Error
**errp
, Qcow2Feature
*table
,
473 g_autoptr(GString
) features
= g_string_sized_new(60);
475 while (table
&& table
->name
[0] != '\0') {
476 if (table
->type
== QCOW2_FEAT_TYPE_INCOMPATIBLE
) {
477 if (mask
& (1ULL << table
->bit
)) {
478 if (features
->len
> 0) {
479 g_string_append(features
, ", ");
481 g_string_append_printf(features
, "%.46s", table
->name
);
482 mask
&= ~(1ULL << table
->bit
);
489 if (features
->len
> 0) {
490 g_string_append(features
, ", ");
492 g_string_append_printf(features
,
493 "Unknown incompatible feature: %" PRIx64
, mask
);
496 error_setg(errp
, "Unsupported qcow2 feature(s): %s", features
->str
);
500 * Sets the dirty bit and flushes afterwards if necessary.
502 * The incompatible_features bit is only set if the image file header was
503 * updated successfully. Therefore it is not required to check the return
504 * value of this function.
506 int qcow2_mark_dirty(BlockDriverState
*bs
)
508 BDRVQcow2State
*s
= bs
->opaque
;
512 assert(s
->qcow_version
>= 3);
514 if (s
->incompatible_features
& QCOW2_INCOMPAT_DIRTY
) {
515 return 0; /* already dirty */
518 val
= cpu_to_be64(s
->incompatible_features
| QCOW2_INCOMPAT_DIRTY
);
519 ret
= bdrv_pwrite_sync(bs
->file
,
520 offsetof(QCowHeader
, incompatible_features
),
521 sizeof(val
), &val
, 0);
526 /* Only treat image as dirty if the header was updated successfully */
527 s
->incompatible_features
|= QCOW2_INCOMPAT_DIRTY
;
532 * Clears the dirty bit and flushes before if necessary. Only call this
533 * function when there are no pending requests, it does not guard against
534 * concurrent requests dirtying the image.
536 static int qcow2_mark_clean(BlockDriverState
*bs
)
538 BDRVQcow2State
*s
= bs
->opaque
;
540 if (s
->incompatible_features
& QCOW2_INCOMPAT_DIRTY
) {
543 s
->incompatible_features
&= ~QCOW2_INCOMPAT_DIRTY
;
545 ret
= qcow2_flush_caches(bs
);
550 return qcow2_update_header(bs
);
556 * Marks the image as corrupt.
558 int qcow2_mark_corrupt(BlockDriverState
*bs
)
560 BDRVQcow2State
*s
= bs
->opaque
;
562 s
->incompatible_features
|= QCOW2_INCOMPAT_CORRUPT
;
563 return qcow2_update_header(bs
);
567 * Marks the image as consistent, i.e., unsets the corrupt bit, and flushes
568 * before if necessary.
570 int qcow2_mark_consistent(BlockDriverState
*bs
)
572 BDRVQcow2State
*s
= bs
->opaque
;
574 if (s
->incompatible_features
& QCOW2_INCOMPAT_CORRUPT
) {
575 int ret
= qcow2_flush_caches(bs
);
580 s
->incompatible_features
&= ~QCOW2_INCOMPAT_CORRUPT
;
581 return qcow2_update_header(bs
);
586 static void qcow2_add_check_result(BdrvCheckResult
*out
,
587 const BdrvCheckResult
*src
,
588 bool set_allocation_info
)
590 out
->corruptions
+= src
->corruptions
;
591 out
->leaks
+= src
->leaks
;
592 out
->check_errors
+= src
->check_errors
;
593 out
->corruptions_fixed
+= src
->corruptions_fixed
;
594 out
->leaks_fixed
+= src
->leaks_fixed
;
596 if (set_allocation_info
) {
597 out
->image_end_offset
= src
->image_end_offset
;
602 static int coroutine_fn
qcow2_co_check_locked(BlockDriverState
*bs
,
603 BdrvCheckResult
*result
,
606 BdrvCheckResult snapshot_res
= {};
607 BdrvCheckResult refcount_res
= {};
610 memset(result
, 0, sizeof(*result
));
612 ret
= qcow2_check_read_snapshot_table(bs
, &snapshot_res
, fix
);
614 qcow2_add_check_result(result
, &snapshot_res
, false);
618 ret
= qcow2_check_refcounts(bs
, &refcount_res
, fix
);
619 qcow2_add_check_result(result
, &refcount_res
, true);
621 qcow2_add_check_result(result
, &snapshot_res
, false);
625 ret
= qcow2_check_fix_snapshot_table(bs
, &snapshot_res
, fix
);
626 qcow2_add_check_result(result
, &snapshot_res
, false);
631 if (fix
&& result
->check_errors
== 0 && result
->corruptions
== 0) {
632 ret
= qcow2_mark_clean(bs
);
636 return qcow2_mark_consistent(bs
);
641 static int coroutine_fn
qcow2_co_check(BlockDriverState
*bs
,
642 BdrvCheckResult
*result
,
645 BDRVQcow2State
*s
= bs
->opaque
;
648 qemu_co_mutex_lock(&s
->lock
);
649 ret
= qcow2_co_check_locked(bs
, result
, fix
);
650 qemu_co_mutex_unlock(&s
->lock
);
654 int qcow2_validate_table(BlockDriverState
*bs
, uint64_t offset
,
655 uint64_t entries
, size_t entry_len
,
656 int64_t max_size_bytes
, const char *table_name
,
659 BDRVQcow2State
*s
= bs
->opaque
;
661 if (entries
> max_size_bytes
/ entry_len
) {
662 error_setg(errp
, "%s too large", table_name
);
666 /* Use signed INT64_MAX as the maximum even for uint64_t header fields,
667 * because values will be passed to qemu functions taking int64_t. */
668 if ((INT64_MAX
- entries
* entry_len
< offset
) ||
669 (offset_into_cluster(s
, offset
) != 0)) {
670 error_setg(errp
, "%s offset invalid", table_name
);
677 static const char *const mutable_opts
[] = {
678 QCOW2_OPT_LAZY_REFCOUNTS
,
679 QCOW2_OPT_DISCARD_REQUEST
,
680 QCOW2_OPT_DISCARD_SNAPSHOT
,
681 QCOW2_OPT_DISCARD_OTHER
,
683 QCOW2_OPT_OVERLAP_TEMPLATE
,
684 QCOW2_OPT_OVERLAP_MAIN_HEADER
,
685 QCOW2_OPT_OVERLAP_ACTIVE_L1
,
686 QCOW2_OPT_OVERLAP_ACTIVE_L2
,
687 QCOW2_OPT_OVERLAP_REFCOUNT_TABLE
,
688 QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK
,
689 QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE
,
690 QCOW2_OPT_OVERLAP_INACTIVE_L1
,
691 QCOW2_OPT_OVERLAP_INACTIVE_L2
,
692 QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY
,
693 QCOW2_OPT_CACHE_SIZE
,
694 QCOW2_OPT_L2_CACHE_SIZE
,
695 QCOW2_OPT_L2_CACHE_ENTRY_SIZE
,
696 QCOW2_OPT_REFCOUNT_CACHE_SIZE
,
697 QCOW2_OPT_CACHE_CLEAN_INTERVAL
,
701 static QemuOptsList qcow2_runtime_opts
= {
703 .head
= QTAILQ_HEAD_INITIALIZER(qcow2_runtime_opts
.head
),
706 .name
= QCOW2_OPT_LAZY_REFCOUNTS
,
707 .type
= QEMU_OPT_BOOL
,
708 .help
= "Postpone refcount updates",
711 .name
= QCOW2_OPT_DISCARD_REQUEST
,
712 .type
= QEMU_OPT_BOOL
,
713 .help
= "Pass guest discard requests to the layer below",
716 .name
= QCOW2_OPT_DISCARD_SNAPSHOT
,
717 .type
= QEMU_OPT_BOOL
,
718 .help
= "Generate discard requests when snapshot related space "
722 .name
= QCOW2_OPT_DISCARD_OTHER
,
723 .type
= QEMU_OPT_BOOL
,
724 .help
= "Generate discard requests when other clusters are freed",
727 .name
= QCOW2_OPT_OVERLAP
,
728 .type
= QEMU_OPT_STRING
,
729 .help
= "Selects which overlap checks to perform from a range of "
730 "templates (none, constant, cached, all)",
733 .name
= QCOW2_OPT_OVERLAP_TEMPLATE
,
734 .type
= QEMU_OPT_STRING
,
735 .help
= "Selects which overlap checks to perform from a range of "
736 "templates (none, constant, cached, all)",
739 .name
= QCOW2_OPT_OVERLAP_MAIN_HEADER
,
740 .type
= QEMU_OPT_BOOL
,
741 .help
= "Check for unintended writes into the main qcow2 header",
744 .name
= QCOW2_OPT_OVERLAP_ACTIVE_L1
,
745 .type
= QEMU_OPT_BOOL
,
746 .help
= "Check for unintended writes into the active L1 table",
749 .name
= QCOW2_OPT_OVERLAP_ACTIVE_L2
,
750 .type
= QEMU_OPT_BOOL
,
751 .help
= "Check for unintended writes into an active L2 table",
754 .name
= QCOW2_OPT_OVERLAP_REFCOUNT_TABLE
,
755 .type
= QEMU_OPT_BOOL
,
756 .help
= "Check for unintended writes into the refcount table",
759 .name
= QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK
,
760 .type
= QEMU_OPT_BOOL
,
761 .help
= "Check for unintended writes into a refcount block",
764 .name
= QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE
,
765 .type
= QEMU_OPT_BOOL
,
766 .help
= "Check for unintended writes into the snapshot table",
769 .name
= QCOW2_OPT_OVERLAP_INACTIVE_L1
,
770 .type
= QEMU_OPT_BOOL
,
771 .help
= "Check for unintended writes into an inactive L1 table",
774 .name
= QCOW2_OPT_OVERLAP_INACTIVE_L2
,
775 .type
= QEMU_OPT_BOOL
,
776 .help
= "Check for unintended writes into an inactive L2 table",
779 .name
= QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY
,
780 .type
= QEMU_OPT_BOOL
,
781 .help
= "Check for unintended writes into the bitmap directory",
784 .name
= QCOW2_OPT_CACHE_SIZE
,
785 .type
= QEMU_OPT_SIZE
,
786 .help
= "Maximum combined metadata (L2 tables and refcount blocks) "
790 .name
= QCOW2_OPT_L2_CACHE_SIZE
,
791 .type
= QEMU_OPT_SIZE
,
792 .help
= "Maximum L2 table cache size",
795 .name
= QCOW2_OPT_L2_CACHE_ENTRY_SIZE
,
796 .type
= QEMU_OPT_SIZE
,
797 .help
= "Size of each entry in the L2 cache",
800 .name
= QCOW2_OPT_REFCOUNT_CACHE_SIZE
,
801 .type
= QEMU_OPT_SIZE
,
802 .help
= "Maximum refcount block cache size",
805 .name
= QCOW2_OPT_CACHE_CLEAN_INTERVAL
,
806 .type
= QEMU_OPT_NUMBER
,
807 .help
= "Clean unused cache entries after this time (in seconds)",
809 BLOCK_CRYPTO_OPT_DEF_KEY_SECRET("encrypt.",
810 "ID of secret providing qcow2 AES key or LUKS passphrase"),
811 { /* end of list */ }
815 static const char *overlap_bool_option_names
[QCOW2_OL_MAX_BITNR
] = {
816 [QCOW2_OL_MAIN_HEADER_BITNR
] = QCOW2_OPT_OVERLAP_MAIN_HEADER
,
817 [QCOW2_OL_ACTIVE_L1_BITNR
] = QCOW2_OPT_OVERLAP_ACTIVE_L1
,
818 [QCOW2_OL_ACTIVE_L2_BITNR
] = QCOW2_OPT_OVERLAP_ACTIVE_L2
,
819 [QCOW2_OL_REFCOUNT_TABLE_BITNR
] = QCOW2_OPT_OVERLAP_REFCOUNT_TABLE
,
820 [QCOW2_OL_REFCOUNT_BLOCK_BITNR
] = QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK
,
821 [QCOW2_OL_SNAPSHOT_TABLE_BITNR
] = QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE
,
822 [QCOW2_OL_INACTIVE_L1_BITNR
] = QCOW2_OPT_OVERLAP_INACTIVE_L1
,
823 [QCOW2_OL_INACTIVE_L2_BITNR
] = QCOW2_OPT_OVERLAP_INACTIVE_L2
,
824 [QCOW2_OL_BITMAP_DIRECTORY_BITNR
] = QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY
,
827 static void cache_clean_timer_cb(void *opaque
)
829 BlockDriverState
*bs
= opaque
;
830 BDRVQcow2State
*s
= bs
->opaque
;
831 qcow2_cache_clean_unused(s
->l2_table_cache
);
832 qcow2_cache_clean_unused(s
->refcount_block_cache
);
833 timer_mod(s
->cache_clean_timer
, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
) +
834 (int64_t) s
->cache_clean_interval
* 1000);
837 static void cache_clean_timer_init(BlockDriverState
*bs
, AioContext
*context
)
839 BDRVQcow2State
*s
= bs
->opaque
;
840 if (s
->cache_clean_interval
> 0) {
841 s
->cache_clean_timer
=
842 aio_timer_new_with_attrs(context
, QEMU_CLOCK_VIRTUAL
,
843 SCALE_MS
, QEMU_TIMER_ATTR_EXTERNAL
,
844 cache_clean_timer_cb
, bs
);
845 timer_mod(s
->cache_clean_timer
, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
) +
846 (int64_t) s
->cache_clean_interval
* 1000);
850 static void cache_clean_timer_del(BlockDriverState
*bs
)
852 BDRVQcow2State
*s
= bs
->opaque
;
853 if (s
->cache_clean_timer
) {
854 timer_free(s
->cache_clean_timer
);
855 s
->cache_clean_timer
= NULL
;
859 static void qcow2_detach_aio_context(BlockDriverState
*bs
)
861 cache_clean_timer_del(bs
);
864 static void qcow2_attach_aio_context(BlockDriverState
*bs
,
865 AioContext
*new_context
)
867 cache_clean_timer_init(bs
, new_context
);
870 static bool read_cache_sizes(BlockDriverState
*bs
, QemuOpts
*opts
,
871 uint64_t *l2_cache_size
,
872 uint64_t *l2_cache_entry_size
,
873 uint64_t *refcount_cache_size
, Error
**errp
)
875 BDRVQcow2State
*s
= bs
->opaque
;
876 uint64_t combined_cache_size
, l2_cache_max_setting
;
877 bool l2_cache_size_set
, refcount_cache_size_set
, combined_cache_size_set
;
878 bool l2_cache_entry_size_set
;
879 int min_refcount_cache
= MIN_REFCOUNT_CACHE_SIZE
* s
->cluster_size
;
880 uint64_t virtual_disk_size
= bs
->total_sectors
* BDRV_SECTOR_SIZE
;
881 uint64_t max_l2_entries
= DIV_ROUND_UP(virtual_disk_size
, s
->cluster_size
);
882 /* An L2 table is always one cluster in size so the max cache size
883 * should be a multiple of the cluster size. */
884 uint64_t max_l2_cache
= ROUND_UP(max_l2_entries
* l2_entry_size(s
),
887 combined_cache_size_set
= qemu_opt_get(opts
, QCOW2_OPT_CACHE_SIZE
);
888 l2_cache_size_set
= qemu_opt_get(opts
, QCOW2_OPT_L2_CACHE_SIZE
);
889 refcount_cache_size_set
= qemu_opt_get(opts
, QCOW2_OPT_REFCOUNT_CACHE_SIZE
);
890 l2_cache_entry_size_set
= qemu_opt_get(opts
, QCOW2_OPT_L2_CACHE_ENTRY_SIZE
);
892 combined_cache_size
= qemu_opt_get_size(opts
, QCOW2_OPT_CACHE_SIZE
, 0);
893 l2_cache_max_setting
= qemu_opt_get_size(opts
, QCOW2_OPT_L2_CACHE_SIZE
,
894 DEFAULT_L2_CACHE_MAX_SIZE
);
895 *refcount_cache_size
= qemu_opt_get_size(opts
,
896 QCOW2_OPT_REFCOUNT_CACHE_SIZE
, 0);
898 *l2_cache_entry_size
= qemu_opt_get_size(
899 opts
, QCOW2_OPT_L2_CACHE_ENTRY_SIZE
, s
->cluster_size
);
901 *l2_cache_size
= MIN(max_l2_cache
, l2_cache_max_setting
);
903 if (combined_cache_size_set
) {
904 if (l2_cache_size_set
&& refcount_cache_size_set
) {
905 error_setg(errp
, QCOW2_OPT_CACHE_SIZE
", " QCOW2_OPT_L2_CACHE_SIZE
906 " and " QCOW2_OPT_REFCOUNT_CACHE_SIZE
" may not be set "
909 } else if (l2_cache_size_set
&&
910 (l2_cache_max_setting
> combined_cache_size
)) {
911 error_setg(errp
, QCOW2_OPT_L2_CACHE_SIZE
" may not exceed "
912 QCOW2_OPT_CACHE_SIZE
);
914 } else if (*refcount_cache_size
> combined_cache_size
) {
915 error_setg(errp
, QCOW2_OPT_REFCOUNT_CACHE_SIZE
" may not exceed "
916 QCOW2_OPT_CACHE_SIZE
);
920 if (l2_cache_size_set
) {
921 *refcount_cache_size
= combined_cache_size
- *l2_cache_size
;
922 } else if (refcount_cache_size_set
) {
923 *l2_cache_size
= combined_cache_size
- *refcount_cache_size
;
925 /* Assign as much memory as possible to the L2 cache, and
926 * use the remainder for the refcount cache */
927 if (combined_cache_size
>= max_l2_cache
+ min_refcount_cache
) {
928 *l2_cache_size
= max_l2_cache
;
929 *refcount_cache_size
= combined_cache_size
- *l2_cache_size
;
931 *refcount_cache_size
=
932 MIN(combined_cache_size
, min_refcount_cache
);
933 *l2_cache_size
= combined_cache_size
- *refcount_cache_size
;
939 * If the L2 cache is not enough to cover the whole disk then
940 * default to 4KB entries. Smaller entries reduce the cost of
941 * loads and evictions and increase I/O performance.
943 if (*l2_cache_size
< max_l2_cache
&& !l2_cache_entry_size_set
) {
944 *l2_cache_entry_size
= MIN(s
->cluster_size
, 4096);
947 /* l2_cache_size and refcount_cache_size are ensured to have at least
948 * their minimum values in qcow2_update_options_prepare() */
950 if (*l2_cache_entry_size
< (1 << MIN_CLUSTER_BITS
) ||
951 *l2_cache_entry_size
> s
->cluster_size
||
952 !is_power_of_2(*l2_cache_entry_size
)) {
953 error_setg(errp
, "L2 cache entry size must be a power of two "
954 "between %d and the cluster size (%d)",
955 1 << MIN_CLUSTER_BITS
, s
->cluster_size
);
962 typedef struct Qcow2ReopenState
{
963 Qcow2Cache
*l2_table_cache
;
964 Qcow2Cache
*refcount_block_cache
;
965 int l2_slice_size
; /* Number of entries in a slice of the L2 table */
966 bool use_lazy_refcounts
;
968 bool discard_passthrough
[QCOW2_DISCARD_MAX
];
969 uint64_t cache_clean_interval
;
970 QCryptoBlockOpenOptions
*crypto_opts
; /* Disk encryption runtime options */
973 static int qcow2_update_options_prepare(BlockDriverState
*bs
,
975 QDict
*options
, int flags
,
978 BDRVQcow2State
*s
= bs
->opaque
;
979 QemuOpts
*opts
= NULL
;
980 const char *opt_overlap_check
, *opt_overlap_check_template
;
981 int overlap_check_template
= 0;
982 uint64_t l2_cache_size
, l2_cache_entry_size
, refcount_cache_size
;
984 const char *encryptfmt
;
985 QDict
*encryptopts
= NULL
;
988 qdict_extract_subqdict(options
, &encryptopts
, "encrypt.");
989 encryptfmt
= qdict_get_try_str(encryptopts
, "format");
991 opts
= qemu_opts_create(&qcow2_runtime_opts
, NULL
, 0, &error_abort
);
992 if (!qemu_opts_absorb_qdict(opts
, options
, errp
)) {
997 /* get L2 table/refcount block cache size from command line options */
998 if (!read_cache_sizes(bs
, opts
, &l2_cache_size
, &l2_cache_entry_size
,
999 &refcount_cache_size
, errp
)) {
1004 l2_cache_size
/= l2_cache_entry_size
;
1005 if (l2_cache_size
< MIN_L2_CACHE_SIZE
) {
1006 l2_cache_size
= MIN_L2_CACHE_SIZE
;
1008 if (l2_cache_size
> INT_MAX
) {
1009 error_setg(errp
, "L2 cache size too big");
1014 refcount_cache_size
/= s
->cluster_size
;
1015 if (refcount_cache_size
< MIN_REFCOUNT_CACHE_SIZE
) {
1016 refcount_cache_size
= MIN_REFCOUNT_CACHE_SIZE
;
1018 if (refcount_cache_size
> INT_MAX
) {
1019 error_setg(errp
, "Refcount cache size too big");
1024 /* alloc new L2 table/refcount block cache, flush old one */
1025 if (s
->l2_table_cache
) {
1026 ret
= qcow2_cache_flush(bs
, s
->l2_table_cache
);
1028 error_setg_errno(errp
, -ret
, "Failed to flush the L2 table cache");
1033 if (s
->refcount_block_cache
) {
1034 ret
= qcow2_cache_flush(bs
, s
->refcount_block_cache
);
1036 error_setg_errno(errp
, -ret
,
1037 "Failed to flush the refcount block cache");
1042 r
->l2_slice_size
= l2_cache_entry_size
/ l2_entry_size(s
);
1043 r
->l2_table_cache
= qcow2_cache_create(bs
, l2_cache_size
,
1044 l2_cache_entry_size
);
1045 r
->refcount_block_cache
= qcow2_cache_create(bs
, refcount_cache_size
,
1047 if (r
->l2_table_cache
== NULL
|| r
->refcount_block_cache
== NULL
) {
1048 error_setg(errp
, "Could not allocate metadata caches");
1053 /* New interval for cache cleanup timer */
1054 r
->cache_clean_interval
=
1055 qemu_opt_get_number(opts
, QCOW2_OPT_CACHE_CLEAN_INTERVAL
,
1056 DEFAULT_CACHE_CLEAN_INTERVAL
);
1057 #ifndef CONFIG_LINUX
1058 if (r
->cache_clean_interval
!= 0) {
1059 error_setg(errp
, QCOW2_OPT_CACHE_CLEAN_INTERVAL
1060 " not supported on this host");
1065 if (r
->cache_clean_interval
> UINT_MAX
) {
1066 error_setg(errp
, "Cache clean interval too big");
1071 /* lazy-refcounts; flush if going from enabled to disabled */
1072 r
->use_lazy_refcounts
= qemu_opt_get_bool(opts
, QCOW2_OPT_LAZY_REFCOUNTS
,
1073 (s
->compatible_features
& QCOW2_COMPAT_LAZY_REFCOUNTS
));
1074 if (r
->use_lazy_refcounts
&& s
->qcow_version
< 3) {
1075 error_setg(errp
, "Lazy refcounts require a qcow2 image with at least "
1076 "qemu 1.1 compatibility level");
1081 if (s
->use_lazy_refcounts
&& !r
->use_lazy_refcounts
) {
1082 ret
= qcow2_mark_clean(bs
);
1084 error_setg_errno(errp
, -ret
, "Failed to disable lazy refcounts");
1089 /* Overlap check options */
1090 opt_overlap_check
= qemu_opt_get(opts
, QCOW2_OPT_OVERLAP
);
1091 opt_overlap_check_template
= qemu_opt_get(opts
, QCOW2_OPT_OVERLAP_TEMPLATE
);
1092 if (opt_overlap_check_template
&& opt_overlap_check
&&
1093 strcmp(opt_overlap_check_template
, opt_overlap_check
))
1095 error_setg(errp
, "Conflicting values for qcow2 options '"
1096 QCOW2_OPT_OVERLAP
"' ('%s') and '" QCOW2_OPT_OVERLAP_TEMPLATE
1097 "' ('%s')", opt_overlap_check
, opt_overlap_check_template
);
1101 if (!opt_overlap_check
) {
1102 opt_overlap_check
= opt_overlap_check_template
?: "cached";
1105 if (!strcmp(opt_overlap_check
, "none")) {
1106 overlap_check_template
= 0;
1107 } else if (!strcmp(opt_overlap_check
, "constant")) {
1108 overlap_check_template
= QCOW2_OL_CONSTANT
;
1109 } else if (!strcmp(opt_overlap_check
, "cached")) {
1110 overlap_check_template
= QCOW2_OL_CACHED
;
1111 } else if (!strcmp(opt_overlap_check
, "all")) {
1112 overlap_check_template
= QCOW2_OL_ALL
;
1114 error_setg(errp
, "Unsupported value '%s' for qcow2 option "
1115 "'overlap-check'. Allowed are any of the following: "
1116 "none, constant, cached, all", opt_overlap_check
);
1121 r
->overlap_check
= 0;
1122 for (i
= 0; i
< QCOW2_OL_MAX_BITNR
; i
++) {
1123 /* overlap-check defines a template bitmask, but every flag may be
1124 * overwritten through the associated boolean option */
1126 qemu_opt_get_bool(opts
, overlap_bool_option_names
[i
],
1127 overlap_check_template
& (1 << i
)) << i
;
1130 r
->discard_passthrough
[QCOW2_DISCARD_NEVER
] = false;
1131 r
->discard_passthrough
[QCOW2_DISCARD_ALWAYS
] = true;
1132 r
->discard_passthrough
[QCOW2_DISCARD_REQUEST
] =
1133 qemu_opt_get_bool(opts
, QCOW2_OPT_DISCARD_REQUEST
,
1134 flags
& BDRV_O_UNMAP
);
1135 r
->discard_passthrough
[QCOW2_DISCARD_SNAPSHOT
] =
1136 qemu_opt_get_bool(opts
, QCOW2_OPT_DISCARD_SNAPSHOT
, true);
1137 r
->discard_passthrough
[QCOW2_DISCARD_OTHER
] =
1138 qemu_opt_get_bool(opts
, QCOW2_OPT_DISCARD_OTHER
, false);
1140 switch (s
->crypt_method_header
) {
1141 case QCOW_CRYPT_NONE
:
1143 error_setg(errp
, "No encryption in image header, but options "
1144 "specified format '%s'", encryptfmt
);
1150 case QCOW_CRYPT_AES
:
1151 if (encryptfmt
&& !g_str_equal(encryptfmt
, "aes")) {
1153 "Header reported 'aes' encryption format but "
1154 "options specify '%s'", encryptfmt
);
1158 qdict_put_str(encryptopts
, "format", "qcow");
1159 r
->crypto_opts
= block_crypto_open_opts_init(encryptopts
, errp
);
1160 if (!r
->crypto_opts
) {
1166 case QCOW_CRYPT_LUKS
:
1167 if (encryptfmt
&& !g_str_equal(encryptfmt
, "luks")) {
1169 "Header reported 'luks' encryption format but "
1170 "options specify '%s'", encryptfmt
);
1174 qdict_put_str(encryptopts
, "format", "luks");
1175 r
->crypto_opts
= block_crypto_open_opts_init(encryptopts
, errp
);
1176 if (!r
->crypto_opts
) {
1183 error_setg(errp
, "Unsupported encryption method %d",
1184 s
->crypt_method_header
);
1191 qobject_unref(encryptopts
);
1192 qemu_opts_del(opts
);
1197 static void qcow2_update_options_commit(BlockDriverState
*bs
,
1198 Qcow2ReopenState
*r
)
1200 BDRVQcow2State
*s
= bs
->opaque
;
1203 if (s
->l2_table_cache
) {
1204 qcow2_cache_destroy(s
->l2_table_cache
);
1206 if (s
->refcount_block_cache
) {
1207 qcow2_cache_destroy(s
->refcount_block_cache
);
1209 s
->l2_table_cache
= r
->l2_table_cache
;
1210 s
->refcount_block_cache
= r
->refcount_block_cache
;
1211 s
->l2_slice_size
= r
->l2_slice_size
;
1213 s
->overlap_check
= r
->overlap_check
;
1214 s
->use_lazy_refcounts
= r
->use_lazy_refcounts
;
1216 for (i
= 0; i
< QCOW2_DISCARD_MAX
; i
++) {
1217 s
->discard_passthrough
[i
] = r
->discard_passthrough
[i
];
1220 if (s
->cache_clean_interval
!= r
->cache_clean_interval
) {
1221 cache_clean_timer_del(bs
);
1222 s
->cache_clean_interval
= r
->cache_clean_interval
;
1223 cache_clean_timer_init(bs
, bdrv_get_aio_context(bs
));
1226 qapi_free_QCryptoBlockOpenOptions(s
->crypto_opts
);
1227 s
->crypto_opts
= r
->crypto_opts
;
1230 static void qcow2_update_options_abort(BlockDriverState
*bs
,
1231 Qcow2ReopenState
*r
)
1233 if (r
->l2_table_cache
) {
1234 qcow2_cache_destroy(r
->l2_table_cache
);
1236 if (r
->refcount_block_cache
) {
1237 qcow2_cache_destroy(r
->refcount_block_cache
);
1239 qapi_free_QCryptoBlockOpenOptions(r
->crypto_opts
);
1242 static int qcow2_update_options(BlockDriverState
*bs
, QDict
*options
,
1243 int flags
, Error
**errp
)
1245 Qcow2ReopenState r
= {};
1248 ret
= qcow2_update_options_prepare(bs
, &r
, options
, flags
, errp
);
1250 qcow2_update_options_commit(bs
, &r
);
1252 qcow2_update_options_abort(bs
, &r
);
1258 static int validate_compression_type(BDRVQcow2State
*s
, Error
**errp
)
1260 switch (s
->compression_type
) {
1261 case QCOW2_COMPRESSION_TYPE_ZLIB
:
1263 case QCOW2_COMPRESSION_TYPE_ZSTD
:
1268 error_setg(errp
, "qcow2: unknown compression type: %u",
1269 s
->compression_type
);
1274 * if the compression type differs from QCOW2_COMPRESSION_TYPE_ZLIB
1275 * the incompatible feature flag must be set
1277 if (s
->compression_type
== QCOW2_COMPRESSION_TYPE_ZLIB
) {
1278 if (s
->incompatible_features
& QCOW2_INCOMPAT_COMPRESSION
) {
1279 error_setg(errp
, "qcow2: Compression type incompatible feature "
1280 "bit must not be set");
1284 if (!(s
->incompatible_features
& QCOW2_INCOMPAT_COMPRESSION
)) {
1285 error_setg(errp
, "qcow2: Compression type incompatible feature "
1294 /* Called with s->lock held. */
1295 static int coroutine_fn
qcow2_do_open(BlockDriverState
*bs
, QDict
*options
,
1296 int flags
, bool open_data_file
,
1300 BDRVQcow2State
*s
= bs
->opaque
;
1301 unsigned int len
, i
;
1305 uint64_t l1_vm_state_index
;
1306 bool update_header
= false;
1308 ret
= bdrv_pread(bs
->file
, 0, sizeof(header
), &header
, 0);
1310 error_setg_errno(errp
, -ret
, "Could not read qcow2 header");
1313 header
.magic
= be32_to_cpu(header
.magic
);
1314 header
.version
= be32_to_cpu(header
.version
);
1315 header
.backing_file_offset
= be64_to_cpu(header
.backing_file_offset
);
1316 header
.backing_file_size
= be32_to_cpu(header
.backing_file_size
);
1317 header
.size
= be64_to_cpu(header
.size
);
1318 header
.cluster_bits
= be32_to_cpu(header
.cluster_bits
);
1319 header
.crypt_method
= be32_to_cpu(header
.crypt_method
);
1320 header
.l1_table_offset
= be64_to_cpu(header
.l1_table_offset
);
1321 header
.l1_size
= be32_to_cpu(header
.l1_size
);
1322 header
.refcount_table_offset
= be64_to_cpu(header
.refcount_table_offset
);
1323 header
.refcount_table_clusters
=
1324 be32_to_cpu(header
.refcount_table_clusters
);
1325 header
.snapshots_offset
= be64_to_cpu(header
.snapshots_offset
);
1326 header
.nb_snapshots
= be32_to_cpu(header
.nb_snapshots
);
1328 if (header
.magic
!= QCOW_MAGIC
) {
1329 error_setg(errp
, "Image is not in qcow2 format");
1333 if (header
.version
< 2 || header
.version
> 3) {
1334 error_setg(errp
, "Unsupported qcow2 version %" PRIu32
, header
.version
);
1339 s
->qcow_version
= header
.version
;
1341 /* Initialise cluster size */
1342 if (header
.cluster_bits
< MIN_CLUSTER_BITS
||
1343 header
.cluster_bits
> MAX_CLUSTER_BITS
) {
1344 error_setg(errp
, "Unsupported cluster size: 2^%" PRIu32
,
1345 header
.cluster_bits
);
1350 s
->cluster_bits
= header
.cluster_bits
;
1351 s
->cluster_size
= 1 << s
->cluster_bits
;
1353 /* Initialise version 3 header fields */
1354 if (header
.version
== 2) {
1355 header
.incompatible_features
= 0;
1356 header
.compatible_features
= 0;
1357 header
.autoclear_features
= 0;
1358 header
.refcount_order
= 4;
1359 header
.header_length
= 72;
1361 header
.incompatible_features
=
1362 be64_to_cpu(header
.incompatible_features
);
1363 header
.compatible_features
= be64_to_cpu(header
.compatible_features
);
1364 header
.autoclear_features
= be64_to_cpu(header
.autoclear_features
);
1365 header
.refcount_order
= be32_to_cpu(header
.refcount_order
);
1366 header
.header_length
= be32_to_cpu(header
.header_length
);
1368 if (header
.header_length
< 104) {
1369 error_setg(errp
, "qcow2 header too short");
1375 if (header
.header_length
> s
->cluster_size
) {
1376 error_setg(errp
, "qcow2 header exceeds cluster size");
1381 if (header
.header_length
> sizeof(header
)) {
1382 s
->unknown_header_fields_size
= header
.header_length
- sizeof(header
);
1383 s
->unknown_header_fields
= g_malloc(s
->unknown_header_fields_size
);
1384 ret
= bdrv_pread(bs
->file
, sizeof(header
),
1385 s
->unknown_header_fields_size
,
1386 s
->unknown_header_fields
, 0);
1388 error_setg_errno(errp
, -ret
, "Could not read unknown qcow2 header "
1394 if (header
.backing_file_offset
> s
->cluster_size
) {
1395 error_setg(errp
, "Invalid backing file offset");
1400 if (header
.backing_file_offset
) {
1401 ext_end
= header
.backing_file_offset
;
1403 ext_end
= 1 << header
.cluster_bits
;
1406 /* Handle feature bits */
1407 s
->incompatible_features
= header
.incompatible_features
;
1408 s
->compatible_features
= header
.compatible_features
;
1409 s
->autoclear_features
= header
.autoclear_features
;
1412 * Handle compression type
1413 * Older qcow2 images don't contain the compression type header.
1414 * Distinguish them by the header length and use
1415 * the only valid (default) compression type in that case
1417 if (header
.header_length
> offsetof(QCowHeader
, compression_type
)) {
1418 s
->compression_type
= header
.compression_type
;
1420 s
->compression_type
= QCOW2_COMPRESSION_TYPE_ZLIB
;
1423 ret
= validate_compression_type(s
, errp
);
1428 if (s
->incompatible_features
& ~QCOW2_INCOMPAT_MASK
) {
1429 void *feature_table
= NULL
;
1430 qcow2_read_extensions(bs
, header
.header_length
, ext_end
,
1431 &feature_table
, flags
, NULL
, NULL
);
1432 report_unsupported_feature(errp
, feature_table
,
1433 s
->incompatible_features
&
1434 ~QCOW2_INCOMPAT_MASK
);
1436 g_free(feature_table
);
1440 if (s
->incompatible_features
& QCOW2_INCOMPAT_CORRUPT
) {
1441 /* Corrupt images may not be written to unless they are being repaired
1443 if ((flags
& BDRV_O_RDWR
) && !(flags
& BDRV_O_CHECK
)) {
1444 error_setg(errp
, "qcow2: Image is corrupt; cannot be opened "
1451 s
->subclusters_per_cluster
=
1452 has_subclusters(s
) ? QCOW_EXTL2_SUBCLUSTERS_PER_CLUSTER
: 1;
1453 s
->subcluster_size
= s
->cluster_size
/ s
->subclusters_per_cluster
;
1454 s
->subcluster_bits
= ctz32(s
->subcluster_size
);
1456 if (s
->subcluster_size
< (1 << MIN_CLUSTER_BITS
)) {
1457 error_setg(errp
, "Unsupported subcluster size: %d", s
->subcluster_size
);
1462 /* Check support for various header values */
1463 if (header
.refcount_order
> 6) {
1464 error_setg(errp
, "Reference count entry width too large; may not "
1469 s
->refcount_order
= header
.refcount_order
;
1470 s
->refcount_bits
= 1 << s
->refcount_order
;
1471 s
->refcount_max
= UINT64_C(1) << (s
->refcount_bits
- 1);
1472 s
->refcount_max
+= s
->refcount_max
- 1;
1474 s
->crypt_method_header
= header
.crypt_method
;
1475 if (s
->crypt_method_header
) {
1476 if (bdrv_uses_whitelist() &&
1477 s
->crypt_method_header
== QCOW_CRYPT_AES
) {
1479 "Use of AES-CBC encrypted qcow2 images is no longer "
1480 "supported in system emulators");
1481 error_append_hint(errp
,
1482 "You can use 'qemu-img convert' to convert your "
1483 "image to an alternative supported format, such "
1484 "as unencrypted qcow2, or raw with the LUKS "
1485 "format instead.\n");
1490 if (s
->crypt_method_header
== QCOW_CRYPT_AES
) {
1491 s
->crypt_physical_offset
= false;
1493 /* Assuming LUKS and any future crypt methods we
1494 * add will all use physical offsets, due to the
1495 * fact that the alternative is insecure... */
1496 s
->crypt_physical_offset
= true;
1499 bs
->encrypted
= true;
1502 s
->l2_bits
= s
->cluster_bits
- ctz32(l2_entry_size(s
));
1503 s
->l2_size
= 1 << s
->l2_bits
;
1504 /* 2^(s->refcount_order - 3) is the refcount width in bytes */
1505 s
->refcount_block_bits
= s
->cluster_bits
- (s
->refcount_order
- 3);
1506 s
->refcount_block_size
= 1 << s
->refcount_block_bits
;
1507 bs
->total_sectors
= header
.size
/ BDRV_SECTOR_SIZE
;
1508 s
->csize_shift
= (62 - (s
->cluster_bits
- 8));
1509 s
->csize_mask
= (1 << (s
->cluster_bits
- 8)) - 1;
1510 s
->cluster_offset_mask
= (1LL << s
->csize_shift
) - 1;
1512 s
->refcount_table_offset
= header
.refcount_table_offset
;
1513 s
->refcount_table_size
=
1514 header
.refcount_table_clusters
<< (s
->cluster_bits
- 3);
1516 if (header
.refcount_table_clusters
== 0 && !(flags
& BDRV_O_CHECK
)) {
1517 error_setg(errp
, "Image does not contain a reference count table");
1522 ret
= qcow2_validate_table(bs
, s
->refcount_table_offset
,
1523 header
.refcount_table_clusters
,
1524 s
->cluster_size
, QCOW_MAX_REFTABLE_SIZE
,
1525 "Reference count table", errp
);
1530 if (!(flags
& BDRV_O_CHECK
)) {
1532 * The total size in bytes of the snapshot table is checked in
1533 * qcow2_read_snapshots() because the size of each snapshot is
1534 * variable and we don't know it yet.
1535 * Here we only check the offset and number of snapshots.
1537 ret
= qcow2_validate_table(bs
, header
.snapshots_offset
,
1538 header
.nb_snapshots
,
1539 sizeof(QCowSnapshotHeader
),
1540 sizeof(QCowSnapshotHeader
) *
1542 "Snapshot table", errp
);
1548 /* read the level 1 table */
1549 ret
= qcow2_validate_table(bs
, header
.l1_table_offset
,
1550 header
.l1_size
, L1E_SIZE
,
1551 QCOW_MAX_L1_SIZE
, "Active L1 table", errp
);
1555 s
->l1_size
= header
.l1_size
;
1556 s
->l1_table_offset
= header
.l1_table_offset
;
1558 l1_vm_state_index
= size_to_l1(s
, header
.size
);
1559 if (l1_vm_state_index
> INT_MAX
) {
1560 error_setg(errp
, "Image is too big");
1564 s
->l1_vm_state_index
= l1_vm_state_index
;
1566 /* the L1 table must contain at least enough entries to put
1567 header.size bytes */
1568 if (s
->l1_size
< s
->l1_vm_state_index
) {
1569 error_setg(errp
, "L1 table is too small");
1574 if (s
->l1_size
> 0) {
1575 s
->l1_table
= qemu_try_blockalign(bs
->file
->bs
, s
->l1_size
* L1E_SIZE
);
1576 if (s
->l1_table
== NULL
) {
1577 error_setg(errp
, "Could not allocate L1 table");
1581 ret
= bdrv_pread(bs
->file
, s
->l1_table_offset
, s
->l1_size
* L1E_SIZE
,
1584 error_setg_errno(errp
, -ret
, "Could not read L1 table");
1587 for(i
= 0;i
< s
->l1_size
; i
++) {
1588 s
->l1_table
[i
] = be64_to_cpu(s
->l1_table
[i
]);
1592 /* Parse driver-specific options */
1593 ret
= qcow2_update_options(bs
, options
, flags
, errp
);
1600 ret
= qcow2_refcount_init(bs
);
1602 error_setg_errno(errp
, -ret
, "Could not initialize refcount handling");
1606 QLIST_INIT(&s
->cluster_allocs
);
1607 QTAILQ_INIT(&s
->discards
);
1609 /* read qcow2 extensions */
1610 if (qcow2_read_extensions(bs
, header
.header_length
, ext_end
, NULL
,
1611 flags
, &update_header
, errp
)) {
1616 if (open_data_file
) {
1617 /* Open external data file */
1618 s
->data_file
= bdrv_open_child(NULL
, options
, "data-file", bs
,
1619 &child_of_bds
, BDRV_CHILD_DATA
,
1626 if (s
->incompatible_features
& QCOW2_INCOMPAT_DATA_FILE
) {
1627 if (!s
->data_file
&& s
->image_data_file
) {
1628 s
->data_file
= bdrv_open_child(s
->image_data_file
, options
,
1629 "data-file", bs
, &child_of_bds
,
1630 BDRV_CHILD_DATA
, false, errp
);
1631 if (!s
->data_file
) {
1636 if (!s
->data_file
) {
1637 error_setg(errp
, "'data-file' is required for this image");
1643 bs
->file
->role
&= ~BDRV_CHILD_DATA
;
1645 /* Must succeed because we have given up permissions if anything */
1646 bdrv_child_refresh_perms(bs
, bs
->file
, &error_abort
);
1649 error_setg(errp
, "'data-file' can only be set for images with "
1650 "an external data file");
1655 s
->data_file
= bs
->file
;
1657 if (data_file_is_raw(bs
)) {
1658 error_setg(errp
, "data-file-raw requires a data file");
1665 /* qcow2_read_extension may have set up the crypto context
1666 * if the crypt method needs a header region, some methods
1667 * don't need header extensions, so must check here
1669 if (s
->crypt_method_header
&& !s
->crypto
) {
1670 if (s
->crypt_method_header
== QCOW_CRYPT_AES
) {
1671 unsigned int cflags
= 0;
1672 if (flags
& BDRV_O_NO_IO
) {
1673 cflags
|= QCRYPTO_BLOCK_OPEN_NO_IO
;
1675 s
->crypto
= qcrypto_block_open(s
->crypto_opts
, "encrypt.",
1677 QCOW2_MAX_THREADS
, errp
);
1682 } else if (!(flags
& BDRV_O_NO_IO
)) {
1683 error_setg(errp
, "Missing CRYPTO header for crypt method %d",
1684 s
->crypt_method_header
);
1690 /* read the backing file name */
1691 if (header
.backing_file_offset
!= 0) {
1692 len
= header
.backing_file_size
;
1693 if (len
> MIN(1023, s
->cluster_size
- header
.backing_file_offset
) ||
1694 len
>= sizeof(bs
->backing_file
)) {
1695 error_setg(errp
, "Backing file name too long");
1699 ret
= bdrv_pread(bs
->file
, header
.backing_file_offset
, len
,
1700 bs
->auto_backing_file
, 0);
1702 error_setg_errno(errp
, -ret
, "Could not read backing file name");
1705 bs
->auto_backing_file
[len
] = '\0';
1706 pstrcpy(bs
->backing_file
, sizeof(bs
->backing_file
),
1707 bs
->auto_backing_file
);
1708 s
->image_backing_file
= g_strdup(bs
->auto_backing_file
);
1712 * Internal snapshots; skip reading them in check mode, because
1713 * we do not need them then, and we do not want to abort because
1714 * of a broken table.
1716 if (!(flags
& BDRV_O_CHECK
)) {
1717 s
->snapshots_offset
= header
.snapshots_offset
;
1718 s
->nb_snapshots
= header
.nb_snapshots
;
1720 ret
= qcow2_read_snapshots(bs
, errp
);
1726 /* Clear unknown autoclear feature bits */
1727 update_header
|= s
->autoclear_features
& ~QCOW2_AUTOCLEAR_MASK
;
1728 update_header
= update_header
&& bdrv_is_writable(bs
);
1729 if (update_header
) {
1730 s
->autoclear_features
&= QCOW2_AUTOCLEAR_MASK
;
1733 /* == Handle persistent dirty bitmaps ==
1735 * We want load dirty bitmaps in three cases:
1737 * 1. Normal open of the disk in active mode, not related to invalidation
1740 * 2. Invalidation of the target vm after pre-copy phase of migration, if
1741 * bitmaps are _not_ migrating through migration channel, i.e.
1742 * 'dirty-bitmaps' capability is disabled.
1744 * 3. Invalidation of source vm after failed or canceled migration.
1745 * This is a very interesting case. There are two possible types of
1748 * A. Stored on inactivation and removed. They should be loaded from the
1751 * B. Not stored: not-persistent bitmaps and bitmaps, migrated through
1752 * the migration channel (with dirty-bitmaps capability).
1754 * On the other hand, there are two possible sub-cases:
1756 * 3.1 disk was changed by somebody else while were inactive. In this
1757 * case all in-RAM dirty bitmaps (both persistent and not) are
1758 * definitely invalid. And we don't have any method to determine
1761 * Simple and safe thing is to just drop all the bitmaps of type B on
1762 * inactivation. But in this case we lose bitmaps in valid 4.2 case.
1764 * On the other hand, resuming source vm, if disk was already changed
1765 * is a bad thing anyway: not only bitmaps, the whole vm state is
1766 * out of sync with disk.
1768 * This means, that user or management tool, who for some reason
1769 * decided to resume source vm, after disk was already changed by
1770 * target vm, should at least drop all dirty bitmaps by hand.
1772 * So, we can ignore this case for now, but TODO: "generation"
1773 * extension for qcow2, to determine, that image was changed after
1774 * last inactivation. And if it is changed, we will drop (or at least
1775 * mark as 'invalid' all the bitmaps of type B, both persistent
1778 * 3.2 disk was _not_ changed while were inactive. Bitmaps may be saved
1779 * to disk ('dirty-bitmaps' capability disabled), or not saved
1780 * ('dirty-bitmaps' capability enabled), but we don't need to care
1781 * of: let's load bitmaps as always: stored bitmaps will be loaded,
1782 * and not stored has flag IN_USE=1 in the image and will be skipped
1785 * One remaining possible case when we don't want load bitmaps:
1787 * 4. Open disk in inactive mode in target vm (bitmaps are migrating or
1788 * will be loaded on invalidation, no needs try loading them before)
1791 if (!(bdrv_get_flags(bs
) & BDRV_O_INACTIVE
)) {
1792 /* It's case 1, 2 or 3.2. Or 3.1 which is BUG in management layer. */
1793 bool header_updated
;
1794 if (!qcow2_load_dirty_bitmaps(bs
, &header_updated
, errp
)) {
1799 update_header
= update_header
&& !header_updated
;
1802 if (update_header
) {
1803 ret
= qcow2_update_header(bs
);
1805 error_setg_errno(errp
, -ret
, "Could not update qcow2 header");
1810 bs
->supported_zero_flags
= header
.version
>= 3 ?
1811 BDRV_REQ_MAY_UNMAP
| BDRV_REQ_NO_FALLBACK
: 0;
1812 bs
->supported_truncate_flags
= BDRV_REQ_ZERO_WRITE
;
1814 /* Repair image if dirty */
1815 if (!(flags
& BDRV_O_CHECK
) && bdrv_is_writable(bs
) &&
1816 (s
->incompatible_features
& QCOW2_INCOMPAT_DIRTY
)) {
1817 BdrvCheckResult result
= {0};
1819 ret
= qcow2_co_check_locked(bs
, &result
,
1820 BDRV_FIX_ERRORS
| BDRV_FIX_LEAKS
);
1821 if (ret
< 0 || result
.check_errors
) {
1825 error_setg_errno(errp
, -ret
, "Could not repair dirty image");
1832 BdrvCheckResult result
= {0};
1833 qcow2_check_refcounts(bs
, &result
, 0);
1837 qemu_co_queue_init(&s
->thread_task_queue
);
1842 g_free(s
->image_data_file
);
1843 if (open_data_file
&& has_data_file(bs
)) {
1844 bdrv_unref_child(bs
, s
->data_file
);
1845 s
->data_file
= NULL
;
1847 g_free(s
->unknown_header_fields
);
1848 cleanup_unknown_header_ext(bs
);
1849 qcow2_free_snapshots(bs
);
1850 qcow2_refcount_close(bs
);
1851 qemu_vfree(s
->l1_table
);
1852 /* else pre-write overlap checks in cache_destroy may crash */
1854 cache_clean_timer_del(bs
);
1855 if (s
->l2_table_cache
) {
1856 qcow2_cache_destroy(s
->l2_table_cache
);
1858 if (s
->refcount_block_cache
) {
1859 qcow2_cache_destroy(s
->refcount_block_cache
);
1861 qcrypto_block_free(s
->crypto
);
1862 qapi_free_QCryptoBlockOpenOptions(s
->crypto_opts
);
1866 typedef struct QCow2OpenCo
{
1867 BlockDriverState
*bs
;
1874 static void coroutine_fn
qcow2_open_entry(void *opaque
)
1876 QCow2OpenCo
*qoc
= opaque
;
1877 BDRVQcow2State
*s
= qoc
->bs
->opaque
;
1879 qemu_co_mutex_lock(&s
->lock
);
1880 qoc
->ret
= qcow2_do_open(qoc
->bs
, qoc
->options
, qoc
->flags
, true,
1882 qemu_co_mutex_unlock(&s
->lock
);
1885 static int qcow2_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
1888 BDRVQcow2State
*s
= bs
->opaque
;
1897 bs
->file
= bdrv_open_child(NULL
, options
, "file", bs
, &child_of_bds
,
1898 BDRV_CHILD_IMAGE
, false, errp
);
1903 /* Initialise locks */
1904 qemu_co_mutex_init(&s
->lock
);
1906 if (qemu_in_coroutine()) {
1907 /* From bdrv_co_create. */
1908 qcow2_open_entry(&qoc
);
1910 assert(qemu_get_current_aio_context() == qemu_get_aio_context());
1911 qemu_coroutine_enter(qemu_coroutine_create(qcow2_open_entry
, &qoc
));
1912 BDRV_POLL_WHILE(bs
, qoc
.ret
== -EINPROGRESS
);
1917 static void qcow2_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
1919 BDRVQcow2State
*s
= bs
->opaque
;
1921 if (bs
->encrypted
) {
1922 /* Encryption works on a sector granularity */
1923 bs
->bl
.request_alignment
= qcrypto_block_get_sector_size(s
->crypto
);
1925 bs
->bl
.pwrite_zeroes_alignment
= s
->subcluster_size
;
1926 bs
->bl
.pdiscard_alignment
= s
->cluster_size
;
1929 static int qcow2_reopen_prepare(BDRVReopenState
*state
,
1930 BlockReopenQueue
*queue
, Error
**errp
)
1932 BDRVQcow2State
*s
= state
->bs
->opaque
;
1933 Qcow2ReopenState
*r
;
1936 r
= g_new0(Qcow2ReopenState
, 1);
1939 ret
= qcow2_update_options_prepare(state
->bs
, r
, state
->options
,
1940 state
->flags
, errp
);
1945 /* We need to write out any unwritten data if we reopen read-only. */
1946 if ((state
->flags
& BDRV_O_RDWR
) == 0) {
1947 ret
= qcow2_reopen_bitmaps_ro(state
->bs
, errp
);
1952 ret
= bdrv_flush(state
->bs
);
1957 ret
= qcow2_mark_clean(state
->bs
);
1964 * Without an external data file, s->data_file points to the same BdrvChild
1965 * as bs->file. It needs to be resynced after reopen because bs->file may
1966 * be changed. We can't use it in the meantime.
1968 if (!has_data_file(state
->bs
)) {
1969 assert(s
->data_file
== state
->bs
->file
);
1970 s
->data_file
= NULL
;
1976 qcow2_update_options_abort(state
->bs
, r
);
1981 static void qcow2_reopen_commit(BDRVReopenState
*state
)
1983 BDRVQcow2State
*s
= state
->bs
->opaque
;
1985 qcow2_update_options_commit(state
->bs
, state
->opaque
);
1986 if (!s
->data_file
) {
1988 * If we don't have an external data file, s->data_file was cleared by
1989 * qcow2_reopen_prepare() and needs to be updated.
1991 s
->data_file
= state
->bs
->file
;
1993 g_free(state
->opaque
);
1996 static void qcow2_reopen_commit_post(BDRVReopenState
*state
)
1998 if (state
->flags
& BDRV_O_RDWR
) {
1999 Error
*local_err
= NULL
;
2001 if (qcow2_reopen_bitmaps_rw(state
->bs
, &local_err
) < 0) {
2003 * This is not fatal, bitmaps just left read-only, so all following
2004 * writes will fail. User can remove read-only bitmaps to unblock
2005 * writes or retry reopen.
2007 error_reportf_err(local_err
,
2008 "%s: Failed to make dirty bitmaps writable: ",
2009 bdrv_get_node_name(state
->bs
));
2014 static void qcow2_reopen_abort(BDRVReopenState
*state
)
2016 BDRVQcow2State
*s
= state
->bs
->opaque
;
2018 if (!s
->data_file
) {
2020 * If we don't have an external data file, s->data_file was cleared by
2021 * qcow2_reopen_prepare() and needs to be restored.
2023 s
->data_file
= state
->bs
->file
;
2025 qcow2_update_options_abort(state
->bs
, state
->opaque
);
2026 g_free(state
->opaque
);
2029 static void qcow2_join_options(QDict
*options
, QDict
*old_options
)
2031 bool has_new_overlap_template
=
2032 qdict_haskey(options
, QCOW2_OPT_OVERLAP
) ||
2033 qdict_haskey(options
, QCOW2_OPT_OVERLAP_TEMPLATE
);
2034 bool has_new_total_cache_size
=
2035 qdict_haskey(options
, QCOW2_OPT_CACHE_SIZE
);
2036 bool has_all_cache_options
;
2038 /* New overlap template overrides all old overlap options */
2039 if (has_new_overlap_template
) {
2040 qdict_del(old_options
, QCOW2_OPT_OVERLAP
);
2041 qdict_del(old_options
, QCOW2_OPT_OVERLAP_TEMPLATE
);
2042 qdict_del(old_options
, QCOW2_OPT_OVERLAP_MAIN_HEADER
);
2043 qdict_del(old_options
, QCOW2_OPT_OVERLAP_ACTIVE_L1
);
2044 qdict_del(old_options
, QCOW2_OPT_OVERLAP_ACTIVE_L2
);
2045 qdict_del(old_options
, QCOW2_OPT_OVERLAP_REFCOUNT_TABLE
);
2046 qdict_del(old_options
, QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK
);
2047 qdict_del(old_options
, QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE
);
2048 qdict_del(old_options
, QCOW2_OPT_OVERLAP_INACTIVE_L1
);
2049 qdict_del(old_options
, QCOW2_OPT_OVERLAP_INACTIVE_L2
);
2052 /* New total cache size overrides all old options */
2053 if (qdict_haskey(options
, QCOW2_OPT_CACHE_SIZE
)) {
2054 qdict_del(old_options
, QCOW2_OPT_L2_CACHE_SIZE
);
2055 qdict_del(old_options
, QCOW2_OPT_REFCOUNT_CACHE_SIZE
);
2058 qdict_join(options
, old_options
, false);
2061 * If after merging all cache size options are set, an old total size is
2062 * overwritten. Do keep all options, however, if all three are new. The
2063 * resulting error message is what we want to happen.
2065 has_all_cache_options
=
2066 qdict_haskey(options
, QCOW2_OPT_CACHE_SIZE
) ||
2067 qdict_haskey(options
, QCOW2_OPT_L2_CACHE_SIZE
) ||
2068 qdict_haskey(options
, QCOW2_OPT_REFCOUNT_CACHE_SIZE
);
2070 if (has_all_cache_options
&& !has_new_total_cache_size
) {
2071 qdict_del(options
, QCOW2_OPT_CACHE_SIZE
);
2075 static int coroutine_fn
qcow2_co_block_status(BlockDriverState
*bs
,
2077 int64_t offset
, int64_t count
,
2078 int64_t *pnum
, int64_t *map
,
2079 BlockDriverState
**file
)
2081 BDRVQcow2State
*s
= bs
->opaque
;
2082 uint64_t host_offset
;
2084 QCow2SubclusterType type
;
2085 int ret
, status
= 0;
2087 qemu_co_mutex_lock(&s
->lock
);
2089 if (!s
->metadata_preallocation_checked
) {
2090 ret
= qcow2_detect_metadata_preallocation(bs
);
2091 s
->metadata_preallocation
= (ret
== 1);
2092 s
->metadata_preallocation_checked
= true;
2095 bytes
= MIN(INT_MAX
, count
);
2096 ret
= qcow2_get_host_offset(bs
, offset
, &bytes
, &host_offset
, &type
);
2097 qemu_co_mutex_unlock(&s
->lock
);
2104 if ((type
== QCOW2_SUBCLUSTER_NORMAL
||
2105 type
== QCOW2_SUBCLUSTER_ZERO_ALLOC
||
2106 type
== QCOW2_SUBCLUSTER_UNALLOCATED_ALLOC
) && !s
->crypto
) {
2108 *file
= s
->data_file
->bs
;
2109 status
|= BDRV_BLOCK_OFFSET_VALID
;
2111 if (type
== QCOW2_SUBCLUSTER_ZERO_PLAIN
||
2112 type
== QCOW2_SUBCLUSTER_ZERO_ALLOC
) {
2113 status
|= BDRV_BLOCK_ZERO
;
2114 } else if (type
!= QCOW2_SUBCLUSTER_UNALLOCATED_PLAIN
&&
2115 type
!= QCOW2_SUBCLUSTER_UNALLOCATED_ALLOC
) {
2116 status
|= BDRV_BLOCK_DATA
;
2118 if (s
->metadata_preallocation
&& (status
& BDRV_BLOCK_DATA
) &&
2119 (status
& BDRV_BLOCK_OFFSET_VALID
))
2121 status
|= BDRV_BLOCK_RECURSE
;
2126 static coroutine_fn
int qcow2_handle_l2meta(BlockDriverState
*bs
,
2127 QCowL2Meta
**pl2meta
,
2131 QCowL2Meta
*l2meta
= *pl2meta
;
2133 while (l2meta
!= NULL
) {
2137 ret
= qcow2_alloc_cluster_link_l2(bs
, l2meta
);
2142 qcow2_alloc_cluster_abort(bs
, l2meta
);
2145 /* Take the request off the list of running requests */
2146 QLIST_REMOVE(l2meta
, next_in_flight
);
2148 qemu_co_queue_restart_all(&l2meta
->dependent_requests
);
2150 next
= l2meta
->next
;
2159 static coroutine_fn
int
2160 qcow2_co_preadv_encrypted(BlockDriverState
*bs
,
2161 uint64_t host_offset
,
2165 uint64_t qiov_offset
)
2168 BDRVQcow2State
*s
= bs
->opaque
;
2171 assert(bs
->encrypted
&& s
->crypto
);
2172 assert(bytes
<= QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_size
);
2175 * For encrypted images, read everything into a temporary
2176 * contiguous buffer on which the AES functions can work.
2177 * Also, decryption in a separate buffer is better as it
2178 * prevents the guest from learning information about the
2179 * encrypted nature of the virtual disk.
2182 buf
= qemu_try_blockalign(s
->data_file
->bs
, bytes
);
2187 BLKDBG_EVENT(bs
->file
, BLKDBG_READ_AIO
);
2188 ret
= bdrv_co_pread(s
->data_file
, host_offset
, bytes
, buf
, 0);
2193 if (qcow2_co_decrypt(bs
, host_offset
, offset
, buf
, bytes
) < 0)
2198 qemu_iovec_from_buf(qiov
, qiov_offset
, buf
, bytes
);
2206 typedef struct Qcow2AioTask
{
2209 BlockDriverState
*bs
;
2210 QCow2SubclusterType subcluster_type
; /* only for read */
2211 uint64_t host_offset
; /* or l2_entry for compressed read */
2215 uint64_t qiov_offset
;
2216 QCowL2Meta
*l2meta
; /* only for write */
2219 static coroutine_fn
int qcow2_co_preadv_task_entry(AioTask
*task
);
2220 static coroutine_fn
int qcow2_add_task(BlockDriverState
*bs
,
2223 QCow2SubclusterType subcluster_type
,
2224 uint64_t host_offset
,
2231 Qcow2AioTask local_task
;
2232 Qcow2AioTask
*task
= pool
? g_new(Qcow2AioTask
, 1) : &local_task
;
2234 *task
= (Qcow2AioTask
) {
2237 .subcluster_type
= subcluster_type
,
2239 .host_offset
= host_offset
,
2242 .qiov_offset
= qiov_offset
,
2246 trace_qcow2_add_task(qemu_coroutine_self(), bs
, pool
,
2247 func
== qcow2_co_preadv_task_entry
? "read" : "write",
2248 subcluster_type
, host_offset
, offset
, bytes
,
2252 return func(&task
->task
);
2255 aio_task_pool_start_task(pool
, &task
->task
);
2260 static coroutine_fn
int qcow2_co_preadv_task(BlockDriverState
*bs
,
2261 QCow2SubclusterType subc_type
,
2262 uint64_t host_offset
,
2263 uint64_t offset
, uint64_t bytes
,
2267 BDRVQcow2State
*s
= bs
->opaque
;
2269 switch (subc_type
) {
2270 case QCOW2_SUBCLUSTER_ZERO_PLAIN
:
2271 case QCOW2_SUBCLUSTER_ZERO_ALLOC
:
2272 /* Both zero types are handled in qcow2_co_preadv_part */
2273 g_assert_not_reached();
2275 case QCOW2_SUBCLUSTER_UNALLOCATED_PLAIN
:
2276 case QCOW2_SUBCLUSTER_UNALLOCATED_ALLOC
:
2277 assert(bs
->backing
); /* otherwise handled in qcow2_co_preadv_part */
2279 BLKDBG_EVENT(bs
->file
, BLKDBG_READ_BACKING_AIO
);
2280 return bdrv_co_preadv_part(bs
->backing
, offset
, bytes
,
2281 qiov
, qiov_offset
, 0);
2283 case QCOW2_SUBCLUSTER_COMPRESSED
:
2284 return qcow2_co_preadv_compressed(bs
, host_offset
,
2285 offset
, bytes
, qiov
, qiov_offset
);
2287 case QCOW2_SUBCLUSTER_NORMAL
:
2288 if (bs
->encrypted
) {
2289 return qcow2_co_preadv_encrypted(bs
, host_offset
,
2290 offset
, bytes
, qiov
, qiov_offset
);
2293 BLKDBG_EVENT(bs
->file
, BLKDBG_READ_AIO
);
2294 return bdrv_co_preadv_part(s
->data_file
, host_offset
,
2295 bytes
, qiov
, qiov_offset
, 0);
2298 g_assert_not_reached();
2301 g_assert_not_reached();
2304 static coroutine_fn
int qcow2_co_preadv_task_entry(AioTask
*task
)
2306 Qcow2AioTask
*t
= container_of(task
, Qcow2AioTask
, task
);
2310 return qcow2_co_preadv_task(t
->bs
, t
->subcluster_type
,
2311 t
->host_offset
, t
->offset
, t
->bytes
,
2312 t
->qiov
, t
->qiov_offset
);
2315 static coroutine_fn
int qcow2_co_preadv_part(BlockDriverState
*bs
,
2316 int64_t offset
, int64_t bytes
,
2319 BdrvRequestFlags flags
)
2321 BDRVQcow2State
*s
= bs
->opaque
;
2323 unsigned int cur_bytes
; /* number of bytes in current iteration */
2324 uint64_t host_offset
= 0;
2325 QCow2SubclusterType type
;
2326 AioTaskPool
*aio
= NULL
;
2328 while (bytes
!= 0 && aio_task_pool_status(aio
) == 0) {
2329 /* prepare next request */
2330 cur_bytes
= MIN(bytes
, INT_MAX
);
2332 cur_bytes
= MIN(cur_bytes
,
2333 QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_size
);
2336 qemu_co_mutex_lock(&s
->lock
);
2337 ret
= qcow2_get_host_offset(bs
, offset
, &cur_bytes
,
2338 &host_offset
, &type
);
2339 qemu_co_mutex_unlock(&s
->lock
);
2344 if (type
== QCOW2_SUBCLUSTER_ZERO_PLAIN
||
2345 type
== QCOW2_SUBCLUSTER_ZERO_ALLOC
||
2346 (type
== QCOW2_SUBCLUSTER_UNALLOCATED_PLAIN
&& !bs
->backing
) ||
2347 (type
== QCOW2_SUBCLUSTER_UNALLOCATED_ALLOC
&& !bs
->backing
))
2349 qemu_iovec_memset(qiov
, qiov_offset
, 0, cur_bytes
);
2351 if (!aio
&& cur_bytes
!= bytes
) {
2352 aio
= aio_task_pool_new(QCOW2_MAX_WORKERS
);
2354 ret
= qcow2_add_task(bs
, aio
, qcow2_co_preadv_task_entry
, type
,
2355 host_offset
, offset
, cur_bytes
,
2356 qiov
, qiov_offset
, NULL
);
2363 offset
+= cur_bytes
;
2364 qiov_offset
+= cur_bytes
;
2369 aio_task_pool_wait_all(aio
);
2371 ret
= aio_task_pool_status(aio
);
2379 /* Check if it's possible to merge a write request with the writing of
2380 * the data from the COW regions */
2381 static bool merge_cow(uint64_t offset
, unsigned bytes
,
2382 QEMUIOVector
*qiov
, size_t qiov_offset
,
2387 for (m
= l2meta
; m
!= NULL
; m
= m
->next
) {
2388 /* If both COW regions are empty then there's nothing to merge */
2389 if (m
->cow_start
.nb_bytes
== 0 && m
->cow_end
.nb_bytes
== 0) {
2393 /* If COW regions are handled already, skip this too */
2399 * The write request should start immediately after the first
2400 * COW region. This does not always happen because the area
2401 * touched by the request can be larger than the one defined
2402 * by @m (a single request can span an area consisting of a
2403 * mix of previously unallocated and allocated clusters, that
2404 * is why @l2meta is a list).
2406 if (l2meta_cow_start(m
) + m
->cow_start
.nb_bytes
!= offset
) {
2407 /* In this case the request starts before this region */
2408 assert(offset
< l2meta_cow_start(m
));
2409 assert(m
->cow_start
.nb_bytes
== 0);
2413 /* The write request should end immediately before the second
2414 * COW region (see above for why it does not always happen) */
2415 if (m
->offset
+ m
->cow_end
.offset
!= offset
+ bytes
) {
2416 assert(offset
+ bytes
> m
->offset
+ m
->cow_end
.offset
);
2417 assert(m
->cow_end
.nb_bytes
== 0);
2421 /* Make sure that adding both COW regions to the QEMUIOVector
2422 * does not exceed IOV_MAX */
2423 if (qemu_iovec_subvec_niov(qiov
, qiov_offset
, bytes
) > IOV_MAX
- 2) {
2427 m
->data_qiov
= qiov
;
2428 m
->data_qiov_offset
= qiov_offset
;
2436 * Return 1 if the COW regions read as zeroes, 0 if not, < 0 on error.
2437 * Note that returning 0 does not guarantee non-zero data.
2439 static int is_zero_cow(BlockDriverState
*bs
, QCowL2Meta
*m
)
2442 * This check is designed for optimization shortcut so it must be
2444 * Instead of is_zero(), use bdrv_co_is_zero_fast() as it is
2445 * faster (but not as accurate and can result in false negatives).
2447 int ret
= bdrv_co_is_zero_fast(bs
, m
->offset
+ m
->cow_start
.offset
,
2448 m
->cow_start
.nb_bytes
);
2453 return bdrv_co_is_zero_fast(bs
, m
->offset
+ m
->cow_end
.offset
,
2454 m
->cow_end
.nb_bytes
);
2457 static int handle_alloc_space(BlockDriverState
*bs
, QCowL2Meta
*l2meta
)
2459 BDRVQcow2State
*s
= bs
->opaque
;
2462 if (!(s
->data_file
->bs
->supported_zero_flags
& BDRV_REQ_NO_FALLBACK
)) {
2466 if (bs
->encrypted
) {
2470 for (m
= l2meta
; m
!= NULL
; m
= m
->next
) {
2472 uint64_t start_offset
= m
->alloc_offset
+ m
->cow_start
.offset
;
2473 unsigned nb_bytes
= m
->cow_end
.offset
+ m
->cow_end
.nb_bytes
-
2474 m
->cow_start
.offset
;
2476 if (!m
->cow_start
.nb_bytes
&& !m
->cow_end
.nb_bytes
) {
2480 ret
= is_zero_cow(bs
, m
);
2483 } else if (ret
== 0) {
2488 * instead of writing zero COW buffers,
2489 * efficiently zero out the whole clusters
2492 ret
= qcow2_pre_write_overlap_check(bs
, 0, start_offset
, nb_bytes
,
2498 BLKDBG_EVENT(bs
->file
, BLKDBG_CLUSTER_ALLOC_SPACE
);
2499 ret
= bdrv_co_pwrite_zeroes(s
->data_file
, start_offset
, nb_bytes
,
2500 BDRV_REQ_NO_FALLBACK
);
2502 if (ret
!= -ENOTSUP
&& ret
!= -EAGAIN
) {
2508 trace_qcow2_skip_cow(qemu_coroutine_self(), m
->offset
, m
->nb_clusters
);
2515 * qcow2_co_pwritev_task
2516 * Called with s->lock unlocked
2517 * l2meta - if not NULL, qcow2_co_pwritev_task() will consume it. Caller must
2518 * not use it somehow after qcow2_co_pwritev_task() call
2520 static coroutine_fn
int qcow2_co_pwritev_task(BlockDriverState
*bs
,
2521 uint64_t host_offset
,
2522 uint64_t offset
, uint64_t bytes
,
2524 uint64_t qiov_offset
,
2528 BDRVQcow2State
*s
= bs
->opaque
;
2529 void *crypt_buf
= NULL
;
2530 QEMUIOVector encrypted_qiov
;
2532 if (bs
->encrypted
) {
2534 assert(bytes
<= QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_size
);
2535 crypt_buf
= qemu_try_blockalign(bs
->file
->bs
, bytes
);
2536 if (crypt_buf
== NULL
) {
2540 qemu_iovec_to_buf(qiov
, qiov_offset
, crypt_buf
, bytes
);
2542 if (qcow2_co_encrypt(bs
, host_offset
, offset
, crypt_buf
, bytes
) < 0) {
2547 qemu_iovec_init_buf(&encrypted_qiov
, crypt_buf
, bytes
);
2548 qiov
= &encrypted_qiov
;
2552 /* Try to efficiently initialize the physical space with zeroes */
2553 ret
= handle_alloc_space(bs
, l2meta
);
2559 * If we need to do COW, check if it's possible to merge the
2560 * writing of the guest data together with that of the COW regions.
2561 * If it's not possible (or not necessary) then write the
2564 if (!merge_cow(offset
, bytes
, qiov
, qiov_offset
, l2meta
)) {
2565 BLKDBG_EVENT(bs
->file
, BLKDBG_WRITE_AIO
);
2566 trace_qcow2_writev_data(qemu_coroutine_self(), host_offset
);
2567 ret
= bdrv_co_pwritev_part(s
->data_file
, host_offset
,
2568 bytes
, qiov
, qiov_offset
, 0);
2574 qemu_co_mutex_lock(&s
->lock
);
2576 ret
= qcow2_handle_l2meta(bs
, &l2meta
, true);
2580 qemu_co_mutex_lock(&s
->lock
);
2583 qcow2_handle_l2meta(bs
, &l2meta
, false);
2584 qemu_co_mutex_unlock(&s
->lock
);
2586 qemu_vfree(crypt_buf
);
2591 static coroutine_fn
int qcow2_co_pwritev_task_entry(AioTask
*task
)
2593 Qcow2AioTask
*t
= container_of(task
, Qcow2AioTask
, task
);
2595 assert(!t
->subcluster_type
);
2597 return qcow2_co_pwritev_task(t
->bs
, t
->host_offset
,
2598 t
->offset
, t
->bytes
, t
->qiov
, t
->qiov_offset
,
2602 static coroutine_fn
int qcow2_co_pwritev_part(
2603 BlockDriverState
*bs
, int64_t offset
, int64_t bytes
,
2604 QEMUIOVector
*qiov
, size_t qiov_offset
, BdrvRequestFlags flags
)
2606 BDRVQcow2State
*s
= bs
->opaque
;
2607 int offset_in_cluster
;
2609 unsigned int cur_bytes
; /* number of sectors in current iteration */
2610 uint64_t host_offset
;
2611 QCowL2Meta
*l2meta
= NULL
;
2612 AioTaskPool
*aio
= NULL
;
2614 trace_qcow2_writev_start_req(qemu_coroutine_self(), offset
, bytes
);
2616 while (bytes
!= 0 && aio_task_pool_status(aio
) == 0) {
2620 trace_qcow2_writev_start_part(qemu_coroutine_self());
2621 offset_in_cluster
= offset_into_cluster(s
, offset
);
2622 cur_bytes
= MIN(bytes
, INT_MAX
);
2623 if (bs
->encrypted
) {
2624 cur_bytes
= MIN(cur_bytes
,
2625 QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_size
2626 - offset_in_cluster
);
2629 qemu_co_mutex_lock(&s
->lock
);
2631 ret
= qcow2_alloc_host_offset(bs
, offset
, &cur_bytes
,
2632 &host_offset
, &l2meta
);
2637 ret
= qcow2_pre_write_overlap_check(bs
, 0, host_offset
,
2643 qemu_co_mutex_unlock(&s
->lock
);
2645 if (!aio
&& cur_bytes
!= bytes
) {
2646 aio
= aio_task_pool_new(QCOW2_MAX_WORKERS
);
2648 ret
= qcow2_add_task(bs
, aio
, qcow2_co_pwritev_task_entry
, 0,
2649 host_offset
, offset
,
2650 cur_bytes
, qiov
, qiov_offset
, l2meta
);
2651 l2meta
= NULL
; /* l2meta is consumed by qcow2_co_pwritev_task() */
2657 offset
+= cur_bytes
;
2658 qiov_offset
+= cur_bytes
;
2659 trace_qcow2_writev_done_part(qemu_coroutine_self(), cur_bytes
);
2663 qemu_co_mutex_lock(&s
->lock
);
2666 qcow2_handle_l2meta(bs
, &l2meta
, false);
2668 qemu_co_mutex_unlock(&s
->lock
);
2672 aio_task_pool_wait_all(aio
);
2674 ret
= aio_task_pool_status(aio
);
2679 trace_qcow2_writev_done_req(qemu_coroutine_self(), ret
);
2684 static int qcow2_inactivate(BlockDriverState
*bs
)
2686 BDRVQcow2State
*s
= bs
->opaque
;
2687 int ret
, result
= 0;
2688 Error
*local_err
= NULL
;
2690 qcow2_store_persistent_dirty_bitmaps(bs
, true, &local_err
);
2691 if (local_err
!= NULL
) {
2693 error_reportf_err(local_err
, "Lost persistent bitmaps during "
2694 "inactivation of node '%s': ",
2695 bdrv_get_device_or_node_name(bs
));
2698 ret
= qcow2_cache_flush(bs
, s
->l2_table_cache
);
2701 error_report("Failed to flush the L2 table cache: %s",
2705 ret
= qcow2_cache_flush(bs
, s
->refcount_block_cache
);
2708 error_report("Failed to flush the refcount block cache: %s",
2713 qcow2_mark_clean(bs
);
2719 static void qcow2_do_close(BlockDriverState
*bs
, bool close_data_file
)
2721 BDRVQcow2State
*s
= bs
->opaque
;
2722 qemu_vfree(s
->l1_table
);
2723 /* else pre-write overlap checks in cache_destroy may crash */
2726 if (!(s
->flags
& BDRV_O_INACTIVE
)) {
2727 qcow2_inactivate(bs
);
2730 cache_clean_timer_del(bs
);
2731 qcow2_cache_destroy(s
->l2_table_cache
);
2732 qcow2_cache_destroy(s
->refcount_block_cache
);
2734 qcrypto_block_free(s
->crypto
);
2736 qapi_free_QCryptoBlockOpenOptions(s
->crypto_opts
);
2738 g_free(s
->unknown_header_fields
);
2739 cleanup_unknown_header_ext(bs
);
2741 g_free(s
->image_data_file
);
2742 g_free(s
->image_backing_file
);
2743 g_free(s
->image_backing_format
);
2745 if (close_data_file
&& has_data_file(bs
)) {
2746 bdrv_unref_child(bs
, s
->data_file
);
2747 s
->data_file
= NULL
;
2750 qcow2_refcount_close(bs
);
2751 qcow2_free_snapshots(bs
);
2754 static void qcow2_close(BlockDriverState
*bs
)
2756 qcow2_do_close(bs
, true);
2759 static void coroutine_fn
qcow2_co_invalidate_cache(BlockDriverState
*bs
,
2763 BDRVQcow2State
*s
= bs
->opaque
;
2764 BdrvChild
*data_file
;
2765 int flags
= s
->flags
;
2766 QCryptoBlock
*crypto
= NULL
;
2771 * Backing files are read-only which makes all of their metadata immutable,
2772 * that means we don't have to worry about reopening them here.
2779 * Do not reopen s->data_file (i.e., have qcow2_do_close() not close it,
2780 * and then prevent qcow2_do_open() from opening it), because this function
2781 * runs in the I/O path and as such we must not invoke global-state
2782 * functions like bdrv_unref_child() and bdrv_open_child().
2785 qcow2_do_close(bs
, false);
2787 data_file
= s
->data_file
;
2788 memset(s
, 0, sizeof(BDRVQcow2State
));
2789 s
->data_file
= data_file
;
2791 options
= qdict_clone_shallow(bs
->options
);
2793 flags
&= ~BDRV_O_INACTIVE
;
2794 qemu_co_mutex_lock(&s
->lock
);
2795 ret
= qcow2_do_open(bs
, options
, flags
, false, errp
);
2796 qemu_co_mutex_unlock(&s
->lock
);
2797 qobject_unref(options
);
2799 error_prepend(errp
, "Could not reopen qcow2 layer: ");
2807 static size_t header_ext_add(char *buf
, uint32_t magic
, const void *s
,
2808 size_t len
, size_t buflen
)
2810 QCowExtension
*ext_backing_fmt
= (QCowExtension
*) buf
;
2811 size_t ext_len
= sizeof(QCowExtension
) + ((len
+ 7) & ~7);
2813 if (buflen
< ext_len
) {
2817 *ext_backing_fmt
= (QCowExtension
) {
2818 .magic
= cpu_to_be32(magic
),
2819 .len
= cpu_to_be32(len
),
2823 memcpy(buf
+ sizeof(QCowExtension
), s
, len
);
2830 * Updates the qcow2 header, including the variable length parts of it, i.e.
2831 * the backing file name and all extensions. qcow2 was not designed to allow
2832 * such changes, so if we run out of space (we can only use the first cluster)
2833 * this function may fail.
2835 * Returns 0 on success, -errno in error cases.
2837 int qcow2_update_header(BlockDriverState
*bs
)
2839 BDRVQcow2State
*s
= bs
->opaque
;
2842 size_t buflen
= s
->cluster_size
;
2844 uint64_t total_size
;
2845 uint32_t refcount_table_clusters
;
2846 size_t header_length
;
2847 Qcow2UnknownHeaderExtension
*uext
;
2849 buf
= qemu_blockalign(bs
, buflen
);
2851 /* Header structure */
2852 header
= (QCowHeader
*) buf
;
2854 if (buflen
< sizeof(*header
)) {
2859 header_length
= sizeof(*header
) + s
->unknown_header_fields_size
;
2860 total_size
= bs
->total_sectors
* BDRV_SECTOR_SIZE
;
2861 refcount_table_clusters
= s
->refcount_table_size
>> (s
->cluster_bits
- 3);
2863 ret
= validate_compression_type(s
, NULL
);
2868 *header
= (QCowHeader
) {
2869 /* Version 2 fields */
2870 .magic
= cpu_to_be32(QCOW_MAGIC
),
2871 .version
= cpu_to_be32(s
->qcow_version
),
2872 .backing_file_offset
= 0,
2873 .backing_file_size
= 0,
2874 .cluster_bits
= cpu_to_be32(s
->cluster_bits
),
2875 .size
= cpu_to_be64(total_size
),
2876 .crypt_method
= cpu_to_be32(s
->crypt_method_header
),
2877 .l1_size
= cpu_to_be32(s
->l1_size
),
2878 .l1_table_offset
= cpu_to_be64(s
->l1_table_offset
),
2879 .refcount_table_offset
= cpu_to_be64(s
->refcount_table_offset
),
2880 .refcount_table_clusters
= cpu_to_be32(refcount_table_clusters
),
2881 .nb_snapshots
= cpu_to_be32(s
->nb_snapshots
),
2882 .snapshots_offset
= cpu_to_be64(s
->snapshots_offset
),
2884 /* Version 3 fields */
2885 .incompatible_features
= cpu_to_be64(s
->incompatible_features
),
2886 .compatible_features
= cpu_to_be64(s
->compatible_features
),
2887 .autoclear_features
= cpu_to_be64(s
->autoclear_features
),
2888 .refcount_order
= cpu_to_be32(s
->refcount_order
),
2889 .header_length
= cpu_to_be32(header_length
),
2890 .compression_type
= s
->compression_type
,
2893 /* For older versions, write a shorter header */
2894 switch (s
->qcow_version
) {
2896 ret
= offsetof(QCowHeader
, incompatible_features
);
2899 ret
= sizeof(*header
);
2908 memset(buf
, 0, buflen
);
2910 /* Preserve any unknown field in the header */
2911 if (s
->unknown_header_fields_size
) {
2912 if (buflen
< s
->unknown_header_fields_size
) {
2917 memcpy(buf
, s
->unknown_header_fields
, s
->unknown_header_fields_size
);
2918 buf
+= s
->unknown_header_fields_size
;
2919 buflen
-= s
->unknown_header_fields_size
;
2922 /* Backing file format header extension */
2923 if (s
->image_backing_format
) {
2924 ret
= header_ext_add(buf
, QCOW2_EXT_MAGIC_BACKING_FORMAT
,
2925 s
->image_backing_format
,
2926 strlen(s
->image_backing_format
),
2936 /* External data file header extension */
2937 if (has_data_file(bs
) && s
->image_data_file
) {
2938 ret
= header_ext_add(buf
, QCOW2_EXT_MAGIC_DATA_FILE
,
2939 s
->image_data_file
, strlen(s
->image_data_file
),
2949 /* Full disk encryption header pointer extension */
2950 if (s
->crypto_header
.offset
!= 0) {
2951 s
->crypto_header
.offset
= cpu_to_be64(s
->crypto_header
.offset
);
2952 s
->crypto_header
.length
= cpu_to_be64(s
->crypto_header
.length
);
2953 ret
= header_ext_add(buf
, QCOW2_EXT_MAGIC_CRYPTO_HEADER
,
2954 &s
->crypto_header
, sizeof(s
->crypto_header
),
2956 s
->crypto_header
.offset
= be64_to_cpu(s
->crypto_header
.offset
);
2957 s
->crypto_header
.length
= be64_to_cpu(s
->crypto_header
.length
);
2966 * Feature table. A mere 8 feature names occupies 392 bytes, and
2967 * when coupled with the v3 minimum header of 104 bytes plus the
2968 * 8-byte end-of-extension marker, that would leave only 8 bytes
2969 * for a backing file name in an image with 512-byte clusters.
2970 * Thus, we choose to omit this header for cluster sizes 4k and
2973 if (s
->qcow_version
>= 3 && s
->cluster_size
> 4096) {
2974 static const Qcow2Feature features
[] = {
2976 .type
= QCOW2_FEAT_TYPE_INCOMPATIBLE
,
2977 .bit
= QCOW2_INCOMPAT_DIRTY_BITNR
,
2978 .name
= "dirty bit",
2981 .type
= QCOW2_FEAT_TYPE_INCOMPATIBLE
,
2982 .bit
= QCOW2_INCOMPAT_CORRUPT_BITNR
,
2983 .name
= "corrupt bit",
2986 .type
= QCOW2_FEAT_TYPE_INCOMPATIBLE
,
2987 .bit
= QCOW2_INCOMPAT_DATA_FILE_BITNR
,
2988 .name
= "external data file",
2991 .type
= QCOW2_FEAT_TYPE_INCOMPATIBLE
,
2992 .bit
= QCOW2_INCOMPAT_COMPRESSION_BITNR
,
2993 .name
= "compression type",
2996 .type
= QCOW2_FEAT_TYPE_INCOMPATIBLE
,
2997 .bit
= QCOW2_INCOMPAT_EXTL2_BITNR
,
2998 .name
= "extended L2 entries",
3001 .type
= QCOW2_FEAT_TYPE_COMPATIBLE
,
3002 .bit
= QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR
,
3003 .name
= "lazy refcounts",
3006 .type
= QCOW2_FEAT_TYPE_AUTOCLEAR
,
3007 .bit
= QCOW2_AUTOCLEAR_BITMAPS_BITNR
,
3011 .type
= QCOW2_FEAT_TYPE_AUTOCLEAR
,
3012 .bit
= QCOW2_AUTOCLEAR_DATA_FILE_RAW_BITNR
,
3013 .name
= "raw external data",
3017 ret
= header_ext_add(buf
, QCOW2_EXT_MAGIC_FEATURE_TABLE
,
3018 features
, sizeof(features
), buflen
);
3026 /* Bitmap extension */
3027 if (s
->nb_bitmaps
> 0) {
3028 Qcow2BitmapHeaderExt bitmaps_header
= {
3029 .nb_bitmaps
= cpu_to_be32(s
->nb_bitmaps
),
3030 .bitmap_directory_size
=
3031 cpu_to_be64(s
->bitmap_directory_size
),
3032 .bitmap_directory_offset
=
3033 cpu_to_be64(s
->bitmap_directory_offset
)
3035 ret
= header_ext_add(buf
, QCOW2_EXT_MAGIC_BITMAPS
,
3036 &bitmaps_header
, sizeof(bitmaps_header
),
3045 /* Keep unknown header extensions */
3046 QLIST_FOREACH(uext
, &s
->unknown_header_ext
, next
) {
3047 ret
= header_ext_add(buf
, uext
->magic
, uext
->data
, uext
->len
, buflen
);
3056 /* End of header extensions */
3057 ret
= header_ext_add(buf
, QCOW2_EXT_MAGIC_END
, NULL
, 0, buflen
);
3065 /* Backing file name */
3066 if (s
->image_backing_file
) {
3067 size_t backing_file_len
= strlen(s
->image_backing_file
);
3069 if (buflen
< backing_file_len
) {
3074 /* Using strncpy is ok here, since buf is not NUL-terminated. */
3075 strncpy(buf
, s
->image_backing_file
, buflen
);
3077 header
->backing_file_offset
= cpu_to_be64(buf
- ((char*) header
));
3078 header
->backing_file_size
= cpu_to_be32(backing_file_len
);
3081 /* Write the new header */
3082 ret
= bdrv_pwrite(bs
->file
, 0, s
->cluster_size
, header
, 0);
3093 static int qcow2_change_backing_file(BlockDriverState
*bs
,
3094 const char *backing_file
, const char *backing_fmt
)
3096 BDRVQcow2State
*s
= bs
->opaque
;
3098 /* Adding a backing file means that the external data file alone won't be
3099 * enough to make sense of the content */
3100 if (backing_file
&& data_file_is_raw(bs
)) {
3104 if (backing_file
&& strlen(backing_file
) > 1023) {
3108 pstrcpy(bs
->auto_backing_file
, sizeof(bs
->auto_backing_file
),
3109 backing_file
?: "");
3110 pstrcpy(bs
->backing_file
, sizeof(bs
->backing_file
), backing_file
?: "");
3111 pstrcpy(bs
->backing_format
, sizeof(bs
->backing_format
), backing_fmt
?: "");
3113 g_free(s
->image_backing_file
);
3114 g_free(s
->image_backing_format
);
3116 s
->image_backing_file
= backing_file
? g_strdup(bs
->backing_file
) : NULL
;
3117 s
->image_backing_format
= backing_fmt
? g_strdup(bs
->backing_format
) : NULL
;
3119 return qcow2_update_header(bs
);
3122 static int qcow2_set_up_encryption(BlockDriverState
*bs
,
3123 QCryptoBlockCreateOptions
*cryptoopts
,
3126 BDRVQcow2State
*s
= bs
->opaque
;
3127 QCryptoBlock
*crypto
= NULL
;
3130 switch (cryptoopts
->format
) {
3131 case Q_CRYPTO_BLOCK_FORMAT_LUKS
:
3132 fmt
= QCOW_CRYPT_LUKS
;
3134 case Q_CRYPTO_BLOCK_FORMAT_QCOW
:
3135 fmt
= QCOW_CRYPT_AES
;
3138 error_setg(errp
, "Crypto format not supported in qcow2");
3142 s
->crypt_method_header
= fmt
;
3144 crypto
= qcrypto_block_create(cryptoopts
, "encrypt.",
3145 qcow2_crypto_hdr_init_func
,
3146 qcow2_crypto_hdr_write_func
,
3152 ret
= qcow2_update_header(bs
);
3154 error_setg_errno(errp
, -ret
, "Could not write encryption header");
3160 qcrypto_block_free(crypto
);
3165 * Preallocates metadata structures for data clusters between @offset (in the
3166 * guest disk) and @new_length (which is thus generally the new guest disk
3169 * Returns: 0 on success, -errno on failure.
3171 static int coroutine_fn
preallocate_co(BlockDriverState
*bs
, uint64_t offset
,
3172 uint64_t new_length
, PreallocMode mode
,
3175 BDRVQcow2State
*s
= bs
->opaque
;
3177 uint64_t host_offset
= 0;
3178 int64_t file_length
;
3179 unsigned int cur_bytes
;
3181 QCowL2Meta
*meta
= NULL
, *m
;
3183 assert(offset
<= new_length
);
3184 bytes
= new_length
- offset
;
3187 cur_bytes
= MIN(bytes
, QEMU_ALIGN_DOWN(INT_MAX
, s
->cluster_size
));
3188 ret
= qcow2_alloc_host_offset(bs
, offset
, &cur_bytes
,
3189 &host_offset
, &meta
);
3191 error_setg_errno(errp
, -ret
, "Allocating clusters failed");
3195 for (m
= meta
; m
!= NULL
; m
= m
->next
) {
3199 ret
= qcow2_handle_l2meta(bs
, &meta
, true);
3201 error_setg_errno(errp
, -ret
, "Mapping clusters failed");
3205 /* TODO Preallocate data if requested */
3208 offset
+= cur_bytes
;
3212 * It is expected that the image file is large enough to actually contain
3213 * all of the allocated clusters (otherwise we get failing reads after
3214 * EOF). Extend the image to the last allocated sector.
3216 file_length
= bdrv_getlength(s
->data_file
->bs
);
3217 if (file_length
< 0) {
3218 error_setg_errno(errp
, -file_length
, "Could not get file size");
3223 if (host_offset
+ cur_bytes
> file_length
) {
3224 if (mode
== PREALLOC_MODE_METADATA
) {
3225 mode
= PREALLOC_MODE_OFF
;
3227 ret
= bdrv_co_truncate(s
->data_file
, host_offset
+ cur_bytes
, false,
3237 qcow2_handle_l2meta(bs
, &meta
, false);
3241 /* qcow2_refcount_metadata_size:
3242 * @clusters: number of clusters to refcount (including data and L1/L2 tables)
3243 * @cluster_size: size of a cluster, in bytes
3244 * @refcount_order: refcount bits power-of-2 exponent
3245 * @generous_increase: allow for the refcount table to be 1.5x as large as it
3248 * Returns: Number of bytes required for refcount blocks and table metadata.
3250 int64_t qcow2_refcount_metadata_size(int64_t clusters
, size_t cluster_size
,
3251 int refcount_order
, bool generous_increase
,
3252 uint64_t *refblock_count
)
3255 * Every host cluster is reference-counted, including metadata (even
3256 * refcount metadata is recursively included).
3258 * An accurate formula for the size of refcount metadata size is difficult
3259 * to derive. An easier method of calculation is finding the fixed point
3260 * where no further refcount blocks or table clusters are required to
3261 * reference count every cluster.
3263 int64_t blocks_per_table_cluster
= cluster_size
/ REFTABLE_ENTRY_SIZE
;
3264 int64_t refcounts_per_block
= cluster_size
* 8 / (1 << refcount_order
);
3265 int64_t table
= 0; /* number of refcount table clusters */
3266 int64_t blocks
= 0; /* number of refcount block clusters */
3272 blocks
= DIV_ROUND_UP(clusters
+ table
+ blocks
, refcounts_per_block
);
3273 table
= DIV_ROUND_UP(blocks
, blocks_per_table_cluster
);
3274 n
= clusters
+ blocks
+ table
;
3276 if (n
== last
&& generous_increase
) {
3277 clusters
+= DIV_ROUND_UP(table
, 2);
3278 n
= 0; /* force another loop */
3279 generous_increase
= false;
3281 } while (n
!= last
);
3283 if (refblock_count
) {
3284 *refblock_count
= blocks
;
3287 return (blocks
+ table
) * cluster_size
;
3291 * qcow2_calc_prealloc_size:
3292 * @total_size: virtual disk size in bytes
3293 * @cluster_size: cluster size in bytes
3294 * @refcount_order: refcount bits power-of-2 exponent
3295 * @extended_l2: true if the image has extended L2 entries
3297 * Returns: Total number of bytes required for the fully allocated image
3298 * (including metadata).
3300 static int64_t qcow2_calc_prealloc_size(int64_t total_size
,
3301 size_t cluster_size
,
3305 int64_t meta_size
= 0;
3306 uint64_t nl1e
, nl2e
;
3307 int64_t aligned_total_size
= ROUND_UP(total_size
, cluster_size
);
3308 size_t l2e_size
= extended_l2
? L2E_SIZE_EXTENDED
: L2E_SIZE_NORMAL
;
3310 /* header: 1 cluster */
3311 meta_size
+= cluster_size
;
3313 /* total size of L2 tables */
3314 nl2e
= aligned_total_size
/ cluster_size
;
3315 nl2e
= ROUND_UP(nl2e
, cluster_size
/ l2e_size
);
3316 meta_size
+= nl2e
* l2e_size
;
3318 /* total size of L1 tables */
3319 nl1e
= nl2e
* l2e_size
/ cluster_size
;
3320 nl1e
= ROUND_UP(nl1e
, cluster_size
/ L1E_SIZE
);
3321 meta_size
+= nl1e
* L1E_SIZE
;
3323 /* total size of refcount table and blocks */
3324 meta_size
+= qcow2_refcount_metadata_size(
3325 (meta_size
+ aligned_total_size
) / cluster_size
,
3326 cluster_size
, refcount_order
, false, NULL
);
3328 return meta_size
+ aligned_total_size
;
3331 static bool validate_cluster_size(size_t cluster_size
, bool extended_l2
,
3334 int cluster_bits
= ctz32(cluster_size
);
3335 if (cluster_bits
< MIN_CLUSTER_BITS
|| cluster_bits
> MAX_CLUSTER_BITS
||
3336 (1 << cluster_bits
) != cluster_size
)
3338 error_setg(errp
, "Cluster size must be a power of two between %d and "
3339 "%dk", 1 << MIN_CLUSTER_BITS
, 1 << (MAX_CLUSTER_BITS
- 10));
3344 unsigned min_cluster_size
=
3345 (1 << MIN_CLUSTER_BITS
) * QCOW_EXTL2_SUBCLUSTERS_PER_CLUSTER
;
3346 if (cluster_size
< min_cluster_size
) {
3347 error_setg(errp
, "Extended L2 entries are only supported with "
3348 "cluster sizes of at least %u bytes", min_cluster_size
);
3356 static size_t qcow2_opt_get_cluster_size_del(QemuOpts
*opts
, bool extended_l2
,
3359 size_t cluster_size
;
3361 cluster_size
= qemu_opt_get_size_del(opts
, BLOCK_OPT_CLUSTER_SIZE
,
3362 DEFAULT_CLUSTER_SIZE
);
3363 if (!validate_cluster_size(cluster_size
, extended_l2
, errp
)) {
3366 return cluster_size
;
3369 static int qcow2_opt_get_version_del(QemuOpts
*opts
, Error
**errp
)
3374 buf
= qemu_opt_get_del(opts
, BLOCK_OPT_COMPAT_LEVEL
);
3376 ret
= 3; /* default */
3377 } else if (!strcmp(buf
, "0.10")) {
3379 } else if (!strcmp(buf
, "1.1")) {
3382 error_setg(errp
, "Invalid compatibility level: '%s'", buf
);
3389 static uint64_t qcow2_opt_get_refcount_bits_del(QemuOpts
*opts
, int version
,
3392 uint64_t refcount_bits
;
3394 refcount_bits
= qemu_opt_get_number_del(opts
, BLOCK_OPT_REFCOUNT_BITS
, 16);
3395 if (refcount_bits
> 64 || !is_power_of_2(refcount_bits
)) {
3396 error_setg(errp
, "Refcount width must be a power of two and may not "
3401 if (version
< 3 && refcount_bits
!= 16) {
3402 error_setg(errp
, "Different refcount widths than 16 bits require "
3403 "compatibility level 1.1 or above (use compat=1.1 or "
3408 return refcount_bits
;
3411 static int coroutine_fn
3412 qcow2_co_create(BlockdevCreateOptions
*create_options
, Error
**errp
)
3414 BlockdevCreateOptionsQcow2
*qcow2_opts
;
3418 * Open the image file and write a minimal qcow2 header.
3420 * We keep things simple and start with a zero-sized image. We also
3421 * do without refcount blocks or a L1 table for now. We'll fix the
3422 * inconsistency later.
3424 * We do need a refcount table because growing the refcount table means
3425 * allocating two new refcount blocks - the second of which would be at
3426 * 2 GB for 64k clusters, and we don't want to have a 2 GB initial file
3427 * size for any qcow2 image.
3429 BlockBackend
*blk
= NULL
;
3430 BlockDriverState
*bs
= NULL
;
3431 BlockDriverState
*data_bs
= NULL
;
3433 size_t cluster_size
;
3436 uint64_t *refcount_table
;
3438 uint8_t compression_type
= QCOW2_COMPRESSION_TYPE_ZLIB
;
3440 assert(create_options
->driver
== BLOCKDEV_DRIVER_QCOW2
);
3441 qcow2_opts
= &create_options
->u
.qcow2
;
3443 bs
= bdrv_open_blockdev_ref(qcow2_opts
->file
, errp
);
3448 /* Validate options and set default values */
3449 if (!QEMU_IS_ALIGNED(qcow2_opts
->size
, BDRV_SECTOR_SIZE
)) {
3450 error_setg(errp
, "Image size must be a multiple of %u bytes",
3451 (unsigned) BDRV_SECTOR_SIZE
);
3456 if (qcow2_opts
->has_version
) {
3457 switch (qcow2_opts
->version
) {
3458 case BLOCKDEV_QCOW2_VERSION_V2
:
3461 case BLOCKDEV_QCOW2_VERSION_V3
:
3465 g_assert_not_reached();
3471 if (qcow2_opts
->has_cluster_size
) {
3472 cluster_size
= qcow2_opts
->cluster_size
;
3474 cluster_size
= DEFAULT_CLUSTER_SIZE
;
3477 if (!qcow2_opts
->has_extended_l2
) {
3478 qcow2_opts
->extended_l2
= false;
3480 if (qcow2_opts
->extended_l2
) {
3482 error_setg(errp
, "Extended L2 entries are only supported with "
3483 "compatibility level 1.1 and above (use version=v3 or "
3490 if (!validate_cluster_size(cluster_size
, qcow2_opts
->extended_l2
, errp
)) {
3495 if (!qcow2_opts
->has_preallocation
) {
3496 qcow2_opts
->preallocation
= PREALLOC_MODE_OFF
;
3498 if (qcow2_opts
->has_backing_file
&&
3499 qcow2_opts
->preallocation
!= PREALLOC_MODE_OFF
&&
3500 !qcow2_opts
->extended_l2
)
3502 error_setg(errp
, "Backing file and preallocation can only be used at "
3503 "the same time if extended_l2 is on");
3507 if (qcow2_opts
->has_backing_fmt
&& !qcow2_opts
->has_backing_file
) {
3508 error_setg(errp
, "Backing format cannot be used without backing file");
3513 if (!qcow2_opts
->has_lazy_refcounts
) {
3514 qcow2_opts
->lazy_refcounts
= false;
3516 if (version
< 3 && qcow2_opts
->lazy_refcounts
) {
3517 error_setg(errp
, "Lazy refcounts only supported with compatibility "
3518 "level 1.1 and above (use version=v3 or greater)");
3523 if (!qcow2_opts
->has_refcount_bits
) {
3524 qcow2_opts
->refcount_bits
= 16;
3526 if (qcow2_opts
->refcount_bits
> 64 ||
3527 !is_power_of_2(qcow2_opts
->refcount_bits
))
3529 error_setg(errp
, "Refcount width must be a power of two and may not "
3534 if (version
< 3 && qcow2_opts
->refcount_bits
!= 16) {
3535 error_setg(errp
, "Different refcount widths than 16 bits require "
3536 "compatibility level 1.1 or above (use version=v3 or "
3541 refcount_order
= ctz32(qcow2_opts
->refcount_bits
);
3543 if (qcow2_opts
->data_file_raw
&& !qcow2_opts
->data_file
) {
3544 error_setg(errp
, "data-file-raw requires data-file");
3548 if (qcow2_opts
->data_file_raw
&& qcow2_opts
->has_backing_file
) {
3549 error_setg(errp
, "Backing file and data-file-raw cannot be used at "
3554 if (qcow2_opts
->data_file_raw
&&
3555 qcow2_opts
->preallocation
== PREALLOC_MODE_OFF
)
3558 * data-file-raw means that "the external data file can be
3559 * read as a consistent standalone raw image without looking
3560 * at the qcow2 metadata." It does not say that the metadata
3561 * must be ignored, though (and the qcow2 driver in fact does
3562 * not ignore it), so the L1/L2 tables must be present and
3563 * give a 1:1 mapping, so you get the same result regardless
3564 * of whether you look at the metadata or whether you ignore
3567 qcow2_opts
->preallocation
= PREALLOC_MODE_METADATA
;
3570 * Cannot use preallocation with backing files, but giving a
3571 * backing file when specifying data_file_raw is an error
3574 assert(!qcow2_opts
->has_backing_file
);
3577 if (qcow2_opts
->data_file
) {
3579 error_setg(errp
, "External data files are only supported with "
3580 "compatibility level 1.1 and above (use version=v3 or "
3585 data_bs
= bdrv_open_blockdev_ref(qcow2_opts
->data_file
, errp
);
3586 if (data_bs
== NULL
) {
3592 if (qcow2_opts
->has_compression_type
&&
3593 qcow2_opts
->compression_type
!= QCOW2_COMPRESSION_TYPE_ZLIB
) {
3598 error_setg(errp
, "Non-zlib compression type is only supported with "
3599 "compatibility level 1.1 and above (use version=v3 or "
3604 switch (qcow2_opts
->compression_type
) {
3606 case QCOW2_COMPRESSION_TYPE_ZSTD
:
3610 error_setg(errp
, "Unknown compression type");
3614 compression_type
= qcow2_opts
->compression_type
;
3617 /* Create BlockBackend to write to the image */
3618 blk
= blk_new_with_bs(bs
, BLK_PERM_WRITE
| BLK_PERM_RESIZE
, BLK_PERM_ALL
,
3624 blk_set_allow_write_beyond_eof(blk
, true);
3626 /* Write the header */
3627 QEMU_BUILD_BUG_ON((1 << MIN_CLUSTER_BITS
) < sizeof(*header
));
3628 header
= g_malloc0(cluster_size
);
3629 *header
= (QCowHeader
) {
3630 .magic
= cpu_to_be32(QCOW_MAGIC
),
3631 .version
= cpu_to_be32(version
),
3632 .cluster_bits
= cpu_to_be32(ctz32(cluster_size
)),
3633 .size
= cpu_to_be64(0),
3634 .l1_table_offset
= cpu_to_be64(0),
3635 .l1_size
= cpu_to_be32(0),
3636 .refcount_table_offset
= cpu_to_be64(cluster_size
),
3637 .refcount_table_clusters
= cpu_to_be32(1),
3638 .refcount_order
= cpu_to_be32(refcount_order
),
3639 /* don't deal with endianness since compression_type is 1 byte long */
3640 .compression_type
= compression_type
,
3641 .header_length
= cpu_to_be32(sizeof(*header
)),
3644 /* We'll update this to correct value later */
3645 header
->crypt_method
= cpu_to_be32(QCOW_CRYPT_NONE
);
3647 if (qcow2_opts
->lazy_refcounts
) {
3648 header
->compatible_features
|=
3649 cpu_to_be64(QCOW2_COMPAT_LAZY_REFCOUNTS
);
3652 header
->incompatible_features
|=
3653 cpu_to_be64(QCOW2_INCOMPAT_DATA_FILE
);
3655 if (qcow2_opts
->data_file_raw
) {
3656 header
->autoclear_features
|=
3657 cpu_to_be64(QCOW2_AUTOCLEAR_DATA_FILE_RAW
);
3659 if (compression_type
!= QCOW2_COMPRESSION_TYPE_ZLIB
) {
3660 header
->incompatible_features
|=
3661 cpu_to_be64(QCOW2_INCOMPAT_COMPRESSION
);
3664 if (qcow2_opts
->extended_l2
) {
3665 header
->incompatible_features
|=
3666 cpu_to_be64(QCOW2_INCOMPAT_EXTL2
);
3669 ret
= blk_pwrite(blk
, 0, cluster_size
, header
, 0);
3672 error_setg_errno(errp
, -ret
, "Could not write qcow2 header");
3676 /* Write a refcount table with one refcount block */
3677 refcount_table
= g_malloc0(2 * cluster_size
);
3678 refcount_table
[0] = cpu_to_be64(2 * cluster_size
);
3679 ret
= blk_pwrite(blk
, cluster_size
, 2 * cluster_size
, refcount_table
, 0);
3680 g_free(refcount_table
);
3683 error_setg_errno(errp
, -ret
, "Could not write refcount table");
3691 * And now open the image and make it consistent first (i.e. increase the
3692 * refcount of the cluster that is occupied by the header and the refcount
3695 options
= qdict_new();
3696 qdict_put_str(options
, "driver", "qcow2");
3697 qdict_put_str(options
, "file", bs
->node_name
);
3699 qdict_put_str(options
, "data-file", data_bs
->node_name
);
3701 blk
= blk_new_open(NULL
, NULL
, options
,
3702 BDRV_O_RDWR
| BDRV_O_RESIZE
| BDRV_O_NO_FLUSH
,
3709 ret
= qcow2_alloc_clusters(blk_bs(blk
), 3 * cluster_size
);
3711 error_setg_errno(errp
, -ret
, "Could not allocate clusters for qcow2 "
3712 "header and refcount table");
3715 } else if (ret
!= 0) {
3716 error_report("Huh, first cluster in empty image is already in use?");
3720 /* Set the external data file if necessary */
3722 BDRVQcow2State
*s
= blk_bs(blk
)->opaque
;
3723 s
->image_data_file
= g_strdup(data_bs
->filename
);
3726 /* Create a full header (including things like feature table) */
3727 ret
= qcow2_update_header(blk_bs(blk
));
3729 error_setg_errno(errp
, -ret
, "Could not update qcow2 header");
3733 /* Okay, now that we have a valid image, let's give it the right size */
3734 ret
= blk_truncate(blk
, qcow2_opts
->size
, false, qcow2_opts
->preallocation
,
3737 error_prepend(errp
, "Could not resize image: ");
3741 /* Want a backing file? There you go. */
3742 if (qcow2_opts
->has_backing_file
) {
3743 const char *backing_format
= NULL
;
3745 if (qcow2_opts
->has_backing_fmt
) {
3746 backing_format
= BlockdevDriver_str(qcow2_opts
->backing_fmt
);
3749 ret
= bdrv_change_backing_file(blk_bs(blk
), qcow2_opts
->backing_file
,
3750 backing_format
, false);
3752 error_setg_errno(errp
, -ret
, "Could not assign backing file '%s' "
3753 "with format '%s'", qcow2_opts
->backing_file
,
3759 /* Want encryption? There you go. */
3760 if (qcow2_opts
->has_encrypt
) {
3761 ret
= qcow2_set_up_encryption(blk_bs(blk
), qcow2_opts
->encrypt
, errp
);
3770 /* Reopen the image without BDRV_O_NO_FLUSH to flush it before returning.
3771 * Using BDRV_O_NO_IO, since encryption is now setup we don't want to
3772 * have to setup decryption context. We're not doing any I/O on the top
3773 * level BlockDriverState, only lower layers, where BDRV_O_NO_IO does
3776 options
= qdict_new();
3777 qdict_put_str(options
, "driver", "qcow2");
3778 qdict_put_str(options
, "file", bs
->node_name
);
3780 qdict_put_str(options
, "data-file", data_bs
->node_name
);
3782 blk
= blk_new_open(NULL
, NULL
, options
,
3783 BDRV_O_RDWR
| BDRV_O_NO_BACKING
| BDRV_O_NO_IO
,
3794 bdrv_unref(data_bs
);
3798 static int coroutine_fn
qcow2_co_create_opts(BlockDriver
*drv
,
3799 const char *filename
,
3803 BlockdevCreateOptions
*create_options
= NULL
;
3806 BlockDriverState
*bs
= NULL
;
3807 BlockDriverState
*data_bs
= NULL
;
3811 /* Only the keyval visitor supports the dotted syntax needed for
3812 * encryption, so go through a QDict before getting a QAPI type. Ignore
3813 * options meant for the protocol layer so that the visitor doesn't
3815 qdict
= qemu_opts_to_qdict_filtered(opts
, NULL
, bdrv_qcow2
.create_opts
,
3818 /* Handle encryption options */
3819 val
= qdict_get_try_str(qdict
, BLOCK_OPT_ENCRYPT
);
3820 if (val
&& !strcmp(val
, "on")) {
3821 qdict_put_str(qdict
, BLOCK_OPT_ENCRYPT
, "qcow");
3822 } else if (val
&& !strcmp(val
, "off")) {
3823 qdict_del(qdict
, BLOCK_OPT_ENCRYPT
);
3826 val
= qdict_get_try_str(qdict
, BLOCK_OPT_ENCRYPT_FORMAT
);
3827 if (val
&& !strcmp(val
, "aes")) {
3828 qdict_put_str(qdict
, BLOCK_OPT_ENCRYPT_FORMAT
, "qcow");
3831 /* Convert compat=0.10/1.1 into compat=v2/v3, to be renamed into
3832 * version=v2/v3 below. */
3833 val
= qdict_get_try_str(qdict
, BLOCK_OPT_COMPAT_LEVEL
);
3834 if (val
&& !strcmp(val
, "0.10")) {
3835 qdict_put_str(qdict
, BLOCK_OPT_COMPAT_LEVEL
, "v2");
3836 } else if (val
&& !strcmp(val
, "1.1")) {
3837 qdict_put_str(qdict
, BLOCK_OPT_COMPAT_LEVEL
, "v3");
3840 /* Change legacy command line options into QMP ones */
3841 static const QDictRenames opt_renames
[] = {
3842 { BLOCK_OPT_BACKING_FILE
, "backing-file" },
3843 { BLOCK_OPT_BACKING_FMT
, "backing-fmt" },
3844 { BLOCK_OPT_CLUSTER_SIZE
, "cluster-size" },
3845 { BLOCK_OPT_LAZY_REFCOUNTS
, "lazy-refcounts" },
3846 { BLOCK_OPT_EXTL2
, "extended-l2" },
3847 { BLOCK_OPT_REFCOUNT_BITS
, "refcount-bits" },
3848 { BLOCK_OPT_ENCRYPT
, BLOCK_OPT_ENCRYPT_FORMAT
},
3849 { BLOCK_OPT_COMPAT_LEVEL
, "version" },
3850 { BLOCK_OPT_DATA_FILE_RAW
, "data-file-raw" },
3851 { BLOCK_OPT_COMPRESSION_TYPE
, "compression-type" },
3855 if (!qdict_rename_keys(qdict
, opt_renames
, errp
)) {
3860 /* Create and open the file (protocol layer) */
3861 ret
= bdrv_create_file(filename
, opts
, errp
);
3866 bs
= bdrv_open(filename
, NULL
, NULL
,
3867 BDRV_O_RDWR
| BDRV_O_RESIZE
| BDRV_O_PROTOCOL
, errp
);
3873 /* Create and open an external data file (protocol layer) */
3874 val
= qdict_get_try_str(qdict
, BLOCK_OPT_DATA_FILE
);
3876 ret
= bdrv_create_file(val
, opts
, errp
);
3881 data_bs
= bdrv_open(val
, NULL
, NULL
,
3882 BDRV_O_RDWR
| BDRV_O_RESIZE
| BDRV_O_PROTOCOL
,
3884 if (data_bs
== NULL
) {
3889 qdict_del(qdict
, BLOCK_OPT_DATA_FILE
);
3890 qdict_put_str(qdict
, "data-file", data_bs
->node_name
);
3893 /* Set 'driver' and 'node' options */
3894 qdict_put_str(qdict
, "driver", "qcow2");
3895 qdict_put_str(qdict
, "file", bs
->node_name
);
3897 /* Now get the QAPI type BlockdevCreateOptions */
3898 v
= qobject_input_visitor_new_flat_confused(qdict
, errp
);
3904 visit_type_BlockdevCreateOptions(v
, NULL
, &create_options
, errp
);
3906 if (!create_options
) {
3911 /* Silently round up size */
3912 create_options
->u
.qcow2
.size
= ROUND_UP(create_options
->u
.qcow2
.size
,
3915 /* Create the qcow2 image (format layer) */
3916 ret
= qcow2_co_create(create_options
, errp
);
3919 bdrv_co_delete_file_noerr(bs
);
3920 bdrv_co_delete_file_noerr(data_bs
);
3925 qobject_unref(qdict
);
3927 bdrv_unref(data_bs
);
3928 qapi_free_BlockdevCreateOptions(create_options
);
3933 static bool is_zero(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
)
3938 /* Clamp to image length, before checking status of underlying sectors */
3939 if (offset
+ bytes
> bs
->total_sectors
* BDRV_SECTOR_SIZE
) {
3940 bytes
= bs
->total_sectors
* BDRV_SECTOR_SIZE
- offset
;
3948 * bdrv_block_status_above doesn't merge different types of zeros, for
3949 * example, zeros which come from the region which is unallocated in
3950 * the whole backing chain, and zeros which come because of a short
3951 * backing file. So, we need a loop.
3954 res
= bdrv_block_status_above(bs
, NULL
, offset
, bytes
, &nr
, NULL
, NULL
);
3957 } while (res
>= 0 && (res
& BDRV_BLOCK_ZERO
) && nr
&& bytes
);
3959 return res
>= 0 && (res
& BDRV_BLOCK_ZERO
) && bytes
== 0;
3962 static coroutine_fn
int qcow2_co_pwrite_zeroes(BlockDriverState
*bs
,
3963 int64_t offset
, int64_t bytes
, BdrvRequestFlags flags
)
3966 BDRVQcow2State
*s
= bs
->opaque
;
3968 uint32_t head
= offset_into_subcluster(s
, offset
);
3969 uint32_t tail
= ROUND_UP(offset
+ bytes
, s
->subcluster_size
) -
3972 trace_qcow2_pwrite_zeroes_start_req(qemu_coroutine_self(), offset
, bytes
);
3973 if (offset
+ bytes
== bs
->total_sectors
* BDRV_SECTOR_SIZE
) {
3980 QCow2SubclusterType type
;
3982 assert(head
+ bytes
+ tail
<= s
->subcluster_size
);
3984 /* check whether remainder of cluster already reads as zero */
3985 if (!(is_zero(bs
, offset
- head
, head
) &&
3986 is_zero(bs
, offset
+ bytes
, tail
))) {
3990 qemu_co_mutex_lock(&s
->lock
);
3991 /* We can have new write after previous check */
3993 bytes
= s
->subcluster_size
;
3994 nr
= s
->subcluster_size
;
3995 ret
= qcow2_get_host_offset(bs
, offset
, &nr
, &off
, &type
);
3997 (type
!= QCOW2_SUBCLUSTER_UNALLOCATED_PLAIN
&&
3998 type
!= QCOW2_SUBCLUSTER_UNALLOCATED_ALLOC
&&
3999 type
!= QCOW2_SUBCLUSTER_ZERO_PLAIN
&&
4000 type
!= QCOW2_SUBCLUSTER_ZERO_ALLOC
)) {
4001 qemu_co_mutex_unlock(&s
->lock
);
4002 return ret
< 0 ? ret
: -ENOTSUP
;
4005 qemu_co_mutex_lock(&s
->lock
);
4008 trace_qcow2_pwrite_zeroes(qemu_coroutine_self(), offset
, bytes
);
4010 /* Whatever is left can use real zero subclusters */
4011 ret
= qcow2_subcluster_zeroize(bs
, offset
, bytes
, flags
);
4012 qemu_co_mutex_unlock(&s
->lock
);
4017 static coroutine_fn
int qcow2_co_pdiscard(BlockDriverState
*bs
,
4018 int64_t offset
, int64_t bytes
)
4021 BDRVQcow2State
*s
= bs
->opaque
;
4023 /* If the image does not support QCOW_OFLAG_ZERO then discarding
4024 * clusters could expose stale data from the backing file. */
4025 if (s
->qcow_version
< 3 && bs
->backing
) {
4029 if (!QEMU_IS_ALIGNED(offset
| bytes
, s
->cluster_size
)) {
4030 assert(bytes
< s
->cluster_size
);
4031 /* Ignore partial clusters, except for the special case of the
4032 * complete partial cluster at the end of an unaligned file */
4033 if (!QEMU_IS_ALIGNED(offset
, s
->cluster_size
) ||
4034 offset
+ bytes
!= bs
->total_sectors
* BDRV_SECTOR_SIZE
) {
4039 qemu_co_mutex_lock(&s
->lock
);
4040 ret
= qcow2_cluster_discard(bs
, offset
, bytes
, QCOW2_DISCARD_REQUEST
,
4042 qemu_co_mutex_unlock(&s
->lock
);
4046 static int coroutine_fn
4047 qcow2_co_copy_range_from(BlockDriverState
*bs
,
4048 BdrvChild
*src
, int64_t src_offset
,
4049 BdrvChild
*dst
, int64_t dst_offset
,
4050 int64_t bytes
, BdrvRequestFlags read_flags
,
4051 BdrvRequestFlags write_flags
)
4053 BDRVQcow2State
*s
= bs
->opaque
;
4055 unsigned int cur_bytes
; /* number of bytes in current iteration */
4056 BdrvChild
*child
= NULL
;
4057 BdrvRequestFlags cur_write_flags
;
4059 assert(!bs
->encrypted
);
4060 qemu_co_mutex_lock(&s
->lock
);
4062 while (bytes
!= 0) {
4063 uint64_t copy_offset
= 0;
4064 QCow2SubclusterType type
;
4065 /* prepare next request */
4066 cur_bytes
= MIN(bytes
, INT_MAX
);
4067 cur_write_flags
= write_flags
;
4069 ret
= qcow2_get_host_offset(bs
, src_offset
, &cur_bytes
,
4070 ©_offset
, &type
);
4076 case QCOW2_SUBCLUSTER_UNALLOCATED_PLAIN
:
4077 case QCOW2_SUBCLUSTER_UNALLOCATED_ALLOC
:
4078 if (bs
->backing
&& bs
->backing
->bs
) {
4079 int64_t backing_length
= bdrv_getlength(bs
->backing
->bs
);
4080 if (src_offset
>= backing_length
) {
4081 cur_write_flags
|= BDRV_REQ_ZERO_WRITE
;
4083 child
= bs
->backing
;
4084 cur_bytes
= MIN(cur_bytes
, backing_length
- src_offset
);
4085 copy_offset
= src_offset
;
4088 cur_write_flags
|= BDRV_REQ_ZERO_WRITE
;
4092 case QCOW2_SUBCLUSTER_ZERO_PLAIN
:
4093 case QCOW2_SUBCLUSTER_ZERO_ALLOC
:
4094 cur_write_flags
|= BDRV_REQ_ZERO_WRITE
;
4097 case QCOW2_SUBCLUSTER_COMPRESSED
:
4101 case QCOW2_SUBCLUSTER_NORMAL
:
4102 child
= s
->data_file
;
4108 qemu_co_mutex_unlock(&s
->lock
);
4109 ret
= bdrv_co_copy_range_from(child
,
4112 cur_bytes
, read_flags
, cur_write_flags
);
4113 qemu_co_mutex_lock(&s
->lock
);
4119 src_offset
+= cur_bytes
;
4120 dst_offset
+= cur_bytes
;
4125 qemu_co_mutex_unlock(&s
->lock
);
4129 static int coroutine_fn
4130 qcow2_co_copy_range_to(BlockDriverState
*bs
,
4131 BdrvChild
*src
, int64_t src_offset
,
4132 BdrvChild
*dst
, int64_t dst_offset
,
4133 int64_t bytes
, BdrvRequestFlags read_flags
,
4134 BdrvRequestFlags write_flags
)
4136 BDRVQcow2State
*s
= bs
->opaque
;
4138 unsigned int cur_bytes
; /* number of sectors in current iteration */
4139 uint64_t host_offset
;
4140 QCowL2Meta
*l2meta
= NULL
;
4142 assert(!bs
->encrypted
);
4144 qemu_co_mutex_lock(&s
->lock
);
4146 while (bytes
!= 0) {
4150 cur_bytes
= MIN(bytes
, INT_MAX
);
4153 * If src->bs == dst->bs, we could simply copy by incrementing
4154 * the refcnt, without copying user data.
4155 * Or if src->bs == dst->bs->backing->bs, we could copy by discarding. */
4156 ret
= qcow2_alloc_host_offset(bs
, dst_offset
, &cur_bytes
,
4157 &host_offset
, &l2meta
);
4162 ret
= qcow2_pre_write_overlap_check(bs
, 0, host_offset
, cur_bytes
,
4168 qemu_co_mutex_unlock(&s
->lock
);
4169 ret
= bdrv_co_copy_range_to(src
, src_offset
, s
->data_file
, host_offset
,
4170 cur_bytes
, read_flags
, write_flags
);
4171 qemu_co_mutex_lock(&s
->lock
);
4176 ret
= qcow2_handle_l2meta(bs
, &l2meta
, true);
4182 src_offset
+= cur_bytes
;
4183 dst_offset
+= cur_bytes
;
4188 qcow2_handle_l2meta(bs
, &l2meta
, false);
4190 qemu_co_mutex_unlock(&s
->lock
);
4192 trace_qcow2_writev_done_req(qemu_coroutine_self(), ret
);
4197 static int coroutine_fn
qcow2_co_truncate(BlockDriverState
*bs
, int64_t offset
,
4198 bool exact
, PreallocMode prealloc
,
4199 BdrvRequestFlags flags
, Error
**errp
)
4201 BDRVQcow2State
*s
= bs
->opaque
;
4202 uint64_t old_length
;
4203 int64_t new_l1_size
;
4207 if (prealloc
!= PREALLOC_MODE_OFF
&& prealloc
!= PREALLOC_MODE_METADATA
&&
4208 prealloc
!= PREALLOC_MODE_FALLOC
&& prealloc
!= PREALLOC_MODE_FULL
)
4210 error_setg(errp
, "Unsupported preallocation mode '%s'",
4211 PreallocMode_str(prealloc
));
4215 if (!QEMU_IS_ALIGNED(offset
, BDRV_SECTOR_SIZE
)) {
4216 error_setg(errp
, "The new size must be a multiple of %u",
4217 (unsigned) BDRV_SECTOR_SIZE
);
4221 qemu_co_mutex_lock(&s
->lock
);
4224 * Even though we store snapshot size for all images, it was not
4225 * required until v3, so it is not safe to proceed for v2.
4227 if (s
->nb_snapshots
&& s
->qcow_version
< 3) {
4228 error_setg(errp
, "Can't resize a v2 image which has snapshots");
4233 /* See qcow2-bitmap.c for which bitmap scenarios prevent a resize. */
4234 if (qcow2_truncate_bitmaps_check(bs
, errp
)) {
4239 old_length
= bs
->total_sectors
* BDRV_SECTOR_SIZE
;
4240 new_l1_size
= size_to_l1(s
, offset
);
4242 if (offset
< old_length
) {
4243 int64_t last_cluster
, old_file_size
;
4244 if (prealloc
!= PREALLOC_MODE_OFF
) {
4246 "Preallocation can't be used for shrinking an image");
4251 ret
= qcow2_cluster_discard(bs
, ROUND_UP(offset
, s
->cluster_size
),
4252 old_length
- ROUND_UP(offset
,
4254 QCOW2_DISCARD_ALWAYS
, true);
4256 error_setg_errno(errp
, -ret
, "Failed to discard cropped clusters");
4260 ret
= qcow2_shrink_l1_table(bs
, new_l1_size
);
4262 error_setg_errno(errp
, -ret
,
4263 "Failed to reduce the number of L2 tables");
4267 ret
= qcow2_shrink_reftable(bs
);
4269 error_setg_errno(errp
, -ret
,
4270 "Failed to discard unused refblocks");
4274 old_file_size
= bdrv_getlength(bs
->file
->bs
);
4275 if (old_file_size
< 0) {
4276 error_setg_errno(errp
, -old_file_size
,
4277 "Failed to inquire current file length");
4278 ret
= old_file_size
;
4281 last_cluster
= qcow2_get_last_cluster(bs
, old_file_size
);
4282 if (last_cluster
< 0) {
4283 error_setg_errno(errp
, -last_cluster
,
4284 "Failed to find the last cluster");
4288 if ((last_cluster
+ 1) * s
->cluster_size
< old_file_size
) {
4289 Error
*local_err
= NULL
;
4292 * Do not pass @exact here: It will not help the user if
4293 * we get an error here just because they wanted to shrink
4294 * their qcow2 image (on a block device) with qemu-img.
4295 * (And on the qcow2 layer, the @exact requirement is
4296 * always fulfilled, so there is no need to pass it on.)
4298 bdrv_co_truncate(bs
->file
, (last_cluster
+ 1) * s
->cluster_size
,
4299 false, PREALLOC_MODE_OFF
, 0, &local_err
);
4301 warn_reportf_err(local_err
,
4302 "Failed to truncate the tail of the image: ");
4306 ret
= qcow2_grow_l1_table(bs
, new_l1_size
, true);
4308 error_setg_errno(errp
, -ret
, "Failed to grow the L1 table");
4312 if (data_file_is_raw(bs
) && prealloc
== PREALLOC_MODE_OFF
) {
4314 * When creating a qcow2 image with data-file-raw, we enforce
4315 * at least prealloc=metadata, so that the L1/L2 tables are
4316 * fully allocated and reading from the data file will return
4317 * the same data as reading from the qcow2 image. When the
4318 * image is grown, we must consequently preallocate the
4319 * metadata structures to cover the added area.
4321 prealloc
= PREALLOC_MODE_METADATA
;
4326 case PREALLOC_MODE_OFF
:
4327 if (has_data_file(bs
)) {
4329 * If the caller wants an exact resize, the external data
4330 * file should be resized to the exact target size, too,
4331 * so we pass @exact here.
4333 ret
= bdrv_co_truncate(s
->data_file
, offset
, exact
, prealloc
, 0,
4341 case PREALLOC_MODE_METADATA
:
4342 ret
= preallocate_co(bs
, old_length
, offset
, prealloc
, errp
);
4348 case PREALLOC_MODE_FALLOC
:
4349 case PREALLOC_MODE_FULL
:
4351 int64_t allocation_start
, host_offset
, guest_offset
;
4352 int64_t clusters_allocated
;
4353 int64_t old_file_size
, last_cluster
, new_file_size
;
4354 uint64_t nb_new_data_clusters
, nb_new_l2_tables
;
4355 bool subclusters_need_allocation
= false;
4357 /* With a data file, preallocation means just allocating the metadata
4358 * and forwarding the truncate request to the data file */
4359 if (has_data_file(bs
)) {
4360 ret
= preallocate_co(bs
, old_length
, offset
, prealloc
, errp
);
4367 old_file_size
= bdrv_getlength(bs
->file
->bs
);
4368 if (old_file_size
< 0) {
4369 error_setg_errno(errp
, -old_file_size
,
4370 "Failed to inquire current file length");
4371 ret
= old_file_size
;
4375 last_cluster
= qcow2_get_last_cluster(bs
, old_file_size
);
4376 if (last_cluster
>= 0) {
4377 old_file_size
= (last_cluster
+ 1) * s
->cluster_size
;
4379 old_file_size
= ROUND_UP(old_file_size
, s
->cluster_size
);
4382 nb_new_data_clusters
= (ROUND_UP(offset
, s
->cluster_size
) -
4383 start_of_cluster(s
, old_length
)) >> s
->cluster_bits
;
4385 /* This is an overestimation; we will not actually allocate space for
4386 * these in the file but just make sure the new refcount structures are
4387 * able to cover them so we will not have to allocate new refblocks
4388 * while entering the data blocks in the potentially new L2 tables.
4389 * (We do not actually care where the L2 tables are placed. Maybe they
4390 * are already allocated or they can be placed somewhere before
4391 * @old_file_size. It does not matter because they will be fully
4392 * allocated automatically, so they do not need to be covered by the
4393 * preallocation. All that matters is that we will not have to allocate
4394 * new refcount structures for them.) */
4395 nb_new_l2_tables
= DIV_ROUND_UP(nb_new_data_clusters
,
4396 s
->cluster_size
/ l2_entry_size(s
));
4397 /* The cluster range may not be aligned to L2 boundaries, so add one L2
4398 * table for a potential head/tail */
4401 allocation_start
= qcow2_refcount_area(bs
, old_file_size
,
4402 nb_new_data_clusters
+
4405 if (allocation_start
< 0) {
4406 error_setg_errno(errp
, -allocation_start
,
4407 "Failed to resize refcount structures");
4408 ret
= allocation_start
;
4412 clusters_allocated
= qcow2_alloc_clusters_at(bs
, allocation_start
,
4413 nb_new_data_clusters
);
4414 if (clusters_allocated
< 0) {
4415 error_setg_errno(errp
, -clusters_allocated
,
4416 "Failed to allocate data clusters");
4417 ret
= clusters_allocated
;
4421 assert(clusters_allocated
== nb_new_data_clusters
);
4423 /* Allocate the data area */
4424 new_file_size
= allocation_start
+
4425 nb_new_data_clusters
* s
->cluster_size
;
4427 * Image file grows, so @exact does not matter.
4429 * If we need to zero out the new area, try first whether the protocol
4430 * driver can already take care of this.
4432 if (flags
& BDRV_REQ_ZERO_WRITE
) {
4433 ret
= bdrv_co_truncate(bs
->file
, new_file_size
, false, prealloc
,
4434 BDRV_REQ_ZERO_WRITE
, NULL
);
4436 flags
&= ~BDRV_REQ_ZERO_WRITE
;
4437 /* Ensure that we read zeroes and not backing file data */
4438 subclusters_need_allocation
= true;
4444 ret
= bdrv_co_truncate(bs
->file
, new_file_size
, false, prealloc
, 0,
4448 error_prepend(errp
, "Failed to resize underlying file: ");
4449 qcow2_free_clusters(bs
, allocation_start
,
4450 nb_new_data_clusters
* s
->cluster_size
,
4451 QCOW2_DISCARD_OTHER
);
4455 /* Create the necessary L2 entries */
4456 host_offset
= allocation_start
;
4457 guest_offset
= old_length
;
4458 while (nb_new_data_clusters
) {
4459 int64_t nb_clusters
= MIN(
4460 nb_new_data_clusters
,
4461 s
->l2_slice_size
- offset_to_l2_slice_index(s
, guest_offset
));
4462 unsigned cow_start_length
= offset_into_cluster(s
, guest_offset
);
4463 QCowL2Meta allocation
;
4464 guest_offset
= start_of_cluster(s
, guest_offset
);
4465 allocation
= (QCowL2Meta
) {
4466 .offset
= guest_offset
,
4467 .alloc_offset
= host_offset
,
4468 .nb_clusters
= nb_clusters
,
4471 .nb_bytes
= cow_start_length
,
4474 .offset
= nb_clusters
<< s
->cluster_bits
,
4477 .prealloc
= !subclusters_need_allocation
,
4479 qemu_co_queue_init(&allocation
.dependent_requests
);
4481 ret
= qcow2_alloc_cluster_link_l2(bs
, &allocation
);
4483 error_setg_errno(errp
, -ret
, "Failed to update L2 tables");
4484 qcow2_free_clusters(bs
, host_offset
,
4485 nb_new_data_clusters
* s
->cluster_size
,
4486 QCOW2_DISCARD_OTHER
);
4490 guest_offset
+= nb_clusters
* s
->cluster_size
;
4491 host_offset
+= nb_clusters
* s
->cluster_size
;
4492 nb_new_data_clusters
-= nb_clusters
;
4498 g_assert_not_reached();
4501 if ((flags
& BDRV_REQ_ZERO_WRITE
) && offset
> old_length
) {
4502 uint64_t zero_start
= QEMU_ALIGN_UP(old_length
, s
->subcluster_size
);
4505 * Use zero clusters as much as we can. qcow2_subcluster_zeroize()
4506 * requires a subcluster-aligned start. The end may be unaligned if
4507 * it is at the end of the image (which it is here).
4509 if (offset
> zero_start
) {
4510 ret
= qcow2_subcluster_zeroize(bs
, zero_start
, offset
- zero_start
,
4513 error_setg_errno(errp
, -ret
, "Failed to zero out new clusters");
4518 /* Write explicit zeros for the unaligned head */
4519 if (zero_start
> old_length
) {
4520 uint64_t len
= MIN(zero_start
, offset
) - old_length
;
4521 uint8_t *buf
= qemu_blockalign0(bs
, len
);
4523 qemu_iovec_init_buf(&qiov
, buf
, len
);
4525 qemu_co_mutex_unlock(&s
->lock
);
4526 ret
= qcow2_co_pwritev_part(bs
, old_length
, len
, &qiov
, 0, 0);
4527 qemu_co_mutex_lock(&s
->lock
);
4531 error_setg_errno(errp
, -ret
, "Failed to zero out the new area");
4537 if (prealloc
!= PREALLOC_MODE_OFF
) {
4538 /* Flush metadata before actually changing the image size */
4539 ret
= qcow2_write_caches(bs
);
4541 error_setg_errno(errp
, -ret
,
4542 "Failed to flush the preallocated area to disk");
4547 bs
->total_sectors
= offset
/ BDRV_SECTOR_SIZE
;
4549 /* write updated header.size */
4550 offset
= cpu_to_be64(offset
);
4551 ret
= bdrv_co_pwrite_sync(bs
->file
, offsetof(QCowHeader
, size
),
4552 sizeof(offset
), &offset
, 0);
4554 error_setg_errno(errp
, -ret
, "Failed to update the image size");
4558 s
->l1_vm_state_index
= new_l1_size
;
4560 /* Update cache sizes */
4561 options
= qdict_clone_shallow(bs
->options
);
4562 ret
= qcow2_update_options(bs
, options
, s
->flags
, errp
);
4563 qobject_unref(options
);
4569 qemu_co_mutex_unlock(&s
->lock
);
4573 static coroutine_fn
int
4574 qcow2_co_pwritev_compressed_task(BlockDriverState
*bs
,
4575 uint64_t offset
, uint64_t bytes
,
4576 QEMUIOVector
*qiov
, size_t qiov_offset
)
4578 BDRVQcow2State
*s
= bs
->opaque
;
4581 uint8_t *buf
, *out_buf
;
4582 uint64_t cluster_offset
;
4584 assert(bytes
== s
->cluster_size
|| (bytes
< s
->cluster_size
&&
4585 (offset
+ bytes
== bs
->total_sectors
<< BDRV_SECTOR_BITS
)));
4587 buf
= qemu_blockalign(bs
, s
->cluster_size
);
4588 if (bytes
< s
->cluster_size
) {
4589 /* Zero-pad last write if image size is not cluster aligned */
4590 memset(buf
+ bytes
, 0, s
->cluster_size
- bytes
);
4592 qemu_iovec_to_buf(qiov
, qiov_offset
, buf
, bytes
);
4594 out_buf
= g_malloc(s
->cluster_size
);
4596 out_len
= qcow2_co_compress(bs
, out_buf
, s
->cluster_size
- 1,
4597 buf
, s
->cluster_size
);
4598 if (out_len
== -ENOMEM
) {
4599 /* could not compress: write normal cluster */
4600 ret
= qcow2_co_pwritev_part(bs
, offset
, bytes
, qiov
, qiov_offset
, 0);
4605 } else if (out_len
< 0) {
4610 qemu_co_mutex_lock(&s
->lock
);
4611 ret
= qcow2_alloc_compressed_cluster_offset(bs
, offset
, out_len
,
4614 qemu_co_mutex_unlock(&s
->lock
);
4618 ret
= qcow2_pre_write_overlap_check(bs
, 0, cluster_offset
, out_len
, true);
4619 qemu_co_mutex_unlock(&s
->lock
);
4624 BLKDBG_EVENT(s
->data_file
, BLKDBG_WRITE_COMPRESSED
);
4625 ret
= bdrv_co_pwrite(s
->data_file
, cluster_offset
, out_len
, out_buf
, 0);
4637 static coroutine_fn
int qcow2_co_pwritev_compressed_task_entry(AioTask
*task
)
4639 Qcow2AioTask
*t
= container_of(task
, Qcow2AioTask
, task
);
4641 assert(!t
->subcluster_type
&& !t
->l2meta
);
4643 return qcow2_co_pwritev_compressed_task(t
->bs
, t
->offset
, t
->bytes
, t
->qiov
,
4648 * XXX: put compressed sectors first, then all the cluster aligned
4649 * tables to avoid losing bytes in alignment
4651 static coroutine_fn
int
4652 qcow2_co_pwritev_compressed_part(BlockDriverState
*bs
,
4653 int64_t offset
, int64_t bytes
,
4654 QEMUIOVector
*qiov
, size_t qiov_offset
)
4656 BDRVQcow2State
*s
= bs
->opaque
;
4657 AioTaskPool
*aio
= NULL
;
4660 if (has_data_file(bs
)) {
4666 * align end of file to a sector boundary to ease reading with
4669 int64_t len
= bdrv_getlength(bs
->file
->bs
);
4673 return bdrv_co_truncate(bs
->file
, len
, false, PREALLOC_MODE_OFF
, 0,
4677 if (offset_into_cluster(s
, offset
)) {
4681 if (offset_into_cluster(s
, bytes
) &&
4682 (offset
+ bytes
) != (bs
->total_sectors
<< BDRV_SECTOR_BITS
)) {
4686 while (bytes
&& aio_task_pool_status(aio
) == 0) {
4687 uint64_t chunk_size
= MIN(bytes
, s
->cluster_size
);
4689 if (!aio
&& chunk_size
!= bytes
) {
4690 aio
= aio_task_pool_new(QCOW2_MAX_WORKERS
);
4693 ret
= qcow2_add_task(bs
, aio
, qcow2_co_pwritev_compressed_task_entry
,
4694 0, 0, offset
, chunk_size
, qiov
, qiov_offset
, NULL
);
4698 qiov_offset
+= chunk_size
;
4699 offset
+= chunk_size
;
4700 bytes
-= chunk_size
;
4704 aio_task_pool_wait_all(aio
);
4706 ret
= aio_task_pool_status(aio
);
4714 static int coroutine_fn
4715 qcow2_co_preadv_compressed(BlockDriverState
*bs
,
4722 BDRVQcow2State
*s
= bs
->opaque
;
4725 uint8_t *buf
, *out_buf
;
4726 int offset_in_cluster
= offset_into_cluster(s
, offset
);
4728 qcow2_parse_compressed_l2_entry(bs
, l2_entry
, &coffset
, &csize
);
4730 buf
= g_try_malloc(csize
);
4735 out_buf
= qemu_blockalign(bs
, s
->cluster_size
);
4737 BLKDBG_EVENT(bs
->file
, BLKDBG_READ_COMPRESSED
);
4738 ret
= bdrv_co_pread(bs
->file
, coffset
, csize
, buf
, 0);
4743 if (qcow2_co_decompress(bs
, out_buf
, s
->cluster_size
, buf
, csize
) < 0) {
4748 qemu_iovec_from_buf(qiov
, qiov_offset
, out_buf
+ offset_in_cluster
, bytes
);
4751 qemu_vfree(out_buf
);
4757 static int make_completely_empty(BlockDriverState
*bs
)
4759 BDRVQcow2State
*s
= bs
->opaque
;
4760 Error
*local_err
= NULL
;
4761 int ret
, l1_clusters
;
4763 uint64_t *new_reftable
= NULL
;
4764 uint64_t rt_entry
, l1_size2
;
4767 uint64_t reftable_offset
;
4768 uint32_t reftable_clusters
;
4769 } QEMU_PACKED l1_ofs_rt_ofs_cls
;
4771 ret
= qcow2_cache_empty(bs
, s
->l2_table_cache
);
4776 ret
= qcow2_cache_empty(bs
, s
->refcount_block_cache
);
4781 /* Refcounts will be broken utterly */
4782 ret
= qcow2_mark_dirty(bs
);
4787 BLKDBG_EVENT(bs
->file
, BLKDBG_L1_UPDATE
);
4789 l1_clusters
= DIV_ROUND_UP(s
->l1_size
, s
->cluster_size
/ L1E_SIZE
);
4790 l1_size2
= (uint64_t)s
->l1_size
* L1E_SIZE
;
4792 /* After this call, neither the in-memory nor the on-disk refcount
4793 * information accurately describe the actual references */
4795 ret
= bdrv_pwrite_zeroes(bs
->file
, s
->l1_table_offset
,
4796 l1_clusters
* s
->cluster_size
, 0);
4798 goto fail_broken_refcounts
;
4800 memset(s
->l1_table
, 0, l1_size2
);
4802 BLKDBG_EVENT(bs
->file
, BLKDBG_EMPTY_IMAGE_PREPARE
);
4804 /* Overwrite enough clusters at the beginning of the sectors to place
4805 * the refcount table, a refcount block and the L1 table in; this may
4806 * overwrite parts of the existing refcount and L1 table, which is not
4807 * an issue because the dirty flag is set, complete data loss is in fact
4808 * desired and partial data loss is consequently fine as well */
4809 ret
= bdrv_pwrite_zeroes(bs
->file
, s
->cluster_size
,
4810 (2 + l1_clusters
) * s
->cluster_size
, 0);
4811 /* This call (even if it failed overall) may have overwritten on-disk
4812 * refcount structures; in that case, the in-memory refcount information
4813 * will probably differ from the on-disk information which makes the BDS
4816 goto fail_broken_refcounts
;
4819 BLKDBG_EVENT(bs
->file
, BLKDBG_L1_UPDATE
);
4820 BLKDBG_EVENT(bs
->file
, BLKDBG_REFTABLE_UPDATE
);
4822 /* "Create" an empty reftable (one cluster) directly after the image
4823 * header and an empty L1 table three clusters after the image header;
4824 * the cluster between those two will be used as the first refblock */
4825 l1_ofs_rt_ofs_cls
.l1_offset
= cpu_to_be64(3 * s
->cluster_size
);
4826 l1_ofs_rt_ofs_cls
.reftable_offset
= cpu_to_be64(s
->cluster_size
);
4827 l1_ofs_rt_ofs_cls
.reftable_clusters
= cpu_to_be32(1);
4828 ret
= bdrv_pwrite_sync(bs
->file
, offsetof(QCowHeader
, l1_table_offset
),
4829 sizeof(l1_ofs_rt_ofs_cls
), &l1_ofs_rt_ofs_cls
, 0);
4831 goto fail_broken_refcounts
;
4834 s
->l1_table_offset
= 3 * s
->cluster_size
;
4836 new_reftable
= g_try_new0(uint64_t, s
->cluster_size
/ REFTABLE_ENTRY_SIZE
);
4837 if (!new_reftable
) {
4839 goto fail_broken_refcounts
;
4842 s
->refcount_table_offset
= s
->cluster_size
;
4843 s
->refcount_table_size
= s
->cluster_size
/ REFTABLE_ENTRY_SIZE
;
4844 s
->max_refcount_table_index
= 0;
4846 g_free(s
->refcount_table
);
4847 s
->refcount_table
= new_reftable
;
4848 new_reftable
= NULL
;
4850 /* Now the in-memory refcount information again corresponds to the on-disk
4851 * information (reftable is empty and no refblocks (the refblock cache is
4852 * empty)); however, this means some clusters (e.g. the image header) are
4853 * referenced, but not refcounted, but the normal qcow2 code assumes that
4854 * the in-memory information is always correct */
4856 BLKDBG_EVENT(bs
->file
, BLKDBG_REFBLOCK_ALLOC
);
4858 /* Enter the first refblock into the reftable */
4859 rt_entry
= cpu_to_be64(2 * s
->cluster_size
);
4860 ret
= bdrv_pwrite_sync(bs
->file
, s
->cluster_size
, sizeof(rt_entry
),
4863 goto fail_broken_refcounts
;
4865 s
->refcount_table
[0] = 2 * s
->cluster_size
;
4867 s
->free_cluster_index
= 0;
4868 assert(3 + l1_clusters
<= s
->refcount_block_size
);
4869 offset
= qcow2_alloc_clusters(bs
, 3 * s
->cluster_size
+ l1_size2
);
4872 goto fail_broken_refcounts
;
4873 } else if (offset
> 0) {
4874 error_report("First cluster in emptied image is in use");
4878 /* Now finally the in-memory information corresponds to the on-disk
4879 * structures and is correct */
4880 ret
= qcow2_mark_clean(bs
);
4885 ret
= bdrv_truncate(bs
->file
, (3 + l1_clusters
) * s
->cluster_size
, false,
4886 PREALLOC_MODE_OFF
, 0, &local_err
);
4888 error_report_err(local_err
);
4894 fail_broken_refcounts
:
4895 /* The BDS is unusable at this point. If we wanted to make it usable, we
4896 * would have to call qcow2_refcount_close(), qcow2_refcount_init(),
4897 * qcow2_check_refcounts(), qcow2_refcount_close() and qcow2_refcount_init()
4898 * again. However, because the functions which could have caused this error
4899 * path to be taken are used by those functions as well, it's very likely
4900 * that that sequence will fail as well. Therefore, just eject the BDS. */
4904 g_free(new_reftable
);
4908 static int qcow2_make_empty(BlockDriverState
*bs
)
4910 BDRVQcow2State
*s
= bs
->opaque
;
4911 uint64_t offset
, end_offset
;
4912 int step
= QEMU_ALIGN_DOWN(INT_MAX
, s
->cluster_size
);
4913 int l1_clusters
, ret
= 0;
4915 l1_clusters
= DIV_ROUND_UP(s
->l1_size
, s
->cluster_size
/ L1E_SIZE
);
4917 if (s
->qcow_version
>= 3 && !s
->snapshots
&& !s
->nb_bitmaps
&&
4918 3 + l1_clusters
<= s
->refcount_block_size
&&
4919 s
->crypt_method_header
!= QCOW_CRYPT_LUKS
&&
4920 !has_data_file(bs
)) {
4921 /* The following function only works for qcow2 v3 images (it
4922 * requires the dirty flag) and only as long as there are no
4923 * features that reserve extra clusters (such as snapshots,
4924 * LUKS header, or persistent bitmaps), because it completely
4925 * empties the image. Furthermore, the L1 table and three
4926 * additional clusters (image header, refcount table, one
4927 * refcount block) have to fit inside one refcount block. It
4928 * only resets the image file, i.e. does not work with an
4929 * external data file. */
4930 return make_completely_empty(bs
);
4933 /* This fallback code simply discards every active cluster; this is slow,
4934 * but works in all cases */
4935 end_offset
= bs
->total_sectors
* BDRV_SECTOR_SIZE
;
4936 for (offset
= 0; offset
< end_offset
; offset
+= step
) {
4937 /* As this function is generally used after committing an external
4938 * snapshot, QCOW2_DISCARD_SNAPSHOT seems appropriate. Also, the
4939 * default action for this kind of discard is to pass the discard,
4940 * which will ideally result in an actually smaller image file, as
4941 * is probably desired. */
4942 ret
= qcow2_cluster_discard(bs
, offset
, MIN(step
, end_offset
- offset
),
4943 QCOW2_DISCARD_SNAPSHOT
, true);
4952 static coroutine_fn
int qcow2_co_flush_to_os(BlockDriverState
*bs
)
4954 BDRVQcow2State
*s
= bs
->opaque
;
4957 qemu_co_mutex_lock(&s
->lock
);
4958 ret
= qcow2_write_caches(bs
);
4959 qemu_co_mutex_unlock(&s
->lock
);
4964 static BlockMeasureInfo
*qcow2_measure(QemuOpts
*opts
, BlockDriverState
*in_bs
,
4967 Error
*local_err
= NULL
;
4968 BlockMeasureInfo
*info
;
4969 uint64_t required
= 0; /* bytes that contribute to required size */
4970 uint64_t virtual_size
; /* disk size as seen by guest */
4971 uint64_t refcount_bits
;
4973 uint64_t luks_payload_size
= 0;
4974 size_t cluster_size
;
4977 PreallocMode prealloc
;
4978 bool has_backing_file
;
4983 /* Parse image creation options */
4984 extended_l2
= qemu_opt_get_bool_del(opts
, BLOCK_OPT_EXTL2
, false);
4986 cluster_size
= qcow2_opt_get_cluster_size_del(opts
, extended_l2
,
4992 version
= qcow2_opt_get_version_del(opts
, &local_err
);
4997 refcount_bits
= qcow2_opt_get_refcount_bits_del(opts
, version
, &local_err
);
5002 optstr
= qemu_opt_get_del(opts
, BLOCK_OPT_PREALLOC
);
5003 prealloc
= qapi_enum_parse(&PreallocMode_lookup
, optstr
,
5004 PREALLOC_MODE_OFF
, &local_err
);
5010 optstr
= qemu_opt_get_del(opts
, BLOCK_OPT_BACKING_FILE
);
5011 has_backing_file
= !!optstr
;
5014 optstr
= qemu_opt_get_del(opts
, BLOCK_OPT_ENCRYPT_FORMAT
);
5015 has_luks
= optstr
&& strcmp(optstr
, "luks") == 0;
5019 g_autoptr(QCryptoBlockCreateOptions
) create_opts
= NULL
;
5020 QDict
*cryptoopts
= qcow2_extract_crypto_opts(opts
, "luks", errp
);
5023 create_opts
= block_crypto_create_opts_init(cryptoopts
, errp
);
5024 qobject_unref(cryptoopts
);
5029 if (!qcrypto_block_calculate_payload_offset(create_opts
,
5036 luks_payload_size
= ROUND_UP(headerlen
, cluster_size
);
5039 virtual_size
= qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0);
5040 virtual_size
= ROUND_UP(virtual_size
, cluster_size
);
5042 /* Check that virtual disk size is valid */
5043 l2e_size
= extended_l2
? L2E_SIZE_EXTENDED
: L2E_SIZE_NORMAL
;
5044 l2_tables
= DIV_ROUND_UP(virtual_size
/ cluster_size
,
5045 cluster_size
/ l2e_size
);
5046 if (l2_tables
* L1E_SIZE
> QCOW_MAX_L1_SIZE
) {
5047 error_setg(&local_err
, "The image size is too large "
5048 "(try using a larger cluster size)");
5052 /* Account for input image */
5054 int64_t ssize
= bdrv_getlength(in_bs
);
5056 error_setg_errno(&local_err
, -ssize
,
5057 "Unable to get image virtual_size");
5061 virtual_size
= ROUND_UP(ssize
, cluster_size
);
5063 if (has_backing_file
) {
5064 /* We don't how much of the backing chain is shared by the input
5065 * image and the new image file. In the worst case the new image's
5066 * backing file has nothing in common with the input image. Be
5067 * conservative and assume all clusters need to be written.
5069 required
= virtual_size
;
5074 for (offset
= 0; offset
< ssize
; offset
+= pnum
) {
5077 ret
= bdrv_block_status_above(in_bs
, NULL
, offset
,
5078 ssize
- offset
, &pnum
, NULL
,
5081 error_setg_errno(&local_err
, -ret
,
5082 "Unable to get block status");
5086 if (ret
& BDRV_BLOCK_ZERO
) {
5087 /* Skip zero regions (safe with no backing file) */
5088 } else if ((ret
& (BDRV_BLOCK_DATA
| BDRV_BLOCK_ALLOCATED
)) ==
5089 (BDRV_BLOCK_DATA
| BDRV_BLOCK_ALLOCATED
)) {
5090 /* Extend pnum to end of cluster for next iteration */
5091 pnum
= ROUND_UP(offset
+ pnum
, cluster_size
) - offset
;
5093 /* Count clusters we've seen */
5094 required
+= offset
% cluster_size
+ pnum
;
5100 /* Take into account preallocation. Nothing special is needed for
5101 * PREALLOC_MODE_METADATA since metadata is always counted.
5103 if (prealloc
== PREALLOC_MODE_FULL
|| prealloc
== PREALLOC_MODE_FALLOC
) {
5104 required
= virtual_size
;
5107 info
= g_new0(BlockMeasureInfo
, 1);
5108 info
->fully_allocated
= luks_payload_size
+
5109 qcow2_calc_prealloc_size(virtual_size
, cluster_size
,
5110 ctz32(refcount_bits
), extended_l2
);
5113 * Remove data clusters that are not required. This overestimates the
5114 * required size because metadata needed for the fully allocated file is
5115 * still counted. Show bitmaps only if both source and destination
5116 * would support them.
5118 info
->required
= info
->fully_allocated
- virtual_size
+ required
;
5119 info
->has_bitmaps
= version
>= 3 && in_bs
&&
5120 bdrv_supports_persistent_dirty_bitmap(in_bs
);
5121 if (info
->has_bitmaps
) {
5122 info
->bitmaps
= qcow2_get_persistent_dirty_bitmap_size(in_bs
,
5128 error_propagate(errp
, local_err
);
5132 static int qcow2_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
5134 BDRVQcow2State
*s
= bs
->opaque
;
5135 bdi
->cluster_size
= s
->cluster_size
;
5136 bdi
->vm_state_offset
= qcow2_vm_state_offset(s
);
5137 bdi
->is_dirty
= s
->incompatible_features
& QCOW2_INCOMPAT_DIRTY
;
5141 static ImageInfoSpecific
*qcow2_get_specific_info(BlockDriverState
*bs
,
5144 BDRVQcow2State
*s
= bs
->opaque
;
5145 ImageInfoSpecific
*spec_info
;
5146 QCryptoBlockInfo
*encrypt_info
= NULL
;
5148 if (s
->crypto
!= NULL
) {
5149 encrypt_info
= qcrypto_block_get_info(s
->crypto
, errp
);
5150 if (!encrypt_info
) {
5155 spec_info
= g_new(ImageInfoSpecific
, 1);
5156 *spec_info
= (ImageInfoSpecific
){
5157 .type
= IMAGE_INFO_SPECIFIC_KIND_QCOW2
,
5158 .u
.qcow2
.data
= g_new0(ImageInfoSpecificQCow2
, 1),
5160 if (s
->qcow_version
== 2) {
5161 *spec_info
->u
.qcow2
.data
= (ImageInfoSpecificQCow2
){
5162 .compat
= g_strdup("0.10"),
5163 .refcount_bits
= s
->refcount_bits
,
5165 } else if (s
->qcow_version
== 3) {
5166 Qcow2BitmapInfoList
*bitmaps
;
5167 if (!qcow2_get_bitmap_info_list(bs
, &bitmaps
, errp
)) {
5168 qapi_free_ImageInfoSpecific(spec_info
);
5169 qapi_free_QCryptoBlockInfo(encrypt_info
);
5172 *spec_info
->u
.qcow2
.data
= (ImageInfoSpecificQCow2
){
5173 .compat
= g_strdup("1.1"),
5174 .lazy_refcounts
= s
->compatible_features
&
5175 QCOW2_COMPAT_LAZY_REFCOUNTS
,
5176 .has_lazy_refcounts
= true,
5177 .corrupt
= s
->incompatible_features
&
5178 QCOW2_INCOMPAT_CORRUPT
,
5179 .has_corrupt
= true,
5180 .has_extended_l2
= true,
5181 .extended_l2
= has_subclusters(s
),
5182 .refcount_bits
= s
->refcount_bits
,
5183 .has_bitmaps
= !!bitmaps
,
5185 .has_data_file
= !!s
->image_data_file
,
5186 .data_file
= g_strdup(s
->image_data_file
),
5187 .has_data_file_raw
= has_data_file(bs
),
5188 .data_file_raw
= data_file_is_raw(bs
),
5189 .compression_type
= s
->compression_type
,
5192 /* if this assertion fails, this probably means a new version was
5193 * added without having it covered here */
5198 ImageInfoSpecificQCow2Encryption
*qencrypt
=
5199 g_new(ImageInfoSpecificQCow2Encryption
, 1);
5200 switch (encrypt_info
->format
) {
5201 case Q_CRYPTO_BLOCK_FORMAT_QCOW
:
5202 qencrypt
->format
= BLOCKDEV_QCOW2_ENCRYPTION_FORMAT_AES
;
5204 case Q_CRYPTO_BLOCK_FORMAT_LUKS
:
5205 qencrypt
->format
= BLOCKDEV_QCOW2_ENCRYPTION_FORMAT_LUKS
;
5206 qencrypt
->u
.luks
= encrypt_info
->u
.luks
;
5211 /* Since we did shallow copy above, erase any pointers
5212 * in the original info */
5213 memset(&encrypt_info
->u
, 0, sizeof(encrypt_info
->u
));
5214 qapi_free_QCryptoBlockInfo(encrypt_info
);
5216 spec_info
->u
.qcow2
.data
->has_encrypt
= true;
5217 spec_info
->u
.qcow2
.data
->encrypt
= qencrypt
;
5223 static int qcow2_has_zero_init(BlockDriverState
*bs
)
5225 BDRVQcow2State
*s
= bs
->opaque
;
5228 if (qemu_in_coroutine()) {
5229 qemu_co_mutex_lock(&s
->lock
);
5232 * Check preallocation status: Preallocated images have all L2
5233 * tables allocated, nonpreallocated images have none. It is
5234 * therefore enough to check the first one.
5236 preallocated
= s
->l1_size
> 0 && s
->l1_table
[0] != 0;
5237 if (qemu_in_coroutine()) {
5238 qemu_co_mutex_unlock(&s
->lock
);
5241 if (!preallocated
) {
5243 } else if (bs
->encrypted
) {
5246 return bdrv_has_zero_init(s
->data_file
->bs
);
5251 * Check the request to vmstate. On success return
5252 * qcow2_vm_state_offset(bs) + @pos
5254 static int64_t qcow2_check_vmstate_request(BlockDriverState
*bs
,
5255 QEMUIOVector
*qiov
, int64_t pos
)
5257 BDRVQcow2State
*s
= bs
->opaque
;
5258 int64_t vmstate_offset
= qcow2_vm_state_offset(s
);
5261 /* Incoming requests must be OK */
5262 bdrv_check_qiov_request(pos
, qiov
->size
, qiov
, 0, &error_abort
);
5264 if (INT64_MAX
- pos
< vmstate_offset
) {
5268 pos
+= vmstate_offset
;
5269 ret
= bdrv_check_qiov_request(pos
, qiov
->size
, qiov
, 0, NULL
);
5277 static int qcow2_save_vmstate(BlockDriverState
*bs
, QEMUIOVector
*qiov
,
5280 int64_t offset
= qcow2_check_vmstate_request(bs
, qiov
, pos
);
5285 BLKDBG_EVENT(bs
->file
, BLKDBG_VMSTATE_SAVE
);
5286 return bs
->drv
->bdrv_co_pwritev_part(bs
, offset
, qiov
->size
, qiov
, 0, 0);
5289 static int qcow2_load_vmstate(BlockDriverState
*bs
, QEMUIOVector
*qiov
,
5292 int64_t offset
= qcow2_check_vmstate_request(bs
, qiov
, pos
);
5297 BLKDBG_EVENT(bs
->file
, BLKDBG_VMSTATE_LOAD
);
5298 return bs
->drv
->bdrv_co_preadv_part(bs
, offset
, qiov
->size
, qiov
, 0, 0);
5301 static int qcow2_has_compressed_clusters(BlockDriverState
*bs
)
5304 int64_t bytes
= bdrv_getlength(bs
);
5310 while (bytes
!= 0) {
5312 QCow2SubclusterType type
;
5313 unsigned int cur_bytes
= MIN(INT_MAX
, bytes
);
5314 uint64_t host_offset
;
5316 ret
= qcow2_get_host_offset(bs
, offset
, &cur_bytes
, &host_offset
,
5322 if (type
== QCOW2_SUBCLUSTER_COMPRESSED
) {
5326 offset
+= cur_bytes
;
5334 * Downgrades an image's version. To achieve this, any incompatible features
5335 * have to be removed.
5337 static int qcow2_downgrade(BlockDriverState
*bs
, int target_version
,
5338 BlockDriverAmendStatusCB
*status_cb
, void *cb_opaque
,
5341 BDRVQcow2State
*s
= bs
->opaque
;
5342 int current_version
= s
->qcow_version
;
5346 /* This is qcow2_downgrade(), not qcow2_upgrade() */
5347 assert(target_version
< current_version
);
5349 /* There are no other versions (now) that you can downgrade to */
5350 assert(target_version
== 2);
5352 if (s
->refcount_order
!= 4) {
5353 error_setg(errp
, "compat=0.10 requires refcount_bits=16");
5357 if (has_data_file(bs
)) {
5358 error_setg(errp
, "Cannot downgrade an image with a data file");
5363 * If any internal snapshot has a different size than the current
5364 * image size, or VM state size that exceeds 32 bits, downgrading
5365 * is unsafe. Even though we would still use v3-compliant output
5366 * to preserve that data, other v2 programs might not realize
5367 * those optional fields are important.
5369 for (i
= 0; i
< s
->nb_snapshots
; i
++) {
5370 if (s
->snapshots
[i
].vm_state_size
> UINT32_MAX
||
5371 s
->snapshots
[i
].disk_size
!= bs
->total_sectors
* BDRV_SECTOR_SIZE
) {
5372 error_setg(errp
, "Internal snapshots prevent downgrade of image");
5377 /* clear incompatible features */
5378 if (s
->incompatible_features
& QCOW2_INCOMPAT_DIRTY
) {
5379 ret
= qcow2_mark_clean(bs
);
5381 error_setg_errno(errp
, -ret
, "Failed to make the image clean");
5386 /* with QCOW2_INCOMPAT_CORRUPT, it is pretty much impossible to get here in
5387 * the first place; if that happens nonetheless, returning -ENOTSUP is the
5388 * best thing to do anyway */
5390 if (s
->incompatible_features
& ~QCOW2_INCOMPAT_COMPRESSION
) {
5391 error_setg(errp
, "Cannot downgrade an image with incompatible features "
5392 "0x%" PRIx64
" set",
5393 s
->incompatible_features
& ~QCOW2_INCOMPAT_COMPRESSION
);
5397 /* since we can ignore compatible features, we can set them to 0 as well */
5398 s
->compatible_features
= 0;
5399 /* if lazy refcounts have been used, they have already been fixed through
5400 * clearing the dirty flag */
5402 /* clearing autoclear features is trivial */
5403 s
->autoclear_features
= 0;
5405 ret
= qcow2_expand_zero_clusters(bs
, status_cb
, cb_opaque
);
5407 error_setg_errno(errp
, -ret
, "Failed to turn zero into data clusters");
5411 if (s
->incompatible_features
& QCOW2_INCOMPAT_COMPRESSION
) {
5412 ret
= qcow2_has_compressed_clusters(bs
);
5414 error_setg(errp
, "Failed to check block status");
5418 error_setg(errp
, "Cannot downgrade an image with zstd compression "
5419 "type and existing compressed clusters");
5423 * No compressed clusters for now, so just chose default zlib
5426 s
->incompatible_features
&= ~QCOW2_INCOMPAT_COMPRESSION
;
5427 s
->compression_type
= QCOW2_COMPRESSION_TYPE_ZLIB
;
5430 assert(s
->incompatible_features
== 0);
5432 s
->qcow_version
= target_version
;
5433 ret
= qcow2_update_header(bs
);
5435 s
->qcow_version
= current_version
;
5436 error_setg_errno(errp
, -ret
, "Failed to update the image header");
5443 * Upgrades an image's version. While newer versions encompass all
5444 * features of older versions, some things may have to be presented
5447 static int qcow2_upgrade(BlockDriverState
*bs
, int target_version
,
5448 BlockDriverAmendStatusCB
*status_cb
, void *cb_opaque
,
5451 BDRVQcow2State
*s
= bs
->opaque
;
5452 bool need_snapshot_update
;
5453 int current_version
= s
->qcow_version
;
5457 /* This is qcow2_upgrade(), not qcow2_downgrade() */
5458 assert(target_version
> current_version
);
5460 /* There are no other versions (yet) that you can upgrade to */
5461 assert(target_version
== 3);
5463 status_cb(bs
, 0, 2, cb_opaque
);
5466 * In v2, snapshots do not need to have extra data. v3 requires
5467 * the 64-bit VM state size and the virtual disk size to be
5469 * qcow2_write_snapshots() will always write the list in the
5470 * v3-compliant format.
5472 need_snapshot_update
= false;
5473 for (i
= 0; i
< s
->nb_snapshots
; i
++) {
5474 if (s
->snapshots
[i
].extra_data_size
<
5475 sizeof_field(QCowSnapshotExtraData
, vm_state_size_large
) +
5476 sizeof_field(QCowSnapshotExtraData
, disk_size
))
5478 need_snapshot_update
= true;
5482 if (need_snapshot_update
) {
5483 ret
= qcow2_write_snapshots(bs
);
5485 error_setg_errno(errp
, -ret
, "Failed to update the snapshot table");
5489 status_cb(bs
, 1, 2, cb_opaque
);
5491 s
->qcow_version
= target_version
;
5492 ret
= qcow2_update_header(bs
);
5494 s
->qcow_version
= current_version
;
5495 error_setg_errno(errp
, -ret
, "Failed to update the image header");
5498 status_cb(bs
, 2, 2, cb_opaque
);
5503 typedef enum Qcow2AmendOperation
{
5504 /* This is the value Qcow2AmendHelperCBInfo::last_operation will be
5505 * statically initialized to so that the helper CB can discern the first
5506 * invocation from an operation change */
5507 QCOW2_NO_OPERATION
= 0,
5510 QCOW2_UPDATING_ENCRYPTION
,
5511 QCOW2_CHANGING_REFCOUNT_ORDER
,
5513 } Qcow2AmendOperation
;
5515 typedef struct Qcow2AmendHelperCBInfo
{
5516 /* The code coordinating the amend operations should only modify
5517 * these four fields; the rest will be managed by the CB */
5518 BlockDriverAmendStatusCB
*original_status_cb
;
5519 void *original_cb_opaque
;
5521 Qcow2AmendOperation current_operation
;
5523 /* Total number of operations to perform (only set once) */
5524 int total_operations
;
5526 /* The following fields are managed by the CB */
5528 /* Number of operations completed */
5529 int operations_completed
;
5531 /* Cumulative offset of all completed operations */
5532 int64_t offset_completed
;
5534 Qcow2AmendOperation last_operation
;
5535 int64_t last_work_size
;
5536 } Qcow2AmendHelperCBInfo
;
5538 static void qcow2_amend_helper_cb(BlockDriverState
*bs
,
5539 int64_t operation_offset
,
5540 int64_t operation_work_size
, void *opaque
)
5542 Qcow2AmendHelperCBInfo
*info
= opaque
;
5543 int64_t current_work_size
;
5544 int64_t projected_work_size
;
5546 if (info
->current_operation
!= info
->last_operation
) {
5547 if (info
->last_operation
!= QCOW2_NO_OPERATION
) {
5548 info
->offset_completed
+= info
->last_work_size
;
5549 info
->operations_completed
++;
5552 info
->last_operation
= info
->current_operation
;
5555 assert(info
->total_operations
> 0);
5556 assert(info
->operations_completed
< info
->total_operations
);
5558 info
->last_work_size
= operation_work_size
;
5560 current_work_size
= info
->offset_completed
+ operation_work_size
;
5562 /* current_work_size is the total work size for (operations_completed + 1)
5563 * operations (which includes this one), so multiply it by the number of
5564 * operations not covered and divide it by the number of operations
5565 * covered to get a projection for the operations not covered */
5566 projected_work_size
= current_work_size
* (info
->total_operations
-
5567 info
->operations_completed
- 1)
5568 / (info
->operations_completed
+ 1);
5570 info
->original_status_cb(bs
, info
->offset_completed
+ operation_offset
,
5571 current_work_size
+ projected_work_size
,
5572 info
->original_cb_opaque
);
5575 static int qcow2_amend_options(BlockDriverState
*bs
, QemuOpts
*opts
,
5576 BlockDriverAmendStatusCB
*status_cb
,
5581 BDRVQcow2State
*s
= bs
->opaque
;
5582 int old_version
= s
->qcow_version
, new_version
= old_version
;
5583 uint64_t new_size
= 0;
5584 const char *backing_file
= NULL
, *backing_format
= NULL
, *data_file
= NULL
;
5585 bool lazy_refcounts
= s
->use_lazy_refcounts
;
5586 bool data_file_raw
= data_file_is_raw(bs
);
5587 const char *compat
= NULL
;
5588 int refcount_bits
= s
->refcount_bits
;
5590 QemuOptDesc
*desc
= opts
->list
->desc
;
5591 Qcow2AmendHelperCBInfo helper_cb_info
;
5592 bool encryption_update
= false;
5594 while (desc
&& desc
->name
) {
5595 if (!qemu_opt_find(opts
, desc
->name
)) {
5596 /* only change explicitly defined options */
5601 if (!strcmp(desc
->name
, BLOCK_OPT_COMPAT_LEVEL
)) {
5602 compat
= qemu_opt_get(opts
, BLOCK_OPT_COMPAT_LEVEL
);
5604 /* preserve default */
5605 } else if (!strcmp(compat
, "0.10") || !strcmp(compat
, "v2")) {
5607 } else if (!strcmp(compat
, "1.1") || !strcmp(compat
, "v3")) {
5610 error_setg(errp
, "Unknown compatibility level %s", compat
);
5613 } else if (!strcmp(desc
->name
, BLOCK_OPT_SIZE
)) {
5614 new_size
= qemu_opt_get_size(opts
, BLOCK_OPT_SIZE
, 0);
5615 } else if (!strcmp(desc
->name
, BLOCK_OPT_BACKING_FILE
)) {
5616 backing_file
= qemu_opt_get(opts
, BLOCK_OPT_BACKING_FILE
);
5617 } else if (!strcmp(desc
->name
, BLOCK_OPT_BACKING_FMT
)) {
5618 backing_format
= qemu_opt_get(opts
, BLOCK_OPT_BACKING_FMT
);
5619 } else if (g_str_has_prefix(desc
->name
, "encrypt.")) {
5622 "Can't amend encryption options - encryption not present");
5625 if (s
->crypt_method_header
!= QCOW_CRYPT_LUKS
) {
5627 "Only LUKS encryption options can be amended");
5630 encryption_update
= true;
5631 } else if (!strcmp(desc
->name
, BLOCK_OPT_LAZY_REFCOUNTS
)) {
5632 lazy_refcounts
= qemu_opt_get_bool(opts
, BLOCK_OPT_LAZY_REFCOUNTS
,
5634 } else if (!strcmp(desc
->name
, BLOCK_OPT_REFCOUNT_BITS
)) {
5635 refcount_bits
= qemu_opt_get_number(opts
, BLOCK_OPT_REFCOUNT_BITS
,
5638 if (refcount_bits
<= 0 || refcount_bits
> 64 ||
5639 !is_power_of_2(refcount_bits
))
5641 error_setg(errp
, "Refcount width must be a power of two and "
5642 "may not exceed 64 bits");
5645 } else if (!strcmp(desc
->name
, BLOCK_OPT_DATA_FILE
)) {
5646 data_file
= qemu_opt_get(opts
, BLOCK_OPT_DATA_FILE
);
5647 if (data_file
&& !has_data_file(bs
)) {
5648 error_setg(errp
, "data-file can only be set for images that "
5649 "use an external data file");
5652 } else if (!strcmp(desc
->name
, BLOCK_OPT_DATA_FILE_RAW
)) {
5653 data_file_raw
= qemu_opt_get_bool(opts
, BLOCK_OPT_DATA_FILE_RAW
,
5655 if (data_file_raw
&& !data_file_is_raw(bs
)) {
5656 error_setg(errp
, "data-file-raw cannot be set on existing "
5661 /* if this point is reached, this probably means a new option was
5662 * added without having it covered here */
5669 helper_cb_info
= (Qcow2AmendHelperCBInfo
){
5670 .original_status_cb
= status_cb
,
5671 .original_cb_opaque
= cb_opaque
,
5672 .total_operations
= (new_version
!= old_version
)
5673 + (s
->refcount_bits
!= refcount_bits
) +
5674 (encryption_update
== true)
5677 /* Upgrade first (some features may require compat=1.1) */
5678 if (new_version
> old_version
) {
5679 helper_cb_info
.current_operation
= QCOW2_UPGRADING
;
5680 ret
= qcow2_upgrade(bs
, new_version
, &qcow2_amend_helper_cb
,
5681 &helper_cb_info
, errp
);
5687 if (encryption_update
) {
5688 QDict
*amend_opts_dict
;
5689 QCryptoBlockAmendOptions
*amend_opts
;
5691 helper_cb_info
.current_operation
= QCOW2_UPDATING_ENCRYPTION
;
5692 amend_opts_dict
= qcow2_extract_crypto_opts(opts
, "luks", errp
);
5693 if (!amend_opts_dict
) {
5696 amend_opts
= block_crypto_amend_opts_init(amend_opts_dict
, errp
);
5697 qobject_unref(amend_opts_dict
);
5701 ret
= qcrypto_block_amend_options(s
->crypto
,
5702 qcow2_crypto_hdr_read_func
,
5703 qcow2_crypto_hdr_write_func
,
5708 qapi_free_QCryptoBlockAmendOptions(amend_opts
);
5714 if (s
->refcount_bits
!= refcount_bits
) {
5715 int refcount_order
= ctz32(refcount_bits
);
5717 if (new_version
< 3 && refcount_bits
!= 16) {
5718 error_setg(errp
, "Refcount widths other than 16 bits require "
5719 "compatibility level 1.1 or above (use compat=1.1 or "
5724 helper_cb_info
.current_operation
= QCOW2_CHANGING_REFCOUNT_ORDER
;
5725 ret
= qcow2_change_refcount_order(bs
, refcount_order
,
5726 &qcow2_amend_helper_cb
,
5727 &helper_cb_info
, errp
);
5733 /* data-file-raw blocks backing files, so clear it first if requested */
5734 if (data_file_raw
) {
5735 s
->autoclear_features
|= QCOW2_AUTOCLEAR_DATA_FILE_RAW
;
5737 s
->autoclear_features
&= ~QCOW2_AUTOCLEAR_DATA_FILE_RAW
;
5741 g_free(s
->image_data_file
);
5742 s
->image_data_file
= *data_file
? g_strdup(data_file
) : NULL
;
5745 ret
= qcow2_update_header(bs
);
5747 error_setg_errno(errp
, -ret
, "Failed to update the image header");
5751 if (backing_file
|| backing_format
) {
5752 if (g_strcmp0(backing_file
, s
->image_backing_file
) ||
5753 g_strcmp0(backing_format
, s
->image_backing_format
)) {
5754 error_setg(errp
, "Cannot amend the backing file");
5755 error_append_hint(errp
,
5756 "You can use 'qemu-img rebase' instead.\n");
5761 if (s
->use_lazy_refcounts
!= lazy_refcounts
) {
5762 if (lazy_refcounts
) {
5763 if (new_version
< 3) {
5764 error_setg(errp
, "Lazy refcounts only supported with "
5765 "compatibility level 1.1 and above (use compat=1.1 "
5769 s
->compatible_features
|= QCOW2_COMPAT_LAZY_REFCOUNTS
;
5770 ret
= qcow2_update_header(bs
);
5772 s
->compatible_features
&= ~QCOW2_COMPAT_LAZY_REFCOUNTS
;
5773 error_setg_errno(errp
, -ret
, "Failed to update the image header");
5776 s
->use_lazy_refcounts
= true;
5778 /* make image clean first */
5779 ret
= qcow2_mark_clean(bs
);
5781 error_setg_errno(errp
, -ret
, "Failed to make the image clean");
5784 /* now disallow lazy refcounts */
5785 s
->compatible_features
&= ~QCOW2_COMPAT_LAZY_REFCOUNTS
;
5786 ret
= qcow2_update_header(bs
);
5788 s
->compatible_features
|= QCOW2_COMPAT_LAZY_REFCOUNTS
;
5789 error_setg_errno(errp
, -ret
, "Failed to update the image header");
5792 s
->use_lazy_refcounts
= false;
5797 BlockBackend
*blk
= blk_new_with_bs(bs
, BLK_PERM_RESIZE
, BLK_PERM_ALL
,
5804 * Amending image options should ensure that the image has
5805 * exactly the given new values, so pass exact=true here.
5807 ret
= blk_truncate(blk
, new_size
, true, PREALLOC_MODE_OFF
, 0, errp
);
5814 /* Downgrade last (so unsupported features can be removed before) */
5815 if (new_version
< old_version
) {
5816 helper_cb_info
.current_operation
= QCOW2_DOWNGRADING
;
5817 ret
= qcow2_downgrade(bs
, new_version
, &qcow2_amend_helper_cb
,
5818 &helper_cb_info
, errp
);
5827 static int coroutine_fn
qcow2_co_amend(BlockDriverState
*bs
,
5828 BlockdevAmendOptions
*opts
,
5832 BlockdevAmendOptionsQcow2
*qopts
= &opts
->u
.qcow2
;
5833 BDRVQcow2State
*s
= bs
->opaque
;
5836 if (qopts
->has_encrypt
) {
5838 error_setg(errp
, "image is not encrypted, can't amend");
5842 if (qopts
->encrypt
->format
!= Q_CRYPTO_BLOCK_FORMAT_LUKS
) {
5844 "Amend can't be used to change the qcow2 encryption format");
5848 if (s
->crypt_method_header
!= QCOW_CRYPT_LUKS
) {
5850 "Only LUKS encryption options can be amended for qcow2 with blockdev-amend");
5854 ret
= qcrypto_block_amend_options(s
->crypto
,
5855 qcow2_crypto_hdr_read_func
,
5856 qcow2_crypto_hdr_write_func
,
5866 * If offset or size are negative, respectively, they will not be included in
5867 * the BLOCK_IMAGE_CORRUPTED event emitted.
5868 * fatal will be ignored for read-only BDS; corruptions found there will always
5869 * be considered non-fatal.
5871 void qcow2_signal_corruption(BlockDriverState
*bs
, bool fatal
, int64_t offset
,
5872 int64_t size
, const char *message_format
, ...)
5874 BDRVQcow2State
*s
= bs
->opaque
;
5875 const char *node_name
;
5879 fatal
= fatal
&& bdrv_is_writable(bs
);
5881 if (s
->signaled_corruption
&&
5882 (!fatal
|| (s
->incompatible_features
& QCOW2_INCOMPAT_CORRUPT
)))
5887 va_start(ap
, message_format
);
5888 message
= g_strdup_vprintf(message_format
, ap
);
5892 fprintf(stderr
, "qcow2: Marking image as corrupt: %s; further "
5893 "corruption events will be suppressed\n", message
);
5895 fprintf(stderr
, "qcow2: Image is corrupt: %s; further non-fatal "
5896 "corruption events will be suppressed\n", message
);
5899 node_name
= bdrv_get_node_name(bs
);
5900 qapi_event_send_block_image_corrupted(bdrv_get_device_name(bs
),
5901 *node_name
!= '\0', node_name
,
5902 message
, offset
>= 0, offset
,
5908 qcow2_mark_corrupt(bs
);
5909 bs
->drv
= NULL
; /* make BDS unusable */
5912 s
->signaled_corruption
= true;
5915 #define QCOW_COMMON_OPTIONS \
5917 .name = BLOCK_OPT_SIZE, \
5918 .type = QEMU_OPT_SIZE, \
5919 .help = "Virtual disk size" \
5922 .name = BLOCK_OPT_COMPAT_LEVEL, \
5923 .type = QEMU_OPT_STRING, \
5924 .help = "Compatibility level (v2 [0.10] or v3 [1.1])" \
5927 .name = BLOCK_OPT_BACKING_FILE, \
5928 .type = QEMU_OPT_STRING, \
5929 .help = "File name of a base image" \
5932 .name = BLOCK_OPT_BACKING_FMT, \
5933 .type = QEMU_OPT_STRING, \
5934 .help = "Image format of the base image" \
5937 .name = BLOCK_OPT_DATA_FILE, \
5938 .type = QEMU_OPT_STRING, \
5939 .help = "File name of an external data file" \
5942 .name = BLOCK_OPT_DATA_FILE_RAW, \
5943 .type = QEMU_OPT_BOOL, \
5944 .help = "The external data file must stay valid " \
5948 .name = BLOCK_OPT_LAZY_REFCOUNTS, \
5949 .type = QEMU_OPT_BOOL, \
5950 .help = "Postpone refcount updates", \
5951 .def_value_str = "off" \
5954 .name = BLOCK_OPT_REFCOUNT_BITS, \
5955 .type = QEMU_OPT_NUMBER, \
5956 .help = "Width of a reference count entry in bits", \
5957 .def_value_str = "16" \
5960 static QemuOptsList qcow2_create_opts
= {
5961 .name
= "qcow2-create-opts",
5962 .head
= QTAILQ_HEAD_INITIALIZER(qcow2_create_opts
.head
),
5965 .name
= BLOCK_OPT_ENCRYPT
, \
5966 .type
= QEMU_OPT_BOOL
, \
5967 .help
= "Encrypt the image with format 'aes'. (Deprecated " \
5968 "in favor of " BLOCK_OPT_ENCRYPT_FORMAT
"=aes)", \
5971 .name
= BLOCK_OPT_ENCRYPT_FORMAT
, \
5972 .type
= QEMU_OPT_STRING
, \
5973 .help
= "Encrypt the image, format choices: 'aes', 'luks'", \
5975 BLOCK_CRYPTO_OPT_DEF_KEY_SECRET("encrypt.", \
5976 "ID of secret providing qcow AES key or LUKS passphrase"), \
5977 BLOCK_CRYPTO_OPT_DEF_LUKS_CIPHER_ALG("encrypt."), \
5978 BLOCK_CRYPTO_OPT_DEF_LUKS_CIPHER_MODE("encrypt."), \
5979 BLOCK_CRYPTO_OPT_DEF_LUKS_IVGEN_ALG("encrypt."), \
5980 BLOCK_CRYPTO_OPT_DEF_LUKS_IVGEN_HASH_ALG("encrypt."), \
5981 BLOCK_CRYPTO_OPT_DEF_LUKS_HASH_ALG("encrypt."), \
5982 BLOCK_CRYPTO_OPT_DEF_LUKS_ITER_TIME("encrypt."), \
5984 .name
= BLOCK_OPT_CLUSTER_SIZE
, \
5985 .type
= QEMU_OPT_SIZE
, \
5986 .help
= "qcow2 cluster size", \
5987 .def_value_str
= stringify(DEFAULT_CLUSTER_SIZE
) \
5990 .name
= BLOCK_OPT_EXTL2
, \
5991 .type
= QEMU_OPT_BOOL
, \
5992 .help
= "Extended L2 tables", \
5993 .def_value_str
= "off" \
5996 .name
= BLOCK_OPT_PREALLOC
, \
5997 .type
= QEMU_OPT_STRING
, \
5998 .help
= "Preallocation mode (allowed values: off, " \
5999 "metadata, falloc, full)" \
6002 .name
= BLOCK_OPT_COMPRESSION_TYPE
, \
6003 .type
= QEMU_OPT_STRING
, \
6004 .help
= "Compression method used for image cluster " \
6006 .def_value_str
= "zlib" \
6008 QCOW_COMMON_OPTIONS
,
6009 { /* end of list */ }
6013 static QemuOptsList qcow2_amend_opts
= {
6014 .name
= "qcow2-amend-opts",
6015 .head
= QTAILQ_HEAD_INITIALIZER(qcow2_amend_opts
.head
),
6017 BLOCK_CRYPTO_OPT_DEF_LUKS_STATE("encrypt."),
6018 BLOCK_CRYPTO_OPT_DEF_LUKS_KEYSLOT("encrypt."),
6019 BLOCK_CRYPTO_OPT_DEF_LUKS_OLD_SECRET("encrypt."),
6020 BLOCK_CRYPTO_OPT_DEF_LUKS_NEW_SECRET("encrypt."),
6021 BLOCK_CRYPTO_OPT_DEF_LUKS_ITER_TIME("encrypt."),
6022 QCOW_COMMON_OPTIONS
,
6023 { /* end of list */ }
6027 static const char *const qcow2_strong_runtime_opts
[] = {
6028 "encrypt." BLOCK_CRYPTO_OPT_QCOW_KEY_SECRET
,
6033 BlockDriver bdrv_qcow2
= {
6034 .format_name
= "qcow2",
6035 .instance_size
= sizeof(BDRVQcow2State
),
6036 .bdrv_probe
= qcow2_probe
,
6037 .bdrv_open
= qcow2_open
,
6038 .bdrv_close
= qcow2_close
,
6039 .bdrv_reopen_prepare
= qcow2_reopen_prepare
,
6040 .bdrv_reopen_commit
= qcow2_reopen_commit
,
6041 .bdrv_reopen_commit_post
= qcow2_reopen_commit_post
,
6042 .bdrv_reopen_abort
= qcow2_reopen_abort
,
6043 .bdrv_join_options
= qcow2_join_options
,
6044 .bdrv_child_perm
= bdrv_default_perms
,
6045 .bdrv_co_create_opts
= qcow2_co_create_opts
,
6046 .bdrv_co_create
= qcow2_co_create
,
6047 .bdrv_has_zero_init
= qcow2_has_zero_init
,
6048 .bdrv_co_block_status
= qcow2_co_block_status
,
6050 .bdrv_co_preadv_part
= qcow2_co_preadv_part
,
6051 .bdrv_co_pwritev_part
= qcow2_co_pwritev_part
,
6052 .bdrv_co_flush_to_os
= qcow2_co_flush_to_os
,
6054 .bdrv_co_pwrite_zeroes
= qcow2_co_pwrite_zeroes
,
6055 .bdrv_co_pdiscard
= qcow2_co_pdiscard
,
6056 .bdrv_co_copy_range_from
= qcow2_co_copy_range_from
,
6057 .bdrv_co_copy_range_to
= qcow2_co_copy_range_to
,
6058 .bdrv_co_truncate
= qcow2_co_truncate
,
6059 .bdrv_co_pwritev_compressed_part
= qcow2_co_pwritev_compressed_part
,
6060 .bdrv_make_empty
= qcow2_make_empty
,
6062 .bdrv_snapshot_create
= qcow2_snapshot_create
,
6063 .bdrv_snapshot_goto
= qcow2_snapshot_goto
,
6064 .bdrv_snapshot_delete
= qcow2_snapshot_delete
,
6065 .bdrv_snapshot_list
= qcow2_snapshot_list
,
6066 .bdrv_snapshot_load_tmp
= qcow2_snapshot_load_tmp
,
6067 .bdrv_measure
= qcow2_measure
,
6068 .bdrv_get_info
= qcow2_get_info
,
6069 .bdrv_get_specific_info
= qcow2_get_specific_info
,
6071 .bdrv_save_vmstate
= qcow2_save_vmstate
,
6072 .bdrv_load_vmstate
= qcow2_load_vmstate
,
6075 .supports_backing
= true,
6076 .bdrv_change_backing_file
= qcow2_change_backing_file
,
6078 .bdrv_refresh_limits
= qcow2_refresh_limits
,
6079 .bdrv_co_invalidate_cache
= qcow2_co_invalidate_cache
,
6080 .bdrv_inactivate
= qcow2_inactivate
,
6082 .create_opts
= &qcow2_create_opts
,
6083 .amend_opts
= &qcow2_amend_opts
,
6084 .strong_runtime_opts
= qcow2_strong_runtime_opts
,
6085 .mutable_opts
= mutable_opts
,
6086 .bdrv_co_check
= qcow2_co_check
,
6087 .bdrv_amend_options
= qcow2_amend_options
,
6088 .bdrv_co_amend
= qcow2_co_amend
,
6090 .bdrv_detach_aio_context
= qcow2_detach_aio_context
,
6091 .bdrv_attach_aio_context
= qcow2_attach_aio_context
,
6093 .bdrv_supports_persistent_dirty_bitmap
=
6094 qcow2_supports_persistent_dirty_bitmap
,
6095 .bdrv_co_can_store_new_dirty_bitmap
= qcow2_co_can_store_new_dirty_bitmap
,
6096 .bdrv_co_remove_persistent_dirty_bitmap
=
6097 qcow2_co_remove_persistent_dirty_bitmap
,
6100 static void bdrv_qcow2_init(void)
6102 bdrv_register(&bdrv_qcow2
);
6105 block_init(bdrv_qcow2_init
);