]> git.proxmox.com Git - mirror_qemu.git/blob - include/block/blockjob.h
blockjobs: add block-job-finalize
[mirror_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
26 #ifndef BLOCKJOB_H
27 #define BLOCKJOB_H
28
29 #include "block/block.h"
30
31 typedef struct BlockJobDriver BlockJobDriver;
32 typedef struct BlockJobTxn BlockJobTxn;
33
34 /**
35 * BlockJob:
36 *
37 * Long-running operation on a BlockDriverState.
38 */
39 typedef struct BlockJob {
40 /** The job type, including the job vtable. */
41 const BlockJobDriver *driver;
42
43 /** The block device on which the job is operating. */
44 BlockBackend *blk;
45
46 /**
47 * The ID of the block job. May be NULL for internal jobs.
48 */
49 char *id;
50
51 /**
52 * The coroutine that executes the job. If not NULL, it is
53 * reentered when busy is false and the job is cancelled.
54 */
55 Coroutine *co;
56
57 /**
58 * Set to true if the job should cancel itself. The flag must
59 * always be tested just before toggling the busy flag from false
60 * to true. After a job has been cancelled, it should only yield
61 * if #aio_poll will ("sooner or later") reenter the coroutine.
62 */
63 bool cancelled;
64
65 /**
66 * Counter for pause request. If non-zero, the block job is either paused,
67 * or if busy == true will pause itself as soon as possible.
68 */
69 int pause_count;
70
71 /**
72 * Set to true if the job is paused by user. Can be unpaused with the
73 * block-job-resume QMP command.
74 */
75 bool user_paused;
76
77 /**
78 * Set to false by the job while the coroutine has yielded and may be
79 * re-entered by block_job_enter(). There may still be I/O or event loop
80 * activity pending. Accessed under block_job_mutex (in blockjob.c).
81 */
82 bool busy;
83
84 /**
85 * Set to true by the job while it is in a quiescent state, where
86 * no I/O or event loop activity is pending.
87 */
88 bool paused;
89
90 /**
91 * Set to true when the job is ready to be completed.
92 */
93 bool ready;
94
95 /**
96 * Set to true when the job has deferred work to the main loop.
97 */
98 bool deferred_to_main_loop;
99
100 /** Element of the list of block jobs */
101 QLIST_ENTRY(BlockJob) job_list;
102
103 /** Status that is published by the query-block-jobs QMP API */
104 BlockDeviceIoStatus iostatus;
105
106 /** Offset that is published by the query-block-jobs QMP API */
107 int64_t offset;
108
109 /** Length that is published by the query-block-jobs QMP API */
110 int64_t len;
111
112 /** Speed that was set with @block_job_set_speed. */
113 int64_t speed;
114
115 /** The completion function that will be called when the job completes. */
116 BlockCompletionFunc *cb;
117
118 /** Block other operations when block job is running */
119 Error *blocker;
120
121 /** BlockDriverStates that are involved in this block job */
122 GSList *nodes;
123
124 /** The opaque value that is passed to the completion function. */
125 void *opaque;
126
127 /** Reference count of the block job */
128 int refcnt;
129
130 /** True when job has reported completion by calling block_job_completed. */
131 bool completed;
132
133 /** ret code passed to block_job_completed. */
134 int ret;
135
136 /**
137 * Timer that is used by @block_job_sleep_ns. Accessed under
138 * block_job_mutex (in blockjob.c).
139 */
140 QEMUTimer sleep_timer;
141
142 /** Current state; See @BlockJobStatus for details. */
143 BlockJobStatus status;
144
145 /** True if this job should automatically finalize itself */
146 bool auto_finalize;
147
148 /** True if this job should automatically dismiss itself */
149 bool auto_dismiss;
150
151 BlockJobTxn *txn;
152 QLIST_ENTRY(BlockJob) txn_list;
153 } BlockJob;
154
155 typedef enum BlockJobCreateFlags {
156 /* Default behavior */
157 BLOCK_JOB_DEFAULT = 0x00,
158 /* BlockJob is not QMP-created and should not send QMP events */
159 BLOCK_JOB_INTERNAL = 0x01,
160 /* BlockJob requires manual finalize step */
161 BLOCK_JOB_MANUAL_FINALIZE = 0x02,
162 /* BlockJob requires manual dismiss step */
163 BLOCK_JOB_MANUAL_DISMISS = 0x04,
164 } BlockJobCreateFlags;
165
166 /**
167 * block_job_next:
168 * @job: A block job, or %NULL.
169 *
170 * Get the next element from the list of block jobs after @job, or the
171 * first one if @job is %NULL.
172 *
173 * Returns the requested job, or %NULL if there are no more jobs left.
174 */
175 BlockJob *block_job_next(BlockJob *job);
176
177 /**
178 * block_job_get:
179 * @id: The id of the block job.
180 *
181 * Get the block job identified by @id (which must not be %NULL).
182 *
183 * Returns the requested job, or %NULL if it doesn't exist.
184 */
185 BlockJob *block_job_get(const char *id);
186
187 /**
188 * block_job_add_bdrv:
189 * @job: A block job
190 * @name: The name to assign to the new BdrvChild
191 * @bs: A BlockDriverState that is involved in @job
192 * @perm, @shared_perm: Permissions to request on the node
193 *
194 * Add @bs to the list of BlockDriverState that are involved in
195 * @job. This means that all operations will be blocked on @bs while
196 * @job exists.
197 */
198 int block_job_add_bdrv(BlockJob *job, const char *name, BlockDriverState *bs,
199 uint64_t perm, uint64_t shared_perm, Error **errp);
200
201 /**
202 * block_job_remove_all_bdrv:
203 * @job: The block job
204 *
205 * Remove all BlockDriverStates from the list of nodes that are involved in the
206 * job. This removes the blockers added with block_job_add_bdrv().
207 */
208 void block_job_remove_all_bdrv(BlockJob *job);
209
210 /**
211 * block_job_set_speed:
212 * @job: The job to set the speed for.
213 * @speed: The new value
214 * @errp: Error object.
215 *
216 * Set a rate-limiting parameter for the job; the actual meaning may
217 * vary depending on the job type.
218 */
219 void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp);
220
221 /**
222 * block_job_start:
223 * @job: A job that has not yet been started.
224 *
225 * Begins execution of a block job.
226 * Takes ownership of one reference to the job object.
227 */
228 void block_job_start(BlockJob *job);
229
230 /**
231 * block_job_cancel:
232 * @job: The job to be canceled.
233 *
234 * Asynchronously cancel the specified job.
235 */
236 void block_job_cancel(BlockJob *job);
237
238 /**
239 * block_job_complete:
240 * @job: The job to be completed.
241 * @errp: Error object.
242 *
243 * Asynchronously complete the specified job.
244 */
245 void block_job_complete(BlockJob *job, Error **errp);
246
247
248 /**
249 * block_job_finalize:
250 * @job: The job to fully commit and finish.
251 * @errp: Error object.
252 *
253 * For jobs that have finished their work and are pending
254 * awaiting explicit acknowledgement to commit their work,
255 * This will commit that work.
256 *
257 * FIXME: Make the below statement universally true:
258 * For jobs that support the manual workflow mode, all graph
259 * changes that occur as a result will occur after this command
260 * and before a successful reply.
261 */
262 void block_job_finalize(BlockJob *job, Error **errp);
263
264 /**
265 * block_job_dismiss:
266 * @job: The job to be dismissed.
267 * @errp: Error object.
268 *
269 * Remove a concluded job from the query list.
270 */
271 void block_job_dismiss(BlockJob **job, Error **errp);
272
273 /**
274 * block_job_query:
275 * @job: The job to get information about.
276 *
277 * Return information about a job.
278 */
279 BlockJobInfo *block_job_query(BlockJob *job, Error **errp);
280
281 /**
282 * block_job_user_pause:
283 * @job: The job to be paused.
284 *
285 * Asynchronously pause the specified job.
286 * Do not allow a resume until a matching call to block_job_user_resume.
287 */
288 void block_job_user_pause(BlockJob *job, Error **errp);
289
290 /**
291 * block_job_paused:
292 * @job: The job to query.
293 *
294 * Returns true if the job is user-paused.
295 */
296 bool block_job_user_paused(BlockJob *job);
297
298 /**
299 * block_job_user_resume:
300 * @job: The job to be resumed.
301 *
302 * Resume the specified job.
303 * Must be paired with a preceding block_job_user_pause.
304 */
305 void block_job_user_resume(BlockJob *job, Error **errp);
306
307 /**
308 * block_job_user_cancel:
309 * @job: The job to be cancelled.
310 *
311 * Cancels the specified job, but may refuse to do so if the
312 * operation isn't currently meaningful.
313 */
314 void block_job_user_cancel(BlockJob *job, Error **errp);
315
316 /**
317 * block_job_cancel_sync:
318 * @job: The job to be canceled.
319 *
320 * Synchronously cancel the job. The completion callback is called
321 * before the function returns. The job may actually complete
322 * instead of canceling itself; the circumstances under which this
323 * happens depend on the kind of job that is active.
324 *
325 * Returns the return value from the job if the job actually completed
326 * during the call, or -ECANCELED if it was canceled.
327 */
328 int block_job_cancel_sync(BlockJob *job);
329
330 /**
331 * block_job_cancel_sync_all:
332 *
333 * Synchronously cancels all jobs using block_job_cancel_sync().
334 */
335 void block_job_cancel_sync_all(void);
336
337 /**
338 * block_job_complete_sync:
339 * @job: The job to be completed.
340 * @errp: Error object which may be set by block_job_complete(); this is not
341 * necessarily set on every error, the job return value has to be
342 * checked as well.
343 *
344 * Synchronously complete the job. The completion callback is called before the
345 * function returns, unless it is NULL (which is permissible when using this
346 * function).
347 *
348 * Returns the return value from the job.
349 */
350 int block_job_complete_sync(BlockJob *job, Error **errp);
351
352 /**
353 * block_job_iostatus_reset:
354 * @job: The job whose I/O status should be reset.
355 *
356 * Reset I/O status on @job and on BlockDriverState objects it uses,
357 * other than job->blk.
358 */
359 void block_job_iostatus_reset(BlockJob *job);
360
361 /**
362 * block_job_txn_new:
363 *
364 * Allocate and return a new block job transaction. Jobs can be added to the
365 * transaction using block_job_txn_add_job().
366 *
367 * The transaction is automatically freed when the last job completes or is
368 * cancelled.
369 *
370 * All jobs in the transaction either complete successfully or fail/cancel as a
371 * group. Jobs wait for each other before completing. Cancelling one job
372 * cancels all jobs in the transaction.
373 */
374 BlockJobTxn *block_job_txn_new(void);
375
376 /**
377 * block_job_ref:
378 *
379 * Add a reference to BlockJob refcnt, it will be decreased with
380 * block_job_unref, and then be freed if it comes to be the last
381 * reference.
382 */
383 void block_job_ref(BlockJob *job);
384
385 /**
386 * block_job_unref:
387 *
388 * Release a reference that was previously acquired with block_job_ref
389 * or block_job_create. If it's the last reference to the object, it will be
390 * freed.
391 */
392 void block_job_unref(BlockJob *job);
393
394 /**
395 * block_job_txn_unref:
396 *
397 * Release a reference that was previously acquired with block_job_txn_add_job
398 * or block_job_txn_new. If it's the last reference to the object, it will be
399 * freed.
400 */
401 void block_job_txn_unref(BlockJobTxn *txn);
402
403 /**
404 * block_job_txn_add_job:
405 * @txn: The transaction (may be NULL)
406 * @job: Job to add to the transaction
407 *
408 * Add @job to the transaction. The @job must not already be in a transaction.
409 * The caller must call either block_job_txn_unref() or block_job_completed()
410 * to release the reference that is automatically grabbed here.
411 */
412 void block_job_txn_add_job(BlockJobTxn *txn, BlockJob *job);
413
414 /**
415 * block_job_is_internal:
416 * @job: The job to determine if it is user-visible or not.
417 *
418 * Returns true if the job should not be visible to the management layer.
419 */
420 bool block_job_is_internal(BlockJob *job);
421
422 #endif