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
);
59 * The block job API is composed of two categories of functions.
61 * The first includes functions used by the monitor. The monitor is
62 * peculiar in that it accesses the block job list with block_job_get, and
63 * therefore needs consistency across block_job_get and the actual operation
64 * (e.g. block_job_set_speed). The consistency is achieved with
65 * aio_context_acquire/release. These functions are declared in blockjob.h.
67 * The second includes functions used by the block job drivers and sometimes
68 * by the core block layer. These do not care about locking, because the
69 * whole coroutine runs under the AioContext lock, and are declared in
73 BlockJob
*block_job_next(BlockJob
*job
)
76 return QLIST_FIRST(&block_jobs
);
78 return QLIST_NEXT(job
, job_list
);
81 BlockJob
*block_job_get(const char *id
)
85 QLIST_FOREACH(job
, &block_jobs
, job_list
) {
86 if (job
->id
&& !strcmp(id
, job
->id
)) {
94 BlockJobTxn
*block_job_txn_new(void)
96 BlockJobTxn
*txn
= g_new0(BlockJobTxn
, 1);
97 QLIST_INIT(&txn
->jobs
);
102 static void block_job_txn_ref(BlockJobTxn
*txn
)
107 void block_job_txn_unref(BlockJobTxn
*txn
)
109 if (txn
&& --txn
->refcnt
== 0) {
114 void block_job_txn_add_job(BlockJobTxn
*txn
, BlockJob
*job
)
123 QLIST_INSERT_HEAD(&txn
->jobs
, job
, txn_list
);
124 block_job_txn_ref(txn
);
127 static void block_job_pause(BlockJob
*job
)
132 static void block_job_resume(BlockJob
*job
)
134 assert(job
->pause_count
> 0);
136 if (job
->pause_count
) {
139 block_job_enter(job
);
142 static void block_job_ref(BlockJob
*job
)
147 static void block_job_attached_aio_context(AioContext
*new_context
,
149 static void block_job_detach_aio_context(void *opaque
);
151 static void block_job_unref(BlockJob
*job
)
153 if (--job
->refcnt
== 0) {
154 BlockDriverState
*bs
= blk_bs(job
->blk
);
156 block_job_remove_all_bdrv(job
);
157 blk_remove_aio_context_notifier(job
->blk
,
158 block_job_attached_aio_context
,
159 block_job_detach_aio_context
, job
);
161 error_free(job
->blocker
);
163 QLIST_REMOVE(job
, job_list
);
168 static void block_job_attached_aio_context(AioContext
*new_context
,
171 BlockJob
*job
= opaque
;
173 if (job
->driver
->attached_aio_context
) {
174 job
->driver
->attached_aio_context(job
, new_context
);
177 block_job_resume(job
);
180 static void block_job_drain(BlockJob
*job
)
182 /* If job is !job->busy this kicks it into the next pause point. */
183 block_job_enter(job
);
186 if (job
->driver
->drain
) {
187 job
->driver
->drain(job
);
191 static void block_job_detach_aio_context(void *opaque
)
193 BlockJob
*job
= opaque
;
195 /* In case the job terminates during aio_poll()... */
198 block_job_pause(job
);
200 while (!job
->paused
&& !job
->completed
) {
201 block_job_drain(job
);
204 block_job_unref(job
);
207 static char *child_job_get_parent_desc(BdrvChild
*c
)
209 BlockJob
*job
= c
->opaque
;
210 return g_strdup_printf("%s job '%s'",
211 BlockJobType_lookup
[job
->driver
->job_type
],
215 static const BdrvChildRole child_job
= {
216 .get_parent_desc
= child_job_get_parent_desc
,
217 .stay_at_node
= true,
220 static void block_job_drained_begin(void *opaque
)
222 BlockJob
*job
= opaque
;
223 block_job_pause(job
);
226 static void block_job_drained_end(void *opaque
)
228 BlockJob
*job
= opaque
;
229 block_job_resume(job
);
232 static const BlockDevOps block_job_dev_ops
= {
233 .drained_begin
= block_job_drained_begin
,
234 .drained_end
= block_job_drained_end
,
237 void block_job_remove_all_bdrv(BlockJob
*job
)
240 for (l
= job
->nodes
; l
; l
= l
->next
) {
241 BdrvChild
*c
= l
->data
;
242 bdrv_op_unblock_all(c
->bs
, job
->blocker
);
243 bdrv_root_unref_child(c
);
245 g_slist_free(job
->nodes
);
249 int block_job_add_bdrv(BlockJob
*job
, const char *name
, BlockDriverState
*bs
,
250 uint64_t perm
, uint64_t shared_perm
, Error
**errp
)
254 c
= bdrv_root_attach_child(bs
, name
, &child_job
, perm
, shared_perm
,
260 job
->nodes
= g_slist_prepend(job
->nodes
, c
);
262 bdrv_op_block_all(bs
, job
->blocker
);
267 bool block_job_is_internal(BlockJob
*job
)
269 return (job
->id
== NULL
);
272 static bool block_job_started(BlockJob
*job
)
278 * All jobs must allow a pause point before entering their job proper. This
279 * ensures that jobs can be paused prior to being started, then resumed later.
281 static void coroutine_fn
block_job_co_entry(void *opaque
)
283 BlockJob
*job
= opaque
;
285 assert(job
&& job
->driver
&& job
->driver
->start
);
286 block_job_pause_point(job
);
287 job
->driver
->start(job
);
290 void block_job_start(BlockJob
*job
)
292 assert(job
&& !block_job_started(job
) && job
->paused
&&
293 job
->driver
&& job
->driver
->start
);
294 job
->co
= qemu_coroutine_create(block_job_co_entry
, job
);
298 bdrv_coroutine_enter(blk_bs(job
->blk
), job
->co
);
301 static void block_job_completed_single(BlockJob
*job
)
303 assert(job
->completed
);
306 if (job
->driver
->commit
) {
307 job
->driver
->commit(job
);
310 if (job
->driver
->abort
) {
311 job
->driver
->abort(job
);
314 if (job
->driver
->clean
) {
315 job
->driver
->clean(job
);
319 job
->cb(job
->opaque
, job
->ret
);
322 /* Emit events only if we actually started */
323 if (block_job_started(job
)) {
324 if (block_job_is_cancelled(job
)) {
325 block_job_event_cancelled(job
);
327 const char *msg
= NULL
;
329 msg
= strerror(-job
->ret
);
331 block_job_event_completed(job
, msg
);
336 QLIST_REMOVE(job
, txn_list
);
337 block_job_txn_unref(job
->txn
);
339 block_job_unref(job
);
342 static void block_job_cancel_async(BlockJob
*job
)
344 if (job
->iostatus
!= BLOCK_DEVICE_IO_STATUS_OK
) {
345 block_job_iostatus_reset(job
);
347 if (job
->user_paused
) {
348 /* Do not call block_job_enter here, the caller will handle it. */
349 job
->user_paused
= false;
352 job
->cancelled
= true;
355 static int block_job_finish_sync(BlockJob
*job
,
356 void (*finish
)(BlockJob
*, Error
**errp
),
359 Error
*local_err
= NULL
;
362 assert(blk_bs(job
->blk
)->job
== job
);
367 finish(job
, &local_err
);
370 error_propagate(errp
, local_err
);
371 block_job_unref(job
);
374 /* block_job_drain calls block_job_enter, and it should be enough to
375 * induce progress until the job completes or moves to the main thread.
377 while (!job
->deferred_to_main_loop
&& !job
->completed
) {
378 block_job_drain(job
);
380 while (!job
->completed
) {
381 aio_poll(qemu_get_aio_context(), true);
383 ret
= (job
->cancelled
&& job
->ret
== 0) ? -ECANCELED
: job
->ret
;
384 block_job_unref(job
);
388 static void block_job_completed_txn_abort(BlockJob
*job
)
391 BlockJobTxn
*txn
= job
->txn
;
396 * We are cancelled by another job, which will handle everything.
400 txn
->aborting
= true;
401 block_job_txn_ref(txn
);
403 /* We are the first failed job. Cancel other jobs. */
404 QLIST_FOREACH(other_job
, &txn
->jobs
, txn_list
) {
405 ctx
= blk_get_aio_context(other_job
->blk
);
406 aio_context_acquire(ctx
);
409 /* Other jobs are effectively cancelled by us, set the status for
410 * them; this job, however, may or may not be cancelled, depending
411 * on the caller, so leave it. */
412 QLIST_FOREACH(other_job
, &txn
->jobs
, txn_list
) {
413 if (other_job
!= job
) {
414 block_job_cancel_async(other_job
);
417 while (!QLIST_EMPTY(&txn
->jobs
)) {
418 other_job
= QLIST_FIRST(&txn
->jobs
);
419 ctx
= blk_get_aio_context(other_job
->blk
);
420 if (!other_job
->completed
) {
421 assert(other_job
->cancelled
);
422 block_job_finish_sync(other_job
, NULL
, NULL
);
424 block_job_completed_single(other_job
);
425 aio_context_release(ctx
);
428 block_job_txn_unref(txn
);
431 static void block_job_completed_txn_success(BlockJob
*job
)
434 BlockJobTxn
*txn
= job
->txn
;
435 BlockJob
*other_job
, *next
;
437 * Successful completion, see if there are other running jobs in this
440 QLIST_FOREACH(other_job
, &txn
->jobs
, txn_list
) {
441 if (!other_job
->completed
) {
445 /* We are the last completed job, commit the transaction. */
446 QLIST_FOREACH_SAFE(other_job
, &txn
->jobs
, txn_list
, next
) {
447 ctx
= blk_get_aio_context(other_job
->blk
);
448 aio_context_acquire(ctx
);
449 assert(other_job
->ret
== 0);
450 block_job_completed_single(other_job
);
451 aio_context_release(ctx
);
455 void block_job_set_speed(BlockJob
*job
, int64_t speed
, Error
**errp
)
457 Error
*local_err
= NULL
;
459 if (!job
->driver
->set_speed
) {
460 error_setg(errp
, QERR_UNSUPPORTED
);
463 job
->driver
->set_speed(job
, speed
, &local_err
);
465 error_propagate(errp
, local_err
);
472 void block_job_complete(BlockJob
*job
, Error
**errp
)
474 /* Should not be reachable via external interface for internal jobs */
476 if (job
->pause_count
|| job
->cancelled
||
477 !block_job_started(job
) || !job
->driver
->complete
) {
478 error_setg(errp
, "The active block job '%s' cannot be completed",
483 job
->driver
->complete(job
, errp
);
486 void block_job_user_pause(BlockJob
*job
)
488 job
->user_paused
= true;
489 block_job_pause(job
);
492 bool block_job_user_paused(BlockJob
*job
)
494 return job
->user_paused
;
497 void block_job_user_resume(BlockJob
*job
)
499 if (job
&& job
->user_paused
&& job
->pause_count
> 0) {
500 block_job_iostatus_reset(job
);
501 job
->user_paused
= false;
502 block_job_resume(job
);
506 void block_job_cancel(BlockJob
*job
)
508 if (block_job_started(job
)) {
509 block_job_cancel_async(job
);
510 block_job_enter(job
);
512 block_job_completed(job
, -ECANCELED
);
516 /* A wrapper around block_job_cancel() taking an Error ** parameter so it may be
517 * used with block_job_finish_sync() without the need for (rather nasty)
518 * function pointer casts there. */
519 static void block_job_cancel_err(BlockJob
*job
, Error
**errp
)
521 block_job_cancel(job
);
524 int block_job_cancel_sync(BlockJob
*job
)
526 return block_job_finish_sync(job
, &block_job_cancel_err
, NULL
);
529 void block_job_cancel_sync_all(void)
532 AioContext
*aio_context
;
534 while ((job
= QLIST_FIRST(&block_jobs
))) {
535 aio_context
= blk_get_aio_context(job
->blk
);
536 aio_context_acquire(aio_context
);
537 block_job_cancel_sync(job
);
538 aio_context_release(aio_context
);
542 int block_job_complete_sync(BlockJob
*job
, Error
**errp
)
544 return block_job_finish_sync(job
, &block_job_complete
, errp
);
547 BlockJobInfo
*block_job_query(BlockJob
*job
, Error
**errp
)
551 if (block_job_is_internal(job
)) {
552 error_setg(errp
, "Cannot query QEMU internal jobs");
555 info
= g_new0(BlockJobInfo
, 1);
556 info
->type
= g_strdup(BlockJobType_lookup
[job
->driver
->job_type
]);
557 info
->device
= g_strdup(job
->id
);
558 info
->len
= job
->len
;
559 info
->busy
= job
->busy
;
560 info
->paused
= job
->pause_count
> 0;
561 info
->offset
= job
->offset
;
562 info
->speed
= job
->speed
;
563 info
->io_status
= job
->iostatus
;
564 info
->ready
= job
->ready
;
568 static void block_job_iostatus_set_err(BlockJob
*job
, int error
)
570 if (job
->iostatus
== BLOCK_DEVICE_IO_STATUS_OK
) {
571 job
->iostatus
= error
== ENOSPC
? BLOCK_DEVICE_IO_STATUS_NOSPACE
:
572 BLOCK_DEVICE_IO_STATUS_FAILED
;
576 static void block_job_event_cancelled(BlockJob
*job
)
578 if (block_job_is_internal(job
)) {
582 qapi_event_send_block_job_cancelled(job
->driver
->job_type
,
590 static void block_job_event_completed(BlockJob
*job
, const char *msg
)
592 if (block_job_is_internal(job
)) {
596 qapi_event_send_block_job_completed(job
->driver
->job_type
,
607 * API for block job drivers and the block layer. These functions are
608 * declared in blockjob_int.h.
611 void *block_job_create(const char *job_id
, const BlockJobDriver
*driver
,
612 BlockDriverState
*bs
, uint64_t perm
,
613 uint64_t shared_perm
, int64_t speed
, int flags
,
614 BlockCompletionFunc
*cb
, void *opaque
, Error
**errp
)
621 error_setg(errp
, QERR_DEVICE_IN_USE
, bdrv_get_device_name(bs
));
625 if (job_id
== NULL
&& !(flags
& BLOCK_JOB_INTERNAL
)) {
626 job_id
= bdrv_get_device_name(bs
);
628 error_setg(errp
, "An explicit job ID is required for this node");
634 if (flags
& BLOCK_JOB_INTERNAL
) {
635 error_setg(errp
, "Cannot specify job ID for internal block job");
639 if (!id_wellformed(job_id
)) {
640 error_setg(errp
, "Invalid job ID '%s'", job_id
);
644 if (block_job_get(job_id
)) {
645 error_setg(errp
, "Job ID '%s' already in use", job_id
);
650 blk
= blk_new(perm
, shared_perm
);
651 ret
= blk_insert_bs(blk
, bs
, errp
);
657 job
= g_malloc0(driver
->instance_size
);
658 job
->driver
= driver
;
659 job
->id
= g_strdup(job_id
);
662 job
->opaque
= opaque
;
665 job
->pause_count
= 1;
668 error_setg(&job
->blocker
, "block device is in use by block job: %s",
669 BlockJobType_lookup
[driver
->job_type
]);
670 block_job_add_bdrv(job
, "main node", bs
, 0, BLK_PERM_ALL
, &error_abort
);
673 blk_set_dev_ops(blk
, &block_job_dev_ops
, job
);
674 bdrv_op_unblock(bs
, BLOCK_OP_TYPE_DATAPLANE
, job
->blocker
);
676 QLIST_INSERT_HEAD(&block_jobs
, job
, job_list
);
678 blk_add_aio_context_notifier(blk
, block_job_attached_aio_context
,
679 block_job_detach_aio_context
, job
);
681 /* Only set speed when necessary to avoid NotSupported error */
683 Error
*local_err
= NULL
;
685 block_job_set_speed(job
, speed
, &local_err
);
687 block_job_unref(job
);
688 error_propagate(errp
, local_err
);
695 void block_job_pause_all(void)
697 BlockJob
*job
= NULL
;
698 while ((job
= block_job_next(job
))) {
699 AioContext
*aio_context
= blk_get_aio_context(job
->blk
);
701 aio_context_acquire(aio_context
);
702 block_job_pause(job
);
703 aio_context_release(aio_context
);
707 void block_job_early_fail(BlockJob
*job
)
709 block_job_unref(job
);
712 void block_job_completed(BlockJob
*job
, int ret
)
714 assert(blk_bs(job
->blk
)->job
== job
);
715 assert(!job
->completed
);
716 job
->completed
= true;
719 block_job_completed_single(job
);
720 } else if (ret
< 0 || block_job_is_cancelled(job
)) {
721 block_job_completed_txn_abort(job
);
723 block_job_completed_txn_success(job
);
727 static bool block_job_should_pause(BlockJob
*job
)
729 return job
->pause_count
> 0;
732 void coroutine_fn
block_job_pause_point(BlockJob
*job
)
734 assert(job
&& block_job_started(job
));
736 if (!block_job_should_pause(job
)) {
739 if (block_job_is_cancelled(job
)) {
743 if (job
->driver
->pause
) {
744 job
->driver
->pause(job
);
747 if (block_job_should_pause(job
) && !block_job_is_cancelled(job
)) {
750 qemu_coroutine_yield(); /* wait for block_job_resume() */
755 if (job
->driver
->resume
) {
756 job
->driver
->resume(job
);
760 void block_job_resume_all(void)
762 BlockJob
*job
= NULL
;
763 while ((job
= block_job_next(job
))) {
764 AioContext
*aio_context
= blk_get_aio_context(job
->blk
);
766 aio_context_acquire(aio_context
);
767 block_job_resume(job
);
768 aio_context_release(aio_context
);
772 void block_job_enter(BlockJob
*job
)
774 if (job
->co
&& !job
->busy
) {
775 bdrv_coroutine_enter(blk_bs(job
->blk
), job
->co
);
779 bool block_job_is_cancelled(BlockJob
*job
)
781 return job
->cancelled
;
784 void block_job_sleep_ns(BlockJob
*job
, QEMUClockType type
, int64_t ns
)
788 /* Check cancellation *before* setting busy = false, too! */
789 if (block_job_is_cancelled(job
)) {
794 if (!block_job_should_pause(job
)) {
795 co_aio_sleep_ns(blk_get_aio_context(job
->blk
), type
, ns
);
799 block_job_pause_point(job
);
802 void block_job_yield(BlockJob
*job
)
806 /* Check cancellation *before* setting busy = false, too! */
807 if (block_job_is_cancelled(job
)) {
812 if (!block_job_should_pause(job
)) {
813 qemu_coroutine_yield();
817 block_job_pause_point(job
);
820 void block_job_iostatus_reset(BlockJob
*job
)
822 if (job
->iostatus
== BLOCK_DEVICE_IO_STATUS_OK
) {
825 assert(job
->user_paused
&& job
->pause_count
> 0);
826 job
->iostatus
= BLOCK_DEVICE_IO_STATUS_OK
;
829 void block_job_event_ready(BlockJob
*job
)
833 if (block_job_is_internal(job
)) {
837 qapi_event_send_block_job_ready(job
->driver
->job_type
,
841 job
->speed
, &error_abort
);
844 BlockErrorAction
block_job_error_action(BlockJob
*job
, BlockdevOnError on_err
,
845 int is_read
, int error
)
847 BlockErrorAction action
;
850 case BLOCKDEV_ON_ERROR_ENOSPC
:
851 case BLOCKDEV_ON_ERROR_AUTO
:
852 action
= (error
== ENOSPC
) ?
853 BLOCK_ERROR_ACTION_STOP
: BLOCK_ERROR_ACTION_REPORT
;
855 case BLOCKDEV_ON_ERROR_STOP
:
856 action
= BLOCK_ERROR_ACTION_STOP
;
858 case BLOCKDEV_ON_ERROR_REPORT
:
859 action
= BLOCK_ERROR_ACTION_REPORT
;
861 case BLOCKDEV_ON_ERROR_IGNORE
:
862 action
= BLOCK_ERROR_ACTION_IGNORE
;
867 if (!block_job_is_internal(job
)) {
868 qapi_event_send_block_job_error(job
->id
,
869 is_read
? IO_OPERATION_TYPE_READ
:
870 IO_OPERATION_TYPE_WRITE
,
871 action
, &error_abort
);
873 if (action
== BLOCK_ERROR_ACTION_STOP
) {
874 /* make the pause user visible, which will be resumed from QMP. */
875 block_job_user_pause(job
);
876 block_job_iostatus_set_err(job
, error
);
883 AioContext
*aio_context
;
884 BlockJobDeferToMainLoopFn
*fn
;
886 } BlockJobDeferToMainLoopData
;
888 static void block_job_defer_to_main_loop_bh(void *opaque
)
890 BlockJobDeferToMainLoopData
*data
= opaque
;
891 AioContext
*aio_context
;
893 /* Prevent race with block_job_defer_to_main_loop() */
894 aio_context_acquire(data
->aio_context
);
896 /* Fetch BDS AioContext again, in case it has changed */
897 aio_context
= blk_get_aio_context(data
->job
->blk
);
898 if (aio_context
!= data
->aio_context
) {
899 aio_context_acquire(aio_context
);
902 data
->job
->deferred_to_main_loop
= false;
903 data
->fn(data
->job
, data
->opaque
);
905 if (aio_context
!= data
->aio_context
) {
906 aio_context_release(aio_context
);
909 aio_context_release(data
->aio_context
);
914 void block_job_defer_to_main_loop(BlockJob
*job
,
915 BlockJobDeferToMainLoopFn
*fn
,
918 BlockJobDeferToMainLoopData
*data
= g_malloc(sizeof(*data
));
920 data
->aio_context
= blk_get_aio_context(job
->blk
);
922 data
->opaque
= opaque
;
923 job
->deferred_to_main_loop
= true;
925 aio_bh_schedule_oneshot(qemu_get_aio_context(),
926 block_job_defer_to_main_loop_bh
, data
);