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