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"
32 #include "block_int.h"
34 #include "qemu-coroutine.h"
35 #include "qmp-commands.h"
36 #include "qemu-timer.h"
38 void *block_job_create(const BlockJobType
*job_type
, BlockDriverState
*bs
,
39 int64_t speed
, BlockDriverCompletionFunc
*cb
,
40 void *opaque
, Error
**errp
)
44 if (bs
->job
|| bdrv_in_use(bs
)) {
45 error_set(errp
, QERR_DEVICE_IN_USE
, bdrv_get_device_name(bs
));
48 bdrv_set_in_use(bs
, 1);
50 job
= g_malloc0(job_type
->instance_size
);
51 job
->job_type
= job_type
;
58 /* Only set speed when necessary to avoid NotSupported error */
60 Error
*local_err
= NULL
;
62 block_job_set_speed(job
, speed
, &local_err
);
63 if (error_is_set(&local_err
)) {
66 bdrv_set_in_use(bs
, 0);
67 error_propagate(errp
, local_err
);
74 void block_job_complete(BlockJob
*job
, int ret
)
76 BlockDriverState
*bs
= job
->bs
;
78 assert(bs
->job
== job
);
79 job
->cb(job
->opaque
, ret
);
82 bdrv_set_in_use(bs
, 0);
85 void block_job_set_speed(BlockJob
*job
, int64_t speed
, Error
**errp
)
87 Error
*local_err
= NULL
;
89 if (!job
->job_type
->set_speed
) {
90 error_set(errp
, QERR_NOT_SUPPORTED
);
93 job
->job_type
->set_speed(job
, speed
, &local_err
);
94 if (error_is_set(&local_err
)) {
95 error_propagate(errp
, local_err
);
102 void block_job_pause(BlockJob
*job
)
107 bool block_job_is_paused(BlockJob
*job
)
112 void block_job_resume(BlockJob
*job
)
115 block_job_iostatus_reset(job
);
116 if (job
->co
&& !job
->busy
) {
117 qemu_coroutine_enter(job
->co
, NULL
);
121 void block_job_cancel(BlockJob
*job
)
123 job
->cancelled
= true;
124 block_job_resume(job
);
127 bool block_job_is_cancelled(BlockJob
*job
)
129 return job
->cancelled
;
132 void block_job_iostatus_reset(BlockJob
*job
)
134 job
->iostatus
= BLOCK_DEVICE_IO_STATUS_OK
;
137 struct BlockCancelData
{
139 BlockDriverCompletionFunc
*cb
;
145 static void block_job_cancel_cb(void *opaque
, int ret
)
147 struct BlockCancelData
*data
= opaque
;
149 data
->cancelled
= block_job_is_cancelled(data
->job
);
151 data
->cb(data
->opaque
, ret
);
154 int block_job_cancel_sync(BlockJob
*job
)
156 struct BlockCancelData data
;
157 BlockDriverState
*bs
= job
->bs
;
159 assert(bs
->job
== job
);
161 /* Set up our own callback to store the result and chain to
162 * the original callback.
166 data
.opaque
= job
->opaque
;
167 data
.ret
= -EINPROGRESS
;
168 job
->cb
= block_job_cancel_cb
;
170 block_job_cancel(job
);
171 while (data
.ret
== -EINPROGRESS
) {
174 return (data
.cancelled
&& data
.ret
== 0) ? -ECANCELED
: data
.ret
;
177 void block_job_sleep_ns(BlockJob
*job
, QEMUClock
*clock
, int64_t ns
)
181 /* Check cancellation *before* setting busy = false, too! */
182 if (block_job_is_cancelled(job
)) {
187 if (block_job_is_paused(job
)) {
188 qemu_coroutine_yield();
190 co_sleep_ns(clock
, ns
);
195 BlockJobInfo
*block_job_query(BlockJob
*job
)
197 BlockJobInfo
*info
= g_new0(BlockJobInfo
, 1);
198 info
->type
= g_strdup(job
->job_type
->job_type
);
199 info
->device
= g_strdup(bdrv_get_device_name(job
->bs
));
200 info
->len
= job
->len
;
201 info
->busy
= job
->busy
;
202 info
->paused
= job
->paused
;
203 info
->offset
= job
->offset
;
204 info
->speed
= job
->speed
;
205 info
->io_status
= job
->iostatus
;
209 static void block_job_iostatus_set_err(BlockJob
*job
, int error
)
211 if (job
->iostatus
== BLOCK_DEVICE_IO_STATUS_OK
) {
212 job
->iostatus
= error
== ENOSPC
? BLOCK_DEVICE_IO_STATUS_NOSPACE
:
213 BLOCK_DEVICE_IO_STATUS_FAILED
;
218 BlockErrorAction
block_job_error_action(BlockJob
*job
, BlockDriverState
*bs
,
219 BlockdevOnError on_err
,
220 int is_read
, int error
)
222 BlockErrorAction action
;
225 case BLOCKDEV_ON_ERROR_ENOSPC
:
226 action
= (error
== ENOSPC
) ? BDRV_ACTION_STOP
: BDRV_ACTION_REPORT
;
228 case BLOCKDEV_ON_ERROR_STOP
:
229 action
= BDRV_ACTION_STOP
;
231 case BLOCKDEV_ON_ERROR_REPORT
:
232 action
= BDRV_ACTION_REPORT
;
234 case BLOCKDEV_ON_ERROR_IGNORE
:
235 action
= BDRV_ACTION_IGNORE
;
240 bdrv_emit_qmp_error_event(job
->bs
, QEVENT_BLOCK_JOB_ERROR
, action
, is_read
);
241 if (action
== BDRV_ACTION_STOP
) {
242 block_job_pause(job
);
243 block_job_iostatus_set_err(job
, error
);
245 bdrv_iostatus_set_err(bs
, error
);