Skip to contentSkip to conversation

Don't always trust server inode size while client holds write delegation

PATCH Superseded Dai Ngo 2 participants 3 messages Apr 1 – Apr 16

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Dai Ngo

author reviewer
#0 lore raw

PATCH 1/1 Don't always trust server inode size while client holds write delegation

With a write delegation, the server permits the client to buffer writes and associated metadata updates (including file size changes) locally and defer propagating them to the server. As a result, the server-reported GETATTR size may legitimately lag behind the client’s cached size for the duration of the delegation, so it must not be treated as authoritative.

This patch modifies nfs_wcc_update_inode to update the cached inode size only when the client does not hold a write delegation or the cache_validity of the nfs_inode has the NFS_INO_INVALID_SIZE bit set.

---
 fs/nfs/inode.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
fs/nfs/inode.c +5 -2
index 4786343eeee0..21161ebbd953 100644
@@ -1649,8 +1649,11 @@ static void nfs_wcc_update_inode(struct inode *inode, struct nfs_fattr *fattr)
 			&& (fattr->valid & NFS_ATTR_FATTR_SIZE)
 			&& i_size_read(inode) == nfs_size_to_loff_t(fattr->pre_size)
 			&& !nfs_have_writebacks(inode)) {
-		trace_nfs_size_wcc(inode, fattr->size);
-		i_size_write(inode, nfs_size_to_loff_t(fattr->size));
+		if ((!nfs_have_write_delegation(inode)) ||
+			(NFS_I(inode)->cache_validity & NFS_INO_INVALID_SIZE)) {
+			trace_nfs_size_wcc(inode, fattr->size);
+			i_size_write(inode, nfs_size_to_loff_t(fattr->size));
+		}
 	}
 }
 
Signature · 1 line
2.47.3
maintainer
#1 lore raw

Hi Dai,

Quoted 42 lines · Dai Ngo

With a write delegation, the server permits the client to buffer writes
and associated metadata updates (including file size changes) locally and
defer propagating them to the server. As a result, the server- reported
GETATTR size may legitimately lag behind the client’s cached size for the
duration of the delegation, so it must not be treated as authoritative.

This patch modifies nfs_wcc_update_inode to update the cached inode size
only when the client does not hold a write delegation or the cache_validity
of the nfs_inode has the NFS_INO_INVALID_SIZE bit set.

---
 fs/nfs/inode.c | 7 +++++--

 1 file changed, 5 insertions(+), 2 deletions(-)
fs/nfs/inode.c +0 -0
index 4786343eeee0..21161ebbd953 100644
@@ -1649,8 +1649,11 @@ static void nfs_wcc_update_inode(struct inode
 *inode, struct nfs_fattr *fattr)
  			&& (fattr->valid & NFS_ATTR_FATTR_SIZE)

  && i_size_read(inode) == nfs_size_to_loff_t(fattr->pre_size)
  && !nfs_have_writebacks(inode)) {

fs/nfs/inode.c +0 -2
-		trace_nfs_size_wcc(inode, fattr->size);
-		i_size_write(inode, nfs_size_to_loff_t(fattr-
Quoted 1 line

size));

fs/nfs/inode.c +2 -0
+		if ((!nfs_have_write_delegation(inode)) ||
+			(NFS_I(inode)->cache_validity &

NFS_INO_INVALID_SIZE)) {

fs/nfs/inode.c +2 -0
+			trace_nfs_size_wcc(inode, fattr->size);
+			i_size_write(inode,

nfs_size_to_loff_t(fattr->size));

fs/nfs/inode.c +1 -0
+		}
Quoted 3 lines

  }
 }

Under what circumstances are you seeing this making a difference? Please see the call to nfs_writeback_check_extend() in nfs_writeback_update_inode() which is supposed to prevent the issue you're talking about.

Signature · 3 lines
Trond Myklebust
Linux NFS client maintainer, Hammerspace
[email protected], [email protected]
author reviewer
#2 lore raw

Hi Trond,

Thank you for your feedback.

Quoted 38 lines · Trond Myklebust

Hi Dai,

Quoted 35 lines · Dai Ngo

With a write delegation, the server permits the client to buffer writes
and associated metadata updates (including file size changes) locally and
defer propagating them to the server. As a result, the server- reported
GETATTR size may legitimately lag behind the client’s cached size for the
duration of the delegation, so it must not be treated as authoritative.

This patch modifies nfs_wcc_update_inode to update the cached inode size
only when the client does not hold a write delegation or the cache_validity
of the nfs_inode has the NFS_INO_INVALID_SIZE bit set.

---
  fs/nfs/inode.c | 7 +++++--
  1 file changed, 5 insertions(+), 2 deletions(-)
fs/nfs/inode.c +0 -2
index 4786343eeee0..21161ebbd953 100644
@@ -1649,8 +1649,11 @@ static void nfs_wcc_update_inode(struct inode
 *inode, struct nfs_fattr *fattr)
  			&& (fattr->valid & NFS_ATTR_FATTR_SIZE)
  			&& i_size_read(inode) ==
 nfs_size_to_loff_t(fattr->pre_size)
  			&& !nfs_have_writebacks(inode)) {
-		trace_nfs_size_wcc(inode, fattr->size);
-		i_size_write(inode, nfs_size_to_loff_t(fattr-
 > size));
Quoted 11 lines

fs/nfs/inode.c

Quoted 10 lines
fs/nfs/inode.c +2 -0
+		if ((!nfs_have_write_delegation(inode)) ||
+			(NFS_I(inode)->cache_validity &

NFS_INO_INVALID_SIZE)) {

fs/nfs/inode.c +2 -0
+			trace_nfs_size_wcc(inode, fattr->size);
+			i_size_write(inode,

nfs_size_to_loff_t(fattr->size));

fs/nfs/inode.c +1 -0
+		}
  	}
  }

Under what circumstances are you seeing this making a difference?

I see file corruption when running the git test with v4.2 and pNFS with SCSI layout:

# GIT_TEST_EXT_CHAIN_LINT=0 && export GIT_TEST_EXT_CHAIN_LINT && time make -j100 rm -f -r 'test-results'

chainlinttmp/actual renamed from chainlinttmp/expect binary +0 -0
index 178e6e4..8370f45 100644
Binary files a/chainlinttmp/expect and b/chainlinttmp/actual differ

make: *** [Makefile:83: check-chainlint] Error 1

real    0m10.935s
user    0m10.977s
sys     0m10.425s

#

Quoted 3 lines

Please see the call to nfs_writeback_check_extend() in nfs_writeback_update_inode() which is supposed to prevent the issue you're talking about.

in nfs_writeback_update_inode(), if the inode has delegated mtime then nfs_writeback_check_extend is not called.

This is the sequence that leads to the file corruption:

. thread 1 writes 2 bytes to offset 4094 of a file. . inode size grows to 4096.

. thread1 writes 32 bytes to offset 4096 of the same file. . inode size grows to 4128.

. thread2 calls nfs_getattr on the same file.
. nfs_getattr checks if it needs up-to-date ctime/mtime/change.
If client has nfs_have_delegated_mtime it calls filemap_fdatawrite to flush dirty page-cache data to the server asynchronously.

nfs_writepages breaks dirty pages into 2 folios, one for each page.

. thread2 calls bl_write_pagelist to write dirty page 0 to the DS.

. pNFS FLUSH/WRITE of page 0 to DS done.
kworker updates pnfs_layout_hdr.plh_lwb to 4096.

. thread2 calls bl_write_pagelist to write dirty page 1 to the DS.

. thread2 then sends LAYOUTCOMMIT/GETATTR and uses pnfs_layout_hdr.plh_lwb
(4096) to set lastbytewritten which is used by the server to update the remote inode size which is then returned in the GETATTR reply.

. pNFS FLUSH/WRITE of page 1 to DS done.
kworker updates pnfs_layout_hdr.plh_lwb to 4128.

. thread2 calls nfs_wcc_update_inode to update inode size to 4096 as
returned by the GETATTR while the local inode size is 4128.

. thread2 then writes 77 bytes to offset 4128. Since the inode size is
now 4096 nfs_truncate_last_folio is called to zero out the 1st 32 bytes of the page. This causes the file to be corrupted.

-Dai

Quoted 1 line