Skip to contentSkip to conversation

nfsd: more fixes for delegated timestamp issues

PATCH In review Jeff Layton 2 participants 7 messages Dec 2

Subsystems (2)
lore
Share & tools
lore

Jeff Layton

author maintainer
#0 lore raw

cover nfsd: more fixes for delegated timestamp issues

This fixes some problems with delegated timestamps that cause some pynfs tests to fail (in particular DELEG27 and the new, as-yet unmerged DELEG30 and DELEG31 tests).

I'm still looking at a rework of the delegated timestamp handling to not use FMODE_NOCMTIME, but for now I thought it best to fix up the known bugs as best we can first.

It would be nice to see this in v7.6.

---
Jeff Layton (5):
nfsd: skip the owner check when CB_GETATTR applies delegated times
nfsd: report vetted delegated times in GETATTR replies
nfsd: don't reapply unchanged CB_GETATTR times
nfsd: check for unchanged CB_GETATTR times under the shared inode_lock
nfsd: apply delegated times from CB_GETATTR when change and size match

 fs/nfsd/nfs4proc.c  |   8 ++++
 fs/nfsd/nfs4state.c | 119 +++++++++++++++++++++++++++++++++++++++++++++++++---
 fs/nfsd/nfs4xdr.c   |   6 ---
 fs/nfsd/state.h     |   3 ++
 4 files changed, 124 insertions(+), 12 deletions(-)

---
base-commit: b2daead2d7c04ad2dd2fc0ad06a130925bb18fcb
change-id: 20261202-delegts-fixes-ca2a66e7d92b

Best regards,

Signature · 1 line
Jeff Layton <[email protected]>
author maintainer
#1 lore raw

PATCH 1/5 nfsd: skip the owner check when CB_GETATTR applies delegated times

cb_getattr_update_times() runs under the credentials of the client whose GETATTR triggered the CB_GETATTR. Setting explicit times (ATTR_[AM]TIME_SET) requires that caller to own the file, so a GETATTR from a non-owner fails with NFS4ERR_PERM when the holder reports newer times.

The times come from the delegation holder, so set ATTR_FORCE, as nfsd4_deleg_set_times() does.

Fixes 6ae30d6eb26b ("nfsd: add support for delegated timestamps")
---
 fs/nfsd/nfs4state.c | 6 ++++++
 1 file changed, 6 insertions(+)
fs/nfsd/nfs4state.c +6 -0
index 6ddb35f5ae5a..40a598bc5b51 100644
@@ -10704,6 +10704,12 @@ static int cb_getattr_update_times(struct dentry *dentry, struct nfs4_delegation
 			if (nfsd4_vet_deleg_time(&attrs.ia_ctime, &dp->dl_ctime, &now))
 				attrs.ia_valid |= ATTR_CTIME_SET;
 		}
+		/*
+		 * The times are the holder's. The GETATTR caller whose
+		 * credentials this runs under need not own the file.
+		 */
+		if (attrs.ia_valid)
+			attrs.ia_valid |= ATTR_FORCE;
 	} else {
 		attrs.ia_valid |= ATTR_MTIME | ATTR_CTIME;
 	}
Signature · 1 line
2.56.1
author maintainer
#2 lore raw

PATCH 2/5 nfsd: report vetted delegated times in GETATTR replies

In reply to Jeff Layton

When a CB_GETATTR reply shows the file modified, nfsd4_encode_fattr4() reports the raw time_deleg_access and time_deleg_modify from the reply. cb_getattr_update_times() has already vetted them per RFC 9754 section 5 and written the result to the inode, so the reply can disagree with the inode:

- a time older than at grant, which the server ignores, is reported
- a time in the future is reported unclamped
- if the delegation was returned before the update, the finalized times are skipped but the raw ones are still reported

vfs_getattr() runs after the update and returns the inode's times. Drop the override.

Fixes 6ae30d6eb26b ("nfsd: add support for delegated timestamps")
---
 fs/nfsd/nfs4xdr.c | 6 ------
 1 file changed, 6 deletions(-)
fs/nfsd/nfs4xdr.c +0 -6
index 80760fd407aa..0e69588d7535 100644
@@ -4109,14 +4109,8 @@ nfsd4_encode_fattr4(struct svc_rqst *rqstp, struct xdr_stream *xdr,
 		if (ncf->ncf_file_modified) {
 			++ncf->ncf_initial_cinfo;
 			args.stat.size = ncf->ncf_cur_fsize;
-			if (!timespec64_is_epoch(&ncf->ncf_cb_mtime))
-				args.stat.mtime = ncf->ncf_cb_mtime;
 		}
 		args.change_attr = ncf->ncf_initial_cinfo;
-
-		if (!timespec64_is_epoch(&ncf->ncf_cb_atime))
-			args.stat.atime = ncf->ncf_cb_atime;
-
 		nfsd4_put_leased_deleg(dp);
 	} else {
 		args.change_attr = nfsd4_change_attribute(&args.stat);
Signature · 1 line
2.56.1
author maintainer
#3 lore raw

PATCH 3/5 nfsd: don't reapply unchanged CB_GETATTR times

In reply to Jeff Layton

ncf_file_modified is sticky, so once a CB_GETATTR reply shows a changed change or size, every later GETATTR calls cb_getattr_update_times(). dl_atime and dl_mtime stay at their grant values, so a reply repeating the holder's times passes vetting each time and is written to the inode
again: on xfs, a transaction with a forced i_version bump under the exclusive inode_lock.

A time in the future is clamped to the current time, which differs on each call, so a repeat moves the inode's time on every GETATTR.

Skip:

- a time the inode already holds after truncation, and the ctime update that goes with a skipped mtime;
- an exact repeat of the last raw time applied (new dl_cb_[am]time) while it is still in the future. Once the current time passes it, it is applied as is, once.

Fixes 6ae30d6eb26b ("nfsd: add support for delegated timestamps")
---
 fs/nfsd/nfs4state.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++++----
 fs/nfsd/state.h     |  3 ++
 2 files changed, 82 insertions(+), 5 deletions(-)
fs/nfsd/nfs4state.c +79 -5
index 40a598bc5b51..6de15d7fb09c 100644
@@ -10678,21 +10678,78 @@ bool nfsd4_vet_deleg_time(struct timespec64 *req, const struct timespec64 *orig,
 	return true;
 }
 
+/*
+ * Should a vetted CB_GETATTR time be dropped rather than applied?
+ * dl_atime and dl_mtime stay at their grant values, so a reply that
+ * repeats the holder's times passes vetting on every GETATTR. Drop a
+ * time the inode already holds. Also drop an exact repeat of the last
+ * raw time applied while it is still in the future: it was clamped to
+ * the current time, and clamping it again would move the inode's time
+ * on every GETATTR. Once the current time passes it, it's applied as is.
+ */
+static bool cb_getattr_time_unchanged(struct inode *inode,
+				      const struct timespec64 *vetted,
+				      const struct timespec64 *raw,
+				      const struct timespec64 *last_raw,
+				      const struct timespec64 *cur,
+				      const struct timespec64 *now)
+{
+	struct timespec64 ts = timestamp_truncate(*vetted, inode);
+
+	if (timespec64_equal(&ts, cur))
+		return true;
+	return timespec64_equal(raw, last_raw) &&
+	       timespec64_compare(raw, now) > 0;
+}
+
+/*
+ * Return @attrs->ia_valid without the vetted CB_GETATTR times that
+ * cb_getattr_time_unchanged() drops. ctime moves only with a delegated
+ * mtime. Caller holds inode_lock.
+ */
+static unsigned int cb_getattr_drop_unchanged(struct inode *inode,
+					      const struct nfs4_delegation *dp,
+					      const struct iattr *attrs,
+					      const struct timespec64 *cb_atime,
+					      const struct timespec64 *cb_mtime,
+					      const struct timespec64 *now)
+{
+	unsigned int valid = attrs->ia_valid;
+	struct timespec64 cur;
+
+	cur = inode_get_atime(inode);
+	if ((valid & ATTR_ATIME_SET) &&
+	    cb_getattr_time_unchanged(inode, &attrs->ia_atime, cb_atime,
+				      &dp->dl_cb_atime, &cur, now))
+		valid &= ~(ATTR_ATIME | ATTR_ATIME_SET);
+	cur = inode_get_mtime(inode);
+	if ((valid & ATTR_MTIME_SET) &&
+	    cb_getattr_time_unchanged(inode, &attrs->ia_mtime, cb_mtime,
+				      &dp->dl_cb_mtime, &cur, now))
+		valid &= ~(ATTR_MTIME | ATTR_MTIME_SET |
+			   ATTR_CTIME | ATTR_CTIME_SET);
+	return valid;
+}
+
 static int cb_getattr_update_times(struct dentry *dentry, struct nfs4_delegation *dp)
 {
 	struct inode *inode = d_inode(dentry);
 	struct nfs4_cb_fattr *ncf = &dp->dl_cb_fattr;
 	struct nfs4_file *fp = dp->dl_stid.sc_file;
+	struct timespec64 now, cb_atime, cb_mtime;
 	struct iattr attrs = { };
 	struct nfsd_file *nf;
+	unsigned int valid;
 	bool hashed;
 	int ret = 0;
 
 	if (deleg_attrs_deleg(dp->dl_type)) {
-		struct timespec64 now = current_time(inode);
+		now = current_time(inode);
+		cb_atime = ncf->ncf_cb_atime;
+		cb_mtime = ncf->ncf_cb_mtime;
 
-		attrs.ia_atime = ncf->ncf_cb_atime;
-		attrs.ia_mtime = ncf->ncf_cb_mtime;
+		attrs.ia_atime = cb_atime;
+		attrs.ia_mtime = cb_mtime;
 
 		if (nfsd4_vet_deleg_time(&attrs.ia_atime, &dp->dl_atime, &now))
 			attrs.ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
@@ -10746,8 +10803,25 @@ static int cb_getattr_update_times(struct dentry *dentry, struct nfs4_delegation
 	spin_lock(&fp->fi_lock);
 	hashed = delegation_hashed(dp);
 	spin_unlock(&fp->fi_lock);
-	if (hashed)
-		ret = notify_change(&nop_mnt_idmap, dentry, &attrs, NULL);
+	if (!hashed)
+		goto out_unlock;
+
+	if (deleg_attrs_deleg(dp->dl_type)) {
+		attrs.ia_valid = cb_getattr_drop_unchanged(inode, dp, &attrs,
+							   &cb_atime, &cb_mtime,
+							   &now);
+		if (!(attrs.ia_valid & (ATTR_ATIME | ATTR_MTIME)))
+			goto out_unlock;
+	}
+
+	/* notify_change() can alter its iattr argument */
+	valid = attrs.ia_valid;
+	ret = notify_change(&nop_mnt_idmap, dentry, &attrs, NULL);
+	if (!ret && (valid & ATTR_ATIME_SET))
+		dp->dl_cb_atime = cb_atime;
+	if (!ret && (valid & ATTR_MTIME_SET))
+		dp->dl_cb_mtime = cb_mtime;
+out_unlock:
 	inode_unlock(inode);
 	file_end_write(nf->nf_file);
 	nfsd_file_put(nf);
fs/nfsd/state.h +3 -0
index 7f2c63cd3bf8..7afdf6ef6727 100644
@@ -335,6 +335,9 @@ struct nfs4_delegation {
 	struct timespec64	dl_atime;
 	struct timespec64	dl_mtime;
 	struct timespec64	dl_ctime;
+	/* CB_GETATTR times last applied, before clamping (inode_lock) */
+	struct timespec64	dl_cb_atime;
+	struct timespec64	dl_cb_mtime;
 
 	/* For dir delegations */
 	u32			dl_notify_mask;
Signature · 1 line
2.56.1
author maintainer
#4 lore raw

PATCH 4/5 nfsd: check for unchanged CB_GETATTR times under the shared inode_lock

In reply to Jeff Layton

cb_getattr_update_times() takes freeze protection and the exclusive inode_lock before finding that a repeated reply has nothing to apply. Such a GETATTR then waits behind I/O for the exclusive lock, or fails with NFS4ERR_DELAY while the filesystem is frozen.

Check for unchanged times under the shared inode_lock first. Take freeze protection and the exclusive inode_lock only when a time is left to apply, and check again under them.

---
 fs/nfsd/nfs4state.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)
fs/nfsd/nfs4state.c +16 -1
index 6de15d7fb09c..f8817e60b0a2 100644
@@ -10705,7 +10705,7 @@ static bool cb_getattr_time_unchanged(struct inode *inode,
 /*
  * Return @attrs->ia_valid without the vetted CB_GETATTR times that
  * cb_getattr_time_unchanged() drops. ctime moves only with a delegated
- * mtime. Caller holds inode_lock.
+ * mtime. Caller holds inode_lock, shared or exclusive.
  */
 static unsigned int cb_getattr_drop_unchanged(struct inode *inode,
 					      const struct nfs4_delegation *dp,
@@ -10774,6 +10774,20 @@ static int cb_getattr_update_times(struct dentry *dentry, struct nfs4_delegation
 	if (!attrs.ia_valid)
 		return 0;
 
+	/*
+	 * A reply that repeats times the inode already holds still passes
+	 * vetting. Check for that under the shared lock first, so that it
+	 * waits for neither freeze protection nor the exclusive lock.
+	 */
+	if (deleg_attrs_deleg(dp->dl_type)) {
+		inode_lock_shared(inode);
+		valid = cb_getattr_drop_unchanged(inode, dp, &attrs, &cb_atime,
+						  &cb_mtime, &now);
+		inode_unlock_shared(inode);
+		if (!(valid & (ATTR_ATIME | ATTR_MTIME)))
+			return 0;
+	}
+
 	/*
 	 * The reference on @dp doesn't keep the delegation's file open.
 	 * A racing DELEGRETURN or revocation can put fi_deleg_file, so
@@ -10806,6 +10820,7 @@ static int cb_getattr_update_times(struct dentry *dentry, struct nfs4_delegation
 	if (!hashed)
 		goto out_unlock;
 
+	/* The inode's times can have moved since the shared check */
 	if (deleg_attrs_deleg(dp->dl_type)) {
 		attrs.ia_valid = cb_getattr_drop_unchanged(inode, dp, &attrs,
 							   &cb_atime, &cb_mtime,
Signature · 1 line
2.56.1
author maintainer
#5 lore raw

PATCH 5/5 nfsd: apply delegated times from CB_GETATTR when change and size match

In reply to Jeff Layton

nfsd4_deleg_getattr_conflict() applies time_deleg_access and time_deleg_modify from a CB_GETATTR reply only when the reply's change or size differs from the server's. RFC 9754 section 5 makes the holder of a timestamp delegation the authority for atime and mtime, with no such condition.

FMODE_NOCMTIME keeps the holder's WRITEs from moving mtime or the change attribute. After a flushed WRITE, change and size match, the holder's times are dropped, and other clients see new data with the mtime from grant time. pynfs DELEG27, DELEG30 and DELEG31 fail this way.

- For timestamp delegations, always vet and apply the CB_GETATTR times.
- Return the delegation to the caller even when nothing looks modified. Applying the times can bump i_version, so the reply must take change from ncf_initial_cinfo, not the inode.
- Mark the file modified when a newer mtime is applied, so change advances along with it.

dl_[amc]time stay at their grant values. The holder's SETATTR of the same times at return then still passes vetting and sets dl_setattr, so DELEGRETURN doesn't restamp over times other clients have seen. Repeated replies now reach cb_getattr_update_times() on every GETATTR, but the previous two patches already skip times with nothing to apply without taking freeze protection or the exclusive inode_lock.

The file is marked modified only when a newer mtime is actually written:
by a CB_GETATTR, or by the holder's own SETATTR of time_deleg_modify. Without the latter, a reply repeating that SETATTR's mtime applies nothing, and other clients would see the new mtime under the grant-time change attribute where they used to see the inode's.

Fixes 6ae30d6eb26b ("nfsd: add support for delegated timestamps")
---
 fs/nfsd/nfs4proc.c  |  8 ++++++++
 fs/nfsd/nfs4state.c | 16 ++++++++++++++--
 2 files changed, 22 insertions(+), 2 deletions(-)
fs/nfsd/nfs4proc.c +8 -0
index 2b83c6a4e289..33858c9035c8 100644
@@ -1409,6 +1409,14 @@ nfsd4_deleg_set_times(struct svc_rqst *rqstp, struct svc_fh *fhp,
 	iattr.ia_valid |= ATTR_FORCE;
 	inode_lock(inode);
 	host_err = notify_change(&nop_mnt_idmap, fhp->fh_dentry, &iattr, NULL);
+	/*
+	 * Other clients' GETATTRs report ncf_initial_cinfo rather than the
+	 * inode's change attribute, and a CB_GETATTR reply repeating this
+	 * mtime applies nothing. As in cb_getattr_update_times(), a newer
+	 * delegated mtime marks the file modified, so change moves with it.
+	 */
+	if (!host_err && deleg_mtime)
+		dp->dl_cb_fattr.ncf_file_modified = true;
 	inode_unlock(inode);
 	if (host_err)
 		return nfserrno(host_err);
fs/nfsd/nfs4state.c +14 -2
index f8817e60b0a2..0bb5e135210b 100644
@@ -10834,8 +10834,14 @@ static int cb_getattr_update_times(struct dentry *dentry, struct nfs4_delegation
 	ret = notify_change(&nop_mnt_idmap, dentry, &attrs, NULL);
 	if (!ret && (valid & ATTR_ATIME_SET))
 		dp->dl_cb_atime = cb_atime;
-	if (!ret && (valid & ATTR_MTIME_SET))
+	if (!ret && (valid & ATTR_MTIME_SET)) {
 		dp->dl_cb_mtime = cb_mtime;
+		/*
+		 * FMODE_NOCMTIME keeps the holder's WRITEs from moving the
+		 * change attribute. A newer delegated mtime shows them.
+		 */
+		ncf->ncf_file_modified = true;
+	}
 out_unlock:
 	inode_unlock(inode);
 	file_end_write(nf->nf_file);
@@ -10928,7 +10934,13 @@ nfsd4_deleg_getattr_conflict(struct svc_rqst *rqstp, struct dentry *dentry,
 		else if (i_size_read(inode) != ncf->ncf_cb_fsize)
 			ncf->ncf_file_modified = true;
 	}
-	if (ncf->ncf_file_modified) {
+	/*
+	 * The holder of a timestamp delegation is the authority for atime
+	 * and mtime (RFC 9754, section 5), even if change and size match.
+	 * Applying them can bump i_version, so the reply must take its
+	 * change attribute from ncf_initial_cinfo.
+	 */
+	if (ncf->ncf_file_modified || deleg_attrs_deleg(dp->dl_type)) {
 		int err;
 
 		/*
Signature · 1 line
2.56.1
maintainer
#6 lore raw
In reply to Jeff Layton
Quoted 9 lines · Jeff Layton

This fixes some problems with delegated timestamps that cause some pynfs tests to fail (in particular DELEG27 and the new, as-yet unmerged DELEG30 and DELEG31 tests).

I'm still looking at a rework of the delegated timestamp handling to not use FMODE_NOCMTIME, but for now I thought it best to fix up the known bugs as best we can first.

[...]

Applied to nfsd-testing, thanks!

[1/5] nfsd: skip the owner check when CB_GETATTR applies delegated times

      commit: bfb34f25820ff29b43172bcf85d1230b747672d6
[2/5] nfsd: report vetted delegated times in GETATTR replies
      commit: aec6ef5b22e97b35135ce80138c14bfe1fe38872
[3/5] nfsd: don't reapply unchanged CB_GETATTR times
      commit: a8e0970a596270c7141964cb4d5ec0830904c3cf
[4/5] nfsd: check for unchanged CB_GETATTR times under the shared inode_lock
      commit: 40b2beea3ac9f896fe827e1bf1e1af015faec8aa
[5/5] nfsd: apply delegated times from CB_GETATTR when change and size match
      commit: e184dd67baccccaa897e15e0162f156e12818bd3

--
Chuck Lever