1 From 02871b1a7fd36d758517e6687a0c3389991584d4 Mon Sep 17 00:00:00 2001
2 From: Sasha Levin <sashal@kernel.org>
3 Date: Fri, 28 Oct 2022 10:47:41 -0400
4 Subject: NFSD: Clean up find_or_add_file()
6 From: Chuck Lever <chuck.lever@oracle.com>
8 [ Upstream commit 9270fc514ba7d415636b23bcb937573a1ce54f6a ]
10 Remove the call to find_file_locked() in insert_nfs4_file(). Tracing
11 shows that over 99% of these calls return NULL. Thus it is not worth
12 the expense of the extra bucket list traversal. insert_file() already
13 deals correctly with the case where the item is already in the hash
16 Since nfsd4_file_hash_insert() is now just a wrapper around
17 insert_file(), move the meat of insert_file() into
18 nfsd4_file_hash_insert() and get rid of it.
20 Signed-off-by: Chuck Lever <chuck.lever@oracle.com>
21 Reviewed-by: NeilBrown <neilb@suse.de>
22 Signed-off-by: Chuck Lever <chuck.lever@oracle.com>
24 fs/nfsd/nfs4state.c | 64 ++++++++++++++++++++-------------------------
25 1 file changed, 28 insertions(+), 36 deletions(-)
27 diff --git a/fs/nfsd/nfs4state.c b/fs/nfsd/nfs4state.c
28 index f723d7d5e1557..192e721525665 100644
29 --- a/fs/nfsd/nfs4state.c
30 +++ b/fs/nfsd/nfs4state.c
31 @@ -4698,24 +4698,42 @@ find_file_locked(const struct svc_fh *fh, unsigned int hashval)
35 -static struct nfs4_file *insert_file(struct nfs4_file *new, struct svc_fh *fh,
36 - unsigned int hashval)
37 +static struct nfs4_file * find_file(struct svc_fh *fh)
40 + unsigned int hashval = file_hashval(fh);
43 + fp = find_file_locked(fh, hashval);
49 + * On hash insertion, identify entries with the same inode but
50 + * distinct filehandles. They will all be in the same hash bucket
51 + * because nfs4_file's are hashed by the address in the fi_inode
54 +static noinline_for_stack struct nfs4_file *
55 +nfsd4_file_hash_insert(struct nfs4_file *new, const struct svc_fh *fhp)
57 + unsigned int hashval = file_hashval(fhp);
58 struct nfs4_file *ret = NULL;
59 bool alias_found = false;
60 + struct nfs4_file *fi;
62 spin_lock(&state_lock);
63 - hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash,
64 + hlist_for_each_entry_rcu(fi, &file_hashtbl[hashval], fi_hash,
65 lockdep_is_held(&state_lock)) {
66 - if (fh_match(&fp->fi_fhandle, &fh->fh_handle)) {
67 - if (refcount_inc_not_zero(&fp->fi_ref))
69 - } else if (d_inode(fh->fh_dentry) == fp->fi_inode)
70 - fp->fi_aliased = alias_found = true;
71 + if (fh_match(&fi->fi_fhandle, &fhp->fh_handle)) {
72 + if (refcount_inc_not_zero(&fi->fi_ref))
74 + } else if (d_inode(fhp->fh_dentry) == fi->fi_inode)
75 + fi->fi_aliased = alias_found = true;
77 if (likely(ret == NULL)) {
78 - nfsd4_file_init(fh, new);
79 + nfsd4_file_init(fhp, new);
80 hlist_add_head_rcu(&new->fi_hash, &file_hashtbl[hashval]);
81 new->fi_aliased = alias_found;
83 @@ -4724,32 +4742,6 @@ static struct nfs4_file *insert_file(struct nfs4_file *new, struct svc_fh *fh,
87 -static struct nfs4_file * find_file(struct svc_fh *fh)
89 - struct nfs4_file *fp;
90 - unsigned int hashval = file_hashval(fh);
93 - fp = find_file_locked(fh, hashval);
98 -static struct nfs4_file *
99 -find_or_add_file(struct nfs4_file *new, struct svc_fh *fh)
101 - struct nfs4_file *fp;
102 - unsigned int hashval = file_hashval(fh);
105 - fp = find_file_locked(fh, hashval);
110 - return insert_file(new, fh, hashval);
113 static noinline_for_stack void nfsd4_file_hash_remove(struct nfs4_file *fi)
115 hlist_del_rcu(&fi->fi_hash);
116 @@ -5641,7 +5633,7 @@ nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nf
117 * and check for delegations in the process of being recalled.
118 * If not found, create the nfs4_file struct
120 - fp = find_or_add_file(open->op_file, current_fh);
121 + fp = nfsd4_file_hash_insert(open->op_file, current_fh);
122 if (fp != open->op_file) {
123 status = nfs4_check_deleg(cl, open, &dp);