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