qcow2: Fix L1 allocation size in qcow2_snapshot_load_tmp() (CVE-2014-0145)
[qemu/kevin.git] / block / qcow2.h
blob364946545d42b8ea13b30d1623f39ebdd12357e1
1 /*
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
22 * THE SOFTWARE.
25 #ifndef BLOCK_QCOW2_H
26 #define BLOCK_QCOW2_H
28 #include "qemu/aes.h"
29 #include "block/coroutine.h"
31 //#define DEBUG_ALLOC
32 //#define DEBUG_ALLOC2
33 //#define DEBUG_EXT
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
41 #define QCOW_MAX_SNAPSHOTS 65536
43 /* 8 MB refcount table is enough for 2 PB images at 64k cluster size
44 * (128 GB for 512 byte clusters, 2 EB for 2 MB clusters) */
45 #define QCOW_MAX_REFTABLE_SIZE 0x800000
47 /* indicate that the refcount of the referenced cluster is exactly one. */
48 #define QCOW_OFLAG_COPIED (1ULL << 63)
49 /* indicate that the cluster is compressed (they never have the copied flag) */
50 #define QCOW_OFLAG_COMPRESSED (1ULL << 62)
51 /* The cluster reads as all zeros */
52 #define QCOW_OFLAG_ZERO (1ULL << 0)
54 #define REFCOUNT_SHIFT 1 /* refcount size is 2 bytes */
56 #define MIN_CLUSTER_BITS 9
57 #define MAX_CLUSTER_BITS 21
59 #define L2_CACHE_SIZE 16
61 /* Must be at least 4 to cover all cases of refcount table growth */
62 #define REFCOUNT_CACHE_SIZE 4
64 #define DEFAULT_CLUSTER_SIZE 65536
67 #define QCOW2_OPT_LAZY_REFCOUNTS "lazy-refcounts"
68 #define QCOW2_OPT_DISCARD_REQUEST "pass-discard-request"
69 #define QCOW2_OPT_DISCARD_SNAPSHOT "pass-discard-snapshot"
70 #define QCOW2_OPT_DISCARD_OTHER "pass-discard-other"
71 #define QCOW2_OPT_OVERLAP "overlap-check"
72 #define QCOW2_OPT_OVERLAP_MAIN_HEADER "overlap-check.main-header"
73 #define QCOW2_OPT_OVERLAP_ACTIVE_L1 "overlap-check.active-l1"
74 #define QCOW2_OPT_OVERLAP_ACTIVE_L2 "overlap-check.active-l2"
75 #define QCOW2_OPT_OVERLAP_REFCOUNT_TABLE "overlap-check.refcount-table"
76 #define QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK "overlap-check.refcount-block"
77 #define QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE "overlap-check.snapshot-table"
78 #define QCOW2_OPT_OVERLAP_INACTIVE_L1 "overlap-check.inactive-l1"
79 #define QCOW2_OPT_OVERLAP_INACTIVE_L2 "overlap-check.inactive-l2"
81 typedef struct QCowHeader {
82 uint32_t magic;
83 uint32_t version;
84 uint64_t backing_file_offset;
85 uint32_t backing_file_size;
86 uint32_t cluster_bits;
87 uint64_t size; /* in bytes */
88 uint32_t crypt_method;
89 uint32_t l1_size; /* XXX: save number of clusters instead ? */
90 uint64_t l1_table_offset;
91 uint64_t refcount_table_offset;
92 uint32_t refcount_table_clusters;
93 uint32_t nb_snapshots;
94 uint64_t snapshots_offset;
96 /* The following fields are only valid for version >= 3 */
97 uint64_t incompatible_features;
98 uint64_t compatible_features;
99 uint64_t autoclear_features;
101 uint32_t refcount_order;
102 uint32_t header_length;
103 } QEMU_PACKED QCowHeader;
105 typedef struct QEMU_PACKED QCowSnapshotHeader {
106 /* header is 8 byte aligned */
107 uint64_t l1_table_offset;
109 uint32_t l1_size;
110 uint16_t id_str_size;
111 uint16_t name_size;
113 uint32_t date_sec;
114 uint32_t date_nsec;
116 uint64_t vm_clock_nsec;
118 uint32_t vm_state_size;
119 uint32_t extra_data_size; /* for extension */
120 /* extra data follows */
121 /* id_str follows */
122 /* name follows */
123 } QCowSnapshotHeader;
125 typedef struct QEMU_PACKED QCowSnapshotExtraData {
126 uint64_t vm_state_size_large;
127 uint64_t disk_size;
128 } QCowSnapshotExtraData;
131 typedef struct QCowSnapshot {
132 uint64_t l1_table_offset;
133 uint32_t l1_size;
134 char *id_str;
135 char *name;
136 uint64_t disk_size;
137 uint64_t vm_state_size;
138 uint32_t date_sec;
139 uint32_t date_nsec;
140 uint64_t vm_clock_nsec;
141 } QCowSnapshot;
143 struct Qcow2Cache;
144 typedef struct Qcow2Cache Qcow2Cache;
146 typedef struct Qcow2UnknownHeaderExtension {
147 uint32_t magic;
148 uint32_t len;
149 QLIST_ENTRY(Qcow2UnknownHeaderExtension) next;
150 uint8_t data[];
151 } Qcow2UnknownHeaderExtension;
153 enum {
154 QCOW2_FEAT_TYPE_INCOMPATIBLE = 0,
155 QCOW2_FEAT_TYPE_COMPATIBLE = 1,
156 QCOW2_FEAT_TYPE_AUTOCLEAR = 2,
159 /* Incompatible feature bits */
160 enum {
161 QCOW2_INCOMPAT_DIRTY_BITNR = 0,
162 QCOW2_INCOMPAT_CORRUPT_BITNR = 1,
163 QCOW2_INCOMPAT_DIRTY = 1 << QCOW2_INCOMPAT_DIRTY_BITNR,
164 QCOW2_INCOMPAT_CORRUPT = 1 << QCOW2_INCOMPAT_CORRUPT_BITNR,
166 QCOW2_INCOMPAT_MASK = QCOW2_INCOMPAT_DIRTY
167 | QCOW2_INCOMPAT_CORRUPT,
170 /* Compatible feature bits */
171 enum {
172 QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR = 0,
173 QCOW2_COMPAT_LAZY_REFCOUNTS = 1 << QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR,
175 QCOW2_COMPAT_FEAT_MASK = QCOW2_COMPAT_LAZY_REFCOUNTS,
178 enum qcow2_discard_type {
179 QCOW2_DISCARD_NEVER = 0,
180 QCOW2_DISCARD_ALWAYS,
181 QCOW2_DISCARD_REQUEST,
182 QCOW2_DISCARD_SNAPSHOT,
183 QCOW2_DISCARD_OTHER,
184 QCOW2_DISCARD_MAX
187 typedef struct Qcow2Feature {
188 uint8_t type;
189 uint8_t bit;
190 char name[46];
191 } QEMU_PACKED Qcow2Feature;
193 typedef struct Qcow2DiscardRegion {
194 BlockDriverState *bs;
195 uint64_t offset;
196 uint64_t bytes;
197 QTAILQ_ENTRY(Qcow2DiscardRegion) next;
198 } Qcow2DiscardRegion;
200 typedef struct BDRVQcowState {
201 int cluster_bits;
202 int cluster_size;
203 int cluster_sectors;
204 int l2_bits;
205 int l2_size;
206 int l1_size;
207 int l1_vm_state_index;
208 int csize_shift;
209 int csize_mask;
210 uint64_t cluster_offset_mask;
211 uint64_t l1_table_offset;
212 uint64_t *l1_table;
214 Qcow2Cache* l2_table_cache;
215 Qcow2Cache* refcount_block_cache;
217 uint8_t *cluster_cache;
218 uint8_t *cluster_data;
219 uint64_t cluster_cache_offset;
220 QLIST_HEAD(QCowClusterAlloc, QCowL2Meta) cluster_allocs;
222 uint64_t *refcount_table;
223 uint64_t refcount_table_offset;
224 uint32_t refcount_table_size;
225 uint64_t free_cluster_index;
226 uint64_t free_byte_offset;
228 CoMutex lock;
230 uint32_t crypt_method; /* current crypt method, 0 if no key yet */
231 uint32_t crypt_method_header;
232 AES_KEY aes_encrypt_key;
233 AES_KEY aes_decrypt_key;
234 uint64_t snapshots_offset;
235 int snapshots_size;
236 unsigned int nb_snapshots;
237 QCowSnapshot *snapshots;
239 int flags;
240 int qcow_version;
241 bool use_lazy_refcounts;
242 int refcount_order;
244 bool discard_passthrough[QCOW2_DISCARD_MAX];
246 int overlap_check; /* bitmask of Qcow2MetadataOverlap values */
248 uint64_t incompatible_features;
249 uint64_t compatible_features;
250 uint64_t autoclear_features;
252 size_t unknown_header_fields_size;
253 void* unknown_header_fields;
254 QLIST_HEAD(, Qcow2UnknownHeaderExtension) unknown_header_ext;
255 QTAILQ_HEAD (, Qcow2DiscardRegion) discards;
256 bool cache_discards;
257 } BDRVQcowState;
259 /* XXX: use std qcow open function ? */
260 typedef struct QCowCreateState {
261 int cluster_size;
262 int cluster_bits;
263 uint16_t *refcount_block;
264 uint64_t *refcount_table;
265 int64_t l1_table_offset;
266 int64_t refcount_table_offset;
267 int64_t refcount_block_offset;
268 } QCowCreateState;
270 struct QCowAIOCB;
272 typedef struct Qcow2COWRegion {
274 * Offset of the COW region in bytes from the start of the first cluster
275 * touched by the request.
277 uint64_t offset;
279 /** Number of sectors to copy */
280 int nb_sectors;
281 } Qcow2COWRegion;
284 * Describes an in-flight (part of a) write request that writes to clusters
285 * that are not referenced in their L2 table yet.
287 typedef struct QCowL2Meta
289 /** Guest offset of the first newly allocated cluster */
290 uint64_t offset;
292 /** Host offset of the first newly allocated cluster */
293 uint64_t alloc_offset;
296 * Number of sectors from the start of the first allocated cluster to
297 * the end of the (possibly shortened) request
299 int nb_available;
301 /** Number of newly allocated clusters */
302 int nb_clusters;
305 * Requests that overlap with this allocation and wait to be restarted
306 * when the allocating request has completed.
308 CoQueue dependent_requests;
311 * The COW Region between the start of the first allocated cluster and the
312 * area the guest actually writes to.
314 Qcow2COWRegion cow_start;
317 * The COW Region between the area the guest actually writes to and the
318 * end of the last allocated cluster.
320 Qcow2COWRegion cow_end;
322 /** Pointer to next L2Meta of the same write request */
323 struct QCowL2Meta *next;
325 QLIST_ENTRY(QCowL2Meta) next_in_flight;
326 } QCowL2Meta;
328 enum {
329 QCOW2_CLUSTER_UNALLOCATED,
330 QCOW2_CLUSTER_NORMAL,
331 QCOW2_CLUSTER_COMPRESSED,
332 QCOW2_CLUSTER_ZERO
335 typedef enum QCow2MetadataOverlap {
336 QCOW2_OL_MAIN_HEADER_BITNR = 0,
337 QCOW2_OL_ACTIVE_L1_BITNR = 1,
338 QCOW2_OL_ACTIVE_L2_BITNR = 2,
339 QCOW2_OL_REFCOUNT_TABLE_BITNR = 3,
340 QCOW2_OL_REFCOUNT_BLOCK_BITNR = 4,
341 QCOW2_OL_SNAPSHOT_TABLE_BITNR = 5,
342 QCOW2_OL_INACTIVE_L1_BITNR = 6,
343 QCOW2_OL_INACTIVE_L2_BITNR = 7,
345 QCOW2_OL_MAX_BITNR = 8,
347 QCOW2_OL_NONE = 0,
348 QCOW2_OL_MAIN_HEADER = (1 << QCOW2_OL_MAIN_HEADER_BITNR),
349 QCOW2_OL_ACTIVE_L1 = (1 << QCOW2_OL_ACTIVE_L1_BITNR),
350 QCOW2_OL_ACTIVE_L2 = (1 << QCOW2_OL_ACTIVE_L2_BITNR),
351 QCOW2_OL_REFCOUNT_TABLE = (1 << QCOW2_OL_REFCOUNT_TABLE_BITNR),
352 QCOW2_OL_REFCOUNT_BLOCK = (1 << QCOW2_OL_REFCOUNT_BLOCK_BITNR),
353 QCOW2_OL_SNAPSHOT_TABLE = (1 << QCOW2_OL_SNAPSHOT_TABLE_BITNR),
354 QCOW2_OL_INACTIVE_L1 = (1 << QCOW2_OL_INACTIVE_L1_BITNR),
355 /* NOTE: Checking overlaps with inactive L2 tables will result in bdrv
356 * reads. */
357 QCOW2_OL_INACTIVE_L2 = (1 << QCOW2_OL_INACTIVE_L2_BITNR),
358 } QCow2MetadataOverlap;
360 /* Perform all overlap checks which can be done in constant time */
361 #define QCOW2_OL_CONSTANT \
362 (QCOW2_OL_MAIN_HEADER | QCOW2_OL_ACTIVE_L1 | QCOW2_OL_REFCOUNT_TABLE | \
363 QCOW2_OL_SNAPSHOT_TABLE)
365 /* Perform all overlap checks which don't require disk access */
366 #define QCOW2_OL_CACHED \
367 (QCOW2_OL_CONSTANT | QCOW2_OL_ACTIVE_L2 | QCOW2_OL_REFCOUNT_BLOCK | \
368 QCOW2_OL_INACTIVE_L1)
370 /* Perform all overlap checks */
371 #define QCOW2_OL_ALL \
372 (QCOW2_OL_CACHED | QCOW2_OL_INACTIVE_L2)
374 #define L1E_OFFSET_MASK 0x00fffffffffffe00ULL
375 #define L2E_OFFSET_MASK 0x00fffffffffffe00ULL
376 #define L2E_COMPRESSED_OFFSET_SIZE_MASK 0x3fffffffffffffffULL
378 #define REFT_OFFSET_MASK 0xfffffffffffffe00ULL
380 static inline int64_t start_of_cluster(BDRVQcowState *s, int64_t offset)
382 return offset & ~(s->cluster_size - 1);
385 static inline int64_t offset_into_cluster(BDRVQcowState *s, int64_t offset)
387 return offset & (s->cluster_size - 1);
390 static inline int size_to_clusters(BDRVQcowState *s, int64_t size)
392 return (size + (s->cluster_size - 1)) >> s->cluster_bits;
395 static inline int64_t size_to_l1(BDRVQcowState *s, int64_t size)
397 int shift = s->cluster_bits + s->l2_bits;
398 return (size + (1ULL << shift) - 1) >> shift;
401 static inline int offset_to_l2_index(BDRVQcowState *s, int64_t offset)
403 return (offset >> s->cluster_bits) & (s->l2_size - 1);
406 static inline int64_t align_offset(int64_t offset, int n)
408 offset = (offset + n - 1) & ~(n - 1);
409 return offset;
412 static inline int64_t qcow2_vm_state_offset(BDRVQcowState *s)
414 return (int64_t)s->l1_vm_state_index << (s->cluster_bits + s->l2_bits);
417 static inline uint64_t qcow2_max_refcount_clusters(BDRVQcowState *s)
419 return QCOW_MAX_REFTABLE_SIZE >> s->cluster_bits;
422 static inline int qcow2_get_cluster_type(uint64_t l2_entry)
424 if (l2_entry & QCOW_OFLAG_COMPRESSED) {
425 return QCOW2_CLUSTER_COMPRESSED;
426 } else if (l2_entry & QCOW_OFLAG_ZERO) {
427 return QCOW2_CLUSTER_ZERO;
428 } else if (!(l2_entry & L2E_OFFSET_MASK)) {
429 return QCOW2_CLUSTER_UNALLOCATED;
430 } else {
431 return QCOW2_CLUSTER_NORMAL;
435 /* Check whether refcounts are eager or lazy */
436 static inline bool qcow2_need_accurate_refcounts(BDRVQcowState *s)
438 return !(s->incompatible_features & QCOW2_INCOMPAT_DIRTY);
441 static inline uint64_t l2meta_cow_start(QCowL2Meta *m)
443 return m->offset + m->cow_start.offset;
446 static inline uint64_t l2meta_cow_end(QCowL2Meta *m)
448 return m->offset + m->cow_end.offset
449 + (m->cow_end.nb_sectors << BDRV_SECTOR_BITS);
452 // FIXME Need qcow2_ prefix to global functions
454 /* qcow2.c functions */
455 int qcow2_backing_read1(BlockDriverState *bs, QEMUIOVector *qiov,
456 int64_t sector_num, int nb_sectors);
458 int qcow2_mark_dirty(BlockDriverState *bs);
459 int qcow2_mark_corrupt(BlockDriverState *bs);
460 int qcow2_mark_consistent(BlockDriverState *bs);
461 int qcow2_update_header(BlockDriverState *bs);
463 /* qcow2-refcount.c functions */
464 int qcow2_refcount_init(BlockDriverState *bs);
465 void qcow2_refcount_close(BlockDriverState *bs);
467 int qcow2_update_cluster_refcount(BlockDriverState *bs, int64_t cluster_index,
468 int addend, enum qcow2_discard_type type);
470 int64_t qcow2_alloc_clusters(BlockDriverState *bs, uint64_t size);
471 int qcow2_alloc_clusters_at(BlockDriverState *bs, uint64_t offset,
472 int nb_clusters);
473 int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size);
474 void qcow2_free_clusters(BlockDriverState *bs,
475 int64_t offset, int64_t size,
476 enum qcow2_discard_type type);
477 void qcow2_free_any_clusters(BlockDriverState *bs, uint64_t l2_entry,
478 int nb_clusters, enum qcow2_discard_type type);
480 int qcow2_update_snapshot_refcount(BlockDriverState *bs,
481 int64_t l1_table_offset, int l1_size, int addend);
483 int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
484 BdrvCheckMode fix);
486 void qcow2_process_discards(BlockDriverState *bs, int ret);
488 int qcow2_check_metadata_overlap(BlockDriverState *bs, int ign, int64_t offset,
489 int64_t size);
490 int qcow2_pre_write_overlap_check(BlockDriverState *bs, int ign, int64_t offset,
491 int64_t size);
493 /* qcow2-cluster.c functions */
494 int qcow2_grow_l1_table(BlockDriverState *bs, uint64_t min_size,
495 bool exact_size);
496 int qcow2_write_l1_entry(BlockDriverState *bs, int l1_index);
497 void qcow2_l2_cache_reset(BlockDriverState *bs);
498 int qcow2_decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset);
499 void qcow2_encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
500 uint8_t *out_buf, const uint8_t *in_buf,
501 int nb_sectors, int enc,
502 const AES_KEY *key);
504 int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
505 int *num, uint64_t *cluster_offset);
506 int qcow2_alloc_cluster_offset(BlockDriverState *bs, uint64_t offset,
507 int *num, uint64_t *host_offset, QCowL2Meta **m);
508 uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
509 uint64_t offset,
510 int compressed_size);
512 int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m);
513 int qcow2_discard_clusters(BlockDriverState *bs, uint64_t offset,
514 int nb_sectors, enum qcow2_discard_type type);
515 int qcow2_zero_clusters(BlockDriverState *bs, uint64_t offset, int nb_sectors);
517 int qcow2_expand_zero_clusters(BlockDriverState *bs);
519 /* qcow2-snapshot.c functions */
520 int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info);
521 int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id);
522 int qcow2_snapshot_delete(BlockDriverState *bs,
523 const char *snapshot_id,
524 const char *name,
525 Error **errp);
526 int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab);
527 int qcow2_snapshot_load_tmp(BlockDriverState *bs,
528 const char *snapshot_id,
529 const char *name,
530 Error **errp);
532 void qcow2_free_snapshots(BlockDriverState *bs);
533 int qcow2_read_snapshots(BlockDriverState *bs);
535 /* qcow2-cache.c functions */
536 Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables);
537 int qcow2_cache_destroy(BlockDriverState* bs, Qcow2Cache *c);
539 void qcow2_cache_entry_mark_dirty(Qcow2Cache *c, void *table);
540 int qcow2_cache_flush(BlockDriverState *bs, Qcow2Cache *c);
541 int qcow2_cache_set_dependency(BlockDriverState *bs, Qcow2Cache *c,
542 Qcow2Cache *dependency);
543 void qcow2_cache_depends_on_flush(Qcow2Cache *c);
545 int qcow2_cache_empty(BlockDriverState *bs, Qcow2Cache *c);
547 int qcow2_cache_get(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
548 void **table);
549 int qcow2_cache_get_empty(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
550 void **table);
551 int qcow2_cache_put(BlockDriverState *bs, Qcow2Cache *c, void **table);
553 #endif