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