xfs_error.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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_mount.h"
  27. #include "xfs_bmap_btree.h"
  28. #include "xfs_dinode.h"
  29. #include "xfs_inode.h"
  30. #include "xfs_utils.h"
  31. #include "xfs_error.h"
  32. #ifdef DEBUG
  33. int xfs_etrap[XFS_ERROR_NTRAP] = {
  34. 0,
  35. };
  36. int
  37. xfs_error_trap(int e)
  38. {
  39. int i;
  40. if (!e)
  41. return 0;
  42. for (i = 0; i < XFS_ERROR_NTRAP; i++) {
  43. if (xfs_etrap[i] == 0)
  44. break;
  45. if (e != xfs_etrap[i])
  46. continue;
  47. xfs_notice(NULL, "%s: error %d", __func__, e);
  48. BUG();
  49. break;
  50. }
  51. return e;
  52. }
  53. int xfs_etest[XFS_NUM_INJECT_ERROR];
  54. int64_t xfs_etest_fsid[XFS_NUM_INJECT_ERROR];
  55. char * xfs_etest_fsname[XFS_NUM_INJECT_ERROR];
  56. int xfs_error_test_active;
  57. int
  58. xfs_error_test(int error_tag, int *fsidp, char *expression,
  59. int line, char *file, unsigned long randfactor)
  60. {
  61. int i;
  62. int64_t fsid;
  63. if (random32() % randfactor)
  64. return 0;
  65. memcpy(&fsid, fsidp, sizeof(xfs_fsid_t));
  66. for (i = 0; i < XFS_NUM_INJECT_ERROR; i++) {
  67. if (xfs_etest[i] == error_tag && xfs_etest_fsid[i] == fsid) {
  68. xfs_warn(NULL,
  69. "Injecting error (%s) at file %s, line %d, on filesystem \"%s\"",
  70. expression, file, line, xfs_etest_fsname[i]);
  71. return 1;
  72. }
  73. }
  74. return 0;
  75. }
  76. int
  77. xfs_errortag_add(int error_tag, xfs_mount_t *mp)
  78. {
  79. int i;
  80. int len;
  81. int64_t fsid;
  82. memcpy(&fsid, mp->m_fixedfsid, sizeof(xfs_fsid_t));
  83. for (i = 0; i < XFS_NUM_INJECT_ERROR; i++) {
  84. if (xfs_etest_fsid[i] == fsid && xfs_etest[i] == error_tag) {
  85. xfs_warn(mp, "error tag #%d on", error_tag);
  86. return 0;
  87. }
  88. }
  89. for (i = 0; i < XFS_NUM_INJECT_ERROR; i++) {
  90. if (xfs_etest[i] == 0) {
  91. xfs_warn(mp, "Turned on XFS error tag #%d",
  92. error_tag);
  93. xfs_etest[i] = error_tag;
  94. xfs_etest_fsid[i] = fsid;
  95. len = strlen(mp->m_fsname);
  96. xfs_etest_fsname[i] = kmem_alloc(len + 1, KM_SLEEP);
  97. strcpy(xfs_etest_fsname[i], mp->m_fsname);
  98. xfs_error_test_active++;
  99. return 0;
  100. }
  101. }
  102. xfs_warn(mp, "error tag overflow, too many turned on");
  103. return 1;
  104. }
  105. int
  106. xfs_errortag_clearall(xfs_mount_t *mp, int loud)
  107. {
  108. int64_t fsid;
  109. int cleared = 0;
  110. int i;
  111. memcpy(&fsid, mp->m_fixedfsid, sizeof(xfs_fsid_t));
  112. for (i = 0; i < XFS_NUM_INJECT_ERROR; i++) {
  113. if ((fsid == 0LL || xfs_etest_fsid[i] == fsid) &&
  114. xfs_etest[i] != 0) {
  115. cleared = 1;
  116. xfs_warn(mp, "Clearing XFS error tag #%d",
  117. xfs_etest[i]);
  118. xfs_etest[i] = 0;
  119. xfs_etest_fsid[i] = 0LL;
  120. kmem_free(xfs_etest_fsname[i]);
  121. xfs_etest_fsname[i] = NULL;
  122. xfs_error_test_active--;
  123. }
  124. }
  125. if (loud || cleared)
  126. xfs_warn(mp, "Cleared all XFS error tags for filesystem");
  127. return 0;
  128. }
  129. #endif /* DEBUG */
  130. void
  131. xfs_error_report(
  132. const char *tag,
  133. int level,
  134. struct xfs_mount *mp,
  135. const char *filename,
  136. int linenum,
  137. inst_t *ra)
  138. {
  139. if (level <= xfs_error_level) {
  140. xfs_alert_tag(mp, XFS_PTAG_ERROR_REPORT,
  141. "Internal error %s at line %d of file %s. Caller 0x%p\n",
  142. tag, linenum, filename, ra);
  143. xfs_stack_trace();
  144. }
  145. }
  146. void
  147. xfs_corruption_error(
  148. const char *tag,
  149. int level,
  150. struct xfs_mount *mp,
  151. void *p,
  152. const char *filename,
  153. int linenum,
  154. inst_t *ra)
  155. {
  156. if (level <= xfs_error_level)
  157. xfs_hex_dump(p, 16);
  158. xfs_error_report(tag, level, mp, filename, linenum, ra);
  159. xfs_alert(mp, "Corruption detected. Unmount and run xfs_repair");
  160. }