xref: /openbmc/linux/fs/xfs/xfs_error.c (revision a591525f)
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_format.h"
20 #include "xfs_fs.h"
21 #include "xfs_log_format.h"
22 #include "xfs_trans_resv.h"
23 #include "xfs_mount.h"
24 #include "xfs_errortag.h"
25 #include "xfs_error.h"
26 #include "xfs_sysfs.h"
27 #include "xfs_inode.h"
28 
29 #ifdef DEBUG
30 
31 static unsigned int xfs_errortag_random_default[] = {
32 	XFS_RANDOM_DEFAULT,
33 	XFS_RANDOM_IFLUSH_1,
34 	XFS_RANDOM_IFLUSH_2,
35 	XFS_RANDOM_IFLUSH_3,
36 	XFS_RANDOM_IFLUSH_4,
37 	XFS_RANDOM_IFLUSH_5,
38 	XFS_RANDOM_IFLUSH_6,
39 	XFS_RANDOM_DA_READ_BUF,
40 	XFS_RANDOM_BTREE_CHECK_LBLOCK,
41 	XFS_RANDOM_BTREE_CHECK_SBLOCK,
42 	XFS_RANDOM_ALLOC_READ_AGF,
43 	XFS_RANDOM_IALLOC_READ_AGI,
44 	XFS_RANDOM_ITOBP_INOTOBP,
45 	XFS_RANDOM_IUNLINK,
46 	XFS_RANDOM_IUNLINK_REMOVE,
47 	XFS_RANDOM_DIR_INO_VALIDATE,
48 	XFS_RANDOM_BULKSTAT_READ_CHUNK,
49 	XFS_RANDOM_IODONE_IOERR,
50 	XFS_RANDOM_STRATREAD_IOERR,
51 	XFS_RANDOM_STRATCMPL_IOERR,
52 	XFS_RANDOM_DIOWRITE_IOERR,
53 	XFS_RANDOM_BMAPIFORMAT,
54 	XFS_RANDOM_FREE_EXTENT,
55 	XFS_RANDOM_RMAP_FINISH_ONE,
56 	XFS_RANDOM_REFCOUNT_CONTINUE_UPDATE,
57 	XFS_RANDOM_REFCOUNT_FINISH_ONE,
58 	XFS_RANDOM_BMAP_FINISH_ONE,
59 	XFS_RANDOM_AG_RESV_CRITICAL,
60 	XFS_RANDOM_DROP_WRITES,
61 	XFS_RANDOM_LOG_BAD_CRC,
62 	XFS_RANDOM_LOG_ITEM_PIN,
63 	XFS_RANDOM_BUF_LRU_REF,
64 	XFS_RANDOM_FORCE_SCRUB_REPAIR,
65 };
66 
67 struct xfs_errortag_attr {
68 	struct attribute	attr;
69 	unsigned int		tag;
70 };
71 
72 static inline struct xfs_errortag_attr *
73 to_attr(struct attribute *attr)
74 {
75 	return container_of(attr, struct xfs_errortag_attr, attr);
76 }
77 
78 static inline struct xfs_mount *
79 to_mp(struct kobject *kobject)
80 {
81 	struct xfs_kobj *kobj = to_kobj(kobject);
82 
83 	return container_of(kobj, struct xfs_mount, m_errortag_kobj);
84 }
85 
86 STATIC ssize_t
87 xfs_errortag_attr_store(
88 	struct kobject		*kobject,
89 	struct attribute	*attr,
90 	const char		*buf,
91 	size_t			count)
92 {
93 	struct xfs_mount	*mp = to_mp(kobject);
94 	struct xfs_errortag_attr *xfs_attr = to_attr(attr);
95 	int			ret;
96 	unsigned int		val;
97 
98 	if (strcmp(buf, "default") == 0) {
99 		val = xfs_errortag_random_default[xfs_attr->tag];
100 	} else {
101 		ret = kstrtouint(buf, 0, &val);
102 		if (ret)
103 			return ret;
104 	}
105 
106 	ret = xfs_errortag_set(mp, xfs_attr->tag, val);
107 	if (ret)
108 		return ret;
109 	return count;
110 }
111 
112 STATIC ssize_t
113 xfs_errortag_attr_show(
114 	struct kobject		*kobject,
115 	struct attribute	*attr,
116 	char			*buf)
117 {
118 	struct xfs_mount	*mp = to_mp(kobject);
119 	struct xfs_errortag_attr *xfs_attr = to_attr(attr);
120 
121 	return snprintf(buf, PAGE_SIZE, "%u\n",
122 			xfs_errortag_get(mp, xfs_attr->tag));
123 }
124 
125 static const struct sysfs_ops xfs_errortag_sysfs_ops = {
126 	.show = xfs_errortag_attr_show,
127 	.store = xfs_errortag_attr_store,
128 };
129 
130 #define XFS_ERRORTAG_ATTR_RW(_name, _tag) \
131 static struct xfs_errortag_attr xfs_errortag_attr_##_name = {		\
132 	.attr = {.name = __stringify(_name),				\
133 		 .mode = VERIFY_OCTAL_PERMISSIONS(S_IWUSR | S_IRUGO) },	\
134 	.tag	= (_tag),						\
135 }
136 
137 #define XFS_ERRORTAG_ATTR_LIST(_name) &xfs_errortag_attr_##_name.attr
138 
139 XFS_ERRORTAG_ATTR_RW(noerror,		XFS_ERRTAG_NOERROR);
140 XFS_ERRORTAG_ATTR_RW(iflush1,		XFS_ERRTAG_IFLUSH_1);
141 XFS_ERRORTAG_ATTR_RW(iflush2,		XFS_ERRTAG_IFLUSH_2);
142 XFS_ERRORTAG_ATTR_RW(iflush3,		XFS_ERRTAG_IFLUSH_3);
143 XFS_ERRORTAG_ATTR_RW(iflush4,		XFS_ERRTAG_IFLUSH_4);
144 XFS_ERRORTAG_ATTR_RW(iflush5,		XFS_ERRTAG_IFLUSH_5);
145 XFS_ERRORTAG_ATTR_RW(iflush6,		XFS_ERRTAG_IFLUSH_6);
146 XFS_ERRORTAG_ATTR_RW(dareadbuf,		XFS_ERRTAG_DA_READ_BUF);
147 XFS_ERRORTAG_ATTR_RW(btree_chk_lblk,	XFS_ERRTAG_BTREE_CHECK_LBLOCK);
148 XFS_ERRORTAG_ATTR_RW(btree_chk_sblk,	XFS_ERRTAG_BTREE_CHECK_SBLOCK);
149 XFS_ERRORTAG_ATTR_RW(readagf,		XFS_ERRTAG_ALLOC_READ_AGF);
150 XFS_ERRORTAG_ATTR_RW(readagi,		XFS_ERRTAG_IALLOC_READ_AGI);
151 XFS_ERRORTAG_ATTR_RW(itobp,		XFS_ERRTAG_ITOBP_INOTOBP);
152 XFS_ERRORTAG_ATTR_RW(iunlink,		XFS_ERRTAG_IUNLINK);
153 XFS_ERRORTAG_ATTR_RW(iunlinkrm,		XFS_ERRTAG_IUNLINK_REMOVE);
154 XFS_ERRORTAG_ATTR_RW(dirinovalid,	XFS_ERRTAG_DIR_INO_VALIDATE);
155 XFS_ERRORTAG_ATTR_RW(bulkstat,		XFS_ERRTAG_BULKSTAT_READ_CHUNK);
156 XFS_ERRORTAG_ATTR_RW(logiodone,		XFS_ERRTAG_IODONE_IOERR);
157 XFS_ERRORTAG_ATTR_RW(stratread,		XFS_ERRTAG_STRATREAD_IOERR);
158 XFS_ERRORTAG_ATTR_RW(stratcmpl,		XFS_ERRTAG_STRATCMPL_IOERR);
159 XFS_ERRORTAG_ATTR_RW(diowrite,		XFS_ERRTAG_DIOWRITE_IOERR);
160 XFS_ERRORTAG_ATTR_RW(bmapifmt,		XFS_ERRTAG_BMAPIFORMAT);
161 XFS_ERRORTAG_ATTR_RW(free_extent,	XFS_ERRTAG_FREE_EXTENT);
162 XFS_ERRORTAG_ATTR_RW(rmap_finish_one,	XFS_ERRTAG_RMAP_FINISH_ONE);
163 XFS_ERRORTAG_ATTR_RW(refcount_continue_update,	XFS_ERRTAG_REFCOUNT_CONTINUE_UPDATE);
164 XFS_ERRORTAG_ATTR_RW(refcount_finish_one,	XFS_ERRTAG_REFCOUNT_FINISH_ONE);
165 XFS_ERRORTAG_ATTR_RW(bmap_finish_one,	XFS_ERRTAG_BMAP_FINISH_ONE);
166 XFS_ERRORTAG_ATTR_RW(ag_resv_critical,	XFS_ERRTAG_AG_RESV_CRITICAL);
167 XFS_ERRORTAG_ATTR_RW(drop_writes,	XFS_ERRTAG_DROP_WRITES);
168 XFS_ERRORTAG_ATTR_RW(log_bad_crc,	XFS_ERRTAG_LOG_BAD_CRC);
169 XFS_ERRORTAG_ATTR_RW(log_item_pin,	XFS_ERRTAG_LOG_ITEM_PIN);
170 XFS_ERRORTAG_ATTR_RW(buf_lru_ref,	XFS_ERRTAG_BUF_LRU_REF);
171 XFS_ERRORTAG_ATTR_RW(force_repair,	XFS_ERRTAG_FORCE_SCRUB_REPAIR);
172 
173 static struct attribute *xfs_errortag_attrs[] = {
174 	XFS_ERRORTAG_ATTR_LIST(noerror),
175 	XFS_ERRORTAG_ATTR_LIST(iflush1),
176 	XFS_ERRORTAG_ATTR_LIST(iflush2),
177 	XFS_ERRORTAG_ATTR_LIST(iflush3),
178 	XFS_ERRORTAG_ATTR_LIST(iflush4),
179 	XFS_ERRORTAG_ATTR_LIST(iflush5),
180 	XFS_ERRORTAG_ATTR_LIST(iflush6),
181 	XFS_ERRORTAG_ATTR_LIST(dareadbuf),
182 	XFS_ERRORTAG_ATTR_LIST(btree_chk_lblk),
183 	XFS_ERRORTAG_ATTR_LIST(btree_chk_sblk),
184 	XFS_ERRORTAG_ATTR_LIST(readagf),
185 	XFS_ERRORTAG_ATTR_LIST(readagi),
186 	XFS_ERRORTAG_ATTR_LIST(itobp),
187 	XFS_ERRORTAG_ATTR_LIST(iunlink),
188 	XFS_ERRORTAG_ATTR_LIST(iunlinkrm),
189 	XFS_ERRORTAG_ATTR_LIST(dirinovalid),
190 	XFS_ERRORTAG_ATTR_LIST(bulkstat),
191 	XFS_ERRORTAG_ATTR_LIST(logiodone),
192 	XFS_ERRORTAG_ATTR_LIST(stratread),
193 	XFS_ERRORTAG_ATTR_LIST(stratcmpl),
194 	XFS_ERRORTAG_ATTR_LIST(diowrite),
195 	XFS_ERRORTAG_ATTR_LIST(bmapifmt),
196 	XFS_ERRORTAG_ATTR_LIST(free_extent),
197 	XFS_ERRORTAG_ATTR_LIST(rmap_finish_one),
198 	XFS_ERRORTAG_ATTR_LIST(refcount_continue_update),
199 	XFS_ERRORTAG_ATTR_LIST(refcount_finish_one),
200 	XFS_ERRORTAG_ATTR_LIST(bmap_finish_one),
201 	XFS_ERRORTAG_ATTR_LIST(ag_resv_critical),
202 	XFS_ERRORTAG_ATTR_LIST(drop_writes),
203 	XFS_ERRORTAG_ATTR_LIST(log_bad_crc),
204 	XFS_ERRORTAG_ATTR_LIST(log_item_pin),
205 	XFS_ERRORTAG_ATTR_LIST(buf_lru_ref),
206 	XFS_ERRORTAG_ATTR_LIST(force_repair),
207 	NULL,
208 };
209 
210 static struct kobj_type xfs_errortag_ktype = {
211 	.release = xfs_sysfs_release,
212 	.sysfs_ops = &xfs_errortag_sysfs_ops,
213 	.default_attrs = xfs_errortag_attrs,
214 };
215 
216 int
217 xfs_errortag_init(
218 	struct xfs_mount	*mp)
219 {
220 	mp->m_errortag = kmem_zalloc(sizeof(unsigned int) * XFS_ERRTAG_MAX,
221 			KM_SLEEP | KM_MAYFAIL);
222 	if (!mp->m_errortag)
223 		return -ENOMEM;
224 
225 	return xfs_sysfs_init(&mp->m_errortag_kobj, &xfs_errortag_ktype,
226 			       &mp->m_kobj, "errortag");
227 }
228 
229 void
230 xfs_errortag_del(
231 	struct xfs_mount	*mp)
232 {
233 	xfs_sysfs_del(&mp->m_errortag_kobj);
234 	kmem_free(mp->m_errortag);
235 }
236 
237 bool
238 xfs_errortag_test(
239 	struct xfs_mount	*mp,
240 	const char		*expression,
241 	const char		*file,
242 	int			line,
243 	unsigned int		error_tag)
244 {
245 	unsigned int		randfactor;
246 
247 	/*
248 	 * To be able to use error injection anywhere, we need to ensure error
249 	 * injection mechanism is already initialized.
250 	 *
251 	 * Code paths like I/O completion can be called before the
252 	 * initialization is complete, but be able to inject errors in such
253 	 * places is still useful.
254 	 */
255 	if (!mp->m_errortag)
256 		return false;
257 
258 	ASSERT(error_tag < XFS_ERRTAG_MAX);
259 	randfactor = mp->m_errortag[error_tag];
260 	if (!randfactor || prandom_u32() % randfactor)
261 		return false;
262 
263 	xfs_warn_ratelimited(mp,
264 "Injecting error (%s) at file %s, line %d, on filesystem \"%s\"",
265 			expression, file, line, mp->m_fsname);
266 	return true;
267 }
268 
269 int
270 xfs_errortag_get(
271 	struct xfs_mount	*mp,
272 	unsigned int		error_tag)
273 {
274 	if (error_tag >= XFS_ERRTAG_MAX)
275 		return -EINVAL;
276 
277 	return mp->m_errortag[error_tag];
278 }
279 
280 int
281 xfs_errortag_set(
282 	struct xfs_mount	*mp,
283 	unsigned int		error_tag,
284 	unsigned int		tag_value)
285 {
286 	if (error_tag >= XFS_ERRTAG_MAX)
287 		return -EINVAL;
288 
289 	mp->m_errortag[error_tag] = tag_value;
290 	return 0;
291 }
292 
293 int
294 xfs_errortag_add(
295 	struct xfs_mount	*mp,
296 	unsigned int		error_tag)
297 {
298 	if (error_tag >= XFS_ERRTAG_MAX)
299 		return -EINVAL;
300 
301 	return xfs_errortag_set(mp, error_tag,
302 			xfs_errortag_random_default[error_tag]);
303 }
304 
305 int
306 xfs_errortag_clearall(
307 	struct xfs_mount	*mp)
308 {
309 	memset(mp->m_errortag, 0, sizeof(unsigned int) * XFS_ERRTAG_MAX);
310 	return 0;
311 }
312 #endif /* DEBUG */
313 
314 void
315 xfs_error_report(
316 	const char		*tag,
317 	int			level,
318 	struct xfs_mount	*mp,
319 	const char		*filename,
320 	int			linenum,
321 	xfs_failaddr_t		failaddr)
322 {
323 	if (level <= xfs_error_level) {
324 		xfs_alert_tag(mp, XFS_PTAG_ERROR_REPORT,
325 		"Internal error %s at line %d of file %s.  Caller %pS",
326 			    tag, linenum, filename, failaddr);
327 
328 		xfs_stack_trace();
329 	}
330 }
331 
332 void
333 xfs_corruption_error(
334 	const char		*tag,
335 	int			level,
336 	struct xfs_mount	*mp,
337 	void			*p,
338 	const char		*filename,
339 	int			linenum,
340 	xfs_failaddr_t		failaddr)
341 {
342 	if (level <= xfs_error_level)
343 		xfs_hex_dump(p, XFS_CORRUPTION_DUMP_LEN);
344 	xfs_error_report(tag, level, mp, filename, linenum, failaddr);
345 	xfs_alert(mp, "Corruption detected. Unmount and run xfs_repair");
346 }
347 
348 /*
349  * Warnings specifically for verifier errors.  Differentiate CRC vs. invalid
350  * values, and omit the stack trace unless the error level is tuned high.
351  */
352 void
353 xfs_buf_verifier_error(
354 	struct xfs_buf		*bp,
355 	int			error,
356 	const char		*name,
357 	void			*buf,
358 	size_t			bufsz,
359 	xfs_failaddr_t		failaddr)
360 {
361 	struct xfs_mount	*mp = bp->b_target->bt_mount;
362 	xfs_failaddr_t		fa;
363 	int			sz;
364 
365 	fa = failaddr ? failaddr : __return_address;
366 	__xfs_buf_ioerror(bp, error, fa);
367 
368 	xfs_alert(mp, "Metadata %s detected at %pS, %s block 0x%llx %s",
369 		  bp->b_error == -EFSBADCRC ? "CRC error" : "corruption",
370 		  fa, bp->b_ops->name, bp->b_bn, name);
371 
372 	xfs_alert(mp, "Unmount and run xfs_repair");
373 
374 	if (xfs_error_level >= XFS_ERRLEVEL_LOW) {
375 		sz = min_t(size_t, XFS_CORRUPTION_DUMP_LEN, bufsz);
376 		xfs_alert(mp, "First %d bytes of corrupted metadata buffer:",
377 				sz);
378 		xfs_hex_dump(buf, sz);
379 	}
380 
381 	if (xfs_error_level >= XFS_ERRLEVEL_HIGH)
382 		xfs_stack_trace();
383 }
384 
385 /*
386  * Warnings specifically for verifier errors.  Differentiate CRC vs. invalid
387  * values, and omit the stack trace unless the error level is tuned high.
388  */
389 void
390 xfs_verifier_error(
391 	struct xfs_buf		*bp,
392 	int			error,
393 	xfs_failaddr_t		failaddr)
394 {
395 	return xfs_buf_verifier_error(bp, error, "", xfs_buf_offset(bp, 0),
396 			XFS_CORRUPTION_DUMP_LEN, failaddr);
397 }
398 
399 /*
400  * Warnings for inode corruption problems.  Don't bother with the stack
401  * trace unless the error level is turned up high.
402  */
403 void
404 xfs_inode_verifier_error(
405 	struct xfs_inode	*ip,
406 	int			error,
407 	const char		*name,
408 	void			*buf,
409 	size_t			bufsz,
410 	xfs_failaddr_t		failaddr)
411 {
412 	struct xfs_mount	*mp = ip->i_mount;
413 	xfs_failaddr_t		fa;
414 	int			sz;
415 
416 	fa = failaddr ? failaddr : __return_address;
417 
418 	xfs_alert(mp, "Metadata %s detected at %pS, inode 0x%llx %s",
419 		  error == -EFSBADCRC ? "CRC error" : "corruption",
420 		  fa, ip->i_ino, name);
421 
422 	xfs_alert(mp, "Unmount and run xfs_repair");
423 
424 	if (buf && xfs_error_level >= XFS_ERRLEVEL_LOW) {
425 		sz = min_t(size_t, XFS_CORRUPTION_DUMP_LEN, bufsz);
426 		xfs_alert(mp, "First %d bytes of corrupted metadata buffer:",
427 				sz);
428 		xfs_hex_dump(buf, sz);
429 	}
430 
431 	if (xfs_error_level >= XFS_ERRLEVEL_HIGH)
432 		xfs_stack_trace();
433 }
434