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