2022-03-17 08:17:36

by Dai Ngo

[permalink] [raw]
Subject: [PATCH RFC v17 0/11] NFSD: Initial implementation of NFSv4 Courteous Server

Hi Chuck, Bruce

This series of patches implement the NFSv4 Courteous Server.

A server which does not immediately expunge the state on lease expiration
is known as a Courteous Server. A Courteous Server continues to recognize
previously generated state tokens as valid until conflict arises between
the expired state and the requests from another client, or the server
reboots.

v2 patch includes:

. add new callback, lm_expire_lock, to lock_manager_operations to
allow the lock manager to take appropriate action with conflict lock.

. handle conflicts of NFSv4 locks with NFSv3/NLM and local locks.

. expire courtesy client after 24hr if client has not reconnected.

. do not allow expired client to become courtesy client if there are
waiters for client's locks.

. modify client_info_show to show courtesy client and seconds from
last renew.

. fix a problem with NFSv4.1 server where the it keeps returning
SEQ4_STATUS_CB_PATH_DOWN in the successful SEQUENCE reply, after
the courtesy client reconnects, causing the client to keep sending
BCTS requests to server.

v3 patch includes:

. modified posix_test_lock to check and resolve conflict locks
to handle NLM TEST and NFSv4 LOCKT requests.

. separate out fix for back channel stuck in SEQ4_STATUS_CB_PATH_DOWN.

v4 patch includes:

. rework nfsd_check_courtesy to avoid dead lock of fl_lock and client_lock
by asking the laudromat thread to destroy the courtesy client.

. handle NFSv4 share reservation conflicts with courtesy client. This
includes conflicts between access mode and deny mode and vice versa.

. drop the patch for back channel stuck in SEQ4_STATUS_CB_PATH_DOWN.

v5 patch includes:

. fix recursive locking of file_rwsem from posix_lock_file.

. retest with LOCKDEP enabled.

v6 patch includes:

. merge witn 5.15-rc7

. fix a bug in nfs4_check_deny_bmap that did not check for matched
nfs4_file before checking for access/deny conflict. This bug causes
pynfs OPEN18 to fail since the server taking too long to release
lots of un-conflict clients' state.

. enhance share reservation conflict handler to handle case where
a large number of conflict courtesy clients need to be expired.
The 1st 100 clients are expired synchronously and the rest are
expired in the background by the laundromat and NFS4ERR_DELAY
is returned to the NFS client. This is needed to prevent the
NFS client from timing out waiting got the reply.

v7 patch includes:

. Fix race condition in posix_test_lock and posix_lock_inode after
dropping spinlock.

. Enhance nfsd4_fl_expire_lock to work with with new lm_expire_lock
callback

. Always resolve share reservation conflicts asynchrously.

. Fix bug in nfs4_laundromat where spinlock is not used when
scanning cl_ownerstr_hashtbl.

. Fix bug in nfs4_laundromat where idr_get_next was called
with incorrect 'id'.

. Merge nfs4_destroy_courtesy_client into nfsd4_fl_expire_lock.

v8 patch includes:

. Fix warning in nfsd4_fl_expire_lock reported by test robot.

v9 patch includes:

. Simplify lm_expire_lock API by (1) remove the 'testonly' flag
and (2) specifying return value as true/false to indicate
whether conflict was succesfully resolved.

. Rework nfsd4_fl_expire_lock to mark client with
NFSD4_DESTROY_COURTESY_CLIENT then tell the laundromat to expire
the client in the background.

. Add a spinlock in nfs4_client to synchronize access to the
NFSD4_COURTESY_CLIENT and NFSD4_DESTROY_COURTESY_CLIENT flag to
handle race conditions when resolving lock and share reservation
conflict.

. Courtesy client that was marked as NFSD4_DESTROY_COURTESY_CLIENT
are now consisdered 'dead', waiting for the laundromat to expire
it. This client is no longer allowed to use its states if it
reconnects before the laundromat finishes expiring the client.

For v4.1 client, the detection is done in the processing of the
SEQUENCE op and returns NFS4ERR_BAD_SESSION to force the client
to re-establish new clientid and session.
For v4.0 client, the detection is done in the processing of the
RENEW and state-related ops and return NFS4ERR_EXPIRE to force
the client to re-establish new clientid.

v10 patch includes:

Resolve deadlock in v9 by avoiding getting cl_client and
cl_cs_lock together. The laundromat needs to determine whether
the expired client has any state and also has no blockers on
its locks. Both of these conditions are allowed to change after
the laundromat transits an expired client to courtesy client.
When this happens, the laundromat will detect it on the next
run and and expire the courtesy client.

Remove client persistent record before marking it as COURTESY_CLIENT
and add client persistent record before clearing the COURTESY_CLIENT
flag to allow the courtesy client to transist to normal client to
continue to use its state.

Lock/delegation/share reversation conflict with courtesy client is
resolved by marking the courtesy client as DESTROY_COURTESY_CLIENT,
effectively disable it, then allow the current request to proceed
immediately.

Courtesy client marked as DESTROY_COURTESY_CLIENT is not allowed
to reconnect to reuse itsstate. It is expired by the laundromat
asynchronously in the background.

Move processing of expired clients from nfs4_laudromat to a
separate function, nfs4_get_client_reaplist, that creates the
reaplist and also to process courtesy clients.

Update Documentation/filesystems/locking.rst to include new
lm_lock_conflict call.

Modify leases_conflict to call lm_breaker_owns_lease only if
there is real conflict. This is to allow the lock manager to
resolve the delegation conflict if possible.

v11 patch includes:

Add comment for lm_lock_conflict callback.

Replace static const courtesy_client_expiry with macro.

Remove courtesy_clnt argument from find_in_sessionid_hashtbl.
Callers use nfs4_client->cl_cs_client boolean to determined if
it's the courtesy client and take appropriate actions.

Rename NFSD4_COURTESY_CLIENT and NFSD4_DESTROY_COURTESY_CLIENT
with NFSD4_CLIENT_COURTESY and NFSD4_CLIENT_DESTROY_COURTESY.

v12 patch includes:

Remove unnecessary comment in nfs4_get_client_reaplist.

Replace nfs4_client->cl_cs_client boolean with
NFSD4_CLIENT_COURTESY_CLNT flag.

Remove courtesy_clnt argument from find_client_in_id_table and
find_clp_in_name_tree. Callers use NFSD4_CLIENT_COURTESY_CLNT to
determined if it's the courtesy client and take appropriate actions.

v13 patch includes:

Merge with 5.17-rc3.

Cleanup Documentation/filesystems/locking.rst: replace i_lock
with flc_lock, update API's that use flc_lock.

Rename lm_lock_conflict to lm_lock_expired().

Remove comment of lm_lock_expired API in lock_manager_operations.
Same information is in patch description.

Update commit messages of 4/4.

Add some comment for NFSD4_CLIENT_COURTESY_CLNT.

Add nfsd4_discard_courtesy_clnt() to eliminate duplicate code of
discarding courtesy client; setting NFSD4_DESTROY_COURTESY_CLIENT.

v14 patch includes:

. merge with Chuck's public for-next branch.

. remove courtesy_client_expiry, use client's last renew time.

. simplify comment of nfs4_check_access_deny_bmap.

. add comment about race condition in nfs4_get_client_reaplist.

. add list_del when walking cslist in nfs4_get_client_reaplist.

. remove duplicate INIT_LIST_HEAD(&reaplist) from nfs4_laundromat

. Modify find_confirmed_client and find_confirmed_client_by_name
to detect courtesy client and destroy it.

. refactor lookup_clientid to use find_client_in_id_table
directly instead of find_confirmed_client.

. refactor nfsd4_setclientid to call find_clp_in_name_tree
directly instead of find_confirmed_client_by_name.

. remove comment of NFSD4_CLIENT_COURTESY.

. replace NFSD4_CLIENT_DESTROY_COURTESY with NFSD4_CLIENT_EXPIRED.

. replace NFSD4_CLIENT_COURTESY_CLNT with NFSD4_CLIENT_RECONNECTED.

v15 patch includes:

. add helper locks_has_blockers_locked in fs.h to check for
lock blockers

. rename nfs4_conflict_clients to nfs4_resolve_deny_conflicts_locked

. update nfs4_upgrade_open() to handle courtesy clients.

. add helper nfs4_check_and_expire_courtesy_client and
nfs4_is_courtesy_client_expired to deduplicate some code.

. update nfs4_anylock_blocker:
. replace list_for_each_entry_safe with list_for_each_entry
. break nfs4_anylock_blocker into 2 smaller functions.

. update nfs4_get_client_reaplist:
. remove unnecessary commets
. acquire cl_cs_lock before setting NFSD4_CLIENT_COURTESY flag

. update client_info_show to show 'time since last renew: 00:00:38'
instead of 'seconds from last renew: 38'.

v16 patch includes:

. update client_info_show to display 'status' as
'confirmed/unconfirmed/courtesy'

. replace helper locks_has_blockers_locked in fs.h in v15 with new
locks_owner_has_blockers call in fs/locks.c

. update nfs4_lockowner_has_blockers to use locks_owner_has_blockers

. move nfs4_check_and_expire_courtesy_client from 5/11 to 4/11

. remove unnecessary check for NULL clp in find_in_sessionid_hashtb

. fix typo in commit messages

v17 patch includes:

. replace flags used for courtesy client with enum courtesy_client_state

. add state table in nfsd/state.h

. make nfsd4_expire_courtesy_clnt, nfsd4_discard_courtesy_clnt and
nfsd4_courtesy_clnt_expired as static inline.

. update nfsd_breaker_owns_lease to use dl->dl_stid.sc_client directly

. fix kernel test robot warning when CONFIG_FILE_LOCKING not defined.


2022-03-17 08:24:50

by Dai Ngo

[permalink] [raw]
Subject: [PATCH RFC v17 03/11] NFSD: Add lm_lock_expired call out

Add callout function nfsd4_lm_lock_expired for lm_lock_expired.
If lock request has conflict with courtesy client then expire the
courtesy client and return no conflict to caller.

Signed-off-by: Dai Ngo <[email protected]>
---
fs/nfsd/nfs4state.c | 22 ++++++++++++++++++++++
fs/nfsd/state.h | 14 ++++++++++++++
2 files changed, 36 insertions(+)

diff --git a/fs/nfsd/nfs4state.c b/fs/nfsd/nfs4state.c
index a65d59510681..80772662236b 100644
--- a/fs/nfsd/nfs4state.c
+++ b/fs/nfsd/nfs4state.c
@@ -6578,10 +6578,32 @@ nfsd4_lm_notify(struct file_lock *fl)
}
}

+/**
+ * nfsd4_lm_lock_expired - check if lock conflict can be resolved.
+ *
+ * @fl: pointer to file_lock with a potential conflict
+ * Return values:
+ * %false: real conflict, lock conflict can not be resolved.
+ * %true: no conflict, lock conflict was resolved.
+ *
+ * Note that this function is called while the flc_lock is held.
+ */
+static bool
+nfsd4_lm_lock_expired(struct file_lock *fl)
+{
+ struct nfs4_lockowner *lo;
+
+ if (!fl)
+ return false;
+ lo = (struct nfs4_lockowner *)fl->fl_owner;
+ return nfsd4_expire_courtesy_clnt(lo->lo_owner.so_client);
+}
+
static const struct lock_manager_operations nfsd_posix_mng_ops = {
.lm_notify = nfsd4_lm_notify,
.lm_get_owner = nfsd4_lm_get_owner,
.lm_put_owner = nfsd4_lm_put_owner,
+ .lm_lock_expired = nfsd4_lm_lock_expired,
};

static inline void
diff --git a/fs/nfsd/state.h b/fs/nfsd/state.h
index 40e390abc842..d156ae3ab46c 100644
--- a/fs/nfsd/state.h
+++ b/fs/nfsd/state.h
@@ -735,4 +735,18 @@ extern void nfsd4_client_record_remove(struct nfs4_client *clp);
extern int nfsd4_client_record_check(struct nfs4_client *clp);
extern void nfsd4_record_grace_done(struct nfsd_net *nn);

+static inline bool
+nfsd4_expire_courtesy_clnt(struct nfs4_client *clp)
+{
+ bool rc = false;
+
+ spin_lock(&clp->cl_cs_lock);
+ if (clp->cl_cs_client_state == NFSD4_CLIENT_COURTESY)
+ clp->cl_cs_client_state = NFSD4_CLIENT_EXPIRED;
+ if (clp->cl_cs_client_state == NFSD4_CLIENT_EXPIRED)
+ rc = true;
+ spin_unlock(&clp->cl_cs_lock);
+ return rc;
+}
+
#endif /* NFSD4_STATE_H */
--
2.9.5

2022-03-17 08:25:08

by Dai Ngo

[permalink] [raw]
Subject: [PATCH RFC v17 08/11] NFSD: Update find_client_in_id_table() to handle courtesy client

Update find_client_in_id_table to:
. skip client with CLIENT_EXPIRED; discarded courtesy client
. if courtesy client was found then set CLIENT_RECONNECTED
state so caller can take appropriate action.

Update find_confirmed_client to discard courtesy client.
Update lookup_clientid to call find_client_in_id_table directly.
Update set_client to create client record for courtesy client.
Update find_cpntf_state to discard courtesy client.

Signed-off-by: Dai Ngo <[email protected]>
---
fs/nfsd/nfs4state.c | 22 ++++++++++++++++++++--
1 file changed, 20 insertions(+), 2 deletions(-)

diff --git a/fs/nfsd/nfs4state.c b/fs/nfsd/nfs4state.c
index 3b1e857eabee..102b4aae286d 100644
--- a/fs/nfsd/nfs4state.c
+++ b/fs/nfsd/nfs4state.c
@@ -2893,6 +2893,8 @@ find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
if (same_clid(&clp->cl_clientid, clid)) {
if ((bool)clp->cl_minorversion != sessions)
return NULL;
+ if (nfsd4_courtesy_clnt_expired(clp))
+ continue;
renew_client_locked(clp);
return clp;
}
@@ -2904,9 +2906,15 @@ static struct nfs4_client *
find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
{
struct list_head *tbl = nn->conf_id_hashtbl;
+ struct nfs4_client *clp;

lockdep_assert_held(&nn->client_lock);
- return find_client_in_id_table(tbl, clid, sessions);
+ clp = find_client_in_id_table(tbl, clid, sessions);
+ if (clp && clp->cl_cs_client_state == NFSD4_CLIENT_RECONNECTED) {
+ nfsd4_discard_courtesy_clnt(clp);
+ clp = NULL;
+ }
+ return clp;
}

static struct nfs4_client *
@@ -4822,9 +4830,10 @@ static struct nfs4_client *lookup_clientid(clientid_t *clid, bool sessions,
struct nfsd_net *nn)
{
struct nfs4_client *found;
+ struct list_head *tbl = nn->conf_id_hashtbl;

spin_lock(&nn->client_lock);
- found = find_confirmed_client(clid, sessions, nn);
+ found = find_client_in_id_table(tbl, clid, sessions);
if (found)
atomic_inc(&found->cl_rpc_users);
spin_unlock(&nn->client_lock);
@@ -4849,6 +4858,8 @@ static __be32 set_client(clientid_t *clid,
cstate->clp = lookup_clientid(clid, false, nn);
if (!cstate->clp)
return nfserr_expired;
+ if (cstate->clp->cl_cs_client_state == NFSD4_CLIENT_RECONNECTED)
+ nfsd4_client_record_create(cstate->clp);
return nfs_ok;
}

@@ -6168,6 +6179,13 @@ static __be32 find_cpntf_state(struct nfsd_net *nn, stateid_t *st,
found = lookup_clientid(&cps->cp_p_clid, true, nn);
if (!found)
goto out;
+ if (found->cl_cs_client_state == NFSD4_CLIENT_RECONNECTED) {
+ nfsd4_discard_courtesy_clnt(found);
+ if (atomic_dec_and_lock(&found->cl_rpc_users,
+ &nn->client_lock))
+ spin_unlock(&nn->client_lock);
+ goto out;
+ }

*stid = find_stateid_by_type(found, &cps->cp_p_stateid,
NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID);
--
2.9.5

2022-03-17 08:29:37

by Dai Ngo

[permalink] [raw]
Subject: [PATCH RFC v17 05/11] NFSD: Update nfs4_get_vfs_file() to handle courtesy client

Update nfs4_get_vfs_file and nfs4_upgrade_open to handle share
reservation conflict with courtesy client.

Update nfs4_get_vfs_file and nfs4_upgrade_open to handle share
reservation conflict with courtesy client.

When we have deny/access conflict we walk the fi_stateids of the
file in question, looking for open stateid and check the deny/access
of that stateid against the one from the open request. If there is
a conflict then we check if the client that owns that stateid is
a courtesy client. If it is then we set the client state to
CLIENT_EXPIRED and allow the open request to continue. We have
to scan all the stateid's of the file since the conflict can be
caused by multiple open stateid's.

Client with CLIENT_EXPIRED is expired by the laundromat.

Signed-off-by: Dai Ngo <[email protected]>
---
fs/nfsd/nfs4state.c | 99 ++++++++++++++++++++++++++++++++++++++++++++++++-----
1 file changed, 91 insertions(+), 8 deletions(-)

diff --git a/fs/nfsd/nfs4state.c b/fs/nfsd/nfs4state.c
index f20c75890594..c6b5e05c9c34 100644
--- a/fs/nfsd/nfs4state.c
+++ b/fs/nfsd/nfs4state.c
@@ -4950,9 +4950,75 @@ nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
return nfsd_setattr(rqstp, fh, &iattr, 0, (time64_t)0);
}

+static bool
+nfs4_check_access_deny_bmap(struct nfs4_ol_stateid *stp, u32 access,
+ bool share_access)
+{
+ if (share_access) {
+ if (!stp->st_deny_bmap)
+ return false;
+
+ if ((stp->st_deny_bmap & (1 << NFS4_SHARE_DENY_BOTH)) ||
+ (access & NFS4_SHARE_ACCESS_READ &&
+ stp->st_deny_bmap & (1 << NFS4_SHARE_DENY_READ)) ||
+ (access & NFS4_SHARE_ACCESS_WRITE &&
+ stp->st_deny_bmap & (1 << NFS4_SHARE_DENY_WRITE))) {
+ return true;
+ }
+ return false;
+ }
+ if ((access & NFS4_SHARE_DENY_BOTH) ||
+ (access & NFS4_SHARE_DENY_READ &&
+ stp->st_access_bmap & (1 << NFS4_SHARE_ACCESS_READ)) ||
+ (access & NFS4_SHARE_DENY_WRITE &&
+ stp->st_access_bmap & (1 << NFS4_SHARE_ACCESS_WRITE))) {
+ return true;
+ }
+ return false;
+}
+
+/*
+ * Check whether courtesy clients have conflicting access
+ *
+ * access: is op_share_access if share_access is true.
+ * Check if access mode, op_share_access, would conflict with
+ * the current deny mode of the file 'fp'.
+ * access: is op_share_deny if share_access is false.
+ * Check if the deny mode, op_share_deny, would conflict with
+ * current access of the file 'fp'.
+ * stp: skip checking this entry.
+ * new_stp: normal open, not open upgrade.
+ *
+ * Function returns:
+ * true - access/deny mode conflict with normal client.
+ * false - no conflict or conflict with courtesy client(s) is resolved.
+ */
+static bool
+nfs4_resolve_deny_conflicts_locked(struct nfs4_file *fp, bool new_stp,
+ struct nfs4_ol_stateid *stp, u32 access, bool share_access)
+{
+ struct nfs4_ol_stateid *st;
+ struct nfs4_client *clp;
+ bool conflict = false;
+
+ lockdep_assert_held(&fp->fi_lock);
+ list_for_each_entry(st, &fp->fi_stateids, st_perfile) {
+ if (st->st_openstp || (st == stp && new_stp) ||
+ (!nfs4_check_access_deny_bmap(st,
+ access, share_access)))
+ continue;
+ clp = st->st_stid.sc_client;
+ if (nfsd4_expire_courtesy_clnt(clp))
+ continue;
+ conflict = true;
+ break;
+ }
+ return conflict;
+}
+
static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
- struct nfsd4_open *open)
+ struct nfsd4_open *open, bool new_stp)
{
struct nfsd_file *nf = NULL;
__be32 status;
@@ -4968,15 +5034,29 @@ static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
*/
status = nfs4_file_check_deny(fp, open->op_share_deny);
if (status != nfs_ok) {
- spin_unlock(&fp->fi_lock);
- goto out;
+ if (status != nfserr_share_denied) {
+ spin_unlock(&fp->fi_lock);
+ goto out;
+ }
+ if (nfs4_resolve_deny_conflicts_locked(fp, new_stp,
+ stp, open->op_share_deny, false)) {
+ spin_unlock(&fp->fi_lock);
+ goto out;
+ }
}

/* set access to the file */
status = nfs4_file_get_access(fp, open->op_share_access);
if (status != nfs_ok) {
- spin_unlock(&fp->fi_lock);
- goto out;
+ if (status != nfserr_share_denied) {
+ spin_unlock(&fp->fi_lock);
+ goto out;
+ }
+ if (nfs4_resolve_deny_conflicts_locked(fp, new_stp,
+ stp, open->op_share_access, true)) {
+ spin_unlock(&fp->fi_lock);
+ goto out;
+ }
}

/* Set access bits in stateid */
@@ -5027,7 +5107,7 @@ nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp, struct svc_fh *c
unsigned char old_deny_bmap = stp->st_deny_bmap;

if (!test_access(open->op_share_access, stp))
- return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
+ return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open, false);

/* test and set deny mode */
spin_lock(&fp->fi_lock);
@@ -5036,7 +5116,10 @@ nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp, struct svc_fh *c
set_deny(open->op_share_deny, stp);
fp->fi_share_deny |=
(open->op_share_deny & NFS4_SHARE_DENY_BOTH);
- }
+ } else if (status == nfserr_share_denied &&
+ !nfs4_resolve_deny_conflicts_locked(fp, false, stp,
+ open->op_share_deny, false))
+ status = nfs_ok;
spin_unlock(&fp->fi_lock);

if (status != nfs_ok)
@@ -5376,7 +5459,7 @@ nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nf
goto out;
}
} else {
- status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
+ status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open, true);
if (status) {
stp->st_stid.sc_type = NFS4_CLOSED_STID;
release_open_stateid(stp);
--
2.9.5

2022-03-17 08:32:55

by Dai Ngo

[permalink] [raw]
Subject: [PATCH RFC v17 06/11] NFSD: Update find_clp_in_name_tree() to handle courtesy client

Update find_clp_in_name_tree to check and expire courtesy client.

Update find_confirmed_client_by_name to discard the courtesy
client by setting CLIENT_EXPIRED.

Update nfsd4_setclientid to expire client with CLIENT_EXPIRED
state to prevent multiple confirmed clients with the same name
on the conf_id_hashtbl list.

Signed-off-by: Dai Ngo <[email protected]>
---
fs/nfsd/nfs4state.c | 27 ++++++++++++++++++++++++---
fs/nfsd/state.h | 22 ++++++++++++++++++++++
2 files changed, 46 insertions(+), 3 deletions(-)

diff --git a/fs/nfsd/nfs4state.c b/fs/nfsd/nfs4state.c
index c6b5e05c9c34..dc0e60bf694c 100644
--- a/fs/nfsd/nfs4state.c
+++ b/fs/nfsd/nfs4state.c
@@ -2834,8 +2834,11 @@ find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
node = node->rb_left;
else if (cmp < 0)
node = node->rb_right;
- else
+ else {
+ if (nfsd4_courtesy_clnt_expired(clp))
+ return NULL;
return clp;
+ }
}
return NULL;
}
@@ -2914,8 +2917,15 @@ static bool clp_used_exchangeid(struct nfs4_client *clp)
static struct nfs4_client *
find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
{
+ struct nfs4_client *clp;
+
lockdep_assert_held(&nn->client_lock);
- return find_clp_in_name_tree(name, &nn->conf_name_tree);
+ clp = find_clp_in_name_tree(name, &nn->conf_name_tree);
+ if (clp && clp->cl_cs_client_state == NFSD4_CLIENT_RECONNECTED) {
+ nfsd4_discard_courtesy_clnt(clp);
+ clp = NULL;
+ }
+ return clp;
}

static struct nfs4_client *
@@ -4032,12 +4042,19 @@ nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
struct nfs4_client *unconf = NULL;
__be32 status;
struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
+ struct nfs4_client *cclient = NULL;

new = create_client(clname, rqstp, &clverifier);
if (new == NULL)
return nfserr_jukebox;
spin_lock(&nn->client_lock);
- conf = find_confirmed_client_by_name(&clname, nn);
+ /* find confirmed client by name */
+ conf = find_clp_in_name_tree(&clname, &nn->conf_name_tree);
+ if (conf && conf->cl_cs_client_state == NFSD4_CLIENT_RECONNECTED) {
+ cclient = conf;
+ conf = NULL;
+ }
+
if (conf && client_has_state(conf)) {
status = nfserr_clid_inuse;
if (clp_used_exchangeid(conf))
@@ -4068,7 +4085,11 @@ nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
new = NULL;
status = nfs_ok;
out:
+ if (cclient)
+ unhash_client_locked(cclient);
spin_unlock(&nn->client_lock);
+ if (cclient)
+ expire_client(cclient);
if (new)
free_client(new);
if (unconf) {
diff --git a/fs/nfsd/state.h b/fs/nfsd/state.h
index d156ae3ab46c..14b2c158ccca 100644
--- a/fs/nfsd/state.h
+++ b/fs/nfsd/state.h
@@ -735,6 +735,7 @@ extern void nfsd4_client_record_remove(struct nfs4_client *clp);
extern int nfsd4_client_record_check(struct nfs4_client *clp);
extern void nfsd4_record_grace_done(struct nfsd_net *nn);

+/* courteous server */
static inline bool
nfsd4_expire_courtesy_clnt(struct nfs4_client *clp)
{
@@ -749,4 +750,25 @@ nfsd4_expire_courtesy_clnt(struct nfs4_client *clp)
return rc;
}

+static inline void
+nfsd4_discard_courtesy_clnt(struct nfs4_client *clp)
+{
+ spin_lock(&clp->cl_cs_lock);
+ clp->cl_cs_client_state = NFSD4_CLIENT_EXPIRED;
+ spin_unlock(&clp->cl_cs_lock);
+}
+
+static inline bool
+nfsd4_courtesy_clnt_expired(struct nfs4_client *clp)
+{
+ bool rc = false;
+
+ spin_lock(&clp->cl_cs_lock);
+ if (clp->cl_cs_client_state == NFSD4_CLIENT_EXPIRED)
+ rc = true;
+ if (clp->cl_cs_client_state == NFSD4_CLIENT_COURTESY)
+ clp->cl_cs_client_state = NFSD4_CLIENT_RECONNECTED;
+ spin_unlock(&clp->cl_cs_lock);
+ return rc;
+}
#endif /* NFSD4_STATE_H */
--
2.9.5

2022-03-17 08:57:47

by Dai Ngo

[permalink] [raw]
Subject: [PATCH RFC v17 09/11] NFSD: Refactor nfsd4_laundromat()

Extract a bit of logic that is about to be expanded to handle
courtesy clients.

Signed-off-by: Dai Ngo <[email protected]>
---
fs/nfsd/nfs4state.c | 33 +++++++++++++++++++++------------
1 file changed, 21 insertions(+), 12 deletions(-)

diff --git a/fs/nfsd/nfs4state.c b/fs/nfsd/nfs4state.c
index 102b4aae286d..b4b976e00ce6 100644
--- a/fs/nfsd/nfs4state.c
+++ b/fs/nfsd/nfs4state.c
@@ -5755,6 +5755,26 @@ static void nfsd4_ssc_expire_umount(struct nfsd_net *nn)
}
#endif

+static void
+nfs4_get_client_reaplist(struct nfsd_net *nn, struct list_head *reaplist,
+ struct laundry_time *lt)
+{
+ struct list_head *pos, *next;
+ struct nfs4_client *clp;
+
+ INIT_LIST_HEAD(reaplist);
+ spin_lock(&nn->client_lock);
+ list_for_each_safe(pos, next, &nn->client_lru) {
+ clp = list_entry(pos, struct nfs4_client, cl_lru);
+ if (!state_expired(lt, clp->cl_time))
+ break;
+ if (mark_client_expired_locked(clp))
+ continue;
+ list_add(&clp->cl_lru, reaplist);
+ }
+ spin_unlock(&nn->client_lock);
+}
+
static time64_t
nfs4_laundromat(struct nfsd_net *nn)
{
@@ -5777,7 +5797,6 @@ nfs4_laundromat(struct nfsd_net *nn)
goto out;
}
nfsd4_end_grace(nn);
- INIT_LIST_HEAD(&reaplist);

spin_lock(&nn->s2s_cp_lock);
idr_for_each_entry(&nn->s2s_cp_stateids, cps_t, i) {
@@ -5787,17 +5806,7 @@ nfs4_laundromat(struct nfsd_net *nn)
_free_cpntf_state_locked(nn, cps);
}
spin_unlock(&nn->s2s_cp_lock);
-
- spin_lock(&nn->client_lock);
- list_for_each_safe(pos, next, &nn->client_lru) {
- clp = list_entry(pos, struct nfs4_client, cl_lru);
- if (!state_expired(&lt, clp->cl_time))
- break;
- if (mark_client_expired_locked(clp))
- continue;
- list_add(&clp->cl_lru, &reaplist);
- }
- spin_unlock(&nn->client_lock);
+ nfs4_get_client_reaplist(nn, &reaplist, &lt);
list_for_each_safe(pos, next, &reaplist) {
clp = list_entry(pos, struct nfs4_client, cl_lru);
trace_nfsd_clid_purged(&clp->cl_clientid);
--
2.9.5

2022-03-17 09:01:45

by Dai Ngo

[permalink] [raw]
Subject: [PATCH RFC v17 07/11] NFSD: Update find_in_sessionid_hashtbl() to handle courtesy client

Update find_in_sessionid_hashtbl to:
. skip client with CLIENT_EXPIRED state; discarded courtesy client.
. if courtesy client was found then set CLIENT_RECONNECTED so caller
can take appropriate action.

Update nfsd4_sequence and nfsd4_bind_conn_to_session to create client
record for courtesy client with CLIENT_RECONNECTED state.

Update nfsd4_destroy_session to discard courtesy client with
CLIENT_RECONNECTED state.

Signed-off-by: Dai Ngo <[email protected]>
---
fs/nfsd/nfs4state.c | 34 ++++++++++++++++++++++++++++++++--
1 file changed, 32 insertions(+), 2 deletions(-)

diff --git a/fs/nfsd/nfs4state.c b/fs/nfsd/nfs4state.c
index dc0e60bf694c..3b1e857eabee 100644
--- a/fs/nfsd/nfs4state.c
+++ b/fs/nfsd/nfs4state.c
@@ -1935,13 +1935,22 @@ find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
{
struct nfsd4_session *session;
__be32 status = nfserr_badsession;
+ struct nfs4_client *clp;

session = __find_in_sessionid_hashtbl(sessionid, net);
if (!session)
goto out;
+ clp = session->se_client;
+ if (nfsd4_courtesy_clnt_expired(clp)) {
+ session = NULL;
+ goto out;
+ }
status = nfsd4_get_session_locked(session);
- if (status)
+ if (status) {
session = NULL;
+ if (clp->cl_cs_client_state == NFSD4_CLIENT_RECONNECTED)
+ nfsd4_discard_courtesy_clnt(clp);
+ }
out:
*ret = status;
return session;
@@ -3643,6 +3652,7 @@ __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
struct nfsd4_session *session;
struct net *net = SVC_NET(rqstp);
struct nfsd_net *nn = net_generic(net, nfsd_net_id);
+ struct nfs4_client *clp;

if (!nfsd4_last_compound_op(rqstp))
return nfserr_not_only_op;
@@ -3675,6 +3685,13 @@ __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
nfsd4_init_conn(rqstp, conn, session);
status = nfs_ok;
out:
+ clp = session->se_client;
+ if (clp->cl_cs_client_state == NFSD4_CLIENT_RECONNECTED) {
+ if (status == nfs_ok)
+ nfsd4_client_record_create(clp);
+ else
+ nfsd4_discard_courtesy_clnt(clp);
+ }
nfsd4_put_session(session);
out_no_session:
return status;
@@ -3697,6 +3714,7 @@ nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate,
int ref_held_by_me = 0;
struct net *net = SVC_NET(r);
struct nfsd_net *nn = net_generic(net, nfsd_net_id);
+ struct nfs4_client *clp;

status = nfserr_not_only_op;
if (nfsd4_compound_in_session(cstate, sessionid)) {
@@ -3709,6 +3727,12 @@ nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate,
ses = find_in_sessionid_hashtbl(sessionid, net, &status);
if (!ses)
goto out_client_lock;
+ clp = ses->se_client;
+ if (clp->cl_cs_client_state == NFSD4_CLIENT_RECONNECTED) {
+ status = nfserr_badsession;
+ nfsd4_discard_courtesy_clnt(clp);
+ goto out_put_session;
+ }
status = nfserr_wrong_cred;
if (!nfsd4_mach_creds_match(ses->se_client, r))
goto out_put_session;
@@ -3813,7 +3837,7 @@ nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
struct nfsd4_compoundres *resp = rqstp->rq_resp;
struct xdr_stream *xdr = resp->xdr;
struct nfsd4_session *session;
- struct nfs4_client *clp;
+ struct nfs4_client *clp = NULL;
struct nfsd4_slot *slot;
struct nfsd4_conn *conn;
__be32 status;
@@ -3923,6 +3947,12 @@ nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
if (conn)
free_conn(conn);
spin_unlock(&nn->client_lock);
+ if (clp && clp->cl_cs_client_state == NFSD4_CLIENT_RECONNECTED) {
+ if (status == nfs_ok)
+ nfsd4_client_record_create(clp);
+ else
+ nfsd4_discard_courtesy_clnt(clp);
+ }
return status;
out_put_session:
nfsd4_put_session_locked(session);
--
2.9.5

2022-03-17 09:33:50

by Dai Ngo

[permalink] [raw]
Subject: [PATCH RFC v17 11/11] NFSD: Show state of courtesy clients in client info

Update client_info_show to show state of courtesy client
and time since last renew.

Signed-off-by: Dai Ngo <[email protected]>
---
fs/nfsd/nfs4state.c | 12 ++++++++++--
1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/fs/nfsd/nfs4state.c b/fs/nfsd/nfs4state.c
index d5758c7101dc..21dd8265bef8 100644
--- a/fs/nfsd/nfs4state.c
+++ b/fs/nfsd/nfs4state.c
@@ -2411,7 +2411,8 @@ static int client_info_show(struct seq_file *m, void *v)
{
struct inode *inode = m->private;
struct nfs4_client *clp;
- u64 clid;
+ u64 clid, hrs;
+ u32 mins, secs;

clp = get_nfsdfs_clp(inode);
if (!clp)
@@ -2419,10 +2420,17 @@ static int client_info_show(struct seq_file *m, void *v)
memcpy(&clid, &clp->cl_clientid, sizeof(clid));
seq_printf(m, "clientid: 0x%llx\n", clid);
seq_printf(m, "address: \"%pISpc\"\n", (struct sockaddr *)&clp->cl_addr);
- if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
+
+ if (clp->cl_cs_client_state == NFSD4_CLIENT_COURTESY)
+ seq_puts(m, "status: courtesy\n");
+ else if (clp->cl_cs_client_state == NFSD4_CLIENT_CONFIRMED)
seq_puts(m, "status: confirmed\n");
else
seq_puts(m, "status: unconfirmed\n");
+ hrs = div_u64_rem(ktime_get_boottime_seconds() - clp->cl_time,
+ 3600, &secs);
+ mins = div_u64_rem((u64)secs, 60, &secs);
+ seq_printf(m, "time since last renew: %llu:%02u:%02u\n", hrs, mins, secs);
seq_printf(m, "name: ");
seq_quote_mem(m, clp->cl_name.data, clp->cl_name.len);
seq_printf(m, "\nminor version: %d\n", clp->cl_minorversion);
--
2.9.5

2022-03-17 12:56:22

by Dai Ngo

[permalink] [raw]
Subject: [PATCH RFC v17 01/11] fs/lock: add helper locks_owner_has_blockers to check for blockers

Add helper locks_owner_has_blockers to check if there is any blockers
for a given lockowner.

Signed-off-by: Dai Ngo <[email protected]>
---
fs/locks.c | 28 ++++++++++++++++++++++++++++
include/linux/fs.h | 7 +++++++
2 files changed, 35 insertions(+)

diff --git a/fs/locks.c b/fs/locks.c
index 050acf8b5110..53864eb99dc5 100644
--- a/fs/locks.c
+++ b/fs/locks.c
@@ -300,6 +300,34 @@ void locks_release_private(struct file_lock *fl)
}
EXPORT_SYMBOL_GPL(locks_release_private);

+/**
+ * locks_owner_has_blockers - Check for blocking lock requests
+ * @flctx: file lock context
+ * @owner: lock owner
+ *
+ * Return values:
+ * %true: @owner has at least one blocker
+ * %false: @owner has no blockers
+ */
+bool locks_owner_has_blockers(struct file_lock_context *flctx,
+ fl_owner_t owner)
+{
+ struct file_lock *fl;
+
+ spin_lock(&flctx->flc_lock);
+ list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
+ if (fl->fl_owner != owner)
+ continue;
+ if (!list_empty(&fl->fl_blocked_requests)) {
+ spin_unlock(&flctx->flc_lock);
+ return true;
+ }
+ }
+ spin_unlock(&flctx->flc_lock);
+ return false;
+}
+EXPORT_SYMBOL_GPL(locks_owner_has_blockers);
+
/* Free a lock which is not in use. */
void locks_free_lock(struct file_lock *fl)
{
diff --git a/include/linux/fs.h b/include/linux/fs.h
index 831b20430d6e..2057a9df790f 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -1200,6 +1200,8 @@ extern void lease_unregister_notifier(struct notifier_block *);
struct files_struct;
extern void show_fd_locks(struct seq_file *f,
struct file *filp, struct files_struct *files);
+extern bool locks_owner_has_blockers(struct file_lock_context *flctx,
+ fl_owner_t owner);
#else /* !CONFIG_FILE_LOCKING */
static inline int fcntl_getlk(struct file *file, unsigned int cmd,
struct flock __user *user)
@@ -1335,6 +1337,11 @@ static inline int lease_modify(struct file_lock *fl, int arg,
struct files_struct;
static inline void show_fd_locks(struct seq_file *f,
struct file *filp, struct files_struct *files) {}
+static inline bool locks_owner_has_blockers(struct file_lock_context *flctx,
+ fl_owner_t owner)
+{
+ return false;
+}
#endif /* !CONFIG_FILE_LOCKING */

static inline struct inode *file_inode(const struct file *f)
--
2.9.5

2022-03-17 17:48:18

by Jeffrey Layton

[permalink] [raw]
Subject: Re: [PATCH RFC v17 01/11] fs/lock: add helper locks_owner_has_blockers to check for blockers

On Thu, 2022-03-17 at 00:43 -0700, Dai Ngo wrote:
> Add helper locks_owner_has_blockers to check if there is any blockers
> for a given lockowner.
>
> Signed-off-by: Dai Ngo <[email protected]>
> ---
> fs/locks.c | 28 ++++++++++++++++++++++++++++
> include/linux/fs.h | 7 +++++++
> 2 files changed, 35 insertions(+)
>
> diff --git a/fs/locks.c b/fs/locks.c
> index 050acf8b5110..53864eb99dc5 100644
> --- a/fs/locks.c
> +++ b/fs/locks.c
> @@ -300,6 +300,34 @@ void locks_release_private(struct file_lock *fl)
> }
> EXPORT_SYMBOL_GPL(locks_release_private);
>
> +/**
> + * locks_owner_has_blockers - Check for blocking lock requests
> + * @flctx: file lock context
> + * @owner: lock owner
> + *
> + * Return values:
> + * %true: @owner has at least one blocker
> + * %false: @owner has no blockers
> + */
> +bool locks_owner_has_blockers(struct file_lock_context *flctx,
> + fl_owner_t owner)
> +{
> + struct file_lock *fl;
> +
> + spin_lock(&flctx->flc_lock);
> + list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
> + if (fl->fl_owner != owner)
> + continue;
> + if (!list_empty(&fl->fl_blocked_requests)) {
> + spin_unlock(&flctx->flc_lock);
> + return true;
> + }
> + }
> + spin_unlock(&flctx->flc_lock);
> + return false;
> +}
> +EXPORT_SYMBOL_GPL(locks_owner_has_blockers);
> +
> /* Free a lock which is not in use. */
> void locks_free_lock(struct file_lock *fl)
> {
> diff --git a/include/linux/fs.h b/include/linux/fs.h
> index 831b20430d6e..2057a9df790f 100644
> --- a/include/linux/fs.h
> +++ b/include/linux/fs.h
> @@ -1200,6 +1200,8 @@ extern void lease_unregister_notifier(struct notifier_block *);
> struct files_struct;
> extern void show_fd_locks(struct seq_file *f,
> struct file *filp, struct files_struct *files);
> +extern bool locks_owner_has_blockers(struct file_lock_context *flctx,
> + fl_owner_t owner);
> #else /* !CONFIG_FILE_LOCKING */
> static inline int fcntl_getlk(struct file *file, unsigned int cmd,
> struct flock __user *user)
> @@ -1335,6 +1337,11 @@ static inline int lease_modify(struct file_lock *fl, int arg,
> struct files_struct;
> static inline void show_fd_locks(struct seq_file *f,
> struct file *filp, struct files_struct *files) {}
> +static inline bool locks_owner_has_blockers(struct file_lock_context *flctx,
> + fl_owner_t owner)
> +{
> + return false;
> +}
> #endif /* !CONFIG_FILE_LOCKING */
>
> static inline struct inode *file_inode(const struct file *f)

Reviewed-by: Jeff Layton <[email protected]>

2022-03-17 19:49:23

by Chuck Lever

[permalink] [raw]
Subject: Re: [PATCH RFC v17 01/11] fs/lock: add helper locks_owner_has_blockers to check for blockers



> On Mar 17, 2022, at 1:39 PM, Jeff Layton <[email protected]> wrote:
>
> On Thu, 2022-03-17 at 00:43 -0700, Dai Ngo wrote:
>> Add helper locks_owner_has_blockers to check if there is any blockers
>> for a given lockowner.
>>
>> Signed-off-by: Dai Ngo <[email protected]>
>> ---
>> fs/locks.c | 28 ++++++++++++++++++++++++++++
>> include/linux/fs.h | 7 +++++++
>> 2 files changed, 35 insertions(+)
>>
>> diff --git a/fs/locks.c b/fs/locks.c
>> index 050acf8b5110..53864eb99dc5 100644
>> --- a/fs/locks.c
>> +++ b/fs/locks.c
>> @@ -300,6 +300,34 @@ void locks_release_private(struct file_lock *fl)
>> }
>> EXPORT_SYMBOL_GPL(locks_release_private);
>>
>> +/**
>> + * locks_owner_has_blockers - Check for blocking lock requests
>> + * @flctx: file lock context
>> + * @owner: lock owner
>> + *
>> + * Return values:
>> + * %true: @owner has at least one blocker
>> + * %false: @owner has no blockers
>> + */
>> +bool locks_owner_has_blockers(struct file_lock_context *flctx,
>> + fl_owner_t owner)
>> +{
>> + struct file_lock *fl;
>> +
>> + spin_lock(&flctx->flc_lock);
>> + list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
>> + if (fl->fl_owner != owner)
>> + continue;
>> + if (!list_empty(&fl->fl_blocked_requests)) {
>> + spin_unlock(&flctx->flc_lock);
>> + return true;
>> + }
>> + }
>> + spin_unlock(&flctx->flc_lock);
>> + return false;
>> +}
>> +EXPORT_SYMBOL_GPL(locks_owner_has_blockers);
>> +
>> /* Free a lock which is not in use. */
>> void locks_free_lock(struct file_lock *fl)
>> {
>> diff --git a/include/linux/fs.h b/include/linux/fs.h
>> index 831b20430d6e..2057a9df790f 100644
>> --- a/include/linux/fs.h
>> +++ b/include/linux/fs.h
>> @@ -1200,6 +1200,8 @@ extern void lease_unregister_notifier(struct notifier_block *);
>> struct files_struct;
>> extern void show_fd_locks(struct seq_file *f,
>> struct file *filp, struct files_struct *files);
>> +extern bool locks_owner_has_blockers(struct file_lock_context *flctx,
>> + fl_owner_t owner);
>> #else /* !CONFIG_FILE_LOCKING */
>> static inline int fcntl_getlk(struct file *file, unsigned int cmd,
>> struct flock __user *user)
>> @@ -1335,6 +1337,11 @@ static inline int lease_modify(struct file_lock *fl, int arg,
>> struct files_struct;
>> static inline void show_fd_locks(struct seq_file *f,
>> struct file *filp, struct files_struct *files) {}
>> +static inline bool locks_owner_has_blockers(struct file_lock_context *flctx,
>> + fl_owner_t owner)
>> +{
>> + return false;
>> +}
>> #endif /* !CONFIG_FILE_LOCKING */
>>
>> static inline struct inode *file_inode(const struct file *f)
>
> Reviewed-by: Jeff Layton <[email protected]>

Thanks!

--
Chuck Lever



2022-03-17 20:03:55

by Chuck Lever

[permalink] [raw]
Subject: Re: [PATCH RFC v17 01/11] fs/lock: add helper locks_owner_has_blockers to check for blockers


> On Mar 17, 2022, at 3:43 AM, Dai Ngo <[email protected]> wrote:
>
> Add helper locks_owner_has_blockers to check if there is any blockers
> for a given lockowner.
>
> Signed-off-by: Dai Ngo <[email protected]>

Hi Jeff, can we get an R-b or Acked from you for this one?


> ---
> fs/locks.c | 28 ++++++++++++++++++++++++++++
> include/linux/fs.h | 7 +++++++
> 2 files changed, 35 insertions(+)
>
> diff --git a/fs/locks.c b/fs/locks.c
> index 050acf8b5110..53864eb99dc5 100644
> --- a/fs/locks.c
> +++ b/fs/locks.c
> @@ -300,6 +300,34 @@ void locks_release_private(struct file_lock *fl)
> }
> EXPORT_SYMBOL_GPL(locks_release_private);
>
> +/**
> + * locks_owner_has_blockers - Check for blocking lock requests
> + * @flctx: file lock context
> + * @owner: lock owner
> + *
> + * Return values:
> + * %true: @owner has at least one blocker
> + * %false: @owner has no blockers
> + */
> +bool locks_owner_has_blockers(struct file_lock_context *flctx,
> + fl_owner_t owner)
> +{
> + struct file_lock *fl;
> +
> + spin_lock(&flctx->flc_lock);
> + list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
> + if (fl->fl_owner != owner)
> + continue;
> + if (!list_empty(&fl->fl_blocked_requests)) {
> + spin_unlock(&flctx->flc_lock);
> + return true;
> + }
> + }
> + spin_unlock(&flctx->flc_lock);
> + return false;
> +}
> +EXPORT_SYMBOL_GPL(locks_owner_has_blockers);
> +
> /* Free a lock which is not in use. */
> void locks_free_lock(struct file_lock *fl)
> {
> diff --git a/include/linux/fs.h b/include/linux/fs.h
> index 831b20430d6e..2057a9df790f 100644
> --- a/include/linux/fs.h
> +++ b/include/linux/fs.h
> @@ -1200,6 +1200,8 @@ extern void lease_unregister_notifier(struct notifier_block *);
> struct files_struct;
> extern void show_fd_locks(struct seq_file *f,
> struct file *filp, struct files_struct *files);
> +extern bool locks_owner_has_blockers(struct file_lock_context *flctx,
> + fl_owner_t owner);
> #else /* !CONFIG_FILE_LOCKING */
> static inline int fcntl_getlk(struct file *file, unsigned int cmd,
> struct flock __user *user)
> @@ -1335,6 +1337,11 @@ static inline int lease_modify(struct file_lock *fl, int arg,
> struct files_struct;
> static inline void show_fd_locks(struct seq_file *f,
> struct file *filp, struct files_struct *files) {}
> +static inline bool locks_owner_has_blockers(struct file_lock_context *flctx,
> + fl_owner_t owner)
> +{
> + return false;
> +}
> #endif /* !CONFIG_FILE_LOCKING */
>
> static inline struct inode *file_inode(const struct file *f)
> --
> 2.9.5
>

--
Chuck Lever



2022-03-21 23:33:51

by J. Bruce Fields

[permalink] [raw]
Subject: Re: [PATCH RFC v17 05/11] NFSD: Update nfs4_get_vfs_file() to handle courtesy client

On Thu, Mar 17, 2022 at 12:43:42AM -0700, Dai Ngo wrote:
> Update nfs4_get_vfs_file and nfs4_upgrade_open to handle share
> reservation conflict with courtesy client.
>
> Update nfs4_get_vfs_file and nfs4_upgrade_open to handle share
> reservation conflict with courtesy client.
>
> When we have deny/access conflict we walk the fi_stateids of the
> file in question, looking for open stateid and check the deny/access
> of that stateid against the one from the open request. If there is
> a conflict then we check if the client that owns that stateid is
> a courtesy client. If it is then we set the client state to
> CLIENT_EXPIRED and allow the open request to continue. We have
> to scan all the stateid's of the file since the conflict can be
> caused by multiple open stateid's.
>
> Client with CLIENT_EXPIRED is expired by the laundromat.
>
> Signed-off-by: Dai Ngo <[email protected]>
> ---
> fs/nfsd/nfs4state.c | 99 ++++++++++++++++++++++++++++++++++++++++++++++++-----
> 1 file changed, 91 insertions(+), 8 deletions(-)
>
> diff --git a/fs/nfsd/nfs4state.c b/fs/nfsd/nfs4state.c
> index f20c75890594..c6b5e05c9c34 100644
> --- a/fs/nfsd/nfs4state.c
> +++ b/fs/nfsd/nfs4state.c
> @@ -4950,9 +4950,75 @@ nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
> return nfsd_setattr(rqstp, fh, &iattr, 0, (time64_t)0);
> }
>
> +static bool
> +nfs4_check_access_deny_bmap(struct nfs4_ol_stateid *stp, u32 access,
> + bool share_access)
> +{
> + if (share_access) {
> + if (!stp->st_deny_bmap)
> + return false;
> +
> + if ((stp->st_deny_bmap & (1 << NFS4_SHARE_DENY_BOTH)) ||
> + (access & NFS4_SHARE_ACCESS_READ &&
> + stp->st_deny_bmap & (1 << NFS4_SHARE_DENY_READ)) ||
> + (access & NFS4_SHARE_ACCESS_WRITE &&
> + stp->st_deny_bmap & (1 << NFS4_SHARE_DENY_WRITE))) {
> + return true;
> + }
> + return false;

Looks like you could do this with just:

return share_access & bmap_to_share_mode(stp->st_deny_bmap);


> + }
> + if ((access & NFS4_SHARE_DENY_BOTH) ||
> + (access & NFS4_SHARE_DENY_READ &&
> + stp->st_access_bmap & (1 << NFS4_SHARE_ACCESS_READ)) ||
> + (access & NFS4_SHARE_DENY_WRITE &&
> + stp->st_access_bmap & (1 << NFS4_SHARE_ACCESS_WRITE))) {
> + return true;
> + }

Likewise.

Also, I think it'd be simpler to check for both access and deny
conflicts here, instead of just one or the other.

> + return false;
> +}
> +
> +/*
> + * Check whether courtesy clients have conflicting access
> + *
> + * access: is op_share_access if share_access is true.
> + * Check if access mode, op_share_access, would conflict with
> + * the current deny mode of the file 'fp'.
> + * access: is op_share_deny if share_access is false.
> + * Check if the deny mode, op_share_deny, would conflict with
> + * current access of the file 'fp'.
> + * stp: skip checking this entry.
> + * new_stp: normal open, not open upgrade.
> + *
> + * Function returns:
> + * true - access/deny mode conflict with normal client.
> + * false - no conflict or conflict with courtesy client(s) is resolved.
> + */
> +static bool
> +nfs4_resolve_deny_conflicts_locked(struct nfs4_file *fp, bool new_stp,
> + struct nfs4_ol_stateid *stp, u32 access, bool share_access)
> +{
> + struct nfs4_ol_stateid *st;
> + struct nfs4_client *clp;
> + bool conflict = false;
> +
> + lockdep_assert_held(&fp->fi_lock);
> + list_for_each_entry(st, &fp->fi_stateids, st_perfile) {
> + if (st->st_openstp || (st == stp && new_stp) ||

I'd split this into separate if statements and add a comment at least
for the st_openstp case, which isn't too obvious:

if (st->st_openstp) /* ignore lock stateids */
continue;

> + (!nfs4_check_access_deny_bmap(st,
> + access, share_access)))
> + continue;
> + clp = st->st_stid.sc_client;
> + if (nfsd4_expire_courtesy_clnt(clp))
> + continue;
> + conflict = true;
> + break;
> + }
> + return conflict;
> +}
> +
> static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
> struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
> - struct nfsd4_open *open)
> + struct nfsd4_open *open, bool new_stp)
> {
> struct nfsd_file *nf = NULL;
> __be32 status;
> @@ -4968,15 +5034,29 @@ static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
> */
> status = nfs4_file_check_deny(fp, open->op_share_deny);
> if (status != nfs_ok) {
> - spin_unlock(&fp->fi_lock);
> - goto out;
> + if (status != nfserr_share_denied) {
> + spin_unlock(&fp->fi_lock);
> + goto out;
> + }
> + if (nfs4_resolve_deny_conflicts_locked(fp, new_stp,
> + stp, open->op_share_deny, false)) {
> + spin_unlock(&fp->fi_lock);
> + goto out;
> + }
> }
>
> /* set access to the file */
> status = nfs4_file_get_access(fp, open->op_share_access);
> if (status != nfs_ok) {
> - spin_unlock(&fp->fi_lock);
> - goto out;
> + if (status != nfserr_share_denied) {
> + spin_unlock(&fp->fi_lock);
> + goto out;
> + }
> + if (nfs4_resolve_deny_conflicts_locked(fp, new_stp,
> + stp, open->op_share_access, true)) {
> + spin_unlock(&fp->fi_lock);
> + goto out;
> + }
> }
>
> /* Set access bits in stateid */
> @@ -5027,7 +5107,7 @@ nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp, struct svc_fh *c
> unsigned char old_deny_bmap = stp->st_deny_bmap;
>
> if (!test_access(open->op_share_access, stp))
> - return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
> + return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open, false);
>
> /* test and set deny mode */
> spin_lock(&fp->fi_lock);
> @@ -5036,7 +5116,10 @@ nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp, struct svc_fh *c
> set_deny(open->op_share_deny, stp);
> fp->fi_share_deny |=
> (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
> - }
> + } else if (status == nfserr_share_denied &&
> + !nfs4_resolve_deny_conflicts_locked(fp, false, stp,
> + open->op_share_deny, false))

Looks to me like these nfs4_resolve_deny_conflicts_locked() calls could
go into nfs4_file_check_deny and nfs4_file_get_access instead.

--b.

> + status = nfs_ok;
> spin_unlock(&fp->fi_lock);
>
> if (status != nfs_ok)
> @@ -5376,7 +5459,7 @@ nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nf
> goto out;
> }
> } else {
> - status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
> + status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open, true);
> if (status) {
> stp->st_stid.sc_type = NFS4_CLOSED_STID;
> release_open_stateid(stp);
> --
> 2.9.5

2022-03-22 23:12:07

by J. Bruce Fields

[permalink] [raw]
Subject: Re: [PATCH RFC v17 06/11] NFSD: Update find_clp_in_name_tree() to handle courtesy client

On Thu, Mar 17, 2022 at 12:43:43AM -0700, Dai Ngo wrote:
> Update find_clp_in_name_tree to check and expire courtesy client.
>
> Update find_confirmed_client_by_name to discard the courtesy
> client by setting CLIENT_EXPIRED.
>
> Update nfsd4_setclientid to expire client with CLIENT_EXPIRED
> state to prevent multiple confirmed clients with the same name
> on the conf_id_hashtbl list.

I could use a little more "why" here.

I'll give it another read, but right now I'm just not understanding how
this is meant to work.

--b.

>
> Signed-off-by: Dai Ngo <[email protected]>
> ---
> fs/nfsd/nfs4state.c | 27 ++++++++++++++++++++++++---
> fs/nfsd/state.h | 22 ++++++++++++++++++++++
> 2 files changed, 46 insertions(+), 3 deletions(-)
>
> diff --git a/fs/nfsd/nfs4state.c b/fs/nfsd/nfs4state.c
> index c6b5e05c9c34..dc0e60bf694c 100644
> --- a/fs/nfsd/nfs4state.c
> +++ b/fs/nfsd/nfs4state.c
> @@ -2834,8 +2834,11 @@ find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
> node = node->rb_left;
> else if (cmp < 0)
> node = node->rb_right;
> - else
> + else {
> + if (nfsd4_courtesy_clnt_expired(clp))
> + return NULL;
> return clp;
> + }
> }
> return NULL;
> }
> @@ -2914,8 +2917,15 @@ static bool clp_used_exchangeid(struct nfs4_client *clp)
> static struct nfs4_client *
> find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
> {
> + struct nfs4_client *clp;
> +
> lockdep_assert_held(&nn->client_lock);
> - return find_clp_in_name_tree(name, &nn->conf_name_tree);
> + clp = find_clp_in_name_tree(name, &nn->conf_name_tree);
> + if (clp && clp->cl_cs_client_state == NFSD4_CLIENT_RECONNECTED) {
> + nfsd4_discard_courtesy_clnt(clp);
> + clp = NULL;
> + }
> + return clp;
> }
>
> static struct nfs4_client *
> @@ -4032,12 +4042,19 @@ nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
> struct nfs4_client *unconf = NULL;
> __be32 status;
> struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
> + struct nfs4_client *cclient = NULL;
>
> new = create_client(clname, rqstp, &clverifier);
> if (new == NULL)
> return nfserr_jukebox;
> spin_lock(&nn->client_lock);
> - conf = find_confirmed_client_by_name(&clname, nn);
> + /* find confirmed client by name */
> + conf = find_clp_in_name_tree(&clname, &nn->conf_name_tree);
> + if (conf && conf->cl_cs_client_state == NFSD4_CLIENT_RECONNECTED) {
> + cclient = conf;
> + conf = NULL;
> + }
> +
> if (conf && client_has_state(conf)) {
> status = nfserr_clid_inuse;
> if (clp_used_exchangeid(conf))
> @@ -4068,7 +4085,11 @@ nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
> new = NULL;
> status = nfs_ok;
> out:
> + if (cclient)
> + unhash_client_locked(cclient);
> spin_unlock(&nn->client_lock);
> + if (cclient)
> + expire_client(cclient);
> if (new)
> free_client(new);
> if (unconf) {
> diff --git a/fs/nfsd/state.h b/fs/nfsd/state.h
> index d156ae3ab46c..14b2c158ccca 100644
> --- a/fs/nfsd/state.h
> +++ b/fs/nfsd/state.h
> @@ -735,6 +735,7 @@ extern void nfsd4_client_record_remove(struct nfs4_client *clp);
> extern int nfsd4_client_record_check(struct nfs4_client *clp);
> extern void nfsd4_record_grace_done(struct nfsd_net *nn);
>
> +/* courteous server */
> static inline bool
> nfsd4_expire_courtesy_clnt(struct nfs4_client *clp)
> {
> @@ -749,4 +750,25 @@ nfsd4_expire_courtesy_clnt(struct nfs4_client *clp)
> return rc;
> }
>
> +static inline void
> +nfsd4_discard_courtesy_clnt(struct nfs4_client *clp)
> +{
> + spin_lock(&clp->cl_cs_lock);
> + clp->cl_cs_client_state = NFSD4_CLIENT_EXPIRED;
> + spin_unlock(&clp->cl_cs_lock);
> +}
> +
> +static inline bool
> +nfsd4_courtesy_clnt_expired(struct nfs4_client *clp)
> +{
> + bool rc = false;
> +
> + spin_lock(&clp->cl_cs_lock);
> + if (clp->cl_cs_client_state == NFSD4_CLIENT_EXPIRED)
> + rc = true;
> + if (clp->cl_cs_client_state == NFSD4_CLIENT_COURTESY)
> + clp->cl_cs_client_state = NFSD4_CLIENT_RECONNECTED;
> + spin_unlock(&clp->cl_cs_lock);
> + return rc;
> +}
> #endif /* NFSD4_STATE_H */
> --
> 2.9.5

2022-03-23 11:43:51

by Dai Ngo

[permalink] [raw]
Subject: Re: [PATCH RFC v17 06/11] NFSD: Update find_clp_in_name_tree() to handle courtesy client


On 3/22/22 2:50 PM, J. Bruce Fields wrote:
> On Thu, Mar 17, 2022 at 12:43:43AM -0700, Dai Ngo wrote:
>> Update find_clp_in_name_tree to check and expire courtesy client.
>>
>> Update find_confirmed_client_by_name to discard the courtesy
>> client by setting CLIENT_EXPIRED.
>>
>> Update nfsd4_setclientid to expire client with CLIENT_EXPIRED
>> state to prevent multiple confirmed clients with the same name
>> on the conf_id_hashtbl list.

This should be nn->conf_name_tree instead of conf_id_hashtbl list.

> I could use a little more "why" here.
>
> I'll give it another read, but right now I'm just not understanding how
> this is meant to work.

Here is the problem description when running pynfs/nfs4.0 CID1 (need to
run CID5 first to create ton of clients) which requires the change in
nfsd4_setclientid to get the test to pass:

1st setclientid(name=clientA,verf=X)
. find confirmed courtesy client with same name clientA (clientid:1) and
mark it DESTROY and leave it on the confirmed list.
. assign new clientA with clientid:2
. put clientid:2 on unconfirmed list
. return clientid:2

1st setclientid_confirm(clientid:2)
. does not find any confirmed for clientid:2
. find unconfirmed clientid:2
. does not find any confirmed clientA (find_confirmed_client_by_name)
since courtesy clientA was marked DESTROY
. put clientid:2: to conf_name_tree list

PROBLEM: new clientA (clientid:2) and courtesy clientA (clientid:1)
are both now on the conf_name_tree list.

clientid:2 does OPEN -> stidX

2nd setclientid(name=clientA, verf=Y)
. does not find any confirmed clientA: skip courtesy clientA since it
was marked DESTROY and does not find confirmed client:2 due to the
PROBLEM noted above; duplicate entry in binary tree.
. create new clientid:3 and put it on unconfirmed list
. return clientid:3

2nd setclientid_confirm(clientid:3)
. does not find any confirmed clientid:3
. find unconfirmed clientid:3
. does not find any confirmed clientA: skip courtesy clientA since it
was marked DESTROY and does not find confirmed client:2 due to the
PROBLEM noted above; duplicate entry in binary tree.
. put clientid:3 on conf_name_tree list

clientid:3 does CLOSE(stidX) -> NFS4_OK since clientid:2 still valid <ERROR!>


-Dai

>
> --b.
>
>> Signed-off-by: Dai Ngo <[email protected]>
>> ---
>> fs/nfsd/nfs4state.c | 27 ++++++++++++++++++++++++---
>> fs/nfsd/state.h | 22 ++++++++++++++++++++++
>> 2 files changed, 46 insertions(+), 3 deletions(-)
>>
>> diff --git a/fs/nfsd/nfs4state.c b/fs/nfsd/nfs4state.c
>> index c6b5e05c9c34..dc0e60bf694c 100644
>> --- a/fs/nfsd/nfs4state.c
>> +++ b/fs/nfsd/nfs4state.c
>> @@ -2834,8 +2834,11 @@ find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
>> node = node->rb_left;
>> else if (cmp < 0)
>> node = node->rb_right;
>> - else
>> + else {
>> + if (nfsd4_courtesy_clnt_expired(clp))
>> + return NULL;
>> return clp;
>> + }
>> }
>> return NULL;
>> }
>> @@ -2914,8 +2917,15 @@ static bool clp_used_exchangeid(struct nfs4_client *clp)
>> static struct nfs4_client *
>> find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
>> {
>> + struct nfs4_client *clp;
>> +
>> lockdep_assert_held(&nn->client_lock);
>> - return find_clp_in_name_tree(name, &nn->conf_name_tree);
>> + clp = find_clp_in_name_tree(name, &nn->conf_name_tree);
>> + if (clp && clp->cl_cs_client_state == NFSD4_CLIENT_RECONNECTED) {
>> + nfsd4_discard_courtesy_clnt(clp);
>> + clp = NULL;
>> + }
>> + return clp;
>> }
>>
>> static struct nfs4_client *
>> @@ -4032,12 +4042,19 @@ nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
>> struct nfs4_client *unconf = NULL;
>> __be32 status;
>> struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
>> + struct nfs4_client *cclient = NULL;
>>
>> new = create_client(clname, rqstp, &clverifier);
>> if (new == NULL)
>> return nfserr_jukebox;
>> spin_lock(&nn->client_lock);
>> - conf = find_confirmed_client_by_name(&clname, nn);
>> + /* find confirmed client by name */
>> + conf = find_clp_in_name_tree(&clname, &nn->conf_name_tree);
>> + if (conf && conf->cl_cs_client_state == NFSD4_CLIENT_RECONNECTED) {
>> + cclient = conf;
>> + conf = NULL;
>> + }
>> +
>> if (conf && client_has_state(conf)) {
>> status = nfserr_clid_inuse;
>> if (clp_used_exchangeid(conf))
>> @@ -4068,7 +4085,11 @@ nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
>> new = NULL;
>> status = nfs_ok;
>> out:
>> + if (cclient)
>> + unhash_client_locked(cclient);
>> spin_unlock(&nn->client_lock);
>> + if (cclient)
>> + expire_client(cclient);
>> if (new)
>> free_client(new);
>> if (unconf) {
>> diff --git a/fs/nfsd/state.h b/fs/nfsd/state.h
>> index d156ae3ab46c..14b2c158ccca 100644
>> --- a/fs/nfsd/state.h
>> +++ b/fs/nfsd/state.h
>> @@ -735,6 +735,7 @@ extern void nfsd4_client_record_remove(struct nfs4_client *clp);
>> extern int nfsd4_client_record_check(struct nfs4_client *clp);
>> extern void nfsd4_record_grace_done(struct nfsd_net *nn);
>>
>> +/* courteous server */
>> static inline bool
>> nfsd4_expire_courtesy_clnt(struct nfs4_client *clp)
>> {
>> @@ -749,4 +750,25 @@ nfsd4_expire_courtesy_clnt(struct nfs4_client *clp)
>> return rc;
>> }
>>
>> +static inline void
>> +nfsd4_discard_courtesy_clnt(struct nfs4_client *clp)
>> +{
>> + spin_lock(&clp->cl_cs_lock);
>> + clp->cl_cs_client_state = NFSD4_CLIENT_EXPIRED;
>> + spin_unlock(&clp->cl_cs_lock);
>> +}
>> +
>> +static inline bool
>> +nfsd4_courtesy_clnt_expired(struct nfs4_client *clp)
>> +{
>> + bool rc = false;
>> +
>> + spin_lock(&clp->cl_cs_lock);
>> + if (clp->cl_cs_client_state == NFSD4_CLIENT_EXPIRED)
>> + rc = true;
>> + if (clp->cl_cs_client_state == NFSD4_CLIENT_COURTESY)
>> + clp->cl_cs_client_state = NFSD4_CLIENT_RECONNECTED;
>> + spin_unlock(&clp->cl_cs_lock);
>> + return rc;
>> +}
>> #endif /* NFSD4_STATE_H */
>> --
>> 2.9.5

2022-03-24 12:25:19

by Dai Ngo

[permalink] [raw]
Subject: Re: [PATCH RFC v17 05/11] NFSD: Update nfs4_get_vfs_file() to handle courtesy client


On 3/21/22 2:44 PM, J. Bruce Fields wrote:
> On Thu, Mar 17, 2022 at 12:43:42AM -0700, Dai Ngo wrote:
>> Update nfs4_get_vfs_file and nfs4_upgrade_open to handle share
>> reservation conflict with courtesy client.
>>
>> Update nfs4_get_vfs_file and nfs4_upgrade_open to handle share
>> reservation conflict with courtesy client.
>>
>> When we have deny/access conflict we walk the fi_stateids of the
>> file in question, looking for open stateid and check the deny/access
>> of that stateid against the one from the open request. If there is
>> a conflict then we check if the client that owns that stateid is
>> a courtesy client. If it is then we set the client state to
>> CLIENT_EXPIRED and allow the open request to continue. We have
>> to scan all the stateid's of the file since the conflict can be
>> caused by multiple open stateid's.
>>
>> Client with CLIENT_EXPIRED is expired by the laundromat.
>>
>> Signed-off-by: Dai Ngo <[email protected]>
>> ---
>> fs/nfsd/nfs4state.c | 99 ++++++++++++++++++++++++++++++++++++++++++++++++-----
>> 1 file changed, 91 insertions(+), 8 deletions(-)
>>
>> diff --git a/fs/nfsd/nfs4state.c b/fs/nfsd/nfs4state.c
>> index f20c75890594..c6b5e05c9c34 100644
>> --- a/fs/nfsd/nfs4state.c
>> +++ b/fs/nfsd/nfs4state.c
>> @@ -4950,9 +4950,75 @@ nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
>> return nfsd_setattr(rqstp, fh, &iattr, 0, (time64_t)0);
>> }
>>
>> +static bool
>> +nfs4_check_access_deny_bmap(struct nfs4_ol_stateid *stp, u32 access,
>> + bool share_access)
>> +{
>> + if (share_access) {
>> + if (!stp->st_deny_bmap)
>> + return false;
>> +
>> + if ((stp->st_deny_bmap & (1 << NFS4_SHARE_DENY_BOTH)) ||
>> + (access & NFS4_SHARE_ACCESS_READ &&
>> + stp->st_deny_bmap & (1 << NFS4_SHARE_DENY_READ)) ||
>> + (access & NFS4_SHARE_ACCESS_WRITE &&
>> + stp->st_deny_bmap & (1 << NFS4_SHARE_DENY_WRITE))) {
>> + return true;
>> + }
>> + return false;
> Looks like you could do this with just:
>
> return share_access & bmap_to_share_mode(stp->st_deny_bmap);

Thanks, this makes it much simpler. Fix in v18.

>
>
>> + }
>> + if ((access & NFS4_SHARE_DENY_BOTH) ||
>> + (access & NFS4_SHARE_DENY_READ &&
>> + stp->st_access_bmap & (1 << NFS4_SHARE_ACCESS_READ)) ||
>> + (access & NFS4_SHARE_DENY_WRITE &&
>> + stp->st_access_bmap & (1 << NFS4_SHARE_ACCESS_WRITE))) {
>> + return true;
>> + }
> Likewise.

Fix in v18.

>
> Also, I think it'd be simpler to check for both access and deny
> conflicts here, instead of just one or the other.

I'm not clear here.

I remove nfs4_check_access_deny_bmap, fold this functionality
into nfs4_resolve_deny_conflicts_locked by making use of
bmap_to_share_mode.


>
>> + return false;
>> +}
>> +
>> +/*
>> + * Check whether courtesy clients have conflicting access
>> + *
>> + * access: is op_share_access if share_access is true.
>> + * Check if access mode, op_share_access, would conflict with
>> + * the current deny mode of the file 'fp'.
>> + * access: is op_share_deny if share_access is false.
>> + * Check if the deny mode, op_share_deny, would conflict with
>> + * current access of the file 'fp'.
>> + * stp: skip checking this entry.
>> + * new_stp: normal open, not open upgrade.
>> + *
>> + * Function returns:
>> + * true - access/deny mode conflict with normal client.
>> + * false - no conflict or conflict with courtesy client(s) is resolved.
>> + */
>> +static bool
>> +nfs4_resolve_deny_conflicts_locked(struct nfs4_file *fp, bool new_stp,
>> + struct nfs4_ol_stateid *stp, u32 access, bool share_access)
>> +{
>> + struct nfs4_ol_stateid *st;
>> + struct nfs4_client *clp;
>> + bool conflict = false;
>> +
>> + lockdep_assert_held(&fp->fi_lock);
>> + list_for_each_entry(st, &fp->fi_stateids, st_perfile) {
>> + if (st->st_openstp || (st == stp && new_stp) ||
> I'd split this into separate if statements and add a comment at least
> for the st_openstp case, which isn't too obvious:
>
> if (st->st_openstp) /* ignore lock stateids */
> continue;

Fix in v18.

>
>> + (!nfs4_check_access_deny_bmap(st,
>> + access, share_access)))
>> + continue;
>> + clp = st->st_stid.sc_client;
>> + if (nfsd4_expire_courtesy_clnt(clp))
>> + continue;
>> + conflict = true;
>> + break;
>> + }
>> + return conflict;
>> +}
>> +
>> static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
>> struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
>> - struct nfsd4_open *open)
>> + struct nfsd4_open *open, bool new_stp)
>> {
>> struct nfsd_file *nf = NULL;
>> __be32 status;
>> @@ -4968,15 +5034,29 @@ static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
>> */
>> status = nfs4_file_check_deny(fp, open->op_share_deny);
>> if (status != nfs_ok) {
>> - spin_unlock(&fp->fi_lock);
>> - goto out;
>> + if (status != nfserr_share_denied) {
>> + spin_unlock(&fp->fi_lock);
>> + goto out;
>> + }
>> + if (nfs4_resolve_deny_conflicts_locked(fp, new_stp,
>> + stp, open->op_share_deny, false)) {
>> + spin_unlock(&fp->fi_lock);
>> + goto out;
>> + }
>> }
>>
>> /* set access to the file */
>> status = nfs4_file_get_access(fp, open->op_share_access);
>> if (status != nfs_ok) {
>> - spin_unlock(&fp->fi_lock);
>> - goto out;
>> + if (status != nfserr_share_denied) {
>> + spin_unlock(&fp->fi_lock);
>> + goto out;
>> + }
>> + if (nfs4_resolve_deny_conflicts_locked(fp, new_stp,
>> + stp, open->op_share_access, true)) {
>> + spin_unlock(&fp->fi_lock);
>> + goto out;
>> + }
>> }
>>
>> /* Set access bits in stateid */
>> @@ -5027,7 +5107,7 @@ nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp, struct svc_fh *c
>> unsigned char old_deny_bmap = stp->st_deny_bmap;
>>
>> if (!test_access(open->op_share_access, stp))
>> - return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
>> + return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open, false);
>>
>> /* test and set deny mode */
>> spin_lock(&fp->fi_lock);
>> @@ -5036,7 +5116,10 @@ nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp, struct svc_fh *c
>> set_deny(open->op_share_deny, stp);
>> fp->fi_share_deny |=
>> (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
>> - }
>> + } else if (status == nfserr_share_denied &&
>> + !nfs4_resolve_deny_conflicts_locked(fp, false, stp,
>> + open->op_share_deny, false))
> Looks to me like these nfs4_resolve_deny_conflicts_locked() calls could
> go into nfs4_file_check_deny and nfs4_file_get_access instead.

Fix in v18.

-Dai

>
> --b.
>
>> + status = nfs_ok;
>> spin_unlock(&fp->fi_lock);
>>
>> if (status != nfs_ok)
>> @@ -5376,7 +5459,7 @@ nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nf
>> goto out;
>> }
>> } else {
>> - status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
>> + status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open, true);
>> if (status) {
>> stp->st_stid.sc_type = NFS4_CLOSED_STID;
>> release_open_stateid(stp);
>> --
>> 2.9.5