]> git.proxmox.com Git - mirror_qemu.git/blame - job.c
plugins: Move function pointer in qemu_plugin_dyn_cb
[mirror_qemu.git] / job.c
CommitLineData
33e9e9bd
KW
1/*
2 * Background jobs (long-running operations)
3 *
4 * Copyright (c) 2011 IBM Corp.
5 * Copyright (c) 2012, 2018 Red Hat, Inc.
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
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
23 * THE SOFTWARE.
24 */
25
26#include "qemu/osdep.h"
33e9e9bd
KW
27#include "qapi/error.h"
28#include "qemu/job.h"
29#include "qemu/id.h"
1908a559 30#include "qemu/main-loop.h"
de0fbe64 31#include "block/aio-wait.h"
243af022 32#include "trace/trace-root.h"
1dac83f1 33#include "qapi/qapi-events-job.h"
33e9e9bd 34
bf61c583
EGE
35/*
36 * The job API is composed of two categories of functions.
37 *
38 * The first includes functions used by the monitor. The monitor is
39 * peculiar in that it accesses the job list with job_get, and
40 * therefore needs consistency across job_get and the actual operation
41 * (e.g. job_user_cancel). To achieve this consistency, the caller
42 * calls job_lock/job_unlock itself around the whole operation.
43 *
44 *
45 * The second includes functions used by the job drivers and sometimes
46 * by the core block layer. These delegate the locking to the callee instead.
bf61c583
EGE
47 */
48
55c5a25a
EGE
49/*
50 * job_mutex protects the jobs list, but also makes the
51 * struct job fields thread-safe.
52 */
53QemuMutex job_mutex;
54
afe1e8a7 55/* Protected by job_mutex */
e7c1d78b
KW
56static QLIST_HEAD(, Job) jobs = QLIST_HEAD_INITIALIZER(jobs);
57
a50c2ab8
KW
58/* Job State Transition Table */
59bool JobSTT[JOB_STATUS__MAX][JOB_STATUS__MAX] = {
60 /* U, C, R, P, Y, S, W, D, X, E, N */
61 /* U: */ [JOB_STATUS_UNDEFINED] = {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
62 /* C: */ [JOB_STATUS_CREATED] = {0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1},
63 /* R: */ [JOB_STATUS_RUNNING] = {0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0},
64 /* P: */ [JOB_STATUS_PAUSED] = {0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0},
65 /* Y: */ [JOB_STATUS_READY] = {0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0},
66 /* S: */ [JOB_STATUS_STANDBY] = {0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0},
67 /* W: */ [JOB_STATUS_WAITING] = {0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0},
68 /* D: */ [JOB_STATUS_PENDING] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0},
69 /* X: */ [JOB_STATUS_ABORTING] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0},
70 /* E: */ [JOB_STATUS_CONCLUDED] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
71 /* N: */ [JOB_STATUS_NULL] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
72};
73
74bool JobVerbTable[JOB_VERB__MAX][JOB_STATUS__MAX] = {
75 /* U, C, R, P, Y, S, W, D, X, E, N */
76 [JOB_VERB_CANCEL] = {0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0},
77 [JOB_VERB_PAUSE] = {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0},
78 [JOB_VERB_RESUME] = {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0},
79 [JOB_VERB_SET_SPEED] = {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0},
53ddb9c8 80 [JOB_VERB_COMPLETE] = {0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0},
a50c2ab8
KW
81 [JOB_VERB_FINALIZE] = {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0},
82 [JOB_VERB_DISMISS] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
61a3a5a7 83 [JOB_VERB_CHANGE] = {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0},
a50c2ab8
KW
84};
85
7eaa8fb5
KW
86/* Transactional group of jobs */
87struct JobTxn {
88
89 /* Is this txn being cancelled? */
90 bool aborting;
91
92 /* List of jobs */
93 QLIST_HEAD(, Job) jobs;
94
95 /* Reference count */
96 int refcnt;
97};
98
55c5a25a 99void job_lock(void)
da01ff7f
KW
100{
101 qemu_mutex_lock(&job_mutex);
102}
103
6f592e5a 104void job_unlock(void)
da01ff7f
KW
105{
106 qemu_mutex_unlock(&job_mutex);
107}
108
109static void __attribute__((__constructor__)) job_init(void)
110{
111 qemu_mutex_init(&job_mutex);
112}
113
7eaa8fb5
KW
114JobTxn *job_txn_new(void)
115{
116 JobTxn *txn = g_new0(JobTxn, 1);
117 QLIST_INIT(&txn->jobs);
118 txn->refcnt = 1;
119 return txn;
120}
121
afe1e8a7
EGE
122/* Called with job_mutex held. */
123static void job_txn_ref_locked(JobTxn *txn)
7eaa8fb5
KW
124{
125 txn->refcnt++;
126}
127
afe1e8a7 128void job_txn_unref_locked(JobTxn *txn)
7eaa8fb5
KW
129{
130 if (txn && --txn->refcnt == 0) {
131 g_free(txn);
132 }
133}
134
afe1e8a7
EGE
135void job_txn_unref(JobTxn *txn)
136{
137 JOB_LOCK_GUARD();
138 job_txn_unref_locked(txn);
139}
140
544f4d52
EGE
141/**
142 * @txn: The transaction (may be NULL)
143 * @job: Job to add to the transaction
144 *
145 * Add @job to the transaction. The @job must not already be in a transaction.
146 * The caller must call either job_txn_unref() or job_completed() to release
147 * the reference that is automatically grabbed here.
148 *
149 * If @txn is NULL, the function does nothing.
afe1e8a7
EGE
150 *
151 * Called with job_mutex held.
544f4d52 152 */
afe1e8a7 153static void job_txn_add_job_locked(JobTxn *txn, Job *job)
7eaa8fb5
KW
154{
155 if (!txn) {
156 return;
157 }
158
159 assert(!job->txn);
160 job->txn = txn;
161
162 QLIST_INSERT_HEAD(&txn->jobs, job, txn_list);
afe1e8a7 163 job_txn_ref_locked(txn);
7eaa8fb5
KW
164}
165
afe1e8a7
EGE
166/* Called with job_mutex held. */
167static void job_txn_del_job_locked(Job *job)
7eaa8fb5
KW
168{
169 if (job->txn) {
170 QLIST_REMOVE(job, txn_list);
afe1e8a7 171 job_txn_unref_locked(job->txn);
7eaa8fb5
KW
172 job->txn = NULL;
173 }
174}
175
afe1e8a7
EGE
176/* Called with job_mutex held, but releases it temporarily. */
177static int job_txn_apply_locked(Job *job, int fn(Job *))
7eaa8fb5 178{
b660a84b
SR
179 Job *other_job, *next;
180 JobTxn *txn = job->txn;
7eaa8fb5
KW
181 int rc = 0;
182
b660a84b
SR
183 /*
184 * Similar to job_completed_txn_abort, we take each job's lock before
185 * applying fn, but since we assume that outer_ctx is held by the caller,
186 * we need to release it here to avoid holding the lock twice - which would
187 * break AIO_WAIT_WHILE from within fn.
188 */
afe1e8a7 189 job_ref_locked(job);
b660a84b
SR
190
191 QLIST_FOREACH_SAFE(other_job, &txn->jobs, txn_list, next) {
b660a84b 192 rc = fn(other_job);
7eaa8fb5
KW
193 if (rc) {
194 break;
195 }
196 }
b660a84b 197
afe1e8a7 198 job_unref_locked(job);
7eaa8fb5
KW
199 return rc;
200}
201
456273b0 202bool job_is_internal(Job *job)
1dac83f1
KW
203{
204 return (job->id == NULL);
205}
206
afe1e8a7
EGE
207/* Called with job_mutex held. */
208static void job_state_transition_locked(Job *job, JobStatus s1)
a50c2ab8
KW
209{
210 JobStatus s0 = job->status;
c2032289 211 assert(s1 >= 0 && s1 < JOB_STATUS__MAX);
4ad35181 212 trace_job_state_transition(job, job->ret,
a50c2ab8
KW
213 JobSTT[s0][s1] ? "allowed" : "disallowed",
214 JobStatus_str(s0), JobStatus_str(s1));
215 assert(JobSTT[s0][s1]);
216 job->status = s1;
1dac83f1
KW
217
218 if (!job_is_internal(job) && s1 != s0) {
3ab72385 219 qapi_event_send_job_status_change(job->id, job->status);
1dac83f1 220 }
a50c2ab8
KW
221}
222
afe1e8a7 223int job_apply_verb_locked(Job *job, JobVerb verb, Error **errp)
a50c2ab8
KW
224{
225 JobStatus s0 = job->status;
c2032289 226 assert(verb >= 0 && verb < JOB_VERB__MAX);
a50c2ab8
KW
227 trace_job_apply_verb(job, JobStatus_str(s0), JobVerb_str(verb),
228 JobVerbTable[verb][s0] ? "allowed" : "prohibited");
229 if (JobVerbTable[verb][s0]) {
230 return 0;
231 }
232 error_setg(errp, "Job '%s' in state '%s' cannot accept command verb '%s'",
233 job->id, JobStatus_str(s0), JobVerb_str(verb));
234 return -EPERM;
235}
236
252291ea
KW
237JobType job_type(const Job *job)
238{
239 return job->driver->job_type;
240}
241
242const char *job_type_str(const Job *job)
243{
244 return JobType_str(job_type(job));
245}
246
afe1e8a7 247bool job_is_cancelled_locked(Job *job)
08b83bff 248{
a640fa0e
HR
249 /* force_cancel may be true only if cancelled is true, too */
250 assert(job->cancelled || !job->force_cancel);
251 return job->force_cancel;
08b83bff
HR
252}
253
afe1e8a7
EGE
254bool job_is_cancelled(Job *job)
255{
256 JOB_LOCK_GUARD();
257 return job_is_cancelled_locked(job);
258}
259
260/* Called with job_mutex held. */
261static bool job_cancel_requested_locked(Job *job)
daa7f2f9
KW
262{
263 return job->cancelled;
264}
265
afe1e8a7
EGE
266bool job_cancel_requested(Job *job)
267{
268 JOB_LOCK_GUARD();
269 return job_cancel_requested_locked(job);
270}
271
272bool job_is_ready_locked(Job *job)
df956ae2
KW
273{
274 switch (job->status) {
275 case JOB_STATUS_UNDEFINED:
276 case JOB_STATUS_CREATED:
277 case JOB_STATUS_RUNNING:
278 case JOB_STATUS_PAUSED:
279 case JOB_STATUS_WAITING:
280 case JOB_STATUS_PENDING:
281 case JOB_STATUS_ABORTING:
282 case JOB_STATUS_CONCLUDED:
283 case JOB_STATUS_NULL:
284 return false;
285 case JOB_STATUS_READY:
286 case JOB_STATUS_STANDBY:
287 return true;
288 default:
289 g_assert_not_reached();
290 }
291 return false;
292}
293
afe1e8a7
EGE
294bool job_is_ready(Job *job)
295{
296 JOB_LOCK_GUARD();
297 return job_is_ready_locked(job);
298}
299
300bool job_is_completed_locked(Job *job)
dbe5e6c1
KW
301{
302 switch (job->status) {
303 case JOB_STATUS_UNDEFINED:
304 case JOB_STATUS_CREATED:
305 case JOB_STATUS_RUNNING:
306 case JOB_STATUS_PAUSED:
307 case JOB_STATUS_READY:
308 case JOB_STATUS_STANDBY:
309 return false;
310 case JOB_STATUS_WAITING:
311 case JOB_STATUS_PENDING:
312 case JOB_STATUS_ABORTING:
313 case JOB_STATUS_CONCLUDED:
314 case JOB_STATUS_NULL:
315 return true;
316 default:
317 g_assert_not_reached();
318 }
319 return false;
320}
321
9bd4d3c2 322static bool job_is_completed(Job *job)
afe1e8a7
EGE
323{
324 JOB_LOCK_GUARD();
325 return job_is_completed_locked(job);
326}
327
328static bool job_started_locked(Job *job)
da01ff7f
KW
329{
330 return job->co;
331}
332
afe1e8a7
EGE
333/* Called with job_mutex held. */
334static bool job_should_pause_locked(Job *job)
da01ff7f
KW
335{
336 return job->pause_count > 0;
337}
338
afe1e8a7 339Job *job_next_locked(Job *job)
e7c1d78b
KW
340{
341 if (!job) {
342 return QLIST_FIRST(&jobs);
343 }
344 return QLIST_NEXT(job, job_list);
345}
346
afe1e8a7
EGE
347Job *job_next(Job *job)
348{
349 JOB_LOCK_GUARD();
350 return job_next_locked(job);
351}
352
353Job *job_get_locked(const char *id)
e7c1d78b
KW
354{
355 Job *job;
356
357 QLIST_FOREACH(job, &jobs, job_list) {
358 if (job->id && !strcmp(id, job->id)) {
359 return job;
360 }
361 }
362
363 return NULL;
364}
365
3ed4f708
EGE
366void job_set_aio_context(Job *job, AioContext *ctx)
367{
368 /* protect against read in job_finish_sync_locked and job_start */
369 GLOBAL_STATE_CODE();
370 /* protect against read in job_do_yield_locked */
371 JOB_LOCK_GUARD();
372 /* ensure the job is quiescent while the AioContext is changed */
373 assert(job->paused || job_is_completed_locked(job));
374 job->aio_context = ctx;
375}
376
afe1e8a7 377/* Called with job_mutex *not* held. */
5d43e86e
KW
378static void job_sleep_timer_cb(void *opaque)
379{
380 Job *job = opaque;
381
382 job_enter(job);
383}
384
7eaa8fb5
KW
385void *job_create(const char *job_id, const JobDriver *driver, JobTxn *txn,
386 AioContext *ctx, int flags, BlockCompletionFunc *cb,
387 void *opaque, Error **errp)
33e9e9bd
KW
388{
389 Job *job;
390
afe1e8a7
EGE
391 JOB_LOCK_GUARD();
392
33e9e9bd 393 if (job_id) {
bb02b65c
KW
394 if (flags & JOB_INTERNAL) {
395 error_setg(errp, "Cannot specify job ID for internal job");
396 return NULL;
397 }
33e9e9bd
KW
398 if (!id_wellformed(job_id)) {
399 error_setg(errp, "Invalid job ID '%s'", job_id);
400 return NULL;
401 }
afe1e8a7 402 if (job_get_locked(job_id)) {
e7c1d78b
KW
403 error_setg(errp, "Job ID '%s' already in use", job_id);
404 return NULL;
405 }
bb02b65c
KW
406 } else if (!(flags & JOB_INTERNAL)) {
407 error_setg(errp, "An explicit job ID is required");
408 return NULL;
33e9e9bd
KW
409 }
410
411 job = g_malloc0(driver->instance_size);
412 job->driver = driver;
413 job->id = g_strdup(job_id);
80fa2c75 414 job->refcnt = 1;
08be6fe2 415 job->aio_context = ctx;
da01ff7f
KW
416 job->busy = false;
417 job->paused = true;
418 job->pause_count = 1;
bb02b65c
KW
419 job->auto_finalize = !(flags & JOB_MANUAL_FINALIZE);
420 job->auto_dismiss = !(flags & JOB_MANUAL_DISMISS);
4ad35181
KW
421 job->cb = cb;
422 job->opaque = opaque;
33e9e9bd 423
a7b4f8fc
EGE
424 progress_init(&job->progress);
425
139a9f02
KW
426 notifier_list_init(&job->on_finalize_cancelled);
427 notifier_list_init(&job->on_finalize_completed);
428 notifier_list_init(&job->on_pending);
2e1795b5 429 notifier_list_init(&job->on_ready);
252f4091 430 notifier_list_init(&job->on_idle);
139a9f02 431
afe1e8a7 432 job_state_transition_locked(job, JOB_STATUS_CREATED);
5d43e86e
KW
433 aio_timer_init(qemu_get_aio_context(), &job->sleep_timer,
434 QEMU_CLOCK_REALTIME, SCALE_NS,
435 job_sleep_timer_cb, job);
a50c2ab8 436
e7c1d78b
KW
437 QLIST_INSERT_HEAD(&jobs, job, job_list);
438
7eaa8fb5
KW
439 /* Single jobs are modeled as single-job transactions for sake of
440 * consolidating the job management logic */
441 if (!txn) {
442 txn = job_txn_new();
afe1e8a7
EGE
443 job_txn_add_job_locked(txn, job);
444 job_txn_unref_locked(txn);
7eaa8fb5 445 } else {
afe1e8a7 446 job_txn_add_job_locked(txn, job);
7eaa8fb5
KW
447 }
448
33e9e9bd
KW
449 return job;
450}
fd61a701 451
afe1e8a7 452void job_ref_locked(Job *job)
fd61a701 453{
80fa2c75
KW
454 ++job->refcnt;
455}
456
afe1e8a7 457void job_unref_locked(Job *job)
80fa2c75 458{
c70b8031
EGE
459 GLOBAL_STATE_CODE();
460
80fa2c75
KW
461 if (--job->refcnt == 0) {
462 assert(job->status == JOB_STATUS_NULL);
5d43e86e 463 assert(!timer_pending(&job->sleep_timer));
7eaa8fb5 464 assert(!job->txn);
e7c1d78b 465
80fa2c75 466 if (job->driver->free) {
afe1e8a7 467 job_unlock();
80fa2c75 468 job->driver->free(job);
afe1e8a7 469 job_lock();
80fa2c75
KW
470 }
471
472 QLIST_REMOVE(job, job_list);
473
a7b4f8fc 474 progress_destroy(&job->progress);
3d1f8b07 475 error_free(job->err);
80fa2c75
KW
476 g_free(job->id);
477 g_free(job);
478 }
fd61a701 479}
1908a559 480
30a5c887
KW
481void job_progress_update(Job *job, uint64_t done)
482{
01fe1ca9 483 progress_work_done(&job->progress, done);
30a5c887
KW
484}
485
486void job_progress_set_remaining(Job *job, uint64_t remaining)
487{
01fe1ca9 488 progress_set_remaining(&job->progress, remaining);
30a5c887
KW
489}
490
62f13600
HR
491void job_progress_increase_remaining(Job *job, uint64_t delta)
492{
01fe1ca9 493 progress_increase_remaining(&job->progress, delta);
62f13600
HR
494}
495
544f4d52
EGE
496/**
497 * To be called when a cancelled job is finalised.
afe1e8a7 498 * Called with job_mutex held.
544f4d52 499 */
afe1e8a7 500static void job_event_cancelled_locked(Job *job)
139a9f02
KW
501{
502 notifier_list_notify(&job->on_finalize_cancelled, job);
503}
504
544f4d52
EGE
505/**
506 * To be called when a successfully completed job is finalised.
afe1e8a7 507 * Called with job_mutex held.
544f4d52 508 */
afe1e8a7 509static void job_event_completed_locked(Job *job)
139a9f02
KW
510{
511 notifier_list_notify(&job->on_finalize_completed, job);
512}
513
afe1e8a7
EGE
514/* Called with job_mutex held. */
515static void job_event_pending_locked(Job *job)
139a9f02
KW
516{
517 notifier_list_notify(&job->on_pending, job);
518}
519
afe1e8a7
EGE
520/* Called with job_mutex held. */
521static void job_event_ready_locked(Job *job)
2e1795b5
KW
522{
523 notifier_list_notify(&job->on_ready, job);
524}
525
afe1e8a7
EGE
526/* Called with job_mutex held. */
527static void job_event_idle_locked(Job *job)
34dc97b9
KW
528{
529 notifier_list_notify(&job->on_idle, job);
530}
531
afe1e8a7 532void job_enter_cond_locked(Job *job, bool(*fn)(Job *job))
da01ff7f 533{
afe1e8a7 534 if (!job_started_locked(job)) {
da01ff7f
KW
535 return;
536 }
537 if (job->deferred_to_main_loop) {
538 return;
539 }
540
da01ff7f 541 if (job->busy) {
da01ff7f
KW
542 return;
543 }
544
545 if (fn && !fn(job)) {
da01ff7f
KW
546 return;
547 }
548
549 assert(!job->deferred_to_main_loop);
550 timer_del(&job->sleep_timer);
551 job->busy = true;
afe1e8a7 552 job_unlock();
ef02dac2 553 aio_co_wake(job->co);
afe1e8a7
EGE
554 job_lock();
555}
556
5d43e86e
KW
557void job_enter(Job *job)
558{
afe1e8a7
EGE
559 JOB_LOCK_GUARD();
560 job_enter_cond_locked(job, NULL);
5d43e86e
KW
561}
562
da01ff7f 563/* Yield, and schedule a timer to reenter the coroutine after @ns nanoseconds.
3d70ff53
KW
564 * Reentering the job coroutine with job_enter() before the timer has expired
565 * is allowed and cancels the timer.
da01ff7f 566 *
3d70ff53 567 * If @ns is (uint64_t) -1, no timer is scheduled and job_enter() must be
afe1e8a7
EGE
568 * called explicitly.
569 *
570 * Called with job_mutex held, but releases it temporarily.
571 */
572static void coroutine_fn job_do_yield_locked(Job *job, uint64_t ns)
da01ff7f 573{
ef02dac2
PB
574 AioContext *next_aio_context;
575
da01ff7f
KW
576 if (ns != -1) {
577 timer_mod(&job->sleep_timer, ns);
578 }
579 job->busy = false;
afe1e8a7 580 job_event_idle_locked(job);
afe1e8a7 581 job_unlock();
da01ff7f 582 qemu_coroutine_yield();
afe1e8a7 583 job_lock();
da01ff7f 584
ef02dac2
PB
585 next_aio_context = job->aio_context;
586 /*
587 * Coroutine has resumed, but in the meanwhile the job AioContext
142e6907 588 * might have changed via bdrv_try_change_aio_context(), so we need to move
ef02dac2
PB
589 * the coroutine too in the new aiocontext.
590 */
591 while (qemu_get_current_aio_context() != next_aio_context) {
592 job_unlock();
593 aio_co_reschedule_self(next_aio_context);
594 job_lock();
595 next_aio_context = job->aio_context;
596 }
597
598 /* Set by job_enter_cond_locked() before re-entering the coroutine. */
da01ff7f
KW
599 assert(job->busy);
600}
601
afe1e8a7
EGE
602/* Called with job_mutex held, but releases it temporarily. */
603static void coroutine_fn job_pause_point_locked(Job *job)
da01ff7f 604{
afe1e8a7 605 assert(job && job_started_locked(job));
da01ff7f 606
afe1e8a7 607 if (!job_should_pause_locked(job)) {
da01ff7f
KW
608 return;
609 }
afe1e8a7 610 if (job_is_cancelled_locked(job)) {
da01ff7f
KW
611 return;
612 }
613
614 if (job->driver->pause) {
afe1e8a7 615 job_unlock();
da01ff7f 616 job->driver->pause(job);
afe1e8a7 617 job_lock();
da01ff7f
KW
618 }
619
afe1e8a7 620 if (job_should_pause_locked(job) && !job_is_cancelled_locked(job)) {
da01ff7f 621 JobStatus status = job->status;
afe1e8a7
EGE
622 job_state_transition_locked(job, status == JOB_STATUS_READY
623 ? JOB_STATUS_STANDBY
624 : JOB_STATUS_PAUSED);
da01ff7f 625 job->paused = true;
afe1e8a7 626 job_do_yield_locked(job, -1);
da01ff7f 627 job->paused = false;
afe1e8a7 628 job_state_transition_locked(job, status);
da01ff7f
KW
629 }
630
631 if (job->driver->resume) {
afe1e8a7 632 job_unlock();
da01ff7f 633 job->driver->resume(job);
afe1e8a7 634 job_lock();
da01ff7f
KW
635 }
636}
637
afe1e8a7
EGE
638void coroutine_fn job_pause_point(Job *job)
639{
640 JOB_LOCK_GUARD();
641 job_pause_point_locked(job);
642}
643
9bd4d3c2 644void coroutine_fn job_yield(Job *job)
198c49cc 645{
9bd4d3c2 646 JOB_LOCK_GUARD();
198c49cc
KW
647 assert(job->busy);
648
649 /* Check cancellation *before* setting busy = false, too! */
afe1e8a7 650 if (job_is_cancelled_locked(job)) {
198c49cc
KW
651 return;
652 }
653
afe1e8a7
EGE
654 if (!job_should_pause_locked(job)) {
655 job_do_yield_locked(job, -1);
198c49cc
KW
656 }
657
afe1e8a7
EGE
658 job_pause_point_locked(job);
659}
660
5d43e86e
KW
661void coroutine_fn job_sleep_ns(Job *job, int64_t ns)
662{
afe1e8a7 663 JOB_LOCK_GUARD();
5d43e86e
KW
664 assert(job->busy);
665
666 /* Check cancellation *before* setting busy = false, too! */
afe1e8a7 667 if (job_is_cancelled_locked(job)) {
5d43e86e
KW
668 return;
669 }
670
afe1e8a7
EGE
671 if (!job_should_pause_locked(job)) {
672 job_do_yield_locked(job, qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + ns);
5d43e86e
KW
673 }
674
afe1e8a7 675 job_pause_point_locked(job);
5d43e86e
KW
676}
677
afe1e8a7
EGE
678/* Assumes the job_mutex is held */
679static bool job_timer_not_pending_locked(Job *job)
b15de828
KW
680{
681 return !timer_pending(&job->sleep_timer);
682}
683
afe1e8a7 684void job_pause_locked(Job *job)
b15de828
KW
685{
686 job->pause_count++;
3ee1483b 687 if (!job->paused) {
afe1e8a7 688 job_enter_cond_locked(job, NULL);
3ee1483b 689 }
b15de828
KW
690}
691
afe1e8a7
EGE
692void job_pause(Job *job)
693{
694 JOB_LOCK_GUARD();
695 job_pause_locked(job);
696}
697
698void job_resume_locked(Job *job)
b15de828
KW
699{
700 assert(job->pause_count > 0);
701 job->pause_count--;
702 if (job->pause_count) {
703 return;
704 }
705
706 /* kick only if no timer is pending */
afe1e8a7 707 job_enter_cond_locked(job, job_timer_not_pending_locked);
b15de828
KW
708}
709
afe1e8a7 710void job_resume(Job *job)
b15de828 711{
afe1e8a7
EGE
712 JOB_LOCK_GUARD();
713 job_resume_locked(job);
714}
715
716void job_user_pause_locked(Job *job, Error **errp)
717{
718 if (job_apply_verb_locked(job, JOB_VERB_PAUSE, errp)) {
b15de828
KW
719 return;
720 }
721 if (job->user_paused) {
722 error_setg(errp, "Job is already paused");
723 return;
724 }
725 job->user_paused = true;
afe1e8a7 726 job_pause_locked(job);
b15de828
KW
727}
728
afe1e8a7 729bool job_user_paused_locked(Job *job)
b15de828
KW
730{
731 return job->user_paused;
732}
733
afe1e8a7 734void job_user_resume_locked(Job *job, Error **errp)
b15de828
KW
735{
736 assert(job);
c70b8031 737 GLOBAL_STATE_CODE();
b15de828
KW
738 if (!job->user_paused || job->pause_count <= 0) {
739 error_setg(errp, "Can't resume a job that was not paused");
740 return;
741 }
afe1e8a7 742 if (job_apply_verb_locked(job, JOB_VERB_RESUME, errp)) {
b15de828
KW
743 return;
744 }
745 if (job->driver->user_resume) {
afe1e8a7 746 job_unlock();
b15de828 747 job->driver->user_resume(job);
afe1e8a7 748 job_lock();
b15de828
KW
749 }
750 job->user_paused = false;
afe1e8a7 751 job_resume_locked(job);
b15de828
KW
752}
753
afe1e8a7
EGE
754/* Called with job_mutex held, but releases it temporarily. */
755static void job_do_dismiss_locked(Job *job)
4ad35181
KW
756{
757 assert(job);
758 job->busy = false;
759 job->paused = false;
760 job->deferred_to_main_loop = true;
761
afe1e8a7 762 job_txn_del_job_locked(job);
4ad35181 763
afe1e8a7
EGE
764 job_state_transition_locked(job, JOB_STATUS_NULL);
765 job_unref_locked(job);
4ad35181
KW
766}
767
afe1e8a7 768void job_dismiss_locked(Job **jobptr, Error **errp)
5f9a6a08
KW
769{
770 Job *job = *jobptr;
771 /* similarly to _complete, this is QMP-interface only. */
772 assert(job->id);
afe1e8a7 773 if (job_apply_verb_locked(job, JOB_VERB_DISMISS, errp)) {
5f9a6a08
KW
774 return;
775 }
776
afe1e8a7 777 job_do_dismiss_locked(job);
5f9a6a08
KW
778 *jobptr = NULL;
779}
780
4ad35181
KW
781void job_early_fail(Job *job)
782{
afe1e8a7 783 JOB_LOCK_GUARD();
4ad35181 784 assert(job->status == JOB_STATUS_CREATED);
afe1e8a7 785 job_do_dismiss_locked(job);
4ad35181
KW
786}
787
afe1e8a7
EGE
788/* Called with job_mutex held. */
789static void job_conclude_locked(Job *job)
4ad35181 790{
afe1e8a7
EGE
791 job_state_transition_locked(job, JOB_STATUS_CONCLUDED);
792 if (job->auto_dismiss || !job_started_locked(job)) {
793 job_do_dismiss_locked(job);
4ad35181
KW
794 }
795}
796
afe1e8a7
EGE
797/* Called with job_mutex held. */
798static void job_update_rc_locked(Job *job)
4ad35181 799{
afe1e8a7 800 if (!job->ret && job_is_cancelled_locked(job)) {
4ad35181
KW
801 job->ret = -ECANCELED;
802 }
803 if (job->ret) {
3d1f8b07
JS
804 if (!job->err) {
805 error_setg(&job->err, "%s", strerror(-job->ret));
1266c9b9 806 }
afe1e8a7 807 job_state_transition_locked(job, JOB_STATUS_ABORTING);
4ad35181
KW
808 }
809}
810
811static void job_commit(Job *job)
812{
813 assert(!job->ret);
c70b8031 814 GLOBAL_STATE_CODE();
4ad35181
KW
815 if (job->driver->commit) {
816 job->driver->commit(job);
817 }
818}
819
820static void job_abort(Job *job)
821{
822 assert(job->ret);
c70b8031 823 GLOBAL_STATE_CODE();
4ad35181
KW
824 if (job->driver->abort) {
825 job->driver->abort(job);
826 }
827}
828
829static void job_clean(Job *job)
830{
c70b8031 831 GLOBAL_STATE_CODE();
4ad35181
KW
832 if (job->driver->clean) {
833 job->driver->clean(job);
834 }
835}
836
6f592e5a
EGE
837/*
838 * Called with job_mutex held, but releases it temporarily.
6f592e5a 839 */
afe1e8a7 840static int job_finalize_single_locked(Job *job)
4ad35181 841{
afe1e8a7
EGE
842 int job_ret;
843
844 assert(job_is_completed_locked(job));
4ad35181
KW
845
846 /* Ensure abort is called for late-transactional failures */
afe1e8a7
EGE
847 job_update_rc_locked(job);
848
849 job_ret = job->ret;
850 job_unlock();
4ad35181 851
afe1e8a7 852 if (!job_ret) {
4ad35181
KW
853 job_commit(job);
854 } else {
855 job_abort(job);
856 }
857 job_clean(job);
858
859 if (job->cb) {
afe1e8a7 860 job->cb(job->opaque, job_ret);
4ad35181
KW
861 }
862
6f592e5a
EGE
863 job_lock();
864
4ad35181 865 /* Emit events only if we actually started */
afe1e8a7
EGE
866 if (job_started_locked(job)) {
867 if (job_is_cancelled_locked(job)) {
868 job_event_cancelled_locked(job);
4ad35181 869 } else {
afe1e8a7 870 job_event_completed_locked(job);
4ad35181
KW
871 }
872 }
873
afe1e8a7
EGE
874 job_txn_del_job_locked(job);
875 job_conclude_locked(job);
4ad35181
KW
876 return 0;
877}
878
6f592e5a
EGE
879/*
880 * Called with job_mutex held, but releases it temporarily.
6f592e5a 881 */
afe1e8a7 882static void job_cancel_async_locked(Job *job, bool force)
7eaa8fb5 883{
c70b8031 884 GLOBAL_STATE_CODE();
9820933b 885 if (job->driver->cancel) {
afe1e8a7 886 job_unlock();
73895f38 887 force = job->driver->cancel(job, force);
afe1e8a7 888 job_lock();
73895f38
HR
889 } else {
890 /* No .cancel() means the job will behave as if force-cancelled */
891 force = true;
9820933b 892 }
73895f38 893
7eaa8fb5
KW
894 if (job->user_paused) {
895 /* Do not call job_enter here, the caller will handle it. */
7eaa8fb5 896 if (job->driver->user_resume) {
afe1e8a7 897 job_unlock();
7eaa8fb5 898 job->driver->user_resume(job);
afe1e8a7 899 job_lock();
7eaa8fb5 900 }
e321c059 901 job->user_paused = false;
7eaa8fb5
KW
902 assert(job->pause_count > 0);
903 job->pause_count--;
904 }
401dd096
HR
905
906 /*
907 * Ignore soft cancel requests after the job is already done
908 * (We will still invoke job->driver->cancel() above, but if the
909 * job driver supports soft cancelling and the job is done, that
910 * should be a no-op, too. We still call it so it can override
911 * @force.)
912 */
913 if (force || !job->deferred_to_main_loop) {
914 job->cancelled = true;
915 /* To prevent 'force == false' overriding a previous 'force == true' */
916 job->force_cancel |= force;
917 }
7eaa8fb5
KW
918}
919
6f592e5a
EGE
920/*
921 * Called with job_mutex held, but releases it temporarily.
6f592e5a 922 */
afe1e8a7 923static void job_completed_txn_abort_locked(Job *job)
7eaa8fb5 924{
7eaa8fb5
KW
925 JobTxn *txn = job->txn;
926 Job *other_job;
927
928 if (txn->aborting) {
929 /*
930 * We are cancelled by another job, which will handle everything.
931 */
932 return;
933 }
934 txn->aborting = true;
afe1e8a7 935 job_txn_ref_locked(txn);
7eaa8fb5 936
afe1e8a7 937 job_ref_locked(job);
7eaa8fb5
KW
938
939 /* Other jobs are effectively cancelled by us, set the status for
940 * them; this job, however, may or may not be cancelled, depending
941 * on the caller, so leave it. */
942 QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
943 if (other_job != job) {
1d4a43e9
HR
944 /*
945 * This is a transaction: If one job failed, no result will matter.
946 * Therefore, pass force=true to terminate all other jobs as quickly
947 * as possible.
948 */
afe1e8a7 949 job_cancel_async_locked(other_job, true);
7eaa8fb5
KW
950 }
951 }
952 while (!QLIST_EMPTY(&txn->jobs)) {
953 other_job = QLIST_FIRST(&txn->jobs);
afe1e8a7
EGE
954 if (!job_is_completed_locked(other_job)) {
955 assert(job_cancel_requested_locked(other_job));
956 job_finish_sync_locked(other_job, NULL, NULL);
7eaa8fb5 957 }
afe1e8a7 958 job_finalize_single_locked(other_job);
7eaa8fb5
KW
959 }
960
afe1e8a7 961 job_unref_locked(job);
afe1e8a7 962 job_txn_unref_locked(txn);
7eaa8fb5
KW
963}
964
afe1e8a7
EGE
965/* Called with job_mutex held, but releases it temporarily */
966static int job_prepare_locked(Job *job)
7eaa8fb5 967{
afe1e8a7
EGE
968 int ret;
969
c70b8031 970 GLOBAL_STATE_CODE();
6f592e5a 971
7eaa8fb5 972 if (job->ret == 0 && job->driver->prepare) {
afe1e8a7
EGE
973 job_unlock();
974 ret = job->driver->prepare(job);
975 job_lock();
976 job->ret = ret;
977 job_update_rc_locked(job);
7eaa8fb5 978 }
6f592e5a 979
7eaa8fb5
KW
980 return job->ret;
981}
982
afe1e8a7
EGE
983/* Called with job_mutex held */
984static int job_needs_finalize_locked(Job *job)
7eaa8fb5
KW
985{
986 return !job->auto_finalize;
987}
988
afe1e8a7
EGE
989/* Called with job_mutex held */
990static void job_do_finalize_locked(Job *job)
7eaa8fb5
KW
991{
992 int rc;
993 assert(job && job->txn);
994
995 /* prepare the transaction to complete */
afe1e8a7 996 rc = job_txn_apply_locked(job, job_prepare_locked);
7eaa8fb5 997 if (rc) {
afe1e8a7 998 job_completed_txn_abort_locked(job);
7eaa8fb5 999 } else {
afe1e8a7 1000 job_txn_apply_locked(job, job_finalize_single_locked);
7eaa8fb5
KW
1001 }
1002}
1003
afe1e8a7 1004void job_finalize_locked(Job *job, Error **errp)
7eaa8fb5
KW
1005{
1006 assert(job && job->id);
afe1e8a7 1007 if (job_apply_verb_locked(job, JOB_VERB_FINALIZE, errp)) {
7eaa8fb5
KW
1008 return;
1009 }
afe1e8a7 1010 job_do_finalize_locked(job);
7eaa8fb5
KW
1011}
1012
afe1e8a7
EGE
1013/* Called with job_mutex held. */
1014static int job_transition_to_pending_locked(Job *job)
1015{
1016 job_state_transition_locked(job, JOB_STATUS_PENDING);
7eaa8fb5 1017 if (!job->auto_finalize) {
afe1e8a7 1018 job_event_pending_locked(job);
7eaa8fb5
KW
1019 }
1020 return 0;
1021}
1022
2e1795b5
KW
1023void job_transition_to_ready(Job *job)
1024{
afe1e8a7
EGE
1025 JOB_LOCK_GUARD();
1026 job_state_transition_locked(job, JOB_STATUS_READY);
1027 job_event_ready_locked(job);
2e1795b5
KW
1028}
1029
afe1e8a7
EGE
1030/* Called with job_mutex held. */
1031static void job_completed_txn_success_locked(Job *job)
7eaa8fb5
KW
1032{
1033 JobTxn *txn = job->txn;
1034 Job *other_job;
1035
afe1e8a7 1036 job_state_transition_locked(job, JOB_STATUS_WAITING);
7eaa8fb5
KW
1037
1038 /*
1039 * Successful completion, see if there are other running jobs in this
1040 * txn.
1041 */
1042 QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
afe1e8a7 1043 if (!job_is_completed_locked(other_job)) {
7eaa8fb5
KW
1044 return;
1045 }
1046 assert(other_job->ret == 0);
1047 }
1048
afe1e8a7 1049 job_txn_apply_locked(job, job_transition_to_pending_locked);
7eaa8fb5
KW
1050
1051 /* If no jobs need manual finalization, automatically do so */
afe1e8a7
EGE
1052 if (job_txn_apply_locked(job, job_needs_finalize_locked) == 0) {
1053 job_do_finalize_locked(job);
7eaa8fb5
KW
1054 }
1055}
1056
afe1e8a7
EGE
1057/* Called with job_mutex held. */
1058static void job_completed_locked(Job *job)
3d70ff53 1059{
afe1e8a7 1060 assert(job && job->txn && !job_is_completed_locked(job));
1266c9b9 1061
afe1e8a7 1062 job_update_rc_locked(job);
404ff28d 1063 trace_job_completed(job, job->ret);
3d70ff53 1064 if (job->ret) {
afe1e8a7 1065 job_completed_txn_abort_locked(job);
3d70ff53 1066 } else {
afe1e8a7 1067 job_completed_txn_success_locked(job);
3d70ff53
KW
1068 }
1069}
1070
afe1e8a7
EGE
1071/**
1072 * Useful only as a type shim for aio_bh_schedule_oneshot.
1073 * Called with job_mutex *not* held.
1074 */
ccbfb331
JS
1075static void job_exit(void *opaque)
1076{
1077 Job *job = (Job *)opaque;
afe1e8a7 1078 JOB_LOCK_GUARD();
afe1e8a7 1079 job_ref_locked(job);
b5a7a057
KW
1080
1081 /* This is a lie, we're not quiescent, but still doing the completion
1082 * callbacks. However, completion callbacks tend to involve operations that
1083 * drain block nodes, and if .drained_poll still returned true, we would
1084 * deadlock. */
1085 job->busy = false;
afe1e8a7 1086 job_event_idle_locked(job);
b5a7a057 1087
afe1e8a7 1088 job_completed_locked(job);
afe1e8a7 1089 job_unref_locked(job);
ccbfb331
JS
1090}
1091
1092/**
1093 * All jobs must allow a pause point before entering their job proper. This
1094 * ensures that jobs can be paused prior to being started, then resumed later.
1095 */
1096static void coroutine_fn job_co_entry(void *opaque)
1097{
1098 Job *job = opaque;
afe1e8a7 1099 int ret;
ccbfb331
JS
1100
1101 assert(job && job->driver && job->driver->run);
afe1e8a7
EGE
1102 WITH_JOB_LOCK_GUARD() {
1103 assert(job->aio_context == qemu_get_current_aio_context());
1104 job_pause_point_locked(job);
1105 }
1106 ret = job->driver->run(job, &job->err);
1107 WITH_JOB_LOCK_GUARD() {
1108 job->ret = ret;
1109 job->deferred_to_main_loop = true;
1110 job->busy = true;
1111 }
ccbfb331
JS
1112 aio_bh_schedule_oneshot(qemu_get_aio_context(), job_exit, job);
1113}
1114
1115void job_start(Job *job)
1116{
afe1e8a7
EGE
1117 assert(qemu_in_main_thread());
1118
1119 WITH_JOB_LOCK_GUARD() {
1120 assert(job && !job_started_locked(job) && job->paused &&
1121 job->driver && job->driver->run);
1122 job->co = qemu_coroutine_create(job_co_entry, job);
1123 job->pause_count--;
1124 job->busy = true;
1125 job->paused = false;
1126 job_state_transition_locked(job, JOB_STATUS_RUNNING);
1127 }
ccbfb331
JS
1128 aio_co_enter(job->aio_context, job->co);
1129}
1130
afe1e8a7 1131void job_cancel_locked(Job *job, bool force)
3d70ff53
KW
1132{
1133 if (job->status == JOB_STATUS_CONCLUDED) {
afe1e8a7 1134 job_do_dismiss_locked(job);
3d70ff53
KW
1135 return;
1136 }
afe1e8a7
EGE
1137 job_cancel_async_locked(job, force);
1138 if (!job_started_locked(job)) {
1139 job_completed_locked(job);
3d70ff53 1140 } else if (job->deferred_to_main_loop) {
401dd096
HR
1141 /*
1142 * job_cancel_async() ignores soft-cancel requests for jobs
1143 * that are already done (i.e. deferred to the main loop). We
1144 * have to check again whether the job is really cancelled.
08b83bff
HR
1145 * (job_cancel_requested() and job_is_cancelled() are equivalent
1146 * here, because job_cancel_async() will make soft-cancel
1147 * requests no-ops when deferred_to_main_loop is true. We
1148 * choose to call job_is_cancelled() to show that we invoke
1149 * job_completed_txn_abort() only for force-cancelled jobs.)
401dd096 1150 */
afe1e8a7
EGE
1151 if (job_is_cancelled_locked(job)) {
1152 job_completed_txn_abort_locked(job);
401dd096 1153 }
3d70ff53 1154 } else {
afe1e8a7 1155 job_enter_cond_locked(job, NULL);
3d70ff53
KW
1156 }
1157}
1158
afe1e8a7
EGE
1159void job_user_cancel_locked(Job *job, bool force, Error **errp)
1160{
1161 if (job_apply_verb_locked(job, JOB_VERB_CANCEL, errp)) {
3d70ff53
KW
1162 return;
1163 }
afe1e8a7
EGE
1164 job_cancel_locked(job, force);
1165}
1166
9bd4d3c2
EGE
1167/* A wrapper around job_cancel_locked() taking an Error ** parameter so it may
1168 * be used with job_finish_sync_locked() without the need for (rather nasty)
1169 * function pointer casts there.
afe1e8a7
EGE
1170 *
1171 * Called with job_mutex held.
1172 */
1173static void job_cancel_err_locked(Job *job, Error **errp)
3d70ff53 1174{
afe1e8a7 1175 job_cancel_locked(job, false);
3d70ff53
KW
1176}
1177
4cfb3f05
HR
1178/**
1179 * Same as job_cancel_err(), but force-cancel.
afe1e8a7 1180 * Called with job_mutex held.
4cfb3f05 1181 */
afe1e8a7 1182static void job_force_cancel_err_locked(Job *job, Error **errp)
3d70ff53 1183{
afe1e8a7 1184 job_cancel_locked(job, true);
4cfb3f05
HR
1185}
1186
afe1e8a7 1187int job_cancel_sync_locked(Job *job, bool force)
4cfb3f05
HR
1188{
1189 if (force) {
afe1e8a7 1190 return job_finish_sync_locked(job, &job_force_cancel_err_locked, NULL);
4cfb3f05 1191 } else {
afe1e8a7 1192 return job_finish_sync_locked(job, &job_cancel_err_locked, NULL);
4cfb3f05 1193 }
3d70ff53
KW
1194}
1195
afe1e8a7
EGE
1196int job_cancel_sync(Job *job, bool force)
1197{
1198 JOB_LOCK_GUARD();
1199 return job_cancel_sync_locked(job, force);
1200}
1201
3d70ff53
KW
1202void job_cancel_sync_all(void)
1203{
1204 Job *job;
afe1e8a7 1205 JOB_LOCK_GUARD();
3d70ff53 1206
afe1e8a7 1207 while ((job = job_next_locked(NULL))) {
afe1e8a7 1208 job_cancel_sync_locked(job, true);
3d70ff53
KW
1209 }
1210}
1211
afe1e8a7
EGE
1212int job_complete_sync_locked(Job *job, Error **errp)
1213{
1214 return job_finish_sync_locked(job, job_complete_locked, errp);
1215}
1216
afe1e8a7 1217void job_complete_locked(Job *job, Error **errp)
3453d972
KW
1218{
1219 /* Should not be reachable via external interface for internal jobs */
1220 assert(job->id);
c70b8031 1221 GLOBAL_STATE_CODE();
afe1e8a7 1222 if (job_apply_verb_locked(job, JOB_VERB_COMPLETE, errp)) {
3453d972
KW
1223 return;
1224 }
afe1e8a7 1225 if (job_cancel_requested_locked(job) || !job->driver->complete) {
3453d972
KW
1226 error_setg(errp, "The active block job '%s' cannot be completed",
1227 job->id);
1228 return;
1229 }
1230
afe1e8a7 1231 job_unlock();
3453d972 1232 job->driver->complete(job, errp);
afe1e8a7 1233 job_lock();
3453d972
KW
1234}
1235
afe1e8a7
EGE
1236int job_finish_sync_locked(Job *job,
1237 void (*finish)(Job *, Error **errp),
1238 Error **errp)
6a74c075
KW
1239{
1240 Error *local_err = NULL;
1241 int ret;
3ed4f708 1242 GLOBAL_STATE_CODE();
6a74c075 1243
afe1e8a7 1244 job_ref_locked(job);
6a74c075
KW
1245
1246 if (finish) {
1247 finish(job, &local_err);
1248 }
1249 if (local_err) {
1250 error_propagate(errp, local_err);
afe1e8a7 1251 job_unref_locked(job);
6a74c075
KW
1252 return -EBUSY;
1253 }
de0fbe64 1254
afe1e8a7 1255 job_unlock();
6f592e5a
EGE
1256 AIO_WAIT_WHILE_UNLOCKED(job->aio_context,
1257 (job_enter(job), !job_is_completed(job)));
afe1e8a7 1258 job_lock();
de0fbe64 1259
afe1e8a7
EGE
1260 ret = (job_is_cancelled_locked(job) && job->ret == 0)
1261 ? -ECANCELED : job->ret;
1262 job_unref_locked(job);
6a74c075
KW
1263 return ret;
1264}