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