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
;
40 * Long-running operation on a BlockDriverState.
42 typedef struct BlockJob
{
43 /** Data belonging to the generic Job infrastructure */
46 /** The job type, including the job vtable. */
47 const BlockJobDriver
*driver
;
49 /** The block device on which the job is operating. */
53 * Set to true when the job is ready to be completed.
57 /** Status that is published by the query-block-jobs QMP API */
58 BlockDeviceIoStatus iostatus
;
60 /** Offset that is published by the query-block-jobs QMP API */
63 /** Length that is published by the query-block-jobs QMP API */
66 /** Speed that was set with @block_job_set_speed. */
69 /** Rate limiting data structure for implementing @speed. */
72 /** Block other operations when block job is running */
75 /** Called when a cancelled job is finalised. */
76 Notifier finalize_cancelled_notifier
;
78 /** Called when a successfully completed job is finalised. */
79 Notifier finalize_completed_notifier
;
81 /** Called when the job transitions to PENDING */
82 Notifier pending_notifier
;
84 /** BlockDriverStates that are involved in this block job */
90 * @job: A block job, or %NULL.
92 * Get the next element from the list of block jobs after @job, or the
93 * first one if @job is %NULL.
95 * Returns the requested job, or %NULL if there are no more jobs left.
97 BlockJob
*block_job_next(BlockJob
*job
);
101 * @id: The id of the block job.
103 * Get the block job identified by @id (which must not be %NULL).
105 * Returns the requested job, or %NULL if it doesn't exist.
107 BlockJob
*block_job_get(const char *id
);
110 * block_job_add_bdrv:
112 * @name: The name to assign to the new BdrvChild
113 * @bs: A BlockDriverState that is involved in @job
114 * @perm, @shared_perm: Permissions to request on the node
116 * Add @bs to the list of BlockDriverState that are involved in
117 * @job. This means that all operations will be blocked on @bs while
120 int block_job_add_bdrv(BlockJob
*job
, const char *name
, BlockDriverState
*bs
,
121 uint64_t perm
, uint64_t shared_perm
, Error
**errp
);
124 * block_job_remove_all_bdrv:
125 * @job: The block job
127 * Remove all BlockDriverStates from the list of nodes that are involved in the
128 * job. This removes the blockers added with block_job_add_bdrv().
130 void block_job_remove_all_bdrv(BlockJob
*job
);
133 * block_job_set_speed:
134 * @job: The job to set the speed for.
135 * @speed: The new value
136 * @errp: Error object.
138 * Set a rate-limiting parameter for the job; the actual meaning may
139 * vary depending on the job type.
141 void block_job_set_speed(BlockJob
*job
, int64_t speed
, Error
**errp
);
145 * @job: The job to be canceled.
146 * @force: Quit a job without waiting for data to be in sync.
148 * Asynchronously cancel the specified job.
150 void block_job_cancel(BlockJob
*job
, bool force
);
154 * @job: The job to be dismissed.
155 * @errp: Error object.
157 * Remove a concluded job from the query list.
159 void block_job_dismiss(BlockJob
**job
, Error
**errp
);
162 * block_job_progress_update:
163 * @job: The job that has made progress
164 * @done: How much progress the job made
166 * Updates the progress counter of the job.
168 void block_job_progress_update(BlockJob
*job
, uint64_t done
);
171 * block_job_progress_set_remaining:
172 * @job: The job whose expected progress end value is set
173 * @remaining: Expected end value of the progress counter of the job
175 * Sets the expected end value of the progress counter of a job so that a
176 * completion percentage can be calculated when the progress is updated.
178 void block_job_progress_set_remaining(BlockJob
*job
, uint64_t remaining
);
182 * @job: The job to get information about.
184 * Return information about a job.
186 BlockJobInfo
*block_job_query(BlockJob
*job
, Error
**errp
);
189 * block_job_user_cancel:
190 * @job: The job to be cancelled.
191 * @force: Quit a job without waiting for data to be in sync.
193 * Cancels the specified job, but may refuse to do so if the
194 * operation isn't currently meaningful.
196 void block_job_user_cancel(BlockJob
*job
, bool force
, Error
**errp
);
199 * block_job_cancel_sync:
200 * @job: The job to be canceled.
202 * Synchronously cancel the job. The completion callback is called
203 * before the function returns. The job may actually complete
204 * instead of canceling itself; the circumstances under which this
205 * happens depend on the kind of job that is active.
207 * Returns the return value from the job if the job actually completed
208 * during the call, or -ECANCELED if it was canceled.
210 int block_job_cancel_sync(BlockJob
*job
);
213 * block_job_cancel_sync_all:
215 * Synchronously cancels all jobs using block_job_cancel_sync().
217 void block_job_cancel_sync_all(void);
220 * block_job_complete_sync:
221 * @job: The job to be completed.
222 * @errp: Error object which may be set by block_job_complete(); this is not
223 * necessarily set on every error, the job return value has to be
226 * Synchronously complete the job. The completion callback is called before the
227 * function returns, unless it is NULL (which is permissible when using this
230 * Returns the return value from the job.
232 int block_job_complete_sync(BlockJob
*job
, Error
**errp
);
235 * block_job_iostatus_reset:
236 * @job: The job whose I/O status should be reset.
238 * Reset I/O status on @job and on BlockDriverState objects it uses,
239 * other than job->blk.
241 void block_job_iostatus_reset(BlockJob
*job
);
244 * block_job_is_internal:
245 * @job: The job to determine if it is user-visible or not.
247 * Returns true if the job should not be visible to the management layer.
249 bool block_job_is_internal(BlockJob
*job
);
254 * Returns the driver associated with a block job.
256 const BlockJobDriver
*block_job_driver(BlockJob
*job
);