Skip to contentSkip to conversation

NFSD: release read access of nfs4_file when a write delegation is returned

PATCH In review Dai Ngo 3 participants 3 messages May 13, 2025 – May 15, 2025

Subsystems (2)
lore
Share & tools
lore

Dai Ngo

author reviewer
#0 lore raw

PATCH 1/1 NFSD: release read access of nfs4_file when a write delegation is returned

When a write delegation is returned, check if read access was added to nfs4_file when client opens file with WRONLY, and release it.

---
 fs/nfsd/nfs4state.c | 9 ++++++++-
 fs/nfsd/state.h     | 1 +
 2 files changed, 9 insertions(+), 1 deletion(-)
fs/nfsd/nfs4state.c +8 -1
index 2bd63594d8da..5e47d9f85ab5 100644
@@ -1207,14 +1207,19 @@ nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
 static void put_deleg_file(struct nfs4_file *fp)
 {
 	struct nfsd_file *nf = NULL;
+	struct nfsd_file *rnf = NULL;
 
 	spin_lock(&fp->fi_lock);
-	if (--fp->fi_delegees == 0)
+	if (--fp->fi_delegees == 0) {
 		swap(nf, fp->fi_deleg_file);
+		swap(rnf, fp->fi_rdeleg_file);
+	}
 	spin_unlock(&fp->fi_lock);
 
 	if (nf)
 		nfsd_file_put(nf);
+	if (rnf)
+		nfs4_file_put_access(fp, NFS4_SHARE_ACCESS_READ);
 }
 
 static void nfs4_unlock_deleg_lease(struct nfs4_delegation *dp)
@@ -4738,6 +4743,7 @@ static void nfsd4_file_init(const struct svc_fh *fh, struct nfs4_file *fp)
 	INIT_LIST_HEAD(&fp->fi_clnt_odstate);
 	fh_copy_shallow(&fp->fi_fhandle, &fh->fh_handle);
 	fp->fi_deleg_file = NULL;
+	fp->fi_rdeleg_file = NULL;
 	fp->fi_had_conflict = false;
 	fp->fi_share_deny = 0;
 	memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
@@ -6171,6 +6177,7 @@ nfsd4_add_rdaccess_to_wrdeleg(struct svc_rqst *rqstp, struct nfsd4_open *open,
 		__nfs4_file_get_access(fp, NFS4_SHARE_ACCESS_READ);
 		fp = stp->st_stid.sc_file;
 		fp->fi_fds[O_RDONLY] = nf;
+		fp->fi_rdeleg_file = nf;
 		spin_unlock(&fp->fi_lock);
 	}
 	return true;
fs/nfsd/state.h +1 -0
index 1995bca158b8..8adc2550129e 100644
@@ -665,6 +665,7 @@ struct nfs4_file {
 	atomic_t		fi_access[2];
 	u32			fi_share_deny;
 	struct nfsd_file	*fi_deleg_file;
+	struct nfsd_file	*fi_rdeleg_file;
 	int			fi_delegees;
 	struct knfsd_fh		fi_fhandle;
 	bool			fi_had_conflict;
Signature · 1 line
2.43.5
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 55 lines · Dai Ngo

When a write delegation is returned, check if read access was added to nfs4_file when client opens file with WRONLY, and release it.

---
 fs/nfsd/nfs4state.c | 9 ++++++++-
 fs/nfsd/state.h     | 1 +
 2 files changed, 9 insertions(+), 1 deletion(-)
fs/nfsd/nfs4state.c +8 -1
index 2bd63594d8da..5e47d9f85ab5 100644
@@ -1207,14 +1207,19 @@ nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
 static void put_deleg_file(struct nfs4_file *fp)
 {
 	struct nfsd_file *nf = NULL;
+	struct nfsd_file *rnf = NULL;
 
 	spin_lock(&fp->fi_lock);
-	if (--fp->fi_delegees == 0)
+	if (--fp->fi_delegees == 0) {
 		swap(nf, fp->fi_deleg_file);
+		swap(rnf, fp->fi_rdeleg_file);
+	}
 	spin_unlock(&fp->fi_lock);
 
 	if (nf)
 		nfsd_file_put(nf);
+	if (rnf)
+		nfs4_file_put_access(fp, NFS4_SHARE_ACCESS_READ);
 }
 
 static void nfs4_unlock_deleg_lease(struct nfs4_delegation *dp)
@@ -4738,6 +4743,7 @@ static void nfsd4_file_init(const struct svc_fh *fh, struct nfs4_file *fp)
 	INIT_LIST_HEAD(&fp->fi_clnt_odstate);
 	fh_copy_shallow(&fp->fi_fhandle, &fh->fh_handle);
 	fp->fi_deleg_file = NULL;
+	fp->fi_rdeleg_file = NULL;
 	fp->fi_had_conflict = false;
 	fp->fi_share_deny = 0;
 	memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
@@ -6171,6 +6177,7 @@ nfsd4_add_rdaccess_to_wrdeleg(struct svc_rqst *rqstp, struct nfsd4_open *open,
 		__nfs4_file_get_access(fp, NFS4_SHARE_ACCESS_READ);
 		fp = stp->st_stid.sc_file;
 		fp->fi_fds[O_RDONLY] = nf;
+		fp->fi_rdeleg_file = nf;
 		spin_unlock(&fp->fi_lock);
 	}
 	return true;
fs/nfsd/state.h +0 -0
index 1995bca158b8..8adc2550129e 100644

fs/nfsd/state.h struct nfs4_file { · line 671

Quoted 7 lines
fs/nfsd/state.h +1 -0
@@ -665,6 +665,7 @@ struct nfs4_file {
 	atomic_t		fi_access[2];
 	u32			fi_share_deny;
 	struct nfsd_file	*fi_deleg_file;
+	struct nfsd_file	*fi_rdeleg_file;
 	int			fi_delegees;
 	struct knfsd_fh		fi_fhandle;
 	bool			fi_had_conflict;
#2 lore raw
In reply to Dai Ngo
Quoted 3 lines · Dai Ngo

When a write delegation is returned, check if read access was added to nfs4_file when client opens file with WRONLY, and release it.

Applied to nfsd-testing, thanks!

The extra nfsd_file pointers in struct nfs4_file are confusing, and have been noted for future clean-up.

[1/1] NFSD: release read access of nfs4_file when a write delegation is returned

      commit: 65de35b2b4b5cff421f48e638010a2f8cecbc62e

--
Chuck Lever <[email protected]>