]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/commitdiff
xfs: condense directories after a mapping exchange operation
authorDarrick J. Wong <djwong@kernel.org>
Mon, 29 Jul 2024 23:22:36 +0000 (16:22 -0700)
committerDarrick J. Wong <djwong@kernel.org>
Tue, 30 Jul 2024 00:00:59 +0000 (17:00 -0700)
Source kernel commit: da165fbde23b84591b6ccdf6749277d2d767b770

The previous commit added a new file mapping exchange flag that enables
us to perform post-swap processing on file2 once we're done exchanging
extent mappings.  Now add this ability for directories.

This isn't used anywhere right now, but we need to have the basic ondisk
flags in place so that a future online directory repair feature can
create salvaged dirents in a temporary directory and exchange the data
fork mappings when ready.  If one file is in extents format and the
other is inline, we will have to promote both to extents format to
perform the exchange.  After the exchange, we can try to condense the
fixed directory down to inline format if possible.

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

index afe2c2d22f0cf1e001ac5ec964432094395ce3e0..6e758cac10622cc46438054a7baece12cac01b3e 100644 (file)
@@ -25,6 +25,8 @@
 #include "xfs_da_btree.h"
 #include "xfs_attr_leaf.h"
 #include "xfs_attr.h"
+#include "xfs_dir2_priv.h"
+#include "xfs_dir2.h"
 
 struct kmem_cache      *xfs_exchmaps_intent_cache;
 
@@ -392,6 +394,42 @@ xfs_exchmaps_attr_to_sf(
        return xfs_attr3_leaf_to_shortform(bp, &args, forkoff);
 }
 
+/* Convert inode2's block dir fork back to shortform, if possible.. */
+STATIC int
+xfs_exchmaps_dir_to_sf(
+       struct xfs_trans                *tp,
+       struct xfs_exchmaps_intent      *xmi)
+{
+       struct xfs_da_args      args = {
+               .dp             = xmi->xmi_ip2,
+               .geo            = tp->t_mountp->m_dir_geo,
+               .whichfork      = XFS_DATA_FORK,
+               .trans          = tp,
+       };
+       struct xfs_dir2_sf_hdr  sfh;
+       struct xfs_buf          *bp;
+       bool                    isblock;
+       int                     size;
+       int                     error;
+
+       error = xfs_dir2_isblock(&args, &isblock);
+       if (error)
+               return error;
+
+       if (!isblock)
+               return 0;
+
+       error = xfs_dir3_block_read(tp, xmi->xmi_ip2, &bp);
+       if (error)
+               return error;
+
+       size = xfs_dir2_block_sfsize(xmi->xmi_ip2, bp->b_addr, &sfh);
+       if (size > xfs_inode_data_fork_size(xmi->xmi_ip2))
+               return 0;
+
+       return xfs_dir2_block_to_sf(&args, bp, size, &sfh);
+}
+
 /* Clear the reflink flag after an exchange. */
 static inline void
 xfs_exchmaps_clear_reflink(
@@ -415,6 +453,8 @@ xfs_exchmaps_do_postop_work(
 
                if (xmi->xmi_flags & XFS_EXCHMAPS_ATTR_FORK)
                        error = xfs_exchmaps_attr_to_sf(tp, xmi);
+               else if (S_ISDIR(VFS_I(xmi->xmi_ip2)->i_mode))
+                       error = xfs_exchmaps_dir_to_sf(tp, xmi);
                xmi->xmi_flags &= ~__XFS_EXCHMAPS_INO2_SHORTFORM;
                if (error)
                        return error;
@@ -879,6 +919,9 @@ xfs_exchmaps_init_intent(
                        xmi->xmi_flags |= XFS_EXCHMAPS_CLEAR_INO2_REFLINK;
        }
 
+       if (S_ISDIR(VFS_I(xmi->xmi_ip2)->i_mode))
+               xmi->xmi_flags |= __XFS_EXCHMAPS_INO2_SHORTFORM;
+
        return xmi;
 }