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/qmp/qerror.h"
34 #include "qemu/coroutine.h"
36 #include "qemu/timer.h"
37 #include "qapi-event.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
);
361 QLIST_REMOVE(job
, txn_list
);
362 block_job_txn_unref(job
->txn
);
364 block_job_unref(job
);
367 static void block_job_cancel_async(BlockJob
*job
)
369 if (job
->iostatus
!= BLOCK_DEVICE_IO_STATUS_OK
) {
370 block_job_iostatus_reset(job
);
372 if (job
->user_paused
) {
373 /* Do not call block_job_enter here, the caller will handle it. */
374 job
->user_paused
= false;
377 job
->cancelled
= true;
380 static int block_job_finish_sync(BlockJob
*job
,
381 void (*finish
)(BlockJob
*, Error
**errp
),
384 Error
*local_err
= NULL
;
387 assert(blk_bs(job
->blk
)->job
== job
);
392 finish(job
, &local_err
);
395 error_propagate(errp
, local_err
);
396 block_job_unref(job
);
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.
402 while (!job
->deferred_to_main_loop
&& !job
->completed
) {
403 block_job_drain(job
);
405 while (!job
->completed
) {
406 aio_poll(qemu_get_aio_context(), true);
408 ret
= (job
->cancelled
&& job
->ret
== 0) ? -ECANCELED
: job
->ret
;
409 block_job_unref(job
);
413 static void block_job_completed_txn_abort(BlockJob
*job
)
416 BlockJobTxn
*txn
= job
->txn
;
421 * We are cancelled by another job, which will handle everything.
425 txn
->aborting
= true;
426 block_job_txn_ref(txn
);
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
);
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
);
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
);
449 block_job_completed_single(other_job
);
450 aio_context_release(ctx
);
453 block_job_txn_unref(txn
);
456 static void block_job_completed_txn_success(BlockJob
*job
)
459 BlockJobTxn
*txn
= job
->txn
;
460 BlockJob
*other_job
, *next
;
462 * Successful completion, see if there are other running jobs in this
465 QLIST_FOREACH(other_job
, &txn
->jobs
, txn_list
) {
466 if (!other_job
->completed
) {
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
);
480 /* Assumes the block_job_mutex is held */
481 static bool block_job_timer_pending(BlockJob
*job
)
483 return timer_pending(&job
->sleep_timer
);
486 void block_job_set_speed(BlockJob
*job
, int64_t speed
, Error
**errp
)
488 Error
*local_err
= NULL
;
489 int64_t old_speed
= job
->speed
;
491 if (!job
->driver
->set_speed
) {
492 error_setg(errp
, QERR_UNSUPPORTED
);
495 job
->driver
->set_speed(job
, speed
, &local_err
);
497 error_propagate(errp
, local_err
);
502 if (speed
<= old_speed
) {
506 /* kick only if a timer is pending */
507 block_job_enter_cond(job
, block_job_timer_pending
);
510 void block_job_complete(BlockJob
*job
, Error
**errp
)
512 /* Should not be reachable via external interface for internal jobs */
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",
521 job
->driver
->complete(job
, errp
);
524 void block_job_user_pause(BlockJob
*job
)
526 job
->user_paused
= true;
527 block_job_pause(job
);
530 bool block_job_user_paused(BlockJob
*job
)
532 return job
->user_paused
;
535 void block_job_user_resume(BlockJob
*job
)
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
);
544 void block_job_cancel(BlockJob
*job
)
546 if (block_job_started(job
)) {
547 block_job_cancel_async(job
);
548 block_job_enter(job
);
550 block_job_completed(job
, -ECANCELED
);
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
)
559 block_job_cancel(job
);
562 int block_job_cancel_sync(BlockJob
*job
)
564 return block_job_finish_sync(job
, &block_job_cancel_err
, NULL
);
567 void block_job_cancel_sync_all(void)
570 AioContext
*aio_context
;
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
);
580 int block_job_complete_sync(BlockJob
*job
, Error
**errp
)
582 return block_job_finish_sync(job
, &block_job_complete
, errp
);
585 BlockJobInfo
*block_job_query(BlockJob
*job
, Error
**errp
)
589 if (block_job_is_internal(job
)) {
590 error_setg(errp
, "Cannot query QEMU internal jobs");
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
;
606 static void block_job_iostatus_set_err(BlockJob
*job
, int error
)
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
;
614 static void block_job_event_cancelled(BlockJob
*job
)
616 if (block_job_is_internal(job
)) {
620 qapi_event_send_block_job_cancelled(job
->driver
->job_type
,
628 static void block_job_event_completed(BlockJob
*job
, const char *msg
)
630 if (block_job_is_internal(job
)) {
634 qapi_event_send_block_job_completed(job
->driver
->job_type
,
645 * API for block job drivers and the block layer. These functions are
646 * declared in blockjob_int.h.
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
)
659 error_setg(errp
, QERR_DEVICE_IN_USE
, bdrv_get_device_name(bs
));
663 if (job_id
== NULL
&& !(flags
& BLOCK_JOB_INTERNAL
)) {
664 job_id
= bdrv_get_device_name(bs
);
666 error_setg(errp
, "An explicit job ID is required for this node");
672 if (flags
& BLOCK_JOB_INTERNAL
) {
673 error_setg(errp
, "Cannot specify job ID for internal block job");
677 if (!id_wellformed(job_id
)) {
678 error_setg(errp
, "Invalid job ID '%s'", job_id
);
682 if (block_job_get(job_id
)) {
683 error_setg(errp
, "Job ID '%s' already in use", job_id
);
688 blk
= blk_new(perm
, shared_perm
);
689 ret
= blk_insert_bs(blk
, bs
, errp
);
695 job
= g_malloc0(driver
->instance_size
);
696 job
->driver
= driver
;
697 job
->id
= g_strdup(job_id
);
700 job
->opaque
= opaque
;
703 job
->pause_count
= 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
);
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
);
714 bdrv_op_unblock(bs
, BLOCK_OP_TYPE_DATAPLANE
, job
->blocker
);
716 QLIST_INSERT_HEAD(&block_jobs
, job
, job_list
);
718 blk_add_aio_context_notifier(blk
, block_job_attached_aio_context
,
719 block_job_detach_aio_context
, job
);
721 /* Only set speed when necessary to avoid NotSupported error */
723 Error
*local_err
= NULL
;
725 block_job_set_speed(job
, speed
, &local_err
);
727 block_job_unref(job
);
728 error_propagate(errp
, local_err
);
735 void block_job_pause_all(void)
737 BlockJob
*job
= NULL
;
738 while ((job
= block_job_next(job
))) {
739 AioContext
*aio_context
= blk_get_aio_context(job
->blk
);
741 aio_context_acquire(aio_context
);
743 block_job_pause(job
);
744 aio_context_release(aio_context
);
748 void block_job_early_fail(BlockJob
*job
)
750 block_job_unref(job
);
753 void block_job_completed(BlockJob
*job
, int ret
)
755 assert(blk_bs(job
->blk
)->job
== job
);
756 assert(!job
->completed
);
757 job
->completed
= true;
760 block_job_completed_single(job
);
761 } else if (ret
< 0 || block_job_is_cancelled(job
)) {
762 block_job_completed_txn_abort(job
);
764 block_job_completed_txn_success(job
);
768 static bool block_job_should_pause(BlockJob
*job
)
770 return job
->pause_count
> 0;
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.
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
)
783 timer_mod(&job
->sleep_timer
, ns
);
787 qemu_coroutine_yield();
789 /* Set by block_job_enter before re-entering the coroutine. */
793 void coroutine_fn
block_job_pause_point(BlockJob
*job
)
795 assert(job
&& block_job_started(job
));
797 if (!block_job_should_pause(job
)) {
800 if (block_job_is_cancelled(job
)) {
804 if (job
->driver
->pause
) {
805 job
->driver
->pause(job
);
808 if (block_job_should_pause(job
) && !block_job_is_cancelled(job
)) {
810 block_job_do_yield(job
, -1);
814 if (job
->driver
->resume
) {
815 job
->driver
->resume(job
);
819 void block_job_resume_all(void)
821 BlockJob
*job
, *next
;
823 QLIST_FOREACH_SAFE(job
, &block_jobs
, job_list
, next
) {
824 AioContext
*aio_context
= blk_get_aio_context(job
->blk
);
826 aio_context_acquire(aio_context
);
827 block_job_resume(job
);
828 block_job_unref(job
);
829 aio_context_release(aio_context
);
834 * Conditionally enter a block_job pending a call to fn() while
835 * under the block_job_lock critical section.
837 static void block_job_enter_cond(BlockJob
*job
, bool(*fn
)(BlockJob
*job
))
839 if (!block_job_started(job
)) {
842 if (job
->deferred_to_main_loop
) {
852 if (fn
&& !fn(job
)) {
857 assert(!job
->deferred_to_main_loop
);
858 timer_del(&job
->sleep_timer
);
861 aio_co_wake(job
->co
);
864 void block_job_enter(BlockJob
*job
)
866 block_job_enter_cond(job
, NULL
);
869 bool block_job_is_cancelled(BlockJob
*job
)
871 return job
->cancelled
;
874 void block_job_sleep_ns(BlockJob
*job
, int64_t ns
)
878 /* Check cancellation *before* setting busy = false, too! */
879 if (block_job_is_cancelled(job
)) {
883 if (!block_job_should_pause(job
)) {
884 block_job_do_yield(job
, qemu_clock_get_ns(QEMU_CLOCK_REALTIME
) + ns
);
887 block_job_pause_point(job
);
890 void block_job_yield(BlockJob
*job
)
894 /* Check cancellation *before* setting busy = false, too! */
895 if (block_job_is_cancelled(job
)) {
899 if (!block_job_should_pause(job
)) {
900 block_job_do_yield(job
, -1);
903 block_job_pause_point(job
);
906 void block_job_iostatus_reset(BlockJob
*job
)
908 if (job
->iostatus
== BLOCK_DEVICE_IO_STATUS_OK
) {
911 assert(job
->user_paused
&& job
->pause_count
> 0);
912 job
->iostatus
= BLOCK_DEVICE_IO_STATUS_OK
;
915 void block_job_event_ready(BlockJob
*job
)
919 if (block_job_is_internal(job
)) {
923 qapi_event_send_block_job_ready(job
->driver
->job_type
,
927 job
->speed
, &error_abort
);
930 BlockErrorAction
block_job_error_action(BlockJob
*job
, BlockdevOnError on_err
,
931 int is_read
, int error
)
933 BlockErrorAction action
;
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
;
941 case BLOCKDEV_ON_ERROR_STOP
:
942 action
= BLOCK_ERROR_ACTION_STOP
;
944 case BLOCKDEV_ON_ERROR_REPORT
:
945 action
= BLOCK_ERROR_ACTION_REPORT
;
947 case BLOCKDEV_ON_ERROR_IGNORE
:
948 action
= BLOCK_ERROR_ACTION_IGNORE
;
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
);
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
);
969 AioContext
*aio_context
;
970 BlockJobDeferToMainLoopFn
*fn
;
972 } BlockJobDeferToMainLoopData
;
974 static void block_job_defer_to_main_loop_bh(void *opaque
)
976 BlockJobDeferToMainLoopData
*data
= opaque
;
977 AioContext
*aio_context
;
979 /* Prevent race with block_job_defer_to_main_loop() */
980 aio_context_acquire(data
->aio_context
);
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
);
988 data
->fn(data
->job
, data
->opaque
);
990 if (aio_context
!= data
->aio_context
) {
991 aio_context_release(aio_context
);
994 aio_context_release(data
->aio_context
);
999 void block_job_defer_to_main_loop(BlockJob
*job
,
1000 BlockJobDeferToMainLoopFn
*fn
,
1003 BlockJobDeferToMainLoopData
*data
= g_malloc(sizeof(*data
));
1005 data
->aio_context
= blk_get_aio_context(job
->blk
);
1007 data
->opaque
= opaque
;
1008 job
->deferred_to_main_loop
= true;
1010 aio_bh_schedule_oneshot(qemu_get_aio_context(),
1011 block_job_defer_to_main_loop_bh
, data
);