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
26
27
28#include "qemu/osdep.h"
29#include "block/block_int.h"
30#include "qemu/job.h"
31#include "qemu/main-loop.h"
32#include "qapi/qapi-commands-block-core.h"
33#include "qapi/qapi-visit-block-core.h"
34#include "qapi/clone-visitor.h"
35#include "qapi/error.h"
36
37typedef struct BlockdevAmendJob {
38 Job common;
39 BlockdevAmendOptions *opts;
40 BlockDriverState *bs;
41 bool force;
42} BlockdevAmendJob;
43
44static int coroutine_fn blockdev_amend_run(Job *job, Error **errp)
45{
46 BlockdevAmendJob *s = container_of(job, BlockdevAmendJob, common);
47 int ret;
48
49 job_progress_set_remaining(&s->common, 1);
50 ret = s->bs->drv->bdrv_co_amend(s->bs, s->opts, s->force, errp);
51 job_progress_update(&s->common, 1);
52 qapi_free_BlockdevAmendOptions(s->opts);
53 return ret;
54}
55
56static const JobDriver blockdev_amend_job_driver = {
57 .instance_size = sizeof(BlockdevAmendJob),
58 .job_type = JOB_TYPE_AMEND,
59 .run = blockdev_amend_run,
60};
61
62void qmp_x_blockdev_amend(const char *job_id,
63 const char *node_name,
64 BlockdevAmendOptions *options,
65 bool has_force,
66 bool force,
67 Error **errp)
68{
69 BlockdevAmendJob *s;
70 const char *fmt = BlockdevDriver_str(options->driver);
71 BlockDriver *drv = bdrv_find_format(fmt);
72 BlockDriverState *bs;
73
74 bs = bdrv_lookup_bs(NULL, node_name, errp);
75 if (!bs) {
76 return;
77 }
78
79 if (!drv) {
80 error_setg(errp, "Block driver '%s' not found or not supported", fmt);
81 return;
82 }
83
84
85
86
87
88 if (bdrv_uses_whitelist() && !bdrv_is_whitelisted(drv, false)) {
89 error_setg(errp, "Driver is not whitelisted");
90 return;
91 }
92
93 if (bs->drv != drv) {
94 error_setg(errp,
95 "x-blockdev-amend doesn't support changing the block driver");
96 return;
97 }
98
99
100 if (!drv->bdrv_co_amend) {
101 error_setg(errp, "Driver does not support x-blockdev-amend");
102 return;
103 }
104
105
106 s = job_create(job_id, &blockdev_amend_job_driver, NULL,
107 bdrv_get_aio_context(bs), JOB_DEFAULT | JOB_MANUAL_DISMISS,
108 NULL, NULL, errp);
109 if (!s) {
110 return;
111 }
112
113 s->bs = bs,
114 s->opts = QAPI_CLONE(BlockdevAmendOptions, options),
115 s->force = has_force ? force : false;
116 job_start(&s->common);
117}
118