]> git.proxmox.com Git - mirror_qemu.git/blame - block/throttle.c
block: Mark bdrv_co_pdiscard() and callers GRAPH_RDLOCK
[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"
e2c1c34f
MA
21#include "block/block-io.h"
22#include "block/block_int.h"
d8e7d87e 23#include "block/throttle-groups.h"
0b8fa32f 24#include "qemu/module.h"
922a01a0 25#include "qemu/option.h"
d8e7d87e
MP
26#include "qemu/throttle-options.h"
27#include "qapi/error.h"
28
29static QemuOptsList throttle_opts = {
30 .name = "throttle",
31 .head = QTAILQ_HEAD_INITIALIZER(throttle_opts.head),
32 .desc = {
33 {
34 .name = QEMU_OPT_THROTTLE_GROUP_NAME,
35 .type = QEMU_OPT_STRING,
36 .help = "Name of the throttle group",
37 },
38 { /* end of list */ }
39 },
40};
41
bc33c047
AG
42/*
43 * If this function succeeds then the throttle group name is stored in
44 * @group and must be freed by the caller.
45 * If there's an error then @group remains unmodified.
46 */
47static int throttle_parse_options(QDict *options, char **group, Error **errp)
d8e7d87e
MP
48{
49 int ret;
50 const char *group_name;
d8e7d87e
MP
51 QemuOpts *opts = qemu_opts_create(&throttle_opts, NULL, 0, &error_abort);
52
af175e85 53 if (!qemu_opts_absorb_qdict(opts, options, errp)) {
d8e7d87e
MP
54 ret = -EINVAL;
55 goto fin;
56 }
57
58 group_name = qemu_opt_get(opts, QEMU_OPT_THROTTLE_GROUP_NAME);
59 if (!group_name) {
60 error_setg(errp, "Please specify a throttle group");
61 ret = -EINVAL;
62 goto fin;
63 } else if (!throttle_group_exists(group_name)) {
64 error_setg(errp, "Throttle group '%s' does not exist", group_name);
65 ret = -EINVAL;
66 goto fin;
67 }
68
bc33c047 69 *group = g_strdup(group_name);
d8e7d87e
MP
70 ret = 0;
71fin:
72 qemu_opts_del(opts);
73 return ret;
74}
75
76static int throttle_open(BlockDriverState *bs, QDict *options,
77 int flags, Error **errp)
78{
79 ThrottleGroupMember *tgm = bs->opaque;
bc33c047
AG
80 char *group;
81 int ret;
d8e7d87e 82
83930780
VSO
83 ret = bdrv_open_file_child(NULL, options, "file", bs, errp);
84 if (ret < 0) {
85 return ret;
d8e7d87e 86 }
228345bf
HR
87 bs->supported_write_flags = bs->file->bs->supported_write_flags |
88 BDRV_REQ_WRITE_UNCHANGED;
89 bs->supported_zero_flags = bs->file->bs->supported_zero_flags |
90 BDRV_REQ_WRITE_UNCHANGED;
d8e7d87e 91
bc33c047
AG
92 ret = throttle_parse_options(options, &group, errp);
93 if (ret == 0) {
94 /* Register membership to group with name group_name */
95 throttle_group_register_tgm(tgm, group, bdrv_get_aio_context(bs));
96 g_free(group);
97 }
98
99 return ret;
d8e7d87e
MP
100}
101
102static void throttle_close(BlockDriverState *bs)
103{
104 ThrottleGroupMember *tgm = bs->opaque;
105 throttle_group_unregister_tgm(tgm);
106}
107
108
c86422c5 109static int64_t coroutine_fn throttle_co_getlength(BlockDriverState *bs)
d8e7d87e 110{
c86422c5 111 return bdrv_co_getlength(bs->file->bs);
d8e7d87e
MP
112}
113
114static int coroutine_fn throttle_co_preadv(BlockDriverState *bs,
f7ef38dd
VSO
115 int64_t offset, int64_t bytes,
116 QEMUIOVector *qiov,
117 BdrvRequestFlags flags)
d8e7d87e
MP
118{
119
120 ThrottleGroupMember *tgm = bs->opaque;
121 throttle_group_co_io_limits_intercept(tgm, bytes, false);
122
123 return bdrv_co_preadv(bs->file, offset, bytes, qiov, flags);
124}
125
126static int coroutine_fn throttle_co_pwritev(BlockDriverState *bs,
e75abeda
VSO
127 int64_t offset, int64_t bytes,
128 QEMUIOVector *qiov,
129 BdrvRequestFlags flags)
d8e7d87e
MP
130{
131 ThrottleGroupMember *tgm = bs->opaque;
132 throttle_group_co_io_limits_intercept(tgm, bytes, true);
133
134 return bdrv_co_pwritev(bs->file, offset, bytes, qiov, flags);
135}
136
137static int coroutine_fn throttle_co_pwrite_zeroes(BlockDriverState *bs,
f34b2bcf 138 int64_t offset, int64_t bytes,
d8e7d87e
MP
139 BdrvRequestFlags flags)
140{
141 ThrottleGroupMember *tgm = bs->opaque;
142 throttle_group_co_io_limits_intercept(tgm, bytes, true);
143
144 return bdrv_co_pwrite_zeroes(bs->file, offset, bytes, flags);
145}
146
9a5a1c62
EGE
147static int coroutine_fn GRAPH_RDLOCK
148throttle_co_pdiscard(BlockDriverState *bs, int64_t offset, int64_t bytes)
d8e7d87e
MP
149{
150 ThrottleGroupMember *tgm = bs->opaque;
151 throttle_group_co_io_limits_intercept(tgm, bytes, true);
152
0b9fd3f4 153 return bdrv_co_pdiscard(bs->file, offset, bytes);
d8e7d87e
MP
154}
155
e7e754ae 156static int coroutine_fn throttle_co_pwritev_compressed(BlockDriverState *bs,
e75abeda
VSO
157 int64_t offset,
158 int64_t bytes,
e7e754ae
HR
159 QEMUIOVector *qiov)
160{
161 return throttle_co_pwritev(bs, offset, bytes, qiov,
162 BDRV_REQ_WRITE_COMPRESSED);
163}
164
88095349 165static int coroutine_fn GRAPH_RDLOCK throttle_co_flush(BlockDriverState *bs)
d8e7d87e
MP
166{
167 return bdrv_co_flush(bs->file->bs);
168}
169
170static void throttle_detach_aio_context(BlockDriverState *bs)
171{
172 ThrottleGroupMember *tgm = bs->opaque;
173 throttle_group_detach_aio_context(tgm);
174}
175
176static void throttle_attach_aio_context(BlockDriverState *bs,
177 AioContext *new_context)
178{
179 ThrottleGroupMember *tgm = bs->opaque;
180 throttle_group_attach_aio_context(tgm, new_context);
181}
182
183static int throttle_reopen_prepare(BDRVReopenState *reopen_state,
184 BlockReopenQueue *queue, Error **errp)
185{
bc33c047
AG
186 int ret;
187 char *group = NULL;
d8e7d87e
MP
188
189 assert(reopen_state != NULL);
190 assert(reopen_state->bs != NULL);
191
bc33c047
AG
192 ret = throttle_parse_options(reopen_state->options, &group, errp);
193 reopen_state->opaque = group;
194 return ret;
d8e7d87e
MP
195}
196
197static void throttle_reopen_commit(BDRVReopenState *reopen_state)
198{
bc33c047
AG
199 BlockDriverState *bs = reopen_state->bs;
200 ThrottleGroupMember *tgm = bs->opaque;
201 char *group = reopen_state->opaque;
202
203 assert(group);
d8e7d87e 204
bc33c047
AG
205 if (strcmp(group, throttle_group_get_name(tgm))) {
206 throttle_group_unregister_tgm(tgm);
207 throttle_group_register_tgm(tgm, group, bdrv_get_aio_context(bs));
208 }
209 g_free(reopen_state->opaque);
d8e7d87e
MP
210 reopen_state->opaque = NULL;
211}
212
213static void throttle_reopen_abort(BDRVReopenState *reopen_state)
214{
bc33c047 215 g_free(reopen_state->opaque);
d8e7d87e
MP
216 reopen_state->opaque = NULL;
217}
218
5e8ac217 219static void throttle_drain_begin(BlockDriverState *bs)
b867eaa1
MP
220{
221 ThrottleGroupMember *tgm = bs->opaque;
d73415a3 222 if (qatomic_fetch_inc(&tgm->io_limits_disabled) == 0) {
b867eaa1
MP
223 throttle_group_restart_tgm(tgm);
224 }
225}
226
5e8ac217 227static void throttle_drain_end(BlockDriverState *bs)
b867eaa1
MP
228{
229 ThrottleGroupMember *tgm = bs->opaque;
230 assert(tgm->io_limits_disabled);
d73415a3 231 qatomic_dec(&tgm->io_limits_disabled);
b867eaa1
MP
232}
233
2654267c
HR
234static const char *const throttle_strong_runtime_opts[] = {
235 QEMU_OPT_THROTTLE_GROUP_NAME,
236
237 NULL
238};
239
d8e7d87e
MP
240static BlockDriver bdrv_throttle = {
241 .format_name = "throttle",
d8e7d87e
MP
242 .instance_size = sizeof(ThrottleGroupMember),
243
a7328ba5 244 .bdrv_open = throttle_open,
d8e7d87e
MP
245 .bdrv_close = throttle_close,
246 .bdrv_co_flush = throttle_co_flush,
247
69dca43d 248 .bdrv_child_perm = bdrv_default_perms,
d8e7d87e 249
c86422c5 250 .bdrv_co_getlength = throttle_co_getlength,
d8e7d87e
MP
251
252 .bdrv_co_preadv = throttle_co_preadv,
253 .bdrv_co_pwritev = throttle_co_pwritev,
254
255 .bdrv_co_pwrite_zeroes = throttle_co_pwrite_zeroes,
256 .bdrv_co_pdiscard = throttle_co_pdiscard,
e7e754ae 257 .bdrv_co_pwritev_compressed = throttle_co_pwritev_compressed,
d8e7d87e 258
d8e7d87e
MP
259 .bdrv_attach_aio_context = throttle_attach_aio_context,
260 .bdrv_detach_aio_context = throttle_detach_aio_context,
261
262 .bdrv_reopen_prepare = throttle_reopen_prepare,
263 .bdrv_reopen_commit = throttle_reopen_commit,
264 .bdrv_reopen_abort = throttle_reopen_abort,
d8e7d87e 265
5e8ac217
KW
266 .bdrv_drain_begin = throttle_drain_begin,
267 .bdrv_drain_end = throttle_drain_end,
b867eaa1 268
d8e7d87e 269 .is_filter = true,
2654267c 270 .strong_runtime_opts = throttle_strong_runtime_opts,
d8e7d87e
MP
271};
272
273static void bdrv_throttle_init(void)
274{
275 bdrv_register(&bdrv_throttle);
276}
277
278block_init(bdrv_throttle_init);