1 /* 2 * QEMU Enhanced Disk Format 3 * 4 * Copyright IBM, Corp. 2010 5 * 6 * Authors: 7 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com> 8 * Anthony Liguori <aliguori@us.ibm.com> 9 * 10 * This work is licensed under the terms of the GNU LGPL, version 2 or later. 11 * See the COPYING.LIB file in the top-level directory. 12 * 13 */ 14 15 #include "qemu/osdep.h" 16 #include "qapi/error.h" 17 #include "qemu/timer.h" 18 #include "qemu/bswap.h" 19 #include "trace.h" 20 #include "qed.h" 21 #include "sysemu/block-backend.h" 22 23 static int bdrv_qed_probe(const uint8_t *buf, int buf_size, 24 const char *filename) 25 { 26 const QEDHeader *header = (const QEDHeader *)buf; 27 28 if (buf_size < sizeof(*header)) { 29 return 0; 30 } 31 if (le32_to_cpu(header->magic) != QED_MAGIC) { 32 return 0; 33 } 34 return 100; 35 } 36 37 /** 38 * Check whether an image format is raw 39 * 40 * @fmt: Backing file format, may be NULL 41 */ 42 static bool qed_fmt_is_raw(const char *fmt) 43 { 44 return fmt && strcmp(fmt, "raw") == 0; 45 } 46 47 static void qed_header_le_to_cpu(const QEDHeader *le, QEDHeader *cpu) 48 { 49 cpu->magic = le32_to_cpu(le->magic); 50 cpu->cluster_size = le32_to_cpu(le->cluster_size); 51 cpu->table_size = le32_to_cpu(le->table_size); 52 cpu->header_size = le32_to_cpu(le->header_size); 53 cpu->features = le64_to_cpu(le->features); 54 cpu->compat_features = le64_to_cpu(le->compat_features); 55 cpu->autoclear_features = le64_to_cpu(le->autoclear_features); 56 cpu->l1_table_offset = le64_to_cpu(le->l1_table_offset); 57 cpu->image_size = le64_to_cpu(le->image_size); 58 cpu->backing_filename_offset = le32_to_cpu(le->backing_filename_offset); 59 cpu->backing_filename_size = le32_to_cpu(le->backing_filename_size); 60 } 61 62 static void qed_header_cpu_to_le(const QEDHeader *cpu, QEDHeader *le) 63 { 64 le->magic = cpu_to_le32(cpu->magic); 65 le->cluster_size = cpu_to_le32(cpu->cluster_size); 66 le->table_size = cpu_to_le32(cpu->table_size); 67 le->header_size = cpu_to_le32(cpu->header_size); 68 le->features = cpu_to_le64(cpu->features); 69 le->compat_features = cpu_to_le64(cpu->compat_features); 70 le->autoclear_features = cpu_to_le64(cpu->autoclear_features); 71 le->l1_table_offset = cpu_to_le64(cpu->l1_table_offset); 72 le->image_size = cpu_to_le64(cpu->image_size); 73 le->backing_filename_offset = cpu_to_le32(cpu->backing_filename_offset); 74 le->backing_filename_size = cpu_to_le32(cpu->backing_filename_size); 75 } 76 77 int qed_write_header_sync(BDRVQEDState *s) 78 { 79 QEDHeader le; 80 int ret; 81 82 qed_header_cpu_to_le(&s->header, &le); 83 ret = bdrv_pwrite(s->bs->file, 0, &le, sizeof(le)); 84 if (ret != sizeof(le)) { 85 return ret; 86 } 87 return 0; 88 } 89 90 /** 91 * Update header in-place (does not rewrite backing filename or other strings) 92 * 93 * This function only updates known header fields in-place and does not affect 94 * extra data after the QED header. 95 * 96 * No new allocating reqs can start while this function runs. 97 */ 98 static int coroutine_fn qed_write_header(BDRVQEDState *s) 99 { 100 /* We must write full sectors for O_DIRECT but cannot necessarily generate 101 * the data following the header if an unrecognized compat feature is 102 * active. Therefore, first read the sectors containing the header, update 103 * them, and write back. 104 */ 105 106 int nsectors = DIV_ROUND_UP(sizeof(QEDHeader), BDRV_SECTOR_SIZE); 107 size_t len = nsectors * BDRV_SECTOR_SIZE; 108 uint8_t *buf; 109 struct iovec iov; 110 QEMUIOVector qiov; 111 int ret; 112 113 assert(s->allocating_acb || s->allocating_write_reqs_plugged); 114 115 buf = qemu_blockalign(s->bs, len); 116 iov = (struct iovec) { 117 .iov_base = buf, 118 .iov_len = len, 119 }; 120 qemu_iovec_init_external(&qiov, &iov, 1); 121 122 ret = bdrv_co_preadv(s->bs->file, 0, qiov.size, &qiov, 0); 123 if (ret < 0) { 124 goto out; 125 } 126 127 /* Update header */ 128 qed_header_cpu_to_le(&s->header, (QEDHeader *) buf); 129 130 ret = bdrv_co_pwritev(s->bs->file, 0, qiov.size, &qiov, 0); 131 if (ret < 0) { 132 goto out; 133 } 134 135 ret = 0; 136 out: 137 qemu_vfree(buf); 138 return ret; 139 } 140 141 static uint64_t qed_max_image_size(uint32_t cluster_size, uint32_t table_size) 142 { 143 uint64_t table_entries; 144 uint64_t l2_size; 145 146 table_entries = (table_size * cluster_size) / sizeof(uint64_t); 147 l2_size = table_entries * cluster_size; 148 149 return l2_size * table_entries; 150 } 151 152 static bool qed_is_cluster_size_valid(uint32_t cluster_size) 153 { 154 if (cluster_size < QED_MIN_CLUSTER_SIZE || 155 cluster_size > QED_MAX_CLUSTER_SIZE) { 156 return false; 157 } 158 if (cluster_size & (cluster_size - 1)) { 159 return false; /* not power of 2 */ 160 } 161 return true; 162 } 163 164 static bool qed_is_table_size_valid(uint32_t table_size) 165 { 166 if (table_size < QED_MIN_TABLE_SIZE || 167 table_size > QED_MAX_TABLE_SIZE) { 168 return false; 169 } 170 if (table_size & (table_size - 1)) { 171 return false; /* not power of 2 */ 172 } 173 return true; 174 } 175 176 static bool qed_is_image_size_valid(uint64_t image_size, uint32_t cluster_size, 177 uint32_t table_size) 178 { 179 if (image_size % BDRV_SECTOR_SIZE != 0) { 180 return false; /* not multiple of sector size */ 181 } 182 if (image_size > qed_max_image_size(cluster_size, table_size)) { 183 return false; /* image is too large */ 184 } 185 return true; 186 } 187 188 /** 189 * Read a string of known length from the image file 190 * 191 * @file: Image file 192 * @offset: File offset to start of string, in bytes 193 * @n: String length in bytes 194 * @buf: Destination buffer 195 * @buflen: Destination buffer length in bytes 196 * @ret: 0 on success, -errno on failure 197 * 198 * The string is NUL-terminated. 199 */ 200 static int qed_read_string(BdrvChild *file, uint64_t offset, size_t n, 201 char *buf, size_t buflen) 202 { 203 int ret; 204 if (n >= buflen) { 205 return -EINVAL; 206 } 207 ret = bdrv_pread(file, offset, buf, n); 208 if (ret < 0) { 209 return ret; 210 } 211 buf[n] = '\0'; 212 return 0; 213 } 214 215 /** 216 * Allocate new clusters 217 * 218 * @s: QED state 219 * @n: Number of contiguous clusters to allocate 220 * @ret: Offset of first allocated cluster 221 * 222 * This function only produces the offset where the new clusters should be 223 * written. It updates BDRVQEDState but does not make any changes to the image 224 * file. 225 * 226 * Called with table_lock held. 227 */ 228 static uint64_t qed_alloc_clusters(BDRVQEDState *s, unsigned int n) 229 { 230 uint64_t offset = s->file_size; 231 s->file_size += n * s->header.cluster_size; 232 return offset; 233 } 234 235 QEDTable *qed_alloc_table(BDRVQEDState *s) 236 { 237 /* Honor O_DIRECT memory alignment requirements */ 238 return qemu_blockalign(s->bs, 239 s->header.cluster_size * s->header.table_size); 240 } 241 242 /** 243 * Allocate a new zeroed L2 table 244 * 245 * Called with table_lock held. 246 */ 247 static CachedL2Table *qed_new_l2_table(BDRVQEDState *s) 248 { 249 CachedL2Table *l2_table = qed_alloc_l2_cache_entry(&s->l2_cache); 250 251 l2_table->table = qed_alloc_table(s); 252 l2_table->offset = qed_alloc_clusters(s, s->header.table_size); 253 254 memset(l2_table->table->offsets, 0, 255 s->header.cluster_size * s->header.table_size); 256 return l2_table; 257 } 258 259 static bool qed_plug_allocating_write_reqs(BDRVQEDState *s) 260 { 261 qemu_co_mutex_lock(&s->table_lock); 262 263 /* No reentrancy is allowed. */ 264 assert(!s->allocating_write_reqs_plugged); 265 if (s->allocating_acb != NULL) { 266 /* Another allocating write came concurrently. This cannot happen 267 * from bdrv_qed_co_drain_begin, but it can happen when the timer runs. 268 */ 269 qemu_co_mutex_unlock(&s->table_lock); 270 return false; 271 } 272 273 s->allocating_write_reqs_plugged = true; 274 qemu_co_mutex_unlock(&s->table_lock); 275 return true; 276 } 277 278 static void qed_unplug_allocating_write_reqs(BDRVQEDState *s) 279 { 280 qemu_co_mutex_lock(&s->table_lock); 281 assert(s->allocating_write_reqs_plugged); 282 s->allocating_write_reqs_plugged = false; 283 qemu_co_queue_next(&s->allocating_write_reqs); 284 qemu_co_mutex_unlock(&s->table_lock); 285 } 286 287 static void coroutine_fn qed_need_check_timer_entry(void *opaque) 288 { 289 BDRVQEDState *s = opaque; 290 int ret; 291 292 trace_qed_need_check_timer_cb(s); 293 294 if (!qed_plug_allocating_write_reqs(s)) { 295 return; 296 } 297 298 /* Ensure writes are on disk before clearing flag */ 299 ret = bdrv_co_flush(s->bs->file->bs); 300 if (ret < 0) { 301 qed_unplug_allocating_write_reqs(s); 302 return; 303 } 304 305 s->header.features &= ~QED_F_NEED_CHECK; 306 ret = qed_write_header(s); 307 (void) ret; 308 309 qed_unplug_allocating_write_reqs(s); 310 311 ret = bdrv_co_flush(s->bs); 312 (void) ret; 313 } 314 315 static void qed_need_check_timer_cb(void *opaque) 316 { 317 Coroutine *co = qemu_coroutine_create(qed_need_check_timer_entry, opaque); 318 qemu_coroutine_enter(co); 319 } 320 321 static void qed_start_need_check_timer(BDRVQEDState *s) 322 { 323 trace_qed_start_need_check_timer(s); 324 325 /* Use QEMU_CLOCK_VIRTUAL so we don't alter the image file while suspended for 326 * migration. 327 */ 328 timer_mod(s->need_check_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 329 NANOSECONDS_PER_SECOND * QED_NEED_CHECK_TIMEOUT); 330 } 331 332 /* It's okay to call this multiple times or when no timer is started */ 333 static void qed_cancel_need_check_timer(BDRVQEDState *s) 334 { 335 trace_qed_cancel_need_check_timer(s); 336 timer_del(s->need_check_timer); 337 } 338 339 static void bdrv_qed_detach_aio_context(BlockDriverState *bs) 340 { 341 BDRVQEDState *s = bs->opaque; 342 343 qed_cancel_need_check_timer(s); 344 timer_free(s->need_check_timer); 345 } 346 347 static void bdrv_qed_attach_aio_context(BlockDriverState *bs, 348 AioContext *new_context) 349 { 350 BDRVQEDState *s = bs->opaque; 351 352 s->need_check_timer = aio_timer_new(new_context, 353 QEMU_CLOCK_VIRTUAL, SCALE_NS, 354 qed_need_check_timer_cb, s); 355 if (s->header.features & QED_F_NEED_CHECK) { 356 qed_start_need_check_timer(s); 357 } 358 } 359 360 static void coroutine_fn bdrv_qed_co_drain_begin(BlockDriverState *bs) 361 { 362 BDRVQEDState *s = bs->opaque; 363 364 /* Fire the timer immediately in order to start doing I/O as soon as the 365 * header is flushed. 366 */ 367 if (s->need_check_timer && timer_pending(s->need_check_timer)) { 368 qed_cancel_need_check_timer(s); 369 qed_need_check_timer_entry(s); 370 } 371 } 372 373 static void bdrv_qed_init_state(BlockDriverState *bs) 374 { 375 BDRVQEDState *s = bs->opaque; 376 377 memset(s, 0, sizeof(BDRVQEDState)); 378 s->bs = bs; 379 qemu_co_mutex_init(&s->table_lock); 380 qemu_co_queue_init(&s->allocating_write_reqs); 381 } 382 383 static int bdrv_qed_do_open(BlockDriverState *bs, QDict *options, int flags, 384 Error **errp) 385 { 386 BDRVQEDState *s = bs->opaque; 387 QEDHeader le_header; 388 int64_t file_size; 389 int ret; 390 391 ret = bdrv_pread(bs->file, 0, &le_header, sizeof(le_header)); 392 if (ret < 0) { 393 return ret; 394 } 395 qed_header_le_to_cpu(&le_header, &s->header); 396 397 if (s->header.magic != QED_MAGIC) { 398 error_setg(errp, "Image not in QED format"); 399 return -EINVAL; 400 } 401 if (s->header.features & ~QED_FEATURE_MASK) { 402 /* image uses unsupported feature bits */ 403 error_setg(errp, "Unsupported QED features: %" PRIx64, 404 s->header.features & ~QED_FEATURE_MASK); 405 return -ENOTSUP; 406 } 407 if (!qed_is_cluster_size_valid(s->header.cluster_size)) { 408 return -EINVAL; 409 } 410 411 /* Round down file size to the last cluster */ 412 file_size = bdrv_getlength(bs->file->bs); 413 if (file_size < 0) { 414 return file_size; 415 } 416 s->file_size = qed_start_of_cluster(s, file_size); 417 418 if (!qed_is_table_size_valid(s->header.table_size)) { 419 return -EINVAL; 420 } 421 if (!qed_is_image_size_valid(s->header.image_size, 422 s->header.cluster_size, 423 s->header.table_size)) { 424 return -EINVAL; 425 } 426 if (!qed_check_table_offset(s, s->header.l1_table_offset)) { 427 return -EINVAL; 428 } 429 430 s->table_nelems = (s->header.cluster_size * s->header.table_size) / 431 sizeof(uint64_t); 432 s->l2_shift = ctz32(s->header.cluster_size); 433 s->l2_mask = s->table_nelems - 1; 434 s->l1_shift = s->l2_shift + ctz32(s->table_nelems); 435 436 /* Header size calculation must not overflow uint32_t */ 437 if (s->header.header_size > UINT32_MAX / s->header.cluster_size) { 438 return -EINVAL; 439 } 440 441 if ((s->header.features & QED_F_BACKING_FILE)) { 442 if ((uint64_t)s->header.backing_filename_offset + 443 s->header.backing_filename_size > 444 s->header.cluster_size * s->header.header_size) { 445 return -EINVAL; 446 } 447 448 ret = qed_read_string(bs->file, s->header.backing_filename_offset, 449 s->header.backing_filename_size, bs->backing_file, 450 sizeof(bs->backing_file)); 451 if (ret < 0) { 452 return ret; 453 } 454 455 if (s->header.features & QED_F_BACKING_FORMAT_NO_PROBE) { 456 pstrcpy(bs->backing_format, sizeof(bs->backing_format), "raw"); 457 } 458 } 459 460 /* Reset unknown autoclear feature bits. This is a backwards 461 * compatibility mechanism that allows images to be opened by older 462 * programs, which "knock out" unknown feature bits. When an image is 463 * opened by a newer program again it can detect that the autoclear 464 * feature is no longer valid. 465 */ 466 if ((s->header.autoclear_features & ~QED_AUTOCLEAR_FEATURE_MASK) != 0 && 467 !bdrv_is_read_only(bs->file->bs) && !(flags & BDRV_O_INACTIVE)) { 468 s->header.autoclear_features &= QED_AUTOCLEAR_FEATURE_MASK; 469 470 ret = qed_write_header_sync(s); 471 if (ret) { 472 return ret; 473 } 474 475 /* From here on only known autoclear feature bits are valid */ 476 bdrv_flush(bs->file->bs); 477 } 478 479 s->l1_table = qed_alloc_table(s); 480 qed_init_l2_cache(&s->l2_cache); 481 482 ret = qed_read_l1_table_sync(s); 483 if (ret) { 484 goto out; 485 } 486 487 /* If image was not closed cleanly, check consistency */ 488 if (!(flags & BDRV_O_CHECK) && (s->header.features & QED_F_NEED_CHECK)) { 489 /* Read-only images cannot be fixed. There is no risk of corruption 490 * since write operations are not possible. Therefore, allow 491 * potentially inconsistent images to be opened read-only. This can 492 * aid data recovery from an otherwise inconsistent image. 493 */ 494 if (!bdrv_is_read_only(bs->file->bs) && 495 !(flags & BDRV_O_INACTIVE)) { 496 BdrvCheckResult result = {0}; 497 498 ret = qed_check(s, &result, true); 499 if (ret) { 500 goto out; 501 } 502 } 503 } 504 505 bdrv_qed_attach_aio_context(bs, bdrv_get_aio_context(bs)); 506 507 out: 508 if (ret) { 509 qed_free_l2_cache(&s->l2_cache); 510 qemu_vfree(s->l1_table); 511 } 512 return ret; 513 } 514 515 static int bdrv_qed_open(BlockDriverState *bs, QDict *options, int flags, 516 Error **errp) 517 { 518 bs->file = bdrv_open_child(NULL, options, "file", bs, &child_file, 519 false, errp); 520 if (!bs->file) { 521 return -EINVAL; 522 } 523 524 bdrv_qed_init_state(bs); 525 return bdrv_qed_do_open(bs, options, flags, errp); 526 } 527 528 static void bdrv_qed_refresh_limits(BlockDriverState *bs, Error **errp) 529 { 530 BDRVQEDState *s = bs->opaque; 531 532 bs->bl.pwrite_zeroes_alignment = s->header.cluster_size; 533 } 534 535 /* We have nothing to do for QED reopen, stubs just return 536 * success */ 537 static int bdrv_qed_reopen_prepare(BDRVReopenState *state, 538 BlockReopenQueue *queue, Error **errp) 539 { 540 return 0; 541 } 542 543 static void bdrv_qed_close(BlockDriverState *bs) 544 { 545 BDRVQEDState *s = bs->opaque; 546 547 bdrv_qed_detach_aio_context(bs); 548 549 /* Ensure writes reach stable storage */ 550 bdrv_flush(bs->file->bs); 551 552 /* Clean shutdown, no check required on next open */ 553 if (s->header.features & QED_F_NEED_CHECK) { 554 s->header.features &= ~QED_F_NEED_CHECK; 555 qed_write_header_sync(s); 556 } 557 558 qed_free_l2_cache(&s->l2_cache); 559 qemu_vfree(s->l1_table); 560 } 561 562 static int qed_create(const char *filename, uint32_t cluster_size, 563 uint64_t image_size, uint32_t table_size, 564 const char *backing_file, const char *backing_fmt, 565 QemuOpts *opts, Error **errp) 566 { 567 QEDHeader header = { 568 .magic = QED_MAGIC, 569 .cluster_size = cluster_size, 570 .table_size = table_size, 571 .header_size = 1, 572 .features = 0, 573 .compat_features = 0, 574 .l1_table_offset = cluster_size, 575 .image_size = image_size, 576 }; 577 QEDHeader le_header; 578 uint8_t *l1_table = NULL; 579 size_t l1_size = header.cluster_size * header.table_size; 580 Error *local_err = NULL; 581 int ret = 0; 582 BlockBackend *blk; 583 584 ret = bdrv_create_file(filename, opts, &local_err); 585 if (ret < 0) { 586 error_propagate(errp, local_err); 587 return ret; 588 } 589 590 blk = blk_new_open(filename, NULL, NULL, 591 BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, 592 &local_err); 593 if (blk == NULL) { 594 error_propagate(errp, local_err); 595 return -EIO; 596 } 597 598 blk_set_allow_write_beyond_eof(blk, true); 599 600 /* File must start empty and grow, check truncate is supported */ 601 ret = blk_truncate(blk, 0, PREALLOC_MODE_OFF, errp); 602 if (ret < 0) { 603 goto out; 604 } 605 606 if (backing_file) { 607 header.features |= QED_F_BACKING_FILE; 608 header.backing_filename_offset = sizeof(le_header); 609 header.backing_filename_size = strlen(backing_file); 610 611 if (qed_fmt_is_raw(backing_fmt)) { 612 header.features |= QED_F_BACKING_FORMAT_NO_PROBE; 613 } 614 } 615 616 qed_header_cpu_to_le(&header, &le_header); 617 ret = blk_pwrite(blk, 0, &le_header, sizeof(le_header), 0); 618 if (ret < 0) { 619 goto out; 620 } 621 ret = blk_pwrite(blk, sizeof(le_header), backing_file, 622 header.backing_filename_size, 0); 623 if (ret < 0) { 624 goto out; 625 } 626 627 l1_table = g_malloc0(l1_size); 628 ret = blk_pwrite(blk, header.l1_table_offset, l1_table, l1_size, 0); 629 if (ret < 0) { 630 goto out; 631 } 632 633 ret = 0; /* success */ 634 out: 635 g_free(l1_table); 636 blk_unref(blk); 637 return ret; 638 } 639 640 static int bdrv_qed_create(const char *filename, QemuOpts *opts, Error **errp) 641 { 642 uint64_t image_size = 0; 643 uint32_t cluster_size = QED_DEFAULT_CLUSTER_SIZE; 644 uint32_t table_size = QED_DEFAULT_TABLE_SIZE; 645 char *backing_file = NULL; 646 char *backing_fmt = NULL; 647 int ret; 648 649 image_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), 650 BDRV_SECTOR_SIZE); 651 backing_file = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE); 652 backing_fmt = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FMT); 653 cluster_size = qemu_opt_get_size_del(opts, 654 BLOCK_OPT_CLUSTER_SIZE, 655 QED_DEFAULT_CLUSTER_SIZE); 656 table_size = qemu_opt_get_size_del(opts, BLOCK_OPT_TABLE_SIZE, 657 QED_DEFAULT_TABLE_SIZE); 658 659 if (!qed_is_cluster_size_valid(cluster_size)) { 660 error_setg(errp, "QED cluster size must be within range [%u, %u] " 661 "and power of 2", 662 QED_MIN_CLUSTER_SIZE, QED_MAX_CLUSTER_SIZE); 663 ret = -EINVAL; 664 goto finish; 665 } 666 if (!qed_is_table_size_valid(table_size)) { 667 error_setg(errp, "QED table size must be within range [%u, %u] " 668 "and power of 2", 669 QED_MIN_TABLE_SIZE, QED_MAX_TABLE_SIZE); 670 ret = -EINVAL; 671 goto finish; 672 } 673 if (!qed_is_image_size_valid(image_size, cluster_size, table_size)) { 674 error_setg(errp, "QED image size must be a non-zero multiple of " 675 "cluster size and less than %" PRIu64 " bytes", 676 qed_max_image_size(cluster_size, table_size)); 677 ret = -EINVAL; 678 goto finish; 679 } 680 681 ret = qed_create(filename, cluster_size, image_size, table_size, 682 backing_file, backing_fmt, opts, errp); 683 684 finish: 685 g_free(backing_file); 686 g_free(backing_fmt); 687 return ret; 688 } 689 690 typedef struct { 691 BlockDriverState *bs; 692 Coroutine *co; 693 uint64_t pos; 694 int64_t status; 695 int *pnum; 696 BlockDriverState **file; 697 } QEDIsAllocatedCB; 698 699 /* Called with table_lock held. */ 700 static void qed_is_allocated_cb(void *opaque, int ret, uint64_t offset, size_t len) 701 { 702 QEDIsAllocatedCB *cb = opaque; 703 BDRVQEDState *s = cb->bs->opaque; 704 *cb->pnum = len / BDRV_SECTOR_SIZE; 705 switch (ret) { 706 case QED_CLUSTER_FOUND: 707 offset |= qed_offset_into_cluster(s, cb->pos); 708 cb->status = BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID | offset; 709 *cb->file = cb->bs->file->bs; 710 break; 711 case QED_CLUSTER_ZERO: 712 cb->status = BDRV_BLOCK_ZERO; 713 break; 714 case QED_CLUSTER_L2: 715 case QED_CLUSTER_L1: 716 cb->status = 0; 717 break; 718 default: 719 assert(ret < 0); 720 cb->status = ret; 721 break; 722 } 723 724 if (cb->co) { 725 aio_co_wake(cb->co); 726 } 727 } 728 729 static int64_t coroutine_fn bdrv_qed_co_get_block_status(BlockDriverState *bs, 730 int64_t sector_num, 731 int nb_sectors, int *pnum, 732 BlockDriverState **file) 733 { 734 BDRVQEDState *s = bs->opaque; 735 size_t len = (size_t)nb_sectors * BDRV_SECTOR_SIZE; 736 QEDIsAllocatedCB cb = { 737 .bs = bs, 738 .pos = (uint64_t)sector_num * BDRV_SECTOR_SIZE, 739 .status = BDRV_BLOCK_OFFSET_MASK, 740 .pnum = pnum, 741 .file = file, 742 }; 743 QEDRequest request = { .l2_table = NULL }; 744 uint64_t offset; 745 int ret; 746 747 qemu_co_mutex_lock(&s->table_lock); 748 ret = qed_find_cluster(s, &request, cb.pos, &len, &offset); 749 qed_is_allocated_cb(&cb, ret, offset, len); 750 751 /* The callback was invoked immediately */ 752 assert(cb.status != BDRV_BLOCK_OFFSET_MASK); 753 754 qed_unref_l2_cache_entry(request.l2_table); 755 qemu_co_mutex_unlock(&s->table_lock); 756 757 return cb.status; 758 } 759 760 static BDRVQEDState *acb_to_s(QEDAIOCB *acb) 761 { 762 return acb->bs->opaque; 763 } 764 765 /** 766 * Read from the backing file or zero-fill if no backing file 767 * 768 * @s: QED state 769 * @pos: Byte position in device 770 * @qiov: Destination I/O vector 771 * @backing_qiov: Possibly shortened copy of qiov, to be allocated here 772 * @cb: Completion function 773 * @opaque: User data for completion function 774 * 775 * This function reads qiov->size bytes starting at pos from the backing file. 776 * If there is no backing file then zeroes are read. 777 */ 778 static int coroutine_fn qed_read_backing_file(BDRVQEDState *s, uint64_t pos, 779 QEMUIOVector *qiov, 780 QEMUIOVector **backing_qiov) 781 { 782 uint64_t backing_length = 0; 783 size_t size; 784 int ret; 785 786 /* If there is a backing file, get its length. Treat the absence of a 787 * backing file like a zero length backing file. 788 */ 789 if (s->bs->backing) { 790 int64_t l = bdrv_getlength(s->bs->backing->bs); 791 if (l < 0) { 792 return l; 793 } 794 backing_length = l; 795 } 796 797 /* Zero all sectors if reading beyond the end of the backing file */ 798 if (pos >= backing_length || 799 pos + qiov->size > backing_length) { 800 qemu_iovec_memset(qiov, 0, 0, qiov->size); 801 } 802 803 /* Complete now if there are no backing file sectors to read */ 804 if (pos >= backing_length) { 805 return 0; 806 } 807 808 /* If the read straddles the end of the backing file, shorten it */ 809 size = MIN((uint64_t)backing_length - pos, qiov->size); 810 811 assert(*backing_qiov == NULL); 812 *backing_qiov = g_new(QEMUIOVector, 1); 813 qemu_iovec_init(*backing_qiov, qiov->niov); 814 qemu_iovec_concat(*backing_qiov, qiov, 0, size); 815 816 BLKDBG_EVENT(s->bs->file, BLKDBG_READ_BACKING_AIO); 817 ret = bdrv_co_preadv(s->bs->backing, pos, size, *backing_qiov, 0); 818 if (ret < 0) { 819 return ret; 820 } 821 return 0; 822 } 823 824 /** 825 * Copy data from backing file into the image 826 * 827 * @s: QED state 828 * @pos: Byte position in device 829 * @len: Number of bytes 830 * @offset: Byte offset in image file 831 */ 832 static int coroutine_fn qed_copy_from_backing_file(BDRVQEDState *s, 833 uint64_t pos, uint64_t len, 834 uint64_t offset) 835 { 836 QEMUIOVector qiov; 837 QEMUIOVector *backing_qiov = NULL; 838 struct iovec iov; 839 int ret; 840 841 /* Skip copy entirely if there is no work to do */ 842 if (len == 0) { 843 return 0; 844 } 845 846 iov = (struct iovec) { 847 .iov_base = qemu_blockalign(s->bs, len), 848 .iov_len = len, 849 }; 850 qemu_iovec_init_external(&qiov, &iov, 1); 851 852 ret = qed_read_backing_file(s, pos, &qiov, &backing_qiov); 853 854 if (backing_qiov) { 855 qemu_iovec_destroy(backing_qiov); 856 g_free(backing_qiov); 857 backing_qiov = NULL; 858 } 859 860 if (ret) { 861 goto out; 862 } 863 864 BLKDBG_EVENT(s->bs->file, BLKDBG_COW_WRITE); 865 ret = bdrv_co_pwritev(s->bs->file, offset, qiov.size, &qiov, 0); 866 if (ret < 0) { 867 goto out; 868 } 869 ret = 0; 870 out: 871 qemu_vfree(iov.iov_base); 872 return ret; 873 } 874 875 /** 876 * Link one or more contiguous clusters into a table 877 * 878 * @s: QED state 879 * @table: L2 table 880 * @index: First cluster index 881 * @n: Number of contiguous clusters 882 * @cluster: First cluster offset 883 * 884 * The cluster offset may be an allocated byte offset in the image file, the 885 * zero cluster marker, or the unallocated cluster marker. 886 * 887 * Called with table_lock held. 888 */ 889 static void coroutine_fn qed_update_l2_table(BDRVQEDState *s, QEDTable *table, 890 int index, unsigned int n, 891 uint64_t cluster) 892 { 893 int i; 894 for (i = index; i < index + n; i++) { 895 table->offsets[i] = cluster; 896 if (!qed_offset_is_unalloc_cluster(cluster) && 897 !qed_offset_is_zero_cluster(cluster)) { 898 cluster += s->header.cluster_size; 899 } 900 } 901 } 902 903 /* Called with table_lock held. */ 904 static void coroutine_fn qed_aio_complete(QEDAIOCB *acb) 905 { 906 BDRVQEDState *s = acb_to_s(acb); 907 908 /* Free resources */ 909 qemu_iovec_destroy(&acb->cur_qiov); 910 qed_unref_l2_cache_entry(acb->request.l2_table); 911 912 /* Free the buffer we may have allocated for zero writes */ 913 if (acb->flags & QED_AIOCB_ZERO) { 914 qemu_vfree(acb->qiov->iov[0].iov_base); 915 acb->qiov->iov[0].iov_base = NULL; 916 } 917 918 /* Start next allocating write request waiting behind this one. Note that 919 * requests enqueue themselves when they first hit an unallocated cluster 920 * but they wait until the entire request is finished before waking up the 921 * next request in the queue. This ensures that we don't cycle through 922 * requests multiple times but rather finish one at a time completely. 923 */ 924 if (acb == s->allocating_acb) { 925 s->allocating_acb = NULL; 926 if (!qemu_co_queue_empty(&s->allocating_write_reqs)) { 927 qemu_co_queue_next(&s->allocating_write_reqs); 928 } else if (s->header.features & QED_F_NEED_CHECK) { 929 qed_start_need_check_timer(s); 930 } 931 } 932 } 933 934 /** 935 * Update L1 table with new L2 table offset and write it out 936 * 937 * Called with table_lock held. 938 */ 939 static int coroutine_fn qed_aio_write_l1_update(QEDAIOCB *acb) 940 { 941 BDRVQEDState *s = acb_to_s(acb); 942 CachedL2Table *l2_table = acb->request.l2_table; 943 uint64_t l2_offset = l2_table->offset; 944 int index, ret; 945 946 index = qed_l1_index(s, acb->cur_pos); 947 s->l1_table->offsets[index] = l2_table->offset; 948 949 ret = qed_write_l1_table(s, index, 1); 950 951 /* Commit the current L2 table to the cache */ 952 qed_commit_l2_cache_entry(&s->l2_cache, l2_table); 953 954 /* This is guaranteed to succeed because we just committed the entry to the 955 * cache. 956 */ 957 acb->request.l2_table = qed_find_l2_cache_entry(&s->l2_cache, l2_offset); 958 assert(acb->request.l2_table != NULL); 959 960 return ret; 961 } 962 963 964 /** 965 * Update L2 table with new cluster offsets and write them out 966 * 967 * Called with table_lock held. 968 */ 969 static int coroutine_fn qed_aio_write_l2_update(QEDAIOCB *acb, uint64_t offset) 970 { 971 BDRVQEDState *s = acb_to_s(acb); 972 bool need_alloc = acb->find_cluster_ret == QED_CLUSTER_L1; 973 int index, ret; 974 975 if (need_alloc) { 976 qed_unref_l2_cache_entry(acb->request.l2_table); 977 acb->request.l2_table = qed_new_l2_table(s); 978 } 979 980 index = qed_l2_index(s, acb->cur_pos); 981 qed_update_l2_table(s, acb->request.l2_table->table, index, acb->cur_nclusters, 982 offset); 983 984 if (need_alloc) { 985 /* Write out the whole new L2 table */ 986 ret = qed_write_l2_table(s, &acb->request, 0, s->table_nelems, true); 987 if (ret) { 988 return ret; 989 } 990 return qed_aio_write_l1_update(acb); 991 } else { 992 /* Write out only the updated part of the L2 table */ 993 ret = qed_write_l2_table(s, &acb->request, index, acb->cur_nclusters, 994 false); 995 if (ret) { 996 return ret; 997 } 998 } 999 return 0; 1000 } 1001 1002 /** 1003 * Write data to the image file 1004 * 1005 * Called with table_lock *not* held. 1006 */ 1007 static int coroutine_fn qed_aio_write_main(QEDAIOCB *acb) 1008 { 1009 BDRVQEDState *s = acb_to_s(acb); 1010 uint64_t offset = acb->cur_cluster + 1011 qed_offset_into_cluster(s, acb->cur_pos); 1012 1013 trace_qed_aio_write_main(s, acb, 0, offset, acb->cur_qiov.size); 1014 1015 BLKDBG_EVENT(s->bs->file, BLKDBG_WRITE_AIO); 1016 return bdrv_co_pwritev(s->bs->file, offset, acb->cur_qiov.size, 1017 &acb->cur_qiov, 0); 1018 } 1019 1020 /** 1021 * Populate untouched regions of new data cluster 1022 * 1023 * Called with table_lock held. 1024 */ 1025 static int coroutine_fn qed_aio_write_cow(QEDAIOCB *acb) 1026 { 1027 BDRVQEDState *s = acb_to_s(acb); 1028 uint64_t start, len, offset; 1029 int ret; 1030 1031 qemu_co_mutex_unlock(&s->table_lock); 1032 1033 /* Populate front untouched region of new data cluster */ 1034 start = qed_start_of_cluster(s, acb->cur_pos); 1035 len = qed_offset_into_cluster(s, acb->cur_pos); 1036 1037 trace_qed_aio_write_prefill(s, acb, start, len, acb->cur_cluster); 1038 ret = qed_copy_from_backing_file(s, start, len, acb->cur_cluster); 1039 if (ret < 0) { 1040 goto out; 1041 } 1042 1043 /* Populate back untouched region of new data cluster */ 1044 start = acb->cur_pos + acb->cur_qiov.size; 1045 len = qed_start_of_cluster(s, start + s->header.cluster_size - 1) - start; 1046 offset = acb->cur_cluster + 1047 qed_offset_into_cluster(s, acb->cur_pos) + 1048 acb->cur_qiov.size; 1049 1050 trace_qed_aio_write_postfill(s, acb, start, len, offset); 1051 ret = qed_copy_from_backing_file(s, start, len, offset); 1052 if (ret < 0) { 1053 goto out; 1054 } 1055 1056 ret = qed_aio_write_main(acb); 1057 if (ret < 0) { 1058 goto out; 1059 } 1060 1061 if (s->bs->backing) { 1062 /* 1063 * Flush new data clusters before updating the L2 table 1064 * 1065 * This flush is necessary when a backing file is in use. A crash 1066 * during an allocating write could result in empty clusters in the 1067 * image. If the write only touched a subregion of the cluster, 1068 * then backing image sectors have been lost in the untouched 1069 * region. The solution is to flush after writing a new data 1070 * cluster and before updating the L2 table. 1071 */ 1072 ret = bdrv_co_flush(s->bs->file->bs); 1073 } 1074 1075 out: 1076 qemu_co_mutex_lock(&s->table_lock); 1077 return ret; 1078 } 1079 1080 /** 1081 * Check if the QED_F_NEED_CHECK bit should be set during allocating write 1082 */ 1083 static bool qed_should_set_need_check(BDRVQEDState *s) 1084 { 1085 /* The flush before L2 update path ensures consistency */ 1086 if (s->bs->backing) { 1087 return false; 1088 } 1089 1090 return !(s->header.features & QED_F_NEED_CHECK); 1091 } 1092 1093 /** 1094 * Write new data cluster 1095 * 1096 * @acb: Write request 1097 * @len: Length in bytes 1098 * 1099 * This path is taken when writing to previously unallocated clusters. 1100 * 1101 * Called with table_lock held. 1102 */ 1103 static int coroutine_fn qed_aio_write_alloc(QEDAIOCB *acb, size_t len) 1104 { 1105 BDRVQEDState *s = acb_to_s(acb); 1106 int ret; 1107 1108 /* Cancel timer when the first allocating request comes in */ 1109 if (s->allocating_acb == NULL) { 1110 qed_cancel_need_check_timer(s); 1111 } 1112 1113 /* Freeze this request if another allocating write is in progress */ 1114 if (s->allocating_acb != acb || s->allocating_write_reqs_plugged) { 1115 if (s->allocating_acb != NULL) { 1116 qemu_co_queue_wait(&s->allocating_write_reqs, &s->table_lock); 1117 assert(s->allocating_acb == NULL); 1118 } 1119 s->allocating_acb = acb; 1120 return -EAGAIN; /* start over with looking up table entries */ 1121 } 1122 1123 acb->cur_nclusters = qed_bytes_to_clusters(s, 1124 qed_offset_into_cluster(s, acb->cur_pos) + len); 1125 qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len); 1126 1127 if (acb->flags & QED_AIOCB_ZERO) { 1128 /* Skip ahead if the clusters are already zero */ 1129 if (acb->find_cluster_ret == QED_CLUSTER_ZERO) { 1130 return 0; 1131 } 1132 acb->cur_cluster = 1; 1133 } else { 1134 acb->cur_cluster = qed_alloc_clusters(s, acb->cur_nclusters); 1135 } 1136 1137 if (qed_should_set_need_check(s)) { 1138 s->header.features |= QED_F_NEED_CHECK; 1139 ret = qed_write_header(s); 1140 if (ret < 0) { 1141 return ret; 1142 } 1143 } 1144 1145 if (!(acb->flags & QED_AIOCB_ZERO)) { 1146 ret = qed_aio_write_cow(acb); 1147 if (ret < 0) { 1148 return ret; 1149 } 1150 } 1151 1152 return qed_aio_write_l2_update(acb, acb->cur_cluster); 1153 } 1154 1155 /** 1156 * Write data cluster in place 1157 * 1158 * @acb: Write request 1159 * @offset: Cluster offset in bytes 1160 * @len: Length in bytes 1161 * 1162 * This path is taken when writing to already allocated clusters. 1163 * 1164 * Called with table_lock held. 1165 */ 1166 static int coroutine_fn qed_aio_write_inplace(QEDAIOCB *acb, uint64_t offset, 1167 size_t len) 1168 { 1169 BDRVQEDState *s = acb_to_s(acb); 1170 int r; 1171 1172 qemu_co_mutex_unlock(&s->table_lock); 1173 1174 /* Allocate buffer for zero writes */ 1175 if (acb->flags & QED_AIOCB_ZERO) { 1176 struct iovec *iov = acb->qiov->iov; 1177 1178 if (!iov->iov_base) { 1179 iov->iov_base = qemu_try_blockalign(acb->bs, iov->iov_len); 1180 if (iov->iov_base == NULL) { 1181 r = -ENOMEM; 1182 goto out; 1183 } 1184 memset(iov->iov_base, 0, iov->iov_len); 1185 } 1186 } 1187 1188 /* Calculate the I/O vector */ 1189 acb->cur_cluster = offset; 1190 qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len); 1191 1192 /* Do the actual write. */ 1193 r = qed_aio_write_main(acb); 1194 out: 1195 qemu_co_mutex_lock(&s->table_lock); 1196 return r; 1197 } 1198 1199 /** 1200 * Write data cluster 1201 * 1202 * @opaque: Write request 1203 * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2 or QED_CLUSTER_L1 1204 * @offset: Cluster offset in bytes 1205 * @len: Length in bytes 1206 * 1207 * Called with table_lock held. 1208 */ 1209 static int coroutine_fn qed_aio_write_data(void *opaque, int ret, 1210 uint64_t offset, size_t len) 1211 { 1212 QEDAIOCB *acb = opaque; 1213 1214 trace_qed_aio_write_data(acb_to_s(acb), acb, ret, offset, len); 1215 1216 acb->find_cluster_ret = ret; 1217 1218 switch (ret) { 1219 case QED_CLUSTER_FOUND: 1220 return qed_aio_write_inplace(acb, offset, len); 1221 1222 case QED_CLUSTER_L2: 1223 case QED_CLUSTER_L1: 1224 case QED_CLUSTER_ZERO: 1225 return qed_aio_write_alloc(acb, len); 1226 1227 default: 1228 g_assert_not_reached(); 1229 } 1230 } 1231 1232 /** 1233 * Read data cluster 1234 * 1235 * @opaque: Read request 1236 * @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2 or QED_CLUSTER_L1 1237 * @offset: Cluster offset in bytes 1238 * @len: Length in bytes 1239 * 1240 * Called with table_lock held. 1241 */ 1242 static int coroutine_fn qed_aio_read_data(void *opaque, int ret, 1243 uint64_t offset, size_t len) 1244 { 1245 QEDAIOCB *acb = opaque; 1246 BDRVQEDState *s = acb_to_s(acb); 1247 BlockDriverState *bs = acb->bs; 1248 int r; 1249 1250 qemu_co_mutex_unlock(&s->table_lock); 1251 1252 /* Adjust offset into cluster */ 1253 offset += qed_offset_into_cluster(s, acb->cur_pos); 1254 1255 trace_qed_aio_read_data(s, acb, ret, offset, len); 1256 1257 qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len); 1258 1259 /* Handle zero cluster and backing file reads, otherwise read 1260 * data cluster directly. 1261 */ 1262 if (ret == QED_CLUSTER_ZERO) { 1263 qemu_iovec_memset(&acb->cur_qiov, 0, 0, acb->cur_qiov.size); 1264 r = 0; 1265 } else if (ret != QED_CLUSTER_FOUND) { 1266 r = qed_read_backing_file(s, acb->cur_pos, &acb->cur_qiov, 1267 &acb->backing_qiov); 1268 } else { 1269 BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO); 1270 r = bdrv_co_preadv(bs->file, offset, acb->cur_qiov.size, 1271 &acb->cur_qiov, 0); 1272 } 1273 1274 qemu_co_mutex_lock(&s->table_lock); 1275 return r; 1276 } 1277 1278 /** 1279 * Begin next I/O or complete the request 1280 */ 1281 static int coroutine_fn qed_aio_next_io(QEDAIOCB *acb) 1282 { 1283 BDRVQEDState *s = acb_to_s(acb); 1284 uint64_t offset; 1285 size_t len; 1286 int ret; 1287 1288 qemu_co_mutex_lock(&s->table_lock); 1289 while (1) { 1290 trace_qed_aio_next_io(s, acb, 0, acb->cur_pos + acb->cur_qiov.size); 1291 1292 if (acb->backing_qiov) { 1293 qemu_iovec_destroy(acb->backing_qiov); 1294 g_free(acb->backing_qiov); 1295 acb->backing_qiov = NULL; 1296 } 1297 1298 acb->qiov_offset += acb->cur_qiov.size; 1299 acb->cur_pos += acb->cur_qiov.size; 1300 qemu_iovec_reset(&acb->cur_qiov); 1301 1302 /* Complete request */ 1303 if (acb->cur_pos >= acb->end_pos) { 1304 ret = 0; 1305 break; 1306 } 1307 1308 /* Find next cluster and start I/O */ 1309 len = acb->end_pos - acb->cur_pos; 1310 ret = qed_find_cluster(s, &acb->request, acb->cur_pos, &len, &offset); 1311 if (ret < 0) { 1312 break; 1313 } 1314 1315 if (acb->flags & QED_AIOCB_WRITE) { 1316 ret = qed_aio_write_data(acb, ret, offset, len); 1317 } else { 1318 ret = qed_aio_read_data(acb, ret, offset, len); 1319 } 1320 1321 if (ret < 0 && ret != -EAGAIN) { 1322 break; 1323 } 1324 } 1325 1326 trace_qed_aio_complete(s, acb, ret); 1327 qed_aio_complete(acb); 1328 qemu_co_mutex_unlock(&s->table_lock); 1329 return ret; 1330 } 1331 1332 static int coroutine_fn qed_co_request(BlockDriverState *bs, int64_t sector_num, 1333 QEMUIOVector *qiov, int nb_sectors, 1334 int flags) 1335 { 1336 QEDAIOCB acb = { 1337 .bs = bs, 1338 .cur_pos = (uint64_t) sector_num * BDRV_SECTOR_SIZE, 1339 .end_pos = (sector_num + nb_sectors) * BDRV_SECTOR_SIZE, 1340 .qiov = qiov, 1341 .flags = flags, 1342 }; 1343 qemu_iovec_init(&acb.cur_qiov, qiov->niov); 1344 1345 trace_qed_aio_setup(bs->opaque, &acb, sector_num, nb_sectors, NULL, flags); 1346 1347 /* Start request */ 1348 return qed_aio_next_io(&acb); 1349 } 1350 1351 static int coroutine_fn bdrv_qed_co_readv(BlockDriverState *bs, 1352 int64_t sector_num, int nb_sectors, 1353 QEMUIOVector *qiov) 1354 { 1355 return qed_co_request(bs, sector_num, qiov, nb_sectors, 0); 1356 } 1357 1358 static int coroutine_fn bdrv_qed_co_writev(BlockDriverState *bs, 1359 int64_t sector_num, int nb_sectors, 1360 QEMUIOVector *qiov) 1361 { 1362 return qed_co_request(bs, sector_num, qiov, nb_sectors, QED_AIOCB_WRITE); 1363 } 1364 1365 static int coroutine_fn bdrv_qed_co_pwrite_zeroes(BlockDriverState *bs, 1366 int64_t offset, 1367 int bytes, 1368 BdrvRequestFlags flags) 1369 { 1370 BDRVQEDState *s = bs->opaque; 1371 QEMUIOVector qiov; 1372 struct iovec iov; 1373 1374 /* Fall back if the request is not aligned */ 1375 if (qed_offset_into_cluster(s, offset) || 1376 qed_offset_into_cluster(s, bytes)) { 1377 return -ENOTSUP; 1378 } 1379 1380 /* Zero writes start without an I/O buffer. If a buffer becomes necessary 1381 * then it will be allocated during request processing. 1382 */ 1383 iov.iov_base = NULL; 1384 iov.iov_len = bytes; 1385 1386 qemu_iovec_init_external(&qiov, &iov, 1); 1387 return qed_co_request(bs, offset >> BDRV_SECTOR_BITS, &qiov, 1388 bytes >> BDRV_SECTOR_BITS, 1389 QED_AIOCB_WRITE | QED_AIOCB_ZERO); 1390 } 1391 1392 static int bdrv_qed_truncate(BlockDriverState *bs, int64_t offset, 1393 PreallocMode prealloc, Error **errp) 1394 { 1395 BDRVQEDState *s = bs->opaque; 1396 uint64_t old_image_size; 1397 int ret; 1398 1399 if (prealloc != PREALLOC_MODE_OFF) { 1400 error_setg(errp, "Unsupported preallocation mode '%s'", 1401 PreallocMode_str(prealloc)); 1402 return -ENOTSUP; 1403 } 1404 1405 if (!qed_is_image_size_valid(offset, s->header.cluster_size, 1406 s->header.table_size)) { 1407 error_setg(errp, "Invalid image size specified"); 1408 return -EINVAL; 1409 } 1410 1411 if ((uint64_t)offset < s->header.image_size) { 1412 error_setg(errp, "Shrinking images is currently not supported"); 1413 return -ENOTSUP; 1414 } 1415 1416 old_image_size = s->header.image_size; 1417 s->header.image_size = offset; 1418 ret = qed_write_header_sync(s); 1419 if (ret < 0) { 1420 s->header.image_size = old_image_size; 1421 error_setg_errno(errp, -ret, "Failed to update the image size"); 1422 } 1423 return ret; 1424 } 1425 1426 static int64_t bdrv_qed_getlength(BlockDriverState *bs) 1427 { 1428 BDRVQEDState *s = bs->opaque; 1429 return s->header.image_size; 1430 } 1431 1432 static int bdrv_qed_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) 1433 { 1434 BDRVQEDState *s = bs->opaque; 1435 1436 memset(bdi, 0, sizeof(*bdi)); 1437 bdi->cluster_size = s->header.cluster_size; 1438 bdi->is_dirty = s->header.features & QED_F_NEED_CHECK; 1439 bdi->unallocated_blocks_are_zero = true; 1440 bdi->can_write_zeroes_with_unmap = true; 1441 return 0; 1442 } 1443 1444 static int bdrv_qed_change_backing_file(BlockDriverState *bs, 1445 const char *backing_file, 1446 const char *backing_fmt) 1447 { 1448 BDRVQEDState *s = bs->opaque; 1449 QEDHeader new_header, le_header; 1450 void *buffer; 1451 size_t buffer_len, backing_file_len; 1452 int ret; 1453 1454 /* Refuse to set backing filename if unknown compat feature bits are 1455 * active. If the image uses an unknown compat feature then we may not 1456 * know the layout of data following the header structure and cannot safely 1457 * add a new string. 1458 */ 1459 if (backing_file && (s->header.compat_features & 1460 ~QED_COMPAT_FEATURE_MASK)) { 1461 return -ENOTSUP; 1462 } 1463 1464 memcpy(&new_header, &s->header, sizeof(new_header)); 1465 1466 new_header.features &= ~(QED_F_BACKING_FILE | 1467 QED_F_BACKING_FORMAT_NO_PROBE); 1468 1469 /* Adjust feature flags */ 1470 if (backing_file) { 1471 new_header.features |= QED_F_BACKING_FILE; 1472 1473 if (qed_fmt_is_raw(backing_fmt)) { 1474 new_header.features |= QED_F_BACKING_FORMAT_NO_PROBE; 1475 } 1476 } 1477 1478 /* Calculate new header size */ 1479 backing_file_len = 0; 1480 1481 if (backing_file) { 1482 backing_file_len = strlen(backing_file); 1483 } 1484 1485 buffer_len = sizeof(new_header); 1486 new_header.backing_filename_offset = buffer_len; 1487 new_header.backing_filename_size = backing_file_len; 1488 buffer_len += backing_file_len; 1489 1490 /* Make sure we can rewrite header without failing */ 1491 if (buffer_len > new_header.header_size * new_header.cluster_size) { 1492 return -ENOSPC; 1493 } 1494 1495 /* Prepare new header */ 1496 buffer = g_malloc(buffer_len); 1497 1498 qed_header_cpu_to_le(&new_header, &le_header); 1499 memcpy(buffer, &le_header, sizeof(le_header)); 1500 buffer_len = sizeof(le_header); 1501 1502 if (backing_file) { 1503 memcpy(buffer + buffer_len, backing_file, backing_file_len); 1504 buffer_len += backing_file_len; 1505 } 1506 1507 /* Write new header */ 1508 ret = bdrv_pwrite_sync(bs->file, 0, buffer, buffer_len); 1509 g_free(buffer); 1510 if (ret == 0) { 1511 memcpy(&s->header, &new_header, sizeof(new_header)); 1512 } 1513 return ret; 1514 } 1515 1516 static void bdrv_qed_invalidate_cache(BlockDriverState *bs, Error **errp) 1517 { 1518 BDRVQEDState *s = bs->opaque; 1519 Error *local_err = NULL; 1520 int ret; 1521 1522 bdrv_qed_close(bs); 1523 1524 bdrv_qed_init_state(bs); 1525 if (qemu_in_coroutine()) { 1526 qemu_co_mutex_lock(&s->table_lock); 1527 } 1528 ret = bdrv_qed_do_open(bs, NULL, bs->open_flags, &local_err); 1529 if (qemu_in_coroutine()) { 1530 qemu_co_mutex_unlock(&s->table_lock); 1531 } 1532 if (local_err) { 1533 error_propagate(errp, local_err); 1534 error_prepend(errp, "Could not reopen qed layer: "); 1535 return; 1536 } else if (ret < 0) { 1537 error_setg_errno(errp, -ret, "Could not reopen qed layer"); 1538 return; 1539 } 1540 } 1541 1542 static int bdrv_qed_check(BlockDriverState *bs, BdrvCheckResult *result, 1543 BdrvCheckMode fix) 1544 { 1545 BDRVQEDState *s = bs->opaque; 1546 1547 return qed_check(s, result, !!fix); 1548 } 1549 1550 static QemuOptsList qed_create_opts = { 1551 .name = "qed-create-opts", 1552 .head = QTAILQ_HEAD_INITIALIZER(qed_create_opts.head), 1553 .desc = { 1554 { 1555 .name = BLOCK_OPT_SIZE, 1556 .type = QEMU_OPT_SIZE, 1557 .help = "Virtual disk size" 1558 }, 1559 { 1560 .name = BLOCK_OPT_BACKING_FILE, 1561 .type = QEMU_OPT_STRING, 1562 .help = "File name of a base image" 1563 }, 1564 { 1565 .name = BLOCK_OPT_BACKING_FMT, 1566 .type = QEMU_OPT_STRING, 1567 .help = "Image format of the base image" 1568 }, 1569 { 1570 .name = BLOCK_OPT_CLUSTER_SIZE, 1571 .type = QEMU_OPT_SIZE, 1572 .help = "Cluster size (in bytes)", 1573 .def_value_str = stringify(QED_DEFAULT_CLUSTER_SIZE) 1574 }, 1575 { 1576 .name = BLOCK_OPT_TABLE_SIZE, 1577 .type = QEMU_OPT_SIZE, 1578 .help = "L1/L2 table size (in clusters)" 1579 }, 1580 { /* end of list */ } 1581 } 1582 }; 1583 1584 static BlockDriver bdrv_qed = { 1585 .format_name = "qed", 1586 .instance_size = sizeof(BDRVQEDState), 1587 .create_opts = &qed_create_opts, 1588 .supports_backing = true, 1589 1590 .bdrv_probe = bdrv_qed_probe, 1591 .bdrv_open = bdrv_qed_open, 1592 .bdrv_close = bdrv_qed_close, 1593 .bdrv_reopen_prepare = bdrv_qed_reopen_prepare, 1594 .bdrv_child_perm = bdrv_format_default_perms, 1595 .bdrv_create = bdrv_qed_create, 1596 .bdrv_has_zero_init = bdrv_has_zero_init_1, 1597 .bdrv_co_get_block_status = bdrv_qed_co_get_block_status, 1598 .bdrv_co_readv = bdrv_qed_co_readv, 1599 .bdrv_co_writev = bdrv_qed_co_writev, 1600 .bdrv_co_pwrite_zeroes = bdrv_qed_co_pwrite_zeroes, 1601 .bdrv_truncate = bdrv_qed_truncate, 1602 .bdrv_getlength = bdrv_qed_getlength, 1603 .bdrv_get_info = bdrv_qed_get_info, 1604 .bdrv_refresh_limits = bdrv_qed_refresh_limits, 1605 .bdrv_change_backing_file = bdrv_qed_change_backing_file, 1606 .bdrv_invalidate_cache = bdrv_qed_invalidate_cache, 1607 .bdrv_check = bdrv_qed_check, 1608 .bdrv_detach_aio_context = bdrv_qed_detach_aio_context, 1609 .bdrv_attach_aio_context = bdrv_qed_attach_aio_context, 1610 .bdrv_co_drain_begin = bdrv_qed_co_drain_begin, 1611 }; 1612 1613 static void bdrv_qed_init(void) 1614 { 1615 bdrv_register(&bdrv_qed); 1616 } 1617 1618 block_init(bdrv_qed_init); 1619