1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
7 #include "xfs_shared.h"
8 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
12 #include "xfs_mount.h"
13 #include "xfs_errortag.h"
14 #include "xfs_error.h"
15 #include "xfs_sysfs.h"
16 #include "xfs_inode.h"
20 static unsigned int xfs_errortag_random_default[] = {
28 XFS_RANDOM_DA_READ_BUF,
29 XFS_RANDOM_BTREE_CHECK_LBLOCK,
30 XFS_RANDOM_BTREE_CHECK_SBLOCK,
31 XFS_RANDOM_ALLOC_READ_AGF,
32 XFS_RANDOM_IALLOC_READ_AGI,
33 XFS_RANDOM_ITOBP_INOTOBP,
35 XFS_RANDOM_IUNLINK_REMOVE,
36 XFS_RANDOM_DIR_INO_VALIDATE,
37 XFS_RANDOM_BULKSTAT_READ_CHUNK,
38 XFS_RANDOM_IODONE_IOERR,
39 XFS_RANDOM_STRATREAD_IOERR,
40 XFS_RANDOM_STRATCMPL_IOERR,
41 XFS_RANDOM_DIOWRITE_IOERR,
42 XFS_RANDOM_BMAPIFORMAT,
43 XFS_RANDOM_FREE_EXTENT,
44 XFS_RANDOM_RMAP_FINISH_ONE,
45 XFS_RANDOM_REFCOUNT_CONTINUE_UPDATE,
46 XFS_RANDOM_REFCOUNT_FINISH_ONE,
47 XFS_RANDOM_BMAP_FINISH_ONE,
48 XFS_RANDOM_AG_RESV_CRITICAL,
49 XFS_RANDOM_DROP_WRITES,
50 XFS_RANDOM_LOG_BAD_CRC,
51 XFS_RANDOM_LOG_ITEM_PIN,
52 XFS_RANDOM_BUF_LRU_REF,
53 XFS_RANDOM_FORCE_SCRUB_REPAIR,
54 XFS_RANDOM_FORCE_SUMMARY_RECALC,
55 XFS_RANDOM_IUNLINK_FALLBACK,
56 XFS_RANDOM_BUF_IOERROR,
57 XFS_RANDOM_REDUCE_MAX_IEXTENTS,
58 XFS_RANDOM_BMAP_ALLOC_MINLEN_EXTENT,
59 XFS_RANDOM_AG_RESV_FAIL,
62 struct xfs_errortag_attr {
63 struct attribute attr;
67 static inline struct xfs_errortag_attr *
68 to_attr(struct attribute *attr)
70 return container_of(attr, struct xfs_errortag_attr, attr);
73 static inline struct xfs_mount *
74 to_mp(struct kobject *kobject)
76 struct xfs_kobj *kobj = to_kobj(kobject);
78 return container_of(kobj, struct xfs_mount, m_errortag_kobj);
82 xfs_errortag_attr_store(
83 struct kobject *kobject,
84 struct attribute *attr,
88 struct xfs_mount *mp = to_mp(kobject);
89 struct xfs_errortag_attr *xfs_attr = to_attr(attr);
93 if (strcmp(buf, "default") == 0) {
94 val = xfs_errortag_random_default[xfs_attr->tag];
96 ret = kstrtouint(buf, 0, &val);
101 ret = xfs_errortag_set(mp, xfs_attr->tag, val);
108 xfs_errortag_attr_show(
109 struct kobject *kobject,
110 struct attribute *attr,
113 struct xfs_mount *mp = to_mp(kobject);
114 struct xfs_errortag_attr *xfs_attr = to_attr(attr);
116 return snprintf(buf, PAGE_SIZE, "%u\n",
117 xfs_errortag_get(mp, xfs_attr->tag));
120 static const struct sysfs_ops xfs_errortag_sysfs_ops = {
121 .show = xfs_errortag_attr_show,
122 .store = xfs_errortag_attr_store,
125 #define XFS_ERRORTAG_ATTR_RW(_name, _tag) \
126 static struct xfs_errortag_attr xfs_errortag_attr_##_name = { \
127 .attr = {.name = __stringify(_name), \
128 .mode = VERIFY_OCTAL_PERMISSIONS(S_IWUSR | S_IRUGO) }, \
132 #define XFS_ERRORTAG_ATTR_LIST(_name) &xfs_errortag_attr_##_name.attr
134 XFS_ERRORTAG_ATTR_RW(noerror, XFS_ERRTAG_NOERROR);
135 XFS_ERRORTAG_ATTR_RW(iflush1, XFS_ERRTAG_IFLUSH_1);
136 XFS_ERRORTAG_ATTR_RW(iflush2, XFS_ERRTAG_IFLUSH_2);
137 XFS_ERRORTAG_ATTR_RW(iflush3, XFS_ERRTAG_IFLUSH_3);
138 XFS_ERRORTAG_ATTR_RW(iflush4, XFS_ERRTAG_IFLUSH_4);
139 XFS_ERRORTAG_ATTR_RW(iflush5, XFS_ERRTAG_IFLUSH_5);
140 XFS_ERRORTAG_ATTR_RW(iflush6, XFS_ERRTAG_IFLUSH_6);
141 XFS_ERRORTAG_ATTR_RW(dareadbuf, XFS_ERRTAG_DA_READ_BUF);
142 XFS_ERRORTAG_ATTR_RW(btree_chk_lblk, XFS_ERRTAG_BTREE_CHECK_LBLOCK);
143 XFS_ERRORTAG_ATTR_RW(btree_chk_sblk, XFS_ERRTAG_BTREE_CHECK_SBLOCK);
144 XFS_ERRORTAG_ATTR_RW(readagf, XFS_ERRTAG_ALLOC_READ_AGF);
145 XFS_ERRORTAG_ATTR_RW(readagi, XFS_ERRTAG_IALLOC_READ_AGI);
146 XFS_ERRORTAG_ATTR_RW(itobp, XFS_ERRTAG_ITOBP_INOTOBP);
147 XFS_ERRORTAG_ATTR_RW(iunlink, XFS_ERRTAG_IUNLINK);
148 XFS_ERRORTAG_ATTR_RW(iunlinkrm, XFS_ERRTAG_IUNLINK_REMOVE);
149 XFS_ERRORTAG_ATTR_RW(dirinovalid, XFS_ERRTAG_DIR_INO_VALIDATE);
150 XFS_ERRORTAG_ATTR_RW(bulkstat, XFS_ERRTAG_BULKSTAT_READ_CHUNK);
151 XFS_ERRORTAG_ATTR_RW(logiodone, XFS_ERRTAG_IODONE_IOERR);
152 XFS_ERRORTAG_ATTR_RW(stratread, XFS_ERRTAG_STRATREAD_IOERR);
153 XFS_ERRORTAG_ATTR_RW(stratcmpl, XFS_ERRTAG_STRATCMPL_IOERR);
154 XFS_ERRORTAG_ATTR_RW(diowrite, XFS_ERRTAG_DIOWRITE_IOERR);
155 XFS_ERRORTAG_ATTR_RW(bmapifmt, XFS_ERRTAG_BMAPIFORMAT);
156 XFS_ERRORTAG_ATTR_RW(free_extent, XFS_ERRTAG_FREE_EXTENT);
157 XFS_ERRORTAG_ATTR_RW(rmap_finish_one, XFS_ERRTAG_RMAP_FINISH_ONE);
158 XFS_ERRORTAG_ATTR_RW(refcount_continue_update, XFS_ERRTAG_REFCOUNT_CONTINUE_UPDATE);
159 XFS_ERRORTAG_ATTR_RW(refcount_finish_one, XFS_ERRTAG_REFCOUNT_FINISH_ONE);
160 XFS_ERRORTAG_ATTR_RW(bmap_finish_one, XFS_ERRTAG_BMAP_FINISH_ONE);
161 XFS_ERRORTAG_ATTR_RW(ag_resv_critical, XFS_ERRTAG_AG_RESV_CRITICAL);
162 XFS_ERRORTAG_ATTR_RW(drop_writes, XFS_ERRTAG_DROP_WRITES);
163 XFS_ERRORTAG_ATTR_RW(log_bad_crc, XFS_ERRTAG_LOG_BAD_CRC);
164 XFS_ERRORTAG_ATTR_RW(log_item_pin, XFS_ERRTAG_LOG_ITEM_PIN);
165 XFS_ERRORTAG_ATTR_RW(buf_lru_ref, XFS_ERRTAG_BUF_LRU_REF);
166 XFS_ERRORTAG_ATTR_RW(force_repair, XFS_ERRTAG_FORCE_SCRUB_REPAIR);
167 XFS_ERRORTAG_ATTR_RW(bad_summary, XFS_ERRTAG_FORCE_SUMMARY_RECALC);
168 XFS_ERRORTAG_ATTR_RW(iunlink_fallback, XFS_ERRTAG_IUNLINK_FALLBACK);
169 XFS_ERRORTAG_ATTR_RW(buf_ioerror, XFS_ERRTAG_BUF_IOERROR);
170 XFS_ERRORTAG_ATTR_RW(reduce_max_iextents, XFS_ERRTAG_REDUCE_MAX_IEXTENTS);
171 XFS_ERRORTAG_ATTR_RW(bmap_alloc_minlen_extent, XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT);
172 XFS_ERRORTAG_ATTR_RW(ag_resv_fail, XFS_ERRTAG_AG_RESV_FAIL);
174 static struct attribute *xfs_errortag_attrs[] = {
175 XFS_ERRORTAG_ATTR_LIST(noerror),
176 XFS_ERRORTAG_ATTR_LIST(iflush1),
177 XFS_ERRORTAG_ATTR_LIST(iflush2),
178 XFS_ERRORTAG_ATTR_LIST(iflush3),
179 XFS_ERRORTAG_ATTR_LIST(iflush4),
180 XFS_ERRORTAG_ATTR_LIST(iflush5),
181 XFS_ERRORTAG_ATTR_LIST(iflush6),
182 XFS_ERRORTAG_ATTR_LIST(dareadbuf),
183 XFS_ERRORTAG_ATTR_LIST(btree_chk_lblk),
184 XFS_ERRORTAG_ATTR_LIST(btree_chk_sblk),
185 XFS_ERRORTAG_ATTR_LIST(readagf),
186 XFS_ERRORTAG_ATTR_LIST(readagi),
187 XFS_ERRORTAG_ATTR_LIST(itobp),
188 XFS_ERRORTAG_ATTR_LIST(iunlink),
189 XFS_ERRORTAG_ATTR_LIST(iunlinkrm),
190 XFS_ERRORTAG_ATTR_LIST(dirinovalid),
191 XFS_ERRORTAG_ATTR_LIST(bulkstat),
192 XFS_ERRORTAG_ATTR_LIST(logiodone),
193 XFS_ERRORTAG_ATTR_LIST(stratread),
194 XFS_ERRORTAG_ATTR_LIST(stratcmpl),
195 XFS_ERRORTAG_ATTR_LIST(diowrite),
196 XFS_ERRORTAG_ATTR_LIST(bmapifmt),
197 XFS_ERRORTAG_ATTR_LIST(free_extent),
198 XFS_ERRORTAG_ATTR_LIST(rmap_finish_one),
199 XFS_ERRORTAG_ATTR_LIST(refcount_continue_update),
200 XFS_ERRORTAG_ATTR_LIST(refcount_finish_one),
201 XFS_ERRORTAG_ATTR_LIST(bmap_finish_one),
202 XFS_ERRORTAG_ATTR_LIST(ag_resv_critical),
203 XFS_ERRORTAG_ATTR_LIST(drop_writes),
204 XFS_ERRORTAG_ATTR_LIST(log_bad_crc),
205 XFS_ERRORTAG_ATTR_LIST(log_item_pin),
206 XFS_ERRORTAG_ATTR_LIST(buf_lru_ref),
207 XFS_ERRORTAG_ATTR_LIST(force_repair),
208 XFS_ERRORTAG_ATTR_LIST(bad_summary),
209 XFS_ERRORTAG_ATTR_LIST(iunlink_fallback),
210 XFS_ERRORTAG_ATTR_LIST(buf_ioerror),
211 XFS_ERRORTAG_ATTR_LIST(reduce_max_iextents),
212 XFS_ERRORTAG_ATTR_LIST(bmap_alloc_minlen_extent),
213 XFS_ERRORTAG_ATTR_LIST(ag_resv_fail),
216 ATTRIBUTE_GROUPS(xfs_errortag);
218 static struct kobj_type xfs_errortag_ktype = {
219 .release = xfs_sysfs_release,
220 .sysfs_ops = &xfs_errortag_sysfs_ops,
221 .default_groups = xfs_errortag_groups,
226 struct xfs_mount *mp)
228 mp->m_errortag = kmem_zalloc(sizeof(unsigned int) * XFS_ERRTAG_MAX,
233 return xfs_sysfs_init(&mp->m_errortag_kobj, &xfs_errortag_ktype,
234 &mp->m_kobj, "errortag");
239 struct xfs_mount *mp)
241 xfs_sysfs_del(&mp->m_errortag_kobj);
242 kmem_free(mp->m_errortag);
247 struct xfs_mount *mp,
248 const char *expression,
251 unsigned int error_tag)
253 unsigned int randfactor;
256 * To be able to use error injection anywhere, we need to ensure error
257 * injection mechanism is already initialized.
259 * Code paths like I/O completion can be called before the
260 * initialization is complete, but be able to inject errors in such
261 * places is still useful.
266 ASSERT(error_tag < XFS_ERRTAG_MAX);
267 randfactor = mp->m_errortag[error_tag];
268 if (!randfactor || prandom_u32() % randfactor)
271 xfs_warn_ratelimited(mp,
272 "Injecting error (%s) at file %s, line %d, on filesystem \"%s\"",
273 expression, file, line, mp->m_super->s_id);
279 struct xfs_mount *mp,
280 unsigned int error_tag)
282 if (error_tag >= XFS_ERRTAG_MAX)
285 return mp->m_errortag[error_tag];
290 struct xfs_mount *mp,
291 unsigned int error_tag,
292 unsigned int tag_value)
294 if (error_tag >= XFS_ERRTAG_MAX)
297 mp->m_errortag[error_tag] = tag_value;
303 struct xfs_mount *mp,
304 unsigned int error_tag)
306 BUILD_BUG_ON(ARRAY_SIZE(xfs_errortag_random_default) != XFS_ERRTAG_MAX);
308 if (error_tag >= XFS_ERRTAG_MAX)
311 return xfs_errortag_set(mp, error_tag,
312 xfs_errortag_random_default[error_tag]);
316 xfs_errortag_clearall(
317 struct xfs_mount *mp)
319 memset(mp->m_errortag, 0, sizeof(unsigned int) * XFS_ERRTAG_MAX);
328 struct xfs_mount *mp,
329 const char *filename,
331 xfs_failaddr_t failaddr)
333 if (level <= xfs_error_level) {
334 xfs_alert_tag(mp, XFS_PTAG_ERROR_REPORT,
335 "Internal error %s at line %d of file %s. Caller %pS",
336 tag, linenum, filename, failaddr);
343 xfs_corruption_error(
346 struct xfs_mount *mp,
349 const char *filename,
351 xfs_failaddr_t failaddr)
353 if (buf && level <= xfs_error_level)
354 xfs_hex_dump(buf, bufsize);
355 xfs_error_report(tag, level, mp, filename, linenum, failaddr);
356 xfs_alert(mp, "Corruption detected. Unmount and run xfs_repair");
360 * Complain about the kinds of metadata corruption that we can't detect from a
361 * verifier, such as incorrect inter-block relationship data. Does not set
364 * Call xfs_buf_mark_corrupt, not this function.
367 xfs_buf_corruption_error(
371 struct xfs_mount *mp = bp->b_mount;
373 xfs_alert_tag(mp, XFS_PTAG_VERIFIER_ERROR,
374 "Metadata corruption detected at %pS, %s block 0x%llx",
375 fa, bp->b_ops->name, xfs_buf_daddr(bp));
377 xfs_alert(mp, "Unmount and run xfs_repair");
379 if (xfs_error_level >= XFS_ERRLEVEL_HIGH)
384 * Warnings specifically for verifier errors. Differentiate CRC vs. invalid
385 * values, and omit the stack trace unless the error level is tuned high.
388 xfs_buf_verifier_error(
394 xfs_failaddr_t failaddr)
396 struct xfs_mount *mp = bp->b_mount;
400 fa = failaddr ? failaddr : __return_address;
401 __xfs_buf_ioerror(bp, error, fa);
403 xfs_alert_tag(mp, XFS_PTAG_VERIFIER_ERROR,
404 "Metadata %s detected at %pS, %s block 0x%llx %s",
405 bp->b_error == -EFSBADCRC ? "CRC error" : "corruption",
406 fa, bp->b_ops->name, xfs_buf_daddr(bp), name);
408 xfs_alert(mp, "Unmount and run xfs_repair");
410 if (xfs_error_level >= XFS_ERRLEVEL_LOW) {
411 sz = min_t(size_t, XFS_CORRUPTION_DUMP_LEN, bufsz);
412 xfs_alert(mp, "First %d bytes of corrupted metadata buffer:",
414 xfs_hex_dump(buf, sz);
417 if (xfs_error_level >= XFS_ERRLEVEL_HIGH)
422 * Warnings specifically for verifier errors. Differentiate CRC vs. invalid
423 * values, and omit the stack trace unless the error level is tuned high.
429 xfs_failaddr_t failaddr)
431 return xfs_buf_verifier_error(bp, error, "", xfs_buf_offset(bp, 0),
432 XFS_CORRUPTION_DUMP_LEN, failaddr);
436 * Warnings for inode corruption problems. Don't bother with the stack
437 * trace unless the error level is turned up high.
440 xfs_inode_verifier_error(
441 struct xfs_inode *ip,
446 xfs_failaddr_t failaddr)
448 struct xfs_mount *mp = ip->i_mount;
452 fa = failaddr ? failaddr : __return_address;
454 xfs_alert(mp, "Metadata %s detected at %pS, inode 0x%llx %s",
455 error == -EFSBADCRC ? "CRC error" : "corruption",
456 fa, ip->i_ino, name);
458 xfs_alert(mp, "Unmount and run xfs_repair");
460 if (buf && xfs_error_level >= XFS_ERRLEVEL_LOW) {
461 sz = min_t(size_t, XFS_CORRUPTION_DUMP_LEN, bufsz);
462 xfs_alert(mp, "First %d bytes of corrupted metadata buffer:",
464 xfs_hex_dump(buf, sz);
467 if (xfs_error_level >= XFS_ERRLEVEL_HIGH)