]> git.ipfire.org Git - thirdparty/qemu.git/blame - blockjob.h
block: introduce BLOCK_JOB_READY event
[thirdparty/qemu.git] / blockjob.h
CommitLineData
2f0c9fe6
PB
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#ifndef BLOCKJOB_H
26#define BLOCKJOB_H 1
27
28#include "block.h"
29
30/**
31 * BlockJobType:
32 *
33 * A class type for block job objects.
34 */
35typedef struct BlockJobType {
36 /** Derived BlockJob struct size */
37 size_t instance_size;
38
39 /** String describing the operation, part of query-block-jobs QMP API */
40 const char *job_type;
41
42 /** Optional callback for job types that support setting a speed limit */
43 void (*set_speed)(BlockJob *job, int64_t speed, Error **errp);
aeae883b
PB
44
45 /**
46 * Optional callback for job types whose completion must be triggered
47 * manually.
48 */
49 void (*complete)(BlockJob *job, Error **errp);
2f0c9fe6
PB
50} BlockJobType;
51
52/**
53 * BlockJob:
54 *
55 * Long-running operation on a BlockDriverState.
56 */
57struct BlockJob {
58 /** The job type, including the job vtable. */
59 const BlockJobType *job_type;
60
61 /** The block device on which the job is operating. */
62 BlockDriverState *bs;
63
64 /**
65 * The coroutine that executes the job. If not NULL, it is
66 * reentered when busy is false and the job is cancelled.
67 */
68 Coroutine *co;
69
70 /**
71 * Set to true if the job should cancel itself. The flag must
72 * always be tested just before toggling the busy flag from false
73 * to true. After a job has been cancelled, it should only yield
74 * if #qemu_aio_wait will ("sooner or later") reenter the coroutine.
75 */
76 bool cancelled;
77
8acc72a4
PB
78 /**
79 * Set to true if the job is either paused, or will pause itself
80 * as soon as possible (if busy == true).
81 */
82 bool paused;
83
2f0c9fe6
PB
84 /**
85 * Set to false by the job while it is in a quiescent state, where
86 * no I/O is pending and the job has yielded on any condition
87 * that is not detected by #qemu_aio_wait, such as a timer.
88 */
89 bool busy;
90
32c81a4a
PB
91 /** Status that is published by the query-block-jobs QMP API */
92 BlockDeviceIoStatus iostatus;
93
2f0c9fe6
PB
94 /** Offset that is published by the query-block-jobs QMP API */
95 int64_t offset;
96
97 /** Length that is published by the query-block-jobs QMP API */
98 int64_t len;
99
100 /** Speed that was set with @block_job_set_speed. */
101 int64_t speed;
102
103 /** The completion function that will be called when the job completes. */
104 BlockDriverCompletionFunc *cb;
105
106 /** The opaque value that is passed to the completion function. */
107 void *opaque;
108};
109
110/**
111 * block_job_create:
112 * @job_type: The class object for the newly-created job.
113 * @bs: The block
114 * @speed: The maximum speed, in bytes per second, or 0 for unlimited.
115 * @cb: Completion function for the job.
116 * @opaque: Opaque pointer value passed to @cb.
117 * @errp: Error object.
118 *
119 * Create a new long-running block device job and return it. The job
120 * will call @cb asynchronously when the job completes. Note that
121 * @bs may have been closed at the time the @cb it is called. If
122 * this is the case, the job may be reported as either cancelled or
123 * completed.
124 *
125 * This function is not part of the public job interface; it should be
126 * called from a wrapper that is specific to the job type.
127 */
128void *block_job_create(const BlockJobType *job_type, BlockDriverState *bs,
129 int64_t speed, BlockDriverCompletionFunc *cb,
130 void *opaque, Error **errp);
131
132/**
133 * block_job_sleep_ns:
134 * @job: The job that calls the function.
135 * @clock: The clock to sleep on.
136 * @ns: How many nanoseconds to stop for.
137 *
138 * Put the job to sleep (assuming that it wasn't canceled) for @ns
139 * nanoseconds. Canceling the job will interrupt the wait immediately.
140 */
141void block_job_sleep_ns(BlockJob *job, QEMUClock *clock, int64_t ns);
142
143/**
65f46322 144 * block_job_completed:
2f0c9fe6
PB
145 * @job: The job being completed.
146 * @ret: The status code.
147 *
148 * Call the completion function that was registered at creation time, and
149 * free @job.
150 */
65f46322 151void block_job_completed(BlockJob *job, int ret);
2f0c9fe6
PB
152
153/**
154 * block_job_set_speed:
155 * @job: The job to set the speed for.
156 * @speed: The new value
157 * @errp: Error object.
158 *
159 * Set a rate-limiting parameter for the job; the actual meaning may
160 * vary depending on the job type.
161 */
162void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp);
163
164/**
165 * block_job_cancel:
166 * @job: The job to be canceled.
167 *
168 * Asynchronously cancel the specified job.
169 */
170void block_job_cancel(BlockJob *job);
171
aeae883b
PB
172/**
173 * block_job_complete:
174 * @job: The job to be completed.
175 * @errp: Error object.
176 *
177 * Asynchronously complete the specified job.
178 */
179void block_job_complete(BlockJob *job, Error **errp);
180
2f0c9fe6
PB
181/**
182 * block_job_is_cancelled:
183 * @job: The job being queried.
184 *
185 * Returns whether the job is scheduled for cancellation.
186 */
187bool block_job_is_cancelled(BlockJob *job);
188
30e628b7
PB
189/**
190 * block_job_query:
191 * @job: The job to get information about.
192 *
193 * Return information about a job.
194 */
195BlockJobInfo *block_job_query(BlockJob *job);
196
8acc72a4
PB
197/**
198 * block_job_pause:
199 * @job: The job to be paused.
200 *
201 * Asynchronously pause the specified job.
202 */
203void block_job_pause(BlockJob *job);
204
205/**
206 * block_job_resume:
207 * @job: The job to be resumed.
208 *
209 * Resume the specified job.
210 */
211void block_job_resume(BlockJob *job);
212
a66a2a36
PB
213/**
214 * qobject_from_block_job:
215 * @job: The job whose information is requested.
216 *
217 * Return a QDict corresponding to @job's query-block-jobs entry.
218 */
219QObject *qobject_from_block_job(BlockJob *job);
220
221/**
222 * block_job_ready:
223 * @job: The job which is now ready to complete.
224 *
225 * Send a BLOCK_JOB_READY event for the specified job.
226 */
227void block_job_ready(BlockJob *job);
228
8acc72a4
PB
229/**
230 * block_job_is_paused:
231 * @job: The job being queried.
232 *
233 * Returns whether the job is currently paused, or will pause
234 * as soon as it reaches a sleeping point.
235 */
236bool block_job_is_paused(BlockJob *job);
237
2f0c9fe6
PB
238/**
239 * block_job_cancel_sync:
240 * @job: The job to be canceled.
241 *
242 * Synchronously cancel the job. The completion callback is called
243 * before the function returns. The job may actually complete
244 * instead of canceling itself; the circumstances under which this
245 * happens depend on the kind of job that is active.
246 *
247 * Returns the return value from the job if the job actually completed
248 * during the call, or -ECANCELED if it was canceled.
249 */
250int block_job_cancel_sync(BlockJob *job);
251
32c81a4a
PB
252/**
253 * block_job_iostatus_reset:
254 * @job: The job whose I/O status should be reset.
255 *
256 * Reset I/O status on @job.
257 */
258void block_job_iostatus_reset(BlockJob *job);
259
260/**
261 * block_job_error_action:
262 * @job: The job to signal an error for.
263 * @bs: The block device on which to set an I/O error.
264 * @on_err: The error action setting.
265 * @is_read: Whether the operation was a read.
266 * @error: The error that was reported.
267 *
268 * Report an I/O error for a block job and possibly stop the VM. Return the
269 * action that was selected based on @on_err and @error.
270 */
271BlockErrorAction block_job_error_action(BlockJob *job, BlockDriverState *bs,
272 BlockdevOnError on_err,
273 int is_read, int error);
2f0c9fe6 274#endif