monitor: fix dangling CPU pointer
[qemu.git] / include / block / blockjob.h
blob67c0968fa50268867f83e006b4b8aba9dd5069f8
1 /*
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
23 * THE SOFTWARE.
26 #ifndef BLOCKJOB_H
27 #define BLOCKJOB_H
29 #include "block/block.h"
31 typedef struct BlockJobDriver BlockJobDriver;
32 typedef struct BlockJobTxn BlockJobTxn;
34 /**
35 * BlockJob:
37 * Long-running operation on a BlockDriverState.
39 typedef struct BlockJob {
40 /** The job type, including the job vtable. */
41 const BlockJobDriver *driver;
43 /** The block device on which the job is operating. */
44 BlockBackend *blk;
46 /**
47 * The ID of the block job. May be NULL for internal jobs.
49 char *id;
51 /**
52 * The coroutine that executes the job. If not NULL, it is
53 * reentered when busy is false and the job is cancelled.
55 Coroutine *co;
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.
63 bool cancelled;
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.
69 int pause_count;
71 /**
72 * Set to true if the job is paused by user. Can be unpaused with the
73 * block-job-resume QMP command.
75 bool user_paused;
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.
82 bool busy;
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.
88 bool paused;
90 /**
91 * Set to true when the job is ready to be completed.
93 bool ready;
95 /**
96 * Set to true when the job has deferred work to the main loop.
98 bool deferred_to_main_loop;
100 /** Element of the list of block jobs */
101 QLIST_ENTRY(BlockJob) job_list;
103 /** Status that is published by the query-block-jobs QMP API */
104 BlockDeviceIoStatus iostatus;
106 /** Offset that is published by the query-block-jobs QMP API */
107 int64_t offset;
109 /** Length that is published by the query-block-jobs QMP API */
110 int64_t len;
112 /** Speed that was set with @block_job_set_speed. */
113 int64_t speed;
115 /** The completion function that will be called when the job completes. */
116 BlockCompletionFunc *cb;
118 /** Block other operations when block job is running */
119 Error *blocker;
121 /** BlockDriverStates that are involved in this block job */
122 GSList *nodes;
124 /** The opaque value that is passed to the completion function. */
125 void *opaque;
127 /** Reference count of the block job */
128 int refcnt;
130 /* True if this job has reported completion by calling block_job_completed.
132 bool completed;
134 /* ret code passed to block_job_completed.
136 int ret;
138 /** Non-NULL if this job is part of a transaction */
139 BlockJobTxn *txn;
140 QLIST_ENTRY(BlockJob) txn_list;
141 } BlockJob;
143 typedef enum BlockJobCreateFlags {
144 BLOCK_JOB_DEFAULT = 0x00,
145 BLOCK_JOB_INTERNAL = 0x01,
146 } BlockJobCreateFlags;
149 * block_job_next:
150 * @job: A block job, or %NULL.
152 * Get the next element from the list of block jobs after @job, or the
153 * first one if @job is %NULL.
155 * Returns the requested job, or %NULL if there are no more jobs left.
157 BlockJob *block_job_next(BlockJob *job);
160 * block_job_get:
161 * @id: The id of the block job.
163 * Get the block job identified by @id (which must not be %NULL).
165 * Returns the requested job, or %NULL if it doesn't exist.
167 BlockJob *block_job_get(const char *id);
170 * block_job_add_bdrv:
171 * @job: A block job
172 * @name: The name to assign to the new BdrvChild
173 * @bs: A BlockDriverState that is involved in @job
174 * @perm, @shared_perm: Permissions to request on the node
176 * Add @bs to the list of BlockDriverState that are involved in
177 * @job. This means that all operations will be blocked on @bs while
178 * @job exists.
180 int block_job_add_bdrv(BlockJob *job, const char *name, BlockDriverState *bs,
181 uint64_t perm, uint64_t shared_perm, Error **errp);
184 * block_job_remove_all_bdrv:
185 * @job: The block job
187 * Remove all BlockDriverStates from the list of nodes that are involved in the
188 * job. This removes the blockers added with block_job_add_bdrv().
190 void block_job_remove_all_bdrv(BlockJob *job);
193 * block_job_set_speed:
194 * @job: The job to set the speed for.
195 * @speed: The new value
196 * @errp: Error object.
198 * Set a rate-limiting parameter for the job; the actual meaning may
199 * vary depending on the job type.
201 void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp);
204 * block_job_start:
205 * @job: A job that has not yet been started.
207 * Begins execution of a block job.
208 * Takes ownership of one reference to the job object.
210 void block_job_start(BlockJob *job);
213 * block_job_cancel:
214 * @job: The job to be canceled.
216 * Asynchronously cancel the specified job.
218 void block_job_cancel(BlockJob *job);
221 * block_job_complete:
222 * @job: The job to be completed.
223 * @errp: Error object.
225 * Asynchronously complete the specified job.
227 void block_job_complete(BlockJob *job, Error **errp);
230 * block_job_query:
231 * @job: The job to get information about.
233 * Return information about a job.
235 BlockJobInfo *block_job_query(BlockJob *job, Error **errp);
238 * block_job_user_pause:
239 * @job: The job to be paused.
241 * Asynchronously pause the specified job.
242 * Do not allow a resume until a matching call to block_job_user_resume.
244 void block_job_user_pause(BlockJob *job);
247 * block_job_paused:
248 * @job: The job to query.
250 * Returns true if the job is user-paused.
252 bool block_job_user_paused(BlockJob *job);
255 * block_job_user_resume:
256 * @job: The job to be resumed.
258 * Resume the specified job.
259 * Must be paired with a preceding block_job_user_pause.
261 void block_job_user_resume(BlockJob *job);
264 * block_job_cancel_sync:
265 * @job: The job to be canceled.
267 * Synchronously cancel the job. The completion callback is called
268 * before the function returns. The job may actually complete
269 * instead of canceling itself; the circumstances under which this
270 * happens depend on the kind of job that is active.
272 * Returns the return value from the job if the job actually completed
273 * during the call, or -ECANCELED if it was canceled.
275 int block_job_cancel_sync(BlockJob *job);
278 * block_job_cancel_sync_all:
280 * Synchronously cancels all jobs using block_job_cancel_sync().
282 void block_job_cancel_sync_all(void);
285 * block_job_complete_sync:
286 * @job: The job to be completed.
287 * @errp: Error object which may be set by block_job_complete(); this is not
288 * necessarily set on every error, the job return value has to be
289 * checked as well.
291 * Synchronously complete the job. The completion callback is called before the
292 * function returns, unless it is NULL (which is permissible when using this
293 * function).
295 * Returns the return value from the job.
297 int block_job_complete_sync(BlockJob *job, Error **errp);
300 * block_job_iostatus_reset:
301 * @job: The job whose I/O status should be reset.
303 * Reset I/O status on @job and on BlockDriverState objects it uses,
304 * other than job->blk.
306 void block_job_iostatus_reset(BlockJob *job);
309 * block_job_txn_new:
311 * Allocate and return a new block job transaction. Jobs can be added to the
312 * transaction using block_job_txn_add_job().
314 * The transaction is automatically freed when the last job completes or is
315 * cancelled.
317 * All jobs in the transaction either complete successfully or fail/cancel as a
318 * group. Jobs wait for each other before completing. Cancelling one job
319 * cancels all jobs in the transaction.
321 BlockJobTxn *block_job_txn_new(void);
324 * block_job_ref:
326 * Add a reference to BlockJob refcnt, it will be decreased with
327 * block_job_unref, and then be freed if it comes to be the last
328 * reference.
330 void block_job_ref(BlockJob *job);
333 * block_job_unref:
335 * Release a reference that was previously acquired with block_job_ref
336 * or block_job_create. If it's the last reference to the object, it will be
337 * freed.
339 void block_job_unref(BlockJob *job);
342 * block_job_txn_unref:
344 * Release a reference that was previously acquired with block_job_txn_add_job
345 * or block_job_txn_new. If it's the last reference to the object, it will be
346 * freed.
348 void block_job_txn_unref(BlockJobTxn *txn);
351 * block_job_txn_add_job:
352 * @txn: The transaction (may be NULL)
353 * @job: Job to add to the transaction
355 * Add @job to the transaction. The @job must not already be in a transaction.
356 * The caller must call either block_job_txn_unref() or block_job_completed()
357 * to release the reference that is automatically grabbed here.
359 void block_job_txn_add_job(BlockJobTxn *txn, BlockJob *job);
362 * block_job_is_internal:
363 * @job: The job to determine if it is user-visible or not.
365 * Returns true if the job should not be visible to the management layer.
367 bool block_job_is_internal(BlockJob *job);
369 #endif