]>
Commit | Line | Data |
---|---|---|
6a143727 KW |
1 | /* |
2 | * Block protocol for I/O error injection | |
3 | * | |
63188c24 | 4 | * Copyright (C) 2016-2017 Red Hat, Inc. |
6a143727 KW |
5 | * Copyright (c) 2010 Kevin Wolf <[email protected]> |
6 | * | |
7 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
8 | * of this software and associated documentation files (the "Software"), to deal | |
9 | * in the Software without restriction, including without limitation the rights | |
10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
11 | * copies of the Software, and to permit persons to whom the Software is | |
12 | * furnished to do so, subject to the following conditions: | |
13 | * | |
14 | * The above copyright notice and this permission notice shall be included in | |
15 | * all copies or substantial portions of the Software. | |
16 | * | |
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
20 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
23 | * THE SOFTWARE. | |
24 | */ | |
25 | ||
80c71a24 | 26 | #include "qemu/osdep.h" |
da34e65c | 27 | #include "qapi/error.h" |
f348b6d1 | 28 | #include "qemu/cutils.h" |
1de7afc9 | 29 | #include "qemu/config-file.h" |
737e150e | 30 | #include "block/block_int.h" |
1de7afc9 | 31 | #include "qemu/module.h" |
2c31b04c HR |
32 | #include "qapi/qmp/qbool.h" |
33 | #include "qapi/qmp/qdict.h" | |
2c31b04c | 34 | #include "qapi/qmp/qstring.h" |
20873526 | 35 | #include "sysemu/qtest.h" |
6a143727 KW |
36 | |
37 | typedef struct BDRVBlkdebugState { | |
571cd43e | 38 | int state; |
8f96b5be | 39 | int new_state; |
3dc834f8 | 40 | uint64_t align; |
430b26a8 EB |
41 | uint64_t max_transfer; |
42 | uint64_t opt_write_zero; | |
43 | uint64_t max_write_zero; | |
44 | uint64_t opt_discard; | |
45 | uint64_t max_discard; | |
3c90c65d | 46 | |
036990d7 HR |
47 | /* For blkdebug_refresh_filename() */ |
48 | char *config_file; | |
49 | ||
7fb1cf16 | 50 | QLIST_HEAD(, BlkdebugRule) rules[BLKDBG__MAX]; |
571cd43e | 51 | QSIMPLEQ_HEAD(, BlkdebugRule) active_rules; |
3c90c65d | 52 | QLIST_HEAD(, BlkdebugSuspendedReq) suspended_reqs; |
6a143727 KW |
53 | } BDRVBlkdebugState; |
54 | ||
b9f66d96 | 55 | typedef struct BlkdebugAIOCB { |
7c84b1b8 | 56 | BlockAIOCB common; |
b9f66d96 KW |
57 | int ret; |
58 | } BlkdebugAIOCB; | |
59 | ||
3c90c65d KW |
60 | typedef struct BlkdebugSuspendedReq { |
61 | Coroutine *co; | |
62 | char *tag; | |
63 | QLIST_ENTRY(BlkdebugSuspendedReq) next; | |
64 | } BlkdebugSuspendedReq; | |
65 | ||
8b9b0cc2 KW |
66 | enum { |
67 | ACTION_INJECT_ERROR, | |
68 | ACTION_SET_STATE, | |
3c90c65d | 69 | ACTION_SUSPEND, |
8b9b0cc2 KW |
70 | }; |
71 | ||
72 | typedef struct BlkdebugRule { | |
a31939e6 | 73 | BlkdebugEvent event; |
8b9b0cc2 KW |
74 | int action; |
75 | int state; | |
76 | union { | |
77 | struct { | |
78 | int error; | |
79 | int immediately; | |
80 | int once; | |
7c3a9985 | 81 | int64_t offset; |
8b9b0cc2 KW |
82 | } inject; |
83 | struct { | |
84 | int new_state; | |
85 | } set_state; | |
3c90c65d KW |
86 | struct { |
87 | char *tag; | |
88 | } suspend; | |
8b9b0cc2 KW |
89 | } options; |
90 | QLIST_ENTRY(BlkdebugRule) next; | |
571cd43e | 91 | QSIMPLEQ_ENTRY(BlkdebugRule) active_next; |
8b9b0cc2 KW |
92 | } BlkdebugRule; |
93 | ||
94 | static QemuOptsList inject_error_opts = { | |
95 | .name = "inject-error", | |
96 | .head = QTAILQ_HEAD_INITIALIZER(inject_error_opts.head), | |
97 | .desc = { | |
98 | { | |
99 | .name = "event", | |
100 | .type = QEMU_OPT_STRING, | |
101 | }, | |
102 | { | |
103 | .name = "state", | |
104 | .type = QEMU_OPT_NUMBER, | |
105 | }, | |
106 | { | |
107 | .name = "errno", | |
108 | .type = QEMU_OPT_NUMBER, | |
109 | }, | |
e4780db4 PB |
110 | { |
111 | .name = "sector", | |
112 | .type = QEMU_OPT_NUMBER, | |
113 | }, | |
8b9b0cc2 KW |
114 | { |
115 | .name = "once", | |
116 | .type = QEMU_OPT_BOOL, | |
117 | }, | |
118 | { | |
119 | .name = "immediately", | |
120 | .type = QEMU_OPT_BOOL, | |
121 | }, | |
122 | { /* end of list */ } | |
123 | }, | |
124 | }; | |
125 | ||
126 | static QemuOptsList set_state_opts = { | |
127 | .name = "set-state", | |
327cdad4 | 128 | .head = QTAILQ_HEAD_INITIALIZER(set_state_opts.head), |
8b9b0cc2 KW |
129 | .desc = { |
130 | { | |
131 | .name = "event", | |
132 | .type = QEMU_OPT_STRING, | |
133 | }, | |
134 | { | |
135 | .name = "state", | |
136 | .type = QEMU_OPT_NUMBER, | |
137 | }, | |
138 | { | |
139 | .name = "new_state", | |
140 | .type = QEMU_OPT_NUMBER, | |
141 | }, | |
142 | { /* end of list */ } | |
143 | }, | |
144 | }; | |
145 | ||
146 | static QemuOptsList *config_groups[] = { | |
147 | &inject_error_opts, | |
148 | &set_state_opts, | |
149 | NULL | |
150 | }; | |
151 | ||
a31939e6 | 152 | static int get_event_by_name(const char *name, BlkdebugEvent *event) |
8b9b0cc2 KW |
153 | { |
154 | int i; | |
155 | ||
7fb1cf16 | 156 | for (i = 0; i < BLKDBG__MAX; i++) { |
a31939e6 | 157 | if (!strcmp(BlkdebugEvent_lookup[i], name)) { |
8b9b0cc2 KW |
158 | *event = i; |
159 | return 0; | |
160 | } | |
161 | } | |
162 | ||
163 | return -1; | |
164 | } | |
165 | ||
166 | struct add_rule_data { | |
167 | BDRVBlkdebugState *s; | |
168 | int action; | |
169 | }; | |
170 | ||
28d0de7a | 171 | static int add_rule(void *opaque, QemuOpts *opts, Error **errp) |
8b9b0cc2 KW |
172 | { |
173 | struct add_rule_data *d = opaque; | |
174 | BDRVBlkdebugState *s = d->s; | |
175 | const char* event_name; | |
a31939e6 | 176 | BlkdebugEvent event; |
8b9b0cc2 | 177 | struct BlkdebugRule *rule; |
7c3a9985 | 178 | int64_t sector; |
8b9b0cc2 KW |
179 | |
180 | /* Find the right event for the rule */ | |
181 | event_name = qemu_opt_get(opts, "event"); | |
d4362d64 | 182 | if (!event_name) { |
8809cfc3 | 183 | error_setg(errp, "Missing event name for rule"); |
d4362d64 SH |
184 | return -1; |
185 | } else if (get_event_by_name(event_name, &event) < 0) { | |
8809cfc3 | 186 | error_setg(errp, "Invalid event name \"%s\"", event_name); |
8b9b0cc2 KW |
187 | return -1; |
188 | } | |
189 | ||
190 | /* Set attributes common for all actions */ | |
7267c094 | 191 | rule = g_malloc0(sizeof(*rule)); |
8b9b0cc2 KW |
192 | *rule = (struct BlkdebugRule) { |
193 | .event = event, | |
194 | .action = d->action, | |
195 | .state = qemu_opt_get_number(opts, "state", 0), | |
196 | }; | |
197 | ||
198 | /* Parse action-specific options */ | |
199 | switch (d->action) { | |
200 | case ACTION_INJECT_ERROR: | |
201 | rule->options.inject.error = qemu_opt_get_number(opts, "errno", EIO); | |
202 | rule->options.inject.once = qemu_opt_get_bool(opts, "once", 0); | |
203 | rule->options.inject.immediately = | |
204 | qemu_opt_get_bool(opts, "immediately", 0); | |
7c3a9985 KW |
205 | sector = qemu_opt_get_number(opts, "sector", -1); |
206 | rule->options.inject.offset = | |
207 | sector == -1 ? -1 : sector * BDRV_SECTOR_SIZE; | |
8b9b0cc2 KW |
208 | break; |
209 | ||
210 | case ACTION_SET_STATE: | |
211 | rule->options.set_state.new_state = | |
212 | qemu_opt_get_number(opts, "new_state", 0); | |
213 | break; | |
3c90c65d KW |
214 | |
215 | case ACTION_SUSPEND: | |
216 | rule->options.suspend.tag = | |
217 | g_strdup(qemu_opt_get(opts, "tag")); | |
218 | break; | |
8b9b0cc2 KW |
219 | }; |
220 | ||
221 | /* Add the rule */ | |
222 | QLIST_INSERT_HEAD(&s->rules[event], rule, next); | |
223 | ||
224 | return 0; | |
225 | } | |
226 | ||
9e35542b KW |
227 | static void remove_rule(BlkdebugRule *rule) |
228 | { | |
229 | switch (rule->action) { | |
230 | case ACTION_INJECT_ERROR: | |
231 | case ACTION_SET_STATE: | |
232 | break; | |
3c90c65d KW |
233 | case ACTION_SUSPEND: |
234 | g_free(rule->options.suspend.tag); | |
235 | break; | |
9e35542b KW |
236 | } |
237 | ||
238 | QLIST_REMOVE(rule, next); | |
239 | g_free(rule); | |
240 | } | |
241 | ||
89f2b21e HR |
242 | static int read_config(BDRVBlkdebugState *s, const char *filename, |
243 | QDict *options, Error **errp) | |
8b9b0cc2 | 244 | { |
85a040e5 | 245 | FILE *f = NULL; |
8b9b0cc2 KW |
246 | int ret; |
247 | struct add_rule_data d; | |
89f2b21e | 248 | Error *local_err = NULL; |
8b9b0cc2 | 249 | |
85a040e5 HR |
250 | if (filename) { |
251 | f = fopen(filename, "r"); | |
252 | if (f == NULL) { | |
253 | error_setg_errno(errp, errno, "Could not read blkdebug config file"); | |
254 | return -errno; | |
255 | } | |
8b9b0cc2 | 256 | |
85a040e5 HR |
257 | ret = qemu_config_parse(f, config_groups, filename); |
258 | if (ret < 0) { | |
259 | error_setg(errp, "Could not parse blkdebug config file"); | |
260 | ret = -EINVAL; | |
261 | goto fail; | |
262 | } | |
8b9b0cc2 KW |
263 | } |
264 | ||
89f2b21e | 265 | qemu_config_parse_qdict(options, config_groups, &local_err); |
84d18f06 | 266 | if (local_err) { |
89f2b21e HR |
267 | error_propagate(errp, local_err); |
268 | ret = -EINVAL; | |
269 | goto fail; | |
270 | } | |
271 | ||
8b9b0cc2 KW |
272 | d.s = s; |
273 | d.action = ACTION_INJECT_ERROR; | |
8809cfc3 | 274 | qemu_opts_foreach(&inject_error_opts, add_rule, &d, &local_err); |
d4362d64 SH |
275 | if (local_err) { |
276 | error_propagate(errp, local_err); | |
277 | ret = -EINVAL; | |
278 | goto fail; | |
279 | } | |
8b9b0cc2 KW |
280 | |
281 | d.action = ACTION_SET_STATE; | |
8809cfc3 | 282 | qemu_opts_foreach(&set_state_opts, add_rule, &d, &local_err); |
d4362d64 SH |
283 | if (local_err) { |
284 | error_propagate(errp, local_err); | |
285 | ret = -EINVAL; | |
286 | goto fail; | |
287 | } | |
8b9b0cc2 KW |
288 | |
289 | ret = 0; | |
290 | fail: | |
698f0d52 KW |
291 | qemu_opts_reset(&inject_error_opts); |
292 | qemu_opts_reset(&set_state_opts); | |
85a040e5 HR |
293 | if (f) { |
294 | fclose(f); | |
295 | } | |
8b9b0cc2 KW |
296 | return ret; |
297 | } | |
298 | ||
299 | /* Valid blkdebug filenames look like blkdebug:path/to/config:path/to/image */ | |
f4681212 KW |
300 | static void blkdebug_parse_filename(const char *filename, QDict *options, |
301 | Error **errp) | |
6a143727 | 302 | { |
f4681212 | 303 | const char *c; |
6a143727 | 304 | |
8b9b0cc2 | 305 | /* Parse the blkdebug: prefix */ |
f4681212 | 306 | if (!strstart(filename, "blkdebug:", &filename)) { |
d4881b9b HR |
307 | /* There was no prefix; therefore, all options have to be already |
308 | present in the QDict (except for the filename) */ | |
46f5ac20 | 309 | qdict_put_str(options, "x-image", filename); |
f4681212 | 310 | return; |
6a143727 | 311 | } |
6a143727 | 312 | |
f4681212 | 313 | /* Parse config file path */ |
8b9b0cc2 KW |
314 | c = strchr(filename, ':'); |
315 | if (c == NULL) { | |
f4681212 KW |
316 | error_setg(errp, "blkdebug requires both config file and image path"); |
317 | return; | |
8b9b0cc2 KW |
318 | } |
319 | ||
f4681212 KW |
320 | if (c != filename) { |
321 | QString *config_path; | |
322 | config_path = qstring_from_substr(filename, 0, c - filename - 1); | |
323 | qdict_put(options, "config", config_path); | |
8b9b0cc2 | 324 | } |
f4681212 KW |
325 | |
326 | /* TODO Allow multi-level nesting and set file.filename here */ | |
8b9b0cc2 | 327 | filename = c + 1; |
46f5ac20 | 328 | qdict_put_str(options, "x-image", filename); |
f4681212 KW |
329 | } |
330 | ||
331 | static QemuOptsList runtime_opts = { | |
332 | .name = "blkdebug", | |
333 | .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head), | |
334 | .desc = { | |
335 | { | |
336 | .name = "config", | |
337 | .type = QEMU_OPT_STRING, | |
338 | .help = "Path to the configuration file", | |
339 | }, | |
340 | { | |
341 | .name = "x-image", | |
342 | .type = QEMU_OPT_STRING, | |
343 | .help = "[internal use only, will be removed]", | |
344 | }, | |
b35ee7fb KW |
345 | { |
346 | .name = "align", | |
347 | .type = QEMU_OPT_SIZE, | |
348 | .help = "Required alignment in bytes", | |
349 | }, | |
430b26a8 EB |
350 | { |
351 | .name = "max-transfer", | |
352 | .type = QEMU_OPT_SIZE, | |
353 | .help = "Maximum transfer size in bytes", | |
354 | }, | |
355 | { | |
356 | .name = "opt-write-zero", | |
357 | .type = QEMU_OPT_SIZE, | |
358 | .help = "Optimum write zero alignment in bytes", | |
359 | }, | |
360 | { | |
361 | .name = "max-write-zero", | |
362 | .type = QEMU_OPT_SIZE, | |
363 | .help = "Maximum write zero size in bytes", | |
364 | }, | |
365 | { | |
366 | .name = "opt-discard", | |
367 | .type = QEMU_OPT_SIZE, | |
368 | .help = "Optimum discard alignment in bytes", | |
369 | }, | |
370 | { | |
371 | .name = "max-discard", | |
372 | .type = QEMU_OPT_SIZE, | |
373 | .help = "Maximum discard size in bytes", | |
374 | }, | |
f4681212 KW |
375 | { /* end of list */ } |
376 | }, | |
377 | }; | |
378 | ||
015a1036 HR |
379 | static int blkdebug_open(BlockDriverState *bs, QDict *options, int flags, |
380 | Error **errp) | |
f4681212 KW |
381 | { |
382 | BDRVBlkdebugState *s = bs->opaque; | |
383 | QemuOpts *opts; | |
384 | Error *local_err = NULL; | |
f4681212 | 385 | int ret; |
430b26a8 | 386 | uint64_t align; |
f4681212 | 387 | |
87ea75d5 | 388 | opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort); |
f4681212 | 389 | qemu_opts_absorb_qdict(opts, options, &local_err); |
84d18f06 | 390 | if (local_err) { |
10ffa72f | 391 | error_propagate(errp, local_err); |
f4681212 | 392 | ret = -EINVAL; |
eaf944a4 | 393 | goto out; |
f4681212 KW |
394 | } |
395 | ||
89f2b21e | 396 | /* Read rules from config file or command line options */ |
036990d7 HR |
397 | s->config_file = g_strdup(qemu_opt_get(opts, "config")); |
398 | ret = read_config(s, s->config_file, options, errp); | |
85a040e5 | 399 | if (ret) { |
eaf944a4 | 400 | goto out; |
f4681212 | 401 | } |
8b9b0cc2 | 402 | |
8db520ce | 403 | /* Set initial state */ |
571cd43e | 404 | s->state = 1; |
8db520ce | 405 | |
6b826af7 | 406 | /* Open the image file */ |
9a4f4c31 KW |
407 | bs->file = bdrv_open_child(qemu_opt_get(opts, "x-image"), options, "image", |
408 | bs, &child_file, false, &local_err); | |
409 | if (local_err) { | |
410 | ret = -EINVAL; | |
10ffa72f | 411 | error_propagate(errp, local_err); |
eaf944a4 | 412 | goto out; |
8b9b0cc2 KW |
413 | } |
414 | ||
63188c24 EB |
415 | bs->supported_write_flags = BDRV_REQ_FUA & |
416 | bs->file->bs->supported_write_flags; | |
417 | bs->supported_zero_flags = (BDRV_REQ_FUA | BDRV_REQ_MAY_UNMAP) & | |
418 | bs->file->bs->supported_zero_flags; | |
3dc834f8 | 419 | ret = -EINVAL; |
63188c24 | 420 | |
430b26a8 | 421 | /* Set alignment overrides */ |
3dc834f8 EB |
422 | s->align = qemu_opt_get_size(opts, "align", 0); |
423 | if (s->align && (s->align >= INT_MAX || !is_power_of_2(s->align))) { | |
424 | error_setg(errp, "Cannot meet constraints with align %" PRIu64, | |
425 | s->align); | |
de234897 | 426 | goto out; |
b35ee7fb | 427 | } |
430b26a8 EB |
428 | align = MAX(s->align, bs->file->bs->bl.request_alignment); |
429 | ||
430 | s->max_transfer = qemu_opt_get_size(opts, "max-transfer", 0); | |
431 | if (s->max_transfer && | |
432 | (s->max_transfer >= INT_MAX || | |
433 | !QEMU_IS_ALIGNED(s->max_transfer, align))) { | |
434 | error_setg(errp, "Cannot meet constraints with max-transfer %" PRIu64, | |
435 | s->max_transfer); | |
436 | goto out; | |
437 | } | |
438 | ||
439 | s->opt_write_zero = qemu_opt_get_size(opts, "opt-write-zero", 0); | |
440 | if (s->opt_write_zero && | |
441 | (s->opt_write_zero >= INT_MAX || | |
442 | !QEMU_IS_ALIGNED(s->opt_write_zero, align))) { | |
443 | error_setg(errp, "Cannot meet constraints with opt-write-zero %" PRIu64, | |
444 | s->opt_write_zero); | |
445 | goto out; | |
446 | } | |
447 | ||
448 | s->max_write_zero = qemu_opt_get_size(opts, "max-write-zero", 0); | |
449 | if (s->max_write_zero && | |
450 | (s->max_write_zero >= INT_MAX || | |
451 | !QEMU_IS_ALIGNED(s->max_write_zero, | |
452 | MAX(s->opt_write_zero, align)))) { | |
453 | error_setg(errp, "Cannot meet constraints with max-write-zero %" PRIu64, | |
454 | s->max_write_zero); | |
455 | goto out; | |
456 | } | |
457 | ||
458 | s->opt_discard = qemu_opt_get_size(opts, "opt-discard", 0); | |
459 | if (s->opt_discard && | |
460 | (s->opt_discard >= INT_MAX || | |
461 | !QEMU_IS_ALIGNED(s->opt_discard, align))) { | |
462 | error_setg(errp, "Cannot meet constraints with opt-discard %" PRIu64, | |
463 | s->opt_discard); | |
464 | goto out; | |
465 | } | |
466 | ||
467 | s->max_discard = qemu_opt_get_size(opts, "max-discard", 0); | |
468 | if (s->max_discard && | |
469 | (s->max_discard >= INT_MAX || | |
470 | !QEMU_IS_ALIGNED(s->max_discard, | |
471 | MAX(s->opt_discard, align)))) { | |
472 | error_setg(errp, "Cannot meet constraints with max-discard %" PRIu64, | |
473 | s->max_discard); | |
474 | goto out; | |
475 | } | |
b35ee7fb | 476 | |
f4681212 | 477 | ret = 0; |
eaf944a4 | 478 | out: |
036990d7 HR |
479 | if (ret < 0) { |
480 | g_free(s->config_file); | |
481 | } | |
f4681212 KW |
482 | qemu_opts_del(opts); |
483 | return ret; | |
6a143727 KW |
484 | } |
485 | ||
d157ed5f | 486 | static int rule_check(BlockDriverState *bs, uint64_t offset, uint64_t bytes) |
b9f66d96 KW |
487 | { |
488 | BDRVBlkdebugState *s = bs->opaque; | |
d157ed5f EB |
489 | BlkdebugRule *rule = NULL; |
490 | int error; | |
491 | bool immediately; | |
492 | ||
493 | QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) { | |
494 | uint64_t inject_offset = rule->options.inject.offset; | |
495 | ||
496 | if (inject_offset == -1 || | |
497 | (bytes && inject_offset >= offset && | |
498 | inject_offset < offset + bytes)) | |
499 | { | |
500 | break; | |
501 | } | |
502 | } | |
503 | ||
504 | if (!rule || !rule->options.inject.error) { | |
505 | return 0; | |
506 | } | |
507 | ||
508 | immediately = rule->options.inject.immediately; | |
509 | error = rule->options.inject.error; | |
b9f66d96 | 510 | |
571cd43e | 511 | if (rule->options.inject.once) { |
a069e2f1 JS |
512 | QSIMPLEQ_REMOVE(&s->active_rules, rule, BlkdebugRule, active_next); |
513 | remove_rule(rule); | |
b9f66d96 KW |
514 | } |
515 | ||
7c3a9985 | 516 | if (!immediately) { |
e5c67ab5 | 517 | aio_co_schedule(qemu_get_current_aio_context(), qemu_coroutine_self()); |
7c3a9985 | 518 | qemu_coroutine_yield(); |
b9f66d96 KW |
519 | } |
520 | ||
7c3a9985 | 521 | return -error; |
b9f66d96 KW |
522 | } |
523 | ||
7c3a9985 KW |
524 | static int coroutine_fn |
525 | blkdebug_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes, | |
526 | QEMUIOVector *qiov, int flags) | |
6a143727 | 527 | { |
d157ed5f | 528 | int err; |
e4780db4 | 529 | |
e0ef4395 EB |
530 | /* Sanity check block layer guarantees */ |
531 | assert(QEMU_IS_ALIGNED(offset, bs->bl.request_alignment)); | |
532 | assert(QEMU_IS_ALIGNED(bytes, bs->bl.request_alignment)); | |
533 | if (bs->bl.max_transfer) { | |
534 | assert(bytes <= bs->bl.max_transfer); | |
535 | } | |
536 | ||
d157ed5f EB |
537 | err = rule_check(bs, offset, bytes); |
538 | if (err) { | |
539 | return err; | |
b9f66d96 KW |
540 | } |
541 | ||
7c3a9985 | 542 | return bdrv_co_preadv(bs->file, offset, bytes, qiov, flags); |
6a143727 KW |
543 | } |
544 | ||
7c3a9985 KW |
545 | static int coroutine_fn |
546 | blkdebug_co_pwritev(BlockDriverState *bs, uint64_t offset, uint64_t bytes, | |
547 | QEMUIOVector *qiov, int flags) | |
6a143727 | 548 | { |
d157ed5f | 549 | int err; |
e4780db4 | 550 | |
e0ef4395 EB |
551 | /* Sanity check block layer guarantees */ |
552 | assert(QEMU_IS_ALIGNED(offset, bs->bl.request_alignment)); | |
553 | assert(QEMU_IS_ALIGNED(bytes, bs->bl.request_alignment)); | |
554 | if (bs->bl.max_transfer) { | |
555 | assert(bytes <= bs->bl.max_transfer); | |
556 | } | |
557 | ||
d157ed5f EB |
558 | err = rule_check(bs, offset, bytes); |
559 | if (err) { | |
560 | return err; | |
b9f66d96 KW |
561 | } |
562 | ||
7c3a9985 | 563 | return bdrv_co_pwritev(bs->file, offset, bytes, qiov, flags); |
6a143727 KW |
564 | } |
565 | ||
7c3a9985 | 566 | static int blkdebug_co_flush(BlockDriverState *bs) |
9e52c53b | 567 | { |
d157ed5f | 568 | int err = rule_check(bs, 0, 0); |
9e52c53b | 569 | |
d157ed5f EB |
570 | if (err) { |
571 | return err; | |
9e52c53b PB |
572 | } |
573 | ||
7c3a9985 | 574 | return bdrv_co_flush(bs->file->bs); |
9e52c53b PB |
575 | } |
576 | ||
63188c24 | 577 | static int coroutine_fn blkdebug_co_pwrite_zeroes(BlockDriverState *bs, |
f5a5ca79 | 578 | int64_t offset, int bytes, |
63188c24 EB |
579 | BdrvRequestFlags flags) |
580 | { | |
581 | uint32_t align = MAX(bs->bl.request_alignment, | |
582 | bs->bl.pwrite_zeroes_alignment); | |
583 | int err; | |
584 | ||
585 | /* Only pass through requests that are larger than requested | |
586 | * preferred alignment (so that we test the fallback to writes on | |
587 | * unaligned portions), and check that the block layer never hands | |
588 | * us anything unaligned that crosses an alignment boundary. */ | |
f5a5ca79 | 589 | if (bytes < align) { |
63188c24 | 590 | assert(QEMU_IS_ALIGNED(offset, align) || |
f5a5ca79 | 591 | QEMU_IS_ALIGNED(offset + bytes, align) || |
63188c24 | 592 | DIV_ROUND_UP(offset, align) == |
f5a5ca79 | 593 | DIV_ROUND_UP(offset + bytes, align)); |
63188c24 EB |
594 | return -ENOTSUP; |
595 | } | |
596 | assert(QEMU_IS_ALIGNED(offset, align)); | |
f5a5ca79 | 597 | assert(QEMU_IS_ALIGNED(bytes, align)); |
63188c24 | 598 | if (bs->bl.max_pwrite_zeroes) { |
f5a5ca79 | 599 | assert(bytes <= bs->bl.max_pwrite_zeroes); |
63188c24 EB |
600 | } |
601 | ||
f5a5ca79 | 602 | err = rule_check(bs, offset, bytes); |
63188c24 EB |
603 | if (err) { |
604 | return err; | |
605 | } | |
606 | ||
f5a5ca79 | 607 | return bdrv_co_pwrite_zeroes(bs->file, offset, bytes, flags); |
63188c24 EB |
608 | } |
609 | ||
610 | static int coroutine_fn blkdebug_co_pdiscard(BlockDriverState *bs, | |
f5a5ca79 | 611 | int64_t offset, int bytes) |
63188c24 EB |
612 | { |
613 | uint32_t align = bs->bl.pdiscard_alignment; | |
614 | int err; | |
615 | ||
616 | /* Only pass through requests that are larger than requested | |
617 | * minimum alignment, and ensure that unaligned requests do not | |
618 | * cross optimum discard boundaries. */ | |
f5a5ca79 | 619 | if (bytes < bs->bl.request_alignment) { |
63188c24 | 620 | assert(QEMU_IS_ALIGNED(offset, align) || |
f5a5ca79 | 621 | QEMU_IS_ALIGNED(offset + bytes, align) || |
63188c24 | 622 | DIV_ROUND_UP(offset, align) == |
f5a5ca79 | 623 | DIV_ROUND_UP(offset + bytes, align)); |
63188c24 EB |
624 | return -ENOTSUP; |
625 | } | |
626 | assert(QEMU_IS_ALIGNED(offset, bs->bl.request_alignment)); | |
f5a5ca79 MP |
627 | assert(QEMU_IS_ALIGNED(bytes, bs->bl.request_alignment)); |
628 | if (align && bytes >= align) { | |
63188c24 | 629 | assert(QEMU_IS_ALIGNED(offset, align)); |
f5a5ca79 | 630 | assert(QEMU_IS_ALIGNED(bytes, align)); |
63188c24 EB |
631 | } |
632 | if (bs->bl.max_pdiscard) { | |
f5a5ca79 | 633 | assert(bytes <= bs->bl.max_pdiscard); |
63188c24 EB |
634 | } |
635 | ||
f5a5ca79 | 636 | err = rule_check(bs, offset, bytes); |
63188c24 EB |
637 | if (err) { |
638 | return err; | |
639 | } | |
640 | ||
f5a5ca79 | 641 | return bdrv_co_pdiscard(bs->file->bs, offset, bytes); |
63188c24 | 642 | } |
3c90c65d | 643 | |
544daf66 EB |
644 | static int64_t coroutine_fn blkdebug_co_get_block_status( |
645 | BlockDriverState *bs, int64_t sector_num, int nb_sectors, int *pnum, | |
646 | BlockDriverState **file) | |
647 | { | |
648 | *pnum = nb_sectors; | |
649 | *file = bs->file->bs; | |
650 | return BDRV_BLOCK_RAW | BDRV_BLOCK_OFFSET_VALID | | |
651 | (sector_num << BDRV_SECTOR_BITS); | |
652 | } | |
653 | ||
6a143727 KW |
654 | static void blkdebug_close(BlockDriverState *bs) |
655 | { | |
656 | BDRVBlkdebugState *s = bs->opaque; | |
8b9b0cc2 KW |
657 | BlkdebugRule *rule, *next; |
658 | int i; | |
659 | ||
7fb1cf16 | 660 | for (i = 0; i < BLKDBG__MAX; i++) { |
8b9b0cc2 | 661 | QLIST_FOREACH_SAFE(rule, &s->rules[i], next, next) { |
9e35542b | 662 | remove_rule(rule); |
8b9b0cc2 KW |
663 | } |
664 | } | |
036990d7 HR |
665 | |
666 | g_free(s->config_file); | |
6a143727 KW |
667 | } |
668 | ||
3c90c65d KW |
669 | static void suspend_request(BlockDriverState *bs, BlkdebugRule *rule) |
670 | { | |
671 | BDRVBlkdebugState *s = bs->opaque; | |
672 | BlkdebugSuspendedReq r; | |
673 | ||
674 | r = (BlkdebugSuspendedReq) { | |
675 | .co = qemu_coroutine_self(), | |
676 | .tag = g_strdup(rule->options.suspend.tag), | |
677 | }; | |
678 | ||
679 | remove_rule(rule); | |
680 | QLIST_INSERT_HEAD(&s->suspended_reqs, &r, next); | |
681 | ||
20873526 MT |
682 | if (!qtest_enabled()) { |
683 | printf("blkdebug: Suspended request '%s'\n", r.tag); | |
684 | } | |
3c90c65d | 685 | qemu_coroutine_yield(); |
20873526 MT |
686 | if (!qtest_enabled()) { |
687 | printf("blkdebug: Resuming request '%s'\n", r.tag); | |
688 | } | |
3c90c65d KW |
689 | |
690 | QLIST_REMOVE(&r, next); | |
691 | g_free(r.tag); | |
692 | } | |
693 | ||
571cd43e | 694 | static bool process_rule(BlockDriverState *bs, struct BlkdebugRule *rule, |
8f96b5be | 695 | bool injected) |
8b9b0cc2 KW |
696 | { |
697 | BDRVBlkdebugState *s = bs->opaque; | |
8b9b0cc2 KW |
698 | |
699 | /* Only process rules for the current state */ | |
8f96b5be | 700 | if (rule->state && rule->state != s->state) { |
571cd43e | 701 | return injected; |
8b9b0cc2 KW |
702 | } |
703 | ||
704 | /* Take the action */ | |
705 | switch (rule->action) { | |
706 | case ACTION_INJECT_ERROR: | |
571cd43e PB |
707 | if (!injected) { |
708 | QSIMPLEQ_INIT(&s->active_rules); | |
709 | injected = true; | |
710 | } | |
711 | QSIMPLEQ_INSERT_HEAD(&s->active_rules, rule, active_next); | |
8b9b0cc2 KW |
712 | break; |
713 | ||
714 | case ACTION_SET_STATE: | |
8f96b5be | 715 | s->new_state = rule->options.set_state.new_state; |
8b9b0cc2 | 716 | break; |
3c90c65d KW |
717 | |
718 | case ACTION_SUSPEND: | |
719 | suspend_request(bs, rule); | |
720 | break; | |
8b9b0cc2 | 721 | } |
571cd43e | 722 | return injected; |
8b9b0cc2 KW |
723 | } |
724 | ||
a31939e6 | 725 | static void blkdebug_debug_event(BlockDriverState *bs, BlkdebugEvent event) |
8b9b0cc2 KW |
726 | { |
727 | BDRVBlkdebugState *s = bs->opaque; | |
3c90c65d | 728 | struct BlkdebugRule *rule, *next; |
571cd43e | 729 | bool injected; |
8b9b0cc2 | 730 | |
7fb1cf16 | 731 | assert((int)event >= 0 && event < BLKDBG__MAX); |
8b9b0cc2 | 732 | |
571cd43e | 733 | injected = false; |
8f96b5be | 734 | s->new_state = s->state; |
3c90c65d | 735 | QLIST_FOREACH_SAFE(rule, &s->rules[event], next, next) { |
8f96b5be | 736 | injected = process_rule(bs, rule, injected); |
8b9b0cc2 | 737 | } |
8f96b5be | 738 | s->state = s->new_state; |
8b9b0cc2 KW |
739 | } |
740 | ||
3c90c65d KW |
741 | static int blkdebug_debug_breakpoint(BlockDriverState *bs, const char *event, |
742 | const char *tag) | |
743 | { | |
744 | BDRVBlkdebugState *s = bs->opaque; | |
745 | struct BlkdebugRule *rule; | |
a31939e6 | 746 | BlkdebugEvent blkdebug_event; |
3c90c65d KW |
747 | |
748 | if (get_event_by_name(event, &blkdebug_event) < 0) { | |
749 | return -ENOENT; | |
750 | } | |
751 | ||
752 | ||
753 | rule = g_malloc(sizeof(*rule)); | |
754 | *rule = (struct BlkdebugRule) { | |
755 | .event = blkdebug_event, | |
756 | .action = ACTION_SUSPEND, | |
757 | .state = 0, | |
758 | .options.suspend.tag = g_strdup(tag), | |
759 | }; | |
760 | ||
761 | QLIST_INSERT_HEAD(&s->rules[blkdebug_event], rule, next); | |
762 | ||
763 | return 0; | |
764 | } | |
765 | ||
766 | static int blkdebug_debug_resume(BlockDriverState *bs, const char *tag) | |
767 | { | |
768 | BDRVBlkdebugState *s = bs->opaque; | |
c547e564 | 769 | BlkdebugSuspendedReq *r, *next; |
3c90c65d | 770 | |
c547e564 | 771 | QLIST_FOREACH_SAFE(r, &s->suspended_reqs, next, next) { |
3c90c65d | 772 | if (!strcmp(r->tag, tag)) { |
0b8b8753 | 773 | qemu_coroutine_enter(r->co); |
3c90c65d KW |
774 | return 0; |
775 | } | |
776 | } | |
777 | return -ENOENT; | |
778 | } | |
779 | ||
4cc70e93 FZ |
780 | static int blkdebug_debug_remove_breakpoint(BlockDriverState *bs, |
781 | const char *tag) | |
782 | { | |
783 | BDRVBlkdebugState *s = bs->opaque; | |
c547e564 | 784 | BlkdebugSuspendedReq *r, *r_next; |
4cc70e93 FZ |
785 | BlkdebugRule *rule, *next; |
786 | int i, ret = -ENOENT; | |
787 | ||
7fb1cf16 | 788 | for (i = 0; i < BLKDBG__MAX; i++) { |
4cc70e93 FZ |
789 | QLIST_FOREACH_SAFE(rule, &s->rules[i], next, next) { |
790 | if (rule->action == ACTION_SUSPEND && | |
791 | !strcmp(rule->options.suspend.tag, tag)) { | |
792 | remove_rule(rule); | |
793 | ret = 0; | |
794 | } | |
795 | } | |
796 | } | |
c547e564 | 797 | QLIST_FOREACH_SAFE(r, &s->suspended_reqs, next, r_next) { |
4cc70e93 | 798 | if (!strcmp(r->tag, tag)) { |
0b8b8753 | 799 | qemu_coroutine_enter(r->co); |
4cc70e93 FZ |
800 | ret = 0; |
801 | } | |
802 | } | |
803 | return ret; | |
804 | } | |
3c90c65d KW |
805 | |
806 | static bool blkdebug_debug_is_suspended(BlockDriverState *bs, const char *tag) | |
807 | { | |
808 | BDRVBlkdebugState *s = bs->opaque; | |
809 | BlkdebugSuspendedReq *r; | |
810 | ||
811 | QLIST_FOREACH(r, &s->suspended_reqs, next) { | |
812 | if (!strcmp(r->tag, tag)) { | |
813 | return true; | |
814 | } | |
815 | } | |
816 | return false; | |
817 | } | |
818 | ||
e1302255 PB |
819 | static int64_t blkdebug_getlength(BlockDriverState *bs) |
820 | { | |
9a4f4c31 | 821 | return bdrv_getlength(bs->file->bs); |
e1302255 PB |
822 | } |
823 | ||
4bff28b8 | 824 | static int blkdebug_truncate(BlockDriverState *bs, int64_t offset, Error **errp) |
8eedfbd4 | 825 | { |
4bff28b8 | 826 | return bdrv_truncate(bs->file, offset, errp); |
8eedfbd4 KW |
827 | } |
828 | ||
4cdd01d3 | 829 | static void blkdebug_refresh_filename(BlockDriverState *bs, QDict *options) |
2c31b04c | 830 | { |
036990d7 | 831 | BDRVBlkdebugState *s = bs->opaque; |
2c31b04c | 832 | QDict *opts; |
8779441b HR |
833 | const QDictEntry *e; |
834 | bool force_json = false; | |
835 | ||
4cdd01d3 | 836 | for (e = qdict_first(options); e; e = qdict_next(options, e)) { |
8779441b | 837 | if (strcmp(qdict_entry_key(e), "config") && |
4cdd01d3 | 838 | strcmp(qdict_entry_key(e), "x-image")) |
8779441b HR |
839 | { |
840 | force_json = true; | |
841 | break; | |
842 | } | |
843 | } | |
2c31b04c | 844 | |
9a4f4c31 | 845 | if (force_json && !bs->file->bs->full_open_options) { |
2c31b04c HR |
846 | /* The config file cannot be recreated, so creating a plain filename |
847 | * is impossible */ | |
848 | return; | |
849 | } | |
850 | ||
9a4f4c31 | 851 | if (!force_json && bs->file->bs->exact_filename[0]) { |
de81d72d HR |
852 | int ret = snprintf(bs->exact_filename, sizeof(bs->exact_filename), |
853 | "blkdebug:%s:%s", s->config_file ?: "", | |
854 | bs->file->bs->exact_filename); | |
855 | if (ret >= sizeof(bs->exact_filename)) { | |
856 | /* An overflow makes the filename unusable, so do not report any */ | |
857 | bs->exact_filename[0] = 0; | |
858 | } | |
8779441b HR |
859 | } |
860 | ||
2c31b04c | 861 | opts = qdict_new(); |
46f5ac20 | 862 | qdict_put_str(opts, "driver", "blkdebug"); |
2c31b04c | 863 | |
9a4f4c31 | 864 | QINCREF(bs->file->bs->full_open_options); |
de6e7951 | 865 | qdict_put(opts, "image", bs->file->bs->full_open_options); |
2c31b04c | 866 | |
4cdd01d3 KW |
867 | for (e = qdict_first(options); e; e = qdict_next(options, e)) { |
868 | if (strcmp(qdict_entry_key(e), "x-image")) { | |
8779441b HR |
869 | qobject_incref(qdict_entry_value(e)); |
870 | qdict_put_obj(opts, qdict_entry_key(e), qdict_entry_value(e)); | |
2c31b04c HR |
871 | } |
872 | } | |
873 | ||
2c31b04c HR |
874 | bs->full_open_options = opts; |
875 | } | |
876 | ||
835db3ee EB |
877 | static void blkdebug_refresh_limits(BlockDriverState *bs, Error **errp) |
878 | { | |
879 | BDRVBlkdebugState *s = bs->opaque; | |
880 | ||
881 | if (s->align) { | |
a5b8dd2c | 882 | bs->bl.request_alignment = s->align; |
835db3ee | 883 | } |
430b26a8 EB |
884 | if (s->max_transfer) { |
885 | bs->bl.max_transfer = s->max_transfer; | |
886 | } | |
887 | if (s->opt_write_zero) { | |
888 | bs->bl.pwrite_zeroes_alignment = s->opt_write_zero; | |
889 | } | |
890 | if (s->max_write_zero) { | |
891 | bs->bl.max_pwrite_zeroes = s->max_write_zero; | |
892 | } | |
893 | if (s->opt_discard) { | |
894 | bs->bl.pdiscard_alignment = s->opt_discard; | |
895 | } | |
896 | if (s->max_discard) { | |
897 | bs->bl.max_pdiscard = s->max_discard; | |
898 | } | |
835db3ee EB |
899 | } |
900 | ||
c5e8bfb7 KW |
901 | static int blkdebug_reopen_prepare(BDRVReopenState *reopen_state, |
902 | BlockReopenQueue *queue, Error **errp) | |
903 | { | |
904 | return 0; | |
905 | } | |
906 | ||
6a143727 | 907 | static BlockDriver bdrv_blkdebug = { |
f4681212 KW |
908 | .format_name = "blkdebug", |
909 | .protocol_name = "blkdebug", | |
910 | .instance_size = sizeof(BDRVBlkdebugState), | |
6a143727 | 911 | |
f4681212 KW |
912 | .bdrv_parse_filename = blkdebug_parse_filename, |
913 | .bdrv_file_open = blkdebug_open, | |
914 | .bdrv_close = blkdebug_close, | |
c5e8bfb7 | 915 | .bdrv_reopen_prepare = blkdebug_reopen_prepare, |
d7010dfb KW |
916 | .bdrv_child_perm = bdrv_filter_default_perms, |
917 | ||
f4681212 | 918 | .bdrv_getlength = blkdebug_getlength, |
8eedfbd4 | 919 | .bdrv_truncate = blkdebug_truncate, |
2c31b04c | 920 | .bdrv_refresh_filename = blkdebug_refresh_filename, |
835db3ee | 921 | .bdrv_refresh_limits = blkdebug_refresh_limits, |
6a143727 | 922 | |
7c3a9985 KW |
923 | .bdrv_co_preadv = blkdebug_co_preadv, |
924 | .bdrv_co_pwritev = blkdebug_co_pwritev, | |
925 | .bdrv_co_flush_to_disk = blkdebug_co_flush, | |
63188c24 EB |
926 | .bdrv_co_pwrite_zeroes = blkdebug_co_pwrite_zeroes, |
927 | .bdrv_co_pdiscard = blkdebug_co_pdiscard, | |
544daf66 | 928 | .bdrv_co_get_block_status = blkdebug_co_get_block_status, |
8b9b0cc2 | 929 | |
3c90c65d KW |
930 | .bdrv_debug_event = blkdebug_debug_event, |
931 | .bdrv_debug_breakpoint = blkdebug_debug_breakpoint, | |
4cc70e93 FZ |
932 | .bdrv_debug_remove_breakpoint |
933 | = blkdebug_debug_remove_breakpoint, | |
3c90c65d KW |
934 | .bdrv_debug_resume = blkdebug_debug_resume, |
935 | .bdrv_debug_is_suspended = blkdebug_debug_is_suspended, | |
6a143727 KW |
936 | }; |
937 | ||
938 | static void bdrv_blkdebug_init(void) | |
939 | { | |
940 | bdrv_register(&bdrv_blkdebug); | |
941 | } | |
942 | ||
943 | block_init(bdrv_blkdebug_init); |