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 "qemu-coroutine.h"
32 //#define DEBUG_ALLOC2
35 #define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
36 #define QCOW_VERSION 2
38 #define QCOW_CRYPT_NONE 0
39 #define QCOW_CRYPT_AES 1
41 #define QCOW_MAX_CRYPT_CLUSTERS 32
43 /* indicate that the refcount of the referenced cluster is exactly one. */
44 #define QCOW_OFLAG_COPIED (1LL << 63)
45 /* indicate that the cluster is compressed (they never have the copied flag) */
46 #define QCOW_OFLAG_COMPRESSED (1LL << 62)
48 #define REFCOUNT_SHIFT 1 /* refcount size is 2 bytes */
50 #define MIN_CLUSTER_BITS 9
51 #define MAX_CLUSTER_BITS 21
53 #define L2_CACHE_SIZE 16
55 /* Must be at least 4 to cover all cases of refcount table growth */
56 #define REFCOUNT_CACHE_SIZE 4
58 #define DEFAULT_CLUSTER_SIZE 65536
60 typedef struct QCowHeader
{
63 uint64_t backing_file_offset
;
64 uint32_t backing_file_size
;
65 uint32_t cluster_bits
;
66 uint64_t size
; /* in bytes */
67 uint32_t crypt_method
;
68 uint32_t l1_size
; /* XXX: save number of clusters instead ? */
69 uint64_t l1_table_offset
;
70 uint64_t refcount_table_offset
;
71 uint32_t refcount_table_clusters
;
72 uint32_t nb_snapshots
;
73 uint64_t snapshots_offset
;
76 typedef struct QCowSnapshot
{
77 uint64_t l1_table_offset
;
81 uint64_t vm_state_size
;
84 uint64_t vm_clock_nsec
;
88 typedef struct Qcow2Cache Qcow2Cache
;
90 typedef struct Qcow2UnknownHeaderExtension
{
93 QLIST_ENTRY(Qcow2UnknownHeaderExtension
) next
;
95 } Qcow2UnknownHeaderExtension
;
97 typedef struct BDRVQcowState
{
104 int l1_vm_state_index
;
107 uint64_t cluster_offset_mask
;
108 uint64_t l1_table_offset
;
111 Qcow2Cache
* l2_table_cache
;
112 Qcow2Cache
* refcount_block_cache
;
114 uint8_t *cluster_cache
;
115 uint8_t *cluster_data
;
116 uint64_t cluster_cache_offset
;
117 QLIST_HEAD(QCowClusterAlloc
, QCowL2Meta
) cluster_allocs
;
119 uint64_t *refcount_table
;
120 uint64_t refcount_table_offset
;
121 uint32_t refcount_table_size
;
122 int64_t free_cluster_index
;
123 int64_t free_byte_offset
;
127 uint32_t crypt_method
; /* current crypt method, 0 if no key yet */
128 uint32_t crypt_method_header
;
129 AES_KEY aes_encrypt_key
;
130 AES_KEY aes_decrypt_key
;
131 uint64_t snapshots_offset
;
134 QCowSnapshot
*snapshots
;
137 QLIST_HEAD(, Qcow2UnknownHeaderExtension
) unknown_header_ext
;
140 /* XXX: use std qcow open function ? */
141 typedef struct QCowCreateState
{
144 uint16_t *refcount_block
;
145 uint64_t *refcount_table
;
146 int64_t l1_table_offset
;
147 int64_t refcount_table_offset
;
148 int64_t refcount_block_offset
;
153 /* XXX This could be private for qcow2-cluster.c */
154 typedef struct QCowL2Meta
157 uint64_t cluster_offset
;
161 CoQueue dependent_requests
;
163 QLIST_ENTRY(QCowL2Meta
) next_in_flight
;
166 static inline int size_to_clusters(BDRVQcowState
*s
, int64_t size
)
168 return (size
+ (s
->cluster_size
- 1)) >> s
->cluster_bits
;
171 static inline int size_to_l1(BDRVQcowState
*s
, int64_t size
)
173 int shift
= s
->cluster_bits
+ s
->l2_bits
;
174 return (size
+ (1ULL << shift
) - 1) >> shift
;
177 static inline int64_t align_offset(int64_t offset
, int n
)
179 offset
= (offset
+ n
- 1) & ~(n
- 1);
184 // FIXME Need qcow2_ prefix to global functions
186 /* qcow2.c functions */
187 int qcow2_backing_read1(BlockDriverState
*bs
, QEMUIOVector
*qiov
,
188 int64_t sector_num
, int nb_sectors
);
189 int qcow2_update_header(BlockDriverState
*bs
);
191 /* qcow2-refcount.c functions */
192 int qcow2_refcount_init(BlockDriverState
*bs
);
193 void qcow2_refcount_close(BlockDriverState
*bs
);
195 int64_t qcow2_alloc_clusters(BlockDriverState
*bs
, int64_t size
);
196 int64_t qcow2_alloc_bytes(BlockDriverState
*bs
, int size
);
197 void qcow2_free_clusters(BlockDriverState
*bs
,
198 int64_t offset
, int64_t size
);
199 void qcow2_free_any_clusters(BlockDriverState
*bs
,
200 uint64_t cluster_offset
, int nb_clusters
);
202 int qcow2_update_snapshot_refcount(BlockDriverState
*bs
,
203 int64_t l1_table_offset
, int l1_size
, int addend
);
205 int qcow2_check_refcounts(BlockDriverState
*bs
, BdrvCheckResult
*res
);
207 /* qcow2-cluster.c functions */
208 int qcow2_grow_l1_table(BlockDriverState
*bs
, int min_size
, bool exact_size
);
209 void qcow2_l2_cache_reset(BlockDriverState
*bs
);
210 int qcow2_decompress_cluster(BlockDriverState
*bs
, uint64_t cluster_offset
);
211 void qcow2_encrypt_sectors(BDRVQcowState
*s
, int64_t sector_num
,
212 uint8_t *out_buf
, const uint8_t *in_buf
,
213 int nb_sectors
, int enc
,
216 int qcow2_get_cluster_offset(BlockDriverState
*bs
, uint64_t offset
,
217 int *num
, uint64_t *cluster_offset
);
218 int qcow2_alloc_cluster_offset(BlockDriverState
*bs
, uint64_t offset
,
219 int n_start
, int n_end
, int *num
, QCowL2Meta
*m
);
220 uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState
*bs
,
222 int compressed_size
);
224 int qcow2_alloc_cluster_link_l2(BlockDriverState
*bs
, QCowL2Meta
*m
);
225 int qcow2_discard_clusters(BlockDriverState
*bs
, uint64_t offset
,
228 /* qcow2-snapshot.c functions */
229 int qcow2_snapshot_create(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
);
230 int qcow2_snapshot_goto(BlockDriverState
*bs
, const char *snapshot_id
);
231 int qcow2_snapshot_delete(BlockDriverState
*bs
, const char *snapshot_id
);
232 int qcow2_snapshot_list(BlockDriverState
*bs
, QEMUSnapshotInfo
**psn_tab
);
233 int qcow2_snapshot_load_tmp(BlockDriverState
*bs
, const char *snapshot_name
);
235 void qcow2_free_snapshots(BlockDriverState
*bs
);
236 int qcow2_read_snapshots(BlockDriverState
*bs
);
238 /* qcow2-cache.c functions */
239 Qcow2Cache
*qcow2_cache_create(BlockDriverState
*bs
, int num_tables
,
241 int qcow2_cache_destroy(BlockDriverState
* bs
, Qcow2Cache
*c
);
242 bool qcow2_cache_set_writethrough(BlockDriverState
*bs
, Qcow2Cache
*c
,
245 void qcow2_cache_entry_mark_dirty(Qcow2Cache
*c
, void *table
);
246 int qcow2_cache_flush(BlockDriverState
*bs
, Qcow2Cache
*c
);
247 int qcow2_cache_set_dependency(BlockDriverState
*bs
, Qcow2Cache
*c
,
248 Qcow2Cache
*dependency
);
249 void qcow2_cache_depends_on_flush(Qcow2Cache
*c
);
251 int qcow2_cache_get(BlockDriverState
*bs
, Qcow2Cache
*c
, uint64_t offset
,
253 int qcow2_cache_get_empty(BlockDriverState
*bs
, Qcow2Cache
*c
, uint64_t offset
,
255 int qcow2_cache_put(BlockDriverState
*bs
, Qcow2Cache
*c
, void **table
);