blob: d6c188cc7d43131ffcbdac9f185ba8d6bcba099f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Dave Chinner239880e2013-10-23 10:50:10 +110020#include "xfs_log_format.h"
21#include "xfs_trans_resv.h"
Nathan Scotta844f452005-11-02 14:38:42 +110022#include "xfs_sb.h"
David Chinnerda353b02007-08-28 14:00:13 +100023#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_mount.h"
25#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_dinode.h"
27#include "xfs_inode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_error.h"
29
30#ifdef DEBUG
31
32int xfs_etrap[XFS_ERROR_NTRAP] = {
33 0,
34};
35
36int
37xfs_error_trap(int e)
38{
39 int i;
40
41 if (!e)
42 return 0;
43 for (i = 0; i < XFS_ERROR_NTRAP; i++) {
44 if (xfs_etrap[i] == 0)
45 break;
46 if (e != xfs_etrap[i])
47 continue;
Dave Chinner0b932cc2011-03-07 10:08:35 +110048 xfs_notice(NULL, "%s: error %d", __func__, e);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 BUG();
50 break;
51 }
52 return e;
53}
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55int xfs_etest[XFS_NUM_INJECT_ERROR];
56int64_t xfs_etest_fsid[XFS_NUM_INJECT_ERROR];
57char * xfs_etest_fsname[XFS_NUM_INJECT_ERROR];
Dave Chinnerc76febe2010-11-30 15:15:31 +110058int xfs_error_test_active;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060int
61xfs_error_test(int error_tag, int *fsidp, char *expression,
62 int line, char *file, unsigned long randfactor)
63{
64 int i;
65 int64_t fsid;
66
Akinobu Mitaecb34032013-03-04 21:58:20 +090067 if (prandom_u32() % randfactor)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 return 0;
69
70 memcpy(&fsid, fsidp, sizeof(xfs_fsid_t));
71
72 for (i = 0; i < XFS_NUM_INJECT_ERROR; i++) {
73 if (xfs_etest[i] == error_tag && xfs_etest_fsid[i] == fsid) {
Dave Chinner0b932cc2011-03-07 10:08:35 +110074 xfs_warn(NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 "Injecting error (%s) at file %s, line %d, on filesystem \"%s\"",
76 expression, file, line, xfs_etest_fsname[i]);
77 return 1;
78 }
79 }
80
81 return 0;
82}
83
84int
85xfs_errortag_add(int error_tag, xfs_mount_t *mp)
86{
87 int i;
88 int len;
89 int64_t fsid;
90
91 memcpy(&fsid, mp->m_fixedfsid, sizeof(xfs_fsid_t));
92
93 for (i = 0; i < XFS_NUM_INJECT_ERROR; i++) {
94 if (xfs_etest_fsid[i] == fsid && xfs_etest[i] == error_tag) {
Dave Chinner0b932cc2011-03-07 10:08:35 +110095 xfs_warn(mp, "error tag #%d on", error_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 return 0;
97 }
98 }
99
100 for (i = 0; i < XFS_NUM_INJECT_ERROR; i++) {
101 if (xfs_etest[i] == 0) {
Dave Chinner0b932cc2011-03-07 10:08:35 +1100102 xfs_warn(mp, "Turned on XFS error tag #%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 error_tag);
104 xfs_etest[i] = error_tag;
105 xfs_etest_fsid[i] = fsid;
106 len = strlen(mp->m_fsname);
107 xfs_etest_fsname[i] = kmem_alloc(len + 1, KM_SLEEP);
108 strcpy(xfs_etest_fsname[i], mp->m_fsname);
Dave Chinnerc76febe2010-11-30 15:15:31 +1100109 xfs_error_test_active++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 return 0;
111 }
112 }
113
Dave Chinner0b932cc2011-03-07 10:08:35 +1100114 xfs_warn(mp, "error tag overflow, too many turned on");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116 return 1;
117}
118
119int
Christoph Hellwig0ce4cfd2007-08-30 17:20:53 +1000120xfs_errortag_clearall(xfs_mount_t *mp, int loud)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Christoph Hellwig0ce4cfd2007-08-30 17:20:53 +1000122 int64_t fsid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 int cleared = 0;
Christoph Hellwig0ce4cfd2007-08-30 17:20:53 +1000124 int i;
125
126 memcpy(&fsid, mp->m_fixedfsid, sizeof(xfs_fsid_t));
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129 for (i = 0; i < XFS_NUM_INJECT_ERROR; i++) {
130 if ((fsid == 0LL || xfs_etest_fsid[i] == fsid) &&
131 xfs_etest[i] != 0) {
132 cleared = 1;
Dave Chinner0b932cc2011-03-07 10:08:35 +1100133 xfs_warn(mp, "Clearing XFS error tag #%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 xfs_etest[i]);
135 xfs_etest[i] = 0;
136 xfs_etest_fsid[i] = 0LL;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000137 kmem_free(xfs_etest_fsname[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 xfs_etest_fsname[i] = NULL;
Dave Chinnerc76febe2010-11-30 15:15:31 +1100139 xfs_error_test_active--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 }
141 }
142
143 if (loud || cleared)
Dave Chinner0b932cc2011-03-07 10:08:35 +1100144 xfs_warn(mp, "Cleared all XFS error tags for filesystem");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 return 0;
147}
Christoph Hellwig1550d0b2008-08-13 16:17:37 +1000148#endif /* DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150void
151xfs_error_report(
Alex Eldera0e856b2010-04-13 15:22:08 +1000152 const char *tag,
153 int level,
154 struct xfs_mount *mp,
155 const char *filename,
156 int linenum,
157 inst_t *ra)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
159 if (level <= xfs_error_level) {
Dave Chinner6a19d932011-03-07 10:02:35 +1100160 xfs_alert_tag(mp, XFS_PTAG_ERROR_REPORT,
Eric Sandeen08e96e12013-10-11 20:59:05 -0500161 "Internal error %s at line %d of file %s. Caller 0x%p",
Alex Eldera0e856b2010-04-13 15:22:08 +1000162 tag, linenum, filename, ra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 xfs_stack_trace();
165 }
166}
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168void
169xfs_corruption_error(
Alex Eldera0e856b2010-04-13 15:22:08 +1000170 const char *tag,
171 int level,
172 struct xfs_mount *mp,
173 void *p,
174 const char *filename,
175 int linenum,
176 inst_t *ra)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
178 if (level <= xfs_error_level)
Dave Chinnera2050642013-04-03 16:11:11 +1100179 xfs_hex_dump(p, 64);
Alex Eldera0e856b2010-04-13 15:22:08 +1000180 xfs_error_report(tag, level, mp, filename, linenum, ra);
Dave Chinner65333b42011-03-07 10:03:35 +1100181 xfs_alert(mp, "Corruption detected. Unmount and run xfs_repair");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}