xfs_error.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * Copyright (c) 2000-2001 Silicon Graphics, Inc. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of version 2 of the GNU General Public License as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it would be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. *
  12. * Further, this software is distributed without any warranty that it is
  13. * free of the rightful claim of any third person regarding infringement
  14. * or the like. Any license provided herein, whether implied or
  15. * otherwise, applies only to this software file. Patent licenses, if
  16. * any, provided herein do not apply to combinations of this program with
  17. * other software, or any other product whatsoever.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write the Free Software Foundation, Inc., 59
  21. * Temple Place - Suite 330, Boston MA 02111-1307, USA.
  22. *
  23. * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
  24. * Mountain View, CA 94043, or:
  25. *
  26. * http://www.sgi.com
  27. *
  28. * For further information regarding this notice, see:
  29. *
  30. * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
  31. */
  32. #include "xfs.h"
  33. #include "xfs_macros.h"
  34. #include "xfs_types.h"
  35. #include "xfs_inum.h"
  36. #include "xfs_log.h"
  37. #include "xfs_sb.h"
  38. #include "xfs_trans.h"
  39. #include "xfs_dir.h"
  40. #include "xfs_dir2.h"
  41. #include "xfs_dmapi.h"
  42. #include "xfs_mount.h"
  43. #include "xfs_bmap_btree.h"
  44. #include "xfs_attr_sf.h"
  45. #include "xfs_dir_sf.h"
  46. #include "xfs_dir2_sf.h"
  47. #include "xfs_dinode.h"
  48. #include "xfs_inode.h"
  49. #include "xfs_utils.h"
  50. #include "xfs_error.h"
  51. #ifdef DEBUG
  52. int xfs_etrap[XFS_ERROR_NTRAP] = {
  53. 0,
  54. };
  55. int
  56. xfs_error_trap(int e)
  57. {
  58. int i;
  59. if (!e)
  60. return 0;
  61. for (i = 0; i < XFS_ERROR_NTRAP; i++) {
  62. if (xfs_etrap[i] == 0)
  63. break;
  64. if (e != xfs_etrap[i])
  65. continue;
  66. cmn_err(CE_NOTE, "xfs_error_trap: error %d", e);
  67. debug_stop_all_cpus((void *)-1LL);
  68. BUG();
  69. break;
  70. }
  71. return e;
  72. }
  73. #endif
  74. #if (defined(DEBUG) || defined(INDUCE_IO_ERROR))
  75. int xfs_etest[XFS_NUM_INJECT_ERROR];
  76. int64_t xfs_etest_fsid[XFS_NUM_INJECT_ERROR];
  77. char * xfs_etest_fsname[XFS_NUM_INJECT_ERROR];
  78. void
  79. xfs_error_test_init(void)
  80. {
  81. memset(xfs_etest, 0, sizeof(xfs_etest));
  82. memset(xfs_etest_fsid, 0, sizeof(xfs_etest_fsid));
  83. memset(xfs_etest_fsname, 0, sizeof(xfs_etest_fsname));
  84. }
  85. int
  86. xfs_error_test(int error_tag, int *fsidp, char *expression,
  87. int line, char *file, unsigned long randfactor)
  88. {
  89. int i;
  90. int64_t fsid;
  91. if (random() % randfactor)
  92. return 0;
  93. memcpy(&fsid, fsidp, sizeof(xfs_fsid_t));
  94. for (i = 0; i < XFS_NUM_INJECT_ERROR; i++) {
  95. if (xfs_etest[i] == error_tag && xfs_etest_fsid[i] == fsid) {
  96. cmn_err(CE_WARN,
  97. "Injecting error (%s) at file %s, line %d, on filesystem \"%s\"",
  98. expression, file, line, xfs_etest_fsname[i]);
  99. return 1;
  100. }
  101. }
  102. return 0;
  103. }
  104. int
  105. xfs_errortag_add(int error_tag, xfs_mount_t *mp)
  106. {
  107. int i;
  108. int len;
  109. int64_t fsid;
  110. memcpy(&fsid, mp->m_fixedfsid, sizeof(xfs_fsid_t));
  111. for (i = 0; i < XFS_NUM_INJECT_ERROR; i++) {
  112. if (xfs_etest_fsid[i] == fsid && xfs_etest[i] == error_tag) {
  113. cmn_err(CE_WARN, "XFS error tag #%d on", error_tag);
  114. return 0;
  115. }
  116. }
  117. for (i = 0; i < XFS_NUM_INJECT_ERROR; i++) {
  118. if (xfs_etest[i] == 0) {
  119. cmn_err(CE_WARN, "Turned on XFS error tag #%d",
  120. error_tag);
  121. xfs_etest[i] = error_tag;
  122. xfs_etest_fsid[i] = fsid;
  123. len = strlen(mp->m_fsname);
  124. xfs_etest_fsname[i] = kmem_alloc(len + 1, KM_SLEEP);
  125. strcpy(xfs_etest_fsname[i], mp->m_fsname);
  126. return 0;
  127. }
  128. }
  129. cmn_err(CE_WARN, "error tag overflow, too many turned on");
  130. return 1;
  131. }
  132. int
  133. xfs_errortag_clear(int error_tag, xfs_mount_t *mp)
  134. {
  135. int i;
  136. int64_t fsid;
  137. memcpy(&fsid, mp->m_fixedfsid, sizeof(xfs_fsid_t));
  138. for (i = 0; i < XFS_NUM_INJECT_ERROR; i++) {
  139. if (xfs_etest_fsid[i] == fsid && xfs_etest[i] == error_tag) {
  140. xfs_etest[i] = 0;
  141. xfs_etest_fsid[i] = 0LL;
  142. kmem_free(xfs_etest_fsname[i],
  143. strlen(xfs_etest_fsname[i]) + 1);
  144. xfs_etest_fsname[i] = NULL;
  145. cmn_err(CE_WARN, "Cleared XFS error tag #%d",
  146. error_tag);
  147. return 0;
  148. }
  149. }
  150. cmn_err(CE_WARN, "XFS error tag %d not on", error_tag);
  151. return 1;
  152. }
  153. int
  154. xfs_errortag_clearall_umount(int64_t fsid, char *fsname, int loud)
  155. {
  156. int i;
  157. int cleared = 0;
  158. for (i = 0; i < XFS_NUM_INJECT_ERROR; i++) {
  159. if ((fsid == 0LL || xfs_etest_fsid[i] == fsid) &&
  160. xfs_etest[i] != 0) {
  161. cleared = 1;
  162. cmn_err(CE_WARN, "Clearing XFS error tag #%d",
  163. xfs_etest[i]);
  164. xfs_etest[i] = 0;
  165. xfs_etest_fsid[i] = 0LL;
  166. kmem_free(xfs_etest_fsname[i],
  167. strlen(xfs_etest_fsname[i]) + 1);
  168. xfs_etest_fsname[i] = NULL;
  169. }
  170. }
  171. if (loud || cleared)
  172. cmn_err(CE_WARN,
  173. "Cleared all XFS error tags for filesystem \"%s\"",
  174. fsname);
  175. return 0;
  176. }
  177. int
  178. xfs_errortag_clearall(xfs_mount_t *mp)
  179. {
  180. int64_t fsid;
  181. memcpy(&fsid, mp->m_fixedfsid, sizeof(xfs_fsid_t));
  182. return xfs_errortag_clearall_umount(fsid, mp->m_fsname, 1);
  183. }
  184. #endif /* DEBUG || INDUCE_IO_ERROR */
  185. static void
  186. xfs_fs_vcmn_err(int level, xfs_mount_t *mp, char *fmt, va_list ap)
  187. {
  188. if (mp != NULL) {
  189. char *newfmt;
  190. int len = 16 + mp->m_fsname_len + strlen(fmt);
  191. newfmt = kmem_alloc(len, KM_SLEEP);
  192. sprintf(newfmt, "Filesystem \"%s\": %s", mp->m_fsname, fmt);
  193. icmn_err(level, newfmt, ap);
  194. kmem_free(newfmt, len);
  195. } else {
  196. icmn_err(level, fmt, ap);
  197. }
  198. }
  199. void
  200. xfs_fs_cmn_err(int level, xfs_mount_t *mp, char *fmt, ...)
  201. {
  202. va_list ap;
  203. va_start(ap, fmt);
  204. xfs_fs_vcmn_err(level, mp, fmt, ap);
  205. va_end(ap);
  206. }
  207. void
  208. xfs_cmn_err(int panic_tag, int level, xfs_mount_t *mp, char *fmt, ...)
  209. {
  210. va_list ap;
  211. #ifdef DEBUG
  212. xfs_panic_mask |= XFS_PTAG_SHUTDOWN_CORRUPT;
  213. #endif
  214. if (xfs_panic_mask && (xfs_panic_mask & panic_tag)
  215. && (level & CE_ALERT)) {
  216. level &= ~CE_ALERT;
  217. level |= CE_PANIC;
  218. cmn_err(CE_ALERT, "XFS: Transforming an alert into a BUG.");
  219. }
  220. va_start(ap, fmt);
  221. xfs_fs_vcmn_err(level, mp, fmt, ap);
  222. va_end(ap);
  223. }
  224. void
  225. xfs_error_report(
  226. char *tag,
  227. int level,
  228. xfs_mount_t *mp,
  229. char *fname,
  230. int linenum,
  231. inst_t *ra)
  232. {
  233. if (level <= xfs_error_level) {
  234. xfs_cmn_err(XFS_PTAG_ERROR_REPORT,
  235. CE_ALERT, mp,
  236. "XFS internal error %s at line %d of file %s. Caller 0x%p\n",
  237. tag, linenum, fname, ra);
  238. xfs_stack_trace();
  239. }
  240. }
  241. STATIC void
  242. xfs_hex_dump(void *p, int length)
  243. {
  244. __uint8_t *uip = (__uint8_t*)p;
  245. int i;
  246. char sbuf[128], *s;
  247. s = sbuf;
  248. *s = '\0';
  249. for (i=0; i<length; i++, uip++) {
  250. if ((i % 16) == 0) {
  251. if (*s != '\0')
  252. cmn_err(CE_ALERT, "%s\n", sbuf);
  253. s = sbuf;
  254. sprintf(s, "0x%x: ", i);
  255. while( *s != '\0')
  256. s++;
  257. }
  258. sprintf(s, "%02x ", *uip);
  259. /*
  260. * the kernel sprintf is a void; user sprintf returns
  261. * the sprintf'ed string's length. Find the new end-
  262. * of-string
  263. */
  264. while( *s != '\0')
  265. s++;
  266. }
  267. cmn_err(CE_ALERT, "%s\n", sbuf);
  268. }
  269. void
  270. xfs_corruption_error(
  271. char *tag,
  272. int level,
  273. xfs_mount_t *mp,
  274. void *p,
  275. char *fname,
  276. int linenum,
  277. inst_t *ra)
  278. {
  279. if (level <= xfs_error_level)
  280. xfs_hex_dump(p, 16);
  281. xfs_error_report(tag, level, mp, fname, linenum, ra);
  282. }