]> git.proxmox.com Git - mirror_qemu.git/blame - block/qcow2.h
qcow2: Use byte granularity in qcow2_alloc_cluster_offset()
[mirror_qemu.git] / block / qcow2.h
CommitLineData
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
acdfb480
KW
61
62#define QCOW2_OPT_LAZY_REFCOUNTS "lazy_refcounts"
63
f7d0fe02
KW
64typedef 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;
6744cbab
KW
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;
f7d0fe02
KW
86} QCowHeader;
87
88typedef struct QCowSnapshot {
89 uint64_t l1_table_offset;
90 uint32_t l1_size;
91 char *id_str;
92 char *name;
90b27759 93 uint64_t disk_size;
c2c9a466 94 uint64_t vm_state_size;
f7d0fe02
KW
95 uint32_t date_sec;
96 uint32_t date_nsec;
97 uint64_t vm_clock_nsec;
98} QCowSnapshot;
99
49381094
KW
100struct Qcow2Cache;
101typedef struct Qcow2Cache Qcow2Cache;
102
75bab85c
KW
103typedef struct Qcow2UnknownHeaderExtension {
104 uint32_t magic;
105 uint32_t len;
106 QLIST_ENTRY(Qcow2UnknownHeaderExtension) next;
107 uint8_t data[];
108} Qcow2UnknownHeaderExtension;
109
cfcc4c62
KW
110enum {
111 QCOW2_FEAT_TYPE_INCOMPATIBLE = 0,
112 QCOW2_FEAT_TYPE_COMPATIBLE = 1,
113 QCOW2_FEAT_TYPE_AUTOCLEAR = 2,
114};
115
c61d0004
SH
116/* Incompatible feature bits */
117enum {
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
bfe8043e
SH
124/* Compatible feature bits */
125enum {
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
cfcc4c62
KW
132typedef struct Qcow2Feature {
133 uint8_t type;
134 uint8_t bit;
135 char name[46];
136} QEMU_PACKED Qcow2Feature;
137
f7d0fe02 138typedef struct BDRVQcowState {
f7d0fe02
KW
139 int cluster_bits;
140 int cluster_size;
141 int cluster_sectors;
142 int l2_bits;
143 int l2_size;
144 int l1_size;
145 int l1_vm_state_index;
146 int csize_shift;
147 int csize_mask;
148 uint64_t cluster_offset_mask;
149 uint64_t l1_table_offset;
150 uint64_t *l1_table;
29c1a730
KW
151
152 Qcow2Cache* l2_table_cache;
153 Qcow2Cache* refcount_block_cache;
154
f7d0fe02
KW
155 uint8_t *cluster_cache;
156 uint8_t *cluster_data;
157 uint64_t cluster_cache_offset;
72cf2d4f 158 QLIST_HEAD(QCowClusterAlloc, QCowL2Meta) cluster_allocs;
f7d0fe02
KW
159
160 uint64_t *refcount_table;
161 uint64_t refcount_table_offset;
162 uint32_t refcount_table_size;
f7d0fe02
KW
163 int64_t free_cluster_index;
164 int64_t free_byte_offset;
165
68d100e9
KW
166 CoMutex lock;
167
f7d0fe02
KW
168 uint32_t crypt_method; /* current crypt method, 0 if no key yet */
169 uint32_t crypt_method_header;
170 AES_KEY aes_encrypt_key;
171 AES_KEY aes_decrypt_key;
172 uint64_t snapshots_offset;
173 int snapshots_size;
174 int nb_snapshots;
175 QCowSnapshot *snapshots;
06d9260f
AL
176
177 int flags;
6744cbab 178 int qcow_version;
74c4510a 179 bool use_lazy_refcounts;
6744cbab
KW
180
181 uint64_t incompatible_features;
182 uint64_t compatible_features;
183 uint64_t autoclear_features;
184
185 size_t unknown_header_fields_size;
186 void* unknown_header_fields;
75bab85c 187 QLIST_HEAD(, Qcow2UnknownHeaderExtension) unknown_header_ext;
f7d0fe02
KW
188} BDRVQcowState;
189
190/* XXX: use std qcow open function ? */
191typedef struct QCowCreateState {
192 int cluster_size;
193 int cluster_bits;
194 uint16_t *refcount_block;
195 uint64_t *refcount_table;
196 int64_t l1_table_offset;
197 int64_t refcount_table_offset;
198 int64_t refcount_block_offset;
199} QCowCreateState;
200
f214978a
KW
201struct QCowAIOCB;
202
593fb83c
KW
203typedef struct Qcow2COWRegion {
204 /**
205 * Offset of the COW region in bytes from the start of the first cluster
206 * touched by the request.
207 */
208 uint64_t offset;
209
210 /** Number of sectors to copy */
211 int nb_sectors;
212} Qcow2COWRegion;
213
f50f88b9
KW
214/**
215 * Describes an in-flight (part of a) write request that writes to clusters
216 * that are not referenced in their L2 table yet.
217 */
45aba42f
KW
218typedef struct QCowL2Meta
219{
1d3afd64 220 /** Guest offset of the first newly allocated cluster */
45aba42f 221 uint64_t offset;
1d3afd64 222
1d3afd64 223 /** Host offset of the first newly allocated cluster */
250196f1 224 uint64_t alloc_offset;
1d3afd64 225
1d3afd64
KW
226 /**
227 * Number of sectors from the start of the first allocated cluster to
228 * the end of the (possibly shortened) request
229 */
45aba42f 230 int nb_available;
1d3afd64
KW
231
232 /** Number of newly allocated clusters */
45aba42f 233 int nb_clusters;
1d3afd64
KW
234
235 /**
236 * Requests that overlap with this allocation and wait to be restarted
237 * when the allocating request has completed.
238 */
68d100e9 239 CoQueue dependent_requests;
f214978a 240
593fb83c
KW
241 /**
242 * The COW Region between the start of the first allocated cluster and the
243 * area the guest actually writes to.
244 */
245 Qcow2COWRegion cow_start;
246
247 /**
248 * The COW Region between the area the guest actually writes to and the
249 * end of the last allocated cluster.
250 */
251 Qcow2COWRegion cow_end;
252
72cf2d4f 253 QLIST_ENTRY(QCowL2Meta) next_in_flight;
45aba42f
KW
254} QCowL2Meta;
255
68d000a3
KW
256enum {
257 QCOW2_CLUSTER_UNALLOCATED,
258 QCOW2_CLUSTER_NORMAL,
259 QCOW2_CLUSTER_COMPRESSED,
6377af48 260 QCOW2_CLUSTER_ZERO
68d000a3
KW
261};
262
263#define L1E_OFFSET_MASK 0x00ffffffffffff00ULL
264#define L2E_OFFSET_MASK 0x00ffffffffffff00ULL
265#define L2E_COMPRESSED_OFFSET_SIZE_MASK 0x3fffffffffffffffULL
266
76dc9e0c
KW
267#define REFT_OFFSET_MASK 0xffffffffffffff00ULL
268
3b8e2e26
KW
269static inline int64_t start_of_cluster(BDRVQcowState *s, int64_t offset)
270{
271 return offset & ~(s->cluster_size - 1);
272}
273
c37f4cd7
KW
274static inline int64_t offset_into_cluster(BDRVQcowState *s, int64_t offset)
275{
276 return offset & (s->cluster_size - 1);
277}
278
45aba42f 279static inline int size_to_clusters(BDRVQcowState *s, int64_t size)
f7d0fe02
KW
280{
281 return (size + (s->cluster_size - 1)) >> s->cluster_bits;
282}
283
419b19d9
SH
284static inline int size_to_l1(BDRVQcowState *s, int64_t size)
285{
286 int shift = s->cluster_bits + s->l2_bits;
287 return (size + (1ULL << shift) - 1) >> shift;
288}
289
17a71e58
KW
290static inline int offset_to_l2_index(BDRVQcowState *s, int64_t offset)
291{
292 return (offset >> s->cluster_bits) & (s->l2_size - 1);
293}
294
c142442b
KW
295static inline int64_t align_offset(int64_t offset, int n)
296{
297 offset = (offset + n - 1) & ~(n - 1);
298 return offset;
299}
300
68d000a3
KW
301static inline int qcow2_get_cluster_type(uint64_t l2_entry)
302{
303 if (l2_entry & QCOW_OFLAG_COMPRESSED) {
304 return QCOW2_CLUSTER_COMPRESSED;
6377af48
KW
305 } else if (l2_entry & QCOW_OFLAG_ZERO) {
306 return QCOW2_CLUSTER_ZERO;
68d000a3
KW
307 } else if (!(l2_entry & L2E_OFFSET_MASK)) {
308 return QCOW2_CLUSTER_UNALLOCATED;
309 } else {
310 return QCOW2_CLUSTER_NORMAL;
311 }
312}
313
bfe8043e
SH
314/* Check whether refcounts are eager or lazy */
315static inline bool qcow2_need_accurate_refcounts(BDRVQcowState *s)
316{
317 return !(s->incompatible_features & QCOW2_INCOMPAT_DIRTY);
318}
c142442b 319
65eb2e35
KW
320static inline uint64_t l2meta_cow_start(QCowL2Meta *m)
321{
322 return m->offset + m->cow_start.offset;
323}
324
325static inline uint64_t l2meta_cow_end(QCowL2Meta *m)
326{
327 return m->offset + m->cow_end.offset
328 + (m->cow_end.nb_sectors << BDRV_SECTOR_BITS);
329}
330
f7d0fe02
KW
331// FIXME Need qcow2_ prefix to global functions
332
333/* qcow2.c functions */
bd28f835
KW
334int qcow2_backing_read1(BlockDriverState *bs, QEMUIOVector *qiov,
335 int64_t sector_num, int nb_sectors);
280d3735
KW
336
337int qcow2_mark_dirty(BlockDriverState *bs);
e24e49e6 338int qcow2_update_header(BlockDriverState *bs);
f7d0fe02
KW
339
340/* qcow2-refcount.c functions */
ed6ccf0f
KW
341int qcow2_refcount_init(BlockDriverState *bs);
342void qcow2_refcount_close(BlockDriverState *bs);
f7d0fe02 343
ed6ccf0f 344int64_t qcow2_alloc_clusters(BlockDriverState *bs, int64_t size);
256900b1
KW
345int qcow2_alloc_clusters_at(BlockDriverState *bs, uint64_t offset,
346 int nb_clusters);
ed6ccf0f
KW
347int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size);
348void qcow2_free_clusters(BlockDriverState *bs,
45aba42f 349 int64_t offset, int64_t size);
ed6ccf0f 350void qcow2_free_any_clusters(BlockDriverState *bs,
45aba42f 351 uint64_t cluster_offset, int nb_clusters);
f7d0fe02 352
ed6ccf0f
KW
353int qcow2_update_snapshot_refcount(BlockDriverState *bs,
354 int64_t l1_table_offset, int l1_size, int addend);
f7d0fe02 355
166acf54
KW
356int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
357 BdrvCheckMode fix);
f7d0fe02 358
45aba42f 359/* qcow2-cluster.c functions */
72893756 360int qcow2_grow_l1_table(BlockDriverState *bs, int min_size, bool exact_size);
ed6ccf0f 361void qcow2_l2_cache_reset(BlockDriverState *bs);
66f82cee 362int qcow2_decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset);
ed6ccf0f 363void qcow2_encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
45aba42f
KW
364 uint8_t *out_buf, const uint8_t *in_buf,
365 int nb_sectors, int enc,
366 const AES_KEY *key);
367
1c46efaa
KW
368int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
369 int *num, uint64_t *cluster_offset);
f4f0d391 370int qcow2_alloc_cluster_offset(BlockDriverState *bs, uint64_t offset,
f50f88b9 371 int n_start, int n_end, int *num, uint64_t *host_offset, QCowL2Meta **m);
ed6ccf0f 372uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
45aba42f
KW
373 uint64_t offset,
374 int compressed_size);
375
148da7ea 376int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m);
5ea929e3
KW
377int qcow2_discard_clusters(BlockDriverState *bs, uint64_t offset,
378 int nb_sectors);
621f0589 379int qcow2_zero_clusters(BlockDriverState *bs, uint64_t offset, int nb_sectors);
45aba42f 380
c142442b 381/* qcow2-snapshot.c functions */
ed6ccf0f
KW
382int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info);
383int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id);
384int qcow2_snapshot_delete(BlockDriverState *bs, const char *snapshot_id);
385int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab);
51ef6727 386int qcow2_snapshot_load_tmp(BlockDriverState *bs, const char *snapshot_name);
c142442b 387
ed6ccf0f
KW
388void qcow2_free_snapshots(BlockDriverState *bs);
389int qcow2_read_snapshots(BlockDriverState *bs);
c142442b 390
49381094 391/* qcow2-cache.c functions */
6af4e9ea 392Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables);
49381094
KW
393int qcow2_cache_destroy(BlockDriverState* bs, Qcow2Cache *c);
394
395void qcow2_cache_entry_mark_dirty(Qcow2Cache *c, void *table);
396int qcow2_cache_flush(BlockDriverState *bs, Qcow2Cache *c);
397int qcow2_cache_set_dependency(BlockDriverState *bs, Qcow2Cache *c,
398 Qcow2Cache *dependency);
3de0a294 399void qcow2_cache_depends_on_flush(Qcow2Cache *c);
49381094
KW
400
401int qcow2_cache_get(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
402 void **table);
403int qcow2_cache_get_empty(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
404 void **table);
405int qcow2_cache_put(BlockDriverState *bs, Qcow2Cache *c, void **table);
406
f7d0fe02 407#endif