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 BlockJob
*block_job_next(BlockJob
*job
)
61 return QLIST_FIRST(&block_jobs
);
63 return QLIST_NEXT(job
, job_list
);
66 BlockJob
*block_job_get(const char *id
)
70 QLIST_FOREACH(job
, &block_jobs
, job_list
) {
71 if (job
->id
&& !strcmp(id
, job
->id
)) {
79 static void block_job_attached_aio_context(AioContext
*new_context
,
82 BlockJob
*job
= opaque
;
84 if (job
->driver
->attached_aio_context
) {
85 job
->driver
->attached_aio_context(job
, new_context
);
88 block_job_resume(job
);
91 static void block_job_drain(BlockJob
*job
)
93 /* If job is !job->busy this kicks it into the next pause point. */
97 if (job
->driver
->drain
) {
98 job
->driver
->drain(job
);
102 static void block_job_detach_aio_context(void *opaque
)
104 BlockJob
*job
= opaque
;
106 /* In case the job terminates during aio_poll()... */
109 block_job_pause(job
);
111 while (!job
->paused
&& !job
->completed
) {
112 block_job_drain(job
);
115 block_job_unref(job
);
118 void block_job_add_bdrv(BlockJob
*job
, BlockDriverState
*bs
)
120 job
->nodes
= g_slist_prepend(job
->nodes
, bs
);
122 bdrv_op_block_all(bs
, job
->blocker
);
125 void *block_job_create(const char *job_id
, const BlockJobDriver
*driver
,
126 BlockDriverState
*bs
, int64_t speed
, int flags
,
127 BlockCompletionFunc
*cb
, void *opaque
, Error
**errp
)
133 error_setg(errp
, QERR_DEVICE_IN_USE
, bdrv_get_device_name(bs
));
137 if (job_id
== NULL
&& !(flags
& BLOCK_JOB_INTERNAL
)) {
138 job_id
= bdrv_get_device_name(bs
);
140 error_setg(errp
, "An explicit job ID is required for this node");
146 if (flags
& BLOCK_JOB_INTERNAL
) {
147 error_setg(errp
, "Cannot specify job ID for internal block job");
151 if (!id_wellformed(job_id
)) {
152 error_setg(errp
, "Invalid job ID '%s'", job_id
);
156 if (block_job_get(job_id
)) {
157 error_setg(errp
, "Job ID '%s' already in use", job_id
);
163 blk_insert_bs(blk
, bs
);
165 job
= g_malloc0(driver
->instance_size
);
166 error_setg(&job
->blocker
, "block device is in use by block job: %s",
167 BlockJobType_lookup
[driver
->job_type
]);
168 block_job_add_bdrv(job
, bs
);
169 bdrv_op_unblock(bs
, BLOCK_OP_TYPE_DATAPLANE
, job
->blocker
);
171 job
->driver
= driver
;
172 job
->id
= g_strdup(job_id
);
175 job
->opaque
= opaque
;
178 job
->pause_count
= 1;
182 QLIST_INSERT_HEAD(&block_jobs
, job
, job_list
);
184 blk_add_aio_context_notifier(blk
, block_job_attached_aio_context
,
185 block_job_detach_aio_context
, job
);
187 /* Only set speed when necessary to avoid NotSupported error */
189 Error
*local_err
= NULL
;
191 block_job_set_speed(job
, speed
, &local_err
);
193 block_job_unref(job
);
194 error_propagate(errp
, local_err
);
201 bool block_job_is_internal(BlockJob
*job
)
203 return (job
->id
== NULL
);
206 static bool block_job_started(BlockJob
*job
)
211 void block_job_start(BlockJob
*job
)
213 assert(job
&& !block_job_started(job
) && job
->paused
&&
214 !job
->busy
&& job
->driver
->start
);
215 job
->co
= qemu_coroutine_create(job
->driver
->start
, job
);
216 if (--job
->pause_count
== 0) {
219 qemu_coroutine_enter(job
->co
);
223 void block_job_ref(BlockJob
*job
)
228 void block_job_unref(BlockJob
*job
)
230 if (--job
->refcnt
== 0) {
232 BlockDriverState
*bs
= blk_bs(job
->blk
);
234 for (l
= job
->nodes
; l
; l
= l
->next
) {
236 bdrv_op_unblock_all(bs
, job
->blocker
);
239 g_slist_free(job
->nodes
);
240 blk_remove_aio_context_notifier(job
->blk
,
241 block_job_attached_aio_context
,
242 block_job_detach_aio_context
, job
);
244 error_free(job
->blocker
);
246 QLIST_REMOVE(job
, job_list
);
251 static void block_job_completed_single(BlockJob
*job
)
254 if (job
->driver
->commit
) {
255 job
->driver
->commit(job
);
258 if (job
->driver
->abort
) {
259 job
->driver
->abort(job
);
262 if (job
->driver
->clean
) {
263 job
->driver
->clean(job
);
267 job
->cb(job
->opaque
, job
->ret
);
270 /* Emit events only if we actually started */
271 if (block_job_started(job
)) {
272 if (block_job_is_cancelled(job
)) {
273 block_job_event_cancelled(job
);
275 const char *msg
= NULL
;
277 msg
= strerror(-job
->ret
);
279 block_job_event_completed(job
, msg
);
284 QLIST_REMOVE(job
, txn_list
);
285 block_job_txn_unref(job
->txn
);
287 block_job_unref(job
);
290 static void block_job_completed_txn_abort(BlockJob
*job
)
293 BlockJobTxn
*txn
= job
->txn
;
294 BlockJob
*other_job
, *next
;
298 * We are cancelled by another job, which will handle everything.
302 txn
->aborting
= true;
303 /* We are the first failed job. Cancel other jobs. */
304 QLIST_FOREACH(other_job
, &txn
->jobs
, txn_list
) {
305 ctx
= blk_get_aio_context(other_job
->blk
);
306 aio_context_acquire(ctx
);
308 QLIST_FOREACH(other_job
, &txn
->jobs
, txn_list
) {
309 if (other_job
== job
|| other_job
->completed
) {
310 /* Other jobs are "effectively" cancelled by us, set the status for
311 * them; this job, however, may or may not be cancelled, depending
312 * on the caller, so leave it. */
313 if (other_job
!= job
) {
314 other_job
->cancelled
= true;
318 block_job_cancel_sync(other_job
);
319 assert(other_job
->completed
);
321 QLIST_FOREACH_SAFE(other_job
, &txn
->jobs
, txn_list
, next
) {
322 ctx
= blk_get_aio_context(other_job
->blk
);
323 block_job_completed_single(other_job
);
324 aio_context_release(ctx
);
328 static void block_job_completed_txn_success(BlockJob
*job
)
331 BlockJobTxn
*txn
= job
->txn
;
332 BlockJob
*other_job
, *next
;
334 * Successful completion, see if there are other running jobs in this
337 QLIST_FOREACH(other_job
, &txn
->jobs
, txn_list
) {
338 if (!other_job
->completed
) {
342 /* We are the last completed job, commit the transaction. */
343 QLIST_FOREACH_SAFE(other_job
, &txn
->jobs
, txn_list
, next
) {
344 ctx
= blk_get_aio_context(other_job
->blk
);
345 aio_context_acquire(ctx
);
346 assert(other_job
->ret
== 0);
347 block_job_completed_single(other_job
);
348 aio_context_release(ctx
);
352 void block_job_completed(BlockJob
*job
, int ret
)
354 assert(blk_bs(job
->blk
)->job
== job
);
355 assert(!job
->completed
);
356 job
->completed
= true;
359 block_job_completed_single(job
);
360 } else if (ret
< 0 || block_job_is_cancelled(job
)) {
361 block_job_completed_txn_abort(job
);
363 block_job_completed_txn_success(job
);
367 void block_job_set_speed(BlockJob
*job
, int64_t speed
, Error
**errp
)
369 Error
*local_err
= NULL
;
371 if (!job
->driver
->set_speed
) {
372 error_setg(errp
, QERR_UNSUPPORTED
);
375 job
->driver
->set_speed(job
, speed
, &local_err
);
377 error_propagate(errp
, local_err
);
384 void block_job_complete(BlockJob
*job
, Error
**errp
)
386 /* Should not be reachable via external interface for internal jobs */
388 if (job
->pause_count
|| job
->cancelled
||
389 !block_job_started(job
) || !job
->driver
->complete
) {
390 error_setg(errp
, "The active block job '%s' cannot be completed",
395 job
->driver
->complete(job
, errp
);
398 void block_job_pause(BlockJob
*job
)
403 void block_job_user_pause(BlockJob
*job
)
405 job
->user_paused
= true;
406 block_job_pause(job
);
409 static bool block_job_should_pause(BlockJob
*job
)
411 return job
->pause_count
> 0;
414 bool block_job_user_paused(BlockJob
*job
)
416 return job
? job
->user_paused
: 0;
419 void coroutine_fn
block_job_pause_point(BlockJob
*job
)
421 assert(job
&& block_job_started(job
));
423 if (!block_job_should_pause(job
)) {
426 if (block_job_is_cancelled(job
)) {
430 if (job
->driver
->pause
) {
431 job
->driver
->pause(job
);
434 if (block_job_should_pause(job
) && !block_job_is_cancelled(job
)) {
437 qemu_coroutine_yield(); /* wait for block_job_resume() */
442 if (job
->driver
->resume
) {
443 job
->driver
->resume(job
);
447 void block_job_resume(BlockJob
*job
)
449 assert(job
->pause_count
> 0);
451 if (job
->pause_count
) {
454 block_job_enter(job
);
457 void block_job_user_resume(BlockJob
*job
)
459 if (job
&& job
->user_paused
&& job
->pause_count
> 0) {
460 job
->user_paused
= false;
461 block_job_resume(job
);
465 void block_job_enter(BlockJob
*job
)
467 if (job
->co
&& !job
->busy
) {
468 qemu_coroutine_enter(job
->co
);
472 void block_job_cancel(BlockJob
*job
)
474 if (block_job_started(job
)) {
475 job
->cancelled
= true;
476 block_job_iostatus_reset(job
);
477 block_job_enter(job
);
479 block_job_completed(job
, -ECANCELED
);
483 bool block_job_is_cancelled(BlockJob
*job
)
485 return job
->cancelled
;
488 void block_job_iostatus_reset(BlockJob
*job
)
490 job
->iostatus
= BLOCK_DEVICE_IO_STATUS_OK
;
491 if (job
->driver
->iostatus_reset
) {
492 job
->driver
->iostatus_reset(job
);
496 static int block_job_finish_sync(BlockJob
*job
,
497 void (*finish
)(BlockJob
*, Error
**errp
),
500 Error
*local_err
= NULL
;
503 assert(blk_bs(job
->blk
)->job
== job
);
507 finish(job
, &local_err
);
509 error_propagate(errp
, local_err
);
510 block_job_unref(job
);
513 /* block_job_drain calls block_job_enter, and it should be enough to
514 * induce progress until the job completes or moves to the main thread.
516 while (!job
->deferred_to_main_loop
&& !job
->completed
) {
517 block_job_drain(job
);
519 while (!job
->completed
) {
520 aio_poll(qemu_get_aio_context(), true);
522 ret
= (job
->cancelled
&& job
->ret
== 0) ? -ECANCELED
: job
->ret
;
523 block_job_unref(job
);
527 /* A wrapper around block_job_cancel() taking an Error ** parameter so it may be
528 * used with block_job_finish_sync() without the need for (rather nasty)
529 * function pointer casts there. */
530 static void block_job_cancel_err(BlockJob
*job
, Error
**errp
)
532 block_job_cancel(job
);
535 int block_job_cancel_sync(BlockJob
*job
)
537 return block_job_finish_sync(job
, &block_job_cancel_err
, NULL
);
540 void block_job_cancel_sync_all(void)
543 AioContext
*aio_context
;
545 while ((job
= QLIST_FIRST(&block_jobs
))) {
546 aio_context
= blk_get_aio_context(job
->blk
);
547 aio_context_acquire(aio_context
);
548 block_job_cancel_sync(job
);
549 aio_context_release(aio_context
);
553 int block_job_complete_sync(BlockJob
*job
, Error
**errp
)
555 return block_job_finish_sync(job
, &block_job_complete
, errp
);
558 void block_job_sleep_ns(BlockJob
*job
, QEMUClockType type
, int64_t ns
)
562 /* Check cancellation *before* setting busy = false, too! */
563 if (block_job_is_cancelled(job
)) {
568 if (!block_job_should_pause(job
)) {
569 co_aio_sleep_ns(blk_get_aio_context(job
->blk
), type
, ns
);
573 block_job_pause_point(job
);
576 void block_job_yield(BlockJob
*job
)
580 /* Check cancellation *before* setting busy = false, too! */
581 if (block_job_is_cancelled(job
)) {
586 if (!block_job_should_pause(job
)) {
587 qemu_coroutine_yield();
591 block_job_pause_point(job
);
594 BlockJobInfo
*block_job_query(BlockJob
*job
, Error
**errp
)
598 if (block_job_is_internal(job
)) {
599 error_setg(errp
, "Cannot query QEMU internal jobs");
602 info
= g_new0(BlockJobInfo
, 1);
603 info
->type
= g_strdup(BlockJobType_lookup
[job
->driver
->job_type
]);
604 info
->device
= g_strdup(job
->id
);
605 info
->len
= job
->len
;
606 info
->busy
= job
->busy
;
607 info
->paused
= job
->pause_count
> 0;
608 info
->offset
= job
->offset
;
609 info
->speed
= job
->speed
;
610 info
->io_status
= job
->iostatus
;
611 info
->ready
= job
->ready
;
615 static void block_job_iostatus_set_err(BlockJob
*job
, int error
)
617 if (job
->iostatus
== BLOCK_DEVICE_IO_STATUS_OK
) {
618 job
->iostatus
= error
== ENOSPC
? BLOCK_DEVICE_IO_STATUS_NOSPACE
:
619 BLOCK_DEVICE_IO_STATUS_FAILED
;
623 static void block_job_event_cancelled(BlockJob
*job
)
625 if (block_job_is_internal(job
)) {
629 qapi_event_send_block_job_cancelled(job
->driver
->job_type
,
637 static void block_job_event_completed(BlockJob
*job
, const char *msg
)
639 if (block_job_is_internal(job
)) {
643 qapi_event_send_block_job_completed(job
->driver
->job_type
,
653 void block_job_event_ready(BlockJob
*job
)
657 if (block_job_is_internal(job
)) {
661 qapi_event_send_block_job_ready(job
->driver
->job_type
,
665 job
->speed
, &error_abort
);
668 BlockErrorAction
block_job_error_action(BlockJob
*job
, BlockdevOnError on_err
,
669 int is_read
, int error
)
671 BlockErrorAction action
;
674 case BLOCKDEV_ON_ERROR_ENOSPC
:
675 case BLOCKDEV_ON_ERROR_AUTO
:
676 action
= (error
== ENOSPC
) ?
677 BLOCK_ERROR_ACTION_STOP
: BLOCK_ERROR_ACTION_REPORT
;
679 case BLOCKDEV_ON_ERROR_STOP
:
680 action
= BLOCK_ERROR_ACTION_STOP
;
682 case BLOCKDEV_ON_ERROR_REPORT
:
683 action
= BLOCK_ERROR_ACTION_REPORT
;
685 case BLOCKDEV_ON_ERROR_IGNORE
:
686 action
= BLOCK_ERROR_ACTION_IGNORE
;
691 if (!block_job_is_internal(job
)) {
692 qapi_event_send_block_job_error(job
->id
,
693 is_read
? IO_OPERATION_TYPE_READ
:
694 IO_OPERATION_TYPE_WRITE
,
695 action
, &error_abort
);
697 if (action
== BLOCK_ERROR_ACTION_STOP
) {
698 /* make the pause user visible, which will be resumed from QMP. */
699 block_job_user_pause(job
);
700 block_job_iostatus_set_err(job
, error
);
707 AioContext
*aio_context
;
708 BlockJobDeferToMainLoopFn
*fn
;
710 } BlockJobDeferToMainLoopData
;
712 static void block_job_defer_to_main_loop_bh(void *opaque
)
714 BlockJobDeferToMainLoopData
*data
= opaque
;
715 AioContext
*aio_context
;
717 /* Prevent race with block_job_defer_to_main_loop() */
718 aio_context_acquire(data
->aio_context
);
720 /* Fetch BDS AioContext again, in case it has changed */
721 aio_context
= blk_get_aio_context(data
->job
->blk
);
722 aio_context_acquire(aio_context
);
724 data
->job
->deferred_to_main_loop
= false;
725 data
->fn(data
->job
, data
->opaque
);
727 aio_context_release(aio_context
);
729 aio_context_release(data
->aio_context
);
734 void block_job_defer_to_main_loop(BlockJob
*job
,
735 BlockJobDeferToMainLoopFn
*fn
,
738 BlockJobDeferToMainLoopData
*data
= g_malloc(sizeof(*data
));
740 data
->aio_context
= blk_get_aio_context(job
->blk
);
742 data
->opaque
= opaque
;
743 job
->deferred_to_main_loop
= true;
745 aio_bh_schedule_oneshot(qemu_get_aio_context(),
746 block_job_defer_to_main_loop_bh
, data
);
749 BlockJobTxn
*block_job_txn_new(void)
751 BlockJobTxn
*txn
= g_new0(BlockJobTxn
, 1);
752 QLIST_INIT(&txn
->jobs
);
757 static void block_job_txn_ref(BlockJobTxn
*txn
)
762 void block_job_txn_unref(BlockJobTxn
*txn
)
764 if (txn
&& --txn
->refcnt
== 0) {
769 void block_job_txn_add_job(BlockJobTxn
*txn
, BlockJob
*job
)
778 QLIST_INSERT_HEAD(&txn
->jobs
, job
, txn_list
);
779 block_job_txn_ref(txn
);