]>
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 | ||
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", | |
afa50193 | 171 | [BLKDBG_REFTABLE_UPDATE] = "reftable_update", |
8252278a KW |
172 | |
173 | [BLKDBG_REFBLOCK_LOAD] = "refblock_load", | |
174 | [BLKDBG_REFBLOCK_UPDATE] = "refblock_update", | |
175 | [BLKDBG_REFBLOCK_UPDATE_PART] = "refblock_update_part", | |
176 | [BLKDBG_REFBLOCK_ALLOC] = "refblock_alloc", | |
177 | [BLKDBG_REFBLOCK_ALLOC_HOOKUP] = "refblock_alloc.hookup", | |
178 | [BLKDBG_REFBLOCK_ALLOC_WRITE] = "refblock_alloc.write", | |
179 | [BLKDBG_REFBLOCK_ALLOC_WRITE_BLOCKS] = "refblock_alloc.write_blocks", | |
180 | [BLKDBG_REFBLOCK_ALLOC_WRITE_TABLE] = "refblock_alloc.write_table", | |
181 | [BLKDBG_REFBLOCK_ALLOC_SWITCH_TABLE] = "refblock_alloc.switch_table", | |
182 | ||
183 | [BLKDBG_CLUSTER_ALLOC] = "cluster_alloc", | |
184 | [BLKDBG_CLUSTER_ALLOC_BYTES] = "cluster_alloc_bytes", | |
185 | [BLKDBG_CLUSTER_FREE] = "cluster_free", | |
bf736fe3 KW |
186 | |
187 | [BLKDBG_FLUSH_TO_OS] = "flush_to_os", | |
188 | [BLKDBG_FLUSH_TO_DISK] = "flush_to_disk", | |
8b9b0cc2 KW |
189 | }; |
190 | ||
191 | static int get_event_by_name(const char *name, BlkDebugEvent *event) | |
192 | { | |
193 | int i; | |
194 | ||
195 | for (i = 0; i < BLKDBG_EVENT_MAX; i++) { | |
196 | if (!strcmp(event_names[i], name)) { | |
197 | *event = i; | |
198 | return 0; | |
199 | } | |
200 | } | |
201 | ||
202 | return -1; | |
203 | } | |
204 | ||
205 | struct add_rule_data { | |
206 | BDRVBlkdebugState *s; | |
207 | int action; | |
208 | }; | |
209 | ||
210 | static int add_rule(QemuOpts *opts, void *opaque) | |
211 | { | |
212 | struct add_rule_data *d = opaque; | |
213 | BDRVBlkdebugState *s = d->s; | |
214 | const char* event_name; | |
215 | BlkDebugEvent event; | |
216 | struct BlkdebugRule *rule; | |
217 | ||
218 | /* Find the right event for the rule */ | |
219 | event_name = qemu_opt_get(opts, "event"); | |
220 | if (!event_name || get_event_by_name(event_name, &event) < 0) { | |
221 | return -1; | |
222 | } | |
223 | ||
224 | /* Set attributes common for all actions */ | |
7267c094 | 225 | rule = g_malloc0(sizeof(*rule)); |
8b9b0cc2 KW |
226 | *rule = (struct BlkdebugRule) { |
227 | .event = event, | |
228 | .action = d->action, | |
229 | .state = qemu_opt_get_number(opts, "state", 0), | |
230 | }; | |
231 | ||
232 | /* Parse action-specific options */ | |
233 | switch (d->action) { | |
234 | case ACTION_INJECT_ERROR: | |
235 | rule->options.inject.error = qemu_opt_get_number(opts, "errno", EIO); | |
236 | rule->options.inject.once = qemu_opt_get_bool(opts, "once", 0); | |
237 | rule->options.inject.immediately = | |
238 | qemu_opt_get_bool(opts, "immediately", 0); | |
e4780db4 | 239 | rule->options.inject.sector = qemu_opt_get_number(opts, "sector", -1); |
8b9b0cc2 KW |
240 | break; |
241 | ||
242 | case ACTION_SET_STATE: | |
243 | rule->options.set_state.new_state = | |
244 | qemu_opt_get_number(opts, "new_state", 0); | |
245 | break; | |
3c90c65d KW |
246 | |
247 | case ACTION_SUSPEND: | |
248 | rule->options.suspend.tag = | |
249 | g_strdup(qemu_opt_get(opts, "tag")); | |
250 | break; | |
8b9b0cc2 KW |
251 | }; |
252 | ||
253 | /* Add the rule */ | |
254 | QLIST_INSERT_HEAD(&s->rules[event], rule, next); | |
255 | ||
256 | return 0; | |
257 | } | |
258 | ||
9e35542b KW |
259 | static void remove_rule(BlkdebugRule *rule) |
260 | { | |
261 | switch (rule->action) { | |
262 | case ACTION_INJECT_ERROR: | |
263 | case ACTION_SET_STATE: | |
264 | break; | |
3c90c65d KW |
265 | case ACTION_SUSPEND: |
266 | g_free(rule->options.suspend.tag); | |
267 | break; | |
9e35542b KW |
268 | } |
269 | ||
270 | QLIST_REMOVE(rule, next); | |
271 | g_free(rule); | |
272 | } | |
273 | ||
89f2b21e HR |
274 | static int read_config(BDRVBlkdebugState *s, const char *filename, |
275 | QDict *options, Error **errp) | |
8b9b0cc2 | 276 | { |
85a040e5 | 277 | FILE *f = NULL; |
8b9b0cc2 KW |
278 | int ret; |
279 | struct add_rule_data d; | |
89f2b21e | 280 | Error *local_err = NULL; |
8b9b0cc2 | 281 | |
85a040e5 HR |
282 | if (filename) { |
283 | f = fopen(filename, "r"); | |
284 | if (f == NULL) { | |
285 | error_setg_errno(errp, errno, "Could not read blkdebug config file"); | |
286 | return -errno; | |
287 | } | |
8b9b0cc2 | 288 | |
85a040e5 HR |
289 | ret = qemu_config_parse(f, config_groups, filename); |
290 | if (ret < 0) { | |
291 | error_setg(errp, "Could not parse blkdebug config file"); | |
292 | ret = -EINVAL; | |
293 | goto fail; | |
294 | } | |
8b9b0cc2 KW |
295 | } |
296 | ||
89f2b21e HR |
297 | qemu_config_parse_qdict(options, config_groups, &local_err); |
298 | if (error_is_set(&local_err)) { | |
299 | error_propagate(errp, local_err); | |
300 | ret = -EINVAL; | |
301 | goto fail; | |
302 | } | |
303 | ||
8b9b0cc2 KW |
304 | d.s = s; |
305 | d.action = ACTION_INJECT_ERROR; | |
306 | qemu_opts_foreach(&inject_error_opts, add_rule, &d, 0); | |
307 | ||
308 | d.action = ACTION_SET_STATE; | |
309 | qemu_opts_foreach(&set_state_opts, add_rule, &d, 0); | |
310 | ||
311 | ret = 0; | |
312 | fail: | |
698f0d52 KW |
313 | qemu_opts_reset(&inject_error_opts); |
314 | qemu_opts_reset(&set_state_opts); | |
85a040e5 HR |
315 | if (f) { |
316 | fclose(f); | |
317 | } | |
8b9b0cc2 KW |
318 | return ret; |
319 | } | |
320 | ||
321 | /* Valid blkdebug filenames look like blkdebug:path/to/config:path/to/image */ | |
f4681212 KW |
322 | static void blkdebug_parse_filename(const char *filename, QDict *options, |
323 | Error **errp) | |
6a143727 | 324 | { |
f4681212 | 325 | const char *c; |
6a143727 | 326 | |
8b9b0cc2 | 327 | /* Parse the blkdebug: prefix */ |
f4681212 | 328 | if (!strstart(filename, "blkdebug:", &filename)) { |
d4881b9b HR |
329 | /* There was no prefix; therefore, all options have to be already |
330 | present in the QDict (except for the filename) */ | |
331 | qdict_put(options, "x-image", qstring_from_str(filename)); | |
f4681212 | 332 | return; |
6a143727 | 333 | } |
6a143727 | 334 | |
f4681212 | 335 | /* Parse config file path */ |
8b9b0cc2 KW |
336 | c = strchr(filename, ':'); |
337 | if (c == NULL) { | |
f4681212 KW |
338 | error_setg(errp, "blkdebug requires both config file and image path"); |
339 | return; | |
8b9b0cc2 KW |
340 | } |
341 | ||
f4681212 KW |
342 | if (c != filename) { |
343 | QString *config_path; | |
344 | config_path = qstring_from_substr(filename, 0, c - filename - 1); | |
345 | qdict_put(options, "config", config_path); | |
8b9b0cc2 | 346 | } |
f4681212 KW |
347 | |
348 | /* TODO Allow multi-level nesting and set file.filename here */ | |
8b9b0cc2 | 349 | filename = c + 1; |
f4681212 KW |
350 | qdict_put(options, "x-image", qstring_from_str(filename)); |
351 | } | |
352 | ||
353 | static QemuOptsList runtime_opts = { | |
354 | .name = "blkdebug", | |
355 | .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head), | |
356 | .desc = { | |
357 | { | |
358 | .name = "config", | |
359 | .type = QEMU_OPT_STRING, | |
360 | .help = "Path to the configuration file", | |
361 | }, | |
362 | { | |
363 | .name = "x-image", | |
364 | .type = QEMU_OPT_STRING, | |
365 | .help = "[internal use only, will be removed]", | |
366 | }, | |
367 | { /* end of list */ } | |
368 | }, | |
369 | }; | |
370 | ||
015a1036 HR |
371 | static int blkdebug_open(BlockDriverState *bs, QDict *options, int flags, |
372 | Error **errp) | |
f4681212 KW |
373 | { |
374 | BDRVBlkdebugState *s = bs->opaque; | |
375 | QemuOpts *opts; | |
376 | Error *local_err = NULL; | |
4373593d | 377 | const char *config; |
f4681212 KW |
378 | int ret; |
379 | ||
87ea75d5 | 380 | opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort); |
f4681212 KW |
381 | qemu_opts_absorb_qdict(opts, options, &local_err); |
382 | if (error_is_set(&local_err)) { | |
10ffa72f | 383 | error_propagate(errp, local_err); |
f4681212 KW |
384 | ret = -EINVAL; |
385 | goto fail; | |
386 | } | |
387 | ||
89f2b21e | 388 | /* Read rules from config file or command line options */ |
f4681212 | 389 | config = qemu_opt_get(opts, "config"); |
89f2b21e | 390 | ret = read_config(s, config, options, errp); |
85a040e5 HR |
391 | if (ret) { |
392 | goto fail; | |
f4681212 | 393 | } |
8b9b0cc2 | 394 | |
8db520ce | 395 | /* Set initial state */ |
571cd43e | 396 | s->state = 1; |
8db520ce | 397 | |
8b9b0cc2 | 398 | /* Open the backing file */ |
4373593d HR |
399 | ret = bdrv_open_image(&bs->file, qemu_opt_get(opts, "x-image"), options, "image", |
400 | flags, true, false, &local_err); | |
8b9b0cc2 | 401 | if (ret < 0) { |
10ffa72f | 402 | error_propagate(errp, local_err); |
f4681212 | 403 | goto fail; |
8b9b0cc2 KW |
404 | } |
405 | ||
f4681212 KW |
406 | ret = 0; |
407 | fail: | |
408 | qemu_opts_del(opts); | |
409 | return ret; | |
6a143727 KW |
410 | } |
411 | ||
b9f66d96 KW |
412 | static void error_callback_bh(void *opaque) |
413 | { | |
414 | struct BlkdebugAIOCB *acb = opaque; | |
415 | qemu_bh_delete(acb->bh); | |
416 | acb->common.cb(acb->common.opaque, acb->ret); | |
417 | qemu_aio_release(acb); | |
418 | } | |
419 | ||
420 | static void blkdebug_aio_cancel(BlockDriverAIOCB *blockacb) | |
421 | { | |
b666d239 | 422 | BlkdebugAIOCB *acb = container_of(blockacb, BlkdebugAIOCB, common); |
b9f66d96 KW |
423 | qemu_aio_release(acb); |
424 | } | |
425 | ||
426 | static BlockDriverAIOCB *inject_error(BlockDriverState *bs, | |
571cd43e | 427 | BlockDriverCompletionFunc *cb, void *opaque, BlkdebugRule *rule) |
b9f66d96 KW |
428 | { |
429 | BDRVBlkdebugState *s = bs->opaque; | |
571cd43e | 430 | int error = rule->options.inject.error; |
b9f66d96 KW |
431 | struct BlkdebugAIOCB *acb; |
432 | QEMUBH *bh; | |
433 | ||
571cd43e PB |
434 | if (rule->options.inject.once) { |
435 | QSIMPLEQ_INIT(&s->active_rules); | |
b9f66d96 KW |
436 | } |
437 | ||
571cd43e | 438 | if (rule->options.inject.immediately) { |
b9f66d96 KW |
439 | return NULL; |
440 | } | |
441 | ||
d7331bed | 442 | acb = qemu_aio_get(&blkdebug_aiocb_info, bs, cb, opaque); |
b9f66d96 KW |
443 | acb->ret = -error; |
444 | ||
445 | bh = qemu_bh_new(error_callback_bh, acb); | |
446 | acb->bh = bh; | |
447 | qemu_bh_schedule(bh); | |
448 | ||
b666d239 | 449 | return &acb->common; |
b9f66d96 KW |
450 | } |
451 | ||
6a143727 KW |
452 | static BlockDriverAIOCB *blkdebug_aio_readv(BlockDriverState *bs, |
453 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, | |
454 | BlockDriverCompletionFunc *cb, void *opaque) | |
455 | { | |
456 | BDRVBlkdebugState *s = bs->opaque; | |
e4780db4 PB |
457 | BlkdebugRule *rule = NULL; |
458 | ||
459 | QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) { | |
460 | if (rule->options.inject.sector == -1 || | |
461 | (rule->options.inject.sector >= sector_num && | |
462 | rule->options.inject.sector < sector_num + nb_sectors)) { | |
463 | break; | |
464 | } | |
465 | } | |
b9f66d96 | 466 | |
571cd43e PB |
467 | if (rule && rule->options.inject.error) { |
468 | return inject_error(bs, cb, opaque, rule); | |
b9f66d96 KW |
469 | } |
470 | ||
368e8dd1 | 471 | return bdrv_aio_readv(bs->file, sector_num, qiov, nb_sectors, cb, opaque); |
6a143727 KW |
472 | } |
473 | ||
474 | static BlockDriverAIOCB *blkdebug_aio_writev(BlockDriverState *bs, | |
475 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, | |
476 | BlockDriverCompletionFunc *cb, void *opaque) | |
477 | { | |
478 | BDRVBlkdebugState *s = bs->opaque; | |
e4780db4 PB |
479 | BlkdebugRule *rule = NULL; |
480 | ||
481 | QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) { | |
482 | if (rule->options.inject.sector == -1 || | |
483 | (rule->options.inject.sector >= sector_num && | |
484 | rule->options.inject.sector < sector_num + nb_sectors)) { | |
485 | break; | |
486 | } | |
487 | } | |
b9f66d96 | 488 | |
571cd43e PB |
489 | if (rule && rule->options.inject.error) { |
490 | return inject_error(bs, cb, opaque, rule); | |
b9f66d96 KW |
491 | } |
492 | ||
368e8dd1 | 493 | return bdrv_aio_writev(bs->file, sector_num, qiov, nb_sectors, cb, opaque); |
6a143727 KW |
494 | } |
495 | ||
3c90c65d | 496 | |
6a143727 KW |
497 | static void blkdebug_close(BlockDriverState *bs) |
498 | { | |
499 | BDRVBlkdebugState *s = bs->opaque; | |
8b9b0cc2 KW |
500 | BlkdebugRule *rule, *next; |
501 | int i; | |
502 | ||
503 | for (i = 0; i < BLKDBG_EVENT_MAX; i++) { | |
504 | QLIST_FOREACH_SAFE(rule, &s->rules[i], next, next) { | |
9e35542b | 505 | remove_rule(rule); |
8b9b0cc2 KW |
506 | } |
507 | } | |
6a143727 KW |
508 | } |
509 | ||
3c90c65d KW |
510 | static void suspend_request(BlockDriverState *bs, BlkdebugRule *rule) |
511 | { | |
512 | BDRVBlkdebugState *s = bs->opaque; | |
513 | BlkdebugSuspendedReq r; | |
514 | ||
515 | r = (BlkdebugSuspendedReq) { | |
516 | .co = qemu_coroutine_self(), | |
517 | .tag = g_strdup(rule->options.suspend.tag), | |
518 | }; | |
519 | ||
520 | remove_rule(rule); | |
521 | QLIST_INSERT_HEAD(&s->suspended_reqs, &r, next); | |
522 | ||
523 | printf("blkdebug: Suspended request '%s'\n", r.tag); | |
524 | qemu_coroutine_yield(); | |
525 | printf("blkdebug: Resuming request '%s'\n", r.tag); | |
526 | ||
527 | QLIST_REMOVE(&r, next); | |
528 | g_free(r.tag); | |
529 | } | |
530 | ||
571cd43e | 531 | static bool process_rule(BlockDriverState *bs, struct BlkdebugRule *rule, |
8f96b5be | 532 | bool injected) |
8b9b0cc2 KW |
533 | { |
534 | BDRVBlkdebugState *s = bs->opaque; | |
8b9b0cc2 KW |
535 | |
536 | /* Only process rules for the current state */ | |
8f96b5be | 537 | if (rule->state && rule->state != s->state) { |
571cd43e | 538 | return injected; |
8b9b0cc2 KW |
539 | } |
540 | ||
541 | /* Take the action */ | |
542 | switch (rule->action) { | |
543 | case ACTION_INJECT_ERROR: | |
571cd43e PB |
544 | if (!injected) { |
545 | QSIMPLEQ_INIT(&s->active_rules); | |
546 | injected = true; | |
547 | } | |
548 | QSIMPLEQ_INSERT_HEAD(&s->active_rules, rule, active_next); | |
8b9b0cc2 KW |
549 | break; |
550 | ||
551 | case ACTION_SET_STATE: | |
8f96b5be | 552 | s->new_state = rule->options.set_state.new_state; |
8b9b0cc2 | 553 | break; |
3c90c65d KW |
554 | |
555 | case ACTION_SUSPEND: | |
556 | suspend_request(bs, rule); | |
557 | break; | |
8b9b0cc2 | 558 | } |
571cd43e | 559 | return injected; |
8b9b0cc2 KW |
560 | } |
561 | ||
562 | static void blkdebug_debug_event(BlockDriverState *bs, BlkDebugEvent event) | |
563 | { | |
564 | BDRVBlkdebugState *s = bs->opaque; | |
3c90c65d | 565 | struct BlkdebugRule *rule, *next; |
571cd43e | 566 | bool injected; |
8b9b0cc2 | 567 | |
95ee3914 | 568 | assert((int)event >= 0 && event < BLKDBG_EVENT_MAX); |
8b9b0cc2 | 569 | |
571cd43e | 570 | injected = false; |
8f96b5be | 571 | s->new_state = s->state; |
3c90c65d | 572 | QLIST_FOREACH_SAFE(rule, &s->rules[event], next, next) { |
8f96b5be | 573 | injected = process_rule(bs, rule, injected); |
8b9b0cc2 | 574 | } |
8f96b5be | 575 | s->state = s->new_state; |
8b9b0cc2 KW |
576 | } |
577 | ||
3c90c65d KW |
578 | static int blkdebug_debug_breakpoint(BlockDriverState *bs, const char *event, |
579 | const char *tag) | |
580 | { | |
581 | BDRVBlkdebugState *s = bs->opaque; | |
582 | struct BlkdebugRule *rule; | |
583 | BlkDebugEvent blkdebug_event; | |
584 | ||
585 | if (get_event_by_name(event, &blkdebug_event) < 0) { | |
586 | return -ENOENT; | |
587 | } | |
588 | ||
589 | ||
590 | rule = g_malloc(sizeof(*rule)); | |
591 | *rule = (struct BlkdebugRule) { | |
592 | .event = blkdebug_event, | |
593 | .action = ACTION_SUSPEND, | |
594 | .state = 0, | |
595 | .options.suspend.tag = g_strdup(tag), | |
596 | }; | |
597 | ||
598 | QLIST_INSERT_HEAD(&s->rules[blkdebug_event], rule, next); | |
599 | ||
600 | return 0; | |
601 | } | |
602 | ||
603 | static int blkdebug_debug_resume(BlockDriverState *bs, const char *tag) | |
604 | { | |
605 | BDRVBlkdebugState *s = bs->opaque; | |
c547e564 | 606 | BlkdebugSuspendedReq *r, *next; |
3c90c65d | 607 | |
c547e564 | 608 | QLIST_FOREACH_SAFE(r, &s->suspended_reqs, next, next) { |
3c90c65d KW |
609 | if (!strcmp(r->tag, tag)) { |
610 | qemu_coroutine_enter(r->co, NULL); | |
611 | return 0; | |
612 | } | |
613 | } | |
614 | return -ENOENT; | |
615 | } | |
616 | ||
4cc70e93 FZ |
617 | static int blkdebug_debug_remove_breakpoint(BlockDriverState *bs, |
618 | const char *tag) | |
619 | { | |
620 | BDRVBlkdebugState *s = bs->opaque; | |
c547e564 | 621 | BlkdebugSuspendedReq *r, *r_next; |
4cc70e93 FZ |
622 | BlkdebugRule *rule, *next; |
623 | int i, ret = -ENOENT; | |
624 | ||
625 | for (i = 0; i < BLKDBG_EVENT_MAX; i++) { | |
626 | QLIST_FOREACH_SAFE(rule, &s->rules[i], next, next) { | |
627 | if (rule->action == ACTION_SUSPEND && | |
628 | !strcmp(rule->options.suspend.tag, tag)) { | |
629 | remove_rule(rule); | |
630 | ret = 0; | |
631 | } | |
632 | } | |
633 | } | |
c547e564 | 634 | QLIST_FOREACH_SAFE(r, &s->suspended_reqs, next, r_next) { |
4cc70e93 FZ |
635 | if (!strcmp(r->tag, tag)) { |
636 | qemu_coroutine_enter(r->co, NULL); | |
637 | ret = 0; | |
638 | } | |
639 | } | |
640 | return ret; | |
641 | } | |
3c90c65d KW |
642 | |
643 | static bool blkdebug_debug_is_suspended(BlockDriverState *bs, const char *tag) | |
644 | { | |
645 | BDRVBlkdebugState *s = bs->opaque; | |
646 | BlkdebugSuspendedReq *r; | |
647 | ||
648 | QLIST_FOREACH(r, &s->suspended_reqs, next) { | |
649 | if (!strcmp(r->tag, tag)) { | |
650 | return true; | |
651 | } | |
652 | } | |
653 | return false; | |
654 | } | |
655 | ||
e1302255 PB |
656 | static int64_t blkdebug_getlength(BlockDriverState *bs) |
657 | { | |
658 | return bdrv_getlength(bs->file); | |
659 | } | |
660 | ||
6a143727 | 661 | static BlockDriver bdrv_blkdebug = { |
f4681212 KW |
662 | .format_name = "blkdebug", |
663 | .protocol_name = "blkdebug", | |
664 | .instance_size = sizeof(BDRVBlkdebugState), | |
6a143727 | 665 | |
f4681212 KW |
666 | .bdrv_parse_filename = blkdebug_parse_filename, |
667 | .bdrv_file_open = blkdebug_open, | |
668 | .bdrv_close = blkdebug_close, | |
669 | .bdrv_getlength = blkdebug_getlength, | |
6a143727 | 670 | |
f4681212 KW |
671 | .bdrv_aio_readv = blkdebug_aio_readv, |
672 | .bdrv_aio_writev = blkdebug_aio_writev, | |
8b9b0cc2 | 673 | |
3c90c65d KW |
674 | .bdrv_debug_event = blkdebug_debug_event, |
675 | .bdrv_debug_breakpoint = blkdebug_debug_breakpoint, | |
4cc70e93 FZ |
676 | .bdrv_debug_remove_breakpoint |
677 | = blkdebug_debug_remove_breakpoint, | |
3c90c65d KW |
678 | .bdrv_debug_resume = blkdebug_debug_resume, |
679 | .bdrv_debug_is_suspended = blkdebug_debug_is_suspended, | |
6a143727 KW |
680 | }; |
681 | ||
682 | static void bdrv_blkdebug_init(void) | |
683 | { | |
684 | bdrv_register(&bdrv_blkdebug); | |
685 | } | |
686 | ||
687 | block_init(bdrv_blkdebug_init); |