]>
Commit | Line | Data |
---|---|---|
f7d0fe02 KW |
1 | /* |
2 | * Block driver for the QCOW version 2 format | |
3 | * | |
4 | * Copyright (c) 2004-2006 Fabrice Bellard | |
5 | * | |
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: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
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 | |
22 | * THE SOFTWARE. | |
23 | */ | |
24 | ||
25 | #ifndef BLOCK_QCOW2_H | |
26 | #define BLOCK_QCOW2_H | |
27 | ||
737e150e PB |
28 | #include "block/aes.h" |
29 | #include "block/coroutine.h" | |
f7d0fe02 | 30 | |
14899cdf FN |
31 | //#define DEBUG_ALLOC |
32 | //#define DEBUG_ALLOC2 | |
33 | //#define DEBUG_EXT | |
34 | ||
f7d0fe02 | 35 | #define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb) |
f7d0fe02 KW |
36 | |
37 | #define QCOW_CRYPT_NONE 0 | |
38 | #define QCOW_CRYPT_AES 1 | |
39 | ||
40 | #define QCOW_MAX_CRYPT_CLUSTERS 32 | |
41 | ||
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) | |
6377af48 KW |
46 | /* The cluster reads as all zeros */ |
47 | #define QCOW_OFLAG_ZERO (1LL << 0) | |
f7d0fe02 KW |
48 | |
49 | #define REFCOUNT_SHIFT 1 /* refcount size is 2 bytes */ | |
50 | ||
51 | #define MIN_CLUSTER_BITS 9 | |
80ee15a6 | 52 | #define MAX_CLUSTER_BITS 21 |
f7d0fe02 KW |
53 | |
54 | #define L2_CACHE_SIZE 16 | |
55 | ||
29c1a730 KW |
56 | /* Must be at least 4 to cover all cases of refcount table growth */ |
57 | #define REFCOUNT_CACHE_SIZE 4 | |
58 | ||
99cce9fa KW |
59 | #define DEFAULT_CLUSTER_SIZE 65536 |
60 | ||
f7d0fe02 KW |
61 | typedef struct QCowHeader { |
62 | uint32_t magic; | |
63 | uint32_t version; | |
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; | |
6744cbab KW |
75 | |
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; | |
80 | ||
81 | uint32_t refcount_order; | |
82 | uint32_t header_length; | |
f7d0fe02 KW |
83 | } QCowHeader; |
84 | ||
85 | typedef struct QCowSnapshot { | |
86 | uint64_t l1_table_offset; | |
87 | uint32_t l1_size; | |
88 | char *id_str; | |
89 | char *name; | |
90b27759 | 90 | uint64_t disk_size; |
c2c9a466 | 91 | uint64_t vm_state_size; |
f7d0fe02 KW |
92 | uint32_t date_sec; |
93 | uint32_t date_nsec; | |
94 | uint64_t vm_clock_nsec; | |
95 | } QCowSnapshot; | |
96 | ||
49381094 KW |
97 | struct Qcow2Cache; |
98 | typedef struct Qcow2Cache Qcow2Cache; | |
99 | ||
75bab85c KW |
100 | typedef struct Qcow2UnknownHeaderExtension { |
101 | uint32_t magic; | |
102 | uint32_t len; | |
103 | QLIST_ENTRY(Qcow2UnknownHeaderExtension) next; | |
104 | uint8_t data[]; | |
105 | } Qcow2UnknownHeaderExtension; | |
106 | ||
cfcc4c62 KW |
107 | enum { |
108 | QCOW2_FEAT_TYPE_INCOMPATIBLE = 0, | |
109 | QCOW2_FEAT_TYPE_COMPATIBLE = 1, | |
110 | QCOW2_FEAT_TYPE_AUTOCLEAR = 2, | |
111 | }; | |
112 | ||
c61d0004 SH |
113 | /* Incompatible feature bits */ |
114 | enum { | |
115 | QCOW2_INCOMPAT_DIRTY_BITNR = 0, | |
116 | QCOW2_INCOMPAT_DIRTY = 1 << QCOW2_INCOMPAT_DIRTY_BITNR, | |
117 | ||
118 | QCOW2_INCOMPAT_MASK = QCOW2_INCOMPAT_DIRTY, | |
119 | }; | |
120 | ||
bfe8043e SH |
121 | /* Compatible feature bits */ |
122 | enum { | |
123 | QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR = 0, | |
124 | QCOW2_COMPAT_LAZY_REFCOUNTS = 1 << QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR, | |
125 | ||
126 | QCOW2_COMPAT_FEAT_MASK = QCOW2_COMPAT_LAZY_REFCOUNTS, | |
127 | }; | |
128 | ||
cfcc4c62 KW |
129 | typedef struct Qcow2Feature { |
130 | uint8_t type; | |
131 | uint8_t bit; | |
132 | char name[46]; | |
133 | } QEMU_PACKED Qcow2Feature; | |
134 | ||
f7d0fe02 | 135 | typedef struct BDRVQcowState { |
f7d0fe02 KW |
136 | int cluster_bits; |
137 | int cluster_size; | |
138 | int cluster_sectors; | |
139 | int l2_bits; | |
140 | int l2_size; | |
141 | int l1_size; | |
142 | int l1_vm_state_index; | |
143 | int csize_shift; | |
144 | int csize_mask; | |
145 | uint64_t cluster_offset_mask; | |
146 | uint64_t l1_table_offset; | |
147 | uint64_t *l1_table; | |
29c1a730 KW |
148 | |
149 | Qcow2Cache* l2_table_cache; | |
150 | Qcow2Cache* refcount_block_cache; | |
151 | ||
f7d0fe02 KW |
152 | uint8_t *cluster_cache; |
153 | uint8_t *cluster_data; | |
154 | uint64_t cluster_cache_offset; | |
72cf2d4f | 155 | QLIST_HEAD(QCowClusterAlloc, QCowL2Meta) cluster_allocs; |
f7d0fe02 KW |
156 | |
157 | uint64_t *refcount_table; | |
158 | uint64_t refcount_table_offset; | |
159 | uint32_t refcount_table_size; | |
f7d0fe02 KW |
160 | int64_t free_cluster_index; |
161 | int64_t free_byte_offset; | |
162 | ||
68d100e9 KW |
163 | CoMutex lock; |
164 | ||
f7d0fe02 KW |
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; | |
170 | int snapshots_size; | |
171 | int nb_snapshots; | |
172 | QCowSnapshot *snapshots; | |
06d9260f AL |
173 | |
174 | int flags; | |
6744cbab KW |
175 | int qcow_version; |
176 | ||
177 | uint64_t incompatible_features; | |
178 | uint64_t compatible_features; | |
179 | uint64_t autoclear_features; | |
180 | ||
181 | size_t unknown_header_fields_size; | |
182 | void* unknown_header_fields; | |
75bab85c | 183 | QLIST_HEAD(, Qcow2UnknownHeaderExtension) unknown_header_ext; |
f7d0fe02 KW |
184 | } BDRVQcowState; |
185 | ||
186 | /* XXX: use std qcow open function ? */ | |
187 | typedef struct QCowCreateState { | |
188 | int cluster_size; | |
189 | int cluster_bits; | |
190 | uint16_t *refcount_block; | |
191 | uint64_t *refcount_table; | |
192 | int64_t l1_table_offset; | |
193 | int64_t refcount_table_offset; | |
194 | int64_t refcount_block_offset; | |
195 | } QCowCreateState; | |
196 | ||
f214978a KW |
197 | struct QCowAIOCB; |
198 | ||
593fb83c KW |
199 | typedef struct Qcow2COWRegion { |
200 | /** | |
201 | * Offset of the COW region in bytes from the start of the first cluster | |
202 | * touched by the request. | |
203 | */ | |
204 | uint64_t offset; | |
205 | ||
206 | /** Number of sectors to copy */ | |
207 | int nb_sectors; | |
208 | } Qcow2COWRegion; | |
209 | ||
f50f88b9 KW |
210 | /** |
211 | * Describes an in-flight (part of a) write request that writes to clusters | |
212 | * that are not referenced in their L2 table yet. | |
213 | */ | |
45aba42f KW |
214 | typedef struct QCowL2Meta |
215 | { | |
1d3afd64 | 216 | /** Guest offset of the first newly allocated cluster */ |
45aba42f | 217 | uint64_t offset; |
1d3afd64 | 218 | |
1d3afd64 | 219 | /** Host offset of the first newly allocated cluster */ |
250196f1 | 220 | uint64_t alloc_offset; |
1d3afd64 | 221 | |
1d3afd64 KW |
222 | /** |
223 | * Number of sectors from the start of the first allocated cluster to | |
224 | * the end of the (possibly shortened) request | |
225 | */ | |
45aba42f | 226 | int nb_available; |
1d3afd64 KW |
227 | |
228 | /** Number of newly allocated clusters */ | |
45aba42f | 229 | int nb_clusters; |
1d3afd64 KW |
230 | |
231 | /** | |
232 | * Requests that overlap with this allocation and wait to be restarted | |
233 | * when the allocating request has completed. | |
234 | */ | |
68d100e9 | 235 | CoQueue dependent_requests; |
f214978a | 236 | |
593fb83c KW |
237 | /** |
238 | * The COW Region between the start of the first allocated cluster and the | |
239 | * area the guest actually writes to. | |
240 | */ | |
241 | Qcow2COWRegion cow_start; | |
242 | ||
243 | /** | |
244 | * The COW Region between the area the guest actually writes to and the | |
245 | * end of the last allocated cluster. | |
246 | */ | |
247 | Qcow2COWRegion cow_end; | |
248 | ||
72cf2d4f | 249 | QLIST_ENTRY(QCowL2Meta) next_in_flight; |
45aba42f KW |
250 | } QCowL2Meta; |
251 | ||
68d000a3 KW |
252 | enum { |
253 | QCOW2_CLUSTER_UNALLOCATED, | |
254 | QCOW2_CLUSTER_NORMAL, | |
255 | QCOW2_CLUSTER_COMPRESSED, | |
6377af48 | 256 | QCOW2_CLUSTER_ZERO |
68d000a3 KW |
257 | }; |
258 | ||
259 | #define L1E_OFFSET_MASK 0x00ffffffffffff00ULL | |
260 | #define L2E_OFFSET_MASK 0x00ffffffffffff00ULL | |
261 | #define L2E_COMPRESSED_OFFSET_SIZE_MASK 0x3fffffffffffffffULL | |
262 | ||
76dc9e0c KW |
263 | #define REFT_OFFSET_MASK 0xffffffffffffff00ULL |
264 | ||
45aba42f | 265 | static inline int size_to_clusters(BDRVQcowState *s, int64_t size) |
f7d0fe02 KW |
266 | { |
267 | return (size + (s->cluster_size - 1)) >> s->cluster_bits; | |
268 | } | |
269 | ||
419b19d9 SH |
270 | static inline int size_to_l1(BDRVQcowState *s, int64_t size) |
271 | { | |
272 | int shift = s->cluster_bits + s->l2_bits; | |
273 | return (size + (1ULL << shift) - 1) >> shift; | |
274 | } | |
275 | ||
c142442b KW |
276 | static inline int64_t align_offset(int64_t offset, int n) |
277 | { | |
278 | offset = (offset + n - 1) & ~(n - 1); | |
279 | return offset; | |
280 | } | |
281 | ||
68d000a3 KW |
282 | static inline int qcow2_get_cluster_type(uint64_t l2_entry) |
283 | { | |
284 | if (l2_entry & QCOW_OFLAG_COMPRESSED) { | |
285 | return QCOW2_CLUSTER_COMPRESSED; | |
6377af48 KW |
286 | } else if (l2_entry & QCOW_OFLAG_ZERO) { |
287 | return QCOW2_CLUSTER_ZERO; | |
68d000a3 KW |
288 | } else if (!(l2_entry & L2E_OFFSET_MASK)) { |
289 | return QCOW2_CLUSTER_UNALLOCATED; | |
290 | } else { | |
291 | return QCOW2_CLUSTER_NORMAL; | |
292 | } | |
293 | } | |
294 | ||
bfe8043e SH |
295 | /* Check whether refcounts are eager or lazy */ |
296 | static inline bool qcow2_need_accurate_refcounts(BDRVQcowState *s) | |
297 | { | |
298 | return !(s->incompatible_features & QCOW2_INCOMPAT_DIRTY); | |
299 | } | |
c142442b | 300 | |
f7d0fe02 KW |
301 | // FIXME Need qcow2_ prefix to global functions |
302 | ||
303 | /* qcow2.c functions */ | |
bd28f835 KW |
304 | int qcow2_backing_read1(BlockDriverState *bs, QEMUIOVector *qiov, |
305 | int64_t sector_num, int nb_sectors); | |
280d3735 KW |
306 | |
307 | int qcow2_mark_dirty(BlockDriverState *bs); | |
e24e49e6 | 308 | int qcow2_update_header(BlockDriverState *bs); |
f7d0fe02 KW |
309 | |
310 | /* qcow2-refcount.c functions */ | |
ed6ccf0f KW |
311 | int qcow2_refcount_init(BlockDriverState *bs); |
312 | void qcow2_refcount_close(BlockDriverState *bs); | |
f7d0fe02 | 313 | |
ed6ccf0f | 314 | int64_t qcow2_alloc_clusters(BlockDriverState *bs, int64_t size); |
256900b1 KW |
315 | int qcow2_alloc_clusters_at(BlockDriverState *bs, uint64_t offset, |
316 | int nb_clusters); | |
ed6ccf0f KW |
317 | int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size); |
318 | void qcow2_free_clusters(BlockDriverState *bs, | |
45aba42f | 319 | int64_t offset, int64_t size); |
ed6ccf0f | 320 | void qcow2_free_any_clusters(BlockDriverState *bs, |
45aba42f | 321 | uint64_t cluster_offset, int nb_clusters); |
f7d0fe02 | 322 | |
ed6ccf0f KW |
323 | int qcow2_update_snapshot_refcount(BlockDriverState *bs, |
324 | int64_t l1_table_offset, int l1_size, int addend); | |
f7d0fe02 | 325 | |
166acf54 KW |
326 | int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res, |
327 | BdrvCheckMode fix); | |
f7d0fe02 | 328 | |
45aba42f | 329 | /* qcow2-cluster.c functions */ |
72893756 | 330 | int qcow2_grow_l1_table(BlockDriverState *bs, int min_size, bool exact_size); |
ed6ccf0f | 331 | void qcow2_l2_cache_reset(BlockDriverState *bs); |
66f82cee | 332 | int qcow2_decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset); |
ed6ccf0f | 333 | void qcow2_encrypt_sectors(BDRVQcowState *s, int64_t sector_num, |
45aba42f KW |
334 | uint8_t *out_buf, const uint8_t *in_buf, |
335 | int nb_sectors, int enc, | |
336 | const AES_KEY *key); | |
337 | ||
1c46efaa KW |
338 | int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset, |
339 | int *num, uint64_t *cluster_offset); | |
f4f0d391 | 340 | int qcow2_alloc_cluster_offset(BlockDriverState *bs, uint64_t offset, |
f50f88b9 | 341 | int n_start, int n_end, int *num, uint64_t *host_offset, QCowL2Meta **m); |
ed6ccf0f | 342 | uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs, |
45aba42f KW |
343 | uint64_t offset, |
344 | int compressed_size); | |
345 | ||
148da7ea | 346 | int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m); |
5ea929e3 KW |
347 | int qcow2_discard_clusters(BlockDriverState *bs, uint64_t offset, |
348 | int nb_sectors); | |
621f0589 | 349 | int qcow2_zero_clusters(BlockDriverState *bs, uint64_t offset, int nb_sectors); |
45aba42f | 350 | |
c142442b | 351 | /* qcow2-snapshot.c functions */ |
ed6ccf0f KW |
352 | int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info); |
353 | int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id); | |
354 | int qcow2_snapshot_delete(BlockDriverState *bs, const char *snapshot_id); | |
355 | int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab); | |
51ef6727 | 356 | int qcow2_snapshot_load_tmp(BlockDriverState *bs, const char *snapshot_name); |
c142442b | 357 | |
ed6ccf0f KW |
358 | void qcow2_free_snapshots(BlockDriverState *bs); |
359 | int qcow2_read_snapshots(BlockDriverState *bs); | |
c142442b | 360 | |
49381094 | 361 | /* qcow2-cache.c functions */ |
6af4e9ea | 362 | Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables); |
49381094 KW |
363 | int qcow2_cache_destroy(BlockDriverState* bs, Qcow2Cache *c); |
364 | ||
365 | void qcow2_cache_entry_mark_dirty(Qcow2Cache *c, void *table); | |
366 | int qcow2_cache_flush(BlockDriverState *bs, Qcow2Cache *c); | |
367 | int qcow2_cache_set_dependency(BlockDriverState *bs, Qcow2Cache *c, | |
368 | Qcow2Cache *dependency); | |
3de0a294 | 369 | void qcow2_cache_depends_on_flush(Qcow2Cache *c); |
49381094 KW |
370 | |
371 | int qcow2_cache_get(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset, | |
372 | void **table); | |
373 | int qcow2_cache_get_empty(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset, | |
374 | void **table); | |
375 | int qcow2_cache_put(BlockDriverState *bs, Qcow2Cache *c, void **table); | |
376 | ||
f7d0fe02 | 377 | #endif |