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
28 #include "crypto/block.h"
29 #include "qemu/coroutine.h"
30 #include "qemu/units.h"
31 #include "block/block_int.h"
34 //#define DEBUG_ALLOC2
37 #define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
39 #define QCOW_CRYPT_NONE 0
40 #define QCOW_CRYPT_AES 1
41 #define QCOW_CRYPT_LUKS 2
43 #define QCOW_MAX_CRYPT_CLUSTERS 32
44 #define QCOW_MAX_SNAPSHOTS 65536
46 /* Field widths in qcow2 mean normal cluster offsets cannot reach
47 * 64PB; depending on cluster size, compressed clusters can have a
48 * smaller limit (64PB for up to 16k clusters, then ramps down to
49 * 512TB for 2M clusters). */
50 #define QCOW_MAX_CLUSTER_OFFSET ((1ULL << 56) - 1)
52 /* 8 MB refcount table is enough for 2 PB images at 64k cluster size
53 * (128 GB for 512 byte clusters, 2 EB for 2 MB clusters) */
54 #define QCOW_MAX_REFTABLE_SIZE (8 * MiB)
56 /* 32 MB L1 table is enough for 2 PB images at 64k cluster size
57 * (128 GB for 512 byte clusters, 2 EB for 2 MB clusters) */
58 #define QCOW_MAX_L1_SIZE (32 * MiB)
60 /* Allow for an average of 1k per snapshot table entry, should be plenty of
61 * space for snapshot names and IDs */
62 #define QCOW_MAX_SNAPSHOTS_SIZE (1024 * QCOW_MAX_SNAPSHOTS)
64 /* Maximum amount of extra data per snapshot table entry to accept */
65 #define QCOW_MAX_SNAPSHOT_EXTRA_DATA 1024
67 /* Bitmap header extension constraints */
68 #define QCOW2_MAX_BITMAPS 65535
69 #define QCOW2_MAX_BITMAP_DIRECTORY_SIZE (1024 * QCOW2_MAX_BITMAPS)
71 /* Maximum of parallel sub-request per guest request */
72 #define QCOW2_MAX_WORKERS 8
74 /* indicate that the refcount of the referenced cluster is exactly one. */
75 #define QCOW_OFLAG_COPIED (1ULL << 63)
76 /* indicate that the cluster is compressed (they never have the copied flag) */
77 #define QCOW_OFLAG_COMPRESSED (1ULL << 62)
78 /* The cluster reads as all zeros */
79 #define QCOW_OFLAG_ZERO (1ULL << 0)
81 #define QCOW_EXTL2_SUBCLUSTERS_PER_CLUSTER 32
83 /* Size of normal and extended L2 entries */
84 #define L2E_SIZE_NORMAL (sizeof(uint64_t))
85 #define L2E_SIZE_EXTENDED (sizeof(uint64_t) * 2)
87 #define MIN_CLUSTER_BITS 9
88 #define MAX_CLUSTER_BITS 21
90 /* Defined in the qcow2 spec (compressed cluster descriptor) */
91 #define QCOW2_COMPRESSED_SECTOR_SIZE 512U
92 #define QCOW2_COMPRESSED_SECTOR_MASK (~(QCOW2_COMPRESSED_SECTOR_SIZE - 1ULL))
94 /* Must be at least 2 to cover COW */
95 #define MIN_L2_CACHE_SIZE 2 /* cache entries */
97 /* Must be at least 4 to cover all cases of refcount table growth */
98 #define MIN_REFCOUNT_CACHE_SIZE 4 /* clusters */
101 #define DEFAULT_L2_CACHE_MAX_SIZE (32 * MiB)
102 #define DEFAULT_CACHE_CLEAN_INTERVAL 600 /* seconds */
104 #define DEFAULT_L2_CACHE_MAX_SIZE (8 * MiB)
105 /* Cache clean interval is currently available only on Linux, so must be 0 */
106 #define DEFAULT_CACHE_CLEAN_INTERVAL 0
109 #define DEFAULT_CLUSTER_SIZE 65536
111 #define QCOW2_OPT_DATA_FILE "data-file"
112 #define QCOW2_OPT_LAZY_REFCOUNTS "lazy-refcounts"
113 #define QCOW2_OPT_DISCARD_REQUEST "pass-discard-request"
114 #define QCOW2_OPT_DISCARD_SNAPSHOT "pass-discard-snapshot"
115 #define QCOW2_OPT_DISCARD_OTHER "pass-discard-other"
116 #define QCOW2_OPT_OVERLAP "overlap-check"
117 #define QCOW2_OPT_OVERLAP_TEMPLATE "overlap-check.template"
118 #define QCOW2_OPT_OVERLAP_MAIN_HEADER "overlap-check.main-header"
119 #define QCOW2_OPT_OVERLAP_ACTIVE_L1 "overlap-check.active-l1"
120 #define QCOW2_OPT_OVERLAP_ACTIVE_L2 "overlap-check.active-l2"
121 #define QCOW2_OPT_OVERLAP_REFCOUNT_TABLE "overlap-check.refcount-table"
122 #define QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK "overlap-check.refcount-block"
123 #define QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE "overlap-check.snapshot-table"
124 #define QCOW2_OPT_OVERLAP_INACTIVE_L1 "overlap-check.inactive-l1"
125 #define QCOW2_OPT_OVERLAP_INACTIVE_L2 "overlap-check.inactive-l2"
126 #define QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY "overlap-check.bitmap-directory"
127 #define QCOW2_OPT_CACHE_SIZE "cache-size"
128 #define QCOW2_OPT_L2_CACHE_SIZE "l2-cache-size"
129 #define QCOW2_OPT_L2_CACHE_ENTRY_SIZE "l2-cache-entry-size"
130 #define QCOW2_OPT_REFCOUNT_CACHE_SIZE "refcount-cache-size"
131 #define QCOW2_OPT_CACHE_CLEAN_INTERVAL "cache-clean-interval"
133 typedef struct QCowHeader
{
136 uint64_t backing_file_offset
;
137 uint32_t backing_file_size
;
138 uint32_t cluster_bits
;
139 uint64_t size
; /* in bytes */
140 uint32_t crypt_method
;
141 uint32_t l1_size
; /* XXX: save number of clusters instead ? */
142 uint64_t l1_table_offset
;
143 uint64_t refcount_table_offset
;
144 uint32_t refcount_table_clusters
;
145 uint32_t nb_snapshots
;
146 uint64_t snapshots_offset
;
148 /* The following fields are only valid for version >= 3 */
149 uint64_t incompatible_features
;
150 uint64_t compatible_features
;
151 uint64_t autoclear_features
;
153 uint32_t refcount_order
;
154 uint32_t header_length
;
156 /* Additional fields */
157 uint8_t compression_type
;
159 /* header must be a multiple of 8 */
161 } QEMU_PACKED QCowHeader
;
163 QEMU_BUILD_BUG_ON(!QEMU_IS_ALIGNED(sizeof(QCowHeader
), 8));
165 typedef struct QEMU_PACKED QCowSnapshotHeader
{
166 /* header is 8 byte aligned */
167 uint64_t l1_table_offset
;
170 uint16_t id_str_size
;
176 uint64_t vm_clock_nsec
;
178 uint32_t vm_state_size
;
179 uint32_t extra_data_size
; /* for extension */
180 /* extra data follows */
183 } QCowSnapshotHeader
;
185 typedef struct QEMU_PACKED QCowSnapshotExtraData
{
186 uint64_t vm_state_size_large
;
188 } QCowSnapshotExtraData
;
191 typedef struct QCowSnapshot
{
192 uint64_t l1_table_offset
;
197 uint64_t vm_state_size
;
200 uint64_t vm_clock_nsec
;
201 /* Size of all extra data, including QCowSnapshotExtraData if available */
202 uint32_t extra_data_size
;
203 /* Data beyond QCowSnapshotExtraData, if any */
204 void *unknown_extra_data
;
208 typedef struct Qcow2Cache Qcow2Cache
;
210 typedef struct Qcow2CryptoHeaderExtension
{
213 } QEMU_PACKED Qcow2CryptoHeaderExtension
;
215 typedef struct Qcow2UnknownHeaderExtension
{
218 QLIST_ENTRY(Qcow2UnknownHeaderExtension
) next
;
220 } Qcow2UnknownHeaderExtension
;
223 QCOW2_FEAT_TYPE_INCOMPATIBLE
= 0,
224 QCOW2_FEAT_TYPE_COMPATIBLE
= 1,
225 QCOW2_FEAT_TYPE_AUTOCLEAR
= 2,
228 /* Incompatible feature bits */
230 QCOW2_INCOMPAT_DIRTY_BITNR
= 0,
231 QCOW2_INCOMPAT_CORRUPT_BITNR
= 1,
232 QCOW2_INCOMPAT_DATA_FILE_BITNR
= 2,
233 QCOW2_INCOMPAT_COMPRESSION_BITNR
= 3,
234 QCOW2_INCOMPAT_DIRTY
= 1 << QCOW2_INCOMPAT_DIRTY_BITNR
,
235 QCOW2_INCOMPAT_CORRUPT
= 1 << QCOW2_INCOMPAT_CORRUPT_BITNR
,
236 QCOW2_INCOMPAT_DATA_FILE
= 1 << QCOW2_INCOMPAT_DATA_FILE_BITNR
,
237 QCOW2_INCOMPAT_COMPRESSION
= 1 << QCOW2_INCOMPAT_COMPRESSION_BITNR
,
239 QCOW2_INCOMPAT_MASK
= QCOW2_INCOMPAT_DIRTY
240 | QCOW2_INCOMPAT_CORRUPT
241 | QCOW2_INCOMPAT_DATA_FILE
242 | QCOW2_INCOMPAT_COMPRESSION
,
245 /* Compatible feature bits */
247 QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR
= 0,
248 QCOW2_COMPAT_LAZY_REFCOUNTS
= 1 << QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR
,
250 QCOW2_COMPAT_FEAT_MASK
= QCOW2_COMPAT_LAZY_REFCOUNTS
,
253 /* Autoclear feature bits */
255 QCOW2_AUTOCLEAR_BITMAPS_BITNR
= 0,
256 QCOW2_AUTOCLEAR_DATA_FILE_RAW_BITNR
= 1,
257 QCOW2_AUTOCLEAR_BITMAPS
= 1 << QCOW2_AUTOCLEAR_BITMAPS_BITNR
,
258 QCOW2_AUTOCLEAR_DATA_FILE_RAW
= 1 << QCOW2_AUTOCLEAR_DATA_FILE_RAW_BITNR
,
260 QCOW2_AUTOCLEAR_MASK
= QCOW2_AUTOCLEAR_BITMAPS
261 | QCOW2_AUTOCLEAR_DATA_FILE_RAW
,
264 enum qcow2_discard_type
{
265 QCOW2_DISCARD_NEVER
= 0,
266 QCOW2_DISCARD_ALWAYS
,
267 QCOW2_DISCARD_REQUEST
,
268 QCOW2_DISCARD_SNAPSHOT
,
273 typedef struct Qcow2Feature
{
277 } QEMU_PACKED Qcow2Feature
;
279 typedef struct Qcow2DiscardRegion
{
280 BlockDriverState
*bs
;
283 QTAILQ_ENTRY(Qcow2DiscardRegion
) next
;
284 } Qcow2DiscardRegion
;
286 typedef uint64_t Qcow2GetRefcountFunc(const void *refcount_array
,
288 typedef void Qcow2SetRefcountFunc(void *refcount_array
,
289 uint64_t index
, uint64_t value
);
291 typedef struct Qcow2BitmapHeaderExt
{
294 uint64_t bitmap_directory_size
;
295 uint64_t bitmap_directory_offset
;
296 } QEMU_PACKED Qcow2BitmapHeaderExt
;
298 #define QCOW2_MAX_THREADS 4
300 typedef struct BDRVQcow2State
{
306 int subclusters_per_cluster
;
310 int l1_vm_state_index
;
311 int refcount_block_bits
;
312 int refcount_block_size
;
315 uint64_t cluster_offset_mask
;
316 uint64_t l1_table_offset
;
319 Qcow2Cache
* l2_table_cache
;
320 Qcow2Cache
* refcount_block_cache
;
321 QEMUTimer
*cache_clean_timer
;
322 unsigned cache_clean_interval
;
324 QLIST_HEAD(, QCowL2Meta
) cluster_allocs
;
326 uint64_t *refcount_table
;
327 uint64_t refcount_table_offset
;
328 uint32_t refcount_table_size
;
329 uint32_t max_refcount_table_index
; /* Last used entry in refcount_table */
330 uint64_t free_cluster_index
;
331 uint64_t free_byte_offset
;
335 Qcow2CryptoHeaderExtension crypto_header
; /* QCow2 header extension */
336 QCryptoBlockOpenOptions
*crypto_opts
; /* Disk encryption runtime options */
337 QCryptoBlock
*crypto
; /* Disk encryption format driver */
338 bool crypt_physical_offset
; /* Whether to use virtual or physical offset
339 for encryption initialization vector tweak */
340 uint32_t crypt_method_header
;
341 uint64_t snapshots_offset
;
343 unsigned int nb_snapshots
;
344 QCowSnapshot
*snapshots
;
347 uint64_t bitmap_directory_size
;
348 uint64_t bitmap_directory_offset
;
352 bool use_lazy_refcounts
;
355 uint64_t refcount_max
;
357 Qcow2GetRefcountFunc
*get_refcount
;
358 Qcow2SetRefcountFunc
*set_refcount
;
360 bool discard_passthrough
[QCOW2_DISCARD_MAX
];
362 int overlap_check
; /* bitmask of Qcow2MetadataOverlap values */
363 bool signaled_corruption
;
365 uint64_t incompatible_features
;
366 uint64_t compatible_features
;
367 uint64_t autoclear_features
;
369 size_t unknown_header_fields_size
;
370 void* unknown_header_fields
;
371 QLIST_HEAD(, Qcow2UnknownHeaderExtension
) unknown_header_ext
;
372 QTAILQ_HEAD (, Qcow2DiscardRegion
) discards
;
375 /* Backing file path and format as stored in the image (this is not the
376 * effective path/format, which may be the result of a runtime option
378 char *image_backing_file
;
379 char *image_backing_format
;
380 char *image_data_file
;
382 CoQueue thread_task_queue
;
385 BdrvChild
*data_file
;
387 bool metadata_preallocation_checked
;
388 bool metadata_preallocation
;
390 * Compression type used for the image. Default: 0 - ZLIB
391 * The image compression type is set on image creation.
392 * For now, the only way to change the compression type
393 * is to convert the image with the desired compression type set.
395 Qcow2CompressionType compression_type
;
398 typedef struct Qcow2COWRegion
{
400 * Offset of the COW region in bytes from the start of the first cluster
401 * touched by the request.
405 /** Number of bytes to copy */
410 * Describes an in-flight (part of a) write request that writes to clusters
411 * that are not referenced in their L2 table yet.
413 typedef struct QCowL2Meta
415 /** Guest offset of the first newly allocated cluster */
418 /** Host offset of the first newly allocated cluster */
419 uint64_t alloc_offset
;
421 /** Number of newly allocated clusters */
424 /** Do not free the old clusters */
425 bool keep_old_clusters
;
428 * Requests that overlap with this allocation and wait to be restarted
429 * when the allocating request has completed.
431 CoQueue dependent_requests
;
434 * The COW Region between the start of the first allocated cluster and the
435 * area the guest actually writes to.
437 Qcow2COWRegion cow_start
;
440 * The COW Region between the area the guest actually writes to and the
441 * end of the last allocated cluster.
443 Qcow2COWRegion cow_end
;
446 * Indicates that COW regions are already handled and do not require
447 * any more processing.
452 * The I/O vector with the data from the actual guest write request.
453 * If non-NULL, this is meant to be merged together with the data
454 * from @cow_start and @cow_end into one single write operation.
456 QEMUIOVector
*data_qiov
;
457 size_t data_qiov_offset
;
459 /** Pointer to next L2Meta of the same write request */
460 struct QCowL2Meta
*next
;
462 QLIST_ENTRY(QCowL2Meta
) next_in_flight
;
465 typedef enum QCow2ClusterType
{
466 QCOW2_CLUSTER_UNALLOCATED
,
467 QCOW2_CLUSTER_ZERO_PLAIN
,
468 QCOW2_CLUSTER_ZERO_ALLOC
,
469 QCOW2_CLUSTER_NORMAL
,
470 QCOW2_CLUSTER_COMPRESSED
,
473 typedef enum QCow2MetadataOverlap
{
474 QCOW2_OL_MAIN_HEADER_BITNR
= 0,
475 QCOW2_OL_ACTIVE_L1_BITNR
= 1,
476 QCOW2_OL_ACTIVE_L2_BITNR
= 2,
477 QCOW2_OL_REFCOUNT_TABLE_BITNR
= 3,
478 QCOW2_OL_REFCOUNT_BLOCK_BITNR
= 4,
479 QCOW2_OL_SNAPSHOT_TABLE_BITNR
= 5,
480 QCOW2_OL_INACTIVE_L1_BITNR
= 6,
481 QCOW2_OL_INACTIVE_L2_BITNR
= 7,
482 QCOW2_OL_BITMAP_DIRECTORY_BITNR
= 8,
484 QCOW2_OL_MAX_BITNR
= 9,
487 QCOW2_OL_MAIN_HEADER
= (1 << QCOW2_OL_MAIN_HEADER_BITNR
),
488 QCOW2_OL_ACTIVE_L1
= (1 << QCOW2_OL_ACTIVE_L1_BITNR
),
489 QCOW2_OL_ACTIVE_L2
= (1 << QCOW2_OL_ACTIVE_L2_BITNR
),
490 QCOW2_OL_REFCOUNT_TABLE
= (1 << QCOW2_OL_REFCOUNT_TABLE_BITNR
),
491 QCOW2_OL_REFCOUNT_BLOCK
= (1 << QCOW2_OL_REFCOUNT_BLOCK_BITNR
),
492 QCOW2_OL_SNAPSHOT_TABLE
= (1 << QCOW2_OL_SNAPSHOT_TABLE_BITNR
),
493 QCOW2_OL_INACTIVE_L1
= (1 << QCOW2_OL_INACTIVE_L1_BITNR
),
494 /* NOTE: Checking overlaps with inactive L2 tables will result in bdrv
496 QCOW2_OL_INACTIVE_L2
= (1 << QCOW2_OL_INACTIVE_L2_BITNR
),
497 QCOW2_OL_BITMAP_DIRECTORY
= (1 << QCOW2_OL_BITMAP_DIRECTORY_BITNR
),
498 } QCow2MetadataOverlap
;
500 /* Perform all overlap checks which can be done in constant time */
501 #define QCOW2_OL_CONSTANT \
502 (QCOW2_OL_MAIN_HEADER | QCOW2_OL_ACTIVE_L1 | QCOW2_OL_REFCOUNT_TABLE | \
503 QCOW2_OL_SNAPSHOT_TABLE | QCOW2_OL_BITMAP_DIRECTORY)
505 /* Perform all overlap checks which don't require disk access */
506 #define QCOW2_OL_CACHED \
507 (QCOW2_OL_CONSTANT | QCOW2_OL_ACTIVE_L2 | QCOW2_OL_REFCOUNT_BLOCK | \
508 QCOW2_OL_INACTIVE_L1)
510 /* Perform all overlap checks */
511 #define QCOW2_OL_ALL \
512 (QCOW2_OL_CACHED | QCOW2_OL_INACTIVE_L2)
514 #define L1E_OFFSET_MASK 0x00fffffffffffe00ULL
515 #define L2E_OFFSET_MASK 0x00fffffffffffe00ULL
516 #define L2E_COMPRESSED_OFFSET_SIZE_MASK 0x3fffffffffffffffULL
518 #define REFT_OFFSET_MASK 0xfffffffffffffe00ULL
520 #define INV_OFFSET (-1ULL)
522 static inline bool has_subclusters(BDRVQcow2State
*s
)
524 /* FIXME: Return false until this feature is complete */
528 static inline size_t l2_entry_size(BDRVQcow2State
*s
)
530 return has_subclusters(s
) ? L2E_SIZE_EXTENDED
: L2E_SIZE_NORMAL
;
533 static inline uint64_t get_l2_entry(BDRVQcow2State
*s
, uint64_t *l2_slice
,
536 idx
*= l2_entry_size(s
) / sizeof(uint64_t);
537 return be64_to_cpu(l2_slice
[idx
]);
540 static inline uint64_t get_l2_bitmap(BDRVQcow2State
*s
, uint64_t *l2_slice
,
543 if (has_subclusters(s
)) {
544 idx
*= l2_entry_size(s
) / sizeof(uint64_t);
545 return be64_to_cpu(l2_slice
[idx
+ 1]);
547 return 0; /* For convenience only; this value has no meaning. */
551 static inline void set_l2_entry(BDRVQcow2State
*s
, uint64_t *l2_slice
,
552 int idx
, uint64_t entry
)
554 idx
*= l2_entry_size(s
) / sizeof(uint64_t);
555 l2_slice
[idx
] = cpu_to_be64(entry
);
558 static inline void set_l2_bitmap(BDRVQcow2State
*s
, uint64_t *l2_slice
,
559 int idx
, uint64_t bitmap
)
561 assert(has_subclusters(s
));
562 idx
*= l2_entry_size(s
) / sizeof(uint64_t);
563 l2_slice
[idx
+ 1] = cpu_to_be64(bitmap
);
566 static inline bool has_data_file(BlockDriverState
*bs
)
568 BDRVQcow2State
*s
= bs
->opaque
;
569 return (s
->data_file
!= bs
->file
);
572 static inline bool data_file_is_raw(BlockDriverState
*bs
)
574 BDRVQcow2State
*s
= bs
->opaque
;
575 return !!(s
->autoclear_features
& QCOW2_AUTOCLEAR_DATA_FILE_RAW
);
578 static inline int64_t start_of_cluster(BDRVQcow2State
*s
, int64_t offset
)
580 return offset
& ~(s
->cluster_size
- 1);
583 static inline int64_t offset_into_cluster(BDRVQcow2State
*s
, int64_t offset
)
585 return offset
& (s
->cluster_size
- 1);
588 static inline int64_t offset_into_subcluster(BDRVQcow2State
*s
, int64_t offset
)
590 return offset
& (s
->subcluster_size
- 1);
593 static inline uint64_t size_to_clusters(BDRVQcow2State
*s
, uint64_t size
)
595 return (size
+ (s
->cluster_size
- 1)) >> s
->cluster_bits
;
598 static inline uint64_t size_to_subclusters(BDRVQcow2State
*s
, uint64_t size
)
600 return (size
+ (s
->subcluster_size
- 1)) >> s
->subcluster_bits
;
603 static inline int64_t size_to_l1(BDRVQcow2State
*s
, int64_t size
)
605 int shift
= s
->cluster_bits
+ s
->l2_bits
;
606 return (size
+ (1ULL << shift
) - 1) >> shift
;
609 static inline int offset_to_l1_index(BDRVQcow2State
*s
, uint64_t offset
)
611 return offset
>> (s
->l2_bits
+ s
->cluster_bits
);
614 static inline int offset_to_l2_index(BDRVQcow2State
*s
, int64_t offset
)
616 return (offset
>> s
->cluster_bits
) & (s
->l2_size
- 1);
619 static inline int offset_to_l2_slice_index(BDRVQcow2State
*s
, int64_t offset
)
621 return (offset
>> s
->cluster_bits
) & (s
->l2_slice_size
- 1);
624 static inline int offset_to_sc_index(BDRVQcow2State
*s
, int64_t offset
)
626 return (offset
>> s
->subcluster_bits
) & (s
->subclusters_per_cluster
- 1);
629 static inline int64_t qcow2_vm_state_offset(BDRVQcow2State
*s
)
631 return (int64_t)s
->l1_vm_state_index
<< (s
->cluster_bits
+ s
->l2_bits
);
634 static inline QCow2ClusterType
qcow2_get_cluster_type(BlockDriverState
*bs
,
637 if (l2_entry
& QCOW_OFLAG_COMPRESSED
) {
638 return QCOW2_CLUSTER_COMPRESSED
;
639 } else if (l2_entry
& QCOW_OFLAG_ZERO
) {
640 if (l2_entry
& L2E_OFFSET_MASK
) {
641 return QCOW2_CLUSTER_ZERO_ALLOC
;
643 return QCOW2_CLUSTER_ZERO_PLAIN
;
644 } else if (!(l2_entry
& L2E_OFFSET_MASK
)) {
645 /* Offset 0 generally means unallocated, but it is ambiguous with
646 * external data files because 0 is a valid offset there. However, all
647 * clusters in external data files always have refcount 1, so we can
648 * rely on QCOW_OFLAG_COPIED to disambiguate. */
649 if (has_data_file(bs
) && (l2_entry
& QCOW_OFLAG_COPIED
)) {
650 return QCOW2_CLUSTER_NORMAL
;
652 return QCOW2_CLUSTER_UNALLOCATED
;
655 return QCOW2_CLUSTER_NORMAL
;
659 /* Check whether refcounts are eager or lazy */
660 static inline bool qcow2_need_accurate_refcounts(BDRVQcow2State
*s
)
662 return !(s
->incompatible_features
& QCOW2_INCOMPAT_DIRTY
);
665 static inline uint64_t l2meta_cow_start(QCowL2Meta
*m
)
667 return m
->offset
+ m
->cow_start
.offset
;
670 static inline uint64_t l2meta_cow_end(QCowL2Meta
*m
)
672 return m
->offset
+ m
->cow_end
.offset
+ m
->cow_end
.nb_bytes
;
675 static inline uint64_t refcount_diff(uint64_t r1
, uint64_t r2
)
677 return r1
> r2
? r1
- r2
: r2
- r1
;
681 uint32_t offset_to_reftable_index(BDRVQcow2State
*s
, uint64_t offset
)
683 return offset
>> (s
->refcount_block_bits
+ s
->cluster_bits
);
686 /* qcow2.c functions */
687 int64_t qcow2_refcount_metadata_size(int64_t clusters
, size_t cluster_size
,
688 int refcount_order
, bool generous_increase
,
689 uint64_t *refblock_count
);
691 int qcow2_mark_dirty(BlockDriverState
*bs
);
692 int qcow2_mark_corrupt(BlockDriverState
*bs
);
693 int qcow2_mark_consistent(BlockDriverState
*bs
);
694 int qcow2_update_header(BlockDriverState
*bs
);
696 void qcow2_signal_corruption(BlockDriverState
*bs
, bool fatal
, int64_t offset
,
697 int64_t size
, const char *message_format
, ...)
700 int qcow2_validate_table(BlockDriverState
*bs
, uint64_t offset
,
701 uint64_t entries
, size_t entry_len
,
702 int64_t max_size_bytes
, const char *table_name
,
705 /* qcow2-refcount.c functions */
706 int qcow2_refcount_init(BlockDriverState
*bs
);
707 void qcow2_refcount_close(BlockDriverState
*bs
);
709 int qcow2_get_refcount(BlockDriverState
*bs
, int64_t cluster_index
,
712 int qcow2_update_cluster_refcount(BlockDriverState
*bs
, int64_t cluster_index
,
713 uint64_t addend
, bool decrease
,
714 enum qcow2_discard_type type
);
716 int64_t qcow2_refcount_area(BlockDriverState
*bs
, uint64_t offset
,
717 uint64_t additional_clusters
, bool exact_size
,
718 int new_refblock_index
,
719 uint64_t new_refblock_offset
);
721 int64_t qcow2_alloc_clusters(BlockDriverState
*bs
, uint64_t size
);
722 int64_t qcow2_alloc_clusters_at(BlockDriverState
*bs
, uint64_t offset
,
723 int64_t nb_clusters
);
724 int64_t qcow2_alloc_bytes(BlockDriverState
*bs
, int size
);
725 void qcow2_free_clusters(BlockDriverState
*bs
,
726 int64_t offset
, int64_t size
,
727 enum qcow2_discard_type type
);
728 void qcow2_free_any_clusters(BlockDriverState
*bs
, uint64_t l2_entry
,
729 int nb_clusters
, enum qcow2_discard_type type
);
731 int qcow2_update_snapshot_refcount(BlockDriverState
*bs
,
732 int64_t l1_table_offset
, int l1_size
, int addend
);
734 int coroutine_fn
qcow2_flush_caches(BlockDriverState
*bs
);
735 int coroutine_fn
qcow2_write_caches(BlockDriverState
*bs
);
736 int qcow2_check_refcounts(BlockDriverState
*bs
, BdrvCheckResult
*res
,
739 void qcow2_process_discards(BlockDriverState
*bs
, int ret
);
741 int qcow2_check_metadata_overlap(BlockDriverState
*bs
, int ign
, int64_t offset
,
743 int qcow2_pre_write_overlap_check(BlockDriverState
*bs
, int ign
, int64_t offset
,
744 int64_t size
, bool data_file
);
745 int qcow2_inc_refcounts_imrt(BlockDriverState
*bs
, BdrvCheckResult
*res
,
746 void **refcount_table
,
747 int64_t *refcount_table_size
,
748 int64_t offset
, int64_t size
);
750 int qcow2_change_refcount_order(BlockDriverState
*bs
, int refcount_order
,
751 BlockDriverAmendStatusCB
*status_cb
,
752 void *cb_opaque
, Error
**errp
);
753 int qcow2_shrink_reftable(BlockDriverState
*bs
);
754 int64_t qcow2_get_last_cluster(BlockDriverState
*bs
, int64_t size
);
755 int qcow2_detect_metadata_preallocation(BlockDriverState
*bs
);
757 /* qcow2-cluster.c functions */
758 int qcow2_grow_l1_table(BlockDriverState
*bs
, uint64_t min_size
,
760 int qcow2_shrink_l1_table(BlockDriverState
*bs
, uint64_t max_size
);
761 int qcow2_write_l1_entry(BlockDriverState
*bs
, int l1_index
);
762 int qcow2_encrypt_sectors(BDRVQcow2State
*s
, int64_t sector_num
,
763 uint8_t *buf
, int nb_sectors
, bool enc
, Error
**errp
);
765 int qcow2_get_host_offset(BlockDriverState
*bs
, uint64_t offset
,
766 unsigned int *bytes
, uint64_t *host_offset
);
767 int qcow2_alloc_cluster_offset(BlockDriverState
*bs
, uint64_t offset
,
768 unsigned int *bytes
, uint64_t *host_offset
,
770 int qcow2_alloc_compressed_cluster_offset(BlockDriverState
*bs
,
773 uint64_t *host_offset
);
775 int qcow2_alloc_cluster_link_l2(BlockDriverState
*bs
, QCowL2Meta
*m
);
776 void qcow2_alloc_cluster_abort(BlockDriverState
*bs
, QCowL2Meta
*m
);
777 int qcow2_cluster_discard(BlockDriverState
*bs
, uint64_t offset
,
778 uint64_t bytes
, enum qcow2_discard_type type
,
780 int qcow2_cluster_zeroize(BlockDriverState
*bs
, uint64_t offset
,
781 uint64_t bytes
, int flags
);
783 int qcow2_expand_zero_clusters(BlockDriverState
*bs
,
784 BlockDriverAmendStatusCB
*status_cb
,
787 /* qcow2-snapshot.c functions */
788 int qcow2_snapshot_create(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
);
789 int qcow2_snapshot_goto(BlockDriverState
*bs
, const char *snapshot_id
);
790 int qcow2_snapshot_delete(BlockDriverState
*bs
,
791 const char *snapshot_id
,
794 int qcow2_snapshot_list(BlockDriverState
*bs
, QEMUSnapshotInfo
**psn_tab
);
795 int qcow2_snapshot_load_tmp(BlockDriverState
*bs
,
796 const char *snapshot_id
,
800 void qcow2_free_snapshots(BlockDriverState
*bs
);
801 int qcow2_read_snapshots(BlockDriverState
*bs
, Error
**errp
);
802 int qcow2_write_snapshots(BlockDriverState
*bs
);
804 int coroutine_fn
qcow2_check_read_snapshot_table(BlockDriverState
*bs
,
805 BdrvCheckResult
*result
,
807 int coroutine_fn
qcow2_check_fix_snapshot_table(BlockDriverState
*bs
,
808 BdrvCheckResult
*result
,
811 /* qcow2-cache.c functions */
812 Qcow2Cache
*qcow2_cache_create(BlockDriverState
*bs
, int num_tables
,
813 unsigned table_size
);
814 int qcow2_cache_destroy(Qcow2Cache
*c
);
816 void qcow2_cache_entry_mark_dirty(Qcow2Cache
*c
, void *table
);
817 int qcow2_cache_flush(BlockDriverState
*bs
, Qcow2Cache
*c
);
818 int qcow2_cache_write(BlockDriverState
*bs
, Qcow2Cache
*c
);
819 int qcow2_cache_set_dependency(BlockDriverState
*bs
, Qcow2Cache
*c
,
820 Qcow2Cache
*dependency
);
821 void qcow2_cache_depends_on_flush(Qcow2Cache
*c
);
823 void qcow2_cache_clean_unused(Qcow2Cache
*c
);
824 int qcow2_cache_empty(BlockDriverState
*bs
, Qcow2Cache
*c
);
826 int qcow2_cache_get(BlockDriverState
*bs
, Qcow2Cache
*c
, uint64_t offset
,
828 int qcow2_cache_get_empty(BlockDriverState
*bs
, Qcow2Cache
*c
, uint64_t offset
,
830 void qcow2_cache_put(Qcow2Cache
*c
, void **table
);
831 void *qcow2_cache_is_table_offset(Qcow2Cache
*c
, uint64_t offset
);
832 void qcow2_cache_discard(Qcow2Cache
*c
, void *table
);
834 /* qcow2-bitmap.c functions */
835 int qcow2_check_bitmaps_refcounts(BlockDriverState
*bs
, BdrvCheckResult
*res
,
836 void **refcount_table
,
837 int64_t *refcount_table_size
);
838 bool qcow2_load_dirty_bitmaps(BlockDriverState
*bs
, Error
**errp
);
839 Qcow2BitmapInfoList
*qcow2_get_bitmap_info_list(BlockDriverState
*bs
,
841 int qcow2_reopen_bitmaps_rw(BlockDriverState
*bs
, Error
**errp
);
842 int qcow2_truncate_bitmaps_check(BlockDriverState
*bs
, Error
**errp
);
843 void qcow2_store_persistent_dirty_bitmaps(BlockDriverState
*bs
,
844 bool release_stored
, Error
**errp
);
845 int qcow2_reopen_bitmaps_ro(BlockDriverState
*bs
, Error
**errp
);
846 bool qcow2_co_can_store_new_dirty_bitmap(BlockDriverState
*bs
,
848 uint32_t granularity
,
850 int qcow2_co_remove_persistent_dirty_bitmap(BlockDriverState
*bs
,
853 bool qcow2_supports_persistent_dirty_bitmap(BlockDriverState
*bs
);
854 uint64_t qcow2_get_persistent_dirty_bitmap_size(BlockDriverState
*bs
,
855 uint32_t cluster_size
);
858 qcow2_co_compress(BlockDriverState
*bs
, void *dest
, size_t dest_size
,
859 const void *src
, size_t src_size
);
861 qcow2_co_decompress(BlockDriverState
*bs
, void *dest
, size_t dest_size
,
862 const void *src
, size_t src_size
);
864 qcow2_co_encrypt(BlockDriverState
*bs
, uint64_t host_offset
,
865 uint64_t guest_offset
, void *buf
, size_t len
);
867 qcow2_co_decrypt(BlockDriverState
*bs
, uint64_t host_offset
,
868 uint64_t guest_offset
, void *buf
, size_t len
);