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