Linux XFS filesystem development
 help / color / mirror / Atom feed
* [PATCH v2 0/2] exportfs, xfs, nfsd: map a whole pNFS block layout in one ->map_blocks call
@ 2026-11-26  1:52 Daejun Park via B4 Relay
  2026-11-26  1:52 ` [PATCH v2 1/2] xfs: factor the mapping of one pNFS extent out of xfs_fs_map_blocks() Daejun Park via B4 Relay
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Daejun Park via B4 Relay @ 2026-11-26  1:52 UTC (permalink / raw)
  To: Chuck Lever, Jeff Layton, NeilBrown, Olga Kornievskaia, Dai Ngo,
	Tom Talpey, Christoph Hellwig, Carlos Maiolino, Amir Goldstein
  Cc: Darrick J. Wong, Dave Chinner, Sergey Bashirov, Christian Brauner,
	linux-nfs, linux-xfs, linux-fsdevel, linux-kernel, Daejun Park

nfsd gets a pNFS block layout from XFS with one ->map_blocks call per
extent. Patch 2 lets one call return all of them, as Christoph and
Darrick suggested, and patch 1 prepares XFS for it. fstests and pynfs
give the same results with and without the series.

The series needs four fixes. The first two are posted separately, and
the other two are in nfsd-testing:

- nfsd: stop a block layout at the maximum file size
- xfs: punch delalloc blocks past EOF before mapping a pNFS layout
- nfsd: do not return overlapping extents in a block layout
- nfsd: do not allocate blocks for an RW layout with a minlength of zero

Once they are in mainline, I will rebase the series for the VFS tree,
as Chuck suggested.

Changes in v2:
- Drop v1 patches 1-3 and 6, now in mainline (2 in another form).
- Fold the interface, the XFS loop and the nfsd caller into one patch.
- Allocate the mapping array with kvmalloc (Chuck).
- Keep nfsd's trim of a mapping that starts below the offset, instead
  of refusing it.

---
Daejun Park (2):
      xfs: factor the mapping of one pNFS extent out of xfs_fs_map_blocks()
      exportfs, xfs, nfsd: map a whole pNFS block layout in one ->map_blocks call

 fs/nfsd/blocklayout.c          | 137 ++++++++++++++++++----------------
 fs/xfs/xfs_pnfs.c              | 163 +++++++++++++++++++++++++----------------
 include/linux/exportfs_block.h |  17 +++--
 3 files changed, 187 insertions(+), 130 deletions(-)
---
base-commit: 25527067098f9d95562244fd64749ebaae3077f9
change-id: 20261008-xfs-nfsd-map-blocks-417d5b6aae42

Best regards,
-- 
Daejun Park <daejun7.park@samsung.com>



^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v2 1/2] xfs: factor the mapping of one pNFS extent out of xfs_fs_map_blocks()
  2026-11-26  1:52 [PATCH v2 0/2] exportfs, xfs, nfsd: map a whole pNFS block layout in one ->map_blocks call Daejun Park via B4 Relay
@ 2026-11-26  1:52 ` Daejun Park via B4 Relay
  2026-12-01  8:17   ` Christoph Hellwig
  2026-11-26  1:52 ` [PATCH v2 2/2] exportfs, xfs, nfsd: map a whole pNFS block layout in one ->map_blocks call Daejun Park via B4 Relay
  2026-12-01  8:21 ` [PATCH v2 0/2] " Christoph Hellwig
  2 siblings, 1 reply; 6+ messages in thread
From: Daejun Park via B4 Relay @ 2026-11-26  1:52 UTC (permalink / raw)
  To: Chuck Lever, Jeff Layton, NeilBrown, Olga Kornievskaia, Dai Ngo,
	Tom Talpey, Christoph Hellwig, Carlos Maiolino, Amir Goldstein
  Cc: Darrick J. Wong, Dave Chinner, Sergey Bashirov, Christian Brauner,
	linux-nfs, linux-xfs, linux-fsdevel, linux-kernel, Daejun Park

From: Daejun Park <daejun7.park@samsung.com>

Move the part of xfs_fs_map_blocks() that maps one extent of a pNFS
layout into a new helper, xfs_fs_map_extent(). The helper also
allocates the extent first if it is a hole in a write layout. The next
patch calls it once for each extent of a layout.

No change in behavior.

Signed-off-by: Daejun Park <daejun7.park@samsung.com>
---
 fs/xfs/xfs_pnfs.c | 137 +++++++++++++++++++++++++++++++-----------------------
 1 file changed, 80 insertions(+), 57 deletions(-)

diff --git a/fs/xfs/xfs_pnfs.c b/fs/xfs/xfs_pnfs.c
index 4a6c1a399d7..39d1c5271a7 100644
--- a/fs/xfs/xfs_pnfs.c
+++ b/fs/xfs/xfs_pnfs.c
@@ -122,6 +122,74 @@ xfs_fs_map_update_inode(
 	return xfs_log_force_inode(ip, false, NULL);
 }
 
+/*
+ * Map the extent of a pNFS layout that starts at @offset_fsb, allocating it
+ * first if it is a hole in a write layout, and set *@update_inode if it
+ * allocated. The mapping may end before or after @end. Called with the iolock
+ * and the invalidate lock held.
+ */
+static int
+xfs_fs_map_extent(
+	struct xfs_inode	*ip,
+	xfs_fileoff_t		offset_fsb,
+	loff_t			end,
+	bool			write,
+	struct xfs_bmbt_irec	*imap,
+	u64			*seq,
+	bool			*update_inode)
+{
+	struct xfs_mount	*mp = ip->i_mount;
+	xfs_fileoff_t		end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)end);
+	int			nimaps = 1;
+	uint			lock_flags;
+	int			error;
+
+	lock_flags = xfs_ilock_data_map_shared(ip);
+	/*
+	 * Map to the end of the extent that covers the start of the range,
+	 * so that a client doing I/O in pieces gets a layout it can use for
+	 * the pieces that follow.  Never map anything before the start of
+	 * the range: nfsd calls in here once per extent of a LAYOUTGET, for
+	 * the range that is left after the previous extent, and the mapping
+	 * can change in between, so a mapping that reaches back can overlap
+	 * one already in the layout.  Don't extend the mapping past EOF
+	 * beyond the range either: xfs_free_eofblocks() can free blocks past
+	 * EOF without breaking the layout.
+	 */
+	error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb,
+				imap, &nimaps, XFS_BMAPI_ENTIRE);
+	if (error) {
+		xfs_iunlock(ip, lock_flags);
+		return error;
+	}
+	if (nimaps)
+		xfs_trim_extent(imap, offset_fsb,
+				max_t(xfs_fileoff_t, end_fsb,
+				      XFS_B_TO_FSB(mp, XFS_ISIZE(ip))) -
+				offset_fsb);
+	*seq = xfs_iomap_inode_sequence(ip, 0);
+
+	ASSERT(!nimaps || imap->br_startblock != DELAYSTARTBLOCK);
+
+	if (!write || (nimaps && imap->br_startblock != HOLESTARTBLOCK)) {
+		xfs_iunlock(ip, lock_flags);
+		return 0;
+	}
+
+	if (end > XFS_ISIZE(ip))
+		end_fsb = xfs_iomap_eof_align_last_fsb(ip, end_fsb);
+	else if (nimaps)
+		end_fsb = min(end_fsb, imap->br_startoff + imap->br_blockcount);
+	xfs_iunlock(ip, lock_flags);
+
+	error = xfs_iomap_write_direct(ip, offset_fsb, end_fsb - offset_fsb, 0,
+			imap, seq);
+	if (error)
+		return error;
+	*update_inode = true;
+	return 0;
+}
+
 /*
  * Get a layout for the pNFS client.
  */
@@ -137,10 +205,7 @@ xfs_fs_map_blocks(
 	struct xfs_inode	*ip = XFS_I(inode);
 	struct xfs_mount	*mp = ip->i_mount;
 	struct xfs_bmbt_irec	imap;
-	xfs_fileoff_t		offset_fsb, end_fsb;
 	loff_t			limit;
-	int			nimaps = 1;
-	uint			lock_flags;
 	int			error = 0;
 	u64			seq;
 	bool			update_inode = false;
@@ -205,64 +270,22 @@ xfs_fs_map_blocks(
 				round_up(XFS_ISIZE(ip), mp->m_sb.sb_blocksize),
 				LLONG_MAX, NULL);
 
-	end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + length);
-	offset_fsb = XFS_B_TO_FSBT(mp, offset);
+	error = xfs_fs_map_extent(ip, XFS_B_TO_FSBT(mp, offset),
+			offset + length, write, &imap, &seq, &update_inode);
+	if (error)
+		goto out_unlock;
 
-	lock_flags = xfs_ilock_data_map_shared(ip);
 	/*
-	 * Map to the end of the extent that covers the start of the range,
-	 * so that a client doing I/O in pieces gets a layout it can use for
-	 * the pieces that follow.  Never map anything before the start of
-	 * the range: nfsd calls in here once per extent of a LAYOUTGET, for
-	 * the range that is left after the previous extent, and the mapping
-	 * can change in between, so a mapping that reaches back can overlap
-	 * one already in the layout.  Don't extend the mapping past EOF
-	 * beyond the range either: xfs_free_eofblocks() can free blocks past
-	 * EOF without breaking the layout.
+	 * Blocks past EOF that were allocated for buffered writes, such as
+	 * speculative preallocation, can be freed by xfs_free_eofblocks()
+	 * without breaking the layout unless the inode is marked as
+	 * preallocated. Mark it before handing them out for writing, as for
+	 * blocks allocated above.
 	 */
-	error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb,
-				&imap, &nimaps, XFS_BMAPI_ENTIRE);
-	if (error) {
-		xfs_iunlock(ip, lock_flags);
-		goto out_unlock;
-	}
-	if (nimaps)
-		xfs_trim_extent(&imap, offset_fsb,
-				max_t(xfs_fileoff_t, end_fsb,
-				      XFS_B_TO_FSB(mp, XFS_ISIZE(ip))) -
-				offset_fsb);
-	seq = xfs_iomap_inode_sequence(ip, 0);
-
-	ASSERT(!nimaps || imap.br_startblock != DELAYSTARTBLOCK);
-
-	if (write && (!nimaps || imap.br_startblock == HOLESTARTBLOCK)) {
-		if (offset + length > XFS_ISIZE(ip))
-			end_fsb = xfs_iomap_eof_align_last_fsb(ip, end_fsb);
-		else if (nimaps && imap.br_startblock == HOLESTARTBLOCK)
-			end_fsb = min(end_fsb, imap.br_startoff +
-					       imap.br_blockcount);
-		xfs_iunlock(ip, lock_flags);
-
-		error = xfs_iomap_write_direct(ip, offset_fsb,
-				end_fsb - offset_fsb, 0, &imap, &seq);
-		if (error)
-			goto out_unlock;
+	if (write && !(ip->i_diflags & XFS_DIFLAG_PREALLOC) &&
+	    imap.br_startoff + imap.br_blockcount >
+	    XFS_B_TO_FSB(mp, XFS_ISIZE(ip)))
 		update_inode = true;
-	} else {
-		xfs_iunlock(ip, lock_flags);
-
-		/*
-		 * Blocks past EOF that were allocated for buffered writes, such
-		 * as speculative preallocation, can be freed by
-		 * xfs_free_eofblocks() without breaking the layout unless the
-		 * inode is marked as preallocated. Mark it before handing them
-		 * out for writing, as for blocks allocated above.
-		 */
-		if (write && !(ip->i_diflags & XFS_DIFLAG_PREALLOC) &&
-		    imap.br_startoff + imap.br_blockcount >
-		    XFS_B_TO_FSB(mp, XFS_ISIZE(ip)))
-			update_inode = true;
-	}
 
 	if (update_inode) {
 		error = xfs_fs_map_update_inode(ip);

-- 
2.43.0



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v2 2/2] exportfs, xfs, nfsd: map a whole pNFS block layout in one ->map_blocks call
  2026-11-26  1:52 [PATCH v2 0/2] exportfs, xfs, nfsd: map a whole pNFS block layout in one ->map_blocks call Daejun Park via B4 Relay
  2026-11-26  1:52 ` [PATCH v2 1/2] xfs: factor the mapping of one pNFS extent out of xfs_fs_map_blocks() Daejun Park via B4 Relay
@ 2026-11-26  1:52 ` Daejun Park via B4 Relay
  2026-12-01  8:24   ` Christoph Hellwig
  2026-12-01  8:21 ` [PATCH v2 0/2] " Christoph Hellwig
  2 siblings, 1 reply; 6+ messages in thread
From: Daejun Park via B4 Relay @ 2026-11-26  1:52 UTC (permalink / raw)
  To: Chuck Lever, Jeff Layton, NeilBrown, Olga Kornievskaia, Dai Ngo,
	Tom Talpey, Christoph Hellwig, Carlos Maiolino, Amir Goldstein
  Cc: Darrick J. Wong, Dave Chinner, Sergey Bashirov, Christian Brauner,
	linux-nfs, linux-xfs, linux-fsdevel, linux-kernel, Daejun Park

From: Daejun Park <daejun7.park@samsung.com>

nfsd calls ->map_blocks once per extent of a block layout. Each call
takes the XFS locks, writes back the page cache and can force the log.
The mapping can change between the calls.

Let ->map_blocks fill an array. XFS fills it under one hold of the
iolock and the invalidate lock and forces the log at most once. It
stops at the maximum file size, where nfsd stopped. If a mapping fails,
the blocks allocated before it get neither the inode update nor the log
force. The LAYOUTGET fails.

The times are medians of LAYOUTGETs for 256 KiB, timed in
nfsd4_block_proc_layoutget() (QEMU, XFS on NVMe/TCP, pynfs, no KASAN or
lockdep). The gapped files have 32 blocks of 4 KiB written with
O_DIRECT, 8 KiB apart:

    READ, gapped:      64 calls -> 1, 6.7 -> 4.9 us
    RW, gapped:        32 log forces -> 1, 46.7 -> 1.8 ms
    READ, one extent:  1 call -> 1, no change (1.9 and 2.1 us)

Suggested-by: Darrick J. Wong <djwong@kernel.org>
Suggested-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Daejun Park <daejun7.park@samsung.com>
---
The medians are over 60 LAYOUTGETs from three boots per kernel, and 160
from eight boots for one extent. The median of one boot for one extent
is 0.5 to 2.4 us over both kernels. pynfs block tests over the block
layout and fstests generic/075, 091 and 263 over the block and SCSI
layouts give the same results as without this series, with KASAN and
lockdep. With XFS_DEBUG, generic/013, 075, 091, 112, 127 and 263 run,
and 075, 112 and 127 fail with fsx errors as without this series.
---
 fs/nfsd/blocklayout.c          | 137 ++++++++++++++++++++++-------------------
 fs/xfs/xfs_pnfs.c              |  54 ++++++++++------
 include/linux/exportfs_block.h |  17 +++--
 3 files changed, 121 insertions(+), 87 deletions(-)

diff --git a/fs/nfsd/blocklayout.c b/fs/nfsd/blocklayout.c
index c8942191b5f..90ed1cacf00 100644
--- a/fs/nfsd/blocklayout.c
+++ b/fs/nfsd/blocklayout.c
@@ -20,48 +20,19 @@
 
 
 /*
- * Get an extent from the file system that contains offset. It may start
- * below offset and may end before or after offset + length.
+ * Turn a mapping from the filesystem into an extent of the layout.
  */
 static __be32
-nfsd4_block_map_extent(struct inode *inode, const struct svc_fh *fhp,
-		u64 offset, u64 length, u32 iomode, u64 minlength,
-		struct pnfs_block_extent *bex)
+nfsd4_block_iomap_to_extent(const struct iomap *iomap, u32 iomode,
+		u64 minlength, struct pnfs_block_extent *bex)
 {
-	struct super_block *sb = inode->i_sb;
-	struct iomap iomap;
-	u32 device_generation = 0;
-	bool write;
-	int error;
-
-	/*
-	 * An RW layout with a minlength of zero only gets extents that can
-	 * be returned as READ_WRITE_DATA (RFC 5663, section 2.3.1), so do
-	 * not have the filesystem allocate blocks that would be refused.
-	 */
-	write = iomode != IOMODE_READ && minlength != 0;
-	error = sb->s_export_op->block_ops->map_blocks(inode, offset, length,
-			&iomap, write, &device_generation);
-	if (error) {
-		if (error == -ENXIO)
-			return nfserr_layoutunavailable;
-		return nfserrno(error);
-	}
-
-	if (WARN_ONCE(iomap.offset > offset ||
-		      offset - iomap.offset >= iomap.length,
-		      "pnfsd: %s ino %llu: filesystem returned extent %lld+%llu for offset %llu\n",
-		      sb->s_id, inode->i_ino, iomap.offset, iomap.length,
-		      offset))
-		return nfserr_layoutunavailable;
-
-	switch (iomap.type) {
+	switch (iomap->type) {
 	case IOMAP_MAPPED:
 		if (iomode == IOMODE_READ)
 			bex->es = PNFS_BLOCK_READ_DATA;
 		else
 			bex->es = PNFS_BLOCK_READWRITE_DATA;
-		bex->soff = iomap.addr;
+		bex->soff = iomap->addr;
 		break;
 	case IOMAP_UNWRITTEN:
 		if (iomode & IOMODE_RW) {
@@ -74,7 +45,7 @@ nfsd4_block_map_extent(struct inode *inode, const struct svc_fh *fhp,
 			}
 
 			bex->es = PNFS_BLOCK_INVALID_DATA;
-			bex->soff = iomap.addr;
+			bex->soff = iomap->addr;
 			break;
 		}
 		fallthrough;
@@ -83,22 +54,18 @@ nfsd4_block_map_extent(struct inode *inode, const struct svc_fh *fhp,
 			bex->es = PNFS_BLOCK_NONE_DATA;
 			break;
 		}
-		/* Not allocated for a minlength of zero, see above. */
+		/* Not allocated for a minlength of zero, see the caller. */
 		if (minlength == 0)
 			return nfserr_layoutunavailable;
 		fallthrough;
 	case IOMAP_DELALLOC:
 	default:
-		WARN(1, "pnfsd: filesystem returned %d extent\n", iomap.type);
+		WARN(1, "pnfsd: filesystem returned %d extent\n", iomap->type);
 		return nfserr_layoutunavailable;
 	}
 
-	error = nfsd4_set_deviceid(&bex->vol_id, fhp, device_generation);
-	if (error)
-		return nfserrno(error);
-
-	bex->foff = iomap.offset;
-	bex->len = iomap.length;
+	bex->foff = iomap->offset;
+	bex->len = iomap->length;
 	return nfs_ok;
 }
 
@@ -111,7 +78,14 @@ nfsd4_block_proc_layoutget(struct svc_rqst *rqstp, struct inode *inode,
 	struct pnfs_block_extent *first_bex, *last_bex;
 	u64 offset = seg->offset, length = seg->length;
 	u32 i, nr_extents_max, block_size = i_blocksize(inode);
+	struct super_block *sb = inode->i_sb;
+	struct nfsd4_deviceid vol_id;
+	struct iomap *iomaps = NULL;
+	u32 device_generation = 0;
+	unsigned int nr_iomaps;
 	__be32 nfserr;
+	bool write;
+	int error;
 
 	if (locks_in_grace(SVC_NET(rqstp)))
 		return nfserr_grace;
@@ -154,23 +128,66 @@ nfsd4_block_proc_layoutget(struct svc_rqst *rqstp, struct inode *inode,
 		goto out_error;
 	bl->nr_extents = nr_extents_max;
 	args->lg_content = bl;
+	iomaps = kvmalloc_objs(*iomaps, nr_extents_max);
+	if (!iomaps)
+		goto out_error;
 
-	for (i = 0; i < bl->nr_extents; i++) {
+	/*
+	 * Get all extents in one call, so that the filesystem maps them in
+	 * one go and each one follows the one before it. An RW layout with a
+	 * minlength of zero only gets extents that can be returned as
+	 * READ_WRITE_DATA (RFC 5663, section 2.3.1), so do not have the
+	 * filesystem allocate blocks that would be refused.
+	 */
+	nr_iomaps = nr_extents_max;
+	write = seg->iomode != IOMODE_READ && args->lg_minlength != 0;
+	error = sb->s_export_op->block_ops->map_blocks(inode, offset, length,
+			iomaps, &nr_iomaps, write, &device_generation);
+	if (error) {
+		if (error == -ENXIO)
+			nfserr = nfserr_layoutunavailable;
+		else
+			nfserr = nfserrno(error);
+		goto out_error;
+	}
+
+	nfserr = nfserr_layoutunavailable;
+	if (WARN_ONCE(!nr_iomaps || nr_iomaps > nr_extents_max,
+		      "pnfsd: %s ino %llu: filesystem returned %u extents for %u\n",
+		      sb->s_id, inode->i_ino, nr_iomaps, nr_extents_max))
+		goto out_error;
+
+	error = nfsd4_set_deviceid(&vol_id, fhp, device_generation);
+	if (error) {
+		nfserr = nfserrno(error);
+		goto out_error;
+	}
+
+	for (i = 0; i < nr_iomaps; i++) {
+		const struct iomap *iomap = iomaps + i;
 		struct pnfs_block_extent *bex = bl->extents + i;
-		u64 bex_length;
 
-		nfserr = nfsd4_block_map_extent(inode, fhp, offset, length,
-				seg->iomode, args->lg_minlength, bex);
+		nfserr = nfserr_layoutunavailable;
+		if (WARN_ONCE(iomap->offset > offset ||
+			      offset - iomap->offset >= iomap->length,
+			      "pnfsd: %s ino %llu: filesystem returned extent %lld+%llu for offset %llu\n",
+			      sb->s_id, inode->i_ino, iomap->offset,
+			      iomap->length, offset))
+			goto out_error;
+
+		nfserr = nfsd4_block_iomap_to_extent(iomap, seg->iomode,
+				args->lg_minlength, bex);
 		if (nfserr != nfs_ok)
 			goto out_error;
+		bex->vol_id = vol_id;
 
 		/*
-		 * Each extent after the first was mapped for the range that
-		 * starts where the previous extent ends, but the filesystem
-		 * may return a mapping that starts below that point. Trim
-		 * it, as RFC 5663 section 2.3.1 does not allow extents to
-		 * overlap. nfsd4_block_map_extent() made sure the mapping
-		 * contains offset. NONE_DATA extents have no volume offset.
+		 * Each mapping after the first should start where the previous
+		 * extent ends, but the filesystem may return a mapping that
+		 * starts below that point. Trim it, as RFC 5663 section 2.3.1
+		 * does not allow extents to overlap. The check above made sure
+		 * the mapping contains offset. NONE_DATA extents have no volume
+		 * offset.
 		 */
 		if (i > 0 && bex->foff < offset) {
 			u64 skip = offset - bex->foff;
@@ -180,18 +197,11 @@ nfsd4_block_proc_layoutget(struct svc_rqst *rqstp, struct inode *inode,
 			if (bex->es != PNFS_BLOCK_NONE_DATA)
 				bex->soff += skip;
 		}
-
-		bex_length = bex->len - (offset - bex->foff);
-		/* There is nothing to map past the maximum file size. */
-		if (bex_length >= length ||
-		    bex->foff + bex->len >= inode->i_sb->s_maxbytes) {
-			bl->nr_extents = i + 1;
-			break;
-		}
-
 		offset = bex->foff + bex->len;
-		length -= bex_length;
 	}
+	bl->nr_extents = nr_iomaps;
+	kvfree(iomaps);
+	iomaps = NULL;
 
 	first_bex = bl->extents;
 	last_bex = bl->extents + bl->nr_extents - 1;
@@ -208,6 +218,7 @@ nfsd4_block_proc_layoutget(struct svc_rqst *rqstp, struct inode *inode,
 	return nfs_ok;
 
 out_error:
+	kvfree(iomaps);
 	seg->length = 0;
 	return nfserr;
 }
diff --git a/fs/xfs/xfs_pnfs.c b/fs/xfs/xfs_pnfs.c
index 39d1c5271a7..93764071804 100644
--- a/fs/xfs/xfs_pnfs.c
+++ b/fs/xfs/xfs_pnfs.c
@@ -148,13 +148,13 @@ xfs_fs_map_extent(
 	/*
 	 * Map to the end of the extent that covers the start of the range,
 	 * so that a client doing I/O in pieces gets a layout it can use for
-	 * the pieces that follow.  Never map anything before the start of
-	 * the range: nfsd calls in here once per extent of a LAYOUTGET, for
-	 * the range that is left after the previous extent, and the mapping
-	 * can change in between, so a mapping that reaches back can overlap
-	 * one already in the layout.  Don't extend the mapping past EOF
-	 * beyond the range either: xfs_free_eofblocks() can free blocks past
-	 * EOF without breaking the layout.
+	 * the pieces that follow. Never map anything before @offset_fsb:
+	 * the extent there can have merged with the one before it, by the
+	 * allocation of a hole for this layout or by the completion of a
+	 * direct write, and a mapping that reaches back would overlap the
+	 * previous one. Don't extend the mapping past EOF beyond the range
+	 * either: xfs_free_eofblocks() can free blocks past EOF without
+	 * breaking the layout.
 	 */
 	error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb,
 				imap, &nimaps, XFS_BMAPI_ENTIRE);
@@ -192,23 +192,29 @@ xfs_fs_map_extent(
 
 /*
  * Get a layout for the pNFS client.
+ *
+ * Map the range, or as much of it as fits into @iomaps, one extent after the
+ * other, each starting where the one before it ends. Holding the iolock and
+ * the invalidate lock across the loop keeps other I/O and page faults from
+ * starting in between.
  */
 static int
 xfs_fs_map_blocks(
 	struct inode		*inode,
 	loff_t			offset,
 	u64			length,
-	struct iomap		*iomap,
+	struct iomap		*iomaps,
+	unsigned int		*nr_iomaps,
 	bool			write,
 	u32			*device_generation)
 {
 	struct xfs_inode	*ip = XFS_I(inode);
 	struct xfs_mount	*mp = ip->i_mount;
-	struct xfs_bmbt_irec	imap;
-	loff_t			limit;
+	unsigned int		nr = 0;
 	int			error = 0;
-	u64			seq;
 	bool			update_inode = false;
+	xfs_fileoff_t		offset_fsb, end_fsb;
+	loff_t			limit;
 
 	if (xfs_is_shutdown(mp))
 		return -EIO;
@@ -270,10 +276,21 @@ xfs_fs_map_blocks(
 				round_up(XFS_ISIZE(ip), mp->m_sb.sb_blocksize),
 				LLONG_MAX, NULL);
 
-	error = xfs_fs_map_extent(ip, XFS_B_TO_FSBT(mp, offset),
-			offset + length, write, &imap, &seq, &update_inode);
-	if (error)
-		goto out_unlock;
+	offset_fsb = XFS_B_TO_FSBT(mp, offset);
+	end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + length);
+	while (nr < *nr_iomaps && offset_fsb < end_fsb) {
+		struct xfs_bmbt_irec	imap;
+		u64			seq;
+
+		error = xfs_fs_map_extent(ip, offset_fsb, offset + length,
+				write, &imap, &seq, &update_inode);
+		if (error)
+			goto out_unlock;
+		error = xfs_bmbt_to_iomap(ip, &iomaps[nr++], &imap, 0, 0, seq);
+		if (error)
+			goto out_unlock;
+		offset_fsb = imap.br_startoff + imap.br_blockcount;
+	}
 
 	/*
 	 * Blocks past EOF that were allocated for buffered writes, such as
@@ -283,8 +300,7 @@ xfs_fs_map_blocks(
 	 * blocks allocated above.
 	 */
 	if (write && !(ip->i_diflags & XFS_DIFLAG_PREALLOC) &&
-	    imap.br_startoff + imap.br_blockcount >
-	    XFS_B_TO_FSB(mp, XFS_ISIZE(ip)))
+	    offset_fsb > XFS_B_TO_FSB(mp, XFS_ISIZE(ip)))
 		update_inode = true;
 
 	if (update_inode) {
@@ -294,9 +310,9 @@ xfs_fs_map_blocks(
 	}
 	xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL);
 
-	error = xfs_bmbt_to_iomap(ip, iomap, &imap, 0, 0, seq);
+	*nr_iomaps = nr;
 	*device_generation = mp->m_generation;
-	return error;
+	return 0;
 out_unlock:
 	xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL);
 	return error;
diff --git a/include/linux/exportfs_block.h b/include/linux/exportfs_block.h
index 21e61fc0124..f85ce1a67e3 100644
--- a/include/linux/exportfs_block.h
+++ b/include/linux/exportfs_block.h
@@ -43,13 +43,20 @@ struct exportfs_block_ops {
 
 	/*
 	 * Map blocks for direct block access.
-	 * If @write is %true, also allocate the blocks for the range if needed.
-	 * The mapping returned must contain @offset. It may start before
-	 * @offset and may end before or after @offset + @len.
+	 * If @write is %true, allocate any hole in the range mapped, so that no
+	 * mapping is a hole. No mapping may be IOMAP_DELALLOC. @len and
+	 * *@nr_iomaps are at least one. Return no mapping that starts at or
+	 * past the maximum file size. @offset may be negative or past it, and
+	 * @offset + @len may lie past it or wrap. On success, fill in between
+	 * one and *@nr_iomaps mappings and set *@nr_iomaps to their number. On
+	 * error, the mappings and *@nr_iomaps are undefined. The first mapping
+	 * must contain @offset, and each further one the offset at which the
+	 * one before it ends. A mapping may start before that offset. Only the
+	 * last one may reach @offset + @len, and it may end before or after it.
 	 */
 	int (*map_blocks)(struct inode *inode, loff_t offset, u64 len,
-			struct iomap *iomap, bool write,
-			u32 *device_generation);
+			struct iomap *iomaps, unsigned int *nr_iomaps,
+			bool write, u32 *device_generation);
 
 	/*
 	 * Commit blocks previously handed out by ->map_blocks and written to by

-- 
2.43.0



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v2 1/2] xfs: factor the mapping of one pNFS extent out of xfs_fs_map_blocks()
  2026-11-26  1:52 ` [PATCH v2 1/2] xfs: factor the mapping of one pNFS extent out of xfs_fs_map_blocks() Daejun Park via B4 Relay
@ 2026-12-01  8:17   ` Christoph Hellwig
  0 siblings, 0 replies; 6+ messages in thread
From: Christoph Hellwig @ 2026-12-01  8:17 UTC (permalink / raw)
  To: daejun7.park
  Cc: Chuck Lever, Jeff Layton, NeilBrown, Olga Kornievskaia, Dai Ngo,
	Tom Talpey, Christoph Hellwig, Carlos Maiolino, Amir Goldstein,
	Darrick J. Wong, Dave Chinner, Sergey Bashirov, Christian Brauner,
	linux-nfs, linux-xfs, linux-fsdevel, linux-kernel

> +	 * can change in between, so a mapping that reaches back can overlap
> +	 * one already in the layout.  Don't extend the mapping past EOF
> +	 * beyond the range either: xfs_free_eofblocks() can free blocks past
> +	 * EOF without breaking the layout.
> +	 */
> +	error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb,
> +				imap, &nimaps, XFS_BMAPI_ENTIRE);
> +	if (error) {
> +		xfs_iunlock(ip, lock_flags);
> +		return error;
> +	}
> +	if (nimaps)
> +		xfs_trim_extent(imap, offset_fsb,
> +				max_t(xfs_fileoff_t, end_fsb,
> +				      XFS_B_TO_FSB(mp, XFS_ISIZE(ip))) -
> +				offset_fsb);

nimaps must be non-NULL here (I should have spotted that in your
original patch adding the check).

> +	*seq = xfs_iomap_inode_sequence(ip, 0);
> +
> +	ASSERT(!nimaps || imap->br_startblock != DELAYSTARTBLOCK);

Or the very original addition.  IIRC we used to have a corner case
where 0 nimaps with a 0 return value could happen, but we fied that a long
time ago.

> +
> +	if (!write || (nimaps && imap->br_startblock != HOLESTARTBLOCK)) {
> +		xfs_iunlock(ip, lock_flags);
> +		return 0;
> +	}
> +
> +	if (end > XFS_ISIZE(ip))
> +		end_fsb = xfs_iomap_eof_align_last_fsb(ip, end_fsb);
> +	else if (nimaps)
> +		end_fsb = min(end_fsb, imap->br_startoff + imap->br_blockcount);
> +	xfs_iunlock(ip, lock_flags);
> +
> +	error = xfs_iomap_write_direct(ip, offset_fsb, end_fsb - offset_fsb, 0,
> +			imap, seq);
> +	if (error)
> +		return error;
> +	*update_inode = true;

Maybe keep the original flow here that leaves the alloction is the more
exceptional path in the branch?

> +	error = xfs_fs_map_extent(ip, XFS_B_TO_FSBT(mp, offset),
> +			offset + length, write, &imap, &seq, &update_inode);

Passing the offset as FSB and the end as bytes is a bit odd.  I'll
see if there is a good reason for that in the next patch, but in doubt
we should avoid this if possible.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2 0/2] exportfs, xfs, nfsd: map a whole pNFS block layout in one ->map_blocks call
  2026-11-26  1:52 [PATCH v2 0/2] exportfs, xfs, nfsd: map a whole pNFS block layout in one ->map_blocks call Daejun Park via B4 Relay
  2026-11-26  1:52 ` [PATCH v2 1/2] xfs: factor the mapping of one pNFS extent out of xfs_fs_map_blocks() Daejun Park via B4 Relay
  2026-11-26  1:52 ` [PATCH v2 2/2] exportfs, xfs, nfsd: map a whole pNFS block layout in one ->map_blocks call Daejun Park via B4 Relay
@ 2026-12-01  8:21 ` Christoph Hellwig
  2 siblings, 0 replies; 6+ messages in thread
From: Christoph Hellwig @ 2026-12-01  8:21 UTC (permalink / raw)
  To: daejun7.park
  Cc: Chuck Lever, Jeff Layton, NeilBrown, Olga Kornievskaia, Dai Ngo,
	Tom Talpey, Christoph Hellwig, Carlos Maiolino, Amir Goldstein,
	Darrick J. Wong, Dave Chinner, Sergey Bashirov, Christian Brauner,
	linux-nfs, linux-xfs, linux-fsdevel, linux-kernel

On Thu, Nov 26, 2026 at 10:52:24AM +0900, Daejun Park via B4 Relay wrote:
> The series needs four fixes. The first two are posted separately, and
> the other two are in nfsd-testing:
> 
> - nfsd: stop a block layout at the maximum file size
> - xfs: punch delalloc blocks past EOF before mapping a pNFS layout
> - nfsd: do not return overlapping extents in a block layout
> - nfsd: do not allocate blocks for an RW layout with a minlength of zero
> 
> Once they are in mainline, I will rebase the series for the VFS tree,
> as Chuck suggested.

Can you supply a git tree with all the base patches applied?  Right now
I have a bit of a hard time getting everything together..


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2 2/2] exportfs, xfs, nfsd: map a whole pNFS block layout in one ->map_blocks call
  2026-11-26  1:52 ` [PATCH v2 2/2] exportfs, xfs, nfsd: map a whole pNFS block layout in one ->map_blocks call Daejun Park via B4 Relay
@ 2026-12-01  8:24   ` Christoph Hellwig
  0 siblings, 0 replies; 6+ messages in thread
From: Christoph Hellwig @ 2026-12-01  8:24 UTC (permalink / raw)
  To: daejun7.park
  Cc: Chuck Lever, Jeff Layton, NeilBrown, Olga Kornievskaia, Dai Ngo,
	Tom Talpey, Christoph Hellwig, Carlos Maiolino, Amir Goldstein,
	Darrick J. Wong, Dave Chinner, Sergey Bashirov, Christian Brauner,
	linux-nfs, linux-xfs, linux-fsdevel, linux-kernel

On Thu, Nov 26, 2026 at 10:52:26AM +0900, Daejun Park via B4 Relay wrote:
> ---
> The medians are over 60 LAYOUTGETs from three boots per kernel, and 160
> from eight boots for one extent. The median of one boot for one extent
> is 0.5 to 2.4 us over both kernels. pynfs block tests over the block
> layout and fstests generic/075, 091 and 263 over the block and SCSI
> layouts give the same results as without this series, with KASAN and
> lockdep. With XFS_DEBUG, generic/013, 075, 091, 112, 127 and 263 run,
> and 075, 112 and 127 fail with fsx errors as without this series.

Interesting, I've not usually seen them fail with pnfs layouts.  Do
you only see this with DEBUG enabled?  What is the failure mode?

> +++ b/fs/nfsd/blocklayout.c
> @@ -20,48 +20,19 @@
>  
>  
>  /*
> - * Get an extent from the file system that contains offset. It may start
> - * below offset and may end before or after offset + length.
> + * Turn a mapping from the filesystem into an extent of the layout.
>   */
>  static __be32
> -nfsd4_block_map_extent(struct inode *inode, const struct svc_fh *fhp,
> -		u64 offset, u64 length, u32 iomode, u64 minlength,
> -		struct pnfs_block_extent *bex)
> +nfsd4_block_iomap_to_extent(const struct iomap *iomap, u32 iomode,
> +		u64 minlength, struct pnfs_block_extent *bex)

Can you split the refactoring of this helper into a prep patch?  That'll
make the real change much easier to read.

Also maybe break up the nfsd/exportfs change for the new interface
into one patch, and then another one for filling in more than a
single extent in xfs, as that might be useful if we have to bisect
bugs later.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-12-01  8:24 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-26  1:52 [PATCH v2 0/2] exportfs, xfs, nfsd: map a whole pNFS block layout in one ->map_blocks call Daejun Park via B4 Relay
2026-11-26  1:52 ` [PATCH v2 1/2] xfs: factor the mapping of one pNFS extent out of xfs_fs_map_blocks() Daejun Park via B4 Relay
2026-12-01  8:17   ` Christoph Hellwig
2026-11-26  1:52 ` [PATCH v2 2/2] exportfs, xfs, nfsd: map a whole pNFS block layout in one ->map_blocks call Daejun Park via B4 Relay
2026-12-01  8:24   ` Christoph Hellwig
2026-12-01  8:21 ` [PATCH v2 0/2] " Christoph Hellwig

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox