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
));
94 QLIST_INSERT_HEAD(&block_jobs
, job
, job_list
);
96 /* Only set speed when necessary to avoid NotSupported error */
98 Error
*local_err
= NULL
;
100 block_job_set_speed(job
, speed
, &local_err
);
102 block_job_unref(job
);
103 error_propagate(errp
, local_err
);
110 void block_job_ref(BlockJob
*job
)
115 void block_job_unref(BlockJob
*job
)
117 if (--job
->refcnt
== 0) {
118 BlockDriverState
*bs
= blk_bs(job
->blk
);
120 bdrv_op_unblock_all(bs
, job
->blocker
);
122 error_free(job
->blocker
);
124 QLIST_REMOVE(job
, job_list
);
129 static void block_job_completed_single(BlockJob
*job
)
132 if (job
->driver
->commit
) {
133 job
->driver
->commit(job
);
136 if (job
->driver
->abort
) {
137 job
->driver
->abort(job
);
140 job
->cb(job
->opaque
, job
->ret
);
142 block_job_txn_unref(job
->txn
);
144 block_job_unref(job
);
147 static void block_job_completed_txn_abort(BlockJob
*job
)
150 BlockJobTxn
*txn
= job
->txn
;
151 BlockJob
*other_job
, *next
;
155 * We are cancelled by another job, which will handle everything.
159 txn
->aborting
= true;
160 /* We are the first failed job. Cancel other jobs. */
161 QLIST_FOREACH(other_job
, &txn
->jobs
, txn_list
) {
162 ctx
= blk_get_aio_context(other_job
->blk
);
163 aio_context_acquire(ctx
);
165 QLIST_FOREACH(other_job
, &txn
->jobs
, txn_list
) {
166 if (other_job
== job
|| other_job
->completed
) {
167 /* Other jobs are "effectively" cancelled by us, set the status for
168 * them; this job, however, may or may not be cancelled, depending
169 * on the caller, so leave it. */
170 if (other_job
!= job
) {
171 other_job
->cancelled
= true;
175 block_job_cancel_sync(other_job
);
176 assert(other_job
->completed
);
178 QLIST_FOREACH_SAFE(other_job
, &txn
->jobs
, txn_list
, next
) {
179 ctx
= blk_get_aio_context(other_job
->blk
);
180 block_job_completed_single(other_job
);
181 aio_context_release(ctx
);
185 static void block_job_completed_txn_success(BlockJob
*job
)
188 BlockJobTxn
*txn
= job
->txn
;
189 BlockJob
*other_job
, *next
;
191 * Successful completion, see if there are other running jobs in this
194 QLIST_FOREACH(other_job
, &txn
->jobs
, txn_list
) {
195 if (!other_job
->completed
) {
199 /* We are the last completed job, commit the transaction. */
200 QLIST_FOREACH_SAFE(other_job
, &txn
->jobs
, txn_list
, next
) {
201 ctx
= blk_get_aio_context(other_job
->blk
);
202 aio_context_acquire(ctx
);
203 assert(other_job
->ret
== 0);
204 block_job_completed_single(other_job
);
205 aio_context_release(ctx
);
209 void block_job_completed(BlockJob
*job
, int ret
)
211 assert(blk_bs(job
->blk
)->job
== job
);
212 assert(!job
->completed
);
213 job
->completed
= true;
216 block_job_completed_single(job
);
217 } else if (ret
< 0 || block_job_is_cancelled(job
)) {
218 block_job_completed_txn_abort(job
);
220 block_job_completed_txn_success(job
);
224 void block_job_set_speed(BlockJob
*job
, int64_t speed
, Error
**errp
)
226 Error
*local_err
= NULL
;
228 if (!job
->driver
->set_speed
) {
229 error_setg(errp
, QERR_UNSUPPORTED
);
232 job
->driver
->set_speed(job
, speed
, &local_err
);
234 error_propagate(errp
, local_err
);
241 void block_job_complete(BlockJob
*job
, Error
**errp
)
243 if (job
->pause_count
|| job
->cancelled
|| !job
->driver
->complete
) {
244 error_setg(errp
, QERR_BLOCK_JOB_NOT_READY
, job
->id
);
248 job
->driver
->complete(job
, errp
);
251 void block_job_pause(BlockJob
*job
)
256 bool block_job_is_paused(BlockJob
*job
)
258 return job
->pause_count
> 0;
261 void block_job_resume(BlockJob
*job
)
263 assert(job
->pause_count
> 0);
265 if (job
->pause_count
) {
268 block_job_enter(job
);
271 void block_job_enter(BlockJob
*job
)
273 block_job_iostatus_reset(job
);
274 if (job
->co
&& !job
->busy
) {
275 qemu_coroutine_enter(job
->co
, NULL
);
279 void block_job_cancel(BlockJob
*job
)
281 job
->cancelled
= true;
282 block_job_enter(job
);
285 bool block_job_is_cancelled(BlockJob
*job
)
287 return job
->cancelled
;
290 void block_job_iostatus_reset(BlockJob
*job
)
292 job
->iostatus
= BLOCK_DEVICE_IO_STATUS_OK
;
293 if (job
->driver
->iostatus_reset
) {
294 job
->driver
->iostatus_reset(job
);
298 static int block_job_finish_sync(BlockJob
*job
,
299 void (*finish
)(BlockJob
*, Error
**errp
),
302 Error
*local_err
= NULL
;
305 assert(blk_bs(job
->blk
)->job
== job
);
308 finish(job
, &local_err
);
310 error_propagate(errp
, local_err
);
311 block_job_unref(job
);
314 while (!job
->completed
) {
315 aio_poll(job
->deferred_to_main_loop
? qemu_get_aio_context() :
316 blk_get_aio_context(job
->blk
),
319 ret
= (job
->cancelled
&& job
->ret
== 0) ? -ECANCELED
: job
->ret
;
320 block_job_unref(job
);
324 /* A wrapper around block_job_cancel() taking an Error ** parameter so it may be
325 * used with block_job_finish_sync() without the need for (rather nasty)
326 * function pointer casts there. */
327 static void block_job_cancel_err(BlockJob
*job
, Error
**errp
)
329 block_job_cancel(job
);
332 int block_job_cancel_sync(BlockJob
*job
)
334 return block_job_finish_sync(job
, &block_job_cancel_err
, NULL
);
337 void block_job_cancel_sync_all(void)
340 AioContext
*aio_context
;
342 while ((job
= QLIST_FIRST(&block_jobs
))) {
343 aio_context
= blk_get_aio_context(job
->blk
);
344 aio_context_acquire(aio_context
);
345 block_job_cancel_sync(job
);
346 aio_context_release(aio_context
);
350 int block_job_complete_sync(BlockJob
*job
, Error
**errp
)
352 return block_job_finish_sync(job
, &block_job_complete
, errp
);
355 void block_job_sleep_ns(BlockJob
*job
, QEMUClockType type
, int64_t ns
)
359 /* Check cancellation *before* setting busy = false, too! */
360 if (block_job_is_cancelled(job
)) {
365 if (block_job_is_paused(job
)) {
366 qemu_coroutine_yield();
368 co_aio_sleep_ns(blk_get_aio_context(job
->blk
), type
, ns
);
373 void block_job_yield(BlockJob
*job
)
377 /* Check cancellation *before* setting busy = false, too! */
378 if (block_job_is_cancelled(job
)) {
383 qemu_coroutine_yield();
387 BlockJobInfo
*block_job_query(BlockJob
*job
)
389 BlockJobInfo
*info
= g_new0(BlockJobInfo
, 1);
390 info
->type
= g_strdup(BlockJobType_lookup
[job
->driver
->job_type
]);
391 info
->device
= g_strdup(job
->id
);
392 info
->len
= job
->len
;
393 info
->busy
= job
->busy
;
394 info
->paused
= job
->pause_count
> 0;
395 info
->offset
= job
->offset
;
396 info
->speed
= job
->speed
;
397 info
->io_status
= job
->iostatus
;
398 info
->ready
= job
->ready
;
402 static void block_job_iostatus_set_err(BlockJob
*job
, int error
)
404 if (job
->iostatus
== BLOCK_DEVICE_IO_STATUS_OK
) {
405 job
->iostatus
= error
== ENOSPC
? BLOCK_DEVICE_IO_STATUS_NOSPACE
:
406 BLOCK_DEVICE_IO_STATUS_FAILED
;
410 void block_job_event_cancelled(BlockJob
*job
)
412 qapi_event_send_block_job_cancelled(job
->driver
->job_type
,
420 void block_job_event_completed(BlockJob
*job
, const char *msg
)
422 qapi_event_send_block_job_completed(job
->driver
->job_type
,
432 void block_job_event_ready(BlockJob
*job
)
436 qapi_event_send_block_job_ready(job
->driver
->job_type
,
440 job
->speed
, &error_abort
);
443 BlockErrorAction
block_job_error_action(BlockJob
*job
, BlockdevOnError on_err
,
444 int is_read
, int error
)
446 BlockErrorAction action
;
449 case BLOCKDEV_ON_ERROR_ENOSPC
:
450 action
= (error
== ENOSPC
) ?
451 BLOCK_ERROR_ACTION_STOP
: BLOCK_ERROR_ACTION_REPORT
;
453 case BLOCKDEV_ON_ERROR_STOP
:
454 action
= BLOCK_ERROR_ACTION_STOP
;
456 case BLOCKDEV_ON_ERROR_REPORT
:
457 action
= BLOCK_ERROR_ACTION_REPORT
;
459 case BLOCKDEV_ON_ERROR_IGNORE
:
460 action
= BLOCK_ERROR_ACTION_IGNORE
;
465 qapi_event_send_block_job_error(job
->id
,
466 is_read
? IO_OPERATION_TYPE_READ
:
467 IO_OPERATION_TYPE_WRITE
,
468 action
, &error_abort
);
469 if (action
== BLOCK_ERROR_ACTION_STOP
) {
470 /* make the pause user visible, which will be resumed from QMP. */
471 job
->user_paused
= true;
472 block_job_pause(job
);
473 block_job_iostatus_set_err(job
, error
);
481 AioContext
*aio_context
;
482 BlockJobDeferToMainLoopFn
*fn
;
484 } BlockJobDeferToMainLoopData
;
486 static void block_job_defer_to_main_loop_bh(void *opaque
)
488 BlockJobDeferToMainLoopData
*data
= opaque
;
489 AioContext
*aio_context
;
491 qemu_bh_delete(data
->bh
);
493 /* Prevent race with block_job_defer_to_main_loop() */
494 aio_context_acquire(data
->aio_context
);
496 /* Fetch BDS AioContext again, in case it has changed */
497 aio_context
= blk_get_aio_context(data
->job
->blk
);
498 aio_context_acquire(aio_context
);
500 data
->job
->deferred_to_main_loop
= false;
501 data
->fn(data
->job
, data
->opaque
);
503 aio_context_release(aio_context
);
505 aio_context_release(data
->aio_context
);
510 void block_job_defer_to_main_loop(BlockJob
*job
,
511 BlockJobDeferToMainLoopFn
*fn
,
514 BlockJobDeferToMainLoopData
*data
= g_malloc(sizeof(*data
));
516 data
->bh
= qemu_bh_new(block_job_defer_to_main_loop_bh
, data
);
517 data
->aio_context
= blk_get_aio_context(job
->blk
);
519 data
->opaque
= opaque
;
520 job
->deferred_to_main_loop
= true;
522 qemu_bh_schedule(data
->bh
);
525 BlockJobTxn
*block_job_txn_new(void)
527 BlockJobTxn
*txn
= g_new0(BlockJobTxn
, 1);
528 QLIST_INIT(&txn
->jobs
);
533 static void block_job_txn_ref(BlockJobTxn
*txn
)
538 void block_job_txn_unref(BlockJobTxn
*txn
)
540 if (txn
&& --txn
->refcnt
== 0) {
545 void block_job_txn_add_job(BlockJobTxn
*txn
, BlockJob
*job
)
554 QLIST_INSERT_HEAD(&txn
->jobs
, job
, txn_list
);
555 block_job_txn_ref(txn
);