xfs_error.c 5.8 KB

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