health.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2019-2023 Oracle. All Rights Reserved.
  4. * Author: Darrick J. Wong <djwong@kernel.org>
  5. */
  6. #include "xfs.h"
  7. #include "xfs_fs.h"
  8. #include "xfs_shared.h"
  9. #include "xfs_format.h"
  10. #include "xfs_trans_resv.h"
  11. #include "xfs_mount.h"
  12. #include "xfs_btree.h"
  13. #include "xfs_ag.h"
  14. #include "xfs_health.h"
  15. #include "scrub/scrub.h"
  16. #include "scrub/health.h"
  17. #include "scrub/common.h"
  18. /*
  19. * Scrub and In-Core Filesystem Health Assessments
  20. * ===============================================
  21. *
  22. * Online scrub and repair have the time and the ability to perform stronger
  23. * checks than we can do from the metadata verifiers, because they can
  24. * cross-reference records between data structures. Therefore, scrub is in a
  25. * good position to update the online filesystem health assessments to reflect
  26. * the good/bad state of the data structure.
  27. *
  28. * We therefore extend scrub in the following ways to achieve this:
  29. *
  30. * 1. Create a "sick_mask" field in the scrub context. When we're setting up a
  31. * scrub call, set this to the default XFS_SICK_* flag(s) for the selected
  32. * scrub type (call it A). Scrub and repair functions can override the default
  33. * sick_mask value if they choose.
  34. *
  35. * 2. If the scrubber returns a runtime error code, we exit making no changes
  36. * to the incore sick state.
  37. *
  38. * 3. If the scrubber finds that A is clean, use sick_mask to clear the incore
  39. * sick flags before exiting.
  40. *
  41. * 4. If the scrubber finds that A is corrupt, use sick_mask to set the incore
  42. * sick flags. If the user didn't want to repair then we exit, leaving the
  43. * metadata structure unfixed and the sick flag set.
  44. *
  45. * 5. Now we know that A is corrupt and the user wants to repair, so run the
  46. * repairer. If the repairer returns an error code, we exit with that error
  47. * code, having made no further changes to the incore sick state.
  48. *
  49. * 6. If repair rebuilds A correctly and the subsequent re-scrub of A is clean,
  50. * use sick_mask to clear the incore sick flags. This should have the effect
  51. * that A is no longer marked sick.
  52. *
  53. * 7. If repair rebuilds A incorrectly, the re-scrub will find it corrupt and
  54. * use sick_mask to set the incore sick flags. This should have no externally
  55. * visible effect since we already set them in step (4).
  56. *
  57. * There are some complications to this story, however. For certain types of
  58. * complementary metadata indices (e.g. inobt/finobt), it is easier to rebuild
  59. * both structures at the same time. The following principles apply to this
  60. * type of repair strategy:
  61. *
  62. * 8. Any repair function that rebuilds multiple structures should update
  63. * sick_mask_visible to reflect whatever other structures are rebuilt, and
  64. * verify that all the rebuilt structures can pass a scrub check. The outcomes
  65. * of 5-7 still apply, but with a sick_mask that covers everything being
  66. * rebuilt.
  67. */
  68. /* Map our scrub type to a sick mask and a set of health update functions. */
  69. enum xchk_health_group {
  70. XHG_FS = 1,
  71. XHG_RT,
  72. XHG_AG,
  73. XHG_INO,
  74. };
  75. struct xchk_health_map {
  76. enum xchk_health_group group;
  77. unsigned int sick_mask;
  78. };
  79. static const struct xchk_health_map type_to_health_flag[XFS_SCRUB_TYPE_NR] = {
  80. [XFS_SCRUB_TYPE_SB] = { XHG_AG, XFS_SICK_AG_SB },
  81. [XFS_SCRUB_TYPE_AGF] = { XHG_AG, XFS_SICK_AG_AGF },
  82. [XFS_SCRUB_TYPE_AGFL] = { XHG_AG, XFS_SICK_AG_AGFL },
  83. [XFS_SCRUB_TYPE_AGI] = { XHG_AG, XFS_SICK_AG_AGI },
  84. [XFS_SCRUB_TYPE_BNOBT] = { XHG_AG, XFS_SICK_AG_BNOBT },
  85. [XFS_SCRUB_TYPE_CNTBT] = { XHG_AG, XFS_SICK_AG_CNTBT },
  86. [XFS_SCRUB_TYPE_INOBT] = { XHG_AG, XFS_SICK_AG_INOBT },
  87. [XFS_SCRUB_TYPE_FINOBT] = { XHG_AG, XFS_SICK_AG_FINOBT },
  88. [XFS_SCRUB_TYPE_RMAPBT] = { XHG_AG, XFS_SICK_AG_RMAPBT },
  89. [XFS_SCRUB_TYPE_REFCNTBT] = { XHG_AG, XFS_SICK_AG_REFCNTBT },
  90. [XFS_SCRUB_TYPE_INODE] = { XHG_INO, XFS_SICK_INO_CORE },
  91. [XFS_SCRUB_TYPE_BMBTD] = { XHG_INO, XFS_SICK_INO_BMBTD },
  92. [XFS_SCRUB_TYPE_BMBTA] = { XHG_INO, XFS_SICK_INO_BMBTA },
  93. [XFS_SCRUB_TYPE_BMBTC] = { XHG_INO, XFS_SICK_INO_BMBTC },
  94. [XFS_SCRUB_TYPE_DIR] = { XHG_INO, XFS_SICK_INO_DIR },
  95. [XFS_SCRUB_TYPE_XATTR] = { XHG_INO, XFS_SICK_INO_XATTR },
  96. [XFS_SCRUB_TYPE_SYMLINK] = { XHG_INO, XFS_SICK_INO_SYMLINK },
  97. [XFS_SCRUB_TYPE_PARENT] = { XHG_INO, XFS_SICK_INO_PARENT },
  98. [XFS_SCRUB_TYPE_RTBITMAP] = { XHG_RT, XFS_SICK_RT_BITMAP },
  99. [XFS_SCRUB_TYPE_RTSUM] = { XHG_RT, XFS_SICK_RT_SUMMARY },
  100. [XFS_SCRUB_TYPE_UQUOTA] = { XHG_FS, XFS_SICK_FS_UQUOTA },
  101. [XFS_SCRUB_TYPE_GQUOTA] = { XHG_FS, XFS_SICK_FS_GQUOTA },
  102. [XFS_SCRUB_TYPE_PQUOTA] = { XHG_FS, XFS_SICK_FS_PQUOTA },
  103. [XFS_SCRUB_TYPE_FSCOUNTERS] = { XHG_FS, XFS_SICK_FS_COUNTERS },
  104. [XFS_SCRUB_TYPE_QUOTACHECK] = { XHG_FS, XFS_SICK_FS_QUOTACHECK },
  105. [XFS_SCRUB_TYPE_NLINKS] = { XHG_FS, XFS_SICK_FS_NLINKS },
  106. [XFS_SCRUB_TYPE_DIRTREE] = { XHG_INO, XFS_SICK_INO_DIRTREE },
  107. };
  108. /* Return the health status mask for this scrub type. */
  109. unsigned int
  110. xchk_health_mask_for_scrub_type(
  111. __u32 scrub_type)
  112. {
  113. return type_to_health_flag[scrub_type].sick_mask;
  114. }
  115. /*
  116. * If the scrub state is clean, add @mask to the scrub sick mask to clear
  117. * additional sick flags from the metadata object's sick state.
  118. */
  119. void
  120. xchk_mark_healthy_if_clean(
  121. struct xfs_scrub *sc,
  122. unsigned int mask)
  123. {
  124. if (!(sc->sm->sm_flags & (XFS_SCRUB_OFLAG_CORRUPT |
  125. XFS_SCRUB_OFLAG_XCORRUPT)))
  126. sc->sick_mask |= mask;
  127. }
  128. /*
  129. * If we're scrubbing a piece of file metadata for the first time, does it look
  130. * like it has been zapped? Skip the check if we just repaired the metadata
  131. * and are revalidating it.
  132. */
  133. bool
  134. xchk_file_looks_zapped(
  135. struct xfs_scrub *sc,
  136. unsigned int mask)
  137. {
  138. ASSERT((mask & ~XFS_SICK_INO_ZAPPED) == 0);
  139. if (sc->flags & XREP_ALREADY_FIXED)
  140. return false;
  141. return xfs_inode_has_sickness(sc->ip, mask);
  142. }
  143. /*
  144. * Scrub gave the filesystem a clean bill of health, so clear all the indirect
  145. * markers of past problems (at least for the fs and ags) so that we can be
  146. * healthy again.
  147. */
  148. STATIC void
  149. xchk_mark_all_healthy(
  150. struct xfs_mount *mp)
  151. {
  152. struct xfs_perag *pag;
  153. xfs_agnumber_t agno;
  154. xfs_fs_mark_healthy(mp, XFS_SICK_FS_INDIRECT);
  155. xfs_rt_mark_healthy(mp, XFS_SICK_RT_INDIRECT);
  156. for_each_perag(mp, agno, pag)
  157. xfs_ag_mark_healthy(pag, XFS_SICK_AG_INDIRECT);
  158. }
  159. /*
  160. * Update filesystem health assessments based on what we found and did.
  161. *
  162. * If the scrubber finds errors, we mark sick whatever's mentioned in
  163. * sick_mask, no matter whether this is a first scan or an
  164. * evaluation of repair effectiveness.
  165. *
  166. * Otherwise, no direct corruption was found, so mark whatever's in
  167. * sick_mask as healthy.
  168. */
  169. void
  170. xchk_update_health(
  171. struct xfs_scrub *sc)
  172. {
  173. struct xfs_perag *pag;
  174. bool bad;
  175. /*
  176. * The HEALTHY scrub type is a request from userspace to clear all the
  177. * indirect flags after a clean scan of the entire filesystem. As such
  178. * there's no sick flag defined for it, so we branch here ahead of the
  179. * mask check.
  180. */
  181. if (sc->sm->sm_type == XFS_SCRUB_TYPE_HEALTHY &&
  182. !(sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)) {
  183. xchk_mark_all_healthy(sc->mp);
  184. return;
  185. }
  186. if (!sc->sick_mask)
  187. return;
  188. bad = (sc->sm->sm_flags & (XFS_SCRUB_OFLAG_CORRUPT |
  189. XFS_SCRUB_OFLAG_XCORRUPT));
  190. switch (type_to_health_flag[sc->sm->sm_type].group) {
  191. case XHG_AG:
  192. pag = xfs_perag_get(sc->mp, sc->sm->sm_agno);
  193. if (bad)
  194. xfs_ag_mark_corrupt(pag, sc->sick_mask);
  195. else
  196. xfs_ag_mark_healthy(pag, sc->sick_mask);
  197. xfs_perag_put(pag);
  198. break;
  199. case XHG_INO:
  200. if (!sc->ip)
  201. return;
  202. if (bad) {
  203. unsigned int mask = sc->sick_mask;
  204. /*
  205. * If we're coming in for repairs then we don't want
  206. * sickness flags to propagate to the incore health
  207. * status if the inode gets inactivated before we can
  208. * fix it.
  209. */
  210. if (sc->sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR)
  211. mask |= XFS_SICK_INO_FORGET;
  212. xfs_inode_mark_corrupt(sc->ip, mask);
  213. } else
  214. xfs_inode_mark_healthy(sc->ip, sc->sick_mask);
  215. break;
  216. case XHG_FS:
  217. if (bad)
  218. xfs_fs_mark_corrupt(sc->mp, sc->sick_mask);
  219. else
  220. xfs_fs_mark_healthy(sc->mp, sc->sick_mask);
  221. break;
  222. case XHG_RT:
  223. if (bad)
  224. xfs_rt_mark_corrupt(sc->mp, sc->sick_mask);
  225. else
  226. xfs_rt_mark_healthy(sc->mp, sc->sick_mask);
  227. break;
  228. default:
  229. ASSERT(0);
  230. break;
  231. }
  232. }
  233. /* Is the given per-AG btree healthy enough for scanning? */
  234. void
  235. xchk_ag_btree_del_cursor_if_sick(
  236. struct xfs_scrub *sc,
  237. struct xfs_btree_cur **curp,
  238. unsigned int sm_type)
  239. {
  240. unsigned int mask = (*curp)->bc_ops->sick_mask;
  241. /*
  242. * We always want the cursor if it's the same type as whatever we're
  243. * scrubbing, even if we already know the structure is corrupt.
  244. *
  245. * Otherwise, we're only interested in the btree for cross-referencing.
  246. * If we know the btree is bad then don't bother, just set XFAIL.
  247. */
  248. if (sc->sm->sm_type == sm_type)
  249. return;
  250. /*
  251. * If we just repaired some AG metadata, sc->sick_mask will reflect all
  252. * the per-AG metadata types that were repaired. Exclude these from
  253. * the filesystem health query because we have not yet updated the
  254. * health status and we want everything to be scanned.
  255. */
  256. if ((sc->flags & XREP_ALREADY_FIXED) &&
  257. type_to_health_flag[sc->sm->sm_type].group == XHG_AG)
  258. mask &= ~sc->sick_mask;
  259. if (xfs_ag_has_sickness((*curp)->bc_ag.pag, mask)) {
  260. sc->sm->sm_flags |= XFS_SCRUB_OFLAG_XFAIL;
  261. xfs_btree_del_cursor(*curp, XFS_BTREE_NOERROR);
  262. *curp = NULL;
  263. }
  264. }
  265. /*
  266. * Quick scan to double-check that there isn't any evidence of lingering
  267. * primary health problems. If we're still clear, then the health update will
  268. * take care of clearing the indirect evidence.
  269. */
  270. int
  271. xchk_health_record(
  272. struct xfs_scrub *sc)
  273. {
  274. struct xfs_mount *mp = sc->mp;
  275. struct xfs_perag *pag;
  276. xfs_agnumber_t agno;
  277. unsigned int sick;
  278. unsigned int checked;
  279. xfs_fs_measure_sickness(mp, &sick, &checked);
  280. if (sick & XFS_SICK_FS_PRIMARY)
  281. xchk_set_corrupt(sc);
  282. xfs_rt_measure_sickness(mp, &sick, &checked);
  283. if (sick & XFS_SICK_RT_PRIMARY)
  284. xchk_set_corrupt(sc);
  285. for_each_perag(mp, agno, pag) {
  286. xfs_ag_measure_sickness(pag, &sick, &checked);
  287. if (sick & XFS_SICK_AG_PRIMARY)
  288. xchk_set_corrupt(sc);
  289. }
  290. return 0;
  291. }