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