xfs_error.c 3.9 KB

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