]>
Commit | Line | Data |
---|---|---|
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 | ||
28 | #include "aes.h" | |
68d100e9 | 29 | #include "qemu-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) | |
46 | ||
47 | #define REFCOUNT_SHIFT 1 /* refcount size is 2 bytes */ | |
48 | ||
49 | #define MIN_CLUSTER_BITS 9 | |
80ee15a6 | 50 | #define MAX_CLUSTER_BITS 21 |
f7d0fe02 KW |
51 | |
52 | #define L2_CACHE_SIZE 16 | |
53 | ||
29c1a730 KW |
54 | /* Must be at least 4 to cover all cases of refcount table growth */ |
55 | #define REFCOUNT_CACHE_SIZE 4 | |
56 | ||
99cce9fa KW |
57 | #define DEFAULT_CLUSTER_SIZE 65536 |
58 | ||
f7d0fe02 KW |
59 | typedef struct QCowHeader { |
60 | uint32_t magic; | |
61 | uint32_t version; | |
62 | uint64_t backing_file_offset; | |
63 | uint32_t backing_file_size; | |
64 | uint32_t cluster_bits; | |
65 | uint64_t size; /* in bytes */ | |
66 | uint32_t crypt_method; | |
67 | uint32_t l1_size; /* XXX: save number of clusters instead ? */ | |
68 | uint64_t l1_table_offset; | |
69 | uint64_t refcount_table_offset; | |
70 | uint32_t refcount_table_clusters; | |
71 | uint32_t nb_snapshots; | |
72 | uint64_t snapshots_offset; | |
6744cbab KW |
73 | |
74 | /* The following fields are only valid for version >= 3 */ | |
75 | uint64_t incompatible_features; | |
76 | uint64_t compatible_features; | |
77 | uint64_t autoclear_features; | |
78 | ||
79 | uint32_t refcount_order; | |
80 | uint32_t header_length; | |
f7d0fe02 KW |
81 | } QCowHeader; |
82 | ||
83 | typedef struct QCowSnapshot { | |
84 | uint64_t l1_table_offset; | |
85 | uint32_t l1_size; | |
86 | char *id_str; | |
87 | char *name; | |
90b27759 | 88 | uint64_t disk_size; |
c2c9a466 | 89 | uint64_t vm_state_size; |
f7d0fe02 KW |
90 | uint32_t date_sec; |
91 | uint32_t date_nsec; | |
92 | uint64_t vm_clock_nsec; | |
93 | } QCowSnapshot; | |
94 | ||
49381094 KW |
95 | struct Qcow2Cache; |
96 | typedef struct Qcow2Cache Qcow2Cache; | |
97 | ||
75bab85c KW |
98 | typedef struct Qcow2UnknownHeaderExtension { |
99 | uint32_t magic; | |
100 | uint32_t len; | |
101 | QLIST_ENTRY(Qcow2UnknownHeaderExtension) next; | |
102 | uint8_t data[]; | |
103 | } Qcow2UnknownHeaderExtension; | |
104 | ||
f7d0fe02 | 105 | typedef struct BDRVQcowState { |
f7d0fe02 KW |
106 | int cluster_bits; |
107 | int cluster_size; | |
108 | int cluster_sectors; | |
109 | int l2_bits; | |
110 | int l2_size; | |
111 | int l1_size; | |
112 | int l1_vm_state_index; | |
113 | int csize_shift; | |
114 | int csize_mask; | |
115 | uint64_t cluster_offset_mask; | |
116 | uint64_t l1_table_offset; | |
117 | uint64_t *l1_table; | |
29c1a730 KW |
118 | |
119 | Qcow2Cache* l2_table_cache; | |
120 | Qcow2Cache* refcount_block_cache; | |
121 | ||
f7d0fe02 KW |
122 | uint8_t *cluster_cache; |
123 | uint8_t *cluster_data; | |
124 | uint64_t cluster_cache_offset; | |
72cf2d4f | 125 | QLIST_HEAD(QCowClusterAlloc, QCowL2Meta) cluster_allocs; |
f7d0fe02 KW |
126 | |
127 | uint64_t *refcount_table; | |
128 | uint64_t refcount_table_offset; | |
129 | uint32_t refcount_table_size; | |
f7d0fe02 KW |
130 | int64_t free_cluster_index; |
131 | int64_t free_byte_offset; | |
132 | ||
68d100e9 KW |
133 | CoMutex lock; |
134 | ||
f7d0fe02 KW |
135 | uint32_t crypt_method; /* current crypt method, 0 if no key yet */ |
136 | uint32_t crypt_method_header; | |
137 | AES_KEY aes_encrypt_key; | |
138 | AES_KEY aes_decrypt_key; | |
139 | uint64_t snapshots_offset; | |
140 | int snapshots_size; | |
141 | int nb_snapshots; | |
142 | QCowSnapshot *snapshots; | |
06d9260f AL |
143 | |
144 | int flags; | |
6744cbab KW |
145 | int qcow_version; |
146 | ||
147 | uint64_t incompatible_features; | |
148 | uint64_t compatible_features; | |
149 | uint64_t autoclear_features; | |
150 | ||
151 | size_t unknown_header_fields_size; | |
152 | void* unknown_header_fields; | |
75bab85c | 153 | QLIST_HEAD(, Qcow2UnknownHeaderExtension) unknown_header_ext; |
f7d0fe02 KW |
154 | } BDRVQcowState; |
155 | ||
156 | /* XXX: use std qcow open function ? */ | |
157 | typedef struct QCowCreateState { | |
158 | int cluster_size; | |
159 | int cluster_bits; | |
160 | uint16_t *refcount_block; | |
161 | uint64_t *refcount_table; | |
162 | int64_t l1_table_offset; | |
163 | int64_t refcount_table_offset; | |
164 | int64_t refcount_block_offset; | |
165 | } QCowCreateState; | |
166 | ||
f214978a KW |
167 | struct QCowAIOCB; |
168 | ||
45aba42f KW |
169 | /* XXX This could be private for qcow2-cluster.c */ |
170 | typedef struct QCowL2Meta | |
171 | { | |
172 | uint64_t offset; | |
148da7ea | 173 | uint64_t cluster_offset; |
250196f1 | 174 | uint64_t alloc_offset; |
45aba42f KW |
175 | int n_start; |
176 | int nb_available; | |
177 | int nb_clusters; | |
68d100e9 | 178 | CoQueue dependent_requests; |
f214978a | 179 | |
72cf2d4f | 180 | QLIST_ENTRY(QCowL2Meta) next_in_flight; |
45aba42f KW |
181 | } QCowL2Meta; |
182 | ||
68d000a3 KW |
183 | enum { |
184 | QCOW2_CLUSTER_UNALLOCATED, | |
185 | QCOW2_CLUSTER_NORMAL, | |
186 | QCOW2_CLUSTER_COMPRESSED, | |
187 | }; | |
188 | ||
189 | #define L1E_OFFSET_MASK 0x00ffffffffffff00ULL | |
190 | #define L2E_OFFSET_MASK 0x00ffffffffffff00ULL | |
191 | #define L2E_COMPRESSED_OFFSET_SIZE_MASK 0x3fffffffffffffffULL | |
192 | ||
76dc9e0c KW |
193 | #define REFT_OFFSET_MASK 0xffffffffffffff00ULL |
194 | ||
45aba42f | 195 | static inline int size_to_clusters(BDRVQcowState *s, int64_t size) |
f7d0fe02 KW |
196 | { |
197 | return (size + (s->cluster_size - 1)) >> s->cluster_bits; | |
198 | } | |
199 | ||
419b19d9 SH |
200 | static inline int size_to_l1(BDRVQcowState *s, int64_t size) |
201 | { | |
202 | int shift = s->cluster_bits + s->l2_bits; | |
203 | return (size + (1ULL << shift) - 1) >> shift; | |
204 | } | |
205 | ||
c142442b KW |
206 | static inline int64_t align_offset(int64_t offset, int n) |
207 | { | |
208 | offset = (offset + n - 1) & ~(n - 1); | |
209 | return offset; | |
210 | } | |
211 | ||
68d000a3 KW |
212 | static inline int qcow2_get_cluster_type(uint64_t l2_entry) |
213 | { | |
214 | if (l2_entry & QCOW_OFLAG_COMPRESSED) { | |
215 | return QCOW2_CLUSTER_COMPRESSED; | |
216 | } else if (!(l2_entry & L2E_OFFSET_MASK)) { | |
217 | return QCOW2_CLUSTER_UNALLOCATED; | |
218 | } else { | |
219 | return QCOW2_CLUSTER_NORMAL; | |
220 | } | |
221 | } | |
222 | ||
c142442b | 223 | |
f7d0fe02 KW |
224 | // FIXME Need qcow2_ prefix to global functions |
225 | ||
226 | /* qcow2.c functions */ | |
bd28f835 KW |
227 | int qcow2_backing_read1(BlockDriverState *bs, QEMUIOVector *qiov, |
228 | int64_t sector_num, int nb_sectors); | |
e24e49e6 | 229 | int qcow2_update_header(BlockDriverState *bs); |
f7d0fe02 KW |
230 | |
231 | /* qcow2-refcount.c functions */ | |
ed6ccf0f KW |
232 | int qcow2_refcount_init(BlockDriverState *bs); |
233 | void qcow2_refcount_close(BlockDriverState *bs); | |
f7d0fe02 | 234 | |
ed6ccf0f | 235 | int64_t qcow2_alloc_clusters(BlockDriverState *bs, int64_t size); |
256900b1 KW |
236 | int qcow2_alloc_clusters_at(BlockDriverState *bs, uint64_t offset, |
237 | int nb_clusters); | |
ed6ccf0f KW |
238 | int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size); |
239 | void qcow2_free_clusters(BlockDriverState *bs, | |
45aba42f | 240 | int64_t offset, int64_t size); |
ed6ccf0f | 241 | void qcow2_free_any_clusters(BlockDriverState *bs, |
45aba42f | 242 | uint64_t cluster_offset, int nb_clusters); |
f7d0fe02 | 243 | |
ed6ccf0f KW |
244 | int qcow2_update_snapshot_refcount(BlockDriverState *bs, |
245 | int64_t l1_table_offset, int l1_size, int addend); | |
f7d0fe02 | 246 | |
9ac228e0 | 247 | int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res); |
f7d0fe02 | 248 | |
45aba42f | 249 | /* qcow2-cluster.c functions */ |
72893756 | 250 | int qcow2_grow_l1_table(BlockDriverState *bs, int min_size, bool exact_size); |
ed6ccf0f | 251 | void qcow2_l2_cache_reset(BlockDriverState *bs); |
66f82cee | 252 | int qcow2_decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset); |
ed6ccf0f | 253 | void qcow2_encrypt_sectors(BDRVQcowState *s, int64_t sector_num, |
45aba42f KW |
254 | uint8_t *out_buf, const uint8_t *in_buf, |
255 | int nb_sectors, int enc, | |
256 | const AES_KEY *key); | |
257 | ||
1c46efaa KW |
258 | int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset, |
259 | int *num, uint64_t *cluster_offset); | |
f4f0d391 KW |
260 | int qcow2_alloc_cluster_offset(BlockDriverState *bs, uint64_t offset, |
261 | int n_start, int n_end, int *num, QCowL2Meta *m); | |
ed6ccf0f | 262 | uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs, |
45aba42f KW |
263 | uint64_t offset, |
264 | int compressed_size); | |
265 | ||
148da7ea | 266 | int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m); |
5ea929e3 KW |
267 | int qcow2_discard_clusters(BlockDriverState *bs, uint64_t offset, |
268 | int nb_sectors); | |
45aba42f | 269 | |
c142442b | 270 | /* qcow2-snapshot.c functions */ |
ed6ccf0f KW |
271 | int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info); |
272 | int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id); | |
273 | int qcow2_snapshot_delete(BlockDriverState *bs, const char *snapshot_id); | |
274 | int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab); | |
51ef6727 | 275 | int qcow2_snapshot_load_tmp(BlockDriverState *bs, const char *snapshot_name); |
c142442b | 276 | |
ed6ccf0f KW |
277 | void qcow2_free_snapshots(BlockDriverState *bs); |
278 | int qcow2_read_snapshots(BlockDriverState *bs); | |
c142442b | 279 | |
49381094 KW |
280 | /* qcow2-cache.c functions */ |
281 | Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables, | |
282 | bool writethrough); | |
283 | int qcow2_cache_destroy(BlockDriverState* bs, Qcow2Cache *c); | |
93913dfd KW |
284 | bool qcow2_cache_set_writethrough(BlockDriverState *bs, Qcow2Cache *c, |
285 | bool enable); | |
49381094 KW |
286 | |
287 | void qcow2_cache_entry_mark_dirty(Qcow2Cache *c, void *table); | |
288 | int qcow2_cache_flush(BlockDriverState *bs, Qcow2Cache *c); | |
289 | int qcow2_cache_set_dependency(BlockDriverState *bs, Qcow2Cache *c, | |
290 | Qcow2Cache *dependency); | |
3de0a294 | 291 | void qcow2_cache_depends_on_flush(Qcow2Cache *c); |
49381094 KW |
292 | |
293 | int qcow2_cache_get(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset, | |
294 | void **table); | |
295 | int qcow2_cache_get_empty(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset, | |
296 | void **table); | |
297 | int qcow2_cache_put(BlockDriverState *bs, Qcow2Cache *c, void **table); | |
298 | ||
f7d0fe02 | 299 | #endif |