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