xfs_error.c 4.2 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_mount.h"
  24. #include "xfs_error.h"
  25. #ifdef DEBUG
  26. int xfs_etest[XFS_NUM_INJECT_ERROR];
  27. int64_t xfs_etest_fsid[XFS_NUM_INJECT_ERROR];
  28. char * xfs_etest_fsname[XFS_NUM_INJECT_ERROR];
  29. int xfs_error_test_active;
  30. int
  31. xfs_error_test(int error_tag, int *fsidp, char *expression,
  32. int line, char *file, unsigned long randfactor)
  33. {
  34. int i;
  35. int64_t fsid;
  36. if (prandom_u32() % randfactor)
  37. return 0;
  38. memcpy(&fsid, fsidp, sizeof(xfs_fsid_t));
  39. for (i = 0; i < XFS_NUM_INJECT_ERROR; i++) {
  40. if (xfs_etest[i] == error_tag && xfs_etest_fsid[i] == fsid) {
  41. xfs_warn(NULL,
  42. "Injecting error (%s) at file %s, line %d, on filesystem \"%s\"",
  43. expression, file, line, xfs_etest_fsname[i]);
  44. return 1;
  45. }
  46. }
  47. return 0;
  48. }
  49. int
  50. xfs_errortag_add(int error_tag, xfs_mount_t *mp)
  51. {
  52. int i;
  53. int len;
  54. int64_t fsid;
  55. memcpy(&fsid, mp->m_fixedfsid, sizeof(xfs_fsid_t));
  56. for (i = 0; i < XFS_NUM_INJECT_ERROR; i++) {
  57. if (xfs_etest_fsid[i] == fsid && xfs_etest[i] == error_tag) {
  58. xfs_warn(mp, "error tag #%d on", error_tag);
  59. return 0;
  60. }
  61. }
  62. for (i = 0; i < XFS_NUM_INJECT_ERROR; i++) {
  63. if (xfs_etest[i] == 0) {
  64. xfs_warn(mp, "Turned on XFS error tag #%d",
  65. error_tag);
  66. xfs_etest[i] = error_tag;
  67. xfs_etest_fsid[i] = fsid;
  68. len = strlen(mp->m_fsname);
  69. xfs_etest_fsname[i] = kmem_alloc(len + 1, KM_SLEEP);
  70. strcpy(xfs_etest_fsname[i], mp->m_fsname);
  71. xfs_error_test_active++;
  72. return 0;
  73. }
  74. }
  75. xfs_warn(mp, "error tag overflow, too many turned on");
  76. return 1;
  77. }
  78. int
  79. xfs_errortag_clearall(xfs_mount_t *mp, int loud)
  80. {
  81. int64_t fsid;
  82. int cleared = 0;
  83. int i;
  84. memcpy(&fsid, mp->m_fixedfsid, sizeof(xfs_fsid_t));
  85. for (i = 0; i < XFS_NUM_INJECT_ERROR; i++) {
  86. if ((fsid == 0LL || xfs_etest_fsid[i] == fsid) &&
  87. xfs_etest[i] != 0) {
  88. cleared = 1;
  89. xfs_warn(mp, "Clearing XFS error tag #%d",
  90. xfs_etest[i]);
  91. xfs_etest[i] = 0;
  92. xfs_etest_fsid[i] = 0LL;
  93. kmem_free(xfs_etest_fsname[i]);
  94. xfs_etest_fsname[i] = NULL;
  95. xfs_error_test_active--;
  96. }
  97. }
  98. if (loud || cleared)
  99. xfs_warn(mp, "Cleared all XFS error tags for filesystem");
  100. return 0;
  101. }
  102. #endif /* DEBUG */
  103. void
  104. xfs_error_report(
  105. const char *tag,
  106. int level,
  107. struct xfs_mount *mp,
  108. const char *filename,
  109. int linenum,
  110. void *ra)
  111. {
  112. if (level <= xfs_error_level) {
  113. xfs_alert_tag(mp, XFS_PTAG_ERROR_REPORT,
  114. "Internal error %s at line %d of file %s. Caller %pS",
  115. tag, linenum, filename, ra);
  116. xfs_stack_trace();
  117. }
  118. }
  119. void
  120. xfs_corruption_error(
  121. const char *tag,
  122. int level,
  123. struct xfs_mount *mp,
  124. void *p,
  125. const char *filename,
  126. int linenum,
  127. void *ra)
  128. {
  129. if (level <= xfs_error_level)
  130. xfs_hex_dump(p, 64);
  131. xfs_error_report(tag, level, mp, filename, linenum, ra);
  132. xfs_alert(mp, "Corruption detected. Unmount and run xfs_repair");
  133. }
  134. /*
  135. * Warnings specifically for verifier errors. Differentiate CRC vs. invalid
  136. * values, and omit the stack trace unless the error level is tuned high.
  137. */
  138. void
  139. xfs_verifier_error(
  140. struct xfs_buf *bp)
  141. {
  142. struct xfs_mount *mp = bp->b_target->bt_mount;
  143. xfs_alert(mp, "Metadata %s detected at %pF, %s block 0x%llx",
  144. bp->b_error == -EFSBADCRC ? "CRC error" : "corruption",
  145. __return_address, bp->b_ops->name, bp->b_bn);
  146. xfs_alert(mp, "Unmount and run xfs_repair");
  147. if (xfs_error_level >= XFS_ERRLEVEL_LOW) {
  148. xfs_alert(mp, "First 64 bytes of corrupted metadata buffer:");
  149. xfs_hex_dump(xfs_buf_offset(bp, 0), 64);
  150. }
  151. if (xfs_error_level >= XFS_ERRLEVEL_HIGH)
  152. xfs_stack_trace();
  153. }