1 /* 2 * QEMU System Emulator block driver 3 * 4 * Copyright (c) 2011 IBM Corp. 5 * Copyright (c) 2012 Red Hat, Inc. 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining a copy 8 * of this software and associated documentation files (the "Software"), to deal 9 * in the Software without restriction, including without limitation the rights 10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 * copies of the Software, and to permit persons to whom the Software is 12 * furnished to do so, subject to the following conditions: 13 * 14 * The above copyright notice and this permission notice shall be included in 15 * all copies or substantial portions of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 * THE SOFTWARE. 24 */ 25 26 #include "qemu/osdep.h" 27 #include "qemu-common.h" 28 #include "block/block.h" 29 #include "block/blockjob_int.h" 30 #include "block/block_int.h" 31 #include "sysemu/block-backend.h" 32 #include "qapi/error.h" 33 #include "qapi/qapi-events-block-core.h" 34 #include "qapi/qmp/qerror.h" 35 #include "qemu/coroutine.h" 36 #include "qemu/id.h" 37 #include "qemu/timer.h" 38 39 /* Right now, this mutex is only needed to synchronize accesses to job->busy 40 * and job->sleep_timer, such as concurrent calls to block_job_do_yield and 41 * block_job_enter. */ 42 static QemuMutex block_job_mutex; 43 44 static void block_job_lock(void) 45 { 46 qemu_mutex_lock(&block_job_mutex); 47 } 48 49 static void block_job_unlock(void) 50 { 51 qemu_mutex_unlock(&block_job_mutex); 52 } 53 54 static void __attribute__((__constructor__)) block_job_init(void) 55 { 56 qemu_mutex_init(&block_job_mutex); 57 } 58 59 static void block_job_event_cancelled(BlockJob *job); 60 static void block_job_event_completed(BlockJob *job, const char *msg); 61 static void block_job_enter_cond(BlockJob *job, bool(*fn)(BlockJob *job)); 62 63 /* Transactional group of block jobs */ 64 struct BlockJobTxn { 65 66 /* Is this txn being cancelled? */ 67 bool aborting; 68 69 /* List of jobs */ 70 QLIST_HEAD(, BlockJob) jobs; 71 72 /* Reference count */ 73 int refcnt; 74 }; 75 76 static QLIST_HEAD(, BlockJob) block_jobs = QLIST_HEAD_INITIALIZER(block_jobs); 77 78 /* 79 * The block job API is composed of two categories of functions. 80 * 81 * The first includes functions used by the monitor. The monitor is 82 * peculiar in that it accesses the block job list with block_job_get, and 83 * therefore needs consistency across block_job_get and the actual operation 84 * (e.g. block_job_set_speed). The consistency is achieved with 85 * aio_context_acquire/release. These functions are declared in blockjob.h. 86 * 87 * The second includes functions used by the block job drivers and sometimes 88 * by the core block layer. These do not care about locking, because the 89 * whole coroutine runs under the AioContext lock, and are declared in 90 * blockjob_int.h. 91 */ 92 93 BlockJob *block_job_next(BlockJob *job) 94 { 95 if (!job) { 96 return QLIST_FIRST(&block_jobs); 97 } 98 return QLIST_NEXT(job, job_list); 99 } 100 101 BlockJob *block_job_get(const char *id) 102 { 103 BlockJob *job; 104 105 QLIST_FOREACH(job, &block_jobs, job_list) { 106 if (job->id && !strcmp(id, job->id)) { 107 return job; 108 } 109 } 110 111 return NULL; 112 } 113 114 BlockJobTxn *block_job_txn_new(void) 115 { 116 BlockJobTxn *txn = g_new0(BlockJobTxn, 1); 117 QLIST_INIT(&txn->jobs); 118 txn->refcnt = 1; 119 return txn; 120 } 121 122 static void block_job_txn_ref(BlockJobTxn *txn) 123 { 124 txn->refcnt++; 125 } 126 127 void block_job_txn_unref(BlockJobTxn *txn) 128 { 129 if (txn && --txn->refcnt == 0) { 130 g_free(txn); 131 } 132 } 133 134 void block_job_txn_add_job(BlockJobTxn *txn, BlockJob *job) 135 { 136 if (!txn) { 137 return; 138 } 139 140 assert(!job->txn); 141 job->txn = txn; 142 143 QLIST_INSERT_HEAD(&txn->jobs, job, txn_list); 144 block_job_txn_ref(txn); 145 } 146 147 static void block_job_pause(BlockJob *job) 148 { 149 job->pause_count++; 150 } 151 152 static void block_job_resume(BlockJob *job) 153 { 154 assert(job->pause_count > 0); 155 job->pause_count--; 156 if (job->pause_count) { 157 return; 158 } 159 block_job_enter(job); 160 } 161 162 void block_job_ref(BlockJob *job) 163 { 164 ++job->refcnt; 165 } 166 167 static void block_job_attached_aio_context(AioContext *new_context, 168 void *opaque); 169 static void block_job_detach_aio_context(void *opaque); 170 171 void block_job_unref(BlockJob *job) 172 { 173 if (--job->refcnt == 0) { 174 BlockDriverState *bs = blk_bs(job->blk); 175 QLIST_REMOVE(job, job_list); 176 bs->job = NULL; 177 block_job_remove_all_bdrv(job); 178 blk_remove_aio_context_notifier(job->blk, 179 block_job_attached_aio_context, 180 block_job_detach_aio_context, job); 181 blk_unref(job->blk); 182 error_free(job->blocker); 183 g_free(job->id); 184 assert(!timer_pending(&job->sleep_timer)); 185 g_free(job); 186 } 187 } 188 189 static void block_job_attached_aio_context(AioContext *new_context, 190 void *opaque) 191 { 192 BlockJob *job = opaque; 193 194 if (job->driver->attached_aio_context) { 195 job->driver->attached_aio_context(job, new_context); 196 } 197 198 block_job_resume(job); 199 } 200 201 static void block_job_drain(BlockJob *job) 202 { 203 /* If job is !job->busy this kicks it into the next pause point. */ 204 block_job_enter(job); 205 206 blk_drain(job->blk); 207 if (job->driver->drain) { 208 job->driver->drain(job); 209 } 210 } 211 212 static void block_job_detach_aio_context(void *opaque) 213 { 214 BlockJob *job = opaque; 215 216 /* In case the job terminates during aio_poll()... */ 217 block_job_ref(job); 218 219 block_job_pause(job); 220 221 while (!job->paused && !job->completed) { 222 block_job_drain(job); 223 } 224 225 block_job_unref(job); 226 } 227 228 static char *child_job_get_parent_desc(BdrvChild *c) 229 { 230 BlockJob *job = c->opaque; 231 return g_strdup_printf("%s job '%s'", 232 BlockJobType_str(job->driver->job_type), 233 job->id); 234 } 235 236 static void child_job_drained_begin(BdrvChild *c) 237 { 238 BlockJob *job = c->opaque; 239 block_job_pause(job); 240 } 241 242 static void child_job_drained_end(BdrvChild *c) 243 { 244 BlockJob *job = c->opaque; 245 block_job_resume(job); 246 } 247 248 static const BdrvChildRole child_job = { 249 .get_parent_desc = child_job_get_parent_desc, 250 .drained_begin = child_job_drained_begin, 251 .drained_end = child_job_drained_end, 252 .stay_at_node = true, 253 }; 254 255 void block_job_remove_all_bdrv(BlockJob *job) 256 { 257 GSList *l; 258 for (l = job->nodes; l; l = l->next) { 259 BdrvChild *c = l->data; 260 bdrv_op_unblock_all(c->bs, job->blocker); 261 bdrv_root_unref_child(c); 262 } 263 g_slist_free(job->nodes); 264 job->nodes = NULL; 265 } 266 267 int block_job_add_bdrv(BlockJob *job, const char *name, BlockDriverState *bs, 268 uint64_t perm, uint64_t shared_perm, Error **errp) 269 { 270 BdrvChild *c; 271 272 c = bdrv_root_attach_child(bs, name, &child_job, perm, shared_perm, 273 job, errp); 274 if (c == NULL) { 275 return -EPERM; 276 } 277 278 job->nodes = g_slist_prepend(job->nodes, c); 279 bdrv_ref(bs); 280 bdrv_op_block_all(bs, job->blocker); 281 282 return 0; 283 } 284 285 bool block_job_is_internal(BlockJob *job) 286 { 287 return (job->id == NULL); 288 } 289 290 static bool block_job_started(BlockJob *job) 291 { 292 return job->co; 293 } 294 295 /** 296 * All jobs must allow a pause point before entering their job proper. This 297 * ensures that jobs can be paused prior to being started, then resumed later. 298 */ 299 static void coroutine_fn block_job_co_entry(void *opaque) 300 { 301 BlockJob *job = opaque; 302 303 assert(job && job->driver && job->driver->start); 304 block_job_pause_point(job); 305 job->driver->start(job); 306 } 307 308 static void block_job_sleep_timer_cb(void *opaque) 309 { 310 BlockJob *job = opaque; 311 312 block_job_enter(job); 313 } 314 315 void block_job_start(BlockJob *job) 316 { 317 assert(job && !block_job_started(job) && job->paused && 318 job->driver && job->driver->start); 319 job->co = qemu_coroutine_create(block_job_co_entry, job); 320 job->pause_count--; 321 job->busy = true; 322 job->paused = false; 323 bdrv_coroutine_enter(blk_bs(job->blk), job->co); 324 } 325 326 static void block_job_completed_single(BlockJob *job) 327 { 328 assert(job->completed); 329 330 if (!job->ret) { 331 if (job->driver->commit) { 332 job->driver->commit(job); 333 } 334 } else { 335 if (job->driver->abort) { 336 job->driver->abort(job); 337 } 338 } 339 if (job->driver->clean) { 340 job->driver->clean(job); 341 } 342 343 if (job->cb) { 344 job->cb(job->opaque, job->ret); 345 } 346 347 /* Emit events only if we actually started */ 348 if (block_job_started(job)) { 349 if (block_job_is_cancelled(job)) { 350 block_job_event_cancelled(job); 351 } else { 352 const char *msg = NULL; 353 if (job->ret < 0) { 354 msg = strerror(-job->ret); 355 } 356 block_job_event_completed(job, msg); 357 } 358 } 359 360 if (job->txn) { 361 QLIST_REMOVE(job, txn_list); 362 block_job_txn_unref(job->txn); 363 } 364 block_job_unref(job); 365 } 366 367 static void block_job_cancel_async(BlockJob *job) 368 { 369 if (job->iostatus != BLOCK_DEVICE_IO_STATUS_OK) { 370 block_job_iostatus_reset(job); 371 } 372 if (job->user_paused) { 373 /* Do not call block_job_enter here, the caller will handle it. */ 374 job->user_paused = false; 375 job->pause_count--; 376 } 377 job->cancelled = true; 378 } 379 380 static int block_job_finish_sync(BlockJob *job, 381 void (*finish)(BlockJob *, Error **errp), 382 Error **errp) 383 { 384 Error *local_err = NULL; 385 int ret; 386 387 assert(blk_bs(job->blk)->job == job); 388 389 block_job_ref(job); 390 391 if (finish) { 392 finish(job, &local_err); 393 } 394 if (local_err) { 395 error_propagate(errp, local_err); 396 block_job_unref(job); 397 return -EBUSY; 398 } 399 /* block_job_drain calls block_job_enter, and it should be enough to 400 * induce progress until the job completes or moves to the main thread. 401 */ 402 while (!job->deferred_to_main_loop && !job->completed) { 403 block_job_drain(job); 404 } 405 while (!job->completed) { 406 aio_poll(qemu_get_aio_context(), true); 407 } 408 ret = (job->cancelled && job->ret == 0) ? -ECANCELED : job->ret; 409 block_job_unref(job); 410 return ret; 411 } 412 413 static void block_job_completed_txn_abort(BlockJob *job) 414 { 415 AioContext *ctx; 416 BlockJobTxn *txn = job->txn; 417 BlockJob *other_job; 418 419 if (txn->aborting) { 420 /* 421 * We are cancelled by another job, which will handle everything. 422 */ 423 return; 424 } 425 txn->aborting = true; 426 block_job_txn_ref(txn); 427 428 /* We are the first failed job. Cancel other jobs. */ 429 QLIST_FOREACH(other_job, &txn->jobs, txn_list) { 430 ctx = blk_get_aio_context(other_job->blk); 431 aio_context_acquire(ctx); 432 } 433 434 /* Other jobs are effectively cancelled by us, set the status for 435 * them; this job, however, may or may not be cancelled, depending 436 * on the caller, so leave it. */ 437 QLIST_FOREACH(other_job, &txn->jobs, txn_list) { 438 if (other_job != job) { 439 block_job_cancel_async(other_job); 440 } 441 } 442 while (!QLIST_EMPTY(&txn->jobs)) { 443 other_job = QLIST_FIRST(&txn->jobs); 444 ctx = blk_get_aio_context(other_job->blk); 445 if (!other_job->completed) { 446 assert(other_job->cancelled); 447 block_job_finish_sync(other_job, NULL, NULL); 448 } 449 block_job_completed_single(other_job); 450 aio_context_release(ctx); 451 } 452 453 block_job_txn_unref(txn); 454 } 455 456 static void block_job_completed_txn_success(BlockJob *job) 457 { 458 AioContext *ctx; 459 BlockJobTxn *txn = job->txn; 460 BlockJob *other_job, *next; 461 /* 462 * Successful completion, see if there are other running jobs in this 463 * txn. 464 */ 465 QLIST_FOREACH(other_job, &txn->jobs, txn_list) { 466 if (!other_job->completed) { 467 return; 468 } 469 } 470 /* We are the last completed job, commit the transaction. */ 471 QLIST_FOREACH_SAFE(other_job, &txn->jobs, txn_list, next) { 472 ctx = blk_get_aio_context(other_job->blk); 473 aio_context_acquire(ctx); 474 assert(other_job->ret == 0); 475 block_job_completed_single(other_job); 476 aio_context_release(ctx); 477 } 478 } 479 480 /* Assumes the block_job_mutex is held */ 481 static bool block_job_timer_pending(BlockJob *job) 482 { 483 return timer_pending(&job->sleep_timer); 484 } 485 486 void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp) 487 { 488 Error *local_err = NULL; 489 int64_t old_speed = job->speed; 490 491 if (!job->driver->set_speed) { 492 error_setg(errp, QERR_UNSUPPORTED); 493 return; 494 } 495 job->driver->set_speed(job, speed, &local_err); 496 if (local_err) { 497 error_propagate(errp, local_err); 498 return; 499 } 500 501 job->speed = speed; 502 if (speed <= old_speed) { 503 return; 504 } 505 506 /* kick only if a timer is pending */ 507 block_job_enter_cond(job, block_job_timer_pending); 508 } 509 510 void block_job_complete(BlockJob *job, Error **errp) 511 { 512 /* Should not be reachable via external interface for internal jobs */ 513 assert(job->id); 514 if (job->pause_count || job->cancelled || 515 !block_job_started(job) || !job->driver->complete) { 516 error_setg(errp, "The active block job '%s' cannot be completed", 517 job->id); 518 return; 519 } 520 521 job->driver->complete(job, errp); 522 } 523 524 void block_job_user_pause(BlockJob *job) 525 { 526 job->user_paused = true; 527 block_job_pause(job); 528 } 529 530 bool block_job_user_paused(BlockJob *job) 531 { 532 return job->user_paused; 533 } 534 535 void block_job_user_resume(BlockJob *job) 536 { 537 if (job && job->user_paused && job->pause_count > 0) { 538 block_job_iostatus_reset(job); 539 job->user_paused = false; 540 block_job_resume(job); 541 } 542 } 543 544 void block_job_cancel(BlockJob *job) 545 { 546 if (block_job_started(job)) { 547 block_job_cancel_async(job); 548 block_job_enter(job); 549 } else { 550 block_job_completed(job, -ECANCELED); 551 } 552 } 553 554 /* A wrapper around block_job_cancel() taking an Error ** parameter so it may be 555 * used with block_job_finish_sync() without the need for (rather nasty) 556 * function pointer casts there. */ 557 static void block_job_cancel_err(BlockJob *job, Error **errp) 558 { 559 block_job_cancel(job); 560 } 561 562 int block_job_cancel_sync(BlockJob *job) 563 { 564 return block_job_finish_sync(job, &block_job_cancel_err, NULL); 565 } 566 567 void block_job_cancel_sync_all(void) 568 { 569 BlockJob *job; 570 AioContext *aio_context; 571 572 while ((job = QLIST_FIRST(&block_jobs))) { 573 aio_context = blk_get_aio_context(job->blk); 574 aio_context_acquire(aio_context); 575 block_job_cancel_sync(job); 576 aio_context_release(aio_context); 577 } 578 } 579 580 int block_job_complete_sync(BlockJob *job, Error **errp) 581 { 582 return block_job_finish_sync(job, &block_job_complete, errp); 583 } 584 585 BlockJobInfo *block_job_query(BlockJob *job, Error **errp) 586 { 587 BlockJobInfo *info; 588 589 if (block_job_is_internal(job)) { 590 error_setg(errp, "Cannot query QEMU internal jobs"); 591 return NULL; 592 } 593 info = g_new0(BlockJobInfo, 1); 594 info->type = g_strdup(BlockJobType_str(job->driver->job_type)); 595 info->device = g_strdup(job->id); 596 info->len = job->len; 597 info->busy = atomic_read(&job->busy); 598 info->paused = job->pause_count > 0; 599 info->offset = job->offset; 600 info->speed = job->speed; 601 info->io_status = job->iostatus; 602 info->ready = job->ready; 603 return info; 604 } 605 606 static void block_job_iostatus_set_err(BlockJob *job, int error) 607 { 608 if (job->iostatus == BLOCK_DEVICE_IO_STATUS_OK) { 609 job->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE : 610 BLOCK_DEVICE_IO_STATUS_FAILED; 611 } 612 } 613 614 static void block_job_event_cancelled(BlockJob *job) 615 { 616 if (block_job_is_internal(job)) { 617 return; 618 } 619 620 qapi_event_send_block_job_cancelled(job->driver->job_type, 621 job->id, 622 job->len, 623 job->offset, 624 job->speed, 625 &error_abort); 626 } 627 628 static void block_job_event_completed(BlockJob *job, const char *msg) 629 { 630 if (block_job_is_internal(job)) { 631 return; 632 } 633 634 qapi_event_send_block_job_completed(job->driver->job_type, 635 job->id, 636 job->len, 637 job->offset, 638 job->speed, 639 !!msg, 640 msg, 641 &error_abort); 642 } 643 644 /* 645 * API for block job drivers and the block layer. These functions are 646 * declared in blockjob_int.h. 647 */ 648 649 void *block_job_create(const char *job_id, const BlockJobDriver *driver, 650 BlockDriverState *bs, uint64_t perm, 651 uint64_t shared_perm, int64_t speed, int flags, 652 BlockCompletionFunc *cb, void *opaque, Error **errp) 653 { 654 BlockBackend *blk; 655 BlockJob *job; 656 int ret; 657 658 if (bs->job) { 659 error_setg(errp, QERR_DEVICE_IN_USE, bdrv_get_device_name(bs)); 660 return NULL; 661 } 662 663 if (job_id == NULL && !(flags & BLOCK_JOB_INTERNAL)) { 664 job_id = bdrv_get_device_name(bs); 665 if (!*job_id) { 666 error_setg(errp, "An explicit job ID is required for this node"); 667 return NULL; 668 } 669 } 670 671 if (job_id) { 672 if (flags & BLOCK_JOB_INTERNAL) { 673 error_setg(errp, "Cannot specify job ID for internal block job"); 674 return NULL; 675 } 676 677 if (!id_wellformed(job_id)) { 678 error_setg(errp, "Invalid job ID '%s'", job_id); 679 return NULL; 680 } 681 682 if (block_job_get(job_id)) { 683 error_setg(errp, "Job ID '%s' already in use", job_id); 684 return NULL; 685 } 686 } 687 688 blk = blk_new(perm, shared_perm); 689 ret = blk_insert_bs(blk, bs, errp); 690 if (ret < 0) { 691 blk_unref(blk); 692 return NULL; 693 } 694 695 job = g_malloc0(driver->instance_size); 696 job->driver = driver; 697 job->id = g_strdup(job_id); 698 job->blk = blk; 699 job->cb = cb; 700 job->opaque = opaque; 701 job->busy = false; 702 job->paused = true; 703 job->pause_count = 1; 704 job->refcnt = 1; 705 aio_timer_init(qemu_get_aio_context(), &job->sleep_timer, 706 QEMU_CLOCK_REALTIME, SCALE_NS, 707 block_job_sleep_timer_cb, job); 708 709 error_setg(&job->blocker, "block device is in use by block job: %s", 710 BlockJobType_str(driver->job_type)); 711 block_job_add_bdrv(job, "main node", bs, 0, BLK_PERM_ALL, &error_abort); 712 bs->job = job; 713 714 bdrv_op_unblock(bs, BLOCK_OP_TYPE_DATAPLANE, job->blocker); 715 716 QLIST_INSERT_HEAD(&block_jobs, job, job_list); 717 718 blk_add_aio_context_notifier(blk, block_job_attached_aio_context, 719 block_job_detach_aio_context, job); 720 721 /* Only set speed when necessary to avoid NotSupported error */ 722 if (speed != 0) { 723 Error *local_err = NULL; 724 725 block_job_set_speed(job, speed, &local_err); 726 if (local_err) { 727 block_job_unref(job); 728 error_propagate(errp, local_err); 729 return NULL; 730 } 731 } 732 return job; 733 } 734 735 void block_job_pause_all(void) 736 { 737 BlockJob *job = NULL; 738 while ((job = block_job_next(job))) { 739 AioContext *aio_context = blk_get_aio_context(job->blk); 740 741 aio_context_acquire(aio_context); 742 block_job_ref(job); 743 block_job_pause(job); 744 aio_context_release(aio_context); 745 } 746 } 747 748 void block_job_early_fail(BlockJob *job) 749 { 750 block_job_unref(job); 751 } 752 753 void block_job_completed(BlockJob *job, int ret) 754 { 755 assert(blk_bs(job->blk)->job == job); 756 assert(!job->completed); 757 job->completed = true; 758 job->ret = ret; 759 if (!job->txn) { 760 block_job_completed_single(job); 761 } else if (ret < 0 || block_job_is_cancelled(job)) { 762 block_job_completed_txn_abort(job); 763 } else { 764 block_job_completed_txn_success(job); 765 } 766 } 767 768 static bool block_job_should_pause(BlockJob *job) 769 { 770 return job->pause_count > 0; 771 } 772 773 /* Yield, and schedule a timer to reenter the coroutine after @ns nanoseconds. 774 * Reentering the job coroutine with block_job_enter() before the timer has 775 * expired is allowed and cancels the timer. 776 * 777 * If @ns is (uint64_t) -1, no timer is scheduled and block_job_enter() must be 778 * called explicitly. */ 779 static void block_job_do_yield(BlockJob *job, uint64_t ns) 780 { 781 block_job_lock(); 782 if (ns != -1) { 783 timer_mod(&job->sleep_timer, ns); 784 } 785 job->busy = false; 786 block_job_unlock(); 787 qemu_coroutine_yield(); 788 789 /* Set by block_job_enter before re-entering the coroutine. */ 790 assert(job->busy); 791 } 792 793 void coroutine_fn block_job_pause_point(BlockJob *job) 794 { 795 assert(job && block_job_started(job)); 796 797 if (!block_job_should_pause(job)) { 798 return; 799 } 800 if (block_job_is_cancelled(job)) { 801 return; 802 } 803 804 if (job->driver->pause) { 805 job->driver->pause(job); 806 } 807 808 if (block_job_should_pause(job) && !block_job_is_cancelled(job)) { 809 job->paused = true; 810 block_job_do_yield(job, -1); 811 job->paused = false; 812 } 813 814 if (job->driver->resume) { 815 job->driver->resume(job); 816 } 817 } 818 819 void block_job_resume_all(void) 820 { 821 BlockJob *job, *next; 822 823 QLIST_FOREACH_SAFE(job, &block_jobs, job_list, next) { 824 AioContext *aio_context = blk_get_aio_context(job->blk); 825 826 aio_context_acquire(aio_context); 827 block_job_resume(job); 828 block_job_unref(job); 829 aio_context_release(aio_context); 830 } 831 } 832 833 /* 834 * Conditionally enter a block_job pending a call to fn() while 835 * under the block_job_lock critical section. 836 */ 837 static void block_job_enter_cond(BlockJob *job, bool(*fn)(BlockJob *job)) 838 { 839 if (!block_job_started(job)) { 840 return; 841 } 842 if (job->deferred_to_main_loop) { 843 return; 844 } 845 846 block_job_lock(); 847 if (job->busy) { 848 block_job_unlock(); 849 return; 850 } 851 852 if (fn && !fn(job)) { 853 block_job_unlock(); 854 return; 855 } 856 857 assert(!job->deferred_to_main_loop); 858 timer_del(&job->sleep_timer); 859 job->busy = true; 860 block_job_unlock(); 861 aio_co_wake(job->co); 862 } 863 864 void block_job_enter(BlockJob *job) 865 { 866 block_job_enter_cond(job, NULL); 867 } 868 869 bool block_job_is_cancelled(BlockJob *job) 870 { 871 return job->cancelled; 872 } 873 874 void block_job_sleep_ns(BlockJob *job, int64_t ns) 875 { 876 assert(job->busy); 877 878 /* Check cancellation *before* setting busy = false, too! */ 879 if (block_job_is_cancelled(job)) { 880 return; 881 } 882 883 if (!block_job_should_pause(job)) { 884 block_job_do_yield(job, qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + ns); 885 } 886 887 block_job_pause_point(job); 888 } 889 890 void block_job_yield(BlockJob *job) 891 { 892 assert(job->busy); 893 894 /* Check cancellation *before* setting busy = false, too! */ 895 if (block_job_is_cancelled(job)) { 896 return; 897 } 898 899 if (!block_job_should_pause(job)) { 900 block_job_do_yield(job, -1); 901 } 902 903 block_job_pause_point(job); 904 } 905 906 void block_job_iostatus_reset(BlockJob *job) 907 { 908 if (job->iostatus == BLOCK_DEVICE_IO_STATUS_OK) { 909 return; 910 } 911 assert(job->user_paused && job->pause_count > 0); 912 job->iostatus = BLOCK_DEVICE_IO_STATUS_OK; 913 } 914 915 void block_job_event_ready(BlockJob *job) 916 { 917 job->ready = true; 918 919 if (block_job_is_internal(job)) { 920 return; 921 } 922 923 qapi_event_send_block_job_ready(job->driver->job_type, 924 job->id, 925 job->len, 926 job->offset, 927 job->speed, &error_abort); 928 } 929 930 BlockErrorAction block_job_error_action(BlockJob *job, BlockdevOnError on_err, 931 int is_read, int error) 932 { 933 BlockErrorAction action; 934 935 switch (on_err) { 936 case BLOCKDEV_ON_ERROR_ENOSPC: 937 case BLOCKDEV_ON_ERROR_AUTO: 938 action = (error == ENOSPC) ? 939 BLOCK_ERROR_ACTION_STOP : BLOCK_ERROR_ACTION_REPORT; 940 break; 941 case BLOCKDEV_ON_ERROR_STOP: 942 action = BLOCK_ERROR_ACTION_STOP; 943 break; 944 case BLOCKDEV_ON_ERROR_REPORT: 945 action = BLOCK_ERROR_ACTION_REPORT; 946 break; 947 case BLOCKDEV_ON_ERROR_IGNORE: 948 action = BLOCK_ERROR_ACTION_IGNORE; 949 break; 950 default: 951 abort(); 952 } 953 if (!block_job_is_internal(job)) { 954 qapi_event_send_block_job_error(job->id, 955 is_read ? IO_OPERATION_TYPE_READ : 956 IO_OPERATION_TYPE_WRITE, 957 action, &error_abort); 958 } 959 if (action == BLOCK_ERROR_ACTION_STOP) { 960 /* make the pause user visible, which will be resumed from QMP. */ 961 block_job_user_pause(job); 962 block_job_iostatus_set_err(job, error); 963 } 964 return action; 965 } 966 967 typedef struct { 968 BlockJob *job; 969 AioContext *aio_context; 970 BlockJobDeferToMainLoopFn *fn; 971 void *opaque; 972 } BlockJobDeferToMainLoopData; 973 974 static void block_job_defer_to_main_loop_bh(void *opaque) 975 { 976 BlockJobDeferToMainLoopData *data = opaque; 977 AioContext *aio_context; 978 979 /* Prevent race with block_job_defer_to_main_loop() */ 980 aio_context_acquire(data->aio_context); 981 982 /* Fetch BDS AioContext again, in case it has changed */ 983 aio_context = blk_get_aio_context(data->job->blk); 984 if (aio_context != data->aio_context) { 985 aio_context_acquire(aio_context); 986 } 987 988 data->fn(data->job, data->opaque); 989 990 if (aio_context != data->aio_context) { 991 aio_context_release(aio_context); 992 } 993 994 aio_context_release(data->aio_context); 995 996 g_free(data); 997 } 998 999 void block_job_defer_to_main_loop(BlockJob *job, 1000 BlockJobDeferToMainLoopFn *fn, 1001 void *opaque) 1002 { 1003 BlockJobDeferToMainLoopData *data = g_malloc(sizeof(*data)); 1004 data->job = job; 1005 data->aio_context = blk_get_aio_context(job->blk); 1006 data->fn = fn; 1007 data->opaque = opaque; 1008 job->deferred_to_main_loop = true; 1009 1010 aio_bh_schedule_oneshot(qemu_get_aio_context(), 1011 block_job_defer_to_main_loop_bh, data); 1012 } 1013