Skip to contentSkip to conversation

nfsd: various delegated timestamp fixes

PATCH New Jeff Layton 1 participant 12 messages Nov 23

Subsystems (2)
lore
Share & tools
lore

Jeff Layton

author maintainer
#0 lore raw

v2 cover nfsd: various delegated timestamp fixes

This version fixes up some bugs that Sashiko pointed out, as well as a nit that Aurélien pointed out.

While I was reworking NFSD's delegated timestamp support, LLM review found several bugs in the existing code. This series fixes them.

The timestamp rework is still a work in progress. These fixes can go in ahead of it.

---
Changes in v2:
- nfsd_setattr(): sanitize a fresh copy of the attributes under
inode_lock on each attempt, since the setid bits a chown must clear
depend on the current mode
- cb_getattr_update_times(): pin fi_deleg_file for the mount's write
access, and skip the update once the delegation is unhashed
- nfsd4_deleg_set_times(): set dl_setattr only after notify_change() succeeds
- add the missing Signed-off-by to the nfsd_setattr() inode_lock patch
- reword changelogs and comments
- don't use ! operator on cl_minorversion
- Link to v1: https://lore.kernel.org/r/[email protected]

---
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     | 171 ++++++++++++++++++++----------
 fs/nfsd/nfs4state.c    | 281 ++++++++++++++++++++++++++++++++++++++-----------
 fs/nfsd/nfs4xdr.c      |  56 +++++-----
 fs/nfsd/state.h        |   2 +
 fs/nfsd/vfs.c          |  62 ++++++-----
 fs/nfsd/vfs.h          |   1 +
 fs/nfsd/xdr4.h         |   3 +
 8 files changed, 405 insertions(+), 172 deletions(-)

---
base-commit: 194069b5ff38d82d302c33a4dd280d948c96cfe6
change-id: 20261120-delegts-fixes-798331d91d8f

Best regards,

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

PATCH v2 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 the delegation and then retries once. nfsd_setattr() waits with inode_lock still held, but what the holder sends before DELEGRETURN can need that lock. WRITEs that flush the holder's 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 the SETATTR fails with NFS4ERR_DELAY after stalling the DELEGRETURN that the wait was 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 after the wait. Check again that the truncate is allowed, take fresh pre-op attributes, check the guard again, and sanitize a fresh copy of the attributes, since the setid bits that a chown must clear depend on the current mode. Redoing these steps under the lock keeps them 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 after the truncate. The retry would truncate again, over whatever was written while the lock was dropped, and the retry's fresh pre-op attributes would hide the first 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 | 48 +++++++++++++++++++++++++++++++-----------------
 1 file changed, 31 insertions(+), 17 deletions(-)
fs/nfsd/vfs.c +31 -17
index e033fbc225cf..4a5674e583cc 100644
@@ -549,7 +549,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);
 
@@ -587,8 +589,6 @@ nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp,
 	dentry = fhp->fh_dentry;
 	inode = d_inode(dentry);
 
-	nfsd_sanitize_attrs(inode, iap);
-
 	/*
 	 * The size case is special, it changes the file in addition to the
 	 * attributes, and file systems don't expect it to be mixed with
@@ -611,6 +611,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);
@@ -630,20 +631,33 @@ 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,
+	 * sanitized under inode_lock, because the setid bits that a
+	 * chown must clear depend on the current mode.
+	 */
+	attrs = *iap;
+	nfsd_sanitize_attrs(inode, &attrs);
+	host_err = __nfsd_setattr(dentry, &attrs);
+	/*
+	 * A delegation was broken. Drop inode_lock while waiting for the
+	 * holder to return the delegation, since what the holder sends
+	 * first, such as WRITEs that flush the holder's dirty data, can
+	 * need the lock. The file can change meanwhile, so start over,
+	 * taking fresh pre-op attributes and checking the guard again.
+	 * Starting over is safe only if this attempt changed nothing. A
+	 * retry would repeat a size change that is already done
+	 * (__nfsd_setattr() clears ATTR_SIZE) over whatever was written
+	 * after the truncate, so return the error in that case.
+	 */
+	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 v2 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's filesystem. They can therefore modify a frozen filesystem. On ext4, a foreign GETATTR whose CB_GETATTR finds that the holder modified the file trips the WARN_ON() in ext4_journal_check_start(), and the inode's mtime and ctime change while the filesystem is frozen.

The delegation's file (but see CB_GETATTR below) and the COPY or CLONE destination are open for write, so the mount's write access is held. Only the superblock's freeze protection is missing. Freeze protection is taken outside inode_lock.

- cb_getattr_update_times() runs in the nfsd thread that is processing the GETATTR. Rather than hold the thread until the thaw, cb_getattr_update_times() tries for freeze protection and returns NFS4ERR_DELAY, so the client retries. The GETATTR's delegation reference doesn't keep the file open, though. A racing DELEGRETURN or revocation can put fi_deleg_file, so pin fi_deleg_file to keep the write access. Skip the update once the delegation is unhashed, because the delegation's return may already have finalized the times. The times from CB_GETATTR are older and would roll the finalized times back.
- 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, including from the laundromat and during NFSD shutdown. Neither path may wait for a thaw, so nfsd4_finalize_deleg_timestamps() only tries for freeze protection, and leaves the times alone with a notice when 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 | 94 ++++++++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 75 insertions(+), 19 deletions(-)
fs/nfsd/nfs4state.c +75 -19
index 048b2616d783..c4c0f6cfa14d 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 function also runs from the laundromat and from NFSD
+	 * shutdown. Neither path may wait for a frozen filesystem to
+	 * thaw, so leave the times alone while the filesystem is frozen.
+	 */
+	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)
@@ -10755,8 +10789,11 @@ static int cb_getattr_update_times(struct dentry *dentry, struct nfs4_delegation
 {
 	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 iattr attrs = { };
-	int ret;
+	struct nfsd_file *nf;
+	bool hashed;
+	int ret = 0;
 
 	if (deleg_attrs_deleg(dp->dl_type)) {
 		struct timespec64 now = current_time(inode);
@@ -10780,10 +10817,40 @@ static int cb_getattr_update_times(struct dentry *dentry, struct nfs4_delegation
 	if (!attrs.ia_valid)
 		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
+	 * pin fi_deleg_file to keep the mount's write access.
+	 */
+	rcu_read_lock();
+	nf = nfsd_file_get(rcu_dereference(fp->fi_deleg_file));
+	rcu_read_unlock();
+	if (!nf)
+		return 0;
+
+	/*
+	 * Don't hold an nfsd thread until a frozen filesystem thaws.
+	 * NFS4ERR_DELAY makes the client retry.
+	 */
+	if (!file_start_write_trylock(nf->nf_file)) {
+		nfsd_file_put(nf);
+		return -EAGAIN;
+	}
 	attrs.ia_valid |= ATTR_DELEG;
 	inode_lock(inode);
-	ret = notify_change(&nop_mnt_idmap, dentry, &attrs, NULL);
+	/*
+	 * Once @dp is unhashed, its return or revocation may already have
+	 * finalized the times under inode_lock. The CB_GETATTR times are
+	 * older and would roll the finalized times back.
+	 */
+	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);
 	inode_unlock(inode);
+	file_end_write(nf->nf_file);
+	nfsd_file_put(nf);
 	return ret;
 }
 
@@ -11052,25 +11119,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 v2 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 nfsd_break_deleg_cb() directly, possibly repeatedly.

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

When dl_recalled is already set, skip the increment, the client expiry, and the stores to dl_recalled and fi_had_conflict. All callers and the F_UNLCK decrement run under flc_lock. Setting fl_break_time to 0, the nfsd_break_one_deleg() call, 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 | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)
fs/nfsd/nfs4state.c +14 -6
index c4c0f6cfa14d..5c2151b329c4 100644
@@ -6645,11 +6645,20 @@ 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 nfsd_break_deleg_cb() directly.
+	 * Only the first call may count the recall, since
+	 * nfsd_change_deleg_cb() decrements cl_delegs_in_recall only 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 +6668,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 v2 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 NFSD can never complete a GETATTR that the client sends before DELEGRETURN in the same compound.

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 5c2151b329c4..4b28553efc8d 100644
@@ -7533,6 +7533,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 == 0)
+				goto out_no_deleg;
 			if (!cb_up)
 				open->op_recall = true;
 			break;
Signature · 1 line
2.56.1
author maintainer
#5 lore raw

PATCH v2 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 fails vetting now moves ctime to the current time instead of leaving ctime alone.

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 4a5674e583cc..7bc251d55c3c 100644
@@ -513,10 +513,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
author maintainer
#6 lore raw

PATCH v2 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 the wait. nfsd4_encode_fattr4() can also hold the delegation until the GETATTR reply is encoded.

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, the GETATTR's put can touch a freed nfs4_client. For example, a client that does not reply to the CB_GETATTR can have its lease expire during the wait.

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 while the lease is on the inode. 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 4b28553efc8d..4e7bbaf65050 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 the initial 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)
 {
@@ -10880,7 +10912,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,
@@ -10925,7 +10957,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);
@@ -10966,7 +11001,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
#7 lore raw

PATCH v2 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 and sets ncf_cur_fsize to zero. GETATTR replies then report the file as empty to every client but the delegation holder.

NFSD always requests SIZE in CB_GETATTR, and the holder may have changed the size. Add ncf_cb_bitmap0 to hold word 0 of the reply's bitmap, cleared when the callback is issued. In nfsd4_cb_getattr_done(), treat a reply without SIZE as failed. The waiter then recalls the delegation, as for any other CB_GETATTR failure.

A reply that omits CHANGE is left alone. The zero change attribute does not match, so the waiter errs on the safe side and treats the file as modified.

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 4e7bbaf65050..645ad916d2ef 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 the callback, 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
#8 lore raw

PATCH v2 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 under RFC 9754 the delegation stateid grants the authority to set the times.
- ATTR_DELEG applies to the whole iattr, so __nfsd_setattr() needs special ctime handling for the other attributes.
- If the times are rejected, attrsset can't report the attributes that were set.

Decode time_deleg_access and 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:
- A times-only request skips nfsd_setattr() and the export's read-only and immutable checks that nfsd_setattr() makes, so call fh_verify(NFSD_MAY_SATTR) first.
- Leave ATTR_DELEG clear. nfsd_breaker_owns_lease() keeps the holder's own lease intact, and any other lease is broken.
- Keep vetting against dl_[amc]time. When the vetted mtime is applied but the delegated ctime is rejected, ctime now moves to the current time instead of staying put, because the inode's mtime did change.
- Set dl_setattr only after notify_change() succeeds, so a failed update doesn't skip the restamp on delegation return.

nfsd4_setattr() skips nfsd_setattr() for a request with only delegated times. A request that sets both the plain and the delegated form of a time gets NFS4ERR_INVAL. nfsd4_setattr() checks for that conflict, and checks the stateid, only after the minor version's supported attributes. A minor version without delegated times therefore 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, because nfsd_setattr() can't report a partial update.

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

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

Fixes 7e13f4f8d27d ("nfsd: handle delegated timestamps in SETATTR")
---
 fs/nfsd/nfs4proc.c | 171 ++++++++++++++++++++++++++++++++++++-----------------
 fs/nfsd/nfs4xdr.c  |  54 +++++++++--------
 fs/nfsd/vfs.c      |  16 +----
 fs/nfsd/vfs.h      |   1 +
 fs/nfsd/xdr4.h     |   3 +
 5 files changed, 152 insertions(+), 93 deletions(-)
fs/nfsd/nfs4proc.c +116 -55
index b76320c265e1..1fa68a046fa5 100644
@@ -1361,33 +1361,62 @@ 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;
+	bool deleg_ctime = false;
+	__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);
+	/* nfsd4_setattr() skips nfsd_setattr() 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))
-				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);
+		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;
+				deleg_ctime = true;
+			}
 		}
 	}
+	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)
+		return nfserrno(host_err);
+	if (deleg_ctime)
+		dp->dl_setattr = true;
+	host_err = commit_metadata(fhp);
+	return nfserrno(host_err);
 }
 
 static __be32
@@ -1401,16 +1430,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)) {
+	/* The checks below assume the minor version supports every attribute */
+	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;
 
 		/*
@@ -1431,20 +1478,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 an *_ATTRS_DELEG delegation may set delegated times */
+		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;
 
@@ -1453,36 +1495,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. nfsd_setattr() can't report which parts were
+		 * applied, so attrsset is filled in only on success.
+		 */
+		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 +29 -25

108 lines not on this page · raw message

fs/nfsd/vfs.c +3 -13
index 7bc251d55c3c..a990c76dce0f 100644
@@ -409,7 +409,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);
@@ -507,20 +507,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 d974929398f8..9054f002df35 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
author maintainer
#9 lore raw

PATCH v2 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 atime or a merge of every read holder's times. NFSD supports neither exclusive atime authority nor such a merge.

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 OPEN_DELEGATE_READ_ATTRS_DELEG stay, since open_delegation_type4 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 645ad916d2ef..466416708072 100644
@@ -7329,10 +7329,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)
@@ -7630,9 +7634,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
#10 lore raw

PATCH v2 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. The recall's prepare callback, nfsd4_cb_recall_prepare(), takes deleg_lock and puts the delegation on del_recall_lru only if the delegation is hashed. If the prepare runs before the hash, NFSD grants a delegation that is already recalled but not on del_recall_lru. The laundromat then never revokes the delegation if the client does not return it.

Check fi_had_conflict again under deleg_lock. The break sets fi_had_conflict before queueing the recall. A prepare that ran before the hash therefore left fi_had_conflict set, and a prepare that runs after the hash finds the delegation hashed.

Before 5ea9a7c5fe41, nfs4_set_delegation() made the final check 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 466416708072..9ea3b0e9b7ec 100644
@@ -7428,7 +7428,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 unlocked check
+	 * has queued a recall whose prepare takes deleg_lock. The prepare
+	 * doesn't queue an unhashed delegation for revocation, so the
+	 * laundromat would never revoke a delegation granted after the
+	 * break.
+	 */
+	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
#11 lore raw

PATCH v2 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 between hashing and the stat is not reachable today. A CB_GETATTR sent in that window does not carry a referring call, so the client replies with 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 nfs4_set_delegation().
- Take the grant stat after kernel_setlease() and the conflict checks, just before hashing. Don't grant the delegation if the stat fails.
- The stat can sleep and so 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 that window.
- 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 ncf_initial_cinfo could report a change attribute below a value that clients already saw. Compare with the grant stat's change attribute, which nfs4_set_delegation() returns, rather than with 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 | 93 ++++++++++++++++++++++++++++++++++++-----------------
 1 file changed, 63 insertions(+), 30 deletions(-)
fs/nfsd/nfs4state.c +63 -30
index 9ea3b0e9b7ec..87ec545ebc48 100644
@@ -7277,11 +7277,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;
@@ -7425,6 +7449,25 @@ nfs4_set_delegation(struct nfsd4_open *open,
 	if (fp->fi_had_conflict)
 		goto out_unlock;
 
+	/*
+	 * Anything that can reach the delegation once the delegation is
+	 * hashed may read ncf_initial_cinfo and dl_[amc]time, so set them
+	 * before hashing. nfs4_open_delegation() stats again after hashing
+	 * to catch a write that lands between the two stats.
+	 */
+	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);
@@ -7488,29 +7531,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
@@ -7579,6 +7599,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;
@@ -7613,8 +7634,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;
@@ -7632,12 +7653,24 @@ 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 ncf_initial_cinfo must not be stale.
+		 * Compare with the grant stat's change attribute, not with
+		 * ncf_initial_cinfo. A GETATTR can already have advanced
+		 * ncf_initial_cinfo past the grant stat's value.
+		 */
+		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