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