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
)
362 const BlockJobDriver
*block_job_driver(BlockJob
*job
)
368 * All jobs must allow a pause point before entering their job proper. This
369 * ensures that jobs can be paused prior to being started, then resumed later.
371 static void coroutine_fn
block_job_co_entry(void *opaque
)
373 BlockJob
*job
= opaque
;
375 assert(job
&& job
->driver
&& job
->driver
->start
);
376 block_job_pause_point(job
);
377 job
->driver
->start(job
);
380 static void block_job_sleep_timer_cb(void *opaque
)
382 BlockJob
*job
= opaque
;
384 block_job_enter(job
);
387 void block_job_start(BlockJob
*job
)
389 assert(job
&& !block_job_started(job
) && job
->paused
&&
390 job
->driver
&& job
->driver
->start
);
391 job
->co
= qemu_coroutine_create(block_job_co_entry
, job
);
395 block_job_state_transition(job
, BLOCK_JOB_STATUS_RUNNING
);
396 bdrv_coroutine_enter(blk_bs(job
->blk
), job
->co
);
399 static void block_job_decommission(BlockJob
*job
)
402 job
->completed
= true;
405 job
->deferred_to_main_loop
= true;
406 block_job_txn_del_job(job
);
407 block_job_state_transition(job
, BLOCK_JOB_STATUS_NULL
);
408 block_job_unref(job
);
411 static void block_job_do_dismiss(BlockJob
*job
)
413 block_job_decommission(job
);
416 static void block_job_conclude(BlockJob
*job
)
418 block_job_state_transition(job
, BLOCK_JOB_STATUS_CONCLUDED
);
419 if (job
->auto_dismiss
|| !block_job_started(job
)) {
420 block_job_do_dismiss(job
);
424 static void block_job_update_rc(BlockJob
*job
)
426 if (!job
->ret
&& block_job_is_cancelled(job
)) {
427 job
->ret
= -ECANCELED
;
430 block_job_state_transition(job
, BLOCK_JOB_STATUS_ABORTING
);
434 static int block_job_prepare(BlockJob
*job
)
436 if (job
->ret
== 0 && job
->driver
->prepare
) {
437 job
->ret
= job
->driver
->prepare(job
);
442 static void block_job_commit(BlockJob
*job
)
445 if (job
->driver
->commit
) {
446 job
->driver
->commit(job
);
450 static void block_job_abort(BlockJob
*job
)
453 if (job
->driver
->abort
) {
454 job
->driver
->abort(job
);
458 static void block_job_clean(BlockJob
*job
)
460 if (job
->driver
->clean
) {
461 job
->driver
->clean(job
);
465 static int block_job_finalize_single(BlockJob
*job
)
467 assert(job
->completed
);
469 /* Ensure abort is called for late-transactional failures */
470 block_job_update_rc(job
);
473 block_job_commit(job
);
475 block_job_abort(job
);
477 block_job_clean(job
);
480 job
->cb(job
->opaque
, job
->ret
);
483 /* Emit events only if we actually started */
484 if (block_job_started(job
)) {
485 if (block_job_is_cancelled(job
)) {
486 block_job_event_cancelled(job
);
488 const char *msg
= NULL
;
490 msg
= strerror(-job
->ret
);
492 block_job_event_completed(job
, msg
);
496 block_job_txn_del_job(job
);
497 block_job_conclude(job
);
501 static void block_job_cancel_async(BlockJob
*job
, bool force
)
503 if (job
->iostatus
!= BLOCK_DEVICE_IO_STATUS_OK
) {
504 block_job_iostatus_reset(job
);
506 if (job
->user_paused
) {
507 /* Do not call block_job_enter here, the caller will handle it. */
508 job
->user_paused
= false;
511 job
->cancelled
= true;
512 /* To prevent 'force == false' overriding a previous 'force == true' */
516 static int block_job_txn_apply(BlockJobTxn
*txn
, int fn(BlockJob
*), bool lock
)
519 BlockJob
*job
, *next
;
522 QLIST_FOREACH_SAFE(job
, &txn
->jobs
, txn_list
, next
) {
524 ctx
= blk_get_aio_context(job
->blk
);
525 aio_context_acquire(ctx
);
529 aio_context_release(ctx
);
538 static int block_job_finish_sync(BlockJob
*job
,
539 void (*finish
)(BlockJob
*, Error
**errp
),
542 Error
*local_err
= NULL
;
545 assert(blk_bs(job
->blk
)->job
== job
);
550 finish(job
, &local_err
);
553 error_propagate(errp
, local_err
);
554 block_job_unref(job
);
557 /* block_job_drain calls block_job_enter, and it should be enough to
558 * induce progress until the job completes or moves to the main thread.
560 while (!job
->deferred_to_main_loop
&& !job
->completed
) {
561 block_job_drain(job
);
563 while (!job
->completed
) {
564 aio_poll(qemu_get_aio_context(), true);
566 ret
= (job
->cancelled
&& job
->ret
== 0) ? -ECANCELED
: job
->ret
;
567 block_job_unref(job
);
571 static void block_job_completed_txn_abort(BlockJob
*job
)
574 BlockJobTxn
*txn
= job
->txn
;
579 * We are cancelled by another job, which will handle everything.
583 txn
->aborting
= true;
584 block_job_txn_ref(txn
);
586 /* We are the first failed job. Cancel other jobs. */
587 QLIST_FOREACH(other_job
, &txn
->jobs
, txn_list
) {
588 ctx
= blk_get_aio_context(other_job
->blk
);
589 aio_context_acquire(ctx
);
592 /* Other jobs are effectively cancelled by us, set the status for
593 * them; this job, however, may or may not be cancelled, depending
594 * on the caller, so leave it. */
595 QLIST_FOREACH(other_job
, &txn
->jobs
, txn_list
) {
596 if (other_job
!= job
) {
597 block_job_cancel_async(other_job
, false);
600 while (!QLIST_EMPTY(&txn
->jobs
)) {
601 other_job
= QLIST_FIRST(&txn
->jobs
);
602 ctx
= blk_get_aio_context(other_job
->blk
);
603 if (!other_job
->completed
) {
604 assert(other_job
->cancelled
);
605 block_job_finish_sync(other_job
, NULL
, NULL
);
607 block_job_finalize_single(other_job
);
608 aio_context_release(ctx
);
611 block_job_txn_unref(txn
);
614 static int block_job_needs_finalize(BlockJob
*job
)
616 return !job
->auto_finalize
;
619 static void block_job_do_finalize(BlockJob
*job
)
622 assert(job
&& job
->txn
);
624 /* prepare the transaction to complete */
625 rc
= block_job_txn_apply(job
->txn
, block_job_prepare
, true);
627 block_job_completed_txn_abort(job
);
629 block_job_txn_apply(job
->txn
, block_job_finalize_single
, true);
633 static void block_job_completed_txn_success(BlockJob
*job
)
635 BlockJobTxn
*txn
= job
->txn
;
638 block_job_state_transition(job
, BLOCK_JOB_STATUS_WAITING
);
641 * Successful completion, see if there are other running jobs in this
644 QLIST_FOREACH(other_job
, &txn
->jobs
, txn_list
) {
645 if (!other_job
->completed
) {
648 assert(other_job
->ret
== 0);
651 block_job_txn_apply(txn
, block_job_event_pending
, false);
653 /* If no jobs need manual finalization, automatically do so */
654 if (block_job_txn_apply(txn
, block_job_needs_finalize
, false) == 0) {
655 block_job_do_finalize(job
);
659 /* Assumes the block_job_mutex is held */
660 static bool block_job_timer_pending(BlockJob
*job
)
662 return timer_pending(&job
->sleep_timer
);
665 void block_job_set_speed(BlockJob
*job
, int64_t speed
, Error
**errp
)
667 int64_t old_speed
= job
->speed
;
669 if (block_job_apply_verb(job
, BLOCK_JOB_VERB_SET_SPEED
, errp
)) {
673 error_setg(errp
, QERR_INVALID_PARAMETER
, "speed");
677 ratelimit_set_speed(&job
->limit
, speed
, BLOCK_JOB_SLICE_TIME
);
680 if (speed
&& speed
<= old_speed
) {
684 /* kick only if a timer is pending */
685 block_job_enter_cond(job
, block_job_timer_pending
);
688 int64_t block_job_ratelimit_get_delay(BlockJob
*job
, uint64_t n
)
694 return ratelimit_calculate_delay(&job
->limit
, n
);
697 void block_job_complete(BlockJob
*job
, Error
**errp
)
699 /* Should not be reachable via external interface for internal jobs */
701 if (block_job_apply_verb(job
, BLOCK_JOB_VERB_COMPLETE
, errp
)) {
704 if (job
->pause_count
|| job
->cancelled
|| !job
->driver
->complete
) {
705 error_setg(errp
, "The active block job '%s' cannot be completed",
710 job
->driver
->complete(job
, errp
);
713 void block_job_finalize(BlockJob
*job
, Error
**errp
)
715 assert(job
&& job
->id
);
716 if (block_job_apply_verb(job
, BLOCK_JOB_VERB_FINALIZE
, errp
)) {
719 block_job_do_finalize(job
);
722 void block_job_dismiss(BlockJob
**jobptr
, Error
**errp
)
724 BlockJob
*job
= *jobptr
;
725 /* similarly to _complete, this is QMP-interface only. */
727 if (block_job_apply_verb(job
, BLOCK_JOB_VERB_DISMISS
, errp
)) {
731 block_job_do_dismiss(job
);
735 void block_job_user_pause(BlockJob
*job
, Error
**errp
)
737 if (block_job_apply_verb(job
, BLOCK_JOB_VERB_PAUSE
, errp
)) {
740 if (job
->user_paused
) {
741 error_setg(errp
, "Job is already paused");
744 job
->user_paused
= true;
745 block_job_pause(job
);
748 bool block_job_user_paused(BlockJob
*job
)
750 return job
->user_paused
;
753 void block_job_user_resume(BlockJob
*job
, Error
**errp
)
756 if (!job
->user_paused
|| job
->pause_count
<= 0) {
757 error_setg(errp
, "Can't resume a job that was not paused");
760 if (block_job_apply_verb(job
, BLOCK_JOB_VERB_RESUME
, errp
)) {
763 block_job_iostatus_reset(job
);
764 job
->user_paused
= false;
765 block_job_resume(job
);
768 void block_job_cancel(BlockJob
*job
, bool force
)
770 if (job
->status
== BLOCK_JOB_STATUS_CONCLUDED
) {
771 block_job_do_dismiss(job
);
774 block_job_cancel_async(job
, force
);
775 if (!block_job_started(job
)) {
776 block_job_completed(job
, -ECANCELED
);
777 } else if (job
->deferred_to_main_loop
) {
778 block_job_completed_txn_abort(job
);
780 block_job_enter(job
);
784 void block_job_user_cancel(BlockJob
*job
, bool force
, Error
**errp
)
786 if (block_job_apply_verb(job
, BLOCK_JOB_VERB_CANCEL
, errp
)) {
789 block_job_cancel(job
, force
);
792 /* A wrapper around block_job_cancel() taking an Error ** parameter so it may be
793 * used with block_job_finish_sync() without the need for (rather nasty)
794 * function pointer casts there. */
795 static void block_job_cancel_err(BlockJob
*job
, Error
**errp
)
797 block_job_cancel(job
, false);
800 int block_job_cancel_sync(BlockJob
*job
)
802 return block_job_finish_sync(job
, &block_job_cancel_err
, NULL
);
805 void block_job_cancel_sync_all(void)
808 AioContext
*aio_context
;
810 while ((job
= QLIST_FIRST(&block_jobs
))) {
811 aio_context
= blk_get_aio_context(job
->blk
);
812 aio_context_acquire(aio_context
);
813 block_job_cancel_sync(job
);
814 aio_context_release(aio_context
);
818 int block_job_complete_sync(BlockJob
*job
, Error
**errp
)
820 return block_job_finish_sync(job
, &block_job_complete
, errp
);
823 void block_job_progress_update(BlockJob
*job
, uint64_t done
)
828 void block_job_progress_set_remaining(BlockJob
*job
, uint64_t remaining
)
830 job
->len
= job
->offset
+ remaining
;
833 BlockJobInfo
*block_job_query(BlockJob
*job
, Error
**errp
)
837 if (block_job_is_internal(job
)) {
838 error_setg(errp
, "Cannot query QEMU internal jobs");
841 info
= g_new0(BlockJobInfo
, 1);
842 info
->type
= g_strdup(BlockJobType_str(job
->driver
->job_type
));
843 info
->device
= g_strdup(job
->id
);
844 info
->len
= job
->len
;
845 info
->busy
= atomic_read(&job
->busy
);
846 info
->paused
= job
->pause_count
> 0;
847 info
->offset
= job
->offset
;
848 info
->speed
= job
->speed
;
849 info
->io_status
= job
->iostatus
;
850 info
->ready
= job
->ready
;
851 info
->status
= job
->status
;
852 info
->auto_finalize
= job
->auto_finalize
;
853 info
->auto_dismiss
= job
->auto_dismiss
;
854 info
->has_error
= job
->ret
!= 0;
855 info
->error
= job
->ret
? g_strdup(strerror(-job
->ret
)) : NULL
;
859 static void block_job_iostatus_set_err(BlockJob
*job
, int error
)
861 if (job
->iostatus
== BLOCK_DEVICE_IO_STATUS_OK
) {
862 job
->iostatus
= error
== ENOSPC
? BLOCK_DEVICE_IO_STATUS_NOSPACE
:
863 BLOCK_DEVICE_IO_STATUS_FAILED
;
867 static void block_job_event_cancelled(BlockJob
*job
)
869 if (block_job_is_internal(job
)) {
873 qapi_event_send_block_job_cancelled(job
->driver
->job_type
,
881 static void block_job_event_completed(BlockJob
*job
, const char *msg
)
883 if (block_job_is_internal(job
)) {
887 qapi_event_send_block_job_completed(job
->driver
->job_type
,
897 static int block_job_event_pending(BlockJob
*job
)
899 block_job_state_transition(job
, BLOCK_JOB_STATUS_PENDING
);
900 if (!job
->auto_finalize
&& !block_job_is_internal(job
)) {
901 qapi_event_send_block_job_pending(job
->driver
->job_type
,
909 * API for block job drivers and the block layer. These functions are
910 * declared in blockjob_int.h.
913 void *block_job_create(const char *job_id
, const BlockJobDriver
*driver
,
914 BlockJobTxn
*txn
, BlockDriverState
*bs
, uint64_t perm
,
915 uint64_t shared_perm
, int64_t speed
, int flags
,
916 BlockCompletionFunc
*cb
, void *opaque
, Error
**errp
)
923 error_setg(errp
, QERR_DEVICE_IN_USE
, bdrv_get_device_name(bs
));
927 if (job_id
== NULL
&& !(flags
& BLOCK_JOB_INTERNAL
)) {
928 job_id
= bdrv_get_device_name(bs
);
930 error_setg(errp
, "An explicit job ID is required for this node");
936 if (flags
& BLOCK_JOB_INTERNAL
) {
937 error_setg(errp
, "Cannot specify job ID for internal block job");
941 if (!id_wellformed(job_id
)) {
942 error_setg(errp
, "Invalid job ID '%s'", job_id
);
946 if (block_job_get(job_id
)) {
947 error_setg(errp
, "Job ID '%s' already in use", job_id
);
952 blk
= blk_new(perm
, shared_perm
);
953 ret
= blk_insert_bs(blk
, bs
, errp
);
959 job
= g_malloc0(driver
->instance_size
);
960 job
->driver
= driver
;
961 job
->id
= g_strdup(job_id
);
964 job
->opaque
= opaque
;
967 job
->pause_count
= 1;
969 job
->auto_finalize
= !(flags
& BLOCK_JOB_MANUAL_FINALIZE
);
970 job
->auto_dismiss
= !(flags
& BLOCK_JOB_MANUAL_DISMISS
);
971 block_job_state_transition(job
, BLOCK_JOB_STATUS_CREATED
);
972 aio_timer_init(qemu_get_aio_context(), &job
->sleep_timer
,
973 QEMU_CLOCK_REALTIME
, SCALE_NS
,
974 block_job_sleep_timer_cb
, job
);
976 error_setg(&job
->blocker
, "block device is in use by block job: %s",
977 BlockJobType_str(driver
->job_type
));
978 block_job_add_bdrv(job
, "main node", bs
, 0, BLK_PERM_ALL
, &error_abort
);
981 bdrv_op_unblock(bs
, BLOCK_OP_TYPE_DATAPLANE
, job
->blocker
);
983 QLIST_INSERT_HEAD(&block_jobs
, job
, job_list
);
985 blk_add_aio_context_notifier(blk
, block_job_attached_aio_context
,
986 block_job_detach_aio_context
, job
);
988 /* Only set speed when necessary to avoid NotSupported error */
990 Error
*local_err
= NULL
;
992 block_job_set_speed(job
, speed
, &local_err
);
994 block_job_early_fail(job
);
995 error_propagate(errp
, local_err
);
1000 /* Single jobs are modeled as single-job transactions for sake of
1001 * consolidating the job management logic */
1003 txn
= block_job_txn_new();
1004 block_job_txn_add_job(txn
, job
);
1005 block_job_txn_unref(txn
);
1007 block_job_txn_add_job(txn
, job
);
1013 void block_job_early_fail(BlockJob
*job
)
1015 assert(job
->status
== BLOCK_JOB_STATUS_CREATED
);
1016 block_job_decommission(job
);
1019 void block_job_completed(BlockJob
*job
, int ret
)
1021 assert(job
&& job
->txn
&& !job
->completed
);
1022 assert(blk_bs(job
->blk
)->job
== job
);
1023 job
->completed
= true;
1025 block_job_update_rc(job
);
1026 trace_block_job_completed(job
, ret
, job
->ret
);
1028 block_job_completed_txn_abort(job
);
1030 block_job_completed_txn_success(job
);
1034 static bool block_job_should_pause(BlockJob
*job
)
1036 return job
->pause_count
> 0;
1039 /* Yield, and schedule a timer to reenter the coroutine after @ns nanoseconds.
1040 * Reentering the job coroutine with block_job_enter() before the timer has
1041 * expired is allowed and cancels the timer.
1043 * If @ns is (uint64_t) -1, no timer is scheduled and block_job_enter() must be
1044 * called explicitly. */
1045 static void block_job_do_yield(BlockJob
*job
, uint64_t ns
)
1049 timer_mod(&job
->sleep_timer
, ns
);
1053 qemu_coroutine_yield();
1055 /* Set by block_job_enter before re-entering the coroutine. */
1059 void coroutine_fn
block_job_pause_point(BlockJob
*job
)
1061 assert(job
&& block_job_started(job
));
1063 if (!block_job_should_pause(job
)) {
1066 if (block_job_is_cancelled(job
)) {
1070 if (job
->driver
->pause
) {
1071 job
->driver
->pause(job
);
1074 if (block_job_should_pause(job
) && !block_job_is_cancelled(job
)) {
1075 BlockJobStatus status
= job
->status
;
1076 block_job_state_transition(job
, status
== BLOCK_JOB_STATUS_READY
? \
1077 BLOCK_JOB_STATUS_STANDBY
: \
1078 BLOCK_JOB_STATUS_PAUSED
);
1080 block_job_do_yield(job
, -1);
1081 job
->paused
= false;
1082 block_job_state_transition(job
, status
);
1085 if (job
->driver
->resume
) {
1086 job
->driver
->resume(job
);
1091 * Conditionally enter a block_job pending a call to fn() while
1092 * under the block_job_lock critical section.
1094 static void block_job_enter_cond(BlockJob
*job
, bool(*fn
)(BlockJob
*job
))
1096 if (!block_job_started(job
)) {
1099 if (job
->deferred_to_main_loop
) {
1109 if (fn
&& !fn(job
)) {
1114 assert(!job
->deferred_to_main_loop
);
1115 timer_del(&job
->sleep_timer
);
1118 aio_co_wake(job
->co
);
1121 void block_job_enter(BlockJob
*job
)
1123 block_job_enter_cond(job
, NULL
);
1126 bool block_job_is_cancelled(BlockJob
*job
)
1128 return job
->cancelled
;
1131 void block_job_sleep_ns(BlockJob
*job
, int64_t ns
)
1135 /* Check cancellation *before* setting busy = false, too! */
1136 if (block_job_is_cancelled(job
)) {
1140 if (!block_job_should_pause(job
)) {
1141 block_job_do_yield(job
, qemu_clock_get_ns(QEMU_CLOCK_REALTIME
) + ns
);
1144 block_job_pause_point(job
);
1147 void block_job_yield(BlockJob
*job
)
1151 /* Check cancellation *before* setting busy = false, too! */
1152 if (block_job_is_cancelled(job
)) {
1156 if (!block_job_should_pause(job
)) {
1157 block_job_do_yield(job
, -1);
1160 block_job_pause_point(job
);
1163 void block_job_iostatus_reset(BlockJob
*job
)
1165 if (job
->iostatus
== BLOCK_DEVICE_IO_STATUS_OK
) {
1168 assert(job
->user_paused
&& job
->pause_count
> 0);
1169 job
->iostatus
= BLOCK_DEVICE_IO_STATUS_OK
;
1172 void block_job_event_ready(BlockJob
*job
)
1174 block_job_state_transition(job
, BLOCK_JOB_STATUS_READY
);
1177 if (block_job_is_internal(job
)) {
1181 qapi_event_send_block_job_ready(job
->driver
->job_type
,
1185 job
->speed
, &error_abort
);
1188 BlockErrorAction
block_job_error_action(BlockJob
*job
, BlockdevOnError on_err
,
1189 int is_read
, int error
)
1191 BlockErrorAction action
;
1194 case BLOCKDEV_ON_ERROR_ENOSPC
:
1195 case BLOCKDEV_ON_ERROR_AUTO
:
1196 action
= (error
== ENOSPC
) ?
1197 BLOCK_ERROR_ACTION_STOP
: BLOCK_ERROR_ACTION_REPORT
;
1199 case BLOCKDEV_ON_ERROR_STOP
:
1200 action
= BLOCK_ERROR_ACTION_STOP
;
1202 case BLOCKDEV_ON_ERROR_REPORT
:
1203 action
= BLOCK_ERROR_ACTION_REPORT
;
1205 case BLOCKDEV_ON_ERROR_IGNORE
:
1206 action
= BLOCK_ERROR_ACTION_IGNORE
;
1211 if (!block_job_is_internal(job
)) {
1212 qapi_event_send_block_job_error(job
->id
,
1213 is_read
? IO_OPERATION_TYPE_READ
:
1214 IO_OPERATION_TYPE_WRITE
,
1215 action
, &error_abort
);
1217 if (action
== BLOCK_ERROR_ACTION_STOP
) {
1218 block_job_pause(job
);
1219 /* make the pause user visible, which will be resumed from QMP. */
1220 job
->user_paused
= true;
1221 block_job_iostatus_set_err(job
, error
);
1228 AioContext
*aio_context
;
1229 BlockJobDeferToMainLoopFn
*fn
;
1231 } BlockJobDeferToMainLoopData
;
1233 static void block_job_defer_to_main_loop_bh(void *opaque
)
1235 BlockJobDeferToMainLoopData
*data
= opaque
;
1236 AioContext
*aio_context
;
1238 /* Prevent race with block_job_defer_to_main_loop() */
1239 aio_context_acquire(data
->aio_context
);
1241 /* Fetch BDS AioContext again, in case it has changed */
1242 aio_context
= blk_get_aio_context(data
->job
->blk
);
1243 if (aio_context
!= data
->aio_context
) {
1244 aio_context_acquire(aio_context
);
1247 data
->fn(data
->job
, data
->opaque
);
1249 if (aio_context
!= data
->aio_context
) {
1250 aio_context_release(aio_context
);
1253 aio_context_release(data
->aio_context
);
1258 void block_job_defer_to_main_loop(BlockJob
*job
,
1259 BlockJobDeferToMainLoopFn
*fn
,
1262 BlockJobDeferToMainLoopData
*data
= g_malloc(sizeof(*data
));
1264 data
->aio_context
= blk_get_aio_context(job
->blk
);
1266 data
->opaque
= opaque
;
1267 job
->deferred_to_main_loop
= true;
1269 aio_bh_schedule_oneshot(qemu_get_aio_context(),
1270 block_job_defer_to_main_loop_bh
, data
);