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