]> git.proxmox.com Git - mirror_qemu.git/blame - block/blkdebug.c
blkdebug: Simplify override logic
[mirror_qemu.git] / block / blkdebug.c
CommitLineData
6a143727
KW
1/*
2 * Block protocol for I/O error injection
3 *
577cf9e6 4 * Copyright (C) 2016-2017 Red Hat, Inc.
6a143727
KW
5 * Copyright (c) 2010 Kevin Wolf <kwolf@redhat.com>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 */
25
80c71a24 26#include "qemu/osdep.h"
da34e65c 27#include "qapi/error.h"
f348b6d1 28#include "qemu/cutils.h"
1de7afc9 29#include "qemu/config-file.h"
737e150e 30#include "block/block_int.h"
1de7afc9 31#include "qemu/module.h"
2c31b04c
HR
32#include "qapi/qmp/qbool.h"
33#include "qapi/qmp/qdict.h"
34#include "qapi/qmp/qint.h"
35#include "qapi/qmp/qstring.h"
20873526 36#include "sysemu/qtest.h"
6a143727
KW
37
38typedef struct BDRVBlkdebugState {
571cd43e 39 int state;
8f96b5be 40 int new_state;
3ae74003 41 uint64_t align;
3c90c65d 42
036990d7
HR
43 /* For blkdebug_refresh_filename() */
44 char *config_file;
45
7fb1cf16 46 QLIST_HEAD(, BlkdebugRule) rules[BLKDBG__MAX];
571cd43e 47 QSIMPLEQ_HEAD(, BlkdebugRule) active_rules;
3c90c65d 48 QLIST_HEAD(, BlkdebugSuspendedReq) suspended_reqs;
6a143727
KW
49} BDRVBlkdebugState;
50
b9f66d96 51typedef struct BlkdebugAIOCB {
7c84b1b8 52 BlockAIOCB common;
b9f66d96
KW
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
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;
7c3a9985 77 int64_t offset;
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 173 struct BlkdebugRule *rule;
7c3a9985 174 int64_t sector;
8b9b0cc2
KW
175
176 /* Find the right event for the rule */
177 event_name = qemu_opt_get(opts, "event");
d4362d64 178 if (!event_name) {
8809cfc3 179 error_setg(errp, "Missing event name for rule");
d4362d64
SH
180 return -1;
181 } else if (get_event_by_name(event_name, &event) < 0) {
8809cfc3 182 error_setg(errp, "Invalid event name \"%s\"", event_name);
8b9b0cc2
KW
183 return -1;
184 }
185
186 /* Set attributes common for all actions */
7267c094 187 rule = g_malloc0(sizeof(*rule));
8b9b0cc2
KW
188 *rule = (struct BlkdebugRule) {
189 .event = event,
190 .action = d->action,
191 .state = qemu_opt_get_number(opts, "state", 0),
192 };
193
194 /* Parse action-specific options */
195 switch (d->action) {
196 case ACTION_INJECT_ERROR:
197 rule->options.inject.error = qemu_opt_get_number(opts, "errno", EIO);
198 rule->options.inject.once = qemu_opt_get_bool(opts, "once", 0);
199 rule->options.inject.immediately =
200 qemu_opt_get_bool(opts, "immediately", 0);
7c3a9985
KW
201 sector = qemu_opt_get_number(opts, "sector", -1);
202 rule->options.inject.offset =
203 sector == -1 ? -1 : sector * BDRV_SECTOR_SIZE;
8b9b0cc2
KW
204 break;
205
206 case ACTION_SET_STATE:
207 rule->options.set_state.new_state =
208 qemu_opt_get_number(opts, "new_state", 0);
209 break;
3c90c65d
KW
210
211 case ACTION_SUSPEND:
212 rule->options.suspend.tag =
213 g_strdup(qemu_opt_get(opts, "tag"));
214 break;
8b9b0cc2
KW
215 };
216
217 /* Add the rule */
218 QLIST_INSERT_HEAD(&s->rules[event], rule, next);
219
220 return 0;
221}
222
9e35542b
KW
223static void remove_rule(BlkdebugRule *rule)
224{
225 switch (rule->action) {
226 case ACTION_INJECT_ERROR:
227 case ACTION_SET_STATE:
228 break;
3c90c65d
KW
229 case ACTION_SUSPEND:
230 g_free(rule->options.suspend.tag);
231 break;
9e35542b
KW
232 }
233
234 QLIST_REMOVE(rule, next);
235 g_free(rule);
236}
237
89f2b21e
HR
238static int read_config(BDRVBlkdebugState *s, const char *filename,
239 QDict *options, Error **errp)
8b9b0cc2 240{
85a040e5 241 FILE *f = NULL;
8b9b0cc2
KW
242 int ret;
243 struct add_rule_data d;
89f2b21e 244 Error *local_err = NULL;
8b9b0cc2 245
85a040e5
HR
246 if (filename) {
247 f = fopen(filename, "r");
248 if (f == NULL) {
249 error_setg_errno(errp, errno, "Could not read blkdebug config file");
250 return -errno;
251 }
8b9b0cc2 252
85a040e5
HR
253 ret = qemu_config_parse(f, config_groups, filename);
254 if (ret < 0) {
255 error_setg(errp, "Could not parse blkdebug config file");
256 ret = -EINVAL;
257 goto fail;
258 }
8b9b0cc2
KW
259 }
260
89f2b21e 261 qemu_config_parse_qdict(options, config_groups, &local_err);
84d18f06 262 if (local_err) {
89f2b21e
HR
263 error_propagate(errp, local_err);
264 ret = -EINVAL;
265 goto fail;
266 }
267
8b9b0cc2
KW
268 d.s = s;
269 d.action = ACTION_INJECT_ERROR;
8809cfc3 270 qemu_opts_foreach(&inject_error_opts, add_rule, &d, &local_err);
d4362d64
SH
271 if (local_err) {
272 error_propagate(errp, local_err);
273 ret = -EINVAL;
274 goto fail;
275 }
8b9b0cc2
KW
276
277 d.action = ACTION_SET_STATE;
8809cfc3 278 qemu_opts_foreach(&set_state_opts, add_rule, &d, &local_err);
d4362d64
SH
279 if (local_err) {
280 error_propagate(errp, local_err);
281 ret = -EINVAL;
282 goto fail;
283 }
8b9b0cc2
KW
284
285 ret = 0;
286fail:
698f0d52
KW
287 qemu_opts_reset(&inject_error_opts);
288 qemu_opts_reset(&set_state_opts);
85a040e5
HR
289 if (f) {
290 fclose(f);
291 }
8b9b0cc2
KW
292 return ret;
293}
294
295/* Valid blkdebug filenames look like blkdebug:path/to/config:path/to/image */
f4681212
KW
296static void blkdebug_parse_filename(const char *filename, QDict *options,
297 Error **errp)
6a143727 298{
f4681212 299 const char *c;
6a143727 300
8b9b0cc2 301 /* Parse the blkdebug: prefix */
f4681212 302 if (!strstart(filename, "blkdebug:", &filename)) {
d4881b9b
HR
303 /* There was no prefix; therefore, all options have to be already
304 present in the QDict (except for the filename) */
e59084b5 305 qdict_put_str(options, "x-image", filename);
f4681212 306 return;
6a143727 307 }
6a143727 308
f4681212 309 /* Parse config file path */
8b9b0cc2
KW
310 c = strchr(filename, ':');
311 if (c == NULL) {
f4681212
KW
312 error_setg(errp, "blkdebug requires both config file and image path");
313 return;
8b9b0cc2
KW
314 }
315
f4681212
KW
316 if (c != filename) {
317 QString *config_path;
318 config_path = qstring_from_substr(filename, 0, c - filename - 1);
319 qdict_put(options, "config", config_path);
8b9b0cc2 320 }
f4681212
KW
321
322 /* TODO Allow multi-level nesting and set file.filename here */
8b9b0cc2 323 filename = c + 1;
e59084b5 324 qdict_put_str(options, "x-image", filename);
f4681212
KW
325}
326
327static QemuOptsList runtime_opts = {
328 .name = "blkdebug",
329 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
330 .desc = {
331 {
332 .name = "config",
333 .type = QEMU_OPT_STRING,
334 .help = "Path to the configuration file",
335 },
336 {
337 .name = "x-image",
338 .type = QEMU_OPT_STRING,
339 .help = "[internal use only, will be removed]",
340 },
b35ee7fb
KW
341 {
342 .name = "align",
343 .type = QEMU_OPT_SIZE,
344 .help = "Required alignment in bytes",
345 },
f4681212
KW
346 { /* end of list */ }
347 },
348};
349
015a1036
HR
350static int blkdebug_open(BlockDriverState *bs, QDict *options, int flags,
351 Error **errp)
f4681212
KW
352{
353 BDRVBlkdebugState *s = bs->opaque;
354 QemuOpts *opts;
355 Error *local_err = NULL;
f4681212
KW
356 int ret;
357
87ea75d5 358 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
f4681212 359 qemu_opts_absorb_qdict(opts, options, &local_err);
84d18f06 360 if (local_err) {
10ffa72f 361 error_propagate(errp, local_err);
f4681212 362 ret = -EINVAL;
eaf944a4 363 goto out;
f4681212
KW
364 }
365
89f2b21e 366 /* Read rules from config file or command line options */
036990d7
HR
367 s->config_file = g_strdup(qemu_opt_get(opts, "config"));
368 ret = read_config(s, s->config_file, options, errp);
85a040e5 369 if (ret) {
eaf944a4 370 goto out;
f4681212 371 }
8b9b0cc2 372
8db520ce 373 /* Set initial state */
571cd43e 374 s->state = 1;
8db520ce 375
6b826af7 376 /* Open the image file */
9a4f4c31
KW
377 bs->file = bdrv_open_child(qemu_opt_get(opts, "x-image"), options, "image",
378 bs, &child_file, false, &local_err);
379 if (local_err) {
380 ret = -EINVAL;
10ffa72f 381 error_propagate(errp, local_err);
eaf944a4 382 goto out;
8b9b0cc2
KW
383 }
384
577cf9e6
EB
385 bs->supported_write_flags = BDRV_REQ_FUA &
386 bs->file->bs->supported_write_flags;
387 bs->supported_zero_flags = (BDRV_REQ_FUA | BDRV_REQ_MAY_UNMAP) &
388 bs->file->bs->supported_zero_flags;
3ae74003 389 ret = -EINVAL;
577cf9e6 390
b35ee7fb 391 /* Set request alignment */
3ae74003
EB
392 s->align = qemu_opt_get_size(opts, "align", 0);
393 if (s->align && (s->align >= INT_MAX || !is_power_of_2(s->align))) {
394 error_setg(errp, "Cannot meet constraints with align %" PRIu64,
395 s->align);
c1059a3a 396 goto out;
b35ee7fb
KW
397 }
398
f4681212 399 ret = 0;
eaf944a4 400out:
036990d7
HR
401 if (ret < 0) {
402 g_free(s->config_file);
403 }
f4681212
KW
404 qemu_opts_del(opts);
405 return ret;
6a143727
KW
406}
407
138cf638 408static int rule_check(BlockDriverState *bs, uint64_t offset, uint64_t bytes)
b9f66d96
KW
409{
410 BDRVBlkdebugState *s = bs->opaque;
138cf638
EB
411 BlkdebugRule *rule = NULL;
412 int error;
413 bool immediately;
414
415 QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) {
416 uint64_t inject_offset = rule->options.inject.offset;
417
418 if (inject_offset == -1 ||
419 (bytes && inject_offset >= offset &&
420 inject_offset < offset + bytes))
421 {
422 break;
423 }
424 }
425
426 if (!rule || !rule->options.inject.error) {
427 return 0;
428 }
429
430 immediately = rule->options.inject.immediately;
431 error = rule->options.inject.error;
b9f66d96 432
571cd43e 433 if (rule->options.inject.once) {
a069e2f1
JS
434 QSIMPLEQ_REMOVE(&s->active_rules, rule, BlkdebugRule, active_next);
435 remove_rule(rule);
b9f66d96
KW
436 }
437
7c3a9985 438 if (!immediately) {
e5c67ab5 439 aio_co_schedule(qemu_get_current_aio_context(), qemu_coroutine_self());
7c3a9985 440 qemu_coroutine_yield();
b9f66d96
KW
441 }
442
7c3a9985 443 return -error;
b9f66d96
KW
444}
445
7c3a9985
KW
446static int coroutine_fn
447blkdebug_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
448 QEMUIOVector *qiov, int flags)
6a143727 449{
138cf638 450 int err;
e4780db4 451
a1a3d603
EB
452 /* Sanity check block layer guarantees */
453 assert(QEMU_IS_ALIGNED(offset, bs->bl.request_alignment));
454 assert(QEMU_IS_ALIGNED(bytes, bs->bl.request_alignment));
455 if (bs->bl.max_transfer) {
456 assert(bytes <= bs->bl.max_transfer);
457 }
458
138cf638
EB
459 err = rule_check(bs, offset, bytes);
460 if (err) {
461 return err;
b9f66d96
KW
462 }
463
7c3a9985 464 return bdrv_co_preadv(bs->file, offset, bytes, qiov, flags);
6a143727
KW
465}
466
7c3a9985
KW
467static int coroutine_fn
468blkdebug_co_pwritev(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
469 QEMUIOVector *qiov, int flags)
6a143727 470{
138cf638 471 int err;
e4780db4 472
a1a3d603
EB
473 /* Sanity check block layer guarantees */
474 assert(QEMU_IS_ALIGNED(offset, bs->bl.request_alignment));
475 assert(QEMU_IS_ALIGNED(bytes, bs->bl.request_alignment));
476 if (bs->bl.max_transfer) {
477 assert(bytes <= bs->bl.max_transfer);
478 }
479
138cf638
EB
480 err = rule_check(bs, offset, bytes);
481 if (err) {
482 return err;
b9f66d96
KW
483 }
484
7c3a9985 485 return bdrv_co_pwritev(bs->file, offset, bytes, qiov, flags);
6a143727
KW
486}
487
7c3a9985 488static int blkdebug_co_flush(BlockDriverState *bs)
9e52c53b 489{
138cf638 490 int err = rule_check(bs, 0, 0);
9e52c53b 491
138cf638
EB
492 if (err) {
493 return err;
9e52c53b
PB
494 }
495
7c3a9985 496 return bdrv_co_flush(bs->file->bs);
9e52c53b
PB
497}
498
577cf9e6
EB
499static int coroutine_fn blkdebug_co_pwrite_zeroes(BlockDriverState *bs,
500 int64_t offset, int count,
501 BdrvRequestFlags flags)
502{
503 uint32_t align = MAX(bs->bl.request_alignment,
504 bs->bl.pwrite_zeroes_alignment);
505 int err;
506
507 /* Only pass through requests that are larger than requested
508 * preferred alignment (so that we test the fallback to writes on
509 * unaligned portions), and check that the block layer never hands
510 * us anything unaligned that crosses an alignment boundary. */
511 if (count < align) {
512 assert(QEMU_IS_ALIGNED(offset, align) ||
513 QEMU_IS_ALIGNED(offset + count, align) ||
514 DIV_ROUND_UP(offset, align) ==
515 DIV_ROUND_UP(offset + count, align));
516 return -ENOTSUP;
517 }
518 assert(QEMU_IS_ALIGNED(offset, align));
519 assert(QEMU_IS_ALIGNED(count, align));
520 if (bs->bl.max_pwrite_zeroes) {
521 assert(count <= bs->bl.max_pwrite_zeroes);
522 }
523
524 err = rule_check(bs, offset, count);
525 if (err) {
526 return err;
527 }
528
529 return bdrv_co_pwrite_zeroes(bs->file, offset, count, flags);
530}
531
532static int coroutine_fn blkdebug_co_pdiscard(BlockDriverState *bs,
533 int64_t offset, int count)
534{
535 uint32_t align = bs->bl.pdiscard_alignment;
536 int err;
537
538 /* Only pass through requests that are larger than requested
539 * minimum alignment, and ensure that unaligned requests do not
540 * cross optimum discard boundaries. */
541 if (count < bs->bl.request_alignment) {
542 assert(QEMU_IS_ALIGNED(offset, align) ||
543 QEMU_IS_ALIGNED(offset + count, align) ||
544 DIV_ROUND_UP(offset, align) ==
545 DIV_ROUND_UP(offset + count, align));
546 return -ENOTSUP;
547 }
548 assert(QEMU_IS_ALIGNED(offset, bs->bl.request_alignment));
549 assert(QEMU_IS_ALIGNED(count, bs->bl.request_alignment));
550 if (align && count >= align) {
551 assert(QEMU_IS_ALIGNED(offset, align));
552 assert(QEMU_IS_ALIGNED(count, align));
553 }
554 if (bs->bl.max_pdiscard) {
555 assert(count <= bs->bl.max_pdiscard);
556 }
557
558 err = rule_check(bs, offset, count);
559 if (err) {
560 return err;
561 }
562
563 return bdrv_co_pdiscard(bs->file->bs, offset, count);
564}
3c90c65d 565
6a143727
KW
566static void blkdebug_close(BlockDriverState *bs)
567{
568 BDRVBlkdebugState *s = bs->opaque;
8b9b0cc2
KW
569 BlkdebugRule *rule, *next;
570 int i;
571
7fb1cf16 572 for (i = 0; i < BLKDBG__MAX; i++) {
8b9b0cc2 573 QLIST_FOREACH_SAFE(rule, &s->rules[i], next, next) {
9e35542b 574 remove_rule(rule);
8b9b0cc2
KW
575 }
576 }
036990d7
HR
577
578 g_free(s->config_file);
6a143727
KW
579}
580
3c90c65d
KW
581static void suspend_request(BlockDriverState *bs, BlkdebugRule *rule)
582{
583 BDRVBlkdebugState *s = bs->opaque;
584 BlkdebugSuspendedReq r;
585
586 r = (BlkdebugSuspendedReq) {
587 .co = qemu_coroutine_self(),
588 .tag = g_strdup(rule->options.suspend.tag),
589 };
590
591 remove_rule(rule);
592 QLIST_INSERT_HEAD(&s->suspended_reqs, &r, next);
593
20873526
MT
594 if (!qtest_enabled()) {
595 printf("blkdebug: Suspended request '%s'\n", r.tag);
596 }
3c90c65d 597 qemu_coroutine_yield();
20873526
MT
598 if (!qtest_enabled()) {
599 printf("blkdebug: Resuming request '%s'\n", r.tag);
600 }
3c90c65d
KW
601
602 QLIST_REMOVE(&r, next);
603 g_free(r.tag);
604}
605
571cd43e 606static bool process_rule(BlockDriverState *bs, struct BlkdebugRule *rule,
8f96b5be 607 bool injected)
8b9b0cc2
KW
608{
609 BDRVBlkdebugState *s = bs->opaque;
8b9b0cc2
KW
610
611 /* Only process rules for the current state */
8f96b5be 612 if (rule->state && rule->state != s->state) {
571cd43e 613 return injected;
8b9b0cc2
KW
614 }
615
616 /* Take the action */
617 switch (rule->action) {
618 case ACTION_INJECT_ERROR:
571cd43e
PB
619 if (!injected) {
620 QSIMPLEQ_INIT(&s->active_rules);
621 injected = true;
622 }
623 QSIMPLEQ_INSERT_HEAD(&s->active_rules, rule, active_next);
8b9b0cc2
KW
624 break;
625
626 case ACTION_SET_STATE:
8f96b5be 627 s->new_state = rule->options.set_state.new_state;
8b9b0cc2 628 break;
3c90c65d
KW
629
630 case ACTION_SUSPEND:
631 suspend_request(bs, rule);
632 break;
8b9b0cc2 633 }
571cd43e 634 return injected;
8b9b0cc2
KW
635}
636
a31939e6 637static void blkdebug_debug_event(BlockDriverState *bs, BlkdebugEvent event)
8b9b0cc2
KW
638{
639 BDRVBlkdebugState *s = bs->opaque;
3c90c65d 640 struct BlkdebugRule *rule, *next;
571cd43e 641 bool injected;
8b9b0cc2 642
7fb1cf16 643 assert((int)event >= 0 && event < BLKDBG__MAX);
8b9b0cc2 644
571cd43e 645 injected = false;
8f96b5be 646 s->new_state = s->state;
3c90c65d 647 QLIST_FOREACH_SAFE(rule, &s->rules[event], next, next) {
8f96b5be 648 injected = process_rule(bs, rule, injected);
8b9b0cc2 649 }
8f96b5be 650 s->state = s->new_state;
8b9b0cc2
KW
651}
652
3c90c65d
KW
653static int blkdebug_debug_breakpoint(BlockDriverState *bs, const char *event,
654 const char *tag)
655{
656 BDRVBlkdebugState *s = bs->opaque;
657 struct BlkdebugRule *rule;
a31939e6 658 BlkdebugEvent blkdebug_event;
3c90c65d
KW
659
660 if (get_event_by_name(event, &blkdebug_event) < 0) {
661 return -ENOENT;
662 }
663
664
665 rule = g_malloc(sizeof(*rule));
666 *rule = (struct BlkdebugRule) {
667 .event = blkdebug_event,
668 .action = ACTION_SUSPEND,
669 .state = 0,
670 .options.suspend.tag = g_strdup(tag),
671 };
672
673 QLIST_INSERT_HEAD(&s->rules[blkdebug_event], rule, next);
674
675 return 0;
676}
677
678static int blkdebug_debug_resume(BlockDriverState *bs, const char *tag)
679{
680 BDRVBlkdebugState *s = bs->opaque;
c547e564 681 BlkdebugSuspendedReq *r, *next;
3c90c65d 682
c547e564 683 QLIST_FOREACH_SAFE(r, &s->suspended_reqs, next, next) {
3c90c65d 684 if (!strcmp(r->tag, tag)) {
0b8b8753 685 qemu_coroutine_enter(r->co);
3c90c65d
KW
686 return 0;
687 }
688 }
689 return -ENOENT;
690}
691
4cc70e93
FZ
692static int blkdebug_debug_remove_breakpoint(BlockDriverState *bs,
693 const char *tag)
694{
695 BDRVBlkdebugState *s = bs->opaque;
c547e564 696 BlkdebugSuspendedReq *r, *r_next;
4cc70e93
FZ
697 BlkdebugRule *rule, *next;
698 int i, ret = -ENOENT;
699
7fb1cf16 700 for (i = 0; i < BLKDBG__MAX; i++) {
4cc70e93
FZ
701 QLIST_FOREACH_SAFE(rule, &s->rules[i], next, next) {
702 if (rule->action == ACTION_SUSPEND &&
703 !strcmp(rule->options.suspend.tag, tag)) {
704 remove_rule(rule);
705 ret = 0;
706 }
707 }
708 }
c547e564 709 QLIST_FOREACH_SAFE(r, &s->suspended_reqs, next, r_next) {
4cc70e93 710 if (!strcmp(r->tag, tag)) {
0b8b8753 711 qemu_coroutine_enter(r->co);
4cc70e93
FZ
712 ret = 0;
713 }
714 }
715 return ret;
716}
3c90c65d
KW
717
718static bool blkdebug_debug_is_suspended(BlockDriverState *bs, const char *tag)
719{
720 BDRVBlkdebugState *s = bs->opaque;
721 BlkdebugSuspendedReq *r;
722
723 QLIST_FOREACH(r, &s->suspended_reqs, next) {
724 if (!strcmp(r->tag, tag)) {
725 return true;
726 }
727 }
728 return false;
729}
730
e1302255
PB
731static int64_t blkdebug_getlength(BlockDriverState *bs)
732{
9a4f4c31 733 return bdrv_getlength(bs->file->bs);
e1302255
PB
734}
735
8eedfbd4
KW
736static int blkdebug_truncate(BlockDriverState *bs, int64_t offset)
737{
5797a36a 738 return bdrv_truncate(bs->file, offset, NULL);
8eedfbd4
KW
739}
740
4cdd01d3 741static void blkdebug_refresh_filename(BlockDriverState *bs, QDict *options)
2c31b04c 742{
036990d7 743 BDRVBlkdebugState *s = bs->opaque;
2c31b04c 744 QDict *opts;
8779441b
HR
745 const QDictEntry *e;
746 bool force_json = false;
747
4cdd01d3 748 for (e = qdict_first(options); e; e = qdict_next(options, e)) {
8779441b 749 if (strcmp(qdict_entry_key(e), "config") &&
4cdd01d3 750 strcmp(qdict_entry_key(e), "x-image"))
8779441b
HR
751 {
752 force_json = true;
753 break;
754 }
755 }
2c31b04c 756
9a4f4c31 757 if (force_json && !bs->file->bs->full_open_options) {
2c31b04c
HR
758 /* The config file cannot be recreated, so creating a plain filename
759 * is impossible */
760 return;
761 }
762
9a4f4c31 763 if (!force_json && bs->file->bs->exact_filename[0]) {
8779441b 764 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
036990d7 765 "blkdebug:%s:%s", s->config_file ?: "",
9a4f4c31 766 bs->file->bs->exact_filename);
8779441b
HR
767 }
768
2c31b04c 769 opts = qdict_new();
e59084b5 770 qdict_put_str(opts, "driver", "blkdebug");
2c31b04c 771
9a4f4c31 772 QINCREF(bs->file->bs->full_open_options);
3f308bf3 773 qdict_put(opts, "image", bs->file->bs->full_open_options);
2c31b04c 774
4cdd01d3
KW
775 for (e = qdict_first(options); e; e = qdict_next(options, e)) {
776 if (strcmp(qdict_entry_key(e), "x-image")) {
8779441b
HR
777 qobject_incref(qdict_entry_value(e));
778 qdict_put_obj(opts, qdict_entry_key(e), qdict_entry_value(e));
2c31b04c
HR
779 }
780 }
781
2c31b04c
HR
782 bs->full_open_options = opts;
783}
784
835db3ee
EB
785static void blkdebug_refresh_limits(BlockDriverState *bs, Error **errp)
786{
787 BDRVBlkdebugState *s = bs->opaque;
788
789 if (s->align) {
a5b8dd2c 790 bs->bl.request_alignment = s->align;
835db3ee
EB
791 }
792}
793
c5e8bfb7
KW
794static int blkdebug_reopen_prepare(BDRVReopenState *reopen_state,
795 BlockReopenQueue *queue, Error **errp)
796{
797 return 0;
798}
799
6a143727 800static BlockDriver bdrv_blkdebug = {
f4681212
KW
801 .format_name = "blkdebug",
802 .protocol_name = "blkdebug",
803 .instance_size = sizeof(BDRVBlkdebugState),
6a143727 804
f4681212
KW
805 .bdrv_parse_filename = blkdebug_parse_filename,
806 .bdrv_file_open = blkdebug_open,
807 .bdrv_close = blkdebug_close,
c5e8bfb7 808 .bdrv_reopen_prepare = blkdebug_reopen_prepare,
d7010dfb
KW
809 .bdrv_child_perm = bdrv_filter_default_perms,
810
f4681212 811 .bdrv_getlength = blkdebug_getlength,
8eedfbd4 812 .bdrv_truncate = blkdebug_truncate,
2c31b04c 813 .bdrv_refresh_filename = blkdebug_refresh_filename,
835db3ee 814 .bdrv_refresh_limits = blkdebug_refresh_limits,
6a143727 815
7c3a9985
KW
816 .bdrv_co_preadv = blkdebug_co_preadv,
817 .bdrv_co_pwritev = blkdebug_co_pwritev,
818 .bdrv_co_flush_to_disk = blkdebug_co_flush,
577cf9e6
EB
819 .bdrv_co_pwrite_zeroes = blkdebug_co_pwrite_zeroes,
820 .bdrv_co_pdiscard = blkdebug_co_pdiscard,
8b9b0cc2 821
3c90c65d
KW
822 .bdrv_debug_event = blkdebug_debug_event,
823 .bdrv_debug_breakpoint = blkdebug_debug_breakpoint,
4cc70e93
FZ
824 .bdrv_debug_remove_breakpoint
825 = blkdebug_debug_remove_breakpoint,
3c90c65d
KW
826 .bdrv_debug_resume = blkdebug_debug_resume,
827 .bdrv_debug_is_suspended = blkdebug_debug_is_suspended,
6a143727
KW
828};
829
830static void bdrv_blkdebug_init(void)
831{
832 bdrv_register(&bdrv_blkdebug);
833}
834
835block_init(bdrv_blkdebug_init);