xfs_error.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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_dir.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_dir_sf.h"
  31. #include "xfs_dir2_sf.h"
  32. #include "xfs_attr_sf.h"
  33. #include "xfs_dinode.h"
  34. #include "xfs_inode.h"
  35. #include "xfs_utils.h"
  36. #include "xfs_error.h"
  37. #ifdef DEBUG
  38. int xfs_etrap[XFS_ERROR_NTRAP] = {
  39. 0,
  40. };
  41. int
  42. xfs_error_trap(int e)
  43. {
  44. int i;
  45. if (!e)
  46. return 0;
  47. for (i = 0; i < XFS_ERROR_NTRAP; i++) {
  48. if (xfs_etrap[i] == 0)
  49. break;
  50. if (e != xfs_etrap[i])
  51. continue;
  52. cmn_err(CE_NOTE, "xfs_error_trap: error %d", e);
  53. debug_stop_all_cpus((void *)-1LL);
  54. BUG();
  55. break;
  56. }
  57. return e;
  58. }
  59. #endif
  60. #if (defined(DEBUG) || defined(INDUCE_IO_ERROR))
  61. int xfs_etest[XFS_NUM_INJECT_ERROR];
  62. int64_t xfs_etest_fsid[XFS_NUM_INJECT_ERROR];
  63. char * xfs_etest_fsname[XFS_NUM_INJECT_ERROR];
  64. void
  65. xfs_error_test_init(void)
  66. {
  67. memset(xfs_etest, 0, sizeof(xfs_etest));
  68. memset(xfs_etest_fsid, 0, sizeof(xfs_etest_fsid));
  69. memset(xfs_etest_fsname, 0, sizeof(xfs_etest_fsname));
  70. }
  71. int
  72. xfs_error_test(int error_tag, int *fsidp, char *expression,
  73. int line, char *file, unsigned long randfactor)
  74. {
  75. int i;
  76. int64_t fsid;
  77. if (random() % randfactor)
  78. return 0;
  79. memcpy(&fsid, fsidp, sizeof(xfs_fsid_t));
  80. for (i = 0; i < XFS_NUM_INJECT_ERROR; i++) {
  81. if (xfs_etest[i] == error_tag && xfs_etest_fsid[i] == fsid) {
  82. cmn_err(CE_WARN,
  83. "Injecting error (%s) at file %s, line %d, on filesystem \"%s\"",
  84. expression, file, line, xfs_etest_fsname[i]);
  85. return 1;
  86. }
  87. }
  88. return 0;
  89. }
  90. int
  91. xfs_errortag_add(int error_tag, xfs_mount_t *mp)
  92. {
  93. int i;
  94. int len;
  95. int64_t fsid;
  96. memcpy(&fsid, mp->m_fixedfsid, sizeof(xfs_fsid_t));
  97. for (i = 0; i < XFS_NUM_INJECT_ERROR; i++) {
  98. if (xfs_etest_fsid[i] == fsid && xfs_etest[i] == error_tag) {
  99. cmn_err(CE_WARN, "XFS error tag #%d on", error_tag);
  100. return 0;
  101. }
  102. }
  103. for (i = 0; i < XFS_NUM_INJECT_ERROR; i++) {
  104. if (xfs_etest[i] == 0) {
  105. cmn_err(CE_WARN, "Turned on XFS error tag #%d",
  106. error_tag);
  107. xfs_etest[i] = error_tag;
  108. xfs_etest_fsid[i] = fsid;
  109. len = strlen(mp->m_fsname);
  110. xfs_etest_fsname[i] = kmem_alloc(len + 1, KM_SLEEP);
  111. strcpy(xfs_etest_fsname[i], mp->m_fsname);
  112. return 0;
  113. }
  114. }
  115. cmn_err(CE_WARN, "error tag overflow, too many turned on");
  116. return 1;
  117. }
  118. int
  119. xfs_errortag_clear(int error_tag, xfs_mount_t *mp)
  120. {
  121. int i;
  122. int64_t fsid;
  123. memcpy(&fsid, mp->m_fixedfsid, sizeof(xfs_fsid_t));
  124. for (i = 0; i < XFS_NUM_INJECT_ERROR; i++) {
  125. if (xfs_etest_fsid[i] == fsid && xfs_etest[i] == error_tag) {
  126. xfs_etest[i] = 0;
  127. xfs_etest_fsid[i] = 0LL;
  128. kmem_free(xfs_etest_fsname[i],
  129. strlen(xfs_etest_fsname[i]) + 1);
  130. xfs_etest_fsname[i] = NULL;
  131. cmn_err(CE_WARN, "Cleared XFS error tag #%d",
  132. error_tag);
  133. return 0;
  134. }
  135. }
  136. cmn_err(CE_WARN, "XFS error tag %d not on", error_tag);
  137. return 1;
  138. }
  139. int
  140. xfs_errortag_clearall_umount(int64_t fsid, char *fsname, int loud)
  141. {
  142. int i;
  143. int cleared = 0;
  144. for (i = 0; i < XFS_NUM_INJECT_ERROR; i++) {
  145. if ((fsid == 0LL || xfs_etest_fsid[i] == fsid) &&
  146. xfs_etest[i] != 0) {
  147. cleared = 1;
  148. cmn_err(CE_WARN, "Clearing XFS error tag #%d",
  149. xfs_etest[i]);
  150. xfs_etest[i] = 0;
  151. xfs_etest_fsid[i] = 0LL;
  152. kmem_free(xfs_etest_fsname[i],
  153. strlen(xfs_etest_fsname[i]) + 1);
  154. xfs_etest_fsname[i] = NULL;
  155. }
  156. }
  157. if (loud || cleared)
  158. cmn_err(CE_WARN,
  159. "Cleared all XFS error tags for filesystem \"%s\"",
  160. fsname);
  161. return 0;
  162. }
  163. int
  164. xfs_errortag_clearall(xfs_mount_t *mp)
  165. {
  166. int64_t fsid;
  167. memcpy(&fsid, mp->m_fixedfsid, sizeof(xfs_fsid_t));
  168. return xfs_errortag_clearall_umount(fsid, mp->m_fsname, 1);
  169. }
  170. #endif /* DEBUG || INDUCE_IO_ERROR */
  171. static void
  172. xfs_fs_vcmn_err(int level, xfs_mount_t *mp, char *fmt, va_list ap)
  173. {
  174. if (mp != NULL) {
  175. char *newfmt;
  176. int len = 16 + mp->m_fsname_len + strlen(fmt);
  177. newfmt = kmem_alloc(len, KM_SLEEP);
  178. sprintf(newfmt, "Filesystem \"%s\": %s", mp->m_fsname, fmt);
  179. icmn_err(level, newfmt, ap);
  180. kmem_free(newfmt, len);
  181. } else {
  182. icmn_err(level, fmt, ap);
  183. }
  184. }
  185. void
  186. xfs_fs_cmn_err(int level, xfs_mount_t *mp, char *fmt, ...)
  187. {
  188. va_list ap;
  189. va_start(ap, fmt);
  190. xfs_fs_vcmn_err(level, mp, fmt, ap);
  191. va_end(ap);
  192. }
  193. void
  194. xfs_cmn_err(int panic_tag, int level, xfs_mount_t *mp, char *fmt, ...)
  195. {
  196. va_list ap;
  197. #ifdef DEBUG
  198. xfs_panic_mask |= XFS_PTAG_SHUTDOWN_CORRUPT;
  199. #endif
  200. if (xfs_panic_mask && (xfs_panic_mask & panic_tag)
  201. && (level & CE_ALERT)) {
  202. level &= ~CE_ALERT;
  203. level |= CE_PANIC;
  204. cmn_err(CE_ALERT, "XFS: Transforming an alert into a BUG.");
  205. }
  206. va_start(ap, fmt);
  207. xfs_fs_vcmn_err(level, mp, fmt, ap);
  208. va_end(ap);
  209. }
  210. void
  211. xfs_error_report(
  212. char *tag,
  213. int level,
  214. xfs_mount_t *mp,
  215. char *fname,
  216. int linenum,
  217. inst_t *ra)
  218. {
  219. if (level <= xfs_error_level) {
  220. xfs_cmn_err(XFS_PTAG_ERROR_REPORT,
  221. CE_ALERT, mp,
  222. "XFS internal error %s at line %d of file %s. Caller 0x%p\n",
  223. tag, linenum, fname, ra);
  224. xfs_stack_trace();
  225. }
  226. }
  227. STATIC void
  228. xfs_hex_dump(void *p, int length)
  229. {
  230. __uint8_t *uip = (__uint8_t*)p;
  231. int i;
  232. char sbuf[128], *s;
  233. s = sbuf;
  234. *s = '\0';
  235. for (i=0; i<length; i++, uip++) {
  236. if ((i % 16) == 0) {
  237. if (*s != '\0')
  238. cmn_err(CE_ALERT, "%s\n", sbuf);
  239. s = sbuf;
  240. sprintf(s, "0x%x: ", i);
  241. while( *s != '\0')
  242. s++;
  243. }
  244. sprintf(s, "%02x ", *uip);
  245. /*
  246. * the kernel sprintf is a void; user sprintf returns
  247. * the sprintf'ed string's length. Find the new end-
  248. * of-string
  249. */
  250. while( *s != '\0')
  251. s++;
  252. }
  253. cmn_err(CE_ALERT, "%s\n", sbuf);
  254. }
  255. void
  256. xfs_corruption_error(
  257. char *tag,
  258. int level,
  259. xfs_mount_t *mp,
  260. void *p,
  261. char *fname,
  262. int linenum,
  263. inst_t *ra)
  264. {
  265. if (level <= xfs_error_level)
  266. xfs_hex_dump(p, 16);
  267. xfs_error_report(tag, level, mp, fname, linenum, ra);
  268. }