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"
29 #include "block/block.h"
30 #include "block/blockjob_int.h"
31 #include "block/block_int.h"
32 #include "sysemu/block-backend.h"
33 #include "qapi/qmp/qerror.h"
34 #include "qapi/qmp/qjson.h"
35 #include "qemu/coroutine.h"
37 #include "qmp-commands.h"
38 #include "qemu/timer.h"
39 #include "qapi-event.h"
41 static void block_job_event_cancelled(BlockJob
*job
);
42 static void block_job_event_completed(BlockJob
*job
, const char *msg
);
44 /* Transactional group of block jobs */
47 /* Is this txn being cancelled? */
51 QLIST_HEAD(, BlockJob
) jobs
;
57 static QLIST_HEAD(, BlockJob
) block_jobs
= QLIST_HEAD_INITIALIZER(block_jobs
);
59 BlockJob
*block_job_next(BlockJob
*job
)
62 return QLIST_FIRST(&block_jobs
);
64 return QLIST_NEXT(job
, job_list
);
67 BlockJob
*block_job_get(const char *id
)
71 QLIST_FOREACH(job
, &block_jobs
, job_list
) {
72 if (job
->id
&& !strcmp(id
, job
->id
)) {
80 static void block_job_attached_aio_context(AioContext
*new_context
,
83 BlockJob
*job
= opaque
;
85 if (job
->driver
->attached_aio_context
) {
86 job
->driver
->attached_aio_context(job
, new_context
);
89 block_job_resume(job
);
92 static void block_job_drain(BlockJob
*job
)
94 /* If job is !job->busy this kicks it into the next pause point. */
98 if (job
->driver
->drain
) {
99 job
->driver
->drain(job
);
103 static void block_job_detach_aio_context(void *opaque
)
105 BlockJob
*job
= opaque
;
107 /* In case the job terminates during aio_poll()... */
110 block_job_pause(job
);
112 while (!job
->paused
&& !job
->completed
) {
113 block_job_drain(job
);
116 block_job_unref(job
);
119 void block_job_add_bdrv(BlockJob
*job
, BlockDriverState
*bs
)
121 job
->nodes
= g_slist_prepend(job
->nodes
, bs
);
123 bdrv_op_block_all(bs
, job
->blocker
);
126 void *block_job_create(const char *job_id
, const BlockJobDriver
*driver
,
127 BlockDriverState
*bs
, int64_t speed
, int flags
,
128 BlockCompletionFunc
*cb
, void *opaque
, Error
**errp
)
134 error_setg(errp
, QERR_DEVICE_IN_USE
, bdrv_get_device_name(bs
));
138 if (job_id
== NULL
&& !(flags
& BLOCK_JOB_INTERNAL
)) {
139 job_id
= bdrv_get_device_name(bs
);
141 error_setg(errp
, "An explicit job ID is required for this node");
147 if (flags
& BLOCK_JOB_INTERNAL
) {
148 error_setg(errp
, "Cannot specify job ID for internal block job");
152 if (!id_wellformed(job_id
)) {
153 error_setg(errp
, "Invalid job ID '%s'", job_id
);
157 if (block_job_get(job_id
)) {
158 error_setg(errp
, "Job ID '%s' already in use", job_id
);
164 blk_insert_bs(blk
, bs
);
166 job
= g_malloc0(driver
->instance_size
);
167 error_setg(&job
->blocker
, "block device is in use by block job: %s",
168 BlockJobType_lookup
[driver
->job_type
]);
169 block_job_add_bdrv(job
, bs
);
170 bdrv_op_unblock(bs
, BLOCK_OP_TYPE_DATAPLANE
, job
->blocker
);
172 job
->driver
= driver
;
173 job
->id
= g_strdup(job_id
);
176 job
->opaque
= opaque
;
179 job
->pause_count
= 1;
183 QLIST_INSERT_HEAD(&block_jobs
, job
, job_list
);
185 blk_add_aio_context_notifier(blk
, block_job_attached_aio_context
,
186 block_job_detach_aio_context
, job
);
188 /* Only set speed when necessary to avoid NotSupported error */
190 Error
*local_err
= NULL
;
192 block_job_set_speed(job
, speed
, &local_err
);
194 block_job_unref(job
);
195 error_propagate(errp
, local_err
);
202 bool block_job_is_internal(BlockJob
*job
)
204 return (job
->id
== NULL
);
207 static bool block_job_started(BlockJob
*job
)
212 void block_job_start(BlockJob
*job
)
214 assert(job
&& !block_job_started(job
) && job
->paused
&&
215 !job
->busy
&& job
->driver
->start
);
216 job
->co
= qemu_coroutine_create(job
->driver
->start
, job
);
217 if (--job
->pause_count
== 0) {
220 qemu_coroutine_enter(job
->co
);
224 void block_job_ref(BlockJob
*job
)
229 void block_job_unref(BlockJob
*job
)
231 if (--job
->refcnt
== 0) {
233 BlockDriverState
*bs
= blk_bs(job
->blk
);
235 for (l
= job
->nodes
; l
; l
= l
->next
) {
237 bdrv_op_unblock_all(bs
, job
->blocker
);
240 g_slist_free(job
->nodes
);
241 blk_remove_aio_context_notifier(job
->blk
,
242 block_job_attached_aio_context
,
243 block_job_detach_aio_context
, job
);
245 error_free(job
->blocker
);
247 QLIST_REMOVE(job
, job_list
);
252 static void block_job_completed_single(BlockJob
*job
)
255 if (job
->driver
->commit
) {
256 job
->driver
->commit(job
);
259 if (job
->driver
->abort
) {
260 job
->driver
->abort(job
);
263 if (job
->driver
->clean
) {
264 job
->driver
->clean(job
);
268 job
->cb(job
->opaque
, job
->ret
);
271 /* Emit events only if we actually started */
272 if (block_job_started(job
)) {
273 if (block_job_is_cancelled(job
)) {
274 block_job_event_cancelled(job
);
276 const char *msg
= NULL
;
278 msg
= strerror(-job
->ret
);
280 block_job_event_completed(job
, msg
);
285 QLIST_REMOVE(job
, txn_list
);
286 block_job_txn_unref(job
->txn
);
288 block_job_unref(job
);
291 static void block_job_completed_txn_abort(BlockJob
*job
)
294 BlockJobTxn
*txn
= job
->txn
;
295 BlockJob
*other_job
, *next
;
299 * We are cancelled by another job, which will handle everything.
303 txn
->aborting
= true;
304 /* We are the first failed job. Cancel other jobs. */
305 QLIST_FOREACH(other_job
, &txn
->jobs
, txn_list
) {
306 ctx
= blk_get_aio_context(other_job
->blk
);
307 aio_context_acquire(ctx
);
309 QLIST_FOREACH(other_job
, &txn
->jobs
, txn_list
) {
310 if (other_job
== job
|| other_job
->completed
) {
311 /* Other jobs are "effectively" cancelled by us, set the status for
312 * them; this job, however, may or may not be cancelled, depending
313 * on the caller, so leave it. */
314 if (other_job
!= job
) {
315 other_job
->cancelled
= true;
319 block_job_cancel_sync(other_job
);
320 assert(other_job
->completed
);
322 QLIST_FOREACH_SAFE(other_job
, &txn
->jobs
, txn_list
, next
) {
323 ctx
= blk_get_aio_context(other_job
->blk
);
324 block_job_completed_single(other_job
);
325 aio_context_release(ctx
);
329 static void block_job_completed_txn_success(BlockJob
*job
)
332 BlockJobTxn
*txn
= job
->txn
;
333 BlockJob
*other_job
, *next
;
335 * Successful completion, see if there are other running jobs in this
338 QLIST_FOREACH(other_job
, &txn
->jobs
, txn_list
) {
339 if (!other_job
->completed
) {
343 /* We are the last completed job, commit the transaction. */
344 QLIST_FOREACH_SAFE(other_job
, &txn
->jobs
, txn_list
, next
) {
345 ctx
= blk_get_aio_context(other_job
->blk
);
346 aio_context_acquire(ctx
);
347 assert(other_job
->ret
== 0);
348 block_job_completed_single(other_job
);
349 aio_context_release(ctx
);
353 void block_job_completed(BlockJob
*job
, int ret
)
355 assert(blk_bs(job
->blk
)->job
== job
);
356 assert(!job
->completed
);
357 job
->completed
= true;
360 block_job_completed_single(job
);
361 } else if (ret
< 0 || block_job_is_cancelled(job
)) {
362 block_job_completed_txn_abort(job
);
364 block_job_completed_txn_success(job
);
368 void block_job_set_speed(BlockJob
*job
, int64_t speed
, Error
**errp
)
370 Error
*local_err
= NULL
;
372 if (!job
->driver
->set_speed
) {
373 error_setg(errp
, QERR_UNSUPPORTED
);
376 job
->driver
->set_speed(job
, speed
, &local_err
);
378 error_propagate(errp
, local_err
);
385 void block_job_complete(BlockJob
*job
, Error
**errp
)
387 /* Should not be reachable via external interface for internal jobs */
389 if (job
->pause_count
|| job
->cancelled
||
390 !block_job_started(job
) || !job
->driver
->complete
) {
391 error_setg(errp
, "The active block job '%s' cannot be completed",
396 job
->driver
->complete(job
, errp
);
399 void block_job_pause(BlockJob
*job
)
404 void block_job_user_pause(BlockJob
*job
)
406 job
->user_paused
= true;
407 block_job_pause(job
);
410 static bool block_job_should_pause(BlockJob
*job
)
412 return job
->pause_count
> 0;
415 bool block_job_user_paused(BlockJob
*job
)
417 return job
? job
->user_paused
: 0;
420 void coroutine_fn
block_job_pause_point(BlockJob
*job
)
422 assert(job
&& block_job_started(job
));
424 if (!block_job_should_pause(job
)) {
427 if (block_job_is_cancelled(job
)) {
431 if (job
->driver
->pause
) {
432 job
->driver
->pause(job
);
435 if (block_job_should_pause(job
) && !block_job_is_cancelled(job
)) {
438 qemu_coroutine_yield(); /* wait for block_job_resume() */
443 if (job
->driver
->resume
) {
444 job
->driver
->resume(job
);
448 void block_job_resume(BlockJob
*job
)
450 assert(job
->pause_count
> 0);
452 if (job
->pause_count
) {
455 block_job_enter(job
);
458 void block_job_user_resume(BlockJob
*job
)
460 if (job
&& job
->user_paused
&& job
->pause_count
> 0) {
461 job
->user_paused
= false;
462 block_job_resume(job
);
466 void block_job_enter(BlockJob
*job
)
468 if (job
->co
&& !job
->busy
) {
469 qemu_coroutine_enter(job
->co
);
473 void block_job_cancel(BlockJob
*job
)
475 if (block_job_started(job
)) {
476 job
->cancelled
= true;
477 block_job_iostatus_reset(job
);
478 block_job_enter(job
);
480 block_job_completed(job
, -ECANCELED
);
484 bool block_job_is_cancelled(BlockJob
*job
)
486 return job
->cancelled
;
489 void block_job_iostatus_reset(BlockJob
*job
)
491 job
->iostatus
= BLOCK_DEVICE_IO_STATUS_OK
;
492 if (job
->driver
->iostatus_reset
) {
493 job
->driver
->iostatus_reset(job
);
497 static int block_job_finish_sync(BlockJob
*job
,
498 void (*finish
)(BlockJob
*, Error
**errp
),
501 Error
*local_err
= NULL
;
504 assert(blk_bs(job
->blk
)->job
== job
);
508 finish(job
, &local_err
);
510 error_propagate(errp
, local_err
);
511 block_job_unref(job
);
514 /* block_job_drain calls block_job_enter, and it should be enough to
515 * induce progress until the job completes or moves to the main thread.
517 while (!job
->deferred_to_main_loop
&& !job
->completed
) {
518 block_job_drain(job
);
520 while (!job
->completed
) {
521 aio_poll(qemu_get_aio_context(), true);
523 ret
= (job
->cancelled
&& job
->ret
== 0) ? -ECANCELED
: job
->ret
;
524 block_job_unref(job
);
528 /* A wrapper around block_job_cancel() taking an Error ** parameter so it may be
529 * used with block_job_finish_sync() without the need for (rather nasty)
530 * function pointer casts there. */
531 static void block_job_cancel_err(BlockJob
*job
, Error
**errp
)
533 block_job_cancel(job
);
536 int block_job_cancel_sync(BlockJob
*job
)
538 return block_job_finish_sync(job
, &block_job_cancel_err
, NULL
);
541 void block_job_cancel_sync_all(void)
544 AioContext
*aio_context
;
546 while ((job
= QLIST_FIRST(&block_jobs
))) {
547 aio_context
= blk_get_aio_context(job
->blk
);
548 aio_context_acquire(aio_context
);
549 block_job_cancel_sync(job
);
550 aio_context_release(aio_context
);
554 int block_job_complete_sync(BlockJob
*job
, Error
**errp
)
556 return block_job_finish_sync(job
, &block_job_complete
, errp
);
559 void block_job_sleep_ns(BlockJob
*job
, QEMUClockType type
, int64_t ns
)
563 /* Check cancellation *before* setting busy = false, too! */
564 if (block_job_is_cancelled(job
)) {
569 if (!block_job_should_pause(job
)) {
570 co_aio_sleep_ns(blk_get_aio_context(job
->blk
), type
, ns
);
574 block_job_pause_point(job
);
577 void block_job_yield(BlockJob
*job
)
581 /* Check cancellation *before* setting busy = false, too! */
582 if (block_job_is_cancelled(job
)) {
587 if (!block_job_should_pause(job
)) {
588 qemu_coroutine_yield();
592 block_job_pause_point(job
);
595 BlockJobInfo
*block_job_query(BlockJob
*job
, Error
**errp
)
599 if (block_job_is_internal(job
)) {
600 error_setg(errp
, "Cannot query QEMU internal jobs");
603 info
= g_new0(BlockJobInfo
, 1);
604 info
->type
= g_strdup(BlockJobType_lookup
[job
->driver
->job_type
]);
605 info
->device
= g_strdup(job
->id
);
606 info
->len
= job
->len
;
607 info
->busy
= job
->busy
;
608 info
->paused
= job
->pause_count
> 0;
609 info
->offset
= job
->offset
;
610 info
->speed
= job
->speed
;
611 info
->io_status
= job
->iostatus
;
612 info
->ready
= job
->ready
;
616 static void block_job_iostatus_set_err(BlockJob
*job
, int error
)
618 if (job
->iostatus
== BLOCK_DEVICE_IO_STATUS_OK
) {
619 job
->iostatus
= error
== ENOSPC
? BLOCK_DEVICE_IO_STATUS_NOSPACE
:
620 BLOCK_DEVICE_IO_STATUS_FAILED
;
624 static void block_job_event_cancelled(BlockJob
*job
)
626 if (block_job_is_internal(job
)) {
630 qapi_event_send_block_job_cancelled(job
->driver
->job_type
,
638 static void block_job_event_completed(BlockJob
*job
, const char *msg
)
640 if (block_job_is_internal(job
)) {
644 qapi_event_send_block_job_completed(job
->driver
->job_type
,
654 void block_job_event_ready(BlockJob
*job
)
658 if (block_job_is_internal(job
)) {
662 qapi_event_send_block_job_ready(job
->driver
->job_type
,
666 job
->speed
, &error_abort
);
669 BlockErrorAction
block_job_error_action(BlockJob
*job
, BlockdevOnError on_err
,
670 int is_read
, int error
)
672 BlockErrorAction action
;
675 case BLOCKDEV_ON_ERROR_ENOSPC
:
676 case BLOCKDEV_ON_ERROR_AUTO
:
677 action
= (error
== ENOSPC
) ?
678 BLOCK_ERROR_ACTION_STOP
: BLOCK_ERROR_ACTION_REPORT
;
680 case BLOCKDEV_ON_ERROR_STOP
:
681 action
= BLOCK_ERROR_ACTION_STOP
;
683 case BLOCKDEV_ON_ERROR_REPORT
:
684 action
= BLOCK_ERROR_ACTION_REPORT
;
686 case BLOCKDEV_ON_ERROR_IGNORE
:
687 action
= BLOCK_ERROR_ACTION_IGNORE
;
692 if (!block_job_is_internal(job
)) {
693 qapi_event_send_block_job_error(job
->id
,
694 is_read
? IO_OPERATION_TYPE_READ
:
695 IO_OPERATION_TYPE_WRITE
,
696 action
, &error_abort
);
698 if (action
== BLOCK_ERROR_ACTION_STOP
) {
699 /* make the pause user visible, which will be resumed from QMP. */
700 block_job_user_pause(job
);
701 block_job_iostatus_set_err(job
, error
);
708 AioContext
*aio_context
;
709 BlockJobDeferToMainLoopFn
*fn
;
711 } BlockJobDeferToMainLoopData
;
713 static void block_job_defer_to_main_loop_bh(void *opaque
)
715 BlockJobDeferToMainLoopData
*data
= opaque
;
716 AioContext
*aio_context
;
718 /* Prevent race with block_job_defer_to_main_loop() */
719 aio_context_acquire(data
->aio_context
);
721 /* Fetch BDS AioContext again, in case it has changed */
722 aio_context
= blk_get_aio_context(data
->job
->blk
);
723 aio_context_acquire(aio_context
);
725 data
->job
->deferred_to_main_loop
= false;
726 data
->fn(data
->job
, data
->opaque
);
728 aio_context_release(aio_context
);
730 aio_context_release(data
->aio_context
);
735 void block_job_defer_to_main_loop(BlockJob
*job
,
736 BlockJobDeferToMainLoopFn
*fn
,
739 BlockJobDeferToMainLoopData
*data
= g_malloc(sizeof(*data
));
741 data
->aio_context
= blk_get_aio_context(job
->blk
);
743 data
->opaque
= opaque
;
744 job
->deferred_to_main_loop
= true;
746 aio_bh_schedule_oneshot(qemu_get_aio_context(),
747 block_job_defer_to_main_loop_bh
, data
);
750 BlockJobTxn
*block_job_txn_new(void)
752 BlockJobTxn
*txn
= g_new0(BlockJobTxn
, 1);
753 QLIST_INIT(&txn
->jobs
);
758 static void block_job_txn_ref(BlockJobTxn
*txn
)
763 void block_job_txn_unref(BlockJobTxn
*txn
)
765 if (txn
&& --txn
->refcnt
== 0) {
770 void block_job_txn_add_job(BlockJobTxn
*txn
, BlockJob
*job
)
779 QLIST_INSERT_HEAD(&txn
->jobs
, job
, txn_list
);
780 block_job_txn_ref(txn
);