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
29 #include "block/coroutine.h"
32 //#define DEBUG_ALLOC2
35 #define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
37 #define QCOW_CRYPT_NONE 0
38 #define QCOW_CRYPT_AES 1
40 #define QCOW_MAX_CRYPT_CLUSTERS 32
41 #define QCOW_MAX_SNAPSHOTS 65536
43 /* 8 MB refcount table is enough for 2 PB images at 64k cluster size
44 * (128 GB for 512 byte clusters, 2 EB for 2 MB clusters) */
45 #define QCOW_MAX_REFTABLE_SIZE 0x800000
47 /* 32 MB L1 table is enough for 2 PB images at 64k cluster size
48 * (128 GB for 512 byte clusters, 2 EB for 2 MB clusters) */
49 #define QCOW_MAX_L1_SIZE 0x2000000
51 /* Allow for an average of 1k per snapshot table entry, should be plenty of
52 * space for snapshot names and IDs */
53 #define QCOW_MAX_SNAPSHOTS_SIZE (1024 * QCOW_MAX_SNAPSHOTS)
55 /* indicate that the refcount of the referenced cluster is exactly one. */
56 #define QCOW_OFLAG_COPIED (1ULL << 63)
57 /* indicate that the cluster is compressed (they never have the copied flag) */
58 #define QCOW_OFLAG_COMPRESSED (1ULL << 62)
59 /* The cluster reads as all zeros */
60 #define QCOW_OFLAG_ZERO (1ULL << 0)
62 #define MIN_CLUSTER_BITS 9
63 #define MAX_CLUSTER_BITS 21
65 /* Must be at least 2 to cover COW */
66 #define MIN_L2_CACHE_SIZE 2 /* clusters */
68 /* Must be at least 4 to cover all cases of refcount table growth */
69 #define MIN_REFCOUNT_CACHE_SIZE 4 /* clusters */
71 /* Whichever is more */
72 #define DEFAULT_L2_CACHE_CLUSTERS 8 /* clusters */
73 #define DEFAULT_L2_CACHE_BYTE_SIZE 1048576 /* bytes */
75 /* The refblock cache needs only a fourth of the L2 cache size to cover as many
77 #define DEFAULT_L2_REFCOUNT_SIZE_RATIO 4
79 #define DEFAULT_CLUSTER_SIZE 65536
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"
100 typedef struct QCowHeader
{
103 uint64_t backing_file_offset
;
104 uint32_t backing_file_size
;
105 uint32_t cluster_bits
;
106 uint64_t size
; /* in bytes */
107 uint32_t crypt_method
;
108 uint32_t l1_size
; /* XXX: save number of clusters instead ? */
109 uint64_t l1_table_offset
;
110 uint64_t refcount_table_offset
;
111 uint32_t refcount_table_clusters
;
112 uint32_t nb_snapshots
;
113 uint64_t snapshots_offset
;
115 /* The following fields are only valid for version >= 3 */
116 uint64_t incompatible_features
;
117 uint64_t compatible_features
;
118 uint64_t autoclear_features
;
120 uint32_t refcount_order
;
121 uint32_t header_length
;
122 } QEMU_PACKED QCowHeader
;
124 typedef struct QEMU_PACKED QCowSnapshotHeader
{
125 /* header is 8 byte aligned */
126 uint64_t l1_table_offset
;
129 uint16_t id_str_size
;
135 uint64_t vm_clock_nsec
;
137 uint32_t vm_state_size
;
138 uint32_t extra_data_size
; /* for extension */
139 /* extra data follows */
142 } QCowSnapshotHeader
;
144 typedef struct QEMU_PACKED QCowSnapshotExtraData
{
145 uint64_t vm_state_size_large
;
147 } QCowSnapshotExtraData
;
150 typedef struct QCowSnapshot
{
151 uint64_t l1_table_offset
;
156 uint64_t vm_state_size
;
159 uint64_t vm_clock_nsec
;
163 typedef struct Qcow2Cache Qcow2Cache
;
165 typedef struct Qcow2UnknownHeaderExtension
{
168 QLIST_ENTRY(Qcow2UnknownHeaderExtension
) next
;
170 } Qcow2UnknownHeaderExtension
;
173 QCOW2_FEAT_TYPE_INCOMPATIBLE
= 0,
174 QCOW2_FEAT_TYPE_COMPATIBLE
= 1,
175 QCOW2_FEAT_TYPE_AUTOCLEAR
= 2,
178 /* Incompatible feature bits */
180 QCOW2_INCOMPAT_DIRTY_BITNR
= 0,
181 QCOW2_INCOMPAT_CORRUPT_BITNR
= 1,
182 QCOW2_INCOMPAT_DIRTY
= 1 << QCOW2_INCOMPAT_DIRTY_BITNR
,
183 QCOW2_INCOMPAT_CORRUPT
= 1 << QCOW2_INCOMPAT_CORRUPT_BITNR
,
185 QCOW2_INCOMPAT_MASK
= QCOW2_INCOMPAT_DIRTY
186 | QCOW2_INCOMPAT_CORRUPT
,
189 /* Compatible feature bits */
191 QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR
= 0,
192 QCOW2_COMPAT_LAZY_REFCOUNTS
= 1 << QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR
,
194 QCOW2_COMPAT_FEAT_MASK
= QCOW2_COMPAT_LAZY_REFCOUNTS
,
197 enum qcow2_discard_type
{
198 QCOW2_DISCARD_NEVER
= 0,
199 QCOW2_DISCARD_ALWAYS
,
200 QCOW2_DISCARD_REQUEST
,
201 QCOW2_DISCARD_SNAPSHOT
,
206 typedef struct Qcow2Feature
{
210 } QEMU_PACKED Qcow2Feature
;
212 typedef struct Qcow2DiscardRegion
{
213 BlockDriverState
*bs
;
216 QTAILQ_ENTRY(Qcow2DiscardRegion
) next
;
217 } Qcow2DiscardRegion
;
219 typedef uint64_t Qcow2GetRefcountFunc(const void *refcount_array
,
221 typedef void Qcow2SetRefcountFunc(void *refcount_array
,
222 uint64_t index
, uint64_t value
);
224 typedef struct BDRVQcowState
{
231 int l1_vm_state_index
;
232 int refcount_block_bits
;
233 int refcount_block_size
;
236 uint64_t cluster_offset_mask
;
237 uint64_t l1_table_offset
;
240 Qcow2Cache
* l2_table_cache
;
241 Qcow2Cache
* refcount_block_cache
;
243 uint8_t *cluster_cache
;
244 uint8_t *cluster_data
;
245 uint64_t cluster_cache_offset
;
246 QLIST_HEAD(QCowClusterAlloc
, QCowL2Meta
) cluster_allocs
;
248 uint64_t *refcount_table
;
249 uint64_t refcount_table_offset
;
250 uint32_t refcount_table_size
;
251 uint64_t free_cluster_index
;
252 uint64_t free_byte_offset
;
256 uint32_t crypt_method
; /* current crypt method, 0 if no key yet */
257 uint32_t crypt_method_header
;
258 AES_KEY aes_encrypt_key
;
259 AES_KEY aes_decrypt_key
;
260 uint64_t snapshots_offset
;
262 unsigned int nb_snapshots
;
263 QCowSnapshot
*snapshots
;
267 bool use_lazy_refcounts
;
270 uint64_t refcount_max
;
272 Qcow2GetRefcountFunc
*get_refcount
;
273 Qcow2SetRefcountFunc
*set_refcount
;
275 bool discard_passthrough
[QCOW2_DISCARD_MAX
];
277 int overlap_check
; /* bitmask of Qcow2MetadataOverlap values */
278 bool signaled_corruption
;
280 uint64_t incompatible_features
;
281 uint64_t compatible_features
;
282 uint64_t autoclear_features
;
284 size_t unknown_header_fields_size
;
285 void* unknown_header_fields
;
286 QLIST_HEAD(, Qcow2UnknownHeaderExtension
) unknown_header_ext
;
287 QTAILQ_HEAD (, Qcow2DiscardRegion
) discards
;
290 /* Backing file path and format as stored in the image (this is not the
291 * effective path/format, which may be the result of a runtime option
293 char *image_backing_file
;
294 char *image_backing_format
;
299 typedef struct Qcow2COWRegion
{
301 * Offset of the COW region in bytes from the start of the first cluster
302 * touched by the request.
306 /** Number of sectors to copy */
311 * Describes an in-flight (part of a) write request that writes to clusters
312 * that are not referenced in their L2 table yet.
314 typedef struct QCowL2Meta
316 /** Guest offset of the first newly allocated cluster */
319 /** Host offset of the first newly allocated cluster */
320 uint64_t alloc_offset
;
323 * Number of sectors from the start of the first allocated cluster to
324 * the end of the (possibly shortened) request
328 /** Number of newly allocated clusters */
332 * Requests that overlap with this allocation and wait to be restarted
333 * when the allocating request has completed.
335 CoQueue dependent_requests
;
338 * The COW Region between the start of the first allocated cluster and the
339 * area the guest actually writes to.
341 Qcow2COWRegion cow_start
;
344 * The COW Region between the area the guest actually writes to and the
345 * end of the last allocated cluster.
347 Qcow2COWRegion cow_end
;
349 /** Pointer to next L2Meta of the same write request */
350 struct QCowL2Meta
*next
;
352 QLIST_ENTRY(QCowL2Meta
) next_in_flight
;
356 QCOW2_CLUSTER_UNALLOCATED
,
357 QCOW2_CLUSTER_NORMAL
,
358 QCOW2_CLUSTER_COMPRESSED
,
362 typedef enum QCow2MetadataOverlap
{
363 QCOW2_OL_MAIN_HEADER_BITNR
= 0,
364 QCOW2_OL_ACTIVE_L1_BITNR
= 1,
365 QCOW2_OL_ACTIVE_L2_BITNR
= 2,
366 QCOW2_OL_REFCOUNT_TABLE_BITNR
= 3,
367 QCOW2_OL_REFCOUNT_BLOCK_BITNR
= 4,
368 QCOW2_OL_SNAPSHOT_TABLE_BITNR
= 5,
369 QCOW2_OL_INACTIVE_L1_BITNR
= 6,
370 QCOW2_OL_INACTIVE_L2_BITNR
= 7,
372 QCOW2_OL_MAX_BITNR
= 8,
375 QCOW2_OL_MAIN_HEADER
= (1 << QCOW2_OL_MAIN_HEADER_BITNR
),
376 QCOW2_OL_ACTIVE_L1
= (1 << QCOW2_OL_ACTIVE_L1_BITNR
),
377 QCOW2_OL_ACTIVE_L2
= (1 << QCOW2_OL_ACTIVE_L2_BITNR
),
378 QCOW2_OL_REFCOUNT_TABLE
= (1 << QCOW2_OL_REFCOUNT_TABLE_BITNR
),
379 QCOW2_OL_REFCOUNT_BLOCK
= (1 << QCOW2_OL_REFCOUNT_BLOCK_BITNR
),
380 QCOW2_OL_SNAPSHOT_TABLE
= (1 << QCOW2_OL_SNAPSHOT_TABLE_BITNR
),
381 QCOW2_OL_INACTIVE_L1
= (1 << QCOW2_OL_INACTIVE_L1_BITNR
),
382 /* NOTE: Checking overlaps with inactive L2 tables will result in bdrv
384 QCOW2_OL_INACTIVE_L2
= (1 << QCOW2_OL_INACTIVE_L2_BITNR
),
385 } QCow2MetadataOverlap
;
387 /* Perform all overlap checks which can be done in constant time */
388 #define QCOW2_OL_CONSTANT \
389 (QCOW2_OL_MAIN_HEADER | QCOW2_OL_ACTIVE_L1 | QCOW2_OL_REFCOUNT_TABLE | \
390 QCOW2_OL_SNAPSHOT_TABLE)
392 /* Perform all overlap checks which don't require disk access */
393 #define QCOW2_OL_CACHED \
394 (QCOW2_OL_CONSTANT | QCOW2_OL_ACTIVE_L2 | QCOW2_OL_REFCOUNT_BLOCK | \
395 QCOW2_OL_INACTIVE_L1)
397 /* Perform all overlap checks */
398 #define QCOW2_OL_ALL \
399 (QCOW2_OL_CACHED | QCOW2_OL_INACTIVE_L2)
401 #define L1E_OFFSET_MASK 0x00fffffffffffe00ULL
402 #define L2E_OFFSET_MASK 0x00fffffffffffe00ULL
403 #define L2E_COMPRESSED_OFFSET_SIZE_MASK 0x3fffffffffffffffULL
405 #define REFT_OFFSET_MASK 0xfffffffffffffe00ULL
407 static inline int64_t start_of_cluster(BDRVQcowState
*s
, int64_t offset
)
409 return offset
& ~(s
->cluster_size
- 1);
412 static inline int64_t offset_into_cluster(BDRVQcowState
*s
, int64_t offset
)
414 return offset
& (s
->cluster_size
- 1);
417 static inline int size_to_clusters(BDRVQcowState
*s
, int64_t size
)
419 return (size
+ (s
->cluster_size
- 1)) >> s
->cluster_bits
;
422 static inline int64_t size_to_l1(BDRVQcowState
*s
, int64_t size
)
424 int shift
= s
->cluster_bits
+ s
->l2_bits
;
425 return (size
+ (1ULL << shift
) - 1) >> shift
;
428 static inline int offset_to_l2_index(BDRVQcowState
*s
, int64_t offset
)
430 return (offset
>> s
->cluster_bits
) & (s
->l2_size
- 1);
433 static inline int64_t align_offset(int64_t offset
, int n
)
435 offset
= (offset
+ n
- 1) & ~(n
- 1);
439 static inline int64_t qcow2_vm_state_offset(BDRVQcowState
*s
)
441 return (int64_t)s
->l1_vm_state_index
<< (s
->cluster_bits
+ s
->l2_bits
);
444 static inline uint64_t qcow2_max_refcount_clusters(BDRVQcowState
*s
)
446 return QCOW_MAX_REFTABLE_SIZE
>> s
->cluster_bits
;
449 static inline int qcow2_get_cluster_type(uint64_t l2_entry
)
451 if (l2_entry
& QCOW_OFLAG_COMPRESSED
) {
452 return QCOW2_CLUSTER_COMPRESSED
;
453 } else if (l2_entry
& QCOW_OFLAG_ZERO
) {
454 return QCOW2_CLUSTER_ZERO
;
455 } else if (!(l2_entry
& L2E_OFFSET_MASK
)) {
456 return QCOW2_CLUSTER_UNALLOCATED
;
458 return QCOW2_CLUSTER_NORMAL
;
462 /* Check whether refcounts are eager or lazy */
463 static inline bool qcow2_need_accurate_refcounts(BDRVQcowState
*s
)
465 return !(s
->incompatible_features
& QCOW2_INCOMPAT_DIRTY
);
468 static inline uint64_t l2meta_cow_start(QCowL2Meta
*m
)
470 return m
->offset
+ m
->cow_start
.offset
;
473 static inline uint64_t l2meta_cow_end(QCowL2Meta
*m
)
475 return m
->offset
+ m
->cow_end
.offset
476 + (m
->cow_end
.nb_sectors
<< BDRV_SECTOR_BITS
);
479 static inline uint64_t refcount_diff(uint64_t r1
, uint64_t r2
)
481 return r1
> r2
? r1
- r2
: r2
- r1
;
484 // FIXME Need qcow2_ prefix to global functions
486 /* qcow2.c functions */
487 int qcow2_backing_read1(BlockDriverState
*bs
, QEMUIOVector
*qiov
,
488 int64_t sector_num
, int nb_sectors
);
490 int qcow2_mark_dirty(BlockDriverState
*bs
);
491 int qcow2_mark_corrupt(BlockDriverState
*bs
);
492 int qcow2_mark_consistent(BlockDriverState
*bs
);
493 int qcow2_update_header(BlockDriverState
*bs
);
495 void qcow2_signal_corruption(BlockDriverState
*bs
, bool fatal
, int64_t offset
,
496 int64_t size
, const char *message_format
, ...)
499 /* qcow2-refcount.c functions */
500 int qcow2_refcount_init(BlockDriverState
*bs
);
501 void qcow2_refcount_close(BlockDriverState
*bs
);
503 int qcow2_get_refcount(BlockDriverState
*bs
, int64_t cluster_index
,
506 int qcow2_update_cluster_refcount(BlockDriverState
*bs
, int64_t cluster_index
,
507 uint64_t addend
, bool decrease
,
508 enum qcow2_discard_type type
);
510 int64_t qcow2_alloc_clusters(BlockDriverState
*bs
, uint64_t size
);
511 int qcow2_alloc_clusters_at(BlockDriverState
*bs
, uint64_t offset
,
513 int64_t qcow2_alloc_bytes(BlockDriverState
*bs
, int size
);
514 void qcow2_free_clusters(BlockDriverState
*bs
,
515 int64_t offset
, int64_t size
,
516 enum qcow2_discard_type type
);
517 void qcow2_free_any_clusters(BlockDriverState
*bs
, uint64_t l2_entry
,
518 int nb_clusters
, enum qcow2_discard_type type
);
520 int qcow2_update_snapshot_refcount(BlockDriverState
*bs
,
521 int64_t l1_table_offset
, int l1_size
, int addend
);
523 int qcow2_check_refcounts(BlockDriverState
*bs
, BdrvCheckResult
*res
,
526 void qcow2_process_discards(BlockDriverState
*bs
, int ret
);
528 int qcow2_check_metadata_overlap(BlockDriverState
*bs
, int ign
, int64_t offset
,
530 int qcow2_pre_write_overlap_check(BlockDriverState
*bs
, int ign
, int64_t offset
,
533 /* qcow2-cluster.c functions */
534 int qcow2_grow_l1_table(BlockDriverState
*bs
, uint64_t min_size
,
536 int qcow2_write_l1_entry(BlockDriverState
*bs
, int l1_index
);
537 void qcow2_l2_cache_reset(BlockDriverState
*bs
);
538 int qcow2_decompress_cluster(BlockDriverState
*bs
, uint64_t cluster_offset
);
539 void qcow2_encrypt_sectors(BDRVQcowState
*s
, int64_t sector_num
,
540 uint8_t *out_buf
, const uint8_t *in_buf
,
541 int nb_sectors
, int enc
,
544 int qcow2_get_cluster_offset(BlockDriverState
*bs
, uint64_t offset
,
545 int *num
, uint64_t *cluster_offset
);
546 int qcow2_alloc_cluster_offset(BlockDriverState
*bs
, uint64_t offset
,
547 int *num
, uint64_t *host_offset
, QCowL2Meta
**m
);
548 uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState
*bs
,
550 int compressed_size
);
552 int qcow2_alloc_cluster_link_l2(BlockDriverState
*bs
, QCowL2Meta
*m
);
553 int qcow2_discard_clusters(BlockDriverState
*bs
, uint64_t offset
,
554 int nb_sectors
, enum qcow2_discard_type type
, bool full_discard
);
555 int qcow2_zero_clusters(BlockDriverState
*bs
, uint64_t offset
, int nb_sectors
);
557 int qcow2_expand_zero_clusters(BlockDriverState
*bs
,
558 BlockDriverAmendStatusCB
*status_cb
);
560 /* qcow2-snapshot.c functions */
561 int qcow2_snapshot_create(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
);
562 int qcow2_snapshot_goto(BlockDriverState
*bs
, const char *snapshot_id
);
563 int qcow2_snapshot_delete(BlockDriverState
*bs
,
564 const char *snapshot_id
,
567 int qcow2_snapshot_list(BlockDriverState
*bs
, QEMUSnapshotInfo
**psn_tab
);
568 int qcow2_snapshot_load_tmp(BlockDriverState
*bs
,
569 const char *snapshot_id
,
573 void qcow2_free_snapshots(BlockDriverState
*bs
);
574 int qcow2_read_snapshots(BlockDriverState
*bs
);
576 /* qcow2-cache.c functions */
577 Qcow2Cache
*qcow2_cache_create(BlockDriverState
*bs
, int num_tables
);
578 int qcow2_cache_destroy(BlockDriverState
* bs
, Qcow2Cache
*c
);
580 void qcow2_cache_entry_mark_dirty(BlockDriverState
*bs
, Qcow2Cache
*c
,
582 int qcow2_cache_flush(BlockDriverState
*bs
, Qcow2Cache
*c
);
583 int qcow2_cache_set_dependency(BlockDriverState
*bs
, Qcow2Cache
*c
,
584 Qcow2Cache
*dependency
);
585 void qcow2_cache_depends_on_flush(Qcow2Cache
*c
);
587 int qcow2_cache_empty(BlockDriverState
*bs
, Qcow2Cache
*c
);
589 int qcow2_cache_get(BlockDriverState
*bs
, Qcow2Cache
*c
, uint64_t offset
,
591 int qcow2_cache_get_empty(BlockDriverState
*bs
, Qcow2Cache
*c
, uint64_t offset
,
593 void qcow2_cache_put(BlockDriverState
*bs
, Qcow2Cache
*c
, void **table
);