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