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 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, 60 XFS_RANDOM_LARP, 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, 66 }; 67 68 struct xfs_errortag_attr { 69 struct attribute attr; 70 unsigned int tag; 71 }; 72 73 static inline struct xfs_errortag_attr * 74 to_attr(struct attribute *attr) 75 { 76 return container_of(attr, struct xfs_errortag_attr, attr); 77 } 78 79 static inline struct xfs_mount * 80 to_mp(struct kobject *kobject) 81 { 82 struct xfs_kobj *kobj = to_kobj(kobject); 83 84 return container_of(kobj, struct xfs_mount, m_errortag_kobj); 85 } 86 87 STATIC ssize_t 88 xfs_errortag_attr_store( 89 struct kobject *kobject, 90 struct attribute *attr, 91 const char *buf, 92 size_t count) 93 { 94 struct xfs_mount *mp = to_mp(kobject); 95 struct xfs_errortag_attr *xfs_attr = to_attr(attr); 96 int ret; 97 unsigned int val; 98 99 if (strcmp(buf, "default") == 0) { 100 val = xfs_errortag_random_default[xfs_attr->tag]; 101 } else { 102 ret = kstrtouint(buf, 0, &val); 103 if (ret) 104 return ret; 105 } 106 107 ret = xfs_errortag_set(mp, xfs_attr->tag, val); 108 if (ret) 109 return ret; 110 return count; 111 } 112 113 STATIC ssize_t 114 xfs_errortag_attr_show( 115 struct kobject *kobject, 116 struct attribute *attr, 117 char *buf) 118 { 119 struct xfs_mount *mp = to_mp(kobject); 120 struct xfs_errortag_attr *xfs_attr = to_attr(attr); 121 122 return snprintf(buf, PAGE_SIZE, "%u\n", 123 xfs_errortag_get(mp, xfs_attr->tag)); 124 } 125 126 static const struct sysfs_ops xfs_errortag_sysfs_ops = { 127 .show = xfs_errortag_attr_show, 128 .store = xfs_errortag_attr_store, 129 }; 130 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) }, \ 135 .tag = (_tag), \ 136 } 137 138 #define XFS_ERRORTAG_ATTR_LIST(_name) &xfs_errortag_attr_##_name.attr 139 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); 184 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), 230 NULL, 231 }; 232 ATTRIBUTE_GROUPS(xfs_errortag); 233 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, 238 }; 239 240 int 241 xfs_errortag_init( 242 struct xfs_mount *mp) 243 { 244 int ret; 245 246 mp->m_errortag = kzalloc(sizeof(unsigned int) * XFS_ERRTAG_MAX, 247 GFP_KERNEL | __GFP_RETRY_MAYFAIL); 248 if (!mp->m_errortag) 249 return -ENOMEM; 250 251 ret = xfs_sysfs_init(&mp->m_errortag_kobj, &xfs_errortag_ktype, 252 &mp->m_kobj, "errortag"); 253 if (ret) 254 kfree(mp->m_errortag); 255 return ret; 256 } 257 258 void 259 xfs_errortag_del( 260 struct xfs_mount *mp) 261 { 262 xfs_sysfs_del(&mp->m_errortag_kobj); 263 kfree(mp->m_errortag); 264 } 265 266 static bool 267 xfs_errortag_valid( 268 unsigned int error_tag) 269 { 270 if (error_tag >= XFS_ERRTAG_MAX) 271 return false; 272 273 /* Error out removed injection types */ 274 if (error_tag == XFS_ERRTAG_DROP_WRITES) 275 return false; 276 return true; 277 } 278 279 bool 280 xfs_errortag_enabled( 281 struct xfs_mount *mp, 282 unsigned int tag) 283 { 284 if (!mp->m_errortag) 285 return false; 286 if (!xfs_errortag_valid(tag)) 287 return false; 288 289 return mp->m_errortag[tag] != 0; 290 } 291 292 bool 293 xfs_errortag_test( 294 struct xfs_mount *mp, 295 const char *expression, 296 const char *file, 297 int line, 298 unsigned int error_tag) 299 { 300 unsigned int randfactor; 301 302 /* 303 * To be able to use error injection anywhere, we need to ensure error 304 * injection mechanism is already initialized. 305 * 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. 309 */ 310 if (!mp->m_errortag) 311 return false; 312 313 if (!xfs_errortag_valid(error_tag)) 314 return false; 315 316 randfactor = mp->m_errortag[error_tag]; 317 if (!randfactor || get_random_u32_below(randfactor)) 318 return false; 319 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); 323 return true; 324 } 325 326 int 327 xfs_errortag_get( 328 struct xfs_mount *mp, 329 unsigned int error_tag) 330 { 331 if (!xfs_errortag_valid(error_tag)) 332 return -EINVAL; 333 334 return mp->m_errortag[error_tag]; 335 } 336 337 int 338 xfs_errortag_set( 339 struct xfs_mount *mp, 340 unsigned int error_tag, 341 unsigned int tag_value) 342 { 343 if (!xfs_errortag_valid(error_tag)) 344 return -EINVAL; 345 346 mp->m_errortag[error_tag] = tag_value; 347 return 0; 348 } 349 350 int 351 xfs_errortag_add( 352 struct xfs_mount *mp, 353 unsigned int error_tag) 354 { 355 BUILD_BUG_ON(ARRAY_SIZE(xfs_errortag_random_default) != XFS_ERRTAG_MAX); 356 357 if (!xfs_errortag_valid(error_tag)) 358 return -EINVAL; 359 360 return xfs_errortag_set(mp, error_tag, 361 xfs_errortag_random_default[error_tag]); 362 } 363 364 int 365 xfs_errortag_clearall( 366 struct xfs_mount *mp) 367 { 368 memset(mp->m_errortag, 0, sizeof(unsigned int) * XFS_ERRTAG_MAX); 369 return 0; 370 } 371 #endif /* DEBUG */ 372 373 void 374 xfs_error_report( 375 const char *tag, 376 int level, 377 struct xfs_mount *mp, 378 const char *filename, 379 int linenum, 380 xfs_failaddr_t failaddr) 381 { 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); 386 387 xfs_stack_trace(); 388 } 389 } 390 391 void 392 xfs_corruption_error( 393 const char *tag, 394 int level, 395 struct xfs_mount *mp, 396 const void *buf, 397 size_t bufsize, 398 const char *filename, 399 int linenum, 400 xfs_failaddr_t failaddr) 401 { 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"); 406 } 407 408 /* 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 411 * bp->b_error. 412 * 413 * Call xfs_buf_mark_corrupt, not this function. 414 */ 415 void 416 xfs_buf_corruption_error( 417 struct xfs_buf *bp, 418 xfs_failaddr_t fa) 419 { 420 struct xfs_mount *mp = bp->b_mount; 421 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)); 425 426 xfs_alert(mp, "Unmount and run xfs_repair"); 427 428 if (xfs_error_level >= XFS_ERRLEVEL_HIGH) 429 xfs_stack_trace(); 430 } 431 432 /* 433 * Warnings specifically for verifier errors. Differentiate CRC vs. invalid 434 * values, and omit the stack trace unless the error level is tuned high. 435 */ 436 void 437 xfs_buf_verifier_error( 438 struct xfs_buf *bp, 439 int error, 440 const char *name, 441 const void *buf, 442 size_t bufsz, 443 xfs_failaddr_t failaddr) 444 { 445 struct xfs_mount *mp = bp->b_mount; 446 xfs_failaddr_t fa; 447 int sz; 448 449 fa = failaddr ? failaddr : __return_address; 450 __xfs_buf_ioerror(bp, error, fa); 451 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); 456 457 xfs_alert(mp, "Unmount and run xfs_repair"); 458 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:", 462 sz); 463 xfs_hex_dump(buf, sz); 464 } 465 466 if (xfs_error_level >= XFS_ERRLEVEL_HIGH) 467 xfs_stack_trace(); 468 } 469 470 /* 471 * Warnings specifically for verifier errors. Differentiate CRC vs. invalid 472 * values, and omit the stack trace unless the error level is tuned high. 473 */ 474 void 475 xfs_verifier_error( 476 struct xfs_buf *bp, 477 int error, 478 xfs_failaddr_t failaddr) 479 { 480 return xfs_buf_verifier_error(bp, error, "", xfs_buf_offset(bp, 0), 481 XFS_CORRUPTION_DUMP_LEN, failaddr); 482 } 483 484 /* 485 * Warnings for inode corruption problems. Don't bother with the stack 486 * trace unless the error level is turned up high. 487 */ 488 void 489 xfs_inode_verifier_error( 490 struct xfs_inode *ip, 491 int error, 492 const char *name, 493 const void *buf, 494 size_t bufsz, 495 xfs_failaddr_t failaddr) 496 { 497 struct xfs_mount *mp = ip->i_mount; 498 xfs_failaddr_t fa; 499 int sz; 500 501 fa = failaddr ? failaddr : __return_address; 502 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); 506 507 xfs_alert(mp, "Unmount and run xfs_repair"); 508 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:", 512 sz); 513 xfs_hex_dump(buf, sz); 514 } 515 516 if (xfs_error_level >= XFS_ERRLEVEL_HIGH) 517 xfs_stack_trace(); 518 } 519
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.