blob: 52f75bc1abacbc7d5430c3dadf813c6083ddb823 [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"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110019#include "xfs_format.h"
Nathan Scotta844f452005-11-02 14:38:42 +110020#include "xfs_fs.h"
Dave Chinner239880e2013-10-23 10:50:10 +110021#include "xfs_log_format.h"
22#include "xfs_trans_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_error.h"
25
26#ifdef DEBUG
27
Darrick J. Wong31965ef2017-06-20 17:54:46 -070028static unsigned int xfs_errortag_random_default[] = {
29 XFS_RANDOM_DEFAULT,
30 XFS_RANDOM_IFLUSH_1,
31 XFS_RANDOM_IFLUSH_2,
32 XFS_RANDOM_IFLUSH_3,
33 XFS_RANDOM_IFLUSH_4,
34 XFS_RANDOM_IFLUSH_5,
35 XFS_RANDOM_IFLUSH_6,
36 XFS_RANDOM_DA_READ_BUF,
37 XFS_RANDOM_BTREE_CHECK_LBLOCK,
38 XFS_RANDOM_BTREE_CHECK_SBLOCK,
39 XFS_RANDOM_ALLOC_READ_AGF,
40 XFS_RANDOM_IALLOC_READ_AGI,
41 XFS_RANDOM_ITOBP_INOTOBP,
42 XFS_RANDOM_IUNLINK,
43 XFS_RANDOM_IUNLINK_REMOVE,
44 XFS_RANDOM_DIR_INO_VALIDATE,
45 XFS_RANDOM_BULKSTAT_READ_CHUNK,
46 XFS_RANDOM_IODONE_IOERR,
47 XFS_RANDOM_STRATREAD_IOERR,
48 XFS_RANDOM_STRATCMPL_IOERR,
49 XFS_RANDOM_DIOWRITE_IOERR,
50 XFS_RANDOM_BMAPIFORMAT,
51 XFS_RANDOM_FREE_EXTENT,
52 XFS_RANDOM_RMAP_FINISH_ONE,
53 XFS_RANDOM_REFCOUNT_CONTINUE_UPDATE,
54 XFS_RANDOM_REFCOUNT_FINISH_ONE,
55 XFS_RANDOM_BMAP_FINISH_ONE,
56 XFS_RANDOM_AG_RESV_CRITICAL,
57};
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Linus Torvalds1da177e2005-04-16 15:20:36 -070059int
Darrick J. Wong31965ef2017-06-20 17:54:46 -070060xfs_errortag_init(
61 struct xfs_mount *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
Darrick J. Wong31965ef2017-06-20 17:54:46 -070063 mp->m_errortag = kmem_zalloc(sizeof(unsigned int) * XFS_ERRTAG_MAX,
64 KM_SLEEP | KM_MAYFAIL);
65 if (!mp->m_errortag)
66 return -ENOMEM;
67 return 0;
68}
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Darrick J. Wong31965ef2017-06-20 17:54:46 -070070void
71xfs_errortag_del(
72 struct xfs_mount *mp)
73{
74 kmem_free(mp->m_errortag);
75}
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Darrick J. Wong31965ef2017-06-20 17:54:46 -070077bool
78xfs_errortag_test(
79 struct xfs_mount *mp,
80 const char *expression,
81 const char *file,
82 int line,
83 unsigned int error_tag)
84{
85 unsigned int randfactor;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Darrick J. Wong31965ef2017-06-20 17:54:46 -070087 ASSERT(error_tag < XFS_ERRTAG_MAX);
88 randfactor = mp->m_errortag[error_tag];
89 if (!randfactor || prandom_u32() % randfactor)
90 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Darrick J. Wong31965ef2017-06-20 17:54:46 -070092 xfs_warn_ratelimited(mp,
93"Injecting error (%s) at file %s, line %d, on filesystem \"%s\"",
94 expression, file, line, mp->m_fsname);
95 return true;
96}
97
98int
99xfs_errortag_set(
100 struct xfs_mount *mp,
101 unsigned int error_tag,
102 unsigned int tag_value)
103{
104 if (error_tag >= XFS_ERRTAG_MAX)
105 return -EINVAL;
106
107 mp->m_errortag[error_tag] = tag_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return 0;
109}
110
111int
Darrick J. Wong31965ef2017-06-20 17:54:46 -0700112xfs_errortag_add(
113 struct xfs_mount *mp,
114 unsigned int error_tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Darrick J. Wong128f24d2016-06-21 11:53:28 +1000116 if (error_tag >= XFS_ERRTAG_MAX)
117 return -EINVAL;
118
Darrick J. Wong31965ef2017-06-20 17:54:46 -0700119 return xfs_errortag_set(mp, error_tag,
120 xfs_errortag_random_default[error_tag]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
123int
Darrick J. Wong31965ef2017-06-20 17:54:46 -0700124xfs_errortag_clearall(
125 struct xfs_mount *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Darrick J. Wong31965ef2017-06-20 17:54:46 -0700127 memset(mp->m_errortag, 0, sizeof(unsigned int) * XFS_ERRTAG_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 return 0;
129}
Christoph Hellwig1550d0b2008-08-13 16:17:37 +1000130#endif /* DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132void
133xfs_error_report(
Alex Eldera0e856b2010-04-13 15:22:08 +1000134 const char *tag,
135 int level,
136 struct xfs_mount *mp,
137 const char *filename,
138 int linenum,
Christoph Hellwigfc51c2b2015-06-22 09:44:02 +1000139 void *ra)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
141 if (level <= xfs_error_level) {
Dave Chinner6a19d932011-03-07 10:02:35 +1100142 xfs_alert_tag(mp, XFS_PTAG_ERROR_REPORT,
Scott Wood65dd2972015-03-25 14:56:21 +1100143 "Internal error %s at line %d of file %s. Caller %pS",
Alex Eldera0e856b2010-04-13 15:22:08 +1000144 tag, linenum, filename, ra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 xfs_stack_trace();
147 }
148}
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150void
151xfs_corruption_error(
Alex Eldera0e856b2010-04-13 15:22:08 +1000152 const char *tag,
153 int level,
154 struct xfs_mount *mp,
155 void *p,
156 const char *filename,
157 int linenum,
Christoph Hellwigfc51c2b2015-06-22 09:44:02 +1000158 void *ra)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
160 if (level <= xfs_error_level)
Dave Chinnera2050642013-04-03 16:11:11 +1100161 xfs_hex_dump(p, 64);
Alex Eldera0e856b2010-04-13 15:22:08 +1000162 xfs_error_report(tag, level, mp, filename, linenum, ra);
Dave Chinner65333b42011-03-07 10:03:35 +1100163 xfs_alert(mp, "Corruption detected. Unmount and run xfs_repair");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
Eric Sandeenca23f8f2014-02-27 15:21:07 +1100165
166/*
167 * Warnings specifically for verifier errors. Differentiate CRC vs. invalid
168 * values, and omit the stack trace unless the error level is tuned high.
169 */
170void
171xfs_verifier_error(
172 struct xfs_buf *bp)
173{
174 struct xfs_mount *mp = bp->b_target->bt_mount;
175
Eric Sandeen233135b2016-01-04 16:10:19 +1100176 xfs_alert(mp, "Metadata %s detected at %pF, %s block 0x%llx",
Dave Chinner24513372014-06-25 14:58:08 +1000177 bp->b_error == -EFSBADCRC ? "CRC error" : "corruption",
Eric Sandeen233135b2016-01-04 16:10:19 +1100178 __return_address, bp->b_ops->name, bp->b_bn);
Eric Sandeenca23f8f2014-02-27 15:21:07 +1100179
180 xfs_alert(mp, "Unmount and run xfs_repair");
181
182 if (xfs_error_level >= XFS_ERRLEVEL_LOW) {
183 xfs_alert(mp, "First 64 bytes of corrupted metadata buffer:");
184 xfs_hex_dump(xfs_buf_offset(bp, 0), 64);
185 }
186
187 if (xfs_error_level >= XFS_ERRLEVEL_HIGH)
188 xfs_stack_trace();
189}