nbd: Treat flags vs. command type as separate fields
[qemu/ar7.git] / include / block / blockjob.h
blob4dfb16b43f4fedf26e50a72bf775dc956e118b8e
1 /*
2 * Declarations for long-running block device operations
4 * Copyright (c) 2011 IBM Corp.
5 * Copyright (c) 2012 Red Hat, Inc.
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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.
26 #ifndef BLOCKJOB_H
27 #define BLOCKJOB_H
29 #include "block/block.h"
31 /**
32 * BlockJobDriver:
34 * A class type for block job driver.
36 typedef struct BlockJobDriver {
37 /** Derived BlockJob struct size */
38 size_t instance_size;
40 /** String describing the operation, part of query-block-jobs QMP API */
41 BlockJobType job_type;
43 /** Optional callback for job types that support setting a speed limit */
44 void (*set_speed)(BlockJob *job, int64_t speed, Error **errp);
46 /** Optional callback for job types that need to forward I/O status reset */
47 void (*iostatus_reset)(BlockJob *job);
49 /**
50 * Optional callback for job types whose completion must be triggered
51 * manually.
53 void (*complete)(BlockJob *job, Error **errp);
55 /**
56 * If the callback is not NULL, it will be invoked when all the jobs
57 * belonging to the same transaction complete; or upon this job's
58 * completion if it is not in a transaction. Skipped if NULL.
60 * All jobs will complete with a call to either .commit() or .abort() but
61 * never both.
63 void (*commit)(BlockJob *job);
65 /**
66 * If the callback is not NULL, it will be invoked when any job in the
67 * same transaction fails; or upon this job's failure (due to error or
68 * cancellation) if it is not in a transaction. Skipped if NULL.
70 * All jobs will complete with a call to either .commit() or .abort() but
71 * never both.
73 void (*abort)(BlockJob *job);
75 /**
76 * If the callback is not NULL, it will be invoked when the job transitions
77 * into the paused state. Paused jobs must not perform any asynchronous
78 * I/O or event loop activity. This callback is used to quiesce jobs.
80 void coroutine_fn (*pause)(BlockJob *job);
82 /**
83 * If the callback is not NULL, it will be invoked when the job transitions
84 * out of the paused state. Any asynchronous I/O or event loop activity
85 * should be restarted from this callback.
87 void coroutine_fn (*resume)(BlockJob *job);
90 * If the callback is not NULL, it will be invoked before the job is
91 * resumed in a new AioContext. This is the place to move any resources
92 * besides job->blk to the new AioContext.
94 void (*attached_aio_context)(BlockJob *job, AioContext *new_context);
97 * If the callback is not NULL, it will be invoked when the job has to be
98 * synchronously cancelled or completed; it should drain BlockDriverStates
99 * as required to ensure progress.
101 void (*drain)(BlockJob *job);
102 } BlockJobDriver;
105 * BlockJob:
107 * Long-running operation on a BlockDriverState.
109 struct BlockJob {
110 /** The job type, including the job vtable. */
111 const BlockJobDriver *driver;
113 /** The block device on which the job is operating. */
114 BlockBackend *blk;
117 * The ID of the block job.
119 char *id;
122 * The coroutine that executes the job. If not NULL, it is
123 * reentered when busy is false and the job is cancelled.
125 Coroutine *co;
128 * Set to true if the job should cancel itself. The flag must
129 * always be tested just before toggling the busy flag from false
130 * to true. After a job has been cancelled, it should only yield
131 * if #aio_poll will ("sooner or later") reenter the coroutine.
133 bool cancelled;
136 * Counter for pause request. If non-zero, the block job is either paused,
137 * or if busy == true will pause itself as soon as possible.
139 int pause_count;
142 * Set to true if the job is paused by user. Can be unpaused with the
143 * block-job-resume QMP command.
145 bool user_paused;
148 * Set to false by the job while the coroutine has yielded and may be
149 * re-entered by block_job_enter(). There may still be I/O or event loop
150 * activity pending.
152 bool busy;
155 * Set to true by the job while it is in a quiescent state, where
156 * no I/O or event loop activity is pending.
158 bool paused;
161 * Set to true when the job is ready to be completed.
163 bool ready;
166 * Set to true when the job has deferred work to the main loop.
168 bool deferred_to_main_loop;
170 /** Element of the list of block jobs */
171 QLIST_ENTRY(BlockJob) job_list;
173 /** Status that is published by the query-block-jobs QMP API */
174 BlockDeviceIoStatus iostatus;
176 /** Offset that is published by the query-block-jobs QMP API */
177 int64_t offset;
179 /** Length that is published by the query-block-jobs QMP API */
180 int64_t len;
182 /** Speed that was set with @block_job_set_speed. */
183 int64_t speed;
185 /** The completion function that will be called when the job completes. */
186 BlockCompletionFunc *cb;
188 /** Block other operations when block job is running */
189 Error *blocker;
191 /** BlockDriverStates that are involved in this block job */
192 GSList *nodes;
194 /** The opaque value that is passed to the completion function. */
195 void *opaque;
197 /** Reference count of the block job */
198 int refcnt;
200 /* True if this job has reported completion by calling block_job_completed.
202 bool completed;
204 /* ret code passed to block_job_completed.
206 int ret;
208 /** Non-NULL if this job is part of a transaction */
209 BlockJobTxn *txn;
210 QLIST_ENTRY(BlockJob) txn_list;
214 * block_job_next:
215 * @job: A block job, or %NULL.
217 * Get the next element from the list of block jobs after @job, or the
218 * first one if @job is %NULL.
220 * Returns the requested job, or %NULL if there are no more jobs left.
222 BlockJob *block_job_next(BlockJob *job);
225 * block_job_get:
226 * @id: The id of the block job.
228 * Get the block job identified by @id (which must not be %NULL).
230 * Returns the requested job, or %NULL if it doesn't exist.
232 BlockJob *block_job_get(const char *id);
235 * block_job_create:
236 * @job_id: The id of the newly-created job, or %NULL to have one
237 * generated automatically.
238 * @job_type: The class object for the newly-created job.
239 * @bs: The block
240 * @speed: The maximum speed, in bytes per second, or 0 for unlimited.
241 * @cb: Completion function for the job.
242 * @opaque: Opaque pointer value passed to @cb.
243 * @errp: Error object.
245 * Create a new long-running block device job and return it. The job
246 * will call @cb asynchronously when the job completes. Note that
247 * @bs may have been closed at the time the @cb it is called. If
248 * this is the case, the job may be reported as either cancelled or
249 * completed.
251 * This function is not part of the public job interface; it should be
252 * called from a wrapper that is specific to the job type.
254 void *block_job_create(const char *job_id, const BlockJobDriver *driver,
255 BlockDriverState *bs, int64_t speed,
256 BlockCompletionFunc *cb, void *opaque, Error **errp);
259 * block_job_add_bdrv:
260 * @job: A block job
261 * @bs: A BlockDriverState that is involved in @job
263 * Add @bs to the list of BlockDriverState that are involved in
264 * @job. This means that all operations will be blocked on @bs while
265 * @job exists.
267 void block_job_add_bdrv(BlockJob *job, BlockDriverState *bs);
270 * block_job_sleep_ns:
271 * @job: The job that calls the function.
272 * @clock: The clock to sleep on.
273 * @ns: How many nanoseconds to stop for.
275 * Put the job to sleep (assuming that it wasn't canceled) for @ns
276 * nanoseconds. Canceling the job will interrupt the wait immediately.
278 void block_job_sleep_ns(BlockJob *job, QEMUClockType type, int64_t ns);
281 * block_job_yield:
282 * @job: The job that calls the function.
284 * Yield the block job coroutine.
286 void block_job_yield(BlockJob *job);
289 * block_job_ref:
290 * @bs: The block device.
292 * Grab a reference to the block job. Should be paired with block_job_unref.
294 void block_job_ref(BlockJob *job);
297 * block_job_unref:
298 * @bs: The block device.
300 * Release reference to the block job and release resources if it is the last
301 * reference.
303 void block_job_unref(BlockJob *job);
306 * block_job_completed:
307 * @job: The job being completed.
308 * @ret: The status code.
310 * Call the completion function that was registered at creation time, and
311 * free @job.
313 void block_job_completed(BlockJob *job, int ret);
316 * block_job_set_speed:
317 * @job: The job to set the speed for.
318 * @speed: The new value
319 * @errp: Error object.
321 * Set a rate-limiting parameter for the job; the actual meaning may
322 * vary depending on the job type.
324 void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp);
327 * block_job_cancel:
328 * @job: The job to be canceled.
330 * Asynchronously cancel the specified job.
332 void block_job_cancel(BlockJob *job);
335 * block_job_complete:
336 * @job: The job to be completed.
337 * @errp: Error object.
339 * Asynchronously complete the specified job.
341 void block_job_complete(BlockJob *job, Error **errp);
344 * block_job_is_cancelled:
345 * @job: The job being queried.
347 * Returns whether the job is scheduled for cancellation.
349 bool block_job_is_cancelled(BlockJob *job);
352 * block_job_query:
353 * @job: The job to get information about.
355 * Return information about a job.
357 BlockJobInfo *block_job_query(BlockJob *job);
360 * block_job_pause_point:
361 * @job: The job that is ready to pause.
363 * Pause now if block_job_pause() has been called. Block jobs that perform
364 * lots of I/O must call this between requests so that the job can be paused.
366 void coroutine_fn block_job_pause_point(BlockJob *job);
369 * block_job_pause:
370 * @job: The job to be paused.
372 * Asynchronously pause the specified job.
374 void block_job_pause(BlockJob *job);
377 * block_job_resume:
378 * @job: The job to be resumed.
380 * Resume the specified job. Must be paired with a preceding block_job_pause.
382 void block_job_resume(BlockJob *job);
385 * block_job_enter:
386 * @job: The job to enter.
388 * Continue the specified job by entering the coroutine.
390 void block_job_enter(BlockJob *job);
393 * block_job_event_cancelled:
394 * @job: The job whose information is requested.
396 * Send a BLOCK_JOB_CANCELLED event for the specified job.
398 void block_job_event_cancelled(BlockJob *job);
401 * block_job_ready:
402 * @job: The job which is now ready to complete.
403 * @msg: Error message. Only present on failure.
405 * Send a BLOCK_JOB_COMPLETED event for the specified job.
407 void block_job_event_completed(BlockJob *job, const char *msg);
410 * block_job_ready:
411 * @job: The job which is now ready to complete.
413 * Send a BLOCK_JOB_READY event for the specified job.
415 void block_job_event_ready(BlockJob *job);
418 * block_job_cancel_sync:
419 * @job: The job to be canceled.
421 * Synchronously cancel the job. The completion callback is called
422 * before the function returns. The job may actually complete
423 * instead of canceling itself; the circumstances under which this
424 * happens depend on the kind of job that is active.
426 * Returns the return value from the job if the job actually completed
427 * during the call, or -ECANCELED if it was canceled.
429 int block_job_cancel_sync(BlockJob *job);
432 * block_job_cancel_sync_all:
434 * Synchronously cancels all jobs using block_job_cancel_sync().
436 void block_job_cancel_sync_all(void);
439 * block_job_complete_sync:
440 * @job: The job to be completed.
441 * @errp: Error object which may be set by block_job_complete(); this is not
442 * necessarily set on every error, the job return value has to be
443 * checked as well.
445 * Synchronously complete the job. The completion callback is called before the
446 * function returns, unless it is NULL (which is permissible when using this
447 * function).
449 * Returns the return value from the job.
451 int block_job_complete_sync(BlockJob *job, Error **errp);
454 * block_job_iostatus_reset:
455 * @job: The job whose I/O status should be reset.
457 * Reset I/O status on @job and on BlockDriverState objects it uses,
458 * other than job->blk.
460 void block_job_iostatus_reset(BlockJob *job);
463 * block_job_error_action:
464 * @job: The job to signal an error for.
465 * @on_err: The error action setting.
466 * @is_read: Whether the operation was a read.
467 * @error: The error that was reported.
469 * Report an I/O error for a block job and possibly stop the VM. Return the
470 * action that was selected based on @on_err and @error.
472 BlockErrorAction block_job_error_action(BlockJob *job, BlockdevOnError on_err,
473 int is_read, int error);
475 typedef void BlockJobDeferToMainLoopFn(BlockJob *job, void *opaque);
478 * block_job_defer_to_main_loop:
479 * @job: The job
480 * @fn: The function to run in the main loop
481 * @opaque: The opaque value that is passed to @fn
483 * Execute a given function in the main loop with the BlockDriverState
484 * AioContext acquired. Block jobs must call bdrv_unref(), bdrv_close(), and
485 * anything that uses bdrv_drain_all() in the main loop.
487 * The @job AioContext is held while @fn executes.
489 void block_job_defer_to_main_loop(BlockJob *job,
490 BlockJobDeferToMainLoopFn *fn,
491 void *opaque);
494 * block_job_txn_new:
496 * Allocate and return a new block job transaction. Jobs can be added to the
497 * transaction using block_job_txn_add_job().
499 * The transaction is automatically freed when the last job completes or is
500 * cancelled.
502 * All jobs in the transaction either complete successfully or fail/cancel as a
503 * group. Jobs wait for each other before completing. Cancelling one job
504 * cancels all jobs in the transaction.
506 BlockJobTxn *block_job_txn_new(void);
509 * block_job_txn_unref:
511 * Release a reference that was previously acquired with block_job_txn_add_job
512 * or block_job_txn_new. If it's the last reference to the object, it will be
513 * freed.
515 void block_job_txn_unref(BlockJobTxn *txn);
518 * block_job_txn_add_job:
519 * @txn: The transaction (may be NULL)
520 * @job: Job to add to the transaction
522 * Add @job to the transaction. The @job must not already be in a transaction.
523 * The caller must call either block_job_txn_unref() or block_job_completed()
524 * to release the reference that is automatically grabbed here.
526 void block_job_txn_add_job(BlockJobTxn *txn, BlockJob *job);
528 #endif