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