maint: Ignore ivshmem binaries
[qemu.git] / blockjob.c
blobc02fe598b8a9051827898726f992084030167bf0
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 bs->job = job;
65 /* Only set speed when necessary to avoid NotSupported error */
66 if (speed != 0) {
67 Error *local_err = NULL;
69 block_job_set_speed(job, speed, &local_err);
70 if (local_err) {
71 block_job_release(bs);
72 error_propagate(errp, local_err);
73 return NULL;
76 return job;
79 void block_job_release(BlockDriverState *bs)
81 BlockJob *job = bs->job;
83 bs->job = NULL;
84 bdrv_op_unblock_all(bs, job->blocker);
85 error_free(job->blocker);
86 g_free(job->id);
87 g_free(job);
90 void block_job_completed(BlockJob *job, int ret)
92 BlockDriverState *bs = job->bs;
94 assert(bs->job == job);
95 job->cb(job->opaque, ret);
96 block_job_release(bs);
99 void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp)
101 Error *local_err = NULL;
103 if (!job->driver->set_speed) {
104 error_setg(errp, QERR_UNSUPPORTED);
105 return;
107 job->driver->set_speed(job, speed, &local_err);
108 if (local_err) {
109 error_propagate(errp, local_err);
110 return;
113 job->speed = speed;
116 void block_job_complete(BlockJob *job, Error **errp)
118 if (job->pause_count || job->cancelled || !job->driver->complete) {
119 error_setg(errp, QERR_BLOCK_JOB_NOT_READY, job->id);
120 return;
123 job->driver->complete(job, errp);
126 void block_job_pause(BlockJob *job)
128 job->pause_count++;
131 bool block_job_is_paused(BlockJob *job)
133 return job->pause_count > 0;
136 void block_job_resume(BlockJob *job)
138 assert(job->pause_count > 0);
139 job->pause_count--;
140 if (job->pause_count) {
141 return;
143 block_job_enter(job);
146 void block_job_enter(BlockJob *job)
148 block_job_iostatus_reset(job);
149 if (job->co && !job->busy) {
150 qemu_coroutine_enter(job->co, NULL);
154 void block_job_cancel(BlockJob *job)
156 job->cancelled = true;
157 block_job_enter(job);
160 bool block_job_is_cancelled(BlockJob *job)
162 return job->cancelled;
165 void block_job_iostatus_reset(BlockJob *job)
167 job->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
168 if (job->driver->iostatus_reset) {
169 job->driver->iostatus_reset(job);
173 struct BlockFinishData {
174 BlockJob *job;
175 BlockCompletionFunc *cb;
176 void *opaque;
177 bool cancelled;
178 int ret;
181 static void block_job_finish_cb(void *opaque, int ret)
183 struct BlockFinishData *data = opaque;
185 data->cancelled = block_job_is_cancelled(data->job);
186 data->ret = ret;
187 data->cb(data->opaque, ret);
190 static int block_job_finish_sync(BlockJob *job,
191 void (*finish)(BlockJob *, Error **errp),
192 Error **errp)
194 struct BlockFinishData data;
195 BlockDriverState *bs = job->bs;
196 Error *local_err = NULL;
198 assert(bs->job == job);
200 /* Set up our own callback to store the result and chain to
201 * the original callback.
203 data.job = job;
204 data.cb = job->cb;
205 data.opaque = job->opaque;
206 data.ret = -EINPROGRESS;
207 job->cb = block_job_finish_cb;
208 job->opaque = &data;
209 finish(job, &local_err);
210 if (local_err) {
211 error_propagate(errp, local_err);
212 return -EBUSY;
214 while (data.ret == -EINPROGRESS) {
215 aio_poll(bdrv_get_aio_context(bs), true);
217 return (data.cancelled && data.ret == 0) ? -ECANCELED : data.ret;
220 /* A wrapper around block_job_cancel() taking an Error ** parameter so it may be
221 * used with block_job_finish_sync() without the need for (rather nasty)
222 * function pointer casts there. */
223 static void block_job_cancel_err(BlockJob *job, Error **errp)
225 block_job_cancel(job);
228 int block_job_cancel_sync(BlockJob *job)
230 return block_job_finish_sync(job, &block_job_cancel_err, NULL);
233 int block_job_complete_sync(BlockJob *job, Error **errp)
235 return block_job_finish_sync(job, &block_job_complete, errp);
238 void block_job_sleep_ns(BlockJob *job, QEMUClockType type, int64_t ns)
240 assert(job->busy);
242 /* Check cancellation *before* setting busy = false, too! */
243 if (block_job_is_cancelled(job)) {
244 return;
247 job->busy = false;
248 if (block_job_is_paused(job)) {
249 qemu_coroutine_yield();
250 } else {
251 co_aio_sleep_ns(bdrv_get_aio_context(job->bs), type, ns);
253 job->busy = true;
256 void block_job_yield(BlockJob *job)
258 assert(job->busy);
260 /* Check cancellation *before* setting busy = false, too! */
261 if (block_job_is_cancelled(job)) {
262 return;
265 job->busy = false;
266 qemu_coroutine_yield();
267 job->busy = true;
270 BlockJobInfo *block_job_query(BlockJob *job)
272 BlockJobInfo *info = g_new0(BlockJobInfo, 1);
273 info->type = g_strdup(BlockJobType_lookup[job->driver->job_type]);
274 info->device = g_strdup(job->id);
275 info->len = job->len;
276 info->busy = job->busy;
277 info->paused = job->pause_count > 0;
278 info->offset = job->offset;
279 info->speed = job->speed;
280 info->io_status = job->iostatus;
281 info->ready = job->ready;
282 return info;
285 static void block_job_iostatus_set_err(BlockJob *job, int error)
287 if (job->iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
288 job->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE :
289 BLOCK_DEVICE_IO_STATUS_FAILED;
293 void block_job_event_cancelled(BlockJob *job)
295 qapi_event_send_block_job_cancelled(job->driver->job_type,
296 job->id,
297 job->len,
298 job->offset,
299 job->speed,
300 &error_abort);
303 void block_job_event_completed(BlockJob *job, const char *msg)
305 qapi_event_send_block_job_completed(job->driver->job_type,
306 job->id,
307 job->len,
308 job->offset,
309 job->speed,
310 !!msg,
311 msg,
312 &error_abort);
315 void block_job_event_ready(BlockJob *job)
317 job->ready = true;
319 qapi_event_send_block_job_ready(job->driver->job_type,
320 job->id,
321 job->len,
322 job->offset,
323 job->speed, &error_abort);
326 BlockErrorAction block_job_error_action(BlockJob *job, BlockDriverState *bs,
327 BlockdevOnError on_err,
328 int is_read, int error)
330 BlockErrorAction action;
332 switch (on_err) {
333 case BLOCKDEV_ON_ERROR_ENOSPC:
334 action = (error == ENOSPC) ?
335 BLOCK_ERROR_ACTION_STOP : BLOCK_ERROR_ACTION_REPORT;
336 break;
337 case BLOCKDEV_ON_ERROR_STOP:
338 action = BLOCK_ERROR_ACTION_STOP;
339 break;
340 case BLOCKDEV_ON_ERROR_REPORT:
341 action = BLOCK_ERROR_ACTION_REPORT;
342 break;
343 case BLOCKDEV_ON_ERROR_IGNORE:
344 action = BLOCK_ERROR_ACTION_IGNORE;
345 break;
346 default:
347 abort();
349 qapi_event_send_block_job_error(job->id,
350 is_read ? IO_OPERATION_TYPE_READ :
351 IO_OPERATION_TYPE_WRITE,
352 action, &error_abort);
353 if (action == BLOCK_ERROR_ACTION_STOP) {
354 /* make the pause user visible, which will be resumed from QMP. */
355 job->user_paused = true;
356 block_job_pause(job);
357 block_job_iostatus_set_err(job, error);
358 if (bs->blk && bs != job->bs) {
359 blk_iostatus_set_err(bs->blk, error);
362 return action;
365 typedef struct {
366 BlockJob *job;
367 QEMUBH *bh;
368 AioContext *aio_context;
369 BlockJobDeferToMainLoopFn *fn;
370 void *opaque;
371 } BlockJobDeferToMainLoopData;
373 static void block_job_defer_to_main_loop_bh(void *opaque)
375 BlockJobDeferToMainLoopData *data = opaque;
376 AioContext *aio_context;
378 qemu_bh_delete(data->bh);
380 /* Prevent race with block_job_defer_to_main_loop() */
381 aio_context_acquire(data->aio_context);
383 /* Fetch BDS AioContext again, in case it has changed */
384 aio_context = bdrv_get_aio_context(data->job->bs);
385 aio_context_acquire(aio_context);
387 data->fn(data->job, data->opaque);
389 aio_context_release(aio_context);
391 aio_context_release(data->aio_context);
393 g_free(data);
396 void block_job_defer_to_main_loop(BlockJob *job,
397 BlockJobDeferToMainLoopFn *fn,
398 void *opaque)
400 BlockJobDeferToMainLoopData *data = g_malloc(sizeof(*data));
401 data->job = job;
402 data->bh = qemu_bh_new(block_job_defer_to_main_loop_bh, data);
403 data->aio_context = bdrv_get_aio_context(job->bs);
404 data->fn = fn;
405 data->opaque = opaque;
407 qemu_bh_schedule(data->bh);