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 /* indicate that the refcount of the referenced cluster is exactly one. */
69 #define QCOW_OFLAG_COPIED (1ULL << 63)
70 /* indicate that the cluster is compressed (they never have the copied flag) */
71 #define QCOW_OFLAG_COMPRESSED (1ULL << 62)
72 /* The cluster reads as all zeros */
73 #define QCOW_OFLAG_ZERO (1ULL << 0)
75 #define MIN_CLUSTER_BITS 9
76 #define MAX_CLUSTER_BITS 21
78 /* Defined in the qcow2 spec (compressed cluster descriptor) */
79 #define QCOW2_COMPRESSED_SECTOR_SIZE 512U
80 #define QCOW2_COMPRESSED_SECTOR_MASK (~(QCOW2_COMPRESSED_SECTOR_SIZE - 1))
82 /* Must be at least 2 to cover COW */
83 #define MIN_L2_CACHE_SIZE 2 /* cache entries */
85 /* Must be at least 4 to cover all cases of refcount table growth */
86 #define MIN_REFCOUNT_CACHE_SIZE 4 /* clusters */
89 #define DEFAULT_L2_CACHE_MAX_SIZE (32 * MiB)
90 #define DEFAULT_CACHE_CLEAN_INTERVAL 600 /* seconds */
92 #define DEFAULT_L2_CACHE_MAX_SIZE (8 * MiB)
93 /* Cache clean interval is currently available only on Linux, so must be 0 */
94 #define DEFAULT_CACHE_CLEAN_INTERVAL 0
97 #define DEFAULT_CLUSTER_SIZE 65536
99 #define QCOW2_OPT_DATA_FILE "data-file"
100 #define QCOW2_OPT_LAZY_REFCOUNTS "lazy-refcounts"
101 #define QCOW2_OPT_DISCARD_REQUEST "pass-discard-request"
102 #define QCOW2_OPT_DISCARD_SNAPSHOT "pass-discard-snapshot"
103 #define QCOW2_OPT_DISCARD_OTHER "pass-discard-other"
104 #define QCOW2_OPT_OVERLAP "overlap-check"
105 #define QCOW2_OPT_OVERLAP_TEMPLATE "overlap-check.template"
106 #define QCOW2_OPT_OVERLAP_MAIN_HEADER "overlap-check.main-header"
107 #define QCOW2_OPT_OVERLAP_ACTIVE_L1 "overlap-check.active-l1"
108 #define QCOW2_OPT_OVERLAP_ACTIVE_L2 "overlap-check.active-l2"
109 #define QCOW2_OPT_OVERLAP_REFCOUNT_TABLE "overlap-check.refcount-table"
110 #define QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK "overlap-check.refcount-block"
111 #define QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE "overlap-check.snapshot-table"
112 #define QCOW2_OPT_OVERLAP_INACTIVE_L1 "overlap-check.inactive-l1"
113 #define QCOW2_OPT_OVERLAP_INACTIVE_L2 "overlap-check.inactive-l2"
114 #define QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY "overlap-check.bitmap-directory"
115 #define QCOW2_OPT_CACHE_SIZE "cache-size"
116 #define QCOW2_OPT_L2_CACHE_SIZE "l2-cache-size"
117 #define QCOW2_OPT_L2_CACHE_ENTRY_SIZE "l2-cache-entry-size"
118 #define QCOW2_OPT_REFCOUNT_CACHE_SIZE "refcount-cache-size"
119 #define QCOW2_OPT_CACHE_CLEAN_INTERVAL "cache-clean-interval"
121 typedef struct QCowHeader
{
124 uint64_t backing_file_offset
;
125 uint32_t backing_file_size
;
126 uint32_t cluster_bits
;
127 uint64_t size
; /* in bytes */
128 uint32_t crypt_method
;
129 uint32_t l1_size
; /* XXX: save number of clusters instead ? */
130 uint64_t l1_table_offset
;
131 uint64_t refcount_table_offset
;
132 uint32_t refcount_table_clusters
;
133 uint32_t nb_snapshots
;
134 uint64_t snapshots_offset
;
136 /* The following fields are only valid for version >= 3 */
137 uint64_t incompatible_features
;
138 uint64_t compatible_features
;
139 uint64_t autoclear_features
;
141 uint32_t refcount_order
;
142 uint32_t header_length
;
143 } QEMU_PACKED QCowHeader
;
145 typedef struct QEMU_PACKED QCowSnapshotHeader
{
146 /* header is 8 byte aligned */
147 uint64_t l1_table_offset
;
150 uint16_t id_str_size
;
156 uint64_t vm_clock_nsec
;
158 uint32_t vm_state_size
;
159 uint32_t extra_data_size
; /* for extension */
160 /* extra data follows */
163 } QCowSnapshotHeader
;
165 typedef struct QEMU_PACKED QCowSnapshotExtraData
{
166 uint64_t vm_state_size_large
;
168 } QCowSnapshotExtraData
;
171 typedef struct QCowSnapshot
{
172 uint64_t l1_table_offset
;
177 uint64_t vm_state_size
;
180 uint64_t vm_clock_nsec
;
184 typedef struct Qcow2Cache Qcow2Cache
;
186 typedef struct Qcow2CryptoHeaderExtension
{
189 } QEMU_PACKED Qcow2CryptoHeaderExtension
;
191 typedef struct Qcow2UnknownHeaderExtension
{
194 QLIST_ENTRY(Qcow2UnknownHeaderExtension
) next
;
196 } Qcow2UnknownHeaderExtension
;
199 QCOW2_FEAT_TYPE_INCOMPATIBLE
= 0,
200 QCOW2_FEAT_TYPE_COMPATIBLE
= 1,
201 QCOW2_FEAT_TYPE_AUTOCLEAR
= 2,
204 /* Incompatible feature bits */
206 QCOW2_INCOMPAT_DIRTY_BITNR
= 0,
207 QCOW2_INCOMPAT_CORRUPT_BITNR
= 1,
208 QCOW2_INCOMPAT_DATA_FILE_BITNR
= 2,
209 QCOW2_INCOMPAT_DIRTY
= 1 << QCOW2_INCOMPAT_DIRTY_BITNR
,
210 QCOW2_INCOMPAT_CORRUPT
= 1 << QCOW2_INCOMPAT_CORRUPT_BITNR
,
211 QCOW2_INCOMPAT_DATA_FILE
= 1 << QCOW2_INCOMPAT_DATA_FILE_BITNR
,
213 QCOW2_INCOMPAT_MASK
= QCOW2_INCOMPAT_DIRTY
214 | QCOW2_INCOMPAT_CORRUPT
215 | QCOW2_INCOMPAT_DATA_FILE
,
218 /* Compatible feature bits */
220 QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR
= 0,
221 QCOW2_COMPAT_LAZY_REFCOUNTS
= 1 << QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR
,
223 QCOW2_COMPAT_FEAT_MASK
= QCOW2_COMPAT_LAZY_REFCOUNTS
,
226 /* Autoclear feature bits */
228 QCOW2_AUTOCLEAR_BITMAPS_BITNR
= 0,
229 QCOW2_AUTOCLEAR_DATA_FILE_RAW_BITNR
= 1,
230 QCOW2_AUTOCLEAR_BITMAPS
= 1 << QCOW2_AUTOCLEAR_BITMAPS_BITNR
,
231 QCOW2_AUTOCLEAR_DATA_FILE_RAW
= 1 << QCOW2_AUTOCLEAR_DATA_FILE_RAW_BITNR
,
233 QCOW2_AUTOCLEAR_MASK
= QCOW2_AUTOCLEAR_BITMAPS
234 | QCOW2_AUTOCLEAR_DATA_FILE_RAW
,
237 enum qcow2_discard_type
{
238 QCOW2_DISCARD_NEVER
= 0,
239 QCOW2_DISCARD_ALWAYS
,
240 QCOW2_DISCARD_REQUEST
,
241 QCOW2_DISCARD_SNAPSHOT
,
246 typedef struct Qcow2Feature
{
250 } QEMU_PACKED Qcow2Feature
;
252 typedef struct Qcow2DiscardRegion
{
253 BlockDriverState
*bs
;
256 QTAILQ_ENTRY(Qcow2DiscardRegion
) next
;
257 } Qcow2DiscardRegion
;
259 typedef uint64_t Qcow2GetRefcountFunc(const void *refcount_array
,
261 typedef void Qcow2SetRefcountFunc(void *refcount_array
,
262 uint64_t index
, uint64_t value
);
264 typedef struct Qcow2BitmapHeaderExt
{
267 uint64_t bitmap_directory_size
;
268 uint64_t bitmap_directory_offset
;
269 } QEMU_PACKED Qcow2BitmapHeaderExt
;
271 typedef struct BDRVQcow2State
{
278 int l1_vm_state_index
;
279 int refcount_block_bits
;
280 int refcount_block_size
;
283 uint64_t cluster_offset_mask
;
284 uint64_t l1_table_offset
;
287 Qcow2Cache
* l2_table_cache
;
288 Qcow2Cache
* refcount_block_cache
;
289 QEMUTimer
*cache_clean_timer
;
290 unsigned cache_clean_interval
;
292 uint8_t *cluster_cache
;
293 uint8_t *cluster_data
;
294 uint64_t cluster_cache_offset
;
295 QLIST_HEAD(, QCowL2Meta
) cluster_allocs
;
297 uint64_t *refcount_table
;
298 uint64_t refcount_table_offset
;
299 uint32_t refcount_table_size
;
300 uint32_t max_refcount_table_index
; /* Last used entry in refcount_table */
301 uint64_t free_cluster_index
;
302 uint64_t free_byte_offset
;
306 Qcow2CryptoHeaderExtension crypto_header
; /* QCow2 header extension */
307 QCryptoBlockOpenOptions
*crypto_opts
; /* Disk encryption runtime options */
308 QCryptoBlock
*crypto
; /* Disk encryption format driver */
309 bool crypt_physical_offset
; /* Whether to use virtual or physical offset
310 for encryption initialization vector tweak */
311 uint32_t crypt_method_header
;
312 uint64_t snapshots_offset
;
314 unsigned int nb_snapshots
;
315 QCowSnapshot
*snapshots
;
318 uint64_t bitmap_directory_size
;
319 uint64_t bitmap_directory_offset
;
323 bool use_lazy_refcounts
;
326 uint64_t refcount_max
;
328 Qcow2GetRefcountFunc
*get_refcount
;
329 Qcow2SetRefcountFunc
*set_refcount
;
331 bool discard_passthrough
[QCOW2_DISCARD_MAX
];
333 int overlap_check
; /* bitmask of Qcow2MetadataOverlap values */
334 bool signaled_corruption
;
336 uint64_t incompatible_features
;
337 uint64_t compatible_features
;
338 uint64_t autoclear_features
;
340 size_t unknown_header_fields_size
;
341 void* unknown_header_fields
;
342 QLIST_HEAD(, Qcow2UnknownHeaderExtension
) unknown_header_ext
;
343 QTAILQ_HEAD (, Qcow2DiscardRegion
) discards
;
346 /* Backing file path and format as stored in the image (this is not the
347 * effective path/format, which may be the result of a runtime option
349 char *image_backing_file
;
350 char *image_backing_format
;
351 char *image_data_file
;
353 CoQueue compress_wait_queue
;
354 int nb_compress_threads
;
356 BdrvChild
*data_file
;
359 typedef struct Qcow2COWRegion
{
361 * Offset of the COW region in bytes from the start of the first cluster
362 * touched by the request.
366 /** Number of bytes to copy */
371 * Describes an in-flight (part of a) write request that writes to clusters
372 * that are not referenced in their L2 table yet.
374 typedef struct QCowL2Meta
376 /** Guest offset of the first newly allocated cluster */
379 /** Host offset of the first newly allocated cluster */
380 uint64_t alloc_offset
;
382 /** Number of newly allocated clusters */
385 /** Do not free the old clusters */
386 bool keep_old_clusters
;
389 * Requests that overlap with this allocation and wait to be restarted
390 * when the allocating request has completed.
392 CoQueue dependent_requests
;
395 * The COW Region between the start of the first allocated cluster and the
396 * area the guest actually writes to.
398 Qcow2COWRegion cow_start
;
401 * The COW Region between the area the guest actually writes to and the
402 * end of the last allocated cluster.
404 Qcow2COWRegion cow_end
;
407 * The I/O vector with the data from the actual guest write request.
408 * If non-NULL, this is meant to be merged together with the data
409 * from @cow_start and @cow_end into one single write operation.
411 QEMUIOVector
*data_qiov
;
413 /** Pointer to next L2Meta of the same write request */
414 struct QCowL2Meta
*next
;
416 QLIST_ENTRY(QCowL2Meta
) next_in_flight
;
419 typedef enum QCow2ClusterType
{
420 QCOW2_CLUSTER_UNALLOCATED
,
421 QCOW2_CLUSTER_ZERO_PLAIN
,
422 QCOW2_CLUSTER_ZERO_ALLOC
,
423 QCOW2_CLUSTER_NORMAL
,
424 QCOW2_CLUSTER_COMPRESSED
,
427 typedef enum QCow2MetadataOverlap
{
428 QCOW2_OL_MAIN_HEADER_BITNR
= 0,
429 QCOW2_OL_ACTIVE_L1_BITNR
= 1,
430 QCOW2_OL_ACTIVE_L2_BITNR
= 2,
431 QCOW2_OL_REFCOUNT_TABLE_BITNR
= 3,
432 QCOW2_OL_REFCOUNT_BLOCK_BITNR
= 4,
433 QCOW2_OL_SNAPSHOT_TABLE_BITNR
= 5,
434 QCOW2_OL_INACTIVE_L1_BITNR
= 6,
435 QCOW2_OL_INACTIVE_L2_BITNR
= 7,
436 QCOW2_OL_BITMAP_DIRECTORY_BITNR
= 8,
438 QCOW2_OL_MAX_BITNR
= 9,
441 QCOW2_OL_MAIN_HEADER
= (1 << QCOW2_OL_MAIN_HEADER_BITNR
),
442 QCOW2_OL_ACTIVE_L1
= (1 << QCOW2_OL_ACTIVE_L1_BITNR
),
443 QCOW2_OL_ACTIVE_L2
= (1 << QCOW2_OL_ACTIVE_L2_BITNR
),
444 QCOW2_OL_REFCOUNT_TABLE
= (1 << QCOW2_OL_REFCOUNT_TABLE_BITNR
),
445 QCOW2_OL_REFCOUNT_BLOCK
= (1 << QCOW2_OL_REFCOUNT_BLOCK_BITNR
),
446 QCOW2_OL_SNAPSHOT_TABLE
= (1 << QCOW2_OL_SNAPSHOT_TABLE_BITNR
),
447 QCOW2_OL_INACTIVE_L1
= (1 << QCOW2_OL_INACTIVE_L1_BITNR
),
448 /* NOTE: Checking overlaps with inactive L2 tables will result in bdrv
450 QCOW2_OL_INACTIVE_L2
= (1 << QCOW2_OL_INACTIVE_L2_BITNR
),
451 QCOW2_OL_BITMAP_DIRECTORY
= (1 << QCOW2_OL_BITMAP_DIRECTORY_BITNR
),
452 } QCow2MetadataOverlap
;
454 /* Perform all overlap checks which can be done in constant time */
455 #define QCOW2_OL_CONSTANT \
456 (QCOW2_OL_MAIN_HEADER | QCOW2_OL_ACTIVE_L1 | QCOW2_OL_REFCOUNT_TABLE | \
457 QCOW2_OL_SNAPSHOT_TABLE | QCOW2_OL_BITMAP_DIRECTORY)
459 /* Perform all overlap checks which don't require disk access */
460 #define QCOW2_OL_CACHED \
461 (QCOW2_OL_CONSTANT | QCOW2_OL_ACTIVE_L2 | QCOW2_OL_REFCOUNT_BLOCK | \
462 QCOW2_OL_INACTIVE_L1)
464 /* Perform all overlap checks */
465 #define QCOW2_OL_ALL \
466 (QCOW2_OL_CACHED | QCOW2_OL_INACTIVE_L2)
468 #define L1E_OFFSET_MASK 0x00fffffffffffe00ULL
469 #define L2E_OFFSET_MASK 0x00fffffffffffe00ULL
470 #define L2E_COMPRESSED_OFFSET_SIZE_MASK 0x3fffffffffffffffULL
472 #define REFT_OFFSET_MASK 0xfffffffffffffe00ULL
474 #define INV_OFFSET (-1ULL)
476 static inline bool has_data_file(BlockDriverState
*bs
)
478 BDRVQcow2State
*s
= bs
->opaque
;
479 return (s
->data_file
!= bs
->file
);
482 static inline bool data_file_is_raw(BlockDriverState
*bs
)
484 BDRVQcow2State
*s
= bs
->opaque
;
485 return !!(s
->autoclear_features
& QCOW2_AUTOCLEAR_DATA_FILE_RAW
);
488 static inline int64_t start_of_cluster(BDRVQcow2State
*s
, int64_t offset
)
490 return offset
& ~(s
->cluster_size
- 1);
493 static inline int64_t offset_into_cluster(BDRVQcow2State
*s
, int64_t offset
)
495 return offset
& (s
->cluster_size
- 1);
498 static inline uint64_t size_to_clusters(BDRVQcow2State
*s
, uint64_t size
)
500 return (size
+ (s
->cluster_size
- 1)) >> s
->cluster_bits
;
503 static inline int64_t size_to_l1(BDRVQcow2State
*s
, int64_t size
)
505 int shift
= s
->cluster_bits
+ s
->l2_bits
;
506 return (size
+ (1ULL << shift
) - 1) >> shift
;
509 static inline int offset_to_l1_index(BDRVQcow2State
*s
, uint64_t offset
)
511 return offset
>> (s
->l2_bits
+ s
->cluster_bits
);
514 static inline int offset_to_l2_index(BDRVQcow2State
*s
, int64_t offset
)
516 return (offset
>> s
->cluster_bits
) & (s
->l2_size
- 1);
519 static inline int offset_to_l2_slice_index(BDRVQcow2State
*s
, int64_t offset
)
521 return (offset
>> s
->cluster_bits
) & (s
->l2_slice_size
- 1);
524 static inline int64_t qcow2_vm_state_offset(BDRVQcow2State
*s
)
526 return (int64_t)s
->l1_vm_state_index
<< (s
->cluster_bits
+ s
->l2_bits
);
529 static inline QCow2ClusterType
qcow2_get_cluster_type(BlockDriverState
*bs
,
532 if (l2_entry
& QCOW_OFLAG_COMPRESSED
) {
533 return QCOW2_CLUSTER_COMPRESSED
;
534 } else if (l2_entry
& QCOW_OFLAG_ZERO
) {
535 if (l2_entry
& L2E_OFFSET_MASK
) {
536 return QCOW2_CLUSTER_ZERO_ALLOC
;
538 return QCOW2_CLUSTER_ZERO_PLAIN
;
539 } else if (!(l2_entry
& L2E_OFFSET_MASK
)) {
540 /* Offset 0 generally means unallocated, but it is ambiguous with
541 * external data files because 0 is a valid offset there. However, all
542 * clusters in external data files always have refcount 1, so we can
543 * rely on QCOW_OFLAG_COPIED to disambiguate. */
544 if (has_data_file(bs
) && (l2_entry
& QCOW_OFLAG_COPIED
)) {
545 return QCOW2_CLUSTER_NORMAL
;
547 return QCOW2_CLUSTER_UNALLOCATED
;
550 return QCOW2_CLUSTER_NORMAL
;
554 /* Check whether refcounts are eager or lazy */
555 static inline bool qcow2_need_accurate_refcounts(BDRVQcow2State
*s
)
557 return !(s
->incompatible_features
& QCOW2_INCOMPAT_DIRTY
);
560 static inline uint64_t l2meta_cow_start(QCowL2Meta
*m
)
562 return m
->offset
+ m
->cow_start
.offset
;
565 static inline uint64_t l2meta_cow_end(QCowL2Meta
*m
)
567 return m
->offset
+ m
->cow_end
.offset
+ m
->cow_end
.nb_bytes
;
570 static inline uint64_t refcount_diff(uint64_t r1
, uint64_t r2
)
572 return r1
> r2
? r1
- r2
: r2
- r1
;
576 uint32_t offset_to_reftable_index(BDRVQcow2State
*s
, uint64_t offset
)
578 return offset
>> (s
->refcount_block_bits
+ s
->cluster_bits
);
581 /* qcow2.c functions */
582 int64_t qcow2_refcount_metadata_size(int64_t clusters
, size_t cluster_size
,
583 int refcount_order
, bool generous_increase
,
584 uint64_t *refblock_count
);
586 int qcow2_mark_dirty(BlockDriverState
*bs
);
587 int qcow2_mark_corrupt(BlockDriverState
*bs
);
588 int qcow2_mark_consistent(BlockDriverState
*bs
);
589 int qcow2_update_header(BlockDriverState
*bs
);
591 void qcow2_signal_corruption(BlockDriverState
*bs
, bool fatal
, int64_t offset
,
592 int64_t size
, const char *message_format
, ...)
595 int qcow2_validate_table(BlockDriverState
*bs
, uint64_t offset
,
596 uint64_t entries
, size_t entry_len
,
597 int64_t max_size_bytes
, const char *table_name
,
600 /* qcow2-refcount.c functions */
601 int qcow2_refcount_init(BlockDriverState
*bs
);
602 void qcow2_refcount_close(BlockDriverState
*bs
);
604 int qcow2_get_refcount(BlockDriverState
*bs
, int64_t cluster_index
,
607 int qcow2_update_cluster_refcount(BlockDriverState
*bs
, int64_t cluster_index
,
608 uint64_t addend
, bool decrease
,
609 enum qcow2_discard_type type
);
611 int64_t qcow2_refcount_area(BlockDriverState
*bs
, uint64_t offset
,
612 uint64_t additional_clusters
, bool exact_size
,
613 int new_refblock_index
,
614 uint64_t new_refblock_offset
);
616 int64_t qcow2_alloc_clusters(BlockDriverState
*bs
, uint64_t size
);
617 int64_t qcow2_alloc_clusters_at(BlockDriverState
*bs
, uint64_t offset
,
618 int64_t nb_clusters
);
619 int64_t qcow2_alloc_bytes(BlockDriverState
*bs
, int size
);
620 void qcow2_free_clusters(BlockDriverState
*bs
,
621 int64_t offset
, int64_t size
,
622 enum qcow2_discard_type type
);
623 void qcow2_free_any_clusters(BlockDriverState
*bs
, uint64_t l2_entry
,
624 int nb_clusters
, enum qcow2_discard_type type
);
626 int qcow2_update_snapshot_refcount(BlockDriverState
*bs
,
627 int64_t l1_table_offset
, int l1_size
, int addend
);
629 int coroutine_fn
qcow2_flush_caches(BlockDriverState
*bs
);
630 int coroutine_fn
qcow2_write_caches(BlockDriverState
*bs
);
631 int qcow2_check_refcounts(BlockDriverState
*bs
, BdrvCheckResult
*res
,
634 void qcow2_process_discards(BlockDriverState
*bs
, int ret
);
636 int qcow2_check_metadata_overlap(BlockDriverState
*bs
, int ign
, int64_t offset
,
638 int qcow2_pre_write_overlap_check(BlockDriverState
*bs
, int ign
, int64_t offset
,
639 int64_t size
, bool data_file
);
640 int qcow2_inc_refcounts_imrt(BlockDriverState
*bs
, BdrvCheckResult
*res
,
641 void **refcount_table
,
642 int64_t *refcount_table_size
,
643 int64_t offset
, int64_t size
);
645 int qcow2_change_refcount_order(BlockDriverState
*bs
, int refcount_order
,
646 BlockDriverAmendStatusCB
*status_cb
,
647 void *cb_opaque
, Error
**errp
);
648 int qcow2_shrink_reftable(BlockDriverState
*bs
);
649 int64_t qcow2_get_last_cluster(BlockDriverState
*bs
, int64_t size
);
651 /* qcow2-cluster.c functions */
652 int qcow2_grow_l1_table(BlockDriverState
*bs
, uint64_t min_size
,
654 int qcow2_shrink_l1_table(BlockDriverState
*bs
, uint64_t max_size
);
655 int qcow2_write_l1_entry(BlockDriverState
*bs
, int l1_index
);
656 int qcow2_encrypt_sectors(BDRVQcow2State
*s
, int64_t sector_num
,
657 uint8_t *buf
, int nb_sectors
, bool enc
, Error
**errp
);
659 int qcow2_get_cluster_offset(BlockDriverState
*bs
, uint64_t offset
,
660 unsigned int *bytes
, uint64_t *cluster_offset
);
661 int qcow2_alloc_cluster_offset(BlockDriverState
*bs
, uint64_t offset
,
662 unsigned int *bytes
, uint64_t *host_offset
,
664 int qcow2_alloc_compressed_cluster_offset(BlockDriverState
*bs
,
667 uint64_t *host_offset
);
669 int qcow2_alloc_cluster_link_l2(BlockDriverState
*bs
, QCowL2Meta
*m
);
670 void qcow2_alloc_cluster_abort(BlockDriverState
*bs
, QCowL2Meta
*m
);
671 int qcow2_cluster_discard(BlockDriverState
*bs
, uint64_t offset
,
672 uint64_t bytes
, enum qcow2_discard_type type
,
674 int qcow2_cluster_zeroize(BlockDriverState
*bs
, uint64_t offset
,
675 uint64_t bytes
, int flags
);
677 int qcow2_expand_zero_clusters(BlockDriverState
*bs
,
678 BlockDriverAmendStatusCB
*status_cb
,
681 /* qcow2-snapshot.c functions */
682 int qcow2_snapshot_create(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
);
683 int qcow2_snapshot_goto(BlockDriverState
*bs
, const char *snapshot_id
);
684 int qcow2_snapshot_delete(BlockDriverState
*bs
,
685 const char *snapshot_id
,
688 int qcow2_snapshot_list(BlockDriverState
*bs
, QEMUSnapshotInfo
**psn_tab
);
689 int qcow2_snapshot_load_tmp(BlockDriverState
*bs
,
690 const char *snapshot_id
,
694 void qcow2_free_snapshots(BlockDriverState
*bs
);
695 int qcow2_read_snapshots(BlockDriverState
*bs
);
697 /* qcow2-cache.c functions */
698 Qcow2Cache
*qcow2_cache_create(BlockDriverState
*bs
, int num_tables
,
699 unsigned table_size
);
700 int qcow2_cache_destroy(Qcow2Cache
*c
);
702 void qcow2_cache_entry_mark_dirty(Qcow2Cache
*c
, void *table
);
703 int qcow2_cache_flush(BlockDriverState
*bs
, Qcow2Cache
*c
);
704 int qcow2_cache_write(BlockDriverState
*bs
, Qcow2Cache
*c
);
705 int qcow2_cache_set_dependency(BlockDriverState
*bs
, Qcow2Cache
*c
,
706 Qcow2Cache
*dependency
);
707 void qcow2_cache_depends_on_flush(Qcow2Cache
*c
);
709 void qcow2_cache_clean_unused(Qcow2Cache
*c
);
710 int qcow2_cache_empty(BlockDriverState
*bs
, Qcow2Cache
*c
);
712 int qcow2_cache_get(BlockDriverState
*bs
, Qcow2Cache
*c
, uint64_t offset
,
714 int qcow2_cache_get_empty(BlockDriverState
*bs
, Qcow2Cache
*c
, uint64_t offset
,
716 void qcow2_cache_put(Qcow2Cache
*c
, void **table
);
717 void *qcow2_cache_is_table_offset(Qcow2Cache
*c
, uint64_t offset
);
718 void qcow2_cache_discard(Qcow2Cache
*c
, void *table
);
720 /* qcow2-bitmap.c functions */
721 int qcow2_check_bitmaps_refcounts(BlockDriverState
*bs
, BdrvCheckResult
*res
,
722 void **refcount_table
,
723 int64_t *refcount_table_size
);
724 bool qcow2_load_dirty_bitmaps(BlockDriverState
*bs
, Error
**errp
);
725 Qcow2BitmapInfoList
*qcow2_get_bitmap_info_list(BlockDriverState
*bs
,
727 int qcow2_reopen_bitmaps_rw_hint(BlockDriverState
*bs
, bool *header_updated
,
729 int qcow2_reopen_bitmaps_rw(BlockDriverState
*bs
, Error
**errp
);
730 int qcow2_truncate_bitmaps_check(BlockDriverState
*bs
, Error
**errp
);
731 void qcow2_store_persistent_dirty_bitmaps(BlockDriverState
*bs
, Error
**errp
);
732 int qcow2_reopen_bitmaps_ro(BlockDriverState
*bs
, Error
**errp
);
733 bool qcow2_can_store_new_dirty_bitmap(BlockDriverState
*bs
,
735 uint32_t granularity
,
737 void qcow2_remove_persistent_dirty_bitmap(BlockDriverState
*bs
,
742 qcow2_co_compress(BlockDriverState
*bs
, void *dest
, size_t dest_size
,
743 const void *src
, size_t src_size
);
745 qcow2_co_decompress(BlockDriverState
*bs
, void *dest
, size_t dest_size
,
746 const void *src
, size_t src_size
);