1ea8dc4b6Seschrock /* 2ea8dc4b6Seschrock * CDDL HEADER START 3ea8dc4b6Seschrock * 4ea8dc4b6Seschrock * The contents of this file are subject to the terms of the 5ea8dc4b6Seschrock * Common Development and Distribution License (the "License"). 6ea8dc4b6Seschrock * You may not use this file except in compliance with the License. 7ea8dc4b6Seschrock * 8ea8dc4b6Seschrock * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9ea8dc4b6Seschrock * or http://www.opensolaris.org/os/licensing. 10ea8dc4b6Seschrock * See the License for the specific language governing permissions 11ea8dc4b6Seschrock * and limitations under the License. 12ea8dc4b6Seschrock * 13ea8dc4b6Seschrock * When distributing Covered Code, include this CDDL HEADER in each 14ea8dc4b6Seschrock * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15ea8dc4b6Seschrock * If applicable, add the following below this CDDL HEADER, with the 16ea8dc4b6Seschrock * fields enclosed by brackets "[]" replaced with your own identifying 17ea8dc4b6Seschrock * information: Portions Copyright [yyyy] [name of copyright owner] 18ea8dc4b6Seschrock * 19ea8dc4b6Seschrock * CDDL HEADER END 20ea8dc4b6Seschrock */ 21ea8dc4b6Seschrock /* 2298d1cbfeSGeorge Wilson * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23ea8dc4b6Seschrock */ 24ea8dc4b6Seschrock 25ea8dc4b6Seschrock /* 26ea8dc4b6Seschrock * ZFS Fault Injector 27ea8dc4b6Seschrock * 28ea8dc4b6Seschrock * This userland component takes a set of options and uses libzpool to translate 29ea8dc4b6Seschrock * from a user-visible object type and name to an internal representation. 30ea8dc4b6Seschrock * There are two basic types of faults: device faults and data faults. 31ea8dc4b6Seschrock * 32ea8dc4b6Seschrock * 33ea8dc4b6Seschrock * DEVICE FAULTS 34ea8dc4b6Seschrock * 35ea8dc4b6Seschrock * Errors can be injected into a particular vdev using the '-d' option. This 36ea8dc4b6Seschrock * option takes a path or vdev GUID to uniquely identify the device within a 37ea8dc4b6Seschrock * pool. There are two types of errors that can be injected, EIO and ENXIO, 3821bf64a7Sgw * that can be controlled through the '-e' option. The default is ENXIO. For 39ea8dc4b6Seschrock * EIO failures, any attempt to read data from the device will return EIO, but 40ea8dc4b6Seschrock * subsequent attempt to reopen the device will succeed. For ENXIO failures, 41ea8dc4b6Seschrock * any attempt to read from the device will return EIO, but any attempt to 42ea8dc4b6Seschrock * reopen the device will also return ENXIO. 4321bf64a7Sgw * For label faults, the -L option must be specified. This allows faults 4498d1cbfeSGeorge Wilson * to be injected into either the nvlist, uberblock, pad1, or pad2 region 4598d1cbfeSGeorge Wilson * of all the labels for the specified device. 46ea8dc4b6Seschrock * 47ea8dc4b6Seschrock * This form of the command looks like: 48ea8dc4b6Seschrock * 4998d1cbfeSGeorge Wilson * zinject -d device [-e errno] [-L <uber | nvlist | pad1 | pad2>] pool 50ea8dc4b6Seschrock * 51ea8dc4b6Seschrock * 52ea8dc4b6Seschrock * DATA FAULTS 53ea8dc4b6Seschrock * 54ea8dc4b6Seschrock * We begin with a tuple of the form: 55ea8dc4b6Seschrock * 56ea8dc4b6Seschrock * <type,level,range,object> 57ea8dc4b6Seschrock * 58ea8dc4b6Seschrock * type A string describing the type of data to target. Each type 59ea8dc4b6Seschrock * implicitly describes how to interpret 'object'. Currently, 60ea8dc4b6Seschrock * the following values are supported: 61ea8dc4b6Seschrock * 62ea8dc4b6Seschrock * data User data for a file 63ea8dc4b6Seschrock * dnode Dnode for a file or directory 64ea8dc4b6Seschrock * 65ea8dc4b6Seschrock * The following MOS objects are special. Instead of injecting 66ea8dc4b6Seschrock * errors on a particular object or blkid, we inject errors across 67ea8dc4b6Seschrock * all objects of the given type. 68ea8dc4b6Seschrock * 69ea8dc4b6Seschrock * mos Any data in the MOS 70ea8dc4b6Seschrock * mosdir object directory 71ea8dc4b6Seschrock * config pool configuration 72*cde58dbcSMatthew Ahrens * bpobj blkptr list 73ea8dc4b6Seschrock * spacemap spacemap 74ea8dc4b6Seschrock * metaslab metaslab 75ea8dc4b6Seschrock * errlog persistent error log 76ea8dc4b6Seschrock * 77ea8dc4b6Seschrock * level Object level. Defaults to '0', not applicable to all types. If 78ea8dc4b6Seschrock * a range is given, this corresponds to the indirect block 79ea8dc4b6Seschrock * corresponding to the specific range. 80ea8dc4b6Seschrock * 81ea8dc4b6Seschrock * range A numerical range [start,end) within the object. Defaults to 82ea8dc4b6Seschrock * the full size of the file. 83ea8dc4b6Seschrock * 84ea8dc4b6Seschrock * object A string describing the logical location of the object. For 85ea8dc4b6Seschrock * files and directories (currently the only supported types), 86ea8dc4b6Seschrock * this is the path of the object on disk. 87ea8dc4b6Seschrock * 88ea8dc4b6Seschrock * This is translated, via libzpool, into the following internal representation: 89ea8dc4b6Seschrock * 90ea8dc4b6Seschrock * <type,objset,object,level,range> 91ea8dc4b6Seschrock * 92ea8dc4b6Seschrock * These types should be self-explanatory. This tuple is then passed to the 93ea8dc4b6Seschrock * kernel via a special ioctl() to initiate fault injection for the given 94ea8dc4b6Seschrock * object. Note that 'type' is not strictly necessary for fault injection, but 95ea8dc4b6Seschrock * is used when translating existing faults into a human-readable string. 96ea8dc4b6Seschrock * 97ea8dc4b6Seschrock * 98ea8dc4b6Seschrock * The command itself takes one of the forms: 99ea8dc4b6Seschrock * 100ea8dc4b6Seschrock * zinject 101ea8dc4b6Seschrock * zinject <-a | -u pool> 102ea8dc4b6Seschrock * zinject -c <id|all> 103ea8dc4b6Seschrock * zinject [-q] <-t type> [-f freq] [-u] [-a] [-m] [-e errno] [-l level] 104ea8dc4b6Seschrock * [-r range] <object> 105ea8dc4b6Seschrock * zinject [-f freq] [-a] [-m] [-u] -b objset:object:level:start:end pool 106ea8dc4b6Seschrock * 107ea8dc4b6Seschrock * With no arguments, the command prints all currently registered injection 108ea8dc4b6Seschrock * handlers, with their numeric identifiers. 109ea8dc4b6Seschrock * 110ea8dc4b6Seschrock * The '-c' option will clear the given handler, or all handlers if 'all' is 111ea8dc4b6Seschrock * specified. 112ea8dc4b6Seschrock * 113ea8dc4b6Seschrock * The '-e' option takes a string describing the errno to simulate. This must 114ea8dc4b6Seschrock * be either 'io' or 'checksum'. In most cases this will result in the same 115ea8dc4b6Seschrock * behavior, but RAID-Z will produce a different set of ereports for this 116ea8dc4b6Seschrock * situation. 117ea8dc4b6Seschrock * 118ea8dc4b6Seschrock * The '-a', '-u', and '-m' flags toggle internal flush behavior. If '-a' is 119ea8dc4b6Seschrock * specified, then the ARC cache is flushed appropriately. If '-u' is 120ea8dc4b6Seschrock * specified, then the underlying SPA is unloaded. Either of these flags can be 121ea8dc4b6Seschrock * specified independently of any other handlers. The '-m' flag automatically 122ea8dc4b6Seschrock * does an unmount and remount of the underlying dataset to aid in flushing the 123ea8dc4b6Seschrock * cache. 124ea8dc4b6Seschrock * 125ea8dc4b6Seschrock * The '-f' flag controls the frequency of errors injected, expressed as a 126ea8dc4b6Seschrock * integer percentage between 1 and 100. The default is 100. 127ea8dc4b6Seschrock * 128ea8dc4b6Seschrock * The this form is responsible for actually injecting the handler into the 129ea8dc4b6Seschrock * framework. It takes the arguments described above, translates them to the 130ea8dc4b6Seschrock * internal tuple using libzpool, and then issues an ioctl() to register the 131ea8dc4b6Seschrock * handler. 132ea8dc4b6Seschrock * 133ea8dc4b6Seschrock * The final form can target a specific bookmark, regardless of whether a 134ea8dc4b6Seschrock * human-readable interface has been designed. It allows developers to specify 135ea8dc4b6Seschrock * a particular block by number. 136ea8dc4b6Seschrock */ 137ea8dc4b6Seschrock 138ea8dc4b6Seschrock #include <errno.h> 139ea8dc4b6Seschrock #include <fcntl.h> 140ea8dc4b6Seschrock #include <stdio.h> 141ea8dc4b6Seschrock #include <stdlib.h> 142ea8dc4b6Seschrock #include <strings.h> 143ea8dc4b6Seschrock #include <unistd.h> 144ea8dc4b6Seschrock 145ea8dc4b6Seschrock #include <sys/fs/zfs.h> 146ea8dc4b6Seschrock #include <sys/mount.h> 147ea8dc4b6Seschrock 148ea8dc4b6Seschrock #include <libzfs.h> 149ea8dc4b6Seschrock 150ea8dc4b6Seschrock #undef verify /* both libzfs.h and zfs_context.h want to define this */ 151ea8dc4b6Seschrock 152ea8dc4b6Seschrock #include "zinject.h" 153ea8dc4b6Seschrock 15499653d4eSeschrock libzfs_handle_t *g_zfs; 155ea8dc4b6Seschrock int zfs_fd; 156ea8dc4b6Seschrock 157ea8dc4b6Seschrock #define ECKSUM EBADE 158ea8dc4b6Seschrock 159ea8dc4b6Seschrock static const char *errtable[TYPE_INVAL] = { 160ea8dc4b6Seschrock "data", 161ea8dc4b6Seschrock "dnode", 162ea8dc4b6Seschrock "mos", 163ea8dc4b6Seschrock "mosdir", 164ea8dc4b6Seschrock "metaslab", 165ea8dc4b6Seschrock "config", 166*cde58dbcSMatthew Ahrens "bpobj", 167ea8dc4b6Seschrock "spacemap", 16821bf64a7Sgw "errlog", 16921bf64a7Sgw "uber", 17098d1cbfeSGeorge Wilson "nvlist", 17198d1cbfeSGeorge Wilson "pad1", 17298d1cbfeSGeorge Wilson "pad2" 173ea8dc4b6Seschrock }; 174ea8dc4b6Seschrock 175ea8dc4b6Seschrock static err_type_t 176ea8dc4b6Seschrock name_to_type(const char *arg) 177ea8dc4b6Seschrock { 178ea8dc4b6Seschrock int i; 179ea8dc4b6Seschrock for (i = 0; i < TYPE_INVAL; i++) 180ea8dc4b6Seschrock if (strcmp(errtable[i], arg) == 0) 181ea8dc4b6Seschrock return (i); 182ea8dc4b6Seschrock 183ea8dc4b6Seschrock return (TYPE_INVAL); 184ea8dc4b6Seschrock } 185ea8dc4b6Seschrock 186ea8dc4b6Seschrock static const char * 187ea8dc4b6Seschrock type_to_name(uint64_t type) 188ea8dc4b6Seschrock { 189ea8dc4b6Seschrock switch (type) { 190ea8dc4b6Seschrock case DMU_OT_OBJECT_DIRECTORY: 191ea8dc4b6Seschrock return ("mosdir"); 192ea8dc4b6Seschrock case DMU_OT_OBJECT_ARRAY: 193ea8dc4b6Seschrock return ("metaslab"); 194ea8dc4b6Seschrock case DMU_OT_PACKED_NVLIST: 195ea8dc4b6Seschrock return ("config"); 196*cde58dbcSMatthew Ahrens case DMU_OT_BPOBJ: 197*cde58dbcSMatthew Ahrens return ("bpobj"); 198ea8dc4b6Seschrock case DMU_OT_SPACE_MAP: 199ea8dc4b6Seschrock return ("spacemap"); 200ea8dc4b6Seschrock case DMU_OT_ERROR_LOG: 201ea8dc4b6Seschrock return ("errlog"); 202ea8dc4b6Seschrock default: 203ea8dc4b6Seschrock return ("-"); 204ea8dc4b6Seschrock } 205ea8dc4b6Seschrock } 206ea8dc4b6Seschrock 207ea8dc4b6Seschrock 208ea8dc4b6Seschrock /* 209ea8dc4b6Seschrock * Print usage message. 210ea8dc4b6Seschrock */ 211ea8dc4b6Seschrock void 212ea8dc4b6Seschrock usage(void) 213ea8dc4b6Seschrock { 214ea8dc4b6Seschrock (void) printf( 215ea8dc4b6Seschrock "usage:\n" 216ea8dc4b6Seschrock "\n" 217ea8dc4b6Seschrock "\tzinject\n" 218ea8dc4b6Seschrock "\n" 219ea8dc4b6Seschrock "\t\tList all active injection records.\n" 220ea8dc4b6Seschrock "\n" 221ea8dc4b6Seschrock "\tzinject -c <id|all>\n" 222ea8dc4b6Seschrock "\n" 223ea8dc4b6Seschrock "\t\tClear the particular record (if given a numeric ID), or\n" 224ea8dc4b6Seschrock "\t\tall records if 'all' is specificed.\n" 225ea8dc4b6Seschrock "\n" 22688ecc943SGeorge Wilson "\tzinject -p <function name> pool\n" 22788ecc943SGeorge Wilson "\t\tInject a panic fault at the specified function. Only \n" 22888ecc943SGeorge Wilson "\t\tfunctions which call spa_vdev_config_exit(), or \n" 22988ecc943SGeorge Wilson "\t\tspa_vdev_exit() will trigger a panic.\n" 23088ecc943SGeorge Wilson "\n" 23198d1cbfeSGeorge Wilson "\tzinject -d device [-e errno] [-L <nvlist|uber|pad1|pad2>] [-F]\n" 2328f18d1faSGeorge Wilson "\t [-T <read|write|free|claim|all> pool\n" 23321bf64a7Sgw "\t\tInject a fault into a particular device or the device's\n" 23498d1cbfeSGeorge Wilson "\t\tlabel. Label injection can either be 'nvlist', 'uber',\n " 23598d1cbfeSGeorge Wilson "\t\t'pad1', or 'pad2'.\n" 23621bf64a7Sgw "\t\t'errno' can either be 'nxio' (the default) or 'io'.\n" 237ea8dc4b6Seschrock "\n" 2388f18d1faSGeorge Wilson "\tzinject -d device -A <degrade|fault> pool\n" 2398f18d1faSGeorge Wilson "\t\tPerform a specific action on a particular device\n" 2408f18d1faSGeorge Wilson "\n" 241468c413aSTim Haley "\tzinject -I [-s <seconds> | -g <txgs>] pool\n" 242468c413aSTim Haley "\t\tCause the pool to stop writing blocks yet not\n" 243468c413aSTim Haley "\t\treport errors for a duration. Simulates buggy hardware\n" 244468c413aSTim Haley "\t\tthat fails to honor cache flush requests.\n" 245468c413aSTim Haley "\t\tDefault duration is 30 seconds. The machine is panicked\n" 246468c413aSTim Haley "\t\tat the end of the duration.\n" 247468c413aSTim Haley "\n" 248ea8dc4b6Seschrock "\tzinject -b objset:object:level:blkid pool\n" 249ea8dc4b6Seschrock "\n" 250ea8dc4b6Seschrock "\t\tInject an error into pool 'pool' with the numeric bookmark\n" 251ea8dc4b6Seschrock "\t\tspecified by the remaining tuple. Each number is in\n" 252ea8dc4b6Seschrock "\t\thexidecimal, and only one block can be specified.\n" 253ea8dc4b6Seschrock "\n" 254ea8dc4b6Seschrock "\tzinject [-q] <-t type> [-e errno] [-l level] [-r range]\n" 255ea8dc4b6Seschrock "\t [-a] [-m] [-u] [-f freq] <object>\n" 256ea8dc4b6Seschrock "\n" 257ea8dc4b6Seschrock "\t\tInject an error into the object specified by the '-t' option\n" 258ea8dc4b6Seschrock "\t\tand the object descriptor. The 'object' parameter is\n" 259ea8dc4b6Seschrock "\t\tinterperted depending on the '-t' option.\n" 260ea8dc4b6Seschrock "\n" 261ea8dc4b6Seschrock "\t\t-q\tQuiet mode. Only print out the handler number added.\n" 262ea8dc4b6Seschrock "\t\t-e\tInject a specific error. Must be either 'io' or\n" 263ea8dc4b6Seschrock "\t\t\t'checksum'. Default is 'io'.\n" 264ea8dc4b6Seschrock "\t\t-l\tInject error at a particular block level. Default is " 265ea8dc4b6Seschrock "0.\n" 266ea8dc4b6Seschrock "\t\t-m\tAutomatically remount underlying filesystem.\n" 267ea8dc4b6Seschrock "\t\t-r\tInject error over a particular logical range of an\n" 268ea8dc4b6Seschrock "\t\t\tobject. Will be translated to the appropriate blkid\n" 269ea8dc4b6Seschrock "\t\t\trange according to the object's properties.\n" 270ea8dc4b6Seschrock "\t\t-a\tFlush the ARC cache. Can be specified without any\n" 271ea8dc4b6Seschrock "\t\t\tassociated object.\n" 272ea8dc4b6Seschrock "\t\t-u\tUnload the associated pool. Can be specified with only\n" 273ea8dc4b6Seschrock "\t\t\ta pool object.\n" 274ea8dc4b6Seschrock "\t\t-f\tOnly inject errors a fraction of the time. Expressed as\n" 275ea8dc4b6Seschrock "\t\t\ta percentage between 1 and 100.\n" 276ea8dc4b6Seschrock "\n" 277ea8dc4b6Seschrock "\t-t data\t\tInject an error into the plain file contents of a\n" 278ea8dc4b6Seschrock "\t\t\tfile. The object must be specified as a complete path\n" 279ea8dc4b6Seschrock "\t\t\tto a file on a ZFS filesystem.\n" 280ea8dc4b6Seschrock "\n" 281ea8dc4b6Seschrock "\t-t dnode\tInject an error into the metadnode in the block\n" 282ea8dc4b6Seschrock "\t\t\tcorresponding to the dnode for a file or directory. The\n" 283ea8dc4b6Seschrock "\t\t\t'-r' option is incompatible with this mode. The object\n" 284ea8dc4b6Seschrock "\t\t\tis specified as a complete path to a file or directory\n" 285ea8dc4b6Seschrock "\t\t\ton a ZFS filesystem.\n" 286ea8dc4b6Seschrock "\n" 287ea8dc4b6Seschrock "\t-t <mos>\tInject errors into the MOS for objects of the given\n" 288*cde58dbcSMatthew Ahrens "\t\t\ttype. Valid types are: mos, mosdir, config, bpobj,\n" 28955434c77Sek "\t\t\tspacemap, metaslab, errlog. The only valid <object> is\n" 29055434c77Sek "\t\t\tthe poolname.\n"); 291ea8dc4b6Seschrock } 292ea8dc4b6Seschrock 293ea8dc4b6Seschrock static int 294ea8dc4b6Seschrock iter_handlers(int (*func)(int, const char *, zinject_record_t *, void *), 295ea8dc4b6Seschrock void *data) 296ea8dc4b6Seschrock { 297ea8dc4b6Seschrock zfs_cmd_t zc; 298ea8dc4b6Seschrock int ret; 299ea8dc4b6Seschrock 300ea8dc4b6Seschrock zc.zc_guid = 0; 301ea8dc4b6Seschrock 302ea8dc4b6Seschrock while (ioctl(zfs_fd, ZFS_IOC_INJECT_LIST_NEXT, &zc) == 0) 303ea8dc4b6Seschrock if ((ret = func((int)zc.zc_guid, zc.zc_name, 304ea8dc4b6Seschrock &zc.zc_inject_record, data)) != 0) 305ea8dc4b6Seschrock return (ret); 306ea8dc4b6Seschrock 30754a91118SChris Kirby if (errno != ENOENT) { 30854a91118SChris Kirby (void) fprintf(stderr, "Unable to list handlers: %s\n", 30954a91118SChris Kirby strerror(errno)); 31054a91118SChris Kirby return (-1); 31154a91118SChris Kirby } 31254a91118SChris Kirby 313ea8dc4b6Seschrock return (0); 314ea8dc4b6Seschrock } 315ea8dc4b6Seschrock 316ea8dc4b6Seschrock static int 317ea8dc4b6Seschrock print_data_handler(int id, const char *pool, zinject_record_t *record, 318ea8dc4b6Seschrock void *data) 319ea8dc4b6Seschrock { 320ea8dc4b6Seschrock int *count = data; 321ea8dc4b6Seschrock 32288ecc943SGeorge Wilson if (record->zi_guid != 0 || record->zi_func[0] != '\0') 323ea8dc4b6Seschrock return (0); 324ea8dc4b6Seschrock 325ea8dc4b6Seschrock if (*count == 0) { 326ea8dc4b6Seschrock (void) printf("%3s %-15s %-6s %-6s %-8s %3s %-15s\n", 327ea8dc4b6Seschrock "ID", "POOL", "OBJSET", "OBJECT", "TYPE", "LVL", "RANGE"); 328ea8dc4b6Seschrock (void) printf("--- --------------- ------ " 329ea8dc4b6Seschrock "------ -------- --- ---------------\n"); 330ea8dc4b6Seschrock } 331ea8dc4b6Seschrock 332ea8dc4b6Seschrock *count += 1; 333ea8dc4b6Seschrock 334ea8dc4b6Seschrock (void) printf("%3d %-15s %-6llu %-6llu %-8s %3d ", id, pool, 335ea8dc4b6Seschrock (u_longlong_t)record->zi_objset, (u_longlong_t)record->zi_object, 336ea8dc4b6Seschrock type_to_name(record->zi_type), record->zi_level); 337ea8dc4b6Seschrock 338ea8dc4b6Seschrock if (record->zi_start == 0 && 339ea8dc4b6Seschrock record->zi_end == -1ULL) 340ea8dc4b6Seschrock (void) printf("all\n"); 341ea8dc4b6Seschrock else 342ea8dc4b6Seschrock (void) printf("[%llu, %llu]\n", (u_longlong_t)record->zi_start, 343ea8dc4b6Seschrock (u_longlong_t)record->zi_end); 344ea8dc4b6Seschrock 345ea8dc4b6Seschrock return (0); 346ea8dc4b6Seschrock } 347ea8dc4b6Seschrock 348ea8dc4b6Seschrock static int 349ea8dc4b6Seschrock print_device_handler(int id, const char *pool, zinject_record_t *record, 350ea8dc4b6Seschrock void *data) 351ea8dc4b6Seschrock { 352ea8dc4b6Seschrock int *count = data; 353ea8dc4b6Seschrock 35488ecc943SGeorge Wilson if (record->zi_guid == 0 || record->zi_func[0] != '\0') 355ea8dc4b6Seschrock return (0); 356ea8dc4b6Seschrock 357ea8dc4b6Seschrock if (*count == 0) { 358ea8dc4b6Seschrock (void) printf("%3s %-15s %s\n", "ID", "POOL", "GUID"); 359ea8dc4b6Seschrock (void) printf("--- --------------- ----------------\n"); 360ea8dc4b6Seschrock } 361ea8dc4b6Seschrock 362ea8dc4b6Seschrock *count += 1; 363ea8dc4b6Seschrock 364ea8dc4b6Seschrock (void) printf("%3d %-15s %llx\n", id, pool, 365ea8dc4b6Seschrock (u_longlong_t)record->zi_guid); 366ea8dc4b6Seschrock 367ea8dc4b6Seschrock return (0); 368ea8dc4b6Seschrock } 369ea8dc4b6Seschrock 37088ecc943SGeorge Wilson static int 37188ecc943SGeorge Wilson print_panic_handler(int id, const char *pool, zinject_record_t *record, 37288ecc943SGeorge Wilson void *data) 37388ecc943SGeorge Wilson { 37488ecc943SGeorge Wilson int *count = data; 37588ecc943SGeorge Wilson 37688ecc943SGeorge Wilson if (record->zi_func[0] == '\0') 37788ecc943SGeorge Wilson return (0); 37888ecc943SGeorge Wilson 37988ecc943SGeorge Wilson if (*count == 0) { 38088ecc943SGeorge Wilson (void) printf("%3s %-15s %s\n", "ID", "POOL", "FUNCTION"); 38188ecc943SGeorge Wilson (void) printf("--- --------------- ----------------\n"); 38288ecc943SGeorge Wilson } 38388ecc943SGeorge Wilson 38488ecc943SGeorge Wilson *count += 1; 38588ecc943SGeorge Wilson 38688ecc943SGeorge Wilson (void) printf("%3d %-15s %s\n", id, pool, record->zi_func); 38788ecc943SGeorge Wilson 38888ecc943SGeorge Wilson return (0); 38988ecc943SGeorge Wilson } 39088ecc943SGeorge Wilson 391ea8dc4b6Seschrock /* 392ea8dc4b6Seschrock * Print all registered error handlers. Returns the number of handlers 393ea8dc4b6Seschrock * registered. 394ea8dc4b6Seschrock */ 395ea8dc4b6Seschrock static int 396ea8dc4b6Seschrock print_all_handlers(void) 397ea8dc4b6Seschrock { 398ea8dc4b6Seschrock int count = 0; 399ea8dc4b6Seschrock 400ea8dc4b6Seschrock (void) iter_handlers(print_device_handler, &count); 401ea8dc4b6Seschrock (void) printf("\n"); 402ea8dc4b6Seschrock count = 0; 403ea8dc4b6Seschrock (void) iter_handlers(print_data_handler, &count); 40488ecc943SGeorge Wilson (void) printf("\n"); 40588ecc943SGeorge Wilson count = 0; 40688ecc943SGeorge Wilson (void) iter_handlers(print_panic_handler, &count); 407ea8dc4b6Seschrock 408ea8dc4b6Seschrock return (count); 409ea8dc4b6Seschrock } 410ea8dc4b6Seschrock 411ea8dc4b6Seschrock /* ARGSUSED */ 412ea8dc4b6Seschrock static int 413ea8dc4b6Seschrock cancel_one_handler(int id, const char *pool, zinject_record_t *record, 414ea8dc4b6Seschrock void *data) 415ea8dc4b6Seschrock { 416ea8dc4b6Seschrock zfs_cmd_t zc; 417ea8dc4b6Seschrock 418ea8dc4b6Seschrock zc.zc_guid = (uint64_t)id; 419ea8dc4b6Seschrock 420ea8dc4b6Seschrock if (ioctl(zfs_fd, ZFS_IOC_CLEAR_FAULT, &zc) != 0) { 421ea8dc4b6Seschrock (void) fprintf(stderr, "failed to remove handler %d: %s\n", 422ea8dc4b6Seschrock id, strerror(errno)); 423ea8dc4b6Seschrock return (1); 424ea8dc4b6Seschrock } 425ea8dc4b6Seschrock 426ea8dc4b6Seschrock return (0); 427ea8dc4b6Seschrock } 428ea8dc4b6Seschrock 429ea8dc4b6Seschrock /* 430ea8dc4b6Seschrock * Remove all fault injection handlers. 431ea8dc4b6Seschrock */ 432ea8dc4b6Seschrock static int 433ea8dc4b6Seschrock cancel_all_handlers(void) 434ea8dc4b6Seschrock { 435ea8dc4b6Seschrock int ret = iter_handlers(cancel_one_handler, NULL); 436ea8dc4b6Seschrock 43754a91118SChris Kirby if (ret == 0) 43854a91118SChris Kirby (void) printf("removed all registered handlers\n"); 439ea8dc4b6Seschrock 440ea8dc4b6Seschrock return (ret); 441ea8dc4b6Seschrock } 442ea8dc4b6Seschrock 443ea8dc4b6Seschrock /* 444ea8dc4b6Seschrock * Remove a specific fault injection handler. 445ea8dc4b6Seschrock */ 446ea8dc4b6Seschrock static int 447ea8dc4b6Seschrock cancel_handler(int id) 448ea8dc4b6Seschrock { 449ea8dc4b6Seschrock zfs_cmd_t zc; 450ea8dc4b6Seschrock 451ea8dc4b6Seschrock zc.zc_guid = (uint64_t)id; 452ea8dc4b6Seschrock 453ea8dc4b6Seschrock if (ioctl(zfs_fd, ZFS_IOC_CLEAR_FAULT, &zc) != 0) { 454ea8dc4b6Seschrock (void) fprintf(stderr, "failed to remove handler %d: %s\n", 455ea8dc4b6Seschrock id, strerror(errno)); 456ea8dc4b6Seschrock return (1); 457ea8dc4b6Seschrock } 458ea8dc4b6Seschrock 459ea8dc4b6Seschrock (void) printf("removed handler %d\n", id); 460ea8dc4b6Seschrock 461ea8dc4b6Seschrock return (0); 462ea8dc4b6Seschrock } 463ea8dc4b6Seschrock 464ea8dc4b6Seschrock /* 465ea8dc4b6Seschrock * Register a new fault injection handler. 466ea8dc4b6Seschrock */ 467ea8dc4b6Seschrock static int 468ea8dc4b6Seschrock register_handler(const char *pool, int flags, zinject_record_t *record, 469ea8dc4b6Seschrock int quiet) 470ea8dc4b6Seschrock { 471ea8dc4b6Seschrock zfs_cmd_t zc; 472ea8dc4b6Seschrock 473ea8dc4b6Seschrock (void) strcpy(zc.zc_name, pool); 474ea8dc4b6Seschrock zc.zc_inject_record = *record; 475ea8dc4b6Seschrock zc.zc_guid = flags; 476ea8dc4b6Seschrock 477ea8dc4b6Seschrock if (ioctl(zfs_fd, ZFS_IOC_INJECT_FAULT, &zc) != 0) { 478ea8dc4b6Seschrock (void) fprintf(stderr, "failed to add handler: %s\n", 479ea8dc4b6Seschrock strerror(errno)); 480ea8dc4b6Seschrock return (1); 481ea8dc4b6Seschrock } 482ea8dc4b6Seschrock 483ea8dc4b6Seschrock if (flags & ZINJECT_NULL) 484ea8dc4b6Seschrock return (0); 485ea8dc4b6Seschrock 486ea8dc4b6Seschrock if (quiet) { 487ea8dc4b6Seschrock (void) printf("%llu\n", (u_longlong_t)zc.zc_guid); 488ea8dc4b6Seschrock } else { 489ea8dc4b6Seschrock (void) printf("Added handler %llu with the following " 490ea8dc4b6Seschrock "properties:\n", (u_longlong_t)zc.zc_guid); 491ea8dc4b6Seschrock (void) printf(" pool: %s\n", pool); 492ea8dc4b6Seschrock if (record->zi_guid) { 493ea8dc4b6Seschrock (void) printf(" vdev: %llx\n", 494ea8dc4b6Seschrock (u_longlong_t)record->zi_guid); 49588ecc943SGeorge Wilson } else if (record->zi_func[0] != '\0') { 49688ecc943SGeorge Wilson (void) printf(" panic function: %s\n", 49788ecc943SGeorge Wilson record->zi_func); 498468c413aSTim Haley } else if (record->zi_duration > 0) { 499468c413aSTim Haley (void) printf(" time: %lld seconds\n", 500468c413aSTim Haley (u_longlong_t)record->zi_duration); 501468c413aSTim Haley } else if (record->zi_duration < 0) { 502468c413aSTim Haley (void) printf(" txgs: %lld \n", 503468c413aSTim Haley (u_longlong_t)-record->zi_duration); 504ea8dc4b6Seschrock } else { 505ea8dc4b6Seschrock (void) printf("objset: %llu\n", 506ea8dc4b6Seschrock (u_longlong_t)record->zi_objset); 507ea8dc4b6Seschrock (void) printf("object: %llu\n", 508ea8dc4b6Seschrock (u_longlong_t)record->zi_object); 509ea8dc4b6Seschrock (void) printf(" type: %llu\n", 510ea8dc4b6Seschrock (u_longlong_t)record->zi_type); 511ea8dc4b6Seschrock (void) printf(" level: %d\n", record->zi_level); 512ea8dc4b6Seschrock if (record->zi_start == 0 && 513ea8dc4b6Seschrock record->zi_end == -1ULL) 514ea8dc4b6Seschrock (void) printf(" range: all\n"); 515ea8dc4b6Seschrock else 516ea8dc4b6Seschrock (void) printf(" range: [%llu, %llu)\n", 517ea8dc4b6Seschrock (u_longlong_t)record->zi_start, 518ea8dc4b6Seschrock (u_longlong_t)record->zi_end); 519ea8dc4b6Seschrock } 520ea8dc4b6Seschrock } 521ea8dc4b6Seschrock 522ea8dc4b6Seschrock return (0); 523ea8dc4b6Seschrock } 524ea8dc4b6Seschrock 5258f18d1faSGeorge Wilson int 5268f18d1faSGeorge Wilson perform_action(const char *pool, zinject_record_t *record, int cmd) 5278f18d1faSGeorge Wilson { 5288f18d1faSGeorge Wilson zfs_cmd_t zc; 5298f18d1faSGeorge Wilson 5308f18d1faSGeorge Wilson ASSERT(cmd == VDEV_STATE_DEGRADED || cmd == VDEV_STATE_FAULTED); 5318f18d1faSGeorge Wilson (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name)); 5328f18d1faSGeorge Wilson zc.zc_guid = record->zi_guid; 5338f18d1faSGeorge Wilson zc.zc_cookie = cmd; 5348f18d1faSGeorge Wilson 5358f18d1faSGeorge Wilson if (ioctl(zfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 5368f18d1faSGeorge Wilson return (0); 5378f18d1faSGeorge Wilson 5388f18d1faSGeorge Wilson return (1); 5398f18d1faSGeorge Wilson } 5408f18d1faSGeorge Wilson 541ea8dc4b6Seschrock int 542ea8dc4b6Seschrock main(int argc, char **argv) 543ea8dc4b6Seschrock { 544ea8dc4b6Seschrock int c; 545ea8dc4b6Seschrock char *range = NULL; 546ea8dc4b6Seschrock char *cancel = NULL; 547ea8dc4b6Seschrock char *end; 548ea8dc4b6Seschrock char *raw = NULL; 549ea8dc4b6Seschrock char *device = NULL; 550ea8dc4b6Seschrock int level = 0; 551ea8dc4b6Seschrock int quiet = 0; 552ea8dc4b6Seschrock int error = 0; 553ea8dc4b6Seschrock int domount = 0; 5548f18d1faSGeorge Wilson int io_type = ZIO_TYPES; 5558f18d1faSGeorge Wilson int action = VDEV_STATE_UNKNOWN; 556ea8dc4b6Seschrock err_type_t type = TYPE_INVAL; 55721bf64a7Sgw err_type_t label = TYPE_INVAL; 558ea8dc4b6Seschrock zinject_record_t record = { 0 }; 559ea8dc4b6Seschrock char pool[MAXNAMELEN]; 560ea8dc4b6Seschrock char dataset[MAXNAMELEN]; 561ea8dc4b6Seschrock zfs_handle_t *zhp; 562468c413aSTim Haley int nowrites = 0; 563468c413aSTim Haley int dur_txg = 0; 564468c413aSTim Haley int dur_secs = 0; 565ea8dc4b6Seschrock int ret; 566ea8dc4b6Seschrock int flags = 0; 567ea8dc4b6Seschrock 56899653d4eSeschrock if ((g_zfs = libzfs_init()) == NULL) { 56999653d4eSeschrock (void) fprintf(stderr, "internal error: failed to " 57099653d4eSeschrock "initialize ZFS library\n"); 57199653d4eSeschrock return (1); 57299653d4eSeschrock } 57399653d4eSeschrock 57499653d4eSeschrock libzfs_print_on_error(g_zfs, B_TRUE); 57599653d4eSeschrock 576ea8dc4b6Seschrock if ((zfs_fd = open(ZFS_DEV, O_RDWR)) < 0) { 577ea8dc4b6Seschrock (void) fprintf(stderr, "failed to open ZFS device\n"); 578ea8dc4b6Seschrock return (1); 579ea8dc4b6Seschrock } 580ea8dc4b6Seschrock 581ea8dc4b6Seschrock if (argc == 1) { 582ea8dc4b6Seschrock /* 583ea8dc4b6Seschrock * No arguments. Print the available handlers. If there are no 584ea8dc4b6Seschrock * available handlers, direct the user to '-h' for help 585ea8dc4b6Seschrock * information. 586ea8dc4b6Seschrock */ 587ea8dc4b6Seschrock if (print_all_handlers() == 0) { 588ea8dc4b6Seschrock (void) printf("No handlers registered.\n"); 589ea8dc4b6Seschrock (void) printf("Run 'zinject -h' for usage " 590ea8dc4b6Seschrock "information.\n"); 591ea8dc4b6Seschrock } 592ea8dc4b6Seschrock 593ea8dc4b6Seschrock return (0); 594ea8dc4b6Seschrock } 595ea8dc4b6Seschrock 5968f18d1faSGeorge Wilson while ((c = getopt(argc, argv, 597468c413aSTim Haley ":aA:b:d:f:Fg:qhIc:t:T:l:mr:s:e:uL:p:")) != -1) { 598ea8dc4b6Seschrock switch (c) { 599ea8dc4b6Seschrock case 'a': 600ea8dc4b6Seschrock flags |= ZINJECT_FLUSH_ARC; 601ea8dc4b6Seschrock break; 6028f18d1faSGeorge Wilson case 'A': 6038f18d1faSGeorge Wilson if (strcasecmp(optarg, "degrade") == 0) { 6048f18d1faSGeorge Wilson action = VDEV_STATE_DEGRADED; 6058f18d1faSGeorge Wilson } else if (strcasecmp(optarg, "fault") == 0) { 6068f18d1faSGeorge Wilson action = VDEV_STATE_FAULTED; 6078f18d1faSGeorge Wilson } else { 6088f18d1faSGeorge Wilson (void) fprintf(stderr, "invalid action '%s': " 6098f18d1faSGeorge Wilson "must be 'degrade' or 'fault'\n", optarg); 6108f18d1faSGeorge Wilson usage(); 6118f18d1faSGeorge Wilson return (1); 6128f18d1faSGeorge Wilson } 6138f18d1faSGeorge Wilson break; 614ea8dc4b6Seschrock case 'b': 615ea8dc4b6Seschrock raw = optarg; 616ea8dc4b6Seschrock break; 617ea8dc4b6Seschrock case 'c': 618ea8dc4b6Seschrock cancel = optarg; 619ea8dc4b6Seschrock break; 620ea8dc4b6Seschrock case 'd': 621ea8dc4b6Seschrock device = optarg; 622ea8dc4b6Seschrock break; 623ea8dc4b6Seschrock case 'e': 624ea8dc4b6Seschrock if (strcasecmp(optarg, "io") == 0) { 625ea8dc4b6Seschrock error = EIO; 626ea8dc4b6Seschrock } else if (strcasecmp(optarg, "checksum") == 0) { 627ea8dc4b6Seschrock error = ECKSUM; 628ea8dc4b6Seschrock } else if (strcasecmp(optarg, "nxio") == 0) { 629ea8dc4b6Seschrock error = ENXIO; 630ea8dc4b6Seschrock } else { 631ea8dc4b6Seschrock (void) fprintf(stderr, "invalid error type " 632ea8dc4b6Seschrock "'%s': must be 'io', 'checksum' or " 633ea8dc4b6Seschrock "'nxio'\n", optarg); 634ea8dc4b6Seschrock usage(); 635ea8dc4b6Seschrock return (1); 636ea8dc4b6Seschrock } 637ea8dc4b6Seschrock break; 638ea8dc4b6Seschrock case 'f': 639ea8dc4b6Seschrock record.zi_freq = atoi(optarg); 640ea8dc4b6Seschrock if (record.zi_freq < 1 || record.zi_freq > 100) { 641ea8dc4b6Seschrock (void) fprintf(stderr, "frequency range must " 642ea8dc4b6Seschrock "be in the range (0, 100]\n"); 643ea8dc4b6Seschrock return (1); 644ea8dc4b6Seschrock } 645ea8dc4b6Seschrock break; 6468956713aSEric Schrock case 'F': 6478956713aSEric Schrock record.zi_failfast = B_TRUE; 6488956713aSEric Schrock break; 649468c413aSTim Haley case 'g': 650468c413aSTim Haley dur_txg = 1; 651468c413aSTim Haley record.zi_duration = (int)strtol(optarg, &end, 10); 652468c413aSTim Haley if (record.zi_duration <= 0 || *end != '\0') { 653468c413aSTim Haley (void) fprintf(stderr, "invalid duration '%s': " 654468c413aSTim Haley "must be a positive integer\n", optarg); 655468c413aSTim Haley usage(); 656468c413aSTim Haley return (1); 657468c413aSTim Haley } 658468c413aSTim Haley /* store duration of txgs as its negative */ 659468c413aSTim Haley record.zi_duration *= -1; 660468c413aSTim Haley break; 661ea8dc4b6Seschrock case 'h': 662ea8dc4b6Seschrock usage(); 663ea8dc4b6Seschrock return (0); 664468c413aSTim Haley case 'I': 665468c413aSTim Haley /* default duration, if one hasn't yet been defined */ 666468c413aSTim Haley nowrites = 1; 667468c413aSTim Haley if (dur_secs == 0 && dur_txg == 0) 668468c413aSTim Haley record.zi_duration = 30; 669468c413aSTim Haley break; 670ea8dc4b6Seschrock case 'l': 671ea8dc4b6Seschrock level = (int)strtol(optarg, &end, 10); 672ea8dc4b6Seschrock if (*end != '\0') { 673ea8dc4b6Seschrock (void) fprintf(stderr, "invalid level '%s': " 674ea8dc4b6Seschrock "must be an integer\n", optarg); 675ea8dc4b6Seschrock usage(); 676ea8dc4b6Seschrock return (1); 677ea8dc4b6Seschrock } 678ea8dc4b6Seschrock break; 679ea8dc4b6Seschrock case 'm': 680ea8dc4b6Seschrock domount = 1; 681ea8dc4b6Seschrock break; 68288ecc943SGeorge Wilson case 'p': 68388ecc943SGeorge Wilson (void) strlcpy(record.zi_func, optarg, 68488ecc943SGeorge Wilson sizeof (record.zi_func)); 68588ecc943SGeorge Wilson break; 686ea8dc4b6Seschrock case 'q': 687ea8dc4b6Seschrock quiet = 1; 688ea8dc4b6Seschrock break; 689ea8dc4b6Seschrock case 'r': 690ea8dc4b6Seschrock range = optarg; 691ea8dc4b6Seschrock break; 692468c413aSTim Haley case 's': 693468c413aSTim Haley dur_secs = 1; 694468c413aSTim Haley record.zi_duration = (int)strtol(optarg, &end, 10); 695468c413aSTim Haley if (record.zi_duration <= 0 || *end != '\0') { 696468c413aSTim Haley (void) fprintf(stderr, "invalid duration '%s': " 697468c413aSTim Haley "must be a positive integer\n", optarg); 698468c413aSTim Haley usage(); 699468c413aSTim Haley return (1); 700468c413aSTim Haley } 701468c413aSTim Haley break; 7028f18d1faSGeorge Wilson case 'T': 7038f18d1faSGeorge Wilson if (strcasecmp(optarg, "read") == 0) { 7048f18d1faSGeorge Wilson io_type = ZIO_TYPE_READ; 7058f18d1faSGeorge Wilson } else if (strcasecmp(optarg, "write") == 0) { 7068f18d1faSGeorge Wilson io_type = ZIO_TYPE_WRITE; 7078f18d1faSGeorge Wilson } else if (strcasecmp(optarg, "free") == 0) { 7088f18d1faSGeorge Wilson io_type = ZIO_TYPE_FREE; 7098f18d1faSGeorge Wilson } else if (strcasecmp(optarg, "claim") == 0) { 7108f18d1faSGeorge Wilson io_type = ZIO_TYPE_CLAIM; 7118f18d1faSGeorge Wilson } else if (strcasecmp(optarg, "all") == 0) { 7128f18d1faSGeorge Wilson io_type = ZIO_TYPES; 7138f18d1faSGeorge Wilson } else { 7148f18d1faSGeorge Wilson (void) fprintf(stderr, "invalid I/O type " 7158f18d1faSGeorge Wilson "'%s': must be 'read', 'write', 'free', " 7168f18d1faSGeorge Wilson "'claim' or 'all'\n", optarg); 7178f18d1faSGeorge Wilson usage(); 7188f18d1faSGeorge Wilson return (1); 7198f18d1faSGeorge Wilson } 7208f18d1faSGeorge Wilson break; 721ea8dc4b6Seschrock case 't': 72221bf64a7Sgw if ((type = name_to_type(optarg)) == TYPE_INVAL && 72321bf64a7Sgw !MOS_TYPE(type)) { 724ea8dc4b6Seschrock (void) fprintf(stderr, "invalid type '%s'\n", 725ea8dc4b6Seschrock optarg); 726ea8dc4b6Seschrock usage(); 727ea8dc4b6Seschrock return (1); 728ea8dc4b6Seschrock } 729ea8dc4b6Seschrock break; 730ea8dc4b6Seschrock case 'u': 731ea8dc4b6Seschrock flags |= ZINJECT_UNLOAD_SPA; 732ea8dc4b6Seschrock break; 73321bf64a7Sgw case 'L': 73421bf64a7Sgw if ((label = name_to_type(optarg)) == TYPE_INVAL && 73521bf64a7Sgw !LABEL_TYPE(type)) { 73621bf64a7Sgw (void) fprintf(stderr, "invalid label type " 73721bf64a7Sgw "'%s'\n", optarg); 73821bf64a7Sgw usage(); 73921bf64a7Sgw return (1); 74021bf64a7Sgw } 74121bf64a7Sgw break; 742ea8dc4b6Seschrock case ':': 743ea8dc4b6Seschrock (void) fprintf(stderr, "option -%c requires an " 744ea8dc4b6Seschrock "operand\n", optopt); 745ea8dc4b6Seschrock usage(); 746ea8dc4b6Seschrock return (1); 747ea8dc4b6Seschrock case '?': 748ea8dc4b6Seschrock (void) fprintf(stderr, "invalid option '%c'\n", 749ea8dc4b6Seschrock optopt); 750ea8dc4b6Seschrock usage(); 751ea8dc4b6Seschrock return (2); 752ea8dc4b6Seschrock } 753ea8dc4b6Seschrock } 754ea8dc4b6Seschrock 755ea8dc4b6Seschrock argc -= optind; 756ea8dc4b6Seschrock argv += optind; 757ea8dc4b6Seschrock 758ea8dc4b6Seschrock if (cancel != NULL) { 759ea8dc4b6Seschrock /* 760ea8dc4b6Seschrock * '-c' is invalid with any other options. 761ea8dc4b6Seschrock */ 762ea8dc4b6Seschrock if (raw != NULL || range != NULL || type != TYPE_INVAL || 763468c413aSTim Haley level != 0 || record.zi_func[0] != '\0' || 764468c413aSTim Haley record.zi_duration != 0) { 765ea8dc4b6Seschrock (void) fprintf(stderr, "cancel (-c) incompatible with " 766ea8dc4b6Seschrock "any other options\n"); 767ea8dc4b6Seschrock usage(); 768ea8dc4b6Seschrock return (2); 769ea8dc4b6Seschrock } 770ea8dc4b6Seschrock if (argc != 0) { 771ea8dc4b6Seschrock (void) fprintf(stderr, "extraneous argument to '-c'\n"); 772ea8dc4b6Seschrock usage(); 773ea8dc4b6Seschrock return (2); 774ea8dc4b6Seschrock } 775ea8dc4b6Seschrock 776ea8dc4b6Seschrock if (strcmp(cancel, "all") == 0) { 777ea8dc4b6Seschrock return (cancel_all_handlers()); 778ea8dc4b6Seschrock } else { 779ea8dc4b6Seschrock int id = (int)strtol(cancel, &end, 10); 780ea8dc4b6Seschrock if (*end != '\0') { 781ea8dc4b6Seschrock (void) fprintf(stderr, "invalid handle id '%s':" 782ea8dc4b6Seschrock " must be an integer or 'all'\n", cancel); 783ea8dc4b6Seschrock usage(); 784ea8dc4b6Seschrock return (1); 785ea8dc4b6Seschrock } 786ea8dc4b6Seschrock return (cancel_handler(id)); 787ea8dc4b6Seschrock } 788ea8dc4b6Seschrock } 789ea8dc4b6Seschrock 790ea8dc4b6Seschrock if (device != NULL) { 791ea8dc4b6Seschrock /* 792ea8dc4b6Seschrock * Device (-d) injection uses a completely different mechanism 793ea8dc4b6Seschrock * for doing injection, so handle it separately here. 794ea8dc4b6Seschrock */ 795ea8dc4b6Seschrock if (raw != NULL || range != NULL || type != TYPE_INVAL || 796468c413aSTim Haley level != 0 || record.zi_func[0] != '\0' || 797468c413aSTim Haley record.zi_duration != 0) { 798ea8dc4b6Seschrock (void) fprintf(stderr, "device (-d) incompatible with " 799ea8dc4b6Seschrock "data error injection\n"); 800ea8dc4b6Seschrock usage(); 801ea8dc4b6Seschrock return (2); 802ea8dc4b6Seschrock } 803ea8dc4b6Seschrock 804ea8dc4b6Seschrock if (argc != 1) { 805ea8dc4b6Seschrock (void) fprintf(stderr, "device (-d) injection requires " 806ea8dc4b6Seschrock "a single pool name\n"); 807ea8dc4b6Seschrock usage(); 808ea8dc4b6Seschrock return (2); 809ea8dc4b6Seschrock } 810ea8dc4b6Seschrock 811ea8dc4b6Seschrock (void) strcpy(pool, argv[0]); 812ea8dc4b6Seschrock dataset[0] = '\0'; 813ea8dc4b6Seschrock 814ea8dc4b6Seschrock if (error == ECKSUM) { 815ea8dc4b6Seschrock (void) fprintf(stderr, "device error type must be " 816ea8dc4b6Seschrock "'io' or 'nxio'\n"); 817ea8dc4b6Seschrock return (1); 818ea8dc4b6Seschrock } 819ea8dc4b6Seschrock 8208f18d1faSGeorge Wilson record.zi_iotype = io_type; 82121bf64a7Sgw if (translate_device(pool, device, label, &record) != 0) 822ea8dc4b6Seschrock return (1); 823ea8dc4b6Seschrock if (!error) 824ea8dc4b6Seschrock error = ENXIO; 8258f18d1faSGeorge Wilson 8268f18d1faSGeorge Wilson if (action != VDEV_STATE_UNKNOWN) 8278f18d1faSGeorge Wilson return (perform_action(pool, &record, action)); 8288f18d1faSGeorge Wilson 829ea8dc4b6Seschrock } else if (raw != NULL) { 83088ecc943SGeorge Wilson if (range != NULL || type != TYPE_INVAL || level != 0 || 831468c413aSTim Haley record.zi_func[0] != '\0' || record.zi_duration != 0) { 832ea8dc4b6Seschrock (void) fprintf(stderr, "raw (-b) format with " 833ea8dc4b6Seschrock "any other options\n"); 834ea8dc4b6Seschrock usage(); 835ea8dc4b6Seschrock return (2); 836ea8dc4b6Seschrock } 837ea8dc4b6Seschrock 838ea8dc4b6Seschrock if (argc != 1) { 839ea8dc4b6Seschrock (void) fprintf(stderr, "raw (-b) format expects a " 840ea8dc4b6Seschrock "single pool name\n"); 841ea8dc4b6Seschrock usage(); 842ea8dc4b6Seschrock return (2); 843ea8dc4b6Seschrock } 844ea8dc4b6Seschrock 845ea8dc4b6Seschrock (void) strcpy(pool, argv[0]); 846ea8dc4b6Seschrock dataset[0] = '\0'; 847ea8dc4b6Seschrock 848ea8dc4b6Seschrock if (error == ENXIO) { 849ea8dc4b6Seschrock (void) fprintf(stderr, "data error type must be " 850ea8dc4b6Seschrock "'checksum' or 'io'\n"); 851ea8dc4b6Seschrock return (1); 852ea8dc4b6Seschrock } 853ea8dc4b6Seschrock 854ea8dc4b6Seschrock if (translate_raw(raw, &record) != 0) 855ea8dc4b6Seschrock return (1); 856ea8dc4b6Seschrock if (!error) 857ea8dc4b6Seschrock error = EIO; 85888ecc943SGeorge Wilson } else if (record.zi_func[0] != '\0') { 85988ecc943SGeorge Wilson if (raw != NULL || range != NULL || type != TYPE_INVAL || 860468c413aSTim Haley level != 0 || device != NULL || record.zi_duration != 0) { 86188ecc943SGeorge Wilson (void) fprintf(stderr, "panic (-p) incompatible with " 86288ecc943SGeorge Wilson "other options\n"); 86388ecc943SGeorge Wilson usage(); 86488ecc943SGeorge Wilson return (2); 86588ecc943SGeorge Wilson } 86688ecc943SGeorge Wilson 8671195e687SMark J Musante if (argc < 1 || argc > 2) { 86888ecc943SGeorge Wilson (void) fprintf(stderr, "panic (-p) injection requires " 8691195e687SMark J Musante "a single pool name and an optional id\n"); 87088ecc943SGeorge Wilson usage(); 87188ecc943SGeorge Wilson return (2); 87288ecc943SGeorge Wilson } 87388ecc943SGeorge Wilson 874468c413aSTim Haley (void) strcpy(pool, argv[0]); 8751195e687SMark J Musante if (argv[1] != NULL) 8761195e687SMark J Musante record.zi_type = atoi(argv[1]); 877468c413aSTim Haley dataset[0] = '\0'; 878468c413aSTim Haley } else if (record.zi_duration != 0) { 879468c413aSTim Haley if (nowrites == 0) { 880468c413aSTim Haley (void) fprintf(stderr, "-s or -g meaningless " 881468c413aSTim Haley "without -I (ignore writes)\n"); 882468c413aSTim Haley usage(); 883468c413aSTim Haley return (2); 884468c413aSTim Haley } else if (dur_secs && dur_txg) { 885468c413aSTim Haley (void) fprintf(stderr, "choose a duration either " 886468c413aSTim Haley "in seconds (-s) or a number of txgs (-g) " 887468c413aSTim Haley "but not both\n"); 888468c413aSTim Haley usage(); 889468c413aSTim Haley return (2); 890468c413aSTim Haley } else if (argc != 1) { 891468c413aSTim Haley (void) fprintf(stderr, "ignore writes (-I) " 892468c413aSTim Haley "injection requires a single pool name\n"); 893468c413aSTim Haley usage(); 894468c413aSTim Haley return (2); 895468c413aSTim Haley } 896468c413aSTim Haley 89788ecc943SGeorge Wilson (void) strcpy(pool, argv[0]); 89888ecc943SGeorge Wilson dataset[0] = '\0'; 899ea8dc4b6Seschrock } else if (type == TYPE_INVAL) { 900ea8dc4b6Seschrock if (flags == 0) { 901ea8dc4b6Seschrock (void) fprintf(stderr, "at least one of '-b', '-d', " 902468c413aSTim Haley "'-t', '-a', '-p', '-I' or '-u' " 903468c413aSTim Haley "must be specified\n"); 904ea8dc4b6Seschrock usage(); 905ea8dc4b6Seschrock return (2); 906ea8dc4b6Seschrock } 907ea8dc4b6Seschrock 908ea8dc4b6Seschrock if (argc == 1 && (flags & ZINJECT_UNLOAD_SPA)) { 909ea8dc4b6Seschrock (void) strcpy(pool, argv[0]); 910ea8dc4b6Seschrock dataset[0] = '\0'; 911ea8dc4b6Seschrock } else if (argc != 0) { 912ea8dc4b6Seschrock (void) fprintf(stderr, "extraneous argument for " 913ea8dc4b6Seschrock "'-f'\n"); 914ea8dc4b6Seschrock usage(); 915ea8dc4b6Seschrock return (2); 916ea8dc4b6Seschrock } 917ea8dc4b6Seschrock 918ea8dc4b6Seschrock flags |= ZINJECT_NULL; 919ea8dc4b6Seschrock } else { 920ea8dc4b6Seschrock if (argc != 1) { 921ea8dc4b6Seschrock (void) fprintf(stderr, "missing object\n"); 922ea8dc4b6Seschrock usage(); 923ea8dc4b6Seschrock return (2); 924ea8dc4b6Seschrock } 925ea8dc4b6Seschrock 926ea8dc4b6Seschrock if (error == ENXIO) { 927ea8dc4b6Seschrock (void) fprintf(stderr, "data error type must be " 928ea8dc4b6Seschrock "'checksum' or 'io'\n"); 929ea8dc4b6Seschrock return (1); 930ea8dc4b6Seschrock } 931ea8dc4b6Seschrock 932ea8dc4b6Seschrock if (translate_record(type, argv[0], range, level, &record, pool, 933ea8dc4b6Seschrock dataset) != 0) 934ea8dc4b6Seschrock return (1); 935ea8dc4b6Seschrock if (!error) 936ea8dc4b6Seschrock error = EIO; 937ea8dc4b6Seschrock } 938ea8dc4b6Seschrock 939ea8dc4b6Seschrock /* 940ea8dc4b6Seschrock * If this is pool-wide metadata, unmount everything. The ioctl() will 941ea8dc4b6Seschrock * unload the pool, so that we trigger spa-wide reopen of metadata next 942ea8dc4b6Seschrock * time we access the pool. 943ea8dc4b6Seschrock */ 944ea8dc4b6Seschrock if (dataset[0] != '\0' && domount) { 945990b4856Slling if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_DATASET)) == NULL) 946ea8dc4b6Seschrock return (1); 947ea8dc4b6Seschrock 948ea8dc4b6Seschrock if (zfs_unmount(zhp, NULL, 0) != 0) 949ea8dc4b6Seschrock return (1); 950ea8dc4b6Seschrock } 951ea8dc4b6Seschrock 952ea8dc4b6Seschrock record.zi_error = error; 953ea8dc4b6Seschrock 954ea8dc4b6Seschrock ret = register_handler(pool, flags, &record, quiet); 955ea8dc4b6Seschrock 956ea8dc4b6Seschrock if (dataset[0] != '\0' && domount) 957ea8dc4b6Seschrock ret = (zfs_mount(zhp, NULL, 0) != 0); 958ea8dc4b6Seschrock 95999653d4eSeschrock libzfs_fini(g_zfs); 96099653d4eSeschrock 961ea8dc4b6Seschrock return (ret); 962ea8dc4b6Seschrock } 963