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