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 "config-host.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 void *block_job_create(const BlockJobDriver
*driver
, BlockDriverState
*bs
,
54 int64_t speed
, BlockCompletionFunc
*cb
,
55 void *opaque
, Error
**errp
)
60 error_setg(errp
, QERR_DEVICE_IN_USE
, bdrv_get_device_name(bs
));
64 job
= g_malloc0(driver
->instance_size
);
65 error_setg(&job
->blocker
, "block device is in use by block job: %s",
66 BlockJobType_lookup
[driver
->job_type
]);
67 bdrv_op_block_all(bs
, job
->blocker
);
68 bdrv_op_unblock(bs
, BLOCK_OP_TYPE_DATAPLANE
, job
->blocker
);
71 job
->id
= g_strdup(bdrv_get_device_name(bs
));
79 /* Only set speed when necessary to avoid NotSupported error */
81 Error
*local_err
= NULL
;
83 block_job_set_speed(job
, speed
, &local_err
);
86 error_propagate(errp
, local_err
);
93 void block_job_ref(BlockJob
*job
)
98 void block_job_unref(BlockJob
*job
)
100 if (--job
->refcnt
== 0) {
102 bdrv_op_unblock_all(job
->bs
, job
->blocker
);
104 error_free(job
->blocker
);
110 static void block_job_completed_single(BlockJob
*job
)
113 if (job
->driver
->commit
) {
114 job
->driver
->commit(job
);
117 if (job
->driver
->abort
) {
118 job
->driver
->abort(job
);
121 job
->cb(job
->opaque
, job
->ret
);
123 block_job_txn_unref(job
->txn
);
125 block_job_unref(job
);
128 static void block_job_completed_txn_abort(BlockJob
*job
)
131 BlockJobTxn
*txn
= job
->txn
;
132 BlockJob
*other_job
, *next
;
136 * We are cancelled by another job, which will handle everything.
140 txn
->aborting
= true;
141 /* We are the first failed job. Cancel other jobs. */
142 QLIST_FOREACH(other_job
, &txn
->jobs
, txn_list
) {
143 ctx
= bdrv_get_aio_context(other_job
->bs
);
144 aio_context_acquire(ctx
);
146 QLIST_FOREACH(other_job
, &txn
->jobs
, txn_list
) {
147 if (other_job
== job
|| other_job
->completed
) {
148 /* Other jobs are "effectively" cancelled by us, set the status for
149 * them; this job, however, may or may not be cancelled, depending
150 * on the caller, so leave it. */
151 if (other_job
!= job
) {
152 other_job
->cancelled
= true;
156 block_job_cancel_sync(other_job
);
157 assert(other_job
->completed
);
159 QLIST_FOREACH_SAFE(other_job
, &txn
->jobs
, txn_list
, next
) {
160 ctx
= bdrv_get_aio_context(other_job
->bs
);
161 block_job_completed_single(other_job
);
162 aio_context_release(ctx
);
166 static void block_job_completed_txn_success(BlockJob
*job
)
169 BlockJobTxn
*txn
= job
->txn
;
170 BlockJob
*other_job
, *next
;
172 * Successful completion, see if there are other running jobs in this
175 QLIST_FOREACH(other_job
, &txn
->jobs
, txn_list
) {
176 if (!other_job
->completed
) {
180 /* We are the last completed job, commit the transaction. */
181 QLIST_FOREACH_SAFE(other_job
, &txn
->jobs
, txn_list
, next
) {
182 ctx
= bdrv_get_aio_context(other_job
->bs
);
183 aio_context_acquire(ctx
);
184 assert(other_job
->ret
== 0);
185 block_job_completed_single(other_job
);
186 aio_context_release(ctx
);
190 void block_job_completed(BlockJob
*job
, int ret
)
192 BlockDriverState
*bs
= job
->bs
;
194 assert(bs
->job
== job
);
195 assert(!job
->completed
);
196 job
->completed
= true;
199 block_job_completed_single(job
);
200 } else if (ret
< 0 || block_job_is_cancelled(job
)) {
201 block_job_completed_txn_abort(job
);
203 block_job_completed_txn_success(job
);
207 void block_job_set_speed(BlockJob
*job
, int64_t speed
, Error
**errp
)
209 Error
*local_err
= NULL
;
211 if (!job
->driver
->set_speed
) {
212 error_setg(errp
, QERR_UNSUPPORTED
);
215 job
->driver
->set_speed(job
, speed
, &local_err
);
217 error_propagate(errp
, local_err
);
224 void block_job_complete(BlockJob
*job
, Error
**errp
)
226 if (job
->pause_count
|| job
->cancelled
|| !job
->driver
->complete
) {
227 error_setg(errp
, QERR_BLOCK_JOB_NOT_READY
, job
->id
);
231 job
->driver
->complete(job
, errp
);
234 void block_job_pause(BlockJob
*job
)
239 bool block_job_is_paused(BlockJob
*job
)
241 return job
->pause_count
> 0;
244 void block_job_resume(BlockJob
*job
)
246 assert(job
->pause_count
> 0);
248 if (job
->pause_count
) {
251 block_job_enter(job
);
254 void block_job_enter(BlockJob
*job
)
256 block_job_iostatus_reset(job
);
257 if (job
->co
&& !job
->busy
) {
258 qemu_coroutine_enter(job
->co
, NULL
);
262 void block_job_cancel(BlockJob
*job
)
264 job
->cancelled
= true;
265 block_job_enter(job
);
268 bool block_job_is_cancelled(BlockJob
*job
)
270 return job
->cancelled
;
273 void block_job_iostatus_reset(BlockJob
*job
)
275 job
->iostatus
= BLOCK_DEVICE_IO_STATUS_OK
;
276 if (job
->driver
->iostatus_reset
) {
277 job
->driver
->iostatus_reset(job
);
281 struct BlockFinishData
{
283 BlockCompletionFunc
*cb
;
289 static int block_job_finish_sync(BlockJob
*job
,
290 void (*finish
)(BlockJob
*, Error
**errp
),
293 BlockDriverState
*bs
= job
->bs
;
294 Error
*local_err
= NULL
;
297 assert(bs
->job
== job
);
300 finish(job
, &local_err
);
302 error_propagate(errp
, local_err
);
303 block_job_unref(job
);
306 while (!job
->completed
) {
307 aio_poll(bdrv_get_aio_context(bs
), true);
309 ret
= (job
->cancelled
&& job
->ret
== 0) ? -ECANCELED
: job
->ret
;
310 block_job_unref(job
);
314 /* A wrapper around block_job_cancel() taking an Error ** parameter so it may be
315 * used with block_job_finish_sync() without the need for (rather nasty)
316 * function pointer casts there. */
317 static void block_job_cancel_err(BlockJob
*job
, Error
**errp
)
319 block_job_cancel(job
);
322 int block_job_cancel_sync(BlockJob
*job
)
324 return block_job_finish_sync(job
, &block_job_cancel_err
, NULL
);
327 int block_job_complete_sync(BlockJob
*job
, Error
**errp
)
329 return block_job_finish_sync(job
, &block_job_complete
, errp
);
332 void block_job_sleep_ns(BlockJob
*job
, QEMUClockType type
, int64_t ns
)
336 /* Check cancellation *before* setting busy = false, too! */
337 if (block_job_is_cancelled(job
)) {
342 if (block_job_is_paused(job
)) {
343 qemu_coroutine_yield();
345 co_aio_sleep_ns(bdrv_get_aio_context(job
->bs
), type
, ns
);
350 void block_job_yield(BlockJob
*job
)
354 /* Check cancellation *before* setting busy = false, too! */
355 if (block_job_is_cancelled(job
)) {
360 qemu_coroutine_yield();
364 BlockJobInfo
*block_job_query(BlockJob
*job
)
366 BlockJobInfo
*info
= g_new0(BlockJobInfo
, 1);
367 info
->type
= g_strdup(BlockJobType_lookup
[job
->driver
->job_type
]);
368 info
->device
= g_strdup(job
->id
);
369 info
->len
= job
->len
;
370 info
->busy
= job
->busy
;
371 info
->paused
= job
->pause_count
> 0;
372 info
->offset
= job
->offset
;
373 info
->speed
= job
->speed
;
374 info
->io_status
= job
->iostatus
;
375 info
->ready
= job
->ready
;
379 static void block_job_iostatus_set_err(BlockJob
*job
, int error
)
381 if (job
->iostatus
== BLOCK_DEVICE_IO_STATUS_OK
) {
382 job
->iostatus
= error
== ENOSPC
? BLOCK_DEVICE_IO_STATUS_NOSPACE
:
383 BLOCK_DEVICE_IO_STATUS_FAILED
;
387 void block_job_event_cancelled(BlockJob
*job
)
389 qapi_event_send_block_job_cancelled(job
->driver
->job_type
,
397 void block_job_event_completed(BlockJob
*job
, const char *msg
)
399 qapi_event_send_block_job_completed(job
->driver
->job_type
,
409 void block_job_event_ready(BlockJob
*job
)
413 qapi_event_send_block_job_ready(job
->driver
->job_type
,
417 job
->speed
, &error_abort
);
420 BlockErrorAction
block_job_error_action(BlockJob
*job
, BlockDriverState
*bs
,
421 BlockdevOnError on_err
,
422 int is_read
, int error
)
424 BlockErrorAction action
;
427 case BLOCKDEV_ON_ERROR_ENOSPC
:
428 action
= (error
== ENOSPC
) ?
429 BLOCK_ERROR_ACTION_STOP
: BLOCK_ERROR_ACTION_REPORT
;
431 case BLOCKDEV_ON_ERROR_STOP
:
432 action
= BLOCK_ERROR_ACTION_STOP
;
434 case BLOCKDEV_ON_ERROR_REPORT
:
435 action
= BLOCK_ERROR_ACTION_REPORT
;
437 case BLOCKDEV_ON_ERROR_IGNORE
:
438 action
= BLOCK_ERROR_ACTION_IGNORE
;
443 qapi_event_send_block_job_error(job
->id
,
444 is_read
? IO_OPERATION_TYPE_READ
:
445 IO_OPERATION_TYPE_WRITE
,
446 action
, &error_abort
);
447 if (action
== BLOCK_ERROR_ACTION_STOP
) {
448 /* make the pause user visible, which will be resumed from QMP. */
449 job
->user_paused
= true;
450 block_job_pause(job
);
451 block_job_iostatus_set_err(job
, error
);
452 if (bs
->blk
&& bs
!= job
->bs
) {
453 blk_iostatus_set_err(bs
->blk
, error
);
462 AioContext
*aio_context
;
463 BlockJobDeferToMainLoopFn
*fn
;
465 } BlockJobDeferToMainLoopData
;
467 static void block_job_defer_to_main_loop_bh(void *opaque
)
469 BlockJobDeferToMainLoopData
*data
= opaque
;
470 AioContext
*aio_context
;
472 qemu_bh_delete(data
->bh
);
474 /* Prevent race with block_job_defer_to_main_loop() */
475 aio_context_acquire(data
->aio_context
);
477 /* Fetch BDS AioContext again, in case it has changed */
478 aio_context
= bdrv_get_aio_context(data
->job
->bs
);
479 aio_context_acquire(aio_context
);
481 data
->fn(data
->job
, data
->opaque
);
483 aio_context_release(aio_context
);
485 aio_context_release(data
->aio_context
);
490 void block_job_defer_to_main_loop(BlockJob
*job
,
491 BlockJobDeferToMainLoopFn
*fn
,
494 BlockJobDeferToMainLoopData
*data
= g_malloc(sizeof(*data
));
496 data
->bh
= qemu_bh_new(block_job_defer_to_main_loop_bh
, data
);
497 data
->aio_context
= bdrv_get_aio_context(job
->bs
);
499 data
->opaque
= opaque
;
501 qemu_bh_schedule(data
->bh
);
504 BlockJobTxn
*block_job_txn_new(void)
506 BlockJobTxn
*txn
= g_new0(BlockJobTxn
, 1);
507 QLIST_INIT(&txn
->jobs
);
512 static void block_job_txn_ref(BlockJobTxn
*txn
)
517 void block_job_txn_unref(BlockJobTxn
*txn
)
519 if (txn
&& --txn
->refcnt
== 0) {
524 void block_job_txn_add_job(BlockJobTxn
*txn
, BlockJob
*job
)
533 QLIST_INSERT_HEAD(&txn
->jobs
, job
, txn_list
);
534 block_job_txn_ref(txn
);