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