xref: /openbmc/qemu/blockjob.c (revision 4f4f6976)
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"
29 #include "block/block.h"
30 #include "block/blockjob.h"
31 #include "block/block_int.h"
32 #include "qapi/qmp/qerror.h"
33 #include "qapi/qmp/qjson.h"
34 #include "block/coroutine.h"
35 #include "qmp-commands.h"
36 #include "qemu/timer.h"
37 #include "qapi-event.h"
38 
39 void *block_job_create(const BlockJobDriver *driver, BlockDriverState *bs,
40                        int64_t speed, BlockCompletionFunc *cb,
41                        void *opaque, Error **errp)
42 {
43     BlockJob *job;
44 
45     if (bs->job) {
46         error_setg(errp, QERR_DEVICE_IN_USE, bdrv_get_device_name(bs));
47         return NULL;
48     }
49     bdrv_ref(bs);
50     job = g_malloc0(driver->instance_size);
51     error_setg(&job->blocker, "block device is in use by block job: %s",
52                BlockJobType_lookup[driver->job_type]);
53     bdrv_op_block_all(bs, job->blocker);
54     bdrv_op_unblock(bs, BLOCK_OP_TYPE_DATAPLANE, job->blocker);
55 
56     job->driver        = driver;
57     job->bs            = bs;
58     job->cb            = cb;
59     job->opaque        = opaque;
60     job->busy          = true;
61     bs->job = job;
62 
63     /* Only set speed when necessary to avoid NotSupported error */
64     if (speed != 0) {
65         Error *local_err = NULL;
66 
67         block_job_set_speed(job, speed, &local_err);
68         if (local_err) {
69             block_job_release(bs);
70             error_propagate(errp, local_err);
71             return NULL;
72         }
73     }
74     return job;
75 }
76 
77 void block_job_release(BlockDriverState *bs)
78 {
79     BlockJob *job = bs->job;
80 
81     bs->job = NULL;
82     bdrv_op_unblock_all(bs, job->blocker);
83     error_free(job->blocker);
84     g_free(job);
85 }
86 
87 void block_job_completed(BlockJob *job, int ret)
88 {
89     BlockDriverState *bs = job->bs;
90 
91     assert(bs->job == job);
92     job->cb(job->opaque, ret);
93     block_job_release(bs);
94 }
95 
96 void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp)
97 {
98     Error *local_err = NULL;
99 
100     if (!job->driver->set_speed) {
101         error_setg(errp, QERR_UNSUPPORTED);
102         return;
103     }
104     job->driver->set_speed(job, speed, &local_err);
105     if (local_err) {
106         error_propagate(errp, local_err);
107         return;
108     }
109 
110     job->speed = speed;
111 }
112 
113 void block_job_complete(BlockJob *job, Error **errp)
114 {
115     if (job->pause_count || job->cancelled || !job->driver->complete) {
116         error_setg(errp, QERR_BLOCK_JOB_NOT_READY,
117                    bdrv_get_device_name(job->bs));
118         return;
119     }
120 
121     job->driver->complete(job, errp);
122 }
123 
124 void block_job_pause(BlockJob *job)
125 {
126     job->pause_count++;
127 }
128 
129 bool block_job_is_paused(BlockJob *job)
130 {
131     return job->pause_count > 0;
132 }
133 
134 void block_job_resume(BlockJob *job)
135 {
136     assert(job->pause_count > 0);
137     job->pause_count--;
138     if (job->pause_count) {
139         return;
140     }
141     block_job_enter(job);
142 }
143 
144 void block_job_enter(BlockJob *job)
145 {
146     block_job_iostatus_reset(job);
147     if (job->co && !job->busy) {
148         qemu_coroutine_enter(job->co, NULL);
149     }
150 }
151 
152 void block_job_cancel(BlockJob *job)
153 {
154     job->cancelled = true;
155     block_job_enter(job);
156 }
157 
158 bool block_job_is_cancelled(BlockJob *job)
159 {
160     return job->cancelled;
161 }
162 
163 void block_job_iostatus_reset(BlockJob *job)
164 {
165     job->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
166     if (job->driver->iostatus_reset) {
167         job->driver->iostatus_reset(job);
168     }
169 }
170 
171 struct BlockFinishData {
172     BlockJob *job;
173     BlockCompletionFunc *cb;
174     void *opaque;
175     bool cancelled;
176     int ret;
177 };
178 
179 static void block_job_finish_cb(void *opaque, int ret)
180 {
181     struct BlockFinishData *data = opaque;
182 
183     data->cancelled = block_job_is_cancelled(data->job);
184     data->ret = ret;
185     data->cb(data->opaque, ret);
186 }
187 
188 static int block_job_finish_sync(BlockJob *job,
189                                  void (*finish)(BlockJob *, Error **errp),
190                                  Error **errp)
191 {
192     struct BlockFinishData data;
193     BlockDriverState *bs = job->bs;
194     Error *local_err = NULL;
195 
196     assert(bs->job == job);
197 
198     /* Set up our own callback to store the result and chain to
199      * the original callback.
200      */
201     data.job = job;
202     data.cb = job->cb;
203     data.opaque = job->opaque;
204     data.ret = -EINPROGRESS;
205     job->cb = block_job_finish_cb;
206     job->opaque = &data;
207     finish(job, &local_err);
208     if (local_err) {
209         error_propagate(errp, local_err);
210         return -EBUSY;
211     }
212     while (data.ret == -EINPROGRESS) {
213         aio_poll(bdrv_get_aio_context(bs), true);
214     }
215     return (data.cancelled && data.ret == 0) ? -ECANCELED : data.ret;
216 }
217 
218 /* A wrapper around block_job_cancel() taking an Error ** parameter so it may be
219  * used with block_job_finish_sync() without the need for (rather nasty)
220  * function pointer casts there. */
221 static void block_job_cancel_err(BlockJob *job, Error **errp)
222 {
223     block_job_cancel(job);
224 }
225 
226 int block_job_cancel_sync(BlockJob *job)
227 {
228     return block_job_finish_sync(job, &block_job_cancel_err, NULL);
229 }
230 
231 int block_job_complete_sync(BlockJob *job, Error **errp)
232 {
233     return block_job_finish_sync(job, &block_job_complete, errp);
234 }
235 
236 void block_job_sleep_ns(BlockJob *job, QEMUClockType type, int64_t ns)
237 {
238     assert(job->busy);
239 
240     /* Check cancellation *before* setting busy = false, too!  */
241     if (block_job_is_cancelled(job)) {
242         return;
243     }
244 
245     job->busy = false;
246     if (block_job_is_paused(job)) {
247         qemu_coroutine_yield();
248     } else {
249         co_aio_sleep_ns(bdrv_get_aio_context(job->bs), type, ns);
250     }
251     job->busy = true;
252 }
253 
254 void block_job_yield(BlockJob *job)
255 {
256     assert(job->busy);
257 
258     /* Check cancellation *before* setting busy = false, too!  */
259     if (block_job_is_cancelled(job)) {
260         return;
261     }
262 
263     job->busy = false;
264     qemu_coroutine_yield();
265     job->busy = true;
266 }
267 
268 BlockJobInfo *block_job_query(BlockJob *job)
269 {
270     BlockJobInfo *info = g_new0(BlockJobInfo, 1);
271     info->type      = g_strdup(BlockJobType_lookup[job->driver->job_type]);
272     info->device    = g_strdup(bdrv_get_device_name(job->bs));
273     info->len       = job->len;
274     info->busy      = job->busy;
275     info->paused    = job->pause_count > 0;
276     info->offset    = job->offset;
277     info->speed     = job->speed;
278     info->io_status = job->iostatus;
279     info->ready     = job->ready;
280     return info;
281 }
282 
283 static void block_job_iostatus_set_err(BlockJob *job, int error)
284 {
285     if (job->iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
286         job->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE :
287                                           BLOCK_DEVICE_IO_STATUS_FAILED;
288     }
289 }
290 
291 void block_job_event_cancelled(BlockJob *job)
292 {
293     qapi_event_send_block_job_cancelled(job->driver->job_type,
294                                         bdrv_get_device_name(job->bs),
295                                         job->len,
296                                         job->offset,
297                                         job->speed,
298                                         &error_abort);
299 }
300 
301 void block_job_event_completed(BlockJob *job, const char *msg)
302 {
303     qapi_event_send_block_job_completed(job->driver->job_type,
304                                         bdrv_get_device_name(job->bs),
305                                         job->len,
306                                         job->offset,
307                                         job->speed,
308                                         !!msg,
309                                         msg,
310                                         &error_abort);
311 }
312 
313 void block_job_event_ready(BlockJob *job)
314 {
315     job->ready = true;
316 
317     qapi_event_send_block_job_ready(job->driver->job_type,
318                                     bdrv_get_device_name(job->bs),
319                                     job->len,
320                                     job->offset,
321                                     job->speed, &error_abort);
322 }
323 
324 BlockErrorAction block_job_error_action(BlockJob *job, BlockDriverState *bs,
325                                         BlockdevOnError on_err,
326                                         int is_read, int error)
327 {
328     BlockErrorAction action;
329 
330     switch (on_err) {
331     case BLOCKDEV_ON_ERROR_ENOSPC:
332         action = (error == ENOSPC) ?
333                  BLOCK_ERROR_ACTION_STOP : BLOCK_ERROR_ACTION_REPORT;
334         break;
335     case BLOCKDEV_ON_ERROR_STOP:
336         action = BLOCK_ERROR_ACTION_STOP;
337         break;
338     case BLOCKDEV_ON_ERROR_REPORT:
339         action = BLOCK_ERROR_ACTION_REPORT;
340         break;
341     case BLOCKDEV_ON_ERROR_IGNORE:
342         action = BLOCK_ERROR_ACTION_IGNORE;
343         break;
344     default:
345         abort();
346     }
347     qapi_event_send_block_job_error(bdrv_get_device_name(job->bs),
348                                     is_read ? IO_OPERATION_TYPE_READ :
349                                     IO_OPERATION_TYPE_WRITE,
350                                     action, &error_abort);
351     if (action == BLOCK_ERROR_ACTION_STOP) {
352         /* make the pause user visible, which will be resumed from QMP. */
353         job->user_paused = true;
354         block_job_pause(job);
355         block_job_iostatus_set_err(job, error);
356         if (bs != job->bs) {
357             bdrv_iostatus_set_err(bs, error);
358         }
359     }
360     return action;
361 }
362 
363 typedef struct {
364     BlockJob *job;
365     QEMUBH *bh;
366     AioContext *aio_context;
367     BlockJobDeferToMainLoopFn *fn;
368     void *opaque;
369 } BlockJobDeferToMainLoopData;
370 
371 static void block_job_defer_to_main_loop_bh(void *opaque)
372 {
373     BlockJobDeferToMainLoopData *data = opaque;
374     AioContext *aio_context;
375 
376     qemu_bh_delete(data->bh);
377 
378     /* Prevent race with block_job_defer_to_main_loop() */
379     aio_context_acquire(data->aio_context);
380 
381     /* Fetch BDS AioContext again, in case it has changed */
382     aio_context = bdrv_get_aio_context(data->job->bs);
383     aio_context_acquire(aio_context);
384 
385     data->fn(data->job, data->opaque);
386 
387     aio_context_release(aio_context);
388 
389     aio_context_release(data->aio_context);
390 
391     g_free(data);
392 }
393 
394 void block_job_defer_to_main_loop(BlockJob *job,
395                                   BlockJobDeferToMainLoopFn *fn,
396                                   void *opaque)
397 {
398     BlockJobDeferToMainLoopData *data = g_malloc(sizeof(*data));
399     data->job = job;
400     data->bh = qemu_bh_new(block_job_defer_to_main_loop_bh, data);
401     data->aio_context = bdrv_get_aio_context(job->bs);
402     data->fn = fn;
403     data->opaque = opaque;
404 
405     qemu_bh_schedule(data->bh);
406 }
407