Skip to contentSkip to conversation

NFSD: Fixes for OPEN4_SHARE_ACCESS_WANT handling

PATCH New Chuck Lever 1 participant 6 messages Oct 8

Subsystems (2)
lore
Share & tools
lore

Chuck Lever

author maintainer
#0 lore raw

cover NFSD: Fixes for OPEN4_SHARE_ACCESS_WANT handling

A client that uses OPEN(CLAIM_FH) with OPEN4_SHARE_ACCESS_WANT_READ_DELEG to regain a delegation on a file the client already has open gets no delegation from NFSD (https://github.com/linux-nfs/nfsd/issues/156).
The report carries no ond_why value, and currently NFSD does not supply a useful one. nfs4_open_delegation() discards the errno from nfs4_set_delegation(), so every refusal reports WND4_RESOURCE, and the WANT_CANCEL and upgrade/downgrade cases in the reply code are unreachable.

The -EAGAIN return of nfs4_set_delegation() is narrowed to mean contention before the recall Bloom filter is mapped onto it (patches 2 and 3). Patch 3 has no effect without patch 2, so patch 3 carries no Fixes: tag. The last patch changes what NFSD grants rather than what it reports, matching what the FreeBSD server grants.

After this series, NFSD still reports WND4_RESOURCE while in grace, while the backchannel is down, and for a want of the delegation type the client already holds, for which RFC 8881 defines no ond_why value. NFSD also still grants no delegation on a file after a recall for as long as any client keeps the file open, now reported as WND4_CONTENTION. Whether the reporter's client hit that policy is unknown. Relaxing the policy and/or implementing access mode upgrade and downgrade is left as future work.

The pynfs NFSv4.1 "deleg" tests pass 24 or 24, including the tests that exercise ond_why and WANT_READ_DELEG on a read-write OPEN. The NFSv4.0 delegations and writedelegations flags were run without the five tests that need a manual operation on the
server. DELEG22 fails and the seven write-delegation tests warn because NFSD grants no delegation to an NFSv4.0 client that opens for write. DELEG6 and REBT8 fail in code this series does not change.

---
Chuck Lever (5):
NFSD: Report WND4_CANCELLED for an OPEN with WANT_CANCEL
NFSD: Report the correct value in an OPEN response's ond_why field
NFSD: Report WND4_CONTENTION for a recently recalled file
NFSD: Report unsupported delegation upgrade and downgrade
NFSD: Honor OPEN4_SHARE_ACCESS_WANT_READ_DELEG on a read-write OPEN

 fs/nfsd/nfs4state.c | 128 +++++++++++++++++++++++++++++-----------------------
 1 file changed, 71 insertions(+), 57 deletions(-)

---
base-commit: 97bf905e837e345107c2ed3efb2e65108e081e0e
change-id: 20261007-want-read-3a08fd18881c

Best regards,

Signature · 1 line
Chuck Lever <[email protected]>
author maintainer
#1 lore raw

PATCH 1/5 NFSD: Report WND4_CANCELLED for an OPEN with WANT_CANCEL

An NFSv4.1 OPEN that sets OPEN4_SHARE_ACCESS_WANT_CANCEL gets OPEN_DELEGATE_NONE_EXT with ond_why set to WND4_NOT_WANTED. RFC 8881 Section 18.16.3 defines WND4_CANCELLED for that request. nfsd4_process_open2() tests op_deleg_want with a bitwise AND against OPEN4_SHARE_ACCESS_WANT_NO_DELEG (0x0400), and OPEN4_SHARE_ACCESS_WANT_CANCEL (0x0500) has that bit set.

Compare the want type by value and report each reason for its own request. An OPEN with either want type never reaches nfsd4_open_deleg_none_ext(), so move the WANT_CANCEL case there under the existing WARN_ON_ONCE().

Fixes d24433cdc91c ("nfsd41: implement NFS4_SHARE_WANT_NO_DELEG, NFS4_OPEN_DELEGATE_NONE_EXT, why_no_deleg")
---
 fs/nfsd/nfs4state.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
fs/nfsd/nfs4state.c +6 -3
index 9d00a58a334d..5075baac7586 100644
@@ -7170,8 +7170,6 @@ static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
 		case OPEN4_SHARE_ACCESS_WANT_ANY_DELEG:
 			break;
 		case OPEN4_SHARE_ACCESS_WANT_CANCEL:
-			open->op_why_no_deleg = WND4_CANCELLED;
-			break;
 		case OPEN4_SHARE_ACCESS_WANT_NO_DELEG:
 			WARN_ON_ONCE(1);
 		}
@@ -7474,10 +7472,15 @@ nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nf
 	mutex_unlock(&stp->st_mutex);
 
 	if (nfsd4_has_session(&resp->cstate)) {
-		if (open->op_deleg_want & OPEN4_SHARE_ACCESS_WANT_NO_DELEG) {
+		switch (open->op_deleg_want & NFS4_SHARE_WANT_TYPE_MASK) {
+		case OPEN4_SHARE_ACCESS_WANT_NO_DELEG:
 			open->op_delegate_type = OPEN_DELEGATE_NONE_EXT;
 			open->op_why_no_deleg = WND4_NOT_WANTED;
 			goto nodeleg;
+		case OPEN4_SHARE_ACCESS_WANT_CANCEL:
+			open->op_delegate_type = OPEN_DELEGATE_NONE_EXT;
+			open->op_why_no_deleg = WND4_CANCELLED;
+			goto nodeleg;
 		}
 	}
 
Signature · 1 line
2.55.0
author maintainer
#2 lore raw

PATCH 2/5 NFSD: Report the correct value in an OPEN response's ond_why field

In reply to Chuck Lever

Currently, when an NFS client sends an OPEN of a file with an OPEN4_SHARE_ACCESS_WANT* bit set and NFSD grants no delegation, NFSD sets ond_why to WND4_RESOURCE, even when a conflicting open or an earlier recall on that file is the reason. NFSD reports WND4_RESOURCE whether NFSD has hit its maximum delegation count or a second NFS client has that file open.

When the server grants no delegation, the ond_why value is how a client chooses between asking again for a delegation on that file versus backing off.

Fixes 0b26693c56cc ("nfsd: clean up nfs4_set_delegation")
---
 fs/nfsd/nfs4state.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)
fs/nfsd/nfs4state.c +11 -9
index 5075baac7586..46e7343e050a 100644
@@ -1511,7 +1511,7 @@ nfs4_delegation_exists(struct nfs4_client *clp, struct nfs4_file *fp)
  * Return:
  *      On success: NULL if the delegation was successfully hashed.
  *
- *      On error: -EAGAIN if one was previously granted to this
+ *      On error: -EEXIST if one was previously granted to this
  *                 nfs4_client for this nfs4_file. Delegation is not hashed.
  *
  */
@@ -1527,7 +1527,7 @@ hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
 	lockdep_assert_held(&clp->cl_lock);
 
 	if (nfs4_delegation_exists(clp, fp))
-		return -EAGAIN;
+		return -EEXIST;
 	refcount_inc(&dp->dl_stid.sc_count);
 	dp->dl_stid.sc_type = SC_TYPE_DELEG;
 	list_add(&dp->dl_perfile, &fp->fi_delegations);
@@ -6944,7 +6944,7 @@ nfsd4_verify_deleg_dentry(struct nfsd4_open *open, struct nfs4_file *fp,
 				 &exp, &child);
 
 	if (err)
-		return -EAGAIN;
+		return -ESTALE;
 
 	exp_put(exp);
 	dput(child);
@@ -6967,7 +6967,7 @@ nfsd4_verify_setuid_write(struct nfsd4_open *open, struct nfsd_file *nf)
 
 	if ((open->op_share_access & NFS4_SHARE_ACCESS_WRITE) &&
 	    (inode->i_mode & (S_ISUID|S_ISGID)))
-		return -EAGAIN;
+		return -EPERM;
 	return 0;
 }
 
@@ -7053,7 +7053,7 @@ nfs4_set_delegation(struct nfsd4_open *open,
 	}
 
 	if (!nf)
-		return ERR_PTR(-EAGAIN);
+		return ERR_PTR(-EBADF);
 
 	/*
 	 * File delegations and associated locks cannot be recovered if the
@@ -7067,9 +7067,9 @@ nfs4_set_delegation(struct nfsd4_open *open,
 	spin_lock(&nn->deleg_lock);
 	spin_lock(&fp->fi_lock);
 	if (nfs4_delegation_exists(clp, fp))
-		status = -EAGAIN;
+		status = -EEXIST;
 	else if (nfsd4_verify_setuid_write(open, nf))
-		status = -EAGAIN;
+		status = -EPERM;
 	else if (!rcu_dereference_protected(fp->fi_deleg_file,
 					    lockdep_is_held(&fp->fi_lock))) {
 		rcu_assign_pointer(fp->fi_deleg_file, nf);
@@ -7298,8 +7298,10 @@ nfs4_open_delegation(struct svc_rqst *rqstp, struct nfsd4_open *open,
 			goto out_no_deleg;
 	}
 	dp = nfs4_set_delegation(open, &resp->cstate, stp, parent);
-	if (IS_ERR(dp))
+	if (IS_ERR(dp)) {
+		status = PTR_ERR(dp);
 		goto out_no_deleg;
+	}
 
 	memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
 
@@ -10687,7 +10689,7 @@ nfsd_get_dir_deleg(struct nfsd4_compound_state *cstate,
 	spin_lock(&fp->fi_lock);
 	/* existing delegation? */
 	if (nfs4_delegation_exists(clp, fp)) {
-		status = -EAGAIN;
+		status = -EEXIST;
 	} else if (!rcu_dereference_protected(fp->fi_deleg_file,
 					      lockdep_is_held(&fp->fi_lock))) {
 		rcu_assign_pointer(fp->fi_deleg_file, nfsd_file_get(nf));
Signature · 1 line
2.55.0
author maintainer
#3 lore raw

PATCH 3/5 NFSD: Report WND4_CONTENTION for a recently recalled file

In reply to Chuck Lever

NFSD grants no delegation on a file for 30 to 60 seconds after recalling one. __alloc_init_deleg() enforces that by returning NULL when delegation_blocked() finds the filehandle, the same result it gives when NFSD is at max_delegations or out of memory. nfs4_set_delegation() reports all three as -ENOMEM, and the reply carries WND4_RESOURCE.

Return ERR_PTR(-EAGAIN) from __alloc_init_deleg() for a blocked filehandle and ERR_PTR(-ENOMEM) for the other failures. Pass that errno up through nfs4_set_delegation() so that nfsd4_open_deleg_none_ext() reports WND4_CONTENTION. alloc_init_dir_deleg() continues to return NULL for every failure, so directory delegations are unaffected.

---
 fs/nfsd/nfs4state.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
fs/nfsd/nfs4state.c +8 -6
index 46e7343e050a..6ed53864c9be 100644
@@ -1263,7 +1263,7 @@ __alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
 	long n;
 
 	if (delegation_blocked(&fp->fi_fhandle))
-		return NULL;
+		return ERR_PTR(-EAGAIN);
 
 	n = atomic_long_inc_return(&num_delegations);
 	atomic_long_inc(&nn->nfsd_delegations);
@@ -1300,7 +1300,7 @@ __alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
 out_dec:
 	atomic_long_dec(&num_delegations);
 	atomic_long_dec(&nn->nfsd_delegations);
-	return NULL;
+	return ERR_PTR(-ENOMEM);
 }
 
 static struct nfs4_delegation *
@@ -1310,8 +1310,8 @@ alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
 	struct nfs4_delegation *dp;
 
 	dp = __alloc_init_deleg(clp, fp, odstate, dl_type, nfs4_free_deleg);
-	if (!dp)
-		return NULL;
+	if (IS_ERR(dp))
+		return dp;
 
 	nfsd4_init_cb(&dp->dl_cb_fattr.ncf_getattr, dp->dl_stid.sc_client,
 			&nfsd4_cb_getattr_ops, NFSPROC4_CLNT_CB_GETATTR);
@@ -1345,7 +1345,7 @@ alloc_init_dir_deleg(struct nfs4_client *clp, struct nfs4_file *fp)
 	int npages;
 
 	dp = __alloc_init_deleg(clp, fp, NULL, NFS4_OPEN_DELEGATE_READ, nfs4_free_dir_deleg);
-	if (!dp)
+	if (IS_ERR(dp))
 		return NULL;
 
 	ncn = &dp->dl_cb_notify;
@@ -7088,8 +7088,10 @@ nfs4_set_delegation(struct nfsd4_open *open,
 
 	status = -ENOMEM;
 	dp = alloc_init_deleg(clp, fp, odstate, dl_type);
-	if (!dp)
+	if (IS_ERR(dp)) {
+		status = PTR_ERR(dp);
 		goto out_delegees;
+	}
 
 	/*
 	 * Record the granting slot before kernel_setlease() makes @dp
Signature · 1 line
2.55.0
author maintainer
#4 lore raw

PATCH 4/5 NFSD: Report unsupported delegation upgrade and downgrade

In reply to Chuck Lever

RFC 8881 Section 18.16.3 defines an OPEN that sets OPEN4_SHARE_ACCESS_WANT_WRITE_DELEG, from a client holding an OPEN_DELEGATE_READ delegation on the file, as a request to upgrade the OPEN_DELEGATE_READ delegation. Likewise, an OPEN that sets OPEN4_SHARE_ACCESS_WANT_READ_DELEG from a client holding an OPEN_DELEGATE_WRITE delegation is a request to downgrade. A server that supports neither upgrade nor downgrade replies with ond_why set to WND4_NOT_SUPP_UPGRADE or WND4_NOT_SUPP_DOWNGRADE.

NFSD supports neither, but replies with WND4_RESOURCE to both requests, inviting the client to ask again, futilely. nfsd4_deleg_xgrade_none_ext() exists to supply the correct reasons, but nfsd4_process_open2() calls it only when the delegation type in the reply is still OPEN_DELEGATE_NONE, and nfs4_open_delegation() has already replaced OPEN_DELEGATE_NONE with OPEN_DELEGATE_NONE_EXT before nfsd4_process_open2() makes the check.

Have nfs4_set_delegation() report the type of the delegation the client already holds, and have nfsd4_open_deleg_none_ext() choose the upgrade or downgrade reason from the held type. Remove the unreachable helper.

Fixes d24433cdc91c ("nfsd41: implement NFS4_SHARE_WANT_NO_DELEG, NFS4_OPEN_DELEGATE_NONE_EXT, why_no_deleg")
---
 fs/nfsd/nfs4state.c | 65 +++++++++++++++++++++++++----------------------------
 1 file changed, 30 insertions(+), 35 deletions(-)
fs/nfsd/nfs4state.c +30 -35
index 6ed53864c9be..6bb70d50e9d9 100644
@@ -1481,10 +1481,12 @@ static void destroy_unhashed_deleg(struct nfs4_delegation *dp)
  * @fp:      a pointer to the nfs4_file we're granting a delegation on
  *
  * Return:
- *      On success: true iff an existing delegation is found
+ *      The delegation @clp already holds on @fp, or NULL if it holds
+ *      none. No reference is taken: the pointer is valid only while
+ *      the caller holds fi_lock.
  */
 
-static bool
+static struct nfs4_delegation *
 nfs4_delegation_exists(struct nfs4_client *clp, struct nfs4_file *fp)
 {
 	struct nfs4_delegation *searchdp = NULL;
@@ -1497,10 +1499,10 @@ nfs4_delegation_exists(struct nfs4_client *clp, struct nfs4_file *fp)
 	list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
 		searchclp = searchdp->dl_stid.sc_client;
 		if (clp == searchclp) {
-			return true;
+			return searchdp;
 		}
 	}
-	return false;
+	return NULL;
 }
 
 /**
@@ -7000,14 +7002,15 @@ nfs4_delegation_record_grant_slot(struct nfs4_delegation *dp,
 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)
+		    struct nfs4_ol_stateid *stp, struct svc_fh *parent,
+		    u32 *held_type)
 {
 	bool deleg_ts = nfsd4_want_deleg_timestamps(open);
 	struct nfs4_client *clp = stp->st_stid.sc_client;
 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
 	struct nfs4_file *fp = stp->st_stid.sc_file;
 	struct nfs4_clnt_odstate *odstate = stp->st_clnt_odstate;
-	struct nfs4_delegation *dp;
+	struct nfs4_delegation *dp, *held;
 	struct nfsd_file *nf = NULL;
 	struct file_lease *fl;
 	int status = 0;
@@ -7066,9 +7069,11 @@ nfs4_set_delegation(struct nfsd4_open *open,
 
 	spin_lock(&nn->deleg_lock);
 	spin_lock(&fp->fi_lock);
-	if (nfs4_delegation_exists(clp, fp))
+	held = nfs4_delegation_exists(clp, fp);
+	if (held) {
+		*held_type = held->dl_type;
 		status = -EEXIST;
-	else if (nfsd4_verify_setuid_write(open, nf))
+	} else if (nfsd4_verify_setuid_write(open, nf))
 		status = -EPERM;
 	else if (!rcu_dereference_protected(fp->fi_deleg_file,
 					    lockdep_is_held(&fp->fi_lock))) {
@@ -7140,6 +7145,10 @@ nfs4_set_delegation(struct nfsd4_open *open,
 	spin_lock(&clp->cl_lock);
 	spin_lock(&fp->fi_lock);
 	status = hash_delegation_locked(dp, fp);
+	if (status == -EEXIST) {
+		held = nfs4_delegation_exists(clp, fp);
+		*held_type = held->dl_type;
+	}
 	spin_unlock(&fp->fi_lock);
 	spin_unlock(&clp->cl_lock);
 	spin_unlock(&nn->deleg_lock);
@@ -7159,16 +7168,23 @@ nfs4_set_delegation(struct nfsd4_open *open,
 	return ERR_PTR(status);
 }
 
-static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
+static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status,
+				      u32 held_type)
 {
 	open->op_delegate_type = OPEN_DELEGATE_NONE_EXT;
 	if (status == -EAGAIN)
 		open->op_why_no_deleg = WND4_CONTENTION;
 	else {
 		open->op_why_no_deleg = WND4_RESOURCE;
-		switch (open->op_deleg_want) {
+		switch (open->op_deleg_want & NFS4_SHARE_WANT_TYPE_MASK) {
 		case OPEN4_SHARE_ACCESS_WANT_READ_DELEG:
+			if (deleg_is_write(held_type))
+				open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
+			break;
 		case OPEN4_SHARE_ACCESS_WANT_WRITE_DELEG:
+			if (deleg_is_read(held_type))
+				open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
+			break;
 		case OPEN4_SHARE_ACCESS_WANT_ANY_DELEG:
 			break;
 		case OPEN4_SHARE_ACCESS_WANT_CANCEL:
@@ -7268,6 +7284,7 @@ nfs4_open_delegation(struct svc_rqst *rqstp, struct nfsd4_open *open,
 	struct nfs4_client *clp = stp->st_stid.sc_client;
 	struct svc_fh *parent = NULL;
 	struct nfs4_delegation *dp;
+	u32 held_type = OPEN_DELEGATE_NONE;
 	struct kstat stat;
 	int status = 0;
 	int cb_up;
@@ -7299,7 +7316,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);
+	dp = nfs4_set_delegation(open, &resp->cstate, stp, parent,
+				 &held_type);
 	if (IS_ERR(dp)) {
 		status = PTR_ERR(dp);
 		goto out_no_deleg;
@@ -7341,28 +7359,10 @@ nfs4_open_delegation(struct svc_rqst *rqstp, struct nfsd4_open *open,
 
 	/* 4.1 client asking for a delegation? */
 	if (open->op_deleg_want)
-		nfsd4_open_deleg_none_ext(open, status);
+		nfsd4_open_deleg_none_ext(open, status, held_type);
 	return;
 }
 
-static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
-					struct nfs4_delegation *dp)
-{
-	if (deleg_is_write(dp->dl_type)) {
-		if (open->op_deleg_want & OPEN4_SHARE_ACCESS_WANT_READ_DELEG) {
-			open->op_delegate_type = OPEN_DELEGATE_NONE_EXT;
-			open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
-		} else if (open->op_deleg_want & OPEN4_SHARE_ACCESS_WANT_WRITE_DELEG) {
-			open->op_delegate_type = OPEN_DELEGATE_NONE_EXT;
-			open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
-		}
-	}
-	/* Otherwise the client must be confused wanting a delegation
-	 * it already has, therefore we don't return
-	 * OPEN_DELEGATE_NONE_EXT and reason.
-	 */
-}
-
 /* Are we returning only a delegation stateid? */
 static bool open_xor_delegation(struct nfsd4_open *open)
 {
@@ -7509,11 +7509,6 @@ nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nf
 	status = nfs_ok;
 	trace_nfsd_open(&stp->st_stid.sc_stateid);
 out:
-	/* 4.1 client trying to upgrade/downgrade delegation? */
-	if (open->op_delegate_type == OPEN_DELEGATE_NONE && dp &&
-	    open->op_deleg_want)
-		nfsd4_deleg_xgrade_none_ext(open, dp);
-
 	if (fp)
 		put_nfs4_file(fp);
 	if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
Signature · 1 line
2.55.0
author maintainer
#5 lore raw

PATCH 5/5 NFSD: Honor OPEN4_SHARE_ACCESS_WANT_READ_DELEG on a read-write OPEN

In reply to Chuck Lever

RFC 8881 Section 18.16.3 defines OPEN4_SHARE_ACCESS_WANT_READ_DELEG as a request for an OPEN_DELEGATE_READ delegation regardless of the access bits in the OPEN. NFSD chooses the delegation type from the access bits alone, so an OPEN for both read and write that sets WANT_READ_DELEG gets a write delegation.

Any other client's open recalls that write delegation, and another client's GETATTR triggers a CB_GETATTR. The read delegation the client asked for survives both. Granting no delegation would also respect the request, but it withholds a delegation NFSD is able to grant.

Try for a read delegation when the OPEN sets WANT_READ_DELEG and includes read access. After this change, a write-only OPEN that sets WANT_READ_DELEG gets no delegation, with ond_why set to WND4_RESOURCE. When the OPEN also sets OPEN4_SHARE_ACCESS_WANT_OPEN_XOR_DELEGATION, return the open stateid along with the read delegation.

---
 fs/nfsd/nfs4state.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)
fs/nfsd/nfs4state.c +16 -4
index 6bb70d50e9d9..920967dd6f84 100644
@@ -6984,6 +6984,12 @@ static bool nfsd4_want_deleg_timestamps(const struct nfsd4_open *open)
 	return open->op_deleg_want & OPEN4_SHARE_ACCESS_WANT_DELEG_TIMESTAMPS;
 }
 
+static bool nfsd4_want_read_deleg(const struct nfsd4_open *open)
+{
+	return (open->op_deleg_want & NFS4_SHARE_WANT_TYPE_MASK) ==
+		OPEN4_SHARE_ACCESS_WANT_READ_DELEG;
+}
+
 static void
 nfs4_delegation_record_grant_slot(struct nfs4_delegation *dp,
 				 const struct nfsd4_compound_state *cstate)
@@ -7041,14 +7047,16 @@ nfs4_set_delegation(struct nfsd4_open *open,
 	 *  We choose to offer a write delegation for OPEN with the
 	 *  OPEN4_SHARE_ACCESS_WRITE access mode to accommodate such clients.
 	 */
-	if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE) {
+	if ((open->op_share_access & NFS4_SHARE_ACCESS_WRITE) &&
+	    !nfsd4_want_read_deleg(open)) {
 		nf = find_writeable_file(fp);
 		dl_type = deleg_ts ? OPEN_DELEGATE_WRITE_ATTRS_DELEG : OPEN_DELEGATE_WRITE;
 	}
 
 	/*
-	 * If the file is being opened O_RDONLY or we couldn't get a O_RDWR
-	 * file for some reason, then try for a read delegation instead.
+	 * 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.
 	 */
 	if (!nf && (open->op_share_access & NFS4_SHARE_ACCESS_READ)) {
 		nf = find_readable_file(fp);
@@ -7325,7 +7333,7 @@ nfs4_open_delegation(struct svc_rqst *rqstp, struct nfsd4_open *open,
 
 	memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
 
-	if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE) {
+	if (deleg_is_write(dp->dl_type)) {
 		struct file *f;
 
 		f = rcu_dereference_protected(dp->dl_stid.sc_file->fi_deleg_file, 1)->nf_file;
@@ -7371,6 +7379,10 @@ static bool open_xor_delegation(struct nfsd4_open *open)
 	/* Did we actually get a delegation? */
 	if (!deleg_is_read(open->op_delegate_type) && !deleg_is_write(open->op_delegate_type))
 		return false;
+	/* A read delegation stateid does not permit WRITE. */
+	if (deleg_is_read(open->op_delegate_type) &&
+	    (open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
+		return false;
 	return true;
 }
 
Signature · 1 line
2.55.0