]> git.proxmox.com Git - mirror_qemu.git/blame - block/throttle.c
block: Add bdrv_supports_compressed_writes()
[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;
d8e7d87e
MP
49 QemuOpts *opts = qemu_opts_create(&throttle_opts, NULL, 0, &error_abort);
50
af175e85 51 if (!qemu_opts_absorb_qdict(opts, options, errp)) {
d8e7d87e
MP
52 ret = -EINVAL;
53 goto fin;
54 }
55
56 group_name = qemu_opt_get(opts, QEMU_OPT_THROTTLE_GROUP_NAME);
57 if (!group_name) {
58 error_setg(errp, "Please specify a throttle group");
59 ret = -EINVAL;
60 goto fin;
61 } else if (!throttle_group_exists(group_name)) {
62 error_setg(errp, "Throttle group '%s' does not exist", group_name);
63 ret = -EINVAL;
64 goto fin;
65 }
66
bc33c047 67 *group = g_strdup(group_name);
d8e7d87e
MP
68 ret = 0;
69fin:
70 qemu_opts_del(opts);
71 return ret;
72}
73
74static int throttle_open(BlockDriverState *bs, QDict *options,
75 int flags, Error **errp)
76{
77 ThrottleGroupMember *tgm = bs->opaque;
bc33c047
AG
78 char *group;
79 int ret;
d8e7d87e 80
b3af2af4
HR
81 bs->file = bdrv_open_child(NULL, options, "file", bs, &child_of_bds,
82 BDRV_CHILD_FILTERED | BDRV_CHILD_PRIMARY,
83 false, errp);
d8e7d87e
MP
84 if (!bs->file) {
85 return -EINVAL;
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
109static int64_t throttle_getlength(BlockDriverState *bs)
110{
111 return bdrv_getlength(bs->file->bs);
112}
113
114static int coroutine_fn throttle_co_preadv(BlockDriverState *bs,
115 uint64_t offset, uint64_t bytes,
116 QEMUIOVector *qiov, int flags)
117{
118
119 ThrottleGroupMember *tgm = bs->opaque;
120 throttle_group_co_io_limits_intercept(tgm, bytes, false);
121
122 return bdrv_co_preadv(bs->file, offset, bytes, qiov, flags);
123}
124
125static int coroutine_fn throttle_co_pwritev(BlockDriverState *bs,
126 uint64_t offset, uint64_t bytes,
127 QEMUIOVector *qiov, int flags)
128{
129 ThrottleGroupMember *tgm = bs->opaque;
130 throttle_group_co_io_limits_intercept(tgm, bytes, true);
131
132 return bdrv_co_pwritev(bs->file, offset, bytes, qiov, flags);
133}
134
135static int coroutine_fn throttle_co_pwrite_zeroes(BlockDriverState *bs,
136 int64_t offset, int bytes,
137 BdrvRequestFlags flags)
138{
139 ThrottleGroupMember *tgm = bs->opaque;
140 throttle_group_co_io_limits_intercept(tgm, bytes, true);
141
142 return bdrv_co_pwrite_zeroes(bs->file, offset, bytes, flags);
143}
144
145static int coroutine_fn throttle_co_pdiscard(BlockDriverState *bs,
146 int64_t offset, int bytes)
147{
148 ThrottleGroupMember *tgm = bs->opaque;
149 throttle_group_co_io_limits_intercept(tgm, bytes, true);
150
0b9fd3f4 151 return bdrv_co_pdiscard(bs->file, offset, bytes);
d8e7d87e
MP
152}
153
154static int throttle_co_flush(BlockDriverState *bs)
155{
156 return bdrv_co_flush(bs->file->bs);
157}
158
159static void throttle_detach_aio_context(BlockDriverState *bs)
160{
161 ThrottleGroupMember *tgm = bs->opaque;
162 throttle_group_detach_aio_context(tgm);
163}
164
165static void throttle_attach_aio_context(BlockDriverState *bs,
166 AioContext *new_context)
167{
168 ThrottleGroupMember *tgm = bs->opaque;
169 throttle_group_attach_aio_context(tgm, new_context);
170}
171
172static int throttle_reopen_prepare(BDRVReopenState *reopen_state,
173 BlockReopenQueue *queue, Error **errp)
174{
bc33c047
AG
175 int ret;
176 char *group = NULL;
d8e7d87e
MP
177
178 assert(reopen_state != NULL);
179 assert(reopen_state->bs != NULL);
180
bc33c047
AG
181 ret = throttle_parse_options(reopen_state->options, &group, errp);
182 reopen_state->opaque = group;
183 return ret;
d8e7d87e
MP
184}
185
186static void throttle_reopen_commit(BDRVReopenState *reopen_state)
187{
bc33c047
AG
188 BlockDriverState *bs = reopen_state->bs;
189 ThrottleGroupMember *tgm = bs->opaque;
190 char *group = reopen_state->opaque;
191
192 assert(group);
d8e7d87e 193
bc33c047
AG
194 if (strcmp(group, throttle_group_get_name(tgm))) {
195 throttle_group_unregister_tgm(tgm);
196 throttle_group_register_tgm(tgm, group, bdrv_get_aio_context(bs));
197 }
198 g_free(reopen_state->opaque);
d8e7d87e
MP
199 reopen_state->opaque = NULL;
200}
201
202static void throttle_reopen_abort(BDRVReopenState *reopen_state)
203{
bc33c047 204 g_free(reopen_state->opaque);
d8e7d87e
MP
205 reopen_state->opaque = NULL;
206}
207
b867eaa1
MP
208static void coroutine_fn throttle_co_drain_begin(BlockDriverState *bs)
209{
210 ThrottleGroupMember *tgm = bs->opaque;
211 if (atomic_fetch_inc(&tgm->io_limits_disabled) == 0) {
212 throttle_group_restart_tgm(tgm);
213 }
214}
215
216static void coroutine_fn throttle_co_drain_end(BlockDriverState *bs)
217{
218 ThrottleGroupMember *tgm = bs->opaque;
219 assert(tgm->io_limits_disabled);
220 atomic_dec(&tgm->io_limits_disabled);
221}
222
2654267c
HR
223static const char *const throttle_strong_runtime_opts[] = {
224 QEMU_OPT_THROTTLE_GROUP_NAME,
225
226 NULL
227};
228
d8e7d87e
MP
229static BlockDriver bdrv_throttle = {
230 .format_name = "throttle",
d8e7d87e
MP
231 .instance_size = sizeof(ThrottleGroupMember),
232
a7328ba5 233 .bdrv_open = throttle_open,
d8e7d87e
MP
234 .bdrv_close = throttle_close,
235 .bdrv_co_flush = throttle_co_flush,
236
69dca43d 237 .bdrv_child_perm = bdrv_default_perms,
d8e7d87e
MP
238
239 .bdrv_getlength = throttle_getlength,
240
241 .bdrv_co_preadv = throttle_co_preadv,
242 .bdrv_co_pwritev = throttle_co_pwritev,
243
244 .bdrv_co_pwrite_zeroes = throttle_co_pwrite_zeroes,
245 .bdrv_co_pdiscard = throttle_co_pdiscard,
246
d8e7d87e
MP
247 .bdrv_attach_aio_context = throttle_attach_aio_context,
248 .bdrv_detach_aio_context = throttle_detach_aio_context,
249
250 .bdrv_reopen_prepare = throttle_reopen_prepare,
251 .bdrv_reopen_commit = throttle_reopen_commit,
252 .bdrv_reopen_abort = throttle_reopen_abort,
3e4d0e72 253 .bdrv_co_block_status = bdrv_co_block_status_from_file,
d8e7d87e 254
b867eaa1
MP
255 .bdrv_co_drain_begin = throttle_co_drain_begin,
256 .bdrv_co_drain_end = throttle_co_drain_end,
257
d8e7d87e 258 .is_filter = true,
2654267c 259 .strong_runtime_opts = throttle_strong_runtime_opts,
d8e7d87e
MP
260};
261
262static void bdrv_throttle_init(void)
263{
264 bdrv_register(&bdrv_throttle);
265}
266
267block_init(bdrv_throttle_init);