1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
7 #include "xfs_format.h"
9 #include "xfs_log_format.h"
10 #include "xfs_trans_resv.h"
11 #include "xfs_mount.h"
12 #include "xfs_errortag.h"
13 #include "xfs_error.h"
14 #include "xfs_sysfs.h"
15 #include "xfs_inode.h"
19 static unsigned int xfs_errortag_random_default[] = {
27 XFS_RANDOM_DA_READ_BUF,
28 XFS_RANDOM_BTREE_CHECK_LBLOCK,
29 XFS_RANDOM_BTREE_CHECK_SBLOCK,
30 XFS_RANDOM_ALLOC_READ_AGF,
31 XFS_RANDOM_IALLOC_READ_AGI,
32 XFS_RANDOM_ITOBP_INOTOBP,
34 XFS_RANDOM_IUNLINK_REMOVE,
35 XFS_RANDOM_DIR_INO_VALIDATE,
36 XFS_RANDOM_BULKSTAT_READ_CHUNK,
37 XFS_RANDOM_IODONE_IOERR,
38 XFS_RANDOM_STRATREAD_IOERR,
39 XFS_RANDOM_STRATCMPL_IOERR,
40 XFS_RANDOM_DIOWRITE_IOERR,
41 XFS_RANDOM_BMAPIFORMAT,
42 XFS_RANDOM_FREE_EXTENT,
43 XFS_RANDOM_RMAP_FINISH_ONE,
44 XFS_RANDOM_REFCOUNT_CONTINUE_UPDATE,
45 XFS_RANDOM_REFCOUNT_FINISH_ONE,
46 XFS_RANDOM_BMAP_FINISH_ONE,
47 XFS_RANDOM_AG_RESV_CRITICAL,
48 XFS_RANDOM_DROP_WRITES,
49 XFS_RANDOM_LOG_BAD_CRC,
50 XFS_RANDOM_LOG_ITEM_PIN,
51 XFS_RANDOM_BUF_LRU_REF,
52 XFS_RANDOM_FORCE_SCRUB_REPAIR,
53 XFS_RANDOM_FORCE_SUMMARY_RECALC,
54 XFS_RANDOM_IUNLINK_FALLBACK,
57 struct xfs_errortag_attr {
58 struct attribute attr;
62 static inline struct xfs_errortag_attr *
63 to_attr(struct attribute *attr)
65 return container_of(attr, struct xfs_errortag_attr, attr);
68 static inline struct xfs_mount *
69 to_mp(struct kobject *kobject)
71 struct xfs_kobj *kobj = to_kobj(kobject);
73 return container_of(kobj, struct xfs_mount, m_errortag_kobj);
77 xfs_errortag_attr_store(
78 struct kobject *kobject,
79 struct attribute *attr,
83 struct xfs_mount *mp = to_mp(kobject);
84 struct xfs_errortag_attr *xfs_attr = to_attr(attr);
88 if (strcmp(buf, "default") == 0) {
89 val = xfs_errortag_random_default[xfs_attr->tag];
91 ret = kstrtouint(buf, 0, &val);
96 ret = xfs_errortag_set(mp, xfs_attr->tag, val);
103 xfs_errortag_attr_show(
104 struct kobject *kobject,
105 struct attribute *attr,
108 struct xfs_mount *mp = to_mp(kobject);
109 struct xfs_errortag_attr *xfs_attr = to_attr(attr);
111 return snprintf(buf, PAGE_SIZE, "%u\n",
112 xfs_errortag_get(mp, xfs_attr->tag));
115 static const struct sysfs_ops xfs_errortag_sysfs_ops = {
116 .show = xfs_errortag_attr_show,
117 .store = xfs_errortag_attr_store,
120 #define XFS_ERRORTAG_ATTR_RW(_name, _tag) \
121 static struct xfs_errortag_attr xfs_errortag_attr_##_name = { \
122 .attr = {.name = __stringify(_name), \
123 .mode = VERIFY_OCTAL_PERMISSIONS(S_IWUSR | S_IRUGO) }, \
127 #define XFS_ERRORTAG_ATTR_LIST(_name) &xfs_errortag_attr_##_name.attr
129 XFS_ERRORTAG_ATTR_RW(noerror, XFS_ERRTAG_NOERROR);
130 XFS_ERRORTAG_ATTR_RW(iflush1, XFS_ERRTAG_IFLUSH_1);
131 XFS_ERRORTAG_ATTR_RW(iflush2, XFS_ERRTAG_IFLUSH_2);
132 XFS_ERRORTAG_ATTR_RW(iflush3, XFS_ERRTAG_IFLUSH_3);
133 XFS_ERRORTAG_ATTR_RW(iflush4, XFS_ERRTAG_IFLUSH_4);
134 XFS_ERRORTAG_ATTR_RW(iflush5, XFS_ERRTAG_IFLUSH_5);
135 XFS_ERRORTAG_ATTR_RW(iflush6, XFS_ERRTAG_IFLUSH_6);
136 XFS_ERRORTAG_ATTR_RW(dareadbuf, XFS_ERRTAG_DA_READ_BUF);
137 XFS_ERRORTAG_ATTR_RW(btree_chk_lblk, XFS_ERRTAG_BTREE_CHECK_LBLOCK);
138 XFS_ERRORTAG_ATTR_RW(btree_chk_sblk, XFS_ERRTAG_BTREE_CHECK_SBLOCK);
139 XFS_ERRORTAG_ATTR_RW(readagf, XFS_ERRTAG_ALLOC_READ_AGF);
140 XFS_ERRORTAG_ATTR_RW(readagi, XFS_ERRTAG_IALLOC_READ_AGI);
141 XFS_ERRORTAG_ATTR_RW(itobp, XFS_ERRTAG_ITOBP_INOTOBP);
142 XFS_ERRORTAG_ATTR_RW(iunlink, XFS_ERRTAG_IUNLINK);
143 XFS_ERRORTAG_ATTR_RW(iunlinkrm, XFS_ERRTAG_IUNLINK_REMOVE);
144 XFS_ERRORTAG_ATTR_RW(dirinovalid, XFS_ERRTAG_DIR_INO_VALIDATE);
145 XFS_ERRORTAG_ATTR_RW(bulkstat, XFS_ERRTAG_BULKSTAT_READ_CHUNK);
146 XFS_ERRORTAG_ATTR_RW(logiodone, XFS_ERRTAG_IODONE_IOERR);
147 XFS_ERRORTAG_ATTR_RW(stratread, XFS_ERRTAG_STRATREAD_IOERR);
148 XFS_ERRORTAG_ATTR_RW(stratcmpl, XFS_ERRTAG_STRATCMPL_IOERR);
149 XFS_ERRORTAG_ATTR_RW(diowrite, XFS_ERRTAG_DIOWRITE_IOERR);
150 XFS_ERRORTAG_ATTR_RW(bmapifmt, XFS_ERRTAG_BMAPIFORMAT);
151 XFS_ERRORTAG_ATTR_RW(free_extent, XFS_ERRTAG_FREE_EXTENT);
152 XFS_ERRORTAG_ATTR_RW(rmap_finish_one, XFS_ERRTAG_RMAP_FINISH_ONE);
153 XFS_ERRORTAG_ATTR_RW(refcount_continue_update, XFS_ERRTAG_REFCOUNT_CONTINUE_UPDATE);
154 XFS_ERRORTAG_ATTR_RW(refcount_finish_one, XFS_ERRTAG_REFCOUNT_FINISH_ONE);
155 XFS_ERRORTAG_ATTR_RW(bmap_finish_one, XFS_ERRTAG_BMAP_FINISH_ONE);
156 XFS_ERRORTAG_ATTR_RW(ag_resv_critical, XFS_ERRTAG_AG_RESV_CRITICAL);
157 XFS_ERRORTAG_ATTR_RW(drop_writes, XFS_ERRTAG_DROP_WRITES);
158 XFS_ERRORTAG_ATTR_RW(log_bad_crc, XFS_ERRTAG_LOG_BAD_CRC);
159 XFS_ERRORTAG_ATTR_RW(log_item_pin, XFS_ERRTAG_LOG_ITEM_PIN);
160 XFS_ERRORTAG_ATTR_RW(buf_lru_ref, XFS_ERRTAG_BUF_LRU_REF);
161 XFS_ERRORTAG_ATTR_RW(force_repair, XFS_ERRTAG_FORCE_SCRUB_REPAIR);
162 XFS_ERRORTAG_ATTR_RW(bad_summary, XFS_ERRTAG_FORCE_SUMMARY_RECALC);
163 XFS_ERRORTAG_ATTR_RW(iunlink_fallback, XFS_ERRTAG_IUNLINK_FALLBACK);
165 static 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),
194 XFS_ERRORTAG_ATTR_LIST(drop_writes),
195 XFS_ERRORTAG_ATTR_LIST(log_bad_crc),
196 XFS_ERRORTAG_ATTR_LIST(log_item_pin),
197 XFS_ERRORTAG_ATTR_LIST(buf_lru_ref),
198 XFS_ERRORTAG_ATTR_LIST(force_repair),
199 XFS_ERRORTAG_ATTR_LIST(bad_summary),
200 XFS_ERRORTAG_ATTR_LIST(iunlink_fallback),
204 static struct kobj_type xfs_errortag_ktype = {
205 .release = xfs_sysfs_release,
206 .sysfs_ops = &xfs_errortag_sysfs_ops,
207 .default_attrs = xfs_errortag_attrs,
212 struct xfs_mount *mp)
214 mp->m_errortag = kmem_zalloc(sizeof(unsigned int) * XFS_ERRTAG_MAX,
215 KM_SLEEP | KM_MAYFAIL);
219 return xfs_sysfs_init(&mp->m_errortag_kobj, &xfs_errortag_ktype,
220 &mp->m_kobj, "errortag");
225 struct xfs_mount *mp)
227 xfs_sysfs_del(&mp->m_errortag_kobj);
228 kmem_free(mp->m_errortag);
233 struct xfs_mount *mp,
234 const char *expression,
237 unsigned int error_tag)
239 unsigned int randfactor;
242 * To be able to use error injection anywhere, we need to ensure error
243 * injection mechanism is already initialized.
245 * Code paths like I/O completion can be called before the
246 * initialization is complete, but be able to inject errors in such
247 * places is still useful.
252 ASSERT(error_tag < XFS_ERRTAG_MAX);
253 randfactor = mp->m_errortag[error_tag];
254 if (!randfactor || prandom_u32() % randfactor)
257 xfs_warn_ratelimited(mp,
258 "Injecting error (%s) at file %s, line %d, on filesystem \"%s\"",
259 expression, file, line, mp->m_fsname);
265 struct xfs_mount *mp,
266 unsigned int error_tag)
268 if (error_tag >= XFS_ERRTAG_MAX)
271 return mp->m_errortag[error_tag];
276 struct xfs_mount *mp,
277 unsigned int error_tag,
278 unsigned int tag_value)
280 if (error_tag >= XFS_ERRTAG_MAX)
283 mp->m_errortag[error_tag] = tag_value;
289 struct xfs_mount *mp,
290 unsigned int error_tag)
292 if (error_tag >= XFS_ERRTAG_MAX)
295 return xfs_errortag_set(mp, error_tag,
296 xfs_errortag_random_default[error_tag]);
300 xfs_errortag_clearall(
301 struct xfs_mount *mp)
303 memset(mp->m_errortag, 0, sizeof(unsigned int) * XFS_ERRTAG_MAX);
312 struct xfs_mount *mp,
313 const char *filename,
315 xfs_failaddr_t failaddr)
317 if (level <= xfs_error_level) {
318 xfs_alert_tag(mp, XFS_PTAG_ERROR_REPORT,
319 "Internal error %s at line %d of file %s. Caller %pS",
320 tag, linenum, filename, failaddr);
327 xfs_corruption_error(
330 struct xfs_mount *mp,
333 const char *filename,
335 xfs_failaddr_t failaddr)
337 if (level <= xfs_error_level)
338 xfs_hex_dump(buf, bufsize);
339 xfs_error_report(tag, level, mp, filename, linenum, failaddr);
340 xfs_alert(mp, "Corruption detected. Unmount and run xfs_repair");
344 * Warnings specifically for verifier errors. Differentiate CRC vs. invalid
345 * values, and omit the stack trace unless the error level is tuned high.
348 xfs_buf_verifier_error(
354 xfs_failaddr_t failaddr)
356 struct xfs_mount *mp = bp->b_target->bt_mount;
360 fa = failaddr ? failaddr : __return_address;
361 __xfs_buf_ioerror(bp, error, fa);
363 xfs_alert_tag(mp, XFS_PTAG_VERIFIER_ERROR,
364 "Metadata %s detected at %pS, %s block 0x%llx %s",
365 bp->b_error == -EFSBADCRC ? "CRC error" : "corruption",
366 fa, bp->b_ops->name, bp->b_bn, name);
368 xfs_alert(mp, "Unmount and run xfs_repair");
370 if (xfs_error_level >= XFS_ERRLEVEL_LOW) {
371 sz = min_t(size_t, XFS_CORRUPTION_DUMP_LEN, bufsz);
372 xfs_alert(mp, "First %d bytes of corrupted metadata buffer:",
374 xfs_hex_dump(buf, sz);
377 if (xfs_error_level >= XFS_ERRLEVEL_HIGH)
382 * Warnings specifically for verifier errors. Differentiate CRC vs. invalid
383 * values, and omit the stack trace unless the error level is tuned high.
389 xfs_failaddr_t failaddr)
391 return xfs_buf_verifier_error(bp, error, "", xfs_buf_offset(bp, 0),
392 XFS_CORRUPTION_DUMP_LEN, failaddr);
396 * Warnings for inode corruption problems. Don't bother with the stack
397 * trace unless the error level is turned up high.
400 xfs_inode_verifier_error(
401 struct xfs_inode *ip,
406 xfs_failaddr_t failaddr)
408 struct xfs_mount *mp = ip->i_mount;
412 fa = failaddr ? failaddr : __return_address;
414 xfs_alert(mp, "Metadata %s detected at %pS, inode 0x%llx %s",
415 error == -EFSBADCRC ? "CRC error" : "corruption",
416 fa, ip->i_ino, name);
418 xfs_alert(mp, "Unmount and run xfs_repair");
420 if (buf && xfs_error_level >= XFS_ERRLEVEL_LOW) {
421 sz = min_t(size_t, XFS_CORRUPTION_DUMP_LEN, bufsz);
422 xfs_alert(mp, "First %d bytes of corrupted metadata buffer:",
424 xfs_hex_dump(buf, sz);
427 if (xfs_error_level >= XFS_ERRLEVEL_HIGH)