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 "qemu/osdep.h" 14 #include "sysemu/block-backend.h" 15 #include "block/block_int.h" 16 #include "block/blockjob.h" 17 #include "block/throttle-groups.h" 18 #include "sysemu/blockdev.h" 19 #include "sysemu/sysemu.h" 20 #include "qapi-event.h" 21 22 /* Number of coroutines to reserve per attached device model */ 23 #define COROUTINE_POOL_RESERVATION 64 24 25 #define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */ 26 27 static AioContext *blk_aiocb_get_aio_context(BlockAIOCB *acb); 28 29 struct BlockBackend { 30 char *name; 31 int refcnt; 32 BdrvChild *root; 33 DriveInfo *legacy_dinfo; /* null unless created by drive_new() */ 34 QTAILQ_ENTRY(BlockBackend) link; /* for block_backends */ 35 QTAILQ_ENTRY(BlockBackend) monitor_link; /* for monitor_block_backends */ 36 37 void *dev; /* attached device model, if any */ 38 /* TODO change to DeviceState when all users are qdevified */ 39 const BlockDevOps *dev_ops; 40 void *dev_opaque; 41 42 /* the block size for which the guest device expects atomicity */ 43 int guest_block_size; 44 45 /* If the BDS tree is removed, some of its options are stored here (which 46 * can be used to restore those options in the new BDS on insert) */ 47 BlockBackendRootState root_state; 48 49 /* I/O stats (display with "info blockstats"). */ 50 BlockAcctStats stats; 51 52 BlockdevOnError on_read_error, on_write_error; 53 bool iostatus_enabled; 54 BlockDeviceIoStatus iostatus; 55 56 bool allow_write_beyond_eof; 57 58 NotifierList remove_bs_notifiers, insert_bs_notifiers; 59 }; 60 61 typedef struct BlockBackendAIOCB { 62 BlockAIOCB common; 63 QEMUBH *bh; 64 BlockBackend *blk; 65 int ret; 66 } BlockBackendAIOCB; 67 68 static const AIOCBInfo block_backend_aiocb_info = { 69 .get_aio_context = blk_aiocb_get_aio_context, 70 .aiocb_size = sizeof(BlockBackendAIOCB), 71 }; 72 73 static void drive_info_del(DriveInfo *dinfo); 74 75 /* All BlockBackends */ 76 static QTAILQ_HEAD(, BlockBackend) block_backends = 77 QTAILQ_HEAD_INITIALIZER(block_backends); 78 79 /* All BlockBackends referenced by the monitor and which are iterated through by 80 * blk_next() */ 81 static QTAILQ_HEAD(, BlockBackend) monitor_block_backends = 82 QTAILQ_HEAD_INITIALIZER(monitor_block_backends); 83 84 static void blk_root_inherit_options(int *child_flags, QDict *child_options, 85 int parent_flags, QDict *parent_options) 86 { 87 /* We're not supposed to call this function for root nodes */ 88 abort(); 89 } 90 91 static const BdrvChildRole child_root = { 92 .inherit_options = blk_root_inherit_options, 93 }; 94 95 /* 96 * Create a new BlockBackend with a reference count of one. 97 * Store an error through @errp on failure, unless it's null. 98 * Return the new BlockBackend on success, null on failure. 99 */ 100 BlockBackend *blk_new(Error **errp) 101 { 102 BlockBackend *blk; 103 104 blk = g_new0(BlockBackend, 1); 105 blk->refcnt = 1; 106 notifier_list_init(&blk->remove_bs_notifiers); 107 notifier_list_init(&blk->insert_bs_notifiers); 108 QTAILQ_INSERT_TAIL(&block_backends, blk, link); 109 return blk; 110 } 111 112 /* 113 * Create a new BlockBackend with a new BlockDriverState attached. 114 * Otherwise just like blk_new(), which see. 115 */ 116 BlockBackend *blk_new_with_bs(Error **errp) 117 { 118 BlockBackend *blk; 119 BlockDriverState *bs; 120 121 blk = blk_new(errp); 122 if (!blk) { 123 return NULL; 124 } 125 126 bs = bdrv_new_root(); 127 blk->root = bdrv_root_attach_child(bs, "root", &child_root); 128 bs->blk = blk; 129 return blk; 130 } 131 132 /* 133 * Calls blk_new_with_bs() and then calls bdrv_open() on the BlockDriverState. 134 * 135 * Just as with bdrv_open(), after having called this function the reference to 136 * @options belongs to the block layer (even on failure). 137 * 138 * TODO: Remove @filename and @flags; it should be possible to specify a whole 139 * BDS tree just by specifying the @options QDict (or @reference, 140 * alternatively). At the time of adding this function, this is not possible, 141 * though, so callers of this function have to be able to specify @filename and 142 * @flags. 143 */ 144 BlockBackend *blk_new_open(const char *filename, const char *reference, 145 QDict *options, int flags, Error **errp) 146 { 147 BlockBackend *blk; 148 int ret; 149 150 blk = blk_new_with_bs(errp); 151 if (!blk) { 152 QDECREF(options); 153 return NULL; 154 } 155 156 ret = bdrv_open(&blk->root->bs, filename, reference, options, flags, errp); 157 if (ret < 0) { 158 blk_unref(blk); 159 return NULL; 160 } 161 162 return blk; 163 } 164 165 static void blk_delete(BlockBackend *blk) 166 { 167 assert(!blk->refcnt); 168 assert(!blk->name); 169 assert(!blk->dev); 170 if (blk->root) { 171 blk_remove_bs(blk); 172 } 173 assert(QLIST_EMPTY(&blk->remove_bs_notifiers.notifiers)); 174 assert(QLIST_EMPTY(&blk->insert_bs_notifiers.notifiers)); 175 if (blk->root_state.throttle_state) { 176 g_free(blk->root_state.throttle_group); 177 throttle_group_unref(blk->root_state.throttle_state); 178 } 179 QTAILQ_REMOVE(&block_backends, blk, link); 180 drive_info_del(blk->legacy_dinfo); 181 block_acct_cleanup(&blk->stats); 182 g_free(blk); 183 } 184 185 static void drive_info_del(DriveInfo *dinfo) 186 { 187 if (!dinfo) { 188 return; 189 } 190 qemu_opts_del(dinfo->opts); 191 g_free(dinfo->serial); 192 g_free(dinfo); 193 } 194 195 int blk_get_refcnt(BlockBackend *blk) 196 { 197 return blk ? blk->refcnt : 0; 198 } 199 200 /* 201 * Increment @blk's reference count. 202 * @blk must not be null. 203 */ 204 void blk_ref(BlockBackend *blk) 205 { 206 blk->refcnt++; 207 } 208 209 /* 210 * Decrement @blk's reference count. 211 * If this drops it to zero, destroy @blk. 212 * For convenience, do nothing if @blk is null. 213 */ 214 void blk_unref(BlockBackend *blk) 215 { 216 if (blk) { 217 assert(blk->refcnt > 0); 218 if (!--blk->refcnt) { 219 blk_delete(blk); 220 } 221 } 222 } 223 224 /* 225 * Behaves similarly to blk_next() but iterates over all BlockBackends, even the 226 * ones which are hidden (i.e. are not referenced by the monitor). 227 */ 228 static BlockBackend *blk_all_next(BlockBackend *blk) 229 { 230 return blk ? QTAILQ_NEXT(blk, link) 231 : QTAILQ_FIRST(&block_backends); 232 } 233 234 void blk_remove_all_bs(void) 235 { 236 BlockBackend *blk = NULL; 237 238 while ((blk = blk_all_next(blk)) != NULL) { 239 AioContext *ctx = blk_get_aio_context(blk); 240 241 aio_context_acquire(ctx); 242 if (blk->root) { 243 blk_remove_bs(blk); 244 } 245 aio_context_release(ctx); 246 } 247 } 248 249 /* 250 * Return the monitor-owned BlockBackend after @blk. 251 * If @blk is null, return the first one. 252 * Else, return @blk's next sibling, which may be null. 253 * 254 * To iterate over all BlockBackends, do 255 * for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { 256 * ... 257 * } 258 */ 259 BlockBackend *blk_next(BlockBackend *blk) 260 { 261 return blk ? QTAILQ_NEXT(blk, monitor_link) 262 : QTAILQ_FIRST(&monitor_block_backends); 263 } 264 265 /* 266 * Iterates over all BlockDriverStates which are attached to a BlockBackend. 267 * This function is for use by bdrv_next(). 268 * 269 * @bs must be NULL or a BDS that is attached to a BB. 270 */ 271 BlockDriverState *blk_next_root_bs(BlockDriverState *bs) 272 { 273 BlockBackend *blk; 274 275 if (bs) { 276 assert(bs->blk); 277 blk = bs->blk; 278 } else { 279 blk = NULL; 280 } 281 282 do { 283 blk = blk_all_next(blk); 284 } while (blk && !blk->root); 285 286 return blk ? blk->root->bs : NULL; 287 } 288 289 /* 290 * Add a BlockBackend into the list of backends referenced by the monitor, with 291 * the given @name acting as the handle for the monitor. 292 * Strictly for use by blockdev.c. 293 * 294 * @name must not be null or empty. 295 * 296 * Returns true on success and false on failure. In the latter case, an Error 297 * object is returned through @errp. 298 */ 299 bool monitor_add_blk(BlockBackend *blk, const char *name, Error **errp) 300 { 301 assert(!blk->name); 302 assert(name && name[0]); 303 304 if (!id_wellformed(name)) { 305 error_setg(errp, "Invalid device name"); 306 return false; 307 } 308 if (blk_by_name(name)) { 309 error_setg(errp, "Device with id '%s' already exists", name); 310 return false; 311 } 312 if (bdrv_find_node(name)) { 313 error_setg(errp, 314 "Device name '%s' conflicts with an existing node name", 315 name); 316 return false; 317 } 318 319 blk->name = g_strdup(name); 320 QTAILQ_INSERT_TAIL(&monitor_block_backends, blk, monitor_link); 321 return true; 322 } 323 324 /* 325 * Remove a BlockBackend from the list of backends referenced by the monitor. 326 * Strictly for use by blockdev.c. 327 */ 328 void monitor_remove_blk(BlockBackend *blk) 329 { 330 if (!blk->name) { 331 return; 332 } 333 334 QTAILQ_REMOVE(&monitor_block_backends, blk, monitor_link); 335 g_free(blk->name); 336 blk->name = NULL; 337 } 338 339 /* 340 * Return @blk's name, a non-null string. 341 * Returns an empty string iff @blk is not referenced by the monitor. 342 */ 343 const char *blk_name(BlockBackend *blk) 344 { 345 return blk->name ?: ""; 346 } 347 348 /* 349 * Return the BlockBackend with name @name if it exists, else null. 350 * @name must not be null. 351 */ 352 BlockBackend *blk_by_name(const char *name) 353 { 354 BlockBackend *blk = NULL; 355 356 assert(name); 357 while ((blk = blk_next(blk)) != NULL) { 358 if (!strcmp(name, blk->name)) { 359 return blk; 360 } 361 } 362 return NULL; 363 } 364 365 /* 366 * Return the BlockDriverState attached to @blk if any, else null. 367 */ 368 BlockDriverState *blk_bs(BlockBackend *blk) 369 { 370 return blk->root ? blk->root->bs : NULL; 371 } 372 373 /* 374 * Changes the BlockDriverState attached to @blk 375 */ 376 void blk_set_bs(BlockBackend *blk, BlockDriverState *bs) 377 { 378 bdrv_ref(bs); 379 380 if (blk->root) { 381 blk->root->bs->blk = NULL; 382 bdrv_root_unref_child(blk->root); 383 } 384 assert(bs->blk == NULL); 385 386 blk->root = bdrv_root_attach_child(bs, "root", &child_root); 387 bs->blk = blk; 388 } 389 390 /* 391 * Return @blk's DriveInfo if any, else null. 392 */ 393 DriveInfo *blk_legacy_dinfo(BlockBackend *blk) 394 { 395 return blk->legacy_dinfo; 396 } 397 398 /* 399 * Set @blk's DriveInfo to @dinfo, and return it. 400 * @blk must not have a DriveInfo set already. 401 * No other BlockBackend may have the same DriveInfo set. 402 */ 403 DriveInfo *blk_set_legacy_dinfo(BlockBackend *blk, DriveInfo *dinfo) 404 { 405 assert(!blk->legacy_dinfo); 406 return blk->legacy_dinfo = dinfo; 407 } 408 409 /* 410 * Return the BlockBackend with DriveInfo @dinfo. 411 * It must exist. 412 */ 413 BlockBackend *blk_by_legacy_dinfo(DriveInfo *dinfo) 414 { 415 BlockBackend *blk = NULL; 416 417 while ((blk = blk_next(blk)) != NULL) { 418 if (blk->legacy_dinfo == dinfo) { 419 return blk; 420 } 421 } 422 abort(); 423 } 424 425 /* 426 * Disassociates the currently associated BlockDriverState from @blk. 427 */ 428 void blk_remove_bs(BlockBackend *blk) 429 { 430 assert(blk->root->bs->blk == blk); 431 432 notifier_list_notify(&blk->remove_bs_notifiers, blk); 433 434 blk_update_root_state(blk); 435 436 blk->root->bs->blk = NULL; 437 bdrv_root_unref_child(blk->root); 438 blk->root = NULL; 439 } 440 441 /* 442 * Associates a new BlockDriverState with @blk. 443 */ 444 void blk_insert_bs(BlockBackend *blk, BlockDriverState *bs) 445 { 446 assert(!blk->root && !bs->blk); 447 bdrv_ref(bs); 448 blk->root = bdrv_root_attach_child(bs, "root", &child_root); 449 bs->blk = blk; 450 451 notifier_list_notify(&blk->insert_bs_notifiers, blk); 452 } 453 454 /* 455 * Attach device model @dev to @blk. 456 * Return 0 on success, -EBUSY when a device model is attached already. 457 */ 458 int blk_attach_dev(BlockBackend *blk, void *dev) 459 /* TODO change to DeviceState *dev when all users are qdevified */ 460 { 461 if (blk->dev) { 462 return -EBUSY; 463 } 464 blk_ref(blk); 465 blk->dev = dev; 466 blk_iostatus_reset(blk); 467 return 0; 468 } 469 470 /* 471 * Attach device model @dev to @blk. 472 * @blk must not have a device model attached already. 473 * TODO qdevified devices don't use this, remove when devices are qdevified 474 */ 475 void blk_attach_dev_nofail(BlockBackend *blk, void *dev) 476 { 477 if (blk_attach_dev(blk, dev) < 0) { 478 abort(); 479 } 480 } 481 482 /* 483 * Detach device model @dev from @blk. 484 * @dev must be currently attached to @blk. 485 */ 486 void blk_detach_dev(BlockBackend *blk, void *dev) 487 /* TODO change to DeviceState *dev when all users are qdevified */ 488 { 489 assert(blk->dev == dev); 490 blk->dev = NULL; 491 blk->dev_ops = NULL; 492 blk->dev_opaque = NULL; 493 blk->guest_block_size = 512; 494 blk_unref(blk); 495 } 496 497 /* 498 * Return the device model attached to @blk if any, else null. 499 */ 500 void *blk_get_attached_dev(BlockBackend *blk) 501 /* TODO change to return DeviceState * when all users are qdevified */ 502 { 503 return blk->dev; 504 } 505 506 /* 507 * Set @blk's device model callbacks to @ops. 508 * @opaque is the opaque argument to pass to the callbacks. 509 * This is for use by device models. 510 */ 511 void blk_set_dev_ops(BlockBackend *blk, const BlockDevOps *ops, 512 void *opaque) 513 { 514 blk->dev_ops = ops; 515 blk->dev_opaque = opaque; 516 } 517 518 /* 519 * Notify @blk's attached device model of media change. 520 * If @load is true, notify of media load. 521 * Else, notify of media eject. 522 * Also send DEVICE_TRAY_MOVED events as appropriate. 523 */ 524 void blk_dev_change_media_cb(BlockBackend *blk, bool load) 525 { 526 if (blk->dev_ops && blk->dev_ops->change_media_cb) { 527 bool tray_was_open, tray_is_open; 528 529 tray_was_open = blk_dev_is_tray_open(blk); 530 blk->dev_ops->change_media_cb(blk->dev_opaque, load); 531 tray_is_open = blk_dev_is_tray_open(blk); 532 533 if (tray_was_open != tray_is_open) { 534 qapi_event_send_device_tray_moved(blk_name(blk), tray_is_open, 535 &error_abort); 536 } 537 } 538 } 539 540 /* 541 * Does @blk's attached device model have removable media? 542 * %true if no device model is attached. 543 */ 544 bool blk_dev_has_removable_media(BlockBackend *blk) 545 { 546 return !blk->dev || (blk->dev_ops && blk->dev_ops->change_media_cb); 547 } 548 549 /* 550 * Does @blk's attached device model have a tray? 551 */ 552 bool blk_dev_has_tray(BlockBackend *blk) 553 { 554 return blk->dev_ops && blk->dev_ops->is_tray_open; 555 } 556 557 /* 558 * Notify @blk's attached device model of a media eject request. 559 * If @force is true, the medium is about to be yanked out forcefully. 560 */ 561 void blk_dev_eject_request(BlockBackend *blk, bool force) 562 { 563 if (blk->dev_ops && blk->dev_ops->eject_request_cb) { 564 blk->dev_ops->eject_request_cb(blk->dev_opaque, force); 565 } 566 } 567 568 /* 569 * Does @blk's attached device model have a tray, and is it open? 570 */ 571 bool blk_dev_is_tray_open(BlockBackend *blk) 572 { 573 if (blk_dev_has_tray(blk)) { 574 return blk->dev_ops->is_tray_open(blk->dev_opaque); 575 } 576 return false; 577 } 578 579 /* 580 * Does @blk's attached device model have the medium locked? 581 * %false if the device model has no such lock. 582 */ 583 bool blk_dev_is_medium_locked(BlockBackend *blk) 584 { 585 if (blk->dev_ops && blk->dev_ops->is_medium_locked) { 586 return blk->dev_ops->is_medium_locked(blk->dev_opaque); 587 } 588 return false; 589 } 590 591 /* 592 * Notify @blk's attached device model of a backend size change. 593 */ 594 void blk_dev_resize_cb(BlockBackend *blk) 595 { 596 if (blk->dev_ops && blk->dev_ops->resize_cb) { 597 blk->dev_ops->resize_cb(blk->dev_opaque); 598 } 599 } 600 601 void blk_iostatus_enable(BlockBackend *blk) 602 { 603 blk->iostatus_enabled = true; 604 blk->iostatus = BLOCK_DEVICE_IO_STATUS_OK; 605 } 606 607 /* The I/O status is only enabled if the drive explicitly 608 * enables it _and_ the VM is configured to stop on errors */ 609 bool blk_iostatus_is_enabled(const BlockBackend *blk) 610 { 611 return (blk->iostatus_enabled && 612 (blk->on_write_error == BLOCKDEV_ON_ERROR_ENOSPC || 613 blk->on_write_error == BLOCKDEV_ON_ERROR_STOP || 614 blk->on_read_error == BLOCKDEV_ON_ERROR_STOP)); 615 } 616 617 BlockDeviceIoStatus blk_iostatus(const BlockBackend *blk) 618 { 619 return blk->iostatus; 620 } 621 622 void blk_iostatus_disable(BlockBackend *blk) 623 { 624 blk->iostatus_enabled = false; 625 } 626 627 void blk_iostatus_reset(BlockBackend *blk) 628 { 629 if (blk_iostatus_is_enabled(blk)) { 630 BlockDriverState *bs = blk_bs(blk); 631 blk->iostatus = BLOCK_DEVICE_IO_STATUS_OK; 632 if (bs && bs->job) { 633 block_job_iostatus_reset(bs->job); 634 } 635 } 636 } 637 638 void blk_iostatus_set_err(BlockBackend *blk, int error) 639 { 640 assert(blk_iostatus_is_enabled(blk)); 641 if (blk->iostatus == BLOCK_DEVICE_IO_STATUS_OK) { 642 blk->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE : 643 BLOCK_DEVICE_IO_STATUS_FAILED; 644 } 645 } 646 647 void blk_set_allow_write_beyond_eof(BlockBackend *blk, bool allow) 648 { 649 blk->allow_write_beyond_eof = allow; 650 } 651 652 static int blk_check_byte_request(BlockBackend *blk, int64_t offset, 653 size_t size) 654 { 655 int64_t len; 656 657 if (size > INT_MAX) { 658 return -EIO; 659 } 660 661 if (!blk_is_available(blk)) { 662 return -ENOMEDIUM; 663 } 664 665 if (offset < 0) { 666 return -EIO; 667 } 668 669 if (!blk->allow_write_beyond_eof) { 670 len = blk_getlength(blk); 671 if (len < 0) { 672 return len; 673 } 674 675 if (offset > len || len - offset < size) { 676 return -EIO; 677 } 678 } 679 680 return 0; 681 } 682 683 static int blk_check_request(BlockBackend *blk, int64_t sector_num, 684 int nb_sectors) 685 { 686 if (sector_num < 0 || sector_num > INT64_MAX / BDRV_SECTOR_SIZE) { 687 return -EIO; 688 } 689 690 if (nb_sectors < 0 || nb_sectors > INT_MAX / BDRV_SECTOR_SIZE) { 691 return -EIO; 692 } 693 694 return blk_check_byte_request(blk, sector_num * BDRV_SECTOR_SIZE, 695 nb_sectors * BDRV_SECTOR_SIZE); 696 } 697 698 static int coroutine_fn blk_co_preadv(BlockBackend *blk, int64_t offset, 699 unsigned int bytes, QEMUIOVector *qiov, 700 BdrvRequestFlags flags) 701 { 702 int ret = blk_check_byte_request(blk, offset, bytes); 703 if (ret < 0) { 704 return ret; 705 } 706 707 return bdrv_co_do_preadv(blk_bs(blk), offset, bytes, qiov, flags); 708 } 709 710 static int coroutine_fn blk_co_pwritev(BlockBackend *blk, int64_t offset, 711 unsigned int bytes, QEMUIOVector *qiov, 712 BdrvRequestFlags flags) 713 { 714 int ret = blk_check_byte_request(blk, offset, bytes); 715 if (ret < 0) { 716 return ret; 717 } 718 719 return bdrv_co_do_pwritev(blk_bs(blk), offset, bytes, qiov, flags); 720 } 721 722 typedef struct BlkRwCo { 723 BlockBackend *blk; 724 int64_t offset; 725 QEMUIOVector *qiov; 726 int ret; 727 BdrvRequestFlags flags; 728 } BlkRwCo; 729 730 static void blk_read_entry(void *opaque) 731 { 732 BlkRwCo *rwco = opaque; 733 734 rwco->ret = blk_co_preadv(rwco->blk, rwco->offset, rwco->qiov->size, 735 rwco->qiov, rwco->flags); 736 } 737 738 static void blk_write_entry(void *opaque) 739 { 740 BlkRwCo *rwco = opaque; 741 742 rwco->ret = blk_co_pwritev(rwco->blk, rwco->offset, rwco->qiov->size, 743 rwco->qiov, rwco->flags); 744 } 745 746 static int blk_prw(BlockBackend *blk, int64_t offset, uint8_t *buf, 747 int64_t bytes, CoroutineEntry co_entry, 748 BdrvRequestFlags flags) 749 { 750 AioContext *aio_context; 751 QEMUIOVector qiov; 752 struct iovec iov; 753 Coroutine *co; 754 BlkRwCo rwco; 755 756 iov = (struct iovec) { 757 .iov_base = buf, 758 .iov_len = bytes, 759 }; 760 qemu_iovec_init_external(&qiov, &iov, 1); 761 762 rwco = (BlkRwCo) { 763 .blk = blk, 764 .offset = offset, 765 .qiov = &qiov, 766 .flags = flags, 767 .ret = NOT_DONE, 768 }; 769 770 co = qemu_coroutine_create(co_entry); 771 qemu_coroutine_enter(co, &rwco); 772 773 aio_context = blk_get_aio_context(blk); 774 while (rwco.ret == NOT_DONE) { 775 aio_poll(aio_context, true); 776 } 777 778 return rwco.ret; 779 } 780 781 static int blk_rw(BlockBackend *blk, int64_t sector_num, uint8_t *buf, 782 int nb_sectors, CoroutineEntry co_entry, 783 BdrvRequestFlags flags) 784 { 785 if (nb_sectors < 0 || nb_sectors > BDRV_REQUEST_MAX_SECTORS) { 786 return -EINVAL; 787 } 788 789 return blk_prw(blk, sector_num << BDRV_SECTOR_BITS, buf, 790 nb_sectors << BDRV_SECTOR_BITS, co_entry, flags); 791 } 792 793 int blk_read(BlockBackend *blk, int64_t sector_num, uint8_t *buf, 794 int nb_sectors) 795 { 796 return blk_rw(blk, sector_num, buf, nb_sectors, blk_read_entry, 0); 797 } 798 799 int blk_read_unthrottled(BlockBackend *blk, int64_t sector_num, uint8_t *buf, 800 int nb_sectors) 801 { 802 BlockDriverState *bs = blk_bs(blk); 803 bool enabled; 804 int ret; 805 806 ret = blk_check_request(blk, sector_num, nb_sectors); 807 if (ret < 0) { 808 return ret; 809 } 810 811 enabled = bs->io_limits_enabled; 812 bs->io_limits_enabled = false; 813 ret = blk_read(blk, sector_num, buf, nb_sectors); 814 bs->io_limits_enabled = enabled; 815 return ret; 816 } 817 818 int blk_write(BlockBackend *blk, int64_t sector_num, const uint8_t *buf, 819 int nb_sectors) 820 { 821 return blk_rw(blk, sector_num, (uint8_t*) buf, nb_sectors, 822 blk_write_entry, 0); 823 } 824 825 int blk_write_zeroes(BlockBackend *blk, int64_t sector_num, 826 int nb_sectors, BdrvRequestFlags flags) 827 { 828 return blk_rw(blk, sector_num, NULL, nb_sectors, blk_write_entry, 829 BDRV_REQ_ZERO_WRITE); 830 } 831 832 static void error_callback_bh(void *opaque) 833 { 834 struct BlockBackendAIOCB *acb = opaque; 835 qemu_bh_delete(acb->bh); 836 acb->common.cb(acb->common.opaque, acb->ret); 837 qemu_aio_unref(acb); 838 } 839 840 BlockAIOCB *blk_abort_aio_request(BlockBackend *blk, 841 BlockCompletionFunc *cb, 842 void *opaque, int ret) 843 { 844 struct BlockBackendAIOCB *acb; 845 QEMUBH *bh; 846 847 acb = blk_aio_get(&block_backend_aiocb_info, blk, cb, opaque); 848 acb->blk = blk; 849 acb->ret = ret; 850 851 bh = aio_bh_new(blk_get_aio_context(blk), error_callback_bh, acb); 852 acb->bh = bh; 853 qemu_bh_schedule(bh); 854 855 return &acb->common; 856 } 857 858 typedef struct BlkAioEmAIOCB { 859 BlockAIOCB common; 860 BlkRwCo rwco; 861 bool has_returned; 862 QEMUBH* bh; 863 } BlkAioEmAIOCB; 864 865 static const AIOCBInfo blk_aio_em_aiocb_info = { 866 .aiocb_size = sizeof(BlkAioEmAIOCB), 867 }; 868 869 static void blk_aio_complete(BlkAioEmAIOCB *acb) 870 { 871 if (acb->bh) { 872 assert(acb->has_returned); 873 qemu_bh_delete(acb->bh); 874 } 875 if (acb->has_returned) { 876 acb->common.cb(acb->common.opaque, acb->rwco.ret); 877 qemu_aio_unref(acb); 878 } 879 } 880 881 static void blk_aio_complete_bh(void *opaque) 882 { 883 blk_aio_complete(opaque); 884 } 885 886 static BlockAIOCB *blk_aio_prwv(BlockBackend *blk, int64_t offset, 887 QEMUIOVector *qiov, CoroutineEntry co_entry, 888 BdrvRequestFlags flags, 889 BlockCompletionFunc *cb, void *opaque) 890 { 891 BlkAioEmAIOCB *acb; 892 Coroutine *co; 893 894 acb = blk_aio_get(&blk_aio_em_aiocb_info, blk, cb, opaque); 895 acb->rwco = (BlkRwCo) { 896 .blk = blk, 897 .offset = offset, 898 .qiov = qiov, 899 .flags = flags, 900 .ret = NOT_DONE, 901 }; 902 acb->bh = NULL; 903 acb->has_returned = false; 904 905 co = qemu_coroutine_create(co_entry); 906 qemu_coroutine_enter(co, acb); 907 908 acb->has_returned = true; 909 if (acb->rwco.ret != NOT_DONE) { 910 acb->bh = aio_bh_new(blk_get_aio_context(blk), blk_aio_complete_bh, acb); 911 qemu_bh_schedule(acb->bh); 912 } 913 914 return &acb->common; 915 } 916 917 static void blk_aio_read_entry(void *opaque) 918 { 919 BlkAioEmAIOCB *acb = opaque; 920 BlkRwCo *rwco = &acb->rwco; 921 922 rwco->ret = blk_co_preadv(rwco->blk, rwco->offset, rwco->qiov->size, 923 rwco->qiov, rwco->flags); 924 blk_aio_complete(acb); 925 } 926 927 static void blk_aio_write_entry(void *opaque) 928 { 929 BlkAioEmAIOCB *acb = opaque; 930 BlkRwCo *rwco = &acb->rwco; 931 932 rwco->ret = blk_co_pwritev(rwco->blk, rwco->offset, 933 rwco->qiov ? rwco->qiov->size : 0, 934 rwco->qiov, rwco->flags); 935 blk_aio_complete(acb); 936 } 937 938 BlockAIOCB *blk_aio_write_zeroes(BlockBackend *blk, int64_t sector_num, 939 int nb_sectors, BdrvRequestFlags flags, 940 BlockCompletionFunc *cb, void *opaque) 941 { 942 if (nb_sectors < 0 || nb_sectors > BDRV_REQUEST_MAX_SECTORS) { 943 return blk_abort_aio_request(blk, cb, opaque, -EINVAL); 944 } 945 946 return blk_aio_prwv(blk, sector_num << BDRV_SECTOR_BITS, NULL, 947 blk_aio_write_entry, BDRV_REQ_ZERO_WRITE, cb, opaque); 948 } 949 950 int blk_pread(BlockBackend *blk, int64_t offset, void *buf, int count) 951 { 952 int ret = blk_prw(blk, offset, buf, count, blk_read_entry, 0); 953 if (ret < 0) { 954 return ret; 955 } 956 return count; 957 } 958 959 int blk_pwrite(BlockBackend *blk, int64_t offset, const void *buf, int count) 960 { 961 int ret = blk_prw(blk, offset, (void*) buf, count, blk_write_entry, 0); 962 if (ret < 0) { 963 return ret; 964 } 965 return count; 966 } 967 968 int64_t blk_getlength(BlockBackend *blk) 969 { 970 if (!blk_is_available(blk)) { 971 return -ENOMEDIUM; 972 } 973 974 return bdrv_getlength(blk_bs(blk)); 975 } 976 977 void blk_get_geometry(BlockBackend *blk, uint64_t *nb_sectors_ptr) 978 { 979 if (!blk_bs(blk)) { 980 *nb_sectors_ptr = 0; 981 } else { 982 bdrv_get_geometry(blk_bs(blk), nb_sectors_ptr); 983 } 984 } 985 986 int64_t blk_nb_sectors(BlockBackend *blk) 987 { 988 if (!blk_is_available(blk)) { 989 return -ENOMEDIUM; 990 } 991 992 return bdrv_nb_sectors(blk_bs(blk)); 993 } 994 995 BlockAIOCB *blk_aio_readv(BlockBackend *blk, int64_t sector_num, 996 QEMUIOVector *iov, int nb_sectors, 997 BlockCompletionFunc *cb, void *opaque) 998 { 999 if (nb_sectors < 0 || nb_sectors > BDRV_REQUEST_MAX_SECTORS) { 1000 return blk_abort_aio_request(blk, cb, opaque, -EINVAL); 1001 } 1002 1003 return blk_aio_prwv(blk, sector_num << BDRV_SECTOR_BITS, iov, 1004 blk_aio_read_entry, 0, cb, opaque); 1005 } 1006 1007 BlockAIOCB *blk_aio_writev(BlockBackend *blk, int64_t sector_num, 1008 QEMUIOVector *iov, int nb_sectors, 1009 BlockCompletionFunc *cb, void *opaque) 1010 { 1011 if (nb_sectors < 0 || nb_sectors > BDRV_REQUEST_MAX_SECTORS) { 1012 return blk_abort_aio_request(blk, cb, opaque, -EINVAL); 1013 } 1014 1015 return blk_aio_prwv(blk, sector_num << BDRV_SECTOR_BITS, iov, 1016 blk_aio_write_entry, 0, cb, opaque); 1017 } 1018 1019 BlockAIOCB *blk_aio_flush(BlockBackend *blk, 1020 BlockCompletionFunc *cb, void *opaque) 1021 { 1022 if (!blk_is_available(blk)) { 1023 return blk_abort_aio_request(blk, cb, opaque, -ENOMEDIUM); 1024 } 1025 1026 return bdrv_aio_flush(blk_bs(blk), cb, opaque); 1027 } 1028 1029 BlockAIOCB *blk_aio_discard(BlockBackend *blk, 1030 int64_t sector_num, int nb_sectors, 1031 BlockCompletionFunc *cb, void *opaque) 1032 { 1033 int ret = blk_check_request(blk, sector_num, nb_sectors); 1034 if (ret < 0) { 1035 return blk_abort_aio_request(blk, cb, opaque, ret); 1036 } 1037 1038 return bdrv_aio_discard(blk_bs(blk), sector_num, nb_sectors, cb, opaque); 1039 } 1040 1041 void blk_aio_cancel(BlockAIOCB *acb) 1042 { 1043 bdrv_aio_cancel(acb); 1044 } 1045 1046 void blk_aio_cancel_async(BlockAIOCB *acb) 1047 { 1048 bdrv_aio_cancel_async(acb); 1049 } 1050 1051 int blk_aio_multiwrite(BlockBackend *blk, BlockRequest *reqs, int num_reqs) 1052 { 1053 int i, ret; 1054 1055 for (i = 0; i < num_reqs; i++) { 1056 ret = blk_check_request(blk, reqs[i].sector, reqs[i].nb_sectors); 1057 if (ret < 0) { 1058 return ret; 1059 } 1060 } 1061 1062 return bdrv_aio_multiwrite(blk_bs(blk), reqs, num_reqs); 1063 } 1064 1065 int blk_ioctl(BlockBackend *blk, unsigned long int req, void *buf) 1066 { 1067 if (!blk_is_available(blk)) { 1068 return -ENOMEDIUM; 1069 } 1070 1071 return bdrv_ioctl(blk_bs(blk), req, buf); 1072 } 1073 1074 BlockAIOCB *blk_aio_ioctl(BlockBackend *blk, unsigned long int req, void *buf, 1075 BlockCompletionFunc *cb, void *opaque) 1076 { 1077 if (!blk_is_available(blk)) { 1078 return blk_abort_aio_request(blk, cb, opaque, -ENOMEDIUM); 1079 } 1080 1081 return bdrv_aio_ioctl(blk_bs(blk), req, buf, cb, opaque); 1082 } 1083 1084 int blk_co_discard(BlockBackend *blk, int64_t sector_num, int nb_sectors) 1085 { 1086 int ret = blk_check_request(blk, sector_num, nb_sectors); 1087 if (ret < 0) { 1088 return ret; 1089 } 1090 1091 return bdrv_co_discard(blk_bs(blk), sector_num, nb_sectors); 1092 } 1093 1094 int blk_co_flush(BlockBackend *blk) 1095 { 1096 if (!blk_is_available(blk)) { 1097 return -ENOMEDIUM; 1098 } 1099 1100 return bdrv_co_flush(blk_bs(blk)); 1101 } 1102 1103 int blk_flush(BlockBackend *blk) 1104 { 1105 if (!blk_is_available(blk)) { 1106 return -ENOMEDIUM; 1107 } 1108 1109 return bdrv_flush(blk_bs(blk)); 1110 } 1111 1112 void blk_drain(BlockBackend *blk) 1113 { 1114 if (blk_bs(blk)) { 1115 bdrv_drain(blk_bs(blk)); 1116 } 1117 } 1118 1119 void blk_drain_all(void) 1120 { 1121 bdrv_drain_all(); 1122 } 1123 1124 void blk_set_on_error(BlockBackend *blk, BlockdevOnError on_read_error, 1125 BlockdevOnError on_write_error) 1126 { 1127 blk->on_read_error = on_read_error; 1128 blk->on_write_error = on_write_error; 1129 } 1130 1131 BlockdevOnError blk_get_on_error(BlockBackend *blk, bool is_read) 1132 { 1133 return is_read ? blk->on_read_error : blk->on_write_error; 1134 } 1135 1136 BlockErrorAction blk_get_error_action(BlockBackend *blk, bool is_read, 1137 int error) 1138 { 1139 BlockdevOnError on_err = blk_get_on_error(blk, is_read); 1140 1141 switch (on_err) { 1142 case BLOCKDEV_ON_ERROR_ENOSPC: 1143 return (error == ENOSPC) ? 1144 BLOCK_ERROR_ACTION_STOP : BLOCK_ERROR_ACTION_REPORT; 1145 case BLOCKDEV_ON_ERROR_STOP: 1146 return BLOCK_ERROR_ACTION_STOP; 1147 case BLOCKDEV_ON_ERROR_REPORT: 1148 return BLOCK_ERROR_ACTION_REPORT; 1149 case BLOCKDEV_ON_ERROR_IGNORE: 1150 return BLOCK_ERROR_ACTION_IGNORE; 1151 default: 1152 abort(); 1153 } 1154 } 1155 1156 static void send_qmp_error_event(BlockBackend *blk, 1157 BlockErrorAction action, 1158 bool is_read, int error) 1159 { 1160 IoOperationType optype; 1161 1162 optype = is_read ? IO_OPERATION_TYPE_READ : IO_OPERATION_TYPE_WRITE; 1163 qapi_event_send_block_io_error(blk_name(blk), optype, action, 1164 blk_iostatus_is_enabled(blk), 1165 error == ENOSPC, strerror(error), 1166 &error_abort); 1167 } 1168 1169 /* This is done by device models because, while the block layer knows 1170 * about the error, it does not know whether an operation comes from 1171 * the device or the block layer (from a job, for example). 1172 */ 1173 void blk_error_action(BlockBackend *blk, BlockErrorAction action, 1174 bool is_read, int error) 1175 { 1176 assert(error >= 0); 1177 1178 if (action == BLOCK_ERROR_ACTION_STOP) { 1179 /* First set the iostatus, so that "info block" returns an iostatus 1180 * that matches the events raised so far (an additional error iostatus 1181 * is fine, but not a lost one). 1182 */ 1183 blk_iostatus_set_err(blk, error); 1184 1185 /* Then raise the request to stop the VM and the event. 1186 * qemu_system_vmstop_request_prepare has two effects. First, 1187 * it ensures that the STOP event always comes after the 1188 * BLOCK_IO_ERROR event. Second, it ensures that even if management 1189 * can observe the STOP event and do a "cont" before the STOP 1190 * event is issued, the VM will not stop. In this case, vm_start() 1191 * also ensures that the STOP/RESUME pair of events is emitted. 1192 */ 1193 qemu_system_vmstop_request_prepare(); 1194 send_qmp_error_event(blk, action, is_read, error); 1195 qemu_system_vmstop_request(RUN_STATE_IO_ERROR); 1196 } else { 1197 send_qmp_error_event(blk, action, is_read, error); 1198 } 1199 } 1200 1201 int blk_is_read_only(BlockBackend *blk) 1202 { 1203 BlockDriverState *bs = blk_bs(blk); 1204 1205 if (bs) { 1206 return bdrv_is_read_only(bs); 1207 } else { 1208 return blk->root_state.read_only; 1209 } 1210 } 1211 1212 int blk_is_sg(BlockBackend *blk) 1213 { 1214 BlockDriverState *bs = blk_bs(blk); 1215 1216 if (!bs) { 1217 return 0; 1218 } 1219 1220 return bdrv_is_sg(bs); 1221 } 1222 1223 int blk_enable_write_cache(BlockBackend *blk) 1224 { 1225 BlockDriverState *bs = blk_bs(blk); 1226 1227 if (bs) { 1228 return bdrv_enable_write_cache(bs); 1229 } else { 1230 return !!(blk->root_state.open_flags & BDRV_O_CACHE_WB); 1231 } 1232 } 1233 1234 void blk_set_enable_write_cache(BlockBackend *blk, bool wce) 1235 { 1236 BlockDriverState *bs = blk_bs(blk); 1237 1238 if (bs) { 1239 bdrv_set_enable_write_cache(bs, wce); 1240 } else { 1241 if (wce) { 1242 blk->root_state.open_flags |= BDRV_O_CACHE_WB; 1243 } else { 1244 blk->root_state.open_flags &= ~BDRV_O_CACHE_WB; 1245 } 1246 } 1247 } 1248 1249 void blk_invalidate_cache(BlockBackend *blk, Error **errp) 1250 { 1251 BlockDriverState *bs = blk_bs(blk); 1252 1253 if (!bs) { 1254 error_setg(errp, "Device '%s' has no medium", blk->name); 1255 return; 1256 } 1257 1258 bdrv_invalidate_cache(bs, errp); 1259 } 1260 1261 bool blk_is_inserted(BlockBackend *blk) 1262 { 1263 BlockDriverState *bs = blk_bs(blk); 1264 1265 return bs && bdrv_is_inserted(bs); 1266 } 1267 1268 bool blk_is_available(BlockBackend *blk) 1269 { 1270 return blk_is_inserted(blk) && !blk_dev_is_tray_open(blk); 1271 } 1272 1273 void blk_lock_medium(BlockBackend *blk, bool locked) 1274 { 1275 BlockDriverState *bs = blk_bs(blk); 1276 1277 if (bs) { 1278 bdrv_lock_medium(bs, locked); 1279 } 1280 } 1281 1282 void blk_eject(BlockBackend *blk, bool eject_flag) 1283 { 1284 BlockDriverState *bs = blk_bs(blk); 1285 1286 if (bs) { 1287 bdrv_eject(bs, eject_flag); 1288 } 1289 } 1290 1291 int blk_get_flags(BlockBackend *blk) 1292 { 1293 BlockDriverState *bs = blk_bs(blk); 1294 1295 if (bs) { 1296 return bdrv_get_flags(bs); 1297 } else { 1298 return blk->root_state.open_flags; 1299 } 1300 } 1301 1302 int blk_get_max_transfer_length(BlockBackend *blk) 1303 { 1304 BlockDriverState *bs = blk_bs(blk); 1305 1306 if (bs) { 1307 return bs->bl.max_transfer_length; 1308 } else { 1309 return 0; 1310 } 1311 } 1312 1313 int blk_get_max_iov(BlockBackend *blk) 1314 { 1315 return blk->root->bs->bl.max_iov; 1316 } 1317 1318 void blk_set_guest_block_size(BlockBackend *blk, int align) 1319 { 1320 blk->guest_block_size = align; 1321 } 1322 1323 void *blk_try_blockalign(BlockBackend *blk, size_t size) 1324 { 1325 return qemu_try_blockalign(blk ? blk_bs(blk) : NULL, size); 1326 } 1327 1328 void *blk_blockalign(BlockBackend *blk, size_t size) 1329 { 1330 return qemu_blockalign(blk ? blk_bs(blk) : NULL, size); 1331 } 1332 1333 bool blk_op_is_blocked(BlockBackend *blk, BlockOpType op, Error **errp) 1334 { 1335 BlockDriverState *bs = blk_bs(blk); 1336 1337 if (!bs) { 1338 return false; 1339 } 1340 1341 return bdrv_op_is_blocked(bs, op, errp); 1342 } 1343 1344 void blk_op_unblock(BlockBackend *blk, BlockOpType op, Error *reason) 1345 { 1346 BlockDriverState *bs = blk_bs(blk); 1347 1348 if (bs) { 1349 bdrv_op_unblock(bs, op, reason); 1350 } 1351 } 1352 1353 void blk_op_block_all(BlockBackend *blk, Error *reason) 1354 { 1355 BlockDriverState *bs = blk_bs(blk); 1356 1357 if (bs) { 1358 bdrv_op_block_all(bs, reason); 1359 } 1360 } 1361 1362 void blk_op_unblock_all(BlockBackend *blk, Error *reason) 1363 { 1364 BlockDriverState *bs = blk_bs(blk); 1365 1366 if (bs) { 1367 bdrv_op_unblock_all(bs, reason); 1368 } 1369 } 1370 1371 AioContext *blk_get_aio_context(BlockBackend *blk) 1372 { 1373 BlockDriverState *bs = blk_bs(blk); 1374 1375 if (bs) { 1376 return bdrv_get_aio_context(bs); 1377 } else { 1378 return qemu_get_aio_context(); 1379 } 1380 } 1381 1382 static AioContext *blk_aiocb_get_aio_context(BlockAIOCB *acb) 1383 { 1384 BlockBackendAIOCB *blk_acb = DO_UPCAST(BlockBackendAIOCB, common, acb); 1385 return blk_get_aio_context(blk_acb->blk); 1386 } 1387 1388 void blk_set_aio_context(BlockBackend *blk, AioContext *new_context) 1389 { 1390 BlockDriverState *bs = blk_bs(blk); 1391 1392 if (bs) { 1393 bdrv_set_aio_context(bs, new_context); 1394 } 1395 } 1396 1397 void blk_add_aio_context_notifier(BlockBackend *blk, 1398 void (*attached_aio_context)(AioContext *new_context, void *opaque), 1399 void (*detach_aio_context)(void *opaque), void *opaque) 1400 { 1401 BlockDriverState *bs = blk_bs(blk); 1402 1403 if (bs) { 1404 bdrv_add_aio_context_notifier(bs, attached_aio_context, 1405 detach_aio_context, opaque); 1406 } 1407 } 1408 1409 void blk_remove_aio_context_notifier(BlockBackend *blk, 1410 void (*attached_aio_context)(AioContext *, 1411 void *), 1412 void (*detach_aio_context)(void *), 1413 void *opaque) 1414 { 1415 BlockDriverState *bs = blk_bs(blk); 1416 1417 if (bs) { 1418 bdrv_remove_aio_context_notifier(bs, attached_aio_context, 1419 detach_aio_context, opaque); 1420 } 1421 } 1422 1423 void blk_add_remove_bs_notifier(BlockBackend *blk, Notifier *notify) 1424 { 1425 notifier_list_add(&blk->remove_bs_notifiers, notify); 1426 } 1427 1428 void blk_add_insert_bs_notifier(BlockBackend *blk, Notifier *notify) 1429 { 1430 notifier_list_add(&blk->insert_bs_notifiers, notify); 1431 } 1432 1433 void blk_io_plug(BlockBackend *blk) 1434 { 1435 BlockDriverState *bs = blk_bs(blk); 1436 1437 if (bs) { 1438 bdrv_io_plug(bs); 1439 } 1440 } 1441 1442 void blk_io_unplug(BlockBackend *blk) 1443 { 1444 BlockDriverState *bs = blk_bs(blk); 1445 1446 if (bs) { 1447 bdrv_io_unplug(bs); 1448 } 1449 } 1450 1451 BlockAcctStats *blk_get_stats(BlockBackend *blk) 1452 { 1453 return &blk->stats; 1454 } 1455 1456 void *blk_aio_get(const AIOCBInfo *aiocb_info, BlockBackend *blk, 1457 BlockCompletionFunc *cb, void *opaque) 1458 { 1459 return qemu_aio_get(aiocb_info, blk_bs(blk), cb, opaque); 1460 } 1461 1462 int coroutine_fn blk_co_write_zeroes(BlockBackend *blk, int64_t sector_num, 1463 int nb_sectors, BdrvRequestFlags flags) 1464 { 1465 if (nb_sectors < 0 || nb_sectors > BDRV_REQUEST_MAX_SECTORS) { 1466 return -EINVAL; 1467 } 1468 1469 return blk_co_pwritev(blk, sector_num << BDRV_SECTOR_BITS, 1470 nb_sectors << BDRV_SECTOR_BITS, NULL, 1471 BDRV_REQ_ZERO_WRITE); 1472 } 1473 1474 int blk_write_compressed(BlockBackend *blk, int64_t sector_num, 1475 const uint8_t *buf, int nb_sectors) 1476 { 1477 int ret = blk_check_request(blk, sector_num, nb_sectors); 1478 if (ret < 0) { 1479 return ret; 1480 } 1481 1482 return bdrv_write_compressed(blk_bs(blk), sector_num, buf, nb_sectors); 1483 } 1484 1485 int blk_truncate(BlockBackend *blk, int64_t offset) 1486 { 1487 if (!blk_is_available(blk)) { 1488 return -ENOMEDIUM; 1489 } 1490 1491 return bdrv_truncate(blk_bs(blk), offset); 1492 } 1493 1494 int blk_discard(BlockBackend *blk, int64_t sector_num, int nb_sectors) 1495 { 1496 int ret = blk_check_request(blk, sector_num, nb_sectors); 1497 if (ret < 0) { 1498 return ret; 1499 } 1500 1501 return bdrv_discard(blk_bs(blk), sector_num, nb_sectors); 1502 } 1503 1504 int blk_save_vmstate(BlockBackend *blk, const uint8_t *buf, 1505 int64_t pos, int size) 1506 { 1507 if (!blk_is_available(blk)) { 1508 return -ENOMEDIUM; 1509 } 1510 1511 return bdrv_save_vmstate(blk_bs(blk), buf, pos, size); 1512 } 1513 1514 int blk_load_vmstate(BlockBackend *blk, uint8_t *buf, int64_t pos, int size) 1515 { 1516 if (!blk_is_available(blk)) { 1517 return -ENOMEDIUM; 1518 } 1519 1520 return bdrv_load_vmstate(blk_bs(blk), buf, pos, size); 1521 } 1522 1523 int blk_probe_blocksizes(BlockBackend *blk, BlockSizes *bsz) 1524 { 1525 if (!blk_is_available(blk)) { 1526 return -ENOMEDIUM; 1527 } 1528 1529 return bdrv_probe_blocksizes(blk_bs(blk), bsz); 1530 } 1531 1532 int blk_probe_geometry(BlockBackend *blk, HDGeometry *geo) 1533 { 1534 if (!blk_is_available(blk)) { 1535 return -ENOMEDIUM; 1536 } 1537 1538 return bdrv_probe_geometry(blk_bs(blk), geo); 1539 } 1540 1541 /* 1542 * Updates the BlockBackendRootState object with data from the currently 1543 * attached BlockDriverState. 1544 */ 1545 void blk_update_root_state(BlockBackend *blk) 1546 { 1547 assert(blk->root); 1548 1549 blk->root_state.open_flags = blk->root->bs->open_flags; 1550 blk->root_state.read_only = blk->root->bs->read_only; 1551 blk->root_state.detect_zeroes = blk->root->bs->detect_zeroes; 1552 1553 if (blk->root_state.throttle_group) { 1554 g_free(blk->root_state.throttle_group); 1555 throttle_group_unref(blk->root_state.throttle_state); 1556 } 1557 if (blk->root->bs->throttle_state) { 1558 const char *name = throttle_group_get_name(blk->root->bs); 1559 blk->root_state.throttle_group = g_strdup(name); 1560 blk->root_state.throttle_state = throttle_group_incref(name); 1561 } else { 1562 blk->root_state.throttle_group = NULL; 1563 blk->root_state.throttle_state = NULL; 1564 } 1565 } 1566 1567 /* 1568 * Applies the information in the root state to the given BlockDriverState. This 1569 * does not include the flags which have to be specified for bdrv_open(), use 1570 * blk_get_open_flags_from_root_state() to inquire them. 1571 */ 1572 void blk_apply_root_state(BlockBackend *blk, BlockDriverState *bs) 1573 { 1574 bs->detect_zeroes = blk->root_state.detect_zeroes; 1575 if (blk->root_state.throttle_group) { 1576 bdrv_io_limits_enable(bs, blk->root_state.throttle_group); 1577 } 1578 } 1579 1580 /* 1581 * Returns the flags to be used for bdrv_open() of a BlockDriverState which is 1582 * supposed to inherit the root state. 1583 */ 1584 int blk_get_open_flags_from_root_state(BlockBackend *blk) 1585 { 1586 int bs_flags; 1587 1588 bs_flags = blk->root_state.read_only ? 0 : BDRV_O_RDWR; 1589 bs_flags |= blk->root_state.open_flags & ~BDRV_O_RDWR; 1590 1591 return bs_flags; 1592 } 1593 1594 BlockBackendRootState *blk_get_root_state(BlockBackend *blk) 1595 { 1596 return &blk->root_state; 1597 } 1598 1599 int blk_commit_all(void) 1600 { 1601 BlockBackend *blk = NULL; 1602 1603 while ((blk = blk_all_next(blk)) != NULL) { 1604 AioContext *aio_context = blk_get_aio_context(blk); 1605 1606 aio_context_acquire(aio_context); 1607 if (blk_is_inserted(blk) && blk->root->bs->backing) { 1608 int ret = bdrv_commit(blk->root->bs); 1609 if (ret < 0) { 1610 aio_context_release(aio_context); 1611 return ret; 1612 } 1613 } 1614 aio_context_release(aio_context); 1615 } 1616 return 0; 1617 } 1618 1619 int blk_flush_all(void) 1620 { 1621 BlockBackend *blk = NULL; 1622 int result = 0; 1623 1624 while ((blk = blk_all_next(blk)) != NULL) { 1625 AioContext *aio_context = blk_get_aio_context(blk); 1626 int ret; 1627 1628 aio_context_acquire(aio_context); 1629 if (blk_is_inserted(blk)) { 1630 ret = blk_flush(blk); 1631 if (ret < 0 && !result) { 1632 result = ret; 1633 } 1634 } 1635 aio_context_release(aio_context); 1636 } 1637 1638 return result; 1639 } 1640