xfs_repair: always check realtime file mappings against incore info
authorDarrick J. Wong <djwong@kernel.org>
Wed, 3 Jul 2024 21:22:24 +0000 (14:22 -0700)
committerDarrick J. Wong <djwong@kernel.org>
Wed, 31 Jul 2024 01:46:56 +0000 (18:46 -0700)
Curiously, the xfs_repair code that processes data fork mappings of
realtime files doesn't actually compare the mappings against the incore
state map during the !check_dups phase (aka phase 3).  As a result, we
lose the opportunity to clear damaged realtime data forks before we get
to crosslinked file checking in phase 4, which results in ondisk
metadata errors calling do_error, which aborts repair.

Split the process_rt_rec_state code into two functions: one to check the
mapping, and another to update the incore state.  The first one can be
called to help us decide if we're going to zap the fork, and the second
one updates the incore state if we decide to keep the fork.  We already
do this for regular data files.

Signed-off-by: Darrick J. Wong <djwong@kernel.org>
repair/dinode.c

index 77474586726c149bfe1ede7c45837b46ae6fdfdf..a896238c00fdc16598716f8ed93eeb9e4e9eb708 100644 (file)
@@ -220,7 +220,7 @@ _("data fork in rt ino %" PRIu64 " claims dup rt extent,"
        return 0;
 }
 
-static int
+static void
 process_rt_rec_state(
        struct xfs_mount        *mp,
        xfs_ino_t               ino,
@@ -264,11 +264,78 @@ _("data fork in rt inode %" PRIu64 " found invalid rt extent %"PRIu64" state %d
                        set_rtbmap(ext, zap_metadata ? XR_E_METADATA :
                                                       XR_E_INUSE);
                        break;
+               case XR_E_BAD_STATE:
+                       do_error(
+_("bad state in rt extent map %" PRIu64 "\n"),
+                               ext);
                case XR_E_METADATA:
+               case XR_E_FS_MAP:
+               case XR_E_INO:
+               case XR_E_INUSE_FS:
+                       break;
+               case XR_E_INUSE:
+               case XR_E_MULT:
+                       set_rtbmap(ext, XR_E_MULT);
+                       break;
+               case XR_E_FREE1:
+               default:
                        do_error(
+_("illegal state %d in rt extent %" PRIu64 "\n"),
+                               state, ext);
+               }
+               b += mp->m_sb.sb_rextsize;
+       } while (b < irec->br_startblock + irec->br_blockcount);
+}
+
+/*
+ * Checks the realtime file's data mapping against in-core extent info, and
+ * complains if there are discrepancies.  Returns 0 if good, 1 if bad.
+ */
+static int
+check_rt_rec_state(
+       struct xfs_mount        *mp,
+       xfs_ino_t               ino,
+       struct xfs_bmbt_irec    *irec)
+{
+       xfs_fsblock_t           b = irec->br_startblock;
+       xfs_rtblock_t           ext;
+       int                     state;
+
+       do {
+               ext = (xfs_rtblock_t)b / mp->m_sb.sb_rextsize;
+               state = get_rtbmap(ext);
+
+               if ((b % mp->m_sb.sb_rextsize) != 0) {
+                       /*
+                        * We are midway through a partially written extent.
+                        * If we don't find the state that gets set in the
+                        * other clause of this loop body, then we have a
+                        * partially *mapped* rt extent and should complain.
+                        */
+                       if (state != XR_E_INUSE && state != XR_E_FREE) {
+                               do_warn(
+_("data fork in rt inode %" PRIu64 " found invalid rt extent %"PRIu64" state %d at rt block %"PRIu64"\n"),
+                                       ino, ext, state, b);
+                               return 1;
+                       }
+
+                       b = roundup(b, mp->m_sb.sb_rextsize);
+                       continue;
+               }
+
+               /*
+                * This is the start of an rt extent.  Complain if there are
+                * conflicting states.  We'll set the state elsewhere.
+                */
+               switch (state)  {
+               case XR_E_FREE:
+               case XR_E_UNKNOWN:
+                       break;
+               case XR_E_METADATA:
+                       do_warn(
 _("data fork in rt inode %" PRIu64 " found metadata file block %" PRIu64 " in rt bmap\n"),
                                ino, ext);
-                       break;
+                       return 1;
                case XR_E_BAD_STATE:
                        do_error(
 _("bad state in rt extent map %" PRIu64 "\n"),
@@ -276,12 +343,12 @@ _("bad state in rt extent map %" PRIu64 "\n"),
                case XR_E_FS_MAP:
                case XR_E_INO:
                case XR_E_INUSE_FS:
-                       do_error(
+                       do_warn(
 _("data fork in rt inode %" PRIu64 " found rt metadata extent %" PRIu64 " in rt bmap\n"),
                                ino, ext);
+                       return 1;
                case XR_E_INUSE:
                case XR_E_MULT:
-                       set_rtbmap(ext, XR_E_MULT);
                        do_warn(
 _("data fork in rt inode %" PRIu64 " claims used rt extent %" PRIu64 "\n"),
                                ino, b);
@@ -342,13 +409,18 @@ _("inode %" PRIu64 " - bad rt extent overflows - start %" PRIu64 ", "
                return 1;
        }
 
-       if (check_dups)
-               bad = process_rt_rec_dups(mp, ino, irec);
-       else
-               bad = process_rt_rec_state(mp, ino, zap_metadata, irec);
+       bad = check_rt_rec_state(mp, ino, irec);
        if (bad)
                return bad;
 
+       if (check_dups) {
+               bad = process_rt_rec_dups(mp, ino, irec);
+               if (bad)
+                       return bad;
+       } else {
+               process_rt_rec_state(mp, ino, zap_metadata, irec);
+       }
+
        /*
         * bump up the block counter
         */