1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
5 */
6 #include "xfs.h"
7 #include "xfs_shared.h"
8 #include "xfs_format.h"
9 #include "xfs_fs.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"
17
18 #ifdef DEBUG
19
20 static unsigned int xfs_errortag_random_default[] = {
21 XFS_RANDOM_DEFAULT,
22 XFS_RANDOM_IFLUSH_1,
23 XFS_RANDOM_IFLUSH_2,
24 XFS_RANDOM_IFLUSH_3,
25 XFS_RANDOM_IFLUSH_4,
26 XFS_RANDOM_IFLUSH_5,
27 XFS_RANDOM_IFLUSH_6,
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,
34 XFS_RANDOM_IUNLINK,
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,
60 XFS_RANDOM_LARP,
61 XFS_RANDOM_DA_LEAF_SPLIT,
62 XFS_RANDOM_ATTR_LEAF_TO_NODE,
63 };
64
65 struct xfs_errortag_attr {
66 struct attribute attr;
67 unsigned int tag;
68 };
69
70 static inline struct xfs_errortag_attr *
to_attr(struct attribute * attr)71 to_attr(struct attribute *attr)
72 {
73 return container_of(attr, struct xfs_errortag_attr, attr);
74 }
75
76 static inline struct xfs_mount *
to_mp(struct kobject * kobject)77 to_mp(struct kobject *kobject)
78 {
79 struct xfs_kobj *kobj = to_kobj(kobject);
80
81 return container_of(kobj, struct xfs_mount, m_errortag_kobj);
82 }
83
84 STATIC ssize_t
xfs_errortag_attr_store(struct kobject * kobject,struct attribute * attr,const char * buf,size_t count)85 xfs_errortag_attr_store(
86 struct kobject *kobject,
87 struct attribute *attr,
88 const char *buf,
89 size_t count)
90 {
91 struct xfs_mount *mp = to_mp(kobject);
92 struct xfs_errortag_attr *xfs_attr = to_attr(attr);
93 int ret;
94 unsigned int val;
95
96 if (strcmp(buf, "default") == 0) {
97 val = xfs_errortag_random_default[xfs_attr->tag];
98 } else {
99 ret = kstrtouint(buf, 0, &val);
100 if (ret)
101 return ret;
102 }
103
104 ret = xfs_errortag_set(mp, xfs_attr->tag, val);
105 if (ret)
106 return ret;
107 return count;
108 }
109
110 STATIC ssize_t
xfs_errortag_attr_show(struct kobject * kobject,struct attribute * attr,char * buf)111 xfs_errortag_attr_show(
112 struct kobject *kobject,
113 struct attribute *attr,
114 char *buf)
115 {
116 struct xfs_mount *mp = to_mp(kobject);
117 struct xfs_errortag_attr *xfs_attr = to_attr(attr);
118
119 return snprintf(buf, PAGE_SIZE, "%u\n",
120 xfs_errortag_get(mp, xfs_attr->tag));
121 }
122
123 static const struct sysfs_ops xfs_errortag_sysfs_ops = {
124 .show = xfs_errortag_attr_show,
125 .store = xfs_errortag_attr_store,
126 };
127
128 #define XFS_ERRORTAG_ATTR_RW(_name, _tag) \
129 static struct xfs_errortag_attr xfs_errortag_attr_##_name = { \
130 .attr = {.name = __stringify(_name), \
131 .mode = VERIFY_OCTAL_PERMISSIONS(S_IWUSR | S_IRUGO) }, \
132 .tag = (_tag), \
133 }
134
135 #define XFS_ERRORTAG_ATTR_LIST(_name) &xfs_errortag_attr_##_name.attr
136
137 XFS_ERRORTAG_ATTR_RW(noerror, XFS_ERRTAG_NOERROR);
138 XFS_ERRORTAG_ATTR_RW(iflush1, XFS_ERRTAG_IFLUSH_1);
139 XFS_ERRORTAG_ATTR_RW(iflush2, XFS_ERRTAG_IFLUSH_2);
140 XFS_ERRORTAG_ATTR_RW(iflush3, XFS_ERRTAG_IFLUSH_3);
141 XFS_ERRORTAG_ATTR_RW(iflush4, XFS_ERRTAG_IFLUSH_4);
142 XFS_ERRORTAG_ATTR_RW(iflush5, XFS_ERRTAG_IFLUSH_5);
143 XFS_ERRORTAG_ATTR_RW(iflush6, XFS_ERRTAG_IFLUSH_6);
144 XFS_ERRORTAG_ATTR_RW(dareadbuf, XFS_ERRTAG_DA_READ_BUF);
145 XFS_ERRORTAG_ATTR_RW(btree_chk_lblk, XFS_ERRTAG_BTREE_CHECK_LBLOCK);
146 XFS_ERRORTAG_ATTR_RW(btree_chk_sblk, XFS_ERRTAG_BTREE_CHECK_SBLOCK);
147 XFS_ERRORTAG_ATTR_RW(readagf, XFS_ERRTAG_ALLOC_READ_AGF);
148 XFS_ERRORTAG_ATTR_RW(readagi, XFS_ERRTAG_IALLOC_READ_AGI);
149 XFS_ERRORTAG_ATTR_RW(itobp, XFS_ERRTAG_ITOBP_INOTOBP);
150 XFS_ERRORTAG_ATTR_RW(iunlink, XFS_ERRTAG_IUNLINK);
151 XFS_ERRORTAG_ATTR_RW(iunlinkrm, XFS_ERRTAG_IUNLINK_REMOVE);
152 XFS_ERRORTAG_ATTR_RW(dirinovalid, XFS_ERRTAG_DIR_INO_VALIDATE);
153 XFS_ERRORTAG_ATTR_RW(bulkstat, XFS_ERRTAG_BULKSTAT_READ_CHUNK);
154 XFS_ERRORTAG_ATTR_RW(logiodone, XFS_ERRTAG_IODONE_IOERR);
155 XFS_ERRORTAG_ATTR_RW(stratread, XFS_ERRTAG_STRATREAD_IOERR);
156 XFS_ERRORTAG_ATTR_RW(stratcmpl, XFS_ERRTAG_STRATCMPL_IOERR);
157 XFS_ERRORTAG_ATTR_RW(diowrite, XFS_ERRTAG_DIOWRITE_IOERR);
158 XFS_ERRORTAG_ATTR_RW(bmapifmt, XFS_ERRTAG_BMAPIFORMAT);
159 XFS_ERRORTAG_ATTR_RW(free_extent, XFS_ERRTAG_FREE_EXTENT);
160 XFS_ERRORTAG_ATTR_RW(rmap_finish_one, XFS_ERRTAG_RMAP_FINISH_ONE);
161 XFS_ERRORTAG_ATTR_RW(refcount_continue_update, XFS_ERRTAG_REFCOUNT_CONTINUE_UPDATE);
162 XFS_ERRORTAG_ATTR_RW(refcount_finish_one, XFS_ERRTAG_REFCOUNT_FINISH_ONE);
163 XFS_ERRORTAG_ATTR_RW(bmap_finish_one, XFS_ERRTAG_BMAP_FINISH_ONE);
164 XFS_ERRORTAG_ATTR_RW(ag_resv_critical, XFS_ERRTAG_AG_RESV_CRITICAL);
165 XFS_ERRORTAG_ATTR_RW(drop_writes, XFS_ERRTAG_DROP_WRITES);
166 XFS_ERRORTAG_ATTR_RW(log_bad_crc, XFS_ERRTAG_LOG_BAD_CRC);
167 XFS_ERRORTAG_ATTR_RW(log_item_pin, XFS_ERRTAG_LOG_ITEM_PIN);
168 XFS_ERRORTAG_ATTR_RW(buf_lru_ref, XFS_ERRTAG_BUF_LRU_REF);
169 XFS_ERRORTAG_ATTR_RW(force_repair, XFS_ERRTAG_FORCE_SCRUB_REPAIR);
170 XFS_ERRORTAG_ATTR_RW(bad_summary, XFS_ERRTAG_FORCE_SUMMARY_RECALC);
171 XFS_ERRORTAG_ATTR_RW(iunlink_fallback, XFS_ERRTAG_IUNLINK_FALLBACK);
172 XFS_ERRORTAG_ATTR_RW(buf_ioerror, XFS_ERRTAG_BUF_IOERROR);
173 XFS_ERRORTAG_ATTR_RW(reduce_max_iextents, XFS_ERRTAG_REDUCE_MAX_IEXTENTS);
174 XFS_ERRORTAG_ATTR_RW(bmap_alloc_minlen_extent, XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT);
175 XFS_ERRORTAG_ATTR_RW(ag_resv_fail, XFS_ERRTAG_AG_RESV_FAIL);
176 XFS_ERRORTAG_ATTR_RW(larp, XFS_ERRTAG_LARP);
177 XFS_ERRORTAG_ATTR_RW(da_leaf_split, XFS_ERRTAG_DA_LEAF_SPLIT);
178 XFS_ERRORTAG_ATTR_RW(attr_leaf_to_node, XFS_ERRTAG_ATTR_LEAF_TO_NODE);
179
180 static struct attribute *xfs_errortag_attrs[] = {
181 XFS_ERRORTAG_ATTR_LIST(noerror),
182 XFS_ERRORTAG_ATTR_LIST(iflush1),
183 XFS_ERRORTAG_ATTR_LIST(iflush2),
184 XFS_ERRORTAG_ATTR_LIST(iflush3),
185 XFS_ERRORTAG_ATTR_LIST(iflush4),
186 XFS_ERRORTAG_ATTR_LIST(iflush5),
187 XFS_ERRORTAG_ATTR_LIST(iflush6),
188 XFS_ERRORTAG_ATTR_LIST(dareadbuf),
189 XFS_ERRORTAG_ATTR_LIST(btree_chk_lblk),
190 XFS_ERRORTAG_ATTR_LIST(btree_chk_sblk),
191 XFS_ERRORTAG_ATTR_LIST(readagf),
192 XFS_ERRORTAG_ATTR_LIST(readagi),
193 XFS_ERRORTAG_ATTR_LIST(itobp),
194 XFS_ERRORTAG_ATTR_LIST(iunlink),
195 XFS_ERRORTAG_ATTR_LIST(iunlinkrm),
196 XFS_ERRORTAG_ATTR_LIST(dirinovalid),
197 XFS_ERRORTAG_ATTR_LIST(bulkstat),
198 XFS_ERRORTAG_ATTR_LIST(logiodone),
199 XFS_ERRORTAG_ATTR_LIST(stratread),
200 XFS_ERRORTAG_ATTR_LIST(stratcmpl),
201 XFS_ERRORTAG_ATTR_LIST(diowrite),
202 XFS_ERRORTAG_ATTR_LIST(bmapifmt),
203 XFS_ERRORTAG_ATTR_LIST(free_extent),
204 XFS_ERRORTAG_ATTR_LIST(rmap_finish_one),
205 XFS_ERRORTAG_ATTR_LIST(refcount_continue_update),
206 XFS_ERRORTAG_ATTR_LIST(refcount_finish_one),
207 XFS_ERRORTAG_ATTR_LIST(bmap_finish_one),
208 XFS_ERRORTAG_ATTR_LIST(ag_resv_critical),
209 XFS_ERRORTAG_ATTR_LIST(drop_writes),
210 XFS_ERRORTAG_ATTR_LIST(log_bad_crc),
211 XFS_ERRORTAG_ATTR_LIST(log_item_pin),
212 XFS_ERRORTAG_ATTR_LIST(buf_lru_ref),
213 XFS_ERRORTAG_ATTR_LIST(force_repair),
214 XFS_ERRORTAG_ATTR_LIST(bad_summary),
215 XFS_ERRORTAG_ATTR_LIST(iunlink_fallback),
216 XFS_ERRORTAG_ATTR_LIST(buf_ioerror),
217 XFS_ERRORTAG_ATTR_LIST(reduce_max_iextents),
218 XFS_ERRORTAG_ATTR_LIST(bmap_alloc_minlen_extent),
219 XFS_ERRORTAG_ATTR_LIST(ag_resv_fail),
220 XFS_ERRORTAG_ATTR_LIST(larp),
221 XFS_ERRORTAG_ATTR_LIST(da_leaf_split),
222 XFS_ERRORTAG_ATTR_LIST(attr_leaf_to_node),
223 NULL,
224 };
225 ATTRIBUTE_GROUPS(xfs_errortag);
226
227 static struct kobj_type xfs_errortag_ktype = {
228 .release = xfs_sysfs_release,
229 .sysfs_ops = &xfs_errortag_sysfs_ops,
230 .default_groups = xfs_errortag_groups,
231 };
232
233 int
xfs_errortag_init(struct xfs_mount * mp)234 xfs_errortag_init(
235 struct xfs_mount *mp)
236 {
237 int ret;
238
239 mp->m_errortag = kmem_zalloc(sizeof(unsigned int) * XFS_ERRTAG_MAX,
240 KM_MAYFAIL);
241 if (!mp->m_errortag)
242 return -ENOMEM;
243
244 ret = xfs_sysfs_init(&mp->m_errortag_kobj, &xfs_errortag_ktype,
245 &mp->m_kobj, "errortag");
246 if (ret)
247 kmem_free(mp->m_errortag);
248 return ret;
249 }
250
251 void
xfs_errortag_del(struct xfs_mount * mp)252 xfs_errortag_del(
253 struct xfs_mount *mp)
254 {
255 xfs_sysfs_del(&mp->m_errortag_kobj);
256 kmem_free(mp->m_errortag);
257 }
258
259 bool
xfs_errortag_test(struct xfs_mount * mp,const char * expression,const char * file,int line,unsigned int error_tag)260 xfs_errortag_test(
261 struct xfs_mount *mp,
262 const char *expression,
263 const char *file,
264 int line,
265 unsigned int error_tag)
266 {
267 unsigned int randfactor;
268
269 /*
270 * To be able to use error injection anywhere, we need to ensure error
271 * injection mechanism is already initialized.
272 *
273 * Code paths like I/O completion can be called before the
274 * initialization is complete, but be able to inject errors in such
275 * places is still useful.
276 */
277 if (!mp->m_errortag)
278 return false;
279
280 ASSERT(error_tag < XFS_ERRTAG_MAX);
281 randfactor = mp->m_errortag[error_tag];
282 if (!randfactor || prandom_u32_max(randfactor))
283 return false;
284
285 xfs_warn_ratelimited(mp,
286 "Injecting error (%s) at file %s, line %d, on filesystem \"%s\"",
287 expression, file, line, mp->m_super->s_id);
288 return true;
289 }
290
291 int
xfs_errortag_get(struct xfs_mount * mp,unsigned int error_tag)292 xfs_errortag_get(
293 struct xfs_mount *mp,
294 unsigned int error_tag)
295 {
296 if (error_tag >= XFS_ERRTAG_MAX)
297 return -EINVAL;
298
299 return mp->m_errortag[error_tag];
300 }
301
302 int
xfs_errortag_set(struct xfs_mount * mp,unsigned int error_tag,unsigned int tag_value)303 xfs_errortag_set(
304 struct xfs_mount *mp,
305 unsigned int error_tag,
306 unsigned int tag_value)
307 {
308 if (error_tag >= XFS_ERRTAG_MAX)
309 return -EINVAL;
310
311 mp->m_errortag[error_tag] = tag_value;
312 return 0;
313 }
314
315 int
xfs_errortag_add(struct xfs_mount * mp,unsigned int error_tag)316 xfs_errortag_add(
317 struct xfs_mount *mp,
318 unsigned int error_tag)
319 {
320 BUILD_BUG_ON(ARRAY_SIZE(xfs_errortag_random_default) != XFS_ERRTAG_MAX);
321
322 if (error_tag >= XFS_ERRTAG_MAX)
323 return -EINVAL;
324
325 return xfs_errortag_set(mp, error_tag,
326 xfs_errortag_random_default[error_tag]);
327 }
328
329 int
xfs_errortag_clearall(struct xfs_mount * mp)330 xfs_errortag_clearall(
331 struct xfs_mount *mp)
332 {
333 memset(mp->m_errortag, 0, sizeof(unsigned int) * XFS_ERRTAG_MAX);
334 return 0;
335 }
336 #endif /* DEBUG */
337
338 void
xfs_error_report(const char * tag,int level,struct xfs_mount * mp,const char * filename,int linenum,xfs_failaddr_t failaddr)339 xfs_error_report(
340 const char *tag,
341 int level,
342 struct xfs_mount *mp,
343 const char *filename,
344 int linenum,
345 xfs_failaddr_t failaddr)
346 {
347 if (level <= xfs_error_level) {
348 xfs_alert_tag(mp, XFS_PTAG_ERROR_REPORT,
349 "Internal error %s at line %d of file %s. Caller %pS",
350 tag, linenum, filename, failaddr);
351
352 xfs_stack_trace();
353 }
354 }
355
356 void
xfs_corruption_error(const char * tag,int level,struct xfs_mount * mp,const void * buf,size_t bufsize,const char * filename,int linenum,xfs_failaddr_t failaddr)357 xfs_corruption_error(
358 const char *tag,
359 int level,
360 struct xfs_mount *mp,
361 const void *buf,
362 size_t bufsize,
363 const char *filename,
364 int linenum,
365 xfs_failaddr_t failaddr)
366 {
367 if (buf && level <= xfs_error_level)
368 xfs_hex_dump(buf, bufsize);
369 xfs_error_report(tag, level, mp, filename, linenum, failaddr);
370 xfs_alert(mp, "Corruption detected. Unmount and run xfs_repair");
371 }
372
373 /*
374 * Complain about the kinds of metadata corruption that we can't detect from a
375 * verifier, such as incorrect inter-block relationship data. Does not set
376 * bp->b_error.
377 *
378 * Call xfs_buf_mark_corrupt, not this function.
379 */
380 void
xfs_buf_corruption_error(struct xfs_buf * bp,xfs_failaddr_t fa)381 xfs_buf_corruption_error(
382 struct xfs_buf *bp,
383 xfs_failaddr_t fa)
384 {
385 struct xfs_mount *mp = bp->b_mount;
386
387 xfs_alert_tag(mp, XFS_PTAG_VERIFIER_ERROR,
388 "Metadata corruption detected at %pS, %s block 0x%llx",
389 fa, bp->b_ops->name, xfs_buf_daddr(bp));
390
391 xfs_alert(mp, "Unmount and run xfs_repair");
392
393 if (xfs_error_level >= XFS_ERRLEVEL_HIGH)
394 xfs_stack_trace();
395 }
396
397 /*
398 * Warnings specifically for verifier errors. Differentiate CRC vs. invalid
399 * values, and omit the stack trace unless the error level is tuned high.
400 */
401 void
xfs_buf_verifier_error(struct xfs_buf * bp,int error,const char * name,const void * buf,size_t bufsz,xfs_failaddr_t failaddr)402 xfs_buf_verifier_error(
403 struct xfs_buf *bp,
404 int error,
405 const char *name,
406 const void *buf,
407 size_t bufsz,
408 xfs_failaddr_t failaddr)
409 {
410 struct xfs_mount *mp = bp->b_mount;
411 xfs_failaddr_t fa;
412 int sz;
413
414 fa = failaddr ? failaddr : __return_address;
415 __xfs_buf_ioerror(bp, error, fa);
416
417 xfs_alert_tag(mp, XFS_PTAG_VERIFIER_ERROR,
418 "Metadata %s detected at %pS, %s block 0x%llx %s",
419 bp->b_error == -EFSBADCRC ? "CRC error" : "corruption",
420 fa, bp->b_ops->name, xfs_buf_daddr(bp), name);
421
422 xfs_alert(mp, "Unmount and run xfs_repair");
423
424 if (xfs_error_level >= XFS_ERRLEVEL_LOW) {
425 sz = min_t(size_t, XFS_CORRUPTION_DUMP_LEN, bufsz);
426 xfs_alert(mp, "First %d bytes of corrupted metadata buffer:",
427 sz);
428 xfs_hex_dump(buf, sz);
429 }
430
431 if (xfs_error_level >= XFS_ERRLEVEL_HIGH)
432 xfs_stack_trace();
433 }
434
435 /*
436 * Warnings specifically for verifier errors. Differentiate CRC vs. invalid
437 * values, and omit the stack trace unless the error level is tuned high.
438 */
439 void
xfs_verifier_error(struct xfs_buf * bp,int error,xfs_failaddr_t failaddr)440 xfs_verifier_error(
441 struct xfs_buf *bp,
442 int error,
443 xfs_failaddr_t failaddr)
444 {
445 return xfs_buf_verifier_error(bp, error, "", xfs_buf_offset(bp, 0),
446 XFS_CORRUPTION_DUMP_LEN, failaddr);
447 }
448
449 /*
450 * Warnings for inode corruption problems. Don't bother with the stack
451 * trace unless the error level is turned up high.
452 */
453 void
xfs_inode_verifier_error(struct xfs_inode * ip,int error,const char * name,const void * buf,size_t bufsz,xfs_failaddr_t failaddr)454 xfs_inode_verifier_error(
455 struct xfs_inode *ip,
456 int error,
457 const char *name,
458 const void *buf,
459 size_t bufsz,
460 xfs_failaddr_t failaddr)
461 {
462 struct xfs_mount *mp = ip->i_mount;
463 xfs_failaddr_t fa;
464 int sz;
465
466 fa = failaddr ? failaddr : __return_address;
467
468 xfs_alert(mp, "Metadata %s detected at %pS, inode 0x%llx %s",
469 error == -EFSBADCRC ? "CRC error" : "corruption",
470 fa, ip->i_ino, name);
471
472 xfs_alert(mp, "Unmount and run xfs_repair");
473
474 if (buf && xfs_error_level >= XFS_ERRLEVEL_LOW) {
475 sz = min_t(size_t, XFS_CORRUPTION_DUMP_LEN, bufsz);
476 xfs_alert(mp, "First %d bytes of corrupted metadata buffer:",
477 sz);
478 xfs_hex_dump(buf, sz);
479 }
480
481 if (xfs_error_level >= XFS_ERRLEVEL_HIGH)
482 xfs_stack_trace();
483 }
484