]> git.proxmox.com Git - mirror_qemu.git/blame - block/blkdebug.c
spapr: Refactor spapr_populate_memory() to allow memoryless nodes
[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"
6a143727
KW
33
34typedef struct BDRVBlkdebugState {
571cd43e 35 int state;
8f96b5be 36 int new_state;
3c90c65d 37
571cd43e
PB
38 QLIST_HEAD(, BlkdebugRule) rules[BLKDBG_EVENT_MAX];
39 QSIMPLEQ_HEAD(, BlkdebugRule) active_rules;
3c90c65d 40 QLIST_HEAD(, BlkdebugSuspendedReq) suspended_reqs;
6a143727
KW
41} BDRVBlkdebugState;
42
b9f66d96
KW
43typedef struct BlkdebugAIOCB {
44 BlockDriverAIOCB common;
45 QEMUBH *bh;
46 int ret;
47} BlkdebugAIOCB;
48
3c90c65d
KW
49typedef struct BlkdebugSuspendedReq {
50 Coroutine *co;
51 char *tag;
52 QLIST_ENTRY(BlkdebugSuspendedReq) next;
53} BlkdebugSuspendedReq;
54
b9f66d96
KW
55static void blkdebug_aio_cancel(BlockDriverAIOCB *blockacb);
56
d7331bed 57static const AIOCBInfo blkdebug_aiocb_info = {
b9f66d96
KW
58 .aiocb_size = sizeof(BlkdebugAIOCB),
59 .cancel = blkdebug_aio_cancel,
60};
61
8b9b0cc2
KW
62enum {
63 ACTION_INJECT_ERROR,
64 ACTION_SET_STATE,
3c90c65d 65 ACTION_SUSPEND,
8b9b0cc2
KW
66};
67
68typedef struct BlkdebugRule {
69 BlkDebugEvent event;
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
148static const char *event_names[BLKDBG_EVENT_MAX] = {
8252278a
KW
149 [BLKDBG_L1_UPDATE] = "l1_update",
150 [BLKDBG_L1_GROW_ALLOC_TABLE] = "l1_grow.alloc_table",
151 [BLKDBG_L1_GROW_WRITE_TABLE] = "l1_grow.write_table",
152 [BLKDBG_L1_GROW_ACTIVATE_TABLE] = "l1_grow.activate_table",
153
154 [BLKDBG_L2_LOAD] = "l2_load",
155 [BLKDBG_L2_UPDATE] = "l2_update",
156 [BLKDBG_L2_UPDATE_COMPRESSED] = "l2_update_compressed",
157 [BLKDBG_L2_ALLOC_COW_READ] = "l2_alloc.cow_read",
158 [BLKDBG_L2_ALLOC_WRITE] = "l2_alloc.write",
159
8252278a 160 [BLKDBG_READ_AIO] = "read_aio",
8252278a
KW
161 [BLKDBG_READ_BACKING_AIO] = "read_backing_aio",
162 [BLKDBG_READ_COMPRESSED] = "read_compressed",
163
164 [BLKDBG_WRITE_AIO] = "write_aio",
165 [BLKDBG_WRITE_COMPRESSED] = "write_compressed",
166
167 [BLKDBG_VMSTATE_LOAD] = "vmstate_load",
168 [BLKDBG_VMSTATE_SAVE] = "vmstate_save",
169
170 [BLKDBG_COW_READ] = "cow_read",
171 [BLKDBG_COW_WRITE] = "cow_write",
172
173 [BLKDBG_REFTABLE_LOAD] = "reftable_load",
174 [BLKDBG_REFTABLE_GROW] = "reftable_grow",
afa50193 175 [BLKDBG_REFTABLE_UPDATE] = "reftable_update",
8252278a
KW
176
177 [BLKDBG_REFBLOCK_LOAD] = "refblock_load",
178 [BLKDBG_REFBLOCK_UPDATE] = "refblock_update",
179 [BLKDBG_REFBLOCK_UPDATE_PART] = "refblock_update_part",
180 [BLKDBG_REFBLOCK_ALLOC] = "refblock_alloc",
181 [BLKDBG_REFBLOCK_ALLOC_HOOKUP] = "refblock_alloc.hookup",
182 [BLKDBG_REFBLOCK_ALLOC_WRITE] = "refblock_alloc.write",
183 [BLKDBG_REFBLOCK_ALLOC_WRITE_BLOCKS] = "refblock_alloc.write_blocks",
184 [BLKDBG_REFBLOCK_ALLOC_WRITE_TABLE] = "refblock_alloc.write_table",
185 [BLKDBG_REFBLOCK_ALLOC_SWITCH_TABLE] = "refblock_alloc.switch_table",
186
187 [BLKDBG_CLUSTER_ALLOC] = "cluster_alloc",
188 [BLKDBG_CLUSTER_ALLOC_BYTES] = "cluster_alloc_bytes",
189 [BLKDBG_CLUSTER_FREE] = "cluster_free",
bf736fe3
KW
190
191 [BLKDBG_FLUSH_TO_OS] = "flush_to_os",
192 [BLKDBG_FLUSH_TO_DISK] = "flush_to_disk",
9e1cb96d
KW
193
194 [BLKDBG_PWRITEV_RMW_HEAD] = "pwritev_rmw.head",
195 [BLKDBG_PWRITEV_RMW_AFTER_HEAD] = "pwritev_rmw.after_head",
196 [BLKDBG_PWRITEV_RMW_TAIL] = "pwritev_rmw.tail",
197 [BLKDBG_PWRITEV_RMW_AFTER_TAIL] = "pwritev_rmw.after_tail",
198 [BLKDBG_PWRITEV] = "pwritev",
199 [BLKDBG_PWRITEV_ZERO] = "pwritev_zero",
200 [BLKDBG_PWRITEV_DONE] = "pwritev_done",
8b9b0cc2
KW
201};
202
203static int get_event_by_name(const char *name, BlkDebugEvent *event)
204{
205 int i;
206
207 for (i = 0; i < BLKDBG_EVENT_MAX; i++) {
208 if (!strcmp(event_names[i], name)) {
209 *event = i;
210 return 0;
211 }
212 }
213
214 return -1;
215}
216
217struct add_rule_data {
218 BDRVBlkdebugState *s;
219 int action;
220};
221
222static int add_rule(QemuOpts *opts, void *opaque)
223{
224 struct add_rule_data *d = opaque;
225 BDRVBlkdebugState *s = d->s;
226 const char* event_name;
227 BlkDebugEvent event;
228 struct BlkdebugRule *rule;
229
230 /* Find the right event for the rule */
231 event_name = qemu_opt_get(opts, "event");
232 if (!event_name || get_event_by_name(event_name, &event) < 0) {
233 return -1;
234 }
235
236 /* Set attributes common for all actions */
7267c094 237 rule = g_malloc0(sizeof(*rule));
8b9b0cc2
KW
238 *rule = (struct BlkdebugRule) {
239 .event = event,
240 .action = d->action,
241 .state = qemu_opt_get_number(opts, "state", 0),
242 };
243
244 /* Parse action-specific options */
245 switch (d->action) {
246 case ACTION_INJECT_ERROR:
247 rule->options.inject.error = qemu_opt_get_number(opts, "errno", EIO);
248 rule->options.inject.once = qemu_opt_get_bool(opts, "once", 0);
249 rule->options.inject.immediately =
250 qemu_opt_get_bool(opts, "immediately", 0);
e4780db4 251 rule->options.inject.sector = qemu_opt_get_number(opts, "sector", -1);
8b9b0cc2
KW
252 break;
253
254 case ACTION_SET_STATE:
255 rule->options.set_state.new_state =
256 qemu_opt_get_number(opts, "new_state", 0);
257 break;
3c90c65d
KW
258
259 case ACTION_SUSPEND:
260 rule->options.suspend.tag =
261 g_strdup(qemu_opt_get(opts, "tag"));
262 break;
8b9b0cc2
KW
263 };
264
265 /* Add the rule */
266 QLIST_INSERT_HEAD(&s->rules[event], rule, next);
267
268 return 0;
269}
270
9e35542b
KW
271static void remove_rule(BlkdebugRule *rule)
272{
273 switch (rule->action) {
274 case ACTION_INJECT_ERROR:
275 case ACTION_SET_STATE:
276 break;
3c90c65d
KW
277 case ACTION_SUSPEND:
278 g_free(rule->options.suspend.tag);
279 break;
9e35542b
KW
280 }
281
282 QLIST_REMOVE(rule, next);
283 g_free(rule);
284}
285
89f2b21e
HR
286static int read_config(BDRVBlkdebugState *s, const char *filename,
287 QDict *options, Error **errp)
8b9b0cc2 288{
85a040e5 289 FILE *f = NULL;
8b9b0cc2
KW
290 int ret;
291 struct add_rule_data d;
89f2b21e 292 Error *local_err = NULL;
8b9b0cc2 293
85a040e5
HR
294 if (filename) {
295 f = fopen(filename, "r");
296 if (f == NULL) {
297 error_setg_errno(errp, errno, "Could not read blkdebug config file");
298 return -errno;
299 }
8b9b0cc2 300
85a040e5
HR
301 ret = qemu_config_parse(f, config_groups, filename);
302 if (ret < 0) {
303 error_setg(errp, "Could not parse blkdebug config file");
304 ret = -EINVAL;
305 goto fail;
306 }
8b9b0cc2
KW
307 }
308
89f2b21e 309 qemu_config_parse_qdict(options, config_groups, &local_err);
84d18f06 310 if (local_err) {
89f2b21e
HR
311 error_propagate(errp, local_err);
312 ret = -EINVAL;
313 goto fail;
314 }
315
8b9b0cc2
KW
316 d.s = s;
317 d.action = ACTION_INJECT_ERROR;
318 qemu_opts_foreach(&inject_error_opts, add_rule, &d, 0);
319
320 d.action = ACTION_SET_STATE;
321 qemu_opts_foreach(&set_state_opts, add_rule, &d, 0);
322
323 ret = 0;
324fail:
698f0d52
KW
325 qemu_opts_reset(&inject_error_opts);
326 qemu_opts_reset(&set_state_opts);
85a040e5
HR
327 if (f) {
328 fclose(f);
329 }
8b9b0cc2
KW
330 return ret;
331}
332
333/* Valid blkdebug filenames look like blkdebug:path/to/config:path/to/image */
f4681212
KW
334static void blkdebug_parse_filename(const char *filename, QDict *options,
335 Error **errp)
6a143727 336{
f4681212 337 const char *c;
6a143727 338
8b9b0cc2 339 /* Parse the blkdebug: prefix */
f4681212 340 if (!strstart(filename, "blkdebug:", &filename)) {
d4881b9b
HR
341 /* There was no prefix; therefore, all options have to be already
342 present in the QDict (except for the filename) */
343 qdict_put(options, "x-image", qstring_from_str(filename));
f4681212 344 return;
6a143727 345 }
6a143727 346
f4681212 347 /* Parse config file path */
8b9b0cc2
KW
348 c = strchr(filename, ':');
349 if (c == NULL) {
f4681212
KW
350 error_setg(errp, "blkdebug requires both config file and image path");
351 return;
8b9b0cc2
KW
352 }
353
f4681212
KW
354 if (c != filename) {
355 QString *config_path;
356 config_path = qstring_from_substr(filename, 0, c - filename - 1);
357 qdict_put(options, "config", config_path);
8b9b0cc2 358 }
f4681212
KW
359
360 /* TODO Allow multi-level nesting and set file.filename here */
8b9b0cc2 361 filename = c + 1;
f4681212
KW
362 qdict_put(options, "x-image", qstring_from_str(filename));
363}
364
365static QemuOptsList runtime_opts = {
366 .name = "blkdebug",
367 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
368 .desc = {
369 {
370 .name = "config",
371 .type = QEMU_OPT_STRING,
372 .help = "Path to the configuration file",
373 },
374 {
375 .name = "x-image",
376 .type = QEMU_OPT_STRING,
377 .help = "[internal use only, will be removed]",
378 },
b35ee7fb
KW
379 {
380 .name = "align",
381 .type = QEMU_OPT_SIZE,
382 .help = "Required alignment in bytes",
383 },
f4681212
KW
384 { /* end of list */ }
385 },
386};
387
015a1036
HR
388static int blkdebug_open(BlockDriverState *bs, QDict *options, int flags,
389 Error **errp)
f4681212
KW
390{
391 BDRVBlkdebugState *s = bs->opaque;
392 QemuOpts *opts;
393 Error *local_err = NULL;
4373593d 394 const char *config;
b35ee7fb 395 uint64_t align;
f4681212
KW
396 int ret;
397
87ea75d5 398 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
f4681212 399 qemu_opts_absorb_qdict(opts, options, &local_err);
84d18f06 400 if (local_err) {
10ffa72f 401 error_propagate(errp, local_err);
f4681212 402 ret = -EINVAL;
eaf944a4 403 goto out;
f4681212
KW
404 }
405
89f2b21e 406 /* Read rules from config file or command line options */
f4681212 407 config = qemu_opt_get(opts, "config");
89f2b21e 408 ret = read_config(s, config, options, errp);
85a040e5 409 if (ret) {
eaf944a4 410 goto out;
f4681212 411 }
8b9b0cc2 412
8db520ce 413 /* Set initial state */
571cd43e 414 s->state = 1;
8db520ce 415
8b9b0cc2 416 /* Open the backing file */
f67503e5 417 assert(bs->file == NULL);
4373593d 418 ret = bdrv_open_image(&bs->file, qemu_opt_get(opts, "x-image"), options, "image",
f7d9fd8c 419 flags | BDRV_O_PROTOCOL, false, &local_err);
8b9b0cc2 420 if (ret < 0) {
10ffa72f 421 error_propagate(errp, local_err);
eaf944a4 422 goto out;
8b9b0cc2
KW
423 }
424
b35ee7fb
KW
425 /* Set request alignment */
426 align = qemu_opt_get_size(opts, "align", bs->request_alignment);
427 if (align > 0 && align < INT_MAX && !(align & (align - 1))) {
428 bs->request_alignment = align;
429 } else {
430 error_setg(errp, "Invalid alignment");
431 ret = -EINVAL;
eaf944a4 432 goto fail_unref;
b35ee7fb
KW
433 }
434
f4681212 435 ret = 0;
eaf944a4
KW
436 goto out;
437
438fail_unref:
439 bdrv_unref(bs->file);
440out:
f4681212
KW
441 qemu_opts_del(opts);
442 return ret;
6a143727
KW
443}
444
b9f66d96
KW
445static void error_callback_bh(void *opaque)
446{
447 struct BlkdebugAIOCB *acb = opaque;
448 qemu_bh_delete(acb->bh);
449 acb->common.cb(acb->common.opaque, acb->ret);
450 qemu_aio_release(acb);
451}
452
453static void blkdebug_aio_cancel(BlockDriverAIOCB *blockacb)
454{
b666d239 455 BlkdebugAIOCB *acb = container_of(blockacb, BlkdebugAIOCB, common);
cbf95a0b
FZ
456 if (acb->bh) {
457 qemu_bh_delete(acb->bh);
458 acb->bh = NULL;
459 }
b9f66d96
KW
460 qemu_aio_release(acb);
461}
462
463static BlockDriverAIOCB *inject_error(BlockDriverState *bs,
571cd43e 464 BlockDriverCompletionFunc *cb, void *opaque, BlkdebugRule *rule)
b9f66d96
KW
465{
466 BDRVBlkdebugState *s = bs->opaque;
571cd43e 467 int error = rule->options.inject.error;
b9f66d96
KW
468 struct BlkdebugAIOCB *acb;
469 QEMUBH *bh;
470
571cd43e
PB
471 if (rule->options.inject.once) {
472 QSIMPLEQ_INIT(&s->active_rules);
b9f66d96
KW
473 }
474
571cd43e 475 if (rule->options.inject.immediately) {
b9f66d96
KW
476 return NULL;
477 }
478
d7331bed 479 acb = qemu_aio_get(&blkdebug_aiocb_info, bs, cb, opaque);
b9f66d96
KW
480 acb->ret = -error;
481
7e1efdf0 482 bh = aio_bh_new(bdrv_get_aio_context(bs), error_callback_bh, acb);
b9f66d96
KW
483 acb->bh = bh;
484 qemu_bh_schedule(bh);
485
b666d239 486 return &acb->common;
b9f66d96
KW
487}
488
6a143727
KW
489static BlockDriverAIOCB *blkdebug_aio_readv(BlockDriverState *bs,
490 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
491 BlockDriverCompletionFunc *cb, void *opaque)
492{
493 BDRVBlkdebugState *s = bs->opaque;
e4780db4
PB
494 BlkdebugRule *rule = NULL;
495
496 QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) {
497 if (rule->options.inject.sector == -1 ||
498 (rule->options.inject.sector >= sector_num &&
499 rule->options.inject.sector < sector_num + nb_sectors)) {
500 break;
501 }
502 }
b9f66d96 503
571cd43e
PB
504 if (rule && rule->options.inject.error) {
505 return inject_error(bs, cb, opaque, rule);
b9f66d96
KW
506 }
507
368e8dd1 508 return bdrv_aio_readv(bs->file, sector_num, qiov, nb_sectors, cb, opaque);
6a143727
KW
509}
510
511static BlockDriverAIOCB *blkdebug_aio_writev(BlockDriverState *bs,
512 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
513 BlockDriverCompletionFunc *cb, void *opaque)
514{
515 BDRVBlkdebugState *s = bs->opaque;
e4780db4
PB
516 BlkdebugRule *rule = NULL;
517
518 QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) {
519 if (rule->options.inject.sector == -1 ||
520 (rule->options.inject.sector >= sector_num &&
521 rule->options.inject.sector < sector_num + nb_sectors)) {
522 break;
523 }
524 }
b9f66d96 525
571cd43e
PB
526 if (rule && rule->options.inject.error) {
527 return inject_error(bs, cb, opaque, rule);
b9f66d96
KW
528 }
529
368e8dd1 530 return bdrv_aio_writev(bs->file, sector_num, qiov, nb_sectors, cb, opaque);
6a143727
KW
531}
532
9e52c53b
PB
533static BlockDriverAIOCB *blkdebug_aio_flush(BlockDriverState *bs,
534 BlockDriverCompletionFunc *cb, void *opaque)
535{
536 BDRVBlkdebugState *s = bs->opaque;
537 BlkdebugRule *rule = NULL;
538
539 QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) {
540 if (rule->options.inject.sector == -1) {
541 break;
542 }
543 }
544
545 if (rule && rule->options.inject.error) {
546 return inject_error(bs, cb, opaque, rule);
547 }
548
549 return bdrv_aio_flush(bs->file, cb, opaque);
550}
551
3c90c65d 552
6a143727
KW
553static void blkdebug_close(BlockDriverState *bs)
554{
555 BDRVBlkdebugState *s = bs->opaque;
8b9b0cc2
KW
556 BlkdebugRule *rule, *next;
557 int i;
558
559 for (i = 0; i < BLKDBG_EVENT_MAX; i++) {
560 QLIST_FOREACH_SAFE(rule, &s->rules[i], next, next) {
9e35542b 561 remove_rule(rule);
8b9b0cc2
KW
562 }
563 }
6a143727
KW
564}
565
3c90c65d
KW
566static void suspend_request(BlockDriverState *bs, BlkdebugRule *rule)
567{
568 BDRVBlkdebugState *s = bs->opaque;
569 BlkdebugSuspendedReq r;
570
571 r = (BlkdebugSuspendedReq) {
572 .co = qemu_coroutine_self(),
573 .tag = g_strdup(rule->options.suspend.tag),
574 };
575
576 remove_rule(rule);
577 QLIST_INSERT_HEAD(&s->suspended_reqs, &r, next);
578
579 printf("blkdebug: Suspended request '%s'\n", r.tag);
580 qemu_coroutine_yield();
581 printf("blkdebug: Resuming request '%s'\n", r.tag);
582
583 QLIST_REMOVE(&r, next);
584 g_free(r.tag);
585}
586
571cd43e 587static bool process_rule(BlockDriverState *bs, struct BlkdebugRule *rule,
8f96b5be 588 bool injected)
8b9b0cc2
KW
589{
590 BDRVBlkdebugState *s = bs->opaque;
8b9b0cc2
KW
591
592 /* Only process rules for the current state */
8f96b5be 593 if (rule->state && rule->state != s->state) {
571cd43e 594 return injected;
8b9b0cc2
KW
595 }
596
597 /* Take the action */
598 switch (rule->action) {
599 case ACTION_INJECT_ERROR:
571cd43e
PB
600 if (!injected) {
601 QSIMPLEQ_INIT(&s->active_rules);
602 injected = true;
603 }
604 QSIMPLEQ_INSERT_HEAD(&s->active_rules, rule, active_next);
8b9b0cc2
KW
605 break;
606
607 case ACTION_SET_STATE:
8f96b5be 608 s->new_state = rule->options.set_state.new_state;
8b9b0cc2 609 break;
3c90c65d
KW
610
611 case ACTION_SUSPEND:
612 suspend_request(bs, rule);
613 break;
8b9b0cc2 614 }
571cd43e 615 return injected;
8b9b0cc2
KW
616}
617
618static void blkdebug_debug_event(BlockDriverState *bs, BlkDebugEvent event)
619{
620 BDRVBlkdebugState *s = bs->opaque;
3c90c65d 621 struct BlkdebugRule *rule, *next;
571cd43e 622 bool injected;
8b9b0cc2 623
95ee3914 624 assert((int)event >= 0 && event < BLKDBG_EVENT_MAX);
8b9b0cc2 625
571cd43e 626 injected = false;
8f96b5be 627 s->new_state = s->state;
3c90c65d 628 QLIST_FOREACH_SAFE(rule, &s->rules[event], next, next) {
8f96b5be 629 injected = process_rule(bs, rule, injected);
8b9b0cc2 630 }
8f96b5be 631 s->state = s->new_state;
8b9b0cc2
KW
632}
633
3c90c65d
KW
634static int blkdebug_debug_breakpoint(BlockDriverState *bs, const char *event,
635 const char *tag)
636{
637 BDRVBlkdebugState *s = bs->opaque;
638 struct BlkdebugRule *rule;
639 BlkDebugEvent blkdebug_event;
640
641 if (get_event_by_name(event, &blkdebug_event) < 0) {
642 return -ENOENT;
643 }
644
645
646 rule = g_malloc(sizeof(*rule));
647 *rule = (struct BlkdebugRule) {
648 .event = blkdebug_event,
649 .action = ACTION_SUSPEND,
650 .state = 0,
651 .options.suspend.tag = g_strdup(tag),
652 };
653
654 QLIST_INSERT_HEAD(&s->rules[blkdebug_event], rule, next);
655
656 return 0;
657}
658
659static int blkdebug_debug_resume(BlockDriverState *bs, const char *tag)
660{
661 BDRVBlkdebugState *s = bs->opaque;
c547e564 662 BlkdebugSuspendedReq *r, *next;
3c90c65d 663
c547e564 664 QLIST_FOREACH_SAFE(r, &s->suspended_reqs, next, next) {
3c90c65d
KW
665 if (!strcmp(r->tag, tag)) {
666 qemu_coroutine_enter(r->co, NULL);
667 return 0;
668 }
669 }
670 return -ENOENT;
671}
672
4cc70e93
FZ
673static int blkdebug_debug_remove_breakpoint(BlockDriverState *bs,
674 const char *tag)
675{
676 BDRVBlkdebugState *s = bs->opaque;
c547e564 677 BlkdebugSuspendedReq *r, *r_next;
4cc70e93
FZ
678 BlkdebugRule *rule, *next;
679 int i, ret = -ENOENT;
680
681 for (i = 0; i < BLKDBG_EVENT_MAX; i++) {
682 QLIST_FOREACH_SAFE(rule, &s->rules[i], next, next) {
683 if (rule->action == ACTION_SUSPEND &&
684 !strcmp(rule->options.suspend.tag, tag)) {
685 remove_rule(rule);
686 ret = 0;
687 }
688 }
689 }
c547e564 690 QLIST_FOREACH_SAFE(r, &s->suspended_reqs, next, r_next) {
4cc70e93
FZ
691 if (!strcmp(r->tag, tag)) {
692 qemu_coroutine_enter(r->co, NULL);
693 ret = 0;
694 }
695 }
696 return ret;
697}
3c90c65d
KW
698
699static bool blkdebug_debug_is_suspended(BlockDriverState *bs, const char *tag)
700{
701 BDRVBlkdebugState *s = bs->opaque;
702 BlkdebugSuspendedReq *r;
703
704 QLIST_FOREACH(r, &s->suspended_reqs, next) {
705 if (!strcmp(r->tag, tag)) {
706 return true;
707 }
708 }
709 return false;
710}
711
e1302255
PB
712static int64_t blkdebug_getlength(BlockDriverState *bs)
713{
714 return bdrv_getlength(bs->file);
715}
716
2c31b04c
HR
717static void blkdebug_refresh_filename(BlockDriverState *bs)
718{
719 BDRVBlkdebugState *s = bs->opaque;
720 struct BlkdebugRule *rule;
721 QDict *opts;
722 QList *inject_error_list = NULL, *set_state_list = NULL;
723 QList *suspend_list = NULL;
724 int event;
725
726 if (!bs->file->full_open_options) {
727 /* The config file cannot be recreated, so creating a plain filename
728 * is impossible */
729 return;
730 }
731
732 opts = qdict_new();
733 qdict_put_obj(opts, "driver", QOBJECT(qstring_from_str("blkdebug")));
734
735 QINCREF(bs->file->full_open_options);
736 qdict_put_obj(opts, "image", QOBJECT(bs->file->full_open_options));
737
738 for (event = 0; event < BLKDBG_EVENT_MAX; event++) {
739 QLIST_FOREACH(rule, &s->rules[event], next) {
740 if (rule->action == ACTION_INJECT_ERROR) {
741 QDict *inject_error = qdict_new();
742
743 qdict_put_obj(inject_error, "event", QOBJECT(qstring_from_str(
744 BlkdebugEvent_lookup[rule->event])));
745 qdict_put_obj(inject_error, "state",
746 QOBJECT(qint_from_int(rule->state)));
747 qdict_put_obj(inject_error, "errno", QOBJECT(qint_from_int(
748 rule->options.inject.error)));
749 qdict_put_obj(inject_error, "sector", QOBJECT(qint_from_int(
750 rule->options.inject.sector)));
751 qdict_put_obj(inject_error, "once", QOBJECT(qbool_from_int(
752 rule->options.inject.once)));
753 qdict_put_obj(inject_error, "immediately",
754 QOBJECT(qbool_from_int(
755 rule->options.inject.immediately)));
756
757 if (!inject_error_list) {
758 inject_error_list = qlist_new();
759 }
760
761 qlist_append_obj(inject_error_list, QOBJECT(inject_error));
762 } else if (rule->action == ACTION_SET_STATE) {
763 QDict *set_state = qdict_new();
764
765 qdict_put_obj(set_state, "event", QOBJECT(qstring_from_str(
766 BlkdebugEvent_lookup[rule->event])));
767 qdict_put_obj(set_state, "state",
768 QOBJECT(qint_from_int(rule->state)));
769 qdict_put_obj(set_state, "new_state", QOBJECT(qint_from_int(
770 rule->options.set_state.new_state)));
771
772 if (!set_state_list) {
773 set_state_list = qlist_new();
774 }
775
776 qlist_append_obj(set_state_list, QOBJECT(set_state));
777 } else if (rule->action == ACTION_SUSPEND) {
778 QDict *suspend = qdict_new();
779
780 qdict_put_obj(suspend, "event", QOBJECT(qstring_from_str(
781 BlkdebugEvent_lookup[rule->event])));
782 qdict_put_obj(suspend, "state",
783 QOBJECT(qint_from_int(rule->state)));
784 qdict_put_obj(suspend, "tag", QOBJECT(qstring_from_str(
785 rule->options.suspend.tag)));
786
787 if (!suspend_list) {
788 suspend_list = qlist_new();
789 }
790
791 qlist_append_obj(suspend_list, QOBJECT(suspend));
792 }
793 }
794 }
795
796 if (inject_error_list) {
797 qdict_put_obj(opts, "inject-error", QOBJECT(inject_error_list));
798 }
799 if (set_state_list) {
800 qdict_put_obj(opts, "set-state", QOBJECT(set_state_list));
801 }
802 if (suspend_list) {
803 qdict_put_obj(opts, "suspend", QOBJECT(suspend_list));
804 }
805
806 bs->full_open_options = opts;
807}
808
6a143727 809static BlockDriver bdrv_blkdebug = {
f4681212
KW
810 .format_name = "blkdebug",
811 .protocol_name = "blkdebug",
812 .instance_size = sizeof(BDRVBlkdebugState),
6a143727 813
f4681212
KW
814 .bdrv_parse_filename = blkdebug_parse_filename,
815 .bdrv_file_open = blkdebug_open,
816 .bdrv_close = blkdebug_close,
817 .bdrv_getlength = blkdebug_getlength,
2c31b04c 818 .bdrv_refresh_filename = blkdebug_refresh_filename,
6a143727 819
f4681212
KW
820 .bdrv_aio_readv = blkdebug_aio_readv,
821 .bdrv_aio_writev = blkdebug_aio_writev,
9e52c53b 822 .bdrv_aio_flush = blkdebug_aio_flush,
8b9b0cc2 823
3c90c65d
KW
824 .bdrv_debug_event = blkdebug_debug_event,
825 .bdrv_debug_breakpoint = blkdebug_debug_breakpoint,
4cc70e93
FZ
826 .bdrv_debug_remove_breakpoint
827 = blkdebug_debug_remove_breakpoint,
3c90c65d
KW
828 .bdrv_debug_resume = blkdebug_debug_resume,
829 .bdrv_debug_is_suspended = blkdebug_debug_is_suspended,
6a143727
KW
830};
831
832static void bdrv_blkdebug_init(void)
833{
834 bdrv_register(&bdrv_blkdebug);
835}
836
837block_init(bdrv_blkdebug_init);