xfs_error.c 4.0 KB

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