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