vpc: fix beX_to_cpu() and cpu_to_beX() confusion
[qemu/cris-port.git] / blockjob.c
blob0689fdd2b55c700100800a2acbbe34d6fd92a8fd
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 "config-host.h"
27 #include "qemu-common.h"
28 #include "trace.h"
29 #include "block/block.h"
30 #include "block/blockjob.h"
31 #include "block/block_int.h"
32 #include "qapi/qmp/qjson.h"
33 #include "block/coroutine.h"
34 #include "qmp-commands.h"
35 #include "qemu/timer.h"
36 #include "qapi-event.h"
38 void *block_job_create(const BlockJobDriver *driver, BlockDriverState *bs,
39 int64_t speed, BlockDriverCompletionFunc *cb,
40 void *opaque, Error **errp)
42 BlockJob *job;
44 if (bs->job) {
45 error_set(errp, QERR_DEVICE_IN_USE, bdrv_get_device_name(bs));
46 return NULL;
48 bdrv_ref(bs);
49 job = g_malloc0(driver->instance_size);
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);
54 job->driver = driver;
55 job->bs = bs;
56 job->cb = cb;
57 job->opaque = opaque;
58 job->busy = true;
59 bs->job = job;
61 /* Only set speed when necessary to avoid NotSupported error */
62 if (speed != 0) {
63 Error *local_err = NULL;
65 block_job_set_speed(job, speed, &local_err);
66 if (local_err) {
67 bs->job = NULL;
68 bdrv_op_unblock_all(bs, job->blocker);
69 error_free(job->blocker);
70 g_free(job);
71 error_propagate(errp, local_err);
72 return NULL;
75 return job;
78 void block_job_completed(BlockJob *job, int ret)
80 BlockDriverState *bs = job->bs;
82 assert(bs->job == job);
83 job->cb(job->opaque, ret);
84 bs->job = NULL;
85 bdrv_op_unblock_all(bs, job->blocker);
86 error_free(job->blocker);
87 g_free(job);
90 void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp)
92 Error *local_err = NULL;
94 if (!job->driver->set_speed) {
95 error_set(errp, QERR_UNSUPPORTED);
96 return;
98 job->driver->set_speed(job, speed, &local_err);
99 if (local_err) {
100 error_propagate(errp, local_err);
101 return;
104 job->speed = speed;
107 void block_job_complete(BlockJob *job, Error **errp)
109 if (job->paused || job->cancelled || !job->driver->complete) {
110 error_set(errp, QERR_BLOCK_JOB_NOT_READY, job->bs->device_name);
111 return;
114 job->driver->complete(job, errp);
117 void block_job_pause(BlockJob *job)
119 job->paused = true;
122 bool block_job_is_paused(BlockJob *job)
124 return job->paused;
127 void block_job_resume(BlockJob *job)
129 job->paused = false;
130 block_job_iostatus_reset(job);
131 if (job->co && !job->busy) {
132 qemu_coroutine_enter(job->co, NULL);
136 void block_job_cancel(BlockJob *job)
138 job->cancelled = true;
139 block_job_resume(job);
142 bool block_job_is_cancelled(BlockJob *job)
144 return job->cancelled;
147 void block_job_iostatus_reset(BlockJob *job)
149 job->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
150 if (job->driver->iostatus_reset) {
151 job->driver->iostatus_reset(job);
155 struct BlockCancelData {
156 BlockJob *job;
157 BlockDriverCompletionFunc *cb;
158 void *opaque;
159 bool cancelled;
160 int ret;
163 static void block_job_cancel_cb(void *opaque, int ret)
165 struct BlockCancelData *data = opaque;
167 data->cancelled = block_job_is_cancelled(data->job);
168 data->ret = ret;
169 data->cb(data->opaque, ret);
172 int block_job_cancel_sync(BlockJob *job)
174 struct BlockCancelData data;
175 BlockDriverState *bs = job->bs;
177 assert(bs->job == job);
179 /* Set up our own callback to store the result and chain to
180 * the original callback.
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 aio_poll(bdrv_get_aio_context(bs), true);
192 return (data.cancelled && data.ret == 0) ? -ECANCELED : data.ret;
195 void block_job_sleep_ns(BlockJob *job, QEMUClockType type, int64_t ns)
197 assert(job->busy);
199 /* Check cancellation *before* setting busy = false, too! */
200 if (block_job_is_cancelled(job)) {
201 return;
204 job->busy = false;
205 if (block_job_is_paused(job)) {
206 qemu_coroutine_yield();
207 } else {
208 co_aio_sleep_ns(bdrv_get_aio_context(job->bs), type, ns);
210 job->busy = true;
213 void block_job_yield(BlockJob *job)
215 assert(job->busy);
217 /* Check cancellation *before* setting busy = false, too! */
218 if (block_job_is_cancelled(job)) {
219 return;
222 job->busy = false;
223 qemu_coroutine_yield();
224 job->busy = true;
227 BlockJobInfo *block_job_query(BlockJob *job)
229 BlockJobInfo *info = g_new0(BlockJobInfo, 1);
230 info->type = g_strdup(BlockJobType_lookup[job->driver->job_type]);
231 info->device = g_strdup(bdrv_get_device_name(job->bs));
232 info->len = job->len;
233 info->busy = job->busy;
234 info->paused = job->paused;
235 info->offset = job->offset;
236 info->speed = job->speed;
237 info->io_status = job->iostatus;
238 return info;
241 static void block_job_iostatus_set_err(BlockJob *job, int error)
243 if (job->iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
244 job->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE :
245 BLOCK_DEVICE_IO_STATUS_FAILED;
249 void block_job_event_cancelled(BlockJob *job)
251 qapi_event_send_block_job_cancelled(job->driver->job_type,
252 bdrv_get_device_name(job->bs),
253 job->len,
254 job->offset,
255 job->speed,
256 &error_abort);
259 void block_job_event_completed(BlockJob *job, const char *msg)
261 qapi_event_send_block_job_completed(job->driver->job_type,
262 bdrv_get_device_name(job->bs),
263 job->len,
264 job->offset,
265 job->speed,
266 !!msg,
267 msg,
268 &error_abort);
271 void block_job_event_ready(BlockJob *job)
273 qapi_event_send_block_job_ready(job->driver->job_type,
274 bdrv_get_device_name(job->bs),
275 job->len,
276 job->offset,
277 job->speed, &error_abort);
280 BlockErrorAction block_job_error_action(BlockJob *job, BlockDriverState *bs,
281 BlockdevOnError on_err,
282 int is_read, int error)
284 BlockErrorAction action;
286 switch (on_err) {
287 case BLOCKDEV_ON_ERROR_ENOSPC:
288 action = (error == ENOSPC) ?
289 BLOCK_ERROR_ACTION_STOP : BLOCK_ERROR_ACTION_REPORT;
290 break;
291 case BLOCKDEV_ON_ERROR_STOP:
292 action = BLOCK_ERROR_ACTION_STOP;
293 break;
294 case BLOCKDEV_ON_ERROR_REPORT:
295 action = BLOCK_ERROR_ACTION_REPORT;
296 break;
297 case BLOCKDEV_ON_ERROR_IGNORE:
298 action = BLOCK_ERROR_ACTION_IGNORE;
299 break;
300 default:
301 abort();
303 qapi_event_send_block_job_error(bdrv_get_device_name(job->bs),
304 is_read ? IO_OPERATION_TYPE_READ :
305 IO_OPERATION_TYPE_WRITE,
306 action, &error_abort);
307 if (action == BLOCK_ERROR_ACTION_STOP) {
308 block_job_pause(job);
309 block_job_iostatus_set_err(job, error);
310 if (bs != job->bs) {
311 bdrv_iostatus_set_err(bs, error);
314 return action;