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