blockjob: Introduce reference count and fix reference to job->bs
[qemu/ar7.git] / blockjob.c
blobae9c5b2925b7e03d6fdcc0613104c94e848e34f1
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 "sysemu/block-backend.h"
33 #include "qapi/qmp/qerror.h"
34 #include "qapi/qmp/qjson.h"
35 #include "qemu/coroutine.h"
36 #include "qmp-commands.h"
37 #include "qemu/timer.h"
38 #include "qapi-event.h"
40 void *block_job_create(const BlockJobDriver *driver, BlockDriverState *bs,
41 int64_t speed, BlockCompletionFunc *cb,
42 void *opaque, Error **errp)
44 BlockJob *job;
46 if (bs->job) {
47 error_setg(errp, QERR_DEVICE_IN_USE, bdrv_get_device_name(bs));
48 return NULL;
50 bdrv_ref(bs);
51 job = g_malloc0(driver->instance_size);
52 error_setg(&job->blocker, "block device is in use by block job: %s",
53 BlockJobType_lookup[driver->job_type]);
54 bdrv_op_block_all(bs, job->blocker);
55 bdrv_op_unblock(bs, BLOCK_OP_TYPE_DATAPLANE, job->blocker);
57 job->driver = driver;
58 job->id = g_strdup(bdrv_get_device_name(bs));
59 job->bs = bs;
60 job->cb = cb;
61 job->opaque = opaque;
62 job->busy = true;
63 job->refcnt = 1;
64 bs->job = job;
66 /* Only set speed when necessary to avoid NotSupported error */
67 if (speed != 0) {
68 Error *local_err = NULL;
70 block_job_set_speed(job, speed, &local_err);
71 if (local_err) {
72 block_job_unref(job);
73 error_propagate(errp, local_err);
74 return NULL;
77 return job;
80 void block_job_ref(BlockJob *job)
82 ++job->refcnt;
85 void block_job_unref(BlockJob *job)
87 if (--job->refcnt == 0) {
88 job->bs->job = NULL;
89 bdrv_op_unblock_all(job->bs, job->blocker);
90 bdrv_unref(job->bs);
91 error_free(job->blocker);
92 g_free(job->id);
93 g_free(job);
97 void block_job_completed(BlockJob *job, int ret)
99 BlockDriverState *bs = job->bs;
101 assert(bs->job == job);
102 job->cb(job->opaque, ret);
103 block_job_unref(job);
106 void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp)
108 Error *local_err = NULL;
110 if (!job->driver->set_speed) {
111 error_setg(errp, QERR_UNSUPPORTED);
112 return;
114 job->driver->set_speed(job, speed, &local_err);
115 if (local_err) {
116 error_propagate(errp, local_err);
117 return;
120 job->speed = speed;
123 void block_job_complete(BlockJob *job, Error **errp)
125 if (job->pause_count || job->cancelled || !job->driver->complete) {
126 error_setg(errp, QERR_BLOCK_JOB_NOT_READY, job->id);
127 return;
130 job->driver->complete(job, errp);
133 void block_job_pause(BlockJob *job)
135 job->pause_count++;
138 bool block_job_is_paused(BlockJob *job)
140 return job->pause_count > 0;
143 void block_job_resume(BlockJob *job)
145 assert(job->pause_count > 0);
146 job->pause_count--;
147 if (job->pause_count) {
148 return;
150 block_job_enter(job);
153 void block_job_enter(BlockJob *job)
155 block_job_iostatus_reset(job);
156 if (job->co && !job->busy) {
157 qemu_coroutine_enter(job->co, NULL);
161 void block_job_cancel(BlockJob *job)
163 job->cancelled = true;
164 block_job_enter(job);
167 bool block_job_is_cancelled(BlockJob *job)
169 return job->cancelled;
172 void block_job_iostatus_reset(BlockJob *job)
174 job->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
175 if (job->driver->iostatus_reset) {
176 job->driver->iostatus_reset(job);
180 struct BlockFinishData {
181 BlockJob *job;
182 BlockCompletionFunc *cb;
183 void *opaque;
184 bool cancelled;
185 int ret;
188 static void block_job_finish_cb(void *opaque, int ret)
190 struct BlockFinishData *data = opaque;
192 data->cancelled = block_job_is_cancelled(data->job);
193 data->ret = ret;
194 data->cb(data->opaque, ret);
197 static int block_job_finish_sync(BlockJob *job,
198 void (*finish)(BlockJob *, Error **errp),
199 Error **errp)
201 struct BlockFinishData data;
202 BlockDriverState *bs = job->bs;
203 Error *local_err = NULL;
205 assert(bs->job == job);
207 /* Set up our own callback to store the result and chain to
208 * the original callback.
210 data.job = job;
211 data.cb = job->cb;
212 data.opaque = job->opaque;
213 data.ret = -EINPROGRESS;
214 job->cb = block_job_finish_cb;
215 job->opaque = &data;
216 finish(job, &local_err);
217 if (local_err) {
218 error_propagate(errp, local_err);
219 return -EBUSY;
221 while (data.ret == -EINPROGRESS) {
222 aio_poll(bdrv_get_aio_context(bs), true);
224 return (data.cancelled && data.ret == 0) ? -ECANCELED : data.ret;
227 /* A wrapper around block_job_cancel() taking an Error ** parameter so it may be
228 * used with block_job_finish_sync() without the need for (rather nasty)
229 * function pointer casts there. */
230 static void block_job_cancel_err(BlockJob *job, Error **errp)
232 block_job_cancel(job);
235 int block_job_cancel_sync(BlockJob *job)
237 return block_job_finish_sync(job, &block_job_cancel_err, NULL);
240 int block_job_complete_sync(BlockJob *job, Error **errp)
242 return block_job_finish_sync(job, &block_job_complete, errp);
245 void block_job_sleep_ns(BlockJob *job, QEMUClockType type, int64_t ns)
247 assert(job->busy);
249 /* Check cancellation *before* setting busy = false, too! */
250 if (block_job_is_cancelled(job)) {
251 return;
254 job->busy = false;
255 if (block_job_is_paused(job)) {
256 qemu_coroutine_yield();
257 } else {
258 co_aio_sleep_ns(bdrv_get_aio_context(job->bs), type, ns);
260 job->busy = true;
263 void block_job_yield(BlockJob *job)
265 assert(job->busy);
267 /* Check cancellation *before* setting busy = false, too! */
268 if (block_job_is_cancelled(job)) {
269 return;
272 job->busy = false;
273 qemu_coroutine_yield();
274 job->busy = true;
277 BlockJobInfo *block_job_query(BlockJob *job)
279 BlockJobInfo *info = g_new0(BlockJobInfo, 1);
280 info->type = g_strdup(BlockJobType_lookup[job->driver->job_type]);
281 info->device = g_strdup(job->id);
282 info->len = job->len;
283 info->busy = job->busy;
284 info->paused = job->pause_count > 0;
285 info->offset = job->offset;
286 info->speed = job->speed;
287 info->io_status = job->iostatus;
288 info->ready = job->ready;
289 return info;
292 static void block_job_iostatus_set_err(BlockJob *job, int error)
294 if (job->iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
295 job->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE :
296 BLOCK_DEVICE_IO_STATUS_FAILED;
300 void block_job_event_cancelled(BlockJob *job)
302 qapi_event_send_block_job_cancelled(job->driver->job_type,
303 job->id,
304 job->len,
305 job->offset,
306 job->speed,
307 &error_abort);
310 void block_job_event_completed(BlockJob *job, const char *msg)
312 qapi_event_send_block_job_completed(job->driver->job_type,
313 job->id,
314 job->len,
315 job->offset,
316 job->speed,
317 !!msg,
318 msg,
319 &error_abort);
322 void block_job_event_ready(BlockJob *job)
324 job->ready = true;
326 qapi_event_send_block_job_ready(job->driver->job_type,
327 job->id,
328 job->len,
329 job->offset,
330 job->speed, &error_abort);
333 BlockErrorAction block_job_error_action(BlockJob *job, BlockDriverState *bs,
334 BlockdevOnError on_err,
335 int is_read, int error)
337 BlockErrorAction action;
339 switch (on_err) {
340 case BLOCKDEV_ON_ERROR_ENOSPC:
341 action = (error == ENOSPC) ?
342 BLOCK_ERROR_ACTION_STOP : BLOCK_ERROR_ACTION_REPORT;
343 break;
344 case BLOCKDEV_ON_ERROR_STOP:
345 action = BLOCK_ERROR_ACTION_STOP;
346 break;
347 case BLOCKDEV_ON_ERROR_REPORT:
348 action = BLOCK_ERROR_ACTION_REPORT;
349 break;
350 case BLOCKDEV_ON_ERROR_IGNORE:
351 action = BLOCK_ERROR_ACTION_IGNORE;
352 break;
353 default:
354 abort();
356 qapi_event_send_block_job_error(job->id,
357 is_read ? IO_OPERATION_TYPE_READ :
358 IO_OPERATION_TYPE_WRITE,
359 action, &error_abort);
360 if (action == BLOCK_ERROR_ACTION_STOP) {
361 /* make the pause user visible, which will be resumed from QMP. */
362 job->user_paused = true;
363 block_job_pause(job);
364 block_job_iostatus_set_err(job, error);
365 if (bs->blk && bs != job->bs) {
366 blk_iostatus_set_err(bs->blk, error);
369 return action;
372 typedef struct {
373 BlockJob *job;
374 QEMUBH *bh;
375 AioContext *aio_context;
376 BlockJobDeferToMainLoopFn *fn;
377 void *opaque;
378 } BlockJobDeferToMainLoopData;
380 static void block_job_defer_to_main_loop_bh(void *opaque)
382 BlockJobDeferToMainLoopData *data = opaque;
383 AioContext *aio_context;
385 qemu_bh_delete(data->bh);
387 /* Prevent race with block_job_defer_to_main_loop() */
388 aio_context_acquire(data->aio_context);
390 /* Fetch BDS AioContext again, in case it has changed */
391 aio_context = bdrv_get_aio_context(data->job->bs);
392 aio_context_acquire(aio_context);
394 data->fn(data->job, data->opaque);
396 aio_context_release(aio_context);
398 aio_context_release(data->aio_context);
400 g_free(data);
403 void block_job_defer_to_main_loop(BlockJob *job,
404 BlockJobDeferToMainLoopFn *fn,
405 void *opaque)
407 BlockJobDeferToMainLoopData *data = g_malloc(sizeof(*data));
408 data->job = job;
409 data->bh = qemu_bh_new(block_job_defer_to_main_loop_bh, data);
410 data->aio_context = bdrv_get_aio_context(job->bs);
411 data->fn = fn;
412 data->opaque = opaque;
414 qemu_bh_schedule(data->bh);