1/*
2 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18#include "xfs.h"
19#include "xfs_fs.h"
20#include "xfs_types.h"
21#include "xfs_log.h"
22#include "xfs_inum.h"
23#include "xfs_trans.h"
24#include "xfs_sb.h"
25#include "xfs_dir2.h"
26#include "xfs_dmapi.h"
27#include "xfs_mount.h"
28#include "xfs_bmap_btree.h"
29#include "xfs_dir2_sf.h"
30#include "xfs_attr_sf.h"
31#include "xfs_dinode.h"
32#include "xfs_inode.h"
33#include "xfs_utils.h"
34#include "xfs_error.h"
35
36#ifdef DEBUG
37
38int	xfs_etrap[XFS_ERROR_NTRAP] = {
39	0,
40};
41
42int
43xfs_error_trap(int e)
44{
45	int i;
46
47	if (!e)
48		return 0;
49	for (i = 0; i < XFS_ERROR_NTRAP; i++) {
50		if (xfs_etrap[i] == 0)
51			break;
52		if (e != xfs_etrap[i])
53			continue;
54		cmn_err(CE_NOTE, "xfs_error_trap: error %d", e);
55		BUG();
56		break;
57	}
58	return e;
59}
60#endif
61
62#if (defined(DEBUG) || defined(INDUCE_IO_ERROR))
63
64int	xfs_etest[XFS_NUM_INJECT_ERROR];
65int64_t	xfs_etest_fsid[XFS_NUM_INJECT_ERROR];
66char *	xfs_etest_fsname[XFS_NUM_INJECT_ERROR];
67
68void
69xfs_error_test_init(void)
70{
71	memset(xfs_etest, 0, sizeof(xfs_etest));
72	memset(xfs_etest_fsid, 0, sizeof(xfs_etest_fsid));
73	memset(xfs_etest_fsname, 0, sizeof(xfs_etest_fsname));
74}
75
76int
77xfs_error_test(int error_tag, int *fsidp, char *expression,
78	       int line, char *file, unsigned long randfactor)
79{
80	int i;
81	int64_t fsid;
82
83	if (random32() % randfactor)
84		return 0;
85
86	memcpy(&fsid, fsidp, sizeof(xfs_fsid_t));
87
88	for (i = 0; i < XFS_NUM_INJECT_ERROR; i++)  {
89		if (xfs_etest[i] == error_tag && xfs_etest_fsid[i] == fsid) {
90			cmn_err(CE_WARN,
91	"Injecting error (%s) at file %s, line %d, on filesystem \"%s\"",
92				expression, file, line, xfs_etest_fsname[i]);
93			return 1;
94		}
95	}
96
97	return 0;
98}
99
100int
101xfs_errortag_add(int error_tag, xfs_mount_t *mp)
102{
103	int i;
104	int len;
105	int64_t fsid;
106
107	memcpy(&fsid, mp->m_fixedfsid, sizeof(xfs_fsid_t));
108
109	for (i = 0; i < XFS_NUM_INJECT_ERROR; i++)  {
110		if (xfs_etest_fsid[i] == fsid && xfs_etest[i] == error_tag) {
111			cmn_err(CE_WARN, "XFS error tag #%d on", error_tag);
112			return 0;
113		}
114	}
115
116	for (i = 0; i < XFS_NUM_INJECT_ERROR; i++)  {
117		if (xfs_etest[i] == 0) {
118			cmn_err(CE_WARN, "Turned on XFS error tag #%d",
119				error_tag);
120			xfs_etest[i] = error_tag;
121			xfs_etest_fsid[i] = fsid;
122			len = strlen(mp->m_fsname);
123			xfs_etest_fsname[i] = kmem_alloc(len + 1, KM_SLEEP);
124			strcpy(xfs_etest_fsname[i], mp->m_fsname);
125			return 0;
126		}
127	}
128
129	cmn_err(CE_WARN, "error tag overflow, too many turned on");
130
131	return 1;
132}
133
134int
135xfs_errortag_clearall_umount(int64_t fsid, char *fsname, int loud)
136{
137	int i;
138	int cleared = 0;
139
140	for (i = 0; i < XFS_NUM_INJECT_ERROR; i++) {
141		if ((fsid == 0LL || xfs_etest_fsid[i] == fsid) &&
142		     xfs_etest[i] != 0) {
143			cleared = 1;
144			cmn_err(CE_WARN, "Clearing XFS error tag #%d",
145				xfs_etest[i]);
146			xfs_etest[i] = 0;
147			xfs_etest_fsid[i] = 0LL;
148			kmem_free(xfs_etest_fsname[i],
149				  strlen(xfs_etest_fsname[i]) + 1);
150			xfs_etest_fsname[i] = NULL;
151		}
152	}
153
154	if (loud || cleared)
155		cmn_err(CE_WARN,
156			"Cleared all XFS error tags for filesystem \"%s\"",
157			fsname);
158
159	return 0;
160}
161
162int
163xfs_errortag_clearall(xfs_mount_t *mp)
164{
165	int64_t fsid;
166
167	memcpy(&fsid, mp->m_fixedfsid, sizeof(xfs_fsid_t));
168
169	return xfs_errortag_clearall_umount(fsid, mp->m_fsname, 1);
170}
171#endif /* DEBUG || INDUCE_IO_ERROR */
172
173static void
174xfs_fs_vcmn_err(int level, xfs_mount_t *mp, char *fmt, va_list ap)
175{
176	if (mp != NULL) {
177		char	*newfmt;
178		int	len = 16 + mp->m_fsname_len + strlen(fmt);
179
180		newfmt = kmem_alloc(len, KM_SLEEP);
181		sprintf(newfmt, "Filesystem \"%s\": %s", mp->m_fsname, fmt);
182		icmn_err(level, newfmt, ap);
183		kmem_free(newfmt, len);
184	} else {
185		icmn_err(level, fmt, ap);
186	}
187}
188
189void
190xfs_fs_cmn_err(int level, xfs_mount_t *mp, char *fmt, ...)
191{
192	va_list ap;
193
194	va_start(ap, fmt);
195	xfs_fs_vcmn_err(level, mp, fmt, ap);
196	va_end(ap);
197}
198
199void
200xfs_cmn_err(int panic_tag, int level, xfs_mount_t *mp, char *fmt, ...)
201{
202	va_list ap;
203
204#ifdef DEBUG
205	xfs_panic_mask |= XFS_PTAG_SHUTDOWN_CORRUPT;
206#endif
207
208	if (xfs_panic_mask && (xfs_panic_mask & panic_tag)
209	    && (level & CE_ALERT)) {
210		level &= ~CE_ALERT;
211		level |= CE_PANIC;
212		cmn_err(CE_ALERT, "XFS: Transforming an alert into a BUG.");
213	}
214	va_start(ap, fmt);
215	xfs_fs_vcmn_err(level, mp, fmt, ap);
216	va_end(ap);
217}
218
219void
220xfs_error_report(
221	char		*tag,
222	int		level,
223	xfs_mount_t	*mp,
224	char		*fname,
225	int		linenum,
226	inst_t		*ra)
227{
228	if (level <= xfs_error_level) {
229		xfs_cmn_err(XFS_PTAG_ERROR_REPORT,
230			    CE_ALERT, mp,
231		"XFS internal error %s at line %d of file %s.  Caller 0x%p\n",
232			    tag, linenum, fname, ra);
233
234		xfs_stack_trace();
235	}
236}
237
238STATIC void
239xfs_hex_dump(void *p, int length)
240{
241	__uint8_t *uip = (__uint8_t*)p;
242	int	i;
243	char	sbuf[128], *s;
244
245	s = sbuf;
246	*s = '\0';
247	for (i=0; i<length; i++, uip++) {
248		if ((i % 16) == 0) {
249			if (*s != '\0')
250				cmn_err(CE_ALERT, "%s\n", sbuf);
251			s = sbuf;
252			sprintf(s, "0x%x: ", i);
253			while( *s != '\0')
254				s++;
255		}
256		sprintf(s, "%02x ", *uip);
257
258		/*
259		 * the kernel sprintf is a void; user sprintf returns
260		 * the sprintf'ed string's length.  Find the new end-
261		 * of-string
262		 */
263		while( *s != '\0')
264			s++;
265	}
266	cmn_err(CE_ALERT, "%s\n", sbuf);
267}
268
269void
270xfs_corruption_error(
271	char		*tag,
272	int		level,
273	xfs_mount_t	*mp,
274	void		*p,
275	char		*fname,
276	int		linenum,
277	inst_t		*ra)
278{
279	if (level <= xfs_error_level)
280		xfs_hex_dump(p, 16);
281	xfs_error_report(tag, level, mp, fname, linenum, ra);
282}
283