]> git.proxmox.com Git - mirror_qemu.git/blame - block/qcow2.h
qcow2: mark the memory as no longer needed after qcow2_cache_empty()
[mirror_qemu.git] / block / qcow2.h
CommitLineData
f7d0fe02
KW
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
f6fa64f6 28#include "crypto/cipher.h"
737e150e 29#include "block/coroutine.h"
f7d0fe02 30
14899cdf
FN
31//#define DEBUG_ALLOC
32//#define DEBUG_ALLOC2
33//#define DEBUG_EXT
34
f7d0fe02 35#define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
f7d0fe02
KW
36
37#define QCOW_CRYPT_NONE 0
38#define QCOW_CRYPT_AES 1
39
40#define QCOW_MAX_CRYPT_CLUSTERS 32
ce48f2f4 41#define QCOW_MAX_SNAPSHOTS 65536
f7d0fe02 42
2b5d5953
KW
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
6a83f8b5
KW
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
5dae6e30
KW
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
f7d0fe02 55/* indicate that the refcount of the referenced cluster is exactly one. */
127c84e1 56#define QCOW_OFLAG_COPIED (1ULL << 63)
f7d0fe02 57/* indicate that the cluster is compressed (they never have the copied flag) */
127c84e1 58#define QCOW_OFLAG_COMPRESSED (1ULL << 62)
6377af48 59/* The cluster reads as all zeros */
127c84e1 60#define QCOW_OFLAG_ZERO (1ULL << 0)
f7d0fe02 61
f7d0fe02 62#define MIN_CLUSTER_BITS 9
80ee15a6 63#define MAX_CLUSTER_BITS 21
f7d0fe02 64
57e21669
HR
65/* Must be at least 2 to cover COW */
66#define MIN_L2_CACHE_SIZE 2 /* clusters */
f7d0fe02 67
29c1a730 68/* Must be at least 4 to cover all cases of refcount table growth */
440ba08a
HR
69#define MIN_REFCOUNT_CACHE_SIZE 4 /* clusters */
70
bc85ef26
HR
71/* Whichever is more */
72#define DEFAULT_L2_CACHE_CLUSTERS 8 /* clusters */
440ba08a
HR
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
29c1a730 78
99cce9fa
KW
79#define DEFAULT_CLUSTER_SIZE 65536
80
acdfb480 81
64aa99d3
KW
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"
05de7e86 86#define QCOW2_OPT_OVERLAP "overlap-check"
ee42b5ce 87#define QCOW2_OPT_OVERLAP_TEMPLATE "overlap-check.template"
05de7e86
HR
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"
6c1c8d5d
HR
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"
acdfb480 99
f7d0fe02
KW
100typedef struct QCowHeader {
101 uint32_t magic;
102 uint32_t version;
103 uint64_t backing_file_offset;
104 uint32_t backing_file_size;
105 uint32_t cluster_bits;
106 uint64_t size; /* in bytes */
107 uint32_t crypt_method;
108 uint32_t l1_size; /* XXX: save number of clusters instead ? */
109 uint64_t l1_table_offset;
110 uint64_t refcount_table_offset;
111 uint32_t refcount_table_clusters;
112 uint32_t nb_snapshots;
113 uint64_t snapshots_offset;
6744cbab
KW
114
115 /* The following fields are only valid for version >= 3 */
116 uint64_t incompatible_features;
117 uint64_t compatible_features;
118 uint64_t autoclear_features;
119
120 uint32_t refcount_order;
121 uint32_t header_length;
c4217f64 122} QEMU_PACKED QCowHeader;
f7d0fe02 123
ce48f2f4
KW
124typedef struct QEMU_PACKED QCowSnapshotHeader {
125 /* header is 8 byte aligned */
126 uint64_t l1_table_offset;
127
128 uint32_t l1_size;
129 uint16_t id_str_size;
130 uint16_t name_size;
131
132 uint32_t date_sec;
133 uint32_t date_nsec;
134
135 uint64_t vm_clock_nsec;
136
137 uint32_t vm_state_size;
138 uint32_t extra_data_size; /* for extension */
139 /* extra data follows */
140 /* id_str follows */
141 /* name follows */
142} QCowSnapshotHeader;
143
144typedef struct QEMU_PACKED QCowSnapshotExtraData {
145 uint64_t vm_state_size_large;
146 uint64_t disk_size;
147} QCowSnapshotExtraData;
148
149
f7d0fe02
KW
150typedef struct QCowSnapshot {
151 uint64_t l1_table_offset;
152 uint32_t l1_size;
153 char *id_str;
154 char *name;
90b27759 155 uint64_t disk_size;
c2c9a466 156 uint64_t vm_state_size;
f7d0fe02
KW
157 uint32_t date_sec;
158 uint32_t date_nsec;
159 uint64_t vm_clock_nsec;
160} QCowSnapshot;
161
49381094
KW
162struct Qcow2Cache;
163typedef struct Qcow2Cache Qcow2Cache;
164
75bab85c
KW
165typedef struct Qcow2UnknownHeaderExtension {
166 uint32_t magic;
167 uint32_t len;
168 QLIST_ENTRY(Qcow2UnknownHeaderExtension) next;
169 uint8_t data[];
170} Qcow2UnknownHeaderExtension;
171
cfcc4c62
KW
172enum {
173 QCOW2_FEAT_TYPE_INCOMPATIBLE = 0,
174 QCOW2_FEAT_TYPE_COMPATIBLE = 1,
175 QCOW2_FEAT_TYPE_AUTOCLEAR = 2,
176};
177
c61d0004
SH
178/* Incompatible feature bits */
179enum {
180 QCOW2_INCOMPAT_DIRTY_BITNR = 0,
69c98726 181 QCOW2_INCOMPAT_CORRUPT_BITNR = 1,
c61d0004 182 QCOW2_INCOMPAT_DIRTY = 1 << QCOW2_INCOMPAT_DIRTY_BITNR,
69c98726 183 QCOW2_INCOMPAT_CORRUPT = 1 << QCOW2_INCOMPAT_CORRUPT_BITNR,
c61d0004 184
69c98726
HR
185 QCOW2_INCOMPAT_MASK = QCOW2_INCOMPAT_DIRTY
186 | QCOW2_INCOMPAT_CORRUPT,
c61d0004
SH
187};
188
bfe8043e
SH
189/* Compatible feature bits */
190enum {
191 QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR = 0,
192 QCOW2_COMPAT_LAZY_REFCOUNTS = 1 << QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR,
193
194 QCOW2_COMPAT_FEAT_MASK = QCOW2_COMPAT_LAZY_REFCOUNTS,
195};
196
6cfcb9b8
KW
197enum qcow2_discard_type {
198 QCOW2_DISCARD_NEVER = 0,
199 QCOW2_DISCARD_ALWAYS,
200 QCOW2_DISCARD_REQUEST,
201 QCOW2_DISCARD_SNAPSHOT,
202 QCOW2_DISCARD_OTHER,
203 QCOW2_DISCARD_MAX
204};
205
cfcc4c62
KW
206typedef struct Qcow2Feature {
207 uint8_t type;
208 uint8_t bit;
209 char name[46];
210} QEMU_PACKED Qcow2Feature;
211
0b919fae
KW
212typedef struct Qcow2DiscardRegion {
213 BlockDriverState *bs;
214 uint64_t offset;
215 uint64_t bytes;
216 QTAILQ_ENTRY(Qcow2DiscardRegion) next;
217} Qcow2DiscardRegion;
218
7453c96b
HR
219typedef uint64_t Qcow2GetRefcountFunc(const void *refcount_array,
220 uint64_t index);
221typedef void Qcow2SetRefcountFunc(void *refcount_array,
222 uint64_t index, uint64_t value);
223
f7d0fe02 224typedef struct BDRVQcowState {
f7d0fe02
KW
225 int cluster_bits;
226 int cluster_size;
227 int cluster_sectors;
228 int l2_bits;
229 int l2_size;
230 int l1_size;
231 int l1_vm_state_index;
1d13d654
HR
232 int refcount_block_bits;
233 int refcount_block_size;
f7d0fe02
KW
234 int csize_shift;
235 int csize_mask;
236 uint64_t cluster_offset_mask;
237 uint64_t l1_table_offset;
238 uint64_t *l1_table;
29c1a730
KW
239
240 Qcow2Cache* l2_table_cache;
241 Qcow2Cache* refcount_block_cache;
242
f7d0fe02
KW
243 uint8_t *cluster_cache;
244 uint8_t *cluster_data;
245 uint64_t cluster_cache_offset;
72cf2d4f 246 QLIST_HEAD(QCowClusterAlloc, QCowL2Meta) cluster_allocs;
f7d0fe02
KW
247
248 uint64_t *refcount_table;
249 uint64_t refcount_table_offset;
250 uint32_t refcount_table_size;
bb572aef
KW
251 uint64_t free_cluster_index;
252 uint64_t free_byte_offset;
f7d0fe02 253
68d100e9
KW
254 CoMutex lock;
255
f6fa64f6 256 QCryptoCipher *cipher; /* current cipher, NULL if no key yet */
f7d0fe02 257 uint32_t crypt_method_header;
f7d0fe02
KW
258 uint64_t snapshots_offset;
259 int snapshots_size;
ce48f2f4 260 unsigned int nb_snapshots;
f7d0fe02 261 QCowSnapshot *snapshots;
06d9260f
AL
262
263 int flags;
6744cbab 264 int qcow_version;
74c4510a 265 bool use_lazy_refcounts;
b6481f37 266 int refcount_order;
346a53df
HR
267 int refcount_bits;
268 uint64_t refcount_max;
6744cbab 269
7453c96b
HR
270 Qcow2GetRefcountFunc *get_refcount;
271 Qcow2SetRefcountFunc *set_refcount;
272
67af674e
KW
273 bool discard_passthrough[QCOW2_DISCARD_MAX];
274
3e355390 275 int overlap_check; /* bitmask of Qcow2MetadataOverlap values */
85186ebd 276 bool signaled_corruption;
3e355390 277
6744cbab
KW
278 uint64_t incompatible_features;
279 uint64_t compatible_features;
280 uint64_t autoclear_features;
281
282 size_t unknown_header_fields_size;
283 void* unknown_header_fields;
75bab85c 284 QLIST_HEAD(, Qcow2UnknownHeaderExtension) unknown_header_ext;
0b919fae
KW
285 QTAILQ_HEAD (, Qcow2DiscardRegion) discards;
286 bool cache_discards;
e4603fe1
KW
287
288 /* Backing file path and format as stored in the image (this is not the
289 * effective path/format, which may be the result of a runtime option
290 * override) */
291 char *image_backing_file;
292 char *image_backing_format;
f7d0fe02
KW
293} BDRVQcowState;
294
f214978a
KW
295struct QCowAIOCB;
296
593fb83c
KW
297typedef struct Qcow2COWRegion {
298 /**
299 * Offset of the COW region in bytes from the start of the first cluster
300 * touched by the request.
301 */
302 uint64_t offset;
303
304 /** Number of sectors to copy */
305 int nb_sectors;
306} Qcow2COWRegion;
307
f50f88b9
KW
308/**
309 * Describes an in-flight (part of a) write request that writes to clusters
310 * that are not referenced in their L2 table yet.
311 */
45aba42f
KW
312typedef struct QCowL2Meta
313{
1d3afd64 314 /** Guest offset of the first newly allocated cluster */
45aba42f 315 uint64_t offset;
1d3afd64 316
1d3afd64 317 /** Host offset of the first newly allocated cluster */
250196f1 318 uint64_t alloc_offset;
1d3afd64 319
1d3afd64
KW
320 /**
321 * Number of sectors from the start of the first allocated cluster to
322 * the end of the (possibly shortened) request
323 */
45aba42f 324 int nb_available;
1d3afd64
KW
325
326 /** Number of newly allocated clusters */
45aba42f 327 int nb_clusters;
1d3afd64
KW
328
329 /**
330 * Requests that overlap with this allocation and wait to be restarted
331 * when the allocating request has completed.
332 */
68d100e9 333 CoQueue dependent_requests;
f214978a 334
593fb83c
KW
335 /**
336 * The COW Region between the start of the first allocated cluster and the
337 * area the guest actually writes to.
338 */
339 Qcow2COWRegion cow_start;
340
341 /**
342 * The COW Region between the area the guest actually writes to and the
343 * end of the last allocated cluster.
344 */
345 Qcow2COWRegion cow_end;
346
88c6588c
KW
347 /** Pointer to next L2Meta of the same write request */
348 struct QCowL2Meta *next;
349
72cf2d4f 350 QLIST_ENTRY(QCowL2Meta) next_in_flight;
45aba42f
KW
351} QCowL2Meta;
352
68d000a3
KW
353enum {
354 QCOW2_CLUSTER_UNALLOCATED,
355 QCOW2_CLUSTER_NORMAL,
356 QCOW2_CLUSTER_COMPRESSED,
6377af48 357 QCOW2_CLUSTER_ZERO
68d000a3
KW
358};
359
a40f1c2a
HR
360typedef enum QCow2MetadataOverlap {
361 QCOW2_OL_MAIN_HEADER_BITNR = 0,
362 QCOW2_OL_ACTIVE_L1_BITNR = 1,
363 QCOW2_OL_ACTIVE_L2_BITNR = 2,
364 QCOW2_OL_REFCOUNT_TABLE_BITNR = 3,
365 QCOW2_OL_REFCOUNT_BLOCK_BITNR = 4,
366 QCOW2_OL_SNAPSHOT_TABLE_BITNR = 5,
367 QCOW2_OL_INACTIVE_L1_BITNR = 6,
368 QCOW2_OL_INACTIVE_L2_BITNR = 7,
369
370 QCOW2_OL_MAX_BITNR = 8,
371
372 QCOW2_OL_NONE = 0,
373 QCOW2_OL_MAIN_HEADER = (1 << QCOW2_OL_MAIN_HEADER_BITNR),
374 QCOW2_OL_ACTIVE_L1 = (1 << QCOW2_OL_ACTIVE_L1_BITNR),
375 QCOW2_OL_ACTIVE_L2 = (1 << QCOW2_OL_ACTIVE_L2_BITNR),
376 QCOW2_OL_REFCOUNT_TABLE = (1 << QCOW2_OL_REFCOUNT_TABLE_BITNR),
377 QCOW2_OL_REFCOUNT_BLOCK = (1 << QCOW2_OL_REFCOUNT_BLOCK_BITNR),
378 QCOW2_OL_SNAPSHOT_TABLE = (1 << QCOW2_OL_SNAPSHOT_TABLE_BITNR),
379 QCOW2_OL_INACTIVE_L1 = (1 << QCOW2_OL_INACTIVE_L1_BITNR),
380 /* NOTE: Checking overlaps with inactive L2 tables will result in bdrv
381 * reads. */
382 QCOW2_OL_INACTIVE_L2 = (1 << QCOW2_OL_INACTIVE_L2_BITNR),
383} QCow2MetadataOverlap;
384
4a273c39
HR
385/* Perform all overlap checks which can be done in constant time */
386#define QCOW2_OL_CONSTANT \
387 (QCOW2_OL_MAIN_HEADER | QCOW2_OL_ACTIVE_L1 | QCOW2_OL_REFCOUNT_TABLE | \
388 QCOW2_OL_SNAPSHOT_TABLE)
389
a40f1c2a
HR
390/* Perform all overlap checks which don't require disk access */
391#define QCOW2_OL_CACHED \
4a273c39
HR
392 (QCOW2_OL_CONSTANT | QCOW2_OL_ACTIVE_L2 | QCOW2_OL_REFCOUNT_BLOCK | \
393 QCOW2_OL_INACTIVE_L1)
394
395/* Perform all overlap checks */
396#define QCOW2_OL_ALL \
397 (QCOW2_OL_CACHED | QCOW2_OL_INACTIVE_L2)
a40f1c2a 398
46bae927
HT
399#define L1E_OFFSET_MASK 0x00fffffffffffe00ULL
400#define L2E_OFFSET_MASK 0x00fffffffffffe00ULL
68d000a3
KW
401#define L2E_COMPRESSED_OFFSET_SIZE_MASK 0x3fffffffffffffffULL
402
46bae927 403#define REFT_OFFSET_MASK 0xfffffffffffffe00ULL
76dc9e0c 404
3b8e2e26
KW
405static inline int64_t start_of_cluster(BDRVQcowState *s, int64_t offset)
406{
407 return offset & ~(s->cluster_size - 1);
408}
409
c37f4cd7
KW
410static inline int64_t offset_into_cluster(BDRVQcowState *s, int64_t offset)
411{
412 return offset & (s->cluster_size - 1);
413}
414
45aba42f 415static inline int size_to_clusters(BDRVQcowState *s, int64_t size)
f7d0fe02
KW
416{
417 return (size + (s->cluster_size - 1)) >> s->cluster_bits;
418}
419
2cf7cfa1 420static inline int64_t size_to_l1(BDRVQcowState *s, int64_t size)
419b19d9
SH
421{
422 int shift = s->cluster_bits + s->l2_bits;
423 return (size + (1ULL << shift) - 1) >> shift;
424}
425
17a71e58
KW
426static inline int offset_to_l2_index(BDRVQcowState *s, int64_t offset)
427{
428 return (offset >> s->cluster_bits) & (s->l2_size - 1);
429}
430
c142442b
KW
431static inline int64_t align_offset(int64_t offset, int n)
432{
433 offset = (offset + n - 1) & ~(n - 1);
434 return offset;
435}
436
1ebf561c
KW
437static inline int64_t qcow2_vm_state_offset(BDRVQcowState *s)
438{
439 return (int64_t)s->l1_vm_state_index << (s->cluster_bits + s->l2_bits);
440}
441
2b5d5953
KW
442static inline uint64_t qcow2_max_refcount_clusters(BDRVQcowState *s)
443{
444 return QCOW_MAX_REFTABLE_SIZE >> s->cluster_bits;
445}
446
68d000a3
KW
447static inline int qcow2_get_cluster_type(uint64_t l2_entry)
448{
449 if (l2_entry & QCOW_OFLAG_COMPRESSED) {
450 return QCOW2_CLUSTER_COMPRESSED;
6377af48
KW
451 } else if (l2_entry & QCOW_OFLAG_ZERO) {
452 return QCOW2_CLUSTER_ZERO;
68d000a3
KW
453 } else if (!(l2_entry & L2E_OFFSET_MASK)) {
454 return QCOW2_CLUSTER_UNALLOCATED;
455 } else {
456 return QCOW2_CLUSTER_NORMAL;
457 }
458}
459
bfe8043e
SH
460/* Check whether refcounts are eager or lazy */
461static inline bool qcow2_need_accurate_refcounts(BDRVQcowState *s)
462{
463 return !(s->incompatible_features & QCOW2_INCOMPAT_DIRTY);
464}
c142442b 465
65eb2e35
KW
466static inline uint64_t l2meta_cow_start(QCowL2Meta *m)
467{
468 return m->offset + m->cow_start.offset;
469}
470
471static inline uint64_t l2meta_cow_end(QCowL2Meta *m)
472{
473 return m->offset + m->cow_end.offset
474 + (m->cow_end.nb_sectors << BDRV_SECTOR_BITS);
475}
476
0e06528e 477static inline uint64_t refcount_diff(uint64_t r1, uint64_t r2)
2aabe7c7
HR
478{
479 return r1 > r2 ? r1 - r2 : r2 - r1;
480}
481
f7d0fe02
KW
482// FIXME Need qcow2_ prefix to global functions
483
484/* qcow2.c functions */
bd28f835
KW
485int qcow2_backing_read1(BlockDriverState *bs, QEMUIOVector *qiov,
486 int64_t sector_num, int nb_sectors);
280d3735
KW
487
488int qcow2_mark_dirty(BlockDriverState *bs);
69c98726
HR
489int qcow2_mark_corrupt(BlockDriverState *bs);
490int qcow2_mark_consistent(BlockDriverState *bs);
e24e49e6 491int qcow2_update_header(BlockDriverState *bs);
f7d0fe02 492
85186ebd
HR
493void qcow2_signal_corruption(BlockDriverState *bs, bool fatal, int64_t offset,
494 int64_t size, const char *message_format, ...)
495 GCC_FMT_ATTR(5, 6);
496
f7d0fe02 497/* qcow2-refcount.c functions */
ed6ccf0f
KW
498int qcow2_refcount_init(BlockDriverState *bs);
499void qcow2_refcount_close(BlockDriverState *bs);
f7d0fe02 500
7324c10f 501int qcow2_get_refcount(BlockDriverState *bs, int64_t cluster_index,
0e06528e 502 uint64_t *refcount);
44751917 503
32b6444d 504int qcow2_update_cluster_refcount(BlockDriverState *bs, int64_t cluster_index,
0e06528e 505 uint64_t addend, bool decrease,
2aabe7c7 506 enum qcow2_discard_type type);
32b6444d 507
bb572aef 508int64_t qcow2_alloc_clusters(BlockDriverState *bs, uint64_t size);
256900b1
KW
509int qcow2_alloc_clusters_at(BlockDriverState *bs, uint64_t offset,
510 int nb_clusters);
ed6ccf0f
KW
511int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size);
512void qcow2_free_clusters(BlockDriverState *bs,
6cfcb9b8
KW
513 int64_t offset, int64_t size,
514 enum qcow2_discard_type type);
515void qcow2_free_any_clusters(BlockDriverState *bs, uint64_t l2_entry,
516 int nb_clusters, enum qcow2_discard_type type);
f7d0fe02 517
ed6ccf0f
KW
518int qcow2_update_snapshot_refcount(BlockDriverState *bs,
519 int64_t l1_table_offset, int l1_size, int addend);
f7d0fe02 520
166acf54
KW
521int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
522 BdrvCheckMode fix);
f7d0fe02 523
0b919fae
KW
524void qcow2_process_discards(BlockDriverState *bs, int ret);
525
231bb267 526int qcow2_check_metadata_overlap(BlockDriverState *bs, int ign, int64_t offset,
a40f1c2a 527 int64_t size);
231bb267 528int qcow2_pre_write_overlap_check(BlockDriverState *bs, int ign, int64_t offset,
a40f1c2a
HR
529 int64_t size);
530
45aba42f 531/* qcow2-cluster.c functions */
2cf7cfa1
KW
532int qcow2_grow_l1_table(BlockDriverState *bs, uint64_t min_size,
533 bool exact_size);
e23e400e 534int qcow2_write_l1_entry(BlockDriverState *bs, int l1_index);
ed6ccf0f 535void qcow2_l2_cache_reset(BlockDriverState *bs);
66f82cee 536int qcow2_decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset);
f6fa64f6
DB
537int qcow2_encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
538 uint8_t *out_buf, const uint8_t *in_buf,
539 int nb_sectors, bool enc, Error **errp);
45aba42f 540
1c46efaa
KW
541int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
542 int *num, uint64_t *cluster_offset);
f4f0d391 543int qcow2_alloc_cluster_offset(BlockDriverState *bs, uint64_t offset,
16f0587e 544 int *num, uint64_t *host_offset, QCowL2Meta **m);
ed6ccf0f 545uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
45aba42f
KW
546 uint64_t offset,
547 int compressed_size);
548
148da7ea 549int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m);
5ea929e3 550int qcow2_discard_clusters(BlockDriverState *bs, uint64_t offset,
808c4b6f 551 int nb_sectors, enum qcow2_discard_type type, bool full_discard);
621f0589 552int qcow2_zero_clusters(BlockDriverState *bs, uint64_t offset, int nb_sectors);
45aba42f 553
4057a2b2
HR
554int qcow2_expand_zero_clusters(BlockDriverState *bs,
555 BlockDriverAmendStatusCB *status_cb);
32b6444d 556
c142442b 557/* qcow2-snapshot.c functions */
ed6ccf0f
KW
558int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info);
559int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id);
a89d89d3
WX
560int qcow2_snapshot_delete(BlockDriverState *bs,
561 const char *snapshot_id,
562 const char *name,
563 Error **errp);
ed6ccf0f 564int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab);
7b4c4781
WX
565int qcow2_snapshot_load_tmp(BlockDriverState *bs,
566 const char *snapshot_id,
567 const char *name,
568 Error **errp);
c142442b 569
ed6ccf0f
KW
570void qcow2_free_snapshots(BlockDriverState *bs);
571int qcow2_read_snapshots(BlockDriverState *bs);
c142442b 572
49381094 573/* qcow2-cache.c functions */
6af4e9ea 574Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables);
49381094
KW
575int qcow2_cache_destroy(BlockDriverState* bs, Qcow2Cache *c);
576
72e80b89
AG
577void qcow2_cache_entry_mark_dirty(BlockDriverState *bs, Qcow2Cache *c,
578 void *table);
49381094
KW
579int qcow2_cache_flush(BlockDriverState *bs, Qcow2Cache *c);
580int qcow2_cache_set_dependency(BlockDriverState *bs, Qcow2Cache *c,
581 Qcow2Cache *dependency);
3de0a294 582void qcow2_cache_depends_on_flush(Qcow2Cache *c);
49381094 583
e7108fea
HR
584int qcow2_cache_empty(BlockDriverState *bs, Qcow2Cache *c);
585
49381094
KW
586int qcow2_cache_get(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
587 void **table);
588int qcow2_cache_get_empty(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
589 void **table);
a3f1afb4 590void qcow2_cache_put(BlockDriverState *bs, Qcow2Cache *c, void **table);
49381094 591
f7d0fe02 592#endif