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" |
22 | #include "qemu/module.h" |
23 | #include "qemu/option.h" |
24 | #include "qemu/throttle-options.h" |
25 | #include "qapi/error.h" |
26 | |
27 | static 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 | |
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 | */ |
45 | static int throttle_parse_options(QDict *options, char **group, Error **errp) |
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 | |
70 | *group = g_strdup(group_name); |
71 | ret = 0; |
72 | fin: |
73 | qemu_opts_del(opts); |
74 | return ret; |
75 | } |
76 | |
77 | static int throttle_open(BlockDriverState *bs, QDict *options, |
78 | int flags, Error **errp) |
79 | { |
80 | ThrottleGroupMember *tgm = bs->opaque; |
81 | char *group; |
82 | int ret; |
83 | |
84 | bs->file = bdrv_open_child(NULL, options, "file" , bs, |
85 | &child_file, false, errp); |
86 | if (!bs->file) { |
87 | return -EINVAL; |
88 | } |
89 | bs->supported_write_flags = bs->file->bs->supported_write_flags | |
90 | BDRV_REQ_WRITE_UNCHANGED; |
91 | bs->supported_zero_flags = bs->file->bs->supported_zero_flags | |
92 | BDRV_REQ_WRITE_UNCHANGED; |
93 | |
94 | ret = throttle_parse_options(options, &group, errp); |
95 | if (ret == 0) { |
96 | /* Register membership to group with name group_name */ |
97 | throttle_group_register_tgm(tgm, group, bdrv_get_aio_context(bs)); |
98 | g_free(group); |
99 | } |
100 | |
101 | return ret; |
102 | } |
103 | |
104 | static void throttle_close(BlockDriverState *bs) |
105 | { |
106 | ThrottleGroupMember *tgm = bs->opaque; |
107 | throttle_group_unregister_tgm(tgm); |
108 | } |
109 | |
110 | |
111 | static int64_t throttle_getlength(BlockDriverState *bs) |
112 | { |
113 | return bdrv_getlength(bs->file->bs); |
114 | } |
115 | |
116 | static int coroutine_fn throttle_co_preadv(BlockDriverState *bs, |
117 | uint64_t offset, uint64_t bytes, |
118 | QEMUIOVector *qiov, int flags) |
119 | { |
120 | |
121 | ThrottleGroupMember *tgm = bs->opaque; |
122 | throttle_group_co_io_limits_intercept(tgm, bytes, false); |
123 | |
124 | return bdrv_co_preadv(bs->file, offset, bytes, qiov, flags); |
125 | } |
126 | |
127 | static int coroutine_fn throttle_co_pwritev(BlockDriverState *bs, |
128 | uint64_t offset, uint64_t bytes, |
129 | QEMUIOVector *qiov, int flags) |
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 | |
137 | static int coroutine_fn throttle_co_pwrite_zeroes(BlockDriverState *bs, |
138 | int64_t offset, int bytes, |
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 | |
147 | static int coroutine_fn throttle_co_pdiscard(BlockDriverState *bs, |
148 | int64_t offset, int bytes) |
149 | { |
150 | ThrottleGroupMember *tgm = bs->opaque; |
151 | throttle_group_co_io_limits_intercept(tgm, bytes, true); |
152 | |
153 | return bdrv_co_pdiscard(bs->file, offset, bytes); |
154 | } |
155 | |
156 | static int throttle_co_flush(BlockDriverState *bs) |
157 | { |
158 | return bdrv_co_flush(bs->file->bs); |
159 | } |
160 | |
161 | static void throttle_detach_aio_context(BlockDriverState *bs) |
162 | { |
163 | ThrottleGroupMember *tgm = bs->opaque; |
164 | throttle_group_detach_aio_context(tgm); |
165 | } |
166 | |
167 | static void throttle_attach_aio_context(BlockDriverState *bs, |
168 | AioContext *new_context) |
169 | { |
170 | ThrottleGroupMember *tgm = bs->opaque; |
171 | throttle_group_attach_aio_context(tgm, new_context); |
172 | } |
173 | |
174 | static int throttle_reopen_prepare(BDRVReopenState *reopen_state, |
175 | BlockReopenQueue *queue, Error **errp) |
176 | { |
177 | int ret; |
178 | char *group = NULL; |
179 | |
180 | assert(reopen_state != NULL); |
181 | assert(reopen_state->bs != NULL); |
182 | |
183 | ret = throttle_parse_options(reopen_state->options, &group, errp); |
184 | reopen_state->opaque = group; |
185 | return ret; |
186 | } |
187 | |
188 | static void throttle_reopen_commit(BDRVReopenState *reopen_state) |
189 | { |
190 | BlockDriverState *bs = reopen_state->bs; |
191 | ThrottleGroupMember *tgm = bs->opaque; |
192 | char *group = reopen_state->opaque; |
193 | |
194 | assert(group); |
195 | |
196 | if (strcmp(group, throttle_group_get_name(tgm))) { |
197 | throttle_group_unregister_tgm(tgm); |
198 | throttle_group_register_tgm(tgm, group, bdrv_get_aio_context(bs)); |
199 | } |
200 | g_free(reopen_state->opaque); |
201 | reopen_state->opaque = NULL; |
202 | } |
203 | |
204 | static void throttle_reopen_abort(BDRVReopenState *reopen_state) |
205 | { |
206 | g_free(reopen_state->opaque); |
207 | reopen_state->opaque = NULL; |
208 | } |
209 | |
210 | static bool throttle_recurse_is_first_non_filter(BlockDriverState *bs, |
211 | BlockDriverState *candidate) |
212 | { |
213 | return bdrv_recurse_is_first_non_filter(bs->file->bs, candidate); |
214 | } |
215 | |
216 | static void coroutine_fn throttle_co_drain_begin(BlockDriverState *bs) |
217 | { |
218 | ThrottleGroupMember *tgm = bs->opaque; |
219 | if (atomic_fetch_inc(&tgm->io_limits_disabled) == 0) { |
220 | throttle_group_restart_tgm(tgm); |
221 | } |
222 | } |
223 | |
224 | static void coroutine_fn throttle_co_drain_end(BlockDriverState *bs) |
225 | { |
226 | ThrottleGroupMember *tgm = bs->opaque; |
227 | assert(tgm->io_limits_disabled); |
228 | atomic_dec(&tgm->io_limits_disabled); |
229 | } |
230 | |
231 | static const char *const throttle_strong_runtime_opts[] = { |
232 | QEMU_OPT_THROTTLE_GROUP_NAME, |
233 | |
234 | NULL |
235 | }; |
236 | |
237 | static BlockDriver bdrv_throttle = { |
238 | .format_name = "throttle" , |
239 | .instance_size = sizeof(ThrottleGroupMember), |
240 | |
241 | .bdrv_open = throttle_open, |
242 | .bdrv_close = throttle_close, |
243 | .bdrv_co_flush = throttle_co_flush, |
244 | |
245 | .bdrv_child_perm = bdrv_filter_default_perms, |
246 | |
247 | .bdrv_getlength = throttle_getlength, |
248 | |
249 | .bdrv_co_preadv = throttle_co_preadv, |
250 | .bdrv_co_pwritev = throttle_co_pwritev, |
251 | |
252 | .bdrv_co_pwrite_zeroes = throttle_co_pwrite_zeroes, |
253 | .bdrv_co_pdiscard = throttle_co_pdiscard, |
254 | |
255 | .bdrv_recurse_is_first_non_filter = throttle_recurse_is_first_non_filter, |
256 | |
257 | .bdrv_attach_aio_context = throttle_attach_aio_context, |
258 | .bdrv_detach_aio_context = throttle_detach_aio_context, |
259 | |
260 | .bdrv_reopen_prepare = throttle_reopen_prepare, |
261 | .bdrv_reopen_commit = throttle_reopen_commit, |
262 | .bdrv_reopen_abort = throttle_reopen_abort, |
263 | .bdrv_co_block_status = bdrv_co_block_status_from_file, |
264 | |
265 | .bdrv_co_drain_begin = throttle_co_drain_begin, |
266 | .bdrv_co_drain_end = throttle_co_drain_end, |
267 | |
268 | .is_filter = true, |
269 | .strong_runtime_opts = throttle_strong_runtime_opts, |
270 | }; |
271 | |
272 | static void bdrv_throttle_init(void) |
273 | { |
274 | bdrv_register(&bdrv_throttle); |
275 | } |
276 | |
277 | block_init(bdrv_throttle_init); |
278 | |