LCOV - code coverage report
Current view: top level - fs/xfs/scrub - health.c (source / functions) Hit Total Coverage
Test: fstests of 6.5.0-rc4-xfsx @ Mon Jul 31 20:08:34 PDT 2023 Lines: 90 109 82.6 %
Date: 2023-07-31 20:08:34 Functions: 5 5 100.0 %

          Line data    Source code
       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_trans_resv.h"
      14             : #include "xfs_mount.h"
      15             : #include "xfs_ag.h"
      16             : #include "xfs_health.h"
      17             : #include "xfs_rtgroup.h"
      18             : #include "scrub/scrub.h"
      19             : #include "scrub/health.h"
      20             : #include "scrub/common.h"
      21             : 
      22             : /*
      23             :  * Scrub and In-Core Filesystem Health Assessments
      24             :  * ===============================================
      25             :  *
      26             :  * Online scrub and repair have the time and the ability to perform stronger
      27             :  * checks than we can do from the metadata verifiers, because they can
      28             :  * cross-reference records between data structures.  Therefore, scrub is in a
      29             :  * good position to update the online filesystem health assessments to reflect
      30             :  * the good/bad state of the data structure.
      31             :  *
      32             :  * We therefore extend scrub in the following ways to achieve this:
      33             :  *
      34             :  * 1. Create a "sick_mask" field in the scrub context.  When we're setting up a
      35             :  * scrub call, set this to the default XFS_SICK_* flag(s) for the selected
      36             :  * scrub type (call it A).  Scrub and repair functions can override the default
      37             :  * sick_mask value if they choose.
      38             :  *
      39             :  * 2. If the scrubber returns a runtime error code, we exit making no changes
      40             :  * to the incore sick state.
      41             :  *
      42             :  * 3. If the scrubber finds that A is clean, use sick_mask to clear the incore
      43             :  * sick flags before exiting.
      44             :  *
      45             :  * 4. If the scrubber finds that A is corrupt, use sick_mask to set the incore
      46             :  * sick flags.  If the user didn't want to repair then we exit, leaving the
      47             :  * metadata structure unfixed and the sick flag set.
      48             :  *
      49             :  * 5. Now we know that A is corrupt and the user wants to repair, so run the
      50             :  * repairer.  If the repairer returns an error code, we exit with that error
      51             :  * code, having made no further changes to the incore sick state.
      52             :  *
      53             :  * 6. If repair rebuilds A correctly and the subsequent re-scrub of A is clean,
      54             :  * use sick_mask to clear the incore sick flags.  This should have the effect
      55             :  * that A is no longer marked sick.
      56             :  *
      57             :  * 7. If repair rebuilds A incorrectly, the re-scrub will find it corrupt and
      58             :  * use sick_mask to set the incore sick flags.  This should have no externally
      59             :  * visible effect since we already set them in step (4).
      60             :  *
      61             :  * There are some complications to this story, however.  For certain types of
      62             :  * complementary metadata indices (e.g. inobt/finobt), it is easier to rebuild
      63             :  * both structures at the same time.  The following principles apply to this
      64             :  * type of repair strategy:
      65             :  *
      66             :  * 8. Any repair function that rebuilds multiple structures should update
      67             :  * sick_mask_visible to reflect whatever other structures are rebuilt, and
      68             :  * verify that all the rebuilt structures can pass a scrub check.  The outcomes
      69             :  * of 5-7 still apply, but with a sick_mask that covers everything being
      70             :  * rebuilt.
      71             :  */
      72             : 
      73             : /* Map our scrub type to a sick mask and a set of health update functions. */
      74             : 
      75             : enum xchk_health_group {
      76             :         XHG_FS = 1,
      77             :         XHG_RT,
      78             :         XHG_AG,
      79             :         XHG_INO,
      80             :         XHG_RTGROUP,
      81             : };
      82             : 
      83             : struct xchk_health_map {
      84             :         enum xchk_health_group  group;
      85             :         unsigned int            sick_mask;
      86             : };
      87             : 
      88             : static const struct xchk_health_map type_to_health_flag[XFS_SCRUB_TYPE_NR] = {
      89             :         [XFS_SCRUB_TYPE_SB]             = { XHG_AG,  XFS_SICK_AG_SB },
      90             :         [XFS_SCRUB_TYPE_AGF]            = { XHG_AG,  XFS_SICK_AG_AGF },
      91             :         [XFS_SCRUB_TYPE_AGFL]           = { XHG_AG,  XFS_SICK_AG_AGFL },
      92             :         [XFS_SCRUB_TYPE_AGI]            = { XHG_AG,  XFS_SICK_AG_AGI },
      93             :         [XFS_SCRUB_TYPE_BNOBT]          = { XHG_AG,  XFS_SICK_AG_BNOBT },
      94             :         [XFS_SCRUB_TYPE_CNTBT]          = { XHG_AG,  XFS_SICK_AG_CNTBT },
      95             :         [XFS_SCRUB_TYPE_INOBT]          = { XHG_AG,  XFS_SICK_AG_INOBT },
      96             :         [XFS_SCRUB_TYPE_FINOBT]         = { XHG_AG,  XFS_SICK_AG_FINOBT },
      97             :         [XFS_SCRUB_TYPE_RMAPBT]         = { XHG_AG,  XFS_SICK_AG_RMAPBT },
      98             :         [XFS_SCRUB_TYPE_REFCNTBT]       = { XHG_AG,  XFS_SICK_AG_REFCNTBT },
      99             :         [XFS_SCRUB_TYPE_INODE]          = { XHG_INO, XFS_SICK_INO_CORE },
     100             :         [XFS_SCRUB_TYPE_BMBTD]          = { XHG_INO, XFS_SICK_INO_BMBTD },
     101             :         [XFS_SCRUB_TYPE_BMBTA]          = { XHG_INO, XFS_SICK_INO_BMBTA },
     102             :         [XFS_SCRUB_TYPE_BMBTC]          = { XHG_INO, XFS_SICK_INO_BMBTC },
     103             :         [XFS_SCRUB_TYPE_DIR]            = { XHG_INO, XFS_SICK_INO_DIR },
     104             :         [XFS_SCRUB_TYPE_XATTR]          = { XHG_INO, XFS_SICK_INO_XATTR },
     105             :         [XFS_SCRUB_TYPE_SYMLINK]        = { XHG_INO, XFS_SICK_INO_SYMLINK },
     106             :         [XFS_SCRUB_TYPE_PARENT]         = { XHG_INO, XFS_SICK_INO_PARENT },
     107             :         [XFS_SCRUB_TYPE_RTBITMAP]       = { XHG_RT,  XFS_SICK_RT_BITMAP },
     108             :         [XFS_SCRUB_TYPE_RTSUM]          = { XHG_RT,  XFS_SICK_RT_SUMMARY },
     109             :         [XFS_SCRUB_TYPE_UQUOTA]         = { XHG_FS,  XFS_SICK_FS_UQUOTA },
     110             :         [XFS_SCRUB_TYPE_GQUOTA]         = { XHG_FS,  XFS_SICK_FS_GQUOTA },
     111             :         [XFS_SCRUB_TYPE_PQUOTA]         = { XHG_FS,  XFS_SICK_FS_PQUOTA },
     112             :         [XFS_SCRUB_TYPE_FSCOUNTERS]     = { XHG_FS,  XFS_SICK_FS_COUNTERS },
     113             :         [XFS_SCRUB_TYPE_QUOTACHECK]     = { XHG_FS,  XFS_SICK_FS_QUOTACHECK },
     114             :         [XFS_SCRUB_TYPE_NLINKS]         = { XHG_FS,  XFS_SICK_FS_NLINKS },
     115             :         [XFS_SCRUB_TYPE_DIRTREE]        = { XHG_INO, XFS_SICK_INO_DIRTREE },
     116             :         [XFS_SCRUB_TYPE_RGSUPER]        = { XHG_RTGROUP, XFS_SICK_RT_SUPER },
     117             :         [XFS_SCRUB_TYPE_RTRMAPBT]       = { XHG_RTGROUP, XFS_SICK_RT_RMAPBT },
     118             :         [XFS_SCRUB_TYPE_RTREFCBT]       = { XHG_RTGROUP, XFS_SICK_RT_REFCNTBT },
     119             : };
     120             : 
     121             : /* Return the health status mask for this scrub type. */
     122             : unsigned int
     123   858405606 : xchk_health_mask_for_scrub_type(
     124             :         __u32                   scrub_type)
     125             : {
     126   858405606 :         return type_to_health_flag[scrub_type].sick_mask;
     127             : }
     128             : 
     129             : /*
     130             :  * Scrub gave the filesystem a clean bill of health, so clear all the indirect
     131             :  * markers of past problems (at least for the fs and ags) so that we can be
     132             :  * healthy again.
     133             :  */
     134             : STATIC void
     135       25682 : xchk_mark_all_healthy(
     136             :         struct xfs_mount        *mp)
     137             : {
     138       25682 :         struct xfs_perag        *pag;
     139       25682 :         struct xfs_rtgroup      *rtg;
     140       25682 :         xfs_agnumber_t          agno;
     141       25682 :         xfs_rgnumber_t          rgno;
     142             : 
     143       25682 :         xfs_fs_mark_healthy(mp, XFS_SICK_FS_INDIRECT);
     144       25682 :         xfs_rt_mark_healthy(mp, XFS_SICK_RT_INDIRECT);
     145      201705 :         for_each_perag(mp, agno, pag)
     146      176023 :                 xfs_ag_mark_healthy(pag, XFS_SICK_AG_INDIRECT);
     147       80690 :         for_each_rtgroup(mp, rgno, rtg)
     148       55008 :                 xfs_rtgroup_mark_healthy(rtg, XFS_SICK_RT_INDIRECT);
     149       25682 : }
     150             : 
     151             : /*
     152             :  * Update filesystem health assessments based on what we found and did.
     153             :  *
     154             :  * If the scrubber finds errors, we mark sick whatever's mentioned in
     155             :  * sick_mask, no matter whether this is a first scan or an
     156             :  * evaluation of repair effectiveness.
     157             :  *
     158             :  * Otherwise, no direct corruption was found, so mark whatever's in
     159             :  * sick_mask as healthy.
     160             :  */
     161             : void
     162   729979232 : xchk_update_health(
     163             :         struct xfs_scrub        *sc)
     164             : {
     165   729979232 :         struct xfs_perag        *pag;
     166   729979232 :         struct xfs_rtgroup      *rtg;
     167   729979232 :         bool                    bad;
     168             : 
     169             :         /*
     170             :          * The HEALTHY scrub type is a request from userspace to clear all the
     171             :          * indirect flags after a clean scan of the entire filesystem.  As such
     172             :          * there's no sick flag defined for it, so we branch here ahead of the
     173             :          * mask check.
     174             :          */
     175   729979232 :         if (sc->sm->sm_type == XFS_SCRUB_TYPE_HEALTHY &&
     176             :             !(sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)) {
     177       25682 :                 xchk_mark_all_healthy(sc->mp);
     178       25682 :                 return;
     179             :         }
     180             : 
     181   729953550 :         if (!sc->sick_mask)
     182             :                 return;
     183             : 
     184   729665525 :         bad = (sc->sm->sm_flags & (XFS_SCRUB_OFLAG_CORRUPT |
     185             :                                    XFS_SCRUB_OFLAG_XCORRUPT));
     186   729665525 :         switch (type_to_health_flag[sc->sm->sm_type].group) {
     187     6161875 :         case XHG_AG:
     188     6161875 :                 pag = xfs_perag_get(sc->mp, sc->sm->sm_agno);
     189     6165403 :                 if (bad) {
     190         100 :                         xfs_ag_mark_sick(pag, sc->sick_mask);
     191         100 :                         xfs_ag_mark_checked(pag, sc->sick_mask);
     192             :                 } else
     193     6165303 :                         xfs_ag_mark_healthy(pag, sc->sick_mask);
     194     6164304 :                 xfs_perag_put(pag);
     195     6164304 :                 break;
     196   722053943 :         case XHG_INO:
     197   722053943 :                 if (!sc->ip)
     198             :                         return;
     199   722053943 :                 if (bad) {
     200          33 :                         unsigned int    mask = sc->sick_mask;
     201             : 
     202             :                         /*
     203             :                          * If we're coming in for repairs then we don't want
     204             :                          * sickness flags to propagate to the incore health
     205             :                          * status if the inode gets inactivated before we can
     206             :                          * fix it.
     207             :                          */
     208          33 :                         if (sc->sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR)
     209           0 :                                 mask |= XFS_SICK_INO_FORGET;
     210          33 :                         xfs_inode_mark_sick(sc->ip, mask);
     211          33 :                         xfs_inode_mark_checked(sc->ip, sc->sick_mask);
     212             :                 } else
     213   722053910 :                         xfs_inode_mark_healthy(sc->ip, sc->sick_mask);
     214             :                 break;
     215      428531 :         case XHG_FS:
     216      428531 :                 if (bad) {
     217           0 :                         xfs_fs_mark_sick(sc->mp, sc->sick_mask);
     218           0 :                         xfs_fs_mark_checked(sc->mp, sc->sick_mask);
     219             :                 } else
     220      428531 :                         xfs_fs_mark_healthy(sc->mp, sc->sick_mask);
     221             :                 break;
     222      171469 :         case XHG_RT:
     223      171469 :                 if (bad) {
     224           0 :                         xfs_rt_mark_sick(sc->mp, sc->sick_mask);
     225           0 :                         xfs_rt_mark_checked(sc->mp, sc->sick_mask);
     226             :                 } else
     227      171469 :                         xfs_rt_mark_healthy(sc->mp, sc->sick_mask);
     228             :                 break;
     229      330885 :         case XHG_RTGROUP:
     230      330885 :                 rtg = xfs_rtgroup_get(sc->mp, sc->sm->sm_agno);
     231      330946 :                 if (bad) {
     232           0 :                         xfs_rtgroup_mark_sick(rtg, sc->sick_mask);
     233           0 :                         xfs_rtgroup_mark_checked(rtg, sc->sick_mask);
     234             :                 } else
     235      330946 :                         xfs_rtgroup_mark_healthy(rtg, sc->sick_mask);
     236      330916 :                 xfs_rtgroup_put(rtg);
     237      330916 :                 break;
     238           0 :         default:
     239           0 :                 ASSERT(0);
     240           0 :                 break;
     241             :         }
     242             : }
     243             : 
     244             : /* Is the given per-AG btree healthy enough for scanning? */
     245             : bool
     246  4359757121 : xchk_ag_btree_healthy_enough(
     247             :         struct xfs_scrub        *sc,
     248             :         struct xfs_perag        *pag,
     249             :         xfs_btnum_t             btnum)
     250             : {
     251  4359757121 :         unsigned int            mask = 0;
     252             : 
     253             :         /*
     254             :          * We always want the cursor if it's the same type as whatever we're
     255             :          * scrubbing, even if we already know the structure is corrupt.
     256             :          *
     257             :          * Otherwise, we're only interested in the btree for cross-referencing.
     258             :          * If we know the btree is bad then don't bother, just set XFAIL.
     259             :          */
     260  4359757121 :         switch (btnum) {
     261   729165826 :         case XFS_BTNUM_BNO:
     262   729165826 :                 if (sc->sm->sm_type == XFS_SCRUB_TYPE_BNOBT)
     263             :                         return true;
     264             :                 mask = XFS_SICK_AG_BNOBT;
     265             :                 break;
     266   729622184 :         case XFS_BTNUM_CNT:
     267   729622184 :                 if (sc->sm->sm_type == XFS_SCRUB_TYPE_CNTBT)
     268             :                         return true;
     269             :                 mask = XFS_SICK_AG_CNTBT;
     270             :                 break;
     271   729737492 :         case XFS_BTNUM_INO:
     272   729737492 :                 if (sc->sm->sm_type == XFS_SCRUB_TYPE_INOBT)
     273             :                         return true;
     274             :                 mask = XFS_SICK_AG_INOBT;
     275             :                 break;
     276   729366114 :         case XFS_BTNUM_FINO:
     277   729366114 :                 if (sc->sm->sm_type == XFS_SCRUB_TYPE_FINOBT)
     278             :                         return true;
     279             :                 mask = XFS_SICK_AG_FINOBT;
     280             :                 break;
     281   721062644 :         case XFS_BTNUM_RMAP:
     282   721062644 :                 if (sc->sm->sm_type == XFS_SCRUB_TYPE_RMAPBT)
     283             :                         return true;
     284             :                 mask = XFS_SICK_AG_RMAPBT;
     285             :                 break;
     286   720802861 :         case XFS_BTNUM_REFC:
     287   720802861 :                 if (sc->sm->sm_type == XFS_SCRUB_TYPE_REFCNTBT)
     288             :                         return true;
     289             :                 mask = XFS_SICK_AG_REFCNTBT;
     290             :                 break;
     291           0 :         default:
     292           0 :                 ASSERT(0);
     293           0 :                 return true;
     294             :         }
     295             : 
     296             :         /*
     297             :          * If we just repaired some AG metadata, sc->sick_mask will reflect all
     298             :          * the per-AG metadata types that were repaired.  Exclude these from
     299             :          * the filesystem health query because we have not yet updated the
     300             :          * health status and we want everything to be scanned.
     301             :          */
     302  4357225853 :         if ((sc->flags & XREP_ALREADY_FIXED) &&
     303   447052406 :             type_to_health_flag[sc->sm->sm_type].group == XHG_AG)
     304     7727657 :                 mask &= ~sc->sick_mask;
     305             : 
     306  4357225839 :         if (xfs_ag_has_sickness(pag, mask)) {
     307           0 :                 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_XFAIL;
     308           0 :                 return false;
     309             :         }
     310             : 
     311             :         return true;
     312             : }
     313             : 
     314             : /*
     315             :  * Quick scan to double-check that there isn't any evidence of lingering
     316             :  * primary health problems.  If we're still clear, then the health update will
     317             :  * take care of clearing the indirect evidence.
     318             :  */
     319             : int
     320       25682 : xchk_health_record(
     321             :         struct xfs_scrub        *sc)
     322             : {
     323       25682 :         struct xfs_mount        *mp = sc->mp;
     324       25682 :         struct xfs_perag        *pag;
     325       25682 :         struct xfs_rtgroup      *rtg;
     326       25682 :         xfs_agnumber_t          agno;
     327       25682 :         xfs_rgnumber_t          rgno;
     328             : 
     329       25682 :         unsigned int            sick;
     330       25682 :         unsigned int            checked;
     331             : 
     332       25682 :         xfs_fs_measure_sickness(mp, &sick, &checked);
     333       25682 :         if (sick & XFS_SICK_FS_PRIMARY)
     334           0 :                 xchk_set_corrupt(sc);
     335             : 
     336       25682 :         xfs_rt_measure_sickness(mp, &sick, &checked);
     337       25682 :         if (sick & XFS_SICK_RT_PRIMARY)
     338           0 :                 xchk_set_corrupt(sc);
     339             : 
     340      201705 :         for_each_perag(mp, agno, pag) {
     341      176023 :                 xfs_ag_measure_sickness(pag, &sick, &checked);
     342      176023 :                 if (sick & XFS_SICK_AG_PRIMARY)
     343           0 :                         xchk_set_corrupt(sc);
     344             :         }
     345             : 
     346       80690 :         for_each_rtgroup(mp, rgno, rtg) {
     347       55008 :                 xfs_rtgroup_measure_sickness(rtg, &sick, &checked);
     348       55008 :                 if (sick & XFS_SICK_RT_PRIMARY)
     349           0 :                         xchk_set_corrupt(sc);
     350             :         }
     351             : 
     352       25682 :         return 0;
     353             : }

Generated by: LCOV version 1.14