xref: /openbmc/qemu/include/block/blockjob.h (revision 5f333d79)
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 "block/block.h"
30 
31 /**
32  * BlockJobDriver:
33  *
34  * A class type for block job driver.
35  */
36 typedef struct BlockJobDriver {
37     /** Derived BlockJob struct size */
38     size_t instance_size;
39 
40     /** String describing the operation, part of query-block-jobs QMP API */
41     BlockJobType job_type;
42 
43     /** Optional callback for job types that support setting a speed limit */
44     void (*set_speed)(BlockJob *job, int64_t speed, Error **errp);
45 
46     /** Optional callback for job types that need to forward I/O status reset */
47     void (*iostatus_reset)(BlockJob *job);
48 
49     /**
50      * Optional callback for job types whose completion must be triggered
51      * manually.
52      */
53     void (*complete)(BlockJob *job, Error **errp);
54 
55     /**
56      * If the callback is not NULL, it will be invoked when all the jobs
57      * belonging to the same transaction complete; or upon this job's
58      * completion if it is not in a transaction. Skipped if NULL.
59      *
60      * All jobs will complete with a call to either .commit() or .abort() but
61      * never both.
62      */
63     void (*commit)(BlockJob *job);
64 
65     /**
66      * If the callback is not NULL, it will be invoked when any job in the
67      * same transaction fails; or upon this job's failure (due to error or
68      * cancellation) if it is not in a transaction. Skipped if NULL.
69      *
70      * All jobs will complete with a call to either .commit() or .abort() but
71      * never both.
72      */
73     void (*abort)(BlockJob *job);
74 
75     /**
76      * If the callback is not NULL, it will be invoked when the job transitions
77      * into the paused state.  Paused jobs must not perform any asynchronous
78      * I/O or event loop activity.  This callback is used to quiesce jobs.
79      */
80     void coroutine_fn (*pause)(BlockJob *job);
81 
82     /**
83      * If the callback is not NULL, it will be invoked when the job transitions
84      * out of the paused state.  Any asynchronous I/O or event loop activity
85      * should be restarted from this callback.
86      */
87     void coroutine_fn (*resume)(BlockJob *job);
88 
89     /*
90      * If the callback is not NULL, it will be invoked before the job is
91      * resumed in a new AioContext.  This is the place to move any resources
92      * besides job->blk to the new AioContext.
93      */
94     void (*attached_aio_context)(BlockJob *job, AioContext *new_context);
95 } BlockJobDriver;
96 
97 /**
98  * BlockJob:
99  *
100  * Long-running operation on a BlockDriverState.
101  */
102 struct BlockJob {
103     /** The job type, including the job vtable.  */
104     const BlockJobDriver *driver;
105 
106     /** The block device on which the job is operating.  */
107     BlockBackend *blk;
108 
109     /**
110      * The ID of the block job.
111      */
112     char *id;
113 
114     /**
115      * The coroutine that executes the job.  If not NULL, it is
116      * reentered when busy is false and the job is cancelled.
117      */
118     Coroutine *co;
119 
120     /**
121      * Set to true if the job should cancel itself.  The flag must
122      * always be tested just before toggling the busy flag from false
123      * to true.  After a job has been cancelled, it should only yield
124      * if #aio_poll will ("sooner or later") reenter the coroutine.
125      */
126     bool cancelled;
127 
128     /**
129      * Counter for pause request. If non-zero, the block job is either paused,
130      * or if busy == true will pause itself as soon as possible.
131      */
132     int pause_count;
133 
134     /**
135      * Set to true if the job is paused by user.  Can be unpaused with the
136      * block-job-resume QMP command.
137      */
138     bool user_paused;
139 
140     /**
141      * Set to false by the job while the coroutine has yielded and may be
142      * re-entered by block_job_enter().  There may still be I/O or event loop
143      * activity pending.
144      */
145     bool busy;
146 
147     /**
148      * Set to true by the job while it is in a quiescent state, where
149      * no I/O or event loop activity is pending.
150      */
151     bool paused;
152 
153     /**
154      * Set to true when the job is ready to be completed.
155      */
156     bool ready;
157 
158     /**
159      * Set to true when the job has deferred work to the main loop.
160      */
161     bool deferred_to_main_loop;
162 
163     /** Element of the list of block jobs */
164     QLIST_ENTRY(BlockJob) job_list;
165 
166     /** Status that is published by the query-block-jobs QMP API */
167     BlockDeviceIoStatus iostatus;
168 
169     /** Offset that is published by the query-block-jobs QMP API */
170     int64_t offset;
171 
172     /** Length that is published by the query-block-jobs QMP API */
173     int64_t len;
174 
175     /** Speed that was set with @block_job_set_speed.  */
176     int64_t speed;
177 
178     /** The completion function that will be called when the job completes.  */
179     BlockCompletionFunc *cb;
180 
181     /** Block other operations when block job is running */
182     Error *blocker;
183 
184     /** The opaque value that is passed to the completion function.  */
185     void *opaque;
186 
187     /** Reference count of the block job */
188     int refcnt;
189 
190     /* True if this job has reported completion by calling block_job_completed.
191      */
192     bool completed;
193 
194     /* ret code passed to block_job_completed.
195      */
196     int ret;
197 
198     /** Non-NULL if this job is part of a transaction */
199     BlockJobTxn *txn;
200     QLIST_ENTRY(BlockJob) txn_list;
201 };
202 
203 /**
204  * block_job_next:
205  * @job: A block job, or %NULL.
206  *
207  * Get the next element from the list of block jobs after @job, or the
208  * first one if @job is %NULL.
209  *
210  * Returns the requested job, or %NULL if there are no more jobs left.
211  */
212 BlockJob *block_job_next(BlockJob *job);
213 
214 /**
215  * block_job_get:
216  * @id: The id of the block job.
217  *
218  * Get the block job identified by @id (which must not be %NULL).
219  *
220  * Returns the requested job, or %NULL if it doesn't exist.
221  */
222 BlockJob *block_job_get(const char *id);
223 
224 /**
225  * block_job_create:
226  * @job_id: The id of the newly-created job, or %NULL to have one
227  * generated automatically.
228  * @job_type: The class object for the newly-created job.
229  * @bs: The block
230  * @speed: The maximum speed, in bytes per second, or 0 for unlimited.
231  * @cb: Completion function for the job.
232  * @opaque: Opaque pointer value passed to @cb.
233  * @errp: Error object.
234  *
235  * Create a new long-running block device job and return it.  The job
236  * will call @cb asynchronously when the job completes.  Note that
237  * @bs may have been closed at the time the @cb it is called.  If
238  * this is the case, the job may be reported as either cancelled or
239  * completed.
240  *
241  * This function is not part of the public job interface; it should be
242  * called from a wrapper that is specific to the job type.
243  */
244 void *block_job_create(const char *job_id, const BlockJobDriver *driver,
245                        BlockDriverState *bs, int64_t speed,
246                        BlockCompletionFunc *cb, void *opaque, Error **errp);
247 
248 /**
249  * block_job_sleep_ns:
250  * @job: The job that calls the function.
251  * @clock: The clock to sleep on.
252  * @ns: How many nanoseconds to stop for.
253  *
254  * Put the job to sleep (assuming that it wasn't canceled) for @ns
255  * nanoseconds.  Canceling the job will interrupt the wait immediately.
256  */
257 void block_job_sleep_ns(BlockJob *job, QEMUClockType type, int64_t ns);
258 
259 /**
260  * block_job_yield:
261  * @job: The job that calls the function.
262  *
263  * Yield the block job coroutine.
264  */
265 void block_job_yield(BlockJob *job);
266 
267 /**
268  * block_job_ref:
269  * @bs: The block device.
270  *
271  * Grab a reference to the block job. Should be paired with block_job_unref.
272  */
273 void block_job_ref(BlockJob *job);
274 
275 /**
276  * block_job_unref:
277  * @bs: The block device.
278  *
279  * Release reference to the block job and release resources if it is the last
280  * reference.
281  */
282 void block_job_unref(BlockJob *job);
283 
284 /**
285  * block_job_completed:
286  * @job: The job being completed.
287  * @ret: The status code.
288  *
289  * Call the completion function that was registered at creation time, and
290  * free @job.
291  */
292 void block_job_completed(BlockJob *job, int ret);
293 
294 /**
295  * block_job_set_speed:
296  * @job: The job to set the speed for.
297  * @speed: The new value
298  * @errp: Error object.
299  *
300  * Set a rate-limiting parameter for the job; the actual meaning may
301  * vary depending on the job type.
302  */
303 void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp);
304 
305 /**
306  * block_job_cancel:
307  * @job: The job to be canceled.
308  *
309  * Asynchronously cancel the specified job.
310  */
311 void block_job_cancel(BlockJob *job);
312 
313 /**
314  * block_job_complete:
315  * @job: The job to be completed.
316  * @errp: Error object.
317  *
318  * Asynchronously complete the specified job.
319  */
320 void block_job_complete(BlockJob *job, Error **errp);
321 
322 /**
323  * block_job_is_cancelled:
324  * @job: The job being queried.
325  *
326  * Returns whether the job is scheduled for cancellation.
327  */
328 bool block_job_is_cancelled(BlockJob *job);
329 
330 /**
331  * block_job_query:
332  * @job: The job to get information about.
333  *
334  * Return information about a job.
335  */
336 BlockJobInfo *block_job_query(BlockJob *job);
337 
338 /**
339  * block_job_pause_point:
340  * @job: The job that is ready to pause.
341  *
342  * Pause now if block_job_pause() has been called.  Block jobs that perform
343  * lots of I/O must call this between requests so that the job can be paused.
344  */
345 void coroutine_fn block_job_pause_point(BlockJob *job);
346 
347 /**
348  * block_job_pause:
349  * @job: The job to be paused.
350  *
351  * Asynchronously pause the specified job.
352  */
353 void block_job_pause(BlockJob *job);
354 
355 /**
356  * block_job_resume:
357  * @job: The job to be resumed.
358  *
359  * Resume the specified job.  Must be paired with a preceding block_job_pause.
360  */
361 void block_job_resume(BlockJob *job);
362 
363 /**
364  * block_job_enter:
365  * @job: The job to enter.
366  *
367  * Continue the specified job by entering the coroutine.
368  */
369 void block_job_enter(BlockJob *job);
370 
371 /**
372  * block_job_event_cancelled:
373  * @job: The job whose information is requested.
374  *
375  * Send a BLOCK_JOB_CANCELLED event for the specified job.
376  */
377 void block_job_event_cancelled(BlockJob *job);
378 
379 /**
380  * block_job_ready:
381  * @job: The job which is now ready to complete.
382  * @msg: Error message. Only present on failure.
383  *
384  * Send a BLOCK_JOB_COMPLETED event for the specified job.
385  */
386 void block_job_event_completed(BlockJob *job, const char *msg);
387 
388 /**
389  * block_job_ready:
390  * @job: The job which is now ready to complete.
391  *
392  * Send a BLOCK_JOB_READY event for the specified job.
393  */
394 void block_job_event_ready(BlockJob *job);
395 
396 /**
397  * block_job_cancel_sync:
398  * @job: The job to be canceled.
399  *
400  * Synchronously cancel the job.  The completion callback is called
401  * before the function returns.  The job may actually complete
402  * instead of canceling itself; the circumstances under which this
403  * happens depend on the kind of job that is active.
404  *
405  * Returns the return value from the job if the job actually completed
406  * during the call, or -ECANCELED if it was canceled.
407  */
408 int block_job_cancel_sync(BlockJob *job);
409 
410 /**
411  * block_job_cancel_sync_all:
412  *
413  * Synchronously cancels all jobs using block_job_cancel_sync().
414  */
415 void block_job_cancel_sync_all(void);
416 
417 /**
418  * block_job_complete_sync:
419  * @job: The job to be completed.
420  * @errp: Error object which may be set by block_job_complete(); this is not
421  *        necessarily set on every error, the job return value has to be
422  *        checked as well.
423  *
424  * Synchronously complete the job.  The completion callback is called before the
425  * function returns, unless it is NULL (which is permissible when using this
426  * function).
427  *
428  * Returns the return value from the job.
429  */
430 int block_job_complete_sync(BlockJob *job, Error **errp);
431 
432 /**
433  * block_job_iostatus_reset:
434  * @job: The job whose I/O status should be reset.
435  *
436  * Reset I/O status on @job and on BlockDriverState objects it uses,
437  * other than job->blk.
438  */
439 void block_job_iostatus_reset(BlockJob *job);
440 
441 /**
442  * block_job_error_action:
443  * @job: The job to signal an error for.
444  * @on_err: The error action setting.
445  * @is_read: Whether the operation was a read.
446  * @error: The error that was reported.
447  *
448  * Report an I/O error for a block job and possibly stop the VM.  Return the
449  * action that was selected based on @on_err and @error.
450  */
451 BlockErrorAction block_job_error_action(BlockJob *job, BlockdevOnError on_err,
452                                         int is_read, int error);
453 
454 typedef void BlockJobDeferToMainLoopFn(BlockJob *job, void *opaque);
455 
456 /**
457  * block_job_defer_to_main_loop:
458  * @job: The job
459  * @fn: The function to run in the main loop
460  * @opaque: The opaque value that is passed to @fn
461  *
462  * Execute a given function in the main loop with the BlockDriverState
463  * AioContext acquired.  Block jobs must call bdrv_unref(), bdrv_close(), and
464  * anything that uses bdrv_drain_all() in the main loop.
465  *
466  * The @job AioContext is held while @fn executes.
467  */
468 void block_job_defer_to_main_loop(BlockJob *job,
469                                   BlockJobDeferToMainLoopFn *fn,
470                                   void *opaque);
471 
472 /**
473  * block_job_txn_new:
474  *
475  * Allocate and return a new block job transaction.  Jobs can be added to the
476  * transaction using block_job_txn_add_job().
477  *
478  * The transaction is automatically freed when the last job completes or is
479  * cancelled.
480  *
481  * All jobs in the transaction either complete successfully or fail/cancel as a
482  * group.  Jobs wait for each other before completing.  Cancelling one job
483  * cancels all jobs in the transaction.
484  */
485 BlockJobTxn *block_job_txn_new(void);
486 
487 /**
488  * block_job_txn_unref:
489  *
490  * Release a reference that was previously acquired with block_job_txn_add_job
491  * or block_job_txn_new. If it's the last reference to the object, it will be
492  * freed.
493  */
494 void block_job_txn_unref(BlockJobTxn *txn);
495 
496 /**
497  * block_job_txn_add_job:
498  * @txn: The transaction (may be NULL)
499  * @job: Job to add to the transaction
500  *
501  * Add @job to the transaction.  The @job must not already be in a transaction.
502  * The caller must call either block_job_txn_unref() or block_job_completed()
503  * to release the reference that is automatically grabbed here.
504  */
505 void block_job_txn_add_job(BlockJobTxn *txn, BlockJob *job);
506 
507 #endif
508