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