Merge remote-tracking branch 'mreitz/tags/pull-block-2017-05-11' into queue-block
[qemu/kevin.git] / blockjob.c
blob6e489327ff4d195b038b96b94dac97c55320043d
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 static char *child_job_get_parent_desc(BdrvChild *c)
60 BlockJob *job = c->opaque;
61 return g_strdup_printf("%s job '%s'",
62 BlockJobType_lookup[job->driver->job_type],
63 job->id);
66 static const BdrvChildRole child_job = {
67 .get_parent_desc = child_job_get_parent_desc,
68 .stay_at_node = true,
71 static void block_job_drained_begin(void *opaque)
73 BlockJob *job = opaque;
74 block_job_pause(job);
77 static void block_job_drained_end(void *opaque)
79 BlockJob *job = opaque;
80 block_job_resume(job);
83 static const BlockDevOps block_job_dev_ops = {
84 .drained_begin = block_job_drained_begin,
85 .drained_end = block_job_drained_end,
88 BlockJob *block_job_next(BlockJob *job)
90 if (!job) {
91 return QLIST_FIRST(&block_jobs);
93 return QLIST_NEXT(job, job_list);
96 BlockJob *block_job_get(const char *id)
98 BlockJob *job;
100 QLIST_FOREACH(job, &block_jobs, job_list) {
101 if (job->id && !strcmp(id, job->id)) {
102 return job;
106 return NULL;
109 static void block_job_attached_aio_context(AioContext *new_context,
110 void *opaque)
112 BlockJob *job = opaque;
114 if (job->driver->attached_aio_context) {
115 job->driver->attached_aio_context(job, new_context);
118 block_job_resume(job);
121 static void block_job_drain(BlockJob *job)
123 /* If job is !job->busy this kicks it into the next pause point. */
124 block_job_enter(job);
126 blk_drain(job->blk);
127 if (job->driver->drain) {
128 job->driver->drain(job);
132 static void block_job_detach_aio_context(void *opaque)
134 BlockJob *job = opaque;
136 /* In case the job terminates during aio_poll()... */
137 block_job_ref(job);
139 block_job_pause(job);
141 while (!job->paused && !job->completed) {
142 block_job_drain(job);
145 block_job_unref(job);
148 void block_job_remove_all_bdrv(BlockJob *job)
150 GSList *l;
151 for (l = job->nodes; l; l = l->next) {
152 BdrvChild *c = l->data;
153 bdrv_op_unblock_all(c->bs, job->blocker);
154 bdrv_root_unref_child(c);
156 g_slist_free(job->nodes);
157 job->nodes = NULL;
160 int block_job_add_bdrv(BlockJob *job, const char *name, BlockDriverState *bs,
161 uint64_t perm, uint64_t shared_perm, Error **errp)
163 BdrvChild *c;
165 c = bdrv_root_attach_child(bs, name, &child_job, perm, shared_perm,
166 job, errp);
167 if (c == NULL) {
168 return -EPERM;
171 job->nodes = g_slist_prepend(job->nodes, c);
172 bdrv_ref(bs);
173 bdrv_op_block_all(bs, job->blocker);
175 return 0;
178 void *block_job_create(const char *job_id, const BlockJobDriver *driver,
179 BlockDriverState *bs, uint64_t perm,
180 uint64_t shared_perm, int64_t speed, int flags,
181 BlockCompletionFunc *cb, void *opaque, Error **errp)
183 BlockBackend *blk;
184 BlockJob *job;
185 int ret;
187 if (bs->job) {
188 error_setg(errp, QERR_DEVICE_IN_USE, bdrv_get_device_name(bs));
189 return NULL;
192 if (job_id == NULL && !(flags & BLOCK_JOB_INTERNAL)) {
193 job_id = bdrv_get_device_name(bs);
194 if (!*job_id) {
195 error_setg(errp, "An explicit job ID is required for this node");
196 return NULL;
200 if (job_id) {
201 if (flags & BLOCK_JOB_INTERNAL) {
202 error_setg(errp, "Cannot specify job ID for internal block job");
203 return NULL;
206 if (!id_wellformed(job_id)) {
207 error_setg(errp, "Invalid job ID '%s'", job_id);
208 return NULL;
211 if (block_job_get(job_id)) {
212 error_setg(errp, "Job ID '%s' already in use", job_id);
213 return NULL;
217 blk = blk_new(perm, shared_perm);
218 ret = blk_insert_bs(blk, bs, errp);
219 if (ret < 0) {
220 blk_unref(blk);
221 return NULL;
224 job = g_malloc0(driver->instance_size);
225 job->driver = driver;
226 job->id = g_strdup(job_id);
227 job->blk = blk;
228 job->cb = cb;
229 job->opaque = opaque;
230 job->busy = false;
231 job->paused = true;
232 job->pause_count = 1;
233 job->refcnt = 1;
235 error_setg(&job->blocker, "block device is in use by block job: %s",
236 BlockJobType_lookup[driver->job_type]);
237 block_job_add_bdrv(job, "main node", bs, 0, BLK_PERM_ALL, &error_abort);
238 bs->job = job;
240 blk_set_dev_ops(blk, &block_job_dev_ops, job);
241 bdrv_op_unblock(bs, BLOCK_OP_TYPE_DATAPLANE, job->blocker);
243 QLIST_INSERT_HEAD(&block_jobs, job, job_list);
245 blk_add_aio_context_notifier(blk, block_job_attached_aio_context,
246 block_job_detach_aio_context, job);
248 /* Only set speed when necessary to avoid NotSupported error */
249 if (speed != 0) {
250 Error *local_err = NULL;
252 block_job_set_speed(job, speed, &local_err);
253 if (local_err) {
254 block_job_unref(job);
255 error_propagate(errp, local_err);
256 return NULL;
259 return job;
262 bool block_job_is_internal(BlockJob *job)
264 return (job->id == NULL);
267 static bool block_job_started(BlockJob *job)
269 return job->co;
273 * All jobs must allow a pause point before entering their job proper. This
274 * ensures that jobs can be paused prior to being started, then resumed later.
276 static void coroutine_fn block_job_co_entry(void *opaque)
278 BlockJob *job = opaque;
280 assert(job && job->driver && job->driver->start);
281 block_job_pause_point(job);
282 job->driver->start(job);
285 void block_job_start(BlockJob *job)
287 assert(job && !block_job_started(job) && job->paused &&
288 job->driver && job->driver->start);
289 job->co = qemu_coroutine_create(block_job_co_entry, job);
290 job->pause_count--;
291 job->busy = true;
292 job->paused = false;
293 bdrv_coroutine_enter(blk_bs(job->blk), job->co);
296 void block_job_ref(BlockJob *job)
298 ++job->refcnt;
301 void block_job_unref(BlockJob *job)
303 if (--job->refcnt == 0) {
304 BlockDriverState *bs = blk_bs(job->blk);
305 bs->job = NULL;
306 block_job_remove_all_bdrv(job);
307 blk_remove_aio_context_notifier(job->blk,
308 block_job_attached_aio_context,
309 block_job_detach_aio_context, job);
310 blk_unref(job->blk);
311 error_free(job->blocker);
312 g_free(job->id);
313 QLIST_REMOVE(job, job_list);
314 g_free(job);
318 static void block_job_completed_single(BlockJob *job)
320 if (!job->ret) {
321 if (job->driver->commit) {
322 job->driver->commit(job);
324 } else {
325 if (job->driver->abort) {
326 job->driver->abort(job);
329 if (job->driver->clean) {
330 job->driver->clean(job);
333 if (job->cb) {
334 job->cb(job->opaque, job->ret);
337 /* Emit events only if we actually started */
338 if (block_job_started(job)) {
339 if (block_job_is_cancelled(job)) {
340 block_job_event_cancelled(job);
341 } else {
342 const char *msg = NULL;
343 if (job->ret < 0) {
344 msg = strerror(-job->ret);
346 block_job_event_completed(job, msg);
350 if (job->txn) {
351 QLIST_REMOVE(job, txn_list);
352 block_job_txn_unref(job->txn);
354 block_job_unref(job);
357 static void block_job_completed_txn_abort(BlockJob *job)
359 AioContext *ctx;
360 BlockJobTxn *txn = job->txn;
361 BlockJob *other_job, *next;
363 if (txn->aborting) {
365 * We are cancelled by another job, which will handle everything.
367 return;
369 txn->aborting = true;
370 /* We are the first failed job. Cancel other jobs. */
371 QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
372 ctx = blk_get_aio_context(other_job->blk);
373 aio_context_acquire(ctx);
375 QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
376 if (other_job == job || other_job->completed) {
377 /* Other jobs are "effectively" cancelled by us, set the status for
378 * them; this job, however, may or may not be cancelled, depending
379 * on the caller, so leave it. */
380 if (other_job != job) {
381 other_job->cancelled = true;
383 continue;
385 block_job_cancel_sync(other_job);
386 assert(other_job->completed);
388 QLIST_FOREACH_SAFE(other_job, &txn->jobs, txn_list, next) {
389 ctx = blk_get_aio_context(other_job->blk);
390 block_job_completed_single(other_job);
391 aio_context_release(ctx);
395 static void block_job_completed_txn_success(BlockJob *job)
397 AioContext *ctx;
398 BlockJobTxn *txn = job->txn;
399 BlockJob *other_job, *next;
401 * Successful completion, see if there are other running jobs in this
402 * txn.
404 QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
405 if (!other_job->completed) {
406 return;
409 /* We are the last completed job, commit the transaction. */
410 QLIST_FOREACH_SAFE(other_job, &txn->jobs, txn_list, next) {
411 ctx = blk_get_aio_context(other_job->blk);
412 aio_context_acquire(ctx);
413 assert(other_job->ret == 0);
414 block_job_completed_single(other_job);
415 aio_context_release(ctx);
419 void block_job_completed(BlockJob *job, int ret)
421 assert(blk_bs(job->blk)->job == job);
422 assert(!job->completed);
423 job->completed = true;
424 job->ret = ret;
425 if (!job->txn) {
426 block_job_completed_single(job);
427 } else if (ret < 0 || block_job_is_cancelled(job)) {
428 block_job_completed_txn_abort(job);
429 } else {
430 block_job_completed_txn_success(job);
434 void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp)
436 Error *local_err = NULL;
438 if (!job->driver->set_speed) {
439 error_setg(errp, QERR_UNSUPPORTED);
440 return;
442 job->driver->set_speed(job, speed, &local_err);
443 if (local_err) {
444 error_propagate(errp, local_err);
445 return;
448 job->speed = speed;
451 void block_job_complete(BlockJob *job, Error **errp)
453 /* Should not be reachable via external interface for internal jobs */
454 assert(job->id);
455 if (job->pause_count || job->cancelled ||
456 !block_job_started(job) || !job->driver->complete) {
457 error_setg(errp, "The active block job '%s' cannot be completed",
458 job->id);
459 return;
462 job->driver->complete(job, errp);
465 void block_job_pause(BlockJob *job)
467 job->pause_count++;
470 void block_job_user_pause(BlockJob *job)
472 job->user_paused = true;
473 block_job_pause(job);
476 static bool block_job_should_pause(BlockJob *job)
478 return job->pause_count > 0;
481 bool block_job_user_paused(BlockJob *job)
483 return job ? job->user_paused : 0;
486 void coroutine_fn block_job_pause_point(BlockJob *job)
488 assert(job && block_job_started(job));
490 if (!block_job_should_pause(job)) {
491 return;
493 if (block_job_is_cancelled(job)) {
494 return;
497 if (job->driver->pause) {
498 job->driver->pause(job);
501 if (block_job_should_pause(job) && !block_job_is_cancelled(job)) {
502 job->paused = true;
503 job->busy = false;
504 qemu_coroutine_yield(); /* wait for block_job_resume() */
505 job->busy = true;
506 job->paused = false;
509 if (job->driver->resume) {
510 job->driver->resume(job);
514 void block_job_resume(BlockJob *job)
516 assert(job->pause_count > 0);
517 job->pause_count--;
518 if (job->pause_count) {
519 return;
521 block_job_enter(job);
524 void block_job_user_resume(BlockJob *job)
526 if (job && job->user_paused && job->pause_count > 0) {
527 job->user_paused = false;
528 block_job_resume(job);
532 void block_job_enter(BlockJob *job)
534 if (job->co && !job->busy) {
535 bdrv_coroutine_enter(blk_bs(job->blk), job->co);
539 void block_job_cancel(BlockJob *job)
541 if (block_job_started(job)) {
542 job->cancelled = true;
543 block_job_iostatus_reset(job);
544 block_job_enter(job);
545 } else {
546 block_job_completed(job, -ECANCELED);
550 bool block_job_is_cancelled(BlockJob *job)
552 return job->cancelled;
555 void block_job_iostatus_reset(BlockJob *job)
557 job->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
558 if (job->driver->iostatus_reset) {
559 job->driver->iostatus_reset(job);
563 static int block_job_finish_sync(BlockJob *job,
564 void (*finish)(BlockJob *, Error **errp),
565 Error **errp)
567 Error *local_err = NULL;
568 int ret;
570 assert(blk_bs(job->blk)->job == job);
572 block_job_ref(job);
574 finish(job, &local_err);
575 if (local_err) {
576 error_propagate(errp, local_err);
577 block_job_unref(job);
578 return -EBUSY;
580 /* block_job_drain calls block_job_enter, and it should be enough to
581 * induce progress until the job completes or moves to the main thread.
583 while (!job->deferred_to_main_loop && !job->completed) {
584 block_job_drain(job);
586 while (!job->completed) {
587 aio_poll(qemu_get_aio_context(), true);
589 ret = (job->cancelled && job->ret == 0) ? -ECANCELED : job->ret;
590 block_job_unref(job);
591 return ret;
594 /* A wrapper around block_job_cancel() taking an Error ** parameter so it may be
595 * used with block_job_finish_sync() without the need for (rather nasty)
596 * function pointer casts there. */
597 static void block_job_cancel_err(BlockJob *job, Error **errp)
599 block_job_cancel(job);
602 int block_job_cancel_sync(BlockJob *job)
604 return block_job_finish_sync(job, &block_job_cancel_err, NULL);
607 void block_job_cancel_sync_all(void)
609 BlockJob *job;
610 AioContext *aio_context;
612 while ((job = QLIST_FIRST(&block_jobs))) {
613 aio_context = blk_get_aio_context(job->blk);
614 aio_context_acquire(aio_context);
615 block_job_cancel_sync(job);
616 aio_context_release(aio_context);
620 int block_job_complete_sync(BlockJob *job, Error **errp)
622 return block_job_finish_sync(job, &block_job_complete, errp);
625 void block_job_sleep_ns(BlockJob *job, QEMUClockType type, int64_t ns)
627 assert(job->busy);
629 /* Check cancellation *before* setting busy = false, too! */
630 if (block_job_is_cancelled(job)) {
631 return;
634 job->busy = false;
635 if (!block_job_should_pause(job)) {
636 co_aio_sleep_ns(blk_get_aio_context(job->blk), type, ns);
638 job->busy = true;
640 block_job_pause_point(job);
643 void block_job_yield(BlockJob *job)
645 assert(job->busy);
647 /* Check cancellation *before* setting busy = false, too! */
648 if (block_job_is_cancelled(job)) {
649 return;
652 job->busy = false;
653 if (!block_job_should_pause(job)) {
654 qemu_coroutine_yield();
656 job->busy = true;
658 block_job_pause_point(job);
661 BlockJobInfo *block_job_query(BlockJob *job, Error **errp)
663 BlockJobInfo *info;
665 if (block_job_is_internal(job)) {
666 error_setg(errp, "Cannot query QEMU internal jobs");
667 return NULL;
669 info = g_new0(BlockJobInfo, 1);
670 info->type = g_strdup(BlockJobType_lookup[job->driver->job_type]);
671 info->device = g_strdup(job->id);
672 info->len = job->len;
673 info->busy = job->busy;
674 info->paused = job->pause_count > 0;
675 info->offset = job->offset;
676 info->speed = job->speed;
677 info->io_status = job->iostatus;
678 info->ready = job->ready;
679 return info;
682 static void block_job_iostatus_set_err(BlockJob *job, int error)
684 if (job->iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
685 job->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE :
686 BLOCK_DEVICE_IO_STATUS_FAILED;
690 static void block_job_event_cancelled(BlockJob *job)
692 if (block_job_is_internal(job)) {
693 return;
696 qapi_event_send_block_job_cancelled(job->driver->job_type,
697 job->id,
698 job->len,
699 job->offset,
700 job->speed,
701 &error_abort);
704 static void block_job_event_completed(BlockJob *job, const char *msg)
706 if (block_job_is_internal(job)) {
707 return;
710 qapi_event_send_block_job_completed(job->driver->job_type,
711 job->id,
712 job->len,
713 job->offset,
714 job->speed,
715 !!msg,
716 msg,
717 &error_abort);
720 void block_job_event_ready(BlockJob *job)
722 job->ready = true;
724 if (block_job_is_internal(job)) {
725 return;
728 qapi_event_send_block_job_ready(job->driver->job_type,
729 job->id,
730 job->len,
731 job->offset,
732 job->speed, &error_abort);
735 BlockErrorAction block_job_error_action(BlockJob *job, BlockdevOnError on_err,
736 int is_read, int error)
738 BlockErrorAction action;
740 switch (on_err) {
741 case BLOCKDEV_ON_ERROR_ENOSPC:
742 case BLOCKDEV_ON_ERROR_AUTO:
743 action = (error == ENOSPC) ?
744 BLOCK_ERROR_ACTION_STOP : BLOCK_ERROR_ACTION_REPORT;
745 break;
746 case BLOCKDEV_ON_ERROR_STOP:
747 action = BLOCK_ERROR_ACTION_STOP;
748 break;
749 case BLOCKDEV_ON_ERROR_REPORT:
750 action = BLOCK_ERROR_ACTION_REPORT;
751 break;
752 case BLOCKDEV_ON_ERROR_IGNORE:
753 action = BLOCK_ERROR_ACTION_IGNORE;
754 break;
755 default:
756 abort();
758 if (!block_job_is_internal(job)) {
759 qapi_event_send_block_job_error(job->id,
760 is_read ? IO_OPERATION_TYPE_READ :
761 IO_OPERATION_TYPE_WRITE,
762 action, &error_abort);
764 if (action == BLOCK_ERROR_ACTION_STOP) {
765 /* make the pause user visible, which will be resumed from QMP. */
766 block_job_user_pause(job);
767 block_job_iostatus_set_err(job, error);
769 return action;
772 typedef struct {
773 BlockJob *job;
774 AioContext *aio_context;
775 BlockJobDeferToMainLoopFn *fn;
776 void *opaque;
777 } BlockJobDeferToMainLoopData;
779 static void block_job_defer_to_main_loop_bh(void *opaque)
781 BlockJobDeferToMainLoopData *data = opaque;
782 AioContext *aio_context;
784 /* Prevent race with block_job_defer_to_main_loop() */
785 aio_context_acquire(data->aio_context);
787 /* Fetch BDS AioContext again, in case it has changed */
788 aio_context = blk_get_aio_context(data->job->blk);
789 if (aio_context != data->aio_context) {
790 aio_context_acquire(aio_context);
793 data->job->deferred_to_main_loop = false;
794 data->fn(data->job, data->opaque);
796 if (aio_context != data->aio_context) {
797 aio_context_release(aio_context);
800 aio_context_release(data->aio_context);
802 g_free(data);
805 void block_job_defer_to_main_loop(BlockJob *job,
806 BlockJobDeferToMainLoopFn *fn,
807 void *opaque)
809 BlockJobDeferToMainLoopData *data = g_malloc(sizeof(*data));
810 data->job = job;
811 data->aio_context = blk_get_aio_context(job->blk);
812 data->fn = fn;
813 data->opaque = opaque;
814 job->deferred_to_main_loop = true;
816 aio_bh_schedule_oneshot(qemu_get_aio_context(),
817 block_job_defer_to_main_loop_bh, data);
820 BlockJobTxn *block_job_txn_new(void)
822 BlockJobTxn *txn = g_new0(BlockJobTxn, 1);
823 QLIST_INIT(&txn->jobs);
824 txn->refcnt = 1;
825 return txn;
828 static void block_job_txn_ref(BlockJobTxn *txn)
830 txn->refcnt++;
833 void block_job_txn_unref(BlockJobTxn *txn)
835 if (txn && --txn->refcnt == 0) {
836 g_free(txn);
840 void block_job_txn_add_job(BlockJobTxn *txn, BlockJob *job)
842 if (!txn) {
843 return;
846 assert(!job->txn);
847 job->txn = txn;
849 QLIST_INSERT_HEAD(&txn->jobs, job, txn_list);
850 block_job_txn_ref(txn);