s390x/tcg: introduce and use s390_program_interrupt()
[qemu.git] / blockjob.c
blob715c2c26801347652c5cea162cf2e23d07cb6df3
1 /*
2 * QEMU System Emulator block driver
4 * Copyright (c) 2011 IBM Corp.
5 * Copyright (c) 2012 Red Hat, Inc.
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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.
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/qmp/qerror.h"
33 #include "qapi/qmp/qjson.h"
34 #include "qemu/coroutine.h"
35 #include "qemu/id.h"
36 #include "qmp-commands.h"
37 #include "qemu/timer.h"
38 #include "qapi-event.h"
40 /* Right now, this mutex is only needed to synchronize accesses to job->busy
41 * and job->sleep_timer, such as concurrent calls to block_job_do_yield and
42 * block_job_enter. */
43 static QemuMutex block_job_mutex;
45 static void block_job_lock(void)
47 qemu_mutex_lock(&block_job_mutex);
50 static void block_job_unlock(void)
52 qemu_mutex_unlock(&block_job_mutex);
55 static void __attribute__((__constructor__)) block_job_init(void)
57 qemu_mutex_init(&block_job_mutex);
60 static void block_job_event_cancelled(BlockJob *job);
61 static void block_job_event_completed(BlockJob *job, const char *msg);
63 /* Transactional group of block jobs */
64 struct BlockJobTxn {
66 /* Is this txn being cancelled? */
67 bool aborting;
69 /* List of jobs */
70 QLIST_HEAD(, BlockJob) jobs;
72 /* Reference count */
73 int refcnt;
76 static QLIST_HEAD(, BlockJob) block_jobs = QLIST_HEAD_INITIALIZER(block_jobs);
79 * The block job API is composed of two categories of functions.
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.
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.
93 BlockJob *block_job_next(BlockJob *job)
95 if (!job) {
96 return QLIST_FIRST(&block_jobs);
98 return QLIST_NEXT(job, job_list);
101 BlockJob *block_job_get(const char *id)
103 BlockJob *job;
105 QLIST_FOREACH(job, &block_jobs, job_list) {
106 if (job->id && !strcmp(id, job->id)) {
107 return job;
111 return NULL;
114 BlockJobTxn *block_job_txn_new(void)
116 BlockJobTxn *txn = g_new0(BlockJobTxn, 1);
117 QLIST_INIT(&txn->jobs);
118 txn->refcnt = 1;
119 return txn;
122 static void block_job_txn_ref(BlockJobTxn *txn)
124 txn->refcnt++;
127 void block_job_txn_unref(BlockJobTxn *txn)
129 if (txn && --txn->refcnt == 0) {
130 g_free(txn);
134 void block_job_txn_add_job(BlockJobTxn *txn, BlockJob *job)
136 if (!txn) {
137 return;
140 assert(!job->txn);
141 job->txn = txn;
143 QLIST_INSERT_HEAD(&txn->jobs, job, txn_list);
144 block_job_txn_ref(txn);
147 static void block_job_pause(BlockJob *job)
149 job->pause_count++;
152 static void block_job_resume(BlockJob *job)
154 assert(job->pause_count > 0);
155 job->pause_count--;
156 if (job->pause_count) {
157 return;
159 block_job_enter(job);
162 void block_job_ref(BlockJob *job)
164 ++job->refcnt;
167 static void block_job_attached_aio_context(AioContext *new_context,
168 void *opaque);
169 static void block_job_detach_aio_context(void *opaque);
171 void block_job_unref(BlockJob *job)
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);
189 static void block_job_attached_aio_context(AioContext *new_context,
190 void *opaque)
192 BlockJob *job = opaque;
194 if (job->driver->attached_aio_context) {
195 job->driver->attached_aio_context(job, new_context);
198 block_job_resume(job);
201 static void block_job_drain(BlockJob *job)
203 /* If job is !job->busy this kicks it into the next pause point. */
204 block_job_enter(job);
206 blk_drain(job->blk);
207 if (job->driver->drain) {
208 job->driver->drain(job);
212 static void block_job_detach_aio_context(void *opaque)
214 BlockJob *job = opaque;
216 /* In case the job terminates during aio_poll()... */
217 block_job_ref(job);
219 block_job_pause(job);
221 while (!job->paused && !job->completed) {
222 block_job_drain(job);
225 block_job_unref(job);
228 static char *child_job_get_parent_desc(BdrvChild *c)
230 BlockJob *job = c->opaque;
231 return g_strdup_printf("%s job '%s'",
232 BlockJobType_str(job->driver->job_type),
233 job->id);
236 static const BdrvChildRole child_job = {
237 .get_parent_desc = child_job_get_parent_desc,
238 .stay_at_node = true,
241 static void block_job_drained_begin(void *opaque)
243 BlockJob *job = opaque;
244 block_job_pause(job);
247 static void block_job_drained_end(void *opaque)
249 BlockJob *job = opaque;
250 block_job_resume(job);
253 static const BlockDevOps block_job_dev_ops = {
254 .drained_begin = block_job_drained_begin,
255 .drained_end = block_job_drained_end,
258 void block_job_remove_all_bdrv(BlockJob *job)
260 GSList *l;
261 for (l = job->nodes; l; l = l->next) {
262 BdrvChild *c = l->data;
263 bdrv_op_unblock_all(c->bs, job->blocker);
264 bdrv_root_unref_child(c);
266 g_slist_free(job->nodes);
267 job->nodes = NULL;
270 int block_job_add_bdrv(BlockJob *job, const char *name, BlockDriverState *bs,
271 uint64_t perm, uint64_t shared_perm, Error **errp)
273 BdrvChild *c;
275 c = bdrv_root_attach_child(bs, name, &child_job, perm, shared_perm,
276 job, errp);
277 if (c == NULL) {
278 return -EPERM;
281 job->nodes = g_slist_prepend(job->nodes, c);
282 bdrv_ref(bs);
283 bdrv_op_block_all(bs, job->blocker);
285 return 0;
288 bool block_job_is_internal(BlockJob *job)
290 return (job->id == NULL);
293 static bool block_job_started(BlockJob *job)
295 return job->co;
299 * All jobs must allow a pause point before entering their job proper. This
300 * ensures that jobs can be paused prior to being started, then resumed later.
302 static void coroutine_fn block_job_co_entry(void *opaque)
304 BlockJob *job = opaque;
306 assert(job && job->driver && job->driver->start);
307 block_job_pause_point(job);
308 job->driver->start(job);
311 static void block_job_sleep_timer_cb(void *opaque)
313 BlockJob *job = opaque;
315 block_job_enter(job);
318 void block_job_start(BlockJob *job)
320 assert(job && !block_job_started(job) && job->paused &&
321 job->driver && job->driver->start);
322 job->co = qemu_coroutine_create(block_job_co_entry, job);
323 job->pause_count--;
324 job->busy = true;
325 job->paused = false;
326 bdrv_coroutine_enter(blk_bs(job->blk), job->co);
329 static void block_job_completed_single(BlockJob *job)
331 assert(job->completed);
333 if (!job->ret) {
334 if (job->driver->commit) {
335 job->driver->commit(job);
337 } else {
338 if (job->driver->abort) {
339 job->driver->abort(job);
342 if (job->driver->clean) {
343 job->driver->clean(job);
346 if (job->cb) {
347 job->cb(job->opaque, job->ret);
350 /* Emit events only if we actually started */
351 if (block_job_started(job)) {
352 if (block_job_is_cancelled(job)) {
353 block_job_event_cancelled(job);
354 } else {
355 const char *msg = NULL;
356 if (job->ret < 0) {
357 msg = strerror(-job->ret);
359 block_job_event_completed(job, msg);
363 if (job->txn) {
364 QLIST_REMOVE(job, txn_list);
365 block_job_txn_unref(job->txn);
367 block_job_unref(job);
370 static void block_job_cancel_async(BlockJob *job)
372 if (job->iostatus != BLOCK_DEVICE_IO_STATUS_OK) {
373 block_job_iostatus_reset(job);
375 if (job->user_paused) {
376 /* Do not call block_job_enter here, the caller will handle it. */
377 job->user_paused = false;
378 job->pause_count--;
380 job->cancelled = true;
383 static int block_job_finish_sync(BlockJob *job,
384 void (*finish)(BlockJob *, Error **errp),
385 Error **errp)
387 Error *local_err = NULL;
388 int ret;
390 assert(blk_bs(job->blk)->job == job);
392 block_job_ref(job);
394 if (finish) {
395 finish(job, &local_err);
397 if (local_err) {
398 error_propagate(errp, local_err);
399 block_job_unref(job);
400 return -EBUSY;
402 /* block_job_drain calls block_job_enter, and it should be enough to
403 * induce progress until the job completes or moves to the main thread.
405 while (!job->deferred_to_main_loop && !job->completed) {
406 block_job_drain(job);
408 while (!job->completed) {
409 aio_poll(qemu_get_aio_context(), true);
411 ret = (job->cancelled && job->ret == 0) ? -ECANCELED : job->ret;
412 block_job_unref(job);
413 return ret;
416 static void block_job_completed_txn_abort(BlockJob *job)
418 AioContext *ctx;
419 BlockJobTxn *txn = job->txn;
420 BlockJob *other_job;
422 if (txn->aborting) {
424 * We are cancelled by another job, which will handle everything.
426 return;
428 txn->aborting = true;
429 block_job_txn_ref(txn);
431 /* We are the first failed job. Cancel other jobs. */
432 QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
433 ctx = blk_get_aio_context(other_job->blk);
434 aio_context_acquire(ctx);
437 /* Other jobs are effectively cancelled by us, set the status for
438 * them; this job, however, may or may not be cancelled, depending
439 * on the caller, so leave it. */
440 QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
441 if (other_job != job) {
442 block_job_cancel_async(other_job);
445 while (!QLIST_EMPTY(&txn->jobs)) {
446 other_job = QLIST_FIRST(&txn->jobs);
447 ctx = blk_get_aio_context(other_job->blk);
448 if (!other_job->completed) {
449 assert(other_job->cancelled);
450 block_job_finish_sync(other_job, NULL, NULL);
452 block_job_completed_single(other_job);
453 aio_context_release(ctx);
456 block_job_txn_unref(txn);
459 static void block_job_completed_txn_success(BlockJob *job)
461 AioContext *ctx;
462 BlockJobTxn *txn = job->txn;
463 BlockJob *other_job, *next;
465 * Successful completion, see if there are other running jobs in this
466 * txn.
468 QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
469 if (!other_job->completed) {
470 return;
473 /* We are the last completed job, commit the transaction. */
474 QLIST_FOREACH_SAFE(other_job, &txn->jobs, txn_list, next) {
475 ctx = blk_get_aio_context(other_job->blk);
476 aio_context_acquire(ctx);
477 assert(other_job->ret == 0);
478 block_job_completed_single(other_job);
479 aio_context_release(ctx);
483 void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp)
485 Error *local_err = NULL;
487 if (!job->driver->set_speed) {
488 error_setg(errp, QERR_UNSUPPORTED);
489 return;
491 job->driver->set_speed(job, speed, &local_err);
492 if (local_err) {
493 error_propagate(errp, local_err);
494 return;
497 job->speed = speed;
500 void block_job_complete(BlockJob *job, Error **errp)
502 /* Should not be reachable via external interface for internal jobs */
503 assert(job->id);
504 if (job->pause_count || job->cancelled ||
505 !block_job_started(job) || !job->driver->complete) {
506 error_setg(errp, "The active block job '%s' cannot be completed",
507 job->id);
508 return;
511 job->driver->complete(job, errp);
514 void block_job_user_pause(BlockJob *job)
516 job->user_paused = true;
517 block_job_pause(job);
520 bool block_job_user_paused(BlockJob *job)
522 return job->user_paused;
525 void block_job_user_resume(BlockJob *job)
527 if (job && job->user_paused && job->pause_count > 0) {
528 block_job_iostatus_reset(job);
529 job->user_paused = false;
530 block_job_resume(job);
534 void block_job_cancel(BlockJob *job)
536 if (block_job_started(job)) {
537 block_job_cancel_async(job);
538 block_job_enter(job);
539 } else {
540 block_job_completed(job, -ECANCELED);
544 /* A wrapper around block_job_cancel() taking an Error ** parameter so it may be
545 * used with block_job_finish_sync() without the need for (rather nasty)
546 * function pointer casts there. */
547 static void block_job_cancel_err(BlockJob *job, Error **errp)
549 block_job_cancel(job);
552 int block_job_cancel_sync(BlockJob *job)
554 return block_job_finish_sync(job, &block_job_cancel_err, NULL);
557 void block_job_cancel_sync_all(void)
559 BlockJob *job;
560 AioContext *aio_context;
562 while ((job = QLIST_FIRST(&block_jobs))) {
563 aio_context = blk_get_aio_context(job->blk);
564 aio_context_acquire(aio_context);
565 block_job_cancel_sync(job);
566 aio_context_release(aio_context);
570 int block_job_complete_sync(BlockJob *job, Error **errp)
572 return block_job_finish_sync(job, &block_job_complete, errp);
575 BlockJobInfo *block_job_query(BlockJob *job, Error **errp)
577 BlockJobInfo *info;
579 if (block_job_is_internal(job)) {
580 error_setg(errp, "Cannot query QEMU internal jobs");
581 return NULL;
583 info = g_new0(BlockJobInfo, 1);
584 info->type = g_strdup(BlockJobType_str(job->driver->job_type));
585 info->device = g_strdup(job->id);
586 info->len = job->len;
587 info->busy = atomic_read(&job->busy);
588 info->paused = job->pause_count > 0;
589 info->offset = job->offset;
590 info->speed = job->speed;
591 info->io_status = job->iostatus;
592 info->ready = job->ready;
593 return info;
596 static void block_job_iostatus_set_err(BlockJob *job, int error)
598 if (job->iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
599 job->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE :
600 BLOCK_DEVICE_IO_STATUS_FAILED;
604 static void block_job_event_cancelled(BlockJob *job)
606 if (block_job_is_internal(job)) {
607 return;
610 qapi_event_send_block_job_cancelled(job->driver->job_type,
611 job->id,
612 job->len,
613 job->offset,
614 job->speed,
615 &error_abort);
618 static void block_job_event_completed(BlockJob *job, const char *msg)
620 if (block_job_is_internal(job)) {
621 return;
624 qapi_event_send_block_job_completed(job->driver->job_type,
625 job->id,
626 job->len,
627 job->offset,
628 job->speed,
629 !!msg,
630 msg,
631 &error_abort);
635 * API for block job drivers and the block layer. These functions are
636 * declared in blockjob_int.h.
639 void *block_job_create(const char *job_id, const BlockJobDriver *driver,
640 BlockDriverState *bs, uint64_t perm,
641 uint64_t shared_perm, int64_t speed, int flags,
642 BlockCompletionFunc *cb, void *opaque, Error **errp)
644 BlockBackend *blk;
645 BlockJob *job;
646 int ret;
648 if (bs->job) {
649 error_setg(errp, QERR_DEVICE_IN_USE, bdrv_get_device_name(bs));
650 return NULL;
653 if (job_id == NULL && !(flags & BLOCK_JOB_INTERNAL)) {
654 job_id = bdrv_get_device_name(bs);
655 if (!*job_id) {
656 error_setg(errp, "An explicit job ID is required for this node");
657 return NULL;
661 if (job_id) {
662 if (flags & BLOCK_JOB_INTERNAL) {
663 error_setg(errp, "Cannot specify job ID for internal block job");
664 return NULL;
667 if (!id_wellformed(job_id)) {
668 error_setg(errp, "Invalid job ID '%s'", job_id);
669 return NULL;
672 if (block_job_get(job_id)) {
673 error_setg(errp, "Job ID '%s' already in use", job_id);
674 return NULL;
678 blk = blk_new(perm, shared_perm);
679 ret = blk_insert_bs(blk, bs, errp);
680 if (ret < 0) {
681 blk_unref(blk);
682 return NULL;
685 job = g_malloc0(driver->instance_size);
686 job->driver = driver;
687 job->id = g_strdup(job_id);
688 job->blk = blk;
689 job->cb = cb;
690 job->opaque = opaque;
691 job->busy = false;
692 job->paused = true;
693 job->pause_count = 1;
694 job->refcnt = 1;
695 aio_timer_init(qemu_get_aio_context(), &job->sleep_timer,
696 QEMU_CLOCK_REALTIME, SCALE_NS,
697 block_job_sleep_timer_cb, job);
699 error_setg(&job->blocker, "block device is in use by block job: %s",
700 BlockJobType_str(driver->job_type));
701 block_job_add_bdrv(job, "main node", bs, 0, BLK_PERM_ALL, &error_abort);
702 bs->job = job;
704 blk_set_dev_ops(blk, &block_job_dev_ops, job);
705 bdrv_op_unblock(bs, BLOCK_OP_TYPE_DATAPLANE, job->blocker);
707 QLIST_INSERT_HEAD(&block_jobs, job, job_list);
709 blk_add_aio_context_notifier(blk, block_job_attached_aio_context,
710 block_job_detach_aio_context, job);
712 /* Only set speed when necessary to avoid NotSupported error */
713 if (speed != 0) {
714 Error *local_err = NULL;
716 block_job_set_speed(job, speed, &local_err);
717 if (local_err) {
718 block_job_unref(job);
719 error_propagate(errp, local_err);
720 return NULL;
723 return job;
726 void block_job_pause_all(void)
728 BlockJob *job = NULL;
729 while ((job = block_job_next(job))) {
730 AioContext *aio_context = blk_get_aio_context(job->blk);
732 aio_context_acquire(aio_context);
733 block_job_ref(job);
734 block_job_pause(job);
735 aio_context_release(aio_context);
739 void block_job_early_fail(BlockJob *job)
741 block_job_unref(job);
744 void block_job_completed(BlockJob *job, int ret)
746 assert(blk_bs(job->blk)->job == job);
747 assert(!job->completed);
748 job->completed = true;
749 job->ret = ret;
750 if (!job->txn) {
751 block_job_completed_single(job);
752 } else if (ret < 0 || block_job_is_cancelled(job)) {
753 block_job_completed_txn_abort(job);
754 } else {
755 block_job_completed_txn_success(job);
759 static bool block_job_should_pause(BlockJob *job)
761 return job->pause_count > 0;
764 /* Yield, and schedule a timer to reenter the coroutine after @ns nanoseconds.
765 * Reentering the job coroutine with block_job_enter() before the timer has
766 * expired is allowed and cancels the timer.
768 * If @ns is (uint64_t) -1, no timer is scheduled and block_job_enter() must be
769 * called explicitly. */
770 static void block_job_do_yield(BlockJob *job, uint64_t ns)
772 block_job_lock();
773 if (ns != -1) {
774 timer_mod(&job->sleep_timer, ns);
776 job->busy = false;
777 block_job_unlock();
778 qemu_coroutine_yield();
780 /* Set by block_job_enter before re-entering the coroutine. */
781 assert(job->busy);
784 void coroutine_fn block_job_pause_point(BlockJob *job)
786 assert(job && block_job_started(job));
788 if (!block_job_should_pause(job)) {
789 return;
791 if (block_job_is_cancelled(job)) {
792 return;
795 if (job->driver->pause) {
796 job->driver->pause(job);
799 if (block_job_should_pause(job) && !block_job_is_cancelled(job)) {
800 job->paused = true;
801 block_job_do_yield(job, -1);
802 job->paused = false;
805 if (job->driver->resume) {
806 job->driver->resume(job);
810 void block_job_resume_all(void)
812 BlockJob *job, *next;
814 QLIST_FOREACH_SAFE(job, &block_jobs, job_list, next) {
815 AioContext *aio_context = blk_get_aio_context(job->blk);
817 aio_context_acquire(aio_context);
818 block_job_resume(job);
819 block_job_unref(job);
820 aio_context_release(aio_context);
824 void block_job_enter(BlockJob *job)
826 if (!block_job_started(job)) {
827 return;
829 if (job->deferred_to_main_loop) {
830 return;
833 block_job_lock();
834 if (job->busy) {
835 block_job_unlock();
836 return;
839 assert(!job->deferred_to_main_loop);
840 timer_del(&job->sleep_timer);
841 job->busy = true;
842 block_job_unlock();
843 aio_co_wake(job->co);
846 bool block_job_is_cancelled(BlockJob *job)
848 return job->cancelled;
851 void block_job_sleep_ns(BlockJob *job, int64_t ns)
853 assert(job->busy);
855 /* Check cancellation *before* setting busy = false, too! */
856 if (block_job_is_cancelled(job)) {
857 return;
860 if (!block_job_should_pause(job)) {
861 block_job_do_yield(job, qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + ns);
864 block_job_pause_point(job);
867 void block_job_yield(BlockJob *job)
869 assert(job->busy);
871 /* Check cancellation *before* setting busy = false, too! */
872 if (block_job_is_cancelled(job)) {
873 return;
876 if (!block_job_should_pause(job)) {
877 block_job_do_yield(job, -1);
880 block_job_pause_point(job);
883 void block_job_iostatus_reset(BlockJob *job)
885 if (job->iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
886 return;
888 assert(job->user_paused && job->pause_count > 0);
889 job->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
892 void block_job_event_ready(BlockJob *job)
894 job->ready = true;
896 if (block_job_is_internal(job)) {
897 return;
900 qapi_event_send_block_job_ready(job->driver->job_type,
901 job->id,
902 job->len,
903 job->offset,
904 job->speed, &error_abort);
907 BlockErrorAction block_job_error_action(BlockJob *job, BlockdevOnError on_err,
908 int is_read, int error)
910 BlockErrorAction action;
912 switch (on_err) {
913 case BLOCKDEV_ON_ERROR_ENOSPC:
914 case BLOCKDEV_ON_ERROR_AUTO:
915 action = (error == ENOSPC) ?
916 BLOCK_ERROR_ACTION_STOP : BLOCK_ERROR_ACTION_REPORT;
917 break;
918 case BLOCKDEV_ON_ERROR_STOP:
919 action = BLOCK_ERROR_ACTION_STOP;
920 break;
921 case BLOCKDEV_ON_ERROR_REPORT:
922 action = BLOCK_ERROR_ACTION_REPORT;
923 break;
924 case BLOCKDEV_ON_ERROR_IGNORE:
925 action = BLOCK_ERROR_ACTION_IGNORE;
926 break;
927 default:
928 abort();
930 if (!block_job_is_internal(job)) {
931 qapi_event_send_block_job_error(job->id,
932 is_read ? IO_OPERATION_TYPE_READ :
933 IO_OPERATION_TYPE_WRITE,
934 action, &error_abort);
936 if (action == BLOCK_ERROR_ACTION_STOP) {
937 /* make the pause user visible, which will be resumed from QMP. */
938 block_job_user_pause(job);
939 block_job_iostatus_set_err(job, error);
941 return action;
944 typedef struct {
945 BlockJob *job;
946 AioContext *aio_context;
947 BlockJobDeferToMainLoopFn *fn;
948 void *opaque;
949 } BlockJobDeferToMainLoopData;
951 static void block_job_defer_to_main_loop_bh(void *opaque)
953 BlockJobDeferToMainLoopData *data = opaque;
954 AioContext *aio_context;
956 /* Prevent race with block_job_defer_to_main_loop() */
957 aio_context_acquire(data->aio_context);
959 /* Fetch BDS AioContext again, in case it has changed */
960 aio_context = blk_get_aio_context(data->job->blk);
961 if (aio_context != data->aio_context) {
962 aio_context_acquire(aio_context);
965 data->fn(data->job, data->opaque);
967 if (aio_context != data->aio_context) {
968 aio_context_release(aio_context);
971 aio_context_release(data->aio_context);
973 g_free(data);
976 void block_job_defer_to_main_loop(BlockJob *job,
977 BlockJobDeferToMainLoopFn *fn,
978 void *opaque)
980 BlockJobDeferToMainLoopData *data = g_malloc(sizeof(*data));
981 data->job = job;
982 data->aio_context = blk_get_aio_context(job->blk);
983 data->fn = fn;
984 data->opaque = opaque;
985 job->deferred_to_main_loop = true;
987 aio_bh_schedule_oneshot(qemu_get_aio_context(),
988 block_job_defer_to_main_loop_bh, data);