xfs_error.c 6.4 KB

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