xfs_error.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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_fs.h"
  20. #include "xfs_types.h"
  21. #include "xfs_log.h"
  22. #include "xfs_inum.h"
  23. #include "xfs_trans.h"
  24. #include "xfs_sb.h"
  25. #include "xfs_ag.h"
  26. #include "xfs_dir2.h"
  27. #include "xfs_dmapi.h"
  28. #include "xfs_mount.h"
  29. #include "xfs_bmap_btree.h"
  30. #include "xfs_dir2_sf.h"
  31. #include "xfs_attr_sf.h"
  32. #include "xfs_dinode.h"
  33. #include "xfs_inode.h"
  34. #include "xfs_utils.h"
  35. #include "xfs_error.h"
  36. #ifdef DEBUG
  37. int xfs_etrap[XFS_ERROR_NTRAP] = {
  38. 0,
  39. };
  40. int
  41. xfs_error_trap(int e)
  42. {
  43. int i;
  44. if (!e)
  45. return 0;
  46. for (i = 0; i < XFS_ERROR_NTRAP; i++) {
  47. if (xfs_etrap[i] == 0)
  48. break;
  49. if (e != xfs_etrap[i])
  50. continue;
  51. cmn_err(CE_NOTE, "xfs_error_trap: error %d", e);
  52. BUG();
  53. break;
  54. }
  55. return e;
  56. }
  57. #endif
  58. #if (defined(DEBUG) || defined(INDUCE_IO_ERROR))
  59. int xfs_etest[XFS_NUM_INJECT_ERROR];
  60. int64_t xfs_etest_fsid[XFS_NUM_INJECT_ERROR];
  61. char * xfs_etest_fsname[XFS_NUM_INJECT_ERROR];
  62. void
  63. xfs_error_test_init(void)
  64. {
  65. memset(xfs_etest, 0, sizeof(xfs_etest));
  66. memset(xfs_etest_fsid, 0, sizeof(xfs_etest_fsid));
  67. memset(xfs_etest_fsname, 0, sizeof(xfs_etest_fsname));
  68. }
  69. int
  70. xfs_error_test(int error_tag, int *fsidp, char *expression,
  71. int line, char *file, unsigned long randfactor)
  72. {
  73. int i;
  74. int64_t fsid;
  75. if (random32() % randfactor)
  76. return 0;
  77. memcpy(&fsid, fsidp, sizeof(xfs_fsid_t));
  78. for (i = 0; i < XFS_NUM_INJECT_ERROR; i++) {
  79. if (xfs_etest[i] == error_tag && xfs_etest_fsid[i] == fsid) {
  80. cmn_err(CE_WARN,
  81. "Injecting error (%s) at file %s, line %d, on filesystem \"%s\"",
  82. expression, file, line, xfs_etest_fsname[i]);
  83. return 1;
  84. }
  85. }
  86. return 0;
  87. }
  88. int
  89. xfs_errortag_add(int error_tag, xfs_mount_t *mp)
  90. {
  91. int i;
  92. int len;
  93. int64_t fsid;
  94. memcpy(&fsid, mp->m_fixedfsid, sizeof(xfs_fsid_t));
  95. for (i = 0; i < XFS_NUM_INJECT_ERROR; i++) {
  96. if (xfs_etest_fsid[i] == fsid && xfs_etest[i] == error_tag) {
  97. cmn_err(CE_WARN, "XFS error tag #%d on", error_tag);
  98. return 0;
  99. }
  100. }
  101. for (i = 0; i < XFS_NUM_INJECT_ERROR; i++) {
  102. if (xfs_etest[i] == 0) {
  103. cmn_err(CE_WARN, "Turned on XFS error tag #%d",
  104. error_tag);
  105. xfs_etest[i] = error_tag;
  106. xfs_etest_fsid[i] = fsid;
  107. len = strlen(mp->m_fsname);
  108. xfs_etest_fsname[i] = kmem_alloc(len + 1, KM_SLEEP);
  109. strcpy(xfs_etest_fsname[i], mp->m_fsname);
  110. return 0;
  111. }
  112. }
  113. cmn_err(CE_WARN, "error tag overflow, too many turned on");
  114. return 1;
  115. }
  116. int
  117. xfs_errortag_clearall(xfs_mount_t *mp, int loud)
  118. {
  119. int64_t fsid;
  120. int cleared = 0;
  121. int i;
  122. memcpy(&fsid, mp->m_fixedfsid, sizeof(xfs_fsid_t));
  123. for (i = 0; i < XFS_NUM_INJECT_ERROR; i++) {
  124. if ((fsid == 0LL || xfs_etest_fsid[i] == fsid) &&
  125. xfs_etest[i] != 0) {
  126. cleared = 1;
  127. cmn_err(CE_WARN, "Clearing XFS error tag #%d",
  128. xfs_etest[i]);
  129. xfs_etest[i] = 0;
  130. xfs_etest_fsid[i] = 0LL;
  131. kmem_free(xfs_etest_fsname[i]);
  132. xfs_etest_fsname[i] = NULL;
  133. }
  134. }
  135. if (loud || cleared)
  136. cmn_err(CE_WARN,
  137. "Cleared all XFS error tags for filesystem \"%s\"",
  138. mp->m_fsname);
  139. return 0;
  140. }
  141. #endif /* DEBUG || INDUCE_IO_ERROR */
  142. static void
  143. xfs_fs_vcmn_err(int level, xfs_mount_t *mp, char *fmt, va_list ap)
  144. {
  145. if (mp != NULL) {
  146. char *newfmt;
  147. int len = 16 + mp->m_fsname_len + strlen(fmt);
  148. newfmt = kmem_alloc(len, KM_SLEEP);
  149. sprintf(newfmt, "Filesystem \"%s\": %s", mp->m_fsname, fmt);
  150. icmn_err(level, newfmt, ap);
  151. kmem_free(newfmt);
  152. } else {
  153. icmn_err(level, fmt, ap);
  154. }
  155. }
  156. void
  157. xfs_fs_cmn_err(int level, xfs_mount_t *mp, char *fmt, ...)
  158. {
  159. va_list ap;
  160. va_start(ap, fmt);
  161. xfs_fs_vcmn_err(level, mp, fmt, ap);
  162. va_end(ap);
  163. }
  164. void
  165. xfs_cmn_err(int panic_tag, int level, xfs_mount_t *mp, char *fmt, ...)
  166. {
  167. va_list ap;
  168. #ifdef DEBUG
  169. xfs_panic_mask |= XFS_PTAG_SHUTDOWN_CORRUPT;
  170. #endif
  171. if (xfs_panic_mask && (xfs_panic_mask & panic_tag)
  172. && (level & CE_ALERT)) {
  173. level &= ~CE_ALERT;
  174. level |= CE_PANIC;
  175. cmn_err(CE_ALERT, "XFS: Transforming an alert into a BUG.");
  176. }
  177. va_start(ap, fmt);
  178. xfs_fs_vcmn_err(level, mp, fmt, ap);
  179. va_end(ap);
  180. }
  181. void
  182. xfs_error_report(
  183. char *tag,
  184. int level,
  185. xfs_mount_t *mp,
  186. char *fname,
  187. int linenum,
  188. inst_t *ra)
  189. {
  190. if (level <= xfs_error_level) {
  191. xfs_cmn_err(XFS_PTAG_ERROR_REPORT,
  192. CE_ALERT, mp,
  193. "XFS internal error %s at line %d of file %s. Caller 0x%p\n",
  194. tag, linenum, fname, ra);
  195. xfs_stack_trace();
  196. }
  197. }
  198. void
  199. xfs_corruption_error(
  200. char *tag,
  201. int level,
  202. xfs_mount_t *mp,
  203. void *p,
  204. char *fname,
  205. int linenum,
  206. inst_t *ra)
  207. {
  208. if (level <= xfs_error_level)
  209. xfs_hex_dump(p, 16);
  210. xfs_error_report(tag, level, mp, fname, linenum, ra);
  211. }