]> git.proxmox.com Git - mirror_qemu.git/blob - include/block/blockjob.h
job: Move single job finalisation to Job
[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 "qemu/job.h"
30 #include "block/block.h"
31 #include "qemu/ratelimit.h"
32
33 #define BLOCK_JOB_SLICE_TIME 100000000ULL /* ns */
34
35 typedef struct BlockJobDriver BlockJobDriver;
36 typedef struct BlockJobTxn BlockJobTxn;
37
38 /**
39 * BlockJob:
40 *
41 * Long-running operation on a BlockDriverState.
42 */
43 typedef struct BlockJob {
44 /** Data belonging to the generic Job infrastructure */
45 Job job;
46
47 /** The job type, including the job vtable. */
48 const BlockJobDriver *driver;
49
50 /** The block device on which the job is operating. */
51 BlockBackend *blk;
52
53 /**
54 * Set to true if the job should abort immediately without waiting
55 * for data to be in sync.
56 */
57 bool force;
58
59 /**
60 * Set to true when the job is ready to be completed.
61 */
62 bool ready;
63
64 /** Status that is published by the query-block-jobs QMP API */
65 BlockDeviceIoStatus iostatus;
66
67 /** Offset that is published by the query-block-jobs QMP API */
68 int64_t offset;
69
70 /** Length that is published by the query-block-jobs QMP API */
71 int64_t len;
72
73 /** Speed that was set with @block_job_set_speed. */
74 int64_t speed;
75
76 /** Rate limiting data structure for implementing @speed. */
77 RateLimit limit;
78
79 /** Block other operations when block job is running */
80 Error *blocker;
81
82 /** Called when a cancelled job is finalised. */
83 Notifier finalize_cancelled_notifier;
84
85 /** Called when a successfully completed job is finalised. */
86 Notifier finalize_completed_notifier;
87
88 /** Called when the job transitions to PENDING */
89 Notifier pending_notifier;
90
91 /** BlockDriverStates that are involved in this block job */
92 GSList *nodes;
93
94 BlockJobTxn *txn;
95 QLIST_ENTRY(BlockJob) txn_list;
96 } BlockJob;
97
98 /**
99 * block_job_next:
100 * @job: A block job, or %NULL.
101 *
102 * Get the next element from the list of block jobs after @job, or the
103 * first one if @job is %NULL.
104 *
105 * Returns the requested job, or %NULL if there are no more jobs left.
106 */
107 BlockJob *block_job_next(BlockJob *job);
108
109 /**
110 * block_job_get:
111 * @id: The id of the block job.
112 *
113 * Get the block job identified by @id (which must not be %NULL).
114 *
115 * Returns the requested job, or %NULL if it doesn't exist.
116 */
117 BlockJob *block_job_get(const char *id);
118
119 /**
120 * block_job_add_bdrv:
121 * @job: A block job
122 * @name: The name to assign to the new BdrvChild
123 * @bs: A BlockDriverState that is involved in @job
124 * @perm, @shared_perm: Permissions to request on the node
125 *
126 * Add @bs to the list of BlockDriverState that are involved in
127 * @job. This means that all operations will be blocked on @bs while
128 * @job exists.
129 */
130 int block_job_add_bdrv(BlockJob *job, const char *name, BlockDriverState *bs,
131 uint64_t perm, uint64_t shared_perm, Error **errp);
132
133 /**
134 * block_job_remove_all_bdrv:
135 * @job: The block job
136 *
137 * Remove all BlockDriverStates from the list of nodes that are involved in the
138 * job. This removes the blockers added with block_job_add_bdrv().
139 */
140 void block_job_remove_all_bdrv(BlockJob *job);
141
142 /**
143 * block_job_set_speed:
144 * @job: The job to set the speed for.
145 * @speed: The new value
146 * @errp: Error object.
147 *
148 * Set a rate-limiting parameter for the job; the actual meaning may
149 * vary depending on the job type.
150 */
151 void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp);
152
153 /**
154 * block_job_cancel:
155 * @job: The job to be canceled.
156 * @force: Quit a job without waiting for data to be in sync.
157 *
158 * Asynchronously cancel the specified job.
159 */
160 void block_job_cancel(BlockJob *job, bool force);
161
162 /**
163 * block_job_complete:
164 * @job: The job to be completed.
165 * @errp: Error object.
166 *
167 * Asynchronously complete the specified job.
168 */
169 void block_job_complete(BlockJob *job, Error **errp);
170
171
172 /**
173 * block_job_finalize:
174 * @job: The job to fully commit and finish.
175 * @errp: Error object.
176 *
177 * For jobs that have finished their work and are pending
178 * awaiting explicit acknowledgement to commit their work,
179 * This will commit that work.
180 *
181 * FIXME: Make the below statement universally true:
182 * For jobs that support the manual workflow mode, all graph
183 * changes that occur as a result will occur after this command
184 * and before a successful reply.
185 */
186 void block_job_finalize(BlockJob *job, Error **errp);
187
188 /**
189 * block_job_dismiss:
190 * @job: The job to be dismissed.
191 * @errp: Error object.
192 *
193 * Remove a concluded job from the query list.
194 */
195 void block_job_dismiss(BlockJob **job, Error **errp);
196
197 /**
198 * block_job_progress_update:
199 * @job: The job that has made progress
200 * @done: How much progress the job made
201 *
202 * Updates the progress counter of the job.
203 */
204 void block_job_progress_update(BlockJob *job, uint64_t done);
205
206 /**
207 * block_job_progress_set_remaining:
208 * @job: The job whose expected progress end value is set
209 * @remaining: Expected end value of the progress counter of the job
210 *
211 * Sets the expected end value of the progress counter of a job so that a
212 * completion percentage can be calculated when the progress is updated.
213 */
214 void block_job_progress_set_remaining(BlockJob *job, uint64_t remaining);
215
216 /**
217 * block_job_query:
218 * @job: The job to get information about.
219 *
220 * Return information about a job.
221 */
222 BlockJobInfo *block_job_query(BlockJob *job, Error **errp);
223
224 /**
225 * block_job_user_cancel:
226 * @job: The job to be cancelled.
227 * @force: Quit a job without waiting for data to be in sync.
228 *
229 * Cancels the specified job, but may refuse to do so if the
230 * operation isn't currently meaningful.
231 */
232 void block_job_user_cancel(BlockJob *job, bool force, Error **errp);
233
234 /**
235 * block_job_cancel_sync:
236 * @job: The job to be canceled.
237 *
238 * Synchronously cancel the job. The completion callback is called
239 * before the function returns. The job may actually complete
240 * instead of canceling itself; the circumstances under which this
241 * happens depend on the kind of job that is active.
242 *
243 * Returns the return value from the job if the job actually completed
244 * during the call, or -ECANCELED if it was canceled.
245 */
246 int block_job_cancel_sync(BlockJob *job);
247
248 /**
249 * block_job_cancel_sync_all:
250 *
251 * Synchronously cancels all jobs using block_job_cancel_sync().
252 */
253 void block_job_cancel_sync_all(void);
254
255 /**
256 * block_job_complete_sync:
257 * @job: The job to be completed.
258 * @errp: Error object which may be set by block_job_complete(); this is not
259 * necessarily set on every error, the job return value has to be
260 * checked as well.
261 *
262 * Synchronously complete the job. The completion callback is called before the
263 * function returns, unless it is NULL (which is permissible when using this
264 * function).
265 *
266 * Returns the return value from the job.
267 */
268 int block_job_complete_sync(BlockJob *job, Error **errp);
269
270 /**
271 * block_job_iostatus_reset:
272 * @job: The job whose I/O status should be reset.
273 *
274 * Reset I/O status on @job and on BlockDriverState objects it uses,
275 * other than job->blk.
276 */
277 void block_job_iostatus_reset(BlockJob *job);
278
279 /**
280 * block_job_txn_new:
281 *
282 * Allocate and return a new block job transaction. Jobs can be added to the
283 * transaction using block_job_txn_add_job().
284 *
285 * The transaction is automatically freed when the last job completes or is
286 * cancelled.
287 *
288 * All jobs in the transaction either complete successfully or fail/cancel as a
289 * group. Jobs wait for each other before completing. Cancelling one job
290 * cancels all jobs in the transaction.
291 */
292 BlockJobTxn *block_job_txn_new(void);
293
294 /**
295 * block_job_txn_unref:
296 *
297 * Release a reference that was previously acquired with block_job_txn_add_job
298 * or block_job_txn_new. If it's the last reference to the object, it will be
299 * freed.
300 */
301 void block_job_txn_unref(BlockJobTxn *txn);
302
303 /**
304 * block_job_txn_add_job:
305 * @txn: The transaction (may be NULL)
306 * @job: Job to add to the transaction
307 *
308 * Add @job to the transaction. The @job must not already be in a transaction.
309 * The caller must call either block_job_txn_unref() or block_job_completed()
310 * to release the reference that is automatically grabbed here.
311 */
312 void block_job_txn_add_job(BlockJobTxn *txn, BlockJob *job);
313
314 /**
315 * block_job_is_internal:
316 * @job: The job to determine if it is user-visible or not.
317 *
318 * Returns true if the job should not be visible to the management layer.
319 */
320 bool block_job_is_internal(BlockJob *job);
321
322 /**
323 * block_job_driver:
324 *
325 * Returns the driver associated with a block job.
326 */
327 const BlockJobDriver *block_job_driver(BlockJob *job);
328
329 #endif