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