2 * Background jobs (long-running operations)
4 * Copyright (c) 2011 IBM Corp.
5 * Copyright (c) 2012, 2018 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 "qapi/error.h"
31 #include "qemu/main-loop.h"
32 #include "trace-root.h"
34 static QLIST_HEAD(, Job
) jobs
= QLIST_HEAD_INITIALIZER(jobs
);
36 /* Job State Transition Table */
37 bool JobSTT
[JOB_STATUS__MAX
][JOB_STATUS__MAX
] = {
38 /* U, C, R, P, Y, S, W, D, X, E, N */
39 /* U: */ [JOB_STATUS_UNDEFINED
] = {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
40 /* C: */ [JOB_STATUS_CREATED
] = {0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1},
41 /* R: */ [JOB_STATUS_RUNNING
] = {0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0},
42 /* P: */ [JOB_STATUS_PAUSED
] = {0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0},
43 /* Y: */ [JOB_STATUS_READY
] = {0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0},
44 /* S: */ [JOB_STATUS_STANDBY
] = {0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0},
45 /* W: */ [JOB_STATUS_WAITING
] = {0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0},
46 /* D: */ [JOB_STATUS_PENDING
] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0},
47 /* X: */ [JOB_STATUS_ABORTING
] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0},
48 /* E: */ [JOB_STATUS_CONCLUDED
] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
49 /* N: */ [JOB_STATUS_NULL
] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
52 bool JobVerbTable
[JOB_VERB__MAX
][JOB_STATUS__MAX
] = {
53 /* U, C, R, P, Y, S, W, D, X, E, N */
54 [JOB_VERB_CANCEL
] = {0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0},
55 [JOB_VERB_PAUSE
] = {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0},
56 [JOB_VERB_RESUME
] = {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0},
57 [JOB_VERB_SET_SPEED
] = {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0},
58 [JOB_VERB_COMPLETE
] = {0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0},
59 [JOB_VERB_FINALIZE
] = {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0},
60 [JOB_VERB_DISMISS
] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
63 /* Right now, this mutex is only needed to synchronize accesses to job->busy
64 * and job->sleep_timer, such as concurrent calls to job_do_yield and
66 static QemuMutex job_mutex
;
68 static void job_lock(void)
70 qemu_mutex_lock(&job_mutex
);
73 static void job_unlock(void)
75 qemu_mutex_unlock(&job_mutex
);
78 static void __attribute__((__constructor__
)) job_init(void)
80 qemu_mutex_init(&job_mutex
);
83 /* TODO Make static once the whole state machine is in job.c */
84 void job_state_transition(Job
*job
, JobStatus s1
)
86 JobStatus s0
= job
->status
;
87 assert(s1
>= 0 && s1
<= JOB_STATUS__MAX
);
88 trace_job_state_transition(job
, job
->ret
,
89 JobSTT
[s0
][s1
] ? "allowed" : "disallowed",
90 JobStatus_str(s0
), JobStatus_str(s1
));
91 assert(JobSTT
[s0
][s1
]);
95 int job_apply_verb(Job
*job
, JobVerb verb
, Error
**errp
)
97 JobStatus s0
= job
->status
;
98 assert(verb
>= 0 && verb
<= JOB_VERB__MAX
);
99 trace_job_apply_verb(job
, JobStatus_str(s0
), JobVerb_str(verb
),
100 JobVerbTable
[verb
][s0
] ? "allowed" : "prohibited");
101 if (JobVerbTable
[verb
][s0
]) {
104 error_setg(errp
, "Job '%s' in state '%s' cannot accept command verb '%s'",
105 job
->id
, JobStatus_str(s0
), JobVerb_str(verb
));
109 JobType
job_type(const Job
*job
)
111 return job
->driver
->job_type
;
114 const char *job_type_str(const Job
*job
)
116 return JobType_str(job_type(job
));
119 bool job_is_cancelled(Job
*job
)
121 return job
->cancelled
;
124 bool job_is_completed(Job
*job
)
126 switch (job
->status
) {
127 case JOB_STATUS_UNDEFINED
:
128 case JOB_STATUS_CREATED
:
129 case JOB_STATUS_RUNNING
:
130 case JOB_STATUS_PAUSED
:
131 case JOB_STATUS_READY
:
132 case JOB_STATUS_STANDBY
:
134 case JOB_STATUS_WAITING
:
135 case JOB_STATUS_PENDING
:
136 case JOB_STATUS_ABORTING
:
137 case JOB_STATUS_CONCLUDED
:
138 case JOB_STATUS_NULL
:
141 g_assert_not_reached();
146 bool job_started(Job
*job
)
151 bool job_should_pause(Job
*job
)
153 return job
->pause_count
> 0;
156 Job
*job_next(Job
*job
)
159 return QLIST_FIRST(&jobs
);
161 return QLIST_NEXT(job
, job_list
);
164 Job
*job_get(const char *id
)
168 QLIST_FOREACH(job
, &jobs
, job_list
) {
169 if (job
->id
&& !strcmp(id
, job
->id
)) {
177 static void job_sleep_timer_cb(void *opaque
)
184 void *job_create(const char *job_id
, const JobDriver
*driver
, AioContext
*ctx
,
185 int flags
, BlockCompletionFunc
*cb
, void *opaque
, Error
**errp
)
190 if (flags
& JOB_INTERNAL
) {
191 error_setg(errp
, "Cannot specify job ID for internal job");
194 if (!id_wellformed(job_id
)) {
195 error_setg(errp
, "Invalid job ID '%s'", job_id
);
198 if (job_get(job_id
)) {
199 error_setg(errp
, "Job ID '%s' already in use", job_id
);
202 } else if (!(flags
& JOB_INTERNAL
)) {
203 error_setg(errp
, "An explicit job ID is required");
207 job
= g_malloc0(driver
->instance_size
);
208 job
->driver
= driver
;
209 job
->id
= g_strdup(job_id
);
211 job
->aio_context
= ctx
;
214 job
->pause_count
= 1;
215 job
->auto_finalize
= !(flags
& JOB_MANUAL_FINALIZE
);
216 job
->auto_dismiss
= !(flags
& JOB_MANUAL_DISMISS
);
218 job
->opaque
= opaque
;
220 notifier_list_init(&job
->on_finalize_cancelled
);
221 notifier_list_init(&job
->on_finalize_completed
);
222 notifier_list_init(&job
->on_pending
);
224 job_state_transition(job
, JOB_STATUS_CREATED
);
225 aio_timer_init(qemu_get_aio_context(), &job
->sleep_timer
,
226 QEMU_CLOCK_REALTIME
, SCALE_NS
,
227 job_sleep_timer_cb
, job
);
229 QLIST_INSERT_HEAD(&jobs
, job
, job_list
);
234 void job_ref(Job
*job
)
239 void job_unref(Job
*job
)
241 if (--job
->refcnt
== 0) {
242 assert(job
->status
== JOB_STATUS_NULL
);
243 assert(!timer_pending(&job
->sleep_timer
));
245 if (job
->driver
->free
) {
246 job
->driver
->free(job
);
249 QLIST_REMOVE(job
, job_list
);
256 void job_event_cancelled(Job
*job
)
258 notifier_list_notify(&job
->on_finalize_cancelled
, job
);
261 void job_event_completed(Job
*job
)
263 notifier_list_notify(&job
->on_finalize_completed
, job
);
266 void job_event_pending(Job
*job
)
268 notifier_list_notify(&job
->on_pending
, job
);
271 void job_enter_cond(Job
*job
, bool(*fn
)(Job
*job
))
273 if (!job_started(job
)) {
276 if (job
->deferred_to_main_loop
) {
286 if (fn
&& !fn(job
)) {
291 assert(!job
->deferred_to_main_loop
);
292 timer_del(&job
->sleep_timer
);
295 aio_co_wake(job
->co
);
298 void job_enter(Job
*job
)
300 job_enter_cond(job
, NULL
);
303 /* Yield, and schedule a timer to reenter the coroutine after @ns nanoseconds.
304 * Reentering the job coroutine with block_job_enter() before the timer has
305 * expired is allowed and cancels the timer.
307 * If @ns is (uint64_t) -1, no timer is scheduled and block_job_enter() must be
308 * called explicitly. */
309 void coroutine_fn
job_do_yield(Job
*job
, uint64_t ns
)
313 timer_mod(&job
->sleep_timer
, ns
);
317 qemu_coroutine_yield();
319 /* Set by job_enter_cond() before re-entering the coroutine. */
323 void coroutine_fn
job_pause_point(Job
*job
)
325 assert(job
&& job_started(job
));
327 if (!job_should_pause(job
)) {
330 if (job_is_cancelled(job
)) {
334 if (job
->driver
->pause
) {
335 job
->driver
->pause(job
);
338 if (job_should_pause(job
) && !job_is_cancelled(job
)) {
339 JobStatus status
= job
->status
;
340 job_state_transition(job
, status
== JOB_STATUS_READY
342 : JOB_STATUS_PAUSED
);
344 job_do_yield(job
, -1);
346 job_state_transition(job
, status
);
349 if (job
->driver
->resume
) {
350 job
->driver
->resume(job
);
354 void coroutine_fn
job_sleep_ns(Job
*job
, int64_t ns
)
358 /* Check cancellation *before* setting busy = false, too! */
359 if (job_is_cancelled(job
)) {
363 if (!job_should_pause(job
)) {
364 job_do_yield(job
, qemu_clock_get_ns(QEMU_CLOCK_REALTIME
) + ns
);
367 job_pause_point(job
);
370 void job_drain(Job
*job
)
372 /* If job is !busy this kicks it into the next pause point. */
375 if (job
->driver
->drain
) {
376 job
->driver
->drain(job
);
382 * All jobs must allow a pause point before entering their job proper. This
383 * ensures that jobs can be paused prior to being started, then resumed later.
385 static void coroutine_fn
job_co_entry(void *opaque
)
389 assert(job
&& job
->driver
&& job
->driver
->start
);
390 job_pause_point(job
);
391 job
->driver
->start(job
);
395 void job_start(Job
*job
)
397 assert(job
&& !job_started(job
) && job
->paused
&&
398 job
->driver
&& job
->driver
->start
);
399 job
->co
= qemu_coroutine_create(job_co_entry
, job
);
403 job_state_transition(job
, JOB_STATUS_RUNNING
);
404 aio_co_enter(job
->aio_context
, job
->co
);
407 /* Assumes the block_job_mutex is held */
408 static bool job_timer_not_pending(Job
*job
)
410 return !timer_pending(&job
->sleep_timer
);
413 void job_pause(Job
*job
)
418 void job_resume(Job
*job
)
420 assert(job
->pause_count
> 0);
422 if (job
->pause_count
) {
426 /* kick only if no timer is pending */
427 job_enter_cond(job
, job_timer_not_pending
);
430 void job_user_pause(Job
*job
, Error
**errp
)
432 if (job_apply_verb(job
, JOB_VERB_PAUSE
, errp
)) {
435 if (job
->user_paused
) {
436 error_setg(errp
, "Job is already paused");
439 job
->user_paused
= true;
443 bool job_user_paused(Job
*job
)
445 return job
->user_paused
;
448 void job_user_resume(Job
*job
, Error
**errp
)
451 if (!job
->user_paused
|| job
->pause_count
<= 0) {
452 error_setg(errp
, "Can't resume a job that was not paused");
455 if (job_apply_verb(job
, JOB_VERB_RESUME
, errp
)) {
458 if (job
->driver
->user_resume
) {
459 job
->driver
->user_resume(job
);
461 job
->user_paused
= false;
465 void job_do_dismiss(Job
*job
)
470 job
->deferred_to_main_loop
= true;
472 /* TODO Don't assume it's a BlockJob */
473 block_job_txn_del_job((BlockJob
*) job
);
475 job_state_transition(job
, JOB_STATUS_NULL
);
479 void job_early_fail(Job
*job
)
481 assert(job
->status
== JOB_STATUS_CREATED
);
485 static void job_conclude(Job
*job
)
487 job_state_transition(job
, JOB_STATUS_CONCLUDED
);
488 if (job
->auto_dismiss
|| !job_started(job
)) {
493 void job_update_rc(Job
*job
)
495 if (!job
->ret
&& job_is_cancelled(job
)) {
496 job
->ret
= -ECANCELED
;
499 job_state_transition(job
, JOB_STATUS_ABORTING
);
503 static void job_commit(Job
*job
)
506 if (job
->driver
->commit
) {
507 job
->driver
->commit(job
);
511 static void job_abort(Job
*job
)
514 if (job
->driver
->abort
) {
515 job
->driver
->abort(job
);
519 static void job_clean(Job
*job
)
521 if (job
->driver
->clean
) {
522 job
->driver
->clean(job
);
526 int job_finalize_single(Job
*job
)
528 assert(job_is_completed(job
));
530 /* Ensure abort is called for late-transactional failures */
541 job
->cb(job
->opaque
, job
->ret
);
544 /* Emit events only if we actually started */
545 if (job_started(job
)) {
546 if (job_is_cancelled(job
)) {
547 job_event_cancelled(job
);
549 job_event_completed(job
);
553 /* TODO Don't assume it's a BlockJob */
554 block_job_txn_del_job((BlockJob
*) job
);
562 JobDeferToMainLoopFn
*fn
;
564 } JobDeferToMainLoopData
;
566 static void job_defer_to_main_loop_bh(void *opaque
)
568 JobDeferToMainLoopData
*data
= opaque
;
569 Job
*job
= data
->job
;
570 AioContext
*aio_context
= job
->aio_context
;
572 aio_context_acquire(aio_context
);
573 data
->fn(data
->job
, data
->opaque
);
574 aio_context_release(aio_context
);
579 void job_defer_to_main_loop(Job
*job
, JobDeferToMainLoopFn
*fn
, void *opaque
)
581 JobDeferToMainLoopData
*data
= g_malloc(sizeof(*data
));
584 data
->opaque
= opaque
;
585 job
->deferred_to_main_loop
= true;
587 aio_bh_schedule_oneshot(qemu_get_aio_context(),
588 job_defer_to_main_loop_bh
, data
);