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 static void block_job_drained_begin(void *opaque
)
73 BlockJob
*job
= opaque
;
77 static void block_job_drained_end(void *opaque
)
79 BlockJob
*job
= opaque
;
80 block_job_resume(job
);
83 static const BlockDevOps block_job_dev_ops
= {
84 .drained_begin
= block_job_drained_begin
,
85 .drained_end
= block_job_drained_end
,
88 BlockJob
*block_job_next(BlockJob
*job
)
91 return QLIST_FIRST(&block_jobs
);
93 return QLIST_NEXT(job
, job_list
);
96 BlockJob
*block_job_get(const char *id
)
100 QLIST_FOREACH(job
, &block_jobs
, job_list
) {
101 if (job
->id
&& !strcmp(id
, job
->id
)) {
109 static void block_job_attached_aio_context(AioContext
*new_context
,
112 BlockJob
*job
= opaque
;
114 if (job
->driver
->attached_aio_context
) {
115 job
->driver
->attached_aio_context(job
, new_context
);
118 block_job_resume(job
);
121 static void block_job_drain(BlockJob
*job
)
123 /* If job is !job->busy this kicks it into the next pause point. */
124 block_job_enter(job
);
127 if (job
->driver
->drain
) {
128 job
->driver
->drain(job
);
132 static void block_job_detach_aio_context(void *opaque
)
134 BlockJob
*job
= opaque
;
136 /* In case the job terminates during aio_poll()... */
139 block_job_pause(job
);
141 while (!job
->paused
&& !job
->completed
) {
142 block_job_drain(job
);
145 block_job_unref(job
);
148 void block_job_remove_all_bdrv(BlockJob
*job
)
151 for (l
= job
->nodes
; l
; l
= l
->next
) {
152 BdrvChild
*c
= l
->data
;
153 bdrv_op_unblock_all(c
->bs
, job
->blocker
);
154 bdrv_root_unref_child(c
);
156 g_slist_free(job
->nodes
);
160 int block_job_add_bdrv(BlockJob
*job
, const char *name
, BlockDriverState
*bs
,
161 uint64_t perm
, uint64_t shared_perm
, Error
**errp
)
165 c
= bdrv_root_attach_child(bs
, name
, &child_job
, perm
, shared_perm
,
171 job
->nodes
= g_slist_prepend(job
->nodes
, c
);
173 bdrv_op_block_all(bs
, job
->blocker
);
178 void *block_job_create(const char *job_id
, const BlockJobDriver
*driver
,
179 BlockDriverState
*bs
, uint64_t perm
,
180 uint64_t shared_perm
, int64_t speed
, int flags
,
181 BlockCompletionFunc
*cb
, void *opaque
, Error
**errp
)
188 error_setg(errp
, QERR_DEVICE_IN_USE
, bdrv_get_device_name(bs
));
192 if (job_id
== NULL
&& !(flags
& BLOCK_JOB_INTERNAL
)) {
193 job_id
= bdrv_get_device_name(bs
);
195 error_setg(errp
, "An explicit job ID is required for this node");
201 if (flags
& BLOCK_JOB_INTERNAL
) {
202 error_setg(errp
, "Cannot specify job ID for internal block job");
206 if (!id_wellformed(job_id
)) {
207 error_setg(errp
, "Invalid job ID '%s'", job_id
);
211 if (block_job_get(job_id
)) {
212 error_setg(errp
, "Job ID '%s' already in use", job_id
);
217 blk
= blk_new(perm
, shared_perm
);
218 ret
= blk_insert_bs(blk
, bs
, errp
);
224 job
= g_malloc0(driver
->instance_size
);
225 job
->driver
= driver
;
226 job
->id
= g_strdup(job_id
);
229 job
->opaque
= opaque
;
232 job
->pause_count
= 1;
235 error_setg(&job
->blocker
, "block device is in use by block job: %s",
236 BlockJobType_lookup
[driver
->job_type
]);
237 block_job_add_bdrv(job
, "main node", bs
, 0, BLK_PERM_ALL
, &error_abort
);
240 blk_set_dev_ops(blk
, &block_job_dev_ops
, job
);
241 bdrv_op_unblock(bs
, BLOCK_OP_TYPE_DATAPLANE
, job
->blocker
);
243 QLIST_INSERT_HEAD(&block_jobs
, job
, job_list
);
245 blk_add_aio_context_notifier(blk
, block_job_attached_aio_context
,
246 block_job_detach_aio_context
, job
);
248 /* Only set speed when necessary to avoid NotSupported error */
250 Error
*local_err
= NULL
;
252 block_job_set_speed(job
, speed
, &local_err
);
254 block_job_unref(job
);
255 error_propagate(errp
, local_err
);
262 bool block_job_is_internal(BlockJob
*job
)
264 return (job
->id
== NULL
);
267 static bool block_job_started(BlockJob
*job
)
273 * All jobs must allow a pause point before entering their job proper. This
274 * ensures that jobs can be paused prior to being started, then resumed later.
276 static void coroutine_fn
block_job_co_entry(void *opaque
)
278 BlockJob
*job
= opaque
;
280 assert(job
&& job
->driver
&& job
->driver
->start
);
281 block_job_pause_point(job
);
282 job
->driver
->start(job
);
285 void block_job_start(BlockJob
*job
)
287 assert(job
&& !block_job_started(job
) && job
->paused
&&
288 job
->driver
&& job
->driver
->start
);
289 job
->co
= qemu_coroutine_create(block_job_co_entry
, job
);
293 qemu_coroutine_enter(job
->co
);
296 void block_job_ref(BlockJob
*job
)
301 void block_job_unref(BlockJob
*job
)
303 if (--job
->refcnt
== 0) {
304 BlockDriverState
*bs
= blk_bs(job
->blk
);
306 block_job_remove_all_bdrv(job
);
307 blk_remove_aio_context_notifier(job
->blk
,
308 block_job_attached_aio_context
,
309 block_job_detach_aio_context
, job
);
311 error_free(job
->blocker
);
313 QLIST_REMOVE(job
, job_list
);
318 static void block_job_completed_single(BlockJob
*job
)
321 if (job
->driver
->commit
) {
322 job
->driver
->commit(job
);
325 if (job
->driver
->abort
) {
326 job
->driver
->abort(job
);
329 if (job
->driver
->clean
) {
330 job
->driver
->clean(job
);
334 job
->cb(job
->opaque
, job
->ret
);
337 /* Emit events only if we actually started */
338 if (block_job_started(job
)) {
339 if (block_job_is_cancelled(job
)) {
340 block_job_event_cancelled(job
);
342 const char *msg
= NULL
;
344 msg
= strerror(-job
->ret
);
346 block_job_event_completed(job
, msg
);
351 QLIST_REMOVE(job
, txn_list
);
352 block_job_txn_unref(job
->txn
);
354 block_job_unref(job
);
357 static void block_job_completed_txn_abort(BlockJob
*job
)
360 BlockJobTxn
*txn
= job
->txn
;
361 BlockJob
*other_job
, *next
;
365 * We are cancelled by another job, which will handle everything.
369 txn
->aborting
= true;
370 /* We are the first failed job. Cancel other jobs. */
371 QLIST_FOREACH(other_job
, &txn
->jobs
, txn_list
) {
372 ctx
= blk_get_aio_context(other_job
->blk
);
373 aio_context_acquire(ctx
);
375 QLIST_FOREACH(other_job
, &txn
->jobs
, txn_list
) {
376 if (other_job
== job
|| other_job
->completed
) {
377 /* Other jobs are "effectively" cancelled by us, set the status for
378 * them; this job, however, may or may not be cancelled, depending
379 * on the caller, so leave it. */
380 if (other_job
!= job
) {
381 other_job
->cancelled
= true;
385 block_job_cancel_sync(other_job
);
386 assert(other_job
->completed
);
388 QLIST_FOREACH_SAFE(other_job
, &txn
->jobs
, txn_list
, next
) {
389 ctx
= blk_get_aio_context(other_job
->blk
);
390 block_job_completed_single(other_job
);
391 aio_context_release(ctx
);
395 static void block_job_completed_txn_success(BlockJob
*job
)
398 BlockJobTxn
*txn
= job
->txn
;
399 BlockJob
*other_job
, *next
;
401 * Successful completion, see if there are other running jobs in this
404 QLIST_FOREACH(other_job
, &txn
->jobs
, txn_list
) {
405 if (!other_job
->completed
) {
409 /* We are the last completed job, commit the transaction. */
410 QLIST_FOREACH_SAFE(other_job
, &txn
->jobs
, txn_list
, next
) {
411 ctx
= blk_get_aio_context(other_job
->blk
);
412 aio_context_acquire(ctx
);
413 assert(other_job
->ret
== 0);
414 block_job_completed_single(other_job
);
415 aio_context_release(ctx
);
419 void block_job_completed(BlockJob
*job
, int ret
)
421 assert(blk_bs(job
->blk
)->job
== job
);
422 assert(!job
->completed
);
423 job
->completed
= true;
426 block_job_completed_single(job
);
427 } else if (ret
< 0 || block_job_is_cancelled(job
)) {
428 block_job_completed_txn_abort(job
);
430 block_job_completed_txn_success(job
);
434 void block_job_set_speed(BlockJob
*job
, int64_t speed
, Error
**errp
)
436 Error
*local_err
= NULL
;
438 if (!job
->driver
->set_speed
) {
439 error_setg(errp
, QERR_UNSUPPORTED
);
442 job
->driver
->set_speed(job
, speed
, &local_err
);
444 error_propagate(errp
, local_err
);
451 void block_job_complete(BlockJob
*job
, Error
**errp
)
453 /* Should not be reachable via external interface for internal jobs */
455 if (job
->pause_count
|| job
->cancelled
||
456 !block_job_started(job
) || !job
->driver
->complete
) {
457 error_setg(errp
, "The active block job '%s' cannot be completed",
462 job
->driver
->complete(job
, errp
);
465 void block_job_pause(BlockJob
*job
)
470 void block_job_user_pause(BlockJob
*job
)
472 job
->user_paused
= true;
473 block_job_pause(job
);
476 static bool block_job_should_pause(BlockJob
*job
)
478 return job
->pause_count
> 0;
481 bool block_job_user_paused(BlockJob
*job
)
483 return job
? job
->user_paused
: 0;
486 void coroutine_fn
block_job_pause_point(BlockJob
*job
)
488 assert(job
&& block_job_started(job
));
490 if (!block_job_should_pause(job
)) {
493 if (block_job_is_cancelled(job
)) {
497 if (job
->driver
->pause
) {
498 job
->driver
->pause(job
);
501 if (block_job_should_pause(job
) && !block_job_is_cancelled(job
)) {
504 qemu_coroutine_yield(); /* wait for block_job_resume() */
509 if (job
->driver
->resume
) {
510 job
->driver
->resume(job
);
514 void block_job_resume(BlockJob
*job
)
516 assert(job
->pause_count
> 0);
518 if (job
->pause_count
) {
521 block_job_enter(job
);
524 void block_job_user_resume(BlockJob
*job
)
526 if (job
&& job
->user_paused
&& job
->pause_count
> 0) {
527 job
->user_paused
= false;
528 block_job_resume(job
);
532 void block_job_enter(BlockJob
*job
)
534 if (job
->co
&& !job
->busy
) {
535 qemu_coroutine_enter(job
->co
);
539 void block_job_cancel(BlockJob
*job
)
541 if (block_job_started(job
)) {
542 job
->cancelled
= true;
543 block_job_iostatus_reset(job
);
544 block_job_enter(job
);
546 block_job_completed(job
, -ECANCELED
);
550 bool block_job_is_cancelled(BlockJob
*job
)
552 return job
->cancelled
;
555 void block_job_iostatus_reset(BlockJob
*job
)
557 job
->iostatus
= BLOCK_DEVICE_IO_STATUS_OK
;
558 if (job
->driver
->iostatus_reset
) {
559 job
->driver
->iostatus_reset(job
);
563 static int block_job_finish_sync(BlockJob
*job
,
564 void (*finish
)(BlockJob
*, Error
**errp
),
567 Error
*local_err
= NULL
;
570 assert(blk_bs(job
->blk
)->job
== job
);
574 finish(job
, &local_err
);
576 error_propagate(errp
, local_err
);
577 block_job_unref(job
);
580 /* block_job_drain calls block_job_enter, and it should be enough to
581 * induce progress until the job completes or moves to the main thread.
583 while (!job
->deferred_to_main_loop
&& !job
->completed
) {
584 block_job_drain(job
);
586 while (!job
->completed
) {
587 aio_poll(qemu_get_aio_context(), true);
589 ret
= (job
->cancelled
&& job
->ret
== 0) ? -ECANCELED
: job
->ret
;
590 block_job_unref(job
);
594 /* A wrapper around block_job_cancel() taking an Error ** parameter so it may be
595 * used with block_job_finish_sync() without the need for (rather nasty)
596 * function pointer casts there. */
597 static void block_job_cancel_err(BlockJob
*job
, Error
**errp
)
599 block_job_cancel(job
);
602 int block_job_cancel_sync(BlockJob
*job
)
604 return block_job_finish_sync(job
, &block_job_cancel_err
, NULL
);
607 void block_job_cancel_sync_all(void)
610 AioContext
*aio_context
;
612 while ((job
= QLIST_FIRST(&block_jobs
))) {
613 aio_context
= blk_get_aio_context(job
->blk
);
614 aio_context_acquire(aio_context
);
615 block_job_cancel_sync(job
);
616 aio_context_release(aio_context
);
620 int block_job_complete_sync(BlockJob
*job
, Error
**errp
)
622 return block_job_finish_sync(job
, &block_job_complete
, errp
);
625 void block_job_sleep_ns(BlockJob
*job
, QEMUClockType type
, int64_t ns
)
629 /* Check cancellation *before* setting busy = false, too! */
630 if (block_job_is_cancelled(job
)) {
635 if (!block_job_should_pause(job
)) {
636 co_aio_sleep_ns(blk_get_aio_context(job
->blk
), type
, ns
);
640 block_job_pause_point(job
);
643 void block_job_yield(BlockJob
*job
)
647 /* Check cancellation *before* setting busy = false, too! */
648 if (block_job_is_cancelled(job
)) {
653 if (!block_job_should_pause(job
)) {
654 qemu_coroutine_yield();
658 block_job_pause_point(job
);
661 BlockJobInfo
*block_job_query(BlockJob
*job
, Error
**errp
)
665 if (block_job_is_internal(job
)) {
666 error_setg(errp
, "Cannot query QEMU internal jobs");
669 info
= g_new0(BlockJobInfo
, 1);
670 info
->type
= g_strdup(BlockJobType_lookup
[job
->driver
->job_type
]);
671 info
->device
= g_strdup(job
->id
);
672 info
->len
= job
->len
;
673 info
->busy
= job
->busy
;
674 info
->paused
= job
->pause_count
> 0;
675 info
->offset
= job
->offset
;
676 info
->speed
= job
->speed
;
677 info
->io_status
= job
->iostatus
;
678 info
->ready
= job
->ready
;
682 static void block_job_iostatus_set_err(BlockJob
*job
, int error
)
684 if (job
->iostatus
== BLOCK_DEVICE_IO_STATUS_OK
) {
685 job
->iostatus
= error
== ENOSPC
? BLOCK_DEVICE_IO_STATUS_NOSPACE
:
686 BLOCK_DEVICE_IO_STATUS_FAILED
;
690 static void block_job_event_cancelled(BlockJob
*job
)
692 if (block_job_is_internal(job
)) {
696 qapi_event_send_block_job_cancelled(job
->driver
->job_type
,
704 static void block_job_event_completed(BlockJob
*job
, const char *msg
)
706 if (block_job_is_internal(job
)) {
710 qapi_event_send_block_job_completed(job
->driver
->job_type
,
720 void block_job_event_ready(BlockJob
*job
)
724 if (block_job_is_internal(job
)) {
728 qapi_event_send_block_job_ready(job
->driver
->job_type
,
732 job
->speed
, &error_abort
);
735 BlockErrorAction
block_job_error_action(BlockJob
*job
, BlockdevOnError on_err
,
736 int is_read
, int error
)
738 BlockErrorAction action
;
741 case BLOCKDEV_ON_ERROR_ENOSPC
:
742 case BLOCKDEV_ON_ERROR_AUTO
:
743 action
= (error
== ENOSPC
) ?
744 BLOCK_ERROR_ACTION_STOP
: BLOCK_ERROR_ACTION_REPORT
;
746 case BLOCKDEV_ON_ERROR_STOP
:
747 action
= BLOCK_ERROR_ACTION_STOP
;
749 case BLOCKDEV_ON_ERROR_REPORT
:
750 action
= BLOCK_ERROR_ACTION_REPORT
;
752 case BLOCKDEV_ON_ERROR_IGNORE
:
753 action
= BLOCK_ERROR_ACTION_IGNORE
;
758 if (!block_job_is_internal(job
)) {
759 qapi_event_send_block_job_error(job
->id
,
760 is_read
? IO_OPERATION_TYPE_READ
:
761 IO_OPERATION_TYPE_WRITE
,
762 action
, &error_abort
);
764 if (action
== BLOCK_ERROR_ACTION_STOP
) {
765 /* make the pause user visible, which will be resumed from QMP. */
766 block_job_user_pause(job
);
767 block_job_iostatus_set_err(job
, error
);
774 AioContext
*aio_context
;
775 BlockJobDeferToMainLoopFn
*fn
;
777 } BlockJobDeferToMainLoopData
;
779 static void block_job_defer_to_main_loop_bh(void *opaque
)
781 BlockJobDeferToMainLoopData
*data
= opaque
;
782 AioContext
*aio_context
;
784 /* Prevent race with block_job_defer_to_main_loop() */
785 aio_context_acquire(data
->aio_context
);
787 /* Fetch BDS AioContext again, in case it has changed */
788 aio_context
= blk_get_aio_context(data
->job
->blk
);
789 if (aio_context
!= data
->aio_context
) {
790 aio_context_acquire(aio_context
);
793 data
->job
->deferred_to_main_loop
= false;
794 data
->fn(data
->job
, data
->opaque
);
796 if (aio_context
!= data
->aio_context
) {
797 aio_context_release(aio_context
);
800 aio_context_release(data
->aio_context
);
805 void block_job_defer_to_main_loop(BlockJob
*job
,
806 BlockJobDeferToMainLoopFn
*fn
,
809 BlockJobDeferToMainLoopData
*data
= g_malloc(sizeof(*data
));
811 data
->aio_context
= blk_get_aio_context(job
->blk
);
813 data
->opaque
= opaque
;
814 job
->deferred_to_main_loop
= true;
816 aio_bh_schedule_oneshot(qemu_get_aio_context(),
817 block_job_defer_to_main_loop_bh
, data
);
820 BlockJobTxn
*block_job_txn_new(void)
822 BlockJobTxn
*txn
= g_new0(BlockJobTxn
, 1);
823 QLIST_INIT(&txn
->jobs
);
828 static void block_job_txn_ref(BlockJobTxn
*txn
)
833 void block_job_txn_unref(BlockJobTxn
*txn
)
835 if (txn
&& --txn
->refcnt
== 0) {
840 void block_job_txn_add_job(BlockJobTxn
*txn
, BlockJob
*job
)
849 QLIST_INSERT_HEAD(&txn
->jobs
, job
, txn_list
);
850 block_job_txn_ref(txn
);