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