xfs: simplify iext overflow checking and upgrade
authorChristoph Hellwig <hch@lst.de>
Thu, 2 May 2024 07:33:55 +0000 (09:33 +0200)
committerChandan Babu R <chandanbabu@kernel.org>
Fri, 3 May 2024 05:50:06 +0000 (11:20 +0530)
Currently the calls to xfs_iext_count_may_overflow and
xfs_iext_count_upgrade are always paired.  Merge them into a single
function to simplify the callers and the actual check and upgrade
logic itself.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: "Darrick J. Wong" <djwong@kernel.org>
Reviewed-by: Dave Chinner <dchinner@redhat.com>
Signed-off-by: Chandan Babu R <chandanbabu@kernel.org>
fs/xfs/libxfs/xfs_attr.c
fs/xfs/libxfs/xfs_bmap.c
fs/xfs/libxfs/xfs_inode_fork.c
fs/xfs/libxfs/xfs_inode_fork.h
fs/xfs/xfs_bmap_item.c
fs/xfs/xfs_bmap_util.c
fs/xfs/xfs_dquot.c
fs/xfs/xfs_iomap.c
fs/xfs/xfs_reflink.c
fs/xfs/xfs_rtalloc.c

index eb274d4d636d8f10bdbe268bb7f2327a6c2e751b..430cd3244c143dc065f0490b1ec3c3feef47e2e3 100644 (file)
@@ -1050,11 +1050,8 @@ xfs_attr_set(
                return error;
 
        if (op != XFS_ATTRUPDATE_REMOVE || xfs_inode_hasattr(dp)) {
-               error = xfs_iext_count_may_overflow(dp, XFS_ATTR_FORK,
+               error = xfs_iext_count_extend(args->trans, dp, XFS_ATTR_FORK,
                                XFS_IEXT_ATTR_MANIP_CNT(rmt_blks));
-               if (error == -EFBIG)
-                       error = xfs_iext_count_upgrade(args->trans, dp,
-                                       XFS_IEXT_ATTR_MANIP_CNT(rmt_blks));
                if (error)
                        goto out_trans_cancel;
        }
index 0633f285875de43f734f75a54176cc6d3cca8295..3b3206d312d6fe157ff90e14f28e96ab39f2e583 100644 (file)
@@ -4651,11 +4651,8 @@ xfs_bmapi_convert_one_delalloc(
        xfs_ilock(ip, XFS_ILOCK_EXCL);
        xfs_trans_ijoin(tp, ip, 0);
 
-       error = xfs_iext_count_may_overflow(ip, whichfork,
+       error = xfs_iext_count_extend(tp, ip, whichfork,
                        XFS_IEXT_ADD_NOSPLIT_CNT);
-       if (error == -EFBIG)
-               error = xfs_iext_count_upgrade(tp, ip,
-                               XFS_IEXT_ADD_NOSPLIT_CNT);
        if (error)
                goto out_trans_cancel;
 
index 7d660a9739090ae0dca7658328412200157abc8a..9d11ae01590919740a2d59c84c8e5094be6eb041 100644 (file)
@@ -765,53 +765,46 @@ xfs_ifork_verify_local_attr(
        return 0;
 }
 
+/*
+ * Check if the inode fork supports adding nr_to_add more extents.
+ *
+ * If it doesn't but we can upgrade it to large extent counters, do the upgrade.
+ * If we can't upgrade or are already using big counters but still can't fit the
+ * additional extents, return -EFBIG.
+ */
 int
-xfs_iext_count_may_overflow(
+xfs_iext_count_extend(
+       struct xfs_trans        *tp,
        struct xfs_inode        *ip,
        int                     whichfork,
-       int                     nr_to_add)
+       uint                    nr_to_add)
 {
+       struct xfs_mount        *mp = ip->i_mount;
+       bool                    has_large =
+               xfs_inode_has_large_extent_counts(ip);
        struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
-       uint64_t                max_exts;
        uint64_t                nr_exts;
 
+       ASSERT(nr_to_add <= XFS_MAX_EXTCNT_UPGRADE_NR);
+
        if (whichfork == XFS_COW_FORK)
                return 0;
 
-       max_exts = xfs_iext_max_nextents(xfs_inode_has_large_extent_counts(ip),
-                               whichfork);
-
-       if (XFS_TEST_ERROR(false, ip->i_mount, XFS_ERRTAG_REDUCE_MAX_IEXTENTS))
-               max_exts = 10;
-
+       /* no point in upgrading if if_nextents overflows */
        nr_exts = ifp->if_nextents + nr_to_add;
-       if (nr_exts < ifp->if_nextents || nr_exts > max_exts)
+       if (nr_exts < ifp->if_nextents)
                return -EFBIG;
 
-       return 0;
-}
-
-/*
- * Upgrade this inode's extent counter fields to be able to handle a potential
- * increase in the extent count by nr_to_add.  Normally this is the same
- * quantity that caused xfs_iext_count_may_overflow() to return -EFBIG.
- */
-int
-xfs_iext_count_upgrade(
-       struct xfs_trans        *tp,
-       struct xfs_inode        *ip,
-       uint                    nr_to_add)
-{
-       ASSERT(nr_to_add <= XFS_MAX_EXTCNT_UPGRADE_NR);
-
-       if (!xfs_has_large_extent_counts(ip->i_mount) ||
-           xfs_inode_has_large_extent_counts(ip) ||
-           XFS_TEST_ERROR(false, ip->i_mount, XFS_ERRTAG_REDUCE_MAX_IEXTENTS))
+       if (XFS_TEST_ERROR(false, mp, XFS_ERRTAG_REDUCE_MAX_IEXTENTS) &&
+           nr_exts > 10)
                return -EFBIG;
 
-       ip->i_diflags2 |= XFS_DIFLAG2_NREXT64;
-       xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
-
+       if (nr_exts > xfs_iext_max_nextents(has_large, whichfork)) {
+               if (has_large || !xfs_has_large_extent_counts(mp))
+                       return -EFBIG;
+               ip->i_diflags2 |= XFS_DIFLAG2_NREXT64;
+               xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
+       }
        return 0;
 }
 
index bd53eb951b6515e1748cca91d2a926f81edf91d0..2373d12fd474f0cc4f8227a038a554649f580ee7 100644 (file)
@@ -256,10 +256,8 @@ extern void xfs_ifork_init_cow(struct xfs_inode *ip);
 
 int xfs_ifork_verify_local_data(struct xfs_inode *ip);
 int xfs_ifork_verify_local_attr(struct xfs_inode *ip);
-int xfs_iext_count_may_overflow(struct xfs_inode *ip, int whichfork,
-               int nr_to_add);
-int xfs_iext_count_upgrade(struct xfs_trans *tp, struct xfs_inode *ip,
-               uint nr_to_add);
+int xfs_iext_count_extend(struct xfs_trans *tp, struct xfs_inode *ip,
+               int whichfork, uint nr_to_add);
 bool xfs_ifork_is_realtime(struct xfs_inode *ip, int whichfork);
 
 /* returns true if the fork has extents but they are not read in yet. */
index d27859a684aa699c2d509fdecc8f9ece344195e2..a19d62e78aa1e6a50881bf0a3793b7cc02555ea3 100644 (file)
@@ -524,9 +524,7 @@ xfs_bmap_recover_work(
        else
                iext_delta = XFS_IEXT_PUNCH_HOLE_CNT;
 
-       error = xfs_iext_count_may_overflow(ip, work->bi_whichfork, iext_delta);
-       if (error == -EFBIG)
-               error = xfs_iext_count_upgrade(tp, ip, iext_delta);
+       error = xfs_iext_count_extend(tp, ip, work->bi_whichfork, iext_delta);
        if (error)
                goto err_cancel;
 
index 14e0eb9b305fd671072828d961995f1b86f34157..ac2e77ebb54c735c07c170f3434e50be8e8d73da 100644 (file)
@@ -710,11 +710,8 @@ xfs_alloc_file_space(
                if (error)
                        break;
 
-               error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK,
+               error = xfs_iext_count_extend(tp, ip, XFS_DATA_FORK,
                                XFS_IEXT_ADD_NOSPLIT_CNT);
-               if (error == -EFBIG)
-                       error = xfs_iext_count_upgrade(tp, ip,
-                                       XFS_IEXT_ADD_NOSPLIT_CNT);
                if (error)
                        goto error;
 
@@ -771,10 +768,8 @@ xfs_unmap_extent(
        if (error)
                return error;
 
-       error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK,
+       error = xfs_iext_count_extend(tp, ip, XFS_DATA_FORK,
                        XFS_IEXT_PUNCH_HOLE_CNT);
-       if (error == -EFBIG)
-               error = xfs_iext_count_upgrade(tp, ip, XFS_IEXT_PUNCH_HOLE_CNT);
        if (error)
                goto out_trans_cancel;
 
@@ -1050,10 +1045,8 @@ xfs_insert_file_space(
        xfs_ilock(ip, XFS_ILOCK_EXCL);
        xfs_trans_ijoin(tp, ip, 0);
 
-       error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK,
+       error = xfs_iext_count_extend(tp, ip, XFS_DATA_FORK,
                        XFS_IEXT_PUNCH_HOLE_CNT);
-       if (error == -EFBIG)
-               error = xfs_iext_count_upgrade(tp, ip, XFS_IEXT_PUNCH_HOLE_CNT);
        if (error)
                goto out_trans_cancel;
 
@@ -1279,23 +1272,17 @@ xfs_swap_extent_rmap(
                        trace_xfs_swap_extent_rmap_remap_piece(tip, &uirec);
 
                        if (xfs_bmap_is_real_extent(&uirec)) {
-                               error = xfs_iext_count_may_overflow(ip,
+                               error = xfs_iext_count_extend(tp, ip,
                                                XFS_DATA_FORK,
                                                XFS_IEXT_SWAP_RMAP_CNT);
-                               if (error == -EFBIG)
-                                       error = xfs_iext_count_upgrade(tp, ip,
-                                                       XFS_IEXT_SWAP_RMAP_CNT);
                                if (error)
                                        goto out;
                        }
 
                        if (xfs_bmap_is_real_extent(&irec)) {
-                               error = xfs_iext_count_may_overflow(tip,
+                               error = xfs_iext_count_extend(tp, tip,
                                                XFS_DATA_FORK,
                                                XFS_IEXT_SWAP_RMAP_CNT);
-                               if (error == -EFBIG)
-                                       error = xfs_iext_count_upgrade(tp, ip,
-                                                       XFS_IEXT_SWAP_RMAP_CNT);
                                if (error)
                                        goto out;
                        }
index 43acb4f0d174330910982ec5a15e06977f43a8dc..c1b211c260a9d5d5f3dd87fc22565eaf43772712 100644 (file)
@@ -341,11 +341,8 @@ xfs_dquot_disk_alloc(
                goto err_cancel;
        }
 
-       error = xfs_iext_count_may_overflow(quotip, XFS_DATA_FORK,
+       error = xfs_iext_count_extend(tp, quotip, XFS_DATA_FORK,
                        XFS_IEXT_ADD_NOSPLIT_CNT);
-       if (error == -EFBIG)
-               error = xfs_iext_count_upgrade(tp, quotip,
-                               XFS_IEXT_ADD_NOSPLIT_CNT);
        if (error)
                goto err_cancel;
 
index 3db93b876de34ac39af081f6333aa16d64fecfe2..3783426739258c9e13b99b64a0444f636254fef9 100644 (file)
@@ -299,9 +299,7 @@ xfs_iomap_write_direct(
        if (error)
                return error;
 
-       error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK, nr_exts);
-       if (error == -EFBIG)
-               error = xfs_iext_count_upgrade(tp, ip, nr_exts);
+       error = xfs_iext_count_extend(tp, ip, XFS_DATA_FORK, nr_exts);
        if (error)
                goto out_trans_cancel;
 
@@ -617,11 +615,8 @@ xfs_iomap_write_unwritten(
                if (error)
                        return error;
 
-               error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK,
+               error = xfs_iext_count_extend(tp, ip, XFS_DATA_FORK,
                                XFS_IEXT_WRITE_UNWRITTEN_CNT);
-               if (error == -EFBIG)
-                       error = xfs_iext_count_upgrade(tp, ip,
-                                       XFS_IEXT_WRITE_UNWRITTEN_CNT);
                if (error)
                        goto error_on_bmapi_transaction;
 
index 520f5ef3743c47dcd5d705d009d78dccaed568fe..063a2e00d1691564c4bd5bce9bef31955299aa11 100644 (file)
@@ -754,11 +754,8 @@ xfs_reflink_end_cow_extent(
        del = got;
        xfs_trim_extent(&del, *offset_fsb, end_fsb - *offset_fsb);
 
-       error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK,
+       error = xfs_iext_count_extend(tp, ip, XFS_DATA_FORK,
                        XFS_IEXT_REFLINK_END_COW_CNT);
-       if (error == -EFBIG)
-               error = xfs_iext_count_upgrade(tp, ip,
-                               XFS_IEXT_REFLINK_END_COW_CNT);
        if (error)
                goto out_cancel;
 
@@ -1258,9 +1255,7 @@ xfs_reflink_remap_extent(
        if (dmap_written)
                ++iext_delta;
 
-       error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK, iext_delta);
-       if (error == -EFBIG)
-               error = xfs_iext_count_upgrade(tp, ip, iext_delta);
+       error = xfs_iext_count_extend(tp, ip, XFS_DATA_FORK, iext_delta);
        if (error)
                goto out_cancel;
 
index 150f544445ca8233a23aa6eb15367fda3d7ba4f4..5a7ddfed1bb8554992c91bb4bdcf576ed206cd84 100644 (file)
@@ -695,11 +695,8 @@ xfs_growfs_rt_alloc(
                xfs_ilock(ip, XFS_ILOCK_EXCL);
                xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
 
-               error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK,
+               error = xfs_iext_count_extend(tp, ip, XFS_DATA_FORK,
                                XFS_IEXT_ADD_NOSPLIT_CNT);
-               if (error == -EFBIG)
-                       error = xfs_iext_count_upgrade(tp, ip,
-                                       XFS_IEXT_ADD_NOSPLIT_CNT);
                if (error)
                        goto out_trans_cancel;