qcow2: Return 0/-errno in qcow2_alloc_compressed_cluster_offset()
[qemu/ar7.git] / block / qcow2.h
blobfad6abf6027e4c26d4ddf56cb263f614b3906992
1 /*
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
22 * THE SOFTWARE.
25 #ifndef BLOCK_QCOW2_H
26 #define BLOCK_QCOW2_H
28 #include "crypto/block.h"
29 #include "qemu/coroutine.h"
30 #include "qemu/units.h"
32 //#define DEBUG_ALLOC
33 //#define DEBUG_ALLOC2
34 //#define DEBUG_EXT
36 #define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
38 #define QCOW_CRYPT_NONE 0
39 #define QCOW_CRYPT_AES 1
40 #define QCOW_CRYPT_LUKS 2
42 #define QCOW_MAX_CRYPT_CLUSTERS 32
43 #define QCOW_MAX_SNAPSHOTS 65536
45 /* Field widths in qcow2 mean normal cluster offsets cannot reach
46 * 64PB; depending on cluster size, compressed clusters can have a
47 * smaller limit (64PB for up to 16k clusters, then ramps down to
48 * 512TB for 2M clusters). */
49 #define QCOW_MAX_CLUSTER_OFFSET ((1ULL << 56) - 1)
51 /* 8 MB refcount table is enough for 2 PB images at 64k cluster size
52 * (128 GB for 512 byte clusters, 2 EB for 2 MB clusters) */
53 #define QCOW_MAX_REFTABLE_SIZE (8 * MiB)
55 /* 32 MB L1 table is enough for 2 PB images at 64k cluster size
56 * (128 GB for 512 byte clusters, 2 EB for 2 MB clusters) */
57 #define QCOW_MAX_L1_SIZE (32 * MiB)
59 /* Allow for an average of 1k per snapshot table entry, should be plenty of
60 * space for snapshot names and IDs */
61 #define QCOW_MAX_SNAPSHOTS_SIZE (1024 * QCOW_MAX_SNAPSHOTS)
63 /* Bitmap header extension constraints */
64 #define QCOW2_MAX_BITMAPS 65535
65 #define QCOW2_MAX_BITMAP_DIRECTORY_SIZE (1024 * QCOW2_MAX_BITMAPS)
67 /* indicate that the refcount of the referenced cluster is exactly one. */
68 #define QCOW_OFLAG_COPIED (1ULL << 63)
69 /* indicate that the cluster is compressed (they never have the copied flag) */
70 #define QCOW_OFLAG_COMPRESSED (1ULL << 62)
71 /* The cluster reads as all zeros */
72 #define QCOW_OFLAG_ZERO (1ULL << 0)
74 #define MIN_CLUSTER_BITS 9
75 #define MAX_CLUSTER_BITS 21
77 /* Must be at least 2 to cover COW */
78 #define MIN_L2_CACHE_SIZE 2 /* cache entries */
80 /* Must be at least 4 to cover all cases of refcount table growth */
81 #define MIN_REFCOUNT_CACHE_SIZE 4 /* clusters */
83 #ifdef CONFIG_LINUX
84 #define DEFAULT_L2_CACHE_MAX_SIZE (32 * MiB)
85 #define DEFAULT_CACHE_CLEAN_INTERVAL 600 /* seconds */
86 #else
87 #define DEFAULT_L2_CACHE_MAX_SIZE (8 * MiB)
88 /* Cache clean interval is currently available only on Linux, so must be 0 */
89 #define DEFAULT_CACHE_CLEAN_INTERVAL 0
90 #endif
92 #define DEFAULT_CLUSTER_SIZE 65536
94 #define QCOW2_OPT_LAZY_REFCOUNTS "lazy-refcounts"
95 #define QCOW2_OPT_DISCARD_REQUEST "pass-discard-request"
96 #define QCOW2_OPT_DISCARD_SNAPSHOT "pass-discard-snapshot"
97 #define QCOW2_OPT_DISCARD_OTHER "pass-discard-other"
98 #define QCOW2_OPT_OVERLAP "overlap-check"
99 #define QCOW2_OPT_OVERLAP_TEMPLATE "overlap-check.template"
100 #define QCOW2_OPT_OVERLAP_MAIN_HEADER "overlap-check.main-header"
101 #define QCOW2_OPT_OVERLAP_ACTIVE_L1 "overlap-check.active-l1"
102 #define QCOW2_OPT_OVERLAP_ACTIVE_L2 "overlap-check.active-l2"
103 #define QCOW2_OPT_OVERLAP_REFCOUNT_TABLE "overlap-check.refcount-table"
104 #define QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK "overlap-check.refcount-block"
105 #define QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE "overlap-check.snapshot-table"
106 #define QCOW2_OPT_OVERLAP_INACTIVE_L1 "overlap-check.inactive-l1"
107 #define QCOW2_OPT_OVERLAP_INACTIVE_L2 "overlap-check.inactive-l2"
108 #define QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY "overlap-check.bitmap-directory"
109 #define QCOW2_OPT_CACHE_SIZE "cache-size"
110 #define QCOW2_OPT_L2_CACHE_SIZE "l2-cache-size"
111 #define QCOW2_OPT_L2_CACHE_ENTRY_SIZE "l2-cache-entry-size"
112 #define QCOW2_OPT_REFCOUNT_CACHE_SIZE "refcount-cache-size"
113 #define QCOW2_OPT_CACHE_CLEAN_INTERVAL "cache-clean-interval"
115 typedef struct QCowHeader {
116 uint32_t magic;
117 uint32_t version;
118 uint64_t backing_file_offset;
119 uint32_t backing_file_size;
120 uint32_t cluster_bits;
121 uint64_t size; /* in bytes */
122 uint32_t crypt_method;
123 uint32_t l1_size; /* XXX: save number of clusters instead ? */
124 uint64_t l1_table_offset;
125 uint64_t refcount_table_offset;
126 uint32_t refcount_table_clusters;
127 uint32_t nb_snapshots;
128 uint64_t snapshots_offset;
130 /* The following fields are only valid for version >= 3 */
131 uint64_t incompatible_features;
132 uint64_t compatible_features;
133 uint64_t autoclear_features;
135 uint32_t refcount_order;
136 uint32_t header_length;
137 } QEMU_PACKED QCowHeader;
139 typedef struct QEMU_PACKED QCowSnapshotHeader {
140 /* header is 8 byte aligned */
141 uint64_t l1_table_offset;
143 uint32_t l1_size;
144 uint16_t id_str_size;
145 uint16_t name_size;
147 uint32_t date_sec;
148 uint32_t date_nsec;
150 uint64_t vm_clock_nsec;
152 uint32_t vm_state_size;
153 uint32_t extra_data_size; /* for extension */
154 /* extra data follows */
155 /* id_str follows */
156 /* name follows */
157 } QCowSnapshotHeader;
159 typedef struct QEMU_PACKED QCowSnapshotExtraData {
160 uint64_t vm_state_size_large;
161 uint64_t disk_size;
162 } QCowSnapshotExtraData;
165 typedef struct QCowSnapshot {
166 uint64_t l1_table_offset;
167 uint32_t l1_size;
168 char *id_str;
169 char *name;
170 uint64_t disk_size;
171 uint64_t vm_state_size;
172 uint32_t date_sec;
173 uint32_t date_nsec;
174 uint64_t vm_clock_nsec;
175 } QCowSnapshot;
177 struct Qcow2Cache;
178 typedef struct Qcow2Cache Qcow2Cache;
180 typedef struct Qcow2CryptoHeaderExtension {
181 uint64_t offset;
182 uint64_t length;
183 } QEMU_PACKED Qcow2CryptoHeaderExtension;
185 typedef struct Qcow2UnknownHeaderExtension {
186 uint32_t magic;
187 uint32_t len;
188 QLIST_ENTRY(Qcow2UnknownHeaderExtension) next;
189 uint8_t data[];
190 } Qcow2UnknownHeaderExtension;
192 enum {
193 QCOW2_FEAT_TYPE_INCOMPATIBLE = 0,
194 QCOW2_FEAT_TYPE_COMPATIBLE = 1,
195 QCOW2_FEAT_TYPE_AUTOCLEAR = 2,
198 /* Incompatible feature bits */
199 enum {
200 QCOW2_INCOMPAT_DIRTY_BITNR = 0,
201 QCOW2_INCOMPAT_CORRUPT_BITNR = 1,
202 QCOW2_INCOMPAT_DATA_FILE_BITNR = 2,
203 QCOW2_INCOMPAT_DIRTY = 1 << QCOW2_INCOMPAT_DIRTY_BITNR,
204 QCOW2_INCOMPAT_CORRUPT = 1 << QCOW2_INCOMPAT_CORRUPT_BITNR,
205 QCOW2_INCOMPAT_DATA_FILE = 1 << QCOW2_INCOMPAT_DATA_FILE_BITNR,
207 QCOW2_INCOMPAT_MASK = QCOW2_INCOMPAT_DIRTY
208 | QCOW2_INCOMPAT_CORRUPT,
211 /* Compatible feature bits */
212 enum {
213 QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR = 0,
214 QCOW2_COMPAT_LAZY_REFCOUNTS = 1 << QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR,
216 QCOW2_COMPAT_FEAT_MASK = QCOW2_COMPAT_LAZY_REFCOUNTS,
219 /* Autoclear feature bits */
220 enum {
221 QCOW2_AUTOCLEAR_BITMAPS_BITNR = 0,
222 QCOW2_AUTOCLEAR_DATA_FILE_RAW_BITNR = 1,
223 QCOW2_AUTOCLEAR_BITMAPS = 1 << QCOW2_AUTOCLEAR_BITMAPS_BITNR,
224 QCOW2_AUTOCLEAR_DATA_FILE_RAW = 1 << QCOW2_AUTOCLEAR_DATA_FILE_RAW_BITNR,
226 QCOW2_AUTOCLEAR_MASK = QCOW2_AUTOCLEAR_BITMAPS,
229 enum qcow2_discard_type {
230 QCOW2_DISCARD_NEVER = 0,
231 QCOW2_DISCARD_ALWAYS,
232 QCOW2_DISCARD_REQUEST,
233 QCOW2_DISCARD_SNAPSHOT,
234 QCOW2_DISCARD_OTHER,
235 QCOW2_DISCARD_MAX
238 typedef struct Qcow2Feature {
239 uint8_t type;
240 uint8_t bit;
241 char name[46];
242 } QEMU_PACKED Qcow2Feature;
244 typedef struct Qcow2DiscardRegion {
245 BlockDriverState *bs;
246 uint64_t offset;
247 uint64_t bytes;
248 QTAILQ_ENTRY(Qcow2DiscardRegion) next;
249 } Qcow2DiscardRegion;
251 typedef uint64_t Qcow2GetRefcountFunc(const void *refcount_array,
252 uint64_t index);
253 typedef void Qcow2SetRefcountFunc(void *refcount_array,
254 uint64_t index, uint64_t value);
256 typedef struct Qcow2BitmapHeaderExt {
257 uint32_t nb_bitmaps;
258 uint32_t reserved32;
259 uint64_t bitmap_directory_size;
260 uint64_t bitmap_directory_offset;
261 } QEMU_PACKED Qcow2BitmapHeaderExt;
263 typedef struct BDRVQcow2State {
264 int cluster_bits;
265 int cluster_size;
266 int cluster_sectors;
267 int l2_slice_size;
268 int l2_bits;
269 int l2_size;
270 int l1_size;
271 int l1_vm_state_index;
272 int refcount_block_bits;
273 int refcount_block_size;
274 int csize_shift;
275 int csize_mask;
276 uint64_t cluster_offset_mask;
277 uint64_t l1_table_offset;
278 uint64_t *l1_table;
280 Qcow2Cache* l2_table_cache;
281 Qcow2Cache* refcount_block_cache;
282 QEMUTimer *cache_clean_timer;
283 unsigned cache_clean_interval;
285 uint8_t *cluster_cache;
286 uint8_t *cluster_data;
287 uint64_t cluster_cache_offset;
288 QLIST_HEAD(, QCowL2Meta) cluster_allocs;
290 uint64_t *refcount_table;
291 uint64_t refcount_table_offset;
292 uint32_t refcount_table_size;
293 uint32_t max_refcount_table_index; /* Last used entry in refcount_table */
294 uint64_t free_cluster_index;
295 uint64_t free_byte_offset;
297 CoMutex lock;
299 Qcow2CryptoHeaderExtension crypto_header; /* QCow2 header extension */
300 QCryptoBlockOpenOptions *crypto_opts; /* Disk encryption runtime options */
301 QCryptoBlock *crypto; /* Disk encryption format driver */
302 bool crypt_physical_offset; /* Whether to use virtual or physical offset
303 for encryption initialization vector tweak */
304 uint32_t crypt_method_header;
305 uint64_t snapshots_offset;
306 int snapshots_size;
307 unsigned int nb_snapshots;
308 QCowSnapshot *snapshots;
310 uint32_t nb_bitmaps;
311 uint64_t bitmap_directory_size;
312 uint64_t bitmap_directory_offset;
314 int flags;
315 int qcow_version;
316 bool use_lazy_refcounts;
317 int refcount_order;
318 int refcount_bits;
319 uint64_t refcount_max;
321 Qcow2GetRefcountFunc *get_refcount;
322 Qcow2SetRefcountFunc *set_refcount;
324 bool discard_passthrough[QCOW2_DISCARD_MAX];
326 int overlap_check; /* bitmask of Qcow2MetadataOverlap values */
327 bool signaled_corruption;
329 uint64_t incompatible_features;
330 uint64_t compatible_features;
331 uint64_t autoclear_features;
333 size_t unknown_header_fields_size;
334 void* unknown_header_fields;
335 QLIST_HEAD(, Qcow2UnknownHeaderExtension) unknown_header_ext;
336 QTAILQ_HEAD (, Qcow2DiscardRegion) discards;
337 bool cache_discards;
339 /* Backing file path and format as stored in the image (this is not the
340 * effective path/format, which may be the result of a runtime option
341 * override) */
342 char *image_backing_file;
343 char *image_backing_format;
345 CoQueue compress_wait_queue;
346 int nb_compress_threads;
348 BdrvChild *data_file;
349 } BDRVQcow2State;
351 typedef struct Qcow2COWRegion {
353 * Offset of the COW region in bytes from the start of the first cluster
354 * touched by the request.
356 unsigned offset;
358 /** Number of bytes to copy */
359 unsigned nb_bytes;
360 } Qcow2COWRegion;
363 * Describes an in-flight (part of a) write request that writes to clusters
364 * that are not referenced in their L2 table yet.
366 typedef struct QCowL2Meta
368 /** Guest offset of the first newly allocated cluster */
369 uint64_t offset;
371 /** Host offset of the first newly allocated cluster */
372 uint64_t alloc_offset;
374 /** Number of newly allocated clusters */
375 int nb_clusters;
377 /** Do not free the old clusters */
378 bool keep_old_clusters;
381 * Requests that overlap with this allocation and wait to be restarted
382 * when the allocating request has completed.
384 CoQueue dependent_requests;
387 * The COW Region between the start of the first allocated cluster and the
388 * area the guest actually writes to.
390 Qcow2COWRegion cow_start;
393 * The COW Region between the area the guest actually writes to and the
394 * end of the last allocated cluster.
396 Qcow2COWRegion cow_end;
399 * The I/O vector with the data from the actual guest write request.
400 * If non-NULL, this is meant to be merged together with the data
401 * from @cow_start and @cow_end into one single write operation.
403 QEMUIOVector *data_qiov;
405 /** Pointer to next L2Meta of the same write request */
406 struct QCowL2Meta *next;
408 QLIST_ENTRY(QCowL2Meta) next_in_flight;
409 } QCowL2Meta;
411 typedef enum QCow2ClusterType {
412 QCOW2_CLUSTER_UNALLOCATED,
413 QCOW2_CLUSTER_ZERO_PLAIN,
414 QCOW2_CLUSTER_ZERO_ALLOC,
415 QCOW2_CLUSTER_NORMAL,
416 QCOW2_CLUSTER_COMPRESSED,
417 } QCow2ClusterType;
419 typedef enum QCow2MetadataOverlap {
420 QCOW2_OL_MAIN_HEADER_BITNR = 0,
421 QCOW2_OL_ACTIVE_L1_BITNR = 1,
422 QCOW2_OL_ACTIVE_L2_BITNR = 2,
423 QCOW2_OL_REFCOUNT_TABLE_BITNR = 3,
424 QCOW2_OL_REFCOUNT_BLOCK_BITNR = 4,
425 QCOW2_OL_SNAPSHOT_TABLE_BITNR = 5,
426 QCOW2_OL_INACTIVE_L1_BITNR = 6,
427 QCOW2_OL_INACTIVE_L2_BITNR = 7,
428 QCOW2_OL_BITMAP_DIRECTORY_BITNR = 8,
430 QCOW2_OL_MAX_BITNR = 9,
432 QCOW2_OL_NONE = 0,
433 QCOW2_OL_MAIN_HEADER = (1 << QCOW2_OL_MAIN_HEADER_BITNR),
434 QCOW2_OL_ACTIVE_L1 = (1 << QCOW2_OL_ACTIVE_L1_BITNR),
435 QCOW2_OL_ACTIVE_L2 = (1 << QCOW2_OL_ACTIVE_L2_BITNR),
436 QCOW2_OL_REFCOUNT_TABLE = (1 << QCOW2_OL_REFCOUNT_TABLE_BITNR),
437 QCOW2_OL_REFCOUNT_BLOCK = (1 << QCOW2_OL_REFCOUNT_BLOCK_BITNR),
438 QCOW2_OL_SNAPSHOT_TABLE = (1 << QCOW2_OL_SNAPSHOT_TABLE_BITNR),
439 QCOW2_OL_INACTIVE_L1 = (1 << QCOW2_OL_INACTIVE_L1_BITNR),
440 /* NOTE: Checking overlaps with inactive L2 tables will result in bdrv
441 * reads. */
442 QCOW2_OL_INACTIVE_L2 = (1 << QCOW2_OL_INACTIVE_L2_BITNR),
443 QCOW2_OL_BITMAP_DIRECTORY = (1 << QCOW2_OL_BITMAP_DIRECTORY_BITNR),
444 } QCow2MetadataOverlap;
446 /* Perform all overlap checks which can be done in constant time */
447 #define QCOW2_OL_CONSTANT \
448 (QCOW2_OL_MAIN_HEADER | QCOW2_OL_ACTIVE_L1 | QCOW2_OL_REFCOUNT_TABLE | \
449 QCOW2_OL_SNAPSHOT_TABLE | QCOW2_OL_BITMAP_DIRECTORY)
451 /* Perform all overlap checks which don't require disk access */
452 #define QCOW2_OL_CACHED \
453 (QCOW2_OL_CONSTANT | QCOW2_OL_ACTIVE_L2 | QCOW2_OL_REFCOUNT_BLOCK | \
454 QCOW2_OL_INACTIVE_L1)
456 /* Perform all overlap checks */
457 #define QCOW2_OL_ALL \
458 (QCOW2_OL_CACHED | QCOW2_OL_INACTIVE_L2)
460 #define L1E_OFFSET_MASK 0x00fffffffffffe00ULL
461 #define L2E_OFFSET_MASK 0x00fffffffffffe00ULL
462 #define L2E_COMPRESSED_OFFSET_SIZE_MASK 0x3fffffffffffffffULL
464 #define REFT_OFFSET_MASK 0xfffffffffffffe00ULL
466 #define INV_OFFSET (-1ULL)
468 static inline bool has_data_file(BlockDriverState *bs)
470 BDRVQcow2State *s = bs->opaque;
471 return (s->data_file != bs->file);
474 static inline int64_t start_of_cluster(BDRVQcow2State *s, int64_t offset)
476 return offset & ~(s->cluster_size - 1);
479 static inline int64_t offset_into_cluster(BDRVQcow2State *s, int64_t offset)
481 return offset & (s->cluster_size - 1);
484 static inline uint64_t size_to_clusters(BDRVQcow2State *s, uint64_t size)
486 return (size + (s->cluster_size - 1)) >> s->cluster_bits;
489 static inline int64_t size_to_l1(BDRVQcow2State *s, int64_t size)
491 int shift = s->cluster_bits + s->l2_bits;
492 return (size + (1ULL << shift) - 1) >> shift;
495 static inline int offset_to_l1_index(BDRVQcow2State *s, uint64_t offset)
497 return offset >> (s->l2_bits + s->cluster_bits);
500 static inline int offset_to_l2_index(BDRVQcow2State *s, int64_t offset)
502 return (offset >> s->cluster_bits) & (s->l2_size - 1);
505 static inline int offset_to_l2_slice_index(BDRVQcow2State *s, int64_t offset)
507 return (offset >> s->cluster_bits) & (s->l2_slice_size - 1);
510 static inline int64_t qcow2_vm_state_offset(BDRVQcow2State *s)
512 return (int64_t)s->l1_vm_state_index << (s->cluster_bits + s->l2_bits);
515 static inline QCow2ClusterType qcow2_get_cluster_type(BlockDriverState *bs,
516 uint64_t l2_entry)
518 if (l2_entry & QCOW_OFLAG_COMPRESSED) {
519 return QCOW2_CLUSTER_COMPRESSED;
520 } else if (l2_entry & QCOW_OFLAG_ZERO) {
521 if (l2_entry & L2E_OFFSET_MASK) {
522 return QCOW2_CLUSTER_ZERO_ALLOC;
524 return QCOW2_CLUSTER_ZERO_PLAIN;
525 } else if (!(l2_entry & L2E_OFFSET_MASK)) {
526 /* Offset 0 generally means unallocated, but it is ambiguous with
527 * external data files because 0 is a valid offset there. However, all
528 * clusters in external data files always have refcount 1, so we can
529 * rely on QCOW_OFLAG_COPIED to disambiguate. */
530 if (has_data_file(bs) && (l2_entry & QCOW_OFLAG_COPIED)) {
531 return QCOW2_CLUSTER_NORMAL;
532 } else {
533 return QCOW2_CLUSTER_UNALLOCATED;
535 } else {
536 return QCOW2_CLUSTER_NORMAL;
540 /* Check whether refcounts are eager or lazy */
541 static inline bool qcow2_need_accurate_refcounts(BDRVQcow2State *s)
543 return !(s->incompatible_features & QCOW2_INCOMPAT_DIRTY);
546 static inline uint64_t l2meta_cow_start(QCowL2Meta *m)
548 return m->offset + m->cow_start.offset;
551 static inline uint64_t l2meta_cow_end(QCowL2Meta *m)
553 return m->offset + m->cow_end.offset + m->cow_end.nb_bytes;
556 static inline uint64_t refcount_diff(uint64_t r1, uint64_t r2)
558 return r1 > r2 ? r1 - r2 : r2 - r1;
561 static inline
562 uint32_t offset_to_reftable_index(BDRVQcow2State *s, uint64_t offset)
564 return offset >> (s->refcount_block_bits + s->cluster_bits);
567 /* qcow2.c functions */
568 int64_t qcow2_refcount_metadata_size(int64_t clusters, size_t cluster_size,
569 int refcount_order, bool generous_increase,
570 uint64_t *refblock_count);
572 int qcow2_mark_dirty(BlockDriverState *bs);
573 int qcow2_mark_corrupt(BlockDriverState *bs);
574 int qcow2_mark_consistent(BlockDriverState *bs);
575 int qcow2_update_header(BlockDriverState *bs);
577 void qcow2_signal_corruption(BlockDriverState *bs, bool fatal, int64_t offset,
578 int64_t size, const char *message_format, ...)
579 GCC_FMT_ATTR(5, 6);
581 int qcow2_validate_table(BlockDriverState *bs, uint64_t offset,
582 uint64_t entries, size_t entry_len,
583 int64_t max_size_bytes, const char *table_name,
584 Error **errp);
586 /* qcow2-refcount.c functions */
587 int qcow2_refcount_init(BlockDriverState *bs);
588 void qcow2_refcount_close(BlockDriverState *bs);
590 int qcow2_get_refcount(BlockDriverState *bs, int64_t cluster_index,
591 uint64_t *refcount);
593 int qcow2_update_cluster_refcount(BlockDriverState *bs, int64_t cluster_index,
594 uint64_t addend, bool decrease,
595 enum qcow2_discard_type type);
597 int64_t qcow2_refcount_area(BlockDriverState *bs, uint64_t offset,
598 uint64_t additional_clusters, bool exact_size,
599 int new_refblock_index,
600 uint64_t new_refblock_offset);
602 int64_t qcow2_alloc_clusters(BlockDriverState *bs, uint64_t size);
603 int64_t qcow2_alloc_clusters_at(BlockDriverState *bs, uint64_t offset,
604 int64_t nb_clusters);
605 int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size);
606 void qcow2_free_clusters(BlockDriverState *bs,
607 int64_t offset, int64_t size,
608 enum qcow2_discard_type type);
609 void qcow2_free_any_clusters(BlockDriverState *bs, uint64_t l2_entry,
610 int nb_clusters, enum qcow2_discard_type type);
612 int qcow2_update_snapshot_refcount(BlockDriverState *bs,
613 int64_t l1_table_offset, int l1_size, int addend);
615 int coroutine_fn qcow2_flush_caches(BlockDriverState *bs);
616 int coroutine_fn qcow2_write_caches(BlockDriverState *bs);
617 int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
618 BdrvCheckMode fix);
620 void qcow2_process_discards(BlockDriverState *bs, int ret);
622 int qcow2_check_metadata_overlap(BlockDriverState *bs, int ign, int64_t offset,
623 int64_t size);
624 int qcow2_pre_write_overlap_check(BlockDriverState *bs, int ign, int64_t offset,
625 int64_t size);
626 int qcow2_inc_refcounts_imrt(BlockDriverState *bs, BdrvCheckResult *res,
627 void **refcount_table,
628 int64_t *refcount_table_size,
629 int64_t offset, int64_t size);
631 int qcow2_change_refcount_order(BlockDriverState *bs, int refcount_order,
632 BlockDriverAmendStatusCB *status_cb,
633 void *cb_opaque, Error **errp);
634 int qcow2_shrink_reftable(BlockDriverState *bs);
635 int64_t qcow2_get_last_cluster(BlockDriverState *bs, int64_t size);
637 /* qcow2-cluster.c functions */
638 int qcow2_grow_l1_table(BlockDriverState *bs, uint64_t min_size,
639 bool exact_size);
640 int qcow2_shrink_l1_table(BlockDriverState *bs, uint64_t max_size);
641 int qcow2_write_l1_entry(BlockDriverState *bs, int l1_index);
642 int qcow2_encrypt_sectors(BDRVQcow2State *s, int64_t sector_num,
643 uint8_t *buf, int nb_sectors, bool enc, Error **errp);
645 int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
646 unsigned int *bytes, uint64_t *cluster_offset);
647 int qcow2_alloc_cluster_offset(BlockDriverState *bs, uint64_t offset,
648 unsigned int *bytes, uint64_t *host_offset,
649 QCowL2Meta **m);
650 int qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
651 uint64_t offset,
652 int compressed_size,
653 uint64_t *host_offset);
655 int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m);
656 void qcow2_alloc_cluster_abort(BlockDriverState *bs, QCowL2Meta *m);
657 int qcow2_cluster_discard(BlockDriverState *bs, uint64_t offset,
658 uint64_t bytes, enum qcow2_discard_type type,
659 bool full_discard);
660 int qcow2_cluster_zeroize(BlockDriverState *bs, uint64_t offset,
661 uint64_t bytes, int flags);
663 int qcow2_expand_zero_clusters(BlockDriverState *bs,
664 BlockDriverAmendStatusCB *status_cb,
665 void *cb_opaque);
667 /* qcow2-snapshot.c functions */
668 int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info);
669 int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id);
670 int qcow2_snapshot_delete(BlockDriverState *bs,
671 const char *snapshot_id,
672 const char *name,
673 Error **errp);
674 int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab);
675 int qcow2_snapshot_load_tmp(BlockDriverState *bs,
676 const char *snapshot_id,
677 const char *name,
678 Error **errp);
680 void qcow2_free_snapshots(BlockDriverState *bs);
681 int qcow2_read_snapshots(BlockDriverState *bs);
683 /* qcow2-cache.c functions */
684 Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables,
685 unsigned table_size);
686 int qcow2_cache_destroy(Qcow2Cache *c);
688 void qcow2_cache_entry_mark_dirty(Qcow2Cache *c, void *table);
689 int qcow2_cache_flush(BlockDriverState *bs, Qcow2Cache *c);
690 int qcow2_cache_write(BlockDriverState *bs, Qcow2Cache *c);
691 int qcow2_cache_set_dependency(BlockDriverState *bs, Qcow2Cache *c,
692 Qcow2Cache *dependency);
693 void qcow2_cache_depends_on_flush(Qcow2Cache *c);
695 void qcow2_cache_clean_unused(Qcow2Cache *c);
696 int qcow2_cache_empty(BlockDriverState *bs, Qcow2Cache *c);
698 int qcow2_cache_get(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
699 void **table);
700 int qcow2_cache_get_empty(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
701 void **table);
702 void qcow2_cache_put(Qcow2Cache *c, void **table);
703 void *qcow2_cache_is_table_offset(Qcow2Cache *c, uint64_t offset);
704 void qcow2_cache_discard(Qcow2Cache *c, void *table);
706 /* qcow2-bitmap.c functions */
707 int qcow2_check_bitmaps_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
708 void **refcount_table,
709 int64_t *refcount_table_size);
710 bool qcow2_load_dirty_bitmaps(BlockDriverState *bs, Error **errp);
711 Qcow2BitmapInfoList *qcow2_get_bitmap_info_list(BlockDriverState *bs,
712 Error **errp);
713 int qcow2_reopen_bitmaps_rw_hint(BlockDriverState *bs, bool *header_updated,
714 Error **errp);
715 int qcow2_reopen_bitmaps_rw(BlockDriverState *bs, Error **errp);
716 void qcow2_store_persistent_dirty_bitmaps(BlockDriverState *bs, Error **errp);
717 int qcow2_reopen_bitmaps_ro(BlockDriverState *bs, Error **errp);
718 bool qcow2_can_store_new_dirty_bitmap(BlockDriverState *bs,
719 const char *name,
720 uint32_t granularity,
721 Error **errp);
722 void qcow2_remove_persistent_dirty_bitmap(BlockDriverState *bs,
723 const char *name,
724 Error **errp);
726 #endif