tcg: remove global exit_request
[qemu/ar7.git] / blockjob.c
blobabee11bb080b1c513c7f4a51416cff55480db469
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 "block/block.h"
29 #include "block/blockjob_int.h"
30 #include "block/block_int.h"
31 #include "sysemu/block-backend.h"
32 #include "qapi/qmp/qerror.h"
33 #include "qapi/qmp/qjson.h"
34 #include "qemu/coroutine.h"
35 #include "qemu/id.h"
36 #include "qmp-commands.h"
37 #include "qemu/timer.h"
38 #include "qapi-event.h"
40 static void block_job_event_cancelled(BlockJob *job);
41 static void block_job_event_completed(BlockJob *job, const char *msg);
43 /* Transactional group of block jobs */
44 struct BlockJobTxn {
46 /* Is this txn being cancelled? */
47 bool aborting;
49 /* List of jobs */
50 QLIST_HEAD(, BlockJob) jobs;
52 /* Reference count */
53 int refcnt;
56 static QLIST_HEAD(, BlockJob) block_jobs = QLIST_HEAD_INITIALIZER(block_jobs);
58 BlockJob *block_job_next(BlockJob *job)
60 if (!job) {
61 return QLIST_FIRST(&block_jobs);
63 return QLIST_NEXT(job, job_list);
66 BlockJob *block_job_get(const char *id)
68 BlockJob *job;
70 QLIST_FOREACH(job, &block_jobs, job_list) {
71 if (job->id && !strcmp(id, job->id)) {
72 return job;
76 return NULL;
79 static void block_job_attached_aio_context(AioContext *new_context,
80 void *opaque)
82 BlockJob *job = opaque;
84 if (job->driver->attached_aio_context) {
85 job->driver->attached_aio_context(job, new_context);
88 block_job_resume(job);
91 static void block_job_drain(BlockJob *job)
93 /* If job is !job->busy this kicks it into the next pause point. */
94 block_job_enter(job);
96 blk_drain(job->blk);
97 if (job->driver->drain) {
98 job->driver->drain(job);
102 static void block_job_detach_aio_context(void *opaque)
104 BlockJob *job = opaque;
106 /* In case the job terminates during aio_poll()... */
107 block_job_ref(job);
109 block_job_pause(job);
111 while (!job->paused && !job->completed) {
112 block_job_drain(job);
115 block_job_unref(job);
118 void block_job_add_bdrv(BlockJob *job, BlockDriverState *bs)
120 job->nodes = g_slist_prepend(job->nodes, bs);
121 bdrv_ref(bs);
122 bdrv_op_block_all(bs, job->blocker);
125 void *block_job_create(const char *job_id, const BlockJobDriver *driver,
126 BlockDriverState *bs, int64_t speed, int flags,
127 BlockCompletionFunc *cb, void *opaque, Error **errp)
129 BlockBackend *blk;
130 BlockJob *job;
132 if (bs->job) {
133 error_setg(errp, QERR_DEVICE_IN_USE, bdrv_get_device_name(bs));
134 return NULL;
137 if (job_id == NULL && !(flags & BLOCK_JOB_INTERNAL)) {
138 job_id = bdrv_get_device_name(bs);
139 if (!*job_id) {
140 error_setg(errp, "An explicit job ID is required for this node");
141 return NULL;
145 if (job_id) {
146 if (flags & BLOCK_JOB_INTERNAL) {
147 error_setg(errp, "Cannot specify job ID for internal block job");
148 return NULL;
151 if (!id_wellformed(job_id)) {
152 error_setg(errp, "Invalid job ID '%s'", job_id);
153 return NULL;
156 if (block_job_get(job_id)) {
157 error_setg(errp, "Job ID '%s' already in use", job_id);
158 return NULL;
162 blk = blk_new();
163 blk_insert_bs(blk, bs);
165 job = g_malloc0(driver->instance_size);
166 error_setg(&job->blocker, "block device is in use by block job: %s",
167 BlockJobType_lookup[driver->job_type]);
168 block_job_add_bdrv(job, bs);
169 bdrv_op_unblock(bs, BLOCK_OP_TYPE_DATAPLANE, job->blocker);
171 job->driver = driver;
172 job->id = g_strdup(job_id);
173 job->blk = blk;
174 job->cb = cb;
175 job->opaque = opaque;
176 job->busy = false;
177 job->paused = true;
178 job->pause_count = 1;
179 job->refcnt = 1;
180 bs->job = job;
182 QLIST_INSERT_HEAD(&block_jobs, job, job_list);
184 blk_add_aio_context_notifier(blk, block_job_attached_aio_context,
185 block_job_detach_aio_context, job);
187 /* Only set speed when necessary to avoid NotSupported error */
188 if (speed != 0) {
189 Error *local_err = NULL;
191 block_job_set_speed(job, speed, &local_err);
192 if (local_err) {
193 block_job_unref(job);
194 error_propagate(errp, local_err);
195 return NULL;
198 return job;
201 bool block_job_is_internal(BlockJob *job)
203 return (job->id == NULL);
206 static bool block_job_started(BlockJob *job)
208 return job->co;
211 void block_job_start(BlockJob *job)
213 assert(job && !block_job_started(job) && job->paused &&
214 !job->busy && job->driver->start);
215 job->co = qemu_coroutine_create(job->driver->start, job);
216 if (--job->pause_count == 0) {
217 job->paused = false;
218 job->busy = true;
219 qemu_coroutine_enter(job->co);
223 void block_job_ref(BlockJob *job)
225 ++job->refcnt;
228 void block_job_unref(BlockJob *job)
230 if (--job->refcnt == 0) {
231 GSList *l;
232 BlockDriverState *bs = blk_bs(job->blk);
233 bs->job = NULL;
234 for (l = job->nodes; l; l = l->next) {
235 bs = l->data;
236 bdrv_op_unblock_all(bs, job->blocker);
237 bdrv_unref(bs);
239 g_slist_free(job->nodes);
240 blk_remove_aio_context_notifier(job->blk,
241 block_job_attached_aio_context,
242 block_job_detach_aio_context, job);
243 blk_unref(job->blk);
244 error_free(job->blocker);
245 g_free(job->id);
246 QLIST_REMOVE(job, job_list);
247 g_free(job);
251 static void block_job_completed_single(BlockJob *job)
253 if (!job->ret) {
254 if (job->driver->commit) {
255 job->driver->commit(job);
257 } else {
258 if (job->driver->abort) {
259 job->driver->abort(job);
262 if (job->driver->clean) {
263 job->driver->clean(job);
266 if (job->cb) {
267 job->cb(job->opaque, job->ret);
270 /* Emit events only if we actually started */
271 if (block_job_started(job)) {
272 if (block_job_is_cancelled(job)) {
273 block_job_event_cancelled(job);
274 } else {
275 const char *msg = NULL;
276 if (job->ret < 0) {
277 msg = strerror(-job->ret);
279 block_job_event_completed(job, msg);
283 if (job->txn) {
284 QLIST_REMOVE(job, txn_list);
285 block_job_txn_unref(job->txn);
287 block_job_unref(job);
290 static void block_job_completed_txn_abort(BlockJob *job)
292 AioContext *ctx;
293 BlockJobTxn *txn = job->txn;
294 BlockJob *other_job, *next;
296 if (txn->aborting) {
298 * We are cancelled by another job, which will handle everything.
300 return;
302 txn->aborting = true;
303 /* We are the first failed job. Cancel other jobs. */
304 QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
305 ctx = blk_get_aio_context(other_job->blk);
306 aio_context_acquire(ctx);
308 QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
309 if (other_job == job || other_job->completed) {
310 /* Other jobs are "effectively" cancelled by us, set the status for
311 * them; this job, however, may or may not be cancelled, depending
312 * on the caller, so leave it. */
313 if (other_job != job) {
314 other_job->cancelled = true;
316 continue;
318 block_job_cancel_sync(other_job);
319 assert(other_job->completed);
321 QLIST_FOREACH_SAFE(other_job, &txn->jobs, txn_list, next) {
322 ctx = blk_get_aio_context(other_job->blk);
323 block_job_completed_single(other_job);
324 aio_context_release(ctx);
328 static void block_job_completed_txn_success(BlockJob *job)
330 AioContext *ctx;
331 BlockJobTxn *txn = job->txn;
332 BlockJob *other_job, *next;
334 * Successful completion, see if there are other running jobs in this
335 * txn.
337 QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
338 if (!other_job->completed) {
339 return;
342 /* We are the last completed job, commit the transaction. */
343 QLIST_FOREACH_SAFE(other_job, &txn->jobs, txn_list, next) {
344 ctx = blk_get_aio_context(other_job->blk);
345 aio_context_acquire(ctx);
346 assert(other_job->ret == 0);
347 block_job_completed_single(other_job);
348 aio_context_release(ctx);
352 void block_job_completed(BlockJob *job, int ret)
354 assert(blk_bs(job->blk)->job == job);
355 assert(!job->completed);
356 job->completed = true;
357 job->ret = ret;
358 if (!job->txn) {
359 block_job_completed_single(job);
360 } else if (ret < 0 || block_job_is_cancelled(job)) {
361 block_job_completed_txn_abort(job);
362 } else {
363 block_job_completed_txn_success(job);
367 void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp)
369 Error *local_err = NULL;
371 if (!job->driver->set_speed) {
372 error_setg(errp, QERR_UNSUPPORTED);
373 return;
375 job->driver->set_speed(job, speed, &local_err);
376 if (local_err) {
377 error_propagate(errp, local_err);
378 return;
381 job->speed = speed;
384 void block_job_complete(BlockJob *job, Error **errp)
386 /* Should not be reachable via external interface for internal jobs */
387 assert(job->id);
388 if (job->pause_count || job->cancelled ||
389 !block_job_started(job) || !job->driver->complete) {
390 error_setg(errp, "The active block job '%s' cannot be completed",
391 job->id);
392 return;
395 job->driver->complete(job, errp);
398 void block_job_pause(BlockJob *job)
400 job->pause_count++;
403 void block_job_user_pause(BlockJob *job)
405 job->user_paused = true;
406 block_job_pause(job);
409 static bool block_job_should_pause(BlockJob *job)
411 return job->pause_count > 0;
414 bool block_job_user_paused(BlockJob *job)
416 return job ? job->user_paused : 0;
419 void coroutine_fn block_job_pause_point(BlockJob *job)
421 assert(job && block_job_started(job));
423 if (!block_job_should_pause(job)) {
424 return;
426 if (block_job_is_cancelled(job)) {
427 return;
430 if (job->driver->pause) {
431 job->driver->pause(job);
434 if (block_job_should_pause(job) && !block_job_is_cancelled(job)) {
435 job->paused = true;
436 job->busy = false;
437 qemu_coroutine_yield(); /* wait for block_job_resume() */
438 job->busy = true;
439 job->paused = false;
442 if (job->driver->resume) {
443 job->driver->resume(job);
447 void block_job_resume(BlockJob *job)
449 assert(job->pause_count > 0);
450 job->pause_count--;
451 if (job->pause_count) {
452 return;
454 block_job_enter(job);
457 void block_job_user_resume(BlockJob *job)
459 if (job && job->user_paused && job->pause_count > 0) {
460 job->user_paused = false;
461 block_job_resume(job);
465 void block_job_enter(BlockJob *job)
467 if (job->co && !job->busy) {
468 qemu_coroutine_enter(job->co);
472 void block_job_cancel(BlockJob *job)
474 if (block_job_started(job)) {
475 job->cancelled = true;
476 block_job_iostatus_reset(job);
477 block_job_enter(job);
478 } else {
479 block_job_completed(job, -ECANCELED);
483 bool block_job_is_cancelled(BlockJob *job)
485 return job->cancelled;
488 void block_job_iostatus_reset(BlockJob *job)
490 job->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
491 if (job->driver->iostatus_reset) {
492 job->driver->iostatus_reset(job);
496 static int block_job_finish_sync(BlockJob *job,
497 void (*finish)(BlockJob *, Error **errp),
498 Error **errp)
500 Error *local_err = NULL;
501 int ret;
503 assert(blk_bs(job->blk)->job == job);
505 block_job_ref(job);
507 finish(job, &local_err);
508 if (local_err) {
509 error_propagate(errp, local_err);
510 block_job_unref(job);
511 return -EBUSY;
513 /* block_job_drain calls block_job_enter, and it should be enough to
514 * induce progress until the job completes or moves to the main thread.
516 while (!job->deferred_to_main_loop && !job->completed) {
517 block_job_drain(job);
519 while (!job->completed) {
520 aio_poll(qemu_get_aio_context(), true);
522 ret = (job->cancelled && job->ret == 0) ? -ECANCELED : job->ret;
523 block_job_unref(job);
524 return ret;
527 /* A wrapper around block_job_cancel() taking an Error ** parameter so it may be
528 * used with block_job_finish_sync() without the need for (rather nasty)
529 * function pointer casts there. */
530 static void block_job_cancel_err(BlockJob *job, Error **errp)
532 block_job_cancel(job);
535 int block_job_cancel_sync(BlockJob *job)
537 return block_job_finish_sync(job, &block_job_cancel_err, NULL);
540 void block_job_cancel_sync_all(void)
542 BlockJob *job;
543 AioContext *aio_context;
545 while ((job = QLIST_FIRST(&block_jobs))) {
546 aio_context = blk_get_aio_context(job->blk);
547 aio_context_acquire(aio_context);
548 block_job_cancel_sync(job);
549 aio_context_release(aio_context);
553 int block_job_complete_sync(BlockJob *job, Error **errp)
555 return block_job_finish_sync(job, &block_job_complete, errp);
558 void block_job_sleep_ns(BlockJob *job, QEMUClockType type, int64_t ns)
560 assert(job->busy);
562 /* Check cancellation *before* setting busy = false, too! */
563 if (block_job_is_cancelled(job)) {
564 return;
567 job->busy = false;
568 if (!block_job_should_pause(job)) {
569 co_aio_sleep_ns(blk_get_aio_context(job->blk), type, ns);
571 job->busy = true;
573 block_job_pause_point(job);
576 void block_job_yield(BlockJob *job)
578 assert(job->busy);
580 /* Check cancellation *before* setting busy = false, too! */
581 if (block_job_is_cancelled(job)) {
582 return;
585 job->busy = false;
586 if (!block_job_should_pause(job)) {
587 qemu_coroutine_yield();
589 job->busy = true;
591 block_job_pause_point(job);
594 BlockJobInfo *block_job_query(BlockJob *job, Error **errp)
596 BlockJobInfo *info;
598 if (block_job_is_internal(job)) {
599 error_setg(errp, "Cannot query QEMU internal jobs");
600 return NULL;
602 info = g_new0(BlockJobInfo, 1);
603 info->type = g_strdup(BlockJobType_lookup[job->driver->job_type]);
604 info->device = g_strdup(job->id);
605 info->len = job->len;
606 info->busy = job->busy;
607 info->paused = job->pause_count > 0;
608 info->offset = job->offset;
609 info->speed = job->speed;
610 info->io_status = job->iostatus;
611 info->ready = job->ready;
612 return info;
615 static void block_job_iostatus_set_err(BlockJob *job, int error)
617 if (job->iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
618 job->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE :
619 BLOCK_DEVICE_IO_STATUS_FAILED;
623 static void block_job_event_cancelled(BlockJob *job)
625 if (block_job_is_internal(job)) {
626 return;
629 qapi_event_send_block_job_cancelled(job->driver->job_type,
630 job->id,
631 job->len,
632 job->offset,
633 job->speed,
634 &error_abort);
637 static void block_job_event_completed(BlockJob *job, const char *msg)
639 if (block_job_is_internal(job)) {
640 return;
643 qapi_event_send_block_job_completed(job->driver->job_type,
644 job->id,
645 job->len,
646 job->offset,
647 job->speed,
648 !!msg,
649 msg,
650 &error_abort);
653 void block_job_event_ready(BlockJob *job)
655 job->ready = true;
657 if (block_job_is_internal(job)) {
658 return;
661 qapi_event_send_block_job_ready(job->driver->job_type,
662 job->id,
663 job->len,
664 job->offset,
665 job->speed, &error_abort);
668 BlockErrorAction block_job_error_action(BlockJob *job, BlockdevOnError on_err,
669 int is_read, int error)
671 BlockErrorAction action;
673 switch (on_err) {
674 case BLOCKDEV_ON_ERROR_ENOSPC:
675 case BLOCKDEV_ON_ERROR_AUTO:
676 action = (error == ENOSPC) ?
677 BLOCK_ERROR_ACTION_STOP : BLOCK_ERROR_ACTION_REPORT;
678 break;
679 case BLOCKDEV_ON_ERROR_STOP:
680 action = BLOCK_ERROR_ACTION_STOP;
681 break;
682 case BLOCKDEV_ON_ERROR_REPORT:
683 action = BLOCK_ERROR_ACTION_REPORT;
684 break;
685 case BLOCKDEV_ON_ERROR_IGNORE:
686 action = BLOCK_ERROR_ACTION_IGNORE;
687 break;
688 default:
689 abort();
691 if (!block_job_is_internal(job)) {
692 qapi_event_send_block_job_error(job->id,
693 is_read ? IO_OPERATION_TYPE_READ :
694 IO_OPERATION_TYPE_WRITE,
695 action, &error_abort);
697 if (action == BLOCK_ERROR_ACTION_STOP) {
698 /* make the pause user visible, which will be resumed from QMP. */
699 block_job_user_pause(job);
700 block_job_iostatus_set_err(job, error);
702 return action;
705 typedef struct {
706 BlockJob *job;
707 AioContext *aio_context;
708 BlockJobDeferToMainLoopFn *fn;
709 void *opaque;
710 } BlockJobDeferToMainLoopData;
712 static void block_job_defer_to_main_loop_bh(void *opaque)
714 BlockJobDeferToMainLoopData *data = opaque;
715 AioContext *aio_context;
717 /* Prevent race with block_job_defer_to_main_loop() */
718 aio_context_acquire(data->aio_context);
720 /* Fetch BDS AioContext again, in case it has changed */
721 aio_context = blk_get_aio_context(data->job->blk);
722 aio_context_acquire(aio_context);
724 data->job->deferred_to_main_loop = false;
725 data->fn(data->job, data->opaque);
727 aio_context_release(aio_context);
729 aio_context_release(data->aio_context);
731 g_free(data);
734 void block_job_defer_to_main_loop(BlockJob *job,
735 BlockJobDeferToMainLoopFn *fn,
736 void *opaque)
738 BlockJobDeferToMainLoopData *data = g_malloc(sizeof(*data));
739 data->job = job;
740 data->aio_context = blk_get_aio_context(job->blk);
741 data->fn = fn;
742 data->opaque = opaque;
743 job->deferred_to_main_loop = true;
745 aio_bh_schedule_oneshot(qemu_get_aio_context(),
746 block_job_defer_to_main_loop_bh, data);
749 BlockJobTxn *block_job_txn_new(void)
751 BlockJobTxn *txn = g_new0(BlockJobTxn, 1);
752 QLIST_INIT(&txn->jobs);
753 txn->refcnt = 1;
754 return txn;
757 static void block_job_txn_ref(BlockJobTxn *txn)
759 txn->refcnt++;
762 void block_job_txn_unref(BlockJobTxn *txn)
764 if (txn && --txn->refcnt == 0) {
765 g_free(txn);
769 void block_job_txn_add_job(BlockJobTxn *txn, BlockJob *job)
771 if (!txn) {
772 return;
775 assert(!job->txn);
776 job->txn = txn;
778 QLIST_INSERT_HEAD(&txn->jobs, job, txn_list);
779 block_job_txn_ref(txn);