zinject.c revision 219089
1/* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21/* 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23 */ 24 25/* 26 * ZFS Fault Injector 27 * 28 * This userland component takes a set of options and uses libzpool to translate 29 * from a user-visible object type and name to an internal representation. 30 * There are two basic types of faults: device faults and data faults. 31 * 32 * 33 * DEVICE FAULTS 34 * 35 * Errors can be injected into a particular vdev using the '-d' option. This 36 * option takes a path or vdev GUID to uniquely identify the device within a 37 * pool. There are two types of errors that can be injected, EIO and ENXIO, 38 * that can be controlled through the '-e' option. The default is ENXIO. For 39 * EIO failures, any attempt to read data from the device will return EIO, but 40 * subsequent attempt to reopen the device will succeed. For ENXIO failures, 41 * any attempt to read from the device will return EIO, but any attempt to 42 * reopen the device will also return ENXIO. 43 * For label faults, the -L option must be specified. This allows faults 44 * to be injected into either the nvlist, uberblock, pad1, or pad2 region 45 * of all the labels for the specified device. 46 * 47 * This form of the command looks like: 48 * 49 * zinject -d device [-e errno] [-L <uber | nvlist | pad1 | pad2>] pool 50 * 51 * 52 * DATA FAULTS 53 * 54 * We begin with a tuple of the form: 55 * 56 * <type,level,range,object> 57 * 58 * type A string describing the type of data to target. Each type 59 * implicitly describes how to interpret 'object'. Currently, 60 * the following values are supported: 61 * 62 * data User data for a file 63 * dnode Dnode for a file or directory 64 * 65 * The following MOS objects are special. Instead of injecting 66 * errors on a particular object or blkid, we inject errors across 67 * all objects of the given type. 68 * 69 * mos Any data in the MOS 70 * mosdir object directory 71 * config pool configuration 72 * bpobj blkptr list 73 * spacemap spacemap 74 * metaslab metaslab 75 * errlog persistent error log 76 * 77 * level Object level. Defaults to '0', not applicable to all types. If 78 * a range is given, this corresponds to the indirect block 79 * corresponding to the specific range. 80 * 81 * range A numerical range [start,end) within the object. Defaults to 82 * the full size of the file. 83 * 84 * object A string describing the logical location of the object. For 85 * files and directories (currently the only supported types), 86 * this is the path of the object on disk. 87 * 88 * This is translated, via libzpool, into the following internal representation: 89 * 90 * <type,objset,object,level,range> 91 * 92 * These types should be self-explanatory. This tuple is then passed to the 93 * kernel via a special ioctl() to initiate fault injection for the given 94 * object. Note that 'type' is not strictly necessary for fault injection, but 95 * is used when translating existing faults into a human-readable string. 96 * 97 * 98 * The command itself takes one of the forms: 99 * 100 * zinject 101 * zinject <-a | -u pool> 102 * zinject -c <id|all> 103 * zinject [-q] <-t type> [-f freq] [-u] [-a] [-m] [-e errno] [-l level] 104 * [-r range] <object> 105 * zinject [-f freq] [-a] [-m] [-u] -b objset:object:level:start:end pool 106 * 107 * With no arguments, the command prints all currently registered injection 108 * handlers, with their numeric identifiers. 109 * 110 * The '-c' option will clear the given handler, or all handlers if 'all' is 111 * specified. 112 * 113 * The '-e' option takes a string describing the errno to simulate. This must 114 * be either 'io' or 'checksum'. In most cases this will result in the same 115 * behavior, but RAID-Z will produce a different set of ereports for this 116 * situation. 117 * 118 * The '-a', '-u', and '-m' flags toggle internal flush behavior. If '-a' is 119 * specified, then the ARC cache is flushed appropriately. If '-u' is 120 * specified, then the underlying SPA is unloaded. Either of these flags can be 121 * specified independently of any other handlers. The '-m' flag automatically 122 * does an unmount and remount of the underlying dataset to aid in flushing the 123 * cache. 124 * 125 * The '-f' flag controls the frequency of errors injected, expressed as a 126 * integer percentage between 1 and 100. The default is 100. 127 * 128 * The this form is responsible for actually injecting the handler into the 129 * framework. It takes the arguments described above, translates them to the 130 * internal tuple using libzpool, and then issues an ioctl() to register the 131 * handler. 132 * 133 * The final form can target a specific bookmark, regardless of whether a 134 * human-readable interface has been designed. It allows developers to specify 135 * a particular block by number. 136 */ 137 138#include <errno.h> 139#include <fcntl.h> 140#include <stdio.h> 141#include <stdlib.h> 142#include <strings.h> 143#include <unistd.h> 144 145#include <sys/fs/zfs.h> 146#include <sys/param.h> 147#include <sys/mount.h> 148 149#include <libzfs.h> 150 151#undef verify /* both libzfs.h and zfs_context.h want to define this */ 152 153#include "zinject.h" 154 155libzfs_handle_t *g_zfs; 156int zfs_fd; 157 158#ifndef ECKSUM 159#define ECKSUM EBADE 160#endif 161 162static const char *errtable[TYPE_INVAL] = { 163 "data", 164 "dnode", 165 "mos", 166 "mosdir", 167 "metaslab", 168 "config", 169 "bpobj", 170 "spacemap", 171 "errlog", 172 "uber", 173 "nvlist", 174 "pad1", 175 "pad2" 176}; 177 178static err_type_t 179name_to_type(const char *arg) 180{ 181 int i; 182 for (i = 0; i < TYPE_INVAL; i++) 183 if (strcmp(errtable[i], arg) == 0) 184 return (i); 185 186 return (TYPE_INVAL); 187} 188 189static const char * 190type_to_name(uint64_t type) 191{ 192 switch (type) { 193 case DMU_OT_OBJECT_DIRECTORY: 194 return ("mosdir"); 195 case DMU_OT_OBJECT_ARRAY: 196 return ("metaslab"); 197 case DMU_OT_PACKED_NVLIST: 198 return ("config"); 199 case DMU_OT_BPOBJ: 200 return ("bpobj"); 201 case DMU_OT_SPACE_MAP: 202 return ("spacemap"); 203 case DMU_OT_ERROR_LOG: 204 return ("errlog"); 205 default: 206 return ("-"); 207 } 208} 209 210 211/* 212 * Print usage message. 213 */ 214void 215usage(void) 216{ 217 (void) printf( 218 "usage:\n" 219 "\n" 220 "\tzinject\n" 221 "\n" 222 "\t\tList all active injection records.\n" 223 "\n" 224 "\tzinject -c <id|all>\n" 225 "\n" 226 "\t\tClear the particular record (if given a numeric ID), or\n" 227 "\t\tall records if 'all' is specificed.\n" 228 "\n" 229 "\tzinject -p <function name> pool\n" 230 "\t\tInject a panic fault at the specified function. Only \n" 231 "\t\tfunctions which call spa_vdev_config_exit(), or \n" 232 "\t\tspa_vdev_exit() will trigger a panic.\n" 233 "\n" 234 "\tzinject -d device [-e errno] [-L <nvlist|uber|pad1|pad2>] [-F]\n" 235 "\t [-T <read|write|free|claim|all> pool\n" 236 "\t\tInject a fault into a particular device or the device's\n" 237 "\t\tlabel. Label injection can either be 'nvlist', 'uber',\n " 238 "\t\t'pad1', or 'pad2'.\n" 239 "\t\t'errno' can be 'nxio' (the default), 'io', or 'dtl'.\n" 240 "\n" 241 "\tzinject -d device -A <degrade|fault> pool\n" 242 "\t\tPerform a specific action on a particular device\n" 243 "\n" 244 "\tzinject -I [-s <seconds> | -g <txgs>] pool\n" 245 "\t\tCause the pool to stop writing blocks yet not\n" 246 "\t\treport errors for a duration. Simulates buggy hardware\n" 247 "\t\tthat fails to honor cache flush requests.\n" 248 "\t\tDefault duration is 30 seconds. The machine is panicked\n" 249 "\t\tat the end of the duration.\n" 250 "\n" 251 "\tzinject -b objset:object:level:blkid pool\n" 252 "\n" 253 "\t\tInject an error into pool 'pool' with the numeric bookmark\n" 254 "\t\tspecified by the remaining tuple. Each number is in\n" 255 "\t\thexidecimal, and only one block can be specified.\n" 256 "\n" 257 "\tzinject [-q] <-t type> [-e errno] [-l level] [-r range]\n" 258 "\t [-a] [-m] [-u] [-f freq] <object>\n" 259 "\n" 260 "\t\tInject an error into the object specified by the '-t' option\n" 261 "\t\tand the object descriptor. The 'object' parameter is\n" 262 "\t\tinterperted depending on the '-t' option.\n" 263 "\n" 264 "\t\t-q\tQuiet mode. Only print out the handler number added.\n" 265 "\t\t-e\tInject a specific error. Must be either 'io' or\n" 266 "\t\t\t'checksum'. Default is 'io'.\n" 267 "\t\t-l\tInject error at a particular block level. Default is " 268 "0.\n" 269 "\t\t-m\tAutomatically remount underlying filesystem.\n" 270 "\t\t-r\tInject error over a particular logical range of an\n" 271 "\t\t\tobject. Will be translated to the appropriate blkid\n" 272 "\t\t\trange according to the object's properties.\n" 273 "\t\t-a\tFlush the ARC cache. Can be specified without any\n" 274 "\t\t\tassociated object.\n" 275 "\t\t-u\tUnload the associated pool. Can be specified with only\n" 276 "\t\t\ta pool object.\n" 277 "\t\t-f\tOnly inject errors a fraction of the time. Expressed as\n" 278 "\t\t\ta percentage between 1 and 100.\n" 279 "\n" 280 "\t-t data\t\tInject an error into the plain file contents of a\n" 281 "\t\t\tfile. The object must be specified as a complete path\n" 282 "\t\t\tto a file on a ZFS filesystem.\n" 283 "\n" 284 "\t-t dnode\tInject an error into the metadnode in the block\n" 285 "\t\t\tcorresponding to the dnode for a file or directory. The\n" 286 "\t\t\t'-r' option is incompatible with this mode. The object\n" 287 "\t\t\tis specified as a complete path to a file or directory\n" 288 "\t\t\ton a ZFS filesystem.\n" 289 "\n" 290 "\t-t <mos>\tInject errors into the MOS for objects of the given\n" 291 "\t\t\ttype. Valid types are: mos, mosdir, config, bpobj,\n" 292 "\t\t\tspacemap, metaslab, errlog. The only valid <object> is\n" 293 "\t\t\tthe poolname.\n"); 294} 295 296static int 297iter_handlers(int (*func)(int, const char *, zinject_record_t *, void *), 298 void *data) 299{ 300 zfs_cmd_t zc; 301 int ret; 302 303 zc.zc_guid = 0; 304 305 while (ioctl(zfs_fd, ZFS_IOC_INJECT_LIST_NEXT, &zc) == 0) 306 if ((ret = func((int)zc.zc_guid, zc.zc_name, 307 &zc.zc_inject_record, data)) != 0) 308 return (ret); 309 310 if (errno != ENOENT) { 311 (void) fprintf(stderr, "Unable to list handlers: %s\n", 312 strerror(errno)); 313 return (-1); 314 } 315 316 return (0); 317} 318 319static int 320print_data_handler(int id, const char *pool, zinject_record_t *record, 321 void *data) 322{ 323 int *count = data; 324 325 if (record->zi_guid != 0 || record->zi_func[0] != '\0') 326 return (0); 327 328 if (*count == 0) { 329 (void) printf("%3s %-15s %-6s %-6s %-8s %3s %-15s\n", 330 "ID", "POOL", "OBJSET", "OBJECT", "TYPE", "LVL", "RANGE"); 331 (void) printf("--- --------------- ------ " 332 "------ -------- --- ---------------\n"); 333 } 334 335 *count += 1; 336 337 (void) printf("%3d %-15s %-6llu %-6llu %-8s %3d ", id, pool, 338 (u_longlong_t)record->zi_objset, (u_longlong_t)record->zi_object, 339 type_to_name(record->zi_type), record->zi_level); 340 341 if (record->zi_start == 0 && 342 record->zi_end == -1ULL) 343 (void) printf("all\n"); 344 else 345 (void) printf("[%llu, %llu]\n", (u_longlong_t)record->zi_start, 346 (u_longlong_t)record->zi_end); 347 348 return (0); 349} 350 351static int 352print_device_handler(int id, const char *pool, zinject_record_t *record, 353 void *data) 354{ 355 int *count = data; 356 357 if (record->zi_guid == 0 || record->zi_func[0] != '\0') 358 return (0); 359 360 if (*count == 0) { 361 (void) printf("%3s %-15s %s\n", "ID", "POOL", "GUID"); 362 (void) printf("--- --------------- ----------------\n"); 363 } 364 365 *count += 1; 366 367 (void) printf("%3d %-15s %llx\n", id, pool, 368 (u_longlong_t)record->zi_guid); 369 370 return (0); 371} 372 373static int 374print_panic_handler(int id, const char *pool, zinject_record_t *record, 375 void *data) 376{ 377 int *count = data; 378 379 if (record->zi_func[0] == '\0') 380 return (0); 381 382 if (*count == 0) { 383 (void) printf("%3s %-15s %s\n", "ID", "POOL", "FUNCTION"); 384 (void) printf("--- --------------- ----------------\n"); 385 } 386 387 *count += 1; 388 389 (void) printf("%3d %-15s %s\n", id, pool, record->zi_func); 390 391 return (0); 392} 393 394/* 395 * Print all registered error handlers. Returns the number of handlers 396 * registered. 397 */ 398static int 399print_all_handlers(void) 400{ 401 int count = 0, total = 0; 402 403 (void) iter_handlers(print_device_handler, &count); 404 if (count > 0) { 405 total += count; 406 (void) printf("\n"); 407 count = 0; 408 } 409 410 (void) iter_handlers(print_data_handler, &count); 411 if (count > 0) { 412 total += count; 413 (void) printf("\n"); 414 count = 0; 415 } 416 417 (void) iter_handlers(print_panic_handler, &count); 418 419 return (count + total); 420} 421 422/* ARGSUSED */ 423static int 424cancel_one_handler(int id, const char *pool, zinject_record_t *record, 425 void *data) 426{ 427 zfs_cmd_t zc; 428 429 zc.zc_guid = (uint64_t)id; 430 431 if (ioctl(zfs_fd, ZFS_IOC_CLEAR_FAULT, &zc) != 0) { 432 (void) fprintf(stderr, "failed to remove handler %d: %s\n", 433 id, strerror(errno)); 434 return (1); 435 } 436 437 return (0); 438} 439 440/* 441 * Remove all fault injection handlers. 442 */ 443static int 444cancel_all_handlers(void) 445{ 446 int ret = iter_handlers(cancel_one_handler, NULL); 447 448 if (ret == 0) 449 (void) printf("removed all registered handlers\n"); 450 451 return (ret); 452} 453 454/* 455 * Remove a specific fault injection handler. 456 */ 457static int 458cancel_handler(int id) 459{ 460 zfs_cmd_t zc; 461 462 zc.zc_guid = (uint64_t)id; 463 464 if (ioctl(zfs_fd, ZFS_IOC_CLEAR_FAULT, &zc) != 0) { 465 (void) fprintf(stderr, "failed to remove handler %d: %s\n", 466 id, strerror(errno)); 467 return (1); 468 } 469 470 (void) printf("removed handler %d\n", id); 471 472 return (0); 473} 474 475/* 476 * Register a new fault injection handler. 477 */ 478static int 479register_handler(const char *pool, int flags, zinject_record_t *record, 480 int quiet) 481{ 482 zfs_cmd_t zc; 483 484 (void) strcpy(zc.zc_name, pool); 485 zc.zc_inject_record = *record; 486 zc.zc_guid = flags; 487 488 if (ioctl(zfs_fd, ZFS_IOC_INJECT_FAULT, &zc) != 0) { 489 (void) fprintf(stderr, "failed to add handler: %s\n", 490 strerror(errno)); 491 return (1); 492 } 493 494 if (flags & ZINJECT_NULL) 495 return (0); 496 497 if (quiet) { 498 (void) printf("%llu\n", (u_longlong_t)zc.zc_guid); 499 } else { 500 (void) printf("Added handler %llu with the following " 501 "properties:\n", (u_longlong_t)zc.zc_guid); 502 (void) printf(" pool: %s\n", pool); 503 if (record->zi_guid) { 504 (void) printf(" vdev: %llx\n", 505 (u_longlong_t)record->zi_guid); 506 } else if (record->zi_func[0] != '\0') { 507 (void) printf(" panic function: %s\n", 508 record->zi_func); 509 } else if (record->zi_duration > 0) { 510 (void) printf(" time: %lld seconds\n", 511 (u_longlong_t)record->zi_duration); 512 } else if (record->zi_duration < 0) { 513 (void) printf(" txgs: %lld \n", 514 (u_longlong_t)-record->zi_duration); 515 } else { 516 (void) printf("objset: %llu\n", 517 (u_longlong_t)record->zi_objset); 518 (void) printf("object: %llu\n", 519 (u_longlong_t)record->zi_object); 520 (void) printf(" type: %llu\n", 521 (u_longlong_t)record->zi_type); 522 (void) printf(" level: %d\n", record->zi_level); 523 if (record->zi_start == 0 && 524 record->zi_end == -1ULL) 525 (void) printf(" range: all\n"); 526 else 527 (void) printf(" range: [%llu, %llu)\n", 528 (u_longlong_t)record->zi_start, 529 (u_longlong_t)record->zi_end); 530 } 531 } 532 533 return (0); 534} 535 536int 537perform_action(const char *pool, zinject_record_t *record, int cmd) 538{ 539 zfs_cmd_t zc; 540 541 ASSERT(cmd == VDEV_STATE_DEGRADED || cmd == VDEV_STATE_FAULTED); 542 (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name)); 543 zc.zc_guid = record->zi_guid; 544 zc.zc_cookie = cmd; 545 546 if (ioctl(zfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 547 return (0); 548 549 return (1); 550} 551 552int 553main(int argc, char **argv) 554{ 555 int c; 556 char *range = NULL; 557 char *cancel = NULL; 558 char *end; 559 char *raw = NULL; 560 char *device = NULL; 561 int level = 0; 562 int quiet = 0; 563 int error = 0; 564 int domount = 0; 565 int io_type = ZIO_TYPES; 566 int action = VDEV_STATE_UNKNOWN; 567 err_type_t type = TYPE_INVAL; 568 err_type_t label = TYPE_INVAL; 569 zinject_record_t record = { 0 }; 570 char pool[MAXNAMELEN]; 571 char dataset[MAXNAMELEN]; 572 zfs_handle_t *zhp; 573 int nowrites = 0; 574 int dur_txg = 0; 575 int dur_secs = 0; 576 int ret; 577 int flags = 0; 578 579 if ((g_zfs = libzfs_init()) == NULL) { 580 (void) fprintf(stderr, "internal error: failed to " 581 "initialize ZFS library\n"); 582 return (1); 583 } 584 585 libzfs_print_on_error(g_zfs, B_TRUE); 586 587 if ((zfs_fd = open(ZFS_DEV, O_RDWR)) < 0) { 588 (void) fprintf(stderr, "failed to open ZFS device\n"); 589 return (1); 590 } 591 592 if (argc == 1) { 593 /* 594 * No arguments. Print the available handlers. If there are no 595 * available handlers, direct the user to '-h' for help 596 * information. 597 */ 598 if (print_all_handlers() == 0) { 599 (void) printf("No handlers registered.\n"); 600 (void) printf("Run 'zinject -h' for usage " 601 "information.\n"); 602 } 603 604 return (0); 605 } 606 607 while ((c = getopt(argc, argv, 608 ":aA:b:d:f:Fg:qhIc:t:T:l:mr:s:e:uL:p:")) != -1) { 609 switch (c) { 610 case 'a': 611 flags |= ZINJECT_FLUSH_ARC; 612 break; 613 case 'A': 614 if (strcasecmp(optarg, "degrade") == 0) { 615 action = VDEV_STATE_DEGRADED; 616 } else if (strcasecmp(optarg, "fault") == 0) { 617 action = VDEV_STATE_FAULTED; 618 } else { 619 (void) fprintf(stderr, "invalid action '%s': " 620 "must be 'degrade' or 'fault'\n", optarg); 621 usage(); 622 return (1); 623 } 624 break; 625 case 'b': 626 raw = optarg; 627 break; 628 case 'c': 629 cancel = optarg; 630 break; 631 case 'd': 632 device = optarg; 633 break; 634 case 'e': 635 if (strcasecmp(optarg, "io") == 0) { 636 error = EIO; 637 } else if (strcasecmp(optarg, "checksum") == 0) { 638 error = ECKSUM; 639 } else if (strcasecmp(optarg, "nxio") == 0) { 640 error = ENXIO; 641 } else if (strcasecmp(optarg, "dtl") == 0) { 642 error = ECHILD; 643 } else { 644 (void) fprintf(stderr, "invalid error type " 645 "'%s': must be 'io', 'checksum' or " 646 "'nxio'\n", optarg); 647 usage(); 648 return (1); 649 } 650 break; 651 case 'f': 652 record.zi_freq = atoi(optarg); 653 if (record.zi_freq < 1 || record.zi_freq > 100) { 654 (void) fprintf(stderr, "frequency range must " 655 "be in the range (0, 100]\n"); 656 return (1); 657 } 658 break; 659 case 'F': 660 record.zi_failfast = B_TRUE; 661 break; 662 case 'g': 663 dur_txg = 1; 664 record.zi_duration = (int)strtol(optarg, &end, 10); 665 if (record.zi_duration <= 0 || *end != '\0') { 666 (void) fprintf(stderr, "invalid duration '%s': " 667 "must be a positive integer\n", optarg); 668 usage(); 669 return (1); 670 } 671 /* store duration of txgs as its negative */ 672 record.zi_duration *= -1; 673 break; 674 case 'h': 675 usage(); 676 return (0); 677 case 'I': 678 /* default duration, if one hasn't yet been defined */ 679 nowrites = 1; 680 if (dur_secs == 0 && dur_txg == 0) 681 record.zi_duration = 30; 682 break; 683 case 'l': 684 level = (int)strtol(optarg, &end, 10); 685 if (*end != '\0') { 686 (void) fprintf(stderr, "invalid level '%s': " 687 "must be an integer\n", optarg); 688 usage(); 689 return (1); 690 } 691 break; 692 case 'm': 693 domount = 1; 694 break; 695 case 'p': 696 (void) strlcpy(record.zi_func, optarg, 697 sizeof (record.zi_func)); 698 break; 699 case 'q': 700 quiet = 1; 701 break; 702 case 'r': 703 range = optarg; 704 break; 705 case 's': 706 dur_secs = 1; 707 record.zi_duration = (int)strtol(optarg, &end, 10); 708 if (record.zi_duration <= 0 || *end != '\0') { 709 (void) fprintf(stderr, "invalid duration '%s': " 710 "must be a positive integer\n", optarg); 711 usage(); 712 return (1); 713 } 714 break; 715 case 'T': 716 if (strcasecmp(optarg, "read") == 0) { 717 io_type = ZIO_TYPE_READ; 718 } else if (strcasecmp(optarg, "write") == 0) { 719 io_type = ZIO_TYPE_WRITE; 720 } else if (strcasecmp(optarg, "free") == 0) { 721 io_type = ZIO_TYPE_FREE; 722 } else if (strcasecmp(optarg, "claim") == 0) { 723 io_type = ZIO_TYPE_CLAIM; 724 } else if (strcasecmp(optarg, "all") == 0) { 725 io_type = ZIO_TYPES; 726 } else { 727 (void) fprintf(stderr, "invalid I/O type " 728 "'%s': must be 'read', 'write', 'free', " 729 "'claim' or 'all'\n", optarg); 730 usage(); 731 return (1); 732 } 733 break; 734 case 't': 735 if ((type = name_to_type(optarg)) == TYPE_INVAL && 736 !MOS_TYPE(type)) { 737 (void) fprintf(stderr, "invalid type '%s'\n", 738 optarg); 739 usage(); 740 return (1); 741 } 742 break; 743 case 'u': 744 flags |= ZINJECT_UNLOAD_SPA; 745 break; 746 case 'L': 747 if ((label = name_to_type(optarg)) == TYPE_INVAL && 748 !LABEL_TYPE(type)) { 749 (void) fprintf(stderr, "invalid label type " 750 "'%s'\n", optarg); 751 usage(); 752 return (1); 753 } 754 break; 755 case ':': 756 (void) fprintf(stderr, "option -%c requires an " 757 "operand\n", optopt); 758 usage(); 759 return (1); 760 case '?': 761 (void) fprintf(stderr, "invalid option '%c'\n", 762 optopt); 763 usage(); 764 return (2); 765 } 766 } 767 768 argc -= optind; 769 argv += optind; 770 771 if (cancel != NULL) { 772 /* 773 * '-c' is invalid with any other options. 774 */ 775 if (raw != NULL || range != NULL || type != TYPE_INVAL || 776 level != 0 || record.zi_func[0] != '\0' || 777 record.zi_duration != 0) { 778 (void) fprintf(stderr, "cancel (-c) incompatible with " 779 "any other options\n"); 780 usage(); 781 return (2); 782 } 783 if (argc != 0) { 784 (void) fprintf(stderr, "extraneous argument to '-c'\n"); 785 usage(); 786 return (2); 787 } 788 789 if (strcmp(cancel, "all") == 0) { 790 return (cancel_all_handlers()); 791 } else { 792 int id = (int)strtol(cancel, &end, 10); 793 if (*end != '\0') { 794 (void) fprintf(stderr, "invalid handle id '%s':" 795 " must be an integer or 'all'\n", cancel); 796 usage(); 797 return (1); 798 } 799 return (cancel_handler(id)); 800 } 801 } 802 803 if (device != NULL) { 804 /* 805 * Device (-d) injection uses a completely different mechanism 806 * for doing injection, so handle it separately here. 807 */ 808 if (raw != NULL || range != NULL || type != TYPE_INVAL || 809 level != 0 || record.zi_func[0] != '\0' || 810 record.zi_duration != 0) { 811 (void) fprintf(stderr, "device (-d) incompatible with " 812 "data error injection\n"); 813 usage(); 814 return (2); 815 } 816 817 if (argc != 1) { 818 (void) fprintf(stderr, "device (-d) injection requires " 819 "a single pool name\n"); 820 usage(); 821 return (2); 822 } 823 824 (void) strcpy(pool, argv[0]); 825 dataset[0] = '\0'; 826 827 if (error == ECKSUM) { 828 (void) fprintf(stderr, "device error type must be " 829 "'io' or 'nxio'\n"); 830 return (1); 831 } 832 833 record.zi_iotype = io_type; 834 if (translate_device(pool, device, label, &record) != 0) 835 return (1); 836 if (!error) 837 error = ENXIO; 838 839 if (action != VDEV_STATE_UNKNOWN) 840 return (perform_action(pool, &record, action)); 841 842 } else if (raw != NULL) { 843 if (range != NULL || type != TYPE_INVAL || level != 0 || 844 record.zi_func[0] != '\0' || record.zi_duration != 0) { 845 (void) fprintf(stderr, "raw (-b) format with " 846 "any other options\n"); 847 usage(); 848 return (2); 849 } 850 851 if (argc != 1) { 852 (void) fprintf(stderr, "raw (-b) format expects a " 853 "single pool name\n"); 854 usage(); 855 return (2); 856 } 857 858 (void) strcpy(pool, argv[0]); 859 dataset[0] = '\0'; 860 861 if (error == ENXIO) { 862 (void) fprintf(stderr, "data error type must be " 863 "'checksum' or 'io'\n"); 864 return (1); 865 } 866 867 if (translate_raw(raw, &record) != 0) 868 return (1); 869 if (!error) 870 error = EIO; 871 } else if (record.zi_func[0] != '\0') { 872 if (raw != NULL || range != NULL || type != TYPE_INVAL || 873 level != 0 || device != NULL || record.zi_duration != 0) { 874 (void) fprintf(stderr, "panic (-p) incompatible with " 875 "other options\n"); 876 usage(); 877 return (2); 878 } 879 880 if (argc < 1 || argc > 2) { 881 (void) fprintf(stderr, "panic (-p) injection requires " 882 "a single pool name and an optional id\n"); 883 usage(); 884 return (2); 885 } 886 887 (void) strcpy(pool, argv[0]); 888 if (argv[1] != NULL) 889 record.zi_type = atoi(argv[1]); 890 dataset[0] = '\0'; 891 } else if (record.zi_duration != 0) { 892 if (nowrites == 0) { 893 (void) fprintf(stderr, "-s or -g meaningless " 894 "without -I (ignore writes)\n"); 895 usage(); 896 return (2); 897 } else if (dur_secs && dur_txg) { 898 (void) fprintf(stderr, "choose a duration either " 899 "in seconds (-s) or a number of txgs (-g) " 900 "but not both\n"); 901 usage(); 902 return (2); 903 } else if (argc != 1) { 904 (void) fprintf(stderr, "ignore writes (-I) " 905 "injection requires a single pool name\n"); 906 usage(); 907 return (2); 908 } 909 910 (void) strcpy(pool, argv[0]); 911 dataset[0] = '\0'; 912 } else if (type == TYPE_INVAL) { 913 if (flags == 0) { 914 (void) fprintf(stderr, "at least one of '-b', '-d', " 915 "'-t', '-a', '-p', '-I' or '-u' " 916 "must be specified\n"); 917 usage(); 918 return (2); 919 } 920 921 if (argc == 1 && (flags & ZINJECT_UNLOAD_SPA)) { 922 (void) strcpy(pool, argv[0]); 923 dataset[0] = '\0'; 924 } else if (argc != 0) { 925 (void) fprintf(stderr, "extraneous argument for " 926 "'-f'\n"); 927 usage(); 928 return (2); 929 } 930 931 flags |= ZINJECT_NULL; 932 } else { 933 if (argc != 1) { 934 (void) fprintf(stderr, "missing object\n"); 935 usage(); 936 return (2); 937 } 938 939 if (error == ENXIO) { 940 (void) fprintf(stderr, "data error type must be " 941 "'checksum' or 'io'\n"); 942 return (1); 943 } 944 945 if (translate_record(type, argv[0], range, level, &record, pool, 946 dataset) != 0) 947 return (1); 948 if (!error) 949 error = EIO; 950 } 951 952 /* 953 * If this is pool-wide metadata, unmount everything. The ioctl() will 954 * unload the pool, so that we trigger spa-wide reopen of metadata next 955 * time we access the pool. 956 */ 957 if (dataset[0] != '\0' && domount) { 958 if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_DATASET)) == NULL) 959 return (1); 960 961 if (zfs_unmount(zhp, NULL, 0) != 0) 962 return (1); 963 } 964 965 record.zi_error = error; 966 967 ret = register_handler(pool, flags, &record, quiet); 968 969 if (dataset[0] != '\0' && domount) 970 ret = (zfs_mount(zhp, NULL, 0) != 0); 971 972 libzfs_fini(g_zfs); 973 974 return (ret); 975} 976