xref: /openbmc/qemu/block/create.c (revision 0dacec87)
1 /*
2  * Block layer code related to image creation
3  *
4  * Copyright (c) 2018 Kevin Wolf <kwolf@redhat.com>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
25 #include "qemu/osdep.h"
26 #include "block/block_int.h"
27 #include "qapi/qapi-commands-block-core.h"
28 #include "qapi/error.h"
29 
30 typedef struct BlockdevCreateCo {
31     BlockDriver *drv;
32     BlockdevCreateOptions *opts;
33     int ret;
34     Error **errp;
35 } BlockdevCreateCo;
36 
37 static void coroutine_fn bdrv_co_create_co_entry(void *opaque)
38 {
39     BlockdevCreateCo *cco = opaque;
40     cco->ret = cco->drv->bdrv_co_create(cco->opts, cco->errp);
41 }
42 
43 void qmp_x_blockdev_create(BlockdevCreateOptions *options, Error **errp)
44 {
45     const char *fmt = BlockdevDriver_str(options->driver);
46     BlockDriver *drv = bdrv_find_format(fmt);
47     Coroutine *co;
48     BlockdevCreateCo cco;
49 
50     /* If the driver is in the schema, we know that it exists. But it may not
51      * be whitelisted. */
52     assert(drv);
53     if (bdrv_uses_whitelist() && !bdrv_is_whitelisted(drv, false)) {
54         error_setg(errp, "Driver is not whitelisted");
55         return;
56     }
57 
58     /* Call callback if it exists */
59     if (!drv->bdrv_co_create) {
60         error_setg(errp, "Driver does not support blockdev-create");
61         return;
62     }
63 
64     cco = (BlockdevCreateCo) {
65         .drv = drv,
66         .opts = options,
67         .ret = -EINPROGRESS,
68         .errp = errp,
69     };
70 
71     co = qemu_coroutine_create(bdrv_co_create_co_entry, &cco);
72     qemu_coroutine_enter(co);
73     while (cco.ret == -EINPROGRESS) {
74         aio_poll(qemu_get_aio_context(), true);
75     }
76 }
77