]> git.proxmox.com Git - mirror_qemu.git/blame - blockjob.c
qmp: add 'busy' member to BlockJobInfo
[mirror_qemu.git] / blockjob.c
CommitLineData
2f0c9fe6
PB
1/*
2 * QEMU System Emulator block driver
3 *
4 * Copyright (c) 2011 IBM Corp.
5 * Copyright (c) 2012 Red Hat, Inc.
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
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.
24 */
25
26#include "config-host.h"
27#include "qemu-common.h"
28#include "trace.h"
29#include "monitor.h"
30#include "block.h"
31#include "blockjob.h"
32#include "block_int.h"
33#include "qjson.h"
34#include "qemu-coroutine.h"
35#include "qmp-commands.h"
36#include "qemu-timer.h"
37
38void *block_job_create(const BlockJobType *job_type, BlockDriverState *bs,
39 int64_t speed, BlockDriverCompletionFunc *cb,
40 void *opaque, Error **errp)
41{
42 BlockJob *job;
43
44 if (bs->job || bdrv_in_use(bs)) {
45 error_set(errp, QERR_DEVICE_IN_USE, bdrv_get_device_name(bs));
46 return NULL;
47 }
48 bdrv_set_in_use(bs, 1);
49
50 job = g_malloc0(job_type->instance_size);
51 job->job_type = job_type;
52 job->bs = bs;
53 job->cb = cb;
54 job->opaque = opaque;
55 job->busy = true;
56 bs->job = job;
57
58 /* Only set speed when necessary to avoid NotSupported error */
59 if (speed != 0) {
60 Error *local_err = NULL;
61
62 block_job_set_speed(job, speed, &local_err);
63 if (error_is_set(&local_err)) {
64 bs->job = NULL;
65 g_free(job);
66 bdrv_set_in_use(bs, 0);
67 error_propagate(errp, local_err);
68 return NULL;
69 }
70 }
71 return job;
72}
73
74void block_job_complete(BlockJob *job, int ret)
75{
76 BlockDriverState *bs = job->bs;
77
78 assert(bs->job == job);
79 job->cb(job->opaque, ret);
80 bs->job = NULL;
81 g_free(job);
82 bdrv_set_in_use(bs, 0);
83}
84
85void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp)
86{
87 Error *local_err = NULL;
88
89 if (!job->job_type->set_speed) {
90 error_set(errp, QERR_NOT_SUPPORTED);
91 return;
92 }
93 job->job_type->set_speed(job, speed, &local_err);
94 if (error_is_set(&local_err)) {
95 error_propagate(errp, local_err);
96 return;
97 }
98
99 job->speed = speed;
100}
101
102void block_job_cancel(BlockJob *job)
103{
104 job->cancelled = true;
105 if (job->co && !job->busy) {
106 qemu_coroutine_enter(job->co, NULL);
107 }
108}
109
110bool block_job_is_cancelled(BlockJob *job)
111{
112 return job->cancelled;
113}
114
115struct BlockCancelData {
116 BlockJob *job;
117 BlockDriverCompletionFunc *cb;
118 void *opaque;
119 bool cancelled;
120 int ret;
121};
122
123static void block_job_cancel_cb(void *opaque, int ret)
124{
125 struct BlockCancelData *data = opaque;
126
127 data->cancelled = block_job_is_cancelled(data->job);
128 data->ret = ret;
129 data->cb(data->opaque, ret);
130}
131
132int block_job_cancel_sync(BlockJob *job)
133{
134 struct BlockCancelData data;
135 BlockDriverState *bs = job->bs;
136
137 assert(bs->job == job);
138
139 /* Set up our own callback to store the result and chain to
140 * the original callback.
141 */
142 data.job = job;
143 data.cb = job->cb;
144 data.opaque = job->opaque;
145 data.ret = -EINPROGRESS;
146 job->cb = block_job_cancel_cb;
147 job->opaque = &data;
148 block_job_cancel(job);
149 while (data.ret == -EINPROGRESS) {
150 qemu_aio_wait();
151 }
152 return (data.cancelled && data.ret == 0) ? -ECANCELED : data.ret;
153}
154
155void block_job_sleep_ns(BlockJob *job, QEMUClock *clock, int64_t ns)
156{
157 /* Check cancellation *before* setting busy = false, too! */
158 if (!block_job_is_cancelled(job)) {
159 job->busy = false;
160 co_sleep_ns(clock, ns);
161 job->busy = true;
162 }
163}
30e628b7
PB
164
165BlockJobInfo *block_job_query(BlockJob *job)
166{
167 BlockJobInfo *info = g_new0(BlockJobInfo, 1);
168 info->type = g_strdup(job->job_type->job_type);
169 info->device = g_strdup(bdrv_get_device_name(job->bs));
170 info->len = job->len;
8d65883f 171 info->busy = job->busy;
30e628b7
PB
172 info->offset = job->offset;
173 info->speed = job->speed;
174 return info;
175}