xref: /openbmc/qemu/block/qcow2.h (revision 6327540d92e4ef4039dc812d73213d248a9de05b)
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 #include "block/block_int.h"
32 
33 //#define DEBUG_ALLOC
34 //#define DEBUG_ALLOC2
35 //#define DEBUG_EXT
36 
37 #define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
38 
39 #define QCOW_CRYPT_NONE 0
40 #define QCOW_CRYPT_AES  1
41 #define QCOW_CRYPT_LUKS 2
42 
43 #define QCOW_MAX_CRYPT_CLUSTERS 32
44 #define QCOW_MAX_SNAPSHOTS 65536
45 
46 /* Field widths in qcow2 mean normal cluster offsets cannot reach
47  * 64PB; depending on cluster size, compressed clusters can have a
48  * smaller limit (64PB for up to 16k clusters, then ramps down to
49  * 512TB for 2M clusters).  */
50 #define QCOW_MAX_CLUSTER_OFFSET ((1ULL << 56) - 1)
51 
52 /* 8 MB refcount table is enough for 2 PB images at 64k cluster size
53  * (128 GB for 512 byte clusters, 2 EB for 2 MB clusters) */
54 #define QCOW_MAX_REFTABLE_SIZE (8 * MiB)
55 
56 /* 32 MB L1 table is enough for 2 PB images at 64k cluster size
57  * (128 GB for 512 byte clusters, 2 EB for 2 MB clusters) */
58 #define QCOW_MAX_L1_SIZE (32 * MiB)
59 
60 /* Allow for an average of 1k per snapshot table entry, should be plenty of
61  * space for snapshot names and IDs */
62 #define QCOW_MAX_SNAPSHOTS_SIZE (1024 * QCOW_MAX_SNAPSHOTS)
63 
64 /* Maximum amount of extra data per snapshot table entry to accept */
65 #define QCOW_MAX_SNAPSHOT_EXTRA_DATA 1024
66 
67 /* Bitmap header extension constraints */
68 #define QCOW2_MAX_BITMAPS 65535
69 #define QCOW2_MAX_BITMAP_DIRECTORY_SIZE (1024 * QCOW2_MAX_BITMAPS)
70 
71 /* Maximum of parallel sub-request per guest request */
72 #define QCOW2_MAX_WORKERS 8
73 
74 /* indicate that the refcount of the referenced cluster is exactly one. */
75 #define QCOW_OFLAG_COPIED     (1ULL << 63)
76 /* indicate that the cluster is compressed (they never have the copied flag) */
77 #define QCOW_OFLAG_COMPRESSED (1ULL << 62)
78 /* The cluster reads as all zeros */
79 #define QCOW_OFLAG_ZERO (1ULL << 0)
80 
81 #define QCOW_EXTL2_SUBCLUSTERS_PER_CLUSTER 32
82 
83 /* The subcluster X [0..31] is allocated */
84 #define QCOW_OFLAG_SUB_ALLOC(X)   (1ULL << (X))
85 /* The subcluster X [0..31] reads as zeroes */
86 #define QCOW_OFLAG_SUB_ZERO(X)    (QCOW_OFLAG_SUB_ALLOC(X) << 32)
87 /* Subclusters [X, Y) (0 <= X <= Y <= 32) are allocated */
88 #define QCOW_OFLAG_SUB_ALLOC_RANGE(X, Y) \
89     (QCOW_OFLAG_SUB_ALLOC(Y) - QCOW_OFLAG_SUB_ALLOC(X))
90 /* Subclusters [X, Y) (0 <= X <= Y <= 32) read as zeroes */
91 #define QCOW_OFLAG_SUB_ZERO_RANGE(X, Y) \
92     (QCOW_OFLAG_SUB_ALLOC_RANGE(X, Y) << 32)
93 /* L2 entry bitmap with all allocation bits set */
94 #define QCOW_L2_BITMAP_ALL_ALLOC  (QCOW_OFLAG_SUB_ALLOC_RANGE(0, 32))
95 /* L2 entry bitmap with all "read as zeroes" bits set */
96 #define QCOW_L2_BITMAP_ALL_ZEROES (QCOW_OFLAG_SUB_ZERO_RANGE(0, 32))
97 
98 /* Size of normal and extended L2 entries */
99 #define L2E_SIZE_NORMAL   (sizeof(uint64_t))
100 #define L2E_SIZE_EXTENDED (sizeof(uint64_t) * 2)
101 
102 /* Size of L1 table entries */
103 #define L1E_SIZE (sizeof(uint64_t))
104 
105 /* Size of reftable entries */
106 #define REFTABLE_ENTRY_SIZE (sizeof(uint64_t))
107 
108 #define MIN_CLUSTER_BITS 9
109 #define MAX_CLUSTER_BITS 21
110 
111 /* Defined in the qcow2 spec (compressed cluster descriptor) */
112 #define QCOW2_COMPRESSED_SECTOR_SIZE 512U
113 
114 /* Must be at least 2 to cover COW */
115 #define MIN_L2_CACHE_SIZE 2 /* cache entries */
116 
117 /* Must be at least 4 to cover all cases of refcount table growth */
118 #define MIN_REFCOUNT_CACHE_SIZE 4 /* clusters */
119 
120 #ifdef CONFIG_LINUX
121 #define DEFAULT_L2_CACHE_MAX_SIZE (32 * MiB)
122 #define DEFAULT_CACHE_CLEAN_INTERVAL 600  /* seconds */
123 #else
124 #define DEFAULT_L2_CACHE_MAX_SIZE (8 * MiB)
125 /* Cache clean interval is currently available only on Linux, so must be 0 */
126 #define DEFAULT_CACHE_CLEAN_INTERVAL 0
127 #endif
128 
129 #define DEFAULT_CLUSTER_SIZE 65536
130 
131 #define QCOW2_OPT_DATA_FILE "data-file"
132 #define QCOW2_OPT_LAZY_REFCOUNTS "lazy-refcounts"
133 #define QCOW2_OPT_DISCARD_REQUEST "pass-discard-request"
134 #define QCOW2_OPT_DISCARD_SNAPSHOT "pass-discard-snapshot"
135 #define QCOW2_OPT_DISCARD_OTHER "pass-discard-other"
136 #define QCOW2_OPT_DISCARD_NO_UNREF "discard-no-unref"
137 #define QCOW2_OPT_OVERLAP "overlap-check"
138 #define QCOW2_OPT_OVERLAP_TEMPLATE "overlap-check.template"
139 #define QCOW2_OPT_OVERLAP_MAIN_HEADER "overlap-check.main-header"
140 #define QCOW2_OPT_OVERLAP_ACTIVE_L1 "overlap-check.active-l1"
141 #define QCOW2_OPT_OVERLAP_ACTIVE_L2 "overlap-check.active-l2"
142 #define QCOW2_OPT_OVERLAP_REFCOUNT_TABLE "overlap-check.refcount-table"
143 #define QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK "overlap-check.refcount-block"
144 #define QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE "overlap-check.snapshot-table"
145 #define QCOW2_OPT_OVERLAP_INACTIVE_L1 "overlap-check.inactive-l1"
146 #define QCOW2_OPT_OVERLAP_INACTIVE_L2 "overlap-check.inactive-l2"
147 #define QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY "overlap-check.bitmap-directory"
148 #define QCOW2_OPT_CACHE_SIZE "cache-size"
149 #define QCOW2_OPT_L2_CACHE_SIZE "l2-cache-size"
150 #define QCOW2_OPT_L2_CACHE_ENTRY_SIZE "l2-cache-entry-size"
151 #define QCOW2_OPT_REFCOUNT_CACHE_SIZE "refcount-cache-size"
152 #define QCOW2_OPT_CACHE_CLEAN_INTERVAL "cache-clean-interval"
153 
154 typedef struct QCowHeader {
155     uint32_t magic;
156     uint32_t version;
157     uint64_t backing_file_offset;
158     uint32_t backing_file_size;
159     uint32_t cluster_bits;
160     uint64_t size; /* in bytes */
161     uint32_t crypt_method;
162     uint32_t l1_size; /* XXX: save number of clusters instead ? */
163     uint64_t l1_table_offset;
164     uint64_t refcount_table_offset;
165     uint32_t refcount_table_clusters;
166     uint32_t nb_snapshots;
167     uint64_t snapshots_offset;
168 
169     /* The following fields are only valid for version >= 3 */
170     uint64_t incompatible_features;
171     uint64_t compatible_features;
172     uint64_t autoclear_features;
173 
174     uint32_t refcount_order;
175     uint32_t header_length;
176 
177     /* Additional fields */
178     uint8_t compression_type;
179 
180     /* header must be a multiple of 8 */
181     uint8_t padding[7];
182 } QEMU_PACKED QCowHeader;
183 
184 QEMU_BUILD_BUG_ON(!QEMU_IS_ALIGNED(sizeof(QCowHeader), 8));
185 
186 typedef struct QEMU_PACKED QCowSnapshotHeader {
187     /* header is 8 byte aligned */
188     uint64_t l1_table_offset;
189 
190     uint32_t l1_size;
191     uint16_t id_str_size;
192     uint16_t name_size;
193 
194     uint32_t date_sec;
195     uint32_t date_nsec;
196 
197     uint64_t vm_clock_nsec;
198 
199     uint32_t vm_state_size;
200     uint32_t extra_data_size; /* for extension */
201     /* extra data follows */
202     /* id_str follows */
203     /* name follows  */
204 } QCowSnapshotHeader;
205 
206 typedef struct QEMU_PACKED QCowSnapshotExtraData {
207     uint64_t vm_state_size_large;
208     uint64_t disk_size;
209     uint64_t icount;
210 } QCowSnapshotExtraData;
211 
212 
213 typedef struct QCowSnapshot {
214     uint64_t l1_table_offset;
215     uint32_t l1_size;
216     char *id_str;
217     char *name;
218     uint64_t disk_size;
219     uint64_t vm_state_size;
220     uint32_t date_sec;
221     uint32_t date_nsec;
222     uint64_t vm_clock_nsec;
223     /* icount value for the moment when snapshot was taken */
224     uint64_t icount;
225     /* Size of all extra data, including QCowSnapshotExtraData if available */
226     uint32_t extra_data_size;
227     /* Data beyond QCowSnapshotExtraData, if any */
228     void *unknown_extra_data;
229 } QCowSnapshot;
230 
231 struct Qcow2Cache;
232 typedef struct Qcow2Cache Qcow2Cache;
233 
234 typedef struct Qcow2CryptoHeaderExtension {
235     uint64_t offset;
236     uint64_t length;
237 } QEMU_PACKED Qcow2CryptoHeaderExtension;
238 
239 typedef struct Qcow2UnknownHeaderExtension {
240     uint32_t magic;
241     uint32_t len;
242     QLIST_ENTRY(Qcow2UnknownHeaderExtension) next;
243     uint8_t data[];
244 } Qcow2UnknownHeaderExtension;
245 
246 enum {
247     QCOW2_FEAT_TYPE_INCOMPATIBLE    = 0,
248     QCOW2_FEAT_TYPE_COMPATIBLE      = 1,
249     QCOW2_FEAT_TYPE_AUTOCLEAR       = 2,
250 };
251 
252 /* Incompatible feature bits */
253 enum {
254     QCOW2_INCOMPAT_DIRTY_BITNR      = 0,
255     QCOW2_INCOMPAT_CORRUPT_BITNR    = 1,
256     QCOW2_INCOMPAT_DATA_FILE_BITNR  = 2,
257     QCOW2_INCOMPAT_COMPRESSION_BITNR = 3,
258     QCOW2_INCOMPAT_EXTL2_BITNR      = 4,
259     QCOW2_INCOMPAT_DIRTY            = 1 << QCOW2_INCOMPAT_DIRTY_BITNR,
260     QCOW2_INCOMPAT_CORRUPT          = 1 << QCOW2_INCOMPAT_CORRUPT_BITNR,
261     QCOW2_INCOMPAT_DATA_FILE        = 1 << QCOW2_INCOMPAT_DATA_FILE_BITNR,
262     QCOW2_INCOMPAT_COMPRESSION      = 1 << QCOW2_INCOMPAT_COMPRESSION_BITNR,
263     QCOW2_INCOMPAT_EXTL2            = 1 << QCOW2_INCOMPAT_EXTL2_BITNR,
264 
265     QCOW2_INCOMPAT_MASK             = QCOW2_INCOMPAT_DIRTY
266                                     | QCOW2_INCOMPAT_CORRUPT
267                                     | QCOW2_INCOMPAT_DATA_FILE
268                                     | QCOW2_INCOMPAT_COMPRESSION
269                                     | QCOW2_INCOMPAT_EXTL2,
270 };
271 
272 /* Compatible feature bits */
273 enum {
274     QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR = 0,
275     QCOW2_COMPAT_LAZY_REFCOUNTS       = 1 << QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR,
276 
277     QCOW2_COMPAT_FEAT_MASK            = QCOW2_COMPAT_LAZY_REFCOUNTS,
278 };
279 
280 /* Autoclear feature bits */
281 enum {
282     QCOW2_AUTOCLEAR_BITMAPS_BITNR       = 0,
283     QCOW2_AUTOCLEAR_DATA_FILE_RAW_BITNR = 1,
284     QCOW2_AUTOCLEAR_BITMAPS             = 1 << QCOW2_AUTOCLEAR_BITMAPS_BITNR,
285     QCOW2_AUTOCLEAR_DATA_FILE_RAW       = 1 << QCOW2_AUTOCLEAR_DATA_FILE_RAW_BITNR,
286 
287     QCOW2_AUTOCLEAR_MASK                = QCOW2_AUTOCLEAR_BITMAPS
288                                         | QCOW2_AUTOCLEAR_DATA_FILE_RAW,
289 };
290 
291 enum qcow2_discard_type {
292     QCOW2_DISCARD_NEVER = 0,
293     QCOW2_DISCARD_ALWAYS,
294     QCOW2_DISCARD_REQUEST,
295     QCOW2_DISCARD_SNAPSHOT,
296     QCOW2_DISCARD_OTHER,
297     QCOW2_DISCARD_MAX
298 };
299 
300 typedef struct Qcow2Feature {
301     uint8_t type;
302     uint8_t bit;
303     char    name[46];
304 } QEMU_PACKED Qcow2Feature;
305 
306 typedef struct Qcow2DiscardRegion {
307     BlockDriverState *bs;
308     uint64_t offset;
309     uint64_t bytes;
310     QTAILQ_ENTRY(Qcow2DiscardRegion) next;
311 } Qcow2DiscardRegion;
312 
313 typedef uint64_t Qcow2GetRefcountFunc(const void *refcount_array,
314                                       uint64_t index);
315 typedef void Qcow2SetRefcountFunc(void *refcount_array,
316                                   uint64_t index, uint64_t value);
317 
318 typedef struct Qcow2BitmapHeaderExt {
319     uint32_t nb_bitmaps;
320     uint32_t reserved32;
321     uint64_t bitmap_directory_size;
322     uint64_t bitmap_directory_offset;
323 } QEMU_PACKED Qcow2BitmapHeaderExt;
324 
325 #define QCOW2_MAX_THREADS 4
326 
327 typedef struct BDRVQcow2State {
328     int cluster_bits;
329     int cluster_size;
330     int l2_slice_size;
331     int subcluster_bits;
332     int subcluster_size;
333     int subclusters_per_cluster;
334     int l2_bits;
335     int l2_size;
336     int l1_size;
337     int l1_vm_state_index;
338     int refcount_block_bits;
339     int refcount_block_size;
340     int csize_shift;
341     int csize_mask;
342     uint64_t cluster_offset_mask;
343     uint64_t l1_table_offset;
344     uint64_t *l1_table;
345 
346     Qcow2Cache *l2_table_cache;
347     Qcow2Cache *refcount_block_cache;
348     /* Non-NULL while the timer is running */
349     Coroutine *cache_clean_timer_co;
350     unsigned cache_clean_interval;
351     QemuCoSleep cache_clean_timer_wake;
352     CoQueue cache_clean_timer_exit;
353 
354     QLIST_HEAD(, QCowL2Meta) cluster_allocs;
355 
356     uint64_t *refcount_table;
357     uint64_t refcount_table_offset;
358     uint32_t refcount_table_size;
359     uint32_t max_refcount_table_index; /* Last used entry in refcount_table */
360     uint64_t free_cluster_index;
361     uint64_t free_byte_offset;
362 
363     CoMutex lock;
364 
365     Qcow2CryptoHeaderExtension crypto_header; /* QCow2 header extension */
366     QCryptoBlockOpenOptions *crypto_opts; /* Disk encryption runtime options */
367     QCryptoBlock *crypto; /* Disk encryption format driver */
368     bool crypt_physical_offset; /* Whether to use virtual or physical offset
369                                    for encryption initialization vector tweak */
370     uint32_t crypt_method_header;
371     uint64_t snapshots_offset;
372     int snapshots_size;
373     unsigned int nb_snapshots;
374     QCowSnapshot *snapshots;
375 
376     uint32_t nb_bitmaps;
377     uint64_t bitmap_directory_size;
378     uint64_t bitmap_directory_offset;
379 
380     int flags;
381     int qcow_version;
382     bool use_lazy_refcounts;
383     int refcount_order;
384     int refcount_bits;
385     uint64_t refcount_max;
386 
387     Qcow2GetRefcountFunc *get_refcount;
388     Qcow2SetRefcountFunc *set_refcount;
389 
390     bool discard_passthrough[QCOW2_DISCARD_MAX];
391 
392     bool discard_no_unref;
393 
394     int overlap_check; /* bitmask of Qcow2MetadataOverlap values */
395     bool signaled_corruption;
396 
397     uint64_t incompatible_features;
398     uint64_t compatible_features;
399     uint64_t autoclear_features;
400 
401     size_t unknown_header_fields_size;
402     void *unknown_header_fields;
403     QLIST_HEAD(, Qcow2UnknownHeaderExtension) unknown_header_ext;
404     QTAILQ_HEAD (, Qcow2DiscardRegion) discards;
405     bool cache_discards;
406 
407     /* Backing file path and format as stored in the image (this is not the
408      * effective path/format, which may be the result of a runtime option
409      * override) */
410     char *image_backing_file;
411     char *image_backing_format;
412     char *image_data_file;
413 
414     CoQueue thread_task_queue;
415     int nb_threads;
416 
417     BdrvChild *data_file;
418 
419     bool metadata_preallocation_checked;
420     bool metadata_preallocation;
421     /*
422      * Compression type used for the image. Default: 0 - ZLIB
423      * The image compression type is set on image creation.
424      * For now, the only way to change the compression type
425      * is to convert the image with the desired compression type set.
426      */
427     Qcow2CompressionType compression_type;
428 } BDRVQcow2State;
429 
430 typedef struct Qcow2COWRegion {
431     /**
432      * Offset of the COW region in bytes from the start of the first cluster
433      * touched by the request.
434      */
435     unsigned    offset;
436 
437     /** Number of bytes to copy */
438     unsigned    nb_bytes;
439 } Qcow2COWRegion;
440 
441 /**
442  * Describes an in-flight (part of a) write request that writes to clusters
443  * that need to have their L2 table entries updated (because they are
444  * newly allocated or need changes in their L2 bitmaps)
445  */
446 typedef struct QCowL2Meta
447 {
448     /** Guest offset of the first updated cluster */
449     uint64_t offset;
450 
451     /** Host offset of the first updated cluster */
452     uint64_t alloc_offset;
453 
454     /** Number of updated clusters */
455     int nb_clusters;
456 
457     /** Do not free the old clusters */
458     bool keep_old_clusters;
459 
460     /**
461      * Requests that overlap with this allocation and wait to be restarted
462      * when the allocating request has completed.
463      */
464     CoQueue dependent_requests;
465 
466     /**
467      * The COW Region immediately before the area the guest actually
468      * writes to. This (part of the) write request starts at
469      * cow_start.offset + cow_start.nb_bytes.
470      */
471     Qcow2COWRegion cow_start;
472 
473     /**
474      * The COW Region immediately after the area the guest actually
475      * writes to. This (part of the) write request ends at cow_end.offset
476      * (which must always be set even when cow_end.nb_bytes is 0).
477      */
478     Qcow2COWRegion cow_end;
479 
480     /*
481      * Indicates that COW regions are already handled and do not require
482      * any more processing.
483      */
484     bool skip_cow;
485 
486     /**
487      * Indicates that this is not a normal write request but a preallocation.
488      * If the image has extended L2 entries this means that no new individual
489      * subclusters will be marked as allocated in the L2 bitmap (but any
490      * existing contents of that bitmap will be kept).
491      */
492     bool prealloc;
493 
494     /**
495      * The I/O vector with the data from the actual guest write request.
496      * If non-NULL, this is meant to be merged together with the data
497      * from @cow_start and @cow_end into one single write operation.
498      */
499     QEMUIOVector *data_qiov;
500     size_t data_qiov_offset;
501 
502     /** Pointer to next L2Meta of the same write request */
503     struct QCowL2Meta *next;
504 
505     QLIST_ENTRY(QCowL2Meta) next_in_flight;
506 } QCowL2Meta;
507 
508 /*
509  * In images with standard L2 entries all clusters are treated as if
510  * they had one subcluster so QCow2ClusterType and QCow2SubclusterType
511  * can be mapped to each other and have the exact same meaning
512  * (QCOW2_SUBCLUSTER_UNALLOCATED_ALLOC cannot happen in these images).
513  *
514  * In images with extended L2 entries QCow2ClusterType refers to the
515  * complete cluster and QCow2SubclusterType to each of the individual
516  * subclusters, so there are several possible combinations:
517  *
518  *     |--------------+---------------------------|
519  *     | Cluster type | Possible subcluster types |
520  *     |--------------+---------------------------|
521  *     | UNALLOCATED  |         UNALLOCATED_PLAIN |
522  *     |              |                ZERO_PLAIN |
523  *     |--------------+---------------------------|
524  *     | NORMAL       |         UNALLOCATED_ALLOC |
525  *     |              |                ZERO_ALLOC |
526  *     |              |                    NORMAL |
527  *     |--------------+---------------------------|
528  *     | COMPRESSED   |                COMPRESSED |
529  *     |--------------+---------------------------|
530  *
531  * QCOW2_SUBCLUSTER_INVALID means that the L2 entry is incorrect and
532  * the image should be marked corrupt.
533  */
534 
535 typedef enum QCow2ClusterType {
536     QCOW2_CLUSTER_UNALLOCATED,
537     QCOW2_CLUSTER_ZERO_PLAIN,
538     QCOW2_CLUSTER_ZERO_ALLOC,
539     QCOW2_CLUSTER_NORMAL,
540     QCOW2_CLUSTER_COMPRESSED,
541 } QCow2ClusterType;
542 
543 typedef enum QCow2SubclusterType {
544     QCOW2_SUBCLUSTER_UNALLOCATED_PLAIN,
545     QCOW2_SUBCLUSTER_UNALLOCATED_ALLOC,
546     QCOW2_SUBCLUSTER_ZERO_PLAIN,
547     QCOW2_SUBCLUSTER_ZERO_ALLOC,
548     QCOW2_SUBCLUSTER_NORMAL,
549     QCOW2_SUBCLUSTER_COMPRESSED,
550     QCOW2_SUBCLUSTER_INVALID,
551 } QCow2SubclusterType;
552 
553 typedef enum QCow2MetadataOverlap {
554     QCOW2_OL_MAIN_HEADER_BITNR      = 0,
555     QCOW2_OL_ACTIVE_L1_BITNR        = 1,
556     QCOW2_OL_ACTIVE_L2_BITNR        = 2,
557     QCOW2_OL_REFCOUNT_TABLE_BITNR   = 3,
558     QCOW2_OL_REFCOUNT_BLOCK_BITNR   = 4,
559     QCOW2_OL_SNAPSHOT_TABLE_BITNR   = 5,
560     QCOW2_OL_INACTIVE_L1_BITNR      = 6,
561     QCOW2_OL_INACTIVE_L2_BITNR      = 7,
562     QCOW2_OL_BITMAP_DIRECTORY_BITNR = 8,
563 
564     QCOW2_OL_MAX_BITNR              = 9,
565 
566     QCOW2_OL_NONE             = 0,
567     QCOW2_OL_MAIN_HEADER      = (1 << QCOW2_OL_MAIN_HEADER_BITNR),
568     QCOW2_OL_ACTIVE_L1        = (1 << QCOW2_OL_ACTIVE_L1_BITNR),
569     QCOW2_OL_ACTIVE_L2        = (1 << QCOW2_OL_ACTIVE_L2_BITNR),
570     QCOW2_OL_REFCOUNT_TABLE   = (1 << QCOW2_OL_REFCOUNT_TABLE_BITNR),
571     QCOW2_OL_REFCOUNT_BLOCK   = (1 << QCOW2_OL_REFCOUNT_BLOCK_BITNR),
572     QCOW2_OL_SNAPSHOT_TABLE   = (1 << QCOW2_OL_SNAPSHOT_TABLE_BITNR),
573     QCOW2_OL_INACTIVE_L1      = (1 << QCOW2_OL_INACTIVE_L1_BITNR),
574     /* NOTE: Checking overlaps with inactive L2 tables will result in bdrv
575      * reads. */
576     QCOW2_OL_INACTIVE_L2      = (1 << QCOW2_OL_INACTIVE_L2_BITNR),
577     QCOW2_OL_BITMAP_DIRECTORY = (1 << QCOW2_OL_BITMAP_DIRECTORY_BITNR),
578 } QCow2MetadataOverlap;
579 
580 /* Perform all overlap checks which can be done in constant time */
581 #define QCOW2_OL_CONSTANT \
582     (QCOW2_OL_MAIN_HEADER | QCOW2_OL_ACTIVE_L1 | QCOW2_OL_REFCOUNT_TABLE | \
583      QCOW2_OL_SNAPSHOT_TABLE | QCOW2_OL_BITMAP_DIRECTORY)
584 
585 /* Perform all overlap checks which don't require disk access */
586 #define QCOW2_OL_CACHED \
587     (QCOW2_OL_CONSTANT | QCOW2_OL_ACTIVE_L2 | QCOW2_OL_REFCOUNT_BLOCK | \
588      QCOW2_OL_INACTIVE_L1)
589 
590 /* Perform all overlap checks */
591 #define QCOW2_OL_ALL \
592     (QCOW2_OL_CACHED | QCOW2_OL_INACTIVE_L2)
593 
594 #define L1E_OFFSET_MASK 0x00fffffffffffe00ULL
595 #define L1E_RESERVED_MASK 0x7f000000000001ffULL
596 #define L2E_OFFSET_MASK 0x00fffffffffffe00ULL
597 #define L2E_STD_RESERVED_MASK 0x3f000000000001feULL
598 
599 #define REFT_OFFSET_MASK 0xfffffffffffffe00ULL
600 #define REFT_RESERVED_MASK 0x1ffULL
601 
602 #define INV_OFFSET (-1ULL)
603 
604 static inline bool has_subclusters(BDRVQcow2State *s)
605 {
606     return s->incompatible_features & QCOW2_INCOMPAT_EXTL2;
607 }
608 
609 static inline size_t l2_entry_size(BDRVQcow2State *s)
610 {
611     return has_subclusters(s) ? L2E_SIZE_EXTENDED : L2E_SIZE_NORMAL;
612 }
613 
614 static inline uint64_t get_l2_entry(BDRVQcow2State *s, uint64_t *l2_slice,
615                                     int idx)
616 {
617     idx *= l2_entry_size(s) / sizeof(uint64_t);
618     return be64_to_cpu(l2_slice[idx]);
619 }
620 
621 static inline uint64_t get_l2_bitmap(BDRVQcow2State *s, uint64_t *l2_slice,
622                                      int idx)
623 {
624     if (has_subclusters(s)) {
625         idx *= l2_entry_size(s) / sizeof(uint64_t);
626         return be64_to_cpu(l2_slice[idx + 1]);
627     } else {
628         return 0; /* For convenience only; this value has no meaning. */
629     }
630 }
631 
632 static inline void set_l2_entry(BDRVQcow2State *s, uint64_t *l2_slice,
633                                 int idx, uint64_t entry)
634 {
635     idx *= l2_entry_size(s) / sizeof(uint64_t);
636     l2_slice[idx] = cpu_to_be64(entry);
637 }
638 
639 static inline void set_l2_bitmap(BDRVQcow2State *s, uint64_t *l2_slice,
640                                  int idx, uint64_t bitmap)
641 {
642     assert(has_subclusters(s));
643     idx *= l2_entry_size(s) / sizeof(uint64_t);
644     l2_slice[idx + 1] = cpu_to_be64(bitmap);
645 }
646 
647 static inline bool GRAPH_RDLOCK has_data_file(BlockDriverState *bs)
648 {
649     BDRVQcow2State *s = bs->opaque;
650     return (s->data_file != bs->file);
651 }
652 
653 static inline bool data_file_is_raw(BlockDriverState *bs)
654 {
655     BDRVQcow2State *s = bs->opaque;
656     return !!(s->autoclear_features & QCOW2_AUTOCLEAR_DATA_FILE_RAW);
657 }
658 
659 static inline int64_t start_of_cluster(BDRVQcow2State *s, int64_t offset)
660 {
661     return offset & ~(s->cluster_size - 1);
662 }
663 
664 static inline int64_t offset_into_cluster(BDRVQcow2State *s, int64_t offset)
665 {
666     return offset & (s->cluster_size - 1);
667 }
668 
669 static inline int64_t offset_into_subcluster(BDRVQcow2State *s, int64_t offset)
670 {
671     return offset & (s->subcluster_size - 1);
672 }
673 
674 static inline uint64_t size_to_clusters(BDRVQcow2State *s, uint64_t size)
675 {
676     return (size + (s->cluster_size - 1)) >> s->cluster_bits;
677 }
678 
679 static inline uint64_t size_to_subclusters(BDRVQcow2State *s, uint64_t size)
680 {
681     return (size + (s->subcluster_size - 1)) >> s->subcluster_bits;
682 }
683 
684 static inline int64_t size_to_l1(BDRVQcow2State *s, int64_t size)
685 {
686     int shift = s->cluster_bits + s->l2_bits;
687     return (size + (1ULL << shift) - 1) >> shift;
688 }
689 
690 static inline int offset_to_l1_index(BDRVQcow2State *s, uint64_t offset)
691 {
692     return offset >> (s->l2_bits + s->cluster_bits);
693 }
694 
695 static inline int offset_to_l2_index(BDRVQcow2State *s, int64_t offset)
696 {
697     return (offset >> s->cluster_bits) & (s->l2_size - 1);
698 }
699 
700 static inline int offset_to_l2_slice_index(BDRVQcow2State *s, int64_t offset)
701 {
702     return (offset >> s->cluster_bits) & (s->l2_slice_size - 1);
703 }
704 
705 static inline int offset_to_sc_index(BDRVQcow2State *s, int64_t offset)
706 {
707     return (offset >> s->subcluster_bits) & (s->subclusters_per_cluster - 1);
708 }
709 
710 static inline int64_t qcow2_vm_state_offset(BDRVQcow2State *s)
711 {
712     return (int64_t)s->l1_vm_state_index << (s->cluster_bits + s->l2_bits);
713 }
714 
715 static inline QCow2ClusterType GRAPH_RDLOCK
716 qcow2_get_cluster_type(BlockDriverState *bs, uint64_t l2_entry)
717 {
718     BDRVQcow2State *s = bs->opaque;
719 
720     if (l2_entry & QCOW_OFLAG_COMPRESSED) {
721         return QCOW2_CLUSTER_COMPRESSED;
722     } else if ((l2_entry & QCOW_OFLAG_ZERO) && !has_subclusters(s)) {
723         if (l2_entry & L2E_OFFSET_MASK) {
724             return QCOW2_CLUSTER_ZERO_ALLOC;
725         }
726         return QCOW2_CLUSTER_ZERO_PLAIN;
727     } else if (!(l2_entry & L2E_OFFSET_MASK)) {
728         /* Offset 0 generally means unallocated, but it is ambiguous with
729          * external data files because 0 is a valid offset there. However, all
730          * clusters in external data files always have refcount 1, so we can
731          * rely on QCOW_OFLAG_COPIED to disambiguate. */
732         if (has_data_file(bs) && (l2_entry & QCOW_OFLAG_COPIED)) {
733             return QCOW2_CLUSTER_NORMAL;
734         } else {
735             return QCOW2_CLUSTER_UNALLOCATED;
736         }
737     } else {
738         return QCOW2_CLUSTER_NORMAL;
739     }
740 }
741 
742 /*
743  * In an image without subsclusters @l2_bitmap is ignored and
744  * @sc_index must be 0.
745  * Return QCOW2_SUBCLUSTER_INVALID if an invalid l2 entry is detected
746  * (this checks the whole entry and bitmap, not only the bits related
747  * to subcluster @sc_index).
748  */
749 static inline GRAPH_RDLOCK
750 QCow2SubclusterType qcow2_get_subcluster_type(BlockDriverState *bs,
751                                               uint64_t l2_entry,
752                                               uint64_t l2_bitmap,
753                                               unsigned sc_index)
754 {
755     BDRVQcow2State *s = bs->opaque;
756     QCow2ClusterType type = qcow2_get_cluster_type(bs, l2_entry);
757     assert(sc_index < s->subclusters_per_cluster);
758 
759     if (has_subclusters(s)) {
760         switch (type) {
761         case QCOW2_CLUSTER_COMPRESSED:
762             return QCOW2_SUBCLUSTER_COMPRESSED;
763         case QCOW2_CLUSTER_NORMAL:
764             if ((l2_bitmap >> 32) & l2_bitmap) {
765                 return QCOW2_SUBCLUSTER_INVALID;
766             } else if (l2_bitmap & QCOW_OFLAG_SUB_ZERO(sc_index)) {
767                 return QCOW2_SUBCLUSTER_ZERO_ALLOC;
768             } else if (l2_bitmap & QCOW_OFLAG_SUB_ALLOC(sc_index)) {
769                 return QCOW2_SUBCLUSTER_NORMAL;
770             } else {
771                 return QCOW2_SUBCLUSTER_UNALLOCATED_ALLOC;
772             }
773         case QCOW2_CLUSTER_UNALLOCATED:
774             if (l2_bitmap & QCOW_L2_BITMAP_ALL_ALLOC) {
775                 return QCOW2_SUBCLUSTER_INVALID;
776             } else if (l2_bitmap & QCOW_OFLAG_SUB_ZERO(sc_index)) {
777                 return QCOW2_SUBCLUSTER_ZERO_PLAIN;
778             } else {
779                 return QCOW2_SUBCLUSTER_UNALLOCATED_PLAIN;
780             }
781         default:
782             g_assert_not_reached();
783         }
784     } else {
785         switch (type) {
786         case QCOW2_CLUSTER_COMPRESSED:
787             return QCOW2_SUBCLUSTER_COMPRESSED;
788         case QCOW2_CLUSTER_ZERO_PLAIN:
789             return QCOW2_SUBCLUSTER_ZERO_PLAIN;
790         case QCOW2_CLUSTER_ZERO_ALLOC:
791             return QCOW2_SUBCLUSTER_ZERO_ALLOC;
792         case QCOW2_CLUSTER_NORMAL:
793             return QCOW2_SUBCLUSTER_NORMAL;
794         case QCOW2_CLUSTER_UNALLOCATED:
795             return QCOW2_SUBCLUSTER_UNALLOCATED_PLAIN;
796         default:
797             g_assert_not_reached();
798         }
799     }
800 }
801 
802 static inline bool qcow2_cluster_is_allocated(QCow2ClusterType type)
803 {
804     return (type == QCOW2_CLUSTER_COMPRESSED || type == QCOW2_CLUSTER_NORMAL ||
805             type == QCOW2_CLUSTER_ZERO_ALLOC);
806 }
807 
808 /* Check whether refcounts are eager or lazy */
809 static inline bool qcow2_need_accurate_refcounts(BDRVQcow2State *s)
810 {
811     return !(s->incompatible_features & QCOW2_INCOMPAT_DIRTY);
812 }
813 
814 static inline uint64_t l2meta_cow_start(QCowL2Meta *m)
815 {
816     return m->offset + m->cow_start.offset;
817 }
818 
819 static inline uint64_t l2meta_cow_end(QCowL2Meta *m)
820 {
821     return m->offset + m->cow_end.offset + m->cow_end.nb_bytes;
822 }
823 
824 static inline uint64_t refcount_diff(uint64_t r1, uint64_t r2)
825 {
826     return r1 > r2 ? r1 - r2 : r2 - r1;
827 }
828 
829 static inline
830 uint32_t offset_to_reftable_index(BDRVQcow2State *s, uint64_t offset)
831 {
832     return offset >> (s->refcount_block_bits + s->cluster_bits);
833 }
834 
835 /* qcow2.c functions */
836 int64_t qcow2_refcount_metadata_size(int64_t clusters, size_t cluster_size,
837                                      int refcount_order, bool generous_increase,
838                                      uint64_t *refblock_count);
839 
840 int GRAPH_RDLOCK qcow2_mark_dirty(BlockDriverState *bs);
841 int GRAPH_RDLOCK qcow2_mark_corrupt(BlockDriverState *bs);
842 int GRAPH_RDLOCK qcow2_update_header(BlockDriverState *bs);
843 
844 void GRAPH_RDLOCK
845 qcow2_signal_corruption(BlockDriverState *bs, bool fatal, int64_t offset,
846                         int64_t size, const char *message_format, ...)
847                         G_GNUC_PRINTF(5, 6);
848 
849 int qcow2_validate_table(BlockDriverState *bs, uint64_t offset,
850                          uint64_t entries, size_t entry_len,
851                          int64_t max_size_bytes, const char *table_name,
852                          Error **errp);
853 
854 /* qcow2-refcount.c functions */
855 int coroutine_fn GRAPH_RDLOCK qcow2_refcount_init(BlockDriverState *bs);
856 void qcow2_refcount_close(BlockDriverState *bs);
857 
858 int GRAPH_RDLOCK qcow2_get_refcount(BlockDriverState *bs, int64_t cluster_index,
859                                     uint64_t *refcount);
860 
861 int GRAPH_RDLOCK
862 qcow2_update_cluster_refcount(BlockDriverState *bs, int64_t cluster_index,
863                               uint64_t addend, bool decrease,
864                               enum qcow2_discard_type type);
865 
866 int64_t GRAPH_RDLOCK
867 qcow2_refcount_area(BlockDriverState *bs, uint64_t offset,
868                     uint64_t additional_clusters, bool exact_size,
869                     int new_refblock_index,
870                     uint64_t new_refblock_offset);
871 
872 int64_t GRAPH_RDLOCK
873 qcow2_alloc_clusters(BlockDriverState *bs, uint64_t size);
874 
875 int64_t GRAPH_RDLOCK coroutine_fn
876 qcow2_alloc_clusters_at(BlockDriverState *bs, uint64_t offset,
877                         int64_t nb_clusters);
878 
879 int64_t coroutine_fn GRAPH_RDLOCK qcow2_alloc_bytes(BlockDriverState *bs, int size);
880 void GRAPH_RDLOCK qcow2_free_clusters(BlockDriverState *bs,
881                                       int64_t offset, int64_t size,
882                                       enum qcow2_discard_type type);
883 void GRAPH_RDLOCK
884 qcow2_free_any_cluster(BlockDriverState *bs, uint64_t l2_entry,
885                        enum qcow2_discard_type type);
886 void GRAPH_RDLOCK
887 qcow2_discard_cluster(BlockDriverState *bs, uint64_t offset,
888                       uint64_t length, QCow2ClusterType ctype,
889                       enum qcow2_discard_type dtype);
890 
891 int GRAPH_RDLOCK
892 qcow2_update_snapshot_refcount(BlockDriverState *bs, int64_t l1_table_offset,
893                                int l1_size, int addend);
894 
895 int GRAPH_RDLOCK qcow2_flush_caches(BlockDriverState *bs);
896 int GRAPH_RDLOCK qcow2_write_caches(BlockDriverState *bs);
897 int coroutine_fn qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
898                                        BdrvCheckMode fix);
899 
900 void GRAPH_RDLOCK qcow2_process_discards(BlockDriverState *bs, int ret);
901 
902 int GRAPH_RDLOCK
903 qcow2_check_metadata_overlap(BlockDriverState *bs, int ign, int64_t offset,
904                              int64_t size);
905 int GRAPH_RDLOCK
906 qcow2_pre_write_overlap_check(BlockDriverState *bs, int ign, int64_t offset,
907                               int64_t size, bool data_file);
908 
909 int coroutine_fn qcow2_inc_refcounts_imrt(BlockDriverState *bs, BdrvCheckResult *res,
910                                           void **refcount_table,
911                                           int64_t *refcount_table_size,
912                                           int64_t offset, int64_t size);
913 
914 int GRAPH_RDLOCK
915 qcow2_change_refcount_order(BlockDriverState *bs, int refcount_order,
916                             BlockDriverAmendStatusCB *status_cb,
917                             void *cb_opaque, Error **errp);
918 int coroutine_fn GRAPH_RDLOCK qcow2_shrink_reftable(BlockDriverState *bs);
919 
920 int64_t coroutine_fn GRAPH_RDLOCK
921 qcow2_get_last_cluster(BlockDriverState *bs, int64_t size);
922 
923 int coroutine_fn GRAPH_RDLOCK
924 qcow2_detect_metadata_preallocation(BlockDriverState *bs);
925 
926 /* qcow2-cluster.c functions */
927 int GRAPH_RDLOCK
928 qcow2_grow_l1_table(BlockDriverState *bs, uint64_t min_size, bool exact_size);
929 
930 int coroutine_fn GRAPH_RDLOCK
931 qcow2_shrink_l1_table(BlockDriverState *bs, uint64_t max_size);
932 
933 int GRAPH_RDLOCK qcow2_write_l1_entry(BlockDriverState *bs, int l1_index);
934 int qcow2_encrypt_sectors(BDRVQcow2State *s, int64_t sector_num,
935                           uint8_t *buf, int nb_sectors, bool enc, Error **errp);
936 
937 int GRAPH_RDLOCK
938 qcow2_get_host_offset(BlockDriverState *bs, uint64_t offset,
939                       unsigned int *bytes, uint64_t *host_offset,
940                       QCow2SubclusterType *subcluster_type);
941 
942 int coroutine_fn GRAPH_RDLOCK
943 qcow2_alloc_host_offset(BlockDriverState *bs, uint64_t offset,
944                         unsigned int *bytes, uint64_t *host_offset,
945                         QCowL2Meta **m);
946 
947 int coroutine_fn GRAPH_RDLOCK
948 qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs, uint64_t offset,
949                                       int compressed_size, uint64_t *host_offset);
950 void GRAPH_RDLOCK
951 qcow2_parse_compressed_l2_entry(BlockDriverState *bs, uint64_t l2_entry,
952                                 uint64_t *coffset, int *csize);
953 
954 int coroutine_fn GRAPH_RDLOCK
955 qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m);
956 
957 void coroutine_fn GRAPH_RDLOCK
958 qcow2_alloc_cluster_abort(BlockDriverState *bs, QCowL2Meta *m);
959 
960 int GRAPH_RDLOCK
961 qcow2_cluster_discard(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
962                       enum qcow2_discard_type type, bool full_discard);
963 
964 int coroutine_fn GRAPH_RDLOCK
965 qcow2_subcluster_zeroize(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
966                          int flags);
967 
968 int GRAPH_RDLOCK
969 qcow2_expand_zero_clusters(BlockDriverState *bs,
970                            BlockDriverAmendStatusCB *status_cb,
971                            void *cb_opaque);
972 
973 /* qcow2-snapshot.c functions */
974 int GRAPH_RDLOCK
975 qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info);
976 
977 int GRAPH_RDLOCK
978 qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id);
979 
980 int GRAPH_RDLOCK
981 qcow2_snapshot_delete(BlockDriverState *bs, const char *snapshot_id,
982                           const char *name, Error **errp);
983 
984 int GRAPH_RDLOCK
985 qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab);
986 
987 int GRAPH_RDLOCK
988 qcow2_snapshot_load_tmp(BlockDriverState *bs, const char *snapshot_id,
989                         const char *name, Error **errp);
990 
991 void qcow2_free_snapshots(BlockDriverState *bs);
992 int coroutine_fn GRAPH_RDLOCK
993 qcow2_read_snapshots(BlockDriverState *bs, Error **errp);
994 int GRAPH_RDLOCK qcow2_write_snapshots(BlockDriverState *bs);
995 
996 int coroutine_fn GRAPH_RDLOCK
997 qcow2_check_read_snapshot_table(BlockDriverState *bs, BdrvCheckResult *result,
998                                 BdrvCheckMode fix);
999 
1000 int coroutine_fn GRAPH_RDLOCK
1001 qcow2_check_fix_snapshot_table(BlockDriverState *bs, BdrvCheckResult *result,
1002                                BdrvCheckMode fix);
1003 
1004 /* qcow2-cache.c functions */
1005 Qcow2Cache * GRAPH_RDLOCK
1006 qcow2_cache_create(BlockDriverState *bs, int num_tables, unsigned table_size);
1007 
1008 int qcow2_cache_destroy(Qcow2Cache *c);
1009 
1010 void qcow2_cache_entry_mark_dirty(Qcow2Cache *c, void *table);
1011 int GRAPH_RDLOCK qcow2_cache_flush(BlockDriverState *bs, Qcow2Cache *c);
1012 int GRAPH_RDLOCK qcow2_cache_write(BlockDriverState *bs, Qcow2Cache *c);
1013 int GRAPH_RDLOCK qcow2_cache_set_dependency(BlockDriverState *bs, Qcow2Cache *c,
1014                                             Qcow2Cache *dependency);
1015 void qcow2_cache_depends_on_flush(Qcow2Cache *c);
1016 
1017 void qcow2_cache_clean_unused(Qcow2Cache *c);
1018 int GRAPH_RDLOCK qcow2_cache_empty(BlockDriverState *bs, Qcow2Cache *c);
1019 
1020 int GRAPH_RDLOCK
1021 qcow2_cache_get(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
1022                 void **table);
1023 
1024 int GRAPH_RDLOCK
1025 qcow2_cache_get_empty(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
1026                       void **table);
1027 
1028 void qcow2_cache_put(Qcow2Cache *c, void **table);
1029 void *qcow2_cache_is_table_offset(Qcow2Cache *c, uint64_t offset);
1030 void qcow2_cache_discard(Qcow2Cache *c, void *table);
1031 
1032 /* qcow2-bitmap.c functions */
1033 int coroutine_fn GRAPH_RDLOCK
1034 qcow2_check_bitmaps_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
1035                               void **refcount_table,
1036                               int64_t *refcount_table_size);
1037 
1038 bool coroutine_fn GRAPH_RDLOCK
1039 qcow2_load_dirty_bitmaps(BlockDriverState *bs, bool *header_updated,
1040                          Error **errp);
1041 
1042 bool GRAPH_RDLOCK
1043 qcow2_get_bitmap_info_list(BlockDriverState *bs,
1044                            Qcow2BitmapInfoList **info_list, Error **errp);
1045 
1046 int GRAPH_RDLOCK qcow2_reopen_bitmaps_rw(BlockDriverState *bs, Error **errp);
1047 int GRAPH_RDLOCK qcow2_reopen_bitmaps_ro(BlockDriverState *bs, Error **errp);
1048 
1049 int coroutine_fn GRAPH_RDLOCK
1050 qcow2_truncate_bitmaps_check(BlockDriverState *bs, Error **errp);
1051 
1052 bool GRAPH_RDLOCK
1053 qcow2_store_persistent_dirty_bitmaps(BlockDriverState *bs, bool release_stored,
1054                                      Error **errp);
1055 
1056 bool coroutine_fn GRAPH_RDLOCK
1057 qcow2_co_can_store_new_dirty_bitmap(BlockDriverState *bs, const char *name,
1058                                     uint32_t granularity, Error **errp);
1059 
1060 int coroutine_fn GRAPH_RDLOCK
1061 qcow2_co_remove_persistent_dirty_bitmap(BlockDriverState *bs, const char *name,
1062                                         Error **errp);
1063 
1064 bool qcow2_supports_persistent_dirty_bitmap(BlockDriverState *bs);
1065 uint64_t qcow2_get_persistent_dirty_bitmap_size(BlockDriverState *bs,
1066                                                 uint32_t cluster_size);
1067 
1068 ssize_t coroutine_fn
1069 qcow2_co_compress(BlockDriverState *bs, void *dest, size_t dest_size,
1070                   const void *src, size_t src_size);
1071 ssize_t coroutine_fn
1072 qcow2_co_decompress(BlockDriverState *bs, void *dest, size_t dest_size,
1073                     const void *src, size_t src_size);
1074 int coroutine_fn
1075 qcow2_co_encrypt(BlockDriverState *bs, uint64_t host_offset,
1076                  uint64_t guest_offset, void *buf, size_t len);
1077 int coroutine_fn
1078 qcow2_co_decrypt(BlockDriverState *bs, uint64_t host_offset,
1079                  uint64_t guest_offset, void *buf, size_t len);
1080 
1081 #endif
1082