]>
Commit | Line | Data |
---|---|---|
6a143727 KW |
1 | /* |
2 | * Block protocol for I/O error injection | |
3 | * | |
4 | * Copyright (c) 2010 Kevin Wolf <[email protected]> | |
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" |
6a143727 | 26 | #include "qemu-common.h" |
1de7afc9 | 27 | #include "qemu/config-file.h" |
737e150e | 28 | #include "block/block_int.h" |
1de7afc9 | 29 | #include "qemu/module.h" |
2c31b04c HR |
30 | #include "qapi/qmp/qbool.h" |
31 | #include "qapi/qmp/qdict.h" | |
32 | #include "qapi/qmp/qint.h" | |
33 | #include "qapi/qmp/qstring.h" | |
20873526 | 34 | #include "sysemu/qtest.h" |
6a143727 KW |
35 | |
36 | typedef struct BDRVBlkdebugState { | |
571cd43e | 37 | int state; |
8f96b5be | 38 | int new_state; |
3c90c65d | 39 | |
7fb1cf16 | 40 | QLIST_HEAD(, BlkdebugRule) rules[BLKDBG__MAX]; |
571cd43e | 41 | QSIMPLEQ_HEAD(, BlkdebugRule) active_rules; |
3c90c65d | 42 | QLIST_HEAD(, BlkdebugSuspendedReq) suspended_reqs; |
6a143727 KW |
43 | } BDRVBlkdebugState; |
44 | ||
b9f66d96 | 45 | typedef struct BlkdebugAIOCB { |
7c84b1b8 | 46 | BlockAIOCB common; |
b9f66d96 KW |
47 | QEMUBH *bh; |
48 | int ret; | |
49 | } BlkdebugAIOCB; | |
50 | ||
3c90c65d KW |
51 | typedef struct BlkdebugSuspendedReq { |
52 | Coroutine *co; | |
53 | char *tag; | |
54 | QLIST_ENTRY(BlkdebugSuspendedReq) next; | |
55 | } BlkdebugSuspendedReq; | |
56 | ||
d7331bed | 57 | static const AIOCBInfo blkdebug_aiocb_info = { |
4c781717 | 58 | .aiocb_size = sizeof(BlkdebugAIOCB), |
b9f66d96 KW |
59 | }; |
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; | |
e4780db4 | 76 | int64_t sector; |
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 KW |
172 | struct BlkdebugRule *rule; |
173 | ||
174 | /* Find the right event for the rule */ | |
175 | event_name = qemu_opt_get(opts, "event"); | |
d4362d64 | 176 | if (!event_name) { |
8809cfc3 | 177 | error_setg(errp, "Missing event name for rule"); |
d4362d64 SH |
178 | return -1; |
179 | } else if (get_event_by_name(event_name, &event) < 0) { | |
8809cfc3 | 180 | error_setg(errp, "Invalid event name \"%s\"", event_name); |
8b9b0cc2 KW |
181 | return -1; |
182 | } | |
183 | ||
184 | /* Set attributes common for all actions */ | |
7267c094 | 185 | rule = g_malloc0(sizeof(*rule)); |
8b9b0cc2 KW |
186 | *rule = (struct BlkdebugRule) { |
187 | .event = event, | |
188 | .action = d->action, | |
189 | .state = qemu_opt_get_number(opts, "state", 0), | |
190 | }; | |
191 | ||
192 | /* Parse action-specific options */ | |
193 | switch (d->action) { | |
194 | case ACTION_INJECT_ERROR: | |
195 | rule->options.inject.error = qemu_opt_get_number(opts, "errno", EIO); | |
196 | rule->options.inject.once = qemu_opt_get_bool(opts, "once", 0); | |
197 | rule->options.inject.immediately = | |
198 | qemu_opt_get_bool(opts, "immediately", 0); | |
e4780db4 | 199 | rule->options.inject.sector = qemu_opt_get_number(opts, "sector", -1); |
8b9b0cc2 KW |
200 | break; |
201 | ||
202 | case ACTION_SET_STATE: | |
203 | rule->options.set_state.new_state = | |
204 | qemu_opt_get_number(opts, "new_state", 0); | |
205 | break; | |
3c90c65d KW |
206 | |
207 | case ACTION_SUSPEND: | |
208 | rule->options.suspend.tag = | |
209 | g_strdup(qemu_opt_get(opts, "tag")); | |
210 | break; | |
8b9b0cc2 KW |
211 | }; |
212 | ||
213 | /* Add the rule */ | |
214 | QLIST_INSERT_HEAD(&s->rules[event], rule, next); | |
215 | ||
216 | return 0; | |
217 | } | |
218 | ||
9e35542b KW |
219 | static void remove_rule(BlkdebugRule *rule) |
220 | { | |
221 | switch (rule->action) { | |
222 | case ACTION_INJECT_ERROR: | |
223 | case ACTION_SET_STATE: | |
224 | break; | |
3c90c65d KW |
225 | case ACTION_SUSPEND: |
226 | g_free(rule->options.suspend.tag); | |
227 | break; | |
9e35542b KW |
228 | } |
229 | ||
230 | QLIST_REMOVE(rule, next); | |
231 | g_free(rule); | |
232 | } | |
233 | ||
89f2b21e HR |
234 | static int read_config(BDRVBlkdebugState *s, const char *filename, |
235 | QDict *options, Error **errp) | |
8b9b0cc2 | 236 | { |
85a040e5 | 237 | FILE *f = NULL; |
8b9b0cc2 KW |
238 | int ret; |
239 | struct add_rule_data d; | |
89f2b21e | 240 | Error *local_err = NULL; |
8b9b0cc2 | 241 | |
85a040e5 HR |
242 | if (filename) { |
243 | f = fopen(filename, "r"); | |
244 | if (f == NULL) { | |
245 | error_setg_errno(errp, errno, "Could not read blkdebug config file"); | |
246 | return -errno; | |
247 | } | |
8b9b0cc2 | 248 | |
85a040e5 HR |
249 | ret = qemu_config_parse(f, config_groups, filename); |
250 | if (ret < 0) { | |
251 | error_setg(errp, "Could not parse blkdebug config file"); | |
252 | ret = -EINVAL; | |
253 | goto fail; | |
254 | } | |
8b9b0cc2 KW |
255 | } |
256 | ||
89f2b21e | 257 | qemu_config_parse_qdict(options, config_groups, &local_err); |
84d18f06 | 258 | if (local_err) { |
89f2b21e HR |
259 | error_propagate(errp, local_err); |
260 | ret = -EINVAL; | |
261 | goto fail; | |
262 | } | |
263 | ||
8b9b0cc2 KW |
264 | d.s = s; |
265 | d.action = ACTION_INJECT_ERROR; | |
8809cfc3 | 266 | qemu_opts_foreach(&inject_error_opts, add_rule, &d, &local_err); |
d4362d64 SH |
267 | if (local_err) { |
268 | error_propagate(errp, local_err); | |
269 | ret = -EINVAL; | |
270 | goto fail; | |
271 | } | |
8b9b0cc2 KW |
272 | |
273 | d.action = ACTION_SET_STATE; | |
8809cfc3 | 274 | qemu_opts_foreach(&set_state_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 | ret = 0; | |
282 | fail: | |
698f0d52 KW |
283 | qemu_opts_reset(&inject_error_opts); |
284 | qemu_opts_reset(&set_state_opts); | |
85a040e5 HR |
285 | if (f) { |
286 | fclose(f); | |
287 | } | |
8b9b0cc2 KW |
288 | return ret; |
289 | } | |
290 | ||
291 | /* Valid blkdebug filenames look like blkdebug:path/to/config:path/to/image */ | |
f4681212 KW |
292 | static void blkdebug_parse_filename(const char *filename, QDict *options, |
293 | Error **errp) | |
6a143727 | 294 | { |
f4681212 | 295 | const char *c; |
6a143727 | 296 | |
8b9b0cc2 | 297 | /* Parse the blkdebug: prefix */ |
f4681212 | 298 | if (!strstart(filename, "blkdebug:", &filename)) { |
d4881b9b HR |
299 | /* There was no prefix; therefore, all options have to be already |
300 | present in the QDict (except for the filename) */ | |
301 | qdict_put(options, "x-image", qstring_from_str(filename)); | |
f4681212 | 302 | return; |
6a143727 | 303 | } |
6a143727 | 304 | |
f4681212 | 305 | /* Parse config file path */ |
8b9b0cc2 KW |
306 | c = strchr(filename, ':'); |
307 | if (c == NULL) { | |
f4681212 KW |
308 | error_setg(errp, "blkdebug requires both config file and image path"); |
309 | return; | |
8b9b0cc2 KW |
310 | } |
311 | ||
f4681212 KW |
312 | if (c != filename) { |
313 | QString *config_path; | |
314 | config_path = qstring_from_substr(filename, 0, c - filename - 1); | |
315 | qdict_put(options, "config", config_path); | |
8b9b0cc2 | 316 | } |
f4681212 KW |
317 | |
318 | /* TODO Allow multi-level nesting and set file.filename here */ | |
8b9b0cc2 | 319 | filename = c + 1; |
f4681212 KW |
320 | qdict_put(options, "x-image", qstring_from_str(filename)); |
321 | } | |
322 | ||
323 | static QemuOptsList runtime_opts = { | |
324 | .name = "blkdebug", | |
325 | .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head), | |
326 | .desc = { | |
327 | { | |
328 | .name = "config", | |
329 | .type = QEMU_OPT_STRING, | |
330 | .help = "Path to the configuration file", | |
331 | }, | |
332 | { | |
333 | .name = "x-image", | |
334 | .type = QEMU_OPT_STRING, | |
335 | .help = "[internal use only, will be removed]", | |
336 | }, | |
b35ee7fb KW |
337 | { |
338 | .name = "align", | |
339 | .type = QEMU_OPT_SIZE, | |
340 | .help = "Required alignment in bytes", | |
341 | }, | |
f4681212 KW |
342 | { /* end of list */ } |
343 | }, | |
344 | }; | |
345 | ||
015a1036 HR |
346 | static int blkdebug_open(BlockDriverState *bs, QDict *options, int flags, |
347 | Error **errp) | |
f4681212 KW |
348 | { |
349 | BDRVBlkdebugState *s = bs->opaque; | |
350 | QemuOpts *opts; | |
351 | Error *local_err = NULL; | |
4373593d | 352 | const char *config; |
b35ee7fb | 353 | uint64_t align; |
f4681212 KW |
354 | int ret; |
355 | ||
87ea75d5 | 356 | opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort); |
f4681212 | 357 | qemu_opts_absorb_qdict(opts, options, &local_err); |
84d18f06 | 358 | if (local_err) { |
10ffa72f | 359 | error_propagate(errp, local_err); |
f4681212 | 360 | ret = -EINVAL; |
eaf944a4 | 361 | goto out; |
f4681212 KW |
362 | } |
363 | ||
89f2b21e | 364 | /* Read rules from config file or command line options */ |
f4681212 | 365 | config = qemu_opt_get(opts, "config"); |
89f2b21e | 366 | ret = read_config(s, config, options, errp); |
85a040e5 | 367 | if (ret) { |
eaf944a4 | 368 | goto out; |
f4681212 | 369 | } |
8b9b0cc2 | 370 | |
8db520ce | 371 | /* Set initial state */ |
571cd43e | 372 | s->state = 1; |
8db520ce | 373 | |
6b826af7 | 374 | /* Open the image file */ |
9a4f4c31 KW |
375 | bs->file = bdrv_open_child(qemu_opt_get(opts, "x-image"), options, "image", |
376 | bs, &child_file, false, &local_err); | |
377 | if (local_err) { | |
378 | ret = -EINVAL; | |
10ffa72f | 379 | error_propagate(errp, local_err); |
eaf944a4 | 380 | goto out; |
8b9b0cc2 KW |
381 | } |
382 | ||
b35ee7fb KW |
383 | /* Set request alignment */ |
384 | align = qemu_opt_get_size(opts, "align", bs->request_alignment); | |
385 | if (align > 0 && align < INT_MAX && !(align & (align - 1))) { | |
386 | bs->request_alignment = align; | |
387 | } else { | |
388 | error_setg(errp, "Invalid alignment"); | |
389 | ret = -EINVAL; | |
eaf944a4 | 390 | goto fail_unref; |
b35ee7fb KW |
391 | } |
392 | ||
f4681212 | 393 | ret = 0; |
eaf944a4 KW |
394 | goto out; |
395 | ||
396 | fail_unref: | |
9a4f4c31 | 397 | bdrv_unref_child(bs, bs->file); |
eaf944a4 | 398 | out: |
f4681212 KW |
399 | qemu_opts_del(opts); |
400 | return ret; | |
6a143727 KW |
401 | } |
402 | ||
b9f66d96 KW |
403 | static void error_callback_bh(void *opaque) |
404 | { | |
405 | struct BlkdebugAIOCB *acb = opaque; | |
406 | qemu_bh_delete(acb->bh); | |
407 | acb->common.cb(acb->common.opaque, acb->ret); | |
8007429a | 408 | qemu_aio_unref(acb); |
b9f66d96 KW |
409 | } |
410 | ||
7c84b1b8 | 411 | static BlockAIOCB *inject_error(BlockDriverState *bs, |
097310b5 | 412 | BlockCompletionFunc *cb, void *opaque, BlkdebugRule *rule) |
b9f66d96 KW |
413 | { |
414 | BDRVBlkdebugState *s = bs->opaque; | |
571cd43e | 415 | int error = rule->options.inject.error; |
b9f66d96 KW |
416 | struct BlkdebugAIOCB *acb; |
417 | QEMUBH *bh; | |
a069e2f1 | 418 | bool immediately = rule->options.inject.immediately; |
b9f66d96 | 419 | |
571cd43e | 420 | if (rule->options.inject.once) { |
a069e2f1 JS |
421 | QSIMPLEQ_REMOVE(&s->active_rules, rule, BlkdebugRule, active_next); |
422 | remove_rule(rule); | |
b9f66d96 KW |
423 | } |
424 | ||
a069e2f1 | 425 | if (immediately) { |
b9f66d96 KW |
426 | return NULL; |
427 | } | |
428 | ||
d7331bed | 429 | acb = qemu_aio_get(&blkdebug_aiocb_info, bs, cb, opaque); |
b9f66d96 KW |
430 | acb->ret = -error; |
431 | ||
7e1efdf0 | 432 | bh = aio_bh_new(bdrv_get_aio_context(bs), error_callback_bh, acb); |
b9f66d96 KW |
433 | acb->bh = bh; |
434 | qemu_bh_schedule(bh); | |
435 | ||
b666d239 | 436 | return &acb->common; |
b9f66d96 KW |
437 | } |
438 | ||
7c84b1b8 | 439 | static BlockAIOCB *blkdebug_aio_readv(BlockDriverState *bs, |
6a143727 | 440 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, |
097310b5 | 441 | BlockCompletionFunc *cb, void *opaque) |
6a143727 KW |
442 | { |
443 | BDRVBlkdebugState *s = bs->opaque; | |
e4780db4 PB |
444 | BlkdebugRule *rule = NULL; |
445 | ||
446 | QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) { | |
447 | if (rule->options.inject.sector == -1 || | |
448 | (rule->options.inject.sector >= sector_num && | |
449 | rule->options.inject.sector < sector_num + nb_sectors)) { | |
450 | break; | |
451 | } | |
452 | } | |
b9f66d96 | 453 | |
571cd43e PB |
454 | if (rule && rule->options.inject.error) { |
455 | return inject_error(bs, cb, opaque, rule); | |
b9f66d96 KW |
456 | } |
457 | ||
9a4f4c31 KW |
458 | return bdrv_aio_readv(bs->file->bs, sector_num, qiov, nb_sectors, |
459 | cb, opaque); | |
6a143727 KW |
460 | } |
461 | ||
7c84b1b8 | 462 | static BlockAIOCB *blkdebug_aio_writev(BlockDriverState *bs, |
6a143727 | 463 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, |
097310b5 | 464 | BlockCompletionFunc *cb, void *opaque) |
6a143727 KW |
465 | { |
466 | BDRVBlkdebugState *s = bs->opaque; | |
e4780db4 PB |
467 | BlkdebugRule *rule = NULL; |
468 | ||
469 | QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) { | |
470 | if (rule->options.inject.sector == -1 || | |
471 | (rule->options.inject.sector >= sector_num && | |
472 | rule->options.inject.sector < sector_num + nb_sectors)) { | |
473 | break; | |
474 | } | |
475 | } | |
b9f66d96 | 476 | |
571cd43e PB |
477 | if (rule && rule->options.inject.error) { |
478 | return inject_error(bs, cb, opaque, rule); | |
b9f66d96 KW |
479 | } |
480 | ||
9a4f4c31 KW |
481 | return bdrv_aio_writev(bs->file->bs, sector_num, qiov, nb_sectors, |
482 | cb, opaque); | |
6a143727 KW |
483 | } |
484 | ||
7c84b1b8 | 485 | static BlockAIOCB *blkdebug_aio_flush(BlockDriverState *bs, |
097310b5 | 486 | BlockCompletionFunc *cb, void *opaque) |
9e52c53b PB |
487 | { |
488 | BDRVBlkdebugState *s = bs->opaque; | |
489 | BlkdebugRule *rule = NULL; | |
490 | ||
491 | QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) { | |
492 | if (rule->options.inject.sector == -1) { | |
493 | break; | |
494 | } | |
495 | } | |
496 | ||
497 | if (rule && rule->options.inject.error) { | |
498 | return inject_error(bs, cb, opaque, rule); | |
499 | } | |
500 | ||
9a4f4c31 | 501 | return bdrv_aio_flush(bs->file->bs, cb, opaque); |
9e52c53b PB |
502 | } |
503 | ||
3c90c65d | 504 | |
6a143727 KW |
505 | static void blkdebug_close(BlockDriverState *bs) |
506 | { | |
507 | BDRVBlkdebugState *s = bs->opaque; | |
8b9b0cc2 KW |
508 | BlkdebugRule *rule, *next; |
509 | int i; | |
510 | ||
7fb1cf16 | 511 | for (i = 0; i < BLKDBG__MAX; i++) { |
8b9b0cc2 | 512 | QLIST_FOREACH_SAFE(rule, &s->rules[i], next, next) { |
9e35542b | 513 | remove_rule(rule); |
8b9b0cc2 KW |
514 | } |
515 | } | |
6a143727 KW |
516 | } |
517 | ||
3c90c65d KW |
518 | static void suspend_request(BlockDriverState *bs, BlkdebugRule *rule) |
519 | { | |
520 | BDRVBlkdebugState *s = bs->opaque; | |
521 | BlkdebugSuspendedReq r; | |
522 | ||
523 | r = (BlkdebugSuspendedReq) { | |
524 | .co = qemu_coroutine_self(), | |
525 | .tag = g_strdup(rule->options.suspend.tag), | |
526 | }; | |
527 | ||
528 | remove_rule(rule); | |
529 | QLIST_INSERT_HEAD(&s->suspended_reqs, &r, next); | |
530 | ||
20873526 MT |
531 | if (!qtest_enabled()) { |
532 | printf("blkdebug: Suspended request '%s'\n", r.tag); | |
533 | } | |
3c90c65d | 534 | qemu_coroutine_yield(); |
20873526 MT |
535 | if (!qtest_enabled()) { |
536 | printf("blkdebug: Resuming request '%s'\n", r.tag); | |
537 | } | |
3c90c65d KW |
538 | |
539 | QLIST_REMOVE(&r, next); | |
540 | g_free(r.tag); | |
541 | } | |
542 | ||
571cd43e | 543 | static bool process_rule(BlockDriverState *bs, struct BlkdebugRule *rule, |
8f96b5be | 544 | bool injected) |
8b9b0cc2 KW |
545 | { |
546 | BDRVBlkdebugState *s = bs->opaque; | |
8b9b0cc2 KW |
547 | |
548 | /* Only process rules for the current state */ | |
8f96b5be | 549 | if (rule->state && rule->state != s->state) { |
571cd43e | 550 | return injected; |
8b9b0cc2 KW |
551 | } |
552 | ||
553 | /* Take the action */ | |
554 | switch (rule->action) { | |
555 | case ACTION_INJECT_ERROR: | |
571cd43e PB |
556 | if (!injected) { |
557 | QSIMPLEQ_INIT(&s->active_rules); | |
558 | injected = true; | |
559 | } | |
560 | QSIMPLEQ_INSERT_HEAD(&s->active_rules, rule, active_next); | |
8b9b0cc2 KW |
561 | break; |
562 | ||
563 | case ACTION_SET_STATE: | |
8f96b5be | 564 | s->new_state = rule->options.set_state.new_state; |
8b9b0cc2 | 565 | break; |
3c90c65d KW |
566 | |
567 | case ACTION_SUSPEND: | |
568 | suspend_request(bs, rule); | |
569 | break; | |
8b9b0cc2 | 570 | } |
571cd43e | 571 | return injected; |
8b9b0cc2 KW |
572 | } |
573 | ||
a31939e6 | 574 | static void blkdebug_debug_event(BlockDriverState *bs, BlkdebugEvent event) |
8b9b0cc2 KW |
575 | { |
576 | BDRVBlkdebugState *s = bs->opaque; | |
3c90c65d | 577 | struct BlkdebugRule *rule, *next; |
571cd43e | 578 | bool injected; |
8b9b0cc2 | 579 | |
7fb1cf16 | 580 | assert((int)event >= 0 && event < BLKDBG__MAX); |
8b9b0cc2 | 581 | |
571cd43e | 582 | injected = false; |
8f96b5be | 583 | s->new_state = s->state; |
3c90c65d | 584 | QLIST_FOREACH_SAFE(rule, &s->rules[event], next, next) { |
8f96b5be | 585 | injected = process_rule(bs, rule, injected); |
8b9b0cc2 | 586 | } |
8f96b5be | 587 | s->state = s->new_state; |
8b9b0cc2 KW |
588 | } |
589 | ||
3c90c65d KW |
590 | static int blkdebug_debug_breakpoint(BlockDriverState *bs, const char *event, |
591 | const char *tag) | |
592 | { | |
593 | BDRVBlkdebugState *s = bs->opaque; | |
594 | struct BlkdebugRule *rule; | |
a31939e6 | 595 | BlkdebugEvent blkdebug_event; |
3c90c65d KW |
596 | |
597 | if (get_event_by_name(event, &blkdebug_event) < 0) { | |
598 | return -ENOENT; | |
599 | } | |
600 | ||
601 | ||
602 | rule = g_malloc(sizeof(*rule)); | |
603 | *rule = (struct BlkdebugRule) { | |
604 | .event = blkdebug_event, | |
605 | .action = ACTION_SUSPEND, | |
606 | .state = 0, | |
607 | .options.suspend.tag = g_strdup(tag), | |
608 | }; | |
609 | ||
610 | QLIST_INSERT_HEAD(&s->rules[blkdebug_event], rule, next); | |
611 | ||
612 | return 0; | |
613 | } | |
614 | ||
615 | static int blkdebug_debug_resume(BlockDriverState *bs, const char *tag) | |
616 | { | |
617 | BDRVBlkdebugState *s = bs->opaque; | |
c547e564 | 618 | BlkdebugSuspendedReq *r, *next; |
3c90c65d | 619 | |
c547e564 | 620 | QLIST_FOREACH_SAFE(r, &s->suspended_reqs, next, next) { |
3c90c65d KW |
621 | if (!strcmp(r->tag, tag)) { |
622 | qemu_coroutine_enter(r->co, NULL); | |
623 | return 0; | |
624 | } | |
625 | } | |
626 | return -ENOENT; | |
627 | } | |
628 | ||
4cc70e93 FZ |
629 | static int blkdebug_debug_remove_breakpoint(BlockDriverState *bs, |
630 | const char *tag) | |
631 | { | |
632 | BDRVBlkdebugState *s = bs->opaque; | |
c547e564 | 633 | BlkdebugSuspendedReq *r, *r_next; |
4cc70e93 FZ |
634 | BlkdebugRule *rule, *next; |
635 | int i, ret = -ENOENT; | |
636 | ||
7fb1cf16 | 637 | for (i = 0; i < BLKDBG__MAX; i++) { |
4cc70e93 FZ |
638 | QLIST_FOREACH_SAFE(rule, &s->rules[i], next, next) { |
639 | if (rule->action == ACTION_SUSPEND && | |
640 | !strcmp(rule->options.suspend.tag, tag)) { | |
641 | remove_rule(rule); | |
642 | ret = 0; | |
643 | } | |
644 | } | |
645 | } | |
c547e564 | 646 | QLIST_FOREACH_SAFE(r, &s->suspended_reqs, next, r_next) { |
4cc70e93 FZ |
647 | if (!strcmp(r->tag, tag)) { |
648 | qemu_coroutine_enter(r->co, NULL); | |
649 | ret = 0; | |
650 | } | |
651 | } | |
652 | return ret; | |
653 | } | |
3c90c65d KW |
654 | |
655 | static bool blkdebug_debug_is_suspended(BlockDriverState *bs, const char *tag) | |
656 | { | |
657 | BDRVBlkdebugState *s = bs->opaque; | |
658 | BlkdebugSuspendedReq *r; | |
659 | ||
660 | QLIST_FOREACH(r, &s->suspended_reqs, next) { | |
661 | if (!strcmp(r->tag, tag)) { | |
662 | return true; | |
663 | } | |
664 | } | |
665 | return false; | |
666 | } | |
667 | ||
e1302255 PB |
668 | static int64_t blkdebug_getlength(BlockDriverState *bs) |
669 | { | |
9a4f4c31 | 670 | return bdrv_getlength(bs->file->bs); |
e1302255 PB |
671 | } |
672 | ||
8eedfbd4 KW |
673 | static int blkdebug_truncate(BlockDriverState *bs, int64_t offset) |
674 | { | |
9a4f4c31 | 675 | return bdrv_truncate(bs->file->bs, offset); |
8eedfbd4 KW |
676 | } |
677 | ||
4cdd01d3 | 678 | static void blkdebug_refresh_filename(BlockDriverState *bs, QDict *options) |
2c31b04c | 679 | { |
2c31b04c | 680 | QDict *opts; |
8779441b HR |
681 | const QDictEntry *e; |
682 | bool force_json = false; | |
683 | ||
4cdd01d3 | 684 | for (e = qdict_first(options); e; e = qdict_next(options, e)) { |
8779441b | 685 | if (strcmp(qdict_entry_key(e), "config") && |
4cdd01d3 | 686 | strcmp(qdict_entry_key(e), "x-image")) |
8779441b HR |
687 | { |
688 | force_json = true; | |
689 | break; | |
690 | } | |
691 | } | |
2c31b04c | 692 | |
9a4f4c31 | 693 | if (force_json && !bs->file->bs->full_open_options) { |
2c31b04c HR |
694 | /* The config file cannot be recreated, so creating a plain filename |
695 | * is impossible */ | |
696 | return; | |
697 | } | |
698 | ||
9a4f4c31 | 699 | if (!force_json && bs->file->bs->exact_filename[0]) { |
8779441b HR |
700 | snprintf(bs->exact_filename, sizeof(bs->exact_filename), |
701 | "blkdebug:%s:%s", | |
4cdd01d3 | 702 | qdict_get_try_str(options, "config") ?: "", |
9a4f4c31 | 703 | bs->file->bs->exact_filename); |
8779441b HR |
704 | } |
705 | ||
2c31b04c HR |
706 | opts = qdict_new(); |
707 | qdict_put_obj(opts, "driver", QOBJECT(qstring_from_str("blkdebug"))); | |
708 | ||
9a4f4c31 KW |
709 | QINCREF(bs->file->bs->full_open_options); |
710 | qdict_put_obj(opts, "image", QOBJECT(bs->file->bs->full_open_options)); | |
2c31b04c | 711 | |
4cdd01d3 KW |
712 | for (e = qdict_first(options); e; e = qdict_next(options, e)) { |
713 | if (strcmp(qdict_entry_key(e), "x-image")) { | |
8779441b HR |
714 | qobject_incref(qdict_entry_value(e)); |
715 | qdict_put_obj(opts, qdict_entry_key(e), qdict_entry_value(e)); | |
2c31b04c HR |
716 | } |
717 | } | |
718 | ||
2c31b04c HR |
719 | bs->full_open_options = opts; |
720 | } | |
721 | ||
c5e8bfb7 KW |
722 | static int blkdebug_reopen_prepare(BDRVReopenState *reopen_state, |
723 | BlockReopenQueue *queue, Error **errp) | |
724 | { | |
725 | return 0; | |
726 | } | |
727 | ||
6a143727 | 728 | static BlockDriver bdrv_blkdebug = { |
f4681212 KW |
729 | .format_name = "blkdebug", |
730 | .protocol_name = "blkdebug", | |
731 | .instance_size = sizeof(BDRVBlkdebugState), | |
6a143727 | 732 | |
f4681212 KW |
733 | .bdrv_parse_filename = blkdebug_parse_filename, |
734 | .bdrv_file_open = blkdebug_open, | |
735 | .bdrv_close = blkdebug_close, | |
c5e8bfb7 | 736 | .bdrv_reopen_prepare = blkdebug_reopen_prepare, |
f4681212 | 737 | .bdrv_getlength = blkdebug_getlength, |
8eedfbd4 | 738 | .bdrv_truncate = blkdebug_truncate, |
2c31b04c | 739 | .bdrv_refresh_filename = blkdebug_refresh_filename, |
6a143727 | 740 | |
f4681212 KW |
741 | .bdrv_aio_readv = blkdebug_aio_readv, |
742 | .bdrv_aio_writev = blkdebug_aio_writev, | |
9e52c53b | 743 | .bdrv_aio_flush = blkdebug_aio_flush, |
8b9b0cc2 | 744 | |
3c90c65d KW |
745 | .bdrv_debug_event = blkdebug_debug_event, |
746 | .bdrv_debug_breakpoint = blkdebug_debug_breakpoint, | |
4cc70e93 FZ |
747 | .bdrv_debug_remove_breakpoint |
748 | = blkdebug_debug_remove_breakpoint, | |
3c90c65d KW |
749 | .bdrv_debug_resume = blkdebug_debug_resume, |
750 | .bdrv_debug_is_suspended = blkdebug_debug_is_suspended, | |
6a143727 KW |
751 | }; |
752 | ||
753 | static void bdrv_blkdebug_init(void) | |
754 | { | |
755 | bdrv_register(&bdrv_blkdebug); | |
756 | } | |
757 | ||
758 | block_init(bdrv_blkdebug_init); |