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