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