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