xfs_error.c 9.8 KB

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