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 void *block_job_create(const BlockJobDriver
*driver
, BlockDriverState
*bs
,
64 int64_t speed
, BlockCompletionFunc
*cb
,
65 void *opaque
, Error
**errp
)
71 error_setg(errp
, QERR_DEVICE_IN_USE
, bdrv_get_device_name(bs
));
76 blk_insert_bs(blk
, bs
);
78 job
= g_malloc0(driver
->instance_size
);
79 error_setg(&job
->blocker
, "block device is in use by block job: %s",
80 BlockJobType_lookup
[driver
->job_type
]);
81 bdrv_op_block_all(bs
, job
->blocker
);
82 bdrv_op_unblock(bs
, BLOCK_OP_TYPE_DATAPLANE
, job
->blocker
);
85 job
->id
= g_strdup(bdrv_get_device_name(bs
));
93 QLIST_INSERT_HEAD(&block_jobs
, job
, job_list
);
95 /* Only set speed when necessary to avoid NotSupported error */
97 Error
*local_err
= NULL
;
99 block_job_set_speed(job
, speed
, &local_err
);
101 block_job_unref(job
);
102 error_propagate(errp
, local_err
);
109 void block_job_ref(BlockJob
*job
)
114 void block_job_unref(BlockJob
*job
)
116 if (--job
->refcnt
== 0) {
117 BlockDriverState
*bs
= blk_bs(job
->blk
);
119 bdrv_op_unblock_all(bs
, job
->blocker
);
121 error_free(job
->blocker
);
123 QLIST_REMOVE(job
, job_list
);
128 static void block_job_completed_single(BlockJob
*job
)
131 if (job
->driver
->commit
) {
132 job
->driver
->commit(job
);
135 if (job
->driver
->abort
) {
136 job
->driver
->abort(job
);
139 job
->cb(job
->opaque
, job
->ret
);
141 block_job_txn_unref(job
->txn
);
143 block_job_unref(job
);
146 static void block_job_completed_txn_abort(BlockJob
*job
)
149 BlockJobTxn
*txn
= job
->txn
;
150 BlockJob
*other_job
, *next
;
154 * We are cancelled by another job, which will handle everything.
158 txn
->aborting
= true;
159 /* We are the first failed job. Cancel other jobs. */
160 QLIST_FOREACH(other_job
, &txn
->jobs
, txn_list
) {
161 ctx
= blk_get_aio_context(other_job
->blk
);
162 aio_context_acquire(ctx
);
164 QLIST_FOREACH(other_job
, &txn
->jobs
, txn_list
) {
165 if (other_job
== job
|| other_job
->completed
) {
166 /* Other jobs are "effectively" cancelled by us, set the status for
167 * them; this job, however, may or may not be cancelled, depending
168 * on the caller, so leave it. */
169 if (other_job
!= job
) {
170 other_job
->cancelled
= true;
174 block_job_cancel_sync(other_job
);
175 assert(other_job
->completed
);
177 QLIST_FOREACH_SAFE(other_job
, &txn
->jobs
, txn_list
, next
) {
178 ctx
= blk_get_aio_context(other_job
->blk
);
179 block_job_completed_single(other_job
);
180 aio_context_release(ctx
);
184 static void block_job_completed_txn_success(BlockJob
*job
)
187 BlockJobTxn
*txn
= job
->txn
;
188 BlockJob
*other_job
, *next
;
190 * Successful completion, see if there are other running jobs in this
193 QLIST_FOREACH(other_job
, &txn
->jobs
, txn_list
) {
194 if (!other_job
->completed
) {
198 /* We are the last completed job, commit the transaction. */
199 QLIST_FOREACH_SAFE(other_job
, &txn
->jobs
, txn_list
, next
) {
200 ctx
= blk_get_aio_context(other_job
->blk
);
201 aio_context_acquire(ctx
);
202 assert(other_job
->ret
== 0);
203 block_job_completed_single(other_job
);
204 aio_context_release(ctx
);
208 void block_job_completed(BlockJob
*job
, int ret
)
210 assert(blk_bs(job
->blk
)->job
== job
);
211 assert(!job
->completed
);
212 job
->completed
= true;
215 block_job_completed_single(job
);
216 } else if (ret
< 0 || block_job_is_cancelled(job
)) {
217 block_job_completed_txn_abort(job
);
219 block_job_completed_txn_success(job
);
223 void block_job_set_speed(BlockJob
*job
, int64_t speed
, Error
**errp
)
225 Error
*local_err
= NULL
;
227 if (!job
->driver
->set_speed
) {
228 error_setg(errp
, QERR_UNSUPPORTED
);
231 job
->driver
->set_speed(job
, speed
, &local_err
);
233 error_propagate(errp
, local_err
);
240 void block_job_complete(BlockJob
*job
, Error
**errp
)
242 if (job
->pause_count
|| job
->cancelled
|| !job
->driver
->complete
) {
243 error_setg(errp
, QERR_BLOCK_JOB_NOT_READY
, job
->id
);
247 job
->driver
->complete(job
, errp
);
250 void block_job_pause(BlockJob
*job
)
255 bool block_job_is_paused(BlockJob
*job
)
257 return job
->pause_count
> 0;
260 void block_job_resume(BlockJob
*job
)
262 assert(job
->pause_count
> 0);
264 if (job
->pause_count
) {
267 block_job_enter(job
);
270 void block_job_enter(BlockJob
*job
)
272 block_job_iostatus_reset(job
);
273 if (job
->co
&& !job
->busy
) {
274 qemu_coroutine_enter(job
->co
, NULL
);
278 void block_job_cancel(BlockJob
*job
)
280 job
->cancelled
= true;
281 block_job_enter(job
);
284 bool block_job_is_cancelled(BlockJob
*job
)
286 return job
->cancelled
;
289 void block_job_iostatus_reset(BlockJob
*job
)
291 job
->iostatus
= BLOCK_DEVICE_IO_STATUS_OK
;
292 if (job
->driver
->iostatus_reset
) {
293 job
->driver
->iostatus_reset(job
);
297 static int block_job_finish_sync(BlockJob
*job
,
298 void (*finish
)(BlockJob
*, Error
**errp
),
301 Error
*local_err
= NULL
;
304 assert(blk_bs(job
->blk
)->job
== job
);
307 finish(job
, &local_err
);
309 error_propagate(errp
, local_err
);
310 block_job_unref(job
);
313 while (!job
->completed
) {
314 aio_poll(job
->deferred_to_main_loop
? qemu_get_aio_context() :
315 blk_get_aio_context(job
->blk
),
318 ret
= (job
->cancelled
&& job
->ret
== 0) ? -ECANCELED
: job
->ret
;
319 block_job_unref(job
);
323 /* A wrapper around block_job_cancel() taking an Error ** parameter so it may be
324 * used with block_job_finish_sync() without the need for (rather nasty)
325 * function pointer casts there. */
326 static void block_job_cancel_err(BlockJob
*job
, Error
**errp
)
328 block_job_cancel(job
);
331 int block_job_cancel_sync(BlockJob
*job
)
333 return block_job_finish_sync(job
, &block_job_cancel_err
, NULL
);
336 void block_job_cancel_sync_all(void)
339 AioContext
*aio_context
;
341 while ((job
= QLIST_FIRST(&block_jobs
))) {
342 aio_context
= blk_get_aio_context(job
->blk
);
343 aio_context_acquire(aio_context
);
344 block_job_cancel_sync(job
);
345 aio_context_release(aio_context
);
349 int block_job_complete_sync(BlockJob
*job
, Error
**errp
)
351 return block_job_finish_sync(job
, &block_job_complete
, errp
);
354 void block_job_sleep_ns(BlockJob
*job
, QEMUClockType type
, int64_t ns
)
358 /* Check cancellation *before* setting busy = false, too! */
359 if (block_job_is_cancelled(job
)) {
364 if (block_job_is_paused(job
)) {
365 qemu_coroutine_yield();
367 co_aio_sleep_ns(blk_get_aio_context(job
->blk
), type
, ns
);
372 void block_job_yield(BlockJob
*job
)
376 /* Check cancellation *before* setting busy = false, too! */
377 if (block_job_is_cancelled(job
)) {
382 qemu_coroutine_yield();
386 BlockJobInfo
*block_job_query(BlockJob
*job
)
388 BlockJobInfo
*info
= g_new0(BlockJobInfo
, 1);
389 info
->type
= g_strdup(BlockJobType_lookup
[job
->driver
->job_type
]);
390 info
->device
= g_strdup(job
->id
);
391 info
->len
= job
->len
;
392 info
->busy
= job
->busy
;
393 info
->paused
= job
->pause_count
> 0;
394 info
->offset
= job
->offset
;
395 info
->speed
= job
->speed
;
396 info
->io_status
= job
->iostatus
;
397 info
->ready
= job
->ready
;
401 static void block_job_iostatus_set_err(BlockJob
*job
, int error
)
403 if (job
->iostatus
== BLOCK_DEVICE_IO_STATUS_OK
) {
404 job
->iostatus
= error
== ENOSPC
? BLOCK_DEVICE_IO_STATUS_NOSPACE
:
405 BLOCK_DEVICE_IO_STATUS_FAILED
;
409 void block_job_event_cancelled(BlockJob
*job
)
411 qapi_event_send_block_job_cancelled(job
->driver
->job_type
,
419 void block_job_event_completed(BlockJob
*job
, const char *msg
)
421 qapi_event_send_block_job_completed(job
->driver
->job_type
,
431 void block_job_event_ready(BlockJob
*job
)
435 qapi_event_send_block_job_ready(job
->driver
->job_type
,
439 job
->speed
, &error_abort
);
442 BlockErrorAction
block_job_error_action(BlockJob
*job
, BlockdevOnError on_err
,
443 int is_read
, int error
)
445 BlockErrorAction action
;
448 case BLOCKDEV_ON_ERROR_ENOSPC
:
449 action
= (error
== ENOSPC
) ?
450 BLOCK_ERROR_ACTION_STOP
: BLOCK_ERROR_ACTION_REPORT
;
452 case BLOCKDEV_ON_ERROR_STOP
:
453 action
= BLOCK_ERROR_ACTION_STOP
;
455 case BLOCKDEV_ON_ERROR_REPORT
:
456 action
= BLOCK_ERROR_ACTION_REPORT
;
458 case BLOCKDEV_ON_ERROR_IGNORE
:
459 action
= BLOCK_ERROR_ACTION_IGNORE
;
464 qapi_event_send_block_job_error(job
->id
,
465 is_read
? IO_OPERATION_TYPE_READ
:
466 IO_OPERATION_TYPE_WRITE
,
467 action
, &error_abort
);
468 if (action
== BLOCK_ERROR_ACTION_STOP
) {
469 /* make the pause user visible, which will be resumed from QMP. */
470 job
->user_paused
= true;
471 block_job_pause(job
);
472 block_job_iostatus_set_err(job
, error
);
480 AioContext
*aio_context
;
481 BlockJobDeferToMainLoopFn
*fn
;
483 } BlockJobDeferToMainLoopData
;
485 static void block_job_defer_to_main_loop_bh(void *opaque
)
487 BlockJobDeferToMainLoopData
*data
= opaque
;
488 AioContext
*aio_context
;
490 qemu_bh_delete(data
->bh
);
492 /* Prevent race with block_job_defer_to_main_loop() */
493 aio_context_acquire(data
->aio_context
);
495 /* Fetch BDS AioContext again, in case it has changed */
496 aio_context
= blk_get_aio_context(data
->job
->blk
);
497 aio_context_acquire(aio_context
);
499 data
->job
->deferred_to_main_loop
= false;
500 data
->fn(data
->job
, data
->opaque
);
502 aio_context_release(aio_context
);
504 aio_context_release(data
->aio_context
);
509 void block_job_defer_to_main_loop(BlockJob
*job
,
510 BlockJobDeferToMainLoopFn
*fn
,
513 BlockJobDeferToMainLoopData
*data
= g_malloc(sizeof(*data
));
515 data
->bh
= qemu_bh_new(block_job_defer_to_main_loop_bh
, data
);
516 data
->aio_context
= blk_get_aio_context(job
->blk
);
518 data
->opaque
= opaque
;
519 job
->deferred_to_main_loop
= true;
521 qemu_bh_schedule(data
->bh
);
524 BlockJobTxn
*block_job_txn_new(void)
526 BlockJobTxn
*txn
= g_new0(BlockJobTxn
, 1);
527 QLIST_INIT(&txn
->jobs
);
532 static void block_job_txn_ref(BlockJobTxn
*txn
)
537 void block_job_txn_unref(BlockJobTxn
*txn
)
539 if (txn
&& --txn
->refcnt
== 0) {
544 void block_job_txn_add_job(BlockJobTxn
*txn
, BlockJob
*job
)
553 QLIST_INSERT_HEAD(&txn
->jobs
, job
, txn_list
);
554 block_job_txn_ref(txn
);