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 "qemu/osdep.h"
27 #include "qemu-common.h"
28 #include "block/block.h"
29 #include "block/blockjob_int.h"
30 #include "block/block_int.h"
31 #include "sysemu/block-backend.h"
32 #include "qapi/qmp/qerror.h"
33 #include "qapi/qmp/qjson.h"
34 #include "qemu/coroutine.h"
36 #include "qmp-commands.h"
37 #include "qemu/timer.h"
38 #include "qapi-event.h"
40 static void block_job_event_cancelled(BlockJob
*job
);
41 static void block_job_event_completed(BlockJob
*job
, const char *msg
);
43 /* Transactional group of block jobs */
46 /* Is this txn being cancelled? */
50 QLIST_HEAD(, BlockJob
) jobs
;
56 static QLIST_HEAD(, BlockJob
) block_jobs
= QLIST_HEAD_INITIALIZER(block_jobs
);
58 static char *child_job_get_parent_desc(BdrvChild
*c
)
60 BlockJob
*job
= c
->opaque
;
61 return g_strdup_printf("%s job '%s'",
62 BlockJobType_lookup
[job
->driver
->job_type
],
66 static const BdrvChildRole child_job
= {
67 .get_parent_desc
= child_job_get_parent_desc
,
71 BlockJob
*block_job_next(BlockJob
*job
)
74 return QLIST_FIRST(&block_jobs
);
76 return QLIST_NEXT(job
, job_list
);
79 BlockJob
*block_job_get(const char *id
)
83 QLIST_FOREACH(job
, &block_jobs
, job_list
) {
84 if (job
->id
&& !strcmp(id
, job
->id
)) {
92 static void block_job_attached_aio_context(AioContext
*new_context
,
95 BlockJob
*job
= opaque
;
97 if (job
->driver
->attached_aio_context
) {
98 job
->driver
->attached_aio_context(job
, new_context
);
101 block_job_resume(job
);
104 static void block_job_drain(BlockJob
*job
)
106 /* If job is !job->busy this kicks it into the next pause point. */
107 block_job_enter(job
);
110 if (job
->driver
->drain
) {
111 job
->driver
->drain(job
);
115 static void block_job_detach_aio_context(void *opaque
)
117 BlockJob
*job
= opaque
;
119 /* In case the job terminates during aio_poll()... */
122 block_job_pause(job
);
124 while (!job
->paused
&& !job
->completed
) {
125 block_job_drain(job
);
128 block_job_unref(job
);
131 int block_job_add_bdrv(BlockJob
*job
, const char *name
, BlockDriverState
*bs
,
132 uint64_t perm
, uint64_t shared_perm
, Error
**errp
)
136 c
= bdrv_root_attach_child(bs
, name
, &child_job
, perm
, shared_perm
,
142 job
->nodes
= g_slist_prepend(job
->nodes
, c
);
144 bdrv_op_block_all(bs
, job
->blocker
);
149 void *block_job_create(const char *job_id
, const BlockJobDriver
*driver
,
150 BlockDriverState
*bs
, uint64_t perm
,
151 uint64_t shared_perm
, int64_t speed
, int flags
,
152 BlockCompletionFunc
*cb
, void *opaque
, Error
**errp
)
159 error_setg(errp
, QERR_DEVICE_IN_USE
, bdrv_get_device_name(bs
));
163 if (job_id
== NULL
&& !(flags
& BLOCK_JOB_INTERNAL
)) {
164 job_id
= bdrv_get_device_name(bs
);
166 error_setg(errp
, "An explicit job ID is required for this node");
172 if (flags
& BLOCK_JOB_INTERNAL
) {
173 error_setg(errp
, "Cannot specify job ID for internal block job");
177 if (!id_wellformed(job_id
)) {
178 error_setg(errp
, "Invalid job ID '%s'", job_id
);
182 if (block_job_get(job_id
)) {
183 error_setg(errp
, "Job ID '%s' already in use", job_id
);
188 blk
= blk_new(perm
, shared_perm
);
189 ret
= blk_insert_bs(blk
, bs
, errp
);
195 job
= g_malloc0(driver
->instance_size
);
196 error_setg(&job
->blocker
, "block device is in use by block job: %s",
197 BlockJobType_lookup
[driver
->job_type
]);
198 block_job_add_bdrv(job
, "main node", bs
, 0, BLK_PERM_ALL
, &error_abort
);
199 bdrv_op_unblock(bs
, BLOCK_OP_TYPE_DATAPLANE
, job
->blocker
);
201 job
->driver
= driver
;
202 job
->id
= g_strdup(job_id
);
205 job
->opaque
= opaque
;
208 job
->pause_count
= 1;
212 QLIST_INSERT_HEAD(&block_jobs
, job
, job_list
);
214 blk_add_aio_context_notifier(blk
, block_job_attached_aio_context
,
215 block_job_detach_aio_context
, job
);
217 /* Only set speed when necessary to avoid NotSupported error */
219 Error
*local_err
= NULL
;
221 block_job_set_speed(job
, speed
, &local_err
);
223 block_job_unref(job
);
224 error_propagate(errp
, local_err
);
231 bool block_job_is_internal(BlockJob
*job
)
233 return (job
->id
== NULL
);
236 static bool block_job_started(BlockJob
*job
)
241 void block_job_start(BlockJob
*job
)
243 assert(job
&& !block_job_started(job
) && job
->paused
&&
244 !job
->busy
&& job
->driver
->start
);
245 job
->co
= qemu_coroutine_create(job
->driver
->start
, job
);
246 if (--job
->pause_count
== 0) {
249 qemu_coroutine_enter(job
->co
);
253 void block_job_ref(BlockJob
*job
)
258 void block_job_unref(BlockJob
*job
)
260 if (--job
->refcnt
== 0) {
262 BlockDriverState
*bs
= blk_bs(job
->blk
);
264 for (l
= job
->nodes
; l
; l
= l
->next
) {
265 BdrvChild
*c
= l
->data
;
266 bdrv_op_unblock_all(c
->bs
, job
->blocker
);
267 bdrv_root_unref_child(c
);
269 g_slist_free(job
->nodes
);
270 blk_remove_aio_context_notifier(job
->blk
,
271 block_job_attached_aio_context
,
272 block_job_detach_aio_context
, job
);
274 error_free(job
->blocker
);
276 QLIST_REMOVE(job
, job_list
);
281 static void block_job_completed_single(BlockJob
*job
)
284 if (job
->driver
->commit
) {
285 job
->driver
->commit(job
);
288 if (job
->driver
->abort
) {
289 job
->driver
->abort(job
);
292 if (job
->driver
->clean
) {
293 job
->driver
->clean(job
);
297 job
->cb(job
->opaque
, job
->ret
);
300 /* Emit events only if we actually started */
301 if (block_job_started(job
)) {
302 if (block_job_is_cancelled(job
)) {
303 block_job_event_cancelled(job
);
305 const char *msg
= NULL
;
307 msg
= strerror(-job
->ret
);
309 block_job_event_completed(job
, msg
);
314 QLIST_REMOVE(job
, txn_list
);
315 block_job_txn_unref(job
->txn
);
317 block_job_unref(job
);
320 static void block_job_completed_txn_abort(BlockJob
*job
)
323 BlockJobTxn
*txn
= job
->txn
;
324 BlockJob
*other_job
, *next
;
328 * We are cancelled by another job, which will handle everything.
332 txn
->aborting
= true;
333 /* We are the first failed job. Cancel other jobs. */
334 QLIST_FOREACH(other_job
, &txn
->jobs
, txn_list
) {
335 ctx
= blk_get_aio_context(other_job
->blk
);
336 aio_context_acquire(ctx
);
338 QLIST_FOREACH(other_job
, &txn
->jobs
, txn_list
) {
339 if (other_job
== job
|| other_job
->completed
) {
340 /* Other jobs are "effectively" cancelled by us, set the status for
341 * them; this job, however, may or may not be cancelled, depending
342 * on the caller, so leave it. */
343 if (other_job
!= job
) {
344 other_job
->cancelled
= true;
348 block_job_cancel_sync(other_job
);
349 assert(other_job
->completed
);
351 QLIST_FOREACH_SAFE(other_job
, &txn
->jobs
, txn_list
, next
) {
352 ctx
= blk_get_aio_context(other_job
->blk
);
353 block_job_completed_single(other_job
);
354 aio_context_release(ctx
);
358 static void block_job_completed_txn_success(BlockJob
*job
)
361 BlockJobTxn
*txn
= job
->txn
;
362 BlockJob
*other_job
, *next
;
364 * Successful completion, see if there are other running jobs in this
367 QLIST_FOREACH(other_job
, &txn
->jobs
, txn_list
) {
368 if (!other_job
->completed
) {
372 /* We are the last completed job, commit the transaction. */
373 QLIST_FOREACH_SAFE(other_job
, &txn
->jobs
, txn_list
, next
) {
374 ctx
= blk_get_aio_context(other_job
->blk
);
375 aio_context_acquire(ctx
);
376 assert(other_job
->ret
== 0);
377 block_job_completed_single(other_job
);
378 aio_context_release(ctx
);
382 void block_job_completed(BlockJob
*job
, int ret
)
384 assert(blk_bs(job
->blk
)->job
== job
);
385 assert(!job
->completed
);
386 job
->completed
= true;
389 block_job_completed_single(job
);
390 } else if (ret
< 0 || block_job_is_cancelled(job
)) {
391 block_job_completed_txn_abort(job
);
393 block_job_completed_txn_success(job
);
397 void block_job_set_speed(BlockJob
*job
, int64_t speed
, Error
**errp
)
399 Error
*local_err
= NULL
;
401 if (!job
->driver
->set_speed
) {
402 error_setg(errp
, QERR_UNSUPPORTED
);
405 job
->driver
->set_speed(job
, speed
, &local_err
);
407 error_propagate(errp
, local_err
);
414 void block_job_complete(BlockJob
*job
, Error
**errp
)
416 /* Should not be reachable via external interface for internal jobs */
418 if (job
->pause_count
|| job
->cancelled
||
419 !block_job_started(job
) || !job
->driver
->complete
) {
420 error_setg(errp
, "The active block job '%s' cannot be completed",
425 job
->driver
->complete(job
, errp
);
428 void block_job_pause(BlockJob
*job
)
433 void block_job_user_pause(BlockJob
*job
)
435 job
->user_paused
= true;
436 block_job_pause(job
);
439 static bool block_job_should_pause(BlockJob
*job
)
441 return job
->pause_count
> 0;
444 bool block_job_user_paused(BlockJob
*job
)
446 return job
? job
->user_paused
: 0;
449 void coroutine_fn
block_job_pause_point(BlockJob
*job
)
451 assert(job
&& block_job_started(job
));
453 if (!block_job_should_pause(job
)) {
456 if (block_job_is_cancelled(job
)) {
460 if (job
->driver
->pause
) {
461 job
->driver
->pause(job
);
464 if (block_job_should_pause(job
) && !block_job_is_cancelled(job
)) {
467 qemu_coroutine_yield(); /* wait for block_job_resume() */
472 if (job
->driver
->resume
) {
473 job
->driver
->resume(job
);
477 void block_job_resume(BlockJob
*job
)
479 assert(job
->pause_count
> 0);
481 if (job
->pause_count
) {
484 block_job_enter(job
);
487 void block_job_user_resume(BlockJob
*job
)
489 if (job
&& job
->user_paused
&& job
->pause_count
> 0) {
490 job
->user_paused
= false;
491 block_job_resume(job
);
495 void block_job_enter(BlockJob
*job
)
497 if (job
->co
&& !job
->busy
) {
498 qemu_coroutine_enter(job
->co
);
502 void block_job_cancel(BlockJob
*job
)
504 if (block_job_started(job
)) {
505 job
->cancelled
= true;
506 block_job_iostatus_reset(job
);
507 block_job_enter(job
);
509 block_job_completed(job
, -ECANCELED
);
513 bool block_job_is_cancelled(BlockJob
*job
)
515 return job
->cancelled
;
518 void block_job_iostatus_reset(BlockJob
*job
)
520 job
->iostatus
= BLOCK_DEVICE_IO_STATUS_OK
;
521 if (job
->driver
->iostatus_reset
) {
522 job
->driver
->iostatus_reset(job
);
526 static int block_job_finish_sync(BlockJob
*job
,
527 void (*finish
)(BlockJob
*, Error
**errp
),
530 Error
*local_err
= NULL
;
533 assert(blk_bs(job
->blk
)->job
== job
);
537 finish(job
, &local_err
);
539 error_propagate(errp
, local_err
);
540 block_job_unref(job
);
543 /* block_job_drain calls block_job_enter, and it should be enough to
544 * induce progress until the job completes or moves to the main thread.
546 while (!job
->deferred_to_main_loop
&& !job
->completed
) {
547 block_job_drain(job
);
549 while (!job
->completed
) {
550 aio_poll(qemu_get_aio_context(), true);
552 ret
= (job
->cancelled
&& job
->ret
== 0) ? -ECANCELED
: job
->ret
;
553 block_job_unref(job
);
557 /* A wrapper around block_job_cancel() taking an Error ** parameter so it may be
558 * used with block_job_finish_sync() without the need for (rather nasty)
559 * function pointer casts there. */
560 static void block_job_cancel_err(BlockJob
*job
, Error
**errp
)
562 block_job_cancel(job
);
565 int block_job_cancel_sync(BlockJob
*job
)
567 return block_job_finish_sync(job
, &block_job_cancel_err
, NULL
);
570 void block_job_cancel_sync_all(void)
573 AioContext
*aio_context
;
575 while ((job
= QLIST_FIRST(&block_jobs
))) {
576 aio_context
= blk_get_aio_context(job
->blk
);
577 aio_context_acquire(aio_context
);
578 block_job_cancel_sync(job
);
579 aio_context_release(aio_context
);
583 int block_job_complete_sync(BlockJob
*job
, Error
**errp
)
585 return block_job_finish_sync(job
, &block_job_complete
, errp
);
588 void block_job_sleep_ns(BlockJob
*job
, QEMUClockType type
, int64_t ns
)
592 /* Check cancellation *before* setting busy = false, too! */
593 if (block_job_is_cancelled(job
)) {
598 if (!block_job_should_pause(job
)) {
599 co_aio_sleep_ns(blk_get_aio_context(job
->blk
), type
, ns
);
603 block_job_pause_point(job
);
606 void block_job_yield(BlockJob
*job
)
610 /* Check cancellation *before* setting busy = false, too! */
611 if (block_job_is_cancelled(job
)) {
616 if (!block_job_should_pause(job
)) {
617 qemu_coroutine_yield();
621 block_job_pause_point(job
);
624 BlockJobInfo
*block_job_query(BlockJob
*job
, Error
**errp
)
628 if (block_job_is_internal(job
)) {
629 error_setg(errp
, "Cannot query QEMU internal jobs");
632 info
= g_new0(BlockJobInfo
, 1);
633 info
->type
= g_strdup(BlockJobType_lookup
[job
->driver
->job_type
]);
634 info
->device
= g_strdup(job
->id
);
635 info
->len
= job
->len
;
636 info
->busy
= job
->busy
;
637 info
->paused
= job
->pause_count
> 0;
638 info
->offset
= job
->offset
;
639 info
->speed
= job
->speed
;
640 info
->io_status
= job
->iostatus
;
641 info
->ready
= job
->ready
;
645 static void block_job_iostatus_set_err(BlockJob
*job
, int error
)
647 if (job
->iostatus
== BLOCK_DEVICE_IO_STATUS_OK
) {
648 job
->iostatus
= error
== ENOSPC
? BLOCK_DEVICE_IO_STATUS_NOSPACE
:
649 BLOCK_DEVICE_IO_STATUS_FAILED
;
653 static void block_job_event_cancelled(BlockJob
*job
)
655 if (block_job_is_internal(job
)) {
659 qapi_event_send_block_job_cancelled(job
->driver
->job_type
,
667 static void block_job_event_completed(BlockJob
*job
, const char *msg
)
669 if (block_job_is_internal(job
)) {
673 qapi_event_send_block_job_completed(job
->driver
->job_type
,
683 void block_job_event_ready(BlockJob
*job
)
687 if (block_job_is_internal(job
)) {
691 qapi_event_send_block_job_ready(job
->driver
->job_type
,
695 job
->speed
, &error_abort
);
698 BlockErrorAction
block_job_error_action(BlockJob
*job
, BlockdevOnError on_err
,
699 int is_read
, int error
)
701 BlockErrorAction action
;
704 case BLOCKDEV_ON_ERROR_ENOSPC
:
705 case BLOCKDEV_ON_ERROR_AUTO
:
706 action
= (error
== ENOSPC
) ?
707 BLOCK_ERROR_ACTION_STOP
: BLOCK_ERROR_ACTION_REPORT
;
709 case BLOCKDEV_ON_ERROR_STOP
:
710 action
= BLOCK_ERROR_ACTION_STOP
;
712 case BLOCKDEV_ON_ERROR_REPORT
:
713 action
= BLOCK_ERROR_ACTION_REPORT
;
715 case BLOCKDEV_ON_ERROR_IGNORE
:
716 action
= BLOCK_ERROR_ACTION_IGNORE
;
721 if (!block_job_is_internal(job
)) {
722 qapi_event_send_block_job_error(job
->id
,
723 is_read
? IO_OPERATION_TYPE_READ
:
724 IO_OPERATION_TYPE_WRITE
,
725 action
, &error_abort
);
727 if (action
== BLOCK_ERROR_ACTION_STOP
) {
728 /* make the pause user visible, which will be resumed from QMP. */
729 block_job_user_pause(job
);
730 block_job_iostatus_set_err(job
, error
);
737 AioContext
*aio_context
;
738 BlockJobDeferToMainLoopFn
*fn
;
740 } BlockJobDeferToMainLoopData
;
742 static void block_job_defer_to_main_loop_bh(void *opaque
)
744 BlockJobDeferToMainLoopData
*data
= opaque
;
745 AioContext
*aio_context
;
747 /* Prevent race with block_job_defer_to_main_loop() */
748 aio_context_acquire(data
->aio_context
);
750 /* Fetch BDS AioContext again, in case it has changed */
751 aio_context
= blk_get_aio_context(data
->job
->blk
);
752 aio_context_acquire(aio_context
);
754 data
->job
->deferred_to_main_loop
= false;
755 data
->fn(data
->job
, data
->opaque
);
757 aio_context_release(aio_context
);
759 aio_context_release(data
->aio_context
);
764 void block_job_defer_to_main_loop(BlockJob
*job
,
765 BlockJobDeferToMainLoopFn
*fn
,
768 BlockJobDeferToMainLoopData
*data
= g_malloc(sizeof(*data
));
770 data
->aio_context
= blk_get_aio_context(job
->blk
);
772 data
->opaque
= opaque
;
773 job
->deferred_to_main_loop
= true;
775 aio_bh_schedule_oneshot(qemu_get_aio_context(),
776 block_job_defer_to_main_loop_bh
, data
);
779 BlockJobTxn
*block_job_txn_new(void)
781 BlockJobTxn
*txn
= g_new0(BlockJobTxn
, 1);
782 QLIST_INIT(&txn
->jobs
);
787 static void block_job_txn_ref(BlockJobTxn
*txn
)
792 void block_job_txn_unref(BlockJobTxn
*txn
)
794 if (txn
&& --txn
->refcnt
== 0) {
799 void block_job_txn_add_job(BlockJobTxn
*txn
, BlockJob
*job
)
808 QLIST_INSERT_HEAD(&txn
->jobs
, job
, txn_list
);
809 block_job_txn_ref(txn
);