blob: 99a2e54b54a899033a9f20ce6252d5be40c7d60f [file] [log] [blame]
Kevin Wolf33e9e9b2018-04-12 17:29:59 +02001/*
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"
Kevin Wolf33e9e9b2018-04-12 17:29:59 +020027#include "qapi/error.h"
28#include "qemu/job.h"
29#include "qemu/id.h"
Kevin Wolf1908a552018-04-17 16:41:17 +020030#include "qemu/main-loop.h"
Kevin Wolfde0fbe62018-08-17 14:58:49 +020031#include "block/aio-wait.h"
Paolo Bonzini243af022020-02-04 12:20:10 +010032#include "trace/trace-root.h"
Kevin Wolf1dac83f2018-04-30 19:09:46 +020033#include "qapi/qapi-events-job.h"
Kevin Wolf33e9e9b2018-04-12 17:29:59 +020034
Emanuele Giuseppe Esposito55c5a252022-09-26 05:31:54 -040035/*
Emanuele Giuseppe Espositobf61c582022-09-26 05:31:59 -040036 * 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.
Emanuele Giuseppe Espositobf61c582022-09-26 05:31:59 -040047 */
48
49/*
Emanuele Giuseppe Esposito55c5a252022-09-26 05:31:54 -040050 * job_mutex protects the jobs list, but also makes the
51 * struct job fields thread-safe.
52 */
53QemuMutex job_mutex;
54
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -040055/* Protected by job_mutex */
Kevin Wolfe7c1d782018-04-12 17:54:37 +020056static QLIST_HEAD(, Job) jobs = QLIST_HEAD_INITIALIZER(jobs);
57
Kevin Wolfa50c2ab2018-04-13 17:19:31 +020058/* 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},
Max Reitz53ddb9c2021-04-09 14:04:20 +020080 [JOB_VERB_COMPLETE] = {0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0},
Kevin Wolfa50c2ab2018-04-13 17:19:31 +020081 [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},
Fiona Ebner61a3a5a2023-10-31 14:54:22 +010083 [JOB_VERB_CHANGE] = {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0},
Kevin Wolfa50c2ab2018-04-13 17:19:31 +020084};
85
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +020086/* 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
Emanuele Giuseppe Esposito55c5a252022-09-26 05:31:54 -040099void job_lock(void)
100{
Kevin Wolfda01ff72018-04-13 17:31:02 +0200101 qemu_mutex_lock(&job_mutex);
102}
103
Emanuele Giuseppe Esposito6f592e52022-09-26 05:32:11 -0400104void job_unlock(void)
Kevin Wolfda01ff72018-04-13 17:31:02 +0200105{
106 qemu_mutex_unlock(&job_mutex);
107}
108
109static void __attribute__((__constructor__)) job_init(void)
110{
111 qemu_mutex_init(&job_mutex);
112}
113
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +0200114JobTxn *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
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400122/* Called with job_mutex held. */
123static void job_txn_ref_locked(JobTxn *txn)
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +0200124{
125 txn->refcnt++;
126}
127
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400128void job_txn_unref_locked(JobTxn *txn)
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +0200129{
130 if (txn && --txn->refcnt == 0) {
131 g_free(txn);
132 }
133}
134
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400135void job_txn_unref(JobTxn *txn)
136{
137 JOB_LOCK_GUARD();
138 job_txn_unref_locked(txn);
139}
140
Emanuele Giuseppe Esposito544f4d52022-09-26 05:31:56 -0400141/**
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.
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400150 *
151 * Called with job_mutex held.
Emanuele Giuseppe Esposito544f4d52022-09-26 05:31:56 -0400152 */
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400153static void job_txn_add_job_locked(JobTxn *txn, Job *job)
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +0200154{
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);
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400163 job_txn_ref_locked(txn);
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +0200164}
165
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400166/* Called with job_mutex held. */
167static void job_txn_del_job_locked(Job *job)
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +0200168{
169 if (job->txn) {
170 QLIST_REMOVE(job, txn_list);
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400171 job_txn_unref_locked(job->txn);
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +0200172 job->txn = NULL;
173 }
174}
175
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400176/* Called with job_mutex held, but releases it temporarily. */
177static int job_txn_apply_locked(Job *job, int fn(Job *))
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +0200178{
Stefan Reiterb660a842020-04-07 13:56:49 +0200179 Job *other_job, *next;
180 JobTxn *txn = job->txn;
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +0200181 int rc = 0;
182
Stefan Reiterb660a842020-04-07 13:56:49 +0200183 /*
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 */
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400189 job_ref_locked(job);
Stefan Reiterb660a842020-04-07 13:56:49 +0200190
191 QLIST_FOREACH_SAFE(other_job, &txn->jobs, txn_list, next) {
Stefan Reiterb660a842020-04-07 13:56:49 +0200192 rc = fn(other_job);
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +0200193 if (rc) {
194 break;
195 }
196 }
Stefan Reiterb660a842020-04-07 13:56:49 +0200197
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400198 job_unref_locked(job);
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +0200199 return rc;
200}
201
Kevin Wolf456273b2018-05-04 16:25:43 +0200202bool job_is_internal(Job *job)
Kevin Wolf1dac83f2018-04-30 19:09:46 +0200203{
204 return (job->id == NULL);
205}
206
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400207/* Called with job_mutex held. */
208static void job_state_transition_locked(Job *job, JobStatus s1)
Kevin Wolfa50c2ab2018-04-13 17:19:31 +0200209{
210 JobStatus s0 = job->status;
Liam Merwickc2032282018-11-05 21:38:35 +0000211 assert(s1 >= 0 && s1 < JOB_STATUS__MAX);
Kevin Wolf4ad35182018-04-19 17:30:16 +0200212 trace_job_state_transition(job, job->ret,
Kevin Wolfa50c2ab2018-04-13 17:19:31 +0200213 JobSTT[s0][s1] ? "allowed" : "disallowed",
214 JobStatus_str(s0), JobStatus_str(s1));
215 assert(JobSTT[s0][s1]);
216 job->status = s1;
Kevin Wolf1dac83f2018-04-30 19:09:46 +0200217
218 if (!job_is_internal(job) && s1 != s0) {
Peter Xu3ab72382018-08-15 21:37:37 +0800219 qapi_event_send_job_status_change(job->id, job->status);
Kevin Wolf1dac83f2018-04-30 19:09:46 +0200220 }
Kevin Wolfa50c2ab2018-04-13 17:19:31 +0200221}
222
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400223int job_apply_verb_locked(Job *job, JobVerb verb, Error **errp)
Kevin Wolfa50c2ab2018-04-13 17:19:31 +0200224{
225 JobStatus s0 = job->status;
Liam Merwickc2032282018-11-05 21:38:35 +0000226 assert(verb >= 0 && verb < JOB_VERB__MAX);
Kevin Wolfa50c2ab2018-04-13 17:19:31 +0200227 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
Kevin Wolf252291e2018-04-12 17:57:08 +0200237JobType 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
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400247bool job_is_cancelled_locked(Job *job)
Kevin Wolfdaa7f2f2018-04-17 12:56:07 +0200248{
Hanna Reitza640fa02021-10-06 17:19:39 +0200249 /* force_cancel may be true only if cancelled is true, too */
250 assert(job->cancelled || !job->force_cancel);
251 return job->force_cancel;
Hanna Reitz08b83bf2021-10-06 17:19:35 +0200252}
253
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400254bool 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)
Hanna Reitz08b83bf2021-10-06 17:19:35 +0200262{
Kevin Wolfdaa7f2f2018-04-17 12:56:07 +0200263 return job->cancelled;
264}
265
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400266bool 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)
Kevin Wolfdf956ae2018-04-25 15:09:58 +0200273{
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
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400294bool 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)
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
Emanuele Giuseppe Esposito9bd4d3c2022-09-26 05:32:14 -0400322static bool job_is_completed(Job *job)
Kevin Wolfdbe5e6c2018-04-19 13:04:01 +0200323{
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400324 JOB_LOCK_GUARD();
325 return job_is_completed_locked(job);
Kevin Wolfdbe5e6c2018-04-19 13:04:01 +0200326}
327
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400328static bool job_started_locked(Job *job)
Kevin Wolfda01ff72018-04-13 17:31:02 +0200329{
330 return job->co;
331}
332
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400333/* Called with job_mutex held. */
334static bool job_should_pause_locked(Job *job)
Kevin Wolfda01ff72018-04-13 17:31:02 +0200335{
336 return job->pause_count > 0;
337}
338
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400339Job *job_next_locked(Job *job)
Kevin Wolfe7c1d782018-04-12 17:54:37 +0200340{
341 if (!job) {
342 return QLIST_FIRST(&jobs);
343 }
344 return QLIST_NEXT(job, job_list);
345}
346
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400347Job *job_next(Job *job)
348{
349 JOB_LOCK_GUARD();
350 return job_next_locked(job);
351}
352
353Job *job_get_locked(const char *id)
Kevin Wolfe7c1d782018-04-12 17:54:37 +0200354{
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
Emanuele Giuseppe Esposito3ed4f702022-09-26 05:32:06 -0400366void 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
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400377/* Called with job_mutex *not* held. */
Kevin Wolf5d43e862018-04-18 16:32:20 +0200378static void job_sleep_timer_cb(void *opaque)
379{
380 Job *job = opaque;
381
382 job_enter(job);
383}
384
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +0200385void *job_create(const char *job_id, const JobDriver *driver, JobTxn *txn,
386 AioContext *ctx, int flags, BlockCompletionFunc *cb,
387 void *opaque, Error **errp)
Kevin Wolf33e9e9b2018-04-12 17:29:59 +0200388{
389 Job *job;
390
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400391 JOB_LOCK_GUARD();
392
Kevin Wolf33e9e9b2018-04-12 17:29:59 +0200393 if (job_id) {
Kevin Wolfbb02b652018-04-19 17:54:56 +0200394 if (flags & JOB_INTERNAL) {
395 error_setg(errp, "Cannot specify job ID for internal job");
396 return NULL;
397 }
Kevin Wolf33e9e9b2018-04-12 17:29:59 +0200398 if (!id_wellformed(job_id)) {
399 error_setg(errp, "Invalid job ID '%s'", job_id);
400 return NULL;
401 }
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400402 if (job_get_locked(job_id)) {
Kevin Wolfe7c1d782018-04-12 17:54:37 +0200403 error_setg(errp, "Job ID '%s' already in use", job_id);
404 return NULL;
405 }
Kevin Wolfbb02b652018-04-19 17:54:56 +0200406 } else if (!(flags & JOB_INTERNAL)) {
407 error_setg(errp, "An explicit job ID is required");
408 return NULL;
Kevin Wolf33e9e9b2018-04-12 17:29:59 +0200409 }
410
411 job = g_malloc0(driver->instance_size);
412 job->driver = driver;
413 job->id = g_strdup(job_id);
Kevin Wolf80fa2c72018-04-13 18:50:05 +0200414 job->refcnt = 1;
Kevin Wolf08be6fe2018-04-17 13:49:33 +0200415 job->aio_context = ctx;
Kevin Wolfda01ff72018-04-13 17:31:02 +0200416 job->busy = false;
417 job->paused = true;
418 job->pause_count = 1;
Kevin Wolfbb02b652018-04-19 17:54:56 +0200419 job->auto_finalize = !(flags & JOB_MANUAL_FINALIZE);
420 job->auto_dismiss = !(flags & JOB_MANUAL_DISMISS);
Kevin Wolf4ad35182018-04-19 17:30:16 +0200421 job->cb = cb;
422 job->opaque = opaque;
Kevin Wolf33e9e9b2018-04-12 17:29:59 +0200423
Emanuele Giuseppe Espositoa7b4f8f2021-06-14 10:11:29 +0200424 progress_init(&job->progress);
425
Kevin Wolf139a9f02018-04-23 18:04:57 +0200426 notifier_list_init(&job->on_finalize_cancelled);
427 notifier_list_init(&job->on_finalize_completed);
428 notifier_list_init(&job->on_pending);
Kevin Wolf2e1795b2018-04-25 14:56:09 +0200429 notifier_list_init(&job->on_ready);
Emanuele Giuseppe Esposito252f4092021-11-03 12:21:55 -0400430 notifier_list_init(&job->on_idle);
Kevin Wolf139a9f02018-04-23 18:04:57 +0200431
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400432 job_state_transition_locked(job, JOB_STATUS_CREATED);
Kevin Wolf5d43e862018-04-18 16:32:20 +0200433 aio_timer_init(qemu_get_aio_context(), &job->sleep_timer,
434 QEMU_CLOCK_REALTIME, SCALE_NS,
435 job_sleep_timer_cb, job);
Kevin Wolfa50c2ab2018-04-13 17:19:31 +0200436
Kevin Wolfe7c1d782018-04-12 17:54:37 +0200437 QLIST_INSERT_HEAD(&jobs, job, job_list);
438
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +0200439 /* 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();
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400443 job_txn_add_job_locked(txn, job);
444 job_txn_unref_locked(txn);
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +0200445 } else {
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400446 job_txn_add_job_locked(txn, job);
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +0200447 }
448
Kevin Wolf33e9e9b2018-04-12 17:29:59 +0200449 return job;
450}
Kevin Wolffd61a702018-04-12 19:06:53 +0200451
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400452void job_ref_locked(Job *job)
Kevin Wolffd61a702018-04-12 19:06:53 +0200453{
Kevin Wolf80fa2c72018-04-13 18:50:05 +0200454 ++job->refcnt;
455}
Kevin Wolfe7c1d782018-04-12 17:54:37 +0200456
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400457void job_unref_locked(Job *job)
Kevin Wolf80fa2c72018-04-13 18:50:05 +0200458{
Emanuele Giuseppe Espositoc70b8032022-03-03 10:16:16 -0500459 GLOBAL_STATE_CODE();
460
Kevin Wolf80fa2c72018-04-13 18:50:05 +0200461 if (--job->refcnt == 0) {
462 assert(job->status == JOB_STATUS_NULL);
Kevin Wolf5d43e862018-04-18 16:32:20 +0200463 assert(!timer_pending(&job->sleep_timer));
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +0200464 assert(!job->txn);
Kevin Wolf80fa2c72018-04-13 18:50:05 +0200465
466 if (job->driver->free) {
Emanuele Giuseppe Esposito6f592e52022-09-26 05:32:11 -0400467 AioContext *aio_context = job->aio_context;
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400468 job_unlock();
Emanuele Giuseppe Esposito6f592e52022-09-26 05:32:11 -0400469 /* FIXME: aiocontext lock is required because cb calls blk_unref */
470 aio_context_acquire(aio_context);
Kevin Wolf80fa2c72018-04-13 18:50:05 +0200471 job->driver->free(job);
Emanuele Giuseppe Esposito6f592e52022-09-26 05:32:11 -0400472 aio_context_release(aio_context);
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400473 job_lock();
Kevin Wolf80fa2c72018-04-13 18:50:05 +0200474 }
475
476 QLIST_REMOVE(job, job_list);
477
Emanuele Giuseppe Espositoa7b4f8f2021-06-14 10:11:29 +0200478 progress_destroy(&job->progress);
John Snow3d1f8b02018-08-29 21:57:27 -0400479 error_free(job->err);
Kevin Wolf80fa2c72018-04-13 18:50:05 +0200480 g_free(job->id);
481 g_free(job);
482 }
Kevin Wolffd61a702018-04-12 19:06:53 +0200483}
Kevin Wolf1908a552018-04-17 16:41:17 +0200484
Kevin Wolf30a5c882018-05-04 12:17:20 +0200485void job_progress_update(Job *job, uint64_t done)
486{
Vladimir Sementsov-Ogievskiy01fe1ca2020-03-11 13:29:56 +0300487 progress_work_done(&job->progress, done);
Kevin Wolf30a5c882018-05-04 12:17:20 +0200488}
489
490void job_progress_set_remaining(Job *job, uint64_t remaining)
491{
Vladimir Sementsov-Ogievskiy01fe1ca2020-03-11 13:29:56 +0300492 progress_set_remaining(&job->progress, remaining);
Kevin Wolf30a5c882018-05-04 12:17:20 +0200493}
494
Max Reitz62f13602018-06-13 20:18:20 +0200495void job_progress_increase_remaining(Job *job, uint64_t delta)
496{
Vladimir Sementsov-Ogievskiy01fe1ca2020-03-11 13:29:56 +0300497 progress_increase_remaining(&job->progress, delta);
Max Reitz62f13602018-06-13 20:18:20 +0200498}
499
Emanuele Giuseppe Esposito544f4d52022-09-26 05:31:56 -0400500/**
501 * To be called when a cancelled job is finalised.
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400502 * Called with job_mutex held.
Emanuele Giuseppe Esposito544f4d52022-09-26 05:31:56 -0400503 */
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400504static void job_event_cancelled_locked(Job *job)
Kevin Wolf139a9f02018-04-23 18:04:57 +0200505{
506 notifier_list_notify(&job->on_finalize_cancelled, job);
507}
508
Emanuele Giuseppe Esposito544f4d52022-09-26 05:31:56 -0400509/**
510 * To be called when a successfully completed job is finalised.
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400511 * Called with job_mutex held.
Emanuele Giuseppe Esposito544f4d52022-09-26 05:31:56 -0400512 */
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400513static void job_event_completed_locked(Job *job)
Kevin Wolf139a9f02018-04-23 18:04:57 +0200514{
515 notifier_list_notify(&job->on_finalize_completed, job);
516}
517
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400518/* Called with job_mutex held. */
519static void job_event_pending_locked(Job *job)
Kevin Wolf139a9f02018-04-23 18:04:57 +0200520{
521 notifier_list_notify(&job->on_pending, job);
522}
523
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400524/* Called with job_mutex held. */
525static void job_event_ready_locked(Job *job)
Kevin Wolf2e1795b2018-04-25 14:56:09 +0200526{
527 notifier_list_notify(&job->on_ready, job);
528}
529
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400530/* Called with job_mutex held. */
531static void job_event_idle_locked(Job *job)
Kevin Wolf34dc97b2018-08-17 14:53:05 +0200532{
533 notifier_list_notify(&job->on_idle, job);
534}
535
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400536void job_enter_cond_locked(Job *job, bool(*fn)(Job *job))
Kevin Wolfda01ff72018-04-13 17:31:02 +0200537{
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400538 if (!job_started_locked(job)) {
Kevin Wolfda01ff72018-04-13 17:31:02 +0200539 return;
540 }
541 if (job->deferred_to_main_loop) {
542 return;
543 }
544
Kevin Wolfda01ff72018-04-13 17:31:02 +0200545 if (job->busy) {
Kevin Wolfda01ff72018-04-13 17:31:02 +0200546 return;
547 }
548
549 if (fn && !fn(job)) {
Kevin Wolfda01ff72018-04-13 17:31:02 +0200550 return;
551 }
552
553 assert(!job->deferred_to_main_loop);
554 timer_del(&job->sleep_timer);
555 job->busy = true;
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400556 job_unlock();
Paolo Bonzinief02dac2022-09-26 05:32:05 -0400557 aio_co_wake(job->co);
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400558 job_lock();
559}
560
Kevin Wolf5d43e862018-04-18 16:32:20 +0200561void job_enter(Job *job)
562{
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400563 JOB_LOCK_GUARD();
564 job_enter_cond_locked(job, NULL);
Kevin Wolf5d43e862018-04-18 16:32:20 +0200565}
566
Kevin Wolfda01ff72018-04-13 17:31:02 +0200567/* Yield, and schedule a timer to reenter the coroutine after @ns nanoseconds.
Kevin Wolf3d70ff52018-04-24 16:13:52 +0200568 * Reentering the job coroutine with job_enter() before the timer has expired
569 * is allowed and cancels the timer.
Kevin Wolfda01ff72018-04-13 17:31:02 +0200570 *
Kevin Wolf3d70ff52018-04-24 16:13:52 +0200571 * If @ns is (uint64_t) -1, no timer is scheduled and job_enter() must be
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400572 * called explicitly.
573 *
574 * Called with job_mutex held, but releases it temporarily.
575 */
576static void coroutine_fn job_do_yield_locked(Job *job, uint64_t ns)
Kevin Wolfda01ff72018-04-13 17:31:02 +0200577{
Paolo Bonzinief02dac2022-09-26 05:32:05 -0400578 AioContext *next_aio_context;
579
Kevin Wolfda01ff72018-04-13 17:31:02 +0200580 if (ns != -1) {
581 timer_mod(&job->sleep_timer, ns);
582 }
583 job->busy = false;
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400584 job_event_idle_locked(job);
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400585 job_unlock();
Kevin Wolfda01ff72018-04-13 17:31:02 +0200586 qemu_coroutine_yield();
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400587 job_lock();
Kevin Wolfda01ff72018-04-13 17:31:02 +0200588
Paolo Bonzinief02dac2022-09-26 05:32:05 -0400589 next_aio_context = job->aio_context;
590 /*
591 * Coroutine has resumed, but in the meanwhile the job AioContext
Emanuele Giuseppe Esposito142e6902022-10-25 04:49:52 -0400592 * might have changed via bdrv_try_change_aio_context(), so we need to move
Paolo Bonzinief02dac2022-09-26 05:32:05 -0400593 * the coroutine too in the new aiocontext.
594 */
595 while (qemu_get_current_aio_context() != next_aio_context) {
596 job_unlock();
597 aio_co_reschedule_self(next_aio_context);
598 job_lock();
599 next_aio_context = job->aio_context;
600 }
601
602 /* Set by job_enter_cond_locked() before re-entering the coroutine. */
Kevin Wolfda01ff72018-04-13 17:31:02 +0200603 assert(job->busy);
604}
605
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400606/* Called with job_mutex held, but releases it temporarily. */
607static void coroutine_fn job_pause_point_locked(Job *job)
Kevin Wolfda01ff72018-04-13 17:31:02 +0200608{
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400609 assert(job && job_started_locked(job));
Kevin Wolfda01ff72018-04-13 17:31:02 +0200610
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400611 if (!job_should_pause_locked(job)) {
Kevin Wolfda01ff72018-04-13 17:31:02 +0200612 return;
613 }
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400614 if (job_is_cancelled_locked(job)) {
Kevin Wolfda01ff72018-04-13 17:31:02 +0200615 return;
616 }
617
618 if (job->driver->pause) {
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400619 job_unlock();
Kevin Wolfda01ff72018-04-13 17:31:02 +0200620 job->driver->pause(job);
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400621 job_lock();
Kevin Wolfda01ff72018-04-13 17:31:02 +0200622 }
623
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400624 if (job_should_pause_locked(job) && !job_is_cancelled_locked(job)) {
Kevin Wolfda01ff72018-04-13 17:31:02 +0200625 JobStatus status = job->status;
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400626 job_state_transition_locked(job, status == JOB_STATUS_READY
627 ? JOB_STATUS_STANDBY
628 : JOB_STATUS_PAUSED);
Kevin Wolfda01ff72018-04-13 17:31:02 +0200629 job->paused = true;
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400630 job_do_yield_locked(job, -1);
Kevin Wolfda01ff72018-04-13 17:31:02 +0200631 job->paused = false;
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400632 job_state_transition_locked(job, status);
Kevin Wolfda01ff72018-04-13 17:31:02 +0200633 }
634
635 if (job->driver->resume) {
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400636 job_unlock();
Kevin Wolfda01ff72018-04-13 17:31:02 +0200637 job->driver->resume(job);
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400638 job_lock();
Kevin Wolfda01ff72018-04-13 17:31:02 +0200639 }
640}
641
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400642void coroutine_fn job_pause_point(Job *job)
643{
644 JOB_LOCK_GUARD();
645 job_pause_point_locked(job);
646}
647
Emanuele Giuseppe Esposito9bd4d3c2022-09-26 05:32:14 -0400648void coroutine_fn job_yield(Job *job)
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400649{
Emanuele Giuseppe Esposito9bd4d3c2022-09-26 05:32:14 -0400650 JOB_LOCK_GUARD();
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400651 assert(job->busy);
652
653 /* Check cancellation *before* setting busy = false, too! */
654 if (job_is_cancelled_locked(job)) {
655 return;
656 }
657
658 if (!job_should_pause_locked(job)) {
659 job_do_yield_locked(job, -1);
660 }
661
662 job_pause_point_locked(job);
663}
664
Kevin Wolf5d43e862018-04-18 16:32:20 +0200665void coroutine_fn job_sleep_ns(Job *job, int64_t ns)
666{
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400667 JOB_LOCK_GUARD();
Kevin Wolf5d43e862018-04-18 16:32:20 +0200668 assert(job->busy);
669
670 /* Check cancellation *before* setting busy = false, too! */
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400671 if (job_is_cancelled_locked(job)) {
Kevin Wolf5d43e862018-04-18 16:32:20 +0200672 return;
673 }
674
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400675 if (!job_should_pause_locked(job)) {
676 job_do_yield_locked(job, qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + ns);
Kevin Wolf5d43e862018-04-18 16:32:20 +0200677 }
678
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400679 job_pause_point_locked(job);
Kevin Wolf5d43e862018-04-18 16:32:20 +0200680}
681
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400682/* Assumes the job_mutex is held */
683static bool job_timer_not_pending_locked(Job *job)
Kevin Wolfb15de822018-04-18 17:10:26 +0200684{
685 return !timer_pending(&job->sleep_timer);
686}
687
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400688void job_pause_locked(Job *job)
Kevin Wolfb15de822018-04-18 17:10:26 +0200689{
690 job->pause_count++;
Vladimir Sementsov-Ogievskiy3ee14832021-01-17 00:46:51 +0300691 if (!job->paused) {
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400692 job_enter_cond_locked(job, NULL);
Vladimir Sementsov-Ogievskiy3ee14832021-01-17 00:46:51 +0300693 }
Kevin Wolfb15de822018-04-18 17:10:26 +0200694}
695
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400696void job_pause(Job *job)
697{
698 JOB_LOCK_GUARD();
699 job_pause_locked(job);
700}
701
702void job_resume_locked(Job *job)
Kevin Wolfb15de822018-04-18 17:10:26 +0200703{
704 assert(job->pause_count > 0);
705 job->pause_count--;
706 if (job->pause_count) {
707 return;
708 }
709
710 /* kick only if no timer is pending */
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400711 job_enter_cond_locked(job, job_timer_not_pending_locked);
Kevin Wolfb15de822018-04-18 17:10:26 +0200712}
713
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400714void job_resume(Job *job)
Kevin Wolfb15de822018-04-18 17:10:26 +0200715{
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400716 JOB_LOCK_GUARD();
717 job_resume_locked(job);
718}
719
720void job_user_pause_locked(Job *job, Error **errp)
721{
722 if (job_apply_verb_locked(job, JOB_VERB_PAUSE, errp)) {
Kevin Wolfb15de822018-04-18 17:10:26 +0200723 return;
724 }
725 if (job->user_paused) {
726 error_setg(errp, "Job is already paused");
727 return;
728 }
729 job->user_paused = true;
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400730 job_pause_locked(job);
Kevin Wolfb15de822018-04-18 17:10:26 +0200731}
732
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400733bool job_user_paused_locked(Job *job)
Kevin Wolfb15de822018-04-18 17:10:26 +0200734{
735 return job->user_paused;
736}
737
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400738void job_user_resume_locked(Job *job, Error **errp)
Kevin Wolfb15de822018-04-18 17:10:26 +0200739{
740 assert(job);
Emanuele Giuseppe Espositoc70b8032022-03-03 10:16:16 -0500741 GLOBAL_STATE_CODE();
Kevin Wolfb15de822018-04-18 17:10:26 +0200742 if (!job->user_paused || job->pause_count <= 0) {
743 error_setg(errp, "Can't resume a job that was not paused");
744 return;
745 }
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400746 if (job_apply_verb_locked(job, JOB_VERB_RESUME, errp)) {
Kevin Wolfb15de822018-04-18 17:10:26 +0200747 return;
748 }
749 if (job->driver->user_resume) {
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400750 job_unlock();
Kevin Wolfb15de822018-04-18 17:10:26 +0200751 job->driver->user_resume(job);
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400752 job_lock();
Kevin Wolfb15de822018-04-18 17:10:26 +0200753 }
754 job->user_paused = false;
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400755 job_resume_locked(job);
Kevin Wolfb15de822018-04-18 17:10:26 +0200756}
757
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400758/* Called with job_mutex held, but releases it temporarily. */
759static void job_do_dismiss_locked(Job *job)
Kevin Wolf4ad35182018-04-19 17:30:16 +0200760{
761 assert(job);
762 job->busy = false;
763 job->paused = false;
764 job->deferred_to_main_loop = true;
765
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400766 job_txn_del_job_locked(job);
Kevin Wolf4ad35182018-04-19 17:30:16 +0200767
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400768 job_state_transition_locked(job, JOB_STATUS_NULL);
769 job_unref_locked(job);
Kevin Wolf4ad35182018-04-19 17:30:16 +0200770}
771
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400772void job_dismiss_locked(Job **jobptr, Error **errp)
Kevin Wolf5f9a6a02018-04-24 17:10:12 +0200773{
774 Job *job = *jobptr;
775 /* similarly to _complete, this is QMP-interface only. */
776 assert(job->id);
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400777 if (job_apply_verb_locked(job, JOB_VERB_DISMISS, errp)) {
Kevin Wolf5f9a6a02018-04-24 17:10:12 +0200778 return;
779 }
780
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400781 job_do_dismiss_locked(job);
Kevin Wolf5f9a6a02018-04-24 17:10:12 +0200782 *jobptr = NULL;
783}
784
Kevin Wolf4ad35182018-04-19 17:30:16 +0200785void job_early_fail(Job *job)
786{
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400787 JOB_LOCK_GUARD();
Kevin Wolf4ad35182018-04-19 17:30:16 +0200788 assert(job->status == JOB_STATUS_CREATED);
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400789 job_do_dismiss_locked(job);
Kevin Wolf4ad35182018-04-19 17:30:16 +0200790}
791
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400792/* Called with job_mutex held. */
793static void job_conclude_locked(Job *job)
Kevin Wolf4ad35182018-04-19 17:30:16 +0200794{
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400795 job_state_transition_locked(job, JOB_STATUS_CONCLUDED);
796 if (job->auto_dismiss || !job_started_locked(job)) {
797 job_do_dismiss_locked(job);
Kevin Wolf4ad35182018-04-19 17:30:16 +0200798 }
799}
800
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400801/* Called with job_mutex held. */
802static void job_update_rc_locked(Job *job)
Kevin Wolf4ad35182018-04-19 17:30:16 +0200803{
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400804 if (!job->ret && job_is_cancelled_locked(job)) {
Kevin Wolf4ad35182018-04-19 17:30:16 +0200805 job->ret = -ECANCELED;
806 }
807 if (job->ret) {
John Snow3d1f8b02018-08-29 21:57:27 -0400808 if (!job->err) {
809 error_setg(&job->err, "%s", strerror(-job->ret));
Kevin Wolf1266c9b2018-05-24 15:26:10 +0200810 }
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400811 job_state_transition_locked(job, JOB_STATUS_ABORTING);
Kevin Wolf4ad35182018-04-19 17:30:16 +0200812 }
813}
814
815static void job_commit(Job *job)
816{
817 assert(!job->ret);
Emanuele Giuseppe Espositoc70b8032022-03-03 10:16:16 -0500818 GLOBAL_STATE_CODE();
Kevin Wolf4ad35182018-04-19 17:30:16 +0200819 if (job->driver->commit) {
820 job->driver->commit(job);
821 }
822}
823
824static void job_abort(Job *job)
825{
826 assert(job->ret);
Emanuele Giuseppe Espositoc70b8032022-03-03 10:16:16 -0500827 GLOBAL_STATE_CODE();
Kevin Wolf4ad35182018-04-19 17:30:16 +0200828 if (job->driver->abort) {
829 job->driver->abort(job);
830 }
831}
832
833static void job_clean(Job *job)
834{
Emanuele Giuseppe Espositoc70b8032022-03-03 10:16:16 -0500835 GLOBAL_STATE_CODE();
Kevin Wolf4ad35182018-04-19 17:30:16 +0200836 if (job->driver->clean) {
837 job->driver->clean(job);
838 }
839}
840
Emanuele Giuseppe Esposito6f592e52022-09-26 05:32:11 -0400841/*
842 * Called with job_mutex held, but releases it temporarily.
843 * Takes AioContext lock internally to invoke a job->driver callback.
844 */
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400845static int job_finalize_single_locked(Job *job)
Kevin Wolf4ad35182018-04-19 17:30:16 +0200846{
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400847 int job_ret;
Emanuele Giuseppe Esposito6f592e52022-09-26 05:32:11 -0400848 AioContext *ctx = job->aio_context;
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400849
850 assert(job_is_completed_locked(job));
Kevin Wolf4ad35182018-04-19 17:30:16 +0200851
852 /* Ensure abort is called for late-transactional failures */
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400853 job_update_rc_locked(job);
Kevin Wolf4ad35182018-04-19 17:30:16 +0200854
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400855 job_ret = job->ret;
856 job_unlock();
Emanuele Giuseppe Esposito6f592e52022-09-26 05:32:11 -0400857 aio_context_acquire(ctx);
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400858
859 if (!job_ret) {
Kevin Wolf4ad35182018-04-19 17:30:16 +0200860 job_commit(job);
861 } else {
862 job_abort(job);
863 }
864 job_clean(job);
865
866 if (job->cb) {
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400867 job->cb(job->opaque, job_ret);
Kevin Wolf4ad35182018-04-19 17:30:16 +0200868 }
869
Emanuele Giuseppe Esposito6f592e52022-09-26 05:32:11 -0400870 aio_context_release(ctx);
871 job_lock();
872
Kevin Wolf4ad35182018-04-19 17:30:16 +0200873 /* Emit events only if we actually started */
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400874 if (job_started_locked(job)) {
875 if (job_is_cancelled_locked(job)) {
876 job_event_cancelled_locked(job);
Kevin Wolf4ad35182018-04-19 17:30:16 +0200877 } else {
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400878 job_event_completed_locked(job);
Kevin Wolf4ad35182018-04-19 17:30:16 +0200879 }
880 }
881
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400882 job_txn_del_job_locked(job);
883 job_conclude_locked(job);
Kevin Wolf4ad35182018-04-19 17:30:16 +0200884 return 0;
885}
886
Emanuele Giuseppe Esposito6f592e52022-09-26 05:32:11 -0400887/*
888 * Called with job_mutex held, but releases it temporarily.
889 * Takes AioContext lock internally to invoke a job->driver callback.
890 */
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400891static void job_cancel_async_locked(Job *job, bool force)
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +0200892{
Emanuele Giuseppe Esposito6f592e52022-09-26 05:32:11 -0400893 AioContext *ctx = job->aio_context;
Emanuele Giuseppe Espositoc70b8032022-03-03 10:16:16 -0500894 GLOBAL_STATE_CODE();
Vladimir Sementsov-Ogievskiy98209332021-02-05 19:37:14 +0300895 if (job->driver->cancel) {
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400896 job_unlock();
Emanuele Giuseppe Esposito6f592e52022-09-26 05:32:11 -0400897 aio_context_acquire(ctx);
Hanna Reitz73895f32021-10-06 17:19:33 +0200898 force = job->driver->cancel(job, force);
Emanuele Giuseppe Esposito6f592e52022-09-26 05:32:11 -0400899 aio_context_release(ctx);
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400900 job_lock();
Hanna Reitz73895f32021-10-06 17:19:33 +0200901 } else {
902 /* No .cancel() means the job will behave as if force-cancelled */
903 force = true;
Vladimir Sementsov-Ogievskiy98209332021-02-05 19:37:14 +0300904 }
Hanna Reitz73895f32021-10-06 17:19:33 +0200905
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +0200906 if (job->user_paused) {
907 /* Do not call job_enter here, the caller will handle it. */
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +0200908 if (job->driver->user_resume) {
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400909 job_unlock();
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +0200910 job->driver->user_resume(job);
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400911 job_lock();
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +0200912 }
Jeff Codye321c052018-08-21 12:26:19 -0400913 job->user_paused = false;
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +0200914 assert(job->pause_count > 0);
915 job->pause_count--;
916 }
Hanna Reitz401dd092021-10-06 17:19:34 +0200917
918 /*
919 * Ignore soft cancel requests after the job is already done
920 * (We will still invoke job->driver->cancel() above, but if the
921 * job driver supports soft cancelling and the job is done, that
922 * should be a no-op, too. We still call it so it can override
923 * @force.)
924 */
925 if (force || !job->deferred_to_main_loop) {
926 job->cancelled = true;
927 /* To prevent 'force == false' overriding a previous 'force == true' */
928 job->force_cancel |= force;
929 }
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +0200930}
931
Emanuele Giuseppe Esposito6f592e52022-09-26 05:32:11 -0400932/*
933 * Called with job_mutex held, but releases it temporarily.
934 * Takes AioContext lock internally to invoke a job->driver callback.
935 */
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400936static void job_completed_txn_abort_locked(Job *job)
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +0200937{
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +0200938 JobTxn *txn = job->txn;
939 Job *other_job;
940
941 if (txn->aborting) {
942 /*
943 * We are cancelled by another job, which will handle everything.
944 */
945 return;
946 }
947 txn->aborting = true;
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400948 job_txn_ref_locked(txn);
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +0200949
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400950 job_ref_locked(job);
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +0200951
952 /* Other jobs are effectively cancelled by us, set the status for
953 * them; this job, however, may or may not be cancelled, depending
954 * on the caller, so leave it. */
955 QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
956 if (other_job != job) {
Hanna Reitz1d4a43e2021-10-06 17:19:31 +0200957 /*
958 * This is a transaction: If one job failed, no result will matter.
959 * Therefore, pass force=true to terminate all other jobs as quickly
960 * as possible.
961 */
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400962 job_cancel_async_locked(other_job, true);
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +0200963 }
964 }
965 while (!QLIST_EMPTY(&txn->jobs)) {
966 other_job = QLIST_FIRST(&txn->jobs);
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400967 if (!job_is_completed_locked(other_job)) {
968 assert(job_cancel_requested_locked(other_job));
969 job_finish_sync_locked(other_job, NULL, NULL);
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +0200970 }
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400971 job_finalize_single_locked(other_job);
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +0200972 }
973
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400974 job_unref_locked(job);
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400975 job_txn_unref_locked(txn);
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +0200976}
977
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400978/* Called with job_mutex held, but releases it temporarily */
979static int job_prepare_locked(Job *job)
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +0200980{
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400981 int ret;
Emanuele Giuseppe Esposito6f592e52022-09-26 05:32:11 -0400982 AioContext *ctx = job->aio_context;
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400983
Emanuele Giuseppe Espositoc70b8032022-03-03 10:16:16 -0500984 GLOBAL_STATE_CODE();
Emanuele Giuseppe Esposito6f592e52022-09-26 05:32:11 -0400985
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +0200986 if (job->ret == 0 && job->driver->prepare) {
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400987 job_unlock();
Emanuele Giuseppe Esposito6f592e52022-09-26 05:32:11 -0400988 aio_context_acquire(ctx);
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400989 ret = job->driver->prepare(job);
Emanuele Giuseppe Esposito6f592e52022-09-26 05:32:11 -0400990 aio_context_release(ctx);
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400991 job_lock();
992 job->ret = ret;
993 job_update_rc_locked(job);
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +0200994 }
Emanuele Giuseppe Esposito6f592e52022-09-26 05:32:11 -0400995
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +0200996 return job->ret;
997}
998
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -0400999/* Called with job_mutex held */
1000static int job_needs_finalize_locked(Job *job)
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +02001001{
1002 return !job->auto_finalize;
1003}
1004
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -04001005/* Called with job_mutex held */
1006static void job_do_finalize_locked(Job *job)
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +02001007{
1008 int rc;
1009 assert(job && job->txn);
1010
1011 /* prepare the transaction to complete */
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -04001012 rc = job_txn_apply_locked(job, job_prepare_locked);
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +02001013 if (rc) {
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -04001014 job_completed_txn_abort_locked(job);
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +02001015 } else {
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -04001016 job_txn_apply_locked(job, job_finalize_single_locked);
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +02001017 }
1018}
1019
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -04001020void job_finalize_locked(Job *job, Error **errp)
1021{
1022 assert(job && job->id);
1023 if (job_apply_verb_locked(job, JOB_VERB_FINALIZE, errp)) {
1024 return;
1025 }
1026 job_do_finalize_locked(job);
1027}
1028
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -04001029/* Called with job_mutex held. */
1030static int job_transition_to_pending_locked(Job *job)
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +02001031{
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -04001032 job_state_transition_locked(job, JOB_STATUS_PENDING);
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +02001033 if (!job->auto_finalize) {
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -04001034 job_event_pending_locked(job);
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +02001035 }
1036 return 0;
1037}
1038
Kevin Wolf2e1795b2018-04-25 14:56:09 +02001039void job_transition_to_ready(Job *job)
1040{
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -04001041 JOB_LOCK_GUARD();
1042 job_state_transition_locked(job, JOB_STATUS_READY);
1043 job_event_ready_locked(job);
Kevin Wolf2e1795b2018-04-25 14:56:09 +02001044}
1045
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -04001046/* Called with job_mutex held. */
1047static void job_completed_txn_success_locked(Job *job)
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +02001048{
1049 JobTxn *txn = job->txn;
1050 Job *other_job;
1051
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -04001052 job_state_transition_locked(job, JOB_STATUS_WAITING);
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +02001053
1054 /*
1055 * Successful completion, see if there are other running jobs in this
1056 * txn.
1057 */
1058 QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -04001059 if (!job_is_completed_locked(other_job)) {
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +02001060 return;
1061 }
1062 assert(other_job->ret == 0);
1063 }
1064
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -04001065 job_txn_apply_locked(job, job_transition_to_pending_locked);
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +02001066
1067 /* If no jobs need manual finalization, automatically do so */
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -04001068 if (job_txn_apply_locked(job, job_needs_finalize_locked) == 0) {
1069 job_do_finalize_locked(job);
Kevin Wolf7eaa8fb2018-04-23 16:06:26 +02001070 }
1071}
1072
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -04001073/* Called with job_mutex held. */
1074static void job_completed_locked(Job *job)
Kevin Wolf3d70ff52018-04-24 16:13:52 +02001075{
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -04001076 assert(job && job->txn && !job_is_completed_locked(job));
Kevin Wolf1266c9b2018-05-24 15:26:10 +02001077
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -04001078 job_update_rc_locked(job);
John Snow404ff282018-08-29 21:57:33 -04001079 trace_job_completed(job, job->ret);
Kevin Wolf3d70ff52018-04-24 16:13:52 +02001080 if (job->ret) {
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -04001081 job_completed_txn_abort_locked(job);
Kevin Wolf3d70ff52018-04-24 16:13:52 +02001082 } else {
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -04001083 job_completed_txn_success_locked(job);
Kevin Wolf3d70ff52018-04-24 16:13:52 +02001084 }
1085}
1086
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -04001087/**
1088 * Useful only as a type shim for aio_bh_schedule_oneshot.
1089 * Called with job_mutex *not* held.
1090 */
John Snowccbfb332018-09-06 09:02:20 -04001091static void job_exit(void *opaque)
1092{
1093 Job *job = (Job *)opaque;
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -04001094 JOB_LOCK_GUARD();
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -04001095 job_ref_locked(job);
Kevin Wolfb5a7a052018-09-07 15:31:22 +02001096
1097 /* This is a lie, we're not quiescent, but still doing the completion
1098 * callbacks. However, completion callbacks tend to involve operations that
1099 * drain block nodes, and if .drained_poll still returned true, we would
1100 * deadlock. */
1101 job->busy = false;
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -04001102 job_event_idle_locked(job);
Kevin Wolfb5a7a052018-09-07 15:31:22 +02001103
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -04001104 job_completed_locked(job);
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -04001105 job_unref_locked(job);
John Snowccbfb332018-09-06 09:02:20 -04001106}
1107
1108/**
1109 * All jobs must allow a pause point before entering their job proper. This
1110 * ensures that jobs can be paused prior to being started, then resumed later.
1111 */
1112static void coroutine_fn job_co_entry(void *opaque)
1113{
1114 Job *job = opaque;
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -04001115 int ret;
John Snowccbfb332018-09-06 09:02:20 -04001116
1117 assert(job && job->driver && job->driver->run);
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -04001118 WITH_JOB_LOCK_GUARD() {
1119 assert(job->aio_context == qemu_get_current_aio_context());
1120 job_pause_point_locked(job);
1121 }
1122 ret = job->driver->run(job, &job->err);
1123 WITH_JOB_LOCK_GUARD() {
1124 job->ret = ret;
1125 job->deferred_to_main_loop = true;
1126 job->busy = true;
1127 }
John Snowccbfb332018-09-06 09:02:20 -04001128 aio_bh_schedule_oneshot(qemu_get_aio_context(), job_exit, job);
1129}
1130
1131void job_start(Job *job)
1132{
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -04001133 assert(qemu_in_main_thread());
1134
1135 WITH_JOB_LOCK_GUARD() {
1136 assert(job && !job_started_locked(job) && job->paused &&
1137 job->driver && job->driver->run);
1138 job->co = qemu_coroutine_create(job_co_entry, job);
1139 job->pause_count--;
1140 job->busy = true;
1141 job->paused = false;
1142 job_state_transition_locked(job, JOB_STATUS_RUNNING);
1143 }
John Snowccbfb332018-09-06 09:02:20 -04001144 aio_co_enter(job->aio_context, job->co);
1145}
1146
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -04001147void job_cancel_locked(Job *job, bool force)
Kevin Wolf3d70ff52018-04-24 16:13:52 +02001148{
1149 if (job->status == JOB_STATUS_CONCLUDED) {
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -04001150 job_do_dismiss_locked(job);
Kevin Wolf3d70ff52018-04-24 16:13:52 +02001151 return;
1152 }
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -04001153 job_cancel_async_locked(job, force);
1154 if (!job_started_locked(job)) {
1155 job_completed_locked(job);
Kevin Wolf3d70ff52018-04-24 16:13:52 +02001156 } else if (job->deferred_to_main_loop) {
Hanna Reitz401dd092021-10-06 17:19:34 +02001157 /*
1158 * job_cancel_async() ignores soft-cancel requests for jobs
1159 * that are already done (i.e. deferred to the main loop). We
1160 * have to check again whether the job is really cancelled.
Hanna Reitz08b83bf2021-10-06 17:19:35 +02001161 * (job_cancel_requested() and job_is_cancelled() are equivalent
1162 * here, because job_cancel_async() will make soft-cancel
1163 * requests no-ops when deferred_to_main_loop is true. We
1164 * choose to call job_is_cancelled() to show that we invoke
1165 * job_completed_txn_abort() only for force-cancelled jobs.)
Hanna Reitz401dd092021-10-06 17:19:34 +02001166 */
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -04001167 if (job_is_cancelled_locked(job)) {
1168 job_completed_txn_abort_locked(job);
Hanna Reitz401dd092021-10-06 17:19:34 +02001169 }
Kevin Wolf3d70ff52018-04-24 16:13:52 +02001170 } else {
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -04001171 job_enter_cond_locked(job, NULL);
Kevin Wolf3d70ff52018-04-24 16:13:52 +02001172 }
1173}
1174
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -04001175void job_user_cancel_locked(Job *job, bool force, Error **errp)
1176{
1177 if (job_apply_verb_locked(job, JOB_VERB_CANCEL, errp)) {
1178 return;
1179 }
1180 job_cancel_locked(job, force);
1181}
1182
Emanuele Giuseppe Esposito9bd4d3c2022-09-26 05:32:14 -04001183/* A wrapper around job_cancel_locked() taking an Error ** parameter so it may
1184 * be used with job_finish_sync_locked() without the need for (rather nasty)
1185 * function pointer casts there.
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -04001186 *
1187 * Called with job_mutex held.
1188 */
1189static void job_cancel_err_locked(Job *job, Error **errp)
Kevin Wolf3d70ff52018-04-24 16:13:52 +02001190{
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -04001191 job_cancel_locked(job, false);
Kevin Wolf3d70ff52018-04-24 16:13:52 +02001192}
1193
Hanna Reitz4cfb3f02021-10-06 17:19:32 +02001194/**
1195 * Same as job_cancel_err(), but force-cancel.
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -04001196 * Called with job_mutex held.
Hanna Reitz4cfb3f02021-10-06 17:19:32 +02001197 */
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -04001198static void job_force_cancel_err_locked(Job *job, Error **errp)
Kevin Wolf3d70ff52018-04-24 16:13:52 +02001199{
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -04001200 job_cancel_locked(job, true);
1201}
1202
1203int job_cancel_sync_locked(Job *job, bool force)
1204{
1205 if (force) {
1206 return job_finish_sync_locked(job, &job_force_cancel_err_locked, NULL);
1207 } else {
1208 return job_finish_sync_locked(job, &job_cancel_err_locked, NULL);
1209 }
Hanna Reitz4cfb3f02021-10-06 17:19:32 +02001210}
1211
1212int job_cancel_sync(Job *job, bool force)
1213{
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -04001214 JOB_LOCK_GUARD();
1215 return job_cancel_sync_locked(job, force);
Kevin Wolf3d70ff52018-04-24 16:13:52 +02001216}
1217
1218void job_cancel_sync_all(void)
1219{
1220 Job *job;
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -04001221 JOB_LOCK_GUARD();
Kevin Wolf3d70ff52018-04-24 16:13:52 +02001222
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -04001223 while ((job = job_next_locked(NULL))) {
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -04001224 job_cancel_sync_locked(job, true);
Kevin Wolf3d70ff52018-04-24 16:13:52 +02001225 }
1226}
1227
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -04001228int job_complete_sync_locked(Job *job, Error **errp)
Kevin Wolf3d70ff52018-04-24 16:13:52 +02001229{
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -04001230 return job_finish_sync_locked(job, job_complete_locked, errp);
Kevin Wolf3d70ff52018-04-24 16:13:52 +02001231}
1232
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -04001233void job_complete_locked(Job *job, Error **errp)
Kevin Wolf3453d972018-04-23 12:24:16 +02001234{
1235 /* Should not be reachable via external interface for internal jobs */
1236 assert(job->id);
Emanuele Giuseppe Espositoc70b8032022-03-03 10:16:16 -05001237 GLOBAL_STATE_CODE();
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -04001238 if (job_apply_verb_locked(job, JOB_VERB_COMPLETE, errp)) {
Kevin Wolf3453d972018-04-23 12:24:16 +02001239 return;
1240 }
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -04001241 if (job_cancel_requested_locked(job) || !job->driver->complete) {
Kevin Wolf3453d972018-04-23 12:24:16 +02001242 error_setg(errp, "The active block job '%s' cannot be completed",
1243 job->id);
1244 return;
1245 }
1246
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -04001247 job_unlock();
Kevin Wolf3453d972018-04-23 12:24:16 +02001248 job->driver->complete(job, errp);
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -04001249 job_lock();
Kevin Wolf3453d972018-04-23 12:24:16 +02001250}
1251
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -04001252int job_finish_sync_locked(Job *job,
1253 void (*finish)(Job *, Error **errp),
1254 Error **errp)
Kevin Wolf6a74c072018-04-20 15:33:57 +02001255{
1256 Error *local_err = NULL;
1257 int ret;
Emanuele Giuseppe Esposito3ed4f702022-09-26 05:32:06 -04001258 GLOBAL_STATE_CODE();
Kevin Wolf6a74c072018-04-20 15:33:57 +02001259
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -04001260 job_ref_locked(job);
Kevin Wolf6a74c072018-04-20 15:33:57 +02001261
1262 if (finish) {
1263 finish(job, &local_err);
1264 }
1265 if (local_err) {
1266 error_propagate(errp, local_err);
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -04001267 job_unref_locked(job);
Kevin Wolf6a74c072018-04-20 15:33:57 +02001268 return -EBUSY;
1269 }
Kevin Wolfde0fbe62018-08-17 14:58:49 +02001270
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -04001271 job_unlock();
Emanuele Giuseppe Esposito6f592e52022-09-26 05:32:11 -04001272 AIO_WAIT_WHILE_UNLOCKED(job->aio_context,
1273 (job_enter(job), !job_is_completed(job)));
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -04001274 job_lock();
Kevin Wolfde0fbe62018-08-17 14:58:49 +02001275
Emanuele Giuseppe Espositoafe1e8a2022-09-26 05:31:58 -04001276 ret = (job_is_cancelled_locked(job) && job->ret == 0)
1277 ? -ECANCELED : job->ret;
1278 job_unref_locked(job);
Kevin Wolf6a74c072018-04-20 15:33:57 +02001279 return ret;
1280}