Skip to contentSkip to conversation

nfsd: various delegated timestamp fixes

PATCH Superseded Jeff Layton 2 participants 15 messages Nov 20 – Nov 23 2 messages not in the archive

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jeff Layton

author maintainer
#0 lore raw

cover nfsd: various delegated timestamp fixes

While working on a rework of knfsd's delegated timestamp support, LLM review pointed out a few bugs in the existing code. This patchset is a pile of fixes for those issues.

The rework of the delegated timestamps is still a WIP, but these should be able to go in ahead of that.

---
Jeff Layton (11):
nfsd: don't hold inode_lock while waiting for a delegation in nfsd_setattr()
nfsd: take freeze protection before updating delegation timestamps
nfsd: count a delegation in cl_delegs_in_recall only once
nfsd: don't grant write delegations to NFSv4.0 clients on CLAIM_PREVIOUS
nfsd: set ctime when a SETATTR with delegated times changes other attributes
nfsd: pin the client while a GETATTR holds a conflicting delegation
nfsd: recall when a CB_GETATTR reply omits the size
nfsd: apply delegated times in a separate times-only setattr
nfsd: grant a plain read delegation when timestamps are wanted on a read open
nfsd: check fi_had_conflict under deleg_lock before hashing a delegation
nfsd: take the write delegation grant stat before hashing

 fs/nfsd/nfs4callback.c |   1 +
 fs/nfsd/nfs4proc.c     | 165 +++++++++++++++++++++-----------
 fs/nfsd/nfs4state.c    | 249 +++++++++++++++++++++++++++++++++++++------------
 fs/nfsd/nfs4xdr.c      |  55 +++++------
 fs/nfsd/state.h        |   2 +
 fs/nfsd/vfs.c          |  56 +++++------
 fs/nfsd/vfs.h          |   1 +
 fs/nfsd/xdr4.h         |   3 +
 8 files changed, 365 insertions(+), 167 deletions(-)

---
base-commit: 8489270d1cb9568fc88f3cda4626a19807300d77
change-id: 20261120-delegts-fixes-798331d91d8f

Best regards,

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

PATCH 1/11 nfsd: don't hold inode_lock while waiting for a delegation in nfsd_setattr()

When notify_change() breaks a delegation, nfsd_setattr() waits up to NFSD_DELEGRETURN_TIMEOUT for the holder to return it and then retries once. It waits while still holding inode_lock, but what the holder sends before DELEGRETURN can need that lock: WRITEs that flush its dirty data take i_rwsem exclusively on ext4 and xfs, and a SETATTR with delegated timestamps in the DELEGRETURN compound goes through nfsd_setattr() itself. The wait then always times out and SETATTR returns NFS4ERR_DELAY, after holding up the return it was waiting for.

Drop inode_lock across the wait, as callers of break_deleg_wait() do. The file can change while the lock is dropped, so start over: check that the truncate is allowed, take fresh pre-op attributes, and check the guard again, so that all of them stay atomic with the attempt that changes the inode. fh_fill_pre_attrs() does nothing once pre-op attributes are saved, so restore fh_pre_saved to its value on entry.

If the wait times out, still try once more rather than finishing with the attributes from before the wait. A lease that is already being broken isn't broken again, so the retry just gets -EAGAIN.

Don't start over once the size change is done, though, as when a filesystem returns -EAGAIN for the other attributes: the retry would truncate again, over whatever was written while the lock was dropped, and its new pre-op attributes would hide the truncate. Return the error instead, as if the wait had failed.

Fixes 34b91dda7124 ("NFSD: Make nfsd4_setattr() wait before returning NFS4ERR_DELAY")
---
 fs/nfsd/vfs.c | 42 +++++++++++++++++++++++++++---------------
 1 file changed, 27 insertions(+), 15 deletions(-)
fs/nfsd/vfs.c +27 -15
index 6905d3285ce1..4f3e480a3981 100644
@@ -550,7 +550,9 @@ nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp,
 	int		host_err = 0;
 	bool		get_write_count;
 	bool		size_change = (iap->ia_valid & ATTR_SIZE);
-	int		retries;
+	bool		pre_saved = fhp->fh_pre_saved;
+	bool		retried = false;
+	struct iattr	attrs;
 
 	trace_nfsd_vfs_setattr(rqstp, fhp, iap, guardtime);
 
@@ -612,6 +614,7 @@ nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp,
 		}
 	}
 
+retry:
 	inode_lock(inode);
 	if (size_change) {
 		err = nfsd_may_truncate(rqstp, fhp, iap);
@@ -631,20 +634,29 @@ nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp,
 		}
 	}
 
-	for (retries = 1;;) {
-		struct iattr attrs;
-
-		/*
-		 * notify_change() can alter its iattr argument, making
-		 * @iap unsuitable for submission multiple times. Make a
-		 * copy for every loop iteration.
-		 */
-		attrs = *iap;
-		host_err = __nfsd_setattr(dentry, &attrs);
-		if (host_err != -EAGAIN || !retries--)
-			break;
-		if (!nfsd_wait_for_delegreturn(rqstp, inode))
-			break;
+	/*
+	 * notify_change() can alter its iattr argument, making @iap
+	 * unsuitable for submission multiple times. Pass a copy.
+	 */
+	attrs = *iap;
+	host_err = __nfsd_setattr(dentry, &attrs);
+	/*
+	 * A delegation was broken. Give its holder a chance to return
+	 * it, without inode_lock: what it sends first, such as WRITEs
+	 * that flush its dirty data, can need the lock. The file can
+	 * change meanwhile, so start over, taking fresh pre-op
+	 * attributes and checking the guard again. That's only safe if
+	 * nothing was changed: a size change that is already done
+	 * (__nfsd_setattr() clears ATTR_SIZE) would be repeated over
+	 * whatever was written after it, so return the error then.
+	 */
+	if (host_err == -EAGAIN && !retried &&
+	    !(size_change && !(attrs.ia_valid & ATTR_SIZE))) {
+		inode_unlock(inode);
+		retried = true;
+		nfsd_wait_for_delegreturn(rqstp, inode);
+		fhp->fh_pre_saved = pre_saved;
+		goto retry;
 	}
 	if (attr->na_seclabel && attr->na_seclabel->len)
 		attr->na_labelerr = security_inode_setsecctx(dentry,
Signature · 1 line
2.56.1
author maintainer
#2 lore raw

PATCH 2/11 nfsd: take freeze protection before updating delegation timestamps

In reply to Jeff Layton

cb_getattr_update_times(), nfsd_update_cmtime_attr() and nfsd4_finalize_deleg_timestamps() call notify_change() under inode_lock(), but nothing holds freeze protection for the export. They modify a frozen filesystem: on ext4, a foreign GETATTR whose CB_GETATTR finds the holder modified the file trips the WARN_ON() in ext4_journal_check_start(), and the inode's mtime and ctime change while it is frozen.

The delegation's file and COPY's or CLONE's destination are open for write, so the mount's write access is held. Only the superblock's freeze protection is missing, and it goes outside inode_lock.

- cb_getattr_update_times() runs in an nfsd thread answering GETATTR. Rather than hold the thread until the thaw, it tries for freeze protection and returns NFS4ERR_DELAY, which has the client retry.
- nfsd_update_cmtime_attr(), from COPY and CLONE, waits for the thaw, as the data change that made the update necessary did.
- nfsd4_finalize_deleg_timestamps() runs when a delegation's lease is released, which includes the laundromat and nfsd shutdown. Those must not wait for a thaw, so it tries for freeze protection and leaves the times alone, with a notice, if the filesystem is frozen.

Fixes 7e8ae8486e44 ("fs/nfsd: fix update of inode attrs in CB_GETATTR")
Fixes e5e9b24ab8fa ("nfsd: freeze c/mtime updates with outstanding WRITE_ATTRS delegation")
Fixes 2863bac7f49c ("nfsd: update mtime/ctime on CLONE in presense of delegated attributes")
Fixes 4183cf383b6f ("nfsd: update mtime/ctime on COPY in presence of delegated attributes")
---
 fs/nfsd/nfs4state.c | 65 +++++++++++++++++++++++++++++++++++++++--------------
 1 file changed, 48 insertions(+), 17 deletions(-)
fs/nfsd/nfs4state.c +48 -17
index 048b2616d783..f6a01ab0602a 100644
@@ -1453,6 +1453,30 @@ static void put_deleg_file(struct nfs4_file *fp)
 	}
 }
 
+static void nfsd_cmtime_notice(struct inode *inode, int err)
+{
+	pr_notice_ratelimited("nfsd: Unable to update timestamps on inode %02x:%02x:%llu: %d\n",
+			      MAJOR(inode->i_sb->s_dev),
+			      MINOR(inode->i_sb->s_dev),
+			      inode->i_ino, err);
+}
+
+/* Caller holds freeze protection for @f's filesystem */
+static void __nfsd_update_cmtime_attr(struct file *f, unsigned int flags)
+{
+	struct inode *inode = file_inode(f);
+	struct iattr attr = {
+		.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_DELEG | flags,
+	};
+	int ret;
+
+	inode_lock(inode);
+	ret = notify_change(&nop_mnt_idmap, f->f_path.dentry, &attr, NULL);
+	inode_unlock(inode);
+	if (ret)
+		nfsd_cmtime_notice(inode, ret);
+}
+
 static void nfsd4_finalize_deleg_timestamps(struct nfs4_delegation *dp, struct file *f)
 {
 	/* don't do anything if FMODE_NOCMTIME isn't set */
@@ -1471,8 +1495,18 @@ static void nfsd4_finalize_deleg_timestamps(struct nfs4_delegation *dp, struct f
 	if (dp->dl_setattr)
 		return;
 
+	/*
+	 * This runs from the laundromat and from nfsd shutdown too, which
+	 * must not wait for a frozen filesystem to thaw. Leave the times
+	 * alone then.
+	 */
+	if (!file_start_write_trylock(f)) {
+		nfsd_cmtime_notice(file_inode(f), -EBUSY);
+		return;
+	}
 	/* Stamp everything to "now" */
-	nfsd_update_cmtime_attr(f, ATTR_ATIME);
+	__nfsd_update_cmtime_attr(f, ATTR_ATIME);
+	file_end_write(f);
 }
 
 static void nfs4_unlock_deleg_lease(struct nfs4_delegation *dp)
@@ -10780,10 +10814,18 @@ static int cb_getattr_update_times(struct dentry *dentry, struct nfs4_delegation
 	if (!attrs.ia_valid)
 		return 0;
 
+	/*
+	 * The delegation's file is open for write, so only freeze
+	 * protection is missing. Don't hold an nfsd thread until a frozen
+	 * filesystem thaws: NFS4ERR_DELAY has the client retry.
+	 */
+	if (!sb_start_write_trylock(inode->i_sb))
+		return -EAGAIN;
 	attrs.ia_valid |= ATTR_DELEG;
 	inode_lock(inode);
 	ret = notify_change(&nop_mnt_idmap, dentry, &attrs, NULL);
 	inode_unlock(inode);
+	sb_end_write(inode->i_sb);
 	return ret;
 }
 
@@ -11052,25 +11094,14 @@ nfsd_get_dir_deleg(struct nfsd4_compound_state *cstate,
  * @flags: any additional flags that should be updated
  *
  * Given upon opening a file delegated attributes were issues, update
- * @f attributes to current times.
+ * @f attributes to current times. Waits for a frozen filesystem to thaw,
+ * like the data change that makes this necessary.
  */
 void nfsd_update_cmtime_attr(struct file *f, unsigned int flags)
 {
-	int ret;
-	struct inode *inode = file_inode(f);
-	struct iattr attr = {
-		.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_DELEG | flags,
-	};
-
-	inode_lock(inode);
-	ret = notify_change(&nop_mnt_idmap, f->f_path.dentry, &attr, NULL);
-	inode_unlock(inode);
-	if (ret)
-		pr_notice_ratelimited("nfsd: Unable to update timestamps on "
-				      "inode %02x:%02x:%llu: %d\n",
-				      MAJOR(inode->i_sb->s_dev),
-				      MINOR(inode->i_sb->s_dev),
-				      inode->i_ino, ret);
+	file_start_write(f);
+	__nfsd_update_cmtime_attr(f, flags);
+	file_end_write(f);
 }
 
 static void
Signature · 1 line
2.56.1
author maintainer
#3 lore raw

PATCH 3/11 nfsd: count a delegation in cl_delegs_in_recall only once

In reply to Jeff Layton

nfsd_break_deleg_cb() can run more than once per lease:
- __break_lease() calls lm_break for a read breaker (FL_DOWNGRADE_PENDING) and again for a later write breaker (FL_UNLOCK_PENDING);
- nfsd_handle_dir_event() and nfsd_recall_all_dir_delegs() call it directly, possibly repeatedly.

Each call incremented cl_delegs_in_recall, but nfsd_change_deleg_cb() decrements it once at F_UNLCK. The leaked count keeps nfs4_anylock_blockers() true, so the client can never again become a courtesy client.

Skip setting dl_recalled, the increment, the client expiry, and fi_had_conflict when dl_recalled is already set. All callers and the F_UNLCK decrement run under flc_lock. fl_break_time = 0, nfsd_break_one_deleg() and the false return stay unconditional.

Fixes 66af25799940 ("NFSD: add courteous server support for thread with only delegation")
Fixes cb7444b27c85 ("nfsd: add notification handlers for dir events")
---
 fs/nfsd/nfs4state.c | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)
fs/nfsd/nfs4state.c +13 -6
index f6a01ab0602a..490fefaef52a 100644
@@ -6645,11 +6645,19 @@ nfsd_break_deleg_cb(struct file_lease *fl)
 
 	trace_nfsd_cb_recall(&dp->dl_stid);
 
-	dp->dl_recalled = true;
-	atomic_inc(&clp->cl_delegs_in_recall);
-	if (try_to_expire_client(clp)) {
-		nn = net_generic(clp->net, nfsd_net_id);
-		mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
+	/*
+	 * lm_break runs again for a write breaker after a read breaker, and
+	 * the dir notification paths call this directly. Only the first call
+	 * may count the recall, since nfsd_change_deleg_cb() undoes it once.
+	 */
+	if (!dp->dl_recalled) {
+		dp->dl_recalled = true;
+		atomic_inc(&clp->cl_delegs_in_recall);
+		if (try_to_expire_client(clp)) {
+			nn = net_generic(clp->net, nfsd_net_id);
+			mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
+		}
+		fp->fi_had_conflict = true;
 	}
 
 	/*
@@ -6659,7 +6667,6 @@ nfsd_break_deleg_cb(struct file_lease *fl)
 	 */
 	fl->fl_break_time = 0;
 
-	fp->fi_had_conflict = true;
 	nfsd_break_one_deleg(dp);
 	return false;
 }
Signature · 1 line
2.56.1
author maintainer
#4 lore raw

PATCH 4/11 nfsd: don't grant write delegations to NFSv4.0 clients on CLAIM_PREVIOUS

In reply to Jeff Layton

nfs4_open_delegation() refuses delegations for NFSv4.0 write opens on CLAIM_NULL and CLAIM_FH, as its comment explains, but CLAIM_PREVIOUS skips the check. An NFSv4.0 client reclaiming a write open during grace can therefore get a write delegation, and a GETATTR it sends before DELEGRETURN in the same compound can never be answered.

Apply the same check to CLAIM_PREVIOUS.

Fixes 1d3dd1d56ce8 ("NFSD: Enable write delegation support")
---
 fs/nfsd/nfs4state.c | 3 +++
 1 file changed, 3 insertions(+)
fs/nfsd/nfs4state.c +3 -0
index 490fefaef52a..64bf2e96dc7b 100644
@@ -7532,6 +7532,9 @@ nfs4_open_delegation(struct svc_rqst *rqstp, struct nfsd4_open *open,
 	open->op_recall = false;
 	switch (open->op_claim_type) {
 		case NFS4_OPEN_CLAIM_PREVIOUS:
+			if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE &&
+					!clp->cl_minorversion)
+				goto out_no_deleg;
 			if (!cb_up)
 				open->op_recall = true;
 			break;
Signature · 1 line
2.56.1
#5 lore raw

Re: [PATCH 04/11] nfsd: don't grant write delegations to NFSv4.0 clients on CLAIM_PREVIOUS

Quoted 20 lines · Jeff Layton

nfs4_open_delegation() refuses delegations for NFSv4.0 write opens on CLAIM_NULL and CLAIM_FH, as its comment explains, but CLAIM_PREVIOUS skips the check. An NFSv4.0 client reclaiming a write open during grace can therefore get a write delegation, and a GETATTR it sends before DELEGRETURN in the same compound can never be answered.

Apply the same check to CLAIM_PREVIOUS.

Fixes 1d3dd1d56ce8 ("NFSD: Enable write delegation support")
---
 fs/nfsd/nfs4state.c | 3 +++
 1 file changed, 3 insertions(+)
fs/nfsd/nfs4state.c +0 -0
index 490fefaef52a..64bf2e96dc7b 100644

fs/nfsd/nfs4state.c nfs4_open_delegation(struct svc_rqst *rqstp, struct nfsd4_open *open, · line 7536

Quoted 5 lines
fs/nfsd/nfs4state.c +2 -0
@@ -7532,6 +7532,9 @@ nfs4_open_delegation(struct svc_rqst *rqstp, struct nfsd4_open *open,
        open->op_recall = false;
        switch (open->op_claim_type) {
                case NFS4_OPEN_CLAIM_PREVIOUS:
+                       if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE &&
+                                       !clp->cl_minorversion)

The ! operator should only be used for boolean operations. Please use an explicit (clp->cl_minorversion == 0) to make the intention clear.

Aurélien
--
Aurélien Couderc <[email protected]>
Big Data/Data mining expert, chess enthusiast

author maintainer
#6 lore raw

PATCH 5/11 nfsd: set ctime when a SETATTR with delegated times changes other attributes

In reply to Jeff Layton

__nfsd_setattr() adds ATTR_CTIME only when ATTR_DELEG is clear, to keep a delegated atime-only update from moving ctime. But ATTR_DELEG is set for the whole iattr, so a holder's chmod or chown sent along with a delegated atime changes the mode or owner without updating ctime.

Add ATTR_CTIME unless the request is a delegated atime-only update. A delegated mtime whose ctime failed vetting now gets ctime = now instead of no ctime update.

Fixes f9a348e0de19 ("nfsd: don't set the ctime on delegated atime updates")
---
 fs/nfsd/vfs.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
fs/nfsd/vfs.c +5 -3
index 4f3e480a3981..c0d10c263a93 100644
@@ -514,10 +514,12 @@ static int __nfsd_setattr(struct dentry *dentry, struct iattr *iap)
 	/*
 	 * If ATTR_DELEG is set, then this is an update from a client that
 	 * holds a delegation. If this is an update for only the atime, the
-	 * ctime should not be changed. If the update contains the mtime
-	 * too, then ATTR_CTIME should already be set.
+	 * ctime should not be changed. Any other attribute change, such as
+	 * a mode or owner change sent along with a delegated atime, must
+	 * update the ctime.
 	 */
-	if (!(iap->ia_valid & ATTR_DELEG))
+	if (!(iap->ia_valid & ATTR_DELEG) ||
+	    (iap->ia_valid & ~(ATTR_DELEG | ATTR_ATIME | ATTR_ATIME_SET)))
 		iap->ia_valid |= ATTR_CTIME;
 
 	return notify_change(&nop_mnt_idmap, dentry, iap, NULL);
Signature · 1 line
2.56.1

A message that is not in the archive (sent off-list or not yet received): [email protected]

author maintainer
#8 lore raw

Re: [PATCH 05/11] nfsd: set ctime when a SETATTR with delegated times changes other attributes

Quoted 62 lines · On Fri, 2026-11-20 at 17:08 +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] nfsd: vetted delegated mtime clobbered on DELEGRETURN when ctime vet fails
--

commit d6f78374677a2327a4f30f528ea96e416823a66d
Author: Jeff Layton <[email protected]>

nfsd: set ctime when a SETATTR with delegated times changes other attributes

This commit sets ATTR_CTIME in __nfsd_setattr() whenever attributes other than a delegated atime are being modified.

Quoted 8 lines
fs/nfsd/vfs.c +0 -0
index 4f3e480a3981d..c0d10c263a930 100644
@@ -514,10 +514,12 @@ static int __nfsd_setattr(struct dentry *dentry, struct iattr *iap)
 	/*
 	 * If ATTR_DELEG is set, then this is an update from a client that
 	 * holds a delegation. If this is an update for only the atime, the

fs/nfsd/vfs.c static int __nfsd_setattr(struct dentry *dentry, struct iattr *iap) · line 523

Quoted 10 lines
fs/nfsd/vfs.c +5 -3
-	 * ctime should not be changed. If the update contains the mtime
-	 * too, then ATTR_CTIME should already be set.
+	 * ctime should not be changed. Any other attribute change, such as
+	 * a mode or owner change sent along with a delegated atime, must
+	 * update the ctime.
 	 */
-	if (!(iap->ia_valid & ATTR_DELEG))
+	if (!(iap->ia_valid & ATTR_DELEG) ||
+	    (iap->ia_valid & ~(ATTR_DELEG | ATTR_ATIME | ATTR_ATIME_SET)))
 		iap->ia_valid |= ATTR_CTIME;

[Severity: Medium]
Can a vetted delegated mtime that fails ctime vetting still get clobbered with current_time() on DELEGRETURN?

When iattr->ia_mtime passes vetting against dp->dl_mtime (where dp->dl_mtime < iattr->ia_mtime <= dp->dl_ctime), vet_deleg_attrs() keeps ATTR_MTIME | ATTR_MTIME_SET, clears ATTR_CTIME | ATTR_CTIME_SET, and leaves dp->dl_setattr as false:

fs/nfsd/nfs4proc.c:vet_deleg_attrs() {
	...
		if (nfsd4_vet_deleg_time(&iattr->ia_mtime, &dp->dl_mtime, &now)) {
			iattr->ia_ctime = iattr->ia_mtime;
			if (nfsd4_vet_deleg_time(&iattr->ia_ctime, &dp->dl_ctime, &now))
				dp->dl_setattr = true;
			else
				iattr->ia_valid &= ~(ATTR_CTIME | ATTR_CTIME_SET);
	...
}

With this change, __nfsd_setattr() now sets ATTR_CTIME so notify_change() applies the vetted delegated mtime and sets ctime = now. However, because dp->dl_setattr remains false (including in nfsd4_deleg_set_times() in the follow-up commit "nfsd: apply delegated times in a separate times-only setattr"), if the file was written (dp->dl_written is true), nfsd4_finalize_deleg_timestamps() on DELEGRETURN sees !dp->dl_setattr and calls __nfsd_update_cmtime_attr(f, ATTR_ATIME), overwriting the vetted delegated mtime and atime with current_time().

Quoted 2 lines

Setting dp->dl_setattr = true whenever ia_mtime passes vetting would avoid overwriting those timestamps on DELEGRETURN.

This predates this patch: before it, the vetted mtime was applied without ctime and dl_setattr likewise stayed false, so DELEGRETURN restamped the times then too. That's intentional per e5e9b24ab8fa — dl_setattr is only set when the delegated mtime is also a valid ctime. An mtime that doesn't post-date the ctime seen at grant time can't correspond to a write done under the delegation (clock skew), so on return we fall back to stamping current_time() rather than keep it.

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

PATCH 6/11 nfsd: pin the client while a GETATTR holds a conflicting delegation

In reply to Jeff Layton

nfsd4_deleg_getattr_conflict() takes a reference on a write delegation it finds through the inode's lease, sends CB_GETATTR, waits for the reply, and drops the reference only after that. nfsd4_encode_fattr4() can also hold the delegation until it has encoded the reply.

A delegation reference doesn't keep its client alive. __destroy_client() doesn't wait for stateid references, and the final nfs4_put_stid() uses the client (cl_lock, the stateid idr and ->sc_free). If the client is destroyed while the GETATTR waits, for example when its lease expires because it doesn't answer the callback, the GETATTR's put can touch a freed nfs4_client.

Under flc_lock, while the lease is still on the inode, take a client reference along with the delegation reference. Client teardown removes the lease before free_client() drops the client's initial reference, so the client is still live at that point. nfsd4_put_leased_deleg() drops both references.

Fixes c5967721e106 ("NFSD: handle GETATTR conflict with write delegation")
---
 fs/nfsd/nfs4state.c | 41 ++++++++++++++++++++++++++++++++++++++---
 fs/nfsd/nfs4xdr.c   |  2 +-
 fs/nfsd/state.h     |  1 +
 3 files changed, 40 insertions(+), 4 deletions(-)
fs/nfsd/nfs4state.c +38 -3
index 64bf2e96dc7b..0cd139ee0048 100644
@@ -2912,6 +2912,38 @@ void nfsd4_put_client(struct nfs4_client *clp)
 	kref_put(&clp->cl_nfsdfs.cl_ref, __free_client);
 }
 
+/*
+ * Reference a delegation found through its lease, for use after flc_lock
+ * is dropped. A delegation reference alone doesn't keep the client:
+ * __destroy_client() doesn't wait for stateid references, yet
+ * nfs4_put_stid() uses the client. So pin the client as well. Caller
+ * holds flc_lock with @dp's lease on the inode. Client teardown removes
+ * the lease before free_client() drops the client's initial reference,
+ * so that reference is still held here.
+ */
+static bool nfsd4_get_leased_deleg(struct nfs4_delegation *dp)
+{
+	if (!refcount_inc_not_zero(&dp->dl_stid.sc_count))
+		return false;
+	nfsd4_get_client(dp->dl_stid.sc_client);
+	return true;
+}
+
+/**
+ * nfsd4_put_leased_deleg - release a delegation found through its lease
+ * @dp: delegation to release
+ *
+ * Drops the delegation and client references that the lease walk took.
+ * May free the client, so the caller must be able to sleep.
+ */
+void nfsd4_put_leased_deleg(struct nfs4_delegation *dp)
+{
+	struct nfs4_client *clp = dp->dl_stid.sc_client;
+
+	nfs4_put_stid(&dp->dl_stid);
+	nfsd4_put_client(clp);
+}
+
 static void
 free_client(struct nfs4_client *clp)
 {
@@ -10854,7 +10886,7 @@ static int cb_getattr_update_times(struct dentry *dentry, struct nfs4_delegation
  *
  * Returns 0 if there is no conflict; otherwise an nfs_stat
  * code is returned. If @pdp is set to a non-NULL value, then the
- * caller must put the reference.
+ * caller must release it with nfsd4_put_leased_deleg().
  */
 __be32
 nfsd4_deleg_getattr_conflict(struct svc_rqst *rqstp, struct dentry *dentry,
@@ -10899,7 +10931,10 @@ nfsd4_deleg_getattr_conflict(struct svc_rqst *rqstp, struct dentry *dentry,
 		return 0;
 	}
 
-	refcount_inc(&dp->dl_stid.sc_count);
+	if (!nfsd4_get_leased_deleg(dp)) {
+		spin_unlock(&ctx->flc_lock);
+		return nfserr_jukebox;
+	}
 	ncf = &dp->dl_cb_fattr;
 	nfs4_cb_getattr(&dp->dl_cb_fattr);
 	spin_unlock(&ctx->flc_lock);
@@ -10940,7 +10975,7 @@ nfsd4_deleg_getattr_conflict(struct svc_rqst *rqstp, struct dentry *dentry,
 	}
 	status = nfs_ok;
 out_status:
-	nfs4_put_stid(&dp->dl_stid);
+	nfsd4_put_leased_deleg(dp);
 	return status;
 }
 
fs/nfsd/nfs4xdr.c +1 -1
index 950c67362566..e6131095024f 100644
@@ -4106,7 +4106,7 @@ nfsd4_encode_fattr4(struct svc_rqst *rqstp, struct xdr_stream *xdr,
 		if (!timespec64_is_epoch(&ncf->ncf_cb_atime))
 			args.stat.atime = ncf->ncf_cb_atime;
 
-		nfs4_put_stid(&dp->dl_stid);
+		nfsd4_put_leased_deleg(dp);
 	} else {
 		args.change_attr = nfsd4_change_attribute(&args.stat);
 	}
fs/nfsd/state.h +1 -0
index e9816000c943..f80abcc278f6 100644
@@ -977,6 +977,7 @@ extern void nfsd4_shutdown_callback(struct nfs4_client *);
 extern void nfsd4_shutdown_copy(struct nfs4_client *clp);
 void nfsd4_get_client(struct nfs4_client *clp);
 void nfsd4_put_client(struct nfs4_client *clp);
+void nfsd4_put_leased_deleg(struct nfs4_delegation *dp);
 void nfsd4_async_copy_reaper(struct nfsd_net *nn);
 bool nfsd4_has_active_async_copies(struct nfs4_client *clp);
 void nfsd_update_cmtime_attr(struct file *f, unsigned int flags);
Signature · 1 line
2.56.1
author maintainer
#10 lore raw

PATCH 7/11 nfsd: recall when a CB_GETATTR reply omits the size

In reply to Jeff Layton

decode_cb_fattr4() zeroes ncf_cb_fsize and fills it in only if the reply's bitmap includes SIZE. nfsd4_deleg_getattr_conflict() doesn't look at that bitmap. For a reply without SIZE, a non-empty file's size doesn't match zero, so the waiter marks the file modified, sets ncf_cur_fsize to zero, and GETATTR reports the file as empty to everyone but the delegation holder.

The server always requests SIZE, and a holder may have changed it. Keep word 0 of the reply's bitmap, cleared when the callback is issued, and in nfsd4_cb_getattr_done() treat a reply without SIZE as failed. The waiter then recalls the delegation, as it does for any other CB_GETATTR failure.

A reply that omits CHANGE is left alone: the zero change attribute mismatches, so the file is treated as modified, which is the safe answer.

Fixes c5967721e106 ("NFSD: handle GETATTR conflict with write delegation")
---
 fs/nfsd/nfs4callback.c | 1 +
 fs/nfsd/nfs4state.c    | 8 ++++++++
 fs/nfsd/state.h        | 1 +
 3 files changed, 10 insertions(+)
fs/nfsd/nfs4callback.c +1 -0
index 70ab4fd32195..ac65ba4c4b7d 100644
@@ -737,6 +737,7 @@ static int nfs4_xdr_dec_cb_getattr(struct rpc_rqst *rqstp,
 		return status;
 	if (xdr_stream_decode_uint32_array(xdr, bitmap, 3) < 0)
 		return -EIO;
+	ncf->ncf_cb_bitmap0 = bitmap[0];
 	if (xdr_stream_decode_u32(xdr, &attrlen) < 0)
 		return -EIO;
 	maxlen = sizeof(ncf->ncf_cb_change) + sizeof(ncf->ncf_cb_fsize);
fs/nfsd/nfs4state.c +8 -0
index 0cd139ee0048..eead615aac0a 100644
@@ -3876,6 +3876,13 @@ nfsd4_cb_getattr_done(struct nfsd4_callback *cb, struct rpc_task *task)
 
 	trace_nfsd_cb_getattr_done(&dp->dl_stid.sc_stateid, task);
 	ncf->ncf_cb_status = task->tk_status;
+	/*
+	 * A reply without SIZE leaves ncf_cb_fsize at zero, which the
+	 * waiter would report as the file's size. Fail it, so the waiter
+	 * recalls the delegation instead.
+	 */
+	if (!task->tk_status && !(ncf->ncf_cb_bitmap0 & FATTR4_WORD0_SIZE))
+		ncf->ncf_cb_status = NFS4ERR_IO;
 	switch (task->tk_status) {
 	case -NFS4ERR_DELAY:
 		rpc_delay(task, 2 * HZ);
@@ -4123,6 +4130,7 @@ static void nfs4_cb_getattr(struct nfs4_cb_fattr *ncf)
 
 	/* set to proper status when nfsd4_cb_getattr_done runs */
 	ncf->ncf_cb_status = NFS4ERR_IO;
+	ncf->ncf_cb_bitmap0 = 0;
 
 	/* ensure that wake_bit is done when RUNNING is cleared */
 	set_bit(NFSD4_CALLBACK_WAKE, &ncf->ncf_getattr.cb_flags);
fs/nfsd/state.h +1 -0
index f80abcc278f6..c5f607049aaf 100644
@@ -205,6 +205,7 @@ struct nfs4_cb_fattr {
 	u32 ncf_cb_status;
 
 	/* from CB_GETATTR reply */
+	u32 ncf_cb_bitmap0;		/* word 0 of the returned bitmap */
 	u64 ncf_cb_change;
 	u64 ncf_cb_fsize;
 	struct timespec64 ncf_cb_mtime;
Signature · 1 line
2.56.1
author maintainer
#11 lore raw

PATCH 8/11 nfsd: apply delegated times in a separate times-only setattr

In reply to Jeff Layton

Delegated times were decoded into sa_iattr with ATTR_DELEG and applied in the same nfsd_setattr() call as every other attribute. Problems:
- a holder that doesn't own the file gets EPERM: notify_change() checks ownership for explicit times, but the stateid is what grants the authority (RFC 9754);
- ATTR_DELEG applies to the whole iattr, so __nfsd_setattr() needs special ctime handling for the non-time attributes;
- if the times are rejected, attrsset can't report the attributes that were set.

Decode time_deleg_access/time_deleg_modify into new sa_deleg_atime and sa_deleg_mtime fields. After nfsd_setattr() handles the other attributes, nfsd4_deleg_set_times() applies the vetted times with notify_change(ATTR_FORCE) and an iattr that holds only times:
- fh_verify(NFSD_MAY_SATTR) first, since a times-only request skips nfsd_setattr() and with it the export's read-only and immutable checks;
- no ATTR_DELEG: nfsd_breaker_owns_lease() keeps the holder's own lease, and any other lease is broken;
- base vetting against dl_*time is kept. When the vetted mtime is applied but the delegated ctime is rejected, ctime now moves to the current time instead of being left alone, since the inode's mtime did change.

nfsd_setattr() is skipped for a request with only delegated times. A request that sets both the plain and delegated form of a time gets NFS4ERR_INVAL. That is checked, as is the stateid, only after the minor version's supported attributes, so a minor version without delegated times gets NFS4ERR_ATTRNOTSUPP for them. attrsset keeps the non-time bits when the times step fails. A failure inside nfsd_setattr() still reports nothing as set, since its interface can't report a partial update.

Delegated times in CREATE and OPEN createattrs get NFS4ERR_INVAL:
RFC 9754 allows them only in SETATTR by a delegation holder, and EXCLUSIVE4_1 already rejects them via suppattr_exclcreat. A minor version that doesn't support them still gets NFS4ERR_ATTRNOTSUPP.

ATTR_DELEG no longer reaches __nfsd_setattr(), so drop its ATTR_DELEG branch and always add ATTR_CTIME. commit_metadata() is made visible to nfs4proc.c.

Fixes 7e13f4f8d27d ("nfsd: handle delegated timestamps in SETATTR")
---
 fs/nfsd/nfs4proc.c | 165 +++++++++++++++++++++++++++++++++++------------------
 fs/nfsd/nfs4xdr.c  |  53 +++++++++--------
 fs/nfsd/vfs.c      |  16 +-----
 fs/nfsd/vfs.h      |   1 +
 fs/nfsd/xdr4.h     |   3 +
 5 files changed, 146 insertions(+), 92 deletions(-)
fs/nfsd/nfs4proc.c +111 -54
index 71d6a473c1bc..ae6ee42d169a 100644
@@ -1361,33 +1361,58 @@ nfsd4_secinfo_no_name_release(union nfsd4_op_u *u)
 		exp_put(u->secinfo_no_name.sin_exp);
 }
 
+#define NFSD4_DELEG_TIME_BITS	(FATTR4_WORD2_TIME_DELEG_ACCESS | \
+				 FATTR4_WORD2_TIME_DELEG_MODIFY)
+
 /*
- * Validate that the requested timestamps are within the acceptable range. If
- * timestamp appears to be in the future, then it will be clamped to
- * current_time().
+ * Apply a delegation holder's times in a times-only setattr. The stateid,
+ * not file ownership, grants the authority to set them, hence ATTR_FORCE.
+ * ATTR_DELEG isn't needed: nfsd_breaker_owns_lease() keeps the holder's
+ * own lease intact, and any other lease is broken.
  */
-static void
-vet_deleg_attrs(struct nfsd4_setattr *setattr, struct nfs4_delegation *dp)
+static __be32
+nfsd4_deleg_set_times(struct svc_rqst *rqstp, struct svc_fh *fhp,
+		      struct nfsd4_setattr *setattr, struct nfs4_delegation *dp)
 {
-	struct timespec64 now = current_time(dp->dl_stid.sc_file->fi_inode);
-	struct iattr *iattr = &setattr->sa_iattr;
+	struct iattr iattr = { };
+	struct timespec64 now;
+	struct inode *inode;
+	__be32 status;
+	int host_err;
 
-	if ((setattr->sa_bmval[2] & FATTR4_WORD2_TIME_DELEG_ACCESS) &&
-	    !nfsd4_vet_deleg_time(&iattr->ia_atime, &dp->dl_atime, &now))
-		iattr->ia_valid &= ~(ATTR_ATIME | ATTR_ATIME_SET);
+	/* nfsd_setattr() is skipped for a times-only request */
+	status = fh_verify(rqstp, fhp, 0, NFSD_MAY_SATTR);
+	if (status)
+		return status;
+	inode = d_inode(fhp->fh_dentry);
 
+	now = current_time(inode);
+	if (setattr->sa_bmval[2] & FATTR4_WORD2_TIME_DELEG_ACCESS) {
+		iattr.ia_atime = setattr->sa_deleg_atime;
+		if (nfsd4_vet_deleg_time(&iattr.ia_atime, &dp->dl_atime, &now))
+			iattr.ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
+	}
 	if (setattr->sa_bmval[2] & FATTR4_WORD2_TIME_DELEG_MODIFY) {
-		if (nfsd4_vet_deleg_time(&iattr->ia_mtime, &dp->dl_mtime, &now)) {
-			iattr->ia_ctime = iattr->ia_mtime;
-			if (nfsd4_vet_deleg_time(&iattr->ia_ctime, &dp->dl_ctime, &now))
+		iattr.ia_mtime = setattr->sa_deleg_mtime;
+		if (nfsd4_vet_deleg_time(&iattr.ia_mtime, &dp->dl_mtime, &now)) {
+			iattr.ia_valid |= ATTR_MTIME | ATTR_MTIME_SET | ATTR_CTIME;
+			iattr.ia_ctime = iattr.ia_mtime;
+			if (nfsd4_vet_deleg_time(&iattr.ia_ctime, &dp->dl_ctime, &now)) {
+				iattr.ia_valid |= ATTR_CTIME_SET;
 				dp->dl_setattr = true;
-			else
-				iattr->ia_valid &= ~(ATTR_CTIME | ATTR_CTIME_SET);
-		} else {
-			iattr->ia_valid &= ~(ATTR_CTIME | ATTR_CTIME_SET |
-					     ATTR_MTIME | ATTR_MTIME_SET);
+			}
 		}
 	}
+	if (!iattr.ia_valid)
+		return nfs_ok;
+
+	iattr.ia_valid |= ATTR_FORCE;
+	inode_lock(inode);
+	host_err = notify_change(&nop_mnt_idmap, fhp->fh_dentry, &iattr, NULL);
+	inode_unlock(inode);
+	if (!host_err)
+		host_err = commit_metadata(fhp);
+	return nfserrno(host_err);
 }
 
 static __be32
@@ -1401,16 +1426,34 @@ nfsd4_setattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
 		.na_pacl	= posix_acl_dup(setattr->sa_pacl),
 		.na_dpacl	= posix_acl_dup(setattr->sa_dpacl),
 	};
-	bool save_no_wcc, deleg_attrs;
+	struct nfs4_delegation *dp = NULL;
+	u32 *bmval = setattr->sa_bmval;
 	struct nfs4_stid *st = NULL;
-	struct inode *inode;
+	u32 deleg_bits, other_bits;
 	__be32 status = nfs_ok;
+	struct inode *inode;
+	bool save_no_wcc;
 	int err;
 
-	deleg_attrs = setattr->sa_bmval[2] & (FATTR4_WORD2_TIME_DELEG_ACCESS |
-					      FATTR4_WORD2_TIME_DELEG_MODIFY);
+	deleg_bits = bmval[2] & NFSD4_DELEG_TIME_BITS;
+	other_bits = bmval[0] | bmval[1] | (bmval[2] & ~NFSD4_DELEG_TIME_BITS);
 
-	if (deleg_attrs || (setattr->sa_iattr.ia_valid & ATTR_SIZE)) {
+	/* Before the checks below, which assume the minor version has them */
+	if (!nfsd_attrs_supported(cstate->minorversion, bmval)) {
+		status = nfserr_attrnotsupp;
+		goto out_err;
+	}
+
+	/* Setting both forms of the same time is ambiguous */
+	if (((deleg_bits & FATTR4_WORD2_TIME_DELEG_ACCESS) &&
+	     (bmval[1] & FATTR4_WORD1_TIME_ACCESS_SET)) ||
+	    ((deleg_bits & FATTR4_WORD2_TIME_DELEG_MODIFY) &&
+	     (bmval[1] & FATTR4_WORD1_TIME_MODIFY_SET))) {
+		status = nfserr_inval;
+		goto out_err;
+	}
+
+	if (deleg_bits || (setattr->sa_iattr.ia_valid & ATTR_SIZE)) {
 		int flags = WR_STATE;
 
 		if (setattr->sa_bmval[2] & FATTR4_WORD2_TIME_DELEG_ACCESS)
@@ -1423,20 +1466,15 @@ nfsd4_setattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
 			goto out_err;
 	}
 
-	if (deleg_attrs) {
+	if (deleg_bits) {
 		status = nfserr_bad_stateid;
-		if (st && (st->sc_type & SC_TYPE_DELEG)) {
-			struct nfs4_delegation *dp = delegstateid(st);
-
-			/* Only for *_ATTRS_DELEG flavors */
-			if (deleg_attrs_deleg(dp->dl_type)) {
-				vet_deleg_attrs(setattr, dp);
-				status = nfs_ok;
-			}
+		/* Only for *_ATTRS_DELEG flavors */
+		if (st && (st->sc_type & SC_TYPE_DELEG) &&
+		    deleg_attrs_deleg(delegstateid(st)->dl_type)) {
+			dp = delegstateid(st);
+			status = nfs_ok;
 		}
 	}
-	if (st)
-		nfs4_put_stid(st);
 	if (status)
 		goto out_err;
 
@@ -1445,36 +1483,55 @@ nfsd4_setattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
 		status = nfserrno(err);
 		goto out_err;
 	}
-	status = nfs_ok;
 
 	status = check_attr_support(cstate, setattr->sa_bmval, nfsd_attrmask);
 	if (status)
 		goto out;
 
-	if (setattr->sa_acl && (attrs.na_dpacl || attrs.na_pacl)) {
-		status = nfserr_inval;
-		goto out;
-	}
+	if (!deleg_bits || other_bits) {
+		if (setattr->sa_acl && (attrs.na_dpacl || attrs.na_pacl)) {
+			status = nfserr_inval;
+			goto out;
+		}
 
-	inode = cstate->current_fh.fh_dentry->d_inode;
-	status = nfsd4_acl_to_attr(S_ISDIR(inode->i_mode) ? NF4DIR : NF4REG,
-				   setattr->sa_acl, &attrs);
+		inode = cstate->current_fh.fh_dentry->d_inode;
+		status = nfsd4_acl_to_attr(S_ISDIR(inode->i_mode) ? NF4DIR : NF4REG,
+					   setattr->sa_acl, &attrs);
+		if (status)
+			goto out;
+		save_no_wcc = cstate->current_fh.fh_no_wcc;
+		cstate->current_fh.fh_no_wcc = true;
+		status = nfsd_setattr(rqstp, &cstate->current_fh, &attrs, NULL);
+		cstate->current_fh.fh_no_wcc = save_no_wcc;
+		if (!status)
+			status = nfserrno(attrs.na_labelerr);
+		if (!status)
+			status = nfserrno(attrs.na_dpaclerr);
+		if (!status)
+			status = nfserrno(attrs.na_paclerr);
+		if (status)
+			goto out;
+		/*
+		 * A failure inside nfsd_setattr() may follow a partial
+		 * update, but its interface can't report which parts were
+		 * applied, so such a failure reports nothing as set.
+		 */
+		setattr->sa_attrsset[0] = bmval[0];
+		setattr->sa_attrsset[1] = bmval[1];
+		setattr->sa_attrsset[2] = bmval[2] & ~NFSD4_DELEG_TIME_BITS;
+	}
 
-	if (status)
-		goto out;
-	save_no_wcc = cstate->current_fh.fh_no_wcc;
-	cstate->current_fh.fh_no_wcc = true;
-	status = nfsd_setattr(rqstp, &cstate->current_fh, &attrs, NULL);
-	cstate->current_fh.fh_no_wcc = save_no_wcc;
-	if (!status)
-		status = nfserrno(attrs.na_labelerr);
-	if (!status)
-		status = nfserrno(attrs.na_dpaclerr);
-	if (!status)
-		status = nfserrno(attrs.na_paclerr);
+	if (deleg_bits) {
+		status = nfsd4_deleg_set_times(rqstp, &cstate->current_fh,
+					       setattr, dp);
+		if (!status)
+			setattr->sa_attrsset[2] |= deleg_bits;
+	}
 out:
 	fh_drop_write(&cstate->current_fh);
 out_err:
+	if (st)
+		nfs4_put_stid(st);
 	nfsd_attrs_free(&attrs);
 	return status;
 }
fs/nfsd/nfs4xdr.c +28 -25

107 lines not on this page · raw message

fs/nfsd/vfs.c +3 -13
index c0d10c263a93..7b84269d52ac 100644
@@ -410,7 +410,7 @@ commit_inode_metadata(struct inode *inode)
 	return sync_inode_metadata(inode, 1);
 }
 
-static int
+int
 commit_metadata(struct svc_fh *fhp)
 {
 	struct inode *inode = d_inode(fhp->fh_dentry);
@@ -508,20 +508,10 @@ static int __nfsd_setattr(struct dentry *dentry, struct iattr *iap)
 			return 0;
 	}
 
-	if ((iap->ia_valid & ~ATTR_DELEG) == 0)
+	if (iap->ia_valid == 0)
 		return 0;
 
-	/*
-	 * If ATTR_DELEG is set, then this is an update from a client that
-	 * holds a delegation. If this is an update for only the atime, the
-	 * ctime should not be changed. Any other attribute change, such as
-	 * a mode or owner change sent along with a delegated atime, must
-	 * update the ctime.
-	 */
-	if (!(iap->ia_valid & ATTR_DELEG) ||
-	    (iap->ia_valid & ~(ATTR_DELEG | ATTR_ATIME | ATTR_ATIME_SET)))
-		iap->ia_valid |= ATTR_CTIME;
-
+	iap->ia_valid |= ATTR_CTIME;
 	return notify_change(&nop_mnt_idmap, dentry, iap, NULL);
 }
 
fs/nfsd/vfs.h +1 -0
index 40115d9aa245..ba07a18d69c6 100644
@@ -166,6 +166,7 @@ __be32		nfsd_lookup_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp,
 				   const char *name, unsigned int len,
 				   struct svc_export **exp_ret,
 				   struct dentry **dentry_ret, int access);
+int		commit_metadata(struct svc_fh *fhp);
 __be32		nfsd_setattr(struct svc_rqst *, struct svc_fh *,
 			     struct nfsd_attrs *, const struct timespec64 *);
 int nfsd_mountpoint(struct dentry *, struct svc_export *);
fs/nfsd/xdr4.h +3 -0
index 28481a2c8285..3031dace6d1b 100644
@@ -377,6 +377,9 @@ struct nfsd4_setattr {
 	struct xdr_netobj sa_label;
 	struct posix_acl *sa_dpacl;
 	struct posix_acl *sa_pacl;
+	struct timespec64 sa_deleg_atime;   /* request */
+	struct timespec64 sa_deleg_mtime;   /* request */
+	u32		sa_attrsset[3];     /* response */
 };
 
 struct nfsd4_setclientid {
Signature · 1 line
2.56.1

A message that is not in the archive (sent off-list or not yet received): [email protected]

author maintainer
#13 lore raw

Re: [PATCH 08/11] nfsd: apply delegated times in a separate times-only setattr

Quoted 115 lines · On Fri, 2026-11-20 at 17:08 +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] nfsd: inaccurate dl_setattr tracking in nfsd4_deleg_set_times()
--

commit 52dc07bb2ea3b8d66b511cb734482beaf6e291eb
Author: Jeff Layton <[email protected]>

nfsd: apply delegated times in a separate times-only setattr

This commit decodes delegated access and modify timestamps into dedicated fields in struct nfsd4_setattr and applies them after nfsd_setattr() via a separate notify_change() call with ATTR_FORCE.

Quoted 69 lines
fs/nfsd/nfs4proc.c +34 -18
index 71d6a473c1bc8..ae6ee42d169a7 100644
@@ -1361,33 +1361,58 @@ nfsd4_secinfo_no_name_release(union nfsd4_op_u *u)
 		exp_put(u->secinfo_no_name.sin_exp);
 }
 
+#define NFSD4_DELEG_TIME_BITS	(FATTR4_WORD2_TIME_DELEG_ACCESS | \
+				 FATTR4_WORD2_TIME_DELEG_MODIFY)
+
 /*
- * Validate that the requested timestamps are within the acceptable range. If
- * timestamp appears to be in the future, then it will be clamped to
- * current_time().
+ * Apply a delegation holder's times in a times-only setattr. The stateid,
+ * not file ownership, grants the authority to set them, hence ATTR_FORCE.
+ * ATTR_DELEG isn't needed: nfsd_breaker_owns_lease() keeps the holder's
+ * own lease intact, and any other lease is broken.
  */
-static void
-vet_deleg_attrs(struct nfsd4_setattr *setattr, struct nfs4_delegation *dp)
+static __be32
+nfsd4_deleg_set_times(struct svc_rqst *rqstp, struct svc_fh *fhp,
+		      struct nfsd4_setattr *setattr, struct nfs4_delegation *dp)
 {
-	struct timespec64 now = current_time(dp->dl_stid.sc_file->fi_inode);
-	struct iattr *iattr = &setattr->sa_iattr;
+	struct iattr iattr = { };
+	struct timespec64 now;
+	struct inode *inode;
+	__be32 status;
+	int host_err;
 
-	if ((setattr->sa_bmval[2] & FATTR4_WORD2_TIME_DELEG_ACCESS) &&
-	    !nfsd4_vet_deleg_time(&iattr->ia_atime, &dp->dl_atime, &now))
-		iattr->ia_valid &= ~(ATTR_ATIME | ATTR_ATIME_SET);
+	/* nfsd_setattr() is skipped for a times-only request */
+	status = fh_verify(rqstp, fhp, 0, NFSD_MAY_SATTR);
+	if (status)
+		return status;
+	inode = d_inode(fhp->fh_dentry);
 
+	now = current_time(inode);
+	if (setattr->sa_bmval[2] & FATTR4_WORD2_TIME_DELEG_ACCESS) {
+		iattr.ia_atime = setattr->sa_deleg_atime;
+		if (nfsd4_vet_deleg_time(&iattr.ia_atime, &dp->dl_atime, &now))
+			iattr.ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
+	}
 	if (setattr->sa_bmval[2] & FATTR4_WORD2_TIME_DELEG_MODIFY) {
-		if (nfsd4_vet_deleg_time(&iattr->ia_mtime, &dp->dl_mtime, &now)) {
-			iattr->ia_ctime = iattr->ia_mtime;
-			if (nfsd4_vet_deleg_time(&iattr->ia_ctime, &dp->dl_ctime, &now))
+		iattr.ia_mtime = setattr->sa_deleg_mtime;
+		if (nfsd4_vet_deleg_time(&iattr.ia_mtime, &dp->dl_mtime, &now)) {
+			iattr.ia_valid |= ATTR_MTIME | ATTR_MTIME_SET | ATTR_CTIME;
+			iattr.ia_ctime = iattr.ia_mtime;
+			if (nfsd4_vet_deleg_time(&iattr.ia_ctime, &dp->dl_ctime, &now)) {
+				iattr.ia_valid |= ATTR_CTIME_SET;
 				dp->dl_setattr = true;
-			else
-				iattr->ia_valid &= ~(ATTR_CTIME | ATTR_CTIME_SET);
-		} else {
-			iattr->ia_valid &= ~(ATTR_CTIME | ATTR_CTIME_SET |
-					     ATTR_MTIME | ATTR_MTIME_SET);
+			}
 		}
 	}
+	if (!iattr.ia_valid)

fs/nfsd/nfs4proc.c nfsd4_secinfo_no_name_release(union nfsd4_op_u *u) · line 1416

Quoted 10 lines
fs/nfsd/nfs4proc.c +9 -0
+		return nfs_ok;
+
+	iattr.ia_valid |= ATTR_FORCE;
+	inode_lock(inode);
+	host_err = notify_change(&nop_mnt_idmap, fhp->fh_dentry, &iattr, NULL);
+	inode_unlock(inode);
+	if (!host_err)
+		host_err = commit_metadata(fhp);
+	return nfserrno(host_err);
 }

[Severity: Medium]
Should dp->dl_setattr be set whenever ia_mtime passes vetting, and only after notify_change() succeeds?

With this change, when sa_deleg_mtime passes vetting against dp->dl_mtime but fails against dp->dl_ctime (dl_mtime < sa_deleg_mtime <= dl_ctime), notify_change() still applies the vetted mtime and updates ctime to the current time via ATTR_CTIME, yet dp->dl_setattr remains false. When the delegation is later returned, nfsd4_finalize_deleg_timestamps() sees !dp->dl_setattr and overwrites the client's vetted mtime (and atime) with the server's current time:

fs/nfsd/nfs4state.c:nfsd4_finalize_deleg_timestamps() {
	...
	if (dp->dl_setattr)
		return;
	...
	ret = __nfsd_update_cmtime_attr(f, ATTR_ATIME);
}
Quoted 4 lines

Conversely, setting dp->dl_setattr = true before notify_change() runs leaves it set if notify_change() fails, causing nfsd4_finalize_deleg_timestamps() to skip updating mtime and ctime on delegation return.

The mtime-only case is intentional (see e5e9b24ab8fa): dl_setattr is only set for a delegated mtime that is also a valid ctime; one that doesn't post-date the ctime seen at grant time is clock skew, so we restamp on return. Agreed on the ordering, though that predates this patch too; v2 sets dl_setattr only after notify_change() succeeds.

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

PATCH 9/11 nfsd: grant a plain read delegation when timestamps are wanted on a read open

In reply to Jeff Layton

A read open that wants delegated timestamps gets OPEN_DELEGATE_READ_ATTRS_DELEG. Several clients can hold read delegations at once, so delegating atime correctly would need exclusive authority over it, or merging the times of every read holder. Neither is implemented.

Grant OPEN_DELEGATE_READ instead. The want flag is a request, so declining the attribute delegation complies with RFC 9754 section 5. The XDR cases for the type stay, since the enum is on the wire.

Fixes 6ae30d6eb26b ("nfsd: add support for delegated timestamps")
---
 fs/nfsd/nfs4state.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
fs/nfsd/nfs4state.c +6 -4
index eead615aac0a..fc01ff50c8b2 100644
@@ -7328,10 +7328,14 @@ nfs4_set_delegation(struct nfsd4_open *open,
 	 * A read delegation can go to a client that holds a write open
 	 * of the file: nfsd_breaker_owns_lease() keeps the holder's own
 	 * opens from breaking it.
+	 *
+	 * Never OPEN_DELEGATE_READ_ATTRS_DELEG: delegating atime correctly
+	 * would need exclusive authority over it, or merging the times of
+	 * several read holders. The want flag is only a request.
 	 */
 	if (!nf && (open->op_share_access & NFS4_SHARE_ACCESS_READ)) {
 		nf = find_readable_file(fp);
-		dl_type = deleg_ts ? OPEN_DELEGATE_READ_ATTRS_DELEG : OPEN_DELEGATE_READ;
+		dl_type = OPEN_DELEGATE_READ;
 	}
 
 	if (!nf)
@@ -7629,9 +7633,7 @@ nfs4_open_delegation(struct svc_rqst *rqstp, struct nfsd4_open *open,
 		spin_unlock(&f->f_lock);
 		trace_nfsd_deleg_write(&dp->dl_stid.sc_stateid);
 	} else {
-		open->op_delegate_type = deleg_ts && nfs4_delegation_stat(dp, currentfh, &stat) ?
-					 OPEN_DELEGATE_READ_ATTRS_DELEG : OPEN_DELEGATE_READ;
-		dp->dl_atime = stat.atime;
+		open->op_delegate_type = OPEN_DELEGATE_READ;
 		trace_nfsd_deleg_read(&dp->dl_stid.sc_stateid);
 	}
 	nfs4_put_stid(&dp->dl_stid);
Signature · 1 line
2.56.1
author maintainer
#15 lore raw

PATCH 10/11 nfsd: check fi_had_conflict under deleg_lock before hashing a delegation

In reply to Jeff Layton

nfs4_set_delegation() checks fi_had_conflict without a lock, then takes deleg_lock and hashes the delegation. A lease break in between sets fi_had_conflict and queues the recall, whose prepare, nfsd4_cb_recall_prepare(), takes deleg_lock and queues the delegation on del_recall_lru only if it is hashed. If the prepare runs before the hash, the delegation is granted already recalled, and is never put on del_recall_lru, so the laundromat never revokes it if the client doesn't return it.

Check fi_had_conflict again under deleg_lock. The break sets it before queueing the recall, so a prepare that ran before the hash leaves it set, and one that runs after finds the delegation hashed.

Before 5ea9a7c5fe41, the final check was made under fi_lock, which nfsd_break_deleg_cb() also took.

Fixes 5ea9a7c5fe41 ("nfsd: don't take fi_lock in nfsd_break_deleg_cb()")
---
 fs/nfsd/nfs4state.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
fs/nfsd/nfs4state.c +11 -1
index fc01ff50c8b2..ea940d8759b0 100644
@@ -7427,7 +7427,17 @@ nfs4_set_delegation(struct nfsd4_open *open,
 	spin_lock(&nn->deleg_lock);
 	spin_lock(&clp->cl_lock);
 	spin_lock(&fp->fi_lock);
-	status = hash_delegation_locked(dp, fp);
+	/*
+	 * Check again under deleg_lock. A break after the check above has
+	 * queued a recall whose prepare takes deleg_lock, and that prepare
+	 * doesn't queue an unhashed delegation for revocation. Granting
+	 * the delegation then would leave a recalled delegation that the
+	 * laundromat never revokes.
+	 */
+	if (fp->fi_had_conflict)
+		status = -EAGAIN;
+	else
+		status = hash_delegation_locked(dp, fp);
 	if (status == -EEXIST) {
 		held = nfs4_delegation_exists(clp, fp);
 		*held_type = held->dl_type;
Signature · 1 line
2.56.1
author maintainer
#16 lore raw

PATCH 11/11 nfsd: take the write delegation grant stat before hashing

In reply to Jeff Layton

nfs4_open_delegation() sets ncf_initial_cinfo and dl_[amc]time from a stat taken after nfs4_set_delegation() has hashed the delegation. The window isn't reachable today: a CB_GETATTR sent in it carries no referring call, so the client answers NFS4ERR_BADHANDLE and the waiter never compares the zero ncf_initial_cinfo. But an upcoming per- delegation timestamp record must be initialized before anything can reach the delegation.

- Pass the current filehandle into nfs4_set_delegation() for the stat's export, and move nfs4_delegation_stat() above it.
- Take the grant stat after kernel_setlease() and the conflict checks, just before hashing. Grant no delegation if it fails.
- The stat can sleep, which widens the window between the unlocked fi_had_conflict check and hashing. The check under deleg_lock that the previous patch added still catches a break in it.
- Stat again after hashing, and decline the delegation if the change attribute moved. The holder's WRITE through an earlier open doesn't break its own lease and can land between the stats, as can a foreign write whose break raced past the fi_had_conflict check. A plain write delegation synthesizes change as ncf_initial_cinfo + 1, so a stale value could go below one clients already saw. Compare with the grant stat's change attribute, which nfs4_set_delegation() returns, not
ncf_initial_cinfo: once the delegation is hashed, a GETATTR whose CB_GETATTR reports a modified file can advance ncf_initial_cinfo.

---
 fs/nfsd/nfs4state.c | 91 +++++++++++++++++++++++++++++++++++------------------
 1 file changed, 61 insertions(+), 30 deletions(-)
fs/nfsd/nfs4state.c +61 -30
index ea940d8759b0..2e1abc6b3af4 100644
@@ -7276,11 +7276,35 @@ nfs4_delegation_record_grant_slot(struct nfs4_delegation *dp,
 	dp->dl_recall_grant.valid = true;
 }
 
+static bool
+nfs4_delegation_stat(struct nfs4_delegation *dp, struct svc_fh *currentfh,
+		     struct kstat *stat)
+{
+	struct nfsd_file *nf = find_writeable_file(dp->dl_stid.sc_file);
+	struct path path;
+	int rc;
+
+	if (!nf)
+		return false;
+
+	path.mnt = currentfh->fh_export->ex_path.mnt;
+	path.dentry = file_dentry(nf->nf_file);
+
+	rc = vfs_getattr(&path, stat,
+			 STATX_MODE | STATX_SIZE | STATX_ATIME |
+			 STATX_MTIME | STATX_CTIME | STATX_CHANGE_COOKIE,
+			 AT_STATX_SYNC_AS_STAT);
+
+	nfsd_file_put(nf);
+	return rc == 0;
+}
+
 static struct nfs4_delegation *
 nfs4_set_delegation(struct nfsd4_open *open,
 		    const struct nfsd4_compound_state *cstate,
 		    struct nfs4_ol_stateid *stp, struct svc_fh *parent,
-		    u32 *held_type)
+		    struct svc_fh *currentfh, u32 *held_type,
+		    u64 *grant_change)
 {
 	bool deleg_ts = nfsd4_want_deleg_timestamps(open);
 	struct nfs4_client *clp = stp->st_stid.sc_client;
@@ -7424,6 +7448,24 @@ nfs4_set_delegation(struct nfsd4_open *open,
 	if (fp->fi_had_conflict)
 		goto out_unlock;
 
+	/*
+	 * Anything that can reach the delegation once it is hashed may
+	 * read these, so set them first. nfs4_open_delegation() stats
+	 * again after hashing to catch a write that lands in between.
+	 */
+	if (deleg_is_write(dl_type)) {
+		struct kstat stat;
+
+		status = -EIO;
+		if (!nfs4_delegation_stat(dp, currentfh, &stat))
+			goto out_unlock;
+		*grant_change = nfsd4_change_attribute(&stat);
+		dp->dl_cb_fattr.ncf_initial_cinfo = *grant_change;
+		dp->dl_atime = stat.atime;
+		dp->dl_ctime = stat.ctime;
+		dp->dl_mtime = stat.mtime;
+	}
+
 	spin_lock(&nn->deleg_lock);
 	spin_lock(&clp->cl_lock);
 	spin_lock(&fp->fi_lock);
@@ -7487,29 +7529,6 @@ static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status,
 	}
 }
 
-static bool
-nfs4_delegation_stat(struct nfs4_delegation *dp, struct svc_fh *currentfh,
-		     struct kstat *stat)
-{
-	struct nfsd_file *nf = find_writeable_file(dp->dl_stid.sc_file);
-	struct path path;
-	int rc;
-
-	if (!nf)
-		return false;
-
-	path.mnt = currentfh->fh_export->ex_path.mnt;
-	path.dentry = file_dentry(nf->nf_file);
-
-	rc = vfs_getattr(&path, stat,
-			 STATX_MODE | STATX_SIZE | STATX_ATIME |
-			 STATX_MTIME | STATX_CTIME | STATX_CHANGE_COOKIE,
-			 AT_STATX_SYNC_AS_STAT);
-
-	nfsd_file_put(nf);
-	return rc == 0;
-}
-
 /*
  * Add NFS4_SHARE_ACCESS_READ to the write delegation granted on OPEN
  * with NFS4_SHARE_ACCESS_WRITE by allocating separate nfsd_file and
@@ -7578,6 +7597,7 @@ nfs4_open_delegation(struct svc_rqst *rqstp, struct nfsd4_open *open,
 	struct svc_fh *parent = NULL;
 	struct nfs4_delegation *dp;
 	u32 held_type = OPEN_DELEGATE_NONE;
+	u64 grant_change = 0;
 	struct kstat stat;
 	int status = 0;
 	int cb_up;
@@ -7612,8 +7632,8 @@ nfs4_open_delegation(struct svc_rqst *rqstp, struct nfsd4_open *open,
 		default:
 			goto out_no_deleg;
 	}
-	dp = nfs4_set_delegation(open, &resp->cstate, stp, parent,
-				 &held_type);
+	dp = nfs4_set_delegation(open, &resp->cstate, stp, parent, currentfh,
+				 &held_type, &grant_change);
 	if (IS_ERR(dp)) {
 		status = PTR_ERR(dp);
 		goto out_no_deleg;
@@ -7631,12 +7651,23 @@ nfs4_open_delegation(struct svc_rqst *rqstp, struct nfsd4_open *open,
 			nfs4_put_stid(&dp->dl_stid);
 			goto out_no_deleg;
 		}
+		/*
+		 * The holder's own WRITE through an earlier open doesn't
+		 * break the lease and can land after the grant stat, as can
+		 * a foreign write whose break raced past the fi_had_conflict
+		 * check. A plain write delegation synthesizes change from
+		 * ncf_initial_cinfo, so it must not be stale. Compare with
+		 * the grant stat itself: a GETATTR can already have moved
+		 * ncf_initial_cinfo past it.
+		 */
+		if (nfsd4_change_attribute(&stat) != grant_change) {
+			destroy_delegation(dp);
+			nfs4_put_stid(&dp->dl_stid);
+			status = -EAGAIN;
+			goto out_no_deleg;
+		}
 		open->op_delegate_type = deleg_ts ? OPEN_DELEGATE_WRITE_ATTRS_DELEG :
 						    OPEN_DELEGATE_WRITE;
-		dp->dl_cb_fattr.ncf_initial_cinfo = nfsd4_change_attribute(&stat);
-		dp->dl_atime = stat.atime;
-		dp->dl_ctime = stat.ctime;
-		dp->dl_mtime = stat.mtime;
 		spin_lock(&f->f_lock);
 		if (deleg_ts)
 			f->f_mode |= FMODE_NOCMTIME;
Signature · 1 line
2.56.1