xfs: refactor creation of bmap btree roots
authorDarrick J. Wong <djwong@kernel.org>
Wed, 3 Jul 2024 21:22:12 +0000 (14:22 -0700)
committerChristoph Hellwig <hch@lst.de>
Sun, 11 Aug 2024 06:35:54 +0000 (08:35 +0200)
Now that we've created inode fork helpers to allocate and free btree
roots, create a new bmap btree helper to create a new bmbt root, and
refactor the extents <-> btree conversion functions to use our new
helpers.

Signed-off-by: Darrick J. Wong <djwong@kernel.org>
libxfs/xfs_bmap.c
libxfs/xfs_bmap_btree.c
libxfs/xfs_bmap_btree.h

index 0f28d25448b4e111de188c27fca4e4495a824931..eba644f067339b8b9a42149a1d1de67e27c98d92 100644 (file)
@@ -609,7 +609,7 @@ xfs_bmap_btree_to_extents(
        xfs_trans_binval(tp, cbp);
        if (cur->bc_levels[0].bp == cbp)
                cur->bc_levels[0].bp = NULL;
-       xfs_iroot_realloc(ip, -1, whichfork);
+       xfs_iroot_free(ip, whichfork);
        ASSERT(ifp->if_broot == NULL);
        ifp->if_format = XFS_DINODE_FMT_EXTENTS;
        *logflagsp |= XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
@@ -650,19 +650,10 @@ xfs_bmap_extents_to_btree(
        ASSERT(ifp->if_format == XFS_DINODE_FMT_EXTENTS);
 
        /*
-        * Make space in the inode incore. This needs to be undone if we fail
-        * to expand the root.
-        */
-       xfs_iroot_realloc(ip, 1, whichfork);
-
-       /*
-        * Fill in the root.
-        */
-       block = ifp->if_broot;
-       xfs_bmbt_init_block(ip, block, NULL, 1, 1);
-       /*
-        * Need a cursor.  Can't allocate until bb_level is filled in.
+        * Fill in the root, create a cursor.  Can't allocate until bb_level is
+        * filled in.
         */
+       xfs_bmbt_iroot_alloc(ip, whichfork);
        cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
        if (wasdel)
                cur->bc_flags |= XFS_BTREE_BMBT_WASDEL;
@@ -719,6 +710,7 @@ xfs_bmap_extents_to_btree(
        /*
         * Fill in the root key and pointer.
         */
+       block = ifp->if_broot;
        kp = xfs_bmbt_key_addr(mp, block, 1);
        arp = xfs_bmbt_rec_addr(mp, ablock, 1);
        kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
@@ -740,7 +732,7 @@ xfs_bmap_extents_to_btree(
 out_unreserve_dquot:
        xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
 out_root_realloc:
-       xfs_iroot_realloc(ip, -1, whichfork);
+       xfs_iroot_free(ip, whichfork);
        ifp->if_format = XFS_DINODE_FMT_EXTENTS;
        ASSERT(ifp->if_broot == NULL);
        xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
index cac644c8ce35a5d63379ce2e923a59a84b3a93f5..9a9bc2f4712fb7dc23415f5a09634a2a13a585f2 100644 (file)
@@ -758,3 +758,18 @@ xfs_bmbt_destroy_cur_cache(void)
        kmem_cache_destroy(xfs_bmbt_cur_cache);
        xfs_bmbt_cur_cache = NULL;
 }
+
+/* Create an incore bmbt btree root block. */
+void
+xfs_bmbt_iroot_alloc(
+       struct xfs_inode        *ip,
+       int                     whichfork)
+{
+       struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
+
+       xfs_iroot_alloc(ip, whichfork,
+                       xfs_bmap_broot_space_calc(ip->i_mount, 1));
+
+       /* Fill in the root. */
+       xfs_bmbt_init_block(ip, ifp->if_broot, NULL, 1, 1);
+}
index af47174fca084ef97150cd8b5ada4816da11f2b5..921c3e02469dd0ef1afcf2d32b8c48bed74de7de 100644 (file)
@@ -197,4 +197,6 @@ xfs_bmap_bmdr_space(struct xfs_btree_block *bb)
        return xfs_bmdr_space_calc(be16_to_cpu(bb->bb_numrecs));
 }
 
+void xfs_bmbt_iroot_alloc(struct xfs_inode *ip, int whichfork);
+
 #endif /* __XFS_BMAP_BTREE_H__ */