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 #include "qemu-common.h" 25 #include "block/block_int.h" 26 #include "qemu/module.h" 27 #include <zlib.h> 28 #include "qemu/aes.h" 29 #include "block/qcow2.h" 30 #include "qemu/error-report.h" 31 #include "qapi/qmp/qerror.h" 32 #include "qapi/qmp/qbool.h" 33 #include "trace.h" 34 35 /* 36 Differences with QCOW: 37 38 - Support for multiple incremental snapshots. 39 - Memory management by reference counts. 40 - Clusters which have a reference count of one have the bit 41 QCOW_OFLAG_COPIED to optimize write performance. 42 - Size of compressed clusters is stored in sectors to reduce bit usage 43 in the cluster offsets. 44 - Support for storing additional data (such as the VM state) in the 45 snapshots. 46 - If a backing store is used, the cluster size is not constrained 47 (could be backported to QCOW). 48 - L2 tables have always a size of one cluster. 49 */ 50 51 52 typedef struct { 53 uint32_t magic; 54 uint32_t len; 55 } QEMU_PACKED QCowExtension; 56 57 #define QCOW2_EXT_MAGIC_END 0 58 #define QCOW2_EXT_MAGIC_BACKING_FORMAT 0xE2792ACA 59 #define QCOW2_EXT_MAGIC_FEATURE_TABLE 0x6803f857 60 61 static int qcow2_probe(const uint8_t *buf, int buf_size, const char *filename) 62 { 63 const QCowHeader *cow_header = (const void *)buf; 64 65 if (buf_size >= sizeof(QCowHeader) && 66 be32_to_cpu(cow_header->magic) == QCOW_MAGIC && 67 be32_to_cpu(cow_header->version) >= 2) 68 return 100; 69 else 70 return 0; 71 } 72 73 74 /* 75 * read qcow2 extension and fill bs 76 * start reading from start_offset 77 * finish reading upon magic of value 0 or when end_offset reached 78 * unknown magic is skipped (future extension this version knows nothing about) 79 * return 0 upon success, non-0 otherwise 80 */ 81 static int qcow2_read_extensions(BlockDriverState *bs, uint64_t start_offset, 82 uint64_t end_offset, void **p_feature_table, 83 Error **errp) 84 { 85 BDRVQcowState *s = bs->opaque; 86 QCowExtension ext; 87 uint64_t offset; 88 int ret; 89 90 #ifdef DEBUG_EXT 91 printf("qcow2_read_extensions: start=%ld end=%ld\n", start_offset, end_offset); 92 #endif 93 offset = start_offset; 94 while (offset < end_offset) { 95 96 #ifdef DEBUG_EXT 97 /* Sanity check */ 98 if (offset > s->cluster_size) 99 printf("qcow2_read_extension: suspicious offset %lu\n", offset); 100 101 printf("attempting to read extended header in offset %lu\n", offset); 102 #endif 103 104 ret = bdrv_pread(bs->file, offset, &ext, sizeof(ext)); 105 if (ret < 0) { 106 error_setg_errno(errp, -ret, "qcow2_read_extension: ERROR: " 107 "pread fail from offset %" PRIu64, offset); 108 return 1; 109 } 110 be32_to_cpus(&ext.magic); 111 be32_to_cpus(&ext.len); 112 offset += sizeof(ext); 113 #ifdef DEBUG_EXT 114 printf("ext.magic = 0x%x\n", ext.magic); 115 #endif 116 if (ext.len > end_offset - offset) { 117 error_setg(errp, "Header extension too large"); 118 return -EINVAL; 119 } 120 121 switch (ext.magic) { 122 case QCOW2_EXT_MAGIC_END: 123 return 0; 124 125 case QCOW2_EXT_MAGIC_BACKING_FORMAT: 126 if (ext.len >= sizeof(bs->backing_format)) { 127 error_setg(errp, "ERROR: ext_backing_format: len=%" PRIu32 128 " too large (>=%zu)", ext.len, 129 sizeof(bs->backing_format)); 130 return 2; 131 } 132 ret = bdrv_pread(bs->file, offset, bs->backing_format, ext.len); 133 if (ret < 0) { 134 error_setg_errno(errp, -ret, "ERROR: ext_backing_format: " 135 "Could not read format name"); 136 return 3; 137 } 138 bs->backing_format[ext.len] = '\0'; 139 #ifdef DEBUG_EXT 140 printf("Qcow2: Got format extension %s\n", bs->backing_format); 141 #endif 142 break; 143 144 case QCOW2_EXT_MAGIC_FEATURE_TABLE: 145 if (p_feature_table != NULL) { 146 void* feature_table = g_malloc0(ext.len + 2 * sizeof(Qcow2Feature)); 147 ret = bdrv_pread(bs->file, offset , feature_table, ext.len); 148 if (ret < 0) { 149 error_setg_errno(errp, -ret, "ERROR: ext_feature_table: " 150 "Could not read table"); 151 return ret; 152 } 153 154 *p_feature_table = feature_table; 155 } 156 break; 157 158 default: 159 /* unknown magic - save it in case we need to rewrite the header */ 160 { 161 Qcow2UnknownHeaderExtension *uext; 162 163 uext = g_malloc0(sizeof(*uext) + ext.len); 164 uext->magic = ext.magic; 165 uext->len = ext.len; 166 QLIST_INSERT_HEAD(&s->unknown_header_ext, uext, next); 167 168 ret = bdrv_pread(bs->file, offset , uext->data, uext->len); 169 if (ret < 0) { 170 error_setg_errno(errp, -ret, "ERROR: unknown extension: " 171 "Could not read data"); 172 return ret; 173 } 174 } 175 break; 176 } 177 178 offset += ((ext.len + 7) & ~7); 179 } 180 181 return 0; 182 } 183 184 static void cleanup_unknown_header_ext(BlockDriverState *bs) 185 { 186 BDRVQcowState *s = bs->opaque; 187 Qcow2UnknownHeaderExtension *uext, *next; 188 189 QLIST_FOREACH_SAFE(uext, &s->unknown_header_ext, next, next) { 190 QLIST_REMOVE(uext, next); 191 g_free(uext); 192 } 193 } 194 195 static void GCC_FMT_ATTR(3, 4) report_unsupported(BlockDriverState *bs, 196 Error **errp, const char *fmt, ...) 197 { 198 char msg[64]; 199 va_list ap; 200 201 va_start(ap, fmt); 202 vsnprintf(msg, sizeof(msg), fmt, ap); 203 va_end(ap); 204 205 error_set(errp, QERR_UNKNOWN_BLOCK_FORMAT_FEATURE, bs->device_name, "qcow2", 206 msg); 207 } 208 209 static void report_unsupported_feature(BlockDriverState *bs, 210 Error **errp, Qcow2Feature *table, uint64_t mask) 211 { 212 while (table && table->name[0] != '\0') { 213 if (table->type == QCOW2_FEAT_TYPE_INCOMPATIBLE) { 214 if (mask & (1 << table->bit)) { 215 report_unsupported(bs, errp, "%.46s", table->name); 216 mask &= ~(1 << table->bit); 217 } 218 } 219 table++; 220 } 221 222 if (mask) { 223 report_unsupported(bs, errp, "Unknown incompatible feature: %" PRIx64, 224 mask); 225 } 226 } 227 228 /* 229 * Sets the dirty bit and flushes afterwards if necessary. 230 * 231 * The incompatible_features bit is only set if the image file header was 232 * updated successfully. Therefore it is not required to check the return 233 * value of this function. 234 */ 235 int qcow2_mark_dirty(BlockDriverState *bs) 236 { 237 BDRVQcowState *s = bs->opaque; 238 uint64_t val; 239 int ret; 240 241 assert(s->qcow_version >= 3); 242 243 if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) { 244 return 0; /* already dirty */ 245 } 246 247 val = cpu_to_be64(s->incompatible_features | QCOW2_INCOMPAT_DIRTY); 248 ret = bdrv_pwrite(bs->file, offsetof(QCowHeader, incompatible_features), 249 &val, sizeof(val)); 250 if (ret < 0) { 251 return ret; 252 } 253 ret = bdrv_flush(bs->file); 254 if (ret < 0) { 255 return ret; 256 } 257 258 /* Only treat image as dirty if the header was updated successfully */ 259 s->incompatible_features |= QCOW2_INCOMPAT_DIRTY; 260 return 0; 261 } 262 263 /* 264 * Clears the dirty bit and flushes before if necessary. Only call this 265 * function when there are no pending requests, it does not guard against 266 * concurrent requests dirtying the image. 267 */ 268 static int qcow2_mark_clean(BlockDriverState *bs) 269 { 270 BDRVQcowState *s = bs->opaque; 271 272 if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) { 273 int ret; 274 275 s->incompatible_features &= ~QCOW2_INCOMPAT_DIRTY; 276 277 ret = bdrv_flush(bs); 278 if (ret < 0) { 279 return ret; 280 } 281 282 return qcow2_update_header(bs); 283 } 284 return 0; 285 } 286 287 /* 288 * Marks the image as corrupt. 289 */ 290 int qcow2_mark_corrupt(BlockDriverState *bs) 291 { 292 BDRVQcowState *s = bs->opaque; 293 294 s->incompatible_features |= QCOW2_INCOMPAT_CORRUPT; 295 return qcow2_update_header(bs); 296 } 297 298 /* 299 * Marks the image as consistent, i.e., unsets the corrupt bit, and flushes 300 * before if necessary. 301 */ 302 int qcow2_mark_consistent(BlockDriverState *bs) 303 { 304 BDRVQcowState *s = bs->opaque; 305 306 if (s->incompatible_features & QCOW2_INCOMPAT_CORRUPT) { 307 int ret = bdrv_flush(bs); 308 if (ret < 0) { 309 return ret; 310 } 311 312 s->incompatible_features &= ~QCOW2_INCOMPAT_CORRUPT; 313 return qcow2_update_header(bs); 314 } 315 return 0; 316 } 317 318 static int qcow2_check(BlockDriverState *bs, BdrvCheckResult *result, 319 BdrvCheckMode fix) 320 { 321 int ret = qcow2_check_refcounts(bs, result, fix); 322 if (ret < 0) { 323 return ret; 324 } 325 326 if (fix && result->check_errors == 0 && result->corruptions == 0) { 327 ret = qcow2_mark_clean(bs); 328 if (ret < 0) { 329 return ret; 330 } 331 return qcow2_mark_consistent(bs); 332 } 333 return ret; 334 } 335 336 static int validate_table_offset(BlockDriverState *bs, uint64_t offset, 337 uint64_t entries, size_t entry_len) 338 { 339 BDRVQcowState *s = bs->opaque; 340 uint64_t size; 341 342 /* Use signed INT64_MAX as the maximum even for uint64_t header fields, 343 * because values will be passed to qemu functions taking int64_t. */ 344 if (entries > INT64_MAX / entry_len) { 345 return -EINVAL; 346 } 347 348 size = entries * entry_len; 349 350 if (INT64_MAX - size < offset) { 351 return -EINVAL; 352 } 353 354 /* Tables must be cluster aligned */ 355 if (offset & (s->cluster_size - 1)) { 356 return -EINVAL; 357 } 358 359 return 0; 360 } 361 362 static QemuOptsList qcow2_runtime_opts = { 363 .name = "qcow2", 364 .head = QTAILQ_HEAD_INITIALIZER(qcow2_runtime_opts.head), 365 .desc = { 366 { 367 .name = QCOW2_OPT_LAZY_REFCOUNTS, 368 .type = QEMU_OPT_BOOL, 369 .help = "Postpone refcount updates", 370 }, 371 { 372 .name = QCOW2_OPT_DISCARD_REQUEST, 373 .type = QEMU_OPT_BOOL, 374 .help = "Pass guest discard requests to the layer below", 375 }, 376 { 377 .name = QCOW2_OPT_DISCARD_SNAPSHOT, 378 .type = QEMU_OPT_BOOL, 379 .help = "Generate discard requests when snapshot related space " 380 "is freed", 381 }, 382 { 383 .name = QCOW2_OPT_DISCARD_OTHER, 384 .type = QEMU_OPT_BOOL, 385 .help = "Generate discard requests when other clusters are freed", 386 }, 387 { 388 .name = QCOW2_OPT_OVERLAP, 389 .type = QEMU_OPT_STRING, 390 .help = "Selects which overlap checks to perform from a range of " 391 "templates (none, constant, cached, all)", 392 }, 393 { 394 .name = QCOW2_OPT_OVERLAP_MAIN_HEADER, 395 .type = QEMU_OPT_BOOL, 396 .help = "Check for unintended writes into the main qcow2 header", 397 }, 398 { 399 .name = QCOW2_OPT_OVERLAP_ACTIVE_L1, 400 .type = QEMU_OPT_BOOL, 401 .help = "Check for unintended writes into the active L1 table", 402 }, 403 { 404 .name = QCOW2_OPT_OVERLAP_ACTIVE_L2, 405 .type = QEMU_OPT_BOOL, 406 .help = "Check for unintended writes into an active L2 table", 407 }, 408 { 409 .name = QCOW2_OPT_OVERLAP_REFCOUNT_TABLE, 410 .type = QEMU_OPT_BOOL, 411 .help = "Check for unintended writes into the refcount table", 412 }, 413 { 414 .name = QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK, 415 .type = QEMU_OPT_BOOL, 416 .help = "Check for unintended writes into a refcount block", 417 }, 418 { 419 .name = QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE, 420 .type = QEMU_OPT_BOOL, 421 .help = "Check for unintended writes into the snapshot table", 422 }, 423 { 424 .name = QCOW2_OPT_OVERLAP_INACTIVE_L1, 425 .type = QEMU_OPT_BOOL, 426 .help = "Check for unintended writes into an inactive L1 table", 427 }, 428 { 429 .name = QCOW2_OPT_OVERLAP_INACTIVE_L2, 430 .type = QEMU_OPT_BOOL, 431 .help = "Check for unintended writes into an inactive L2 table", 432 }, 433 { /* end of list */ } 434 }, 435 }; 436 437 static const char *overlap_bool_option_names[QCOW2_OL_MAX_BITNR] = { 438 [QCOW2_OL_MAIN_HEADER_BITNR] = QCOW2_OPT_OVERLAP_MAIN_HEADER, 439 [QCOW2_OL_ACTIVE_L1_BITNR] = QCOW2_OPT_OVERLAP_ACTIVE_L1, 440 [QCOW2_OL_ACTIVE_L2_BITNR] = QCOW2_OPT_OVERLAP_ACTIVE_L2, 441 [QCOW2_OL_REFCOUNT_TABLE_BITNR] = QCOW2_OPT_OVERLAP_REFCOUNT_TABLE, 442 [QCOW2_OL_REFCOUNT_BLOCK_BITNR] = QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK, 443 [QCOW2_OL_SNAPSHOT_TABLE_BITNR] = QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE, 444 [QCOW2_OL_INACTIVE_L1_BITNR] = QCOW2_OPT_OVERLAP_INACTIVE_L1, 445 [QCOW2_OL_INACTIVE_L2_BITNR] = QCOW2_OPT_OVERLAP_INACTIVE_L2, 446 }; 447 448 static int qcow2_open(BlockDriverState *bs, QDict *options, int flags, 449 Error **errp) 450 { 451 BDRVQcowState *s = bs->opaque; 452 unsigned int len, i; 453 int ret = 0; 454 QCowHeader header; 455 QemuOpts *opts; 456 Error *local_err = NULL; 457 uint64_t ext_end; 458 uint64_t l1_vm_state_index; 459 const char *opt_overlap_check; 460 int overlap_check_template = 0; 461 462 ret = bdrv_pread(bs->file, 0, &header, sizeof(header)); 463 if (ret < 0) { 464 error_setg_errno(errp, -ret, "Could not read qcow2 header"); 465 goto fail; 466 } 467 be32_to_cpus(&header.magic); 468 be32_to_cpus(&header.version); 469 be64_to_cpus(&header.backing_file_offset); 470 be32_to_cpus(&header.backing_file_size); 471 be64_to_cpus(&header.size); 472 be32_to_cpus(&header.cluster_bits); 473 be32_to_cpus(&header.crypt_method); 474 be64_to_cpus(&header.l1_table_offset); 475 be32_to_cpus(&header.l1_size); 476 be64_to_cpus(&header.refcount_table_offset); 477 be32_to_cpus(&header.refcount_table_clusters); 478 be64_to_cpus(&header.snapshots_offset); 479 be32_to_cpus(&header.nb_snapshots); 480 481 if (header.magic != QCOW_MAGIC) { 482 error_setg(errp, "Image is not in qcow2 format"); 483 ret = -EINVAL; 484 goto fail; 485 } 486 if (header.version < 2 || header.version > 3) { 487 report_unsupported(bs, errp, "QCOW version %" PRIu32, header.version); 488 ret = -ENOTSUP; 489 goto fail; 490 } 491 492 s->qcow_version = header.version; 493 494 /* Initialise cluster size */ 495 if (header.cluster_bits < MIN_CLUSTER_BITS || 496 header.cluster_bits > MAX_CLUSTER_BITS) { 497 error_setg(errp, "Unsupported cluster size: 2^%" PRIu32, 498 header.cluster_bits); 499 ret = -EINVAL; 500 goto fail; 501 } 502 503 s->cluster_bits = header.cluster_bits; 504 s->cluster_size = 1 << s->cluster_bits; 505 s->cluster_sectors = 1 << (s->cluster_bits - 9); 506 507 /* Initialise version 3 header fields */ 508 if (header.version == 2) { 509 header.incompatible_features = 0; 510 header.compatible_features = 0; 511 header.autoclear_features = 0; 512 header.refcount_order = 4; 513 header.header_length = 72; 514 } else { 515 be64_to_cpus(&header.incompatible_features); 516 be64_to_cpus(&header.compatible_features); 517 be64_to_cpus(&header.autoclear_features); 518 be32_to_cpus(&header.refcount_order); 519 be32_to_cpus(&header.header_length); 520 521 if (header.header_length < 104) { 522 error_setg(errp, "qcow2 header too short"); 523 ret = -EINVAL; 524 goto fail; 525 } 526 } 527 528 if (header.header_length > s->cluster_size) { 529 error_setg(errp, "qcow2 header exceeds cluster size"); 530 ret = -EINVAL; 531 goto fail; 532 } 533 534 if (header.header_length > sizeof(header)) { 535 s->unknown_header_fields_size = header.header_length - sizeof(header); 536 s->unknown_header_fields = g_malloc(s->unknown_header_fields_size); 537 ret = bdrv_pread(bs->file, sizeof(header), s->unknown_header_fields, 538 s->unknown_header_fields_size); 539 if (ret < 0) { 540 error_setg_errno(errp, -ret, "Could not read unknown qcow2 header " 541 "fields"); 542 goto fail; 543 } 544 } 545 546 if (header.backing_file_offset > s->cluster_size) { 547 error_setg(errp, "Invalid backing file offset"); 548 ret = -EINVAL; 549 goto fail; 550 } 551 552 if (header.backing_file_offset) { 553 ext_end = header.backing_file_offset; 554 } else { 555 ext_end = 1 << header.cluster_bits; 556 } 557 558 /* Handle feature bits */ 559 s->incompatible_features = header.incompatible_features; 560 s->compatible_features = header.compatible_features; 561 s->autoclear_features = header.autoclear_features; 562 563 if (s->incompatible_features & ~QCOW2_INCOMPAT_MASK) { 564 void *feature_table = NULL; 565 qcow2_read_extensions(bs, header.header_length, ext_end, 566 &feature_table, NULL); 567 report_unsupported_feature(bs, errp, feature_table, 568 s->incompatible_features & 569 ~QCOW2_INCOMPAT_MASK); 570 ret = -ENOTSUP; 571 g_free(feature_table); 572 goto fail; 573 } 574 575 if (s->incompatible_features & QCOW2_INCOMPAT_CORRUPT) { 576 /* Corrupt images may not be written to unless they are being repaired 577 */ 578 if ((flags & BDRV_O_RDWR) && !(flags & BDRV_O_CHECK)) { 579 error_setg(errp, "qcow2: Image is corrupt; cannot be opened " 580 "read/write"); 581 ret = -EACCES; 582 goto fail; 583 } 584 } 585 586 /* Check support for various header values */ 587 if (header.refcount_order != 4) { 588 report_unsupported(bs, errp, "%d bit reference counts", 589 1 << header.refcount_order); 590 ret = -ENOTSUP; 591 goto fail; 592 } 593 s->refcount_order = header.refcount_order; 594 595 if (header.crypt_method > QCOW_CRYPT_AES) { 596 error_setg(errp, "Unsupported encryption method: %" PRIu32, 597 header.crypt_method); 598 ret = -EINVAL; 599 goto fail; 600 } 601 s->crypt_method_header = header.crypt_method; 602 if (s->crypt_method_header) { 603 bs->encrypted = 1; 604 } 605 606 s->l2_bits = s->cluster_bits - 3; /* L2 is always one cluster */ 607 s->l2_size = 1 << s->l2_bits; 608 bs->total_sectors = header.size / 512; 609 s->csize_shift = (62 - (s->cluster_bits - 8)); 610 s->csize_mask = (1 << (s->cluster_bits - 8)) - 1; 611 s->cluster_offset_mask = (1LL << s->csize_shift) - 1; 612 613 s->refcount_table_offset = header.refcount_table_offset; 614 s->refcount_table_size = 615 header.refcount_table_clusters << (s->cluster_bits - 3); 616 617 if (header.refcount_table_clusters > qcow2_max_refcount_clusters(s)) { 618 error_setg(errp, "Reference count table too large"); 619 ret = -EINVAL; 620 goto fail; 621 } 622 623 ret = validate_table_offset(bs, s->refcount_table_offset, 624 s->refcount_table_size, sizeof(uint64_t)); 625 if (ret < 0) { 626 error_setg(errp, "Invalid reference count table offset"); 627 goto fail; 628 } 629 630 /* Snapshot table offset/length */ 631 if (header.nb_snapshots > QCOW_MAX_SNAPSHOTS) { 632 error_setg(errp, "Too many snapshots"); 633 ret = -EINVAL; 634 goto fail; 635 } 636 637 ret = validate_table_offset(bs, header.snapshots_offset, 638 header.nb_snapshots, 639 sizeof(QCowSnapshotHeader)); 640 if (ret < 0) { 641 error_setg(errp, "Invalid snapshot table offset"); 642 goto fail; 643 } 644 645 /* read the level 1 table */ 646 if (header.l1_size > QCOW_MAX_L1_SIZE) { 647 error_setg(errp, "Active L1 table too large"); 648 ret = -EFBIG; 649 goto fail; 650 } 651 s->l1_size = header.l1_size; 652 653 l1_vm_state_index = size_to_l1(s, header.size); 654 if (l1_vm_state_index > INT_MAX) { 655 error_setg(errp, "Image is too big"); 656 ret = -EFBIG; 657 goto fail; 658 } 659 s->l1_vm_state_index = l1_vm_state_index; 660 661 /* the L1 table must contain at least enough entries to put 662 header.size bytes */ 663 if (s->l1_size < s->l1_vm_state_index) { 664 error_setg(errp, "L1 table is too small"); 665 ret = -EINVAL; 666 goto fail; 667 } 668 669 ret = validate_table_offset(bs, header.l1_table_offset, 670 header.l1_size, sizeof(uint64_t)); 671 if (ret < 0) { 672 error_setg(errp, "Invalid L1 table offset"); 673 goto fail; 674 } 675 s->l1_table_offset = header.l1_table_offset; 676 677 678 if (s->l1_size > 0) { 679 s->l1_table = g_malloc0( 680 align_offset(s->l1_size * sizeof(uint64_t), 512)); 681 ret = bdrv_pread(bs->file, s->l1_table_offset, s->l1_table, 682 s->l1_size * sizeof(uint64_t)); 683 if (ret < 0) { 684 error_setg_errno(errp, -ret, "Could not read L1 table"); 685 goto fail; 686 } 687 for(i = 0;i < s->l1_size; i++) { 688 be64_to_cpus(&s->l1_table[i]); 689 } 690 } 691 692 /* alloc L2 table/refcount block cache */ 693 s->l2_table_cache = qcow2_cache_create(bs, L2_CACHE_SIZE); 694 s->refcount_block_cache = qcow2_cache_create(bs, REFCOUNT_CACHE_SIZE); 695 696 s->cluster_cache = g_malloc(s->cluster_size); 697 /* one more sector for decompressed data alignment */ 698 s->cluster_data = qemu_blockalign(bs, QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size 699 + 512); 700 s->cluster_cache_offset = -1; 701 s->flags = flags; 702 703 ret = qcow2_refcount_init(bs); 704 if (ret != 0) { 705 error_setg_errno(errp, -ret, "Could not initialize refcount handling"); 706 goto fail; 707 } 708 709 QLIST_INIT(&s->cluster_allocs); 710 QTAILQ_INIT(&s->discards); 711 712 /* read qcow2 extensions */ 713 if (qcow2_read_extensions(bs, header.header_length, ext_end, NULL, 714 &local_err)) { 715 error_propagate(errp, local_err); 716 ret = -EINVAL; 717 goto fail; 718 } 719 720 /* read the backing file name */ 721 if (header.backing_file_offset != 0) { 722 len = header.backing_file_size; 723 if (len > MIN(1023, s->cluster_size - header.backing_file_offset)) { 724 error_setg(errp, "Backing file name too long"); 725 ret = -EINVAL; 726 goto fail; 727 } 728 ret = bdrv_pread(bs->file, header.backing_file_offset, 729 bs->backing_file, len); 730 if (ret < 0) { 731 error_setg_errno(errp, -ret, "Could not read backing file name"); 732 goto fail; 733 } 734 bs->backing_file[len] = '\0'; 735 } 736 737 /* Internal snapshots */ 738 s->snapshots_offset = header.snapshots_offset; 739 s->nb_snapshots = header.nb_snapshots; 740 741 ret = qcow2_read_snapshots(bs); 742 if (ret < 0) { 743 error_setg_errno(errp, -ret, "Could not read snapshots"); 744 goto fail; 745 } 746 747 /* Clear unknown autoclear feature bits */ 748 if (!bs->read_only && !(flags & BDRV_O_INCOMING) && s->autoclear_features) { 749 s->autoclear_features = 0; 750 ret = qcow2_update_header(bs); 751 if (ret < 0) { 752 error_setg_errno(errp, -ret, "Could not update qcow2 header"); 753 goto fail; 754 } 755 } 756 757 /* Initialise locks */ 758 qemu_co_mutex_init(&s->lock); 759 760 /* Repair image if dirty */ 761 if (!(flags & (BDRV_O_CHECK | BDRV_O_INCOMING)) && !bs->read_only && 762 (s->incompatible_features & QCOW2_INCOMPAT_DIRTY)) { 763 BdrvCheckResult result = {0}; 764 765 ret = qcow2_check(bs, &result, BDRV_FIX_ERRORS); 766 if (ret < 0) { 767 error_setg_errno(errp, -ret, "Could not repair dirty image"); 768 goto fail; 769 } 770 } 771 772 /* Enable lazy_refcounts according to image and command line options */ 773 opts = qemu_opts_create(&qcow2_runtime_opts, NULL, 0, &error_abort); 774 qemu_opts_absorb_qdict(opts, options, &local_err); 775 if (local_err) { 776 error_propagate(errp, local_err); 777 ret = -EINVAL; 778 goto fail; 779 } 780 781 s->use_lazy_refcounts = qemu_opt_get_bool(opts, QCOW2_OPT_LAZY_REFCOUNTS, 782 (s->compatible_features & QCOW2_COMPAT_LAZY_REFCOUNTS)); 783 784 s->discard_passthrough[QCOW2_DISCARD_NEVER] = false; 785 s->discard_passthrough[QCOW2_DISCARD_ALWAYS] = true; 786 s->discard_passthrough[QCOW2_DISCARD_REQUEST] = 787 qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_REQUEST, 788 flags & BDRV_O_UNMAP); 789 s->discard_passthrough[QCOW2_DISCARD_SNAPSHOT] = 790 qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_SNAPSHOT, true); 791 s->discard_passthrough[QCOW2_DISCARD_OTHER] = 792 qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_OTHER, false); 793 794 opt_overlap_check = qemu_opt_get(opts, "overlap-check") ?: "cached"; 795 if (!strcmp(opt_overlap_check, "none")) { 796 overlap_check_template = 0; 797 } else if (!strcmp(opt_overlap_check, "constant")) { 798 overlap_check_template = QCOW2_OL_CONSTANT; 799 } else if (!strcmp(opt_overlap_check, "cached")) { 800 overlap_check_template = QCOW2_OL_CACHED; 801 } else if (!strcmp(opt_overlap_check, "all")) { 802 overlap_check_template = QCOW2_OL_ALL; 803 } else { 804 error_setg(errp, "Unsupported value '%s' for qcow2 option " 805 "'overlap-check'. Allowed are either of the following: " 806 "none, constant, cached, all", opt_overlap_check); 807 qemu_opts_del(opts); 808 ret = -EINVAL; 809 goto fail; 810 } 811 812 s->overlap_check = 0; 813 for (i = 0; i < QCOW2_OL_MAX_BITNR; i++) { 814 /* overlap-check defines a template bitmask, but every flag may be 815 * overwritten through the associated boolean option */ 816 s->overlap_check |= 817 qemu_opt_get_bool(opts, overlap_bool_option_names[i], 818 overlap_check_template & (1 << i)) << i; 819 } 820 821 qemu_opts_del(opts); 822 823 if (s->use_lazy_refcounts && s->qcow_version < 3) { 824 error_setg(errp, "Lazy refcounts require a qcow2 image with at least " 825 "qemu 1.1 compatibility level"); 826 ret = -EINVAL; 827 goto fail; 828 } 829 830 #ifdef DEBUG_ALLOC 831 { 832 BdrvCheckResult result = {0}; 833 qcow2_check_refcounts(bs, &result, 0); 834 } 835 #endif 836 return ret; 837 838 fail: 839 g_free(s->unknown_header_fields); 840 cleanup_unknown_header_ext(bs); 841 qcow2_free_snapshots(bs); 842 qcow2_refcount_close(bs); 843 g_free(s->l1_table); 844 /* else pre-write overlap checks in cache_destroy may crash */ 845 s->l1_table = NULL; 846 if (s->l2_table_cache) { 847 qcow2_cache_destroy(bs, s->l2_table_cache); 848 } 849 if (s->refcount_block_cache) { 850 qcow2_cache_destroy(bs, s->refcount_block_cache); 851 } 852 g_free(s->cluster_cache); 853 qemu_vfree(s->cluster_data); 854 return ret; 855 } 856 857 static int qcow2_refresh_limits(BlockDriverState *bs) 858 { 859 BDRVQcowState *s = bs->opaque; 860 861 bs->bl.write_zeroes_alignment = s->cluster_sectors; 862 863 return 0; 864 } 865 866 static int qcow2_set_key(BlockDriverState *bs, const char *key) 867 { 868 BDRVQcowState *s = bs->opaque; 869 uint8_t keybuf[16]; 870 int len, i; 871 872 memset(keybuf, 0, 16); 873 len = strlen(key); 874 if (len > 16) 875 len = 16; 876 /* XXX: we could compress the chars to 7 bits to increase 877 entropy */ 878 for(i = 0;i < len;i++) { 879 keybuf[i] = key[i]; 880 } 881 s->crypt_method = s->crypt_method_header; 882 883 if (AES_set_encrypt_key(keybuf, 128, &s->aes_encrypt_key) != 0) 884 return -1; 885 if (AES_set_decrypt_key(keybuf, 128, &s->aes_decrypt_key) != 0) 886 return -1; 887 #if 0 888 /* test */ 889 { 890 uint8_t in[16]; 891 uint8_t out[16]; 892 uint8_t tmp[16]; 893 for(i=0;i<16;i++) 894 in[i] = i; 895 AES_encrypt(in, tmp, &s->aes_encrypt_key); 896 AES_decrypt(tmp, out, &s->aes_decrypt_key); 897 for(i = 0; i < 16; i++) 898 printf(" %02x", tmp[i]); 899 printf("\n"); 900 for(i = 0; i < 16; i++) 901 printf(" %02x", out[i]); 902 printf("\n"); 903 } 904 #endif 905 return 0; 906 } 907 908 /* We have no actual commit/abort logic for qcow2, but we need to write out any 909 * unwritten data if we reopen read-only. */ 910 static int qcow2_reopen_prepare(BDRVReopenState *state, 911 BlockReopenQueue *queue, Error **errp) 912 { 913 int ret; 914 915 if ((state->flags & BDRV_O_RDWR) == 0) { 916 ret = bdrv_flush(state->bs); 917 if (ret < 0) { 918 return ret; 919 } 920 921 ret = qcow2_mark_clean(state->bs); 922 if (ret < 0) { 923 return ret; 924 } 925 } 926 927 return 0; 928 } 929 930 static int64_t coroutine_fn qcow2_co_get_block_status(BlockDriverState *bs, 931 int64_t sector_num, int nb_sectors, int *pnum) 932 { 933 BDRVQcowState *s = bs->opaque; 934 uint64_t cluster_offset; 935 int index_in_cluster, ret; 936 int64_t status = 0; 937 938 *pnum = nb_sectors; 939 qemu_co_mutex_lock(&s->lock); 940 ret = qcow2_get_cluster_offset(bs, sector_num << 9, pnum, &cluster_offset); 941 qemu_co_mutex_unlock(&s->lock); 942 if (ret < 0) { 943 return ret; 944 } 945 946 if (cluster_offset != 0 && ret != QCOW2_CLUSTER_COMPRESSED && 947 !s->crypt_method) { 948 index_in_cluster = sector_num & (s->cluster_sectors - 1); 949 cluster_offset |= (index_in_cluster << BDRV_SECTOR_BITS); 950 status |= BDRV_BLOCK_OFFSET_VALID | cluster_offset; 951 } 952 if (ret == QCOW2_CLUSTER_ZERO) { 953 status |= BDRV_BLOCK_ZERO; 954 } else if (ret != QCOW2_CLUSTER_UNALLOCATED) { 955 status |= BDRV_BLOCK_DATA; 956 } 957 return status; 958 } 959 960 /* handle reading after the end of the backing file */ 961 int qcow2_backing_read1(BlockDriverState *bs, QEMUIOVector *qiov, 962 int64_t sector_num, int nb_sectors) 963 { 964 int n1; 965 if ((sector_num + nb_sectors) <= bs->total_sectors) 966 return nb_sectors; 967 if (sector_num >= bs->total_sectors) 968 n1 = 0; 969 else 970 n1 = bs->total_sectors - sector_num; 971 972 qemu_iovec_memset(qiov, 512 * n1, 0, 512 * (nb_sectors - n1)); 973 974 return n1; 975 } 976 977 static coroutine_fn int qcow2_co_readv(BlockDriverState *bs, int64_t sector_num, 978 int remaining_sectors, QEMUIOVector *qiov) 979 { 980 BDRVQcowState *s = bs->opaque; 981 int index_in_cluster, n1; 982 int ret; 983 int cur_nr_sectors; /* number of sectors in current iteration */ 984 uint64_t cluster_offset = 0; 985 uint64_t bytes_done = 0; 986 QEMUIOVector hd_qiov; 987 uint8_t *cluster_data = NULL; 988 989 qemu_iovec_init(&hd_qiov, qiov->niov); 990 991 qemu_co_mutex_lock(&s->lock); 992 993 while (remaining_sectors != 0) { 994 995 /* prepare next request */ 996 cur_nr_sectors = remaining_sectors; 997 if (s->crypt_method) { 998 cur_nr_sectors = MIN(cur_nr_sectors, 999 QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors); 1000 } 1001 1002 ret = qcow2_get_cluster_offset(bs, sector_num << 9, 1003 &cur_nr_sectors, &cluster_offset); 1004 if (ret < 0) { 1005 goto fail; 1006 } 1007 1008 index_in_cluster = sector_num & (s->cluster_sectors - 1); 1009 1010 qemu_iovec_reset(&hd_qiov); 1011 qemu_iovec_concat(&hd_qiov, qiov, bytes_done, 1012 cur_nr_sectors * 512); 1013 1014 switch (ret) { 1015 case QCOW2_CLUSTER_UNALLOCATED: 1016 1017 if (bs->backing_hd) { 1018 /* read from the base image */ 1019 n1 = qcow2_backing_read1(bs->backing_hd, &hd_qiov, 1020 sector_num, cur_nr_sectors); 1021 if (n1 > 0) { 1022 BLKDBG_EVENT(bs->file, BLKDBG_READ_BACKING_AIO); 1023 qemu_co_mutex_unlock(&s->lock); 1024 ret = bdrv_co_readv(bs->backing_hd, sector_num, 1025 n1, &hd_qiov); 1026 qemu_co_mutex_lock(&s->lock); 1027 if (ret < 0) { 1028 goto fail; 1029 } 1030 } 1031 } else { 1032 /* Note: in this case, no need to wait */ 1033 qemu_iovec_memset(&hd_qiov, 0, 0, 512 * cur_nr_sectors); 1034 } 1035 break; 1036 1037 case QCOW2_CLUSTER_ZERO: 1038 qemu_iovec_memset(&hd_qiov, 0, 0, 512 * cur_nr_sectors); 1039 break; 1040 1041 case QCOW2_CLUSTER_COMPRESSED: 1042 /* add AIO support for compressed blocks ? */ 1043 ret = qcow2_decompress_cluster(bs, cluster_offset); 1044 if (ret < 0) { 1045 goto fail; 1046 } 1047 1048 qemu_iovec_from_buf(&hd_qiov, 0, 1049 s->cluster_cache + index_in_cluster * 512, 1050 512 * cur_nr_sectors); 1051 break; 1052 1053 case QCOW2_CLUSTER_NORMAL: 1054 if ((cluster_offset & 511) != 0) { 1055 ret = -EIO; 1056 goto fail; 1057 } 1058 1059 if (s->crypt_method) { 1060 /* 1061 * For encrypted images, read everything into a temporary 1062 * contiguous buffer on which the AES functions can work. 1063 */ 1064 if (!cluster_data) { 1065 cluster_data = 1066 qemu_blockalign(bs, QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size); 1067 } 1068 1069 assert(cur_nr_sectors <= 1070 QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors); 1071 qemu_iovec_reset(&hd_qiov); 1072 qemu_iovec_add(&hd_qiov, cluster_data, 1073 512 * cur_nr_sectors); 1074 } 1075 1076 BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO); 1077 qemu_co_mutex_unlock(&s->lock); 1078 ret = bdrv_co_readv(bs->file, 1079 (cluster_offset >> 9) + index_in_cluster, 1080 cur_nr_sectors, &hd_qiov); 1081 qemu_co_mutex_lock(&s->lock); 1082 if (ret < 0) { 1083 goto fail; 1084 } 1085 if (s->crypt_method) { 1086 qcow2_encrypt_sectors(s, sector_num, cluster_data, 1087 cluster_data, cur_nr_sectors, 0, &s->aes_decrypt_key); 1088 qemu_iovec_from_buf(qiov, bytes_done, 1089 cluster_data, 512 * cur_nr_sectors); 1090 } 1091 break; 1092 1093 default: 1094 g_assert_not_reached(); 1095 ret = -EIO; 1096 goto fail; 1097 } 1098 1099 remaining_sectors -= cur_nr_sectors; 1100 sector_num += cur_nr_sectors; 1101 bytes_done += cur_nr_sectors * 512; 1102 } 1103 ret = 0; 1104 1105 fail: 1106 qemu_co_mutex_unlock(&s->lock); 1107 1108 qemu_iovec_destroy(&hd_qiov); 1109 qemu_vfree(cluster_data); 1110 1111 return ret; 1112 } 1113 1114 static coroutine_fn int qcow2_co_writev(BlockDriverState *bs, 1115 int64_t sector_num, 1116 int remaining_sectors, 1117 QEMUIOVector *qiov) 1118 { 1119 BDRVQcowState *s = bs->opaque; 1120 int index_in_cluster; 1121 int ret; 1122 int cur_nr_sectors; /* number of sectors in current iteration */ 1123 uint64_t cluster_offset; 1124 QEMUIOVector hd_qiov; 1125 uint64_t bytes_done = 0; 1126 uint8_t *cluster_data = NULL; 1127 QCowL2Meta *l2meta = NULL; 1128 1129 trace_qcow2_writev_start_req(qemu_coroutine_self(), sector_num, 1130 remaining_sectors); 1131 1132 qemu_iovec_init(&hd_qiov, qiov->niov); 1133 1134 s->cluster_cache_offset = -1; /* disable compressed cache */ 1135 1136 qemu_co_mutex_lock(&s->lock); 1137 1138 while (remaining_sectors != 0) { 1139 1140 l2meta = NULL; 1141 1142 trace_qcow2_writev_start_part(qemu_coroutine_self()); 1143 index_in_cluster = sector_num & (s->cluster_sectors - 1); 1144 cur_nr_sectors = remaining_sectors; 1145 if (s->crypt_method && 1146 cur_nr_sectors > 1147 QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors - index_in_cluster) { 1148 cur_nr_sectors = 1149 QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors - index_in_cluster; 1150 } 1151 1152 ret = qcow2_alloc_cluster_offset(bs, sector_num << 9, 1153 &cur_nr_sectors, &cluster_offset, &l2meta); 1154 if (ret < 0) { 1155 goto fail; 1156 } 1157 1158 assert((cluster_offset & 511) == 0); 1159 1160 qemu_iovec_reset(&hd_qiov); 1161 qemu_iovec_concat(&hd_qiov, qiov, bytes_done, 1162 cur_nr_sectors * 512); 1163 1164 if (s->crypt_method) { 1165 if (!cluster_data) { 1166 cluster_data = qemu_blockalign(bs, QCOW_MAX_CRYPT_CLUSTERS * 1167 s->cluster_size); 1168 } 1169 1170 assert(hd_qiov.size <= 1171 QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size); 1172 qemu_iovec_to_buf(&hd_qiov, 0, cluster_data, hd_qiov.size); 1173 1174 qcow2_encrypt_sectors(s, sector_num, cluster_data, 1175 cluster_data, cur_nr_sectors, 1, &s->aes_encrypt_key); 1176 1177 qemu_iovec_reset(&hd_qiov); 1178 qemu_iovec_add(&hd_qiov, cluster_data, 1179 cur_nr_sectors * 512); 1180 } 1181 1182 ret = qcow2_pre_write_overlap_check(bs, 0, 1183 cluster_offset + index_in_cluster * BDRV_SECTOR_SIZE, 1184 cur_nr_sectors * BDRV_SECTOR_SIZE); 1185 if (ret < 0) { 1186 goto fail; 1187 } 1188 1189 qemu_co_mutex_unlock(&s->lock); 1190 BLKDBG_EVENT(bs->file, BLKDBG_WRITE_AIO); 1191 trace_qcow2_writev_data(qemu_coroutine_self(), 1192 (cluster_offset >> 9) + index_in_cluster); 1193 ret = bdrv_co_writev(bs->file, 1194 (cluster_offset >> 9) + index_in_cluster, 1195 cur_nr_sectors, &hd_qiov); 1196 qemu_co_mutex_lock(&s->lock); 1197 if (ret < 0) { 1198 goto fail; 1199 } 1200 1201 while (l2meta != NULL) { 1202 QCowL2Meta *next; 1203 1204 ret = qcow2_alloc_cluster_link_l2(bs, l2meta); 1205 if (ret < 0) { 1206 goto fail; 1207 } 1208 1209 /* Take the request off the list of running requests */ 1210 if (l2meta->nb_clusters != 0) { 1211 QLIST_REMOVE(l2meta, next_in_flight); 1212 } 1213 1214 qemu_co_queue_restart_all(&l2meta->dependent_requests); 1215 1216 next = l2meta->next; 1217 g_free(l2meta); 1218 l2meta = next; 1219 } 1220 1221 remaining_sectors -= cur_nr_sectors; 1222 sector_num += cur_nr_sectors; 1223 bytes_done += cur_nr_sectors * 512; 1224 trace_qcow2_writev_done_part(qemu_coroutine_self(), cur_nr_sectors); 1225 } 1226 ret = 0; 1227 1228 fail: 1229 qemu_co_mutex_unlock(&s->lock); 1230 1231 while (l2meta != NULL) { 1232 QCowL2Meta *next; 1233 1234 if (l2meta->nb_clusters != 0) { 1235 QLIST_REMOVE(l2meta, next_in_flight); 1236 } 1237 qemu_co_queue_restart_all(&l2meta->dependent_requests); 1238 1239 next = l2meta->next; 1240 g_free(l2meta); 1241 l2meta = next; 1242 } 1243 1244 qemu_iovec_destroy(&hd_qiov); 1245 qemu_vfree(cluster_data); 1246 trace_qcow2_writev_done_req(qemu_coroutine_self(), ret); 1247 1248 return ret; 1249 } 1250 1251 static void qcow2_close(BlockDriverState *bs) 1252 { 1253 BDRVQcowState *s = bs->opaque; 1254 g_free(s->l1_table); 1255 /* else pre-write overlap checks in cache_destroy may crash */ 1256 s->l1_table = NULL; 1257 1258 if (!(bs->open_flags & BDRV_O_INCOMING)) { 1259 qcow2_cache_flush(bs, s->l2_table_cache); 1260 qcow2_cache_flush(bs, s->refcount_block_cache); 1261 1262 qcow2_mark_clean(bs); 1263 } 1264 1265 qcow2_cache_destroy(bs, s->l2_table_cache); 1266 qcow2_cache_destroy(bs, s->refcount_block_cache); 1267 1268 g_free(s->unknown_header_fields); 1269 cleanup_unknown_header_ext(bs); 1270 1271 g_free(s->cluster_cache); 1272 qemu_vfree(s->cluster_data); 1273 qcow2_refcount_close(bs); 1274 qcow2_free_snapshots(bs); 1275 } 1276 1277 static void qcow2_invalidate_cache(BlockDriverState *bs, Error **errp) 1278 { 1279 BDRVQcowState *s = bs->opaque; 1280 int flags = s->flags; 1281 AES_KEY aes_encrypt_key; 1282 AES_KEY aes_decrypt_key; 1283 uint32_t crypt_method = 0; 1284 QDict *options; 1285 Error *local_err = NULL; 1286 int ret; 1287 1288 /* 1289 * Backing files are read-only which makes all of their metadata immutable, 1290 * that means we don't have to worry about reopening them here. 1291 */ 1292 1293 if (s->crypt_method) { 1294 crypt_method = s->crypt_method; 1295 memcpy(&aes_encrypt_key, &s->aes_encrypt_key, sizeof(aes_encrypt_key)); 1296 memcpy(&aes_decrypt_key, &s->aes_decrypt_key, sizeof(aes_decrypt_key)); 1297 } 1298 1299 qcow2_close(bs); 1300 1301 bdrv_invalidate_cache(bs->file, &local_err); 1302 if (local_err) { 1303 error_propagate(errp, local_err); 1304 return; 1305 } 1306 1307 memset(s, 0, sizeof(BDRVQcowState)); 1308 options = qdict_clone_shallow(bs->options); 1309 1310 ret = qcow2_open(bs, options, flags, &local_err); 1311 QDECREF(options); 1312 if (local_err) { 1313 error_setg(errp, "Could not reopen qcow2 layer: %s", 1314 error_get_pretty(local_err)); 1315 error_free(local_err); 1316 return; 1317 } else if (ret < 0) { 1318 error_setg_errno(errp, -ret, "Could not reopen qcow2 layer"); 1319 return; 1320 } 1321 1322 if (crypt_method) { 1323 s->crypt_method = crypt_method; 1324 memcpy(&s->aes_encrypt_key, &aes_encrypt_key, sizeof(aes_encrypt_key)); 1325 memcpy(&s->aes_decrypt_key, &aes_decrypt_key, sizeof(aes_decrypt_key)); 1326 } 1327 } 1328 1329 static size_t header_ext_add(char *buf, uint32_t magic, const void *s, 1330 size_t len, size_t buflen) 1331 { 1332 QCowExtension *ext_backing_fmt = (QCowExtension*) buf; 1333 size_t ext_len = sizeof(QCowExtension) + ((len + 7) & ~7); 1334 1335 if (buflen < ext_len) { 1336 return -ENOSPC; 1337 } 1338 1339 *ext_backing_fmt = (QCowExtension) { 1340 .magic = cpu_to_be32(magic), 1341 .len = cpu_to_be32(len), 1342 }; 1343 memcpy(buf + sizeof(QCowExtension), s, len); 1344 1345 return ext_len; 1346 } 1347 1348 /* 1349 * Updates the qcow2 header, including the variable length parts of it, i.e. 1350 * the backing file name and all extensions. qcow2 was not designed to allow 1351 * such changes, so if we run out of space (we can only use the first cluster) 1352 * this function may fail. 1353 * 1354 * Returns 0 on success, -errno in error cases. 1355 */ 1356 int qcow2_update_header(BlockDriverState *bs) 1357 { 1358 BDRVQcowState *s = bs->opaque; 1359 QCowHeader *header; 1360 char *buf; 1361 size_t buflen = s->cluster_size; 1362 int ret; 1363 uint64_t total_size; 1364 uint32_t refcount_table_clusters; 1365 size_t header_length; 1366 Qcow2UnknownHeaderExtension *uext; 1367 1368 buf = qemu_blockalign(bs, buflen); 1369 1370 /* Header structure */ 1371 header = (QCowHeader*) buf; 1372 1373 if (buflen < sizeof(*header)) { 1374 ret = -ENOSPC; 1375 goto fail; 1376 } 1377 1378 header_length = sizeof(*header) + s->unknown_header_fields_size; 1379 total_size = bs->total_sectors * BDRV_SECTOR_SIZE; 1380 refcount_table_clusters = s->refcount_table_size >> (s->cluster_bits - 3); 1381 1382 *header = (QCowHeader) { 1383 /* Version 2 fields */ 1384 .magic = cpu_to_be32(QCOW_MAGIC), 1385 .version = cpu_to_be32(s->qcow_version), 1386 .backing_file_offset = 0, 1387 .backing_file_size = 0, 1388 .cluster_bits = cpu_to_be32(s->cluster_bits), 1389 .size = cpu_to_be64(total_size), 1390 .crypt_method = cpu_to_be32(s->crypt_method_header), 1391 .l1_size = cpu_to_be32(s->l1_size), 1392 .l1_table_offset = cpu_to_be64(s->l1_table_offset), 1393 .refcount_table_offset = cpu_to_be64(s->refcount_table_offset), 1394 .refcount_table_clusters = cpu_to_be32(refcount_table_clusters), 1395 .nb_snapshots = cpu_to_be32(s->nb_snapshots), 1396 .snapshots_offset = cpu_to_be64(s->snapshots_offset), 1397 1398 /* Version 3 fields */ 1399 .incompatible_features = cpu_to_be64(s->incompatible_features), 1400 .compatible_features = cpu_to_be64(s->compatible_features), 1401 .autoclear_features = cpu_to_be64(s->autoclear_features), 1402 .refcount_order = cpu_to_be32(s->refcount_order), 1403 .header_length = cpu_to_be32(header_length), 1404 }; 1405 1406 /* For older versions, write a shorter header */ 1407 switch (s->qcow_version) { 1408 case 2: 1409 ret = offsetof(QCowHeader, incompatible_features); 1410 break; 1411 case 3: 1412 ret = sizeof(*header); 1413 break; 1414 default: 1415 ret = -EINVAL; 1416 goto fail; 1417 } 1418 1419 buf += ret; 1420 buflen -= ret; 1421 memset(buf, 0, buflen); 1422 1423 /* Preserve any unknown field in the header */ 1424 if (s->unknown_header_fields_size) { 1425 if (buflen < s->unknown_header_fields_size) { 1426 ret = -ENOSPC; 1427 goto fail; 1428 } 1429 1430 memcpy(buf, s->unknown_header_fields, s->unknown_header_fields_size); 1431 buf += s->unknown_header_fields_size; 1432 buflen -= s->unknown_header_fields_size; 1433 } 1434 1435 /* Backing file format header extension */ 1436 if (*bs->backing_format) { 1437 ret = header_ext_add(buf, QCOW2_EXT_MAGIC_BACKING_FORMAT, 1438 bs->backing_format, strlen(bs->backing_format), 1439 buflen); 1440 if (ret < 0) { 1441 goto fail; 1442 } 1443 1444 buf += ret; 1445 buflen -= ret; 1446 } 1447 1448 /* Feature table */ 1449 Qcow2Feature features[] = { 1450 { 1451 .type = QCOW2_FEAT_TYPE_INCOMPATIBLE, 1452 .bit = QCOW2_INCOMPAT_DIRTY_BITNR, 1453 .name = "dirty bit", 1454 }, 1455 { 1456 .type = QCOW2_FEAT_TYPE_INCOMPATIBLE, 1457 .bit = QCOW2_INCOMPAT_CORRUPT_BITNR, 1458 .name = "corrupt bit", 1459 }, 1460 { 1461 .type = QCOW2_FEAT_TYPE_COMPATIBLE, 1462 .bit = QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR, 1463 .name = "lazy refcounts", 1464 }, 1465 }; 1466 1467 ret = header_ext_add(buf, QCOW2_EXT_MAGIC_FEATURE_TABLE, 1468 features, sizeof(features), buflen); 1469 if (ret < 0) { 1470 goto fail; 1471 } 1472 buf += ret; 1473 buflen -= ret; 1474 1475 /* Keep unknown header extensions */ 1476 QLIST_FOREACH(uext, &s->unknown_header_ext, next) { 1477 ret = header_ext_add(buf, uext->magic, uext->data, uext->len, buflen); 1478 if (ret < 0) { 1479 goto fail; 1480 } 1481 1482 buf += ret; 1483 buflen -= ret; 1484 } 1485 1486 /* End of header extensions */ 1487 ret = header_ext_add(buf, QCOW2_EXT_MAGIC_END, NULL, 0, buflen); 1488 if (ret < 0) { 1489 goto fail; 1490 } 1491 1492 buf += ret; 1493 buflen -= ret; 1494 1495 /* Backing file name */ 1496 if (*bs->backing_file) { 1497 size_t backing_file_len = strlen(bs->backing_file); 1498 1499 if (buflen < backing_file_len) { 1500 ret = -ENOSPC; 1501 goto fail; 1502 } 1503 1504 /* Using strncpy is ok here, since buf is not NUL-terminated. */ 1505 strncpy(buf, bs->backing_file, buflen); 1506 1507 header->backing_file_offset = cpu_to_be64(buf - ((char*) header)); 1508 header->backing_file_size = cpu_to_be32(backing_file_len); 1509 } 1510 1511 /* Write the new header */ 1512 ret = bdrv_pwrite(bs->file, 0, header, s->cluster_size); 1513 if (ret < 0) { 1514 goto fail; 1515 } 1516 1517 ret = 0; 1518 fail: 1519 qemu_vfree(header); 1520 return ret; 1521 } 1522 1523 static int qcow2_change_backing_file(BlockDriverState *bs, 1524 const char *backing_file, const char *backing_fmt) 1525 { 1526 pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: ""); 1527 pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: ""); 1528 1529 return qcow2_update_header(bs); 1530 } 1531 1532 static int preallocate(BlockDriverState *bs) 1533 { 1534 uint64_t nb_sectors; 1535 uint64_t offset; 1536 uint64_t host_offset = 0; 1537 int num; 1538 int ret; 1539 QCowL2Meta *meta; 1540 1541 nb_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS; 1542 offset = 0; 1543 1544 while (nb_sectors) { 1545 num = MIN(nb_sectors, INT_MAX >> BDRV_SECTOR_BITS); 1546 ret = qcow2_alloc_cluster_offset(bs, offset, &num, 1547 &host_offset, &meta); 1548 if (ret < 0) { 1549 return ret; 1550 } 1551 1552 while (meta) { 1553 QCowL2Meta *next = meta->next; 1554 1555 ret = qcow2_alloc_cluster_link_l2(bs, meta); 1556 if (ret < 0) { 1557 qcow2_free_any_clusters(bs, meta->alloc_offset, 1558 meta->nb_clusters, QCOW2_DISCARD_NEVER); 1559 return ret; 1560 } 1561 1562 /* There are no dependent requests, but we need to remove our 1563 * request from the list of in-flight requests */ 1564 QLIST_REMOVE(meta, next_in_flight); 1565 1566 g_free(meta); 1567 meta = next; 1568 } 1569 1570 /* TODO Preallocate data if requested */ 1571 1572 nb_sectors -= num; 1573 offset += num << BDRV_SECTOR_BITS; 1574 } 1575 1576 /* 1577 * It is expected that the image file is large enough to actually contain 1578 * all of the allocated clusters (otherwise we get failing reads after 1579 * EOF). Extend the image to the last allocated sector. 1580 */ 1581 if (host_offset != 0) { 1582 uint8_t buf[BDRV_SECTOR_SIZE]; 1583 memset(buf, 0, BDRV_SECTOR_SIZE); 1584 ret = bdrv_write(bs->file, (host_offset >> BDRV_SECTOR_BITS) + num - 1, 1585 buf, 1); 1586 if (ret < 0) { 1587 return ret; 1588 } 1589 } 1590 1591 return 0; 1592 } 1593 1594 static int qcow2_create2(const char *filename, int64_t total_size, 1595 const char *backing_file, const char *backing_format, 1596 int flags, size_t cluster_size, int prealloc, 1597 QEMUOptionParameter *options, int version, 1598 Error **errp) 1599 { 1600 /* Calculate cluster_bits */ 1601 int cluster_bits; 1602 cluster_bits = ffs(cluster_size) - 1; 1603 if (cluster_bits < MIN_CLUSTER_BITS || cluster_bits > MAX_CLUSTER_BITS || 1604 (1 << cluster_bits) != cluster_size) 1605 { 1606 error_setg(errp, "Cluster size must be a power of two between %d and " 1607 "%dk", 1 << MIN_CLUSTER_BITS, 1 << (MAX_CLUSTER_BITS - 10)); 1608 return -EINVAL; 1609 } 1610 1611 /* 1612 * Open the image file and write a minimal qcow2 header. 1613 * 1614 * We keep things simple and start with a zero-sized image. We also 1615 * do without refcount blocks or a L1 table for now. We'll fix the 1616 * inconsistency later. 1617 * 1618 * We do need a refcount table because growing the refcount table means 1619 * allocating two new refcount blocks - the seconds of which would be at 1620 * 2 GB for 64k clusters, and we don't want to have a 2 GB initial file 1621 * size for any qcow2 image. 1622 */ 1623 BlockDriverState* bs; 1624 QCowHeader *header; 1625 uint64_t* refcount_table; 1626 Error *local_err = NULL; 1627 int ret; 1628 1629 ret = bdrv_create_file(filename, options, &local_err); 1630 if (ret < 0) { 1631 error_propagate(errp, local_err); 1632 return ret; 1633 } 1634 1635 bs = NULL; 1636 ret = bdrv_open(&bs, filename, NULL, NULL, BDRV_O_RDWR | BDRV_O_PROTOCOL, 1637 NULL, &local_err); 1638 if (ret < 0) { 1639 error_propagate(errp, local_err); 1640 return ret; 1641 } 1642 1643 /* Write the header */ 1644 QEMU_BUILD_BUG_ON((1 << MIN_CLUSTER_BITS) < sizeof(*header)); 1645 header = g_malloc0(cluster_size); 1646 *header = (QCowHeader) { 1647 .magic = cpu_to_be32(QCOW_MAGIC), 1648 .version = cpu_to_be32(version), 1649 .cluster_bits = cpu_to_be32(cluster_bits), 1650 .size = cpu_to_be64(0), 1651 .l1_table_offset = cpu_to_be64(0), 1652 .l1_size = cpu_to_be32(0), 1653 .refcount_table_offset = cpu_to_be64(cluster_size), 1654 .refcount_table_clusters = cpu_to_be32(1), 1655 .refcount_order = cpu_to_be32(3 + REFCOUNT_SHIFT), 1656 .header_length = cpu_to_be32(sizeof(*header)), 1657 }; 1658 1659 if (flags & BLOCK_FLAG_ENCRYPT) { 1660 header->crypt_method = cpu_to_be32(QCOW_CRYPT_AES); 1661 } else { 1662 header->crypt_method = cpu_to_be32(QCOW_CRYPT_NONE); 1663 } 1664 1665 if (flags & BLOCK_FLAG_LAZY_REFCOUNTS) { 1666 header->compatible_features |= 1667 cpu_to_be64(QCOW2_COMPAT_LAZY_REFCOUNTS); 1668 } 1669 1670 ret = bdrv_pwrite(bs, 0, header, cluster_size); 1671 g_free(header); 1672 if (ret < 0) { 1673 error_setg_errno(errp, -ret, "Could not write qcow2 header"); 1674 goto out; 1675 } 1676 1677 /* Write a refcount table with one refcount block */ 1678 refcount_table = g_malloc0(2 * cluster_size); 1679 refcount_table[0] = cpu_to_be64(2 * cluster_size); 1680 ret = bdrv_pwrite(bs, cluster_size, refcount_table, 2 * cluster_size); 1681 g_free(refcount_table); 1682 1683 if (ret < 0) { 1684 error_setg_errno(errp, -ret, "Could not write refcount table"); 1685 goto out; 1686 } 1687 1688 bdrv_unref(bs); 1689 bs = NULL; 1690 1691 /* 1692 * And now open the image and make it consistent first (i.e. increase the 1693 * refcount of the cluster that is occupied by the header and the refcount 1694 * table) 1695 */ 1696 BlockDriver* drv = bdrv_find_format("qcow2"); 1697 assert(drv != NULL); 1698 ret = bdrv_open(&bs, filename, NULL, NULL, 1699 BDRV_O_RDWR | BDRV_O_CACHE_WB | BDRV_O_NO_FLUSH, drv, &local_err); 1700 if (ret < 0) { 1701 error_propagate(errp, local_err); 1702 goto out; 1703 } 1704 1705 ret = qcow2_alloc_clusters(bs, 3 * cluster_size); 1706 if (ret < 0) { 1707 error_setg_errno(errp, -ret, "Could not allocate clusters for qcow2 " 1708 "header and refcount table"); 1709 goto out; 1710 1711 } else if (ret != 0) { 1712 error_report("Huh, first cluster in empty image is already in use?"); 1713 abort(); 1714 } 1715 1716 /* Okay, now that we have a valid image, let's give it the right size */ 1717 ret = bdrv_truncate(bs, total_size * BDRV_SECTOR_SIZE); 1718 if (ret < 0) { 1719 error_setg_errno(errp, -ret, "Could not resize image"); 1720 goto out; 1721 } 1722 1723 /* Want a backing file? There you go.*/ 1724 if (backing_file) { 1725 ret = bdrv_change_backing_file(bs, backing_file, backing_format); 1726 if (ret < 0) { 1727 error_setg_errno(errp, -ret, "Could not assign backing file '%s' " 1728 "with format '%s'", backing_file, backing_format); 1729 goto out; 1730 } 1731 } 1732 1733 /* And if we're supposed to preallocate metadata, do that now */ 1734 if (prealloc) { 1735 BDRVQcowState *s = bs->opaque; 1736 qemu_co_mutex_lock(&s->lock); 1737 ret = preallocate(bs); 1738 qemu_co_mutex_unlock(&s->lock); 1739 if (ret < 0) { 1740 error_setg_errno(errp, -ret, "Could not preallocate metadata"); 1741 goto out; 1742 } 1743 } 1744 1745 bdrv_unref(bs); 1746 bs = NULL; 1747 1748 /* Reopen the image without BDRV_O_NO_FLUSH to flush it before returning */ 1749 ret = bdrv_open(&bs, filename, NULL, NULL, 1750 BDRV_O_RDWR | BDRV_O_CACHE_WB | BDRV_O_NO_BACKING, 1751 drv, &local_err); 1752 if (local_err) { 1753 error_propagate(errp, local_err); 1754 goto out; 1755 } 1756 1757 ret = 0; 1758 out: 1759 if (bs) { 1760 bdrv_unref(bs); 1761 } 1762 return ret; 1763 } 1764 1765 static int qcow2_create(const char *filename, QEMUOptionParameter *options, 1766 Error **errp) 1767 { 1768 const char *backing_file = NULL; 1769 const char *backing_fmt = NULL; 1770 uint64_t sectors = 0; 1771 int flags = 0; 1772 size_t cluster_size = DEFAULT_CLUSTER_SIZE; 1773 int prealloc = 0; 1774 int version = 3; 1775 Error *local_err = NULL; 1776 int ret; 1777 1778 /* Read out options */ 1779 while (options && options->name) { 1780 if (!strcmp(options->name, BLOCK_OPT_SIZE)) { 1781 sectors = options->value.n / 512; 1782 } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) { 1783 backing_file = options->value.s; 1784 } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FMT)) { 1785 backing_fmt = options->value.s; 1786 } else if (!strcmp(options->name, BLOCK_OPT_ENCRYPT)) { 1787 flags |= options->value.n ? BLOCK_FLAG_ENCRYPT : 0; 1788 } else if (!strcmp(options->name, BLOCK_OPT_CLUSTER_SIZE)) { 1789 if (options->value.n) { 1790 cluster_size = options->value.n; 1791 } 1792 } else if (!strcmp(options->name, BLOCK_OPT_PREALLOC)) { 1793 if (!options->value.s || !strcmp(options->value.s, "off")) { 1794 prealloc = 0; 1795 } else if (!strcmp(options->value.s, "metadata")) { 1796 prealloc = 1; 1797 } else { 1798 error_setg(errp, "Invalid preallocation mode: '%s'", 1799 options->value.s); 1800 return -EINVAL; 1801 } 1802 } else if (!strcmp(options->name, BLOCK_OPT_COMPAT_LEVEL)) { 1803 if (!options->value.s) { 1804 /* keep the default */ 1805 } else if (!strcmp(options->value.s, "0.10")) { 1806 version = 2; 1807 } else if (!strcmp(options->value.s, "1.1")) { 1808 version = 3; 1809 } else { 1810 error_setg(errp, "Invalid compatibility level: '%s'", 1811 options->value.s); 1812 return -EINVAL; 1813 } 1814 } else if (!strcmp(options->name, BLOCK_OPT_LAZY_REFCOUNTS)) { 1815 flags |= options->value.n ? BLOCK_FLAG_LAZY_REFCOUNTS : 0; 1816 } 1817 options++; 1818 } 1819 1820 if (backing_file && prealloc) { 1821 error_setg(errp, "Backing file and preallocation cannot be used at " 1822 "the same time"); 1823 return -EINVAL; 1824 } 1825 1826 if (version < 3 && (flags & BLOCK_FLAG_LAZY_REFCOUNTS)) { 1827 error_setg(errp, "Lazy refcounts only supported with compatibility " 1828 "level 1.1 and above (use compat=1.1 or greater)"); 1829 return -EINVAL; 1830 } 1831 1832 ret = qcow2_create2(filename, sectors, backing_file, backing_fmt, flags, 1833 cluster_size, prealloc, options, version, &local_err); 1834 if (local_err) { 1835 error_propagate(errp, local_err); 1836 } 1837 return ret; 1838 } 1839 1840 static coroutine_fn int qcow2_co_write_zeroes(BlockDriverState *bs, 1841 int64_t sector_num, int nb_sectors, BdrvRequestFlags flags) 1842 { 1843 int ret; 1844 BDRVQcowState *s = bs->opaque; 1845 1846 /* Emulate misaligned zero writes */ 1847 if (sector_num % s->cluster_sectors || nb_sectors % s->cluster_sectors) { 1848 return -ENOTSUP; 1849 } 1850 1851 /* Whatever is left can use real zero clusters */ 1852 qemu_co_mutex_lock(&s->lock); 1853 ret = qcow2_zero_clusters(bs, sector_num << BDRV_SECTOR_BITS, 1854 nb_sectors); 1855 qemu_co_mutex_unlock(&s->lock); 1856 1857 return ret; 1858 } 1859 1860 static coroutine_fn int qcow2_co_discard(BlockDriverState *bs, 1861 int64_t sector_num, int nb_sectors) 1862 { 1863 int ret; 1864 BDRVQcowState *s = bs->opaque; 1865 1866 qemu_co_mutex_lock(&s->lock); 1867 ret = qcow2_discard_clusters(bs, sector_num << BDRV_SECTOR_BITS, 1868 nb_sectors, QCOW2_DISCARD_REQUEST); 1869 qemu_co_mutex_unlock(&s->lock); 1870 return ret; 1871 } 1872 1873 static int qcow2_truncate(BlockDriverState *bs, int64_t offset) 1874 { 1875 BDRVQcowState *s = bs->opaque; 1876 int64_t new_l1_size; 1877 int ret; 1878 1879 if (offset & 511) { 1880 error_report("The new size must be a multiple of 512"); 1881 return -EINVAL; 1882 } 1883 1884 /* cannot proceed if image has snapshots */ 1885 if (s->nb_snapshots) { 1886 error_report("Can't resize an image which has snapshots"); 1887 return -ENOTSUP; 1888 } 1889 1890 /* shrinking is currently not supported */ 1891 if (offset < bs->total_sectors * 512) { 1892 error_report("qcow2 doesn't support shrinking images yet"); 1893 return -ENOTSUP; 1894 } 1895 1896 new_l1_size = size_to_l1(s, offset); 1897 ret = qcow2_grow_l1_table(bs, new_l1_size, true); 1898 if (ret < 0) { 1899 return ret; 1900 } 1901 1902 /* write updated header.size */ 1903 offset = cpu_to_be64(offset); 1904 ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, size), 1905 &offset, sizeof(uint64_t)); 1906 if (ret < 0) { 1907 return ret; 1908 } 1909 1910 s->l1_vm_state_index = new_l1_size; 1911 return 0; 1912 } 1913 1914 /* XXX: put compressed sectors first, then all the cluster aligned 1915 tables to avoid losing bytes in alignment */ 1916 static int qcow2_write_compressed(BlockDriverState *bs, int64_t sector_num, 1917 const uint8_t *buf, int nb_sectors) 1918 { 1919 BDRVQcowState *s = bs->opaque; 1920 z_stream strm; 1921 int ret, out_len; 1922 uint8_t *out_buf; 1923 uint64_t cluster_offset; 1924 1925 if (nb_sectors == 0) { 1926 /* align end of file to a sector boundary to ease reading with 1927 sector based I/Os */ 1928 cluster_offset = bdrv_getlength(bs->file); 1929 cluster_offset = (cluster_offset + 511) & ~511; 1930 bdrv_truncate(bs->file, cluster_offset); 1931 return 0; 1932 } 1933 1934 if (nb_sectors != s->cluster_sectors) { 1935 ret = -EINVAL; 1936 1937 /* Zero-pad last write if image size is not cluster aligned */ 1938 if (sector_num + nb_sectors == bs->total_sectors && 1939 nb_sectors < s->cluster_sectors) { 1940 uint8_t *pad_buf = qemu_blockalign(bs, s->cluster_size); 1941 memset(pad_buf, 0, s->cluster_size); 1942 memcpy(pad_buf, buf, nb_sectors * BDRV_SECTOR_SIZE); 1943 ret = qcow2_write_compressed(bs, sector_num, 1944 pad_buf, s->cluster_sectors); 1945 qemu_vfree(pad_buf); 1946 } 1947 return ret; 1948 } 1949 1950 out_buf = g_malloc(s->cluster_size + (s->cluster_size / 1000) + 128); 1951 1952 /* best compression, small window, no zlib header */ 1953 memset(&strm, 0, sizeof(strm)); 1954 ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, 1955 Z_DEFLATED, -12, 1956 9, Z_DEFAULT_STRATEGY); 1957 if (ret != 0) { 1958 ret = -EINVAL; 1959 goto fail; 1960 } 1961 1962 strm.avail_in = s->cluster_size; 1963 strm.next_in = (uint8_t *)buf; 1964 strm.avail_out = s->cluster_size; 1965 strm.next_out = out_buf; 1966 1967 ret = deflate(&strm, Z_FINISH); 1968 if (ret != Z_STREAM_END && ret != Z_OK) { 1969 deflateEnd(&strm); 1970 ret = -EINVAL; 1971 goto fail; 1972 } 1973 out_len = strm.next_out - out_buf; 1974 1975 deflateEnd(&strm); 1976 1977 if (ret != Z_STREAM_END || out_len >= s->cluster_size) { 1978 /* could not compress: write normal cluster */ 1979 ret = bdrv_write(bs, sector_num, buf, s->cluster_sectors); 1980 if (ret < 0) { 1981 goto fail; 1982 } 1983 } else { 1984 cluster_offset = qcow2_alloc_compressed_cluster_offset(bs, 1985 sector_num << 9, out_len); 1986 if (!cluster_offset) { 1987 ret = -EIO; 1988 goto fail; 1989 } 1990 cluster_offset &= s->cluster_offset_mask; 1991 1992 ret = qcow2_pre_write_overlap_check(bs, 0, cluster_offset, out_len); 1993 if (ret < 0) { 1994 goto fail; 1995 } 1996 1997 BLKDBG_EVENT(bs->file, BLKDBG_WRITE_COMPRESSED); 1998 ret = bdrv_pwrite(bs->file, cluster_offset, out_buf, out_len); 1999 if (ret < 0) { 2000 goto fail; 2001 } 2002 } 2003 2004 ret = 0; 2005 fail: 2006 g_free(out_buf); 2007 return ret; 2008 } 2009 2010 static coroutine_fn int qcow2_co_flush_to_os(BlockDriverState *bs) 2011 { 2012 BDRVQcowState *s = bs->opaque; 2013 int ret; 2014 2015 qemu_co_mutex_lock(&s->lock); 2016 ret = qcow2_cache_flush(bs, s->l2_table_cache); 2017 if (ret < 0) { 2018 qemu_co_mutex_unlock(&s->lock); 2019 return ret; 2020 } 2021 2022 if (qcow2_need_accurate_refcounts(s)) { 2023 ret = qcow2_cache_flush(bs, s->refcount_block_cache); 2024 if (ret < 0) { 2025 qemu_co_mutex_unlock(&s->lock); 2026 return ret; 2027 } 2028 } 2029 qemu_co_mutex_unlock(&s->lock); 2030 2031 return 0; 2032 } 2033 2034 static int qcow2_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) 2035 { 2036 BDRVQcowState *s = bs->opaque; 2037 bdi->unallocated_blocks_are_zero = true; 2038 bdi->can_write_zeroes_with_unmap = (s->qcow_version >= 3); 2039 bdi->cluster_size = s->cluster_size; 2040 bdi->vm_state_offset = qcow2_vm_state_offset(s); 2041 return 0; 2042 } 2043 2044 static ImageInfoSpecific *qcow2_get_specific_info(BlockDriverState *bs) 2045 { 2046 BDRVQcowState *s = bs->opaque; 2047 ImageInfoSpecific *spec_info = g_new(ImageInfoSpecific, 1); 2048 2049 *spec_info = (ImageInfoSpecific){ 2050 .kind = IMAGE_INFO_SPECIFIC_KIND_QCOW2, 2051 { 2052 .qcow2 = g_new(ImageInfoSpecificQCow2, 1), 2053 }, 2054 }; 2055 if (s->qcow_version == 2) { 2056 *spec_info->qcow2 = (ImageInfoSpecificQCow2){ 2057 .compat = g_strdup("0.10"), 2058 }; 2059 } else if (s->qcow_version == 3) { 2060 *spec_info->qcow2 = (ImageInfoSpecificQCow2){ 2061 .compat = g_strdup("1.1"), 2062 .lazy_refcounts = s->compatible_features & 2063 QCOW2_COMPAT_LAZY_REFCOUNTS, 2064 .has_lazy_refcounts = true, 2065 }; 2066 } 2067 2068 return spec_info; 2069 } 2070 2071 #if 0 2072 static void dump_refcounts(BlockDriverState *bs) 2073 { 2074 BDRVQcowState *s = bs->opaque; 2075 int64_t nb_clusters, k, k1, size; 2076 int refcount; 2077 2078 size = bdrv_getlength(bs->file); 2079 nb_clusters = size_to_clusters(s, size); 2080 for(k = 0; k < nb_clusters;) { 2081 k1 = k; 2082 refcount = get_refcount(bs, k); 2083 k++; 2084 while (k < nb_clusters && get_refcount(bs, k) == refcount) 2085 k++; 2086 printf("%" PRId64 ": refcount=%d nb=%" PRId64 "\n", k, refcount, 2087 k - k1); 2088 } 2089 } 2090 #endif 2091 2092 static int qcow2_save_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, 2093 int64_t pos) 2094 { 2095 BDRVQcowState *s = bs->opaque; 2096 int64_t total_sectors = bs->total_sectors; 2097 int growable = bs->growable; 2098 bool zero_beyond_eof = bs->zero_beyond_eof; 2099 int ret; 2100 2101 BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_SAVE); 2102 bs->growable = 1; 2103 bs->zero_beyond_eof = false; 2104 ret = bdrv_pwritev(bs, qcow2_vm_state_offset(s) + pos, qiov); 2105 bs->growable = growable; 2106 bs->zero_beyond_eof = zero_beyond_eof; 2107 2108 /* bdrv_co_do_writev will have increased the total_sectors value to include 2109 * the VM state - the VM state is however not an actual part of the block 2110 * device, therefore, we need to restore the old value. */ 2111 bs->total_sectors = total_sectors; 2112 2113 return ret; 2114 } 2115 2116 static int qcow2_load_vmstate(BlockDriverState *bs, uint8_t *buf, 2117 int64_t pos, int size) 2118 { 2119 BDRVQcowState *s = bs->opaque; 2120 int growable = bs->growable; 2121 bool zero_beyond_eof = bs->zero_beyond_eof; 2122 int ret; 2123 2124 BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_LOAD); 2125 bs->growable = 1; 2126 bs->zero_beyond_eof = false; 2127 ret = bdrv_pread(bs, qcow2_vm_state_offset(s) + pos, buf, size); 2128 bs->growable = growable; 2129 bs->zero_beyond_eof = zero_beyond_eof; 2130 2131 return ret; 2132 } 2133 2134 /* 2135 * Downgrades an image's version. To achieve this, any incompatible features 2136 * have to be removed. 2137 */ 2138 static int qcow2_downgrade(BlockDriverState *bs, int target_version) 2139 { 2140 BDRVQcowState *s = bs->opaque; 2141 int current_version = s->qcow_version; 2142 int ret; 2143 2144 if (target_version == current_version) { 2145 return 0; 2146 } else if (target_version > current_version) { 2147 return -EINVAL; 2148 } else if (target_version != 2) { 2149 return -EINVAL; 2150 } 2151 2152 if (s->refcount_order != 4) { 2153 /* we would have to convert the image to a refcount_order == 4 image 2154 * here; however, since qemu (at the time of writing this) does not 2155 * support anything different than 4 anyway, there is no point in doing 2156 * so right now; however, we should error out (if qemu supports this in 2157 * the future and this code has not been adapted) */ 2158 error_report("qcow2_downgrade: Image refcount orders other than 4 are " 2159 "currently not supported."); 2160 return -ENOTSUP; 2161 } 2162 2163 /* clear incompatible features */ 2164 if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) { 2165 ret = qcow2_mark_clean(bs); 2166 if (ret < 0) { 2167 return ret; 2168 } 2169 } 2170 2171 /* with QCOW2_INCOMPAT_CORRUPT, it is pretty much impossible to get here in 2172 * the first place; if that happens nonetheless, returning -ENOTSUP is the 2173 * best thing to do anyway */ 2174 2175 if (s->incompatible_features) { 2176 return -ENOTSUP; 2177 } 2178 2179 /* since we can ignore compatible features, we can set them to 0 as well */ 2180 s->compatible_features = 0; 2181 /* if lazy refcounts have been used, they have already been fixed through 2182 * clearing the dirty flag */ 2183 2184 /* clearing autoclear features is trivial */ 2185 s->autoclear_features = 0; 2186 2187 ret = qcow2_expand_zero_clusters(bs); 2188 if (ret < 0) { 2189 return ret; 2190 } 2191 2192 s->qcow_version = target_version; 2193 ret = qcow2_update_header(bs); 2194 if (ret < 0) { 2195 s->qcow_version = current_version; 2196 return ret; 2197 } 2198 return 0; 2199 } 2200 2201 static int qcow2_amend_options(BlockDriverState *bs, 2202 QEMUOptionParameter *options) 2203 { 2204 BDRVQcowState *s = bs->opaque; 2205 int old_version = s->qcow_version, new_version = old_version; 2206 uint64_t new_size = 0; 2207 const char *backing_file = NULL, *backing_format = NULL; 2208 bool lazy_refcounts = s->use_lazy_refcounts; 2209 int ret; 2210 int i; 2211 2212 for (i = 0; options[i].name; i++) 2213 { 2214 if (!options[i].assigned) { 2215 /* only change explicitly defined options */ 2216 continue; 2217 } 2218 2219 if (!strcmp(options[i].name, "compat")) { 2220 if (!options[i].value.s) { 2221 /* preserve default */ 2222 } else if (!strcmp(options[i].value.s, "0.10")) { 2223 new_version = 2; 2224 } else if (!strcmp(options[i].value.s, "1.1")) { 2225 new_version = 3; 2226 } else { 2227 fprintf(stderr, "Unknown compatibility level %s.\n", 2228 options[i].value.s); 2229 return -EINVAL; 2230 } 2231 } else if (!strcmp(options[i].name, "preallocation")) { 2232 fprintf(stderr, "Cannot change preallocation mode.\n"); 2233 return -ENOTSUP; 2234 } else if (!strcmp(options[i].name, "size")) { 2235 new_size = options[i].value.n; 2236 } else if (!strcmp(options[i].name, "backing_file")) { 2237 backing_file = options[i].value.s; 2238 } else if (!strcmp(options[i].name, "backing_fmt")) { 2239 backing_format = options[i].value.s; 2240 } else if (!strcmp(options[i].name, "encryption")) { 2241 if ((options[i].value.n != !!s->crypt_method)) { 2242 fprintf(stderr, "Changing the encryption flag is not " 2243 "supported.\n"); 2244 return -ENOTSUP; 2245 } 2246 } else if (!strcmp(options[i].name, "cluster_size")) { 2247 if (options[i].value.n != s->cluster_size) { 2248 fprintf(stderr, "Changing the cluster size is not " 2249 "supported.\n"); 2250 return -ENOTSUP; 2251 } 2252 } else if (!strcmp(options[i].name, "lazy_refcounts")) { 2253 lazy_refcounts = options[i].value.n; 2254 } else { 2255 /* if this assertion fails, this probably means a new option was 2256 * added without having it covered here */ 2257 assert(false); 2258 } 2259 } 2260 2261 if (new_version != old_version) { 2262 if (new_version > old_version) { 2263 /* Upgrade */ 2264 s->qcow_version = new_version; 2265 ret = qcow2_update_header(bs); 2266 if (ret < 0) { 2267 s->qcow_version = old_version; 2268 return ret; 2269 } 2270 } else { 2271 ret = qcow2_downgrade(bs, new_version); 2272 if (ret < 0) { 2273 return ret; 2274 } 2275 } 2276 } 2277 2278 if (backing_file || backing_format) { 2279 ret = qcow2_change_backing_file(bs, backing_file ?: bs->backing_file, 2280 backing_format ?: bs->backing_format); 2281 if (ret < 0) { 2282 return ret; 2283 } 2284 } 2285 2286 if (s->use_lazy_refcounts != lazy_refcounts) { 2287 if (lazy_refcounts) { 2288 if (s->qcow_version < 3) { 2289 fprintf(stderr, "Lazy refcounts only supported with compatibility " 2290 "level 1.1 and above (use compat=1.1 or greater)\n"); 2291 return -EINVAL; 2292 } 2293 s->compatible_features |= QCOW2_COMPAT_LAZY_REFCOUNTS; 2294 ret = qcow2_update_header(bs); 2295 if (ret < 0) { 2296 s->compatible_features &= ~QCOW2_COMPAT_LAZY_REFCOUNTS; 2297 return ret; 2298 } 2299 s->use_lazy_refcounts = true; 2300 } else { 2301 /* make image clean first */ 2302 ret = qcow2_mark_clean(bs); 2303 if (ret < 0) { 2304 return ret; 2305 } 2306 /* now disallow lazy refcounts */ 2307 s->compatible_features &= ~QCOW2_COMPAT_LAZY_REFCOUNTS; 2308 ret = qcow2_update_header(bs); 2309 if (ret < 0) { 2310 s->compatible_features |= QCOW2_COMPAT_LAZY_REFCOUNTS; 2311 return ret; 2312 } 2313 s->use_lazy_refcounts = false; 2314 } 2315 } 2316 2317 if (new_size) { 2318 ret = bdrv_truncate(bs, new_size); 2319 if (ret < 0) { 2320 return ret; 2321 } 2322 } 2323 2324 return 0; 2325 } 2326 2327 static QEMUOptionParameter qcow2_create_options[] = { 2328 { 2329 .name = BLOCK_OPT_SIZE, 2330 .type = OPT_SIZE, 2331 .help = "Virtual disk size" 2332 }, 2333 { 2334 .name = BLOCK_OPT_COMPAT_LEVEL, 2335 .type = OPT_STRING, 2336 .help = "Compatibility level (0.10 or 1.1)" 2337 }, 2338 { 2339 .name = BLOCK_OPT_BACKING_FILE, 2340 .type = OPT_STRING, 2341 .help = "File name of a base image" 2342 }, 2343 { 2344 .name = BLOCK_OPT_BACKING_FMT, 2345 .type = OPT_STRING, 2346 .help = "Image format of the base image" 2347 }, 2348 { 2349 .name = BLOCK_OPT_ENCRYPT, 2350 .type = OPT_FLAG, 2351 .help = "Encrypt the image" 2352 }, 2353 { 2354 .name = BLOCK_OPT_CLUSTER_SIZE, 2355 .type = OPT_SIZE, 2356 .help = "qcow2 cluster size", 2357 .value = { .n = DEFAULT_CLUSTER_SIZE }, 2358 }, 2359 { 2360 .name = BLOCK_OPT_PREALLOC, 2361 .type = OPT_STRING, 2362 .help = "Preallocation mode (allowed values: off, metadata)" 2363 }, 2364 { 2365 .name = BLOCK_OPT_LAZY_REFCOUNTS, 2366 .type = OPT_FLAG, 2367 .help = "Postpone refcount updates", 2368 }, 2369 { NULL } 2370 }; 2371 2372 static BlockDriver bdrv_qcow2 = { 2373 .format_name = "qcow2", 2374 .instance_size = sizeof(BDRVQcowState), 2375 .bdrv_probe = qcow2_probe, 2376 .bdrv_open = qcow2_open, 2377 .bdrv_close = qcow2_close, 2378 .bdrv_reopen_prepare = qcow2_reopen_prepare, 2379 .bdrv_create = qcow2_create, 2380 .bdrv_has_zero_init = bdrv_has_zero_init_1, 2381 .bdrv_co_get_block_status = qcow2_co_get_block_status, 2382 .bdrv_set_key = qcow2_set_key, 2383 2384 .bdrv_co_readv = qcow2_co_readv, 2385 .bdrv_co_writev = qcow2_co_writev, 2386 .bdrv_co_flush_to_os = qcow2_co_flush_to_os, 2387 2388 .bdrv_co_write_zeroes = qcow2_co_write_zeroes, 2389 .bdrv_co_discard = qcow2_co_discard, 2390 .bdrv_truncate = qcow2_truncate, 2391 .bdrv_write_compressed = qcow2_write_compressed, 2392 2393 .bdrv_snapshot_create = qcow2_snapshot_create, 2394 .bdrv_snapshot_goto = qcow2_snapshot_goto, 2395 .bdrv_snapshot_delete = qcow2_snapshot_delete, 2396 .bdrv_snapshot_list = qcow2_snapshot_list, 2397 .bdrv_snapshot_load_tmp = qcow2_snapshot_load_tmp, 2398 .bdrv_get_info = qcow2_get_info, 2399 .bdrv_get_specific_info = qcow2_get_specific_info, 2400 2401 .bdrv_save_vmstate = qcow2_save_vmstate, 2402 .bdrv_load_vmstate = qcow2_load_vmstate, 2403 2404 .bdrv_change_backing_file = qcow2_change_backing_file, 2405 2406 .bdrv_refresh_limits = qcow2_refresh_limits, 2407 .bdrv_invalidate_cache = qcow2_invalidate_cache, 2408 2409 .create_options = qcow2_create_options, 2410 .bdrv_check = qcow2_check, 2411 .bdrv_amend_options = qcow2_amend_options, 2412 }; 2413 2414 static void bdrv_qcow2_init(void) 2415 { 2416 bdrv_register(&bdrv_qcow2); 2417 } 2418 2419 block_init(bdrv_qcow2_init); 2420