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