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
);
53 bdrv_op_unblock(bs
, BLOCK_OP_TYPE_DATAPLANE
, job
->blocker
);
62 /* Only set speed when necessary to avoid NotSupported error */
64 Error
*local_err
= NULL
;
66 block_job_set_speed(job
, speed
, &local_err
);
69 bdrv_op_unblock_all(bs
, job
->blocker
);
70 error_free(job
->blocker
);
72 error_propagate(errp
, local_err
);
79 void block_job_completed(BlockJob
*job
, int ret
)
81 BlockDriverState
*bs
= job
->bs
;
83 assert(bs
->job
== job
);
84 job
->cb(job
->opaque
, ret
);
86 bdrv_op_unblock_all(bs
, job
->blocker
);
87 error_free(job
->blocker
);
91 void block_job_set_speed(BlockJob
*job
, int64_t speed
, Error
**errp
)
93 Error
*local_err
= NULL
;
95 if (!job
->driver
->set_speed
) {
96 error_set(errp
, QERR_UNSUPPORTED
);
99 job
->driver
->set_speed(job
, speed
, &local_err
);
101 error_propagate(errp
, local_err
);
108 void block_job_complete(BlockJob
*job
, Error
**errp
)
110 if (job
->paused
|| job
->cancelled
|| !job
->driver
->complete
) {
111 error_set(errp
, QERR_BLOCK_JOB_NOT_READY
,
112 bdrv_get_device_name(job
->bs
));
116 job
->driver
->complete(job
, errp
);
119 void block_job_pause(BlockJob
*job
)
124 bool block_job_is_paused(BlockJob
*job
)
129 void block_job_resume(BlockJob
*job
)
132 block_job_iostatus_reset(job
);
133 if (job
->co
&& !job
->busy
) {
134 qemu_coroutine_enter(job
->co
, NULL
);
138 void block_job_cancel(BlockJob
*job
)
140 job
->cancelled
= true;
141 block_job_resume(job
);
144 bool block_job_is_cancelled(BlockJob
*job
)
146 return job
->cancelled
;
149 void block_job_iostatus_reset(BlockJob
*job
)
151 job
->iostatus
= BLOCK_DEVICE_IO_STATUS_OK
;
152 if (job
->driver
->iostatus_reset
) {
153 job
->driver
->iostatus_reset(job
);
157 struct BlockFinishData
{
159 BlockCompletionFunc
*cb
;
165 static void block_job_finish_cb(void *opaque
, int ret
)
167 struct BlockFinishData
*data
= opaque
;
169 data
->cancelled
= block_job_is_cancelled(data
->job
);
171 data
->cb(data
->opaque
, ret
);
174 static int block_job_finish_sync(BlockJob
*job
,
175 void (*finish
)(BlockJob
*, Error
**errp
),
178 struct BlockFinishData data
;
179 BlockDriverState
*bs
= job
->bs
;
180 Error
*local_err
= NULL
;
182 assert(bs
->job
== job
);
184 /* Set up our own callback to store the result and chain to
185 * the original callback.
189 data
.opaque
= job
->opaque
;
190 data
.ret
= -EINPROGRESS
;
191 job
->cb
= block_job_finish_cb
;
193 finish(job
, &local_err
);
195 error_propagate(errp
, local_err
);
198 while (data
.ret
== -EINPROGRESS
) {
199 aio_poll(bdrv_get_aio_context(bs
), true);
201 return (data
.cancelled
&& data
.ret
== 0) ? -ECANCELED
: data
.ret
;
204 /* A wrapper around block_job_cancel() taking an Error ** parameter so it may be
205 * used with block_job_finish_sync() without the need for (rather nasty)
206 * function pointer casts there. */
207 static void block_job_cancel_err(BlockJob
*job
, Error
**errp
)
209 block_job_cancel(job
);
212 int block_job_cancel_sync(BlockJob
*job
)
214 return block_job_finish_sync(job
, &block_job_cancel_err
, NULL
);
217 int block_job_complete_sync(BlockJob
*job
, Error
**errp
)
219 return block_job_finish_sync(job
, &block_job_complete
, errp
);
222 void block_job_sleep_ns(BlockJob
*job
, QEMUClockType type
, int64_t ns
)
226 /* Check cancellation *before* setting busy = false, too! */
227 if (block_job_is_cancelled(job
)) {
232 if (block_job_is_paused(job
)) {
233 qemu_coroutine_yield();
235 co_aio_sleep_ns(bdrv_get_aio_context(job
->bs
), type
, ns
);
240 void block_job_yield(BlockJob
*job
)
244 /* Check cancellation *before* setting busy = false, too! */
245 if (block_job_is_cancelled(job
)) {
250 qemu_coroutine_yield();
254 BlockJobInfo
*block_job_query(BlockJob
*job
)
256 BlockJobInfo
*info
= g_new0(BlockJobInfo
, 1);
257 info
->type
= g_strdup(BlockJobType_lookup
[job
->driver
->job_type
]);
258 info
->device
= g_strdup(bdrv_get_device_name(job
->bs
));
259 info
->len
= job
->len
;
260 info
->busy
= job
->busy
;
261 info
->paused
= job
->paused
;
262 info
->offset
= job
->offset
;
263 info
->speed
= job
->speed
;
264 info
->io_status
= job
->iostatus
;
265 info
->ready
= job
->ready
;
269 static void block_job_iostatus_set_err(BlockJob
*job
, int error
)
271 if (job
->iostatus
== BLOCK_DEVICE_IO_STATUS_OK
) {
272 job
->iostatus
= error
== ENOSPC
? BLOCK_DEVICE_IO_STATUS_NOSPACE
:
273 BLOCK_DEVICE_IO_STATUS_FAILED
;
277 void block_job_event_cancelled(BlockJob
*job
)
279 qapi_event_send_block_job_cancelled(job
->driver
->job_type
,
280 bdrv_get_device_name(job
->bs
),
287 void block_job_event_completed(BlockJob
*job
, const char *msg
)
289 qapi_event_send_block_job_completed(job
->driver
->job_type
,
290 bdrv_get_device_name(job
->bs
),
299 void block_job_event_ready(BlockJob
*job
)
303 qapi_event_send_block_job_ready(job
->driver
->job_type
,
304 bdrv_get_device_name(job
->bs
),
307 job
->speed
, &error_abort
);
310 BlockErrorAction
block_job_error_action(BlockJob
*job
, BlockDriverState
*bs
,
311 BlockdevOnError on_err
,
312 int is_read
, int error
)
314 BlockErrorAction action
;
317 case BLOCKDEV_ON_ERROR_ENOSPC
:
318 action
= (error
== ENOSPC
) ?
319 BLOCK_ERROR_ACTION_STOP
: BLOCK_ERROR_ACTION_REPORT
;
321 case BLOCKDEV_ON_ERROR_STOP
:
322 action
= BLOCK_ERROR_ACTION_STOP
;
324 case BLOCKDEV_ON_ERROR_REPORT
:
325 action
= BLOCK_ERROR_ACTION_REPORT
;
327 case BLOCKDEV_ON_ERROR_IGNORE
:
328 action
= BLOCK_ERROR_ACTION_IGNORE
;
333 qapi_event_send_block_job_error(bdrv_get_device_name(job
->bs
),
334 is_read
? IO_OPERATION_TYPE_READ
:
335 IO_OPERATION_TYPE_WRITE
,
336 action
, &error_abort
);
337 if (action
== BLOCK_ERROR_ACTION_STOP
) {
338 block_job_pause(job
);
339 block_job_iostatus_set_err(job
, error
);
341 bdrv_iostatus_set_err(bs
, error
);
350 AioContext
*aio_context
;
351 BlockJobDeferToMainLoopFn
*fn
;
353 } BlockJobDeferToMainLoopData
;
355 static void block_job_defer_to_main_loop_bh(void *opaque
)
357 BlockJobDeferToMainLoopData
*data
= opaque
;
358 AioContext
*aio_context
;
360 qemu_bh_delete(data
->bh
);
362 /* Prevent race with block_job_defer_to_main_loop() */
363 aio_context_acquire(data
->aio_context
);
365 /* Fetch BDS AioContext again, in case it has changed */
366 aio_context
= bdrv_get_aio_context(data
->job
->bs
);
367 aio_context_acquire(aio_context
);
369 data
->fn(data
->job
, data
->opaque
);
371 aio_context_release(aio_context
);
373 aio_context_release(data
->aio_context
);
378 void block_job_defer_to_main_loop(BlockJob
*job
,
379 BlockJobDeferToMainLoopFn
*fn
,
382 BlockJobDeferToMainLoopData
*data
= g_malloc(sizeof(*data
));
384 data
->bh
= qemu_bh_new(block_job_defer_to_main_loop_bh
, data
);
385 data
->aio_context
= bdrv_get_aio_context(job
->bs
);
387 data
->opaque
= opaque
;
389 qemu_bh_schedule(data
->bh
);