xref: /linux/fs/xfs/xfs_error.c (revision 69050f8d6d075dc01af7a5f2f550a8067510366f)
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 	struct xfs_mount	*mp,
245 	const char		*filename,
246 	int			linenum,
247 	xfs_failaddr_t		failaddr)
248 {
249 	if (level <= xfs_error_level) {
250 		xfs_alert_tag(mp, XFS_PTAG_ERROR_REPORT,
251 		"Internal error %s at line %d of file %s.  Caller %pS",
252 			    tag, linenum, filename, failaddr);
253 
254 		xfs_stack_trace();
255 	}
256 }
257 
258 void
259 xfs_corruption_error(
260 	const char		*tag,
261 	int			level,
262 	struct xfs_mount	*mp,
263 	const void		*buf,
264 	size_t			bufsize,
265 	const char		*filename,
266 	int			linenum,
267 	xfs_failaddr_t		failaddr)
268 {
269 	if (buf && level <= xfs_error_level)
270 		xfs_hex_dump(buf, bufsize);
271 	xfs_error_report(tag, level, mp, filename, linenum, failaddr);
272 	xfs_alert(mp, "Corruption detected. Unmount and run xfs_repair");
273 }
274 
275 /*
276  * Complain about the kinds of metadata corruption that we can't detect from a
277  * verifier, such as incorrect inter-block relationship data.  Does not set
278  * bp->b_error.
279  *
280  * Call xfs_buf_mark_corrupt, not this function.
281  */
282 void
283 xfs_buf_corruption_error(
284 	struct xfs_buf		*bp,
285 	xfs_failaddr_t		fa)
286 {
287 	struct xfs_mount	*mp = bp->b_mount;
288 
289 	xfs_alert_tag(mp, XFS_PTAG_VERIFIER_ERROR,
290 		  "Metadata corruption detected at %pS, %s block 0x%llx",
291 		  fa, bp->b_ops->name, xfs_buf_daddr(bp));
292 
293 	xfs_alert(mp, "Unmount and run xfs_repair");
294 
295 	if (xfs_error_level >= XFS_ERRLEVEL_HIGH)
296 		xfs_stack_trace();
297 }
298 
299 /*
300  * Warnings specifically for verifier errors.  Differentiate CRC vs. invalid
301  * values, and omit the stack trace unless the error level is tuned high.
302  */
303 void
304 xfs_buf_verifier_error(
305 	struct xfs_buf		*bp,
306 	int			error,
307 	const char		*name,
308 	const void		*buf,
309 	size_t			bufsz,
310 	xfs_failaddr_t		failaddr)
311 {
312 	struct xfs_mount	*mp = bp->b_mount;
313 	xfs_failaddr_t		fa;
314 	int			sz;
315 
316 	fa = failaddr ? failaddr : __return_address;
317 	__xfs_buf_ioerror(bp, error, fa);
318 
319 	xfs_alert_tag(mp, XFS_PTAG_VERIFIER_ERROR,
320 		  "Metadata %s detected at %pS, %s block 0x%llx %s",
321 		  bp->b_error == -EFSBADCRC ? "CRC error" : "corruption",
322 		  fa, bp->b_ops->name, xfs_buf_daddr(bp), name);
323 
324 	xfs_alert(mp, "Unmount and run xfs_repair");
325 
326 	if (xfs_error_level >= XFS_ERRLEVEL_LOW) {
327 		sz = min_t(size_t, XFS_CORRUPTION_DUMP_LEN, bufsz);
328 		xfs_alert(mp, "First %d bytes of corrupted metadata buffer:",
329 				sz);
330 		xfs_hex_dump(buf, sz);
331 	}
332 
333 	if (xfs_error_level >= XFS_ERRLEVEL_HIGH)
334 		xfs_stack_trace();
335 }
336 
337 /*
338  * Warnings specifically for verifier errors.  Differentiate CRC vs. invalid
339  * values, and omit the stack trace unless the error level is tuned high.
340  */
341 void
342 xfs_verifier_error(
343 	struct xfs_buf		*bp,
344 	int			error,
345 	xfs_failaddr_t		failaddr)
346 {
347 	return xfs_buf_verifier_error(bp, error, "", xfs_buf_offset(bp, 0),
348 			XFS_CORRUPTION_DUMP_LEN, failaddr);
349 }
350 
351 /*
352  * Warnings for inode corruption problems.  Don't bother with the stack
353  * trace unless the error level is turned up high.
354  */
355 void
356 xfs_inode_verifier_error(
357 	struct xfs_inode	*ip,
358 	int			error,
359 	const char		*name,
360 	const void		*buf,
361 	size_t			bufsz,
362 	xfs_failaddr_t		failaddr)
363 {
364 	struct xfs_mount	*mp = ip->i_mount;
365 	xfs_failaddr_t		fa;
366 	int			sz;
367 
368 	fa = failaddr ? failaddr : __return_address;
369 
370 	xfs_alert(mp, "Metadata %s detected at %pS, inode 0x%llx %s",
371 		  error == -EFSBADCRC ? "CRC error" : "corruption",
372 		  fa, ip->i_ino, name);
373 
374 	xfs_alert(mp, "Unmount and run xfs_repair");
375 
376 	if (buf && xfs_error_level >= XFS_ERRLEVEL_LOW) {
377 		sz = min_t(size_t, XFS_CORRUPTION_DUMP_LEN, bufsz);
378 		xfs_alert(mp, "First %d bytes of corrupted metadata buffer:",
379 				sz);
380 		xfs_hex_dump(buf, sz);
381 	}
382 
383 	if (xfs_error_level >= XFS_ERRLEVEL_HIGH)
384 		xfs_stack_trace();
385 }
386