Update version for v2.4.0-rc2 release
[qemu/ar7.git] / blockjob.c
blob62bb9066349345f05b764a1fd004f03dbaa84589
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 block_job_release(bs);
70 error_propagate(errp, local_err);
71 return NULL;
74 return job;
77 void block_job_release(BlockDriverState *bs)
79 BlockJob *job = bs->job;
81 bs->job = NULL;
82 bdrv_op_unblock_all(bs, job->blocker);
83 error_free(job->blocker);
84 g_free(job);
87 void block_job_completed(BlockJob *job, int ret)
89 BlockDriverState *bs = job->bs;
91 assert(bs->job == job);
92 job->cb(job->opaque, ret);
93 block_job_release(bs);
96 void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp)
98 Error *local_err = NULL;
100 if (!job->driver->set_speed) {
101 error_setg(errp, QERR_UNSUPPORTED);
102 return;
104 job->driver->set_speed(job, speed, &local_err);
105 if (local_err) {
106 error_propagate(errp, local_err);
107 return;
110 job->speed = speed;
113 void block_job_complete(BlockJob *job, Error **errp)
115 if (job->pause_count || job->cancelled || !job->driver->complete) {
116 error_setg(errp, QERR_BLOCK_JOB_NOT_READY,
117 bdrv_get_device_name(job->bs));
118 return;
121 job->driver->complete(job, errp);
124 void block_job_pause(BlockJob *job)
126 job->pause_count++;
129 bool block_job_is_paused(BlockJob *job)
131 return job->pause_count > 0;
134 void block_job_resume(BlockJob *job)
136 assert(job->pause_count > 0);
137 job->pause_count--;
138 if (job->pause_count) {
139 return;
141 block_job_enter(job);
144 void block_job_enter(BlockJob *job)
146 block_job_iostatus_reset(job);
147 if (job->co && !job->busy) {
148 qemu_coroutine_enter(job->co, NULL);
152 void block_job_cancel(BlockJob *job)
154 job->cancelled = true;
155 block_job_enter(job);
158 bool block_job_is_cancelled(BlockJob *job)
160 return job->cancelled;
163 void block_job_iostatus_reset(BlockJob *job)
165 job->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
166 if (job->driver->iostatus_reset) {
167 job->driver->iostatus_reset(job);
171 struct BlockFinishData {
172 BlockJob *job;
173 BlockCompletionFunc *cb;
174 void *opaque;
175 bool cancelled;
176 int ret;
179 static void block_job_finish_cb(void *opaque, int ret)
181 struct BlockFinishData *data = opaque;
183 data->cancelled = block_job_is_cancelled(data->job);
184 data->ret = ret;
185 data->cb(data->opaque, ret);
188 static int block_job_finish_sync(BlockJob *job,
189 void (*finish)(BlockJob *, Error **errp),
190 Error **errp)
192 struct BlockFinishData data;
193 BlockDriverState *bs = job->bs;
194 Error *local_err = NULL;
196 assert(bs->job == job);
198 /* Set up our own callback to store the result and chain to
199 * the original callback.
201 data.job = job;
202 data.cb = job->cb;
203 data.opaque = job->opaque;
204 data.ret = -EINPROGRESS;
205 job->cb = block_job_finish_cb;
206 job->opaque = &data;
207 finish(job, &local_err);
208 if (local_err) {
209 error_propagate(errp, local_err);
210 return -EBUSY;
212 while (data.ret == -EINPROGRESS) {
213 aio_poll(bdrv_get_aio_context(bs), true);
215 return (data.cancelled && data.ret == 0) ? -ECANCELED : data.ret;
218 /* A wrapper around block_job_cancel() taking an Error ** parameter so it may be
219 * used with block_job_finish_sync() without the need for (rather nasty)
220 * function pointer casts there. */
221 static void block_job_cancel_err(BlockJob *job, Error **errp)
223 block_job_cancel(job);
226 int block_job_cancel_sync(BlockJob *job)
228 return block_job_finish_sync(job, &block_job_cancel_err, NULL);
231 int block_job_complete_sync(BlockJob *job, Error **errp)
233 return block_job_finish_sync(job, &block_job_complete, errp);
236 void block_job_sleep_ns(BlockJob *job, QEMUClockType type, int64_t ns)
238 assert(job->busy);
240 /* Check cancellation *before* setting busy = false, too! */
241 if (block_job_is_cancelled(job)) {
242 return;
245 job->busy = false;
246 if (block_job_is_paused(job)) {
247 qemu_coroutine_yield();
248 } else {
249 co_aio_sleep_ns(bdrv_get_aio_context(job->bs), type, ns);
251 job->busy = true;
254 void block_job_yield(BlockJob *job)
256 assert(job->busy);
258 /* Check cancellation *before* setting busy = false, too! */
259 if (block_job_is_cancelled(job)) {
260 return;
263 job->busy = false;
264 qemu_coroutine_yield();
265 job->busy = true;
268 BlockJobInfo *block_job_query(BlockJob *job)
270 BlockJobInfo *info = g_new0(BlockJobInfo, 1);
271 info->type = g_strdup(BlockJobType_lookup[job->driver->job_type]);
272 info->device = g_strdup(bdrv_get_device_name(job->bs));
273 info->len = job->len;
274 info->busy = job->busy;
275 info->paused = job->pause_count > 0;
276 info->offset = job->offset;
277 info->speed = job->speed;
278 info->io_status = job->iostatus;
279 info->ready = job->ready;
280 return info;
283 static void block_job_iostatus_set_err(BlockJob *job, int error)
285 if (job->iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
286 job->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE :
287 BLOCK_DEVICE_IO_STATUS_FAILED;
291 void block_job_event_cancelled(BlockJob *job)
293 qapi_event_send_block_job_cancelled(job->driver->job_type,
294 bdrv_get_device_name(job->bs),
295 job->len,
296 job->offset,
297 job->speed,
298 &error_abort);
301 void block_job_event_completed(BlockJob *job, const char *msg)
303 qapi_event_send_block_job_completed(job->driver->job_type,
304 bdrv_get_device_name(job->bs),
305 job->len,
306 job->offset,
307 job->speed,
308 !!msg,
309 msg,
310 &error_abort);
313 void block_job_event_ready(BlockJob *job)
315 job->ready = true;
317 qapi_event_send_block_job_ready(job->driver->job_type,
318 bdrv_get_device_name(job->bs),
319 job->len,
320 job->offset,
321 job->speed, &error_abort);
324 BlockErrorAction block_job_error_action(BlockJob *job, BlockDriverState *bs,
325 BlockdevOnError on_err,
326 int is_read, int error)
328 BlockErrorAction action;
330 switch (on_err) {
331 case BLOCKDEV_ON_ERROR_ENOSPC:
332 action = (error == ENOSPC) ?
333 BLOCK_ERROR_ACTION_STOP : BLOCK_ERROR_ACTION_REPORT;
334 break;
335 case BLOCKDEV_ON_ERROR_STOP:
336 action = BLOCK_ERROR_ACTION_STOP;
337 break;
338 case BLOCKDEV_ON_ERROR_REPORT:
339 action = BLOCK_ERROR_ACTION_REPORT;
340 break;
341 case BLOCKDEV_ON_ERROR_IGNORE:
342 action = BLOCK_ERROR_ACTION_IGNORE;
343 break;
344 default:
345 abort();
347 qapi_event_send_block_job_error(bdrv_get_device_name(job->bs),
348 is_read ? IO_OPERATION_TYPE_READ :
349 IO_OPERATION_TYPE_WRITE,
350 action, &error_abort);
351 if (action == BLOCK_ERROR_ACTION_STOP) {
352 /* make the pause user visible, which will be resumed from QMP. */
353 job->user_paused = true;
354 block_job_pause(job);
355 block_job_iostatus_set_err(job, error);
356 if (bs != job->bs) {
357 bdrv_iostatus_set_err(bs, error);
360 return action;
363 typedef struct {
364 BlockJob *job;
365 QEMUBH *bh;
366 AioContext *aio_context;
367 BlockJobDeferToMainLoopFn *fn;
368 void *opaque;
369 } BlockJobDeferToMainLoopData;
371 static void block_job_defer_to_main_loop_bh(void *opaque)
373 BlockJobDeferToMainLoopData *data = opaque;
374 AioContext *aio_context;
376 qemu_bh_delete(data->bh);
378 /* Prevent race with block_job_defer_to_main_loop() */
379 aio_context_acquire(data->aio_context);
381 /* Fetch BDS AioContext again, in case it has changed */
382 aio_context = bdrv_get_aio_context(data->job->bs);
383 aio_context_acquire(aio_context);
385 data->fn(data->job, data->opaque);
387 aio_context_release(aio_context);
389 aio_context_release(data->aio_context);
391 g_free(data);
394 void block_job_defer_to_main_loop(BlockJob *job,
395 BlockJobDeferToMainLoopFn *fn,
396 void *opaque)
398 BlockJobDeferToMainLoopData *data = g_malloc(sizeof(*data));
399 data->job = job;
400 data->bh = qemu_bh_new(block_job_defer_to_main_loop_bh, data);
401 data->aio_context = bdrv_get_aio_context(job->bs);
402 data->fn = fn;
403 data->opaque = opaque;
405 qemu_bh_schedule(data->bh);