[PATCH v2] 9p: fix i_size update race in getattr with writeback caching

From: Pierre Barre
Date: Sat Dec 27 2025 - 13:03:57 EST


With writeback caching (cache=mmap), v9fs_stat2inode() and
v9fs_stat2inode_dotl() unconditionally overwrite i_size from the server
response, even when dirty pages may exist locally. This causes processes
using lseek(SEEK_END) to see incorrect file sizes, leading to data
corruption when extending files while concurrent stat() calls occur.

Fix by passing V9FS_STAT2INODE_KEEP_ISIZE when CACHE_WRITEBACK is
enabled to preserve the client's authoritative i_size.

Signed-off-by: Pierre Barre <pierre@xxxxxxxx>
---
fs/9p/vfs_inode.c | 7 ++++++-
fs/9p/vfs_inode_dotl.c | 7 ++++++-
2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/fs/9p/vfs_inode.c b/fs/9p/vfs_inode.c
index 97abe65bf7c1..bfba21f5d8a9 100644
--- a/fs/9p/vfs_inode.c
+++ b/fs/9p/vfs_inode.c
@@ -993,7 +993,12 @@ v9fs_vfs_getattr(struct mnt_idmap *idmap, const struct path *path,
if (IS_ERR(st))
return PTR_ERR(st);

- v9fs_stat2inode(st, d_inode(dentry), dentry->d_sb, 0);
+ /*
+ * With writeback caching, the client is authoritative for i_size.
+ * Don't let the server overwrite it with a potentially stale value.
+ */
+ v9fs_stat2inode(st, d_inode(dentry), dentry->d_sb,
+ (v9ses->cache & CACHE_WRITEBACK) ? V9FS_STAT2INODE_KEEP_ISIZE : 0);
generic_fillattr(&nop_mnt_idmap, request_mask, d_inode(dentry), stat);

p9stat_free(st);
diff --git a/fs/9p/vfs_inode_dotl.c b/fs/9p/vfs_inode_dotl.c
index 643e759eacb2..67a0ded2e223 100644
--- a/fs/9p/vfs_inode_dotl.c
+++ b/fs/9p/vfs_inode_dotl.c
@@ -453,7 +453,12 @@ v9fs_vfs_getattr_dotl(struct mnt_idmap *idmap,
if (IS_ERR(st))
return PTR_ERR(st);

- v9fs_stat2inode_dotl(st, d_inode(dentry), 0);
+ /*
+ * With writeback caching, the client is authoritative for i_size.
+ * Don't let the server overwrite it with a potentially stale value.
+ */
+ v9fs_stat2inode_dotl(st, d_inode(dentry),
+ (v9ses->cache & CACHE_WRITEBACK) ? V9FS_STAT2INODE_KEEP_ISIZE : 0);
generic_fillattr(&nop_mnt_idmap, request_mask, d_inode(dentry), stat);
/* Change block size to what the server returned */
stat->blksize = st->st_blksize;
--
2.43.0