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
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"
37 #include "qemu/timer.h"
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
42 static QemuMutex block_job_mutex
;
44 static void block_job_lock(void)
46 qemu_mutex_lock(&block_job_mutex
);
49 static void block_job_unlock(void)
51 qemu_mutex_unlock(&block_job_mutex
);
54 static void __attribute__((__constructor__
)) block_job_init(void)
56 qemu_mutex_init(&block_job_mutex
);
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
));
63 /* Transactional group of block jobs */
66 /* Is this txn being cancelled? */
70 QLIST_HEAD(, BlockJob
) jobs
;
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
93 BlockJob
*block_job_next(BlockJob
*job
)
96 return QLIST_FIRST(&block_jobs
);
98 return QLIST_NEXT(job
, job_list
);
101 BlockJob
*block_job_get(const char *id
)
105 QLIST_FOREACH(job
, &block_jobs
, job_list
) {
106 if (job
->id
&& !strcmp(id
, job
->id
)) {
114 BlockJobTxn
*block_job_txn_new(void)
116 BlockJobTxn
*txn
= g_new0(BlockJobTxn
, 1);
117 QLIST_INIT(&txn
->jobs
);
122 static void block_job_txn_ref(BlockJobTxn
*txn
)
127 void block_job_txn_unref(BlockJobTxn
*txn
)
129 if (txn
&& --txn
->refcnt
== 0) {
134 void block_job_txn_add_job(BlockJobTxn
*txn
, BlockJob
*job
)
143 QLIST_INSERT_HEAD(&txn
->jobs
, job
, txn_list
);
144 block_job_txn_ref(txn
);
147 static void block_job_pause(BlockJob
*job
)
152 static void block_job_resume(BlockJob
*job
)
154 assert(job
->pause_count
> 0);
156 if (job
->pause_count
) {
159 block_job_enter(job
);
162 void block_job_ref(BlockJob
*job
)
167 static void block_job_attached_aio_context(AioContext
*new_context
,
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
);
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
);
182 error_free(job
->blocker
);
184 assert(!timer_pending(&job
->sleep_timer
));
189 static void block_job_attached_aio_context(AioContext
*new_context
,
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
);
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()... */
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
),
236 static void child_job_drained_begin(BdrvChild
*c
)
238 BlockJob
*job
= c
->opaque
;
239 block_job_pause(job
);
242 static void child_job_drained_end(BdrvChild
*c
)
244 BlockJob
*job
= c
->opaque
;
245 block_job_resume(job
);
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,
255 void block_job_remove_all_bdrv(BlockJob
*job
)
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
);
263 g_slist_free(job
->nodes
);
267 int block_job_add_bdrv(BlockJob
*job
, const char *name
, BlockDriverState
*bs
,
268 uint64_t perm
, uint64_t shared_perm
, Error
**errp
)
272 c
= bdrv_root_attach_child(bs
, name
, &child_job
, perm
, shared_perm
,
278 job
->nodes
= g_slist_prepend(job
->nodes
, c
);
280 bdrv_op_block_all(bs
, job
->blocker
);
285 bool block_job_is_internal(BlockJob
*job
)
287 return (job
->id
== NULL
);
290 static bool block_job_started(BlockJob
*job
)
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.
299 static void coroutine_fn
block_job_co_entry(void *opaque
)
301 BlockJob
*job
= opaque
;
303 assert(job
&& job
->driver
&& job
->driver
->start
);
304 block_job_pause_point(job
);
305 job
->driver
->start(job
);
308 static void block_job_sleep_timer_cb(void *opaque
)
310 BlockJob
*job
= opaque
;
312 block_job_enter(job
);
315 void block_job_start(BlockJob
*job
)
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
);
323 bdrv_coroutine_enter(blk_bs(job
->blk
), job
->co
);
326 static void block_job_completed_single(BlockJob
*job
)
328 assert(job
->completed
);
331 if (job
->driver
->commit
) {
332 job
->driver
->commit(job
);
335 if (job
->driver
->abort
) {
336 job
->driver
->abort(job
);
339 if (job
->driver
->clean
) {
340 job
->driver
->clean(job
);
344 job
->cb(job
->opaque
, job
->ret
);
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
);
352 const char *msg
= NULL
;
354 msg
= strerror(-job
->ret
);
356 block_job_event_completed(job
, msg
);
360 QLIST_REMOVE(job
, txn_list
);
361 block_job_txn_unref(job
->txn
);
362 block_job_unref(job
);
365 static void block_job_cancel_async(BlockJob
*job
)
367 if (job
->iostatus
!= BLOCK_DEVICE_IO_STATUS_OK
) {
368 block_job_iostatus_reset(job
);
370 if (job
->user_paused
) {
371 /* Do not call block_job_enter here, the caller will handle it. */
372 job
->user_paused
= false;
375 job
->cancelled
= true;
378 static int block_job_finish_sync(BlockJob
*job
,
379 void (*finish
)(BlockJob
*, Error
**errp
),
382 Error
*local_err
= NULL
;
385 assert(blk_bs(job
->blk
)->job
== job
);
390 finish(job
, &local_err
);
393 error_propagate(errp
, local_err
);
394 block_job_unref(job
);
397 /* block_job_drain calls block_job_enter, and it should be enough to
398 * induce progress until the job completes or moves to the main thread.
400 while (!job
->deferred_to_main_loop
&& !job
->completed
) {
401 block_job_drain(job
);
403 while (!job
->completed
) {
404 aio_poll(qemu_get_aio_context(), true);
406 ret
= (job
->cancelled
&& job
->ret
== 0) ? -ECANCELED
: job
->ret
;
407 block_job_unref(job
);
411 static void block_job_completed_txn_abort(BlockJob
*job
)
414 BlockJobTxn
*txn
= job
->txn
;
419 * We are cancelled by another job, which will handle everything.
423 txn
->aborting
= true;
424 block_job_txn_ref(txn
);
426 /* We are the first failed job. Cancel other jobs. */
427 QLIST_FOREACH(other_job
, &txn
->jobs
, txn_list
) {
428 ctx
= blk_get_aio_context(other_job
->blk
);
429 aio_context_acquire(ctx
);
432 /* Other jobs are effectively cancelled by us, set the status for
433 * them; this job, however, may or may not be cancelled, depending
434 * on the caller, so leave it. */
435 QLIST_FOREACH(other_job
, &txn
->jobs
, txn_list
) {
436 if (other_job
!= job
) {
437 block_job_cancel_async(other_job
);
440 while (!QLIST_EMPTY(&txn
->jobs
)) {
441 other_job
= QLIST_FIRST(&txn
->jobs
);
442 ctx
= blk_get_aio_context(other_job
->blk
);
443 if (!other_job
->completed
) {
444 assert(other_job
->cancelled
);
445 block_job_finish_sync(other_job
, NULL
, NULL
);
447 block_job_completed_single(other_job
);
448 aio_context_release(ctx
);
451 block_job_txn_unref(txn
);
454 static void block_job_completed_txn_success(BlockJob
*job
)
457 BlockJobTxn
*txn
= job
->txn
;
458 BlockJob
*other_job
, *next
;
460 * Successful completion, see if there are other running jobs in this
463 QLIST_FOREACH(other_job
, &txn
->jobs
, txn_list
) {
464 if (!other_job
->completed
) {
468 /* We are the last completed job, commit the transaction. */
469 QLIST_FOREACH_SAFE(other_job
, &txn
->jobs
, txn_list
, next
) {
470 ctx
= blk_get_aio_context(other_job
->blk
);
471 aio_context_acquire(ctx
);
472 assert(other_job
->ret
== 0);
473 block_job_completed_single(other_job
);
474 aio_context_release(ctx
);
478 /* Assumes the block_job_mutex is held */
479 static bool block_job_timer_pending(BlockJob
*job
)
481 return timer_pending(&job
->sleep_timer
);
484 void block_job_set_speed(BlockJob
*job
, int64_t speed
, Error
**errp
)
486 Error
*local_err
= NULL
;
487 int64_t old_speed
= job
->speed
;
489 if (!job
->driver
->set_speed
) {
490 error_setg(errp
, QERR_UNSUPPORTED
);
493 job
->driver
->set_speed(job
, speed
, &local_err
);
495 error_propagate(errp
, local_err
);
500 if (speed
&& speed
<= old_speed
) {
504 /* kick only if a timer is pending */
505 block_job_enter_cond(job
, block_job_timer_pending
);
508 void block_job_complete(BlockJob
*job
, Error
**errp
)
510 /* Should not be reachable via external interface for internal jobs */
512 if (job
->pause_count
|| job
->cancelled
||
513 !block_job_started(job
) || !job
->driver
->complete
) {
514 error_setg(errp
, "The active block job '%s' cannot be completed",
519 job
->driver
->complete(job
, errp
);
522 void block_job_user_pause(BlockJob
*job
)
524 job
->user_paused
= true;
525 block_job_pause(job
);
528 bool block_job_user_paused(BlockJob
*job
)
530 return job
->user_paused
;
533 void block_job_user_resume(BlockJob
*job
)
535 if (job
&& job
->user_paused
&& job
->pause_count
> 0) {
536 block_job_iostatus_reset(job
);
537 job
->user_paused
= false;
538 block_job_resume(job
);
542 void block_job_cancel(BlockJob
*job
)
544 if (block_job_started(job
)) {
545 block_job_cancel_async(job
);
546 block_job_enter(job
);
548 block_job_completed(job
, -ECANCELED
);
552 /* A wrapper around block_job_cancel() taking an Error ** parameter so it may be
553 * used with block_job_finish_sync() without the need for (rather nasty)
554 * function pointer casts there. */
555 static void block_job_cancel_err(BlockJob
*job
, Error
**errp
)
557 block_job_cancel(job
);
560 int block_job_cancel_sync(BlockJob
*job
)
562 return block_job_finish_sync(job
, &block_job_cancel_err
, NULL
);
565 void block_job_cancel_sync_all(void)
568 AioContext
*aio_context
;
570 while ((job
= QLIST_FIRST(&block_jobs
))) {
571 aio_context
= blk_get_aio_context(job
->blk
);
572 aio_context_acquire(aio_context
);
573 block_job_cancel_sync(job
);
574 aio_context_release(aio_context
);
578 int block_job_complete_sync(BlockJob
*job
, Error
**errp
)
580 return block_job_finish_sync(job
, &block_job_complete
, errp
);
583 BlockJobInfo
*block_job_query(BlockJob
*job
, Error
**errp
)
587 if (block_job_is_internal(job
)) {
588 error_setg(errp
, "Cannot query QEMU internal jobs");
591 info
= g_new0(BlockJobInfo
, 1);
592 info
->type
= g_strdup(BlockJobType_str(job
->driver
->job_type
));
593 info
->device
= g_strdup(job
->id
);
594 info
->len
= job
->len
;
595 info
->busy
= atomic_read(&job
->busy
);
596 info
->paused
= job
->pause_count
> 0;
597 info
->offset
= job
->offset
;
598 info
->speed
= job
->speed
;
599 info
->io_status
= job
->iostatus
;
600 info
->ready
= job
->ready
;
604 static void block_job_iostatus_set_err(BlockJob
*job
, int error
)
606 if (job
->iostatus
== BLOCK_DEVICE_IO_STATUS_OK
) {
607 job
->iostatus
= error
== ENOSPC
? BLOCK_DEVICE_IO_STATUS_NOSPACE
:
608 BLOCK_DEVICE_IO_STATUS_FAILED
;
612 static void block_job_event_cancelled(BlockJob
*job
)
614 if (block_job_is_internal(job
)) {
618 qapi_event_send_block_job_cancelled(job
->driver
->job_type
,
626 static void block_job_event_completed(BlockJob
*job
, const char *msg
)
628 if (block_job_is_internal(job
)) {
632 qapi_event_send_block_job_completed(job
->driver
->job_type
,
643 * API for block job drivers and the block layer. These functions are
644 * declared in blockjob_int.h.
647 void *block_job_create(const char *job_id
, const BlockJobDriver
*driver
,
648 BlockJobTxn
*txn
, BlockDriverState
*bs
, uint64_t perm
,
649 uint64_t shared_perm
, int64_t speed
, int flags
,
650 BlockCompletionFunc
*cb
, void *opaque
, Error
**errp
)
657 error_setg(errp
, QERR_DEVICE_IN_USE
, bdrv_get_device_name(bs
));
661 if (job_id
== NULL
&& !(flags
& BLOCK_JOB_INTERNAL
)) {
662 job_id
= bdrv_get_device_name(bs
);
664 error_setg(errp
, "An explicit job ID is required for this node");
670 if (flags
& BLOCK_JOB_INTERNAL
) {
671 error_setg(errp
, "Cannot specify job ID for internal block job");
675 if (!id_wellformed(job_id
)) {
676 error_setg(errp
, "Invalid job ID '%s'", job_id
);
680 if (block_job_get(job_id
)) {
681 error_setg(errp
, "Job ID '%s' already in use", job_id
);
686 blk
= blk_new(perm
, shared_perm
);
687 ret
= blk_insert_bs(blk
, bs
, errp
);
693 job
= g_malloc0(driver
->instance_size
);
694 job
->driver
= driver
;
695 job
->id
= g_strdup(job_id
);
698 job
->opaque
= opaque
;
701 job
->pause_count
= 1;
703 aio_timer_init(qemu_get_aio_context(), &job
->sleep_timer
,
704 QEMU_CLOCK_REALTIME
, SCALE_NS
,
705 block_job_sleep_timer_cb
, job
);
707 error_setg(&job
->blocker
, "block device is in use by block job: %s",
708 BlockJobType_str(driver
->job_type
));
709 block_job_add_bdrv(job
, "main node", bs
, 0, BLK_PERM_ALL
, &error_abort
);
712 bdrv_op_unblock(bs
, BLOCK_OP_TYPE_DATAPLANE
, job
->blocker
);
714 QLIST_INSERT_HEAD(&block_jobs
, job
, job_list
);
716 blk_add_aio_context_notifier(blk
, block_job_attached_aio_context
,
717 block_job_detach_aio_context
, job
);
719 /* Only set speed when necessary to avoid NotSupported error */
721 Error
*local_err
= NULL
;
723 block_job_set_speed(job
, speed
, &local_err
);
725 block_job_unref(job
);
726 error_propagate(errp
, local_err
);
731 /* Single jobs are modeled as single-job transactions for sake of
732 * consolidating the job management logic */
734 txn
= block_job_txn_new();
735 block_job_txn_add_job(txn
, job
);
736 block_job_txn_unref(txn
);
738 block_job_txn_add_job(txn
, job
);
744 void block_job_pause_all(void)
746 BlockJob
*job
= NULL
;
747 while ((job
= block_job_next(job
))) {
748 AioContext
*aio_context
= blk_get_aio_context(job
->blk
);
750 aio_context_acquire(aio_context
);
752 block_job_pause(job
);
753 aio_context_release(aio_context
);
757 void block_job_early_fail(BlockJob
*job
)
759 block_job_unref(job
);
762 void block_job_completed(BlockJob
*job
, int ret
)
764 assert(job
&& job
->txn
&& !job
->completed
);
765 assert(blk_bs(job
->blk
)->job
== job
);
766 job
->completed
= true;
768 if (ret
< 0 || block_job_is_cancelled(job
)) {
769 block_job_completed_txn_abort(job
);
771 block_job_completed_txn_success(job
);
775 static bool block_job_should_pause(BlockJob
*job
)
777 return job
->pause_count
> 0;
780 /* Yield, and schedule a timer to reenter the coroutine after @ns nanoseconds.
781 * Reentering the job coroutine with block_job_enter() before the timer has
782 * expired is allowed and cancels the timer.
784 * If @ns is (uint64_t) -1, no timer is scheduled and block_job_enter() must be
785 * called explicitly. */
786 static void block_job_do_yield(BlockJob
*job
, uint64_t ns
)
790 timer_mod(&job
->sleep_timer
, ns
);
794 qemu_coroutine_yield();
796 /* Set by block_job_enter before re-entering the coroutine. */
800 void coroutine_fn
block_job_pause_point(BlockJob
*job
)
802 assert(job
&& block_job_started(job
));
804 if (!block_job_should_pause(job
)) {
807 if (block_job_is_cancelled(job
)) {
811 if (job
->driver
->pause
) {
812 job
->driver
->pause(job
);
815 if (block_job_should_pause(job
) && !block_job_is_cancelled(job
)) {
817 block_job_do_yield(job
, -1);
821 if (job
->driver
->resume
) {
822 job
->driver
->resume(job
);
826 void block_job_resume_all(void)
828 BlockJob
*job
, *next
;
830 QLIST_FOREACH_SAFE(job
, &block_jobs
, job_list
, next
) {
831 AioContext
*aio_context
= blk_get_aio_context(job
->blk
);
833 aio_context_acquire(aio_context
);
834 block_job_resume(job
);
835 block_job_unref(job
);
836 aio_context_release(aio_context
);
841 * Conditionally enter a block_job pending a call to fn() while
842 * under the block_job_lock critical section.
844 static void block_job_enter_cond(BlockJob
*job
, bool(*fn
)(BlockJob
*job
))
846 if (!block_job_started(job
)) {
849 if (job
->deferred_to_main_loop
) {
859 if (fn
&& !fn(job
)) {
864 assert(!job
->deferred_to_main_loop
);
865 timer_del(&job
->sleep_timer
);
868 aio_co_wake(job
->co
);
871 void block_job_enter(BlockJob
*job
)
873 block_job_enter_cond(job
, NULL
);
876 bool block_job_is_cancelled(BlockJob
*job
)
878 return job
->cancelled
;
881 void block_job_sleep_ns(BlockJob
*job
, int64_t ns
)
885 /* Check cancellation *before* setting busy = false, too! */
886 if (block_job_is_cancelled(job
)) {
890 if (!block_job_should_pause(job
)) {
891 block_job_do_yield(job
, qemu_clock_get_ns(QEMU_CLOCK_REALTIME
) + ns
);
894 block_job_pause_point(job
);
897 void block_job_yield(BlockJob
*job
)
901 /* Check cancellation *before* setting busy = false, too! */
902 if (block_job_is_cancelled(job
)) {
906 if (!block_job_should_pause(job
)) {
907 block_job_do_yield(job
, -1);
910 block_job_pause_point(job
);
913 void block_job_iostatus_reset(BlockJob
*job
)
915 if (job
->iostatus
== BLOCK_DEVICE_IO_STATUS_OK
) {
918 assert(job
->user_paused
&& job
->pause_count
> 0);
919 job
->iostatus
= BLOCK_DEVICE_IO_STATUS_OK
;
922 void block_job_event_ready(BlockJob
*job
)
926 if (block_job_is_internal(job
)) {
930 qapi_event_send_block_job_ready(job
->driver
->job_type
,
934 job
->speed
, &error_abort
);
937 BlockErrorAction
block_job_error_action(BlockJob
*job
, BlockdevOnError on_err
,
938 int is_read
, int error
)
940 BlockErrorAction action
;
943 case BLOCKDEV_ON_ERROR_ENOSPC
:
944 case BLOCKDEV_ON_ERROR_AUTO
:
945 action
= (error
== ENOSPC
) ?
946 BLOCK_ERROR_ACTION_STOP
: BLOCK_ERROR_ACTION_REPORT
;
948 case BLOCKDEV_ON_ERROR_STOP
:
949 action
= BLOCK_ERROR_ACTION_STOP
;
951 case BLOCKDEV_ON_ERROR_REPORT
:
952 action
= BLOCK_ERROR_ACTION_REPORT
;
954 case BLOCKDEV_ON_ERROR_IGNORE
:
955 action
= BLOCK_ERROR_ACTION_IGNORE
;
960 if (!block_job_is_internal(job
)) {
961 qapi_event_send_block_job_error(job
->id
,
962 is_read
? IO_OPERATION_TYPE_READ
:
963 IO_OPERATION_TYPE_WRITE
,
964 action
, &error_abort
);
966 if (action
== BLOCK_ERROR_ACTION_STOP
) {
967 /* make the pause user visible, which will be resumed from QMP. */
968 block_job_user_pause(job
);
969 block_job_iostatus_set_err(job
, error
);
976 AioContext
*aio_context
;
977 BlockJobDeferToMainLoopFn
*fn
;
979 } BlockJobDeferToMainLoopData
;
981 static void block_job_defer_to_main_loop_bh(void *opaque
)
983 BlockJobDeferToMainLoopData
*data
= opaque
;
984 AioContext
*aio_context
;
986 /* Prevent race with block_job_defer_to_main_loop() */
987 aio_context_acquire(data
->aio_context
);
989 /* Fetch BDS AioContext again, in case it has changed */
990 aio_context
= blk_get_aio_context(data
->job
->blk
);
991 if (aio_context
!= data
->aio_context
) {
992 aio_context_acquire(aio_context
);
995 data
->fn(data
->job
, data
->opaque
);
997 if (aio_context
!= data
->aio_context
) {
998 aio_context_release(aio_context
);
1001 aio_context_release(data
->aio_context
);
1006 void block_job_defer_to_main_loop(BlockJob
*job
,
1007 BlockJobDeferToMainLoopFn
*fn
,
1010 BlockJobDeferToMainLoopData
*data
= g_malloc(sizeof(*data
));
1012 data
->aio_context
= blk_get_aio_context(job
->blk
);
1014 data
->opaque
= opaque
;
1015 job
->deferred_to_main_loop
= true;
1017 aio_bh_schedule_oneshot(qemu_get_aio_context(),
1018 block_job_defer_to_main_loop_bh
, data
);