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