]> Git Repo - qemu.git/blame - include/qemu/job.h
block/mirror: Add MirrorBDSOpaque
[qemu.git] / include / qemu / job.h
CommitLineData
33e9e9bd
KW
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
252291ea 29#include "qapi/qapi-types-block-core.h"
e7c1d78b 30#include "qemu/queue.h"
da01ff7f 31#include "qemu/coroutine.h"
4ad35181 32#include "block/aio.h"
252291ea 33
33e9e9bd 34typedef struct JobDriver JobDriver;
7eaa8fb5
KW
35typedef struct JobTxn JobTxn;
36
33e9e9bd
KW
37
38/**
39 * Long-running operation.
40 */
41typedef struct Job {
42 /** The ID of the job. May be NULL for internal jobs. */
43 char *id;
44
45 /** The type of this job. */
46 const JobDriver *driver;
e7c1d78b 47
80fa2c75
KW
48 /** Reference count of the block job */
49 int refcnt;
50
a50c2ab8
KW
51 /** Current state; See @JobStatus for details. */
52 JobStatus status;
53
08be6fe2
KW
54 /** AioContext to run the job coroutine in */
55 AioContext *aio_context;
56
da01ff7f
KW
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 */
61 Coroutine *co;
62
63 /**
5d43e86e 64 * Timer that is used by @job_sleep_ns. Accessed under job_mutex (in
da01ff7f
KW
65 * job.c).
66 */
67 QEMUTimer sleep_timer;
68
69 /**
70 * Counter for pause request. If non-zero, the block job is either paused,
71 * or if busy == true will pause itself as soon as possible.
72 */
73 int pause_count;
74
75 /**
76 * Set to false by the job while the coroutine has yielded and may be
3d70ff53
KW
77 * re-entered by job_enter(). There may still be I/O or event loop activity
78 * pending. Accessed under block_job_mutex (in blockjob.c).
da01ff7f
KW
79 */
80 bool busy;
81
82 /**
83 * Set to true by the job while it is in a quiescent state, where
84 * no I/O or event loop activity is pending.
85 */
86 bool paused;
87
b15de828
KW
88 /**
89 * Set to true if the job is paused by user. Can be unpaused with the
90 * block-job-resume QMP command.
91 */
92 bool user_paused;
93
daa7f2f9
KW
94 /**
95 * Set to true if the job should cancel itself. The flag must
96 * always be tested just before toggling the busy flag from false
97 * to true. After a job has been cancelled, it should only yield
98 * if #aio_poll will ("sooner or later") reenter the coroutine.
99 */
100 bool cancelled;
101
004e95df
KW
102 /**
103 * Set to true if the job should abort immediately without waiting
104 * for data to be in sync.
105 */
106 bool force_cancel;
107
1908a559
KW
108 /** Set to true when the job has deferred work to the main loop. */
109 bool deferred_to_main_loop;
110
bb02b65c
KW
111 /** True if this job should automatically finalize itself */
112 bool auto_finalize;
113
114 /** True if this job should automatically dismiss itself */
115 bool auto_dismiss;
116
30a5c887
KW
117 /**
118 * Current progress. The unit is arbitrary as long as the ratio between
119 * progress_current and progress_total represents the estimated percentage
120 * of work already done.
121 */
122 int64_t progress_current;
123
124 /** Estimated progress_current value at the completion of the job */
125 int64_t progress_total;
126
1266c9b9
KW
127 /** Error string for a failed job (NULL if, and only if, job->ret == 0) */
128 char *error;
129
3d70ff53 130 /** ret code passed to job_completed. */
4ad35181
KW
131 int ret;
132
133 /** The completion function that will be called when the job completes. */
134 BlockCompletionFunc *cb;
135
136 /** The opaque value that is passed to the completion function. */
137 void *opaque;
138
139a9f02
KW
139 /** Notifiers called when a cancelled job is finalised */
140 NotifierList on_finalize_cancelled;
141
142 /** Notifiers called when a successfully completed job is finalised */
143 NotifierList on_finalize_completed;
144
145 /** Notifiers called when the job transitions to PENDING */
146 NotifierList on_pending;
147
2e1795b5
KW
148 /** Notifiers called when the job transitions to READY */
149 NotifierList on_ready;
150
e7c1d78b
KW
151 /** Element of the list of jobs */
152 QLIST_ENTRY(Job) job_list;
62c9e416 153
7eaa8fb5
KW
154 /** Transaction this job is part of */
155 JobTxn *txn;
156
62c9e416
KW
157 /** Element of the list of jobs in a job transaction */
158 QLIST_ENTRY(Job) txn_list;
33e9e9bd
KW
159} Job;
160
161/**
162 * Callbacks and other information about a Job driver.
163 */
164struct JobDriver {
165 /** Derived Job struct size */
166 size_t instance_size;
252291ea
KW
167
168 /** Enum describing the operation */
169 JobType job_type;
80fa2c75 170
da01ff7f
KW
171 /** Mandatory: Entrypoint for the Coroutine. */
172 CoroutineEntry *start;
173
174 /**
175 * If the callback is not NULL, it will be invoked when the job transitions
176 * into the paused state. Paused jobs must not perform any asynchronous
177 * I/O or event loop activity. This callback is used to quiesce jobs.
178 */
179 void coroutine_fn (*pause)(Job *job);
180
181 /**
182 * If the callback is not NULL, it will be invoked when the job transitions
183 * out of the paused state. Any asynchronous I/O or event loop activity
184 * should be restarted from this callback.
185 */
186 void coroutine_fn (*resume)(Job *job);
187
b15de828
KW
188 /**
189 * Called when the job is resumed by the user (i.e. user_paused becomes
190 * false). .user_resume is called before .resume.
191 */
192 void (*user_resume)(Job *job);
193
3453d972
KW
194 /**
195 * Optional callback for job types whose completion must be triggered
196 * manually.
197 */
198 void (*complete)(Job *job, Error **errp);
199
b69f777d
KW
200 /*
201 * If the callback is not NULL, it will be invoked when the job has to be
202 * synchronously cancelled or completed; it should drain any activities
203 * as required to ensure progress.
204 */
205 void (*drain)(Job *job);
206
7eaa8fb5
KW
207 /**
208 * If the callback is not NULL, prepare will be invoked when all the jobs
209 * belonging to the same transaction complete; or upon this job's completion
210 * if it is not in a transaction.
211 *
212 * This callback will not be invoked if the job has already failed.
213 * If it fails, abort and then clean will be called.
214 */
215 int (*prepare)(Job *job);
216
4ad35181
KW
217 /**
218 * If the callback is not NULL, it will be invoked when all the jobs
219 * belonging to the same transaction complete; or upon this job's
220 * completion if it is not in a transaction. Skipped if NULL.
221 *
222 * All jobs will complete with a call to either .commit() or .abort() but
223 * never both.
224 */
225 void (*commit)(Job *job);
226
227 /**
228 * If the callback is not NULL, it will be invoked when any job in the
229 * same transaction fails; or upon this job's failure (due to error or
230 * cancellation) if it is not in a transaction. Skipped if NULL.
231 *
232 * All jobs will complete with a call to either .commit() or .abort() but
233 * never both.
234 */
235 void (*abort)(Job *job);
236
237 /**
238 * If the callback is not NULL, it will be invoked after a call to either
239 * .commit() or .abort(). Regardless of which callback is invoked after
240 * completion, .clean() will always be called, even if the job does not
241 * belong to a transaction group.
242 */
243 void (*clean)(Job *job);
244
245
80fa2c75
KW
246 /** Called when the job is freed */
247 void (*free)(Job *job);
33e9e9bd
KW
248};
249
bb02b65c
KW
250typedef enum JobCreateFlags {
251 /* Default behavior */
252 JOB_DEFAULT = 0x00,
253 /* Job is not QMP-created and should not send QMP events */
254 JOB_INTERNAL = 0x01,
255 /* Job requires manual finalize step */
256 JOB_MANUAL_FINALIZE = 0x02,
257 /* Job requires manual dismiss step */
258 JOB_MANUAL_DISMISS = 0x04,
259} JobCreateFlags;
260
7eaa8fb5
KW
261/**
262 * Allocate and return a new job transaction. Jobs can be added to the
263 * transaction using job_txn_add_job().
264 *
265 * The transaction is automatically freed when the last job completes or is
266 * cancelled.
267 *
268 * All jobs in the transaction either complete successfully or fail/cancel as a
269 * group. Jobs wait for each other before completing. Cancelling one job
270 * cancels all jobs in the transaction.
271 */
272JobTxn *job_txn_new(void);
273
274/**
275 * Release a reference that was previously acquired with job_txn_add_job or
276 * job_txn_new. If it's the last reference to the object, it will be freed.
277 */
278void job_txn_unref(JobTxn *txn);
279
280/**
281 * @txn: The transaction (may be NULL)
282 * @job: Job to add to the transaction
283 *
284 * Add @job to the transaction. The @job must not already be in a transaction.
3d70ff53
KW
285 * The caller must call either job_txn_unref() or job_completed() to release
286 * the reference that is automatically grabbed here.
7eaa8fb5
KW
287 *
288 * If @txn is NULL, the function does nothing.
289 */
290void job_txn_add_job(JobTxn *txn, Job *job);
33e9e9bd
KW
291
292/**
293 * Create a new long-running job and return it.
294 *
295 * @job_id: The id of the newly-created job, or %NULL for internal jobs
296 * @driver: The class object for the newly-created job.
7eaa8fb5 297 * @txn: The transaction this job belongs to, if any. %NULL otherwise.
08be6fe2 298 * @ctx: The AioContext to run the job coroutine in.
bb02b65c 299 * @flags: Creation flags for the job. See @JobCreateFlags.
4ad35181
KW
300 * @cb: Completion function for the job.
301 * @opaque: Opaque pointer value passed to @cb.
33e9e9bd
KW
302 * @errp: Error object.
303 */
7eaa8fb5
KW
304void *job_create(const char *job_id, const JobDriver *driver, JobTxn *txn,
305 AioContext *ctx, int flags, BlockCompletionFunc *cb,
306 void *opaque, Error **errp);
33e9e9bd 307
80fa2c75
KW
308/**
309 * Add a reference to Job refcnt, it will be decreased with job_unref, and then
310 * be freed if it comes to be the last reference.
311 */
312void job_ref(Job *job);
313
314/**
315 * Release a reference that was previously acquired with job_ref() or
316 * job_create(). If it's the last reference to the object, it will be freed.
317 */
318void job_unref(Job *job);
fd61a701 319
30a5c887
KW
320/**
321 * @job: The job that has made progress
322 * @done: How much progress the job made since the last call
323 *
324 * Updates the progress counter of the job.
325 */
326void job_progress_update(Job *job, uint64_t done);
327
328/**
329 * @job: The job whose expected progress end value is set
330 * @remaining: Missing progress (on top of the current progress counter value)
331 * until the new expected end value is reached
332 *
333 * Sets the expected end value of the progress counter of a job so that a
334 * completion percentage can be calculated when the progress is updated.
335 */
336void job_progress_set_remaining(Job *job, uint64_t remaining);
337
139a9f02
KW
338/** To be called when a cancelled job is finalised. */
339void job_event_cancelled(Job *job);
340
341/** To be called when a successfully completed job is finalised. */
342void job_event_completed(Job *job);
343
da01ff7f
KW
344/**
345 * Conditionally enter the job coroutine if the job is ready to run, not
346 * already busy and fn() returns true. fn() is called while under the job_lock
347 * critical section.
348 */
349void job_enter_cond(Job *job, bool(*fn)(Job *job));
350
351/**
352 * @job: A job that has not yet been started.
353 *
354 * Begins execution of a job.
355 * Takes ownership of one reference to the job object.
356 */
357void job_start(Job *job);
358
5d43e86e
KW
359/**
360 * @job: The job to enter.
361 *
362 * Continue the specified job by entering the coroutine.
363 */
364void job_enter(Job *job);
365
da01ff7f
KW
366/**
367 * @job: The job that is ready to pause.
368 *
369 * Pause now if job_pause() has been called. Jobs that perform lots of I/O
370 * must call this between requests so that the job can be paused.
371 */
372void coroutine_fn job_pause_point(Job *job);
373
198c49cc
KW
374/**
375 * @job: The job that calls the function.
376 *
377 * Yield the job coroutine.
378 */
379void job_yield(Job *job);
380
5d43e86e
KW
381/**
382 * @job: The job that calls the function.
383 * @ns: How many nanoseconds to stop for.
384 *
385 * Put the job to sleep (assuming that it wasn't canceled) for @ns
386 * %QEMU_CLOCK_REALTIME nanoseconds. Canceling the job will immediately
387 * interrupt the wait.
388 */
389void coroutine_fn job_sleep_ns(Job *job, int64_t ns);
390
da01ff7f 391
252291ea
KW
392/** Returns the JobType of a given Job. */
393JobType job_type(const Job *job);
394
395/** Returns the enum string for the JobType of a given Job. */
396const char *job_type_str(const Job *job);
397
456273b0
KW
398/** Returns true if the job should not be visible to the management layer. */
399bool job_is_internal(Job *job);
400
daa7f2f9
KW
401/** Returns whether the job is scheduled for cancellation. */
402bool job_is_cancelled(Job *job);
403
dbe5e6c1
KW
404/** Returns whether the job is in a completed state. */
405bool job_is_completed(Job *job);
406
df956ae2
KW
407/** Returns whether the job is ready to be completed. */
408bool job_is_ready(Job *job);
409
b15de828
KW
410/**
411 * Request @job to pause at the next pause point. Must be paired with
412 * job_resume(). If the job is supposed to be resumed by user action, call
413 * job_user_pause() instead.
414 */
415void job_pause(Job *job);
416
417/** Resumes a @job paused with job_pause. */
418void job_resume(Job *job);
419
420/**
421 * Asynchronously pause the specified @job.
422 * Do not allow a resume until a matching call to job_user_resume.
423 */
424void job_user_pause(Job *job, Error **errp);
425
426/** Returns true if the job is user-paused. */
427bool job_user_paused(Job *job);
428
429/**
430 * Resume the specified @job.
431 * Must be paired with a preceding job_user_pause.
432 */
433void job_user_resume(Job *job, Error **errp);
434
b69f777d
KW
435/*
436 * Drain any activities as required to ensure progress. This can be called in a
437 * loop to synchronously complete a job.
438 */
439void job_drain(Job *job);
440
e7c1d78b
KW
441/**
442 * Get the next element from the list of block jobs after @job, or the
443 * first one if @job is %NULL.
444 *
445 * Returns the requested job, or %NULL if there are no more jobs left.
446 */
447Job *job_next(Job *job);
448
449/**
450 * Get the job identified by @id (which must not be %NULL).
451 *
452 * Returns the requested job, or %NULL if it doesn't exist.
453 */
454Job *job_get(const char *id);
455
a50c2ab8
KW
456/**
457 * Check whether the verb @verb can be applied to @job in its current state.
458 * Returns 0 if the verb can be applied; otherwise errp is set and -EPERM
459 * returned.
460 */
461int job_apply_verb(Job *job, JobVerb verb, Error **errp);
462
4ad35181
KW
463/** The @job could not be started, free it. */
464void job_early_fail(Job *job);
465
2e1795b5
KW
466/** Moves the @job from RUNNING to READY */
467void job_transition_to_ready(Job *job);
468
3d70ff53
KW
469/**
470 * @job: The job being completed.
471 * @ret: The status code.
1266c9b9
KW
472 * @error: The error message for a failing job (only with @ret < 0). If @ret is
473 * negative, but NULL is given for @error, strerror() is used.
3d70ff53
KW
474 *
475 * Marks @job as completed. If @ret is non-zero, the job transaction it is part
476 * of is aborted. If @ret is zero, the job moves into the WAITING state. If it
477 * is the last job to complete in its transaction, all jobs in the transaction
478 * move from WAITING to PENDING.
479 */
1266c9b9 480void job_completed(Job *job, int ret, Error *error);
3d70ff53 481
3453d972 482/** Asynchronously complete the specified @job. */
3d70ff53
KW
483void job_complete(Job *job, Error **errp);
484
485/**
486 * Asynchronously cancel the specified @job. If @force is true, the job should
487 * be cancelled immediately without waiting for a consistent state.
488 */
489void job_cancel(Job *job, bool force);
490
491/**
492 * Cancels the specified job like job_cancel(), but may refuse to do so if the
493 * operation isn't meaningful in the current state of the job.
494 */
495void job_user_cancel(Job *job, bool force, Error **errp);
496
497/**
498 * Synchronously cancel the @job. The completion callback is called
499 * before the function returns. The job may actually complete
500 * instead of canceling itself; the circumstances under which this
501 * happens depend on the kind of job that is active.
502 *
503 * Returns the return value from the job if the job actually completed
504 * during the call, or -ECANCELED if it was canceled.
505 */
506int job_cancel_sync(Job *job);
507
508/** Synchronously cancels all jobs using job_cancel_sync(). */
509void job_cancel_sync_all(void);
510
511/**
512 * @job: The job to be completed.
513 * @errp: Error object which may be set by job_complete(); this is not
514 * necessarily set on every error, the job return value has to be
515 * checked as well.
516 *
517 * Synchronously complete the job. The completion callback is called before the
518 * function returns, unless it is NULL (which is permissible when using this
519 * function).
520 *
521 * Returns the return value from the job.
522 */
523int job_complete_sync(Job *job, Error **errp);
4ad35181 524
7eaa8fb5
KW
525/**
526 * For a @job that has finished its work and is pending awaiting explicit
527 * acknowledgement to commit its work, this will commit that work.
528 *
529 * FIXME: Make the below statement universally true:
530 * For jobs that support the manual workflow mode, all graph changes that occur
531 * as a result will occur after this command and before a successful reply.
532 */
533void job_finalize(Job *job, Error **errp);
534
5f9a6a08
KW
535/**
536 * Remove the concluded @job from the query list and resets the passed pointer
537 * to %NULL. Returns an error if the job is not actually concluded.
538 */
539void job_dismiss(Job **job, Error **errp);
540
1908a559
KW
541typedef void JobDeferToMainLoopFn(Job *job, void *opaque);
542
543/**
544 * @job: The job
545 * @fn: The function to run in the main loop
546 * @opaque: The opaque value that is passed to @fn
547 *
548 * This function must be called by the main job coroutine just before it
549 * returns. @fn is executed in the main loop with the job AioContext acquired.
550 *
551 * Block jobs must call bdrv_unref(), bdrv_close(), and anything that uses
552 * bdrv_drain_all() in the main loop.
553 *
554 * The @job AioContext is held while @fn executes.
555 */
556void job_defer_to_main_loop(Job *job, JobDeferToMainLoopFn *fn, void *opaque);
557
6a74c075
KW
558/**
559 * Synchronously finishes the given @job. If @finish is given, it is called to
560 * trigger completion or cancellation of the job.
561 *
562 * Returns 0 if the job is successfully completed, -ECANCELED if the job was
563 * cancelled before completing, and -errno in other error cases.
564 */
565int job_finish_sync(Job *job, void (*finish)(Job *, Error **errp), Error **errp);
566
33e9e9bd 567#endif
This page took 0.099506 seconds and 4 git commands to generate.