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