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