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 /* Bitmap header extension constraints */
65 #define QCOW2_MAX_BITMAPS 65535
66 #define QCOW2_MAX_BITMAP_DIRECTORY_SIZE (1024 * QCOW2_MAX_BITMAPS)
68 /* Maximum of parallel sub-request per guest request */
69 #define QCOW2_MAX_WORKERS 8
71 /* indicate that the refcount of the referenced cluster is exactly one. */
72 #define QCOW_OFLAG_COPIED (1ULL << 63)
73 /* indicate that the cluster is compressed (they never have the copied flag) */
74 #define QCOW_OFLAG_COMPRESSED (1ULL << 62)
75 /* The cluster reads as all zeros */
76 #define QCOW_OFLAG_ZERO (1ULL << 0)
78 #define MIN_CLUSTER_BITS 9
79 #define MAX_CLUSTER_BITS 21
81 /* Defined in the qcow2 spec (compressed cluster descriptor) */
82 #define QCOW2_COMPRESSED_SECTOR_SIZE 512U
83 #define QCOW2_COMPRESSED_SECTOR_MASK (~(QCOW2_COMPRESSED_SECTOR_SIZE - 1))
85 /* Must be at least 2 to cover COW */
86 #define MIN_L2_CACHE_SIZE 2 /* cache entries */
88 /* Must be at least 4 to cover all cases of refcount table growth */
89 #define MIN_REFCOUNT_CACHE_SIZE 4 /* clusters */
92 #define DEFAULT_L2_CACHE_MAX_SIZE (32 * MiB)
93 #define DEFAULT_CACHE_CLEAN_INTERVAL 600 /* seconds */
95 #define DEFAULT_L2_CACHE_MAX_SIZE (8 * MiB)
96 /* Cache clean interval is currently available only on Linux, so must be 0 */
97 #define DEFAULT_CACHE_CLEAN_INTERVAL 0
100 #define DEFAULT_CLUSTER_SIZE 65536
102 #define QCOW2_OPT_DATA_FILE "data-file"
103 #define QCOW2_OPT_LAZY_REFCOUNTS "lazy-refcounts"
104 #define QCOW2_OPT_DISCARD_REQUEST "pass-discard-request"
105 #define QCOW2_OPT_DISCARD_SNAPSHOT "pass-discard-snapshot"
106 #define QCOW2_OPT_DISCARD_OTHER "pass-discard-other"
107 #define QCOW2_OPT_OVERLAP "overlap-check"
108 #define QCOW2_OPT_OVERLAP_TEMPLATE "overlap-check.template"
109 #define QCOW2_OPT_OVERLAP_MAIN_HEADER "overlap-check.main-header"
110 #define QCOW2_OPT_OVERLAP_ACTIVE_L1 "overlap-check.active-l1"
111 #define QCOW2_OPT_OVERLAP_ACTIVE_L2 "overlap-check.active-l2"
112 #define QCOW2_OPT_OVERLAP_REFCOUNT_TABLE "overlap-check.refcount-table"
113 #define QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK "overlap-check.refcount-block"
114 #define QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE "overlap-check.snapshot-table"
115 #define QCOW2_OPT_OVERLAP_INACTIVE_L1 "overlap-check.inactive-l1"
116 #define QCOW2_OPT_OVERLAP_INACTIVE_L2 "overlap-check.inactive-l2"
117 #define QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY "overlap-check.bitmap-directory"
118 #define QCOW2_OPT_CACHE_SIZE "cache-size"
119 #define QCOW2_OPT_L2_CACHE_SIZE "l2-cache-size"
120 #define QCOW2_OPT_L2_CACHE_ENTRY_SIZE "l2-cache-entry-size"
121 #define QCOW2_OPT_REFCOUNT_CACHE_SIZE "refcount-cache-size"
122 #define QCOW2_OPT_CACHE_CLEAN_INTERVAL "cache-clean-interval"
124 typedef struct QCowHeader
{
127 uint64_t backing_file_offset
;
128 uint32_t backing_file_size
;
129 uint32_t cluster_bits
;
130 uint64_t size
; /* in bytes */
131 uint32_t crypt_method
;
132 uint32_t l1_size
; /* XXX: save number of clusters instead ? */
133 uint64_t l1_table_offset
;
134 uint64_t refcount_table_offset
;
135 uint32_t refcount_table_clusters
;
136 uint32_t nb_snapshots
;
137 uint64_t snapshots_offset
;
139 /* The following fields are only valid for version >= 3 */
140 uint64_t incompatible_features
;
141 uint64_t compatible_features
;
142 uint64_t autoclear_features
;
144 uint32_t refcount_order
;
145 uint32_t header_length
;
146 } QEMU_PACKED QCowHeader
;
148 typedef struct QEMU_PACKED QCowSnapshotHeader
{
149 /* header is 8 byte aligned */
150 uint64_t l1_table_offset
;
153 uint16_t id_str_size
;
159 uint64_t vm_clock_nsec
;
161 uint32_t vm_state_size
;
162 uint32_t extra_data_size
; /* for extension */
163 /* extra data follows */
166 } QCowSnapshotHeader
;
168 typedef struct QEMU_PACKED QCowSnapshotExtraData
{
169 uint64_t vm_state_size_large
;
171 } QCowSnapshotExtraData
;
174 typedef struct QCowSnapshot
{
175 uint64_t l1_table_offset
;
180 uint64_t vm_state_size
;
183 uint64_t vm_clock_nsec
;
187 typedef struct Qcow2Cache Qcow2Cache
;
189 typedef struct Qcow2CryptoHeaderExtension
{
192 } QEMU_PACKED Qcow2CryptoHeaderExtension
;
194 typedef struct Qcow2UnknownHeaderExtension
{
197 QLIST_ENTRY(Qcow2UnknownHeaderExtension
) next
;
199 } Qcow2UnknownHeaderExtension
;
202 QCOW2_FEAT_TYPE_INCOMPATIBLE
= 0,
203 QCOW2_FEAT_TYPE_COMPATIBLE
= 1,
204 QCOW2_FEAT_TYPE_AUTOCLEAR
= 2,
207 /* Incompatible feature bits */
209 QCOW2_INCOMPAT_DIRTY_BITNR
= 0,
210 QCOW2_INCOMPAT_CORRUPT_BITNR
= 1,
211 QCOW2_INCOMPAT_DATA_FILE_BITNR
= 2,
212 QCOW2_INCOMPAT_DIRTY
= 1 << QCOW2_INCOMPAT_DIRTY_BITNR
,
213 QCOW2_INCOMPAT_CORRUPT
= 1 << QCOW2_INCOMPAT_CORRUPT_BITNR
,
214 QCOW2_INCOMPAT_DATA_FILE
= 1 << QCOW2_INCOMPAT_DATA_FILE_BITNR
,
216 QCOW2_INCOMPAT_MASK
= QCOW2_INCOMPAT_DIRTY
217 | QCOW2_INCOMPAT_CORRUPT
218 | QCOW2_INCOMPAT_DATA_FILE
,
221 /* Compatible feature bits */
223 QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR
= 0,
224 QCOW2_COMPAT_LAZY_REFCOUNTS
= 1 << QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR
,
226 QCOW2_COMPAT_FEAT_MASK
= QCOW2_COMPAT_LAZY_REFCOUNTS
,
229 /* Autoclear feature bits */
231 QCOW2_AUTOCLEAR_BITMAPS_BITNR
= 0,
232 QCOW2_AUTOCLEAR_DATA_FILE_RAW_BITNR
= 1,
233 QCOW2_AUTOCLEAR_BITMAPS
= 1 << QCOW2_AUTOCLEAR_BITMAPS_BITNR
,
234 QCOW2_AUTOCLEAR_DATA_FILE_RAW
= 1 << QCOW2_AUTOCLEAR_DATA_FILE_RAW_BITNR
,
236 QCOW2_AUTOCLEAR_MASK
= QCOW2_AUTOCLEAR_BITMAPS
237 | QCOW2_AUTOCLEAR_DATA_FILE_RAW
,
240 enum qcow2_discard_type
{
241 QCOW2_DISCARD_NEVER
= 0,
242 QCOW2_DISCARD_ALWAYS
,
243 QCOW2_DISCARD_REQUEST
,
244 QCOW2_DISCARD_SNAPSHOT
,
249 typedef struct Qcow2Feature
{
253 } QEMU_PACKED Qcow2Feature
;
255 typedef struct Qcow2DiscardRegion
{
256 BlockDriverState
*bs
;
259 QTAILQ_ENTRY(Qcow2DiscardRegion
) next
;
260 } Qcow2DiscardRegion
;
262 typedef uint64_t Qcow2GetRefcountFunc(const void *refcount_array
,
264 typedef void Qcow2SetRefcountFunc(void *refcount_array
,
265 uint64_t index
, uint64_t value
);
267 typedef struct Qcow2BitmapHeaderExt
{
270 uint64_t bitmap_directory_size
;
271 uint64_t bitmap_directory_offset
;
272 } QEMU_PACKED Qcow2BitmapHeaderExt
;
274 #define QCOW2_MAX_THREADS 4
276 typedef struct BDRVQcow2State
{
283 int l1_vm_state_index
;
284 int refcount_block_bits
;
285 int refcount_block_size
;
288 uint64_t cluster_offset_mask
;
289 uint64_t l1_table_offset
;
292 Qcow2Cache
* l2_table_cache
;
293 Qcow2Cache
* refcount_block_cache
;
294 QEMUTimer
*cache_clean_timer
;
295 unsigned cache_clean_interval
;
297 uint8_t *cluster_cache
;
298 uint8_t *cluster_data
;
299 uint64_t cluster_cache_offset
;
300 QLIST_HEAD(, QCowL2Meta
) cluster_allocs
;
302 uint64_t *refcount_table
;
303 uint64_t refcount_table_offset
;
304 uint32_t refcount_table_size
;
305 uint32_t max_refcount_table_index
; /* Last used entry in refcount_table */
306 uint64_t free_cluster_index
;
307 uint64_t free_byte_offset
;
311 Qcow2CryptoHeaderExtension crypto_header
; /* QCow2 header extension */
312 QCryptoBlockOpenOptions
*crypto_opts
; /* Disk encryption runtime options */
313 QCryptoBlock
*crypto
; /* Disk encryption format driver */
314 bool crypt_physical_offset
; /* Whether to use virtual or physical offset
315 for encryption initialization vector tweak */
316 uint32_t crypt_method_header
;
317 uint64_t snapshots_offset
;
319 unsigned int nb_snapshots
;
320 QCowSnapshot
*snapshots
;
323 uint64_t bitmap_directory_size
;
324 uint64_t bitmap_directory_offset
;
328 bool use_lazy_refcounts
;
331 uint64_t refcount_max
;
333 Qcow2GetRefcountFunc
*get_refcount
;
334 Qcow2SetRefcountFunc
*set_refcount
;
336 bool discard_passthrough
[QCOW2_DISCARD_MAX
];
338 int overlap_check
; /* bitmask of Qcow2MetadataOverlap values */
339 bool signaled_corruption
;
341 uint64_t incompatible_features
;
342 uint64_t compatible_features
;
343 uint64_t autoclear_features
;
345 size_t unknown_header_fields_size
;
346 void* unknown_header_fields
;
347 QLIST_HEAD(, Qcow2UnknownHeaderExtension
) unknown_header_ext
;
348 QTAILQ_HEAD (, Qcow2DiscardRegion
) discards
;
351 /* Backing file path and format as stored in the image (this is not the
352 * effective path/format, which may be the result of a runtime option
354 char *image_backing_file
;
355 char *image_backing_format
;
356 char *image_data_file
;
358 CoQueue thread_task_queue
;
361 BdrvChild
*data_file
;
363 bool metadata_preallocation_checked
;
364 bool metadata_preallocation
;
367 typedef struct Qcow2COWRegion
{
369 * Offset of the COW region in bytes from the start of the first cluster
370 * touched by the request.
374 /** Number of bytes to copy */
379 * Describes an in-flight (part of a) write request that writes to clusters
380 * that are not referenced in their L2 table yet.
382 typedef struct QCowL2Meta
384 /** Guest offset of the first newly allocated cluster */
387 /** Host offset of the first newly allocated cluster */
388 uint64_t alloc_offset
;
390 /** Number of newly allocated clusters */
393 /** Do not free the old clusters */
394 bool keep_old_clusters
;
397 * Requests that overlap with this allocation and wait to be restarted
398 * when the allocating request has completed.
400 CoQueue dependent_requests
;
403 * The COW Region between the start of the first allocated cluster and the
404 * area the guest actually writes to.
406 Qcow2COWRegion cow_start
;
409 * The COW Region between the area the guest actually writes to and the
410 * end of the last allocated cluster.
412 Qcow2COWRegion cow_end
;
415 * Indicates that COW regions are already handled and do not require
416 * any more processing.
421 * The I/O vector with the data from the actual guest write request.
422 * If non-NULL, this is meant to be merged together with the data
423 * from @cow_start and @cow_end into one single write operation.
425 QEMUIOVector
*data_qiov
;
426 size_t data_qiov_offset
;
428 /** Pointer to next L2Meta of the same write request */
429 struct QCowL2Meta
*next
;
431 QLIST_ENTRY(QCowL2Meta
) next_in_flight
;
434 typedef enum QCow2ClusterType
{
435 QCOW2_CLUSTER_UNALLOCATED
,
436 QCOW2_CLUSTER_ZERO_PLAIN
,
437 QCOW2_CLUSTER_ZERO_ALLOC
,
438 QCOW2_CLUSTER_NORMAL
,
439 QCOW2_CLUSTER_COMPRESSED
,
442 typedef enum QCow2MetadataOverlap
{
443 QCOW2_OL_MAIN_HEADER_BITNR
= 0,
444 QCOW2_OL_ACTIVE_L1_BITNR
= 1,
445 QCOW2_OL_ACTIVE_L2_BITNR
= 2,
446 QCOW2_OL_REFCOUNT_TABLE_BITNR
= 3,
447 QCOW2_OL_REFCOUNT_BLOCK_BITNR
= 4,
448 QCOW2_OL_SNAPSHOT_TABLE_BITNR
= 5,
449 QCOW2_OL_INACTIVE_L1_BITNR
= 6,
450 QCOW2_OL_INACTIVE_L2_BITNR
= 7,
451 QCOW2_OL_BITMAP_DIRECTORY_BITNR
= 8,
453 QCOW2_OL_MAX_BITNR
= 9,
456 QCOW2_OL_MAIN_HEADER
= (1 << QCOW2_OL_MAIN_HEADER_BITNR
),
457 QCOW2_OL_ACTIVE_L1
= (1 << QCOW2_OL_ACTIVE_L1_BITNR
),
458 QCOW2_OL_ACTIVE_L2
= (1 << QCOW2_OL_ACTIVE_L2_BITNR
),
459 QCOW2_OL_REFCOUNT_TABLE
= (1 << QCOW2_OL_REFCOUNT_TABLE_BITNR
),
460 QCOW2_OL_REFCOUNT_BLOCK
= (1 << QCOW2_OL_REFCOUNT_BLOCK_BITNR
),
461 QCOW2_OL_SNAPSHOT_TABLE
= (1 << QCOW2_OL_SNAPSHOT_TABLE_BITNR
),
462 QCOW2_OL_INACTIVE_L1
= (1 << QCOW2_OL_INACTIVE_L1_BITNR
),
463 /* NOTE: Checking overlaps with inactive L2 tables will result in bdrv
465 QCOW2_OL_INACTIVE_L2
= (1 << QCOW2_OL_INACTIVE_L2_BITNR
),
466 QCOW2_OL_BITMAP_DIRECTORY
= (1 << QCOW2_OL_BITMAP_DIRECTORY_BITNR
),
467 } QCow2MetadataOverlap
;
469 /* Perform all overlap checks which can be done in constant time */
470 #define QCOW2_OL_CONSTANT \
471 (QCOW2_OL_MAIN_HEADER | QCOW2_OL_ACTIVE_L1 | QCOW2_OL_REFCOUNT_TABLE | \
472 QCOW2_OL_SNAPSHOT_TABLE | QCOW2_OL_BITMAP_DIRECTORY)
474 /* Perform all overlap checks which don't require disk access */
475 #define QCOW2_OL_CACHED \
476 (QCOW2_OL_CONSTANT | QCOW2_OL_ACTIVE_L2 | QCOW2_OL_REFCOUNT_BLOCK | \
477 QCOW2_OL_INACTIVE_L1)
479 /* Perform all overlap checks */
480 #define QCOW2_OL_ALL \
481 (QCOW2_OL_CACHED | QCOW2_OL_INACTIVE_L2)
483 #define L1E_OFFSET_MASK 0x00fffffffffffe00ULL
484 #define L2E_OFFSET_MASK 0x00fffffffffffe00ULL
485 #define L2E_COMPRESSED_OFFSET_SIZE_MASK 0x3fffffffffffffffULL
487 #define REFT_OFFSET_MASK 0xfffffffffffffe00ULL
489 #define INV_OFFSET (-1ULL)
491 static inline bool has_data_file(BlockDriverState
*bs
)
493 BDRVQcow2State
*s
= bs
->opaque
;
494 return (s
->data_file
!= bs
->file
);
497 static inline bool data_file_is_raw(BlockDriverState
*bs
)
499 BDRVQcow2State
*s
= bs
->opaque
;
500 return !!(s
->autoclear_features
& QCOW2_AUTOCLEAR_DATA_FILE_RAW
);
503 static inline int64_t start_of_cluster(BDRVQcow2State
*s
, int64_t offset
)
505 return offset
& ~(s
->cluster_size
- 1);
508 static inline int64_t offset_into_cluster(BDRVQcow2State
*s
, int64_t offset
)
510 return offset
& (s
->cluster_size
- 1);
513 static inline uint64_t size_to_clusters(BDRVQcow2State
*s
, uint64_t size
)
515 return (size
+ (s
->cluster_size
- 1)) >> s
->cluster_bits
;
518 static inline int64_t size_to_l1(BDRVQcow2State
*s
, int64_t size
)
520 int shift
= s
->cluster_bits
+ s
->l2_bits
;
521 return (size
+ (1ULL << shift
) - 1) >> shift
;
524 static inline int offset_to_l1_index(BDRVQcow2State
*s
, uint64_t offset
)
526 return offset
>> (s
->l2_bits
+ s
->cluster_bits
);
529 static inline int offset_to_l2_index(BDRVQcow2State
*s
, int64_t offset
)
531 return (offset
>> s
->cluster_bits
) & (s
->l2_size
- 1);
534 static inline int offset_to_l2_slice_index(BDRVQcow2State
*s
, int64_t offset
)
536 return (offset
>> s
->cluster_bits
) & (s
->l2_slice_size
- 1);
539 static inline int64_t qcow2_vm_state_offset(BDRVQcow2State
*s
)
541 return (int64_t)s
->l1_vm_state_index
<< (s
->cluster_bits
+ s
->l2_bits
);
544 static inline QCow2ClusterType
qcow2_get_cluster_type(BlockDriverState
*bs
,
547 if (l2_entry
& QCOW_OFLAG_COMPRESSED
) {
548 return QCOW2_CLUSTER_COMPRESSED
;
549 } else if (l2_entry
& QCOW_OFLAG_ZERO
) {
550 if (l2_entry
& L2E_OFFSET_MASK
) {
551 return QCOW2_CLUSTER_ZERO_ALLOC
;
553 return QCOW2_CLUSTER_ZERO_PLAIN
;
554 } else if (!(l2_entry
& L2E_OFFSET_MASK
)) {
555 /* Offset 0 generally means unallocated, but it is ambiguous with
556 * external data files because 0 is a valid offset there. However, all
557 * clusters in external data files always have refcount 1, so we can
558 * rely on QCOW_OFLAG_COPIED to disambiguate. */
559 if (has_data_file(bs
) && (l2_entry
& QCOW_OFLAG_COPIED
)) {
560 return QCOW2_CLUSTER_NORMAL
;
562 return QCOW2_CLUSTER_UNALLOCATED
;
565 return QCOW2_CLUSTER_NORMAL
;
569 /* Check whether refcounts are eager or lazy */
570 static inline bool qcow2_need_accurate_refcounts(BDRVQcow2State
*s
)
572 return !(s
->incompatible_features
& QCOW2_INCOMPAT_DIRTY
);
575 static inline uint64_t l2meta_cow_start(QCowL2Meta
*m
)
577 return m
->offset
+ m
->cow_start
.offset
;
580 static inline uint64_t l2meta_cow_end(QCowL2Meta
*m
)
582 return m
->offset
+ m
->cow_end
.offset
+ m
->cow_end
.nb_bytes
;
585 static inline uint64_t refcount_diff(uint64_t r1
, uint64_t r2
)
587 return r1
> r2
? r1
- r2
: r2
- r1
;
591 uint32_t offset_to_reftable_index(BDRVQcow2State
*s
, uint64_t offset
)
593 return offset
>> (s
->refcount_block_bits
+ s
->cluster_bits
);
596 /* qcow2.c functions */
597 int64_t qcow2_refcount_metadata_size(int64_t clusters
, size_t cluster_size
,
598 int refcount_order
, bool generous_increase
,
599 uint64_t *refblock_count
);
601 int qcow2_mark_dirty(BlockDriverState
*bs
);
602 int qcow2_mark_corrupt(BlockDriverState
*bs
);
603 int qcow2_mark_consistent(BlockDriverState
*bs
);
604 int qcow2_update_header(BlockDriverState
*bs
);
606 void qcow2_signal_corruption(BlockDriverState
*bs
, bool fatal
, int64_t offset
,
607 int64_t size
, const char *message_format
, ...)
610 int qcow2_validate_table(BlockDriverState
*bs
, uint64_t offset
,
611 uint64_t entries
, size_t entry_len
,
612 int64_t max_size_bytes
, const char *table_name
,
615 /* qcow2-refcount.c functions */
616 int qcow2_refcount_init(BlockDriverState
*bs
);
617 void qcow2_refcount_close(BlockDriverState
*bs
);
619 int qcow2_get_refcount(BlockDriverState
*bs
, int64_t cluster_index
,
622 int qcow2_update_cluster_refcount(BlockDriverState
*bs
, int64_t cluster_index
,
623 uint64_t addend
, bool decrease
,
624 enum qcow2_discard_type type
);
626 int64_t qcow2_refcount_area(BlockDriverState
*bs
, uint64_t offset
,
627 uint64_t additional_clusters
, bool exact_size
,
628 int new_refblock_index
,
629 uint64_t new_refblock_offset
);
631 int64_t qcow2_alloc_clusters(BlockDriverState
*bs
, uint64_t size
);
632 int64_t qcow2_alloc_clusters_at(BlockDriverState
*bs
, uint64_t offset
,
633 int64_t nb_clusters
);
634 int64_t qcow2_alloc_bytes(BlockDriverState
*bs
, int size
);
635 void qcow2_free_clusters(BlockDriverState
*bs
,
636 int64_t offset
, int64_t size
,
637 enum qcow2_discard_type type
);
638 void qcow2_free_any_clusters(BlockDriverState
*bs
, uint64_t l2_entry
,
639 int nb_clusters
, enum qcow2_discard_type type
);
641 int qcow2_update_snapshot_refcount(BlockDriverState
*bs
,
642 int64_t l1_table_offset
, int l1_size
, int addend
);
644 int coroutine_fn
qcow2_flush_caches(BlockDriverState
*bs
);
645 int coroutine_fn
qcow2_write_caches(BlockDriverState
*bs
);
646 int qcow2_check_refcounts(BlockDriverState
*bs
, BdrvCheckResult
*res
,
649 void qcow2_process_discards(BlockDriverState
*bs
, int ret
);
651 int qcow2_check_metadata_overlap(BlockDriverState
*bs
, int ign
, int64_t offset
,
653 int qcow2_pre_write_overlap_check(BlockDriverState
*bs
, int ign
, int64_t offset
,
654 int64_t size
, bool data_file
);
655 int qcow2_inc_refcounts_imrt(BlockDriverState
*bs
, BdrvCheckResult
*res
,
656 void **refcount_table
,
657 int64_t *refcount_table_size
,
658 int64_t offset
, int64_t size
);
660 int qcow2_change_refcount_order(BlockDriverState
*bs
, int refcount_order
,
661 BlockDriverAmendStatusCB
*status_cb
,
662 void *cb_opaque
, Error
**errp
);
663 int qcow2_shrink_reftable(BlockDriverState
*bs
);
664 int64_t qcow2_get_last_cluster(BlockDriverState
*bs
, int64_t size
);
665 int qcow2_detect_metadata_preallocation(BlockDriverState
*bs
);
667 /* qcow2-cluster.c functions */
668 int qcow2_grow_l1_table(BlockDriverState
*bs
, uint64_t min_size
,
670 int qcow2_shrink_l1_table(BlockDriverState
*bs
, uint64_t max_size
);
671 int qcow2_write_l1_entry(BlockDriverState
*bs
, int l1_index
);
672 int qcow2_encrypt_sectors(BDRVQcow2State
*s
, int64_t sector_num
,
673 uint8_t *buf
, int nb_sectors
, bool enc
, Error
**errp
);
675 int qcow2_get_cluster_offset(BlockDriverState
*bs
, uint64_t offset
,
676 unsigned int *bytes
, uint64_t *cluster_offset
);
677 int qcow2_alloc_cluster_offset(BlockDriverState
*bs
, uint64_t offset
,
678 unsigned int *bytes
, uint64_t *host_offset
,
680 int qcow2_alloc_compressed_cluster_offset(BlockDriverState
*bs
,
683 uint64_t *host_offset
);
685 int qcow2_alloc_cluster_link_l2(BlockDriverState
*bs
, QCowL2Meta
*m
);
686 void qcow2_alloc_cluster_abort(BlockDriverState
*bs
, QCowL2Meta
*m
);
687 int qcow2_cluster_discard(BlockDriverState
*bs
, uint64_t offset
,
688 uint64_t bytes
, enum qcow2_discard_type type
,
690 int qcow2_cluster_zeroize(BlockDriverState
*bs
, uint64_t offset
,
691 uint64_t bytes
, int flags
);
693 int qcow2_expand_zero_clusters(BlockDriverState
*bs
,
694 BlockDriverAmendStatusCB
*status_cb
,
697 /* qcow2-snapshot.c functions */
698 int qcow2_snapshot_create(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
);
699 int qcow2_snapshot_goto(BlockDriverState
*bs
, const char *snapshot_id
);
700 int qcow2_snapshot_delete(BlockDriverState
*bs
,
701 const char *snapshot_id
,
704 int qcow2_snapshot_list(BlockDriverState
*bs
, QEMUSnapshotInfo
**psn_tab
);
705 int qcow2_snapshot_load_tmp(BlockDriverState
*bs
,
706 const char *snapshot_id
,
710 void qcow2_free_snapshots(BlockDriverState
*bs
);
711 int qcow2_read_snapshots(BlockDriverState
*bs
);
713 /* qcow2-cache.c functions */
714 Qcow2Cache
*qcow2_cache_create(BlockDriverState
*bs
, int num_tables
,
715 unsigned table_size
);
716 int qcow2_cache_destroy(Qcow2Cache
*c
);
718 void qcow2_cache_entry_mark_dirty(Qcow2Cache
*c
, void *table
);
719 int qcow2_cache_flush(BlockDriverState
*bs
, Qcow2Cache
*c
);
720 int qcow2_cache_write(BlockDriverState
*bs
, Qcow2Cache
*c
);
721 int qcow2_cache_set_dependency(BlockDriverState
*bs
, Qcow2Cache
*c
,
722 Qcow2Cache
*dependency
);
723 void qcow2_cache_depends_on_flush(Qcow2Cache
*c
);
725 void qcow2_cache_clean_unused(Qcow2Cache
*c
);
726 int qcow2_cache_empty(BlockDriverState
*bs
, Qcow2Cache
*c
);
728 int qcow2_cache_get(BlockDriverState
*bs
, Qcow2Cache
*c
, uint64_t offset
,
730 int qcow2_cache_get_empty(BlockDriverState
*bs
, Qcow2Cache
*c
, uint64_t offset
,
732 void qcow2_cache_put(Qcow2Cache
*c
, void **table
);
733 void *qcow2_cache_is_table_offset(Qcow2Cache
*c
, uint64_t offset
);
734 void qcow2_cache_discard(Qcow2Cache
*c
, void *table
);
736 /* qcow2-bitmap.c functions */
737 int qcow2_check_bitmaps_refcounts(BlockDriverState
*bs
, BdrvCheckResult
*res
,
738 void **refcount_table
,
739 int64_t *refcount_table_size
);
740 bool qcow2_load_dirty_bitmaps(BlockDriverState
*bs
, Error
**errp
);
741 Qcow2BitmapInfoList
*qcow2_get_bitmap_info_list(BlockDriverState
*bs
,
743 int qcow2_reopen_bitmaps_rw_hint(BlockDriverState
*bs
, bool *header_updated
,
745 int qcow2_reopen_bitmaps_rw(BlockDriverState
*bs
, Error
**errp
);
746 int qcow2_truncate_bitmaps_check(BlockDriverState
*bs
, Error
**errp
);
747 void qcow2_store_persistent_dirty_bitmaps(BlockDriverState
*bs
, Error
**errp
);
748 int qcow2_reopen_bitmaps_ro(BlockDriverState
*bs
, Error
**errp
);
749 bool qcow2_can_store_new_dirty_bitmap(BlockDriverState
*bs
,
751 uint32_t granularity
,
753 void qcow2_remove_persistent_dirty_bitmap(BlockDriverState
*bs
,
758 qcow2_co_compress(BlockDriverState
*bs
, void *dest
, size_t dest_size
,
759 const void *src
, size_t src_size
);
761 qcow2_co_decompress(BlockDriverState
*bs
, void *dest
, size_t dest_size
,
762 const void *src
, size_t src_size
);
764 qcow2_co_encrypt(BlockDriverState
*bs
, uint64_t host_offset
,
765 uint64_t guest_offset
, void *buf
, size_t len
);
767 qcow2_co_decrypt(BlockDriverState
*bs
, uint64_t host_offset
,
768 uint64_t guest_offset
, void *buf
, size_t len
);