~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/nfsd/nfs4state.c

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /fs/nfsd/nfs4state.c (Version linux-6.12-rc7) and /fs/nfsd/nfs4state.c (Version linux-6.8.12)


  1 /*                                                  1 /*
  2 *  Copyright (c) 2001 The Regents of the Unive      2 *  Copyright (c) 2001 The Regents of the University of Michigan.
  3 *  All rights reserved.                             3 *  All rights reserved.
  4 *                                                   4 *
  5 *  Kendrick Smith <kmsmith@umich.edu>               5 *  Kendrick Smith <kmsmith@umich.edu>
  6 *  Andy Adamson <kandros@umich.edu>                 6 *  Andy Adamson <kandros@umich.edu>
  7 *                                                   7 *
  8 *  Redistribution and use in source and binary      8 *  Redistribution and use in source and binary forms, with or without
  9 *  modification, are permitted provided that t      9 *  modification, are permitted provided that the following conditions
 10 *  are met:                                        10 *  are met:
 11 *                                                  11 *
 12 *  1. Redistributions of source code must reta     12 *  1. Redistributions of source code must retain the above copyright
 13 *     notice, this list of conditions and the      13 *     notice, this list of conditions and the following disclaimer.
 14 *  2. Redistributions in binary form must repr     14 *  2. Redistributions in binary form must reproduce the above copyright
 15 *     notice, this list of conditions and the      15 *     notice, this list of conditions and the following disclaimer in the
 16 *     documentation and/or other materials pro     16 *     documentation and/or other materials provided with the distribution.
 17 *  3. Neither the name of the University nor t     17 *  3. Neither the name of the University nor the names of its
 18 *     contributors may be used to endorse or p     18 *     contributors may be used to endorse or promote products derived
 19 *     from this software without specific prio     19 *     from this software without specific prior written permission.
 20 *                                                  20 *
 21 *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY     21 *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
 22 *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO,      22 *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 23 *  MERCHANTABILITY AND FITNESS FOR A PARTICULA     23 *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 24 *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS O     24 *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 25 *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECI     25 *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 26 *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT L     26 *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 27 *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,      27 *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 28 *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND O     28 *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 29 *  LIABILITY, WHETHER IN CONTRACT, STRICT LIAB     29 *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 30 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY     30 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 31 *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILIT     31 *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 32 *                                                  32 *
 33 */                                                 33 */
 34                                                    34 
 35 #include <linux/file.h>                            35 #include <linux/file.h>
 36 #include <linux/fs.h>                              36 #include <linux/fs.h>
 37 #include <linux/slab.h>                            37 #include <linux/slab.h>
 38 #include <linux/namei.h>                           38 #include <linux/namei.h>
 39 #include <linux/swap.h>                            39 #include <linux/swap.h>
 40 #include <linux/pagemap.h>                         40 #include <linux/pagemap.h>
 41 #include <linux/ratelimit.h>                       41 #include <linux/ratelimit.h>
 42 #include <linux/sunrpc/svcauth_gss.h>              42 #include <linux/sunrpc/svcauth_gss.h>
 43 #include <linux/sunrpc/addr.h>                     43 #include <linux/sunrpc/addr.h>
 44 #include <linux/jhash.h>                           44 #include <linux/jhash.h>
 45 #include <linux/string_helpers.h>                  45 #include <linux/string_helpers.h>
 46 #include <linux/fsnotify.h>                        46 #include <linux/fsnotify.h>
 47 #include <linux/rhashtable.h>                      47 #include <linux/rhashtable.h>
 48 #include <linux/nfs_ssc.h>                         48 #include <linux/nfs_ssc.h>
 49                                                    49 
 50 #include "xdr4.h"                                  50 #include "xdr4.h"
 51 #include "xdr4cb.h"                                51 #include "xdr4cb.h"
 52 #include "vfs.h"                                   52 #include "vfs.h"
 53 #include "current_stateid.h"                       53 #include "current_stateid.h"
 54                                                    54 
 55 #include "netns.h"                                 55 #include "netns.h"
 56 #include "pnfs.h"                                  56 #include "pnfs.h"
 57 #include "filecache.h"                             57 #include "filecache.h"
 58 #include "trace.h"                                 58 #include "trace.h"
 59                                                    59 
 60 #define NFSDDBG_FACILITY                NFSDDB     60 #define NFSDDBG_FACILITY                NFSDDBG_PROC
 61                                                    61 
 62 #define all_ones {{ ~0, ~0}, ~0}                   62 #define all_ones {{ ~0, ~0}, ~0}
 63 static const stateid_t one_stateid = {             63 static const stateid_t one_stateid = {
 64         .si_generation = ~0,                       64         .si_generation = ~0,
 65         .si_opaque = all_ones,                     65         .si_opaque = all_ones,
 66 };                                                 66 };
 67 static const stateid_t zero_stateid = {            67 static const stateid_t zero_stateid = {
 68         /* all fields zero */                      68         /* all fields zero */
 69 };                                                 69 };
 70 static const stateid_t currentstateid = {          70 static const stateid_t currentstateid = {
 71         .si_generation = 1,                        71         .si_generation = 1,
 72 };                                                 72 };
 73 static const stateid_t close_stateid = {           73 static const stateid_t close_stateid = {
 74         .si_generation = 0xffffffffU,              74         .si_generation = 0xffffffffU,
 75 };                                                 75 };
 76                                                    76 
 77 static u64 current_sessionid = 1;                  77 static u64 current_sessionid = 1;
 78                                                    78 
 79 #define ZERO_STATEID(stateid) (!memcmp((statei     79 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
 80 #define ONE_STATEID(stateid)  (!memcmp((statei     80 #define ONE_STATEID(stateid)  (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
 81 #define CURRENT_STATEID(stateid) (!memcmp((sta     81 #define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
 82 #define CLOSE_STATEID(stateid)  (!memcmp((stat     82 #define CLOSE_STATEID(stateid)  (!memcmp((stateid), &close_stateid, sizeof(stateid_t)))
 83                                                    83 
 84 /* forward declarations */                         84 /* forward declarations */
 85 static bool check_for_locks(struct nfs4_file *     85 static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
 86 static void nfs4_free_ol_stateid(struct nfs4_s     86 static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
 87 void nfsd4_end_grace(struct nfsd_net *nn);         87 void nfsd4_end_grace(struct nfsd_net *nn);
 88 static void _free_cpntf_state_locked(struct nf     88 static void _free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps);
 89 static void nfsd4_file_hash_remove(struct nfs4     89 static void nfsd4_file_hash_remove(struct nfs4_file *fi);
 90 static void deleg_reaper(struct nfsd_net *nn); << 
 91                                                    90 
 92 /* Locking: */                                     91 /* Locking: */
 93                                                    92 
 94 /*                                                 93 /*
 95  * Currently used for the del_recall_lru and f     94  * Currently used for the del_recall_lru and file hash table.  In an
 96  * effort to decrease the scope of the client_     95  * effort to decrease the scope of the client_mutex, this spinlock may
 97  * eventually cover more:                          96  * eventually cover more:
 98  */                                                97  */
 99 static DEFINE_SPINLOCK(state_lock);                98 static DEFINE_SPINLOCK(state_lock);
100                                                    99 
101 enum nfsd4_st_mutex_lock_subclass {               100 enum nfsd4_st_mutex_lock_subclass {
102         OPEN_STATEID_MUTEX = 0,                   101         OPEN_STATEID_MUTEX = 0,
103         LOCK_STATEID_MUTEX = 1,                   102         LOCK_STATEID_MUTEX = 1,
104 };                                                103 };
105                                                   104 
106 /*                                                105 /*
107  * A waitqueue for all in-progress 4.0 CLOSE o    106  * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
108  * the refcount on the open stateid to drop.      107  * the refcount on the open stateid to drop.
109  */                                               108  */
110 static DECLARE_WAIT_QUEUE_HEAD(close_wq);         109 static DECLARE_WAIT_QUEUE_HEAD(close_wq);
111                                                   110 
112 /*                                                111 /*
113  * A waitqueue where a writer to clients/#/ctl    112  * A waitqueue where a writer to clients/#/ctl destroying a client can
114  * wait for cl_rpc_users to drop to 0 and then    113  * wait for cl_rpc_users to drop to 0 and then for the client to be
115  * unhashed.                                      114  * unhashed.
116  */                                               115  */
117 static DECLARE_WAIT_QUEUE_HEAD(expiry_wq);        116 static DECLARE_WAIT_QUEUE_HEAD(expiry_wq);
118                                                   117 
119 static struct kmem_cache *client_slab;            118 static struct kmem_cache *client_slab;
120 static struct kmem_cache *openowner_slab;         119 static struct kmem_cache *openowner_slab;
121 static struct kmem_cache *lockowner_slab;         120 static struct kmem_cache *lockowner_slab;
122 static struct kmem_cache *file_slab;              121 static struct kmem_cache *file_slab;
123 static struct kmem_cache *stateid_slab;           122 static struct kmem_cache *stateid_slab;
124 static struct kmem_cache *deleg_slab;             123 static struct kmem_cache *deleg_slab;
125 static struct kmem_cache *odstate_slab;           124 static struct kmem_cache *odstate_slab;
126                                                   125 
127 static void free_session(struct nfsd4_session     126 static void free_session(struct nfsd4_session *);
128                                                   127 
129 static const struct nfsd4_callback_ops nfsd4_c    128 static const struct nfsd4_callback_ops nfsd4_cb_recall_ops;
130 static const struct nfsd4_callback_ops nfsd4_c    129 static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops;
131 static const struct nfsd4_callback_ops nfsd4_c << 
132                                                   130 
133 static struct workqueue_struct *laundry_wq;       131 static struct workqueue_struct *laundry_wq;
134                                                   132 
135 int nfsd4_create_laundry_wq(void)                 133 int nfsd4_create_laundry_wq(void)
136 {                                                 134 {
137         int rc = 0;                               135         int rc = 0;
138                                                   136 
139         laundry_wq = alloc_workqueue("%s", WQ_    137         laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
140         if (laundry_wq == NULL)                   138         if (laundry_wq == NULL)
141                 rc = -ENOMEM;                     139                 rc = -ENOMEM;
142         return rc;                                140         return rc;
143 }                                                 141 }
144                                                   142 
145 void nfsd4_destroy_laundry_wq(void)               143 void nfsd4_destroy_laundry_wq(void)
146 {                                                 144 {
147         destroy_workqueue(laundry_wq);            145         destroy_workqueue(laundry_wq);
148 }                                                 146 }
149                                                   147 
150 static bool is_session_dead(struct nfsd4_sessi    148 static bool is_session_dead(struct nfsd4_session *ses)
151 {                                                 149 {
152         return ses->se_flags & NFS4_SESSION_DE    150         return ses->se_flags & NFS4_SESSION_DEAD;
153 }                                                 151 }
154                                                   152 
155 static __be32 mark_session_dead_locked(struct     153 static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
156 {                                                 154 {
157         if (atomic_read(&ses->se_ref) > ref_he    155         if (atomic_read(&ses->se_ref) > ref_held_by_me)
158                 return nfserr_jukebox;            156                 return nfserr_jukebox;
159         ses->se_flags |= NFS4_SESSION_DEAD;       157         ses->se_flags |= NFS4_SESSION_DEAD;
160         return nfs_ok;                            158         return nfs_ok;
161 }                                                 159 }
162                                                   160 
163 static bool is_client_expired(struct nfs4_clie    161 static bool is_client_expired(struct nfs4_client *clp)
164 {                                                 162 {
165         return clp->cl_time == 0;                 163         return clp->cl_time == 0;
166 }                                                 164 }
167                                                   165 
168 static void nfsd4_dec_courtesy_client_count(st    166 static void nfsd4_dec_courtesy_client_count(struct nfsd_net *nn,
169                                         struct    167                                         struct nfs4_client *clp)
170 {                                                 168 {
171         if (clp->cl_state != NFSD4_ACTIVE)        169         if (clp->cl_state != NFSD4_ACTIVE)
172                 atomic_add_unless(&nn->nfsd_co    170                 atomic_add_unless(&nn->nfsd_courtesy_clients, -1, 0);
173 }                                                 171 }
174                                                   172 
175 static __be32 get_client_locked(struct nfs4_cl    173 static __be32 get_client_locked(struct nfs4_client *clp)
176 {                                                 174 {
177         struct nfsd_net *nn = net_generic(clp-    175         struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
178                                                   176 
179         lockdep_assert_held(&nn->client_lock);    177         lockdep_assert_held(&nn->client_lock);
180                                                   178 
181         if (is_client_expired(clp))               179         if (is_client_expired(clp))
182                 return nfserr_expired;            180                 return nfserr_expired;
183         atomic_inc(&clp->cl_rpc_users);           181         atomic_inc(&clp->cl_rpc_users);
184         nfsd4_dec_courtesy_client_count(nn, cl    182         nfsd4_dec_courtesy_client_count(nn, clp);
185         clp->cl_state = NFSD4_ACTIVE;             183         clp->cl_state = NFSD4_ACTIVE;
186         return nfs_ok;                            184         return nfs_ok;
187 }                                                 185 }
188                                                   186 
189 /* must be called under the client_lock */        187 /* must be called under the client_lock */
190 static inline void                                188 static inline void
191 renew_client_locked(struct nfs4_client *clp)      189 renew_client_locked(struct nfs4_client *clp)
192 {                                                 190 {
193         struct nfsd_net *nn = net_generic(clp-    191         struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
194                                                   192 
195         if (is_client_expired(clp)) {             193         if (is_client_expired(clp)) {
196                 WARN_ON(1);                       194                 WARN_ON(1);
197                 printk("%s: client (clientid %    195                 printk("%s: client (clientid %08x/%08x) already expired\n",
198                         __func__,                 196                         __func__,
199                         clp->cl_clientid.cl_bo    197                         clp->cl_clientid.cl_boot,
200                         clp->cl_clientid.cl_id    198                         clp->cl_clientid.cl_id);
201                 return;                           199                 return;
202         }                                         200         }
203                                                   201 
204         list_move_tail(&clp->cl_lru, &nn->clie    202         list_move_tail(&clp->cl_lru, &nn->client_lru);
205         clp->cl_time = ktime_get_boottime_seco    203         clp->cl_time = ktime_get_boottime_seconds();
206         nfsd4_dec_courtesy_client_count(nn, cl    204         nfsd4_dec_courtesy_client_count(nn, clp);
207         clp->cl_state = NFSD4_ACTIVE;             205         clp->cl_state = NFSD4_ACTIVE;
208 }                                                 206 }
209                                                   207 
210 static void put_client_renew_locked(struct nfs    208 static void put_client_renew_locked(struct nfs4_client *clp)
211 {                                                 209 {
212         struct nfsd_net *nn = net_generic(clp-    210         struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
213                                                   211 
214         lockdep_assert_held(&nn->client_lock);    212         lockdep_assert_held(&nn->client_lock);
215                                                   213 
216         if (!atomic_dec_and_test(&clp->cl_rpc_    214         if (!atomic_dec_and_test(&clp->cl_rpc_users))
217                 return;                           215                 return;
218         if (!is_client_expired(clp))              216         if (!is_client_expired(clp))
219                 renew_client_locked(clp);         217                 renew_client_locked(clp);
220         else                                      218         else
221                 wake_up_all(&expiry_wq);          219                 wake_up_all(&expiry_wq);
222 }                                                 220 }
223                                                   221 
224 static void put_client_renew(struct nfs4_clien    222 static void put_client_renew(struct nfs4_client *clp)
225 {                                                 223 {
226         struct nfsd_net *nn = net_generic(clp-    224         struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
227                                                   225 
228         if (!atomic_dec_and_lock(&clp->cl_rpc_    226         if (!atomic_dec_and_lock(&clp->cl_rpc_users, &nn->client_lock))
229                 return;                           227                 return;
230         if (!is_client_expired(clp))              228         if (!is_client_expired(clp))
231                 renew_client_locked(clp);         229                 renew_client_locked(clp);
232         else                                      230         else
233                 wake_up_all(&expiry_wq);          231                 wake_up_all(&expiry_wq);
234         spin_unlock(&nn->client_lock);            232         spin_unlock(&nn->client_lock);
235 }                                                 233 }
236                                                   234 
237 static __be32 nfsd4_get_session_locked(struct     235 static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
238 {                                                 236 {
239         __be32 status;                            237         __be32 status;
240                                                   238 
241         if (is_session_dead(ses))                 239         if (is_session_dead(ses))
242                 return nfserr_badsession;         240                 return nfserr_badsession;
243         status = get_client_locked(ses->se_cli    241         status = get_client_locked(ses->se_client);
244         if (status)                               242         if (status)
245                 return status;                    243                 return status;
246         atomic_inc(&ses->se_ref);                 244         atomic_inc(&ses->se_ref);
247         return nfs_ok;                            245         return nfs_ok;
248 }                                                 246 }
249                                                   247 
250 static void nfsd4_put_session_locked(struct nf    248 static void nfsd4_put_session_locked(struct nfsd4_session *ses)
251 {                                                 249 {
252         struct nfs4_client *clp = ses->se_clie    250         struct nfs4_client *clp = ses->se_client;
253         struct nfsd_net *nn = net_generic(clp-    251         struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
254                                                   252 
255         lockdep_assert_held(&nn->client_lock);    253         lockdep_assert_held(&nn->client_lock);
256                                                   254 
257         if (atomic_dec_and_test(&ses->se_ref)     255         if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
258                 free_session(ses);                256                 free_session(ses);
259         put_client_renew_locked(clp);             257         put_client_renew_locked(clp);
260 }                                                 258 }
261                                                   259 
262 static void nfsd4_put_session(struct nfsd4_ses    260 static void nfsd4_put_session(struct nfsd4_session *ses)
263 {                                                 261 {
264         struct nfs4_client *clp = ses->se_clie    262         struct nfs4_client *clp = ses->se_client;
265         struct nfsd_net *nn = net_generic(clp-    263         struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
266                                                   264 
267         spin_lock(&nn->client_lock);              265         spin_lock(&nn->client_lock);
268         nfsd4_put_session_locked(ses);            266         nfsd4_put_session_locked(ses);
269         spin_unlock(&nn->client_lock);            267         spin_unlock(&nn->client_lock);
270 }                                                 268 }
271                                                   269 
272 static struct nfsd4_blocked_lock *                270 static struct nfsd4_blocked_lock *
273 find_blocked_lock(struct nfs4_lockowner *lo, s    271 find_blocked_lock(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
274                         struct nfsd_net *nn)      272                         struct nfsd_net *nn)
275 {                                                 273 {
276         struct nfsd4_blocked_lock *cur, *found    274         struct nfsd4_blocked_lock *cur, *found = NULL;
277                                                   275 
278         spin_lock(&nn->blocked_locks_lock);       276         spin_lock(&nn->blocked_locks_lock);
279         list_for_each_entry(cur, &lo->lo_block    277         list_for_each_entry(cur, &lo->lo_blocked, nbl_list) {
280                 if (fh_match(fh, &cur->nbl_fh)    278                 if (fh_match(fh, &cur->nbl_fh)) {
281                         list_del_init(&cur->nb    279                         list_del_init(&cur->nbl_list);
282                         WARN_ON(list_empty(&cu    280                         WARN_ON(list_empty(&cur->nbl_lru));
283                         list_del_init(&cur->nb    281                         list_del_init(&cur->nbl_lru);
284                         found = cur;              282                         found = cur;
285                         break;                    283                         break;
286                 }                                 284                 }
287         }                                         285         }
288         spin_unlock(&nn->blocked_locks_lock);     286         spin_unlock(&nn->blocked_locks_lock);
289         if (found)                                287         if (found)
290                 locks_delete_block(&found->nbl    288                 locks_delete_block(&found->nbl_lock);
291         return found;                             289         return found;
292 }                                                 290 }
293                                                   291 
294 static struct nfsd4_blocked_lock *                292 static struct nfsd4_blocked_lock *
295 find_or_allocate_block(struct nfs4_lockowner *    293 find_or_allocate_block(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
296                         struct nfsd_net *nn)      294                         struct nfsd_net *nn)
297 {                                                 295 {
298         struct nfsd4_blocked_lock *nbl;           296         struct nfsd4_blocked_lock *nbl;
299                                                   297 
300         nbl = find_blocked_lock(lo, fh, nn);      298         nbl = find_blocked_lock(lo, fh, nn);
301         if (!nbl) {                               299         if (!nbl) {
302                 nbl = kmalloc(sizeof(*nbl), GF    300                 nbl = kmalloc(sizeof(*nbl), GFP_KERNEL);
303                 if (nbl) {                        301                 if (nbl) {
304                         INIT_LIST_HEAD(&nbl->n    302                         INIT_LIST_HEAD(&nbl->nbl_list);
305                         INIT_LIST_HEAD(&nbl->n    303                         INIT_LIST_HEAD(&nbl->nbl_lru);
306                         fh_copy_shallow(&nbl->    304                         fh_copy_shallow(&nbl->nbl_fh, fh);
307                         locks_init_lock(&nbl->    305                         locks_init_lock(&nbl->nbl_lock);
308                         kref_init(&nbl->nbl_kr    306                         kref_init(&nbl->nbl_kref);
309                         nfsd4_init_cb(&nbl->nb    307                         nfsd4_init_cb(&nbl->nbl_cb, lo->lo_owner.so_client,
310                                         &nfsd4    308                                         &nfsd4_cb_notify_lock_ops,
311                                         NFSPRO    309                                         NFSPROC4_CLNT_CB_NOTIFY_LOCK);
312                 }                                 310                 }
313         }                                         311         }
314         return nbl;                               312         return nbl;
315 }                                                 313 }
316                                                   314 
317 static void                                       315 static void
318 free_nbl(struct kref *kref)                       316 free_nbl(struct kref *kref)
319 {                                                 317 {
320         struct nfsd4_blocked_lock *nbl;           318         struct nfsd4_blocked_lock *nbl;
321                                                   319 
322         nbl = container_of(kref, struct nfsd4_    320         nbl = container_of(kref, struct nfsd4_blocked_lock, nbl_kref);
323         locks_release_private(&nbl->nbl_lock); << 
324         kfree(nbl);                               321         kfree(nbl);
325 }                                                 322 }
326                                                   323 
327 static void                                       324 static void
328 free_blocked_lock(struct nfsd4_blocked_lock *n    325 free_blocked_lock(struct nfsd4_blocked_lock *nbl)
329 {                                                 326 {
330         locks_delete_block(&nbl->nbl_lock);       327         locks_delete_block(&nbl->nbl_lock);
                                                   >> 328         locks_release_private(&nbl->nbl_lock);
331         kref_put(&nbl->nbl_kref, free_nbl);       329         kref_put(&nbl->nbl_kref, free_nbl);
332 }                                                 330 }
333                                                   331 
334 static void                                       332 static void
335 remove_blocked_locks(struct nfs4_lockowner *lo    333 remove_blocked_locks(struct nfs4_lockowner *lo)
336 {                                                 334 {
337         struct nfs4_client *clp = lo->lo_owner    335         struct nfs4_client *clp = lo->lo_owner.so_client;
338         struct nfsd_net *nn = net_generic(clp-    336         struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
339         struct nfsd4_blocked_lock *nbl;           337         struct nfsd4_blocked_lock *nbl;
340         LIST_HEAD(reaplist);                      338         LIST_HEAD(reaplist);
341                                                   339 
342         /* Dequeue all blocked locks */           340         /* Dequeue all blocked locks */
343         spin_lock(&nn->blocked_locks_lock);       341         spin_lock(&nn->blocked_locks_lock);
344         while (!list_empty(&lo->lo_blocked)) {    342         while (!list_empty(&lo->lo_blocked)) {
345                 nbl = list_first_entry(&lo->lo    343                 nbl = list_first_entry(&lo->lo_blocked,
346                                         struct    344                                         struct nfsd4_blocked_lock,
347                                         nbl_li    345                                         nbl_list);
348                 list_del_init(&nbl->nbl_list);    346                 list_del_init(&nbl->nbl_list);
349                 WARN_ON(list_empty(&nbl->nbl_l    347                 WARN_ON(list_empty(&nbl->nbl_lru));
350                 list_move(&nbl->nbl_lru, &reap    348                 list_move(&nbl->nbl_lru, &reaplist);
351         }                                         349         }
352         spin_unlock(&nn->blocked_locks_lock);     350         spin_unlock(&nn->blocked_locks_lock);
353                                                   351 
354         /* Now free them */                       352         /* Now free them */
355         while (!list_empty(&reaplist)) {          353         while (!list_empty(&reaplist)) {
356                 nbl = list_first_entry(&reapli    354                 nbl = list_first_entry(&reaplist, struct nfsd4_blocked_lock,
357                                         nbl_lr    355                                         nbl_lru);
358                 list_del_init(&nbl->nbl_lru);     356                 list_del_init(&nbl->nbl_lru);
359                 free_blocked_lock(nbl);           357                 free_blocked_lock(nbl);
360         }                                         358         }
361 }                                                 359 }
362                                                   360 
363 static void                                       361 static void
364 nfsd4_cb_notify_lock_prepare(struct nfsd4_call    362 nfsd4_cb_notify_lock_prepare(struct nfsd4_callback *cb)
365 {                                                 363 {
366         struct nfsd4_blocked_lock       *nbl =    364         struct nfsd4_blocked_lock       *nbl = container_of(cb,
367                                                   365                                                 struct nfsd4_blocked_lock, nbl_cb);
368         locks_delete_block(&nbl->nbl_lock);       366         locks_delete_block(&nbl->nbl_lock);
369 }                                                 367 }
370                                                   368 
371 static int                                        369 static int
372 nfsd4_cb_notify_lock_done(struct nfsd4_callbac    370 nfsd4_cb_notify_lock_done(struct nfsd4_callback *cb, struct rpc_task *task)
373 {                                                 371 {
374         trace_nfsd_cb_notify_lock_done(&zero_s    372         trace_nfsd_cb_notify_lock_done(&zero_stateid, task);
375                                                   373 
376         /*                                        374         /*
377          * Since this is just an optimization,    375          * Since this is just an optimization, we don't try very hard if it
378          * turns out not to succeed. We'll req    376          * turns out not to succeed. We'll requeue it on NFS4ERR_DELAY, and
379          * just quit trying on anything else.     377          * just quit trying on anything else.
380          */                                       378          */
381         switch (task->tk_status) {                379         switch (task->tk_status) {
382         case -NFS4ERR_DELAY:                      380         case -NFS4ERR_DELAY:
383                 rpc_delay(task, 1 * HZ);          381                 rpc_delay(task, 1 * HZ);
384                 return 0;                         382                 return 0;
385         default:                                  383         default:
386                 return 1;                         384                 return 1;
387         }                                         385         }
388 }                                                 386 }
389                                                   387 
390 static void                                       388 static void
391 nfsd4_cb_notify_lock_release(struct nfsd4_call    389 nfsd4_cb_notify_lock_release(struct nfsd4_callback *cb)
392 {                                                 390 {
393         struct nfsd4_blocked_lock       *nbl =    391         struct nfsd4_blocked_lock       *nbl = container_of(cb,
394                                                   392                                                 struct nfsd4_blocked_lock, nbl_cb);
395                                                   393 
396         free_blocked_lock(nbl);                   394         free_blocked_lock(nbl);
397 }                                                 395 }
398                                                   396 
399 static const struct nfsd4_callback_ops nfsd4_c    397 static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops = {
400         .prepare        = nfsd4_cb_notify_lock    398         .prepare        = nfsd4_cb_notify_lock_prepare,
401         .done           = nfsd4_cb_notify_lock    399         .done           = nfsd4_cb_notify_lock_done,
402         .release        = nfsd4_cb_notify_lock    400         .release        = nfsd4_cb_notify_lock_release,
403         .opcode         = OP_CB_NOTIFY_LOCK,   << 
404 };                                                401 };
405                                                   402 
406 /*                                                403 /*
407  * We store the NONE, READ, WRITE, and BOTH bi    404  * We store the NONE, READ, WRITE, and BOTH bits separately in the
408  * st_{access,deny}_bmap field of the stateid,    405  * st_{access,deny}_bmap field of the stateid, in order to track not
409  * only what share bits are currently in force    406  * only what share bits are currently in force, but also what
410  * combinations of share bits previous opens h    407  * combinations of share bits previous opens have used.  This allows us
411  * to enforce the recommendation in               408  * to enforce the recommendation in
412  * https://datatracker.ietf.org/doc/html/rfc75    409  * https://datatracker.ietf.org/doc/html/rfc7530#section-16.19.4 that
413  * the server return an error if the client at    410  * the server return an error if the client attempt to downgrade to a
414  * combination of share bits not explicable by    411  * combination of share bits not explicable by closing some of its
415  * previous opens.                                412  * previous opens.
416  *                                                413  *
417  * This enforcement is arguably incomplete, si    414  * This enforcement is arguably incomplete, since we don't keep
418  * track of access/deny bit combinations; so,     415  * track of access/deny bit combinations; so, e.g., we allow:
419  *                                                416  *
420  *      OPEN allow read, deny write               417  *      OPEN allow read, deny write
421  *      OPEN allow both, deny none                418  *      OPEN allow both, deny none
422  *      DOWNGRADE allow read, deny none           419  *      DOWNGRADE allow read, deny none
423  *                                                420  *
424  * which we should reject.                        421  * which we should reject.
425  *                                                422  *
426  * But you could also argue that our current c    423  * But you could also argue that our current code is already overkill,
427  * since it only exists to return NFS4ERR_INVA    424  * since it only exists to return NFS4ERR_INVAL on incorrect client
428  * behavior.                                      425  * behavior.
429  */                                               426  */
430 static unsigned int                               427 static unsigned int
431 bmap_to_share_mode(unsigned long bmap)            428 bmap_to_share_mode(unsigned long bmap)
432 {                                                 429 {
433         int i;                                    430         int i;
434         unsigned int access = 0;                  431         unsigned int access = 0;
435                                                   432 
436         for (i = 1; i < 4; i++) {                 433         for (i = 1; i < 4; i++) {
437                 if (test_bit(i, &bmap))           434                 if (test_bit(i, &bmap))
438                         access |= i;              435                         access |= i;
439         }                                         436         }
440         return access;                            437         return access;
441 }                                                 438 }
442                                                   439 
443 /* set share access for a given stateid */        440 /* set share access for a given stateid */
444 static inline void                                441 static inline void
445 set_access(u32 access, struct nfs4_ol_stateid     442 set_access(u32 access, struct nfs4_ol_stateid *stp)
446 {                                                 443 {
447         unsigned char mask = 1 << access;         444         unsigned char mask = 1 << access;
448                                                   445 
449         WARN_ON_ONCE(access > NFS4_SHARE_ACCES    446         WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
450         stp->st_access_bmap |= mask;              447         stp->st_access_bmap |= mask;
451 }                                                 448 }
452                                                   449 
453 /* clear share access for a given stateid */      450 /* clear share access for a given stateid */
454 static inline void                                451 static inline void
455 clear_access(u32 access, struct nfs4_ol_statei    452 clear_access(u32 access, struct nfs4_ol_stateid *stp)
456 {                                                 453 {
457         unsigned char mask = 1 << access;         454         unsigned char mask = 1 << access;
458                                                   455 
459         WARN_ON_ONCE(access > NFS4_SHARE_ACCES    456         WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
460         stp->st_access_bmap &= ~mask;             457         stp->st_access_bmap &= ~mask;
461 }                                                 458 }
462                                                   459 
463 /* test whether a given stateid has access */     460 /* test whether a given stateid has access */
464 static inline bool                                461 static inline bool
465 test_access(u32 access, struct nfs4_ol_stateid    462 test_access(u32 access, struct nfs4_ol_stateid *stp)
466 {                                                 463 {
467         unsigned char mask = 1 << access;         464         unsigned char mask = 1 << access;
468                                                   465 
469         return (bool)(stp->st_access_bmap & ma    466         return (bool)(stp->st_access_bmap & mask);
470 }                                                 467 }
471                                                   468 
472 /* set share deny for a given stateid */          469 /* set share deny for a given stateid */
473 static inline void                                470 static inline void
474 set_deny(u32 deny, struct nfs4_ol_stateid *stp    471 set_deny(u32 deny, struct nfs4_ol_stateid *stp)
475 {                                                 472 {
476         unsigned char mask = 1 << deny;           473         unsigned char mask = 1 << deny;
477                                                   474 
478         WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BO    475         WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
479         stp->st_deny_bmap |= mask;                476         stp->st_deny_bmap |= mask;
480 }                                                 477 }
481                                                   478 
482 /* clear share deny for a given stateid */        479 /* clear share deny for a given stateid */
483 static inline void                                480 static inline void
484 clear_deny(u32 deny, struct nfs4_ol_stateid *s    481 clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
485 {                                                 482 {
486         unsigned char mask = 1 << deny;           483         unsigned char mask = 1 << deny;
487                                                   484 
488         WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BO    485         WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
489         stp->st_deny_bmap &= ~mask;               486         stp->st_deny_bmap &= ~mask;
490 }                                                 487 }
491                                                   488 
492 /* test whether a given stateid is denying spe    489 /* test whether a given stateid is denying specific access */
493 static inline bool                                490 static inline bool
494 test_deny(u32 deny, struct nfs4_ol_stateid *st    491 test_deny(u32 deny, struct nfs4_ol_stateid *stp)
495 {                                                 492 {
496         unsigned char mask = 1 << deny;           493         unsigned char mask = 1 << deny;
497                                                   494 
498         return (bool)(stp->st_deny_bmap & mask    495         return (bool)(stp->st_deny_bmap & mask);
499 }                                                 496 }
500                                                   497 
501 static int nfs4_access_to_omode(u32 access)       498 static int nfs4_access_to_omode(u32 access)
502 {                                                 499 {
503         switch (access & NFS4_SHARE_ACCESS_BOT    500         switch (access & NFS4_SHARE_ACCESS_BOTH) {
504         case NFS4_SHARE_ACCESS_READ:              501         case NFS4_SHARE_ACCESS_READ:
505                 return O_RDONLY;                  502                 return O_RDONLY;
506         case NFS4_SHARE_ACCESS_WRITE:             503         case NFS4_SHARE_ACCESS_WRITE:
507                 return O_WRONLY;                  504                 return O_WRONLY;
508         case NFS4_SHARE_ACCESS_BOTH:              505         case NFS4_SHARE_ACCESS_BOTH:
509                 return O_RDWR;                    506                 return O_RDWR;
510         }                                         507         }
511         WARN_ON_ONCE(1);                          508         WARN_ON_ONCE(1);
512         return O_RDONLY;                          509         return O_RDONLY;
513 }                                                 510 }
514                                                   511 
515 static inline int                                 512 static inline int
516 access_permit_read(struct nfs4_ol_stateid *stp    513 access_permit_read(struct nfs4_ol_stateid *stp)
517 {                                                 514 {
518         return test_access(NFS4_SHARE_ACCESS_R    515         return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
519                 test_access(NFS4_SHARE_ACCESS_    516                 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
520                 test_access(NFS4_SHARE_ACCESS_    517                 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
521 }                                                 518 }
522                                                   519 
523 static inline int                                 520 static inline int
524 access_permit_write(struct nfs4_ol_stateid *st    521 access_permit_write(struct nfs4_ol_stateid *stp)
525 {                                                 522 {
526         return test_access(NFS4_SHARE_ACCESS_W    523         return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
527                 test_access(NFS4_SHARE_ACCESS_    524                 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
528 }                                                 525 }
529                                                   526 
530 static inline struct nfs4_stateowner *            527 static inline struct nfs4_stateowner *
531 nfs4_get_stateowner(struct nfs4_stateowner *so    528 nfs4_get_stateowner(struct nfs4_stateowner *sop)
532 {                                                 529 {
533         atomic_inc(&sop->so_count);               530         atomic_inc(&sop->so_count);
534         return sop;                               531         return sop;
535 }                                                 532 }
536                                                   533 
537 static int                                        534 static int
538 same_owner_str(struct nfs4_stateowner *sop, st    535 same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
539 {                                                 536 {
540         return (sop->so_owner.len == owner->le    537         return (sop->so_owner.len == owner->len) &&
541                 0 == memcmp(sop->so_owner.data    538                 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
542 }                                                 539 }
543                                                   540 
544 static struct nfs4_openowner *                    541 static struct nfs4_openowner *
545 find_openstateowner_str(unsigned int hashval,  !! 542 find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
546                         struct nfs4_client *cl    543                         struct nfs4_client *clp)
547 {                                                 544 {
548         struct nfs4_stateowner *so;               545         struct nfs4_stateowner *so;
549                                                   546 
550         lockdep_assert_held(&clp->cl_lock);       547         lockdep_assert_held(&clp->cl_lock);
551                                                   548 
552         list_for_each_entry(so, &clp->cl_owner    549         list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
553                             so_strhash) {         550                             so_strhash) {
554                 if (!so->so_is_open_owner)        551                 if (!so->so_is_open_owner)
555                         continue;                 552                         continue;
556                 if (same_owner_str(so, &open->    553                 if (same_owner_str(so, &open->op_owner))
557                         return openowner(nfs4_    554                         return openowner(nfs4_get_stateowner(so));
558         }                                         555         }
559         return NULL;                              556         return NULL;
560 }                                                 557 }
561                                                   558 
                                                   >> 559 static struct nfs4_openowner *
                                                   >> 560 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
                                                   >> 561                         struct nfs4_client *clp)
                                                   >> 562 {
                                                   >> 563         struct nfs4_openowner *oo;
                                                   >> 564 
                                                   >> 565         spin_lock(&clp->cl_lock);
                                                   >> 566         oo = find_openstateowner_str_locked(hashval, open, clp);
                                                   >> 567         spin_unlock(&clp->cl_lock);
                                                   >> 568         return oo;
                                                   >> 569 }
                                                   >> 570 
562 static inline u32                                 571 static inline u32
563 opaque_hashval(const void *ptr, int nbytes)       572 opaque_hashval(const void *ptr, int nbytes)
564 {                                                 573 {
565         unsigned char *cptr = (unsigned char *    574         unsigned char *cptr = (unsigned char *) ptr;
566                                                   575 
567         u32 x = 0;                                576         u32 x = 0;
568         while (nbytes--) {                        577         while (nbytes--) {
569                 x *= 37;                          578                 x *= 37;
570                 x += *cptr++;                     579                 x += *cptr++;
571         }                                         580         }
572         return x;                                 581         return x;
573 }                                                 582 }
574                                                   583 
575 static void nfsd4_free_file_rcu(struct rcu_hea    584 static void nfsd4_free_file_rcu(struct rcu_head *rcu)
576 {                                                 585 {
577         struct nfs4_file *fp = container_of(rc    586         struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
578                                                   587 
579         kmem_cache_free(file_slab, fp);           588         kmem_cache_free(file_slab, fp);
580 }                                                 589 }
581                                                   590 
582 void                                              591 void
583 put_nfs4_file(struct nfs4_file *fi)               592 put_nfs4_file(struct nfs4_file *fi)
584 {                                                 593 {
585         if (refcount_dec_and_test(&fi->fi_ref)    594         if (refcount_dec_and_test(&fi->fi_ref)) {
586                 nfsd4_file_hash_remove(fi);       595                 nfsd4_file_hash_remove(fi);
587                 WARN_ON_ONCE(!list_empty(&fi->    596                 WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
588                 WARN_ON_ONCE(!list_empty(&fi->    597                 WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
589                 call_rcu(&fi->fi_rcu, nfsd4_fr    598                 call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
590         }                                         599         }
591 }                                                 600 }
592                                                   601 
593 static struct nfsd_file *                         602 static struct nfsd_file *
594 find_writeable_file_locked(struct nfs4_file *f    603 find_writeable_file_locked(struct nfs4_file *f)
595 {                                                 604 {
596         struct nfsd_file *ret;                    605         struct nfsd_file *ret;
597                                                   606 
598         lockdep_assert_held(&f->fi_lock);         607         lockdep_assert_held(&f->fi_lock);
599                                                   608 
600         ret = nfsd_file_get(f->fi_fds[O_WRONLY    609         ret = nfsd_file_get(f->fi_fds[O_WRONLY]);
601         if (!ret)                                 610         if (!ret)
602                 ret = nfsd_file_get(f->fi_fds[    611                 ret = nfsd_file_get(f->fi_fds[O_RDWR]);
603         return ret;                               612         return ret;
604 }                                                 613 }
605                                                   614 
606 static struct nfsd_file *                         615 static struct nfsd_file *
607 find_writeable_file(struct nfs4_file *f)          616 find_writeable_file(struct nfs4_file *f)
608 {                                                 617 {
609         struct nfsd_file *ret;                    618         struct nfsd_file *ret;
610                                                   619 
611         spin_lock(&f->fi_lock);                   620         spin_lock(&f->fi_lock);
612         ret = find_writeable_file_locked(f);      621         ret = find_writeable_file_locked(f);
613         spin_unlock(&f->fi_lock);                 622         spin_unlock(&f->fi_lock);
614                                                   623 
615         return ret;                               624         return ret;
616 }                                                 625 }
617                                                   626 
618 static struct nfsd_file *                         627 static struct nfsd_file *
619 find_readable_file_locked(struct nfs4_file *f)    628 find_readable_file_locked(struct nfs4_file *f)
620 {                                                 629 {
621         struct nfsd_file *ret;                    630         struct nfsd_file *ret;
622                                                   631 
623         lockdep_assert_held(&f->fi_lock);         632         lockdep_assert_held(&f->fi_lock);
624                                                   633 
625         ret = nfsd_file_get(f->fi_fds[O_RDONLY    634         ret = nfsd_file_get(f->fi_fds[O_RDONLY]);
626         if (!ret)                                 635         if (!ret)
627                 ret = nfsd_file_get(f->fi_fds[    636                 ret = nfsd_file_get(f->fi_fds[O_RDWR]);
628         return ret;                               637         return ret;
629 }                                                 638 }
630                                                   639 
631 static struct nfsd_file *                         640 static struct nfsd_file *
632 find_readable_file(struct nfs4_file *f)           641 find_readable_file(struct nfs4_file *f)
633 {                                                 642 {
634         struct nfsd_file *ret;                    643         struct nfsd_file *ret;
635                                                   644 
636         spin_lock(&f->fi_lock);                   645         spin_lock(&f->fi_lock);
637         ret = find_readable_file_locked(f);       646         ret = find_readable_file_locked(f);
638         spin_unlock(&f->fi_lock);                 647         spin_unlock(&f->fi_lock);
639                                                   648 
640         return ret;                               649         return ret;
641 }                                                 650 }
642                                                   651 
643 static struct nfsd_file *                         652 static struct nfsd_file *
644 find_rw_file(struct nfs4_file *f)                 653 find_rw_file(struct nfs4_file *f)
645 {                                                 654 {
646         struct nfsd_file *ret;                    655         struct nfsd_file *ret;
647                                                   656 
648         spin_lock(&f->fi_lock);                   657         spin_lock(&f->fi_lock);
649         ret = nfsd_file_get(f->fi_fds[O_RDWR])    658         ret = nfsd_file_get(f->fi_fds[O_RDWR]);
650         spin_unlock(&f->fi_lock);                 659         spin_unlock(&f->fi_lock);
651                                                   660 
652         return ret;                               661         return ret;
653 }                                                 662 }
654                                                   663 
655 struct nfsd_file *                                664 struct nfsd_file *
656 find_any_file(struct nfs4_file *f)                665 find_any_file(struct nfs4_file *f)
657 {                                                 666 {
658         struct nfsd_file *ret;                    667         struct nfsd_file *ret;
659                                                   668 
660         if (!f)                                   669         if (!f)
661                 return NULL;                      670                 return NULL;
662         spin_lock(&f->fi_lock);                   671         spin_lock(&f->fi_lock);
663         ret = nfsd_file_get(f->fi_fds[O_RDWR])    672         ret = nfsd_file_get(f->fi_fds[O_RDWR]);
664         if (!ret) {                               673         if (!ret) {
665                 ret = nfsd_file_get(f->fi_fds[    674                 ret = nfsd_file_get(f->fi_fds[O_WRONLY]);
666                 if (!ret)                         675                 if (!ret)
667                         ret = nfsd_file_get(f-    676                         ret = nfsd_file_get(f->fi_fds[O_RDONLY]);
668         }                                         677         }
669         spin_unlock(&f->fi_lock);                 678         spin_unlock(&f->fi_lock);
670         return ret;                               679         return ret;
671 }                                                 680 }
672                                                   681 
673 static struct nfsd_file *find_any_file_locked(    682 static struct nfsd_file *find_any_file_locked(struct nfs4_file *f)
674 {                                                 683 {
675         lockdep_assert_held(&f->fi_lock);         684         lockdep_assert_held(&f->fi_lock);
676                                                   685 
677         if (f->fi_fds[O_RDWR])                    686         if (f->fi_fds[O_RDWR])
678                 return f->fi_fds[O_RDWR];         687                 return f->fi_fds[O_RDWR];
679         if (f->fi_fds[O_WRONLY])                  688         if (f->fi_fds[O_WRONLY])
680                 return f->fi_fds[O_WRONLY];       689                 return f->fi_fds[O_WRONLY];
681         if (f->fi_fds[O_RDONLY])                  690         if (f->fi_fds[O_RDONLY])
682                 return f->fi_fds[O_RDONLY];       691                 return f->fi_fds[O_RDONLY];
683         return NULL;                              692         return NULL;
684 }                                                 693 }
685                                                   694 
686 static atomic_long_t num_delegations;             695 static atomic_long_t num_delegations;
687 unsigned long max_delegations;                    696 unsigned long max_delegations;
688                                                   697 
689 /*                                                698 /*
690  * Open owner state (share locks)                 699  * Open owner state (share locks)
691  */                                               700  */
692                                                   701 
693 /* hash tables for lock and open owners */        702 /* hash tables for lock and open owners */
694 #define OWNER_HASH_BITS              8            703 #define OWNER_HASH_BITS              8
695 #define OWNER_HASH_SIZE             (1 << OWNE    704 #define OWNER_HASH_SIZE             (1 << OWNER_HASH_BITS)
696 #define OWNER_HASH_MASK             (OWNER_HAS    705 #define OWNER_HASH_MASK             (OWNER_HASH_SIZE - 1)
697                                                   706 
698 static unsigned int ownerstr_hashval(struct xd    707 static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
699 {                                                 708 {
700         unsigned int ret;                         709         unsigned int ret;
701                                                   710 
702         ret = opaque_hashval(ownername->data,     711         ret = opaque_hashval(ownername->data, ownername->len);
703         return ret & OWNER_HASH_MASK;             712         return ret & OWNER_HASH_MASK;
704 }                                                 713 }
705                                                   714 
706 static struct rhltable nfs4_file_rhltable ____    715 static struct rhltable nfs4_file_rhltable ____cacheline_aligned_in_smp;
707                                                   716 
708 static const struct rhashtable_params nfs4_fil    717 static const struct rhashtable_params nfs4_file_rhash_params = {
709         .key_len                = sizeof_field    718         .key_len                = sizeof_field(struct nfs4_file, fi_inode),
710         .key_offset             = offsetof(str    719         .key_offset             = offsetof(struct nfs4_file, fi_inode),
711         .head_offset            = offsetof(str    720         .head_offset            = offsetof(struct nfs4_file, fi_rlist),
712                                                   721 
713         /*                                        722         /*
714          * Start with a single page hash table    723          * Start with a single page hash table to reduce resizing churn
715          * on light workloads.                    724          * on light workloads.
716          */                                       725          */
717         .min_size               = 256,            726         .min_size               = 256,
718         .automatic_shrinking    = true,           727         .automatic_shrinking    = true,
719 };                                                728 };
720                                                   729 
721 /*                                                730 /*
722  * Check if courtesy clients have conflicting     731  * Check if courtesy clients have conflicting access and resolve it if possible
723  *                                                732  *
724  * access:  is op_share_access if share_access    733  * access:  is op_share_access if share_access is true.
725  *          Check if access mode, op_share_acc    734  *          Check if access mode, op_share_access, would conflict with
726  *          the current deny mode of the file     735  *          the current deny mode of the file 'fp'.
727  * access:  is op_share_deny if share_access i    736  * access:  is op_share_deny if share_access is false.
728  *          Check if the deny mode, op_share_d    737  *          Check if the deny mode, op_share_deny, would conflict with
729  *          current access of the file 'fp'.      738  *          current access of the file 'fp'.
730  * stp:     skip checking this entry.             739  * stp:     skip checking this entry.
731  * new_stp: normal open, not open upgrade.        740  * new_stp: normal open, not open upgrade.
732  *                                                741  *
733  * Function returns:                              742  * Function returns:
734  *      false - access/deny mode conflict with    743  *      false - access/deny mode conflict with normal client.
735  *      true  - no conflict or conflict with c    744  *      true  - no conflict or conflict with courtesy client(s) is resolved.
736  */                                               745  */
737 static bool                                       746 static bool
738 nfs4_resolve_deny_conflicts_locked(struct nfs4    747 nfs4_resolve_deny_conflicts_locked(struct nfs4_file *fp, bool new_stp,
739                 struct nfs4_ol_stateid *stp, u    748                 struct nfs4_ol_stateid *stp, u32 access, bool share_access)
740 {                                                 749 {
741         struct nfs4_ol_stateid *st;               750         struct nfs4_ol_stateid *st;
742         bool resolvable = true;                   751         bool resolvable = true;
743         unsigned char bmap;                       752         unsigned char bmap;
744         struct nfsd_net *nn;                      753         struct nfsd_net *nn;
745         struct nfs4_client *clp;                  754         struct nfs4_client *clp;
746                                                   755 
747         lockdep_assert_held(&fp->fi_lock);        756         lockdep_assert_held(&fp->fi_lock);
748         list_for_each_entry(st, &fp->fi_statei    757         list_for_each_entry(st, &fp->fi_stateids, st_perfile) {
749                 /* ignore lock stateid */         758                 /* ignore lock stateid */
750                 if (st->st_openstp)               759                 if (st->st_openstp)
751                         continue;                 760                         continue;
752                 if (st == stp && new_stp)         761                 if (st == stp && new_stp)
753                         continue;                 762                         continue;
754                 /* check file access against d    763                 /* check file access against deny mode or vice versa */
755                 bmap = share_access ? st->st_d    764                 bmap = share_access ? st->st_deny_bmap : st->st_access_bmap;
756                 if (!(access & bmap_to_share_m    765                 if (!(access & bmap_to_share_mode(bmap)))
757                         continue;                 766                         continue;
758                 clp = st->st_stid.sc_client;      767                 clp = st->st_stid.sc_client;
759                 if (try_to_expire_client(clp))    768                 if (try_to_expire_client(clp))
760                         continue;                 769                         continue;
761                 resolvable = false;               770                 resolvable = false;
762                 break;                            771                 break;
763         }                                         772         }
764         if (resolvable) {                         773         if (resolvable) {
765                 clp = stp->st_stid.sc_client;     774                 clp = stp->st_stid.sc_client;
766                 nn = net_generic(clp->net, nfs    775                 nn = net_generic(clp->net, nfsd_net_id);
767                 mod_delayed_work(laundry_wq, &    776                 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
768         }                                         777         }
769         return resolvable;                        778         return resolvable;
770 }                                                 779 }
771                                                   780 
772 static void                                       781 static void
773 __nfs4_file_get_access(struct nfs4_file *fp, u    782 __nfs4_file_get_access(struct nfs4_file *fp, u32 access)
774 {                                                 783 {
775         lockdep_assert_held(&fp->fi_lock);        784         lockdep_assert_held(&fp->fi_lock);
776                                                   785 
777         if (access & NFS4_SHARE_ACCESS_WRITE)     786         if (access & NFS4_SHARE_ACCESS_WRITE)
778                 atomic_inc(&fp->fi_access[O_WR    787                 atomic_inc(&fp->fi_access[O_WRONLY]);
779         if (access & NFS4_SHARE_ACCESS_READ)      788         if (access & NFS4_SHARE_ACCESS_READ)
780                 atomic_inc(&fp->fi_access[O_RD    789                 atomic_inc(&fp->fi_access[O_RDONLY]);
781 }                                                 790 }
782                                                   791 
783 static __be32                                     792 static __be32
784 nfs4_file_get_access(struct nfs4_file *fp, u32    793 nfs4_file_get_access(struct nfs4_file *fp, u32 access)
785 {                                                 794 {
786         lockdep_assert_held(&fp->fi_lock);        795         lockdep_assert_held(&fp->fi_lock);
787                                                   796 
788         /* Does this access mode make sense? *    797         /* Does this access mode make sense? */
789         if (access & ~NFS4_SHARE_ACCESS_BOTH)     798         if (access & ~NFS4_SHARE_ACCESS_BOTH)
790                 return nfserr_inval;              799                 return nfserr_inval;
791                                                   800 
792         /* Does it conflict with a deny mode a    801         /* Does it conflict with a deny mode already set? */
793         if ((access & fp->fi_share_deny) != 0)    802         if ((access & fp->fi_share_deny) != 0)
794                 return nfserr_share_denied;       803                 return nfserr_share_denied;
795                                                   804 
796         __nfs4_file_get_access(fp, access);       805         __nfs4_file_get_access(fp, access);
797         return nfs_ok;                            806         return nfs_ok;
798 }                                                 807 }
799                                                   808 
800 static __be32 nfs4_file_check_deny(struct nfs4    809 static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
801 {                                                 810 {
802         /* Common case is that there is no den    811         /* Common case is that there is no deny mode. */
803         if (deny) {                               812         if (deny) {
804                 /* Does this deny mode make se    813                 /* Does this deny mode make sense? */
805                 if (deny & ~NFS4_SHARE_DENY_BO    814                 if (deny & ~NFS4_SHARE_DENY_BOTH)
806                         return nfserr_inval;      815                         return nfserr_inval;
807                                                   816 
808                 if ((deny & NFS4_SHARE_DENY_RE    817                 if ((deny & NFS4_SHARE_DENY_READ) &&
809                     atomic_read(&fp->fi_access    818                     atomic_read(&fp->fi_access[O_RDONLY]))
810                         return nfserr_share_de    819                         return nfserr_share_denied;
811                                                   820 
812                 if ((deny & NFS4_SHARE_DENY_WR    821                 if ((deny & NFS4_SHARE_DENY_WRITE) &&
813                     atomic_read(&fp->fi_access    822                     atomic_read(&fp->fi_access[O_WRONLY]))
814                         return nfserr_share_de    823                         return nfserr_share_denied;
815         }                                         824         }
816         return nfs_ok;                            825         return nfs_ok;
817 }                                                 826 }
818                                                   827 
819 static void __nfs4_file_put_access(struct nfs4    828 static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
820 {                                                 829 {
821         might_lock(&fp->fi_lock);                 830         might_lock(&fp->fi_lock);
822                                                   831 
823         if (atomic_dec_and_lock(&fp->fi_access    832         if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
824                 struct nfsd_file *f1 = NULL;      833                 struct nfsd_file *f1 = NULL;
825                 struct nfsd_file *f2 = NULL;      834                 struct nfsd_file *f2 = NULL;
826                                                   835 
827                 swap(f1, fp->fi_fds[oflag]);      836                 swap(f1, fp->fi_fds[oflag]);
828                 if (atomic_read(&fp->fi_access    837                 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
829                         swap(f2, fp->fi_fds[O_    838                         swap(f2, fp->fi_fds[O_RDWR]);
830                 spin_unlock(&fp->fi_lock);        839                 spin_unlock(&fp->fi_lock);
831                 if (f1)                           840                 if (f1)
832                         nfsd_file_put(f1);        841                         nfsd_file_put(f1);
833                 if (f2)                           842                 if (f2)
834                         nfsd_file_put(f2);        843                         nfsd_file_put(f2);
835         }                                         844         }
836 }                                                 845 }
837                                                   846 
838 static void nfs4_file_put_access(struct nfs4_f    847 static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
839 {                                                 848 {
840         WARN_ON_ONCE(access & ~NFS4_SHARE_ACCE    849         WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
841                                                   850 
842         if (access & NFS4_SHARE_ACCESS_WRITE)     851         if (access & NFS4_SHARE_ACCESS_WRITE)
843                 __nfs4_file_put_access(fp, O_W    852                 __nfs4_file_put_access(fp, O_WRONLY);
844         if (access & NFS4_SHARE_ACCESS_READ)      853         if (access & NFS4_SHARE_ACCESS_READ)
845                 __nfs4_file_put_access(fp, O_R    854                 __nfs4_file_put_access(fp, O_RDONLY);
846 }                                                 855 }
847                                                   856 
848 /*                                                857 /*
849  * Allocate a new open/delegation state counte    858  * Allocate a new open/delegation state counter. This is needed for
850  * pNFS for proper return on close semantics.     859  * pNFS for proper return on close semantics.
851  *                                                860  *
852  * Note that we only allocate it for pNFS-enab    861  * Note that we only allocate it for pNFS-enabled exports, otherwise
853  * all pointers to struct nfs4_clnt_odstate ar    862  * all pointers to struct nfs4_clnt_odstate are always NULL.
854  */                                               863  */
855 static struct nfs4_clnt_odstate *                 864 static struct nfs4_clnt_odstate *
856 alloc_clnt_odstate(struct nfs4_client *clp)       865 alloc_clnt_odstate(struct nfs4_client *clp)
857 {                                                 866 {
858         struct nfs4_clnt_odstate *co;             867         struct nfs4_clnt_odstate *co;
859                                                   868 
860         co = kmem_cache_zalloc(odstate_slab, G    869         co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
861         if (co) {                                 870         if (co) {
862                 co->co_client = clp;              871                 co->co_client = clp;
863                 refcount_set(&co->co_odcount,     872                 refcount_set(&co->co_odcount, 1);
864         }                                         873         }
865         return co;                                874         return co;
866 }                                                 875 }
867                                                   876 
868 static void                                       877 static void
869 hash_clnt_odstate_locked(struct nfs4_clnt_odst    878 hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
870 {                                                 879 {
871         struct nfs4_file *fp = co->co_file;       880         struct nfs4_file *fp = co->co_file;
872                                                   881 
873         lockdep_assert_held(&fp->fi_lock);        882         lockdep_assert_held(&fp->fi_lock);
874         list_add(&co->co_perfile, &fp->fi_clnt    883         list_add(&co->co_perfile, &fp->fi_clnt_odstate);
875 }                                                 884 }
876                                                   885 
877 static inline void                                886 static inline void
878 get_clnt_odstate(struct nfs4_clnt_odstate *co)    887 get_clnt_odstate(struct nfs4_clnt_odstate *co)
879 {                                                 888 {
880         if (co)                                   889         if (co)
881                 refcount_inc(&co->co_odcount);    890                 refcount_inc(&co->co_odcount);
882 }                                                 891 }
883                                                   892 
884 static void                                       893 static void
885 put_clnt_odstate(struct nfs4_clnt_odstate *co)    894 put_clnt_odstate(struct nfs4_clnt_odstate *co)
886 {                                                 895 {
887         struct nfs4_file *fp;                     896         struct nfs4_file *fp;
888                                                   897 
889         if (!co)                                  898         if (!co)
890                 return;                           899                 return;
891                                                   900 
892         fp = co->co_file;                         901         fp = co->co_file;
893         if (refcount_dec_and_lock(&co->co_odco    902         if (refcount_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
894                 list_del(&co->co_perfile);        903                 list_del(&co->co_perfile);
895                 spin_unlock(&fp->fi_lock);        904                 spin_unlock(&fp->fi_lock);
896                                                   905 
897                 nfsd4_return_all_file_layouts(    906                 nfsd4_return_all_file_layouts(co->co_client, fp);
898                 kmem_cache_free(odstate_slab,     907                 kmem_cache_free(odstate_slab, co);
899         }                                         908         }
900 }                                                 909 }
901                                                   910 
902 static struct nfs4_clnt_odstate *                 911 static struct nfs4_clnt_odstate *
903 find_or_hash_clnt_odstate(struct nfs4_file *fp    912 find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
904 {                                                 913 {
905         struct nfs4_clnt_odstate *co;             914         struct nfs4_clnt_odstate *co;
906         struct nfs4_client *cl;                   915         struct nfs4_client *cl;
907                                                   916 
908         if (!new)                                 917         if (!new)
909                 return NULL;                      918                 return NULL;
910                                                   919 
911         cl = new->co_client;                      920         cl = new->co_client;
912                                                   921 
913         spin_lock(&fp->fi_lock);                  922         spin_lock(&fp->fi_lock);
914         list_for_each_entry(co, &fp->fi_clnt_o    923         list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
915                 if (co->co_client == cl) {        924                 if (co->co_client == cl) {
916                         get_clnt_odstate(co);     925                         get_clnt_odstate(co);
917                         goto out;                 926                         goto out;
918                 }                                 927                 }
919         }                                         928         }
920         co = new;                                 929         co = new;
921         co->co_file = fp;                         930         co->co_file = fp;
922         hash_clnt_odstate_locked(new);            931         hash_clnt_odstate_locked(new);
923 out:                                              932 out:
924         spin_unlock(&fp->fi_lock);                933         spin_unlock(&fp->fi_lock);
925         return co;                                934         return co;
926 }                                                 935 }
927                                                   936 
928 struct nfs4_stid *nfs4_alloc_stid(struct nfs4_    937 struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab,
929                                   void (*sc_fr    938                                   void (*sc_free)(struct nfs4_stid *))
930 {                                                 939 {
931         struct nfs4_stid *stid;                   940         struct nfs4_stid *stid;
932         int new_id;                               941         int new_id;
933                                                   942 
934         stid = kmem_cache_zalloc(slab, GFP_KER    943         stid = kmem_cache_zalloc(slab, GFP_KERNEL);
935         if (!stid)                                944         if (!stid)
936                 return NULL;                      945                 return NULL;
937                                                   946 
938         idr_preload(GFP_KERNEL);                  947         idr_preload(GFP_KERNEL);
939         spin_lock(&cl->cl_lock);                  948         spin_lock(&cl->cl_lock);
940         /* Reserving 0 for start of file in nf    949         /* Reserving 0 for start of file in nfsdfs "states" file: */
941         new_id = idr_alloc_cyclic(&cl->cl_stat    950         new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 1, 0, GFP_NOWAIT);
942         spin_unlock(&cl->cl_lock);                951         spin_unlock(&cl->cl_lock);
943         idr_preload_end();                        952         idr_preload_end();
944         if (new_id < 0)                           953         if (new_id < 0)
945                 goto out_free;                    954                 goto out_free;
946                                                   955 
947         stid->sc_free = sc_free;                  956         stid->sc_free = sc_free;
948         stid->sc_client = cl;                     957         stid->sc_client = cl;
949         stid->sc_stateid.si_opaque.so_id = new    958         stid->sc_stateid.si_opaque.so_id = new_id;
950         stid->sc_stateid.si_opaque.so_clid = c    959         stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
951         /* Will be incremented before return t    960         /* Will be incremented before return to client: */
952         refcount_set(&stid->sc_count, 1);         961         refcount_set(&stid->sc_count, 1);
953         spin_lock_init(&stid->sc_lock);           962         spin_lock_init(&stid->sc_lock);
954         INIT_LIST_HEAD(&stid->sc_cp_list);        963         INIT_LIST_HEAD(&stid->sc_cp_list);
955                                                   964 
956         /*                                        965         /*
957          * It shouldn't be a problem to reuse     966          * It shouldn't be a problem to reuse an opaque stateid value.
958          * I don't think it is for 4.1.  But w    967          * I don't think it is for 4.1.  But with 4.0 I worry that, for
959          * example, a stray write retransmissi    968          * example, a stray write retransmission could be accepted by
960          * the server when it should have been    969          * the server when it should have been rejected.  Therefore,
961          * adopt a trick from the sctp code to    970          * adopt a trick from the sctp code to attempt to maximize the
962          * amount of time until an id is reuse    971          * amount of time until an id is reused, by ensuring they always
963          * "increase" (mod INT_MAX):              972          * "increase" (mod INT_MAX):
964          */                                       973          */
965         return stid;                              974         return stid;
966 out_free:                                         975 out_free:
967         kmem_cache_free(slab, stid);              976         kmem_cache_free(slab, stid);
968         return NULL;                              977         return NULL;
969 }                                                 978 }
970                                                   979 
971 /*                                                980 /*
972  * Create a unique stateid_t to represent each    981  * Create a unique stateid_t to represent each COPY.
973  */                                               982  */
974 static int nfs4_init_cp_state(struct nfsd_net     983 static int nfs4_init_cp_state(struct nfsd_net *nn, copy_stateid_t *stid,
975                               unsigned char cs    984                               unsigned char cs_type)
976 {                                                 985 {
977         int new_id;                               986         int new_id;
978                                                   987 
979         stid->cs_stid.si_opaque.so_clid.cl_boo    988         stid->cs_stid.si_opaque.so_clid.cl_boot = (u32)nn->boot_time;
980         stid->cs_stid.si_opaque.so_clid.cl_id     989         stid->cs_stid.si_opaque.so_clid.cl_id = nn->s2s_cp_cl_id;
981                                                   990 
982         idr_preload(GFP_KERNEL);                  991         idr_preload(GFP_KERNEL);
983         spin_lock(&nn->s2s_cp_lock);              992         spin_lock(&nn->s2s_cp_lock);
984         new_id = idr_alloc_cyclic(&nn->s2s_cp_    993         new_id = idr_alloc_cyclic(&nn->s2s_cp_stateids, stid, 0, 0, GFP_NOWAIT);
985         stid->cs_stid.si_opaque.so_id = new_id    994         stid->cs_stid.si_opaque.so_id = new_id;
986         stid->cs_stid.si_generation = 1;          995         stid->cs_stid.si_generation = 1;
987         spin_unlock(&nn->s2s_cp_lock);            996         spin_unlock(&nn->s2s_cp_lock);
988         idr_preload_end();                        997         idr_preload_end();
989         if (new_id < 0)                           998         if (new_id < 0)
990                 return 0;                         999                 return 0;
991         stid->cs_type = cs_type;                  1000         stid->cs_type = cs_type;
992         return 1;                                 1001         return 1;
993 }                                                 1002 }
994                                                   1003 
995 int nfs4_init_copy_state(struct nfsd_net *nn,     1004 int nfs4_init_copy_state(struct nfsd_net *nn, struct nfsd4_copy *copy)
996 {                                                 1005 {
997         return nfs4_init_cp_state(nn, &copy->c    1006         return nfs4_init_cp_state(nn, &copy->cp_stateid, NFS4_COPY_STID);
998 }                                                 1007 }
999                                                   1008 
1000 struct nfs4_cpntf_state *nfs4_alloc_init_cpnt    1009 struct nfs4_cpntf_state *nfs4_alloc_init_cpntf_state(struct nfsd_net *nn,
1001                                                  1010                                                      struct nfs4_stid *p_stid)
1002 {                                                1011 {
1003         struct nfs4_cpntf_state *cps;            1012         struct nfs4_cpntf_state *cps;
1004                                                  1013 
1005         cps = kzalloc(sizeof(struct nfs4_cpnt    1014         cps = kzalloc(sizeof(struct nfs4_cpntf_state), GFP_KERNEL);
1006         if (!cps)                                1015         if (!cps)
1007                 return NULL;                     1016                 return NULL;
1008         cps->cpntf_time = ktime_get_boottime_    1017         cps->cpntf_time = ktime_get_boottime_seconds();
1009         refcount_set(&cps->cp_stateid.cs_coun    1018         refcount_set(&cps->cp_stateid.cs_count, 1);
1010         if (!nfs4_init_cp_state(nn, &cps->cp_    1019         if (!nfs4_init_cp_state(nn, &cps->cp_stateid, NFS4_COPYNOTIFY_STID))
1011                 goto out_free;                   1020                 goto out_free;
1012         spin_lock(&nn->s2s_cp_lock);             1021         spin_lock(&nn->s2s_cp_lock);
1013         list_add(&cps->cp_list, &p_stid->sc_c    1022         list_add(&cps->cp_list, &p_stid->sc_cp_list);
1014         spin_unlock(&nn->s2s_cp_lock);           1023         spin_unlock(&nn->s2s_cp_lock);
1015         return cps;                              1024         return cps;
1016 out_free:                                        1025 out_free:
1017         kfree(cps);                              1026         kfree(cps);
1018         return NULL;                             1027         return NULL;
1019 }                                                1028 }
1020                                                  1029 
1021 void nfs4_free_copy_state(struct nfsd4_copy *    1030 void nfs4_free_copy_state(struct nfsd4_copy *copy)
1022 {                                                1031 {
1023         struct nfsd_net *nn;                     1032         struct nfsd_net *nn;
1024                                                  1033 
1025         if (copy->cp_stateid.cs_type != NFS4_    1034         if (copy->cp_stateid.cs_type != NFS4_COPY_STID)
1026                 return;                          1035                 return;
1027         nn = net_generic(copy->cp_clp->net, n    1036         nn = net_generic(copy->cp_clp->net, nfsd_net_id);
1028         spin_lock(&nn->s2s_cp_lock);             1037         spin_lock(&nn->s2s_cp_lock);
1029         idr_remove(&nn->s2s_cp_stateids,         1038         idr_remove(&nn->s2s_cp_stateids,
1030                    copy->cp_stateid.cs_stid.s    1039                    copy->cp_stateid.cs_stid.si_opaque.so_id);
1031         spin_unlock(&nn->s2s_cp_lock);           1040         spin_unlock(&nn->s2s_cp_lock);
1032 }                                                1041 }
1033                                                  1042 
1034 static void nfs4_free_cpntf_statelist(struct     1043 static void nfs4_free_cpntf_statelist(struct net *net, struct nfs4_stid *stid)
1035 {                                                1044 {
1036         struct nfs4_cpntf_state *cps;            1045         struct nfs4_cpntf_state *cps;
1037         struct nfsd_net *nn;                     1046         struct nfsd_net *nn;
1038                                                  1047 
1039         nn = net_generic(net, nfsd_net_id);      1048         nn = net_generic(net, nfsd_net_id);
1040         spin_lock(&nn->s2s_cp_lock);             1049         spin_lock(&nn->s2s_cp_lock);
1041         while (!list_empty(&stid->sc_cp_list)    1050         while (!list_empty(&stid->sc_cp_list)) {
1042                 cps = list_first_entry(&stid-    1051                 cps = list_first_entry(&stid->sc_cp_list,
1043                                        struct    1052                                        struct nfs4_cpntf_state, cp_list);
1044                 _free_cpntf_state_locked(nn,     1053                 _free_cpntf_state_locked(nn, cps);
1045         }                                        1054         }
1046         spin_unlock(&nn->s2s_cp_lock);           1055         spin_unlock(&nn->s2s_cp_lock);
1047 }                                                1056 }
1048                                                  1057 
1049 static struct nfs4_ol_stateid * nfs4_alloc_op    1058 static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
1050 {                                                1059 {
1051         struct nfs4_stid *stid;                  1060         struct nfs4_stid *stid;
1052                                                  1061 
1053         stid = nfs4_alloc_stid(clp, stateid_s    1062         stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid);
1054         if (!stid)                               1063         if (!stid)
1055                 return NULL;                     1064                 return NULL;
1056                                                  1065 
1057         return openlockstateid(stid);            1066         return openlockstateid(stid);
1058 }                                                1067 }
1059                                                  1068 
1060 static void nfs4_free_deleg(struct nfs4_stid     1069 static void nfs4_free_deleg(struct nfs4_stid *stid)
1061 {                                                1070 {
1062         struct nfs4_delegation *dp = delegsta    1071         struct nfs4_delegation *dp = delegstateid(stid);
1063                                                  1072 
1064         WARN_ON_ONCE(!list_empty(&stid->sc_cp    1073         WARN_ON_ONCE(!list_empty(&stid->sc_cp_list));
1065         WARN_ON_ONCE(!list_empty(&dp->dl_perf    1074         WARN_ON_ONCE(!list_empty(&dp->dl_perfile));
1066         WARN_ON_ONCE(!list_empty(&dp->dl_perc    1075         WARN_ON_ONCE(!list_empty(&dp->dl_perclnt));
1067         WARN_ON_ONCE(!list_empty(&dp->dl_reca    1076         WARN_ON_ONCE(!list_empty(&dp->dl_recall_lru));
1068         kmem_cache_free(deleg_slab, stid);       1077         kmem_cache_free(deleg_slab, stid);
1069         atomic_long_dec(&num_delegations);       1078         atomic_long_dec(&num_delegations);
1070 }                                                1079 }
1071                                                  1080 
1072 /*                                               1081 /*
1073  * When we recall a delegation, we should be     1082  * When we recall a delegation, we should be careful not to hand it
1074  * out again straight away.                      1083  * out again straight away.
1075  * To ensure this we keep a pair of bloom fil    1084  * To ensure this we keep a pair of bloom filters ('new' and 'old')
1076  * in which the filehandles of recalled deleg    1085  * in which the filehandles of recalled delegations are "stored".
1077  * If a filehandle appear in either filter, a    1086  * If a filehandle appear in either filter, a delegation is blocked.
1078  * When a delegation is recalled, the filehan    1087  * When a delegation is recalled, the filehandle is stored in the "new"
1079  * filter.                                       1088  * filter.
1080  * Every 30 seconds we swap the filters and c    1089  * Every 30 seconds we swap the filters and clear the "new" one,
1081  * unless both are empty of course.  This res !! 1090  * unless both are empty of course.
1082  * given filehandle being blocked for between << 
1083  *                                               1091  *
1084  * Each filter is 256 bits.  We hash the file    1092  * Each filter is 256 bits.  We hash the filehandle to 32bit and use the
1085  * low 3 bytes as hash-table indices.            1093  * low 3 bytes as hash-table indices.
1086  *                                               1094  *
1087  * 'blocked_delegations_lock', which is alway    1095  * 'blocked_delegations_lock', which is always taken in block_delegations(),
1088  * is used to manage concurrent access.  Test    1096  * is used to manage concurrent access.  Testing does not need the lock
1089  * except when swapping the two filters.         1097  * except when swapping the two filters.
1090  */                                              1098  */
1091 static DEFINE_SPINLOCK(blocked_delegations_lo    1099 static DEFINE_SPINLOCK(blocked_delegations_lock);
1092 static struct bloom_pair {                       1100 static struct bloom_pair {
1093         int     entries, old_entries;            1101         int     entries, old_entries;
1094         time64_t swap_time;                      1102         time64_t swap_time;
1095         int     new; /* index into 'set' */      1103         int     new; /* index into 'set' */
1096         DECLARE_BITMAP(set[2], 256);             1104         DECLARE_BITMAP(set[2], 256);
1097 } blocked_delegations;                           1105 } blocked_delegations;
1098                                                  1106 
1099 static int delegation_blocked(struct knfsd_fh    1107 static int delegation_blocked(struct knfsd_fh *fh)
1100 {                                                1108 {
1101         u32 hash;                                1109         u32 hash;
1102         struct bloom_pair *bd = &blocked_dele    1110         struct bloom_pair *bd = &blocked_delegations;
1103                                                  1111 
1104         if (bd->entries == 0)                    1112         if (bd->entries == 0)
1105                 return 0;                        1113                 return 0;
1106         if (ktime_get_seconds() - bd->swap_ti    1114         if (ktime_get_seconds() - bd->swap_time > 30) {
1107                 spin_lock(&blocked_delegation    1115                 spin_lock(&blocked_delegations_lock);
1108                 if (ktime_get_seconds() - bd-    1116                 if (ktime_get_seconds() - bd->swap_time > 30) {
1109                         bd->entries -= bd->ol    1117                         bd->entries -= bd->old_entries;
1110                         bd->old_entries = bd-    1118                         bd->old_entries = bd->entries;
1111                         bd->new = 1-bd->new;  << 
1112                         memset(bd->set[bd->ne    1119                         memset(bd->set[bd->new], 0,
1113                                sizeof(bd->set    1120                                sizeof(bd->set[0]));
                                                   >> 1121                         bd->new = 1-bd->new;
1114                         bd->swap_time = ktime    1122                         bd->swap_time = ktime_get_seconds();
1115                 }                                1123                 }
1116                 spin_unlock(&blocked_delegati    1124                 spin_unlock(&blocked_delegations_lock);
1117         }                                        1125         }
1118         hash = jhash(&fh->fh_raw, fh->fh_size    1126         hash = jhash(&fh->fh_raw, fh->fh_size, 0);
1119         if (test_bit(hash&255, bd->set[0]) &&    1127         if (test_bit(hash&255, bd->set[0]) &&
1120             test_bit((hash>>8)&255, bd->set[0    1128             test_bit((hash>>8)&255, bd->set[0]) &&
1121             test_bit((hash>>16)&255, bd->set[    1129             test_bit((hash>>16)&255, bd->set[0]))
1122                 return 1;                        1130                 return 1;
1123                                                  1131 
1124         if (test_bit(hash&255, bd->set[1]) &&    1132         if (test_bit(hash&255, bd->set[1]) &&
1125             test_bit((hash>>8)&255, bd->set[1    1133             test_bit((hash>>8)&255, bd->set[1]) &&
1126             test_bit((hash>>16)&255, bd->set[    1134             test_bit((hash>>16)&255, bd->set[1]))
1127                 return 1;                        1135                 return 1;
1128                                                  1136 
1129         return 0;                                1137         return 0;
1130 }                                                1138 }
1131                                                  1139 
1132 static void block_delegations(struct knfsd_fh    1140 static void block_delegations(struct knfsd_fh *fh)
1133 {                                                1141 {
1134         u32 hash;                                1142         u32 hash;
1135         struct bloom_pair *bd = &blocked_dele    1143         struct bloom_pair *bd = &blocked_delegations;
1136                                                  1144 
1137         hash = jhash(&fh->fh_raw, fh->fh_size    1145         hash = jhash(&fh->fh_raw, fh->fh_size, 0);
1138                                                  1146 
1139         spin_lock(&blocked_delegations_lock);    1147         spin_lock(&blocked_delegations_lock);
1140         __set_bit(hash&255, bd->set[bd->new])    1148         __set_bit(hash&255, bd->set[bd->new]);
1141         __set_bit((hash>>8)&255, bd->set[bd->    1149         __set_bit((hash>>8)&255, bd->set[bd->new]);
1142         __set_bit((hash>>16)&255, bd->set[bd-    1150         __set_bit((hash>>16)&255, bd->set[bd->new]);
1143         if (bd->entries == 0)                    1151         if (bd->entries == 0)
1144                 bd->swap_time = ktime_get_sec    1152                 bd->swap_time = ktime_get_seconds();
1145         bd->entries += 1;                        1153         bd->entries += 1;
1146         spin_unlock(&blocked_delegations_lock    1154         spin_unlock(&blocked_delegations_lock);
1147 }                                                1155 }
1148                                                  1156 
1149 static struct nfs4_delegation *                  1157 static struct nfs4_delegation *
1150 alloc_init_deleg(struct nfs4_client *clp, str    1158 alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
1151                  struct nfs4_clnt_odstate *od    1159                  struct nfs4_clnt_odstate *odstate, u32 dl_type)
1152 {                                                1160 {
1153         struct nfs4_delegation *dp;              1161         struct nfs4_delegation *dp;
1154         struct nfs4_stid *stid;                  1162         struct nfs4_stid *stid;
1155         long n;                                  1163         long n;
1156                                                  1164 
1157         dprintk("NFSD alloc_init_deleg\n");      1165         dprintk("NFSD alloc_init_deleg\n");
1158         n = atomic_long_inc_return(&num_deleg    1166         n = atomic_long_inc_return(&num_delegations);
1159         if (n < 0 || n > max_delegations)        1167         if (n < 0 || n > max_delegations)
1160                 goto out_dec;                    1168                 goto out_dec;
1161         if (delegation_blocked(&fp->fi_fhandl    1169         if (delegation_blocked(&fp->fi_fhandle))
1162                 goto out_dec;                    1170                 goto out_dec;
1163         stid = nfs4_alloc_stid(clp, deleg_sla    1171         stid = nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg);
1164         if (stid == NULL)                        1172         if (stid == NULL)
1165                 goto out_dec;                    1173                 goto out_dec;
1166         dp = delegstateid(stid);                 1174         dp = delegstateid(stid);
1167                                                  1175 
1168         /*                                       1176         /*
1169          * delegation seqid's are never incre    1177          * delegation seqid's are never incremented.  The 4.1 special
1170          * meaning of seqid 0 isn't meaningfu    1178          * meaning of seqid 0 isn't meaningful, really, but let's avoid
1171          * 0 anyway just for consistency and     1179          * 0 anyway just for consistency and use 1:
1172          */                                      1180          */
1173         dp->dl_stid.sc_stateid.si_generation     1181         dp->dl_stid.sc_stateid.si_generation = 1;
1174         INIT_LIST_HEAD(&dp->dl_perfile);         1182         INIT_LIST_HEAD(&dp->dl_perfile);
1175         INIT_LIST_HEAD(&dp->dl_perclnt);         1183         INIT_LIST_HEAD(&dp->dl_perclnt);
1176         INIT_LIST_HEAD(&dp->dl_recall_lru);      1184         INIT_LIST_HEAD(&dp->dl_recall_lru);
1177         dp->dl_clnt_odstate = odstate;           1185         dp->dl_clnt_odstate = odstate;
1178         get_clnt_odstate(odstate);               1186         get_clnt_odstate(odstate);
1179         dp->dl_type = dl_type;                   1187         dp->dl_type = dl_type;
1180         dp->dl_retries = 1;                      1188         dp->dl_retries = 1;
1181         dp->dl_recalled = false;                 1189         dp->dl_recalled = false;
1182         nfsd4_init_cb(&dp->dl_recall, dp->dl_    1190         nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
1183                       &nfsd4_cb_recall_ops, N    1191                       &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
1184         nfsd4_init_cb(&dp->dl_cb_fattr.ncf_ge << 
1185                         &nfsd4_cb_getattr_ops << 
1186         dp->dl_cb_fattr.ncf_file_modified = f << 
1187         dp->dl_cb_fattr.ncf_cb_bmap[0] = FATT << 
1188         get_nfs4_file(fp);                       1192         get_nfs4_file(fp);
1189         dp->dl_stid.sc_file = fp;                1193         dp->dl_stid.sc_file = fp;
1190         return dp;                               1194         return dp;
1191 out_dec:                                         1195 out_dec:
1192         atomic_long_dec(&num_delegations);       1196         atomic_long_dec(&num_delegations);
1193         return NULL;                             1197         return NULL;
1194 }                                                1198 }
1195                                                  1199 
1196 void                                             1200 void
1197 nfs4_put_stid(struct nfs4_stid *s)               1201 nfs4_put_stid(struct nfs4_stid *s)
1198 {                                                1202 {
1199         struct nfs4_file *fp = s->sc_file;       1203         struct nfs4_file *fp = s->sc_file;
1200         struct nfs4_client *clp = s->sc_clien    1204         struct nfs4_client *clp = s->sc_client;
1201                                                  1205 
1202         might_lock(&clp->cl_lock);               1206         might_lock(&clp->cl_lock);
1203                                                  1207 
1204         if (!refcount_dec_and_lock(&s->sc_cou    1208         if (!refcount_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
1205                 wake_up_all(&close_wq);          1209                 wake_up_all(&close_wq);
1206                 return;                          1210                 return;
1207         }                                        1211         }
1208         idr_remove(&clp->cl_stateids, s->sc_s    1212         idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1209         if (s->sc_status & SC_STATUS_ADMIN_RE << 
1210                 atomic_dec(&s->sc_client->cl_ << 
1211         nfs4_free_cpntf_statelist(clp->net, s    1213         nfs4_free_cpntf_statelist(clp->net, s);
1212         spin_unlock(&clp->cl_lock);              1214         spin_unlock(&clp->cl_lock);
1213         s->sc_free(s);                           1215         s->sc_free(s);
1214         if (fp)                                  1216         if (fp)
1215                 put_nfs4_file(fp);               1217                 put_nfs4_file(fp);
1216 }                                                1218 }
1217                                                  1219 
1218 void                                             1220 void
1219 nfs4_inc_and_copy_stateid(stateid_t *dst, str    1221 nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
1220 {                                                1222 {
1221         stateid_t *src = &stid->sc_stateid;      1223         stateid_t *src = &stid->sc_stateid;
1222                                                  1224 
1223         spin_lock(&stid->sc_lock);               1225         spin_lock(&stid->sc_lock);
1224         if (unlikely(++src->si_generation ==     1226         if (unlikely(++src->si_generation == 0))
1225                 src->si_generation = 1;          1227                 src->si_generation = 1;
1226         memcpy(dst, src, sizeof(*dst));          1228         memcpy(dst, src, sizeof(*dst));
1227         spin_unlock(&stid->sc_lock);             1229         spin_unlock(&stid->sc_lock);
1228 }                                                1230 }
1229                                                  1231 
1230 static void put_deleg_file(struct nfs4_file *    1232 static void put_deleg_file(struct nfs4_file *fp)
1231 {                                                1233 {
1232         struct nfsd_file *nf = NULL;             1234         struct nfsd_file *nf = NULL;
1233                                                  1235 
1234         spin_lock(&fp->fi_lock);                 1236         spin_lock(&fp->fi_lock);
1235         if (--fp->fi_delegees == 0)              1237         if (--fp->fi_delegees == 0)
1236                 swap(nf, fp->fi_deleg_file);     1238                 swap(nf, fp->fi_deleg_file);
1237         spin_unlock(&fp->fi_lock);               1239         spin_unlock(&fp->fi_lock);
1238                                                  1240 
1239         if (nf)                                  1241         if (nf)
1240                 nfsd_file_put(nf);               1242                 nfsd_file_put(nf);
1241 }                                                1243 }
1242                                                  1244 
1243 static void nfs4_unlock_deleg_lease(struct nf    1245 static void nfs4_unlock_deleg_lease(struct nfs4_delegation *dp)
1244 {                                                1246 {
1245         struct nfs4_file *fp = dp->dl_stid.sc    1247         struct nfs4_file *fp = dp->dl_stid.sc_file;
1246         struct nfsd_file *nf = fp->fi_deleg_f    1248         struct nfsd_file *nf = fp->fi_deleg_file;
1247                                                  1249 
1248         WARN_ON_ONCE(!fp->fi_delegees);          1250         WARN_ON_ONCE(!fp->fi_delegees);
1249                                                  1251 
1250         kernel_setlease(nf->nf_file, F_UNLCK, !! 1252         vfs_setlease(nf->nf_file, F_UNLCK, NULL, (void **)&dp);
1251         put_deleg_file(fp);                      1253         put_deleg_file(fp);
1252 }                                                1254 }
1253                                                  1255 
1254 static void destroy_unhashed_deleg(struct nfs    1256 static void destroy_unhashed_deleg(struct nfs4_delegation *dp)
1255 {                                                1257 {
1256         put_clnt_odstate(dp->dl_clnt_odstate)    1258         put_clnt_odstate(dp->dl_clnt_odstate);
1257         nfs4_unlock_deleg_lease(dp);             1259         nfs4_unlock_deleg_lease(dp);
1258         nfs4_put_stid(&dp->dl_stid);             1260         nfs4_put_stid(&dp->dl_stid);
1259 }                                                1261 }
1260                                                  1262 
                                                   >> 1263 void nfs4_unhash_stid(struct nfs4_stid *s)
                                                   >> 1264 {
                                                   >> 1265         s->sc_type = 0;
                                                   >> 1266 }
                                                   >> 1267 
1261 /**                                              1268 /**
1262  * nfs4_delegation_exists - Discover if this     1269  * nfs4_delegation_exists - Discover if this delegation already exists
1263  * @clp:     a pointer to the nfs4_client we'    1270  * @clp:     a pointer to the nfs4_client we're granting a delegation to
1264  * @fp:      a pointer to the nfs4_file we're    1271  * @fp:      a pointer to the nfs4_file we're granting a delegation on
1265  *                                               1272  *
1266  * Return:                                       1273  * Return:
1267  *      On success: true iff an existing dele    1274  *      On success: true iff an existing delegation is found
1268  */                                              1275  */
1269                                                  1276 
1270 static bool                                      1277 static bool
1271 nfs4_delegation_exists(struct nfs4_client *cl    1278 nfs4_delegation_exists(struct nfs4_client *clp, struct nfs4_file *fp)
1272 {                                                1279 {
1273         struct nfs4_delegation *searchdp = NU    1280         struct nfs4_delegation *searchdp = NULL;
1274         struct nfs4_client *searchclp = NULL;    1281         struct nfs4_client *searchclp = NULL;
1275                                                  1282 
1276         lockdep_assert_held(&state_lock);        1283         lockdep_assert_held(&state_lock);
1277         lockdep_assert_held(&fp->fi_lock);       1284         lockdep_assert_held(&fp->fi_lock);
1278                                                  1285 
1279         list_for_each_entry(searchdp, &fp->fi    1286         list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
1280                 searchclp = searchdp->dl_stid    1287                 searchclp = searchdp->dl_stid.sc_client;
1281                 if (clp == searchclp) {          1288                 if (clp == searchclp) {
1282                         return true;             1289                         return true;
1283                 }                                1290                 }
1284         }                                        1291         }
1285         return false;                            1292         return false;
1286 }                                                1293 }
1287                                                  1294 
1288 /**                                              1295 /**
1289  * hash_delegation_locked - Add a delegation     1296  * hash_delegation_locked - Add a delegation to the appropriate lists
1290  * @dp:     a pointer to the nfs4_delegation     1297  * @dp:     a pointer to the nfs4_delegation we are adding.
1291  * @fp:     a pointer to the nfs4_file we're     1298  * @fp:     a pointer to the nfs4_file we're granting a delegation on
1292  *                                               1299  *
1293  * Return:                                       1300  * Return:
1294  *      On success: NULL if the delegation wa    1301  *      On success: NULL if the delegation was successfully hashed.
1295  *                                               1302  *
1296  *      On error: -EAGAIN if one was previous    1303  *      On error: -EAGAIN if one was previously granted to this
1297  *                 nfs4_client for this nfs4_    1304  *                 nfs4_client for this nfs4_file. Delegation is not hashed.
1298  *                                               1305  *
1299  */                                              1306  */
1300                                                  1307 
1301 static int                                       1308 static int
1302 hash_delegation_locked(struct nfs4_delegation    1309 hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
1303 {                                                1310 {
1304         struct nfs4_client *clp = dp->dl_stid    1311         struct nfs4_client *clp = dp->dl_stid.sc_client;
1305                                                  1312 
1306         lockdep_assert_held(&state_lock);        1313         lockdep_assert_held(&state_lock);
1307         lockdep_assert_held(&fp->fi_lock);       1314         lockdep_assert_held(&fp->fi_lock);
1308         lockdep_assert_held(&clp->cl_lock);   << 
1309                                                  1315 
1310         if (nfs4_delegation_exists(clp, fp))     1316         if (nfs4_delegation_exists(clp, fp))
1311                 return -EAGAIN;                  1317                 return -EAGAIN;
1312         refcount_inc(&dp->dl_stid.sc_count);     1318         refcount_inc(&dp->dl_stid.sc_count);
1313         dp->dl_stid.sc_type = SC_TYPE_DELEG;  !! 1319         dp->dl_stid.sc_type = NFS4_DELEG_STID;
1314         list_add(&dp->dl_perfile, &fp->fi_del    1320         list_add(&dp->dl_perfile, &fp->fi_delegations);
1315         list_add(&dp->dl_perclnt, &clp->cl_de    1321         list_add(&dp->dl_perclnt, &clp->cl_delegations);
1316         return 0;                                1322         return 0;
1317 }                                                1323 }
1318                                                  1324 
1319 static bool delegation_hashed(struct nfs4_del    1325 static bool delegation_hashed(struct nfs4_delegation *dp)
1320 {                                                1326 {
1321         return !(list_empty(&dp->dl_perfile))    1327         return !(list_empty(&dp->dl_perfile));
1322 }                                                1328 }
1323                                                  1329 
1324 static bool                                      1330 static bool
1325 unhash_delegation_locked(struct nfs4_delegati !! 1331 unhash_delegation_locked(struct nfs4_delegation *dp)
1326 {                                                1332 {
1327         struct nfs4_file *fp = dp->dl_stid.sc    1333         struct nfs4_file *fp = dp->dl_stid.sc_file;
1328                                                  1334 
1329         lockdep_assert_held(&state_lock);        1335         lockdep_assert_held(&state_lock);
1330                                                  1336 
1331         if (!delegation_hashed(dp))              1337         if (!delegation_hashed(dp))
1332                 return false;                    1338                 return false;
1333                                                  1339 
1334         if (statusmask == SC_STATUS_REVOKED & !! 1340         dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
1335             dp->dl_stid.sc_client->cl_minorve << 
1336                 statusmask = SC_STATUS_CLOSED << 
1337         dp->dl_stid.sc_status |= statusmask;  << 
1338         if (statusmask & SC_STATUS_ADMIN_REVO << 
1339                 atomic_inc(&dp->dl_stid.sc_cl << 
1340                                               << 
1341         /* Ensure that deleg break won't try     1341         /* Ensure that deleg break won't try to requeue it */
1342         ++dp->dl_time;                           1342         ++dp->dl_time;
1343         spin_lock(&fp->fi_lock);                 1343         spin_lock(&fp->fi_lock);
1344         list_del_init(&dp->dl_perclnt);          1344         list_del_init(&dp->dl_perclnt);
1345         list_del_init(&dp->dl_recall_lru);       1345         list_del_init(&dp->dl_recall_lru);
1346         list_del_init(&dp->dl_perfile);          1346         list_del_init(&dp->dl_perfile);
1347         spin_unlock(&fp->fi_lock);               1347         spin_unlock(&fp->fi_lock);
1348         return true;                             1348         return true;
1349 }                                                1349 }
1350                                                  1350 
1351 static void destroy_delegation(struct nfs4_de    1351 static void destroy_delegation(struct nfs4_delegation *dp)
1352 {                                                1352 {
1353         bool unhashed;                           1353         bool unhashed;
1354                                                  1354 
1355         spin_lock(&state_lock);                  1355         spin_lock(&state_lock);
1356         unhashed = unhash_delegation_locked(d !! 1356         unhashed = unhash_delegation_locked(dp);
1357         spin_unlock(&state_lock);                1357         spin_unlock(&state_lock);
1358         if (unhashed)                            1358         if (unhashed)
1359                 destroy_unhashed_deleg(dp);      1359                 destroy_unhashed_deleg(dp);
1360 }                                                1360 }
1361                                                  1361 
1362 /**                                           << 
1363  * revoke_delegation - perform nfs4 delegatio << 
1364  * @dp: pointer to the delegation             << 
1365  *                                            << 
1366  * This function assumes that it's called eit << 
1367  * interface (nfsd4_revoke_states()) that's r << 
1368  * stateid or it's called from a laundromat t << 
1369  * determined that this specific state has ex << 
1370  * (both mark state with the appropriate stid << 
1371  * assumed that a reference was taken on the  << 
1372  *                                            << 
1373  * If this function finds that the @dp state  << 
1374  * that a FREE_STATEID operation for this sta << 
1375  * we can proceed to removing it from recalle << 
1376  * isn't marked SC_STATUS_FREED, it means we  << 
1377  * list and wait for the FREE_STATEID to arri << 
1378  * time, we need to mark it as SC_STATUS_FREE << 
1379  * nfsd4_free_stateid() function that this st << 
1380  * to the cl_revoked list and that nfsd4_free << 
1381  * for removing it from the list. Inspection  << 
1382  * in the revocation process is protected by  << 
1383  */                                           << 
1384 static void revoke_delegation(struct nfs4_del    1362 static void revoke_delegation(struct nfs4_delegation *dp)
1385 {                                                1363 {
1386         struct nfs4_client *clp = dp->dl_stid    1364         struct nfs4_client *clp = dp->dl_stid.sc_client;
1387                                                  1365 
1388         WARN_ON(!list_empty(&dp->dl_recall_lr    1366         WARN_ON(!list_empty(&dp->dl_recall_lru));
1389         WARN_ON_ONCE(!(dp->dl_stid.sc_status  << 
1390                      (SC_STATUS_REVOKED | SC_ << 
1391                                                  1367 
1392         trace_nfsd_stid_revoke(&dp->dl_stid);    1368         trace_nfsd_stid_revoke(&dp->dl_stid);
1393                                                  1369 
1394         spin_lock(&clp->cl_lock);             !! 1370         if (clp->cl_minorversion) {
1395         if (dp->dl_stid.sc_status & SC_STATUS !! 1371                 spin_lock(&clp->cl_lock);
1396                 list_del_init(&dp->dl_recall_ !! 1372                 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
1397                 goto out;                     !! 1373                 refcount_inc(&dp->dl_stid.sc_count);
                                                   >> 1374                 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
                                                   >> 1375                 spin_unlock(&clp->cl_lock);
1398         }                                        1376         }
1399         list_add(&dp->dl_recall_lru, &clp->cl << 
1400         dp->dl_stid.sc_status |= SC_STATUS_FR << 
1401 out:                                          << 
1402         spin_unlock(&clp->cl_lock);           << 
1403         destroy_unhashed_deleg(dp);              1377         destroy_unhashed_deleg(dp);
1404 }                                                1378 }
1405                                                  1379 
1406 /*                                            !! 1380 /* 
1407  * SETCLIENTID state                          !! 1381  * SETCLIENTID state 
1408  */                                              1382  */
1409                                                  1383 
1410 static unsigned int clientid_hashval(u32 id)     1384 static unsigned int clientid_hashval(u32 id)
1411 {                                                1385 {
1412         return id & CLIENT_HASH_MASK;            1386         return id & CLIENT_HASH_MASK;
1413 }                                                1387 }
1414                                                  1388 
1415 static unsigned int clientstr_hashval(struct     1389 static unsigned int clientstr_hashval(struct xdr_netobj name)
1416 {                                                1390 {
1417         return opaque_hashval(name.data, 8) &    1391         return opaque_hashval(name.data, 8) & CLIENT_HASH_MASK;
1418 }                                                1392 }
1419                                                  1393 
1420 /*                                               1394 /*
1421  * A stateid that had a deny mode associated     1395  * A stateid that had a deny mode associated with it is being released
1422  * or downgraded. Recalculate the deny mode o    1396  * or downgraded. Recalculate the deny mode on the file.
1423  */                                              1397  */
1424 static void                                      1398 static void
1425 recalculate_deny_mode(struct nfs4_file *fp)      1399 recalculate_deny_mode(struct nfs4_file *fp)
1426 {                                                1400 {
1427         struct nfs4_ol_stateid *stp;             1401         struct nfs4_ol_stateid *stp;
1428         u32 old_deny;                         << 
1429                                                  1402 
1430         spin_lock(&fp->fi_lock);                 1403         spin_lock(&fp->fi_lock);
1431         old_deny = fp->fi_share_deny;         << 
1432         fp->fi_share_deny = 0;                   1404         fp->fi_share_deny = 0;
1433         list_for_each_entry(stp, &fp->fi_stat !! 1405         list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
1434                 fp->fi_share_deny |= bmap_to_    1406                 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
1435                 if (fp->fi_share_deny == old_ << 
1436                         break;                << 
1437         }                                     << 
1438         spin_unlock(&fp->fi_lock);               1407         spin_unlock(&fp->fi_lock);
1439 }                                                1408 }
1440                                                  1409 
1441 static void                                      1410 static void
1442 reset_union_bmap_deny(u32 deny, struct nfs4_o    1411 reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
1443 {                                                1412 {
1444         int i;                                   1413         int i;
1445         bool change = false;                     1414         bool change = false;
1446                                                  1415 
1447         for (i = 1; i < 4; i++) {                1416         for (i = 1; i < 4; i++) {
1448                 if ((i & deny) != i) {           1417                 if ((i & deny) != i) {
1449                         change = true;           1418                         change = true;
1450                         clear_deny(i, stp);      1419                         clear_deny(i, stp);
1451                 }                                1420                 }
1452         }                                        1421         }
1453                                                  1422 
1454         /* Recalculate per-file deny mode if     1423         /* Recalculate per-file deny mode if there was a change */
1455         if (change)                              1424         if (change)
1456                 recalculate_deny_mode(stp->st    1425                 recalculate_deny_mode(stp->st_stid.sc_file);
1457 }                                                1426 }
1458                                                  1427 
1459 /* release all access and file references for    1428 /* release all access and file references for a given stateid */
1460 static void                                      1429 static void
1461 release_all_access(struct nfs4_ol_stateid *st    1430 release_all_access(struct nfs4_ol_stateid *stp)
1462 {                                                1431 {
1463         int i;                                   1432         int i;
1464         struct nfs4_file *fp = stp->st_stid.s    1433         struct nfs4_file *fp = stp->st_stid.sc_file;
1465                                                  1434 
1466         if (fp && stp->st_deny_bmap != 0)        1435         if (fp && stp->st_deny_bmap != 0)
1467                 recalculate_deny_mode(fp);       1436                 recalculate_deny_mode(fp);
1468                                                  1437 
1469         for (i = 1; i < 4; i++) {                1438         for (i = 1; i < 4; i++) {
1470                 if (test_access(i, stp))         1439                 if (test_access(i, stp))
1471                         nfs4_file_put_access(    1440                         nfs4_file_put_access(stp->st_stid.sc_file, i);
1472                 clear_access(i, stp);            1441                 clear_access(i, stp);
1473         }                                        1442         }
1474 }                                                1443 }
1475                                                  1444 
1476 static inline void nfs4_free_stateowner(struc    1445 static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1477 {                                                1446 {
1478         kfree(sop->so_owner.data);               1447         kfree(sop->so_owner.data);
1479         sop->so_ops->so_free(sop);               1448         sop->so_ops->so_free(sop);
1480 }                                                1449 }
1481                                                  1450 
1482 static void nfs4_put_stateowner(struct nfs4_s    1451 static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1483 {                                                1452 {
1484         struct nfs4_client *clp = sop->so_cli    1453         struct nfs4_client *clp = sop->so_client;
1485                                                  1454 
1486         might_lock(&clp->cl_lock);               1455         might_lock(&clp->cl_lock);
1487                                                  1456 
1488         if (!atomic_dec_and_lock(&sop->so_cou    1457         if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
1489                 return;                          1458                 return;
1490         sop->so_ops->so_unhash(sop);             1459         sop->so_ops->so_unhash(sop);
1491         spin_unlock(&clp->cl_lock);              1460         spin_unlock(&clp->cl_lock);
1492         nfs4_free_stateowner(sop);               1461         nfs4_free_stateowner(sop);
1493 }                                                1462 }
1494                                                  1463 
1495 static bool                                      1464 static bool
1496 nfs4_ol_stateid_unhashed(const struct nfs4_ol    1465 nfs4_ol_stateid_unhashed(const struct nfs4_ol_stateid *stp)
1497 {                                                1466 {
1498         return list_empty(&stp->st_perfile);     1467         return list_empty(&stp->st_perfile);
1499 }                                                1468 }
1500                                                  1469 
1501 static bool unhash_ol_stateid(struct nfs4_ol_    1470 static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
1502 {                                                1471 {
1503         struct nfs4_file *fp = stp->st_stid.s    1472         struct nfs4_file *fp = stp->st_stid.sc_file;
1504                                                  1473 
1505         lockdep_assert_held(&stp->st_stateown    1474         lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1506                                                  1475 
1507         if (list_empty(&stp->st_perfile))        1476         if (list_empty(&stp->st_perfile))
1508                 return false;                    1477                 return false;
1509                                                  1478 
1510         spin_lock(&fp->fi_lock);                 1479         spin_lock(&fp->fi_lock);
1511         list_del_init(&stp->st_perfile);         1480         list_del_init(&stp->st_perfile);
1512         spin_unlock(&fp->fi_lock);               1481         spin_unlock(&fp->fi_lock);
1513         list_del(&stp->st_perstateowner);        1482         list_del(&stp->st_perstateowner);
1514         return true;                             1483         return true;
1515 }                                                1484 }
1516                                                  1485 
1517 static void nfs4_free_ol_stateid(struct nfs4_    1486 static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
1518 {                                                1487 {
1519         struct nfs4_ol_stateid *stp = openloc    1488         struct nfs4_ol_stateid *stp = openlockstateid(stid);
1520                                                  1489 
1521         put_clnt_odstate(stp->st_clnt_odstate    1490         put_clnt_odstate(stp->st_clnt_odstate);
1522         release_all_access(stp);                 1491         release_all_access(stp);
1523         if (stp->st_stateowner)                  1492         if (stp->st_stateowner)
1524                 nfs4_put_stateowner(stp->st_s    1493                 nfs4_put_stateowner(stp->st_stateowner);
1525         WARN_ON(!list_empty(&stid->sc_cp_list    1494         WARN_ON(!list_empty(&stid->sc_cp_list));
1526         kmem_cache_free(stateid_slab, stid);     1495         kmem_cache_free(stateid_slab, stid);
1527 }                                                1496 }
1528                                                  1497 
1529 static void nfs4_free_lock_stateid(struct nfs    1498 static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
1530 {                                                1499 {
1531         struct nfs4_ol_stateid *stp = openloc    1500         struct nfs4_ol_stateid *stp = openlockstateid(stid);
1532         struct nfs4_lockowner *lo = lockowner    1501         struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
1533         struct nfsd_file *nf;                    1502         struct nfsd_file *nf;
1534                                                  1503 
1535         nf = find_any_file(stp->st_stid.sc_fi    1504         nf = find_any_file(stp->st_stid.sc_file);
1536         if (nf) {                                1505         if (nf) {
1537                 get_file(nf->nf_file);           1506                 get_file(nf->nf_file);
1538                 filp_close(nf->nf_file, (fl_o    1507                 filp_close(nf->nf_file, (fl_owner_t)lo);
1539                 nfsd_file_put(nf);               1508                 nfsd_file_put(nf);
1540         }                                        1509         }
1541         nfs4_free_ol_stateid(stid);              1510         nfs4_free_ol_stateid(stid);
1542 }                                                1511 }
1543                                                  1512 
1544 /*                                               1513 /*
1545  * Put the persistent reference to an already    1514  * Put the persistent reference to an already unhashed generic stateid, while
1546  * holding the cl_lock. If it's the last refe    1515  * holding the cl_lock. If it's the last reference, then put it onto the
1547  * reaplist for later destruction.               1516  * reaplist for later destruction.
1548  */                                              1517  */
1549 static void put_ol_stateid_locked(struct nfs4    1518 static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1550                                        struct    1519                                        struct list_head *reaplist)
1551 {                                                1520 {
1552         struct nfs4_stid *s = &stp->st_stid;     1521         struct nfs4_stid *s = &stp->st_stid;
1553         struct nfs4_client *clp = s->sc_clien    1522         struct nfs4_client *clp = s->sc_client;
1554                                                  1523 
1555         lockdep_assert_held(&clp->cl_lock);      1524         lockdep_assert_held(&clp->cl_lock);
1556                                                  1525 
1557         WARN_ON_ONCE(!list_empty(&stp->st_loc    1526         WARN_ON_ONCE(!list_empty(&stp->st_locks));
1558                                                  1527 
1559         if (!refcount_dec_and_test(&s->sc_cou    1528         if (!refcount_dec_and_test(&s->sc_count)) {
1560                 wake_up_all(&close_wq);          1529                 wake_up_all(&close_wq);
1561                 return;                          1530                 return;
1562         }                                        1531         }
1563                                                  1532 
1564         idr_remove(&clp->cl_stateids, s->sc_s    1533         idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1565         if (s->sc_status & SC_STATUS_ADMIN_RE << 
1566                 atomic_dec(&s->sc_client->cl_ << 
1567         list_add(&stp->st_locks, reaplist);      1534         list_add(&stp->st_locks, reaplist);
1568 }                                                1535 }
1569                                                  1536 
1570 static bool unhash_lock_stateid(struct nfs4_o    1537 static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
1571 {                                                1538 {
1572         lockdep_assert_held(&stp->st_stid.sc_    1539         lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1573                                                  1540 
1574         if (!unhash_ol_stateid(stp))             1541         if (!unhash_ol_stateid(stp))
1575                 return false;                    1542                 return false;
1576         list_del_init(&stp->st_locks);           1543         list_del_init(&stp->st_locks);
1577         stp->st_stid.sc_status |= SC_STATUS_C !! 1544         nfs4_unhash_stid(&stp->st_stid);
1578         return true;                             1545         return true;
1579 }                                                1546 }
1580                                                  1547 
1581 static void release_lock_stateid(struct nfs4_    1548 static void release_lock_stateid(struct nfs4_ol_stateid *stp)
1582 {                                                1549 {
1583         struct nfs4_client *clp = stp->st_sti    1550         struct nfs4_client *clp = stp->st_stid.sc_client;
1584         bool unhashed;                           1551         bool unhashed;
1585                                                  1552 
1586         spin_lock(&clp->cl_lock);                1553         spin_lock(&clp->cl_lock);
1587         unhashed = unhash_lock_stateid(stp);     1554         unhashed = unhash_lock_stateid(stp);
1588         spin_unlock(&clp->cl_lock);              1555         spin_unlock(&clp->cl_lock);
1589         if (unhashed)                            1556         if (unhashed)
1590                 nfs4_put_stid(&stp->st_stid);    1557                 nfs4_put_stid(&stp->st_stid);
1591 }                                                1558 }
1592                                                  1559 
1593 static void unhash_lockowner_locked(struct nf    1560 static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
1594 {                                                1561 {
1595         struct nfs4_client *clp = lo->lo_owne    1562         struct nfs4_client *clp = lo->lo_owner.so_client;
1596                                                  1563 
1597         lockdep_assert_held(&clp->cl_lock);      1564         lockdep_assert_held(&clp->cl_lock);
1598                                                  1565 
1599         list_del_init(&lo->lo_owner.so_strhas    1566         list_del_init(&lo->lo_owner.so_strhash);
1600 }                                                1567 }
1601                                                  1568 
1602 /*                                               1569 /*
1603  * Free a list of generic stateids that were     1570  * Free a list of generic stateids that were collected earlier after being
1604  * fully unhashed.                               1571  * fully unhashed.
1605  */                                              1572  */
1606 static void                                      1573 static void
1607 free_ol_stateid_reaplist(struct list_head *re    1574 free_ol_stateid_reaplist(struct list_head *reaplist)
1608 {                                                1575 {
1609         struct nfs4_ol_stateid *stp;             1576         struct nfs4_ol_stateid *stp;
1610         struct nfs4_file *fp;                    1577         struct nfs4_file *fp;
1611                                                  1578 
1612         might_sleep();                           1579         might_sleep();
1613                                                  1580 
1614         while (!list_empty(reaplist)) {          1581         while (!list_empty(reaplist)) {
1615                 stp = list_first_entry(reapli    1582                 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1616                                        st_loc    1583                                        st_locks);
1617                 list_del(&stp->st_locks);        1584                 list_del(&stp->st_locks);
1618                 fp = stp->st_stid.sc_file;       1585                 fp = stp->st_stid.sc_file;
1619                 stp->st_stid.sc_free(&stp->st    1586                 stp->st_stid.sc_free(&stp->st_stid);
1620                 if (fp)                          1587                 if (fp)
1621                         put_nfs4_file(fp);       1588                         put_nfs4_file(fp);
1622         }                                        1589         }
1623 }                                                1590 }
1624                                                  1591 
1625 static void release_open_stateid_locks(struct    1592 static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1626                                        struct    1593                                        struct list_head *reaplist)
1627 {                                                1594 {
1628         struct nfs4_ol_stateid *stp;             1595         struct nfs4_ol_stateid *stp;
1629                                                  1596 
1630         lockdep_assert_held(&open_stp->st_sti    1597         lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1631                                                  1598 
1632         while (!list_empty(&open_stp->st_lock    1599         while (!list_empty(&open_stp->st_locks)) {
1633                 stp = list_entry(open_stp->st    1600                 stp = list_entry(open_stp->st_locks.next,
1634                                 struct nfs4_o    1601                                 struct nfs4_ol_stateid, st_locks);
1635                 unhash_lock_stateid(stp);     !! 1602                 WARN_ON(!unhash_lock_stateid(stp));
1636                 put_ol_stateid_locked(stp, re    1603                 put_ol_stateid_locked(stp, reaplist);
1637         }                                        1604         }
1638 }                                                1605 }
1639                                                  1606 
1640 static bool unhash_open_stateid(struct nfs4_o    1607 static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
1641                                 struct list_h    1608                                 struct list_head *reaplist)
1642 {                                                1609 {
1643         lockdep_assert_held(&stp->st_stid.sc_    1610         lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1644                                                  1611 
1645         if (!unhash_ol_stateid(stp))             1612         if (!unhash_ol_stateid(stp))
1646                 return false;                    1613                 return false;
1647         release_open_stateid_locks(stp, reapl    1614         release_open_stateid_locks(stp, reaplist);
1648         return true;                             1615         return true;
1649 }                                                1616 }
1650                                                  1617 
1651 static void release_open_stateid(struct nfs4_    1618 static void release_open_stateid(struct nfs4_ol_stateid *stp)
1652 {                                                1619 {
1653         LIST_HEAD(reaplist);                     1620         LIST_HEAD(reaplist);
1654                                                  1621 
1655         spin_lock(&stp->st_stid.sc_client->cl    1622         spin_lock(&stp->st_stid.sc_client->cl_lock);
1656         stp->st_stid.sc_status |= SC_STATUS_C << 
1657         if (unhash_open_stateid(stp, &reaplis    1623         if (unhash_open_stateid(stp, &reaplist))
1658                 put_ol_stateid_locked(stp, &r    1624                 put_ol_stateid_locked(stp, &reaplist);
1659         spin_unlock(&stp->st_stid.sc_client->    1625         spin_unlock(&stp->st_stid.sc_client->cl_lock);
1660         free_ol_stateid_reaplist(&reaplist);     1626         free_ol_stateid_reaplist(&reaplist);
1661 }                                                1627 }
1662                                                  1628 
1663 static void unhash_openowner_locked(struct nf    1629 static void unhash_openowner_locked(struct nfs4_openowner *oo)
1664 {                                                1630 {
1665         struct nfs4_client *clp = oo->oo_owne    1631         struct nfs4_client *clp = oo->oo_owner.so_client;
1666                                                  1632 
1667         lockdep_assert_held(&clp->cl_lock);      1633         lockdep_assert_held(&clp->cl_lock);
1668                                                  1634 
1669         list_del_init(&oo->oo_owner.so_strhas    1635         list_del_init(&oo->oo_owner.so_strhash);
1670         list_del_init(&oo->oo_perclient);        1636         list_del_init(&oo->oo_perclient);
1671 }                                                1637 }
1672                                                  1638 
1673 static void release_last_closed_stateid(struc    1639 static void release_last_closed_stateid(struct nfs4_openowner *oo)
1674 {                                                1640 {
1675         struct nfsd_net *nn = net_generic(oo-    1641         struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1676                                           nfs    1642                                           nfsd_net_id);
1677         struct nfs4_ol_stateid *s;               1643         struct nfs4_ol_stateid *s;
1678                                                  1644 
1679         spin_lock(&nn->client_lock);             1645         spin_lock(&nn->client_lock);
1680         s = oo->oo_last_closed_stid;             1646         s = oo->oo_last_closed_stid;
1681         if (s) {                                 1647         if (s) {
1682                 list_del_init(&oo->oo_close_l    1648                 list_del_init(&oo->oo_close_lru);
1683                 oo->oo_last_closed_stid = NUL    1649                 oo->oo_last_closed_stid = NULL;
1684         }                                        1650         }
1685         spin_unlock(&nn->client_lock);           1651         spin_unlock(&nn->client_lock);
1686         if (s)                                   1652         if (s)
1687                 nfs4_put_stid(&s->st_stid);      1653                 nfs4_put_stid(&s->st_stid);
1688 }                                                1654 }
1689                                                  1655 
1690 static void release_openowner(struct nfs4_ope    1656 static void release_openowner(struct nfs4_openowner *oo)
1691 {                                                1657 {
1692         struct nfs4_ol_stateid *stp;             1658         struct nfs4_ol_stateid *stp;
1693         struct nfs4_client *clp = oo->oo_owne    1659         struct nfs4_client *clp = oo->oo_owner.so_client;
1694         LIST_HEAD(reaplist);                  !! 1660         struct list_head reaplist;
                                                   >> 1661 
                                                   >> 1662         INIT_LIST_HEAD(&reaplist);
1695                                                  1663 
1696         spin_lock(&clp->cl_lock);                1664         spin_lock(&clp->cl_lock);
1697         unhash_openowner_locked(oo);             1665         unhash_openowner_locked(oo);
1698         while (!list_empty(&oo->oo_owner.so_s    1666         while (!list_empty(&oo->oo_owner.so_stateids)) {
1699                 stp = list_first_entry(&oo->o    1667                 stp = list_first_entry(&oo->oo_owner.so_stateids,
1700                                 struct nfs4_o    1668                                 struct nfs4_ol_stateid, st_perstateowner);
1701                 if (unhash_open_stateid(stp,     1669                 if (unhash_open_stateid(stp, &reaplist))
1702                         put_ol_stateid_locked    1670                         put_ol_stateid_locked(stp, &reaplist);
1703         }                                        1671         }
1704         spin_unlock(&clp->cl_lock);              1672         spin_unlock(&clp->cl_lock);
1705         free_ol_stateid_reaplist(&reaplist);     1673         free_ol_stateid_reaplist(&reaplist);
1706         release_last_closed_stateid(oo);         1674         release_last_closed_stateid(oo);
1707         nfs4_put_stateowner(&oo->oo_owner);      1675         nfs4_put_stateowner(&oo->oo_owner);
1708 }                                                1676 }
1709                                                  1677 
1710 static struct nfs4_stid *find_one_sb_stid(str << 
1711                                           str << 
1712                                           uns << 
1713 {                                             << 
1714         unsigned long id, tmp;                << 
1715         struct nfs4_stid *stid;               << 
1716                                               << 
1717         spin_lock(&clp->cl_lock);             << 
1718         idr_for_each_entry_ul(&clp->cl_statei << 
1719                 if ((stid->sc_type & sc_types << 
1720                     stid->sc_status == 0 &&   << 
1721                     stid->sc_file->fi_inode-> << 
1722                         refcount_inc(&stid->s << 
1723                         break;                << 
1724                 }                             << 
1725         spin_unlock(&clp->cl_lock);           << 
1726         return stid;                          << 
1727 }                                             << 
1728                                               << 
1729 /**                                           << 
1730  * nfsd4_revoke_states - revoke all nfsv4 sta << 
1731  * @net:  used to identify instance of nfsd ( << 
1732  * @sb:   super_block used to identify target << 
1733  *                                            << 
1734  * All nfs4 states (open, lock, delegation, l << 
1735  * and associated with a file on the given fi << 
1736  * in any files being closed and so all refer << 
1737  * being released.  Thus nfsd will no longer  << 
1738  * unmounted.                                 << 
1739  *                                            << 
1740  * The clients which own the states will subs << 
1741  * states have been "admin-revoked".          << 
1742  */                                           << 
1743 void nfsd4_revoke_states(struct net *net, str << 
1744 {                                             << 
1745         struct nfsd_net *nn = net_generic(net << 
1746         unsigned int idhashval;               << 
1747         unsigned int sc_types;                << 
1748                                               << 
1749         sc_types = SC_TYPE_OPEN | SC_TYPE_LOC << 
1750                                               << 
1751         spin_lock(&nn->client_lock);          << 
1752         for (idhashval = 0; idhashval < CLIEN << 
1753                 struct list_head *head = &nn- << 
1754                 struct nfs4_client *clp;      << 
1755         retry:                                << 
1756                 list_for_each_entry(clp, head << 
1757                         struct nfs4_stid *sti << 
1758                                               << 
1759                         if (stid) {           << 
1760                                 struct nfs4_o << 
1761                                 struct nfs4_d << 
1762                                 struct nfs4_l << 
1763                                               << 
1764                                 spin_unlock(& << 
1765                                 switch (stid- << 
1766                                 case SC_TYPE_ << 
1767                                         stp = << 
1768                                         mutex << 
1769                                               << 
1770                                               << 
1771                                         spin_ << 
1772                                         if (s << 
1773                                               << 
1774                                               << 
1775                                               << 
1776                                               << 
1777                                               << 
1778                                         } els << 
1779                                               << 
1780                                         mutex << 
1781                                         break << 
1782                                 case SC_TYPE_ << 
1783                                         stp = << 
1784                                         mutex << 
1785                                               << 
1786                                         spin_ << 
1787                                         if (s << 
1788                                               << 
1789                                               << 
1790                                               << 
1791                                               << 
1792                                               << 
1793                                               << 
1794                                               << 
1795                                               << 
1796                                               << 
1797                                               << 
1798                                               << 
1799                                               << 
1800                                               << 
1801                                               << 
1802                                               << 
1803                                               << 
1804                                         } els << 
1805                                               << 
1806                                         mutex << 
1807                                         break << 
1808                                 case SC_TYPE_ << 
1809                                         refco << 
1810                                         dp =  << 
1811                                         spin_ << 
1812                                         if (! << 
1813                                               << 
1814                                               << 
1815                                         spin_ << 
1816                                         if (d << 
1817                                               << 
1818                                         break << 
1819                                 case SC_TYPE_ << 
1820                                         ls =  << 
1821                                         nfsd4 << 
1822                                         break << 
1823                                 }             << 
1824                                 nfs4_put_stid << 
1825                                 spin_lock(&nn << 
1826                                 if (clp->cl_m << 
1827                                         /* Al << 
1828                                          * st << 
1829                                          * se << 
1830                                          * se << 
1831                                          */   << 
1832                                         nn->n << 
1833                                               << 
1834                                 goto retry;   << 
1835                         }                     << 
1836                 }                             << 
1837         }                                     << 
1838         spin_unlock(&nn->client_lock);        << 
1839 }                                             << 
1840                                               << 
1841 static inline int                                1678 static inline int
1842 hash_sessionid(struct nfs4_sessionid *session    1679 hash_sessionid(struct nfs4_sessionid *sessionid)
1843 {                                                1680 {
1844         struct nfsd4_sessionid *sid = (struct    1681         struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1845                                                  1682 
1846         return sid->sequence % SESSION_HASH_S    1683         return sid->sequence % SESSION_HASH_SIZE;
1847 }                                                1684 }
1848                                                  1685 
1849 #ifdef CONFIG_SUNRPC_DEBUG                       1686 #ifdef CONFIG_SUNRPC_DEBUG
1850 static inline void                               1687 static inline void
1851 dump_sessionid(const char *fn, struct nfs4_se    1688 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1852 {                                                1689 {
1853         u32 *ptr = (u32 *)(&sessionid->data[0    1690         u32 *ptr = (u32 *)(&sessionid->data[0]);
1854         dprintk("%s: %u:%u:%u:%u\n", fn, ptr[    1691         dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1855 }                                                1692 }
1856 #else                                            1693 #else
1857 static inline void                               1694 static inline void
1858 dump_sessionid(const char *fn, struct nfs4_se    1695 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1859 {                                                1696 {
1860 }                                                1697 }
1861 #endif                                           1698 #endif
1862                                                  1699 
1863 /*                                               1700 /*
1864  * Bump the seqid on cstate->replay_owner, an    1701  * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1865  * won't be used for replay.                     1702  * won't be used for replay.
1866  */                                              1703  */
1867 void nfsd4_bump_seqid(struct nfsd4_compound_s    1704 void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1868 {                                                1705 {
1869         struct nfs4_stateowner *so = cstate->    1706         struct nfs4_stateowner *so = cstate->replay_owner;
1870                                                  1707 
1871         if (nfserr == nfserr_replay_me)          1708         if (nfserr == nfserr_replay_me)
1872                 return;                          1709                 return;
1873                                                  1710 
1874         if (!seqid_mutating_err(ntohl(nfserr)    1711         if (!seqid_mutating_err(ntohl(nfserr))) {
1875                 nfsd4_cstate_clear_replay(cst    1712                 nfsd4_cstate_clear_replay(cstate);
1876                 return;                          1713                 return;
1877         }                                        1714         }
1878         if (!so)                                 1715         if (!so)
1879                 return;                          1716                 return;
1880         if (so->so_is_open_owner)                1717         if (so->so_is_open_owner)
1881                 release_last_closed_stateid(o    1718                 release_last_closed_stateid(openowner(so));
1882         so->so_seqid++;                          1719         so->so_seqid++;
1883         return;                                  1720         return;
1884 }                                                1721 }
1885                                                  1722 
1886 static void                                      1723 static void
1887 gen_sessionid(struct nfsd4_session *ses)         1724 gen_sessionid(struct nfsd4_session *ses)
1888 {                                                1725 {
1889         struct nfs4_client *clp = ses->se_cli    1726         struct nfs4_client *clp = ses->se_client;
1890         struct nfsd4_sessionid *sid;             1727         struct nfsd4_sessionid *sid;
1891                                                  1728 
1892         sid = (struct nfsd4_sessionid *)ses->    1729         sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1893         sid->clientid = clp->cl_clientid;        1730         sid->clientid = clp->cl_clientid;
1894         sid->sequence = current_sessionid++;     1731         sid->sequence = current_sessionid++;
1895         sid->reserved = 0;                       1732         sid->reserved = 0;
1896 }                                                1733 }
1897                                                  1734 
1898 /*                                               1735 /*
1899  * The protocol defines ca_maxresponssize_cac    1736  * The protocol defines ca_maxresponssize_cached to include the size of
1900  * the rpc header, but all we need to cache i    1737  * the rpc header, but all we need to cache is the data starting after
1901  * the end of the initial SEQUENCE operation-    1738  * the end of the initial SEQUENCE operation--the rest we regenerate
1902  * each time.  Therefore we can advertise a c    1739  * each time.  Therefore we can advertise a ca_maxresponssize_cached
1903  * value that is the number of bytes in our c    1740  * value that is the number of bytes in our cache plus a few additional
1904  * bytes.  In order to stay on the safe side,    1741  * bytes.  In order to stay on the safe side, and not promise more than
1905  * we can cache, those additional bytes must     1742  * we can cache, those additional bytes must be the minimum possible: 24
1906  * bytes of rpc header (xid through accept st    1743  * bytes of rpc header (xid through accept state, with AUTH_NULL
1907  * verifier), 12 for the compound header (wit    1744  * verifier), 12 for the compound header (with zero-length tag), and 44
1908  * for the SEQUENCE op response:                 1745  * for the SEQUENCE op response:
1909  */                                              1746  */
1910 #define NFSD_MIN_HDR_SEQ_SZ  (24 + 12 + 44)      1747 #define NFSD_MIN_HDR_SEQ_SZ  (24 + 12 + 44)
1911                                                  1748 
1912 static void                                      1749 static void
1913 free_session_slots(struct nfsd4_session *ses)    1750 free_session_slots(struct nfsd4_session *ses)
1914 {                                                1751 {
1915         int i;                                   1752         int i;
1916                                                  1753 
1917         for (i = 0; i < ses->se_fchannel.maxr    1754         for (i = 0; i < ses->se_fchannel.maxreqs; i++) {
1918                 free_svc_cred(&ses->se_slots[    1755                 free_svc_cred(&ses->se_slots[i]->sl_cred);
1919                 kfree(ses->se_slots[i]);         1756                 kfree(ses->se_slots[i]);
1920         }                                        1757         }
1921 }                                                1758 }
1922                                                  1759 
1923 /*                                               1760 /*
1924  * We don't actually need to cache the rpc an    1761  * We don't actually need to cache the rpc and session headers, so we
1925  * can allocate a little less for each slot:     1762  * can allocate a little less for each slot:
1926  */                                              1763  */
1927 static inline u32 slot_bytes(struct nfsd4_cha    1764 static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
1928 {                                                1765 {
1929         u32 size;                                1766         u32 size;
1930                                                  1767 
1931         if (ca->maxresp_cached < NFSD_MIN_HDR    1768         if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1932                 size = 0;                        1769                 size = 0;
1933         else                                     1770         else
1934                 size = ca->maxresp_cached - N    1771                 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1935         return size + sizeof(struct nfsd4_slo    1772         return size + sizeof(struct nfsd4_slot);
1936 }                                                1773 }
1937                                                  1774 
1938 /*                                               1775 /*
1939  * XXX: If we run out of reserved DRC memory     1776  * XXX: If we run out of reserved DRC memory we could (up to a point)
1940  * re-negotiate active sessions and reduce th    1777  * re-negotiate active sessions and reduce their slot usage to make
1941  * room for new connections. For now we just     1778  * room for new connections. For now we just fail the create session.
1942  */                                              1779  */
1943 static u32 nfsd4_get_drc_mem(struct nfsd4_cha    1780 static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
1944 {                                                1781 {
1945         u32 slotsize = slot_bytes(ca);           1782         u32 slotsize = slot_bytes(ca);
1946         u32 num = ca->maxreqs;                   1783         u32 num = ca->maxreqs;
1947         unsigned long avail, total_avail;        1784         unsigned long avail, total_avail;
1948         unsigned int scale_factor;               1785         unsigned int scale_factor;
1949                                                  1786 
1950         spin_lock(&nfsd_drc_lock);               1787         spin_lock(&nfsd_drc_lock);
1951         if (nfsd_drc_max_mem > nfsd_drc_mem_u    1788         if (nfsd_drc_max_mem > nfsd_drc_mem_used)
1952                 total_avail = nfsd_drc_max_me    1789                 total_avail = nfsd_drc_max_mem - nfsd_drc_mem_used;
1953         else                                     1790         else
1954                 /* We have handed out more sp    1791                 /* We have handed out more space than we chose in
1955                  * set_max_drc() to allow.  T    1792                  * set_max_drc() to allow.  That isn't really a
1956                  * problem as long as that do    1793                  * problem as long as that doesn't make us think we
1957                  * have lots more due to inte    1794                  * have lots more due to integer overflow.
1958                  */                              1795                  */
1959                 total_avail = 0;                 1796                 total_avail = 0;
1960         avail = min((unsigned long)NFSD_MAX_M    1797         avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, total_avail);
1961         /*                                       1798         /*
1962          * Never use more than a fraction of     1799          * Never use more than a fraction of the remaining memory,
1963          * unless it's the only way to give t    1800          * unless it's the only way to give this client a slot.
1964          * The chosen fraction is either 1/8     1801          * The chosen fraction is either 1/8 or 1/number of threads,
1965          * whichever is smaller.  This ensure    1802          * whichever is smaller.  This ensures there are adequate
1966          * slots to support multiple clients     1803          * slots to support multiple clients per thread.
1967          * Give the client one slot even if t    1804          * Give the client one slot even if that would require
1968          * over-allocation--it is better than    1805          * over-allocation--it is better than failure.
1969          */                                      1806          */
1970         scale_factor = max_t(unsigned int, 8,    1807         scale_factor = max_t(unsigned int, 8, nn->nfsd_serv->sv_nrthreads);
1971                                                  1808 
1972         avail = clamp_t(unsigned long, avail,    1809         avail = clamp_t(unsigned long, avail, slotsize,
1973                         total_avail/scale_fac    1810                         total_avail/scale_factor);
1974         num = min_t(int, num, avail / slotsiz    1811         num = min_t(int, num, avail / slotsize);
1975         num = max_t(int, num, 1);                1812         num = max_t(int, num, 1);
1976         nfsd_drc_mem_used += num * slotsize;     1813         nfsd_drc_mem_used += num * slotsize;
1977         spin_unlock(&nfsd_drc_lock);             1814         spin_unlock(&nfsd_drc_lock);
1978                                                  1815 
1979         return num;                              1816         return num;
1980 }                                                1817 }
1981                                                  1818 
1982 static void nfsd4_put_drc_mem(struct nfsd4_ch    1819 static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
1983 {                                                1820 {
1984         int slotsize = slot_bytes(ca);           1821         int slotsize = slot_bytes(ca);
1985                                                  1822 
1986         spin_lock(&nfsd_drc_lock);               1823         spin_lock(&nfsd_drc_lock);
1987         nfsd_drc_mem_used -= slotsize * ca->m    1824         nfsd_drc_mem_used -= slotsize * ca->maxreqs;
1988         spin_unlock(&nfsd_drc_lock);             1825         spin_unlock(&nfsd_drc_lock);
1989 }                                                1826 }
1990                                                  1827 
1991 static struct nfsd4_session *alloc_session(st    1828 static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1992                                            st    1829                                            struct nfsd4_channel_attrs *battrs)
1993 {                                                1830 {
1994         int numslots = fattrs->maxreqs;          1831         int numslots = fattrs->maxreqs;
1995         int slotsize = slot_bytes(fattrs);       1832         int slotsize = slot_bytes(fattrs);
1996         struct nfsd4_session *new;               1833         struct nfsd4_session *new;
1997         int i;                                   1834         int i;
1998                                                  1835 
1999         BUILD_BUG_ON(struct_size(new, se_slot    1836         BUILD_BUG_ON(struct_size(new, se_slots, NFSD_MAX_SLOTS_PER_SESSION)
2000                      > PAGE_SIZE);               1837                      > PAGE_SIZE);
2001                                                  1838 
2002         new = kzalloc(struct_size(new, se_slo    1839         new = kzalloc(struct_size(new, se_slots, numslots), GFP_KERNEL);
2003         if (!new)                                1840         if (!new)
2004                 return NULL;                     1841                 return NULL;
2005         /* allocate each struct nfsd4_slot an    1842         /* allocate each struct nfsd4_slot and data cache in one piece */
2006         for (i = 0; i < numslots; i++) {         1843         for (i = 0; i < numslots; i++) {
2007                 new->se_slots[i] = kzalloc(sl    1844                 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
2008                 if (!new->se_slots[i])           1845                 if (!new->se_slots[i])
2009                         goto out_free;           1846                         goto out_free;
2010         }                                        1847         }
2011                                                  1848 
2012         memcpy(&new->se_fchannel, fattrs, siz    1849         memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
2013         memcpy(&new->se_bchannel, battrs, siz    1850         memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
2014                                                  1851 
2015         return new;                              1852         return new;
2016 out_free:                                        1853 out_free:
2017         while (i--)                              1854         while (i--)
2018                 kfree(new->se_slots[i]);         1855                 kfree(new->se_slots[i]);
2019         kfree(new);                              1856         kfree(new);
2020         return NULL;                             1857         return NULL;
2021 }                                                1858 }
2022                                                  1859 
2023 static void free_conn(struct nfsd4_conn *c)      1860 static void free_conn(struct nfsd4_conn *c)
2024 {                                                1861 {
2025         svc_xprt_put(c->cn_xprt);                1862         svc_xprt_put(c->cn_xprt);
2026         kfree(c);                                1863         kfree(c);
2027 }                                                1864 }
2028                                                  1865 
2029 static void nfsd4_conn_lost(struct svc_xpt_us    1866 static void nfsd4_conn_lost(struct svc_xpt_user *u)
2030 {                                                1867 {
2031         struct nfsd4_conn *c = container_of(u    1868         struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
2032         struct nfs4_client *clp = c->cn_sessi    1869         struct nfs4_client *clp = c->cn_session->se_client;
2033                                                  1870 
2034         trace_nfsd_cb_lost(clp);                 1871         trace_nfsd_cb_lost(clp);
2035                                                  1872 
2036         spin_lock(&clp->cl_lock);                1873         spin_lock(&clp->cl_lock);
2037         if (!list_empty(&c->cn_persession)) {    1874         if (!list_empty(&c->cn_persession)) {
2038                 list_del(&c->cn_persession);     1875                 list_del(&c->cn_persession);
2039                 free_conn(c);                    1876                 free_conn(c);
2040         }                                        1877         }
2041         nfsd4_probe_callback(clp);               1878         nfsd4_probe_callback(clp);
2042         spin_unlock(&clp->cl_lock);              1879         spin_unlock(&clp->cl_lock);
2043 }                                                1880 }
2044                                                  1881 
2045 static struct nfsd4_conn *alloc_conn(struct s    1882 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
2046 {                                                1883 {
2047         struct nfsd4_conn *conn;                 1884         struct nfsd4_conn *conn;
2048                                                  1885 
2049         conn = kmalloc(sizeof(struct nfsd4_co    1886         conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
2050         if (!conn)                               1887         if (!conn)
2051                 return NULL;                     1888                 return NULL;
2052         svc_xprt_get(rqstp->rq_xprt);            1889         svc_xprt_get(rqstp->rq_xprt);
2053         conn->cn_xprt = rqstp->rq_xprt;          1890         conn->cn_xprt = rqstp->rq_xprt;
2054         conn->cn_flags = flags;                  1891         conn->cn_flags = flags;
2055         INIT_LIST_HEAD(&conn->cn_xpt_user.lis    1892         INIT_LIST_HEAD(&conn->cn_xpt_user.list);
2056         return conn;                             1893         return conn;
2057 }                                                1894 }
2058                                                  1895 
2059 static void __nfsd4_hash_conn(struct nfsd4_co    1896 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
2060 {                                                1897 {
2061         conn->cn_session = ses;                  1898         conn->cn_session = ses;
2062         list_add(&conn->cn_persession, &ses->    1899         list_add(&conn->cn_persession, &ses->se_conns);
2063 }                                                1900 }
2064                                                  1901 
2065 static void nfsd4_hash_conn(struct nfsd4_conn    1902 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
2066 {                                                1903 {
2067         struct nfs4_client *clp = ses->se_cli    1904         struct nfs4_client *clp = ses->se_client;
2068                                                  1905 
2069         spin_lock(&clp->cl_lock);                1906         spin_lock(&clp->cl_lock);
2070         __nfsd4_hash_conn(conn, ses);            1907         __nfsd4_hash_conn(conn, ses);
2071         spin_unlock(&clp->cl_lock);              1908         spin_unlock(&clp->cl_lock);
2072 }                                                1909 }
2073                                                  1910 
2074 static int nfsd4_register_conn(struct nfsd4_c    1911 static int nfsd4_register_conn(struct nfsd4_conn *conn)
2075 {                                                1912 {
2076         conn->cn_xpt_user.callback = nfsd4_co    1913         conn->cn_xpt_user.callback = nfsd4_conn_lost;
2077         return register_xpt_user(conn->cn_xpr    1914         return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
2078 }                                                1915 }
2079                                                  1916 
2080 static void nfsd4_init_conn(struct svc_rqst *    1917 static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
2081 {                                                1918 {
2082         int ret;                                 1919         int ret;
2083                                                  1920 
2084         nfsd4_hash_conn(conn, ses);              1921         nfsd4_hash_conn(conn, ses);
2085         ret = nfsd4_register_conn(conn);         1922         ret = nfsd4_register_conn(conn);
2086         if (ret)                                 1923         if (ret)
2087                 /* oops; xprt is already down    1924                 /* oops; xprt is already down: */
2088                 nfsd4_conn_lost(&conn->cn_xpt    1925                 nfsd4_conn_lost(&conn->cn_xpt_user);
2089         /* We may have gained or lost a callb    1926         /* We may have gained or lost a callback channel: */
2090         nfsd4_probe_callback_sync(ses->se_cli    1927         nfsd4_probe_callback_sync(ses->se_client);
2091 }                                                1928 }
2092                                                  1929 
2093 static struct nfsd4_conn *alloc_conn_from_crs    1930 static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
2094 {                                                1931 {
2095         u32 dir = NFS4_CDFC4_FORE;               1932         u32 dir = NFS4_CDFC4_FORE;
2096                                                  1933 
2097         if (cses->flags & SESSION4_BACK_CHAN)    1934         if (cses->flags & SESSION4_BACK_CHAN)
2098                 dir |= NFS4_CDFC4_BACK;          1935                 dir |= NFS4_CDFC4_BACK;
2099         return alloc_conn(rqstp, dir);           1936         return alloc_conn(rqstp, dir);
2100 }                                                1937 }
2101                                                  1938 
2102 /* must be called under client_lock */           1939 /* must be called under client_lock */
2103 static void nfsd4_del_conns(struct nfsd4_sess    1940 static void nfsd4_del_conns(struct nfsd4_session *s)
2104 {                                                1941 {
2105         struct nfs4_client *clp = s->se_clien    1942         struct nfs4_client *clp = s->se_client;
2106         struct nfsd4_conn *c;                    1943         struct nfsd4_conn *c;
2107                                                  1944 
2108         spin_lock(&clp->cl_lock);                1945         spin_lock(&clp->cl_lock);
2109         while (!list_empty(&s->se_conns)) {      1946         while (!list_empty(&s->se_conns)) {
2110                 c = list_first_entry(&s->se_c    1947                 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
2111                 list_del_init(&c->cn_persessi    1948                 list_del_init(&c->cn_persession);
2112                 spin_unlock(&clp->cl_lock);      1949                 spin_unlock(&clp->cl_lock);
2113                                                  1950 
2114                 unregister_xpt_user(c->cn_xpr    1951                 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
2115                 free_conn(c);                    1952                 free_conn(c);
2116                                                  1953 
2117                 spin_lock(&clp->cl_lock);        1954                 spin_lock(&clp->cl_lock);
2118         }                                        1955         }
2119         spin_unlock(&clp->cl_lock);              1956         spin_unlock(&clp->cl_lock);
2120 }                                                1957 }
2121                                                  1958 
2122 static void __free_session(struct nfsd4_sessi    1959 static void __free_session(struct nfsd4_session *ses)
2123 {                                                1960 {
2124         free_session_slots(ses);                 1961         free_session_slots(ses);
2125         kfree(ses);                              1962         kfree(ses);
2126 }                                                1963 }
2127                                                  1964 
2128 static void free_session(struct nfsd4_session    1965 static void free_session(struct nfsd4_session *ses)
2129 {                                                1966 {
2130         nfsd4_del_conns(ses);                    1967         nfsd4_del_conns(ses);
2131         nfsd4_put_drc_mem(&ses->se_fchannel);    1968         nfsd4_put_drc_mem(&ses->se_fchannel);
2132         __free_session(ses);                     1969         __free_session(ses);
2133 }                                                1970 }
2134                                                  1971 
2135 static void init_session(struct svc_rqst *rqs    1972 static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
2136 {                                                1973 {
2137         int idx;                                 1974         int idx;
2138         struct nfsd_net *nn = net_generic(SVC    1975         struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2139                                                  1976 
2140         new->se_client = clp;                    1977         new->se_client = clp;
2141         gen_sessionid(new);                      1978         gen_sessionid(new);
2142                                                  1979 
2143         INIT_LIST_HEAD(&new->se_conns);          1980         INIT_LIST_HEAD(&new->se_conns);
2144                                                  1981 
2145         new->se_cb_seq_nr = 1;                   1982         new->se_cb_seq_nr = 1;
2146         new->se_flags = cses->flags;             1983         new->se_flags = cses->flags;
2147         new->se_cb_prog = cses->callback_prog    1984         new->se_cb_prog = cses->callback_prog;
2148         new->se_cb_sec = cses->cb_sec;           1985         new->se_cb_sec = cses->cb_sec;
2149         atomic_set(&new->se_ref, 0);             1986         atomic_set(&new->se_ref, 0);
2150         idx = hash_sessionid(&new->se_session    1987         idx = hash_sessionid(&new->se_sessionid);
2151         list_add(&new->se_hash, &nn->sessioni    1988         list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
2152         spin_lock(&clp->cl_lock);                1989         spin_lock(&clp->cl_lock);
2153         list_add(&new->se_perclnt, &clp->cl_s    1990         list_add(&new->se_perclnt, &clp->cl_sessions);
2154         spin_unlock(&clp->cl_lock);              1991         spin_unlock(&clp->cl_lock);
2155                                                  1992 
2156         {                                        1993         {
2157                 struct sockaddr *sa = svc_add    1994                 struct sockaddr *sa = svc_addr(rqstp);
2158                 /*                               1995                 /*
2159                  * This is a little silly; wi    1996                  * This is a little silly; with sessions there's no real
2160                  * use for the callback addre    1997                  * use for the callback address.  Use the peer address
2161                  * as a reasonable default fo    1998                  * as a reasonable default for now, but consider fixing
2162                  * the rpc client not to requ    1999                  * the rpc client not to require an address in the
2163                  * future:                       2000                  * future:
2164                  */                              2001                  */
2165                 rpc_copy_addr((struct sockadd    2002                 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
2166                 clp->cl_cb_conn.cb_addrlen =     2003                 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
2167         }                                        2004         }
2168 }                                                2005 }
2169                                                  2006 
2170 /* caller must hold client_lock */               2007 /* caller must hold client_lock */
2171 static struct nfsd4_session *                    2008 static struct nfsd4_session *
2172 __find_in_sessionid_hashtbl(struct nfs4_sessi    2009 __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
2173 {                                                2010 {
2174         struct nfsd4_session *elem;              2011         struct nfsd4_session *elem;
2175         int idx;                                 2012         int idx;
2176         struct nfsd_net *nn = net_generic(net    2013         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2177                                                  2014 
2178         lockdep_assert_held(&nn->client_lock)    2015         lockdep_assert_held(&nn->client_lock);
2179                                                  2016 
2180         dump_sessionid(__func__, sessionid);     2017         dump_sessionid(__func__, sessionid);
2181         idx = hash_sessionid(sessionid);         2018         idx = hash_sessionid(sessionid);
2182         /* Search in the appropriate list */     2019         /* Search in the appropriate list */
2183         list_for_each_entry(elem, &nn->sessio    2020         list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
2184                 if (!memcmp(elem->se_sessioni    2021                 if (!memcmp(elem->se_sessionid.data, sessionid->data,
2185                             NFS4_MAX_SESSIONI    2022                             NFS4_MAX_SESSIONID_LEN)) {
2186                         return elem;             2023                         return elem;
2187                 }                                2024                 }
2188         }                                        2025         }
2189                                                  2026 
2190         dprintk("%s: session not found\n", __    2027         dprintk("%s: session not found\n", __func__);
2191         return NULL;                             2028         return NULL;
2192 }                                                2029 }
2193                                                  2030 
2194 static struct nfsd4_session *                    2031 static struct nfsd4_session *
2195 find_in_sessionid_hashtbl(struct nfs4_session    2032 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
2196                 __be32 *ret)                     2033                 __be32 *ret)
2197 {                                                2034 {
2198         struct nfsd4_session *session;           2035         struct nfsd4_session *session;
2199         __be32 status = nfserr_badsession;       2036         __be32 status = nfserr_badsession;
2200                                                  2037 
2201         session = __find_in_sessionid_hashtbl    2038         session = __find_in_sessionid_hashtbl(sessionid, net);
2202         if (!session)                            2039         if (!session)
2203                 goto out;                        2040                 goto out;
2204         status = nfsd4_get_session_locked(ses    2041         status = nfsd4_get_session_locked(session);
2205         if (status)                              2042         if (status)
2206                 session = NULL;                  2043                 session = NULL;
2207 out:                                             2044 out:
2208         *ret = status;                           2045         *ret = status;
2209         return session;                          2046         return session;
2210 }                                                2047 }
2211                                                  2048 
2212 /* caller must hold client_lock */               2049 /* caller must hold client_lock */
2213 static void                                      2050 static void
2214 unhash_session(struct nfsd4_session *ses)        2051 unhash_session(struct nfsd4_session *ses)
2215 {                                                2052 {
2216         struct nfs4_client *clp = ses->se_cli    2053         struct nfs4_client *clp = ses->se_client;
2217         struct nfsd_net *nn = net_generic(clp    2054         struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2218                                                  2055 
2219         lockdep_assert_held(&nn->client_lock)    2056         lockdep_assert_held(&nn->client_lock);
2220                                                  2057 
2221         list_del(&ses->se_hash);                 2058         list_del(&ses->se_hash);
2222         spin_lock(&ses->se_client->cl_lock);     2059         spin_lock(&ses->se_client->cl_lock);
2223         list_del(&ses->se_perclnt);              2060         list_del(&ses->se_perclnt);
2224         spin_unlock(&ses->se_client->cl_lock)    2061         spin_unlock(&ses->se_client->cl_lock);
2225 }                                                2062 }
2226                                                  2063 
2227 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper    2064 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
2228 static int                                       2065 static int
2229 STALE_CLIENTID(clientid_t *clid, struct nfsd_    2066 STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
2230 {                                                2067 {
2231         /*                                       2068         /*
2232          * We're assuming the clid was not gi    2069          * We're assuming the clid was not given out from a boot
2233          * precisely 2^32 (about 136 years) b    2070          * precisely 2^32 (about 136 years) before this one.  That seems
2234          * a safe assumption:                    2071          * a safe assumption:
2235          */                                      2072          */
2236         if (clid->cl_boot == (u32)nn->boot_ti    2073         if (clid->cl_boot == (u32)nn->boot_time)
2237                 return 0;                        2074                 return 0;
2238         trace_nfsd_clid_stale(clid);             2075         trace_nfsd_clid_stale(clid);
2239         return 1;                                2076         return 1;
2240 }                                                2077 }
2241                                                  2078 
2242 /*                                               2079 /* 
2243  * XXX Should we use a slab cache ?              2080  * XXX Should we use a slab cache ?
2244  * This type of memory management is somewhat    2081  * This type of memory management is somewhat inefficient, but we use it
2245  * anyway since SETCLIENTID is not a common o    2082  * anyway since SETCLIENTID is not a common operation.
2246  */                                              2083  */
2247 static struct nfs4_client *alloc_client(struc    2084 static struct nfs4_client *alloc_client(struct xdr_netobj name,
2248                                 struct nfsd_n    2085                                 struct nfsd_net *nn)
2249 {                                                2086 {
2250         struct nfs4_client *clp;                 2087         struct nfs4_client *clp;
2251         int i;                                   2088         int i;
2252                                                  2089 
2253         if (atomic_read(&nn->nfs4_client_coun    2090         if (atomic_read(&nn->nfs4_client_count) >= nn->nfs4_max_clients) {
2254                 mod_delayed_work(laundry_wq,     2091                 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
2255                 return NULL;                     2092                 return NULL;
2256         }                                        2093         }
2257         clp = kmem_cache_zalloc(client_slab,     2094         clp = kmem_cache_zalloc(client_slab, GFP_KERNEL);
2258         if (clp == NULL)                         2095         if (clp == NULL)
2259                 return NULL;                     2096                 return NULL;
2260         xdr_netobj_dup(&clp->cl_name, &name,     2097         xdr_netobj_dup(&clp->cl_name, &name, GFP_KERNEL);
2261         if (clp->cl_name.data == NULL)           2098         if (clp->cl_name.data == NULL)
2262                 goto err_no_name;                2099                 goto err_no_name;
2263         clp->cl_ownerstr_hashtbl = kmalloc_ar    2100         clp->cl_ownerstr_hashtbl = kmalloc_array(OWNER_HASH_SIZE,
2264                                                  2101                                                  sizeof(struct list_head),
2265                                                  2102                                                  GFP_KERNEL);
2266         if (!clp->cl_ownerstr_hashtbl)           2103         if (!clp->cl_ownerstr_hashtbl)
2267                 goto err_no_hashtbl;             2104                 goto err_no_hashtbl;
2268         clp->cl_callback_wq = alloc_ordered_w << 
2269         if (!clp->cl_callback_wq)             << 
2270                 goto err_no_callback_wq;      << 
2271                                               << 
2272         for (i = 0; i < OWNER_HASH_SIZE; i++)    2105         for (i = 0; i < OWNER_HASH_SIZE; i++)
2273                 INIT_LIST_HEAD(&clp->cl_owner    2106                 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
2274         INIT_LIST_HEAD(&clp->cl_sessions);       2107         INIT_LIST_HEAD(&clp->cl_sessions);
2275         idr_init(&clp->cl_stateids);             2108         idr_init(&clp->cl_stateids);
2276         atomic_set(&clp->cl_rpc_users, 0);       2109         atomic_set(&clp->cl_rpc_users, 0);
2277         clp->cl_cb_state = NFSD4_CB_UNKNOWN;     2110         clp->cl_cb_state = NFSD4_CB_UNKNOWN;
2278         clp->cl_state = NFSD4_ACTIVE;            2111         clp->cl_state = NFSD4_ACTIVE;
2279         atomic_inc(&nn->nfs4_client_count);      2112         atomic_inc(&nn->nfs4_client_count);
2280         atomic_set(&clp->cl_delegs_in_recall,    2113         atomic_set(&clp->cl_delegs_in_recall, 0);
2281         INIT_LIST_HEAD(&clp->cl_idhash);         2114         INIT_LIST_HEAD(&clp->cl_idhash);
2282         INIT_LIST_HEAD(&clp->cl_openowners);     2115         INIT_LIST_HEAD(&clp->cl_openowners);
2283         INIT_LIST_HEAD(&clp->cl_delegations);    2116         INIT_LIST_HEAD(&clp->cl_delegations);
2284         INIT_LIST_HEAD(&clp->cl_lru);            2117         INIT_LIST_HEAD(&clp->cl_lru);
2285         INIT_LIST_HEAD(&clp->cl_revoked);        2118         INIT_LIST_HEAD(&clp->cl_revoked);
2286 #ifdef CONFIG_NFSD_PNFS                          2119 #ifdef CONFIG_NFSD_PNFS
2287         INIT_LIST_HEAD(&clp->cl_lo_states);      2120         INIT_LIST_HEAD(&clp->cl_lo_states);
2288 #endif                                           2121 #endif
2289         INIT_LIST_HEAD(&clp->async_copies);      2122         INIT_LIST_HEAD(&clp->async_copies);
2290         spin_lock_init(&clp->async_lock);        2123         spin_lock_init(&clp->async_lock);
2291         spin_lock_init(&clp->cl_lock);           2124         spin_lock_init(&clp->cl_lock);
2292         rpc_init_wait_queue(&clp->cl_cb_waitq    2125         rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
2293         return clp;                              2126         return clp;
2294 err_no_callback_wq:                           << 
2295         kfree(clp->cl_ownerstr_hashtbl);      << 
2296 err_no_hashtbl:                                  2127 err_no_hashtbl:
2297         kfree(clp->cl_name.data);                2128         kfree(clp->cl_name.data);
2298 err_no_name:                                     2129 err_no_name:
2299         kmem_cache_free(client_slab, clp);       2130         kmem_cache_free(client_slab, clp);
2300         return NULL;                             2131         return NULL;
2301 }                                                2132 }
2302                                                  2133 
2303 static void __free_client(struct kref *k)        2134 static void __free_client(struct kref *k)
2304 {                                                2135 {
2305         struct nfsdfs_client *c = container_o    2136         struct nfsdfs_client *c = container_of(k, struct nfsdfs_client, cl_ref);
2306         struct nfs4_client *clp = container_o    2137         struct nfs4_client *clp = container_of(c, struct nfs4_client, cl_nfsdfs);
2307                                                  2138 
2308         free_svc_cred(&clp->cl_cred);            2139         free_svc_cred(&clp->cl_cred);
2309         destroy_workqueue(clp->cl_callback_wq << 
2310         kfree(clp->cl_ownerstr_hashtbl);         2140         kfree(clp->cl_ownerstr_hashtbl);
2311         kfree(clp->cl_name.data);                2141         kfree(clp->cl_name.data);
2312         kfree(clp->cl_nii_domain.data);          2142         kfree(clp->cl_nii_domain.data);
2313         kfree(clp->cl_nii_name.data);            2143         kfree(clp->cl_nii_name.data);
2314         idr_destroy(&clp->cl_stateids);          2144         idr_destroy(&clp->cl_stateids);
2315         kfree(clp->cl_ra);                       2145         kfree(clp->cl_ra);
2316         kmem_cache_free(client_slab, clp);       2146         kmem_cache_free(client_slab, clp);
2317 }                                                2147 }
2318                                                  2148 
2319 static void drop_client(struct nfs4_client *c    2149 static void drop_client(struct nfs4_client *clp)
2320 {                                                2150 {
2321         kref_put(&clp->cl_nfsdfs.cl_ref, __fr    2151         kref_put(&clp->cl_nfsdfs.cl_ref, __free_client);
2322 }                                                2152 }
2323                                                  2153 
2324 static void                                      2154 static void
2325 free_client(struct nfs4_client *clp)             2155 free_client(struct nfs4_client *clp)
2326 {                                                2156 {
2327         while (!list_empty(&clp->cl_sessions)    2157         while (!list_empty(&clp->cl_sessions)) {
2328                 struct nfsd4_session *ses;       2158                 struct nfsd4_session *ses;
2329                 ses = list_entry(clp->cl_sess    2159                 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
2330                                 se_perclnt);     2160                                 se_perclnt);
2331                 list_del(&ses->se_perclnt);      2161                 list_del(&ses->se_perclnt);
2332                 WARN_ON_ONCE(atomic_read(&ses    2162                 WARN_ON_ONCE(atomic_read(&ses->se_ref));
2333                 free_session(ses);               2163                 free_session(ses);
2334         }                                        2164         }
2335         rpc_destroy_wait_queue(&clp->cl_cb_wa    2165         rpc_destroy_wait_queue(&clp->cl_cb_waitq);
2336         if (clp->cl_nfsd_dentry) {               2166         if (clp->cl_nfsd_dentry) {
2337                 nfsd_client_rmdir(clp->cl_nfs    2167                 nfsd_client_rmdir(clp->cl_nfsd_dentry);
2338                 clp->cl_nfsd_dentry = NULL;      2168                 clp->cl_nfsd_dentry = NULL;
2339                 wake_up_all(&expiry_wq);         2169                 wake_up_all(&expiry_wq);
2340         }                                        2170         }
2341         drop_client(clp);                        2171         drop_client(clp);
2342 }                                                2172 }
2343                                                  2173 
2344 /* must be called under the client_lock */       2174 /* must be called under the client_lock */
2345 static void                                      2175 static void
2346 unhash_client_locked(struct nfs4_client *clp)    2176 unhash_client_locked(struct nfs4_client *clp)
2347 {                                                2177 {
2348         struct nfsd_net *nn = net_generic(clp    2178         struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2349         struct nfsd4_session *ses;               2179         struct nfsd4_session *ses;
2350                                                  2180 
2351         lockdep_assert_held(&nn->client_lock)    2181         lockdep_assert_held(&nn->client_lock);
2352                                                  2182 
2353         /* Mark the client as expired! */        2183         /* Mark the client as expired! */
2354         clp->cl_time = 0;                        2184         clp->cl_time = 0;
2355         /* Make it invisible */                  2185         /* Make it invisible */
2356         if (!list_empty(&clp->cl_idhash)) {      2186         if (!list_empty(&clp->cl_idhash)) {
2357                 list_del_init(&clp->cl_idhash    2187                 list_del_init(&clp->cl_idhash);
2358                 if (test_bit(NFSD4_CLIENT_CON    2188                 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
2359                         rb_erase(&clp->cl_nam    2189                         rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
2360                 else                             2190                 else
2361                         rb_erase(&clp->cl_nam    2191                         rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
2362         }                                        2192         }
2363         list_del_init(&clp->cl_lru);             2193         list_del_init(&clp->cl_lru);
2364         spin_lock(&clp->cl_lock);                2194         spin_lock(&clp->cl_lock);
2365         list_for_each_entry(ses, &clp->cl_ses    2195         list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
2366                 list_del_init(&ses->se_hash);    2196                 list_del_init(&ses->se_hash);
2367         spin_unlock(&clp->cl_lock);              2197         spin_unlock(&clp->cl_lock);
2368 }                                                2198 }
2369                                                  2199 
2370 static void                                      2200 static void
2371 unhash_client(struct nfs4_client *clp)           2201 unhash_client(struct nfs4_client *clp)
2372 {                                                2202 {
2373         struct nfsd_net *nn = net_generic(clp    2203         struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2374                                                  2204 
2375         spin_lock(&nn->client_lock);             2205         spin_lock(&nn->client_lock);
2376         unhash_client_locked(clp);               2206         unhash_client_locked(clp);
2377         spin_unlock(&nn->client_lock);           2207         spin_unlock(&nn->client_lock);
2378 }                                                2208 }
2379                                                  2209 
2380 static __be32 mark_client_expired_locked(stru    2210 static __be32 mark_client_expired_locked(struct nfs4_client *clp)
2381 {                                                2211 {
2382         int users = atomic_read(&clp->cl_rpc_ !! 2212         if (atomic_read(&clp->cl_rpc_users))
2383                                               << 
2384         trace_nfsd_mark_client_expired(clp, u << 
2385                                               << 
2386         if (users)                            << 
2387                 return nfserr_jukebox;           2213                 return nfserr_jukebox;
2388         unhash_client_locked(clp);               2214         unhash_client_locked(clp);
2389         return nfs_ok;                           2215         return nfs_ok;
2390 }                                                2216 }
2391                                                  2217 
2392 static void                                      2218 static void
2393 __destroy_client(struct nfs4_client *clp)        2219 __destroy_client(struct nfs4_client *clp)
2394 {                                                2220 {
2395         struct nfsd_net *nn = net_generic(clp    2221         struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2396         int i;                                   2222         int i;
2397         struct nfs4_openowner *oo;               2223         struct nfs4_openowner *oo;
2398         struct nfs4_delegation *dp;              2224         struct nfs4_delegation *dp;
2399         LIST_HEAD(reaplist);                  !! 2225         struct list_head reaplist;
2400                                                  2226 
                                                   >> 2227         INIT_LIST_HEAD(&reaplist);
2401         spin_lock(&state_lock);                  2228         spin_lock(&state_lock);
2402         while (!list_empty(&clp->cl_delegatio    2229         while (!list_empty(&clp->cl_delegations)) {
2403                 dp = list_entry(clp->cl_deleg    2230                 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
2404                 unhash_delegation_locked(dp,  !! 2231                 WARN_ON(!unhash_delegation_locked(dp));
2405                 list_add(&dp->dl_recall_lru,     2232                 list_add(&dp->dl_recall_lru, &reaplist);
2406         }                                        2233         }
2407         spin_unlock(&state_lock);                2234         spin_unlock(&state_lock);
2408         while (!list_empty(&reaplist)) {         2235         while (!list_empty(&reaplist)) {
2409                 dp = list_entry(reaplist.next    2236                 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
2410                 list_del_init(&dp->dl_recall_    2237                 list_del_init(&dp->dl_recall_lru);
2411                 destroy_unhashed_deleg(dp);      2238                 destroy_unhashed_deleg(dp);
2412         }                                        2239         }
2413         while (!list_empty(&clp->cl_revoked))    2240         while (!list_empty(&clp->cl_revoked)) {
2414                 dp = list_entry(clp->cl_revok    2241                 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
2415                 list_del_init(&dp->dl_recall_    2242                 list_del_init(&dp->dl_recall_lru);
2416                 nfs4_put_stid(&dp->dl_stid);     2243                 nfs4_put_stid(&dp->dl_stid);
2417         }                                        2244         }
2418         while (!list_empty(&clp->cl_openowner    2245         while (!list_empty(&clp->cl_openowners)) {
2419                 oo = list_entry(clp->cl_openo    2246                 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
2420                 nfs4_get_stateowner(&oo->oo_o    2247                 nfs4_get_stateowner(&oo->oo_owner);
2421                 release_openowner(oo);           2248                 release_openowner(oo);
2422         }                                        2249         }
2423         for (i = 0; i < OWNER_HASH_SIZE; i++)    2250         for (i = 0; i < OWNER_HASH_SIZE; i++) {
2424                 struct nfs4_stateowner *so, *    2251                 struct nfs4_stateowner *so, *tmp;
2425                                                  2252 
2426                 list_for_each_entry_safe(so,     2253                 list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i],
2427                                          so_s    2254                                          so_strhash) {
2428                         /* Should be no openo    2255                         /* Should be no openowners at this point */
2429                         WARN_ON_ONCE(so->so_i    2256                         WARN_ON_ONCE(so->so_is_open_owner);
2430                         remove_blocked_locks(    2257                         remove_blocked_locks(lockowner(so));
2431                 }                                2258                 }
2432         }                                        2259         }
2433         nfsd4_return_all_client_layouts(clp);    2260         nfsd4_return_all_client_layouts(clp);
2434         nfsd4_shutdown_copy(clp);                2261         nfsd4_shutdown_copy(clp);
2435         nfsd4_shutdown_callback(clp);            2262         nfsd4_shutdown_callback(clp);
2436         if (clp->cl_cb_conn.cb_xprt)             2263         if (clp->cl_cb_conn.cb_xprt)
2437                 svc_xprt_put(clp->cl_cb_conn.    2264                 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
2438         atomic_add_unless(&nn->nfs4_client_co    2265         atomic_add_unless(&nn->nfs4_client_count, -1, 0);
2439         nfsd4_dec_courtesy_client_count(nn, c    2266         nfsd4_dec_courtesy_client_count(nn, clp);
2440         free_client(clp);                        2267         free_client(clp);
2441         wake_up_all(&expiry_wq);                 2268         wake_up_all(&expiry_wq);
2442 }                                                2269 }
2443                                                  2270 
2444 static void                                      2271 static void
2445 destroy_client(struct nfs4_client *clp)          2272 destroy_client(struct nfs4_client *clp)
2446 {                                                2273 {
2447         unhash_client(clp);                      2274         unhash_client(clp);
2448         __destroy_client(clp);                   2275         __destroy_client(clp);
2449 }                                                2276 }
2450                                                  2277 
2451 static void inc_reclaim_complete(struct nfs4_    2278 static void inc_reclaim_complete(struct nfs4_client *clp)
2452 {                                                2279 {
2453         struct nfsd_net *nn = net_generic(clp    2280         struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2454                                                  2281 
2455         if (!nn->track_reclaim_completes)        2282         if (!nn->track_reclaim_completes)
2456                 return;                          2283                 return;
2457         if (!nfsd4_find_reclaim_client(clp->c    2284         if (!nfsd4_find_reclaim_client(clp->cl_name, nn))
2458                 return;                          2285                 return;
2459         if (atomic_inc_return(&nn->nr_reclaim    2286         if (atomic_inc_return(&nn->nr_reclaim_complete) ==
2460                         nn->reclaim_str_hasht    2287                         nn->reclaim_str_hashtbl_size) {
2461                 printk(KERN_INFO "NFSD: all c    2288                 printk(KERN_INFO "NFSD: all clients done reclaiming, ending NFSv4 grace period (net %x)\n",
2462                                 clp->net->ns.    2289                                 clp->net->ns.inum);
2463                 nfsd4_end_grace(nn);             2290                 nfsd4_end_grace(nn);
2464         }                                        2291         }
2465 }                                                2292 }
2466                                                  2293 
2467 static void expire_client(struct nfs4_client     2294 static void expire_client(struct nfs4_client *clp)
2468 {                                                2295 {
2469         unhash_client(clp);                      2296         unhash_client(clp);
2470         nfsd4_client_record_remove(clp);         2297         nfsd4_client_record_remove(clp);
2471         __destroy_client(clp);                   2298         __destroy_client(clp);
2472 }                                                2299 }
2473                                                  2300 
2474 static void copy_verf(struct nfs4_client *tar    2301 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
2475 {                                                2302 {
2476         memcpy(target->cl_verifier.data, sour    2303         memcpy(target->cl_verifier.data, source->data,
2477                         sizeof(target->cl_ver    2304                         sizeof(target->cl_verifier.data));
2478 }                                                2305 }
2479                                                  2306 
2480 static void copy_clid(struct nfs4_client *tar    2307 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
2481 {                                                2308 {
2482         target->cl_clientid.cl_boot = source-    2309         target->cl_clientid.cl_boot = source->cl_clientid.cl_boot; 
2483         target->cl_clientid.cl_id = source->c    2310         target->cl_clientid.cl_id = source->cl_clientid.cl_id; 
2484 }                                                2311 }
2485                                                  2312 
2486 static int copy_cred(struct svc_cred *target,    2313 static int copy_cred(struct svc_cred *target, struct svc_cred *source)
2487 {                                                2314 {
2488         target->cr_principal = kstrdup(source    2315         target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL);
2489         target->cr_raw_principal = kstrdup(so    2316         target->cr_raw_principal = kstrdup(source->cr_raw_principal,
2490                                                  2317                                                                 GFP_KERNEL);
2491         target->cr_targ_princ = kstrdup(sourc    2318         target->cr_targ_princ = kstrdup(source->cr_targ_princ, GFP_KERNEL);
2492         if ((source->cr_principal && !target-    2319         if ((source->cr_principal && !target->cr_principal) ||
2493             (source->cr_raw_principal && !tar    2320             (source->cr_raw_principal && !target->cr_raw_principal) ||
2494             (source->cr_targ_princ && !target    2321             (source->cr_targ_princ && !target->cr_targ_princ))
2495                 return -ENOMEM;                  2322                 return -ENOMEM;
2496                                                  2323 
2497         target->cr_flavor = source->cr_flavor    2324         target->cr_flavor = source->cr_flavor;
2498         target->cr_uid = source->cr_uid;         2325         target->cr_uid = source->cr_uid;
2499         target->cr_gid = source->cr_gid;         2326         target->cr_gid = source->cr_gid;
2500         target->cr_group_info = source->cr_gr    2327         target->cr_group_info = source->cr_group_info;
2501         get_group_info(target->cr_group_info)    2328         get_group_info(target->cr_group_info);
2502         target->cr_gss_mech = source->cr_gss_    2329         target->cr_gss_mech = source->cr_gss_mech;
2503         if (source->cr_gss_mech)                 2330         if (source->cr_gss_mech)
2504                 gss_mech_get(source->cr_gss_m    2331                 gss_mech_get(source->cr_gss_mech);
2505         return 0;                                2332         return 0;
2506 }                                                2333 }
2507                                                  2334 
2508 static int                                       2335 static int
2509 compare_blob(const struct xdr_netobj *o1, con    2336 compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
2510 {                                                2337 {
2511         if (o1->len < o2->len)                   2338         if (o1->len < o2->len)
2512                 return -1;                       2339                 return -1;
2513         if (o1->len > o2->len)                   2340         if (o1->len > o2->len)
2514                 return 1;                        2341                 return 1;
2515         return memcmp(o1->data, o2->data, o1-    2342         return memcmp(o1->data, o2->data, o1->len);
2516 }                                                2343 }
2517                                                  2344 
2518 static int                                       2345 static int
2519 same_verf(nfs4_verifier *v1, nfs4_verifier *v    2346 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
2520 {                                                2347 {
2521         return 0 == memcmp(v1->data, v2->data    2348         return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
2522 }                                                2349 }
2523                                                  2350 
2524 static int                                       2351 static int
2525 same_clid(clientid_t *cl1, clientid_t *cl2)      2352 same_clid(clientid_t *cl1, clientid_t *cl2)
2526 {                                                2353 {
2527         return (cl1->cl_boot == cl2->cl_boot)    2354         return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
2528 }                                                2355 }
2529                                                  2356 
2530 static bool groups_equal(struct group_info *g    2357 static bool groups_equal(struct group_info *g1, struct group_info *g2)
2531 {                                                2358 {
2532         int i;                                   2359         int i;
2533                                                  2360 
2534         if (g1->ngroups != g2->ngroups)          2361         if (g1->ngroups != g2->ngroups)
2535                 return false;                    2362                 return false;
2536         for (i=0; i<g1->ngroups; i++)            2363         for (i=0; i<g1->ngroups; i++)
2537                 if (!gid_eq(g1->gid[i], g2->g    2364                 if (!gid_eq(g1->gid[i], g2->gid[i]))
2538                         return false;            2365                         return false;
2539         return true;                             2366         return true;
2540 }                                                2367 }
2541                                                  2368 
2542 /*                                               2369 /*
2543  * RFC 3530 language requires clid_inuse be r    2370  * RFC 3530 language requires clid_inuse be returned when the
2544  * "principal" associated with a requests dif    2371  * "principal" associated with a requests differs from that previously
2545  * used.  We use uid, gid's, and gss principa    2372  * used.  We use uid, gid's, and gss principal string as our best
2546  * approximation.  We also don't want to allo    2373  * approximation.  We also don't want to allow non-gss use of a client
2547  * established using gss: in theory cr_princi    2374  * established using gss: in theory cr_principal should catch that
2548  * change, but in practice cr_principal can b    2375  * change, but in practice cr_principal can be null even in the gss case
2549  * since gssd doesn't always pass down a prin    2376  * since gssd doesn't always pass down a principal string.
2550  */                                              2377  */
2551 static bool is_gss_cred(struct svc_cred *cr)     2378 static bool is_gss_cred(struct svc_cred *cr)
2552 {                                                2379 {
2553         /* Is cr_flavor one of the gss "pseud    2380         /* Is cr_flavor one of the gss "pseudoflavors"?: */
2554         return (cr->cr_flavor > RPC_AUTH_MAXF    2381         return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
2555 }                                                2382 }
2556                                                  2383 
2557                                                  2384 
2558 static bool                                      2385 static bool
2559 same_creds(struct svc_cred *cr1, struct svc_c    2386 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
2560 {                                                2387 {
2561         if ((is_gss_cred(cr1) != is_gss_cred(    2388         if ((is_gss_cred(cr1) != is_gss_cred(cr2))
2562                 || (!uid_eq(cr1->cr_uid, cr2-    2389                 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
2563                 || (!gid_eq(cr1->cr_gid, cr2-    2390                 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
2564                 || !groups_equal(cr1->cr_grou    2391                 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
2565                 return false;                    2392                 return false;
2566         /* XXX: check that cr_targ_princ fiel    2393         /* XXX: check that cr_targ_princ fields match ? */
2567         if (cr1->cr_principal == cr2->cr_prin    2394         if (cr1->cr_principal == cr2->cr_principal)
2568                 return true;                     2395                 return true;
2569         if (!cr1->cr_principal || !cr2->cr_pr    2396         if (!cr1->cr_principal || !cr2->cr_principal)
2570                 return false;                    2397                 return false;
2571         return 0 == strcmp(cr1->cr_principal,    2398         return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
2572 }                                                2399 }
2573                                                  2400 
2574 static bool svc_rqst_integrity_protected(stru    2401 static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
2575 {                                                2402 {
2576         struct svc_cred *cr = &rqstp->rq_cred    2403         struct svc_cred *cr = &rqstp->rq_cred;
2577         u32 service;                             2404         u32 service;
2578                                                  2405 
2579         if (!cr->cr_gss_mech)                    2406         if (!cr->cr_gss_mech)
2580                 return false;                    2407                 return false;
2581         service = gss_pseudoflavor_to_service    2408         service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
2582         return service == RPC_GSS_SVC_INTEGRI    2409         return service == RPC_GSS_SVC_INTEGRITY ||
2583                service == RPC_GSS_SVC_PRIVACY    2410                service == RPC_GSS_SVC_PRIVACY;
2584 }                                                2411 }
2585                                                  2412 
2586 bool nfsd4_mach_creds_match(struct nfs4_clien    2413 bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
2587 {                                                2414 {
2588         struct svc_cred *cr = &rqstp->rq_cred    2415         struct svc_cred *cr = &rqstp->rq_cred;
2589                                                  2416 
2590         if (!cl->cl_mach_cred)                   2417         if (!cl->cl_mach_cred)
2591                 return true;                     2418                 return true;
2592         if (cl->cl_cred.cr_gss_mech != cr->cr    2419         if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
2593                 return false;                    2420                 return false;
2594         if (!svc_rqst_integrity_protected(rqs    2421         if (!svc_rqst_integrity_protected(rqstp))
2595                 return false;                    2422                 return false;
2596         if (cl->cl_cred.cr_raw_principal)        2423         if (cl->cl_cred.cr_raw_principal)
2597                 return 0 == strcmp(cl->cl_cre    2424                 return 0 == strcmp(cl->cl_cred.cr_raw_principal,
2598                                                  2425                                                 cr->cr_raw_principal);
2599         if (!cr->cr_principal)                   2426         if (!cr->cr_principal)
2600                 return false;                    2427                 return false;
2601         return 0 == strcmp(cl->cl_cred.cr_pri    2428         return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
2602 }                                                2429 }
2603                                                  2430 
2604 static void gen_confirm(struct nfs4_client *c    2431 static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
2605 {                                                2432 {
2606         __be32 verf[2];                          2433         __be32 verf[2];
2607                                                  2434 
2608         /*                                       2435         /*
2609          * This is opaque to client, so no ne    2436          * This is opaque to client, so no need to byte-swap. Use
2610          * __force to keep sparse happy          2437          * __force to keep sparse happy
2611          */                                      2438          */
2612         verf[0] = (__force __be32)(u32)ktime_    2439         verf[0] = (__force __be32)(u32)ktime_get_real_seconds();
2613         verf[1] = (__force __be32)nn->clverif    2440         verf[1] = (__force __be32)nn->clverifier_counter++;
2614         memcpy(clp->cl_confirm.data, verf, si    2441         memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
2615 }                                                2442 }
2616                                                  2443 
2617 static void gen_clid(struct nfs4_client *clp,    2444 static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
2618 {                                                2445 {
2619         clp->cl_clientid.cl_boot = (u32)nn->b    2446         clp->cl_clientid.cl_boot = (u32)nn->boot_time;
2620         clp->cl_clientid.cl_id = nn->clientid    2447         clp->cl_clientid.cl_id = nn->clientid_counter++;
2621         gen_confirm(clp, nn);                    2448         gen_confirm(clp, nn);
2622 }                                                2449 }
2623                                                  2450 
2624 static struct nfs4_stid *                        2451 static struct nfs4_stid *
2625 find_stateid_locked(struct nfs4_client *cl, s    2452 find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
2626 {                                                2453 {
2627         struct nfs4_stid *ret;                   2454         struct nfs4_stid *ret;
2628                                                  2455 
2629         ret = idr_find(&cl->cl_stateids, t->s    2456         ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
2630         if (!ret || !ret->sc_type)               2457         if (!ret || !ret->sc_type)
2631                 return NULL;                     2458                 return NULL;
2632         return ret;                              2459         return ret;
2633 }                                                2460 }
2634                                                  2461 
2635 static struct nfs4_stid *                        2462 static struct nfs4_stid *
2636 find_stateid_by_type(struct nfs4_client *cl,  !! 2463 find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
2637                      unsigned short typemask, << 
2638 {                                                2464 {
2639         struct nfs4_stid *s;                     2465         struct nfs4_stid *s;
2640                                                  2466 
2641         spin_lock(&cl->cl_lock);                 2467         spin_lock(&cl->cl_lock);
2642         s = find_stateid_locked(cl, t);          2468         s = find_stateid_locked(cl, t);
2643         if (s != NULL) {                         2469         if (s != NULL) {
2644                 if ((s->sc_status & ~ok_state !! 2470                 if (typemask & s->sc_type)
2645                     (typemask & s->sc_type))  << 
2646                         refcount_inc(&s->sc_c    2471                         refcount_inc(&s->sc_count);
2647                 else                             2472                 else
2648                         s = NULL;                2473                         s = NULL;
2649         }                                        2474         }
2650         spin_unlock(&cl->cl_lock);               2475         spin_unlock(&cl->cl_lock);
2651         return s;                                2476         return s;
2652 }                                                2477 }
2653                                                  2478 
2654 static struct nfs4_client *get_nfsdfs_clp(str    2479 static struct nfs4_client *get_nfsdfs_clp(struct inode *inode)
2655 {                                                2480 {
2656         struct nfsdfs_client *nc;                2481         struct nfsdfs_client *nc;
2657         nc = get_nfsdfs_client(inode);           2482         nc = get_nfsdfs_client(inode);
2658         if (!nc)                                 2483         if (!nc)
2659                 return NULL;                     2484                 return NULL;
2660         return container_of(nc, struct nfs4_c    2485         return container_of(nc, struct nfs4_client, cl_nfsdfs);
2661 }                                                2486 }
2662                                                  2487 
2663 static void seq_quote_mem(struct seq_file *m,    2488 static void seq_quote_mem(struct seq_file *m, char *data, int len)
2664 {                                                2489 {
2665         seq_puts(m, "\"");                    !! 2490         seq_printf(m, "\"");
2666         seq_escape_mem(m, data, len, ESCAPE_H    2491         seq_escape_mem(m, data, len, ESCAPE_HEX | ESCAPE_NAP | ESCAPE_APPEND, "\"\\");
2667         seq_puts(m, "\"");                    !! 2492         seq_printf(m, "\"");
2668 }                                                2493 }
2669                                                  2494 
2670 static const char *cb_state2str(int state)       2495 static const char *cb_state2str(int state)
2671 {                                                2496 {
2672         switch (state) {                         2497         switch (state) {
2673         case NFSD4_CB_UP:                        2498         case NFSD4_CB_UP:
2674                 return "UP";                     2499                 return "UP";
2675         case NFSD4_CB_UNKNOWN:                   2500         case NFSD4_CB_UNKNOWN:
2676                 return "UNKNOWN";                2501                 return "UNKNOWN";
2677         case NFSD4_CB_DOWN:                      2502         case NFSD4_CB_DOWN:
2678                 return "DOWN";                   2503                 return "DOWN";
2679         case NFSD4_CB_FAULT:                     2504         case NFSD4_CB_FAULT:
2680                 return "FAULT";                  2505                 return "FAULT";
2681         }                                        2506         }
2682         return "UNDEFINED";                      2507         return "UNDEFINED";
2683 }                                                2508 }
2684                                                  2509 
2685 static int client_info_show(struct seq_file *    2510 static int client_info_show(struct seq_file *m, void *v)
2686 {                                                2511 {
2687         struct inode *inode = file_inode(m->f    2512         struct inode *inode = file_inode(m->file);
2688         struct nfs4_client *clp;                 2513         struct nfs4_client *clp;
2689         u64 clid;                                2514         u64 clid;
2690                                                  2515 
2691         clp = get_nfsdfs_clp(inode);             2516         clp = get_nfsdfs_clp(inode);
2692         if (!clp)                                2517         if (!clp)
2693                 return -ENXIO;                   2518                 return -ENXIO;
2694         memcpy(&clid, &clp->cl_clientid, size    2519         memcpy(&clid, &clp->cl_clientid, sizeof(clid));
2695         seq_printf(m, "clientid: 0x%llx\n", c    2520         seq_printf(m, "clientid: 0x%llx\n", clid);
2696         seq_printf(m, "address: \"%pISpc\"\n"    2521         seq_printf(m, "address: \"%pISpc\"\n", (struct sockaddr *)&clp->cl_addr);
2697                                                  2522 
2698         if (clp->cl_state == NFSD4_COURTESY)     2523         if (clp->cl_state == NFSD4_COURTESY)
2699                 seq_puts(m, "status: courtesy    2524                 seq_puts(m, "status: courtesy\n");
2700         else if (clp->cl_state == NFSD4_EXPIR    2525         else if (clp->cl_state == NFSD4_EXPIRABLE)
2701                 seq_puts(m, "status: expirabl    2526                 seq_puts(m, "status: expirable\n");
2702         else if (test_bit(NFSD4_CLIENT_CONFIR    2527         else if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
2703                 seq_puts(m, "status: confirme    2528                 seq_puts(m, "status: confirmed\n");
2704         else                                     2529         else
2705                 seq_puts(m, "status: unconfir    2530                 seq_puts(m, "status: unconfirmed\n");
2706         seq_printf(m, "seconds from last rene    2531         seq_printf(m, "seconds from last renew: %lld\n",
2707                 ktime_get_boottime_seconds()     2532                 ktime_get_boottime_seconds() - clp->cl_time);
2708         seq_puts(m, "name: ");                !! 2533         seq_printf(m, "name: ");
2709         seq_quote_mem(m, clp->cl_name.data, c    2534         seq_quote_mem(m, clp->cl_name.data, clp->cl_name.len);
2710         seq_printf(m, "\nminor version: %d\n"    2535         seq_printf(m, "\nminor version: %d\n", clp->cl_minorversion);
2711         if (clp->cl_nii_domain.data) {           2536         if (clp->cl_nii_domain.data) {
2712                 seq_puts(m, "Implementation d !! 2537                 seq_printf(m, "Implementation domain: ");
2713                 seq_quote_mem(m, clp->cl_nii_    2538                 seq_quote_mem(m, clp->cl_nii_domain.data,
2714                                         clp->    2539                                         clp->cl_nii_domain.len);
2715                 seq_puts(m, "\nImplementation !! 2540                 seq_printf(m, "\nImplementation name: ");
2716                 seq_quote_mem(m, clp->cl_nii_    2541                 seq_quote_mem(m, clp->cl_nii_name.data, clp->cl_nii_name.len);
2717                 seq_printf(m, "\nImplementati    2542                 seq_printf(m, "\nImplementation time: [%lld, %ld]\n",
2718                         clp->cl_nii_time.tv_s    2543                         clp->cl_nii_time.tv_sec, clp->cl_nii_time.tv_nsec);
2719         }                                        2544         }
2720         seq_printf(m, "callback state: %s\n",    2545         seq_printf(m, "callback state: %s\n", cb_state2str(clp->cl_cb_state));
2721         seq_printf(m, "callback address: \"%p !! 2546         seq_printf(m, "callback address: %pISpc\n", &clp->cl_cb_conn.cb_addr);
2722         seq_printf(m, "admin-revoked states:  << 
2723                    atomic_read(&clp->cl_admin << 
2724         drop_client(clp);                        2547         drop_client(clp);
2725                                                  2548 
2726         return 0;                                2549         return 0;
2727 }                                                2550 }
2728                                                  2551 
2729 DEFINE_SHOW_ATTRIBUTE(client_info);              2552 DEFINE_SHOW_ATTRIBUTE(client_info);
2730                                                  2553 
2731 static void *states_start(struct seq_file *s,    2554 static void *states_start(struct seq_file *s, loff_t *pos)
2732         __acquires(&clp->cl_lock)                2555         __acquires(&clp->cl_lock)
2733 {                                                2556 {
2734         struct nfs4_client *clp = s->private;    2557         struct nfs4_client *clp = s->private;
2735         unsigned long id = *pos;                 2558         unsigned long id = *pos;
2736         void *ret;                               2559         void *ret;
2737                                                  2560 
2738         spin_lock(&clp->cl_lock);                2561         spin_lock(&clp->cl_lock);
2739         ret = idr_get_next_ul(&clp->cl_statei    2562         ret = idr_get_next_ul(&clp->cl_stateids, &id);
2740         *pos = id;                               2563         *pos = id;
2741         return ret;                              2564         return ret;
2742 }                                                2565 }
2743                                                  2566 
2744 static void *states_next(struct seq_file *s,     2567 static void *states_next(struct seq_file *s, void *v, loff_t *pos)
2745 {                                                2568 {
2746         struct nfs4_client *clp = s->private;    2569         struct nfs4_client *clp = s->private;
2747         unsigned long id = *pos;                 2570         unsigned long id = *pos;
2748         void *ret;                               2571         void *ret;
2749                                                  2572 
2750         id = *pos;                               2573         id = *pos;
2751         id++;                                    2574         id++;
2752         ret = idr_get_next_ul(&clp->cl_statei    2575         ret = idr_get_next_ul(&clp->cl_stateids, &id);
2753         *pos = id;                               2576         *pos = id;
2754         return ret;                              2577         return ret;
2755 }                                                2578 }
2756                                                  2579 
2757 static void states_stop(struct seq_file *s, v    2580 static void states_stop(struct seq_file *s, void *v)
2758         __releases(&clp->cl_lock)                2581         __releases(&clp->cl_lock)
2759 {                                                2582 {
2760         struct nfs4_client *clp = s->private;    2583         struct nfs4_client *clp = s->private;
2761                                                  2584 
2762         spin_unlock(&clp->cl_lock);              2585         spin_unlock(&clp->cl_lock);
2763 }                                                2586 }
2764                                                  2587 
2765 static void nfs4_show_fname(struct seq_file *    2588 static void nfs4_show_fname(struct seq_file *s, struct nfsd_file *f)
2766 {                                                2589 {
2767          seq_printf(s, "filename: \"%pD2\"",     2590          seq_printf(s, "filename: \"%pD2\"", f->nf_file);
2768 }                                                2591 }
2769                                                  2592 
2770 static void nfs4_show_superblock(struct seq_f    2593 static void nfs4_show_superblock(struct seq_file *s, struct nfsd_file *f)
2771 {                                                2594 {
2772         struct inode *inode = file_inode(f->n    2595         struct inode *inode = file_inode(f->nf_file);
2773                                                  2596 
2774         seq_printf(s, "superblock: \"%02x:%02    2597         seq_printf(s, "superblock: \"%02x:%02x:%ld\"",
2775                                         MAJOR    2598                                         MAJOR(inode->i_sb->s_dev),
2776                                          MINO    2599                                          MINOR(inode->i_sb->s_dev),
2777                                          inod    2600                                          inode->i_ino);
2778 }                                                2601 }
2779                                                  2602 
2780 static void nfs4_show_owner(struct seq_file *    2603 static void nfs4_show_owner(struct seq_file *s, struct nfs4_stateowner *oo)
2781 {                                                2604 {
2782         seq_puts(s, "owner: ");               !! 2605         seq_printf(s, "owner: ");
2783         seq_quote_mem(s, oo->so_owner.data, o    2606         seq_quote_mem(s, oo->so_owner.data, oo->so_owner.len);
2784 }                                                2607 }
2785                                                  2608 
2786 static void nfs4_show_stateid(struct seq_file    2609 static void nfs4_show_stateid(struct seq_file *s, stateid_t *stid)
2787 {                                                2610 {
2788         seq_printf(s, "0x%.8x", stid->si_gene    2611         seq_printf(s, "0x%.8x", stid->si_generation);
2789         seq_printf(s, "%12phN", &stid->si_opa    2612         seq_printf(s, "%12phN", &stid->si_opaque);
2790 }                                                2613 }
2791                                                  2614 
2792 static int nfs4_show_open(struct seq_file *s,    2615 static int nfs4_show_open(struct seq_file *s, struct nfs4_stid *st)
2793 {                                                2616 {
2794         struct nfs4_ol_stateid *ols;             2617         struct nfs4_ol_stateid *ols;
2795         struct nfs4_file *nf;                    2618         struct nfs4_file *nf;
2796         struct nfsd_file *file;                  2619         struct nfsd_file *file;
2797         struct nfs4_stateowner *oo;              2620         struct nfs4_stateowner *oo;
2798         unsigned int access, deny;               2621         unsigned int access, deny;
2799                                                  2622 
                                                   >> 2623         if (st->sc_type != NFS4_OPEN_STID && st->sc_type != NFS4_LOCK_STID)
                                                   >> 2624                 return 0; /* XXX: or SEQ_SKIP? */
2800         ols = openlockstateid(st);               2625         ols = openlockstateid(st);
2801         oo = ols->st_stateowner;                 2626         oo = ols->st_stateowner;
2802         nf = st->sc_file;                        2627         nf = st->sc_file;
2803                                                  2628 
2804         seq_puts(s, "- ");                    !! 2629         spin_lock(&nf->fi_lock);
                                                   >> 2630         file = find_any_file_locked(nf);
                                                   >> 2631         if (!file)
                                                   >> 2632                 goto out;
                                                   >> 2633 
                                                   >> 2634         seq_printf(s, "- ");
2805         nfs4_show_stateid(s, &st->sc_stateid)    2635         nfs4_show_stateid(s, &st->sc_stateid);
2806         seq_puts(s, ": { type: open, ");      !! 2636         seq_printf(s, ": { type: open, ");
2807                                                  2637 
2808         access = bmap_to_share_mode(ols->st_a    2638         access = bmap_to_share_mode(ols->st_access_bmap);
2809         deny   = bmap_to_share_mode(ols->st_d    2639         deny   = bmap_to_share_mode(ols->st_deny_bmap);
2810                                                  2640 
2811         seq_printf(s, "access: %s%s, ",          2641         seq_printf(s, "access: %s%s, ",
2812                 access & NFS4_SHARE_ACCESS_RE    2642                 access & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2813                 access & NFS4_SHARE_ACCESS_WR    2643                 access & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2814         seq_printf(s, "deny: %s%s, ",            2644         seq_printf(s, "deny: %s%s, ",
2815                 deny & NFS4_SHARE_ACCESS_READ    2645                 deny & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2816                 deny & NFS4_SHARE_ACCESS_WRIT    2646                 deny & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2817                                                  2647 
2818         if (nf) {                             !! 2648         nfs4_show_superblock(s, file);
2819                 spin_lock(&nf->fi_lock);      !! 2649         seq_printf(s, ", ");
2820                 file = find_any_file_locked(n !! 2650         nfs4_show_fname(s, file);
2821                 if (file) {                   !! 2651         seq_printf(s, ", ");
2822                         nfs4_show_superblock( << 
2823                         seq_puts(s, ", ");    << 
2824                         nfs4_show_fname(s, fi << 
2825                         seq_puts(s, ", ");    << 
2826                 }                             << 
2827                 spin_unlock(&nf->fi_lock);    << 
2828         } else                                << 
2829                 seq_puts(s, "closed, ");      << 
2830         nfs4_show_owner(s, oo);                  2652         nfs4_show_owner(s, oo);
2831         if (st->sc_status & SC_STATUS_ADMIN_R !! 2653         seq_printf(s, " }\n");
2832                 seq_puts(s, ", admin-revoked" !! 2654 out:
2833         seq_puts(s, " }\n");                  !! 2655         spin_unlock(&nf->fi_lock);
2834         return 0;                                2656         return 0;
2835 }                                                2657 }
2836                                                  2658 
2837 static int nfs4_show_lock(struct seq_file *s,    2659 static int nfs4_show_lock(struct seq_file *s, struct nfs4_stid *st)
2838 {                                                2660 {
2839         struct nfs4_ol_stateid *ols;             2661         struct nfs4_ol_stateid *ols;
2840         struct nfs4_file *nf;                    2662         struct nfs4_file *nf;
2841         struct nfsd_file *file;                  2663         struct nfsd_file *file;
2842         struct nfs4_stateowner *oo;              2664         struct nfs4_stateowner *oo;
2843                                                  2665 
2844         ols = openlockstateid(st);               2666         ols = openlockstateid(st);
2845         oo = ols->st_stateowner;                 2667         oo = ols->st_stateowner;
2846         nf = st->sc_file;                        2668         nf = st->sc_file;
                                                   >> 2669         spin_lock(&nf->fi_lock);
                                                   >> 2670         file = find_any_file_locked(nf);
                                                   >> 2671         if (!file)
                                                   >> 2672                 goto out;
2847                                                  2673 
2848         seq_puts(s, "- ");                    !! 2674         seq_printf(s, "- ");
2849         nfs4_show_stateid(s, &st->sc_stateid)    2675         nfs4_show_stateid(s, &st->sc_stateid);
2850         seq_puts(s, ": { type: lock, ");      !! 2676         seq_printf(s, ": { type: lock, ");
2851                                                  2677 
2852         spin_lock(&nf->fi_lock);              !! 2678         /*
2853         file = find_any_file_locked(nf);      !! 2679          * Note: a lock stateid isn't really the same thing as a lock,
2854         if (file) {                           !! 2680          * it's the locking state held by one owner on a file, and there
2855                 /*                            !! 2681          * may be multiple (or no) lock ranges associated with it.
2856                  * Note: a lock stateid isn't !! 2682          * (Same for the matter is true of open stateids.)
2857                  * it's the locking state hel !! 2683          */
2858                  * may be multiple (or no) lo << 
2859                  * (Same for the matter is tr << 
2860                  */                           << 
2861                                                  2684 
2862                 nfs4_show_superblock(s, file) !! 2685         nfs4_show_superblock(s, file);
2863                 /* XXX: open stateid? */      !! 2686         /* XXX: open stateid? */
2864                 seq_puts(s, ", ");            !! 2687         seq_printf(s, ", ");
2865                 nfs4_show_fname(s, file);     !! 2688         nfs4_show_fname(s, file);
2866                 seq_puts(s, ", ");            !! 2689         seq_printf(s, ", ");
2867         }                                     << 
2868         nfs4_show_owner(s, oo);                  2690         nfs4_show_owner(s, oo);
2869         if (st->sc_status & SC_STATUS_ADMIN_R !! 2691         seq_printf(s, " }\n");
2870                 seq_puts(s, ", admin-revoked" !! 2692 out:
2871         seq_puts(s, " }\n");                  << 
2872         spin_unlock(&nf->fi_lock);               2693         spin_unlock(&nf->fi_lock);
2873         return 0;                                2694         return 0;
2874 }                                                2695 }
2875                                                  2696 
2876 static int nfs4_show_deleg(struct seq_file *s    2697 static int nfs4_show_deleg(struct seq_file *s, struct nfs4_stid *st)
2877 {                                                2698 {
2878         struct nfs4_delegation *ds;              2699         struct nfs4_delegation *ds;
2879         struct nfs4_file *nf;                    2700         struct nfs4_file *nf;
2880         struct nfsd_file *file;                  2701         struct nfsd_file *file;
2881                                                  2702 
2882         ds = delegstateid(st);                   2703         ds = delegstateid(st);
2883         nf = st->sc_file;                        2704         nf = st->sc_file;
                                                   >> 2705         spin_lock(&nf->fi_lock);
                                                   >> 2706         file = nf->fi_deleg_file;
                                                   >> 2707         if (!file)
                                                   >> 2708                 goto out;
2884                                                  2709 
2885         seq_puts(s, "- ");                    !! 2710         seq_printf(s, "- ");
2886         nfs4_show_stateid(s, &st->sc_stateid)    2711         nfs4_show_stateid(s, &st->sc_stateid);
2887         seq_puts(s, ": { type: deleg, ");     !! 2712         seq_printf(s, ": { type: deleg, ");
2888                                                  2713 
2889         seq_printf(s, "access: %s",           !! 2714         /* Kinda dead code as long as we only support read delegs: */
2890                    ds->dl_type == NFS4_OPEN_D !! 2715         seq_printf(s, "access: %s, ",
                                                   >> 2716                 ds->dl_type == NFS4_OPEN_DELEGATE_READ ? "r" : "w");
2891                                                  2717 
2892         /* XXX: lease time, whether it's bein    2718         /* XXX: lease time, whether it's being recalled. */
2893                                                  2719 
2894         spin_lock(&nf->fi_lock);              !! 2720         nfs4_show_superblock(s, file);
2895         file = nf->fi_deleg_file;             !! 2721         seq_printf(s, ", ");
2896         if (file) {                           !! 2722         nfs4_show_fname(s, file);
2897                 seq_puts(s, ", ");            !! 2723         seq_printf(s, " }\n");
2898                 nfs4_show_superblock(s, file) !! 2724 out:
2899                 seq_puts(s, ", ");            << 
2900                 nfs4_show_fname(s, file);     << 
2901         }                                     << 
2902         spin_unlock(&nf->fi_lock);               2725         spin_unlock(&nf->fi_lock);
2903         if (st->sc_status & SC_STATUS_ADMIN_R << 
2904                 seq_puts(s, ", admin-revoked" << 
2905         seq_puts(s, " }\n");                  << 
2906         return 0;                                2726         return 0;
2907 }                                                2727 }
2908                                                  2728 
2909 static int nfs4_show_layout(struct seq_file *    2729 static int nfs4_show_layout(struct seq_file *s, struct nfs4_stid *st)
2910 {                                                2730 {
2911         struct nfs4_layout_stateid *ls;          2731         struct nfs4_layout_stateid *ls;
2912         struct nfsd_file *file;                  2732         struct nfsd_file *file;
2913                                                  2733 
2914         ls = container_of(st, struct nfs4_lay    2734         ls = container_of(st, struct nfs4_layout_stateid, ls_stid);
                                                   >> 2735         file = ls->ls_file;
2915                                                  2736 
2916         seq_puts(s, "- ");                    !! 2737         seq_printf(s, "- ");
2917         nfs4_show_stateid(s, &st->sc_stateid)    2738         nfs4_show_stateid(s, &st->sc_stateid);
2918         seq_puts(s, ": { type: layout");      !! 2739         seq_printf(s, ": { type: layout, ");
2919                                                  2740 
2920         /* XXX: What else would be useful? */    2741         /* XXX: What else would be useful? */
2921                                                  2742 
2922         spin_lock(&ls->ls_stid.sc_file->fi_lo !! 2743         nfs4_show_superblock(s, file);
2923         file = ls->ls_file;                   !! 2744         seq_printf(s, ", ");
2924         if (file) {                           !! 2745         nfs4_show_fname(s, file);
2925                 seq_puts(s, ", ");            !! 2746         seq_printf(s, " }\n");
2926                 nfs4_show_superblock(s, file) << 
2927                 seq_puts(s, ", ");            << 
2928                 nfs4_show_fname(s, file);     << 
2929         }                                     << 
2930         spin_unlock(&ls->ls_stid.sc_file->fi_ << 
2931         if (st->sc_status & SC_STATUS_ADMIN_R << 
2932                 seq_puts(s, ", admin-revoked" << 
2933         seq_puts(s, " }\n");                  << 
2934                                                  2747 
2935         return 0;                                2748         return 0;
2936 }                                                2749 }
2937                                                  2750 
2938 static int states_show(struct seq_file *s, vo    2751 static int states_show(struct seq_file *s, void *v)
2939 {                                                2752 {
2940         struct nfs4_stid *st = v;                2753         struct nfs4_stid *st = v;
2941                                                  2754 
2942         switch (st->sc_type) {                   2755         switch (st->sc_type) {
2943         case SC_TYPE_OPEN:                    !! 2756         case NFS4_OPEN_STID:
2944                 return nfs4_show_open(s, st);    2757                 return nfs4_show_open(s, st);
2945         case SC_TYPE_LOCK:                    !! 2758         case NFS4_LOCK_STID:
2946                 return nfs4_show_lock(s, st);    2759                 return nfs4_show_lock(s, st);
2947         case SC_TYPE_DELEG:                   !! 2760         case NFS4_DELEG_STID:
2948                 return nfs4_show_deleg(s, st)    2761                 return nfs4_show_deleg(s, st);
2949         case SC_TYPE_LAYOUT:                  !! 2762         case NFS4_LAYOUT_STID:
2950                 return nfs4_show_layout(s, st    2763                 return nfs4_show_layout(s, st);
2951         default:                                 2764         default:
2952                 return 0; /* XXX: or SEQ_SKIP    2765                 return 0; /* XXX: or SEQ_SKIP? */
2953         }                                        2766         }
2954         /* XXX: copy stateids? */                2767         /* XXX: copy stateids? */
2955 }                                                2768 }
2956                                                  2769 
2957 static struct seq_operations states_seq_ops =    2770 static struct seq_operations states_seq_ops = {
2958         .start = states_start,                   2771         .start = states_start,
2959         .next = states_next,                     2772         .next = states_next,
2960         .stop = states_stop,                     2773         .stop = states_stop,
2961         .show = states_show                      2774         .show = states_show
2962 };                                               2775 };
2963                                                  2776 
2964 static int client_states_open(struct inode *i    2777 static int client_states_open(struct inode *inode, struct file *file)
2965 {                                                2778 {
2966         struct seq_file *s;                      2779         struct seq_file *s;
2967         struct nfs4_client *clp;                 2780         struct nfs4_client *clp;
2968         int ret;                                 2781         int ret;
2969                                                  2782 
2970         clp = get_nfsdfs_clp(inode);             2783         clp = get_nfsdfs_clp(inode);
2971         if (!clp)                                2784         if (!clp)
2972                 return -ENXIO;                   2785                 return -ENXIO;
2973                                                  2786 
2974         ret = seq_open(file, &states_seq_ops)    2787         ret = seq_open(file, &states_seq_ops);
2975         if (ret)                                 2788         if (ret)
2976                 return ret;                      2789                 return ret;
2977         s = file->private_data;                  2790         s = file->private_data;
2978         s->private = clp;                        2791         s->private = clp;
2979         return 0;                                2792         return 0;
2980 }                                                2793 }
2981                                                  2794 
2982 static int client_opens_release(struct inode     2795 static int client_opens_release(struct inode *inode, struct file *file)
2983 {                                                2796 {
2984         struct seq_file *m = file->private_da    2797         struct seq_file *m = file->private_data;
2985         struct nfs4_client *clp = m->private;    2798         struct nfs4_client *clp = m->private;
2986                                                  2799 
2987         /* XXX: alternatively, we could get/d    2800         /* XXX: alternatively, we could get/drop in seq start/stop */
2988         drop_client(clp);                        2801         drop_client(clp);
2989         return seq_release(inode, file);         2802         return seq_release(inode, file);
2990 }                                                2803 }
2991                                                  2804 
2992 static const struct file_operations client_st    2805 static const struct file_operations client_states_fops = {
2993         .open           = client_states_open,    2806         .open           = client_states_open,
2994         .read           = seq_read,              2807         .read           = seq_read,
2995         .llseek         = seq_lseek,             2808         .llseek         = seq_lseek,
2996         .release        = client_opens_releas    2809         .release        = client_opens_release,
2997 };                                               2810 };
2998                                                  2811 
2999 /*                                               2812 /*
3000  * Normally we refuse to destroy clients that    2813  * Normally we refuse to destroy clients that are in use, but here the
3001  * administrator is telling us to just do it.    2814  * administrator is telling us to just do it.  We also want to wait
3002  * so the caller has a guarantee that the cli    2815  * so the caller has a guarantee that the client's locks are gone by
3003  * the time the write returns:                   2816  * the time the write returns:
3004  */                                              2817  */
3005 static void force_expire_client(struct nfs4_c    2818 static void force_expire_client(struct nfs4_client *clp)
3006 {                                                2819 {
3007         struct nfsd_net *nn = net_generic(clp    2820         struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
3008         bool already_expired;                    2821         bool already_expired;
3009                                                  2822 
3010         trace_nfsd_clid_admin_expired(&clp->c    2823         trace_nfsd_clid_admin_expired(&clp->cl_clientid);
3011                                                  2824 
3012         spin_lock(&nn->client_lock);             2825         spin_lock(&nn->client_lock);
3013         clp->cl_time = 0;                        2826         clp->cl_time = 0;
3014         spin_unlock(&nn->client_lock);           2827         spin_unlock(&nn->client_lock);
3015                                                  2828 
3016         wait_event(expiry_wq, atomic_read(&cl    2829         wait_event(expiry_wq, atomic_read(&clp->cl_rpc_users) == 0);
3017         spin_lock(&nn->client_lock);             2830         spin_lock(&nn->client_lock);
3018         already_expired = list_empty(&clp->cl    2831         already_expired = list_empty(&clp->cl_lru);
3019         if (!already_expired)                    2832         if (!already_expired)
3020                 unhash_client_locked(clp);       2833                 unhash_client_locked(clp);
3021         spin_unlock(&nn->client_lock);           2834         spin_unlock(&nn->client_lock);
3022                                                  2835 
3023         if (!already_expired)                    2836         if (!already_expired)
3024                 expire_client(clp);              2837                 expire_client(clp);
3025         else                                     2838         else
3026                 wait_event(expiry_wq, clp->cl    2839                 wait_event(expiry_wq, clp->cl_nfsd_dentry == NULL);
3027 }                                                2840 }
3028                                                  2841 
3029 static ssize_t client_ctl_write(struct file *    2842 static ssize_t client_ctl_write(struct file *file, const char __user *buf,
3030                                    size_t siz    2843                                    size_t size, loff_t *pos)
3031 {                                                2844 {
3032         char *data;                              2845         char *data;
3033         struct nfs4_client *clp;                 2846         struct nfs4_client *clp;
3034                                                  2847 
3035         data = simple_transaction_get(file, b    2848         data = simple_transaction_get(file, buf, size);
3036         if (IS_ERR(data))                        2849         if (IS_ERR(data))
3037                 return PTR_ERR(data);            2850                 return PTR_ERR(data);
3038         if (size != 7 || 0 != memcmp(data, "e    2851         if (size != 7 || 0 != memcmp(data, "expire\n", 7))
3039                 return -EINVAL;                  2852                 return -EINVAL;
3040         clp = get_nfsdfs_clp(file_inode(file)    2853         clp = get_nfsdfs_clp(file_inode(file));
3041         if (!clp)                                2854         if (!clp)
3042                 return -ENXIO;                   2855                 return -ENXIO;
3043         force_expire_client(clp);                2856         force_expire_client(clp);
3044         drop_client(clp);                        2857         drop_client(clp);
3045         return 7;                                2858         return 7;
3046 }                                                2859 }
3047                                                  2860 
3048 static const struct file_operations client_ct    2861 static const struct file_operations client_ctl_fops = {
3049         .write          = client_ctl_write,      2862         .write          = client_ctl_write,
3050         .release        = simple_transaction_    2863         .release        = simple_transaction_release,
3051 };                                               2864 };
3052                                                  2865 
3053 static const struct tree_descr client_files[]    2866 static const struct tree_descr client_files[] = {
3054         [0] = {"info", &client_info_fops, S_I    2867         [0] = {"info", &client_info_fops, S_IRUSR},
3055         [1] = {"states", &client_states_fops,    2868         [1] = {"states", &client_states_fops, S_IRUSR},
3056         [2] = {"ctl", &client_ctl_fops, S_IWU    2869         [2] = {"ctl", &client_ctl_fops, S_IWUSR},
3057         [3] = {""},                              2870         [3] = {""},
3058 };                                               2871 };
3059                                                  2872 
3060 static int                                       2873 static int
3061 nfsd4_cb_recall_any_done(struct nfsd4_callbac    2874 nfsd4_cb_recall_any_done(struct nfsd4_callback *cb,
3062                                 struct rpc_ta    2875                                 struct rpc_task *task)
3063 {                                                2876 {
3064         trace_nfsd_cb_recall_any_done(cb, tas    2877         trace_nfsd_cb_recall_any_done(cb, task);
3065         switch (task->tk_status) {               2878         switch (task->tk_status) {
3066         case -NFS4ERR_DELAY:                     2879         case -NFS4ERR_DELAY:
3067                 rpc_delay(task, 2 * HZ);         2880                 rpc_delay(task, 2 * HZ);
3068                 return 0;                        2881                 return 0;
3069         default:                                 2882         default:
3070                 return 1;                        2883                 return 1;
3071         }                                        2884         }
3072 }                                                2885 }
3073                                                  2886 
3074 static void                                      2887 static void
3075 nfsd4_cb_recall_any_release(struct nfsd4_call    2888 nfsd4_cb_recall_any_release(struct nfsd4_callback *cb)
3076 {                                                2889 {
3077         struct nfs4_client *clp = cb->cb_clp;    2890         struct nfs4_client *clp = cb->cb_clp;
3078                                                  2891 
3079         clear_bit(NFSD4_CLIENT_CB_RECALL_ANY,    2892         clear_bit(NFSD4_CLIENT_CB_RECALL_ANY, &clp->cl_flags);
3080         drop_client(clp);                        2893         drop_client(clp);
3081 }                                                2894 }
3082                                                  2895 
3083 static int                                    << 
3084 nfsd4_cb_getattr_done(struct nfsd4_callback * << 
3085 {                                             << 
3086         struct nfs4_cb_fattr *ncf =           << 
3087                         container_of(cb, stru << 
3088         struct nfs4_delegation *dp =          << 
3089                         container_of(ncf, str << 
3090                                               << 
3091         trace_nfsd_cb_getattr_done(&dp->dl_st << 
3092         ncf->ncf_cb_status = task->tk_status; << 
3093         switch (task->tk_status) {            << 
3094         case -NFS4ERR_DELAY:                  << 
3095                 rpc_delay(task, 2 * HZ);      << 
3096                 return 0;                     << 
3097         default:                              << 
3098                 return 1;                     << 
3099         }                                     << 
3100 }                                             << 
3101                                               << 
3102 static void                                   << 
3103 nfsd4_cb_getattr_release(struct nfsd4_callbac << 
3104 {                                             << 
3105         struct nfs4_cb_fattr *ncf =           << 
3106                         container_of(cb, stru << 
3107         struct nfs4_delegation *dp =          << 
3108                         container_of(ncf, str << 
3109                                               << 
3110         clear_and_wake_up_bit(CB_GETATTR_BUSY << 
3111         nfs4_put_stid(&dp->dl_stid);          << 
3112 }                                             << 
3113                                               << 
3114 static const struct nfsd4_callback_ops nfsd4_    2896 static const struct nfsd4_callback_ops nfsd4_cb_recall_any_ops = {
3115         .done           = nfsd4_cb_recall_any    2897         .done           = nfsd4_cb_recall_any_done,
3116         .release        = nfsd4_cb_recall_any    2898         .release        = nfsd4_cb_recall_any_release,
3117         .opcode         = OP_CB_RECALL_ANY,   << 
3118 };                                               2899 };
3119                                                  2900 
3120 static const struct nfsd4_callback_ops nfsd4_ << 
3121         .done           = nfsd4_cb_getattr_do << 
3122         .release        = nfsd4_cb_getattr_re << 
3123         .opcode         = OP_CB_GETATTR,      << 
3124 };                                            << 
3125                                               << 
3126 static void nfs4_cb_getattr(struct nfs4_cb_fa << 
3127 {                                             << 
3128         struct nfs4_delegation *dp =          << 
3129                         container_of(ncf, str << 
3130                                               << 
3131         if (test_and_set_bit(CB_GETATTR_BUSY, << 
3132                 return;                       << 
3133         /* set to proper status when nfsd4_cb << 
3134         ncf->ncf_cb_status = NFS4ERR_IO;      << 
3135                                               << 
3136         refcount_inc(&dp->dl_stid.sc_count);  << 
3137         nfsd4_run_cb(&ncf->ncf_getattr);      << 
3138 }                                             << 
3139                                               << 
3140 static struct nfs4_client *create_client(stru    2901 static struct nfs4_client *create_client(struct xdr_netobj name,
3141                 struct svc_rqst *rqstp, nfs4_    2902                 struct svc_rqst *rqstp, nfs4_verifier *verf)
3142 {                                                2903 {
3143         struct nfs4_client *clp;                 2904         struct nfs4_client *clp;
3144         struct sockaddr *sa = svc_addr(rqstp)    2905         struct sockaddr *sa = svc_addr(rqstp);
3145         int ret;                                 2906         int ret;
3146         struct net *net = SVC_NET(rqstp);        2907         struct net *net = SVC_NET(rqstp);
3147         struct nfsd_net *nn = net_generic(net    2908         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3148         struct dentry *dentries[ARRAY_SIZE(cl    2909         struct dentry *dentries[ARRAY_SIZE(client_files)];
3149                                                  2910 
3150         clp = alloc_client(name, nn);            2911         clp = alloc_client(name, nn);
3151         if (clp == NULL)                         2912         if (clp == NULL)
3152                 return NULL;                     2913                 return NULL;
3153                                                  2914 
3154         ret = copy_cred(&clp->cl_cred, &rqstp    2915         ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
3155         if (ret) {                               2916         if (ret) {
3156                 free_client(clp);                2917                 free_client(clp);
3157                 return NULL;                     2918                 return NULL;
3158         }                                        2919         }
3159         gen_clid(clp, nn);                       2920         gen_clid(clp, nn);
3160         kref_init(&clp->cl_nfsdfs.cl_ref);       2921         kref_init(&clp->cl_nfsdfs.cl_ref);
3161         nfsd4_init_cb(&clp->cl_cb_null, clp,     2922         nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
3162         clp->cl_time = ktime_get_boottime_sec    2923         clp->cl_time = ktime_get_boottime_seconds();
3163         clear_bit(0, &clp->cl_cb_slot_busy);     2924         clear_bit(0, &clp->cl_cb_slot_busy);
3164         copy_verf(clp, verf);                    2925         copy_verf(clp, verf);
3165         memcpy(&clp->cl_addr, sa, sizeof(stru    2926         memcpy(&clp->cl_addr, sa, sizeof(struct sockaddr_storage));
3166         clp->cl_cb_session = NULL;               2927         clp->cl_cb_session = NULL;
3167         clp->net = net;                          2928         clp->net = net;
3168         clp->cl_nfsd_dentry = nfsd_client_mkd    2929         clp->cl_nfsd_dentry = nfsd_client_mkdir(
3169                 nn, &clp->cl_nfsdfs,             2930                 nn, &clp->cl_nfsdfs,
3170                 clp->cl_clientid.cl_id - nn->    2931                 clp->cl_clientid.cl_id - nn->clientid_base,
3171                 client_files, dentries);         2932                 client_files, dentries);
3172         clp->cl_nfsd_info_dentry = dentries[0    2933         clp->cl_nfsd_info_dentry = dentries[0];
3173         if (!clp->cl_nfsd_dentry) {              2934         if (!clp->cl_nfsd_dentry) {
3174                 free_client(clp);                2935                 free_client(clp);
3175                 return NULL;                     2936                 return NULL;
3176         }                                        2937         }
3177         clp->cl_ra = kzalloc(sizeof(*clp->cl_    2938         clp->cl_ra = kzalloc(sizeof(*clp->cl_ra), GFP_KERNEL);
3178         if (!clp->cl_ra) {                       2939         if (!clp->cl_ra) {
3179                 free_client(clp);                2940                 free_client(clp);
3180                 return NULL;                     2941                 return NULL;
3181         }                                        2942         }
3182         clp->cl_ra_time = 0;                     2943         clp->cl_ra_time = 0;
3183         nfsd4_init_cb(&clp->cl_ra->ra_cb, clp    2944         nfsd4_init_cb(&clp->cl_ra->ra_cb, clp, &nfsd4_cb_recall_any_ops,
3184                         NFSPROC4_CLNT_CB_RECA    2945                         NFSPROC4_CLNT_CB_RECALL_ANY);
3185         return clp;                              2946         return clp;
3186 }                                                2947 }
3187                                                  2948 
3188 static void                                      2949 static void
3189 add_clp_to_name_tree(struct nfs4_client *new_    2950 add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
3190 {                                                2951 {
3191         struct rb_node **new = &(root->rb_nod    2952         struct rb_node **new = &(root->rb_node), *parent = NULL;
3192         struct nfs4_client *clp;                 2953         struct nfs4_client *clp;
3193                                                  2954 
3194         while (*new) {                           2955         while (*new) {
3195                 clp = rb_entry(*new, struct n    2956                 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
3196                 parent = *new;                   2957                 parent = *new;
3197                                                  2958 
3198                 if (compare_blob(&clp->cl_nam    2959                 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
3199                         new = &((*new)->rb_le    2960                         new = &((*new)->rb_left);
3200                 else                             2961                 else
3201                         new = &((*new)->rb_ri    2962                         new = &((*new)->rb_right);
3202         }                                        2963         }
3203                                                  2964 
3204         rb_link_node(&new_clp->cl_namenode, p    2965         rb_link_node(&new_clp->cl_namenode, parent, new);
3205         rb_insert_color(&new_clp->cl_namenode    2966         rb_insert_color(&new_clp->cl_namenode, root);
3206 }                                                2967 }
3207                                                  2968 
3208 static struct nfs4_client *                      2969 static struct nfs4_client *
3209 find_clp_in_name_tree(struct xdr_netobj *name    2970 find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
3210 {                                                2971 {
3211         int cmp;                                 2972         int cmp;
3212         struct rb_node *node = root->rb_node;    2973         struct rb_node *node = root->rb_node;
3213         struct nfs4_client *clp;                 2974         struct nfs4_client *clp;
3214                                                  2975 
3215         while (node) {                           2976         while (node) {
3216                 clp = rb_entry(node, struct n    2977                 clp = rb_entry(node, struct nfs4_client, cl_namenode);
3217                 cmp = compare_blob(&clp->cl_n    2978                 cmp = compare_blob(&clp->cl_name, name);
3218                 if (cmp > 0)                     2979                 if (cmp > 0)
3219                         node = node->rb_left;    2980                         node = node->rb_left;
3220                 else if (cmp < 0)                2981                 else if (cmp < 0)
3221                         node = node->rb_right    2982                         node = node->rb_right;
3222                 else                             2983                 else
3223                         return clp;              2984                         return clp;
3224         }                                        2985         }
3225         return NULL;                             2986         return NULL;
3226 }                                                2987 }
3227                                                  2988 
3228 static void                                      2989 static void
3229 add_to_unconfirmed(struct nfs4_client *clp)      2990 add_to_unconfirmed(struct nfs4_client *clp)
3230 {                                                2991 {
3231         unsigned int idhashval;                  2992         unsigned int idhashval;
3232         struct nfsd_net *nn = net_generic(clp    2993         struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
3233                                                  2994 
3234         lockdep_assert_held(&nn->client_lock)    2995         lockdep_assert_held(&nn->client_lock);
3235                                                  2996 
3236         clear_bit(NFSD4_CLIENT_CONFIRMED, &cl    2997         clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
3237         add_clp_to_name_tree(clp, &nn->unconf    2998         add_clp_to_name_tree(clp, &nn->unconf_name_tree);
3238         idhashval = clientid_hashval(clp->cl_    2999         idhashval = clientid_hashval(clp->cl_clientid.cl_id);
3239         list_add(&clp->cl_idhash, &nn->unconf    3000         list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
3240         renew_client_locked(clp);                3001         renew_client_locked(clp);
3241 }                                                3002 }
3242                                                  3003 
3243 static void                                      3004 static void
3244 move_to_confirmed(struct nfs4_client *clp)       3005 move_to_confirmed(struct nfs4_client *clp)
3245 {                                                3006 {
3246         unsigned int idhashval = clientid_has    3007         unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
3247         struct nfsd_net *nn = net_generic(clp    3008         struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
3248                                                  3009 
3249         lockdep_assert_held(&nn->client_lock)    3010         lockdep_assert_held(&nn->client_lock);
3250                                                  3011 
3251         list_move(&clp->cl_idhash, &nn->conf_    3012         list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
3252         rb_erase(&clp->cl_namenode, &nn->unco    3013         rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
3253         add_clp_to_name_tree(clp, &nn->conf_n    3014         add_clp_to_name_tree(clp, &nn->conf_name_tree);
3254         set_bit(NFSD4_CLIENT_CONFIRMED, &clp-    3015         set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
3255         trace_nfsd_clid_confirmed(&clp->cl_cl    3016         trace_nfsd_clid_confirmed(&clp->cl_clientid);
3256         renew_client_locked(clp);                3017         renew_client_locked(clp);
3257 }                                                3018 }
3258                                                  3019 
3259 static struct nfs4_client *                      3020 static struct nfs4_client *
3260 find_client_in_id_table(struct list_head *tbl    3021 find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
3261 {                                                3022 {
3262         struct nfs4_client *clp;                 3023         struct nfs4_client *clp;
3263         unsigned int idhashval = clientid_has    3024         unsigned int idhashval = clientid_hashval(clid->cl_id);
3264                                                  3025 
3265         list_for_each_entry(clp, &tbl[idhashv    3026         list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
3266                 if (same_clid(&clp->cl_client    3027                 if (same_clid(&clp->cl_clientid, clid)) {
3267                         if ((bool)clp->cl_min    3028                         if ((bool)clp->cl_minorversion != sessions)
3268                                 return NULL;     3029                                 return NULL;
3269                         renew_client_locked(c    3030                         renew_client_locked(clp);
3270                         return clp;              3031                         return clp;
3271                 }                                3032                 }
3272         }                                        3033         }
3273         return NULL;                             3034         return NULL;
3274 }                                                3035 }
3275                                                  3036 
3276 static struct nfs4_client *                      3037 static struct nfs4_client *
3277 find_confirmed_client(clientid_t *clid, bool     3038 find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
3278 {                                                3039 {
3279         struct list_head *tbl = nn->conf_id_h    3040         struct list_head *tbl = nn->conf_id_hashtbl;
3280                                                  3041 
3281         lockdep_assert_held(&nn->client_lock)    3042         lockdep_assert_held(&nn->client_lock);
3282         return find_client_in_id_table(tbl, c    3043         return find_client_in_id_table(tbl, clid, sessions);
3283 }                                                3044 }
3284                                                  3045 
3285 static struct nfs4_client *                      3046 static struct nfs4_client *
3286 find_unconfirmed_client(clientid_t *clid, boo    3047 find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
3287 {                                                3048 {
3288         struct list_head *tbl = nn->unconf_id    3049         struct list_head *tbl = nn->unconf_id_hashtbl;
3289                                                  3050 
3290         lockdep_assert_held(&nn->client_lock)    3051         lockdep_assert_held(&nn->client_lock);
3291         return find_client_in_id_table(tbl, c    3052         return find_client_in_id_table(tbl, clid, sessions);
3292 }                                                3053 }
3293                                                  3054 
3294 static bool clp_used_exchangeid(struct nfs4_c    3055 static bool clp_used_exchangeid(struct nfs4_client *clp)
3295 {                                                3056 {
3296         return clp->cl_exchange_flags != 0;      3057         return clp->cl_exchange_flags != 0;
3297 }                                                3058 } 
3298                                                  3059 
3299 static struct nfs4_client *                      3060 static struct nfs4_client *
3300 find_confirmed_client_by_name(struct xdr_neto    3061 find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
3301 {                                                3062 {
3302         lockdep_assert_held(&nn->client_lock)    3063         lockdep_assert_held(&nn->client_lock);
3303         return find_clp_in_name_tree(name, &n    3064         return find_clp_in_name_tree(name, &nn->conf_name_tree);
3304 }                                                3065 }
3305                                                  3066 
3306 static struct nfs4_client *                      3067 static struct nfs4_client *
3307 find_unconfirmed_client_by_name(struct xdr_ne    3068 find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
3308 {                                                3069 {
3309         lockdep_assert_held(&nn->client_lock)    3070         lockdep_assert_held(&nn->client_lock);
3310         return find_clp_in_name_tree(name, &n    3071         return find_clp_in_name_tree(name, &nn->unconf_name_tree);
3311 }                                                3072 }
3312                                                  3073 
3313 static void                                      3074 static void
3314 gen_callback(struct nfs4_client *clp, struct     3075 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
3315 {                                                3076 {
3316         struct nfs4_cb_conn *conn = &clp->cl_    3077         struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
3317         struct sockaddr *sa = svc_addr(rqstp)    3078         struct sockaddr *sa = svc_addr(rqstp);
3318         u32 scopeid = rpc_get_scope_id(sa);      3079         u32 scopeid = rpc_get_scope_id(sa);
3319         unsigned short expected_family;          3080         unsigned short expected_family;
3320                                                  3081 
3321         /* Currently, we only support tcp and    3082         /* Currently, we only support tcp and tcp6 for the callback channel */
3322         if (se->se_callback_netid_len == 3 &&    3083         if (se->se_callback_netid_len == 3 &&
3323             !memcmp(se->se_callback_netid_val    3084             !memcmp(se->se_callback_netid_val, "tcp", 3))
3324                 expected_family = AF_INET;       3085                 expected_family = AF_INET;
3325         else if (se->se_callback_netid_len ==    3086         else if (se->se_callback_netid_len == 4 &&
3326                  !memcmp(se->se_callback_neti    3087                  !memcmp(se->se_callback_netid_val, "tcp6", 4))
3327                 expected_family = AF_INET6;      3088                 expected_family = AF_INET6;
3328         else                                     3089         else
3329                 goto out_err;                    3090                 goto out_err;
3330                                                  3091 
3331         conn->cb_addrlen = rpc_uaddr2sockaddr    3092         conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
3332                                             s    3093                                             se->se_callback_addr_len,
3333                                             (    3094                                             (struct sockaddr *)&conn->cb_addr,
3334                                             s    3095                                             sizeof(conn->cb_addr));
3335                                                  3096 
3336         if (!conn->cb_addrlen || conn->cb_add    3097         if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
3337                 goto out_err;                    3098                 goto out_err;
3338                                                  3099 
3339         if (conn->cb_addr.ss_family == AF_INE    3100         if (conn->cb_addr.ss_family == AF_INET6)
3340                 ((struct sockaddr_in6 *)&conn    3101                 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
3341                                                  3102 
3342         conn->cb_prog = se->se_callback_prog;    3103         conn->cb_prog = se->se_callback_prog;
3343         conn->cb_ident = se->se_callback_iden    3104         conn->cb_ident = se->se_callback_ident;
3344         memcpy(&conn->cb_saddr, &rqstp->rq_da    3105         memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
3345         trace_nfsd_cb_args(clp, conn);           3106         trace_nfsd_cb_args(clp, conn);
3346         return;                                  3107         return;
3347 out_err:                                         3108 out_err:
3348         conn->cb_addr.ss_family = AF_UNSPEC;     3109         conn->cb_addr.ss_family = AF_UNSPEC;
3349         conn->cb_addrlen = 0;                    3110         conn->cb_addrlen = 0;
3350         trace_nfsd_cb_nodelegs(clp);             3111         trace_nfsd_cb_nodelegs(clp);
3351         return;                                  3112         return;
3352 }                                                3113 }
3353                                                  3114 
3354 /*                                               3115 /*
3355  * Cache a reply. nfsd4_check_resp_size() has    3116  * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
3356  */                                              3117  */
3357 static void                                      3118 static void
3358 nfsd4_store_cache_entry(struct nfsd4_compound    3119 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
3359 {                                                3120 {
3360         struct xdr_buf *buf = resp->xdr->buf;    3121         struct xdr_buf *buf = resp->xdr->buf;
3361         struct nfsd4_slot *slot = resp->cstat    3122         struct nfsd4_slot *slot = resp->cstate.slot;
3362         unsigned int base;                       3123         unsigned int base;
3363                                                  3124 
3364         dprintk("--> %s slot %p\n", __func__,    3125         dprintk("--> %s slot %p\n", __func__, slot);
3365                                                  3126 
3366         slot->sl_flags |= NFSD4_SLOT_INITIALI    3127         slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
3367         slot->sl_opcnt = resp->opcnt;            3128         slot->sl_opcnt = resp->opcnt;
3368         slot->sl_status = resp->cstate.status    3129         slot->sl_status = resp->cstate.status;
3369         free_svc_cred(&slot->sl_cred);           3130         free_svc_cred(&slot->sl_cred);
3370         copy_cred(&slot->sl_cred, &resp->rqst    3131         copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred);
3371                                                  3132 
3372         if (!nfsd4_cache_this(resp)) {           3133         if (!nfsd4_cache_this(resp)) {
3373                 slot->sl_flags &= ~NFSD4_SLOT    3134                 slot->sl_flags &= ~NFSD4_SLOT_CACHED;
3374                 return;                          3135                 return;
3375         }                                        3136         }
3376         slot->sl_flags |= NFSD4_SLOT_CACHED;     3137         slot->sl_flags |= NFSD4_SLOT_CACHED;
3377                                                  3138 
3378         base = resp->cstate.data_offset;         3139         base = resp->cstate.data_offset;
3379         slot->sl_datalen = buf->len - base;      3140         slot->sl_datalen = buf->len - base;
3380         if (read_bytes_from_xdr_buf(buf, base    3141         if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
3381                 WARN(1, "%s: sessions DRC cou    3142                 WARN(1, "%s: sessions DRC could not cache compound\n",
3382                      __func__);                  3143                      __func__);
3383         return;                                  3144         return;
3384 }                                                3145 }
3385                                                  3146 
3386 /*                                               3147 /*
3387  * Encode the replay sequence operation from     3148  * Encode the replay sequence operation from the slot values.
3388  * If cachethis is FALSE encode the uncached     3149  * If cachethis is FALSE encode the uncached rep error on the next
3389  * operation which sets resp->p and increment    3150  * operation which sets resp->p and increments resp->opcnt for
3390  * nfs4svc_encode_compoundres.                   3151  * nfs4svc_encode_compoundres.
3391  *                                               3152  *
3392  */                                              3153  */
3393 static __be32                                    3154 static __be32
3394 nfsd4_enc_sequence_replay(struct nfsd4_compou    3155 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
3395                           struct nfsd4_compou    3156                           struct nfsd4_compoundres *resp)
3396 {                                                3157 {
3397         struct nfsd4_op *op;                     3158         struct nfsd4_op *op;
3398         struct nfsd4_slot *slot = resp->cstat    3159         struct nfsd4_slot *slot = resp->cstate.slot;
3399                                                  3160 
3400         /* Encode the replayed sequence opera    3161         /* Encode the replayed sequence operation */
3401         op = &args->ops[resp->opcnt - 1];        3162         op = &args->ops[resp->opcnt - 1];
3402         nfsd4_encode_operation(resp, op);        3163         nfsd4_encode_operation(resp, op);
3403                                                  3164 
3404         if (slot->sl_flags & NFSD4_SLOT_CACHE    3165         if (slot->sl_flags & NFSD4_SLOT_CACHED)
3405                 return op->status;               3166                 return op->status;
3406         if (args->opcnt == 1) {                  3167         if (args->opcnt == 1) {
3407                 /*                               3168                 /*
3408                  * The original operation was    3169                  * The original operation wasn't a solo sequence--we
3409                  * always cache those--so thi    3170                  * always cache those--so this retry must not match the
3410                  * original:                     3171                  * original:
3411                  */                              3172                  */
3412                 op->status = nfserr_seq_false    3173                 op->status = nfserr_seq_false_retry;
3413         } else {                                 3174         } else {
3414                 op = &args->ops[resp->opcnt++    3175                 op = &args->ops[resp->opcnt++];
3415                 op->status = nfserr_retry_unc    3176                 op->status = nfserr_retry_uncached_rep;
3416                 nfsd4_encode_operation(resp,     3177                 nfsd4_encode_operation(resp, op);
3417         }                                        3178         }
3418         return op->status;                       3179         return op->status;
3419 }                                                3180 }
3420                                                  3181 
3421 /*                                               3182 /*
3422  * The sequence operation is not cached becau    3183  * The sequence operation is not cached because we can use the slot and
3423  * session values.                               3184  * session values.
3424  */                                              3185  */
3425 static __be32                                    3186 static __be32
3426 nfsd4_replay_cache_entry(struct nfsd4_compoun    3187 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
3427                          struct nfsd4_sequenc    3188                          struct nfsd4_sequence *seq)
3428 {                                                3189 {
3429         struct nfsd4_slot *slot = resp->cstat    3190         struct nfsd4_slot *slot = resp->cstate.slot;
3430         struct xdr_stream *xdr = resp->xdr;      3191         struct xdr_stream *xdr = resp->xdr;
3431         __be32 *p;                               3192         __be32 *p;
3432         __be32 status;                           3193         __be32 status;
3433                                                  3194 
3434         dprintk("--> %s slot %p\n", __func__,    3195         dprintk("--> %s slot %p\n", __func__, slot);
3435                                                  3196 
3436         status = nfsd4_enc_sequence_replay(re    3197         status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
3437         if (status)                              3198         if (status)
3438                 return status;                   3199                 return status;
3439                                                  3200 
3440         p = xdr_reserve_space(xdr, slot->sl_d    3201         p = xdr_reserve_space(xdr, slot->sl_datalen);
3441         if (!p) {                                3202         if (!p) {
3442                 WARN_ON_ONCE(1);                 3203                 WARN_ON_ONCE(1);
3443                 return nfserr_serverfault;       3204                 return nfserr_serverfault;
3444         }                                        3205         }
3445         xdr_encode_opaque_fixed(p, slot->sl_d    3206         xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
3446         xdr_commit_encode(xdr);                  3207         xdr_commit_encode(xdr);
3447                                                  3208 
3448         resp->opcnt = slot->sl_opcnt;            3209         resp->opcnt = slot->sl_opcnt;
3449         return slot->sl_status;                  3210         return slot->sl_status;
3450 }                                                3211 }
3451                                                  3212 
3452 /*                                               3213 /*
3453  * Set the exchange_id flags returned by the     3214  * Set the exchange_id flags returned by the server.
3454  */                                              3215  */
3455 static void                                      3216 static void
3456 nfsd4_set_ex_flags(struct nfs4_client *new, s    3217 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
3457 {                                                3218 {
3458 #ifdef CONFIG_NFSD_PNFS                          3219 #ifdef CONFIG_NFSD_PNFS
3459         new->cl_exchange_flags |= EXCHGID4_FL    3220         new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
3460 #else                                            3221 #else
3461         new->cl_exchange_flags |= EXCHGID4_FL    3222         new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
3462 #endif                                           3223 #endif
3463                                                  3224 
3464         /* Referrals are supported, Migration    3225         /* Referrals are supported, Migration is not. */
3465         new->cl_exchange_flags |= EXCHGID4_FL    3226         new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
3466                                                  3227 
3467         /* set the wire flags to return to cl    3228         /* set the wire flags to return to client. */
3468         clid->flags = new->cl_exchange_flags;    3229         clid->flags = new->cl_exchange_flags;
3469 }                                                3230 }
3470                                                  3231 
3471 static bool client_has_openowners(struct nfs4    3232 static bool client_has_openowners(struct nfs4_client *clp)
3472 {                                                3233 {
3473         struct nfs4_openowner *oo;               3234         struct nfs4_openowner *oo;
3474                                                  3235 
3475         list_for_each_entry(oo, &clp->cl_open    3236         list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
3476                 if (!list_empty(&oo->oo_owner    3237                 if (!list_empty(&oo->oo_owner.so_stateids))
3477                         return true;             3238                         return true;
3478         }                                        3239         }
3479         return false;                            3240         return false;
3480 }                                                3241 }
3481                                                  3242 
3482 static bool client_has_state(struct nfs4_clie    3243 static bool client_has_state(struct nfs4_client *clp)
3483 {                                                3244 {
3484         return client_has_openowners(clp)        3245         return client_has_openowners(clp)
3485 #ifdef CONFIG_NFSD_PNFS                          3246 #ifdef CONFIG_NFSD_PNFS
3486                 || !list_empty(&clp->cl_lo_st    3247                 || !list_empty(&clp->cl_lo_states)
3487 #endif                                           3248 #endif
3488                 || !list_empty(&clp->cl_deleg    3249                 || !list_empty(&clp->cl_delegations)
3489                 || !list_empty(&clp->cl_sessi    3250                 || !list_empty(&clp->cl_sessions)
3490                 || !list_empty(&clp->async_co    3251                 || !list_empty(&clp->async_copies);
3491 }                                                3252 }
3492                                                  3253 
3493 static __be32 copy_impl_id(struct nfs4_client    3254 static __be32 copy_impl_id(struct nfs4_client *clp,
3494                                 struct nfsd4_    3255                                 struct nfsd4_exchange_id *exid)
3495 {                                                3256 {
3496         if (!exid->nii_domain.data)              3257         if (!exid->nii_domain.data)
3497                 return 0;                        3258                 return 0;
3498         xdr_netobj_dup(&clp->cl_nii_domain, &    3259         xdr_netobj_dup(&clp->cl_nii_domain, &exid->nii_domain, GFP_KERNEL);
3499         if (!clp->cl_nii_domain.data)            3260         if (!clp->cl_nii_domain.data)
3500                 return nfserr_jukebox;           3261                 return nfserr_jukebox;
3501         xdr_netobj_dup(&clp->cl_nii_name, &ex    3262         xdr_netobj_dup(&clp->cl_nii_name, &exid->nii_name, GFP_KERNEL);
3502         if (!clp->cl_nii_name.data)              3263         if (!clp->cl_nii_name.data)
3503                 return nfserr_jukebox;           3264                 return nfserr_jukebox;
3504         clp->cl_nii_time = exid->nii_time;       3265         clp->cl_nii_time = exid->nii_time;
3505         return 0;                                3266         return 0;
3506 }                                                3267 }
3507                                                  3268 
3508 __be32                                           3269 __be32
3509 nfsd4_exchange_id(struct svc_rqst *rqstp, str    3270 nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3510                 union nfsd4_op_u *u)             3271                 union nfsd4_op_u *u)
3511 {                                                3272 {
3512         struct nfsd4_exchange_id *exid = &u->    3273         struct nfsd4_exchange_id *exid = &u->exchange_id;
3513         struct nfs4_client *conf, *new;          3274         struct nfs4_client *conf, *new;
3514         struct nfs4_client *unconf = NULL;       3275         struct nfs4_client *unconf = NULL;
3515         __be32 status;                           3276         __be32 status;
3516         char                    addr_str[INET    3277         char                    addr_str[INET6_ADDRSTRLEN];
3517         nfs4_verifier           verf = exid->    3278         nfs4_verifier           verf = exid->verifier;
3518         struct sockaddr         *sa = svc_add    3279         struct sockaddr         *sa = svc_addr(rqstp);
3519         bool    update = exid->flags & EXCHGI    3280         bool    update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
3520         struct nfsd_net         *nn = net_gen    3281         struct nfsd_net         *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3521                                                  3282 
3522         rpc_ntop(sa, addr_str, sizeof(addr_st    3283         rpc_ntop(sa, addr_str, sizeof(addr_str));
3523         dprintk("%s rqstp=%p exid=%p clname.l    3284         dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
3524                 "ip_addr=%s flags %x, spa_how    3285                 "ip_addr=%s flags %x, spa_how %u\n",
3525                 __func__, rqstp, exid, exid->    3286                 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
3526                 addr_str, exid->flags, exid->    3287                 addr_str, exid->flags, exid->spa_how);
3527                                                  3288 
3528         if (exid->flags & ~EXCHGID4_FLAG_MASK    3289         if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
3529                 return nfserr_inval;             3290                 return nfserr_inval;
3530                                                  3291 
3531         new = create_client(exid->clname, rqs    3292         new = create_client(exid->clname, rqstp, &verf);
3532         if (new == NULL)                         3293         if (new == NULL)
3533                 return nfserr_jukebox;           3294                 return nfserr_jukebox;
3534         status = copy_impl_id(new, exid);        3295         status = copy_impl_id(new, exid);
3535         if (status)                              3296         if (status)
3536                 goto out_nolock;                 3297                 goto out_nolock;
3537                                                  3298 
3538         switch (exid->spa_how) {                 3299         switch (exid->spa_how) {
3539         case SP4_MACH_CRED:                      3300         case SP4_MACH_CRED:
3540                 exid->spo_must_enforce[0] = 0    3301                 exid->spo_must_enforce[0] = 0;
3541                 exid->spo_must_enforce[1] = (    3302                 exid->spo_must_enforce[1] = (
3542                         1 << (OP_BIND_CONN_TO    3303                         1 << (OP_BIND_CONN_TO_SESSION - 32) |
3543                         1 << (OP_EXCHANGE_ID     3304                         1 << (OP_EXCHANGE_ID - 32) |
3544                         1 << (OP_CREATE_SESSI    3305                         1 << (OP_CREATE_SESSION - 32) |
3545                         1 << (OP_DESTROY_SESS    3306                         1 << (OP_DESTROY_SESSION - 32) |
3546                         1 << (OP_DESTROY_CLIE    3307                         1 << (OP_DESTROY_CLIENTID - 32));
3547                                                  3308 
3548                 exid->spo_must_allow[0] &= (1    3309                 exid->spo_must_allow[0] &= (1 << (OP_CLOSE) |
3549                                         1 <<     3310                                         1 << (OP_OPEN_DOWNGRADE) |
3550                                         1 <<     3311                                         1 << (OP_LOCKU) |
3551                                         1 <<     3312                                         1 << (OP_DELEGRETURN));
3552                                                  3313 
3553                 exid->spo_must_allow[1] &= (     3314                 exid->spo_must_allow[1] &= (
3554                                         1 <<     3315                                         1 << (OP_TEST_STATEID - 32) |
3555                                         1 <<     3316                                         1 << (OP_FREE_STATEID - 32));
3556                 if (!svc_rqst_integrity_prote    3317                 if (!svc_rqst_integrity_protected(rqstp)) {
3557                         status = nfserr_inval    3318                         status = nfserr_inval;
3558                         goto out_nolock;         3319                         goto out_nolock;
3559                 }                                3320                 }
3560                 /*                               3321                 /*
3561                  * Sometimes userspace doesn'    3322                  * Sometimes userspace doesn't give us a principal.
3562                  * Which is a bug, really.  A    3323                  * Which is a bug, really.  Anyway, we can't enforce
3563                  * MACH_CRED in that case, be    3324                  * MACH_CRED in that case, better to give up now:
3564                  */                              3325                  */
3565                 if (!new->cl_cred.cr_principa    3326                 if (!new->cl_cred.cr_principal &&
3566                                         !new-    3327                                         !new->cl_cred.cr_raw_principal) {
3567                         status = nfserr_serve    3328                         status = nfserr_serverfault;
3568                         goto out_nolock;         3329                         goto out_nolock;
3569                 }                                3330                 }
3570                 new->cl_mach_cred = true;        3331                 new->cl_mach_cred = true;
3571                 break;                           3332                 break;
3572         case SP4_NONE:                           3333         case SP4_NONE:
3573                 break;                           3334                 break;
3574         default:                                 3335         default:                                /* checked by xdr code */
3575                 WARN_ON_ONCE(1);                 3336                 WARN_ON_ONCE(1);
3576                 fallthrough;                     3337                 fallthrough;
3577         case SP4_SSV:                            3338         case SP4_SSV:
3578                 status = nfserr_encr_alg_unsu    3339                 status = nfserr_encr_alg_unsupp;
3579                 goto out_nolock;                 3340                 goto out_nolock;
3580         }                                        3341         }
3581                                                  3342 
3582         /* Cases below refer to rfc 5661 sect    3343         /* Cases below refer to rfc 5661 section 18.35.4: */
3583         spin_lock(&nn->client_lock);             3344         spin_lock(&nn->client_lock);
3584         conf = find_confirmed_client_by_name(    3345         conf = find_confirmed_client_by_name(&exid->clname, nn);
3585         if (conf) {                              3346         if (conf) {
3586                 bool creds_match = same_creds    3347                 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
3587                 bool verfs_match = same_verf(    3348                 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
3588                                                  3349 
3589                 if (update) {                    3350                 if (update) {
3590                         if (!clp_used_exchang    3351                         if (!clp_used_exchangeid(conf)) { /* buggy client */
3591                                 status = nfse    3352                                 status = nfserr_inval;
3592                                 goto out;        3353                                 goto out;
3593                         }                        3354                         }
3594                         if (!nfsd4_mach_creds    3355                         if (!nfsd4_mach_creds_match(conf, rqstp)) {
3595                                 status = nfse    3356                                 status = nfserr_wrong_cred;
3596                                 goto out;        3357                                 goto out;
3597                         }                        3358                         }
3598                         if (!creds_match) { /    3359                         if (!creds_match) { /* case 9 */
3599                                 status = nfse    3360                                 status = nfserr_perm;
3600                                 goto out;        3361                                 goto out;
3601                         }                        3362                         }
3602                         if (!verfs_match) { /    3363                         if (!verfs_match) { /* case 8 */
3603                                 status = nfse    3364                                 status = nfserr_not_same;
3604                                 goto out;        3365                                 goto out;
3605                         }                        3366                         }
3606                         /* case 6 */             3367                         /* case 6 */
3607                         exid->flags |= EXCHGI    3368                         exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
3608                         trace_nfsd_clid_confi    3369                         trace_nfsd_clid_confirmed_r(conf);
3609                         goto out_copy;           3370                         goto out_copy;
3610                 }                                3371                 }
3611                 if (!creds_match) { /* case 3    3372                 if (!creds_match) { /* case 3 */
3612                         if (client_has_state(    3373                         if (client_has_state(conf)) {
3613                                 status = nfse    3374                                 status = nfserr_clid_inuse;
3614                                 trace_nfsd_cl    3375                                 trace_nfsd_clid_cred_mismatch(conf, rqstp);
3615                                 goto out;        3376                                 goto out;
3616                         }                        3377                         }
3617                         goto out_new;            3378                         goto out_new;
3618                 }                                3379                 }
3619                 if (verfs_match) { /* case 2     3380                 if (verfs_match) { /* case 2 */
3620                         conf->cl_exchange_fla    3381                         conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
3621                         trace_nfsd_clid_confi    3382                         trace_nfsd_clid_confirmed_r(conf);
3622                         goto out_copy;           3383                         goto out_copy;
3623                 }                                3384                 }
3624                 /* case 5, client reboot */      3385                 /* case 5, client reboot */
3625                 trace_nfsd_clid_verf_mismatch    3386                 trace_nfsd_clid_verf_mismatch(conf, rqstp, &verf);
3626                 conf = NULL;                     3387                 conf = NULL;
3627                 goto out_new;                    3388                 goto out_new;
3628         }                                        3389         }
3629                                                  3390 
3630         if (update) { /* case 7 */               3391         if (update) { /* case 7 */
3631                 status = nfserr_noent;           3392                 status = nfserr_noent;
3632                 goto out;                        3393                 goto out;
3633         }                                        3394         }
3634                                                  3395 
3635         unconf = find_unconfirmed_client_by_n    3396         unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
3636         if (unconf) /* case 4, possible retry    3397         if (unconf) /* case 4, possible retry or client restart */
3637                 unhash_client_locked(unconf);    3398                 unhash_client_locked(unconf);
3638                                                  3399 
3639         /* case 1, new owner ID */               3400         /* case 1, new owner ID */
3640         trace_nfsd_clid_fresh(new);              3401         trace_nfsd_clid_fresh(new);
3641                                                  3402 
3642 out_new:                                         3403 out_new:
3643         if (conf) {                              3404         if (conf) {
3644                 status = mark_client_expired_    3405                 status = mark_client_expired_locked(conf);
3645                 if (status)                      3406                 if (status)
3646                         goto out;                3407                         goto out;
3647                 trace_nfsd_clid_replaced(&con    3408                 trace_nfsd_clid_replaced(&conf->cl_clientid);
3648         }                                        3409         }
3649         new->cl_minorversion = cstate->minorv    3410         new->cl_minorversion = cstate->minorversion;
3650         new->cl_spo_must_allow.u.words[0] = e    3411         new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0];
3651         new->cl_spo_must_allow.u.words[1] = e    3412         new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1];
3652                                                  3413 
3653         /* Contrived initial CREATE_SESSION r << 
3654         new->cl_cs_slot.sl_status = nfserr_se << 
3655                                               << 
3656         add_to_unconfirmed(new);                 3414         add_to_unconfirmed(new);
3657         swap(new, conf);                         3415         swap(new, conf);
3658 out_copy:                                        3416 out_copy:
3659         exid->clientid.cl_boot = conf->cl_cli    3417         exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
3660         exid->clientid.cl_id = conf->cl_clien    3418         exid->clientid.cl_id = conf->cl_clientid.cl_id;
3661                                                  3419 
3662         exid->seqid = conf->cl_cs_slot.sl_seq    3420         exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
3663         nfsd4_set_ex_flags(conf, exid);          3421         nfsd4_set_ex_flags(conf, exid);
3664                                                  3422 
3665         dprintk("nfsd4_exchange_id seqid %d f    3423         dprintk("nfsd4_exchange_id seqid %d flags %x\n",
3666                 conf->cl_cs_slot.sl_seqid, co    3424                 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
3667         status = nfs_ok;                         3425         status = nfs_ok;
3668                                                  3426 
3669 out:                                             3427 out:
3670         spin_unlock(&nn->client_lock);           3428         spin_unlock(&nn->client_lock);
3671 out_nolock:                                      3429 out_nolock:
3672         if (new)                                 3430         if (new)
3673                 expire_client(new);              3431                 expire_client(new);
3674         if (unconf) {                            3432         if (unconf) {
3675                 trace_nfsd_clid_expire_unconf    3433                 trace_nfsd_clid_expire_unconf(&unconf->cl_clientid);
3676                 expire_client(unconf);           3434                 expire_client(unconf);
3677         }                                        3435         }
3678         return status;                           3436         return status;
3679 }                                                3437 }
3680                                                  3438 
3681 static __be32 check_slot_seqid(u32 seqid, u32 !! 3439 static __be32
                                                   >> 3440 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
3682 {                                                3441 {
                                                   >> 3442         dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
                                                   >> 3443                 slot_seqid);
                                                   >> 3444 
3683         /* The slot is in use, and no respons    3445         /* The slot is in use, and no response has been sent. */
3684         if (slot_inuse) {                        3446         if (slot_inuse) {
3685                 if (seqid == slot_seqid)         3447                 if (seqid == slot_seqid)
3686                         return nfserr_jukebox    3448                         return nfserr_jukebox;
3687                 else                             3449                 else
3688                         return nfserr_seq_mis    3450                         return nfserr_seq_misordered;
3689         }                                        3451         }
3690         /* Note unsigned 32-bit arithmetic ha    3452         /* Note unsigned 32-bit arithmetic handles wraparound: */
3691         if (likely(seqid == slot_seqid + 1))     3453         if (likely(seqid == slot_seqid + 1))
3692                 return nfs_ok;                   3454                 return nfs_ok;
3693         if (seqid == slot_seqid)                 3455         if (seqid == slot_seqid)
3694                 return nfserr_replay_cache;      3456                 return nfserr_replay_cache;
3695         return nfserr_seq_misordered;            3457         return nfserr_seq_misordered;
3696 }                                                3458 }
3697                                                  3459 
3698 /*                                               3460 /*
3699  * Cache the create session result into the c    3461  * Cache the create session result into the create session single DRC
3700  * slot cache by saving the xdr structure. sl    3462  * slot cache by saving the xdr structure. sl_seqid has been set.
3701  * Do this for solo or embedded create sessio    3463  * Do this for solo or embedded create session operations.
3702  */                                              3464  */
3703 static void                                      3465 static void
3704 nfsd4_cache_create_session(struct nfsd4_creat    3466 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
3705                            struct nfsd4_clid_    3467                            struct nfsd4_clid_slot *slot, __be32 nfserr)
3706 {                                                3468 {
3707         slot->sl_status = nfserr;                3469         slot->sl_status = nfserr;
3708         memcpy(&slot->sl_cr_ses, cr_ses, size    3470         memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
3709 }                                                3471 }
3710                                                  3472 
3711 static __be32                                    3473 static __be32
3712 nfsd4_replay_create_session(struct nfsd4_crea    3474 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
3713                             struct nfsd4_clid    3475                             struct nfsd4_clid_slot *slot)
3714 {                                                3476 {
3715         memcpy(cr_ses, &slot->sl_cr_ses, size    3477         memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
3716         return slot->sl_status;                  3478         return slot->sl_status;
3717 }                                                3479 }
3718                                                  3480 
3719 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\              3481 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
3720                         2 * 2 + /* credential    3482                         2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
3721                         1 +     /* MIN tag is    3483                         1 +     /* MIN tag is length with zero, only length */ \
3722                         3 +     /* version, o    3484                         3 +     /* version, opcount, opcode */ \
3723                         XDR_QUADLEN(NFS4_MAX_    3485                         XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3724                                 /* seqid, slo    3486                                 /* seqid, slotID, slotID, cache */ \
3725                         4 ) * sizeof(__be32))    3487                         4 ) * sizeof(__be32))
3726                                                  3488 
3727 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\             3489 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
3728                         2 +     /* verifier:     3490                         2 +     /* verifier: AUTH_NULL, length 0 */\
3729                         1 +     /* status */     3491                         1 +     /* status */ \
3730                         1 +     /* MIN tag is    3492                         1 +     /* MIN tag is length with zero, only length */ \
3731                         3 +     /* opcount, o    3493                         3 +     /* opcount, opcode, opstatus*/ \
3732                         XDR_QUADLEN(NFS4_MAX_    3494                         XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3733                                 /* seqid, slo    3495                                 /* seqid, slotID, slotID, slotID, status */ \
3734                         5 ) * sizeof(__be32))    3496                         5 ) * sizeof(__be32))
3735                                                  3497 
3736 static __be32 check_forechannel_attrs(struct     3498 static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
3737 {                                                3499 {
3738         u32 maxrpc = nn->nfsd_serv->sv_max_me    3500         u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
3739                                                  3501 
3740         if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_    3502         if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
3741                 return nfserr_toosmall;          3503                 return nfserr_toosmall;
3742         if (ca->maxresp_sz < NFSD_MIN_RESP_HD    3504         if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
3743                 return nfserr_toosmall;          3505                 return nfserr_toosmall;
3744         ca->headerpadsz = 0;                     3506         ca->headerpadsz = 0;
3745         ca->maxreq_sz = min_t(u32, ca->maxreq    3507         ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
3746         ca->maxresp_sz = min_t(u32, ca->maxre    3508         ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
3747         ca->maxops = min_t(u32, ca->maxops, N    3509         ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
3748         ca->maxresp_cached = min_t(u32, ca->m    3510         ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
3749                         NFSD_SLOT_CACHE_SIZE     3511                         NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
3750         ca->maxreqs = min_t(u32, ca->maxreqs,    3512         ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
3751         /*                                       3513         /*
3752          * Note decreasing slot size below cl    3514          * Note decreasing slot size below client's request may make it
3753          * difficult for client to function c    3515          * difficult for client to function correctly, whereas
3754          * decreasing the number of slots wil    3516          * decreasing the number of slots will (just?) affect
3755          * performance.  When short on memory    3517          * performance.  When short on memory we therefore prefer to
3756          * decrease number of slots instead o    3518          * decrease number of slots instead of their size.  Clients that
3757          * request larger slots than they nee    3519          * request larger slots than they need will get poor results:
3758          * Note that we always allow at least    3520          * Note that we always allow at least one slot, because our
3759          * accounting is soft and provides no    3521          * accounting is soft and provides no guarantees either way.
3760          */                                      3522          */
3761         ca->maxreqs = nfsd4_get_drc_mem(ca, n    3523         ca->maxreqs = nfsd4_get_drc_mem(ca, nn);
3762                                                  3524 
3763         return nfs_ok;                           3525         return nfs_ok;
3764 }                                                3526 }
3765                                                  3527 
3766 /*                                               3528 /*
3767  * Server's NFSv4.1 backchannel support is AU    3529  * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
3768  * These are based on similar macros in linux    3530  * These are based on similar macros in linux/sunrpc/msg_prot.h .
3769  */                                              3531  */
3770 #define RPC_MAX_HEADER_WITH_AUTH_SYS \           3532 #define RPC_MAX_HEADER_WITH_AUTH_SYS \
3771         (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLS    3533         (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
3772                                                  3534 
3773 #define RPC_MAX_REPHEADER_WITH_AUTH_SYS \        3535 #define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
3774         (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK    3536         (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
3775                                                  3537 
3776 #define NFSD_CB_MAX_REQ_SZ      ((NFS4_enc_cb    3538 #define NFSD_CB_MAX_REQ_SZ      ((NFS4_enc_cb_recall_sz + \
3777                                  RPC_MAX_HEAD    3539                                  RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
3778 #define NFSD_CB_MAX_RESP_SZ     ((NFS4_dec_cb    3540 #define NFSD_CB_MAX_RESP_SZ     ((NFS4_dec_cb_recall_sz + \
3779                                  RPC_MAX_REPH    3541                                  RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
3780                                  sizeof(__be3    3542                                  sizeof(__be32))
3781                                                  3543 
3782 static __be32 check_backchannel_attrs(struct     3544 static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
3783 {                                                3545 {
3784         ca->headerpadsz = 0;                     3546         ca->headerpadsz = 0;
3785                                                  3547 
3786         if (ca->maxreq_sz < NFSD_CB_MAX_REQ_S    3548         if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
3787                 return nfserr_toosmall;          3549                 return nfserr_toosmall;
3788         if (ca->maxresp_sz < NFSD_CB_MAX_RESP    3550         if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
3789                 return nfserr_toosmall;          3551                 return nfserr_toosmall;
3790         ca->maxresp_cached = 0;                  3552         ca->maxresp_cached = 0;
3791         if (ca->maxops < 2)                      3553         if (ca->maxops < 2)
3792                 return nfserr_toosmall;          3554                 return nfserr_toosmall;
3793                                                  3555 
3794         return nfs_ok;                           3556         return nfs_ok;
3795 }                                                3557 }
3796                                                  3558 
3797 static __be32 nfsd4_check_cb_sec(struct nfsd4    3559 static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
3798 {                                                3560 {
3799         switch (cbs->flavor) {                   3561         switch (cbs->flavor) {
3800         case RPC_AUTH_NULL:                      3562         case RPC_AUTH_NULL:
3801         case RPC_AUTH_UNIX:                      3563         case RPC_AUTH_UNIX:
3802                 return nfs_ok;                   3564                 return nfs_ok;
3803         default:                                 3565         default:
3804                 /*                               3566                 /*
3805                  * GSS case: the spec doesn't    3567                  * GSS case: the spec doesn't allow us to return this
3806                  * error.  But it also doesn'    3568                  * error.  But it also doesn't allow us not to support
3807                  * GSS.                          3569                  * GSS.
3808                  * I'd rather this fail hard     3570                  * I'd rather this fail hard than return some error the
3809                  * client might think it can     3571                  * client might think it can already handle:
3810                  */                              3572                  */
3811                 return nfserr_encr_alg_unsupp    3573                 return nfserr_encr_alg_unsupp;
3812         }                                        3574         }
3813 }                                                3575 }
3814                                                  3576 
3815 __be32                                           3577 __be32
3816 nfsd4_create_session(struct svc_rqst *rqstp,     3578 nfsd4_create_session(struct svc_rqst *rqstp,
3817                 struct nfsd4_compound_state *    3579                 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
3818 {                                                3580 {
3819         struct nfsd4_create_session *cr_ses =    3581         struct nfsd4_create_session *cr_ses = &u->create_session;
3820         struct sockaddr *sa = svc_addr(rqstp)    3582         struct sockaddr *sa = svc_addr(rqstp);
3821         struct nfs4_client *conf, *unconf;       3583         struct nfs4_client *conf, *unconf;
3822         struct nfsd4_clid_slot *cs_slot;      << 
3823         struct nfs4_client *old = NULL;          3584         struct nfs4_client *old = NULL;
3824         struct nfsd4_session *new;               3585         struct nfsd4_session *new;
3825         struct nfsd4_conn *conn;                 3586         struct nfsd4_conn *conn;
                                                   >> 3587         struct nfsd4_clid_slot *cs_slot = NULL;
3826         __be32 status = 0;                       3588         __be32 status = 0;
3827         struct nfsd_net *nn = net_generic(SVC    3589         struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3828                                                  3590 
3829         if (cr_ses->flags & ~SESSION4_FLAG_MA    3591         if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
3830                 return nfserr_inval;             3592                 return nfserr_inval;
3831         status = nfsd4_check_cb_sec(&cr_ses->    3593         status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
3832         if (status)                              3594         if (status)
3833                 return status;                   3595                 return status;
3834         status = check_forechannel_attrs(&cr_    3596         status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
3835         if (status)                              3597         if (status)
3836                 return status;                   3598                 return status;
3837         status = check_backchannel_attrs(&cr_    3599         status = check_backchannel_attrs(&cr_ses->back_channel);
3838         if (status)                              3600         if (status)
3839                 goto out_release_drc_mem;        3601                 goto out_release_drc_mem;
3840         status = nfserr_jukebox;                 3602         status = nfserr_jukebox;
3841         new = alloc_session(&cr_ses->fore_cha    3603         new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
3842         if (!new)                                3604         if (!new)
3843                 goto out_release_drc_mem;        3605                 goto out_release_drc_mem;
3844         conn = alloc_conn_from_crses(rqstp, c    3606         conn = alloc_conn_from_crses(rqstp, cr_ses);
3845         if (!conn)                               3607         if (!conn)
3846                 goto out_free_session;           3608                 goto out_free_session;
3847                                                  3609 
3848         spin_lock(&nn->client_lock);             3610         spin_lock(&nn->client_lock);
3849                                               << 
3850         /* RFC 8881 Section 18.36.4 Phase 1:  << 
3851         unconf = find_unconfirmed_client(&cr_    3611         unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
3852         conf = find_confirmed_client(&cr_ses-    3612         conf = find_confirmed_client(&cr_ses->clientid, true, nn);
3853         if (!conf && !unconf) {               !! 3613         WARN_ON_ONCE(conf && unconf);
3854                 status = nfserr_stale_clienti << 
3855                 goto out_free_conn;           << 
3856         }                                     << 
3857                                               << 
3858         /* RFC 8881 Section 18.36.4 Phase 2:  << 
3859         if (conf) {                           << 
3860                 cs_slot = &conf->cl_cs_slot;  << 
3861                 trace_nfsd_slot_seqid_conf(co << 
3862         } else {                              << 
3863                 cs_slot = &unconf->cl_cs_slot << 
3864                 trace_nfsd_slot_seqid_unconf( << 
3865         }                                     << 
3866         status = check_slot_seqid(cr_ses->seq << 
3867         switch (status) {                     << 
3868         case nfs_ok:                          << 
3869                 cs_slot->sl_seqid++;          << 
3870                 cr_ses->seqid = cs_slot->sl_s << 
3871                 break;                        << 
3872         case nfserr_replay_cache:             << 
3873                 status = nfsd4_replay_create_ << 
3874                 fallthrough;                  << 
3875         case nfserr_jukebox:                  << 
3876                 /* The server MUST NOT cache  << 
3877                 goto out_free_conn;           << 
3878         default:                              << 
3879                 goto out_cache_error;         << 
3880         }                                     << 
3881                                                  3614 
3882         /* RFC 8881 Section 18.36.4 Phase 3:  << 
3883         if (conf) {                              3615         if (conf) {
3884                 status = nfserr_wrong_cred;      3616                 status = nfserr_wrong_cred;
3885                 if (!nfsd4_mach_creds_match(c    3617                 if (!nfsd4_mach_creds_match(conf, rqstp))
3886                         goto out_cache_error; !! 3618                         goto out_free_conn;
3887         } else {                              !! 3619                 cs_slot = &conf->cl_cs_slot;
                                                   >> 3620                 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
                                                   >> 3621                 if (status) {
                                                   >> 3622                         if (status == nfserr_replay_cache)
                                                   >> 3623                                 status = nfsd4_replay_create_session(cr_ses, cs_slot);
                                                   >> 3624                         goto out_free_conn;
                                                   >> 3625                 }
                                                   >> 3626         } else if (unconf) {
3888                 status = nfserr_clid_inuse;      3627                 status = nfserr_clid_inuse;
3889                 if (!same_creds(&unconf->cl_c    3628                 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
3890                     !rpc_cmp_addr(sa, (struct    3629                     !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
3891                         trace_nfsd_clid_cred_    3630                         trace_nfsd_clid_cred_mismatch(unconf, rqstp);
3892                         goto out_cache_error; !! 3631                         goto out_free_conn;
3893                 }                                3632                 }
3894                 status = nfserr_wrong_cred;      3633                 status = nfserr_wrong_cred;
3895                 if (!nfsd4_mach_creds_match(u    3634                 if (!nfsd4_mach_creds_match(unconf, rqstp))
3896                         goto out_cache_error; !! 3635                         goto out_free_conn;
                                                   >> 3636                 cs_slot = &unconf->cl_cs_slot;
                                                   >> 3637                 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
                                                   >> 3638                 if (status) {
                                                   >> 3639                         /* an unconfirmed replay returns misordered */
                                                   >> 3640                         status = nfserr_seq_misordered;
                                                   >> 3641                         goto out_free_conn;
                                                   >> 3642                 }
3897                 old = find_confirmed_client_b    3643                 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3898                 if (old) {                       3644                 if (old) {
3899                         status = mark_client_    3645                         status = mark_client_expired_locked(old);
3900                         if (status)           !! 3646                         if (status) {
3901                                 goto out_expi !! 3647                                 old = NULL;
                                                   >> 3648                                 goto out_free_conn;
                                                   >> 3649                         }
3902                         trace_nfsd_clid_repla    3650                         trace_nfsd_clid_replaced(&old->cl_clientid);
3903                 }                                3651                 }
3904                 move_to_confirmed(unconf);       3652                 move_to_confirmed(unconf);
3905                 conf = unconf;                   3653                 conf = unconf;
                                                   >> 3654         } else {
                                                   >> 3655                 status = nfserr_stale_clientid;
                                                   >> 3656                 goto out_free_conn;
3906         }                                        3657         }
3907                                               << 
3908         /* RFC 8881 Section 18.36.4 Phase 4:  << 
3909         status = nfs_ok;                         3658         status = nfs_ok;
3910         /* Persistent sessions are not suppor    3659         /* Persistent sessions are not supported */
3911         cr_ses->flags &= ~SESSION4_PERSIST;      3660         cr_ses->flags &= ~SESSION4_PERSIST;
3912         /* Upshifting from TCP to RDMA is not    3661         /* Upshifting from TCP to RDMA is not supported */
3913         cr_ses->flags &= ~SESSION4_RDMA;         3662         cr_ses->flags &= ~SESSION4_RDMA;
3914                                                  3663 
3915         init_session(rqstp, new, conf, cr_ses    3664         init_session(rqstp, new, conf, cr_ses);
3916         nfsd4_get_session_locked(new);           3665         nfsd4_get_session_locked(new);
3917                                                  3666 
3918         memcpy(cr_ses->sessionid.data, new->s    3667         memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
3919                NFS4_MAX_SESSIONID_LEN);          3668                NFS4_MAX_SESSIONID_LEN);
                                                   >> 3669         cs_slot->sl_seqid++;
                                                   >> 3670         cr_ses->seqid = cs_slot->sl_seqid;
3920                                                  3671 
3921         /* cache solo and embedded create ses    3672         /* cache solo and embedded create sessions under the client_lock */
3922         nfsd4_cache_create_session(cr_ses, cs    3673         nfsd4_cache_create_session(cr_ses, cs_slot, status);
3923         spin_unlock(&nn->client_lock);           3674         spin_unlock(&nn->client_lock);
3924         if (conf == unconf)                      3675         if (conf == unconf)
3925                 fsnotify_dentry(conf->cl_nfsd    3676                 fsnotify_dentry(conf->cl_nfsd_info_dentry, FS_MODIFY);
3926         /* init connection and backchannel */    3677         /* init connection and backchannel */
3927         nfsd4_init_conn(rqstp, conn, new);       3678         nfsd4_init_conn(rqstp, conn, new);
3928         nfsd4_put_session(new);                  3679         nfsd4_put_session(new);
3929         if (old)                                 3680         if (old)
3930                 expire_client(old);              3681                 expire_client(old);
3931         return status;                           3682         return status;
3932                                               << 
3933 out_expired_error:                            << 
3934         old = NULL;                           << 
3935         /*                                    << 
3936          * Revert the slot seq_nr change so t << 
3937          * the client's resend instead of ret << 
3938          */                                   << 
3939         if (status == nfserr_jukebox) {       << 
3940                 cs_slot->sl_seqid--;          << 
3941                 cr_ses->seqid = cs_slot->sl_s << 
3942                 goto out_free_conn;           << 
3943         }                                     << 
3944 out_cache_error:                              << 
3945         nfsd4_cache_create_session(cr_ses, cs << 
3946 out_free_conn:                                   3683 out_free_conn:
3947         spin_unlock(&nn->client_lock);           3684         spin_unlock(&nn->client_lock);
3948         free_conn(conn);                         3685         free_conn(conn);
3949         if (old)                                 3686         if (old)
3950                 expire_client(old);              3687                 expire_client(old);
3951 out_free_session:                                3688 out_free_session:
3952         __free_session(new);                     3689         __free_session(new);
3953 out_release_drc_mem:                             3690 out_release_drc_mem:
3954         nfsd4_put_drc_mem(&cr_ses->fore_chann    3691         nfsd4_put_drc_mem(&cr_ses->fore_channel);
3955         return status;                           3692         return status;
3956 }                                                3693 }
3957                                                  3694 
3958 static __be32 nfsd4_map_bcts_dir(u32 *dir)       3695 static __be32 nfsd4_map_bcts_dir(u32 *dir)
3959 {                                                3696 {
3960         switch (*dir) {                          3697         switch (*dir) {
3961         case NFS4_CDFC4_FORE:                    3698         case NFS4_CDFC4_FORE:
3962         case NFS4_CDFC4_BACK:                    3699         case NFS4_CDFC4_BACK:
3963                 return nfs_ok;                   3700                 return nfs_ok;
3964         case NFS4_CDFC4_FORE_OR_BOTH:            3701         case NFS4_CDFC4_FORE_OR_BOTH:
3965         case NFS4_CDFC4_BACK_OR_BOTH:            3702         case NFS4_CDFC4_BACK_OR_BOTH:
3966                 *dir = NFS4_CDFC4_BOTH;          3703                 *dir = NFS4_CDFC4_BOTH;
3967                 return nfs_ok;                   3704                 return nfs_ok;
3968         }                                        3705         }
3969         return nfserr_inval;                     3706         return nfserr_inval;
3970 }                                                3707 }
3971                                                  3708 
3972 __be32 nfsd4_backchannel_ctl(struct svc_rqst     3709 __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp,
3973                 struct nfsd4_compound_state *    3710                 struct nfsd4_compound_state *cstate,
3974                 union nfsd4_op_u *u)             3711                 union nfsd4_op_u *u)
3975 {                                                3712 {
3976         struct nfsd4_backchannel_ctl *bc = &u    3713         struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl;
3977         struct nfsd4_session *session = cstat    3714         struct nfsd4_session *session = cstate->session;
3978         struct nfsd_net *nn = net_generic(SVC    3715         struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3979         __be32 status;                           3716         __be32 status;
3980                                                  3717 
3981         status = nfsd4_check_cb_sec(&bc->bc_c    3718         status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
3982         if (status)                              3719         if (status)
3983                 return status;                   3720                 return status;
3984         spin_lock(&nn->client_lock);             3721         spin_lock(&nn->client_lock);
3985         session->se_cb_prog = bc->bc_cb_progr    3722         session->se_cb_prog = bc->bc_cb_program;
3986         session->se_cb_sec = bc->bc_cb_sec;      3723         session->se_cb_sec = bc->bc_cb_sec;
3987         spin_unlock(&nn->client_lock);           3724         spin_unlock(&nn->client_lock);
3988                                                  3725 
3989         nfsd4_probe_callback(session->se_clie    3726         nfsd4_probe_callback(session->se_client);
3990                                                  3727 
3991         return nfs_ok;                           3728         return nfs_ok;
3992 }                                                3729 }
3993                                                  3730 
3994 static struct nfsd4_conn *__nfsd4_find_conn(s    3731 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
3995 {                                                3732 {
3996         struct nfsd4_conn *c;                    3733         struct nfsd4_conn *c;
3997                                                  3734 
3998         list_for_each_entry(c, &s->se_conns,     3735         list_for_each_entry(c, &s->se_conns, cn_persession) {
3999                 if (c->cn_xprt == xpt) {         3736                 if (c->cn_xprt == xpt) {
4000                         return c;                3737                         return c;
4001                 }                                3738                 }
4002         }                                        3739         }
4003         return NULL;                             3740         return NULL;
4004 }                                                3741 }
4005                                                  3742 
4006 static __be32 nfsd4_match_existing_connection    3743 static __be32 nfsd4_match_existing_connection(struct svc_rqst *rqst,
4007                 struct nfsd4_session *session    3744                 struct nfsd4_session *session, u32 req, struct nfsd4_conn **conn)
4008 {                                                3745 {
4009         struct nfs4_client *clp = session->se    3746         struct nfs4_client *clp = session->se_client;
4010         struct svc_xprt *xpt = rqst->rq_xprt;    3747         struct svc_xprt *xpt = rqst->rq_xprt;
4011         struct nfsd4_conn *c;                    3748         struct nfsd4_conn *c;
4012         __be32 status;                           3749         __be32 status;
4013                                                  3750 
4014         /* Following the last paragraph of RF    3751         /* Following the last paragraph of RFC 5661 Section 18.34.3: */
4015         spin_lock(&clp->cl_lock);                3752         spin_lock(&clp->cl_lock);
4016         c = __nfsd4_find_conn(xpt, session);     3753         c = __nfsd4_find_conn(xpt, session);
4017         if (!c)                                  3754         if (!c)
4018                 status = nfserr_noent;           3755                 status = nfserr_noent;
4019         else if (req == c->cn_flags)             3756         else if (req == c->cn_flags)
4020                 status = nfs_ok;                 3757                 status = nfs_ok;
4021         else if (req == NFS4_CDFC4_FORE_OR_BO    3758         else if (req == NFS4_CDFC4_FORE_OR_BOTH &&
4022                                 c->cn_flags !    3759                                 c->cn_flags != NFS4_CDFC4_BACK)
4023                 status = nfs_ok;                 3760                 status = nfs_ok;
4024         else if (req == NFS4_CDFC4_BACK_OR_BO    3761         else if (req == NFS4_CDFC4_BACK_OR_BOTH &&
4025                                 c->cn_flags !    3762                                 c->cn_flags != NFS4_CDFC4_FORE)
4026                 status = nfs_ok;                 3763                 status = nfs_ok;
4027         else                                     3764         else
4028                 status = nfserr_inval;           3765                 status = nfserr_inval;
4029         spin_unlock(&clp->cl_lock);              3766         spin_unlock(&clp->cl_lock);
4030         if (status == nfs_ok && conn)            3767         if (status == nfs_ok && conn)
4031                 *conn = c;                       3768                 *conn = c;
4032         return status;                           3769         return status;
4033 }                                                3770 }
4034                                                  3771 
4035 __be32 nfsd4_bind_conn_to_session(struct svc_    3772 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
4036                      struct nfsd4_compound_st    3773                      struct nfsd4_compound_state *cstate,
4037                      union nfsd4_op_u *u)        3774                      union nfsd4_op_u *u)
4038 {                                                3775 {
4039         struct nfsd4_bind_conn_to_session *bc    3776         struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session;
4040         __be32 status;                           3777         __be32 status;
4041         struct nfsd4_conn *conn;                 3778         struct nfsd4_conn *conn;
4042         struct nfsd4_session *session;           3779         struct nfsd4_session *session;
4043         struct net *net = SVC_NET(rqstp);        3780         struct net *net = SVC_NET(rqstp);
4044         struct nfsd_net *nn = net_generic(net    3781         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4045                                                  3782 
4046         if (!nfsd4_last_compound_op(rqstp))      3783         if (!nfsd4_last_compound_op(rqstp))
4047                 return nfserr_not_only_op;       3784                 return nfserr_not_only_op;
4048         spin_lock(&nn->client_lock);             3785         spin_lock(&nn->client_lock);
4049         session = find_in_sessionid_hashtbl(&    3786         session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
4050         spin_unlock(&nn->client_lock);           3787         spin_unlock(&nn->client_lock);
4051         if (!session)                            3788         if (!session)
4052                 goto out_no_session;             3789                 goto out_no_session;
4053         status = nfserr_wrong_cred;              3790         status = nfserr_wrong_cred;
4054         if (!nfsd4_mach_creds_match(session->    3791         if (!nfsd4_mach_creds_match(session->se_client, rqstp))
4055                 goto out;                        3792                 goto out;
4056         status = nfsd4_match_existing_connect    3793         status = nfsd4_match_existing_connection(rqstp, session,
4057                         bcts->dir, &conn);       3794                         bcts->dir, &conn);
4058         if (status == nfs_ok) {                  3795         if (status == nfs_ok) {
4059                 if (bcts->dir == NFS4_CDFC4_F    3796                 if (bcts->dir == NFS4_CDFC4_FORE_OR_BOTH ||
4060                                 bcts->dir ==     3797                                 bcts->dir == NFS4_CDFC4_BACK)
4061                         conn->cn_flags |= NFS    3798                         conn->cn_flags |= NFS4_CDFC4_BACK;
4062                 nfsd4_probe_callback(session-    3799                 nfsd4_probe_callback(session->se_client);
4063                 goto out;                        3800                 goto out;
4064         }                                        3801         }
4065         if (status == nfserr_inval)              3802         if (status == nfserr_inval)
4066                 goto out;                        3803                 goto out;
4067         status = nfsd4_map_bcts_dir(&bcts->di    3804         status = nfsd4_map_bcts_dir(&bcts->dir);
4068         if (status)                              3805         if (status)
4069                 goto out;                        3806                 goto out;
4070         conn = alloc_conn(rqstp, bcts->dir);     3807         conn = alloc_conn(rqstp, bcts->dir);
4071         status = nfserr_jukebox;                 3808         status = nfserr_jukebox;
4072         if (!conn)                               3809         if (!conn)
4073                 goto out;                        3810                 goto out;
4074         nfsd4_init_conn(rqstp, conn, session)    3811         nfsd4_init_conn(rqstp, conn, session);
4075         status = nfs_ok;                         3812         status = nfs_ok;
4076 out:                                             3813 out:
4077         nfsd4_put_session(session);              3814         nfsd4_put_session(session);
4078 out_no_session:                                  3815 out_no_session:
4079         return status;                           3816         return status;
4080 }                                                3817 }
4081                                                  3818 
4082 static bool nfsd4_compound_in_session(struct     3819 static bool nfsd4_compound_in_session(struct nfsd4_compound_state *cstate, struct nfs4_sessionid *sid)
4083 {                                                3820 {
4084         if (!cstate->session)                    3821         if (!cstate->session)
4085                 return false;                    3822                 return false;
4086         return !memcmp(sid, &cstate->session-    3823         return !memcmp(sid, &cstate->session->se_sessionid, sizeof(*sid));
4087 }                                                3824 }
4088                                                  3825 
4089 __be32                                           3826 __be32
4090 nfsd4_destroy_session(struct svc_rqst *r, str    3827 nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate,
4091                 union nfsd4_op_u *u)             3828                 union nfsd4_op_u *u)
4092 {                                                3829 {
4093         struct nfs4_sessionid *sessionid = &u    3830         struct nfs4_sessionid *sessionid = &u->destroy_session.sessionid;
4094         struct nfsd4_session *ses;               3831         struct nfsd4_session *ses;
4095         __be32 status;                           3832         __be32 status;
4096         int ref_held_by_me = 0;                  3833         int ref_held_by_me = 0;
4097         struct net *net = SVC_NET(r);            3834         struct net *net = SVC_NET(r);
4098         struct nfsd_net *nn = net_generic(net    3835         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4099                                                  3836 
4100         status = nfserr_not_only_op;             3837         status = nfserr_not_only_op;
4101         if (nfsd4_compound_in_session(cstate,    3838         if (nfsd4_compound_in_session(cstate, sessionid)) {
4102                 if (!nfsd4_last_compound_op(r    3839                 if (!nfsd4_last_compound_op(r))
4103                         goto out;                3840                         goto out;
4104                 ref_held_by_me++;                3841                 ref_held_by_me++;
4105         }                                        3842         }
4106         dump_sessionid(__func__, sessionid);     3843         dump_sessionid(__func__, sessionid);
4107         spin_lock(&nn->client_lock);             3844         spin_lock(&nn->client_lock);
4108         ses = find_in_sessionid_hashtbl(sessi    3845         ses = find_in_sessionid_hashtbl(sessionid, net, &status);
4109         if (!ses)                                3846         if (!ses)
4110                 goto out_client_lock;            3847                 goto out_client_lock;
4111         status = nfserr_wrong_cred;              3848         status = nfserr_wrong_cred;
4112         if (!nfsd4_mach_creds_match(ses->se_c    3849         if (!nfsd4_mach_creds_match(ses->se_client, r))
4113                 goto out_put_session;            3850                 goto out_put_session;
4114         status = mark_session_dead_locked(ses    3851         status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
4115         if (status)                              3852         if (status)
4116                 goto out_put_session;            3853                 goto out_put_session;
4117         unhash_session(ses);                     3854         unhash_session(ses);
4118         spin_unlock(&nn->client_lock);           3855         spin_unlock(&nn->client_lock);
4119                                                  3856 
4120         nfsd4_probe_callback_sync(ses->se_cli    3857         nfsd4_probe_callback_sync(ses->se_client);
4121                                                  3858 
4122         spin_lock(&nn->client_lock);             3859         spin_lock(&nn->client_lock);
4123         status = nfs_ok;                         3860         status = nfs_ok;
4124 out_put_session:                                 3861 out_put_session:
4125         nfsd4_put_session_locked(ses);           3862         nfsd4_put_session_locked(ses);
4126 out_client_lock:                                 3863 out_client_lock:
4127         spin_unlock(&nn->client_lock);           3864         spin_unlock(&nn->client_lock);
4128 out:                                             3865 out:
4129         return status;                           3866         return status;
4130 }                                                3867 }
4131                                                  3868 
4132 static __be32 nfsd4_sequence_check_conn(struc    3869 static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
4133 {                                                3870 {
4134         struct nfs4_client *clp = ses->se_cli    3871         struct nfs4_client *clp = ses->se_client;
4135         struct nfsd4_conn *c;                    3872         struct nfsd4_conn *c;
4136         __be32 status = nfs_ok;                  3873         __be32 status = nfs_ok;
4137         int ret;                                 3874         int ret;
4138                                                  3875 
4139         spin_lock(&clp->cl_lock);                3876         spin_lock(&clp->cl_lock);
4140         c = __nfsd4_find_conn(new->cn_xprt, s    3877         c = __nfsd4_find_conn(new->cn_xprt, ses);
4141         if (c)                                   3878         if (c)
4142                 goto out_free;                   3879                 goto out_free;
4143         status = nfserr_conn_not_bound_to_ses    3880         status = nfserr_conn_not_bound_to_session;
4144         if (clp->cl_mach_cred)                   3881         if (clp->cl_mach_cred)
4145                 goto out_free;                   3882                 goto out_free;
4146         __nfsd4_hash_conn(new, ses);             3883         __nfsd4_hash_conn(new, ses);
4147         spin_unlock(&clp->cl_lock);              3884         spin_unlock(&clp->cl_lock);
4148         ret = nfsd4_register_conn(new);          3885         ret = nfsd4_register_conn(new);
4149         if (ret)                                 3886         if (ret)
4150                 /* oops; xprt is already down    3887                 /* oops; xprt is already down: */
4151                 nfsd4_conn_lost(&new->cn_xpt_    3888                 nfsd4_conn_lost(&new->cn_xpt_user);
4152         return nfs_ok;                           3889         return nfs_ok;
4153 out_free:                                        3890 out_free:
4154         spin_unlock(&clp->cl_lock);              3891         spin_unlock(&clp->cl_lock);
4155         free_conn(new);                          3892         free_conn(new);
4156         return status;                           3893         return status;
4157 }                                                3894 }
4158                                                  3895 
4159 static bool nfsd4_session_too_many_ops(struct    3896 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
4160 {                                                3897 {
4161         struct nfsd4_compoundargs *args = rqs    3898         struct nfsd4_compoundargs *args = rqstp->rq_argp;
4162                                                  3899 
4163         return args->opcnt > session->se_fcha    3900         return args->opcnt > session->se_fchannel.maxops;
4164 }                                                3901 }
4165                                                  3902 
4166 static bool nfsd4_request_too_big(struct svc_    3903 static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
4167                                   struct nfsd    3904                                   struct nfsd4_session *session)
4168 {                                                3905 {
4169         struct xdr_buf *xb = &rqstp->rq_arg;     3906         struct xdr_buf *xb = &rqstp->rq_arg;
4170                                                  3907 
4171         return xb->len > session->se_fchannel    3908         return xb->len > session->se_fchannel.maxreq_sz;
4172 }                                                3909 }
4173                                                  3910 
4174 static bool replay_matches_cache(struct svc_r    3911 static bool replay_matches_cache(struct svc_rqst *rqstp,
4175                  struct nfsd4_sequence *seq,     3912                  struct nfsd4_sequence *seq, struct nfsd4_slot *slot)
4176 {                                                3913 {
4177         struct nfsd4_compoundargs *argp = rqs    3914         struct nfsd4_compoundargs *argp = rqstp->rq_argp;
4178                                                  3915 
4179         if ((bool)(slot->sl_flags & NFSD4_SLO    3916         if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) !=
4180             (bool)seq->cachethis)                3917             (bool)seq->cachethis)
4181                 return false;                    3918                 return false;
4182         /*                                       3919         /*
4183          * If there's an error then the reply    3920          * If there's an error then the reply can have fewer ops than
4184          * the call.                             3921          * the call.
4185          */                                      3922          */
4186         if (slot->sl_opcnt < argp->opcnt && !    3923         if (slot->sl_opcnt < argp->opcnt && !slot->sl_status)
4187                 return false;                    3924                 return false;
4188         /*                                       3925         /*
4189          * But if we cached a reply with *mor    3926          * But if we cached a reply with *more* ops than the call you're
4190          * sending us now, then this new call    3927          * sending us now, then this new call is clearly not really a
4191          * replay of the old one:                3928          * replay of the old one:
4192          */                                      3929          */
4193         if (slot->sl_opcnt > argp->opcnt)        3930         if (slot->sl_opcnt > argp->opcnt)
4194                 return false;                    3931                 return false;
4195         /* This is the only check explicitly     3932         /* This is the only check explicitly called by spec: */
4196         if (!same_creds(&rqstp->rq_cred, &slo    3933         if (!same_creds(&rqstp->rq_cred, &slot->sl_cred))
4197                 return false;                    3934                 return false;
4198         /*                                       3935         /*
4199          * There may be more comparisons we c    3936          * There may be more comparisons we could actually do, but the
4200          * spec doesn't require us to catch e    3937          * spec doesn't require us to catch every case where the calls
4201          * don't match (that would require ca    3938          * don't match (that would require caching the call as well as
4202          * the reply), so we don't bother.       3939          * the reply), so we don't bother.
4203          */                                      3940          */
4204         return true;                             3941         return true;
4205 }                                                3942 }
4206                                                  3943 
4207 __be32                                           3944 __be32
4208 nfsd4_sequence(struct svc_rqst *rqstp, struct    3945 nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4209                 union nfsd4_op_u *u)             3946                 union nfsd4_op_u *u)
4210 {                                                3947 {
4211         struct nfsd4_sequence *seq = &u->sequ    3948         struct nfsd4_sequence *seq = &u->sequence;
4212         struct nfsd4_compoundres *resp = rqst    3949         struct nfsd4_compoundres *resp = rqstp->rq_resp;
4213         struct xdr_stream *xdr = resp->xdr;      3950         struct xdr_stream *xdr = resp->xdr;
4214         struct nfsd4_session *session;           3951         struct nfsd4_session *session;
4215         struct nfs4_client *clp;                 3952         struct nfs4_client *clp;
4216         struct nfsd4_slot *slot;                 3953         struct nfsd4_slot *slot;
4217         struct nfsd4_conn *conn;                 3954         struct nfsd4_conn *conn;
4218         __be32 status;                           3955         __be32 status;
4219         int buflen;                              3956         int buflen;
4220         struct net *net = SVC_NET(rqstp);        3957         struct net *net = SVC_NET(rqstp);
4221         struct nfsd_net *nn = net_generic(net    3958         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4222                                                  3959 
4223         if (resp->opcnt != 1)                    3960         if (resp->opcnt != 1)
4224                 return nfserr_sequence_pos;      3961                 return nfserr_sequence_pos;
4225                                                  3962 
4226         /*                                       3963         /*
4227          * Will be either used or freed by nf    3964          * Will be either used or freed by nfsd4_sequence_check_conn
4228          * below.                                3965          * below.
4229          */                                      3966          */
4230         conn = alloc_conn(rqstp, NFS4_CDFC4_F    3967         conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
4231         if (!conn)                               3968         if (!conn)
4232                 return nfserr_jukebox;           3969                 return nfserr_jukebox;
4233                                                  3970 
4234         spin_lock(&nn->client_lock);             3971         spin_lock(&nn->client_lock);
4235         session = find_in_sessionid_hashtbl(&    3972         session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
4236         if (!session)                            3973         if (!session)
4237                 goto out_no_session;             3974                 goto out_no_session;
4238         clp = session->se_client;                3975         clp = session->se_client;
4239                                                  3976 
4240         status = nfserr_too_many_ops;            3977         status = nfserr_too_many_ops;
4241         if (nfsd4_session_too_many_ops(rqstp,    3978         if (nfsd4_session_too_many_ops(rqstp, session))
4242                 goto out_put_session;            3979                 goto out_put_session;
4243                                                  3980 
4244         status = nfserr_req_too_big;             3981         status = nfserr_req_too_big;
4245         if (nfsd4_request_too_big(rqstp, sess    3982         if (nfsd4_request_too_big(rqstp, session))
4246                 goto out_put_session;            3983                 goto out_put_session;
4247                                                  3984 
4248         status = nfserr_badslot;                 3985         status = nfserr_badslot;
4249         if (seq->slotid >= session->se_fchann    3986         if (seq->slotid >= session->se_fchannel.maxreqs)
4250                 goto out_put_session;            3987                 goto out_put_session;
4251                                                  3988 
4252         slot = session->se_slots[seq->slotid]    3989         slot = session->se_slots[seq->slotid];
4253         dprintk("%s: slotid %d\n", __func__,     3990         dprintk("%s: slotid %d\n", __func__, seq->slotid);
4254                                                  3991 
4255         /* We do not negotiate the number of     3992         /* We do not negotiate the number of slots yet, so set the
4256          * maxslots to the session maxreqs wh    3993          * maxslots to the session maxreqs which is used to encode
4257          * sr_highest_slotid and the sr_targe    3994          * sr_highest_slotid and the sr_target_slot id to maxslots */
4258         seq->maxslots = session->se_fchannel.    3995         seq->maxslots = session->se_fchannel.maxreqs;
4259                                                  3996 
4260         trace_nfsd_slot_seqid_sequence(clp, s << 
4261         status = check_slot_seqid(seq->seqid,    3997         status = check_slot_seqid(seq->seqid, slot->sl_seqid,
4262                                         slot-    3998                                         slot->sl_flags & NFSD4_SLOT_INUSE);
4263         if (status == nfserr_replay_cache) {     3999         if (status == nfserr_replay_cache) {
4264                 status = nfserr_seq_misordere    4000                 status = nfserr_seq_misordered;
4265                 if (!(slot->sl_flags & NFSD4_    4001                 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
4266                         goto out_put_session;    4002                         goto out_put_session;
4267                 status = nfserr_seq_false_ret    4003                 status = nfserr_seq_false_retry;
4268                 if (!replay_matches_cache(rqs    4004                 if (!replay_matches_cache(rqstp, seq, slot))
4269                         goto out_put_session;    4005                         goto out_put_session;
4270                 cstate->slot = slot;             4006                 cstate->slot = slot;
4271                 cstate->session = session;       4007                 cstate->session = session;
4272                 cstate->clp = clp;               4008                 cstate->clp = clp;
4273                 /* Return the cached reply st    4009                 /* Return the cached reply status and set cstate->status
4274                  * for nfsd4_proc_compound pr    4010                  * for nfsd4_proc_compound processing */
4275                 status = nfsd4_replay_cache_e    4011                 status = nfsd4_replay_cache_entry(resp, seq);
4276                 cstate->status = nfserr_repla    4012                 cstate->status = nfserr_replay_cache;
4277                 goto out;                        4013                 goto out;
4278         }                                        4014         }
4279         if (status)                              4015         if (status)
4280                 goto out_put_session;            4016                 goto out_put_session;
4281                                                  4017 
4282         status = nfsd4_sequence_check_conn(co    4018         status = nfsd4_sequence_check_conn(conn, session);
4283         conn = NULL;                             4019         conn = NULL;
4284         if (status)                              4020         if (status)
4285                 goto out_put_session;            4021                 goto out_put_session;
4286                                                  4022 
4287         buflen = (seq->cachethis) ?              4023         buflen = (seq->cachethis) ?
4288                         session->se_fchannel.    4024                         session->se_fchannel.maxresp_cached :
4289                         session->se_fchannel.    4025                         session->se_fchannel.maxresp_sz;
4290         status = (seq->cachethis) ? nfserr_re    4026         status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
4291                                     nfserr_re    4027                                     nfserr_rep_too_big;
4292         if (xdr_restrict_buflen(xdr, buflen -    4028         if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
4293                 goto out_put_session;            4029                 goto out_put_session;
4294         svc_reserve(rqstp, buflen);              4030         svc_reserve(rqstp, buflen);
4295                                                  4031 
4296         status = nfs_ok;                         4032         status = nfs_ok;
4297         /* Success! bump slot seqid */           4033         /* Success! bump slot seqid */
4298         slot->sl_seqid = seq->seqid;             4034         slot->sl_seqid = seq->seqid;
4299         slot->sl_flags |= NFSD4_SLOT_INUSE;      4035         slot->sl_flags |= NFSD4_SLOT_INUSE;
4300         if (seq->cachethis)                      4036         if (seq->cachethis)
4301                 slot->sl_flags |= NFSD4_SLOT_    4037                 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
4302         else                                     4038         else
4303                 slot->sl_flags &= ~NFSD4_SLOT    4039                 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
4304                                                  4040 
4305         cstate->slot = slot;                     4041         cstate->slot = slot;
4306         cstate->session = session;               4042         cstate->session = session;
4307         cstate->clp = clp;                       4043         cstate->clp = clp;
4308                                                  4044 
4309 out:                                             4045 out:
4310         switch (clp->cl_cb_state) {              4046         switch (clp->cl_cb_state) {
4311         case NFSD4_CB_DOWN:                      4047         case NFSD4_CB_DOWN:
4312                 seq->status_flags = SEQ4_STAT    4048                 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
4313                 break;                           4049                 break;
4314         case NFSD4_CB_FAULT:                     4050         case NFSD4_CB_FAULT:
4315                 seq->status_flags = SEQ4_STAT    4051                 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
4316                 break;                           4052                 break;
4317         default:                                 4053         default:
4318                 seq->status_flags = 0;           4054                 seq->status_flags = 0;
4319         }                                        4055         }
4320         if (!list_empty(&clp->cl_revoked))       4056         if (!list_empty(&clp->cl_revoked))
4321                 seq->status_flags |= SEQ4_STA    4057                 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
4322         if (atomic_read(&clp->cl_admin_revoke << 
4323                 seq->status_flags |= SEQ4_STA << 
4324         trace_nfsd_seq4_status(rqstp, seq);   << 
4325 out_no_session:                                  4058 out_no_session:
4326         if (conn)                                4059         if (conn)
4327                 free_conn(conn);                 4060                 free_conn(conn);
4328         spin_unlock(&nn->client_lock);           4061         spin_unlock(&nn->client_lock);
4329         return status;                           4062         return status;
4330 out_put_session:                                 4063 out_put_session:
4331         nfsd4_put_session_locked(session);       4064         nfsd4_put_session_locked(session);
4332         goto out_no_session;                     4065         goto out_no_session;
4333 }                                                4066 }
4334                                                  4067 
4335 void                                             4068 void
4336 nfsd4_sequence_done(struct nfsd4_compoundres     4069 nfsd4_sequence_done(struct nfsd4_compoundres *resp)
4337 {                                                4070 {
4338         struct nfsd4_compound_state *cs = &re    4071         struct nfsd4_compound_state *cs = &resp->cstate;
4339                                                  4072 
4340         if (nfsd4_has_session(cs)) {             4073         if (nfsd4_has_session(cs)) {
4341                 if (cs->status != nfserr_repl    4074                 if (cs->status != nfserr_replay_cache) {
4342                         nfsd4_store_cache_ent    4075                         nfsd4_store_cache_entry(resp);
4343                         cs->slot->sl_flags &=    4076                         cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
4344                 }                                4077                 }
4345                 /* Drop session reference tha    4078                 /* Drop session reference that was taken in nfsd4_sequence() */
4346                 nfsd4_put_session(cs->session    4079                 nfsd4_put_session(cs->session);
4347         } else if (cs->clp)                      4080         } else if (cs->clp)
4348                 put_client_renew(cs->clp);       4081                 put_client_renew(cs->clp);
4349 }                                                4082 }
4350                                                  4083 
4351 __be32                                           4084 __be32
4352 nfsd4_destroy_clientid(struct svc_rqst *rqstp    4085 nfsd4_destroy_clientid(struct svc_rqst *rqstp,
4353                 struct nfsd4_compound_state *    4086                 struct nfsd4_compound_state *cstate,
4354                 union nfsd4_op_u *u)             4087                 union nfsd4_op_u *u)
4355 {                                                4088 {
4356         struct nfsd4_destroy_clientid *dc = &    4089         struct nfsd4_destroy_clientid *dc = &u->destroy_clientid;
4357         struct nfs4_client *conf, *unconf;       4090         struct nfs4_client *conf, *unconf;
4358         struct nfs4_client *clp = NULL;          4091         struct nfs4_client *clp = NULL;
4359         __be32 status = 0;                       4092         __be32 status = 0;
4360         struct nfsd_net *nn = net_generic(SVC    4093         struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4361                                                  4094 
4362         spin_lock(&nn->client_lock);             4095         spin_lock(&nn->client_lock);
4363         unconf = find_unconfirmed_client(&dc-    4096         unconf = find_unconfirmed_client(&dc->clientid, true, nn);
4364         conf = find_confirmed_client(&dc->cli    4097         conf = find_confirmed_client(&dc->clientid, true, nn);
4365         WARN_ON_ONCE(conf && unconf);            4098         WARN_ON_ONCE(conf && unconf);
4366                                                  4099 
4367         if (conf) {                              4100         if (conf) {
4368                 if (client_has_state(conf)) {    4101                 if (client_has_state(conf)) {
4369                         status = nfserr_clien    4102                         status = nfserr_clientid_busy;
4370                         goto out;                4103                         goto out;
4371                 }                                4104                 }
4372                 status = mark_client_expired_    4105                 status = mark_client_expired_locked(conf);
4373                 if (status)                      4106                 if (status)
4374                         goto out;                4107                         goto out;
4375                 clp = conf;                      4108                 clp = conf;
4376         } else if (unconf)                       4109         } else if (unconf)
4377                 clp = unconf;                    4110                 clp = unconf;
4378         else {                                   4111         else {
4379                 status = nfserr_stale_clienti    4112                 status = nfserr_stale_clientid;
4380                 goto out;                        4113                 goto out;
4381         }                                        4114         }
4382         if (!nfsd4_mach_creds_match(clp, rqst    4115         if (!nfsd4_mach_creds_match(clp, rqstp)) {
4383                 clp = NULL;                      4116                 clp = NULL;
4384                 status = nfserr_wrong_cred;      4117                 status = nfserr_wrong_cred;
4385                 goto out;                        4118                 goto out;
4386         }                                        4119         }
4387         trace_nfsd_clid_destroyed(&clp->cl_cl    4120         trace_nfsd_clid_destroyed(&clp->cl_clientid);
4388         unhash_client_locked(clp);               4121         unhash_client_locked(clp);
4389 out:                                             4122 out:
4390         spin_unlock(&nn->client_lock);           4123         spin_unlock(&nn->client_lock);
4391         if (clp)                                 4124         if (clp)
4392                 expire_client(clp);              4125                 expire_client(clp);
4393         return status;                           4126         return status;
4394 }                                                4127 }
4395                                                  4128 
4396 __be32                                           4129 __be32
4397 nfsd4_reclaim_complete(struct svc_rqst *rqstp    4130 nfsd4_reclaim_complete(struct svc_rqst *rqstp,
4398                 struct nfsd4_compound_state *    4131                 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
4399 {                                                4132 {
4400         struct nfsd4_reclaim_complete *rc = &    4133         struct nfsd4_reclaim_complete *rc = &u->reclaim_complete;
4401         struct nfs4_client *clp = cstate->clp    4134         struct nfs4_client *clp = cstate->clp;
4402         __be32 status = 0;                       4135         __be32 status = 0;
4403                                                  4136 
4404         if (rc->rca_one_fs) {                    4137         if (rc->rca_one_fs) {
4405                 if (!cstate->current_fh.fh_de    4138                 if (!cstate->current_fh.fh_dentry)
4406                         return nfserr_nofileh    4139                         return nfserr_nofilehandle;
4407                 /*                               4140                 /*
4408                  * We don't take advantage of    4141                  * We don't take advantage of the rca_one_fs case.
4409                  * That's OK, it's optional,     4142                  * That's OK, it's optional, we can safely ignore it.
4410                  */                              4143                  */
4411                 return nfs_ok;                   4144                 return nfs_ok;
4412         }                                        4145         }
4413                                                  4146 
4414         status = nfserr_complete_already;        4147         status = nfserr_complete_already;
4415         if (test_and_set_bit(NFSD4_CLIENT_REC    4148         if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &clp->cl_flags))
4416                 goto out;                        4149                 goto out;
4417                                                  4150 
4418         status = nfserr_stale_clientid;          4151         status = nfserr_stale_clientid;
4419         if (is_client_expired(clp))              4152         if (is_client_expired(clp))
4420                 /*                               4153                 /*
4421                  * The following error isn't     4154                  * The following error isn't really legal.
4422                  * But we only get here if th    4155                  * But we only get here if the client just explicitly
4423                  * destroyed the client.  Sur    4156                  * destroyed the client.  Surely it no longer cares what
4424                  * error it gets back on an o    4157                  * error it gets back on an operation for the dead
4425                  * client.                       4158                  * client.
4426                  */                              4159                  */
4427                 goto out;                        4160                 goto out;
4428                                                  4161 
4429         status = nfs_ok;                         4162         status = nfs_ok;
4430         trace_nfsd_clid_reclaim_complete(&clp    4163         trace_nfsd_clid_reclaim_complete(&clp->cl_clientid);
4431         nfsd4_client_record_create(clp);         4164         nfsd4_client_record_create(clp);
4432         inc_reclaim_complete(clp);               4165         inc_reclaim_complete(clp);
4433 out:                                             4166 out:
4434         return status;                           4167         return status;
4435 }                                                4168 }
4436                                                  4169 
4437 __be32                                           4170 __be32
4438 nfsd4_setclientid(struct svc_rqst *rqstp, str    4171 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4439                   union nfsd4_op_u *u)           4172                   union nfsd4_op_u *u)
4440 {                                                4173 {
4441         struct nfsd4_setclientid *setclid = &    4174         struct nfsd4_setclientid *setclid = &u->setclientid;
4442         struct xdr_netobj       clname = setc    4175         struct xdr_netobj       clname = setclid->se_name;
4443         nfs4_verifier           clverifier =     4176         nfs4_verifier           clverifier = setclid->se_verf;
4444         struct nfs4_client      *conf, *new;     4177         struct nfs4_client      *conf, *new;
4445         struct nfs4_client      *unconf = NUL    4178         struct nfs4_client      *unconf = NULL;
4446         __be32                  status;          4179         __be32                  status;
4447         struct nfsd_net         *nn = net_gen    4180         struct nfsd_net         *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4448                                                  4181 
4449         new = create_client(clname, rqstp, &c    4182         new = create_client(clname, rqstp, &clverifier);
4450         if (new == NULL)                         4183         if (new == NULL)
4451                 return nfserr_jukebox;           4184                 return nfserr_jukebox;
4452         spin_lock(&nn->client_lock);             4185         spin_lock(&nn->client_lock);
4453         conf = find_confirmed_client_by_name(    4186         conf = find_confirmed_client_by_name(&clname, nn);
4454         if (conf && client_has_state(conf)) {    4187         if (conf && client_has_state(conf)) {
4455                 status = nfserr_clid_inuse;      4188                 status = nfserr_clid_inuse;
4456                 if (clp_used_exchangeid(conf)    4189                 if (clp_used_exchangeid(conf))
4457                         goto out;                4190                         goto out;
4458                 if (!same_creds(&conf->cl_cre    4191                 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
4459                         trace_nfsd_clid_cred_    4192                         trace_nfsd_clid_cred_mismatch(conf, rqstp);
4460                         goto out;                4193                         goto out;
4461                 }                                4194                 }
4462         }                                        4195         }
4463         unconf = find_unconfirmed_client_by_n    4196         unconf = find_unconfirmed_client_by_name(&clname, nn);
4464         if (unconf)                              4197         if (unconf)
4465                 unhash_client_locked(unconf);    4198                 unhash_client_locked(unconf);
4466         if (conf) {                              4199         if (conf) {
4467                 if (same_verf(&conf->cl_verif    4200                 if (same_verf(&conf->cl_verifier, &clverifier)) {
4468                         copy_clid(new, conf);    4201                         copy_clid(new, conf);
4469                         gen_confirm(new, nn);    4202                         gen_confirm(new, nn);
4470                 } else                           4203                 } else
4471                         trace_nfsd_clid_verf_    4204                         trace_nfsd_clid_verf_mismatch(conf, rqstp,
4472                                                  4205                                                       &clverifier);
4473         } else                                   4206         } else
4474                 trace_nfsd_clid_fresh(new);      4207                 trace_nfsd_clid_fresh(new);
4475         new->cl_minorversion = 0;                4208         new->cl_minorversion = 0;
4476         gen_callback(new, setclid, rqstp);       4209         gen_callback(new, setclid, rqstp);
4477         add_to_unconfirmed(new);                 4210         add_to_unconfirmed(new);
4478         setclid->se_clientid.cl_boot = new->c    4211         setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
4479         setclid->se_clientid.cl_id = new->cl_    4212         setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
4480         memcpy(setclid->se_confirm.data, new-    4213         memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
4481         new = NULL;                              4214         new = NULL;
4482         status = nfs_ok;                         4215         status = nfs_ok;
4483 out:                                             4216 out:
4484         spin_unlock(&nn->client_lock);           4217         spin_unlock(&nn->client_lock);
4485         if (new)                                 4218         if (new)
4486                 free_client(new);                4219                 free_client(new);
4487         if (unconf) {                            4220         if (unconf) {
4488                 trace_nfsd_clid_expire_unconf    4221                 trace_nfsd_clid_expire_unconf(&unconf->cl_clientid);
4489                 expire_client(unconf);           4222                 expire_client(unconf);
4490         }                                        4223         }
4491         return status;                           4224         return status;
4492 }                                                4225 }
4493                                                  4226 
4494 __be32                                           4227 __be32
4495 nfsd4_setclientid_confirm(struct svc_rqst *rq    4228 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
4496                         struct nfsd4_compound    4229                         struct nfsd4_compound_state *cstate,
4497                         union nfsd4_op_u *u)     4230                         union nfsd4_op_u *u)
4498 {                                                4231 {
4499         struct nfsd4_setclientid_confirm *set    4232         struct nfsd4_setclientid_confirm *setclientid_confirm =
4500                         &u->setclientid_confi    4233                         &u->setclientid_confirm;
4501         struct nfs4_client *conf, *unconf;       4234         struct nfs4_client *conf, *unconf;
4502         struct nfs4_client *old = NULL;          4235         struct nfs4_client *old = NULL;
4503         nfs4_verifier confirm = setclientid_c    4236         nfs4_verifier confirm = setclientid_confirm->sc_confirm; 
4504         clientid_t * clid = &setclientid_conf    4237         clientid_t * clid = &setclientid_confirm->sc_clientid;
4505         __be32 status;                           4238         __be32 status;
4506         struct nfsd_net *nn = net_generic(SVC    4239         struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4507                                                  4240 
4508         if (STALE_CLIENTID(clid, nn))            4241         if (STALE_CLIENTID(clid, nn))
4509                 return nfserr_stale_clientid;    4242                 return nfserr_stale_clientid;
4510                                                  4243 
4511         spin_lock(&nn->client_lock);             4244         spin_lock(&nn->client_lock);
4512         conf = find_confirmed_client(clid, fa    4245         conf = find_confirmed_client(clid, false, nn);
4513         unconf = find_unconfirmed_client(clid    4246         unconf = find_unconfirmed_client(clid, false, nn);
4514         /*                                       4247         /*
4515          * We try hard to give out unique cli    4248          * We try hard to give out unique clientid's, so if we get an
4516          * attempt to confirm the same client    4249          * attempt to confirm the same clientid with a different cred,
4517          * the client may be buggy; this shou    4250          * the client may be buggy; this should never happen.
4518          *                                       4251          *
4519          * Nevertheless, RFC 7530 recommends     4252          * Nevertheless, RFC 7530 recommends INUSE for this case:
4520          */                                      4253          */
4521         status = nfserr_clid_inuse;              4254         status = nfserr_clid_inuse;
4522         if (unconf && !same_creds(&unconf->cl    4255         if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
4523                 trace_nfsd_clid_cred_mismatch    4256                 trace_nfsd_clid_cred_mismatch(unconf, rqstp);
4524                 goto out;                        4257                 goto out;
4525         }                                        4258         }
4526         if (conf && !same_creds(&conf->cl_cre    4259         if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
4527                 trace_nfsd_clid_cred_mismatch    4260                 trace_nfsd_clid_cred_mismatch(conf, rqstp);
4528                 goto out;                        4261                 goto out;
4529         }                                        4262         }
4530         if (!unconf || !same_verf(&confirm, &    4263         if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
4531                 if (conf && same_verf(&confir    4264                 if (conf && same_verf(&confirm, &conf->cl_confirm)) {
4532                         status = nfs_ok;         4265                         status = nfs_ok;
4533                 } else                           4266                 } else
4534                         status = nfserr_stale    4267                         status = nfserr_stale_clientid;
4535                 goto out;                        4268                 goto out;
4536         }                                        4269         }
4537         status = nfs_ok;                         4270         status = nfs_ok;
4538         if (conf) {                              4271         if (conf) {
4539                 old = unconf;                    4272                 old = unconf;
4540                 unhash_client_locked(old);       4273                 unhash_client_locked(old);
4541                 nfsd4_change_callback(conf, &    4274                 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
4542         } else {                                 4275         } else {
4543                 old = find_confirmed_client_b    4276                 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
4544                 if (old) {                       4277                 if (old) {
4545                         status = nfserr_clid_    4278                         status = nfserr_clid_inuse;
4546                         if (client_has_state(    4279                         if (client_has_state(old)
4547                                         && !s    4280                                         && !same_creds(&unconf->cl_cred,
4548                                                  4281                                                         &old->cl_cred)) {
4549                                 old = NULL;      4282                                 old = NULL;
4550                                 goto out;        4283                                 goto out;
4551                         }                        4284                         }
4552                         status = mark_client_    4285                         status = mark_client_expired_locked(old);
4553                         if (status) {            4286                         if (status) {
4554                                 old = NULL;      4287                                 old = NULL;
4555                                 goto out;        4288                                 goto out;
4556                         }                        4289                         }
4557                         trace_nfsd_clid_repla    4290                         trace_nfsd_clid_replaced(&old->cl_clientid);
4558                 }                                4291                 }
4559                 move_to_confirmed(unconf);       4292                 move_to_confirmed(unconf);
4560                 conf = unconf;                   4293                 conf = unconf;
4561         }                                        4294         }
4562         get_client_locked(conf);                 4295         get_client_locked(conf);
4563         spin_unlock(&nn->client_lock);           4296         spin_unlock(&nn->client_lock);
4564         if (conf == unconf)                      4297         if (conf == unconf)
4565                 fsnotify_dentry(conf->cl_nfsd    4298                 fsnotify_dentry(conf->cl_nfsd_info_dentry, FS_MODIFY);
4566         nfsd4_probe_callback(conf);              4299         nfsd4_probe_callback(conf);
4567         spin_lock(&nn->client_lock);             4300         spin_lock(&nn->client_lock);
4568         put_client_renew_locked(conf);           4301         put_client_renew_locked(conf);
4569 out:                                             4302 out:
4570         spin_unlock(&nn->client_lock);           4303         spin_unlock(&nn->client_lock);
4571         if (old)                                 4304         if (old)
4572                 expire_client(old);              4305                 expire_client(old);
4573         return status;                           4306         return status;
4574 }                                                4307 }
4575                                                  4308 
4576 static struct nfs4_file *nfsd4_alloc_file(voi    4309 static struct nfs4_file *nfsd4_alloc_file(void)
4577 {                                                4310 {
4578         return kmem_cache_alloc(file_slab, GF    4311         return kmem_cache_alloc(file_slab, GFP_KERNEL);
4579 }                                                4312 }
4580                                                  4313 
4581 /* OPEN Share state helper functions */          4314 /* OPEN Share state helper functions */
4582                                                  4315 
4583 static void nfsd4_file_init(const struct svc_    4316 static void nfsd4_file_init(const struct svc_fh *fh, struct nfs4_file *fp)
4584 {                                                4317 {
4585         refcount_set(&fp->fi_ref, 1);            4318         refcount_set(&fp->fi_ref, 1);
4586         spin_lock_init(&fp->fi_lock);            4319         spin_lock_init(&fp->fi_lock);
4587         INIT_LIST_HEAD(&fp->fi_stateids);        4320         INIT_LIST_HEAD(&fp->fi_stateids);
4588         INIT_LIST_HEAD(&fp->fi_delegations);     4321         INIT_LIST_HEAD(&fp->fi_delegations);
4589         INIT_LIST_HEAD(&fp->fi_clnt_odstate);    4322         INIT_LIST_HEAD(&fp->fi_clnt_odstate);
4590         fh_copy_shallow(&fp->fi_fhandle, &fh-    4323         fh_copy_shallow(&fp->fi_fhandle, &fh->fh_handle);
4591         fp->fi_deleg_file = NULL;                4324         fp->fi_deleg_file = NULL;
4592         fp->fi_had_conflict = false;             4325         fp->fi_had_conflict = false;
4593         fp->fi_share_deny = 0;                   4326         fp->fi_share_deny = 0;
4594         memset(fp->fi_fds, 0, sizeof(fp->fi_f    4327         memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
4595         memset(fp->fi_access, 0, sizeof(fp->f    4328         memset(fp->fi_access, 0, sizeof(fp->fi_access));
4596         fp->fi_aliased = false;                  4329         fp->fi_aliased = false;
4597         fp->fi_inode = d_inode(fh->fh_dentry)    4330         fp->fi_inode = d_inode(fh->fh_dentry);
4598 #ifdef CONFIG_NFSD_PNFS                          4331 #ifdef CONFIG_NFSD_PNFS
4599         INIT_LIST_HEAD(&fp->fi_lo_states);       4332         INIT_LIST_HEAD(&fp->fi_lo_states);
4600         atomic_set(&fp->fi_lo_recalls, 0);       4333         atomic_set(&fp->fi_lo_recalls, 0);
4601 #endif                                           4334 #endif
4602 }                                                4335 }
4603                                                  4336 
4604 void                                             4337 void
4605 nfsd4_free_slabs(void)                           4338 nfsd4_free_slabs(void)
4606 {                                                4339 {
4607         kmem_cache_destroy(client_slab);         4340         kmem_cache_destroy(client_slab);
4608         kmem_cache_destroy(openowner_slab);      4341         kmem_cache_destroy(openowner_slab);
4609         kmem_cache_destroy(lockowner_slab);      4342         kmem_cache_destroy(lockowner_slab);
4610         kmem_cache_destroy(file_slab);           4343         kmem_cache_destroy(file_slab);
4611         kmem_cache_destroy(stateid_slab);        4344         kmem_cache_destroy(stateid_slab);
4612         kmem_cache_destroy(deleg_slab);          4345         kmem_cache_destroy(deleg_slab);
4613         kmem_cache_destroy(odstate_slab);        4346         kmem_cache_destroy(odstate_slab);
4614 }                                                4347 }
4615                                                  4348 
4616 int                                              4349 int
4617 nfsd4_init_slabs(void)                           4350 nfsd4_init_slabs(void)
4618 {                                                4351 {
4619         client_slab = KMEM_CACHE(nfs4_client, !! 4352         client_slab = kmem_cache_create("nfsd4_clients",
                                                   >> 4353                         sizeof(struct nfs4_client), 0, 0, NULL);
4620         if (client_slab == NULL)                 4354         if (client_slab == NULL)
4621                 goto out;                        4355                 goto out;
4622         openowner_slab = KMEM_CACHE(nfs4_open !! 4356         openowner_slab = kmem_cache_create("nfsd4_openowners",
                                                   >> 4357                         sizeof(struct nfs4_openowner), 0, 0, NULL);
4623         if (openowner_slab == NULL)              4358         if (openowner_slab == NULL)
4624                 goto out_free_client_slab;       4359                 goto out_free_client_slab;
4625         lockowner_slab = KMEM_CACHE(nfs4_lock !! 4360         lockowner_slab = kmem_cache_create("nfsd4_lockowners",
                                                   >> 4361                         sizeof(struct nfs4_lockowner), 0, 0, NULL);
4626         if (lockowner_slab == NULL)              4362         if (lockowner_slab == NULL)
4627                 goto out_free_openowner_slab;    4363                 goto out_free_openowner_slab;
4628         file_slab = KMEM_CACHE(nfs4_file, 0); !! 4364         file_slab = kmem_cache_create("nfsd4_files",
                                                   >> 4365                         sizeof(struct nfs4_file), 0, 0, NULL);
4629         if (file_slab == NULL)                   4366         if (file_slab == NULL)
4630                 goto out_free_lockowner_slab;    4367                 goto out_free_lockowner_slab;
4631         stateid_slab = KMEM_CACHE(nfs4_ol_sta !! 4368         stateid_slab = kmem_cache_create("nfsd4_stateids",
                                                   >> 4369                         sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
4632         if (stateid_slab == NULL)                4370         if (stateid_slab == NULL)
4633                 goto out_free_file_slab;         4371                 goto out_free_file_slab;
4634         deleg_slab = KMEM_CACHE(nfs4_delegati !! 4372         deleg_slab = kmem_cache_create("nfsd4_delegations",
                                                   >> 4373                         sizeof(struct nfs4_delegation), 0, 0, NULL);
4635         if (deleg_slab == NULL)                  4374         if (deleg_slab == NULL)
4636                 goto out_free_stateid_slab;      4375                 goto out_free_stateid_slab;
4637         odstate_slab = KMEM_CACHE(nfs4_clnt_o !! 4376         odstate_slab = kmem_cache_create("nfsd4_odstate",
                                                   >> 4377                         sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
4638         if (odstate_slab == NULL)                4378         if (odstate_slab == NULL)
4639                 goto out_free_deleg_slab;        4379                 goto out_free_deleg_slab;
4640         return 0;                                4380         return 0;
4641                                                  4381 
4642 out_free_deleg_slab:                             4382 out_free_deleg_slab:
4643         kmem_cache_destroy(deleg_slab);          4383         kmem_cache_destroy(deleg_slab);
4644 out_free_stateid_slab:                           4384 out_free_stateid_slab:
4645         kmem_cache_destroy(stateid_slab);        4385         kmem_cache_destroy(stateid_slab);
4646 out_free_file_slab:                              4386 out_free_file_slab:
4647         kmem_cache_destroy(file_slab);           4387         kmem_cache_destroy(file_slab);
4648 out_free_lockowner_slab:                         4388 out_free_lockowner_slab:
4649         kmem_cache_destroy(lockowner_slab);      4389         kmem_cache_destroy(lockowner_slab);
4650 out_free_openowner_slab:                         4390 out_free_openowner_slab:
4651         kmem_cache_destroy(openowner_slab);      4391         kmem_cache_destroy(openowner_slab);
4652 out_free_client_slab:                            4392 out_free_client_slab:
4653         kmem_cache_destroy(client_slab);         4393         kmem_cache_destroy(client_slab);
4654 out:                                             4394 out:
4655         return -ENOMEM;                          4395         return -ENOMEM;
4656 }                                                4396 }
4657                                                  4397 
4658 static unsigned long                             4398 static unsigned long
4659 nfsd4_state_shrinker_count(struct shrinker *s    4399 nfsd4_state_shrinker_count(struct shrinker *shrink, struct shrink_control *sc)
4660 {                                                4400 {
4661         int count;                               4401         int count;
4662         struct nfsd_net *nn = shrink->private    4402         struct nfsd_net *nn = shrink->private_data;
4663                                                  4403 
4664         count = atomic_read(&nn->nfsd_courtes    4404         count = atomic_read(&nn->nfsd_courtesy_clients);
4665         if (!count)                              4405         if (!count)
4666                 count = atomic_long_read(&num    4406                 count = atomic_long_read(&num_delegations);
4667         if (count)                               4407         if (count)
4668                 queue_work(laundry_wq, &nn->n    4408                 queue_work(laundry_wq, &nn->nfsd_shrinker_work);
4669         return (unsigned long)count;             4409         return (unsigned long)count;
4670 }                                                4410 }
4671                                                  4411 
4672 static unsigned long                             4412 static unsigned long
4673 nfsd4_state_shrinker_scan(struct shrinker *sh    4413 nfsd4_state_shrinker_scan(struct shrinker *shrink, struct shrink_control *sc)
4674 {                                                4414 {
4675         return SHRINK_STOP;                      4415         return SHRINK_STOP;
4676 }                                                4416 }
4677                                                  4417 
4678 void                                             4418 void
4679 nfsd4_init_leases_net(struct nfsd_net *nn)       4419 nfsd4_init_leases_net(struct nfsd_net *nn)
4680 {                                                4420 {
4681         struct sysinfo si;                       4421         struct sysinfo si;
4682         u64 max_clients;                         4422         u64 max_clients;
4683                                                  4423 
4684         nn->nfsd4_lease = 90;   /* default le    4424         nn->nfsd4_lease = 90;   /* default lease time */
4685         nn->nfsd4_grace = 90;                    4425         nn->nfsd4_grace = 90;
4686         nn->somebody_reclaimed = false;          4426         nn->somebody_reclaimed = false;
4687         nn->track_reclaim_completes = false;     4427         nn->track_reclaim_completes = false;
4688         nn->clverifier_counter = get_random_u    4428         nn->clverifier_counter = get_random_u32();
4689         nn->clientid_base = get_random_u32();    4429         nn->clientid_base = get_random_u32();
4690         nn->clientid_counter = nn->clientid_b    4430         nn->clientid_counter = nn->clientid_base + 1;
4691         nn->s2s_cp_cl_id = nn->clientid_count    4431         nn->s2s_cp_cl_id = nn->clientid_counter++;
4692                                                  4432 
4693         atomic_set(&nn->nfs4_client_count, 0)    4433         atomic_set(&nn->nfs4_client_count, 0);
4694         si_meminfo(&si);                         4434         si_meminfo(&si);
4695         max_clients = (u64)si.totalram * si.m    4435         max_clients = (u64)si.totalram * si.mem_unit / (1024 * 1024 * 1024);
4696         max_clients *= NFS4_CLIENTS_PER_GB;      4436         max_clients *= NFS4_CLIENTS_PER_GB;
4697         nn->nfs4_max_clients = max_t(int, max    4437         nn->nfs4_max_clients = max_t(int, max_clients, NFS4_CLIENTS_PER_GB);
4698                                                  4438 
4699         atomic_set(&nn->nfsd_courtesy_clients    4439         atomic_set(&nn->nfsd_courtesy_clients, 0);
4700 }                                                4440 }
4701                                                  4441 
4702 enum rp_lock {                                << 
4703         RP_UNLOCKED,                          << 
4704         RP_LOCKED,                            << 
4705         RP_UNHASHED,                          << 
4706 };                                            << 
4707                                               << 
4708 static void init_nfs4_replay(struct nfs4_repl    4442 static void init_nfs4_replay(struct nfs4_replay *rp)
4709 {                                                4443 {
4710         rp->rp_status = nfserr_serverfault;      4444         rp->rp_status = nfserr_serverfault;
4711         rp->rp_buflen = 0;                       4445         rp->rp_buflen = 0;
4712         rp->rp_buf = rp->rp_ibuf;                4446         rp->rp_buf = rp->rp_ibuf;
4713         atomic_set(&rp->rp_locked, RP_UNLOCKE !! 4447         mutex_init(&rp->rp_mutex);
4714 }                                                4448 }
4715                                                  4449 
4716 static int nfsd4_cstate_assign_replay(struct  !! 4450 static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
4717                                       struct  !! 4451                 struct nfs4_stateowner *so)
4718 {                                                4452 {
4719         if (!nfsd4_has_session(cstate)) {        4453         if (!nfsd4_has_session(cstate)) {
4720                 wait_var_event(&so->so_replay !! 4454                 mutex_lock(&so->so_replay.rp_mutex);
4721                                atomic_cmpxchg << 
4722                                               << 
4723                 if (atomic_read(&so->so_repla << 
4724                         return -EAGAIN;       << 
4725                 cstate->replay_owner = nfs4_g    4455                 cstate->replay_owner = nfs4_get_stateowner(so);
4726         }                                        4456         }
4727         return 0;                             << 
4728 }                                                4457 }
4729                                                  4458 
4730 void nfsd4_cstate_clear_replay(struct nfsd4_c    4459 void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
4731 {                                                4460 {
4732         struct nfs4_stateowner *so = cstate->    4461         struct nfs4_stateowner *so = cstate->replay_owner;
4733                                                  4462 
4734         if (so != NULL) {                        4463         if (so != NULL) {
4735                 cstate->replay_owner = NULL;     4464                 cstate->replay_owner = NULL;
4736                 atomic_set(&so->so_replay.rp_ !! 4465                 mutex_unlock(&so->so_replay.rp_mutex);
4737                 smp_mb__after_atomic();       << 
4738                 wake_up_var(&so->so_replay.rp << 
4739                 nfs4_put_stateowner(so);         4466                 nfs4_put_stateowner(so);
4740         }                                        4467         }
4741 }                                                4468 }
4742                                                  4469 
4743 static inline void *alloc_stateowner(struct k    4470 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
4744 {                                                4471 {
4745         struct nfs4_stateowner *sop;             4472         struct nfs4_stateowner *sop;
4746                                                  4473 
4747         sop = kmem_cache_alloc(slab, GFP_KERN    4474         sop = kmem_cache_alloc(slab, GFP_KERNEL);
4748         if (!sop)                                4475         if (!sop)
4749                 return NULL;                     4476                 return NULL;
4750                                                  4477 
4751         xdr_netobj_dup(&sop->so_owner, owner,    4478         xdr_netobj_dup(&sop->so_owner, owner, GFP_KERNEL);
4752         if (!sop->so_owner.data) {               4479         if (!sop->so_owner.data) {
4753                 kmem_cache_free(slab, sop);      4480                 kmem_cache_free(slab, sop);
4754                 return NULL;                     4481                 return NULL;
4755         }                                        4482         }
4756                                                  4483 
4757         INIT_LIST_HEAD(&sop->so_stateids);       4484         INIT_LIST_HEAD(&sop->so_stateids);
4758         sop->so_client = clp;                    4485         sop->so_client = clp;
4759         init_nfs4_replay(&sop->so_replay);       4486         init_nfs4_replay(&sop->so_replay);
4760         atomic_set(&sop->so_count, 1);           4487         atomic_set(&sop->so_count, 1);
4761         return sop;                              4488         return sop;
4762 }                                                4489 }
4763                                                  4490 
4764 static void hash_openowner(struct nfs4_openow    4491 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
4765 {                                                4492 {
4766         lockdep_assert_held(&clp->cl_lock);      4493         lockdep_assert_held(&clp->cl_lock);
4767                                                  4494 
4768         list_add(&oo->oo_owner.so_strhash,       4495         list_add(&oo->oo_owner.so_strhash,
4769                  &clp->cl_ownerstr_hashtbl[st    4496                  &clp->cl_ownerstr_hashtbl[strhashval]);
4770         list_add(&oo->oo_perclient, &clp->cl_    4497         list_add(&oo->oo_perclient, &clp->cl_openowners);
4771 }                                                4498 }
4772                                                  4499 
4773 static void nfs4_unhash_openowner(struct nfs4    4500 static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
4774 {                                                4501 {
4775         unhash_openowner_locked(openowner(so)    4502         unhash_openowner_locked(openowner(so));
4776 }                                                4503 }
4777                                                  4504 
4778 static void nfs4_free_openowner(struct nfs4_s    4505 static void nfs4_free_openowner(struct nfs4_stateowner *so)
4779 {                                                4506 {
4780         struct nfs4_openowner *oo = openowner    4507         struct nfs4_openowner *oo = openowner(so);
4781                                                  4508 
4782         kmem_cache_free(openowner_slab, oo);     4509         kmem_cache_free(openowner_slab, oo);
4783 }                                                4510 }
4784                                                  4511 
4785 static const struct nfs4_stateowner_operation    4512 static const struct nfs4_stateowner_operations openowner_ops = {
4786         .so_unhash =    nfs4_unhash_openowner    4513         .so_unhash =    nfs4_unhash_openowner,
4787         .so_free =      nfs4_free_openowner,     4514         .so_free =      nfs4_free_openowner,
4788 };                                               4515 };
4789                                                  4516 
4790 static struct nfs4_ol_stateid *                  4517 static struct nfs4_ol_stateid *
4791 nfsd4_find_existing_open(struct nfs4_file *fp    4518 nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4792 {                                                4519 {
4793         struct nfs4_ol_stateid *local, *ret =    4520         struct nfs4_ol_stateid *local, *ret = NULL;
4794         struct nfs4_openowner *oo = open->op_    4521         struct nfs4_openowner *oo = open->op_openowner;
4795                                                  4522 
4796         lockdep_assert_held(&fp->fi_lock);       4523         lockdep_assert_held(&fp->fi_lock);
4797                                                  4524 
4798         list_for_each_entry(local, &fp->fi_st    4525         list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
4799                 /* ignore lock owners */         4526                 /* ignore lock owners */
4800                 if (local->st_stateowner->so_    4527                 if (local->st_stateowner->so_is_open_owner == 0)
4801                         continue;                4528                         continue;
4802                 if (local->st_stateowner != &    4529                 if (local->st_stateowner != &oo->oo_owner)
4803                         continue;                4530                         continue;
4804                 if (local->st_stid.sc_type == !! 4531                 if (local->st_stid.sc_type == NFS4_OPEN_STID) {
4805                     !local->st_stid.sc_status << 
4806                         ret = local;             4532                         ret = local;
4807                         refcount_inc(&ret->st    4533                         refcount_inc(&ret->st_stid.sc_count);
4808                         break;                   4534                         break;
4809                 }                                4535                 }
4810         }                                        4536         }
4811         return ret;                              4537         return ret;
4812 }                                                4538 }
4813                                                  4539 
4814 static void nfsd4_drop_revoked_stid(struct nf << 
4815         __releases(&s->sc_client->cl_lock)    << 
4816 {                                             << 
4817         struct nfs4_client *cl = s->sc_client << 
4818         LIST_HEAD(reaplist);                  << 
4819         struct nfs4_ol_stateid *stp;          << 
4820         struct nfs4_delegation *dp;           << 
4821         bool unhashed;                        << 
4822                                               << 
4823         switch (s->sc_type) {                 << 
4824         case SC_TYPE_OPEN:                    << 
4825                 stp = openlockstateid(s);     << 
4826                 if (unhash_open_stateid(stp,  << 
4827                         put_ol_stateid_locked << 
4828                 spin_unlock(&cl->cl_lock);    << 
4829                 free_ol_stateid_reaplist(&rea << 
4830                 break;                        << 
4831         case SC_TYPE_LOCK:                    << 
4832                 stp = openlockstateid(s);     << 
4833                 unhashed = unhash_lock_statei << 
4834                 spin_unlock(&cl->cl_lock);    << 
4835                 if (unhashed)                 << 
4836                         nfs4_put_stid(s);     << 
4837                 break;                        << 
4838         case SC_TYPE_DELEG:                   << 
4839                 dp = delegstateid(s);         << 
4840                 list_del_init(&dp->dl_recall_ << 
4841                 spin_unlock(&cl->cl_lock);    << 
4842                 nfs4_put_stid(s);             << 
4843                 break;                        << 
4844         default:                              << 
4845                 spin_unlock(&cl->cl_lock);    << 
4846         }                                     << 
4847 }                                             << 
4848                                               << 
4849 static void nfsd40_drop_revoked_stid(struct n << 
4850                                     stateid_t << 
4851 {                                             << 
4852         /* NFSv4.0 has no way for the client  << 
4853          * that it can forget an admin-revoke << 
4854          * So we keep it around until the fir << 
4855          * client uses it, and drop it the fi << 
4856          * nfserr_admin_revoked is returned.  << 
4857          * For v4.1 and later we wait until e << 
4858          * to free the stateid.               << 
4859          */                                   << 
4860         if (cl->cl_minorversion == 0) {       << 
4861                 struct nfs4_stid *st;         << 
4862                                               << 
4863                 spin_lock(&cl->cl_lock);      << 
4864                 st = find_stateid_locked(cl,  << 
4865                 if (st)                       << 
4866                         nfsd4_drop_revoked_st << 
4867                 else                          << 
4868                         spin_unlock(&cl->cl_l << 
4869         }                                     << 
4870 }                                             << 
4871                                               << 
4872 static __be32                                    4540 static __be32
4873 nfsd4_verify_open_stid(struct nfs4_stid *s)      4541 nfsd4_verify_open_stid(struct nfs4_stid *s)
4874 {                                                4542 {
4875         __be32 ret = nfs_ok;                     4543         __be32 ret = nfs_ok;
4876                                                  4544 
4877         if (s->sc_status & SC_STATUS_ADMIN_RE !! 4545         switch (s->sc_type) {
4878                 ret = nfserr_admin_revoked;   !! 4546         default:
4879         else if (s->sc_status & SC_STATUS_REV !! 4547                 break;
4880                 ret = nfserr_deleg_revoked;   !! 4548         case 0:
4881         else if (s->sc_status & SC_STATUS_CLO !! 4549         case NFS4_CLOSED_STID:
                                                   >> 4550         case NFS4_CLOSED_DELEG_STID:
4882                 ret = nfserr_bad_stateid;        4551                 ret = nfserr_bad_stateid;
                                                   >> 4552                 break;
                                                   >> 4553         case NFS4_REVOKED_DELEG_STID:
                                                   >> 4554                 ret = nfserr_deleg_revoked;
                                                   >> 4555         }
4883         return ret;                              4556         return ret;
4884 }                                                4557 }
4885                                                  4558 
4886 /* Lock the stateid st_mutex, and deal with r    4559 /* Lock the stateid st_mutex, and deal with races with CLOSE */
4887 static __be32                                    4560 static __be32
4888 nfsd4_lock_ol_stateid(struct nfs4_ol_stateid     4561 nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp)
4889 {                                                4562 {
4890         __be32 ret;                              4563         __be32 ret;
4891                                                  4564 
4892         mutex_lock_nested(&stp->st_mutex, LOC    4565         mutex_lock_nested(&stp->st_mutex, LOCK_STATEID_MUTEX);
4893         ret = nfsd4_verify_open_stid(&stp->st    4566         ret = nfsd4_verify_open_stid(&stp->st_stid);
4894         if (ret == nfserr_admin_revoked)      << 
4895                 nfsd40_drop_revoked_stid(stp- << 
4896                                         &stp- << 
4897                                               << 
4898         if (ret != nfs_ok)                       4567         if (ret != nfs_ok)
4899                 mutex_unlock(&stp->st_mutex);    4568                 mutex_unlock(&stp->st_mutex);
4900         return ret;                              4569         return ret;
4901 }                                                4570 }
4902                                                  4571 
4903 static struct nfs4_ol_stateid *                  4572 static struct nfs4_ol_stateid *
4904 nfsd4_find_and_lock_existing_open(struct nfs4    4573 nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4905 {                                                4574 {
4906         struct nfs4_ol_stateid *stp;             4575         struct nfs4_ol_stateid *stp;
4907         for (;;) {                               4576         for (;;) {
4908                 spin_lock(&fp->fi_lock);         4577                 spin_lock(&fp->fi_lock);
4909                 stp = nfsd4_find_existing_ope    4578                 stp = nfsd4_find_existing_open(fp, open);
4910                 spin_unlock(&fp->fi_lock);       4579                 spin_unlock(&fp->fi_lock);
4911                 if (!stp || nfsd4_lock_ol_sta    4580                 if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok)
4912                         break;                   4581                         break;
4913                 nfs4_put_stid(&stp->st_stid);    4582                 nfs4_put_stid(&stp->st_stid);
4914         }                                        4583         }
4915         return stp;                              4584         return stp;
4916 }                                                4585 }
4917                                                  4586 
4918 static struct nfs4_openowner *                   4587 static struct nfs4_openowner *
4919 find_or_alloc_open_stateowner(unsigned int st !! 4588 alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
4920                               struct nfsd4_co !! 4589                            struct nfsd4_compound_state *cstate)
4921 {                                                4590 {
4922         struct nfs4_client *clp = cstate->clp    4591         struct nfs4_client *clp = cstate->clp;
4923         struct nfs4_openowner *oo, *new = NUL !! 4592         struct nfs4_openowner *oo, *ret;
4924                                                  4593 
4925 retry:                                        !! 4594         oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
4926         spin_lock(&clp->cl_lock);             !! 4595         if (!oo)
4927         oo = find_openstateowner_str(strhashv << 
4928         if (!oo && new) {                     << 
4929                 hash_openowner(new, clp, strh << 
4930                 spin_unlock(&clp->cl_lock);   << 
4931                 return new;                   << 
4932         }                                     << 
4933         spin_unlock(&clp->cl_lock);           << 
4934                                               << 
4935         if (oo && !(oo->oo_flags & NFS4_OO_CO << 
4936                 /* Replace unconfirmed owners << 
4937                 release_openowner(oo);        << 
4938                 oo = NULL;                    << 
4939         }                                     << 
4940         if (oo) {                             << 
4941                 if (new)                      << 
4942                         nfs4_free_stateowner( << 
4943                 return oo;                    << 
4944         }                                     << 
4945                                               << 
4946         new = alloc_stateowner(openowner_slab << 
4947         if (!new)                             << 
4948                 return NULL;                     4596                 return NULL;
4949         new->oo_owner.so_ops = &openowner_ops !! 4597         oo->oo_owner.so_ops = &openowner_ops;
4950         new->oo_owner.so_is_open_owner = 1;   !! 4598         oo->oo_owner.so_is_open_owner = 1;
4951         new->oo_owner.so_seqid = open->op_seq !! 4599         oo->oo_owner.so_seqid = open->op_seqid;
4952         new->oo_flags = 0;                    !! 4600         oo->oo_flags = 0;
4953         if (nfsd4_has_session(cstate))           4601         if (nfsd4_has_session(cstate))
4954                 new->oo_flags |= NFS4_OO_CONF !! 4602                 oo->oo_flags |= NFS4_OO_CONFIRMED;
4955         new->oo_time = 0;                     !! 4603         oo->oo_time = 0;
4956         new->oo_last_closed_stid = NULL;      !! 4604         oo->oo_last_closed_stid = NULL;
4957         INIT_LIST_HEAD(&new->oo_close_lru);   !! 4605         INIT_LIST_HEAD(&oo->oo_close_lru);
4958         goto retry;                           !! 4606         spin_lock(&clp->cl_lock);
                                                   >> 4607         ret = find_openstateowner_str_locked(strhashval, open, clp);
                                                   >> 4608         if (ret == NULL) {
                                                   >> 4609                 hash_openowner(oo, clp, strhashval);
                                                   >> 4610                 ret = oo;
                                                   >> 4611         } else
                                                   >> 4612                 nfs4_free_stateowner(&oo->oo_owner);
                                                   >> 4613 
                                                   >> 4614         spin_unlock(&clp->cl_lock);
                                                   >> 4615         return ret;
4959 }                                                4616 }
4960                                                  4617 
4961 static struct nfs4_ol_stateid *                  4618 static struct nfs4_ol_stateid *
4962 init_open_stateid(struct nfs4_file *fp, struc    4619 init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
4963 {                                                4620 {
4964                                                  4621 
4965         struct nfs4_openowner *oo = open->op_    4622         struct nfs4_openowner *oo = open->op_openowner;
4966         struct nfs4_ol_stateid *retstp = NULL    4623         struct nfs4_ol_stateid *retstp = NULL;
4967         struct nfs4_ol_stateid *stp;             4624         struct nfs4_ol_stateid *stp;
4968                                                  4625 
4969         stp = open->op_stp;                      4626         stp = open->op_stp;
4970         /* We are moving these outside of the    4627         /* We are moving these outside of the spinlocks to avoid the warnings */
4971         mutex_init(&stp->st_mutex);              4628         mutex_init(&stp->st_mutex);
4972         mutex_lock_nested(&stp->st_mutex, OPE    4629         mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
4973                                                  4630 
4974 retry:                                           4631 retry:
4975         spin_lock(&oo->oo_owner.so_client->cl    4632         spin_lock(&oo->oo_owner.so_client->cl_lock);
4976         spin_lock(&fp->fi_lock);                 4633         spin_lock(&fp->fi_lock);
4977                                                  4634 
4978         retstp = nfsd4_find_existing_open(fp,    4635         retstp = nfsd4_find_existing_open(fp, open);
4979         if (retstp)                              4636         if (retstp)
4980                 goto out_unlock;                 4637                 goto out_unlock;
4981                                                  4638 
4982         open->op_stp = NULL;                     4639         open->op_stp = NULL;
4983         refcount_inc(&stp->st_stid.sc_count);    4640         refcount_inc(&stp->st_stid.sc_count);
4984         stp->st_stid.sc_type = SC_TYPE_OPEN;  !! 4641         stp->st_stid.sc_type = NFS4_OPEN_STID;
4985         INIT_LIST_HEAD(&stp->st_locks);          4642         INIT_LIST_HEAD(&stp->st_locks);
4986         stp->st_stateowner = nfs4_get_stateow    4643         stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
4987         get_nfs4_file(fp);                       4644         get_nfs4_file(fp);
4988         stp->st_stid.sc_file = fp;               4645         stp->st_stid.sc_file = fp;
4989         stp->st_access_bmap = 0;                 4646         stp->st_access_bmap = 0;
4990         stp->st_deny_bmap = 0;                   4647         stp->st_deny_bmap = 0;
4991         stp->st_openstp = NULL;                  4648         stp->st_openstp = NULL;
4992         list_add(&stp->st_perstateowner, &oo-    4649         list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
4993         list_add(&stp->st_perfile, &fp->fi_st    4650         list_add(&stp->st_perfile, &fp->fi_stateids);
4994                                                  4651 
4995 out_unlock:                                      4652 out_unlock:
4996         spin_unlock(&fp->fi_lock);               4653         spin_unlock(&fp->fi_lock);
4997         spin_unlock(&oo->oo_owner.so_client->    4654         spin_unlock(&oo->oo_owner.so_client->cl_lock);
4998         if (retstp) {                            4655         if (retstp) {
4999                 /* Handle races with CLOSE */    4656                 /* Handle races with CLOSE */
5000                 if (nfsd4_lock_ol_stateid(ret    4657                 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
5001                         nfs4_put_stid(&retstp    4658                         nfs4_put_stid(&retstp->st_stid);
5002                         goto retry;              4659                         goto retry;
5003                 }                                4660                 }
5004                 /* To keep mutex tracking hap    4661                 /* To keep mutex tracking happy */
5005                 mutex_unlock(&stp->st_mutex);    4662                 mutex_unlock(&stp->st_mutex);
5006                 stp = retstp;                    4663                 stp = retstp;
5007         }                                        4664         }
5008         return stp;                              4665         return stp;
5009 }                                                4666 }
5010                                                  4667 
5011 /*                                               4668 /*
5012  * In the 4.0 case we need to keep the owners    4669  * In the 4.0 case we need to keep the owners around a little while to handle
5013  * CLOSE replay. We still do need to release     4670  * CLOSE replay. We still do need to release any file access that is held by
5014  * them before returning however.                4671  * them before returning however.
5015  */                                              4672  */
5016 static void                                      4673 static void
5017 move_to_close_lru(struct nfs4_ol_stateid *s,     4674 move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
5018 {                                                4675 {
5019         struct nfs4_ol_stateid *last;            4676         struct nfs4_ol_stateid *last;
5020         struct nfs4_openowner *oo = openowner    4677         struct nfs4_openowner *oo = openowner(s->st_stateowner);
5021         struct nfsd_net *nn = net_generic(s->    4678         struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
5022                                                  4679                                                 nfsd_net_id);
5023                                                  4680 
5024         dprintk("NFSD: move_to_close_lru nfs4    4681         dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
5025                                                  4682 
5026         /*                                       4683         /*
5027          * We know that we hold one reference    4684          * We know that we hold one reference via nfsd4_close, and another
5028          * "persistent" reference for the cli    4685          * "persistent" reference for the client. If the refcount is higher
5029          * than 2, then there are still calls    4686          * than 2, then there are still calls in progress that are using this
5030          * stateid. We can't put the sc_file     4687          * stateid. We can't put the sc_file reference until they are finished.
5031          * Wait for the refcount to drop to 2    4688          * Wait for the refcount to drop to 2. Since it has been unhashed,
5032          * there should be no danger of the r    4689          * there should be no danger of the refcount going back up again at
5033          * this point.                           4690          * this point.
5034          * Some threads with a reference migh << 
5035          * so tell them to stop waiting.      << 
5036          */                                      4691          */
5037         atomic_set(&oo->oo_owner.so_replay.rp << 
5038         smp_mb__after_atomic();               << 
5039         wake_up_var(&oo->oo_owner.so_replay.r << 
5040         wait_event(close_wq, refcount_read(&s    4692         wait_event(close_wq, refcount_read(&s->st_stid.sc_count) == 2);
5041                                                  4693 
5042         release_all_access(s);                   4694         release_all_access(s);
5043         if (s->st_stid.sc_file) {                4695         if (s->st_stid.sc_file) {
5044                 put_nfs4_file(s->st_stid.sc_f    4696                 put_nfs4_file(s->st_stid.sc_file);
5045                 s->st_stid.sc_file = NULL;       4697                 s->st_stid.sc_file = NULL;
5046         }                                        4698         }
5047                                                  4699 
5048         spin_lock(&nn->client_lock);             4700         spin_lock(&nn->client_lock);
5049         last = oo->oo_last_closed_stid;          4701         last = oo->oo_last_closed_stid;
5050         oo->oo_last_closed_stid = s;             4702         oo->oo_last_closed_stid = s;
5051         list_move_tail(&oo->oo_close_lru, &nn    4703         list_move_tail(&oo->oo_close_lru, &nn->close_lru);
5052         oo->oo_time = ktime_get_boottime_seco    4704         oo->oo_time = ktime_get_boottime_seconds();
5053         spin_unlock(&nn->client_lock);           4705         spin_unlock(&nn->client_lock);
5054         if (last)                                4706         if (last)
5055                 nfs4_put_stid(&last->st_stid)    4707                 nfs4_put_stid(&last->st_stid);
5056 }                                                4708 }
5057                                                  4709 
5058 static noinline_for_stack struct nfs4_file *     4710 static noinline_for_stack struct nfs4_file *
5059 nfsd4_file_hash_lookup(const struct svc_fh *f    4711 nfsd4_file_hash_lookup(const struct svc_fh *fhp)
5060 {                                                4712 {
5061         struct inode *inode = d_inode(fhp->fh    4713         struct inode *inode = d_inode(fhp->fh_dentry);
5062         struct rhlist_head *tmp, *list;          4714         struct rhlist_head *tmp, *list;
5063         struct nfs4_file *fi;                    4715         struct nfs4_file *fi;
5064                                                  4716 
5065         rcu_read_lock();                         4717         rcu_read_lock();
5066         list = rhltable_lookup(&nfs4_file_rhl    4718         list = rhltable_lookup(&nfs4_file_rhltable, &inode,
5067                                nfs4_file_rhas    4719                                nfs4_file_rhash_params);
5068         rhl_for_each_entry_rcu(fi, tmp, list,    4720         rhl_for_each_entry_rcu(fi, tmp, list, fi_rlist) {
5069                 if (fh_match(&fi->fi_fhandle,    4721                 if (fh_match(&fi->fi_fhandle, &fhp->fh_handle)) {
5070                         if (refcount_inc_not_    4722                         if (refcount_inc_not_zero(&fi->fi_ref)) {
5071                                 rcu_read_unlo    4723                                 rcu_read_unlock();
5072                                 return fi;       4724                                 return fi;
5073                         }                        4725                         }
5074                 }                                4726                 }
5075         }                                        4727         }
5076         rcu_read_unlock();                       4728         rcu_read_unlock();
5077         return NULL;                             4729         return NULL;
5078 }                                                4730 }
5079                                                  4731 
5080 /*                                               4732 /*
5081  * On hash insertion, identify entries with t    4733  * On hash insertion, identify entries with the same inode but
5082  * distinct filehandles. They will all be on     4734  * distinct filehandles. They will all be on the list returned
5083  * by rhltable_lookup().                         4735  * by rhltable_lookup().
5084  *                                               4736  *
5085  * inode->i_lock prevents racing insertions f    4737  * inode->i_lock prevents racing insertions from adding an entry
5086  * for the same inode/fhp pair twice.            4738  * for the same inode/fhp pair twice.
5087  */                                              4739  */
5088 static noinline_for_stack struct nfs4_file *     4740 static noinline_for_stack struct nfs4_file *
5089 nfsd4_file_hash_insert(struct nfs4_file *new,    4741 nfsd4_file_hash_insert(struct nfs4_file *new, const struct svc_fh *fhp)
5090 {                                                4742 {
5091         struct inode *inode = d_inode(fhp->fh    4743         struct inode *inode = d_inode(fhp->fh_dentry);
5092         struct rhlist_head *tmp, *list;          4744         struct rhlist_head *tmp, *list;
5093         struct nfs4_file *ret = NULL;            4745         struct nfs4_file *ret = NULL;
5094         bool alias_found = false;                4746         bool alias_found = false;
5095         struct nfs4_file *fi;                    4747         struct nfs4_file *fi;
5096         int err;                                 4748         int err;
5097                                                  4749 
5098         rcu_read_lock();                         4750         rcu_read_lock();
5099         spin_lock(&inode->i_lock);               4751         spin_lock(&inode->i_lock);
5100                                                  4752 
5101         list = rhltable_lookup(&nfs4_file_rhl    4753         list = rhltable_lookup(&nfs4_file_rhltable, &inode,
5102                                nfs4_file_rhas    4754                                nfs4_file_rhash_params);
5103         rhl_for_each_entry_rcu(fi, tmp, list,    4755         rhl_for_each_entry_rcu(fi, tmp, list, fi_rlist) {
5104                 if (fh_match(&fi->fi_fhandle,    4756                 if (fh_match(&fi->fi_fhandle, &fhp->fh_handle)) {
5105                         if (refcount_inc_not_    4757                         if (refcount_inc_not_zero(&fi->fi_ref))
5106                                 ret = fi;        4758                                 ret = fi;
5107                 } else                           4759                 } else
5108                         fi->fi_aliased = alia    4760                         fi->fi_aliased = alias_found = true;
5109         }                                        4761         }
5110         if (ret)                                 4762         if (ret)
5111                 goto out_unlock;                 4763                 goto out_unlock;
5112                                                  4764 
5113         nfsd4_file_init(fhp, new);               4765         nfsd4_file_init(fhp, new);
5114         err = rhltable_insert(&nfs4_file_rhlt    4766         err = rhltable_insert(&nfs4_file_rhltable, &new->fi_rlist,
5115                               nfs4_file_rhash    4767                               nfs4_file_rhash_params);
5116         if (err)                                 4768         if (err)
5117                 goto out_unlock;                 4769                 goto out_unlock;
5118                                                  4770 
5119         new->fi_aliased = alias_found;           4771         new->fi_aliased = alias_found;
5120         ret = new;                               4772         ret = new;
5121                                                  4773 
5122 out_unlock:                                      4774 out_unlock:
5123         spin_unlock(&inode->i_lock);             4775         spin_unlock(&inode->i_lock);
5124         rcu_read_unlock();                       4776         rcu_read_unlock();
5125         return ret;                              4777         return ret;
5126 }                                                4778 }
5127                                                  4779 
5128 static noinline_for_stack void nfsd4_file_has    4780 static noinline_for_stack void nfsd4_file_hash_remove(struct nfs4_file *fi)
5129 {                                                4781 {
5130         rhltable_remove(&nfs4_file_rhltable,     4782         rhltable_remove(&nfs4_file_rhltable, &fi->fi_rlist,
5131                         nfs4_file_rhash_param    4783                         nfs4_file_rhash_params);
5132 }                                                4784 }
5133                                                  4785 
5134 /*                                               4786 /*
5135  * Called to check deny when READ with all ze    4787  * Called to check deny when READ with all zero stateid or
5136  * WRITE with all zero or all one stateid        4788  * WRITE with all zero or all one stateid
5137  */                                              4789  */
5138 static __be32                                    4790 static __be32
5139 nfs4_share_conflict(struct svc_fh *current_fh    4791 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
5140 {                                                4792 {
5141         struct nfs4_file *fp;                    4793         struct nfs4_file *fp;
5142         __be32 ret = nfs_ok;                     4794         __be32 ret = nfs_ok;
5143                                                  4795 
5144         fp = nfsd4_file_hash_lookup(current_f    4796         fp = nfsd4_file_hash_lookup(current_fh);
5145         if (!fp)                                 4797         if (!fp)
5146                 return ret;                      4798                 return ret;
5147                                                  4799 
5148         /* Check for conflicting share reserv    4800         /* Check for conflicting share reservations */
5149         spin_lock(&fp->fi_lock);                 4801         spin_lock(&fp->fi_lock);
5150         if (fp->fi_share_deny & deny_type)       4802         if (fp->fi_share_deny & deny_type)
5151                 ret = nfserr_locked;             4803                 ret = nfserr_locked;
5152         spin_unlock(&fp->fi_lock);               4804         spin_unlock(&fp->fi_lock);
5153         put_nfs4_file(fp);                       4805         put_nfs4_file(fp);
5154         return ret;                              4806         return ret;
5155 }                                                4807 }
5156                                                  4808 
5157 static bool nfsd4_deleg_present(const struct     4809 static bool nfsd4_deleg_present(const struct inode *inode)
5158 {                                                4810 {
5159         struct file_lock_context *ctx = locks    4811         struct file_lock_context *ctx = locks_inode_context(inode);
5160                                                  4812 
5161         return ctx && !list_empty_careful(&ct    4813         return ctx && !list_empty_careful(&ctx->flc_lease);
5162 }                                                4814 }
5163                                                  4815 
5164 /**                                              4816 /**
5165  * nfsd_wait_for_delegreturn - wait for deleg    4817  * nfsd_wait_for_delegreturn - wait for delegations to be returned
5166  * @rqstp: the RPC transaction being executed    4818  * @rqstp: the RPC transaction being executed
5167  * @inode: in-core inode of the file being wa    4819  * @inode: in-core inode of the file being waited for
5168  *                                               4820  *
5169  * The timeout prevents deadlock if all nfsd     4821  * The timeout prevents deadlock if all nfsd threads happen to be
5170  * tied up waiting for returning delegations.    4822  * tied up waiting for returning delegations.
5171  *                                               4823  *
5172  * Return values:                                4824  * Return values:
5173  *   %true: delegation was returned              4825  *   %true: delegation was returned
5174  *   %false: timed out waiting for delegretur    4826  *   %false: timed out waiting for delegreturn
5175  */                                              4827  */
5176 bool nfsd_wait_for_delegreturn(struct svc_rqs    4828 bool nfsd_wait_for_delegreturn(struct svc_rqst *rqstp, struct inode *inode)
5177 {                                                4829 {
5178         long __maybe_unused timeo;               4830         long __maybe_unused timeo;
5179                                                  4831 
5180         timeo = wait_var_event_timeout(inode,    4832         timeo = wait_var_event_timeout(inode, !nfsd4_deleg_present(inode),
5181                                        NFSD_D    4833                                        NFSD_DELEGRETURN_TIMEOUT);
5182         trace_nfsd_delegret_wakeup(rqstp, ino    4834         trace_nfsd_delegret_wakeup(rqstp, inode, timeo);
5183         return timeo > 0;                        4835         return timeo > 0;
5184 }                                                4836 }
5185                                                  4837 
5186 static void nfsd4_cb_recall_prepare(struct nf    4838 static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
5187 {                                                4839 {
5188         struct nfs4_delegation *dp = cb_to_de    4840         struct nfs4_delegation *dp = cb_to_delegation(cb);
5189         struct nfsd_net *nn = net_generic(dp-    4841         struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
5190                                           nfs    4842                                           nfsd_net_id);
5191                                                  4843 
5192         block_delegations(&dp->dl_stid.sc_fil    4844         block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
5193                                                  4845 
5194         /*                                       4846         /*
5195          * We can't do this in nfsd_break_del    4847          * We can't do this in nfsd_break_deleg_cb because it is
5196          * already holding inode->i_lock.        4848          * already holding inode->i_lock.
5197          *                                       4849          *
5198          * If the dl_time != 0, then we know     4850          * If the dl_time != 0, then we know that it has already been
5199          * queued for a lease break. Don't qu    4851          * queued for a lease break. Don't queue it again.
5200          */                                      4852          */
5201         spin_lock(&state_lock);                  4853         spin_lock(&state_lock);
5202         if (delegation_hashed(dp) && dp->dl_t    4854         if (delegation_hashed(dp) && dp->dl_time == 0) {
5203                 dp->dl_time = ktime_get_boott    4855                 dp->dl_time = ktime_get_boottime_seconds();
5204                 list_add_tail(&dp->dl_recall_    4856                 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
5205         }                                        4857         }
5206         spin_unlock(&state_lock);                4858         spin_unlock(&state_lock);
5207 }                                                4859 }
5208                                                  4860 
5209 static int nfsd4_cb_recall_done(struct nfsd4_    4861 static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
5210                 struct rpc_task *task)           4862                 struct rpc_task *task)
5211 {                                                4863 {
5212         struct nfs4_delegation *dp = cb_to_de    4864         struct nfs4_delegation *dp = cb_to_delegation(cb);
5213                                                  4865 
5214         trace_nfsd_cb_recall_done(&dp->dl_sti    4866         trace_nfsd_cb_recall_done(&dp->dl_stid.sc_stateid, task);
5215                                                  4867 
5216         if (dp->dl_stid.sc_status)            !! 4868         if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID ||
5217                 /* CLOSED or REVOKED */       !! 4869             dp->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID)
5218                 return 1;                     !! 4870                 return 1;
5219                                                  4871 
5220         switch (task->tk_status) {               4872         switch (task->tk_status) {
5221         case 0:                                  4873         case 0:
5222                 return 1;                        4874                 return 1;
5223         case -NFS4ERR_DELAY:                     4875         case -NFS4ERR_DELAY:
5224                 rpc_delay(task, 2 * HZ);         4876                 rpc_delay(task, 2 * HZ);
5225                 return 0;                        4877                 return 0;
5226         case -EBADHANDLE:                        4878         case -EBADHANDLE:
5227         case -NFS4ERR_BAD_STATEID:               4879         case -NFS4ERR_BAD_STATEID:
5228                 /*                               4880                 /*
5229                  * Race: client probably got     4881                  * Race: client probably got cb_recall before open reply
5230                  * granting delegation.          4882                  * granting delegation.
5231                  */                              4883                  */
5232                 if (dp->dl_retries--) {          4884                 if (dp->dl_retries--) {
5233                         rpc_delay(task, 2 * H    4885                         rpc_delay(task, 2 * HZ);
5234                         return 0;                4886                         return 0;
5235                 }                                4887                 }
5236                 fallthrough;                     4888                 fallthrough;
5237         default:                                 4889         default:
5238                 return 1;                        4890                 return 1;
5239         }                                        4891         }
5240 }                                                4892 }
5241                                                  4893 
5242 static void nfsd4_cb_recall_release(struct nf    4894 static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
5243 {                                                4895 {
5244         struct nfs4_delegation *dp = cb_to_de    4896         struct nfs4_delegation *dp = cb_to_delegation(cb);
5245                                                  4897 
5246         nfs4_put_stid(&dp->dl_stid);             4898         nfs4_put_stid(&dp->dl_stid);
5247 }                                                4899 }
5248                                                  4900 
5249 static const struct nfsd4_callback_ops nfsd4_    4901 static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
5250         .prepare        = nfsd4_cb_recall_pre    4902         .prepare        = nfsd4_cb_recall_prepare,
5251         .done           = nfsd4_cb_recall_don    4903         .done           = nfsd4_cb_recall_done,
5252         .release        = nfsd4_cb_recall_rel    4904         .release        = nfsd4_cb_recall_release,
5253         .opcode         = OP_CB_RECALL,       << 
5254 };                                               4905 };
5255                                                  4906 
5256 static void nfsd_break_one_deleg(struct nfs4_    4907 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
5257 {                                                4908 {
5258         /*                                       4909         /*
5259          * We're assuming the state code neve    4910          * We're assuming the state code never drops its reference
5260          * without first removing the lease.     4911          * without first removing the lease.  Since we're in this lease
5261          * callback (and since the lease code    4912          * callback (and since the lease code is serialized by the
5262          * flc_lock) we know the server hasn'    4913          * flc_lock) we know the server hasn't removed the lease yet, and
5263          * we know it's safe to take a refere    4914          * we know it's safe to take a reference.
5264          */                                      4915          */
5265         refcount_inc(&dp->dl_stid.sc_count);     4916         refcount_inc(&dp->dl_stid.sc_count);
5266         WARN_ON_ONCE(!nfsd4_run_cb(&dp->dl_re    4917         WARN_ON_ONCE(!nfsd4_run_cb(&dp->dl_recall));
5267 }                                                4918 }
5268                                                  4919 
5269 /* Called from break_lease() with flc_lock he    4920 /* Called from break_lease() with flc_lock held. */
5270 static bool                                      4921 static bool
5271 nfsd_break_deleg_cb(struct file_lease *fl)    !! 4922 nfsd_break_deleg_cb(struct file_lock *fl)
5272 {                                                4923 {
5273         struct nfs4_delegation *dp = (struct  !! 4924         struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
5274         struct nfs4_file *fp = dp->dl_stid.sc    4925         struct nfs4_file *fp = dp->dl_stid.sc_file;
5275         struct nfs4_client *clp = dp->dl_stid    4926         struct nfs4_client *clp = dp->dl_stid.sc_client;
5276         struct nfsd_net *nn;                     4927         struct nfsd_net *nn;
5277                                                  4928 
5278         trace_nfsd_cb_recall(&dp->dl_stid);      4929         trace_nfsd_cb_recall(&dp->dl_stid);
5279                                                  4930 
5280         dp->dl_recalled = true;                  4931         dp->dl_recalled = true;
5281         atomic_inc(&clp->cl_delegs_in_recall)    4932         atomic_inc(&clp->cl_delegs_in_recall);
5282         if (try_to_expire_client(clp)) {         4933         if (try_to_expire_client(clp)) {
5283                 nn = net_generic(clp->net, nf    4934                 nn = net_generic(clp->net, nfsd_net_id);
5284                 mod_delayed_work(laundry_wq,     4935                 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
5285         }                                        4936         }
5286                                                  4937 
5287         /*                                       4938         /*
5288          * We don't want the locks code to ti    4939          * We don't want the locks code to timeout the lease for us;
5289          * we'll remove it ourself if a deleg    4940          * we'll remove it ourself if a delegation isn't returned
5290          * in time:                              4941          * in time:
5291          */                                      4942          */
5292         fl->fl_break_time = 0;                   4943         fl->fl_break_time = 0;
5293                                                  4944 
5294         fp->fi_had_conflict = true;              4945         fp->fi_had_conflict = true;
5295         nfsd_break_one_deleg(dp);                4946         nfsd_break_one_deleg(dp);
5296         return false;                            4947         return false;
5297 }                                                4948 }
5298                                                  4949 
5299 /**                                              4950 /**
5300  * nfsd_breaker_owns_lease - Check if lease c    4951  * nfsd_breaker_owns_lease - Check if lease conflict was resolved
5301  * @fl: Lock state to check                      4952  * @fl: Lock state to check
5302  *                                               4953  *
5303  * Return values:                                4954  * Return values:
5304  *   %true: Lease conflict was resolved          4955  *   %true: Lease conflict was resolved
5305  *   %false: Lease conflict was not resolved.    4956  *   %false: Lease conflict was not resolved.
5306  */                                              4957  */
5307 static bool nfsd_breaker_owns_lease(struct fi !! 4958 static bool nfsd_breaker_owns_lease(struct file_lock *fl)
5308 {                                                4959 {
5309         struct nfs4_delegation *dl = fl->c.fl !! 4960         struct nfs4_delegation *dl = fl->fl_owner;
5310         struct svc_rqst *rqst;                   4961         struct svc_rqst *rqst;
5311         struct nfs4_client *clp;                 4962         struct nfs4_client *clp;
5312                                                  4963 
5313         rqst = nfsd_current_rqst();           !! 4964         if (!i_am_nfsd())
5314         if (!nfsd_v4client(rqst))             !! 4965                 return false;
                                                   >> 4966         rqst = kthread_data(current);
                                                   >> 4967         /* Note rq_prog == NFS_ACL_PROGRAM is also possible: */
                                                   >> 4968         if (rqst->rq_prog != NFS_PROGRAM || rqst->rq_vers < 4)
5315                 return false;                    4969                 return false;
5316         clp = *(rqst->rq_lease_breaker);         4970         clp = *(rqst->rq_lease_breaker);
5317         return dl->dl_stid.sc_client == clp;     4971         return dl->dl_stid.sc_client == clp;
5318 }                                                4972 }
5319                                                  4973 
5320 static int                                       4974 static int
5321 nfsd_change_deleg_cb(struct file_lease *onlis !! 4975 nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
5322                      struct list_head *dispos    4976                      struct list_head *dispose)
5323 {                                                4977 {
5324         struct nfs4_delegation *dp = (struct  !! 4978         struct nfs4_delegation *dp = (struct nfs4_delegation *)onlist->fl_owner;
5325         struct nfs4_client *clp = dp->dl_stid    4979         struct nfs4_client *clp = dp->dl_stid.sc_client;
5326                                                  4980 
5327         if (arg & F_UNLCK) {                     4981         if (arg & F_UNLCK) {
5328                 if (dp->dl_recalled)             4982                 if (dp->dl_recalled)
5329                         atomic_dec(&clp->cl_d    4983                         atomic_dec(&clp->cl_delegs_in_recall);
5330                 return lease_modify(onlist, a    4984                 return lease_modify(onlist, arg, dispose);
5331         } else                                   4985         } else
5332                 return -EAGAIN;                  4986                 return -EAGAIN;
5333 }                                                4987 }
5334                                                  4988 
5335 static const struct lease_manager_operations  !! 4989 static const struct lock_manager_operations nfsd_lease_mng_ops = {
5336         .lm_breaker_owns_lease = nfsd_breaker    4990         .lm_breaker_owns_lease = nfsd_breaker_owns_lease,
5337         .lm_break = nfsd_break_deleg_cb,         4991         .lm_break = nfsd_break_deleg_cb,
5338         .lm_change = nfsd_change_deleg_cb,       4992         .lm_change = nfsd_change_deleg_cb,
5339 };                                               4993 };
5340                                                  4994 
5341 static __be32 nfsd4_check_seqid(struct nfsd4_    4995 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
5342 {                                                4996 {
5343         if (nfsd4_has_session(cstate))           4997         if (nfsd4_has_session(cstate))
5344                 return nfs_ok;                   4998                 return nfs_ok;
5345         if (seqid == so->so_seqid - 1)           4999         if (seqid == so->so_seqid - 1)
5346                 return nfserr_replay_me;         5000                 return nfserr_replay_me;
5347         if (seqid == so->so_seqid)               5001         if (seqid == so->so_seqid)
5348                 return nfs_ok;                   5002                 return nfs_ok;
5349         return nfserr_bad_seqid;                 5003         return nfserr_bad_seqid;
5350 }                                                5004 }
5351                                                  5005 
5352 static struct nfs4_client *lookup_clientid(cl    5006 static struct nfs4_client *lookup_clientid(clientid_t *clid, bool sessions,
5353                                                  5007                                                 struct nfsd_net *nn)
5354 {                                                5008 {
5355         struct nfs4_client *found;               5009         struct nfs4_client *found;
5356                                                  5010 
5357         spin_lock(&nn->client_lock);             5011         spin_lock(&nn->client_lock);
5358         found = find_confirmed_client(clid, s    5012         found = find_confirmed_client(clid, sessions, nn);
5359         if (found)                               5013         if (found)
5360                 atomic_inc(&found->cl_rpc_use    5014                 atomic_inc(&found->cl_rpc_users);
5361         spin_unlock(&nn->client_lock);           5015         spin_unlock(&nn->client_lock);
5362         return found;                            5016         return found;
5363 }                                                5017 }
5364                                                  5018 
5365 static __be32 set_client(clientid_t *clid,       5019 static __be32 set_client(clientid_t *clid,
5366                 struct nfsd4_compound_state *    5020                 struct nfsd4_compound_state *cstate,
5367                 struct nfsd_net *nn)             5021                 struct nfsd_net *nn)
5368 {                                                5022 {
5369         if (cstate->clp) {                       5023         if (cstate->clp) {
5370                 if (!same_clid(&cstate->clp->    5024                 if (!same_clid(&cstate->clp->cl_clientid, clid))
5371                         return nfserr_stale_c    5025                         return nfserr_stale_clientid;
5372                 return nfs_ok;                   5026                 return nfs_ok;
5373         }                                        5027         }
5374         if (STALE_CLIENTID(clid, nn))            5028         if (STALE_CLIENTID(clid, nn))
5375                 return nfserr_stale_clientid;    5029                 return nfserr_stale_clientid;
5376         /*                                       5030         /*
5377          * We're in the 4.0 case (otherwise t    5031          * We're in the 4.0 case (otherwise the SEQUENCE op would have
5378          * set cstate->clp), so session = fal    5032          * set cstate->clp), so session = false:
5379          */                                      5033          */
5380         cstate->clp = lookup_clientid(clid, f    5034         cstate->clp = lookup_clientid(clid, false, nn);
5381         if (!cstate->clp)                        5035         if (!cstate->clp)
5382                 return nfserr_expired;           5036                 return nfserr_expired;
5383         return nfs_ok;                           5037         return nfs_ok;
5384 }                                                5038 }
5385                                                  5039 
5386 __be32                                           5040 __be32
5387 nfsd4_process_open1(struct nfsd4_compound_sta    5041 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
5388                     struct nfsd4_open *open,     5042                     struct nfsd4_open *open, struct nfsd_net *nn)
5389 {                                                5043 {
5390         clientid_t *clientid = &open->op_clie    5044         clientid_t *clientid = &open->op_clientid;
5391         struct nfs4_client *clp = NULL;          5045         struct nfs4_client *clp = NULL;
5392         unsigned int strhashval;                 5046         unsigned int strhashval;
5393         struct nfs4_openowner *oo = NULL;        5047         struct nfs4_openowner *oo = NULL;
5394         __be32 status;                           5048         __be32 status;
5395                                                  5049 
5396         /*                                       5050         /*
5397          * In case we need it later, after we    5051          * In case we need it later, after we've already created the
5398          * file and don't want to risk a furt    5052          * file and don't want to risk a further failure:
5399          */                                      5053          */
5400         open->op_file = nfsd4_alloc_file();      5054         open->op_file = nfsd4_alloc_file();
5401         if (open->op_file == NULL)               5055         if (open->op_file == NULL)
5402                 return nfserr_jukebox;           5056                 return nfserr_jukebox;
5403                                                  5057 
5404         status = set_client(clientid, cstate,    5058         status = set_client(clientid, cstate, nn);
5405         if (status)                              5059         if (status)
5406                 return status;                   5060                 return status;
5407         clp = cstate->clp;                       5061         clp = cstate->clp;
5408                                                  5062 
5409         strhashval = ownerstr_hashval(&open->    5063         strhashval = ownerstr_hashval(&open->op_owner);
5410 retry:                                        !! 5064         oo = find_openstateowner_str(strhashval, open, clp);
5411         oo = find_or_alloc_open_stateowner(st << 
5412         open->op_openowner = oo;                 5065         open->op_openowner = oo;
5413         if (!oo)                              !! 5066         if (!oo) {
5414                 return nfserr_jukebox;        !! 5067                 goto new_owner;
5415         if (nfsd4_cstate_assign_replay(cstate !! 5068         }
5416                 nfs4_put_stateowner(&oo->oo_o !! 5069         if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
5417                 goto retry;                   !! 5070                 /* Replace unconfirmed owners without checking for replay. */
                                                   >> 5071                 release_openowner(oo);
                                                   >> 5072                 open->op_openowner = NULL;
                                                   >> 5073                 goto new_owner;
5418         }                                        5074         }
5419         status = nfsd4_check_seqid(cstate, &o    5075         status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
5420         if (status)                              5076         if (status)
5421                 return status;                   5077                 return status;
5422                                               !! 5078         goto alloc_stateid;
                                                   >> 5079 new_owner:
                                                   >> 5080         oo = alloc_init_open_stateowner(strhashval, open, cstate);
                                                   >> 5081         if (oo == NULL)
                                                   >> 5082                 return nfserr_jukebox;
                                                   >> 5083         open->op_openowner = oo;
                                                   >> 5084 alloc_stateid:
5423         open->op_stp = nfs4_alloc_open_statei    5085         open->op_stp = nfs4_alloc_open_stateid(clp);
5424         if (!open->op_stp)                       5086         if (!open->op_stp)
5425                 return nfserr_jukebox;           5087                 return nfserr_jukebox;
5426                                                  5088 
5427         if (nfsd4_has_session(cstate) &&         5089         if (nfsd4_has_session(cstate) &&
5428             (cstate->current_fh.fh_export->ex    5090             (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
5429                 open->op_odstate = alloc_clnt    5091                 open->op_odstate = alloc_clnt_odstate(clp);
5430                 if (!open->op_odstate)           5092                 if (!open->op_odstate)
5431                         return nfserr_jukebox    5093                         return nfserr_jukebox;
5432         }                                        5094         }
5433                                                  5095 
5434         return nfs_ok;                           5096         return nfs_ok;
5435 }                                                5097 }
5436                                                  5098 
5437 static inline __be32                             5099 static inline __be32
5438 nfs4_check_delegmode(struct nfs4_delegation *    5100 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
5439 {                                                5101 {
5440         if ((flags & WR_STATE) && (dp->dl_typ    5102         if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
5441                 return nfserr_openmode;          5103                 return nfserr_openmode;
5442         else                                     5104         else
5443                 return nfs_ok;                   5105                 return nfs_ok;
5444 }                                                5106 }
5445                                                  5107 
5446 static int share_access_to_flags(u32 share_ac    5108 static int share_access_to_flags(u32 share_access)
5447 {                                                5109 {
5448         return share_access == NFS4_SHARE_ACC    5110         return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
5449 }                                                5111 }
5450                                                  5112 
5451 static struct nfs4_delegation *find_deleg_sta !! 5113 static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
5452                                               << 
5453 {                                                5114 {
5454         struct nfs4_stid *ret;                   5115         struct nfs4_stid *ret;
5455                                                  5116 
5456         ret = find_stateid_by_type(cl, s, SC_ !! 5117         ret = find_stateid_by_type(cl, s,
                                                   >> 5118                                 NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID);
5457         if (!ret)                                5119         if (!ret)
5458                 return NULL;                     5120                 return NULL;
5459         return delegstateid(ret);                5121         return delegstateid(ret);
5460 }                                                5122 }
5461                                                  5123 
5462 static bool nfsd4_is_deleg_cur(struct nfsd4_o    5124 static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
5463 {                                                5125 {
5464         return open->op_claim_type == NFS4_OP    5126         return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
5465                open->op_claim_type == NFS4_OP    5127                open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
5466 }                                                5128 }
5467                                                  5129 
5468 static __be32                                    5130 static __be32
5469 nfs4_check_deleg(struct nfs4_client *cl, stru    5131 nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
5470                 struct nfs4_delegation **dp)     5132                 struct nfs4_delegation **dp)
5471 {                                                5133 {
5472         int flags;                               5134         int flags;
5473         __be32 status = nfserr_bad_stateid;      5135         __be32 status = nfserr_bad_stateid;
5474         struct nfs4_delegation *deleg;           5136         struct nfs4_delegation *deleg;
5475                                                  5137 
5476         deleg = find_deleg_stateid(cl, &open-    5138         deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
5477         if (deleg == NULL)                       5139         if (deleg == NULL)
5478                 goto out;                        5140                 goto out;
5479         if (deleg->dl_stid.sc_status & SC_STA !! 5141         if (deleg->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) {
5480                 nfs4_put_stid(&deleg->dl_stid << 
5481                 status = nfserr_admin_revoked << 
5482                 goto out;                     << 
5483         }                                     << 
5484         if (deleg->dl_stid.sc_status & SC_STA << 
5485                 nfs4_put_stid(&deleg->dl_stid    5142                 nfs4_put_stid(&deleg->dl_stid);
5486                 nfsd40_drop_revoked_stid(cl,  !! 5143                 if (cl->cl_minorversion)
5487                 status = nfserr_deleg_revoked !! 5144                         status = nfserr_deleg_revoked;
5488                 goto out;                        5145                 goto out;
5489         }                                        5146         }
5490         flags = share_access_to_flags(open->o    5147         flags = share_access_to_flags(open->op_share_access);
5491         status = nfs4_check_delegmode(deleg,     5148         status = nfs4_check_delegmode(deleg, flags);
5492         if (status) {                            5149         if (status) {
5493                 nfs4_put_stid(&deleg->dl_stid    5150                 nfs4_put_stid(&deleg->dl_stid);
5494                 goto out;                        5151                 goto out;
5495         }                                        5152         }
5496         *dp = deleg;                             5153         *dp = deleg;
5497 out:                                             5154 out:
5498         if (!nfsd4_is_deleg_cur(open))           5155         if (!nfsd4_is_deleg_cur(open))
5499                 return nfs_ok;                   5156                 return nfs_ok;
5500         if (status)                              5157         if (status)
5501                 return status;                   5158                 return status;
5502         open->op_openowner->oo_flags |= NFS4_    5159         open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
5503         return nfs_ok;                           5160         return nfs_ok;
5504 }                                                5161 }
5505                                                  5162 
5506 static inline int nfs4_access_to_access(u32 n    5163 static inline int nfs4_access_to_access(u32 nfs4_access)
5507 {                                                5164 {
5508         int flags = 0;                           5165         int flags = 0;
5509                                                  5166 
5510         if (nfs4_access & NFS4_SHARE_ACCESS_R    5167         if (nfs4_access & NFS4_SHARE_ACCESS_READ)
5511                 flags |= NFSD_MAY_READ;          5168                 flags |= NFSD_MAY_READ;
5512         if (nfs4_access & NFS4_SHARE_ACCESS_W    5169         if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
5513                 flags |= NFSD_MAY_WRITE;         5170                 flags |= NFSD_MAY_WRITE;
5514         return flags;                            5171         return flags;
5515 }                                                5172 }
5516                                                  5173 
5517 static inline __be32                             5174 static inline __be32
5518 nfsd4_truncate(struct svc_rqst *rqstp, struct    5175 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
5519                 struct nfsd4_open *open)         5176                 struct nfsd4_open *open)
5520 {                                                5177 {
5521         struct iattr iattr = {                   5178         struct iattr iattr = {
5522                 .ia_valid = ATTR_SIZE,           5179                 .ia_valid = ATTR_SIZE,
5523                 .ia_size = 0,                    5180                 .ia_size = 0,
5524         };                                       5181         };
5525         struct nfsd_attrs attrs = {              5182         struct nfsd_attrs attrs = {
5526                 .na_iattr       = &iattr,        5183                 .na_iattr       = &iattr,
5527         };                                       5184         };
5528         if (!open->op_truncate)                  5185         if (!open->op_truncate)
5529                 return 0;                        5186                 return 0;
5530         if (!(open->op_share_access & NFS4_SH    5187         if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
5531                 return nfserr_inval;             5188                 return nfserr_inval;
5532         return nfsd_setattr(rqstp, fh, &attrs !! 5189         return nfsd_setattr(rqstp, fh, &attrs, 0, (time64_t)0);
5533 }                                                5190 }
5534                                                  5191 
5535 static __be32 nfs4_get_vfs_file(struct svc_rq    5192 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
5536                 struct svc_fh *cur_fh, struct    5193                 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
5537                 struct nfsd4_open *open, bool    5194                 struct nfsd4_open *open, bool new_stp)
5538 {                                                5195 {
5539         struct nfsd_file *nf = NULL;             5196         struct nfsd_file *nf = NULL;
5540         __be32 status;                           5197         __be32 status;
5541         int oflag = nfs4_access_to_omode(open    5198         int oflag = nfs4_access_to_omode(open->op_share_access);
5542         int access = nfs4_access_to_access(op    5199         int access = nfs4_access_to_access(open->op_share_access);
5543         unsigned char old_access_bmap, old_de    5200         unsigned char old_access_bmap, old_deny_bmap;
5544                                                  5201 
5545         spin_lock(&fp->fi_lock);                 5202         spin_lock(&fp->fi_lock);
5546                                                  5203 
5547         /*                                       5204         /*
5548          * Are we trying to set a deny mode t    5205          * Are we trying to set a deny mode that would conflict with
5549          * current access?                       5206          * current access?
5550          */                                      5207          */
5551         status = nfs4_file_check_deny(fp, ope    5208         status = nfs4_file_check_deny(fp, open->op_share_deny);
5552         if (status != nfs_ok) {                  5209         if (status != nfs_ok) {
5553                 if (status != nfserr_share_de    5210                 if (status != nfserr_share_denied) {
5554                         spin_unlock(&fp->fi_l    5211                         spin_unlock(&fp->fi_lock);
5555                         goto out;                5212                         goto out;
5556                 }                                5213                 }
5557                 if (nfs4_resolve_deny_conflic    5214                 if (nfs4_resolve_deny_conflicts_locked(fp, new_stp,
5558                                 stp, open->op    5215                                 stp, open->op_share_deny, false))
5559                         status = nfserr_jukeb    5216                         status = nfserr_jukebox;
5560                 spin_unlock(&fp->fi_lock);       5217                 spin_unlock(&fp->fi_lock);
5561                 goto out;                        5218                 goto out;
5562         }                                        5219         }
5563                                                  5220 
5564         /* set access to the file */             5221         /* set access to the file */
5565         status = nfs4_file_get_access(fp, ope    5222         status = nfs4_file_get_access(fp, open->op_share_access);
5566         if (status != nfs_ok) {                  5223         if (status != nfs_ok) {
5567                 if (status != nfserr_share_de    5224                 if (status != nfserr_share_denied) {
5568                         spin_unlock(&fp->fi_l    5225                         spin_unlock(&fp->fi_lock);
5569                         goto out;                5226                         goto out;
5570                 }                                5227                 }
5571                 if (nfs4_resolve_deny_conflic    5228                 if (nfs4_resolve_deny_conflicts_locked(fp, new_stp,
5572                                 stp, open->op    5229                                 stp, open->op_share_access, true))
5573                         status = nfserr_jukeb    5230                         status = nfserr_jukebox;
5574                 spin_unlock(&fp->fi_lock);       5231                 spin_unlock(&fp->fi_lock);
5575                 goto out;                        5232                 goto out;
5576         }                                        5233         }
5577                                                  5234 
5578         /* Set access bits in stateid */         5235         /* Set access bits in stateid */
5579         old_access_bmap = stp->st_access_bmap    5236         old_access_bmap = stp->st_access_bmap;
5580         set_access(open->op_share_access, stp    5237         set_access(open->op_share_access, stp);
5581                                                  5238 
5582         /* Set new deny mask */                  5239         /* Set new deny mask */
5583         old_deny_bmap = stp->st_deny_bmap;       5240         old_deny_bmap = stp->st_deny_bmap;
5584         set_deny(open->op_share_deny, stp);      5241         set_deny(open->op_share_deny, stp);
5585         fp->fi_share_deny |= (open->op_share_    5242         fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
5586                                                  5243 
5587         if (!fp->fi_fds[oflag]) {                5244         if (!fp->fi_fds[oflag]) {
5588                 spin_unlock(&fp->fi_lock);       5245                 spin_unlock(&fp->fi_lock);
5589                                                  5246 
5590                 status = nfsd_file_acquire_op    5247                 status = nfsd_file_acquire_opened(rqstp, cur_fh, access,
5591                                                  5248                                                   open->op_filp, &nf);
5592                 if (status != nfs_ok)            5249                 if (status != nfs_ok)
5593                         goto out_put_access;     5250                         goto out_put_access;
5594                                                  5251 
5595                 spin_lock(&fp->fi_lock);         5252                 spin_lock(&fp->fi_lock);
5596                 if (!fp->fi_fds[oflag]) {        5253                 if (!fp->fi_fds[oflag]) {
5597                         fp->fi_fds[oflag] = n    5254                         fp->fi_fds[oflag] = nf;
5598                         nf = NULL;               5255                         nf = NULL;
5599                 }                                5256                 }
5600         }                                        5257         }
5601         spin_unlock(&fp->fi_lock);               5258         spin_unlock(&fp->fi_lock);
5602         if (nf)                                  5259         if (nf)
5603                 nfsd_file_put(nf);               5260                 nfsd_file_put(nf);
5604                                                  5261 
5605         status = nfserrno(nfsd_open_break_lea    5262         status = nfserrno(nfsd_open_break_lease(cur_fh->fh_dentry->d_inode,
5606                                                  5263                                                                 access));
5607         if (status)                              5264         if (status)
5608                 goto out_put_access;             5265                 goto out_put_access;
5609                                                  5266 
5610         status = nfsd4_truncate(rqstp, cur_fh    5267         status = nfsd4_truncate(rqstp, cur_fh, open);
5611         if (status)                              5268         if (status)
5612                 goto out_put_access;             5269                 goto out_put_access;
5613 out:                                             5270 out:
5614         return status;                           5271         return status;
5615 out_put_access:                                  5272 out_put_access:
5616         stp->st_access_bmap = old_access_bmap    5273         stp->st_access_bmap = old_access_bmap;
5617         nfs4_file_put_access(fp, open->op_sha    5274         nfs4_file_put_access(fp, open->op_share_access);
5618         reset_union_bmap_deny(bmap_to_share_m    5275         reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
5619         goto out;                                5276         goto out;
5620 }                                                5277 }
5621                                                  5278 
5622 static __be32                                    5279 static __be32
5623 nfs4_upgrade_open(struct svc_rqst *rqstp, str    5280 nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp,
5624                 struct svc_fh *cur_fh, struct    5281                 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
5625                 struct nfsd4_open *open)         5282                 struct nfsd4_open *open)
5626 {                                                5283 {
5627         __be32 status;                           5284         __be32 status;
5628         unsigned char old_deny_bmap = stp->st    5285         unsigned char old_deny_bmap = stp->st_deny_bmap;
5629                                                  5286 
5630         if (!test_access(open->op_share_acces    5287         if (!test_access(open->op_share_access, stp))
5631                 return nfs4_get_vfs_file(rqst    5288                 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open, false);
5632                                                  5289 
5633         /* test and set deny mode */             5290         /* test and set deny mode */
5634         spin_lock(&fp->fi_lock);                 5291         spin_lock(&fp->fi_lock);
5635         status = nfs4_file_check_deny(fp, ope    5292         status = nfs4_file_check_deny(fp, open->op_share_deny);
5636         switch (status) {                        5293         switch (status) {
5637         case nfs_ok:                             5294         case nfs_ok:
5638                 set_deny(open->op_share_deny,    5295                 set_deny(open->op_share_deny, stp);
5639                 fp->fi_share_deny |=             5296                 fp->fi_share_deny |=
5640                         (open->op_share_deny     5297                         (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
5641                 break;                           5298                 break;
5642         case nfserr_share_denied:                5299         case nfserr_share_denied:
5643                 if (nfs4_resolve_deny_conflic    5300                 if (nfs4_resolve_deny_conflicts_locked(fp, false,
5644                                 stp, open->op    5301                                 stp, open->op_share_deny, false))
5645                         status = nfserr_jukeb    5302                         status = nfserr_jukebox;
5646                 break;                           5303                 break;
5647         }                                        5304         }
5648         spin_unlock(&fp->fi_lock);               5305         spin_unlock(&fp->fi_lock);
5649                                                  5306 
5650         if (status != nfs_ok)                    5307         if (status != nfs_ok)
5651                 return status;                   5308                 return status;
5652                                                  5309 
5653         status = nfsd4_truncate(rqstp, cur_fh    5310         status = nfsd4_truncate(rqstp, cur_fh, open);
5654         if (status != nfs_ok)                    5311         if (status != nfs_ok)
5655                 reset_union_bmap_deny(old_den    5312                 reset_union_bmap_deny(old_deny_bmap, stp);
5656         return status;                           5313         return status;
5657 }                                                5314 }
5658                                                  5315 
5659 /* Should we give out recallable state?: */      5316 /* Should we give out recallable state?: */
5660 static bool nfsd4_cb_channel_good(struct nfs4    5317 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
5661 {                                                5318 {
5662         if (clp->cl_cb_state == NFSD4_CB_UP)     5319         if (clp->cl_cb_state == NFSD4_CB_UP)
5663                 return true;                     5320                 return true;
5664         /*                                       5321         /*
5665          * In the sessions case, since we don    5322          * In the sessions case, since we don't have to establish a
5666          * separate connection for callbacks,    5323          * separate connection for callbacks, we assume it's OK
5667          * until we hear otherwise:              5324          * until we hear otherwise:
5668          */                                      5325          */
5669         return clp->cl_minorversion && clp->c    5326         return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
5670 }                                                5327 }
5671                                                  5328 
5672 static struct file_lease *nfs4_alloc_init_lea !! 5329 static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp,
5673                                                  5330                                                 int flag)
5674 {                                                5331 {
5675         struct file_lease *fl;                !! 5332         struct file_lock *fl;
5676                                                  5333 
5677         fl = locks_alloc_lease();             !! 5334         fl = locks_alloc_lock();
5678         if (!fl)                                 5335         if (!fl)
5679                 return NULL;                     5336                 return NULL;
5680         fl->fl_lmops = &nfsd_lease_mng_ops;      5337         fl->fl_lmops = &nfsd_lease_mng_ops;
5681         fl->c.flc_flags = FL_DELEG;           !! 5338         fl->fl_flags = FL_DELEG;
5682         fl->c.flc_type = flag == NFS4_OPEN_DE !! 5339         fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
5683         fl->c.flc_owner = (fl_owner_t)dp;     !! 5340         fl->fl_end = OFFSET_MAX;
5684         fl->c.flc_pid = current->tgid;        !! 5341         fl->fl_owner = (fl_owner_t)dp;
5685         fl->c.flc_file = dp->dl_stid.sc_file- !! 5342         fl->fl_pid = current->tgid;
                                                   >> 5343         fl->fl_file = dp->dl_stid.sc_file->fi_deleg_file->nf_file;
5686         return fl;                               5344         return fl;
5687 }                                                5345 }
5688                                                  5346 
5689 static int nfsd4_check_conflicting_opens(stru    5347 static int nfsd4_check_conflicting_opens(struct nfs4_client *clp,
5690                                          stru    5348                                          struct nfs4_file *fp)
5691 {                                                5349 {
5692         struct nfs4_ol_stateid *st;              5350         struct nfs4_ol_stateid *st;
5693         struct file *f = fp->fi_deleg_file->n    5351         struct file *f = fp->fi_deleg_file->nf_file;
5694         struct inode *ino = file_inode(f);       5352         struct inode *ino = file_inode(f);
5695         int writes;                              5353         int writes;
5696                                                  5354 
5697         writes = atomic_read(&ino->i_writecou    5355         writes = atomic_read(&ino->i_writecount);
5698         if (!writes)                             5356         if (!writes)
5699                 return 0;                        5357                 return 0;
5700         /*                                       5358         /*
5701          * There could be multiple filehandle    5359          * There could be multiple filehandles (hence multiple
5702          * nfs4_files) referencing this file,    5360          * nfs4_files) referencing this file, but that's not too
5703          * common; let's just give up in that    5361          * common; let's just give up in that case rather than
5704          * trying to go look up all the clien    5362          * trying to go look up all the clients using that other
5705          * nfs4_file as well:                    5363          * nfs4_file as well:
5706          */                                      5364          */
5707         if (fp->fi_aliased)                      5365         if (fp->fi_aliased)
5708                 return -EAGAIN;                  5366                 return -EAGAIN;
5709         /*                                       5367         /*
5710          * If there's a close in progress, ma    5368          * If there's a close in progress, make sure that we see it
5711          * clear any fi_fds[] entries before     5369          * clear any fi_fds[] entries before we see it decrement
5712          * i_writecount:                         5370          * i_writecount:
5713          */                                      5371          */
5714         smp_mb__after_atomic();                  5372         smp_mb__after_atomic();
5715                                                  5373 
5716         if (fp->fi_fds[O_WRONLY])                5374         if (fp->fi_fds[O_WRONLY])
5717                 writes--;                        5375                 writes--;
5718         if (fp->fi_fds[O_RDWR])                  5376         if (fp->fi_fds[O_RDWR])
5719                 writes--;                        5377                 writes--;
5720         if (writes > 0)                          5378         if (writes > 0)
5721                 return -EAGAIN; /* There may     5379                 return -EAGAIN; /* There may be non-NFSv4 writers */
5722         /*                                       5380         /*
5723          * It's possible there are non-NFSv4     5381          * It's possible there are non-NFSv4 write opens in progress,
5724          * but if they haven't incremented i_    5382          * but if they haven't incremented i_writecount yet then they
5725          * also haven't called break lease ye    5383          * also haven't called break lease yet; so, they'll break this
5726          * lease soon enough.  So, all that's    5384          * lease soon enough.  So, all that's left to check for is NFSv4
5727          * opens:                                5385          * opens:
5728          */                                      5386          */
5729         spin_lock(&fp->fi_lock);                 5387         spin_lock(&fp->fi_lock);
5730         list_for_each_entry(st, &fp->fi_state    5388         list_for_each_entry(st, &fp->fi_stateids, st_perfile) {
5731                 if (st->st_openstp == NULL /*    5389                 if (st->st_openstp == NULL /* it's an open */ &&
5732                     access_permit_write(st) &    5390                     access_permit_write(st) &&
5733                     st->st_stid.sc_client !=     5391                     st->st_stid.sc_client != clp) {
5734                         spin_unlock(&fp->fi_l    5392                         spin_unlock(&fp->fi_lock);
5735                         return -EAGAIN;          5393                         return -EAGAIN;
5736                 }                                5394                 }
5737         }                                        5395         }
5738         spin_unlock(&fp->fi_lock);               5396         spin_unlock(&fp->fi_lock);
5739         /*                                       5397         /*
5740          * There's a small chance that we cou    5398          * There's a small chance that we could be racing with another
5741          * NFSv4 open.  However, any open tha    5399          * NFSv4 open.  However, any open that hasn't added itself to
5742          * the fi_stateids list also hasn't c    5400          * the fi_stateids list also hasn't called break_lease yet; so,
5743          * they'll break this lease soon enou    5401          * they'll break this lease soon enough.
5744          */                                      5402          */
5745         return 0;                                5403         return 0;
5746 }                                                5404 }
5747                                                  5405 
5748 /*                                               5406 /*
5749  * It's possible that between opening the den    5407  * It's possible that between opening the dentry and setting the delegation,
5750  * that it has been renamed or unlinked. Redo    5408  * that it has been renamed or unlinked. Redo the lookup to verify that this
5751  * hasn't happened.                              5409  * hasn't happened.
5752  */                                              5410  */
5753 static int                                       5411 static int
5754 nfsd4_verify_deleg_dentry(struct nfsd4_open *    5412 nfsd4_verify_deleg_dentry(struct nfsd4_open *open, struct nfs4_file *fp,
5755                           struct svc_fh *pare    5413                           struct svc_fh *parent)
5756 {                                                5414 {
5757         struct svc_export *exp;                  5415         struct svc_export *exp;
5758         struct dentry *child;                    5416         struct dentry *child;
5759         __be32 err;                              5417         __be32 err;
5760                                                  5418 
5761         err = nfsd_lookup_dentry(open->op_rqs    5419         err = nfsd_lookup_dentry(open->op_rqstp, parent,
5762                                  open->op_fna    5420                                  open->op_fname, open->op_fnamelen,
5763                                  &exp, &child    5421                                  &exp, &child);
5764                                                  5422 
5765         if (err)                                 5423         if (err)
5766                 return -EAGAIN;                  5424                 return -EAGAIN;
5767                                                  5425 
5768         exp_put(exp);                            5426         exp_put(exp);
5769         dput(child);                             5427         dput(child);
5770         if (child != file_dentry(fp->fi_deleg    5428         if (child != file_dentry(fp->fi_deleg_file->nf_file))
5771                 return -EAGAIN;                  5429                 return -EAGAIN;
5772                                                  5430 
5773         return 0;                                5431         return 0;
5774 }                                                5432 }
5775                                                  5433 
5776 /*                                               5434 /*
5777  * We avoid breaking delegations held by a cl    5435  * We avoid breaking delegations held by a client due to its own activity, but
5778  * clearing setuid/setgid bits on a write is     5436  * clearing setuid/setgid bits on a write is an implicit activity and the client
5779  * may not notice and continue using the old     5437  * may not notice and continue using the old mode. Avoid giving out a delegation
5780  * on setuid/setgid files when the client is     5438  * on setuid/setgid files when the client is requesting an open for write.
5781  */                                              5439  */
5782 static int                                       5440 static int
5783 nfsd4_verify_setuid_write(struct nfsd4_open *    5441 nfsd4_verify_setuid_write(struct nfsd4_open *open, struct nfsd_file *nf)
5784 {                                                5442 {
5785         struct inode *inode = file_inode(nf->    5443         struct inode *inode = file_inode(nf->nf_file);
5786                                                  5444 
5787         if ((open->op_share_access & NFS4_SHA    5445         if ((open->op_share_access & NFS4_SHARE_ACCESS_WRITE) &&
5788             (inode->i_mode & (S_ISUID|S_ISGID    5446             (inode->i_mode & (S_ISUID|S_ISGID)))
5789                 return -EAGAIN;                  5447                 return -EAGAIN;
5790         return 0;                                5448         return 0;
5791 }                                                5449 }
5792                                                  5450 
5793 static struct nfs4_delegation *                  5451 static struct nfs4_delegation *
5794 nfs4_set_delegation(struct nfsd4_open *open,     5452 nfs4_set_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp,
5795                     struct svc_fh *parent)       5453                     struct svc_fh *parent)
5796 {                                                5454 {
5797         int status = 0;                          5455         int status = 0;
5798         struct nfs4_client *clp = stp->st_sti    5456         struct nfs4_client *clp = stp->st_stid.sc_client;
5799         struct nfs4_file *fp = stp->st_stid.s    5457         struct nfs4_file *fp = stp->st_stid.sc_file;
5800         struct nfs4_clnt_odstate *odstate = s    5458         struct nfs4_clnt_odstate *odstate = stp->st_clnt_odstate;
5801         struct nfs4_delegation *dp;              5459         struct nfs4_delegation *dp;
5802         struct nfsd_file *nf = NULL;             5460         struct nfsd_file *nf = NULL;
5803         struct file_lease *fl;                !! 5461         struct file_lock *fl;
5804         u32 dl_type;                             5462         u32 dl_type;
5805                                                  5463 
5806         /*                                       5464         /*
5807          * The fi_had_conflict and nfs_get_ex    5465          * The fi_had_conflict and nfs_get_existing_delegation checks
5808          * here are just optimizations; we'll    5466          * here are just optimizations; we'll need to recheck them at
5809          * the end:                              5467          * the end:
5810          */                                      5468          */
5811         if (fp->fi_had_conflict)                 5469         if (fp->fi_had_conflict)
5812                 return ERR_PTR(-EAGAIN);         5470                 return ERR_PTR(-EAGAIN);
5813                                                  5471 
5814         /*                                       5472         /*
5815          * Try for a write delegation first.     5473          * Try for a write delegation first. RFC8881 section 10.4 says:
5816          *                                       5474          *
5817          *  "An OPEN_DELEGATE_WRITE delegatio    5475          *  "An OPEN_DELEGATE_WRITE delegation allows the client to handle,
5818          *   on its own, all opens."             5476          *   on its own, all opens."
5819          *                                       5477          *
5820          * Furthermore the client can use a w    5478          * Furthermore the client can use a write delegation for most READ
5821          * operations as well, so we require     5479          * operations as well, so we require a O_RDWR file here.
5822          *                                       5480          *
5823          * Offer a write delegation in the ca    5481          * Offer a write delegation in the case of a BOTH open, and ensure
5824          * we get the O_RDWR descriptor.         5482          * we get the O_RDWR descriptor.
5825          */                                      5483          */
5826         if ((open->op_share_access & NFS4_SHA    5484         if ((open->op_share_access & NFS4_SHARE_ACCESS_BOTH) == NFS4_SHARE_ACCESS_BOTH) {
5827                 nf = find_rw_file(fp);           5485                 nf = find_rw_file(fp);
5828                 dl_type = NFS4_OPEN_DELEGATE_    5486                 dl_type = NFS4_OPEN_DELEGATE_WRITE;
5829         }                                        5487         }
5830                                                  5488 
5831         /*                                       5489         /*
5832          * If the file is being opened O_RDON    5490          * If the file is being opened O_RDONLY or we couldn't get a O_RDWR
5833          * file for some reason, then try for    5491          * file for some reason, then try for a read delegation instead.
5834          */                                      5492          */
5835         if (!nf && (open->op_share_access & N    5493         if (!nf && (open->op_share_access & NFS4_SHARE_ACCESS_READ)) {
5836                 nf = find_readable_file(fp);     5494                 nf = find_readable_file(fp);
5837                 dl_type = NFS4_OPEN_DELEGATE_    5495                 dl_type = NFS4_OPEN_DELEGATE_READ;
5838         }                                        5496         }
5839                                                  5497 
5840         if (!nf)                                 5498         if (!nf)
5841                 return ERR_PTR(-EAGAIN);         5499                 return ERR_PTR(-EAGAIN);
5842                                                  5500 
5843         spin_lock(&state_lock);                  5501         spin_lock(&state_lock);
5844         spin_lock(&fp->fi_lock);                 5502         spin_lock(&fp->fi_lock);
5845         if (nfs4_delegation_exists(clp, fp))     5503         if (nfs4_delegation_exists(clp, fp))
5846                 status = -EAGAIN;                5504                 status = -EAGAIN;
5847         else if (nfsd4_verify_setuid_write(op    5505         else if (nfsd4_verify_setuid_write(open, nf))
5848                 status = -EAGAIN;                5506                 status = -EAGAIN;
5849         else if (!fp->fi_deleg_file) {           5507         else if (!fp->fi_deleg_file) {
5850                 fp->fi_deleg_file = nf;          5508                 fp->fi_deleg_file = nf;
5851                 /* increment early to prevent    5509                 /* increment early to prevent fi_deleg_file from being
5852                  * cleared */                    5510                  * cleared */
5853                 fp->fi_delegees = 1;             5511                 fp->fi_delegees = 1;
5854                 nf = NULL;                       5512                 nf = NULL;
5855         } else                                   5513         } else
5856                 fp->fi_delegees++;               5514                 fp->fi_delegees++;
5857         spin_unlock(&fp->fi_lock);               5515         spin_unlock(&fp->fi_lock);
5858         spin_unlock(&state_lock);                5516         spin_unlock(&state_lock);
5859         if (nf)                                  5517         if (nf)
5860                 nfsd_file_put(nf);               5518                 nfsd_file_put(nf);
5861         if (status)                              5519         if (status)
5862                 return ERR_PTR(status);          5520                 return ERR_PTR(status);
5863                                                  5521 
5864         status = -ENOMEM;                        5522         status = -ENOMEM;
5865         dp = alloc_init_deleg(clp, fp, odstat    5523         dp = alloc_init_deleg(clp, fp, odstate, dl_type);
5866         if (!dp)                                 5524         if (!dp)
5867                 goto out_delegees;               5525                 goto out_delegees;
5868                                                  5526 
5869         fl = nfs4_alloc_init_lease(dp, dl_typ    5527         fl = nfs4_alloc_init_lease(dp, dl_type);
5870         if (!fl)                                 5528         if (!fl)
5871                 goto out_clnt_odstate;           5529                 goto out_clnt_odstate;
5872                                                  5530 
5873         status = kernel_setlease(fp->fi_deleg !! 5531         status = vfs_setlease(fp->fi_deleg_file->nf_file, fl->fl_type, &fl, NULL);
5874                                       fl->c.f << 
5875         if (fl)                                  5532         if (fl)
5876                 locks_free_lease(fl);         !! 5533                 locks_free_lock(fl);
5877         if (status)                              5534         if (status)
5878                 goto out_clnt_odstate;           5535                 goto out_clnt_odstate;
5879                                                  5536 
5880         if (parent) {                            5537         if (parent) {
5881                 status = nfsd4_verify_deleg_d    5538                 status = nfsd4_verify_deleg_dentry(open, fp, parent);
5882                 if (status)                      5539                 if (status)
5883                         goto out_unlock;         5540                         goto out_unlock;
5884         }                                        5541         }
5885                                                  5542 
5886         status = nfsd4_check_conflicting_open    5543         status = nfsd4_check_conflicting_opens(clp, fp);
5887         if (status)                              5544         if (status)
5888                 goto out_unlock;                 5545                 goto out_unlock;
5889                                                  5546 
5890         /*                                       5547         /*
5891          * Now that the deleg is set, check a    5548          * Now that the deleg is set, check again to ensure that nothing
5892          * raced in and changed the mode whil !! 5549          * raced in and changed the mode while we weren't lookng.
5893          */                                      5550          */
5894         status = nfsd4_verify_setuid_write(op    5551         status = nfsd4_verify_setuid_write(open, fp->fi_deleg_file);
5895         if (status)                              5552         if (status)
5896                 goto out_unlock;                 5553                 goto out_unlock;
5897                                                  5554 
5898         status = -EAGAIN;                        5555         status = -EAGAIN;
5899         if (fp->fi_had_conflict)                 5556         if (fp->fi_had_conflict)
5900                 goto out_unlock;                 5557                 goto out_unlock;
5901                                                  5558 
5902         spin_lock(&state_lock);                  5559         spin_lock(&state_lock);
5903         spin_lock(&clp->cl_lock);             << 
5904         spin_lock(&fp->fi_lock);                 5560         spin_lock(&fp->fi_lock);
5905         status = hash_delegation_locked(dp, f    5561         status = hash_delegation_locked(dp, fp);
5906         spin_unlock(&fp->fi_lock);               5562         spin_unlock(&fp->fi_lock);
5907         spin_unlock(&clp->cl_lock);           << 
5908         spin_unlock(&state_lock);                5563         spin_unlock(&state_lock);
5909                                                  5564 
5910         if (status)                              5565         if (status)
5911                 goto out_unlock;                 5566                 goto out_unlock;
5912                                                  5567 
5913         return dp;                               5568         return dp;
5914 out_unlock:                                      5569 out_unlock:
5915         kernel_setlease(fp->fi_deleg_file->nf !! 5570         vfs_setlease(fp->fi_deleg_file->nf_file, F_UNLCK, NULL, (void **)&dp);
5916 out_clnt_odstate:                                5571 out_clnt_odstate:
5917         put_clnt_odstate(dp->dl_clnt_odstate)    5572         put_clnt_odstate(dp->dl_clnt_odstate);
5918         nfs4_put_stid(&dp->dl_stid);             5573         nfs4_put_stid(&dp->dl_stid);
5919 out_delegees:                                    5574 out_delegees:
5920         put_deleg_file(fp);                      5575         put_deleg_file(fp);
5921         return ERR_PTR(status);                  5576         return ERR_PTR(status);
5922 }                                                5577 }
5923                                                  5578 
5924 static void nfsd4_open_deleg_none_ext(struct     5579 static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
5925 {                                                5580 {
5926         open->op_delegate_type = NFS4_OPEN_DE    5581         open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5927         if (status == -EAGAIN)                   5582         if (status == -EAGAIN)
5928                 open->op_why_no_deleg = WND4_    5583                 open->op_why_no_deleg = WND4_CONTENTION;
5929         else {                                   5584         else {
5930                 open->op_why_no_deleg = WND4_    5585                 open->op_why_no_deleg = WND4_RESOURCE;
5931                 switch (open->op_deleg_want)     5586                 switch (open->op_deleg_want) {
5932                 case NFS4_SHARE_WANT_READ_DEL    5587                 case NFS4_SHARE_WANT_READ_DELEG:
5933                 case NFS4_SHARE_WANT_WRITE_DE    5588                 case NFS4_SHARE_WANT_WRITE_DELEG:
5934                 case NFS4_SHARE_WANT_ANY_DELE    5589                 case NFS4_SHARE_WANT_ANY_DELEG:
5935                         break;                   5590                         break;
5936                 case NFS4_SHARE_WANT_CANCEL:     5591                 case NFS4_SHARE_WANT_CANCEL:
5937                         open->op_why_no_deleg    5592                         open->op_why_no_deleg = WND4_CANCELLED;
5938                         break;                   5593                         break;
5939                 case NFS4_SHARE_WANT_NO_DELEG    5594                 case NFS4_SHARE_WANT_NO_DELEG:
5940                         WARN_ON_ONCE(1);         5595                         WARN_ON_ONCE(1);
5941                 }                                5596                 }
5942         }                                        5597         }
5943 }                                                5598 }
5944                                                  5599 
5945 static bool                                   << 
5946 nfs4_delegation_stat(struct nfs4_delegation * << 
5947                      struct kstat *stat)      << 
5948 {                                             << 
5949         struct nfsd_file *nf = find_rw_file(d << 
5950         struct path path;                     << 
5951         int rc;                               << 
5952                                               << 
5953         if (!nf)                              << 
5954                 return false;                 << 
5955                                               << 
5956         path.mnt = currentfh->fh_export->ex_p << 
5957         path.dentry = file_dentry(nf->nf_file << 
5958                                               << 
5959         rc = vfs_getattr(&path, stat,         << 
5960                          (STATX_SIZE | STATX_ << 
5961                          AT_STATX_SYNC_AS_STA << 
5962                                               << 
5963         nfsd_file_put(nf);                    << 
5964         return rc == 0;                       << 
5965 }                                             << 
5966                                               << 
5967 /*                                               5600 /*
5968  * The Linux NFS server does not offer write     5601  * The Linux NFS server does not offer write delegations to NFSv4.0
5969  * clients in order to avoid conflicts betwee    5602  * clients in order to avoid conflicts between write delegations and
5970  * GETATTRs requesting CHANGE or SIZE attribu    5603  * GETATTRs requesting CHANGE or SIZE attributes.
5971  *                                               5604  *
5972  * With NFSv4.1 and later minorversions, the     5605  * With NFSv4.1 and later minorversions, the SEQUENCE operation that
5973  * begins each COMPOUND contains a client ID.    5606  * begins each COMPOUND contains a client ID. Delegation recall can
5974  * be avoided when the server recognizes the     5607  * be avoided when the server recognizes the client sending a
5975  * GETATTR also holds write delegation it con    5608  * GETATTR also holds write delegation it conflicts with.
5976  *                                               5609  *
5977  * However, the NFSv4.0 protocol does not ena    5610  * However, the NFSv4.0 protocol does not enable a server to
5978  * determine that a GETATTR originated from t    5611  * determine that a GETATTR originated from the client holding the
5979  * conflicting delegation versus coming from     5612  * conflicting delegation versus coming from some other client. Per
5980  * RFC 7530 Section 16.7.5, the server must r    5613  * RFC 7530 Section 16.7.5, the server must recall or send a
5981  * CB_GETATTR even when the GETATTR originate    5614  * CB_GETATTR even when the GETATTR originates from the client that
5982  * holds the conflicting delegation.             5615  * holds the conflicting delegation.
5983  *                                               5616  *
5984  * An NFSv4.0 client can trigger a pathologic    5617  * An NFSv4.0 client can trigger a pathological situation if it
5985  * always sends a DELEGRETURN preceded by a c    5618  * always sends a DELEGRETURN preceded by a conflicting GETATTR in
5986  * the same COMPOUND. COMPOUND execution will    5619  * the same COMPOUND. COMPOUND execution will always stop at the
5987  * GETATTR and the DELEGRETURN will never get    5620  * GETATTR and the DELEGRETURN will never get executed. The server
5988  * eventually revokes the delegation, which c    5621  * eventually revokes the delegation, which can result in loss of
5989  * open or lock state.                           5622  * open or lock state.
5990  */                                              5623  */
5991 static void                                      5624 static void
5992 nfs4_open_delegation(struct nfsd4_open *open,    5625 nfs4_open_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp,
5993                      struct svc_fh *currentfh    5626                      struct svc_fh *currentfh)
5994 {                                                5627 {
5995         struct nfs4_delegation *dp;              5628         struct nfs4_delegation *dp;
5996         struct nfs4_openowner *oo = openowner    5629         struct nfs4_openowner *oo = openowner(stp->st_stateowner);
5997         struct nfs4_client *clp = stp->st_sti    5630         struct nfs4_client *clp = stp->st_stid.sc_client;
5998         struct svc_fh *parent = NULL;            5631         struct svc_fh *parent = NULL;
5999         int cb_up;                               5632         int cb_up;
6000         int status = 0;                          5633         int status = 0;
6001         struct kstat stat;                    << 
6002                                                  5634 
6003         cb_up = nfsd4_cb_channel_good(oo->oo_    5635         cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
6004         open->op_recall = false;                 5636         open->op_recall = false;
6005         switch (open->op_claim_type) {           5637         switch (open->op_claim_type) {
6006                 case NFS4_OPEN_CLAIM_PREVIOUS    5638                 case NFS4_OPEN_CLAIM_PREVIOUS:
6007                         if (!cb_up)              5639                         if (!cb_up)
6008                                 open->op_reca    5640                                 open->op_recall = true;
6009                         break;                   5641                         break;
6010                 case NFS4_OPEN_CLAIM_NULL:       5642                 case NFS4_OPEN_CLAIM_NULL:
6011                         parent = currentfh;      5643                         parent = currentfh;
6012                         fallthrough;             5644                         fallthrough;
6013                 case NFS4_OPEN_CLAIM_FH:         5645                 case NFS4_OPEN_CLAIM_FH:
6014                         /*                       5646                         /*
6015                          * Let's not give out    5647                          * Let's not give out any delegations till everyone's
6016                          * had the chance to     5648                          * had the chance to reclaim theirs, *and* until
6017                          * NLM locks have all    5649                          * NLM locks have all been reclaimed:
6018                          */                      5650                          */
6019                         if (locks_in_grace(cl    5651                         if (locks_in_grace(clp->net))
6020                                 goto out_no_d    5652                                 goto out_no_deleg;
6021                         if (!cb_up || !(oo->o    5653                         if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
6022                                 goto out_no_d    5654                                 goto out_no_deleg;
6023                         if (open->op_share_ac    5655                         if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE &&
6024                                         !clp-    5656                                         !clp->cl_minorversion)
6025                                 goto out_no_d    5657                                 goto out_no_deleg;
6026                         break;                   5658                         break;
6027                 default:                         5659                 default:
6028                         goto out_no_deleg;       5660                         goto out_no_deleg;
6029         }                                        5661         }
6030         dp = nfs4_set_delegation(open, stp, p    5662         dp = nfs4_set_delegation(open, stp, parent);
6031         if (IS_ERR(dp))                          5663         if (IS_ERR(dp))
6032                 goto out_no_deleg;               5664                 goto out_no_deleg;
6033                                                  5665 
6034         memcpy(&open->op_delegate_stateid, &d    5666         memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
6035                                                  5667 
6036         if (open->op_share_access & NFS4_SHAR    5668         if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE) {
6037                 if (!nfs4_delegation_stat(dp, << 
6038                         nfs4_put_stid(&dp->dl << 
6039                         destroy_delegation(dp << 
6040                         goto out_no_deleg;    << 
6041                 }                             << 
6042                 open->op_delegate_type = NFS4    5669                 open->op_delegate_type = NFS4_OPEN_DELEGATE_WRITE;
6043                 dp->dl_cb_fattr.ncf_cur_fsize << 
6044                 dp->dl_cb_fattr.ncf_initial_c << 
6045                         nfsd4_change_attribut << 
6046                 trace_nfsd_deleg_write(&dp->d    5670                 trace_nfsd_deleg_write(&dp->dl_stid.sc_stateid);
6047         } else {                                 5671         } else {
6048                 open->op_delegate_type = NFS4    5672                 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
6049                 trace_nfsd_deleg_read(&dp->dl    5673                 trace_nfsd_deleg_read(&dp->dl_stid.sc_stateid);
6050         }                                        5674         }
6051         nfs4_put_stid(&dp->dl_stid);             5675         nfs4_put_stid(&dp->dl_stid);
6052         return;                                  5676         return;
6053 out_no_deleg:                                    5677 out_no_deleg:
6054         open->op_delegate_type = NFS4_OPEN_DE    5678         open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
6055         if (open->op_claim_type == NFS4_OPEN_    5679         if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
6056             open->op_delegate_type != NFS4_OP    5680             open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
6057                 dprintk("NFSD: WARNING: refus    5681                 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
6058                 open->op_recall = true;          5682                 open->op_recall = true;
6059         }                                        5683         }
6060                                                  5684 
6061         /* 4.1 client asking for a delegation    5685         /* 4.1 client asking for a delegation? */
6062         if (open->op_deleg_want)                 5686         if (open->op_deleg_want)
6063                 nfsd4_open_deleg_none_ext(ope    5687                 nfsd4_open_deleg_none_ext(open, status);
6064         return;                                  5688         return;
6065 }                                                5689 }
6066                                                  5690 
6067 static void nfsd4_deleg_xgrade_none_ext(struc    5691 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
6068                                         struc    5692                                         struct nfs4_delegation *dp)
6069 {                                                5693 {
6070         if (open->op_deleg_want == NFS4_SHARE    5694         if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
6071             dp->dl_type == NFS4_OPEN_DELEGATE    5695             dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
6072                 open->op_delegate_type = NFS4    5696                 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
6073                 open->op_why_no_deleg = WND4_    5697                 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
6074         } else if (open->op_deleg_want == NFS    5698         } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
6075                    dp->dl_type == NFS4_OPEN_D    5699                    dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
6076                 open->op_delegate_type = NFS4    5700                 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
6077                 open->op_why_no_deleg = WND4_    5701                 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
6078         }                                        5702         }
6079         /* Otherwise the client must be confu    5703         /* Otherwise the client must be confused wanting a delegation
6080          * it already has, therefore we don't    5704          * it already has, therefore we don't return
6081          * NFS4_OPEN_DELEGATE_NONE_EXT and re    5705          * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
6082          */                                      5706          */
6083 }                                                5707 }
6084                                                  5708 
6085 /**                                              5709 /**
6086  * nfsd4_process_open2 - finish open processi    5710  * nfsd4_process_open2 - finish open processing
6087  * @rqstp: the RPC transaction being executed    5711  * @rqstp: the RPC transaction being executed
6088  * @current_fh: NFSv4 COMPOUND's current file    5712  * @current_fh: NFSv4 COMPOUND's current filehandle
6089  * @open: OPEN arguments                         5713  * @open: OPEN arguments
6090  *                                               5714  *
6091  * If successful, (1) truncate the file if op    5715  * If successful, (1) truncate the file if open->op_truncate was
6092  * set, (2) set open->op_stateid, (3) set ope    5716  * set, (2) set open->op_stateid, (3) set open->op_delegation.
6093  *                                               5717  *
6094  * Returns %nfs_ok on success; otherwise an n    5718  * Returns %nfs_ok on success; otherwise an nfs4stat value in
6095  * network byte order is returned.               5719  * network byte order is returned.
6096  */                                              5720  */
6097 __be32                                           5721 __be32
6098 nfsd4_process_open2(struct svc_rqst *rqstp, s    5722 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
6099 {                                                5723 {
6100         struct nfsd4_compoundres *resp = rqst    5724         struct nfsd4_compoundres *resp = rqstp->rq_resp;
6101         struct nfs4_client *cl = open->op_ope    5725         struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
6102         struct nfs4_file *fp = NULL;             5726         struct nfs4_file *fp = NULL;
6103         struct nfs4_ol_stateid *stp = NULL;      5727         struct nfs4_ol_stateid *stp = NULL;
6104         struct nfs4_delegation *dp = NULL;       5728         struct nfs4_delegation *dp = NULL;
6105         __be32 status;                           5729         __be32 status;
6106         bool new_stp = false;                    5730         bool new_stp = false;
6107                                                  5731 
6108         /*                                       5732         /*
6109          * Lookup file; if found, lookup stat    5733          * Lookup file; if found, lookup stateid and check open request,
6110          * and check for delegations in the p    5734          * and check for delegations in the process of being recalled.
6111          * If not found, create the nfs4_file    5735          * If not found, create the nfs4_file struct
6112          */                                      5736          */
6113         fp = nfsd4_file_hash_insert(open->op_    5737         fp = nfsd4_file_hash_insert(open->op_file, current_fh);
6114         if (unlikely(!fp))                       5738         if (unlikely(!fp))
6115                 return nfserr_jukebox;           5739                 return nfserr_jukebox;
6116         if (fp != open->op_file) {               5740         if (fp != open->op_file) {
6117                 status = nfs4_check_deleg(cl,    5741                 status = nfs4_check_deleg(cl, open, &dp);
6118                 if (status)                      5742                 if (status)
6119                         goto out;                5743                         goto out;
6120                 stp = nfsd4_find_and_lock_exi    5744                 stp = nfsd4_find_and_lock_existing_open(fp, open);
6121         } else {                                 5745         } else {
6122                 open->op_file = NULL;            5746                 open->op_file = NULL;
6123                 status = nfserr_bad_stateid;     5747                 status = nfserr_bad_stateid;
6124                 if (nfsd4_is_deleg_cur(open))    5748                 if (nfsd4_is_deleg_cur(open))
6125                         goto out;                5749                         goto out;
6126         }                                        5750         }
6127                                                  5751 
6128         if (!stp) {                              5752         if (!stp) {
6129                 stp = init_open_stateid(fp, o    5753                 stp = init_open_stateid(fp, open);
6130                 if (!open->op_stp)               5754                 if (!open->op_stp)
6131                         new_stp = true;          5755                         new_stp = true;
6132         }                                        5756         }
6133                                                  5757 
6134         /*                                       5758         /*
6135          * OPEN the file, or upgrade an exist    5759          * OPEN the file, or upgrade an existing OPEN.
6136          * If truncate fails, the OPEN fails.    5760          * If truncate fails, the OPEN fails.
6137          *                                       5761          *
6138          * stp is already locked.                5762          * stp is already locked.
6139          */                                      5763          */
6140         if (!new_stp) {                          5764         if (!new_stp) {
6141                 /* Stateid was found, this is    5765                 /* Stateid was found, this is an OPEN upgrade */
6142                 status = nfs4_upgrade_open(rq    5766                 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
6143                 if (status) {                    5767                 if (status) {
6144                         mutex_unlock(&stp->st    5768                         mutex_unlock(&stp->st_mutex);
6145                         goto out;                5769                         goto out;
6146                 }                                5770                 }
6147         } else {                                 5771         } else {
6148                 status = nfs4_get_vfs_file(rq    5772                 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open, true);
6149                 if (status) {                    5773                 if (status) {
                                                   >> 5774                         stp->st_stid.sc_type = NFS4_CLOSED_STID;
6150                         release_open_stateid(    5775                         release_open_stateid(stp);
6151                         mutex_unlock(&stp->st    5776                         mutex_unlock(&stp->st_mutex);
6152                         goto out;                5777                         goto out;
6153                 }                                5778                 }
6154                                                  5779 
6155                 stp->st_clnt_odstate = find_o    5780                 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
6156                                                  5781                                                         open->op_odstate);
6157                 if (stp->st_clnt_odstate == o    5782                 if (stp->st_clnt_odstate == open->op_odstate)
6158                         open->op_odstate = NU    5783                         open->op_odstate = NULL;
6159         }                                        5784         }
6160                                                  5785 
6161         nfs4_inc_and_copy_stateid(&open->op_s    5786         nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
6162         mutex_unlock(&stp->st_mutex);            5787         mutex_unlock(&stp->st_mutex);
6163                                                  5788 
6164         if (nfsd4_has_session(&resp->cstate))    5789         if (nfsd4_has_session(&resp->cstate)) {
6165                 if (open->op_deleg_want & NFS    5790                 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
6166                         open->op_delegate_typ    5791                         open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
6167                         open->op_why_no_deleg    5792                         open->op_why_no_deleg = WND4_NOT_WANTED;
6168                         goto nodeleg;            5793                         goto nodeleg;
6169                 }                                5794                 }
6170         }                                        5795         }
6171                                                  5796 
6172         /*                                       5797         /*
6173         * Attempt to hand out a delegation. N    5798         * Attempt to hand out a delegation. No error return, because the
6174         * OPEN succeeds even if we fail.         5799         * OPEN succeeds even if we fail.
6175         */                                       5800         */
6176         nfs4_open_delegation(open, stp, &resp    5801         nfs4_open_delegation(open, stp, &resp->cstate.current_fh);
6177 nodeleg:                                         5802 nodeleg:
6178         status = nfs_ok;                         5803         status = nfs_ok;
6179         trace_nfsd_open(&stp->st_stid.sc_stat    5804         trace_nfsd_open(&stp->st_stid.sc_stateid);
6180 out:                                             5805 out:
6181         /* 4.1 client trying to upgrade/downg    5806         /* 4.1 client trying to upgrade/downgrade delegation? */
6182         if (open->op_delegate_type == NFS4_OP    5807         if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
6183             open->op_deleg_want)                 5808             open->op_deleg_want)
6184                 nfsd4_deleg_xgrade_none_ext(o    5809                 nfsd4_deleg_xgrade_none_ext(open, dp);
6185                                                  5810 
6186         if (fp)                                  5811         if (fp)
6187                 put_nfs4_file(fp);               5812                 put_nfs4_file(fp);
6188         if (status == 0 && open->op_claim_typ    5813         if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
6189                 open->op_openowner->oo_flags     5814                 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
6190         /*                                       5815         /*
6191         * To finish the open response, we jus    5816         * To finish the open response, we just need to set the rflags.
6192         */                                       5817         */
6193         open->op_rflags = NFS4_OPEN_RESULT_LO    5818         open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
6194         if (nfsd4_has_session(&resp->cstate))    5819         if (nfsd4_has_session(&resp->cstate))
6195                 open->op_rflags |= NFS4_OPEN_    5820                 open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK;
6196         else if (!(open->op_openowner->oo_fla    5821         else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED))
6197                 open->op_rflags |= NFS4_OPEN_    5822                 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
6198                                                  5823 
6199         if (dp)                                  5824         if (dp)
6200                 nfs4_put_stid(&dp->dl_stid);     5825                 nfs4_put_stid(&dp->dl_stid);
6201         if (stp)                                 5826         if (stp)
6202                 nfs4_put_stid(&stp->st_stid);    5827                 nfs4_put_stid(&stp->st_stid);
6203                                                  5828 
6204         return status;                           5829         return status;
6205 }                                                5830 }
6206                                                  5831 
6207 void nfsd4_cleanup_open_state(struct nfsd4_co    5832 void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
6208                               struct nfsd4_op    5833                               struct nfsd4_open *open)
6209 {                                                5834 {
6210         if (open->op_openowner)               !! 5835         if (open->op_openowner) {
6211                 nfs4_put_stateowner(&open->op !! 5836                 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
                                                   >> 5837 
                                                   >> 5838                 nfsd4_cstate_assign_replay(cstate, so);
                                                   >> 5839                 nfs4_put_stateowner(so);
                                                   >> 5840         }
6212         if (open->op_file)                       5841         if (open->op_file)
6213                 kmem_cache_free(file_slab, op    5842                 kmem_cache_free(file_slab, open->op_file);
6214         if (open->op_stp)                        5843         if (open->op_stp)
6215                 nfs4_put_stid(&open->op_stp->    5844                 nfs4_put_stid(&open->op_stp->st_stid);
6216         if (open->op_odstate)                    5845         if (open->op_odstate)
6217                 kmem_cache_free(odstate_slab,    5846                 kmem_cache_free(odstate_slab, open->op_odstate);
6218 }                                                5847 }
6219                                                  5848 
6220 __be32                                           5849 __be32
6221 nfsd4_renew(struct svc_rqst *rqstp, struct nf    5850 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6222             union nfsd4_op_u *u)                 5851             union nfsd4_op_u *u)
6223 {                                                5852 {
6224         clientid_t *clid = &u->renew;            5853         clientid_t *clid = &u->renew;
6225         struct nfs4_client *clp;                 5854         struct nfs4_client *clp;
6226         __be32 status;                           5855         __be32 status;
6227         struct nfsd_net *nn = net_generic(SVC    5856         struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6228                                                  5857 
6229         trace_nfsd_clid_renew(clid);             5858         trace_nfsd_clid_renew(clid);
6230         status = set_client(clid, cstate, nn)    5859         status = set_client(clid, cstate, nn);
6231         if (status)                              5860         if (status)
6232                 return status;                   5861                 return status;
6233         clp = cstate->clp;                       5862         clp = cstate->clp;
6234         if (!list_empty(&clp->cl_delegations)    5863         if (!list_empty(&clp->cl_delegations)
6235                         && clp->cl_cb_state !    5864                         && clp->cl_cb_state != NFSD4_CB_UP)
6236                 return nfserr_cb_path_down;      5865                 return nfserr_cb_path_down;
6237         return nfs_ok;                           5866         return nfs_ok;
6238 }                                                5867 }
6239                                                  5868 
6240 void                                             5869 void
6241 nfsd4_end_grace(struct nfsd_net *nn)             5870 nfsd4_end_grace(struct nfsd_net *nn)
6242 {                                                5871 {
6243         /* do nothing if grace period already    5872         /* do nothing if grace period already ended */
6244         if (nn->grace_ended)                     5873         if (nn->grace_ended)
6245                 return;                          5874                 return;
6246                                                  5875 
6247         trace_nfsd_grace_complete(nn);           5876         trace_nfsd_grace_complete(nn);
6248         nn->grace_ended = true;                  5877         nn->grace_ended = true;
6249         /*                                       5878         /*
6250          * If the server goes down again righ    5879          * If the server goes down again right now, an NFSv4
6251          * client will still be allowed to re    5880          * client will still be allowed to reclaim after it comes back up,
6252          * even if it hasn't yet had a chance    5881          * even if it hasn't yet had a chance to reclaim state this time.
6253          *                                       5882          *
6254          */                                      5883          */
6255         nfsd4_record_grace_done(nn);             5884         nfsd4_record_grace_done(nn);
6256         /*                                       5885         /*
6257          * At this point, NFSv4 clients can s    5886          * At this point, NFSv4 clients can still reclaim.  But if the
6258          * server crashes, any that have not     5887          * server crashes, any that have not yet reclaimed will be out
6259          * of luck on the next boot.             5888          * of luck on the next boot.
6260          *                                       5889          *
6261          * (NFSv4.1+ clients are considered t    5890          * (NFSv4.1+ clients are considered to have reclaimed once they
6262          * call RECLAIM_COMPLETE.  NFSv4.0 cl    5891          * call RECLAIM_COMPLETE.  NFSv4.0 clients are considered to
6263          * have reclaimed after their first O    5892          * have reclaimed after their first OPEN.)
6264          */                                      5893          */
6265         locks_end_grace(&nn->nfsd4_manager);     5894         locks_end_grace(&nn->nfsd4_manager);
6266         /*                                       5895         /*
6267          * At this point, and once lockd and/    5896          * At this point, and once lockd and/or any other containers
6268          * exit their grace period, further r    5897          * exit their grace period, further reclaims will fail and
6269          * regular locking can resume.           5898          * regular locking can resume.
6270          */                                      5899          */
6271 }                                                5900 }
6272                                                  5901 
6273 /*                                               5902 /*
6274  * If we've waited a lease period but there a    5903  * If we've waited a lease period but there are still clients trying to
6275  * reclaim, wait a little longer to give them    5904  * reclaim, wait a little longer to give them a chance to finish.
6276  */                                              5905  */
6277 static bool clients_still_reclaiming(struct n    5906 static bool clients_still_reclaiming(struct nfsd_net *nn)
6278 {                                                5907 {
6279         time64_t double_grace_period_end = nn    5908         time64_t double_grace_period_end = nn->boot_time +
6280                                            2     5909                                            2 * nn->nfsd4_lease;
6281                                                  5910 
6282         if (nn->track_reclaim_completes &&       5911         if (nn->track_reclaim_completes &&
6283                         atomic_read(&nn->nr_r    5912                         atomic_read(&nn->nr_reclaim_complete) ==
6284                         nn->reclaim_str_hasht    5913                         nn->reclaim_str_hashtbl_size)
6285                 return false;                    5914                 return false;
6286         if (!nn->somebody_reclaimed)             5915         if (!nn->somebody_reclaimed)
6287                 return false;                    5916                 return false;
6288         nn->somebody_reclaimed = false;          5917         nn->somebody_reclaimed = false;
6289         /*                                       5918         /*
6290          * If we've given them *two* lease ti    5919          * If we've given them *two* lease times to reclaim, and they're
6291          * still not done, give up:              5920          * still not done, give up:
6292          */                                      5921          */
6293         if (ktime_get_boottime_seconds() > do    5922         if (ktime_get_boottime_seconds() > double_grace_period_end)
6294                 return false;                    5923                 return false;
6295         return true;                             5924         return true;
6296 }                                                5925 }
6297                                                  5926 
6298 struct laundry_time {                            5927 struct laundry_time {
6299         time64_t cutoff;                         5928         time64_t cutoff;
6300         time64_t new_timeo;                      5929         time64_t new_timeo;
6301 };                                               5930 };
6302                                                  5931 
6303 static bool state_expired(struct laundry_time    5932 static bool state_expired(struct laundry_time *lt, time64_t last_refresh)
6304 {                                                5933 {
6305         time64_t time_remaining;                 5934         time64_t time_remaining;
6306                                                  5935 
6307         if (last_refresh < lt->cutoff)           5936         if (last_refresh < lt->cutoff)
6308                 return true;                     5937                 return true;
6309         time_remaining = last_refresh - lt->c    5938         time_remaining = last_refresh - lt->cutoff;
6310         lt->new_timeo = min(lt->new_timeo, ti    5939         lt->new_timeo = min(lt->new_timeo, time_remaining);
6311         return false;                            5940         return false;
6312 }                                                5941 }
6313                                                  5942 
6314 #ifdef CONFIG_NFSD_V4_2_INTER_SSC                5943 #ifdef CONFIG_NFSD_V4_2_INTER_SSC
6315 void nfsd4_ssc_init_umount_work(struct nfsd_n    5944 void nfsd4_ssc_init_umount_work(struct nfsd_net *nn)
6316 {                                                5945 {
6317         spin_lock_init(&nn->nfsd_ssc_lock);      5946         spin_lock_init(&nn->nfsd_ssc_lock);
6318         INIT_LIST_HEAD(&nn->nfsd_ssc_mount_li    5947         INIT_LIST_HEAD(&nn->nfsd_ssc_mount_list);
6319         init_waitqueue_head(&nn->nfsd_ssc_wai    5948         init_waitqueue_head(&nn->nfsd_ssc_waitq);
6320 }                                                5949 }
                                                   >> 5950 EXPORT_SYMBOL_GPL(nfsd4_ssc_init_umount_work);
6321                                                  5951 
6322 /*                                               5952 /*
6323  * This is called when nfsd is being shutdown    5953  * This is called when nfsd is being shutdown, after all inter_ssc
6324  * cleanup were done, to destroy the ssc dela    5954  * cleanup were done, to destroy the ssc delayed unmount list.
6325  */                                              5955  */
6326 static void nfsd4_ssc_shutdown_umount(struct     5956 static void nfsd4_ssc_shutdown_umount(struct nfsd_net *nn)
6327 {                                                5957 {
6328         struct nfsd4_ssc_umount_item *ni = NU    5958         struct nfsd4_ssc_umount_item *ni = NULL;
6329         struct nfsd4_ssc_umount_item *tmp;       5959         struct nfsd4_ssc_umount_item *tmp;
6330                                                  5960 
6331         spin_lock(&nn->nfsd_ssc_lock);           5961         spin_lock(&nn->nfsd_ssc_lock);
6332         list_for_each_entry_safe(ni, tmp, &nn    5962         list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) {
6333                 list_del(&ni->nsui_list);        5963                 list_del(&ni->nsui_list);
6334                 spin_unlock(&nn->nfsd_ssc_loc    5964                 spin_unlock(&nn->nfsd_ssc_lock);
6335                 mntput(ni->nsui_vfsmount);       5965                 mntput(ni->nsui_vfsmount);
6336                 kfree(ni);                       5966                 kfree(ni);
6337                 spin_lock(&nn->nfsd_ssc_lock)    5967                 spin_lock(&nn->nfsd_ssc_lock);
6338         }                                        5968         }
6339         spin_unlock(&nn->nfsd_ssc_lock);         5969         spin_unlock(&nn->nfsd_ssc_lock);
6340 }                                                5970 }
6341                                                  5971 
6342 static void nfsd4_ssc_expire_umount(struct nf    5972 static void nfsd4_ssc_expire_umount(struct nfsd_net *nn)
6343 {                                                5973 {
6344         bool do_wakeup = false;                  5974         bool do_wakeup = false;
6345         struct nfsd4_ssc_umount_item *ni = NU    5975         struct nfsd4_ssc_umount_item *ni = NULL;
6346         struct nfsd4_ssc_umount_item *tmp;       5976         struct nfsd4_ssc_umount_item *tmp;
6347                                                  5977 
6348         spin_lock(&nn->nfsd_ssc_lock);           5978         spin_lock(&nn->nfsd_ssc_lock);
6349         list_for_each_entry_safe(ni, tmp, &nn    5979         list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) {
6350                 if (time_after(jiffies, ni->n    5980                 if (time_after(jiffies, ni->nsui_expire)) {
6351                         if (refcount_read(&ni    5981                         if (refcount_read(&ni->nsui_refcnt) > 1)
6352                                 continue;        5982                                 continue;
6353                                                  5983 
6354                         /* mark being unmount    5984                         /* mark being unmount */
6355                         ni->nsui_busy = true;    5985                         ni->nsui_busy = true;
6356                         spin_unlock(&nn->nfsd    5986                         spin_unlock(&nn->nfsd_ssc_lock);
6357                         mntput(ni->nsui_vfsmo    5987                         mntput(ni->nsui_vfsmount);
6358                         spin_lock(&nn->nfsd_s    5988                         spin_lock(&nn->nfsd_ssc_lock);
6359                                                  5989 
6360                         /* waiters need to st    5990                         /* waiters need to start from begin of list */
6361                         list_del(&ni->nsui_li    5991                         list_del(&ni->nsui_list);
6362                         kfree(ni);               5992                         kfree(ni);
6363                                                  5993 
6364                         /* wakeup ssc_connect    5994                         /* wakeup ssc_connect waiters */
6365                         do_wakeup = true;        5995                         do_wakeup = true;
6366                         continue;                5996                         continue;
6367                 }                                5997                 }
6368                 break;                           5998                 break;
6369         }                                        5999         }
6370         if (do_wakeup)                           6000         if (do_wakeup)
6371                 wake_up_all(&nn->nfsd_ssc_wai    6001                 wake_up_all(&nn->nfsd_ssc_waitq);
6372         spin_unlock(&nn->nfsd_ssc_lock);         6002         spin_unlock(&nn->nfsd_ssc_lock);
6373 }                                                6003 }
6374 #endif                                           6004 #endif
6375                                                  6005 
6376 /* Check if any lock belonging to this lockow    6006 /* Check if any lock belonging to this lockowner has any blockers */
6377 static bool                                      6007 static bool
6378 nfs4_lockowner_has_blockers(struct nfs4_locko    6008 nfs4_lockowner_has_blockers(struct nfs4_lockowner *lo)
6379 {                                                6009 {
6380         struct file_lock_context *ctx;           6010         struct file_lock_context *ctx;
6381         struct nfs4_ol_stateid *stp;             6011         struct nfs4_ol_stateid *stp;
6382         struct nfs4_file *nf;                    6012         struct nfs4_file *nf;
6383                                                  6013 
6384         list_for_each_entry(stp, &lo->lo_owne    6014         list_for_each_entry(stp, &lo->lo_owner.so_stateids, st_perstateowner) {
6385                 nf = stp->st_stid.sc_file;       6015                 nf = stp->st_stid.sc_file;
6386                 ctx = locks_inode_context(nf-    6016                 ctx = locks_inode_context(nf->fi_inode);
6387                 if (!ctx)                        6017                 if (!ctx)
6388                         continue;                6018                         continue;
6389                 if (locks_owner_has_blockers(    6019                 if (locks_owner_has_blockers(ctx, lo))
6390                         return true;             6020                         return true;
6391         }                                        6021         }
6392         return false;                            6022         return false;
6393 }                                                6023 }
6394                                                  6024 
6395 static bool                                      6025 static bool
6396 nfs4_anylock_blockers(struct nfs4_client *clp    6026 nfs4_anylock_blockers(struct nfs4_client *clp)
6397 {                                                6027 {
6398         int i;                                   6028         int i;
6399         struct nfs4_stateowner *so;              6029         struct nfs4_stateowner *so;
6400         struct nfs4_lockowner *lo;               6030         struct nfs4_lockowner *lo;
6401                                                  6031 
6402         if (atomic_read(&clp->cl_delegs_in_re    6032         if (atomic_read(&clp->cl_delegs_in_recall))
6403                 return true;                     6033                 return true;
6404         spin_lock(&clp->cl_lock);                6034         spin_lock(&clp->cl_lock);
6405         for (i = 0; i < OWNER_HASH_SIZE; i++)    6035         for (i = 0; i < OWNER_HASH_SIZE; i++) {
6406                 list_for_each_entry(so, &clp-    6036                 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[i],
6407                                 so_strhash) {    6037                                 so_strhash) {
6408                         if (so->so_is_open_ow    6038                         if (so->so_is_open_owner)
6409                                 continue;        6039                                 continue;
6410                         lo = lockowner(so);      6040                         lo = lockowner(so);
6411                         if (nfs4_lockowner_ha    6041                         if (nfs4_lockowner_has_blockers(lo)) {
6412                                 spin_unlock(&    6042                                 spin_unlock(&clp->cl_lock);
6413                                 return true;     6043                                 return true;
6414                         }                        6044                         }
6415                 }                                6045                 }
6416         }                                        6046         }
6417         spin_unlock(&clp->cl_lock);              6047         spin_unlock(&clp->cl_lock);
6418         return false;                            6048         return false;
6419 }                                                6049 }
6420                                                  6050 
6421 static void                                      6051 static void
6422 nfs4_get_client_reaplist(struct nfsd_net *nn,    6052 nfs4_get_client_reaplist(struct nfsd_net *nn, struct list_head *reaplist,
6423                                 struct laundr    6053                                 struct laundry_time *lt)
6424 {                                                6054 {
6425         unsigned int maxreap, reapcnt = 0;       6055         unsigned int maxreap, reapcnt = 0;
6426         struct list_head *pos, *next;            6056         struct list_head *pos, *next;
6427         struct nfs4_client *clp;                 6057         struct nfs4_client *clp;
6428                                                  6058 
6429         maxreap = (atomic_read(&nn->nfs4_clie    6059         maxreap = (atomic_read(&nn->nfs4_client_count) >= nn->nfs4_max_clients) ?
6430                         NFSD_CLIENT_MAX_TRIM_    6060                         NFSD_CLIENT_MAX_TRIM_PER_RUN : 0;
6431         INIT_LIST_HEAD(reaplist);                6061         INIT_LIST_HEAD(reaplist);
6432         spin_lock(&nn->client_lock);             6062         spin_lock(&nn->client_lock);
6433         list_for_each_safe(pos, next, &nn->cl    6063         list_for_each_safe(pos, next, &nn->client_lru) {
6434                 clp = list_entry(pos, struct     6064                 clp = list_entry(pos, struct nfs4_client, cl_lru);
6435                 if (clp->cl_state == NFSD4_EX    6065                 if (clp->cl_state == NFSD4_EXPIRABLE)
6436                         goto exp_client;         6066                         goto exp_client;
6437                 if (!state_expired(lt, clp->c    6067                 if (!state_expired(lt, clp->cl_time))
6438                         break;                   6068                         break;
6439                 if (!atomic_read(&clp->cl_rpc    6069                 if (!atomic_read(&clp->cl_rpc_users)) {
6440                         if (clp->cl_state ==     6070                         if (clp->cl_state == NFSD4_ACTIVE)
6441                                 atomic_inc(&n    6071                                 atomic_inc(&nn->nfsd_courtesy_clients);
6442                         clp->cl_state = NFSD4    6072                         clp->cl_state = NFSD4_COURTESY;
6443                 }                                6073                 }
6444                 if (!client_has_state(clp))      6074                 if (!client_has_state(clp))
6445                         goto exp_client;         6075                         goto exp_client;
6446                 if (!nfs4_anylock_blockers(cl    6076                 if (!nfs4_anylock_blockers(clp))
6447                         if (reapcnt >= maxrea    6077                         if (reapcnt >= maxreap)
6448                                 continue;        6078                                 continue;
6449 exp_client:                                      6079 exp_client:
6450                 if (!mark_client_expired_lock    6080                 if (!mark_client_expired_locked(clp)) {
6451                         list_add(&clp->cl_lru    6081                         list_add(&clp->cl_lru, reaplist);
6452                         reapcnt++;               6082                         reapcnt++;
6453                 }                                6083                 }
6454         }                                        6084         }
6455         spin_unlock(&nn->client_lock);           6085         spin_unlock(&nn->client_lock);
6456 }                                                6086 }
6457                                                  6087 
6458 static void                                      6088 static void
6459 nfs4_get_courtesy_client_reaplist(struct nfsd    6089 nfs4_get_courtesy_client_reaplist(struct nfsd_net *nn,
6460                                 struct list_h    6090                                 struct list_head *reaplist)
6461 {                                                6091 {
6462         unsigned int maxreap = 0, reapcnt = 0    6092         unsigned int maxreap = 0, reapcnt = 0;
6463         struct list_head *pos, *next;            6093         struct list_head *pos, *next;
6464         struct nfs4_client *clp;                 6094         struct nfs4_client *clp;
6465                                                  6095 
6466         maxreap = NFSD_CLIENT_MAX_TRIM_PER_RU    6096         maxreap = NFSD_CLIENT_MAX_TRIM_PER_RUN;
6467         INIT_LIST_HEAD(reaplist);                6097         INIT_LIST_HEAD(reaplist);
6468                                                  6098 
6469         spin_lock(&nn->client_lock);             6099         spin_lock(&nn->client_lock);
6470         list_for_each_safe(pos, next, &nn->cl    6100         list_for_each_safe(pos, next, &nn->client_lru) {
6471                 clp = list_entry(pos, struct     6101                 clp = list_entry(pos, struct nfs4_client, cl_lru);
6472                 if (clp->cl_state == NFSD4_AC    6102                 if (clp->cl_state == NFSD4_ACTIVE)
6473                         break;                   6103                         break;
6474                 if (reapcnt >= maxreap)          6104                 if (reapcnt >= maxreap)
6475                         break;                   6105                         break;
6476                 if (!mark_client_expired_lock    6106                 if (!mark_client_expired_locked(clp)) {
6477                         list_add(&clp->cl_lru    6107                         list_add(&clp->cl_lru, reaplist);
6478                         reapcnt++;               6108                         reapcnt++;
6479                 }                                6109                 }
6480         }                                        6110         }
6481         spin_unlock(&nn->client_lock);           6111         spin_unlock(&nn->client_lock);
6482 }                                                6112 }
6483                                                  6113 
6484 static void                                      6114 static void
6485 nfs4_process_client_reaplist(struct list_head    6115 nfs4_process_client_reaplist(struct list_head *reaplist)
6486 {                                                6116 {
6487         struct list_head *pos, *next;            6117         struct list_head *pos, *next;
6488         struct nfs4_client *clp;                 6118         struct nfs4_client *clp;
6489                                                  6119 
6490         list_for_each_safe(pos, next, reaplis    6120         list_for_each_safe(pos, next, reaplist) {
6491                 clp = list_entry(pos, struct     6121                 clp = list_entry(pos, struct nfs4_client, cl_lru);
6492                 trace_nfsd_clid_purged(&clp->    6122                 trace_nfsd_clid_purged(&clp->cl_clientid);
6493                 list_del_init(&clp->cl_lru);     6123                 list_del_init(&clp->cl_lru);
6494                 expire_client(clp);              6124                 expire_client(clp);
6495         }                                        6125         }
6496 }                                                6126 }
6497                                                  6127 
6498 static void nfs40_clean_admin_revoked(struct  << 
6499                                       struct  << 
6500 {                                             << 
6501         struct nfs4_client *clp;              << 
6502                                               << 
6503         spin_lock(&nn->client_lock);          << 
6504         if (nn->nfs40_last_revoke == 0 ||     << 
6505             nn->nfs40_last_revoke > lt->cutof << 
6506                 spin_unlock(&nn->client_lock) << 
6507                 return;                       << 
6508         }                                     << 
6509         nn->nfs40_last_revoke = 0;            << 
6510                                               << 
6511 retry:                                        << 
6512         list_for_each_entry(clp, &nn->client_ << 
6513                 unsigned long id, tmp;        << 
6514                 struct nfs4_stid *stid;       << 
6515                                               << 
6516                 if (atomic_read(&clp->cl_admi << 
6517                         continue;             << 
6518                                               << 
6519                 spin_lock(&clp->cl_lock);     << 
6520                 idr_for_each_entry_ul(&clp->c << 
6521                         if (stid->sc_status & << 
6522                                 refcount_inc( << 
6523                                 spin_unlock(& << 
6524                                 /* this funct << 
6525                                 nfsd4_drop_re << 
6526                                 nfs4_put_stid << 
6527                                 spin_lock(&nn << 
6528                                 goto retry;   << 
6529                         }                     << 
6530                 spin_unlock(&clp->cl_lock);   << 
6531         }                                     << 
6532         spin_unlock(&nn->client_lock);        << 
6533 }                                             << 
6534                                               << 
6535 static time64_t                                  6128 static time64_t
6536 nfs4_laundromat(struct nfsd_net *nn)             6129 nfs4_laundromat(struct nfsd_net *nn)
6537 {                                                6130 {
6538         struct nfs4_openowner *oo;               6131         struct nfs4_openowner *oo;
6539         struct nfs4_delegation *dp;              6132         struct nfs4_delegation *dp;
6540         struct nfs4_ol_stateid *stp;             6133         struct nfs4_ol_stateid *stp;
6541         struct nfsd4_blocked_lock *nbl;          6134         struct nfsd4_blocked_lock *nbl;
6542         struct list_head *pos, *next, reaplis    6135         struct list_head *pos, *next, reaplist;
6543         struct laundry_time lt = {               6136         struct laundry_time lt = {
6544                 .cutoff = ktime_get_boottime_    6137                 .cutoff = ktime_get_boottime_seconds() - nn->nfsd4_lease,
6545                 .new_timeo = nn->nfsd4_lease     6138                 .new_timeo = nn->nfsd4_lease
6546         };                                       6139         };
6547         struct nfs4_cpntf_state *cps;            6140         struct nfs4_cpntf_state *cps;
6548         copy_stateid_t *cps_t;                   6141         copy_stateid_t *cps_t;
6549         int i;                                   6142         int i;
6550                                                  6143 
6551         if (clients_still_reclaiming(nn)) {      6144         if (clients_still_reclaiming(nn)) {
6552                 lt.new_timeo = 0;                6145                 lt.new_timeo = 0;
6553                 goto out;                        6146                 goto out;
6554         }                                        6147         }
6555         nfsd4_end_grace(nn);                     6148         nfsd4_end_grace(nn);
6556                                                  6149 
6557         spin_lock(&nn->s2s_cp_lock);             6150         spin_lock(&nn->s2s_cp_lock);
6558         idr_for_each_entry(&nn->s2s_cp_statei    6151         idr_for_each_entry(&nn->s2s_cp_stateids, cps_t, i) {
6559                 cps = container_of(cps_t, str    6152                 cps = container_of(cps_t, struct nfs4_cpntf_state, cp_stateid);
6560                 if (cps->cp_stateid.cs_type =    6153                 if (cps->cp_stateid.cs_type == NFS4_COPYNOTIFY_STID &&
6561                                 state_expired    6154                                 state_expired(&lt, cps->cpntf_time))
6562                         _free_cpntf_state_loc    6155                         _free_cpntf_state_locked(nn, cps);
6563         }                                        6156         }
6564         spin_unlock(&nn->s2s_cp_lock);           6157         spin_unlock(&nn->s2s_cp_lock);
6565         nfs4_get_client_reaplist(nn, &reaplis    6158         nfs4_get_client_reaplist(nn, &reaplist, &lt);
6566         nfs4_process_client_reaplist(&reaplis    6159         nfs4_process_client_reaplist(&reaplist);
6567                                                  6160 
6568         nfs40_clean_admin_revoked(nn, &lt);   << 
6569                                               << 
6570         spin_lock(&state_lock);                  6161         spin_lock(&state_lock);
6571         list_for_each_safe(pos, next, &nn->de    6162         list_for_each_safe(pos, next, &nn->del_recall_lru) {
6572                 dp = list_entry (pos, struct     6163                 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
6573                 if (!state_expired(&lt, dp->d    6164                 if (!state_expired(&lt, dp->dl_time))
6574                         break;                   6165                         break;
6575                 refcount_inc(&dp->dl_stid.sc_ !! 6166                 WARN_ON(!unhash_delegation_locked(dp));
6576                 unhash_delegation_locked(dp,  << 
6577                 list_add(&dp->dl_recall_lru,     6167                 list_add(&dp->dl_recall_lru, &reaplist);
6578         }                                        6168         }
6579         spin_unlock(&state_lock);                6169         spin_unlock(&state_lock);
6580         while (!list_empty(&reaplist)) {         6170         while (!list_empty(&reaplist)) {
6581                 dp = list_first_entry(&reapli    6171                 dp = list_first_entry(&reaplist, struct nfs4_delegation,
6582                                         dl_re    6172                                         dl_recall_lru);
6583                 list_del_init(&dp->dl_recall_    6173                 list_del_init(&dp->dl_recall_lru);
6584                 revoke_delegation(dp);           6174                 revoke_delegation(dp);
6585         }                                        6175         }
6586                                                  6176 
6587         spin_lock(&nn->client_lock);             6177         spin_lock(&nn->client_lock);
6588         while (!list_empty(&nn->close_lru)) {    6178         while (!list_empty(&nn->close_lru)) {
6589                 oo = list_first_entry(&nn->cl    6179                 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
6590                                         oo_cl    6180                                         oo_close_lru);
6591                 if (!state_expired(&lt, oo->o    6181                 if (!state_expired(&lt, oo->oo_time))
6592                         break;                   6182                         break;
6593                 list_del_init(&oo->oo_close_l    6183                 list_del_init(&oo->oo_close_lru);
6594                 stp = oo->oo_last_closed_stid    6184                 stp = oo->oo_last_closed_stid;
6595                 oo->oo_last_closed_stid = NUL    6185                 oo->oo_last_closed_stid = NULL;
6596                 spin_unlock(&nn->client_lock)    6186                 spin_unlock(&nn->client_lock);
6597                 nfs4_put_stid(&stp->st_stid);    6187                 nfs4_put_stid(&stp->st_stid);
6598                 spin_lock(&nn->client_lock);     6188                 spin_lock(&nn->client_lock);
6599         }                                        6189         }
6600         spin_unlock(&nn->client_lock);           6190         spin_unlock(&nn->client_lock);
6601                                                  6191 
6602         /*                                       6192         /*
6603          * It's possible for a client to try     6193          * It's possible for a client to try and acquire an already held lock
6604          * that is being held for a long time    6194          * that is being held for a long time, and then lose interest in it.
6605          * So, we clean out any un-revisited     6195          * So, we clean out any un-revisited request after a lease period
6606          * under the assumption that the clie    6196          * under the assumption that the client is no longer interested.
6607          *                                       6197          *
6608          * RFC5661, sec. 9.6 states that the     6198          * RFC5661, sec. 9.6 states that the client must not rely on getting
6609          * notifications and must continue to    6199          * notifications and must continue to poll for locks, even when the
6610          * server supports them. Thus this sh    6200          * server supports them. Thus this shouldn't lead to clients blocking
6611          * indefinitely once the lock does be    6201          * indefinitely once the lock does become free.
6612          */                                      6202          */
6613         BUG_ON(!list_empty(&reaplist));          6203         BUG_ON(!list_empty(&reaplist));
6614         spin_lock(&nn->blocked_locks_lock);      6204         spin_lock(&nn->blocked_locks_lock);
6615         while (!list_empty(&nn->blocked_locks    6205         while (!list_empty(&nn->blocked_locks_lru)) {
6616                 nbl = list_first_entry(&nn->b    6206                 nbl = list_first_entry(&nn->blocked_locks_lru,
6617                                         struc    6207                                         struct nfsd4_blocked_lock, nbl_lru);
6618                 if (!state_expired(&lt, nbl->    6208                 if (!state_expired(&lt, nbl->nbl_time))
6619                         break;                   6209                         break;
6620                 list_move(&nbl->nbl_lru, &rea    6210                 list_move(&nbl->nbl_lru, &reaplist);
6621                 list_del_init(&nbl->nbl_list)    6211                 list_del_init(&nbl->nbl_list);
6622         }                                        6212         }
6623         spin_unlock(&nn->blocked_locks_lock);    6213         spin_unlock(&nn->blocked_locks_lock);
6624                                                  6214 
6625         while (!list_empty(&reaplist)) {         6215         while (!list_empty(&reaplist)) {
6626                 nbl = list_first_entry(&reapl    6216                 nbl = list_first_entry(&reaplist,
6627                                         struc    6217                                         struct nfsd4_blocked_lock, nbl_lru);
6628                 list_del_init(&nbl->nbl_lru);    6218                 list_del_init(&nbl->nbl_lru);
6629                 free_blocked_lock(nbl);          6219                 free_blocked_lock(nbl);
6630         }                                        6220         }
6631 #ifdef CONFIG_NFSD_V4_2_INTER_SSC                6221 #ifdef CONFIG_NFSD_V4_2_INTER_SSC
6632         /* service the server-to-server copy     6222         /* service the server-to-server copy delayed unmount list */
6633         nfsd4_ssc_expire_umount(nn);             6223         nfsd4_ssc_expire_umount(nn);
6634 #endif                                           6224 #endif
6635         if (atomic_long_read(&num_delegations << 
6636                 deleg_reaper(nn);             << 
6637 out:                                             6225 out:
6638         return max_t(time64_t, lt.new_timeo,     6226         return max_t(time64_t, lt.new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
6639 }                                                6227 }
6640                                                  6228 
6641 static void laundromat_main(struct work_struc    6229 static void laundromat_main(struct work_struct *);
6642                                                  6230 
6643 static void                                      6231 static void
6644 laundromat_main(struct work_struct *laundry)     6232 laundromat_main(struct work_struct *laundry)
6645 {                                                6233 {
6646         time64_t t;                              6234         time64_t t;
6647         struct delayed_work *dwork = to_delay    6235         struct delayed_work *dwork = to_delayed_work(laundry);
6648         struct nfsd_net *nn = container_of(dw    6236         struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
6649                                            la    6237                                            laundromat_work);
6650                                                  6238 
6651         t = nfs4_laundromat(nn);                 6239         t = nfs4_laundromat(nn);
6652         queue_delayed_work(laundry_wq, &nn->l    6240         queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
6653 }                                                6241 }
6654                                                  6242 
6655 static void                                      6243 static void
6656 courtesy_client_reaper(struct nfsd_net *nn)      6244 courtesy_client_reaper(struct nfsd_net *nn)
6657 {                                                6245 {
6658         struct list_head reaplist;               6246         struct list_head reaplist;
6659                                                  6247 
6660         nfs4_get_courtesy_client_reaplist(nn,    6248         nfs4_get_courtesy_client_reaplist(nn, &reaplist);
6661         nfs4_process_client_reaplist(&reaplis    6249         nfs4_process_client_reaplist(&reaplist);
6662 }                                                6250 }
6663                                                  6251 
6664 static void                                      6252 static void
6665 deleg_reaper(struct nfsd_net *nn)                6253 deleg_reaper(struct nfsd_net *nn)
6666 {                                                6254 {
6667         struct list_head *pos, *next;            6255         struct list_head *pos, *next;
6668         struct nfs4_client *clp;                 6256         struct nfs4_client *clp;
6669         LIST_HEAD(cblist);                    !! 6257         struct list_head cblist;
6670                                                  6258 
                                                   >> 6259         INIT_LIST_HEAD(&cblist);
6671         spin_lock(&nn->client_lock);             6260         spin_lock(&nn->client_lock);
6672         list_for_each_safe(pos, next, &nn->cl    6261         list_for_each_safe(pos, next, &nn->client_lru) {
6673                 clp = list_entry(pos, struct     6262                 clp = list_entry(pos, struct nfs4_client, cl_lru);
6674                 if (clp->cl_state != NFSD4_AC    6263                 if (clp->cl_state != NFSD4_ACTIVE ||
6675                         list_empty(&clp->cl_d    6264                         list_empty(&clp->cl_delegations) ||
6676                         atomic_read(&clp->cl_    6265                         atomic_read(&clp->cl_delegs_in_recall) ||
6677                         test_bit(NFSD4_CLIENT    6266                         test_bit(NFSD4_CLIENT_CB_RECALL_ANY, &clp->cl_flags) ||
6678                         (ktime_get_boottime_s    6267                         (ktime_get_boottime_seconds() -
6679                                 clp->cl_ra_ti    6268                                 clp->cl_ra_time < 5)) {
6680                         continue;                6269                         continue;
6681                 }                                6270                 }
6682                 list_add(&clp->cl_ra_cblist,     6271                 list_add(&clp->cl_ra_cblist, &cblist);
6683                                                  6272 
6684                 /* release in nfsd4_cb_recall    6273                 /* release in nfsd4_cb_recall_any_release */
6685                 kref_get(&clp->cl_nfsdfs.cl_r    6274                 kref_get(&clp->cl_nfsdfs.cl_ref);
6686                 set_bit(NFSD4_CLIENT_CB_RECAL    6275                 set_bit(NFSD4_CLIENT_CB_RECALL_ANY, &clp->cl_flags);
6687                 clp->cl_ra_time = ktime_get_b    6276                 clp->cl_ra_time = ktime_get_boottime_seconds();
6688         }                                        6277         }
6689         spin_unlock(&nn->client_lock);           6278         spin_unlock(&nn->client_lock);
6690                                                  6279 
6691         while (!list_empty(&cblist)) {           6280         while (!list_empty(&cblist)) {
6692                 clp = list_first_entry(&cblis    6281                 clp = list_first_entry(&cblist, struct nfs4_client,
6693                                         cl_ra    6282                                         cl_ra_cblist);
6694                 list_del_init(&clp->cl_ra_cbl    6283                 list_del_init(&clp->cl_ra_cblist);
6695                 clp->cl_ra->ra_keep = 0;         6284                 clp->cl_ra->ra_keep = 0;
6696                 clp->cl_ra->ra_bmval[0] = BIT !! 6285                 clp->cl_ra->ra_bmval[0] = BIT(RCA4_TYPE_MASK_RDATA_DLG);
6697                                               << 
6698                 trace_nfsd_cb_recall_any(clp-    6286                 trace_nfsd_cb_recall_any(clp->cl_ra);
6699                 nfsd4_run_cb(&clp->cl_ra->ra_    6287                 nfsd4_run_cb(&clp->cl_ra->ra_cb);
6700         }                                        6288         }
6701 }                                                6289 }
6702                                                  6290 
6703 static void                                      6291 static void
6704 nfsd4_state_shrinker_worker(struct work_struc    6292 nfsd4_state_shrinker_worker(struct work_struct *work)
6705 {                                                6293 {
6706         struct nfsd_net *nn = container_of(wo    6294         struct nfsd_net *nn = container_of(work, struct nfsd_net,
6707                                 nfsd_shrinker    6295                                 nfsd_shrinker_work);
6708                                                  6296 
6709         courtesy_client_reaper(nn);              6297         courtesy_client_reaper(nn);
6710         deleg_reaper(nn);                        6298         deleg_reaper(nn);
6711 }                                                6299 }
6712                                                  6300 
6713 static inline __be32 nfs4_check_fh(struct svc    6301 static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
6714 {                                                6302 {
6715         if (!fh_match(&fhp->fh_handle, &stp->    6303         if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
6716                 return nfserr_bad_stateid;       6304                 return nfserr_bad_stateid;
6717         return nfs_ok;                           6305         return nfs_ok;
6718 }                                                6306 }
6719                                                  6307 
6720 static                                           6308 static
6721 __be32 nfs4_check_openmode(struct nfs4_ol_sta    6309 __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
6722 {                                                6310 {
6723         __be32 status = nfserr_openmode;         6311         __be32 status = nfserr_openmode;
6724                                                  6312 
6725         /* For lock stateid's, we test the pa    6313         /* For lock stateid's, we test the parent open, not the lock: */
6726         if (stp->st_openstp)                     6314         if (stp->st_openstp)
6727                 stp = stp->st_openstp;           6315                 stp = stp->st_openstp;
6728         if ((flags & WR_STATE) && !access_per    6316         if ((flags & WR_STATE) && !access_permit_write(stp))
6729                 goto out;                        6317                 goto out;
6730         if ((flags & RD_STATE) && !access_per    6318         if ((flags & RD_STATE) && !access_permit_read(stp))
6731                 goto out;                        6319                 goto out;
6732         status = nfs_ok;                         6320         status = nfs_ok;
6733 out:                                             6321 out:
6734         return status;                           6322         return status;
6735 }                                                6323 }
6736                                                  6324 
6737 static inline __be32                             6325 static inline __be32
6738 check_special_stateids(struct net *net, svc_f    6326 check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
6739 {                                                6327 {
6740         if (ONE_STATEID(stateid) && (flags &     6328         if (ONE_STATEID(stateid) && (flags & RD_STATE))
6741                 return nfs_ok;                   6329                 return nfs_ok;
6742         else if (opens_in_grace(net)) {          6330         else if (opens_in_grace(net)) {
6743                 /* Answer in remaining cases     6331                 /* Answer in remaining cases depends on existence of
6744                  * conflicting state; so we m    6332                  * conflicting state; so we must wait out the grace period. */
6745                 return nfserr_grace;             6333                 return nfserr_grace;
6746         } else if (flags & WR_STATE)             6334         } else if (flags & WR_STATE)
6747                 return nfs4_share_conflict(cu    6335                 return nfs4_share_conflict(current_fh,
6748                                 NFS4_SHARE_DE    6336                                 NFS4_SHARE_DENY_WRITE);
6749         else /* (flags & RD_STATE) && ZERO_ST    6337         else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
6750                 return nfs4_share_conflict(cu    6338                 return nfs4_share_conflict(current_fh,
6751                                 NFS4_SHARE_DE    6339                                 NFS4_SHARE_DENY_READ);
6752 }                                                6340 }
6753                                                  6341 
6754 static __be32 check_stateid_generation(statei    6342 static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
6755 {                                                6343 {
6756         /*                                       6344         /*
6757          * When sessions are used the stateid    6345          * When sessions are used the stateid generation number is ignored
6758          * when it is zero.                      6346          * when it is zero.
6759          */                                      6347          */
6760         if (has_session && in->si_generation     6348         if (has_session && in->si_generation == 0)
6761                 return nfs_ok;                   6349                 return nfs_ok;
6762                                                  6350 
6763         if (in->si_generation == ref->si_gene    6351         if (in->si_generation == ref->si_generation)
6764                 return nfs_ok;                   6352                 return nfs_ok;
6765                                                  6353 
6766         /* If the client sends us a stateid f    6354         /* If the client sends us a stateid from the future, it's buggy: */
6767         if (nfsd4_stateid_generation_after(in    6355         if (nfsd4_stateid_generation_after(in, ref))
6768                 return nfserr_bad_stateid;       6356                 return nfserr_bad_stateid;
6769         /*                                       6357         /*
6770          * However, we could see a stateid fr    6358          * However, we could see a stateid from the past, even from a
6771          * non-buggy client.  For example, if    6359          * non-buggy client.  For example, if the client sends a lock
6772          * while some IO is outstanding, the     6360          * while some IO is outstanding, the lock may bump si_generation
6773          * while the IO is still in flight.      6361          * while the IO is still in flight.  The client could avoid that
6774          * situation by waiting for responses    6362          * situation by waiting for responses on all the IO requests,
6775          * but better performance may result     6363          * but better performance may result in retrying IO that
6776          * receives an old_stateid error if r    6364          * receives an old_stateid error if requests are rarely
6777          * reordered in flight:                  6365          * reordered in flight:
6778          */                                      6366          */
6779         return nfserr_old_stateid;               6367         return nfserr_old_stateid;
6780 }                                                6368 }
6781                                                  6369 
6782 static __be32 nfsd4_stid_check_stateid_genera    6370 static __be32 nfsd4_stid_check_stateid_generation(stateid_t *in, struct nfs4_stid *s, bool has_session)
6783 {                                                6371 {
6784         __be32 ret;                              6372         __be32 ret;
6785                                                  6373 
6786         spin_lock(&s->sc_lock);                  6374         spin_lock(&s->sc_lock);
6787         ret = nfsd4_verify_open_stid(s);         6375         ret = nfsd4_verify_open_stid(s);
6788         if (ret == nfs_ok)                       6376         if (ret == nfs_ok)
6789                 ret = check_stateid_generatio    6377                 ret = check_stateid_generation(in, &s->sc_stateid, has_session);
6790         spin_unlock(&s->sc_lock);                6378         spin_unlock(&s->sc_lock);
6791         if (ret == nfserr_admin_revoked)      << 
6792                 nfsd40_drop_revoked_stid(s->s << 
6793                                         &s->s << 
6794         return ret;                              6379         return ret;
6795 }                                                6380 }
6796                                                  6381 
6797 static __be32 nfsd4_check_openowner_confirmed    6382 static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
6798 {                                                6383 {
6799         if (ols->st_stateowner->so_is_open_ow    6384         if (ols->st_stateowner->so_is_open_owner &&
6800             !(openowner(ols->st_stateowner)->    6385             !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
6801                 return nfserr_bad_stateid;       6386                 return nfserr_bad_stateid;
6802         return nfs_ok;                           6387         return nfs_ok;
6803 }                                                6388 }
6804                                                  6389 
6805 static __be32 nfsd4_validate_stateid(struct n    6390 static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
6806 {                                                6391 {
6807         struct nfs4_stid *s;                     6392         struct nfs4_stid *s;
6808         __be32 status = nfserr_bad_stateid;      6393         __be32 status = nfserr_bad_stateid;
6809                                                  6394 
6810         if (ZERO_STATEID(stateid) || ONE_STAT    6395         if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
6811                 CLOSE_STATEID(stateid))          6396                 CLOSE_STATEID(stateid))
6812                 return status;                   6397                 return status;
6813         spin_lock(&cl->cl_lock);                 6398         spin_lock(&cl->cl_lock);
6814         s = find_stateid_locked(cl, stateid);    6399         s = find_stateid_locked(cl, stateid);
6815         if (!s)                                  6400         if (!s)
6816                 goto out_unlock;                 6401                 goto out_unlock;
6817         status = nfsd4_stid_check_stateid_gen    6402         status = nfsd4_stid_check_stateid_generation(stateid, s, 1);
6818         if (status)                              6403         if (status)
6819                 goto out_unlock;                 6404                 goto out_unlock;
6820         status = nfsd4_verify_open_stid(s);   << 
6821         if (status)                           << 
6822                 goto out_unlock;              << 
6823                                               << 
6824         switch (s->sc_type) {                    6405         switch (s->sc_type) {
6825         case SC_TYPE_DELEG:                   !! 6406         case NFS4_DELEG_STID:
6826                 status = nfs_ok;                 6407                 status = nfs_ok;
6827                 break;                           6408                 break;
6828         case SC_TYPE_OPEN:                    !! 6409         case NFS4_REVOKED_DELEG_STID:
6829         case SC_TYPE_LOCK:                    !! 6410                 status = nfserr_deleg_revoked;
                                                   >> 6411                 break;
                                                   >> 6412         case NFS4_OPEN_STID:
                                                   >> 6413         case NFS4_LOCK_STID:
6830                 status = nfsd4_check_openowne    6414                 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
6831                 break;                           6415                 break;
6832         default:                                 6416         default:
6833                 printk("unknown stateid type     6417                 printk("unknown stateid type %x\n", s->sc_type);
                                                   >> 6418                 fallthrough;
                                                   >> 6419         case NFS4_CLOSED_STID:
                                                   >> 6420         case NFS4_CLOSED_DELEG_STID:
6834                 status = nfserr_bad_stateid;     6421                 status = nfserr_bad_stateid;
6835         }                                        6422         }
6836 out_unlock:                                      6423 out_unlock:
6837         spin_unlock(&cl->cl_lock);               6424         spin_unlock(&cl->cl_lock);
6838         if (status == nfserr_admin_revoked)   << 
6839                 nfsd40_drop_revoked_stid(cl,  << 
6840         return status;                           6425         return status;
6841 }                                                6426 }
6842                                                  6427 
6843 __be32                                           6428 __be32
6844 nfsd4_lookup_stateid(struct nfsd4_compound_st    6429 nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
6845                      stateid_t *stateid,      !! 6430                      stateid_t *stateid, unsigned char typemask,
6846                      unsigned short typemask, << 
6847                      struct nfs4_stid **s, st    6431                      struct nfs4_stid **s, struct nfsd_net *nn)
6848 {                                                6432 {
6849         __be32 status;                           6433         __be32 status;
6850         struct nfs4_stid *stid;                  6434         struct nfs4_stid *stid;
6851         bool return_revoked = false;             6435         bool return_revoked = false;
6852                                                  6436 
6853         /*                                       6437         /*
6854          *  only return revoked delegations i    6438          *  only return revoked delegations if explicitly asked.
6855          *  otherwise we report revoked or ba    6439          *  otherwise we report revoked or bad_stateid status.
6856          */                                      6440          */
6857         if (statusmask & SC_STATUS_REVOKED)   !! 6441         if (typemask & NFS4_REVOKED_DELEG_STID)
6858                 return_revoked = true;           6442                 return_revoked = true;
6859         if (typemask & SC_TYPE_DELEG)         !! 6443         else if (typemask & NFS4_DELEG_STID)
6860                 /* Always allow REVOKED for D !! 6444                 typemask |= NFS4_REVOKED_DELEG_STID;
6861                  * retturn the appropriate er << 
6862                  */                           << 
6863                 statusmask |= SC_STATUS_REVOK << 
6864                                               << 
6865         statusmask |= SC_STATUS_ADMIN_REVOKED << 
6866                                                  6445 
6867         if (ZERO_STATEID(stateid) || ONE_STAT    6446         if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
6868                 CLOSE_STATEID(stateid))          6447                 CLOSE_STATEID(stateid))
6869                 return nfserr_bad_stateid;       6448                 return nfserr_bad_stateid;
6870         status = set_client(&stateid->si_opaq    6449         status = set_client(&stateid->si_opaque.so_clid, cstate, nn);
6871         if (status == nfserr_stale_clientid)     6450         if (status == nfserr_stale_clientid) {
6872                 if (cstate->session)             6451                 if (cstate->session)
6873                         return nfserr_bad_sta    6452                         return nfserr_bad_stateid;
6874                 return nfserr_stale_stateid;     6453                 return nfserr_stale_stateid;
6875         }                                        6454         }
6876         if (status)                              6455         if (status)
6877                 return status;                   6456                 return status;
6878         stid = find_stateid_by_type(cstate->c !! 6457         stid = find_stateid_by_type(cstate->clp, stateid, typemask);
6879         if (!stid)                               6458         if (!stid)
6880                 return nfserr_bad_stateid;       6459                 return nfserr_bad_stateid;
6881         if ((stid->sc_status & SC_STATUS_REVO !! 6460         if ((stid->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) {
6882                 nfs4_put_stid(stid);          << 
6883                 return nfserr_deleg_revoked;  << 
6884         }                                     << 
6885         if (stid->sc_status & SC_STATUS_ADMIN << 
6886                 nfsd40_drop_revoked_stid(csta << 
6887                 nfs4_put_stid(stid);             6461                 nfs4_put_stid(stid);
6888                 return nfserr_admin_revoked;  !! 6462                 if (cstate->minorversion)
                                                   >> 6463                         return nfserr_deleg_revoked;
                                                   >> 6464                 return nfserr_bad_stateid;
6889         }                                        6465         }
6890         *s = stid;                               6466         *s = stid;
6891         return nfs_ok;                           6467         return nfs_ok;
6892 }                                                6468 }
6893                                                  6469 
6894 static struct nfsd_file *                        6470 static struct nfsd_file *
6895 nfs4_find_file(struct nfs4_stid *s, int flags    6471 nfs4_find_file(struct nfs4_stid *s, int flags)
6896 {                                                6472 {
6897         struct nfsd_file *ret = NULL;            6473         struct nfsd_file *ret = NULL;
6898                                                  6474 
6899         if (!s || s->sc_status)               !! 6475         if (!s)
6900                 return NULL;                     6476                 return NULL;
6901                                                  6477 
6902         switch (s->sc_type) {                    6478         switch (s->sc_type) {
6903         case SC_TYPE_DELEG:                   !! 6479         case NFS4_DELEG_STID:
6904                 spin_lock(&s->sc_file->fi_loc    6480                 spin_lock(&s->sc_file->fi_lock);
6905                 ret = nfsd_file_get(s->sc_fil    6481                 ret = nfsd_file_get(s->sc_file->fi_deleg_file);
6906                 spin_unlock(&s->sc_file->fi_l    6482                 spin_unlock(&s->sc_file->fi_lock);
6907                 break;                           6483                 break;
6908         case SC_TYPE_OPEN:                    !! 6484         case NFS4_OPEN_STID:
6909         case SC_TYPE_LOCK:                    !! 6485         case NFS4_LOCK_STID:
6910                 if (flags & RD_STATE)            6486                 if (flags & RD_STATE)
6911                         ret = find_readable_f    6487                         ret = find_readable_file(s->sc_file);
6912                 else                             6488                 else
6913                         ret = find_writeable_    6489                         ret = find_writeable_file(s->sc_file);
6914         }                                        6490         }
6915                                                  6491 
6916         return ret;                              6492         return ret;
6917 }                                                6493 }
6918                                                  6494 
6919 static __be32                                    6495 static __be32
6920 nfs4_check_olstateid(struct nfs4_ol_stateid *    6496 nfs4_check_olstateid(struct nfs4_ol_stateid *ols, int flags)
6921 {                                                6497 {
6922         __be32 status;                           6498         __be32 status;
6923                                                  6499 
6924         status = nfsd4_check_openowner_confir    6500         status = nfsd4_check_openowner_confirmed(ols);
6925         if (status)                              6501         if (status)
6926                 return status;                   6502                 return status;
6927         return nfs4_check_openmode(ols, flags    6503         return nfs4_check_openmode(ols, flags);
6928 }                                                6504 }
6929                                                  6505 
6930 static __be32                                    6506 static __be32
6931 nfs4_check_file(struct svc_rqst *rqstp, struc    6507 nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
6932                 struct nfsd_file **nfp, int f    6508                 struct nfsd_file **nfp, int flags)
6933 {                                                6509 {
6934         int acc = (flags & RD_STATE) ? NFSD_M    6510         int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
6935         struct nfsd_file *nf;                    6511         struct nfsd_file *nf;
6936         __be32 status;                           6512         __be32 status;
6937                                                  6513 
6938         nf = nfs4_find_file(s, flags);           6514         nf = nfs4_find_file(s, flags);
6939         if (nf) {                                6515         if (nf) {
6940                 status = nfsd_permission(&rqs !! 6516                 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
6941                                          fhp- << 
6942                                 acc | NFSD_MA    6517                                 acc | NFSD_MAY_OWNER_OVERRIDE);
6943                 if (status) {                    6518                 if (status) {
6944                         nfsd_file_put(nf);       6519                         nfsd_file_put(nf);
6945                         goto out;                6520                         goto out;
6946                 }                                6521                 }
6947         } else {                                 6522         } else {
6948                 status = nfsd_file_acquire(rq    6523                 status = nfsd_file_acquire(rqstp, fhp, acc, &nf);
6949                 if (status)                      6524                 if (status)
6950                         return status;           6525                         return status;
6951         }                                        6526         }
6952         *nfp = nf;                               6527         *nfp = nf;
6953 out:                                             6528 out:
6954         return status;                           6529         return status;
6955 }                                                6530 }
6956 static void                                      6531 static void
6957 _free_cpntf_state_locked(struct nfsd_net *nn,    6532 _free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
6958 {                                                6533 {
6959         WARN_ON_ONCE(cps->cp_stateid.cs_type     6534         WARN_ON_ONCE(cps->cp_stateid.cs_type != NFS4_COPYNOTIFY_STID);
6960         if (!refcount_dec_and_test(&cps->cp_s    6535         if (!refcount_dec_and_test(&cps->cp_stateid.cs_count))
6961                 return;                          6536                 return;
6962         list_del(&cps->cp_list);                 6537         list_del(&cps->cp_list);
6963         idr_remove(&nn->s2s_cp_stateids,         6538         idr_remove(&nn->s2s_cp_stateids,
6964                    cps->cp_stateid.cs_stid.si    6539                    cps->cp_stateid.cs_stid.si_opaque.so_id);
6965         kfree(cps);                              6540         kfree(cps);
6966 }                                                6541 }
6967 /*                                               6542 /*
6968  * A READ from an inter server to server COPY    6543  * A READ from an inter server to server COPY will have a
6969  * copy stateid. Look up the copy notify stat    6544  * copy stateid. Look up the copy notify stateid from the
6970  * idr structure and take a reference on it.     6545  * idr structure and take a reference on it.
6971  */                                              6546  */
6972 __be32 manage_cpntf_state(struct nfsd_net *nn    6547 __be32 manage_cpntf_state(struct nfsd_net *nn, stateid_t *st,
6973                           struct nfs4_client     6548                           struct nfs4_client *clp,
6974                           struct nfs4_cpntf_s    6549                           struct nfs4_cpntf_state **cps)
6975 {                                                6550 {
6976         copy_stateid_t *cps_t;                   6551         copy_stateid_t *cps_t;
6977         struct nfs4_cpntf_state *state = NULL    6552         struct nfs4_cpntf_state *state = NULL;
6978                                                  6553 
6979         if (st->si_opaque.so_clid.cl_id != nn    6554         if (st->si_opaque.so_clid.cl_id != nn->s2s_cp_cl_id)
6980                 return nfserr_bad_stateid;       6555                 return nfserr_bad_stateid;
6981         spin_lock(&nn->s2s_cp_lock);             6556         spin_lock(&nn->s2s_cp_lock);
6982         cps_t = idr_find(&nn->s2s_cp_stateids    6557         cps_t = idr_find(&nn->s2s_cp_stateids, st->si_opaque.so_id);
6983         if (cps_t) {                             6558         if (cps_t) {
6984                 state = container_of(cps_t, s    6559                 state = container_of(cps_t, struct nfs4_cpntf_state,
6985                                      cp_state    6560                                      cp_stateid);
6986                 if (state->cp_stateid.cs_type    6561                 if (state->cp_stateid.cs_type != NFS4_COPYNOTIFY_STID) {
6987                         state = NULL;            6562                         state = NULL;
6988                         goto unlock;             6563                         goto unlock;
6989                 }                                6564                 }
6990                 if (!clp)                        6565                 if (!clp)
6991                         refcount_inc(&state->    6566                         refcount_inc(&state->cp_stateid.cs_count);
6992                 else                             6567                 else
6993                         _free_cpntf_state_loc    6568                         _free_cpntf_state_locked(nn, state);
6994         }                                        6569         }
6995 unlock:                                          6570 unlock:
6996         spin_unlock(&nn->s2s_cp_lock);           6571         spin_unlock(&nn->s2s_cp_lock);
6997         if (!state)                              6572         if (!state)
6998                 return nfserr_bad_stateid;       6573                 return nfserr_bad_stateid;
6999         if (!clp)                                6574         if (!clp)
7000                 *cps = state;                    6575                 *cps = state;
7001         return 0;                                6576         return 0;
7002 }                                                6577 }
7003                                                  6578 
7004 static __be32 find_cpntf_state(struct nfsd_ne    6579 static __be32 find_cpntf_state(struct nfsd_net *nn, stateid_t *st,
7005                                struct nfs4_st    6580                                struct nfs4_stid **stid)
7006 {                                                6581 {
7007         __be32 status;                           6582         __be32 status;
7008         struct nfs4_cpntf_state *cps = NULL;     6583         struct nfs4_cpntf_state *cps = NULL;
7009         struct nfs4_client *found;               6584         struct nfs4_client *found;
7010                                                  6585 
7011         status = manage_cpntf_state(nn, st, N    6586         status = manage_cpntf_state(nn, st, NULL, &cps);
7012         if (status)                              6587         if (status)
7013                 return status;                   6588                 return status;
7014                                                  6589 
7015         cps->cpntf_time = ktime_get_boottime_    6590         cps->cpntf_time = ktime_get_boottime_seconds();
7016                                                  6591 
7017         status = nfserr_expired;                 6592         status = nfserr_expired;
7018         found = lookup_clientid(&cps->cp_p_cl    6593         found = lookup_clientid(&cps->cp_p_clid, true, nn);
7019         if (!found)                              6594         if (!found)
7020                 goto out;                        6595                 goto out;
7021                                                  6596 
7022         *stid = find_stateid_by_type(found, &    6597         *stid = find_stateid_by_type(found, &cps->cp_p_stateid,
7023                                      SC_TYPE_ !! 6598                         NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID);
7024                                      0);      << 
7025         if (*stid)                               6599         if (*stid)
7026                 status = nfs_ok;                 6600                 status = nfs_ok;
7027         else                                     6601         else
7028                 status = nfserr_bad_stateid;     6602                 status = nfserr_bad_stateid;
7029                                                  6603 
7030         put_client_renew(found);                 6604         put_client_renew(found);
7031 out:                                             6605 out:
7032         nfs4_put_cpntf_state(nn, cps);           6606         nfs4_put_cpntf_state(nn, cps);
7033         return status;                           6607         return status;
7034 }                                                6608 }
7035                                                  6609 
7036 void nfs4_put_cpntf_state(struct nfsd_net *nn    6610 void nfs4_put_cpntf_state(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
7037 {                                                6611 {
7038         spin_lock(&nn->s2s_cp_lock);             6612         spin_lock(&nn->s2s_cp_lock);
7039         _free_cpntf_state_locked(nn, cps);       6613         _free_cpntf_state_locked(nn, cps);
7040         spin_unlock(&nn->s2s_cp_lock);           6614         spin_unlock(&nn->s2s_cp_lock);
7041 }                                                6615 }
7042                                                  6616 
7043 /**                                              6617 /**
7044  * nfs4_preprocess_stateid_op - find and prep    6618  * nfs4_preprocess_stateid_op - find and prep stateid for an operation
7045  * @rqstp: incoming request from client          6619  * @rqstp: incoming request from client
7046  * @cstate: current compound state               6620  * @cstate: current compound state
7047  * @fhp: filehandle associated with requested    6621  * @fhp: filehandle associated with requested stateid
7048  * @stateid: stateid (provided by client)        6622  * @stateid: stateid (provided by client)
7049  * @flags: flags describing type of operation    6623  * @flags: flags describing type of operation to be done
7050  * @nfp: optional nfsd_file return pointer (m    6624  * @nfp: optional nfsd_file return pointer (may be NULL)
7051  * @cstid: optional returned nfs4_stid pointe    6625  * @cstid: optional returned nfs4_stid pointer (may be NULL)
7052  *                                               6626  *
7053  * Given info from the client, look up a nfs4    6627  * Given info from the client, look up a nfs4_stid for the operation. On
7054  * success, it returns a reference to the nfs    6628  * success, it returns a reference to the nfs4_stid and/or the nfsd_file
7055  * associated with it.                           6629  * associated with it.
7056  */                                              6630  */
7057 __be32                                           6631 __be32
7058 nfs4_preprocess_stateid_op(struct svc_rqst *r    6632 nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
7059                 struct nfsd4_compound_state *    6633                 struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
7060                 stateid_t *stateid, int flags    6634                 stateid_t *stateid, int flags, struct nfsd_file **nfp,
7061                 struct nfs4_stid **cstid)        6635                 struct nfs4_stid **cstid)
7062 {                                                6636 {
7063         struct net *net = SVC_NET(rqstp);        6637         struct net *net = SVC_NET(rqstp);
7064         struct nfsd_net *nn = net_generic(net    6638         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7065         struct nfs4_stid *s = NULL;              6639         struct nfs4_stid *s = NULL;
7066         __be32 status;                           6640         __be32 status;
7067                                                  6641 
7068         if (nfp)                                 6642         if (nfp)
7069                 *nfp = NULL;                     6643                 *nfp = NULL;
7070                                                  6644 
7071         if (ZERO_STATEID(stateid) || ONE_STAT    6645         if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
7072                 status = check_special_statei !! 6646                 if (cstid)
                                                   >> 6647                         status = nfserr_bad_stateid;
                                                   >> 6648                 else
                                                   >> 6649                         status = check_special_stateids(net, fhp, stateid,
                                                   >> 6650                                                                         flags);
7073                 goto done;                       6651                 goto done;
7074         }                                        6652         }
7075                                                  6653 
7076         status = nfsd4_lookup_stateid(cstate,    6654         status = nfsd4_lookup_stateid(cstate, stateid,
7077                                 SC_TYPE_DELEG !! 6655                                 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
7078                                 0, &s, nn);   !! 6656                                 &s, nn);
7079         if (status == nfserr_bad_stateid)        6657         if (status == nfserr_bad_stateid)
7080                 status = find_cpntf_state(nn,    6658                 status = find_cpntf_state(nn, stateid, &s);
7081         if (status)                              6659         if (status)
7082                 return status;                   6660                 return status;
7083         status = nfsd4_stid_check_stateid_gen    6661         status = nfsd4_stid_check_stateid_generation(stateid, s,
7084                         nfsd4_has_session(cst    6662                         nfsd4_has_session(cstate));
7085         if (status)                              6663         if (status)
7086                 goto out;                        6664                 goto out;
7087                                                  6665 
7088         switch (s->sc_type) {                    6666         switch (s->sc_type) {
7089         case SC_TYPE_DELEG:                   !! 6667         case NFS4_DELEG_STID:
7090                 status = nfs4_check_delegmode    6668                 status = nfs4_check_delegmode(delegstateid(s), flags);
7091                 break;                           6669                 break;
7092         case SC_TYPE_OPEN:                    !! 6670         case NFS4_OPEN_STID:
7093         case SC_TYPE_LOCK:                    !! 6671         case NFS4_LOCK_STID:
7094                 status = nfs4_check_olstateid    6672                 status = nfs4_check_olstateid(openlockstateid(s), flags);
7095                 break;                           6673                 break;
                                                   >> 6674         default:
                                                   >> 6675                 status = nfserr_bad_stateid;
                                                   >> 6676                 break;
7096         }                                        6677         }
7097         if (status)                              6678         if (status)
7098                 goto out;                        6679                 goto out;
7099         status = nfs4_check_fh(fhp, s);          6680         status = nfs4_check_fh(fhp, s);
7100                                                  6681 
7101 done:                                            6682 done:
7102         if (status == nfs_ok && nfp)             6683         if (status == nfs_ok && nfp)
7103                 status = nfs4_check_file(rqst    6684                 status = nfs4_check_file(rqstp, fhp, s, nfp, flags);
7104 out:                                             6685 out:
7105         if (s) {                                 6686         if (s) {
7106                 if (!status && cstid)            6687                 if (!status && cstid)
7107                         *cstid = s;              6688                         *cstid = s;
7108                 else                             6689                 else
7109                         nfs4_put_stid(s);        6690                         nfs4_put_stid(s);
7110         }                                        6691         }
7111         return status;                           6692         return status;
7112 }                                                6693 }
7113                                                  6694 
7114 /*                                               6695 /*
7115  * Test if the stateid is valid                  6696  * Test if the stateid is valid
7116  */                                              6697  */
7117 __be32                                           6698 __be32
7118 nfsd4_test_stateid(struct svc_rqst *rqstp, st    6699 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7119                    union nfsd4_op_u *u)          6700                    union nfsd4_op_u *u)
7120 {                                                6701 {
7121         struct nfsd4_test_stateid *test_state    6702         struct nfsd4_test_stateid *test_stateid = &u->test_stateid;
7122         struct nfsd4_test_stateid_id *stateid    6703         struct nfsd4_test_stateid_id *stateid;
7123         struct nfs4_client *cl = cstate->clp;    6704         struct nfs4_client *cl = cstate->clp;
7124                                                  6705 
7125         list_for_each_entry(stateid, &test_st    6706         list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
7126                 stateid->ts_id_status =          6707                 stateid->ts_id_status =
7127                         nfsd4_validate_statei    6708                         nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
7128                                                  6709 
7129         return nfs_ok;                           6710         return nfs_ok;
7130 }                                                6711 }
7131                                                  6712 
7132 static __be32                                    6713 static __be32
7133 nfsd4_free_lock_stateid(stateid_t *stateid, s    6714 nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
7134 {                                                6715 {
7135         struct nfs4_ol_stateid *stp = openloc    6716         struct nfs4_ol_stateid *stp = openlockstateid(s);
7136         __be32 ret;                              6717         __be32 ret;
7137                                                  6718 
7138         ret = nfsd4_lock_ol_stateid(stp);        6719         ret = nfsd4_lock_ol_stateid(stp);
7139         if (ret)                                 6720         if (ret)
7140                 goto out_put_stid;               6721                 goto out_put_stid;
7141                                                  6722 
7142         ret = check_stateid_generation(statei    6723         ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
7143         if (ret)                                 6724         if (ret)
7144                 goto out;                        6725                 goto out;
7145                                                  6726 
7146         ret = nfserr_locks_held;                 6727         ret = nfserr_locks_held;
7147         if (check_for_locks(stp->st_stid.sc_f    6728         if (check_for_locks(stp->st_stid.sc_file,
7148                             lockowner(stp->st    6729                             lockowner(stp->st_stateowner)))
7149                 goto out;                        6730                 goto out;
7150                                                  6731 
7151         release_lock_stateid(stp);               6732         release_lock_stateid(stp);
7152         ret = nfs_ok;                            6733         ret = nfs_ok;
7153                                                  6734 
7154 out:                                             6735 out:
7155         mutex_unlock(&stp->st_mutex);            6736         mutex_unlock(&stp->st_mutex);
7156 out_put_stid:                                    6737 out_put_stid:
7157         nfs4_put_stid(s);                        6738         nfs4_put_stid(s);
7158         return ret;                              6739         return ret;
7159 }                                                6740 }
7160                                                  6741 
7161 __be32                                           6742 __be32
7162 nfsd4_free_stateid(struct svc_rqst *rqstp, st    6743 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7163                    union nfsd4_op_u *u)          6744                    union nfsd4_op_u *u)
7164 {                                                6745 {
7165         struct nfsd4_free_stateid *free_state    6746         struct nfsd4_free_stateid *free_stateid = &u->free_stateid;
7166         stateid_t *stateid = &free_stateid->f    6747         stateid_t *stateid = &free_stateid->fr_stateid;
7167         struct nfs4_stid *s;                     6748         struct nfs4_stid *s;
7168         struct nfs4_delegation *dp;              6749         struct nfs4_delegation *dp;
7169         struct nfs4_client *cl = cstate->clp;    6750         struct nfs4_client *cl = cstate->clp;
7170         __be32 ret = nfserr_bad_stateid;         6751         __be32 ret = nfserr_bad_stateid;
7171                                                  6752 
7172         spin_lock(&cl->cl_lock);                 6753         spin_lock(&cl->cl_lock);
7173         s = find_stateid_locked(cl, stateid);    6754         s = find_stateid_locked(cl, stateid);
7174         if (!s || s->sc_status & SC_STATUS_CL !! 6755         if (!s)
7175                 goto out_unlock;                 6756                 goto out_unlock;
7176         if (s->sc_status & SC_STATUS_ADMIN_RE << 
7177                 nfsd4_drop_revoked_stid(s);   << 
7178                 ret = nfs_ok;                 << 
7179                 goto out;                     << 
7180         }                                     << 
7181         spin_lock(&s->sc_lock);                  6757         spin_lock(&s->sc_lock);
7182         switch (s->sc_type) {                    6758         switch (s->sc_type) {
7183         case SC_TYPE_DELEG:                   !! 6759         case NFS4_DELEG_STID:
7184                 if (s->sc_status & SC_STATUS_ << 
7185                         s->sc_status |= SC_ST << 
7186                         spin_unlock(&s->sc_lo << 
7187                         dp = delegstateid(s); << 
7188                         if (s->sc_status & SC << 
7189                                 list_del_init << 
7190                         s->sc_status |= SC_ST << 
7191                         spin_unlock(&cl->cl_l << 
7192                         nfs4_put_stid(s);     << 
7193                         ret = nfs_ok;         << 
7194                         goto out;             << 
7195                 }                             << 
7196                 ret = nfserr_locks_held;         6760                 ret = nfserr_locks_held;
7197                 break;                           6761                 break;
7198         case SC_TYPE_OPEN:                    !! 6762         case NFS4_OPEN_STID:
7199                 ret = check_stateid_generatio    6763                 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
7200                 if (ret)                         6764                 if (ret)
7201                         break;                   6765                         break;
7202                 ret = nfserr_locks_held;         6766                 ret = nfserr_locks_held;
7203                 break;                           6767                 break;
7204         case SC_TYPE_LOCK:                    !! 6768         case NFS4_LOCK_STID:
7205                 spin_unlock(&s->sc_lock);        6769                 spin_unlock(&s->sc_lock);
7206                 refcount_inc(&s->sc_count);      6770                 refcount_inc(&s->sc_count);
7207                 spin_unlock(&cl->cl_lock);       6771                 spin_unlock(&cl->cl_lock);
7208                 ret = nfsd4_free_lock_stateid    6772                 ret = nfsd4_free_lock_stateid(stateid, s);
7209                 goto out;                        6773                 goto out;
                                                   >> 6774         case NFS4_REVOKED_DELEG_STID:
                                                   >> 6775                 spin_unlock(&s->sc_lock);
                                                   >> 6776                 dp = delegstateid(s);
                                                   >> 6777                 list_del_init(&dp->dl_recall_lru);
                                                   >> 6778                 spin_unlock(&cl->cl_lock);
                                                   >> 6779                 nfs4_put_stid(s);
                                                   >> 6780                 ret = nfs_ok;
                                                   >> 6781                 goto out;
                                                   >> 6782         /* Default falls through and returns nfserr_bad_stateid */
7210         }                                        6783         }
7211         spin_unlock(&s->sc_lock);                6784         spin_unlock(&s->sc_lock);
7212 out_unlock:                                      6785 out_unlock:
7213         spin_unlock(&cl->cl_lock);               6786         spin_unlock(&cl->cl_lock);
7214 out:                                             6787 out:
7215         return ret;                              6788         return ret;
7216 }                                                6789 }
7217                                                  6790 
7218 static inline int                                6791 static inline int
7219 setlkflg (int type)                              6792 setlkflg (int type)
7220 {                                                6793 {
7221         return (type == NFS4_READW_LT || type    6794         return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
7222                 RD_STATE : WR_STATE;             6795                 RD_STATE : WR_STATE;
7223 }                                                6796 }
7224                                                  6797 
7225 static __be32 nfs4_seqid_op_checks(struct nfs    6798 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
7226 {                                                6799 {
7227         struct svc_fh *current_fh = &cstate->    6800         struct svc_fh *current_fh = &cstate->current_fh;
7228         struct nfs4_stateowner *sop = stp->st    6801         struct nfs4_stateowner *sop = stp->st_stateowner;
7229         __be32 status;                           6802         __be32 status;
7230                                                  6803 
7231         status = nfsd4_check_seqid(cstate, so    6804         status = nfsd4_check_seqid(cstate, sop, seqid);
7232         if (status)                              6805         if (status)
7233                 return status;                   6806                 return status;
7234         status = nfsd4_lock_ol_stateid(stp);     6807         status = nfsd4_lock_ol_stateid(stp);
7235         if (status != nfs_ok)                    6808         if (status != nfs_ok)
7236                 return status;                   6809                 return status;
7237         status = check_stateid_generation(sta    6810         status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
7238         if (status == nfs_ok)                    6811         if (status == nfs_ok)
7239                 status = nfs4_check_fh(curren    6812                 status = nfs4_check_fh(current_fh, &stp->st_stid);
7240         if (status != nfs_ok)                    6813         if (status != nfs_ok)
7241                 mutex_unlock(&stp->st_mutex);    6814                 mutex_unlock(&stp->st_mutex);
7242         return status;                           6815         return status;
7243 }                                                6816 }
7244                                                  6817 
7245 /**                                              6818 /**
7246  * nfs4_preprocess_seqid_op - find and prep a    6819  * nfs4_preprocess_seqid_op - find and prep an ol_stateid for a seqid-morphing op
7247  * @cstate: compund state                        6820  * @cstate: compund state
7248  * @seqid: seqid (provided by client)            6821  * @seqid: seqid (provided by client)
7249  * @stateid: stateid (provided by client)        6822  * @stateid: stateid (provided by client)
7250  * @typemask: mask of allowable types for thi    6823  * @typemask: mask of allowable types for this operation
7251  * @statusmask: mask of allowed states: 0 or  << 
7252  * @stpp: return pointer for the stateid foun    6824  * @stpp: return pointer for the stateid found
7253  * @nn: net namespace for request                6825  * @nn: net namespace for request
7254  *                                               6826  *
7255  * Given a stateid+seqid from a client, look     6827  * Given a stateid+seqid from a client, look up an nfs4_ol_stateid and
7256  * return it in @stpp. On a nfs_ok return, th    6828  * return it in @stpp. On a nfs_ok return, the returned stateid will
7257  * have its st_mutex locked.                     6829  * have its st_mutex locked.
7258  */                                              6830  */
7259 static __be32                                    6831 static __be32
7260 nfs4_preprocess_seqid_op(struct nfsd4_compoun    6832 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
7261                          stateid_t *stateid,  !! 6833                          stateid_t *stateid, char typemask,
7262                          unsigned short typem << 
7263                          struct nfs4_ol_state    6834                          struct nfs4_ol_stateid **stpp,
7264                          struct nfsd_net *nn)    6835                          struct nfsd_net *nn)
7265 {                                                6836 {
7266         __be32 status;                           6837         __be32 status;
7267         struct nfs4_stid *s;                     6838         struct nfs4_stid *s;
7268         struct nfs4_ol_stateid *stp = NULL;      6839         struct nfs4_ol_stateid *stp = NULL;
7269                                                  6840 
7270         trace_nfsd_preprocess(seqid, stateid)    6841         trace_nfsd_preprocess(seqid, stateid);
7271                                                  6842 
7272         *stpp = NULL;                            6843         *stpp = NULL;
7273 retry:                                        !! 6844         status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
7274         status = nfsd4_lookup_stateid(cstate, << 
7275                                       typemas << 
7276         if (status)                              6845         if (status)
7277                 return status;                   6846                 return status;
7278         stp = openlockstateid(s);                6847         stp = openlockstateid(s);
7279         if (nfsd4_cstate_assign_replay(cstate !! 6848         nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
7280                 nfs4_put_stateowner(stp->st_s << 
7281                 goto retry;                   << 
7282         }                                     << 
7283                                                  6849 
7284         status = nfs4_seqid_op_checks(cstate,    6850         status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
7285         if (!status)                             6851         if (!status)
7286                 *stpp = stp;                     6852                 *stpp = stp;
7287         else                                     6853         else
7288                 nfs4_put_stid(&stp->st_stid);    6854                 nfs4_put_stid(&stp->st_stid);
7289         return status;                           6855         return status;
7290 }                                                6856 }
7291                                                  6857 
7292 static __be32 nfs4_preprocess_confirmed_seqid    6858 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
7293                                                  6859                                                  stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
7294 {                                                6860 {
7295         __be32 status;                           6861         __be32 status;
7296         struct nfs4_openowner *oo;               6862         struct nfs4_openowner *oo;
7297         struct nfs4_ol_stateid *stp;             6863         struct nfs4_ol_stateid *stp;
7298                                                  6864 
7299         status = nfs4_preprocess_seqid_op(cst    6865         status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
7300                                           SC_ !! 6866                                                 NFS4_OPEN_STID, &stp, nn);
7301         if (status)                              6867         if (status)
7302                 return status;                   6868                 return status;
7303         oo = openowner(stp->st_stateowner);      6869         oo = openowner(stp->st_stateowner);
7304         if (!(oo->oo_flags & NFS4_OO_CONFIRME    6870         if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
7305                 mutex_unlock(&stp->st_mutex);    6871                 mutex_unlock(&stp->st_mutex);
7306                 nfs4_put_stid(&stp->st_stid);    6872                 nfs4_put_stid(&stp->st_stid);
7307                 return nfserr_bad_stateid;       6873                 return nfserr_bad_stateid;
7308         }                                        6874         }
7309         *stpp = stp;                             6875         *stpp = stp;
7310         return nfs_ok;                           6876         return nfs_ok;
7311 }                                                6877 }
7312                                                  6878 
7313 __be32                                           6879 __be32
7314 nfsd4_open_confirm(struct svc_rqst *rqstp, st    6880 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7315                    union nfsd4_op_u *u)          6881                    union nfsd4_op_u *u)
7316 {                                                6882 {
7317         struct nfsd4_open_confirm *oc = &u->o    6883         struct nfsd4_open_confirm *oc = &u->open_confirm;
7318         __be32 status;                           6884         __be32 status;
7319         struct nfs4_openowner *oo;               6885         struct nfs4_openowner *oo;
7320         struct nfs4_ol_stateid *stp;             6886         struct nfs4_ol_stateid *stp;
7321         struct nfsd_net *nn = net_generic(SVC    6887         struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
7322                                                  6888 
7323         dprintk("NFSD: nfsd4_open_confirm on     6889         dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
7324                         cstate->current_fh.fh    6890                         cstate->current_fh.fh_dentry);
7325                                                  6891 
7326         status = fh_verify(rqstp, &cstate->cu    6892         status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
7327         if (status)                              6893         if (status)
7328                 return status;                   6894                 return status;
7329                                                  6895 
7330         status = nfs4_preprocess_seqid_op(cst    6896         status = nfs4_preprocess_seqid_op(cstate,
7331                                           oc- !! 6897                                         oc->oc_seqid, &oc->oc_req_stateid,
7332                                           SC_ !! 6898                                         NFS4_OPEN_STID, &stp, nn);
7333         if (status)                              6899         if (status)
7334                 goto out;                        6900                 goto out;
7335         oo = openowner(stp->st_stateowner);      6901         oo = openowner(stp->st_stateowner);
7336         status = nfserr_bad_stateid;             6902         status = nfserr_bad_stateid;
7337         if (oo->oo_flags & NFS4_OO_CONFIRMED)    6903         if (oo->oo_flags & NFS4_OO_CONFIRMED) {
7338                 mutex_unlock(&stp->st_mutex);    6904                 mutex_unlock(&stp->st_mutex);
7339                 goto put_stateid;                6905                 goto put_stateid;
7340         }                                        6906         }
7341         oo->oo_flags |= NFS4_OO_CONFIRMED;       6907         oo->oo_flags |= NFS4_OO_CONFIRMED;
7342         nfs4_inc_and_copy_stateid(&oc->oc_res    6908         nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
7343         mutex_unlock(&stp->st_mutex);            6909         mutex_unlock(&stp->st_mutex);
7344         trace_nfsd_open_confirm(oc->oc_seqid,    6910         trace_nfsd_open_confirm(oc->oc_seqid, &stp->st_stid.sc_stateid);
7345         nfsd4_client_record_create(oo->oo_own    6911         nfsd4_client_record_create(oo->oo_owner.so_client);
7346         status = nfs_ok;                         6912         status = nfs_ok;
7347 put_stateid:                                     6913 put_stateid:
7348         nfs4_put_stid(&stp->st_stid);            6914         nfs4_put_stid(&stp->st_stid);
7349 out:                                             6915 out:
7350         nfsd4_bump_seqid(cstate, status);        6916         nfsd4_bump_seqid(cstate, status);
7351         return status;                           6917         return status;
7352 }                                                6918 }
7353                                                  6919 
7354 static inline void nfs4_stateid_downgrade_bit    6920 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
7355 {                                                6921 {
7356         if (!test_access(access, stp))           6922         if (!test_access(access, stp))
7357                 return;                          6923                 return;
7358         nfs4_file_put_access(stp->st_stid.sc_    6924         nfs4_file_put_access(stp->st_stid.sc_file, access);
7359         clear_access(access, stp);               6925         clear_access(access, stp);
7360 }                                                6926 }
7361                                                  6927 
7362 static inline void nfs4_stateid_downgrade(str    6928 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
7363 {                                                6929 {
7364         switch (to_access) {                     6930         switch (to_access) {
7365         case NFS4_SHARE_ACCESS_READ:             6931         case NFS4_SHARE_ACCESS_READ:
7366                 nfs4_stateid_downgrade_bit(st    6932                 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
7367                 nfs4_stateid_downgrade_bit(st    6933                 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
7368                 break;                           6934                 break;
7369         case NFS4_SHARE_ACCESS_WRITE:            6935         case NFS4_SHARE_ACCESS_WRITE:
7370                 nfs4_stateid_downgrade_bit(st    6936                 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
7371                 nfs4_stateid_downgrade_bit(st    6937                 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
7372                 break;                           6938                 break;
7373         case NFS4_SHARE_ACCESS_BOTH:             6939         case NFS4_SHARE_ACCESS_BOTH:
7374                 break;                           6940                 break;
7375         default:                                 6941         default:
7376                 WARN_ON_ONCE(1);                 6942                 WARN_ON_ONCE(1);
7377         }                                        6943         }
7378 }                                                6944 }
7379                                                  6945 
7380 __be32                                           6946 __be32
7381 nfsd4_open_downgrade(struct svc_rqst *rqstp,     6947 nfsd4_open_downgrade(struct svc_rqst *rqstp,
7382                      struct nfsd4_compound_st    6948                      struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
7383 {                                                6949 {
7384         struct nfsd4_open_downgrade *od = &u-    6950         struct nfsd4_open_downgrade *od = &u->open_downgrade;
7385         __be32 status;                           6951         __be32 status;
7386         struct nfs4_ol_stateid *stp;             6952         struct nfs4_ol_stateid *stp;
7387         struct nfsd_net *nn = net_generic(SVC    6953         struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
7388                                                  6954 
7389         dprintk("NFSD: nfsd4_open_downgrade o    6955         dprintk("NFSD: nfsd4_open_downgrade on file %pd\n", 
7390                         cstate->current_fh.fh    6956                         cstate->current_fh.fh_dentry);
7391                                                  6957 
7392         /* We don't yet support WANT bits: */    6958         /* We don't yet support WANT bits: */
7393         if (od->od_deleg_want)                   6959         if (od->od_deleg_want)
7394                 dprintk("NFSD: %s: od_deleg_w    6960                 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
7395                         od->od_deleg_want);      6961                         od->od_deleg_want);
7396                                                  6962 
7397         status = nfs4_preprocess_confirmed_se    6963         status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
7398                                         &od->    6964                                         &od->od_stateid, &stp, nn);
7399         if (status)                              6965         if (status)
7400                 goto out;                        6966                 goto out; 
7401         status = nfserr_inval;                   6967         status = nfserr_inval;
7402         if (!test_access(od->od_share_access,    6968         if (!test_access(od->od_share_access, stp)) {
7403                 dprintk("NFSD: access not a s    6969                 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
7404                         stp->st_access_bmap,     6970                         stp->st_access_bmap, od->od_share_access);
7405                 goto put_stateid;                6971                 goto put_stateid;
7406         }                                        6972         }
7407         if (!test_deny(od->od_share_deny, stp    6973         if (!test_deny(od->od_share_deny, stp)) {
7408                 dprintk("NFSD: deny not a sub    6974                 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
7409                         stp->st_deny_bmap, od    6975                         stp->st_deny_bmap, od->od_share_deny);
7410                 goto put_stateid;                6976                 goto put_stateid;
7411         }                                        6977         }
7412         nfs4_stateid_downgrade(stp, od->od_sh    6978         nfs4_stateid_downgrade(stp, od->od_share_access);
7413         reset_union_bmap_deny(od->od_share_de    6979         reset_union_bmap_deny(od->od_share_deny, stp);
7414         nfs4_inc_and_copy_stateid(&od->od_sta    6980         nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
7415         status = nfs_ok;                         6981         status = nfs_ok;
7416 put_stateid:                                     6982 put_stateid:
7417         mutex_unlock(&stp->st_mutex);            6983         mutex_unlock(&stp->st_mutex);
7418         nfs4_put_stid(&stp->st_stid);            6984         nfs4_put_stid(&stp->st_stid);
7419 out:                                             6985 out:
7420         nfsd4_bump_seqid(cstate, status);        6986         nfsd4_bump_seqid(cstate, status);
7421         return status;                           6987         return status;
7422 }                                                6988 }
7423                                                  6989 
7424 static bool nfsd4_close_open_stateid(struct n !! 6990 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
7425 {                                                6991 {
7426         struct nfs4_client *clp = s->st_stid.    6992         struct nfs4_client *clp = s->st_stid.sc_client;
7427         bool unhashed;                           6993         bool unhashed;
7428         LIST_HEAD(reaplist);                     6994         LIST_HEAD(reaplist);
7429         struct nfs4_ol_stateid *stp;             6995         struct nfs4_ol_stateid *stp;
7430                                                  6996 
7431         spin_lock(&clp->cl_lock);                6997         spin_lock(&clp->cl_lock);
7432         unhashed = unhash_open_stateid(s, &re    6998         unhashed = unhash_open_stateid(s, &reaplist);
7433                                                  6999 
7434         if (clp->cl_minorversion) {              7000         if (clp->cl_minorversion) {
7435                 if (unhashed)                    7001                 if (unhashed)
7436                         put_ol_stateid_locked    7002                         put_ol_stateid_locked(s, &reaplist);
7437                 spin_unlock(&clp->cl_lock);      7003                 spin_unlock(&clp->cl_lock);
7438                 list_for_each_entry(stp, &rea    7004                 list_for_each_entry(stp, &reaplist, st_locks)
7439                         nfs4_free_cpntf_state    7005                         nfs4_free_cpntf_statelist(clp->net, &stp->st_stid);
7440                 free_ol_stateid_reaplist(&rea    7006                 free_ol_stateid_reaplist(&reaplist);
7441                 return false;                 << 
7442         } else {                                 7007         } else {
7443                 spin_unlock(&clp->cl_lock);      7008                 spin_unlock(&clp->cl_lock);
7444                 free_ol_stateid_reaplist(&rea    7009                 free_ol_stateid_reaplist(&reaplist);
7445                 return unhashed;              !! 7010                 if (unhashed)
                                                   >> 7011                         move_to_close_lru(s, clp->net);
7446         }                                        7012         }
7447 }                                                7013 }
7448                                                  7014 
7449 /*                                               7015 /*
7450  * nfs4_unlock_state() called after encode       7016  * nfs4_unlock_state() called after encode
7451  */                                              7017  */
7452 __be32                                           7018 __be32
7453 nfsd4_close(struct svc_rqst *rqstp, struct nf    7019 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7454                 union nfsd4_op_u *u)             7020                 union nfsd4_op_u *u)
7455 {                                                7021 {
7456         struct nfsd4_close *close = &u->close    7022         struct nfsd4_close *close = &u->close;
7457         __be32 status;                           7023         __be32 status;
7458         struct nfs4_ol_stateid *stp;             7024         struct nfs4_ol_stateid *stp;
7459         struct net *net = SVC_NET(rqstp);        7025         struct net *net = SVC_NET(rqstp);
7460         struct nfsd_net *nn = net_generic(net    7026         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7461         bool need_move_to_close_list;         << 
7462                                                  7027 
7463         dprintk("NFSD: nfsd4_close on file %p !! 7028         dprintk("NFSD: nfsd4_close on file %pd\n", 
7464                         cstate->current_fh.fh    7029                         cstate->current_fh.fh_dentry);
7465                                                  7030 
7466         status = nfs4_preprocess_seqid_op(cst    7031         status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
7467                                           &cl !! 7032                                         &close->cl_stateid,
7468                                           SC_ !! 7033                                         NFS4_OPEN_STID|NFS4_CLOSED_STID,
7469                                           &st !! 7034                                         &stp, nn);
7470         nfsd4_bump_seqid(cstate, status);        7035         nfsd4_bump_seqid(cstate, status);
7471         if (status)                              7036         if (status)
7472                 goto out;                     !! 7037                 goto out; 
7473                                                  7038 
7474         spin_lock(&stp->st_stid.sc_client->cl !! 7039         stp->st_stid.sc_type = NFS4_CLOSED_STID;
7475         stp->st_stid.sc_status |= SC_STATUS_C << 
7476         spin_unlock(&stp->st_stid.sc_client-> << 
7477                                                  7040 
7478         /*                                       7041         /*
7479          * Technically we don't _really_ have    7042          * Technically we don't _really_ have to increment or copy it, since
7480          * it should just be gone after this     7043          * it should just be gone after this operation and we clobber the
7481          * copied value below, but we continu    7044          * copied value below, but we continue to do so here just to ensure
7482          * that racing ops see that there was    7045          * that racing ops see that there was a state change.
7483          */                                      7046          */
7484         nfs4_inc_and_copy_stateid(&close->cl_    7047         nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
7485                                                  7048 
7486         need_move_to_close_list = nfsd4_close !! 7049         nfsd4_close_open_stateid(stp);
7487         mutex_unlock(&stp->st_mutex);            7050         mutex_unlock(&stp->st_mutex);
7488         if (need_move_to_close_list)          << 
7489                 move_to_close_lru(stp, net);  << 
7490                                                  7051 
7491         /* v4.1+ suggests that we send a spec    7052         /* v4.1+ suggests that we send a special stateid in here, since the
7492          * clients should just ignore this an    7053          * clients should just ignore this anyway. Since this is not useful
7493          * for v4.0 clients either, we set it    7054          * for v4.0 clients either, we set it to the special close_stateid
7494          * universally.                          7055          * universally.
7495          *                                       7056          *
7496          * See RFC5661 section 18.2.4, and RF    7057          * See RFC5661 section 18.2.4, and RFC7530 section 16.2.5
7497          */                                      7058          */
7498         memcpy(&close->cl_stateid, &close_sta    7059         memcpy(&close->cl_stateid, &close_stateid, sizeof(close->cl_stateid));
7499                                                  7060 
7500         /* put reference from nfs4_preprocess    7061         /* put reference from nfs4_preprocess_seqid_op */
7501         nfs4_put_stid(&stp->st_stid);            7062         nfs4_put_stid(&stp->st_stid);
7502 out:                                             7063 out:
7503         return status;                           7064         return status;
7504 }                                                7065 }
7505                                                  7066 
7506 __be32                                           7067 __be32
7507 nfsd4_delegreturn(struct svc_rqst *rqstp, str    7068 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7508                   union nfsd4_op_u *u)           7069                   union nfsd4_op_u *u)
7509 {                                                7070 {
7510         struct nfsd4_delegreturn *dr = &u->de    7071         struct nfsd4_delegreturn *dr = &u->delegreturn;
7511         struct nfs4_delegation *dp;              7072         struct nfs4_delegation *dp;
7512         stateid_t *stateid = &dr->dr_stateid;    7073         stateid_t *stateid = &dr->dr_stateid;
7513         struct nfs4_stid *s;                     7074         struct nfs4_stid *s;
7514         __be32 status;                           7075         __be32 status;
7515         struct nfsd_net *nn = net_generic(SVC    7076         struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
7516                                                  7077 
7517         if ((status = fh_verify(rqstp, &cstat    7078         if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
7518                 return status;                   7079                 return status;
7519                                                  7080 
7520         status = nfsd4_lookup_stateid(cstate, !! 7081         status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
7521                                       SC_STAT << 
7522                                       &s, nn) << 
7523         if (status)                              7082         if (status)
7524                 goto out;                        7083                 goto out;
7525         dp = delegstateid(s);                    7084         dp = delegstateid(s);
7526         status = nfsd4_stid_check_stateid_gen    7085         status = nfsd4_stid_check_stateid_generation(stateid, &dp->dl_stid, nfsd4_has_session(cstate));
7527         if (status)                              7086         if (status)
7528                 goto put_stateid;                7087                 goto put_stateid;
7529                                                  7088 
7530         trace_nfsd_deleg_return(stateid);        7089         trace_nfsd_deleg_return(stateid);
7531         destroy_delegation(dp);               << 
7532         smp_mb__after_atomic();               << 
7533         wake_up_var(d_inode(cstate->current_f    7090         wake_up_var(d_inode(cstate->current_fh.fh_dentry));
                                                   >> 7091         destroy_delegation(dp);
7534 put_stateid:                                     7092 put_stateid:
7535         nfs4_put_stid(&dp->dl_stid);             7093         nfs4_put_stid(&dp->dl_stid);
7536 out:                                             7094 out:
7537         return status;                           7095         return status;
7538 }                                                7096 }
7539                                                  7097 
7540 /* last octet in a range */                      7098 /* last octet in a range */
7541 static inline u64                                7099 static inline u64
7542 last_byte_offset(u64 start, u64 len)             7100 last_byte_offset(u64 start, u64 len)
7543 {                                                7101 {
7544         u64 end;                                 7102         u64 end;
7545                                                  7103 
7546         WARN_ON_ONCE(!len);                      7104         WARN_ON_ONCE(!len);
7547         end = start + len;                       7105         end = start + len;
7548         return end > start ? end - 1: NFS4_MA    7106         return end > start ? end - 1: NFS4_MAX_UINT64;
7549 }                                                7107 }
7550                                                  7108 
7551 /*                                               7109 /*
7552  * TODO: Linux file offsets are _signed_ 64-b    7110  * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
7553  * we can't properly handle lock requests tha    7111  * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
7554  * byte, because of sign extension problems.     7112  * byte, because of sign extension problems.  Since NFSv4 calls for 64-bit
7555  * locking, this prevents us from being compl    7113  * locking, this prevents us from being completely protocol-compliant.  The
7556  * real solution to this problem is to start     7114  * real solution to this problem is to start using unsigned file offsets in
7557  * the VFS, but this is a very deep change!      7115  * the VFS, but this is a very deep change!
7558  */                                              7116  */
7559 static inline void                               7117 static inline void
7560 nfs4_transform_lock_offset(struct file_lock *    7118 nfs4_transform_lock_offset(struct file_lock *lock)
7561 {                                                7119 {
7562         if (lock->fl_start < 0)                  7120         if (lock->fl_start < 0)
7563                 lock->fl_start = OFFSET_MAX;     7121                 lock->fl_start = OFFSET_MAX;
7564         if (lock->fl_end < 0)                    7122         if (lock->fl_end < 0)
7565                 lock->fl_end = OFFSET_MAX;       7123                 lock->fl_end = OFFSET_MAX;
7566 }                                                7124 }
7567                                                  7125 
7568 static fl_owner_t                                7126 static fl_owner_t
7569 nfsd4_lm_get_owner(fl_owner_t owner)             7127 nfsd4_lm_get_owner(fl_owner_t owner)
7570 {                                                7128 {
7571         struct nfs4_lockowner *lo = (struct n    7129         struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
7572                                                  7130 
7573         nfs4_get_stateowner(&lo->lo_owner);      7131         nfs4_get_stateowner(&lo->lo_owner);
7574         return owner;                            7132         return owner;
7575 }                                                7133 }
7576                                                  7134 
7577 static void                                      7135 static void
7578 nfsd4_lm_put_owner(fl_owner_t owner)             7136 nfsd4_lm_put_owner(fl_owner_t owner)
7579 {                                                7137 {
7580         struct nfs4_lockowner *lo = (struct n    7138         struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
7581                                                  7139 
7582         if (lo)                                  7140         if (lo)
7583                 nfs4_put_stateowner(&lo->lo_o    7141                 nfs4_put_stateowner(&lo->lo_owner);
7584 }                                                7142 }
7585                                                  7143 
7586 /* return pointer to struct nfs4_client if cl    7144 /* return pointer to struct nfs4_client if client is expirable */
7587 static bool                                      7145 static bool
7588 nfsd4_lm_lock_expirable(struct file_lock *cfl    7146 nfsd4_lm_lock_expirable(struct file_lock *cfl)
7589 {                                                7147 {
7590         struct nfs4_lockowner *lo = (struct n !! 7148         struct nfs4_lockowner *lo = (struct nfs4_lockowner *)cfl->fl_owner;
7591         struct nfs4_client *clp = lo->lo_owne    7149         struct nfs4_client *clp = lo->lo_owner.so_client;
7592         struct nfsd_net *nn;                     7150         struct nfsd_net *nn;
7593                                                  7151 
7594         if (try_to_expire_client(clp)) {         7152         if (try_to_expire_client(clp)) {
7595                 nn = net_generic(clp->net, nf    7153                 nn = net_generic(clp->net, nfsd_net_id);
7596                 mod_delayed_work(laundry_wq,     7154                 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
7597                 return true;                     7155                 return true;
7598         }                                        7156         }
7599         return false;                            7157         return false;
7600 }                                                7158 }
7601                                                  7159 
7602 /* schedule laundromat to run immediately and    7160 /* schedule laundromat to run immediately and wait for it to complete */
7603 static void                                      7161 static void
7604 nfsd4_lm_expire_lock(void)                       7162 nfsd4_lm_expire_lock(void)
7605 {                                                7163 {
7606         flush_workqueue(laundry_wq);             7164         flush_workqueue(laundry_wq);
7607 }                                                7165 }
7608                                                  7166 
7609 static void                                      7167 static void
7610 nfsd4_lm_notify(struct file_lock *fl)            7168 nfsd4_lm_notify(struct file_lock *fl)
7611 {                                                7169 {
7612         struct nfs4_lockowner           *lo = !! 7170         struct nfs4_lockowner           *lo = (struct nfs4_lockowner *)fl->fl_owner;
7613         struct net                      *net     7171         struct net                      *net = lo->lo_owner.so_client->net;
7614         struct nfsd_net                 *nn =    7172         struct nfsd_net                 *nn = net_generic(net, nfsd_net_id);
7615         struct nfsd4_blocked_lock       *nbl     7173         struct nfsd4_blocked_lock       *nbl = container_of(fl,
7616                                                  7174                                                 struct nfsd4_blocked_lock, nbl_lock);
7617         bool queue = false;                      7175         bool queue = false;
7618                                                  7176 
7619         /* An empty list means that something    7177         /* An empty list means that something else is going to be using it */
7620         spin_lock(&nn->blocked_locks_lock);      7178         spin_lock(&nn->blocked_locks_lock);
7621         if (!list_empty(&nbl->nbl_list)) {       7179         if (!list_empty(&nbl->nbl_list)) {
7622                 list_del_init(&nbl->nbl_list)    7180                 list_del_init(&nbl->nbl_list);
7623                 list_del_init(&nbl->nbl_lru);    7181                 list_del_init(&nbl->nbl_lru);
7624                 queue = true;                    7182                 queue = true;
7625         }                                        7183         }
7626         spin_unlock(&nn->blocked_locks_lock);    7184         spin_unlock(&nn->blocked_locks_lock);
7627                                                  7185 
7628         if (queue) {                             7186         if (queue) {
7629                 trace_nfsd_cb_notify_lock(lo,    7187                 trace_nfsd_cb_notify_lock(lo, nbl);
7630                 nfsd4_run_cb(&nbl->nbl_cb);      7188                 nfsd4_run_cb(&nbl->nbl_cb);
7631         }                                        7189         }
7632 }                                                7190 }
7633                                                  7191 
7634 static const struct lock_manager_operations n    7192 static const struct lock_manager_operations nfsd_posix_mng_ops  = {
7635         .lm_mod_owner = THIS_MODULE,             7193         .lm_mod_owner = THIS_MODULE,
7636         .lm_notify = nfsd4_lm_notify,            7194         .lm_notify = nfsd4_lm_notify,
7637         .lm_get_owner = nfsd4_lm_get_owner,      7195         .lm_get_owner = nfsd4_lm_get_owner,
7638         .lm_put_owner = nfsd4_lm_put_owner,      7196         .lm_put_owner = nfsd4_lm_put_owner,
7639         .lm_lock_expirable = nfsd4_lm_lock_ex    7197         .lm_lock_expirable = nfsd4_lm_lock_expirable,
7640         .lm_expire_lock = nfsd4_lm_expire_loc    7198         .lm_expire_lock = nfsd4_lm_expire_lock,
7641 };                                               7199 };
7642                                                  7200 
7643 static inline void                               7201 static inline void
7644 nfs4_set_lock_denied(struct file_lock *fl, st    7202 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
7645 {                                                7203 {
7646         struct nfs4_lockowner *lo;               7204         struct nfs4_lockowner *lo;
7647                                                  7205 
7648         if (fl->fl_lmops == &nfsd_posix_mng_o    7206         if (fl->fl_lmops == &nfsd_posix_mng_ops) {
7649                 lo = (struct nfs4_lockowner * !! 7207                 lo = (struct nfs4_lockowner *) fl->fl_owner;
7650                 xdr_netobj_dup(&deny->ld_owne    7208                 xdr_netobj_dup(&deny->ld_owner, &lo->lo_owner.so_owner,
7651                                                  7209                                                 GFP_KERNEL);
7652                 if (!deny->ld_owner.data)        7210                 if (!deny->ld_owner.data)
7653                         /* We just don't care    7211                         /* We just don't care that much */
7654                         goto nevermind;          7212                         goto nevermind;
7655                 deny->ld_clientid = lo->lo_ow    7213                 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
7656         } else {                                 7214         } else {
7657 nevermind:                                       7215 nevermind:
7658                 deny->ld_owner.len = 0;          7216                 deny->ld_owner.len = 0;
7659                 deny->ld_owner.data = NULL;      7217                 deny->ld_owner.data = NULL;
7660                 deny->ld_clientid.cl_boot = 0    7218                 deny->ld_clientid.cl_boot = 0;
7661                 deny->ld_clientid.cl_id = 0;     7219                 deny->ld_clientid.cl_id = 0;
7662         }                                        7220         }
7663         deny->ld_start = fl->fl_start;           7221         deny->ld_start = fl->fl_start;
7664         deny->ld_length = NFS4_MAX_UINT64;       7222         deny->ld_length = NFS4_MAX_UINT64;
7665         if (fl->fl_end != NFS4_MAX_UINT64)       7223         if (fl->fl_end != NFS4_MAX_UINT64)
7666                 deny->ld_length = fl->fl_end     7224                 deny->ld_length = fl->fl_end - fl->fl_start + 1;        
7667         deny->ld_type = NFS4_READ_LT;            7225         deny->ld_type = NFS4_READ_LT;
7668         if (fl->c.flc_type != F_RDLCK)        !! 7226         if (fl->fl_type != F_RDLCK)
7669                 deny->ld_type = NFS4_WRITE_LT    7227                 deny->ld_type = NFS4_WRITE_LT;
7670 }                                                7228 }
7671                                                  7229 
7672 static struct nfs4_lockowner *                   7230 static struct nfs4_lockowner *
7673 find_lockowner_str_locked(struct nfs4_client     7231 find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
7674 {                                                7232 {
7675         unsigned int strhashval = ownerstr_ha    7233         unsigned int strhashval = ownerstr_hashval(owner);
7676         struct nfs4_stateowner *so;              7234         struct nfs4_stateowner *so;
7677                                                  7235 
7678         lockdep_assert_held(&clp->cl_lock);      7236         lockdep_assert_held(&clp->cl_lock);
7679                                                  7237 
7680         list_for_each_entry(so, &clp->cl_owne    7238         list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
7681                             so_strhash) {        7239                             so_strhash) {
7682                 if (so->so_is_open_owner)        7240                 if (so->so_is_open_owner)
7683                         continue;                7241                         continue;
7684                 if (same_owner_str(so, owner)    7242                 if (same_owner_str(so, owner))
7685                         return lockowner(nfs4    7243                         return lockowner(nfs4_get_stateowner(so));
7686         }                                        7244         }
7687         return NULL;                             7245         return NULL;
7688 }                                                7246 }
7689                                                  7247 
7690 static struct nfs4_lockowner *                   7248 static struct nfs4_lockowner *
7691 find_lockowner_str(struct nfs4_client *clp, s    7249 find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
7692 {                                                7250 {
7693         struct nfs4_lockowner *lo;               7251         struct nfs4_lockowner *lo;
7694                                                  7252 
7695         spin_lock(&clp->cl_lock);                7253         spin_lock(&clp->cl_lock);
7696         lo = find_lockowner_str_locked(clp, o    7254         lo = find_lockowner_str_locked(clp, owner);
7697         spin_unlock(&clp->cl_lock);              7255         spin_unlock(&clp->cl_lock);
7698         return lo;                               7256         return lo;
7699 }                                                7257 }
7700                                                  7258 
7701 static void nfs4_unhash_lockowner(struct nfs4    7259 static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
7702 {                                                7260 {
7703         unhash_lockowner_locked(lockowner(sop    7261         unhash_lockowner_locked(lockowner(sop));
7704 }                                                7262 }
7705                                                  7263 
7706 static void nfs4_free_lockowner(struct nfs4_s    7264 static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
7707 {                                                7265 {
7708         struct nfs4_lockowner *lo = lockowner    7266         struct nfs4_lockowner *lo = lockowner(sop);
7709                                                  7267 
7710         kmem_cache_free(lockowner_slab, lo);     7268         kmem_cache_free(lockowner_slab, lo);
7711 }                                                7269 }
7712                                                  7270 
7713 static const struct nfs4_stateowner_operation    7271 static const struct nfs4_stateowner_operations lockowner_ops = {
7714         .so_unhash =    nfs4_unhash_lockowner    7272         .so_unhash =    nfs4_unhash_lockowner,
7715         .so_free =      nfs4_free_lockowner,     7273         .so_free =      nfs4_free_lockowner,
7716 };                                               7274 };
7717                                                  7275 
7718 /*                                               7276 /*
7719  * Alloc a lock owner structure.                 7277  * Alloc a lock owner structure.
7720  * Called in nfsd4_lock - therefore, OPEN and    7278  * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has 
7721  * occurred.                                     7279  * occurred. 
7722  *                                               7280  *
7723  * strhashval = ownerstr_hashval                 7281  * strhashval = ownerstr_hashval
7724  */                                              7282  */
7725 static struct nfs4_lockowner *                   7283 static struct nfs4_lockowner *
7726 alloc_init_lock_stateowner(unsigned int strha    7284 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
7727                            struct nfs4_ol_sta    7285                            struct nfs4_ol_stateid *open_stp,
7728                            struct nfsd4_lock     7286                            struct nfsd4_lock *lock)
7729 {                                                7287 {
7730         struct nfs4_lockowner *lo, *ret;         7288         struct nfs4_lockowner *lo, *ret;
7731                                                  7289 
7732         lo = alloc_stateowner(lockowner_slab,    7290         lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
7733         if (!lo)                                 7291         if (!lo)
7734                 return NULL;                     7292                 return NULL;
7735         INIT_LIST_HEAD(&lo->lo_blocked);         7293         INIT_LIST_HEAD(&lo->lo_blocked);
7736         INIT_LIST_HEAD(&lo->lo_owner.so_state    7294         INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
7737         lo->lo_owner.so_is_open_owner = 0;       7295         lo->lo_owner.so_is_open_owner = 0;
7738         lo->lo_owner.so_seqid = lock->lk_new_    7296         lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
7739         lo->lo_owner.so_ops = &lockowner_ops;    7297         lo->lo_owner.so_ops = &lockowner_ops;
7740         spin_lock(&clp->cl_lock);                7298         spin_lock(&clp->cl_lock);
7741         ret = find_lockowner_str_locked(clp,     7299         ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
7742         if (ret == NULL) {                       7300         if (ret == NULL) {
7743                 list_add(&lo->lo_owner.so_str    7301                 list_add(&lo->lo_owner.so_strhash,
7744                          &clp->cl_ownerstr_ha    7302                          &clp->cl_ownerstr_hashtbl[strhashval]);
7745                 ret = lo;                        7303                 ret = lo;
7746         } else                                   7304         } else
7747                 nfs4_free_stateowner(&lo->lo_    7305                 nfs4_free_stateowner(&lo->lo_owner);
7748                                                  7306 
7749         spin_unlock(&clp->cl_lock);              7307         spin_unlock(&clp->cl_lock);
7750         return ret;                              7308         return ret;
7751 }                                                7309 }
7752                                                  7310 
7753 static struct nfs4_ol_stateid *                  7311 static struct nfs4_ol_stateid *
7754 find_lock_stateid(const struct nfs4_lockowner    7312 find_lock_stateid(const struct nfs4_lockowner *lo,
7755                   const struct nfs4_ol_statei    7313                   const struct nfs4_ol_stateid *ost)
7756 {                                                7314 {
7757         struct nfs4_ol_stateid *lst;             7315         struct nfs4_ol_stateid *lst;
7758                                                  7316 
7759         lockdep_assert_held(&ost->st_stid.sc_    7317         lockdep_assert_held(&ost->st_stid.sc_client->cl_lock);
7760                                                  7318 
7761         /* If ost is not hashed, ost->st_lock    7319         /* If ost is not hashed, ost->st_locks will not be valid */
7762         if (!nfs4_ol_stateid_unhashed(ost))      7320         if (!nfs4_ol_stateid_unhashed(ost))
7763                 list_for_each_entry(lst, &ost    7321                 list_for_each_entry(lst, &ost->st_locks, st_locks) {
7764                         if (lst->st_stateowne    7322                         if (lst->st_stateowner == &lo->lo_owner) {
7765                                 refcount_inc(    7323                                 refcount_inc(&lst->st_stid.sc_count);
7766                                 return lst;      7324                                 return lst;
7767                         }                        7325                         }
7768                 }                                7326                 }
7769         return NULL;                             7327         return NULL;
7770 }                                                7328 }
7771                                                  7329 
7772 static struct nfs4_ol_stateid *                  7330 static struct nfs4_ol_stateid *
7773 init_lock_stateid(struct nfs4_ol_stateid *stp    7331 init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
7774                   struct nfs4_file *fp, struc    7332                   struct nfs4_file *fp, struct inode *inode,
7775                   struct nfs4_ol_stateid *ope    7333                   struct nfs4_ol_stateid *open_stp)
7776 {                                                7334 {
7777         struct nfs4_client *clp = lo->lo_owne    7335         struct nfs4_client *clp = lo->lo_owner.so_client;
7778         struct nfs4_ol_stateid *retstp;          7336         struct nfs4_ol_stateid *retstp;
7779                                                  7337 
7780         mutex_init(&stp->st_mutex);              7338         mutex_init(&stp->st_mutex);
7781         mutex_lock_nested(&stp->st_mutex, OPE    7339         mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
7782 retry:                                           7340 retry:
7783         spin_lock(&clp->cl_lock);                7341         spin_lock(&clp->cl_lock);
7784         if (nfs4_ol_stateid_unhashed(open_stp    7342         if (nfs4_ol_stateid_unhashed(open_stp))
7785                 goto out_close;                  7343                 goto out_close;
7786         retstp = find_lock_stateid(lo, open_s    7344         retstp = find_lock_stateid(lo, open_stp);
7787         if (retstp)                              7345         if (retstp)
7788                 goto out_found;                  7346                 goto out_found;
7789         refcount_inc(&stp->st_stid.sc_count);    7347         refcount_inc(&stp->st_stid.sc_count);
7790         stp->st_stid.sc_type = SC_TYPE_LOCK;  !! 7348         stp->st_stid.sc_type = NFS4_LOCK_STID;
7791         stp->st_stateowner = nfs4_get_stateow    7349         stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
7792         get_nfs4_file(fp);                       7350         get_nfs4_file(fp);
7793         stp->st_stid.sc_file = fp;               7351         stp->st_stid.sc_file = fp;
7794         stp->st_access_bmap = 0;                 7352         stp->st_access_bmap = 0;
7795         stp->st_deny_bmap = open_stp->st_deny    7353         stp->st_deny_bmap = open_stp->st_deny_bmap;
7796         stp->st_openstp = open_stp;              7354         stp->st_openstp = open_stp;
7797         spin_lock(&fp->fi_lock);                 7355         spin_lock(&fp->fi_lock);
7798         list_add(&stp->st_locks, &open_stp->s    7356         list_add(&stp->st_locks, &open_stp->st_locks);
7799         list_add(&stp->st_perstateowner, &lo-    7357         list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
7800         list_add(&stp->st_perfile, &fp->fi_st    7358         list_add(&stp->st_perfile, &fp->fi_stateids);
7801         spin_unlock(&fp->fi_lock);               7359         spin_unlock(&fp->fi_lock);
7802         spin_unlock(&clp->cl_lock);              7360         spin_unlock(&clp->cl_lock);
7803         return stp;                              7361         return stp;
7804 out_found:                                       7362 out_found:
7805         spin_unlock(&clp->cl_lock);              7363         spin_unlock(&clp->cl_lock);
7806         if (nfsd4_lock_ol_stateid(retstp) !=     7364         if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
7807                 nfs4_put_stid(&retstp->st_sti    7365                 nfs4_put_stid(&retstp->st_stid);
7808                 goto retry;                      7366                 goto retry;
7809         }                                        7367         }
7810         /* To keep mutex tracking happy */       7368         /* To keep mutex tracking happy */
7811         mutex_unlock(&stp->st_mutex);            7369         mutex_unlock(&stp->st_mutex);
7812         return retstp;                           7370         return retstp;
7813 out_close:                                       7371 out_close:
7814         spin_unlock(&clp->cl_lock);              7372         spin_unlock(&clp->cl_lock);
7815         mutex_unlock(&stp->st_mutex);            7373         mutex_unlock(&stp->st_mutex);
7816         return NULL;                             7374         return NULL;
7817 }                                                7375 }
7818                                                  7376 
7819 static struct nfs4_ol_stateid *                  7377 static struct nfs4_ol_stateid *
7820 find_or_create_lock_stateid(struct nfs4_locko    7378 find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
7821                             struct inode *ino    7379                             struct inode *inode, struct nfs4_ol_stateid *ost,
7822                             bool *new)           7380                             bool *new)
7823 {                                                7381 {
7824         struct nfs4_stid *ns = NULL;             7382         struct nfs4_stid *ns = NULL;
7825         struct nfs4_ol_stateid *lst;             7383         struct nfs4_ol_stateid *lst;
7826         struct nfs4_openowner *oo = openowner    7384         struct nfs4_openowner *oo = openowner(ost->st_stateowner);
7827         struct nfs4_client *clp = oo->oo_owne    7385         struct nfs4_client *clp = oo->oo_owner.so_client;
7828                                                  7386 
7829         *new = false;                            7387         *new = false;
7830         spin_lock(&clp->cl_lock);                7388         spin_lock(&clp->cl_lock);
7831         lst = find_lock_stateid(lo, ost);        7389         lst = find_lock_stateid(lo, ost);
7832         spin_unlock(&clp->cl_lock);              7390         spin_unlock(&clp->cl_lock);
7833         if (lst != NULL) {                       7391         if (lst != NULL) {
7834                 if (nfsd4_lock_ol_stateid(lst    7392                 if (nfsd4_lock_ol_stateid(lst) == nfs_ok)
7835                         goto out;                7393                         goto out;
7836                 nfs4_put_stid(&lst->st_stid);    7394                 nfs4_put_stid(&lst->st_stid);
7837         }                                        7395         }
7838         ns = nfs4_alloc_stid(clp, stateid_sla    7396         ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid);
7839         if (ns == NULL)                          7397         if (ns == NULL)
7840                 return NULL;                     7398                 return NULL;
7841                                                  7399 
7842         lst = init_lock_stateid(openlockstate    7400         lst = init_lock_stateid(openlockstateid(ns), lo, fi, inode, ost);
7843         if (lst == openlockstateid(ns))          7401         if (lst == openlockstateid(ns))
7844                 *new = true;                     7402                 *new = true;
7845         else                                     7403         else
7846                 nfs4_put_stid(ns);               7404                 nfs4_put_stid(ns);
7847 out:                                             7405 out:
7848         return lst;                              7406         return lst;
7849 }                                                7407 }
7850                                                  7408 
7851 static int                                       7409 static int
7852 check_lock_length(u64 offset, u64 length)        7410 check_lock_length(u64 offset, u64 length)
7853 {                                                7411 {
7854         return ((length == 0) || ((length !=     7412         return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
7855                 (length > ~offset)));            7413                 (length > ~offset)));
7856 }                                                7414 }
7857                                                  7415 
7858 static void get_lock_access(struct nfs4_ol_st    7416 static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
7859 {                                                7417 {
7860         struct nfs4_file *fp = lock_stp->st_s    7418         struct nfs4_file *fp = lock_stp->st_stid.sc_file;
7861                                                  7419 
7862         lockdep_assert_held(&fp->fi_lock);       7420         lockdep_assert_held(&fp->fi_lock);
7863                                                  7421 
7864         if (test_access(access, lock_stp))       7422         if (test_access(access, lock_stp))
7865                 return;                          7423                 return;
7866         __nfs4_file_get_access(fp, access);      7424         __nfs4_file_get_access(fp, access);
7867         set_access(access, lock_stp);            7425         set_access(access, lock_stp);
7868 }                                                7426 }
7869                                                  7427 
7870 static __be32                                    7428 static __be32
7871 lookup_or_create_lock_state(struct nfsd4_comp    7429 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
7872                             struct nfs4_ol_st    7430                             struct nfs4_ol_stateid *ost,
7873                             struct nfsd4_lock    7431                             struct nfsd4_lock *lock,
7874                             struct nfs4_ol_st    7432                             struct nfs4_ol_stateid **plst, bool *new)
7875 {                                                7433 {
7876         __be32 status;                           7434         __be32 status;
7877         struct nfs4_file *fi = ost->st_stid.s    7435         struct nfs4_file *fi = ost->st_stid.sc_file;
7878         struct nfs4_openowner *oo = openowner    7436         struct nfs4_openowner *oo = openowner(ost->st_stateowner);
7879         struct nfs4_client *cl = oo->oo_owner    7437         struct nfs4_client *cl = oo->oo_owner.so_client;
7880         struct inode *inode = d_inode(cstate-    7438         struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
7881         struct nfs4_lockowner *lo;               7439         struct nfs4_lockowner *lo;
7882         struct nfs4_ol_stateid *lst;             7440         struct nfs4_ol_stateid *lst;
7883         unsigned int strhashval;                 7441         unsigned int strhashval;
7884                                                  7442 
7885         lo = find_lockowner_str(cl, &lock->lk    7443         lo = find_lockowner_str(cl, &lock->lk_new_owner);
7886         if (!lo) {                               7444         if (!lo) {
7887                 strhashval = ownerstr_hashval    7445                 strhashval = ownerstr_hashval(&lock->lk_new_owner);
7888                 lo = alloc_init_lock_stateown    7446                 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
7889                 if (lo == NULL)                  7447                 if (lo == NULL)
7890                         return nfserr_jukebox    7448                         return nfserr_jukebox;
7891         } else {                                 7449         } else {
7892                 /* with an existing lockowner    7450                 /* with an existing lockowner, seqids must be the same */
7893                 status = nfserr_bad_seqid;       7451                 status = nfserr_bad_seqid;
7894                 if (!cstate->minorversion &&     7452                 if (!cstate->minorversion &&
7895                     lock->lk_new_lock_seqid !    7453                     lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
7896                         goto out;                7454                         goto out;
7897         }                                        7455         }
7898                                                  7456 
7899         lst = find_or_create_lock_stateid(lo,    7457         lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
7900         if (lst == NULL) {                       7458         if (lst == NULL) {
7901                 status = nfserr_jukebox;         7459                 status = nfserr_jukebox;
7902                 goto out;                        7460                 goto out;
7903         }                                        7461         }
7904                                                  7462 
7905         status = nfs_ok;                         7463         status = nfs_ok;
7906         *plst = lst;                             7464         *plst = lst;
7907 out:                                             7465 out:
7908         nfs4_put_stateowner(&lo->lo_owner);      7466         nfs4_put_stateowner(&lo->lo_owner);
7909         return status;                           7467         return status;
7910 }                                                7468 }
7911                                                  7469 
7912 /*                                               7470 /*
7913  *  LOCK operation                               7471  *  LOCK operation 
7914  */                                              7472  */
7915 __be32                                           7473 __be32
7916 nfsd4_lock(struct svc_rqst *rqstp, struct nfs    7474 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7917            union nfsd4_op_u *u)                  7475            union nfsd4_op_u *u)
7918 {                                                7476 {
7919         struct nfsd4_lock *lock = &u->lock;      7477         struct nfsd4_lock *lock = &u->lock;
7920         struct nfs4_openowner *open_sop = NUL    7478         struct nfs4_openowner *open_sop = NULL;
7921         struct nfs4_lockowner *lock_sop = NUL    7479         struct nfs4_lockowner *lock_sop = NULL;
7922         struct nfs4_ol_stateid *lock_stp = NU    7480         struct nfs4_ol_stateid *lock_stp = NULL;
7923         struct nfs4_ol_stateid *open_stp = NU    7481         struct nfs4_ol_stateid *open_stp = NULL;
7924         struct nfs4_file *fp;                    7482         struct nfs4_file *fp;
7925         struct nfsd_file *nf = NULL;             7483         struct nfsd_file *nf = NULL;
7926         struct nfsd4_blocked_lock *nbl = NULL    7484         struct nfsd4_blocked_lock *nbl = NULL;
7927         struct file_lock *file_lock = NULL;      7485         struct file_lock *file_lock = NULL;
7928         struct file_lock *conflock = NULL;       7486         struct file_lock *conflock = NULL;
7929         struct super_block *sb;                  7487         struct super_block *sb;
7930         __be32 status = 0;                       7488         __be32 status = 0;
7931         int lkflg;                               7489         int lkflg;
7932         int err;                                 7490         int err;
7933         bool new = false;                        7491         bool new = false;
7934         unsigned char type;                   !! 7492         unsigned char fl_type;
7935         unsigned int flags = FL_POSIX;        !! 7493         unsigned int fl_flags = FL_POSIX;
7936         struct net *net = SVC_NET(rqstp);        7494         struct net *net = SVC_NET(rqstp);
7937         struct nfsd_net *nn = net_generic(net    7495         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7938                                                  7496 
7939         dprintk("NFSD: nfsd4_lock: start=%Ld     7497         dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
7940                 (long long) lock->lk_offset,     7498                 (long long) lock->lk_offset,
7941                 (long long) lock->lk_length);    7499                 (long long) lock->lk_length);
7942                                                  7500 
7943         if (check_lock_length(lock->lk_offset    7501         if (check_lock_length(lock->lk_offset, lock->lk_length))
7944                  return nfserr_inval;            7502                  return nfserr_inval;
7945                                                  7503 
7946         if ((status = fh_verify(rqstp, &cstat    7504         if ((status = fh_verify(rqstp, &cstate->current_fh,
7947                                 S_IFREG, NFSD    7505                                 S_IFREG, NFSD_MAY_LOCK))) {
7948                 dprintk("NFSD: nfsd4_lock: pe    7506                 dprintk("NFSD: nfsd4_lock: permission denied!\n");
7949                 return status;                   7507                 return status;
7950         }                                        7508         }
7951         sb = cstate->current_fh.fh_dentry->d_    7509         sb = cstate->current_fh.fh_dentry->d_sb;
7952                                                  7510 
7953         if (lock->lk_is_new) {                   7511         if (lock->lk_is_new) {
7954                 if (nfsd4_has_session(cstate)    7512                 if (nfsd4_has_session(cstate))
7955                         /* See rfc 5661 18.10    7513                         /* See rfc 5661 18.10.3: given clientid is ignored: */
7956                         memcpy(&lock->lk_new_    7514                         memcpy(&lock->lk_new_clientid,
7957                                 &cstate->clp-    7515                                 &cstate->clp->cl_clientid,
7958                                 sizeof(client    7516                                 sizeof(clientid_t));
7959                                                  7517 
7960                 /* validate and update open s    7518                 /* validate and update open stateid and open seqid */
7961                 status = nfs4_preprocess_conf    7519                 status = nfs4_preprocess_confirmed_seqid_op(cstate,
7962                                         lock-    7520                                         lock->lk_new_open_seqid,
7963                                         &lock    7521                                         &lock->lk_new_open_stateid,
7964                                         &open    7522                                         &open_stp, nn);
7965                 if (status)                      7523                 if (status)
7966                         goto out;                7524                         goto out;
7967                 mutex_unlock(&open_stp->st_mu    7525                 mutex_unlock(&open_stp->st_mutex);
7968                 open_sop = openowner(open_stp    7526                 open_sop = openowner(open_stp->st_stateowner);
7969                 status = nfserr_bad_stateid;     7527                 status = nfserr_bad_stateid;
7970                 if (!same_clid(&open_sop->oo_    7528                 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
7971                                                  7529                                                 &lock->lk_new_clientid))
7972                         goto out;                7530                         goto out;
7973                 status = lookup_or_create_loc    7531                 status = lookup_or_create_lock_state(cstate, open_stp, lock,
7974                                                  7532                                                         &lock_stp, &new);
7975         } else {                                 7533         } else {
7976                 status = nfs4_preprocess_seqi    7534                 status = nfs4_preprocess_seqid_op(cstate,
7977                                               !! 7535                                        lock->lk_old_lock_seqid,
7978                                               !! 7536                                        &lock->lk_old_lock_stateid,
7979                                               !! 7537                                        NFS4_LOCK_STID, &lock_stp, nn);
7980                                               << 
7981         }                                        7538         }
7982         if (status)                              7539         if (status)
7983                 goto out;                        7540                 goto out;
7984         lock_sop = lockowner(lock_stp->st_sta    7541         lock_sop = lockowner(lock_stp->st_stateowner);
7985                                                  7542 
7986         lkflg = setlkflg(lock->lk_type);         7543         lkflg = setlkflg(lock->lk_type);
7987         status = nfs4_check_openmode(lock_stp    7544         status = nfs4_check_openmode(lock_stp, lkflg);
7988         if (status)                              7545         if (status)
7989                 goto out;                        7546                 goto out;
7990                                                  7547 
7991         status = nfserr_grace;                   7548         status = nfserr_grace;
7992         if (locks_in_grace(net) && !lock->lk_    7549         if (locks_in_grace(net) && !lock->lk_reclaim)
7993                 goto out;                        7550                 goto out;
7994         status = nfserr_no_grace;                7551         status = nfserr_no_grace;
7995         if (!locks_in_grace(net) && lock->lk_    7552         if (!locks_in_grace(net) && lock->lk_reclaim)
7996                 goto out;                        7553                 goto out;
7997                                                  7554 
7998         if (lock->lk_reclaim)                    7555         if (lock->lk_reclaim)
7999                 flags |= FL_RECLAIM;          !! 7556                 fl_flags |= FL_RECLAIM;
8000                                                  7557 
8001         fp = lock_stp->st_stid.sc_file;          7558         fp = lock_stp->st_stid.sc_file;
8002         switch (lock->lk_type) {                 7559         switch (lock->lk_type) {
8003                 case NFS4_READW_LT:              7560                 case NFS4_READW_LT:
8004                         if (nfsd4_has_session    7561                         if (nfsd4_has_session(cstate) ||
8005                             exportfs_lock_op_    7562                             exportfs_lock_op_is_async(sb->s_export_op))
8006                                 flags |= FL_S !! 7563                                 fl_flags |= FL_SLEEP;
8007                         fallthrough;             7564                         fallthrough;
8008                 case NFS4_READ_LT:               7565                 case NFS4_READ_LT:
8009                         spin_lock(&fp->fi_loc    7566                         spin_lock(&fp->fi_lock);
8010                         nf = find_readable_fi    7567                         nf = find_readable_file_locked(fp);
8011                         if (nf)                  7568                         if (nf)
8012                                 get_lock_acce    7569                                 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
8013                         spin_unlock(&fp->fi_l    7570                         spin_unlock(&fp->fi_lock);
8014                         type = F_RDLCK;       !! 7571                         fl_type = F_RDLCK;
8015                         break;                   7572                         break;
8016                 case NFS4_WRITEW_LT:             7573                 case NFS4_WRITEW_LT:
8017                         if (nfsd4_has_session    7574                         if (nfsd4_has_session(cstate) ||
8018                             exportfs_lock_op_    7575                             exportfs_lock_op_is_async(sb->s_export_op))
8019                                 flags |= FL_S !! 7576                                 fl_flags |= FL_SLEEP;
8020                         fallthrough;             7577                         fallthrough;
8021                 case NFS4_WRITE_LT:              7578                 case NFS4_WRITE_LT:
8022                         spin_lock(&fp->fi_loc    7579                         spin_lock(&fp->fi_lock);
8023                         nf = find_writeable_f    7580                         nf = find_writeable_file_locked(fp);
8024                         if (nf)                  7581                         if (nf)
8025                                 get_lock_acce    7582                                 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
8026                         spin_unlock(&fp->fi_l    7583                         spin_unlock(&fp->fi_lock);
8027                         type = F_WRLCK;       !! 7584                         fl_type = F_WRLCK;
8028                         break;                   7585                         break;
8029                 default:                         7586                 default:
8030                         status = nfserr_inval    7587                         status = nfserr_inval;
8031                 goto out;                        7588                 goto out;
8032         }                                        7589         }
8033                                                  7590 
8034         if (!nf) {                               7591         if (!nf) {
8035                 status = nfserr_openmode;        7592                 status = nfserr_openmode;
8036                 goto out;                        7593                 goto out;
8037         }                                        7594         }
8038                                                  7595 
8039         /*                                       7596         /*
8040          * Most filesystems with their own ->    7597          * Most filesystems with their own ->lock operations will block
8041          * the nfsd thread waiting to acquire    7598          * the nfsd thread waiting to acquire the lock.  That leads to
8042          * deadlocks (we don't want every nfs    7599          * deadlocks (we don't want every nfsd thread tied up waiting
8043          * for file locks), so don't attempt     7600          * for file locks), so don't attempt blocking lock notifications
8044          * on those filesystems:                 7601          * on those filesystems:
8045          */                                      7602          */
8046         if (!exportfs_lock_op_is_async(sb->s_    7603         if (!exportfs_lock_op_is_async(sb->s_export_op))
8047                 flags &= ~FL_SLEEP;           !! 7604                 fl_flags &= ~FL_SLEEP;
8048                                                  7605 
8049         nbl = find_or_allocate_block(lock_sop    7606         nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
8050         if (!nbl) {                              7607         if (!nbl) {
8051                 dprintk("NFSD: %s: unable to     7608                 dprintk("NFSD: %s: unable to allocate block!\n", __func__);
8052                 status = nfserr_jukebox;         7609                 status = nfserr_jukebox;
8053                 goto out;                        7610                 goto out;
8054         }                                        7611         }
8055                                                  7612 
8056         file_lock = &nbl->nbl_lock;              7613         file_lock = &nbl->nbl_lock;
8057         file_lock->c.flc_type = type;         !! 7614         file_lock->fl_type = fl_type;
8058         file_lock->c.flc_owner = (fl_owner_t) !! 7615         file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
8059         file_lock->c.flc_pid = current->tgid; !! 7616         file_lock->fl_pid = current->tgid;
8060         file_lock->c.flc_file = nf->nf_file;  !! 7617         file_lock->fl_file = nf->nf_file;
8061         file_lock->c.flc_flags = flags;       !! 7618         file_lock->fl_flags = fl_flags;
8062         file_lock->fl_lmops = &nfsd_posix_mng    7619         file_lock->fl_lmops = &nfsd_posix_mng_ops;
8063         file_lock->fl_start = lock->lk_offset    7620         file_lock->fl_start = lock->lk_offset;
8064         file_lock->fl_end = last_byte_offset(    7621         file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
8065         nfs4_transform_lock_offset(file_lock)    7622         nfs4_transform_lock_offset(file_lock);
8066                                                  7623 
8067         conflock = locks_alloc_lock();           7624         conflock = locks_alloc_lock();
8068         if (!conflock) {                         7625         if (!conflock) {
8069                 dprintk("NFSD: %s: unable to     7626                 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
8070                 status = nfserr_jukebox;         7627                 status = nfserr_jukebox;
8071                 goto out;                        7628                 goto out;
8072         }                                        7629         }
8073                                                  7630 
8074         if (flags & FL_SLEEP) {               !! 7631         if (fl_flags & FL_SLEEP) {
8075                 nbl->nbl_time = ktime_get_boo    7632                 nbl->nbl_time = ktime_get_boottime_seconds();
8076                 spin_lock(&nn->blocked_locks_    7633                 spin_lock(&nn->blocked_locks_lock);
8077                 list_add_tail(&nbl->nbl_list,    7634                 list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
8078                 list_add_tail(&nbl->nbl_lru,     7635                 list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
8079                 kref_get(&nbl->nbl_kref);        7636                 kref_get(&nbl->nbl_kref);
8080                 spin_unlock(&nn->blocked_lock    7637                 spin_unlock(&nn->blocked_locks_lock);
8081         }                                        7638         }
8082                                                  7639 
8083         err = vfs_lock_file(nf->nf_file, F_SE    7640         err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, conflock);
8084         switch (err) {                           7641         switch (err) {
8085         case 0: /* success! */                   7642         case 0: /* success! */
8086                 nfs4_inc_and_copy_stateid(&lo    7643                 nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
8087                 status = 0;                      7644                 status = 0;
8088                 if (lock->lk_reclaim)            7645                 if (lock->lk_reclaim)
8089                         nn->somebody_reclaime    7646                         nn->somebody_reclaimed = true;
8090                 break;                           7647                 break;
8091         case FILE_LOCK_DEFERRED:                 7648         case FILE_LOCK_DEFERRED:
8092                 kref_put(&nbl->nbl_kref, free    7649                 kref_put(&nbl->nbl_kref, free_nbl);
8093                 nbl = NULL;                      7650                 nbl = NULL;
8094                 fallthrough;                     7651                 fallthrough;
8095         case -EAGAIN:           /* conflock h    7652         case -EAGAIN:           /* conflock holds conflicting lock */
8096                 status = nfserr_denied;          7653                 status = nfserr_denied;
8097                 dprintk("NFSD: nfsd4_lock: co    7654                 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
8098                 nfs4_set_lock_denied(conflock    7655                 nfs4_set_lock_denied(conflock, &lock->lk_denied);
8099                 break;                           7656                 break;
8100         case -EDEADLK:                           7657         case -EDEADLK:
8101                 status = nfserr_deadlock;        7658                 status = nfserr_deadlock;
8102                 break;                           7659                 break;
8103         default:                                 7660         default:
8104                 dprintk("NFSD: nfsd4_lock: vf    7661                 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
8105                 status = nfserrno(err);          7662                 status = nfserrno(err);
8106                 break;                           7663                 break;
8107         }                                        7664         }
8108 out:                                             7665 out:
8109         if (nbl) {                               7666         if (nbl) {
8110                 /* dequeue it if we queued it    7667                 /* dequeue it if we queued it before */
8111                 if (flags & FL_SLEEP) {       !! 7668                 if (fl_flags & FL_SLEEP) {
8112                         spin_lock(&nn->blocke    7669                         spin_lock(&nn->blocked_locks_lock);
8113                         if (!list_empty(&nbl-    7670                         if (!list_empty(&nbl->nbl_list) &&
8114                             !list_empty(&nbl-    7671                             !list_empty(&nbl->nbl_lru)) {
8115                                 list_del_init    7672                                 list_del_init(&nbl->nbl_list);
8116                                 list_del_init    7673                                 list_del_init(&nbl->nbl_lru);
8117                                 kref_put(&nbl    7674                                 kref_put(&nbl->nbl_kref, free_nbl);
8118                         }                        7675                         }
8119                         /* nbl can use one of    7676                         /* nbl can use one of lists to be linked to reaplist */
8120                         spin_unlock(&nn->bloc    7677                         spin_unlock(&nn->blocked_locks_lock);
8121                 }                                7678                 }
8122                 free_blocked_lock(nbl);          7679                 free_blocked_lock(nbl);
8123         }                                        7680         }
8124         if (nf)                                  7681         if (nf)
8125                 nfsd_file_put(nf);               7682                 nfsd_file_put(nf);
8126         if (lock_stp) {                          7683         if (lock_stp) {
8127                 /* Bump seqid manually if the    7684                 /* Bump seqid manually if the 4.0 replay owner is openowner */
8128                 if (cstate->replay_owner &&      7685                 if (cstate->replay_owner &&
8129                     cstate->replay_owner != &    7686                     cstate->replay_owner != &lock_sop->lo_owner &&
8130                     seqid_mutating_err(ntohl(    7687                     seqid_mutating_err(ntohl(status)))
8131                         lock_sop->lo_owner.so    7688                         lock_sop->lo_owner.so_seqid++;
8132                                                  7689 
8133                 /*                               7690                 /*
8134                  * If this is a new, never-be    7691                  * If this is a new, never-before-used stateid, and we are
8135                  * returning an error, then j    7692                  * returning an error, then just go ahead and release it.
8136                  */                              7693                  */
8137                 if (status && new)               7694                 if (status && new)
8138                         release_lock_stateid(    7695                         release_lock_stateid(lock_stp);
8139                                                  7696 
8140                 mutex_unlock(&lock_stp->st_mu    7697                 mutex_unlock(&lock_stp->st_mutex);
8141                                                  7698 
8142                 nfs4_put_stid(&lock_stp->st_s    7699                 nfs4_put_stid(&lock_stp->st_stid);
8143         }                                        7700         }
8144         if (open_stp)                            7701         if (open_stp)
8145                 nfs4_put_stid(&open_stp->st_s    7702                 nfs4_put_stid(&open_stp->st_stid);
8146         nfsd4_bump_seqid(cstate, status);        7703         nfsd4_bump_seqid(cstate, status);
8147         if (conflock)                            7704         if (conflock)
8148                 locks_free_lock(conflock);       7705                 locks_free_lock(conflock);
8149         return status;                           7706         return status;
8150 }                                                7707 }
8151                                                  7708 
8152 void nfsd4_lock_release(union nfsd4_op_u *u)     7709 void nfsd4_lock_release(union nfsd4_op_u *u)
8153 {                                                7710 {
8154         struct nfsd4_lock *lock = &u->lock;      7711         struct nfsd4_lock *lock = &u->lock;
8155         struct nfsd4_lock_denied *deny = &loc    7712         struct nfsd4_lock_denied *deny = &lock->lk_denied;
8156                                                  7713 
8157         kfree(deny->ld_owner.data);              7714         kfree(deny->ld_owner.data);
8158 }                                                7715 }
8159                                                  7716 
8160 /*                                               7717 /*
8161  * The NFSv4 spec allows a client to do a LOC    7718  * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
8162  * so we do a temporary open here just to get    7719  * so we do a temporary open here just to get an open file to pass to
8163  * vfs_test_lock.                                7720  * vfs_test_lock.
8164  */                                              7721  */
8165 static __be32 nfsd_test_lock(struct svc_rqst     7722 static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
8166 {                                                7723 {
8167         struct nfsd_file *nf;                    7724         struct nfsd_file *nf;
8168         struct inode *inode;                     7725         struct inode *inode;
8169         __be32 err;                              7726         __be32 err;
8170                                                  7727 
8171         err = nfsd_file_acquire(rqstp, fhp, N    7728         err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf);
8172         if (err)                                 7729         if (err)
8173                 return err;                      7730                 return err;
8174         inode = fhp->fh_dentry->d_inode;         7731         inode = fhp->fh_dentry->d_inode;
8175         inode_lock(inode); /* to block new le    7732         inode_lock(inode); /* to block new leases till after test_lock: */
8176         err = nfserrno(nfsd_open_break_lease(    7733         err = nfserrno(nfsd_open_break_lease(inode, NFSD_MAY_READ));
8177         if (err)                                 7734         if (err)
8178                 goto out;                        7735                 goto out;
8179         lock->c.flc_file = nf->nf_file;       !! 7736         lock->fl_file = nf->nf_file;
8180         err = nfserrno(vfs_test_lock(nf->nf_f    7737         err = nfserrno(vfs_test_lock(nf->nf_file, lock));
8181         lock->c.flc_file = NULL;              !! 7738         lock->fl_file = NULL;
8182 out:                                             7739 out:
8183         inode_unlock(inode);                     7740         inode_unlock(inode);
8184         nfsd_file_put(nf);                       7741         nfsd_file_put(nf);
8185         return err;                              7742         return err;
8186 }                                                7743 }
8187                                                  7744 
8188 /*                                               7745 /*
8189  * LOCKT operation                               7746  * LOCKT operation
8190  */                                              7747  */
8191 __be32                                           7748 __be32
8192 nfsd4_lockt(struct svc_rqst *rqstp, struct nf    7749 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
8193             union nfsd4_op_u *u)                 7750             union nfsd4_op_u *u)
8194 {                                                7751 {
8195         struct nfsd4_lockt *lockt = &u->lockt    7752         struct nfsd4_lockt *lockt = &u->lockt;
8196         struct file_lock *file_lock = NULL;      7753         struct file_lock *file_lock = NULL;
8197         struct nfs4_lockowner *lo = NULL;        7754         struct nfs4_lockowner *lo = NULL;
8198         __be32 status;                           7755         __be32 status;
8199         struct nfsd_net *nn = net_generic(SVC    7756         struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
8200                                                  7757 
8201         if (locks_in_grace(SVC_NET(rqstp)))      7758         if (locks_in_grace(SVC_NET(rqstp)))
8202                 return nfserr_grace;             7759                 return nfserr_grace;
8203                                                  7760 
8204         if (check_lock_length(lockt->lt_offse    7761         if (check_lock_length(lockt->lt_offset, lockt->lt_length))
8205                  return nfserr_inval;            7762                  return nfserr_inval;
8206                                                  7763 
8207         if (!nfsd4_has_session(cstate)) {        7764         if (!nfsd4_has_session(cstate)) {
8208                 status = set_client(&lockt->l    7765                 status = set_client(&lockt->lt_clientid, cstate, nn);
8209                 if (status)                      7766                 if (status)
8210                         goto out;                7767                         goto out;
8211         }                                        7768         }
8212                                                  7769 
8213         if ((status = fh_verify(rqstp, &cstat    7770         if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
8214                 goto out;                        7771                 goto out;
8215                                                  7772 
8216         file_lock = locks_alloc_lock();          7773         file_lock = locks_alloc_lock();
8217         if (!file_lock) {                        7774         if (!file_lock) {
8218                 dprintk("NFSD: %s: unable to     7775                 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
8219                 status = nfserr_jukebox;         7776                 status = nfserr_jukebox;
8220                 goto out;                        7777                 goto out;
8221         }                                        7778         }
8222                                                  7779 
8223         switch (lockt->lt_type) {                7780         switch (lockt->lt_type) {
8224                 case NFS4_READ_LT:               7781                 case NFS4_READ_LT:
8225                 case NFS4_READW_LT:              7782                 case NFS4_READW_LT:
8226                         file_lock->c.flc_type !! 7783                         file_lock->fl_type = F_RDLCK;
8227                         break;                   7784                         break;
8228                 case NFS4_WRITE_LT:              7785                 case NFS4_WRITE_LT:
8229                 case NFS4_WRITEW_LT:             7786                 case NFS4_WRITEW_LT:
8230                         file_lock->c.flc_type !! 7787                         file_lock->fl_type = F_WRLCK;
8231                         break;                   7788                         break;
8232                 default:                         7789                 default:
8233                         dprintk("NFSD: nfs4_l    7790                         dprintk("NFSD: nfs4_lockt: bad lock type!\n");
8234                         status = nfserr_inval    7791                         status = nfserr_inval;
8235                         goto out;                7792                         goto out;
8236         }                                        7793         }
8237                                                  7794 
8238         lo = find_lockowner_str(cstate->clp,     7795         lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
8239         if (lo)                                  7796         if (lo)
8240                 file_lock->c.flc_owner = (fl_ !! 7797                 file_lock->fl_owner = (fl_owner_t)lo;
8241         file_lock->c.flc_pid = current->tgid; !! 7798         file_lock->fl_pid = current->tgid;
8242         file_lock->c.flc_flags = FL_POSIX;    !! 7799         file_lock->fl_flags = FL_POSIX;
8243                                                  7800 
8244         file_lock->fl_start = lockt->lt_offse    7801         file_lock->fl_start = lockt->lt_offset;
8245         file_lock->fl_end = last_byte_offset(    7802         file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
8246                                                  7803 
8247         nfs4_transform_lock_offset(file_lock)    7804         nfs4_transform_lock_offset(file_lock);
8248                                                  7805 
8249         status = nfsd_test_lock(rqstp, &cstat    7806         status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
8250         if (status)                              7807         if (status)
8251                 goto out;                        7808                 goto out;
8252                                                  7809 
8253         if (file_lock->c.flc_type != F_UNLCK) !! 7810         if (file_lock->fl_type != F_UNLCK) {
8254                 status = nfserr_denied;          7811                 status = nfserr_denied;
8255                 nfs4_set_lock_denied(file_loc    7812                 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
8256         }                                        7813         }
8257 out:                                             7814 out:
8258         if (lo)                                  7815         if (lo)
8259                 nfs4_put_stateowner(&lo->lo_o    7816                 nfs4_put_stateowner(&lo->lo_owner);
8260         if (file_lock)                           7817         if (file_lock)
8261                 locks_free_lock(file_lock);      7818                 locks_free_lock(file_lock);
8262         return status;                           7819         return status;
8263 }                                                7820 }
8264                                                  7821 
8265 void nfsd4_lockt_release(union nfsd4_op_u *u)    7822 void nfsd4_lockt_release(union nfsd4_op_u *u)
8266 {                                                7823 {
8267         struct nfsd4_lockt *lockt = &u->lockt    7824         struct nfsd4_lockt *lockt = &u->lockt;
8268         struct nfsd4_lock_denied *deny = &loc    7825         struct nfsd4_lock_denied *deny = &lockt->lt_denied;
8269                                                  7826 
8270         kfree(deny->ld_owner.data);              7827         kfree(deny->ld_owner.data);
8271 }                                                7828 }
8272                                                  7829 
8273 __be32                                           7830 __be32
8274 nfsd4_locku(struct svc_rqst *rqstp, struct nf    7831 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
8275             union nfsd4_op_u *u)                 7832             union nfsd4_op_u *u)
8276 {                                                7833 {
8277         struct nfsd4_locku *locku = &u->locku    7834         struct nfsd4_locku *locku = &u->locku;
8278         struct nfs4_ol_stateid *stp;             7835         struct nfs4_ol_stateid *stp;
8279         struct nfsd_file *nf = NULL;             7836         struct nfsd_file *nf = NULL;
8280         struct file_lock *file_lock = NULL;      7837         struct file_lock *file_lock = NULL;
8281         __be32 status;                           7838         __be32 status;
8282         int err;                                 7839         int err;
8283         struct nfsd_net *nn = net_generic(SVC    7840         struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
8284                                                  7841 
8285         dprintk("NFSD: nfsd4_locku: start=%Ld    7842         dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
8286                 (long long) locku->lu_offset,    7843                 (long long) locku->lu_offset,
8287                 (long long) locku->lu_length)    7844                 (long long) locku->lu_length);
8288                                                  7845 
8289         if (check_lock_length(locku->lu_offse    7846         if (check_lock_length(locku->lu_offset, locku->lu_length))
8290                  return nfserr_inval;            7847                  return nfserr_inval;
8291                                                  7848 
8292         status = nfs4_preprocess_seqid_op(cst    7849         status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
8293                                           &lo !! 7850                                         &locku->lu_stateid, NFS4_LOCK_STID,
8294                                           &st !! 7851                                         &stp, nn);
8295         if (status)                              7852         if (status)
8296                 goto out;                        7853                 goto out;
8297         nf = find_any_file(stp->st_stid.sc_fi    7854         nf = find_any_file(stp->st_stid.sc_file);
8298         if (!nf) {                               7855         if (!nf) {
8299                 status = nfserr_lock_range;      7856                 status = nfserr_lock_range;
8300                 goto put_stateid;                7857                 goto put_stateid;
8301         }                                        7858         }
8302         file_lock = locks_alloc_lock();          7859         file_lock = locks_alloc_lock();
8303         if (!file_lock) {                        7860         if (!file_lock) {
8304                 dprintk("NFSD: %s: unable to     7861                 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
8305                 status = nfserr_jukebox;         7862                 status = nfserr_jukebox;
8306                 goto put_file;                   7863                 goto put_file;
8307         }                                        7864         }
8308                                                  7865 
8309         file_lock->c.flc_type = F_UNLCK;      !! 7866         file_lock->fl_type = F_UNLCK;
8310         file_lock->c.flc_owner = (fl_owner_t) !! 7867         file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
8311         file_lock->c.flc_pid = current->tgid; !! 7868         file_lock->fl_pid = current->tgid;
8312         file_lock->c.flc_file = nf->nf_file;  !! 7869         file_lock->fl_file = nf->nf_file;
8313         file_lock->c.flc_flags = FL_POSIX;    !! 7870         file_lock->fl_flags = FL_POSIX;
8314         file_lock->fl_lmops = &nfsd_posix_mng    7871         file_lock->fl_lmops = &nfsd_posix_mng_ops;
8315         file_lock->fl_start = locku->lu_offse    7872         file_lock->fl_start = locku->lu_offset;
8316                                                  7873 
8317         file_lock->fl_end = last_byte_offset(    7874         file_lock->fl_end = last_byte_offset(locku->lu_offset,
8318                                                  7875                                                 locku->lu_length);
8319         nfs4_transform_lock_offset(file_lock)    7876         nfs4_transform_lock_offset(file_lock);
8320                                                  7877 
8321         err = vfs_lock_file(nf->nf_file, F_SE    7878         err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, NULL);
8322         if (err) {                               7879         if (err) {
8323                 dprintk("NFSD: nfs4_locku: vf    7880                 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
8324                 goto out_nfserr;                 7881                 goto out_nfserr;
8325         }                                        7882         }
8326         nfs4_inc_and_copy_stateid(&locku->lu_    7883         nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
8327 put_file:                                        7884 put_file:
8328         nfsd_file_put(nf);                       7885         nfsd_file_put(nf);
8329 put_stateid:                                     7886 put_stateid:
8330         mutex_unlock(&stp->st_mutex);            7887         mutex_unlock(&stp->st_mutex);
8331         nfs4_put_stid(&stp->st_stid);            7888         nfs4_put_stid(&stp->st_stid);
8332 out:                                             7889 out:
8333         nfsd4_bump_seqid(cstate, status);        7890         nfsd4_bump_seqid(cstate, status);
8334         if (file_lock)                           7891         if (file_lock)
8335                 locks_free_lock(file_lock);      7892                 locks_free_lock(file_lock);
8336         return status;                           7893         return status;
8337                                                  7894 
8338 out_nfserr:                                      7895 out_nfserr:
8339         status = nfserrno(err);                  7896         status = nfserrno(err);
8340         goto put_file;                           7897         goto put_file;
8341 }                                                7898 }
8342                                                  7899 
8343 /*                                               7900 /*
8344  * returns                                       7901  * returns
8345  *      true:  locks held by lockowner           7902  *      true:  locks held by lockowner
8346  *      false: no locks held by lockowner        7903  *      false: no locks held by lockowner
8347  */                                              7904  */
8348 static bool                                      7905 static bool
8349 check_for_locks(struct nfs4_file *fp, struct     7906 check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
8350 {                                                7907 {
8351         struct file_lock *fl;                    7908         struct file_lock *fl;
8352         int status = false;                      7909         int status = false;
8353         struct nfsd_file *nf;                    7910         struct nfsd_file *nf;
8354         struct inode *inode;                     7911         struct inode *inode;
8355         struct file_lock_context *flctx;         7912         struct file_lock_context *flctx;
8356                                                  7913 
8357         spin_lock(&fp->fi_lock);                 7914         spin_lock(&fp->fi_lock);
8358         nf = find_any_file_locked(fp);           7915         nf = find_any_file_locked(fp);
8359         if (!nf) {                               7916         if (!nf) {
8360                 /* Any valid lock stateid sho    7917                 /* Any valid lock stateid should have some sort of access */
8361                 WARN_ON_ONCE(1);                 7918                 WARN_ON_ONCE(1);
8362                 goto out;                        7919                 goto out;
8363         }                                        7920         }
8364                                                  7921 
8365         inode = file_inode(nf->nf_file);         7922         inode = file_inode(nf->nf_file);
8366         flctx = locks_inode_context(inode);      7923         flctx = locks_inode_context(inode);
8367                                                  7924 
8368         if (flctx && !list_empty_careful(&flc    7925         if (flctx && !list_empty_careful(&flctx->flc_posix)) {
8369                 spin_lock(&flctx->flc_lock);     7926                 spin_lock(&flctx->flc_lock);
8370                 for_each_file_lock(fl, &flctx !! 7927                 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
8371                         if (fl->c.flc_owner = !! 7928                         if (fl->fl_owner == (fl_owner_t)lowner) {
8372                                 status = true    7929                                 status = true;
8373                                 break;           7930                                 break;
8374                         }                        7931                         }
8375                 }                                7932                 }
8376                 spin_unlock(&flctx->flc_lock)    7933                 spin_unlock(&flctx->flc_lock);
8377         }                                        7934         }
8378 out:                                             7935 out:
8379         spin_unlock(&fp->fi_lock);               7936         spin_unlock(&fp->fi_lock);
8380         return status;                           7937         return status;
8381 }                                                7938 }
8382                                                  7939 
8383 /**                                              7940 /**
8384  * nfsd4_release_lockowner - process NFSv4.0     7941  * nfsd4_release_lockowner - process NFSv4.0 RELEASE_LOCKOWNER operations
8385  * @rqstp: RPC transaction                       7942  * @rqstp: RPC transaction
8386  * @cstate: NFSv4 COMPOUND state                 7943  * @cstate: NFSv4 COMPOUND state
8387  * @u: RELEASE_LOCKOWNER arguments               7944  * @u: RELEASE_LOCKOWNER arguments
8388  *                                               7945  *
8389  * Check if there are any locks still held an !! 7946  * Check if theree are any locks still held and if not - free the lockowner
8390  * and any lock state that is owned.             7947  * and any lock state that is owned.
8391  *                                               7948  *
8392  * Return values:                                7949  * Return values:
8393  *   %nfs_ok: lockowner released or not found    7950  *   %nfs_ok: lockowner released or not found
8394  *   %nfserr_locks_held: lockowner still in u    7951  *   %nfserr_locks_held: lockowner still in use
8395  *   %nfserr_stale_clientid: clientid no long    7952  *   %nfserr_stale_clientid: clientid no longer active
8396  *   %nfserr_expired: clientid not recognized    7953  *   %nfserr_expired: clientid not recognized
8397  */                                              7954  */
8398 __be32                                           7955 __be32
8399 nfsd4_release_lockowner(struct svc_rqst *rqst    7956 nfsd4_release_lockowner(struct svc_rqst *rqstp,
8400                         struct nfsd4_compound    7957                         struct nfsd4_compound_state *cstate,
8401                         union nfsd4_op_u *u)     7958                         union nfsd4_op_u *u)
8402 {                                                7959 {
8403         struct nfsd4_release_lockowner *rlock    7960         struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner;
8404         struct nfsd_net *nn = net_generic(SVC    7961         struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
8405         clientid_t *clid = &rlockowner->rl_cl    7962         clientid_t *clid = &rlockowner->rl_clientid;
8406         struct nfs4_ol_stateid *stp;             7963         struct nfs4_ol_stateid *stp;
8407         struct nfs4_lockowner *lo;               7964         struct nfs4_lockowner *lo;
8408         struct nfs4_client *clp;                 7965         struct nfs4_client *clp;
8409         LIST_HEAD(reaplist);                     7966         LIST_HEAD(reaplist);
8410         __be32 status;                           7967         __be32 status;
8411                                                  7968 
8412         dprintk("nfsd4_release_lockowner clie    7969         dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
8413                 clid->cl_boot, clid->cl_id);     7970                 clid->cl_boot, clid->cl_id);
8414                                                  7971 
8415         status = set_client(clid, cstate, nn)    7972         status = set_client(clid, cstate, nn);
8416         if (status)                              7973         if (status)
8417                 return status;                   7974                 return status;
8418         clp = cstate->clp;                       7975         clp = cstate->clp;
8419                                                  7976 
8420         spin_lock(&clp->cl_lock);                7977         spin_lock(&clp->cl_lock);
8421         lo = find_lockowner_str_locked(clp, &    7978         lo = find_lockowner_str_locked(clp, &rlockowner->rl_owner);
8422         if (!lo) {                               7979         if (!lo) {
8423                 spin_unlock(&clp->cl_lock);      7980                 spin_unlock(&clp->cl_lock);
8424                 return nfs_ok;                   7981                 return nfs_ok;
8425         }                                        7982         }
8426                                                  7983 
8427         list_for_each_entry(stp, &lo->lo_owne    7984         list_for_each_entry(stp, &lo->lo_owner.so_stateids, st_perstateowner) {
8428                 if (check_for_locks(stp->st_s    7985                 if (check_for_locks(stp->st_stid.sc_file, lo)) {
8429                         spin_unlock(&clp->cl_    7986                         spin_unlock(&clp->cl_lock);
8430                         nfs4_put_stateowner(&    7987                         nfs4_put_stateowner(&lo->lo_owner);
8431                         return nfserr_locks_h    7988                         return nfserr_locks_held;
8432                 }                                7989                 }
8433         }                                        7990         }
8434         unhash_lockowner_locked(lo);             7991         unhash_lockowner_locked(lo);
8435         while (!list_empty(&lo->lo_owner.so_s    7992         while (!list_empty(&lo->lo_owner.so_stateids)) {
8436                 stp = list_first_entry(&lo->l    7993                 stp = list_first_entry(&lo->lo_owner.so_stateids,
8437                                        struct    7994                                        struct nfs4_ol_stateid,
8438                                        st_per    7995                                        st_perstateowner);
8439                 unhash_lock_stateid(stp);     !! 7996                 WARN_ON(!unhash_lock_stateid(stp));
8440                 put_ol_stateid_locked(stp, &r    7997                 put_ol_stateid_locked(stp, &reaplist);
8441         }                                        7998         }
8442         spin_unlock(&clp->cl_lock);              7999         spin_unlock(&clp->cl_lock);
8443                                                  8000 
8444         free_ol_stateid_reaplist(&reaplist);     8001         free_ol_stateid_reaplist(&reaplist);
8445         remove_blocked_locks(lo);                8002         remove_blocked_locks(lo);
8446         nfs4_put_stateowner(&lo->lo_owner);      8003         nfs4_put_stateowner(&lo->lo_owner);
8447         return nfs_ok;                           8004         return nfs_ok;
8448 }                                                8005 }
8449                                                  8006 
8450 static inline struct nfs4_client_reclaim *       8007 static inline struct nfs4_client_reclaim *
8451 alloc_reclaim(void)                              8008 alloc_reclaim(void)
8452 {                                                8009 {
8453         return kmalloc(sizeof(struct nfs4_cli    8010         return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
8454 }                                                8011 }
8455                                                  8012 
8456 bool                                             8013 bool
8457 nfs4_has_reclaimed_state(struct xdr_netobj na    8014 nfs4_has_reclaimed_state(struct xdr_netobj name, struct nfsd_net *nn)
8458 {                                                8015 {
8459         struct nfs4_client_reclaim *crp;         8016         struct nfs4_client_reclaim *crp;
8460                                                  8017 
8461         crp = nfsd4_find_reclaim_client(name,    8018         crp = nfsd4_find_reclaim_client(name, nn);
8462         return (crp && crp->cr_clp);             8019         return (crp && crp->cr_clp);
8463 }                                                8020 }
8464                                                  8021 
8465 /*                                               8022 /*
8466  * failure => all reset bets are off, nfserr_    8023  * failure => all reset bets are off, nfserr_no_grace...
8467  *                                               8024  *
8468  * The caller is responsible for freeing name    8025  * The caller is responsible for freeing name.data if NULL is returned (it
8469  * will be freed in nfs4_remove_reclaim_recor    8026  * will be freed in nfs4_remove_reclaim_record in the normal case).
8470  */                                              8027  */
8471 struct nfs4_client_reclaim *                     8028 struct nfs4_client_reclaim *
8472 nfs4_client_to_reclaim(struct xdr_netobj name    8029 nfs4_client_to_reclaim(struct xdr_netobj name, struct xdr_netobj princhash,
8473                 struct nfsd_net *nn)             8030                 struct nfsd_net *nn)
8474 {                                                8031 {
8475         unsigned int strhashval;                 8032         unsigned int strhashval;
8476         struct nfs4_client_reclaim *crp;         8033         struct nfs4_client_reclaim *crp;
8477                                                  8034 
8478         crp = alloc_reclaim();                   8035         crp = alloc_reclaim();
8479         if (crp) {                               8036         if (crp) {
8480                 strhashval = clientstr_hashva    8037                 strhashval = clientstr_hashval(name);
8481                 INIT_LIST_HEAD(&crp->cr_strha    8038                 INIT_LIST_HEAD(&crp->cr_strhash);
8482                 list_add(&crp->cr_strhash, &n    8039                 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
8483                 crp->cr_name.data = name.data    8040                 crp->cr_name.data = name.data;
8484                 crp->cr_name.len = name.len;     8041                 crp->cr_name.len = name.len;
8485                 crp->cr_princhash.data = prin    8042                 crp->cr_princhash.data = princhash.data;
8486                 crp->cr_princhash.len = princ    8043                 crp->cr_princhash.len = princhash.len;
8487                 crp->cr_clp = NULL;              8044                 crp->cr_clp = NULL;
8488                 nn->reclaim_str_hashtbl_size+    8045                 nn->reclaim_str_hashtbl_size++;
8489         }                                        8046         }
8490         return crp;                              8047         return crp;
8491 }                                                8048 }
8492                                                  8049 
8493 void                                             8050 void
8494 nfs4_remove_reclaim_record(struct nfs4_client    8051 nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
8495 {                                                8052 {
8496         list_del(&crp->cr_strhash);              8053         list_del(&crp->cr_strhash);
8497         kfree(crp->cr_name.data);                8054         kfree(crp->cr_name.data);
8498         kfree(crp->cr_princhash.data);           8055         kfree(crp->cr_princhash.data);
8499         kfree(crp);                              8056         kfree(crp);
8500         nn->reclaim_str_hashtbl_size--;          8057         nn->reclaim_str_hashtbl_size--;
8501 }                                                8058 }
8502                                                  8059 
8503 void                                             8060 void
8504 nfs4_release_reclaim(struct nfsd_net *nn)        8061 nfs4_release_reclaim(struct nfsd_net *nn)
8505 {                                                8062 {
8506         struct nfs4_client_reclaim *crp = NUL    8063         struct nfs4_client_reclaim *crp = NULL;
8507         int i;                                   8064         int i;
8508                                                  8065 
8509         for (i = 0; i < CLIENT_HASH_SIZE; i++    8066         for (i = 0; i < CLIENT_HASH_SIZE; i++) {
8510                 while (!list_empty(&nn->recla    8067                 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
8511                         crp = list_entry(nn->    8068                         crp = list_entry(nn->reclaim_str_hashtbl[i].next,
8512                                         struc    8069                                         struct nfs4_client_reclaim, cr_strhash);
8513                         nfs4_remove_reclaim_r    8070                         nfs4_remove_reclaim_record(crp, nn);
8514                 }                                8071                 }
8515         }                                        8072         }
8516         WARN_ON_ONCE(nn->reclaim_str_hashtbl_    8073         WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
8517 }                                                8074 }
8518                                                  8075 
8519 /*                                               8076 /*
8520  * called from OPEN, CLAIM_PREVIOUS with a ne    8077  * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
8521 struct nfs4_client_reclaim *                     8078 struct nfs4_client_reclaim *
8522 nfsd4_find_reclaim_client(struct xdr_netobj n    8079 nfsd4_find_reclaim_client(struct xdr_netobj name, struct nfsd_net *nn)
8523 {                                                8080 {
8524         unsigned int strhashval;                 8081         unsigned int strhashval;
8525         struct nfs4_client_reclaim *crp = NUL    8082         struct nfs4_client_reclaim *crp = NULL;
8526                                                  8083 
8527         strhashval = clientstr_hashval(name);    8084         strhashval = clientstr_hashval(name);
8528         list_for_each_entry(crp, &nn->reclaim    8085         list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
8529                 if (compare_blob(&crp->cr_nam    8086                 if (compare_blob(&crp->cr_name, &name) == 0) {
8530                         return crp;              8087                         return crp;
8531                 }                                8088                 }
8532         }                                        8089         }
8533         return NULL;                             8090         return NULL;
8534 }                                                8091 }
8535                                                  8092 
8536 __be32                                           8093 __be32
8537 nfs4_check_open_reclaim(struct nfs4_client *c    8094 nfs4_check_open_reclaim(struct nfs4_client *clp)
8538 {                                                8095 {
8539         if (test_bit(NFSD4_CLIENT_RECLAIM_COM    8096         if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &clp->cl_flags))
8540                 return nfserr_no_grace;          8097                 return nfserr_no_grace;
8541                                                  8098 
8542         if (nfsd4_client_record_check(clp))      8099         if (nfsd4_client_record_check(clp))
8543                 return nfserr_reclaim_bad;       8100                 return nfserr_reclaim_bad;
8544                                                  8101 
8545         return nfs_ok;                           8102         return nfs_ok;
8546 }                                                8103 }
8547                                                  8104 
8548 /*                                               8105 /*
8549  * Since the lifetime of a delegation isn't l    8106  * Since the lifetime of a delegation isn't limited to that of an open, a
8550  * client may quite reasonably hang on to a d    8107  * client may quite reasonably hang on to a delegation as long as it has
8551  * the inode cached.  This becomes an obvious    8108  * the inode cached.  This becomes an obvious problem the first time a
8552  * client's inode cache approaches the size o    8109  * client's inode cache approaches the size of the server's total memory.
8553  *                                               8110  *
8554  * For now we avoid this problem by imposing     8111  * For now we avoid this problem by imposing a hard limit on the number
8555  * of delegations, which varies according to     8112  * of delegations, which varies according to the server's memory size.
8556  */                                              8113  */
8557 static void                                      8114 static void
8558 set_max_delegations(void)                        8115 set_max_delegations(void)
8559 {                                                8116 {
8560         /*                                       8117         /*
8561          * Allow at most 4 delegations per me    8118          * Allow at most 4 delegations per megabyte of RAM.  Quick
8562          * estimates suggest that in the wors    8119          * estimates suggest that in the worst case (where every delegation
8563          * is for a different inode), a deleg    8120          * is for a different inode), a delegation could take about 1.5K,
8564          * giving a worst case usage of about    8121          * giving a worst case usage of about 6% of memory.
8565          */                                      8122          */
8566         max_delegations = nr_free_buffer_page    8123         max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
8567 }                                                8124 }
8568                                                  8125 
8569 static int nfs4_state_create_net(struct net *    8126 static int nfs4_state_create_net(struct net *net)
8570 {                                                8127 {
8571         struct nfsd_net *nn = net_generic(net    8128         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
8572         int i;                                   8129         int i;
8573                                                  8130 
8574         nn->conf_id_hashtbl = kmalloc_array(C    8131         nn->conf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
8575                                             s    8132                                             sizeof(struct list_head),
8576                                             G    8133                                             GFP_KERNEL);
8577         if (!nn->conf_id_hashtbl)                8134         if (!nn->conf_id_hashtbl)
8578                 goto err;                        8135                 goto err;
8579         nn->unconf_id_hashtbl = kmalloc_array    8136         nn->unconf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
8580                                                  8137                                               sizeof(struct list_head),
8581                                                  8138                                               GFP_KERNEL);
8582         if (!nn->unconf_id_hashtbl)              8139         if (!nn->unconf_id_hashtbl)
8583                 goto err_unconf_id;              8140                 goto err_unconf_id;
8584         nn->sessionid_hashtbl = kmalloc_array    8141         nn->sessionid_hashtbl = kmalloc_array(SESSION_HASH_SIZE,
8585                                                  8142                                               sizeof(struct list_head),
8586                                                  8143                                               GFP_KERNEL);
8587         if (!nn->sessionid_hashtbl)              8144         if (!nn->sessionid_hashtbl)
8588                 goto err_sessionid;              8145                 goto err_sessionid;
8589                                                  8146 
8590         for (i = 0; i < CLIENT_HASH_SIZE; i++    8147         for (i = 0; i < CLIENT_HASH_SIZE; i++) {
8591                 INIT_LIST_HEAD(&nn->conf_id_h    8148                 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
8592                 INIT_LIST_HEAD(&nn->unconf_id    8149                 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
8593         }                                        8150         }
8594         for (i = 0; i < SESSION_HASH_SIZE; i+    8151         for (i = 0; i < SESSION_HASH_SIZE; i++)
8595                 INIT_LIST_HEAD(&nn->sessionid    8152                 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
8596         nn->conf_name_tree = RB_ROOT;            8153         nn->conf_name_tree = RB_ROOT;
8597         nn->unconf_name_tree = RB_ROOT;          8154         nn->unconf_name_tree = RB_ROOT;
8598         nn->boot_time = ktime_get_real_second    8155         nn->boot_time = ktime_get_real_seconds();
8599         nn->grace_ended = false;                 8156         nn->grace_ended = false;
8600         nn->nfsd4_manager.block_opens = true;    8157         nn->nfsd4_manager.block_opens = true;
8601         INIT_LIST_HEAD(&nn->nfsd4_manager.lis    8158         INIT_LIST_HEAD(&nn->nfsd4_manager.list);
8602         INIT_LIST_HEAD(&nn->client_lru);         8159         INIT_LIST_HEAD(&nn->client_lru);
8603         INIT_LIST_HEAD(&nn->close_lru);          8160         INIT_LIST_HEAD(&nn->close_lru);
8604         INIT_LIST_HEAD(&nn->del_recall_lru);     8161         INIT_LIST_HEAD(&nn->del_recall_lru);
8605         spin_lock_init(&nn->client_lock);        8162         spin_lock_init(&nn->client_lock);
8606         spin_lock_init(&nn->s2s_cp_lock);        8163         spin_lock_init(&nn->s2s_cp_lock);
8607         idr_init(&nn->s2s_cp_stateids);          8164         idr_init(&nn->s2s_cp_stateids);
8608         atomic_set(&nn->pending_async_copies, << 
8609                                                  8165 
8610         spin_lock_init(&nn->blocked_locks_loc    8166         spin_lock_init(&nn->blocked_locks_lock);
8611         INIT_LIST_HEAD(&nn->blocked_locks_lru    8167         INIT_LIST_HEAD(&nn->blocked_locks_lru);
8612                                                  8168 
8613         INIT_DELAYED_WORK(&nn->laundromat_wor    8169         INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
8614         INIT_WORK(&nn->nfsd_shrinker_work, nf    8170         INIT_WORK(&nn->nfsd_shrinker_work, nfsd4_state_shrinker_worker);
8615         get_net(net);                            8171         get_net(net);
8616                                                  8172 
8617         nn->nfsd_client_shrinker = shrinker_a    8173         nn->nfsd_client_shrinker = shrinker_alloc(0, "nfsd-client");
8618         if (!nn->nfsd_client_shrinker)           8174         if (!nn->nfsd_client_shrinker)
8619                 goto err_shrinker;               8175                 goto err_shrinker;
8620                                                  8176 
8621         nn->nfsd_client_shrinker->scan_object    8177         nn->nfsd_client_shrinker->scan_objects = nfsd4_state_shrinker_scan;
8622         nn->nfsd_client_shrinker->count_objec    8178         nn->nfsd_client_shrinker->count_objects = nfsd4_state_shrinker_count;
8623         nn->nfsd_client_shrinker->private_dat    8179         nn->nfsd_client_shrinker->private_data = nn;
8624                                                  8180 
8625         shrinker_register(nn->nfsd_client_shr    8181         shrinker_register(nn->nfsd_client_shrinker);
8626                                                  8182 
8627         return 0;                                8183         return 0;
8628                                                  8184 
8629 err_shrinker:                                    8185 err_shrinker:
8630         put_net(net);                            8186         put_net(net);
8631         kfree(nn->sessionid_hashtbl);            8187         kfree(nn->sessionid_hashtbl);
8632 err_sessionid:                                   8188 err_sessionid:
8633         kfree(nn->unconf_id_hashtbl);            8189         kfree(nn->unconf_id_hashtbl);
8634 err_unconf_id:                                   8190 err_unconf_id:
8635         kfree(nn->conf_id_hashtbl);              8191         kfree(nn->conf_id_hashtbl);
8636 err:                                             8192 err:
8637         return -ENOMEM;                          8193         return -ENOMEM;
8638 }                                                8194 }
8639                                                  8195 
8640 static void                                      8196 static void
8641 nfs4_state_destroy_net(struct net *net)          8197 nfs4_state_destroy_net(struct net *net)
8642 {                                                8198 {
8643         int i;                                   8199         int i;
8644         struct nfs4_client *clp = NULL;          8200         struct nfs4_client *clp = NULL;
8645         struct nfsd_net *nn = net_generic(net    8201         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
8646                                                  8202 
8647         for (i = 0; i < CLIENT_HASH_SIZE; i++    8203         for (i = 0; i < CLIENT_HASH_SIZE; i++) {
8648                 while (!list_empty(&nn->conf_    8204                 while (!list_empty(&nn->conf_id_hashtbl[i])) {
8649                         clp = list_entry(nn->    8205                         clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
8650                         destroy_client(clp);     8206                         destroy_client(clp);
8651                 }                                8207                 }
8652         }                                        8208         }
8653                                                  8209 
8654         WARN_ON(!list_empty(&nn->blocked_lock    8210         WARN_ON(!list_empty(&nn->blocked_locks_lru));
8655                                                  8211 
8656         for (i = 0; i < CLIENT_HASH_SIZE; i++    8212         for (i = 0; i < CLIENT_HASH_SIZE; i++) {
8657                 while (!list_empty(&nn->uncon    8213                 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
8658                         clp = list_entry(nn->    8214                         clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
8659                         destroy_client(clp);     8215                         destroy_client(clp);
8660                 }                                8216                 }
8661         }                                        8217         }
8662                                                  8218 
8663         kfree(nn->sessionid_hashtbl);            8219         kfree(nn->sessionid_hashtbl);
8664         kfree(nn->unconf_id_hashtbl);            8220         kfree(nn->unconf_id_hashtbl);
8665         kfree(nn->conf_id_hashtbl);              8221         kfree(nn->conf_id_hashtbl);
8666         put_net(net);                            8222         put_net(net);
8667 }                                                8223 }
8668                                                  8224 
8669 int                                              8225 int
8670 nfs4_state_start_net(struct net *net)            8226 nfs4_state_start_net(struct net *net)
8671 {                                                8227 {
8672         struct nfsd_net *nn = net_generic(net    8228         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
8673         int ret;                                 8229         int ret;
8674                                                  8230 
8675         ret = nfs4_state_create_net(net);        8231         ret = nfs4_state_create_net(net);
8676         if (ret)                                 8232         if (ret)
8677                 return ret;                      8233                 return ret;
8678         locks_start_grace(net, &nn->nfsd4_man    8234         locks_start_grace(net, &nn->nfsd4_manager);
8679         nfsd4_client_tracking_init(net);         8235         nfsd4_client_tracking_init(net);
8680         if (nn->track_reclaim_completes && nn    8236         if (nn->track_reclaim_completes && nn->reclaim_str_hashtbl_size == 0)
8681                 goto skip_grace;                 8237                 goto skip_grace;
8682         printk(KERN_INFO "NFSD: starting %lld    8238         printk(KERN_INFO "NFSD: starting %lld-second grace period (net %x)\n",
8683                nn->nfsd4_grace, net->ns.inum)    8239                nn->nfsd4_grace, net->ns.inum);
8684         trace_nfsd_grace_start(nn);              8240         trace_nfsd_grace_start(nn);
8685         queue_delayed_work(laundry_wq, &nn->l    8241         queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
8686         return 0;                                8242         return 0;
8687                                                  8243 
8688 skip_grace:                                      8244 skip_grace:
8689         printk(KERN_INFO "NFSD: no clients to    8245         printk(KERN_INFO "NFSD: no clients to reclaim, skipping NFSv4 grace period (net %x)\n",
8690                         net->ns.inum);           8246                         net->ns.inum);
8691         queue_delayed_work(laundry_wq, &nn->l    8247         queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_lease * HZ);
8692         nfsd4_end_grace(nn);                     8248         nfsd4_end_grace(nn);
8693         return 0;                                8249         return 0;
8694 }                                                8250 }
8695                                                  8251 
8696 /* initialization to perform when the nfsd se    8252 /* initialization to perform when the nfsd service is started: */
8697                                                  8253 
8698 int                                              8254 int
8699 nfs4_state_start(void)                           8255 nfs4_state_start(void)
8700 {                                                8256 {
8701         int ret;                                 8257         int ret;
8702                                                  8258 
8703         ret = rhltable_init(&nfs4_file_rhltab    8259         ret = rhltable_init(&nfs4_file_rhltable, &nfs4_file_rhash_params);
8704         if (ret)                                 8260         if (ret)
8705                 return ret;                      8261                 return ret;
8706                                                  8262 
                                                   >> 8263         ret = nfsd4_create_callback_queue();
                                                   >> 8264         if (ret) {
                                                   >> 8265                 rhltable_destroy(&nfs4_file_rhltable);
                                                   >> 8266                 return ret;
                                                   >> 8267         }
                                                   >> 8268 
8707         set_max_delegations();                   8269         set_max_delegations();
8708         return 0;                                8270         return 0;
8709 }                                                8271 }
8710                                                  8272 
8711 void                                             8273 void
8712 nfs4_state_shutdown_net(struct net *net)         8274 nfs4_state_shutdown_net(struct net *net)
8713 {                                                8275 {
8714         struct nfs4_delegation *dp = NULL;       8276         struct nfs4_delegation *dp = NULL;
8715         struct list_head *pos, *next, reaplis    8277         struct list_head *pos, *next, reaplist;
8716         struct nfsd_net *nn = net_generic(net    8278         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
8717                                                  8279 
8718         shrinker_free(nn->nfsd_client_shrinke    8280         shrinker_free(nn->nfsd_client_shrinker);
8719         cancel_work_sync(&nn->nfsd_shrinker_w !! 8281         cancel_work(&nn->nfsd_shrinker_work);
8720         cancel_delayed_work_sync(&nn->laundro    8282         cancel_delayed_work_sync(&nn->laundromat_work);
8721         locks_end_grace(&nn->nfsd4_manager);     8283         locks_end_grace(&nn->nfsd4_manager);
8722                                                  8284 
8723         INIT_LIST_HEAD(&reaplist);               8285         INIT_LIST_HEAD(&reaplist);
8724         spin_lock(&state_lock);                  8286         spin_lock(&state_lock);
8725         list_for_each_safe(pos, next, &nn->de    8287         list_for_each_safe(pos, next, &nn->del_recall_lru) {
8726                 dp = list_entry (pos, struct     8288                 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
8727                 unhash_delegation_locked(dp,  !! 8289                 WARN_ON(!unhash_delegation_locked(dp));
8728                 list_add(&dp->dl_recall_lru,     8290                 list_add(&dp->dl_recall_lru, &reaplist);
8729         }                                        8291         }
8730         spin_unlock(&state_lock);                8292         spin_unlock(&state_lock);
8731         list_for_each_safe(pos, next, &reapli    8293         list_for_each_safe(pos, next, &reaplist) {
8732                 dp = list_entry (pos, struct     8294                 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
8733                 list_del_init(&dp->dl_recall_    8295                 list_del_init(&dp->dl_recall_lru);
8734                 destroy_unhashed_deleg(dp);      8296                 destroy_unhashed_deleg(dp);
8735         }                                        8297         }
8736                                                  8298 
8737         nfsd4_client_tracking_exit(net);         8299         nfsd4_client_tracking_exit(net);
8738         nfs4_state_destroy_net(net);             8300         nfs4_state_destroy_net(net);
8739 #ifdef CONFIG_NFSD_V4_2_INTER_SSC                8301 #ifdef CONFIG_NFSD_V4_2_INTER_SSC
8740         nfsd4_ssc_shutdown_umount(nn);           8302         nfsd4_ssc_shutdown_umount(nn);
8741 #endif                                           8303 #endif
8742 }                                                8304 }
8743                                                  8305 
8744 void                                             8306 void
8745 nfs4_state_shutdown(void)                        8307 nfs4_state_shutdown(void)
8746 {                                                8308 {
                                                   >> 8309         nfsd4_destroy_callback_queue();
8747         rhltable_destroy(&nfs4_file_rhltable)    8310         rhltable_destroy(&nfs4_file_rhltable);
8748 }                                                8311 }
8749                                                  8312 
8750 static void                                      8313 static void
8751 get_stateid(struct nfsd4_compound_state *csta    8314 get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
8752 {                                                8315 {
8753         if (HAS_CSTATE_FLAG(cstate, CURRENT_S    8316         if (HAS_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG) &&
8754             CURRENT_STATEID(stateid))            8317             CURRENT_STATEID(stateid))
8755                 memcpy(stateid, &cstate->curr    8318                 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
8756 }                                                8319 }
8757                                                  8320 
8758 static void                                      8321 static void
8759 put_stateid(struct nfsd4_compound_state *csta    8322 put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
8760 {                                                8323 {
8761         if (cstate->minorversion) {              8324         if (cstate->minorversion) {
8762                 memcpy(&cstate->current_state    8325                 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
8763                 SET_CSTATE_FLAG(cstate, CURRE    8326                 SET_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
8764         }                                        8327         }
8765 }                                                8328 }
8766                                                  8329 
8767 void                                             8330 void
8768 clear_current_stateid(struct nfsd4_compound_s    8331 clear_current_stateid(struct nfsd4_compound_state *cstate)
8769 {                                                8332 {
8770         CLEAR_CSTATE_FLAG(cstate, CURRENT_STA    8333         CLEAR_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
8771 }                                                8334 }
8772                                                  8335 
8773 /*                                               8336 /*
8774  * functions to set current state id             8337  * functions to set current state id
8775  */                                              8338  */
8776 void                                             8339 void
8777 nfsd4_set_opendowngradestateid(struct nfsd4_c    8340 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate,
8778                 union nfsd4_op_u *u)             8341                 union nfsd4_op_u *u)
8779 {                                                8342 {
8780         put_stateid(cstate, &u->open_downgrad    8343         put_stateid(cstate, &u->open_downgrade.od_stateid);
8781 }                                                8344 }
8782                                                  8345 
8783 void                                             8346 void
8784 nfsd4_set_openstateid(struct nfsd4_compound_s    8347 nfsd4_set_openstateid(struct nfsd4_compound_state *cstate,
8785                 union nfsd4_op_u *u)             8348                 union nfsd4_op_u *u)
8786 {                                                8349 {
8787         put_stateid(cstate, &u->open.op_state    8350         put_stateid(cstate, &u->open.op_stateid);
8788 }                                                8351 }
8789                                                  8352 
8790 void                                             8353 void
8791 nfsd4_set_closestateid(struct nfsd4_compound_    8354 nfsd4_set_closestateid(struct nfsd4_compound_state *cstate,
8792                 union nfsd4_op_u *u)             8355                 union nfsd4_op_u *u)
8793 {                                                8356 {
8794         put_stateid(cstate, &u->close.cl_stat    8357         put_stateid(cstate, &u->close.cl_stateid);
8795 }                                                8358 }
8796                                                  8359 
8797 void                                             8360 void
8798 nfsd4_set_lockstateid(struct nfsd4_compound_s    8361 nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate,
8799                 union nfsd4_op_u *u)             8362                 union nfsd4_op_u *u)
8800 {                                                8363 {
8801         put_stateid(cstate, &u->lock.lk_resp_    8364         put_stateid(cstate, &u->lock.lk_resp_stateid);
8802 }                                                8365 }
8803                                                  8366 
8804 /*                                               8367 /*
8805  * functions to consume current state id         8368  * functions to consume current state id
8806  */                                              8369  */
8807                                                  8370 
8808 void                                             8371 void
8809 nfsd4_get_opendowngradestateid(struct nfsd4_c    8372 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate,
8810                 union nfsd4_op_u *u)             8373                 union nfsd4_op_u *u)
8811 {                                                8374 {
8812         get_stateid(cstate, &u->open_downgrad    8375         get_stateid(cstate, &u->open_downgrade.od_stateid);
8813 }                                                8376 }
8814                                                  8377 
8815 void                                             8378 void
8816 nfsd4_get_delegreturnstateid(struct nfsd4_com    8379 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate,
8817                 union nfsd4_op_u *u)             8380                 union nfsd4_op_u *u)
8818 {                                                8381 {
8819         get_stateid(cstate, &u->delegreturn.d    8382         get_stateid(cstate, &u->delegreturn.dr_stateid);
8820 }                                                8383 }
8821                                                  8384 
8822 void                                             8385 void
8823 nfsd4_get_freestateid(struct nfsd4_compound_s    8386 nfsd4_get_freestateid(struct nfsd4_compound_state *cstate,
8824                 union nfsd4_op_u *u)             8387                 union nfsd4_op_u *u)
8825 {                                                8388 {
8826         get_stateid(cstate, &u->free_stateid.    8389         get_stateid(cstate, &u->free_stateid.fr_stateid);
8827 }                                                8390 }
8828                                                  8391 
8829 void                                             8392 void
8830 nfsd4_get_setattrstateid(struct nfsd4_compoun    8393 nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate,
8831                 union nfsd4_op_u *u)             8394                 union nfsd4_op_u *u)
8832 {                                                8395 {
8833         get_stateid(cstate, &u->setattr.sa_st    8396         get_stateid(cstate, &u->setattr.sa_stateid);
8834 }                                                8397 }
8835                                                  8398 
8836 void                                             8399 void
8837 nfsd4_get_closestateid(struct nfsd4_compound_    8400 nfsd4_get_closestateid(struct nfsd4_compound_state *cstate,
8838                 union nfsd4_op_u *u)             8401                 union nfsd4_op_u *u)
8839 {                                                8402 {
8840         get_stateid(cstate, &u->close.cl_stat    8403         get_stateid(cstate, &u->close.cl_stateid);
8841 }                                                8404 }
8842                                                  8405 
8843 void                                             8406 void
8844 nfsd4_get_lockustateid(struct nfsd4_compound_    8407 nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate,
8845                 union nfsd4_op_u *u)             8408                 union nfsd4_op_u *u)
8846 {                                                8409 {
8847         get_stateid(cstate, &u->locku.lu_stat    8410         get_stateid(cstate, &u->locku.lu_stateid);
8848 }                                                8411 }
8849                                                  8412 
8850 void                                             8413 void
8851 nfsd4_get_readstateid(struct nfsd4_compound_s    8414 nfsd4_get_readstateid(struct nfsd4_compound_state *cstate,
8852                 union nfsd4_op_u *u)             8415                 union nfsd4_op_u *u)
8853 {                                                8416 {
8854         get_stateid(cstate, &u->read.rd_state    8417         get_stateid(cstate, &u->read.rd_stateid);
8855 }                                                8418 }
8856                                                  8419 
8857 void                                             8420 void
8858 nfsd4_get_writestateid(struct nfsd4_compound_    8421 nfsd4_get_writestateid(struct nfsd4_compound_state *cstate,
8859                 union nfsd4_op_u *u)             8422                 union nfsd4_op_u *u)
8860 {                                                8423 {
8861         get_stateid(cstate, &u->write.wr_stat    8424         get_stateid(cstate, &u->write.wr_stateid);
8862 }                                                8425 }
8863                                                  8426 
8864 /**                                              8427 /**
8865  * nfsd4_deleg_getattr_conflict - Recall if G    8428  * nfsd4_deleg_getattr_conflict - Recall if GETATTR causes conflict
8866  * @rqstp: RPC transaction context               8429  * @rqstp: RPC transaction context
8867  * @dentry: dentry of inode to be checked for !! 8430  * @inode: file to be checked for a conflict
8868  * @modified: return true if file was modifie << 
8869  * @size: new size of file if modified is tru << 
8870  *                                               8431  *
8871  * This function is called when there is a co    8432  * This function is called when there is a conflict between a write
8872  * delegation and a change/size GETATTR from     8433  * delegation and a change/size GETATTR from another client. The server
8873  * must either use the CB_GETATTR to get the     8434  * must either use the CB_GETATTR to get the current values of the
8874  * attributes from the client that holds the     8435  * attributes from the client that holds the delegation or recall the
8875  * delegation before replying to the GETATTR.    8436  * delegation before replying to the GETATTR. See RFC 8881 section
8876  * 18.7.4.                                       8437  * 18.7.4.
8877  *                                               8438  *
                                                   >> 8439  * The current implementation does not support CB_GETATTR yet. However
                                                   >> 8440  * this can avoid recalling the delegation could be added in follow up
                                                   >> 8441  * work.
                                                   >> 8442  *
8878  * Returns 0 if there is no conflict; otherwi    8443  * Returns 0 if there is no conflict; otherwise an nfs_stat
8879  * code is returned.                             8444  * code is returned.
8880  */                                              8445  */
8881 __be32                                           8446 __be32
8882 nfsd4_deleg_getattr_conflict(struct svc_rqst  !! 8447 nfsd4_deleg_getattr_conflict(struct svc_rqst *rqstp, struct inode *inode)
8883                                 bool *modifie << 
8884 {                                                8448 {
8885         __be32 status;                           8449         __be32 status;
8886         struct nfsd_net *nn = net_generic(SVC    8450         struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
8887         struct file_lock_context *ctx;           8451         struct file_lock_context *ctx;
8888         struct nfs4_delegation *dp = NULL;    !! 8452         struct file_lock *fl;
8889         struct file_lease *fl;                !! 8453         struct nfs4_delegation *dp;
8890         struct iattr attrs;                   << 
8891         struct nfs4_cb_fattr *ncf;            << 
8892         struct inode *inode = d_inode(dentry) << 
8893                                                  8454 
8894         *modified = false;                    << 
8895         ctx = locks_inode_context(inode);        8455         ctx = locks_inode_context(inode);
8896         if (!ctx)                                8456         if (!ctx)
8897                 return 0;                        8457                 return 0;
8898                                               << 
8899 #define NON_NFSD_LEASE ((void *)1)            << 
8900                                               << 
8901         spin_lock(&ctx->flc_lock);               8458         spin_lock(&ctx->flc_lock);
8902         for_each_file_lock(fl, &ctx->flc_leas !! 8459         list_for_each_entry(fl, &ctx->flc_lease, fl_list) {
8903                 if (fl->c.flc_flags == FL_LAY !! 8460                 if (fl->fl_flags == FL_LAYOUT)
8904                         continue;                8461                         continue;
8905                 if (fl->c.flc_type == F_WRLCK !! 8462                 if (fl->fl_lmops != &nfsd_lease_mng_ops) {
8906                         if (fl->fl_lmops == & !! 8463                         /*
8907                                 dp = fl->c.fl !! 8464                          * non-nfs lease, if it's a lease with F_RDLCK then
8908                         else                  !! 8465                          * we are done; there isn't any write delegation
8909                                 dp = NON_NFSD !! 8466                          * on this inode
                                                   >> 8467                          */
                                                   >> 8468                         if (fl->fl_type == F_RDLCK)
                                                   >> 8469                                 break;
                                                   >> 8470                         goto break_lease;
8910                 }                                8471                 }
8911                 break;                        !! 8472                 if (fl->fl_type == F_WRLCK) {
8912         }                                     !! 8473                         dp = fl->fl_owner;
8913         if (dp == NULL || dp == NON_NFSD_LEAS !! 8474                         if (dp->dl_recall.cb_clp == *(rqstp->rq_lease_breaker)) {
8914             dp->dl_recall.cb_clp == *(rqstp-> !! 8475                                 spin_unlock(&ctx->flc_lock);
8915                 spin_unlock(&ctx->flc_lock);  !! 8476                                 return 0;
8916                 if (dp == NON_NFSD_LEASE) {   !! 8477                         }
8917                         status = nfserrno(nfs !! 8478 break_lease:
8918                                               !! 8479                         spin_unlock(&ctx->flc_lock);
                                                   >> 8480                         nfsd_stats_wdeleg_getattr_inc(nn);
                                                   >> 8481                         status = nfserrno(nfsd_open_break_lease(inode, NFSD_MAY_READ));
8919                         if (status != nfserr_    8482                         if (status != nfserr_jukebox ||
8920                             !nfsd_wait_for_de !! 8483                                         !nfsd_wait_for_delegreturn(rqstp, inode))
8921                                 return status    8484                                 return status;
                                                   >> 8485                         return 0;
8922                 }                                8486                 }
8923                 return 0;                     !! 8487                 break;
8924         }                                        8488         }
8925                                               << 
8926         nfsd_stats_wdeleg_getattr_inc(nn);    << 
8927         refcount_inc(&dp->dl_stid.sc_count);  << 
8928         ncf = &dp->dl_cb_fattr;               << 
8929         nfs4_cb_getattr(&dp->dl_cb_fattr);    << 
8930         spin_unlock(&ctx->flc_lock);             8489         spin_unlock(&ctx->flc_lock);
8931                                               !! 8490         return 0;
8932         wait_on_bit_timeout(&ncf->ncf_cb_flag << 
8933                             TASK_INTERRUPTIBL << 
8934         if (ncf->ncf_cb_status) {             << 
8935                 /* Recall delegation only if  << 
8936                 status = nfserrno(nfsd_open_b << 
8937                 if (status != nfserr_jukebox  << 
8938                     !nfsd_wait_for_delegretur << 
8939                         goto out_status;      << 
8940         }                                     << 
8941         if (!ncf->ncf_file_modified &&        << 
8942             (ncf->ncf_initial_cinfo != ncf->n << 
8943              ncf->ncf_cur_fsize != ncf->ncf_c << 
8944                 ncf->ncf_file_modified = true << 
8945         if (ncf->ncf_file_modified) {         << 
8946                 int err;                      << 
8947                                               << 
8948                 /*                            << 
8949                  * Per section 10.4.3 of RFC  << 
8950                  * not update the file's meta << 
8951                  * modified size              << 
8952                  */                           << 
8953                 attrs.ia_mtime = attrs.ia_cti << 
8954                 attrs.ia_valid = ATTR_MTIME | << 
8955                 inode_lock(inode);            << 
8956                 err = notify_change(&nop_mnt_ << 
8957                 inode_unlock(inode);          << 
8958                 if (err) {                    << 
8959                         status = nfserrno(err << 
8960                         goto out_status;      << 
8961                 }                             << 
8962                 ncf->ncf_cur_fsize = ncf->ncf << 
8963                 *size = ncf->ncf_cur_fsize;   << 
8964                 *modified = true;             << 
8965         }                                     << 
8966         status = 0;                           << 
8967 out_status:                                   << 
8968         nfs4_put_stid(&dp->dl_stid);          << 
8969         return status;                        << 
8970 }                                                8491 }
8971                                                  8492 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php