]> git.proxmox.com Git - mirror_qemu.git/blame - block/throttle.c
tests: Use child_of_bds instead of child_file
[mirror_qemu.git] / block / throttle.c
CommitLineData
d8e7d87e
MP
1/*
2 * QEMU block throttling filter driver infrastructure
3 *
4 * Copyright (c) 2017 Manos Pitsidianakis
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 or
9 * (at your option) version 3 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "qemu/osdep.h"
21#include "block/throttle-groups.h"
0b8fa32f 22#include "qemu/module.h"
922a01a0 23#include "qemu/option.h"
d8e7d87e
MP
24#include "qemu/throttle-options.h"
25#include "qapi/error.h"
26
27static QemuOptsList throttle_opts = {
28 .name = "throttle",
29 .head = QTAILQ_HEAD_INITIALIZER(throttle_opts.head),
30 .desc = {
31 {
32 .name = QEMU_OPT_THROTTLE_GROUP_NAME,
33 .type = QEMU_OPT_STRING,
34 .help = "Name of the throttle group",
35 },
36 { /* end of list */ }
37 },
38};
39
bc33c047
AG
40/*
41 * If this function succeeds then the throttle group name is stored in
42 * @group and must be freed by the caller.
43 * If there's an error then @group remains unmodified.
44 */
45static int throttle_parse_options(QDict *options, char **group, Error **errp)
d8e7d87e
MP
46{
47 int ret;
48 const char *group_name;
49 Error *local_err = NULL;
50 QemuOpts *opts = qemu_opts_create(&throttle_opts, NULL, 0, &error_abort);
51
52 qemu_opts_absorb_qdict(opts, options, &local_err);
53 if (local_err) {
54 error_propagate(errp, local_err);
55 ret = -EINVAL;
56 goto fin;
57 }
58
59 group_name = qemu_opt_get(opts, QEMU_OPT_THROTTLE_GROUP_NAME);
60 if (!group_name) {
61 error_setg(errp, "Please specify a throttle group");
62 ret = -EINVAL;
63 goto fin;
64 } else if (!throttle_group_exists(group_name)) {
65 error_setg(errp, "Throttle group '%s' does not exist", group_name);
66 ret = -EINVAL;
67 goto fin;
68 }
69
bc33c047 70 *group = g_strdup(group_name);
d8e7d87e
MP
71 ret = 0;
72fin:
73 qemu_opts_del(opts);
74 return ret;
75}
76
77static int throttle_open(BlockDriverState *bs, QDict *options,
78 int flags, Error **errp)
79{
80 ThrottleGroupMember *tgm = bs->opaque;
bc33c047
AG
81 char *group;
82 int ret;
d8e7d87e 83
b3af2af4
HR
84 bs->file = bdrv_open_child(NULL, options, "file", bs, &child_of_bds,
85 BDRV_CHILD_FILTERED | BDRV_CHILD_PRIMARY,
86 false, errp);
d8e7d87e
MP
87 if (!bs->file) {
88 return -EINVAL;
89 }
228345bf
HR
90 bs->supported_write_flags = bs->file->bs->supported_write_flags |
91 BDRV_REQ_WRITE_UNCHANGED;
92 bs->supported_zero_flags = bs->file->bs->supported_zero_flags |
93 BDRV_REQ_WRITE_UNCHANGED;
d8e7d87e 94
bc33c047
AG
95 ret = throttle_parse_options(options, &group, errp);
96 if (ret == 0) {
97 /* Register membership to group with name group_name */
98 throttle_group_register_tgm(tgm, group, bdrv_get_aio_context(bs));
99 g_free(group);
100 }
101
102 return ret;
d8e7d87e
MP
103}
104
105static void throttle_close(BlockDriverState *bs)
106{
107 ThrottleGroupMember *tgm = bs->opaque;
108 throttle_group_unregister_tgm(tgm);
109}
110
111
112static int64_t throttle_getlength(BlockDriverState *bs)
113{
114 return bdrv_getlength(bs->file->bs);
115}
116
117static int coroutine_fn throttle_co_preadv(BlockDriverState *bs,
118 uint64_t offset, uint64_t bytes,
119 QEMUIOVector *qiov, int flags)
120{
121
122 ThrottleGroupMember *tgm = bs->opaque;
123 throttle_group_co_io_limits_intercept(tgm, bytes, false);
124
125 return bdrv_co_preadv(bs->file, offset, bytes, qiov, flags);
126}
127
128static int coroutine_fn throttle_co_pwritev(BlockDriverState *bs,
129 uint64_t offset, uint64_t bytes,
130 QEMUIOVector *qiov, int flags)
131{
132 ThrottleGroupMember *tgm = bs->opaque;
133 throttle_group_co_io_limits_intercept(tgm, bytes, true);
134
135 return bdrv_co_pwritev(bs->file, offset, bytes, qiov, flags);
136}
137
138static int coroutine_fn throttle_co_pwrite_zeroes(BlockDriverState *bs,
139 int64_t offset, int bytes,
140 BdrvRequestFlags flags)
141{
142 ThrottleGroupMember *tgm = bs->opaque;
143 throttle_group_co_io_limits_intercept(tgm, bytes, true);
144
145 return bdrv_co_pwrite_zeroes(bs->file, offset, bytes, flags);
146}
147
148static int coroutine_fn throttle_co_pdiscard(BlockDriverState *bs,
149 int64_t offset, int bytes)
150{
151 ThrottleGroupMember *tgm = bs->opaque;
152 throttle_group_co_io_limits_intercept(tgm, bytes, true);
153
0b9fd3f4 154 return bdrv_co_pdiscard(bs->file, offset, bytes);
d8e7d87e
MP
155}
156
157static int throttle_co_flush(BlockDriverState *bs)
158{
159 return bdrv_co_flush(bs->file->bs);
160}
161
162static void throttle_detach_aio_context(BlockDriverState *bs)
163{
164 ThrottleGroupMember *tgm = bs->opaque;
165 throttle_group_detach_aio_context(tgm);
166}
167
168static void throttle_attach_aio_context(BlockDriverState *bs,
169 AioContext *new_context)
170{
171 ThrottleGroupMember *tgm = bs->opaque;
172 throttle_group_attach_aio_context(tgm, new_context);
173}
174
175static int throttle_reopen_prepare(BDRVReopenState *reopen_state,
176 BlockReopenQueue *queue, Error **errp)
177{
bc33c047
AG
178 int ret;
179 char *group = NULL;
d8e7d87e
MP
180
181 assert(reopen_state != NULL);
182 assert(reopen_state->bs != NULL);
183
bc33c047
AG
184 ret = throttle_parse_options(reopen_state->options, &group, errp);
185 reopen_state->opaque = group;
186 return ret;
d8e7d87e
MP
187}
188
189static void throttle_reopen_commit(BDRVReopenState *reopen_state)
190{
bc33c047
AG
191 BlockDriverState *bs = reopen_state->bs;
192 ThrottleGroupMember *tgm = bs->opaque;
193 char *group = reopen_state->opaque;
194
195 assert(group);
d8e7d87e 196
bc33c047
AG
197 if (strcmp(group, throttle_group_get_name(tgm))) {
198 throttle_group_unregister_tgm(tgm);
199 throttle_group_register_tgm(tgm, group, bdrv_get_aio_context(bs));
200 }
201 g_free(reopen_state->opaque);
d8e7d87e
MP
202 reopen_state->opaque = NULL;
203}
204
205static void throttle_reopen_abort(BDRVReopenState *reopen_state)
206{
bc33c047 207 g_free(reopen_state->opaque);
d8e7d87e
MP
208 reopen_state->opaque = NULL;
209}
210
b867eaa1
MP
211static void coroutine_fn throttle_co_drain_begin(BlockDriverState *bs)
212{
213 ThrottleGroupMember *tgm = bs->opaque;
214 if (atomic_fetch_inc(&tgm->io_limits_disabled) == 0) {
215 throttle_group_restart_tgm(tgm);
216 }
217}
218
219static void coroutine_fn throttle_co_drain_end(BlockDriverState *bs)
220{
221 ThrottleGroupMember *tgm = bs->opaque;
222 assert(tgm->io_limits_disabled);
223 atomic_dec(&tgm->io_limits_disabled);
224}
225
2654267c
HR
226static const char *const throttle_strong_runtime_opts[] = {
227 QEMU_OPT_THROTTLE_GROUP_NAME,
228
229 NULL
230};
231
d8e7d87e
MP
232static BlockDriver bdrv_throttle = {
233 .format_name = "throttle",
d8e7d87e
MP
234 .instance_size = sizeof(ThrottleGroupMember),
235
a7328ba5 236 .bdrv_open = throttle_open,
d8e7d87e
MP
237 .bdrv_close = throttle_close,
238 .bdrv_co_flush = throttle_co_flush,
239
240 .bdrv_child_perm = bdrv_filter_default_perms,
241
242 .bdrv_getlength = throttle_getlength,
243
244 .bdrv_co_preadv = throttle_co_preadv,
245 .bdrv_co_pwritev = throttle_co_pwritev,
246
247 .bdrv_co_pwrite_zeroes = throttle_co_pwrite_zeroes,
248 .bdrv_co_pdiscard = throttle_co_pdiscard,
249
d8e7d87e
MP
250 .bdrv_attach_aio_context = throttle_attach_aio_context,
251 .bdrv_detach_aio_context = throttle_detach_aio_context,
252
253 .bdrv_reopen_prepare = throttle_reopen_prepare,
254 .bdrv_reopen_commit = throttle_reopen_commit,
255 .bdrv_reopen_abort = throttle_reopen_abort,
3e4d0e72 256 .bdrv_co_block_status = bdrv_co_block_status_from_file,
d8e7d87e 257
b867eaa1
MP
258 .bdrv_co_drain_begin = throttle_co_drain_begin,
259 .bdrv_co_drain_end = throttle_co_drain_end,
260
d8e7d87e 261 .is_filter = true,
2654267c 262 .strong_runtime_opts = throttle_strong_runtime_opts,
d8e7d87e
MP
263};
264
265static void bdrv_throttle_init(void)
266{
267 bdrv_register(&bdrv_throttle);
268}
269
270block_init(bdrv_throttle_init);