docs: fix COLO architecture diagram
[qemu.git] / blockjob.c
blob4aa14a4974b602d0063d97c5107b54470391ed59
1 /*
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
23 * THE SOFTWARE.
26 #include "qemu/osdep.h"
27 #include "qemu-common.h"
28 #include "trace.h"
29 #include "block/block.h"
30 #include "block/blockjob_int.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 "qemu/id.h"
37 #include "qmp-commands.h"
38 #include "qemu/timer.h"
39 #include "qapi-event.h"
41 static void block_job_event_cancelled(BlockJob *job);
42 static void block_job_event_completed(BlockJob *job, const char *msg);
44 /* Transactional group of block jobs */
45 struct BlockJobTxn {
47 /* Is this txn being cancelled? */
48 bool aborting;
50 /* List of jobs */
51 QLIST_HEAD(, BlockJob) jobs;
53 /* Reference count */
54 int refcnt;
57 static QLIST_HEAD(, BlockJob) block_jobs = QLIST_HEAD_INITIALIZER(block_jobs);
59 BlockJob *block_job_next(BlockJob *job)
61 if (!job) {
62 return QLIST_FIRST(&block_jobs);
64 return QLIST_NEXT(job, job_list);
67 BlockJob *block_job_get(const char *id)
69 BlockJob *job;
71 QLIST_FOREACH(job, &block_jobs, job_list) {
72 if (job->id && !strcmp(id, job->id)) {
73 return job;
77 return NULL;
80 static void block_job_attached_aio_context(AioContext *new_context,
81 void *opaque)
83 BlockJob *job = opaque;
85 if (job->driver->attached_aio_context) {
86 job->driver->attached_aio_context(job, new_context);
89 block_job_resume(job);
92 static void block_job_drain(BlockJob *job)
94 /* If job is !job->busy this kicks it into the next pause point. */
95 block_job_enter(job);
97 blk_drain(job->blk);
98 if (job->driver->drain) {
99 job->driver->drain(job);
103 static void block_job_detach_aio_context(void *opaque)
105 BlockJob *job = opaque;
107 /* In case the job terminates during aio_poll()... */
108 block_job_ref(job);
110 block_job_pause(job);
112 while (!job->paused && !job->completed) {
113 block_job_drain(job);
116 block_job_unref(job);
119 void block_job_add_bdrv(BlockJob *job, BlockDriverState *bs)
121 job->nodes = g_slist_prepend(job->nodes, bs);
122 bdrv_ref(bs);
123 bdrv_op_block_all(bs, job->blocker);
126 void *block_job_create(const char *job_id, const BlockJobDriver *driver,
127 BlockDriverState *bs, int64_t speed, int flags,
128 BlockCompletionFunc *cb, void *opaque, Error **errp)
130 BlockBackend *blk;
131 BlockJob *job;
133 if (bs->job) {
134 error_setg(errp, QERR_DEVICE_IN_USE, bdrv_get_device_name(bs));
135 return NULL;
138 if (job_id == NULL && !(flags & BLOCK_JOB_INTERNAL)) {
139 job_id = bdrv_get_device_name(bs);
140 if (!*job_id) {
141 error_setg(errp, "An explicit job ID is required for this node");
142 return NULL;
146 if (job_id) {
147 if (flags & BLOCK_JOB_INTERNAL) {
148 error_setg(errp, "Cannot specify job ID for internal block job");
149 return NULL;
152 if (!id_wellformed(job_id)) {
153 error_setg(errp, "Invalid job ID '%s'", job_id);
154 return NULL;
157 if (block_job_get(job_id)) {
158 error_setg(errp, "Job ID '%s' already in use", job_id);
159 return NULL;
163 blk = blk_new();
164 blk_insert_bs(blk, bs);
166 job = g_malloc0(driver->instance_size);
167 error_setg(&job->blocker, "block device is in use by block job: %s",
168 BlockJobType_lookup[driver->job_type]);
169 block_job_add_bdrv(job, bs);
170 bdrv_op_unblock(bs, BLOCK_OP_TYPE_DATAPLANE, job->blocker);
172 job->driver = driver;
173 job->id = g_strdup(job_id);
174 job->blk = blk;
175 job->cb = cb;
176 job->opaque = opaque;
177 job->busy = true;
178 job->refcnt = 1;
179 bs->job = job;
181 QLIST_INSERT_HEAD(&block_jobs, job, job_list);
183 blk_add_aio_context_notifier(blk, block_job_attached_aio_context,
184 block_job_detach_aio_context, job);
186 /* Only set speed when necessary to avoid NotSupported error */
187 if (speed != 0) {
188 Error *local_err = NULL;
190 block_job_set_speed(job, speed, &local_err);
191 if (local_err) {
192 block_job_unref(job);
193 error_propagate(errp, local_err);
194 return NULL;
197 return job;
200 bool block_job_is_internal(BlockJob *job)
202 return (job->id == NULL);
205 void block_job_ref(BlockJob *job)
207 ++job->refcnt;
210 void block_job_unref(BlockJob *job)
212 if (--job->refcnt == 0) {
213 GSList *l;
214 BlockDriverState *bs = blk_bs(job->blk);
215 bs->job = NULL;
216 for (l = job->nodes; l; l = l->next) {
217 bs = l->data;
218 bdrv_op_unblock_all(bs, job->blocker);
219 bdrv_unref(bs);
221 g_slist_free(job->nodes);
222 blk_remove_aio_context_notifier(job->blk,
223 block_job_attached_aio_context,
224 block_job_detach_aio_context, job);
225 blk_unref(job->blk);
226 error_free(job->blocker);
227 g_free(job->id);
228 QLIST_REMOVE(job, job_list);
229 g_free(job);
233 static void block_job_completed_single(BlockJob *job)
235 if (!job->ret) {
236 if (job->driver->commit) {
237 job->driver->commit(job);
239 } else {
240 if (job->driver->abort) {
241 job->driver->abort(job);
245 if (job->cb) {
246 job->cb(job->opaque, job->ret);
248 if (block_job_is_cancelled(job)) {
249 block_job_event_cancelled(job);
250 } else {
251 const char *msg = NULL;
252 if (job->ret < 0) {
253 msg = strerror(-job->ret);
255 block_job_event_completed(job, msg);
258 if (job->txn) {
259 block_job_txn_unref(job->txn);
261 block_job_unref(job);
264 static void block_job_completed_txn_abort(BlockJob *job)
266 AioContext *ctx;
267 BlockJobTxn *txn = job->txn;
268 BlockJob *other_job, *next;
270 if (txn->aborting) {
272 * We are cancelled by another job, which will handle everything.
274 return;
276 txn->aborting = true;
277 /* We are the first failed job. Cancel other jobs. */
278 QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
279 ctx = blk_get_aio_context(other_job->blk);
280 aio_context_acquire(ctx);
282 QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
283 if (other_job == job || other_job->completed) {
284 /* Other jobs are "effectively" cancelled by us, set the status for
285 * them; this job, however, may or may not be cancelled, depending
286 * on the caller, so leave it. */
287 if (other_job != job) {
288 other_job->cancelled = true;
290 continue;
292 block_job_cancel_sync(other_job);
293 assert(other_job->completed);
295 QLIST_FOREACH_SAFE(other_job, &txn->jobs, txn_list, next) {
296 ctx = blk_get_aio_context(other_job->blk);
297 block_job_completed_single(other_job);
298 aio_context_release(ctx);
302 static void block_job_completed_txn_success(BlockJob *job)
304 AioContext *ctx;
305 BlockJobTxn *txn = job->txn;
306 BlockJob *other_job, *next;
308 * Successful completion, see if there are other running jobs in this
309 * txn.
311 QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
312 if (!other_job->completed) {
313 return;
316 /* We are the last completed job, commit the transaction. */
317 QLIST_FOREACH_SAFE(other_job, &txn->jobs, txn_list, next) {
318 ctx = blk_get_aio_context(other_job->blk);
319 aio_context_acquire(ctx);
320 assert(other_job->ret == 0);
321 block_job_completed_single(other_job);
322 aio_context_release(ctx);
326 void block_job_completed(BlockJob *job, int ret)
328 assert(blk_bs(job->blk)->job == job);
329 assert(!job->completed);
330 job->completed = true;
331 job->ret = ret;
332 if (!job->txn) {
333 block_job_completed_single(job);
334 } else if (ret < 0 || block_job_is_cancelled(job)) {
335 block_job_completed_txn_abort(job);
336 } else {
337 block_job_completed_txn_success(job);
341 void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp)
343 Error *local_err = NULL;
345 if (!job->driver->set_speed) {
346 error_setg(errp, QERR_UNSUPPORTED);
347 return;
349 job->driver->set_speed(job, speed, &local_err);
350 if (local_err) {
351 error_propagate(errp, local_err);
352 return;
355 job->speed = speed;
358 void block_job_complete(BlockJob *job, Error **errp)
360 /* Should not be reachable via external interface for internal jobs */
361 assert(job->id);
362 if (job->pause_count || job->cancelled || !job->driver->complete) {
363 error_setg(errp, "The active block job '%s' cannot be completed",
364 job->id);
365 return;
368 job->driver->complete(job, errp);
371 void block_job_pause(BlockJob *job)
373 job->pause_count++;
376 void block_job_user_pause(BlockJob *job)
378 job->user_paused = true;
379 block_job_pause(job);
382 static bool block_job_should_pause(BlockJob *job)
384 return job->pause_count > 0;
387 bool block_job_user_paused(BlockJob *job)
389 return job ? job->user_paused : 0;
392 void coroutine_fn block_job_pause_point(BlockJob *job)
394 if (!block_job_should_pause(job)) {
395 return;
397 if (block_job_is_cancelled(job)) {
398 return;
401 if (job->driver->pause) {
402 job->driver->pause(job);
405 if (block_job_should_pause(job) && !block_job_is_cancelled(job)) {
406 job->paused = true;
407 job->busy = false;
408 qemu_coroutine_yield(); /* wait for block_job_resume() */
409 job->busy = true;
410 job->paused = false;
413 if (job->driver->resume) {
414 job->driver->resume(job);
418 void block_job_resume(BlockJob *job)
420 assert(job->pause_count > 0);
421 job->pause_count--;
422 if (job->pause_count) {
423 return;
425 block_job_enter(job);
428 void block_job_user_resume(BlockJob *job)
430 if (job && job->user_paused && job->pause_count > 0) {
431 job->user_paused = false;
432 block_job_resume(job);
436 void block_job_enter(BlockJob *job)
438 if (job->co && !job->busy) {
439 qemu_coroutine_enter(job->co);
443 void block_job_cancel(BlockJob *job)
445 job->cancelled = true;
446 block_job_iostatus_reset(job);
447 block_job_enter(job);
450 bool block_job_is_cancelled(BlockJob *job)
452 return job->cancelled;
455 void block_job_iostatus_reset(BlockJob *job)
457 job->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
458 if (job->driver->iostatus_reset) {
459 job->driver->iostatus_reset(job);
463 static int block_job_finish_sync(BlockJob *job,
464 void (*finish)(BlockJob *, Error **errp),
465 Error **errp)
467 Error *local_err = NULL;
468 int ret;
470 assert(blk_bs(job->blk)->job == job);
472 block_job_ref(job);
474 finish(job, &local_err);
475 if (local_err) {
476 error_propagate(errp, local_err);
477 block_job_unref(job);
478 return -EBUSY;
480 /* block_job_drain calls block_job_enter, and it should be enough to
481 * induce progress until the job completes or moves to the main thread.
483 while (!job->deferred_to_main_loop && !job->completed) {
484 block_job_drain(job);
486 while (!job->completed) {
487 aio_poll(qemu_get_aio_context(), true);
489 ret = (job->cancelled && job->ret == 0) ? -ECANCELED : job->ret;
490 block_job_unref(job);
491 return ret;
494 /* A wrapper around block_job_cancel() taking an Error ** parameter so it may be
495 * used with block_job_finish_sync() without the need for (rather nasty)
496 * function pointer casts there. */
497 static void block_job_cancel_err(BlockJob *job, Error **errp)
499 block_job_cancel(job);
502 int block_job_cancel_sync(BlockJob *job)
504 return block_job_finish_sync(job, &block_job_cancel_err, NULL);
507 void block_job_cancel_sync_all(void)
509 BlockJob *job;
510 AioContext *aio_context;
512 while ((job = QLIST_FIRST(&block_jobs))) {
513 aio_context = blk_get_aio_context(job->blk);
514 aio_context_acquire(aio_context);
515 block_job_cancel_sync(job);
516 aio_context_release(aio_context);
520 int block_job_complete_sync(BlockJob *job, Error **errp)
522 return block_job_finish_sync(job, &block_job_complete, errp);
525 void block_job_sleep_ns(BlockJob *job, QEMUClockType type, int64_t ns)
527 assert(job->busy);
529 /* Check cancellation *before* setting busy = false, too! */
530 if (block_job_is_cancelled(job)) {
531 return;
534 job->busy = false;
535 if (!block_job_should_pause(job)) {
536 co_aio_sleep_ns(blk_get_aio_context(job->blk), type, ns);
538 job->busy = true;
540 block_job_pause_point(job);
543 void block_job_yield(BlockJob *job)
545 assert(job->busy);
547 /* Check cancellation *before* setting busy = false, too! */
548 if (block_job_is_cancelled(job)) {
549 return;
552 job->busy = false;
553 if (!block_job_should_pause(job)) {
554 qemu_coroutine_yield();
556 job->busy = true;
558 block_job_pause_point(job);
561 BlockJobInfo *block_job_query(BlockJob *job, Error **errp)
563 BlockJobInfo *info;
565 if (block_job_is_internal(job)) {
566 error_setg(errp, "Cannot query QEMU internal jobs");
567 return NULL;
569 info = g_new0(BlockJobInfo, 1);
570 info->type = g_strdup(BlockJobType_lookup[job->driver->job_type]);
571 info->device = g_strdup(job->id);
572 info->len = job->len;
573 info->busy = job->busy;
574 info->paused = job->pause_count > 0;
575 info->offset = job->offset;
576 info->speed = job->speed;
577 info->io_status = job->iostatus;
578 info->ready = job->ready;
579 return info;
582 static void block_job_iostatus_set_err(BlockJob *job, int error)
584 if (job->iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
585 job->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE :
586 BLOCK_DEVICE_IO_STATUS_FAILED;
590 static void block_job_event_cancelled(BlockJob *job)
592 if (block_job_is_internal(job)) {
593 return;
596 qapi_event_send_block_job_cancelled(job->driver->job_type,
597 job->id,
598 job->len,
599 job->offset,
600 job->speed,
601 &error_abort);
604 static void block_job_event_completed(BlockJob *job, const char *msg)
606 if (block_job_is_internal(job)) {
607 return;
610 qapi_event_send_block_job_completed(job->driver->job_type,
611 job->id,
612 job->len,
613 job->offset,
614 job->speed,
615 !!msg,
616 msg,
617 &error_abort);
620 void block_job_event_ready(BlockJob *job)
622 job->ready = true;
624 if (block_job_is_internal(job)) {
625 return;
628 qapi_event_send_block_job_ready(job->driver->job_type,
629 job->id,
630 job->len,
631 job->offset,
632 job->speed, &error_abort);
635 BlockErrorAction block_job_error_action(BlockJob *job, BlockdevOnError on_err,
636 int is_read, int error)
638 BlockErrorAction action;
640 switch (on_err) {
641 case BLOCKDEV_ON_ERROR_ENOSPC:
642 case BLOCKDEV_ON_ERROR_AUTO:
643 action = (error == ENOSPC) ?
644 BLOCK_ERROR_ACTION_STOP : BLOCK_ERROR_ACTION_REPORT;
645 break;
646 case BLOCKDEV_ON_ERROR_STOP:
647 action = BLOCK_ERROR_ACTION_STOP;
648 break;
649 case BLOCKDEV_ON_ERROR_REPORT:
650 action = BLOCK_ERROR_ACTION_REPORT;
651 break;
652 case BLOCKDEV_ON_ERROR_IGNORE:
653 action = BLOCK_ERROR_ACTION_IGNORE;
654 break;
655 default:
656 abort();
658 if (!block_job_is_internal(job)) {
659 qapi_event_send_block_job_error(job->id,
660 is_read ? IO_OPERATION_TYPE_READ :
661 IO_OPERATION_TYPE_WRITE,
662 action, &error_abort);
664 if (action == BLOCK_ERROR_ACTION_STOP) {
665 /* make the pause user visible, which will be resumed from QMP. */
666 block_job_user_pause(job);
667 block_job_iostatus_set_err(job, error);
669 return action;
672 typedef struct {
673 BlockJob *job;
674 AioContext *aio_context;
675 BlockJobDeferToMainLoopFn *fn;
676 void *opaque;
677 } BlockJobDeferToMainLoopData;
679 static void block_job_defer_to_main_loop_bh(void *opaque)
681 BlockJobDeferToMainLoopData *data = opaque;
682 AioContext *aio_context;
684 /* Prevent race with block_job_defer_to_main_loop() */
685 aio_context_acquire(data->aio_context);
687 /* Fetch BDS AioContext again, in case it has changed */
688 aio_context = blk_get_aio_context(data->job->blk);
689 aio_context_acquire(aio_context);
691 data->job->deferred_to_main_loop = false;
692 data->fn(data->job, data->opaque);
694 aio_context_release(aio_context);
696 aio_context_release(data->aio_context);
698 g_free(data);
701 void block_job_defer_to_main_loop(BlockJob *job,
702 BlockJobDeferToMainLoopFn *fn,
703 void *opaque)
705 BlockJobDeferToMainLoopData *data = g_malloc(sizeof(*data));
706 data->job = job;
707 data->aio_context = blk_get_aio_context(job->blk);
708 data->fn = fn;
709 data->opaque = opaque;
710 job->deferred_to_main_loop = true;
712 aio_bh_schedule_oneshot(qemu_get_aio_context(),
713 block_job_defer_to_main_loop_bh, data);
716 BlockJobTxn *block_job_txn_new(void)
718 BlockJobTxn *txn = g_new0(BlockJobTxn, 1);
719 QLIST_INIT(&txn->jobs);
720 txn->refcnt = 1;
721 return txn;
724 static void block_job_txn_ref(BlockJobTxn *txn)
726 txn->refcnt++;
729 void block_job_txn_unref(BlockJobTxn *txn)
731 if (txn && --txn->refcnt == 0) {
732 g_free(txn);
736 void block_job_txn_add_job(BlockJobTxn *txn, BlockJob *job)
738 if (!txn) {
739 return;
742 assert(!job->txn);
743 job->txn = txn;
745 QLIST_INSERT_HEAD(&txn->jobs, job, txn_list);
746 block_job_txn_ref(txn);