sysbus: Correct SYSTEM_BUS(obj) defines
[qemu.git] / blockjob.c
1 /*
2 * QEMU System Emulator block driver
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 #include "config-host.h"
27 #include "qemu-common.h"
28 #include "trace.h"
29 #include "block/block.h"
30 #include "block/blockjob.h"
31 #include "block/block_int.h"
32 #include "qapi/qmp/qjson.h"
33 #include "block/coroutine.h"
34 #include "qmp-commands.h"
35 #include "qemu/timer.h"
36 #include "qapi-event.h"
37
38 void *block_job_create(const BlockJobDriver *driver, BlockDriverState *bs,
39 int64_t speed, BlockCompletionFunc *cb,
40 void *opaque, Error **errp)
41 {
42 BlockJob *job;
43
44 if (bs->job) {
45 error_set(errp, QERR_DEVICE_IN_USE, bdrv_get_device_name(bs));
46 return NULL;
47 }
48 bdrv_ref(bs);
49 job = g_malloc0(driver->instance_size);
50 error_setg(&job->blocker, "block device is in use by block job: %s",
51 BlockJobType_lookup[driver->job_type]);
52 bdrv_op_block_all(bs, job->blocker);
53
54 job->driver = driver;
55 job->bs = bs;
56 job->cb = cb;
57 job->opaque = opaque;
58 job->busy = true;
59 bs->job = job;
60
61 /* Only set speed when necessary to avoid NotSupported error */
62 if (speed != 0) {
63 Error *local_err = NULL;
64
65 block_job_set_speed(job, speed, &local_err);
66 if (local_err) {
67 bs->job = NULL;
68 bdrv_op_unblock_all(bs, job->blocker);
69 error_free(job->blocker);
70 g_free(job);
71 error_propagate(errp, local_err);
72 return NULL;
73 }
74 }
75 return job;
76 }
77
78 void block_job_completed(BlockJob *job, int ret)
79 {
80 BlockDriverState *bs = job->bs;
81
82 assert(bs->job == job);
83 job->cb(job->opaque, ret);
84 bs->job = NULL;
85 bdrv_op_unblock_all(bs, job->blocker);
86 error_free(job->blocker);
87 g_free(job);
88 }
89
90 void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp)
91 {
92 Error *local_err = NULL;
93
94 if (!job->driver->set_speed) {
95 error_set(errp, QERR_UNSUPPORTED);
96 return;
97 }
98 job->driver->set_speed(job, speed, &local_err);
99 if (local_err) {
100 error_propagate(errp, local_err);
101 return;
102 }
103
104 job->speed = speed;
105 }
106
107 void block_job_complete(BlockJob *job, Error **errp)
108 {
109 if (job->paused || job->cancelled || !job->driver->complete) {
110 error_set(errp, QERR_BLOCK_JOB_NOT_READY,
111 bdrv_get_device_name(job->bs));
112 return;
113 }
114
115 job->driver->complete(job, errp);
116 }
117
118 void block_job_pause(BlockJob *job)
119 {
120 job->paused = true;
121 }
122
123 bool block_job_is_paused(BlockJob *job)
124 {
125 return job->paused;
126 }
127
128 void block_job_resume(BlockJob *job)
129 {
130 job->paused = false;
131 block_job_iostatus_reset(job);
132 if (job->co && !job->busy) {
133 qemu_coroutine_enter(job->co, NULL);
134 }
135 }
136
137 void block_job_cancel(BlockJob *job)
138 {
139 job->cancelled = true;
140 block_job_resume(job);
141 }
142
143 bool block_job_is_cancelled(BlockJob *job)
144 {
145 return job->cancelled;
146 }
147
148 void block_job_iostatus_reset(BlockJob *job)
149 {
150 job->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
151 if (job->driver->iostatus_reset) {
152 job->driver->iostatus_reset(job);
153 }
154 }
155
156 struct BlockCancelData {
157 BlockJob *job;
158 BlockCompletionFunc *cb;
159 void *opaque;
160 bool cancelled;
161 int ret;
162 };
163
164 static void block_job_cancel_cb(void *opaque, int ret)
165 {
166 struct BlockCancelData *data = opaque;
167
168 data->cancelled = block_job_is_cancelled(data->job);
169 data->ret = ret;
170 data->cb(data->opaque, ret);
171 }
172
173 int block_job_cancel_sync(BlockJob *job)
174 {
175 struct BlockCancelData data;
176 BlockDriverState *bs = job->bs;
177
178 assert(bs->job == job);
179
180 /* Set up our own callback to store the result and chain to
181 * the original callback.
182 */
183 data.job = job;
184 data.cb = job->cb;
185 data.opaque = job->opaque;
186 data.ret = -EINPROGRESS;
187 job->cb = block_job_cancel_cb;
188 job->opaque = &data;
189 block_job_cancel(job);
190 while (data.ret == -EINPROGRESS) {
191 aio_poll(bdrv_get_aio_context(bs), true);
192 }
193 return (data.cancelled && data.ret == 0) ? -ECANCELED : data.ret;
194 }
195
196 void block_job_sleep_ns(BlockJob *job, QEMUClockType type, int64_t ns)
197 {
198 assert(job->busy);
199
200 /* Check cancellation *before* setting busy = false, too! */
201 if (block_job_is_cancelled(job)) {
202 return;
203 }
204
205 job->busy = false;
206 if (block_job_is_paused(job)) {
207 qemu_coroutine_yield();
208 } else {
209 co_aio_sleep_ns(bdrv_get_aio_context(job->bs), type, ns);
210 }
211 job->busy = true;
212 }
213
214 void block_job_yield(BlockJob *job)
215 {
216 assert(job->busy);
217
218 /* Check cancellation *before* setting busy = false, too! */
219 if (block_job_is_cancelled(job)) {
220 return;
221 }
222
223 job->busy = false;
224 qemu_coroutine_yield();
225 job->busy = true;
226 }
227
228 BlockJobInfo *block_job_query(BlockJob *job)
229 {
230 BlockJobInfo *info = g_new0(BlockJobInfo, 1);
231 info->type = g_strdup(BlockJobType_lookup[job->driver->job_type]);
232 info->device = g_strdup(bdrv_get_device_name(job->bs));
233 info->len = job->len;
234 info->busy = job->busy;
235 info->paused = job->paused;
236 info->offset = job->offset;
237 info->speed = job->speed;
238 info->io_status = job->iostatus;
239 return info;
240 }
241
242 static void block_job_iostatus_set_err(BlockJob *job, int error)
243 {
244 if (job->iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
245 job->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE :
246 BLOCK_DEVICE_IO_STATUS_FAILED;
247 }
248 }
249
250 void block_job_event_cancelled(BlockJob *job)
251 {
252 qapi_event_send_block_job_cancelled(job->driver->job_type,
253 bdrv_get_device_name(job->bs),
254 job->len,
255 job->offset,
256 job->speed,
257 &error_abort);
258 }
259
260 void block_job_event_completed(BlockJob *job, const char *msg)
261 {
262 qapi_event_send_block_job_completed(job->driver->job_type,
263 bdrv_get_device_name(job->bs),
264 job->len,
265 job->offset,
266 job->speed,
267 !!msg,
268 msg,
269 &error_abort);
270 }
271
272 void block_job_event_ready(BlockJob *job)
273 {
274 qapi_event_send_block_job_ready(job->driver->job_type,
275 bdrv_get_device_name(job->bs),
276 job->len,
277 job->offset,
278 job->speed, &error_abort);
279 }
280
281 BlockErrorAction block_job_error_action(BlockJob *job, BlockDriverState *bs,
282 BlockdevOnError on_err,
283 int is_read, int error)
284 {
285 BlockErrorAction action;
286
287 switch (on_err) {
288 case BLOCKDEV_ON_ERROR_ENOSPC:
289 action = (error == ENOSPC) ?
290 BLOCK_ERROR_ACTION_STOP : BLOCK_ERROR_ACTION_REPORT;
291 break;
292 case BLOCKDEV_ON_ERROR_STOP:
293 action = BLOCK_ERROR_ACTION_STOP;
294 break;
295 case BLOCKDEV_ON_ERROR_REPORT:
296 action = BLOCK_ERROR_ACTION_REPORT;
297 break;
298 case BLOCKDEV_ON_ERROR_IGNORE:
299 action = BLOCK_ERROR_ACTION_IGNORE;
300 break;
301 default:
302 abort();
303 }
304 qapi_event_send_block_job_error(bdrv_get_device_name(job->bs),
305 is_read ? IO_OPERATION_TYPE_READ :
306 IO_OPERATION_TYPE_WRITE,
307 action, &error_abort);
308 if (action == BLOCK_ERROR_ACTION_STOP) {
309 block_job_pause(job);
310 block_job_iostatus_set_err(job, error);
311 if (bs != job->bs) {
312 bdrv_iostatus_set_err(bs, error);
313 }
314 }
315 return action;
316 }