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
,
56 struct xfs_errortag_attr
{
57 struct attribute attr
;
61 static inline struct xfs_errortag_attr
*
62 to_attr(struct attribute
*attr
)
64 return container_of(attr
, struct xfs_errortag_attr
, attr
);
67 static inline struct xfs_mount
*
68 to_mp(struct kobject
*kobject
)
70 struct xfs_kobj
*kobj
= to_kobj(kobject
);
72 return container_of(kobj
, struct xfs_mount
, m_errortag_kobj
);
76 xfs_errortag_attr_store(
77 struct kobject
*kobject
,
78 struct attribute
*attr
,
82 struct xfs_mount
*mp
= to_mp(kobject
);
83 struct xfs_errortag_attr
*xfs_attr
= to_attr(attr
);
87 if (strcmp(buf
, "default") == 0) {
88 val
= xfs_errortag_random_default
[xfs_attr
->tag
];
90 ret
= kstrtouint(buf
, 0, &val
);
95 ret
= xfs_errortag_set(mp
, xfs_attr
->tag
, val
);
102 xfs_errortag_attr_show(
103 struct kobject
*kobject
,
104 struct attribute
*attr
,
107 struct xfs_mount
*mp
= to_mp(kobject
);
108 struct xfs_errortag_attr
*xfs_attr
= to_attr(attr
);
110 return snprintf(buf
, PAGE_SIZE
, "%u\n",
111 xfs_errortag_get(mp
, xfs_attr
->tag
));
114 static const struct sysfs_ops xfs_errortag_sysfs_ops
= {
115 .show
= xfs_errortag_attr_show
,
116 .store
= xfs_errortag_attr_store
,
119 #define XFS_ERRORTAG_ATTR_RW(_name, _tag) \
120 static struct xfs_errortag_attr xfs_errortag_attr_##_name = { \
121 .attr = {.name = __stringify(_name), \
122 .mode = VERIFY_OCTAL_PERMISSIONS(S_IWUSR | S_IRUGO) }, \
126 #define XFS_ERRORTAG_ATTR_LIST(_name) &xfs_errortag_attr_##_name.attr
128 XFS_ERRORTAG_ATTR_RW(noerror
, XFS_ERRTAG_NOERROR
);
129 XFS_ERRORTAG_ATTR_RW(iflush1
, XFS_ERRTAG_IFLUSH_1
);
130 XFS_ERRORTAG_ATTR_RW(iflush2
, XFS_ERRTAG_IFLUSH_2
);
131 XFS_ERRORTAG_ATTR_RW(iflush3
, XFS_ERRTAG_IFLUSH_3
);
132 XFS_ERRORTAG_ATTR_RW(iflush4
, XFS_ERRTAG_IFLUSH_4
);
133 XFS_ERRORTAG_ATTR_RW(iflush5
, XFS_ERRTAG_IFLUSH_5
);
134 XFS_ERRORTAG_ATTR_RW(iflush6
, XFS_ERRTAG_IFLUSH_6
);
135 XFS_ERRORTAG_ATTR_RW(dareadbuf
, XFS_ERRTAG_DA_READ_BUF
);
136 XFS_ERRORTAG_ATTR_RW(btree_chk_lblk
, XFS_ERRTAG_BTREE_CHECK_LBLOCK
);
137 XFS_ERRORTAG_ATTR_RW(btree_chk_sblk
, XFS_ERRTAG_BTREE_CHECK_SBLOCK
);
138 XFS_ERRORTAG_ATTR_RW(readagf
, XFS_ERRTAG_ALLOC_READ_AGF
);
139 XFS_ERRORTAG_ATTR_RW(readagi
, XFS_ERRTAG_IALLOC_READ_AGI
);
140 XFS_ERRORTAG_ATTR_RW(itobp
, XFS_ERRTAG_ITOBP_INOTOBP
);
141 XFS_ERRORTAG_ATTR_RW(iunlink
, XFS_ERRTAG_IUNLINK
);
142 XFS_ERRORTAG_ATTR_RW(iunlinkrm
, XFS_ERRTAG_IUNLINK_REMOVE
);
143 XFS_ERRORTAG_ATTR_RW(dirinovalid
, XFS_ERRTAG_DIR_INO_VALIDATE
);
144 XFS_ERRORTAG_ATTR_RW(bulkstat
, XFS_ERRTAG_BULKSTAT_READ_CHUNK
);
145 XFS_ERRORTAG_ATTR_RW(logiodone
, XFS_ERRTAG_IODONE_IOERR
);
146 XFS_ERRORTAG_ATTR_RW(stratread
, XFS_ERRTAG_STRATREAD_IOERR
);
147 XFS_ERRORTAG_ATTR_RW(stratcmpl
, XFS_ERRTAG_STRATCMPL_IOERR
);
148 XFS_ERRORTAG_ATTR_RW(diowrite
, XFS_ERRTAG_DIOWRITE_IOERR
);
149 XFS_ERRORTAG_ATTR_RW(bmapifmt
, XFS_ERRTAG_BMAPIFORMAT
);
150 XFS_ERRORTAG_ATTR_RW(free_extent
, XFS_ERRTAG_FREE_EXTENT
);
151 XFS_ERRORTAG_ATTR_RW(rmap_finish_one
, XFS_ERRTAG_RMAP_FINISH_ONE
);
152 XFS_ERRORTAG_ATTR_RW(refcount_continue_update
, XFS_ERRTAG_REFCOUNT_CONTINUE_UPDATE
);
153 XFS_ERRORTAG_ATTR_RW(refcount_finish_one
, XFS_ERRTAG_REFCOUNT_FINISH_ONE
);
154 XFS_ERRORTAG_ATTR_RW(bmap_finish_one
, XFS_ERRTAG_BMAP_FINISH_ONE
);
155 XFS_ERRORTAG_ATTR_RW(ag_resv_critical
, XFS_ERRTAG_AG_RESV_CRITICAL
);
156 XFS_ERRORTAG_ATTR_RW(drop_writes
, XFS_ERRTAG_DROP_WRITES
);
157 XFS_ERRORTAG_ATTR_RW(log_bad_crc
, XFS_ERRTAG_LOG_BAD_CRC
);
158 XFS_ERRORTAG_ATTR_RW(log_item_pin
, XFS_ERRTAG_LOG_ITEM_PIN
);
159 XFS_ERRORTAG_ATTR_RW(buf_lru_ref
, XFS_ERRTAG_BUF_LRU_REF
);
160 XFS_ERRORTAG_ATTR_RW(force_repair
, XFS_ERRTAG_FORCE_SCRUB_REPAIR
);
161 XFS_ERRORTAG_ATTR_RW(bad_summary
, XFS_ERRTAG_FORCE_SUMMARY_RECALC
);
163 static struct attribute
*xfs_errortag_attrs
[] = {
164 XFS_ERRORTAG_ATTR_LIST(noerror
),
165 XFS_ERRORTAG_ATTR_LIST(iflush1
),
166 XFS_ERRORTAG_ATTR_LIST(iflush2
),
167 XFS_ERRORTAG_ATTR_LIST(iflush3
),
168 XFS_ERRORTAG_ATTR_LIST(iflush4
),
169 XFS_ERRORTAG_ATTR_LIST(iflush5
),
170 XFS_ERRORTAG_ATTR_LIST(iflush6
),
171 XFS_ERRORTAG_ATTR_LIST(dareadbuf
),
172 XFS_ERRORTAG_ATTR_LIST(btree_chk_lblk
),
173 XFS_ERRORTAG_ATTR_LIST(btree_chk_sblk
),
174 XFS_ERRORTAG_ATTR_LIST(readagf
),
175 XFS_ERRORTAG_ATTR_LIST(readagi
),
176 XFS_ERRORTAG_ATTR_LIST(itobp
),
177 XFS_ERRORTAG_ATTR_LIST(iunlink
),
178 XFS_ERRORTAG_ATTR_LIST(iunlinkrm
),
179 XFS_ERRORTAG_ATTR_LIST(dirinovalid
),
180 XFS_ERRORTAG_ATTR_LIST(bulkstat
),
181 XFS_ERRORTAG_ATTR_LIST(logiodone
),
182 XFS_ERRORTAG_ATTR_LIST(stratread
),
183 XFS_ERRORTAG_ATTR_LIST(stratcmpl
),
184 XFS_ERRORTAG_ATTR_LIST(diowrite
),
185 XFS_ERRORTAG_ATTR_LIST(bmapifmt
),
186 XFS_ERRORTAG_ATTR_LIST(free_extent
),
187 XFS_ERRORTAG_ATTR_LIST(rmap_finish_one
),
188 XFS_ERRORTAG_ATTR_LIST(refcount_continue_update
),
189 XFS_ERRORTAG_ATTR_LIST(refcount_finish_one
),
190 XFS_ERRORTAG_ATTR_LIST(bmap_finish_one
),
191 XFS_ERRORTAG_ATTR_LIST(ag_resv_critical
),
192 XFS_ERRORTAG_ATTR_LIST(drop_writes
),
193 XFS_ERRORTAG_ATTR_LIST(log_bad_crc
),
194 XFS_ERRORTAG_ATTR_LIST(log_item_pin
),
195 XFS_ERRORTAG_ATTR_LIST(buf_lru_ref
),
196 XFS_ERRORTAG_ATTR_LIST(force_repair
),
197 XFS_ERRORTAG_ATTR_LIST(bad_summary
),
201 static struct kobj_type xfs_errortag_ktype
= {
202 .release
= xfs_sysfs_release
,
203 .sysfs_ops
= &xfs_errortag_sysfs_ops
,
204 .default_attrs
= xfs_errortag_attrs
,
209 struct xfs_mount
*mp
)
211 mp
->m_errortag
= kmem_zalloc(sizeof(unsigned int) * XFS_ERRTAG_MAX
,
212 KM_SLEEP
| KM_MAYFAIL
);
216 return xfs_sysfs_init(&mp
->m_errortag_kobj
, &xfs_errortag_ktype
,
217 &mp
->m_kobj
, "errortag");
222 struct xfs_mount
*mp
)
224 xfs_sysfs_del(&mp
->m_errortag_kobj
);
225 kmem_free(mp
->m_errortag
);
230 struct xfs_mount
*mp
,
231 const char *expression
,
234 unsigned int error_tag
)
236 unsigned int randfactor
;
239 * To be able to use error injection anywhere, we need to ensure error
240 * injection mechanism is already initialized.
242 * Code paths like I/O completion can be called before the
243 * initialization is complete, but be able to inject errors in such
244 * places is still useful.
249 ASSERT(error_tag
< XFS_ERRTAG_MAX
);
250 randfactor
= mp
->m_errortag
[error_tag
];
251 if (!randfactor
|| prandom_u32() % randfactor
)
254 xfs_warn_ratelimited(mp
,
255 "Injecting error (%s) at file %s, line %d, on filesystem \"%s\"",
256 expression
, file
, line
, mp
->m_fsname
);
262 struct xfs_mount
*mp
,
263 unsigned int error_tag
)
265 if (error_tag
>= XFS_ERRTAG_MAX
)
268 return mp
->m_errortag
[error_tag
];
273 struct xfs_mount
*mp
,
274 unsigned int error_tag
,
275 unsigned int tag_value
)
277 if (error_tag
>= XFS_ERRTAG_MAX
)
280 mp
->m_errortag
[error_tag
] = tag_value
;
286 struct xfs_mount
*mp
,
287 unsigned int error_tag
)
289 if (error_tag
>= XFS_ERRTAG_MAX
)
292 return xfs_errortag_set(mp
, error_tag
,
293 xfs_errortag_random_default
[error_tag
]);
297 xfs_errortag_clearall(
298 struct xfs_mount
*mp
)
300 memset(mp
->m_errortag
, 0, sizeof(unsigned int) * XFS_ERRTAG_MAX
);
309 struct xfs_mount
*mp
,
310 const char *filename
,
312 xfs_failaddr_t failaddr
)
314 if (level
<= xfs_error_level
) {
315 xfs_alert_tag(mp
, XFS_PTAG_ERROR_REPORT
,
316 "Internal error %s at line %d of file %s. Caller %pS",
317 tag
, linenum
, filename
, failaddr
);
324 xfs_corruption_error(
327 struct xfs_mount
*mp
,
330 const char *filename
,
332 xfs_failaddr_t failaddr
)
334 if (level
<= xfs_error_level
)
335 xfs_hex_dump(buf
, bufsize
);
336 xfs_error_report(tag
, level
, mp
, filename
, linenum
, failaddr
);
337 xfs_alert(mp
, "Corruption detected. Unmount and run xfs_repair");
341 * Warnings specifically for verifier errors. Differentiate CRC vs. invalid
342 * values, and omit the stack trace unless the error level is tuned high.
345 xfs_buf_verifier_error(
351 xfs_failaddr_t failaddr
)
353 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
357 fa
= failaddr
? failaddr
: __return_address
;
358 __xfs_buf_ioerror(bp
, error
, fa
);
360 xfs_alert(mp
, "Metadata %s detected at %pS, %s block 0x%llx %s",
361 bp
->b_error
== -EFSBADCRC
? "CRC error" : "corruption",
362 fa
, bp
->b_ops
->name
, bp
->b_bn
, name
);
364 xfs_alert(mp
, "Unmount and run xfs_repair");
366 if (xfs_error_level
>= XFS_ERRLEVEL_LOW
) {
367 sz
= min_t(size_t, XFS_CORRUPTION_DUMP_LEN
, bufsz
);
368 xfs_alert(mp
, "First %d bytes of corrupted metadata buffer:",
370 xfs_hex_dump(buf
, sz
);
373 if (xfs_error_level
>= XFS_ERRLEVEL_HIGH
)
378 * Warnings specifically for verifier errors. Differentiate CRC vs. invalid
379 * values, and omit the stack trace unless the error level is tuned high.
385 xfs_failaddr_t failaddr
)
387 return xfs_buf_verifier_error(bp
, error
, "", xfs_buf_offset(bp
, 0),
388 XFS_CORRUPTION_DUMP_LEN
, failaddr
);
392 * Warnings for inode corruption problems. Don't bother with the stack
393 * trace unless the error level is turned up high.
396 xfs_inode_verifier_error(
397 struct xfs_inode
*ip
,
402 xfs_failaddr_t failaddr
)
404 struct xfs_mount
*mp
= ip
->i_mount
;
408 fa
= failaddr
? failaddr
: __return_address
;
410 xfs_alert(mp
, "Metadata %s detected at %pS, inode 0x%llx %s",
411 error
== -EFSBADCRC
? "CRC error" : "corruption",
412 fa
, ip
->i_ino
, name
);
414 xfs_alert(mp
, "Unmount and run xfs_repair");
416 if (buf
&& xfs_error_level
>= XFS_ERRLEVEL_LOW
) {
417 sz
= min_t(size_t, XFS_CORRUPTION_DUMP_LEN
, bufsz
);
418 xfs_alert(mp
, "First %d bytes of corrupted metadata buffer:",
420 xfs_hex_dump(buf
, sz
);
423 if (xfs_error_level
>= XFS_ERRLEVEL_HIGH
)