1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#ifndef BLOCK_QCOW2_H
26#define BLOCK_QCOW2_H
27
28#include "crypto/block.h"
29#include "qemu/coroutine.h"
30#include "qemu/units.h"
31#include "block/block_int.h"
32
33
34
35
36
37#define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
38
39#define QCOW_CRYPT_NONE 0
40#define QCOW_CRYPT_AES 1
41#define QCOW_CRYPT_LUKS 2
42
43#define QCOW_MAX_CRYPT_CLUSTERS 32
44#define QCOW_MAX_SNAPSHOTS 65536
45
46
47
48
49
50#define QCOW_MAX_CLUSTER_OFFSET ((1ULL << 56) - 1)
51
52
53
54#define QCOW_MAX_REFTABLE_SIZE (8 * MiB)
55
56
57
58#define QCOW_MAX_L1_SIZE (32 * MiB)
59
60
61
62#define QCOW_MAX_SNAPSHOTS_SIZE (1024 * QCOW_MAX_SNAPSHOTS)
63
64
65#define QCOW_MAX_SNAPSHOT_EXTRA_DATA 1024
66
67
68#define QCOW2_MAX_BITMAPS 65535
69#define QCOW2_MAX_BITMAP_DIRECTORY_SIZE (1024 * QCOW2_MAX_BITMAPS)
70
71
72#define QCOW2_MAX_WORKERS 8
73
74
75#define QCOW_OFLAG_COPIED (1ULL << 63)
76
77#define QCOW_OFLAG_COMPRESSED (1ULL << 62)
78
79#define QCOW_OFLAG_ZERO (1ULL << 0)
80
81#define QCOW_EXTL2_SUBCLUSTERS_PER_CLUSTER 32
82
83
84#define QCOW_OFLAG_SUB_ALLOC(X) (1ULL << (X))
85
86#define QCOW_OFLAG_SUB_ZERO(X) (QCOW_OFLAG_SUB_ALLOC(X) << 32)
87
88#define QCOW_OFLAG_SUB_ALLOC_RANGE(X, Y) \
89 (QCOW_OFLAG_SUB_ALLOC(Y) - QCOW_OFLAG_SUB_ALLOC(X))
90
91#define QCOW_OFLAG_SUB_ZERO_RANGE(X, Y) \
92 (QCOW_OFLAG_SUB_ALLOC_RANGE(X, Y) << 32)
93
94#define QCOW_L2_BITMAP_ALL_ALLOC (QCOW_OFLAG_SUB_ALLOC_RANGE(0, 32))
95
96#define QCOW_L2_BITMAP_ALL_ZEROES (QCOW_OFLAG_SUB_ZERO_RANGE(0, 32))
97
98
99#define L2E_SIZE_NORMAL (sizeof(uint64_t))
100#define L2E_SIZE_EXTENDED (sizeof(uint64_t) * 2)
101
102
103#define L1E_SIZE (sizeof(uint64_t))
104
105
106#define REFTABLE_ENTRY_SIZE (sizeof(uint64_t))
107
108#define MIN_CLUSTER_BITS 9
109#define MAX_CLUSTER_BITS 21
110
111
112#define QCOW2_COMPRESSED_SECTOR_SIZE 512U
113
114
115#define MIN_L2_CACHE_SIZE 2
116
117
118#define MIN_REFCOUNT_CACHE_SIZE 4
119
120#ifdef CONFIG_LINUX
121#define DEFAULT_L2_CACHE_MAX_SIZE (32 * MiB)
122#define DEFAULT_CACHE_CLEAN_INTERVAL 600
123#else
124#define DEFAULT_L2_CACHE_MAX_SIZE (8 * MiB)
125
126#define DEFAULT_CACHE_CLEAN_INTERVAL 0
127#endif
128
129#define DEFAULT_CLUSTER_SIZE 65536
130
131#define QCOW2_OPT_DATA_FILE "data-file"
132#define QCOW2_OPT_LAZY_REFCOUNTS "lazy-refcounts"
133#define QCOW2_OPT_DISCARD_REQUEST "pass-discard-request"
134#define QCOW2_OPT_DISCARD_SNAPSHOT "pass-discard-snapshot"
135#define QCOW2_OPT_DISCARD_OTHER "pass-discard-other"
136#define QCOW2_OPT_DISCARD_NO_UNREF "discard-no-unref"
137#define QCOW2_OPT_OVERLAP "overlap-check"
138#define QCOW2_OPT_OVERLAP_TEMPLATE "overlap-check.template"
139#define QCOW2_OPT_OVERLAP_MAIN_HEADER "overlap-check.main-header"
140#define QCOW2_OPT_OVERLAP_ACTIVE_L1 "overlap-check.active-l1"
141#define QCOW2_OPT_OVERLAP_ACTIVE_L2 "overlap-check.active-l2"
142#define QCOW2_OPT_OVERLAP_REFCOUNT_TABLE "overlap-check.refcount-table"
143#define QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK "overlap-check.refcount-block"
144#define QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE "overlap-check.snapshot-table"
145#define QCOW2_OPT_OVERLAP_INACTIVE_L1 "overlap-check.inactive-l1"
146#define QCOW2_OPT_OVERLAP_INACTIVE_L2 "overlap-check.inactive-l2"
147#define QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY "overlap-check.bitmap-directory"
148#define QCOW2_OPT_CACHE_SIZE "cache-size"
149#define QCOW2_OPT_L2_CACHE_SIZE "l2-cache-size"
150#define QCOW2_OPT_L2_CACHE_ENTRY_SIZE "l2-cache-entry-size"
151#define QCOW2_OPT_REFCOUNT_CACHE_SIZE "refcount-cache-size"
152#define QCOW2_OPT_CACHE_CLEAN_INTERVAL "cache-clean-interval"
153
154typedef struct QCowHeader {
155 uint32_t magic;
156 uint32_t version;
157 uint64_t backing_file_offset;
158 uint32_t backing_file_size;
159 uint32_t cluster_bits;
160 uint64_t size;
161 uint32_t crypt_method;
162 uint32_t l1_size;
163 uint64_t l1_table_offset;
164 uint64_t refcount_table_offset;
165 uint32_t refcount_table_clusters;
166 uint32_t nb_snapshots;
167 uint64_t snapshots_offset;
168
169
170 uint64_t incompatible_features;
171 uint64_t compatible_features;
172 uint64_t autoclear_features;
173
174 uint32_t refcount_order;
175 uint32_t header_length;
176
177
178 uint8_t compression_type;
179
180
181 uint8_t padding[7];
182} QEMU_PACKED QCowHeader;
183
184QEMU_BUILD_BUG_ON(!QEMU_IS_ALIGNED(sizeof(QCowHeader), 8));
185
186typedef struct QEMU_PACKED QCowSnapshotHeader {
187
188 uint64_t l1_table_offset;
189
190 uint32_t l1_size;
191 uint16_t id_str_size;
192 uint16_t name_size;
193
194 uint32_t date_sec;
195 uint32_t date_nsec;
196
197 uint64_t vm_clock_nsec;
198
199 uint32_t vm_state_size;
200 uint32_t extra_data_size;
201
202
203
204} QCowSnapshotHeader;
205
206typedef struct QEMU_PACKED QCowSnapshotExtraData {
207 uint64_t vm_state_size_large;
208 uint64_t disk_size;
209 uint64_t icount;
210} QCowSnapshotExtraData;
211
212
213typedef struct QCowSnapshot {
214 uint64_t l1_table_offset;
215 uint32_t l1_size;
216 char *id_str;
217 char *name;
218 uint64_t disk_size;
219 uint64_t vm_state_size;
220 uint32_t date_sec;
221 uint32_t date_nsec;
222 uint64_t vm_clock_nsec;
223
224 uint64_t icount;
225
226 uint32_t extra_data_size;
227
228 void *unknown_extra_data;
229} QCowSnapshot;
230
231struct Qcow2Cache;
232typedef struct Qcow2Cache Qcow2Cache;
233
234typedef struct Qcow2CryptoHeaderExtension {
235 uint64_t offset;
236 uint64_t length;
237} QEMU_PACKED Qcow2CryptoHeaderExtension;
238
239typedef struct Qcow2UnknownHeaderExtension {
240 uint32_t magic;
241 uint32_t len;
242 QLIST_ENTRY(Qcow2UnknownHeaderExtension) next;
243 uint8_t data[];
244} Qcow2UnknownHeaderExtension;
245
246enum {
247 QCOW2_FEAT_TYPE_INCOMPATIBLE = 0,
248 QCOW2_FEAT_TYPE_COMPATIBLE = 1,
249 QCOW2_FEAT_TYPE_AUTOCLEAR = 2,
250};
251
252
253enum {
254 QCOW2_INCOMPAT_DIRTY_BITNR = 0,
255 QCOW2_INCOMPAT_CORRUPT_BITNR = 1,
256 QCOW2_INCOMPAT_DATA_FILE_BITNR = 2,
257 QCOW2_INCOMPAT_COMPRESSION_BITNR = 3,
258 QCOW2_INCOMPAT_EXTL2_BITNR = 4,
259 QCOW2_INCOMPAT_DIRTY = 1 << QCOW2_INCOMPAT_DIRTY_BITNR,
260 QCOW2_INCOMPAT_CORRUPT = 1 << QCOW2_INCOMPAT_CORRUPT_BITNR,
261 QCOW2_INCOMPAT_DATA_FILE = 1 << QCOW2_INCOMPAT_DATA_FILE_BITNR,
262 QCOW2_INCOMPAT_COMPRESSION = 1 << QCOW2_INCOMPAT_COMPRESSION_BITNR,
263 QCOW2_INCOMPAT_EXTL2 = 1 << QCOW2_INCOMPAT_EXTL2_BITNR,
264
265 QCOW2_INCOMPAT_MASK = QCOW2_INCOMPAT_DIRTY
266 | QCOW2_INCOMPAT_CORRUPT
267 | QCOW2_INCOMPAT_DATA_FILE
268 | QCOW2_INCOMPAT_COMPRESSION
269 | QCOW2_INCOMPAT_EXTL2,
270};
271
272
273enum {
274 QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR = 0,
275 QCOW2_COMPAT_LAZY_REFCOUNTS = 1 << QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR,
276
277 QCOW2_COMPAT_FEAT_MASK = QCOW2_COMPAT_LAZY_REFCOUNTS,
278};
279
280
281enum {
282 QCOW2_AUTOCLEAR_BITMAPS_BITNR = 0,
283 QCOW2_AUTOCLEAR_DATA_FILE_RAW_BITNR = 1,
284 QCOW2_AUTOCLEAR_BITMAPS = 1 << QCOW2_AUTOCLEAR_BITMAPS_BITNR,
285 QCOW2_AUTOCLEAR_DATA_FILE_RAW = 1 << QCOW2_AUTOCLEAR_DATA_FILE_RAW_BITNR,
286
287 QCOW2_AUTOCLEAR_MASK = QCOW2_AUTOCLEAR_BITMAPS
288 | QCOW2_AUTOCLEAR_DATA_FILE_RAW,
289};
290
291enum qcow2_discard_type {
292 QCOW2_DISCARD_NEVER = 0,
293 QCOW2_DISCARD_ALWAYS,
294 QCOW2_DISCARD_REQUEST,
295 QCOW2_DISCARD_SNAPSHOT,
296 QCOW2_DISCARD_OTHER,
297 QCOW2_DISCARD_MAX
298};
299
300typedef struct Qcow2Feature {
301 uint8_t type;
302 uint8_t bit;
303 char name[46];
304} QEMU_PACKED Qcow2Feature;
305
306typedef struct Qcow2DiscardRegion {
307 BlockDriverState *bs;
308 uint64_t offset;
309 uint64_t bytes;
310 QTAILQ_ENTRY(Qcow2DiscardRegion) next;
311} Qcow2DiscardRegion;
312
313typedef uint64_t Qcow2GetRefcountFunc(const void *refcount_array,
314 uint64_t index);
315typedef void Qcow2SetRefcountFunc(void *refcount_array,
316 uint64_t index, uint64_t value);
317
318typedef struct Qcow2BitmapHeaderExt {
319 uint32_t nb_bitmaps;
320 uint32_t reserved32;
321 uint64_t bitmap_directory_size;
322 uint64_t bitmap_directory_offset;
323} QEMU_PACKED Qcow2BitmapHeaderExt;
324
325#define QCOW2_MAX_THREADS 4
326
327typedef struct BDRVQcow2State {
328 int cluster_bits;
329 int cluster_size;
330 int l2_slice_size;
331 int subcluster_bits;
332 int subcluster_size;
333 int subclusters_per_cluster;
334 int l2_bits;
335 int l2_size;
336 int l1_size;
337 int l1_vm_state_index;
338 int refcount_block_bits;
339 int refcount_block_size;
340 int csize_shift;
341 int csize_mask;
342 uint64_t cluster_offset_mask;
343 uint64_t l1_table_offset;
344 uint64_t *l1_table;
345
346 Qcow2Cache *l2_table_cache;
347 Qcow2Cache *refcount_block_cache;
348 QEMUTimer *cache_clean_timer;
349 unsigned cache_clean_interval;
350
351 QLIST_HEAD(, QCowL2Meta) cluster_allocs;
352
353 uint64_t *refcount_table;
354 uint64_t refcount_table_offset;
355 uint32_t refcount_table_size;
356 uint32_t max_refcount_table_index;
357 uint64_t free_cluster_index;
358 uint64_t free_byte_offset;
359
360 CoMutex lock;
361
362 Qcow2CryptoHeaderExtension crypto_header;
363 QCryptoBlockOpenOptions *crypto_opts;
364 QCryptoBlock *crypto;
365 bool crypt_physical_offset;
366
367 uint32_t crypt_method_header;
368 uint64_t snapshots_offset;
369 int snapshots_size;
370 unsigned int nb_snapshots;
371 QCowSnapshot *snapshots;
372
373 uint32_t nb_bitmaps;
374 uint64_t bitmap_directory_size;
375 uint64_t bitmap_directory_offset;
376
377 int flags;
378 int qcow_version;
379 bool use_lazy_refcounts;
380 int refcount_order;
381 int refcount_bits;
382 uint64_t refcount_max;
383
384 Qcow2GetRefcountFunc *get_refcount;
385 Qcow2SetRefcountFunc *set_refcount;
386
387 bool discard_passthrough[QCOW2_DISCARD_MAX];
388
389 bool discard_no_unref;
390
391 int overlap_check;
392 bool signaled_corruption;
393
394 uint64_t incompatible_features;
395 uint64_t compatible_features;
396 uint64_t autoclear_features;
397
398 size_t unknown_header_fields_size;
399 void *unknown_header_fields;
400 QLIST_HEAD(, Qcow2UnknownHeaderExtension) unknown_header_ext;
401 QTAILQ_HEAD (, Qcow2DiscardRegion) discards;
402 bool cache_discards;
403
404
405
406
407 char *image_backing_file;
408 char *image_backing_format;
409 char *image_data_file;
410
411 CoQueue thread_task_queue;
412 int nb_threads;
413
414 BdrvChild *data_file;
415
416 bool metadata_preallocation_checked;
417 bool metadata_preallocation;
418
419
420
421
422
423
424 Qcow2CompressionType compression_type;
425} BDRVQcow2State;
426
427typedef struct Qcow2COWRegion {
428
429
430
431
432 unsigned offset;
433
434
435 unsigned nb_bytes;
436} Qcow2COWRegion;
437
438
439
440
441
442
443typedef struct QCowL2Meta
444{
445
446 uint64_t offset;
447
448
449 uint64_t alloc_offset;
450
451
452 int nb_clusters;
453
454
455 bool keep_old_clusters;
456
457
458
459
460
461 CoQueue dependent_requests;
462
463
464
465
466
467
468 Qcow2COWRegion cow_start;
469
470
471
472
473
474
475 Qcow2COWRegion cow_end;
476
477
478
479
480
481 bool skip_cow;
482
483
484
485
486
487
488
489 bool prealloc;
490
491
492
493
494
495
496 QEMUIOVector *data_qiov;
497 size_t data_qiov_offset;
498
499
500 struct QCowL2Meta *next;
501
502 QLIST_ENTRY(QCowL2Meta) next_in_flight;
503} QCowL2Meta;
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532typedef enum QCow2ClusterType {
533 QCOW2_CLUSTER_UNALLOCATED,
534 QCOW2_CLUSTER_ZERO_PLAIN,
535 QCOW2_CLUSTER_ZERO_ALLOC,
536 QCOW2_CLUSTER_NORMAL,
537 QCOW2_CLUSTER_COMPRESSED,
538} QCow2ClusterType;
539
540typedef enum QCow2SubclusterType {
541 QCOW2_SUBCLUSTER_UNALLOCATED_PLAIN,
542 QCOW2_SUBCLUSTER_UNALLOCATED_ALLOC,
543 QCOW2_SUBCLUSTER_ZERO_PLAIN,
544 QCOW2_SUBCLUSTER_ZERO_ALLOC,
545 QCOW2_SUBCLUSTER_NORMAL,
546 QCOW2_SUBCLUSTER_COMPRESSED,
547 QCOW2_SUBCLUSTER_INVALID,
548} QCow2SubclusterType;
549
550typedef enum QCow2MetadataOverlap {
551 QCOW2_OL_MAIN_HEADER_BITNR = 0,
552 QCOW2_OL_ACTIVE_L1_BITNR = 1,
553 QCOW2_OL_ACTIVE_L2_BITNR = 2,
554 QCOW2_OL_REFCOUNT_TABLE_BITNR = 3,
555 QCOW2_OL_REFCOUNT_BLOCK_BITNR = 4,
556 QCOW2_OL_SNAPSHOT_TABLE_BITNR = 5,
557 QCOW2_OL_INACTIVE_L1_BITNR = 6,
558 QCOW2_OL_INACTIVE_L2_BITNR = 7,
559 QCOW2_OL_BITMAP_DIRECTORY_BITNR = 8,
560
561 QCOW2_OL_MAX_BITNR = 9,
562
563 QCOW2_OL_NONE = 0,
564 QCOW2_OL_MAIN_HEADER = (1 << QCOW2_OL_MAIN_HEADER_BITNR),
565 QCOW2_OL_ACTIVE_L1 = (1 << QCOW2_OL_ACTIVE_L1_BITNR),
566 QCOW2_OL_ACTIVE_L2 = (1 << QCOW2_OL_ACTIVE_L2_BITNR),
567 QCOW2_OL_REFCOUNT_TABLE = (1 << QCOW2_OL_REFCOUNT_TABLE_BITNR),
568 QCOW2_OL_REFCOUNT_BLOCK = (1 << QCOW2_OL_REFCOUNT_BLOCK_BITNR),
569 QCOW2_OL_SNAPSHOT_TABLE = (1 << QCOW2_OL_SNAPSHOT_TABLE_BITNR),
570 QCOW2_OL_INACTIVE_L1 = (1 << QCOW2_OL_INACTIVE_L1_BITNR),
571
572
573 QCOW2_OL_INACTIVE_L2 = (1 << QCOW2_OL_INACTIVE_L2_BITNR),
574 QCOW2_OL_BITMAP_DIRECTORY = (1 << QCOW2_OL_BITMAP_DIRECTORY_BITNR),
575} QCow2MetadataOverlap;
576
577
578#define QCOW2_OL_CONSTANT \
579 (QCOW2_OL_MAIN_HEADER | QCOW2_OL_ACTIVE_L1 | QCOW2_OL_REFCOUNT_TABLE | \
580 QCOW2_OL_SNAPSHOT_TABLE | QCOW2_OL_BITMAP_DIRECTORY)
581
582
583#define QCOW2_OL_CACHED \
584 (QCOW2_OL_CONSTANT | QCOW2_OL_ACTIVE_L2 | QCOW2_OL_REFCOUNT_BLOCK | \
585 QCOW2_OL_INACTIVE_L1)
586
587
588#define QCOW2_OL_ALL \
589 (QCOW2_OL_CACHED | QCOW2_OL_INACTIVE_L2)
590
591#define L1E_OFFSET_MASK 0x00fffffffffffe00ULL
592#define L1E_RESERVED_MASK 0x7f000000000001ffULL
593#define L2E_OFFSET_MASK 0x00fffffffffffe00ULL
594#define L2E_STD_RESERVED_MASK 0x3f000000000001feULL
595
596#define REFT_OFFSET_MASK 0xfffffffffffffe00ULL
597#define REFT_RESERVED_MASK 0x1ffULL
598
599#define INV_OFFSET (-1ULL)
600
601static inline bool has_subclusters(BDRVQcow2State *s)
602{
603 return s->incompatible_features & QCOW2_INCOMPAT_EXTL2;
604}
605
606static inline size_t l2_entry_size(BDRVQcow2State *s)
607{
608 return has_subclusters(s) ? L2E_SIZE_EXTENDED : L2E_SIZE_NORMAL;
609}
610
611static inline uint64_t get_l2_entry(BDRVQcow2State *s, uint64_t *l2_slice,
612 int idx)
613{
614 idx *= l2_entry_size(s) / sizeof(uint64_t);
615 return be64_to_cpu(l2_slice[idx]);
616}
617
618static inline uint64_t get_l2_bitmap(BDRVQcow2State *s, uint64_t *l2_slice,
619 int idx)
620{
621 if (has_subclusters(s)) {
622 idx *= l2_entry_size(s) / sizeof(uint64_t);
623 return be64_to_cpu(l2_slice[idx + 1]);
624 } else {
625 return 0;
626 }
627}
628
629static inline void set_l2_entry(BDRVQcow2State *s, uint64_t *l2_slice,
630 int idx, uint64_t entry)
631{
632 idx *= l2_entry_size(s) / sizeof(uint64_t);
633 l2_slice[idx] = cpu_to_be64(entry);
634}
635
636static inline void set_l2_bitmap(BDRVQcow2State *s, uint64_t *l2_slice,
637 int idx, uint64_t bitmap)
638{
639 assert(has_subclusters(s));
640 idx *= l2_entry_size(s) / sizeof(uint64_t);
641 l2_slice[idx + 1] = cpu_to_be64(bitmap);
642}
643
644static inline bool has_data_file(BlockDriverState *bs)
645{
646 BDRVQcow2State *s = bs->opaque;
647 return (s->data_file != bs->file);
648}
649
650static inline bool data_file_is_raw(BlockDriverState *bs)
651{
652 BDRVQcow2State *s = bs->opaque;
653 return !!(s->autoclear_features & QCOW2_AUTOCLEAR_DATA_FILE_RAW);
654}
655
656static inline int64_t start_of_cluster(BDRVQcow2State *s, int64_t offset)
657{
658 return offset & ~(s->cluster_size - 1);
659}
660
661static inline int64_t offset_into_cluster(BDRVQcow2State *s, int64_t offset)
662{
663 return offset & (s->cluster_size - 1);
664}
665
666static inline int64_t offset_into_subcluster(BDRVQcow2State *s, int64_t offset)
667{
668 return offset & (s->subcluster_size - 1);
669}
670
671static inline uint64_t size_to_clusters(BDRVQcow2State *s, uint64_t size)
672{
673 return (size + (s->cluster_size - 1)) >> s->cluster_bits;
674}
675
676static inline uint64_t size_to_subclusters(BDRVQcow2State *s, uint64_t size)
677{
678 return (size + (s->subcluster_size - 1)) >> s->subcluster_bits;
679}
680
681static inline int64_t size_to_l1(BDRVQcow2State *s, int64_t size)
682{
683 int shift = s->cluster_bits + s->l2_bits;
684 return (size + (1ULL << shift) - 1) >> shift;
685}
686
687static inline int offset_to_l1_index(BDRVQcow2State *s, uint64_t offset)
688{
689 return offset >> (s->l2_bits + s->cluster_bits);
690}
691
692static inline int offset_to_l2_index(BDRVQcow2State *s, int64_t offset)
693{
694 return (offset >> s->cluster_bits) & (s->l2_size - 1);
695}
696
697static inline int offset_to_l2_slice_index(BDRVQcow2State *s, int64_t offset)
698{
699 return (offset >> s->cluster_bits) & (s->l2_slice_size - 1);
700}
701
702static inline int offset_to_sc_index(BDRVQcow2State *s, int64_t offset)
703{
704 return (offset >> s->subcluster_bits) & (s->subclusters_per_cluster - 1);
705}
706
707static inline int64_t qcow2_vm_state_offset(BDRVQcow2State *s)
708{
709 return (int64_t)s->l1_vm_state_index << (s->cluster_bits + s->l2_bits);
710}
711
712static inline QCow2ClusterType qcow2_get_cluster_type(BlockDriverState *bs,
713 uint64_t l2_entry)
714{
715 BDRVQcow2State *s = bs->opaque;
716
717 if (l2_entry & QCOW_OFLAG_COMPRESSED) {
718 return QCOW2_CLUSTER_COMPRESSED;
719 } else if ((l2_entry & QCOW_OFLAG_ZERO) && !has_subclusters(s)) {
720 if (l2_entry & L2E_OFFSET_MASK) {
721 return QCOW2_CLUSTER_ZERO_ALLOC;
722 }
723 return QCOW2_CLUSTER_ZERO_PLAIN;
724 } else if (!(l2_entry & L2E_OFFSET_MASK)) {
725
726
727
728
729 if (has_data_file(bs) && (l2_entry & QCOW_OFLAG_COPIED)) {
730 return QCOW2_CLUSTER_NORMAL;
731 } else {
732 return QCOW2_CLUSTER_UNALLOCATED;
733 }
734 } else {
735 return QCOW2_CLUSTER_NORMAL;
736 }
737}
738
739
740
741
742
743
744
745
746static inline
747QCow2SubclusterType qcow2_get_subcluster_type(BlockDriverState *bs,
748 uint64_t l2_entry,
749 uint64_t l2_bitmap,
750 unsigned sc_index)
751{
752 BDRVQcow2State *s = bs->opaque;
753 QCow2ClusterType type = qcow2_get_cluster_type(bs, l2_entry);
754 assert(sc_index < s->subclusters_per_cluster);
755
756 if (has_subclusters(s)) {
757 switch (type) {
758 case QCOW2_CLUSTER_COMPRESSED:
759 return QCOW2_SUBCLUSTER_COMPRESSED;
760 case QCOW2_CLUSTER_NORMAL:
761 if ((l2_bitmap >> 32) & l2_bitmap) {
762 return QCOW2_SUBCLUSTER_INVALID;
763 } else if (l2_bitmap & QCOW_OFLAG_SUB_ZERO(sc_index)) {
764 return QCOW2_SUBCLUSTER_ZERO_ALLOC;
765 } else if (l2_bitmap & QCOW_OFLAG_SUB_ALLOC(sc_index)) {
766 return QCOW2_SUBCLUSTER_NORMAL;
767 } else {
768 return QCOW2_SUBCLUSTER_UNALLOCATED_ALLOC;
769 }
770 case QCOW2_CLUSTER_UNALLOCATED:
771 if (l2_bitmap & QCOW_L2_BITMAP_ALL_ALLOC) {
772 return QCOW2_SUBCLUSTER_INVALID;
773 } else if (l2_bitmap & QCOW_OFLAG_SUB_ZERO(sc_index)) {
774 return QCOW2_SUBCLUSTER_ZERO_PLAIN;
775 } else {
776 return QCOW2_SUBCLUSTER_UNALLOCATED_PLAIN;
777 }
778 default:
779 g_assert_not_reached();
780 }
781 } else {
782 switch (type) {
783 case QCOW2_CLUSTER_COMPRESSED:
784 return QCOW2_SUBCLUSTER_COMPRESSED;
785 case QCOW2_CLUSTER_ZERO_PLAIN:
786 return QCOW2_SUBCLUSTER_ZERO_PLAIN;
787 case QCOW2_CLUSTER_ZERO_ALLOC:
788 return QCOW2_SUBCLUSTER_ZERO_ALLOC;
789 case QCOW2_CLUSTER_NORMAL:
790 return QCOW2_SUBCLUSTER_NORMAL;
791 case QCOW2_CLUSTER_UNALLOCATED:
792 return QCOW2_SUBCLUSTER_UNALLOCATED_PLAIN;
793 default:
794 g_assert_not_reached();
795 }
796 }
797}
798
799static inline bool qcow2_cluster_is_allocated(QCow2ClusterType type)
800{
801 return (type == QCOW2_CLUSTER_COMPRESSED || type == QCOW2_CLUSTER_NORMAL ||
802 type == QCOW2_CLUSTER_ZERO_ALLOC);
803}
804
805
806static inline bool qcow2_need_accurate_refcounts(BDRVQcow2State *s)
807{
808 return !(s->incompatible_features & QCOW2_INCOMPAT_DIRTY);
809}
810
811static inline uint64_t l2meta_cow_start(QCowL2Meta *m)
812{
813 return m->offset + m->cow_start.offset;
814}
815
816static inline uint64_t l2meta_cow_end(QCowL2Meta *m)
817{
818 return m->offset + m->cow_end.offset + m->cow_end.nb_bytes;
819}
820
821static inline uint64_t refcount_diff(uint64_t r1, uint64_t r2)
822{
823 return r1 > r2 ? r1 - r2 : r2 - r1;
824}
825
826static inline
827uint32_t offset_to_reftable_index(BDRVQcow2State *s, uint64_t offset)
828{
829 return offset >> (s->refcount_block_bits + s->cluster_bits);
830}
831
832
833int64_t qcow2_refcount_metadata_size(int64_t clusters, size_t cluster_size,
834 int refcount_order, bool generous_increase,
835 uint64_t *refblock_count);
836
837int qcow2_mark_dirty(BlockDriverState *bs);
838int qcow2_mark_corrupt(BlockDriverState *bs);
839int qcow2_update_header(BlockDriverState *bs);
840
841void qcow2_signal_corruption(BlockDriverState *bs, bool fatal, int64_t offset,
842 int64_t size, const char *message_format, ...)
843 G_GNUC_PRINTF(5, 6);
844
845int qcow2_validate_table(BlockDriverState *bs, uint64_t offset,
846 uint64_t entries, size_t entry_len,
847 int64_t max_size_bytes, const char *table_name,
848 Error **errp);
849
850
851int coroutine_fn GRAPH_RDLOCK qcow2_refcount_init(BlockDriverState *bs);
852void qcow2_refcount_close(BlockDriverState *bs);
853
854int qcow2_get_refcount(BlockDriverState *bs, int64_t cluster_index,
855 uint64_t *refcount);
856
857int qcow2_update_cluster_refcount(BlockDriverState *bs, int64_t cluster_index,
858 uint64_t addend, bool decrease,
859 enum qcow2_discard_type type);
860
861int64_t qcow2_refcount_area(BlockDriverState *bs, uint64_t offset,
862 uint64_t additional_clusters, bool exact_size,
863 int new_refblock_index,
864 uint64_t new_refblock_offset);
865
866int64_t qcow2_alloc_clusters(BlockDriverState *bs, uint64_t size);
867int64_t coroutine_fn qcow2_alloc_clusters_at(BlockDriverState *bs, uint64_t offset,
868 int64_t nb_clusters);
869int64_t coroutine_fn GRAPH_RDLOCK qcow2_alloc_bytes(BlockDriverState *bs, int size);
870void qcow2_free_clusters(BlockDriverState *bs,
871 int64_t offset, int64_t size,
872 enum qcow2_discard_type type);
873void qcow2_free_any_cluster(BlockDriverState *bs, uint64_t l2_entry,
874 enum qcow2_discard_type type);
875
876int qcow2_update_snapshot_refcount(BlockDriverState *bs,
877 int64_t l1_table_offset, int l1_size, int addend);
878
879int qcow2_flush_caches(BlockDriverState *bs);
880int qcow2_write_caches(BlockDriverState *bs);
881int coroutine_fn qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
882 BdrvCheckMode fix);
883
884void qcow2_process_discards(BlockDriverState *bs, int ret);
885
886int qcow2_check_metadata_overlap(BlockDriverState *bs, int ign, int64_t offset,
887 int64_t size);
888int qcow2_pre_write_overlap_check(BlockDriverState *bs, int ign, int64_t offset,
889 int64_t size, bool data_file);
890int coroutine_fn qcow2_inc_refcounts_imrt(BlockDriverState *bs, BdrvCheckResult *res,
891 void **refcount_table,
892 int64_t *refcount_table_size,
893 int64_t offset, int64_t size);
894
895int qcow2_change_refcount_order(BlockDriverState *bs, int refcount_order,
896 BlockDriverAmendStatusCB *status_cb,
897 void *cb_opaque, Error **errp);
898int coroutine_fn GRAPH_RDLOCK qcow2_shrink_reftable(BlockDriverState *bs);
899int64_t coroutine_fn qcow2_get_last_cluster(BlockDriverState *bs, int64_t size);
900
901int coroutine_fn GRAPH_RDLOCK
902qcow2_detect_metadata_preallocation(BlockDriverState *bs);
903
904
905int qcow2_grow_l1_table(BlockDriverState *bs, uint64_t min_size,
906 bool exact_size);
907
908int coroutine_fn GRAPH_RDLOCK
909qcow2_shrink_l1_table(BlockDriverState *bs, uint64_t max_size);
910
911int qcow2_write_l1_entry(BlockDriverState *bs, int l1_index);
912int qcow2_encrypt_sectors(BDRVQcow2State *s, int64_t sector_num,
913 uint8_t *buf, int nb_sectors, bool enc, Error **errp);
914
915int qcow2_get_host_offset(BlockDriverState *bs, uint64_t offset,
916 unsigned int *bytes, uint64_t *host_offset,
917 QCow2SubclusterType *subcluster_type);
918int coroutine_fn qcow2_alloc_host_offset(BlockDriverState *bs, uint64_t offset,
919 unsigned int *bytes,
920 uint64_t *host_offset, QCowL2Meta **m);
921int coroutine_fn GRAPH_RDLOCK
922qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs, uint64_t offset,
923 int compressed_size, uint64_t *host_offset);
924void qcow2_parse_compressed_l2_entry(BlockDriverState *bs, uint64_t l2_entry,
925 uint64_t *coffset, int *csize);
926
927int coroutine_fn GRAPH_RDLOCK
928qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m);
929
930void coroutine_fn qcow2_alloc_cluster_abort(BlockDriverState *bs, QCowL2Meta *m);
931int qcow2_cluster_discard(BlockDriverState *bs, uint64_t offset,
932 uint64_t bytes, enum qcow2_discard_type type,
933 bool full_discard);
934
935int coroutine_fn GRAPH_RDLOCK
936qcow2_subcluster_zeroize(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
937 int flags);
938
939int qcow2_expand_zero_clusters(BlockDriverState *bs,
940 BlockDriverAmendStatusCB *status_cb,
941 void *cb_opaque);
942
943
944int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info);
945int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id);
946int qcow2_snapshot_delete(BlockDriverState *bs,
947 const char *snapshot_id,
948 const char *name,
949 Error **errp);
950int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab);
951int qcow2_snapshot_load_tmp(BlockDriverState *bs,
952 const char *snapshot_id,
953 const char *name,
954 Error **errp);
955
956void qcow2_free_snapshots(BlockDriverState *bs);
957int coroutine_fn GRAPH_RDLOCK
958qcow2_read_snapshots(BlockDriverState *bs, Error **errp);
959int qcow2_write_snapshots(BlockDriverState *bs);
960
961int coroutine_fn GRAPH_RDLOCK
962qcow2_check_read_snapshot_table(BlockDriverState *bs, BdrvCheckResult *result,
963 BdrvCheckMode fix);
964
965int coroutine_fn qcow2_check_fix_snapshot_table(BlockDriverState *bs,
966 BdrvCheckResult *result,
967 BdrvCheckMode fix);
968
969
970Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables,
971 unsigned table_size);
972int qcow2_cache_destroy(Qcow2Cache *c);
973
974void qcow2_cache_entry_mark_dirty(Qcow2Cache *c, void *table);
975int qcow2_cache_flush(BlockDriverState *bs, Qcow2Cache *c);
976int qcow2_cache_write(BlockDriverState *bs, Qcow2Cache *c);
977int qcow2_cache_set_dependency(BlockDriverState *bs, Qcow2Cache *c,
978 Qcow2Cache *dependency);
979void qcow2_cache_depends_on_flush(Qcow2Cache *c);
980
981void qcow2_cache_clean_unused(Qcow2Cache *c);
982int qcow2_cache_empty(BlockDriverState *bs, Qcow2Cache *c);
983
984int qcow2_cache_get(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
985 void **table);
986int qcow2_cache_get_empty(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
987 void **table);
988void qcow2_cache_put(Qcow2Cache *c, void **table);
989void *qcow2_cache_is_table_offset(Qcow2Cache *c, uint64_t offset);
990void qcow2_cache_discard(Qcow2Cache *c, void *table);
991
992
993int coroutine_fn
994qcow2_check_bitmaps_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
995 void **refcount_table,
996 int64_t *refcount_table_size);
997bool coroutine_fn GRAPH_RDLOCK
998qcow2_load_dirty_bitmaps(BlockDriverState *bs, bool *header_updated, Error **errp);
999bool qcow2_get_bitmap_info_list(BlockDriverState *bs,
1000 Qcow2BitmapInfoList **info_list, Error **errp);
1001int qcow2_reopen_bitmaps_rw(BlockDriverState *bs, Error **errp);
1002int coroutine_fn qcow2_truncate_bitmaps_check(BlockDriverState *bs, Error **errp);
1003bool qcow2_store_persistent_dirty_bitmaps(BlockDriverState *bs,
1004 bool release_stored, Error **errp);
1005int qcow2_reopen_bitmaps_ro(BlockDriverState *bs, Error **errp);
1006bool coroutine_fn qcow2_co_can_store_new_dirty_bitmap(BlockDriverState *bs,
1007 const char *name,
1008 uint32_t granularity,
1009 Error **errp);
1010int coroutine_fn qcow2_co_remove_persistent_dirty_bitmap(BlockDriverState *bs,
1011 const char *name,
1012 Error **errp);
1013bool qcow2_supports_persistent_dirty_bitmap(BlockDriverState *bs);
1014uint64_t qcow2_get_persistent_dirty_bitmap_size(BlockDriverState *bs,
1015 uint32_t cluster_size);
1016
1017ssize_t coroutine_fn
1018qcow2_co_compress(BlockDriverState *bs, void *dest, size_t dest_size,
1019 const void *src, size_t src_size);
1020ssize_t coroutine_fn
1021qcow2_co_decompress(BlockDriverState *bs, void *dest, size_t dest_size,
1022 const void *src, size_t src_size);
1023int coroutine_fn
1024qcow2_co_encrypt(BlockDriverState *bs, uint64_t host_offset,
1025 uint64_t guest_offset, void *buf, size_t len);
1026int coroutine_fn
1027qcow2_co_decrypt(BlockDriverState *bs, uint64_t host_offset,
1028 uint64_t guest_offset, void *buf, size_t len);
1029
1030#endif
1031