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