]>
Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
7b718769 NS |
2 | * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc. |
3 | * All Rights Reserved. | |
1da177e4 | 4 | * |
7b718769 NS |
5 | * This program is free software; you can redistribute it and/or |
6 | * modify it under the terms of the GNU General Public License as | |
1da177e4 LT |
7 | * published by the Free Software Foundation. |
8 | * | |
7b718769 NS |
9 | * This program is distributed in the hope that it would be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
1da177e4 | 13 | * |
7b718769 NS |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write the Free Software Foundation, | |
16 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
1da177e4 | 17 | */ |
1da177e4 | 18 | #include "xfs.h" |
a4fbe6ab | 19 | #include "xfs_format.h" |
a844f451 | 20 | #include "xfs_fs.h" |
239880ef DC |
21 | #include "xfs_log_format.h" |
22 | #include "xfs_trans_resv.h" | |
1da177e4 | 23 | #include "xfs_mount.h" |
1da177e4 | 24 | #include "xfs_error.h" |
c6840101 | 25 | #include "xfs_sysfs.h" |
1da177e4 LT |
26 | |
27 | #ifdef DEBUG | |
28 | ||
31965ef3 DW |
29 | static unsigned int xfs_errortag_random_default[] = { |
30 | XFS_RANDOM_DEFAULT, | |
31 | XFS_RANDOM_IFLUSH_1, | |
32 | XFS_RANDOM_IFLUSH_2, | |
33 | XFS_RANDOM_IFLUSH_3, | |
34 | XFS_RANDOM_IFLUSH_4, | |
35 | XFS_RANDOM_IFLUSH_5, | |
36 | XFS_RANDOM_IFLUSH_6, | |
37 | XFS_RANDOM_DA_READ_BUF, | |
38 | XFS_RANDOM_BTREE_CHECK_LBLOCK, | |
39 | XFS_RANDOM_BTREE_CHECK_SBLOCK, | |
40 | XFS_RANDOM_ALLOC_READ_AGF, | |
41 | XFS_RANDOM_IALLOC_READ_AGI, | |
42 | XFS_RANDOM_ITOBP_INOTOBP, | |
43 | XFS_RANDOM_IUNLINK, | |
44 | XFS_RANDOM_IUNLINK_REMOVE, | |
45 | XFS_RANDOM_DIR_INO_VALIDATE, | |
46 | XFS_RANDOM_BULKSTAT_READ_CHUNK, | |
47 | XFS_RANDOM_IODONE_IOERR, | |
48 | XFS_RANDOM_STRATREAD_IOERR, | |
49 | XFS_RANDOM_STRATCMPL_IOERR, | |
50 | XFS_RANDOM_DIOWRITE_IOERR, | |
51 | XFS_RANDOM_BMAPIFORMAT, | |
52 | XFS_RANDOM_FREE_EXTENT, | |
53 | XFS_RANDOM_RMAP_FINISH_ONE, | |
54 | XFS_RANDOM_REFCOUNT_CONTINUE_UPDATE, | |
55 | XFS_RANDOM_REFCOUNT_FINISH_ONE, | |
56 | XFS_RANDOM_BMAP_FINISH_ONE, | |
57 | XFS_RANDOM_AG_RESV_CRITICAL, | |
f8c47250 | 58 | XFS_RANDOM_DROP_WRITES, |
3e88a007 | 59 | XFS_RANDOM_LOG_BAD_CRC, |
7f4d01f3 | 60 | XFS_RANDOM_LOG_ITEM_PIN, |
31965ef3 | 61 | }; |
1da177e4 | 62 | |
c6840101 DW |
63 | struct xfs_errortag_attr { |
64 | struct attribute attr; | |
65 | unsigned int tag; | |
66 | }; | |
67 | ||
68 | static inline struct xfs_errortag_attr * | |
69 | to_attr(struct attribute *attr) | |
70 | { | |
71 | return container_of(attr, struct xfs_errortag_attr, attr); | |
72 | } | |
73 | ||
74 | static inline struct xfs_mount * | |
75 | to_mp(struct kobject *kobject) | |
76 | { | |
77 | struct xfs_kobj *kobj = to_kobj(kobject); | |
78 | ||
79 | return container_of(kobj, struct xfs_mount, m_errortag_kobj); | |
80 | } | |
81 | ||
82 | STATIC ssize_t | |
83 | xfs_errortag_attr_store( | |
84 | struct kobject *kobject, | |
85 | struct attribute *attr, | |
86 | const char *buf, | |
87 | size_t count) | |
88 | { | |
89 | struct xfs_mount *mp = to_mp(kobject); | |
90 | struct xfs_errortag_attr *xfs_attr = to_attr(attr); | |
91 | int ret; | |
92 | unsigned int val; | |
93 | ||
94 | if (strcmp(buf, "default") == 0) { | |
95 | val = xfs_errortag_random_default[xfs_attr->tag]; | |
96 | } else { | |
97 | ret = kstrtouint(buf, 0, &val); | |
98 | if (ret) | |
99 | return ret; | |
100 | } | |
101 | ||
102 | ret = xfs_errortag_set(mp, xfs_attr->tag, val); | |
103 | if (ret) | |
104 | return ret; | |
105 | return count; | |
106 | } | |
107 | ||
108 | STATIC ssize_t | |
109 | xfs_errortag_attr_show( | |
110 | struct kobject *kobject, | |
111 | struct attribute *attr, | |
112 | char *buf) | |
113 | { | |
114 | struct xfs_mount *mp = to_mp(kobject); | |
115 | struct xfs_errortag_attr *xfs_attr = to_attr(attr); | |
116 | ||
117 | return snprintf(buf, PAGE_SIZE, "%u\n", | |
118 | xfs_errortag_get(mp, xfs_attr->tag)); | |
119 | } | |
120 | ||
121 | static const struct sysfs_ops xfs_errortag_sysfs_ops = { | |
122 | .show = xfs_errortag_attr_show, | |
123 | .store = xfs_errortag_attr_store, | |
124 | }; | |
125 | ||
126 | #define XFS_ERRORTAG_ATTR_RW(_name, _tag) \ | |
127 | static struct xfs_errortag_attr xfs_errortag_attr_##_name = { \ | |
128 | .attr = {.name = __stringify(_name), \ | |
129 | .mode = VERIFY_OCTAL_PERMISSIONS(S_IWUSR | S_IRUGO) }, \ | |
130 | .tag = (_tag), \ | |
131 | } | |
132 | ||
133 | #define XFS_ERRORTAG_ATTR_LIST(_name) &xfs_errortag_attr_##_name.attr | |
134 | ||
135 | XFS_ERRORTAG_ATTR_RW(noerror, XFS_ERRTAG_NOERROR); | |
136 | XFS_ERRORTAG_ATTR_RW(iflush1, XFS_ERRTAG_IFLUSH_1); | |
137 | XFS_ERRORTAG_ATTR_RW(iflush2, XFS_ERRTAG_IFLUSH_2); | |
138 | XFS_ERRORTAG_ATTR_RW(iflush3, XFS_ERRTAG_IFLUSH_3); | |
139 | XFS_ERRORTAG_ATTR_RW(iflush4, XFS_ERRTAG_IFLUSH_4); | |
140 | XFS_ERRORTAG_ATTR_RW(iflush5, XFS_ERRTAG_IFLUSH_5); | |
141 | XFS_ERRORTAG_ATTR_RW(iflush6, XFS_ERRTAG_IFLUSH_6); | |
142 | XFS_ERRORTAG_ATTR_RW(dareadbuf, XFS_ERRTAG_DA_READ_BUF); | |
143 | XFS_ERRORTAG_ATTR_RW(btree_chk_lblk, XFS_ERRTAG_BTREE_CHECK_LBLOCK); | |
144 | XFS_ERRORTAG_ATTR_RW(btree_chk_sblk, XFS_ERRTAG_BTREE_CHECK_SBLOCK); | |
145 | XFS_ERRORTAG_ATTR_RW(readagf, XFS_ERRTAG_ALLOC_READ_AGF); | |
146 | XFS_ERRORTAG_ATTR_RW(readagi, XFS_ERRTAG_IALLOC_READ_AGI); | |
147 | XFS_ERRORTAG_ATTR_RW(itobp, XFS_ERRTAG_ITOBP_INOTOBP); | |
148 | XFS_ERRORTAG_ATTR_RW(iunlink, XFS_ERRTAG_IUNLINK); | |
149 | XFS_ERRORTAG_ATTR_RW(iunlinkrm, XFS_ERRTAG_IUNLINK_REMOVE); | |
150 | XFS_ERRORTAG_ATTR_RW(dirinovalid, XFS_ERRTAG_DIR_INO_VALIDATE); | |
151 | XFS_ERRORTAG_ATTR_RW(bulkstat, XFS_ERRTAG_BULKSTAT_READ_CHUNK); | |
152 | XFS_ERRORTAG_ATTR_RW(logiodone, XFS_ERRTAG_IODONE_IOERR); | |
153 | XFS_ERRORTAG_ATTR_RW(stratread, XFS_ERRTAG_STRATREAD_IOERR); | |
154 | XFS_ERRORTAG_ATTR_RW(stratcmpl, XFS_ERRTAG_STRATCMPL_IOERR); | |
155 | XFS_ERRORTAG_ATTR_RW(diowrite, XFS_ERRTAG_DIOWRITE_IOERR); | |
156 | XFS_ERRORTAG_ATTR_RW(bmapifmt, XFS_ERRTAG_BMAPIFORMAT); | |
157 | XFS_ERRORTAG_ATTR_RW(free_extent, XFS_ERRTAG_FREE_EXTENT); | |
158 | XFS_ERRORTAG_ATTR_RW(rmap_finish_one, XFS_ERRTAG_RMAP_FINISH_ONE); | |
159 | XFS_ERRORTAG_ATTR_RW(refcount_continue_update, XFS_ERRTAG_REFCOUNT_CONTINUE_UPDATE); | |
160 | XFS_ERRORTAG_ATTR_RW(refcount_finish_one, XFS_ERRTAG_REFCOUNT_FINISH_ONE); | |
161 | XFS_ERRORTAG_ATTR_RW(bmap_finish_one, XFS_ERRTAG_BMAP_FINISH_ONE); | |
162 | XFS_ERRORTAG_ATTR_RW(ag_resv_critical, XFS_ERRTAG_AG_RESV_CRITICAL); | |
f8c47250 | 163 | XFS_ERRORTAG_ATTR_RW(drop_writes, XFS_ERRTAG_DROP_WRITES); |
3e88a007 | 164 | XFS_ERRORTAG_ATTR_RW(log_bad_crc, XFS_ERRTAG_LOG_BAD_CRC); |
7f4d01f3 | 165 | XFS_ERRORTAG_ATTR_RW(log_item_pin, XFS_ERRTAG_LOG_ITEM_PIN); |
c6840101 DW |
166 | |
167 | static struct attribute *xfs_errortag_attrs[] = { | |
168 | XFS_ERRORTAG_ATTR_LIST(noerror), | |
169 | XFS_ERRORTAG_ATTR_LIST(iflush1), | |
170 | XFS_ERRORTAG_ATTR_LIST(iflush2), | |
171 | XFS_ERRORTAG_ATTR_LIST(iflush3), | |
172 | XFS_ERRORTAG_ATTR_LIST(iflush4), | |
173 | XFS_ERRORTAG_ATTR_LIST(iflush5), | |
174 | XFS_ERRORTAG_ATTR_LIST(iflush6), | |
175 | XFS_ERRORTAG_ATTR_LIST(dareadbuf), | |
176 | XFS_ERRORTAG_ATTR_LIST(btree_chk_lblk), | |
177 | XFS_ERRORTAG_ATTR_LIST(btree_chk_sblk), | |
178 | XFS_ERRORTAG_ATTR_LIST(readagf), | |
179 | XFS_ERRORTAG_ATTR_LIST(readagi), | |
180 | XFS_ERRORTAG_ATTR_LIST(itobp), | |
181 | XFS_ERRORTAG_ATTR_LIST(iunlink), | |
182 | XFS_ERRORTAG_ATTR_LIST(iunlinkrm), | |
183 | XFS_ERRORTAG_ATTR_LIST(dirinovalid), | |
184 | XFS_ERRORTAG_ATTR_LIST(bulkstat), | |
185 | XFS_ERRORTAG_ATTR_LIST(logiodone), | |
186 | XFS_ERRORTAG_ATTR_LIST(stratread), | |
187 | XFS_ERRORTAG_ATTR_LIST(stratcmpl), | |
188 | XFS_ERRORTAG_ATTR_LIST(diowrite), | |
189 | XFS_ERRORTAG_ATTR_LIST(bmapifmt), | |
190 | XFS_ERRORTAG_ATTR_LIST(free_extent), | |
191 | XFS_ERRORTAG_ATTR_LIST(rmap_finish_one), | |
192 | XFS_ERRORTAG_ATTR_LIST(refcount_continue_update), | |
193 | XFS_ERRORTAG_ATTR_LIST(refcount_finish_one), | |
194 | XFS_ERRORTAG_ATTR_LIST(bmap_finish_one), | |
195 | XFS_ERRORTAG_ATTR_LIST(ag_resv_critical), | |
f8c47250 | 196 | XFS_ERRORTAG_ATTR_LIST(drop_writes), |
3e88a007 | 197 | XFS_ERRORTAG_ATTR_LIST(log_bad_crc), |
7f4d01f3 | 198 | XFS_ERRORTAG_ATTR_LIST(log_item_pin), |
c6840101 DW |
199 | NULL, |
200 | }; | |
201 | ||
202 | struct kobj_type xfs_errortag_ktype = { | |
203 | .release = xfs_sysfs_release, | |
204 | .sysfs_ops = &xfs_errortag_sysfs_ops, | |
205 | .default_attrs = xfs_errortag_attrs, | |
206 | }; | |
207 | ||
1da177e4 | 208 | int |
31965ef3 DW |
209 | xfs_errortag_init( |
210 | struct xfs_mount *mp) | |
1da177e4 | 211 | { |
31965ef3 DW |
212 | mp->m_errortag = kmem_zalloc(sizeof(unsigned int) * XFS_ERRTAG_MAX, |
213 | KM_SLEEP | KM_MAYFAIL); | |
214 | if (!mp->m_errortag) | |
215 | return -ENOMEM; | |
c6840101 DW |
216 | |
217 | return xfs_sysfs_init(&mp->m_errortag_kobj, &xfs_errortag_ktype, | |
218 | &mp->m_kobj, "errortag"); | |
31965ef3 | 219 | } |
1da177e4 | 220 | |
31965ef3 DW |
221 | void |
222 | xfs_errortag_del( | |
223 | struct xfs_mount *mp) | |
224 | { | |
c6840101 | 225 | xfs_sysfs_del(&mp->m_errortag_kobj); |
31965ef3 DW |
226 | kmem_free(mp->m_errortag); |
227 | } | |
1da177e4 | 228 | |
31965ef3 DW |
229 | bool |
230 | xfs_errortag_test( | |
231 | struct xfs_mount *mp, | |
232 | const char *expression, | |
233 | const char *file, | |
234 | int line, | |
235 | unsigned int error_tag) | |
236 | { | |
237 | unsigned int randfactor; | |
1da177e4 | 238 | |
d04c241c CM |
239 | /* |
240 | * To be able to use error injection anywhere, we need to ensure error | |
241 | * injection mechanism is already initialized. | |
242 | * | |
243 | * Code paths like I/O completion can be called before the | |
244 | * initialization is complete, but be able to inject errors in such | |
245 | * places is still useful. | |
246 | */ | |
247 | if (!mp->m_errortag) | |
248 | return false; | |
249 | ||
31965ef3 DW |
250 | ASSERT(error_tag < XFS_ERRTAG_MAX); |
251 | randfactor = mp->m_errortag[error_tag]; | |
252 | if (!randfactor || prandom_u32() % randfactor) | |
253 | return false; | |
1da177e4 | 254 | |
31965ef3 DW |
255 | xfs_warn_ratelimited(mp, |
256 | "Injecting error (%s) at file %s, line %d, on filesystem \"%s\"", | |
257 | expression, file, line, mp->m_fsname); | |
258 | return true; | |
1da177e4 LT |
259 | } |
260 | ||
c6840101 DW |
261 | int |
262 | xfs_errortag_get( | |
263 | struct xfs_mount *mp, | |
264 | unsigned int error_tag) | |
265 | { | |
266 | if (error_tag >= XFS_ERRTAG_MAX) | |
267 | return -EINVAL; | |
268 | ||
269 | return mp->m_errortag[error_tag]; | |
270 | } | |
271 | ||
1da177e4 | 272 | int |
31965ef3 DW |
273 | xfs_errortag_set( |
274 | struct xfs_mount *mp, | |
275 | unsigned int error_tag, | |
276 | unsigned int tag_value) | |
1da177e4 | 277 | { |
128f24d5 DW |
278 | if (error_tag >= XFS_ERRTAG_MAX) |
279 | return -EINVAL; | |
280 | ||
31965ef3 DW |
281 | mp->m_errortag[error_tag] = tag_value; |
282 | return 0; | |
1da177e4 LT |
283 | } |
284 | ||
1da177e4 | 285 | int |
31965ef3 DW |
286 | xfs_errortag_add( |
287 | struct xfs_mount *mp, | |
288 | unsigned int error_tag) | |
1da177e4 | 289 | { |
31965ef3 DW |
290 | if (error_tag >= XFS_ERRTAG_MAX) |
291 | return -EINVAL; | |
1da177e4 | 292 | |
31965ef3 DW |
293 | return xfs_errortag_set(mp, error_tag, |
294 | xfs_errortag_random_default[error_tag]); | |
295 | } | |
1da177e4 | 296 | |
31965ef3 DW |
297 | int |
298 | xfs_errortag_clearall( | |
299 | struct xfs_mount *mp) | |
300 | { | |
301 | memset(mp->m_errortag, 0, sizeof(unsigned int) * XFS_ERRTAG_MAX); | |
1da177e4 LT |
302 | return 0; |
303 | } | |
1550d0b0 | 304 | #endif /* DEBUG */ |
1da177e4 | 305 | |
1da177e4 LT |
306 | void |
307 | xfs_error_report( | |
a0e856b0 AE |
308 | const char *tag, |
309 | int level, | |
310 | struct xfs_mount *mp, | |
311 | const char *filename, | |
312 | int linenum, | |
fc51c2b5 | 313 | void *ra) |
1da177e4 LT |
314 | { |
315 | if (level <= xfs_error_level) { | |
6a19d939 | 316 | xfs_alert_tag(mp, XFS_PTAG_ERROR_REPORT, |
65dd297a | 317 | "Internal error %s at line %d of file %s. Caller %pS", |
a0e856b0 | 318 | tag, linenum, filename, ra); |
1da177e4 LT |
319 | |
320 | xfs_stack_trace(); | |
321 | } | |
322 | } | |
323 | ||
1da177e4 LT |
324 | void |
325 | xfs_corruption_error( | |
a0e856b0 AE |
326 | const char *tag, |
327 | int level, | |
328 | struct xfs_mount *mp, | |
329 | void *p, | |
330 | const char *filename, | |
331 | int linenum, | |
fc51c2b5 | 332 | void *ra) |
1da177e4 LT |
333 | { |
334 | if (level <= xfs_error_level) | |
a2050646 | 335 | xfs_hex_dump(p, 64); |
a0e856b0 | 336 | xfs_error_report(tag, level, mp, filename, linenum, ra); |
65333b4c | 337 | xfs_alert(mp, "Corruption detected. Unmount and run xfs_repair"); |
1da177e4 | 338 | } |
ca23f8fd ES |
339 | |
340 | /* | |
341 | * Warnings specifically for verifier errors. Differentiate CRC vs. invalid | |
342 | * values, and omit the stack trace unless the error level is tuned high. | |
343 | */ | |
344 | void | |
345 | xfs_verifier_error( | |
346 | struct xfs_buf *bp) | |
347 | { | |
348 | struct xfs_mount *mp = bp->b_target->bt_mount; | |
349 | ||
233135b7 | 350 | xfs_alert(mp, "Metadata %s detected at %pF, %s block 0x%llx", |
2451337d | 351 | bp->b_error == -EFSBADCRC ? "CRC error" : "corruption", |
233135b7 | 352 | __return_address, bp->b_ops->name, bp->b_bn); |
ca23f8fd ES |
353 | |
354 | xfs_alert(mp, "Unmount and run xfs_repair"); | |
355 | ||
356 | if (xfs_error_level >= XFS_ERRLEVEL_LOW) { | |
357 | xfs_alert(mp, "First 64 bytes of corrupted metadata buffer:"); | |
358 | xfs_hex_dump(xfs_buf_offset(bp, 0), 64); | |
359 | } | |
360 | ||
361 | if (xfs_error_level >= XFS_ERRLEVEL_HIGH) | |
362 | xfs_stack_trace(); | |
363 | } |