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 "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
)
46 error_setg(errp
, QERR_DEVICE_IN_USE
, bdrv_get_device_name(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
);
63 /* Only set speed when necessary to avoid NotSupported error */
65 Error
*local_err
= NULL
;
67 block_job_set_speed(job
, speed
, &local_err
);
70 bdrv_op_unblock_all(bs
, job
->blocker
);
71 error_free(job
->blocker
);
73 error_propagate(errp
, local_err
);
80 void block_job_completed(BlockJob
*job
, int ret
)
82 BlockDriverState
*bs
= job
->bs
;
84 assert(bs
->job
== job
);
85 job
->cb(job
->opaque
, ret
);
87 bdrv_op_unblock_all(bs
, job
->blocker
);
88 error_free(job
->blocker
);
92 void block_job_set_speed(BlockJob
*job
, int64_t speed
, Error
**errp
)
94 Error
*local_err
= NULL
;
96 if (!job
->driver
->set_speed
) {
97 error_setg(errp
, QERR_UNSUPPORTED
);
100 job
->driver
->set_speed(job
, speed
, &local_err
);
102 error_propagate(errp
, local_err
);
109 void block_job_complete(BlockJob
*job
, Error
**errp
)
111 if (job
->pause_count
|| job
->cancelled
|| !job
->driver
->complete
) {
112 error_setg(errp
, QERR_BLOCK_JOB_NOT_READY
,
113 bdrv_get_device_name(job
->bs
));
117 job
->driver
->complete(job
, errp
);
120 void block_job_pause(BlockJob
*job
)
125 bool block_job_is_paused(BlockJob
*job
)
127 return job
->pause_count
> 0;
130 void block_job_resume(BlockJob
*job
)
132 assert(job
->pause_count
> 0);
134 if (job
->pause_count
) {
137 block_job_enter(job
);
140 void block_job_enter(BlockJob
*job
)
142 block_job_iostatus_reset(job
);
143 if (job
->co
&& !job
->busy
) {
144 qemu_coroutine_enter(job
->co
, NULL
);
148 void block_job_cancel(BlockJob
*job
)
150 job
->cancelled
= true;
151 block_job_enter(job
);
154 bool block_job_is_cancelled(BlockJob
*job
)
156 return job
->cancelled
;
159 void block_job_iostatus_reset(BlockJob
*job
)
161 job
->iostatus
= BLOCK_DEVICE_IO_STATUS_OK
;
162 if (job
->driver
->iostatus_reset
) {
163 job
->driver
->iostatus_reset(job
);
167 struct BlockFinishData
{
169 BlockCompletionFunc
*cb
;
175 static void block_job_finish_cb(void *opaque
, int ret
)
177 struct BlockFinishData
*data
= opaque
;
179 data
->cancelled
= block_job_is_cancelled(data
->job
);
181 data
->cb(data
->opaque
, ret
);
184 static int block_job_finish_sync(BlockJob
*job
,
185 void (*finish
)(BlockJob
*, Error
**errp
),
188 struct BlockFinishData data
;
189 BlockDriverState
*bs
= job
->bs
;
190 Error
*local_err
= NULL
;
192 assert(bs
->job
== job
);
194 /* Set up our own callback to store the result and chain to
195 * the original callback.
199 data
.opaque
= job
->opaque
;
200 data
.ret
= -EINPROGRESS
;
201 job
->cb
= block_job_finish_cb
;
203 finish(job
, &local_err
);
205 error_propagate(errp
, local_err
);
208 while (data
.ret
== -EINPROGRESS
) {
209 aio_poll(bdrv_get_aio_context(bs
), true);
211 return (data
.cancelled
&& data
.ret
== 0) ? -ECANCELED
: data
.ret
;
214 /* A wrapper around block_job_cancel() taking an Error ** parameter so it may be
215 * used with block_job_finish_sync() without the need for (rather nasty)
216 * function pointer casts there. */
217 static void block_job_cancel_err(BlockJob
*job
, Error
**errp
)
219 block_job_cancel(job
);
222 int block_job_cancel_sync(BlockJob
*job
)
224 return block_job_finish_sync(job
, &block_job_cancel_err
, NULL
);
227 int block_job_complete_sync(BlockJob
*job
, Error
**errp
)
229 return block_job_finish_sync(job
, &block_job_complete
, errp
);
232 void block_job_sleep_ns(BlockJob
*job
, QEMUClockType type
, int64_t ns
)
236 /* Check cancellation *before* setting busy = false, too! */
237 if (block_job_is_cancelled(job
)) {
242 if (block_job_is_paused(job
)) {
243 qemu_coroutine_yield();
245 co_aio_sleep_ns(bdrv_get_aio_context(job
->bs
), type
, ns
);
250 void block_job_yield(BlockJob
*job
)
254 /* Check cancellation *before* setting busy = false, too! */
255 if (block_job_is_cancelled(job
)) {
260 qemu_coroutine_yield();
264 BlockJobInfo
*block_job_query(BlockJob
*job
)
266 BlockJobInfo
*info
= g_new0(BlockJobInfo
, 1);
267 info
->type
= g_strdup(BlockJobType_lookup
[job
->driver
->job_type
]);
268 info
->device
= g_strdup(bdrv_get_device_name(job
->bs
));
269 info
->len
= job
->len
;
270 info
->busy
= job
->busy
;
271 info
->paused
= job
->pause_count
> 0;
272 info
->offset
= job
->offset
;
273 info
->speed
= job
->speed
;
274 info
->io_status
= job
->iostatus
;
275 info
->ready
= job
->ready
;
279 static void block_job_iostatus_set_err(BlockJob
*job
, int error
)
281 if (job
->iostatus
== BLOCK_DEVICE_IO_STATUS_OK
) {
282 job
->iostatus
= error
== ENOSPC
? BLOCK_DEVICE_IO_STATUS_NOSPACE
:
283 BLOCK_DEVICE_IO_STATUS_FAILED
;
287 void block_job_event_cancelled(BlockJob
*job
)
289 qapi_event_send_block_job_cancelled(job
->driver
->job_type
,
290 bdrv_get_device_name(job
->bs
),
297 void block_job_event_completed(BlockJob
*job
, const char *msg
)
299 qapi_event_send_block_job_completed(job
->driver
->job_type
,
300 bdrv_get_device_name(job
->bs
),
309 void block_job_event_ready(BlockJob
*job
)
313 qapi_event_send_block_job_ready(job
->driver
->job_type
,
314 bdrv_get_device_name(job
->bs
),
317 job
->speed
, &error_abort
);
320 BlockErrorAction
block_job_error_action(BlockJob
*job
, BlockDriverState
*bs
,
321 BlockdevOnError on_err
,
322 int is_read
, int error
)
324 BlockErrorAction action
;
327 case BLOCKDEV_ON_ERROR_ENOSPC
:
328 action
= (error
== ENOSPC
) ?
329 BLOCK_ERROR_ACTION_STOP
: BLOCK_ERROR_ACTION_REPORT
;
331 case BLOCKDEV_ON_ERROR_STOP
:
332 action
= BLOCK_ERROR_ACTION_STOP
;
334 case BLOCKDEV_ON_ERROR_REPORT
:
335 action
= BLOCK_ERROR_ACTION_REPORT
;
337 case BLOCKDEV_ON_ERROR_IGNORE
:
338 action
= BLOCK_ERROR_ACTION_IGNORE
;
343 qapi_event_send_block_job_error(bdrv_get_device_name(job
->bs
),
344 is_read
? IO_OPERATION_TYPE_READ
:
345 IO_OPERATION_TYPE_WRITE
,
346 action
, &error_abort
);
347 if (action
== BLOCK_ERROR_ACTION_STOP
) {
348 /* make the pause user visible, which will be resumed from QMP. */
349 job
->user_paused
= true;
350 block_job_pause(job
);
351 block_job_iostatus_set_err(job
, error
);
353 bdrv_iostatus_set_err(bs
, error
);
362 AioContext
*aio_context
;
363 BlockJobDeferToMainLoopFn
*fn
;
365 } BlockJobDeferToMainLoopData
;
367 static void block_job_defer_to_main_loop_bh(void *opaque
)
369 BlockJobDeferToMainLoopData
*data
= opaque
;
370 AioContext
*aio_context
;
372 qemu_bh_delete(data
->bh
);
374 /* Prevent race with block_job_defer_to_main_loop() */
375 aio_context_acquire(data
->aio_context
);
377 /* Fetch BDS AioContext again, in case it has changed */
378 aio_context
= bdrv_get_aio_context(data
->job
->bs
);
379 aio_context_acquire(aio_context
);
381 data
->fn(data
->job
, data
->opaque
);
383 aio_context_release(aio_context
);
385 aio_context_release(data
->aio_context
);
390 void block_job_defer_to_main_loop(BlockJob
*job
,
391 BlockJobDeferToMainLoopFn
*fn
,
394 BlockJobDeferToMainLoopData
*data
= g_malloc(sizeof(*data
));
396 data
->bh
= qemu_bh_new(block_job_defer_to_main_loop_bh
, data
);
397 data
->aio_context
= bdrv_get_aio_context(job
->bs
);
399 data
->opaque
= opaque
;
401 qemu_bh_schedule(data
->bh
);