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