]>
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 | ||
b25b387f | 28 | #include "crypto/block.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 | |
4652b8f3 | 39 | #define QCOW_CRYPT_LUKS 2 |
f7d0fe02 KW |
40 | |
41 | #define QCOW_MAX_CRYPT_CLUSTERS 32 | |
ce48f2f4 | 42 | #define QCOW_MAX_SNAPSHOTS 65536 |
f7d0fe02 | 43 | |
2b5d5953 KW |
44 | /* 8 MB refcount table is enough for 2 PB images at 64k cluster size |
45 | * (128 GB for 512 byte clusters, 2 EB for 2 MB clusters) */ | |
46 | #define QCOW_MAX_REFTABLE_SIZE 0x800000 | |
47 | ||
6a83f8b5 KW |
48 | /* 32 MB L1 table is enough for 2 PB images at 64k cluster size |
49 | * (128 GB for 512 byte clusters, 2 EB for 2 MB clusters) */ | |
50 | #define QCOW_MAX_L1_SIZE 0x2000000 | |
51 | ||
5dae6e30 KW |
52 | /* Allow for an average of 1k per snapshot table entry, should be plenty of |
53 | * space for snapshot names and IDs */ | |
54 | #define QCOW_MAX_SNAPSHOTS_SIZE (1024 * QCOW_MAX_SNAPSHOTS) | |
55 | ||
88ddffae VSO |
56 | /* Bitmap header extension constraints */ |
57 | #define QCOW2_MAX_BITMAPS 65535 | |
58 | #define QCOW2_MAX_BITMAP_DIRECTORY_SIZE (1024 * QCOW2_MAX_BITMAPS) | |
59 | ||
f7d0fe02 | 60 | /* indicate that the refcount of the referenced cluster is exactly one. */ |
127c84e1 | 61 | #define QCOW_OFLAG_COPIED (1ULL << 63) |
f7d0fe02 | 62 | /* indicate that the cluster is compressed (they never have the copied flag) */ |
127c84e1 | 63 | #define QCOW_OFLAG_COMPRESSED (1ULL << 62) |
6377af48 | 64 | /* The cluster reads as all zeros */ |
127c84e1 | 65 | #define QCOW_OFLAG_ZERO (1ULL << 0) |
f7d0fe02 | 66 | |
f7d0fe02 | 67 | #define MIN_CLUSTER_BITS 9 |
80ee15a6 | 68 | #define MAX_CLUSTER_BITS 21 |
f7d0fe02 | 69 | |
57e21669 | 70 | /* Must be at least 2 to cover COW */ |
1221fe6f | 71 | #define MIN_L2_CACHE_SIZE 2 /* cache entries */ |
f7d0fe02 | 72 | |
29c1a730 | 73 | /* Must be at least 4 to cover all cases of refcount table growth */ |
440ba08a HR |
74 | #define MIN_REFCOUNT_CACHE_SIZE 4 /* clusters */ |
75 | ||
bc85ef26 HR |
76 | /* Whichever is more */ |
77 | #define DEFAULT_L2_CACHE_CLUSTERS 8 /* clusters */ | |
440ba08a HR |
78 | #define DEFAULT_L2_CACHE_BYTE_SIZE 1048576 /* bytes */ |
79 | ||
99cce9fa KW |
80 | #define DEFAULT_CLUSTER_SIZE 65536 |
81 | ||
acdfb480 | 82 | |
64aa99d3 KW |
83 | #define QCOW2_OPT_LAZY_REFCOUNTS "lazy-refcounts" |
84 | #define QCOW2_OPT_DISCARD_REQUEST "pass-discard-request" | |
85 | #define QCOW2_OPT_DISCARD_SNAPSHOT "pass-discard-snapshot" | |
86 | #define QCOW2_OPT_DISCARD_OTHER "pass-discard-other" | |
05de7e86 | 87 | #define QCOW2_OPT_OVERLAP "overlap-check" |
ee42b5ce | 88 | #define QCOW2_OPT_OVERLAP_TEMPLATE "overlap-check.template" |
05de7e86 HR |
89 | #define QCOW2_OPT_OVERLAP_MAIN_HEADER "overlap-check.main-header" |
90 | #define QCOW2_OPT_OVERLAP_ACTIVE_L1 "overlap-check.active-l1" | |
91 | #define QCOW2_OPT_OVERLAP_ACTIVE_L2 "overlap-check.active-l2" | |
92 | #define QCOW2_OPT_OVERLAP_REFCOUNT_TABLE "overlap-check.refcount-table" | |
93 | #define QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK "overlap-check.refcount-block" | |
94 | #define QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE "overlap-check.snapshot-table" | |
95 | #define QCOW2_OPT_OVERLAP_INACTIVE_L1 "overlap-check.inactive-l1" | |
96 | #define QCOW2_OPT_OVERLAP_INACTIVE_L2 "overlap-check.inactive-l2" | |
0e4e4318 | 97 | #define QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY "overlap-check.bitmap-directory" |
6c1c8d5d HR |
98 | #define QCOW2_OPT_CACHE_SIZE "cache-size" |
99 | #define QCOW2_OPT_L2_CACHE_SIZE "l2-cache-size" | |
1221fe6f | 100 | #define QCOW2_OPT_L2_CACHE_ENTRY_SIZE "l2-cache-entry-size" |
6c1c8d5d | 101 | #define QCOW2_OPT_REFCOUNT_CACHE_SIZE "refcount-cache-size" |
279621c0 | 102 | #define QCOW2_OPT_CACHE_CLEAN_INTERVAL "cache-clean-interval" |
acdfb480 | 103 | |
f7d0fe02 KW |
104 | typedef struct QCowHeader { |
105 | uint32_t magic; | |
106 | uint32_t version; | |
107 | uint64_t backing_file_offset; | |
108 | uint32_t backing_file_size; | |
109 | uint32_t cluster_bits; | |
110 | uint64_t size; /* in bytes */ | |
111 | uint32_t crypt_method; | |
112 | uint32_t l1_size; /* XXX: save number of clusters instead ? */ | |
113 | uint64_t l1_table_offset; | |
114 | uint64_t refcount_table_offset; | |
115 | uint32_t refcount_table_clusters; | |
116 | uint32_t nb_snapshots; | |
117 | uint64_t snapshots_offset; | |
6744cbab KW |
118 | |
119 | /* The following fields are only valid for version >= 3 */ | |
120 | uint64_t incompatible_features; | |
121 | uint64_t compatible_features; | |
122 | uint64_t autoclear_features; | |
123 | ||
124 | uint32_t refcount_order; | |
125 | uint32_t header_length; | |
c4217f64 | 126 | } QEMU_PACKED QCowHeader; |
f7d0fe02 | 127 | |
ce48f2f4 KW |
128 | typedef struct QEMU_PACKED QCowSnapshotHeader { |
129 | /* header is 8 byte aligned */ | |
130 | uint64_t l1_table_offset; | |
131 | ||
132 | uint32_t l1_size; | |
133 | uint16_t id_str_size; | |
134 | uint16_t name_size; | |
135 | ||
136 | uint32_t date_sec; | |
137 | uint32_t date_nsec; | |
138 | ||
139 | uint64_t vm_clock_nsec; | |
140 | ||
141 | uint32_t vm_state_size; | |
142 | uint32_t extra_data_size; /* for extension */ | |
143 | /* extra data follows */ | |
144 | /* id_str follows */ | |
145 | /* name follows */ | |
146 | } QCowSnapshotHeader; | |
147 | ||
148 | typedef struct QEMU_PACKED QCowSnapshotExtraData { | |
149 | uint64_t vm_state_size_large; | |
150 | uint64_t disk_size; | |
151 | } QCowSnapshotExtraData; | |
152 | ||
153 | ||
f7d0fe02 KW |
154 | typedef struct QCowSnapshot { |
155 | uint64_t l1_table_offset; | |
156 | uint32_t l1_size; | |
157 | char *id_str; | |
158 | char *name; | |
90b27759 | 159 | uint64_t disk_size; |
c2c9a466 | 160 | uint64_t vm_state_size; |
f7d0fe02 KW |
161 | uint32_t date_sec; |
162 | uint32_t date_nsec; | |
163 | uint64_t vm_clock_nsec; | |
164 | } QCowSnapshot; | |
165 | ||
49381094 KW |
166 | struct Qcow2Cache; |
167 | typedef struct Qcow2Cache Qcow2Cache; | |
168 | ||
4652b8f3 DB |
169 | typedef struct Qcow2CryptoHeaderExtension { |
170 | uint64_t offset; | |
171 | uint64_t length; | |
172 | } QEMU_PACKED Qcow2CryptoHeaderExtension; | |
173 | ||
75bab85c KW |
174 | typedef struct Qcow2UnknownHeaderExtension { |
175 | uint32_t magic; | |
176 | uint32_t len; | |
177 | QLIST_ENTRY(Qcow2UnknownHeaderExtension) next; | |
178 | uint8_t data[]; | |
179 | } Qcow2UnknownHeaderExtension; | |
180 | ||
cfcc4c62 KW |
181 | enum { |
182 | QCOW2_FEAT_TYPE_INCOMPATIBLE = 0, | |
183 | QCOW2_FEAT_TYPE_COMPATIBLE = 1, | |
184 | QCOW2_FEAT_TYPE_AUTOCLEAR = 2, | |
185 | }; | |
186 | ||
c61d0004 SH |
187 | /* Incompatible feature bits */ |
188 | enum { | |
189 | QCOW2_INCOMPAT_DIRTY_BITNR = 0, | |
69c98726 | 190 | QCOW2_INCOMPAT_CORRUPT_BITNR = 1, |
c61d0004 | 191 | QCOW2_INCOMPAT_DIRTY = 1 << QCOW2_INCOMPAT_DIRTY_BITNR, |
69c98726 | 192 | QCOW2_INCOMPAT_CORRUPT = 1 << QCOW2_INCOMPAT_CORRUPT_BITNR, |
c61d0004 | 193 | |
69c98726 HR |
194 | QCOW2_INCOMPAT_MASK = QCOW2_INCOMPAT_DIRTY |
195 | | QCOW2_INCOMPAT_CORRUPT, | |
c61d0004 SH |
196 | }; |
197 | ||
bfe8043e SH |
198 | /* Compatible feature bits */ |
199 | enum { | |
200 | QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR = 0, | |
201 | QCOW2_COMPAT_LAZY_REFCOUNTS = 1 << QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR, | |
202 | ||
203 | QCOW2_COMPAT_FEAT_MASK = QCOW2_COMPAT_LAZY_REFCOUNTS, | |
204 | }; | |
205 | ||
88ddffae VSO |
206 | /* Autoclear feature bits */ |
207 | enum { | |
208 | QCOW2_AUTOCLEAR_BITMAPS_BITNR = 0, | |
209 | QCOW2_AUTOCLEAR_BITMAPS = 1 << QCOW2_AUTOCLEAR_BITMAPS_BITNR, | |
210 | ||
211 | QCOW2_AUTOCLEAR_MASK = QCOW2_AUTOCLEAR_BITMAPS, | |
212 | }; | |
213 | ||
6cfcb9b8 KW |
214 | enum qcow2_discard_type { |
215 | QCOW2_DISCARD_NEVER = 0, | |
216 | QCOW2_DISCARD_ALWAYS, | |
217 | QCOW2_DISCARD_REQUEST, | |
218 | QCOW2_DISCARD_SNAPSHOT, | |
219 | QCOW2_DISCARD_OTHER, | |
220 | QCOW2_DISCARD_MAX | |
221 | }; | |
222 | ||
cfcc4c62 KW |
223 | typedef struct Qcow2Feature { |
224 | uint8_t type; | |
225 | uint8_t bit; | |
226 | char name[46]; | |
227 | } QEMU_PACKED Qcow2Feature; | |
228 | ||
0b919fae KW |
229 | typedef struct Qcow2DiscardRegion { |
230 | BlockDriverState *bs; | |
231 | uint64_t offset; | |
232 | uint64_t bytes; | |
233 | QTAILQ_ENTRY(Qcow2DiscardRegion) next; | |
234 | } Qcow2DiscardRegion; | |
235 | ||
7453c96b HR |
236 | typedef uint64_t Qcow2GetRefcountFunc(const void *refcount_array, |
237 | uint64_t index); | |
238 | typedef void Qcow2SetRefcountFunc(void *refcount_array, | |
239 | uint64_t index, uint64_t value); | |
240 | ||
88ddffae VSO |
241 | typedef struct Qcow2BitmapHeaderExt { |
242 | uint32_t nb_bitmaps; | |
243 | uint32_t reserved32; | |
244 | uint64_t bitmap_directory_size; | |
245 | uint64_t bitmap_directory_offset; | |
246 | } QEMU_PACKED Qcow2BitmapHeaderExt; | |
247 | ||
ff99129a | 248 | typedef struct BDRVQcow2State { |
f7d0fe02 KW |
249 | int cluster_bits; |
250 | int cluster_size; | |
251 | int cluster_sectors; | |
3c2e511a | 252 | int l2_slice_size; |
f7d0fe02 KW |
253 | int l2_bits; |
254 | int l2_size; | |
255 | int l1_size; | |
256 | int l1_vm_state_index; | |
1d13d654 HR |
257 | int refcount_block_bits; |
258 | int refcount_block_size; | |
f7d0fe02 KW |
259 | int csize_shift; |
260 | int csize_mask; | |
261 | uint64_t cluster_offset_mask; | |
262 | uint64_t l1_table_offset; | |
263 | uint64_t *l1_table; | |
29c1a730 KW |
264 | |
265 | Qcow2Cache* l2_table_cache; | |
266 | Qcow2Cache* refcount_block_cache; | |
279621c0 AG |
267 | QEMUTimer *cache_clean_timer; |
268 | unsigned cache_clean_interval; | |
29c1a730 | 269 | |
f7d0fe02 KW |
270 | uint8_t *cluster_cache; |
271 | uint8_t *cluster_data; | |
272 | uint64_t cluster_cache_offset; | |
72cf2d4f | 273 | QLIST_HEAD(QCowClusterAlloc, QCowL2Meta) cluster_allocs; |
f7d0fe02 KW |
274 | |
275 | uint64_t *refcount_table; | |
276 | uint64_t refcount_table_offset; | |
277 | uint32_t refcount_table_size; | |
7061a078 | 278 | uint32_t max_refcount_table_index; /* Last used entry in refcount_table */ |
bb572aef KW |
279 | uint64_t free_cluster_index; |
280 | uint64_t free_byte_offset; | |
f7d0fe02 | 281 | |
68d100e9 KW |
282 | CoMutex lock; |
283 | ||
4652b8f3 | 284 | Qcow2CryptoHeaderExtension crypto_header; /* QCow2 header extension */ |
b25b387f DB |
285 | QCryptoBlockOpenOptions *crypto_opts; /* Disk encryption runtime options */ |
286 | QCryptoBlock *crypto; /* Disk encryption format driver */ | |
4652b8f3 DB |
287 | bool crypt_physical_offset; /* Whether to use virtual or physical offset |
288 | for encryption initialization vector tweak */ | |
f7d0fe02 | 289 | uint32_t crypt_method_header; |
f7d0fe02 KW |
290 | uint64_t snapshots_offset; |
291 | int snapshots_size; | |
ce48f2f4 | 292 | unsigned int nb_snapshots; |
f7d0fe02 | 293 | QCowSnapshot *snapshots; |
06d9260f | 294 | |
88ddffae VSO |
295 | uint32_t nb_bitmaps; |
296 | uint64_t bitmap_directory_size; | |
297 | uint64_t bitmap_directory_offset; | |
605bc8be | 298 | bool dirty_bitmaps_loaded; |
88ddffae | 299 | |
06d9260f | 300 | int flags; |
6744cbab | 301 | int qcow_version; |
74c4510a | 302 | bool use_lazy_refcounts; |
b6481f37 | 303 | int refcount_order; |
346a53df HR |
304 | int refcount_bits; |
305 | uint64_t refcount_max; | |
6744cbab | 306 | |
7453c96b HR |
307 | Qcow2GetRefcountFunc *get_refcount; |
308 | Qcow2SetRefcountFunc *set_refcount; | |
309 | ||
67af674e KW |
310 | bool discard_passthrough[QCOW2_DISCARD_MAX]; |
311 | ||
3e355390 | 312 | int overlap_check; /* bitmask of Qcow2MetadataOverlap values */ |
85186ebd | 313 | bool signaled_corruption; |
3e355390 | 314 | |
6744cbab KW |
315 | uint64_t incompatible_features; |
316 | uint64_t compatible_features; | |
317 | uint64_t autoclear_features; | |
318 | ||
319 | size_t unknown_header_fields_size; | |
320 | void* unknown_header_fields; | |
75bab85c | 321 | QLIST_HEAD(, Qcow2UnknownHeaderExtension) unknown_header_ext; |
0b919fae KW |
322 | QTAILQ_HEAD (, Qcow2DiscardRegion) discards; |
323 | bool cache_discards; | |
e4603fe1 KW |
324 | |
325 | /* Backing file path and format as stored in the image (this is not the | |
326 | * effective path/format, which may be the result of a runtime option | |
327 | * override) */ | |
328 | char *image_backing_file; | |
329 | char *image_backing_format; | |
ceb029cd VSO |
330 | |
331 | CoQueue compress_wait_queue; | |
332 | int nb_compress_threads; | |
ff99129a | 333 | } BDRVQcow2State; |
f7d0fe02 | 334 | |
593fb83c KW |
335 | typedef struct Qcow2COWRegion { |
336 | /** | |
337 | * Offset of the COW region in bytes from the start of the first cluster | |
338 | * touched by the request. | |
339 | */ | |
e034f5bc | 340 | unsigned offset; |
593fb83c | 341 | |
85567393 | 342 | /** Number of bytes to copy */ |
e034f5bc | 343 | unsigned nb_bytes; |
593fb83c KW |
344 | } Qcow2COWRegion; |
345 | ||
f50f88b9 KW |
346 | /** |
347 | * Describes an in-flight (part of a) write request that writes to clusters | |
348 | * that are not referenced in their L2 table yet. | |
349 | */ | |
45aba42f KW |
350 | typedef struct QCowL2Meta |
351 | { | |
1d3afd64 | 352 | /** Guest offset of the first newly allocated cluster */ |
45aba42f | 353 | uint64_t offset; |
1d3afd64 | 354 | |
1d3afd64 | 355 | /** Host offset of the first newly allocated cluster */ |
250196f1 | 356 | uint64_t alloc_offset; |
1d3afd64 | 357 | |
1d3afd64 | 358 | /** Number of newly allocated clusters */ |
45aba42f | 359 | int nb_clusters; |
1d3afd64 | 360 | |
564a6b69 HR |
361 | /** Do not free the old clusters */ |
362 | bool keep_old_clusters; | |
363 | ||
1d3afd64 KW |
364 | /** |
365 | * Requests that overlap with this allocation and wait to be restarted | |
366 | * when the allocating request has completed. | |
367 | */ | |
68d100e9 | 368 | CoQueue dependent_requests; |
f214978a | 369 | |
593fb83c KW |
370 | /** |
371 | * The COW Region between the start of the first allocated cluster and the | |
372 | * area the guest actually writes to. | |
373 | */ | |
374 | Qcow2COWRegion cow_start; | |
375 | ||
376 | /** | |
377 | * The COW Region between the area the guest actually writes to and the | |
378 | * end of the last allocated cluster. | |
379 | */ | |
380 | Qcow2COWRegion cow_end; | |
381 | ||
ee22a9d8 AG |
382 | /** |
383 | * The I/O vector with the data from the actual guest write request. | |
384 | * If non-NULL, this is meant to be merged together with the data | |
385 | * from @cow_start and @cow_end into one single write operation. | |
386 | */ | |
387 | QEMUIOVector *data_qiov; | |
388 | ||
88c6588c KW |
389 | /** Pointer to next L2Meta of the same write request */ |
390 | struct QCowL2Meta *next; | |
391 | ||
72cf2d4f | 392 | QLIST_ENTRY(QCowL2Meta) next_in_flight; |
45aba42f KW |
393 | } QCowL2Meta; |
394 | ||
3ef95218 | 395 | typedef enum QCow2ClusterType { |
68d000a3 | 396 | QCOW2_CLUSTER_UNALLOCATED, |
fdfab37d EB |
397 | QCOW2_CLUSTER_ZERO_PLAIN, |
398 | QCOW2_CLUSTER_ZERO_ALLOC, | |
68d000a3 KW |
399 | QCOW2_CLUSTER_NORMAL, |
400 | QCOW2_CLUSTER_COMPRESSED, | |
3ef95218 | 401 | } QCow2ClusterType; |
68d000a3 | 402 | |
a40f1c2a | 403 | typedef enum QCow2MetadataOverlap { |
0e4e4318 VSO |
404 | QCOW2_OL_MAIN_HEADER_BITNR = 0, |
405 | QCOW2_OL_ACTIVE_L1_BITNR = 1, | |
406 | QCOW2_OL_ACTIVE_L2_BITNR = 2, | |
407 | QCOW2_OL_REFCOUNT_TABLE_BITNR = 3, | |
408 | QCOW2_OL_REFCOUNT_BLOCK_BITNR = 4, | |
409 | QCOW2_OL_SNAPSHOT_TABLE_BITNR = 5, | |
410 | QCOW2_OL_INACTIVE_L1_BITNR = 6, | |
411 | QCOW2_OL_INACTIVE_L2_BITNR = 7, | |
412 | QCOW2_OL_BITMAP_DIRECTORY_BITNR = 8, | |
413 | ||
414 | QCOW2_OL_MAX_BITNR = 9, | |
415 | ||
416 | QCOW2_OL_NONE = 0, | |
417 | QCOW2_OL_MAIN_HEADER = (1 << QCOW2_OL_MAIN_HEADER_BITNR), | |
418 | QCOW2_OL_ACTIVE_L1 = (1 << QCOW2_OL_ACTIVE_L1_BITNR), | |
419 | QCOW2_OL_ACTIVE_L2 = (1 << QCOW2_OL_ACTIVE_L2_BITNR), | |
420 | QCOW2_OL_REFCOUNT_TABLE = (1 << QCOW2_OL_REFCOUNT_TABLE_BITNR), | |
421 | QCOW2_OL_REFCOUNT_BLOCK = (1 << QCOW2_OL_REFCOUNT_BLOCK_BITNR), | |
422 | QCOW2_OL_SNAPSHOT_TABLE = (1 << QCOW2_OL_SNAPSHOT_TABLE_BITNR), | |
423 | QCOW2_OL_INACTIVE_L1 = (1 << QCOW2_OL_INACTIVE_L1_BITNR), | |
a40f1c2a HR |
424 | /* NOTE: Checking overlaps with inactive L2 tables will result in bdrv |
425 | * reads. */ | |
0e4e4318 VSO |
426 | QCOW2_OL_INACTIVE_L2 = (1 << QCOW2_OL_INACTIVE_L2_BITNR), |
427 | QCOW2_OL_BITMAP_DIRECTORY = (1 << QCOW2_OL_BITMAP_DIRECTORY_BITNR), | |
a40f1c2a HR |
428 | } QCow2MetadataOverlap; |
429 | ||
4a273c39 HR |
430 | /* Perform all overlap checks which can be done in constant time */ |
431 | #define QCOW2_OL_CONSTANT \ | |
432 | (QCOW2_OL_MAIN_HEADER | QCOW2_OL_ACTIVE_L1 | QCOW2_OL_REFCOUNT_TABLE | \ | |
0e4e4318 | 433 | QCOW2_OL_SNAPSHOT_TABLE | QCOW2_OL_BITMAP_DIRECTORY) |
4a273c39 | 434 | |
a40f1c2a HR |
435 | /* Perform all overlap checks which don't require disk access */ |
436 | #define QCOW2_OL_CACHED \ | |
4a273c39 HR |
437 | (QCOW2_OL_CONSTANT | QCOW2_OL_ACTIVE_L2 | QCOW2_OL_REFCOUNT_BLOCK | \ |
438 | QCOW2_OL_INACTIVE_L1) | |
439 | ||
440 | /* Perform all overlap checks */ | |
441 | #define QCOW2_OL_ALL \ | |
442 | (QCOW2_OL_CACHED | QCOW2_OL_INACTIVE_L2) | |
a40f1c2a | 443 | |
46bae927 HT |
444 | #define L1E_OFFSET_MASK 0x00fffffffffffe00ULL |
445 | #define L2E_OFFSET_MASK 0x00fffffffffffe00ULL | |
68d000a3 KW |
446 | #define L2E_COMPRESSED_OFFSET_SIZE_MASK 0x3fffffffffffffffULL |
447 | ||
46bae927 | 448 | #define REFT_OFFSET_MASK 0xfffffffffffffe00ULL |
76dc9e0c | 449 | |
ff99129a | 450 | static inline int64_t start_of_cluster(BDRVQcow2State *s, int64_t offset) |
3b8e2e26 KW |
451 | { |
452 | return offset & ~(s->cluster_size - 1); | |
453 | } | |
454 | ||
ff99129a | 455 | static inline int64_t offset_into_cluster(BDRVQcow2State *s, int64_t offset) |
c37f4cd7 KW |
456 | { |
457 | return offset & (s->cluster_size - 1); | |
458 | } | |
459 | ||
b6d36def | 460 | static inline uint64_t size_to_clusters(BDRVQcow2State *s, uint64_t size) |
f7d0fe02 KW |
461 | { |
462 | return (size + (s->cluster_size - 1)) >> s->cluster_bits; | |
463 | } | |
464 | ||
ff99129a | 465 | static inline int64_t size_to_l1(BDRVQcow2State *s, int64_t size) |
419b19d9 SH |
466 | { |
467 | int shift = s->cluster_bits + s->l2_bits; | |
468 | return (size + (1ULL << shift) - 1) >> shift; | |
469 | } | |
470 | ||
05b5b6ee AG |
471 | static inline int offset_to_l1_index(BDRVQcow2State *s, uint64_t offset) |
472 | { | |
473 | return offset >> (s->l2_bits + s->cluster_bits); | |
474 | } | |
475 | ||
ff99129a | 476 | static inline int offset_to_l2_index(BDRVQcow2State *s, int64_t offset) |
17a71e58 KW |
477 | { |
478 | return (offset >> s->cluster_bits) & (s->l2_size - 1); | |
479 | } | |
480 | ||
8f818175 AG |
481 | static inline int offset_to_l2_slice_index(BDRVQcow2State *s, int64_t offset) |
482 | { | |
483 | return (offset >> s->cluster_bits) & (s->l2_slice_size - 1); | |
484 | } | |
485 | ||
ff99129a | 486 | static inline int64_t qcow2_vm_state_offset(BDRVQcow2State *s) |
1ebf561c KW |
487 | { |
488 | return (int64_t)s->l1_vm_state_index << (s->cluster_bits + s->l2_bits); | |
489 | } | |
490 | ||
3ef95218 | 491 | static inline QCow2ClusterType qcow2_get_cluster_type(uint64_t l2_entry) |
68d000a3 KW |
492 | { |
493 | if (l2_entry & QCOW_OFLAG_COMPRESSED) { | |
494 | return QCOW2_CLUSTER_COMPRESSED; | |
6377af48 | 495 | } else if (l2_entry & QCOW_OFLAG_ZERO) { |
fdfab37d EB |
496 | if (l2_entry & L2E_OFFSET_MASK) { |
497 | return QCOW2_CLUSTER_ZERO_ALLOC; | |
498 | } | |
499 | return QCOW2_CLUSTER_ZERO_PLAIN; | |
68d000a3 KW |
500 | } else if (!(l2_entry & L2E_OFFSET_MASK)) { |
501 | return QCOW2_CLUSTER_UNALLOCATED; | |
502 | } else { | |
503 | return QCOW2_CLUSTER_NORMAL; | |
504 | } | |
505 | } | |
506 | ||
bfe8043e | 507 | /* Check whether refcounts are eager or lazy */ |
ff99129a | 508 | static inline bool qcow2_need_accurate_refcounts(BDRVQcow2State *s) |
bfe8043e SH |
509 | { |
510 | return !(s->incompatible_features & QCOW2_INCOMPAT_DIRTY); | |
511 | } | |
c142442b | 512 | |
65eb2e35 KW |
513 | static inline uint64_t l2meta_cow_start(QCowL2Meta *m) |
514 | { | |
515 | return m->offset + m->cow_start.offset; | |
516 | } | |
517 | ||
518 | static inline uint64_t l2meta_cow_end(QCowL2Meta *m) | |
519 | { | |
85567393 | 520 | return m->offset + m->cow_end.offset + m->cow_end.nb_bytes; |
65eb2e35 KW |
521 | } |
522 | ||
0e06528e | 523 | static inline uint64_t refcount_diff(uint64_t r1, uint64_t r2) |
2aabe7c7 HR |
524 | { |
525 | return r1 > r2 ? r1 - r2 : r2 - r1; | |
526 | } | |
527 | ||
46b732cd PB |
528 | static inline |
529 | uint32_t offset_to_reftable_index(BDRVQcow2State *s, uint64_t offset) | |
530 | { | |
531 | return offset >> (s->refcount_block_bits + s->cluster_bits); | |
532 | } | |
533 | ||
f7d0fe02 | 534 | /* qcow2.c functions */ |
12cc30a8 HR |
535 | int64_t qcow2_refcount_metadata_size(int64_t clusters, size_t cluster_size, |
536 | int refcount_order, bool generous_increase, | |
537 | uint64_t *refblock_count); | |
538 | ||
280d3735 | 539 | int qcow2_mark_dirty(BlockDriverState *bs); |
69c98726 HR |
540 | int qcow2_mark_corrupt(BlockDriverState *bs); |
541 | int qcow2_mark_consistent(BlockDriverState *bs); | |
e24e49e6 | 542 | int qcow2_update_header(BlockDriverState *bs); |
f7d0fe02 | 543 | |
85186ebd HR |
544 | void qcow2_signal_corruption(BlockDriverState *bs, bool fatal, int64_t offset, |
545 | int64_t size, const char *message_format, ...) | |
546 | GCC_FMT_ATTR(5, 6); | |
547 | ||
0cf0e598 AG |
548 | int qcow2_validate_table(BlockDriverState *bs, uint64_t offset, |
549 | uint64_t entries, size_t entry_len, | |
550 | int64_t max_size_bytes, const char *table_name, | |
551 | Error **errp); | |
552 | ||
f7d0fe02 | 553 | /* qcow2-refcount.c functions */ |
ed6ccf0f KW |
554 | int qcow2_refcount_init(BlockDriverState *bs); |
555 | void qcow2_refcount_close(BlockDriverState *bs); | |
f7d0fe02 | 556 | |
7324c10f | 557 | int qcow2_get_refcount(BlockDriverState *bs, int64_t cluster_index, |
0e06528e | 558 | uint64_t *refcount); |
44751917 | 559 | |
32b6444d | 560 | int qcow2_update_cluster_refcount(BlockDriverState *bs, int64_t cluster_index, |
0e06528e | 561 | uint64_t addend, bool decrease, |
2aabe7c7 | 562 | enum qcow2_discard_type type); |
32b6444d | 563 | |
772d1f97 HR |
564 | int64_t qcow2_refcount_area(BlockDriverState *bs, uint64_t offset, |
565 | uint64_t additional_clusters, bool exact_size, | |
566 | int new_refblock_index, | |
567 | uint64_t new_refblock_offset); | |
568 | ||
bb572aef | 569 | int64_t qcow2_alloc_clusters(BlockDriverState *bs, uint64_t size); |
b6d36def HR |
570 | int64_t qcow2_alloc_clusters_at(BlockDriverState *bs, uint64_t offset, |
571 | int64_t nb_clusters); | |
ed6ccf0f KW |
572 | int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size); |
573 | void qcow2_free_clusters(BlockDriverState *bs, | |
6cfcb9b8 KW |
574 | int64_t offset, int64_t size, |
575 | enum qcow2_discard_type type); | |
576 | void qcow2_free_any_clusters(BlockDriverState *bs, uint64_t l2_entry, | |
577 | int nb_clusters, enum qcow2_discard_type type); | |
f7d0fe02 | 578 | |
ed6ccf0f KW |
579 | int qcow2_update_snapshot_refcount(BlockDriverState *bs, |
580 | int64_t l1_table_offset, int l1_size, int addend); | |
f7d0fe02 | 581 | |
8b220eb7 PB |
582 | int coroutine_fn qcow2_flush_caches(BlockDriverState *bs); |
583 | int coroutine_fn qcow2_write_caches(BlockDriverState *bs); | |
166acf54 KW |
584 | int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res, |
585 | BdrvCheckMode fix); | |
f7d0fe02 | 586 | |
0b919fae KW |
587 | void qcow2_process_discards(BlockDriverState *bs, int ret); |
588 | ||
231bb267 | 589 | int qcow2_check_metadata_overlap(BlockDriverState *bs, int ign, int64_t offset, |
a40f1c2a | 590 | int64_t size); |
231bb267 | 591 | int qcow2_pre_write_overlap_check(BlockDriverState *bs, int ign, int64_t offset, |
a40f1c2a | 592 | int64_t size); |
8a5bb1f1 VSO |
593 | int qcow2_inc_refcounts_imrt(BlockDriverState *bs, BdrvCheckResult *res, |
594 | void **refcount_table, | |
595 | int64_t *refcount_table_size, | |
596 | int64_t offset, int64_t size); | |
a40f1c2a | 597 | |
791c9a00 HR |
598 | int qcow2_change_refcount_order(BlockDriverState *bs, int refcount_order, |
599 | BlockDriverAmendStatusCB *status_cb, | |
600 | void *cb_opaque, Error **errp); | |
46b732cd | 601 | int qcow2_shrink_reftable(BlockDriverState *bs); |
163bc39d | 602 | int64_t qcow2_get_last_cluster(BlockDriverState *bs, int64_t size); |
791c9a00 | 603 | |
45aba42f | 604 | /* qcow2-cluster.c functions */ |
2cf7cfa1 KW |
605 | int qcow2_grow_l1_table(BlockDriverState *bs, uint64_t min_size, |
606 | bool exact_size); | |
46b732cd | 607 | int qcow2_shrink_l1_table(BlockDriverState *bs, uint64_t max_size); |
e23e400e | 608 | int qcow2_write_l1_entry(BlockDriverState *bs, int l1_index); |
66f82cee | 609 | int qcow2_decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset); |
ff99129a | 610 | int qcow2_encrypt_sectors(BDRVQcow2State *s, int64_t sector_num, |
446d306d | 611 | uint8_t *buf, int nb_sectors, bool enc, Error **errp); |
45aba42f | 612 | |
1c46efaa | 613 | int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset, |
ecfe1863 | 614 | unsigned int *bytes, uint64_t *cluster_offset); |
f4f0d391 | 615 | int qcow2_alloc_cluster_offset(BlockDriverState *bs, uint64_t offset, |
d46a0bb2 KW |
616 | unsigned int *bytes, uint64_t *host_offset, |
617 | QCowL2Meta **m); | |
ed6ccf0f | 618 | uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs, |
45aba42f KW |
619 | uint64_t offset, |
620 | int compressed_size); | |
621 | ||
148da7ea | 622 | int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m); |
8b24cd14 | 623 | void qcow2_alloc_cluster_abort(BlockDriverState *bs, QCowL2Meta *m); |
d2cb36af EB |
624 | int qcow2_cluster_discard(BlockDriverState *bs, uint64_t offset, |
625 | uint64_t bytes, enum qcow2_discard_type type, | |
626 | bool full_discard); | |
627 | int qcow2_cluster_zeroize(BlockDriverState *bs, uint64_t offset, | |
628 | uint64_t bytes, int flags); | |
45aba42f | 629 | |
4057a2b2 | 630 | int qcow2_expand_zero_clusters(BlockDriverState *bs, |
8b13976d HR |
631 | BlockDriverAmendStatusCB *status_cb, |
632 | void *cb_opaque); | |
32b6444d | 633 | |
c142442b | 634 | /* qcow2-snapshot.c functions */ |
ed6ccf0f KW |
635 | int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info); |
636 | int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id); | |
a89d89d3 WX |
637 | int qcow2_snapshot_delete(BlockDriverState *bs, |
638 | const char *snapshot_id, | |
639 | const char *name, | |
640 | Error **errp); | |
ed6ccf0f | 641 | int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab); |
7b4c4781 WX |
642 | int qcow2_snapshot_load_tmp(BlockDriverState *bs, |
643 | const char *snapshot_id, | |
644 | const char *name, | |
645 | Error **errp); | |
c142442b | 646 | |
ed6ccf0f KW |
647 | void qcow2_free_snapshots(BlockDriverState *bs); |
648 | int qcow2_read_snapshots(BlockDriverState *bs); | |
c142442b | 649 | |
49381094 | 650 | /* qcow2-cache.c functions */ |
1221fe6f AG |
651 | Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables, |
652 | unsigned table_size); | |
e64d4072 | 653 | int qcow2_cache_destroy(Qcow2Cache *c); |
49381094 | 654 | |
2d135ee9 | 655 | void qcow2_cache_entry_mark_dirty(Qcow2Cache *c, void *table); |
49381094 | 656 | int qcow2_cache_flush(BlockDriverState *bs, Qcow2Cache *c); |
f3c3b87d | 657 | int qcow2_cache_write(BlockDriverState *bs, Qcow2Cache *c); |
49381094 KW |
658 | int qcow2_cache_set_dependency(BlockDriverState *bs, Qcow2Cache *c, |
659 | Qcow2Cache *dependency); | |
3de0a294 | 660 | void qcow2_cache_depends_on_flush(Qcow2Cache *c); |
49381094 | 661 | |
b2f68bff | 662 | void qcow2_cache_clean_unused(Qcow2Cache *c); |
e7108fea HR |
663 | int qcow2_cache_empty(BlockDriverState *bs, Qcow2Cache *c); |
664 | ||
49381094 KW |
665 | int qcow2_cache_get(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset, |
666 | void **table); | |
667 | int qcow2_cache_get_empty(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset, | |
668 | void **table); | |
2013c3d4 | 669 | void qcow2_cache_put(Qcow2Cache *c, void **table); |
6e6fa760 | 670 | void *qcow2_cache_is_table_offset(Qcow2Cache *c, uint64_t offset); |
77aadd7b | 671 | void qcow2_cache_discard(Qcow2Cache *c, void *table); |
49381094 | 672 | |
88ddffae VSO |
673 | /* qcow2-bitmap.c functions */ |
674 | int qcow2_check_bitmaps_refcounts(BlockDriverState *bs, BdrvCheckResult *res, | |
675 | void **refcount_table, | |
676 | int64_t *refcount_table_size); | |
3e99da5e | 677 | bool qcow2_load_dirty_bitmaps(BlockDriverState *bs, Error **errp); |
b1336cc2 VSO |
678 | int qcow2_reopen_bitmaps_rw_hint(BlockDriverState *bs, bool *header_updated, |
679 | Error **errp); | |
1b6b0562 | 680 | int qcow2_reopen_bitmaps_rw(BlockDriverState *bs, Error **errp); |
5f72826e | 681 | void qcow2_store_persistent_dirty_bitmaps(BlockDriverState *bs, Error **errp); |
169b8793 | 682 | int qcow2_reopen_bitmaps_ro(BlockDriverState *bs, Error **errp); |
da0eb242 VSO |
683 | bool qcow2_can_store_new_dirty_bitmap(BlockDriverState *bs, |
684 | const char *name, | |
685 | uint32_t granularity, | |
686 | Error **errp); | |
469c71ed VSO |
687 | void qcow2_remove_persistent_dirty_bitmap(BlockDriverState *bs, |
688 | const char *name, | |
689 | Error **errp); | |
d1258dd0 | 690 | |
f7d0fe02 | 691 | #endif |