xfs: inactivate directory data blocks
authorDarrick J. Wong <djwong@kernel.org>
Mon, 15 Apr 2024 21:54:50 +0000 (14:54 -0700)
committerDarrick J. Wong <djwong@kernel.org>
Mon, 15 Apr 2024 21:58:55 +0000 (14:58 -0700)
Teach inode inactivation to delete all the incore buffers backing a
directory.  In normal runtime this should never happen because the VFS
forbids rmdir on a non-empty directory.

In the next patch, online directory repair stands up a new directory,
exchanges it with the broken directory, and then drops the private
temporary directory.  If we cancel the repair just prior to exchanging
the directory contents, the new directory will need to be torn down.
Note: If we commit the repair, reaping will take care of all the ondisk
space allocations and incore buffers for the old corrupt directory.

Signed-off-by: Darrick J. Wong <djwong@kernel.org>
Reviewed-by: Christoph Hellwig <hch@lst.de>
fs/xfs/xfs_inode.c

index b24c0e23d37d72e0aed22429698212e4c6f9a258..09d643a9e997b38eb5667baab397bdc179449c95 100644 (file)
@@ -16,6 +16,7 @@
 #include "xfs_inode.h"
 #include "xfs_dir2.h"
 #include "xfs_attr.h"
+#include "xfs_bit.h"
 #include "xfs_trans_space.h"
 #include "xfs_trans.h"
 #include "xfs_buf_item.h"
@@ -1551,6 +1552,51 @@ out_unlock:
        return error;
 }
 
+/*
+ * Mark all the buffers attached to this directory stale.  In theory we should
+ * never be freeing a directory with any blocks at all, but this covers the
+ * case where we've recovered a directory swap with a "temporary" directory
+ * created by online repair and now need to dump it.
+ */
+STATIC void
+xfs_inactive_dir(
+       struct xfs_inode        *dp)
+{
+       struct xfs_iext_cursor  icur;
+       struct xfs_bmbt_irec    got;
+       struct xfs_mount        *mp = dp->i_mount;
+       struct xfs_da_geometry  *geo = mp->m_dir_geo;
+       struct xfs_ifork        *ifp = xfs_ifork_ptr(dp, XFS_DATA_FORK);
+       xfs_fileoff_t           off;
+
+       /*
+        * Invalidate each directory block.  All directory blocks are of
+        * fsbcount length and alignment, so we only need to walk those same
+        * offsets.  We hold the only reference to this inode, so we must wait
+        * for the buffer locks.
+        */
+       for_each_xfs_iext(ifp, &icur, &got) {
+               for (off = round_up(got.br_startoff, geo->fsbcount);
+                    off < got.br_startoff + got.br_blockcount;
+                    off += geo->fsbcount) {
+                       struct xfs_buf  *bp = NULL;
+                       xfs_fsblock_t   fsbno;
+                       int             error;
+
+                       fsbno = (off - got.br_startoff) + got.br_startblock;
+                       error = xfs_buf_incore(mp->m_ddev_targp,
+                                       XFS_FSB_TO_DADDR(mp, fsbno),
+                                       XFS_FSB_TO_BB(mp, geo->fsbcount),
+                                       XBF_LIVESCAN, &bp);
+                       if (error)
+                               continue;
+
+                       xfs_buf_stale(bp);
+                       xfs_buf_relse(bp);
+               }
+       }
+}
+
 /*
  * xfs_inactive_truncate
  *
@@ -1861,6 +1907,11 @@ xfs_inactive(
                        goto out;
        }
 
+       if (S_ISDIR(VFS_I(ip)->i_mode) && ip->i_df.if_nextents > 0) {
+               xfs_inactive_dir(ip);
+               truncate = 1;
+       }
+
        if (S_ISLNK(VFS_I(ip)->i_mode))
                error = xfs_inactive_symlink(ip);
        else if (truncate)