nvdimm: Use configurable ACPI IO base and size
[qemu/ar7.git] / block / qcow2.h
blobf4de0a27d5c30384754750c25ee2f846241fbe81
1 /*
2 * Block driver for the QCOW version 2 format
4 * Copyright (c) 2004-2006 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
25 #ifndef BLOCK_QCOW2_H
26 #define BLOCK_QCOW2_H
28 #include "crypto/block.h"
29 #include "qemu/coroutine.h"
30 #include "qemu/units.h"
31 #include "block/block_int.h"
33 //#define DEBUG_ALLOC
34 //#define DEBUG_ALLOC2
35 //#define DEBUG_EXT
37 #define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
39 #define QCOW_CRYPT_NONE 0
40 #define QCOW_CRYPT_AES 1
41 #define QCOW_CRYPT_LUKS 2
43 #define QCOW_MAX_CRYPT_CLUSTERS 32
44 #define QCOW_MAX_SNAPSHOTS 65536
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)
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) */
54 #define QCOW_MAX_REFTABLE_SIZE (8 * MiB)
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) */
58 #define QCOW_MAX_L1_SIZE (32 * MiB)
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)
64 /* Maximum amount of extra data per snapshot table entry to accept */
65 #define QCOW_MAX_SNAPSHOT_EXTRA_DATA 1024
67 /* Bitmap header extension constraints */
68 #define QCOW2_MAX_BITMAPS 65535
69 #define QCOW2_MAX_BITMAP_DIRECTORY_SIZE (1024 * QCOW2_MAX_BITMAPS)
71 /* Maximum of parallel sub-request per guest request */
72 #define QCOW2_MAX_WORKERS 8
74 /* indicate that the refcount of the referenced cluster is exactly one. */
75 #define QCOW_OFLAG_COPIED (1ULL << 63)
76 /* indicate that the cluster is compressed (they never have the copied flag) */
77 #define QCOW_OFLAG_COMPRESSED (1ULL << 62)
78 /* The cluster reads as all zeros */
79 #define QCOW_OFLAG_ZERO (1ULL << 0)
81 #define MIN_CLUSTER_BITS 9
82 #define MAX_CLUSTER_BITS 21
84 /* Defined in the qcow2 spec (compressed cluster descriptor) */
85 #define QCOW2_COMPRESSED_SECTOR_SIZE 512U
86 #define QCOW2_COMPRESSED_SECTOR_MASK (~(QCOW2_COMPRESSED_SECTOR_SIZE - 1ULL))
88 /* Must be at least 2 to cover COW */
89 #define MIN_L2_CACHE_SIZE 2 /* cache entries */
91 /* Must be at least 4 to cover all cases of refcount table growth */
92 #define MIN_REFCOUNT_CACHE_SIZE 4 /* clusters */
94 #ifdef CONFIG_LINUX
95 #define DEFAULT_L2_CACHE_MAX_SIZE (32 * MiB)
96 #define DEFAULT_CACHE_CLEAN_INTERVAL 600 /* seconds */
97 #else
98 #define DEFAULT_L2_CACHE_MAX_SIZE (8 * MiB)
99 /* Cache clean interval is currently available only on Linux, so must be 0 */
100 #define DEFAULT_CACHE_CLEAN_INTERVAL 0
101 #endif
103 #define DEFAULT_CLUSTER_SIZE 65536
105 #define QCOW2_OPT_DATA_FILE "data-file"
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"
110 #define QCOW2_OPT_OVERLAP "overlap-check"
111 #define QCOW2_OPT_OVERLAP_TEMPLATE "overlap-check.template"
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"
120 #define QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY "overlap-check.bitmap-directory"
121 #define QCOW2_OPT_CACHE_SIZE "cache-size"
122 #define QCOW2_OPT_L2_CACHE_SIZE "l2-cache-size"
123 #define QCOW2_OPT_L2_CACHE_ENTRY_SIZE "l2-cache-entry-size"
124 #define QCOW2_OPT_REFCOUNT_CACHE_SIZE "refcount-cache-size"
125 #define QCOW2_OPT_CACHE_CLEAN_INTERVAL "cache-clean-interval"
127 typedef 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;
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;
147 uint32_t refcount_order;
148 uint32_t header_length;
149 } QEMU_PACKED QCowHeader;
151 typedef struct QEMU_PACKED QCowSnapshotHeader {
152 /* header is 8 byte aligned */
153 uint64_t l1_table_offset;
155 uint32_t l1_size;
156 uint16_t id_str_size;
157 uint16_t name_size;
159 uint32_t date_sec;
160 uint32_t date_nsec;
162 uint64_t vm_clock_nsec;
164 uint32_t vm_state_size;
165 uint32_t extra_data_size; /* for extension */
166 /* extra data follows */
167 /* id_str follows */
168 /* name follows */
169 } QCowSnapshotHeader;
171 typedef struct QEMU_PACKED QCowSnapshotExtraData {
172 uint64_t vm_state_size_large;
173 uint64_t disk_size;
174 } QCowSnapshotExtraData;
177 typedef struct QCowSnapshot {
178 uint64_t l1_table_offset;
179 uint32_t l1_size;
180 char *id_str;
181 char *name;
182 uint64_t disk_size;
183 uint64_t vm_state_size;
184 uint32_t date_sec;
185 uint32_t date_nsec;
186 uint64_t vm_clock_nsec;
187 /* Size of all extra data, including QCowSnapshotExtraData if available */
188 uint32_t extra_data_size;
189 /* Data beyond QCowSnapshotExtraData, if any */
190 void *unknown_extra_data;
191 } QCowSnapshot;
193 struct Qcow2Cache;
194 typedef struct Qcow2Cache Qcow2Cache;
196 typedef struct Qcow2CryptoHeaderExtension {
197 uint64_t offset;
198 uint64_t length;
199 } QEMU_PACKED Qcow2CryptoHeaderExtension;
201 typedef struct Qcow2UnknownHeaderExtension {
202 uint32_t magic;
203 uint32_t len;
204 QLIST_ENTRY(Qcow2UnknownHeaderExtension) next;
205 uint8_t data[];
206 } Qcow2UnknownHeaderExtension;
208 enum {
209 QCOW2_FEAT_TYPE_INCOMPATIBLE = 0,
210 QCOW2_FEAT_TYPE_COMPATIBLE = 1,
211 QCOW2_FEAT_TYPE_AUTOCLEAR = 2,
214 /* Incompatible feature bits */
215 enum {
216 QCOW2_INCOMPAT_DIRTY_BITNR = 0,
217 QCOW2_INCOMPAT_CORRUPT_BITNR = 1,
218 QCOW2_INCOMPAT_DATA_FILE_BITNR = 2,
219 QCOW2_INCOMPAT_DIRTY = 1 << QCOW2_INCOMPAT_DIRTY_BITNR,
220 QCOW2_INCOMPAT_CORRUPT = 1 << QCOW2_INCOMPAT_CORRUPT_BITNR,
221 QCOW2_INCOMPAT_DATA_FILE = 1 << QCOW2_INCOMPAT_DATA_FILE_BITNR,
223 QCOW2_INCOMPAT_MASK = QCOW2_INCOMPAT_DIRTY
224 | QCOW2_INCOMPAT_CORRUPT
225 | QCOW2_INCOMPAT_DATA_FILE,
228 /* Compatible feature bits */
229 enum {
230 QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR = 0,
231 QCOW2_COMPAT_LAZY_REFCOUNTS = 1 << QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR,
233 QCOW2_COMPAT_FEAT_MASK = QCOW2_COMPAT_LAZY_REFCOUNTS,
236 /* Autoclear feature bits */
237 enum {
238 QCOW2_AUTOCLEAR_BITMAPS_BITNR = 0,
239 QCOW2_AUTOCLEAR_DATA_FILE_RAW_BITNR = 1,
240 QCOW2_AUTOCLEAR_BITMAPS = 1 << QCOW2_AUTOCLEAR_BITMAPS_BITNR,
241 QCOW2_AUTOCLEAR_DATA_FILE_RAW = 1 << QCOW2_AUTOCLEAR_DATA_FILE_RAW_BITNR,
243 QCOW2_AUTOCLEAR_MASK = QCOW2_AUTOCLEAR_BITMAPS
244 | QCOW2_AUTOCLEAR_DATA_FILE_RAW,
247 enum qcow2_discard_type {
248 QCOW2_DISCARD_NEVER = 0,
249 QCOW2_DISCARD_ALWAYS,
250 QCOW2_DISCARD_REQUEST,
251 QCOW2_DISCARD_SNAPSHOT,
252 QCOW2_DISCARD_OTHER,
253 QCOW2_DISCARD_MAX
256 typedef struct Qcow2Feature {
257 uint8_t type;
258 uint8_t bit;
259 char name[46];
260 } QEMU_PACKED Qcow2Feature;
262 typedef struct Qcow2DiscardRegion {
263 BlockDriverState *bs;
264 uint64_t offset;
265 uint64_t bytes;
266 QTAILQ_ENTRY(Qcow2DiscardRegion) next;
267 } Qcow2DiscardRegion;
269 typedef uint64_t Qcow2GetRefcountFunc(const void *refcount_array,
270 uint64_t index);
271 typedef void Qcow2SetRefcountFunc(void *refcount_array,
272 uint64_t index, uint64_t value);
274 typedef struct Qcow2BitmapHeaderExt {
275 uint32_t nb_bitmaps;
276 uint32_t reserved32;
277 uint64_t bitmap_directory_size;
278 uint64_t bitmap_directory_offset;
279 } QEMU_PACKED Qcow2BitmapHeaderExt;
281 #define QCOW2_MAX_THREADS 4
283 typedef struct BDRVQcow2State {
284 int cluster_bits;
285 int cluster_size;
286 int l2_slice_size;
287 int l2_bits;
288 int l2_size;
289 int l1_size;
290 int l1_vm_state_index;
291 int refcount_block_bits;
292 int refcount_block_size;
293 int csize_shift;
294 int csize_mask;
295 uint64_t cluster_offset_mask;
296 uint64_t l1_table_offset;
297 uint64_t *l1_table;
299 Qcow2Cache* l2_table_cache;
300 Qcow2Cache* refcount_block_cache;
301 QEMUTimer *cache_clean_timer;
302 unsigned cache_clean_interval;
304 QLIST_HEAD(, QCowL2Meta) cluster_allocs;
306 uint64_t *refcount_table;
307 uint64_t refcount_table_offset;
308 uint32_t refcount_table_size;
309 uint32_t max_refcount_table_index; /* Last used entry in refcount_table */
310 uint64_t free_cluster_index;
311 uint64_t free_byte_offset;
313 CoMutex lock;
315 Qcow2CryptoHeaderExtension crypto_header; /* QCow2 header extension */
316 QCryptoBlockOpenOptions *crypto_opts; /* Disk encryption runtime options */
317 QCryptoBlock *crypto; /* Disk encryption format driver */
318 bool crypt_physical_offset; /* Whether to use virtual or physical offset
319 for encryption initialization vector tweak */
320 uint32_t crypt_method_header;
321 uint64_t snapshots_offset;
322 int snapshots_size;
323 unsigned int nb_snapshots;
324 QCowSnapshot *snapshots;
326 uint32_t nb_bitmaps;
327 uint64_t bitmap_directory_size;
328 uint64_t bitmap_directory_offset;
330 int flags;
331 int qcow_version;
332 bool use_lazy_refcounts;
333 int refcount_order;
334 int refcount_bits;
335 uint64_t refcount_max;
337 Qcow2GetRefcountFunc *get_refcount;
338 Qcow2SetRefcountFunc *set_refcount;
340 bool discard_passthrough[QCOW2_DISCARD_MAX];
342 int overlap_check; /* bitmask of Qcow2MetadataOverlap values */
343 bool signaled_corruption;
345 uint64_t incompatible_features;
346 uint64_t compatible_features;
347 uint64_t autoclear_features;
349 size_t unknown_header_fields_size;
350 void* unknown_header_fields;
351 QLIST_HEAD(, Qcow2UnknownHeaderExtension) unknown_header_ext;
352 QTAILQ_HEAD (, Qcow2DiscardRegion) discards;
353 bool cache_discards;
355 /* Backing file path and format as stored in the image (this is not the
356 * effective path/format, which may be the result of a runtime option
357 * override) */
358 char *image_backing_file;
359 char *image_backing_format;
360 char *image_data_file;
362 CoQueue thread_task_queue;
363 int nb_threads;
365 BdrvChild *data_file;
367 bool metadata_preallocation_checked;
368 bool metadata_preallocation;
369 } BDRVQcow2State;
371 typedef struct Qcow2COWRegion {
373 * Offset of the COW region in bytes from the start of the first cluster
374 * touched by the request.
376 unsigned offset;
378 /** Number of bytes to copy */
379 unsigned nb_bytes;
380 } Qcow2COWRegion;
383 * Describes an in-flight (part of a) write request that writes to clusters
384 * that are not referenced in their L2 table yet.
386 typedef struct QCowL2Meta
388 /** Guest offset of the first newly allocated cluster */
389 uint64_t offset;
391 /** Host offset of the first newly allocated cluster */
392 uint64_t alloc_offset;
394 /** Number of newly allocated clusters */
395 int nb_clusters;
397 /** Do not free the old clusters */
398 bool keep_old_clusters;
401 * Requests that overlap with this allocation and wait to be restarted
402 * when the allocating request has completed.
404 CoQueue dependent_requests;
407 * The COW Region between the start of the first allocated cluster and the
408 * area the guest actually writes to.
410 Qcow2COWRegion cow_start;
413 * The COW Region between the area the guest actually writes to and the
414 * end of the last allocated cluster.
416 Qcow2COWRegion cow_end;
419 * Indicates that COW regions are already handled and do not require
420 * any more processing.
422 bool skip_cow;
425 * The I/O vector with the data from the actual guest write request.
426 * If non-NULL, this is meant to be merged together with the data
427 * from @cow_start and @cow_end into one single write operation.
429 QEMUIOVector *data_qiov;
430 size_t data_qiov_offset;
432 /** Pointer to next L2Meta of the same write request */
433 struct QCowL2Meta *next;
435 QLIST_ENTRY(QCowL2Meta) next_in_flight;
436 } QCowL2Meta;
438 typedef enum QCow2ClusterType {
439 QCOW2_CLUSTER_UNALLOCATED,
440 QCOW2_CLUSTER_ZERO_PLAIN,
441 QCOW2_CLUSTER_ZERO_ALLOC,
442 QCOW2_CLUSTER_NORMAL,
443 QCOW2_CLUSTER_COMPRESSED,
444 } QCow2ClusterType;
446 typedef enum QCow2MetadataOverlap {
447 QCOW2_OL_MAIN_HEADER_BITNR = 0,
448 QCOW2_OL_ACTIVE_L1_BITNR = 1,
449 QCOW2_OL_ACTIVE_L2_BITNR = 2,
450 QCOW2_OL_REFCOUNT_TABLE_BITNR = 3,
451 QCOW2_OL_REFCOUNT_BLOCK_BITNR = 4,
452 QCOW2_OL_SNAPSHOT_TABLE_BITNR = 5,
453 QCOW2_OL_INACTIVE_L1_BITNR = 6,
454 QCOW2_OL_INACTIVE_L2_BITNR = 7,
455 QCOW2_OL_BITMAP_DIRECTORY_BITNR = 8,
457 QCOW2_OL_MAX_BITNR = 9,
459 QCOW2_OL_NONE = 0,
460 QCOW2_OL_MAIN_HEADER = (1 << QCOW2_OL_MAIN_HEADER_BITNR),
461 QCOW2_OL_ACTIVE_L1 = (1 << QCOW2_OL_ACTIVE_L1_BITNR),
462 QCOW2_OL_ACTIVE_L2 = (1 << QCOW2_OL_ACTIVE_L2_BITNR),
463 QCOW2_OL_REFCOUNT_TABLE = (1 << QCOW2_OL_REFCOUNT_TABLE_BITNR),
464 QCOW2_OL_REFCOUNT_BLOCK = (1 << QCOW2_OL_REFCOUNT_BLOCK_BITNR),
465 QCOW2_OL_SNAPSHOT_TABLE = (1 << QCOW2_OL_SNAPSHOT_TABLE_BITNR),
466 QCOW2_OL_INACTIVE_L1 = (1 << QCOW2_OL_INACTIVE_L1_BITNR),
467 /* NOTE: Checking overlaps with inactive L2 tables will result in bdrv
468 * reads. */
469 QCOW2_OL_INACTIVE_L2 = (1 << QCOW2_OL_INACTIVE_L2_BITNR),
470 QCOW2_OL_BITMAP_DIRECTORY = (1 << QCOW2_OL_BITMAP_DIRECTORY_BITNR),
471 } QCow2MetadataOverlap;
473 /* Perform all overlap checks which can be done in constant time */
474 #define QCOW2_OL_CONSTANT \
475 (QCOW2_OL_MAIN_HEADER | QCOW2_OL_ACTIVE_L1 | QCOW2_OL_REFCOUNT_TABLE | \
476 QCOW2_OL_SNAPSHOT_TABLE | QCOW2_OL_BITMAP_DIRECTORY)
478 /* Perform all overlap checks which don't require disk access */
479 #define QCOW2_OL_CACHED \
480 (QCOW2_OL_CONSTANT | QCOW2_OL_ACTIVE_L2 | QCOW2_OL_REFCOUNT_BLOCK | \
481 QCOW2_OL_INACTIVE_L1)
483 /* Perform all overlap checks */
484 #define QCOW2_OL_ALL \
485 (QCOW2_OL_CACHED | QCOW2_OL_INACTIVE_L2)
487 #define L1E_OFFSET_MASK 0x00fffffffffffe00ULL
488 #define L2E_OFFSET_MASK 0x00fffffffffffe00ULL
489 #define L2E_COMPRESSED_OFFSET_SIZE_MASK 0x3fffffffffffffffULL
491 #define REFT_OFFSET_MASK 0xfffffffffffffe00ULL
493 #define INV_OFFSET (-1ULL)
495 static inline bool has_data_file(BlockDriverState *bs)
497 BDRVQcow2State *s = bs->opaque;
498 return (s->data_file != bs->file);
501 static inline bool data_file_is_raw(BlockDriverState *bs)
503 BDRVQcow2State *s = bs->opaque;
504 return !!(s->autoclear_features & QCOW2_AUTOCLEAR_DATA_FILE_RAW);
507 static inline int64_t start_of_cluster(BDRVQcow2State *s, int64_t offset)
509 return offset & ~(s->cluster_size - 1);
512 static inline int64_t offset_into_cluster(BDRVQcow2State *s, int64_t offset)
514 return offset & (s->cluster_size - 1);
517 static inline uint64_t size_to_clusters(BDRVQcow2State *s, uint64_t size)
519 return (size + (s->cluster_size - 1)) >> s->cluster_bits;
522 static inline int64_t size_to_l1(BDRVQcow2State *s, int64_t size)
524 int shift = s->cluster_bits + s->l2_bits;
525 return (size + (1ULL << shift) - 1) >> shift;
528 static inline int offset_to_l1_index(BDRVQcow2State *s, uint64_t offset)
530 return offset >> (s->l2_bits + s->cluster_bits);
533 static inline int offset_to_l2_index(BDRVQcow2State *s, int64_t offset)
535 return (offset >> s->cluster_bits) & (s->l2_size - 1);
538 static inline int offset_to_l2_slice_index(BDRVQcow2State *s, int64_t offset)
540 return (offset >> s->cluster_bits) & (s->l2_slice_size - 1);
543 static inline int64_t qcow2_vm_state_offset(BDRVQcow2State *s)
545 return (int64_t)s->l1_vm_state_index << (s->cluster_bits + s->l2_bits);
548 static inline QCow2ClusterType qcow2_get_cluster_type(BlockDriverState *bs,
549 uint64_t l2_entry)
551 if (l2_entry & QCOW_OFLAG_COMPRESSED) {
552 return QCOW2_CLUSTER_COMPRESSED;
553 } else if (l2_entry & QCOW_OFLAG_ZERO) {
554 if (l2_entry & L2E_OFFSET_MASK) {
555 return QCOW2_CLUSTER_ZERO_ALLOC;
557 return QCOW2_CLUSTER_ZERO_PLAIN;
558 } else if (!(l2_entry & L2E_OFFSET_MASK)) {
559 /* Offset 0 generally means unallocated, but it is ambiguous with
560 * external data files because 0 is a valid offset there. However, all
561 * clusters in external data files always have refcount 1, so we can
562 * rely on QCOW_OFLAG_COPIED to disambiguate. */
563 if (has_data_file(bs) && (l2_entry & QCOW_OFLAG_COPIED)) {
564 return QCOW2_CLUSTER_NORMAL;
565 } else {
566 return QCOW2_CLUSTER_UNALLOCATED;
568 } else {
569 return QCOW2_CLUSTER_NORMAL;
573 /* Check whether refcounts are eager or lazy */
574 static inline bool qcow2_need_accurate_refcounts(BDRVQcow2State *s)
576 return !(s->incompatible_features & QCOW2_INCOMPAT_DIRTY);
579 static inline uint64_t l2meta_cow_start(QCowL2Meta *m)
581 return m->offset + m->cow_start.offset;
584 static inline uint64_t l2meta_cow_end(QCowL2Meta *m)
586 return m->offset + m->cow_end.offset + m->cow_end.nb_bytes;
589 static inline uint64_t refcount_diff(uint64_t r1, uint64_t r2)
591 return r1 > r2 ? r1 - r2 : r2 - r1;
594 static inline
595 uint32_t offset_to_reftable_index(BDRVQcow2State *s, uint64_t offset)
597 return offset >> (s->refcount_block_bits + s->cluster_bits);
600 /* qcow2.c functions */
601 int64_t qcow2_refcount_metadata_size(int64_t clusters, size_t cluster_size,
602 int refcount_order, bool generous_increase,
603 uint64_t *refblock_count);
605 int qcow2_mark_dirty(BlockDriverState *bs);
606 int qcow2_mark_corrupt(BlockDriverState *bs);
607 int qcow2_mark_consistent(BlockDriverState *bs);
608 int qcow2_update_header(BlockDriverState *bs);
610 void qcow2_signal_corruption(BlockDriverState *bs, bool fatal, int64_t offset,
611 int64_t size, const char *message_format, ...)
612 GCC_FMT_ATTR(5, 6);
614 int qcow2_validate_table(BlockDriverState *bs, uint64_t offset,
615 uint64_t entries, size_t entry_len,
616 int64_t max_size_bytes, const char *table_name,
617 Error **errp);
619 /* qcow2-refcount.c functions */
620 int qcow2_refcount_init(BlockDriverState *bs);
621 void qcow2_refcount_close(BlockDriverState *bs);
623 int qcow2_get_refcount(BlockDriverState *bs, int64_t cluster_index,
624 uint64_t *refcount);
626 int qcow2_update_cluster_refcount(BlockDriverState *bs, int64_t cluster_index,
627 uint64_t addend, bool decrease,
628 enum qcow2_discard_type type);
630 int64_t qcow2_refcount_area(BlockDriverState *bs, uint64_t offset,
631 uint64_t additional_clusters, bool exact_size,
632 int new_refblock_index,
633 uint64_t new_refblock_offset);
635 int64_t qcow2_alloc_clusters(BlockDriverState *bs, uint64_t size);
636 int64_t qcow2_alloc_clusters_at(BlockDriverState *bs, uint64_t offset,
637 int64_t nb_clusters);
638 int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size);
639 void qcow2_free_clusters(BlockDriverState *bs,
640 int64_t offset, int64_t size,
641 enum qcow2_discard_type type);
642 void qcow2_free_any_clusters(BlockDriverState *bs, uint64_t l2_entry,
643 int nb_clusters, enum qcow2_discard_type type);
645 int qcow2_update_snapshot_refcount(BlockDriverState *bs,
646 int64_t l1_table_offset, int l1_size, int addend);
648 int coroutine_fn qcow2_flush_caches(BlockDriverState *bs);
649 int coroutine_fn qcow2_write_caches(BlockDriverState *bs);
650 int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
651 BdrvCheckMode fix);
653 void qcow2_process_discards(BlockDriverState *bs, int ret);
655 int qcow2_check_metadata_overlap(BlockDriverState *bs, int ign, int64_t offset,
656 int64_t size);
657 int qcow2_pre_write_overlap_check(BlockDriverState *bs, int ign, int64_t offset,
658 int64_t size, bool data_file);
659 int qcow2_inc_refcounts_imrt(BlockDriverState *bs, BdrvCheckResult *res,
660 void **refcount_table,
661 int64_t *refcount_table_size,
662 int64_t offset, int64_t size);
664 int qcow2_change_refcount_order(BlockDriverState *bs, int refcount_order,
665 BlockDriverAmendStatusCB *status_cb,
666 void *cb_opaque, Error **errp);
667 int qcow2_shrink_reftable(BlockDriverState *bs);
668 int64_t qcow2_get_last_cluster(BlockDriverState *bs, int64_t size);
669 int qcow2_detect_metadata_preallocation(BlockDriverState *bs);
671 /* qcow2-cluster.c functions */
672 int qcow2_grow_l1_table(BlockDriverState *bs, uint64_t min_size,
673 bool exact_size);
674 int qcow2_shrink_l1_table(BlockDriverState *bs, uint64_t max_size);
675 int qcow2_write_l1_entry(BlockDriverState *bs, int l1_index);
676 int qcow2_encrypt_sectors(BDRVQcow2State *s, int64_t sector_num,
677 uint8_t *buf, int nb_sectors, bool enc, Error **errp);
679 int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
680 unsigned int *bytes, uint64_t *cluster_offset);
681 int qcow2_alloc_cluster_offset(BlockDriverState *bs, uint64_t offset,
682 unsigned int *bytes, uint64_t *host_offset,
683 QCowL2Meta **m);
684 int qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
685 uint64_t offset,
686 int compressed_size,
687 uint64_t *host_offset);
689 int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m);
690 void qcow2_alloc_cluster_abort(BlockDriverState *bs, QCowL2Meta *m);
691 int qcow2_cluster_discard(BlockDriverState *bs, uint64_t offset,
692 uint64_t bytes, enum qcow2_discard_type type,
693 bool full_discard);
694 int qcow2_cluster_zeroize(BlockDriverState *bs, uint64_t offset,
695 uint64_t bytes, int flags);
697 int qcow2_expand_zero_clusters(BlockDriverState *bs,
698 BlockDriverAmendStatusCB *status_cb,
699 void *cb_opaque);
701 /* qcow2-snapshot.c functions */
702 int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info);
703 int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id);
704 int qcow2_snapshot_delete(BlockDriverState *bs,
705 const char *snapshot_id,
706 const char *name,
707 Error **errp);
708 int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab);
709 int qcow2_snapshot_load_tmp(BlockDriverState *bs,
710 const char *snapshot_id,
711 const char *name,
712 Error **errp);
714 void qcow2_free_snapshots(BlockDriverState *bs);
715 int qcow2_read_snapshots(BlockDriverState *bs, Error **errp);
716 int qcow2_write_snapshots(BlockDriverState *bs);
718 int coroutine_fn qcow2_check_read_snapshot_table(BlockDriverState *bs,
719 BdrvCheckResult *result,
720 BdrvCheckMode fix);
721 int coroutine_fn qcow2_check_fix_snapshot_table(BlockDriverState *bs,
722 BdrvCheckResult *result,
723 BdrvCheckMode fix);
725 /* qcow2-cache.c functions */
726 Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables,
727 unsigned table_size);
728 int qcow2_cache_destroy(Qcow2Cache *c);
730 void qcow2_cache_entry_mark_dirty(Qcow2Cache *c, void *table);
731 int qcow2_cache_flush(BlockDriverState *bs, Qcow2Cache *c);
732 int qcow2_cache_write(BlockDriverState *bs, Qcow2Cache *c);
733 int qcow2_cache_set_dependency(BlockDriverState *bs, Qcow2Cache *c,
734 Qcow2Cache *dependency);
735 void qcow2_cache_depends_on_flush(Qcow2Cache *c);
737 void qcow2_cache_clean_unused(Qcow2Cache *c);
738 int qcow2_cache_empty(BlockDriverState *bs, Qcow2Cache *c);
740 int qcow2_cache_get(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
741 void **table);
742 int qcow2_cache_get_empty(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
743 void **table);
744 void qcow2_cache_put(Qcow2Cache *c, void **table);
745 void *qcow2_cache_is_table_offset(Qcow2Cache *c, uint64_t offset);
746 void qcow2_cache_discard(Qcow2Cache *c, void *table);
748 /* qcow2-bitmap.c functions */
749 int qcow2_check_bitmaps_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
750 void **refcount_table,
751 int64_t *refcount_table_size);
752 bool qcow2_load_dirty_bitmaps(BlockDriverState *bs, Error **errp);
753 Qcow2BitmapInfoList *qcow2_get_bitmap_info_list(BlockDriverState *bs,
754 Error **errp);
755 int qcow2_reopen_bitmaps_rw(BlockDriverState *bs, Error **errp);
756 int qcow2_truncate_bitmaps_check(BlockDriverState *bs, Error **errp);
757 void qcow2_store_persistent_dirty_bitmaps(BlockDriverState *bs,
758 bool release_stored, Error **errp);
759 int qcow2_reopen_bitmaps_ro(BlockDriverState *bs, Error **errp);
760 bool qcow2_co_can_store_new_dirty_bitmap(BlockDriverState *bs,
761 const char *name,
762 uint32_t granularity,
763 Error **errp);
764 int qcow2_co_remove_persistent_dirty_bitmap(BlockDriverState *bs,
765 const char *name,
766 Error **errp);
768 ssize_t coroutine_fn
769 qcow2_co_compress(BlockDriverState *bs, void *dest, size_t dest_size,
770 const void *src, size_t src_size);
771 ssize_t coroutine_fn
772 qcow2_co_decompress(BlockDriverState *bs, void *dest, size_t dest_size,
773 const void *src, size_t src_size);
774 int coroutine_fn
775 qcow2_co_encrypt(BlockDriverState *bs, uint64_t host_offset,
776 uint64_t guest_offset, void *buf, size_t len);
777 int coroutine_fn
778 qcow2_co_decrypt(BlockDriverState *bs, uint64_t host_offset,
779 uint64_t guest_offset, void *buf, size_t len);
781 #endif