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