]>
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 | ||
25 | #include "qemu-common.h" | |
1de7afc9 | 26 | #include "qemu/config-file.h" |
737e150e | 27 | #include "block/block_int.h" |
1de7afc9 | 28 | #include "qemu/module.h" |
6a143727 KW |
29 | |
30 | typedef struct BDRVBlkdebugState { | |
571cd43e | 31 | int state; |
8f96b5be | 32 | int new_state; |
3c90c65d | 33 | |
571cd43e PB |
34 | QLIST_HEAD(, BlkdebugRule) rules[BLKDBG_EVENT_MAX]; |
35 | QSIMPLEQ_HEAD(, BlkdebugRule) active_rules; | |
3c90c65d | 36 | QLIST_HEAD(, BlkdebugSuspendedReq) suspended_reqs; |
6a143727 KW |
37 | } BDRVBlkdebugState; |
38 | ||
b9f66d96 KW |
39 | typedef struct BlkdebugAIOCB { |
40 | BlockDriverAIOCB common; | |
41 | QEMUBH *bh; | |
42 | int ret; | |
43 | } BlkdebugAIOCB; | |
44 | ||
3c90c65d KW |
45 | typedef struct BlkdebugSuspendedReq { |
46 | Coroutine *co; | |
47 | char *tag; | |
48 | QLIST_ENTRY(BlkdebugSuspendedReq) next; | |
49 | } BlkdebugSuspendedReq; | |
50 | ||
b9f66d96 KW |
51 | static void blkdebug_aio_cancel(BlockDriverAIOCB *blockacb); |
52 | ||
d7331bed | 53 | static const AIOCBInfo blkdebug_aiocb_info = { |
b9f66d96 KW |
54 | .aiocb_size = sizeof(BlkdebugAIOCB), |
55 | .cancel = blkdebug_aio_cancel, | |
56 | }; | |
57 | ||
8b9b0cc2 KW |
58 | enum { |
59 | ACTION_INJECT_ERROR, | |
60 | ACTION_SET_STATE, | |
3c90c65d | 61 | ACTION_SUSPEND, |
8b9b0cc2 KW |
62 | }; |
63 | ||
64 | typedef struct BlkdebugRule { | |
65 | BlkDebugEvent event; | |
66 | int action; | |
67 | int state; | |
68 | union { | |
69 | struct { | |
70 | int error; | |
71 | int immediately; | |
72 | int once; | |
e4780db4 | 73 | int64_t sector; |
8b9b0cc2 KW |
74 | } inject; |
75 | struct { | |
76 | int new_state; | |
77 | } set_state; | |
3c90c65d KW |
78 | struct { |
79 | char *tag; | |
80 | } suspend; | |
8b9b0cc2 KW |
81 | } options; |
82 | QLIST_ENTRY(BlkdebugRule) next; | |
571cd43e | 83 | QSIMPLEQ_ENTRY(BlkdebugRule) active_next; |
8b9b0cc2 KW |
84 | } BlkdebugRule; |
85 | ||
86 | static QemuOptsList inject_error_opts = { | |
87 | .name = "inject-error", | |
88 | .head = QTAILQ_HEAD_INITIALIZER(inject_error_opts.head), | |
89 | .desc = { | |
90 | { | |
91 | .name = "event", | |
92 | .type = QEMU_OPT_STRING, | |
93 | }, | |
94 | { | |
95 | .name = "state", | |
96 | .type = QEMU_OPT_NUMBER, | |
97 | }, | |
98 | { | |
99 | .name = "errno", | |
100 | .type = QEMU_OPT_NUMBER, | |
101 | }, | |
e4780db4 PB |
102 | { |
103 | .name = "sector", | |
104 | .type = QEMU_OPT_NUMBER, | |
105 | }, | |
8b9b0cc2 KW |
106 | { |
107 | .name = "once", | |
108 | .type = QEMU_OPT_BOOL, | |
109 | }, | |
110 | { | |
111 | .name = "immediately", | |
112 | .type = QEMU_OPT_BOOL, | |
113 | }, | |
114 | { /* end of list */ } | |
115 | }, | |
116 | }; | |
117 | ||
118 | static QemuOptsList set_state_opts = { | |
119 | .name = "set-state", | |
327cdad4 | 120 | .head = QTAILQ_HEAD_INITIALIZER(set_state_opts.head), |
8b9b0cc2 KW |
121 | .desc = { |
122 | { | |
123 | .name = "event", | |
124 | .type = QEMU_OPT_STRING, | |
125 | }, | |
126 | { | |
127 | .name = "state", | |
128 | .type = QEMU_OPT_NUMBER, | |
129 | }, | |
130 | { | |
131 | .name = "new_state", | |
132 | .type = QEMU_OPT_NUMBER, | |
133 | }, | |
134 | { /* end of list */ } | |
135 | }, | |
136 | }; | |
137 | ||
138 | static QemuOptsList *config_groups[] = { | |
139 | &inject_error_opts, | |
140 | &set_state_opts, | |
141 | NULL | |
142 | }; | |
143 | ||
144 | static const char *event_names[BLKDBG_EVENT_MAX] = { | |
8252278a KW |
145 | [BLKDBG_L1_UPDATE] = "l1_update", |
146 | [BLKDBG_L1_GROW_ALLOC_TABLE] = "l1_grow.alloc_table", | |
147 | [BLKDBG_L1_GROW_WRITE_TABLE] = "l1_grow.write_table", | |
148 | [BLKDBG_L1_GROW_ACTIVATE_TABLE] = "l1_grow.activate_table", | |
149 | ||
150 | [BLKDBG_L2_LOAD] = "l2_load", | |
151 | [BLKDBG_L2_UPDATE] = "l2_update", | |
152 | [BLKDBG_L2_UPDATE_COMPRESSED] = "l2_update_compressed", | |
153 | [BLKDBG_L2_ALLOC_COW_READ] = "l2_alloc.cow_read", | |
154 | [BLKDBG_L2_ALLOC_WRITE] = "l2_alloc.write", | |
155 | ||
8252278a | 156 | [BLKDBG_READ_AIO] = "read_aio", |
8252278a KW |
157 | [BLKDBG_READ_BACKING_AIO] = "read_backing_aio", |
158 | [BLKDBG_READ_COMPRESSED] = "read_compressed", | |
159 | ||
160 | [BLKDBG_WRITE_AIO] = "write_aio", | |
161 | [BLKDBG_WRITE_COMPRESSED] = "write_compressed", | |
162 | ||
163 | [BLKDBG_VMSTATE_LOAD] = "vmstate_load", | |
164 | [BLKDBG_VMSTATE_SAVE] = "vmstate_save", | |
165 | ||
166 | [BLKDBG_COW_READ] = "cow_read", | |
167 | [BLKDBG_COW_WRITE] = "cow_write", | |
168 | ||
169 | [BLKDBG_REFTABLE_LOAD] = "reftable_load", | |
170 | [BLKDBG_REFTABLE_GROW] = "reftable_grow", | |
171 | ||
172 | [BLKDBG_REFBLOCK_LOAD] = "refblock_load", | |
173 | [BLKDBG_REFBLOCK_UPDATE] = "refblock_update", | |
174 | [BLKDBG_REFBLOCK_UPDATE_PART] = "refblock_update_part", | |
175 | [BLKDBG_REFBLOCK_ALLOC] = "refblock_alloc", | |
176 | [BLKDBG_REFBLOCK_ALLOC_HOOKUP] = "refblock_alloc.hookup", | |
177 | [BLKDBG_REFBLOCK_ALLOC_WRITE] = "refblock_alloc.write", | |
178 | [BLKDBG_REFBLOCK_ALLOC_WRITE_BLOCKS] = "refblock_alloc.write_blocks", | |
179 | [BLKDBG_REFBLOCK_ALLOC_WRITE_TABLE] = "refblock_alloc.write_table", | |
180 | [BLKDBG_REFBLOCK_ALLOC_SWITCH_TABLE] = "refblock_alloc.switch_table", | |
181 | ||
182 | [BLKDBG_CLUSTER_ALLOC] = "cluster_alloc", | |
183 | [BLKDBG_CLUSTER_ALLOC_BYTES] = "cluster_alloc_bytes", | |
184 | [BLKDBG_CLUSTER_FREE] = "cluster_free", | |
8b9b0cc2 KW |
185 | }; |
186 | ||
187 | static int get_event_by_name(const char *name, BlkDebugEvent *event) | |
188 | { | |
189 | int i; | |
190 | ||
191 | for (i = 0; i < BLKDBG_EVENT_MAX; i++) { | |
192 | if (!strcmp(event_names[i], name)) { | |
193 | *event = i; | |
194 | return 0; | |
195 | } | |
196 | } | |
197 | ||
198 | return -1; | |
199 | } | |
200 | ||
201 | struct add_rule_data { | |
202 | BDRVBlkdebugState *s; | |
203 | int action; | |
204 | }; | |
205 | ||
206 | static int add_rule(QemuOpts *opts, void *opaque) | |
207 | { | |
208 | struct add_rule_data *d = opaque; | |
209 | BDRVBlkdebugState *s = d->s; | |
210 | const char* event_name; | |
211 | BlkDebugEvent event; | |
212 | struct BlkdebugRule *rule; | |
213 | ||
214 | /* Find the right event for the rule */ | |
215 | event_name = qemu_opt_get(opts, "event"); | |
216 | if (!event_name || get_event_by_name(event_name, &event) < 0) { | |
217 | return -1; | |
218 | } | |
219 | ||
220 | /* Set attributes common for all actions */ | |
7267c094 | 221 | rule = g_malloc0(sizeof(*rule)); |
8b9b0cc2 KW |
222 | *rule = (struct BlkdebugRule) { |
223 | .event = event, | |
224 | .action = d->action, | |
225 | .state = qemu_opt_get_number(opts, "state", 0), | |
226 | }; | |
227 | ||
228 | /* Parse action-specific options */ | |
229 | switch (d->action) { | |
230 | case ACTION_INJECT_ERROR: | |
231 | rule->options.inject.error = qemu_opt_get_number(opts, "errno", EIO); | |
232 | rule->options.inject.once = qemu_opt_get_bool(opts, "once", 0); | |
233 | rule->options.inject.immediately = | |
234 | qemu_opt_get_bool(opts, "immediately", 0); | |
e4780db4 | 235 | rule->options.inject.sector = qemu_opt_get_number(opts, "sector", -1); |
8b9b0cc2 KW |
236 | break; |
237 | ||
238 | case ACTION_SET_STATE: | |
239 | rule->options.set_state.new_state = | |
240 | qemu_opt_get_number(opts, "new_state", 0); | |
241 | break; | |
3c90c65d KW |
242 | |
243 | case ACTION_SUSPEND: | |
244 | rule->options.suspend.tag = | |
245 | g_strdup(qemu_opt_get(opts, "tag")); | |
246 | break; | |
8b9b0cc2 KW |
247 | }; |
248 | ||
249 | /* Add the rule */ | |
250 | QLIST_INSERT_HEAD(&s->rules[event], rule, next); | |
251 | ||
252 | return 0; | |
253 | } | |
254 | ||
9e35542b KW |
255 | static void remove_rule(BlkdebugRule *rule) |
256 | { | |
257 | switch (rule->action) { | |
258 | case ACTION_INJECT_ERROR: | |
259 | case ACTION_SET_STATE: | |
260 | break; | |
3c90c65d KW |
261 | case ACTION_SUSPEND: |
262 | g_free(rule->options.suspend.tag); | |
263 | break; | |
9e35542b KW |
264 | } |
265 | ||
266 | QLIST_REMOVE(rule, next); | |
267 | g_free(rule); | |
268 | } | |
269 | ||
8b9b0cc2 KW |
270 | static int read_config(BDRVBlkdebugState *s, const char *filename) |
271 | { | |
272 | FILE *f; | |
273 | int ret; | |
274 | struct add_rule_data d; | |
275 | ||
276 | f = fopen(filename, "r"); | |
277 | if (f == NULL) { | |
278 | return -errno; | |
279 | } | |
280 | ||
281 | ret = qemu_config_parse(f, config_groups, filename); | |
282 | if (ret < 0) { | |
283 | goto fail; | |
284 | } | |
285 | ||
286 | d.s = s; | |
287 | d.action = ACTION_INJECT_ERROR; | |
288 | qemu_opts_foreach(&inject_error_opts, add_rule, &d, 0); | |
289 | ||
290 | d.action = ACTION_SET_STATE; | |
291 | qemu_opts_foreach(&set_state_opts, add_rule, &d, 0); | |
292 | ||
293 | ret = 0; | |
294 | fail: | |
698f0d52 KW |
295 | qemu_opts_reset(&inject_error_opts); |
296 | qemu_opts_reset(&set_state_opts); | |
8b9b0cc2 KW |
297 | fclose(f); |
298 | return ret; | |
299 | } | |
300 | ||
301 | /* Valid blkdebug filenames look like blkdebug:path/to/config:path/to/image */ | |
f4681212 KW |
302 | static void blkdebug_parse_filename(const char *filename, QDict *options, |
303 | Error **errp) | |
6a143727 | 304 | { |
f4681212 | 305 | const char *c; |
6a143727 | 306 | |
8b9b0cc2 | 307 | /* Parse the blkdebug: prefix */ |
f4681212 KW |
308 | if (!strstart(filename, "blkdebug:", &filename)) { |
309 | error_setg(errp, "File name string must start with 'blkdebug:'"); | |
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; |
f4681212 KW |
328 | qdict_put(options, "x-image", qstring_from_str(filename)); |
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 | }, | |
345 | { /* end of list */ } | |
346 | }, | |
347 | }; | |
348 | ||
56d1b4d2 | 349 | static int blkdebug_open(BlockDriverState *bs, QDict *options, int flags) |
f4681212 KW |
350 | { |
351 | BDRVBlkdebugState *s = bs->opaque; | |
352 | QemuOpts *opts; | |
353 | Error *local_err = NULL; | |
354 | const char *filename, *config; | |
355 | int ret; | |
356 | ||
357 | opts = qemu_opts_create_nofail(&runtime_opts); | |
358 | qemu_opts_absorb_qdict(opts, options, &local_err); | |
359 | if (error_is_set(&local_err)) { | |
360 | qerror_report_err(local_err); | |
361 | error_free(local_err); | |
362 | ret = -EINVAL; | |
363 | goto fail; | |
364 | } | |
365 | ||
366 | /* Read rules from config file */ | |
367 | config = qemu_opt_get(opts, "config"); | |
368 | if (config) { | |
369 | ret = read_config(s, config); | |
370 | if (ret < 0) { | |
371 | goto fail; | |
372 | } | |
373 | } | |
8b9b0cc2 | 374 | |
8db520ce | 375 | /* Set initial state */ |
571cd43e | 376 | s->state = 1; |
8db520ce | 377 | |
8b9b0cc2 | 378 | /* Open the backing file */ |
f4681212 KW |
379 | filename = qemu_opt_get(opts, "x-image"); |
380 | if (filename == NULL) { | |
381 | ret = -EINVAL; | |
382 | goto fail; | |
383 | } | |
384 | ||
787e4a85 | 385 | ret = bdrv_file_open(&bs->file, filename, NULL, flags); |
8b9b0cc2 | 386 | if (ret < 0) { |
f4681212 | 387 | goto fail; |
8b9b0cc2 KW |
388 | } |
389 | ||
f4681212 KW |
390 | ret = 0; |
391 | fail: | |
392 | qemu_opts_del(opts); | |
393 | return ret; | |
6a143727 KW |
394 | } |
395 | ||
b9f66d96 KW |
396 | static void error_callback_bh(void *opaque) |
397 | { | |
398 | struct BlkdebugAIOCB *acb = opaque; | |
399 | qemu_bh_delete(acb->bh); | |
400 | acb->common.cb(acb->common.opaque, acb->ret); | |
401 | qemu_aio_release(acb); | |
402 | } | |
403 | ||
404 | static void blkdebug_aio_cancel(BlockDriverAIOCB *blockacb) | |
405 | { | |
b666d239 | 406 | BlkdebugAIOCB *acb = container_of(blockacb, BlkdebugAIOCB, common); |
b9f66d96 KW |
407 | qemu_aio_release(acb); |
408 | } | |
409 | ||
410 | static BlockDriverAIOCB *inject_error(BlockDriverState *bs, | |
571cd43e | 411 | BlockDriverCompletionFunc *cb, void *opaque, BlkdebugRule *rule) |
b9f66d96 KW |
412 | { |
413 | BDRVBlkdebugState *s = bs->opaque; | |
571cd43e | 414 | int error = rule->options.inject.error; |
b9f66d96 KW |
415 | struct BlkdebugAIOCB *acb; |
416 | QEMUBH *bh; | |
417 | ||
571cd43e PB |
418 | if (rule->options.inject.once) { |
419 | QSIMPLEQ_INIT(&s->active_rules); | |
b9f66d96 KW |
420 | } |
421 | ||
571cd43e | 422 | if (rule->options.inject.immediately) { |
b9f66d96 KW |
423 | return NULL; |
424 | } | |
425 | ||
d7331bed | 426 | acb = qemu_aio_get(&blkdebug_aiocb_info, bs, cb, opaque); |
b9f66d96 KW |
427 | acb->ret = -error; |
428 | ||
429 | bh = qemu_bh_new(error_callback_bh, acb); | |
430 | acb->bh = bh; | |
431 | qemu_bh_schedule(bh); | |
432 | ||
b666d239 | 433 | return &acb->common; |
b9f66d96 KW |
434 | } |
435 | ||
6a143727 KW |
436 | static BlockDriverAIOCB *blkdebug_aio_readv(BlockDriverState *bs, |
437 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, | |
438 | BlockDriverCompletionFunc *cb, void *opaque) | |
439 | { | |
440 | BDRVBlkdebugState *s = bs->opaque; | |
e4780db4 PB |
441 | BlkdebugRule *rule = NULL; |
442 | ||
443 | QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) { | |
444 | if (rule->options.inject.sector == -1 || | |
445 | (rule->options.inject.sector >= sector_num && | |
446 | rule->options.inject.sector < sector_num + nb_sectors)) { | |
447 | break; | |
448 | } | |
449 | } | |
b9f66d96 | 450 | |
571cd43e PB |
451 | if (rule && rule->options.inject.error) { |
452 | return inject_error(bs, cb, opaque, rule); | |
b9f66d96 KW |
453 | } |
454 | ||
368e8dd1 | 455 | return bdrv_aio_readv(bs->file, sector_num, qiov, nb_sectors, cb, opaque); |
6a143727 KW |
456 | } |
457 | ||
458 | static BlockDriverAIOCB *blkdebug_aio_writev(BlockDriverState *bs, | |
459 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, | |
460 | BlockDriverCompletionFunc *cb, void *opaque) | |
461 | { | |
462 | BDRVBlkdebugState *s = bs->opaque; | |
e4780db4 PB |
463 | BlkdebugRule *rule = NULL; |
464 | ||
465 | QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) { | |
466 | if (rule->options.inject.sector == -1 || | |
467 | (rule->options.inject.sector >= sector_num && | |
468 | rule->options.inject.sector < sector_num + nb_sectors)) { | |
469 | break; | |
470 | } | |
471 | } | |
b9f66d96 | 472 | |
571cd43e PB |
473 | if (rule && rule->options.inject.error) { |
474 | return inject_error(bs, cb, opaque, rule); | |
b9f66d96 KW |
475 | } |
476 | ||
368e8dd1 | 477 | return bdrv_aio_writev(bs->file, sector_num, qiov, nb_sectors, cb, opaque); |
6a143727 KW |
478 | } |
479 | ||
3c90c65d | 480 | |
6a143727 KW |
481 | static void blkdebug_close(BlockDriverState *bs) |
482 | { | |
483 | BDRVBlkdebugState *s = bs->opaque; | |
8b9b0cc2 KW |
484 | BlkdebugRule *rule, *next; |
485 | int i; | |
486 | ||
487 | for (i = 0; i < BLKDBG_EVENT_MAX; i++) { | |
488 | QLIST_FOREACH_SAFE(rule, &s->rules[i], next, next) { | |
9e35542b | 489 | remove_rule(rule); |
8b9b0cc2 KW |
490 | } |
491 | } | |
6a143727 KW |
492 | } |
493 | ||
3c90c65d KW |
494 | static void suspend_request(BlockDriverState *bs, BlkdebugRule *rule) |
495 | { | |
496 | BDRVBlkdebugState *s = bs->opaque; | |
497 | BlkdebugSuspendedReq r; | |
498 | ||
499 | r = (BlkdebugSuspendedReq) { | |
500 | .co = qemu_coroutine_self(), | |
501 | .tag = g_strdup(rule->options.suspend.tag), | |
502 | }; | |
503 | ||
504 | remove_rule(rule); | |
505 | QLIST_INSERT_HEAD(&s->suspended_reqs, &r, next); | |
506 | ||
507 | printf("blkdebug: Suspended request '%s'\n", r.tag); | |
508 | qemu_coroutine_yield(); | |
509 | printf("blkdebug: Resuming request '%s'\n", r.tag); | |
510 | ||
511 | QLIST_REMOVE(&r, next); | |
512 | g_free(r.tag); | |
513 | } | |
514 | ||
571cd43e | 515 | static bool process_rule(BlockDriverState *bs, struct BlkdebugRule *rule, |
8f96b5be | 516 | bool injected) |
8b9b0cc2 KW |
517 | { |
518 | BDRVBlkdebugState *s = bs->opaque; | |
8b9b0cc2 KW |
519 | |
520 | /* Only process rules for the current state */ | |
8f96b5be | 521 | if (rule->state && rule->state != s->state) { |
571cd43e | 522 | return injected; |
8b9b0cc2 KW |
523 | } |
524 | ||
525 | /* Take the action */ | |
526 | switch (rule->action) { | |
527 | case ACTION_INJECT_ERROR: | |
571cd43e PB |
528 | if (!injected) { |
529 | QSIMPLEQ_INIT(&s->active_rules); | |
530 | injected = true; | |
531 | } | |
532 | QSIMPLEQ_INSERT_HEAD(&s->active_rules, rule, active_next); | |
8b9b0cc2 KW |
533 | break; |
534 | ||
535 | case ACTION_SET_STATE: | |
8f96b5be | 536 | s->new_state = rule->options.set_state.new_state; |
8b9b0cc2 | 537 | break; |
3c90c65d KW |
538 | |
539 | case ACTION_SUSPEND: | |
540 | suspend_request(bs, rule); | |
541 | break; | |
8b9b0cc2 | 542 | } |
571cd43e | 543 | return injected; |
8b9b0cc2 KW |
544 | } |
545 | ||
546 | static void blkdebug_debug_event(BlockDriverState *bs, BlkDebugEvent event) | |
547 | { | |
548 | BDRVBlkdebugState *s = bs->opaque; | |
3c90c65d | 549 | struct BlkdebugRule *rule, *next; |
571cd43e | 550 | bool injected; |
8b9b0cc2 | 551 | |
95ee3914 | 552 | assert((int)event >= 0 && event < BLKDBG_EVENT_MAX); |
8b9b0cc2 | 553 | |
571cd43e | 554 | injected = false; |
8f96b5be | 555 | s->new_state = s->state; |
3c90c65d | 556 | QLIST_FOREACH_SAFE(rule, &s->rules[event], next, next) { |
8f96b5be | 557 | injected = process_rule(bs, rule, injected); |
8b9b0cc2 | 558 | } |
8f96b5be | 559 | s->state = s->new_state; |
8b9b0cc2 KW |
560 | } |
561 | ||
3c90c65d KW |
562 | static int blkdebug_debug_breakpoint(BlockDriverState *bs, const char *event, |
563 | const char *tag) | |
564 | { | |
565 | BDRVBlkdebugState *s = bs->opaque; | |
566 | struct BlkdebugRule *rule; | |
567 | BlkDebugEvent blkdebug_event; | |
568 | ||
569 | if (get_event_by_name(event, &blkdebug_event) < 0) { | |
570 | return -ENOENT; | |
571 | } | |
572 | ||
573 | ||
574 | rule = g_malloc(sizeof(*rule)); | |
575 | *rule = (struct BlkdebugRule) { | |
576 | .event = blkdebug_event, | |
577 | .action = ACTION_SUSPEND, | |
578 | .state = 0, | |
579 | .options.suspend.tag = g_strdup(tag), | |
580 | }; | |
581 | ||
582 | QLIST_INSERT_HEAD(&s->rules[blkdebug_event], rule, next); | |
583 | ||
584 | return 0; | |
585 | } | |
586 | ||
587 | static int blkdebug_debug_resume(BlockDriverState *bs, const char *tag) | |
588 | { | |
589 | BDRVBlkdebugState *s = bs->opaque; | |
590 | BlkdebugSuspendedReq *r; | |
591 | ||
592 | QLIST_FOREACH(r, &s->suspended_reqs, next) { | |
593 | if (!strcmp(r->tag, tag)) { | |
594 | qemu_coroutine_enter(r->co, NULL); | |
595 | return 0; | |
596 | } | |
597 | } | |
598 | return -ENOENT; | |
599 | } | |
600 | ||
601 | ||
602 | static bool blkdebug_debug_is_suspended(BlockDriverState *bs, const char *tag) | |
603 | { | |
604 | BDRVBlkdebugState *s = bs->opaque; | |
605 | BlkdebugSuspendedReq *r; | |
606 | ||
607 | QLIST_FOREACH(r, &s->suspended_reqs, next) { | |
608 | if (!strcmp(r->tag, tag)) { | |
609 | return true; | |
610 | } | |
611 | } | |
612 | return false; | |
613 | } | |
614 | ||
e1302255 PB |
615 | static int64_t blkdebug_getlength(BlockDriverState *bs) |
616 | { | |
617 | return bdrv_getlength(bs->file); | |
618 | } | |
619 | ||
6a143727 | 620 | static BlockDriver bdrv_blkdebug = { |
f4681212 KW |
621 | .format_name = "blkdebug", |
622 | .protocol_name = "blkdebug", | |
623 | .instance_size = sizeof(BDRVBlkdebugState), | |
6a143727 | 624 | |
f4681212 KW |
625 | .bdrv_parse_filename = blkdebug_parse_filename, |
626 | .bdrv_file_open = blkdebug_open, | |
627 | .bdrv_close = blkdebug_close, | |
628 | .bdrv_getlength = blkdebug_getlength, | |
6a143727 | 629 | |
f4681212 KW |
630 | .bdrv_aio_readv = blkdebug_aio_readv, |
631 | .bdrv_aio_writev = blkdebug_aio_writev, | |
8b9b0cc2 | 632 | |
3c90c65d KW |
633 | .bdrv_debug_event = blkdebug_debug_event, |
634 | .bdrv_debug_breakpoint = blkdebug_debug_breakpoint, | |
635 | .bdrv_debug_resume = blkdebug_debug_resume, | |
636 | .bdrv_debug_is_suspended = blkdebug_debug_is_suspended, | |
6a143727 KW |
637 | }; |
638 | ||
639 | static void bdrv_blkdebug_init(void) | |
640 | { | |
641 | bdrv_register(&bdrv_blkdebug); | |
642 | } | |
643 | ||
644 | block_init(bdrv_blkdebug_init); |