xfs_error.c 11 KB

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