blob: 2f4feb959bfb46812678b65e262270b2fc55842a [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"
Darrick J. Wongc6840102017-06-20 17:54:47 -070025#include "xfs_sysfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#ifdef DEBUG
28
Darrick J. Wong31965ef2017-06-20 17:54:46 -070029static unsigned int xfs_errortag_random_default[] = {
30 XFS_RANDOM_DEFAULT,
31 XFS_RANDOM_IFLUSH_1,
32 XFS_RANDOM_IFLUSH_2,
33 XFS_RANDOM_IFLUSH_3,
34 XFS_RANDOM_IFLUSH_4,
35 XFS_RANDOM_IFLUSH_5,
36 XFS_RANDOM_IFLUSH_6,
37 XFS_RANDOM_DA_READ_BUF,
38 XFS_RANDOM_BTREE_CHECK_LBLOCK,
39 XFS_RANDOM_BTREE_CHECK_SBLOCK,
40 XFS_RANDOM_ALLOC_READ_AGF,
41 XFS_RANDOM_IALLOC_READ_AGI,
42 XFS_RANDOM_ITOBP_INOTOBP,
43 XFS_RANDOM_IUNLINK,
44 XFS_RANDOM_IUNLINK_REMOVE,
45 XFS_RANDOM_DIR_INO_VALIDATE,
46 XFS_RANDOM_BULKSTAT_READ_CHUNK,
47 XFS_RANDOM_IODONE_IOERR,
48 XFS_RANDOM_STRATREAD_IOERR,
49 XFS_RANDOM_STRATCMPL_IOERR,
50 XFS_RANDOM_DIOWRITE_IOERR,
51 XFS_RANDOM_BMAPIFORMAT,
52 XFS_RANDOM_FREE_EXTENT,
53 XFS_RANDOM_RMAP_FINISH_ONE,
54 XFS_RANDOM_REFCOUNT_CONTINUE_UPDATE,
55 XFS_RANDOM_REFCOUNT_FINISH_ONE,
56 XFS_RANDOM_BMAP_FINISH_ONE,
57 XFS_RANDOM_AG_RESV_CRITICAL,
Darrick J. Wongf8c47252017-06-20 17:54:48 -070058 XFS_RANDOM_DROP_WRITES,
Brian Foster3e88a002017-06-27 09:52:32 -070059 XFS_RANDOM_LOG_BAD_CRC,
Darrick J. Wong31965ef2017-06-20 17:54:46 -070060};
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Darrick J. Wongc6840102017-06-20 17:54:47 -070062struct xfs_errortag_attr {
63 struct attribute attr;
64 unsigned int tag;
65};
66
67static inline struct xfs_errortag_attr *
68to_attr(struct attribute *attr)
69{
70 return container_of(attr, struct xfs_errortag_attr, attr);
71}
72
73static inline struct xfs_mount *
74to_mp(struct kobject *kobject)
75{
76 struct xfs_kobj *kobj = to_kobj(kobject);
77
78 return container_of(kobj, struct xfs_mount, m_errortag_kobj);
79}
80
81STATIC ssize_t
82xfs_errortag_attr_store(
83 struct kobject *kobject,
84 struct attribute *attr,
85 const char *buf,
86 size_t count)
87{
88 struct xfs_mount *mp = to_mp(kobject);
89 struct xfs_errortag_attr *xfs_attr = to_attr(attr);
90 int ret;
91 unsigned int val;
92
93 if (strcmp(buf, "default") == 0) {
94 val = xfs_errortag_random_default[xfs_attr->tag];
95 } else {
96 ret = kstrtouint(buf, 0, &val);
97 if (ret)
98 return ret;
99 }
100
101 ret = xfs_errortag_set(mp, xfs_attr->tag, val);
102 if (ret)
103 return ret;
104 return count;
105}
106
107STATIC ssize_t
108xfs_errortag_attr_show(
109 struct kobject *kobject,
110 struct attribute *attr,
111 char *buf)
112{
113 struct xfs_mount *mp = to_mp(kobject);
114 struct xfs_errortag_attr *xfs_attr = to_attr(attr);
115
116 return snprintf(buf, PAGE_SIZE, "%u\n",
117 xfs_errortag_get(mp, xfs_attr->tag));
118}
119
120static const struct sysfs_ops xfs_errortag_sysfs_ops = {
121 .show = xfs_errortag_attr_show,
122 .store = xfs_errortag_attr_store,
123};
124
125#define XFS_ERRORTAG_ATTR_RW(_name, _tag) \
126static struct xfs_errortag_attr xfs_errortag_attr_##_name = { \
127 .attr = {.name = __stringify(_name), \
128 .mode = VERIFY_OCTAL_PERMISSIONS(S_IWUSR | S_IRUGO) }, \
129 .tag = (_tag), \
130}
131
132#define XFS_ERRORTAG_ATTR_LIST(_name) &xfs_errortag_attr_##_name.attr
133
134XFS_ERRORTAG_ATTR_RW(noerror, XFS_ERRTAG_NOERROR);
135XFS_ERRORTAG_ATTR_RW(iflush1, XFS_ERRTAG_IFLUSH_1);
136XFS_ERRORTAG_ATTR_RW(iflush2, XFS_ERRTAG_IFLUSH_2);
137XFS_ERRORTAG_ATTR_RW(iflush3, XFS_ERRTAG_IFLUSH_3);
138XFS_ERRORTAG_ATTR_RW(iflush4, XFS_ERRTAG_IFLUSH_4);
139XFS_ERRORTAG_ATTR_RW(iflush5, XFS_ERRTAG_IFLUSH_5);
140XFS_ERRORTAG_ATTR_RW(iflush6, XFS_ERRTAG_IFLUSH_6);
141XFS_ERRORTAG_ATTR_RW(dareadbuf, XFS_ERRTAG_DA_READ_BUF);
142XFS_ERRORTAG_ATTR_RW(btree_chk_lblk, XFS_ERRTAG_BTREE_CHECK_LBLOCK);
143XFS_ERRORTAG_ATTR_RW(btree_chk_sblk, XFS_ERRTAG_BTREE_CHECK_SBLOCK);
144XFS_ERRORTAG_ATTR_RW(readagf, XFS_ERRTAG_ALLOC_READ_AGF);
145XFS_ERRORTAG_ATTR_RW(readagi, XFS_ERRTAG_IALLOC_READ_AGI);
146XFS_ERRORTAG_ATTR_RW(itobp, XFS_ERRTAG_ITOBP_INOTOBP);
147XFS_ERRORTAG_ATTR_RW(iunlink, XFS_ERRTAG_IUNLINK);
148XFS_ERRORTAG_ATTR_RW(iunlinkrm, XFS_ERRTAG_IUNLINK_REMOVE);
149XFS_ERRORTAG_ATTR_RW(dirinovalid, XFS_ERRTAG_DIR_INO_VALIDATE);
150XFS_ERRORTAG_ATTR_RW(bulkstat, XFS_ERRTAG_BULKSTAT_READ_CHUNK);
151XFS_ERRORTAG_ATTR_RW(logiodone, XFS_ERRTAG_IODONE_IOERR);
152XFS_ERRORTAG_ATTR_RW(stratread, XFS_ERRTAG_STRATREAD_IOERR);
153XFS_ERRORTAG_ATTR_RW(stratcmpl, XFS_ERRTAG_STRATCMPL_IOERR);
154XFS_ERRORTAG_ATTR_RW(diowrite, XFS_ERRTAG_DIOWRITE_IOERR);
155XFS_ERRORTAG_ATTR_RW(bmapifmt, XFS_ERRTAG_BMAPIFORMAT);
156XFS_ERRORTAG_ATTR_RW(free_extent, XFS_ERRTAG_FREE_EXTENT);
157XFS_ERRORTAG_ATTR_RW(rmap_finish_one, XFS_ERRTAG_RMAP_FINISH_ONE);
158XFS_ERRORTAG_ATTR_RW(refcount_continue_update, XFS_ERRTAG_REFCOUNT_CONTINUE_UPDATE);
159XFS_ERRORTAG_ATTR_RW(refcount_finish_one, XFS_ERRTAG_REFCOUNT_FINISH_ONE);
160XFS_ERRORTAG_ATTR_RW(bmap_finish_one, XFS_ERRTAG_BMAP_FINISH_ONE);
161XFS_ERRORTAG_ATTR_RW(ag_resv_critical, XFS_ERRTAG_AG_RESV_CRITICAL);
Darrick J. Wongf8c47252017-06-20 17:54:48 -0700162XFS_ERRORTAG_ATTR_RW(drop_writes, XFS_ERRTAG_DROP_WRITES);
Brian Foster3e88a002017-06-27 09:52:32 -0700163XFS_ERRORTAG_ATTR_RW(log_bad_crc, XFS_ERRTAG_LOG_BAD_CRC);
Darrick J. Wongc6840102017-06-20 17:54:47 -0700164
165static struct attribute *xfs_errortag_attrs[] = {
166 XFS_ERRORTAG_ATTR_LIST(noerror),
167 XFS_ERRORTAG_ATTR_LIST(iflush1),
168 XFS_ERRORTAG_ATTR_LIST(iflush2),
169 XFS_ERRORTAG_ATTR_LIST(iflush3),
170 XFS_ERRORTAG_ATTR_LIST(iflush4),
171 XFS_ERRORTAG_ATTR_LIST(iflush5),
172 XFS_ERRORTAG_ATTR_LIST(iflush6),
173 XFS_ERRORTAG_ATTR_LIST(dareadbuf),
174 XFS_ERRORTAG_ATTR_LIST(btree_chk_lblk),
175 XFS_ERRORTAG_ATTR_LIST(btree_chk_sblk),
176 XFS_ERRORTAG_ATTR_LIST(readagf),
177 XFS_ERRORTAG_ATTR_LIST(readagi),
178 XFS_ERRORTAG_ATTR_LIST(itobp),
179 XFS_ERRORTAG_ATTR_LIST(iunlink),
180 XFS_ERRORTAG_ATTR_LIST(iunlinkrm),
181 XFS_ERRORTAG_ATTR_LIST(dirinovalid),
182 XFS_ERRORTAG_ATTR_LIST(bulkstat),
183 XFS_ERRORTAG_ATTR_LIST(logiodone),
184 XFS_ERRORTAG_ATTR_LIST(stratread),
185 XFS_ERRORTAG_ATTR_LIST(stratcmpl),
186 XFS_ERRORTAG_ATTR_LIST(diowrite),
187 XFS_ERRORTAG_ATTR_LIST(bmapifmt),
188 XFS_ERRORTAG_ATTR_LIST(free_extent),
189 XFS_ERRORTAG_ATTR_LIST(rmap_finish_one),
190 XFS_ERRORTAG_ATTR_LIST(refcount_continue_update),
191 XFS_ERRORTAG_ATTR_LIST(refcount_finish_one),
192 XFS_ERRORTAG_ATTR_LIST(bmap_finish_one),
193 XFS_ERRORTAG_ATTR_LIST(ag_resv_critical),
Darrick J. Wongf8c47252017-06-20 17:54:48 -0700194 XFS_ERRORTAG_ATTR_LIST(drop_writes),
Brian Foster3e88a002017-06-27 09:52:32 -0700195 XFS_ERRORTAG_ATTR_LIST(log_bad_crc),
Darrick J. Wongc6840102017-06-20 17:54:47 -0700196 NULL,
197};
198
199struct kobj_type xfs_errortag_ktype = {
200 .release = xfs_sysfs_release,
201 .sysfs_ops = &xfs_errortag_sysfs_ops,
202 .default_attrs = xfs_errortag_attrs,
203};
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205int
Darrick J. Wong31965ef2017-06-20 17:54:46 -0700206xfs_errortag_init(
207 struct xfs_mount *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
Darrick J. Wong31965ef2017-06-20 17:54:46 -0700209 mp->m_errortag = kmem_zalloc(sizeof(unsigned int) * XFS_ERRTAG_MAX,
210 KM_SLEEP | KM_MAYFAIL);
211 if (!mp->m_errortag)
212 return -ENOMEM;
Darrick J. Wongc6840102017-06-20 17:54:47 -0700213
214 return xfs_sysfs_init(&mp->m_errortag_kobj, &xfs_errortag_ktype,
215 &mp->m_kobj, "errortag");
Darrick J. Wong31965ef2017-06-20 17:54:46 -0700216}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Darrick J. Wong31965ef2017-06-20 17:54:46 -0700218void
219xfs_errortag_del(
220 struct xfs_mount *mp)
221{
Darrick J. Wongc6840102017-06-20 17:54:47 -0700222 xfs_sysfs_del(&mp->m_errortag_kobj);
Darrick J. Wong31965ef2017-06-20 17:54:46 -0700223 kmem_free(mp->m_errortag);
224}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Darrick J. Wong31965ef2017-06-20 17:54:46 -0700226bool
227xfs_errortag_test(
228 struct xfs_mount *mp,
229 const char *expression,
230 const char *file,
231 int line,
232 unsigned int error_tag)
233{
234 unsigned int randfactor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Carlos Maiolinod04c2412017-06-30 09:46:07 -0700236 /*
237 * To be able to use error injection anywhere, we need to ensure error
238 * injection mechanism is already initialized.
239 *
240 * Code paths like I/O completion can be called before the
241 * initialization is complete, but be able to inject errors in such
242 * places is still useful.
243 */
244 if (!mp->m_errortag)
245 return false;
246
Darrick J. Wong31965ef2017-06-20 17:54:46 -0700247 ASSERT(error_tag < XFS_ERRTAG_MAX);
248 randfactor = mp->m_errortag[error_tag];
249 if (!randfactor || prandom_u32() % randfactor)
250 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Darrick J. Wong31965ef2017-06-20 17:54:46 -0700252 xfs_warn_ratelimited(mp,
253"Injecting error (%s) at file %s, line %d, on filesystem \"%s\"",
254 expression, file, line, mp->m_fsname);
255 return true;
256}
257
258int
Darrick J. Wongc6840102017-06-20 17:54:47 -0700259xfs_errortag_get(
260 struct xfs_mount *mp,
261 unsigned int error_tag)
262{
263 if (error_tag >= XFS_ERRTAG_MAX)
264 return -EINVAL;
265
266 return mp->m_errortag[error_tag];
267}
268
269int
Darrick J. Wong31965ef2017-06-20 17:54:46 -0700270xfs_errortag_set(
271 struct xfs_mount *mp,
272 unsigned int error_tag,
273 unsigned int tag_value)
274{
275 if (error_tag >= XFS_ERRTAG_MAX)
276 return -EINVAL;
277
278 mp->m_errortag[error_tag] = tag_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 return 0;
280}
281
282int
Darrick J. Wong31965ef2017-06-20 17:54:46 -0700283xfs_errortag_add(
284 struct xfs_mount *mp,
285 unsigned int error_tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
Darrick J. Wong128f24d2016-06-21 11:53:28 +1000287 if (error_tag >= XFS_ERRTAG_MAX)
288 return -EINVAL;
289
Darrick J. Wong31965ef2017-06-20 17:54:46 -0700290 return xfs_errortag_set(mp, error_tag,
291 xfs_errortag_random_default[error_tag]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
293
294int
Darrick J. Wong31965ef2017-06-20 17:54:46 -0700295xfs_errortag_clearall(
296 struct xfs_mount *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
Darrick J. Wong31965ef2017-06-20 17:54:46 -0700298 memset(mp->m_errortag, 0, sizeof(unsigned int) * XFS_ERRTAG_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 return 0;
300}
Christoph Hellwig1550d0b2008-08-13 16:17:37 +1000301#endif /* DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303void
304xfs_error_report(
Alex Eldera0e856b2010-04-13 15:22:08 +1000305 const char *tag,
306 int level,
307 struct xfs_mount *mp,
308 const char *filename,
309 int linenum,
Christoph Hellwigfc51c2b2015-06-22 09:44:02 +1000310 void *ra)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
312 if (level <= xfs_error_level) {
Dave Chinner6a19d932011-03-07 10:02:35 +1100313 xfs_alert_tag(mp, XFS_PTAG_ERROR_REPORT,
Scott Wood65dd2972015-03-25 14:56:21 +1100314 "Internal error %s at line %d of file %s. Caller %pS",
Alex Eldera0e856b2010-04-13 15:22:08 +1000315 tag, linenum, filename, ra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 xfs_stack_trace();
318 }
319}
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321void
322xfs_corruption_error(
Alex Eldera0e856b2010-04-13 15:22:08 +1000323 const char *tag,
324 int level,
325 struct xfs_mount *mp,
326 void *p,
327 const char *filename,
328 int linenum,
Christoph Hellwigfc51c2b2015-06-22 09:44:02 +1000329 void *ra)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
331 if (level <= xfs_error_level)
Dave Chinnera2050642013-04-03 16:11:11 +1100332 xfs_hex_dump(p, 64);
Alex Eldera0e856b2010-04-13 15:22:08 +1000333 xfs_error_report(tag, level, mp, filename, linenum, ra);
Dave Chinner65333b42011-03-07 10:03:35 +1100334 xfs_alert(mp, "Corruption detected. Unmount and run xfs_repair");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
Eric Sandeenca23f8f2014-02-27 15:21:07 +1100336
337/*
338 * Warnings specifically for verifier errors. Differentiate CRC vs. invalid
339 * values, and omit the stack trace unless the error level is tuned high.
340 */
341void
342xfs_verifier_error(
343 struct xfs_buf *bp)
344{
345 struct xfs_mount *mp = bp->b_target->bt_mount;
346
Eric Sandeen233135b2016-01-04 16:10:19 +1100347 xfs_alert(mp, "Metadata %s detected at %pF, %s block 0x%llx",
Dave Chinner24513372014-06-25 14:58:08 +1000348 bp->b_error == -EFSBADCRC ? "CRC error" : "corruption",
Eric Sandeen233135b2016-01-04 16:10:19 +1100349 __return_address, bp->b_ops->name, bp->b_bn);
Eric Sandeenca23f8f2014-02-27 15:21:07 +1100350
351 xfs_alert(mp, "Unmount and run xfs_repair");
352
353 if (xfs_error_level >= XFS_ERRLEVEL_LOW) {
354 xfs_alert(mp, "First 64 bytes of corrupted metadata buffer:");
355 xfs_hex_dump(xfs_buf_offset(bp, 0), 64);
356 }
357
358 if (xfs_error_level >= XFS_ERRLEVEL_HIGH)
359 xfs_stack_trace();
360}