]>
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" |
922a01a0 | 32 | #include "qemu/option.h" |
2c31b04c | 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 | ||
8b9b0cc2 KW |
152 | struct add_rule_data { |
153 | BDRVBlkdebugState *s; | |
154 | int action; | |
155 | }; | |
156 | ||
28d0de7a | 157 | static int add_rule(void *opaque, QemuOpts *opts, Error **errp) |
8b9b0cc2 KW |
158 | { |
159 | struct add_rule_data *d = opaque; | |
160 | BDRVBlkdebugState *s = d->s; | |
161 | const char* event_name; | |
f9509d15 | 162 | int event; |
8b9b0cc2 | 163 | struct BlkdebugRule *rule; |
7c3a9985 | 164 | int64_t sector; |
8b9b0cc2 KW |
165 | |
166 | /* Find the right event for the rule */ | |
167 | event_name = qemu_opt_get(opts, "event"); | |
d4362d64 | 168 | if (!event_name) { |
8809cfc3 | 169 | error_setg(errp, "Missing event name for rule"); |
d4362d64 | 170 | return -1; |
f9509d15 | 171 | } |
f7abe0ec | 172 | event = qapi_enum_parse(&BlkdebugEvent_lookup, event_name, -1, errp); |
f9509d15 | 173 | if (event < 0) { |
8b9b0cc2 KW |
174 | return -1; |
175 | } | |
176 | ||
177 | /* Set attributes common for all actions */ | |
7267c094 | 178 | rule = g_malloc0(sizeof(*rule)); |
8b9b0cc2 KW |
179 | *rule = (struct BlkdebugRule) { |
180 | .event = event, | |
181 | .action = d->action, | |
182 | .state = qemu_opt_get_number(opts, "state", 0), | |
183 | }; | |
184 | ||
185 | /* Parse action-specific options */ | |
186 | switch (d->action) { | |
187 | case ACTION_INJECT_ERROR: | |
188 | rule->options.inject.error = qemu_opt_get_number(opts, "errno", EIO); | |
189 | rule->options.inject.once = qemu_opt_get_bool(opts, "once", 0); | |
190 | rule->options.inject.immediately = | |
191 | qemu_opt_get_bool(opts, "immediately", 0); | |
7c3a9985 KW |
192 | sector = qemu_opt_get_number(opts, "sector", -1); |
193 | rule->options.inject.offset = | |
194 | sector == -1 ? -1 : sector * BDRV_SECTOR_SIZE; | |
8b9b0cc2 KW |
195 | break; |
196 | ||
197 | case ACTION_SET_STATE: | |
198 | rule->options.set_state.new_state = | |
199 | qemu_opt_get_number(opts, "new_state", 0); | |
200 | break; | |
3c90c65d KW |
201 | |
202 | case ACTION_SUSPEND: | |
203 | rule->options.suspend.tag = | |
204 | g_strdup(qemu_opt_get(opts, "tag")); | |
205 | break; | |
8b9b0cc2 KW |
206 | }; |
207 | ||
208 | /* Add the rule */ | |
209 | QLIST_INSERT_HEAD(&s->rules[event], rule, next); | |
210 | ||
211 | return 0; | |
212 | } | |
213 | ||
9e35542b KW |
214 | static void remove_rule(BlkdebugRule *rule) |
215 | { | |
216 | switch (rule->action) { | |
217 | case ACTION_INJECT_ERROR: | |
218 | case ACTION_SET_STATE: | |
219 | break; | |
3c90c65d KW |
220 | case ACTION_SUSPEND: |
221 | g_free(rule->options.suspend.tag); | |
222 | break; | |
9e35542b KW |
223 | } |
224 | ||
225 | QLIST_REMOVE(rule, next); | |
226 | g_free(rule); | |
227 | } | |
228 | ||
89f2b21e HR |
229 | static int read_config(BDRVBlkdebugState *s, const char *filename, |
230 | QDict *options, Error **errp) | |
8b9b0cc2 | 231 | { |
85a040e5 | 232 | FILE *f = NULL; |
8b9b0cc2 KW |
233 | int ret; |
234 | struct add_rule_data d; | |
89f2b21e | 235 | Error *local_err = NULL; |
8b9b0cc2 | 236 | |
85a040e5 HR |
237 | if (filename) { |
238 | f = fopen(filename, "r"); | |
239 | if (f == NULL) { | |
240 | error_setg_errno(errp, errno, "Could not read blkdebug config file"); | |
241 | return -errno; | |
242 | } | |
8b9b0cc2 | 243 | |
85a040e5 HR |
244 | ret = qemu_config_parse(f, config_groups, filename); |
245 | if (ret < 0) { | |
246 | error_setg(errp, "Could not parse blkdebug config file"); | |
85a040e5 HR |
247 | goto fail; |
248 | } | |
8b9b0cc2 KW |
249 | } |
250 | ||
89f2b21e | 251 | qemu_config_parse_qdict(options, config_groups, &local_err); |
84d18f06 | 252 | if (local_err) { |
89f2b21e HR |
253 | error_propagate(errp, local_err); |
254 | ret = -EINVAL; | |
255 | goto fail; | |
256 | } | |
257 | ||
8b9b0cc2 KW |
258 | d.s = s; |
259 | d.action = ACTION_INJECT_ERROR; | |
8809cfc3 | 260 | qemu_opts_foreach(&inject_error_opts, add_rule, &d, &local_err); |
d4362d64 SH |
261 | if (local_err) { |
262 | error_propagate(errp, local_err); | |
263 | ret = -EINVAL; | |
264 | goto fail; | |
265 | } | |
8b9b0cc2 KW |
266 | |
267 | d.action = ACTION_SET_STATE; | |
8809cfc3 | 268 | qemu_opts_foreach(&set_state_opts, add_rule, &d, &local_err); |
d4362d64 SH |
269 | if (local_err) { |
270 | error_propagate(errp, local_err); | |
271 | ret = -EINVAL; | |
272 | goto fail; | |
273 | } | |
8b9b0cc2 KW |
274 | |
275 | ret = 0; | |
276 | fail: | |
698f0d52 KW |
277 | qemu_opts_reset(&inject_error_opts); |
278 | qemu_opts_reset(&set_state_opts); | |
85a040e5 HR |
279 | if (f) { |
280 | fclose(f); | |
281 | } | |
8b9b0cc2 KW |
282 | return ret; |
283 | } | |
284 | ||
285 | /* Valid blkdebug filenames look like blkdebug:path/to/config:path/to/image */ | |
f4681212 KW |
286 | static void blkdebug_parse_filename(const char *filename, QDict *options, |
287 | Error **errp) | |
6a143727 | 288 | { |
f4681212 | 289 | const char *c; |
6a143727 | 290 | |
8b9b0cc2 | 291 | /* Parse the blkdebug: prefix */ |
f4681212 | 292 | if (!strstart(filename, "blkdebug:", &filename)) { |
d4881b9b HR |
293 | /* There was no prefix; therefore, all options have to be already |
294 | present in the QDict (except for the filename) */ | |
46f5ac20 | 295 | qdict_put_str(options, "x-image", filename); |
f4681212 | 296 | return; |
6a143727 | 297 | } |
6a143727 | 298 | |
f4681212 | 299 | /* Parse config file path */ |
8b9b0cc2 KW |
300 | c = strchr(filename, ':'); |
301 | if (c == NULL) { | |
f4681212 KW |
302 | error_setg(errp, "blkdebug requires both config file and image path"); |
303 | return; | |
8b9b0cc2 KW |
304 | } |
305 | ||
f4681212 KW |
306 | if (c != filename) { |
307 | QString *config_path; | |
308 | config_path = qstring_from_substr(filename, 0, c - filename - 1); | |
309 | qdict_put(options, "config", config_path); | |
8b9b0cc2 | 310 | } |
f4681212 KW |
311 | |
312 | /* TODO Allow multi-level nesting and set file.filename here */ | |
8b9b0cc2 | 313 | filename = c + 1; |
46f5ac20 | 314 | qdict_put_str(options, "x-image", filename); |
f4681212 KW |
315 | } |
316 | ||
317 | static QemuOptsList runtime_opts = { | |
318 | .name = "blkdebug", | |
319 | .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head), | |
320 | .desc = { | |
321 | { | |
322 | .name = "config", | |
323 | .type = QEMU_OPT_STRING, | |
324 | .help = "Path to the configuration file", | |
325 | }, | |
326 | { | |
327 | .name = "x-image", | |
328 | .type = QEMU_OPT_STRING, | |
329 | .help = "[internal use only, will be removed]", | |
330 | }, | |
b35ee7fb KW |
331 | { |
332 | .name = "align", | |
333 | .type = QEMU_OPT_SIZE, | |
334 | .help = "Required alignment in bytes", | |
335 | }, | |
430b26a8 EB |
336 | { |
337 | .name = "max-transfer", | |
338 | .type = QEMU_OPT_SIZE, | |
339 | .help = "Maximum transfer size in bytes", | |
340 | }, | |
341 | { | |
342 | .name = "opt-write-zero", | |
343 | .type = QEMU_OPT_SIZE, | |
344 | .help = "Optimum write zero alignment in bytes", | |
345 | }, | |
346 | { | |
347 | .name = "max-write-zero", | |
348 | .type = QEMU_OPT_SIZE, | |
349 | .help = "Maximum write zero size in bytes", | |
350 | }, | |
351 | { | |
352 | .name = "opt-discard", | |
353 | .type = QEMU_OPT_SIZE, | |
354 | .help = "Optimum discard alignment in bytes", | |
355 | }, | |
356 | { | |
357 | .name = "max-discard", | |
358 | .type = QEMU_OPT_SIZE, | |
359 | .help = "Maximum discard size in bytes", | |
360 | }, | |
f4681212 KW |
361 | { /* end of list */ } |
362 | }, | |
363 | }; | |
364 | ||
015a1036 HR |
365 | static int blkdebug_open(BlockDriverState *bs, QDict *options, int flags, |
366 | Error **errp) | |
f4681212 KW |
367 | { |
368 | BDRVBlkdebugState *s = bs->opaque; | |
369 | QemuOpts *opts; | |
370 | Error *local_err = NULL; | |
f4681212 | 371 | int ret; |
430b26a8 | 372 | uint64_t align; |
f4681212 | 373 | |
87ea75d5 | 374 | opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort); |
f4681212 | 375 | qemu_opts_absorb_qdict(opts, options, &local_err); |
84d18f06 | 376 | if (local_err) { |
10ffa72f | 377 | error_propagate(errp, local_err); |
f4681212 | 378 | ret = -EINVAL; |
eaf944a4 | 379 | goto out; |
f4681212 KW |
380 | } |
381 | ||
89f2b21e | 382 | /* Read rules from config file or command line options */ |
036990d7 HR |
383 | s->config_file = g_strdup(qemu_opt_get(opts, "config")); |
384 | ret = read_config(s, s->config_file, options, errp); | |
85a040e5 | 385 | if (ret) { |
eaf944a4 | 386 | goto out; |
f4681212 | 387 | } |
8b9b0cc2 | 388 | |
8db520ce | 389 | /* Set initial state */ |
571cd43e | 390 | s->state = 1; |
8db520ce | 391 | |
6b826af7 | 392 | /* Open the image file */ |
9a4f4c31 KW |
393 | bs->file = bdrv_open_child(qemu_opt_get(opts, "x-image"), options, "image", |
394 | bs, &child_file, false, &local_err); | |
395 | if (local_err) { | |
396 | ret = -EINVAL; | |
10ffa72f | 397 | error_propagate(errp, local_err); |
eaf944a4 | 398 | goto out; |
8b9b0cc2 KW |
399 | } |
400 | ||
63188c24 EB |
401 | bs->supported_write_flags = BDRV_REQ_FUA & |
402 | bs->file->bs->supported_write_flags; | |
403 | bs->supported_zero_flags = (BDRV_REQ_FUA | BDRV_REQ_MAY_UNMAP) & | |
404 | bs->file->bs->supported_zero_flags; | |
3dc834f8 | 405 | ret = -EINVAL; |
63188c24 | 406 | |
430b26a8 | 407 | /* Set alignment overrides */ |
3dc834f8 EB |
408 | s->align = qemu_opt_get_size(opts, "align", 0); |
409 | if (s->align && (s->align >= INT_MAX || !is_power_of_2(s->align))) { | |
410 | error_setg(errp, "Cannot meet constraints with align %" PRIu64, | |
411 | s->align); | |
de234897 | 412 | goto out; |
b35ee7fb | 413 | } |
430b26a8 EB |
414 | align = MAX(s->align, bs->file->bs->bl.request_alignment); |
415 | ||
416 | s->max_transfer = qemu_opt_get_size(opts, "max-transfer", 0); | |
417 | if (s->max_transfer && | |
418 | (s->max_transfer >= INT_MAX || | |
419 | !QEMU_IS_ALIGNED(s->max_transfer, align))) { | |
420 | error_setg(errp, "Cannot meet constraints with max-transfer %" PRIu64, | |
421 | s->max_transfer); | |
422 | goto out; | |
423 | } | |
424 | ||
425 | s->opt_write_zero = qemu_opt_get_size(opts, "opt-write-zero", 0); | |
426 | if (s->opt_write_zero && | |
427 | (s->opt_write_zero >= INT_MAX || | |
428 | !QEMU_IS_ALIGNED(s->opt_write_zero, align))) { | |
429 | error_setg(errp, "Cannot meet constraints with opt-write-zero %" PRIu64, | |
430 | s->opt_write_zero); | |
431 | goto out; | |
432 | } | |
433 | ||
434 | s->max_write_zero = qemu_opt_get_size(opts, "max-write-zero", 0); | |
435 | if (s->max_write_zero && | |
436 | (s->max_write_zero >= INT_MAX || | |
437 | !QEMU_IS_ALIGNED(s->max_write_zero, | |
438 | MAX(s->opt_write_zero, align)))) { | |
439 | error_setg(errp, "Cannot meet constraints with max-write-zero %" PRIu64, | |
440 | s->max_write_zero); | |
441 | goto out; | |
442 | } | |
443 | ||
444 | s->opt_discard = qemu_opt_get_size(opts, "opt-discard", 0); | |
445 | if (s->opt_discard && | |
446 | (s->opt_discard >= INT_MAX || | |
447 | !QEMU_IS_ALIGNED(s->opt_discard, align))) { | |
448 | error_setg(errp, "Cannot meet constraints with opt-discard %" PRIu64, | |
449 | s->opt_discard); | |
450 | goto out; | |
451 | } | |
452 | ||
453 | s->max_discard = qemu_opt_get_size(opts, "max-discard", 0); | |
454 | if (s->max_discard && | |
455 | (s->max_discard >= INT_MAX || | |
456 | !QEMU_IS_ALIGNED(s->max_discard, | |
457 | MAX(s->opt_discard, align)))) { | |
458 | error_setg(errp, "Cannot meet constraints with max-discard %" PRIu64, | |
459 | s->max_discard); | |
460 | goto out; | |
461 | } | |
b35ee7fb | 462 | |
f4681212 | 463 | ret = 0; |
eaf944a4 | 464 | out: |
036990d7 HR |
465 | if (ret < 0) { |
466 | g_free(s->config_file); | |
467 | } | |
f4681212 KW |
468 | qemu_opts_del(opts); |
469 | return ret; | |
6a143727 KW |
470 | } |
471 | ||
d157ed5f | 472 | static int rule_check(BlockDriverState *bs, uint64_t offset, uint64_t bytes) |
b9f66d96 KW |
473 | { |
474 | BDRVBlkdebugState *s = bs->opaque; | |
d157ed5f EB |
475 | BlkdebugRule *rule = NULL; |
476 | int error; | |
477 | bool immediately; | |
478 | ||
479 | QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) { | |
480 | uint64_t inject_offset = rule->options.inject.offset; | |
481 | ||
482 | if (inject_offset == -1 || | |
483 | (bytes && inject_offset >= offset && | |
484 | inject_offset < offset + bytes)) | |
485 | { | |
486 | break; | |
487 | } | |
488 | } | |
489 | ||
490 | if (!rule || !rule->options.inject.error) { | |
491 | return 0; | |
492 | } | |
493 | ||
494 | immediately = rule->options.inject.immediately; | |
495 | error = rule->options.inject.error; | |
b9f66d96 | 496 | |
571cd43e | 497 | if (rule->options.inject.once) { |
a069e2f1 JS |
498 | QSIMPLEQ_REMOVE(&s->active_rules, rule, BlkdebugRule, active_next); |
499 | remove_rule(rule); | |
b9f66d96 KW |
500 | } |
501 | ||
7c3a9985 | 502 | if (!immediately) { |
e5c67ab5 | 503 | aio_co_schedule(qemu_get_current_aio_context(), qemu_coroutine_self()); |
7c3a9985 | 504 | qemu_coroutine_yield(); |
b9f66d96 KW |
505 | } |
506 | ||
7c3a9985 | 507 | return -error; |
b9f66d96 KW |
508 | } |
509 | ||
7c3a9985 KW |
510 | static int coroutine_fn |
511 | blkdebug_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes, | |
512 | QEMUIOVector *qiov, int flags) | |
6a143727 | 513 | { |
d157ed5f | 514 | int err; |
e4780db4 | 515 | |
e0ef4395 EB |
516 | /* Sanity check block layer guarantees */ |
517 | assert(QEMU_IS_ALIGNED(offset, bs->bl.request_alignment)); | |
518 | assert(QEMU_IS_ALIGNED(bytes, bs->bl.request_alignment)); | |
519 | if (bs->bl.max_transfer) { | |
520 | assert(bytes <= bs->bl.max_transfer); | |
521 | } | |
522 | ||
d157ed5f EB |
523 | err = rule_check(bs, offset, bytes); |
524 | if (err) { | |
525 | return err; | |
b9f66d96 KW |
526 | } |
527 | ||
7c3a9985 | 528 | return bdrv_co_preadv(bs->file, offset, bytes, qiov, flags); |
6a143727 KW |
529 | } |
530 | ||
7c3a9985 KW |
531 | static int coroutine_fn |
532 | blkdebug_co_pwritev(BlockDriverState *bs, uint64_t offset, uint64_t bytes, | |
533 | QEMUIOVector *qiov, int flags) | |
6a143727 | 534 | { |
d157ed5f | 535 | int err; |
e4780db4 | 536 | |
e0ef4395 EB |
537 | /* Sanity check block layer guarantees */ |
538 | assert(QEMU_IS_ALIGNED(offset, bs->bl.request_alignment)); | |
539 | assert(QEMU_IS_ALIGNED(bytes, bs->bl.request_alignment)); | |
540 | if (bs->bl.max_transfer) { | |
541 | assert(bytes <= bs->bl.max_transfer); | |
542 | } | |
543 | ||
d157ed5f EB |
544 | err = rule_check(bs, offset, bytes); |
545 | if (err) { | |
546 | return err; | |
b9f66d96 KW |
547 | } |
548 | ||
7c3a9985 | 549 | return bdrv_co_pwritev(bs->file, offset, bytes, qiov, flags); |
6a143727 KW |
550 | } |
551 | ||
7c3a9985 | 552 | static int blkdebug_co_flush(BlockDriverState *bs) |
9e52c53b | 553 | { |
d157ed5f | 554 | int err = rule_check(bs, 0, 0); |
9e52c53b | 555 | |
d157ed5f EB |
556 | if (err) { |
557 | return err; | |
9e52c53b PB |
558 | } |
559 | ||
7c3a9985 | 560 | return bdrv_co_flush(bs->file->bs); |
9e52c53b PB |
561 | } |
562 | ||
63188c24 | 563 | static int coroutine_fn blkdebug_co_pwrite_zeroes(BlockDriverState *bs, |
f5a5ca79 | 564 | int64_t offset, int bytes, |
63188c24 EB |
565 | BdrvRequestFlags flags) |
566 | { | |
567 | uint32_t align = MAX(bs->bl.request_alignment, | |
568 | bs->bl.pwrite_zeroes_alignment); | |
569 | int err; | |
570 | ||
571 | /* Only pass through requests that are larger than requested | |
572 | * preferred alignment (so that we test the fallback to writes on | |
573 | * unaligned portions), and check that the block layer never hands | |
574 | * us anything unaligned that crosses an alignment boundary. */ | |
f5a5ca79 | 575 | if (bytes < align) { |
63188c24 | 576 | assert(QEMU_IS_ALIGNED(offset, align) || |
f5a5ca79 | 577 | QEMU_IS_ALIGNED(offset + bytes, align) || |
63188c24 | 578 | DIV_ROUND_UP(offset, align) == |
f5a5ca79 | 579 | DIV_ROUND_UP(offset + bytes, align)); |
63188c24 EB |
580 | return -ENOTSUP; |
581 | } | |
582 | assert(QEMU_IS_ALIGNED(offset, align)); | |
f5a5ca79 | 583 | assert(QEMU_IS_ALIGNED(bytes, align)); |
63188c24 | 584 | if (bs->bl.max_pwrite_zeroes) { |
f5a5ca79 | 585 | assert(bytes <= bs->bl.max_pwrite_zeroes); |
63188c24 EB |
586 | } |
587 | ||
f5a5ca79 | 588 | err = rule_check(bs, offset, bytes); |
63188c24 EB |
589 | if (err) { |
590 | return err; | |
591 | } | |
592 | ||
f5a5ca79 | 593 | return bdrv_co_pwrite_zeroes(bs->file, offset, bytes, flags); |
63188c24 EB |
594 | } |
595 | ||
596 | static int coroutine_fn blkdebug_co_pdiscard(BlockDriverState *bs, | |
f5a5ca79 | 597 | int64_t offset, int bytes) |
63188c24 EB |
598 | { |
599 | uint32_t align = bs->bl.pdiscard_alignment; | |
600 | int err; | |
601 | ||
602 | /* Only pass through requests that are larger than requested | |
603 | * minimum alignment, and ensure that unaligned requests do not | |
604 | * cross optimum discard boundaries. */ | |
f5a5ca79 | 605 | if (bytes < bs->bl.request_alignment) { |
63188c24 | 606 | assert(QEMU_IS_ALIGNED(offset, align) || |
f5a5ca79 | 607 | QEMU_IS_ALIGNED(offset + bytes, align) || |
63188c24 | 608 | DIV_ROUND_UP(offset, align) == |
f5a5ca79 | 609 | DIV_ROUND_UP(offset + bytes, align)); |
63188c24 EB |
610 | return -ENOTSUP; |
611 | } | |
612 | assert(QEMU_IS_ALIGNED(offset, bs->bl.request_alignment)); | |
f5a5ca79 MP |
613 | assert(QEMU_IS_ALIGNED(bytes, bs->bl.request_alignment)); |
614 | if (align && bytes >= align) { | |
63188c24 | 615 | assert(QEMU_IS_ALIGNED(offset, align)); |
f5a5ca79 | 616 | assert(QEMU_IS_ALIGNED(bytes, align)); |
63188c24 EB |
617 | } |
618 | if (bs->bl.max_pdiscard) { | |
f5a5ca79 | 619 | assert(bytes <= bs->bl.max_pdiscard); |
63188c24 EB |
620 | } |
621 | ||
f5a5ca79 | 622 | err = rule_check(bs, offset, bytes); |
63188c24 EB |
623 | if (err) { |
624 | return err; | |
625 | } | |
626 | ||
f5a5ca79 | 627 | return bdrv_co_pdiscard(bs->file->bs, offset, bytes); |
63188c24 | 628 | } |
3c90c65d | 629 | |
3e4d0e72 EB |
630 | static int coroutine_fn blkdebug_co_block_status(BlockDriverState *bs, |
631 | bool want_zero, | |
632 | int64_t offset, | |
633 | int64_t bytes, | |
634 | int64_t *pnum, | |
635 | int64_t *map, | |
636 | BlockDriverState **file) | |
efa6e2ed | 637 | { |
3e4d0e72 EB |
638 | assert(QEMU_IS_ALIGNED(offset | bytes, bs->bl.request_alignment)); |
639 | return bdrv_co_block_status_from_file(bs, want_zero, offset, bytes, | |
640 | pnum, map, file); | |
efa6e2ed EB |
641 | } |
642 | ||
6a143727 KW |
643 | static void blkdebug_close(BlockDriverState *bs) |
644 | { | |
645 | BDRVBlkdebugState *s = bs->opaque; | |
8b9b0cc2 KW |
646 | BlkdebugRule *rule, *next; |
647 | int i; | |
648 | ||
7fb1cf16 | 649 | for (i = 0; i < BLKDBG__MAX; i++) { |
8b9b0cc2 | 650 | QLIST_FOREACH_SAFE(rule, &s->rules[i], next, next) { |
9e35542b | 651 | remove_rule(rule); |
8b9b0cc2 KW |
652 | } |
653 | } | |
036990d7 HR |
654 | |
655 | g_free(s->config_file); | |
6a143727 KW |
656 | } |
657 | ||
3c90c65d KW |
658 | static void suspend_request(BlockDriverState *bs, BlkdebugRule *rule) |
659 | { | |
660 | BDRVBlkdebugState *s = bs->opaque; | |
661 | BlkdebugSuspendedReq r; | |
662 | ||
663 | r = (BlkdebugSuspendedReq) { | |
664 | .co = qemu_coroutine_self(), | |
665 | .tag = g_strdup(rule->options.suspend.tag), | |
666 | }; | |
667 | ||
668 | remove_rule(rule); | |
669 | QLIST_INSERT_HEAD(&s->suspended_reqs, &r, next); | |
670 | ||
20873526 MT |
671 | if (!qtest_enabled()) { |
672 | printf("blkdebug: Suspended request '%s'\n", r.tag); | |
673 | } | |
3c90c65d | 674 | qemu_coroutine_yield(); |
20873526 MT |
675 | if (!qtest_enabled()) { |
676 | printf("blkdebug: Resuming request '%s'\n", r.tag); | |
677 | } | |
3c90c65d KW |
678 | |
679 | QLIST_REMOVE(&r, next); | |
680 | g_free(r.tag); | |
681 | } | |
682 | ||
571cd43e | 683 | static bool process_rule(BlockDriverState *bs, struct BlkdebugRule *rule, |
8f96b5be | 684 | bool injected) |
8b9b0cc2 KW |
685 | { |
686 | BDRVBlkdebugState *s = bs->opaque; | |
8b9b0cc2 KW |
687 | |
688 | /* Only process rules for the current state */ | |
8f96b5be | 689 | if (rule->state && rule->state != s->state) { |
571cd43e | 690 | return injected; |
8b9b0cc2 KW |
691 | } |
692 | ||
693 | /* Take the action */ | |
694 | switch (rule->action) { | |
695 | case ACTION_INJECT_ERROR: | |
571cd43e PB |
696 | if (!injected) { |
697 | QSIMPLEQ_INIT(&s->active_rules); | |
698 | injected = true; | |
699 | } | |
700 | QSIMPLEQ_INSERT_HEAD(&s->active_rules, rule, active_next); | |
8b9b0cc2 KW |
701 | break; |
702 | ||
703 | case ACTION_SET_STATE: | |
8f96b5be | 704 | s->new_state = rule->options.set_state.new_state; |
8b9b0cc2 | 705 | break; |
3c90c65d KW |
706 | |
707 | case ACTION_SUSPEND: | |
708 | suspend_request(bs, rule); | |
709 | break; | |
8b9b0cc2 | 710 | } |
571cd43e | 711 | return injected; |
8b9b0cc2 KW |
712 | } |
713 | ||
a31939e6 | 714 | static void blkdebug_debug_event(BlockDriverState *bs, BlkdebugEvent event) |
8b9b0cc2 KW |
715 | { |
716 | BDRVBlkdebugState *s = bs->opaque; | |
3c90c65d | 717 | struct BlkdebugRule *rule, *next; |
571cd43e | 718 | bool injected; |
8b9b0cc2 | 719 | |
7fb1cf16 | 720 | assert((int)event >= 0 && event < BLKDBG__MAX); |
8b9b0cc2 | 721 | |
571cd43e | 722 | injected = false; |
8f96b5be | 723 | s->new_state = s->state; |
3c90c65d | 724 | QLIST_FOREACH_SAFE(rule, &s->rules[event], next, next) { |
8f96b5be | 725 | injected = process_rule(bs, rule, injected); |
8b9b0cc2 | 726 | } |
8f96b5be | 727 | s->state = s->new_state; |
8b9b0cc2 KW |
728 | } |
729 | ||
3c90c65d KW |
730 | static int blkdebug_debug_breakpoint(BlockDriverState *bs, const char *event, |
731 | const char *tag) | |
732 | { | |
733 | BDRVBlkdebugState *s = bs->opaque; | |
734 | struct BlkdebugRule *rule; | |
f9509d15 | 735 | int blkdebug_event; |
3c90c65d | 736 | |
f7abe0ec | 737 | blkdebug_event = qapi_enum_parse(&BlkdebugEvent_lookup, event, -1, NULL); |
f9509d15 | 738 | if (blkdebug_event < 0) { |
3c90c65d KW |
739 | return -ENOENT; |
740 | } | |
741 | ||
3c90c65d KW |
742 | rule = g_malloc(sizeof(*rule)); |
743 | *rule = (struct BlkdebugRule) { | |
744 | .event = blkdebug_event, | |
745 | .action = ACTION_SUSPEND, | |
746 | .state = 0, | |
747 | .options.suspend.tag = g_strdup(tag), | |
748 | }; | |
749 | ||
750 | QLIST_INSERT_HEAD(&s->rules[blkdebug_event], rule, next); | |
751 | ||
752 | return 0; | |
753 | } | |
754 | ||
755 | static int blkdebug_debug_resume(BlockDriverState *bs, const char *tag) | |
756 | { | |
757 | BDRVBlkdebugState *s = bs->opaque; | |
c547e564 | 758 | BlkdebugSuspendedReq *r, *next; |
3c90c65d | 759 | |
c547e564 | 760 | QLIST_FOREACH_SAFE(r, &s->suspended_reqs, next, next) { |
3c90c65d | 761 | if (!strcmp(r->tag, tag)) { |
0b8b8753 | 762 | qemu_coroutine_enter(r->co); |
3c90c65d KW |
763 | return 0; |
764 | } | |
765 | } | |
766 | return -ENOENT; | |
767 | } | |
768 | ||
4cc70e93 FZ |
769 | static int blkdebug_debug_remove_breakpoint(BlockDriverState *bs, |
770 | const char *tag) | |
771 | { | |
772 | BDRVBlkdebugState *s = bs->opaque; | |
c547e564 | 773 | BlkdebugSuspendedReq *r, *r_next; |
4cc70e93 FZ |
774 | BlkdebugRule *rule, *next; |
775 | int i, ret = -ENOENT; | |
776 | ||
7fb1cf16 | 777 | for (i = 0; i < BLKDBG__MAX; i++) { |
4cc70e93 FZ |
778 | QLIST_FOREACH_SAFE(rule, &s->rules[i], next, next) { |
779 | if (rule->action == ACTION_SUSPEND && | |
780 | !strcmp(rule->options.suspend.tag, tag)) { | |
781 | remove_rule(rule); | |
782 | ret = 0; | |
783 | } | |
784 | } | |
785 | } | |
c547e564 | 786 | QLIST_FOREACH_SAFE(r, &s->suspended_reqs, next, r_next) { |
4cc70e93 | 787 | if (!strcmp(r->tag, tag)) { |
0b8b8753 | 788 | qemu_coroutine_enter(r->co); |
4cc70e93 FZ |
789 | ret = 0; |
790 | } | |
791 | } | |
792 | return ret; | |
793 | } | |
3c90c65d KW |
794 | |
795 | static bool blkdebug_debug_is_suspended(BlockDriverState *bs, const char *tag) | |
796 | { | |
797 | BDRVBlkdebugState *s = bs->opaque; | |
798 | BlkdebugSuspendedReq *r; | |
799 | ||
800 | QLIST_FOREACH(r, &s->suspended_reqs, next) { | |
801 | if (!strcmp(r->tag, tag)) { | |
802 | return true; | |
803 | } | |
804 | } | |
805 | return false; | |
806 | } | |
807 | ||
e1302255 PB |
808 | static int64_t blkdebug_getlength(BlockDriverState *bs) |
809 | { | |
9a4f4c31 | 810 | return bdrv_getlength(bs->file->bs); |
e1302255 PB |
811 | } |
812 | ||
4cdd01d3 | 813 | static void blkdebug_refresh_filename(BlockDriverState *bs, QDict *options) |
2c31b04c | 814 | { |
036990d7 | 815 | BDRVBlkdebugState *s = bs->opaque; |
2c31b04c | 816 | QDict *opts; |
8779441b HR |
817 | const QDictEntry *e; |
818 | bool force_json = false; | |
819 | ||
4cdd01d3 | 820 | for (e = qdict_first(options); e; e = qdict_next(options, e)) { |
8779441b | 821 | if (strcmp(qdict_entry_key(e), "config") && |
4cdd01d3 | 822 | strcmp(qdict_entry_key(e), "x-image")) |
8779441b HR |
823 | { |
824 | force_json = true; | |
825 | break; | |
826 | } | |
827 | } | |
2c31b04c | 828 | |
9a4f4c31 | 829 | if (force_json && !bs->file->bs->full_open_options) { |
2c31b04c HR |
830 | /* The config file cannot be recreated, so creating a plain filename |
831 | * is impossible */ | |
832 | return; | |
833 | } | |
834 | ||
9a4f4c31 | 835 | if (!force_json && bs->file->bs->exact_filename[0]) { |
de81d72d HR |
836 | int ret = snprintf(bs->exact_filename, sizeof(bs->exact_filename), |
837 | "blkdebug:%s:%s", s->config_file ?: "", | |
838 | bs->file->bs->exact_filename); | |
839 | if (ret >= sizeof(bs->exact_filename)) { | |
840 | /* An overflow makes the filename unusable, so do not report any */ | |
841 | bs->exact_filename[0] = 0; | |
842 | } | |
8779441b HR |
843 | } |
844 | ||
2c31b04c | 845 | opts = qdict_new(); |
46f5ac20 | 846 | qdict_put_str(opts, "driver", "blkdebug"); |
2c31b04c | 847 | |
9a4f4c31 | 848 | QINCREF(bs->file->bs->full_open_options); |
de6e7951 | 849 | qdict_put(opts, "image", bs->file->bs->full_open_options); |
2c31b04c | 850 | |
4cdd01d3 KW |
851 | for (e = qdict_first(options); e; e = qdict_next(options, e)) { |
852 | if (strcmp(qdict_entry_key(e), "x-image")) { | |
8779441b HR |
853 | qobject_incref(qdict_entry_value(e)); |
854 | qdict_put_obj(opts, qdict_entry_key(e), qdict_entry_value(e)); | |
2c31b04c HR |
855 | } |
856 | } | |
857 | ||
2c31b04c HR |
858 | bs->full_open_options = opts; |
859 | } | |
860 | ||
835db3ee EB |
861 | static void blkdebug_refresh_limits(BlockDriverState *bs, Error **errp) |
862 | { | |
863 | BDRVBlkdebugState *s = bs->opaque; | |
864 | ||
865 | if (s->align) { | |
a5b8dd2c | 866 | bs->bl.request_alignment = s->align; |
835db3ee | 867 | } |
430b26a8 EB |
868 | if (s->max_transfer) { |
869 | bs->bl.max_transfer = s->max_transfer; | |
870 | } | |
871 | if (s->opt_write_zero) { | |
872 | bs->bl.pwrite_zeroes_alignment = s->opt_write_zero; | |
873 | } | |
874 | if (s->max_write_zero) { | |
875 | bs->bl.max_pwrite_zeroes = s->max_write_zero; | |
876 | } | |
877 | if (s->opt_discard) { | |
878 | bs->bl.pdiscard_alignment = s->opt_discard; | |
879 | } | |
880 | if (s->max_discard) { | |
881 | bs->bl.max_pdiscard = s->max_discard; | |
882 | } | |
835db3ee EB |
883 | } |
884 | ||
c5e8bfb7 KW |
885 | static int blkdebug_reopen_prepare(BDRVReopenState *reopen_state, |
886 | BlockReopenQueue *queue, Error **errp) | |
887 | { | |
888 | return 0; | |
889 | } | |
890 | ||
6a143727 | 891 | static BlockDriver bdrv_blkdebug = { |
f4681212 KW |
892 | .format_name = "blkdebug", |
893 | .protocol_name = "blkdebug", | |
894 | .instance_size = sizeof(BDRVBlkdebugState), | |
d8e12cd3 | 895 | .is_filter = true, |
6a143727 | 896 | |
f4681212 KW |
897 | .bdrv_parse_filename = blkdebug_parse_filename, |
898 | .bdrv_file_open = blkdebug_open, | |
899 | .bdrv_close = blkdebug_close, | |
c5e8bfb7 | 900 | .bdrv_reopen_prepare = blkdebug_reopen_prepare, |
d7010dfb KW |
901 | .bdrv_child_perm = bdrv_filter_default_perms, |
902 | ||
f4681212 | 903 | .bdrv_getlength = blkdebug_getlength, |
2c31b04c | 904 | .bdrv_refresh_filename = blkdebug_refresh_filename, |
835db3ee | 905 | .bdrv_refresh_limits = blkdebug_refresh_limits, |
6a143727 | 906 | |
7c3a9985 KW |
907 | .bdrv_co_preadv = blkdebug_co_preadv, |
908 | .bdrv_co_pwritev = blkdebug_co_pwritev, | |
909 | .bdrv_co_flush_to_disk = blkdebug_co_flush, | |
63188c24 EB |
910 | .bdrv_co_pwrite_zeroes = blkdebug_co_pwrite_zeroes, |
911 | .bdrv_co_pdiscard = blkdebug_co_pdiscard, | |
3e4d0e72 | 912 | .bdrv_co_block_status = blkdebug_co_block_status, |
8b9b0cc2 | 913 | |
3c90c65d KW |
914 | .bdrv_debug_event = blkdebug_debug_event, |
915 | .bdrv_debug_breakpoint = blkdebug_debug_breakpoint, | |
4cc70e93 FZ |
916 | .bdrv_debug_remove_breakpoint |
917 | = blkdebug_debug_remove_breakpoint, | |
3c90c65d KW |
918 | .bdrv_debug_resume = blkdebug_debug_resume, |
919 | .bdrv_debug_is_suspended = blkdebug_debug_is_suspended, | |
6a143727 KW |
920 | }; |
921 | ||
922 | static void bdrv_blkdebug_init(void) | |
923 | { | |
924 | bdrv_register(&bdrv_blkdebug); | |
925 | } | |
926 | ||
927 | block_init(bdrv_blkdebug_init); |