]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - io/inject.c
xfsprogs: Release v6.7.0
[thirdparty/xfsprogs-dev.git] / io / inject.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2004-2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
5 */
6
7 #include "command.h"
8 #include "input.h"
9 #include "init.h"
10 #include "io.h"
11 #include "xfs_errortag.h"
12
13 static cmdinfo_t inject_cmd;
14
15 static int
16 error_tag(char *name)
17 {
18 static struct {
19 int tag;
20 char *name;
21 } *e, eflags[] = {
22 { XFS_ERRTAG_NOERROR, "noerror" },
23 { XFS_ERRTAG_IFLUSH_1, "iflush1" },
24 { XFS_ERRTAG_IFLUSH_2, "iflush2" },
25 { XFS_ERRTAG_IFLUSH_3, "iflush3" },
26 { XFS_ERRTAG_IFLUSH_4, "iflush4" },
27 { XFS_ERRTAG_IFLUSH_5, "iflush5" },
28 { XFS_ERRTAG_IFLUSH_6, "iflush6" },
29 { XFS_ERRTAG_DA_READ_BUF, "dareadbuf" },
30 { XFS_ERRTAG_BTREE_CHECK_LBLOCK, "btree_chk_lblk" },
31 { XFS_ERRTAG_BTREE_CHECK_SBLOCK, "btree_chk_sblk" },
32 { XFS_ERRTAG_ALLOC_READ_AGF, "readagf" },
33 { XFS_ERRTAG_IALLOC_READ_AGI, "readagi" },
34 { XFS_ERRTAG_ITOBP_INOTOBP, "itobp" },
35 { XFS_ERRTAG_IUNLINK, "iunlink" },
36 { XFS_ERRTAG_IUNLINK_REMOVE, "iunlinkrm" },
37 { XFS_ERRTAG_DIR_INO_VALIDATE, "dirinovalid" },
38 { XFS_ERRTAG_BULKSTAT_READ_CHUNK, "bulkstat" },
39 { XFS_ERRTAG_IODONE_IOERR, "logiodone" },
40 { XFS_ERRTAG_STRATREAD_IOERR, "stratread" },
41 { XFS_ERRTAG_STRATCMPL_IOERR, "stratcmpl" },
42 { XFS_ERRTAG_DIOWRITE_IOERR, "diowrite" },
43 { XFS_ERRTAG_BMAPIFORMAT, "bmapifmt" },
44 { XFS_ERRTAG_FREE_EXTENT, "free_extent" },
45 { XFS_ERRTAG_RMAP_FINISH_ONE, "rmap_finish_one" },
46 { XFS_ERRTAG_REFCOUNT_CONTINUE_UPDATE, "refcount_continue_update" },
47 { XFS_ERRTAG_REFCOUNT_FINISH_ONE, "refcount_finish_one" },
48 { XFS_ERRTAG_BMAP_FINISH_ONE, "bmap_finish_one" },
49 { XFS_ERRTAG_AG_RESV_CRITICAL, "ag_resv_critical" },
50 { XFS_ERRTAG_DROP_WRITES, "drop_writes" },
51 { XFS_ERRTAG_LOG_BAD_CRC, "log_bad_crc" },
52 { XFS_ERRTAG_LOG_ITEM_PIN, "log_item_pin" },
53 { XFS_ERRTAG_BUF_LRU_REF, "buf_lru_ref" },
54 { XFS_ERRTAG_FORCE_SCRUB_REPAIR, "force_repair" },
55 { XFS_ERRTAG_FORCE_SUMMARY_RECALC, "bad_summary" },
56 { XFS_ERRTAG_IUNLINK_FALLBACK, "iunlink_fallback" },
57 { XFS_ERRTAG_MAX, NULL }
58 };
59 int count;
60
61 /*
62 * If this fails make sure every tag is defined in the array above,
63 * see xfs_errortag_attrs in kernelspace.
64 */
65 BUILD_BUG_ON(sizeof(eflags) != (XFS_ERRTAG_MAX + 1) * sizeof(*e));
66
67 /* Search for a name */
68 if (name) {
69 for (e = eflags; e->name; e++)
70 if (strcmp(name, e->name) == 0)
71 return e->tag;
72 return -1;
73 }
74
75 /* Dump all the names */
76 fputs("tags: [ ", stdout);
77 for (count = 0, e = eflags; e->name; e++, count++) {
78 if (count) {
79 fputs(", ", stdout);
80 if (!(count % 5))
81 fputs("\n\t", stdout);
82 }
83 fputs(e->name, stdout);
84 }
85 fputs(" ]\n", stdout);
86 return 0;
87 }
88
89 static void
90 inject_help(void)
91 {
92 printf(_(
93 "\n"
94 " inject errors into the filesystem of the currently open file\n"
95 "\n"
96 " Example:\n"
97 " 'inject readagf' - cause errors on allocation group freespace reads\n"
98 "\n"
99 " Causes the kernel to generate and react to errors within XFS, provided\n"
100 " the XFS kernel code has been built with debugging features enabled.\n"
101 " With no arguments, displays the list of error injection tags.\n"
102 "\n"));
103 }
104
105 static int
106 inject_f(
107 int argc,
108 char **argv)
109 {
110 xfs_error_injection_t error;
111 int command = XFS_IOC_ERROR_INJECTION;
112
113 if (argc == 1)
114 return error_tag(NULL);
115
116 while (--argc > 0) {
117 error.fd = file->fd;
118 if ((error.errtag = error_tag(argv[argc])) < 0) {
119 fprintf(stderr, _("no such tag -- %s\n"), argv[1]);
120 continue;
121 }
122 if (error.errtag == XFS_ERRTAG_NOERROR)
123 command = XFS_IOC_ERROR_CLEARALL;
124 if ((xfsctl(file->name, file->fd, command, &error)) < 0) {
125 perror("XFS_IOC_ERROR_INJECTION");
126 continue;
127 }
128 }
129 return 0;
130 }
131
132 void
133 inject_init(void)
134 {
135 inject_cmd.name = "inject";
136 inject_cmd.cfunc = inject_f;
137 inject_cmd.argmin = 0;
138 inject_cmd.argmax = -1;
139 inject_cmd.flags = CMD_NOMAP_OK | CMD_FLAG_ONESHOT;
140 inject_cmd.args = _("[tag ...]");
141 inject_cmd.oneline = _("inject errors into a filesystem");
142 inject_cmd.help = inject_help;
143
144 if (expert)
145 add_command(&inject_cmd);
146 }