health.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2019 Oracle. All Rights Reserved.
  4. * Author: Darrick J. Wong <[email protected]>
  5. */
  6. #include "xfs.h"
  7. #include "xfs_fs.h"
  8. #include "xfs_shared.h"
  9. #include "xfs_format.h"
  10. #include "xfs_btree.h"
  11. #include "xfs_trans_resv.h"
  12. #include "xfs_mount.h"
  13. #include "xfs_ag.h"
  14. #include "xfs_health.h"
  15. #include "scrub/scrub.h"
  16. #include "scrub/health.h"
  17. /*
  18. * Scrub and In-Core Filesystem Health Assessments
  19. * ===============================================
  20. *
  21. * Online scrub and repair have the time and the ability to perform stronger
  22. * checks than we can do from the metadata verifiers, because they can
  23. * cross-reference records between data structures. Therefore, scrub is in a
  24. * good position to update the online filesystem health assessments to reflect
  25. * the good/bad state of the data structure.
  26. *
  27. * We therefore extend scrub in the following ways to achieve this:
  28. *
  29. * 1. Create a "sick_mask" field in the scrub context. When we're setting up a
  30. * scrub call, set this to the default XFS_SICK_* flag(s) for the selected
  31. * scrub type (call it A). Scrub and repair functions can override the default
  32. * sick_mask value if they choose.
  33. *
  34. * 2. If the scrubber returns a runtime error code, we exit making no changes
  35. * to the incore sick state.
  36. *
  37. * 3. If the scrubber finds that A is clean, use sick_mask to clear the incore
  38. * sick flags before exiting.
  39. *
  40. * 4. If the scrubber finds that A is corrupt, use sick_mask to set the incore
  41. * sick flags. If the user didn't want to repair then we exit, leaving the
  42. * metadata structure unfixed and the sick flag set.
  43. *
  44. * 5. Now we know that A is corrupt and the user wants to repair, so run the
  45. * repairer. If the repairer returns an error code, we exit with that error
  46. * code, having made no further changes to the incore sick state.
  47. *
  48. * 6. If repair rebuilds A correctly and the subsequent re-scrub of A is clean,
  49. * use sick_mask to clear the incore sick flags. This should have the effect
  50. * that A is no longer marked sick.
  51. *
  52. * 7. If repair rebuilds A incorrectly, the re-scrub will find it corrupt and
  53. * use sick_mask to set the incore sick flags. This should have no externally
  54. * visible effect since we already set them in step (4).
  55. *
  56. * There are some complications to this story, however. For certain types of
  57. * complementary metadata indices (e.g. inobt/finobt), it is easier to rebuild
  58. * both structures at the same time. The following principles apply to this
  59. * type of repair strategy:
  60. *
  61. * 8. Any repair function that rebuilds multiple structures should update
  62. * sick_mask_visible to reflect whatever other structures are rebuilt, and
  63. * verify that all the rebuilt structures can pass a scrub check. The outcomes
  64. * of 5-7 still apply, but with a sick_mask that covers everything being
  65. * rebuilt.
  66. */
  67. /* Map our scrub type to a sick mask and a set of health update functions. */
  68. enum xchk_health_group {
  69. XHG_FS = 1,
  70. XHG_RT,
  71. XHG_AG,
  72. XHG_INO,
  73. };
  74. struct xchk_health_map {
  75. enum xchk_health_group group;
  76. unsigned int sick_mask;
  77. };
  78. static const struct xchk_health_map type_to_health_flag[XFS_SCRUB_TYPE_NR] = {
  79. [XFS_SCRUB_TYPE_SB] = { XHG_AG, XFS_SICK_AG_SB },
  80. [XFS_SCRUB_TYPE_AGF] = { XHG_AG, XFS_SICK_AG_AGF },
  81. [XFS_SCRUB_TYPE_AGFL] = { XHG_AG, XFS_SICK_AG_AGFL },
  82. [XFS_SCRUB_TYPE_AGI] = { XHG_AG, XFS_SICK_AG_AGI },
  83. [XFS_SCRUB_TYPE_BNOBT] = { XHG_AG, XFS_SICK_AG_BNOBT },
  84. [XFS_SCRUB_TYPE_CNTBT] = { XHG_AG, XFS_SICK_AG_CNTBT },
  85. [XFS_SCRUB_TYPE_INOBT] = { XHG_AG, XFS_SICK_AG_INOBT },
  86. [XFS_SCRUB_TYPE_FINOBT] = { XHG_AG, XFS_SICK_AG_FINOBT },
  87. [XFS_SCRUB_TYPE_RMAPBT] = { XHG_AG, XFS_SICK_AG_RMAPBT },
  88. [XFS_SCRUB_TYPE_REFCNTBT] = { XHG_AG, XFS_SICK_AG_REFCNTBT },
  89. [XFS_SCRUB_TYPE_INODE] = { XHG_INO, XFS_SICK_INO_CORE },
  90. [XFS_SCRUB_TYPE_BMBTD] = { XHG_INO, XFS_SICK_INO_BMBTD },
  91. [XFS_SCRUB_TYPE_BMBTA] = { XHG_INO, XFS_SICK_INO_BMBTA },
  92. [XFS_SCRUB_TYPE_BMBTC] = { XHG_INO, XFS_SICK_INO_BMBTC },
  93. [XFS_SCRUB_TYPE_DIR] = { XHG_INO, XFS_SICK_INO_DIR },
  94. [XFS_SCRUB_TYPE_XATTR] = { XHG_INO, XFS_SICK_INO_XATTR },
  95. [XFS_SCRUB_TYPE_SYMLINK] = { XHG_INO, XFS_SICK_INO_SYMLINK },
  96. [XFS_SCRUB_TYPE_PARENT] = { XHG_INO, XFS_SICK_INO_PARENT },
  97. [XFS_SCRUB_TYPE_RTBITMAP] = { XHG_RT, XFS_SICK_RT_BITMAP },
  98. [XFS_SCRUB_TYPE_RTSUM] = { XHG_RT, XFS_SICK_RT_SUMMARY },
  99. [XFS_SCRUB_TYPE_UQUOTA] = { XHG_FS, XFS_SICK_FS_UQUOTA },
  100. [XFS_SCRUB_TYPE_GQUOTA] = { XHG_FS, XFS_SICK_FS_GQUOTA },
  101. [XFS_SCRUB_TYPE_PQUOTA] = { XHG_FS, XFS_SICK_FS_PQUOTA },
  102. [XFS_SCRUB_TYPE_FSCOUNTERS] = { XHG_FS, XFS_SICK_FS_COUNTERS },
  103. };
  104. /* Return the health status mask for this scrub type. */
  105. unsigned int
  106. xchk_health_mask_for_scrub_type(
  107. __u32 scrub_type)
  108. {
  109. return type_to_health_flag[scrub_type].sick_mask;
  110. }
  111. /*
  112. * Update filesystem health assessments based on what we found and did.
  113. *
  114. * If the scrubber finds errors, we mark sick whatever's mentioned in
  115. * sick_mask, no matter whether this is a first scan or an
  116. * evaluation of repair effectiveness.
  117. *
  118. * Otherwise, no direct corruption was found, so mark whatever's in
  119. * sick_mask as healthy.
  120. */
  121. void
  122. xchk_update_health(
  123. struct xfs_scrub *sc)
  124. {
  125. struct xfs_perag *pag;
  126. bool bad;
  127. if (!sc->sick_mask)
  128. return;
  129. bad = (sc->sm->sm_flags & (XFS_SCRUB_OFLAG_CORRUPT |
  130. XFS_SCRUB_OFLAG_XCORRUPT));
  131. switch (type_to_health_flag[sc->sm->sm_type].group) {
  132. case XHG_AG:
  133. pag = xfs_perag_get(sc->mp, sc->sm->sm_agno);
  134. if (bad)
  135. xfs_ag_mark_sick(pag, sc->sick_mask);
  136. else
  137. xfs_ag_mark_healthy(pag, sc->sick_mask);
  138. xfs_perag_put(pag);
  139. break;
  140. case XHG_INO:
  141. if (!sc->ip)
  142. return;
  143. if (bad)
  144. xfs_inode_mark_sick(sc->ip, sc->sick_mask);
  145. else
  146. xfs_inode_mark_healthy(sc->ip, sc->sick_mask);
  147. break;
  148. case XHG_FS:
  149. if (bad)
  150. xfs_fs_mark_sick(sc->mp, sc->sick_mask);
  151. else
  152. xfs_fs_mark_healthy(sc->mp, sc->sick_mask);
  153. break;
  154. case XHG_RT:
  155. if (bad)
  156. xfs_rt_mark_sick(sc->mp, sc->sick_mask);
  157. else
  158. xfs_rt_mark_healthy(sc->mp, sc->sick_mask);
  159. break;
  160. default:
  161. ASSERT(0);
  162. break;
  163. }
  164. }
  165. /* Is the given per-AG btree healthy enough for scanning? */
  166. bool
  167. xchk_ag_btree_healthy_enough(
  168. struct xfs_scrub *sc,
  169. struct xfs_perag *pag,
  170. xfs_btnum_t btnum)
  171. {
  172. unsigned int mask = 0;
  173. /*
  174. * We always want the cursor if it's the same type as whatever we're
  175. * scrubbing, even if we already know the structure is corrupt.
  176. *
  177. * Otherwise, we're only interested in the btree for cross-referencing.
  178. * If we know the btree is bad then don't bother, just set XFAIL.
  179. */
  180. switch (btnum) {
  181. case XFS_BTNUM_BNO:
  182. if (sc->sm->sm_type == XFS_SCRUB_TYPE_BNOBT)
  183. return true;
  184. mask = XFS_SICK_AG_BNOBT;
  185. break;
  186. case XFS_BTNUM_CNT:
  187. if (sc->sm->sm_type == XFS_SCRUB_TYPE_CNTBT)
  188. return true;
  189. mask = XFS_SICK_AG_CNTBT;
  190. break;
  191. case XFS_BTNUM_INO:
  192. if (sc->sm->sm_type == XFS_SCRUB_TYPE_INOBT)
  193. return true;
  194. mask = XFS_SICK_AG_INOBT;
  195. break;
  196. case XFS_BTNUM_FINO:
  197. if (sc->sm->sm_type == XFS_SCRUB_TYPE_FINOBT)
  198. return true;
  199. mask = XFS_SICK_AG_FINOBT;
  200. break;
  201. case XFS_BTNUM_RMAP:
  202. if (sc->sm->sm_type == XFS_SCRUB_TYPE_RMAPBT)
  203. return true;
  204. mask = XFS_SICK_AG_RMAPBT;
  205. break;
  206. case XFS_BTNUM_REFC:
  207. if (sc->sm->sm_type == XFS_SCRUB_TYPE_REFCNTBT)
  208. return true;
  209. mask = XFS_SICK_AG_REFCNTBT;
  210. break;
  211. default:
  212. ASSERT(0);
  213. return true;
  214. }
  215. if (xfs_ag_has_sickness(pag, mask)) {
  216. sc->sm->sm_flags |= XFS_SCRUB_OFLAG_XFAIL;
  217. return false;
  218. }
  219. return true;
  220. }