memory: extract first iteration of address_space_read and address_space_write
[qemu/ar7.git] / include / block / blockjob.h
blobd84ccd8d2cc548066709a5f8977097ff1da57514
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);
73 } BlockJobDriver;
75 /**
76 * BlockJob:
78 * Long-running operation on a BlockDriverState.
80 struct BlockJob {
81 /** The job type, including the job vtable. */
82 const BlockJobDriver *driver;
84 /** The block device on which the job is operating. */
85 BlockDriverState *bs;
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.
91 * TODO Decouple block job IDs from BlockBackend names
93 char *id;
95 /**
96 * The coroutine that executes the job. If not NULL, it is
97 * reentered when busy is false and the job is cancelled.
99 Coroutine *co;
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.
107 bool cancelled;
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.
113 int pause_count;
116 * Set to true if the job is paused by user. Can be unpaused with the
117 * block-job-resume QMP command.
119 bool user_paused;
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.
126 bool busy;
129 * Set to true when the job is ready to be completed.
131 bool ready;
133 /** Status that is published by the query-block-jobs QMP API */
134 BlockDeviceIoStatus iostatus;
136 /** Offset that is published by the query-block-jobs QMP API */
137 int64_t offset;
139 /** Length that is published by the query-block-jobs QMP API */
140 int64_t len;
142 /** Speed that was set with @block_job_set_speed. */
143 int64_t speed;
145 /** The completion function that will be called when the job completes. */
146 BlockCompletionFunc *cb;
148 /** Block other operations when block job is running */
149 Error *blocker;
151 /** The opaque value that is passed to the completion function. */
152 void *opaque;
154 /** Reference count of the block job */
155 int refcnt;
157 /* True if this job has reported completion by calling block_job_completed.
159 bool completed;
161 /* ret code passed to block_job_completed.
163 int ret;
165 /** Non-NULL if this job is part of a transaction */
166 BlockJobTxn *txn;
167 QLIST_ENTRY(BlockJob) txn_list;
171 * block_job_create:
172 * @job_type: The class object for the newly-created job.
173 * @bs: The block
174 * @speed: The maximum speed, in bytes per second, or 0 for unlimited.
175 * @cb: Completion function for the job.
176 * @opaque: Opaque pointer value passed to @cb.
177 * @errp: Error object.
179 * Create a new long-running block device job and return it. The job
180 * will call @cb asynchronously when the job completes. Note that
181 * @bs may have been closed at the time the @cb it is called. If
182 * this is the case, the job may be reported as either cancelled or
183 * completed.
185 * This function is not part of the public job interface; it should be
186 * called from a wrapper that is specific to the job type.
188 void *block_job_create(const BlockJobDriver *driver, BlockDriverState *bs,
189 int64_t speed, BlockCompletionFunc *cb,
190 void *opaque, Error **errp);
193 * block_job_sleep_ns:
194 * @job: The job that calls the function.
195 * @clock: The clock to sleep on.
196 * @ns: How many nanoseconds to stop for.
198 * Put the job to sleep (assuming that it wasn't canceled) for @ns
199 * nanoseconds. Canceling the job will interrupt the wait immediately.
201 void block_job_sleep_ns(BlockJob *job, QEMUClockType type, int64_t ns);
204 * block_job_yield:
205 * @job: The job that calls the function.
207 * Yield the block job coroutine.
209 void block_job_yield(BlockJob *job);
212 * block_job_ref:
213 * @bs: The block device.
215 * Grab a reference to the block job. Should be paired with block_job_unref.
217 void block_job_ref(BlockJob *job);
220 * block_job_unref:
221 * @bs: The block device.
223 * Release reference to the block job and release resources if it is the last
224 * reference.
226 void block_job_unref(BlockJob *job);
229 * block_job_completed:
230 * @job: The job being completed.
231 * @ret: The status code.
233 * Call the completion function that was registered at creation time, and
234 * free @job.
236 void block_job_completed(BlockJob *job, int ret);
239 * block_job_set_speed:
240 * @job: The job to set the speed for.
241 * @speed: The new value
242 * @errp: Error object.
244 * Set a rate-limiting parameter for the job; the actual meaning may
245 * vary depending on the job type.
247 void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp);
250 * block_job_cancel:
251 * @job: The job to be canceled.
253 * Asynchronously cancel the specified job.
255 void block_job_cancel(BlockJob *job);
258 * block_job_complete:
259 * @job: The job to be completed.
260 * @errp: Error object.
262 * Asynchronously complete the specified job.
264 void block_job_complete(BlockJob *job, Error **errp);
267 * block_job_is_cancelled:
268 * @job: The job being queried.
270 * Returns whether the job is scheduled for cancellation.
272 bool block_job_is_cancelled(BlockJob *job);
275 * block_job_query:
276 * @job: The job to get information about.
278 * Return information about a job.
280 BlockJobInfo *block_job_query(BlockJob *job);
283 * block_job_pause:
284 * @job: The job to be paused.
286 * Asynchronously pause the specified job.
288 void block_job_pause(BlockJob *job);
291 * block_job_resume:
292 * @job: The job to be resumed.
294 * Resume the specified job. Must be paired with a preceding block_job_pause.
296 void block_job_resume(BlockJob *job);
299 * block_job_enter:
300 * @job: The job to enter.
302 * Continue the specified job by entering the coroutine.
304 void block_job_enter(BlockJob *job);
307 * block_job_event_cancelled:
308 * @job: The job whose information is requested.
310 * Send a BLOCK_JOB_CANCELLED event for the specified job.
312 void block_job_event_cancelled(BlockJob *job);
315 * block_job_ready:
316 * @job: The job which is now ready to complete.
317 * @msg: Error message. Only present on failure.
319 * Send a BLOCK_JOB_COMPLETED event for the specified job.
321 void block_job_event_completed(BlockJob *job, const char *msg);
324 * block_job_ready:
325 * @job: The job which is now ready to complete.
327 * Send a BLOCK_JOB_READY event for the specified job.
329 void block_job_event_ready(BlockJob *job);
332 * block_job_is_paused:
333 * @job: The job being queried.
335 * Returns whether the job is currently paused, or will pause
336 * as soon as it reaches a sleeping point.
338 bool block_job_is_paused(BlockJob *job);
341 * block_job_cancel_sync:
342 * @job: The job to be canceled.
344 * Synchronously cancel the job. The completion callback is called
345 * before the function returns. The job may actually complete
346 * instead of canceling itself; the circumstances under which this
347 * happens depend on the kind of job that is active.
349 * Returns the return value from the job if the job actually completed
350 * during the call, or -ECANCELED if it was canceled.
352 int block_job_cancel_sync(BlockJob *job);
355 * block_job_complete_sync:
356 * @job: The job to be completed.
357 * @errp: Error object which may be set by block_job_complete(); this is not
358 * necessarily set on every error, the job return value has to be
359 * checked as well.
361 * Synchronously complete the job. The completion callback is called before the
362 * function returns, unless it is NULL (which is permissible when using this
363 * function).
365 * Returns the return value from the job.
367 int block_job_complete_sync(BlockJob *job, Error **errp);
370 * block_job_iostatus_reset:
371 * @job: The job whose I/O status should be reset.
373 * Reset I/O status on @job and on BlockDriverState objects it uses,
374 * other than job->bs.
376 void block_job_iostatus_reset(BlockJob *job);
379 * block_job_error_action:
380 * @job: The job to signal an error for.
381 * @bs: The block device on which to set an I/O error.
382 * @on_err: The error action setting.
383 * @is_read: Whether the operation was a read.
384 * @error: The error that was reported.
386 * Report an I/O error for a block job and possibly stop the VM. Return the
387 * action that was selected based on @on_err and @error.
389 BlockErrorAction block_job_error_action(BlockJob *job, BlockDriverState *bs,
390 BlockdevOnError on_err,
391 int is_read, int error);
393 typedef void BlockJobDeferToMainLoopFn(BlockJob *job, void *opaque);
396 * block_job_defer_to_main_loop:
397 * @job: The job
398 * @fn: The function to run in the main loop
399 * @opaque: The opaque value that is passed to @fn
401 * Execute a given function in the main loop with the BlockDriverState
402 * AioContext acquired. Block jobs must call bdrv_unref(), bdrv_close(), and
403 * anything that uses bdrv_drain_all() in the main loop.
405 * The @job AioContext is held while @fn executes.
407 void block_job_defer_to_main_loop(BlockJob *job,
408 BlockJobDeferToMainLoopFn *fn,
409 void *opaque);
412 * block_job_txn_new:
414 * Allocate and return a new block job transaction. Jobs can be added to the
415 * transaction using block_job_txn_add_job().
417 * The transaction is automatically freed when the last job completes or is
418 * cancelled.
420 * All jobs in the transaction either complete successfully or fail/cancel as a
421 * group. Jobs wait for each other before completing. Cancelling one job
422 * cancels all jobs in the transaction.
424 BlockJobTxn *block_job_txn_new(void);
427 * block_job_txn_unref:
429 * Release a reference that was previously acquired with block_job_txn_add_job
430 * or block_job_txn_new. If it's the last reference to the object, it will be
431 * freed.
433 void block_job_txn_unref(BlockJobTxn *txn);
436 * block_job_txn_add_job:
437 * @txn: The transaction (may be NULL)
438 * @job: Job to add to the transaction
440 * Add @job to the transaction. The @job must not already be in a transaction.
441 * The caller must call either block_job_txn_unref() or block_job_completed()
442 * to release the reference that is automatically grabbed here.
444 void block_job_txn_add_job(BlockJobTxn *txn, BlockJob *job);
446 #endif