1 /* 2 * QEMU Block backends 3 * 4 * Copyright (C) 2014 Red Hat, Inc. 5 * 6 * Authors: 7 * Markus Armbruster <armbru@redhat.com>, 8 * 9 * This work is licensed under the terms of the GNU LGPL, version 2.1 10 * or later. See the COPYING.LIB file in the top-level directory. 11 */ 12 13 #include "sysemu/block-backend.h" 14 #include "block/block_int.h" 15 #include "sysemu/blockdev.h" 16 #include "qapi-event.h" 17 18 /* Number of coroutines to reserve per attached device model */ 19 #define COROUTINE_POOL_RESERVATION 64 20 21 struct BlockBackend { 22 char *name; 23 int refcnt; 24 BlockDriverState *bs; 25 DriveInfo *legacy_dinfo; /* null unless created by drive_new() */ 26 QTAILQ_ENTRY(BlockBackend) link; /* for blk_backends */ 27 28 void *dev; /* attached device model, if any */ 29 /* TODO change to DeviceState when all users are qdevified */ 30 const BlockDevOps *dev_ops; 31 void *dev_opaque; 32 }; 33 34 typedef struct BlockBackendAIOCB { 35 BlockAIOCB common; 36 QEMUBH *bh; 37 int ret; 38 } BlockBackendAIOCB; 39 40 static const AIOCBInfo block_backend_aiocb_info = { 41 .aiocb_size = sizeof(BlockBackendAIOCB), 42 }; 43 44 static void drive_info_del(DriveInfo *dinfo); 45 46 /* All the BlockBackends (except for hidden ones) */ 47 static QTAILQ_HEAD(, BlockBackend) blk_backends = 48 QTAILQ_HEAD_INITIALIZER(blk_backends); 49 50 /* 51 * Create a new BlockBackend with @name, with a reference count of one. 52 * @name must not be null or empty. 53 * Fail if a BlockBackend with this name already exists. 54 * Store an error through @errp on failure, unless it's null. 55 * Return the new BlockBackend on success, null on failure. 56 */ 57 BlockBackend *blk_new(const char *name, Error **errp) 58 { 59 BlockBackend *blk; 60 61 assert(name && name[0]); 62 if (!id_wellformed(name)) { 63 error_setg(errp, "Invalid device name"); 64 return NULL; 65 } 66 if (blk_by_name(name)) { 67 error_setg(errp, "Device with id '%s' already exists", name); 68 return NULL; 69 } 70 if (bdrv_find_node(name)) { 71 error_setg(errp, 72 "Device name '%s' conflicts with an existing node name", 73 name); 74 return NULL; 75 } 76 77 blk = g_new0(BlockBackend, 1); 78 blk->name = g_strdup(name); 79 blk->refcnt = 1; 80 QTAILQ_INSERT_TAIL(&blk_backends, blk, link); 81 return blk; 82 } 83 84 /* 85 * Create a new BlockBackend with a new BlockDriverState attached. 86 * Otherwise just like blk_new(), which see. 87 */ 88 BlockBackend *blk_new_with_bs(const char *name, Error **errp) 89 { 90 BlockBackend *blk; 91 BlockDriverState *bs; 92 93 blk = blk_new(name, errp); 94 if (!blk) { 95 return NULL; 96 } 97 98 bs = bdrv_new_root(); 99 blk->bs = bs; 100 bs->blk = blk; 101 return blk; 102 } 103 104 /* 105 * Calls blk_new_with_bs() and then calls bdrv_open() on the BlockDriverState. 106 * 107 * Just as with bdrv_open(), after having called this function the reference to 108 * @options belongs to the block layer (even on failure). 109 * 110 * TODO: Remove @filename and @flags; it should be possible to specify a whole 111 * BDS tree just by specifying the @options QDict (or @reference, 112 * alternatively). At the time of adding this function, this is not possible, 113 * though, so callers of this function have to be able to specify @filename and 114 * @flags. 115 */ 116 BlockBackend *blk_new_open(const char *name, const char *filename, 117 const char *reference, QDict *options, int flags, 118 Error **errp) 119 { 120 BlockBackend *blk; 121 int ret; 122 123 blk = blk_new_with_bs(name, errp); 124 if (!blk) { 125 QDECREF(options); 126 return NULL; 127 } 128 129 ret = bdrv_open(&blk->bs, filename, reference, options, flags, errp); 130 if (ret < 0) { 131 blk_unref(blk); 132 return NULL; 133 } 134 135 return blk; 136 } 137 138 static void blk_delete(BlockBackend *blk) 139 { 140 assert(!blk->refcnt); 141 assert(!blk->dev); 142 if (blk->bs) { 143 assert(blk->bs->blk == blk); 144 blk->bs->blk = NULL; 145 bdrv_unref(blk->bs); 146 blk->bs = NULL; 147 } 148 /* Avoid double-remove after blk_hide_on_behalf_of_hmp_drive_del() */ 149 if (blk->name[0]) { 150 QTAILQ_REMOVE(&blk_backends, blk, link); 151 } 152 g_free(blk->name); 153 drive_info_del(blk->legacy_dinfo); 154 g_free(blk); 155 } 156 157 static void drive_info_del(DriveInfo *dinfo) 158 { 159 if (!dinfo) { 160 return; 161 } 162 qemu_opts_del(dinfo->opts); 163 g_free(dinfo->serial); 164 g_free(dinfo); 165 } 166 167 /* 168 * Increment @blk's reference count. 169 * @blk must not be null. 170 */ 171 void blk_ref(BlockBackend *blk) 172 { 173 blk->refcnt++; 174 } 175 176 /* 177 * Decrement @blk's reference count. 178 * If this drops it to zero, destroy @blk. 179 * For convenience, do nothing if @blk is null. 180 */ 181 void blk_unref(BlockBackend *blk) 182 { 183 if (blk) { 184 assert(blk->refcnt > 0); 185 if (!--blk->refcnt) { 186 blk_delete(blk); 187 } 188 } 189 } 190 191 /* 192 * Return the BlockBackend after @blk. 193 * If @blk is null, return the first one. 194 * Else, return @blk's next sibling, which may be null. 195 * 196 * To iterate over all BlockBackends, do 197 * for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { 198 * ... 199 * } 200 */ 201 BlockBackend *blk_next(BlockBackend *blk) 202 { 203 return blk ? QTAILQ_NEXT(blk, link) : QTAILQ_FIRST(&blk_backends); 204 } 205 206 /* 207 * Return @blk's name, a non-null string. 208 * Wart: the name is empty iff @blk has been hidden with 209 * blk_hide_on_behalf_of_hmp_drive_del(). 210 */ 211 const char *blk_name(BlockBackend *blk) 212 { 213 return blk->name; 214 } 215 216 /* 217 * Return the BlockBackend with name @name if it exists, else null. 218 * @name must not be null. 219 */ 220 BlockBackend *blk_by_name(const char *name) 221 { 222 BlockBackend *blk; 223 224 assert(name); 225 QTAILQ_FOREACH(blk, &blk_backends, link) { 226 if (!strcmp(name, blk->name)) { 227 return blk; 228 } 229 } 230 return NULL; 231 } 232 233 /* 234 * Return the BlockDriverState attached to @blk if any, else null. 235 */ 236 BlockDriverState *blk_bs(BlockBackend *blk) 237 { 238 return blk->bs; 239 } 240 241 /* 242 * Changes the BlockDriverState attached to @blk 243 */ 244 void blk_set_bs(BlockBackend *blk, BlockDriverState *bs) 245 { 246 bdrv_ref(bs); 247 248 if (blk->bs) { 249 blk->bs->blk = NULL; 250 bdrv_unref(blk->bs); 251 } 252 assert(bs->blk == NULL); 253 254 blk->bs = bs; 255 bs->blk = blk; 256 } 257 258 /* 259 * Return @blk's DriveInfo if any, else null. 260 */ 261 DriveInfo *blk_legacy_dinfo(BlockBackend *blk) 262 { 263 return blk->legacy_dinfo; 264 } 265 266 /* 267 * Set @blk's DriveInfo to @dinfo, and return it. 268 * @blk must not have a DriveInfo set already. 269 * No other BlockBackend may have the same DriveInfo set. 270 */ 271 DriveInfo *blk_set_legacy_dinfo(BlockBackend *blk, DriveInfo *dinfo) 272 { 273 assert(!blk->legacy_dinfo); 274 return blk->legacy_dinfo = dinfo; 275 } 276 277 /* 278 * Return the BlockBackend with DriveInfo @dinfo. 279 * It must exist. 280 */ 281 BlockBackend *blk_by_legacy_dinfo(DriveInfo *dinfo) 282 { 283 BlockBackend *blk; 284 285 QTAILQ_FOREACH(blk, &blk_backends, link) { 286 if (blk->legacy_dinfo == dinfo) { 287 return blk; 288 } 289 } 290 abort(); 291 } 292 293 /* 294 * Hide @blk. 295 * @blk must not have been hidden already. 296 * Make attached BlockDriverState, if any, anonymous. 297 * Once hidden, @blk is invisible to all functions that don't receive 298 * it as argument. For example, blk_by_name() won't return it. 299 * Strictly for use by do_drive_del(). 300 * TODO get rid of it! 301 */ 302 void blk_hide_on_behalf_of_hmp_drive_del(BlockBackend *blk) 303 { 304 QTAILQ_REMOVE(&blk_backends, blk, link); 305 blk->name[0] = 0; 306 if (blk->bs) { 307 bdrv_make_anon(blk->bs); 308 } 309 } 310 311 /* 312 * Attach device model @dev to @blk. 313 * Return 0 on success, -EBUSY when a device model is attached already. 314 */ 315 int blk_attach_dev(BlockBackend *blk, void *dev) 316 /* TODO change to DeviceState *dev when all users are qdevified */ 317 { 318 if (blk->dev) { 319 return -EBUSY; 320 } 321 blk_ref(blk); 322 blk->dev = dev; 323 bdrv_iostatus_reset(blk->bs); 324 return 0; 325 } 326 327 /* 328 * Attach device model @dev to @blk. 329 * @blk must not have a device model attached already. 330 * TODO qdevified devices don't use this, remove when devices are qdevified 331 */ 332 void blk_attach_dev_nofail(BlockBackend *blk, void *dev) 333 { 334 if (blk_attach_dev(blk, dev) < 0) { 335 abort(); 336 } 337 } 338 339 /* 340 * Detach device model @dev from @blk. 341 * @dev must be currently attached to @blk. 342 */ 343 void blk_detach_dev(BlockBackend *blk, void *dev) 344 /* TODO change to DeviceState *dev when all users are qdevified */ 345 { 346 assert(blk->dev == dev); 347 blk->dev = NULL; 348 blk->dev_ops = NULL; 349 blk->dev_opaque = NULL; 350 bdrv_set_guest_block_size(blk->bs, 512); 351 blk_unref(blk); 352 } 353 354 /* 355 * Return the device model attached to @blk if any, else null. 356 */ 357 void *blk_get_attached_dev(BlockBackend *blk) 358 /* TODO change to return DeviceState * when all users are qdevified */ 359 { 360 return blk->dev; 361 } 362 363 /* 364 * Set @blk's device model callbacks to @ops. 365 * @opaque is the opaque argument to pass to the callbacks. 366 * This is for use by device models. 367 */ 368 void blk_set_dev_ops(BlockBackend *blk, const BlockDevOps *ops, 369 void *opaque) 370 { 371 blk->dev_ops = ops; 372 blk->dev_opaque = opaque; 373 } 374 375 /* 376 * Notify @blk's attached device model of media change. 377 * If @load is true, notify of media load. 378 * Else, notify of media eject. 379 * Also send DEVICE_TRAY_MOVED events as appropriate. 380 */ 381 void blk_dev_change_media_cb(BlockBackend *blk, bool load) 382 { 383 if (blk->dev_ops && blk->dev_ops->change_media_cb) { 384 bool tray_was_closed = !blk_dev_is_tray_open(blk); 385 386 blk->dev_ops->change_media_cb(blk->dev_opaque, load); 387 if (tray_was_closed) { 388 /* tray open */ 389 qapi_event_send_device_tray_moved(blk_name(blk), 390 true, &error_abort); 391 } 392 if (load) { 393 /* tray close */ 394 qapi_event_send_device_tray_moved(blk_name(blk), 395 false, &error_abort); 396 } 397 } 398 } 399 400 /* 401 * Does @blk's attached device model have removable media? 402 * %true if no device model is attached. 403 */ 404 bool blk_dev_has_removable_media(BlockBackend *blk) 405 { 406 return !blk->dev || (blk->dev_ops && blk->dev_ops->change_media_cb); 407 } 408 409 /* 410 * Notify @blk's attached device model of a media eject request. 411 * If @force is true, the medium is about to be yanked out forcefully. 412 */ 413 void blk_dev_eject_request(BlockBackend *blk, bool force) 414 { 415 if (blk->dev_ops && blk->dev_ops->eject_request_cb) { 416 blk->dev_ops->eject_request_cb(blk->dev_opaque, force); 417 } 418 } 419 420 /* 421 * Does @blk's attached device model have a tray, and is it open? 422 */ 423 bool blk_dev_is_tray_open(BlockBackend *blk) 424 { 425 if (blk->dev_ops && blk->dev_ops->is_tray_open) { 426 return blk->dev_ops->is_tray_open(blk->dev_opaque); 427 } 428 return false; 429 } 430 431 /* 432 * Does @blk's attached device model have the medium locked? 433 * %false if the device model has no such lock. 434 */ 435 bool blk_dev_is_medium_locked(BlockBackend *blk) 436 { 437 if (blk->dev_ops && blk->dev_ops->is_medium_locked) { 438 return blk->dev_ops->is_medium_locked(blk->dev_opaque); 439 } 440 return false; 441 } 442 443 /* 444 * Notify @blk's attached device model of a backend size change. 445 */ 446 void blk_dev_resize_cb(BlockBackend *blk) 447 { 448 if (blk->dev_ops && blk->dev_ops->resize_cb) { 449 blk->dev_ops->resize_cb(blk->dev_opaque); 450 } 451 } 452 453 void blk_iostatus_enable(BlockBackend *blk) 454 { 455 bdrv_iostatus_enable(blk->bs); 456 } 457 458 static int blk_check_byte_request(BlockBackend *blk, int64_t offset, 459 size_t size) 460 { 461 int64_t len; 462 463 if (size > INT_MAX) { 464 return -EIO; 465 } 466 467 if (!blk_is_inserted(blk)) { 468 return -ENOMEDIUM; 469 } 470 471 len = blk_getlength(blk); 472 if (len < 0) { 473 return len; 474 } 475 476 if (offset < 0) { 477 return -EIO; 478 } 479 480 if (offset > len || len - offset < size) { 481 return -EIO; 482 } 483 484 return 0; 485 } 486 487 static int blk_check_request(BlockBackend *blk, int64_t sector_num, 488 int nb_sectors) 489 { 490 if (sector_num < 0 || sector_num > INT64_MAX / BDRV_SECTOR_SIZE) { 491 return -EIO; 492 } 493 494 if (nb_sectors < 0 || nb_sectors > INT_MAX / BDRV_SECTOR_SIZE) { 495 return -EIO; 496 } 497 498 return blk_check_byte_request(blk, sector_num * BDRV_SECTOR_SIZE, 499 nb_sectors * BDRV_SECTOR_SIZE); 500 } 501 502 int blk_read(BlockBackend *blk, int64_t sector_num, uint8_t *buf, 503 int nb_sectors) 504 { 505 int ret = blk_check_request(blk, sector_num, nb_sectors); 506 if (ret < 0) { 507 return ret; 508 } 509 510 return bdrv_read(blk->bs, sector_num, buf, nb_sectors); 511 } 512 513 int blk_read_unthrottled(BlockBackend *blk, int64_t sector_num, uint8_t *buf, 514 int nb_sectors) 515 { 516 int ret = blk_check_request(blk, sector_num, nb_sectors); 517 if (ret < 0) { 518 return ret; 519 } 520 521 return bdrv_read_unthrottled(blk->bs, sector_num, buf, nb_sectors); 522 } 523 524 int blk_write(BlockBackend *blk, int64_t sector_num, const uint8_t *buf, 525 int nb_sectors) 526 { 527 int ret = blk_check_request(blk, sector_num, nb_sectors); 528 if (ret < 0) { 529 return ret; 530 } 531 532 return bdrv_write(blk->bs, sector_num, buf, nb_sectors); 533 } 534 535 int blk_write_zeroes(BlockBackend *blk, int64_t sector_num, 536 int nb_sectors, BdrvRequestFlags flags) 537 { 538 int ret = blk_check_request(blk, sector_num, nb_sectors); 539 if (ret < 0) { 540 return ret; 541 } 542 543 return bdrv_write_zeroes(blk->bs, sector_num, nb_sectors, flags); 544 } 545 546 static void error_callback_bh(void *opaque) 547 { 548 struct BlockBackendAIOCB *acb = opaque; 549 qemu_bh_delete(acb->bh); 550 acb->common.cb(acb->common.opaque, acb->ret); 551 qemu_aio_unref(acb); 552 } 553 554 static BlockAIOCB *abort_aio_request(BlockBackend *blk, BlockCompletionFunc *cb, 555 void *opaque, int ret) 556 { 557 struct BlockBackendAIOCB *acb; 558 QEMUBH *bh; 559 560 acb = blk_aio_get(&block_backend_aiocb_info, blk, cb, opaque); 561 acb->ret = ret; 562 563 bh = aio_bh_new(blk_get_aio_context(blk), error_callback_bh, acb); 564 acb->bh = bh; 565 qemu_bh_schedule(bh); 566 567 return &acb->common; 568 } 569 570 BlockAIOCB *blk_aio_write_zeroes(BlockBackend *blk, int64_t sector_num, 571 int nb_sectors, BdrvRequestFlags flags, 572 BlockCompletionFunc *cb, void *opaque) 573 { 574 int ret = blk_check_request(blk, sector_num, nb_sectors); 575 if (ret < 0) { 576 return abort_aio_request(blk, cb, opaque, ret); 577 } 578 579 return bdrv_aio_write_zeroes(blk->bs, sector_num, nb_sectors, flags, 580 cb, opaque); 581 } 582 583 int blk_pread(BlockBackend *blk, int64_t offset, void *buf, int count) 584 { 585 int ret = blk_check_byte_request(blk, offset, count); 586 if (ret < 0) { 587 return ret; 588 } 589 590 return bdrv_pread(blk->bs, offset, buf, count); 591 } 592 593 int blk_pwrite(BlockBackend *blk, int64_t offset, const void *buf, int count) 594 { 595 int ret = blk_check_byte_request(blk, offset, count); 596 if (ret < 0) { 597 return ret; 598 } 599 600 return bdrv_pwrite(blk->bs, offset, buf, count); 601 } 602 603 int64_t blk_getlength(BlockBackend *blk) 604 { 605 return bdrv_getlength(blk->bs); 606 } 607 608 void blk_get_geometry(BlockBackend *blk, uint64_t *nb_sectors_ptr) 609 { 610 bdrv_get_geometry(blk->bs, nb_sectors_ptr); 611 } 612 613 int64_t blk_nb_sectors(BlockBackend *blk) 614 { 615 return bdrv_nb_sectors(blk->bs); 616 } 617 618 BlockAIOCB *blk_aio_readv(BlockBackend *blk, int64_t sector_num, 619 QEMUIOVector *iov, int nb_sectors, 620 BlockCompletionFunc *cb, void *opaque) 621 { 622 int ret = blk_check_request(blk, sector_num, nb_sectors); 623 if (ret < 0) { 624 return abort_aio_request(blk, cb, opaque, ret); 625 } 626 627 return bdrv_aio_readv(blk->bs, sector_num, iov, nb_sectors, cb, opaque); 628 } 629 630 BlockAIOCB *blk_aio_writev(BlockBackend *blk, int64_t sector_num, 631 QEMUIOVector *iov, int nb_sectors, 632 BlockCompletionFunc *cb, void *opaque) 633 { 634 int ret = blk_check_request(blk, sector_num, nb_sectors); 635 if (ret < 0) { 636 return abort_aio_request(blk, cb, opaque, ret); 637 } 638 639 return bdrv_aio_writev(blk->bs, sector_num, iov, nb_sectors, cb, opaque); 640 } 641 642 BlockAIOCB *blk_aio_flush(BlockBackend *blk, 643 BlockCompletionFunc *cb, void *opaque) 644 { 645 return bdrv_aio_flush(blk->bs, cb, opaque); 646 } 647 648 BlockAIOCB *blk_aio_discard(BlockBackend *blk, 649 int64_t sector_num, int nb_sectors, 650 BlockCompletionFunc *cb, void *opaque) 651 { 652 int ret = blk_check_request(blk, sector_num, nb_sectors); 653 if (ret < 0) { 654 return abort_aio_request(blk, cb, opaque, ret); 655 } 656 657 return bdrv_aio_discard(blk->bs, sector_num, nb_sectors, cb, opaque); 658 } 659 660 void blk_aio_cancel(BlockAIOCB *acb) 661 { 662 bdrv_aio_cancel(acb); 663 } 664 665 void blk_aio_cancel_async(BlockAIOCB *acb) 666 { 667 bdrv_aio_cancel_async(acb); 668 } 669 670 int blk_aio_multiwrite(BlockBackend *blk, BlockRequest *reqs, int num_reqs) 671 { 672 int i, ret; 673 674 for (i = 0; i < num_reqs; i++) { 675 ret = blk_check_request(blk, reqs[i].sector, reqs[i].nb_sectors); 676 if (ret < 0) { 677 return ret; 678 } 679 } 680 681 return bdrv_aio_multiwrite(blk->bs, reqs, num_reqs); 682 } 683 684 int blk_ioctl(BlockBackend *blk, unsigned long int req, void *buf) 685 { 686 return bdrv_ioctl(blk->bs, req, buf); 687 } 688 689 BlockAIOCB *blk_aio_ioctl(BlockBackend *blk, unsigned long int req, void *buf, 690 BlockCompletionFunc *cb, void *opaque) 691 { 692 return bdrv_aio_ioctl(blk->bs, req, buf, cb, opaque); 693 } 694 695 int blk_co_discard(BlockBackend *blk, int64_t sector_num, int nb_sectors) 696 { 697 int ret = blk_check_request(blk, sector_num, nb_sectors); 698 if (ret < 0) { 699 return ret; 700 } 701 702 return bdrv_co_discard(blk->bs, sector_num, nb_sectors); 703 } 704 705 int blk_co_flush(BlockBackend *blk) 706 { 707 return bdrv_co_flush(blk->bs); 708 } 709 710 int blk_flush(BlockBackend *blk) 711 { 712 return bdrv_flush(blk->bs); 713 } 714 715 int blk_flush_all(void) 716 { 717 return bdrv_flush_all(); 718 } 719 720 void blk_drain(BlockBackend *blk) 721 { 722 bdrv_drain(blk->bs); 723 } 724 725 void blk_drain_all(void) 726 { 727 bdrv_drain_all(); 728 } 729 730 BlockdevOnError blk_get_on_error(BlockBackend *blk, bool is_read) 731 { 732 return bdrv_get_on_error(blk->bs, is_read); 733 } 734 735 BlockErrorAction blk_get_error_action(BlockBackend *blk, bool is_read, 736 int error) 737 { 738 return bdrv_get_error_action(blk->bs, is_read, error); 739 } 740 741 void blk_error_action(BlockBackend *blk, BlockErrorAction action, 742 bool is_read, int error) 743 { 744 bdrv_error_action(blk->bs, action, is_read, error); 745 } 746 747 int blk_is_read_only(BlockBackend *blk) 748 { 749 return bdrv_is_read_only(blk->bs); 750 } 751 752 int blk_is_sg(BlockBackend *blk) 753 { 754 return bdrv_is_sg(blk->bs); 755 } 756 757 int blk_enable_write_cache(BlockBackend *blk) 758 { 759 return bdrv_enable_write_cache(blk->bs); 760 } 761 762 void blk_set_enable_write_cache(BlockBackend *blk, bool wce) 763 { 764 bdrv_set_enable_write_cache(blk->bs, wce); 765 } 766 767 void blk_invalidate_cache(BlockBackend *blk, Error **errp) 768 { 769 bdrv_invalidate_cache(blk->bs, errp); 770 } 771 772 int blk_is_inserted(BlockBackend *blk) 773 { 774 return bdrv_is_inserted(blk->bs); 775 } 776 777 void blk_lock_medium(BlockBackend *blk, bool locked) 778 { 779 bdrv_lock_medium(blk->bs, locked); 780 } 781 782 void blk_eject(BlockBackend *blk, bool eject_flag) 783 { 784 bdrv_eject(blk->bs, eject_flag); 785 } 786 787 int blk_get_flags(BlockBackend *blk) 788 { 789 return bdrv_get_flags(blk->bs); 790 } 791 792 int blk_get_max_transfer_length(BlockBackend *blk) 793 { 794 return blk->bs->bl.max_transfer_length; 795 } 796 797 void blk_set_guest_block_size(BlockBackend *blk, int align) 798 { 799 bdrv_set_guest_block_size(blk->bs, align); 800 } 801 802 void *blk_blockalign(BlockBackend *blk, size_t size) 803 { 804 return qemu_blockalign(blk ? blk->bs : NULL, size); 805 } 806 807 bool blk_op_is_blocked(BlockBackend *blk, BlockOpType op, Error **errp) 808 { 809 return bdrv_op_is_blocked(blk->bs, op, errp); 810 } 811 812 void blk_op_unblock(BlockBackend *blk, BlockOpType op, Error *reason) 813 { 814 bdrv_op_unblock(blk->bs, op, reason); 815 } 816 817 void blk_op_block_all(BlockBackend *blk, Error *reason) 818 { 819 bdrv_op_block_all(blk->bs, reason); 820 } 821 822 void blk_op_unblock_all(BlockBackend *blk, Error *reason) 823 { 824 bdrv_op_unblock_all(blk->bs, reason); 825 } 826 827 AioContext *blk_get_aio_context(BlockBackend *blk) 828 { 829 return bdrv_get_aio_context(blk->bs); 830 } 831 832 void blk_set_aio_context(BlockBackend *blk, AioContext *new_context) 833 { 834 bdrv_set_aio_context(blk->bs, new_context); 835 } 836 837 void blk_add_aio_context_notifier(BlockBackend *blk, 838 void (*attached_aio_context)(AioContext *new_context, void *opaque), 839 void (*detach_aio_context)(void *opaque), void *opaque) 840 { 841 bdrv_add_aio_context_notifier(blk->bs, attached_aio_context, 842 detach_aio_context, opaque); 843 } 844 845 void blk_remove_aio_context_notifier(BlockBackend *blk, 846 void (*attached_aio_context)(AioContext *, 847 void *), 848 void (*detach_aio_context)(void *), 849 void *opaque) 850 { 851 bdrv_remove_aio_context_notifier(blk->bs, attached_aio_context, 852 detach_aio_context, opaque); 853 } 854 855 void blk_add_close_notifier(BlockBackend *blk, Notifier *notify) 856 { 857 bdrv_add_close_notifier(blk->bs, notify); 858 } 859 860 void blk_io_plug(BlockBackend *blk) 861 { 862 bdrv_io_plug(blk->bs); 863 } 864 865 void blk_io_unplug(BlockBackend *blk) 866 { 867 bdrv_io_unplug(blk->bs); 868 } 869 870 BlockAcctStats *blk_get_stats(BlockBackend *blk) 871 { 872 return bdrv_get_stats(blk->bs); 873 } 874 875 void *blk_aio_get(const AIOCBInfo *aiocb_info, BlockBackend *blk, 876 BlockCompletionFunc *cb, void *opaque) 877 { 878 return qemu_aio_get(aiocb_info, blk_bs(blk), cb, opaque); 879 } 880 881 int coroutine_fn blk_co_write_zeroes(BlockBackend *blk, int64_t sector_num, 882 int nb_sectors, BdrvRequestFlags flags) 883 { 884 int ret = blk_check_request(blk, sector_num, nb_sectors); 885 if (ret < 0) { 886 return ret; 887 } 888 889 return bdrv_co_write_zeroes(blk->bs, sector_num, nb_sectors, flags); 890 } 891 892 int blk_write_compressed(BlockBackend *blk, int64_t sector_num, 893 const uint8_t *buf, int nb_sectors) 894 { 895 int ret = blk_check_request(blk, sector_num, nb_sectors); 896 if (ret < 0) { 897 return ret; 898 } 899 900 return bdrv_write_compressed(blk->bs, sector_num, buf, nb_sectors); 901 } 902 903 int blk_truncate(BlockBackend *blk, int64_t offset) 904 { 905 return bdrv_truncate(blk->bs, offset); 906 } 907 908 int blk_discard(BlockBackend *blk, int64_t sector_num, int nb_sectors) 909 { 910 int ret = blk_check_request(blk, sector_num, nb_sectors); 911 if (ret < 0) { 912 return ret; 913 } 914 915 return bdrv_discard(blk->bs, sector_num, nb_sectors); 916 } 917 918 int blk_save_vmstate(BlockBackend *blk, const uint8_t *buf, 919 int64_t pos, int size) 920 { 921 return bdrv_save_vmstate(blk->bs, buf, pos, size); 922 } 923 924 int blk_load_vmstate(BlockBackend *blk, uint8_t *buf, int64_t pos, int size) 925 { 926 return bdrv_load_vmstate(blk->bs, buf, pos, size); 927 } 928 929 int blk_probe_blocksizes(BlockBackend *blk, BlockSizes *bsz) 930 { 931 return bdrv_probe_blocksizes(blk->bs, bsz); 932 } 933 934 int blk_probe_geometry(BlockBackend *blk, HDGeometry *geo) 935 { 936 return bdrv_probe_geometry(blk->bs, geo); 937 } 938