target/hppa: Implement tlb_fill
[qemu/ar7.git] / blockjob.c
blobf5cea84e7320cfd006145c3df68db32f0d592dc9
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 /* Right now, this mutex is only needed to synchronize accesses to job->busy
41 * and job->sleep_timer, such as concurrent calls to block_job_do_yield and
42 * block_job_enter. */
43 static QemuMutex block_job_mutex;
45 static void block_job_lock(void)
47 qemu_mutex_lock(&block_job_mutex);
50 static void block_job_unlock(void)
52 qemu_mutex_unlock(&block_job_mutex);
55 static void __attribute__((__constructor__)) block_job_init(void)
57 qemu_mutex_init(&block_job_mutex);
60 static void block_job_event_cancelled(BlockJob *job);
61 static void block_job_event_completed(BlockJob *job, const char *msg);
62 static void block_job_enter_cond(BlockJob *job, bool(*fn)(BlockJob *job));
64 /* Transactional group of block jobs */
65 struct BlockJobTxn {
67 /* Is this txn being cancelled? */
68 bool aborting;
70 /* List of jobs */
71 QLIST_HEAD(, BlockJob) jobs;
73 /* Reference count */
74 int refcnt;
77 static QLIST_HEAD(, BlockJob) block_jobs = QLIST_HEAD_INITIALIZER(block_jobs);
80 * The block job API is composed of two categories of functions.
82 * The first includes functions used by the monitor. The monitor is
83 * peculiar in that it accesses the block job list with block_job_get, and
84 * therefore needs consistency across block_job_get and the actual operation
85 * (e.g. block_job_set_speed). The consistency is achieved with
86 * aio_context_acquire/release. These functions are declared in blockjob.h.
88 * The second includes functions used by the block job drivers and sometimes
89 * by the core block layer. These do not care about locking, because the
90 * whole coroutine runs under the AioContext lock, and are declared in
91 * blockjob_int.h.
94 BlockJob *block_job_next(BlockJob *job)
96 if (!job) {
97 return QLIST_FIRST(&block_jobs);
99 return QLIST_NEXT(job, job_list);
102 BlockJob *block_job_get(const char *id)
104 BlockJob *job;
106 QLIST_FOREACH(job, &block_jobs, job_list) {
107 if (job->id && !strcmp(id, job->id)) {
108 return job;
112 return NULL;
115 BlockJobTxn *block_job_txn_new(void)
117 BlockJobTxn *txn = g_new0(BlockJobTxn, 1);
118 QLIST_INIT(&txn->jobs);
119 txn->refcnt = 1;
120 return txn;
123 static void block_job_txn_ref(BlockJobTxn *txn)
125 txn->refcnt++;
128 void block_job_txn_unref(BlockJobTxn *txn)
130 if (txn && --txn->refcnt == 0) {
131 g_free(txn);
135 void block_job_txn_add_job(BlockJobTxn *txn, BlockJob *job)
137 if (!txn) {
138 return;
141 assert(!job->txn);
142 job->txn = txn;
144 QLIST_INSERT_HEAD(&txn->jobs, job, txn_list);
145 block_job_txn_ref(txn);
148 static void block_job_pause(BlockJob *job)
150 job->pause_count++;
153 static void block_job_resume(BlockJob *job)
155 assert(job->pause_count > 0);
156 job->pause_count--;
157 if (job->pause_count) {
158 return;
160 block_job_enter(job);
163 void block_job_ref(BlockJob *job)
165 ++job->refcnt;
168 static void block_job_attached_aio_context(AioContext *new_context,
169 void *opaque);
170 static void block_job_detach_aio_context(void *opaque);
172 void block_job_unref(BlockJob *job)
174 if (--job->refcnt == 0) {
175 BlockDriverState *bs = blk_bs(job->blk);
176 QLIST_REMOVE(job, job_list);
177 bs->job = NULL;
178 block_job_remove_all_bdrv(job);
179 blk_remove_aio_context_notifier(job->blk,
180 block_job_attached_aio_context,
181 block_job_detach_aio_context, job);
182 blk_unref(job->blk);
183 error_free(job->blocker);
184 g_free(job->id);
185 assert(!timer_pending(&job->sleep_timer));
186 g_free(job);
190 static void block_job_attached_aio_context(AioContext *new_context,
191 void *opaque)
193 BlockJob *job = opaque;
195 if (job->driver->attached_aio_context) {
196 job->driver->attached_aio_context(job, new_context);
199 block_job_resume(job);
202 static void block_job_drain(BlockJob *job)
204 /* If job is !job->busy this kicks it into the next pause point. */
205 block_job_enter(job);
207 blk_drain(job->blk);
208 if (job->driver->drain) {
209 job->driver->drain(job);
213 static void block_job_detach_aio_context(void *opaque)
215 BlockJob *job = opaque;
217 /* In case the job terminates during aio_poll()... */
218 block_job_ref(job);
220 block_job_pause(job);
222 while (!job->paused && !job->completed) {
223 block_job_drain(job);
226 block_job_unref(job);
229 static char *child_job_get_parent_desc(BdrvChild *c)
231 BlockJob *job = c->opaque;
232 return g_strdup_printf("%s job '%s'",
233 BlockJobType_str(job->driver->job_type),
234 job->id);
237 static void child_job_drained_begin(BdrvChild *c)
239 BlockJob *job = c->opaque;
240 block_job_pause(job);
243 static void child_job_drained_end(BdrvChild *c)
245 BlockJob *job = c->opaque;
246 block_job_resume(job);
249 static const BdrvChildRole child_job = {
250 .get_parent_desc = child_job_get_parent_desc,
251 .drained_begin = child_job_drained_begin,
252 .drained_end = child_job_drained_end,
253 .stay_at_node = true,
256 void block_job_remove_all_bdrv(BlockJob *job)
258 GSList *l;
259 for (l = job->nodes; l; l = l->next) {
260 BdrvChild *c = l->data;
261 bdrv_op_unblock_all(c->bs, job->blocker);
262 bdrv_root_unref_child(c);
264 g_slist_free(job->nodes);
265 job->nodes = NULL;
268 int block_job_add_bdrv(BlockJob *job, const char *name, BlockDriverState *bs,
269 uint64_t perm, uint64_t shared_perm, Error **errp)
271 BdrvChild *c;
273 c = bdrv_root_attach_child(bs, name, &child_job, perm, shared_perm,
274 job, errp);
275 if (c == NULL) {
276 return -EPERM;
279 job->nodes = g_slist_prepend(job->nodes, c);
280 bdrv_ref(bs);
281 bdrv_op_block_all(bs, job->blocker);
283 return 0;
286 bool block_job_is_internal(BlockJob *job)
288 return (job->id == NULL);
291 static bool block_job_started(BlockJob *job)
293 return job->co;
297 * All jobs must allow a pause point before entering their job proper. This
298 * ensures that jobs can be paused prior to being started, then resumed later.
300 static void coroutine_fn block_job_co_entry(void *opaque)
302 BlockJob *job = opaque;
304 assert(job && job->driver && job->driver->start);
305 block_job_pause_point(job);
306 job->driver->start(job);
309 static void block_job_sleep_timer_cb(void *opaque)
311 BlockJob *job = opaque;
313 block_job_enter(job);
316 void block_job_start(BlockJob *job)
318 assert(job && !block_job_started(job) && job->paused &&
319 job->driver && job->driver->start);
320 job->co = qemu_coroutine_create(block_job_co_entry, job);
321 job->pause_count--;
322 job->busy = true;
323 job->paused = false;
324 bdrv_coroutine_enter(blk_bs(job->blk), job->co);
327 static void block_job_completed_single(BlockJob *job)
329 assert(job->completed);
331 if (!job->ret) {
332 if (job->driver->commit) {
333 job->driver->commit(job);
335 } else {
336 if (job->driver->abort) {
337 job->driver->abort(job);
340 if (job->driver->clean) {
341 job->driver->clean(job);
344 if (job->cb) {
345 job->cb(job->opaque, job->ret);
348 /* Emit events only if we actually started */
349 if (block_job_started(job)) {
350 if (block_job_is_cancelled(job)) {
351 block_job_event_cancelled(job);
352 } else {
353 const char *msg = NULL;
354 if (job->ret < 0) {
355 msg = strerror(-job->ret);
357 block_job_event_completed(job, msg);
361 if (job->txn) {
362 QLIST_REMOVE(job, txn_list);
363 block_job_txn_unref(job->txn);
365 block_job_unref(job);
368 static void block_job_cancel_async(BlockJob *job)
370 if (job->iostatus != BLOCK_DEVICE_IO_STATUS_OK) {
371 block_job_iostatus_reset(job);
373 if (job->user_paused) {
374 /* Do not call block_job_enter here, the caller will handle it. */
375 job->user_paused = false;
376 job->pause_count--;
378 job->cancelled = true;
381 static int block_job_finish_sync(BlockJob *job,
382 void (*finish)(BlockJob *, Error **errp),
383 Error **errp)
385 Error *local_err = NULL;
386 int ret;
388 assert(blk_bs(job->blk)->job == job);
390 block_job_ref(job);
392 if (finish) {
393 finish(job, &local_err);
395 if (local_err) {
396 error_propagate(errp, local_err);
397 block_job_unref(job);
398 return -EBUSY;
400 /* block_job_drain calls block_job_enter, and it should be enough to
401 * induce progress until the job completes or moves to the main thread.
403 while (!job->deferred_to_main_loop && !job->completed) {
404 block_job_drain(job);
406 while (!job->completed) {
407 aio_poll(qemu_get_aio_context(), true);
409 ret = (job->cancelled && job->ret == 0) ? -ECANCELED : job->ret;
410 block_job_unref(job);
411 return ret;
414 static void block_job_completed_txn_abort(BlockJob *job)
416 AioContext *ctx;
417 BlockJobTxn *txn = job->txn;
418 BlockJob *other_job;
420 if (txn->aborting) {
422 * We are cancelled by another job, which will handle everything.
424 return;
426 txn->aborting = true;
427 block_job_txn_ref(txn);
429 /* We are the first failed job. Cancel other jobs. */
430 QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
431 ctx = blk_get_aio_context(other_job->blk);
432 aio_context_acquire(ctx);
435 /* Other jobs are effectively cancelled by us, set the status for
436 * them; this job, however, may or may not be cancelled, depending
437 * on the caller, so leave it. */
438 QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
439 if (other_job != job) {
440 block_job_cancel_async(other_job);
443 while (!QLIST_EMPTY(&txn->jobs)) {
444 other_job = QLIST_FIRST(&txn->jobs);
445 ctx = blk_get_aio_context(other_job->blk);
446 if (!other_job->completed) {
447 assert(other_job->cancelled);
448 block_job_finish_sync(other_job, NULL, NULL);
450 block_job_completed_single(other_job);
451 aio_context_release(ctx);
454 block_job_txn_unref(txn);
457 static void block_job_completed_txn_success(BlockJob *job)
459 AioContext *ctx;
460 BlockJobTxn *txn = job->txn;
461 BlockJob *other_job, *next;
463 * Successful completion, see if there are other running jobs in this
464 * txn.
466 QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
467 if (!other_job->completed) {
468 return;
471 /* We are the last completed job, commit the transaction. */
472 QLIST_FOREACH_SAFE(other_job, &txn->jobs, txn_list, next) {
473 ctx = blk_get_aio_context(other_job->blk);
474 aio_context_acquire(ctx);
475 assert(other_job->ret == 0);
476 block_job_completed_single(other_job);
477 aio_context_release(ctx);
481 /* Assumes the block_job_mutex is held */
482 static bool block_job_timer_pending(BlockJob *job)
484 return timer_pending(&job->sleep_timer);
487 void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp)
489 Error *local_err = NULL;
490 int64_t old_speed = job->speed;
492 if (!job->driver->set_speed) {
493 error_setg(errp, QERR_UNSUPPORTED);
494 return;
496 job->driver->set_speed(job, speed, &local_err);
497 if (local_err) {
498 error_propagate(errp, local_err);
499 return;
502 job->speed = speed;
503 if (speed <= old_speed) {
504 return;
507 /* kick only if a timer is pending */
508 block_job_enter_cond(job, block_job_timer_pending);
511 void block_job_complete(BlockJob *job, Error **errp)
513 /* Should not be reachable via external interface for internal jobs */
514 assert(job->id);
515 if (job->pause_count || job->cancelled ||
516 !block_job_started(job) || !job->driver->complete) {
517 error_setg(errp, "The active block job '%s' cannot be completed",
518 job->id);
519 return;
522 job->driver->complete(job, errp);
525 void block_job_user_pause(BlockJob *job)
527 job->user_paused = true;
528 block_job_pause(job);
531 bool block_job_user_paused(BlockJob *job)
533 return job->user_paused;
536 void block_job_user_resume(BlockJob *job)
538 if (job && job->user_paused && job->pause_count > 0) {
539 block_job_iostatus_reset(job);
540 job->user_paused = false;
541 block_job_resume(job);
545 void block_job_cancel(BlockJob *job)
547 if (block_job_started(job)) {
548 block_job_cancel_async(job);
549 block_job_enter(job);
550 } else {
551 block_job_completed(job, -ECANCELED);
555 /* A wrapper around block_job_cancel() taking an Error ** parameter so it may be
556 * used with block_job_finish_sync() without the need for (rather nasty)
557 * function pointer casts there. */
558 static void block_job_cancel_err(BlockJob *job, Error **errp)
560 block_job_cancel(job);
563 int block_job_cancel_sync(BlockJob *job)
565 return block_job_finish_sync(job, &block_job_cancel_err, NULL);
568 void block_job_cancel_sync_all(void)
570 BlockJob *job;
571 AioContext *aio_context;
573 while ((job = QLIST_FIRST(&block_jobs))) {
574 aio_context = blk_get_aio_context(job->blk);
575 aio_context_acquire(aio_context);
576 block_job_cancel_sync(job);
577 aio_context_release(aio_context);
581 int block_job_complete_sync(BlockJob *job, Error **errp)
583 return block_job_finish_sync(job, &block_job_complete, errp);
586 BlockJobInfo *block_job_query(BlockJob *job, Error **errp)
588 BlockJobInfo *info;
590 if (block_job_is_internal(job)) {
591 error_setg(errp, "Cannot query QEMU internal jobs");
592 return NULL;
594 info = g_new0(BlockJobInfo, 1);
595 info->type = g_strdup(BlockJobType_str(job->driver->job_type));
596 info->device = g_strdup(job->id);
597 info->len = job->len;
598 info->busy = atomic_read(&job->busy);
599 info->paused = job->pause_count > 0;
600 info->offset = job->offset;
601 info->speed = job->speed;
602 info->io_status = job->iostatus;
603 info->ready = job->ready;
604 return info;
607 static void block_job_iostatus_set_err(BlockJob *job, int error)
609 if (job->iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
610 job->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE :
611 BLOCK_DEVICE_IO_STATUS_FAILED;
615 static void block_job_event_cancelled(BlockJob *job)
617 if (block_job_is_internal(job)) {
618 return;
621 qapi_event_send_block_job_cancelled(job->driver->job_type,
622 job->id,
623 job->len,
624 job->offset,
625 job->speed,
626 &error_abort);
629 static void block_job_event_completed(BlockJob *job, const char *msg)
631 if (block_job_is_internal(job)) {
632 return;
635 qapi_event_send_block_job_completed(job->driver->job_type,
636 job->id,
637 job->len,
638 job->offset,
639 job->speed,
640 !!msg,
641 msg,
642 &error_abort);
646 * API for block job drivers and the block layer. These functions are
647 * declared in blockjob_int.h.
650 void *block_job_create(const char *job_id, const BlockJobDriver *driver,
651 BlockDriverState *bs, uint64_t perm,
652 uint64_t shared_perm, int64_t speed, int flags,
653 BlockCompletionFunc *cb, void *opaque, Error **errp)
655 BlockBackend *blk;
656 BlockJob *job;
657 int ret;
659 if (bs->job) {
660 error_setg(errp, QERR_DEVICE_IN_USE, bdrv_get_device_name(bs));
661 return NULL;
664 if (job_id == NULL && !(flags & BLOCK_JOB_INTERNAL)) {
665 job_id = bdrv_get_device_name(bs);
666 if (!*job_id) {
667 error_setg(errp, "An explicit job ID is required for this node");
668 return NULL;
672 if (job_id) {
673 if (flags & BLOCK_JOB_INTERNAL) {
674 error_setg(errp, "Cannot specify job ID for internal block job");
675 return NULL;
678 if (!id_wellformed(job_id)) {
679 error_setg(errp, "Invalid job ID '%s'", job_id);
680 return NULL;
683 if (block_job_get(job_id)) {
684 error_setg(errp, "Job ID '%s' already in use", job_id);
685 return NULL;
689 blk = blk_new(perm, shared_perm);
690 ret = blk_insert_bs(blk, bs, errp);
691 if (ret < 0) {
692 blk_unref(blk);
693 return NULL;
696 job = g_malloc0(driver->instance_size);
697 job->driver = driver;
698 job->id = g_strdup(job_id);
699 job->blk = blk;
700 job->cb = cb;
701 job->opaque = opaque;
702 job->busy = false;
703 job->paused = true;
704 job->pause_count = 1;
705 job->refcnt = 1;
706 aio_timer_init(qemu_get_aio_context(), &job->sleep_timer,
707 QEMU_CLOCK_REALTIME, SCALE_NS,
708 block_job_sleep_timer_cb, job);
710 error_setg(&job->blocker, "block device is in use by block job: %s",
711 BlockJobType_str(driver->job_type));
712 block_job_add_bdrv(job, "main node", bs, 0, BLK_PERM_ALL, &error_abort);
713 bs->job = job;
715 bdrv_op_unblock(bs, BLOCK_OP_TYPE_DATAPLANE, job->blocker);
717 QLIST_INSERT_HEAD(&block_jobs, job, job_list);
719 blk_add_aio_context_notifier(blk, block_job_attached_aio_context,
720 block_job_detach_aio_context, job);
722 /* Only set speed when necessary to avoid NotSupported error */
723 if (speed != 0) {
724 Error *local_err = NULL;
726 block_job_set_speed(job, speed, &local_err);
727 if (local_err) {
728 block_job_unref(job);
729 error_propagate(errp, local_err);
730 return NULL;
733 return job;
736 void block_job_pause_all(void)
738 BlockJob *job = NULL;
739 while ((job = block_job_next(job))) {
740 AioContext *aio_context = blk_get_aio_context(job->blk);
742 aio_context_acquire(aio_context);
743 block_job_ref(job);
744 block_job_pause(job);
745 aio_context_release(aio_context);
749 void block_job_early_fail(BlockJob *job)
751 block_job_unref(job);
754 void block_job_completed(BlockJob *job, int ret)
756 assert(blk_bs(job->blk)->job == job);
757 assert(!job->completed);
758 job->completed = true;
759 job->ret = ret;
760 if (!job->txn) {
761 block_job_completed_single(job);
762 } else if (ret < 0 || block_job_is_cancelled(job)) {
763 block_job_completed_txn_abort(job);
764 } else {
765 block_job_completed_txn_success(job);
769 static bool block_job_should_pause(BlockJob *job)
771 return job->pause_count > 0;
774 /* Yield, and schedule a timer to reenter the coroutine after @ns nanoseconds.
775 * Reentering the job coroutine with block_job_enter() before the timer has
776 * expired is allowed and cancels the timer.
778 * If @ns is (uint64_t) -1, no timer is scheduled and block_job_enter() must be
779 * called explicitly. */
780 static void block_job_do_yield(BlockJob *job, uint64_t ns)
782 block_job_lock();
783 if (ns != -1) {
784 timer_mod(&job->sleep_timer, ns);
786 job->busy = false;
787 block_job_unlock();
788 qemu_coroutine_yield();
790 /* Set by block_job_enter before re-entering the coroutine. */
791 assert(job->busy);
794 void coroutine_fn block_job_pause_point(BlockJob *job)
796 assert(job && block_job_started(job));
798 if (!block_job_should_pause(job)) {
799 return;
801 if (block_job_is_cancelled(job)) {
802 return;
805 if (job->driver->pause) {
806 job->driver->pause(job);
809 if (block_job_should_pause(job) && !block_job_is_cancelled(job)) {
810 job->paused = true;
811 block_job_do_yield(job, -1);
812 job->paused = false;
815 if (job->driver->resume) {
816 job->driver->resume(job);
820 void block_job_resume_all(void)
822 BlockJob *job, *next;
824 QLIST_FOREACH_SAFE(job, &block_jobs, job_list, next) {
825 AioContext *aio_context = blk_get_aio_context(job->blk);
827 aio_context_acquire(aio_context);
828 block_job_resume(job);
829 block_job_unref(job);
830 aio_context_release(aio_context);
835 * Conditionally enter a block_job pending a call to fn() while
836 * under the block_job_lock critical section.
838 static void block_job_enter_cond(BlockJob *job, bool(*fn)(BlockJob *job))
840 if (!block_job_started(job)) {
841 return;
843 if (job->deferred_to_main_loop) {
844 return;
847 block_job_lock();
848 if (job->busy) {
849 block_job_unlock();
850 return;
853 if (fn && !fn(job)) {
854 block_job_unlock();
855 return;
858 assert(!job->deferred_to_main_loop);
859 timer_del(&job->sleep_timer);
860 job->busy = true;
861 block_job_unlock();
862 aio_co_wake(job->co);
865 void block_job_enter(BlockJob *job)
867 block_job_enter_cond(job, NULL);
870 bool block_job_is_cancelled(BlockJob *job)
872 return job->cancelled;
875 void block_job_sleep_ns(BlockJob *job, int64_t ns)
877 assert(job->busy);
879 /* Check cancellation *before* setting busy = false, too! */
880 if (block_job_is_cancelled(job)) {
881 return;
884 if (!block_job_should_pause(job)) {
885 block_job_do_yield(job, qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + ns);
888 block_job_pause_point(job);
891 void block_job_yield(BlockJob *job)
893 assert(job->busy);
895 /* Check cancellation *before* setting busy = false, too! */
896 if (block_job_is_cancelled(job)) {
897 return;
900 if (!block_job_should_pause(job)) {
901 block_job_do_yield(job, -1);
904 block_job_pause_point(job);
907 void block_job_iostatus_reset(BlockJob *job)
909 if (job->iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
910 return;
912 assert(job->user_paused && job->pause_count > 0);
913 job->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
916 void block_job_event_ready(BlockJob *job)
918 job->ready = true;
920 if (block_job_is_internal(job)) {
921 return;
924 qapi_event_send_block_job_ready(job->driver->job_type,
925 job->id,
926 job->len,
927 job->offset,
928 job->speed, &error_abort);
931 BlockErrorAction block_job_error_action(BlockJob *job, BlockdevOnError on_err,
932 int is_read, int error)
934 BlockErrorAction action;
936 switch (on_err) {
937 case BLOCKDEV_ON_ERROR_ENOSPC:
938 case BLOCKDEV_ON_ERROR_AUTO:
939 action = (error == ENOSPC) ?
940 BLOCK_ERROR_ACTION_STOP : BLOCK_ERROR_ACTION_REPORT;
941 break;
942 case BLOCKDEV_ON_ERROR_STOP:
943 action = BLOCK_ERROR_ACTION_STOP;
944 break;
945 case BLOCKDEV_ON_ERROR_REPORT:
946 action = BLOCK_ERROR_ACTION_REPORT;
947 break;
948 case BLOCKDEV_ON_ERROR_IGNORE:
949 action = BLOCK_ERROR_ACTION_IGNORE;
950 break;
951 default:
952 abort();
954 if (!block_job_is_internal(job)) {
955 qapi_event_send_block_job_error(job->id,
956 is_read ? IO_OPERATION_TYPE_READ :
957 IO_OPERATION_TYPE_WRITE,
958 action, &error_abort);
960 if (action == BLOCK_ERROR_ACTION_STOP) {
961 /* make the pause user visible, which will be resumed from QMP. */
962 block_job_user_pause(job);
963 block_job_iostatus_set_err(job, error);
965 return action;
968 typedef struct {
969 BlockJob *job;
970 AioContext *aio_context;
971 BlockJobDeferToMainLoopFn *fn;
972 void *opaque;
973 } BlockJobDeferToMainLoopData;
975 static void block_job_defer_to_main_loop_bh(void *opaque)
977 BlockJobDeferToMainLoopData *data = opaque;
978 AioContext *aio_context;
980 /* Prevent race with block_job_defer_to_main_loop() */
981 aio_context_acquire(data->aio_context);
983 /* Fetch BDS AioContext again, in case it has changed */
984 aio_context = blk_get_aio_context(data->job->blk);
985 if (aio_context != data->aio_context) {
986 aio_context_acquire(aio_context);
989 data->fn(data->job, data->opaque);
991 if (aio_context != data->aio_context) {
992 aio_context_release(aio_context);
995 aio_context_release(data->aio_context);
997 g_free(data);
1000 void block_job_defer_to_main_loop(BlockJob *job,
1001 BlockJobDeferToMainLoopFn *fn,
1002 void *opaque)
1004 BlockJobDeferToMainLoopData *data = g_malloc(sizeof(*data));
1005 data->job = job;
1006 data->aio_context = blk_get_aio_context(job->blk);
1007 data->fn = fn;
1008 data->opaque = opaque;
1009 job->deferred_to_main_loop = true;
1011 aio_bh_schedule_oneshot(qemu_get_aio_context(),
1012 block_job_defer_to_main_loop_bh, data);