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/cipher.h"
29#include "qemu/coroutine.h"
30
31
32
33
34
35#define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
36
37#define QCOW_CRYPT_NONE 0
38#define QCOW_CRYPT_AES 1
39
40#define QCOW_MAX_CRYPT_CLUSTERS 32
41#define QCOW_MAX_SNAPSHOTS 65536
42
43
44
45#define QCOW_MAX_REFTABLE_SIZE 0x800000
46
47
48
49#define QCOW_MAX_L1_SIZE 0x2000000
50
51
52
53#define QCOW_MAX_SNAPSHOTS_SIZE (1024 * QCOW_MAX_SNAPSHOTS)
54
55
56#define QCOW_OFLAG_COPIED (1ULL << 63)
57
58#define QCOW_OFLAG_COMPRESSED (1ULL << 62)
59
60#define QCOW_OFLAG_ZERO (1ULL << 0)
61
62#define MIN_CLUSTER_BITS 9
63#define MAX_CLUSTER_BITS 21
64
65
66#define MIN_L2_CACHE_SIZE 2
67
68
69#define MIN_REFCOUNT_CACHE_SIZE 4
70
71
72#define DEFAULT_L2_CACHE_CLUSTERS 8
73#define DEFAULT_L2_CACHE_BYTE_SIZE 1048576
74
75
76
77#define DEFAULT_L2_REFCOUNT_SIZE_RATIO 4
78
79#define DEFAULT_CLUSTER_SIZE 65536
80
81
82#define QCOW2_OPT_LAZY_REFCOUNTS "lazy-refcounts"
83#define QCOW2_OPT_DISCARD_REQUEST "pass-discard-request"
84#define QCOW2_OPT_DISCARD_SNAPSHOT "pass-discard-snapshot"
85#define QCOW2_OPT_DISCARD_OTHER "pass-discard-other"
86#define QCOW2_OPT_OVERLAP "overlap-check"
87#define QCOW2_OPT_OVERLAP_TEMPLATE "overlap-check.template"
88#define QCOW2_OPT_OVERLAP_MAIN_HEADER "overlap-check.main-header"
89#define QCOW2_OPT_OVERLAP_ACTIVE_L1 "overlap-check.active-l1"
90#define QCOW2_OPT_OVERLAP_ACTIVE_L2 "overlap-check.active-l2"
91#define QCOW2_OPT_OVERLAP_REFCOUNT_TABLE "overlap-check.refcount-table"
92#define QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK "overlap-check.refcount-block"
93#define QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE "overlap-check.snapshot-table"
94#define QCOW2_OPT_OVERLAP_INACTIVE_L1 "overlap-check.inactive-l1"
95#define QCOW2_OPT_OVERLAP_INACTIVE_L2 "overlap-check.inactive-l2"
96#define QCOW2_OPT_CACHE_SIZE "cache-size"
97#define QCOW2_OPT_L2_CACHE_SIZE "l2-cache-size"
98#define QCOW2_OPT_REFCOUNT_CACHE_SIZE "refcount-cache-size"
99#define QCOW2_OPT_CACHE_CLEAN_INTERVAL "cache-clean-interval"
100
101typedef struct QCowHeader {
102 uint32_t magic;
103 uint32_t version;
104 uint64_t backing_file_offset;
105 uint32_t backing_file_size;
106 uint32_t cluster_bits;
107 uint64_t size;
108 uint32_t crypt_method;
109 uint32_t l1_size;
110 uint64_t l1_table_offset;
111 uint64_t refcount_table_offset;
112 uint32_t refcount_table_clusters;
113 uint32_t nb_snapshots;
114 uint64_t snapshots_offset;
115
116
117 uint64_t incompatible_features;
118 uint64_t compatible_features;
119 uint64_t autoclear_features;
120
121 uint32_t refcount_order;
122 uint32_t header_length;
123} QEMU_PACKED QCowHeader;
124
125typedef struct QEMU_PACKED QCowSnapshotHeader {
126
127 uint64_t l1_table_offset;
128
129 uint32_t l1_size;
130 uint16_t id_str_size;
131 uint16_t name_size;
132
133 uint32_t date_sec;
134 uint32_t date_nsec;
135
136 uint64_t vm_clock_nsec;
137
138 uint32_t vm_state_size;
139 uint32_t extra_data_size;
140
141
142
143} QCowSnapshotHeader;
144
145typedef struct QEMU_PACKED QCowSnapshotExtraData {
146 uint64_t vm_state_size_large;
147 uint64_t disk_size;
148} QCowSnapshotExtraData;
149
150
151typedef struct QCowSnapshot {
152 uint64_t l1_table_offset;
153 uint32_t l1_size;
154 char *id_str;
155 char *name;
156 uint64_t disk_size;
157 uint64_t vm_state_size;
158 uint32_t date_sec;
159 uint32_t date_nsec;
160 uint64_t vm_clock_nsec;
161} QCowSnapshot;
162
163struct Qcow2Cache;
164typedef struct Qcow2Cache Qcow2Cache;
165
166typedef struct Qcow2UnknownHeaderExtension {
167 uint32_t magic;
168 uint32_t len;
169 QLIST_ENTRY(Qcow2UnknownHeaderExtension) next;
170 uint8_t data[];
171} Qcow2UnknownHeaderExtension;
172
173enum {
174 QCOW2_FEAT_TYPE_INCOMPATIBLE = 0,
175 QCOW2_FEAT_TYPE_COMPATIBLE = 1,
176 QCOW2_FEAT_TYPE_AUTOCLEAR = 2,
177};
178
179
180enum {
181 QCOW2_INCOMPAT_DIRTY_BITNR = 0,
182 QCOW2_INCOMPAT_CORRUPT_BITNR = 1,
183 QCOW2_INCOMPAT_DIRTY = 1 << QCOW2_INCOMPAT_DIRTY_BITNR,
184 QCOW2_INCOMPAT_CORRUPT = 1 << QCOW2_INCOMPAT_CORRUPT_BITNR,
185
186 QCOW2_INCOMPAT_MASK = QCOW2_INCOMPAT_DIRTY
187 | QCOW2_INCOMPAT_CORRUPT,
188};
189
190
191enum {
192 QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR = 0,
193 QCOW2_COMPAT_LAZY_REFCOUNTS = 1 << QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR,
194
195 QCOW2_COMPAT_FEAT_MASK = QCOW2_COMPAT_LAZY_REFCOUNTS,
196};
197
198enum qcow2_discard_type {
199 QCOW2_DISCARD_NEVER = 0,
200 QCOW2_DISCARD_ALWAYS,
201 QCOW2_DISCARD_REQUEST,
202 QCOW2_DISCARD_SNAPSHOT,
203 QCOW2_DISCARD_OTHER,
204 QCOW2_DISCARD_MAX
205};
206
207typedef struct Qcow2Feature {
208 uint8_t type;
209 uint8_t bit;
210 char name[46];
211} QEMU_PACKED Qcow2Feature;
212
213typedef struct Qcow2DiscardRegion {
214 BlockDriverState *bs;
215 uint64_t offset;
216 uint64_t bytes;
217 QTAILQ_ENTRY(Qcow2DiscardRegion) next;
218} Qcow2DiscardRegion;
219
220typedef uint64_t Qcow2GetRefcountFunc(const void *refcount_array,
221 uint64_t index);
222typedef void Qcow2SetRefcountFunc(void *refcount_array,
223 uint64_t index, uint64_t value);
224
225typedef struct BDRVQcow2State {
226 int cluster_bits;
227 int cluster_size;
228 int cluster_sectors;
229 int l2_bits;
230 int l2_size;
231 int l1_size;
232 int l1_vm_state_index;
233 int refcount_block_bits;
234 int refcount_block_size;
235 int csize_shift;
236 int csize_mask;
237 uint64_t cluster_offset_mask;
238 uint64_t l1_table_offset;
239 uint64_t *l1_table;
240
241 Qcow2Cache* l2_table_cache;
242 Qcow2Cache* refcount_block_cache;
243 QEMUTimer *cache_clean_timer;
244 unsigned cache_clean_interval;
245
246 uint8_t *cluster_cache;
247 uint8_t *cluster_data;
248 uint64_t cluster_cache_offset;
249 QLIST_HEAD(QCowClusterAlloc, QCowL2Meta) cluster_allocs;
250
251 uint64_t *refcount_table;
252 uint64_t refcount_table_offset;
253 uint32_t refcount_table_size;
254 uint64_t free_cluster_index;
255 uint64_t free_byte_offset;
256
257 CoMutex lock;
258
259 QCryptoCipher *cipher;
260 uint32_t crypt_method_header;
261 uint64_t snapshots_offset;
262 int snapshots_size;
263 unsigned int nb_snapshots;
264 QCowSnapshot *snapshots;
265
266 int flags;
267 int qcow_version;
268 bool use_lazy_refcounts;
269 int refcount_order;
270 int refcount_bits;
271 uint64_t refcount_max;
272
273 Qcow2GetRefcountFunc *get_refcount;
274 Qcow2SetRefcountFunc *set_refcount;
275
276 bool discard_passthrough[QCOW2_DISCARD_MAX];
277
278 int overlap_check;
279 bool signaled_corruption;
280
281 uint64_t incompatible_features;
282 uint64_t compatible_features;
283 uint64_t autoclear_features;
284
285 size_t unknown_header_fields_size;
286 void* unknown_header_fields;
287 QLIST_HEAD(, Qcow2UnknownHeaderExtension) unknown_header_ext;
288 QTAILQ_HEAD (, Qcow2DiscardRegion) discards;
289 bool cache_discards;
290
291
292
293
294 char *image_backing_file;
295 char *image_backing_format;
296} BDRVQcow2State;
297
298typedef struct Qcow2COWRegion {
299
300
301
302
303 uint64_t offset;
304
305
306 int nb_sectors;
307} Qcow2COWRegion;
308
309
310
311
312
313typedef struct QCowL2Meta
314{
315
316 uint64_t offset;
317
318
319 uint64_t alloc_offset;
320
321
322
323
324
325 int nb_available;
326
327
328 int nb_clusters;
329
330
331
332
333
334 CoQueue dependent_requests;
335
336
337
338
339
340 Qcow2COWRegion cow_start;
341
342
343
344
345
346 Qcow2COWRegion cow_end;
347
348
349 struct QCowL2Meta *next;
350
351 QLIST_ENTRY(QCowL2Meta) next_in_flight;
352} QCowL2Meta;
353
354enum {
355 QCOW2_CLUSTER_UNALLOCATED,
356 QCOW2_CLUSTER_NORMAL,
357 QCOW2_CLUSTER_COMPRESSED,
358 QCOW2_CLUSTER_ZERO
359};
360
361typedef enum QCow2MetadataOverlap {
362 QCOW2_OL_MAIN_HEADER_BITNR = 0,
363 QCOW2_OL_ACTIVE_L1_BITNR = 1,
364 QCOW2_OL_ACTIVE_L2_BITNR = 2,
365 QCOW2_OL_REFCOUNT_TABLE_BITNR = 3,
366 QCOW2_OL_REFCOUNT_BLOCK_BITNR = 4,
367 QCOW2_OL_SNAPSHOT_TABLE_BITNR = 5,
368 QCOW2_OL_INACTIVE_L1_BITNR = 6,
369 QCOW2_OL_INACTIVE_L2_BITNR = 7,
370
371 QCOW2_OL_MAX_BITNR = 8,
372
373 QCOW2_OL_NONE = 0,
374 QCOW2_OL_MAIN_HEADER = (1 << QCOW2_OL_MAIN_HEADER_BITNR),
375 QCOW2_OL_ACTIVE_L1 = (1 << QCOW2_OL_ACTIVE_L1_BITNR),
376 QCOW2_OL_ACTIVE_L2 = (1 << QCOW2_OL_ACTIVE_L2_BITNR),
377 QCOW2_OL_REFCOUNT_TABLE = (1 << QCOW2_OL_REFCOUNT_TABLE_BITNR),
378 QCOW2_OL_REFCOUNT_BLOCK = (1 << QCOW2_OL_REFCOUNT_BLOCK_BITNR),
379 QCOW2_OL_SNAPSHOT_TABLE = (1 << QCOW2_OL_SNAPSHOT_TABLE_BITNR),
380 QCOW2_OL_INACTIVE_L1 = (1 << QCOW2_OL_INACTIVE_L1_BITNR),
381
382
383 QCOW2_OL_INACTIVE_L2 = (1 << QCOW2_OL_INACTIVE_L2_BITNR),
384} QCow2MetadataOverlap;
385
386
387#define QCOW2_OL_CONSTANT \
388 (QCOW2_OL_MAIN_HEADER | QCOW2_OL_ACTIVE_L1 | QCOW2_OL_REFCOUNT_TABLE | \
389 QCOW2_OL_SNAPSHOT_TABLE)
390
391
392#define QCOW2_OL_CACHED \
393 (QCOW2_OL_CONSTANT | QCOW2_OL_ACTIVE_L2 | QCOW2_OL_REFCOUNT_BLOCK | \
394 QCOW2_OL_INACTIVE_L1)
395
396
397#define QCOW2_OL_ALL \
398 (QCOW2_OL_CACHED | QCOW2_OL_INACTIVE_L2)
399
400#define L1E_OFFSET_MASK 0x00fffffffffffe00ULL
401#define L2E_OFFSET_MASK 0x00fffffffffffe00ULL
402#define L2E_COMPRESSED_OFFSET_SIZE_MASK 0x3fffffffffffffffULL
403
404#define REFT_OFFSET_MASK 0xfffffffffffffe00ULL
405
406static inline int64_t start_of_cluster(BDRVQcow2State *s, int64_t offset)
407{
408 return offset & ~(s->cluster_size - 1);
409}
410
411static inline int64_t offset_into_cluster(BDRVQcow2State *s, int64_t offset)
412{
413 return offset & (s->cluster_size - 1);
414}
415
416static inline uint64_t size_to_clusters(BDRVQcow2State *s, uint64_t size)
417{
418 return (size + (s->cluster_size - 1)) >> s->cluster_bits;
419}
420
421static inline int64_t size_to_l1(BDRVQcow2State *s, int64_t size)
422{
423 int shift = s->cluster_bits + s->l2_bits;
424 return (size + (1ULL << shift) - 1) >> shift;
425}
426
427static inline int offset_to_l2_index(BDRVQcow2State *s, int64_t offset)
428{
429 return (offset >> s->cluster_bits) & (s->l2_size - 1);
430}
431
432static inline int64_t align_offset(int64_t offset, int n)
433{
434 offset = (offset + n - 1) & ~(n - 1);
435 return offset;
436}
437
438static inline int64_t qcow2_vm_state_offset(BDRVQcow2State *s)
439{
440 return (int64_t)s->l1_vm_state_index << (s->cluster_bits + s->l2_bits);
441}
442
443static inline uint64_t qcow2_max_refcount_clusters(BDRVQcow2State *s)
444{
445 return QCOW_MAX_REFTABLE_SIZE >> s->cluster_bits;
446}
447
448static inline int qcow2_get_cluster_type(uint64_t l2_entry)
449{
450 if (l2_entry & QCOW_OFLAG_COMPRESSED) {
451 return QCOW2_CLUSTER_COMPRESSED;
452 } else if (l2_entry & QCOW_OFLAG_ZERO) {
453 return QCOW2_CLUSTER_ZERO;
454 } else if (!(l2_entry & L2E_OFFSET_MASK)) {
455 return QCOW2_CLUSTER_UNALLOCATED;
456 } else {
457 return QCOW2_CLUSTER_NORMAL;
458 }
459}
460
461
462static inline bool qcow2_need_accurate_refcounts(BDRVQcow2State *s)
463{
464 return !(s->incompatible_features & QCOW2_INCOMPAT_DIRTY);
465}
466
467static inline uint64_t l2meta_cow_start(QCowL2Meta *m)
468{
469 return m->offset + m->cow_start.offset;
470}
471
472static inline uint64_t l2meta_cow_end(QCowL2Meta *m)
473{
474 return m->offset + m->cow_end.offset
475 + (m->cow_end.nb_sectors << BDRV_SECTOR_BITS);
476}
477
478static inline uint64_t refcount_diff(uint64_t r1, uint64_t r2)
479{
480 return r1 > r2 ? r1 - r2 : r2 - r1;
481}
482
483
484
485
486int qcow2_backing_read1(BlockDriverState *bs, QEMUIOVector *qiov,
487 int64_t sector_num, int nb_sectors);
488
489int qcow2_mark_dirty(BlockDriverState *bs);
490int qcow2_mark_corrupt(BlockDriverState *bs);
491int qcow2_mark_consistent(BlockDriverState *bs);
492int qcow2_update_header(BlockDriverState *bs);
493
494void qcow2_signal_corruption(BlockDriverState *bs, bool fatal, int64_t offset,
495 int64_t size, const char *message_format, ...)
496 GCC_FMT_ATTR(5, 6);
497
498
499int qcow2_refcount_init(BlockDriverState *bs);
500void qcow2_refcount_close(BlockDriverState *bs);
501
502int qcow2_get_refcount(BlockDriverState *bs, int64_t cluster_index,
503 uint64_t *refcount);
504
505int qcow2_update_cluster_refcount(BlockDriverState *bs, int64_t cluster_index,
506 uint64_t addend, bool decrease,
507 enum qcow2_discard_type type);
508
509int64_t qcow2_alloc_clusters(BlockDriverState *bs, uint64_t size);
510int64_t qcow2_alloc_clusters_at(BlockDriverState *bs, uint64_t offset,
511 int64_t nb_clusters);
512int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size);
513void qcow2_free_clusters(BlockDriverState *bs,
514 int64_t offset, int64_t size,
515 enum qcow2_discard_type type);
516void qcow2_free_any_clusters(BlockDriverState *bs, uint64_t l2_entry,
517 int nb_clusters, enum qcow2_discard_type type);
518
519int qcow2_update_snapshot_refcount(BlockDriverState *bs,
520 int64_t l1_table_offset, int l1_size, int addend);
521
522int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
523 BdrvCheckMode fix);
524
525void qcow2_process_discards(BlockDriverState *bs, int ret);
526
527int qcow2_check_metadata_overlap(BlockDriverState *bs, int ign, int64_t offset,
528 int64_t size);
529int qcow2_pre_write_overlap_check(BlockDriverState *bs, int ign, int64_t offset,
530 int64_t size);
531
532int qcow2_change_refcount_order(BlockDriverState *bs, int refcount_order,
533 BlockDriverAmendStatusCB *status_cb,
534 void *cb_opaque, Error **errp);
535
536
537int qcow2_grow_l1_table(BlockDriverState *bs, uint64_t min_size,
538 bool exact_size);
539int qcow2_write_l1_entry(BlockDriverState *bs, int l1_index);
540void qcow2_l2_cache_reset(BlockDriverState *bs);
541int qcow2_decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset);
542int qcow2_encrypt_sectors(BDRVQcow2State *s, int64_t sector_num,
543 uint8_t *out_buf, const uint8_t *in_buf,
544 int nb_sectors, bool enc, Error **errp);
545
546int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
547 int *num, uint64_t *cluster_offset);
548int qcow2_alloc_cluster_offset(BlockDriverState *bs, uint64_t offset,
549 int *num, uint64_t *host_offset, QCowL2Meta **m);
550uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
551 uint64_t offset,
552 int compressed_size);
553
554int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m);
555int qcow2_discard_clusters(BlockDriverState *bs, uint64_t offset,
556 int nb_sectors, enum qcow2_discard_type type, bool full_discard);
557int qcow2_zero_clusters(BlockDriverState *bs, uint64_t offset, int nb_sectors);
558
559int qcow2_expand_zero_clusters(BlockDriverState *bs,
560 BlockDriverAmendStatusCB *status_cb,
561 void *cb_opaque);
562
563
564int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info);
565int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id);
566int qcow2_snapshot_delete(BlockDriverState *bs,
567 const char *snapshot_id,
568 const char *name,
569 Error **errp);
570int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab);
571int qcow2_snapshot_load_tmp(BlockDriverState *bs,
572 const char *snapshot_id,
573 const char *name,
574 Error **errp);
575
576void qcow2_free_snapshots(BlockDriverState *bs);
577int qcow2_read_snapshots(BlockDriverState *bs);
578
579
580Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables);
581int qcow2_cache_destroy(BlockDriverState* bs, Qcow2Cache *c);
582
583void qcow2_cache_entry_mark_dirty(BlockDriverState *bs, Qcow2Cache *c,
584 void *table);
585int qcow2_cache_flush(BlockDriverState *bs, Qcow2Cache *c);
586int qcow2_cache_set_dependency(BlockDriverState *bs, Qcow2Cache *c,
587 Qcow2Cache *dependency);
588void qcow2_cache_depends_on_flush(Qcow2Cache *c);
589
590void qcow2_cache_clean_unused(BlockDriverState *bs, Qcow2Cache *c);
591int qcow2_cache_empty(BlockDriverState *bs, Qcow2Cache *c);
592
593int qcow2_cache_get(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
594 void **table);
595int qcow2_cache_get_empty(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
596 void **table);
597void qcow2_cache_put(BlockDriverState *bs, Qcow2Cache *c, void **table);
598
599#endif
600