xfs_error.c 4.0 KB

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