xref: /qemu/include/block/blockjob.h (revision 60b412dd)
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     BlockBackend *blk;
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     /** Element of the list of block jobs */
139     QLIST_ENTRY(BlockJob) job_list;
140 
141     /** Status that is published by the query-block-jobs QMP API */
142     BlockDeviceIoStatus iostatus;
143 
144     /** Offset that is published by the query-block-jobs QMP API */
145     int64_t offset;
146 
147     /** Length that is published by the query-block-jobs QMP API */
148     int64_t len;
149 
150     /** Speed that was set with @block_job_set_speed.  */
151     int64_t speed;
152 
153     /** The completion function that will be called when the job completes.  */
154     BlockCompletionFunc *cb;
155 
156     /** Block other operations when block job is running */
157     Error *blocker;
158 
159     /** The opaque value that is passed to the completion function.  */
160     void *opaque;
161 
162     /** Reference count of the block job */
163     int refcnt;
164 
165     /* True if this job has reported completion by calling block_job_completed.
166      */
167     bool completed;
168 
169     /* ret code passed to block_job_completed.
170      */
171     int ret;
172 
173     /** Non-NULL if this job is part of a transaction */
174     BlockJobTxn *txn;
175     QLIST_ENTRY(BlockJob) txn_list;
176 };
177 
178 /**
179  * block_job_next:
180  * @job: A block job, or %NULL.
181  *
182  * Get the next element from the list of block jobs after @job, or the
183  * first one if @job is %NULL.
184  *
185  * Returns the requested job, or %NULL if there are no more jobs left.
186  */
187 BlockJob *block_job_next(BlockJob *job);
188 
189 /**
190  * block_job_create:
191  * @job_type: The class object for the newly-created job.
192  * @bs: The block
193  * @speed: The maximum speed, in bytes per second, or 0 for unlimited.
194  * @cb: Completion function for the job.
195  * @opaque: Opaque pointer value passed to @cb.
196  * @errp: Error object.
197  *
198  * Create a new long-running block device job and return it.  The job
199  * will call @cb asynchronously when the job completes.  Note that
200  * @bs may have been closed at the time the @cb it is called.  If
201  * this is the case, the job may be reported as either cancelled or
202  * completed.
203  *
204  * This function is not part of the public job interface; it should be
205  * called from a wrapper that is specific to the job type.
206  */
207 void *block_job_create(const BlockJobDriver *driver, BlockDriverState *bs,
208                        int64_t speed, BlockCompletionFunc *cb,
209                        void *opaque, Error **errp);
210 
211 /**
212  * block_job_sleep_ns:
213  * @job: The job that calls the function.
214  * @clock: The clock to sleep on.
215  * @ns: How many nanoseconds to stop for.
216  *
217  * Put the job to sleep (assuming that it wasn't canceled) for @ns
218  * nanoseconds.  Canceling the job will interrupt the wait immediately.
219  */
220 void block_job_sleep_ns(BlockJob *job, QEMUClockType type, int64_t ns);
221 
222 /**
223  * block_job_yield:
224  * @job: The job that calls the function.
225  *
226  * Yield the block job coroutine.
227  */
228 void block_job_yield(BlockJob *job);
229 
230 /**
231  * block_job_ref:
232  * @bs: The block device.
233  *
234  * Grab a reference to the block job. Should be paired with block_job_unref.
235  */
236 void block_job_ref(BlockJob *job);
237 
238 /**
239  * block_job_unref:
240  * @bs: The block device.
241  *
242  * Release reference to the block job and release resources if it is the last
243  * reference.
244  */
245 void block_job_unref(BlockJob *job);
246 
247 /**
248  * block_job_completed:
249  * @job: The job being completed.
250  * @ret: The status code.
251  *
252  * Call the completion function that was registered at creation time, and
253  * free @job.
254  */
255 void block_job_completed(BlockJob *job, int ret);
256 
257 /**
258  * block_job_set_speed:
259  * @job: The job to set the speed for.
260  * @speed: The new value
261  * @errp: Error object.
262  *
263  * Set a rate-limiting parameter for the job; the actual meaning may
264  * vary depending on the job type.
265  */
266 void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp);
267 
268 /**
269  * block_job_cancel:
270  * @job: The job to be canceled.
271  *
272  * Asynchronously cancel the specified job.
273  */
274 void block_job_cancel(BlockJob *job);
275 
276 /**
277  * block_job_complete:
278  * @job: The job to be completed.
279  * @errp: Error object.
280  *
281  * Asynchronously complete the specified job.
282  */
283 void block_job_complete(BlockJob *job, Error **errp);
284 
285 /**
286  * block_job_is_cancelled:
287  * @job: The job being queried.
288  *
289  * Returns whether the job is scheduled for cancellation.
290  */
291 bool block_job_is_cancelled(BlockJob *job);
292 
293 /**
294  * block_job_query:
295  * @job: The job to get information about.
296  *
297  * Return information about a job.
298  */
299 BlockJobInfo *block_job_query(BlockJob *job);
300 
301 /**
302  * block_job_pause:
303  * @job: The job to be paused.
304  *
305  * Asynchronously pause the specified job.
306  */
307 void block_job_pause(BlockJob *job);
308 
309 /**
310  * block_job_resume:
311  * @job: The job to be resumed.
312  *
313  * Resume the specified job.  Must be paired with a preceding block_job_pause.
314  */
315 void block_job_resume(BlockJob *job);
316 
317 /**
318  * block_job_enter:
319  * @job: The job to enter.
320  *
321  * Continue the specified job by entering the coroutine.
322  */
323 void block_job_enter(BlockJob *job);
324 
325 /**
326  * block_job_event_cancelled:
327  * @job: The job whose information is requested.
328  *
329  * Send a BLOCK_JOB_CANCELLED event for the specified job.
330  */
331 void block_job_event_cancelled(BlockJob *job);
332 
333 /**
334  * block_job_ready:
335  * @job: The job which is now ready to complete.
336  * @msg: Error message. Only present on failure.
337  *
338  * Send a BLOCK_JOB_COMPLETED event for the specified job.
339  */
340 void block_job_event_completed(BlockJob *job, const char *msg);
341 
342 /**
343  * block_job_ready:
344  * @job: The job which is now ready to complete.
345  *
346  * Send a BLOCK_JOB_READY event for the specified job.
347  */
348 void block_job_event_ready(BlockJob *job);
349 
350 /**
351  * block_job_is_paused:
352  * @job: The job being queried.
353  *
354  * Returns whether the job is currently paused, or will pause
355  * as soon as it reaches a sleeping point.
356  */
357 bool block_job_is_paused(BlockJob *job);
358 
359 /**
360  * block_job_cancel_sync:
361  * @job: The job to be canceled.
362  *
363  * Synchronously cancel the job.  The completion callback is called
364  * before the function returns.  The job may actually complete
365  * instead of canceling itself; the circumstances under which this
366  * happens depend on the kind of job that is active.
367  *
368  * Returns the return value from the job if the job actually completed
369  * during the call, or -ECANCELED if it was canceled.
370  */
371 int block_job_cancel_sync(BlockJob *job);
372 
373 /**
374  * block_job_cancel_sync_all:
375  *
376  * Synchronously cancels all jobs using block_job_cancel_sync().
377  */
378 void block_job_cancel_sync_all(void);
379 
380 /**
381  * block_job_complete_sync:
382  * @job: The job to be completed.
383  * @errp: Error object which may be set by block_job_complete(); this is not
384  *        necessarily set on every error, the job return value has to be
385  *        checked as well.
386  *
387  * Synchronously complete the job.  The completion callback is called before the
388  * function returns, unless it is NULL (which is permissible when using this
389  * function).
390  *
391  * Returns the return value from the job.
392  */
393 int block_job_complete_sync(BlockJob *job, Error **errp);
394 
395 /**
396  * block_job_iostatus_reset:
397  * @job: The job whose I/O status should be reset.
398  *
399  * Reset I/O status on @job and on BlockDriverState objects it uses,
400  * other than job->bs.
401  */
402 void block_job_iostatus_reset(BlockJob *job);
403 
404 /**
405  * block_job_error_action:
406  * @job: The job to signal an error for.
407  * @on_err: The error action setting.
408  * @is_read: Whether the operation was a read.
409  * @error: The error that was reported.
410  *
411  * Report an I/O error for a block job and possibly stop the VM.  Return the
412  * action that was selected based on @on_err and @error.
413  */
414 BlockErrorAction block_job_error_action(BlockJob *job, BlockdevOnError on_err,
415                                         int is_read, int error);
416 
417 typedef void BlockJobDeferToMainLoopFn(BlockJob *job, void *opaque);
418 
419 /**
420  * block_job_defer_to_main_loop:
421  * @job: The job
422  * @fn: The function to run in the main loop
423  * @opaque: The opaque value that is passed to @fn
424  *
425  * Execute a given function in the main loop with the BlockDriverState
426  * AioContext acquired.  Block jobs must call bdrv_unref(), bdrv_close(), and
427  * anything that uses bdrv_drain_all() in the main loop.
428  *
429  * The @job AioContext is held while @fn executes.
430  */
431 void block_job_defer_to_main_loop(BlockJob *job,
432                                   BlockJobDeferToMainLoopFn *fn,
433                                   void *opaque);
434 
435 /**
436  * block_job_txn_new:
437  *
438  * Allocate and return a new block job transaction.  Jobs can be added to the
439  * transaction using block_job_txn_add_job().
440  *
441  * The transaction is automatically freed when the last job completes or is
442  * cancelled.
443  *
444  * All jobs in the transaction either complete successfully or fail/cancel as a
445  * group.  Jobs wait for each other before completing.  Cancelling one job
446  * cancels all jobs in the transaction.
447  */
448 BlockJobTxn *block_job_txn_new(void);
449 
450 /**
451  * block_job_txn_unref:
452  *
453  * Release a reference that was previously acquired with block_job_txn_add_job
454  * or block_job_txn_new. If it's the last reference to the object, it will be
455  * freed.
456  */
457 void block_job_txn_unref(BlockJobTxn *txn);
458 
459 /**
460  * block_job_txn_add_job:
461  * @txn: The transaction (may be NULL)
462  * @job: Job to add to the transaction
463  *
464  * Add @job to the transaction.  The @job must not already be in a transaction.
465  * The caller must call either block_job_txn_unref() or block_job_completed()
466  * to release the reference that is automatically grabbed here.
467  */
468 void block_job_txn_add_job(BlockJobTxn *txn, BlockJob *job);
469 
470 #endif
471