]> git.proxmox.com Git - mirror_qemu.git/blame - block/blkdebug.c
block/blkdebug: remove new_state field and instead use a local variable
[mirror_qemu.git] / block / blkdebug.c
CommitLineData
6a143727
KW
1/*
2 * Block protocol for I/O error injection
3 *
63188c24 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"
69c6449f 31#include "block/qdict.h"
1de7afc9 32#include "qemu/module.h"
922a01a0 33#include "qemu/option.h"
69c6449f 34#include "qapi/qapi-visit-block-core.h"
2c31b04c 35#include "qapi/qmp/qdict.h"
69c6449f 36#include "qapi/qmp/qlist.h"
2c31b04c 37#include "qapi/qmp/qstring.h"
69c6449f 38#include "qapi/qobject-input-visitor.h"
20873526 39#include "sysemu/qtest.h"
6a143727
KW
40
41typedef struct BDRVBlkdebugState {
571cd43e 42 int state;
3dc834f8 43 uint64_t align;
430b26a8
EB
44 uint64_t max_transfer;
45 uint64_t opt_write_zero;
46 uint64_t max_write_zero;
47 uint64_t opt_discard;
48 uint64_t max_discard;
3c90c65d 49
69c6449f
HR
50 uint64_t take_child_perms;
51 uint64_t unshare_child_perms;
52
036990d7
HR
53 /* For blkdebug_refresh_filename() */
54 char *config_file;
55
7fb1cf16 56 QLIST_HEAD(, BlkdebugRule) rules[BLKDBG__MAX];
571cd43e 57 QSIMPLEQ_HEAD(, BlkdebugRule) active_rules;
3c90c65d 58 QLIST_HEAD(, BlkdebugSuspendedReq) suspended_reqs;
6a143727
KW
59} BDRVBlkdebugState;
60
b9f66d96 61typedef struct BlkdebugAIOCB {
7c84b1b8 62 BlockAIOCB common;
b9f66d96
KW
63 int ret;
64} BlkdebugAIOCB;
65
3c90c65d
KW
66typedef struct BlkdebugSuspendedReq {
67 Coroutine *co;
68 char *tag;
69 QLIST_ENTRY(BlkdebugSuspendedReq) next;
70} BlkdebugSuspendedReq;
71
8b9b0cc2
KW
72enum {
73 ACTION_INJECT_ERROR,
74 ACTION_SET_STATE,
3c90c65d 75 ACTION_SUSPEND,
51a46368 76 ACTION__MAX,
8b9b0cc2
KW
77};
78
79typedef struct BlkdebugRule {
a31939e6 80 BlkdebugEvent event;
8b9b0cc2
KW
81 int action;
82 int state;
83 union {
84 struct {
16789db3 85 uint64_t iotype_mask;
8b9b0cc2
KW
86 int error;
87 int immediately;
88 int once;
7c3a9985 89 int64_t offset;
8b9b0cc2
KW
90 } inject;
91 struct {
92 int new_state;
93 } set_state;
3c90c65d
KW
94 struct {
95 char *tag;
96 } suspend;
8b9b0cc2
KW
97 } options;
98 QLIST_ENTRY(BlkdebugRule) next;
571cd43e 99 QSIMPLEQ_ENTRY(BlkdebugRule) active_next;
8b9b0cc2
KW
100} BlkdebugRule;
101
16789db3
HR
102QEMU_BUILD_BUG_MSG(BLKDEBUG_IO_TYPE__MAX > 64,
103 "BlkdebugIOType mask does not fit into an uint64_t");
104
8b9b0cc2
KW
105static QemuOptsList inject_error_opts = {
106 .name = "inject-error",
107 .head = QTAILQ_HEAD_INITIALIZER(inject_error_opts.head),
108 .desc = {
109 {
110 .name = "event",
111 .type = QEMU_OPT_STRING,
112 },
113 {
114 .name = "state",
115 .type = QEMU_OPT_NUMBER,
116 },
16789db3
HR
117 {
118 .name = "iotype",
119 .type = QEMU_OPT_STRING,
120 },
8b9b0cc2
KW
121 {
122 .name = "errno",
123 .type = QEMU_OPT_NUMBER,
124 },
e4780db4
PB
125 {
126 .name = "sector",
127 .type = QEMU_OPT_NUMBER,
128 },
8b9b0cc2
KW
129 {
130 .name = "once",
131 .type = QEMU_OPT_BOOL,
132 },
133 {
134 .name = "immediately",
135 .type = QEMU_OPT_BOOL,
136 },
137 { /* end of list */ }
138 },
139};
140
141static QemuOptsList set_state_opts = {
142 .name = "set-state",
327cdad4 143 .head = QTAILQ_HEAD_INITIALIZER(set_state_opts.head),
8b9b0cc2
KW
144 .desc = {
145 {
146 .name = "event",
147 .type = QEMU_OPT_STRING,
148 },
149 {
150 .name = "state",
151 .type = QEMU_OPT_NUMBER,
152 },
153 {
154 .name = "new_state",
155 .type = QEMU_OPT_NUMBER,
156 },
157 { /* end of list */ }
158 },
159};
160
161static QemuOptsList *config_groups[] = {
162 &inject_error_opts,
163 &set_state_opts,
164 NULL
165};
166
8b9b0cc2
KW
167struct add_rule_data {
168 BDRVBlkdebugState *s;
169 int action;
170};
171
28d0de7a 172static int add_rule(void *opaque, QemuOpts *opts, Error **errp)
8b9b0cc2
KW
173{
174 struct add_rule_data *d = opaque;
175 BDRVBlkdebugState *s = d->s;
5f14f31d 176 const char *event_name;
f9509d15 177 int event;
8b9b0cc2 178 struct BlkdebugRule *rule;
7c3a9985 179 int64_t sector;
16789db3
HR
180 BlkdebugIOType iotype;
181 Error *local_error = NULL;
8b9b0cc2
KW
182
183 /* Find the right event for the rule */
184 event_name = qemu_opt_get(opts, "event");
d4362d64 185 if (!event_name) {
8809cfc3 186 error_setg(errp, "Missing event name for rule");
d4362d64 187 return -1;
f9509d15 188 }
f7abe0ec 189 event = qapi_enum_parse(&BlkdebugEvent_lookup, event_name, -1, errp);
f9509d15 190 if (event < 0) {
8b9b0cc2
KW
191 return -1;
192 }
193
194 /* Set attributes common for all actions */
7267c094 195 rule = g_malloc0(sizeof(*rule));
8b9b0cc2
KW
196 *rule = (struct BlkdebugRule) {
197 .event = event,
198 .action = d->action,
199 .state = qemu_opt_get_number(opts, "state", 0),
200 };
201
202 /* Parse action-specific options */
203 switch (d->action) {
204 case ACTION_INJECT_ERROR:
205 rule->options.inject.error = qemu_opt_get_number(opts, "errno", EIO);
206 rule->options.inject.once = qemu_opt_get_bool(opts, "once", 0);
207 rule->options.inject.immediately =
208 qemu_opt_get_bool(opts, "immediately", 0);
7c3a9985
KW
209 sector = qemu_opt_get_number(opts, "sector", -1);
210 rule->options.inject.offset =
211 sector == -1 ? -1 : sector * BDRV_SECTOR_SIZE;
16789db3
HR
212
213 iotype = qapi_enum_parse(&BlkdebugIOType_lookup,
214 qemu_opt_get(opts, "iotype"),
215 BLKDEBUG_IO_TYPE__MAX, &local_error);
216 if (local_error) {
217 error_propagate(errp, local_error);
5b4c95d0 218 g_free(rule);
16789db3
HR
219 return -1;
220 }
221 if (iotype != BLKDEBUG_IO_TYPE__MAX) {
222 rule->options.inject.iotype_mask = (1ull << iotype);
223 } else {
224 /* Apply the default */
225 rule->options.inject.iotype_mask =
226 (1ull << BLKDEBUG_IO_TYPE_READ)
227 | (1ull << BLKDEBUG_IO_TYPE_WRITE)
228 | (1ull << BLKDEBUG_IO_TYPE_WRITE_ZEROES)
229 | (1ull << BLKDEBUG_IO_TYPE_DISCARD)
230 | (1ull << BLKDEBUG_IO_TYPE_FLUSH);
231 }
232
8b9b0cc2
KW
233 break;
234
235 case ACTION_SET_STATE:
236 rule->options.set_state.new_state =
237 qemu_opt_get_number(opts, "new_state", 0);
238 break;
3c90c65d
KW
239
240 case ACTION_SUSPEND:
241 rule->options.suspend.tag =
242 g_strdup(qemu_opt_get(opts, "tag"));
243 break;
8b9b0cc2
KW
244 };
245
246 /* Add the rule */
247 QLIST_INSERT_HEAD(&s->rules[event], rule, next);
248
249 return 0;
250}
251
9e35542b
KW
252static void remove_rule(BlkdebugRule *rule)
253{
254 switch (rule->action) {
255 case ACTION_INJECT_ERROR:
256 case ACTION_SET_STATE:
257 break;
3c90c65d
KW
258 case ACTION_SUSPEND:
259 g_free(rule->options.suspend.tag);
260 break;
9e35542b
KW
261 }
262
263 QLIST_REMOVE(rule, next);
264 g_free(rule);
265}
266
89f2b21e
HR
267static int read_config(BDRVBlkdebugState *s, const char *filename,
268 QDict *options, Error **errp)
8b9b0cc2 269{
85a040e5 270 FILE *f = NULL;
8b9b0cc2
KW
271 int ret;
272 struct add_rule_data d;
89f2b21e 273 Error *local_err = NULL;
8b9b0cc2 274
85a040e5
HR
275 if (filename) {
276 f = fopen(filename, "r");
277 if (f == NULL) {
278 error_setg_errno(errp, errno, "Could not read blkdebug config file");
279 return -errno;
280 }
8b9b0cc2 281
f7544edc 282 ret = qemu_config_parse(f, config_groups, filename, errp);
85a040e5 283 if (ret < 0) {
85a040e5
HR
284 goto fail;
285 }
8b9b0cc2
KW
286 }
287
89f2b21e 288 qemu_config_parse_qdict(options, config_groups, &local_err);
84d18f06 289 if (local_err) {
89f2b21e
HR
290 error_propagate(errp, local_err);
291 ret = -EINVAL;
292 goto fail;
293 }
294
8b9b0cc2
KW
295 d.s = s;
296 d.action = ACTION_INJECT_ERROR;
8809cfc3 297 qemu_opts_foreach(&inject_error_opts, add_rule, &d, &local_err);
d4362d64
SH
298 if (local_err) {
299 error_propagate(errp, local_err);
300 ret = -EINVAL;
301 goto fail;
302 }
8b9b0cc2
KW
303
304 d.action = ACTION_SET_STATE;
8809cfc3 305 qemu_opts_foreach(&set_state_opts, add_rule, &d, &local_err);
d4362d64
SH
306 if (local_err) {
307 error_propagate(errp, local_err);
308 ret = -EINVAL;
309 goto fail;
310 }
8b9b0cc2
KW
311
312 ret = 0;
313fail:
698f0d52
KW
314 qemu_opts_reset(&inject_error_opts);
315 qemu_opts_reset(&set_state_opts);
85a040e5
HR
316 if (f) {
317 fclose(f);
318 }
8b9b0cc2
KW
319 return ret;
320}
321
322/* Valid blkdebug filenames look like blkdebug:path/to/config:path/to/image */
f4681212
KW
323static void blkdebug_parse_filename(const char *filename, QDict *options,
324 Error **errp)
6a143727 325{
f4681212 326 const char *c;
6a143727 327
8b9b0cc2 328 /* Parse the blkdebug: prefix */
f4681212 329 if (!strstart(filename, "blkdebug:", &filename)) {
d4881b9b
HR
330 /* There was no prefix; therefore, all options have to be already
331 present in the QDict (except for the filename) */
46f5ac20 332 qdict_put_str(options, "x-image", filename);
f4681212 333 return;
6a143727 334 }
6a143727 335
f4681212 336 /* Parse config file path */
8b9b0cc2
KW
337 c = strchr(filename, ':');
338 if (c == NULL) {
f4681212
KW
339 error_setg(errp, "blkdebug requires both config file and image path");
340 return;
8b9b0cc2
KW
341 }
342
f4681212
KW
343 if (c != filename) {
344 QString *config_path;
ba891d68 345 config_path = qstring_from_substr(filename, 0, c - filename);
f4681212 346 qdict_put(options, "config", config_path);
8b9b0cc2 347 }
f4681212
KW
348
349 /* TODO Allow multi-level nesting and set file.filename here */
8b9b0cc2 350 filename = c + 1;
46f5ac20 351 qdict_put_str(options, "x-image", filename);
f4681212
KW
352}
353
69c6449f
HR
354static int blkdebug_parse_perm_list(uint64_t *dest, QDict *options,
355 const char *prefix, Error **errp)
356{
357 int ret = 0;
358 QDict *subqdict = NULL;
359 QObject *crumpled_subqdict = NULL;
360 Visitor *v = NULL;
361 BlockPermissionList *perm_list = NULL, *element;
69c6449f
HR
362
363 *dest = 0;
364
365 qdict_extract_subqdict(options, &subqdict, prefix);
366 if (!qdict_size(subqdict)) {
367 goto out;
368 }
369
370 crumpled_subqdict = qdict_crumple(subqdict, errp);
371 if (!crumpled_subqdict) {
372 ret = -EINVAL;
373 goto out;
374 }
375
376 v = qobject_input_visitor_new(crumpled_subqdict);
af175e85 377 if (!visit_type_BlockPermissionList(v, NULL, &perm_list, errp)) {
69c6449f
HR
378 ret = -EINVAL;
379 goto out;
380 }
381
382 for (element = perm_list; element; element = element->next) {
383 *dest |= bdrv_qapi_perm_to_blk_perm(element->value);
384 }
385
386out:
387 qapi_free_BlockPermissionList(perm_list);
388 visit_free(v);
389 qobject_unref(subqdict);
390 qobject_unref(crumpled_subqdict);
391 return ret;
392}
393
394static int blkdebug_parse_perms(BDRVBlkdebugState *s, QDict *options,
395 Error **errp)
396{
397 int ret;
398
399 ret = blkdebug_parse_perm_list(&s->take_child_perms, options,
400 "take-child-perms.", errp);
401 if (ret < 0) {
402 return ret;
403 }
404
405 ret = blkdebug_parse_perm_list(&s->unshare_child_perms, options,
406 "unshare-child-perms.", errp);
407 if (ret < 0) {
408 return ret;
409 }
410
411 return 0;
412}
413
f4681212
KW
414static QemuOptsList runtime_opts = {
415 .name = "blkdebug",
416 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
417 .desc = {
418 {
419 .name = "config",
420 .type = QEMU_OPT_STRING,
421 .help = "Path to the configuration file",
422 },
423 {
424 .name = "x-image",
425 .type = QEMU_OPT_STRING,
426 .help = "[internal use only, will be removed]",
427 },
b35ee7fb
KW
428 {
429 .name = "align",
430 .type = QEMU_OPT_SIZE,
431 .help = "Required alignment in bytes",
432 },
430b26a8
EB
433 {
434 .name = "max-transfer",
435 .type = QEMU_OPT_SIZE,
436 .help = "Maximum transfer size in bytes",
437 },
438 {
439 .name = "opt-write-zero",
440 .type = QEMU_OPT_SIZE,
441 .help = "Optimum write zero alignment in bytes",
442 },
443 {
444 .name = "max-write-zero",
445 .type = QEMU_OPT_SIZE,
446 .help = "Maximum write zero size in bytes",
447 },
448 {
449 .name = "opt-discard",
450 .type = QEMU_OPT_SIZE,
451 .help = "Optimum discard alignment in bytes",
452 },
453 {
454 .name = "max-discard",
455 .type = QEMU_OPT_SIZE,
456 .help = "Maximum discard size in bytes",
457 },
f4681212
KW
458 { /* end of list */ }
459 },
460};
461
015a1036
HR
462static int blkdebug_open(BlockDriverState *bs, QDict *options, int flags,
463 Error **errp)
f4681212
KW
464{
465 BDRVBlkdebugState *s = bs->opaque;
466 QemuOpts *opts;
f4681212 467 int ret;
430b26a8 468 uint64_t align;
f4681212 469
87ea75d5 470 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
af175e85 471 if (!qemu_opts_absorb_qdict(opts, options, errp)) {
f4681212 472 ret = -EINVAL;
eaf944a4 473 goto out;
f4681212
KW
474 }
475
89f2b21e 476 /* Read rules from config file or command line options */
036990d7
HR
477 s->config_file = g_strdup(qemu_opt_get(opts, "config"));
478 ret = read_config(s, s->config_file, options, errp);
85a040e5 479 if (ret) {
eaf944a4 480 goto out;
f4681212 481 }
8b9b0cc2 482
8db520ce 483 /* Set initial state */
571cd43e 484 s->state = 1;
8db520ce 485
69c6449f
HR
486 /* Parse permissions modifiers before opening the image file */
487 ret = blkdebug_parse_perms(s, options, errp);
488 if (ret < 0) {
489 goto out;
490 }
491
6b826af7 492 /* Open the image file */
9a4f4c31 493 bs->file = bdrv_open_child(qemu_opt_get(opts, "x-image"), options, "image",
b3af2af4
HR
494 bs, &child_of_bds,
495 BDRV_CHILD_FILTERED | BDRV_CHILD_PRIMARY,
bc520249
VSO
496 false, errp);
497 if (!bs->file) {
9a4f4c31 498 ret = -EINVAL;
eaf944a4 499 goto out;
8b9b0cc2
KW
500 }
501
228345bf
HR
502 bs->supported_write_flags = BDRV_REQ_WRITE_UNCHANGED |
503 (BDRV_REQ_FUA & bs->file->bs->supported_write_flags);
504 bs->supported_zero_flags = BDRV_REQ_WRITE_UNCHANGED |
80f5c33f 505 ((BDRV_REQ_FUA | BDRV_REQ_MAY_UNMAP | BDRV_REQ_NO_FALLBACK) &
228345bf 506 bs->file->bs->supported_zero_flags);
3dc834f8 507 ret = -EINVAL;
63188c24 508
430b26a8 509 /* Set alignment overrides */
3dc834f8
EB
510 s->align = qemu_opt_get_size(opts, "align", 0);
511 if (s->align && (s->align >= INT_MAX || !is_power_of_2(s->align))) {
512 error_setg(errp, "Cannot meet constraints with align %" PRIu64,
513 s->align);
de234897 514 goto out;
b35ee7fb 515 }
430b26a8
EB
516 align = MAX(s->align, bs->file->bs->bl.request_alignment);
517
518 s->max_transfer = qemu_opt_get_size(opts, "max-transfer", 0);
519 if (s->max_transfer &&
520 (s->max_transfer >= INT_MAX ||
521 !QEMU_IS_ALIGNED(s->max_transfer, align))) {
522 error_setg(errp, "Cannot meet constraints with max-transfer %" PRIu64,
523 s->max_transfer);
524 goto out;
525 }
526
527 s->opt_write_zero = qemu_opt_get_size(opts, "opt-write-zero", 0);
528 if (s->opt_write_zero &&
529 (s->opt_write_zero >= INT_MAX ||
530 !QEMU_IS_ALIGNED(s->opt_write_zero, align))) {
531 error_setg(errp, "Cannot meet constraints with opt-write-zero %" PRIu64,
532 s->opt_write_zero);
533 goto out;
534 }
535
536 s->max_write_zero = qemu_opt_get_size(opts, "max-write-zero", 0);
537 if (s->max_write_zero &&
538 (s->max_write_zero >= INT_MAX ||
539 !QEMU_IS_ALIGNED(s->max_write_zero,
540 MAX(s->opt_write_zero, align)))) {
541 error_setg(errp, "Cannot meet constraints with max-write-zero %" PRIu64,
542 s->max_write_zero);
543 goto out;
544 }
545
546 s->opt_discard = qemu_opt_get_size(opts, "opt-discard", 0);
547 if (s->opt_discard &&
548 (s->opt_discard >= INT_MAX ||
549 !QEMU_IS_ALIGNED(s->opt_discard, align))) {
550 error_setg(errp, "Cannot meet constraints with opt-discard %" PRIu64,
551 s->opt_discard);
552 goto out;
553 }
554
555 s->max_discard = qemu_opt_get_size(opts, "max-discard", 0);
556 if (s->max_discard &&
557 (s->max_discard >= INT_MAX ||
558 !QEMU_IS_ALIGNED(s->max_discard,
559 MAX(s->opt_discard, align)))) {
560 error_setg(errp, "Cannot meet constraints with max-discard %" PRIu64,
561 s->max_discard);
562 goto out;
563 }
b35ee7fb 564
f8cec157
HR
565 bdrv_debug_event(bs, BLKDBG_NONE);
566
f4681212 567 ret = 0;
eaf944a4 568out:
036990d7
HR
569 if (ret < 0) {
570 g_free(s->config_file);
571 }
f4681212
KW
572 qemu_opts_del(opts);
573 return ret;
6a143727
KW
574}
575
16789db3
HR
576static int rule_check(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
577 BlkdebugIOType iotype)
b9f66d96
KW
578{
579 BDRVBlkdebugState *s = bs->opaque;
d157ed5f
EB
580 BlkdebugRule *rule = NULL;
581 int error;
582 bool immediately;
583
584 QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) {
585 uint64_t inject_offset = rule->options.inject.offset;
586
16789db3
HR
587 if ((inject_offset == -1 ||
588 (bytes && inject_offset >= offset &&
589 inject_offset < offset + bytes)) &&
590 (rule->options.inject.iotype_mask & (1ull << iotype)))
d157ed5f
EB
591 {
592 break;
593 }
594 }
595
596 if (!rule || !rule->options.inject.error) {
597 return 0;
598 }
599
600 immediately = rule->options.inject.immediately;
601 error = rule->options.inject.error;
b9f66d96 602
571cd43e 603 if (rule->options.inject.once) {
a069e2f1
JS
604 QSIMPLEQ_REMOVE(&s->active_rules, rule, BlkdebugRule, active_next);
605 remove_rule(rule);
b9f66d96
KW
606 }
607
7c3a9985 608 if (!immediately) {
e5c67ab5 609 aio_co_schedule(qemu_get_current_aio_context(), qemu_coroutine_self());
7c3a9985 610 qemu_coroutine_yield();
b9f66d96
KW
611 }
612
7c3a9985 613 return -error;
b9f66d96
KW
614}
615
7c3a9985
KW
616static int coroutine_fn
617blkdebug_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
618 QEMUIOVector *qiov, int flags)
6a143727 619{
d157ed5f 620 int err;
e4780db4 621
e0ef4395
EB
622 /* Sanity check block layer guarantees */
623 assert(QEMU_IS_ALIGNED(offset, bs->bl.request_alignment));
624 assert(QEMU_IS_ALIGNED(bytes, bs->bl.request_alignment));
625 if (bs->bl.max_transfer) {
626 assert(bytes <= bs->bl.max_transfer);
627 }
628
16789db3 629 err = rule_check(bs, offset, bytes, BLKDEBUG_IO_TYPE_READ);
d157ed5f
EB
630 if (err) {
631 return err;
b9f66d96
KW
632 }
633
7c3a9985 634 return bdrv_co_preadv(bs->file, offset, bytes, qiov, flags);
6a143727
KW
635}
636
7c3a9985
KW
637static int coroutine_fn
638blkdebug_co_pwritev(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
639 QEMUIOVector *qiov, int flags)
6a143727 640{
d157ed5f 641 int err;
e4780db4 642
e0ef4395
EB
643 /* Sanity check block layer guarantees */
644 assert(QEMU_IS_ALIGNED(offset, bs->bl.request_alignment));
645 assert(QEMU_IS_ALIGNED(bytes, bs->bl.request_alignment));
646 if (bs->bl.max_transfer) {
647 assert(bytes <= bs->bl.max_transfer);
648 }
649
16789db3 650 err = rule_check(bs, offset, bytes, BLKDEBUG_IO_TYPE_WRITE);
d157ed5f
EB
651 if (err) {
652 return err;
b9f66d96
KW
653 }
654
7c3a9985 655 return bdrv_co_pwritev(bs->file, offset, bytes, qiov, flags);
6a143727
KW
656}
657
7c3a9985 658static int blkdebug_co_flush(BlockDriverState *bs)
9e52c53b 659{
16789db3 660 int err = rule_check(bs, 0, 0, BLKDEBUG_IO_TYPE_FLUSH);
9e52c53b 661
d157ed5f
EB
662 if (err) {
663 return err;
9e52c53b
PB
664 }
665
7c3a9985 666 return bdrv_co_flush(bs->file->bs);
9e52c53b
PB
667}
668
63188c24 669static int coroutine_fn blkdebug_co_pwrite_zeroes(BlockDriverState *bs,
f5a5ca79 670 int64_t offset, int bytes,
63188c24
EB
671 BdrvRequestFlags flags)
672{
673 uint32_t align = MAX(bs->bl.request_alignment,
674 bs->bl.pwrite_zeroes_alignment);
675 int err;
676
677 /* Only pass through requests that are larger than requested
678 * preferred alignment (so that we test the fallback to writes on
679 * unaligned portions), and check that the block layer never hands
680 * us anything unaligned that crosses an alignment boundary. */
f5a5ca79 681 if (bytes < align) {
63188c24 682 assert(QEMU_IS_ALIGNED(offset, align) ||
f5a5ca79 683 QEMU_IS_ALIGNED(offset + bytes, align) ||
63188c24 684 DIV_ROUND_UP(offset, align) ==
f5a5ca79 685 DIV_ROUND_UP(offset + bytes, align));
63188c24
EB
686 return -ENOTSUP;
687 }
688 assert(QEMU_IS_ALIGNED(offset, align));
f5a5ca79 689 assert(QEMU_IS_ALIGNED(bytes, align));
63188c24 690 if (bs->bl.max_pwrite_zeroes) {
f5a5ca79 691 assert(bytes <= bs->bl.max_pwrite_zeroes);
63188c24
EB
692 }
693
16789db3 694 err = rule_check(bs, offset, bytes, BLKDEBUG_IO_TYPE_WRITE_ZEROES);
63188c24
EB
695 if (err) {
696 return err;
697 }
698
f5a5ca79 699 return bdrv_co_pwrite_zeroes(bs->file, offset, bytes, flags);
63188c24
EB
700}
701
702static int coroutine_fn blkdebug_co_pdiscard(BlockDriverState *bs,
f5a5ca79 703 int64_t offset, int bytes)
63188c24
EB
704{
705 uint32_t align = bs->bl.pdiscard_alignment;
706 int err;
707
708 /* Only pass through requests that are larger than requested
709 * minimum alignment, and ensure that unaligned requests do not
710 * cross optimum discard boundaries. */
f5a5ca79 711 if (bytes < bs->bl.request_alignment) {
63188c24 712 assert(QEMU_IS_ALIGNED(offset, align) ||
f5a5ca79 713 QEMU_IS_ALIGNED(offset + bytes, align) ||
63188c24 714 DIV_ROUND_UP(offset, align) ==
f5a5ca79 715 DIV_ROUND_UP(offset + bytes, align));
63188c24
EB
716 return -ENOTSUP;
717 }
718 assert(QEMU_IS_ALIGNED(offset, bs->bl.request_alignment));
f5a5ca79
MP
719 assert(QEMU_IS_ALIGNED(bytes, bs->bl.request_alignment));
720 if (align && bytes >= align) {
63188c24 721 assert(QEMU_IS_ALIGNED(offset, align));
f5a5ca79 722 assert(QEMU_IS_ALIGNED(bytes, align));
63188c24
EB
723 }
724 if (bs->bl.max_pdiscard) {
f5a5ca79 725 assert(bytes <= bs->bl.max_pdiscard);
63188c24
EB
726 }
727
16789db3 728 err = rule_check(bs, offset, bytes, BLKDEBUG_IO_TYPE_DISCARD);
63188c24
EB
729 if (err) {
730 return err;
731 }
732
0b9fd3f4 733 return bdrv_co_pdiscard(bs->file, offset, bytes);
63188c24 734}
3c90c65d 735
3e4d0e72
EB
736static int coroutine_fn blkdebug_co_block_status(BlockDriverState *bs,
737 bool want_zero,
738 int64_t offset,
739 int64_t bytes,
740 int64_t *pnum,
741 int64_t *map,
742 BlockDriverState **file)
efa6e2ed 743{
1adb0b5e
HR
744 int err;
745
3e4d0e72 746 assert(QEMU_IS_ALIGNED(offset | bytes, bs->bl.request_alignment));
1adb0b5e
HR
747
748 err = rule_check(bs, offset, bytes, BLKDEBUG_IO_TYPE_BLOCK_STATUS);
749 if (err) {
750 return err;
751 }
752
549ec0d9
HR
753 assert(bs->file && bs->file->bs);
754 *pnum = bytes;
755 *map = offset;
756 *file = bs->file->bs;
757 return BDRV_BLOCK_RAW | BDRV_BLOCK_OFFSET_VALID;
efa6e2ed
EB
758}
759
6a143727
KW
760static void blkdebug_close(BlockDriverState *bs)
761{
762 BDRVBlkdebugState *s = bs->opaque;
8b9b0cc2
KW
763 BlkdebugRule *rule, *next;
764 int i;
765
7fb1cf16 766 for (i = 0; i < BLKDBG__MAX; i++) {
8b9b0cc2 767 QLIST_FOREACH_SAFE(rule, &s->rules[i], next, next) {
9e35542b 768 remove_rule(rule);
8b9b0cc2
KW
769 }
770 }
036990d7
HR
771
772 g_free(s->config_file);
6a143727
KW
773}
774
3c90c65d
KW
775static void suspend_request(BlockDriverState *bs, BlkdebugRule *rule)
776{
777 BDRVBlkdebugState *s = bs->opaque;
f48ff5af 778 BlkdebugSuspendedReq *r;
3c90c65d 779
f48ff5af
EGE
780 r = g_new(BlkdebugSuspendedReq, 1);
781
782 r->co = qemu_coroutine_self();
783 r->tag = g_strdup(rule->options.suspend.tag);
3c90c65d
KW
784
785 remove_rule(rule);
f48ff5af 786 QLIST_INSERT_HEAD(&s->suspended_reqs, r, next);
3c90c65d 787
20873526 788 if (!qtest_enabled()) {
f48ff5af 789 printf("blkdebug: Suspended request '%s'\n", r->tag);
20873526 790 }
3c90c65d
KW
791}
792
51a46368 793static void process_rule(BlockDriverState *bs, struct BlkdebugRule *rule,
4153b553 794 int *action_count, int *new_state)
8b9b0cc2
KW
795{
796 BDRVBlkdebugState *s = bs->opaque;
8b9b0cc2
KW
797
798 /* Only process rules for the current state */
8f96b5be 799 if (rule->state && rule->state != s->state) {
51a46368 800 return;
8b9b0cc2
KW
801 }
802
803 /* Take the action */
51a46368 804 action_count[rule->action]++;
8b9b0cc2
KW
805 switch (rule->action) {
806 case ACTION_INJECT_ERROR:
51a46368 807 if (action_count[ACTION_INJECT_ERROR] == 1) {
571cd43e 808 QSIMPLEQ_INIT(&s->active_rules);
571cd43e
PB
809 }
810 QSIMPLEQ_INSERT_HEAD(&s->active_rules, rule, active_next);
8b9b0cc2
KW
811 break;
812
813 case ACTION_SET_STATE:
4153b553 814 *new_state = rule->options.set_state.new_state;
8b9b0cc2 815 break;
3c90c65d
KW
816
817 case ACTION_SUSPEND:
818 suspend_request(bs, rule);
819 break;
8b9b0cc2
KW
820 }
821}
822
a31939e6 823static void blkdebug_debug_event(BlockDriverState *bs, BlkdebugEvent event)
8b9b0cc2
KW
824{
825 BDRVBlkdebugState *s = bs->opaque;
3c90c65d 826 struct BlkdebugRule *rule, *next;
4153b553 827 int new_state;
51a46368 828 int actions_count[ACTION__MAX] = { 0 };
8b9b0cc2 829
7fb1cf16 830 assert((int)event >= 0 && event < BLKDBG__MAX);
8b9b0cc2 831
4153b553 832 new_state = s->state;
3c90c65d 833 QLIST_FOREACH_SAFE(rule, &s->rules[event], next, next) {
4153b553 834 process_rule(bs, rule, actions_count, &new_state);
8b9b0cc2 835 }
4153b553 836 s->state = new_state;
2196c341
EGE
837
838 while (actions_count[ACTION_SUSPEND] > 0) {
839 qemu_coroutine_yield();
840 actions_count[ACTION_SUSPEND]--;
841 }
8b9b0cc2
KW
842}
843
3c90c65d
KW
844static int blkdebug_debug_breakpoint(BlockDriverState *bs, const char *event,
845 const char *tag)
846{
847 BDRVBlkdebugState *s = bs->opaque;
848 struct BlkdebugRule *rule;
f9509d15 849 int blkdebug_event;
3c90c65d 850
f7abe0ec 851 blkdebug_event = qapi_enum_parse(&BlkdebugEvent_lookup, event, -1, NULL);
f9509d15 852 if (blkdebug_event < 0) {
3c90c65d
KW
853 return -ENOENT;
854 }
855
3c90c65d
KW
856 rule = g_malloc(sizeof(*rule));
857 *rule = (struct BlkdebugRule) {
858 .event = blkdebug_event,
859 .action = ACTION_SUSPEND,
860 .state = 0,
861 .options.suspend.tag = g_strdup(tag),
862 };
863
864 QLIST_INSERT_HEAD(&s->rules[blkdebug_event], rule, next);
865
866 return 0;
867}
868
69d0690c 869static int resume_req_by_tag(BDRVBlkdebugState *s, const char *tag, bool all)
3c90c65d 870{
69d0690c 871 BlkdebugSuspendedReq *r;
3c90c65d 872
69d0690c
EGE
873retry:
874 /*
875 * No need for _SAFE, since a different coroutine can remove another node
876 * (not the current one) in this list, and when the current one is removed
877 * the iteration starts back from beginning anyways.
878 */
879 QLIST_FOREACH(r, &s->suspended_reqs, next) {
3c90c65d 880 if (!strcmp(r->tag, tag)) {
f48ff5af
EGE
881 Coroutine *co = r->co;
882
883 if (!qtest_enabled()) {
884 printf("blkdebug: Resuming request '%s'\n", r->tag);
885 }
886
69d0690c 887 QLIST_REMOVE(r, next);
f48ff5af
EGE
888 g_free(r->tag);
889 g_free(r);
890
891 qemu_coroutine_enter(co);
892
69d0690c
EGE
893 if (all) {
894 goto retry;
895 }
3c90c65d
KW
896 return 0;
897 }
898 }
899 return -ENOENT;
900}
901
69d0690c
EGE
902static int blkdebug_debug_resume(BlockDriverState *bs, const char *tag)
903{
904 BDRVBlkdebugState *s = bs->opaque;
905
906 return resume_req_by_tag(s, tag, false);
907}
908
4cc70e93
FZ
909static int blkdebug_debug_remove_breakpoint(BlockDriverState *bs,
910 const char *tag)
911{
912 BDRVBlkdebugState *s = bs->opaque;
4cc70e93
FZ
913 BlkdebugRule *rule, *next;
914 int i, ret = -ENOENT;
915
7fb1cf16 916 for (i = 0; i < BLKDBG__MAX; i++) {
4cc70e93
FZ
917 QLIST_FOREACH_SAFE(rule, &s->rules[i], next, next) {
918 if (rule->action == ACTION_SUSPEND &&
919 !strcmp(rule->options.suspend.tag, tag)) {
920 remove_rule(rule);
921 ret = 0;
922 }
923 }
924 }
69d0690c
EGE
925 if (resume_req_by_tag(s, tag, true) == 0) {
926 ret = 0;
4cc70e93
FZ
927 }
928 return ret;
929}
3c90c65d
KW
930
931static bool blkdebug_debug_is_suspended(BlockDriverState *bs, const char *tag)
932{
933 BDRVBlkdebugState *s = bs->opaque;
934 BlkdebugSuspendedReq *r;
935
936 QLIST_FOREACH(r, &s->suspended_reqs, next) {
937 if (!strcmp(r->tag, tag)) {
938 return true;
939 }
940 }
941 return false;
942}
943
e1302255
PB
944static int64_t blkdebug_getlength(BlockDriverState *bs)
945{
9a4f4c31 946 return bdrv_getlength(bs->file->bs);
e1302255
PB
947}
948
998b3a1e 949static void blkdebug_refresh_filename(BlockDriverState *bs)
2c31b04c 950{
036990d7 951 BDRVBlkdebugState *s = bs->opaque;
8779441b 952 const QDictEntry *e;
998b3a1e 953 int ret;
2c31b04c 954
998b3a1e 955 if (!bs->file->bs->exact_filename[0]) {
2c31b04c
HR
956 return;
957 }
958
998b3a1e
HR
959 for (e = qdict_first(bs->full_open_options); e;
960 e = qdict_next(bs->full_open_options, e))
961 {
962 /* Real child options are under "image", but "x-image" may
963 * contain a filename */
964 if (strcmp(qdict_entry_key(e), "config") &&
965 strcmp(qdict_entry_key(e), "image") &&
966 strcmp(qdict_entry_key(e), "x-image") &&
967 strcmp(qdict_entry_key(e), "driver"))
968 {
969 return;
de81d72d 970 }
8779441b
HR
971 }
972
998b3a1e
HR
973 ret = snprintf(bs->exact_filename, sizeof(bs->exact_filename),
974 "blkdebug:%s:%s",
975 s->config_file ?: "", bs->file->bs->exact_filename);
976 if (ret >= sizeof(bs->exact_filename)) {
977 /* An overflow makes the filename unusable, so do not report any */
978 bs->exact_filename[0] = 0;
2c31b04c 979 }
2c31b04c
HR
980}
981
835db3ee
EB
982static void blkdebug_refresh_limits(BlockDriverState *bs, Error **errp)
983{
984 BDRVBlkdebugState *s = bs->opaque;
985
986 if (s->align) {
a5b8dd2c 987 bs->bl.request_alignment = s->align;
835db3ee 988 }
430b26a8
EB
989 if (s->max_transfer) {
990 bs->bl.max_transfer = s->max_transfer;
991 }
992 if (s->opt_write_zero) {
993 bs->bl.pwrite_zeroes_alignment = s->opt_write_zero;
994 }
995 if (s->max_write_zero) {
996 bs->bl.max_pwrite_zeroes = s->max_write_zero;
997 }
998 if (s->opt_discard) {
999 bs->bl.pdiscard_alignment = s->opt_discard;
1000 }
1001 if (s->max_discard) {
1002 bs->bl.max_pdiscard = s->max_discard;
1003 }
835db3ee
EB
1004}
1005
c5e8bfb7
KW
1006static int blkdebug_reopen_prepare(BDRVReopenState *reopen_state,
1007 BlockReopenQueue *queue, Error **errp)
1008{
1009 return 0;
1010}
1011
69c6449f 1012static void blkdebug_child_perm(BlockDriverState *bs, BdrvChild *c,
bf8e925e 1013 BdrvChildRole role,
69c6449f
HR
1014 BlockReopenQueue *reopen_queue,
1015 uint64_t perm, uint64_t shared,
1016 uint64_t *nperm, uint64_t *nshared)
1017{
1018 BDRVBlkdebugState *s = bs->opaque;
1019
e5d8a406 1020 bdrv_default_perms(bs, c, role, reopen_queue,
69dca43d 1021 perm, shared, nperm, nshared);
69c6449f
HR
1022
1023 *nperm |= s->take_child_perms;
1024 *nshared &= ~s->unshare_child_perms;
1025}
1026
2654267c
HR
1027static const char *const blkdebug_strong_runtime_opts[] = {
1028 "config",
1029 "inject-error.",
1030 "set-state.",
1031 "align",
1032 "max-transfer",
1033 "opt-write-zero",
1034 "max-write-zero",
1035 "opt-discard",
1036 "max-discard",
1037
1038 NULL
1039};
1040
6a143727 1041static BlockDriver bdrv_blkdebug = {
f4681212
KW
1042 .format_name = "blkdebug",
1043 .protocol_name = "blkdebug",
1044 .instance_size = sizeof(BDRVBlkdebugState),
d8e12cd3 1045 .is_filter = true,
6a143727 1046
f4681212
KW
1047 .bdrv_parse_filename = blkdebug_parse_filename,
1048 .bdrv_file_open = blkdebug_open,
1049 .bdrv_close = blkdebug_close,
c5e8bfb7 1050 .bdrv_reopen_prepare = blkdebug_reopen_prepare,
69c6449f 1051 .bdrv_child_perm = blkdebug_child_perm,
d7010dfb 1052
f4681212 1053 .bdrv_getlength = blkdebug_getlength,
2c31b04c 1054 .bdrv_refresh_filename = blkdebug_refresh_filename,
835db3ee 1055 .bdrv_refresh_limits = blkdebug_refresh_limits,
6a143727 1056
7c3a9985
KW
1057 .bdrv_co_preadv = blkdebug_co_preadv,
1058 .bdrv_co_pwritev = blkdebug_co_pwritev,
1059 .bdrv_co_flush_to_disk = blkdebug_co_flush,
63188c24
EB
1060 .bdrv_co_pwrite_zeroes = blkdebug_co_pwrite_zeroes,
1061 .bdrv_co_pdiscard = blkdebug_co_pdiscard,
3e4d0e72 1062 .bdrv_co_block_status = blkdebug_co_block_status,
8b9b0cc2 1063
3c90c65d
KW
1064 .bdrv_debug_event = blkdebug_debug_event,
1065 .bdrv_debug_breakpoint = blkdebug_debug_breakpoint,
4cc70e93
FZ
1066 .bdrv_debug_remove_breakpoint
1067 = blkdebug_debug_remove_breakpoint,
3c90c65d
KW
1068 .bdrv_debug_resume = blkdebug_debug_resume,
1069 .bdrv_debug_is_suspended = blkdebug_debug_is_suspended,
2654267c
HR
1070
1071 .strong_runtime_opts = blkdebug_strong_runtime_opts,
6a143727
KW
1072};
1073
1074static void bdrv_blkdebug_init(void)
1075{
1076 bdrv_register(&bdrv_blkdebug);
1077}
1078
1079block_init(bdrv_blkdebug_init);