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
26 #include "config-host.h"
27 #include "qemu-common.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
, BlockCompletionFunc
*cb
,
40 void *opaque
, Error
**errp
)
45 error_set(errp
, QERR_DEVICE_IN_USE
, bdrv_get_device_name(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
);
61 /* Only set speed when necessary to avoid NotSupported error */
63 Error
*local_err
= NULL
;
65 block_job_set_speed(job
, speed
, &local_err
);
68 bdrv_op_unblock_all(bs
, job
->blocker
);
69 error_free(job
->blocker
);
71 error_propagate(errp
, local_err
);
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
);
85 bdrv_op_unblock_all(bs
, job
->blocker
);
86 error_free(job
->blocker
);
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
);
98 job
->driver
->set_speed(job
, speed
, &local_err
);
100 error_propagate(errp
, local_err
);
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
,
111 bdrv_get_device_name(job
->bs
));
115 job
->driver
->complete(job
, errp
);
118 void block_job_pause(BlockJob
*job
)
123 bool block_job_is_paused(BlockJob
*job
)
128 void block_job_resume(BlockJob
*job
)
131 block_job_iostatus_reset(job
);
132 if (job
->co
&& !job
->busy
) {
133 qemu_coroutine_enter(job
->co
, NULL
);
137 void block_job_cancel(BlockJob
*job
)
139 job
->cancelled
= true;
140 block_job_resume(job
);
143 bool block_job_is_cancelled(BlockJob
*job
)
145 return job
->cancelled
;
148 void block_job_iostatus_reset(BlockJob
*job
)
150 job
->iostatus
= BLOCK_DEVICE_IO_STATUS_OK
;
151 if (job
->driver
->iostatus_reset
) {
152 job
->driver
->iostatus_reset(job
);
156 struct BlockFinishData
{
158 BlockCompletionFunc
*cb
;
164 static void block_job_finish_cb(void *opaque
, int ret
)
166 struct BlockFinishData
*data
= opaque
;
168 data
->cancelled
= block_job_is_cancelled(data
->job
);
170 data
->cb(data
->opaque
, ret
);
173 static int block_job_finish_sync(BlockJob
*job
,
174 void (*finish
)(BlockJob
*, Error
**errp
),
177 struct BlockFinishData data
;
178 BlockDriverState
*bs
= job
->bs
;
179 Error
*local_err
= NULL
;
181 assert(bs
->job
== job
);
183 /* Set up our own callback to store the result and chain to
184 * the original callback.
188 data
.opaque
= job
->opaque
;
189 data
.ret
= -EINPROGRESS
;
190 job
->cb
= block_job_finish_cb
;
192 finish(job
, &local_err
);
194 error_propagate(errp
, local_err
);
197 while (data
.ret
== -EINPROGRESS
) {
198 aio_poll(bdrv_get_aio_context(bs
), true);
200 return (data
.cancelled
&& data
.ret
== 0) ? -ECANCELED
: data
.ret
;
203 /* A wrapper around block_job_cancel() taking an Error ** parameter so it may be
204 * used with block_job_finish_sync() without the need for (rather nasty)
205 * function pointer casts there. */
206 static void block_job_cancel_err(BlockJob
*job
, Error
**errp
)
208 block_job_cancel(job
);
211 int block_job_cancel_sync(BlockJob
*job
)
213 return block_job_finish_sync(job
, &block_job_cancel_err
, NULL
);
216 int block_job_complete_sync(BlockJob
*job
, Error
**errp
)
218 return block_job_finish_sync(job
, &block_job_complete
, errp
);
221 void block_job_sleep_ns(BlockJob
*job
, QEMUClockType type
, int64_t ns
)
225 /* Check cancellation *before* setting busy = false, too! */
226 if (block_job_is_cancelled(job
)) {
231 if (block_job_is_paused(job
)) {
232 qemu_coroutine_yield();
234 co_aio_sleep_ns(bdrv_get_aio_context(job
->bs
), type
, ns
);
239 void block_job_yield(BlockJob
*job
)
243 /* Check cancellation *before* setting busy = false, too! */
244 if (block_job_is_cancelled(job
)) {
249 qemu_coroutine_yield();
253 BlockJobInfo
*block_job_query(BlockJob
*job
)
255 BlockJobInfo
*info
= g_new0(BlockJobInfo
, 1);
256 info
->type
= g_strdup(BlockJobType_lookup
[job
->driver
->job_type
]);
257 info
->device
= g_strdup(bdrv_get_device_name(job
->bs
));
258 info
->len
= job
->len
;
259 info
->busy
= job
->busy
;
260 info
->paused
= job
->paused
;
261 info
->offset
= job
->offset
;
262 info
->speed
= job
->speed
;
263 info
->io_status
= job
->iostatus
;
264 info
->ready
= job
->ready
;
268 static void block_job_iostatus_set_err(BlockJob
*job
, int error
)
270 if (job
->iostatus
== BLOCK_DEVICE_IO_STATUS_OK
) {
271 job
->iostatus
= error
== ENOSPC
? BLOCK_DEVICE_IO_STATUS_NOSPACE
:
272 BLOCK_DEVICE_IO_STATUS_FAILED
;
276 void block_job_event_cancelled(BlockJob
*job
)
278 qapi_event_send_block_job_cancelled(job
->driver
->job_type
,
279 bdrv_get_device_name(job
->bs
),
286 void block_job_event_completed(BlockJob
*job
, const char *msg
)
288 qapi_event_send_block_job_completed(job
->driver
->job_type
,
289 bdrv_get_device_name(job
->bs
),
298 void block_job_event_ready(BlockJob
*job
)
302 qapi_event_send_block_job_ready(job
->driver
->job_type
,
303 bdrv_get_device_name(job
->bs
),
306 job
->speed
, &error_abort
);
309 BlockErrorAction
block_job_error_action(BlockJob
*job
, BlockDriverState
*bs
,
310 BlockdevOnError on_err
,
311 int is_read
, int error
)
313 BlockErrorAction action
;
316 case BLOCKDEV_ON_ERROR_ENOSPC
:
317 action
= (error
== ENOSPC
) ?
318 BLOCK_ERROR_ACTION_STOP
: BLOCK_ERROR_ACTION_REPORT
;
320 case BLOCKDEV_ON_ERROR_STOP
:
321 action
= BLOCK_ERROR_ACTION_STOP
;
323 case BLOCKDEV_ON_ERROR_REPORT
:
324 action
= BLOCK_ERROR_ACTION_REPORT
;
326 case BLOCKDEV_ON_ERROR_IGNORE
:
327 action
= BLOCK_ERROR_ACTION_IGNORE
;
332 qapi_event_send_block_job_error(bdrv_get_device_name(job
->bs
),
333 is_read
? IO_OPERATION_TYPE_READ
:
334 IO_OPERATION_TYPE_WRITE
,
335 action
, &error_abort
);
336 if (action
== BLOCK_ERROR_ACTION_STOP
) {
337 block_job_pause(job
);
338 block_job_iostatus_set_err(job
, error
);
340 bdrv_iostatus_set_err(bs
, error
);