2 * Declarations for background jobs
4 * Copyright (c) 2011 IBM Corp.
5 * Copyright (c) 2012, 2018 Red Hat, Inc.
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29 #include "qapi/qapi-types-job.h"
30 #include "qemu/queue.h"
31 #include "qemu/progress_meter.h"
32 #include "qemu/coroutine.h"
33 #include "block/aio.h"
35 typedef struct JobDriver JobDriver
;
36 typedef struct JobTxn JobTxn
;
40 * Long-running operation.
44 /* Fields set at initialization (job_create), and never modified */
46 /** The ID of the job. May be NULL for internal jobs. */
50 * The type of this job.
51 * All callbacks are called with job_mutex *not* held.
53 const JobDriver
*driver
;
56 * The coroutine that executes the job. If not NULL, it is reentered when
57 * busy is false and the job is cancelled.
58 * Initialized in job_start()
62 /** True if this job should automatically finalize itself */
65 /** True if this job should automatically dismiss itself */
69 * The completion function that will be called when the job completes.
70 * Called with AioContext lock held, since many callback implementations
71 * use bdrv_* functions that require to hold the lock.
73 BlockCompletionFunc
*cb
;
75 /** The opaque value that is passed to the completion function. */
78 /* ProgressMeter API is thread-safe */
79 ProgressMeter progress
;
82 * AioContext to run the job coroutine in.
83 * The job Aiocontext can be read when holding *either*
84 * the BQL (so we are in the main loop) or the job_mutex.
85 * It can only be written when we hold *both* BQL
88 AioContext
*aio_context
;
91 /** Protected by job_mutex */
93 /** Reference count of the block job */
96 /** Current state; See @JobStatus for details. */
100 * Timer that is used by @job_sleep_ns. Accessed under job_mutex (in
103 QEMUTimer sleep_timer
;
106 * Counter for pause request. If non-zero, the block job is either paused,
107 * or if busy == true will pause itself as soon as possible.
112 * Set to false by the job while the coroutine has yielded and may be
113 * re-entered by job_enter(). There may still be I/O or event loop activity
114 * pending. Accessed under job_mutex.
116 * When the job is deferred to the main loop, busy is true as long as the
117 * bottom half is still pending.
122 * Set to true by the job while it is in a quiescent state, where
123 * no I/O or event loop activity is pending.
128 * Set to true if the job is paused by user. Can be unpaused with the
129 * block-job-resume QMP command.
134 * Set to true if the job should cancel itself. The flag must
135 * always be tested just before toggling the busy flag from false
136 * to true. After a job has been cancelled, it should only yield
137 * if #aio_poll will ("sooner or later") reenter the coroutine.
142 * Set to true if the job should abort immediately without waiting
143 * for data to be in sync.
147 /** Set to true when the job has deferred work to the main loop. */
148 bool deferred_to_main_loop
;
151 * Return code from @run and/or @prepare callback(s).
152 * Not final until the job has reached the CONCLUDED status.
153 * 0 on success, -errno on failure.
158 * Error object for a failed job.
159 * If job->ret is nonzero and an error object was not set, it will be set
160 * to strerror(-job->ret) during job_completed.
164 /** Notifiers called when a cancelled job is finalised */
165 NotifierList on_finalize_cancelled
;
167 /** Notifiers called when a successfully completed job is finalised */
168 NotifierList on_finalize_completed
;
170 /** Notifiers called when the job transitions to PENDING */
171 NotifierList on_pending
;
173 /** Notifiers called when the job transitions to READY */
174 NotifierList on_ready
;
176 /** Notifiers called when the job coroutine yields or terminates */
177 NotifierList on_idle
;
179 /** Element of the list of jobs */
180 QLIST_ENTRY(Job
) job_list
;
182 /** Transaction this job is part of */
185 /** Element of the list of jobs in a job transaction */
186 QLIST_ENTRY(Job
) txn_list
;
190 * Callbacks and other information about a Job driver.
191 * All callbacks are invoked with job_mutex *not* held.
196 * These fields are initialized when this object is created,
197 * and are never changed afterwards
200 /** Derived Job struct size */
201 size_t instance_size
;
203 /** Enum describing the operation */
207 * Mandatory: Entrypoint for the Coroutine.
209 * This callback will be invoked when moving from CREATED to RUNNING.
211 * If this callback returns nonzero, the job transaction it is part of is
212 * aborted. If it returns zero, the job moves into the WAITING state. If it
213 * is the last job to complete in its transaction, all jobs in the
214 * transaction move from WAITING to PENDING.
216 * This callback must be run in the job's context.
218 int coroutine_fn (*run
)(Job
*job
, Error
**errp
);
221 * Functions run without regard to the BQL that may run in any
222 * arbitrary thread. These functions do not need to be thread-safe
223 * because the caller ensures that they are invoked from one
228 * If the callback is not NULL, it will be invoked when the job transitions
229 * into the paused state. Paused jobs must not perform any asynchronous
230 * I/O or event loop activity. This callback is used to quiesce jobs.
232 void coroutine_fn (*pause
)(Job
*job
);
235 * If the callback is not NULL, it will be invoked when the job transitions
236 * out of the paused state. Any asynchronous I/O or event loop activity
237 * should be restarted from this callback.
239 void coroutine_fn (*resume
)(Job
*job
);
242 * Global state (GS) API. These functions run under the BQL.
244 * See include/block/block-global-state.h for more information about
249 * Called when the job is resumed by the user (i.e. user_paused becomes
250 * false). .user_resume is called before .resume.
252 void (*user_resume
)(Job
*job
);
255 * Optional callback for job types whose completion must be triggered
258 void (*complete
)(Job
*job
, Error
**errp
);
261 * If the callback is not NULL, prepare will be invoked when all the jobs
262 * belonging to the same transaction complete; or upon this job's completion
263 * if it is not in a transaction.
265 * This callback will not be invoked if the job has already failed.
266 * If it fails, abort and then clean will be called.
268 * Called with AioContext lock held, since many callbacs implementations
269 * use bdrv_* functions that require to hold the lock.
271 int (*prepare
)(Job
*job
);
274 * If the callback is not NULL, it will be invoked when all the jobs
275 * belonging to the same transaction complete; or upon this job's
276 * completion if it is not in a transaction. Skipped if NULL.
278 * All jobs will complete with a call to either .commit() or .abort() but
281 * Called with AioContext lock held, since many callback implementations
282 * use bdrv_* functions that require to hold the lock.
284 void (*commit
)(Job
*job
);
287 * If the callback is not NULL, it will be invoked when any job in the
288 * same transaction fails; or upon this job's failure (due to error or
289 * cancellation) if it is not in a transaction. Skipped if NULL.
291 * All jobs will complete with a call to either .commit() or .abort() but
294 * Called with AioContext lock held, since many callback implementations
295 * use bdrv_* functions that require to hold the lock.
297 void (*abort
)(Job
*job
);
300 * If the callback is not NULL, it will be invoked after a call to either
301 * .commit() or .abort(). Regardless of which callback is invoked after
302 * completion, .clean() will always be called, even if the job does not
303 * belong to a transaction group.
305 * Called with AioContext lock held, since many callbacs implementations
306 * use bdrv_* functions that require to hold the lock.
308 void (*clean
)(Job
*job
);
311 * If the callback is not NULL, it will be invoked in job_cancel_async
313 * This function must return true if the job will be cancelled
314 * immediately without any further I/O (mandatory if @force is
315 * true), and false otherwise. This lets the generic job layer
316 * know whether a job has been truly (force-)cancelled, or whether
317 * it is just in a special completion mode (like mirror after
319 * (If the callback is NULL, the job is assumed to terminate
322 * Called with AioContext lock held, since many callback implementations
323 * use bdrv_* functions that require to hold the lock.
325 bool (*cancel
)(Job
*job
, bool force
);
329 * Called when the job is freed.
330 * Called with AioContext lock held, since many callback implementations
331 * use bdrv_* functions that require to hold the lock.
333 void (*free
)(Job
*job
);
336 typedef enum JobCreateFlags
{
337 /* Default behavior */
339 /* Job is not QMP-created and should not send QMP events */
341 /* Job requires manual finalize step */
342 JOB_MANUAL_FINALIZE
= 0x02,
343 /* Job requires manual dismiss step */
344 JOB_MANUAL_DISMISS
= 0x04,
347 extern QemuMutex job_mutex
;
349 #define JOB_LOCK_GUARD() QEMU_LOCK_GUARD(&job_mutex)
351 #define WITH_JOB_LOCK_GUARD() WITH_QEMU_LOCK_GUARD(&job_mutex)
356 * Take the mutex protecting the list of jobs and their status.
357 * Most functions called by the monitor need to call job_lock
358 * and job_unlock manually. On the other hand, function called
359 * by the block jobs themselves and by the block layer will take the
367 * Release the mutex protecting the list of jobs and their status.
369 void job_unlock(void);
372 * Allocate and return a new job transaction. Jobs can be added to the
373 * transaction using job_txn_add_job().
375 * The transaction is automatically freed when the last job completes or is
378 * All jobs in the transaction either complete successfully or fail/cancel as a
379 * group. Jobs wait for each other before completing. Cancelling one job
380 * cancels all jobs in the transaction.
382 JobTxn
*job_txn_new(void);
385 * Release a reference that was previously acquired with job_txn_add_job or
386 * job_txn_new. If it's the last reference to the object, it will be freed.
388 * Called with job lock *not* held.
390 void job_txn_unref(JobTxn
*txn
);
393 * Same as job_txn_unref(), but called with job lock held.
394 * Might release the lock temporarily.
396 void job_txn_unref_locked(JobTxn
*txn
);
399 * Create a new long-running job and return it.
400 * Called with job_mutex *not* held.
402 * @job_id: The id of the newly-created job, or %NULL for internal jobs
403 * @driver: The class object for the newly-created job.
404 * @txn: The transaction this job belongs to, if any. %NULL otherwise.
405 * @ctx: The AioContext to run the job coroutine in.
406 * @flags: Creation flags for the job. See @JobCreateFlags.
407 * @cb: Completion function for the job.
408 * @opaque: Opaque pointer value passed to @cb.
409 * @errp: Error object.
411 void *job_create(const char *job_id
, const JobDriver
*driver
, JobTxn
*txn
,
412 AioContext
*ctx
, int flags
, BlockCompletionFunc
*cb
,
413 void *opaque
, Error
**errp
);
416 * Add a reference to Job refcnt, it will be decreased with job_unref, and then
417 * be freed if it comes to be the last reference.
419 * Called with job lock held.
421 void job_ref_locked(Job
*job
);
424 * Release a reference that was previously acquired with job_ref_locked() or
425 * job_create(). If it's the last reference to the object, it will be freed.
427 * Takes AioContext lock internally to invoke a job->driver callback.
428 * Called with job lock held.
430 void job_unref_locked(Job
*job
);
433 * @job: The job that has made progress
434 * @done: How much progress the job made since the last call
436 * Updates the progress counter of the job.
438 * May be called with mutex held or not held.
440 void job_progress_update(Job
*job
, uint64_t done
);
443 * @job: The job whose expected progress end value is set
444 * @remaining: Missing progress (on top of the current progress counter value)
445 * until the new expected end value is reached
447 * Sets the expected end value of the progress counter of a job so that a
448 * completion percentage can be calculated when the progress is updated.
450 * May be called with mutex held or not held.
452 void job_progress_set_remaining(Job
*job
, uint64_t remaining
);
455 * @job: The job whose expected progress end value is updated
456 * @delta: Value which is to be added to the current expected end
459 * Increases the expected end value of the progress counter of a job.
460 * This is useful for parenthesis operations: If a job has to
461 * conditionally perform a high-priority operation as part of its
462 * progress, it calls this function with the expected operation's
463 * length before, and job_progress_update() afterwards.
464 * (So the operation acts as a parenthesis in regards to the main job
465 * operation running in background.)
467 * May be called with mutex held or not held.
469 void job_progress_increase_remaining(Job
*job
, uint64_t delta
);
472 * Conditionally enter the job coroutine if the job is ready to run, not
473 * already busy and fn() returns true. fn() is called while under the job_lock
476 * Called with job lock held, but might release it temporarily.
478 void job_enter_cond_locked(Job
*job
, bool(*fn
)(Job
*job
));
481 * @job: A job that has not yet been started.
483 * Begins execution of a job.
484 * Takes ownership of one reference to the job object.
486 * Called with job_mutex *not* held.
488 void job_start(Job
*job
);
491 * @job: The job to enter.
493 * Continue the specified job by entering the coroutine.
494 * Called with job_mutex *not* held.
496 void job_enter(Job
*job
);
499 * @job: The job that is ready to pause.
501 * Pause now if job_pause() has been called. Jobs that perform lots of I/O
502 * must call this between requests so that the job can be paused.
504 * Called with job_mutex *not* held.
506 void coroutine_fn
job_pause_point(Job
*job
);
509 * @job: The job that calls the function.
511 * Yield the job coroutine.
512 * Called with job_mutex *not* held.
514 void coroutine_fn
job_yield(Job
*job
);
517 * @job: The job that calls the function.
518 * @ns: How many nanoseconds to stop for.
520 * Put the job to sleep (assuming that it wasn't canceled) for @ns
521 * %QEMU_CLOCK_REALTIME nanoseconds. Canceling the job will immediately
522 * interrupt the wait.
524 * Called with job_mutex *not* held.
526 void coroutine_fn
job_sleep_ns(Job
*job
, int64_t ns
);
528 /** Returns the JobType of a given Job. */
529 JobType
job_type(const Job
*job
);
531 /** Returns the enum string for the JobType of a given Job. */
532 const char *job_type_str(const Job
*job
);
534 /** Returns true if the job should not be visible to the management layer. */
535 bool job_is_internal(Job
*job
);
538 * Returns whether the job is being cancelled.
539 * Called with job_mutex *not* held.
541 bool job_is_cancelled(Job
*job
);
543 /* Same as job_is_cancelled(), but called with job lock held. */
544 bool job_is_cancelled_locked(Job
*job
);
547 * Returns whether the job is scheduled for cancellation (at an
549 * Called with job_mutex *not* held.
551 bool job_cancel_requested(Job
*job
);
554 * Returns whether the job is in a completed state.
555 * Called with job lock held.
557 bool job_is_completed_locked(Job
*job
);
560 * Returns whether the job is ready to be completed.
561 * Called with job_mutex *not* held.
563 bool job_is_ready(Job
*job
);
565 /* Same as job_is_ready(), but called with job lock held. */
566 bool job_is_ready_locked(Job
*job
);
569 * Request @job to pause at the next pause point. Must be paired with
570 * job_resume(). If the job is supposed to be resumed by user action, call
571 * job_user_pause_locked() instead.
573 * Called with job lock *not* held.
575 void job_pause(Job
*job
);
577 /* Same as job_pause(), but called with job lock held. */
578 void job_pause_locked(Job
*job
);
580 /** Resumes a @job paused with job_pause. Called with job lock *not* held. */
581 void job_resume(Job
*job
);
584 * Same as job_resume(), but called with job lock held.
585 * Might release the lock temporarily.
587 void job_resume_locked(Job
*job
);
590 * Asynchronously pause the specified @job.
591 * Do not allow a resume until a matching call to job_user_resume.
592 * Called with job lock held.
594 void job_user_pause_locked(Job
*job
, Error
**errp
);
597 * Returns true if the job is user-paused.
598 * Called with job lock held.
600 bool job_user_paused_locked(Job
*job
);
603 * Resume the specified @job.
604 * Must be paired with a preceding job_user_pause_locked.
605 * Called with job lock held, but might release it temporarily.
607 void job_user_resume_locked(Job
*job
, Error
**errp
);
610 * Get the next element from the list of block jobs after @job, or the
611 * first one if @job is %NULL.
613 * Returns the requested job, or %NULL if there are no more jobs left.
614 * Called with job lock *not* held.
616 Job
*job_next(Job
*job
);
618 /* Same as job_next(), but called with job lock held. */
619 Job
*job_next_locked(Job
*job
);
622 * Get the job identified by @id (which must not be %NULL).
624 * Returns the requested job, or %NULL if it doesn't exist.
625 * Called with job lock held.
627 Job
*job_get_locked(const char *id
);
630 * Check whether the verb @verb can be applied to @job in its current state.
631 * Returns 0 if the verb can be applied; otherwise errp is set and -EPERM
634 * Called with job lock held.
636 int job_apply_verb_locked(Job
*job
, JobVerb verb
, Error
**errp
);
639 * The @job could not be started, free it.
640 * Called with job_mutex *not* held.
642 void job_early_fail(Job
*job
);
645 * Moves the @job from RUNNING to READY.
646 * Called with job_mutex *not* held.
648 void job_transition_to_ready(Job
*job
);
651 * Asynchronously complete the specified @job.
652 * Called with job lock held, but might release it temporarily.
654 void job_complete_locked(Job
*job
, Error
**errp
);
657 * Asynchronously cancel the specified @job. If @force is true, the job should
658 * be cancelled immediately without waiting for a consistent state.
659 * Called with job lock held.
661 void job_cancel_locked(Job
*job
, bool force
);
664 * Cancels the specified job like job_cancel_locked(), but may refuse
665 * to do so if the operation isn't meaningful in the current state of the job.
666 * Called with job lock held.
668 void job_user_cancel_locked(Job
*job
, bool force
, Error
**errp
);
671 * Synchronously cancel the @job. The completion callback is called
672 * before the function returns. If @force is false, the job may
673 * actually complete instead of canceling itself; the circumstances
674 * under which this happens depend on the kind of job that is active.
676 * Returns the return value from the job if the job actually completed
677 * during the call, or -ECANCELED if it was canceled.
679 * Called with job_lock *not* held.
681 int job_cancel_sync(Job
*job
, bool force
);
683 /* Same as job_cancel_sync, but called with job lock held. */
684 int job_cancel_sync_locked(Job
*job
, bool force
);
687 * Synchronously force-cancels all jobs using job_cancel_sync_locked().
689 * Called with job_lock *not* held.
691 void job_cancel_sync_all(void);
694 * @job: The job to be completed.
695 * @errp: Error object which may be set by job_complete_locked(); this is not
696 * necessarily set on every error, the job return value has to be
699 * Synchronously complete the job. The completion callback is called before the
700 * function returns, unless it is NULL (which is permissible when using this
703 * Returns the return value from the job.
704 * Called with job_lock held.
706 int job_complete_sync_locked(Job
*job
, Error
**errp
);
709 * For a @job that has finished its work and is pending awaiting explicit
710 * acknowledgement to commit its work, this will commit that work.
712 * FIXME: Make the below statement universally true:
713 * For jobs that support the manual workflow mode, all graph changes that occur
714 * as a result will occur after this command and before a successful reply.
716 * Called with job lock held.
718 void job_finalize_locked(Job
*job
, Error
**errp
);
721 * Remove the concluded @job from the query list and resets the passed pointer
722 * to %NULL. Returns an error if the job is not actually concluded.
724 * Called with job lock held.
726 void job_dismiss_locked(Job
**job
, Error
**errp
);
729 * Synchronously finishes the given @job. If @finish is given, it is called to
730 * trigger completion or cancellation of the job.
732 * Returns 0 if the job is successfully completed, -ECANCELED if the job was
733 * cancelled before completing, and -errno in other error cases.
735 * Called with job_lock held, but might release it temporarily.
737 int job_finish_sync_locked(Job
*job
, void (*finish
)(Job
*, Error
**errp
),
741 * Sets the @job->aio_context.
742 * Called with job_mutex *not* held.
744 * This function must run in the main thread to protect against
745 * concurrent read in job_finish_sync_locked(), takes the job_mutex
746 * lock to protect against the read in job_do_yield_locked(), and must
747 * be called when the job is quiescent.
749 void job_set_aio_context(Job
*job
, AioContext
*ctx
);