root / block / qcow2.h @ 6cfcb9b8
History | View | Annotate | Download (12.5 kB)
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 |
|
28 |
#include "qemu/aes.h" |
29 |
#include "block/coroutine.h" |
30 |
|
31 |
//#define DEBUG_ALLOC
|
32 |
//#define DEBUG_ALLOC2
|
33 |
//#define DEBUG_EXT
|
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 |
|
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) |
48 |
|
49 |
#define REFCOUNT_SHIFT 1 /* refcount size is 2 bytes */ |
50 |
|
51 |
#define MIN_CLUSTER_BITS 9 |
52 |
#define MAX_CLUSTER_BITS 21 |
53 |
|
54 |
#define L2_CACHE_SIZE 16 |
55 |
|
56 |
/* Must be at least 4 to cover all cases of refcount table growth */
|
57 |
#define REFCOUNT_CACHE_SIZE 4 |
58 |
|
59 |
#define DEFAULT_CLUSTER_SIZE 65536 |
60 |
|
61 |
|
62 |
#define QCOW2_OPT_LAZY_REFCOUNTS "lazy_refcounts" |
63 |
|
64 |
typedef struct QCowHeader { |
65 |
uint32_t magic; |
66 |
uint32_t version; |
67 |
uint64_t backing_file_offset; |
68 |
uint32_t backing_file_size; |
69 |
uint32_t cluster_bits; |
70 |
uint64_t size; /* in bytes */
|
71 |
uint32_t crypt_method; |
72 |
uint32_t l1_size; /* XXX: save number of clusters instead ? */
|
73 |
uint64_t l1_table_offset; |
74 |
uint64_t refcount_table_offset; |
75 |
uint32_t refcount_table_clusters; |
76 |
uint32_t nb_snapshots; |
77 |
uint64_t snapshots_offset; |
78 |
|
79 |
/* The following fields are only valid for version >= 3 */
|
80 |
uint64_t incompatible_features; |
81 |
uint64_t compatible_features; |
82 |
uint64_t autoclear_features; |
83 |
|
84 |
uint32_t refcount_order; |
85 |
uint32_t header_length; |
86 |
} QCowHeader; |
87 |
|
88 |
typedef struct QCowSnapshot { |
89 |
uint64_t l1_table_offset; |
90 |
uint32_t l1_size; |
91 |
char *id_str;
|
92 |
char *name;
|
93 |
uint64_t disk_size; |
94 |
uint64_t vm_state_size; |
95 |
uint32_t date_sec; |
96 |
uint32_t date_nsec; |
97 |
uint64_t vm_clock_nsec; |
98 |
} QCowSnapshot; |
99 |
|
100 |
struct Qcow2Cache;
|
101 |
typedef struct Qcow2Cache Qcow2Cache; |
102 |
|
103 |
typedef struct Qcow2UnknownHeaderExtension { |
104 |
uint32_t magic; |
105 |
uint32_t len; |
106 |
QLIST_ENTRY(Qcow2UnknownHeaderExtension) next; |
107 |
uint8_t data[]; |
108 |
} Qcow2UnknownHeaderExtension; |
109 |
|
110 |
enum {
|
111 |
QCOW2_FEAT_TYPE_INCOMPATIBLE = 0,
|
112 |
QCOW2_FEAT_TYPE_COMPATIBLE = 1,
|
113 |
QCOW2_FEAT_TYPE_AUTOCLEAR = 2,
|
114 |
}; |
115 |
|
116 |
/* Incompatible feature bits */
|
117 |
enum {
|
118 |
QCOW2_INCOMPAT_DIRTY_BITNR = 0,
|
119 |
QCOW2_INCOMPAT_DIRTY = 1 << QCOW2_INCOMPAT_DIRTY_BITNR,
|
120 |
|
121 |
QCOW2_INCOMPAT_MASK = QCOW2_INCOMPAT_DIRTY, |
122 |
}; |
123 |
|
124 |
/* Compatible feature bits */
|
125 |
enum {
|
126 |
QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR = 0,
|
127 |
QCOW2_COMPAT_LAZY_REFCOUNTS = 1 << QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR,
|
128 |
|
129 |
QCOW2_COMPAT_FEAT_MASK = QCOW2_COMPAT_LAZY_REFCOUNTS, |
130 |
}; |
131 |
|
132 |
enum qcow2_discard_type {
|
133 |
QCOW2_DISCARD_NEVER = 0,
|
134 |
QCOW2_DISCARD_ALWAYS, |
135 |
QCOW2_DISCARD_REQUEST, |
136 |
QCOW2_DISCARD_SNAPSHOT, |
137 |
QCOW2_DISCARD_OTHER, |
138 |
QCOW2_DISCARD_MAX |
139 |
}; |
140 |
|
141 |
typedef struct Qcow2Feature { |
142 |
uint8_t type; |
143 |
uint8_t bit; |
144 |
char name[46]; |
145 |
} QEMU_PACKED Qcow2Feature; |
146 |
|
147 |
typedef struct BDRVQcowState { |
148 |
int cluster_bits;
|
149 |
int cluster_size;
|
150 |
int cluster_sectors;
|
151 |
int l2_bits;
|
152 |
int l2_size;
|
153 |
int l1_size;
|
154 |
int l1_vm_state_index;
|
155 |
int csize_shift;
|
156 |
int csize_mask;
|
157 |
uint64_t cluster_offset_mask; |
158 |
uint64_t l1_table_offset; |
159 |
uint64_t *l1_table; |
160 |
|
161 |
Qcow2Cache* l2_table_cache; |
162 |
Qcow2Cache* refcount_block_cache; |
163 |
|
164 |
uint8_t *cluster_cache; |
165 |
uint8_t *cluster_data; |
166 |
uint64_t cluster_cache_offset; |
167 |
QLIST_HEAD(QCowClusterAlloc, QCowL2Meta) cluster_allocs; |
168 |
|
169 |
uint64_t *refcount_table; |
170 |
uint64_t refcount_table_offset; |
171 |
uint32_t refcount_table_size; |
172 |
int64_t free_cluster_index; |
173 |
int64_t free_byte_offset; |
174 |
|
175 |
CoMutex lock; |
176 |
|
177 |
uint32_t crypt_method; /* current crypt method, 0 if no key yet */
|
178 |
uint32_t crypt_method_header; |
179 |
AES_KEY aes_encrypt_key; |
180 |
AES_KEY aes_decrypt_key; |
181 |
uint64_t snapshots_offset; |
182 |
int snapshots_size;
|
183 |
int nb_snapshots;
|
184 |
QCowSnapshot *snapshots; |
185 |
|
186 |
int flags;
|
187 |
int qcow_version;
|
188 |
bool use_lazy_refcounts;
|
189 |
|
190 |
uint64_t incompatible_features; |
191 |
uint64_t compatible_features; |
192 |
uint64_t autoclear_features; |
193 |
|
194 |
size_t unknown_header_fields_size; |
195 |
void* unknown_header_fields;
|
196 |
QLIST_HEAD(, Qcow2UnknownHeaderExtension) unknown_header_ext; |
197 |
} BDRVQcowState; |
198 |
|
199 |
/* XXX: use std qcow open function ? */
|
200 |
typedef struct QCowCreateState { |
201 |
int cluster_size;
|
202 |
int cluster_bits;
|
203 |
uint16_t *refcount_block; |
204 |
uint64_t *refcount_table; |
205 |
int64_t l1_table_offset; |
206 |
int64_t refcount_table_offset; |
207 |
int64_t refcount_block_offset; |
208 |
} QCowCreateState; |
209 |
|
210 |
struct QCowAIOCB;
|
211 |
|
212 |
typedef struct Qcow2COWRegion { |
213 |
/**
|
214 |
* Offset of the COW region in bytes from the start of the first cluster
|
215 |
* touched by the request.
|
216 |
*/
|
217 |
uint64_t offset; |
218 |
|
219 |
/** Number of sectors to copy */
|
220 |
int nb_sectors;
|
221 |
} Qcow2COWRegion; |
222 |
|
223 |
/**
|
224 |
* Describes an in-flight (part of a) write request that writes to clusters
|
225 |
* that are not referenced in their L2 table yet.
|
226 |
*/
|
227 |
typedef struct QCowL2Meta |
228 |
{ |
229 |
/** Guest offset of the first newly allocated cluster */
|
230 |
uint64_t offset; |
231 |
|
232 |
/** Host offset of the first newly allocated cluster */
|
233 |
uint64_t alloc_offset; |
234 |
|
235 |
/**
|
236 |
* Number of sectors from the start of the first allocated cluster to
|
237 |
* the end of the (possibly shortened) request
|
238 |
*/
|
239 |
int nb_available;
|
240 |
|
241 |
/** Number of newly allocated clusters */
|
242 |
int nb_clusters;
|
243 |
|
244 |
/**
|
245 |
* Requests that overlap with this allocation and wait to be restarted
|
246 |
* when the allocating request has completed.
|
247 |
*/
|
248 |
CoQueue dependent_requests; |
249 |
|
250 |
/**
|
251 |
* The COW Region between the start of the first allocated cluster and the
|
252 |
* area the guest actually writes to.
|
253 |
*/
|
254 |
Qcow2COWRegion cow_start; |
255 |
|
256 |
/**
|
257 |
* The COW Region between the area the guest actually writes to and the
|
258 |
* end of the last allocated cluster.
|
259 |
*/
|
260 |
Qcow2COWRegion cow_end; |
261 |
|
262 |
/** Pointer to next L2Meta of the same write request */
|
263 |
struct QCowL2Meta *next;
|
264 |
|
265 |
QLIST_ENTRY(QCowL2Meta) next_in_flight; |
266 |
} QCowL2Meta; |
267 |
|
268 |
enum {
|
269 |
QCOW2_CLUSTER_UNALLOCATED, |
270 |
QCOW2_CLUSTER_NORMAL, |
271 |
QCOW2_CLUSTER_COMPRESSED, |
272 |
QCOW2_CLUSTER_ZERO |
273 |
}; |
274 |
|
275 |
#define L1E_OFFSET_MASK 0x00ffffffffffff00ULL |
276 |
#define L2E_OFFSET_MASK 0x00ffffffffffff00ULL |
277 |
#define L2E_COMPRESSED_OFFSET_SIZE_MASK 0x3fffffffffffffffULL |
278 |
|
279 |
#define REFT_OFFSET_MASK 0xffffffffffffff00ULL |
280 |
|
281 |
static inline int64_t start_of_cluster(BDRVQcowState *s, int64_t offset) |
282 |
{ |
283 |
return offset & ~(s->cluster_size - 1); |
284 |
} |
285 |
|
286 |
static inline int64_t offset_into_cluster(BDRVQcowState *s, int64_t offset) |
287 |
{ |
288 |
return offset & (s->cluster_size - 1); |
289 |
} |
290 |
|
291 |
static inline int size_to_clusters(BDRVQcowState *s, int64_t size) |
292 |
{ |
293 |
return (size + (s->cluster_size - 1)) >> s->cluster_bits; |
294 |
} |
295 |
|
296 |
static inline int64_t size_to_l1(BDRVQcowState *s, int64_t size) |
297 |
{ |
298 |
int shift = s->cluster_bits + s->l2_bits;
|
299 |
return (size + (1ULL << shift) - 1) >> shift; |
300 |
} |
301 |
|
302 |
static inline int offset_to_l2_index(BDRVQcowState *s, int64_t offset) |
303 |
{ |
304 |
return (offset >> s->cluster_bits) & (s->l2_size - 1); |
305 |
} |
306 |
|
307 |
static inline int64_t align_offset(int64_t offset, int n) |
308 |
{ |
309 |
offset = (offset + n - 1) & ~(n - 1); |
310 |
return offset;
|
311 |
} |
312 |
|
313 |
static inline int qcow2_get_cluster_type(uint64_t l2_entry) |
314 |
{ |
315 |
if (l2_entry & QCOW_OFLAG_COMPRESSED) {
|
316 |
return QCOW2_CLUSTER_COMPRESSED;
|
317 |
} else if (l2_entry & QCOW_OFLAG_ZERO) { |
318 |
return QCOW2_CLUSTER_ZERO;
|
319 |
} else if (!(l2_entry & L2E_OFFSET_MASK)) { |
320 |
return QCOW2_CLUSTER_UNALLOCATED;
|
321 |
} else {
|
322 |
return QCOW2_CLUSTER_NORMAL;
|
323 |
} |
324 |
} |
325 |
|
326 |
/* Check whether refcounts are eager or lazy */
|
327 |
static inline bool qcow2_need_accurate_refcounts(BDRVQcowState *s) |
328 |
{ |
329 |
return !(s->incompatible_features & QCOW2_INCOMPAT_DIRTY);
|
330 |
} |
331 |
|
332 |
static inline uint64_t l2meta_cow_start(QCowL2Meta *m) |
333 |
{ |
334 |
return m->offset + m->cow_start.offset;
|
335 |
} |
336 |
|
337 |
static inline uint64_t l2meta_cow_end(QCowL2Meta *m) |
338 |
{ |
339 |
return m->offset + m->cow_end.offset
|
340 |
+ (m->cow_end.nb_sectors << BDRV_SECTOR_BITS); |
341 |
} |
342 |
|
343 |
// FIXME Need qcow2_ prefix to global functions
|
344 |
|
345 |
/* qcow2.c functions */
|
346 |
int qcow2_backing_read1(BlockDriverState *bs, QEMUIOVector *qiov,
|
347 |
int64_t sector_num, int nb_sectors);
|
348 |
|
349 |
int qcow2_mark_dirty(BlockDriverState *bs);
|
350 |
int qcow2_update_header(BlockDriverState *bs);
|
351 |
|
352 |
/* qcow2-refcount.c functions */
|
353 |
int qcow2_refcount_init(BlockDriverState *bs);
|
354 |
void qcow2_refcount_close(BlockDriverState *bs);
|
355 |
|
356 |
int64_t qcow2_alloc_clusters(BlockDriverState *bs, int64_t size); |
357 |
int qcow2_alloc_clusters_at(BlockDriverState *bs, uint64_t offset,
|
358 |
int nb_clusters);
|
359 |
int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size);
|
360 |
void qcow2_free_clusters(BlockDriverState *bs,
|
361 |
int64_t offset, int64_t size, |
362 |
enum qcow2_discard_type type);
|
363 |
void qcow2_free_any_clusters(BlockDriverState *bs, uint64_t l2_entry,
|
364 |
int nb_clusters, enum qcow2_discard_type type); |
365 |
|
366 |
int qcow2_update_snapshot_refcount(BlockDriverState *bs,
|
367 |
int64_t l1_table_offset, int l1_size, int addend); |
368 |
|
369 |
int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
|
370 |
BdrvCheckMode fix); |
371 |
|
372 |
/* qcow2-cluster.c functions */
|
373 |
int qcow2_grow_l1_table(BlockDriverState *bs, uint64_t min_size,
|
374 |
bool exact_size);
|
375 |
void qcow2_l2_cache_reset(BlockDriverState *bs);
|
376 |
int qcow2_decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset);
|
377 |
void qcow2_encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
|
378 |
uint8_t *out_buf, const uint8_t *in_buf,
|
379 |
int nb_sectors, int enc, |
380 |
const AES_KEY *key);
|
381 |
|
382 |
int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
|
383 |
int *num, uint64_t *cluster_offset);
|
384 |
int qcow2_alloc_cluster_offset(BlockDriverState *bs, uint64_t offset,
|
385 |
int n_start, int n_end, int *num, uint64_t *host_offset, QCowL2Meta **m); |
386 |
uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs, |
387 |
uint64_t offset, |
388 |
int compressed_size);
|
389 |
|
390 |
int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m);
|
391 |
int qcow2_discard_clusters(BlockDriverState *bs, uint64_t offset,
|
392 |
int nb_sectors);
|
393 |
int qcow2_zero_clusters(BlockDriverState *bs, uint64_t offset, int nb_sectors); |
394 |
|
395 |
/* qcow2-snapshot.c functions */
|
396 |
int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info);
|
397 |
int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id); |
398 |
int qcow2_snapshot_delete(BlockDriverState *bs, const char *snapshot_id); |
399 |
int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab);
|
400 |
int qcow2_snapshot_load_tmp(BlockDriverState *bs, const char *snapshot_name); |
401 |
|
402 |
void qcow2_free_snapshots(BlockDriverState *bs);
|
403 |
int qcow2_read_snapshots(BlockDriverState *bs);
|
404 |
|
405 |
/* qcow2-cache.c functions */
|
406 |
Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables);
|
407 |
int qcow2_cache_destroy(BlockDriverState* bs, Qcow2Cache *c);
|
408 |
|
409 |
void qcow2_cache_entry_mark_dirty(Qcow2Cache *c, void *table); |
410 |
int qcow2_cache_flush(BlockDriverState *bs, Qcow2Cache *c);
|
411 |
int qcow2_cache_set_dependency(BlockDriverState *bs, Qcow2Cache *c,
|
412 |
Qcow2Cache *dependency); |
413 |
void qcow2_cache_depends_on_flush(Qcow2Cache *c);
|
414 |
|
415 |
int qcow2_cache_get(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
|
416 |
void **table);
|
417 |
int qcow2_cache_get_empty(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
|
418 |
void **table);
|
419 |
int qcow2_cache_put(BlockDriverState *bs, Qcow2Cache *c, void **table); |
420 |
|
421 |
#endif
|