xref: /linux/fs/xfs/xfs_error.c (revision bb29b957eaaa15be33243550d9ee576c2d601669)
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_platform.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_error.h"
14 #include "xfs_sysfs.h"
15 #include "xfs_inode.h"
16 
17 #ifdef DEBUG
18 
19 #define XFS_ERRTAG(_tag, _name, _default) \
20 	[XFS_ERRTAG_##_tag]	= (_default),
21 #include "xfs_errortag.h"
22 static const unsigned int xfs_errortag_random_default[] = { XFS_ERRTAGS };
23 #undef XFS_ERRTAG
24 
25 #define XFS_ERRTAG(_tag, _name, _default) \
26         [XFS_ERRTAG_##_tag]	=  __stringify(_name),
27 #include "xfs_errortag.h"
28 static const char *xfs_errortag_names[] = { XFS_ERRTAGS };
29 #undef XFS_ERRTAG
30 
31 struct xfs_errortag_attr {
32 	struct attribute	attr;
33 	unsigned int		tag;
34 };
35 
36 static inline struct xfs_errortag_attr *
37 to_attr(struct attribute *attr)
38 {
39 	return container_of(attr, struct xfs_errortag_attr, attr);
40 }
41 
42 static inline struct xfs_mount *
43 to_mp(struct kobject *kobject)
44 {
45 	struct xfs_kobj *kobj = to_kobj(kobject);
46 
47 	return container_of(kobj, struct xfs_mount, m_errortag_kobj);
48 }
49 
50 STATIC ssize_t
51 xfs_errortag_attr_store(
52 	struct kobject		*kobject,
53 	struct attribute	*attr,
54 	const char		*buf,
55 	size_t			count)
56 {
57 	struct xfs_mount	*mp = to_mp(kobject);
58 	unsigned int		error_tag = to_attr(attr)->tag;
59 	unsigned int		val;
60 	int			ret;
61 
62 	if (strcmp(buf, "default") == 0) {
63 		val = xfs_errortag_random_default[error_tag];
64 	} else {
65 		ret = kstrtouint(buf, 0, &val);
66 		if (ret)
67 			return ret;
68 	}
69 
70 	WRITE_ONCE(mp->m_errortag[error_tag], val);
71 	return count;
72 }
73 
74 STATIC ssize_t
75 xfs_errortag_attr_show(
76 	struct kobject		*kobject,
77 	struct attribute	*attr,
78 	char			*buf)
79 {
80 	struct xfs_mount	*mp = to_mp(kobject);
81 
82 	return snprintf(buf, PAGE_SIZE, "%u\n",
83 			READ_ONCE(mp->m_errortag[to_attr(attr)->tag]));
84 }
85 
86 static const struct sysfs_ops xfs_errortag_sysfs_ops = {
87 	.show = xfs_errortag_attr_show,
88 	.store = xfs_errortag_attr_store,
89 };
90 
91 #define XFS_ERRTAG(_tag, _name, _default)				\
92 static struct xfs_errortag_attr xfs_errortag_attr_##_name = {		\
93 	.attr = {.name = __stringify(_name),				\
94 		 .mode = VERIFY_OCTAL_PERMISSIONS(S_IWUSR | S_IRUGO) },	\
95 	.tag	= XFS_ERRTAG_##_tag,					\
96 };
97 #include "xfs_errortag.h"
98 XFS_ERRTAGS
99 #undef XFS_ERRTAG
100 
101 #define XFS_ERRTAG(_tag, _name, _default) \
102 	&xfs_errortag_attr_##_name.attr,
103 #include "xfs_errortag.h"
104 static struct attribute *xfs_errortag_attrs[] = {
105 	XFS_ERRTAGS
106 	NULL
107 };
108 ATTRIBUTE_GROUPS(xfs_errortag);
109 #undef XFS_ERRTAG
110 
111 /* -1 because XFS_ERRTAG_DROP_WRITES got removed, + 1 for NULL termination */
112 static_assert(ARRAY_SIZE(xfs_errortag_attrs) == XFS_ERRTAG_MAX);
113 
114 static const struct kobj_type xfs_errortag_ktype = {
115 	.release = xfs_sysfs_release,
116 	.sysfs_ops = &xfs_errortag_sysfs_ops,
117 	.default_groups = xfs_errortag_groups,
118 };
119 
120 int
121 xfs_errortag_init(
122 	struct xfs_mount	*mp)
123 {
124 	return xfs_sysfs_init(&mp->m_errortag_kobj, &xfs_errortag_ktype,
125 				&mp->m_kobj, "errortag");
126 }
127 
128 void
129 xfs_errortag_del(
130 	struct xfs_mount	*mp)
131 {
132 	xfs_sysfs_del(&mp->m_errortag_kobj);
133 }
134 
135 bool
136 xfs_errortag_test(
137 	struct xfs_mount	*mp,
138 	const char		*file,
139 	int			line,
140 	unsigned int		error_tag)
141 {
142 	unsigned int		randfactor;
143 
144 	randfactor = READ_ONCE(mp->m_errortag[error_tag]);
145 	if (!randfactor || get_random_u32_below(randfactor))
146 		return false;
147 
148 	xfs_warn_ratelimited(mp,
149 "Injecting error at file %s, line %d, on filesystem \"%s\"",
150 			file, line, mp->m_super->s_id);
151 	return true;
152 }
153 
154 void
155 xfs_errortag_delay(
156 	struct xfs_mount	*mp,
157 	const char		*file,
158 	int			line,
159 	unsigned int		error_tag)
160 {
161 	unsigned int		delay = READ_ONCE(mp->m_errortag[error_tag]);
162 
163 	might_sleep();
164 
165 	if (!delay)
166 		return;
167 
168 	xfs_warn_ratelimited(mp,
169 "Injecting %ums delay at file %s, line %d, on filesystem \"%s\"",
170 		delay, file, line,
171 		mp->m_super->s_id);
172 	mdelay(delay);
173 }
174 
175 int
176 xfs_errortag_add(
177 	struct xfs_mount	*mp,
178 	unsigned int		error_tag)
179 {
180 	BUILD_BUG_ON(ARRAY_SIZE(xfs_errortag_random_default) != XFS_ERRTAG_MAX);
181 
182 	if (error_tag >= XFS_ERRTAG_MAX)
183 		return -EINVAL;
184 
185 	/* Error out removed injection types */
186 	switch (error_tag) {
187 	case XFS_ERRTAG_DROP_WRITES:
188 		return -EINVAL;
189 	default:
190 		break;
191 	}
192 
193 	WRITE_ONCE(mp->m_errortag[error_tag],
194 		   xfs_errortag_random_default[error_tag]);
195 	return 0;
196 }
197 
198 int
199 xfs_errortag_add_name(
200 	struct xfs_mount	*mp,
201 	const char		*tag_name)
202 {
203 	unsigned int		i;
204 
205 	for (i = 0; i < XFS_ERRTAG_MAX; i++) {
206 		if (xfs_errortag_names[i] &&
207 		    !strcmp(xfs_errortag_names[i], tag_name))
208 			return xfs_errortag_add(mp, i);
209 	}
210 
211 	return -EINVAL;
212 }
213 
214 void
215 xfs_errortag_copy(
216 	struct xfs_mount	*dst_mp,
217 	struct xfs_mount	*src_mp)
218 {
219 	unsigned int		val, i;
220 
221 	for (i = 0; i < XFS_ERRTAG_MAX; i++) {
222 		val = READ_ONCE(src_mp->m_errortag[i]);
223 		if (val)
224 			WRITE_ONCE(dst_mp->m_errortag[i], val);
225 	}
226 }
227 
228 int
229 xfs_errortag_clearall(
230 	struct xfs_mount	*mp)
231 {
232 	unsigned int		i;
233 
234 	for (i = 0; i < XFS_ERRTAG_MAX; i++)
235 		WRITE_ONCE(mp->m_errortag[i], 0);
236 	return 0;
237 }
238 #endif /* DEBUG */
239 
240 void
241 xfs_error_report(
242 	const char		*tag,
243 	int			level,
244 	int			error,
245 	struct xfs_mount	*mp,
246 	const char		*filename,
247 	int			linenum,
248 	xfs_failaddr_t		failaddr)
249 {
250 	if (level <= xfs_error_level) {
251 		if (error)
252 			xfs_alert_tag(mp, XFS_PTAG_ERROR_REPORT,
253 "Internal error %s (%d) at line %d of file %s.  Caller %pS",
254 				tag, error, linenum, filename, failaddr);
255 		else
256 			xfs_alert_tag(mp, XFS_PTAG_ERROR_REPORT,
257 "Internal error %s at line %d of file %s.  Caller %pS",
258 				tag, linenum, filename, failaddr);
259 		xfs_stack_trace();
260 	}
261 }
262 
263 void
264 xfs_corruption_error(
265 	const char		*tag,
266 	int			level,
267 	struct xfs_mount	*mp,
268 	const void		*buf,
269 	size_t			bufsize,
270 	const char		*filename,
271 	int			linenum,
272 	xfs_failaddr_t		failaddr)
273 {
274 	if (buf && level <= xfs_error_level)
275 		xfs_hex_dump(buf, bufsize);
276 	xfs_error_report(tag, level, -EFSCORRUPTED, mp,
277 			 filename, linenum, failaddr);
278 	xfs_alert(mp, "Corruption detected. Unmount and run xfs_repair");
279 }
280 
281 /*
282  * Complain about the kinds of metadata corruption that we can't detect from a
283  * verifier, such as incorrect inter-block relationship data.  Does not set
284  * bp->b_error.
285  *
286  * Call xfs_buf_mark_corrupt, not this function.
287  */
288 void
289 xfs_buf_corruption_error(
290 	struct xfs_buf		*bp,
291 	xfs_failaddr_t		fa)
292 {
293 	struct xfs_mount	*mp = bp->b_mount;
294 
295 	xfs_alert_tag(mp, XFS_PTAG_VERIFIER_ERROR,
296 		  "Metadata corruption detected at %pS, %s block 0x%llx",
297 		  fa, bp->b_ops->name, xfs_buf_daddr(bp));
298 
299 	xfs_alert(mp, "Unmount and run xfs_repair");
300 
301 	if (xfs_error_level >= XFS_ERRLEVEL_HIGH)
302 		xfs_stack_trace();
303 }
304 
305 /*
306  * Warnings specifically for verifier errors.  Differentiate CRC vs. invalid
307  * values, and omit the stack trace unless the error level is tuned high.
308  */
309 void
310 xfs_buf_verifier_error(
311 	struct xfs_buf		*bp,
312 	int			error,
313 	const char		*name,
314 	const void		*buf,
315 	size_t			bufsz,
316 	xfs_failaddr_t		failaddr)
317 {
318 	struct xfs_mount	*mp = bp->b_mount;
319 	xfs_failaddr_t		fa;
320 	int			sz;
321 
322 	fa = failaddr ? failaddr : __return_address;
323 	__xfs_buf_ioerror(bp, error, fa);
324 
325 	xfs_alert_tag(mp, XFS_PTAG_VERIFIER_ERROR,
326 		  "Metadata %s detected at %pS, %s block 0x%llx %s",
327 		  bp->b_error == -EFSBADCRC ? "CRC error" : "corruption",
328 		  fa, bp->b_ops->name, xfs_buf_daddr(bp), name);
329 
330 	xfs_alert(mp, "Unmount and run xfs_repair");
331 
332 	if (xfs_error_level >= XFS_ERRLEVEL_LOW) {
333 		sz = min_t(size_t, XFS_CORRUPTION_DUMP_LEN, bufsz);
334 		xfs_alert(mp, "First %d bytes of corrupted metadata buffer:",
335 				sz);
336 		xfs_hex_dump(buf, sz);
337 	}
338 
339 	if (xfs_error_level >= XFS_ERRLEVEL_HIGH)
340 		xfs_stack_trace();
341 }
342 
343 /*
344  * Warnings specifically for verifier errors.  Differentiate CRC vs. invalid
345  * values, and omit the stack trace unless the error level is tuned high.
346  */
347 void
348 xfs_verifier_error(
349 	struct xfs_buf		*bp,
350 	int			error,
351 	xfs_failaddr_t		failaddr)
352 {
353 	return xfs_buf_verifier_error(bp, error, "", xfs_buf_offset(bp, 0),
354 			XFS_CORRUPTION_DUMP_LEN, failaddr);
355 }
356 
357 /*
358  * Warnings for inode corruption problems.  Don't bother with the stack
359  * trace unless the error level is turned up high.
360  */
361 void
362 xfs_inode_verifier_error(
363 	struct xfs_inode	*ip,
364 	int			error,
365 	const char		*name,
366 	const void		*buf,
367 	size_t			bufsz,
368 	xfs_failaddr_t		failaddr)
369 {
370 	struct xfs_mount	*mp = ip->i_mount;
371 	xfs_failaddr_t		fa;
372 	int			sz;
373 
374 	fa = failaddr ? failaddr : __return_address;
375 
376 	xfs_alert(mp, "Metadata %s detected at %pS, inode 0x%llx %s",
377 		  error == -EFSBADCRC ? "CRC error" : "corruption",
378 		  fa, I_INO(ip), name);
379 
380 	xfs_alert(mp, "Unmount and run xfs_repair");
381 
382 	if (buf && xfs_error_level >= XFS_ERRLEVEL_LOW) {
383 		sz = min_t(size_t, XFS_CORRUPTION_DUMP_LEN, bufsz);
384 		xfs_alert(mp, "First %d bytes of corrupted metadata buffer:",
385 				sz);
386 		xfs_hex_dump(buf, sz);
387 	}
388 
389 	if (xfs_error_level >= XFS_ERRLEVEL_HIGH)
390 		xfs_stack_trace();
391 }
392