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 0, /* XFS_RANDOM_DROP_WRITES has been removed */
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
,
61 XFS_RANDOM_DA_LEAF_SPLIT
,
62 XFS_RANDOM_ATTR_LEAF_TO_NODE
,
63 XFS_RANDOM_WB_DELAY_MS
,
64 XFS_RANDOM_WRITE_DELAY_MS
,
65 XFS_RANDOM_EXCHMAPS_FINISH_ONE
,
68 struct xfs_errortag_attr
{
69 struct attribute attr
;
73 static inline struct xfs_errortag_attr
*
74 to_attr(struct attribute
*attr
)
76 return container_of(attr
, struct xfs_errortag_attr
, attr
);
79 static inline struct xfs_mount
*
80 to_mp(struct kobject
*kobject
)
82 struct xfs_kobj
*kobj
= to_kobj(kobject
);
84 return container_of(kobj
, struct xfs_mount
, m_errortag_kobj
);
88 xfs_errortag_attr_store(
89 struct kobject
*kobject
,
90 struct attribute
*attr
,
94 struct xfs_mount
*mp
= to_mp(kobject
);
95 struct xfs_errortag_attr
*xfs_attr
= to_attr(attr
);
99 if (strcmp(buf
, "default") == 0) {
100 val
= xfs_errortag_random_default
[xfs_attr
->tag
];
102 ret
= kstrtouint(buf
, 0, &val
);
107 ret
= xfs_errortag_set(mp
, xfs_attr
->tag
, val
);
114 xfs_errortag_attr_show(
115 struct kobject
*kobject
,
116 struct attribute
*attr
,
119 struct xfs_mount
*mp
= to_mp(kobject
);
120 struct xfs_errortag_attr
*xfs_attr
= to_attr(attr
);
122 return snprintf(buf
, PAGE_SIZE
, "%u\n",
123 xfs_errortag_get(mp
, xfs_attr
->tag
));
126 static const struct sysfs_ops xfs_errortag_sysfs_ops
= {
127 .show
= xfs_errortag_attr_show
,
128 .store
= xfs_errortag_attr_store
,
131 #define XFS_ERRORTAG_ATTR_RW(_name, _tag) \
132 static struct xfs_errortag_attr xfs_errortag_attr_##_name = { \
133 .attr = {.name = __stringify(_name), \
134 .mode = VERIFY_OCTAL_PERMISSIONS(S_IWUSR | S_IRUGO) }, \
138 #define XFS_ERRORTAG_ATTR_LIST(_name) &xfs_errortag_attr_##_name.attr
140 XFS_ERRORTAG_ATTR_RW(noerror
, XFS_ERRTAG_NOERROR
);
141 XFS_ERRORTAG_ATTR_RW(iflush1
, XFS_ERRTAG_IFLUSH_1
);
142 XFS_ERRORTAG_ATTR_RW(iflush2
, XFS_ERRTAG_IFLUSH_2
);
143 XFS_ERRORTAG_ATTR_RW(iflush3
, XFS_ERRTAG_IFLUSH_3
);
144 XFS_ERRORTAG_ATTR_RW(iflush4
, XFS_ERRTAG_IFLUSH_4
);
145 XFS_ERRORTAG_ATTR_RW(iflush5
, XFS_ERRTAG_IFLUSH_5
);
146 XFS_ERRORTAG_ATTR_RW(iflush6
, XFS_ERRTAG_IFLUSH_6
);
147 XFS_ERRORTAG_ATTR_RW(dareadbuf
, XFS_ERRTAG_DA_READ_BUF
);
148 XFS_ERRORTAG_ATTR_RW(btree_chk_lblk
, XFS_ERRTAG_BTREE_CHECK_LBLOCK
);
149 XFS_ERRORTAG_ATTR_RW(btree_chk_sblk
, XFS_ERRTAG_BTREE_CHECK_SBLOCK
);
150 XFS_ERRORTAG_ATTR_RW(readagf
, XFS_ERRTAG_ALLOC_READ_AGF
);
151 XFS_ERRORTAG_ATTR_RW(readagi
, XFS_ERRTAG_IALLOC_READ_AGI
);
152 XFS_ERRORTAG_ATTR_RW(itobp
, XFS_ERRTAG_ITOBP_INOTOBP
);
153 XFS_ERRORTAG_ATTR_RW(iunlink
, XFS_ERRTAG_IUNLINK
);
154 XFS_ERRORTAG_ATTR_RW(iunlinkrm
, XFS_ERRTAG_IUNLINK_REMOVE
);
155 XFS_ERRORTAG_ATTR_RW(dirinovalid
, XFS_ERRTAG_DIR_INO_VALIDATE
);
156 XFS_ERRORTAG_ATTR_RW(bulkstat
, XFS_ERRTAG_BULKSTAT_READ_CHUNK
);
157 XFS_ERRORTAG_ATTR_RW(logiodone
, XFS_ERRTAG_IODONE_IOERR
);
158 XFS_ERRORTAG_ATTR_RW(stratread
, XFS_ERRTAG_STRATREAD_IOERR
);
159 XFS_ERRORTAG_ATTR_RW(stratcmpl
, XFS_ERRTAG_STRATCMPL_IOERR
);
160 XFS_ERRORTAG_ATTR_RW(diowrite
, XFS_ERRTAG_DIOWRITE_IOERR
);
161 XFS_ERRORTAG_ATTR_RW(bmapifmt
, XFS_ERRTAG_BMAPIFORMAT
);
162 XFS_ERRORTAG_ATTR_RW(free_extent
, XFS_ERRTAG_FREE_EXTENT
);
163 XFS_ERRORTAG_ATTR_RW(rmap_finish_one
, XFS_ERRTAG_RMAP_FINISH_ONE
);
164 XFS_ERRORTAG_ATTR_RW(refcount_continue_update
, XFS_ERRTAG_REFCOUNT_CONTINUE_UPDATE
);
165 XFS_ERRORTAG_ATTR_RW(refcount_finish_one
, XFS_ERRTAG_REFCOUNT_FINISH_ONE
);
166 XFS_ERRORTAG_ATTR_RW(bmap_finish_one
, XFS_ERRTAG_BMAP_FINISH_ONE
);
167 XFS_ERRORTAG_ATTR_RW(ag_resv_critical
, XFS_ERRTAG_AG_RESV_CRITICAL
);
168 XFS_ERRORTAG_ATTR_RW(log_bad_crc
, XFS_ERRTAG_LOG_BAD_CRC
);
169 XFS_ERRORTAG_ATTR_RW(log_item_pin
, XFS_ERRTAG_LOG_ITEM_PIN
);
170 XFS_ERRORTAG_ATTR_RW(buf_lru_ref
, XFS_ERRTAG_BUF_LRU_REF
);
171 XFS_ERRORTAG_ATTR_RW(force_repair
, XFS_ERRTAG_FORCE_SCRUB_REPAIR
);
172 XFS_ERRORTAG_ATTR_RW(bad_summary
, XFS_ERRTAG_FORCE_SUMMARY_RECALC
);
173 XFS_ERRORTAG_ATTR_RW(iunlink_fallback
, XFS_ERRTAG_IUNLINK_FALLBACK
);
174 XFS_ERRORTAG_ATTR_RW(buf_ioerror
, XFS_ERRTAG_BUF_IOERROR
);
175 XFS_ERRORTAG_ATTR_RW(reduce_max_iextents
, XFS_ERRTAG_REDUCE_MAX_IEXTENTS
);
176 XFS_ERRORTAG_ATTR_RW(bmap_alloc_minlen_extent
, XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT
);
177 XFS_ERRORTAG_ATTR_RW(ag_resv_fail
, XFS_ERRTAG_AG_RESV_FAIL
);
178 XFS_ERRORTAG_ATTR_RW(larp
, XFS_ERRTAG_LARP
);
179 XFS_ERRORTAG_ATTR_RW(da_leaf_split
, XFS_ERRTAG_DA_LEAF_SPLIT
);
180 XFS_ERRORTAG_ATTR_RW(attr_leaf_to_node
, XFS_ERRTAG_ATTR_LEAF_TO_NODE
);
181 XFS_ERRORTAG_ATTR_RW(wb_delay_ms
, XFS_ERRTAG_WB_DELAY_MS
);
182 XFS_ERRORTAG_ATTR_RW(write_delay_ms
, XFS_ERRTAG_WRITE_DELAY_MS
);
183 XFS_ERRORTAG_ATTR_RW(exchmaps_finish_one
, XFS_ERRTAG_EXCHMAPS_FINISH_ONE
);
185 static struct attribute
*xfs_errortag_attrs
[] = {
186 XFS_ERRORTAG_ATTR_LIST(noerror
),
187 XFS_ERRORTAG_ATTR_LIST(iflush1
),
188 XFS_ERRORTAG_ATTR_LIST(iflush2
),
189 XFS_ERRORTAG_ATTR_LIST(iflush3
),
190 XFS_ERRORTAG_ATTR_LIST(iflush4
),
191 XFS_ERRORTAG_ATTR_LIST(iflush5
),
192 XFS_ERRORTAG_ATTR_LIST(iflush6
),
193 XFS_ERRORTAG_ATTR_LIST(dareadbuf
),
194 XFS_ERRORTAG_ATTR_LIST(btree_chk_lblk
),
195 XFS_ERRORTAG_ATTR_LIST(btree_chk_sblk
),
196 XFS_ERRORTAG_ATTR_LIST(readagf
),
197 XFS_ERRORTAG_ATTR_LIST(readagi
),
198 XFS_ERRORTAG_ATTR_LIST(itobp
),
199 XFS_ERRORTAG_ATTR_LIST(iunlink
),
200 XFS_ERRORTAG_ATTR_LIST(iunlinkrm
),
201 XFS_ERRORTAG_ATTR_LIST(dirinovalid
),
202 XFS_ERRORTAG_ATTR_LIST(bulkstat
),
203 XFS_ERRORTAG_ATTR_LIST(logiodone
),
204 XFS_ERRORTAG_ATTR_LIST(stratread
),
205 XFS_ERRORTAG_ATTR_LIST(stratcmpl
),
206 XFS_ERRORTAG_ATTR_LIST(diowrite
),
207 XFS_ERRORTAG_ATTR_LIST(bmapifmt
),
208 XFS_ERRORTAG_ATTR_LIST(free_extent
),
209 XFS_ERRORTAG_ATTR_LIST(rmap_finish_one
),
210 XFS_ERRORTAG_ATTR_LIST(refcount_continue_update
),
211 XFS_ERRORTAG_ATTR_LIST(refcount_finish_one
),
212 XFS_ERRORTAG_ATTR_LIST(bmap_finish_one
),
213 XFS_ERRORTAG_ATTR_LIST(ag_resv_critical
),
214 XFS_ERRORTAG_ATTR_LIST(log_bad_crc
),
215 XFS_ERRORTAG_ATTR_LIST(log_item_pin
),
216 XFS_ERRORTAG_ATTR_LIST(buf_lru_ref
),
217 XFS_ERRORTAG_ATTR_LIST(force_repair
),
218 XFS_ERRORTAG_ATTR_LIST(bad_summary
),
219 XFS_ERRORTAG_ATTR_LIST(iunlink_fallback
),
220 XFS_ERRORTAG_ATTR_LIST(buf_ioerror
),
221 XFS_ERRORTAG_ATTR_LIST(reduce_max_iextents
),
222 XFS_ERRORTAG_ATTR_LIST(bmap_alloc_minlen_extent
),
223 XFS_ERRORTAG_ATTR_LIST(ag_resv_fail
),
224 XFS_ERRORTAG_ATTR_LIST(larp
),
225 XFS_ERRORTAG_ATTR_LIST(da_leaf_split
),
226 XFS_ERRORTAG_ATTR_LIST(attr_leaf_to_node
),
227 XFS_ERRORTAG_ATTR_LIST(wb_delay_ms
),
228 XFS_ERRORTAG_ATTR_LIST(write_delay_ms
),
229 XFS_ERRORTAG_ATTR_LIST(exchmaps_finish_one
),
232 ATTRIBUTE_GROUPS(xfs_errortag
);
234 static const struct kobj_type xfs_errortag_ktype
= {
235 .release
= xfs_sysfs_release
,
236 .sysfs_ops
= &xfs_errortag_sysfs_ops
,
237 .default_groups
= xfs_errortag_groups
,
242 struct xfs_mount
*mp
)
246 mp
->m_errortag
= kzalloc(sizeof(unsigned int) * XFS_ERRTAG_MAX
,
247 GFP_KERNEL
| __GFP_RETRY_MAYFAIL
);
251 ret
= xfs_sysfs_init(&mp
->m_errortag_kobj
, &xfs_errortag_ktype
,
252 &mp
->m_kobj
, "errortag");
254 kfree(mp
->m_errortag
);
260 struct xfs_mount
*mp
)
262 xfs_sysfs_del(&mp
->m_errortag_kobj
);
263 kfree(mp
->m_errortag
);
268 unsigned int error_tag
)
270 if (error_tag
>= XFS_ERRTAG_MAX
)
273 /* Error out removed injection types */
274 if (error_tag
== XFS_ERRTAG_DROP_WRITES
)
280 xfs_errortag_enabled(
281 struct xfs_mount
*mp
,
286 if (!xfs_errortag_valid(tag
))
289 return mp
->m_errortag
[tag
] != 0;
294 struct xfs_mount
*mp
,
295 const char *expression
,
298 unsigned int error_tag
)
300 unsigned int randfactor
;
303 * To be able to use error injection anywhere, we need to ensure error
304 * injection mechanism is already initialized.
306 * Code paths like I/O completion can be called before the
307 * initialization is complete, but be able to inject errors in such
308 * places is still useful.
313 if (!xfs_errortag_valid(error_tag
))
316 randfactor
= mp
->m_errortag
[error_tag
];
317 if (!randfactor
|| get_random_u32_below(randfactor
))
320 xfs_warn_ratelimited(mp
,
321 "Injecting error (%s) at file %s, line %d, on filesystem \"%s\"",
322 expression
, file
, line
, mp
->m_super
->s_id
);
328 struct xfs_mount
*mp
,
329 unsigned int error_tag
)
331 if (!xfs_errortag_valid(error_tag
))
334 return mp
->m_errortag
[error_tag
];
339 struct xfs_mount
*mp
,
340 unsigned int error_tag
,
341 unsigned int tag_value
)
343 if (!xfs_errortag_valid(error_tag
))
346 mp
->m_errortag
[error_tag
] = tag_value
;
352 struct xfs_mount
*mp
,
353 unsigned int error_tag
)
355 BUILD_BUG_ON(ARRAY_SIZE(xfs_errortag_random_default
) != XFS_ERRTAG_MAX
);
357 if (!xfs_errortag_valid(error_tag
))
360 return xfs_errortag_set(mp
, error_tag
,
361 xfs_errortag_random_default
[error_tag
]);
365 xfs_errortag_clearall(
366 struct xfs_mount
*mp
)
368 memset(mp
->m_errortag
, 0, sizeof(unsigned int) * XFS_ERRTAG_MAX
);
377 struct xfs_mount
*mp
,
378 const char *filename
,
380 xfs_failaddr_t failaddr
)
382 if (level
<= xfs_error_level
) {
383 xfs_alert_tag(mp
, XFS_PTAG_ERROR_REPORT
,
384 "Internal error %s at line %d of file %s. Caller %pS",
385 tag
, linenum
, filename
, failaddr
);
392 xfs_corruption_error(
395 struct xfs_mount
*mp
,
398 const char *filename
,
400 xfs_failaddr_t failaddr
)
402 if (buf
&& level
<= xfs_error_level
)
403 xfs_hex_dump(buf
, bufsize
);
404 xfs_error_report(tag
, level
, mp
, filename
, linenum
, failaddr
);
405 xfs_alert(mp
, "Corruption detected. Unmount and run xfs_repair");
409 * Complain about the kinds of metadata corruption that we can't detect from a
410 * verifier, such as incorrect inter-block relationship data. Does not set
413 * Call xfs_buf_mark_corrupt, not this function.
416 xfs_buf_corruption_error(
420 struct xfs_mount
*mp
= bp
->b_mount
;
422 xfs_alert_tag(mp
, XFS_PTAG_VERIFIER_ERROR
,
423 "Metadata corruption detected at %pS, %s block 0x%llx",
424 fa
, bp
->b_ops
->name
, xfs_buf_daddr(bp
));
426 xfs_alert(mp
, "Unmount and run xfs_repair");
428 if (xfs_error_level
>= XFS_ERRLEVEL_HIGH
)
433 * Warnings specifically for verifier errors. Differentiate CRC vs. invalid
434 * values, and omit the stack trace unless the error level is tuned high.
437 xfs_buf_verifier_error(
443 xfs_failaddr_t failaddr
)
445 struct xfs_mount
*mp
= bp
->b_mount
;
449 fa
= failaddr
? failaddr
: __return_address
;
450 __xfs_buf_ioerror(bp
, error
, fa
);
452 xfs_alert_tag(mp
, XFS_PTAG_VERIFIER_ERROR
,
453 "Metadata %s detected at %pS, %s block 0x%llx %s",
454 bp
->b_error
== -EFSBADCRC
? "CRC error" : "corruption",
455 fa
, bp
->b_ops
->name
, xfs_buf_daddr(bp
), name
);
457 xfs_alert(mp
, "Unmount and run xfs_repair");
459 if (xfs_error_level
>= XFS_ERRLEVEL_LOW
) {
460 sz
= min_t(size_t, XFS_CORRUPTION_DUMP_LEN
, bufsz
);
461 xfs_alert(mp
, "First %d bytes of corrupted metadata buffer:",
463 xfs_hex_dump(buf
, sz
);
466 if (xfs_error_level
>= XFS_ERRLEVEL_HIGH
)
471 * Warnings specifically for verifier errors. Differentiate CRC vs. invalid
472 * values, and omit the stack trace unless the error level is tuned high.
478 xfs_failaddr_t failaddr
)
480 return xfs_buf_verifier_error(bp
, error
, "", xfs_buf_offset(bp
, 0),
481 XFS_CORRUPTION_DUMP_LEN
, failaddr
);
485 * Warnings for inode corruption problems. Don't bother with the stack
486 * trace unless the error level is turned up high.
489 xfs_inode_verifier_error(
490 struct xfs_inode
*ip
,
495 xfs_failaddr_t failaddr
)
497 struct xfs_mount
*mp
= ip
->i_mount
;
501 fa
= failaddr
? failaddr
: __return_address
;
503 xfs_alert(mp
, "Metadata %s detected at %pS, inode 0x%llx %s",
504 error
== -EFSBADCRC
? "CRC error" : "corruption",
505 fa
, ip
->i_ino
, name
);
507 xfs_alert(mp
, "Unmount and run xfs_repair");
509 if (buf
&& xfs_error_level
>= XFS_ERRLEVEL_LOW
) {
510 sz
= min_t(size_t, XFS_CORRUPTION_DUMP_LEN
, bufsz
);
511 xfs_alert(mp
, "First %d bytes of corrupted metadata buffer:",
513 xfs_hex_dump(buf
, sz
);
516 if (xfs_error_level
>= XFS_ERRLEVEL_HIGH
)