[PATCH 11/12] xfs: move buffer locking out of xfs_find_get_buf

Christoph Hellwig <[email protected]> Tue, 28 Jul 2026 10:11:19 +0200
Newsgroups org.kernel.vger.linux-xfs
Message-ID <[email protected]>
To prepare for buffer loookups that don't lock the buffer, move the
call to xfs_buf_find_lock from xfs_find_get_buf to its callers.

Signed-off-by: Christoph Hellwig <[email protected]>
Reviewed-by: Brian Foster <[email protected]>
Reviewed-by: "Darrick J. Wong" <[email protected]>
---
 fs/xfs/xfs_buf.c | 35 ++++++++++++++++++++++++-----------
 1 file changed, 24 insertions(+), 11 deletions(-)

diff --git a/fs/xfs/xfs_buf.c b/fs/xfs/xfs_buf.c
index aee58aca8c7c..bc23fa59680e 100644
--- a/fs/xfs/xfs_buf.c
+++ b/fs/xfs/xfs_buf.c
@@ -593,12 +593,6 @@ __xfs_find_get_buf(
 			return error;
 	}
 
-	error = xfs_buf_find_lock(bp, flags);
-	if (error) {
-		xfs_buf_rele(bp);
-		return error;
-	}
-
 	*bpp = bp;
 	return 0;
 }
@@ -620,6 +614,13 @@ xfs_buf_get_map(
 	if (error)
 		return error;
 
+	error = xfs_buf_find_lock(*bpp, flags);
+	if (error) {
+		xfs_buf_rele(*bpp);
+		*bpp = NULL;
+		return error;
+	}
+
 	/*
 	 * Clear b_error as the callers don't expect to find valid data in the
 	 * buffer.
@@ -664,6 +665,12 @@ xfs_buf_read_map(
 	error = __xfs_find_get_buf(target, map, nmaps, flags, &bp);
 	if (error)
 		return error;
+	error = xfs_buf_find_lock(bp, flags);
+	if (error) {
+		xfs_buf_rele(bp);
+		*bpp = NULL;
+		return error;
+	}
 
 	trace_xfs_buf_read(bp, flags, _RET_IP_);
 
@@ -766,20 +773,26 @@ xfs_buf_readahead_map(
 	if (xfs_buftarg_is_mem(target))
 		return;
 
-	if (__xfs_find_get_buf(target, map, nmaps, flags | XBF_TRYLOCK, &bp))
+	if (__xfs_find_get_buf(target, map, nmaps, flags, &bp))
 		return;
+	if (xfs_buf_find_lock(bp, XBF_TRYLOCK))
+		goto out_rele;
+
 	trace_xfs_buf_readahead(bp, 0, _RET_IP_);
+	if (bp->b_flags & XBF_DONE)
+		goto out_unlock;
 
-	if (bp->b_flags & XBF_DONE) {
-		xfs_buf_relse(bp);
-		return;
-	}
 	XFS_STATS_INC(target->bt_mount, xb_get_read);
 	bp->b_ops = ops;
 	xfs_buf_clear_flags(bp, XBF_WRITE | XBF_DONE);
 	xfs_buf_set_flags(bp, flags);
 	percpu_counter_inc(&target->bt_readahead_count);
 	xfs_buf_submit(bp);
+	return;
+out_unlock:
+	xfs_buf_unlock(bp);
+out_rele:
+	xfs_buf_rele(bp);
 }
 
 /*
-- 
2.53.0