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