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