xfs_error.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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_error.h"
  25. #include "xfs_sysfs.h"
  26. #ifdef DEBUG
  27. static unsigned int xfs_errortag_random_default[] = {
  28. XFS_RANDOM_DEFAULT,
  29. XFS_RANDOM_IFLUSH_1,
  30. XFS_RANDOM_IFLUSH_2,
  31. XFS_RANDOM_IFLUSH_3,
  32. XFS_RANDOM_IFLUSH_4,
  33. XFS_RANDOM_IFLUSH_5,
  34. XFS_RANDOM_IFLUSH_6,
  35. XFS_RANDOM_DA_READ_BUF,
  36. XFS_RANDOM_BTREE_CHECK_LBLOCK,
  37. XFS_RANDOM_BTREE_CHECK_SBLOCK,
  38. XFS_RANDOM_ALLOC_READ_AGF,
  39. XFS_RANDOM_IALLOC_READ_AGI,
  40. XFS_RANDOM_ITOBP_INOTOBP,
  41. XFS_RANDOM_IUNLINK,
  42. XFS_RANDOM_IUNLINK_REMOVE,
  43. XFS_RANDOM_DIR_INO_VALIDATE,
  44. XFS_RANDOM_BULKSTAT_READ_CHUNK,
  45. XFS_RANDOM_IODONE_IOERR,
  46. XFS_RANDOM_STRATREAD_IOERR,
  47. XFS_RANDOM_STRATCMPL_IOERR,
  48. XFS_RANDOM_DIOWRITE_IOERR,
  49. XFS_RANDOM_BMAPIFORMAT,
  50. XFS_RANDOM_FREE_EXTENT,
  51. XFS_RANDOM_RMAP_FINISH_ONE,
  52. XFS_RANDOM_REFCOUNT_CONTINUE_UPDATE,
  53. XFS_RANDOM_REFCOUNT_FINISH_ONE,
  54. XFS_RANDOM_BMAP_FINISH_ONE,
  55. XFS_RANDOM_AG_RESV_CRITICAL,
  56. XFS_RANDOM_DROP_WRITES,
  57. XFS_RANDOM_LOG_BAD_CRC,
  58. };
  59. struct xfs_errortag_attr {
  60. struct attribute attr;
  61. unsigned int tag;
  62. };
  63. static inline struct xfs_errortag_attr *
  64. to_attr(struct attribute *attr)
  65. {
  66. return container_of(attr, struct xfs_errortag_attr, attr);
  67. }
  68. static inline struct xfs_mount *
  69. to_mp(struct kobject *kobject)
  70. {
  71. struct xfs_kobj *kobj = to_kobj(kobject);
  72. return container_of(kobj, struct xfs_mount, m_errortag_kobj);
  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. if (strcmp(buf, "default") == 0) {
  86. val = xfs_errortag_random_default[xfs_attr->tag];
  87. } else {
  88. ret = kstrtouint(buf, 0, &val);
  89. if (ret)
  90. return ret;
  91. }
  92. ret = xfs_errortag_set(mp, xfs_attr->tag, val);
  93. if (ret)
  94. return ret;
  95. return count;
  96. }
  97. STATIC ssize_t
  98. xfs_errortag_attr_show(
  99. struct kobject *kobject,
  100. struct attribute *attr,
  101. char *buf)
  102. {
  103. struct xfs_mount *mp = to_mp(kobject);
  104. struct xfs_errortag_attr *xfs_attr = to_attr(attr);
  105. return snprintf(buf, PAGE_SIZE, "%u\n",
  106. xfs_errortag_get(mp, xfs_attr->tag));
  107. }
  108. static const struct sysfs_ops xfs_errortag_sysfs_ops = {
  109. .show = xfs_errortag_attr_show,
  110. .store = xfs_errortag_attr_store,
  111. };
  112. #define XFS_ERRORTAG_ATTR_RW(_name, _tag) \
  113. static struct xfs_errortag_attr xfs_errortag_attr_##_name = { \
  114. .attr = {.name = __stringify(_name), \
  115. .mode = VERIFY_OCTAL_PERMISSIONS(S_IWUSR | S_IRUGO) }, \
  116. .tag = (_tag), \
  117. }
  118. #define XFS_ERRORTAG_ATTR_LIST(_name) &xfs_errortag_attr_##_name.attr
  119. XFS_ERRORTAG_ATTR_RW(noerror, XFS_ERRTAG_NOERROR);
  120. XFS_ERRORTAG_ATTR_RW(iflush1, XFS_ERRTAG_IFLUSH_1);
  121. XFS_ERRORTAG_ATTR_RW(iflush2, XFS_ERRTAG_IFLUSH_2);
  122. XFS_ERRORTAG_ATTR_RW(iflush3, XFS_ERRTAG_IFLUSH_3);
  123. XFS_ERRORTAG_ATTR_RW(iflush4, XFS_ERRTAG_IFLUSH_4);
  124. XFS_ERRORTAG_ATTR_RW(iflush5, XFS_ERRTAG_IFLUSH_5);
  125. XFS_ERRORTAG_ATTR_RW(iflush6, XFS_ERRTAG_IFLUSH_6);
  126. XFS_ERRORTAG_ATTR_RW(dareadbuf, XFS_ERRTAG_DA_READ_BUF);
  127. XFS_ERRORTAG_ATTR_RW(btree_chk_lblk, XFS_ERRTAG_BTREE_CHECK_LBLOCK);
  128. XFS_ERRORTAG_ATTR_RW(btree_chk_sblk, XFS_ERRTAG_BTREE_CHECK_SBLOCK);
  129. XFS_ERRORTAG_ATTR_RW(readagf, XFS_ERRTAG_ALLOC_READ_AGF);
  130. XFS_ERRORTAG_ATTR_RW(readagi, XFS_ERRTAG_IALLOC_READ_AGI);
  131. XFS_ERRORTAG_ATTR_RW(itobp, XFS_ERRTAG_ITOBP_INOTOBP);
  132. XFS_ERRORTAG_ATTR_RW(iunlink, XFS_ERRTAG_IUNLINK);
  133. XFS_ERRORTAG_ATTR_RW(iunlinkrm, XFS_ERRTAG_IUNLINK_REMOVE);
  134. XFS_ERRORTAG_ATTR_RW(dirinovalid, XFS_ERRTAG_DIR_INO_VALIDATE);
  135. XFS_ERRORTAG_ATTR_RW(bulkstat, XFS_ERRTAG_BULKSTAT_READ_CHUNK);
  136. XFS_ERRORTAG_ATTR_RW(logiodone, XFS_ERRTAG_IODONE_IOERR);
  137. XFS_ERRORTAG_ATTR_RW(stratread, XFS_ERRTAG_STRATREAD_IOERR);
  138. XFS_ERRORTAG_ATTR_RW(stratcmpl, XFS_ERRTAG_STRATCMPL_IOERR);
  139. XFS_ERRORTAG_ATTR_RW(diowrite, XFS_ERRTAG_DIOWRITE_IOERR);
  140. XFS_ERRORTAG_ATTR_RW(bmapifmt, XFS_ERRTAG_BMAPIFORMAT);
  141. XFS_ERRORTAG_ATTR_RW(free_extent, XFS_ERRTAG_FREE_EXTENT);
  142. XFS_ERRORTAG_ATTR_RW(rmap_finish_one, XFS_ERRTAG_RMAP_FINISH_ONE);
  143. XFS_ERRORTAG_ATTR_RW(refcount_continue_update, XFS_ERRTAG_REFCOUNT_CONTINUE_UPDATE);
  144. XFS_ERRORTAG_ATTR_RW(refcount_finish_one, XFS_ERRTAG_REFCOUNT_FINISH_ONE);
  145. XFS_ERRORTAG_ATTR_RW(bmap_finish_one, XFS_ERRTAG_BMAP_FINISH_ONE);
  146. XFS_ERRORTAG_ATTR_RW(ag_resv_critical, XFS_ERRTAG_AG_RESV_CRITICAL);
  147. XFS_ERRORTAG_ATTR_RW(drop_writes, XFS_ERRTAG_DROP_WRITES);
  148. XFS_ERRORTAG_ATTR_RW(log_bad_crc, XFS_ERRTAG_LOG_BAD_CRC);
  149. static struct attribute *xfs_errortag_attrs[] = {
  150. XFS_ERRORTAG_ATTR_LIST(noerror),
  151. XFS_ERRORTAG_ATTR_LIST(iflush1),
  152. XFS_ERRORTAG_ATTR_LIST(iflush2),
  153. XFS_ERRORTAG_ATTR_LIST(iflush3),
  154. XFS_ERRORTAG_ATTR_LIST(iflush4),
  155. XFS_ERRORTAG_ATTR_LIST(iflush5),
  156. XFS_ERRORTAG_ATTR_LIST(iflush6),
  157. XFS_ERRORTAG_ATTR_LIST(dareadbuf),
  158. XFS_ERRORTAG_ATTR_LIST(btree_chk_lblk),
  159. XFS_ERRORTAG_ATTR_LIST(btree_chk_sblk),
  160. XFS_ERRORTAG_ATTR_LIST(readagf),
  161. XFS_ERRORTAG_ATTR_LIST(readagi),
  162. XFS_ERRORTAG_ATTR_LIST(itobp),
  163. XFS_ERRORTAG_ATTR_LIST(iunlink),
  164. XFS_ERRORTAG_ATTR_LIST(iunlinkrm),
  165. XFS_ERRORTAG_ATTR_LIST(dirinovalid),
  166. XFS_ERRORTAG_ATTR_LIST(bulkstat),
  167. XFS_ERRORTAG_ATTR_LIST(logiodone),
  168. XFS_ERRORTAG_ATTR_LIST(stratread),
  169. XFS_ERRORTAG_ATTR_LIST(stratcmpl),
  170. XFS_ERRORTAG_ATTR_LIST(diowrite),
  171. XFS_ERRORTAG_ATTR_LIST(bmapifmt),
  172. XFS_ERRORTAG_ATTR_LIST(free_extent),
  173. XFS_ERRORTAG_ATTR_LIST(rmap_finish_one),
  174. XFS_ERRORTAG_ATTR_LIST(refcount_continue_update),
  175. XFS_ERRORTAG_ATTR_LIST(refcount_finish_one),
  176. XFS_ERRORTAG_ATTR_LIST(bmap_finish_one),
  177. XFS_ERRORTAG_ATTR_LIST(ag_resv_critical),
  178. XFS_ERRORTAG_ATTR_LIST(drop_writes),
  179. XFS_ERRORTAG_ATTR_LIST(log_bad_crc),
  180. NULL,
  181. };
  182. struct kobj_type xfs_errortag_ktype = {
  183. .release = xfs_sysfs_release,
  184. .sysfs_ops = &xfs_errortag_sysfs_ops,
  185. .default_attrs = xfs_errortag_attrs,
  186. };
  187. int
  188. xfs_errortag_init(
  189. struct xfs_mount *mp)
  190. {
  191. mp->m_errortag = kmem_zalloc(sizeof(unsigned int) * XFS_ERRTAG_MAX,
  192. KM_SLEEP | KM_MAYFAIL);
  193. if (!mp->m_errortag)
  194. return -ENOMEM;
  195. return xfs_sysfs_init(&mp->m_errortag_kobj, &xfs_errortag_ktype,
  196. &mp->m_kobj, "errortag");
  197. }
  198. void
  199. xfs_errortag_del(
  200. struct xfs_mount *mp)
  201. {
  202. xfs_sysfs_del(&mp->m_errortag_kobj);
  203. kmem_free(mp->m_errortag);
  204. }
  205. bool
  206. xfs_errortag_test(
  207. struct xfs_mount *mp,
  208. const char *expression,
  209. const char *file,
  210. int line,
  211. unsigned int error_tag)
  212. {
  213. unsigned int randfactor;
  214. /*
  215. * To be able to use error injection anywhere, we need to ensure error
  216. * injection mechanism is already initialized.
  217. *
  218. * Code paths like I/O completion can be called before the
  219. * initialization is complete, but be able to inject errors in such
  220. * places is still useful.
  221. */
  222. if (!mp->m_errortag)
  223. return false;
  224. ASSERT(error_tag < XFS_ERRTAG_MAX);
  225. randfactor = mp->m_errortag[error_tag];
  226. if (!randfactor || prandom_u32() % randfactor)
  227. return false;
  228. xfs_warn_ratelimited(mp,
  229. "Injecting error (%s) at file %s, line %d, on filesystem \"%s\"",
  230. expression, file, line, mp->m_fsname);
  231. return true;
  232. }
  233. int
  234. xfs_errortag_get(
  235. struct xfs_mount *mp,
  236. unsigned int error_tag)
  237. {
  238. if (error_tag >= XFS_ERRTAG_MAX)
  239. return -EINVAL;
  240. return mp->m_errortag[error_tag];
  241. }
  242. int
  243. xfs_errortag_set(
  244. struct xfs_mount *mp,
  245. unsigned int error_tag,
  246. unsigned int tag_value)
  247. {
  248. if (error_tag >= XFS_ERRTAG_MAX)
  249. return -EINVAL;
  250. mp->m_errortag[error_tag] = tag_value;
  251. return 0;
  252. }
  253. int
  254. xfs_errortag_add(
  255. struct xfs_mount *mp,
  256. unsigned int error_tag)
  257. {
  258. if (error_tag >= XFS_ERRTAG_MAX)
  259. return -EINVAL;
  260. return xfs_errortag_set(mp, error_tag,
  261. xfs_errortag_random_default[error_tag]);
  262. }
  263. int
  264. xfs_errortag_clearall(
  265. struct xfs_mount *mp)
  266. {
  267. memset(mp->m_errortag, 0, sizeof(unsigned int) * XFS_ERRTAG_MAX);
  268. return 0;
  269. }
  270. #endif /* DEBUG */
  271. void
  272. xfs_error_report(
  273. const char *tag,
  274. int level,
  275. struct xfs_mount *mp,
  276. const char *filename,
  277. int linenum,
  278. void *ra)
  279. {
  280. if (level <= xfs_error_level) {
  281. xfs_alert_tag(mp, XFS_PTAG_ERROR_REPORT,
  282. "Internal error %s at line %d of file %s. Caller %pS",
  283. tag, linenum, filename, ra);
  284. xfs_stack_trace();
  285. }
  286. }
  287. void
  288. xfs_corruption_error(
  289. const char *tag,
  290. int level,
  291. struct xfs_mount *mp,
  292. void *p,
  293. const char *filename,
  294. int linenum,
  295. void *ra)
  296. {
  297. if (level <= xfs_error_level)
  298. xfs_hex_dump(p, 64);
  299. xfs_error_report(tag, level, mp, filename, linenum, ra);
  300. xfs_alert(mp, "Corruption detected. Unmount and run xfs_repair");
  301. }
  302. /*
  303. * Warnings specifically for verifier errors. Differentiate CRC vs. invalid
  304. * values, and omit the stack trace unless the error level is tuned high.
  305. */
  306. void
  307. xfs_verifier_error(
  308. struct xfs_buf *bp)
  309. {
  310. struct xfs_mount *mp = bp->b_target->bt_mount;
  311. xfs_alert(mp, "Metadata %s detected at %pF, %s block 0x%llx",
  312. bp->b_error == -EFSBADCRC ? "CRC error" : "corruption",
  313. __return_address, bp->b_ops->name, bp->b_bn);
  314. xfs_alert(mp, "Unmount and run xfs_repair");
  315. if (xfs_error_level >= XFS_ERRLEVEL_LOW) {
  316. xfs_alert(mp, "First 64 bytes of corrupted metadata buffer:");
  317. xfs_hex_dump(xfs_buf_offset(bp, 0), 64);
  318. }
  319. if (xfs_error_level >= XFS_ERRLEVEL_HIGH)
  320. xfs_stack_trace();
  321. }