1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include "qemu/osdep.h"
26#include "block/block_int.h"
27#include "qemu/job.h"
28#include "qapi/qapi-commands-block-core.h"
29#include "qapi/qapi-visit-block-core.h"
30#include "qapi/clone-visitor.h"
31#include "qapi/error.h"
32
33typedef struct BlockdevCreateJob {
34 Job common;
35 BlockDriver *drv;
36 BlockdevCreateOptions *opts;
37} BlockdevCreateJob;
38
39static int coroutine_fn blockdev_create_run(Job *job, Error **errp)
40{
41 BlockdevCreateJob *s = container_of(job, BlockdevCreateJob, common);
42 int ret;
43
44 job_progress_set_remaining(&s->common, 1);
45 ret = s->drv->bdrv_co_create(s->opts, errp);
46 job_progress_update(&s->common, 1);
47
48 qapi_free_BlockdevCreateOptions(s->opts);
49
50 return ret;
51}
52
53static const JobDriver blockdev_create_job_driver = {
54 .instance_size = sizeof(BlockdevCreateJob),
55 .job_type = JOB_TYPE_CREATE,
56 .run = blockdev_create_run,
57};
58
59void qmp_blockdev_create(const char *job_id, BlockdevCreateOptions *options,
60 Error **errp)
61{
62 BlockdevCreateJob *s;
63 const char *fmt = BlockdevDriver_str(options->driver);
64 BlockDriver *drv = bdrv_find_format(fmt);
65
66 if (!drv) {
67 error_setg(errp, "Block driver '%s' not found or not supported", fmt);
68 return;
69 }
70
71
72
73 if (bdrv_uses_whitelist() && !bdrv_is_whitelisted(drv, false)) {
74 error_setg(errp, "Driver is not whitelisted");
75 return;
76 }
77
78
79 if (!drv->bdrv_co_create) {
80 error_setg(errp, "Driver does not support blockdev-create");
81 return;
82 }
83
84
85
86
87 s = job_create(job_id, &blockdev_create_job_driver, NULL,
88 qemu_get_aio_context(), JOB_DEFAULT | JOB_MANUAL_DISMISS,
89 NULL, NULL, errp);
90 if (!s) {
91 return;
92 }
93
94 s->drv = drv,
95 s->opts = QAPI_CLONE(BlockdevCreateOptions, options),
96
97 job_start(&s->common);
98}
99