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
26 #include "config-host.h"
27 #include "qemu-common.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
)
47 error_setg(errp
, QERR_DEVICE_IN_USE
, bdrv_get_device_name(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
);
58 job
->id
= g_strdup(bdrv_get_device_name(bs
));
65 /* Only set speed when necessary to avoid NotSupported error */
67 Error
*local_err
= NULL
;
69 block_job_set_speed(job
, speed
, &local_err
);
71 block_job_release(bs
);
72 error_propagate(errp
, local_err
);
79 void block_job_release(BlockDriverState
*bs
)
81 BlockJob
*job
= bs
->job
;
84 bdrv_op_unblock_all(bs
, job
->blocker
);
85 error_free(job
->blocker
);
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
);
107 job
->driver
->set_speed(job
, speed
, &local_err
);
109 error_propagate(errp
, local_err
);
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
);
123 job
->driver
->complete(job
, errp
);
126 void block_job_pause(BlockJob
*job
)
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);
140 if (job
->pause_count
) {
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
{
175 BlockCompletionFunc
*cb
;
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
);
187 data
->cb(data
->opaque
, ret
);
190 static int block_job_finish_sync(BlockJob
*job
,
191 void (*finish
)(BlockJob
*, 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.
205 data
.opaque
= job
->opaque
;
206 data
.ret
= -EINPROGRESS
;
207 job
->cb
= block_job_finish_cb
;
209 finish(job
, &local_err
);
211 error_propagate(errp
, local_err
);
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
)
242 /* Check cancellation *before* setting busy = false, too! */
243 if (block_job_is_cancelled(job
)) {
248 if (block_job_is_paused(job
)) {
249 qemu_coroutine_yield();
251 co_aio_sleep_ns(bdrv_get_aio_context(job
->bs
), type
, ns
);
256 void block_job_yield(BlockJob
*job
)
260 /* Check cancellation *before* setting busy = false, too! */
261 if (block_job_is_cancelled(job
)) {
266 qemu_coroutine_yield();
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
;
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
,
303 void block_job_event_completed(BlockJob
*job
, const char *msg
)
305 qapi_event_send_block_job_completed(job
->driver
->job_type
,
315 void block_job_event_ready(BlockJob
*job
)
319 qapi_event_send_block_job_ready(job
->driver
->job_type
,
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
;
333 case BLOCKDEV_ON_ERROR_ENOSPC
:
334 action
= (error
== ENOSPC
) ?
335 BLOCK_ERROR_ACTION_STOP
: BLOCK_ERROR_ACTION_REPORT
;
337 case BLOCKDEV_ON_ERROR_STOP
:
338 action
= BLOCK_ERROR_ACTION_STOP
;
340 case BLOCKDEV_ON_ERROR_REPORT
:
341 action
= BLOCK_ERROR_ACTION_REPORT
;
343 case BLOCKDEV_ON_ERROR_IGNORE
:
344 action
= BLOCK_ERROR_ACTION_IGNORE
;
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
);
368 AioContext
*aio_context
;
369 BlockJobDeferToMainLoopFn
*fn
;
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
);
396 void block_job_defer_to_main_loop(BlockJob
*job
,
397 BlockJobDeferToMainLoopFn
*fn
,
400 BlockJobDeferToMainLoopData
*data
= g_malloc(sizeof(*data
));
402 data
->bh
= qemu_bh_new(block_job_defer_to_main_loop_bh
, data
);
403 data
->aio_context
= bdrv_get_aio_context(job
->bs
);
405 data
->opaque
= opaque
;
407 qemu_bh_schedule(data
->bh
);