[PATCH v2 2/3] NFS: stamp after the server's ctime when truncating under delegated times

From: Jeff Layton

Date: Mon Nov 09 2026 - 15:45:05 EST


A size-changing SETATTR always has the server stamp c/mtime
(__nfsd_setattr(), per RFC 5661 section 18.30.4). With delegated
timestamps the client discards the reply's ctime and stamps its own in
nfs_vmtruncate(), clamped only against its cached ctime. A coarse client
stamp can still land before the server's, which then won't move ctime
back when the holder returns its times, so ctime changes after
DELEGRETURN.

- Raise the cached ctime to the reply's before nfs_vmtruncate() stamps,
so the delegated stamp lands after the server's.
- Stamp before nfs_vmtruncate() drops i_lock for truncate_pagecache(),
not after. A delegation return starting in that window made the stamp
a no-op, and DELEGRETURN sent the pre-truncate mtime, which the server
could apply over its own truncate stamp.

Fixes: e12912d94137 ("NFSv4: Add support for delegated atime and mtime attributes")
Signed-off-by: Jeff Layton <jlayton@xxxxxxxxxx>
---
fs/nfs/inode.c | 23 ++++++++++++++++++++++-
1 file changed, 22 insertions(+), 1 deletion(-)

diff --git a/fs/nfs/inode.c b/fs/nfs/inode.c
index 98d8a34fab9d..270d8bb46110 100644
--- a/fs/nfs/inode.c
+++ b/fs/nfs/inode.c
@@ -647,6 +647,21 @@ static struct timespec64 nfs_delegated_stamp(struct inode *inode)
return now;
}

+/*
+ * The server stamped c/mtime itself (e.g. for a size change). Raise the
+ * cached ctime to the server's so that the next delegated stamp lands
+ * after it.
+ */
+static void nfs_delegated_ctime_raise(struct inode *inode,
+ const struct nfs_fattr *fattr)
+{
+ struct timespec64 ctime = inode_get_ctime(inode);
+
+ if ((fattr->valid & NFS_ATTR_FATTR_CTIME) &&
+ timespec64_compare(&fattr->ctime, &ctime) > 0)
+ inode_set_ctime_to_ts(inode, fattr->ctime);
+}
+
static void nfs_set_timestamps_to_ts(struct inode *inode, struct iattr *attr)
{
unsigned int cache_flags = 0;
@@ -820,10 +835,14 @@ static int nfs_vmtruncate(struct inode * inode, loff_t offset)
nfs_ooo_clear(NFS_I(inode));
}
NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_SIZE;
+ /*
+ * Stamp in the same i_lock section that checked the delegation and
+ * raised ctime, before a delegation return can start.
+ */
+ nfs_update_delegated_mtime_locked(inode);

spin_unlock(&inode->i_lock);
truncate_pagecache(inode, offset);
- nfs_update_delegated_mtime_locked(inode);
spin_lock(&inode->i_lock);
out:
return err;
@@ -849,6 +868,8 @@ void nfs_setattr_update_inode(struct inode *inode, struct iattr *attr,
if ((attr->ia_valid & ATTR_SIZE) != 0) {
if (!nfs_have_delegated_mtime(inode))
nfs_set_cache_invalid(inode, NFS_INO_INVALID_MTIME);
+ else
+ nfs_delegated_ctime_raise(inode, fattr);
nfs_set_cache_invalid(inode, NFS_INO_INVALID_BLOCKS);
nfs_inc_stats(inode, NFSIOS_SETATTRTRUNC);
nfs_vmtruncate(inode, attr->ia_size);

--
2.56.0