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