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