xref: /openbmc/qemu/block/qcow2.h (revision fa59483d)
1 /*
2  * Block driver for the QCOW version 2 format
3  *
4  * Copyright (c) 2004-2006 Fabrice Bellard
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
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.
23  */
24 
25 #ifndef BLOCK_QCOW2_H
26 #define BLOCK_QCOW2_H
27 
28 #include "crypto/block.h"
29 #include "qemu/coroutine.h"
30 #include "qemu/units.h"
31 
32 //#define DEBUG_ALLOC
33 //#define DEBUG_ALLOC2
34 //#define DEBUG_EXT
35 
36 #define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
37 
38 #define QCOW_CRYPT_NONE 0
39 #define QCOW_CRYPT_AES  1
40 #define QCOW_CRYPT_LUKS 2
41 
42 #define QCOW_MAX_CRYPT_CLUSTERS 32
43 #define QCOW_MAX_SNAPSHOTS 65536
44 
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)
50 
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)
54 
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)
58 
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)
62 
63 /* Bitmap header extension constraints */
64 #define QCOW2_MAX_BITMAPS 65535
65 #define QCOW2_MAX_BITMAP_DIRECTORY_SIZE (1024 * QCOW2_MAX_BITMAPS)
66 
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)
73 
74 #define MIN_CLUSTER_BITS 9
75 #define MAX_CLUSTER_BITS 21
76 
77 /* Must be at least 2 to cover COW */
78 #define MIN_L2_CACHE_SIZE 2 /* cache entries */
79 
80 /* Must be at least 4 to cover all cases of refcount table growth */
81 #define MIN_REFCOUNT_CACHE_SIZE 4 /* clusters */
82 
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
91 
92 #define DEFAULT_CLUSTER_SIZE 65536
93 
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"
114 
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;
129 
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;
134 
135     uint32_t refcount_order;
136     uint32_t header_length;
137 } QEMU_PACKED QCowHeader;
138 
139 typedef struct QEMU_PACKED QCowSnapshotHeader {
140     /* header is 8 byte aligned */
141     uint64_t l1_table_offset;
142 
143     uint32_t l1_size;
144     uint16_t id_str_size;
145     uint16_t name_size;
146 
147     uint32_t date_sec;
148     uint32_t date_nsec;
149 
150     uint64_t vm_clock_nsec;
151 
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;
158 
159 typedef struct QEMU_PACKED QCowSnapshotExtraData {
160     uint64_t vm_state_size_large;
161     uint64_t disk_size;
162 } QCowSnapshotExtraData;
163 
164 
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;
176 
177 struct Qcow2Cache;
178 typedef struct Qcow2Cache Qcow2Cache;
179 
180 typedef struct Qcow2CryptoHeaderExtension {
181     uint64_t offset;
182     uint64_t length;
183 } QEMU_PACKED Qcow2CryptoHeaderExtension;
184 
185 typedef struct Qcow2UnknownHeaderExtension {
186     uint32_t magic;
187     uint32_t len;
188     QLIST_ENTRY(Qcow2UnknownHeaderExtension) next;
189     uint8_t data[];
190 } Qcow2UnknownHeaderExtension;
191 
192 enum {
193     QCOW2_FEAT_TYPE_INCOMPATIBLE    = 0,
194     QCOW2_FEAT_TYPE_COMPATIBLE      = 1,
195     QCOW2_FEAT_TYPE_AUTOCLEAR       = 2,
196 };
197 
198 /* Incompatible feature bits */
199 enum {
200     QCOW2_INCOMPAT_DIRTY_BITNR   = 0,
201     QCOW2_INCOMPAT_CORRUPT_BITNR = 1,
202     QCOW2_INCOMPAT_DIRTY         = 1 << QCOW2_INCOMPAT_DIRTY_BITNR,
203     QCOW2_INCOMPAT_CORRUPT       = 1 << QCOW2_INCOMPAT_CORRUPT_BITNR,
204 
205     QCOW2_INCOMPAT_MASK          = QCOW2_INCOMPAT_DIRTY
206                                  | QCOW2_INCOMPAT_CORRUPT,
207 };
208 
209 /* Compatible feature bits */
210 enum {
211     QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR = 0,
212     QCOW2_COMPAT_LAZY_REFCOUNTS       = 1 << QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR,
213 
214     QCOW2_COMPAT_FEAT_MASK            = QCOW2_COMPAT_LAZY_REFCOUNTS,
215 };
216 
217 /* Autoclear feature bits */
218 enum {
219     QCOW2_AUTOCLEAR_BITMAPS_BITNR = 0,
220     QCOW2_AUTOCLEAR_BITMAPS       = 1 << QCOW2_AUTOCLEAR_BITMAPS_BITNR,
221 
222     QCOW2_AUTOCLEAR_MASK          = QCOW2_AUTOCLEAR_BITMAPS,
223 };
224 
225 enum qcow2_discard_type {
226     QCOW2_DISCARD_NEVER = 0,
227     QCOW2_DISCARD_ALWAYS,
228     QCOW2_DISCARD_REQUEST,
229     QCOW2_DISCARD_SNAPSHOT,
230     QCOW2_DISCARD_OTHER,
231     QCOW2_DISCARD_MAX
232 };
233 
234 typedef struct Qcow2Feature {
235     uint8_t type;
236     uint8_t bit;
237     char    name[46];
238 } QEMU_PACKED Qcow2Feature;
239 
240 typedef struct Qcow2DiscardRegion {
241     BlockDriverState *bs;
242     uint64_t offset;
243     uint64_t bytes;
244     QTAILQ_ENTRY(Qcow2DiscardRegion) next;
245 } Qcow2DiscardRegion;
246 
247 typedef uint64_t Qcow2GetRefcountFunc(const void *refcount_array,
248                                       uint64_t index);
249 typedef void Qcow2SetRefcountFunc(void *refcount_array,
250                                   uint64_t index, uint64_t value);
251 
252 typedef struct Qcow2BitmapHeaderExt {
253     uint32_t nb_bitmaps;
254     uint32_t reserved32;
255     uint64_t bitmap_directory_size;
256     uint64_t bitmap_directory_offset;
257 } QEMU_PACKED Qcow2BitmapHeaderExt;
258 
259 typedef struct BDRVQcow2State {
260     int cluster_bits;
261     int cluster_size;
262     int cluster_sectors;
263     int l2_slice_size;
264     int l2_bits;
265     int l2_size;
266     int l1_size;
267     int l1_vm_state_index;
268     int refcount_block_bits;
269     int refcount_block_size;
270     int csize_shift;
271     int csize_mask;
272     uint64_t cluster_offset_mask;
273     uint64_t l1_table_offset;
274     uint64_t *l1_table;
275 
276     Qcow2Cache* l2_table_cache;
277     Qcow2Cache* refcount_block_cache;
278     QEMUTimer *cache_clean_timer;
279     unsigned cache_clean_interval;
280 
281     uint8_t *cluster_cache;
282     uint8_t *cluster_data;
283     uint64_t cluster_cache_offset;
284     QLIST_HEAD(, QCowL2Meta) cluster_allocs;
285 
286     uint64_t *refcount_table;
287     uint64_t refcount_table_offset;
288     uint32_t refcount_table_size;
289     uint32_t max_refcount_table_index; /* Last used entry in refcount_table */
290     uint64_t free_cluster_index;
291     uint64_t free_byte_offset;
292 
293     CoMutex lock;
294 
295     Qcow2CryptoHeaderExtension crypto_header; /* QCow2 header extension */
296     QCryptoBlockOpenOptions *crypto_opts; /* Disk encryption runtime options */
297     QCryptoBlock *crypto; /* Disk encryption format driver */
298     bool crypt_physical_offset; /* Whether to use virtual or physical offset
299                                    for encryption initialization vector tweak */
300     uint32_t crypt_method_header;
301     uint64_t snapshots_offset;
302     int snapshots_size;
303     unsigned int nb_snapshots;
304     QCowSnapshot *snapshots;
305 
306     uint32_t nb_bitmaps;
307     uint64_t bitmap_directory_size;
308     uint64_t bitmap_directory_offset;
309 
310     int flags;
311     int qcow_version;
312     bool use_lazy_refcounts;
313     int refcount_order;
314     int refcount_bits;
315     uint64_t refcount_max;
316 
317     Qcow2GetRefcountFunc *get_refcount;
318     Qcow2SetRefcountFunc *set_refcount;
319 
320     bool discard_passthrough[QCOW2_DISCARD_MAX];
321 
322     int overlap_check; /* bitmask of Qcow2MetadataOverlap values */
323     bool signaled_corruption;
324 
325     uint64_t incompatible_features;
326     uint64_t compatible_features;
327     uint64_t autoclear_features;
328 
329     size_t unknown_header_fields_size;
330     void* unknown_header_fields;
331     QLIST_HEAD(, Qcow2UnknownHeaderExtension) unknown_header_ext;
332     QTAILQ_HEAD (, Qcow2DiscardRegion) discards;
333     bool cache_discards;
334 
335     /* Backing file path and format as stored in the image (this is not the
336      * effective path/format, which may be the result of a runtime option
337      * override) */
338     char *image_backing_file;
339     char *image_backing_format;
340 
341     CoQueue compress_wait_queue;
342     int nb_compress_threads;
343 } BDRVQcow2State;
344 
345 typedef struct Qcow2COWRegion {
346     /**
347      * Offset of the COW region in bytes from the start of the first cluster
348      * touched by the request.
349      */
350     unsigned    offset;
351 
352     /** Number of bytes to copy */
353     unsigned    nb_bytes;
354 } Qcow2COWRegion;
355 
356 /**
357  * Describes an in-flight (part of a) write request that writes to clusters
358  * that are not referenced in their L2 table yet.
359  */
360 typedef struct QCowL2Meta
361 {
362     /** Guest offset of the first newly allocated cluster */
363     uint64_t offset;
364 
365     /** Host offset of the first newly allocated cluster */
366     uint64_t alloc_offset;
367 
368     /** Number of newly allocated clusters */
369     int nb_clusters;
370 
371     /** Do not free the old clusters */
372     bool keep_old_clusters;
373 
374     /**
375      * Requests that overlap with this allocation and wait to be restarted
376      * when the allocating request has completed.
377      */
378     CoQueue dependent_requests;
379 
380     /**
381      * The COW Region between the start of the first allocated cluster and the
382      * area the guest actually writes to.
383      */
384     Qcow2COWRegion cow_start;
385 
386     /**
387      * The COW Region between the area the guest actually writes to and the
388      * end of the last allocated cluster.
389      */
390     Qcow2COWRegion cow_end;
391 
392     /**
393      * The I/O vector with the data from the actual guest write request.
394      * If non-NULL, this is meant to be merged together with the data
395      * from @cow_start and @cow_end into one single write operation.
396      */
397     QEMUIOVector *data_qiov;
398 
399     /** Pointer to next L2Meta of the same write request */
400     struct QCowL2Meta *next;
401 
402     QLIST_ENTRY(QCowL2Meta) next_in_flight;
403 } QCowL2Meta;
404 
405 typedef enum QCow2ClusterType {
406     QCOW2_CLUSTER_UNALLOCATED,
407     QCOW2_CLUSTER_ZERO_PLAIN,
408     QCOW2_CLUSTER_ZERO_ALLOC,
409     QCOW2_CLUSTER_NORMAL,
410     QCOW2_CLUSTER_COMPRESSED,
411 } QCow2ClusterType;
412 
413 typedef enum QCow2MetadataOverlap {
414     QCOW2_OL_MAIN_HEADER_BITNR      = 0,
415     QCOW2_OL_ACTIVE_L1_BITNR        = 1,
416     QCOW2_OL_ACTIVE_L2_BITNR        = 2,
417     QCOW2_OL_REFCOUNT_TABLE_BITNR   = 3,
418     QCOW2_OL_REFCOUNT_BLOCK_BITNR   = 4,
419     QCOW2_OL_SNAPSHOT_TABLE_BITNR   = 5,
420     QCOW2_OL_INACTIVE_L1_BITNR      = 6,
421     QCOW2_OL_INACTIVE_L2_BITNR      = 7,
422     QCOW2_OL_BITMAP_DIRECTORY_BITNR = 8,
423 
424     QCOW2_OL_MAX_BITNR              = 9,
425 
426     QCOW2_OL_NONE             = 0,
427     QCOW2_OL_MAIN_HEADER      = (1 << QCOW2_OL_MAIN_HEADER_BITNR),
428     QCOW2_OL_ACTIVE_L1        = (1 << QCOW2_OL_ACTIVE_L1_BITNR),
429     QCOW2_OL_ACTIVE_L2        = (1 << QCOW2_OL_ACTIVE_L2_BITNR),
430     QCOW2_OL_REFCOUNT_TABLE   = (1 << QCOW2_OL_REFCOUNT_TABLE_BITNR),
431     QCOW2_OL_REFCOUNT_BLOCK   = (1 << QCOW2_OL_REFCOUNT_BLOCK_BITNR),
432     QCOW2_OL_SNAPSHOT_TABLE   = (1 << QCOW2_OL_SNAPSHOT_TABLE_BITNR),
433     QCOW2_OL_INACTIVE_L1      = (1 << QCOW2_OL_INACTIVE_L1_BITNR),
434     /* NOTE: Checking overlaps with inactive L2 tables will result in bdrv
435      * reads. */
436     QCOW2_OL_INACTIVE_L2      = (1 << QCOW2_OL_INACTIVE_L2_BITNR),
437     QCOW2_OL_BITMAP_DIRECTORY = (1 << QCOW2_OL_BITMAP_DIRECTORY_BITNR),
438 } QCow2MetadataOverlap;
439 
440 /* Perform all overlap checks which can be done in constant time */
441 #define QCOW2_OL_CONSTANT \
442     (QCOW2_OL_MAIN_HEADER | QCOW2_OL_ACTIVE_L1 | QCOW2_OL_REFCOUNT_TABLE | \
443      QCOW2_OL_SNAPSHOT_TABLE | QCOW2_OL_BITMAP_DIRECTORY)
444 
445 /* Perform all overlap checks which don't require disk access */
446 #define QCOW2_OL_CACHED \
447     (QCOW2_OL_CONSTANT | QCOW2_OL_ACTIVE_L2 | QCOW2_OL_REFCOUNT_BLOCK | \
448      QCOW2_OL_INACTIVE_L1)
449 
450 /* Perform all overlap checks */
451 #define QCOW2_OL_ALL \
452     (QCOW2_OL_CACHED | QCOW2_OL_INACTIVE_L2)
453 
454 #define L1E_OFFSET_MASK 0x00fffffffffffe00ULL
455 #define L2E_OFFSET_MASK 0x00fffffffffffe00ULL
456 #define L2E_COMPRESSED_OFFSET_SIZE_MASK 0x3fffffffffffffffULL
457 
458 #define REFT_OFFSET_MASK 0xfffffffffffffe00ULL
459 
460 static inline int64_t start_of_cluster(BDRVQcow2State *s, int64_t offset)
461 {
462     return offset & ~(s->cluster_size - 1);
463 }
464 
465 static inline int64_t offset_into_cluster(BDRVQcow2State *s, int64_t offset)
466 {
467     return offset & (s->cluster_size - 1);
468 }
469 
470 static inline uint64_t size_to_clusters(BDRVQcow2State *s, uint64_t size)
471 {
472     return (size + (s->cluster_size - 1)) >> s->cluster_bits;
473 }
474 
475 static inline int64_t size_to_l1(BDRVQcow2State *s, int64_t size)
476 {
477     int shift = s->cluster_bits + s->l2_bits;
478     return (size + (1ULL << shift) - 1) >> shift;
479 }
480 
481 static inline int offset_to_l1_index(BDRVQcow2State *s, uint64_t offset)
482 {
483     return offset >> (s->l2_bits + s->cluster_bits);
484 }
485 
486 static inline int offset_to_l2_index(BDRVQcow2State *s, int64_t offset)
487 {
488     return (offset >> s->cluster_bits) & (s->l2_size - 1);
489 }
490 
491 static inline int offset_to_l2_slice_index(BDRVQcow2State *s, int64_t offset)
492 {
493     return (offset >> s->cluster_bits) & (s->l2_slice_size - 1);
494 }
495 
496 static inline int64_t qcow2_vm_state_offset(BDRVQcow2State *s)
497 {
498     return (int64_t)s->l1_vm_state_index << (s->cluster_bits + s->l2_bits);
499 }
500 
501 static inline QCow2ClusterType qcow2_get_cluster_type(uint64_t l2_entry)
502 {
503     if (l2_entry & QCOW_OFLAG_COMPRESSED) {
504         return QCOW2_CLUSTER_COMPRESSED;
505     } else if (l2_entry & QCOW_OFLAG_ZERO) {
506         if (l2_entry & L2E_OFFSET_MASK) {
507             return QCOW2_CLUSTER_ZERO_ALLOC;
508         }
509         return QCOW2_CLUSTER_ZERO_PLAIN;
510     } else if (!(l2_entry & L2E_OFFSET_MASK)) {
511         return QCOW2_CLUSTER_UNALLOCATED;
512     } else {
513         return QCOW2_CLUSTER_NORMAL;
514     }
515 }
516 
517 /* Check whether refcounts are eager or lazy */
518 static inline bool qcow2_need_accurate_refcounts(BDRVQcow2State *s)
519 {
520     return !(s->incompatible_features & QCOW2_INCOMPAT_DIRTY);
521 }
522 
523 static inline uint64_t l2meta_cow_start(QCowL2Meta *m)
524 {
525     return m->offset + m->cow_start.offset;
526 }
527 
528 static inline uint64_t l2meta_cow_end(QCowL2Meta *m)
529 {
530     return m->offset + m->cow_end.offset + m->cow_end.nb_bytes;
531 }
532 
533 static inline uint64_t refcount_diff(uint64_t r1, uint64_t r2)
534 {
535     return r1 > r2 ? r1 - r2 : r2 - r1;
536 }
537 
538 static inline
539 uint32_t offset_to_reftable_index(BDRVQcow2State *s, uint64_t offset)
540 {
541     return offset >> (s->refcount_block_bits + s->cluster_bits);
542 }
543 
544 /* qcow2.c functions */
545 int64_t qcow2_refcount_metadata_size(int64_t clusters, size_t cluster_size,
546                                      int refcount_order, bool generous_increase,
547                                      uint64_t *refblock_count);
548 
549 int qcow2_mark_dirty(BlockDriverState *bs);
550 int qcow2_mark_corrupt(BlockDriverState *bs);
551 int qcow2_mark_consistent(BlockDriverState *bs);
552 int qcow2_update_header(BlockDriverState *bs);
553 
554 void qcow2_signal_corruption(BlockDriverState *bs, bool fatal, int64_t offset,
555                              int64_t size, const char *message_format, ...)
556                              GCC_FMT_ATTR(5, 6);
557 
558 int qcow2_validate_table(BlockDriverState *bs, uint64_t offset,
559                          uint64_t entries, size_t entry_len,
560                          int64_t max_size_bytes, const char *table_name,
561                          Error **errp);
562 
563 /* qcow2-refcount.c functions */
564 int qcow2_refcount_init(BlockDriverState *bs);
565 void qcow2_refcount_close(BlockDriverState *bs);
566 
567 int qcow2_get_refcount(BlockDriverState *bs, int64_t cluster_index,
568                        uint64_t *refcount);
569 
570 int qcow2_update_cluster_refcount(BlockDriverState *bs, int64_t cluster_index,
571                                   uint64_t addend, bool decrease,
572                                   enum qcow2_discard_type type);
573 
574 int64_t qcow2_refcount_area(BlockDriverState *bs, uint64_t offset,
575                             uint64_t additional_clusters, bool exact_size,
576                             int new_refblock_index,
577                             uint64_t new_refblock_offset);
578 
579 int64_t qcow2_alloc_clusters(BlockDriverState *bs, uint64_t size);
580 int64_t qcow2_alloc_clusters_at(BlockDriverState *bs, uint64_t offset,
581                                 int64_t nb_clusters);
582 int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size);
583 void qcow2_free_clusters(BlockDriverState *bs,
584                           int64_t offset, int64_t size,
585                           enum qcow2_discard_type type);
586 void qcow2_free_any_clusters(BlockDriverState *bs, uint64_t l2_entry,
587                              int nb_clusters, enum qcow2_discard_type type);
588 
589 int qcow2_update_snapshot_refcount(BlockDriverState *bs,
590     int64_t l1_table_offset, int l1_size, int addend);
591 
592 int coroutine_fn qcow2_flush_caches(BlockDriverState *bs);
593 int coroutine_fn qcow2_write_caches(BlockDriverState *bs);
594 int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
595                           BdrvCheckMode fix);
596 
597 void qcow2_process_discards(BlockDriverState *bs, int ret);
598 
599 int qcow2_check_metadata_overlap(BlockDriverState *bs, int ign, int64_t offset,
600                                  int64_t size);
601 int qcow2_pre_write_overlap_check(BlockDriverState *bs, int ign, int64_t offset,
602                                   int64_t size);
603 int qcow2_inc_refcounts_imrt(BlockDriverState *bs, BdrvCheckResult *res,
604                              void **refcount_table,
605                              int64_t *refcount_table_size,
606                              int64_t offset, int64_t size);
607 
608 int qcow2_change_refcount_order(BlockDriverState *bs, int refcount_order,
609                                 BlockDriverAmendStatusCB *status_cb,
610                                 void *cb_opaque, Error **errp);
611 int qcow2_shrink_reftable(BlockDriverState *bs);
612 int64_t qcow2_get_last_cluster(BlockDriverState *bs, int64_t size);
613 
614 /* qcow2-cluster.c functions */
615 int qcow2_grow_l1_table(BlockDriverState *bs, uint64_t min_size,
616                         bool exact_size);
617 int qcow2_shrink_l1_table(BlockDriverState *bs, uint64_t max_size);
618 int qcow2_write_l1_entry(BlockDriverState *bs, int l1_index);
619 int qcow2_encrypt_sectors(BDRVQcow2State *s, int64_t sector_num,
620                           uint8_t *buf, int nb_sectors, bool enc, Error **errp);
621 
622 int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
623                              unsigned int *bytes, uint64_t *cluster_offset);
624 int qcow2_alloc_cluster_offset(BlockDriverState *bs, uint64_t offset,
625                                unsigned int *bytes, uint64_t *host_offset,
626                                QCowL2Meta **m);
627 uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
628                                          uint64_t offset,
629                                          int compressed_size);
630 
631 int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m);
632 void qcow2_alloc_cluster_abort(BlockDriverState *bs, QCowL2Meta *m);
633 int qcow2_cluster_discard(BlockDriverState *bs, uint64_t offset,
634                           uint64_t bytes, enum qcow2_discard_type type,
635                           bool full_discard);
636 int qcow2_cluster_zeroize(BlockDriverState *bs, uint64_t offset,
637                           uint64_t bytes, int flags);
638 
639 int qcow2_expand_zero_clusters(BlockDriverState *bs,
640                                BlockDriverAmendStatusCB *status_cb,
641                                void *cb_opaque);
642 
643 /* qcow2-snapshot.c functions */
644 int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info);
645 int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id);
646 int qcow2_snapshot_delete(BlockDriverState *bs,
647                           const char *snapshot_id,
648                           const char *name,
649                           Error **errp);
650 int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab);
651 int qcow2_snapshot_load_tmp(BlockDriverState *bs,
652                             const char *snapshot_id,
653                             const char *name,
654                             Error **errp);
655 
656 void qcow2_free_snapshots(BlockDriverState *bs);
657 int qcow2_read_snapshots(BlockDriverState *bs);
658 
659 /* qcow2-cache.c functions */
660 Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables,
661                                unsigned table_size);
662 int qcow2_cache_destroy(Qcow2Cache *c);
663 
664 void qcow2_cache_entry_mark_dirty(Qcow2Cache *c, void *table);
665 int qcow2_cache_flush(BlockDriverState *bs, Qcow2Cache *c);
666 int qcow2_cache_write(BlockDriverState *bs, Qcow2Cache *c);
667 int qcow2_cache_set_dependency(BlockDriverState *bs, Qcow2Cache *c,
668     Qcow2Cache *dependency);
669 void qcow2_cache_depends_on_flush(Qcow2Cache *c);
670 
671 void qcow2_cache_clean_unused(Qcow2Cache *c);
672 int qcow2_cache_empty(BlockDriverState *bs, Qcow2Cache *c);
673 
674 int qcow2_cache_get(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
675     void **table);
676 int qcow2_cache_get_empty(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
677     void **table);
678 void qcow2_cache_put(Qcow2Cache *c, void **table);
679 void *qcow2_cache_is_table_offset(Qcow2Cache *c, uint64_t offset);
680 void qcow2_cache_discard(Qcow2Cache *c, void *table);
681 
682 /* qcow2-bitmap.c functions */
683 int qcow2_check_bitmaps_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
684                                   void **refcount_table,
685                                   int64_t *refcount_table_size);
686 bool qcow2_load_dirty_bitmaps(BlockDriverState *bs, Error **errp);
687 Qcow2BitmapInfoList *qcow2_get_bitmap_info_list(BlockDriverState *bs,
688                                                 Error **errp);
689 int qcow2_reopen_bitmaps_rw_hint(BlockDriverState *bs, bool *header_updated,
690                                  Error **errp);
691 int qcow2_reopen_bitmaps_rw(BlockDriverState *bs, Error **errp);
692 void qcow2_store_persistent_dirty_bitmaps(BlockDriverState *bs, Error **errp);
693 int qcow2_reopen_bitmaps_ro(BlockDriverState *bs, Error **errp);
694 bool qcow2_can_store_new_dirty_bitmap(BlockDriverState *bs,
695                                       const char *name,
696                                       uint32_t granularity,
697                                       Error **errp);
698 void qcow2_remove_persistent_dirty_bitmap(BlockDriverState *bs,
699                                           const char *name,
700                                           Error **errp);
701 
702 #endif
703