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
));
66 /* Only set speed when necessary to avoid NotSupported error */
68 Error
*local_err
= NULL
;
70 block_job_set_speed(job
, speed
, &local_err
);
73 error_propagate(errp
, local_err
);
80 void block_job_ref(BlockJob
*job
)
85 void block_job_unref(BlockJob
*job
)
87 if (--job
->refcnt
== 0) {
89 bdrv_op_unblock_all(job
->bs
, job
->blocker
);
91 error_free(job
->blocker
);
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
);
114 job
->driver
->set_speed(job
, speed
, &local_err
);
116 error_propagate(errp
, local_err
);
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
);
130 job
->driver
->complete(job
, errp
);
133 void block_job_pause(BlockJob
*job
)
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);
147 if (job
->pause_count
) {
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
{
182 BlockCompletionFunc
*cb
;
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
);
194 data
->cb(data
->opaque
, ret
);
197 static int block_job_finish_sync(BlockJob
*job
,
198 void (*finish
)(BlockJob
*, 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.
212 data
.opaque
= job
->opaque
;
213 data
.ret
= -EINPROGRESS
;
214 job
->cb
= block_job_finish_cb
;
216 finish(job
, &local_err
);
218 error_propagate(errp
, local_err
);
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
)
249 /* Check cancellation *before* setting busy = false, too! */
250 if (block_job_is_cancelled(job
)) {
255 if (block_job_is_paused(job
)) {
256 qemu_coroutine_yield();
258 co_aio_sleep_ns(bdrv_get_aio_context(job
->bs
), type
, ns
);
263 void block_job_yield(BlockJob
*job
)
267 /* Check cancellation *before* setting busy = false, too! */
268 if (block_job_is_cancelled(job
)) {
273 qemu_coroutine_yield();
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
;
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
,
310 void block_job_event_completed(BlockJob
*job
, const char *msg
)
312 qapi_event_send_block_job_completed(job
->driver
->job_type
,
322 void block_job_event_ready(BlockJob
*job
)
326 qapi_event_send_block_job_ready(job
->driver
->job_type
,
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
;
340 case BLOCKDEV_ON_ERROR_ENOSPC
:
341 action
= (error
== ENOSPC
) ?
342 BLOCK_ERROR_ACTION_STOP
: BLOCK_ERROR_ACTION_REPORT
;
344 case BLOCKDEV_ON_ERROR_STOP
:
345 action
= BLOCK_ERROR_ACTION_STOP
;
347 case BLOCKDEV_ON_ERROR_REPORT
:
348 action
= BLOCK_ERROR_ACTION_REPORT
;
350 case BLOCKDEV_ON_ERROR_IGNORE
:
351 action
= BLOCK_ERROR_ACTION_IGNORE
;
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
);
375 AioContext
*aio_context
;
376 BlockJobDeferToMainLoopFn
*fn
;
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
);
403 void block_job_defer_to_main_loop(BlockJob
*job
,
404 BlockJobDeferToMainLoopFn
*fn
,
407 BlockJobDeferToMainLoopData
*data
= g_malloc(sizeof(*data
));
409 data
->bh
= qemu_bh_new(block_job_defer_to_main_loop_bh
, data
);
410 data
->aio_context
= bdrv_get_aio_context(job
->bs
);
412 data
->opaque
= opaque
;
414 qemu_bh_schedule(data
->bh
);