xref: /linux/fs/xfs/xfs_error.c (revision d8327c784b51b57dac2c26cfad87dce0d68dfd98)
1 /*
2  * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
3  * All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write the Free Software Foundation,
16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_types.h"
21 #include "xfs_log.h"
22 #include "xfs_inum.h"
23 #include "xfs_trans.h"
24 #include "xfs_sb.h"
25 #include "xfs_dir.h"
26 #include "xfs_dir2.h"
27 #include "xfs_dmapi.h"
28 #include "xfs_mount.h"
29 #include "xfs_bmap_btree.h"
30 #include "xfs_dir_sf.h"
31 #include "xfs_dir2_sf.h"
32 #include "xfs_attr_sf.h"
33 #include "xfs_dinode.h"
34 #include "xfs_inode.h"
35 #include "xfs_utils.h"
36 #include "xfs_error.h"
37 
38 #ifdef DEBUG
39 
40 int	xfs_etrap[XFS_ERROR_NTRAP] = {
41 	0,
42 };
43 
44 int
45 xfs_error_trap(int e)
46 {
47 	int i;
48 
49 	if (!e)
50 		return 0;
51 	for (i = 0; i < XFS_ERROR_NTRAP; i++) {
52 		if (xfs_etrap[i] == 0)
53 			break;
54 		if (e != xfs_etrap[i])
55 			continue;
56 		cmn_err(CE_NOTE, "xfs_error_trap: error %d", e);
57 		BUG();
58 		break;
59 	}
60 	return e;
61 }
62 #endif
63 
64 #if (defined(DEBUG) || defined(INDUCE_IO_ERROR))
65 
66 int	xfs_etest[XFS_NUM_INJECT_ERROR];
67 int64_t	xfs_etest_fsid[XFS_NUM_INJECT_ERROR];
68 char *	xfs_etest_fsname[XFS_NUM_INJECT_ERROR];
69 
70 void
71 xfs_error_test_init(void)
72 {
73 	memset(xfs_etest, 0, sizeof(xfs_etest));
74 	memset(xfs_etest_fsid, 0, sizeof(xfs_etest_fsid));
75 	memset(xfs_etest_fsname, 0, sizeof(xfs_etest_fsname));
76 }
77 
78 int
79 xfs_error_test(int error_tag, int *fsidp, char *expression,
80 	       int line, char *file, unsigned long randfactor)
81 {
82 	int i;
83 	int64_t fsid;
84 
85 	if (random() % randfactor)
86 		return 0;
87 
88 	memcpy(&fsid, fsidp, sizeof(xfs_fsid_t));
89 
90 	for (i = 0; i < XFS_NUM_INJECT_ERROR; i++)  {
91 		if (xfs_etest[i] == error_tag && xfs_etest_fsid[i] == fsid) {
92 			cmn_err(CE_WARN,
93 	"Injecting error (%s) at file %s, line %d, on filesystem \"%s\"",
94 				expression, file, line, xfs_etest_fsname[i]);
95 			return 1;
96 		}
97 	}
98 
99 	return 0;
100 }
101 
102 int
103 xfs_errortag_add(int error_tag, xfs_mount_t *mp)
104 {
105 	int i;
106 	int len;
107 	int64_t fsid;
108 
109 	memcpy(&fsid, mp->m_fixedfsid, sizeof(xfs_fsid_t));
110 
111 	for (i = 0; i < XFS_NUM_INJECT_ERROR; i++)  {
112 		if (xfs_etest_fsid[i] == fsid && xfs_etest[i] == error_tag) {
113 			cmn_err(CE_WARN, "XFS error tag #%d on", error_tag);
114 			return 0;
115 		}
116 	}
117 
118 	for (i = 0; i < XFS_NUM_INJECT_ERROR; i++)  {
119 		if (xfs_etest[i] == 0) {
120 			cmn_err(CE_WARN, "Turned on XFS error tag #%d",
121 				error_tag);
122 			xfs_etest[i] = error_tag;
123 			xfs_etest_fsid[i] = fsid;
124 			len = strlen(mp->m_fsname);
125 			xfs_etest_fsname[i] = kmem_alloc(len + 1, KM_SLEEP);
126 			strcpy(xfs_etest_fsname[i], mp->m_fsname);
127 			return 0;
128 		}
129 	}
130 
131 	cmn_err(CE_WARN, "error tag overflow, too many turned on");
132 
133 	return 1;
134 }
135 
136 int
137 xfs_errortag_clear(int error_tag, xfs_mount_t *mp)
138 {
139 	int i;
140 	int64_t fsid;
141 
142 	memcpy(&fsid, mp->m_fixedfsid, sizeof(xfs_fsid_t));
143 
144 	for (i = 0; i < XFS_NUM_INJECT_ERROR; i++) {
145 		if (xfs_etest_fsid[i] == fsid && xfs_etest[i] == error_tag) {
146 			xfs_etest[i] = 0;
147 			xfs_etest_fsid[i] = 0LL;
148 			kmem_free(xfs_etest_fsname[i],
149 				  strlen(xfs_etest_fsname[i]) + 1);
150 			xfs_etest_fsname[i] = NULL;
151 			cmn_err(CE_WARN, "Cleared XFS error tag #%d",
152 				error_tag);
153 			return 0;
154 		}
155 	}
156 
157 	cmn_err(CE_WARN, "XFS error tag %d not on", error_tag);
158 
159 	return 1;
160 }
161 
162 int
163 xfs_errortag_clearall_umount(int64_t fsid, char *fsname, int loud)
164 {
165 	int i;
166 	int cleared = 0;
167 
168 	for (i = 0; i < XFS_NUM_INJECT_ERROR; i++) {
169 		if ((fsid == 0LL || xfs_etest_fsid[i] == fsid) &&
170 		     xfs_etest[i] != 0) {
171 			cleared = 1;
172 			cmn_err(CE_WARN, "Clearing XFS error tag #%d",
173 				xfs_etest[i]);
174 			xfs_etest[i] = 0;
175 			xfs_etest_fsid[i] = 0LL;
176 			kmem_free(xfs_etest_fsname[i],
177 				  strlen(xfs_etest_fsname[i]) + 1);
178 			xfs_etest_fsname[i] = NULL;
179 		}
180 	}
181 
182 	if (loud || cleared)
183 		cmn_err(CE_WARN,
184 			"Cleared all XFS error tags for filesystem \"%s\"",
185 			fsname);
186 
187 	return 0;
188 }
189 
190 int
191 xfs_errortag_clearall(xfs_mount_t *mp)
192 {
193 	int64_t fsid;
194 
195 	memcpy(&fsid, mp->m_fixedfsid, sizeof(xfs_fsid_t));
196 
197 	return xfs_errortag_clearall_umount(fsid, mp->m_fsname, 1);
198 }
199 #endif /* DEBUG || INDUCE_IO_ERROR */
200 
201 static void
202 xfs_fs_vcmn_err(int level, xfs_mount_t *mp, char *fmt, va_list ap)
203 {
204 	if (mp != NULL) {
205 		char	*newfmt;
206 		int	len = 16 + mp->m_fsname_len + strlen(fmt);
207 
208 		newfmt = kmem_alloc(len, KM_SLEEP);
209 		sprintf(newfmt, "Filesystem \"%s\": %s", mp->m_fsname, fmt);
210 		icmn_err(level, newfmt, ap);
211 		kmem_free(newfmt, len);
212 	} else {
213 		icmn_err(level, fmt, ap);
214 	}
215 }
216 
217 void
218 xfs_fs_cmn_err(int level, xfs_mount_t *mp, char *fmt, ...)
219 {
220 	va_list ap;
221 
222 	va_start(ap, fmt);
223 	xfs_fs_vcmn_err(level, mp, fmt, ap);
224 	va_end(ap);
225 }
226 
227 void
228 xfs_cmn_err(int panic_tag, int level, xfs_mount_t *mp, char *fmt, ...)
229 {
230 	va_list ap;
231 
232 #ifdef DEBUG
233 	xfs_panic_mask |= XFS_PTAG_SHUTDOWN_CORRUPT;
234 #endif
235 
236 	if (xfs_panic_mask && (xfs_panic_mask & panic_tag)
237 	    && (level & CE_ALERT)) {
238 		level &= ~CE_ALERT;
239 		level |= CE_PANIC;
240 		cmn_err(CE_ALERT, "XFS: Transforming an alert into a BUG.");
241 	}
242 	va_start(ap, fmt);
243 	xfs_fs_vcmn_err(level, mp, fmt, ap);
244 	va_end(ap);
245 }
246 
247 void
248 xfs_error_report(
249 	char		*tag,
250 	int		level,
251 	xfs_mount_t	*mp,
252 	char		*fname,
253 	int		linenum,
254 	inst_t		*ra)
255 {
256 	if (level <= xfs_error_level) {
257 		xfs_cmn_err(XFS_PTAG_ERROR_REPORT,
258 			    CE_ALERT, mp,
259 		"XFS internal error %s at line %d of file %s.  Caller 0x%p\n",
260 			    tag, linenum, fname, ra);
261 
262 		xfs_stack_trace();
263 	}
264 }
265 
266 STATIC void
267 xfs_hex_dump(void *p, int length)
268 {
269 	__uint8_t *uip = (__uint8_t*)p;
270 	int	i;
271 	char	sbuf[128], *s;
272 
273 	s = sbuf;
274 	*s = '\0';
275 	for (i=0; i<length; i++, uip++) {
276 		if ((i % 16) == 0) {
277 			if (*s != '\0')
278 				cmn_err(CE_ALERT, "%s\n", sbuf);
279 			s = sbuf;
280 			sprintf(s, "0x%x: ", i);
281 			while( *s != '\0')
282 				s++;
283 		}
284 		sprintf(s, "%02x ", *uip);
285 
286 		/*
287 		 * the kernel sprintf is a void; user sprintf returns
288 		 * the sprintf'ed string's length.  Find the new end-
289 		 * of-string
290 		 */
291 		while( *s != '\0')
292 			s++;
293 	}
294 	cmn_err(CE_ALERT, "%s\n", sbuf);
295 }
296 
297 void
298 xfs_corruption_error(
299 	char		*tag,
300 	int		level,
301 	xfs_mount_t	*mp,
302 	void		*p,
303 	char		*fname,
304 	int		linenum,
305 	inst_t		*ra)
306 {
307 	if (level <= xfs_error_level)
308 		xfs_hex_dump(p, 16);
309 	xfs_error_report(tag, level, mp, fname, linenum, ra);
310 }
311