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 void block_job_remove_all_bdrv(BlockJob
*job
)
134 for (l
= job
->nodes
; l
; l
= l
->next
) {
135 BdrvChild
*c
= l
->data
;
136 bdrv_op_unblock_all(c
->bs
, job
->blocker
);
137 bdrv_root_unref_child(c
);
139 g_slist_free(job
->nodes
);
143 int block_job_add_bdrv(BlockJob
*job
, const char *name
, BlockDriverState
*bs
,
144 uint64_t perm
, uint64_t shared_perm
, Error
**errp
)
148 c
= bdrv_root_attach_child(bs
, name
, &child_job
, perm
, shared_perm
,
154 job
->nodes
= g_slist_prepend(job
->nodes
, c
);
156 bdrv_op_block_all(bs
, job
->blocker
);
161 void *block_job_create(const char *job_id
, const BlockJobDriver
*driver
,
162 BlockDriverState
*bs
, uint64_t perm
,
163 uint64_t shared_perm
, int64_t speed
, int flags
,
164 BlockCompletionFunc
*cb
, void *opaque
, Error
**errp
)
171 error_setg(errp
, QERR_DEVICE_IN_USE
, bdrv_get_device_name(bs
));
175 if (job_id
== NULL
&& !(flags
& BLOCK_JOB_INTERNAL
)) {
176 job_id
= bdrv_get_device_name(bs
);
178 error_setg(errp
, "An explicit job ID is required for this node");
184 if (flags
& BLOCK_JOB_INTERNAL
) {
185 error_setg(errp
, "Cannot specify job ID for internal block job");
189 if (!id_wellformed(job_id
)) {
190 error_setg(errp
, "Invalid job ID '%s'", job_id
);
194 if (block_job_get(job_id
)) {
195 error_setg(errp
, "Job ID '%s' already in use", job_id
);
200 blk
= blk_new(perm
, shared_perm
);
201 ret
= blk_insert_bs(blk
, bs
, errp
);
207 job
= g_malloc0(driver
->instance_size
);
208 error_setg(&job
->blocker
, "block device is in use by block job: %s",
209 BlockJobType_lookup
[driver
->job_type
]);
210 block_job_add_bdrv(job
, "main node", bs
, 0, BLK_PERM_ALL
, &error_abort
);
211 bdrv_op_unblock(bs
, BLOCK_OP_TYPE_DATAPLANE
, job
->blocker
);
213 job
->driver
= driver
;
214 job
->id
= g_strdup(job_id
);
217 job
->opaque
= opaque
;
220 job
->pause_count
= 1;
224 QLIST_INSERT_HEAD(&block_jobs
, job
, job_list
);
226 blk_add_aio_context_notifier(blk
, block_job_attached_aio_context
,
227 block_job_detach_aio_context
, job
);
229 /* Only set speed when necessary to avoid NotSupported error */
231 Error
*local_err
= NULL
;
233 block_job_set_speed(job
, speed
, &local_err
);
235 block_job_unref(job
);
236 error_propagate(errp
, local_err
);
243 bool block_job_is_internal(BlockJob
*job
)
245 return (job
->id
== NULL
);
248 static bool block_job_started(BlockJob
*job
)
253 void block_job_start(BlockJob
*job
)
255 assert(job
&& !block_job_started(job
) && job
->paused
&&
256 !job
->busy
&& job
->driver
->start
);
257 job
->co
= qemu_coroutine_create(job
->driver
->start
, job
);
258 if (--job
->pause_count
== 0) {
261 qemu_coroutine_enter(job
->co
);
265 void block_job_ref(BlockJob
*job
)
270 void block_job_unref(BlockJob
*job
)
272 if (--job
->refcnt
== 0) {
273 BlockDriverState
*bs
= blk_bs(job
->blk
);
275 block_job_remove_all_bdrv(job
);
276 blk_remove_aio_context_notifier(job
->blk
,
277 block_job_attached_aio_context
,
278 block_job_detach_aio_context
, job
);
280 error_free(job
->blocker
);
282 QLIST_REMOVE(job
, job_list
);
287 static void block_job_completed_single(BlockJob
*job
)
290 if (job
->driver
->commit
) {
291 job
->driver
->commit(job
);
294 if (job
->driver
->abort
) {
295 job
->driver
->abort(job
);
298 if (job
->driver
->clean
) {
299 job
->driver
->clean(job
);
303 job
->cb(job
->opaque
, job
->ret
);
306 /* Emit events only if we actually started */
307 if (block_job_started(job
)) {
308 if (block_job_is_cancelled(job
)) {
309 block_job_event_cancelled(job
);
311 const char *msg
= NULL
;
313 msg
= strerror(-job
->ret
);
315 block_job_event_completed(job
, msg
);
320 QLIST_REMOVE(job
, txn_list
);
321 block_job_txn_unref(job
->txn
);
323 block_job_unref(job
);
326 static void block_job_completed_txn_abort(BlockJob
*job
)
329 BlockJobTxn
*txn
= job
->txn
;
330 BlockJob
*other_job
, *next
;
334 * We are cancelled by another job, which will handle everything.
338 txn
->aborting
= true;
339 /* We are the first failed job. Cancel other jobs. */
340 QLIST_FOREACH(other_job
, &txn
->jobs
, txn_list
) {
341 ctx
= blk_get_aio_context(other_job
->blk
);
342 aio_context_acquire(ctx
);
344 QLIST_FOREACH(other_job
, &txn
->jobs
, txn_list
) {
345 if (other_job
== job
|| other_job
->completed
) {
346 /* Other jobs are "effectively" cancelled by us, set the status for
347 * them; this job, however, may or may not be cancelled, depending
348 * on the caller, so leave it. */
349 if (other_job
!= job
) {
350 other_job
->cancelled
= true;
354 block_job_cancel_sync(other_job
);
355 assert(other_job
->completed
);
357 QLIST_FOREACH_SAFE(other_job
, &txn
->jobs
, txn_list
, next
) {
358 ctx
= blk_get_aio_context(other_job
->blk
);
359 block_job_completed_single(other_job
);
360 aio_context_release(ctx
);
364 static void block_job_completed_txn_success(BlockJob
*job
)
367 BlockJobTxn
*txn
= job
->txn
;
368 BlockJob
*other_job
, *next
;
370 * Successful completion, see if there are other running jobs in this
373 QLIST_FOREACH(other_job
, &txn
->jobs
, txn_list
) {
374 if (!other_job
->completed
) {
378 /* We are the last completed job, commit the transaction. */
379 QLIST_FOREACH_SAFE(other_job
, &txn
->jobs
, txn_list
, next
) {
380 ctx
= blk_get_aio_context(other_job
->blk
);
381 aio_context_acquire(ctx
);
382 assert(other_job
->ret
== 0);
383 block_job_completed_single(other_job
);
384 aio_context_release(ctx
);
388 void block_job_completed(BlockJob
*job
, int ret
)
390 assert(blk_bs(job
->blk
)->job
== job
);
391 assert(!job
->completed
);
392 job
->completed
= true;
395 block_job_completed_single(job
);
396 } else if (ret
< 0 || block_job_is_cancelled(job
)) {
397 block_job_completed_txn_abort(job
);
399 block_job_completed_txn_success(job
);
403 void block_job_set_speed(BlockJob
*job
, int64_t speed
, Error
**errp
)
405 Error
*local_err
= NULL
;
407 if (!job
->driver
->set_speed
) {
408 error_setg(errp
, QERR_UNSUPPORTED
);
411 job
->driver
->set_speed(job
, speed
, &local_err
);
413 error_propagate(errp
, local_err
);
420 void block_job_complete(BlockJob
*job
, Error
**errp
)
422 /* Should not be reachable via external interface for internal jobs */
424 if (job
->pause_count
|| job
->cancelled
||
425 !block_job_started(job
) || !job
->driver
->complete
) {
426 error_setg(errp
, "The active block job '%s' cannot be completed",
431 job
->driver
->complete(job
, errp
);
434 void block_job_pause(BlockJob
*job
)
439 void block_job_user_pause(BlockJob
*job
)
441 job
->user_paused
= true;
442 block_job_pause(job
);
445 static bool block_job_should_pause(BlockJob
*job
)
447 return job
->pause_count
> 0;
450 bool block_job_user_paused(BlockJob
*job
)
452 return job
? job
->user_paused
: 0;
455 void coroutine_fn
block_job_pause_point(BlockJob
*job
)
457 assert(job
&& block_job_started(job
));
459 if (!block_job_should_pause(job
)) {
462 if (block_job_is_cancelled(job
)) {
466 if (job
->driver
->pause
) {
467 job
->driver
->pause(job
);
470 if (block_job_should_pause(job
) && !block_job_is_cancelled(job
)) {
473 qemu_coroutine_yield(); /* wait for block_job_resume() */
478 if (job
->driver
->resume
) {
479 job
->driver
->resume(job
);
483 void block_job_resume(BlockJob
*job
)
485 assert(job
->pause_count
> 0);
487 if (job
->pause_count
) {
490 block_job_enter(job
);
493 void block_job_user_resume(BlockJob
*job
)
495 if (job
&& job
->user_paused
&& job
->pause_count
> 0) {
496 job
->user_paused
= false;
497 block_job_resume(job
);
501 void block_job_enter(BlockJob
*job
)
503 if (job
->co
&& !job
->busy
) {
504 qemu_coroutine_enter(job
->co
);
508 void block_job_cancel(BlockJob
*job
)
510 if (block_job_started(job
)) {
511 job
->cancelled
= true;
512 block_job_iostatus_reset(job
);
513 block_job_enter(job
);
515 block_job_completed(job
, -ECANCELED
);
519 bool block_job_is_cancelled(BlockJob
*job
)
521 return job
->cancelled
;
524 void block_job_iostatus_reset(BlockJob
*job
)
526 job
->iostatus
= BLOCK_DEVICE_IO_STATUS_OK
;
527 if (job
->driver
->iostatus_reset
) {
528 job
->driver
->iostatus_reset(job
);
532 static int block_job_finish_sync(BlockJob
*job
,
533 void (*finish
)(BlockJob
*, Error
**errp
),
536 Error
*local_err
= NULL
;
539 assert(blk_bs(job
->blk
)->job
== job
);
543 finish(job
, &local_err
);
545 error_propagate(errp
, local_err
);
546 block_job_unref(job
);
549 /* block_job_drain calls block_job_enter, and it should be enough to
550 * induce progress until the job completes or moves to the main thread.
552 while (!job
->deferred_to_main_loop
&& !job
->completed
) {
553 block_job_drain(job
);
555 while (!job
->completed
) {
556 aio_poll(qemu_get_aio_context(), true);
558 ret
= (job
->cancelled
&& job
->ret
== 0) ? -ECANCELED
: job
->ret
;
559 block_job_unref(job
);
563 /* A wrapper around block_job_cancel() taking an Error ** parameter so it may be
564 * used with block_job_finish_sync() without the need for (rather nasty)
565 * function pointer casts there. */
566 static void block_job_cancel_err(BlockJob
*job
, Error
**errp
)
568 block_job_cancel(job
);
571 int block_job_cancel_sync(BlockJob
*job
)
573 return block_job_finish_sync(job
, &block_job_cancel_err
, NULL
);
576 void block_job_cancel_sync_all(void)
579 AioContext
*aio_context
;
581 while ((job
= QLIST_FIRST(&block_jobs
))) {
582 aio_context
= blk_get_aio_context(job
->blk
);
583 aio_context_acquire(aio_context
);
584 block_job_cancel_sync(job
);
585 aio_context_release(aio_context
);
589 int block_job_complete_sync(BlockJob
*job
, Error
**errp
)
591 return block_job_finish_sync(job
, &block_job_complete
, errp
);
594 void block_job_sleep_ns(BlockJob
*job
, QEMUClockType type
, int64_t ns
)
598 /* Check cancellation *before* setting busy = false, too! */
599 if (block_job_is_cancelled(job
)) {
604 if (!block_job_should_pause(job
)) {
605 co_aio_sleep_ns(blk_get_aio_context(job
->blk
), type
, ns
);
609 block_job_pause_point(job
);
612 void block_job_yield(BlockJob
*job
)
616 /* Check cancellation *before* setting busy = false, too! */
617 if (block_job_is_cancelled(job
)) {
622 if (!block_job_should_pause(job
)) {
623 qemu_coroutine_yield();
627 block_job_pause_point(job
);
630 BlockJobInfo
*block_job_query(BlockJob
*job
, Error
**errp
)
634 if (block_job_is_internal(job
)) {
635 error_setg(errp
, "Cannot query QEMU internal jobs");
638 info
= g_new0(BlockJobInfo
, 1);
639 info
->type
= g_strdup(BlockJobType_lookup
[job
->driver
->job_type
]);
640 info
->device
= g_strdup(job
->id
);
641 info
->len
= job
->len
;
642 info
->busy
= job
->busy
;
643 info
->paused
= job
->pause_count
> 0;
644 info
->offset
= job
->offset
;
645 info
->speed
= job
->speed
;
646 info
->io_status
= job
->iostatus
;
647 info
->ready
= job
->ready
;
651 static void block_job_iostatus_set_err(BlockJob
*job
, int error
)
653 if (job
->iostatus
== BLOCK_DEVICE_IO_STATUS_OK
) {
654 job
->iostatus
= error
== ENOSPC
? BLOCK_DEVICE_IO_STATUS_NOSPACE
:
655 BLOCK_DEVICE_IO_STATUS_FAILED
;
659 static void block_job_event_cancelled(BlockJob
*job
)
661 if (block_job_is_internal(job
)) {
665 qapi_event_send_block_job_cancelled(job
->driver
->job_type
,
673 static void block_job_event_completed(BlockJob
*job
, const char *msg
)
675 if (block_job_is_internal(job
)) {
679 qapi_event_send_block_job_completed(job
->driver
->job_type
,
689 void block_job_event_ready(BlockJob
*job
)
693 if (block_job_is_internal(job
)) {
697 qapi_event_send_block_job_ready(job
->driver
->job_type
,
701 job
->speed
, &error_abort
);
704 BlockErrorAction
block_job_error_action(BlockJob
*job
, BlockdevOnError on_err
,
705 int is_read
, int error
)
707 BlockErrorAction action
;
710 case BLOCKDEV_ON_ERROR_ENOSPC
:
711 case BLOCKDEV_ON_ERROR_AUTO
:
712 action
= (error
== ENOSPC
) ?
713 BLOCK_ERROR_ACTION_STOP
: BLOCK_ERROR_ACTION_REPORT
;
715 case BLOCKDEV_ON_ERROR_STOP
:
716 action
= BLOCK_ERROR_ACTION_STOP
;
718 case BLOCKDEV_ON_ERROR_REPORT
:
719 action
= BLOCK_ERROR_ACTION_REPORT
;
721 case BLOCKDEV_ON_ERROR_IGNORE
:
722 action
= BLOCK_ERROR_ACTION_IGNORE
;
727 if (!block_job_is_internal(job
)) {
728 qapi_event_send_block_job_error(job
->id
,
729 is_read
? IO_OPERATION_TYPE_READ
:
730 IO_OPERATION_TYPE_WRITE
,
731 action
, &error_abort
);
733 if (action
== BLOCK_ERROR_ACTION_STOP
) {
734 /* make the pause user visible, which will be resumed from QMP. */
735 block_job_user_pause(job
);
736 block_job_iostatus_set_err(job
, error
);
743 AioContext
*aio_context
;
744 BlockJobDeferToMainLoopFn
*fn
;
746 } BlockJobDeferToMainLoopData
;
748 static void block_job_defer_to_main_loop_bh(void *opaque
)
750 BlockJobDeferToMainLoopData
*data
= opaque
;
751 AioContext
*aio_context
;
753 /* Prevent race with block_job_defer_to_main_loop() */
754 aio_context_acquire(data
->aio_context
);
756 /* Fetch BDS AioContext again, in case it has changed */
757 aio_context
= blk_get_aio_context(data
->job
->blk
);
758 aio_context_acquire(aio_context
);
760 data
->job
->deferred_to_main_loop
= false;
761 data
->fn(data
->job
, data
->opaque
);
763 aio_context_release(aio_context
);
765 aio_context_release(data
->aio_context
);
770 void block_job_defer_to_main_loop(BlockJob
*job
,
771 BlockJobDeferToMainLoopFn
*fn
,
774 BlockJobDeferToMainLoopData
*data
= g_malloc(sizeof(*data
));
776 data
->aio_context
= blk_get_aio_context(job
->blk
);
778 data
->opaque
= opaque
;
779 job
->deferred_to_main_loop
= true;
781 aio_bh_schedule_oneshot(qemu_get_aio_context(),
782 block_job_defer_to_main_loop_bh
, data
);
785 BlockJobTxn
*block_job_txn_new(void)
787 BlockJobTxn
*txn
= g_new0(BlockJobTxn
, 1);
788 QLIST_INIT(&txn
->jobs
);
793 static void block_job_txn_ref(BlockJobTxn
*txn
)
798 void block_job_txn_unref(BlockJobTxn
*txn
)
800 if (txn
&& --txn
->refcnt
== 0) {
805 void block_job_txn_add_job(BlockJobTxn
*txn
, BlockJob
*job
)
814 QLIST_INSERT_HEAD(&txn
->jobs
, job
, txn_list
);
815 block_job_txn_ref(txn
);