master
h 736 lines 22.1 KB
Raw
1 /*
2 * Declarations for background jobs
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 #ifndef JOB_H
27 #define JOB_H
28
29 #include "qapi/qapi-types-job.h"
30 #include "qemu/aiocb.h"
31 #include "qemu/queue.h"
32 #include "qemu/progress_meter.h"
33 #include "qemu/coroutine.h"
34 #include "qemu/aio.h"
35 #include "block/graph-lock.h"
36
37 typedef struct JobDriver JobDriver;
38 typedef struct JobTxn JobTxn;
39
40
41 /**
42 * Long-running operation.
43 */
44 typedef struct Job {
45
46 /* Fields set at initialization (job_create), and never modified */
47
48 /** The ID of the job. May be NULL for internal jobs. */
49 char *id;
50
51 /**
52 * The type of this job.
53 * All callbacks are called with job_mutex *not* held.
54 */
55 const JobDriver *driver;
56
57 /**
58 * The coroutine that executes the job. If not NULL, it is reentered when
59 * busy is false and the job is cancelled.
60 * Initialized in job_start()
61 */
62 Coroutine *co;
63
64 /** True if this job should automatically finalize itself */
65 bool auto_finalize;
66
67 /** True if this job should automatically dismiss itself */
68 bool auto_dismiss;
69
70 /**
71 * The completion function that will be called when the job completes.
72 */
73 BlockCompletionFunc *cb;
74
75 /** The opaque value that is passed to the completion function. */
76 void *opaque;
77
78 /* ProgressMeter API is thread-safe */
79 ProgressMeter progress;
80
81 /**
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
86 * and the job_mutex.
87 */
88 AioContext *aio_context;
89
90
91 /** Protected by job_mutex */
92
93 /** Reference count of the block job */
94 int refcnt;
95
96 /** Current state; See @JobStatus for details. */
97 JobStatus status;
98
99 /**
100 * Timer that is used by @job_sleep_ns. Accessed under job_mutex (in
101 * job.c).
102 */
103 QEMUTimer sleep_timer;
104
105 /**
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.
108 */
109 int pause_count;
110
111 /**
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.
115 *
116 * When the job is deferred to the main loop, busy is true as long as the
117 * bottom half is still pending.
118 */
119 bool busy;
120
121 /**
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.
124 */
125 bool paused;
126
127 /**
128 * Set to true if the job is paused by user. Can be unpaused with the
129 * block-job-resume QMP command.
130 */
131 bool user_paused;
132
133 /**
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.
138 */
139 bool cancelled;
140
141 /**
142 * Set to true if the job should abort immediately without waiting
143 * for data to be in sync.
144 */
145 bool force_cancel;
146
147 /** Set to true when the job has deferred work to the main loop. */
148 bool deferred_to_main_loop;
149
150 /**
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.
154 */
155 int ret;
156
157 /**
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.
161 */
162 Error *err;
163
164 /** Notifiers called when a cancelled job is finalised */
165 NotifierList on_finalize_cancelled;
166
167 /** Notifiers called when a successfully completed job is finalised */
168 NotifierList on_finalize_completed;
169
170 /** Notifiers called when the job transitions to PENDING */
171 NotifierList on_pending;
172
173 /** Notifiers called when the job transitions to READY */
174 NotifierList on_ready;
175
176 /** Notifiers called when the job coroutine yields or terminates */
177 NotifierList on_idle;
178
179 /** Element of the list of jobs */
180 QLIST_ENTRY(Job) job_list;
181
182 /** Transaction this job is part of */
183 JobTxn *txn;
184
185 /** Element of the list of jobs in a job transaction */
186 QLIST_ENTRY(Job) txn_list;
187 } Job;
188
189 /**
190 * Callbacks and other information about a Job driver.
191 * All callbacks are invoked with job_mutex *not* held.
192 */
193 struct JobDriver {
194
195 /*
196 * These fields are initialized when this object is created,
197 * and are never changed afterwards
198 */
199
200 /** Derived Job struct size */
201 size_t instance_size;
202
203 /** Enum describing the operation */
204 JobType job_type;
205
206 /**
207 * Mandatory: Entrypoint for the Coroutine.
208 *
209 * This callback will be invoked when moving from CREATED to RUNNING.
210 *
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.
215 *
216 * This callback must be run in the job's context.
217 */
218 int coroutine_fn (*run)(Job *job, Error **errp);
219
220 /*
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
224 * thread at time.
225 */
226
227 /**
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.
231 */
232 void coroutine_fn (*pause)(Job *job);
233
234 /**
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.
238 */
239 void coroutine_fn (*resume)(Job *job);
240
241 /*
242 * Global state (GS) API. These functions run under the BQL.
243 *
244 * See include/block/block-global-state.h for more information about
245 * the GS API.
246 */
247
248 /**
249 * Called when the job is resumed by the user (i.e. user_paused becomes
250 * false). .user_resume is called before .resume.
251 */
252 void (*user_resume)(Job *job);
253
254 /**
255 * Optional callback for job types whose completion must be triggered
256 * manually.
257 */
258 void (*complete)(Job *job, Error **errp);
259
260 /**
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.
264 *
265 * This callback will not be invoked if the job has already failed.
266 * If it fails, abort and then clean will be called.
267 */
268 int GRAPH_UNLOCKED_PTR (*prepare)(Job *job);
269
270 /**
271 * If the callback is not NULL, it will be invoked when all the jobs
272 * belonging to the same transaction complete; or upon this job's
273 * completion if it is not in a transaction. Skipped if NULL.
274 *
275 * All jobs will complete with a call to either .commit() or .abort() but
276 * never both.
277 */
278 void (*commit)(Job *job);
279
280 /**
281 * If the callback is not NULL, it will be invoked when any job in the
282 * same transaction fails; or upon this job's failure (due to error or
283 * cancellation) if it is not in a transaction. Skipped if NULL.
284 *
285 * All jobs will complete with a call to either .commit() or .abort() but
286 * never both.
287 */
288 void GRAPH_UNLOCKED_PTR (*abort)(Job *job);
289
290 /**
291 * If the callback is not NULL, it will be invoked after a call to either
292 * .commit() or .abort(). Regardless of which callback is invoked after
293 * completion, .clean() will always be called, even if the job does not
294 * belong to a transaction group.
295 */
296 void (*clean)(Job *job);
297
298 /**
299 * If the callback is not NULL, it will be invoked in job_cancel_async
300 *
301 * This function must return true if the job will be cancelled
302 * immediately without any further I/O (mandatory if @force is
303 * true), and false otherwise. This lets the generic job layer
304 * know whether a job has been truly (force-)cancelled, or whether
305 * it is just in a special completion mode (like mirror after
306 * READY).
307 * (If the callback is NULL, the job is assumed to terminate
308 * without I/O.)
309 */
310 bool (*cancel)(Job *job, bool force);
311
312
313 /**
314 * Called when the job is freed.
315 */
316 void (*free)(Job *job);
317 };
318
319 typedef enum JobCreateFlags {
320 /* Default behavior */
321 JOB_DEFAULT = 0x00,
322 /* Job is not QMP-created and should not send QMP events */
323 JOB_INTERNAL = 0x01,
324 /* Job requires manual finalize step */
325 JOB_MANUAL_FINALIZE = 0x02,
326 /* Job requires manual dismiss step */
327 JOB_MANUAL_DISMISS = 0x04,
328 } JobCreateFlags;
329
330 extern QemuMutex job_mutex;
331
332 #define JOB_LOCK_GUARD() QEMU_LOCK_GUARD(&job_mutex)
333
334 #define WITH_JOB_LOCK_GUARD() WITH_QEMU_LOCK_GUARD(&job_mutex)
335
336 /**
337 * job_lock:
338 *
339 * Take the mutex protecting the list of jobs and their status.
340 * Most functions called by the monitor need to call job_lock
341 * and job_unlock manually. On the other hand, function called
342 * by the block jobs themselves and by the block layer will take the
343 * lock for you.
344 */
345 void job_lock(void);
346
347 /**
348 * job_unlock:
349 *
350 * Release the mutex protecting the list of jobs and their status.
351 */
352 void job_unlock(void);
353
354 /**
355 * Allocate and return a new job transaction. Jobs can be added to the
356 * transaction using job_txn_add_job().
357 *
358 * The transaction is automatically freed when the last job completes or is
359 * cancelled.
360 *
361 * All jobs in the transaction either complete successfully or fail/cancel as a
362 * group. Jobs wait for each other before completing. Cancelling one job
363 * cancels all jobs in the transaction.
364 */
365 JobTxn *job_txn_new(void);
366
367 /**
368 * Release a reference that was previously acquired with job_txn_add_job or
369 * job_txn_new. If it's the last reference to the object, it will be freed.
370 *
371 * Called with job lock *not* held.
372 */
373 void job_txn_unref(JobTxn *txn);
374
375 /*
376 * Same as job_txn_unref(), but called with job lock held.
377 * Might release the lock temporarily.
378 */
379 void job_txn_unref_locked(JobTxn *txn);
380
381 /**
382 * Create a new long-running job and return it.
383 * Called with job_mutex *not* held.
384 *
385 * @job_id: The id of the newly-created job, or %NULL for internal jobs
386 * @driver: The class object for the newly-created job.
387 * @txn: The transaction this job belongs to, if any. %NULL otherwise.
388 * @ctx: The AioContext to run the job coroutine in.
389 * @flags: Creation flags for the job. See @JobCreateFlags.
390 * @cb: Completion function for the job.
391 * @opaque: Opaque pointer value passed to @cb.
392 * @errp: Error object.
393 */
394 void *job_create(const char *job_id, const JobDriver *driver, JobTxn *txn,
395 AioContext *ctx, int flags, BlockCompletionFunc *cb,
396 void *opaque, Error **errp);
397
398 /**
399 * Add a reference to Job refcnt, it will be decreased with job_unref, and then
400 * be freed if it comes to be the last reference.
401 *
402 * Called with job lock held.
403 */
404 void job_ref_locked(Job *job);
405
406 /**
407 * Release a reference that was previously acquired with job_ref_locked() or
408 * job_create(). If it's the last reference to the object, it will be freed.
409 *
410 * Called with job lock held.
411 */
412 void job_unref_locked(Job *job);
413
414 /**
415 * @job: The job that has made progress
416 * @done: How much progress the job made since the last call
417 *
418 * Updates the progress counter of the job.
419 *
420 * May be called with mutex held or not held.
421 */
422 void job_progress_update(Job *job, uint64_t done);
423
424 /**
425 * @job: The job whose expected progress end value is set
426 * @remaining: Missing progress (on top of the current progress counter value)
427 * until the new expected end value is reached
428 *
429 * Sets the expected end value of the progress counter of a job so that a
430 * completion percentage can be calculated when the progress is updated.
431 *
432 * May be called with mutex held or not held.
433 */
434 void job_progress_set_remaining(Job *job, uint64_t remaining);
435
436 /**
437 * @job: The job whose expected progress end value is updated
438 * @delta: Value which is to be added to the current expected end
439 * value
440 *
441 * Increases the expected end value of the progress counter of a job.
442 * This is useful for parenthesis operations: If a job has to
443 * conditionally perform a high-priority operation as part of its
444 * progress, it calls this function with the expected operation's
445 * length before, and job_progress_update() afterwards.
446 * (So the operation acts as a parenthesis in regards to the main job
447 * operation running in background.)
448 *
449 * May be called with mutex held or not held.
450 */
451 void job_progress_increase_remaining(Job *job, uint64_t delta);
452
453 /**
454 * Conditionally enter the job coroutine if the job is ready to run, not
455 * already busy and fn() returns true. fn() is called while under the job_lock
456 * critical section.
457 *
458 * Called with job lock held, but might release it temporarily.
459 */
460 void job_enter_cond_locked(Job *job, bool(*fn)(Job *job));
461
462 /**
463 * @job: A job that has not yet been started.
464 *
465 * Begins execution of a job.
466 * Takes ownership of one reference to the job object.
467 *
468 * Called with job_mutex *not* held.
469 */
470 void job_start(Job *job);
471
472 /**
473 * @job: The job to enter.
474 *
475 * Continue the specified job by entering the coroutine.
476 * Called with job_mutex *not* held.
477 */
478 void job_enter(Job *job);
479
480 /**
481 * @job: The job that is ready to pause.
482 *
483 * Pause now if job_pause() has been called. Jobs that perform lots of I/O
484 * must call this between requests so that the job can be paused.
485 *
486 * Called with job_mutex *not* held.
487 */
488 void coroutine_fn GRAPH_UNLOCKED job_pause_point(Job *job);
489
490 /**
491 * @job: The job that calls the function.
492 *
493 * Yield the job coroutine.
494 * Called with job_mutex *not* held.
495 */
496 void coroutine_fn job_yield(Job *job);
497
498 /**
499 * @job: The job that calls the function.
500 * @ns: How many nanoseconds to stop for.
501 *
502 * Put the job to sleep (assuming that it wasn't canceled) for @ns
503 * %QEMU_CLOCK_REALTIME nanoseconds. Canceling the job will immediately
504 * interrupt the wait.
505 *
506 * Called with job_mutex *not* held.
507 */
508 void coroutine_fn job_sleep_ns(Job *job, int64_t ns);
509
510 /** Returns the JobType of a given Job. */
511 JobType job_type(const Job *job);
512
513 /** Returns the enum string for the JobType of a given Job. */
514 const char *job_type_str(const Job *job);
515
516 /** Returns true if the job should not be visible to the management layer. */
517 bool job_is_internal(Job *job);
518
519 /**
520 * Returns whether the job is being cancelled.
521 * Called with job_mutex *not* held.
522 */
523 bool job_is_cancelled(Job *job);
524
525 /* Same as job_is_cancelled(), but called with job lock held. */
526 bool job_is_cancelled_locked(Job *job);
527
528 /**
529 * Returns whether the job is scheduled for cancellation (at an
530 * indefinite point).
531 * Called with job_mutex *not* held.
532 */
533 bool job_cancel_requested(Job *job);
534
535 /**
536 * Returns whether the job is in a completed state.
537 * Called with job lock held.
538 */
539 bool job_is_completed_locked(Job *job);
540
541 /**
542 * Returns whether the job is ready to be completed.
543 * Called with job_mutex *not* held.
544 */
545 bool job_is_ready(Job *job);
546
547 /* Same as job_is_ready(), but called with job lock held. */
548 bool job_is_ready_locked(Job *job);
549
550 /** Returns whether the job is paused. Called with job_mutex *not* held. */
551 bool job_is_paused(Job *job);
552
553 /**
554 * Request @job to pause at the next pause point. Must be paired with
555 * job_resume(). If the job is supposed to be resumed by user action, call
556 * job_user_pause_locked() instead.
557 *
558 * Called with job lock *not* held.
559 */
560 void job_pause(Job *job);
561
562 /* Same as job_pause(), but called with job lock held. */
563 void job_pause_locked(Job *job);
564
565 /** Resumes a @job paused with job_pause. Called with job lock *not* held. */
566 void job_resume(Job *job);
567
568 /*
569 * Same as job_resume(), but called with job lock held.
570 * Might release the lock temporarily.
571 */
572 void job_resume_locked(Job *job);
573
574 /**
575 * Asynchronously pause the specified @job.
576 * Do not allow a resume until a matching call to job_user_resume.
577 * Called with job lock held.
578 */
579 void job_user_pause_locked(Job *job, Error **errp);
580
581 /**
582 * Returns true if the job is user-paused.
583 * Called with job lock held.
584 */
585 bool job_user_paused_locked(Job *job);
586
587 /**
588 * Resume the specified @job.
589 * Must be paired with a preceding job_user_pause_locked.
590 * Called with job lock held, but might release it temporarily.
591 */
592 void job_user_resume_locked(Job *job, Error **errp);
593
594 /**
595 * Get the next element from the list of block jobs after @job, or the
596 * first one if @job is %NULL.
597 *
598 * Returns the requested job, or %NULL if there are no more jobs left.
599 * Called with job lock *not* held.
600 */
601 Job *job_next(Job *job);
602
603 /* Same as job_next(), but called with job lock held. */
604 Job *job_next_locked(Job *job);
605
606 /**
607 * Get the job identified by @id (which must not be %NULL).
608 *
609 * Returns the requested job, or %NULL if it doesn't exist.
610 * Called with job lock held.
611 */
612 Job *job_get_locked(const char *id);
613
614 /**
615 * Check whether the verb @verb can be applied to @job in its current state.
616 * Returns 0 if the verb can be applied; otherwise errp is set and -EPERM
617 * returned.
618 *
619 * Called with job lock held.
620 */
621 int job_apply_verb_locked(Job *job, JobVerb verb, Error **errp);
622
623 /**
624 * The @job could not be started, free it.
625 * Called with job_mutex *not* held.
626 */
627 void job_early_fail(Job *job);
628
629 /**
630 * Moves the @job from RUNNING to READY.
631 * Called with job_mutex *not* held.
632 */
633 void job_transition_to_ready(Job *job);
634
635 /**
636 * Asynchronously complete the specified @job.
637 * Called with job lock held, but might release it temporarily.
638 */
639 void job_complete_locked(Job *job, Error **errp);
640
641 /**
642 * Asynchronously cancel the specified @job. If @force is true, the job should
643 * be cancelled immediately without waiting for a consistent state.
644 * Called with job lock held.
645 */
646 void job_cancel_locked(Job *job, bool force);
647
648 /**
649 * Cancels the specified job like job_cancel_locked(), but may refuse
650 * to do so if the operation isn't meaningful in the current state of the job.
651 * Called with job lock held.
652 */
653 void job_user_cancel_locked(Job *job, bool force, Error **errp);
654
655 /**
656 * Synchronously cancel the @job. The completion callback is called
657 * before the function returns. If @force is false, the job may
658 * actually complete instead of canceling itself; the circumstances
659 * under which this happens depend on the kind of job that is active.
660 *
661 * Returns the return value from the job if the job actually completed
662 * during the call, or -ECANCELED if it was canceled.
663 *
664 * Called with job_lock *not* held.
665 */
666 int job_cancel_sync(Job *job, bool force);
667
668 /* Same as job_cancel_sync, but called with job lock held. */
669 int job_cancel_sync_locked(Job *job, bool force);
670
671 /**
672 * Synchronously force-cancels all jobs using job_cancel_sync_locked().
673 *
674 * Called with job_lock *not* held.
675 */
676 void job_cancel_sync_all(void);
677
678 /**
679 * @job: The job to be completed.
680 * @errp: Error object which may be set by job_complete_locked(); this is not
681 * necessarily set on every error, the job return value has to be
682 * checked as well.
683 *
684 * Synchronously complete the job. The completion callback is called before the
685 * function returns, unless it is NULL (which is permissible when using this
686 * function).
687 *
688 * Returns the return value from the job.
689 * Called with job_lock held.
690 */
691 int job_complete_sync_locked(Job *job, Error **errp);
692
693 /**
694 * For a @job that has finished its work and is pending awaiting explicit
695 * acknowledgement to commit its work, this will commit that work.
696 *
697 * FIXME: Make the below statement universally true:
698 * For jobs that support the manual workflow mode, all graph changes that occur
699 * as a result will occur after this command and before a successful reply.
700 *
701 * Called with job lock held.
702 */
703 void job_finalize_locked(Job *job, Error **errp);
704
705 /**
706 * Remove the concluded @job from the query list and resets the passed pointer
707 * to %NULL. Returns an error if the job is not actually concluded.
708 *
709 * Called with job lock held.
710 */
711 void job_dismiss_locked(Job **job, Error **errp);
712
713 /**
714 * Synchronously finishes the given @job. If @finish is given, it is called to
715 * trigger completion or cancellation of the job.
716 *
717 * Returns 0 if the job is successfully completed, -ECANCELED if the job was
718 * cancelled before completing, and -errno in other error cases.
719 *
720 * Called with job_lock held, but might release it temporarily.
721 */
722 int job_finish_sync_locked(Job *job, void (*finish)(Job *, Error **errp),
723 Error **errp);
724
725 /**
726 * Sets the @job->aio_context.
727 * Called with job_mutex *not* held.
728 *
729 * This function must run in the main thread to protect against
730 * concurrent read in job_finish_sync_locked(), takes the job_mutex
731 * lock to protect against the read in job_do_yield_locked(), and must
732 * be called when the job is quiescent.
733 */
734 void job_set_aio_context(Job *job, AioContext *ctx);
735
736 #endif