]> git.proxmox.com Git - mirror_qemu.git/blob - include/block/blockjob.h
job: Move state transitions to Job
[mirror_qemu.git] / include / block / blockjob.h
1 /*
2 * Declarations for long-running block device operations
3 *
4 * Copyright (c) 2011 IBM Corp.
5 * Copyright (c) 2012 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 BLOCKJOB_H
27 #define BLOCKJOB_H
28
29 #include "qemu/job.h"
30 #include "block/block.h"
31 #include "qemu/ratelimit.h"
32
33 #define BLOCK_JOB_SLICE_TIME 100000000ULL /* ns */
34
35 typedef struct BlockJobDriver BlockJobDriver;
36 typedef struct BlockJobTxn BlockJobTxn;
37
38 /**
39 * BlockJob:
40 *
41 * Long-running operation on a BlockDriverState.
42 */
43 typedef struct BlockJob {
44 /** Data belonging to the generic Job infrastructure */
45 Job job;
46
47 /** The job type, including the job vtable. */
48 const BlockJobDriver *driver;
49
50 /** The block device on which the job is operating. */
51 BlockBackend *blk;
52
53 /**
54 * The coroutine that executes the job. If not NULL, it is
55 * reentered when busy is false and the job is cancelled.
56 */
57 Coroutine *co;
58
59 /**
60 * Set to true if the job should cancel itself. The flag must
61 * always be tested just before toggling the busy flag from false
62 * to true. After a job has been cancelled, it should only yield
63 * if #aio_poll will ("sooner or later") reenter the coroutine.
64 */
65 bool cancelled;
66
67 /**
68 * Set to true if the job should abort immediately without waiting
69 * for data to be in sync.
70 */
71 bool force;
72
73 /**
74 * Counter for pause request. If non-zero, the block job is either paused,
75 * or if busy == true will pause itself as soon as possible.
76 */
77 int pause_count;
78
79 /**
80 * Set to true if the job is paused by user. Can be unpaused with the
81 * block-job-resume QMP command.
82 */
83 bool user_paused;
84
85 /**
86 * Set to false by the job while the coroutine has yielded and may be
87 * re-entered by block_job_enter(). There may still be I/O or event loop
88 * activity pending. Accessed under block_job_mutex (in blockjob.c).
89 */
90 bool busy;
91
92 /**
93 * Set to true by the job while it is in a quiescent state, where
94 * no I/O or event loop activity is pending.
95 */
96 bool paused;
97
98 /**
99 * Set to true when the job is ready to be completed.
100 */
101 bool ready;
102
103 /**
104 * Set to true when the job has deferred work to the main loop.
105 */
106 bool deferred_to_main_loop;
107
108 /** Status that is published by the query-block-jobs QMP API */
109 BlockDeviceIoStatus iostatus;
110
111 /** Offset that is published by the query-block-jobs QMP API */
112 int64_t offset;
113
114 /** Length that is published by the query-block-jobs QMP API */
115 int64_t len;
116
117 /** Speed that was set with @block_job_set_speed. */
118 int64_t speed;
119
120 /** Rate limiting data structure for implementing @speed. */
121 RateLimit limit;
122
123 /** The completion function that will be called when the job completes. */
124 BlockCompletionFunc *cb;
125
126 /** Block other operations when block job is running */
127 Error *blocker;
128
129 /** BlockDriverStates that are involved in this block job */
130 GSList *nodes;
131
132 /** The opaque value that is passed to the completion function. */
133 void *opaque;
134
135 /** Reference count of the block job */
136 int refcnt;
137
138 /** True when job has reported completion by calling block_job_completed. */
139 bool completed;
140
141 /** ret code passed to block_job_completed. */
142 int ret;
143
144 /**
145 * Timer that is used by @block_job_sleep_ns. Accessed under
146 * block_job_mutex (in blockjob.c).
147 */
148 QEMUTimer sleep_timer;
149
150 /** True if this job should automatically finalize itself */
151 bool auto_finalize;
152
153 /** True if this job should automatically dismiss itself */
154 bool auto_dismiss;
155
156 BlockJobTxn *txn;
157 QLIST_ENTRY(BlockJob) txn_list;
158 } BlockJob;
159
160 typedef enum BlockJobCreateFlags {
161 /* Default behavior */
162 BLOCK_JOB_DEFAULT = 0x00,
163 /* BlockJob is not QMP-created and should not send QMP events */
164 BLOCK_JOB_INTERNAL = 0x01,
165 /* BlockJob requires manual finalize step */
166 BLOCK_JOB_MANUAL_FINALIZE = 0x02,
167 /* BlockJob requires manual dismiss step */
168 BLOCK_JOB_MANUAL_DISMISS = 0x04,
169 } BlockJobCreateFlags;
170
171 /**
172 * block_job_next:
173 * @job: A block job, or %NULL.
174 *
175 * Get the next element from the list of block jobs after @job, or the
176 * first one if @job is %NULL.
177 *
178 * Returns the requested job, or %NULL if there are no more jobs left.
179 */
180 BlockJob *block_job_next(BlockJob *job);
181
182 /**
183 * block_job_get:
184 * @id: The id of the block job.
185 *
186 * Get the block job identified by @id (which must not be %NULL).
187 *
188 * Returns the requested job, or %NULL if it doesn't exist.
189 */
190 BlockJob *block_job_get(const char *id);
191
192 /**
193 * block_job_add_bdrv:
194 * @job: A block job
195 * @name: The name to assign to the new BdrvChild
196 * @bs: A BlockDriverState that is involved in @job
197 * @perm, @shared_perm: Permissions to request on the node
198 *
199 * Add @bs to the list of BlockDriverState that are involved in
200 * @job. This means that all operations will be blocked on @bs while
201 * @job exists.
202 */
203 int block_job_add_bdrv(BlockJob *job, const char *name, BlockDriverState *bs,
204 uint64_t perm, uint64_t shared_perm, Error **errp);
205
206 /**
207 * block_job_remove_all_bdrv:
208 * @job: The block job
209 *
210 * Remove all BlockDriverStates from the list of nodes that are involved in the
211 * job. This removes the blockers added with block_job_add_bdrv().
212 */
213 void block_job_remove_all_bdrv(BlockJob *job);
214
215 /**
216 * block_job_set_speed:
217 * @job: The job to set the speed for.
218 * @speed: The new value
219 * @errp: Error object.
220 *
221 * Set a rate-limiting parameter for the job; the actual meaning may
222 * vary depending on the job type.
223 */
224 void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp);
225
226 /**
227 * block_job_start:
228 * @job: A job that has not yet been started.
229 *
230 * Begins execution of a block job.
231 * Takes ownership of one reference to the job object.
232 */
233 void block_job_start(BlockJob *job);
234
235 /**
236 * block_job_cancel:
237 * @job: The job to be canceled.
238 * @force: Quit a job without waiting for data to be in sync.
239 *
240 * Asynchronously cancel the specified job.
241 */
242 void block_job_cancel(BlockJob *job, bool force);
243
244 /**
245 * block_job_complete:
246 * @job: The job to be completed.
247 * @errp: Error object.
248 *
249 * Asynchronously complete the specified job.
250 */
251 void block_job_complete(BlockJob *job, Error **errp);
252
253
254 /**
255 * block_job_finalize:
256 * @job: The job to fully commit and finish.
257 * @errp: Error object.
258 *
259 * For jobs that have finished their work and are pending
260 * awaiting explicit acknowledgement to commit their work,
261 * This will commit that work.
262 *
263 * FIXME: Make the below statement universally true:
264 * For jobs that support the manual workflow mode, all graph
265 * changes that occur as a result will occur after this command
266 * and before a successful reply.
267 */
268 void block_job_finalize(BlockJob *job, Error **errp);
269
270 /**
271 * block_job_dismiss:
272 * @job: The job to be dismissed.
273 * @errp: Error object.
274 *
275 * Remove a concluded job from the query list.
276 */
277 void block_job_dismiss(BlockJob **job, Error **errp);
278
279 /**
280 * block_job_progress_update:
281 * @job: The job that has made progress
282 * @done: How much progress the job made
283 *
284 * Updates the progress counter of the job.
285 */
286 void block_job_progress_update(BlockJob *job, uint64_t done);
287
288 /**
289 * block_job_progress_set_remaining:
290 * @job: The job whose expected progress end value is set
291 * @remaining: Expected end value of the progress counter of the job
292 *
293 * Sets the expected end value of the progress counter of a job so that a
294 * completion percentage can be calculated when the progress is updated.
295 */
296 void block_job_progress_set_remaining(BlockJob *job, uint64_t remaining);
297
298 /**
299 * block_job_query:
300 * @job: The job to get information about.
301 *
302 * Return information about a job.
303 */
304 BlockJobInfo *block_job_query(BlockJob *job, Error **errp);
305
306 /**
307 * block_job_user_pause:
308 * @job: The job to be paused.
309 *
310 * Asynchronously pause the specified job.
311 * Do not allow a resume until a matching call to block_job_user_resume.
312 */
313 void block_job_user_pause(BlockJob *job, Error **errp);
314
315 /**
316 * block_job_paused:
317 * @job: The job to query.
318 *
319 * Returns true if the job is user-paused.
320 */
321 bool block_job_user_paused(BlockJob *job);
322
323 /**
324 * block_job_user_resume:
325 * @job: The job to be resumed.
326 *
327 * Resume the specified job.
328 * Must be paired with a preceding block_job_user_pause.
329 */
330 void block_job_user_resume(BlockJob *job, Error **errp);
331
332 /**
333 * block_job_user_cancel:
334 * @job: The job to be cancelled.
335 * @force: Quit a job without waiting for data to be in sync.
336 *
337 * Cancels the specified job, but may refuse to do so if the
338 * operation isn't currently meaningful.
339 */
340 void block_job_user_cancel(BlockJob *job, bool force, Error **errp);
341
342 /**
343 * block_job_cancel_sync:
344 * @job: The job to be canceled.
345 *
346 * Synchronously cancel the job. The completion callback is called
347 * before the function returns. The job may actually complete
348 * instead of canceling itself; the circumstances under which this
349 * happens depend on the kind of job that is active.
350 *
351 * Returns the return value from the job if the job actually completed
352 * during the call, or -ECANCELED if it was canceled.
353 */
354 int block_job_cancel_sync(BlockJob *job);
355
356 /**
357 * block_job_cancel_sync_all:
358 *
359 * Synchronously cancels all jobs using block_job_cancel_sync().
360 */
361 void block_job_cancel_sync_all(void);
362
363 /**
364 * block_job_complete_sync:
365 * @job: The job to be completed.
366 * @errp: Error object which may be set by block_job_complete(); this is not
367 * necessarily set on every error, the job return value has to be
368 * checked as well.
369 *
370 * Synchronously complete the job. The completion callback is called before the
371 * function returns, unless it is NULL (which is permissible when using this
372 * function).
373 *
374 * Returns the return value from the job.
375 */
376 int block_job_complete_sync(BlockJob *job, Error **errp);
377
378 /**
379 * block_job_iostatus_reset:
380 * @job: The job whose I/O status should be reset.
381 *
382 * Reset I/O status on @job and on BlockDriverState objects it uses,
383 * other than job->blk.
384 */
385 void block_job_iostatus_reset(BlockJob *job);
386
387 /**
388 * block_job_txn_new:
389 *
390 * Allocate and return a new block job transaction. Jobs can be added to the
391 * transaction using block_job_txn_add_job().
392 *
393 * The transaction is automatically freed when the last job completes or is
394 * cancelled.
395 *
396 * All jobs in the transaction either complete successfully or fail/cancel as a
397 * group. Jobs wait for each other before completing. Cancelling one job
398 * cancels all jobs in the transaction.
399 */
400 BlockJobTxn *block_job_txn_new(void);
401
402 /**
403 * block_job_ref:
404 *
405 * Add a reference to BlockJob refcnt, it will be decreased with
406 * block_job_unref, and then be freed if it comes to be the last
407 * reference.
408 */
409 void block_job_ref(BlockJob *job);
410
411 /**
412 * block_job_unref:
413 *
414 * Release a reference that was previously acquired with block_job_ref
415 * or block_job_create. If it's the last reference to the object, it will be
416 * freed.
417 */
418 void block_job_unref(BlockJob *job);
419
420 /**
421 * block_job_txn_unref:
422 *
423 * Release a reference that was previously acquired with block_job_txn_add_job
424 * or block_job_txn_new. If it's the last reference to the object, it will be
425 * freed.
426 */
427 void block_job_txn_unref(BlockJobTxn *txn);
428
429 /**
430 * block_job_txn_add_job:
431 * @txn: The transaction (may be NULL)
432 * @job: Job to add to the transaction
433 *
434 * Add @job to the transaction. The @job must not already be in a transaction.
435 * The caller must call either block_job_txn_unref() or block_job_completed()
436 * to release the reference that is automatically grabbed here.
437 */
438 void block_job_txn_add_job(BlockJobTxn *txn, BlockJob *job);
439
440 /**
441 * block_job_is_internal:
442 * @job: The job to determine if it is user-visible or not.
443 *
444 * Returns true if the job should not be visible to the management layer.
445 */
446 bool block_job_is_internal(BlockJob *job);
447
448 /**
449 * block_job_driver:
450 *
451 * Returns the driver associated with a block job.
452 */
453 const BlockJobDriver *block_job_driver(BlockJob *job);
454
455 #endif