xref: /openbmc/qemu/include/block/blockjob.h (revision 91bfcdb0)
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 } BlockJobDriver;
54 
55 /**
56  * BlockJob:
57  *
58  * Long-running operation on a BlockDriverState.
59  */
60 struct BlockJob {
61     /** The job type, including the job vtable.  */
62     const BlockJobDriver *driver;
63 
64     /** The block device on which the job is operating.  */
65     BlockDriverState *bs;
66 
67     /**
68      * The ID of the block job. Currently the BlockBackend name of the BDS
69      * owning the job at the time when the job is started.
70      *
71      * TODO Decouple block job IDs from BlockBackend names
72      */
73     char *id;
74 
75     /**
76      * The coroutine that executes the job.  If not NULL, it is
77      * reentered when busy is false and the job is cancelled.
78      */
79     Coroutine *co;
80 
81     /**
82      * Set to true if the job should cancel itself.  The flag must
83      * always be tested just before toggling the busy flag from false
84      * to true.  After a job has been cancelled, it should only yield
85      * if #aio_poll will ("sooner or later") reenter the coroutine.
86      */
87     bool cancelled;
88 
89     /**
90      * Counter for pause request. If non-zero, the block job is either paused,
91      * or if busy == true will pause itself as soon as possible.
92      */
93     int pause_count;
94 
95     /**
96      * Set to true if the job is paused by user.  Can be unpaused with the
97      * block-job-resume QMP command.
98      */
99     bool user_paused;
100 
101     /**
102      * Set to false by the job while it is in a quiescent state, where
103      * no I/O is pending and the job has yielded on any condition
104      * that is not detected by #aio_poll, such as a timer.
105      */
106     bool busy;
107 
108     /**
109      * Set to true when the job is ready to be completed.
110      */
111     bool ready;
112 
113     /** Status that is published by the query-block-jobs QMP API */
114     BlockDeviceIoStatus iostatus;
115 
116     /** Offset that is published by the query-block-jobs QMP API */
117     int64_t offset;
118 
119     /** Length that is published by the query-block-jobs QMP API */
120     int64_t len;
121 
122     /** Speed that was set with @block_job_set_speed.  */
123     int64_t speed;
124 
125     /** The completion function that will be called when the job completes.  */
126     BlockCompletionFunc *cb;
127 
128     /** Block other operations when block job is running */
129     Error *blocker;
130 
131     /** The opaque value that is passed to the completion function.  */
132     void *opaque;
133 };
134 
135 /**
136  * block_job_create:
137  * @job_type: The class object for the newly-created job.
138  * @bs: The block
139  * @speed: The maximum speed, in bytes per second, or 0 for unlimited.
140  * @cb: Completion function for the job.
141  * @opaque: Opaque pointer value passed to @cb.
142  * @errp: Error object.
143  *
144  * Create a new long-running block device job and return it.  The job
145  * will call @cb asynchronously when the job completes.  Note that
146  * @bs may have been closed at the time the @cb it is called.  If
147  * this is the case, the job may be reported as either cancelled or
148  * completed.
149  *
150  * This function is not part of the public job interface; it should be
151  * called from a wrapper that is specific to the job type.
152  */
153 void *block_job_create(const BlockJobDriver *driver, BlockDriverState *bs,
154                        int64_t speed, BlockCompletionFunc *cb,
155                        void *opaque, Error **errp);
156 
157 /**
158  * block_job_sleep_ns:
159  * @job: The job that calls the function.
160  * @clock: The clock to sleep on.
161  * @ns: How many nanoseconds to stop for.
162  *
163  * Put the job to sleep (assuming that it wasn't canceled) for @ns
164  * nanoseconds.  Canceling the job will interrupt the wait immediately.
165  */
166 void block_job_sleep_ns(BlockJob *job, QEMUClockType type, int64_t ns);
167 
168 /**
169  * block_job_yield:
170  * @job: The job that calls the function.
171  *
172  * Yield the block job coroutine.
173  */
174 void block_job_yield(BlockJob *job);
175 
176 /**
177  * block_job_release:
178  * @bs: The block device.
179  *
180  * Release job resources when an error occurred or job completed.
181  */
182 void block_job_release(BlockDriverState *bs);
183 
184 /**
185  * block_job_completed:
186  * @job: The job being completed.
187  * @ret: The status code.
188  *
189  * Call the completion function that was registered at creation time, and
190  * free @job.
191  */
192 void block_job_completed(BlockJob *job, int ret);
193 
194 /**
195  * block_job_set_speed:
196  * @job: The job to set the speed for.
197  * @speed: The new value
198  * @errp: Error object.
199  *
200  * Set a rate-limiting parameter for the job; the actual meaning may
201  * vary depending on the job type.
202  */
203 void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp);
204 
205 /**
206  * block_job_cancel:
207  * @job: The job to be canceled.
208  *
209  * Asynchronously cancel the specified job.
210  */
211 void block_job_cancel(BlockJob *job);
212 
213 /**
214  * block_job_complete:
215  * @job: The job to be completed.
216  * @errp: Error object.
217  *
218  * Asynchronously complete the specified job.
219  */
220 void block_job_complete(BlockJob *job, Error **errp);
221 
222 /**
223  * block_job_is_cancelled:
224  * @job: The job being queried.
225  *
226  * Returns whether the job is scheduled for cancellation.
227  */
228 bool block_job_is_cancelled(BlockJob *job);
229 
230 /**
231  * block_job_query:
232  * @job: The job to get information about.
233  *
234  * Return information about a job.
235  */
236 BlockJobInfo *block_job_query(BlockJob *job);
237 
238 /**
239  * block_job_pause:
240  * @job: The job to be paused.
241  *
242  * Asynchronously pause the specified job.
243  */
244 void block_job_pause(BlockJob *job);
245 
246 /**
247  * block_job_resume:
248  * @job: The job to be resumed.
249  *
250  * Resume the specified job.  Must be paired with a preceding block_job_pause.
251  */
252 void block_job_resume(BlockJob *job);
253 
254 /**
255  * block_job_enter:
256  * @job: The job to enter.
257  *
258  * Continue the specified job by entering the coroutine.
259  */
260 void block_job_enter(BlockJob *job);
261 
262 /**
263  * block_job_event_cancelled:
264  * @job: The job whose information is requested.
265  *
266  * Send a BLOCK_JOB_CANCELLED event for the specified job.
267  */
268 void block_job_event_cancelled(BlockJob *job);
269 
270 /**
271  * block_job_ready:
272  * @job: The job which is now ready to complete.
273  * @msg: Error message. Only present on failure.
274  *
275  * Send a BLOCK_JOB_COMPLETED event for the specified job.
276  */
277 void block_job_event_completed(BlockJob *job, const char *msg);
278 
279 /**
280  * block_job_ready:
281  * @job: The job which is now ready to complete.
282  *
283  * Send a BLOCK_JOB_READY event for the specified job.
284  */
285 void block_job_event_ready(BlockJob *job);
286 
287 /**
288  * block_job_is_paused:
289  * @job: The job being queried.
290  *
291  * Returns whether the job is currently paused, or will pause
292  * as soon as it reaches a sleeping point.
293  */
294 bool block_job_is_paused(BlockJob *job);
295 
296 /**
297  * block_job_cancel_sync:
298  * @job: The job to be canceled.
299  *
300  * Synchronously cancel the job.  The completion callback is called
301  * before the function returns.  The job may actually complete
302  * instead of canceling itself; the circumstances under which this
303  * happens depend on the kind of job that is active.
304  *
305  * Returns the return value from the job if the job actually completed
306  * during the call, or -ECANCELED if it was canceled.
307  */
308 int block_job_cancel_sync(BlockJob *job);
309 
310 /**
311  * block_job_complete_sync:
312  * @job: The job to be completed.
313  * @errp: Error object which may be set by block_job_complete(); this is not
314  *        necessarily set on every error, the job return value has to be
315  *        checked as well.
316  *
317  * Synchronously complete the job.  The completion callback is called before the
318  * function returns, unless it is NULL (which is permissible when using this
319  * function).
320  *
321  * Returns the return value from the job.
322  */
323 int block_job_complete_sync(BlockJob *job, Error **errp);
324 
325 /**
326  * block_job_iostatus_reset:
327  * @job: The job whose I/O status should be reset.
328  *
329  * Reset I/O status on @job and on BlockDriverState objects it uses,
330  * other than job->bs.
331  */
332 void block_job_iostatus_reset(BlockJob *job);
333 
334 /**
335  * block_job_error_action:
336  * @job: The job to signal an error for.
337  * @bs: The block device on which to set an I/O error.
338  * @on_err: The error action setting.
339  * @is_read: Whether the operation was a read.
340  * @error: The error that was reported.
341  *
342  * Report an I/O error for a block job and possibly stop the VM.  Return the
343  * action that was selected based on @on_err and @error.
344  */
345 BlockErrorAction block_job_error_action(BlockJob *job, BlockDriverState *bs,
346                                         BlockdevOnError on_err,
347                                         int is_read, int error);
348 
349 typedef void BlockJobDeferToMainLoopFn(BlockJob *job, void *opaque);
350 
351 /**
352  * block_job_defer_to_main_loop:
353  * @job: The job
354  * @fn: The function to run in the main loop
355  * @opaque: The opaque value that is passed to @fn
356  *
357  * Execute a given function in the main loop with the BlockDriverState
358  * AioContext acquired.  Block jobs must call bdrv_unref(), bdrv_close(), and
359  * anything that uses bdrv_drain_all() in the main loop.
360  *
361  * The @job AioContext is held while @fn executes.
362  */
363 void block_job_defer_to_main_loop(BlockJob *job,
364                                   BlockJobDeferToMainLoopFn *fn,
365                                   void *opaque);
366 
367 #endif
368