]> git.proxmox.com Git - mirror_qemu.git/blame - block/blkdebug.c
blkdebug: Add @iotype error option
[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
f4681212 494 ret = 0;
eaf944a4 495out:
036990d7
HR
496 if (ret < 0) {
497 g_free(s->config_file);
498 }
f4681212
KW
499 qemu_opts_del(opts);
500 return ret;
6a143727
KW
501}
502
16789db3
HR
503static int rule_check(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
504 BlkdebugIOType iotype)
b9f66d96
KW
505{
506 BDRVBlkdebugState *s = bs->opaque;
d157ed5f
EB
507 BlkdebugRule *rule = NULL;
508 int error;
509 bool immediately;
510
511 QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) {
512 uint64_t inject_offset = rule->options.inject.offset;
513
16789db3
HR
514 if ((inject_offset == -1 ||
515 (bytes && inject_offset >= offset &&
516 inject_offset < offset + bytes)) &&
517 (rule->options.inject.iotype_mask & (1ull << iotype)))
d157ed5f
EB
518 {
519 break;
520 }
521 }
522
523 if (!rule || !rule->options.inject.error) {
524 return 0;
525 }
526
527 immediately = rule->options.inject.immediately;
528 error = rule->options.inject.error;
b9f66d96 529
571cd43e 530 if (rule->options.inject.once) {
a069e2f1
JS
531 QSIMPLEQ_REMOVE(&s->active_rules, rule, BlkdebugRule, active_next);
532 remove_rule(rule);
b9f66d96
KW
533 }
534
7c3a9985 535 if (!immediately) {
e5c67ab5 536 aio_co_schedule(qemu_get_current_aio_context(), qemu_coroutine_self());
7c3a9985 537 qemu_coroutine_yield();
b9f66d96
KW
538 }
539
7c3a9985 540 return -error;
b9f66d96
KW
541}
542
7c3a9985
KW
543static int coroutine_fn
544blkdebug_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
545 QEMUIOVector *qiov, int flags)
6a143727 546{
d157ed5f 547 int err;
e4780db4 548
e0ef4395
EB
549 /* Sanity check block layer guarantees */
550 assert(QEMU_IS_ALIGNED(offset, bs->bl.request_alignment));
551 assert(QEMU_IS_ALIGNED(bytes, bs->bl.request_alignment));
552 if (bs->bl.max_transfer) {
553 assert(bytes <= bs->bl.max_transfer);
554 }
555
16789db3 556 err = rule_check(bs, offset, bytes, BLKDEBUG_IO_TYPE_READ);
d157ed5f
EB
557 if (err) {
558 return err;
b9f66d96
KW
559 }
560
7c3a9985 561 return bdrv_co_preadv(bs->file, offset, bytes, qiov, flags);
6a143727
KW
562}
563
7c3a9985
KW
564static int coroutine_fn
565blkdebug_co_pwritev(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
566 QEMUIOVector *qiov, int flags)
6a143727 567{
d157ed5f 568 int err;
e4780db4 569
e0ef4395
EB
570 /* Sanity check block layer guarantees */
571 assert(QEMU_IS_ALIGNED(offset, bs->bl.request_alignment));
572 assert(QEMU_IS_ALIGNED(bytes, bs->bl.request_alignment));
573 if (bs->bl.max_transfer) {
574 assert(bytes <= bs->bl.max_transfer);
575 }
576
16789db3 577 err = rule_check(bs, offset, bytes, BLKDEBUG_IO_TYPE_WRITE);
d157ed5f
EB
578 if (err) {
579 return err;
b9f66d96
KW
580 }
581
7c3a9985 582 return bdrv_co_pwritev(bs->file, offset, bytes, qiov, flags);
6a143727
KW
583}
584
7c3a9985 585static int blkdebug_co_flush(BlockDriverState *bs)
9e52c53b 586{
16789db3 587 int err = rule_check(bs, 0, 0, BLKDEBUG_IO_TYPE_FLUSH);
9e52c53b 588
d157ed5f
EB
589 if (err) {
590 return err;
9e52c53b
PB
591 }
592
7c3a9985 593 return bdrv_co_flush(bs->file->bs);
9e52c53b
PB
594}
595
63188c24 596static int coroutine_fn blkdebug_co_pwrite_zeroes(BlockDriverState *bs,
f5a5ca79 597 int64_t offset, int bytes,
63188c24
EB
598 BdrvRequestFlags flags)
599{
600 uint32_t align = MAX(bs->bl.request_alignment,
601 bs->bl.pwrite_zeroes_alignment);
602 int err;
603
604 /* Only pass through requests that are larger than requested
605 * preferred alignment (so that we test the fallback to writes on
606 * unaligned portions), and check that the block layer never hands
607 * us anything unaligned that crosses an alignment boundary. */
f5a5ca79 608 if (bytes < align) {
63188c24 609 assert(QEMU_IS_ALIGNED(offset, align) ||
f5a5ca79 610 QEMU_IS_ALIGNED(offset + bytes, align) ||
63188c24 611 DIV_ROUND_UP(offset, align) ==
f5a5ca79 612 DIV_ROUND_UP(offset + bytes, align));
63188c24
EB
613 return -ENOTSUP;
614 }
615 assert(QEMU_IS_ALIGNED(offset, align));
f5a5ca79 616 assert(QEMU_IS_ALIGNED(bytes, align));
63188c24 617 if (bs->bl.max_pwrite_zeroes) {
f5a5ca79 618 assert(bytes <= bs->bl.max_pwrite_zeroes);
63188c24
EB
619 }
620
16789db3 621 err = rule_check(bs, offset, bytes, BLKDEBUG_IO_TYPE_WRITE_ZEROES);
63188c24
EB
622 if (err) {
623 return err;
624 }
625
f5a5ca79 626 return bdrv_co_pwrite_zeroes(bs->file, offset, bytes, flags);
63188c24
EB
627}
628
629static int coroutine_fn blkdebug_co_pdiscard(BlockDriverState *bs,
f5a5ca79 630 int64_t offset, int bytes)
63188c24
EB
631{
632 uint32_t align = bs->bl.pdiscard_alignment;
633 int err;
634
635 /* Only pass through requests that are larger than requested
636 * minimum alignment, and ensure that unaligned requests do not
637 * cross optimum discard boundaries. */
f5a5ca79 638 if (bytes < bs->bl.request_alignment) {
63188c24 639 assert(QEMU_IS_ALIGNED(offset, align) ||
f5a5ca79 640 QEMU_IS_ALIGNED(offset + bytes, align) ||
63188c24 641 DIV_ROUND_UP(offset, align) ==
f5a5ca79 642 DIV_ROUND_UP(offset + bytes, align));
63188c24
EB
643 return -ENOTSUP;
644 }
645 assert(QEMU_IS_ALIGNED(offset, bs->bl.request_alignment));
f5a5ca79
MP
646 assert(QEMU_IS_ALIGNED(bytes, bs->bl.request_alignment));
647 if (align && bytes >= align) {
63188c24 648 assert(QEMU_IS_ALIGNED(offset, align));
f5a5ca79 649 assert(QEMU_IS_ALIGNED(bytes, align));
63188c24
EB
650 }
651 if (bs->bl.max_pdiscard) {
f5a5ca79 652 assert(bytes <= bs->bl.max_pdiscard);
63188c24
EB
653 }
654
16789db3 655 err = rule_check(bs, offset, bytes, BLKDEBUG_IO_TYPE_DISCARD);
63188c24
EB
656 if (err) {
657 return err;
658 }
659
0b9fd3f4 660 return bdrv_co_pdiscard(bs->file, offset, bytes);
63188c24 661}
3c90c65d 662
3e4d0e72
EB
663static int coroutine_fn blkdebug_co_block_status(BlockDriverState *bs,
664 bool want_zero,
665 int64_t offset,
666 int64_t bytes,
667 int64_t *pnum,
668 int64_t *map,
669 BlockDriverState **file)
efa6e2ed 670{
3e4d0e72
EB
671 assert(QEMU_IS_ALIGNED(offset | bytes, bs->bl.request_alignment));
672 return bdrv_co_block_status_from_file(bs, want_zero, offset, bytes,
673 pnum, map, file);
efa6e2ed
EB
674}
675
6a143727
KW
676static void blkdebug_close(BlockDriverState *bs)
677{
678 BDRVBlkdebugState *s = bs->opaque;
8b9b0cc2
KW
679 BlkdebugRule *rule, *next;
680 int i;
681
7fb1cf16 682 for (i = 0; i < BLKDBG__MAX; i++) {
8b9b0cc2 683 QLIST_FOREACH_SAFE(rule, &s->rules[i], next, next) {
9e35542b 684 remove_rule(rule);
8b9b0cc2
KW
685 }
686 }
036990d7
HR
687
688 g_free(s->config_file);
6a143727
KW
689}
690
3c90c65d
KW
691static void suspend_request(BlockDriverState *bs, BlkdebugRule *rule)
692{
693 BDRVBlkdebugState *s = bs->opaque;
694 BlkdebugSuspendedReq r;
695
696 r = (BlkdebugSuspendedReq) {
697 .co = qemu_coroutine_self(),
698 .tag = g_strdup(rule->options.suspend.tag),
699 };
700
701 remove_rule(rule);
702 QLIST_INSERT_HEAD(&s->suspended_reqs, &r, next);
703
20873526
MT
704 if (!qtest_enabled()) {
705 printf("blkdebug: Suspended request '%s'\n", r.tag);
706 }
3c90c65d 707 qemu_coroutine_yield();
20873526
MT
708 if (!qtest_enabled()) {
709 printf("blkdebug: Resuming request '%s'\n", r.tag);
710 }
3c90c65d
KW
711
712 QLIST_REMOVE(&r, next);
713 g_free(r.tag);
714}
715
571cd43e 716static bool process_rule(BlockDriverState *bs, struct BlkdebugRule *rule,
8f96b5be 717 bool injected)
8b9b0cc2
KW
718{
719 BDRVBlkdebugState *s = bs->opaque;
8b9b0cc2
KW
720
721 /* Only process rules for the current state */
8f96b5be 722 if (rule->state && rule->state != s->state) {
571cd43e 723 return injected;
8b9b0cc2
KW
724 }
725
726 /* Take the action */
727 switch (rule->action) {
728 case ACTION_INJECT_ERROR:
571cd43e
PB
729 if (!injected) {
730 QSIMPLEQ_INIT(&s->active_rules);
731 injected = true;
732 }
733 QSIMPLEQ_INSERT_HEAD(&s->active_rules, rule, active_next);
8b9b0cc2
KW
734 break;
735
736 case ACTION_SET_STATE:
8f96b5be 737 s->new_state = rule->options.set_state.new_state;
8b9b0cc2 738 break;
3c90c65d
KW
739
740 case ACTION_SUSPEND:
741 suspend_request(bs, rule);
742 break;
8b9b0cc2 743 }
571cd43e 744 return injected;
8b9b0cc2
KW
745}
746
a31939e6 747static void blkdebug_debug_event(BlockDriverState *bs, BlkdebugEvent event)
8b9b0cc2
KW
748{
749 BDRVBlkdebugState *s = bs->opaque;
3c90c65d 750 struct BlkdebugRule *rule, *next;
571cd43e 751 bool injected;
8b9b0cc2 752
7fb1cf16 753 assert((int)event >= 0 && event < BLKDBG__MAX);
8b9b0cc2 754
571cd43e 755 injected = false;
8f96b5be 756 s->new_state = s->state;
3c90c65d 757 QLIST_FOREACH_SAFE(rule, &s->rules[event], next, next) {
8f96b5be 758 injected = process_rule(bs, rule, injected);
8b9b0cc2 759 }
8f96b5be 760 s->state = s->new_state;
8b9b0cc2
KW
761}
762
3c90c65d
KW
763static int blkdebug_debug_breakpoint(BlockDriverState *bs, const char *event,
764 const char *tag)
765{
766 BDRVBlkdebugState *s = bs->opaque;
767 struct BlkdebugRule *rule;
f9509d15 768 int blkdebug_event;
3c90c65d 769
f7abe0ec 770 blkdebug_event = qapi_enum_parse(&BlkdebugEvent_lookup, event, -1, NULL);
f9509d15 771 if (blkdebug_event < 0) {
3c90c65d
KW
772 return -ENOENT;
773 }
774
3c90c65d
KW
775 rule = g_malloc(sizeof(*rule));
776 *rule = (struct BlkdebugRule) {
777 .event = blkdebug_event,
778 .action = ACTION_SUSPEND,
779 .state = 0,
780 .options.suspend.tag = g_strdup(tag),
781 };
782
783 QLIST_INSERT_HEAD(&s->rules[blkdebug_event], rule, next);
784
785 return 0;
786}
787
788static int blkdebug_debug_resume(BlockDriverState *bs, const char *tag)
789{
790 BDRVBlkdebugState *s = bs->opaque;
c547e564 791 BlkdebugSuspendedReq *r, *next;
3c90c65d 792
c547e564 793 QLIST_FOREACH_SAFE(r, &s->suspended_reqs, next, next) {
3c90c65d 794 if (!strcmp(r->tag, tag)) {
0b8b8753 795 qemu_coroutine_enter(r->co);
3c90c65d
KW
796 return 0;
797 }
798 }
799 return -ENOENT;
800}
801
4cc70e93
FZ
802static int blkdebug_debug_remove_breakpoint(BlockDriverState *bs,
803 const char *tag)
804{
805 BDRVBlkdebugState *s = bs->opaque;
c547e564 806 BlkdebugSuspendedReq *r, *r_next;
4cc70e93
FZ
807 BlkdebugRule *rule, *next;
808 int i, ret = -ENOENT;
809
7fb1cf16 810 for (i = 0; i < BLKDBG__MAX; i++) {
4cc70e93
FZ
811 QLIST_FOREACH_SAFE(rule, &s->rules[i], next, next) {
812 if (rule->action == ACTION_SUSPEND &&
813 !strcmp(rule->options.suspend.tag, tag)) {
814 remove_rule(rule);
815 ret = 0;
816 }
817 }
818 }
c547e564 819 QLIST_FOREACH_SAFE(r, &s->suspended_reqs, next, r_next) {
4cc70e93 820 if (!strcmp(r->tag, tag)) {
0b8b8753 821 qemu_coroutine_enter(r->co);
4cc70e93
FZ
822 ret = 0;
823 }
824 }
825 return ret;
826}
3c90c65d
KW
827
828static bool blkdebug_debug_is_suspended(BlockDriverState *bs, const char *tag)
829{
830 BDRVBlkdebugState *s = bs->opaque;
831 BlkdebugSuspendedReq *r;
832
833 QLIST_FOREACH(r, &s->suspended_reqs, next) {
834 if (!strcmp(r->tag, tag)) {
835 return true;
836 }
837 }
838 return false;
839}
840
e1302255
PB
841static int64_t blkdebug_getlength(BlockDriverState *bs)
842{
9a4f4c31 843 return bdrv_getlength(bs->file->bs);
e1302255
PB
844}
845
998b3a1e 846static void blkdebug_refresh_filename(BlockDriverState *bs)
2c31b04c 847{
036990d7 848 BDRVBlkdebugState *s = bs->opaque;
8779441b 849 const QDictEntry *e;
998b3a1e 850 int ret;
2c31b04c 851
998b3a1e 852 if (!bs->file->bs->exact_filename[0]) {
2c31b04c
HR
853 return;
854 }
855
998b3a1e
HR
856 for (e = qdict_first(bs->full_open_options); e;
857 e = qdict_next(bs->full_open_options, e))
858 {
859 /* Real child options are under "image", but "x-image" may
860 * contain a filename */
861 if (strcmp(qdict_entry_key(e), "config") &&
862 strcmp(qdict_entry_key(e), "image") &&
863 strcmp(qdict_entry_key(e), "x-image") &&
864 strcmp(qdict_entry_key(e), "driver"))
865 {
866 return;
de81d72d 867 }
8779441b
HR
868 }
869
998b3a1e
HR
870 ret = snprintf(bs->exact_filename, sizeof(bs->exact_filename),
871 "blkdebug:%s:%s",
872 s->config_file ?: "", bs->file->bs->exact_filename);
873 if (ret >= sizeof(bs->exact_filename)) {
874 /* An overflow makes the filename unusable, so do not report any */
875 bs->exact_filename[0] = 0;
2c31b04c 876 }
2c31b04c
HR
877}
878
835db3ee
EB
879static void blkdebug_refresh_limits(BlockDriverState *bs, Error **errp)
880{
881 BDRVBlkdebugState *s = bs->opaque;
882
883 if (s->align) {
a5b8dd2c 884 bs->bl.request_alignment = s->align;
835db3ee 885 }
430b26a8
EB
886 if (s->max_transfer) {
887 bs->bl.max_transfer = s->max_transfer;
888 }
889 if (s->opt_write_zero) {
890 bs->bl.pwrite_zeroes_alignment = s->opt_write_zero;
891 }
892 if (s->max_write_zero) {
893 bs->bl.max_pwrite_zeroes = s->max_write_zero;
894 }
895 if (s->opt_discard) {
896 bs->bl.pdiscard_alignment = s->opt_discard;
897 }
898 if (s->max_discard) {
899 bs->bl.max_pdiscard = s->max_discard;
900 }
835db3ee
EB
901}
902
c5e8bfb7
KW
903static int blkdebug_reopen_prepare(BDRVReopenState *reopen_state,
904 BlockReopenQueue *queue, Error **errp)
905{
906 return 0;
907}
908
2654267c
HR
909static const char *const blkdebug_strong_runtime_opts[] = {
910 "config",
911 "inject-error.",
912 "set-state.",
913 "align",
914 "max-transfer",
915 "opt-write-zero",
916 "max-write-zero",
917 "opt-discard",
918 "max-discard",
919
920 NULL
921};
922
6a143727 923static BlockDriver bdrv_blkdebug = {
f4681212
KW
924 .format_name = "blkdebug",
925 .protocol_name = "blkdebug",
926 .instance_size = sizeof(BDRVBlkdebugState),
d8e12cd3 927 .is_filter = true,
6a143727 928
f4681212
KW
929 .bdrv_parse_filename = blkdebug_parse_filename,
930 .bdrv_file_open = blkdebug_open,
931 .bdrv_close = blkdebug_close,
c5e8bfb7 932 .bdrv_reopen_prepare = blkdebug_reopen_prepare,
d7010dfb
KW
933 .bdrv_child_perm = bdrv_filter_default_perms,
934
f4681212 935 .bdrv_getlength = blkdebug_getlength,
2c31b04c 936 .bdrv_refresh_filename = blkdebug_refresh_filename,
835db3ee 937 .bdrv_refresh_limits = blkdebug_refresh_limits,
6a143727 938
7c3a9985
KW
939 .bdrv_co_preadv = blkdebug_co_preadv,
940 .bdrv_co_pwritev = blkdebug_co_pwritev,
941 .bdrv_co_flush_to_disk = blkdebug_co_flush,
63188c24
EB
942 .bdrv_co_pwrite_zeroes = blkdebug_co_pwrite_zeroes,
943 .bdrv_co_pdiscard = blkdebug_co_pdiscard,
3e4d0e72 944 .bdrv_co_block_status = blkdebug_co_block_status,
8b9b0cc2 945
3c90c65d
KW
946 .bdrv_debug_event = blkdebug_debug_event,
947 .bdrv_debug_breakpoint = blkdebug_debug_breakpoint,
4cc70e93
FZ
948 .bdrv_debug_remove_breakpoint
949 = blkdebug_debug_remove_breakpoint,
3c90c65d
KW
950 .bdrv_debug_resume = blkdebug_debug_resume,
951 .bdrv_debug_is_suspended = blkdebug_debug_is_suspended,
2654267c
HR
952
953 .strong_runtime_opts = blkdebug_strong_runtime_opts,
6a143727
KW
954};
955
956static void bdrv_blkdebug_init(void)
957{
958 bdrv_register(&bdrv_blkdebug);
959}
960
961block_init(bdrv_blkdebug_init);