]> git.proxmox.com Git - mirror_zfs.git/blame_incremental - cmd/zinject/zinject.c
zinject: inject device errors into ioctls
[mirror_zfs.git] / cmd / zinject / zinject.c
... / ...
CommitLineData
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 https://opensource.org/licenses/CDDL-1.0.
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 * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
24 * Copyright (c) 2017, Intel Corporation.
25 * Copyright (c) 2024, Klara Inc.
26 */
27
28/*
29 * ZFS Fault Injector
30 *
31 * This userland component takes a set of options and uses libzpool to translate
32 * from a user-visible object type and name to an internal representation.
33 * There are two basic types of faults: device faults and data faults.
34 *
35 *
36 * DEVICE FAULTS
37 *
38 * Errors can be injected into a particular vdev using the '-d' option. This
39 * option takes a path or vdev GUID to uniquely identify the device within a
40 * pool. There are four types of errors that can be injected, IO, ENXIO,
41 * ECHILD, and EILSEQ. These can be controlled through the '-e' option and the
42 * default is ENXIO. For EIO failures, any attempt to read data from the device
43 * will return EIO, but a subsequent attempt to reopen the device will succeed.
44 * For ENXIO failures, any attempt to read from the device will return EIO, but
45 * any attempt to reopen the device will also return ENXIO. The EILSEQ failures
46 * only apply to read operations (-T read) and will flip a bit after the device
47 * has read the original data.
48 *
49 * For label faults, the -L option must be specified. This allows faults
50 * to be injected into either the nvlist, uberblock, pad1, or pad2 region
51 * of all the labels for the specified device.
52 *
53 * This form of the command looks like:
54 *
55 * zinject -d device [-e errno] [-L <uber | nvlist | pad1 | pad2>] pool
56 *
57 *
58 * DATA FAULTS
59 *
60 * We begin with a tuple of the form:
61 *
62 * <type,level,range,object>
63 *
64 * type A string describing the type of data to target. Each type
65 * implicitly describes how to interpret 'object'. Currently,
66 * the following values are supported:
67 *
68 * data User data for a file
69 * dnode Dnode for a file or directory
70 *
71 * The following MOS objects are special. Instead of injecting
72 * errors on a particular object or blkid, we inject errors across
73 * all objects of the given type.
74 *
75 * mos Any data in the MOS
76 * mosdir object directory
77 * config pool configuration
78 * bpobj blkptr list
79 * spacemap spacemap
80 * metaslab metaslab
81 * errlog persistent error log
82 *
83 * level Object level. Defaults to '0', not applicable to all types. If
84 * a range is given, this corresponds to the indirect block
85 * corresponding to the specific range.
86 *
87 * range A numerical range [start,end) within the object. Defaults to
88 * the full size of the file.
89 *
90 * object A string describing the logical location of the object. For
91 * files and directories (currently the only supported types),
92 * this is the path of the object on disk.
93 *
94 * This is translated, via libzpool, into the following internal representation:
95 *
96 * <type,objset,object,level,range>
97 *
98 * These types should be self-explanatory. This tuple is then passed to the
99 * kernel via a special ioctl() to initiate fault injection for the given
100 * object. Note that 'type' is not strictly necessary for fault injection, but
101 * is used when translating existing faults into a human-readable string.
102 *
103 *
104 * The command itself takes one of the forms:
105 *
106 * zinject
107 * zinject <-a | -u pool>
108 * zinject -c <id|all>
109 * zinject [-q] <-t type> [-f freq] [-u] [-a] [-m] [-e errno] [-l level]
110 * [-r range] <object>
111 * zinject [-f freq] [-a] [-m] [-u] -b objset:object:level:start:end pool
112 *
113 * With no arguments, the command prints all currently registered injection
114 * handlers, with their numeric identifiers.
115 *
116 * The '-c' option will clear the given handler, or all handlers if 'all' is
117 * specified.
118 *
119 * The '-e' option takes a string describing the errno to simulate. This must
120 * be one of 'io', 'checksum', 'decompress', or 'decrypt'. In most cases this
121 * will result in the same behavior, but RAID-Z will produce a different set of
122 * ereports for this situation.
123 *
124 * The '-a', '-u', and '-m' flags toggle internal flush behavior. If '-a' is
125 * specified, then the ARC cache is flushed appropriately. If '-u' is
126 * specified, then the underlying SPA is unloaded. Either of these flags can be
127 * specified independently of any other handlers. The '-m' flag automatically
128 * does an unmount and remount of the underlying dataset to aid in flushing the
129 * cache.
130 *
131 * The '-f' flag controls the frequency of errors injected, expressed as a
132 * real number percentage between 0.0001 and 100. The default is 100.
133 *
134 * The this form is responsible for actually injecting the handler into the
135 * framework. It takes the arguments described above, translates them to the
136 * internal tuple using libzpool, and then issues an ioctl() to register the
137 * handler.
138 *
139 * The final form can target a specific bookmark, regardless of whether a
140 * human-readable interface has been designed. It allows developers to specify
141 * a particular block by number.
142 */
143
144#include <errno.h>
145#include <fcntl.h>
146#include <stdio.h>
147#include <stdlib.h>
148#include <string.h>
149#include <strings.h>
150#include <unistd.h>
151
152#include <sys/fs/zfs.h>
153#include <sys/mount.h>
154
155#include <libzfs.h>
156
157#undef verify /* both libzfs.h and zfs_context.h want to define this */
158
159#include "zinject.h"
160
161libzfs_handle_t *g_zfs;
162int zfs_fd;
163
164static const char *const errtable[TYPE_INVAL] = {
165 "data",
166 "dnode",
167 "mos",
168 "mosdir",
169 "metaslab",
170 "config",
171 "bpobj",
172 "spacemap",
173 "errlog",
174 "uber",
175 "nvlist",
176 "pad1",
177 "pad2"
178};
179
180static err_type_t
181name_to_type(const char *arg)
182{
183 int i;
184 for (i = 0; i < TYPE_INVAL; i++)
185 if (strcmp(errtable[i], arg) == 0)
186 return (i);
187
188 return (TYPE_INVAL);
189}
190
191static const char *
192type_to_name(uint64_t type)
193{
194 switch (type) {
195 case DMU_OT_OBJECT_DIRECTORY:
196 return ("mosdir");
197 case DMU_OT_OBJECT_ARRAY:
198 return ("metaslab");
199 case DMU_OT_PACKED_NVLIST:
200 return ("config");
201 case DMU_OT_BPOBJ:
202 return ("bpobj");
203 case DMU_OT_SPACE_MAP:
204 return ("spacemap");
205 case DMU_OT_ERROR_LOG:
206 return ("errlog");
207 default:
208 return ("-");
209 }
210}
211
212struct errstr {
213 int err;
214 const char *str;
215};
216static const struct errstr errstrtable[] = {
217 { EIO, "io" },
218 { ECKSUM, "checksum" },
219 { EINVAL, "decompress" },
220 { EACCES, "decrypt" },
221 { ENXIO, "nxio" },
222 { ECHILD, "dtl" },
223 { EILSEQ, "corrupt" },
224 { 0, NULL },
225};
226
227static int
228str_to_err(const char *str)
229{
230 for (int i = 0; errstrtable[i].str != NULL; i++)
231 if (strcasecmp(errstrtable[i].str, str) == 0)
232 return (errstrtable[i].err);
233 return (-1);
234}
235static const char *
236err_to_str(int err)
237{
238 for (int i = 0; errstrtable[i].str != NULL; i++)
239 if (errstrtable[i].err == err)
240 return (errstrtable[i].str);
241 return ("[unknown]");
242}
243
244/*
245 * Print usage message.
246 */
247void
248usage(void)
249{
250 (void) printf(
251 "usage:\n"
252 "\n"
253 "\tzinject\n"
254 "\n"
255 "\t\tList all active injection records.\n"
256 "\n"
257 "\tzinject -c <id|all>\n"
258 "\n"
259 "\t\tClear the particular record (if given a numeric ID), or\n"
260 "\t\tall records if 'all' is specified.\n"
261 "\n"
262 "\tzinject -p <function name> pool\n"
263 "\t\tInject a panic fault at the specified function. Only \n"
264 "\t\tfunctions which call spa_vdev_config_exit(), or \n"
265 "\t\tspa_vdev_exit() will trigger a panic.\n"
266 "\n"
267 "\tzinject -d device [-e errno] [-L <nvlist|uber|pad1|pad2>] [-F]\n"
268 "\t\t[-T <read|write|free|claim|ioctl|all>] [-f frequency] pool\n\n"
269 "\t\tInject a fault into a particular device or the device's\n"
270 "\t\tlabel. Label injection can either be 'nvlist', 'uber',\n "
271 "\t\t'pad1', or 'pad2'.\n"
272 "\t\t'errno' can be 'nxio' (the default), 'io', 'dtl', or\n"
273 "\t\t'corrupt' (bit flip).\n"
274 "\t\t'frequency' is a value between 0.0001 and 100.0 that limits\n"
275 "\t\tdevice error injection to a percentage of the IOs.\n"
276 "\n"
277 "\tzinject -d device -A <degrade|fault> -D <delay secs> pool\n"
278 "\t\tPerform a specific action on a particular device.\n"
279 "\n"
280 "\tzinject -d device -D latency:lanes pool\n"
281 "\n"
282 "\t\tAdd an artificial delay to IO requests on a particular\n"
283 "\t\tdevice, such that the requests take a minimum of 'latency'\n"
284 "\t\tmilliseconds to complete. Each delay has an associated\n"
285 "\t\tnumber of 'lanes' which defines the number of concurrent\n"
286 "\t\tIO requests that can be processed.\n"
287 "\n"
288 "\t\tFor example, with a single lane delay of 10 ms (-D 10:1),\n"
289 "\t\tthe device will only be able to service a single IO request\n"
290 "\t\tat a time with each request taking 10 ms to complete. So,\n"
291 "\t\tif only a single request is submitted every 10 ms, the\n"
292 "\t\taverage latency will be 10 ms; but if more than one request\n"
293 "\t\tis submitted every 10 ms, the average latency will be more\n"
294 "\t\tthan 10 ms.\n"
295 "\n"
296 "\t\tSimilarly, if a delay of 10 ms is specified to have two\n"
297 "\t\tlanes (-D 10:2), then the device will be able to service\n"
298 "\t\ttwo requests at a time, each with a minimum latency of\n"
299 "\t\t10 ms. So, if two requests are submitted every 10 ms, then\n"
300 "\t\tthe average latency will be 10 ms; but if more than two\n"
301 "\t\trequests are submitted every 10 ms, the average latency\n"
302 "\t\twill be more than 10 ms.\n"
303 "\n"
304 "\t\tAlso note, these delays are additive. So two invocations\n"
305 "\t\tof '-D 10:1', is roughly equivalent to a single invocation\n"
306 "\t\tof '-D 10:2'. This also means, one can specify multiple\n"
307 "\t\tlanes with differing target latencies. For example, an\n"
308 "\t\tinvocation of '-D 10:1' followed by '-D 25:2' will\n"
309 "\t\tcreate 3 lanes on the device; one lane with a latency\n"
310 "\t\tof 10 ms and two lanes with a 25 ms latency.\n"
311 "\n"
312 "\tzinject -I [-s <seconds> | -g <txgs>] pool\n"
313 "\t\tCause the pool to stop writing blocks yet not\n"
314 "\t\treport errors for a duration. Simulates buggy hardware\n"
315 "\t\tthat fails to honor cache flush requests.\n"
316 "\t\tDefault duration is 30 seconds. The machine is panicked\n"
317 "\t\tat the end of the duration.\n"
318 "\n"
319 "\tzinject -b objset:object:level:blkid pool\n"
320 "\n"
321 "\t\tInject an error into pool 'pool' with the numeric bookmark\n"
322 "\t\tspecified by the remaining tuple. Each number is in\n"
323 "\t\thexadecimal, and only one block can be specified.\n"
324 "\n"
325 "\tzinject [-q] <-t type> [-C dvas] [-e errno] [-l level]\n"
326 "\t\t[-r range] [-a] [-m] [-u] [-f freq] <object>\n"
327 "\n"
328 "\t\tInject an error into the object specified by the '-t' option\n"
329 "\t\tand the object descriptor. The 'object' parameter is\n"
330 "\t\tinterpreted depending on the '-t' option.\n"
331 "\n"
332 "\t\t-q\tQuiet mode. Only print out the handler number added.\n"
333 "\t\t-e\tInject a specific error. Must be one of 'io',\n"
334 "\t\t\t'checksum', 'decompress', or 'decrypt'. Default is 'io'.\n"
335 "\t\t-C\tInject the given error only into specific DVAs. The\n"
336 "\t\t\tDVAs should be specified as a list of 0-indexed DVAs\n"
337 "\t\t\tseparated by commas (ex. '0,2').\n"
338 "\t\t-l\tInject error at a particular block level. Default is "
339 "0.\n"
340 "\t\t-m\tAutomatically remount underlying filesystem.\n"
341 "\t\t-r\tInject error over a particular logical range of an\n"
342 "\t\t\tobject. Will be translated to the appropriate blkid\n"
343 "\t\t\trange according to the object's properties.\n"
344 "\t\t-a\tFlush the ARC cache. Can be specified without any\n"
345 "\t\t\tassociated object.\n"
346 "\t\t-u\tUnload the associated pool. Can be specified with only\n"
347 "\t\t\ta pool object.\n"
348 "\t\t-f\tOnly inject errors a fraction of the time. Expressed as\n"
349 "\t\t\ta percentage between 0.0001 and 100.\n"
350 "\n"
351 "\t-t data\t\tInject an error into the plain file contents of a\n"
352 "\t\t\tfile. The object must be specified as a complete path\n"
353 "\t\t\tto a file on a ZFS filesystem.\n"
354 "\n"
355 "\t-t dnode\tInject an error into the metadnode in the block\n"
356 "\t\t\tcorresponding to the dnode for a file or directory. The\n"
357 "\t\t\t'-r' option is incompatible with this mode. The object\n"
358 "\t\t\tis specified as a complete path to a file or directory\n"
359 "\t\t\ton a ZFS filesystem.\n"
360 "\n"
361 "\t-t <mos>\tInject errors into the MOS for objects of the given\n"
362 "\t\t\ttype. Valid types are: mos, mosdir, config, bpobj,\n"
363 "\t\t\tspacemap, metaslab, errlog. The only valid <object> is\n"
364 "\t\t\tthe poolname.\n");
365}
366
367static int
368iter_handlers(int (*func)(int, const char *, zinject_record_t *, void *),
369 void *data)
370{
371 zfs_cmd_t zc = {"\0"};
372 int ret;
373
374 while (zfs_ioctl(g_zfs, ZFS_IOC_INJECT_LIST_NEXT, &zc) == 0)
375 if ((ret = func((int)zc.zc_guid, zc.zc_name,
376 &zc.zc_inject_record, data)) != 0)
377 return (ret);
378
379 if (errno != ENOENT) {
380 (void) fprintf(stderr, "Unable to list handlers: %s\n",
381 strerror(errno));
382 return (-1);
383 }
384
385 return (0);
386}
387
388static int
389print_data_handler(int id, const char *pool, zinject_record_t *record,
390 void *data)
391{
392 int *count = data;
393
394 if (record->zi_guid != 0 || record->zi_func[0] != '\0')
395 return (0);
396
397 if (*count == 0) {
398 (void) printf("%3s %-15s %-6s %-6s %-8s %3s %-4s "
399 "%-15s\n", "ID", "POOL", "OBJSET", "OBJECT", "TYPE",
400 "LVL", "DVAs", "RANGE");
401 (void) printf("--- --------------- ------ "
402 "------ -------- --- ---- ---------------\n");
403 }
404
405 *count += 1;
406
407 (void) printf("%3d %-15s %-6llu %-6llu %-8s %-3d 0x%02x ",
408 id, pool, (u_longlong_t)record->zi_objset,
409 (u_longlong_t)record->zi_object, type_to_name(record->zi_type),
410 record->zi_level, record->zi_dvas);
411
412
413 if (record->zi_start == 0 &&
414 record->zi_end == -1ULL)
415 (void) printf("all\n");
416 else
417 (void) printf("[%llu, %llu]\n", (u_longlong_t)record->zi_start,
418 (u_longlong_t)record->zi_end);
419
420 return (0);
421}
422
423static int
424print_device_handler(int id, const char *pool, zinject_record_t *record,
425 void *data)
426{
427 static const char *iotypestr[] = {
428 "null", "read", "write", "free", "claim", "ioctl", "trim", "all",
429 };
430
431 int *count = data;
432
433 if (record->zi_guid == 0 || record->zi_func[0] != '\0')
434 return (0);
435
436 if (record->zi_cmd == ZINJECT_DELAY_IO)
437 return (0);
438
439 if (*count == 0) {
440 (void) printf("%3s %-15s %-16s %-5s %-10s %-9s\n",
441 "ID", "POOL", "GUID", "TYPE", "ERROR", "FREQ");
442 (void) printf(
443 "--- --------------- ---------------- "
444 "----- ---------- ---------\n");
445 }
446
447 *count += 1;
448
449 double freq = record->zi_freq == 0 ? 100.0f :
450 (((double)record->zi_freq) / ZI_PERCENTAGE_MAX) * 100.0f;
451
452 (void) printf("%3d %-15s %llx %-5s %-10s %8.4f%%\n", id, pool,
453 (u_longlong_t)record->zi_guid, iotypestr[record->zi_iotype],
454 err_to_str(record->zi_error), freq);
455
456 return (0);
457}
458
459static int
460print_delay_handler(int id, const char *pool, zinject_record_t *record,
461 void *data)
462{
463 int *count = data;
464
465 if (record->zi_guid == 0 || record->zi_func[0] != '\0')
466 return (0);
467
468 if (record->zi_cmd != ZINJECT_DELAY_IO)
469 return (0);
470
471 if (*count == 0) {
472 (void) printf("%3s %-15s %-15s %-15s %s\n",
473 "ID", "POOL", "DELAY (ms)", "LANES", "GUID");
474 (void) printf("--- --------------- --------------- "
475 "--------------- ----------------\n");
476 }
477
478 *count += 1;
479
480 (void) printf("%3d %-15s %-15llu %-15llu %llx\n", id, pool,
481 (u_longlong_t)NSEC2MSEC(record->zi_timer),
482 (u_longlong_t)record->zi_nlanes,
483 (u_longlong_t)record->zi_guid);
484
485 return (0);
486}
487
488static int
489print_panic_handler(int id, const char *pool, zinject_record_t *record,
490 void *data)
491{
492 int *count = data;
493
494 if (record->zi_func[0] == '\0')
495 return (0);
496
497 if (*count == 0) {
498 (void) printf("%3s %-15s %s\n", "ID", "POOL", "FUNCTION");
499 (void) printf("--- --------------- ----------------\n");
500 }
501
502 *count += 1;
503
504 (void) printf("%3d %-15s %s\n", id, pool, record->zi_func);
505
506 return (0);
507}
508
509/*
510 * Print all registered error handlers. Returns the number of handlers
511 * registered.
512 */
513static int
514print_all_handlers(void)
515{
516 int count = 0, total = 0;
517
518 (void) iter_handlers(print_device_handler, &count);
519 if (count > 0) {
520 total += count;
521 (void) printf("\n");
522 count = 0;
523 }
524
525 (void) iter_handlers(print_delay_handler, &count);
526 if (count > 0) {
527 total += count;
528 (void) printf("\n");
529 count = 0;
530 }
531
532 (void) iter_handlers(print_data_handler, &count);
533 if (count > 0) {
534 total += count;
535 (void) printf("\n");
536 count = 0;
537 }
538
539 (void) iter_handlers(print_panic_handler, &count);
540
541 return (count + total);
542}
543
544static int
545cancel_one_handler(int id, const char *pool, zinject_record_t *record,
546 void *data)
547{
548 (void) pool, (void) record, (void) data;
549 zfs_cmd_t zc = {"\0"};
550
551 zc.zc_guid = (uint64_t)id;
552
553 if (zfs_ioctl(g_zfs, ZFS_IOC_CLEAR_FAULT, &zc) != 0) {
554 (void) fprintf(stderr, "failed to remove handler %d: %s\n",
555 id, strerror(errno));
556 return (1);
557 }
558
559 return (0);
560}
561
562/*
563 * Remove all fault injection handlers.
564 */
565static int
566cancel_all_handlers(void)
567{
568 int ret = iter_handlers(cancel_one_handler, NULL);
569
570 if (ret == 0)
571 (void) printf("removed all registered handlers\n");
572
573 return (ret);
574}
575
576/*
577 * Remove a specific fault injection handler.
578 */
579static int
580cancel_handler(int id)
581{
582 zfs_cmd_t zc = {"\0"};
583
584 zc.zc_guid = (uint64_t)id;
585
586 if (zfs_ioctl(g_zfs, ZFS_IOC_CLEAR_FAULT, &zc) != 0) {
587 (void) fprintf(stderr, "failed to remove handler %d: %s\n",
588 id, strerror(errno));
589 return (1);
590 }
591
592 (void) printf("removed handler %d\n", id);
593
594 return (0);
595}
596
597/*
598 * Register a new fault injection handler.
599 */
600static int
601register_handler(const char *pool, int flags, zinject_record_t *record,
602 int quiet)
603{
604 zfs_cmd_t zc = {"\0"};
605
606 (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
607 zc.zc_inject_record = *record;
608 zc.zc_guid = flags;
609
610 if (zfs_ioctl(g_zfs, ZFS_IOC_INJECT_FAULT, &zc) != 0) {
611 (void) fprintf(stderr, "failed to add handler: %s\n",
612 errno == EDOM ? "block level exceeds max level of object" :
613 strerror(errno));
614 return (1);
615 }
616
617 if (flags & ZINJECT_NULL)
618 return (0);
619
620 if (quiet) {
621 (void) printf("%llu\n", (u_longlong_t)zc.zc_guid);
622 } else {
623 (void) printf("Added handler %llu with the following "
624 "properties:\n", (u_longlong_t)zc.zc_guid);
625 (void) printf(" pool: %s\n", pool);
626 if (record->zi_guid) {
627 (void) printf(" vdev: %llx\n",
628 (u_longlong_t)record->zi_guid);
629 } else if (record->zi_func[0] != '\0') {
630 (void) printf(" panic function: %s\n",
631 record->zi_func);
632 } else if (record->zi_duration > 0) {
633 (void) printf(" time: %lld seconds\n",
634 (u_longlong_t)record->zi_duration);
635 } else if (record->zi_duration < 0) {
636 (void) printf(" txgs: %lld \n",
637 (u_longlong_t)-record->zi_duration);
638 } else {
639 (void) printf("objset: %llu\n",
640 (u_longlong_t)record->zi_objset);
641 (void) printf("object: %llu\n",
642 (u_longlong_t)record->zi_object);
643 (void) printf(" type: %llu\n",
644 (u_longlong_t)record->zi_type);
645 (void) printf(" level: %d\n", record->zi_level);
646 if (record->zi_start == 0 &&
647 record->zi_end == -1ULL)
648 (void) printf(" range: all\n");
649 else
650 (void) printf(" range: [%llu, %llu)\n",
651 (u_longlong_t)record->zi_start,
652 (u_longlong_t)record->zi_end);
653 (void) printf(" dvas: 0x%x\n", record->zi_dvas);
654 }
655 }
656
657 return (0);
658}
659
660static int
661perform_action(const char *pool, zinject_record_t *record, int cmd)
662{
663 zfs_cmd_t zc = {"\0"};
664
665 ASSERT(cmd == VDEV_STATE_DEGRADED || cmd == VDEV_STATE_FAULTED);
666 (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
667 zc.zc_guid = record->zi_guid;
668 zc.zc_cookie = cmd;
669
670 if (zfs_ioctl(g_zfs, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
671 return (0);
672
673 return (1);
674}
675
676static int
677parse_delay(char *str, uint64_t *delay, uint64_t *nlanes)
678{
679 unsigned long scan_delay;
680 unsigned long scan_nlanes;
681
682 if (sscanf(str, "%lu:%lu", &scan_delay, &scan_nlanes) != 2)
683 return (1);
684
685 /*
686 * We explicitly disallow a delay of zero here, because we key
687 * off this value being non-zero in translate_device(), to
688 * determine if the fault is a ZINJECT_DELAY_IO fault or not.
689 */
690 if (scan_delay == 0)
691 return (1);
692
693 /*
694 * The units for the CLI delay parameter is milliseconds, but
695 * the data passed to the kernel is interpreted as nanoseconds.
696 * Thus we scale the milliseconds to nanoseconds here, and this
697 * nanosecond value is used to pass the delay to the kernel.
698 */
699 *delay = MSEC2NSEC(scan_delay);
700 *nlanes = scan_nlanes;
701
702 return (0);
703}
704
705static int
706parse_frequency(const char *str, uint32_t *percent)
707{
708 double val;
709 char *post;
710
711 val = strtod(str, &post);
712 if (post == NULL || *post != '\0')
713 return (EINVAL);
714
715 /* valid range is [0.0001, 100.0] */
716 val /= 100.0f;
717 if (val < 0.000001f || val > 1.0f)
718 return (ERANGE);
719
720 /* convert to an integer for use by kernel */
721 *percent = ((uint32_t)(val * ZI_PERCENTAGE_MAX));
722
723 return (0);
724}
725
726/*
727 * This function converts a string specifier for DVAs into a bit mask.
728 * The dva's provided by the user should be 0 indexed and separated by
729 * a comma. For example:
730 * "1" -> 0b0010 (0x2)
731 * "0,1" -> 0b0011 (0x3)
732 * "0,1,2" -> 0b0111 (0x7)
733 */
734static int
735parse_dvas(const char *str, uint32_t *dvas_out)
736{
737 const char *c = str;
738 uint32_t mask = 0;
739 boolean_t need_delim = B_FALSE;
740
741 /* max string length is 5 ("0,1,2") */
742 if (strlen(str) > 5 || strlen(str) == 0)
743 return (EINVAL);
744
745 while (*c != '\0') {
746 switch (*c) {
747 case '0':
748 case '1':
749 case '2':
750 /* check for pipe between DVAs */
751 if (need_delim)
752 return (EINVAL);
753
754 /* check if this DVA has been set already */
755 if (mask & (1 << ((*c) - '0')))
756 return (EINVAL);
757
758 mask |= (1 << ((*c) - '0'));
759 need_delim = B_TRUE;
760 break;
761 case ',':
762 need_delim = B_FALSE;
763 break;
764 default:
765 /* check for invalid character */
766 return (EINVAL);
767 }
768 c++;
769 }
770
771 /* check for dangling delimiter */
772 if (!need_delim)
773 return (EINVAL);
774
775 *dvas_out = mask;
776 return (0);
777}
778
779int
780main(int argc, char **argv)
781{
782 int c;
783 char *range = NULL;
784 char *cancel = NULL;
785 char *end;
786 char *raw = NULL;
787 char *device = NULL;
788 int level = 0;
789 int quiet = 0;
790 int error = 0;
791 int domount = 0;
792 int io_type = ZIO_TYPES;
793 int action = VDEV_STATE_UNKNOWN;
794 err_type_t type = TYPE_INVAL;
795 err_type_t label = TYPE_INVAL;
796 zinject_record_t record = { 0 };
797 char pool[MAXNAMELEN] = "";
798 char dataset[MAXNAMELEN] = "";
799 zfs_handle_t *zhp = NULL;
800 int nowrites = 0;
801 int dur_txg = 0;
802 int dur_secs = 0;
803 int ret;
804 int flags = 0;
805 uint32_t dvas = 0;
806
807 if ((g_zfs = libzfs_init()) == NULL) {
808 (void) fprintf(stderr, "%s\n", libzfs_error_init(errno));
809 return (1);
810 }
811
812 libzfs_print_on_error(g_zfs, B_TRUE);
813
814 if ((zfs_fd = open(ZFS_DEV, O_RDWR)) < 0) {
815 (void) fprintf(stderr, "failed to open ZFS device\n");
816 libzfs_fini(g_zfs);
817 return (1);
818 }
819
820 if (argc == 1) {
821 /*
822 * No arguments. Print the available handlers. If there are no
823 * available handlers, direct the user to '-h' for help
824 * information.
825 */
826 if (print_all_handlers() == 0) {
827 (void) printf("No handlers registered.\n");
828 (void) printf("Run 'zinject -h' for usage "
829 "information.\n");
830 }
831 libzfs_fini(g_zfs);
832 return (0);
833 }
834
835 while ((c = getopt(argc, argv,
836 ":aA:b:C:d:D:f:Fg:qhIc:t:T:l:mr:s:e:uL:p:")) != -1) {
837 switch (c) {
838 case 'a':
839 flags |= ZINJECT_FLUSH_ARC;
840 break;
841 case 'A':
842 if (strcasecmp(optarg, "degrade") == 0) {
843 action = VDEV_STATE_DEGRADED;
844 } else if (strcasecmp(optarg, "fault") == 0) {
845 action = VDEV_STATE_FAULTED;
846 } else {
847 (void) fprintf(stderr, "invalid action '%s': "
848 "must be 'degrade' or 'fault'\n", optarg);
849 usage();
850 libzfs_fini(g_zfs);
851 return (1);
852 }
853 break;
854 case 'b':
855 raw = optarg;
856 break;
857 case 'c':
858 cancel = optarg;
859 break;
860 case 'C':
861 ret = parse_dvas(optarg, &dvas);
862 if (ret != 0) {
863 (void) fprintf(stderr, "invalid DVA list '%s': "
864 "DVAs should be 0 indexed and separated by "
865 "commas.\n", optarg);
866 usage();
867 libzfs_fini(g_zfs);
868 return (1);
869 }
870 break;
871 case 'd':
872 device = optarg;
873 break;
874 case 'D':
875 errno = 0;
876 ret = parse_delay(optarg, &record.zi_timer,
877 &record.zi_nlanes);
878 if (ret != 0) {
879
880 (void) fprintf(stderr, "invalid i/o delay "
881 "value: '%s'\n", optarg);
882 usage();
883 libzfs_fini(g_zfs);
884 return (1);
885 }
886 break;
887 case 'e':
888 error = str_to_err(optarg);
889 if (error < 0) {
890 (void) fprintf(stderr, "invalid error type "
891 "'%s': must be one of: io decompress "
892 "decrypt nxio dtl corrupt\n",
893 optarg);
894 usage();
895 libzfs_fini(g_zfs);
896 return (1);
897 }
898 break;
899 case 'f':
900 ret = parse_frequency(optarg, &record.zi_freq);
901 if (ret != 0) {
902 (void) fprintf(stderr, "%sfrequency value must "
903 "be in the range [0.0001, 100.0]\n",
904 ret == EINVAL ? "invalid value: " :
905 ret == ERANGE ? "out of range: " : "");
906 libzfs_fini(g_zfs);
907 return (1);
908 }
909 break;
910 case 'F':
911 record.zi_failfast = B_TRUE;
912 break;
913 case 'g':
914 dur_txg = 1;
915 record.zi_duration = (int)strtol(optarg, &end, 10);
916 if (record.zi_duration <= 0 || *end != '\0') {
917 (void) fprintf(stderr, "invalid duration '%s': "
918 "must be a positive integer\n", optarg);
919 usage();
920 libzfs_fini(g_zfs);
921 return (1);
922 }
923 /* store duration of txgs as its negative */
924 record.zi_duration *= -1;
925 break;
926 case 'h':
927 usage();
928 libzfs_fini(g_zfs);
929 return (0);
930 case 'I':
931 /* default duration, if one hasn't yet been defined */
932 nowrites = 1;
933 if (dur_secs == 0 && dur_txg == 0)
934 record.zi_duration = 30;
935 break;
936 case 'l':
937 level = (int)strtol(optarg, &end, 10);
938 if (*end != '\0') {
939 (void) fprintf(stderr, "invalid level '%s': "
940 "must be an integer\n", optarg);
941 usage();
942 libzfs_fini(g_zfs);
943 return (1);
944 }
945 break;
946 case 'm':
947 domount = 1;
948 break;
949 case 'p':
950 (void) strlcpy(record.zi_func, optarg,
951 sizeof (record.zi_func));
952 record.zi_cmd = ZINJECT_PANIC;
953 break;
954 case 'q':
955 quiet = 1;
956 break;
957 case 'r':
958 range = optarg;
959 flags |= ZINJECT_CALC_RANGE;
960 break;
961 case 's':
962 dur_secs = 1;
963 record.zi_duration = (int)strtol(optarg, &end, 10);
964 if (record.zi_duration <= 0 || *end != '\0') {
965 (void) fprintf(stderr, "invalid duration '%s': "
966 "must be a positive integer\n", optarg);
967 usage();
968 libzfs_fini(g_zfs);
969 return (1);
970 }
971 break;
972 case 'T':
973 if (strcasecmp(optarg, "read") == 0) {
974 io_type = ZIO_TYPE_READ;
975 } else if (strcasecmp(optarg, "write") == 0) {
976 io_type = ZIO_TYPE_WRITE;
977 } else if (strcasecmp(optarg, "free") == 0) {
978 io_type = ZIO_TYPE_FREE;
979 } else if (strcasecmp(optarg, "claim") == 0) {
980 io_type = ZIO_TYPE_CLAIM;
981 } else if (strcasecmp(optarg, "ioctl") == 0) {
982 io_type = ZIO_TYPE_IOCTL;
983 } else if (strcasecmp(optarg, "all") == 0) {
984 io_type = ZIO_TYPES;
985 } else {
986 (void) fprintf(stderr, "invalid I/O type "
987 "'%s': must be 'read', 'write', 'free', "
988 "'claim', 'ioctl' or 'all'\n", optarg);
989 usage();
990 libzfs_fini(g_zfs);
991 return (1);
992 }
993 break;
994 case 't':
995 if ((type = name_to_type(optarg)) == TYPE_INVAL &&
996 !MOS_TYPE(type)) {
997 (void) fprintf(stderr, "invalid type '%s'\n",
998 optarg);
999 usage();
1000 libzfs_fini(g_zfs);
1001 return (1);
1002 }
1003 break;
1004 case 'u':
1005 flags |= ZINJECT_UNLOAD_SPA;
1006 break;
1007 case 'L':
1008 if ((label = name_to_type(optarg)) == TYPE_INVAL &&
1009 !LABEL_TYPE(type)) {
1010 (void) fprintf(stderr, "invalid label type "
1011 "'%s'\n", optarg);
1012 usage();
1013 libzfs_fini(g_zfs);
1014 return (1);
1015 }
1016 break;
1017 case ':':
1018 (void) fprintf(stderr, "option -%c requires an "
1019 "operand\n", optopt);
1020 usage();
1021 libzfs_fini(g_zfs);
1022 return (1);
1023 case '?':
1024 (void) fprintf(stderr, "invalid option '%c'\n",
1025 optopt);
1026 usage();
1027 libzfs_fini(g_zfs);
1028 return (2);
1029 }
1030 }
1031
1032 argc -= optind;
1033 argv += optind;
1034
1035 if (record.zi_duration != 0)
1036 record.zi_cmd = ZINJECT_IGNORED_WRITES;
1037
1038 if (cancel != NULL) {
1039 /*
1040 * '-c' is invalid with any other options.
1041 */
1042 if (raw != NULL || range != NULL || type != TYPE_INVAL ||
1043 level != 0 || record.zi_cmd != ZINJECT_UNINITIALIZED ||
1044 record.zi_freq > 0 || dvas != 0) {
1045 (void) fprintf(stderr, "cancel (-c) incompatible with "
1046 "any other options\n");
1047 usage();
1048 libzfs_fini(g_zfs);
1049 return (2);
1050 }
1051 if (argc != 0) {
1052 (void) fprintf(stderr, "extraneous argument to '-c'\n");
1053 usage();
1054 libzfs_fini(g_zfs);
1055 return (2);
1056 }
1057
1058 if (strcmp(cancel, "all") == 0) {
1059 return (cancel_all_handlers());
1060 } else {
1061 int id = (int)strtol(cancel, &end, 10);
1062 if (*end != '\0') {
1063 (void) fprintf(stderr, "invalid handle id '%s':"
1064 " must be an integer or 'all'\n", cancel);
1065 usage();
1066 libzfs_fini(g_zfs);
1067 return (1);
1068 }
1069 return (cancel_handler(id));
1070 }
1071 }
1072
1073 if (device != NULL) {
1074 /*
1075 * Device (-d) injection uses a completely different mechanism
1076 * for doing injection, so handle it separately here.
1077 */
1078 if (raw != NULL || range != NULL || type != TYPE_INVAL ||
1079 level != 0 || record.zi_cmd != ZINJECT_UNINITIALIZED ||
1080 dvas != 0) {
1081 (void) fprintf(stderr, "device (-d) incompatible with "
1082 "data error injection\n");
1083 usage();
1084 libzfs_fini(g_zfs);
1085 return (2);
1086 }
1087
1088 if (argc != 1) {
1089 (void) fprintf(stderr, "device (-d) injection requires "
1090 "a single pool name\n");
1091 usage();
1092 libzfs_fini(g_zfs);
1093 return (2);
1094 }
1095
1096 (void) strlcpy(pool, argv[0], sizeof (pool));
1097 dataset[0] = '\0';
1098
1099 if (error == ECKSUM) {
1100 (void) fprintf(stderr, "device error type must be "
1101 "'io', 'nxio' or 'corrupt'\n");
1102 libzfs_fini(g_zfs);
1103 return (1);
1104 }
1105
1106 if (error == EILSEQ &&
1107 (record.zi_freq == 0 || io_type != ZIO_TYPE_READ)) {
1108 (void) fprintf(stderr, "device corrupt errors require "
1109 "io type read and a frequency value\n");
1110 libzfs_fini(g_zfs);
1111 return (1);
1112 }
1113
1114 record.zi_iotype = io_type;
1115 if (translate_device(pool, device, label, &record) != 0) {
1116 libzfs_fini(g_zfs);
1117 return (1);
1118 }
1119
1120 if (record.zi_nlanes) {
1121 switch (io_type) {
1122 case ZIO_TYPE_READ:
1123 case ZIO_TYPE_WRITE:
1124 case ZIO_TYPES:
1125 break;
1126 default:
1127 (void) fprintf(stderr, "I/O type for a delay "
1128 "must be 'read' or 'write'\n");
1129 usage();
1130 libzfs_fini(g_zfs);
1131 return (1);
1132 }
1133 }
1134
1135 if (!error)
1136 error = ENXIO;
1137
1138 if (action != VDEV_STATE_UNKNOWN)
1139 return (perform_action(pool, &record, action));
1140
1141 } else if (raw != NULL) {
1142 if (range != NULL || type != TYPE_INVAL || level != 0 ||
1143 record.zi_cmd != ZINJECT_UNINITIALIZED ||
1144 record.zi_freq > 0 || dvas != 0) {
1145 (void) fprintf(stderr, "raw (-b) format with "
1146 "any other options\n");
1147 usage();
1148 libzfs_fini(g_zfs);
1149 return (2);
1150 }
1151
1152 if (argc != 1) {
1153 (void) fprintf(stderr, "raw (-b) format expects a "
1154 "single pool name\n");
1155 usage();
1156 libzfs_fini(g_zfs);
1157 return (2);
1158 }
1159
1160 (void) strlcpy(pool, argv[0], sizeof (pool));
1161 dataset[0] = '\0';
1162
1163 if (error == ENXIO) {
1164 (void) fprintf(stderr, "data error type must be "
1165 "'checksum' or 'io'\n");
1166 libzfs_fini(g_zfs);
1167 return (1);
1168 }
1169
1170 record.zi_cmd = ZINJECT_DATA_FAULT;
1171 if (translate_raw(raw, &record) != 0) {
1172 libzfs_fini(g_zfs);
1173 return (1);
1174 }
1175 if (!error)
1176 error = EIO;
1177 } else if (record.zi_cmd == ZINJECT_PANIC) {
1178 if (raw != NULL || range != NULL || type != TYPE_INVAL ||
1179 level != 0 || device != NULL || record.zi_freq > 0 ||
1180 dvas != 0) {
1181 (void) fprintf(stderr, "panic (-p) incompatible with "
1182 "other options\n");
1183 usage();
1184 libzfs_fini(g_zfs);
1185 return (2);
1186 }
1187
1188 if (argc < 1 || argc > 2) {
1189 (void) fprintf(stderr, "panic (-p) injection requires "
1190 "a single pool name and an optional id\n");
1191 usage();
1192 libzfs_fini(g_zfs);
1193 return (2);
1194 }
1195
1196 (void) strlcpy(pool, argv[0], sizeof (pool));
1197 if (argv[1] != NULL)
1198 record.zi_type = atoi(argv[1]);
1199 dataset[0] = '\0';
1200 } else if (record.zi_cmd == ZINJECT_IGNORED_WRITES) {
1201 if (raw != NULL || range != NULL || type != TYPE_INVAL ||
1202 level != 0 || record.zi_freq > 0 || dvas != 0) {
1203 (void) fprintf(stderr, "hardware failure (-I) "
1204 "incompatible with other options\n");
1205 usage();
1206 libzfs_fini(g_zfs);
1207 return (2);
1208 }
1209
1210 if (nowrites == 0) {
1211 (void) fprintf(stderr, "-s or -g meaningless "
1212 "without -I (ignore writes)\n");
1213 usage();
1214 libzfs_fini(g_zfs);
1215 return (2);
1216 } else if (dur_secs && dur_txg) {
1217 (void) fprintf(stderr, "choose a duration either "
1218 "in seconds (-s) or a number of txgs (-g) "
1219 "but not both\n");
1220 usage();
1221 libzfs_fini(g_zfs);
1222 return (2);
1223 } else if (argc != 1) {
1224 (void) fprintf(stderr, "ignore writes (-I) "
1225 "injection requires a single pool name\n");
1226 usage();
1227 libzfs_fini(g_zfs);
1228 return (2);
1229 }
1230
1231 (void) strlcpy(pool, argv[0], sizeof (pool));
1232 dataset[0] = '\0';
1233 } else if (type == TYPE_INVAL) {
1234 if (flags == 0) {
1235 (void) fprintf(stderr, "at least one of '-b', '-d', "
1236 "'-t', '-a', '-p', '-I' or '-u' "
1237 "must be specified\n");
1238 usage();
1239 libzfs_fini(g_zfs);
1240 return (2);
1241 }
1242
1243 if (argc == 1 && (flags & ZINJECT_UNLOAD_SPA)) {
1244 (void) strlcpy(pool, argv[0], sizeof (pool));
1245 dataset[0] = '\0';
1246 } else if (argc != 0) {
1247 (void) fprintf(stderr, "extraneous argument for "
1248 "'-f'\n");
1249 usage();
1250 libzfs_fini(g_zfs);
1251 return (2);
1252 }
1253
1254 flags |= ZINJECT_NULL;
1255 } else {
1256 if (argc != 1) {
1257 (void) fprintf(stderr, "missing object\n");
1258 usage();
1259 libzfs_fini(g_zfs);
1260 return (2);
1261 }
1262
1263 if (error == ENXIO || error == EILSEQ) {
1264 (void) fprintf(stderr, "data error type must be "
1265 "'checksum' or 'io'\n");
1266 libzfs_fini(g_zfs);
1267 return (1);
1268 }
1269
1270 if (dvas != 0) {
1271 if (error == EACCES || error == EINVAL) {
1272 (void) fprintf(stderr, "the '-C' option may "
1273 "not be used with logical data errors "
1274 "'decrypt' and 'decompress'\n");
1275 libzfs_fini(g_zfs);
1276 return (1);
1277 }
1278
1279 record.zi_dvas = dvas;
1280 }
1281
1282 if (error == EACCES) {
1283 if (type != TYPE_DATA) {
1284 (void) fprintf(stderr, "decryption errors "
1285 "may only be injected for 'data' types\n");
1286 libzfs_fini(g_zfs);
1287 return (1);
1288 }
1289
1290 record.zi_cmd = ZINJECT_DECRYPT_FAULT;
1291 /*
1292 * Internally, ZFS actually uses ECKSUM for decryption
1293 * errors since EACCES is used to indicate the key was
1294 * not found.
1295 */
1296 error = ECKSUM;
1297 } else {
1298 record.zi_cmd = ZINJECT_DATA_FAULT;
1299 }
1300
1301 if (translate_record(type, argv[0], range, level, &record, pool,
1302 dataset) != 0) {
1303 libzfs_fini(g_zfs);
1304 return (1);
1305 }
1306 if (!error)
1307 error = EIO;
1308 }
1309
1310 /*
1311 * If this is pool-wide metadata, unmount everything. The ioctl() will
1312 * unload the pool, so that we trigger spa-wide reopen of metadata next
1313 * time we access the pool.
1314 */
1315 if (dataset[0] != '\0' && domount) {
1316 if ((zhp = zfs_open(g_zfs, dataset,
1317 ZFS_TYPE_DATASET)) == NULL) {
1318 libzfs_fini(g_zfs);
1319 return (1);
1320 }
1321 if (zfs_unmount(zhp, NULL, 0) != 0) {
1322 libzfs_fini(g_zfs);
1323 return (1);
1324 }
1325 }
1326
1327 record.zi_error = error;
1328
1329 ret = register_handler(pool, flags, &record, quiet);
1330
1331 if (dataset[0] != '\0' && domount)
1332 ret = (zfs_mount(zhp, NULL, 0) != 0);
1333
1334 libzfs_fini(g_zfs);
1335
1336 return (ret);
1337}