]> git.ipfire.org Git - thirdparty/kernel/stable-queue.git/blob - queue-5.15/nfsd-clean-up-find_or_add_file.patch
Fixes for 5.15
[thirdparty/kernel/stable-queue.git] / queue-5.15 / nfsd-clean-up-find_or_add_file.patch
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()
5
6 From: Chuck Lever <chuck.lever@oracle.com>
7
8 [ Upstream commit 9270fc514ba7d415636b23bcb937573a1ce54f6a ]
9
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
14 bucket.
15
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.
19
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>
23 ---
24 fs/nfsd/nfs4state.c | 64 ++++++++++++++++++++-------------------------
25 1 file changed, 28 insertions(+), 36 deletions(-)
26
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)
32 return NULL;
33 }
34
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)
38 {
39 struct nfs4_file *fp;
40 + unsigned int hashval = file_hashval(fh);
41 +
42 + rcu_read_lock();
43 + fp = find_file_locked(fh, hashval);
44 + rcu_read_unlock();
45 + return fp;
46 +}
47 +
48 +/*
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
52 + * field.
53 + */
54 +static noinline_for_stack struct nfs4_file *
55 +nfsd4_file_hash_insert(struct nfs4_file *new, const struct svc_fh *fhp)
56 +{
57 + unsigned int hashval = file_hashval(fhp);
58 struct nfs4_file *ret = NULL;
59 bool alias_found = false;
60 + struct nfs4_file *fi;
61
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))
68 - ret = fp;
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))
73 + ret = fi;
74 + } else if (d_inode(fhp->fh_dentry) == fi->fi_inode)
75 + fi->fi_aliased = alias_found = true;
76 }
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;
82 ret = new;
83 @@ -4724,32 +4742,6 @@ static struct nfs4_file *insert_file(struct nfs4_file *new, struct svc_fh *fh,
84 return ret;
85 }
86
87 -static struct nfs4_file * find_file(struct svc_fh *fh)
88 -{
89 - struct nfs4_file *fp;
90 - unsigned int hashval = file_hashval(fh);
91 -
92 - rcu_read_lock();
93 - fp = find_file_locked(fh, hashval);
94 - rcu_read_unlock();
95 - return fp;
96 -}
97 -
98 -static struct nfs4_file *
99 -find_or_add_file(struct nfs4_file *new, struct svc_fh *fh)
100 -{
101 - struct nfs4_file *fp;
102 - unsigned int hashval = file_hashval(fh);
103 -
104 - rcu_read_lock();
105 - fp = find_file_locked(fh, hashval);
106 - rcu_read_unlock();
107 - if (fp)
108 - return fp;
109 -
110 - return insert_file(new, fh, hashval);
111 -}
112 -
113 static noinline_for_stack void nfsd4_file_hash_remove(struct nfs4_file *fi)
114 {
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
119 */
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);
124 if (status)
125 --
126 2.43.0
127