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