Merge remote-tracking branch 'remotes/pmaydell/tags/pull-target-arm-20150706' into...
[qemu/ar7.git] / blockjob.c
blobec46fad2f11d73cd7f1cdbb0cbab4a26ea232113
1 /*
2 * QEMU System Emulator block driver
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 #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/qerror.h"
33 #include "qapi/qmp/qjson.h"
34 #include "block/coroutine.h"
35 #include "qmp-commands.h"
36 #include "qemu/timer.h"
37 #include "qapi-event.h"
39 void *block_job_create(const BlockJobDriver *driver, BlockDriverState *bs,
40 int64_t speed, BlockCompletionFunc *cb,
41 void *opaque, Error **errp)
43 BlockJob *job;
45 if (bs->job) {
46 error_setg(errp, QERR_DEVICE_IN_USE, bdrv_get_device_name(bs));
47 return NULL;
49 bdrv_ref(bs);
50 job = g_malloc0(driver->instance_size);
51 error_setg(&job->blocker, "block device is in use by block job: %s",
52 BlockJobType_lookup[driver->job_type]);
53 bdrv_op_block_all(bs, job->blocker);
54 bdrv_op_unblock(bs, BLOCK_OP_TYPE_DATAPLANE, job->blocker);
56 job->driver = driver;
57 job->bs = bs;
58 job->cb = cb;
59 job->opaque = opaque;
60 job->busy = true;
61 bs->job = job;
63 /* Only set speed when necessary to avoid NotSupported error */
64 if (speed != 0) {
65 Error *local_err = NULL;
67 block_job_set_speed(job, speed, &local_err);
68 if (local_err) {
69 bs->job = NULL;
70 bdrv_op_unblock_all(bs, job->blocker);
71 error_free(job->blocker);
72 g_free(job);
73 error_propagate(errp, local_err);
74 return NULL;
77 return job;
80 void block_job_completed(BlockJob *job, int ret)
82 BlockDriverState *bs = job->bs;
84 assert(bs->job == job);
85 job->cb(job->opaque, ret);
86 bs->job = NULL;
87 bdrv_op_unblock_all(bs, job->blocker);
88 error_free(job->blocker);
89 g_free(job);
92 void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp)
94 Error *local_err = NULL;
96 if (!job->driver->set_speed) {
97 error_setg(errp, QERR_UNSUPPORTED);
98 return;
100 job->driver->set_speed(job, speed, &local_err);
101 if (local_err) {
102 error_propagate(errp, local_err);
103 return;
106 job->speed = speed;
109 void block_job_complete(BlockJob *job, Error **errp)
111 if (job->pause_count || job->cancelled || !job->driver->complete) {
112 error_setg(errp, QERR_BLOCK_JOB_NOT_READY,
113 bdrv_get_device_name(job->bs));
114 return;
117 job->driver->complete(job, errp);
120 void block_job_pause(BlockJob *job)
122 job->pause_count++;
125 bool block_job_is_paused(BlockJob *job)
127 return job->pause_count > 0;
130 void block_job_resume(BlockJob *job)
132 assert(job->pause_count > 0);
133 job->pause_count--;
134 if (job->pause_count) {
135 return;
137 block_job_enter(job);
140 void block_job_enter(BlockJob *job)
142 block_job_iostatus_reset(job);
143 if (job->co && !job->busy) {
144 qemu_coroutine_enter(job->co, NULL);
148 void block_job_cancel(BlockJob *job)
150 job->cancelled = true;
151 block_job_enter(job);
154 bool block_job_is_cancelled(BlockJob *job)
156 return job->cancelled;
159 void block_job_iostatus_reset(BlockJob *job)
161 job->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
162 if (job->driver->iostatus_reset) {
163 job->driver->iostatus_reset(job);
167 struct BlockFinishData {
168 BlockJob *job;
169 BlockCompletionFunc *cb;
170 void *opaque;
171 bool cancelled;
172 int ret;
175 static void block_job_finish_cb(void *opaque, int ret)
177 struct BlockFinishData *data = opaque;
179 data->cancelled = block_job_is_cancelled(data->job);
180 data->ret = ret;
181 data->cb(data->opaque, ret);
184 static int block_job_finish_sync(BlockJob *job,
185 void (*finish)(BlockJob *, Error **errp),
186 Error **errp)
188 struct BlockFinishData data;
189 BlockDriverState *bs = job->bs;
190 Error *local_err = NULL;
192 assert(bs->job == job);
194 /* Set up our own callback to store the result and chain to
195 * the original callback.
197 data.job = job;
198 data.cb = job->cb;
199 data.opaque = job->opaque;
200 data.ret = -EINPROGRESS;
201 job->cb = block_job_finish_cb;
202 job->opaque = &data;
203 finish(job, &local_err);
204 if (local_err) {
205 error_propagate(errp, local_err);
206 return -EBUSY;
208 while (data.ret == -EINPROGRESS) {
209 aio_poll(bdrv_get_aio_context(bs), true);
211 return (data.cancelled && data.ret == 0) ? -ECANCELED : data.ret;
214 /* A wrapper around block_job_cancel() taking an Error ** parameter so it may be
215 * used with block_job_finish_sync() without the need for (rather nasty)
216 * function pointer casts there. */
217 static void block_job_cancel_err(BlockJob *job, Error **errp)
219 block_job_cancel(job);
222 int block_job_cancel_sync(BlockJob *job)
224 return block_job_finish_sync(job, &block_job_cancel_err, NULL);
227 int block_job_complete_sync(BlockJob *job, Error **errp)
229 return block_job_finish_sync(job, &block_job_complete, errp);
232 void block_job_sleep_ns(BlockJob *job, QEMUClockType type, int64_t ns)
234 assert(job->busy);
236 /* Check cancellation *before* setting busy = false, too! */
237 if (block_job_is_cancelled(job)) {
238 return;
241 job->busy = false;
242 if (block_job_is_paused(job)) {
243 qemu_coroutine_yield();
244 } else {
245 co_aio_sleep_ns(bdrv_get_aio_context(job->bs), type, ns);
247 job->busy = true;
250 void block_job_yield(BlockJob *job)
252 assert(job->busy);
254 /* Check cancellation *before* setting busy = false, too! */
255 if (block_job_is_cancelled(job)) {
256 return;
259 job->busy = false;
260 qemu_coroutine_yield();
261 job->busy = true;
264 BlockJobInfo *block_job_query(BlockJob *job)
266 BlockJobInfo *info = g_new0(BlockJobInfo, 1);
267 info->type = g_strdup(BlockJobType_lookup[job->driver->job_type]);
268 info->device = g_strdup(bdrv_get_device_name(job->bs));
269 info->len = job->len;
270 info->busy = job->busy;
271 info->paused = job->pause_count > 0;
272 info->offset = job->offset;
273 info->speed = job->speed;
274 info->io_status = job->iostatus;
275 info->ready = job->ready;
276 return info;
279 static void block_job_iostatus_set_err(BlockJob *job, int error)
281 if (job->iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
282 job->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE :
283 BLOCK_DEVICE_IO_STATUS_FAILED;
287 void block_job_event_cancelled(BlockJob *job)
289 qapi_event_send_block_job_cancelled(job->driver->job_type,
290 bdrv_get_device_name(job->bs),
291 job->len,
292 job->offset,
293 job->speed,
294 &error_abort);
297 void block_job_event_completed(BlockJob *job, const char *msg)
299 qapi_event_send_block_job_completed(job->driver->job_type,
300 bdrv_get_device_name(job->bs),
301 job->len,
302 job->offset,
303 job->speed,
304 !!msg,
305 msg,
306 &error_abort);
309 void block_job_event_ready(BlockJob *job)
311 job->ready = true;
313 qapi_event_send_block_job_ready(job->driver->job_type,
314 bdrv_get_device_name(job->bs),
315 job->len,
316 job->offset,
317 job->speed, &error_abort);
320 BlockErrorAction block_job_error_action(BlockJob *job, BlockDriverState *bs,
321 BlockdevOnError on_err,
322 int is_read, int error)
324 BlockErrorAction action;
326 switch (on_err) {
327 case BLOCKDEV_ON_ERROR_ENOSPC:
328 action = (error == ENOSPC) ?
329 BLOCK_ERROR_ACTION_STOP : BLOCK_ERROR_ACTION_REPORT;
330 break;
331 case BLOCKDEV_ON_ERROR_STOP:
332 action = BLOCK_ERROR_ACTION_STOP;
333 break;
334 case BLOCKDEV_ON_ERROR_REPORT:
335 action = BLOCK_ERROR_ACTION_REPORT;
336 break;
337 case BLOCKDEV_ON_ERROR_IGNORE:
338 action = BLOCK_ERROR_ACTION_IGNORE;
339 break;
340 default:
341 abort();
343 qapi_event_send_block_job_error(bdrv_get_device_name(job->bs),
344 is_read ? IO_OPERATION_TYPE_READ :
345 IO_OPERATION_TYPE_WRITE,
346 action, &error_abort);
347 if (action == BLOCK_ERROR_ACTION_STOP) {
348 /* make the pause user visible, which will be resumed from QMP. */
349 job->user_paused = true;
350 block_job_pause(job);
351 block_job_iostatus_set_err(job, error);
352 if (bs != job->bs) {
353 bdrv_iostatus_set_err(bs, error);
356 return action;
359 typedef struct {
360 BlockJob *job;
361 QEMUBH *bh;
362 AioContext *aio_context;
363 BlockJobDeferToMainLoopFn *fn;
364 void *opaque;
365 } BlockJobDeferToMainLoopData;
367 static void block_job_defer_to_main_loop_bh(void *opaque)
369 BlockJobDeferToMainLoopData *data = opaque;
370 AioContext *aio_context;
372 qemu_bh_delete(data->bh);
374 /* Prevent race with block_job_defer_to_main_loop() */
375 aio_context_acquire(data->aio_context);
377 /* Fetch BDS AioContext again, in case it has changed */
378 aio_context = bdrv_get_aio_context(data->job->bs);
379 aio_context_acquire(aio_context);
381 data->fn(data->job, data->opaque);
383 aio_context_release(aio_context);
385 aio_context_release(data->aio_context);
387 g_free(data);
390 void block_job_defer_to_main_loop(BlockJob *job,
391 BlockJobDeferToMainLoopFn *fn,
392 void *opaque)
394 BlockJobDeferToMainLoopData *data = g_malloc(sizeof(*data));
395 data->job = job;
396 data->bh = qemu_bh_new(block_job_defer_to_main_loop_bh, data);
397 data->aio_context = bdrv_get_aio_context(job->bs);
398 data->fn = fn;
399 data->opaque = opaque;
401 qemu_bh_schedule(data->bh);