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