xfs_error.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. strlen(xfs_etest_fsname[i]) + 1);
  133. xfs_etest_fsname[i] = NULL;
  134. }
  135. }
  136. if (loud || cleared)
  137. cmn_err(CE_WARN,
  138. "Cleared all XFS error tags for filesystem \"%s\"",
  139. mp->m_fsname);
  140. return 0;
  141. }
  142. #endif /* DEBUG || INDUCE_IO_ERROR */
  143. static void
  144. xfs_fs_vcmn_err(int level, xfs_mount_t *mp, char *fmt, va_list ap)
  145. {
  146. if (mp != NULL) {
  147. char *newfmt;
  148. int len = 16 + mp->m_fsname_len + strlen(fmt);
  149. newfmt = kmem_alloc(len, KM_SLEEP);
  150. sprintf(newfmt, "Filesystem \"%s\": %s", mp->m_fsname, fmt);
  151. icmn_err(level, newfmt, ap);
  152. kmem_free(newfmt, len);
  153. } else {
  154. icmn_err(level, fmt, ap);
  155. }
  156. }
  157. void
  158. xfs_fs_cmn_err(int level, xfs_mount_t *mp, char *fmt, ...)
  159. {
  160. va_list ap;
  161. va_start(ap, fmt);
  162. xfs_fs_vcmn_err(level, mp, fmt, ap);
  163. va_end(ap);
  164. }
  165. void
  166. xfs_cmn_err(int panic_tag, int level, xfs_mount_t *mp, char *fmt, ...)
  167. {
  168. va_list ap;
  169. #ifdef DEBUG
  170. xfs_panic_mask |= XFS_PTAG_SHUTDOWN_CORRUPT;
  171. #endif
  172. if (xfs_panic_mask && (xfs_panic_mask & panic_tag)
  173. && (level & CE_ALERT)) {
  174. level &= ~CE_ALERT;
  175. level |= CE_PANIC;
  176. cmn_err(CE_ALERT, "XFS: Transforming an alert into a BUG.");
  177. }
  178. va_start(ap, fmt);
  179. xfs_fs_vcmn_err(level, mp, fmt, ap);
  180. va_end(ap);
  181. }
  182. void
  183. xfs_error_report(
  184. char *tag,
  185. int level,
  186. xfs_mount_t *mp,
  187. char *fname,
  188. int linenum,
  189. inst_t *ra)
  190. {
  191. if (level <= xfs_error_level) {
  192. xfs_cmn_err(XFS_PTAG_ERROR_REPORT,
  193. CE_ALERT, mp,
  194. "XFS internal error %s at line %d of file %s. Caller 0x%p\n",
  195. tag, linenum, fname, ra);
  196. xfs_stack_trace();
  197. }
  198. }
  199. STATIC void
  200. xfs_hex_dump(void *p, int length)
  201. {
  202. __uint8_t *uip = (__uint8_t*)p;
  203. int i;
  204. char sbuf[128], *s;
  205. s = sbuf;
  206. *s = '\0';
  207. for (i=0; i<length; i++, uip++) {
  208. if ((i % 16) == 0) {
  209. if (*s != '\0')
  210. cmn_err(CE_ALERT, "%s\n", sbuf);
  211. s = sbuf;
  212. sprintf(s, "0x%x: ", i);
  213. while( *s != '\0')
  214. s++;
  215. }
  216. sprintf(s, "%02x ", *uip);
  217. /*
  218. * the kernel sprintf is a void; user sprintf returns
  219. * the sprintf'ed string's length. Find the new end-
  220. * of-string
  221. */
  222. while( *s != '\0')
  223. s++;
  224. }
  225. cmn_err(CE_ALERT, "%s\n", sbuf);
  226. }
  227. void
  228. xfs_corruption_error(
  229. char *tag,
  230. int level,
  231. xfs_mount_t *mp,
  232. void *p,
  233. char *fname,
  234. int linenum,
  235. inst_t *ra)
  236. {
  237. if (level <= xfs_error_level)
  238. xfs_hex_dump(p, 16);
  239. xfs_error_report(tag, level, mp, fname, linenum, ra);
  240. }