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
30 #include "block/block.h"
31 #include "qemu/ratelimit.h"
33 #define BLOCK_JOB_SLICE_TIME 100000000ULL /* ns */
35 typedef struct BlockJobDriver BlockJobDriver
;
36 typedef struct BlockJobTxn BlockJobTxn
;
41 * Long-running operation on a BlockDriverState.
43 typedef struct BlockJob
{
44 /** Data belonging to the generic Job infrastructure */
47 /** The job type, including the job vtable. */
48 const BlockJobDriver
*driver
;
50 /** The block device on which the job is operating. */
54 * The coroutine that executes the job. If not NULL, it is
55 * reentered when busy is false and the job is cancelled.
60 * Set to true if the job should cancel itself. The flag must
61 * always be tested just before toggling the busy flag from false
62 * to true. After a job has been cancelled, it should only yield
63 * if #aio_poll will ("sooner or later") reenter the coroutine.
68 * Set to true if the job should abort immediately without waiting
69 * for data to be in sync.
74 * Counter for pause request. If non-zero, the block job is either paused,
75 * or if busy == true will pause itself as soon as possible.
80 * Set to true if the job is paused by user. Can be unpaused with the
81 * block-job-resume QMP command.
86 * Set to false by the job while the coroutine has yielded and may be
87 * re-entered by block_job_enter(). There may still be I/O or event loop
88 * activity pending. Accessed under block_job_mutex (in blockjob.c).
93 * Set to true by the job while it is in a quiescent state, where
94 * no I/O or event loop activity is pending.
99 * Set to true when the job is ready to be completed.
104 * Set to true when the job has deferred work to the main loop.
106 bool deferred_to_main_loop
;
108 /** Status that is published by the query-block-jobs QMP API */
109 BlockDeviceIoStatus iostatus
;
111 /** Offset that is published by the query-block-jobs QMP API */
114 /** Length that is published by the query-block-jobs QMP API */
117 /** Speed that was set with @block_job_set_speed. */
120 /** Rate limiting data structure for implementing @speed. */
123 /** The completion function that will be called when the job completes. */
124 BlockCompletionFunc
*cb
;
126 /** Block other operations when block job is running */
129 /** BlockDriverStates that are involved in this block job */
132 /** The opaque value that is passed to the completion function. */
135 /** Reference count of the block job */
138 /** True when job has reported completion by calling block_job_completed. */
141 /** ret code passed to block_job_completed. */
145 * Timer that is used by @block_job_sleep_ns. Accessed under
146 * block_job_mutex (in blockjob.c).
148 QEMUTimer sleep_timer
;
150 /** True if this job should automatically finalize itself */
153 /** True if this job should automatically dismiss itself */
157 QLIST_ENTRY(BlockJob
) txn_list
;
160 typedef enum BlockJobCreateFlags
{
161 /* Default behavior */
162 BLOCK_JOB_DEFAULT
= 0x00,
163 /* BlockJob is not QMP-created and should not send QMP events */
164 BLOCK_JOB_INTERNAL
= 0x01,
165 /* BlockJob requires manual finalize step */
166 BLOCK_JOB_MANUAL_FINALIZE
= 0x02,
167 /* BlockJob requires manual dismiss step */
168 BLOCK_JOB_MANUAL_DISMISS
= 0x04,
169 } BlockJobCreateFlags
;
173 * @job: A block job, or %NULL.
175 * Get the next element from the list of block jobs after @job, or the
176 * first one if @job is %NULL.
178 * Returns the requested job, or %NULL if there are no more jobs left.
180 BlockJob
*block_job_next(BlockJob
*job
);
184 * @id: The id of the block job.
186 * Get the block job identified by @id (which must not be %NULL).
188 * Returns the requested job, or %NULL if it doesn't exist.
190 BlockJob
*block_job_get(const char *id
);
193 * block_job_add_bdrv:
195 * @name: The name to assign to the new BdrvChild
196 * @bs: A BlockDriverState that is involved in @job
197 * @perm, @shared_perm: Permissions to request on the node
199 * Add @bs to the list of BlockDriverState that are involved in
200 * @job. This means that all operations will be blocked on @bs while
203 int block_job_add_bdrv(BlockJob
*job
, const char *name
, BlockDriverState
*bs
,
204 uint64_t perm
, uint64_t shared_perm
, Error
**errp
);
207 * block_job_remove_all_bdrv:
208 * @job: The block job
210 * Remove all BlockDriverStates from the list of nodes that are involved in the
211 * job. This removes the blockers added with block_job_add_bdrv().
213 void block_job_remove_all_bdrv(BlockJob
*job
);
216 * block_job_set_speed:
217 * @job: The job to set the speed for.
218 * @speed: The new value
219 * @errp: Error object.
221 * Set a rate-limiting parameter for the job; the actual meaning may
222 * vary depending on the job type.
224 void block_job_set_speed(BlockJob
*job
, int64_t speed
, Error
**errp
);
228 * @job: A job that has not yet been started.
230 * Begins execution of a block job.
231 * Takes ownership of one reference to the job object.
233 void block_job_start(BlockJob
*job
);
237 * @job: The job to be canceled.
238 * @force: Quit a job without waiting for data to be in sync.
240 * Asynchronously cancel the specified job.
242 void block_job_cancel(BlockJob
*job
, bool force
);
245 * block_job_complete:
246 * @job: The job to be completed.
247 * @errp: Error object.
249 * Asynchronously complete the specified job.
251 void block_job_complete(BlockJob
*job
, Error
**errp
);
255 * block_job_finalize:
256 * @job: The job to fully commit and finish.
257 * @errp: Error object.
259 * For jobs that have finished their work and are pending
260 * awaiting explicit acknowledgement to commit their work,
261 * This will commit that work.
263 * FIXME: Make the below statement universally true:
264 * For jobs that support the manual workflow mode, all graph
265 * changes that occur as a result will occur after this command
266 * and before a successful reply.
268 void block_job_finalize(BlockJob
*job
, Error
**errp
);
272 * @job: The job to be dismissed.
273 * @errp: Error object.
275 * Remove a concluded job from the query list.
277 void block_job_dismiss(BlockJob
**job
, Error
**errp
);
280 * block_job_progress_update:
281 * @job: The job that has made progress
282 * @done: How much progress the job made
284 * Updates the progress counter of the job.
286 void block_job_progress_update(BlockJob
*job
, uint64_t done
);
289 * block_job_progress_set_remaining:
290 * @job: The job whose expected progress end value is set
291 * @remaining: Expected end value of the progress counter of the job
293 * Sets the expected end value of the progress counter of a job so that a
294 * completion percentage can be calculated when the progress is updated.
296 void block_job_progress_set_remaining(BlockJob
*job
, uint64_t remaining
);
300 * @job: The job to get information about.
302 * Return information about a job.
304 BlockJobInfo
*block_job_query(BlockJob
*job
, Error
**errp
);
307 * block_job_user_pause:
308 * @job: The job to be paused.
310 * Asynchronously pause the specified job.
311 * Do not allow a resume until a matching call to block_job_user_resume.
313 void block_job_user_pause(BlockJob
*job
, Error
**errp
);
317 * @job: The job to query.
319 * Returns true if the job is user-paused.
321 bool block_job_user_paused(BlockJob
*job
);
324 * block_job_user_resume:
325 * @job: The job to be resumed.
327 * Resume the specified job.
328 * Must be paired with a preceding block_job_user_pause.
330 void block_job_user_resume(BlockJob
*job
, Error
**errp
);
333 * block_job_user_cancel:
334 * @job: The job to be cancelled.
335 * @force: Quit a job without waiting for data to be in sync.
337 * Cancels the specified job, but may refuse to do so if the
338 * operation isn't currently meaningful.
340 void block_job_user_cancel(BlockJob
*job
, bool force
, Error
**errp
);
343 * block_job_cancel_sync:
344 * @job: The job to be canceled.
346 * Synchronously cancel the job. The completion callback is called
347 * before the function returns. The job may actually complete
348 * instead of canceling itself; the circumstances under which this
349 * happens depend on the kind of job that is active.
351 * Returns the return value from the job if the job actually completed
352 * during the call, or -ECANCELED if it was canceled.
354 int block_job_cancel_sync(BlockJob
*job
);
357 * block_job_cancel_sync_all:
359 * Synchronously cancels all jobs using block_job_cancel_sync().
361 void block_job_cancel_sync_all(void);
364 * block_job_complete_sync:
365 * @job: The job to be completed.
366 * @errp: Error object which may be set by block_job_complete(); this is not
367 * necessarily set on every error, the job return value has to be
370 * Synchronously complete the job. The completion callback is called before the
371 * function returns, unless it is NULL (which is permissible when using this
374 * Returns the return value from the job.
376 int block_job_complete_sync(BlockJob
*job
, Error
**errp
);
379 * block_job_iostatus_reset:
380 * @job: The job whose I/O status should be reset.
382 * Reset I/O status on @job and on BlockDriverState objects it uses,
383 * other than job->blk.
385 void block_job_iostatus_reset(BlockJob
*job
);
390 * Allocate and return a new block job transaction. Jobs can be added to the
391 * transaction using block_job_txn_add_job().
393 * The transaction is automatically freed when the last job completes or is
396 * All jobs in the transaction either complete successfully or fail/cancel as a
397 * group. Jobs wait for each other before completing. Cancelling one job
398 * cancels all jobs in the transaction.
400 BlockJobTxn
*block_job_txn_new(void);
405 * Add a reference to BlockJob refcnt, it will be decreased with
406 * block_job_unref, and then be freed if it comes to be the last
409 void block_job_ref(BlockJob
*job
);
414 * Release a reference that was previously acquired with block_job_ref
415 * or block_job_create. If it's the last reference to the object, it will be
418 void block_job_unref(BlockJob
*job
);
421 * block_job_txn_unref:
423 * Release a reference that was previously acquired with block_job_txn_add_job
424 * or block_job_txn_new. If it's the last reference to the object, it will be
427 void block_job_txn_unref(BlockJobTxn
*txn
);
430 * block_job_txn_add_job:
431 * @txn: The transaction (may be NULL)
432 * @job: Job to add to the transaction
434 * Add @job to the transaction. The @job must not already be in a transaction.
435 * The caller must call either block_job_txn_unref() or block_job_completed()
436 * to release the reference that is automatically grabbed here.
438 void block_job_txn_add_job(BlockJobTxn
*txn
, BlockJob
*job
);
441 * block_job_is_internal:
442 * @job: The job to determine if it is user-visible or not.
444 * Returns true if the job should not be visible to the management layer.
446 bool block_job_is_internal(BlockJob
*job
);
451 * Returns the driver associated with a block job.
453 const BlockJobDriver
*block_job_driver(BlockJob
*job
);