]> git.proxmox.com Git - qemu.git/blob - block/qcow2.h
qcow2: introduce dirty 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 "aes.h"
29 #include "qemu-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 typedef struct QCowHeader {
62 uint32_t magic;
63 uint32_t version;
64 uint64_t backing_file_offset;
65 uint32_t backing_file_size;
66 uint32_t cluster_bits;
67 uint64_t size; /* in bytes */
68 uint32_t crypt_method;
69 uint32_t l1_size; /* XXX: save number of clusters instead ? */
70 uint64_t l1_table_offset;
71 uint64_t refcount_table_offset;
72 uint32_t refcount_table_clusters;
73 uint32_t nb_snapshots;
74 uint64_t snapshots_offset;
75
76 /* The following fields are only valid for version >= 3 */
77 uint64_t incompatible_features;
78 uint64_t compatible_features;
79 uint64_t autoclear_features;
80
81 uint32_t refcount_order;
82 uint32_t header_length;
83 } QCowHeader;
84
85 typedef struct QCowSnapshot {
86 uint64_t l1_table_offset;
87 uint32_t l1_size;
88 char *id_str;
89 char *name;
90 uint64_t disk_size;
91 uint64_t vm_state_size;
92 uint32_t date_sec;
93 uint32_t date_nsec;
94 uint64_t vm_clock_nsec;
95 } QCowSnapshot;
96
97 struct Qcow2Cache;
98 typedef struct Qcow2Cache Qcow2Cache;
99
100 typedef struct Qcow2UnknownHeaderExtension {
101 uint32_t magic;
102 uint32_t len;
103 QLIST_ENTRY(Qcow2UnknownHeaderExtension) next;
104 uint8_t data[];
105 } Qcow2UnknownHeaderExtension;
106
107 enum {
108 QCOW2_FEAT_TYPE_INCOMPATIBLE = 0,
109 QCOW2_FEAT_TYPE_COMPATIBLE = 1,
110 QCOW2_FEAT_TYPE_AUTOCLEAR = 2,
111 };
112
113 /* Incompatible feature bits */
114 enum {
115 QCOW2_INCOMPAT_DIRTY_BITNR = 0,
116 QCOW2_INCOMPAT_DIRTY = 1 << QCOW2_INCOMPAT_DIRTY_BITNR,
117
118 QCOW2_INCOMPAT_MASK = QCOW2_INCOMPAT_DIRTY,
119 };
120
121 typedef struct Qcow2Feature {
122 uint8_t type;
123 uint8_t bit;
124 char name[46];
125 } QEMU_PACKED Qcow2Feature;
126
127 typedef struct BDRVQcowState {
128 int cluster_bits;
129 int cluster_size;
130 int cluster_sectors;
131 int l2_bits;
132 int l2_size;
133 int l1_size;
134 int l1_vm_state_index;
135 int csize_shift;
136 int csize_mask;
137 uint64_t cluster_offset_mask;
138 uint64_t l1_table_offset;
139 uint64_t *l1_table;
140
141 Qcow2Cache* l2_table_cache;
142 Qcow2Cache* refcount_block_cache;
143
144 uint8_t *cluster_cache;
145 uint8_t *cluster_data;
146 uint64_t cluster_cache_offset;
147 QLIST_HEAD(QCowClusterAlloc, QCowL2Meta) cluster_allocs;
148
149 uint64_t *refcount_table;
150 uint64_t refcount_table_offset;
151 uint32_t refcount_table_size;
152 int64_t free_cluster_index;
153 int64_t free_byte_offset;
154
155 CoMutex lock;
156
157 uint32_t crypt_method; /* current crypt method, 0 if no key yet */
158 uint32_t crypt_method_header;
159 AES_KEY aes_encrypt_key;
160 AES_KEY aes_decrypt_key;
161 uint64_t snapshots_offset;
162 int snapshots_size;
163 int nb_snapshots;
164 QCowSnapshot *snapshots;
165
166 int flags;
167 int qcow_version;
168
169 uint64_t incompatible_features;
170 uint64_t compatible_features;
171 uint64_t autoclear_features;
172
173 size_t unknown_header_fields_size;
174 void* unknown_header_fields;
175 QLIST_HEAD(, Qcow2UnknownHeaderExtension) unknown_header_ext;
176 } BDRVQcowState;
177
178 /* XXX: use std qcow open function ? */
179 typedef struct QCowCreateState {
180 int cluster_size;
181 int cluster_bits;
182 uint16_t *refcount_block;
183 uint64_t *refcount_table;
184 int64_t l1_table_offset;
185 int64_t refcount_table_offset;
186 int64_t refcount_block_offset;
187 } QCowCreateState;
188
189 struct QCowAIOCB;
190
191 /* XXX This could be private for qcow2-cluster.c */
192 typedef struct QCowL2Meta
193 {
194 uint64_t offset;
195 uint64_t cluster_offset;
196 uint64_t alloc_offset;
197 int n_start;
198 int nb_available;
199 int nb_clusters;
200 CoQueue dependent_requests;
201
202 QLIST_ENTRY(QCowL2Meta) next_in_flight;
203 } QCowL2Meta;
204
205 enum {
206 QCOW2_CLUSTER_UNALLOCATED,
207 QCOW2_CLUSTER_NORMAL,
208 QCOW2_CLUSTER_COMPRESSED,
209 QCOW2_CLUSTER_ZERO
210 };
211
212 #define L1E_OFFSET_MASK 0x00ffffffffffff00ULL
213 #define L2E_OFFSET_MASK 0x00ffffffffffff00ULL
214 #define L2E_COMPRESSED_OFFSET_SIZE_MASK 0x3fffffffffffffffULL
215
216 #define REFT_OFFSET_MASK 0xffffffffffffff00ULL
217
218 static inline int size_to_clusters(BDRVQcowState *s, int64_t size)
219 {
220 return (size + (s->cluster_size - 1)) >> s->cluster_bits;
221 }
222
223 static inline int size_to_l1(BDRVQcowState *s, int64_t size)
224 {
225 int shift = s->cluster_bits + s->l2_bits;
226 return (size + (1ULL << shift) - 1) >> shift;
227 }
228
229 static inline int64_t align_offset(int64_t offset, int n)
230 {
231 offset = (offset + n - 1) & ~(n - 1);
232 return offset;
233 }
234
235 static inline int qcow2_get_cluster_type(uint64_t l2_entry)
236 {
237 if (l2_entry & QCOW_OFLAG_COMPRESSED) {
238 return QCOW2_CLUSTER_COMPRESSED;
239 } else if (l2_entry & QCOW_OFLAG_ZERO) {
240 return QCOW2_CLUSTER_ZERO;
241 } else if (!(l2_entry & L2E_OFFSET_MASK)) {
242 return QCOW2_CLUSTER_UNALLOCATED;
243 } else {
244 return QCOW2_CLUSTER_NORMAL;
245 }
246 }
247
248
249 // FIXME Need qcow2_ prefix to global functions
250
251 /* qcow2.c functions */
252 int qcow2_backing_read1(BlockDriverState *bs, QEMUIOVector *qiov,
253 int64_t sector_num, int nb_sectors);
254 int qcow2_update_header(BlockDriverState *bs);
255
256 /* qcow2-refcount.c functions */
257 int qcow2_refcount_init(BlockDriverState *bs);
258 void qcow2_refcount_close(BlockDriverState *bs);
259
260 int64_t qcow2_alloc_clusters(BlockDriverState *bs, int64_t size);
261 int qcow2_alloc_clusters_at(BlockDriverState *bs, uint64_t offset,
262 int nb_clusters);
263 int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size);
264 void qcow2_free_clusters(BlockDriverState *bs,
265 int64_t offset, int64_t size);
266 void qcow2_free_any_clusters(BlockDriverState *bs,
267 uint64_t cluster_offset, int nb_clusters);
268
269 int qcow2_update_snapshot_refcount(BlockDriverState *bs,
270 int64_t l1_table_offset, int l1_size, int addend);
271
272 int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
273 BdrvCheckMode fix);
274
275 /* qcow2-cluster.c functions */
276 int qcow2_grow_l1_table(BlockDriverState *bs, int min_size, bool exact_size);
277 void qcow2_l2_cache_reset(BlockDriverState *bs);
278 int qcow2_decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset);
279 void qcow2_encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
280 uint8_t *out_buf, const uint8_t *in_buf,
281 int nb_sectors, int enc,
282 const AES_KEY *key);
283
284 int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
285 int *num, uint64_t *cluster_offset);
286 int qcow2_alloc_cluster_offset(BlockDriverState *bs, uint64_t offset,
287 int n_start, int n_end, int *num, QCowL2Meta *m);
288 uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
289 uint64_t offset,
290 int compressed_size);
291
292 int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m);
293 int qcow2_discard_clusters(BlockDriverState *bs, uint64_t offset,
294 int nb_sectors);
295 int qcow2_zero_clusters(BlockDriverState *bs, uint64_t offset, int nb_sectors);
296
297 /* qcow2-snapshot.c functions */
298 int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info);
299 int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id);
300 int qcow2_snapshot_delete(BlockDriverState *bs, const char *snapshot_id);
301 int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab);
302 int qcow2_snapshot_load_tmp(BlockDriverState *bs, const char *snapshot_name);
303
304 void qcow2_free_snapshots(BlockDriverState *bs);
305 int qcow2_read_snapshots(BlockDriverState *bs);
306
307 /* qcow2-cache.c functions */
308 Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables);
309 int qcow2_cache_destroy(BlockDriverState* bs, Qcow2Cache *c);
310
311 void qcow2_cache_entry_mark_dirty(Qcow2Cache *c, void *table);
312 int qcow2_cache_flush(BlockDriverState *bs, Qcow2Cache *c);
313 int qcow2_cache_set_dependency(BlockDriverState *bs, Qcow2Cache *c,
314 Qcow2Cache *dependency);
315 void qcow2_cache_depends_on_flush(Qcow2Cache *c);
316
317 int qcow2_cache_get(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
318 void **table);
319 int qcow2_cache_get_empty(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
320 void **table);
321 int qcow2_cache_put(BlockDriverState *bs, Qcow2Cache *c, void **table);
322
323 #endif