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