xfs_error.c 9.1 KB

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