blockjob: add pause points
[qemu/ar7.git] / include / block / blockjob.h
blob7739f37c3aed911084e9dd7b05d57175154fcf7d
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.
25 #ifndef BLOCKJOB_H
26 #define BLOCKJOB_H 1
28 #include "block/block.h"
30 /**
31 * BlockJobDriver:
33 * A class type for block job driver.
35 typedef struct BlockJobDriver {
36 /** Derived BlockJob struct size */
37 size_t instance_size;
39 /** String describing the operation, part of query-block-jobs QMP API */
40 BlockJobType job_type;
42 /** Optional callback for job types that support setting a speed limit */
43 void (*set_speed)(BlockJob *job, int64_t speed, Error **errp);
45 /** Optional callback for job types that need to forward I/O status reset */
46 void (*iostatus_reset)(BlockJob *job);
48 /**
49 * Optional callback for job types whose completion must be triggered
50 * manually.
52 void (*complete)(BlockJob *job, Error **errp);
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.
59 * All jobs will complete with a call to either .commit() or .abort() but
60 * never both.
62 void (*commit)(BlockJob *job);
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.
69 * All jobs will complete with a call to either .commit() or .abort() but
70 * never both.
72 void (*abort)(BlockJob *job);
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.
79 void coroutine_fn (*pause)(BlockJob *job);
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.
86 void coroutine_fn (*resume)(BlockJob *job);
87 } BlockJobDriver;
89 /**
90 * BlockJob:
92 * Long-running operation on a BlockDriverState.
94 struct BlockJob {
95 /** The job type, including the job vtable. */
96 const BlockJobDriver *driver;
98 /** The block device on which the job is operating. */
99 BlockBackend *blk;
102 * The ID of the block job. Currently the BlockBackend name of the BDS
103 * owning the job at the time when the job is started.
105 * TODO Decouple block job IDs from BlockBackend names
107 char *id;
110 * The coroutine that executes the job. If not NULL, it is
111 * reentered when busy is false and the job is cancelled.
113 Coroutine *co;
116 * Set to true if the job should cancel itself. The flag must
117 * always be tested just before toggling the busy flag from false
118 * to true. After a job has been cancelled, it should only yield
119 * if #aio_poll will ("sooner or later") reenter the coroutine.
121 bool cancelled;
124 * Counter for pause request. If non-zero, the block job is either paused,
125 * or if busy == true will pause itself as soon as possible.
127 int pause_count;
130 * Set to true if the job is paused by user. Can be unpaused with the
131 * block-job-resume QMP command.
133 bool user_paused;
136 * Set to false by the job while the coroutine has yielded and may be
137 * re-entered by block_job_enter(). There may still be I/O or event loop
138 * activity pending.
140 bool busy;
143 * Set to true by the job while it is in a quiescent state, where
144 * no I/O or event loop activity is pending.
146 bool paused;
149 * Set to true when the job is ready to be completed.
151 bool ready;
154 * Set to true when the job has deferred work to the main loop.
156 bool deferred_to_main_loop;
158 /** Element of the list of block jobs */
159 QLIST_ENTRY(BlockJob) job_list;
161 /** Status that is published by the query-block-jobs QMP API */
162 BlockDeviceIoStatus iostatus;
164 /** Offset that is published by the query-block-jobs QMP API */
165 int64_t offset;
167 /** Length that is published by the query-block-jobs QMP API */
168 int64_t len;
170 /** Speed that was set with @block_job_set_speed. */
171 int64_t speed;
173 /** The completion function that will be called when the job completes. */
174 BlockCompletionFunc *cb;
176 /** Block other operations when block job is running */
177 Error *blocker;
179 /** The opaque value that is passed to the completion function. */
180 void *opaque;
182 /** Reference count of the block job */
183 int refcnt;
185 /* True if this job has reported completion by calling block_job_completed.
187 bool completed;
189 /* ret code passed to block_job_completed.
191 int ret;
193 /** Non-NULL if this job is part of a transaction */
194 BlockJobTxn *txn;
195 QLIST_ENTRY(BlockJob) txn_list;
199 * block_job_next:
200 * @job: A block job, or %NULL.
202 * Get the next element from the list of block jobs after @job, or the
203 * first one if @job is %NULL.
205 * Returns the requested job, or %NULL if there are no more jobs left.
207 BlockJob *block_job_next(BlockJob *job);
210 * block_job_create:
211 * @job_type: The class object for the newly-created job.
212 * @bs: The block
213 * @speed: The maximum speed, in bytes per second, or 0 for unlimited.
214 * @cb: Completion function for the job.
215 * @opaque: Opaque pointer value passed to @cb.
216 * @errp: Error object.
218 * Create a new long-running block device job and return it. The job
219 * will call @cb asynchronously when the job completes. Note that
220 * @bs may have been closed at the time the @cb it is called. If
221 * this is the case, the job may be reported as either cancelled or
222 * completed.
224 * This function is not part of the public job interface; it should be
225 * called from a wrapper that is specific to the job type.
227 void *block_job_create(const BlockJobDriver *driver, BlockDriverState *bs,
228 int64_t speed, BlockCompletionFunc *cb,
229 void *opaque, Error **errp);
232 * block_job_sleep_ns:
233 * @job: The job that calls the function.
234 * @clock: The clock to sleep on.
235 * @ns: How many nanoseconds to stop for.
237 * Put the job to sleep (assuming that it wasn't canceled) for @ns
238 * nanoseconds. Canceling the job will interrupt the wait immediately.
240 void block_job_sleep_ns(BlockJob *job, QEMUClockType type, int64_t ns);
243 * block_job_yield:
244 * @job: The job that calls the function.
246 * Yield the block job coroutine.
248 void block_job_yield(BlockJob *job);
251 * block_job_ref:
252 * @bs: The block device.
254 * Grab a reference to the block job. Should be paired with block_job_unref.
256 void block_job_ref(BlockJob *job);
259 * block_job_unref:
260 * @bs: The block device.
262 * Release reference to the block job and release resources if it is the last
263 * reference.
265 void block_job_unref(BlockJob *job);
268 * block_job_completed:
269 * @job: The job being completed.
270 * @ret: The status code.
272 * Call the completion function that was registered at creation time, and
273 * free @job.
275 void block_job_completed(BlockJob *job, int ret);
278 * block_job_set_speed:
279 * @job: The job to set the speed for.
280 * @speed: The new value
281 * @errp: Error object.
283 * Set a rate-limiting parameter for the job; the actual meaning may
284 * vary depending on the job type.
286 void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp);
289 * block_job_cancel:
290 * @job: The job to be canceled.
292 * Asynchronously cancel the specified job.
294 void block_job_cancel(BlockJob *job);
297 * block_job_complete:
298 * @job: The job to be completed.
299 * @errp: Error object.
301 * Asynchronously complete the specified job.
303 void block_job_complete(BlockJob *job, Error **errp);
306 * block_job_is_cancelled:
307 * @job: The job being queried.
309 * Returns whether the job is scheduled for cancellation.
311 bool block_job_is_cancelled(BlockJob *job);
314 * block_job_query:
315 * @job: The job to get information about.
317 * Return information about a job.
319 BlockJobInfo *block_job_query(BlockJob *job);
322 * block_job_pause_point:
323 * @job: The job that is ready to pause.
325 * Pause now if block_job_pause() has been called. Block jobs that perform
326 * lots of I/O must call this between requests so that the job can be paused.
328 void coroutine_fn block_job_pause_point(BlockJob *job);
331 * block_job_pause:
332 * @job: The job to be paused.
334 * Asynchronously pause the specified job.
336 void block_job_pause(BlockJob *job);
339 * block_job_resume:
340 * @job: The job to be resumed.
342 * Resume the specified job. Must be paired with a preceding block_job_pause.
344 void block_job_resume(BlockJob *job);
347 * block_job_enter:
348 * @job: The job to enter.
350 * Continue the specified job by entering the coroutine.
352 void block_job_enter(BlockJob *job);
355 * block_job_event_cancelled:
356 * @job: The job whose information is requested.
358 * Send a BLOCK_JOB_CANCELLED event for the specified job.
360 void block_job_event_cancelled(BlockJob *job);
363 * block_job_ready:
364 * @job: The job which is now ready to complete.
365 * @msg: Error message. Only present on failure.
367 * Send a BLOCK_JOB_COMPLETED event for the specified job.
369 void block_job_event_completed(BlockJob *job, const char *msg);
372 * block_job_ready:
373 * @job: The job which is now ready to complete.
375 * Send a BLOCK_JOB_READY event for the specified job.
377 void block_job_event_ready(BlockJob *job);
380 * block_job_cancel_sync:
381 * @job: The job to be canceled.
383 * Synchronously cancel the job. The completion callback is called
384 * before the function returns. The job may actually complete
385 * instead of canceling itself; the circumstances under which this
386 * happens depend on the kind of job that is active.
388 * Returns the return value from the job if the job actually completed
389 * during the call, or -ECANCELED if it was canceled.
391 int block_job_cancel_sync(BlockJob *job);
394 * block_job_cancel_sync_all:
396 * Synchronously cancels all jobs using block_job_cancel_sync().
398 void block_job_cancel_sync_all(void);
401 * block_job_complete_sync:
402 * @job: The job to be completed.
403 * @errp: Error object which may be set by block_job_complete(); this is not
404 * necessarily set on every error, the job return value has to be
405 * checked as well.
407 * Synchronously complete the job. The completion callback is called before the
408 * function returns, unless it is NULL (which is permissible when using this
409 * function).
411 * Returns the return value from the job.
413 int block_job_complete_sync(BlockJob *job, Error **errp);
416 * block_job_iostatus_reset:
417 * @job: The job whose I/O status should be reset.
419 * Reset I/O status on @job and on BlockDriverState objects it uses,
420 * other than job->blk.
422 void block_job_iostatus_reset(BlockJob *job);
425 * block_job_error_action:
426 * @job: The job to signal an error for.
427 * @on_err: The error action setting.
428 * @is_read: Whether the operation was a read.
429 * @error: The error that was reported.
431 * Report an I/O error for a block job and possibly stop the VM. Return the
432 * action that was selected based on @on_err and @error.
434 BlockErrorAction block_job_error_action(BlockJob *job, BlockdevOnError on_err,
435 int is_read, int error);
437 typedef void BlockJobDeferToMainLoopFn(BlockJob *job, void *opaque);
440 * block_job_defer_to_main_loop:
441 * @job: The job
442 * @fn: The function to run in the main loop
443 * @opaque: The opaque value that is passed to @fn
445 * Execute a given function in the main loop with the BlockDriverState
446 * AioContext acquired. Block jobs must call bdrv_unref(), bdrv_close(), and
447 * anything that uses bdrv_drain_all() in the main loop.
449 * The @job AioContext is held while @fn executes.
451 void block_job_defer_to_main_loop(BlockJob *job,
452 BlockJobDeferToMainLoopFn *fn,
453 void *opaque);
456 * block_job_txn_new:
458 * Allocate and return a new block job transaction. Jobs can be added to the
459 * transaction using block_job_txn_add_job().
461 * The transaction is automatically freed when the last job completes or is
462 * cancelled.
464 * All jobs in the transaction either complete successfully or fail/cancel as a
465 * group. Jobs wait for each other before completing. Cancelling one job
466 * cancels all jobs in the transaction.
468 BlockJobTxn *block_job_txn_new(void);
471 * block_job_txn_unref:
473 * Release a reference that was previously acquired with block_job_txn_add_job
474 * or block_job_txn_new. If it's the last reference to the object, it will be
475 * freed.
477 void block_job_txn_unref(BlockJobTxn *txn);
480 * block_job_txn_add_job:
481 * @txn: The transaction (may be NULL)
482 * @job: Job to add to the transaction
484 * Add @job to the transaction. The @job must not already be in a transaction.
485 * The caller must call either block_job_txn_unref() or block_job_completed()
486 * to release the reference that is automatically grabbed here.
488 void block_job_txn_add_job(BlockJobTxn *txn, BlockJob *job);
490 #endif