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