]>
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 | ||
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 | |
ce48f2f4 | 41 | #define QCOW_MAX_SNAPSHOTS 65536 |
f7d0fe02 | 42 | |
2b5d5953 KW |
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 | |
46 | ||
6a83f8b5 KW |
47 | /* 32 MB L1 table is enough for 2 PB images at 64k cluster size |
48 | * (128 GB for 512 byte clusters, 2 EB for 2 MB clusters) */ | |
49 | #define QCOW_MAX_L1_SIZE 0x2000000 | |
50 | ||
5dae6e30 KW |
51 | /* Allow for an average of 1k per snapshot table entry, should be plenty of |
52 | * space for snapshot names and IDs */ | |
53 | #define QCOW_MAX_SNAPSHOTS_SIZE (1024 * QCOW_MAX_SNAPSHOTS) | |
54 | ||
f7d0fe02 | 55 | /* indicate that the refcount of the referenced cluster is exactly one. */ |
127c84e1 | 56 | #define QCOW_OFLAG_COPIED (1ULL << 63) |
f7d0fe02 | 57 | /* indicate that the cluster is compressed (they never have the copied flag) */ |
127c84e1 | 58 | #define QCOW_OFLAG_COMPRESSED (1ULL << 62) |
6377af48 | 59 | /* The cluster reads as all zeros */ |
127c84e1 | 60 | #define QCOW_OFLAG_ZERO (1ULL << 0) |
f7d0fe02 KW |
61 | |
62 | #define REFCOUNT_SHIFT 1 /* refcount size is 2 bytes */ | |
63 | ||
64 | #define MIN_CLUSTER_BITS 9 | |
80ee15a6 | 65 | #define MAX_CLUSTER_BITS 21 |
f7d0fe02 | 66 | |
440ba08a | 67 | #define MIN_L2_CACHE_SIZE 1 /* cluster */ |
f7d0fe02 | 68 | |
29c1a730 | 69 | /* Must be at least 4 to cover all cases of refcount table growth */ |
440ba08a HR |
70 | #define MIN_REFCOUNT_CACHE_SIZE 4 /* clusters */ |
71 | ||
72 | #define DEFAULT_L2_CACHE_BYTE_SIZE 1048576 /* bytes */ | |
73 | ||
74 | /* The refblock cache needs only a fourth of the L2 cache size to cover as many | |
75 | * clusters */ | |
76 | #define DEFAULT_L2_REFCOUNT_SIZE_RATIO 4 | |
29c1a730 | 77 | |
99cce9fa KW |
78 | #define DEFAULT_CLUSTER_SIZE 65536 |
79 | ||
acdfb480 | 80 | |
64aa99d3 KW |
81 | #define QCOW2_OPT_LAZY_REFCOUNTS "lazy-refcounts" |
82 | #define QCOW2_OPT_DISCARD_REQUEST "pass-discard-request" | |
83 | #define QCOW2_OPT_DISCARD_SNAPSHOT "pass-discard-snapshot" | |
84 | #define QCOW2_OPT_DISCARD_OTHER "pass-discard-other" | |
05de7e86 HR |
85 | #define QCOW2_OPT_OVERLAP "overlap-check" |
86 | #define QCOW2_OPT_OVERLAP_MAIN_HEADER "overlap-check.main-header" | |
87 | #define QCOW2_OPT_OVERLAP_ACTIVE_L1 "overlap-check.active-l1" | |
88 | #define QCOW2_OPT_OVERLAP_ACTIVE_L2 "overlap-check.active-l2" | |
89 | #define QCOW2_OPT_OVERLAP_REFCOUNT_TABLE "overlap-check.refcount-table" | |
90 | #define QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK "overlap-check.refcount-block" | |
91 | #define QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE "overlap-check.snapshot-table" | |
92 | #define QCOW2_OPT_OVERLAP_INACTIVE_L1 "overlap-check.inactive-l1" | |
93 | #define QCOW2_OPT_OVERLAP_INACTIVE_L2 "overlap-check.inactive-l2" | |
6c1c8d5d HR |
94 | #define QCOW2_OPT_CACHE_SIZE "cache-size" |
95 | #define QCOW2_OPT_L2_CACHE_SIZE "l2-cache-size" | |
96 | #define QCOW2_OPT_REFCOUNT_CACHE_SIZE "refcount-cache-size" | |
acdfb480 | 97 | |
f7d0fe02 KW |
98 | typedef struct QCowHeader { |
99 | uint32_t magic; | |
100 | uint32_t version; | |
101 | uint64_t backing_file_offset; | |
102 | uint32_t backing_file_size; | |
103 | uint32_t cluster_bits; | |
104 | uint64_t size; /* in bytes */ | |
105 | uint32_t crypt_method; | |
106 | uint32_t l1_size; /* XXX: save number of clusters instead ? */ | |
107 | uint64_t l1_table_offset; | |
108 | uint64_t refcount_table_offset; | |
109 | uint32_t refcount_table_clusters; | |
110 | uint32_t nb_snapshots; | |
111 | uint64_t snapshots_offset; | |
6744cbab KW |
112 | |
113 | /* The following fields are only valid for version >= 3 */ | |
114 | uint64_t incompatible_features; | |
115 | uint64_t compatible_features; | |
116 | uint64_t autoclear_features; | |
117 | ||
118 | uint32_t refcount_order; | |
119 | uint32_t header_length; | |
c4217f64 | 120 | } QEMU_PACKED QCowHeader; |
f7d0fe02 | 121 | |
ce48f2f4 KW |
122 | typedef struct QEMU_PACKED QCowSnapshotHeader { |
123 | /* header is 8 byte aligned */ | |
124 | uint64_t l1_table_offset; | |
125 | ||
126 | uint32_t l1_size; | |
127 | uint16_t id_str_size; | |
128 | uint16_t name_size; | |
129 | ||
130 | uint32_t date_sec; | |
131 | uint32_t date_nsec; | |
132 | ||
133 | uint64_t vm_clock_nsec; | |
134 | ||
135 | uint32_t vm_state_size; | |
136 | uint32_t extra_data_size; /* for extension */ | |
137 | /* extra data follows */ | |
138 | /* id_str follows */ | |
139 | /* name follows */ | |
140 | } QCowSnapshotHeader; | |
141 | ||
142 | typedef struct QEMU_PACKED QCowSnapshotExtraData { | |
143 | uint64_t vm_state_size_large; | |
144 | uint64_t disk_size; | |
145 | } QCowSnapshotExtraData; | |
146 | ||
147 | ||
f7d0fe02 KW |
148 | typedef struct QCowSnapshot { |
149 | uint64_t l1_table_offset; | |
150 | uint32_t l1_size; | |
151 | char *id_str; | |
152 | char *name; | |
90b27759 | 153 | uint64_t disk_size; |
c2c9a466 | 154 | uint64_t vm_state_size; |
f7d0fe02 KW |
155 | uint32_t date_sec; |
156 | uint32_t date_nsec; | |
157 | uint64_t vm_clock_nsec; | |
158 | } QCowSnapshot; | |
159 | ||
49381094 KW |
160 | struct Qcow2Cache; |
161 | typedef struct Qcow2Cache Qcow2Cache; | |
162 | ||
75bab85c KW |
163 | typedef struct Qcow2UnknownHeaderExtension { |
164 | uint32_t magic; | |
165 | uint32_t len; | |
166 | QLIST_ENTRY(Qcow2UnknownHeaderExtension) next; | |
167 | uint8_t data[]; | |
168 | } Qcow2UnknownHeaderExtension; | |
169 | ||
cfcc4c62 KW |
170 | enum { |
171 | QCOW2_FEAT_TYPE_INCOMPATIBLE = 0, | |
172 | QCOW2_FEAT_TYPE_COMPATIBLE = 1, | |
173 | QCOW2_FEAT_TYPE_AUTOCLEAR = 2, | |
174 | }; | |
175 | ||
c61d0004 SH |
176 | /* Incompatible feature bits */ |
177 | enum { | |
178 | QCOW2_INCOMPAT_DIRTY_BITNR = 0, | |
69c98726 | 179 | QCOW2_INCOMPAT_CORRUPT_BITNR = 1, |
c61d0004 | 180 | QCOW2_INCOMPAT_DIRTY = 1 << QCOW2_INCOMPAT_DIRTY_BITNR, |
69c98726 | 181 | QCOW2_INCOMPAT_CORRUPT = 1 << QCOW2_INCOMPAT_CORRUPT_BITNR, |
c61d0004 | 182 | |
69c98726 HR |
183 | QCOW2_INCOMPAT_MASK = QCOW2_INCOMPAT_DIRTY |
184 | | QCOW2_INCOMPAT_CORRUPT, | |
c61d0004 SH |
185 | }; |
186 | ||
bfe8043e SH |
187 | /* Compatible feature bits */ |
188 | enum { | |
189 | QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR = 0, | |
190 | QCOW2_COMPAT_LAZY_REFCOUNTS = 1 << QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR, | |
191 | ||
192 | QCOW2_COMPAT_FEAT_MASK = QCOW2_COMPAT_LAZY_REFCOUNTS, | |
193 | }; | |
194 | ||
6cfcb9b8 KW |
195 | enum qcow2_discard_type { |
196 | QCOW2_DISCARD_NEVER = 0, | |
197 | QCOW2_DISCARD_ALWAYS, | |
198 | QCOW2_DISCARD_REQUEST, | |
199 | QCOW2_DISCARD_SNAPSHOT, | |
200 | QCOW2_DISCARD_OTHER, | |
201 | QCOW2_DISCARD_MAX | |
202 | }; | |
203 | ||
cfcc4c62 KW |
204 | typedef struct Qcow2Feature { |
205 | uint8_t type; | |
206 | uint8_t bit; | |
207 | char name[46]; | |
208 | } QEMU_PACKED Qcow2Feature; | |
209 | ||
0b919fae KW |
210 | typedef struct Qcow2DiscardRegion { |
211 | BlockDriverState *bs; | |
212 | uint64_t offset; | |
213 | uint64_t bytes; | |
214 | QTAILQ_ENTRY(Qcow2DiscardRegion) next; | |
215 | } Qcow2DiscardRegion; | |
216 | ||
f7d0fe02 | 217 | typedef struct BDRVQcowState { |
f7d0fe02 KW |
218 | int cluster_bits; |
219 | int cluster_size; | |
220 | int cluster_sectors; | |
221 | int l2_bits; | |
222 | int l2_size; | |
223 | int l1_size; | |
224 | int l1_vm_state_index; | |
225 | int csize_shift; | |
226 | int csize_mask; | |
227 | uint64_t cluster_offset_mask; | |
228 | uint64_t l1_table_offset; | |
229 | uint64_t *l1_table; | |
29c1a730 KW |
230 | |
231 | Qcow2Cache* l2_table_cache; | |
232 | Qcow2Cache* refcount_block_cache; | |
233 | ||
f7d0fe02 KW |
234 | uint8_t *cluster_cache; |
235 | uint8_t *cluster_data; | |
236 | uint64_t cluster_cache_offset; | |
72cf2d4f | 237 | QLIST_HEAD(QCowClusterAlloc, QCowL2Meta) cluster_allocs; |
f7d0fe02 KW |
238 | |
239 | uint64_t *refcount_table; | |
240 | uint64_t refcount_table_offset; | |
241 | uint32_t refcount_table_size; | |
bb572aef KW |
242 | uint64_t free_cluster_index; |
243 | uint64_t free_byte_offset; | |
f7d0fe02 | 244 | |
68d100e9 KW |
245 | CoMutex lock; |
246 | ||
f7d0fe02 KW |
247 | uint32_t crypt_method; /* current crypt method, 0 if no key yet */ |
248 | uint32_t crypt_method_header; | |
249 | AES_KEY aes_encrypt_key; | |
250 | AES_KEY aes_decrypt_key; | |
251 | uint64_t snapshots_offset; | |
252 | int snapshots_size; | |
ce48f2f4 | 253 | unsigned int nb_snapshots; |
f7d0fe02 | 254 | QCowSnapshot *snapshots; |
06d9260f AL |
255 | |
256 | int flags; | |
6744cbab | 257 | int qcow_version; |
74c4510a | 258 | bool use_lazy_refcounts; |
b6481f37 | 259 | int refcount_order; |
6744cbab | 260 | |
67af674e KW |
261 | bool discard_passthrough[QCOW2_DISCARD_MAX]; |
262 | ||
3e355390 HR |
263 | int overlap_check; /* bitmask of Qcow2MetadataOverlap values */ |
264 | ||
6744cbab KW |
265 | uint64_t incompatible_features; |
266 | uint64_t compatible_features; | |
267 | uint64_t autoclear_features; | |
268 | ||
269 | size_t unknown_header_fields_size; | |
270 | void* unknown_header_fields; | |
75bab85c | 271 | QLIST_HEAD(, Qcow2UnknownHeaderExtension) unknown_header_ext; |
0b919fae KW |
272 | QTAILQ_HEAD (, Qcow2DiscardRegion) discards; |
273 | bool cache_discards; | |
f7d0fe02 KW |
274 | } BDRVQcowState; |
275 | ||
276 | /* XXX: use std qcow open function ? */ | |
277 | typedef struct QCowCreateState { | |
278 | int cluster_size; | |
279 | int cluster_bits; | |
280 | uint16_t *refcount_block; | |
281 | uint64_t *refcount_table; | |
282 | int64_t l1_table_offset; | |
283 | int64_t refcount_table_offset; | |
284 | int64_t refcount_block_offset; | |
285 | } QCowCreateState; | |
286 | ||
f214978a KW |
287 | struct QCowAIOCB; |
288 | ||
593fb83c KW |
289 | typedef struct Qcow2COWRegion { |
290 | /** | |
291 | * Offset of the COW region in bytes from the start of the first cluster | |
292 | * touched by the request. | |
293 | */ | |
294 | uint64_t offset; | |
295 | ||
296 | /** Number of sectors to copy */ | |
297 | int nb_sectors; | |
298 | } Qcow2COWRegion; | |
299 | ||
f50f88b9 KW |
300 | /** |
301 | * Describes an in-flight (part of a) write request that writes to clusters | |
302 | * that are not referenced in their L2 table yet. | |
303 | */ | |
45aba42f KW |
304 | typedef struct QCowL2Meta |
305 | { | |
1d3afd64 | 306 | /** Guest offset of the first newly allocated cluster */ |
45aba42f | 307 | uint64_t offset; |
1d3afd64 | 308 | |
1d3afd64 | 309 | /** Host offset of the first newly allocated cluster */ |
250196f1 | 310 | uint64_t alloc_offset; |
1d3afd64 | 311 | |
1d3afd64 KW |
312 | /** |
313 | * Number of sectors from the start of the first allocated cluster to | |
314 | * the end of the (possibly shortened) request | |
315 | */ | |
45aba42f | 316 | int nb_available; |
1d3afd64 KW |
317 | |
318 | /** Number of newly allocated clusters */ | |
45aba42f | 319 | int nb_clusters; |
1d3afd64 KW |
320 | |
321 | /** | |
322 | * Requests that overlap with this allocation and wait to be restarted | |
323 | * when the allocating request has completed. | |
324 | */ | |
68d100e9 | 325 | CoQueue dependent_requests; |
f214978a | 326 | |
593fb83c KW |
327 | /** |
328 | * The COW Region between the start of the first allocated cluster and the | |
329 | * area the guest actually writes to. | |
330 | */ | |
331 | Qcow2COWRegion cow_start; | |
332 | ||
333 | /** | |
334 | * The COW Region between the area the guest actually writes to and the | |
335 | * end of the last allocated cluster. | |
336 | */ | |
337 | Qcow2COWRegion cow_end; | |
338 | ||
88c6588c KW |
339 | /** Pointer to next L2Meta of the same write request */ |
340 | struct QCowL2Meta *next; | |
341 | ||
72cf2d4f | 342 | QLIST_ENTRY(QCowL2Meta) next_in_flight; |
45aba42f KW |
343 | } QCowL2Meta; |
344 | ||
68d000a3 KW |
345 | enum { |
346 | QCOW2_CLUSTER_UNALLOCATED, | |
347 | QCOW2_CLUSTER_NORMAL, | |
348 | QCOW2_CLUSTER_COMPRESSED, | |
6377af48 | 349 | QCOW2_CLUSTER_ZERO |
68d000a3 KW |
350 | }; |
351 | ||
a40f1c2a HR |
352 | typedef enum QCow2MetadataOverlap { |
353 | QCOW2_OL_MAIN_HEADER_BITNR = 0, | |
354 | QCOW2_OL_ACTIVE_L1_BITNR = 1, | |
355 | QCOW2_OL_ACTIVE_L2_BITNR = 2, | |
356 | QCOW2_OL_REFCOUNT_TABLE_BITNR = 3, | |
357 | QCOW2_OL_REFCOUNT_BLOCK_BITNR = 4, | |
358 | QCOW2_OL_SNAPSHOT_TABLE_BITNR = 5, | |
359 | QCOW2_OL_INACTIVE_L1_BITNR = 6, | |
360 | QCOW2_OL_INACTIVE_L2_BITNR = 7, | |
361 | ||
362 | QCOW2_OL_MAX_BITNR = 8, | |
363 | ||
364 | QCOW2_OL_NONE = 0, | |
365 | QCOW2_OL_MAIN_HEADER = (1 << QCOW2_OL_MAIN_HEADER_BITNR), | |
366 | QCOW2_OL_ACTIVE_L1 = (1 << QCOW2_OL_ACTIVE_L1_BITNR), | |
367 | QCOW2_OL_ACTIVE_L2 = (1 << QCOW2_OL_ACTIVE_L2_BITNR), | |
368 | QCOW2_OL_REFCOUNT_TABLE = (1 << QCOW2_OL_REFCOUNT_TABLE_BITNR), | |
369 | QCOW2_OL_REFCOUNT_BLOCK = (1 << QCOW2_OL_REFCOUNT_BLOCK_BITNR), | |
370 | QCOW2_OL_SNAPSHOT_TABLE = (1 << QCOW2_OL_SNAPSHOT_TABLE_BITNR), | |
371 | QCOW2_OL_INACTIVE_L1 = (1 << QCOW2_OL_INACTIVE_L1_BITNR), | |
372 | /* NOTE: Checking overlaps with inactive L2 tables will result in bdrv | |
373 | * reads. */ | |
374 | QCOW2_OL_INACTIVE_L2 = (1 << QCOW2_OL_INACTIVE_L2_BITNR), | |
375 | } QCow2MetadataOverlap; | |
376 | ||
4a273c39 HR |
377 | /* Perform all overlap checks which can be done in constant time */ |
378 | #define QCOW2_OL_CONSTANT \ | |
379 | (QCOW2_OL_MAIN_HEADER | QCOW2_OL_ACTIVE_L1 | QCOW2_OL_REFCOUNT_TABLE | \ | |
380 | QCOW2_OL_SNAPSHOT_TABLE) | |
381 | ||
a40f1c2a HR |
382 | /* Perform all overlap checks which don't require disk access */ |
383 | #define QCOW2_OL_CACHED \ | |
4a273c39 HR |
384 | (QCOW2_OL_CONSTANT | QCOW2_OL_ACTIVE_L2 | QCOW2_OL_REFCOUNT_BLOCK | \ |
385 | QCOW2_OL_INACTIVE_L1) | |
386 | ||
387 | /* Perform all overlap checks */ | |
388 | #define QCOW2_OL_ALL \ | |
389 | (QCOW2_OL_CACHED | QCOW2_OL_INACTIVE_L2) | |
a40f1c2a | 390 | |
46bae927 HT |
391 | #define L1E_OFFSET_MASK 0x00fffffffffffe00ULL |
392 | #define L2E_OFFSET_MASK 0x00fffffffffffe00ULL | |
68d000a3 KW |
393 | #define L2E_COMPRESSED_OFFSET_SIZE_MASK 0x3fffffffffffffffULL |
394 | ||
46bae927 | 395 | #define REFT_OFFSET_MASK 0xfffffffffffffe00ULL |
76dc9e0c | 396 | |
3b8e2e26 KW |
397 | static inline int64_t start_of_cluster(BDRVQcowState *s, int64_t offset) |
398 | { | |
399 | return offset & ~(s->cluster_size - 1); | |
400 | } | |
401 | ||
c37f4cd7 KW |
402 | static inline int64_t offset_into_cluster(BDRVQcowState *s, int64_t offset) |
403 | { | |
404 | return offset & (s->cluster_size - 1); | |
405 | } | |
406 | ||
45aba42f | 407 | static inline int size_to_clusters(BDRVQcowState *s, int64_t size) |
f7d0fe02 KW |
408 | { |
409 | return (size + (s->cluster_size - 1)) >> s->cluster_bits; | |
410 | } | |
411 | ||
2cf7cfa1 | 412 | static inline int64_t size_to_l1(BDRVQcowState *s, int64_t size) |
419b19d9 SH |
413 | { |
414 | int shift = s->cluster_bits + s->l2_bits; | |
415 | return (size + (1ULL << shift) - 1) >> shift; | |
416 | } | |
417 | ||
17a71e58 KW |
418 | static inline int offset_to_l2_index(BDRVQcowState *s, int64_t offset) |
419 | { | |
420 | return (offset >> s->cluster_bits) & (s->l2_size - 1); | |
421 | } | |
422 | ||
c142442b KW |
423 | static inline int64_t align_offset(int64_t offset, int n) |
424 | { | |
425 | offset = (offset + n - 1) & ~(n - 1); | |
426 | return offset; | |
427 | } | |
428 | ||
1ebf561c KW |
429 | static inline int64_t qcow2_vm_state_offset(BDRVQcowState *s) |
430 | { | |
431 | return (int64_t)s->l1_vm_state_index << (s->cluster_bits + s->l2_bits); | |
432 | } | |
433 | ||
2b5d5953 KW |
434 | static inline uint64_t qcow2_max_refcount_clusters(BDRVQcowState *s) |
435 | { | |
436 | return QCOW_MAX_REFTABLE_SIZE >> s->cluster_bits; | |
437 | } | |
438 | ||
68d000a3 KW |
439 | static inline int qcow2_get_cluster_type(uint64_t l2_entry) |
440 | { | |
441 | if (l2_entry & QCOW_OFLAG_COMPRESSED) { | |
442 | return QCOW2_CLUSTER_COMPRESSED; | |
6377af48 KW |
443 | } else if (l2_entry & QCOW_OFLAG_ZERO) { |
444 | return QCOW2_CLUSTER_ZERO; | |
68d000a3 KW |
445 | } else if (!(l2_entry & L2E_OFFSET_MASK)) { |
446 | return QCOW2_CLUSTER_UNALLOCATED; | |
447 | } else { | |
448 | return QCOW2_CLUSTER_NORMAL; | |
449 | } | |
450 | } | |
451 | ||
bfe8043e SH |
452 | /* Check whether refcounts are eager or lazy */ |
453 | static inline bool qcow2_need_accurate_refcounts(BDRVQcowState *s) | |
454 | { | |
455 | return !(s->incompatible_features & QCOW2_INCOMPAT_DIRTY); | |
456 | } | |
c142442b | 457 | |
65eb2e35 KW |
458 | static inline uint64_t l2meta_cow_start(QCowL2Meta *m) |
459 | { | |
460 | return m->offset + m->cow_start.offset; | |
461 | } | |
462 | ||
463 | static inline uint64_t l2meta_cow_end(QCowL2Meta *m) | |
464 | { | |
465 | return m->offset + m->cow_end.offset | |
466 | + (m->cow_end.nb_sectors << BDRV_SECTOR_BITS); | |
467 | } | |
468 | ||
f7d0fe02 KW |
469 | // FIXME Need qcow2_ prefix to global functions |
470 | ||
471 | /* qcow2.c functions */ | |
bd28f835 KW |
472 | int qcow2_backing_read1(BlockDriverState *bs, QEMUIOVector *qiov, |
473 | int64_t sector_num, int nb_sectors); | |
280d3735 KW |
474 | |
475 | int qcow2_mark_dirty(BlockDriverState *bs); | |
69c98726 HR |
476 | int qcow2_mark_corrupt(BlockDriverState *bs); |
477 | int qcow2_mark_consistent(BlockDriverState *bs); | |
e24e49e6 | 478 | int qcow2_update_header(BlockDriverState *bs); |
f7d0fe02 KW |
479 | |
480 | /* qcow2-refcount.c functions */ | |
ed6ccf0f KW |
481 | int qcow2_refcount_init(BlockDriverState *bs); |
482 | void qcow2_refcount_close(BlockDriverState *bs); | |
f7d0fe02 | 483 | |
32b6444d HR |
484 | int qcow2_update_cluster_refcount(BlockDriverState *bs, int64_t cluster_index, |
485 | int addend, enum qcow2_discard_type type); | |
486 | ||
bb572aef | 487 | int64_t qcow2_alloc_clusters(BlockDriverState *bs, uint64_t size); |
256900b1 KW |
488 | int qcow2_alloc_clusters_at(BlockDriverState *bs, uint64_t offset, |
489 | int nb_clusters); | |
ed6ccf0f KW |
490 | int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size); |
491 | void qcow2_free_clusters(BlockDriverState *bs, | |
6cfcb9b8 KW |
492 | int64_t offset, int64_t size, |
493 | enum qcow2_discard_type type); | |
494 | void qcow2_free_any_clusters(BlockDriverState *bs, uint64_t l2_entry, | |
495 | int nb_clusters, enum qcow2_discard_type type); | |
f7d0fe02 | 496 | |
ed6ccf0f KW |
497 | int qcow2_update_snapshot_refcount(BlockDriverState *bs, |
498 | int64_t l1_table_offset, int l1_size, int addend); | |
f7d0fe02 | 499 | |
166acf54 KW |
500 | int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res, |
501 | BdrvCheckMode fix); | |
f7d0fe02 | 502 | |
0b919fae KW |
503 | void qcow2_process_discards(BlockDriverState *bs, int ret); |
504 | ||
231bb267 | 505 | int qcow2_check_metadata_overlap(BlockDriverState *bs, int ign, int64_t offset, |
a40f1c2a | 506 | int64_t size); |
231bb267 | 507 | int qcow2_pre_write_overlap_check(BlockDriverState *bs, int ign, int64_t offset, |
a40f1c2a HR |
508 | int64_t size); |
509 | ||
45aba42f | 510 | /* qcow2-cluster.c functions */ |
2cf7cfa1 KW |
511 | int qcow2_grow_l1_table(BlockDriverState *bs, uint64_t min_size, |
512 | bool exact_size); | |
e23e400e | 513 | int qcow2_write_l1_entry(BlockDriverState *bs, int l1_index); |
ed6ccf0f | 514 | void qcow2_l2_cache_reset(BlockDriverState *bs); |
66f82cee | 515 | int qcow2_decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset); |
ed6ccf0f | 516 | void qcow2_encrypt_sectors(BDRVQcowState *s, int64_t sector_num, |
45aba42f KW |
517 | uint8_t *out_buf, const uint8_t *in_buf, |
518 | int nb_sectors, int enc, | |
519 | const AES_KEY *key); | |
520 | ||
1c46efaa KW |
521 | int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset, |
522 | int *num, uint64_t *cluster_offset); | |
f4f0d391 | 523 | int qcow2_alloc_cluster_offset(BlockDriverState *bs, uint64_t offset, |
16f0587e | 524 | int *num, uint64_t *host_offset, QCowL2Meta **m); |
ed6ccf0f | 525 | uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs, |
45aba42f KW |
526 | uint64_t offset, |
527 | int compressed_size); | |
528 | ||
148da7ea | 529 | int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m); |
5ea929e3 | 530 | int qcow2_discard_clusters(BlockDriverState *bs, uint64_t offset, |
670df5e3 | 531 | int nb_sectors, enum qcow2_discard_type type); |
621f0589 | 532 | int qcow2_zero_clusters(BlockDriverState *bs, uint64_t offset, int nb_sectors); |
45aba42f | 533 | |
32b6444d HR |
534 | int qcow2_expand_zero_clusters(BlockDriverState *bs); |
535 | ||
c142442b | 536 | /* qcow2-snapshot.c functions */ |
ed6ccf0f KW |
537 | int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info); |
538 | int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id); | |
a89d89d3 WX |
539 | int qcow2_snapshot_delete(BlockDriverState *bs, |
540 | const char *snapshot_id, | |
541 | const char *name, | |
542 | Error **errp); | |
ed6ccf0f | 543 | int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab); |
7b4c4781 WX |
544 | int qcow2_snapshot_load_tmp(BlockDriverState *bs, |
545 | const char *snapshot_id, | |
546 | const char *name, | |
547 | Error **errp); | |
c142442b | 548 | |
ed6ccf0f KW |
549 | void qcow2_free_snapshots(BlockDriverState *bs); |
550 | int qcow2_read_snapshots(BlockDriverState *bs); | |
c142442b | 551 | |
49381094 | 552 | /* qcow2-cache.c functions */ |
6af4e9ea | 553 | Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables); |
49381094 KW |
554 | int qcow2_cache_destroy(BlockDriverState* bs, Qcow2Cache *c); |
555 | ||
556 | void qcow2_cache_entry_mark_dirty(Qcow2Cache *c, void *table); | |
557 | int qcow2_cache_flush(BlockDriverState *bs, Qcow2Cache *c); | |
558 | int qcow2_cache_set_dependency(BlockDriverState *bs, Qcow2Cache *c, | |
559 | Qcow2Cache *dependency); | |
3de0a294 | 560 | void qcow2_cache_depends_on_flush(Qcow2Cache *c); |
49381094 | 561 | |
e7108fea HR |
562 | int qcow2_cache_empty(BlockDriverState *bs, Qcow2Cache *c); |
563 | ||
49381094 KW |
564 | int qcow2_cache_get(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset, |
565 | void **table); | |
566 | int qcow2_cache_get_empty(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset, | |
567 | void **table); | |
568 | int qcow2_cache_put(BlockDriverState *bs, Qcow2Cache *c, void **table); | |
569 | ||
f7d0fe02 | 570 | #endif |