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