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.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"
36 #include "qmp-commands.h"
37 #include "qemu/timer.h"
38 #include "qapi-event.h"
40 /* Transactional group of block jobs */
43 /* Is this txn being cancelled? */
47 QLIST_HEAD(, BlockJob
) jobs
;
53 static QLIST_HEAD(, BlockJob
) block_jobs
= QLIST_HEAD_INITIALIZER(block_jobs
);
55 BlockJob
*block_job_next(BlockJob
*job
)
58 return QLIST_FIRST(&block_jobs
);
60 return QLIST_NEXT(job
, job_list
);
63 /* Normally the job runs in its BlockBackend's AioContext. The exception is
64 * block_job_defer_to_main_loop() where it runs in the QEMU main loop. Code
65 * that supports both cases uses this helper function.
67 static AioContext
*block_job_get_aio_context(BlockJob
*job
)
69 return job
->deferred_to_main_loop
?
70 qemu_get_aio_context() :
71 blk_get_aio_context(job
->blk
);
74 static void block_job_attached_aio_context(AioContext
*new_context
,
77 BlockJob
*job
= opaque
;
79 if (job
->driver
->attached_aio_context
) {
80 job
->driver
->attached_aio_context(job
, new_context
);
83 block_job_resume(job
);
86 static void block_job_detach_aio_context(void *opaque
)
88 BlockJob
*job
= opaque
;
90 /* In case the job terminates during aio_poll()... */
96 /* If job is !job->busy this kicks it into the next pause point. */
99 while (!job
->paused
&& !job
->completed
) {
100 aio_poll(block_job_get_aio_context(job
), true);
103 block_job_unref(job
);
106 void *block_job_create(const BlockJobDriver
*driver
, BlockDriverState
*bs
,
107 int64_t speed
, BlockCompletionFunc
*cb
,
108 void *opaque
, Error
**errp
)
114 error_setg(errp
, QERR_DEVICE_IN_USE
, bdrv_get_device_name(bs
));
119 blk_insert_bs(blk
, bs
);
121 job
= g_malloc0(driver
->instance_size
);
122 error_setg(&job
->blocker
, "block device is in use by block job: %s",
123 BlockJobType_lookup
[driver
->job_type
]);
124 bdrv_op_block_all(bs
, job
->blocker
);
125 bdrv_op_unblock(bs
, BLOCK_OP_TYPE_DATAPLANE
, job
->blocker
);
127 job
->driver
= driver
;
128 job
->id
= g_strdup(bdrv_get_device_name(bs
));
131 job
->opaque
= opaque
;
136 QLIST_INSERT_HEAD(&block_jobs
, job
, job_list
);
138 blk_add_aio_context_notifier(blk
, block_job_attached_aio_context
,
139 block_job_detach_aio_context
, job
);
141 /* Only set speed when necessary to avoid NotSupported error */
143 Error
*local_err
= NULL
;
145 block_job_set_speed(job
, speed
, &local_err
);
147 block_job_unref(job
);
148 error_propagate(errp
, local_err
);
155 void block_job_ref(BlockJob
*job
)
160 void block_job_unref(BlockJob
*job
)
162 if (--job
->refcnt
== 0) {
163 BlockDriverState
*bs
= blk_bs(job
->blk
);
165 bdrv_op_unblock_all(bs
, job
->blocker
);
166 blk_remove_aio_context_notifier(job
->blk
,
167 block_job_attached_aio_context
,
168 block_job_detach_aio_context
, job
);
170 error_free(job
->blocker
);
172 QLIST_REMOVE(job
, job_list
);
177 static void block_job_completed_single(BlockJob
*job
)
180 if (job
->driver
->commit
) {
181 job
->driver
->commit(job
);
184 if (job
->driver
->abort
) {
185 job
->driver
->abort(job
);
188 job
->cb(job
->opaque
, job
->ret
);
190 block_job_txn_unref(job
->txn
);
192 block_job_unref(job
);
195 static void block_job_completed_txn_abort(BlockJob
*job
)
198 BlockJobTxn
*txn
= job
->txn
;
199 BlockJob
*other_job
, *next
;
203 * We are cancelled by another job, which will handle everything.
207 txn
->aborting
= true;
208 /* We are the first failed job. Cancel other jobs. */
209 QLIST_FOREACH(other_job
, &txn
->jobs
, txn_list
) {
210 ctx
= blk_get_aio_context(other_job
->blk
);
211 aio_context_acquire(ctx
);
213 QLIST_FOREACH(other_job
, &txn
->jobs
, txn_list
) {
214 if (other_job
== job
|| other_job
->completed
) {
215 /* Other jobs are "effectively" cancelled by us, set the status for
216 * them; this job, however, may or may not be cancelled, depending
217 * on the caller, so leave it. */
218 if (other_job
!= job
) {
219 other_job
->cancelled
= true;
223 block_job_cancel_sync(other_job
);
224 assert(other_job
->completed
);
226 QLIST_FOREACH_SAFE(other_job
, &txn
->jobs
, txn_list
, next
) {
227 ctx
= blk_get_aio_context(other_job
->blk
);
228 block_job_completed_single(other_job
);
229 aio_context_release(ctx
);
233 static void block_job_completed_txn_success(BlockJob
*job
)
236 BlockJobTxn
*txn
= job
->txn
;
237 BlockJob
*other_job
, *next
;
239 * Successful completion, see if there are other running jobs in this
242 QLIST_FOREACH(other_job
, &txn
->jobs
, txn_list
) {
243 if (!other_job
->completed
) {
247 /* We are the last completed job, commit the transaction. */
248 QLIST_FOREACH_SAFE(other_job
, &txn
->jobs
, txn_list
, next
) {
249 ctx
= blk_get_aio_context(other_job
->blk
);
250 aio_context_acquire(ctx
);
251 assert(other_job
->ret
== 0);
252 block_job_completed_single(other_job
);
253 aio_context_release(ctx
);
257 void block_job_completed(BlockJob
*job
, int ret
)
259 assert(blk_bs(job
->blk
)->job
== job
);
260 assert(!job
->completed
);
261 job
->completed
= true;
264 block_job_completed_single(job
);
265 } else if (ret
< 0 || block_job_is_cancelled(job
)) {
266 block_job_completed_txn_abort(job
);
268 block_job_completed_txn_success(job
);
272 void block_job_set_speed(BlockJob
*job
, int64_t speed
, Error
**errp
)
274 Error
*local_err
= NULL
;
276 if (!job
->driver
->set_speed
) {
277 error_setg(errp
, QERR_UNSUPPORTED
);
280 job
->driver
->set_speed(job
, speed
, &local_err
);
282 error_propagate(errp
, local_err
);
289 void block_job_complete(BlockJob
*job
, Error
**errp
)
291 if (job
->pause_count
|| job
->cancelled
|| !job
->driver
->complete
) {
292 error_setg(errp
, QERR_BLOCK_JOB_NOT_READY
, job
->id
);
296 job
->driver
->complete(job
, errp
);
299 void block_job_pause(BlockJob
*job
)
304 static bool block_job_should_pause(BlockJob
*job
)
306 return job
->pause_count
> 0;
309 void coroutine_fn
block_job_pause_point(BlockJob
*job
)
311 if (!block_job_should_pause(job
)) {
314 if (block_job_is_cancelled(job
)) {
318 if (job
->driver
->pause
) {
319 job
->driver
->pause(job
);
322 if (block_job_should_pause(job
) && !block_job_is_cancelled(job
)) {
325 qemu_coroutine_yield(); /* wait for block_job_resume() */
330 if (job
->driver
->resume
) {
331 job
->driver
->resume(job
);
335 void block_job_resume(BlockJob
*job
)
337 assert(job
->pause_count
> 0);
339 if (job
->pause_count
) {
342 block_job_enter(job
);
345 void block_job_enter(BlockJob
*job
)
347 if (job
->co
&& !job
->busy
) {
348 qemu_coroutine_enter(job
->co
, NULL
);
352 void block_job_cancel(BlockJob
*job
)
354 job
->cancelled
= true;
355 block_job_iostatus_reset(job
);
356 block_job_enter(job
);
359 bool block_job_is_cancelled(BlockJob
*job
)
361 return job
->cancelled
;
364 void block_job_iostatus_reset(BlockJob
*job
)
366 job
->iostatus
= BLOCK_DEVICE_IO_STATUS_OK
;
367 if (job
->driver
->iostatus_reset
) {
368 job
->driver
->iostatus_reset(job
);
372 static int block_job_finish_sync(BlockJob
*job
,
373 void (*finish
)(BlockJob
*, Error
**errp
),
376 Error
*local_err
= NULL
;
379 assert(blk_bs(job
->blk
)->job
== job
);
382 finish(job
, &local_err
);
384 error_propagate(errp
, local_err
);
385 block_job_unref(job
);
388 while (!job
->completed
) {
389 aio_poll(block_job_get_aio_context(job
), true);
391 ret
= (job
->cancelled
&& job
->ret
== 0) ? -ECANCELED
: job
->ret
;
392 block_job_unref(job
);
396 /* A wrapper around block_job_cancel() taking an Error ** parameter so it may be
397 * used with block_job_finish_sync() without the need for (rather nasty)
398 * function pointer casts there. */
399 static void block_job_cancel_err(BlockJob
*job
, Error
**errp
)
401 block_job_cancel(job
);
404 int block_job_cancel_sync(BlockJob
*job
)
406 return block_job_finish_sync(job
, &block_job_cancel_err
, NULL
);
409 void block_job_cancel_sync_all(void)
412 AioContext
*aio_context
;
414 while ((job
= QLIST_FIRST(&block_jobs
))) {
415 aio_context
= blk_get_aio_context(job
->blk
);
416 aio_context_acquire(aio_context
);
417 block_job_cancel_sync(job
);
418 aio_context_release(aio_context
);
422 int block_job_complete_sync(BlockJob
*job
, Error
**errp
)
424 return block_job_finish_sync(job
, &block_job_complete
, errp
);
427 void block_job_sleep_ns(BlockJob
*job
, QEMUClockType type
, int64_t ns
)
431 /* Check cancellation *before* setting busy = false, too! */
432 if (block_job_is_cancelled(job
)) {
437 if (!block_job_should_pause(job
)) {
438 co_aio_sleep_ns(blk_get_aio_context(job
->blk
), type
, ns
);
442 block_job_pause_point(job
);
445 void block_job_yield(BlockJob
*job
)
449 /* Check cancellation *before* setting busy = false, too! */
450 if (block_job_is_cancelled(job
)) {
455 if (!block_job_should_pause(job
)) {
456 qemu_coroutine_yield();
460 block_job_pause_point(job
);
463 BlockJobInfo
*block_job_query(BlockJob
*job
)
465 BlockJobInfo
*info
= g_new0(BlockJobInfo
, 1);
466 info
->type
= g_strdup(BlockJobType_lookup
[job
->driver
->job_type
]);
467 info
->device
= g_strdup(job
->id
);
468 info
->len
= job
->len
;
469 info
->busy
= job
->busy
;
470 info
->paused
= job
->pause_count
> 0;
471 info
->offset
= job
->offset
;
472 info
->speed
= job
->speed
;
473 info
->io_status
= job
->iostatus
;
474 info
->ready
= job
->ready
;
478 static void block_job_iostatus_set_err(BlockJob
*job
, int error
)
480 if (job
->iostatus
== BLOCK_DEVICE_IO_STATUS_OK
) {
481 job
->iostatus
= error
== ENOSPC
? BLOCK_DEVICE_IO_STATUS_NOSPACE
:
482 BLOCK_DEVICE_IO_STATUS_FAILED
;
486 void block_job_event_cancelled(BlockJob
*job
)
488 qapi_event_send_block_job_cancelled(job
->driver
->job_type
,
496 void block_job_event_completed(BlockJob
*job
, const char *msg
)
498 qapi_event_send_block_job_completed(job
->driver
->job_type
,
508 void block_job_event_ready(BlockJob
*job
)
512 qapi_event_send_block_job_ready(job
->driver
->job_type
,
516 job
->speed
, &error_abort
);
519 BlockErrorAction
block_job_error_action(BlockJob
*job
, BlockdevOnError on_err
,
520 int is_read
, int error
)
522 BlockErrorAction action
;
525 case BLOCKDEV_ON_ERROR_ENOSPC
:
526 action
= (error
== ENOSPC
) ?
527 BLOCK_ERROR_ACTION_STOP
: BLOCK_ERROR_ACTION_REPORT
;
529 case BLOCKDEV_ON_ERROR_STOP
:
530 action
= BLOCK_ERROR_ACTION_STOP
;
532 case BLOCKDEV_ON_ERROR_REPORT
:
533 action
= BLOCK_ERROR_ACTION_REPORT
;
535 case BLOCKDEV_ON_ERROR_IGNORE
:
536 action
= BLOCK_ERROR_ACTION_IGNORE
;
541 qapi_event_send_block_job_error(job
->id
,
542 is_read
? IO_OPERATION_TYPE_READ
:
543 IO_OPERATION_TYPE_WRITE
,
544 action
, &error_abort
);
545 if (action
== BLOCK_ERROR_ACTION_STOP
) {
546 /* make the pause user visible, which will be resumed from QMP. */
547 job
->user_paused
= true;
548 block_job_pause(job
);
549 block_job_iostatus_set_err(job
, error
);
557 AioContext
*aio_context
;
558 BlockJobDeferToMainLoopFn
*fn
;
560 } BlockJobDeferToMainLoopData
;
562 static void block_job_defer_to_main_loop_bh(void *opaque
)
564 BlockJobDeferToMainLoopData
*data
= opaque
;
565 AioContext
*aio_context
;
567 qemu_bh_delete(data
->bh
);
569 /* Prevent race with block_job_defer_to_main_loop() */
570 aio_context_acquire(data
->aio_context
);
572 /* Fetch BDS AioContext again, in case it has changed */
573 aio_context
= blk_get_aio_context(data
->job
->blk
);
574 aio_context_acquire(aio_context
);
576 data
->job
->deferred_to_main_loop
= false;
577 data
->fn(data
->job
, data
->opaque
);
579 aio_context_release(aio_context
);
581 aio_context_release(data
->aio_context
);
586 void block_job_defer_to_main_loop(BlockJob
*job
,
587 BlockJobDeferToMainLoopFn
*fn
,
590 BlockJobDeferToMainLoopData
*data
= g_malloc(sizeof(*data
));
592 data
->bh
= qemu_bh_new(block_job_defer_to_main_loop_bh
, data
);
593 data
->aio_context
= blk_get_aio_context(job
->blk
);
595 data
->opaque
= opaque
;
596 job
->deferred_to_main_loop
= true;
598 qemu_bh_schedule(data
->bh
);
601 BlockJobTxn
*block_job_txn_new(void)
603 BlockJobTxn
*txn
= g_new0(BlockJobTxn
, 1);
604 QLIST_INIT(&txn
->jobs
);
609 static void block_job_txn_ref(BlockJobTxn
*txn
)
614 void block_job_txn_unref(BlockJobTxn
*txn
)
616 if (txn
&& --txn
->refcnt
== 0) {
621 void block_job_txn_add_job(BlockJobTxn
*txn
, BlockJob
*job
)
630 QLIST_INSERT_HEAD(&txn
->jobs
, job
, txn_list
);
631 block_job_txn_ref(txn
);