Skip to contentSkip to conversation

NFSD: add support for NFSv4 write delegation

PATCH Superseded Dai Ngo 3 participants 10 messages May 17, 2023 – May 18, 2023

Subsystems (4)

v3 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Dai Ngo

author reviewer
#0 lore raw

v3 cover NFSD: add support for NFSv4 write delegation

NFSD: add support for NFSv4 write delegation

The NFSv4 server currently supports read delegation using VFS lease which is implemented using file_lock.

This patch series add write delegation support for NFSv4 server by:

. remove the check for F_WRLCK in generic_add_lease to allow
file_lock to be used for write delegation.

. grant write delegation for OPEN with NFS4_SHARE_ACCESS_WRITE
if there is no conflict with other OPENs.

Write delegation conflict with another OPEN, REMOVE, RENAME and SETATTR are handled the same as read delegation using notify_change, try_break_deleg.

Changes since v1:

[PATCH 3/4] NFSD: add supports for CB_GETATTR callback
- remove WARN_ON_ONCE from encode_bitmap4
- replace decode_bitmap4 with xdr_stream_decode_uint32_array
- replace xdr_inline_decode and xdr_decode_hyper in decode_cb_getattr with xdr_stream_decode_u64. Also remove the un-needed likely().
- modify signature of encode_cb_getattr4args to take pointer to nfs4_cb_fattr
- replace decode_attr_length with xdr_stream_decode_u32
- rename decode_cb_getattr to decode_cb_fattr4
- fold the initialization of cb_cinfo and cb_fsize into decode_cb_fattr4
- rename ncf_cb_cinfo to ncf_cb_change to avoid confusion of cindo usage in fs/nfsd/nfs4xdr.c
- correct NFS4_dec_cb_getattr_sz and update size description

[PATCH 4/4] NFSD: handle GETATTR conflict with write delegation
- change nfs4_handle_wrdeleg_conflict returns __be32 to fix test robot
- change ncf_cb_cinfo to ncf_cb_change to avoid confusion of cindo usage in fs/nfsd/nfs4xdr.c

Changes since v2:

[PATCH 2/4] NFSD: enable support for write delegation
- rename 'deleg' to 'dl_type' in nfs4_set_delegation
- remove 'wdeleg' in nfs4_open_delegation

- drop [PATCH 3/4] NFSD: add supports for CB_GETATTR callback and [PATCH 4/4] NFSD: handle GETATTR conflict with write delegation for futher clarification of the benefits of using CB_GETATTR for handling GETATTR from the 2nd client

author reviewer
#1 lore raw

PATCH v3 1/2 locks: allow support for write delegation

Remove the check for F_WRLCK in generic_add_lease to allow file_lock to be used for write delegation.

First consumer is NFSD.

---
 fs/locks.c | 7 -------
 1 file changed, 7 deletions(-)
fs/locks.c +0 -7
index df8b26a42524..08fb0b4fd4f8 100644
@@ -1729,13 +1729,6 @@ generic_add_lease(struct file *filp, long arg, struct file_lock **flp, void **pr
 	if (is_deleg && !inode_trylock(inode))
 		return -EAGAIN;
 
-	if (is_deleg && arg == F_WRLCK) {
-		/* Write delegations are not currently supported: */
-		inode_unlock(inode);
-		WARN_ON_ONCE(1);
-		return -EINVAL;
-	}
-
 	percpu_down_read(&file_rwsem);
 	spin_lock(&ctx->flc_lock);
 	time_out_leases(inode, &dispose);
Signature · 1 line
2.9.5
author reviewer
#2 lore raw

PATCH v3 2/2 NFSD: enable support for write delegation

In reply to Dai Ngo

This patch grants write delegation for OPEN with NFS4_SHARE_ACCESS_WRITE if there is no conflict with other OPENs.

Write delegation conflict with another OPEN, REMOVE, RENAME and SETATTR are handled the same as read delegation using notify_change, try_break_deleg.

---
 fs/nfsd/nfs4state.c | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)
fs/nfsd/nfs4state.c +16 -8
index 6e61fa3acaf1..09a9e16407f9 100644
@@ -1144,7 +1144,7 @@ static void block_delegations(struct knfsd_fh *fh)
 
 static struct nfs4_delegation *
 alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
-		 struct nfs4_clnt_odstate *odstate)
+		struct nfs4_clnt_odstate *odstate, u32 dl_type)
 {
 	struct nfs4_delegation *dp;
 	long n;
@@ -1170,7 +1170,7 @@ alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
 	INIT_LIST_HEAD(&dp->dl_recall_lru);
 	dp->dl_clnt_odstate = odstate;
 	get_clnt_odstate(odstate);
-	dp->dl_type = NFS4_OPEN_DELEGATE_READ;
+	dp->dl_type = dl_type;
 	dp->dl_retries = 1;
 	dp->dl_recalled = false;
 	nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
@@ -5451,6 +5451,7 @@ nfs4_set_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp,
 	struct nfs4_delegation *dp;
 	struct nfsd_file *nf;
 	struct file_lock *fl;
+	u32 deleg;
 
 	/*
 	 * The fi_had_conflict and nfs_get_existing_delegation checks
@@ -5460,7 +5461,13 @@ nfs4_set_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp,
 	if (fp->fi_had_conflict)
 		return ERR_PTR(-EAGAIN);
 
-	nf = find_readable_file(fp);
+	if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE) {
+		nf = find_writeable_file(fp);
+		deleg = NFS4_OPEN_DELEGATE_WRITE;
+	} else {
+		nf = find_readable_file(fp);
+		deleg = NFS4_OPEN_DELEGATE_READ;
+	}
 	if (!nf) {
 		/*
 		 * We probably could attempt another open and get a read
@@ -5491,11 +5498,11 @@ nfs4_set_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp,
 		return ERR_PTR(status);
 
 	status = -ENOMEM;
-	dp = alloc_init_deleg(clp, fp, odstate);
+	dp = alloc_init_deleg(clp, fp, odstate, deleg);
 	if (!dp)
 		goto out_delegees;
 
-	fl = nfs4_alloc_init_lease(dp, NFS4_OPEN_DELEGATE_READ);
+	fl = nfs4_alloc_init_lease(dp, deleg);
 	if (!fl)
 		goto out_clnt_odstate;
 
@@ -5583,6 +5590,7 @@ nfs4_open_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp,
 	struct svc_fh *parent = NULL;
 	int cb_up;
 	int status = 0;
+	u32 wdeleg = false;
 
 	cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
 	open->op_recall = 0;
@@ -5590,8 +5598,6 @@ nfs4_open_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp,
 		case NFS4_OPEN_CLAIM_PREVIOUS:
 			if (!cb_up)
 				open->op_recall = 1;
-			if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
-				goto out_no_deleg;
 			break;
 		case NFS4_OPEN_CLAIM_NULL:
 			parent = currentfh;
@@ -5617,7 +5623,9 @@ nfs4_open_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp,
 	memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
 
 	trace_nfsd_deleg_read(&dp->dl_stid.sc_stateid);
-	open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
+	wdeleg = open->op_share_access & NFS4_SHARE_ACCESS_WRITE;
+	open->op_delegate_type = wdeleg ?
+			NFS4_OPEN_DELEGATE_WRITE : NFS4_OPEN_DELEGATE_READ;
 	nfs4_put_stid(&dp->dl_stid);
 	return;
 out_no_deleg:
Signature · 1 line
2.9.5
#3 lore raw

Re: [PATCH v3 2/2] NFSD: enable support for write delegation

Quoted 10 lines · Dai Ngo

This patch grants write delegation for OPEN with NFS4_SHARE_ACCESS_WRITE if there is no conflict with other OPENs.

Write delegation conflict with another OPEN, REMOVE, RENAME and SETATTR are handled the same as read delegation using notify_change, try_break_deleg.

Very clean. A couple of suggestions, one is down below, and here is the other:

I was thinking we should add one or two counters in fs/nfsd/stats.c to track how often read and write delegations are offered, and perhaps one to count the number of DELEGRETURN operations. What do you think makes sense?

Quoted 82 lines

---
fs/nfsd/nfs4state.c | 24 ++++++++++++++++--------

1 file changed, 16 insertions(+), 8 deletions(-)
fs/nfsd/nfs4state.c +0 -0
index 6e61fa3acaf1..09a9e16407f9 100644
@@ -1144,7 +1144,7 @@ static void block_delegations(struct knfsd_fh *fh)
 

static struct nfs4_delegation * alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
- struct nfs4_clnt_odstate *odstate)
+ struct nfs4_clnt_odstate *odstate, u32 dl_type)
{
struct nfs4_delegation *dp;
long n;

fs/nfsd/nfs4state.c +12 -4
@@ -1170,7 +1170,7 @@ alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
 INIT_LIST_HEAD(&dp->dl_recall_lru);
 dp->dl_clnt_odstate = odstate;
 get_clnt_odstate(odstate);
- dp->dl_type = NFS4_OPEN_DELEGATE_READ;
+ dp->dl_type = dl_type;
 dp->dl_retries = 1;
 dp->dl_recalled = false;
 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
@@ -5451,6 +5451,7 @@ nfs4_set_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp,
 struct nfs4_delegation *dp;
 struct nfsd_file *nf;
 struct file_lock *fl;
+ u32 deleg;
 
 /*
 * The fi_had_conflict and nfs_get_existing_delegation checks
@@ -5460,7 +5461,13 @@ nfs4_set_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp,
 if (fp->fi_had_conflict)
 return ERR_PTR(-EAGAIN);
 
- nf = find_readable_file(fp);
+ if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE) {
+ nf = find_writeable_file(fp);
+ deleg = NFS4_OPEN_DELEGATE_WRITE;
+ } else {
+ nf = find_readable_file(fp);
+ deleg = NFS4_OPEN_DELEGATE_READ;
+ }
 if (!nf) {
 /*
 * We probably could attempt another open and get a read
@@ -5491,11 +5498,11 @@ nfs4_set_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp,
 return ERR_PTR(status);
 
 status = -ENOMEM;
- dp = alloc_init_deleg(clp, fp, odstate);
+ dp = alloc_init_deleg(clp, fp, odstate, deleg);
 if (!dp)
 goto out_delegees;
 
- fl = nfs4_alloc_init_lease(dp, NFS4_OPEN_DELEGATE_READ);
+ fl = nfs4_alloc_init_lease(dp, deleg);
 if (!fl)
 goto out_clnt_odstate;
 
@@ -5583,6 +5590,7 @@ nfs4_open_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp,
 struct svc_fh *parent = NULL;
 int cb_up;
 int status = 0;
+ u32 wdeleg = false;
 
 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
 open->op_recall = 0;
@@ -5590,8 +5598,6 @@ nfs4_open_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp,

case NFS4_OPEN_CLAIM_PREVIOUS:
if (!cb_up)
open->op_recall = 1;
- if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
- goto out_no_deleg;
break;
case NFS4_OPEN_CLAIM_NULL:
parent = currentfh;

fs/nfsd/nfs4state.c nfs4_open_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp, · line 5625

Quoted 3 lines
fs/nfsd/nfs4state.c +0 -0
@@ -5617,7 +5623,9 @@ nfs4_open_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp,
 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
 
 trace_nfsd_deleg_read(&dp->dl_stid.sc_stateid);

I'd like you to add a trace_nfsd_deleg_write(), and invoke it here instead of trace_nfsd_deleg_read when NFSD hands out a write delegation.

Quoted 10 lines
fs/nfsd/nfs4state.c +3 -1
- open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
+ wdeleg = open->op_share_access & NFS4_SHARE_ACCESS_WRITE;
+ open->op_delegate_type = wdeleg ?
+ NFS4_OPEN_DELEGATE_WRITE : NFS4_OPEN_DELEGATE_READ;
 nfs4_put_stid(&dp->dl_stid);
 return;

out_no_deleg:

Signature · 1 line
2.9.5

--
Chuck Lever

author reviewer
#4 lore raw

Re: [PATCH v3 2/2] NFSD: enable support for write delegation

Quoted 9 lines · Chuck Lever III
Quoted 9 lines · Dai Ngo

This patch grants write delegation for OPEN with NFS4_SHARE_ACCESS_WRITE if there is no conflict with other OPENs.

Write delegation conflict with another OPEN, REMOVE, RENAME and SETATTR are handled the same as read delegation using notify_change, try_break_deleg.

Quoted 7 lines

Very clean. A couple of suggestions, one is down below, and here is the other:

I was thinking we should add one or two counters in fs/nfsd/stats.c to track how often read and write delegations are offered, and perhaps one to count the number of DELEGRETURN operations. What do you think makes sense?

I'm not sure what these counters will tell us, currently we already has a counter for number of delegations handed out. I think a counter on how often nfsd has to recall the write delegation due to GETATTR can be useful to know whether we should implement CB_GETATTR.

Quoted 86 lines
Quoted 86 lines

---
fs/nfsd/nfs4state.c | 24 ++++++++++++++++--------

1 file changed, 16 insertions(+), 8 deletions(-)
fs/nfsd/nfs4state.c +0 -0
index 6e61fa3acaf1..09a9e16407f9 100644
@@ -1144,7 +1144,7 @@ static void block_delegations(struct knfsd_fh *fh)
 

static struct nfs4_delegation * alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
- struct nfs4_clnt_odstate *odstate)
+ struct nfs4_clnt_odstate *odstate, u32 dl_type)
{
struct nfs4_delegation *dp;
long n;

fs/nfsd/nfs4state.c +12 -4
@@ -1170,7 +1170,7 @@ alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
 INIT_LIST_HEAD(&dp->dl_recall_lru);
 dp->dl_clnt_odstate = odstate;
 get_clnt_odstate(odstate);
- dp->dl_type = NFS4_OPEN_DELEGATE_READ;
+ dp->dl_type = dl_type;
 dp->dl_retries = 1;
 dp->dl_recalled = false;
 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
@@ -5451,6 +5451,7 @@ nfs4_set_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp,
 struct nfs4_delegation *dp;
 struct nfsd_file *nf;
 struct file_lock *fl;
+ u32 deleg;
 
 /*
 * The fi_had_conflict and nfs_get_existing_delegation checks
@@ -5460,7 +5461,13 @@ nfs4_set_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp,
 if (fp->fi_had_conflict)
 return ERR_PTR(-EAGAIN);
 
- nf = find_readable_file(fp);
+ if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE) {
+ nf = find_writeable_file(fp);
+ deleg = NFS4_OPEN_DELEGATE_WRITE;
+ } else {
+ nf = find_readable_file(fp);
+ deleg = NFS4_OPEN_DELEGATE_READ;
+ }
 if (!nf) {
 /*
 * We probably could attempt another open and get a read
@@ -5491,11 +5498,11 @@ nfs4_set_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp,
 return ERR_PTR(status);
 
 status = -ENOMEM;
- dp = alloc_init_deleg(clp, fp, odstate);
+ dp = alloc_init_deleg(clp, fp, odstate, deleg);
 if (!dp)
 goto out_delegees;
 
- fl = nfs4_alloc_init_lease(dp, NFS4_OPEN_DELEGATE_READ);
+ fl = nfs4_alloc_init_lease(dp, deleg);
 if (!fl)
 goto out_clnt_odstate;
 
@@ -5583,6 +5590,7 @@ nfs4_open_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp,
 struct svc_fh *parent = NULL;
 int cb_up;
 int status = 0;
+ u32 wdeleg = false;
 
 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
 open->op_recall = 0;
@@ -5590,8 +5598,6 @@ nfs4_open_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp,

case NFS4_OPEN_CLAIM_PREVIOUS:
if (!cb_up)
open->op_recall = 1;
- if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
- goto out_no_deleg;
break;
case NFS4_OPEN_CLAIM_NULL:
parent = currentfh;

fs/nfsd/nfs4state.c +0 -0
@@ -5617,7 +5623,9 @@ nfs4_open_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp,
 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
 
Quoted 4 lines

fs/nfsd/nfs4state.c nfs4_open_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp, · line 5625

Quoted 1 line
fs/nfsd/nfs4state.c +0 -0
 trace_nfsd_deleg_read(&dp->dl_stid.sc_stateid);

I'd like you to add a trace_nfsd_deleg_write(), and invoke it here instead of trace_nfsd_deleg_read when NFSD hands out a write delegation.

Fix in v4.

-Dai

Quoted 16 lines
Quoted 8 lines
fs/nfsd/nfs4state.c +3 -1
- open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
+ wdeleg = open->op_share_access & NFS4_SHARE_ACCESS_WRITE;
+ open->op_delegate_type = wdeleg ?
+ NFS4_OPEN_DELEGATE_WRITE : NFS4_OPEN_DELEGATE_READ;
 nfs4_put_stid(&dp->dl_stid);
 return;
Quoted 4 lines

out_no_deleg:

Signature · 1 line
2.9.5

--
Chuck Lever

#5 lore raw

Re: [PATCH v3 2/2] NFSD: enable support for write delegation

Quoted 18 lines · Dai Ngo
Quoted 12 lines · Chuck Lever III
Quoted 9 lines · Dai Ngo

This patch grants write delegation for OPEN with NFS4_SHARE_ACCESS_WRITE if there is no conflict with other OPENs.

Write delegation conflict with another OPEN, REMOVE, RENAME and SETATTR are handled the same as read delegation using notify_change, try_break_deleg.

Very clean. A couple of suggestions, one is down below, and here is the other:

Quoted 7 lines
Quoted 4 lines

I was thinking we should add one or two counters in fs/nfsd/stats.c to track how often read and write delegations are offered, and perhaps one to count the number of DELEGRETURN operations. What do you think makes sense?

I'm not sure what these counters will tell us, currently we already has a counter for number of delegations handed out.

I haven't found that, where is it? Certainly, if NFSD already has one, then no need to add more.

It would be nice one day, perhaps, to have a metric of how many delegations a client holds. That's not for this series.

Quoted 3 lines

I think a counter
on how often nfsd has to recall the write delegation due to GETATTR can be useful to know whether we should implement CB_GETATTR.

I hesitated to mention that because I wonder if that's something that would be interesting only for defending a design choice, not for site-to-site tuning. In other words, after we plumb it into NFSD, it will never actually be used after CB_GETATTR support is added.

Do you believe it's something that administrators can use to help balance or tune their workloads?

Quoted 84 lines
Quoted 84 lines
Quoted 84 lines

---
fs/nfsd/nfs4state.c | 24 ++++++++++++++++--------

1 file changed, 16 insertions(+), 8 deletions(-)
fs/nfsd/nfs4state.c +0 -0
index 6e61fa3acaf1..09a9e16407f9 100644
@@ -1144,7 +1144,7 @@ static void block_delegations(struct knfsd_fh *fh)
 

static struct nfs4_delegation * alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
- struct nfs4_clnt_odstate *odstate)
+ struct nfs4_clnt_odstate *odstate, u32 dl_type)
{
struct nfs4_delegation *dp;
long n;

fs/nfsd/nfs4state.c +12 -4
@@ -1170,7 +1170,7 @@ alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
 INIT_LIST_HEAD(&dp->dl_recall_lru);
 dp->dl_clnt_odstate = odstate;
 get_clnt_odstate(odstate);
- dp->dl_type = NFS4_OPEN_DELEGATE_READ;
+ dp->dl_type = dl_type;
 dp->dl_retries = 1;
 dp->dl_recalled = false;
 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
@@ -5451,6 +5451,7 @@ nfs4_set_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp,
 struct nfs4_delegation *dp;
 struct nfsd_file *nf;
 struct file_lock *fl;
+ u32 deleg;
 
 /*
 * The fi_had_conflict and nfs_get_existing_delegation checks
@@ -5460,7 +5461,13 @@ nfs4_set_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp,
 if (fp->fi_had_conflict)
 return ERR_PTR(-EAGAIN);
 
- nf = find_readable_file(fp);
+ if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE) {
+ nf = find_writeable_file(fp);
+ deleg = NFS4_OPEN_DELEGATE_WRITE;
+ } else {
+ nf = find_readable_file(fp);
+ deleg = NFS4_OPEN_DELEGATE_READ;
+ }
 if (!nf) {
 /*
 * We probably could attempt another open and get a read
@@ -5491,11 +5498,11 @@ nfs4_set_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp,
 return ERR_PTR(status);
 
 status = -ENOMEM;
- dp = alloc_init_deleg(clp, fp, odstate);
+ dp = alloc_init_deleg(clp, fp, odstate, deleg);
 if (!dp)
 goto out_delegees;
 
- fl = nfs4_alloc_init_lease(dp, NFS4_OPEN_DELEGATE_READ);
+ fl = nfs4_alloc_init_lease(dp, deleg);
 if (!fl)
 goto out_clnt_odstate;
 
@@ -5583,6 +5590,7 @@ nfs4_open_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp,
 struct svc_fh *parent = NULL;
 int cb_up;
 int status = 0;
+ u32 wdeleg = false;
 
 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
 open->op_recall = 0;
@@ -5590,8 +5598,6 @@ nfs4_open_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp,

case NFS4_OPEN_CLAIM_PREVIOUS:
if (!cb_up)
open->op_recall = 1;
- if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
- goto out_no_deleg;
break;
case NFS4_OPEN_CLAIM_NULL:
parent = currentfh;

fs/nfsd/nfs4state.c +0 -0
@@ -5617,7 +5623,9 @@ nfs4_open_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp,
 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
 
Quoted 23 lines
Quoted 4 lines

fs/nfsd/nfs4state.c nfs4_open_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp, · line 5625

Quoted 1 line
fs/nfsd/nfs4state.c +0 -0
 trace_nfsd_deleg_read(&dp->dl_stid.sc_stateid);

I'd like you to add a trace_nfsd_deleg_write(), and invoke it here instead of trace_nfsd_deleg_read when NFSD hands out a write delegation.

Fix in v4.

-Dai

Quoted 14 lines
Quoted 8 lines
fs/nfsd/nfs4state.c +3 -1
- open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
+ wdeleg = open->op_share_access & NFS4_SHARE_ACCESS_WRITE;
+ open->op_delegate_type = wdeleg ?
+ NFS4_OPEN_DELEGATE_WRITE : NFS4_OPEN_DELEGATE_READ;
 nfs4_put_stid(&dp->dl_stid);
 return;
Quoted 4 lines

out_no_deleg:

Signature · 1 line
2.9.5

--
Chuck Lever

--
Chuck Lever

author reviewer
#6 lore raw

Re: [PATCH v3 2/2] NFSD: enable support for write delegation

Quoted 16 lines · Chuck Lever III
Quoted 16 lines · Dai Ngo
Quoted 11 lines · Chuck Lever III
Quoted 8 lines · Dai Ngo

This patch grants write delegation for OPEN with NFS4_SHARE_ACCESS_WRITE if there is no conflict with other OPENs.

Write delegation conflict with another OPEN, REMOVE, RENAME and SETATTR are handled the same as read delegation using notify_change, try_break_deleg.

Very clean. A couple of suggestions, one is down below, and here is the other:

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

I was thinking we should add one or two counters in fs/nfsd/stats.c to track how often read and write delegations are offered, and perhaps one to count the number of DELEGRETURN operations. What do you think makes sense?

I'm not sure what these counters will tell us, currently we already has a counter for number of delegations handed out.

I haven't found that, where is it? Certainly, if NFSD already has one, then no need to add more.

num_delegations in nfs4state.c

Quoted 3 lines

It would be nice one day, perhaps, to have a metric of how many delegations a client holds. That's not for this series.

okay.

Quoted 5 lines
Quoted 5 lines

I think a counter
on how often nfsd has to recall the write delegation due to GETATTR can be useful to know whether we should implement CB_GETATTR.

Quoted 8 lines

I hesitated to mention that because I wonder if that's something that would be interesting only for defending a design choice, not for site-to-site tuning. In other words, after we plumb it into NFSD, it will never actually be used after CB_GETATTR support is added.

Do you believe it's something that administrators can use to help balance or tune their workloads?

You're right. That is just for ourselves to determine if CB_GETATTR is needed.

-Dai

Quoted 112 lines
Quoted 86 lines
Quoted 86 lines
Quoted 86 lines

---
fs/nfsd/nfs4state.c | 24 ++++++++++++++++--------

1 file changed, 16 insertions(+), 8 deletions(-)
fs/nfsd/nfs4state.c +0 -0
index 6e61fa3acaf1..09a9e16407f9 100644
@@ -1144,7 +1144,7 @@ static void block_delegations(struct knfsd_fh *fh)
 

static struct nfs4_delegation * alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
- struct nfs4_clnt_odstate *odstate)
+ struct nfs4_clnt_odstate *odstate, u32 dl_type)
{
struct nfs4_delegation *dp;
long n;

fs/nfsd/nfs4state.c +12 -4
@@ -1170,7 +1170,7 @@ alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
 INIT_LIST_HEAD(&dp->dl_recall_lru);
 dp->dl_clnt_odstate = odstate;
 get_clnt_odstate(odstate);
- dp->dl_type = NFS4_OPEN_DELEGATE_READ;
+ dp->dl_type = dl_type;
 dp->dl_retries = 1;
 dp->dl_recalled = false;
 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
@@ -5451,6 +5451,7 @@ nfs4_set_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp,
 struct nfs4_delegation *dp;
 struct nfsd_file *nf;
 struct file_lock *fl;
+ u32 deleg;
 
 /*
 * The fi_had_conflict and nfs_get_existing_delegation checks
@@ -5460,7 +5461,13 @@ nfs4_set_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp,
 if (fp->fi_had_conflict)
 return ERR_PTR(-EAGAIN);
 
- nf = find_readable_file(fp);
+ if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE) {
+ nf = find_writeable_file(fp);
+ deleg = NFS4_OPEN_DELEGATE_WRITE;
+ } else {
+ nf = find_readable_file(fp);
+ deleg = NFS4_OPEN_DELEGATE_READ;
+ }
 if (!nf) {
 /*
 * We probably could attempt another open and get a read
@@ -5491,11 +5498,11 @@ nfs4_set_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp,
 return ERR_PTR(status);
 
 status = -ENOMEM;
- dp = alloc_init_deleg(clp, fp, odstate);
+ dp = alloc_init_deleg(clp, fp, odstate, deleg);
 if (!dp)
 goto out_delegees;
 
- fl = nfs4_alloc_init_lease(dp, NFS4_OPEN_DELEGATE_READ);
+ fl = nfs4_alloc_init_lease(dp, deleg);
 if (!fl)
 goto out_clnt_odstate;
 
@@ -5583,6 +5590,7 @@ nfs4_open_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp,
 struct svc_fh *parent = NULL;
 int cb_up;
 int status = 0;
+ u32 wdeleg = false;
 
 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
 open->op_recall = 0;
@@ -5590,8 +5598,6 @@ nfs4_open_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp,

case NFS4_OPEN_CLAIM_PREVIOUS:
if (!cb_up)
open->op_recall = 1;
- if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
- goto out_no_deleg;
break;
case NFS4_OPEN_CLAIM_NULL:
parent = currentfh;

fs/nfsd/nfs4state.c +0 -0
@@ -5617,7 +5623,9 @@ nfs4_open_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp,
 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
 
Quoted 21 lines
Quoted 4 lines

fs/nfsd/nfs4state.c nfs4_open_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp, · line 5625

Quoted 1 line
fs/nfsd/nfs4state.c +0 -0
 trace_nfsd_deleg_read(&dp->dl_stid.sc_stateid);

I'd like you to add a trace_nfsd_deleg_write(), and invoke it here instead of trace_nfsd_deleg_read when NFSD hands out a write delegation.

Fix in v4.

-Dai

Quoted 13 lines
Quoted 11 lines
fs/nfsd/nfs4state.c +3 -1
- open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
+ wdeleg = open->op_share_access & NFS4_SHARE_ACCESS_WRITE;
+ open->op_delegate_type = wdeleg ?
+ NFS4_OPEN_DELEGATE_WRITE : NFS4_OPEN_DELEGATE_READ;
 nfs4_put_stid(&dp->dl_stid);
 return;

out_no_deleg:

Signature · 1 line
2.9.5

--
Chuck Lever

--
Chuck Lever

#7 lore raw

Re: [PATCH v3 2/2] NFSD: enable support for write delegation

Quoted 50 lines · Dai Ngo
Quoted 16 lines · Chuck Lever III
Quoted 16 lines · Dai Ngo
Quoted 11 lines · Chuck Lever III
Quoted 8 lines · Dai Ngo

This patch grants write delegation for OPEN with NFS4_SHARE_ACCESS_WRITE if there is no conflict with other OPENs.

Write delegation conflict with another OPEN, REMOVE, RENAME and SETATTR are handled the same as read delegation using notify_change, try_break_deleg.

Very clean. A couple of suggestions, one is down below, and here is the other:

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

I was thinking we should add one or two counters in fs/nfsd/stats.c to track how often read and write delegations are offered, and perhaps one to count the number of DELEGRETURN operations. What do you think makes sense?

I'm not sure what these counters will tell us, currently we already has a counter for number of delegations handed out.

I haven't found that, where is it? Certainly, if NFSD already has one, then no need to add more.

num_delegations in nfs4state.c

Quoted 3 lines

It would be nice one day, perhaps, to have a metric of how many delegations a client holds. That's not for this series.

okay.

Quoted 11 lines
Quoted 5 lines

I think a counter
on how often nfsd has to recall the write delegation due to GETATTR can be useful to know whether we should implement CB_GETATTR.

I hesitated to mention that because I wonder if that's something that would be interesting only for defending a design choice, not for site-to-site tuning. In other words, after we plumb it into NFSD, it will never actually be used after CB_GETATTR support is added.

Quoted 5 lines
Quoted 2 lines

Do you believe it's something that administrators can use to help balance or tune their workloads?

You're right. That is just for ourselves to determine if CB_GETATTR is needed.

To be clear, such a counter, I agree, would be useful /to us/. I'm just not sure how we could add something that would not become part of the kernel/userspace API. Anyone have thoughts about that?

--
Chuck Lever

maintainer Reviewed-by on the v3 series
#8 lore raw
In reply to Dai Ngo
Quoted 39 lines · Dai Ngo

NFSD: add support for NFSv4 write delegation

The NFSv4 server currently supports read delegation using VFS lease which is implemented using file_lock.

This patch series add write delegation support for NFSv4 server by:

. remove the check for F_WRLCK in generic_add_lease to allow
file_lock to be used for write delegation.

. grant write delegation for OPEN with NFS4_SHARE_ACCESS_WRITE
if there is no conflict with other OPENs.

Write delegation conflict with another OPEN, REMOVE, RENAME and SETATTR are handled the same as read delegation using notify_change, try_break_deleg.

Changes since v1:

[PATCH 3/4] NFSD: add supports for CB_GETATTR callback
- remove WARN_ON_ONCE from encode_bitmap4
- replace decode_bitmap4 with xdr_stream_decode_uint32_array
- replace xdr_inline_decode and xdr_decode_hyper in decode_cb_getattr with xdr_stream_decode_u64. Also remove the un-needed likely().
- modify signature of encode_cb_getattr4args to take pointer to nfs4_cb_fattr
- replace decode_attr_length with xdr_stream_decode_u32
- rename decode_cb_getattr to decode_cb_fattr4
- fold the initialization of cb_cinfo and cb_fsize into decode_cb_fattr4
- rename ncf_cb_cinfo to ncf_cb_change to avoid confusion of cindo usage in fs/nfsd/nfs4xdr.c
- correct NFS4_dec_cb_getattr_sz and update size description

[PATCH 4/4] NFSD: handle GETATTR conflict with write delegation
- change nfs4_handle_wrdeleg_conflict returns __be32 to fix test robot
- change ncf_cb_cinfo to ncf_cb_change to avoid confusion of cindo usage in fs/nfsd/nfs4xdr.c

Changes since v2:

Quoted 9 lines

[PATCH 2/4] NFSD: enable support for write delegation
- rename 'deleg' to 'dl_type' in nfs4_set_delegation
- remove 'wdeleg' in nfs4_open_delegation

- drop [PATCH 3/4] NFSD: add supports for CB_GETATTR callback and [PATCH 4/4] NFSD: handle GETATTR conflict with write delegation for futher clarification of the benefits of using CB_GETATTR for handling GETATTR from the 2nd client

Pretty straightforward. Not as useful (IMO) without CB_GETATTR, since even a stray 'ls -l' in the parent directory will cause the delegation to be recalled, but it's a reasonable first step.

author reviewer
#9 lore raw
Quoted 44 lines · Jeff Layton
Quoted 43 lines · Dai Ngo

NFSD: add support for NFSv4 write delegation

The NFSv4 server currently supports read delegation using VFS lease which is implemented using file_lock.

This patch series add write delegation support for NFSv4 server by:

. remove the check for F_WRLCK in generic_add_lease to allow
file_lock to be used for write delegation.

. grant write delegation for OPEN with NFS4_SHARE_ACCESS_WRITE
if there is no conflict with other OPENs.

Write delegation conflict with another OPEN, REMOVE, RENAME and SETATTR are handled the same as read delegation using notify_change, try_break_deleg.

Changes since v1:

[PATCH 3/4] NFSD: add supports for CB_GETATTR callback
- remove WARN_ON_ONCE from encode_bitmap4
- replace decode_bitmap4 with xdr_stream_decode_uint32_array
- replace xdr_inline_decode and xdr_decode_hyper in decode_cb_getattr with xdr_stream_decode_u64. Also remove the un-needed likely().
- modify signature of encode_cb_getattr4args to take pointer to nfs4_cb_fattr
- replace decode_attr_length with xdr_stream_decode_u32
- rename decode_cb_getattr to decode_cb_fattr4
- fold the initialization of cb_cinfo and cb_fsize into decode_cb_fattr4
- rename ncf_cb_cinfo to ncf_cb_change to avoid confusion of cindo usage in fs/nfsd/nfs4xdr.c
- correct NFS4_dec_cb_getattr_sz and update size description

[PATCH 4/4] NFSD: handle GETATTR conflict with write delegation
- change nfs4_handle_wrdeleg_conflict returns __be32 to fix test robot
- change ncf_cb_cinfo to ncf_cb_change to avoid confusion of cindo usage in fs/nfsd/nfs4xdr.c

Changes since v2:

[PATCH 2/4] NFSD: enable support for write delegation
- rename 'deleg' to 'dl_type' in nfs4_set_delegation
- remove 'wdeleg' in nfs4_open_delegation

Quoted 8 lines
Quoted 5 lines

- drop [PATCH 3/4] NFSD: add supports for CB_GETATTR callback and [PATCH 4/4] NFSD: handle GETATTR conflict with write delegation for futher clarification of the benefits of using CB_GETATTR for handling GETATTR from the 2nd client

Pretty straightforward. Not as useful (IMO) without CB_GETATTR, since even a stray 'ls -l' in the parent directory will cause the delegation to be recalled, but it's a reasonable first step.

Oh I forgot to leave to code to recall the write delegation in when there is GETATTR. Will fix in v4.

Yes, let just run with this simple code for awhile to make sure nothing break then we can add the CB_GETATTR later it it's needed.

-Dai

Quoted 2 lines