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 /* The subcluster X [0..31] is allocated */
84 #define QCOW_OFLAG_SUB_ALLOC(X) (1ULL << (X))
85 /* The subcluster X [0..31] reads as zeroes */
86 #define QCOW_OFLAG_SUB_ZERO(X) (QCOW_OFLAG_SUB_ALLOC(X) << 32)
87 /* Subclusters [X, Y) (0 <= X <= Y <= 32) are allocated */
88 #define QCOW_OFLAG_SUB_ALLOC_RANGE(X, Y) \
89 (QCOW_OFLAG_SUB_ALLOC(Y) - QCOW_OFLAG_SUB_ALLOC(X))
90 /* Subclusters [X, Y) (0 <= X <= Y <= 32) read as zeroes */
91 #define QCOW_OFLAG_SUB_ZERO_RANGE(X, Y) \
92 (QCOW_OFLAG_SUB_ALLOC_RANGE(X, Y) << 32)
93 /* L2 entry bitmap with all allocation bits set */
94 #define QCOW_L2_BITMAP_ALL_ALLOC (QCOW_OFLAG_SUB_ALLOC_RANGE(0, 32))
95 /* L2 entry bitmap with all "read as zeroes" bits set */
96 #define QCOW_L2_BITMAP_ALL_ZEROES (QCOW_OFLAG_SUB_ZERO_RANGE(0, 32))
98 /* Size of normal and extended L2 entries */
99 #define L2E_SIZE_NORMAL (sizeof(uint64_t))
100 #define L2E_SIZE_EXTENDED (sizeof(uint64_t) * 2)
102 /* Size of L1 table entries */
103 #define L1E_SIZE (sizeof(uint64_t))
105 /* Size of reftable entries */
106 #define REFTABLE_ENTRY_SIZE (sizeof(uint64_t))
108 #define MIN_CLUSTER_BITS 9
109 #define MAX_CLUSTER_BITS 21
111 /* Defined in the qcow2 spec (compressed cluster descriptor) */
112 #define QCOW2_COMPRESSED_SECTOR_SIZE 512U
113 #define QCOW2_COMPRESSED_SECTOR_MASK (~(QCOW2_COMPRESSED_SECTOR_SIZE - 1ULL))
115 /* Must be at least 2 to cover COW */
116 #define MIN_L2_CACHE_SIZE 2 /* cache entries */
118 /* Must be at least 4 to cover all cases of refcount table growth */
119 #define MIN_REFCOUNT_CACHE_SIZE 4 /* clusters */
122 #define DEFAULT_L2_CACHE_MAX_SIZE (32 * MiB)
123 #define DEFAULT_CACHE_CLEAN_INTERVAL 600 /* seconds */
125 #define DEFAULT_L2_CACHE_MAX_SIZE (8 * MiB)
126 /* Cache clean interval is currently available only on Linux, so must be 0 */
127 #define DEFAULT_CACHE_CLEAN_INTERVAL 0
130 #define DEFAULT_CLUSTER_SIZE 65536
132 #define QCOW2_OPT_DATA_FILE "data-file"
133 #define QCOW2_OPT_LAZY_REFCOUNTS "lazy-refcounts"
134 #define QCOW2_OPT_DISCARD_REQUEST "pass-discard-request"
135 #define QCOW2_OPT_DISCARD_SNAPSHOT "pass-discard-snapshot"
136 #define QCOW2_OPT_DISCARD_OTHER "pass-discard-other"
137 #define QCOW2_OPT_OVERLAP "overlap-check"
138 #define QCOW2_OPT_OVERLAP_TEMPLATE "overlap-check.template"
139 #define QCOW2_OPT_OVERLAP_MAIN_HEADER "overlap-check.main-header"
140 #define QCOW2_OPT_OVERLAP_ACTIVE_L1 "overlap-check.active-l1"
141 #define QCOW2_OPT_OVERLAP_ACTIVE_L2 "overlap-check.active-l2"
142 #define QCOW2_OPT_OVERLAP_REFCOUNT_TABLE "overlap-check.refcount-table"
143 #define QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK "overlap-check.refcount-block"
144 #define QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE "overlap-check.snapshot-table"
145 #define QCOW2_OPT_OVERLAP_INACTIVE_L1 "overlap-check.inactive-l1"
146 #define QCOW2_OPT_OVERLAP_INACTIVE_L2 "overlap-check.inactive-l2"
147 #define QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY "overlap-check.bitmap-directory"
148 #define QCOW2_OPT_CACHE_SIZE "cache-size"
149 #define QCOW2_OPT_L2_CACHE_SIZE "l2-cache-size"
150 #define QCOW2_OPT_L2_CACHE_ENTRY_SIZE "l2-cache-entry-size"
151 #define QCOW2_OPT_REFCOUNT_CACHE_SIZE "refcount-cache-size"
152 #define QCOW2_OPT_CACHE_CLEAN_INTERVAL "cache-clean-interval"
154 typedef struct QCowHeader
{
157 uint64_t backing_file_offset
;
158 uint32_t backing_file_size
;
159 uint32_t cluster_bits
;
160 uint64_t size
; /* in bytes */
161 uint32_t crypt_method
;
162 uint32_t l1_size
; /* XXX: save number of clusters instead ? */
163 uint64_t l1_table_offset
;
164 uint64_t refcount_table_offset
;
165 uint32_t refcount_table_clusters
;
166 uint32_t nb_snapshots
;
167 uint64_t snapshots_offset
;
169 /* The following fields are only valid for version >= 3 */
170 uint64_t incompatible_features
;
171 uint64_t compatible_features
;
172 uint64_t autoclear_features
;
174 uint32_t refcount_order
;
175 uint32_t header_length
;
177 /* Additional fields */
178 uint8_t compression_type
;
180 /* header must be a multiple of 8 */
182 } QEMU_PACKED QCowHeader
;
184 QEMU_BUILD_BUG_ON(!QEMU_IS_ALIGNED(sizeof(QCowHeader
), 8));
186 typedef struct QEMU_PACKED QCowSnapshotHeader
{
187 /* header is 8 byte aligned */
188 uint64_t l1_table_offset
;
191 uint16_t id_str_size
;
197 uint64_t vm_clock_nsec
;
199 uint32_t vm_state_size
;
200 uint32_t extra_data_size
; /* for extension */
201 /* extra data follows */
204 } QCowSnapshotHeader
;
206 typedef struct QEMU_PACKED QCowSnapshotExtraData
{
207 uint64_t vm_state_size_large
;
209 } QCowSnapshotExtraData
;
212 typedef struct QCowSnapshot
{
213 uint64_t l1_table_offset
;
218 uint64_t vm_state_size
;
221 uint64_t vm_clock_nsec
;
222 /* Size of all extra data, including QCowSnapshotExtraData if available */
223 uint32_t extra_data_size
;
224 /* Data beyond QCowSnapshotExtraData, if any */
225 void *unknown_extra_data
;
229 typedef struct Qcow2Cache Qcow2Cache
;
231 typedef struct Qcow2CryptoHeaderExtension
{
234 } QEMU_PACKED Qcow2CryptoHeaderExtension
;
236 typedef struct Qcow2UnknownHeaderExtension
{
239 QLIST_ENTRY(Qcow2UnknownHeaderExtension
) next
;
241 } Qcow2UnknownHeaderExtension
;
244 QCOW2_FEAT_TYPE_INCOMPATIBLE
= 0,
245 QCOW2_FEAT_TYPE_COMPATIBLE
= 1,
246 QCOW2_FEAT_TYPE_AUTOCLEAR
= 2,
249 /* Incompatible feature bits */
251 QCOW2_INCOMPAT_DIRTY_BITNR
= 0,
252 QCOW2_INCOMPAT_CORRUPT_BITNR
= 1,
253 QCOW2_INCOMPAT_DATA_FILE_BITNR
= 2,
254 QCOW2_INCOMPAT_COMPRESSION_BITNR
= 3,
255 QCOW2_INCOMPAT_EXTL2_BITNR
= 4,
256 QCOW2_INCOMPAT_DIRTY
= 1 << QCOW2_INCOMPAT_DIRTY_BITNR
,
257 QCOW2_INCOMPAT_CORRUPT
= 1 << QCOW2_INCOMPAT_CORRUPT_BITNR
,
258 QCOW2_INCOMPAT_DATA_FILE
= 1 << QCOW2_INCOMPAT_DATA_FILE_BITNR
,
259 QCOW2_INCOMPAT_COMPRESSION
= 1 << QCOW2_INCOMPAT_COMPRESSION_BITNR
,
260 QCOW2_INCOMPAT_EXTL2
= 1 << QCOW2_INCOMPAT_EXTL2_BITNR
,
262 QCOW2_INCOMPAT_MASK
= QCOW2_INCOMPAT_DIRTY
263 | QCOW2_INCOMPAT_CORRUPT
264 | QCOW2_INCOMPAT_DATA_FILE
265 | QCOW2_INCOMPAT_COMPRESSION
266 | QCOW2_INCOMPAT_EXTL2
,
269 /* Compatible feature bits */
271 QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR
= 0,
272 QCOW2_COMPAT_LAZY_REFCOUNTS
= 1 << QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR
,
274 QCOW2_COMPAT_FEAT_MASK
= QCOW2_COMPAT_LAZY_REFCOUNTS
,
277 /* Autoclear feature bits */
279 QCOW2_AUTOCLEAR_BITMAPS_BITNR
= 0,
280 QCOW2_AUTOCLEAR_DATA_FILE_RAW_BITNR
= 1,
281 QCOW2_AUTOCLEAR_BITMAPS
= 1 << QCOW2_AUTOCLEAR_BITMAPS_BITNR
,
282 QCOW2_AUTOCLEAR_DATA_FILE_RAW
= 1 << QCOW2_AUTOCLEAR_DATA_FILE_RAW_BITNR
,
284 QCOW2_AUTOCLEAR_MASK
= QCOW2_AUTOCLEAR_BITMAPS
285 | QCOW2_AUTOCLEAR_DATA_FILE_RAW
,
288 enum qcow2_discard_type
{
289 QCOW2_DISCARD_NEVER
= 0,
290 QCOW2_DISCARD_ALWAYS
,
291 QCOW2_DISCARD_REQUEST
,
292 QCOW2_DISCARD_SNAPSHOT
,
297 typedef struct Qcow2Feature
{
301 } QEMU_PACKED Qcow2Feature
;
303 typedef struct Qcow2DiscardRegion
{
304 BlockDriverState
*bs
;
307 QTAILQ_ENTRY(Qcow2DiscardRegion
) next
;
308 } Qcow2DiscardRegion
;
310 typedef uint64_t Qcow2GetRefcountFunc(const void *refcount_array
,
312 typedef void Qcow2SetRefcountFunc(void *refcount_array
,
313 uint64_t index
, uint64_t value
);
315 typedef struct Qcow2BitmapHeaderExt
{
318 uint64_t bitmap_directory_size
;
319 uint64_t bitmap_directory_offset
;
320 } QEMU_PACKED Qcow2BitmapHeaderExt
;
322 #define QCOW2_MAX_THREADS 4
324 typedef struct BDRVQcow2State
{
330 int subclusters_per_cluster
;
334 int l1_vm_state_index
;
335 int refcount_block_bits
;
336 int refcount_block_size
;
339 uint64_t cluster_offset_mask
;
340 uint64_t l1_table_offset
;
343 Qcow2Cache
* l2_table_cache
;
344 Qcow2Cache
* refcount_block_cache
;
345 QEMUTimer
*cache_clean_timer
;
346 unsigned cache_clean_interval
;
348 QLIST_HEAD(, QCowL2Meta
) cluster_allocs
;
350 uint64_t *refcount_table
;
351 uint64_t refcount_table_offset
;
352 uint32_t refcount_table_size
;
353 uint32_t max_refcount_table_index
; /* Last used entry in refcount_table */
354 uint64_t free_cluster_index
;
355 uint64_t free_byte_offset
;
359 Qcow2CryptoHeaderExtension crypto_header
; /* QCow2 header extension */
360 QCryptoBlockOpenOptions
*crypto_opts
; /* Disk encryption runtime options */
361 QCryptoBlock
*crypto
; /* Disk encryption format driver */
362 bool crypt_physical_offset
; /* Whether to use virtual or physical offset
363 for encryption initialization vector tweak */
364 uint32_t crypt_method_header
;
365 uint64_t snapshots_offset
;
367 unsigned int nb_snapshots
;
368 QCowSnapshot
*snapshots
;
371 uint64_t bitmap_directory_size
;
372 uint64_t bitmap_directory_offset
;
376 bool use_lazy_refcounts
;
379 uint64_t refcount_max
;
381 Qcow2GetRefcountFunc
*get_refcount
;
382 Qcow2SetRefcountFunc
*set_refcount
;
384 bool discard_passthrough
[QCOW2_DISCARD_MAX
];
386 int overlap_check
; /* bitmask of Qcow2MetadataOverlap values */
387 bool signaled_corruption
;
389 uint64_t incompatible_features
;
390 uint64_t compatible_features
;
391 uint64_t autoclear_features
;
393 size_t unknown_header_fields_size
;
394 void* unknown_header_fields
;
395 QLIST_HEAD(, Qcow2UnknownHeaderExtension
) unknown_header_ext
;
396 QTAILQ_HEAD (, Qcow2DiscardRegion
) discards
;
399 /* Backing file path and format as stored in the image (this is not the
400 * effective path/format, which may be the result of a runtime option
402 char *image_backing_file
;
403 char *image_backing_format
;
404 char *image_data_file
;
406 CoQueue thread_task_queue
;
409 BdrvChild
*data_file
;
411 bool metadata_preallocation_checked
;
412 bool metadata_preallocation
;
414 * Compression type used for the image. Default: 0 - ZLIB
415 * The image compression type is set on image creation.
416 * For now, the only way to change the compression type
417 * is to convert the image with the desired compression type set.
419 Qcow2CompressionType compression_type
;
422 typedef struct Qcow2COWRegion
{
424 * Offset of the COW region in bytes from the start of the first cluster
425 * touched by the request.
429 /** Number of bytes to copy */
434 * Describes an in-flight (part of a) write request that writes to clusters
435 * that are not referenced in their L2 table yet.
437 typedef struct QCowL2Meta
439 /** Guest offset of the first newly allocated cluster */
442 /** Host offset of the first newly allocated cluster */
443 uint64_t alloc_offset
;
445 /** Number of newly allocated clusters */
448 /** Do not free the old clusters */
449 bool keep_old_clusters
;
452 * Requests that overlap with this allocation and wait to be restarted
453 * when the allocating request has completed.
455 CoQueue dependent_requests
;
458 * The COW Region between the start of the first allocated cluster and the
459 * area the guest actually writes to.
461 Qcow2COWRegion cow_start
;
464 * The COW Region between the area the guest actually writes to and the
465 * end of the last allocated cluster.
467 Qcow2COWRegion cow_end
;
470 * Indicates that COW regions are already handled and do not require
471 * any more processing.
476 * Indicates that this is not a normal write request but a preallocation.
477 * If the image has extended L2 entries this means that no new individual
478 * subclusters will be marked as allocated in the L2 bitmap (but any
479 * existing contents of that bitmap will be kept).
484 * The I/O vector with the data from the actual guest write request.
485 * If non-NULL, this is meant to be merged together with the data
486 * from @cow_start and @cow_end into one single write operation.
488 QEMUIOVector
*data_qiov
;
489 size_t data_qiov_offset
;
491 /** Pointer to next L2Meta of the same write request */
492 struct QCowL2Meta
*next
;
494 QLIST_ENTRY(QCowL2Meta
) next_in_flight
;
498 * In images with standard L2 entries all clusters are treated as if
499 * they had one subcluster so QCow2ClusterType and QCow2SubclusterType
500 * can be mapped to each other and have the exact same meaning
501 * (QCOW2_SUBCLUSTER_UNALLOCATED_ALLOC cannot happen in these images).
503 * In images with extended L2 entries QCow2ClusterType refers to the
504 * complete cluster and QCow2SubclusterType to each of the individual
505 * subclusters, so there are several possible combinations:
507 * |--------------+---------------------------|
508 * | Cluster type | Possible subcluster types |
509 * |--------------+---------------------------|
510 * | UNALLOCATED | UNALLOCATED_PLAIN |
512 * |--------------+---------------------------|
513 * | NORMAL | UNALLOCATED_ALLOC |
516 * |--------------+---------------------------|
517 * | COMPRESSED | COMPRESSED |
518 * |--------------+---------------------------|
520 * QCOW2_SUBCLUSTER_INVALID means that the L2 entry is incorrect and
521 * the image should be marked corrupt.
524 typedef enum QCow2ClusterType
{
525 QCOW2_CLUSTER_UNALLOCATED
,
526 QCOW2_CLUSTER_ZERO_PLAIN
,
527 QCOW2_CLUSTER_ZERO_ALLOC
,
528 QCOW2_CLUSTER_NORMAL
,
529 QCOW2_CLUSTER_COMPRESSED
,
532 typedef enum QCow2SubclusterType
{
533 QCOW2_SUBCLUSTER_UNALLOCATED_PLAIN
,
534 QCOW2_SUBCLUSTER_UNALLOCATED_ALLOC
,
535 QCOW2_SUBCLUSTER_ZERO_PLAIN
,
536 QCOW2_SUBCLUSTER_ZERO_ALLOC
,
537 QCOW2_SUBCLUSTER_NORMAL
,
538 QCOW2_SUBCLUSTER_COMPRESSED
,
539 QCOW2_SUBCLUSTER_INVALID
,
540 } QCow2SubclusterType
;
542 typedef enum QCow2MetadataOverlap
{
543 QCOW2_OL_MAIN_HEADER_BITNR
= 0,
544 QCOW2_OL_ACTIVE_L1_BITNR
= 1,
545 QCOW2_OL_ACTIVE_L2_BITNR
= 2,
546 QCOW2_OL_REFCOUNT_TABLE_BITNR
= 3,
547 QCOW2_OL_REFCOUNT_BLOCK_BITNR
= 4,
548 QCOW2_OL_SNAPSHOT_TABLE_BITNR
= 5,
549 QCOW2_OL_INACTIVE_L1_BITNR
= 6,
550 QCOW2_OL_INACTIVE_L2_BITNR
= 7,
551 QCOW2_OL_BITMAP_DIRECTORY_BITNR
= 8,
553 QCOW2_OL_MAX_BITNR
= 9,
556 QCOW2_OL_MAIN_HEADER
= (1 << QCOW2_OL_MAIN_HEADER_BITNR
),
557 QCOW2_OL_ACTIVE_L1
= (1 << QCOW2_OL_ACTIVE_L1_BITNR
),
558 QCOW2_OL_ACTIVE_L2
= (1 << QCOW2_OL_ACTIVE_L2_BITNR
),
559 QCOW2_OL_REFCOUNT_TABLE
= (1 << QCOW2_OL_REFCOUNT_TABLE_BITNR
),
560 QCOW2_OL_REFCOUNT_BLOCK
= (1 << QCOW2_OL_REFCOUNT_BLOCK_BITNR
),
561 QCOW2_OL_SNAPSHOT_TABLE
= (1 << QCOW2_OL_SNAPSHOT_TABLE_BITNR
),
562 QCOW2_OL_INACTIVE_L1
= (1 << QCOW2_OL_INACTIVE_L1_BITNR
),
563 /* NOTE: Checking overlaps with inactive L2 tables will result in bdrv
565 QCOW2_OL_INACTIVE_L2
= (1 << QCOW2_OL_INACTIVE_L2_BITNR
),
566 QCOW2_OL_BITMAP_DIRECTORY
= (1 << QCOW2_OL_BITMAP_DIRECTORY_BITNR
),
567 } QCow2MetadataOverlap
;
569 /* Perform all overlap checks which can be done in constant time */
570 #define QCOW2_OL_CONSTANT \
571 (QCOW2_OL_MAIN_HEADER | QCOW2_OL_ACTIVE_L1 | QCOW2_OL_REFCOUNT_TABLE | \
572 QCOW2_OL_SNAPSHOT_TABLE | QCOW2_OL_BITMAP_DIRECTORY)
574 /* Perform all overlap checks which don't require disk access */
575 #define QCOW2_OL_CACHED \
576 (QCOW2_OL_CONSTANT | QCOW2_OL_ACTIVE_L2 | QCOW2_OL_REFCOUNT_BLOCK | \
577 QCOW2_OL_INACTIVE_L1)
579 /* Perform all overlap checks */
580 #define QCOW2_OL_ALL \
581 (QCOW2_OL_CACHED | QCOW2_OL_INACTIVE_L2)
583 #define L1E_OFFSET_MASK 0x00fffffffffffe00ULL
584 #define L2E_OFFSET_MASK 0x00fffffffffffe00ULL
585 #define L2E_COMPRESSED_OFFSET_SIZE_MASK 0x3fffffffffffffffULL
587 #define REFT_OFFSET_MASK 0xfffffffffffffe00ULL
589 #define INV_OFFSET (-1ULL)
591 static inline bool has_subclusters(BDRVQcow2State
*s
)
593 return s
->incompatible_features
& QCOW2_INCOMPAT_EXTL2
;
596 static inline size_t l2_entry_size(BDRVQcow2State
*s
)
598 return has_subclusters(s
) ? L2E_SIZE_EXTENDED
: L2E_SIZE_NORMAL
;
601 static inline uint64_t get_l2_entry(BDRVQcow2State
*s
, uint64_t *l2_slice
,
604 idx
*= l2_entry_size(s
) / sizeof(uint64_t);
605 return be64_to_cpu(l2_slice
[idx
]);
608 static inline uint64_t get_l2_bitmap(BDRVQcow2State
*s
, uint64_t *l2_slice
,
611 if (has_subclusters(s
)) {
612 idx
*= l2_entry_size(s
) / sizeof(uint64_t);
613 return be64_to_cpu(l2_slice
[idx
+ 1]);
615 return 0; /* For convenience only; this value has no meaning. */
619 static inline void set_l2_entry(BDRVQcow2State
*s
, uint64_t *l2_slice
,
620 int idx
, uint64_t entry
)
622 idx
*= l2_entry_size(s
) / sizeof(uint64_t);
623 l2_slice
[idx
] = cpu_to_be64(entry
);
626 static inline void set_l2_bitmap(BDRVQcow2State
*s
, uint64_t *l2_slice
,
627 int idx
, uint64_t bitmap
)
629 assert(has_subclusters(s
));
630 idx
*= l2_entry_size(s
) / sizeof(uint64_t);
631 l2_slice
[idx
+ 1] = cpu_to_be64(bitmap
);
634 static inline bool has_data_file(BlockDriverState
*bs
)
636 BDRVQcow2State
*s
= bs
->opaque
;
637 return (s
->data_file
!= bs
->file
);
640 static inline bool data_file_is_raw(BlockDriverState
*bs
)
642 BDRVQcow2State
*s
= bs
->opaque
;
643 return !!(s
->autoclear_features
& QCOW2_AUTOCLEAR_DATA_FILE_RAW
);
646 static inline int64_t start_of_cluster(BDRVQcow2State
*s
, int64_t offset
)
648 return offset
& ~(s
->cluster_size
- 1);
651 static inline int64_t offset_into_cluster(BDRVQcow2State
*s
, int64_t offset
)
653 return offset
& (s
->cluster_size
- 1);
656 static inline int64_t offset_into_subcluster(BDRVQcow2State
*s
, int64_t offset
)
658 return offset
& (s
->subcluster_size
- 1);
661 static inline uint64_t size_to_clusters(BDRVQcow2State
*s
, uint64_t size
)
663 return (size
+ (s
->cluster_size
- 1)) >> s
->cluster_bits
;
666 static inline uint64_t size_to_subclusters(BDRVQcow2State
*s
, uint64_t size
)
668 return (size
+ (s
->subcluster_size
- 1)) >> s
->subcluster_bits
;
671 static inline int64_t size_to_l1(BDRVQcow2State
*s
, int64_t size
)
673 int shift
= s
->cluster_bits
+ s
->l2_bits
;
674 return (size
+ (1ULL << shift
) - 1) >> shift
;
677 static inline int offset_to_l1_index(BDRVQcow2State
*s
, uint64_t offset
)
679 return offset
>> (s
->l2_bits
+ s
->cluster_bits
);
682 static inline int offset_to_l2_index(BDRVQcow2State
*s
, int64_t offset
)
684 return (offset
>> s
->cluster_bits
) & (s
->l2_size
- 1);
687 static inline int offset_to_l2_slice_index(BDRVQcow2State
*s
, int64_t offset
)
689 return (offset
>> s
->cluster_bits
) & (s
->l2_slice_size
- 1);
692 static inline int offset_to_sc_index(BDRVQcow2State
*s
, int64_t offset
)
694 return (offset
>> s
->subcluster_bits
) & (s
->subclusters_per_cluster
- 1);
697 static inline int64_t qcow2_vm_state_offset(BDRVQcow2State
*s
)
699 return (int64_t)s
->l1_vm_state_index
<< (s
->cluster_bits
+ s
->l2_bits
);
702 static inline QCow2ClusterType
qcow2_get_cluster_type(BlockDriverState
*bs
,
705 BDRVQcow2State
*s
= bs
->opaque
;
707 if (l2_entry
& QCOW_OFLAG_COMPRESSED
) {
708 return QCOW2_CLUSTER_COMPRESSED
;
709 } else if ((l2_entry
& QCOW_OFLAG_ZERO
) && !has_subclusters(s
)) {
710 if (l2_entry
& L2E_OFFSET_MASK
) {
711 return QCOW2_CLUSTER_ZERO_ALLOC
;
713 return QCOW2_CLUSTER_ZERO_PLAIN
;
714 } else if (!(l2_entry
& L2E_OFFSET_MASK
)) {
715 /* Offset 0 generally means unallocated, but it is ambiguous with
716 * external data files because 0 is a valid offset there. However, all
717 * clusters in external data files always have refcount 1, so we can
718 * rely on QCOW_OFLAG_COPIED to disambiguate. */
719 if (has_data_file(bs
) && (l2_entry
& QCOW_OFLAG_COPIED
)) {
720 return QCOW2_CLUSTER_NORMAL
;
722 return QCOW2_CLUSTER_UNALLOCATED
;
725 return QCOW2_CLUSTER_NORMAL
;
730 * In an image without subsclusters @l2_bitmap is ignored and
731 * @sc_index must be 0.
732 * Return QCOW2_SUBCLUSTER_INVALID if an invalid l2 entry is detected
733 * (this checks the whole entry and bitmap, not only the bits related
734 * to subcluster @sc_index).
737 QCow2SubclusterType
qcow2_get_subcluster_type(BlockDriverState
*bs
,
742 BDRVQcow2State
*s
= bs
->opaque
;
743 QCow2ClusterType type
= qcow2_get_cluster_type(bs
, l2_entry
);
744 assert(sc_index
< s
->subclusters_per_cluster
);
746 if (has_subclusters(s
)) {
748 case QCOW2_CLUSTER_COMPRESSED
:
749 return QCOW2_SUBCLUSTER_COMPRESSED
;
750 case QCOW2_CLUSTER_NORMAL
:
751 if ((l2_bitmap
>> 32) & l2_bitmap
) {
752 return QCOW2_SUBCLUSTER_INVALID
;
753 } else if (l2_bitmap
& QCOW_OFLAG_SUB_ZERO(sc_index
)) {
754 return QCOW2_SUBCLUSTER_ZERO_ALLOC
;
755 } else if (l2_bitmap
& QCOW_OFLAG_SUB_ALLOC(sc_index
)) {
756 return QCOW2_SUBCLUSTER_NORMAL
;
758 return QCOW2_SUBCLUSTER_UNALLOCATED_ALLOC
;
760 case QCOW2_CLUSTER_UNALLOCATED
:
761 if (l2_bitmap
& QCOW_L2_BITMAP_ALL_ALLOC
) {
762 return QCOW2_SUBCLUSTER_INVALID
;
763 } else if (l2_bitmap
& QCOW_OFLAG_SUB_ZERO(sc_index
)) {
764 return QCOW2_SUBCLUSTER_ZERO_PLAIN
;
766 return QCOW2_SUBCLUSTER_UNALLOCATED_PLAIN
;
769 g_assert_not_reached();
773 case QCOW2_CLUSTER_COMPRESSED
:
774 return QCOW2_SUBCLUSTER_COMPRESSED
;
775 case QCOW2_CLUSTER_ZERO_PLAIN
:
776 return QCOW2_SUBCLUSTER_ZERO_PLAIN
;
777 case QCOW2_CLUSTER_ZERO_ALLOC
:
778 return QCOW2_SUBCLUSTER_ZERO_ALLOC
;
779 case QCOW2_CLUSTER_NORMAL
:
780 return QCOW2_SUBCLUSTER_NORMAL
;
781 case QCOW2_CLUSTER_UNALLOCATED
:
782 return QCOW2_SUBCLUSTER_UNALLOCATED_PLAIN
;
784 g_assert_not_reached();
789 static inline bool qcow2_cluster_is_allocated(QCow2ClusterType type
)
791 return (type
== QCOW2_CLUSTER_COMPRESSED
|| type
== QCOW2_CLUSTER_NORMAL
||
792 type
== QCOW2_CLUSTER_ZERO_ALLOC
);
795 /* Check whether refcounts are eager or lazy */
796 static inline bool qcow2_need_accurate_refcounts(BDRVQcow2State
*s
)
798 return !(s
->incompatible_features
& QCOW2_INCOMPAT_DIRTY
);
801 static inline uint64_t l2meta_cow_start(QCowL2Meta
*m
)
803 return m
->offset
+ m
->cow_start
.offset
;
806 static inline uint64_t l2meta_cow_end(QCowL2Meta
*m
)
808 return m
->offset
+ m
->cow_end
.offset
+ m
->cow_end
.nb_bytes
;
811 static inline uint64_t refcount_diff(uint64_t r1
, uint64_t r2
)
813 return r1
> r2
? r1
- r2
: r2
- r1
;
817 uint32_t offset_to_reftable_index(BDRVQcow2State
*s
, uint64_t offset
)
819 return offset
>> (s
->refcount_block_bits
+ s
->cluster_bits
);
822 /* qcow2.c functions */
823 int64_t qcow2_refcount_metadata_size(int64_t clusters
, size_t cluster_size
,
824 int refcount_order
, bool generous_increase
,
825 uint64_t *refblock_count
);
827 int qcow2_mark_dirty(BlockDriverState
*bs
);
828 int qcow2_mark_corrupt(BlockDriverState
*bs
);
829 int qcow2_mark_consistent(BlockDriverState
*bs
);
830 int qcow2_update_header(BlockDriverState
*bs
);
832 void qcow2_signal_corruption(BlockDriverState
*bs
, bool fatal
, int64_t offset
,
833 int64_t size
, const char *message_format
, ...)
836 int qcow2_validate_table(BlockDriverState
*bs
, uint64_t offset
,
837 uint64_t entries
, size_t entry_len
,
838 int64_t max_size_bytes
, const char *table_name
,
841 /* qcow2-refcount.c functions */
842 int qcow2_refcount_init(BlockDriverState
*bs
);
843 void qcow2_refcount_close(BlockDriverState
*bs
);
845 int qcow2_get_refcount(BlockDriverState
*bs
, int64_t cluster_index
,
848 int qcow2_update_cluster_refcount(BlockDriverState
*bs
, int64_t cluster_index
,
849 uint64_t addend
, bool decrease
,
850 enum qcow2_discard_type type
);
852 int64_t qcow2_refcount_area(BlockDriverState
*bs
, uint64_t offset
,
853 uint64_t additional_clusters
, bool exact_size
,
854 int new_refblock_index
,
855 uint64_t new_refblock_offset
);
857 int64_t qcow2_alloc_clusters(BlockDriverState
*bs
, uint64_t size
);
858 int64_t qcow2_alloc_clusters_at(BlockDriverState
*bs
, uint64_t offset
,
859 int64_t nb_clusters
);
860 int64_t qcow2_alloc_bytes(BlockDriverState
*bs
, int size
);
861 void qcow2_free_clusters(BlockDriverState
*bs
,
862 int64_t offset
, int64_t size
,
863 enum qcow2_discard_type type
);
864 void qcow2_free_any_cluster(BlockDriverState
*bs
, uint64_t l2_entry
,
865 enum qcow2_discard_type type
);
867 int qcow2_update_snapshot_refcount(BlockDriverState
*bs
,
868 int64_t l1_table_offset
, int l1_size
, int addend
);
870 int coroutine_fn
qcow2_flush_caches(BlockDriverState
*bs
);
871 int coroutine_fn
qcow2_write_caches(BlockDriverState
*bs
);
872 int qcow2_check_refcounts(BlockDriverState
*bs
, BdrvCheckResult
*res
,
875 void qcow2_process_discards(BlockDriverState
*bs
, int ret
);
877 int qcow2_check_metadata_overlap(BlockDriverState
*bs
, int ign
, int64_t offset
,
879 int qcow2_pre_write_overlap_check(BlockDriverState
*bs
, int ign
, int64_t offset
,
880 int64_t size
, bool data_file
);
881 int qcow2_inc_refcounts_imrt(BlockDriverState
*bs
, BdrvCheckResult
*res
,
882 void **refcount_table
,
883 int64_t *refcount_table_size
,
884 int64_t offset
, int64_t size
);
886 int qcow2_change_refcount_order(BlockDriverState
*bs
, int refcount_order
,
887 BlockDriverAmendStatusCB
*status_cb
,
888 void *cb_opaque
, Error
**errp
);
889 int qcow2_shrink_reftable(BlockDriverState
*bs
);
890 int64_t qcow2_get_last_cluster(BlockDriverState
*bs
, int64_t size
);
891 int qcow2_detect_metadata_preallocation(BlockDriverState
*bs
);
893 /* qcow2-cluster.c functions */
894 int qcow2_grow_l1_table(BlockDriverState
*bs
, uint64_t min_size
,
896 int qcow2_shrink_l1_table(BlockDriverState
*bs
, uint64_t max_size
);
897 int qcow2_write_l1_entry(BlockDriverState
*bs
, int l1_index
);
898 int qcow2_encrypt_sectors(BDRVQcow2State
*s
, int64_t sector_num
,
899 uint8_t *buf
, int nb_sectors
, bool enc
, Error
**errp
);
901 int qcow2_get_host_offset(BlockDriverState
*bs
, uint64_t offset
,
902 unsigned int *bytes
, uint64_t *host_offset
,
903 QCow2SubclusterType
*subcluster_type
);
904 int qcow2_alloc_host_offset(BlockDriverState
*bs
, uint64_t offset
,
905 unsigned int *bytes
, uint64_t *host_offset
,
907 int qcow2_alloc_compressed_cluster_offset(BlockDriverState
*bs
,
910 uint64_t *host_offset
);
912 int qcow2_alloc_cluster_link_l2(BlockDriverState
*bs
, QCowL2Meta
*m
);
913 void qcow2_alloc_cluster_abort(BlockDriverState
*bs
, QCowL2Meta
*m
);
914 int qcow2_cluster_discard(BlockDriverState
*bs
, uint64_t offset
,
915 uint64_t bytes
, enum qcow2_discard_type type
,
917 int qcow2_subcluster_zeroize(BlockDriverState
*bs
, uint64_t offset
,
918 uint64_t bytes
, int flags
);
920 int qcow2_expand_zero_clusters(BlockDriverState
*bs
,
921 BlockDriverAmendStatusCB
*status_cb
,
924 /* qcow2-snapshot.c functions */
925 int qcow2_snapshot_create(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
);
926 int qcow2_snapshot_goto(BlockDriverState
*bs
, const char *snapshot_id
);
927 int qcow2_snapshot_delete(BlockDriverState
*bs
,
928 const char *snapshot_id
,
931 int qcow2_snapshot_list(BlockDriverState
*bs
, QEMUSnapshotInfo
**psn_tab
);
932 int qcow2_snapshot_load_tmp(BlockDriverState
*bs
,
933 const char *snapshot_id
,
937 void qcow2_free_snapshots(BlockDriverState
*bs
);
938 int qcow2_read_snapshots(BlockDriverState
*bs
, Error
**errp
);
939 int qcow2_write_snapshots(BlockDriverState
*bs
);
941 int coroutine_fn
qcow2_check_read_snapshot_table(BlockDriverState
*bs
,
942 BdrvCheckResult
*result
,
944 int coroutine_fn
qcow2_check_fix_snapshot_table(BlockDriverState
*bs
,
945 BdrvCheckResult
*result
,
948 /* qcow2-cache.c functions */
949 Qcow2Cache
*qcow2_cache_create(BlockDriverState
*bs
, int num_tables
,
950 unsigned table_size
);
951 int qcow2_cache_destroy(Qcow2Cache
*c
);
953 void qcow2_cache_entry_mark_dirty(Qcow2Cache
*c
, void *table
);
954 int qcow2_cache_flush(BlockDriverState
*bs
, Qcow2Cache
*c
);
955 int qcow2_cache_write(BlockDriverState
*bs
, Qcow2Cache
*c
);
956 int qcow2_cache_set_dependency(BlockDriverState
*bs
, Qcow2Cache
*c
,
957 Qcow2Cache
*dependency
);
958 void qcow2_cache_depends_on_flush(Qcow2Cache
*c
);
960 void qcow2_cache_clean_unused(Qcow2Cache
*c
);
961 int qcow2_cache_empty(BlockDriverState
*bs
, Qcow2Cache
*c
);
963 int qcow2_cache_get(BlockDriverState
*bs
, Qcow2Cache
*c
, uint64_t offset
,
965 int qcow2_cache_get_empty(BlockDriverState
*bs
, Qcow2Cache
*c
, uint64_t offset
,
967 void qcow2_cache_put(Qcow2Cache
*c
, void **table
);
968 void *qcow2_cache_is_table_offset(Qcow2Cache
*c
, uint64_t offset
);
969 void qcow2_cache_discard(Qcow2Cache
*c
, void *table
);
971 /* qcow2-bitmap.c functions */
972 int qcow2_check_bitmaps_refcounts(BlockDriverState
*bs
, BdrvCheckResult
*res
,
973 void **refcount_table
,
974 int64_t *refcount_table_size
);
975 bool qcow2_load_dirty_bitmaps(BlockDriverState
*bs
, Error
**errp
);
976 Qcow2BitmapInfoList
*qcow2_get_bitmap_info_list(BlockDriverState
*bs
,
978 int qcow2_reopen_bitmaps_rw(BlockDriverState
*bs
, Error
**errp
);
979 int qcow2_truncate_bitmaps_check(BlockDriverState
*bs
, Error
**errp
);
980 void qcow2_store_persistent_dirty_bitmaps(BlockDriverState
*bs
,
981 bool release_stored
, Error
**errp
);
982 int qcow2_reopen_bitmaps_ro(BlockDriverState
*bs
, Error
**errp
);
983 bool qcow2_co_can_store_new_dirty_bitmap(BlockDriverState
*bs
,
985 uint32_t granularity
,
987 int qcow2_co_remove_persistent_dirty_bitmap(BlockDriverState
*bs
,
990 bool qcow2_supports_persistent_dirty_bitmap(BlockDriverState
*bs
);
991 uint64_t qcow2_get_persistent_dirty_bitmap_size(BlockDriverState
*bs
,
992 uint32_t cluster_size
);
995 qcow2_co_compress(BlockDriverState
*bs
, void *dest
, size_t dest_size
,
996 const void *src
, size_t src_size
);
998 qcow2_co_decompress(BlockDriverState
*bs
, void *dest
, size_t dest_size
,
999 const void *src
, size_t src_size
);
1001 qcow2_co_encrypt(BlockDriverState
*bs
, uint64_t host_offset
,
1002 uint64_t guest_offset
, void *buf
, size_t len
);
1004 qcow2_co_decrypt(BlockDriverState
*bs
, uint64_t host_offset
,
1005 uint64_t guest_offset
, void *buf
, size_t len
);