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/qmp/qerror.h"
33 #include "qapi/qmp/qjson.h"
34 #include "qemu/coroutine.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
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
);
62 static void block_job_enter_cond(BlockJob
*job
, bool(*fn
)(BlockJob
*job
));
64 /* Transactional group of block jobs */
67 /* Is this txn being cancelled? */
71 QLIST_HEAD(, BlockJob
) jobs
;
77 static QLIST_HEAD(, BlockJob
) block_jobs
= QLIST_HEAD_INITIALIZER(block_jobs
);
80 * The block job API is composed of two categories of functions.
82 * The first includes functions used by the monitor. The monitor is
83 * peculiar in that it accesses the block job list with block_job_get, and
84 * therefore needs consistency across block_job_get and the actual operation
85 * (e.g. block_job_set_speed). The consistency is achieved with
86 * aio_context_acquire/release. These functions are declared in blockjob.h.
88 * The second includes functions used by the block job drivers and sometimes
89 * by the core block layer. These do not care about locking, because the
90 * whole coroutine runs under the AioContext lock, and are declared in
94 BlockJob
*block_job_next(BlockJob
*job
)
97 return QLIST_FIRST(&block_jobs
);
99 return QLIST_NEXT(job
, job_list
);
102 BlockJob
*block_job_get(const char *id
)
106 QLIST_FOREACH(job
, &block_jobs
, job_list
) {
107 if (job
->id
&& !strcmp(id
, job
->id
)) {
115 BlockJobTxn
*block_job_txn_new(void)
117 BlockJobTxn
*txn
= g_new0(BlockJobTxn
, 1);
118 QLIST_INIT(&txn
->jobs
);
123 static void block_job_txn_ref(BlockJobTxn
*txn
)
128 void block_job_txn_unref(BlockJobTxn
*txn
)
130 if (txn
&& --txn
->refcnt
== 0) {
135 void block_job_txn_add_job(BlockJobTxn
*txn
, BlockJob
*job
)
144 QLIST_INSERT_HEAD(&txn
->jobs
, job
, txn_list
);
145 block_job_txn_ref(txn
);
148 static void block_job_pause(BlockJob
*job
)
153 static void block_job_resume(BlockJob
*job
)
155 assert(job
->pause_count
> 0);
157 if (job
->pause_count
) {
160 block_job_enter(job
);
163 void block_job_ref(BlockJob
*job
)
168 static void block_job_attached_aio_context(AioContext
*new_context
,
170 static void block_job_detach_aio_context(void *opaque
);
172 void block_job_unref(BlockJob
*job
)
174 if (--job
->refcnt
== 0) {
175 BlockDriverState
*bs
= blk_bs(job
->blk
);
176 QLIST_REMOVE(job
, job_list
);
178 block_job_remove_all_bdrv(job
);
179 blk_remove_aio_context_notifier(job
->blk
,
180 block_job_attached_aio_context
,
181 block_job_detach_aio_context
, job
);
183 error_free(job
->blocker
);
185 assert(!timer_pending(&job
->sleep_timer
));
190 static void block_job_attached_aio_context(AioContext
*new_context
,
193 BlockJob
*job
= opaque
;
195 if (job
->driver
->attached_aio_context
) {
196 job
->driver
->attached_aio_context(job
, new_context
);
199 block_job_resume(job
);
202 static void block_job_drain(BlockJob
*job
)
204 /* If job is !job->busy this kicks it into the next pause point. */
205 block_job_enter(job
);
208 if (job
->driver
->drain
) {
209 job
->driver
->drain(job
);
213 static void block_job_detach_aio_context(void *opaque
)
215 BlockJob
*job
= opaque
;
217 /* In case the job terminates during aio_poll()... */
220 block_job_pause(job
);
222 while (!job
->paused
&& !job
->completed
) {
223 block_job_drain(job
);
226 block_job_unref(job
);
229 static char *child_job_get_parent_desc(BdrvChild
*c
)
231 BlockJob
*job
= c
->opaque
;
232 return g_strdup_printf("%s job '%s'",
233 BlockJobType_str(job
->driver
->job_type
),
237 static void child_job_drained_begin(BdrvChild
*c
)
239 BlockJob
*job
= c
->opaque
;
240 block_job_pause(job
);
243 static void child_job_drained_end(BdrvChild
*c
)
245 BlockJob
*job
= c
->opaque
;
246 block_job_resume(job
);
249 static const BdrvChildRole child_job
= {
250 .get_parent_desc
= child_job_get_parent_desc
,
251 .drained_begin
= child_job_drained_begin
,
252 .drained_end
= child_job_drained_end
,
253 .stay_at_node
= true,
256 void block_job_remove_all_bdrv(BlockJob
*job
)
259 for (l
= job
->nodes
; l
; l
= l
->next
) {
260 BdrvChild
*c
= l
->data
;
261 bdrv_op_unblock_all(c
->bs
, job
->blocker
);
262 bdrv_root_unref_child(c
);
264 g_slist_free(job
->nodes
);
268 int block_job_add_bdrv(BlockJob
*job
, const char *name
, BlockDriverState
*bs
,
269 uint64_t perm
, uint64_t shared_perm
, Error
**errp
)
273 c
= bdrv_root_attach_child(bs
, name
, &child_job
, perm
, shared_perm
,
279 job
->nodes
= g_slist_prepend(job
->nodes
, c
);
281 bdrv_op_block_all(bs
, job
->blocker
);
286 bool block_job_is_internal(BlockJob
*job
)
288 return (job
->id
== NULL
);
291 static bool block_job_started(BlockJob
*job
)
297 * All jobs must allow a pause point before entering their job proper. This
298 * ensures that jobs can be paused prior to being started, then resumed later.
300 static void coroutine_fn
block_job_co_entry(void *opaque
)
302 BlockJob
*job
= opaque
;
304 assert(job
&& job
->driver
&& job
->driver
->start
);
305 block_job_pause_point(job
);
306 job
->driver
->start(job
);
309 static void block_job_sleep_timer_cb(void *opaque
)
311 BlockJob
*job
= opaque
;
313 block_job_enter(job
);
316 void block_job_start(BlockJob
*job
)
318 assert(job
&& !block_job_started(job
) && job
->paused
&&
319 job
->driver
&& job
->driver
->start
);
320 job
->co
= qemu_coroutine_create(block_job_co_entry
, job
);
324 bdrv_coroutine_enter(blk_bs(job
->blk
), job
->co
);
327 static void block_job_completed_single(BlockJob
*job
)
329 assert(job
->completed
);
332 if (job
->driver
->commit
) {
333 job
->driver
->commit(job
);
336 if (job
->driver
->abort
) {
337 job
->driver
->abort(job
);
340 if (job
->driver
->clean
) {
341 job
->driver
->clean(job
);
345 job
->cb(job
->opaque
, job
->ret
);
348 /* Emit events only if we actually started */
349 if (block_job_started(job
)) {
350 if (block_job_is_cancelled(job
)) {
351 block_job_event_cancelled(job
);
353 const char *msg
= NULL
;
355 msg
= strerror(-job
->ret
);
357 block_job_event_completed(job
, msg
);
362 QLIST_REMOVE(job
, txn_list
);
363 block_job_txn_unref(job
->txn
);
365 block_job_unref(job
);
368 static void block_job_cancel_async(BlockJob
*job
)
370 if (job
->iostatus
!= BLOCK_DEVICE_IO_STATUS_OK
) {
371 block_job_iostatus_reset(job
);
373 if (job
->user_paused
) {
374 /* Do not call block_job_enter here, the caller will handle it. */
375 job
->user_paused
= false;
378 job
->cancelled
= true;
381 static int block_job_finish_sync(BlockJob
*job
,
382 void (*finish
)(BlockJob
*, Error
**errp
),
385 Error
*local_err
= NULL
;
388 assert(blk_bs(job
->blk
)->job
== job
);
393 finish(job
, &local_err
);
396 error_propagate(errp
, local_err
);
397 block_job_unref(job
);
400 /* block_job_drain calls block_job_enter, and it should be enough to
401 * induce progress until the job completes or moves to the main thread.
403 while (!job
->deferred_to_main_loop
&& !job
->completed
) {
404 block_job_drain(job
);
406 while (!job
->completed
) {
407 aio_poll(qemu_get_aio_context(), true);
409 ret
= (job
->cancelled
&& job
->ret
== 0) ? -ECANCELED
: job
->ret
;
410 block_job_unref(job
);
414 static void block_job_completed_txn_abort(BlockJob
*job
)
417 BlockJobTxn
*txn
= job
->txn
;
422 * We are cancelled by another job, which will handle everything.
426 txn
->aborting
= true;
427 block_job_txn_ref(txn
);
429 /* We are the first failed job. Cancel other jobs. */
430 QLIST_FOREACH(other_job
, &txn
->jobs
, txn_list
) {
431 ctx
= blk_get_aio_context(other_job
->blk
);
432 aio_context_acquire(ctx
);
435 /* Other jobs are effectively cancelled by us, set the status for
436 * them; this job, however, may or may not be cancelled, depending
437 * on the caller, so leave it. */
438 QLIST_FOREACH(other_job
, &txn
->jobs
, txn_list
) {
439 if (other_job
!= job
) {
440 block_job_cancel_async(other_job
);
443 while (!QLIST_EMPTY(&txn
->jobs
)) {
444 other_job
= QLIST_FIRST(&txn
->jobs
);
445 ctx
= blk_get_aio_context(other_job
->blk
);
446 if (!other_job
->completed
) {
447 assert(other_job
->cancelled
);
448 block_job_finish_sync(other_job
, NULL
, NULL
);
450 block_job_completed_single(other_job
);
451 aio_context_release(ctx
);
454 block_job_txn_unref(txn
);
457 static void block_job_completed_txn_success(BlockJob
*job
)
460 BlockJobTxn
*txn
= job
->txn
;
461 BlockJob
*other_job
, *next
;
463 * Successful completion, see if there are other running jobs in this
466 QLIST_FOREACH(other_job
, &txn
->jobs
, txn_list
) {
467 if (!other_job
->completed
) {
471 /* We are the last completed job, commit the transaction. */
472 QLIST_FOREACH_SAFE(other_job
, &txn
->jobs
, txn_list
, next
) {
473 ctx
= blk_get_aio_context(other_job
->blk
);
474 aio_context_acquire(ctx
);
475 assert(other_job
->ret
== 0);
476 block_job_completed_single(other_job
);
477 aio_context_release(ctx
);
481 /* Assumes the block_job_mutex is held */
482 static bool block_job_timer_pending(BlockJob
*job
)
484 return timer_pending(&job
->sleep_timer
);
487 void block_job_set_speed(BlockJob
*job
, int64_t speed
, Error
**errp
)
489 Error
*local_err
= NULL
;
490 int64_t old_speed
= job
->speed
;
492 if (!job
->driver
->set_speed
) {
493 error_setg(errp
, QERR_UNSUPPORTED
);
496 job
->driver
->set_speed(job
, speed
, &local_err
);
498 error_propagate(errp
, local_err
);
503 if (speed
<= old_speed
) {
507 /* kick only if a timer is pending */
508 block_job_enter_cond(job
, block_job_timer_pending
);
511 void block_job_complete(BlockJob
*job
, Error
**errp
)
513 /* Should not be reachable via external interface for internal jobs */
515 if (job
->pause_count
|| job
->cancelled
||
516 !block_job_started(job
) || !job
->driver
->complete
) {
517 error_setg(errp
, "The active block job '%s' cannot be completed",
522 job
->driver
->complete(job
, errp
);
525 void block_job_user_pause(BlockJob
*job
)
527 job
->user_paused
= true;
528 block_job_pause(job
);
531 bool block_job_user_paused(BlockJob
*job
)
533 return job
->user_paused
;
536 void block_job_user_resume(BlockJob
*job
)
538 if (job
&& job
->user_paused
&& job
->pause_count
> 0) {
539 block_job_iostatus_reset(job
);
540 job
->user_paused
= false;
541 block_job_resume(job
);
545 void block_job_cancel(BlockJob
*job
)
547 if (block_job_started(job
)) {
548 block_job_cancel_async(job
);
549 block_job_enter(job
);
551 block_job_completed(job
, -ECANCELED
);
555 /* A wrapper around block_job_cancel() taking an Error ** parameter so it may be
556 * used with block_job_finish_sync() without the need for (rather nasty)
557 * function pointer casts there. */
558 static void block_job_cancel_err(BlockJob
*job
, Error
**errp
)
560 block_job_cancel(job
);
563 int block_job_cancel_sync(BlockJob
*job
)
565 return block_job_finish_sync(job
, &block_job_cancel_err
, NULL
);
568 void block_job_cancel_sync_all(void)
571 AioContext
*aio_context
;
573 while ((job
= QLIST_FIRST(&block_jobs
))) {
574 aio_context
= blk_get_aio_context(job
->blk
);
575 aio_context_acquire(aio_context
);
576 block_job_cancel_sync(job
);
577 aio_context_release(aio_context
);
581 int block_job_complete_sync(BlockJob
*job
, Error
**errp
)
583 return block_job_finish_sync(job
, &block_job_complete
, errp
);
586 BlockJobInfo
*block_job_query(BlockJob
*job
, Error
**errp
)
590 if (block_job_is_internal(job
)) {
591 error_setg(errp
, "Cannot query QEMU internal jobs");
594 info
= g_new0(BlockJobInfo
, 1);
595 info
->type
= g_strdup(BlockJobType_str(job
->driver
->job_type
));
596 info
->device
= g_strdup(job
->id
);
597 info
->len
= job
->len
;
598 info
->busy
= atomic_read(&job
->busy
);
599 info
->paused
= job
->pause_count
> 0;
600 info
->offset
= job
->offset
;
601 info
->speed
= job
->speed
;
602 info
->io_status
= job
->iostatus
;
603 info
->ready
= job
->ready
;
607 static void block_job_iostatus_set_err(BlockJob
*job
, int error
)
609 if (job
->iostatus
== BLOCK_DEVICE_IO_STATUS_OK
) {
610 job
->iostatus
= error
== ENOSPC
? BLOCK_DEVICE_IO_STATUS_NOSPACE
:
611 BLOCK_DEVICE_IO_STATUS_FAILED
;
615 static void block_job_event_cancelled(BlockJob
*job
)
617 if (block_job_is_internal(job
)) {
621 qapi_event_send_block_job_cancelled(job
->driver
->job_type
,
629 static void block_job_event_completed(BlockJob
*job
, const char *msg
)
631 if (block_job_is_internal(job
)) {
635 qapi_event_send_block_job_completed(job
->driver
->job_type
,
646 * API for block job drivers and the block layer. These functions are
647 * declared in blockjob_int.h.
650 void *block_job_create(const char *job_id
, const BlockJobDriver
*driver
,
651 BlockDriverState
*bs
, uint64_t perm
,
652 uint64_t shared_perm
, int64_t speed
, int flags
,
653 BlockCompletionFunc
*cb
, void *opaque
, Error
**errp
)
660 error_setg(errp
, QERR_DEVICE_IN_USE
, bdrv_get_device_name(bs
));
664 if (job_id
== NULL
&& !(flags
& BLOCK_JOB_INTERNAL
)) {
665 job_id
= bdrv_get_device_name(bs
);
667 error_setg(errp
, "An explicit job ID is required for this node");
673 if (flags
& BLOCK_JOB_INTERNAL
) {
674 error_setg(errp
, "Cannot specify job ID for internal block job");
678 if (!id_wellformed(job_id
)) {
679 error_setg(errp
, "Invalid job ID '%s'", job_id
);
683 if (block_job_get(job_id
)) {
684 error_setg(errp
, "Job ID '%s' already in use", job_id
);
689 blk
= blk_new(perm
, shared_perm
);
690 ret
= blk_insert_bs(blk
, bs
, errp
);
696 job
= g_malloc0(driver
->instance_size
);
697 job
->driver
= driver
;
698 job
->id
= g_strdup(job_id
);
701 job
->opaque
= opaque
;
704 job
->pause_count
= 1;
706 aio_timer_init(qemu_get_aio_context(), &job
->sleep_timer
,
707 QEMU_CLOCK_REALTIME
, SCALE_NS
,
708 block_job_sleep_timer_cb
, job
);
710 error_setg(&job
->blocker
, "block device is in use by block job: %s",
711 BlockJobType_str(driver
->job_type
));
712 block_job_add_bdrv(job
, "main node", bs
, 0, BLK_PERM_ALL
, &error_abort
);
715 bdrv_op_unblock(bs
, BLOCK_OP_TYPE_DATAPLANE
, job
->blocker
);
717 QLIST_INSERT_HEAD(&block_jobs
, job
, job_list
);
719 blk_add_aio_context_notifier(blk
, block_job_attached_aio_context
,
720 block_job_detach_aio_context
, job
);
722 /* Only set speed when necessary to avoid NotSupported error */
724 Error
*local_err
= NULL
;
726 block_job_set_speed(job
, speed
, &local_err
);
728 block_job_unref(job
);
729 error_propagate(errp
, local_err
);
736 void block_job_pause_all(void)
738 BlockJob
*job
= NULL
;
739 while ((job
= block_job_next(job
))) {
740 AioContext
*aio_context
= blk_get_aio_context(job
->blk
);
742 aio_context_acquire(aio_context
);
744 block_job_pause(job
);
745 aio_context_release(aio_context
);
749 void block_job_early_fail(BlockJob
*job
)
751 block_job_unref(job
);
754 void block_job_completed(BlockJob
*job
, int ret
)
756 assert(blk_bs(job
->blk
)->job
== job
);
757 assert(!job
->completed
);
758 job
->completed
= true;
761 block_job_completed_single(job
);
762 } else if (ret
< 0 || block_job_is_cancelled(job
)) {
763 block_job_completed_txn_abort(job
);
765 block_job_completed_txn_success(job
);
769 static bool block_job_should_pause(BlockJob
*job
)
771 return job
->pause_count
> 0;
774 /* Yield, and schedule a timer to reenter the coroutine after @ns nanoseconds.
775 * Reentering the job coroutine with block_job_enter() before the timer has
776 * expired is allowed and cancels the timer.
778 * If @ns is (uint64_t) -1, no timer is scheduled and block_job_enter() must be
779 * called explicitly. */
780 static void block_job_do_yield(BlockJob
*job
, uint64_t ns
)
784 timer_mod(&job
->sleep_timer
, ns
);
788 qemu_coroutine_yield();
790 /* Set by block_job_enter before re-entering the coroutine. */
794 void coroutine_fn
block_job_pause_point(BlockJob
*job
)
796 assert(job
&& block_job_started(job
));
798 if (!block_job_should_pause(job
)) {
801 if (block_job_is_cancelled(job
)) {
805 if (job
->driver
->pause
) {
806 job
->driver
->pause(job
);
809 if (block_job_should_pause(job
) && !block_job_is_cancelled(job
)) {
811 block_job_do_yield(job
, -1);
815 if (job
->driver
->resume
) {
816 job
->driver
->resume(job
);
820 void block_job_resume_all(void)
822 BlockJob
*job
, *next
;
824 QLIST_FOREACH_SAFE(job
, &block_jobs
, job_list
, next
) {
825 AioContext
*aio_context
= blk_get_aio_context(job
->blk
);
827 aio_context_acquire(aio_context
);
828 block_job_resume(job
);
829 block_job_unref(job
);
830 aio_context_release(aio_context
);
835 * Conditionally enter a block_job pending a call to fn() while
836 * under the block_job_lock critical section.
838 static void block_job_enter_cond(BlockJob
*job
, bool(*fn
)(BlockJob
*job
))
840 if (!block_job_started(job
)) {
843 if (job
->deferred_to_main_loop
) {
853 if (fn
&& !fn(job
)) {
858 assert(!job
->deferred_to_main_loop
);
859 timer_del(&job
->sleep_timer
);
862 aio_co_wake(job
->co
);
865 void block_job_enter(BlockJob
*job
)
867 block_job_enter_cond(job
, NULL
);
870 bool block_job_is_cancelled(BlockJob
*job
)
872 return job
->cancelled
;
875 void block_job_sleep_ns(BlockJob
*job
, int64_t ns
)
879 /* Check cancellation *before* setting busy = false, too! */
880 if (block_job_is_cancelled(job
)) {
884 if (!block_job_should_pause(job
)) {
885 block_job_do_yield(job
, qemu_clock_get_ns(QEMU_CLOCK_REALTIME
) + ns
);
888 block_job_pause_point(job
);
891 void block_job_yield(BlockJob
*job
)
895 /* Check cancellation *before* setting busy = false, too! */
896 if (block_job_is_cancelled(job
)) {
900 if (!block_job_should_pause(job
)) {
901 block_job_do_yield(job
, -1);
904 block_job_pause_point(job
);
907 void block_job_iostatus_reset(BlockJob
*job
)
909 if (job
->iostatus
== BLOCK_DEVICE_IO_STATUS_OK
) {
912 assert(job
->user_paused
&& job
->pause_count
> 0);
913 job
->iostatus
= BLOCK_DEVICE_IO_STATUS_OK
;
916 void block_job_event_ready(BlockJob
*job
)
920 if (block_job_is_internal(job
)) {
924 qapi_event_send_block_job_ready(job
->driver
->job_type
,
928 job
->speed
, &error_abort
);
931 BlockErrorAction
block_job_error_action(BlockJob
*job
, BlockdevOnError on_err
,
932 int is_read
, int error
)
934 BlockErrorAction action
;
937 case BLOCKDEV_ON_ERROR_ENOSPC
:
938 case BLOCKDEV_ON_ERROR_AUTO
:
939 action
= (error
== ENOSPC
) ?
940 BLOCK_ERROR_ACTION_STOP
: BLOCK_ERROR_ACTION_REPORT
;
942 case BLOCKDEV_ON_ERROR_STOP
:
943 action
= BLOCK_ERROR_ACTION_STOP
;
945 case BLOCKDEV_ON_ERROR_REPORT
:
946 action
= BLOCK_ERROR_ACTION_REPORT
;
948 case BLOCKDEV_ON_ERROR_IGNORE
:
949 action
= BLOCK_ERROR_ACTION_IGNORE
;
954 if (!block_job_is_internal(job
)) {
955 qapi_event_send_block_job_error(job
->id
,
956 is_read
? IO_OPERATION_TYPE_READ
:
957 IO_OPERATION_TYPE_WRITE
,
958 action
, &error_abort
);
960 if (action
== BLOCK_ERROR_ACTION_STOP
) {
961 /* make the pause user visible, which will be resumed from QMP. */
962 block_job_user_pause(job
);
963 block_job_iostatus_set_err(job
, error
);
970 AioContext
*aio_context
;
971 BlockJobDeferToMainLoopFn
*fn
;
973 } BlockJobDeferToMainLoopData
;
975 static void block_job_defer_to_main_loop_bh(void *opaque
)
977 BlockJobDeferToMainLoopData
*data
= opaque
;
978 AioContext
*aio_context
;
980 /* Prevent race with block_job_defer_to_main_loop() */
981 aio_context_acquire(data
->aio_context
);
983 /* Fetch BDS AioContext again, in case it has changed */
984 aio_context
= blk_get_aio_context(data
->job
->blk
);
985 if (aio_context
!= data
->aio_context
) {
986 aio_context_acquire(aio_context
);
989 data
->fn(data
->job
, data
->opaque
);
991 if (aio_context
!= data
->aio_context
) {
992 aio_context_release(aio_context
);
995 aio_context_release(data
->aio_context
);
1000 void block_job_defer_to_main_loop(BlockJob
*job
,
1001 BlockJobDeferToMainLoopFn
*fn
,
1004 BlockJobDeferToMainLoopData
*data
= g_malloc(sizeof(*data
));
1006 data
->aio_context
= blk_get_aio_context(job
->blk
);
1008 data
->opaque
= opaque
;
1009 job
->deferred_to_main_loop
= true;
1011 aio_bh_schedule_oneshot(qemu_get_aio_context(),
1012 block_job_defer_to_main_loop_bh
, data
);