xfs_error.c 4.6 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2 3
 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
 * All Rights Reserved.
L
Linus Torvalds 已提交
4
 *
5 6
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
L
Linus Torvalds 已提交
7 8
 * published by the Free Software Foundation.
 *
9 10 11 12
 * This program is distributed in the hope that it would be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
L
Linus Torvalds 已提交
13
 *
14 15 16
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write the Free Software Foundation,
 * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
L
Linus Torvalds 已提交
17 18
 */
#include "xfs.h"
19
#include "xfs_format.h"
20
#include "xfs_fs.h"
21 22
#include "xfs_log_format.h"
#include "xfs_trans_resv.h"
23
#include "xfs_sb.h"
24
#include "xfs_ag.h"
L
Linus Torvalds 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#include "xfs_mount.h"
#include "xfs_error.h"

#ifdef DEBUG

int	xfs_etrap[XFS_ERROR_NTRAP] = {
	0,
};

int
xfs_error_trap(int e)
{
	int i;

	if (!e)
		return 0;
	for (i = 0; i < XFS_ERROR_NTRAP; i++) {
		if (xfs_etrap[i] == 0)
			break;
		if (e != xfs_etrap[i])
			continue;
46
		xfs_notice(NULL, "%s: error %d", __func__, e);
L
Linus Torvalds 已提交
47 48 49 50 51 52 53 54 55
		BUG();
		break;
	}
	return e;
}

int	xfs_etest[XFS_NUM_INJECT_ERROR];
int64_t	xfs_etest_fsid[XFS_NUM_INJECT_ERROR];
char *	xfs_etest_fsname[XFS_NUM_INJECT_ERROR];
56
int	xfs_error_test_active;
L
Linus Torvalds 已提交
57 58 59 60 61 62 63 64

int
xfs_error_test(int error_tag, int *fsidp, char *expression,
	       int line, char *file, unsigned long randfactor)
{
	int i;
	int64_t fsid;

65
	if (prandom_u32() % randfactor)
L
Linus Torvalds 已提交
66 67 68 69 70 71
		return 0;

	memcpy(&fsid, fsidp, sizeof(xfs_fsid_t));

	for (i = 0; i < XFS_NUM_INJECT_ERROR; i++)  {
		if (xfs_etest[i] == error_tag && xfs_etest_fsid[i] == fsid) {
72
			xfs_warn(NULL,
L
Linus Torvalds 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
	"Injecting error (%s) at file %s, line %d, on filesystem \"%s\"",
				expression, file, line, xfs_etest_fsname[i]);
			return 1;
		}
	}

	return 0;
}

int
xfs_errortag_add(int error_tag, xfs_mount_t *mp)
{
	int i;
	int len;
	int64_t fsid;

	memcpy(&fsid, mp->m_fixedfsid, sizeof(xfs_fsid_t));

	for (i = 0; i < XFS_NUM_INJECT_ERROR; i++)  {
		if (xfs_etest_fsid[i] == fsid && xfs_etest[i] == error_tag) {
93
			xfs_warn(mp, "error tag #%d on", error_tag);
L
Linus Torvalds 已提交
94 95 96 97 98 99
			return 0;
		}
	}

	for (i = 0; i < XFS_NUM_INJECT_ERROR; i++)  {
		if (xfs_etest[i] == 0) {
100
			xfs_warn(mp, "Turned on XFS error tag #%d",
L
Linus Torvalds 已提交
101 102 103 104 105 106
				error_tag);
			xfs_etest[i] = error_tag;
			xfs_etest_fsid[i] = fsid;
			len = strlen(mp->m_fsname);
			xfs_etest_fsname[i] = kmem_alloc(len + 1, KM_SLEEP);
			strcpy(xfs_etest_fsname[i], mp->m_fsname);
107
			xfs_error_test_active++;
L
Linus Torvalds 已提交
108 109 110 111
			return 0;
		}
	}

112
	xfs_warn(mp, "error tag overflow, too many turned on");
L
Linus Torvalds 已提交
113 114 115 116 117

	return 1;
}

int
118
xfs_errortag_clearall(xfs_mount_t *mp, int loud)
L
Linus Torvalds 已提交
119
{
120
	int64_t fsid;
L
Linus Torvalds 已提交
121
	int cleared = 0;
122 123 124 125
	int i;

	memcpy(&fsid, mp->m_fixedfsid, sizeof(xfs_fsid_t));

L
Linus Torvalds 已提交
126 127 128 129 130

	for (i = 0; i < XFS_NUM_INJECT_ERROR; i++) {
		if ((fsid == 0LL || xfs_etest_fsid[i] == fsid) &&
		     xfs_etest[i] != 0) {
			cleared = 1;
131
			xfs_warn(mp, "Clearing XFS error tag #%d",
L
Linus Torvalds 已提交
132 133 134
				xfs_etest[i]);
			xfs_etest[i] = 0;
			xfs_etest_fsid[i] = 0LL;
135
			kmem_free(xfs_etest_fsname[i]);
L
Linus Torvalds 已提交
136
			xfs_etest_fsname[i] = NULL;
137
			xfs_error_test_active--;
L
Linus Torvalds 已提交
138 139 140 141
		}
	}

	if (loud || cleared)
142
		xfs_warn(mp, "Cleared all XFS error tags for filesystem");
L
Linus Torvalds 已提交
143 144 145

	return 0;
}
C
Christoph Hellwig 已提交
146
#endif /* DEBUG */
L
Linus Torvalds 已提交
147 148 149

void
xfs_error_report(
150 151 152 153 154 155
	const char		*tag,
	int			level,
	struct xfs_mount	*mp,
	const char		*filename,
	int			linenum,
	inst_t			*ra)
L
Linus Torvalds 已提交
156 157
{
	if (level <= xfs_error_level) {
158
		xfs_alert_tag(mp, XFS_PTAG_ERROR_REPORT,
159
		"Internal error %s at line %d of file %s.  Caller 0x%p",
160
			    tag, linenum, filename, ra);
L
Linus Torvalds 已提交
161 162 163 164 165 166 167

		xfs_stack_trace();
	}
}

void
xfs_corruption_error(
168 169 170 171 172 173 174
	const char		*tag,
	int			level,
	struct xfs_mount	*mp,
	void			*p,
	const char		*filename,
	int			linenum,
	inst_t			*ra)
L
Linus Torvalds 已提交
175 176
{
	if (level <= xfs_error_level)
177
		xfs_hex_dump(p, 64);
178
	xfs_error_report(tag, level, mp, filename, linenum, ra);
179
	xfs_alert(mp, "Corruption detected. Unmount and run xfs_repair");
L
Linus Torvalds 已提交
180
}
E
Eric Sandeen 已提交
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205

/*
 * Warnings specifically for verifier errors.  Differentiate CRC vs. invalid
 * values, and omit the stack trace unless the error level is tuned high.
 */
void
xfs_verifier_error(
	struct xfs_buf		*bp)
{
	struct xfs_mount *mp = bp->b_target->bt_mount;

	xfs_alert(mp, "Metadata %s detected at %pF, block 0x%llx",
		  bp->b_error == EFSBADCRC ? "CRC error" : "corruption",
		  __return_address, bp->b_bn);

	xfs_alert(mp, "Unmount and run xfs_repair");

	if (xfs_error_level >= XFS_ERRLEVEL_LOW) {
		xfs_alert(mp, "First 64 bytes of corrupted metadata buffer:");
		xfs_hex_dump(xfs_buf_offset(bp, 0), 64);
	}

	if (xfs_error_level >= XFS_ERRLEVEL_HIGH)
		xfs_stack_trace();
}