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