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 "block/trace.h"
32 #include "sysemu/block-backend.h"
33 #include "qapi/error.h"
34 #include "qapi/qapi-events-block-core.h"
35 #include "qapi/qmp/qerror.h"
36 #include "qemu/coroutine.h"
38 #include "qemu/timer.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 /* BlockJob State Transition Table */
46 bool BlockJobSTT
[BLOCK_JOB_STATUS__MAX
][BLOCK_JOB_STATUS__MAX
] = {
47 /* U, C, R, P, Y, S, W, D, X, E, N */
48 /* U: */ [BLOCK_JOB_STATUS_UNDEFINED
] = {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
49 /* C: */ [BLOCK_JOB_STATUS_CREATED
] = {0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1},
50 /* R: */ [BLOCK_JOB_STATUS_RUNNING
] = {0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0},
51 /* P: */ [BLOCK_JOB_STATUS_PAUSED
] = {0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0},
52 /* Y: */ [BLOCK_JOB_STATUS_READY
] = {0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0},
53 /* S: */ [BLOCK_JOB_STATUS_STANDBY
] = {0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0},
54 /* W: */ [BLOCK_JOB_STATUS_WAITING
] = {0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0},
55 /* D: */ [BLOCK_JOB_STATUS_PENDING
] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0},
56 /* X: */ [BLOCK_JOB_STATUS_ABORTING
] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0},
57 /* E: */ [BLOCK_JOB_STATUS_CONCLUDED
] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
58 /* N: */ [BLOCK_JOB_STATUS_NULL
] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
61 bool BlockJobVerbTable
[BLOCK_JOB_VERB__MAX
][BLOCK_JOB_STATUS__MAX
] = {
62 /* U, C, R, P, Y, S, W, D, X, E, N */
63 [BLOCK_JOB_VERB_CANCEL
] = {0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0},
64 [BLOCK_JOB_VERB_PAUSE
] = {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0},
65 [BLOCK_JOB_VERB_RESUME
] = {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0},
66 [BLOCK_JOB_VERB_SET_SPEED
] = {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0},
67 [BLOCK_JOB_VERB_COMPLETE
] = {0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0},
68 [BLOCK_JOB_VERB_FINALIZE
] = {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0},
69 [BLOCK_JOB_VERB_DISMISS
] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
72 static void block_job_state_transition(BlockJob
*job
, BlockJobStatus s1
)
74 BlockJobStatus s0
= job
->status
;
75 assert(s1
>= 0 && s1
<= BLOCK_JOB_STATUS__MAX
);
76 trace_block_job_state_transition(job
, job
->ret
, BlockJobSTT
[s0
][s1
] ?
77 "allowed" : "disallowed",
78 BlockJobStatus_str(s0
),
79 BlockJobStatus_str(s1
));
80 assert(BlockJobSTT
[s0
][s1
]);
84 static int block_job_apply_verb(BlockJob
*job
, BlockJobVerb bv
, Error
**errp
)
86 assert(bv
>= 0 && bv
<= BLOCK_JOB_VERB__MAX
);
87 trace_block_job_apply_verb(job
, BlockJobStatus_str(job
->status
),
89 BlockJobVerbTable
[bv
][job
->status
] ?
90 "allowed" : "prohibited");
91 if (BlockJobVerbTable
[bv
][job
->status
]) {
94 error_setg(errp
, "Job '%s' in state '%s' cannot accept command verb '%s'",
95 job
->id
, BlockJobStatus_str(job
->status
), BlockJobVerb_str(bv
));
99 static void block_job_lock(void)
101 qemu_mutex_lock(&block_job_mutex
);
104 static void block_job_unlock(void)
106 qemu_mutex_unlock(&block_job_mutex
);
109 static void __attribute__((__constructor__
)) block_job_init(void)
111 qemu_mutex_init(&block_job_mutex
);
114 static void block_job_event_cancelled(BlockJob
*job
);
115 static void block_job_event_completed(BlockJob
*job
, const char *msg
);
116 static int block_job_event_pending(BlockJob
*job
);
117 static void block_job_enter_cond(BlockJob
*job
, bool(*fn
)(BlockJob
*job
));
119 /* Transactional group of block jobs */
122 /* Is this txn being cancelled? */
126 QLIST_HEAD(, BlockJob
) jobs
;
128 /* Reference count */
132 static QLIST_HEAD(, BlockJob
) block_jobs
= QLIST_HEAD_INITIALIZER(block_jobs
);
135 * The block job API is composed of two categories of functions.
137 * The first includes functions used by the monitor. The monitor is
138 * peculiar in that it accesses the block job list with block_job_get, and
139 * therefore needs consistency across block_job_get and the actual operation
140 * (e.g. block_job_set_speed). The consistency is achieved with
141 * aio_context_acquire/release. These functions are declared in blockjob.h.
143 * The second includes functions used by the block job drivers and sometimes
144 * by the core block layer. These do not care about locking, because the
145 * whole coroutine runs under the AioContext lock, and are declared in
149 BlockJob
*block_job_next(BlockJob
*job
)
152 return QLIST_FIRST(&block_jobs
);
154 return QLIST_NEXT(job
, job_list
);
157 BlockJob
*block_job_get(const char *id
)
161 QLIST_FOREACH(job
, &block_jobs
, job_list
) {
162 if (job
->id
&& !strcmp(id
, job
->id
)) {
170 BlockJobTxn
*block_job_txn_new(void)
172 BlockJobTxn
*txn
= g_new0(BlockJobTxn
, 1);
173 QLIST_INIT(&txn
->jobs
);
178 static void block_job_txn_ref(BlockJobTxn
*txn
)
183 void block_job_txn_unref(BlockJobTxn
*txn
)
185 if (txn
&& --txn
->refcnt
== 0) {
190 void block_job_txn_add_job(BlockJobTxn
*txn
, BlockJob
*job
)
199 QLIST_INSERT_HEAD(&txn
->jobs
, job
, txn_list
);
200 block_job_txn_ref(txn
);
203 static void block_job_txn_del_job(BlockJob
*job
)
206 QLIST_REMOVE(job
, txn_list
);
207 block_job_txn_unref(job
->txn
);
212 static void block_job_pause(BlockJob
*job
)
217 static void block_job_resume(BlockJob
*job
)
219 assert(job
->pause_count
> 0);
221 if (job
->pause_count
) {
224 block_job_enter(job
);
227 void block_job_ref(BlockJob
*job
)
232 static void block_job_attached_aio_context(AioContext
*new_context
,
234 static void block_job_detach_aio_context(void *opaque
);
236 void block_job_unref(BlockJob
*job
)
238 if (--job
->refcnt
== 0) {
239 assert(job
->status
== BLOCK_JOB_STATUS_NULL
);
241 BlockDriverState
*bs
= blk_bs(job
->blk
);
242 QLIST_REMOVE(job
, job_list
);
244 block_job_remove_all_bdrv(job
);
245 blk_remove_aio_context_notifier(job
->blk
,
246 block_job_attached_aio_context
,
247 block_job_detach_aio_context
, job
);
249 error_free(job
->blocker
);
251 assert(!timer_pending(&job
->sleep_timer
));
256 static void block_job_attached_aio_context(AioContext
*new_context
,
259 BlockJob
*job
= opaque
;
261 if (job
->driver
->attached_aio_context
) {
262 job
->driver
->attached_aio_context(job
, new_context
);
265 block_job_resume(job
);
268 static void block_job_drain(BlockJob
*job
)
270 /* If job is !job->busy this kicks it into the next pause point. */
271 block_job_enter(job
);
274 if (job
->driver
->drain
) {
275 job
->driver
->drain(job
);
279 static void block_job_detach_aio_context(void *opaque
)
281 BlockJob
*job
= opaque
;
283 /* In case the job terminates during aio_poll()... */
286 block_job_pause(job
);
288 while (!job
->paused
&& !job
->completed
) {
289 block_job_drain(job
);
292 block_job_unref(job
);
295 static char *child_job_get_parent_desc(BdrvChild
*c
)
297 BlockJob
*job
= c
->opaque
;
298 return g_strdup_printf("%s job '%s'",
299 BlockJobType_str(job
->driver
->job_type
),
303 static void child_job_drained_begin(BdrvChild
*c
)
305 BlockJob
*job
= c
->opaque
;
306 block_job_pause(job
);
309 static void child_job_drained_end(BdrvChild
*c
)
311 BlockJob
*job
= c
->opaque
;
312 block_job_resume(job
);
315 static const BdrvChildRole child_job
= {
316 .get_parent_desc
= child_job_get_parent_desc
,
317 .drained_begin
= child_job_drained_begin
,
318 .drained_end
= child_job_drained_end
,
319 .stay_at_node
= true,
322 void block_job_remove_all_bdrv(BlockJob
*job
)
325 for (l
= job
->nodes
; l
; l
= l
->next
) {
326 BdrvChild
*c
= l
->data
;
327 bdrv_op_unblock_all(c
->bs
, job
->blocker
);
328 bdrv_root_unref_child(c
);
330 g_slist_free(job
->nodes
);
334 int block_job_add_bdrv(BlockJob
*job
, const char *name
, BlockDriverState
*bs
,
335 uint64_t perm
, uint64_t shared_perm
, Error
**errp
)
339 c
= bdrv_root_attach_child(bs
, name
, &child_job
, perm
, shared_perm
,
345 job
->nodes
= g_slist_prepend(job
->nodes
, c
);
347 bdrv_op_block_all(bs
, job
->blocker
);
352 bool block_job_is_internal(BlockJob
*job
)
354 return (job
->id
== NULL
);
357 static bool block_job_started(BlockJob
*job
)
363 * All jobs must allow a pause point before entering their job proper. This
364 * ensures that jobs can be paused prior to being started, then resumed later.
366 static void coroutine_fn
block_job_co_entry(void *opaque
)
368 BlockJob
*job
= opaque
;
370 assert(job
&& job
->driver
&& job
->driver
->start
);
371 block_job_pause_point(job
);
372 job
->driver
->start(job
);
375 static void block_job_sleep_timer_cb(void *opaque
)
377 BlockJob
*job
= opaque
;
379 block_job_enter(job
);
382 void block_job_start(BlockJob
*job
)
384 assert(job
&& !block_job_started(job
) && job
->paused
&&
385 job
->driver
&& job
->driver
->start
);
386 job
->co
= qemu_coroutine_create(block_job_co_entry
, job
);
390 block_job_state_transition(job
, BLOCK_JOB_STATUS_RUNNING
);
391 bdrv_coroutine_enter(blk_bs(job
->blk
), job
->co
);
394 static void block_job_decommission(BlockJob
*job
)
397 job
->completed
= true;
400 job
->deferred_to_main_loop
= true;
401 block_job_txn_del_job(job
);
402 block_job_state_transition(job
, BLOCK_JOB_STATUS_NULL
);
403 block_job_unref(job
);
406 static void block_job_do_dismiss(BlockJob
*job
)
408 block_job_decommission(job
);
411 static void block_job_conclude(BlockJob
*job
)
413 block_job_state_transition(job
, BLOCK_JOB_STATUS_CONCLUDED
);
414 if (job
->auto_dismiss
|| !block_job_started(job
)) {
415 block_job_do_dismiss(job
);
419 static void block_job_update_rc(BlockJob
*job
)
421 if (!job
->ret
&& block_job_is_cancelled(job
)) {
422 job
->ret
= -ECANCELED
;
425 block_job_state_transition(job
, BLOCK_JOB_STATUS_ABORTING
);
429 static int block_job_prepare(BlockJob
*job
)
431 if (job
->ret
== 0 && job
->driver
->prepare
) {
432 job
->ret
= job
->driver
->prepare(job
);
437 static void block_job_commit(BlockJob
*job
)
440 if (job
->driver
->commit
) {
441 job
->driver
->commit(job
);
445 static void block_job_abort(BlockJob
*job
)
448 if (job
->driver
->abort
) {
449 job
->driver
->abort(job
);
453 static void block_job_clean(BlockJob
*job
)
455 if (job
->driver
->clean
) {
456 job
->driver
->clean(job
);
460 static int block_job_finalize_single(BlockJob
*job
)
462 assert(job
->completed
);
464 /* Ensure abort is called for late-transactional failures */
465 block_job_update_rc(job
);
468 block_job_commit(job
);
470 block_job_abort(job
);
472 block_job_clean(job
);
475 job
->cb(job
->opaque
, job
->ret
);
478 /* Emit events only if we actually started */
479 if (block_job_started(job
)) {
480 if (block_job_is_cancelled(job
)) {
481 block_job_event_cancelled(job
);
483 const char *msg
= NULL
;
485 msg
= strerror(-job
->ret
);
487 block_job_event_completed(job
, msg
);
491 block_job_txn_del_job(job
);
492 block_job_conclude(job
);
496 static void block_job_cancel_async(BlockJob
*job
, bool force
)
498 if (job
->iostatus
!= BLOCK_DEVICE_IO_STATUS_OK
) {
499 block_job_iostatus_reset(job
);
501 if (job
->user_paused
) {
502 /* Do not call block_job_enter here, the caller will handle it. */
503 job
->user_paused
= false;
506 job
->cancelled
= true;
507 /* To prevent 'force == false' overriding a previous 'force == true' */
511 static int block_job_txn_apply(BlockJobTxn
*txn
, int fn(BlockJob
*), bool lock
)
514 BlockJob
*job
, *next
;
517 QLIST_FOREACH_SAFE(job
, &txn
->jobs
, txn_list
, next
) {
519 ctx
= blk_get_aio_context(job
->blk
);
520 aio_context_acquire(ctx
);
524 aio_context_release(ctx
);
533 static int block_job_finish_sync(BlockJob
*job
,
534 void (*finish
)(BlockJob
*, Error
**errp
),
537 Error
*local_err
= NULL
;
540 assert(blk_bs(job
->blk
)->job
== job
);
545 finish(job
, &local_err
);
548 error_propagate(errp
, local_err
);
549 block_job_unref(job
);
552 /* block_job_drain calls block_job_enter, and it should be enough to
553 * induce progress until the job completes or moves to the main thread.
555 while (!job
->deferred_to_main_loop
&& !job
->completed
) {
556 block_job_drain(job
);
558 while (!job
->completed
) {
559 aio_poll(qemu_get_aio_context(), true);
561 ret
= (job
->cancelled
&& job
->ret
== 0) ? -ECANCELED
: job
->ret
;
562 block_job_unref(job
);
566 static void block_job_completed_txn_abort(BlockJob
*job
)
569 BlockJobTxn
*txn
= job
->txn
;
574 * We are cancelled by another job, which will handle everything.
578 txn
->aborting
= true;
579 block_job_txn_ref(txn
);
581 /* We are the first failed job. Cancel other jobs. */
582 QLIST_FOREACH(other_job
, &txn
->jobs
, txn_list
) {
583 ctx
= blk_get_aio_context(other_job
->blk
);
584 aio_context_acquire(ctx
);
587 /* Other jobs are effectively cancelled by us, set the status for
588 * them; this job, however, may or may not be cancelled, depending
589 * on the caller, so leave it. */
590 QLIST_FOREACH(other_job
, &txn
->jobs
, txn_list
) {
591 if (other_job
!= job
) {
592 block_job_cancel_async(other_job
, false);
595 while (!QLIST_EMPTY(&txn
->jobs
)) {
596 other_job
= QLIST_FIRST(&txn
->jobs
);
597 ctx
= blk_get_aio_context(other_job
->blk
);
598 if (!other_job
->completed
) {
599 assert(other_job
->cancelled
);
600 block_job_finish_sync(other_job
, NULL
, NULL
);
602 block_job_finalize_single(other_job
);
603 aio_context_release(ctx
);
606 block_job_txn_unref(txn
);
609 static int block_job_needs_finalize(BlockJob
*job
)
611 return !job
->auto_finalize
;
614 static void block_job_do_finalize(BlockJob
*job
)
617 assert(job
&& job
->txn
);
619 /* prepare the transaction to complete */
620 rc
= block_job_txn_apply(job
->txn
, block_job_prepare
, true);
622 block_job_completed_txn_abort(job
);
624 block_job_txn_apply(job
->txn
, block_job_finalize_single
, true);
628 static void block_job_completed_txn_success(BlockJob
*job
)
630 BlockJobTxn
*txn
= job
->txn
;
633 block_job_state_transition(job
, BLOCK_JOB_STATUS_WAITING
);
636 * Successful completion, see if there are other running jobs in this
639 QLIST_FOREACH(other_job
, &txn
->jobs
, txn_list
) {
640 if (!other_job
->completed
) {
643 assert(other_job
->ret
== 0);
646 block_job_txn_apply(txn
, block_job_event_pending
, false);
648 /* If no jobs need manual finalization, automatically do so */
649 if (block_job_txn_apply(txn
, block_job_needs_finalize
, false) == 0) {
650 block_job_do_finalize(job
);
654 /* Assumes the block_job_mutex is held */
655 static bool block_job_timer_pending(BlockJob
*job
)
657 return timer_pending(&job
->sleep_timer
);
660 void block_job_set_speed(BlockJob
*job
, int64_t speed
, Error
**errp
)
662 Error
*local_err
= NULL
;
663 int64_t old_speed
= job
->speed
;
665 if (!job
->driver
->set_speed
) {
666 error_setg(errp
, QERR_UNSUPPORTED
);
669 if (block_job_apply_verb(job
, BLOCK_JOB_VERB_SET_SPEED
, errp
)) {
672 job
->driver
->set_speed(job
, speed
, &local_err
);
674 error_propagate(errp
, local_err
);
679 if (speed
&& speed
<= old_speed
) {
683 /* kick only if a timer is pending */
684 block_job_enter_cond(job
, block_job_timer_pending
);
687 void block_job_complete(BlockJob
*job
, Error
**errp
)
689 /* Should not be reachable via external interface for internal jobs */
691 if (block_job_apply_verb(job
, BLOCK_JOB_VERB_COMPLETE
, errp
)) {
694 if (job
->pause_count
|| job
->cancelled
|| !job
->driver
->complete
) {
695 error_setg(errp
, "The active block job '%s' cannot be completed",
700 job
->driver
->complete(job
, errp
);
703 void block_job_finalize(BlockJob
*job
, Error
**errp
)
705 assert(job
&& job
->id
&& job
->txn
);
706 if (block_job_apply_verb(job
, BLOCK_JOB_VERB_FINALIZE
, errp
)) {
709 block_job_do_finalize(job
);
712 void block_job_dismiss(BlockJob
**jobptr
, Error
**errp
)
714 BlockJob
*job
= *jobptr
;
715 /* similarly to _complete, this is QMP-interface only. */
717 if (block_job_apply_verb(job
, BLOCK_JOB_VERB_DISMISS
, errp
)) {
721 block_job_do_dismiss(job
);
725 void block_job_user_pause(BlockJob
*job
, Error
**errp
)
727 if (block_job_apply_verb(job
, BLOCK_JOB_VERB_PAUSE
, errp
)) {
730 if (job
->user_paused
) {
731 error_setg(errp
, "Job is already paused");
734 job
->user_paused
= true;
735 block_job_pause(job
);
738 bool block_job_user_paused(BlockJob
*job
)
740 return job
->user_paused
;
743 void block_job_user_resume(BlockJob
*job
, Error
**errp
)
746 if (!job
->user_paused
|| job
->pause_count
<= 0) {
747 error_setg(errp
, "Can't resume a job that was not paused");
750 if (block_job_apply_verb(job
, BLOCK_JOB_VERB_RESUME
, errp
)) {
753 block_job_iostatus_reset(job
);
754 job
->user_paused
= false;
755 block_job_resume(job
);
758 void block_job_cancel(BlockJob
*job
, bool force
)
760 if (job
->status
== BLOCK_JOB_STATUS_CONCLUDED
) {
761 block_job_do_dismiss(job
);
764 block_job_cancel_async(job
, force
);
765 if (!block_job_started(job
)) {
766 block_job_completed(job
, -ECANCELED
);
767 } else if (job
->deferred_to_main_loop
) {
768 block_job_completed_txn_abort(job
);
770 block_job_enter(job
);
774 void block_job_user_cancel(BlockJob
*job
, bool force
, Error
**errp
)
776 if (block_job_apply_verb(job
, BLOCK_JOB_VERB_CANCEL
, errp
)) {
779 block_job_cancel(job
, force
);
782 /* A wrapper around block_job_cancel() taking an Error ** parameter so it may be
783 * used with block_job_finish_sync() without the need for (rather nasty)
784 * function pointer casts there. */
785 static void block_job_cancel_err(BlockJob
*job
, Error
**errp
)
787 block_job_cancel(job
, false);
790 int block_job_cancel_sync(BlockJob
*job
)
792 return block_job_finish_sync(job
, &block_job_cancel_err
, NULL
);
795 void block_job_cancel_sync_all(void)
798 AioContext
*aio_context
;
800 while ((job
= QLIST_FIRST(&block_jobs
))) {
801 aio_context
= blk_get_aio_context(job
->blk
);
802 aio_context_acquire(aio_context
);
803 block_job_cancel_sync(job
);
804 aio_context_release(aio_context
);
808 int block_job_complete_sync(BlockJob
*job
, Error
**errp
)
810 return block_job_finish_sync(job
, &block_job_complete
, errp
);
813 BlockJobInfo
*block_job_query(BlockJob
*job
, Error
**errp
)
817 if (block_job_is_internal(job
)) {
818 error_setg(errp
, "Cannot query QEMU internal jobs");
821 info
= g_new0(BlockJobInfo
, 1);
822 info
->type
= g_strdup(BlockJobType_str(job
->driver
->job_type
));
823 info
->device
= g_strdup(job
->id
);
824 info
->len
= job
->len
;
825 info
->busy
= atomic_read(&job
->busy
);
826 info
->paused
= job
->pause_count
> 0;
827 info
->offset
= job
->offset
;
828 info
->speed
= job
->speed
;
829 info
->io_status
= job
->iostatus
;
830 info
->ready
= job
->ready
;
831 info
->status
= job
->status
;
832 info
->auto_finalize
= job
->auto_finalize
;
833 info
->auto_dismiss
= job
->auto_dismiss
;
837 static void block_job_iostatus_set_err(BlockJob
*job
, int error
)
839 if (job
->iostatus
== BLOCK_DEVICE_IO_STATUS_OK
) {
840 job
->iostatus
= error
== ENOSPC
? BLOCK_DEVICE_IO_STATUS_NOSPACE
:
841 BLOCK_DEVICE_IO_STATUS_FAILED
;
845 static void block_job_event_cancelled(BlockJob
*job
)
847 if (block_job_is_internal(job
)) {
851 qapi_event_send_block_job_cancelled(job
->driver
->job_type
,
859 static void block_job_event_completed(BlockJob
*job
, const char *msg
)
861 if (block_job_is_internal(job
)) {
865 qapi_event_send_block_job_completed(job
->driver
->job_type
,
875 static int block_job_event_pending(BlockJob
*job
)
877 block_job_state_transition(job
, BLOCK_JOB_STATUS_PENDING
);
878 if (!job
->auto_finalize
&& !block_job_is_internal(job
)) {
879 qapi_event_send_block_job_pending(job
->driver
->job_type
,
887 * API for block job drivers and the block layer. These functions are
888 * declared in blockjob_int.h.
891 void *block_job_create(const char *job_id
, const BlockJobDriver
*driver
,
892 BlockJobTxn
*txn
, BlockDriverState
*bs
, uint64_t perm
,
893 uint64_t shared_perm
, int64_t speed
, int flags
,
894 BlockCompletionFunc
*cb
, void *opaque
, Error
**errp
)
901 error_setg(errp
, QERR_DEVICE_IN_USE
, bdrv_get_device_name(bs
));
905 if (job_id
== NULL
&& !(flags
& BLOCK_JOB_INTERNAL
)) {
906 job_id
= bdrv_get_device_name(bs
);
908 error_setg(errp
, "An explicit job ID is required for this node");
914 if (flags
& BLOCK_JOB_INTERNAL
) {
915 error_setg(errp
, "Cannot specify job ID for internal block job");
919 if (!id_wellformed(job_id
)) {
920 error_setg(errp
, "Invalid job ID '%s'", job_id
);
924 if (block_job_get(job_id
)) {
925 error_setg(errp
, "Job ID '%s' already in use", job_id
);
930 blk
= blk_new(perm
, shared_perm
);
931 ret
= blk_insert_bs(blk
, bs
, errp
);
937 job
= g_malloc0(driver
->instance_size
);
938 job
->driver
= driver
;
939 job
->id
= g_strdup(job_id
);
942 job
->opaque
= opaque
;
945 job
->pause_count
= 1;
947 job
->auto_finalize
= !(flags
& BLOCK_JOB_MANUAL_FINALIZE
);
948 job
->auto_dismiss
= !(flags
& BLOCK_JOB_MANUAL_DISMISS
);
949 block_job_state_transition(job
, BLOCK_JOB_STATUS_CREATED
);
950 aio_timer_init(qemu_get_aio_context(), &job
->sleep_timer
,
951 QEMU_CLOCK_REALTIME
, SCALE_NS
,
952 block_job_sleep_timer_cb
, job
);
954 error_setg(&job
->blocker
, "block device is in use by block job: %s",
955 BlockJobType_str(driver
->job_type
));
956 block_job_add_bdrv(job
, "main node", bs
, 0, BLK_PERM_ALL
, &error_abort
);
959 bdrv_op_unblock(bs
, BLOCK_OP_TYPE_DATAPLANE
, job
->blocker
);
961 QLIST_INSERT_HEAD(&block_jobs
, job
, job_list
);
963 blk_add_aio_context_notifier(blk
, block_job_attached_aio_context
,
964 block_job_detach_aio_context
, job
);
966 /* Only set speed when necessary to avoid NotSupported error */
968 Error
*local_err
= NULL
;
970 block_job_set_speed(job
, speed
, &local_err
);
972 block_job_early_fail(job
);
973 error_propagate(errp
, local_err
);
978 /* Single jobs are modeled as single-job transactions for sake of
979 * consolidating the job management logic */
981 txn
= block_job_txn_new();
982 block_job_txn_add_job(txn
, job
);
983 block_job_txn_unref(txn
);
985 block_job_txn_add_job(txn
, job
);
991 void block_job_pause_all(void)
993 BlockJob
*job
= NULL
;
994 while ((job
= block_job_next(job
))) {
995 AioContext
*aio_context
= blk_get_aio_context(job
->blk
);
997 aio_context_acquire(aio_context
);
999 block_job_pause(job
);
1000 aio_context_release(aio_context
);
1004 void block_job_early_fail(BlockJob
*job
)
1006 assert(job
->status
== BLOCK_JOB_STATUS_CREATED
);
1007 block_job_decommission(job
);
1010 void block_job_completed(BlockJob
*job
, int ret
)
1012 assert(job
&& job
->txn
&& !job
->completed
);
1013 assert(blk_bs(job
->blk
)->job
== job
);
1014 job
->completed
= true;
1016 block_job_update_rc(job
);
1017 trace_block_job_completed(job
, ret
, job
->ret
);
1019 block_job_completed_txn_abort(job
);
1021 block_job_completed_txn_success(job
);
1025 static bool block_job_should_pause(BlockJob
*job
)
1027 return job
->pause_count
> 0;
1030 /* Yield, and schedule a timer to reenter the coroutine after @ns nanoseconds.
1031 * Reentering the job coroutine with block_job_enter() before the timer has
1032 * expired is allowed and cancels the timer.
1034 * If @ns is (uint64_t) -1, no timer is scheduled and block_job_enter() must be
1035 * called explicitly. */
1036 static void block_job_do_yield(BlockJob
*job
, uint64_t ns
)
1040 timer_mod(&job
->sleep_timer
, ns
);
1044 qemu_coroutine_yield();
1046 /* Set by block_job_enter before re-entering the coroutine. */
1050 void coroutine_fn
block_job_pause_point(BlockJob
*job
)
1052 assert(job
&& block_job_started(job
));
1054 if (!block_job_should_pause(job
)) {
1057 if (block_job_is_cancelled(job
)) {
1061 if (job
->driver
->pause
) {
1062 job
->driver
->pause(job
);
1065 if (block_job_should_pause(job
) && !block_job_is_cancelled(job
)) {
1066 BlockJobStatus status
= job
->status
;
1067 block_job_state_transition(job
, status
== BLOCK_JOB_STATUS_READY
? \
1068 BLOCK_JOB_STATUS_STANDBY
: \
1069 BLOCK_JOB_STATUS_PAUSED
);
1071 block_job_do_yield(job
, -1);
1072 job
->paused
= false;
1073 block_job_state_transition(job
, status
);
1076 if (job
->driver
->resume
) {
1077 job
->driver
->resume(job
);
1081 void block_job_resume_all(void)
1083 BlockJob
*job
, *next
;
1085 QLIST_FOREACH_SAFE(job
, &block_jobs
, job_list
, next
) {
1086 AioContext
*aio_context
= blk_get_aio_context(job
->blk
);
1088 aio_context_acquire(aio_context
);
1089 block_job_resume(job
);
1090 block_job_unref(job
);
1091 aio_context_release(aio_context
);
1096 * Conditionally enter a block_job pending a call to fn() while
1097 * under the block_job_lock critical section.
1099 static void block_job_enter_cond(BlockJob
*job
, bool(*fn
)(BlockJob
*job
))
1101 if (!block_job_started(job
)) {
1104 if (job
->deferred_to_main_loop
) {
1114 if (fn
&& !fn(job
)) {
1119 assert(!job
->deferred_to_main_loop
);
1120 timer_del(&job
->sleep_timer
);
1123 aio_co_wake(job
->co
);
1126 void block_job_enter(BlockJob
*job
)
1128 block_job_enter_cond(job
, NULL
);
1131 bool block_job_is_cancelled(BlockJob
*job
)
1133 return job
->cancelled
;
1136 void block_job_sleep_ns(BlockJob
*job
, int64_t ns
)
1140 /* Check cancellation *before* setting busy = false, too! */
1141 if (block_job_is_cancelled(job
)) {
1145 if (!block_job_should_pause(job
)) {
1146 block_job_do_yield(job
, qemu_clock_get_ns(QEMU_CLOCK_REALTIME
) + ns
);
1149 block_job_pause_point(job
);
1152 void block_job_yield(BlockJob
*job
)
1156 /* Check cancellation *before* setting busy = false, too! */
1157 if (block_job_is_cancelled(job
)) {
1161 if (!block_job_should_pause(job
)) {
1162 block_job_do_yield(job
, -1);
1165 block_job_pause_point(job
);
1168 void block_job_iostatus_reset(BlockJob
*job
)
1170 if (job
->iostatus
== BLOCK_DEVICE_IO_STATUS_OK
) {
1173 assert(job
->user_paused
&& job
->pause_count
> 0);
1174 job
->iostatus
= BLOCK_DEVICE_IO_STATUS_OK
;
1177 void block_job_event_ready(BlockJob
*job
)
1179 block_job_state_transition(job
, BLOCK_JOB_STATUS_READY
);
1182 if (block_job_is_internal(job
)) {
1186 qapi_event_send_block_job_ready(job
->driver
->job_type
,
1190 job
->speed
, &error_abort
);
1193 BlockErrorAction
block_job_error_action(BlockJob
*job
, BlockdevOnError on_err
,
1194 int is_read
, int error
)
1196 BlockErrorAction action
;
1199 case BLOCKDEV_ON_ERROR_ENOSPC
:
1200 case BLOCKDEV_ON_ERROR_AUTO
:
1201 action
= (error
== ENOSPC
) ?
1202 BLOCK_ERROR_ACTION_STOP
: BLOCK_ERROR_ACTION_REPORT
;
1204 case BLOCKDEV_ON_ERROR_STOP
:
1205 action
= BLOCK_ERROR_ACTION_STOP
;
1207 case BLOCKDEV_ON_ERROR_REPORT
:
1208 action
= BLOCK_ERROR_ACTION_REPORT
;
1210 case BLOCKDEV_ON_ERROR_IGNORE
:
1211 action
= BLOCK_ERROR_ACTION_IGNORE
;
1216 if (!block_job_is_internal(job
)) {
1217 qapi_event_send_block_job_error(job
->id
,
1218 is_read
? IO_OPERATION_TYPE_READ
:
1219 IO_OPERATION_TYPE_WRITE
,
1220 action
, &error_abort
);
1222 if (action
== BLOCK_ERROR_ACTION_STOP
) {
1223 block_job_pause(job
);
1224 /* make the pause user visible, which will be resumed from QMP. */
1225 job
->user_paused
= true;
1226 block_job_iostatus_set_err(job
, error
);
1233 AioContext
*aio_context
;
1234 BlockJobDeferToMainLoopFn
*fn
;
1236 } BlockJobDeferToMainLoopData
;
1238 static void block_job_defer_to_main_loop_bh(void *opaque
)
1240 BlockJobDeferToMainLoopData
*data
= opaque
;
1241 AioContext
*aio_context
;
1243 /* Prevent race with block_job_defer_to_main_loop() */
1244 aio_context_acquire(data
->aio_context
);
1246 /* Fetch BDS AioContext again, in case it has changed */
1247 aio_context
= blk_get_aio_context(data
->job
->blk
);
1248 if (aio_context
!= data
->aio_context
) {
1249 aio_context_acquire(aio_context
);
1252 data
->fn(data
->job
, data
->opaque
);
1254 if (aio_context
!= data
->aio_context
) {
1255 aio_context_release(aio_context
);
1258 aio_context_release(data
->aio_context
);
1263 void block_job_defer_to_main_loop(BlockJob
*job
,
1264 BlockJobDeferToMainLoopFn
*fn
,
1267 BlockJobDeferToMainLoopData
*data
= g_malloc(sizeof(*data
));
1269 data
->aio_context
= blk_get_aio_context(job
->blk
);
1271 data
->opaque
= opaque
;
1272 job
->deferred_to_main_loop
= true;
1274 aio_bh_schedule_oneshot(qemu_get_aio_context(),
1275 block_job_defer_to_main_loop_bh
, data
);