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