]> git.proxmox.com Git - mirror_qemu.git/blob - include/block/blockjob_int.h
job: Move cancelled to Job
[mirror_qemu.git] / include / block / blockjob_int.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
26 #ifndef BLOCKJOB_INT_H
27 #define BLOCKJOB_INT_H
28
29 #include "block/blockjob.h"
30 #include "block/block.h"
31
32 /**
33 * BlockJobDriver:
34 *
35 * A class type for block job driver.
36 */
37 struct BlockJobDriver {
38 /** Generic JobDriver callbacks and settings */
39 JobDriver job_driver;
40
41 /** Mandatory: Entrypoint for the Coroutine. */
42 CoroutineEntry *start;
43
44 /**
45 * Optional callback for job types whose completion must be triggered
46 * manually.
47 */
48 void (*complete)(BlockJob *job, Error **errp);
49
50 /**
51 * If the callback is not NULL, prepare will be invoked when all the jobs
52 * belonging to the same transaction complete; or upon this job's completion
53 * if it is not in a transaction.
54 *
55 * This callback will not be invoked if the job has already failed.
56 * If it fails, abort and then clean will be called.
57 */
58 int (*prepare)(BlockJob *job);
59
60 /**
61 * If the callback is not NULL, it will be invoked when all the jobs
62 * belonging to the same transaction complete; or upon this job's
63 * completion if it is not in a transaction. Skipped if NULL.
64 *
65 * All jobs will complete with a call to either .commit() or .abort() but
66 * never both.
67 */
68 void (*commit)(BlockJob *job);
69
70 /**
71 * If the callback is not NULL, it will be invoked when any job in the
72 * same transaction fails; or upon this job's failure (due to error or
73 * cancellation) if it is not in a transaction. Skipped if NULL.
74 *
75 * All jobs will complete with a call to either .commit() or .abort() but
76 * never both.
77 */
78 void (*abort)(BlockJob *job);
79
80 /**
81 * If the callback is not NULL, it will be invoked after a call to either
82 * .commit() or .abort(). Regardless of which callback is invoked after
83 * completion, .clean() will always be called, even if the job does not
84 * belong to a transaction group.
85 */
86 void (*clean)(BlockJob *job);
87
88 /**
89 * If the callback is not NULL, it will be invoked when the job transitions
90 * into the paused state. Paused jobs must not perform any asynchronous
91 * I/O or event loop activity. This callback is used to quiesce jobs.
92 */
93 void coroutine_fn (*pause)(BlockJob *job);
94
95 /**
96 * If the callback is not NULL, it will be invoked when the job transitions
97 * out of the paused state. Any asynchronous I/O or event loop activity
98 * should be restarted from this callback.
99 */
100 void coroutine_fn (*resume)(BlockJob *job);
101
102 /*
103 * If the callback is not NULL, it will be invoked before the job is
104 * resumed in a new AioContext. This is the place to move any resources
105 * besides job->blk to the new AioContext.
106 */
107 void (*attached_aio_context)(BlockJob *job, AioContext *new_context);
108
109 /*
110 * If the callback is not NULL, it will be invoked when the job has to be
111 * synchronously cancelled or completed; it should drain BlockDriverStates
112 * as required to ensure progress.
113 */
114 void (*drain)(BlockJob *job);
115 };
116
117 /**
118 * block_job_create:
119 * @job_id: The id of the newly-created job, or %NULL to have one
120 * generated automatically.
121 * @driver: The class object for the newly-created job.
122 * @txn: The transaction this job belongs to, if any. %NULL otherwise.
123 * @bs: The block
124 * @perm, @shared_perm: Permissions to request for @bs
125 * @speed: The maximum speed, in bytes per second, or 0 for unlimited.
126 * @flags: Creation flags for the Block Job.
127 * See @BlockJobCreateFlags
128 * @cb: Completion function for the job.
129 * @opaque: Opaque pointer value passed to @cb.
130 * @errp: Error object.
131 *
132 * Create a new long-running block device job and return it. The job
133 * will call @cb asynchronously when the job completes. Note that
134 * @bs may have been closed at the time the @cb it is called. If
135 * this is the case, the job may be reported as either cancelled or
136 * completed.
137 *
138 * This function is not part of the public job interface; it should be
139 * called from a wrapper that is specific to the job type.
140 */
141 void *block_job_create(const char *job_id, const BlockJobDriver *driver,
142 BlockJobTxn *txn, BlockDriverState *bs, uint64_t perm,
143 uint64_t shared_perm, int64_t speed, int flags,
144 BlockCompletionFunc *cb, void *opaque, Error **errp);
145
146 /**
147 * block_job_free:
148 * Callback to be used for JobDriver.free in all block jobs. Frees block job
149 * specific resources in @job.
150 */
151 void block_job_free(Job *job);
152
153 /**
154 * block_job_sleep_ns:
155 * @job: The job that calls the function.
156 * @ns: How many nanoseconds to stop for.
157 *
158 * Put the job to sleep (assuming that it wasn't canceled) for @ns
159 * %QEMU_CLOCK_REALTIME nanoseconds. Canceling the job will immediately
160 * interrupt the wait.
161 */
162 void block_job_sleep_ns(BlockJob *job, int64_t ns);
163
164 /**
165 * block_job_yield:
166 * @job: The job that calls the function.
167 *
168 * Yield the block job coroutine.
169 */
170 void block_job_yield(BlockJob *job);
171
172 /**
173 * block_job_ratelimit_get_delay:
174 *
175 * Calculate and return delay for the next request in ns. See the documentation
176 * of ratelimit_calculate_delay() for details.
177 */
178 int64_t block_job_ratelimit_get_delay(BlockJob *job, uint64_t n);
179
180 /**
181 * block_job_early_fail:
182 * @bs: The block device.
183 *
184 * The block job could not be started, free it.
185 */
186 void block_job_early_fail(BlockJob *job);
187
188 /**
189 * block_job_completed:
190 * @job: The job being completed.
191 * @ret: The status code.
192 *
193 * Call the completion function that was registered at creation time, and
194 * free @job.
195 */
196 void block_job_completed(BlockJob *job, int ret);
197
198 /**
199 * block_job_pause_point:
200 * @job: The job that is ready to pause.
201 *
202 * Pause now if block_job_pause() has been called. Block jobs that perform
203 * lots of I/O must call this between requests so that the job can be paused.
204 */
205 void coroutine_fn block_job_pause_point(BlockJob *job);
206
207 /**
208 * block_job_enter:
209 * @job: The job to enter.
210 *
211 * Continue the specified job by entering the coroutine.
212 */
213 void block_job_enter(BlockJob *job);
214
215 /**
216 * block_job_event_ready:
217 * @job: The job which is now ready to be completed.
218 *
219 * Send a BLOCK_JOB_READY event for the specified job.
220 */
221 void block_job_event_ready(BlockJob *job);
222
223 /**
224 * block_job_error_action:
225 * @job: The job to signal an error for.
226 * @on_err: The error action setting.
227 * @is_read: Whether the operation was a read.
228 * @error: The error that was reported.
229 *
230 * Report an I/O error for a block job and possibly stop the VM. Return the
231 * action that was selected based on @on_err and @error.
232 */
233 BlockErrorAction block_job_error_action(BlockJob *job, BlockdevOnError on_err,
234 int is_read, int error);
235
236 typedef void BlockJobDeferToMainLoopFn(BlockJob *job, void *opaque);
237
238 /**
239 * block_job_defer_to_main_loop:
240 * @job: The job
241 * @fn: The function to run in the main loop
242 * @opaque: The opaque value that is passed to @fn
243 *
244 * This function must be called by the main job coroutine just before it
245 * returns. @fn is executed in the main loop with the BlockDriverState
246 * AioContext acquired. Block jobs must call bdrv_unref(), bdrv_close(), and
247 * anything that uses bdrv_drain_all() in the main loop.
248 *
249 * The @job AioContext is held while @fn executes.
250 */
251 void block_job_defer_to_main_loop(BlockJob *job,
252 BlockJobDeferToMainLoopFn *fn,
253 void *opaque);
254
255 #endif