]> git.proxmox.com Git - mirror_qemu.git/blame - blockjob.c
Add new vhost-user netdev backend
[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"
83c9089e 29#include "monitor/monitor.h"
737e150e
PB
30#include "block/block.h"
31#include "block/blockjob.h"
32#include "block/block_int.h"
7b1b5d19 33#include "qapi/qmp/qjson.h"
737e150e 34#include "block/coroutine.h"
2f0c9fe6 35#include "qmp-commands.h"
1de7afc9 36#include "qemu/timer.h"
2f0c9fe6 37
3fc4b10a 38void *block_job_create(const BlockJobDriver *driver, BlockDriverState *bs,
2f0c9fe6
PB
39 int64_t speed, BlockDriverCompletionFunc *cb,
40 void *opaque, Error **errp)
41{
42 BlockJob *job;
43
628ff683 44 if (bs->job) {
2f0c9fe6
PB
45 error_set(errp, QERR_DEVICE_IN_USE, bdrv_get_device_name(bs));
46 return NULL;
47 }
fa510ebf 48 bdrv_ref(bs);
3fc4b10a 49 job = g_malloc0(driver->instance_size);
3718d8ab
FZ
50 error_setg(&job->blocker, "block device is in use by block job: %s",
51 BlockJobType_lookup[driver->job_type]);
52 bdrv_op_block_all(bs, job->blocker);
53
3fc4b10a 54 job->driver = driver;
2f0c9fe6
PB
55 job->bs = bs;
56 job->cb = cb;
57 job->opaque = opaque;
58 job->busy = true;
59 bs->job = job;
60
61 /* Only set speed when necessary to avoid NotSupported error */
62 if (speed != 0) {
63 Error *local_err = NULL;
64
65 block_job_set_speed(job, speed, &local_err);
84d18f06 66 if (local_err) {
2f0c9fe6 67 bs->job = NULL;
3718d8ab
FZ
68 bdrv_op_unblock_all(bs, job->blocker);
69 error_free(job->blocker);
2f0c9fe6 70 g_free(job);
2f0c9fe6
PB
71 error_propagate(errp, local_err);
72 return NULL;
73 }
74 }
75 return job;
76}
77
65f46322 78void block_job_completed(BlockJob *job, int ret)
2f0c9fe6
PB
79{
80 BlockDriverState *bs = job->bs;
81
82 assert(bs->job == job);
83 job->cb(job->opaque, ret);
84 bs->job = NULL;
3718d8ab
FZ
85 bdrv_op_unblock_all(bs, job->blocker);
86 error_free(job->blocker);
2f0c9fe6 87 g_free(job);
2f0c9fe6
PB
88}
89
90void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp)
91{
92 Error *local_err = NULL;
93
3fc4b10a 94 if (!job->driver->set_speed) {
0b15abfc 95 error_set(errp, QERR_UNSUPPORTED);
2f0c9fe6
PB
96 return;
97 }
3fc4b10a 98 job->driver->set_speed(job, speed, &local_err);
84d18f06 99 if (local_err) {
2f0c9fe6
PB
100 error_propagate(errp, local_err);
101 return;
102 }
103
104 job->speed = speed;
105}
106
aeae883b
PB
107void block_job_complete(BlockJob *job, Error **errp)
108{
3fc4b10a 109 if (job->paused || job->cancelled || !job->driver->complete) {
aeae883b
PB
110 error_set(errp, QERR_BLOCK_JOB_NOT_READY, job->bs->device_name);
111 return;
112 }
113
3fc4b10a 114 job->driver->complete(job, errp);
aeae883b
PB
115}
116
8acc72a4 117void block_job_pause(BlockJob *job)
2f0c9fe6 118{
8acc72a4
PB
119 job->paused = true;
120}
121
122bool block_job_is_paused(BlockJob *job)
123{
124 return job->paused;
125}
126
127void block_job_resume(BlockJob *job)
128{
129 job->paused = false;
32c81a4a 130 block_job_iostatus_reset(job);
2f0c9fe6
PB
131 if (job->co && !job->busy) {
132 qemu_coroutine_enter(job->co, NULL);
133 }
134}
135
8acc72a4
PB
136void block_job_cancel(BlockJob *job)
137{
138 job->cancelled = true;
139 block_job_resume(job);
140}
141
2f0c9fe6
PB
142bool block_job_is_cancelled(BlockJob *job)
143{
144 return job->cancelled;
145}
146
32c81a4a
PB
147void block_job_iostatus_reset(BlockJob *job)
148{
149 job->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
3fc4b10a
FZ
150 if (job->driver->iostatus_reset) {
151 job->driver->iostatus_reset(job);
3bd293c3 152 }
32c81a4a
PB
153}
154
2f0c9fe6
PB
155struct BlockCancelData {
156 BlockJob *job;
157 BlockDriverCompletionFunc *cb;
158 void *opaque;
159 bool cancelled;
160 int ret;
161};
162
163static void block_job_cancel_cb(void *opaque, int ret)
164{
165 struct BlockCancelData *data = opaque;
166
167 data->cancelled = block_job_is_cancelled(data->job);
168 data->ret = ret;
169 data->cb(data->opaque, ret);
170}
171
172int block_job_cancel_sync(BlockJob *job)
173{
174 struct BlockCancelData data;
175 BlockDriverState *bs = job->bs;
176
177 assert(bs->job == job);
178
179 /* Set up our own callback to store the result and chain to
180 * the original callback.
181 */
182 data.job = job;
183 data.cb = job->cb;
184 data.opaque = job->opaque;
185 data.ret = -EINPROGRESS;
186 job->cb = block_job_cancel_cb;
187 job->opaque = &data;
188 block_job_cancel(job);
189 while (data.ret == -EINPROGRESS) {
190 qemu_aio_wait();
191 }
192 return (data.cancelled && data.ret == 0) ? -ECANCELED : data.ret;
193}
194
7483d1e5 195void block_job_sleep_ns(BlockJob *job, QEMUClockType type, int64_t ns)
2f0c9fe6 196{
8acc72a4
PB
197 assert(job->busy);
198
2f0c9fe6 199 /* Check cancellation *before* setting busy = false, too! */
8acc72a4
PB
200 if (block_job_is_cancelled(job)) {
201 return;
202 }
203
204 job->busy = false;
205 if (block_job_is_paused(job)) {
206 qemu_coroutine_yield();
207 } else {
7483d1e5 208 co_sleep_ns(type, ns);
2f0c9fe6 209 }
8acc72a4 210 job->busy = true;
2f0c9fe6 211}
30e628b7
PB
212
213BlockJobInfo *block_job_query(BlockJob *job)
214{
215 BlockJobInfo *info = g_new0(BlockJobInfo, 1);
79e14bf7 216 info->type = g_strdup(BlockJobType_lookup[job->driver->job_type]);
32c81a4a
PB
217 info->device = g_strdup(bdrv_get_device_name(job->bs));
218 info->len = job->len;
219 info->busy = job->busy;
220 info->paused = job->paused;
221 info->offset = job->offset;
222 info->speed = job->speed;
223 info->io_status = job->iostatus;
30e628b7
PB
224 return info;
225}
32c81a4a
PB
226
227static void block_job_iostatus_set_err(BlockJob *job, int error)
228{
229 if (job->iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
230 job->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE :
231 BLOCK_DEVICE_IO_STATUS_FAILED;
232 }
233}
234
235
a66a2a36
PB
236QObject *qobject_from_block_job(BlockJob *job)
237{
238 return qobject_from_jsonf("{ 'type': %s,"
239 "'device': %s,"
240 "'len': %" PRId64 ","
241 "'offset': %" PRId64 ","
242 "'speed': %" PRId64 " }",
79e14bf7 243 BlockJobType_lookup[job->driver->job_type],
a66a2a36
PB
244 bdrv_get_device_name(job->bs),
245 job->len,
246 job->offset,
247 job->speed);
248}
249
250void block_job_ready(BlockJob *job)
251{
252 QObject *data = qobject_from_block_job(job);
253 monitor_protocol_event(QEVENT_BLOCK_JOB_READY, data);
254 qobject_decref(data);
255}
256
32c81a4a
PB
257BlockErrorAction block_job_error_action(BlockJob *job, BlockDriverState *bs,
258 BlockdevOnError on_err,
259 int is_read, int error)
260{
261 BlockErrorAction action;
262
263 switch (on_err) {
264 case BLOCKDEV_ON_ERROR_ENOSPC:
265 action = (error == ENOSPC) ? BDRV_ACTION_STOP : BDRV_ACTION_REPORT;
266 break;
267 case BLOCKDEV_ON_ERROR_STOP:
268 action = BDRV_ACTION_STOP;
269 break;
270 case BLOCKDEV_ON_ERROR_REPORT:
271 action = BDRV_ACTION_REPORT;
272 break;
273 case BLOCKDEV_ON_ERROR_IGNORE:
274 action = BDRV_ACTION_IGNORE;
275 break;
276 default:
277 abort();
278 }
279 bdrv_emit_qmp_error_event(job->bs, QEVENT_BLOCK_JOB_ERROR, action, is_read);
280 if (action == BDRV_ACTION_STOP) {
281 block_job_pause(job);
282 block_job_iostatus_set_err(job, error);
283 if (bs != job->bs) {
284 bdrv_iostatus_set_err(bs, error);
285 }
286 }
287 return action;
288}