]> git.proxmox.com Git - mirror_qemu.git/blame - block/qcow2.h
qcow2: Use endof()
[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
88ddffae
VSO
64/* Bitmap header extension constraints */
65#define QCOW2_MAX_BITMAPS 65535
66#define QCOW2_MAX_BITMAP_DIRECTORY_SIZE (1024 * QCOW2_MAX_BITMAPS)
67
d710cf57
VSO
68/* Maximum of parallel sub-request per guest request */
69#define QCOW2_MAX_WORKERS 8
70
f7d0fe02 71/* indicate that the refcount of the referenced cluster is exactly one. */
127c84e1 72#define QCOW_OFLAG_COPIED (1ULL << 63)
f7d0fe02 73/* indicate that the cluster is compressed (they never have the copied flag) */
127c84e1 74#define QCOW_OFLAG_COMPRESSED (1ULL << 62)
6377af48 75/* The cluster reads as all zeros */
127c84e1 76#define QCOW_OFLAG_ZERO (1ULL << 0)
f7d0fe02 77
f7d0fe02 78#define MIN_CLUSTER_BITS 9
80ee15a6 79#define MAX_CLUSTER_BITS 21
f7d0fe02 80
b6c24694
AG
81/* Defined in the qcow2 spec (compressed cluster descriptor) */
82#define QCOW2_COMPRESSED_SECTOR_SIZE 512U
83#define QCOW2_COMPRESSED_SECTOR_MASK (~(QCOW2_COMPRESSED_SECTOR_SIZE - 1))
84
57e21669 85/* Must be at least 2 to cover COW */
1221fe6f 86#define MIN_L2_CACHE_SIZE 2 /* cache entries */
f7d0fe02 87
29c1a730 88/* Must be at least 4 to cover all cases of refcount table growth */
440ba08a
HR
89#define MIN_REFCOUNT_CACHE_SIZE 4 /* clusters */
90
80668d0f 91#ifdef CONFIG_LINUX
14632122 92#define DEFAULT_L2_CACHE_MAX_SIZE (32 * MiB)
e957b50b 93#define DEFAULT_CACHE_CLEAN_INTERVAL 600 /* seconds */
80668d0f 94#else
14632122 95#define DEFAULT_L2_CACHE_MAX_SIZE (8 * MiB)
e957b50b
LB
96/* Cache clean interval is currently available only on Linux, so must be 0 */
97#define DEFAULT_CACHE_CLEAN_INTERVAL 0
80668d0f 98#endif
440ba08a 99
14632122 100#define DEFAULT_CLUSTER_SIZE 65536
99cce9fa 101
0e8c08be 102#define QCOW2_OPT_DATA_FILE "data-file"
64aa99d3
KW
103#define QCOW2_OPT_LAZY_REFCOUNTS "lazy-refcounts"
104#define QCOW2_OPT_DISCARD_REQUEST "pass-discard-request"
105#define QCOW2_OPT_DISCARD_SNAPSHOT "pass-discard-snapshot"
106#define QCOW2_OPT_DISCARD_OTHER "pass-discard-other"
05de7e86 107#define QCOW2_OPT_OVERLAP "overlap-check"
ee42b5ce 108#define QCOW2_OPT_OVERLAP_TEMPLATE "overlap-check.template"
05de7e86
HR
109#define QCOW2_OPT_OVERLAP_MAIN_HEADER "overlap-check.main-header"
110#define QCOW2_OPT_OVERLAP_ACTIVE_L1 "overlap-check.active-l1"
111#define QCOW2_OPT_OVERLAP_ACTIVE_L2 "overlap-check.active-l2"
112#define QCOW2_OPT_OVERLAP_REFCOUNT_TABLE "overlap-check.refcount-table"
113#define QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK "overlap-check.refcount-block"
114#define QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE "overlap-check.snapshot-table"
115#define QCOW2_OPT_OVERLAP_INACTIVE_L1 "overlap-check.inactive-l1"
116#define QCOW2_OPT_OVERLAP_INACTIVE_L2 "overlap-check.inactive-l2"
0e4e4318 117#define QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY "overlap-check.bitmap-directory"
6c1c8d5d
HR
118#define QCOW2_OPT_CACHE_SIZE "cache-size"
119#define QCOW2_OPT_L2_CACHE_SIZE "l2-cache-size"
1221fe6f 120#define QCOW2_OPT_L2_CACHE_ENTRY_SIZE "l2-cache-entry-size"
6c1c8d5d 121#define QCOW2_OPT_REFCOUNT_CACHE_SIZE "refcount-cache-size"
279621c0 122#define QCOW2_OPT_CACHE_CLEAN_INTERVAL "cache-clean-interval"
acdfb480 123
f7d0fe02
KW
124typedef struct QCowHeader {
125 uint32_t magic;
126 uint32_t version;
127 uint64_t backing_file_offset;
128 uint32_t backing_file_size;
129 uint32_t cluster_bits;
130 uint64_t size; /* in bytes */
131 uint32_t crypt_method;
132 uint32_t l1_size; /* XXX: save number of clusters instead ? */
133 uint64_t l1_table_offset;
134 uint64_t refcount_table_offset;
135 uint32_t refcount_table_clusters;
136 uint32_t nb_snapshots;
137 uint64_t snapshots_offset;
6744cbab
KW
138
139 /* The following fields are only valid for version >= 3 */
140 uint64_t incompatible_features;
141 uint64_t compatible_features;
142 uint64_t autoclear_features;
143
144 uint32_t refcount_order;
145 uint32_t header_length;
c4217f64 146} QEMU_PACKED QCowHeader;
f7d0fe02 147
ce48f2f4
KW
148typedef struct QEMU_PACKED QCowSnapshotHeader {
149 /* header is 8 byte aligned */
150 uint64_t l1_table_offset;
151
152 uint32_t l1_size;
153 uint16_t id_str_size;
154 uint16_t name_size;
155
156 uint32_t date_sec;
157 uint32_t date_nsec;
158
159 uint64_t vm_clock_nsec;
160
161 uint32_t vm_state_size;
162 uint32_t extra_data_size; /* for extension */
163 /* extra data follows */
164 /* id_str follows */
165 /* name follows */
166} QCowSnapshotHeader;
167
168typedef struct QEMU_PACKED QCowSnapshotExtraData {
169 uint64_t vm_state_size_large;
170 uint64_t disk_size;
171} QCowSnapshotExtraData;
172
173
f7d0fe02
KW
174typedef struct QCowSnapshot {
175 uint64_t l1_table_offset;
176 uint32_t l1_size;
177 char *id_str;
178 char *name;
90b27759 179 uint64_t disk_size;
c2c9a466 180 uint64_t vm_state_size;
f7d0fe02
KW
181 uint32_t date_sec;
182 uint32_t date_nsec;
183 uint64_t vm_clock_nsec;
184} QCowSnapshot;
185
49381094
KW
186struct Qcow2Cache;
187typedef struct Qcow2Cache Qcow2Cache;
188
4652b8f3
DB
189typedef struct Qcow2CryptoHeaderExtension {
190 uint64_t offset;
191 uint64_t length;
192} QEMU_PACKED Qcow2CryptoHeaderExtension;
193
75bab85c
KW
194typedef struct Qcow2UnknownHeaderExtension {
195 uint32_t magic;
196 uint32_t len;
197 QLIST_ENTRY(Qcow2UnknownHeaderExtension) next;
198 uint8_t data[];
199} Qcow2UnknownHeaderExtension;
200
cfcc4c62
KW
201enum {
202 QCOW2_FEAT_TYPE_INCOMPATIBLE = 0,
203 QCOW2_FEAT_TYPE_COMPATIBLE = 1,
204 QCOW2_FEAT_TYPE_AUTOCLEAR = 2,
205};
206
c61d0004
SH
207/* Incompatible feature bits */
208enum {
93c24936
KW
209 QCOW2_INCOMPAT_DIRTY_BITNR = 0,
210 QCOW2_INCOMPAT_CORRUPT_BITNR = 1,
211 QCOW2_INCOMPAT_DATA_FILE_BITNR = 2,
212 QCOW2_INCOMPAT_DIRTY = 1 << QCOW2_INCOMPAT_DIRTY_BITNR,
213 QCOW2_INCOMPAT_CORRUPT = 1 << QCOW2_INCOMPAT_CORRUPT_BITNR,
214 QCOW2_INCOMPAT_DATA_FILE = 1 << QCOW2_INCOMPAT_DATA_FILE_BITNR,
215
216 QCOW2_INCOMPAT_MASK = QCOW2_INCOMPAT_DIRTY
0e8c08be
KW
217 | QCOW2_INCOMPAT_CORRUPT
218 | QCOW2_INCOMPAT_DATA_FILE,
c61d0004
SH
219};
220
bfe8043e
SH
221/* Compatible feature bits */
222enum {
223 QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR = 0,
224 QCOW2_COMPAT_LAZY_REFCOUNTS = 1 << QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR,
225
226 QCOW2_COMPAT_FEAT_MASK = QCOW2_COMPAT_LAZY_REFCOUNTS,
227};
228
88ddffae
VSO
229/* Autoclear feature bits */
230enum {
93c24936
KW
231 QCOW2_AUTOCLEAR_BITMAPS_BITNR = 0,
232 QCOW2_AUTOCLEAR_DATA_FILE_RAW_BITNR = 1,
233 QCOW2_AUTOCLEAR_BITMAPS = 1 << QCOW2_AUTOCLEAR_BITMAPS_BITNR,
234 QCOW2_AUTOCLEAR_DATA_FILE_RAW = 1 << QCOW2_AUTOCLEAR_DATA_FILE_RAW_BITNR,
88ddffae 235
6c3944dc
KW
236 QCOW2_AUTOCLEAR_MASK = QCOW2_AUTOCLEAR_BITMAPS
237 | QCOW2_AUTOCLEAR_DATA_FILE_RAW,
88ddffae
VSO
238};
239
6cfcb9b8
KW
240enum qcow2_discard_type {
241 QCOW2_DISCARD_NEVER = 0,
242 QCOW2_DISCARD_ALWAYS,
243 QCOW2_DISCARD_REQUEST,
244 QCOW2_DISCARD_SNAPSHOT,
245 QCOW2_DISCARD_OTHER,
246 QCOW2_DISCARD_MAX
247};
248
cfcc4c62
KW
249typedef struct Qcow2Feature {
250 uint8_t type;
251 uint8_t bit;
252 char name[46];
253} QEMU_PACKED Qcow2Feature;
254
0b919fae
KW
255typedef struct Qcow2DiscardRegion {
256 BlockDriverState *bs;
257 uint64_t offset;
258 uint64_t bytes;
259 QTAILQ_ENTRY(Qcow2DiscardRegion) next;
260} Qcow2DiscardRegion;
261
7453c96b
HR
262typedef uint64_t Qcow2GetRefcountFunc(const void *refcount_array,
263 uint64_t index);
264typedef void Qcow2SetRefcountFunc(void *refcount_array,
265 uint64_t index, uint64_t value);
266
88ddffae
VSO
267typedef struct Qcow2BitmapHeaderExt {
268 uint32_t nb_bitmaps;
269 uint32_t reserved32;
270 uint64_t bitmap_directory_size;
271 uint64_t bitmap_directory_offset;
272} QEMU_PACKED Qcow2BitmapHeaderExt;
273
8ac0f15f
VSO
274#define QCOW2_MAX_THREADS 4
275
ff99129a 276typedef struct BDRVQcow2State {
f7d0fe02
KW
277 int cluster_bits;
278 int cluster_size;
3c2e511a 279 int l2_slice_size;
f7d0fe02
KW
280 int l2_bits;
281 int l2_size;
282 int l1_size;
283 int l1_vm_state_index;
1d13d654
HR
284 int refcount_block_bits;
285 int refcount_block_size;
f7d0fe02
KW
286 int csize_shift;
287 int csize_mask;
288 uint64_t cluster_offset_mask;
289 uint64_t l1_table_offset;
290 uint64_t *l1_table;
29c1a730
KW
291
292 Qcow2Cache* l2_table_cache;
293 Qcow2Cache* refcount_block_cache;
279621c0
AG
294 QEMUTimer *cache_clean_timer;
295 unsigned cache_clean_interval;
29c1a730 296
b58deb34
PB
297 uint8_t *cluster_cache;
298 uint8_t *cluster_data;
299 uint64_t cluster_cache_offset;
300 QLIST_HEAD(, QCowL2Meta) cluster_allocs;
f7d0fe02
KW
301
302 uint64_t *refcount_table;
303 uint64_t refcount_table_offset;
304 uint32_t refcount_table_size;
7061a078 305 uint32_t max_refcount_table_index; /* Last used entry in refcount_table */
bb572aef
KW
306 uint64_t free_cluster_index;
307 uint64_t free_byte_offset;
f7d0fe02 308
68d100e9
KW
309 CoMutex lock;
310
4652b8f3 311 Qcow2CryptoHeaderExtension crypto_header; /* QCow2 header extension */
b25b387f
DB
312 QCryptoBlockOpenOptions *crypto_opts; /* Disk encryption runtime options */
313 QCryptoBlock *crypto; /* Disk encryption format driver */
4652b8f3
DB
314 bool crypt_physical_offset; /* Whether to use virtual or physical offset
315 for encryption initialization vector tweak */
f7d0fe02 316 uint32_t crypt_method_header;
f7d0fe02
KW
317 uint64_t snapshots_offset;
318 int snapshots_size;
ce48f2f4 319 unsigned int nb_snapshots;
f7d0fe02 320 QCowSnapshot *snapshots;
06d9260f 321
88ddffae
VSO
322 uint32_t nb_bitmaps;
323 uint64_t bitmap_directory_size;
324 uint64_t bitmap_directory_offset;
325
06d9260f 326 int flags;
6744cbab 327 int qcow_version;
74c4510a 328 bool use_lazy_refcounts;
b6481f37 329 int refcount_order;
346a53df
HR
330 int refcount_bits;
331 uint64_t refcount_max;
6744cbab 332
7453c96b
HR
333 Qcow2GetRefcountFunc *get_refcount;
334 Qcow2SetRefcountFunc *set_refcount;
335
67af674e
KW
336 bool discard_passthrough[QCOW2_DISCARD_MAX];
337
3e355390 338 int overlap_check; /* bitmask of Qcow2MetadataOverlap values */
85186ebd 339 bool signaled_corruption;
3e355390 340
6744cbab
KW
341 uint64_t incompatible_features;
342 uint64_t compatible_features;
343 uint64_t autoclear_features;
344
345 size_t unknown_header_fields_size;
346 void* unknown_header_fields;
75bab85c 347 QLIST_HEAD(, Qcow2UnknownHeaderExtension) unknown_header_ext;
0b919fae
KW
348 QTAILQ_HEAD (, Qcow2DiscardRegion) discards;
349 bool cache_discards;
e4603fe1
KW
350
351 /* Backing file path and format as stored in the image (this is not the
352 * effective path/format, which may be the result of a runtime option
353 * override) */
354 char *image_backing_file;
355 char *image_backing_format;
9b890bdc 356 char *image_data_file;
ceb029cd 357
6f13a316
VSO
358 CoQueue thread_task_queue;
359 int nb_threads;
93c24936
KW
360
361 BdrvChild *data_file;
69f47505
VSO
362
363 bool metadata_preallocation_checked;
364 bool metadata_preallocation;
ff99129a 365} BDRVQcow2State;
f7d0fe02 366
593fb83c
KW
367typedef struct Qcow2COWRegion {
368 /**
369 * Offset of the COW region in bytes from the start of the first cluster
370 * touched by the request.
371 */
e034f5bc 372 unsigned offset;
593fb83c 373
85567393 374 /** Number of bytes to copy */
e034f5bc 375 unsigned nb_bytes;
593fb83c
KW
376} Qcow2COWRegion;
377
f50f88b9
KW
378/**
379 * Describes an in-flight (part of a) write request that writes to clusters
380 * that are not referenced in their L2 table yet.
381 */
45aba42f
KW
382typedef struct QCowL2Meta
383{
1d3afd64 384 /** Guest offset of the first newly allocated cluster */
45aba42f 385 uint64_t offset;
1d3afd64 386
1d3afd64 387 /** Host offset of the first newly allocated cluster */
250196f1 388 uint64_t alloc_offset;
1d3afd64 389
1d3afd64 390 /** Number of newly allocated clusters */
45aba42f 391 int nb_clusters;
1d3afd64 392
564a6b69
HR
393 /** Do not free the old clusters */
394 bool keep_old_clusters;
395
1d3afd64
KW
396 /**
397 * Requests that overlap with this allocation and wait to be restarted
398 * when the allocating request has completed.
399 */
68d100e9 400 CoQueue dependent_requests;
f214978a 401
593fb83c
KW
402 /**
403 * The COW Region between the start of the first allocated cluster and the
404 * area the guest actually writes to.
405 */
406 Qcow2COWRegion cow_start;
407
408 /**
409 * The COW Region between the area the guest actually writes to and the
410 * end of the last allocated cluster.
411 */
412 Qcow2COWRegion cow_end;
413
c8bb23cb
AN
414 /*
415 * Indicates that COW regions are already handled and do not require
416 * any more processing.
417 */
418 bool skip_cow;
419
ee22a9d8
AG
420 /**
421 * The I/O vector with the data from the actual guest write request.
422 * If non-NULL, this is meant to be merged together with the data
423 * from @cow_start and @cow_end into one single write operation.
424 */
425 QEMUIOVector *data_qiov;
5396234b 426 size_t data_qiov_offset;
ee22a9d8 427
88c6588c
KW
428 /** Pointer to next L2Meta of the same write request */
429 struct QCowL2Meta *next;
430
72cf2d4f 431 QLIST_ENTRY(QCowL2Meta) next_in_flight;
45aba42f
KW
432} QCowL2Meta;
433
3ef95218 434typedef enum QCow2ClusterType {
68d000a3 435 QCOW2_CLUSTER_UNALLOCATED,
fdfab37d
EB
436 QCOW2_CLUSTER_ZERO_PLAIN,
437 QCOW2_CLUSTER_ZERO_ALLOC,
68d000a3
KW
438 QCOW2_CLUSTER_NORMAL,
439 QCOW2_CLUSTER_COMPRESSED,
3ef95218 440} QCow2ClusterType;
68d000a3 441
a40f1c2a 442typedef enum QCow2MetadataOverlap {
0e4e4318
VSO
443 QCOW2_OL_MAIN_HEADER_BITNR = 0,
444 QCOW2_OL_ACTIVE_L1_BITNR = 1,
445 QCOW2_OL_ACTIVE_L2_BITNR = 2,
446 QCOW2_OL_REFCOUNT_TABLE_BITNR = 3,
447 QCOW2_OL_REFCOUNT_BLOCK_BITNR = 4,
448 QCOW2_OL_SNAPSHOT_TABLE_BITNR = 5,
449 QCOW2_OL_INACTIVE_L1_BITNR = 6,
450 QCOW2_OL_INACTIVE_L2_BITNR = 7,
451 QCOW2_OL_BITMAP_DIRECTORY_BITNR = 8,
452
453 QCOW2_OL_MAX_BITNR = 9,
454
455 QCOW2_OL_NONE = 0,
456 QCOW2_OL_MAIN_HEADER = (1 << QCOW2_OL_MAIN_HEADER_BITNR),
457 QCOW2_OL_ACTIVE_L1 = (1 << QCOW2_OL_ACTIVE_L1_BITNR),
458 QCOW2_OL_ACTIVE_L2 = (1 << QCOW2_OL_ACTIVE_L2_BITNR),
459 QCOW2_OL_REFCOUNT_TABLE = (1 << QCOW2_OL_REFCOUNT_TABLE_BITNR),
460 QCOW2_OL_REFCOUNT_BLOCK = (1 << QCOW2_OL_REFCOUNT_BLOCK_BITNR),
461 QCOW2_OL_SNAPSHOT_TABLE = (1 << QCOW2_OL_SNAPSHOT_TABLE_BITNR),
462 QCOW2_OL_INACTIVE_L1 = (1 << QCOW2_OL_INACTIVE_L1_BITNR),
a40f1c2a
HR
463 /* NOTE: Checking overlaps with inactive L2 tables will result in bdrv
464 * reads. */
0e4e4318
VSO
465 QCOW2_OL_INACTIVE_L2 = (1 << QCOW2_OL_INACTIVE_L2_BITNR),
466 QCOW2_OL_BITMAP_DIRECTORY = (1 << QCOW2_OL_BITMAP_DIRECTORY_BITNR),
a40f1c2a
HR
467} QCow2MetadataOverlap;
468
4a273c39
HR
469/* Perform all overlap checks which can be done in constant time */
470#define QCOW2_OL_CONSTANT \
471 (QCOW2_OL_MAIN_HEADER | QCOW2_OL_ACTIVE_L1 | QCOW2_OL_REFCOUNT_TABLE | \
0e4e4318 472 QCOW2_OL_SNAPSHOT_TABLE | QCOW2_OL_BITMAP_DIRECTORY)
4a273c39 473
a40f1c2a
HR
474/* Perform all overlap checks which don't require disk access */
475#define QCOW2_OL_CACHED \
4a273c39
HR
476 (QCOW2_OL_CONSTANT | QCOW2_OL_ACTIVE_L2 | QCOW2_OL_REFCOUNT_BLOCK | \
477 QCOW2_OL_INACTIVE_L1)
478
479/* Perform all overlap checks */
480#define QCOW2_OL_ALL \
481 (QCOW2_OL_CACHED | QCOW2_OL_INACTIVE_L2)
a40f1c2a 482
46bae927
HT
483#define L1E_OFFSET_MASK 0x00fffffffffffe00ULL
484#define L2E_OFFSET_MASK 0x00fffffffffffe00ULL
68d000a3
KW
485#define L2E_COMPRESSED_OFFSET_SIZE_MASK 0x3fffffffffffffffULL
486
46bae927 487#define REFT_OFFSET_MASK 0xfffffffffffffe00ULL
76dc9e0c 488
c6d619cc
KW
489#define INV_OFFSET (-1ULL)
490
93c24936
KW
491static inline bool has_data_file(BlockDriverState *bs)
492{
493 BDRVQcow2State *s = bs->opaque;
494 return (s->data_file != bs->file);
495}
496
6c3944dc
KW
497static inline bool data_file_is_raw(BlockDriverState *bs)
498{
499 BDRVQcow2State *s = bs->opaque;
500 return !!(s->autoclear_features & QCOW2_AUTOCLEAR_DATA_FILE_RAW);
501}
502
ff99129a 503static inline int64_t start_of_cluster(BDRVQcow2State *s, int64_t offset)
3b8e2e26
KW
504{
505 return offset & ~(s->cluster_size - 1);
506}
507
ff99129a 508static inline int64_t offset_into_cluster(BDRVQcow2State *s, int64_t offset)
c37f4cd7
KW
509{
510 return offset & (s->cluster_size - 1);
511}
512
b6d36def 513static inline uint64_t size_to_clusters(BDRVQcow2State *s, uint64_t size)
f7d0fe02
KW
514{
515 return (size + (s->cluster_size - 1)) >> s->cluster_bits;
516}
517
ff99129a 518static inline int64_t size_to_l1(BDRVQcow2State *s, int64_t size)
419b19d9
SH
519{
520 int shift = s->cluster_bits + s->l2_bits;
521 return (size + (1ULL << shift) - 1) >> shift;
522}
523
05b5b6ee
AG
524static inline int offset_to_l1_index(BDRVQcow2State *s, uint64_t offset)
525{
526 return offset >> (s->l2_bits + s->cluster_bits);
527}
528
ff99129a 529static inline int offset_to_l2_index(BDRVQcow2State *s, int64_t offset)
17a71e58
KW
530{
531 return (offset >> s->cluster_bits) & (s->l2_size - 1);
532}
533
8f818175
AG
534static inline int offset_to_l2_slice_index(BDRVQcow2State *s, int64_t offset)
535{
536 return (offset >> s->cluster_bits) & (s->l2_slice_size - 1);
537}
538
ff99129a 539static inline int64_t qcow2_vm_state_offset(BDRVQcow2State *s)
1ebf561c
KW
540{
541 return (int64_t)s->l1_vm_state_index << (s->cluster_bits + s->l2_bits);
542}
543
808c2bb4
KW
544static inline QCow2ClusterType qcow2_get_cluster_type(BlockDriverState *bs,
545 uint64_t l2_entry)
68d000a3
KW
546{
547 if (l2_entry & QCOW_OFLAG_COMPRESSED) {
548 return QCOW2_CLUSTER_COMPRESSED;
6377af48 549 } else if (l2_entry & QCOW_OFLAG_ZERO) {
fdfab37d
EB
550 if (l2_entry & L2E_OFFSET_MASK) {
551 return QCOW2_CLUSTER_ZERO_ALLOC;
552 }
553 return QCOW2_CLUSTER_ZERO_PLAIN;
68d000a3 554 } else if (!(l2_entry & L2E_OFFSET_MASK)) {
a4ea184d
KW
555 /* Offset 0 generally means unallocated, but it is ambiguous with
556 * external data files because 0 is a valid offset there. However, all
557 * clusters in external data files always have refcount 1, so we can
558 * rely on QCOW_OFLAG_COPIED to disambiguate. */
559 if (has_data_file(bs) && (l2_entry & QCOW_OFLAG_COPIED)) {
560 return QCOW2_CLUSTER_NORMAL;
561 } else {
562 return QCOW2_CLUSTER_UNALLOCATED;
563 }
68d000a3
KW
564 } else {
565 return QCOW2_CLUSTER_NORMAL;
566 }
567}
568
bfe8043e 569/* Check whether refcounts are eager or lazy */
ff99129a 570static inline bool qcow2_need_accurate_refcounts(BDRVQcow2State *s)
bfe8043e
SH
571{
572 return !(s->incompatible_features & QCOW2_INCOMPAT_DIRTY);
573}
c142442b 574
65eb2e35
KW
575static inline uint64_t l2meta_cow_start(QCowL2Meta *m)
576{
577 return m->offset + m->cow_start.offset;
578}
579
580static inline uint64_t l2meta_cow_end(QCowL2Meta *m)
581{
85567393 582 return m->offset + m->cow_end.offset + m->cow_end.nb_bytes;
65eb2e35
KW
583}
584
0e06528e 585static inline uint64_t refcount_diff(uint64_t r1, uint64_t r2)
2aabe7c7
HR
586{
587 return r1 > r2 ? r1 - r2 : r2 - r1;
588}
589
46b732cd
PB
590static inline
591uint32_t offset_to_reftable_index(BDRVQcow2State *s, uint64_t offset)
592{
593 return offset >> (s->refcount_block_bits + s->cluster_bits);
594}
595
f7d0fe02 596/* qcow2.c functions */
12cc30a8
HR
597int64_t qcow2_refcount_metadata_size(int64_t clusters, size_t cluster_size,
598 int refcount_order, bool generous_increase,
599 uint64_t *refblock_count);
600
280d3735 601int qcow2_mark_dirty(BlockDriverState *bs);
69c98726
HR
602int qcow2_mark_corrupt(BlockDriverState *bs);
603int qcow2_mark_consistent(BlockDriverState *bs);
e24e49e6 604int qcow2_update_header(BlockDriverState *bs);
f7d0fe02 605
85186ebd
HR
606void qcow2_signal_corruption(BlockDriverState *bs, bool fatal, int64_t offset,
607 int64_t size, const char *message_format, ...)
608 GCC_FMT_ATTR(5, 6);
609
0cf0e598
AG
610int qcow2_validate_table(BlockDriverState *bs, uint64_t offset,
611 uint64_t entries, size_t entry_len,
612 int64_t max_size_bytes, const char *table_name,
613 Error **errp);
614
f7d0fe02 615/* qcow2-refcount.c functions */
ed6ccf0f
KW
616int qcow2_refcount_init(BlockDriverState *bs);
617void qcow2_refcount_close(BlockDriverState *bs);
f7d0fe02 618
7324c10f 619int qcow2_get_refcount(BlockDriverState *bs, int64_t cluster_index,
0e06528e 620 uint64_t *refcount);
44751917 621
32b6444d 622int qcow2_update_cluster_refcount(BlockDriverState *bs, int64_t cluster_index,
0e06528e 623 uint64_t addend, bool decrease,
2aabe7c7 624 enum qcow2_discard_type type);
32b6444d 625
772d1f97
HR
626int64_t qcow2_refcount_area(BlockDriverState *bs, uint64_t offset,
627 uint64_t additional_clusters, bool exact_size,
628 int new_refblock_index,
629 uint64_t new_refblock_offset);
630
bb572aef 631int64_t qcow2_alloc_clusters(BlockDriverState *bs, uint64_t size);
b6d36def
HR
632int64_t qcow2_alloc_clusters_at(BlockDriverState *bs, uint64_t offset,
633 int64_t nb_clusters);
ed6ccf0f
KW
634int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size);
635void qcow2_free_clusters(BlockDriverState *bs,
6cfcb9b8
KW
636 int64_t offset, int64_t size,
637 enum qcow2_discard_type type);
638void qcow2_free_any_clusters(BlockDriverState *bs, uint64_t l2_entry,
639 int nb_clusters, enum qcow2_discard_type type);
f7d0fe02 640
ed6ccf0f
KW
641int qcow2_update_snapshot_refcount(BlockDriverState *bs,
642 int64_t l1_table_offset, int l1_size, int addend);
f7d0fe02 643
8b220eb7
PB
644int coroutine_fn qcow2_flush_caches(BlockDriverState *bs);
645int coroutine_fn qcow2_write_caches(BlockDriverState *bs);
166acf54
KW
646int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
647 BdrvCheckMode fix);
f7d0fe02 648
0b919fae
KW
649void qcow2_process_discards(BlockDriverState *bs, int ret);
650
231bb267 651int qcow2_check_metadata_overlap(BlockDriverState *bs, int ign, int64_t offset,
a40f1c2a 652 int64_t size);
231bb267 653int qcow2_pre_write_overlap_check(BlockDriverState *bs, int ign, int64_t offset,
966b000f 654 int64_t size, bool data_file);
8a5bb1f1
VSO
655int qcow2_inc_refcounts_imrt(BlockDriverState *bs, BdrvCheckResult *res,
656 void **refcount_table,
657 int64_t *refcount_table_size,
658 int64_t offset, int64_t size);
a40f1c2a 659
791c9a00
HR
660int qcow2_change_refcount_order(BlockDriverState *bs, int refcount_order,
661 BlockDriverAmendStatusCB *status_cb,
662 void *cb_opaque, Error **errp);
46b732cd 663int qcow2_shrink_reftable(BlockDriverState *bs);
163bc39d 664int64_t qcow2_get_last_cluster(BlockDriverState *bs, int64_t size);
69f47505 665int qcow2_detect_metadata_preallocation(BlockDriverState *bs);
791c9a00 666
45aba42f 667/* qcow2-cluster.c functions */
2cf7cfa1
KW
668int qcow2_grow_l1_table(BlockDriverState *bs, uint64_t min_size,
669 bool exact_size);
46b732cd 670int qcow2_shrink_l1_table(BlockDriverState *bs, uint64_t max_size);
e23e400e 671int qcow2_write_l1_entry(BlockDriverState *bs, int l1_index);
ff99129a 672int qcow2_encrypt_sectors(BDRVQcow2State *s, int64_t sector_num,
446d306d 673 uint8_t *buf, int nb_sectors, bool enc, Error **errp);
45aba42f 674
1c46efaa 675int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
ecfe1863 676 unsigned int *bytes, uint64_t *cluster_offset);
f4f0d391 677int qcow2_alloc_cluster_offset(BlockDriverState *bs, uint64_t offset,
d46a0bb2
KW
678 unsigned int *bytes, uint64_t *host_offset,
679 QCowL2Meta **m);
77e023ff
KW
680int qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
681 uint64_t offset,
682 int compressed_size,
683 uint64_t *host_offset);
45aba42f 684
148da7ea 685int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m);
8b24cd14 686void qcow2_alloc_cluster_abort(BlockDriverState *bs, QCowL2Meta *m);
d2cb36af
EB
687int qcow2_cluster_discard(BlockDriverState *bs, uint64_t offset,
688 uint64_t bytes, enum qcow2_discard_type type,
689 bool full_discard);
690int qcow2_cluster_zeroize(BlockDriverState *bs, uint64_t offset,
691 uint64_t bytes, int flags);
45aba42f 692
4057a2b2 693int qcow2_expand_zero_clusters(BlockDriverState *bs,
8b13976d
HR
694 BlockDriverAmendStatusCB *status_cb,
695 void *cb_opaque);
32b6444d 696
c142442b 697/* qcow2-snapshot.c functions */
ed6ccf0f
KW
698int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info);
699int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id);
a89d89d3
WX
700int qcow2_snapshot_delete(BlockDriverState *bs,
701 const char *snapshot_id,
702 const char *name,
703 Error **errp);
ed6ccf0f 704int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab);
7b4c4781
WX
705int qcow2_snapshot_load_tmp(BlockDriverState *bs,
706 const char *snapshot_id,
707 const char *name,
708 Error **errp);
c142442b 709
ed6ccf0f
KW
710void qcow2_free_snapshots(BlockDriverState *bs);
711int qcow2_read_snapshots(BlockDriverState *bs);
c142442b 712
49381094 713/* qcow2-cache.c functions */
1221fe6f
AG
714Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables,
715 unsigned table_size);
e64d4072 716int qcow2_cache_destroy(Qcow2Cache *c);
49381094 717
2d135ee9 718void qcow2_cache_entry_mark_dirty(Qcow2Cache *c, void *table);
49381094 719int qcow2_cache_flush(BlockDriverState *bs, Qcow2Cache *c);
f3c3b87d 720int qcow2_cache_write(BlockDriverState *bs, Qcow2Cache *c);
49381094
KW
721int qcow2_cache_set_dependency(BlockDriverState *bs, Qcow2Cache *c,
722 Qcow2Cache *dependency);
3de0a294 723void qcow2_cache_depends_on_flush(Qcow2Cache *c);
49381094 724
b2f68bff 725void qcow2_cache_clean_unused(Qcow2Cache *c);
e7108fea
HR
726int qcow2_cache_empty(BlockDriverState *bs, Qcow2Cache *c);
727
49381094
KW
728int qcow2_cache_get(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
729 void **table);
730int qcow2_cache_get_empty(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
731 void **table);
2013c3d4 732void qcow2_cache_put(Qcow2Cache *c, void **table);
6e6fa760 733void *qcow2_cache_is_table_offset(Qcow2Cache *c, uint64_t offset);
77aadd7b 734void qcow2_cache_discard(Qcow2Cache *c, void *table);
49381094 735
88ddffae
VSO
736/* qcow2-bitmap.c functions */
737int qcow2_check_bitmaps_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
738 void **refcount_table,
739 int64_t *refcount_table_size);
3e99da5e 740bool qcow2_load_dirty_bitmaps(BlockDriverState *bs, Error **errp);
b8968c87
AS
741Qcow2BitmapInfoList *qcow2_get_bitmap_info_list(BlockDriverState *bs,
742 Error **errp);
1b6b0562 743int qcow2_reopen_bitmaps_rw(BlockDriverState *bs, Error **errp);
d19c6b36 744int qcow2_truncate_bitmaps_check(BlockDriverState *bs, Error **errp);
644ddbb7
VSO
745void qcow2_store_persistent_dirty_bitmaps(BlockDriverState *bs,
746 bool release_stored, Error **errp);
169b8793 747int qcow2_reopen_bitmaps_ro(BlockDriverState *bs, Error **errp);
d2c3080e
VSO
748bool qcow2_co_can_store_new_dirty_bitmap(BlockDriverState *bs,
749 const char *name,
750 uint32_t granularity,
b56a1e31 751 Error **errp);
d2c3080e
VSO
752int qcow2_co_remove_persistent_dirty_bitmap(BlockDriverState *bs,
753 const char *name,
754 Error **errp);
d1258dd0 755
56e2f1d8
VSO
756ssize_t coroutine_fn
757qcow2_co_compress(BlockDriverState *bs, void *dest, size_t dest_size,
758 const void *src, size_t src_size);
759ssize_t coroutine_fn
760qcow2_co_decompress(BlockDriverState *bs, void *dest, size_t dest_size,
761 const void *src, size_t src_size);
8ac0f15f 762int coroutine_fn
603fbd07
ML
763qcow2_co_encrypt(BlockDriverState *bs, uint64_t host_offset,
764 uint64_t guest_offset, void *buf, size_t len);
8ac0f15f 765int coroutine_fn
603fbd07
ML
766qcow2_co_decrypt(BlockDriverState *bs, uint64_t host_offset,
767 uint64_t guest_offset, void *buf, size_t len);
56e2f1d8 768
f7d0fe02 769#endif