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