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