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
28 #include "block/aes.h"
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
42 /* indicate that the refcount of the referenced cluster is exactly one. */
43 #define QCOW_OFLAG_COPIED (1LL << 63)
44 /* indicate that the cluster is compressed (they never have the copied flag) */
45 #define QCOW_OFLAG_COMPRESSED (1LL << 62)
46 /* The cluster reads as all zeros */
47 #define QCOW_OFLAG_ZERO (1LL << 0)
49 #define REFCOUNT_SHIFT 1 /* refcount size is 2 bytes */
51 #define MIN_CLUSTER_BITS 9
52 #define MAX_CLUSTER_BITS 21
54 #define L2_CACHE_SIZE 16
56 /* Must be at least 4 to cover all cases of refcount table growth */
57 #define REFCOUNT_CACHE_SIZE 4
59 #define DEFAULT_CLUSTER_SIZE 65536
61 typedef struct QCowHeader
{
64 uint64_t backing_file_offset
;
65 uint32_t backing_file_size
;
66 uint32_t cluster_bits
;
67 uint64_t size
; /* in bytes */
68 uint32_t crypt_method
;
69 uint32_t l1_size
; /* XXX: save number of clusters instead ? */
70 uint64_t l1_table_offset
;
71 uint64_t refcount_table_offset
;
72 uint32_t refcount_table_clusters
;
73 uint32_t nb_snapshots
;
74 uint64_t snapshots_offset
;
76 /* The following fields are only valid for version >= 3 */
77 uint64_t incompatible_features
;
78 uint64_t compatible_features
;
79 uint64_t autoclear_features
;
81 uint32_t refcount_order
;
82 uint32_t header_length
;
85 typedef struct QCowSnapshot
{
86 uint64_t l1_table_offset
;
91 uint64_t vm_state_size
;
94 uint64_t vm_clock_nsec
;
98 typedef struct Qcow2Cache Qcow2Cache
;
100 typedef struct Qcow2UnknownHeaderExtension
{
103 QLIST_ENTRY(Qcow2UnknownHeaderExtension
) next
;
105 } Qcow2UnknownHeaderExtension
;
108 QCOW2_FEAT_TYPE_INCOMPATIBLE
= 0,
109 QCOW2_FEAT_TYPE_COMPATIBLE
= 1,
110 QCOW2_FEAT_TYPE_AUTOCLEAR
= 2,
113 /* Incompatible feature bits */
115 QCOW2_INCOMPAT_DIRTY_BITNR
= 0,
116 QCOW2_INCOMPAT_DIRTY
= 1 << QCOW2_INCOMPAT_DIRTY_BITNR
,
118 QCOW2_INCOMPAT_MASK
= QCOW2_INCOMPAT_DIRTY
,
121 /* Compatible feature bits */
123 QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR
= 0,
124 QCOW2_COMPAT_LAZY_REFCOUNTS
= 1 << QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR
,
126 QCOW2_COMPAT_FEAT_MASK
= QCOW2_COMPAT_LAZY_REFCOUNTS
,
129 typedef struct Qcow2Feature
{
133 } QEMU_PACKED Qcow2Feature
;
135 typedef struct BDRVQcowState
{
142 int l1_vm_state_index
;
145 uint64_t cluster_offset_mask
;
146 uint64_t l1_table_offset
;
149 Qcow2Cache
* l2_table_cache
;
150 Qcow2Cache
* refcount_block_cache
;
152 uint8_t *cluster_cache
;
153 uint8_t *cluster_data
;
154 uint64_t cluster_cache_offset
;
155 QLIST_HEAD(QCowClusterAlloc
, QCowL2Meta
) cluster_allocs
;
157 uint64_t *refcount_table
;
158 uint64_t refcount_table_offset
;
159 uint32_t refcount_table_size
;
160 int64_t free_cluster_index
;
161 int64_t free_byte_offset
;
165 uint32_t crypt_method
; /* current crypt method, 0 if no key yet */
166 uint32_t crypt_method_header
;
167 AES_KEY aes_encrypt_key
;
168 AES_KEY aes_decrypt_key
;
169 uint64_t snapshots_offset
;
172 QCowSnapshot
*snapshots
;
176 bool use_lazy_refcounts
;
178 uint64_t incompatible_features
;
179 uint64_t compatible_features
;
180 uint64_t autoclear_features
;
182 size_t unknown_header_fields_size
;
183 void* unknown_header_fields
;
184 QLIST_HEAD(, Qcow2UnknownHeaderExtension
) unknown_header_ext
;
187 /* XXX: use std qcow open function ? */
188 typedef struct QCowCreateState
{
191 uint16_t *refcount_block
;
192 uint64_t *refcount_table
;
193 int64_t l1_table_offset
;
194 int64_t refcount_table_offset
;
195 int64_t refcount_block_offset
;
200 typedef struct Qcow2COWRegion
{
202 * Offset of the COW region in bytes from the start of the first cluster
203 * touched by the request.
207 /** Number of sectors to copy */
212 * Describes an in-flight (part of a) write request that writes to clusters
213 * that are not referenced in their L2 table yet.
215 typedef struct QCowL2Meta
217 /** Guest offset of the first newly allocated cluster */
220 /** Host offset of the first newly allocated cluster */
221 uint64_t alloc_offset
;
224 * Number of sectors from the start of the first allocated cluster to
225 * the end of the (possibly shortened) request
229 /** Number of newly allocated clusters */
233 * Requests that overlap with this allocation and wait to be restarted
234 * when the allocating request has completed.
236 CoQueue dependent_requests
;
239 * The COW Region between the start of the first allocated cluster and the
240 * area the guest actually writes to.
242 Qcow2COWRegion cow_start
;
245 * The COW Region between the area the guest actually writes to and the
246 * end of the last allocated cluster.
248 Qcow2COWRegion cow_end
;
250 QLIST_ENTRY(QCowL2Meta
) next_in_flight
;
254 QCOW2_CLUSTER_UNALLOCATED
,
255 QCOW2_CLUSTER_NORMAL
,
256 QCOW2_CLUSTER_COMPRESSED
,
260 #define L1E_OFFSET_MASK 0x00ffffffffffff00ULL
261 #define L2E_OFFSET_MASK 0x00ffffffffffff00ULL
262 #define L2E_COMPRESSED_OFFSET_SIZE_MASK 0x3fffffffffffffffULL
264 #define REFT_OFFSET_MASK 0xffffffffffffff00ULL
266 static inline int size_to_clusters(BDRVQcowState
*s
, int64_t size
)
268 return (size
+ (s
->cluster_size
- 1)) >> s
->cluster_bits
;
271 static inline int size_to_l1(BDRVQcowState
*s
, int64_t size
)
273 int shift
= s
->cluster_bits
+ s
->l2_bits
;
274 return (size
+ (1ULL << shift
) - 1) >> shift
;
277 static inline int64_t align_offset(int64_t offset
, int n
)
279 offset
= (offset
+ n
- 1) & ~(n
- 1);
283 static inline int qcow2_get_cluster_type(uint64_t l2_entry
)
285 if (l2_entry
& QCOW_OFLAG_COMPRESSED
) {
286 return QCOW2_CLUSTER_COMPRESSED
;
287 } else if (l2_entry
& QCOW_OFLAG_ZERO
) {
288 return QCOW2_CLUSTER_ZERO
;
289 } else if (!(l2_entry
& L2E_OFFSET_MASK
)) {
290 return QCOW2_CLUSTER_UNALLOCATED
;
292 return QCOW2_CLUSTER_NORMAL
;
296 /* Check whether refcounts are eager or lazy */
297 static inline bool qcow2_need_accurate_refcounts(BDRVQcowState
*s
)
299 return !(s
->incompatible_features
& QCOW2_INCOMPAT_DIRTY
);
302 // FIXME Need qcow2_ prefix to global functions
304 /* qcow2.c functions */
305 int qcow2_backing_read1(BlockDriverState
*bs
, QEMUIOVector
*qiov
,
306 int64_t sector_num
, int nb_sectors
);
308 int qcow2_mark_dirty(BlockDriverState
*bs
);
309 int qcow2_update_header(BlockDriverState
*bs
);
311 /* qcow2-refcount.c functions */
312 int qcow2_refcount_init(BlockDriverState
*bs
);
313 void qcow2_refcount_close(BlockDriverState
*bs
);
315 int64_t qcow2_alloc_clusters(BlockDriverState
*bs
, int64_t size
);
316 int qcow2_alloc_clusters_at(BlockDriverState
*bs
, uint64_t offset
,
318 int64_t qcow2_alloc_bytes(BlockDriverState
*bs
, int size
);
319 void qcow2_free_clusters(BlockDriverState
*bs
,
320 int64_t offset
, int64_t size
);
321 void qcow2_free_any_clusters(BlockDriverState
*bs
,
322 uint64_t cluster_offset
, int nb_clusters
);
324 int qcow2_update_snapshot_refcount(BlockDriverState
*bs
,
325 int64_t l1_table_offset
, int l1_size
, int addend
);
327 int qcow2_check_refcounts(BlockDriverState
*bs
, BdrvCheckResult
*res
,
330 /* qcow2-cluster.c functions */
331 int qcow2_grow_l1_table(BlockDriverState
*bs
, int min_size
, bool exact_size
);
332 void qcow2_l2_cache_reset(BlockDriverState
*bs
);
333 int qcow2_decompress_cluster(BlockDriverState
*bs
, uint64_t cluster_offset
);
334 void qcow2_encrypt_sectors(BDRVQcowState
*s
, int64_t sector_num
,
335 uint8_t *out_buf
, const uint8_t *in_buf
,
336 int nb_sectors
, int enc
,
339 int qcow2_get_cluster_offset(BlockDriverState
*bs
, uint64_t offset
,
340 int *num
, uint64_t *cluster_offset
);
341 int qcow2_alloc_cluster_offset(BlockDriverState
*bs
, uint64_t offset
,
342 int n_start
, int n_end
, int *num
, uint64_t *host_offset
, QCowL2Meta
**m
);
343 uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState
*bs
,
345 int compressed_size
);
347 int qcow2_alloc_cluster_link_l2(BlockDriverState
*bs
, QCowL2Meta
*m
);
348 int qcow2_discard_clusters(BlockDriverState
*bs
, uint64_t offset
,
350 int qcow2_zero_clusters(BlockDriverState
*bs
, uint64_t offset
, int nb_sectors
);
352 /* qcow2-snapshot.c functions */
353 int qcow2_snapshot_create(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
);
354 int qcow2_snapshot_goto(BlockDriverState
*bs
, const char *snapshot_id
);
355 int qcow2_snapshot_delete(BlockDriverState
*bs
, const char *snapshot_id
);
356 int qcow2_snapshot_list(BlockDriverState
*bs
, QEMUSnapshotInfo
**psn_tab
);
357 int qcow2_snapshot_load_tmp(BlockDriverState
*bs
, const char *snapshot_name
);
359 void qcow2_free_snapshots(BlockDriverState
*bs
);
360 int qcow2_read_snapshots(BlockDriverState
*bs
);
362 /* qcow2-cache.c functions */
363 Qcow2Cache
*qcow2_cache_create(BlockDriverState
*bs
, int num_tables
);
364 int qcow2_cache_destroy(BlockDriverState
* bs
, Qcow2Cache
*c
);
366 void qcow2_cache_entry_mark_dirty(Qcow2Cache
*c
, void *table
);
367 int qcow2_cache_flush(BlockDriverState
*bs
, Qcow2Cache
*c
);
368 int qcow2_cache_set_dependency(BlockDriverState
*bs
, Qcow2Cache
*c
,
369 Qcow2Cache
*dependency
);
370 void qcow2_cache_depends_on_flush(Qcow2Cache
*c
);
372 int qcow2_cache_get(BlockDriverState
*bs
, Qcow2Cache
*c
, uint64_t offset
,
374 int qcow2_cache_get_empty(BlockDriverState
*bs
, Qcow2Cache
*c
, uint64_t offset
,
376 int qcow2_cache_put(BlockDriverState
*bs
, Qcow2Cache
*c
, void **table
);