Linux Audio

Check our new training course

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