]> git.ipfire.org Git - thirdparty/qemu.git/blame - include/qemu/job.h
job: Move job_finish_sync() to Job
[thirdparty/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
KW
34typedef struct JobDriver JobDriver;
35
36/**
37 * Long-running operation.
38 */
39typedef struct Job {
40 /** The ID of the job. May be NULL for internal jobs. */
41 char *id;
42
43 /** The type of this job. */
44 const JobDriver *driver;
e7c1d78b 45
80fa2c75
KW
46 /** Reference count of the block job */
47 int refcnt;
48
a50c2ab8
KW
49 /** Current state; See @JobStatus for details. */
50 JobStatus status;
51
08be6fe2
KW
52 /** AioContext to run the job coroutine in */
53 AioContext *aio_context;
54
da01ff7f
KW
55 /**
56 * The coroutine that executes the job. If not NULL, it is reentered when
57 * busy is false and the job is cancelled.
58 */
59 Coroutine *co;
60
61 /**
5d43e86e 62 * Timer that is used by @job_sleep_ns. Accessed under job_mutex (in
da01ff7f
KW
63 * job.c).
64 */
65 QEMUTimer sleep_timer;
66
67 /**
68 * Counter for pause request. If non-zero, the block job is either paused,
69 * or if busy == true will pause itself as soon as possible.
70 */
71 int pause_count;
72
73 /**
74 * Set to false by the job while the coroutine has yielded and may be
75 * re-entered by block_job_enter(). There may still be I/O or event loop
76 * activity pending. Accessed under block_job_mutex (in blockjob.c).
77 */
78 bool busy;
79
80 /**
81 * Set to true by the job while it is in a quiescent state, where
82 * no I/O or event loop activity is pending.
83 */
84 bool paused;
85
b15de828
KW
86 /**
87 * Set to true if the job is paused by user. Can be unpaused with the
88 * block-job-resume QMP command.
89 */
90 bool user_paused;
91
daa7f2f9
KW
92 /**
93 * Set to true if the job should cancel itself. The flag must
94 * always be tested just before toggling the busy flag from false
95 * to true. After a job has been cancelled, it should only yield
96 * if #aio_poll will ("sooner or later") reenter the coroutine.
97 */
98 bool cancelled;
99
004e95df
KW
100 /**
101 * Set to true if the job should abort immediately without waiting
102 * for data to be in sync.
103 */
104 bool force_cancel;
105
1908a559
KW
106 /** Set to true when the job has deferred work to the main loop. */
107 bool deferred_to_main_loop;
108
bb02b65c
KW
109 /** True if this job should automatically finalize itself */
110 bool auto_finalize;
111
112 /** True if this job should automatically dismiss itself */
113 bool auto_dismiss;
114
4ad35181
KW
115 /** ret code passed to block_job_completed. */
116 int ret;
117
118 /** The completion function that will be called when the job completes. */
119 BlockCompletionFunc *cb;
120
121 /** The opaque value that is passed to the completion function. */
122 void *opaque;
123
139a9f02
KW
124 /** Notifiers called when a cancelled job is finalised */
125 NotifierList on_finalize_cancelled;
126
127 /** Notifiers called when a successfully completed job is finalised */
128 NotifierList on_finalize_completed;
129
130 /** Notifiers called when the job transitions to PENDING */
131 NotifierList on_pending;
132
e7c1d78b
KW
133 /** Element of the list of jobs */
134 QLIST_ENTRY(Job) job_list;
33e9e9bd
KW
135} Job;
136
137/**
138 * Callbacks and other information about a Job driver.
139 */
140struct JobDriver {
141 /** Derived Job struct size */
142 size_t instance_size;
252291ea
KW
143
144 /** Enum describing the operation */
145 JobType job_type;
80fa2c75 146
da01ff7f
KW
147 /** Mandatory: Entrypoint for the Coroutine. */
148 CoroutineEntry *start;
149
150 /**
151 * If the callback is not NULL, it will be invoked when the job transitions
152 * into the paused state. Paused jobs must not perform any asynchronous
153 * I/O or event loop activity. This callback is used to quiesce jobs.
154 */
155 void coroutine_fn (*pause)(Job *job);
156
157 /**
158 * If the callback is not NULL, it will be invoked when the job transitions
159 * out of the paused state. Any asynchronous I/O or event loop activity
160 * should be restarted from this callback.
161 */
162 void coroutine_fn (*resume)(Job *job);
163
b15de828
KW
164 /**
165 * Called when the job is resumed by the user (i.e. user_paused becomes
166 * false). .user_resume is called before .resume.
167 */
168 void (*user_resume)(Job *job);
169
3453d972
KW
170 /**
171 * Optional callback for job types whose completion must be triggered
172 * manually.
173 */
174 void (*complete)(Job *job, Error **errp);
175
b69f777d
KW
176 /*
177 * If the callback is not NULL, it will be invoked when the job has to be
178 * synchronously cancelled or completed; it should drain any activities
179 * as required to ensure progress.
180 */
181 void (*drain)(Job *job);
182
4ad35181
KW
183 /**
184 * If the callback is not NULL, it will be invoked when all the jobs
185 * belonging to the same transaction complete; or upon this job's
186 * completion if it is not in a transaction. Skipped if NULL.
187 *
188 * All jobs will complete with a call to either .commit() or .abort() but
189 * never both.
190 */
191 void (*commit)(Job *job);
192
193 /**
194 * If the callback is not NULL, it will be invoked when any job in the
195 * same transaction fails; or upon this job's failure (due to error or
196 * cancellation) if it is not in a transaction. Skipped if NULL.
197 *
198 * All jobs will complete with a call to either .commit() or .abort() but
199 * never both.
200 */
201 void (*abort)(Job *job);
202
203 /**
204 * If the callback is not NULL, it will be invoked after a call to either
205 * .commit() or .abort(). Regardless of which callback is invoked after
206 * completion, .clean() will always be called, even if the job does not
207 * belong to a transaction group.
208 */
209 void (*clean)(Job *job);
210
211
80fa2c75
KW
212 /** Called when the job is freed */
213 void (*free)(Job *job);
33e9e9bd
KW
214};
215
bb02b65c
KW
216typedef enum JobCreateFlags {
217 /* Default behavior */
218 JOB_DEFAULT = 0x00,
219 /* Job is not QMP-created and should not send QMP events */
220 JOB_INTERNAL = 0x01,
221 /* Job requires manual finalize step */
222 JOB_MANUAL_FINALIZE = 0x02,
223 /* Job requires manual dismiss step */
224 JOB_MANUAL_DISMISS = 0x04,
225} JobCreateFlags;
226
33e9e9bd
KW
227
228/**
229 * Create a new long-running job and return it.
230 *
231 * @job_id: The id of the newly-created job, or %NULL for internal jobs
232 * @driver: The class object for the newly-created job.
08be6fe2 233 * @ctx: The AioContext to run the job coroutine in.
bb02b65c 234 * @flags: Creation flags for the job. See @JobCreateFlags.
4ad35181
KW
235 * @cb: Completion function for the job.
236 * @opaque: Opaque pointer value passed to @cb.
33e9e9bd
KW
237 * @errp: Error object.
238 */
08be6fe2 239void *job_create(const char *job_id, const JobDriver *driver, AioContext *ctx,
4ad35181 240 int flags, BlockCompletionFunc *cb, void *opaque, Error **errp);
33e9e9bd 241
80fa2c75
KW
242/**
243 * Add a reference to Job refcnt, it will be decreased with job_unref, and then
244 * be freed if it comes to be the last reference.
245 */
246void job_ref(Job *job);
247
248/**
249 * Release a reference that was previously acquired with job_ref() or
250 * job_create(). If it's the last reference to the object, it will be freed.
251 */
252void job_unref(Job *job);
fd61a701 253
139a9f02
KW
254/** To be called when a cancelled job is finalised. */
255void job_event_cancelled(Job *job);
256
257/** To be called when a successfully completed job is finalised. */
258void job_event_completed(Job *job);
259
260/** To be called when the job transitions to PENDING */
261void job_event_pending(Job *job);
262
da01ff7f
KW
263/**
264 * Conditionally enter the job coroutine if the job is ready to run, not
265 * already busy and fn() returns true. fn() is called while under the job_lock
266 * critical section.
267 */
268void job_enter_cond(Job *job, bool(*fn)(Job *job));
269
270/**
271 * @job: A job that has not yet been started.
272 *
273 * Begins execution of a job.
274 * Takes ownership of one reference to the job object.
275 */
276void job_start(Job *job);
277
5d43e86e
KW
278/**
279 * @job: The job to enter.
280 *
281 * Continue the specified job by entering the coroutine.
282 */
283void job_enter(Job *job);
284
da01ff7f
KW
285/**
286 * @job: The job that is ready to pause.
287 *
288 * Pause now if job_pause() has been called. Jobs that perform lots of I/O
289 * must call this between requests so that the job can be paused.
290 */
291void coroutine_fn job_pause_point(Job *job);
292
5d43e86e
KW
293/**
294 * @job: The job that calls the function.
295 * @ns: How many nanoseconds to stop for.
296 *
297 * Put the job to sleep (assuming that it wasn't canceled) for @ns
298 * %QEMU_CLOCK_REALTIME nanoseconds. Canceling the job will immediately
299 * interrupt the wait.
300 */
301void coroutine_fn job_sleep_ns(Job *job, int64_t ns);
302
da01ff7f 303
252291ea
KW
304/** Returns the JobType of a given Job. */
305JobType job_type(const Job *job);
306
307/** Returns the enum string for the JobType of a given Job. */
308const char *job_type_str(const Job *job);
309
daa7f2f9
KW
310/** Returns whether the job is scheduled for cancellation. */
311bool job_is_cancelled(Job *job);
312
dbe5e6c1
KW
313/** Returns whether the job is in a completed state. */
314bool job_is_completed(Job *job);
315
b15de828
KW
316/**
317 * Request @job to pause at the next pause point. Must be paired with
318 * job_resume(). If the job is supposed to be resumed by user action, call
319 * job_user_pause() instead.
320 */
321void job_pause(Job *job);
322
323/** Resumes a @job paused with job_pause. */
324void job_resume(Job *job);
325
326/**
327 * Asynchronously pause the specified @job.
328 * Do not allow a resume until a matching call to job_user_resume.
329 */
330void job_user_pause(Job *job, Error **errp);
331
332/** Returns true if the job is user-paused. */
333bool job_user_paused(Job *job);
334
335/**
336 * Resume the specified @job.
337 * Must be paired with a preceding job_user_pause.
338 */
339void job_user_resume(Job *job, Error **errp);
340
b69f777d
KW
341/*
342 * Drain any activities as required to ensure progress. This can be called in a
343 * loop to synchronously complete a job.
344 */
345void job_drain(Job *job);
346
e7c1d78b
KW
347/**
348 * Get the next element from the list of block jobs after @job, or the
349 * first one if @job is %NULL.
350 *
351 * Returns the requested job, or %NULL if there are no more jobs left.
352 */
353Job *job_next(Job *job);
354
355/**
356 * Get the job identified by @id (which must not be %NULL).
357 *
358 * Returns the requested job, or %NULL if it doesn't exist.
359 */
360Job *job_get(const char *id);
361
a50c2ab8
KW
362/**
363 * Check whether the verb @verb can be applied to @job in its current state.
364 * Returns 0 if the verb can be applied; otherwise errp is set and -EPERM
365 * returned.
366 */
367int job_apply_verb(Job *job, JobVerb verb, Error **errp);
368
4ad35181
KW
369/** The @job could not be started, free it. */
370void job_early_fail(Job *job);
371
3453d972
KW
372/** Asynchronously complete the specified @job. */
373void job_complete(Job *job, Error **errp);;
4ad35181 374
1908a559
KW
375typedef void JobDeferToMainLoopFn(Job *job, void *opaque);
376
377/**
378 * @job: The job
379 * @fn: The function to run in the main loop
380 * @opaque: The opaque value that is passed to @fn
381 *
382 * This function must be called by the main job coroutine just before it
383 * returns. @fn is executed in the main loop with the job AioContext acquired.
384 *
385 * Block jobs must call bdrv_unref(), bdrv_close(), and anything that uses
386 * bdrv_drain_all() in the main loop.
387 *
388 * The @job AioContext is held while @fn executes.
389 */
390void job_defer_to_main_loop(Job *job, JobDeferToMainLoopFn *fn, void *opaque);
391
6a74c075
KW
392/**
393 * Synchronously finishes the given @job. If @finish is given, it is called to
394 * trigger completion or cancellation of the job.
395 *
396 * Returns 0 if the job is successfully completed, -ECANCELED if the job was
397 * cancelled before completing, and -errno in other error cases.
398 */
399int job_finish_sync(Job *job, void (*finish)(Job *, Error **errp), Error **errp);
400
a50c2ab8
KW
401/* TODO To be removed from the public interface */
402void job_state_transition(Job *job, JobStatus s1);
da01ff7f
KW
403void coroutine_fn job_do_yield(Job *job, uint64_t ns);
404bool job_should_pause(Job *job);
405bool job_started(Job *job);
4ad35181
KW
406void job_do_dismiss(Job *job);
407int job_finalize_single(Job *job);
408void job_update_rc(Job *job);
409
410typedef struct BlockJob BlockJob;
411void block_job_txn_del_job(BlockJob *job);
a50c2ab8 412
33e9e9bd 413#endif