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
;
181 QLIST_INSERT_HEAD(&block_jobs
, job
, job_list
);
183 blk_add_aio_context_notifier(blk
, block_job_attached_aio_context
,
184 block_job_detach_aio_context
, job
);
186 /* Only set speed when necessary to avoid NotSupported error */
188 Error
*local_err
= NULL
;
190 block_job_set_speed(job
, speed
, &local_err
);
192 block_job_unref(job
);
193 error_propagate(errp
, local_err
);
200 bool block_job_is_internal(BlockJob
*job
)
202 return (job
->id
== NULL
);
205 void block_job_ref(BlockJob
*job
)
210 void block_job_unref(BlockJob
*job
)
212 if (--job
->refcnt
== 0) {
214 BlockDriverState
*bs
= blk_bs(job
->blk
);
216 for (l
= job
->nodes
; l
; l
= l
->next
) {
218 bdrv_op_unblock_all(bs
, job
->blocker
);
221 g_slist_free(job
->nodes
);
222 blk_remove_aio_context_notifier(job
->blk
,
223 block_job_attached_aio_context
,
224 block_job_detach_aio_context
, job
);
226 error_free(job
->blocker
);
228 QLIST_REMOVE(job
, job_list
);
233 static void block_job_completed_single(BlockJob
*job
)
236 if (job
->driver
->commit
) {
237 job
->driver
->commit(job
);
240 if (job
->driver
->abort
) {
241 job
->driver
->abort(job
);
246 job
->cb(job
->opaque
, job
->ret
);
248 if (block_job_is_cancelled(job
)) {
249 block_job_event_cancelled(job
);
251 const char *msg
= NULL
;
253 msg
= strerror(-job
->ret
);
255 block_job_event_completed(job
, msg
);
259 block_job_txn_unref(job
->txn
);
261 block_job_unref(job
);
264 static void block_job_completed_txn_abort(BlockJob
*job
)
267 BlockJobTxn
*txn
= job
->txn
;
268 BlockJob
*other_job
, *next
;
272 * We are cancelled by another job, which will handle everything.
276 txn
->aborting
= true;
277 /* We are the first failed job. Cancel other jobs. */
278 QLIST_FOREACH(other_job
, &txn
->jobs
, txn_list
) {
279 ctx
= blk_get_aio_context(other_job
->blk
);
280 aio_context_acquire(ctx
);
282 QLIST_FOREACH(other_job
, &txn
->jobs
, txn_list
) {
283 if (other_job
== job
|| other_job
->completed
) {
284 /* Other jobs are "effectively" cancelled by us, set the status for
285 * them; this job, however, may or may not be cancelled, depending
286 * on the caller, so leave it. */
287 if (other_job
!= job
) {
288 other_job
->cancelled
= true;
292 block_job_cancel_sync(other_job
);
293 assert(other_job
->completed
);
295 QLIST_FOREACH_SAFE(other_job
, &txn
->jobs
, txn_list
, next
) {
296 ctx
= blk_get_aio_context(other_job
->blk
);
297 block_job_completed_single(other_job
);
298 aio_context_release(ctx
);
302 static void block_job_completed_txn_success(BlockJob
*job
)
305 BlockJobTxn
*txn
= job
->txn
;
306 BlockJob
*other_job
, *next
;
308 * Successful completion, see if there are other running jobs in this
311 QLIST_FOREACH(other_job
, &txn
->jobs
, txn_list
) {
312 if (!other_job
->completed
) {
316 /* We are the last completed job, commit the transaction. */
317 QLIST_FOREACH_SAFE(other_job
, &txn
->jobs
, txn_list
, next
) {
318 ctx
= blk_get_aio_context(other_job
->blk
);
319 aio_context_acquire(ctx
);
320 assert(other_job
->ret
== 0);
321 block_job_completed_single(other_job
);
322 aio_context_release(ctx
);
326 void block_job_completed(BlockJob
*job
, int ret
)
328 assert(blk_bs(job
->blk
)->job
== job
);
329 assert(!job
->completed
);
330 job
->completed
= true;
333 block_job_completed_single(job
);
334 } else if (ret
< 0 || block_job_is_cancelled(job
)) {
335 block_job_completed_txn_abort(job
);
337 block_job_completed_txn_success(job
);
341 void block_job_set_speed(BlockJob
*job
, int64_t speed
, Error
**errp
)
343 Error
*local_err
= NULL
;
345 if (!job
->driver
->set_speed
) {
346 error_setg(errp
, QERR_UNSUPPORTED
);
349 job
->driver
->set_speed(job
, speed
, &local_err
);
351 error_propagate(errp
, local_err
);
358 void block_job_complete(BlockJob
*job
, Error
**errp
)
360 /* Should not be reachable via external interface for internal jobs */
362 if (job
->pause_count
|| job
->cancelled
|| !job
->driver
->complete
) {
363 error_setg(errp
, "The active block job '%s' cannot be completed",
368 job
->driver
->complete(job
, errp
);
371 void block_job_pause(BlockJob
*job
)
376 void block_job_user_pause(BlockJob
*job
)
378 job
->user_paused
= true;
379 block_job_pause(job
);
382 static bool block_job_should_pause(BlockJob
*job
)
384 return job
->pause_count
> 0;
387 bool block_job_user_paused(BlockJob
*job
)
389 return job
? job
->user_paused
: 0;
392 void coroutine_fn
block_job_pause_point(BlockJob
*job
)
394 if (!block_job_should_pause(job
)) {
397 if (block_job_is_cancelled(job
)) {
401 if (job
->driver
->pause
) {
402 job
->driver
->pause(job
);
405 if (block_job_should_pause(job
) && !block_job_is_cancelled(job
)) {
408 qemu_coroutine_yield(); /* wait for block_job_resume() */
413 if (job
->driver
->resume
) {
414 job
->driver
->resume(job
);
418 void block_job_resume(BlockJob
*job
)
420 assert(job
->pause_count
> 0);
422 if (job
->pause_count
) {
425 block_job_enter(job
);
428 void block_job_user_resume(BlockJob
*job
)
430 if (job
&& job
->user_paused
&& job
->pause_count
> 0) {
431 job
->user_paused
= false;
432 block_job_resume(job
);
436 void block_job_enter(BlockJob
*job
)
438 if (job
->co
&& !job
->busy
) {
439 qemu_coroutine_enter(job
->co
);
443 void block_job_cancel(BlockJob
*job
)
445 job
->cancelled
= true;
446 block_job_iostatus_reset(job
);
447 block_job_enter(job
);
450 bool block_job_is_cancelled(BlockJob
*job
)
452 return job
->cancelled
;
455 void block_job_iostatus_reset(BlockJob
*job
)
457 job
->iostatus
= BLOCK_DEVICE_IO_STATUS_OK
;
458 if (job
->driver
->iostatus_reset
) {
459 job
->driver
->iostatus_reset(job
);
463 static int block_job_finish_sync(BlockJob
*job
,
464 void (*finish
)(BlockJob
*, Error
**errp
),
467 Error
*local_err
= NULL
;
470 assert(blk_bs(job
->blk
)->job
== job
);
474 finish(job
, &local_err
);
476 error_propagate(errp
, local_err
);
477 block_job_unref(job
);
480 /* block_job_drain calls block_job_enter, and it should be enough to
481 * induce progress until the job completes or moves to the main thread.
483 while (!job
->deferred_to_main_loop
&& !job
->completed
) {
484 block_job_drain(job
);
486 while (!job
->completed
) {
487 aio_poll(qemu_get_aio_context(), true);
489 ret
= (job
->cancelled
&& job
->ret
== 0) ? -ECANCELED
: job
->ret
;
490 block_job_unref(job
);
494 /* A wrapper around block_job_cancel() taking an Error ** parameter so it may be
495 * used with block_job_finish_sync() without the need for (rather nasty)
496 * function pointer casts there. */
497 static void block_job_cancel_err(BlockJob
*job
, Error
**errp
)
499 block_job_cancel(job
);
502 int block_job_cancel_sync(BlockJob
*job
)
504 return block_job_finish_sync(job
, &block_job_cancel_err
, NULL
);
507 void block_job_cancel_sync_all(void)
510 AioContext
*aio_context
;
512 while ((job
= QLIST_FIRST(&block_jobs
))) {
513 aio_context
= blk_get_aio_context(job
->blk
);
514 aio_context_acquire(aio_context
);
515 block_job_cancel_sync(job
);
516 aio_context_release(aio_context
);
520 int block_job_complete_sync(BlockJob
*job
, Error
**errp
)
522 return block_job_finish_sync(job
, &block_job_complete
, errp
);
525 void block_job_sleep_ns(BlockJob
*job
, QEMUClockType type
, int64_t ns
)
529 /* Check cancellation *before* setting busy = false, too! */
530 if (block_job_is_cancelled(job
)) {
535 if (!block_job_should_pause(job
)) {
536 co_aio_sleep_ns(blk_get_aio_context(job
->blk
), type
, ns
);
540 block_job_pause_point(job
);
543 void block_job_yield(BlockJob
*job
)
547 /* Check cancellation *before* setting busy = false, too! */
548 if (block_job_is_cancelled(job
)) {
553 if (!block_job_should_pause(job
)) {
554 qemu_coroutine_yield();
558 block_job_pause_point(job
);
561 BlockJobInfo
*block_job_query(BlockJob
*job
, Error
**errp
)
565 if (block_job_is_internal(job
)) {
566 error_setg(errp
, "Cannot query QEMU internal jobs");
569 info
= g_new0(BlockJobInfo
, 1);
570 info
->type
= g_strdup(BlockJobType_lookup
[job
->driver
->job_type
]);
571 info
->device
= g_strdup(job
->id
);
572 info
->len
= job
->len
;
573 info
->busy
= job
->busy
;
574 info
->paused
= job
->pause_count
> 0;
575 info
->offset
= job
->offset
;
576 info
->speed
= job
->speed
;
577 info
->io_status
= job
->iostatus
;
578 info
->ready
= job
->ready
;
582 static void block_job_iostatus_set_err(BlockJob
*job
, int error
)
584 if (job
->iostatus
== BLOCK_DEVICE_IO_STATUS_OK
) {
585 job
->iostatus
= error
== ENOSPC
? BLOCK_DEVICE_IO_STATUS_NOSPACE
:
586 BLOCK_DEVICE_IO_STATUS_FAILED
;
590 static void block_job_event_cancelled(BlockJob
*job
)
592 if (block_job_is_internal(job
)) {
596 qapi_event_send_block_job_cancelled(job
->driver
->job_type
,
604 static void block_job_event_completed(BlockJob
*job
, const char *msg
)
606 if (block_job_is_internal(job
)) {
610 qapi_event_send_block_job_completed(job
->driver
->job_type
,
620 void block_job_event_ready(BlockJob
*job
)
624 if (block_job_is_internal(job
)) {
628 qapi_event_send_block_job_ready(job
->driver
->job_type
,
632 job
->speed
, &error_abort
);
635 BlockErrorAction
block_job_error_action(BlockJob
*job
, BlockdevOnError on_err
,
636 int is_read
, int error
)
638 BlockErrorAction action
;
641 case BLOCKDEV_ON_ERROR_ENOSPC
:
642 case BLOCKDEV_ON_ERROR_AUTO
:
643 action
= (error
== ENOSPC
) ?
644 BLOCK_ERROR_ACTION_STOP
: BLOCK_ERROR_ACTION_REPORT
;
646 case BLOCKDEV_ON_ERROR_STOP
:
647 action
= BLOCK_ERROR_ACTION_STOP
;
649 case BLOCKDEV_ON_ERROR_REPORT
:
650 action
= BLOCK_ERROR_ACTION_REPORT
;
652 case BLOCKDEV_ON_ERROR_IGNORE
:
653 action
= BLOCK_ERROR_ACTION_IGNORE
;
658 if (!block_job_is_internal(job
)) {
659 qapi_event_send_block_job_error(job
->id
,
660 is_read
? IO_OPERATION_TYPE_READ
:
661 IO_OPERATION_TYPE_WRITE
,
662 action
, &error_abort
);
664 if (action
== BLOCK_ERROR_ACTION_STOP
) {
665 /* make the pause user visible, which will be resumed from QMP. */
666 block_job_user_pause(job
);
667 block_job_iostatus_set_err(job
, error
);
674 AioContext
*aio_context
;
675 BlockJobDeferToMainLoopFn
*fn
;
677 } BlockJobDeferToMainLoopData
;
679 static void block_job_defer_to_main_loop_bh(void *opaque
)
681 BlockJobDeferToMainLoopData
*data
= opaque
;
682 AioContext
*aio_context
;
684 /* Prevent race with block_job_defer_to_main_loop() */
685 aio_context_acquire(data
->aio_context
);
687 /* Fetch BDS AioContext again, in case it has changed */
688 aio_context
= blk_get_aio_context(data
->job
->blk
);
689 aio_context_acquire(aio_context
);
691 data
->job
->deferred_to_main_loop
= false;
692 data
->fn(data
->job
, data
->opaque
);
694 aio_context_release(aio_context
);
696 aio_context_release(data
->aio_context
);
701 void block_job_defer_to_main_loop(BlockJob
*job
,
702 BlockJobDeferToMainLoopFn
*fn
,
705 BlockJobDeferToMainLoopData
*data
= g_malloc(sizeof(*data
));
707 data
->aio_context
= blk_get_aio_context(job
->blk
);
709 data
->opaque
= opaque
;
710 job
->deferred_to_main_loop
= true;
712 aio_bh_schedule_oneshot(qemu_get_aio_context(),
713 block_job_defer_to_main_loop_bh
, data
);
716 BlockJobTxn
*block_job_txn_new(void)
718 BlockJobTxn
*txn
= g_new0(BlockJobTxn
, 1);
719 QLIST_INIT(&txn
->jobs
);
724 static void block_job_txn_ref(BlockJobTxn
*txn
)
729 void block_job_txn_unref(BlockJobTxn
*txn
)
731 if (txn
&& --txn
->refcnt
== 0) {
736 void block_job_txn_add_job(BlockJobTxn
*txn
, BlockJob
*job
)
745 QLIST_INSERT_HEAD(&txn
->jobs
, job
, txn_list
);
746 block_job_txn_ref(txn
);