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