xfs: fix overlapping extents returned for pNFS LAYOUTGET
Subsystems (3)
Dai Ngo
reviewerPATCH 1/1 xfs: fix overlapping extents returned for pNFS LAYOUTGET
xfs_fs_map_blocks() currently passes XFS_BMAPI_ENTIRE to xfs_bmapi_read(), which causes the bmap code to expand the mapping to cover the entire extent rather than the requested range.
A single LAYOUTGET request from the client can cause the server to issue multiple calls to xfs_fs_map_blocks() for different offsets within the same extent. Because the use of XFS_BMAPI_ENTIRE flag, these calls can produce overlapping mappings.
As a result, the LAYOUTGET reply sent to the NFS client may contain overlapping extents. This creates ambiguity in extent selection for a given file range, which can lead to incorrect device selection, inconsistent handling of datastate, and ultimately data corruption or protocol violations on the client side.
Problem discovered with xfstest generic/075 test using NFSv4.2 mount with SCSI layout.
Fix this by replacing the XFS_BMAPI_ENTIRE flag with '0' so that xfs_bmapi_read() returns only the mapping for the requested range.
Also drop the check for (!error) since it was checked after call to xfs_bmapi_read().
--- fs/xfs/xfs_pnfs.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
- This patch is based on top of the patch:
xfs: fix use of uninitialized imap in xfs_fs_map_blocks error path
fs/xfs/xfs_pnfs.c +3 -3
@@ -118,7 +118,7 @@ xfs_fs_map_blocks(struct xfs_bmbt_irec imap;xfs_fileoff_t offset_fsb, end_fsb;loff_t limit; -int bmapi_flags =XFS_BMAPI_ENTIRE ; +int bmapi_flags;int nimaps =1 ;uint lock_flags;int error =0 ;
@@ -172,6 +172,7 @@ xfs_fs_map_blocks( offset_fsb =XFS_B_TO_FSBT (mp, offset); lock_flags =xfs_ilock_data_map_shared (ip); + bmapi_flags =0 ;/* return map for requested range only */ error =xfs_bmapi_read (ip, offset_fsb, end_fsb - offset_fsb, &imap, &nimaps, bmapi_flags);if (error) {
@@ -182,8 +183,7 @@ xfs_fs_map_blocks(ASSERT (!nimaps || imap.br_startblock !=DELAYSTARTBLOCK ); -if (!error && write && - (!nimaps || imap.br_startblock ==HOLESTARTBLOCK )) { +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 )
Signature · 1 line
2.47.3
Quoted 3 lines · Dai Ngo
xfs_fs_map_blocks()currently passesXFS_BMAPI_ENTIREtoxfs_bmapi_read(), which causes the bmap code to expand the mapping to cover the entire extent rather than the requested range.
Nitpicking: _ENTIRE causes bmapi_read to return the whole extent instead of trimming it down to the requested range.
Quoted 5 lines
A single LAYOUTGET request from the client can cause the server to issue multiple calls to
xfs_fs_map_blocks()for different offsets within the same extent. Because the use ofXFS_BMAPI_ENTIREflag, these calls can produce overlapping mappings.
Quoted 8 lines
As a result, the LAYOUTGET reply sent to the NFS client may contain overlapping extents. This creates ambiguity in extent selection for a given file range, which can lead to incorrect device selection, inconsistent handling of datastate, and ultimately data corruption or protocol violations on the client side.
Problem discovered with xfstest generic/075 test using NFSv4.2 mount with SCSI layout.
Might be helpful to provide an example of the request vs. the overlapping layouts. IIRC the client asks for a layout for the first 32 fsblocks of the file. On the first call to xfs_fs_map_blocks, block 0 is a single unwritten mapping, so that gets returned.
Meanwhile, another thread fallocates block 2 and gets lucky in that an adjacent block is free, so the first mapping in the file is now 2 unwritten fsblocks starting at 0. This can happen because we don't hold i_rwsem (or the ILOCK) between calls to ->map_blocks.
Returning to the first thread, it calls xfs_fs_map_blocks again to map block 1. However, the mapping's been changed, so we now return the entire 2-fsblock mapping. What gets sent to the client is
{.offset = 0, .length = 4096, .addr = X, .dev = Y}, {.offset = 0, .length = 8192, .addr = X, .dev = Y},
and the client rejects that as overlapping. Right?
Quoted 5 lines
Fix this by replacing the
XFS_BMAPI_ENTIREflag with '0' so thatxfs_bmapi_read()returns only the mapping for the requested range.Also drop the check for (!error) since it was checked after call to
xfs_bmapi_read().
Quoted 14 lines
Fixes cc6c40e09d7b1 ("NFSD/blocklayout: Support multiple extents per LAYOUTGET").Signed-off-by Dai Ngo <[email protected]>--- fs/xfs/xfs_pnfs.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)- This patch is based on top of the patch:
xfs: fix use of uninitialized imap inxfs_fs_map_blockserror pathfs/xfs/xfs_pnfs.c +0
-0
fs/xfs/xfs_pnfs.c xfs_fs_map_blocks( · line 121
Quoted 5 lines
fs/xfs/xfs_pnfs.c +1
-1@@ -118,7 +118,7 @@ xfs_fs_map_blocks(struct xfs_bmbt_irec imap;xfs_fileoff_t offset_fsb, end_fsb;loff_t limit; -int bmapi_flags =XFS_BMAPI_ENTIRE ; +int bmapi_flags;
Why not just replace the argument to xfs_bmapi_read with a constant zero?
--D
Quoted 25 lines
fs/xfs/xfs_pnfs.c +2
-2int nimaps =1 ;uint lock_flags;int error =0 ;@@ -172,6 +172,7 @@ xfs_fs_map_blocks( offset_fsb =XFS_B_TO_FSBT (mp, offset); lock_flags =xfs_ilock_data_map_shared (ip); + bmapi_flags =0 ;/* return map for requested range only */ error =xfs_bmapi_read (ip, offset_fsb, end_fsb - offset_fsb, &imap, &nimaps, bmapi_flags);if (error) {@@ -182,8 +183,7 @@ xfs_fs_map_blocks(ASSERT (!nimaps || imap.br_startblock !=DELAYSTARTBLOCK ); -if (!error && write && - (!nimaps || imap.br_startblock ==HOLESTARTBLOCK )) { +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 )Signature · 1 line
2.47.3
Dai Ngo
reviewerThank you for your review!
Quoted 6 lines · Darrick J. Wong
Quoted 3 lines · Dai Ngo
xfs_fs_map_blocks()currently passesXFS_BMAPI_ENTIREtoxfs_bmapi_read(), which causes the bmap code to expand the mapping to cover the entire extent rather than the requested range.Nitpicking:
_ENTIREcausesbmapi_readto return the whole extent instead of trimming it down to the requested range.
Fix in v2.
Quoted 19 lines
Quoted 6 lines
A single LAYOUTGET request from the client can cause the server to issue multiple calls to
xfs_fs_map_blocks()for different offsets within the same extent. Because the use ofXFS_BMAPI_ENTIREflag, these calls can produce overlapping mappings.Quoted 8 lines
As a result, the LAYOUTGET reply sent to the NFS client may contain overlapping extents. This creates ambiguity in extent selection for a given file range, which can lead to incorrect device selection, inconsistent handling of datastate, and ultimately data corruption or protocol violations on the client side.
Problem discovered with xfstest generic/075 test using NFSv4.2 mount with SCSI layout.
Might be helpful to provide an example of the request vs. the overlapping layouts. IIRC the client asks for a layout for the first 32 fsblocks of the file. On the first call to
xfs_fs_map_blocks, block 0 is a single unwritten mapping, so that gets returned.
Quoted 8 lines
Meanwhile, another thread fallocates block 2 and gets lucky in that an adjacent block is free, so the first mapping in the file is now 2 unwritten fsblocks starting at 0. This can happen because we don't hold
i_rwsem(or the ILOCK) between calls to ->map_blocks.Returning to the first thread, it calls
xfs_fs_map_blocksagain to map block 1. However, the mapping's been changed, so we now return the entire 2-fsblock mapping.
I'm not sure why the file map gets change between the successive calls from the same thread that services the LAYOUTGET. This test does lots of ALLOCATE and DEALLOCATE ops prior to the LAYOUTGET.
Quoted 4 lines
What gets sent to the client is
{.offset = 0, .length = 4096, .addr = X, .dev = Y}, {.offset = 0, .length = 8192, .addr = X, .dev = Y},
Yes, I will include on-the-wire capture of a LAYOUTGET operation and reply showing the overlapping extents in v2.
Quoted 1 line
and the client rejects that as overlapping. Right?
The Linux client uses a red-black tree to maintain these extents. Overlapping extents can cause the client to select the wrong extent, resulting in intermittent test failures.
Quoted 7 lines
Quoted 6 lines
Fix this by replacing the
XFS_BMAPI_ENTIREflag with '0' so thatxfs_bmapi_read()returns only the mapping for the requested range.Also drop the check for (!error) since it was checked after call to
xfs_bmapi_read().Cc <[email protected]> # v6.19
Fix in v2.
Quoted 10 lines
Quoted 10 lines
Fixes cc6c40e09d7b1 ("NFSD/blocklayout: Support multiple extents per LAYOUTGET").Signed-off-by Dai Ngo <[email protected]>--- fs/xfs/xfs_pnfs.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)- This patch is based on top of the patch:
xfs: fix use of uninitialized imap inxfs_fs_map_blockserror path
Quoted 12 lines
fs/xfs/xfs_pnfs.c xfs_fs_map_blocks( · line 121
Quoted 10 lines
fs/xfs/xfs_pnfs.c +1
-1@@ -118,7 +118,7 @@ xfs_fs_map_blocks(struct xfs_bmbt_irec imap;xfs_fileoff_t offset_fsb, end_fsb;loff_t limit; -int bmapi_flags =XFS_BMAPI_ENTIRE ; +int bmapi_flags;Why not just replace the argument to
xfs_bmapi_readwith a constant zero?
This makes the code easier to understand since there is no comment describing the meaning of the '0' argument passed toxfs_bmapi_read().
Thanks,
-Dai
Quoted 28 lines
--D
Quoted 25 lines
fs/xfs/xfs_pnfs.c +2
-2int nimaps =1 ;uint lock_flags;int error =0 ;@@ -172,6 +172,7 @@ xfs_fs_map_blocks( offset_fsb =XFS_B_TO_FSBT (mp, offset); lock_flags =xfs_ilock_data_map_shared (ip); + bmapi_flags =0 ;/* return map for requested range only */ error =xfs_bmapi_read (ip, offset_fsb, end_fsb - offset_fsb, &imap, &nimaps, bmapi_flags);if (error) {@@ -182,8 +183,7 @@ xfs_fs_map_blocks(ASSERT (!nimaps || imap.br_startblock !=DELAYSTARTBLOCK ); -if (!error && write && - (!nimaps || imap.br_startblock ==HOLESTARTBLOCK )) { +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 )Signature · 1 line
2.47.3
Quoted 10 lines · Dai Ngo
A single LAYOUTGET request from the client can cause the server to issue multiple calls to
xfs_fs_map_blocks()for different offsets within the same extent. Because the use ofXFS_BMAPI_ENTIREflag, these calls can produce overlapping mappings.As a result, the LAYOUTGET reply sent to the NFS client may contain overlapping extents. This creates ambiguity in extent selection for a given file range, which can lead to incorrect device selection, inconsistent handling of datastate, and ultimately data corruption or protocol violations on the client side.
Please also add a check to the client that catches this and doesn't use the layout that has extents outside the requested range. And maybe warn about it as well.
Quoted 11 lines
Also drop the check for (!error) since it was checked after call to
xfs_bmapi_read().Fixes cc6c40e09d7b1 ("NFSD/blocklayout: Support multiple extents per LAYOUTGET").Signed-off-by Dai Ngo <[email protected]>--- fs/xfs/xfs_pnfs.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)- This patch is based on top of the patch:
xfs: fix use of uninitialized imap inxfs_fs_map_blockserror path
The error changes should go into that patch, so please resend it with that fixes. Maybe as a series together with this patch to keep them together.
fs/xfs/xfs_pnfs.c xfs_fs_map_blocks( · line 175
Quoted 5 lines
fs/xfs/xfs_pnfs.c +1
-0@@ -172,6 +172,7 @@ xfs_fs_map_blocks( offset_fsb =XFS_B_TO_FSBT (mp, offset); lock_flags =xfs_ilock_data_map_shared (ip); + bmapi_flags =0 ;/* return map for requested range only */
Just remove the variable and hard code the 0 in the xfs_bmapi_read call.
Dai Ngo
reviewerQuoted 6 lines · Christoph Hellwig
Quoted 5 lines · Dai Ngo
A single LAYOUTGET request from the client can cause the server to issue multiple calls to
xfs_fs_map_blocks()for different offsets within the same extent. Because the use ofXFS_BMAPI_ENTIREflag, these calls can produce overlapping mappings.
Quoted 8 lines
Quoted 5 lines
As a result, the LAYOUTGET reply sent to the NFS client may contain overlapping extents. This creates ambiguity in extent selection for a given file range, which can lead to incorrect device selection, inconsistent handling of datastate, and ultimately data corruption or protocol violations on the client side.
Please also add a check to the client that catches this and doesn't use the layout that has extents outside the requested range. And maybe warn about it as well.
The returned extents cover exactly the range requested in the LAYOUTGET op. However these extents are overlapping. For example, here is the on-the-wire capture of the LAYOUTGET operation and reply showing the overlapping extents:
Network File System, Ops(3): SEQUENCE, PUTFH, LAYOUTGET
[Program Version: 4]
[V4 Procedure: COMPOUND (1)]
Tag: <EMPTY>
minorversion: 2
Operations (count: 3): SEQUENCE, PUTFH, LAYOUTGET
Opcode: SEQUENCE (53)
Opcode: PUTFH (22)
Opcode: LAYOUTGET (50)
layout available?: No
layout type: LAYOUT4_SCSI (5)
IO mode: IOMODE_RW (2)
offset: 122880
length: 65536
min length: 4096
StateID
maxcount: 4096
[Main Opcode: LAYOUTGET (50)]
Network File System, Ops(3): SEQUENCE PUTFH LAYOUTGET
[Program Version: 4]
[V4 Procedure: COMPOUND (1)]
Status: NFS4_OK (0)
Tag: <EMPTY>
Operations (count: 3)
Opcode: SEQUENCE (53)
Opcode: PUTFH (22)
Opcode: LAYOUTGET (50)
Status: NFS4_OK (0)
return on close?: Yes
StateID
Layout Segment (count: 1)
offset: 122880
length: 77824
IO mode: IOMODE_RW (2)
layout type: LAYOUT4_SCSI (5)
SCSI Extents (count: 2)
extent 0
device ID: 01000000000000000000000000000000 file offset: 122880
length: 53248
volume offset: 339460096
extent state: INVALID_DATA (2)
extent 1
device ID: 01000000000000000000000000000000 file offset: 122880
length: 77824
volume offset: 339460096
extent state: INVALID_DATA (2)
[Main Opcode: LAYOUTGET (50)]
-Dai
Quoted 23 lines
Quoted 4 lines
Also drop the check for (!error) since it was checked after call to
xfs_bmapi_read().Quoted 8 lines
Fixes cc6c40e09d7b1 ("NFSD/blocklayout: Support multiple extents per LAYOUTGET").Signed-off-by Dai Ngo <[email protected]>--- fs/xfs/xfs_pnfs.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)- This patch is based on top of the patch:
xfs: fix use of uninitialized imap inxfs_fs_map_blockserror pathThe error changes should go into that patch, so please resend it with that fixes. Maybe as a series together with this patch to keep them together.
fs/xfs/xfs_pnfs.c xfs_fs_map_blocks( · line 175
Quoted 5 lines
fs/xfs/xfs_pnfs.c +1
-0@@ -172,6 +172,7 @@ xfs_fs_map_blocks( offset_fsb =XFS_B_TO_FSBT (mp, offset); lock_flags =xfs_ilock_data_map_shared (ip); + bmapi_flags =0 ;/* return map for requested range only */ Just remove the variable and hard code the 0 in the
xfs_bmapi_readcall.
Dai Ngo
reviewerHi Christoph,
Quoted 63 lines · Dai Ngo
Quoted 6 lines · Christoph Hellwig
Quoted 5 lines · Dai Ngo
A single LAYOUTGET request from the client can cause the server to issue multiple calls to
xfs_fs_map_blocks()for different offsets within the same extent. Because the use ofXFS_BMAPI_ENTIREflag, these calls can produce overlapping mappings.Quoted 8 lines
Quoted 5 lines
As a result, the LAYOUTGET reply sent to the NFS client may contain overlapping extents. This creates ambiguity in extent selection for a given file range, which can lead to incorrect device selection, inconsistent handling of datastate, and ultimately data corruption or protocol violations on the client side.
Please also add a check to the client that catches this and doesn't use the layout that has extents outside the requested range. And maybe warn about it as well.
The returned extents cover exactly the range requested in the LAYOUTGET op. However these extents are overlapping. For example, here is the on-the-wire capture of the LAYOUTGET operation and reply showing the overlapping extents:
Network File System, Ops(3): SEQUENCE, PUTFH, LAYOUTGET [Program Version: 4]
[V4 Procedure: COMPOUND (1)]
Tag: <EMPTY>
minorversion: 2
Operations (count: 3): SEQUENCE, PUTFH, LAYOUTGET
Opcode: SEQUENCE (53)
Opcode: PUTFH (22)
Opcode: LAYOUTGET (50)
layout available?: No
layout type:LAYOUT4_SCSI(5)
IO mode:IOMODE_RW(2)
offset: 122880
length: 65536
min length: 4096
StateID
maxcount: 4096
[Main Opcode: LAYOUTGET (50)]
Network File System, Ops(3): SEQUENCE PUTFH LAYOUTGET [Program Version: 4]
[V4 Procedure: COMPOUND (1)]
Status:NFS4_OK(0)
Tag: <EMPTY>
Operations (count: 3)
Opcode: SEQUENCE (53)
Opcode: PUTFH (22)
Opcode: LAYOUTGET (50)
Status:NFS4_OK(0)
return on close?: Yes
StateID
Layout Segment (count: 1)
offset: 122880
length: 77824
IO mode:IOMODE_RW(2)
layout type:LAYOUT4_SCSI(5)
SCSI Extents (count: 2)
extent 0
device ID: 01000000000000000000000000000000 file offset: 122880
length: 53248
volume offset: 339460096
Quoted 8 lines
extent state:
INVALID_DATA(2) extent 1
device ID: 01000000000000000000000000000000 file offset: 122880
length: 77824
volume offset: 339460096
extent state:INVALID_DATA(2) [Main Opcode: LAYOUTGET (50)]
After reviewing ext_tree_insert(), with assist from Codex, I think this function handles overlapping extents properly. The only issue I see in ext_tree_insert() is the accuracy of the return error code, EINVAL instead of ENOMEM, when kmemdup() fails.
Since ext_tree_insert seems to handle overlapping extents fine, do you think it's worth it to fix xfs_fs_map_blocks() to avoid returning overlap extents?
IMHO, I think we still should fix xfs_fs_map_blocks() to avoid any overhead and complication in ext_tree_insert having to handle overlapping extents.
-Dai
Quoted 26 lines
-Dai
Quoted 22 lines
Quoted 4 lines
Also drop the check for (!error) since it was checked after call to
xfs_bmapi_read().Quoted 8 lines
Fixes cc6c40e09d7b1 ("NFSD/blocklayout: Support multiple extents per LAYOUTGET").Signed-off-by Dai Ngo <[email protected]>---
fs/xfs/xfs_pnfs.c| 6 +++---1 file changed, 3 insertions(+), 3 deletions(-)- This patch is based on top of the patch:
xfs: fix use of uninitialized imap inxfs_fs_map_blockserror pathThe error changes should go into that patch, so please resend it with that fixes. Maybe as a series together with this patch to keep them together.
fs/xfs/xfs_pnfs.c xfs_fs_map_blocks( · line 174
Quoted 4 lines
fs/xfs/xfs_pnfs.c +1
-0@@ -172,6 +172,7 @@ xfs_fs_map_blocks( offset_fsb = XFS_B_TO_FSBT(mp, offset); lock_flags = xfs_ilock_data_map_shared(ip); + bmapi_flags = 0; /* return map for requested range only */Just remove the variable and hard code the 0 in the
xfs_bmapi_readcall.
Quoted 80 lines · Dai Ngo
Hi Christoph,
Quoted 63 lines · Dai Ngo
Quoted 6 lines · Christoph Hellwig
Quoted 5 lines · Dai Ngo
A single LAYOUTGET request from the client can cause the server to issue multiple calls to
xfs_fs_map_blocks()for different offsets within the same extent. Because the use ofXFS_BMAPI_ENTIREflag, these calls can produce overlapping mappings.Quoted 8 lines
Quoted 5 lines
As a result, the LAYOUTGET reply sent to the NFS client may contain overlapping extents. This creates ambiguity in extent selection for a given file range, which can lead to incorrect device selection, inconsistent handling of datastate, and ultimately data corruption or protocol violations on the client side.
Please also add a check to the client that catches this and doesn't use the layout that has extents outside the requested range. And maybe warn about it as well.
The returned extents cover exactly the range requested in the LAYOUTGET op. However these extents are overlapping. For example, here is the on-the-wire capture of the LAYOUTGET operation and reply showing the overlapping extents:
Network File System, Ops(3): SEQUENCE, PUTFH, LAYOUTGET [Program Version: 4]
[V4 Procedure: COMPOUND (1)]
Tag: <EMPTY>
minorversion: 2
Operations (count: 3): SEQUENCE, PUTFH, LAYOUTGET
Opcode: SEQUENCE (53)
Opcode: PUTFH (22)
Opcode: LAYOUTGET (50)
layout available?: No
layout type:LAYOUT4_SCSI(5)
IO mode:IOMODE_RW(2)
offset: 122880
length: 65536
min length: 4096
StateID
maxcount: 4096
[Main Opcode: LAYOUTGET (50)]
Network File System, Ops(3): SEQUENCE PUTFH LAYOUTGET [Program Version: 4]
[V4 Procedure: COMPOUND (1)]
Status:NFS4_OK(0)
Tag: <EMPTY>
Operations (count: 3)
Opcode: SEQUENCE (53)
Opcode: PUTFH (22)
Opcode: LAYOUTGET (50)
Status:NFS4_OK(0)
return on close?: Yes
StateID
Layout Segment (count: 1)
offset: 122880
length: 77824
IO mode:IOMODE_RW(2)
layout type:LAYOUT4_SCSI(5)
SCSI Extents (count: 2)
extent 0
device ID: 01000000000000000000000000000000 file offset: 122880
length: 53248
volume offset: 339460096Quoted 8 lines
extent state:
INVALID_DATA(2) extent 1
device ID: 01000000000000000000000000000000 file offset: 122880
length: 77824
volume offset: 339460096
extent state:INVALID_DATA(2) [Main Opcode: LAYOUTGET (50)]After reviewing
ext_tree_insert(), with assist from Codex, I think this function handles overlapping extents properly. The only issue I see inext_tree_insert()is the accuracy of the return error code, EINVAL instead of ENOMEM, whenkmemdup()fails.
Quoted 6 lines
Since
ext_tree_insertseems to handle overlapping extents fine, do you think it's worth it to fixxfs_fs_map_blocks()to avoid returning overlap extents?IMHO, I think we still should fix
xfs_fs_map_blocks()to avoid any overhead and complication inext_tree_inserthaving to handle overlapping extents.
I don't know enough about the nfs blocklayout code to say for sure, but it seems like you want to upsert the mapping returned by xfs_fs_map_blocks into the "ext_tree" right?
And by "upsert" I mean "clear out any mappings for the (offset, length) range, then insert the new mapping", sort of like what the fuse iomap cache does:
https://git.kernel.org/pub/scm/linux/kernel/git/djwong/xfs-linux.git/tree/fs/fuse/fuse_iomap_cache.c?h=fuse-iomap-cache_2026-05-07#n1682
or I guess the xfs scrub bitmap support code does when you set a range:
https://git.kernel.org/pub/scm/linux/kernel/git/djwong/xfs-linux.git/tree/fs/xfs/scrub/bitmap.c?h=fuse-iomap-cache_2026-05-07#n395
But as I said before, I don't know if "two mappings retrieved in rapid succession that overlap" is actually an NFS error.
--D
Quoted 30 lines
-Dai
Quoted 27 lines
-Dai
Quoted 23 lines
Quoted 5 lines
Also drop the check for (!error) since it was checked after call to
xfs_bmapi_read().Fixes cc6c40e09d7b1 ("NFSD/blocklayout: Support multipleQuoted 8 lines
extents per LAYOUTGET").
Signed-off-by Dai Ngo <[email protected]>---
fs/xfs/xfs_pnfs.c| 6 +++---1 file changed, 3 insertions(+), 3 deletions(-)- This patch is based on top of the patch:
xfs: fix use of uninitialized imap inxfs_fs_map_blockserror pathThe error changes should go into that patch, so please resend it with that fixes. Maybe as a series together with this patch to keep them together.
fs/xfs/xfs_pnfs.c xfs_fs_map_blocks( · line 174
Quoted 4 lines
fs/xfs/xfs_pnfs.c +1
-0@@ -172,6 +172,7 @@ xfs_fs_map_blocks( offset_fsb = XFS_B_TO_FSBT(mp, offset); lock_flags = xfs_ilock_data_map_shared(ip); + bmapi_flags = 0; /* return map for requested range only */Just remove the variable and hard code the 0 in the
xfs_bmapi_readcall.
Dai Ngo
reviewerQuoted 82 lines · Darrick J. Wong
Quoted 81 lines · Dai Ngo
Hi Christoph,
Quoted 61 lines · Dai Ngo
Quoted 6 lines · Christoph Hellwig
Quoted 5 lines · Dai Ngo
A single LAYOUTGET request from the client can cause the server to issue multiple calls to
xfs_fs_map_blocks()for different offsets within the same extent. Because the use ofXFS_BMAPI_ENTIREflag, these calls can produce overlapping mappings.Quoted 8 lines
Quoted 5 lines
As a result, the LAYOUTGET reply sent to the NFS client may contain overlapping extents. This creates ambiguity in extent selection for a given file range, which can lead to incorrect device selection, inconsistent handling of datastate, and ultimately data corruption or protocol violations on the client side.
Please also add a check to the client that catches this and doesn't use the layout that has extents outside the requested range. And maybe warn about it as well.
The returned extents cover exactly the range requested in the LAYOUTGET op. However these extents are overlapping. For example, here is the on-the-wire capture of the LAYOUTGET operation and reply showing the overlapping extents:
Network File System, Ops(3): SEQUENCE, PUTFH, LAYOUTGET [Program Version: 4]
[V4 Procedure: COMPOUND (1)]
Tag: <EMPTY>
minorversion: 2
Operations (count: 3): SEQUENCE, PUTFH, LAYOUTGET
Opcode: SEQUENCE (53)
Opcode: PUTFH (22)
Opcode: LAYOUTGET (50)
layout available?: No
layout type:LAYOUT4_SCSI(5)
IO mode:IOMODE_RW(2)
offset: 122880
length: 65536
min length: 4096
StateID
maxcount: 4096
[Main Opcode: LAYOUTGET (50)]
Network File System, Ops(3): SEQUENCE PUTFH LAYOUTGET [Program Version: 4]
[V4 Procedure: COMPOUND (1)]
Status:NFS4_OK(0)
Tag: <EMPTY>
Operations (count: 3)
Opcode: SEQUENCE (53)
Opcode: PUTFH (22)
Opcode: LAYOUTGET (50)
Status:NFS4_OK(0)
return on close?: Yes
StateID
Layout Segment (count: 1)
offset: 122880
length: 77824
IO mode:IOMODE_RW(2)
layout type:LAYOUT4_SCSI(5)
SCSI Extents (count: 2)
extent 0
device ID: 01000000000000000000000000000000 file offset: 122880
length: 53248
volume offset: 339460096Quoted 8 lines
extent state:
INVALID_DATA(2) extent 1
device ID: 01000000000000000000000000000000 file offset: 122880
length: 77824
volume offset: 339460096
extent state:INVALID_DATA(2) [Main Opcode: LAYOUTGET (50)]After reviewing
ext_tree_insert(), with assist from Codex, I think this function handles overlapping extents properly. The only issue I see inext_tree_insert()is the accuracy of the return error code, EINVAL instead of ENOMEM, whenkmemdup()fails.Since
ext_tree_insertseems to handle overlapping extents fine, do you think it's worth it to fixxfs_fs_map_blocks()to avoid returning overlap extents?
Quoted 5 lines
Quoted 2 lines
IMHO, I think we still should fix
xfs_fs_map_blocks()to avoid any overhead and complication inext_tree_inserthaving to handle overlapping extents.I don't know enough about the nfs blocklayout code to say for sure, but it seems like you want to upsert the mapping returned by
xfs_fs_map_blocksinto the "ext_tree" right?
This is currently done on the NFS client side by ext_tree_insert(). The question I have is should we enhance the server side by passing '0' to xfs_fs_map_blocks() so the client does not have to do the work of handling the overlap extents.
Quoted 11 lines
And by "upsert" I mean "clear out any mappings for the (offset, length) range, then insert the new mapping", sort of like what the fuse iomap cache does:
https://urldefense.com/v3/__https://git.kernel.org/pub/scm/linux/kernel/git/djwong/xfs-linux.git/tree/fs/fuse/fuse_iomap_cache.c?h=fuse-iomap-cache_2026-05-07*n1682__;Iw!!ACWV5N9M2RV99hQ!LP7Lgbj10myml6rtWPCyBcfoKlvvpS39fLQ4Dy8puJ9c8ZQbxV6ToyYupyVa8TrICy--mS-sUwGxrA$or I guess the xfs scrub bitmap support code does when you set a range:
https://urldefense.com/v3/__https://git.kernel.org/pub/scm/linux/kernel/git/djwong/xfs-linux.git/tree/fs/xfs/scrub/bitmap.c?h=fuse-iomap-cache_2026-05-07*n395__;Iw!!ACWV5N9M2RV99hQ!LP7Lgbj10myml6rtWPCyBcfoKlvvpS39fLQ4Dy8puJ9c8ZQbxV6ToyYupyVa8TrICy--mS9n7W3nLw$But as I said before, I don't know if "two mappings retrieved in rapid succession that overlap" is actually an NFS error.
As far as I can tell,|nfsd4_block_proc_layoutget()| correctly advances the offset and decrements the length after each call to|nfsd4_block_map_extent()|, which passes the arguments through verbatim to|xfs_fs_map_blocks()|.
-Dai
Quoted 29 lines
--D
Quoted 26 lines
-Dai
Quoted 24 lines
-Dai
Quoted 22 lines
Quoted 4 lines
Also drop the check for (!error) since it was checked after call to
xfs_bmapi_read().Fixes cc6c40e09d7b1 ("NFSD/blocklayout: Support multipleQuoted 8 lines
extents per LAYOUTGET").
Signed-off-by Dai Ngo <[email protected]>--- fs/xfs/xfs_pnfs.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)- This patch is based on top of the patch:
xfs: fix use of uninitialized imap inxfs_fs_map_blockserror pathThe error changes should go into that patch, so please resend it with that fixes. Maybe as a series together with this patch to keep them together.
fs/xfs/xfs_pnfs.c xfs_fs_map_blocks( · line 174
Quoted 4 lines
fs/xfs/xfs_pnfs.c +1
-0@@ -172,6 +172,7 @@ xfs_fs_map_blocks( offset_fsb = XFS_B_TO_FSBT(mp, offset); lock_flags = xfs_ilock_data_map_shared(ip); + bmapi_flags = 0; /* return map for requested range only */Just remove the variable and hard code the 0 in the
xfs_bmapi_readcall.
Quoted 86 lines · Dai Ngo
Quoted 84 lines · Darrick J. Wong
Quoted 83 lines · Dai Ngo
Hi Christoph,
Quoted 61 lines · Dai Ngo
Quoted 6 lines · Christoph Hellwig
Quoted 5 lines · Dai Ngo
A single LAYOUTGET request from the client can cause the server to issue multiple calls to
xfs_fs_map_blocks()for different offsets within the same extent. Because the use ofXFS_BMAPI_ENTIREflag, these calls can produce overlapping mappings.Quoted 8 lines
Quoted 5 lines
As a result, the LAYOUTGET reply sent to the NFS client may contain overlapping extents. This creates ambiguity in extent selection for a given file range, which can lead to incorrect device selection, inconsistent handling of datastate, and ultimately data corruption or protocol violations on the client side.
Please also add a check to the client that catches this and doesn't use the layout that has extents outside the requested range. And maybe warn about it as well.
The returned extents cover exactly the range requested in the LAYOUTGET op. However these extents are overlapping. For example, here is the on-the-wire capture of the LAYOUTGET operation and reply showing the overlapping extents:
Network File System, Ops(3): SEQUENCE, PUTFH, LAYOUTGET [Program Version: 4]
[V4 Procedure: COMPOUND (1)]
Tag: <EMPTY>
minorversion: 2
Operations (count: 3): SEQUENCE, PUTFH, LAYOUTGET
Opcode: SEQUENCE (53)
Opcode: PUTFH (22)
Opcode: LAYOUTGET (50)
layout available?: No
layout type:LAYOUT4_SCSI(5)
IO mode:IOMODE_RW(2)
offset: 122880
length: 65536
min length: 4096
StateID
maxcount: 4096
[Main Opcode: LAYOUTGET (50)]
Network File System, Ops(3): SEQUENCE PUTFH LAYOUTGET [Program Version: 4]
[V4 Procedure: COMPOUND (1)]
Status:NFS4_OK(0)
Tag: <EMPTY>
Operations (count: 3)
Opcode: SEQUENCE (53)
Opcode: PUTFH (22)
Opcode: LAYOUTGET (50)
Status:NFS4_OK(0)
return on close?: Yes
StateID
Layout Segment (count: 1)
offset: 122880
length: 77824
IO mode:IOMODE_RW(2)
layout type:LAYOUT4_SCSI(5)
SCSI Extents (count: 2)
extent 0
device ID: 01000000000000000000000000000000 file offset: 122880
length: 53248
volume offset: 339460096Quoted 8 lines
extent state:
INVALID_DATA(2) extent 1
device ID: 01000000000000000000000000000000 file offset: 122880
length: 77824
volume offset: 339460096
extent state:INVALID_DATA(2) [Main Opcode: LAYOUTGET (50)]After reviewing
ext_tree_insert(), with assist from Codex, I think this function handles overlapping extents properly. The only issue I see inext_tree_insert()is the accuracy of the return error code, EINVAL instead of ENOMEM, whenkmemdup()fails.Since
ext_tree_insertseems to handle overlapping extents fine, do you think it's worth it to fixxfs_fs_map_blocks()to avoid returning overlap extents?IMHO, I think we still should fix
xfs_fs_map_blocks()to avoid any overhead and complication inext_tree_inserthaving to handle overlapping extents.
Quoted 8 lines
Quoted 3 lines
I don't know enough about the nfs blocklayout code to say for sure, but it seems like you want to upsert the mapping returned by
xfs_fs_map_blocksinto the "ext_tree" right?This is currently done on the NFS client side by
ext_tree_insert(). The question I have is should we enhance the server side by passing '0' toxfs_fs_map_blocks()so the client does not have to do the work of handling the overlap extents.
Oh, ok.
Not passing XFS_BMAPI_ENTIRE into bmapi_read sounds fine to me.
Quoted 9 lines
Quoted 9 lines
And by "upsert" I mean "clear out any mappings for the (offset, length) range, then insert the new mapping", sort of like what the fuse iomap cache does:
https://urldefense.com/v3/__https://git.kernel.org/pub/scm/linux/kernel/git/djwong/xfs-linux.git/tree/fs/fuse/fuse_iomap_cache.c?h=fuse-iomap-cache_2026-05-07*n1682__;Iw!!ACWV5N9M2RV99hQ!LP7Lgbj10myml6rtWPCyBcfoKlvvpS39fLQ4Dy8puJ9c8ZQbxV6ToyYupyVa8TrICy--mS-sUwGxrA$or I guess the xfs scrub bitmap support code does when you set a range:
https://urldefense.com/v3/__https://git.kernel.org/pub/scm/linux/kernel/git/djwong/xfs-linux.git/tree/fs/xfs/scrub/bitmap.c?h=fuse-iomap-cache_2026-05-07*n395__;Iw!!ACWV5N9M2RV99hQ!LP7Lgbj10myml6rtWPCyBcfoKlvvpS39fLQ4Dy8puJ9c8ZQbxV6ToyYupyVa8TrICy--mS9n7W3nLw$
Quoted 6 lines
Quoted 2 lines
But as I said before, I don't know if "two mappings retrieved in rapid succession that overlap" is actually an NFS error.
As far as I can tell,|nfsd4_block_proc_layoutget()| correctly advances the offset and decrements the length after each call to|nfsd4_block_map_extent()|, which passes the arguments through verbatim to|xfs_fs_map_blocks()|.
<nod>
--D
Quoted 32 lines
-Dai
Quoted 29 lines
--D
Quoted 26 lines
-Dai
Quoted 24 lines
-Dai
Quoted 22 lines
Quoted 4 lines
Also drop the check for (!error) since it was checked after call to
xfs_bmapi_read().Fixes cc6c40e09d7b1 ("NFSD/blocklayout: Support multipleQuoted 8 lines
extents per LAYOUTGET").
Signed-off-by Dai Ngo <[email protected]>--- fs/xfs/xfs_pnfs.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)- This patch is based on top of the patch:
xfs: fix use of uninitialized imap inxfs_fs_map_blockserror pathThe error changes should go into that patch, so please resend it with that fixes. Maybe as a series together with this patch to keep them together.
fs/xfs/xfs_pnfs.c xfs_fs_map_blocks( · line 174
Quoted 4 lines
fs/xfs/xfs_pnfs.c +1
-0@@ -172,6 +172,7 @@ xfs_fs_map_blocks( offset_fsb = XFS_B_TO_FSBT(mp, offset); lock_flags = xfs_ilock_data_map_shared(ip); + bmapi_flags = 0; /* return map for requested range only */Just remove the variable and hard code the 0 in the
xfs_bmapi_readcall.
Quoted 4 lines · Dai Ngo
Quoted 3 lines · Darrick J. Wong
Quoted 2 lines · Dai Ngo
IMHO, I think we still should fix
xfs_fs_map_blocks()to avoid any overhead and complication inext_tree_inserthaving to handle overlapping extents.
Quoted 8 lines
Quoted 3 lines
I don't know enough about the nfs blocklayout code to say for sure, but it seems like you want to upsert the mapping returned by
xfs_fs_map_blocksinto the "ext_tree" right?This is currently done on the NFS client side by
ext_tree_insert(). The question I have is should we enhance the server side by passing '0' toxfs_fs_map_blocks()so the client does not have to do the work of handling the overlap extents.
I think you've all missed the optimal solution to the problem.
The problem is not the use of XFS_BMAPI_ENTIRE on the first mapping call, it's the use of it on the -second- after the first call didn't return a range that mapped the -entire- request range.
Hence the second and subsequent calls need range trimming so that they don't overlap with the first range that was returned.
IOWs, we keep the use XFS_BMAPI_ENTIRE for the first mapping call so we retain the optimisation that minimises the number of pNFS client mapping calls needed, but if it needs to make a second call we drop the ENTIRE flag and append extents trimmed to the range being asked for (which doesn't include the first extent already returned).
That was we get large extents reported most of the time, and in the corner cases where we have a race like this one or an extent boundary lies in the middle of the requested range, we will get correct, non-overlapping behaviour.
Best of both worlds, yes?
-Dave.
Signature · 2 lines
Dave Chinner [email protected]
Dai Ngo
reviewerHi Dave,
Thank you for your feedback.
Quoted 12 lines · Dave Chinner
Quoted 11 lines · Dai Ngo
Quoted 6 lines · Darrick J. Wong
Quoted 2 lines · Dai Ngo
IMHO, I think we still should fix
xfs_fs_map_blocks()to avoid any overhead and complication inext_tree_inserthaving to handle overlapping extents.I don't know enough about the nfs blocklayout code to say for sure, but it seems like you want to upsert the mapping returned by
xfs_fs_map_blocksinto the "ext_tree" right?This is currently done on the NFS client side by
ext_tree_insert(). The question I have is should we enhance the server side by passing '0' toxfs_fs_map_blocks()so the client does not have to do the work of handling the overlap extents.
Quoted 5 lines
I think you've all missed the optimal solution to the problem.
The problem is not the use of
XFS_BMAPI_ENTIREon the first mapping call, it's the use of it on the -second- after the first call didn't return a range that mapped the -entire- request range.
Currently the map_blocks() API between the NFS server and XFS does not provide a way to specify whether XFS should use XFS_BMAPI_ENTIRE or '0'. xfs_fs_map_blocks() just uses XFS_BMAPI_ENTIRE.
On the first mapping call, NFS server always specify the whole file range that requested by the client in the LAYOUTGET.
So if xfs_fs_map_blocks() can not return the requested mapping range with '0' on the first mapping call then I think using XFS_BMAPI_ENTIRE in the first mapping call makes any different.
-Dai
Quoted 19 lines
Hence the second and subsequent calls need range trimming so that they don't overlap with the first range that was returned.
IOWs, we keep the use
XFS_BMAPI_ENTIREfor the first mapping call so we retain the optimisation that minimises the number of pNFS client mapping calls needed, but if it needs to make a second call we drop the ENTIRE flag and append extents trimmed to the range being asked for (which doesn't include the first extent already returned).That was we get large extents reported most of the time, and in the corner cases where we have a race like this one or an extent boundary lies in the middle of the requested range, we will get correct, non-overlapping behaviour.
Best of both worlds, yes?
-Dave.
Quoted 3 lines · Dai Ngo
Currently the
map_blocks()API between the NFS server and XFS does not provide a way to specify whether XFS should useXFS_BMAPI_ENTIREor '0'.xfs_fs_map_blocks()just usesXFS_BMAPI_ENTIRE.
And that is a good thing.
Quoted 6 lines
On the first mapping call, NFS server always specify the whole file range that requested by the client in the LAYOUTGET.
So if
xfs_fs_map_blocks()can not return the requested mapping range with '0' on the first mapping call then I think usingXFS_BMAPI_ENTIREin the first mapping call makes any different.
Yes. Still not sure why we get a second call that overlaps with the first one in a single layoutget operation, though.
Dai Ngo
reviewerQuoted 6 lines · Christoph Hellwig
Quoted 3 lines · Dai Ngo
Currently the
map_blocks()API between the NFS server and XFS does not provide a way to specify whether XFS should useXFS_BMAPI_ENTIREor '0'.xfs_fs_map_blocks()just usesXFS_BMAPI_ENTIRE.And that is a good thing.
Quoted 8 lines
Quoted 6 lines
On the first mapping call, NFS server always specify the whole file range that requested by the client in the LAYOUTGET.
So if
xfs_fs_map_blocks()can not return the requested mapping range with '0' on the first mapping call then I think usingXFS_BMAPI_ENTIREin the first mapping call makes any different.Yes. Still not sure why we get a second call that overlaps with the first one in a single layoutget operation, though.
I added debug code to dump the mapping of the file before and after NFSD calls to xfs_fs_map_blocks() completed:
May 18 18:03:59 dngo kernel: XFS (sdb): pNFS inode 3846447 iomap dump May 18 18:03:59 dngo kernel: xfs_pnfs_dump_inode_iomaps: off[28672] len[12288] addr[1957031936] type[3] flags[0x0] state[1] May 18 18:03:59 dngo kernel: xfs_pnfs_dump_inode_iomaps: off[73728] len[65536] addr[1957076992] type[2] flags[0x0] state[0] May 18 18:03:59 dngo kernel: xfs_pnfs_dump_inode_iomaps: off[139264] len[4096] addr[1957142528] type[3] flags[0x0] state[1] May 18 18:03:59 dngo kernel: xfs_pnfs_dump_inode_iomaps: off[143360] len[16384] addr[1957146624] type[2] flags[0x0] state[0] May 18 18:03:59 dngo kernel: xfs_pnfs_dump_inode_iomaps: off[159744] len[4096] addr[1957163008] type[3] flags[0x0] state[1] May 18 18:03:59 dngo kernel: xfs_pnfs_dump_inode_iomaps: off[163840] len[8192] addr[1957167104] type[2] flags[0x0] state[0] /* type_2: IOMAP_MAPPED */ May 18 18:03:59 dngo kernel: xfs_pnfs_dump_inode_iomaps: off[172032] len[12288] addr[1957175296] type[3] flags[0x0] state[1] <<= entry# 7 & 8 will merge May 18 18:03:59 dngo kernel: xfs_pnfs_dump_inode_iomaps: off[188416] len[32768] addr[1957191680] type[3] flags[0x0] state[1] <<= into entry# 7 below May 18 18:03:59 dngo kernel: xfs_pnfs_dump_inode_iomaps: off[258048] len[4096] addr[1957261312] type[3] flags[0x0] state[1]
May 18 18:03:59 dngo kernel: nfsd4_block_proc_layoutget: i[1] seg->offset[184320] seg->length[65536] seg->iomode[2] bex->foff[172032] offset[188416] May 18 18:03:59 dngo kernel: Ext[0] foff[184320] len[4096] es[2] soff[1957187584] /* es_2: PNFS_BLOCK_INVALID_DATA */ May 18 18:03:59 dngo kernel: Ext[1] foff[172032] len[49152] es[2] soff[1957175296] May 18 18:03:59 dngo kernel: Ext[2] foff[221184] len[28672] es[2] soff[1957224448]
May 18 18:03:59 dngo kernel: XFS (sdb): pNFS inode 3846447 iomap dump May 18 18:03:59 dngo kernel: xfs_pnfs_dump_inode_iomaps: off[28672] len[12288] addr[1957031936] type[3] flags[0x0] state[1] May 18 18:03:59 dngo kernel: xfs_pnfs_dump_inode_iomaps: off[73728] len[65536] addr[1957076992] type[2] flags[0x0] state[0] May 18 18:03:59 dngo kernel: xfs_pnfs_dump_inode_iomaps: off[139264] len[4096] addr[1957142528] type[3] flags[0x0] state[1] May 18 18:03:59 dngo kernel: xfs_pnfs_dump_inode_iomaps: off[143360] len[16384] addr[1957146624] type[2] flags[0x0] state[0] May 18 18:03:59 dngo kernel: xfs_pnfs_dump_inode_iomaps: off[159744] len[4096] addr[1957163008] type[3] flags[0x0] state[1] May 18 18:03:59 dngo kernel: xfs_pnfs_dump_inode_iomaps: off[163840] len[8192] addr[1957167104] type[2] flags[0x0] state[0] /* type_2: IOMAP_MAPPED */ May 18 18:03:59 dngo kernel: xfs_pnfs_dump_inode_iomaps: off[172032] len[77824] addr[1957175296] type[3] flags[0x0] state[1] /* type_3: IOMAP_UNWRITTEN */ May 18 18:03:59 dngo kernel: xfs_pnfs_dump_inode_iomaps: off[258048] len[4096] addr[1957261312] type[3] flags[0x0] state[1]
As shown, the file map changes. Entry# 7 and 8 before the NFSD calls merged into entry#7 after the calls. So there must be some activities that cause the map to change. I don't know whether the activities were triggered by NFS or something in XFS or the block device layer.
However, based on this data I think it's better to change the bmapi_flags from XFS_BMAPI_ENTIRE to '0' to address the overlap issue.
-Dai
Quoted 4 lines · Dai Ngo
As shown, the file map changes. Entry# 7 and 8 before the NFSD calls merged into entry#7 after the calls. So there must be some activities that cause the map to change. I don't know whether the activities were triggered by NFS or something in XFS or the block device layer.
Hmm. We only insert layouts (and search for conflicts) after calling ->proc_layoutget. So this might be racy against unwritten extent conversion, or other writers, which is a bit of an issue. I guess we need to fix nfsd4_layoutget to insert an in-progress layout before calling into ->layouget.
Quoted 2 lines
However, based on this data I think it's better to change the
bmapi_flagsfromXFS_BMAPI_ENTIREto '0' to address the overlap issue.
We absolutely should be doing that as I said from my first reply. Still trying to understand what is going on here at a higher level, though.
Dai Ngo
reviewerQuoted 10 lines · Christoph Hellwig
Quoted 4 lines · Dai Ngo
As shown, the file map changes. Entry# 7 and 8 before the NFSD calls merged into entry#7 after the calls. So there must be some activities that cause the map to change. I don't know whether the activities were triggered by NFS or something in XFS or the block device layer.
Hmm. We only insert layouts (and search for conflicts) after calling ->proc_layoutget. So this might be racy against unwritten extent conversion, or other writers, which is a bit of an issue. I guess we need to fix
nfsd4_layoutgetto insert an in-progress layout before calling into ->layouget.
I can't quite see the race condition regarding layoutget here. Could you please explain?
Quoted 6 lines
Quoted 3 lines
However, based on this data I think it's better to change the
bmapi_flagsfromXFS_BMAPI_ENTIREto '0' to address the overlap issue.We absolutely should be doing that as I said from my first reply. Still trying to understand what is going on here at a higher level, though.
I'll resubmit the patch series with both fixes combined: the uninitialized imap handling in the error path and the replacement of XFS_BMAPI_ENTIRE with 0.
Thanks,
-Dai
Quoted 7 lines · Dai Ngo
Quoted 5 lines · Christoph Hellwig
Quoted 4 lines · Dai Ngo
As shown, the file map changes. Entry# 7 and 8 before the NFSD calls merged into entry#7 after the calls. So there must be some activities that cause the map to change. I don't know whether the activities were triggered by NFS or something in XFS or the block device layer.
Quoted 8 lines
Quoted 5 lines
Hmm. We only insert layouts (and search for conflicts) after calling ->proc_layoutget. So this might be racy against unwritten extent conversion, or other writers, which is a bit of an issue. I guess we need to fix
nfsd4_layoutgetto insert an in-progress layout before calling into ->layouget.I can't quite see the race condition regarding layoutget here. Could you please explain?
/me has no idea about nfs layouts but has thoughts anyway :)
1) xfs_fs_map_blocks only takes i_rwsem and XFS_ILOCK; it doesn't take the mmap invalidation lock. Does that mean that pagefaults could wander in and mess with the layout?
(I think the race that Christoph is referring to here is that any other thread can wander in and change the mapping after a ->map_blocks call returns, because nfs isn't holding any kind of lock on the inode)
2) Now that NFS apparently can serve up multiple mappings at once, should ->map_blocks pass in an array element count so that we can do multiple iomaps in a single lock cycle?
3) Do the reflink and realtime inode checks need to be re-assessed after grabbing the ilock since they can change?
--D
Quoted 14 lines
Quoted 5 lines
Quoted 2 lines
However, based on this data I think it's better to change the
bmapi_flagsfromXFS_BMAPI_ENTIREto '0' to address the overlap issue.We absolutely should be doing that as I said from my first reply. Still trying to understand what is going on here at a higher level, though.
I'll resubmit the patch series with both fixes combined: the uninitialized imap handling in the error path and the replacement of
XFS_BMAPI_ENTIREwith 0.Thanks,
-Dai
Dai Ngo
reviewerQuoted 16 lines · Darrick J. Wong
Quoted 6 lines · Dai Ngo
Quoted 5 lines · Christoph Hellwig
Quoted 4 lines · Dai Ngo
As shown, the file map changes. Entry# 7 and 8 before the NFSD calls merged into entry#7 after the calls. So there must be some activities that cause the map to change. I don't know whether the activities were triggered by NFS or something in XFS or the block device layer.
Quoted 7 lines
Quoted 5 lines
Hmm. We only insert layouts (and search for conflicts) after calling ->proc_layoutget. So this might be racy against unwritten extent conversion, or other writers, which is a bit of an issue. I guess we need to fix
nfsd4_layoutgetto insert an in-progress layout before calling into ->layouget.I can't quite see the race condition regarding layoutget here. Could you please explain?
/me has no idea about nfs layouts but has thoughts anyway :)
Quoted 7 lines
1)
xfs_fs_map_blocksonly takesi_rwsemandXFS_ILOCK; it doesn't take the mmap invalidation lock. Does that mean that pagefaults could wander in and mess with the layout?(I think the race that Christoph is referring to here is that any other thread can wander in and change the mapping after a ->map_blocks call returns, because nfs isn't holding any kind of lock on the inode)
I think this is what happened.
Assuming the current map of the file as follow:
0000-4095: IOMAP_UNWRITTEN - block allocated
4096-8191: IOMAP_HOLE - no block allocated
8192-12287: IOMAP_UNWRITTEN - block allocated
NOTE:
. the bmapi_flags used by xfs_bmapi_read is set to XFS_BMAPI_ENTIRE. . the nimaps count is set to 1. xfs_bmapi_read can return only 1 extent.
1st call from nfsd to xfs_fs_map_blocks(), specifying offset 0, length 12288 and write is true.
Extent Returned:
- offset: 0
- length: 4096 bytes (one filesystem block)
- type: IOMAP_UNWRITTEN (existing preallocated extent)
- addr: physical block number of the first unwritten extent.
2nd call from nfsd to xfs_fs_map_blocks(), specifying offset 4096, length 8192 and write is true.
Returned Extent:
- offset: 4096
- length: 4096 bytes (one filesystem block)
- type: IOMAP_UNWRITTEN (freshly allocated unwritten extent)
- addr: physical block number assigned when filling the hole at 4096
3rd call from nfsd to xfs_fs_map_blocks(), specifying offset 8192, length 4096 and write is true.
Returned Extent
- offset: 0
- length: 12288 bytes (three contiguous blocks)
- type: IOMAP_UNWRITTEN
- addr: physical address of the merged unwritten allocation
After the second call, filling the hole merged the three unwritten segments into a single extent (br_startoff = 0, br_blockcount = 3). When the third call looks up the range starting at 8192, xfs_bmapi_read() finds that single extent. Because XFS_BMAPI_ENTIRE is set, xfs_bmapi_trim_map() bypasses trimming and returns the full extent to xfs_bmbt_to_iomap().
-Dai
Quoted 23 lines
2) Now that NFS apparently can serve up multiple mappings at once, should ->map_blocks pass in an array element count so that we can do multiple iomaps in a single lock cycle?
3) Do the reflink and realtime inode checks need to be re-assessed after grabbing the ilock since they can change?
--D
Quoted 13 lines
Quoted 5 lines
Quoted 2 lines
However, based on this data I think it's better to change the
bmapi_flagsfromXFS_BMAPI_ENTIREto '0' to address the overlap issue.We absolutely should be doing that as I said from my first reply. Still trying to understand what is going on here at a higher level, though.
I'll resubmit the patch series with both fixes combined: the uninitialized imap handling in the error path and the replacement of
XFS_BMAPI_ENTIREwith 0.Thanks,
-Dai
[adding Sergey, who wrote the code to allow multiple mappings]
Quoted 3 lines · Darrick J. Wong
1)
xfs_fs_map_blocksonly takesi_rwsemandXFS_ILOCK; it doesn't take the mmap invalidation lock. Does that mean that pagefaults could wander in and mess with the layout?
Looks like it.
Quoted 3 lines
2) Now that NFS apparently can serve up multiple mappings at once, should ->map_blocks pass in an array element count so that we can do multiple iomaps in a single lock cycle?
I guess we need to do that, or revert the code to provide multiple maps for now.
Quoted 2 lines
3) Do the reflink and realtime inode checks need to be re-assessed after grabbing the ilock since they can change?
Yes.
Dai Ngo
reviewerQuoted 7 lines · Christoph Hellwig
[adding Sergey, who wrote the code to allow multiple mappings]
Quoted 3 lines · Darrick J. Wong
1)
xfs_fs_map_blocksonly takesi_rwsemandXFS_ILOCK; it doesn't take the mmap invalidation lock. Does that mean that pagefaults could wander in and mess with the layout?Looks like it.
I already described the scenario in which the file mapping can change between successive calls from nfsd—triggered by a single LAYOUTGET request—to xfs_fs_map_blocks().
I also don't see how page faults are relevant in the context of pNFS layouts. The server is not servicing actual data accesses through page faults when constructing a LAYOUTGET response
Quoted 6 lines
Quoted 4 lines
2) Now that NFS apparently can serve up multiple mappings at once, should ->map_blocks pass in an array element count so that we can do multiple iomaps in a single lock cycle?
I guess we need to do that, or revert the code to provide multiple maps for now.
I think we should address the immediate problem first by replacing XFS_BMAPI_ENTIRE with 0, and handle support for returning multiple map entries from a single call to xfs_fs_map_blocks() in follow-up patches. That work is more involved, as it requires coordinated changes in both the nfsd and XFS code.
Quoted 4 lines
Quoted 3 lines
3) Do the reflink and realtime inode checks need to be re-assessed after grabbing the ilock since they can change?
Yes.
I can move the check for reflink and realtime inode inside the ilock in v2 of this patch series.
-Dai
Quoted 10 lines · Dai Ngo
Quoted 7 lines · Christoph Hellwig
[adding Sergey, who wrote the code to allow multiple mappings]
Quoted 3 lines · Darrick J. Wong
1)
xfs_fs_map_blocksonly takesi_rwsemandXFS_ILOCK; it doesn't take the mmap invalidation lock. Does that mean that pagefaults could wander in and mess with the layout?Looks like it.
Quoted 7 lines
I already described the scenario in which the file mapping can change between successive calls from nfsd—triggered by a single LAYOUTGET request—to
xfs_fs_map_blocks().I also don't see how page faults are relevant in the context of pNFS layouts. The server is not servicing actual data accesses through page faults when constructing a LAYOUTGET response
Some other process on the nfs server mmaps the file and generates faults.
Quoted 11 lines
Quoted 6 lines
Quoted 4 lines
2) Now that NFS apparently can serve up multiple mappings at once, should ->map_blocks pass in an array element count so that we can do multiple iomaps in a single lock cycle?
I guess we need to do that, or revert the code to provide multiple maps for now.
I think we should address the immediate problem first by replacing
XFS_BMAPI_ENTIREwith 0, and handle support for returning multiple map entries from a single call toxfs_fs_map_blocks()in follow-up patches. That work is more involved, as it requires coordinated changes in both the nfsd and XFS code.
Agreed.
Quoted 7 lines
Quoted 4 lines
Quoted 3 lines
3) Do the reflink and realtime inode checks need to be re-assessed after grabbing the ilock since they can change?
Yes.
I can move the check for reflink and realtime inode inside the ilock in v2 of this patch series.
Yeah, that probably needs to be made too. :)
--D
Quoted 3 lines
-Dai
Dai Ngo
reviewerQuoted 13 lines · Darrick J. Wong
Quoted 12 lines · Dai Ngo
Quoted 7 lines · Christoph Hellwig
[adding Sergey, who wrote the code to allow multiple mappings]
Quoted 3 lines · Darrick J. Wong
1)
xfs_fs_map_blocksonly takesi_rwsemandXFS_ILOCK; it doesn't take the mmap invalidation lock. Does that mean that pagefaults could wander in and mess with the layout?Looks like it.
I already described the scenario in which the file mapping can change between successive calls from nfsd—triggered by a single LAYOUTGET request—to
xfs_fs_map_blocks().
Quoted 5 lines
Quoted 3 lines
I also don't see how page faults are relevant in the context of pNFS layouts. The server is not servicing actual data accesses through page faults when constructing a LAYOUTGET response
Some other process on the nfs server mmaps the file and generates faults.
This would cause a layout conflict and this process would be delayed until the nfs server finishes recalling the layout from the first client.
Quoted 10 lines
Quoted 10 lines
Quoted 6 lines
Quoted 4 lines
2) Now that NFS apparently can serve up multiple mappings at once, should ->map_blocks pass in an array element count so that we can do multiple iomaps in a single lock cycle?
I guess we need to do that, or revert the code to provide multiple maps for now.
I think we should address the immediate problem first by replacing
XFS_BMAPI_ENTIREwith 0, and handle support for returning multiple map entries from a single call toxfs_fs_map_blocks()in follow-up patches. That work is more involved, as it requires coordinated changes in both the nfsd and XFS code.
Quoted 8 lines
Agreed.
Quoted 5 lines
Quoted 3 lines
Quoted 2 lines
3) Do the reflink and realtime inode checks need to be re-assessed after grabbing the ilock since they can change?
Yes.
I can move the check for reflink and realtime inode inside the ilock in v2 of this patch series.
Yeah, that probably needs to be made too. :)
Thanks! I'll include this change in v2.
-Dai
Quoted 6 lines
--D
Quoted 3 lines
-Dai
Hello Christoph, Dai, Darrick,
Thanks for the add!
Quoted 42 lines · Dai Ngo
I think this is what happened.
Assuming the current map of the file as follow:
0000-4095:
IOMAP_UNWRITTEN- block allocated
4096-8191:IOMAP_HOLE- no block allocated
8192-12287:IOMAP_UNWRITTEN- block allocatedNOTE:
. thebmapi_flagsused byxfs_bmapi_readis set toXFS_BMAPI_ENTIRE. . the nimaps count is set to 1.xfs_bmapi_readcan return only 1 extent.1st call from nfsd to
xfs_fs_map_blocks(), specifying offset 0, length 12288 and write is true.Extent Returned:
- offset: 0
- length: 4096 bytes (one filesystem block)
- type:IOMAP_UNWRITTEN(existing preallocated extent)
- addr: physical block number of the first unwritten extent.2nd call from nfsd to
xfs_fs_map_blocks(), specifying offset 4096, length 8192 and write is true.Returned Extent:
- offset: 4096
- length: 4096 bytes (one filesystem block)
- type:IOMAP_UNWRITTEN(freshly allocated unwritten extent)
- addr: physical block number assigned when filling the hole at 40963rd call from nfsd to
xfs_fs_map_blocks(), specifying offset 8192, length 4096 and write is true.Returned Extent
- offset: 0
- length: 12288 bytes (three contiguous blocks)
- type:IOMAP_UNWRITTEN
- addr: physical address of the merged unwritten allocation
Quoted 5 lines
After the second call, filling the hole merged the three unwritten segments into a single extent (
br_startoff= 0,br_blockcount= 3). When the third call looks up the range starting at 8192,xfs_bmapi_read()finds that single extent. BecauseXFS_BMAPI_ENTIREis set,xfs_bmapi_trim_map()bypasses trimming and returns the full extent toxfs_bmbt_to_iomap().
Great job! I think so too.
Quoted 6 lines · Dai Ngo
Quoted 5 lines · Christoph Hellwig
Quoted 4 lines
2) Now that NFS apparently can serve up multiple mappings at once, should ->map_blocks pass in an array element count so that we can do multiple iomaps in a single lock cycle?
Quoted 7 lines
Quoted 2 lines
I guess we need to do that, or revert the code to provide multiple maps for now.
I think we should address the immediate problem first by replacing
XFS_BMAPI_ENTIREwith 0, and handle support for returning multiple map entries from a single call toxfs_fs_map_blocks()in follow-up patches. That work is more involved, as it requires coordinated changes in both the nfsd and XFS code.
Agree with Dai. I believe that a revert is unnecessary as the upcoming patchset should fix the issue. At the same time, passing the array element count to the XFS code to find all iomaps in one lock cycle is a great direction for further improvement.
During LAYOUTGET processing, before extents are mapped, the nfsd server allocates a layout stateid and takes a lease on the file. Consequently, it is impossible for multiple clients to acquire layouts to the same file simultaneously. The server will first recall the layout from one client and then grant it to another. Additionally, two LAYOUTGET requests to the same file from a single client cannot be processed simultaneously as well, they are strictly ordered by the seqid. Thus, I do not know a race condition with two processes/threads accessing the same file via pNFS. And direct access to XFS on the MDS server, bypassing pNFS, is strictly discouraged, since it may have unexpected side effects regardless of this issue.
--
Sergey Bashirov
Quoted 8 lines · Dai Ngo
After reviewing
ext_tree_insert(), with assist from Codex, I think this function handles overlapping extents properly. The only issue I see inext_tree_insert()is the accuracy of the return error code, EINVAL instead of ENOMEM, whenkmemdup()fails.Since
ext_tree_insertseems to handle overlapping extents fine, do you think it's worth it to fixxfs_fs_map_blocks()to avoid returning overlap extents?
Oh, we absolutely should not return overlapping extents of the same class. So the bug fix itself is good, I was just hoping we could also improve the sanity checking on the client side.
Quoted 13 lines · Dai Ngo
Quoted 11 lines · Christoph Hellwig
Quoted 10 lines · Dai Ngo
A single LAYOUTGET request from the client can cause the server to issue multiple calls to
xfs_fs_map_blocks()for different offsets within the same extent. Because the use ofXFS_BMAPI_ENTIREflag, these calls can produce overlapping mappings.As a result, the LAYOUTGET reply sent to the NFS client may contain overlapping extents. This creates ambiguity in extent selection for a given file range, which can lead to incorrect device selection, inconsistent handling of datastate, and ultimately data corruption or protocol violations on the client side.
Quoted 8 lines
Quoted 3 lines
Please also add a check to the client that catches this and doesn't use the layout that has extents outside the requested range. And maybe warn about it as well.
The returned extents cover exactly the range requested in the LAYOUTGET op. However these extents are overlapping. For example, here is the on-the-wire capture of the LAYOUTGET operation and reply showing the overlapping extents:
Now that is really weird. How do we end up not using the remainder of the previous extent from a previous nfsd4_block_map_extent call in nfsd4_block_proc_layoutget? Looks like there is another bug hiding in the nfsd code somewhere.
And the client should probably also validate that extents of the same kind do not overlap (read vs write extents are allowed to overlap).