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"
37 #include "qmp-commands.h"
38 #include "qemu/timer.h"
39 #include "qapi-event.h"
41 /* Transactional group of block jobs */
44 /* Is this txn being cancelled? */
48 QLIST_HEAD(, BlockJob) jobs;
54 static QLIST_HEAD(, BlockJob) block_jobs = QLIST_HEAD_INITIALIZER(block_jobs);
56 BlockJob *block_job_next(BlockJob *job)
59 return QLIST_FIRST(&block_jobs);
61 return QLIST_NEXT(job, job_list);
64 BlockJob *block_job_get(const char *id)
68 QLIST_FOREACH(job, &block_jobs, job_list) {
69 if (!strcmp(id, job->id)) {
77 static void block_job_attached_aio_context(AioContext *new_context,
80 BlockJob *job = opaque;
82 if (job->driver->attached_aio_context) {
83 job->driver->attached_aio_context(job, new_context);
86 block_job_resume(job);
89 static void block_job_drain(BlockJob *job)
91 /* If job is !job->busy this kicks it into the next pause point. */
95 if (job->driver->drain) {
96 job->driver->drain(job);
100 static void block_job_detach_aio_context(void *opaque)
102 BlockJob *job = opaque;
104 /* In case the job terminates during aio_poll()... */
107 block_job_pause(job);
109 while (!job->paused && !job->completed) {
110 block_job_drain(job);
113 block_job_unref(job);
116 void *block_job_create(const char *job_id, const BlockJobDriver *driver,
117 BlockDriverState *bs, int64_t speed,
118 BlockCompletionFunc *cb, void *opaque, Error **errp)
125 error_setg(errp, QERR_DEVICE_IN_USE, bdrv_get_device_name(bs));
129 if (job_id == NULL) {
130 job_id = bdrv_get_device_name(bs);
132 error_setg(errp, "An explicit job ID is required for this node");
137 if (!id_wellformed(job_id)) {
138 error_setg(errp, "Invalid job ID '%s'", job_id);
142 if (block_job_get(job_id)) {
143 error_setg(errp, "Job ID '%s' already in use", job_id);
148 blk_insert_bs(blk, bs);
150 job = g_malloc0(driver->instance_size);
151 error_setg(&job->blocker, "block device is in use by block job: %s",
152 BlockJobType_lookup[driver->job_type]);
153 bdrv_op_block_all(bs, job->blocker);
154 bdrv_op_unblock(bs, BLOCK_OP_TYPE_DATAPLANE, job->blocker);
156 job->driver = driver;
157 job->id = g_strdup(job_id);
160 job->opaque = opaque;
165 QLIST_INSERT_HEAD(&block_jobs, job, job_list);
167 blk_add_aio_context_notifier(blk, block_job_attached_aio_context,
168 block_job_detach_aio_context, job);
170 /* Only set speed when necessary to avoid NotSupported error */
172 Error *local_err = NULL;
174 block_job_set_speed(job, speed, &local_err);
176 block_job_unref(job);
177 error_propagate(errp, local_err);
184 void block_job_ref(BlockJob *job)
189 void block_job_unref(BlockJob *job)
191 if (--job->refcnt == 0) {
192 BlockDriverState *bs = blk_bs(job->blk);
194 bdrv_op_unblock_all(bs, job->blocker);
195 blk_remove_aio_context_notifier(job->blk,
196 block_job_attached_aio_context,
197 block_job_detach_aio_context, job);
199 error_free(job->blocker);
201 QLIST_REMOVE(job, job_list);
206 static void block_job_completed_single(BlockJob *job)
209 if (job->driver->commit) {
210 job->driver->commit(job);
213 if (job->driver->abort) {
214 job->driver->abort(job);
217 job->cb(job->opaque, job->ret);
219 block_job_txn_unref(job->txn);
221 block_job_unref(job);
224 static void block_job_completed_txn_abort(BlockJob *job)
227 BlockJobTxn *txn = job->txn;
228 BlockJob *other_job, *next;
232 * We are cancelled by another job, which will handle everything.
236 txn->aborting = true;
237 /* We are the first failed job. Cancel other jobs. */
238 QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
239 ctx = blk_get_aio_context(other_job->blk);
240 aio_context_acquire(ctx);
242 QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
243 if (other_job == job || other_job->completed) {
244 /* Other jobs are "effectively" cancelled by us, set the status for
245 * them; this job, however, may or may not be cancelled, depending
246 * on the caller, so leave it. */
247 if (other_job != job) {
248 other_job->cancelled = true;
252 block_job_cancel_sync(other_job);
253 assert(other_job->completed);
255 QLIST_FOREACH_SAFE(other_job, &txn->jobs, txn_list, next) {
256 ctx = blk_get_aio_context(other_job->blk);
257 block_job_completed_single(other_job);
258 aio_context_release(ctx);
262 static void block_job_completed_txn_success(BlockJob *job)
265 BlockJobTxn *txn = job->txn;
266 BlockJob *other_job, *next;
268 * Successful completion, see if there are other running jobs in this
271 QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
272 if (!other_job->completed) {
276 /* We are the last completed job, commit the transaction. */
277 QLIST_FOREACH_SAFE(other_job, &txn->jobs, txn_list, next) {
278 ctx = blk_get_aio_context(other_job->blk);
279 aio_context_acquire(ctx);
280 assert(other_job->ret == 0);
281 block_job_completed_single(other_job);
282 aio_context_release(ctx);
286 void block_job_completed(BlockJob *job, int ret)
288 assert(blk_bs(job->blk)->job == job);
289 assert(!job->completed);
290 job->completed = true;
293 block_job_completed_single(job);
294 } else if (ret < 0 || block_job_is_cancelled(job)) {
295 block_job_completed_txn_abort(job);
297 block_job_completed_txn_success(job);
301 void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp)
303 Error *local_err = NULL;
305 if (!job->driver->set_speed) {
306 error_setg(errp, QERR_UNSUPPORTED);
309 job->driver->set_speed(job, speed, &local_err);
311 error_propagate(errp, local_err);
318 void block_job_complete(BlockJob *job, Error **errp)
320 if (job->pause_count || job->cancelled || !job->driver->complete) {
321 error_setg(errp, "The active block job '%s' cannot be completed",
326 job->driver->complete(job, errp);
329 void block_job_pause(BlockJob *job)
334 static bool block_job_should_pause(BlockJob *job)
336 return job->pause_count > 0;
339 void coroutine_fn block_job_pause_point(BlockJob *job)
341 if (!block_job_should_pause(job)) {
344 if (block_job_is_cancelled(job)) {
348 if (job->driver->pause) {
349 job->driver->pause(job);
352 if (block_job_should_pause(job) && !block_job_is_cancelled(job)) {
355 qemu_coroutine_yield(); /* wait for block_job_resume() */
360 if (job->driver->resume) {
361 job->driver->resume(job);
365 void block_job_resume(BlockJob *job)
367 assert(job->pause_count > 0);
369 if (job->pause_count) {
372 block_job_enter(job);
375 void block_job_enter(BlockJob *job)
377 if (job->co && !job->busy) {
378 qemu_coroutine_enter(job->co);
382 void block_job_cancel(BlockJob *job)
384 job->cancelled = true;
385 block_job_iostatus_reset(job);
386 block_job_enter(job);
389 bool block_job_is_cancelled(BlockJob *job)
391 return job->cancelled;
394 void block_job_iostatus_reset(BlockJob *job)
396 job->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
397 if (job->driver->iostatus_reset) {
398 job->driver->iostatus_reset(job);
402 static int block_job_finish_sync(BlockJob *job,
403 void (*finish)(BlockJob *, Error **errp),
406 Error *local_err = NULL;
409 assert(blk_bs(job->blk)->job == job);
413 finish(job, &local_err);
415 error_propagate(errp, local_err);
416 block_job_unref(job);
419 /* block_job_drain calls block_job_enter, and it should be enough to
420 * induce progress until the job completes or moves to the main thread.
422 while (!job->deferred_to_main_loop && !job->completed) {
423 block_job_drain(job);
425 while (!job->completed) {
426 aio_poll(qemu_get_aio_context(), true);
428 ret = (job->cancelled && job->ret == 0) ? -ECANCELED : job->ret;
429 block_job_unref(job);
433 /* A wrapper around block_job_cancel() taking an Error ** parameter so it may be
434 * used with block_job_finish_sync() without the need for (rather nasty)
435 * function pointer casts there. */
436 static void block_job_cancel_err(BlockJob *job, Error **errp)
438 block_job_cancel(job);
441 int block_job_cancel_sync(BlockJob *job)
443 return block_job_finish_sync(job, &block_job_cancel_err, NULL);
446 void block_job_cancel_sync_all(void)
449 AioContext *aio_context;
451 while ((job = QLIST_FIRST(&block_jobs))) {
452 aio_context = blk_get_aio_context(job->blk);
453 aio_context_acquire(aio_context);
454 block_job_cancel_sync(job);
455 aio_context_release(aio_context);
459 int block_job_complete_sync(BlockJob *job, Error **errp)
461 return block_job_finish_sync(job, &block_job_complete, errp);
464 void block_job_sleep_ns(BlockJob *job, QEMUClockType type, int64_t ns)
468 /* Check cancellation *before* setting busy = false, too! */
469 if (block_job_is_cancelled(job)) {
474 if (!block_job_should_pause(job)) {
475 co_aio_sleep_ns(blk_get_aio_context(job->blk), type, ns);
479 block_job_pause_point(job);
482 void block_job_yield(BlockJob *job)
486 /* Check cancellation *before* setting busy = false, too! */
487 if (block_job_is_cancelled(job)) {
492 if (!block_job_should_pause(job)) {
493 qemu_coroutine_yield();
497 block_job_pause_point(job);
500 BlockJobInfo *block_job_query(BlockJob *job)
502 BlockJobInfo *info = g_new0(BlockJobInfo, 1);
503 info->type = g_strdup(BlockJobType_lookup[job->driver->job_type]);
504 info->device = g_strdup(job->id);
505 info->len = job->len;
506 info->busy = job->busy;
507 info->paused = job->pause_count > 0;
508 info->offset = job->offset;
509 info->speed = job->speed;
510 info->io_status = job->iostatus;
511 info->ready = job->ready;
515 static void block_job_iostatus_set_err(BlockJob *job, int error)
517 if (job->iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
518 job->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE :
519 BLOCK_DEVICE_IO_STATUS_FAILED;
523 void block_job_event_cancelled(BlockJob *job)
525 qapi_event_send_block_job_cancelled(job->driver->job_type,
533 void block_job_event_completed(BlockJob *job, const char *msg)
535 qapi_event_send_block_job_completed(job->driver->job_type,
545 void block_job_event_ready(BlockJob *job)
549 qapi_event_send_block_job_ready(job->driver->job_type,
553 job->speed, &error_abort);
556 BlockErrorAction block_job_error_action(BlockJob *job, BlockdevOnError on_err,
557 int is_read, int error)
559 BlockErrorAction action;
562 case BLOCKDEV_ON_ERROR_ENOSPC:
563 case BLOCKDEV_ON_ERROR_AUTO:
564 action = (error == ENOSPC) ?
565 BLOCK_ERROR_ACTION_STOP : BLOCK_ERROR_ACTION_REPORT;
567 case BLOCKDEV_ON_ERROR_STOP:
568 action = BLOCK_ERROR_ACTION_STOP;
570 case BLOCKDEV_ON_ERROR_REPORT:
571 action = BLOCK_ERROR_ACTION_REPORT;
573 case BLOCKDEV_ON_ERROR_IGNORE:
574 action = BLOCK_ERROR_ACTION_IGNORE;
579 qapi_event_send_block_job_error(job->id,
580 is_read ? IO_OPERATION_TYPE_READ :
581 IO_OPERATION_TYPE_WRITE,
582 action, &error_abort);
583 if (action == BLOCK_ERROR_ACTION_STOP) {
584 /* make the pause user visible, which will be resumed from QMP. */
585 job->user_paused = true;
586 block_job_pause(job);
587 block_job_iostatus_set_err(job, error);
594 AioContext *aio_context;
595 BlockJobDeferToMainLoopFn *fn;
597 } BlockJobDeferToMainLoopData;
599 static void block_job_defer_to_main_loop_bh(void *opaque)
601 BlockJobDeferToMainLoopData *data = opaque;
602 AioContext *aio_context;
604 /* Prevent race with block_job_defer_to_main_loop() */
605 aio_context_acquire(data->aio_context);
607 /* Fetch BDS AioContext again, in case it has changed */
608 aio_context = blk_get_aio_context(data->job->blk);
609 aio_context_acquire(aio_context);
611 data->job->deferred_to_main_loop = false;
612 data->fn(data->job, data->opaque);
614 aio_context_release(aio_context);
616 aio_context_release(data->aio_context);
621 void block_job_defer_to_main_loop(BlockJob *job,
622 BlockJobDeferToMainLoopFn *fn,
625 BlockJobDeferToMainLoopData *data = g_malloc(sizeof(*data));
627 data->aio_context = blk_get_aio_context(job->blk);
629 data->opaque = opaque;
630 job->deferred_to_main_loop = true;
632 aio_bh_schedule_oneshot(qemu_get_aio_context(),
633 block_job_defer_to_main_loop_bh, data);
636 BlockJobTxn *block_job_txn_new(void)
638 BlockJobTxn *txn = g_new0(BlockJobTxn, 1);
639 QLIST_INIT(&txn->jobs);
644 static void block_job_txn_ref(BlockJobTxn *txn)
649 void block_job_txn_unref(BlockJobTxn *txn)
651 if (txn && --txn->refcnt == 0) {
656 void block_job_txn_add_job(BlockJobTxn *txn, BlockJob *job)
665 QLIST_INSERT_HEAD(&txn->jobs, job, txn_list);
666 block_job_txn_ref(txn);