~ [ 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-4.9.337)


  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>              << 
 46 #include <linux/fsnotify.h>                    << 
 47 #include <linux/rhashtable.h>                  << 
 48 #include <linux/nfs_ssc.h>                     << 
 49                                                << 
 50 #include "xdr4.h"                                  45 #include "xdr4.h"
 51 #include "xdr4cb.h"                                46 #include "xdr4cb.h"
 52 #include "vfs.h"                                   47 #include "vfs.h"
 53 #include "current_stateid.h"                       48 #include "current_stateid.h"
 54                                                    49 
 55 #include "netns.h"                                 50 #include "netns.h"
 56 #include "pnfs.h"                                  51 #include "pnfs.h"
 57 #include "filecache.h"                         << 
 58 #include "trace.h"                             << 
 59                                                    52 
 60 #define NFSDDBG_FACILITY                NFSDDB     53 #define NFSDDBG_FACILITY                NFSDDBG_PROC
 61                                                    54 
 62 #define all_ones {{ ~0, ~0}, ~0}               !!  55 #define all_ones {{~0,~0},~0}
 63 static const stateid_t one_stateid = {             56 static const stateid_t one_stateid = {
 64         .si_generation = ~0,                       57         .si_generation = ~0,
 65         .si_opaque = all_ones,                     58         .si_opaque = all_ones,
 66 };                                                 59 };
 67 static const stateid_t zero_stateid = {            60 static const stateid_t zero_stateid = {
 68         /* all fields zero */                      61         /* all fields zero */
 69 };                                                 62 };
 70 static const stateid_t currentstateid = {          63 static const stateid_t currentstateid = {
 71         .si_generation = 1,                        64         .si_generation = 1,
 72 };                                                 65 };
 73 static const stateid_t close_stateid = {           66 static const stateid_t close_stateid = {
 74         .si_generation = 0xffffffffU,              67         .si_generation = 0xffffffffU,
 75 };                                                 68 };
 76                                                    69 
 77 static u64 current_sessionid = 1;                  70 static u64 current_sessionid = 1;
 78                                                    71 
 79 #define ZERO_STATEID(stateid) (!memcmp((statei     72 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
 80 #define ONE_STATEID(stateid)  (!memcmp((statei     73 #define ONE_STATEID(stateid)  (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
 81 #define CURRENT_STATEID(stateid) (!memcmp((sta     74 #define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
 82 #define CLOSE_STATEID(stateid)  (!memcmp((stat     75 #define CLOSE_STATEID(stateid)  (!memcmp((stateid), &close_stateid, sizeof(stateid_t)))
 83                                                    76 
 84 /* forward declarations */                         77 /* forward declarations */
 85 static bool check_for_locks(struct nfs4_file *     78 static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
 86 static void nfs4_free_ol_stateid(struct nfs4_s     79 static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
 87 void nfsd4_end_grace(struct nfsd_net *nn);     << 
 88 static void _free_cpntf_state_locked(struct nf << 
 89 static void nfsd4_file_hash_remove(struct nfs4 << 
 90 static void deleg_reaper(struct nfsd_net *nn); << 
 91                                                    80 
 92 /* Locking: */                                     81 /* Locking: */
 93                                                    82 
 94 /*                                                 83 /*
 95  * Currently used for the del_recall_lru and f     84  * Currently used for the del_recall_lru and file hash table.  In an
 96  * effort to decrease the scope of the client_     85  * effort to decrease the scope of the client_mutex, this spinlock may
 97  * eventually cover more:                          86  * eventually cover more:
 98  */                                                87  */
 99 static DEFINE_SPINLOCK(state_lock);                88 static DEFINE_SPINLOCK(state_lock);
100                                                    89 
101 enum nfsd4_st_mutex_lock_subclass {            << 
102         OPEN_STATEID_MUTEX = 0,                << 
103         LOCK_STATEID_MUTEX = 1,                << 
104 };                                             << 
105                                                << 
106 /*                                                 90 /*
107  * A waitqueue for all in-progress 4.0 CLOSE o     91  * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
108  * the refcount on the open stateid to drop.       92  * the refcount on the open stateid to drop.
109  */                                                93  */
110 static DECLARE_WAIT_QUEUE_HEAD(close_wq);          94 static DECLARE_WAIT_QUEUE_HEAD(close_wq);
111                                                    95 
112 /*                                             << 
113  * A waitqueue where a writer to clients/#/ctl << 
114  * wait for cl_rpc_users to drop to 0 and then << 
115  * unhashed.                                   << 
116  */                                            << 
117 static DECLARE_WAIT_QUEUE_HEAD(expiry_wq);     << 
118                                                << 
119 static struct kmem_cache *client_slab;         << 
120 static struct kmem_cache *openowner_slab;          96 static struct kmem_cache *openowner_slab;
121 static struct kmem_cache *lockowner_slab;          97 static struct kmem_cache *lockowner_slab;
122 static struct kmem_cache *file_slab;               98 static struct kmem_cache *file_slab;
123 static struct kmem_cache *stateid_slab;            99 static struct kmem_cache *stateid_slab;
124 static struct kmem_cache *deleg_slab;             100 static struct kmem_cache *deleg_slab;
125 static struct kmem_cache *odstate_slab;           101 static struct kmem_cache *odstate_slab;
126                                                   102 
127 static void free_session(struct nfsd4_session     103 static void free_session(struct nfsd4_session *);
128                                                   104 
129 static const struct nfsd4_callback_ops nfsd4_c    105 static const struct nfsd4_callback_ops nfsd4_cb_recall_ops;
130 static const struct nfsd4_callback_ops nfsd4_c    106 static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops;
131 static const struct nfsd4_callback_ops nfsd4_c << 
132                                                << 
133 static struct workqueue_struct *laundry_wq;    << 
134                                                << 
135 int nfsd4_create_laundry_wq(void)              << 
136 {                                              << 
137         int rc = 0;                            << 
138                                                << 
139         laundry_wq = alloc_workqueue("%s", WQ_ << 
140         if (laundry_wq == NULL)                << 
141                 rc = -ENOMEM;                  << 
142         return rc;                             << 
143 }                                              << 
144                                                << 
145 void nfsd4_destroy_laundry_wq(void)            << 
146 {                                              << 
147         destroy_workqueue(laundry_wq);         << 
148 }                                              << 
149                                                   107 
150 static bool is_session_dead(struct nfsd4_sessi    108 static bool is_session_dead(struct nfsd4_session *ses)
151 {                                                 109 {
152         return ses->se_flags & NFS4_SESSION_DE    110         return ses->se_flags & NFS4_SESSION_DEAD;
153 }                                                 111 }
154                                                   112 
155 static __be32 mark_session_dead_locked(struct     113 static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
156 {                                                 114 {
157         if (atomic_read(&ses->se_ref) > ref_he    115         if (atomic_read(&ses->se_ref) > ref_held_by_me)
158                 return nfserr_jukebox;            116                 return nfserr_jukebox;
159         ses->se_flags |= NFS4_SESSION_DEAD;       117         ses->se_flags |= NFS4_SESSION_DEAD;
160         return nfs_ok;                            118         return nfs_ok;
161 }                                                 119 }
162                                                   120 
163 static bool is_client_expired(struct nfs4_clie    121 static bool is_client_expired(struct nfs4_client *clp)
164 {                                                 122 {
165         return clp->cl_time == 0;                 123         return clp->cl_time == 0;
166 }                                                 124 }
167                                                   125 
168 static void nfsd4_dec_courtesy_client_count(st << 
169                                         struct << 
170 {                                              << 
171         if (clp->cl_state != NFSD4_ACTIVE)     << 
172                 atomic_add_unless(&nn->nfsd_co << 
173 }                                              << 
174                                                << 
175 static __be32 get_client_locked(struct nfs4_cl    126 static __be32 get_client_locked(struct nfs4_client *clp)
176 {                                                 127 {
177         struct nfsd_net *nn = net_generic(clp-    128         struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
178                                                   129 
179         lockdep_assert_held(&nn->client_lock);    130         lockdep_assert_held(&nn->client_lock);
180                                                   131 
181         if (is_client_expired(clp))               132         if (is_client_expired(clp))
182                 return nfserr_expired;            133                 return nfserr_expired;
183         atomic_inc(&clp->cl_rpc_users);        !! 134         atomic_inc(&clp->cl_refcount);
184         nfsd4_dec_courtesy_client_count(nn, cl << 
185         clp->cl_state = NFSD4_ACTIVE;          << 
186         return nfs_ok;                            135         return nfs_ok;
187 }                                                 136 }
188                                                   137 
189 /* must be called under the client_lock */        138 /* must be called under the client_lock */
190 static inline void                                139 static inline void
191 renew_client_locked(struct nfs4_client *clp)      140 renew_client_locked(struct nfs4_client *clp)
192 {                                                 141 {
193         struct nfsd_net *nn = net_generic(clp-    142         struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
194                                                   143 
195         if (is_client_expired(clp)) {             144         if (is_client_expired(clp)) {
196                 WARN_ON(1);                       145                 WARN_ON(1);
197                 printk("%s: client (clientid %    146                 printk("%s: client (clientid %08x/%08x) already expired\n",
198                         __func__,                 147                         __func__,
199                         clp->cl_clientid.cl_bo    148                         clp->cl_clientid.cl_boot,
200                         clp->cl_clientid.cl_id    149                         clp->cl_clientid.cl_id);
201                 return;                           150                 return;
202         }                                         151         }
203                                                   152 
                                                   >> 153         dprintk("renewing client (clientid %08x/%08x)\n",
                                                   >> 154                         clp->cl_clientid.cl_boot,
                                                   >> 155                         clp->cl_clientid.cl_id);
204         list_move_tail(&clp->cl_lru, &nn->clie    156         list_move_tail(&clp->cl_lru, &nn->client_lru);
205         clp->cl_time = ktime_get_boottime_seco !! 157         clp->cl_time = get_seconds();
206         nfsd4_dec_courtesy_client_count(nn, cl << 
207         clp->cl_state = NFSD4_ACTIVE;          << 
208 }                                                 158 }
209                                                   159 
210 static void put_client_renew_locked(struct nfs    160 static void put_client_renew_locked(struct nfs4_client *clp)
211 {                                                 161 {
212         struct nfsd_net *nn = net_generic(clp-    162         struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
213                                                   163 
214         lockdep_assert_held(&nn->client_lock);    164         lockdep_assert_held(&nn->client_lock);
215                                                   165 
216         if (!atomic_dec_and_test(&clp->cl_rpc_ !! 166         if (!atomic_dec_and_test(&clp->cl_refcount))
217                 return;                           167                 return;
218         if (!is_client_expired(clp))              168         if (!is_client_expired(clp))
219                 renew_client_locked(clp);         169                 renew_client_locked(clp);
220         else                                   << 
221                 wake_up_all(&expiry_wq);       << 
222 }                                                 170 }
223                                                   171 
224 static void put_client_renew(struct nfs4_clien    172 static void put_client_renew(struct nfs4_client *clp)
225 {                                                 173 {
226         struct nfsd_net *nn = net_generic(clp-    174         struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
227                                                   175 
228         if (!atomic_dec_and_lock(&clp->cl_rpc_ !! 176         if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
229                 return;                           177                 return;
230         if (!is_client_expired(clp))              178         if (!is_client_expired(clp))
231                 renew_client_locked(clp);         179                 renew_client_locked(clp);
232         else                                   << 
233                 wake_up_all(&expiry_wq);       << 
234         spin_unlock(&nn->client_lock);            180         spin_unlock(&nn->client_lock);
235 }                                                 181 }
236                                                   182 
237 static __be32 nfsd4_get_session_locked(struct     183 static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
238 {                                                 184 {
239         __be32 status;                            185         __be32 status;
240                                                   186 
241         if (is_session_dead(ses))                 187         if (is_session_dead(ses))
242                 return nfserr_badsession;         188                 return nfserr_badsession;
243         status = get_client_locked(ses->se_cli    189         status = get_client_locked(ses->se_client);
244         if (status)                               190         if (status)
245                 return status;                    191                 return status;
246         atomic_inc(&ses->se_ref);                 192         atomic_inc(&ses->se_ref);
247         return nfs_ok;                            193         return nfs_ok;
248 }                                                 194 }
249                                                   195 
250 static void nfsd4_put_session_locked(struct nf    196 static void nfsd4_put_session_locked(struct nfsd4_session *ses)
251 {                                                 197 {
252         struct nfs4_client *clp = ses->se_clie    198         struct nfs4_client *clp = ses->se_client;
253         struct nfsd_net *nn = net_generic(clp-    199         struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
254                                                   200 
255         lockdep_assert_held(&nn->client_lock);    201         lockdep_assert_held(&nn->client_lock);
256                                                   202 
257         if (atomic_dec_and_test(&ses->se_ref)     203         if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
258                 free_session(ses);                204                 free_session(ses);
259         put_client_renew_locked(clp);             205         put_client_renew_locked(clp);
260 }                                                 206 }
261                                                   207 
262 static void nfsd4_put_session(struct nfsd4_ses    208 static void nfsd4_put_session(struct nfsd4_session *ses)
263 {                                                 209 {
264         struct nfs4_client *clp = ses->se_clie    210         struct nfs4_client *clp = ses->se_client;
265         struct nfsd_net *nn = net_generic(clp-    211         struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
266                                                   212 
267         spin_lock(&nn->client_lock);              213         spin_lock(&nn->client_lock);
268         nfsd4_put_session_locked(ses);            214         nfsd4_put_session_locked(ses);
269         spin_unlock(&nn->client_lock);            215         spin_unlock(&nn->client_lock);
270 }                                                 216 }
271                                                   217 
272 static struct nfsd4_blocked_lock *                218 static struct nfsd4_blocked_lock *
273 find_blocked_lock(struct nfs4_lockowner *lo, s    219 find_blocked_lock(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
274                         struct nfsd_net *nn)      220                         struct nfsd_net *nn)
275 {                                                 221 {
276         struct nfsd4_blocked_lock *cur, *found    222         struct nfsd4_blocked_lock *cur, *found = NULL;
277                                                   223 
278         spin_lock(&nn->blocked_locks_lock);       224         spin_lock(&nn->blocked_locks_lock);
279         list_for_each_entry(cur, &lo->lo_block    225         list_for_each_entry(cur, &lo->lo_blocked, nbl_list) {
280                 if (fh_match(fh, &cur->nbl_fh)    226                 if (fh_match(fh, &cur->nbl_fh)) {
281                         list_del_init(&cur->nb    227                         list_del_init(&cur->nbl_list);
282                         WARN_ON(list_empty(&cu << 
283                         list_del_init(&cur->nb    228                         list_del_init(&cur->nbl_lru);
284                         found = cur;              229                         found = cur;
285                         break;                    230                         break;
286                 }                                 231                 }
287         }                                         232         }
288         spin_unlock(&nn->blocked_locks_lock);     233         spin_unlock(&nn->blocked_locks_lock);
289         if (found)                                234         if (found)
290                 locks_delete_block(&found->nbl !! 235                 posix_unblock_lock(&found->nbl_lock);
291         return found;                             236         return found;
292 }                                                 237 }
293                                                   238 
294 static struct nfsd4_blocked_lock *                239 static struct nfsd4_blocked_lock *
295 find_or_allocate_block(struct nfs4_lockowner *    240 find_or_allocate_block(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
296                         struct nfsd_net *nn)      241                         struct nfsd_net *nn)
297 {                                                 242 {
298         struct nfsd4_blocked_lock *nbl;           243         struct nfsd4_blocked_lock *nbl;
299                                                   244 
300         nbl = find_blocked_lock(lo, fh, nn);      245         nbl = find_blocked_lock(lo, fh, nn);
301         if (!nbl) {                               246         if (!nbl) {
302                 nbl = kmalloc(sizeof(*nbl), GF !! 247                 nbl= kmalloc(sizeof(*nbl), GFP_KERNEL);
303                 if (nbl) {                        248                 if (nbl) {
304                         INIT_LIST_HEAD(&nbl->n    249                         INIT_LIST_HEAD(&nbl->nbl_list);
305                         INIT_LIST_HEAD(&nbl->n    250                         INIT_LIST_HEAD(&nbl->nbl_lru);
306                         fh_copy_shallow(&nbl->    251                         fh_copy_shallow(&nbl->nbl_fh, fh);
307                         locks_init_lock(&nbl->    252                         locks_init_lock(&nbl->nbl_lock);
308                         kref_init(&nbl->nbl_kr << 
309                         nfsd4_init_cb(&nbl->nb    253                         nfsd4_init_cb(&nbl->nbl_cb, lo->lo_owner.so_client,
310                                         &nfsd4    254                                         &nfsd4_cb_notify_lock_ops,
311                                         NFSPRO    255                                         NFSPROC4_CLNT_CB_NOTIFY_LOCK);
312                 }                                 256                 }
313         }                                         257         }
314         return nbl;                               258         return nbl;
315 }                                                 259 }
316                                                   260 
317 static void                                       261 static void
318 free_nbl(struct kref *kref)                    !! 262 free_blocked_lock(struct nfsd4_blocked_lock *nbl)
319 {                                                 263 {
320         struct nfsd4_blocked_lock *nbl;        << 
321                                                << 
322         nbl = container_of(kref, struct nfsd4_ << 
323         locks_release_private(&nbl->nbl_lock);    264         locks_release_private(&nbl->nbl_lock);
324         kfree(nbl);                               265         kfree(nbl);
325 }                                                 266 }
326                                                   267 
327 static void                                       268 static void
328 free_blocked_lock(struct nfsd4_blocked_lock *n << 
329 {                                              << 
330         locks_delete_block(&nbl->nbl_lock);    << 
331         kref_put(&nbl->nbl_kref, free_nbl);    << 
332 }                                              << 
333                                                << 
334 static void                                    << 
335 remove_blocked_locks(struct nfs4_lockowner *lo    269 remove_blocked_locks(struct nfs4_lockowner *lo)
336 {                                                 270 {
337         struct nfs4_client *clp = lo->lo_owner    271         struct nfs4_client *clp = lo->lo_owner.so_client;
338         struct nfsd_net *nn = net_generic(clp-    272         struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
339         struct nfsd4_blocked_lock *nbl;           273         struct nfsd4_blocked_lock *nbl;
340         LIST_HEAD(reaplist);                      274         LIST_HEAD(reaplist);
341                                                   275 
342         /* Dequeue all blocked locks */           276         /* Dequeue all blocked locks */
343         spin_lock(&nn->blocked_locks_lock);       277         spin_lock(&nn->blocked_locks_lock);
344         while (!list_empty(&lo->lo_blocked)) {    278         while (!list_empty(&lo->lo_blocked)) {
345                 nbl = list_first_entry(&lo->lo    279                 nbl = list_first_entry(&lo->lo_blocked,
346                                         struct    280                                         struct nfsd4_blocked_lock,
347                                         nbl_li    281                                         nbl_list);
348                 list_del_init(&nbl->nbl_list);    282                 list_del_init(&nbl->nbl_list);
349                 WARN_ON(list_empty(&nbl->nbl_l << 
350                 list_move(&nbl->nbl_lru, &reap    283                 list_move(&nbl->nbl_lru, &reaplist);
351         }                                         284         }
352         spin_unlock(&nn->blocked_locks_lock);     285         spin_unlock(&nn->blocked_locks_lock);
353                                                   286 
354         /* Now free them */                       287         /* Now free them */
355         while (!list_empty(&reaplist)) {          288         while (!list_empty(&reaplist)) {
356                 nbl = list_first_entry(&reapli    289                 nbl = list_first_entry(&reaplist, struct nfsd4_blocked_lock,
357                                         nbl_lr    290                                         nbl_lru);
358                 list_del_init(&nbl->nbl_lru);     291                 list_del_init(&nbl->nbl_lru);
                                                   >> 292                 posix_unblock_lock(&nbl->nbl_lock);
359                 free_blocked_lock(nbl);           293                 free_blocked_lock(nbl);
360         }                                         294         }
361 }                                                 295 }
362                                                   296 
363 static void                                    << 
364 nfsd4_cb_notify_lock_prepare(struct nfsd4_call << 
365 {                                              << 
366         struct nfsd4_blocked_lock       *nbl = << 
367                                                << 
368         locks_delete_block(&nbl->nbl_lock);    << 
369 }                                              << 
370                                                << 
371 static int                                        297 static int
372 nfsd4_cb_notify_lock_done(struct nfsd4_callbac    298 nfsd4_cb_notify_lock_done(struct nfsd4_callback *cb, struct rpc_task *task)
373 {                                                 299 {
374         trace_nfsd_cb_notify_lock_done(&zero_s << 
375                                                << 
376         /*                                        300         /*
377          * Since this is just an optimization,    301          * Since this is just an optimization, we don't try very hard if it
378          * turns out not to succeed. We'll req    302          * turns out not to succeed. We'll requeue it on NFS4ERR_DELAY, and
379          * just quit trying on anything else.     303          * just quit trying on anything else.
380          */                                       304          */
381         switch (task->tk_status) {                305         switch (task->tk_status) {
382         case -NFS4ERR_DELAY:                      306         case -NFS4ERR_DELAY:
383                 rpc_delay(task, 1 * HZ);          307                 rpc_delay(task, 1 * HZ);
384                 return 0;                         308                 return 0;
385         default:                                  309         default:
386                 return 1;                         310                 return 1;
387         }                                         311         }
388 }                                                 312 }
389                                                   313 
390 static void                                       314 static void
391 nfsd4_cb_notify_lock_release(struct nfsd4_call    315 nfsd4_cb_notify_lock_release(struct nfsd4_callback *cb)
392 {                                                 316 {
393         struct nfsd4_blocked_lock       *nbl =    317         struct nfsd4_blocked_lock       *nbl = container_of(cb,
394                                                   318                                                 struct nfsd4_blocked_lock, nbl_cb);
395                                                   319 
396         free_blocked_lock(nbl);                   320         free_blocked_lock(nbl);
397 }                                                 321 }
398                                                   322 
399 static const struct nfsd4_callback_ops nfsd4_c    323 static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops = {
400         .prepare        = nfsd4_cb_notify_lock << 
401         .done           = nfsd4_cb_notify_lock    324         .done           = nfsd4_cb_notify_lock_done,
402         .release        = nfsd4_cb_notify_lock    325         .release        = nfsd4_cb_notify_lock_release,
403         .opcode         = OP_CB_NOTIFY_LOCK,   << 
404 };                                                326 };
405                                                   327 
406 /*                                             << 
407  * We store the NONE, READ, WRITE, and BOTH bi << 
408  * st_{access,deny}_bmap field of the stateid, << 
409  * only what share bits are currently in force << 
410  * combinations of share bits previous opens h << 
411  * to enforce the recommendation in            << 
412  * https://datatracker.ietf.org/doc/html/rfc75 << 
413  * the server return an error if the client at << 
414  * combination of share bits not explicable by << 
415  * previous opens.                             << 
416  *                                             << 
417  * This enforcement is arguably incomplete, si << 
418  * track of access/deny bit combinations; so,  << 
419  *                                             << 
420  *      OPEN allow read, deny write            << 
421  *      OPEN allow both, deny none             << 
422  *      DOWNGRADE allow read, deny none        << 
423  *                                             << 
424  * which we should reject.                     << 
425  *                                             << 
426  * But you could also argue that our current c << 
427  * since it only exists to return NFS4ERR_INVA << 
428  * behavior.                                   << 
429  */                                            << 
430 static unsigned int                            << 
431 bmap_to_share_mode(unsigned long bmap)         << 
432 {                                              << 
433         int i;                                 << 
434         unsigned int access = 0;               << 
435                                                << 
436         for (i = 1; i < 4; i++) {              << 
437                 if (test_bit(i, &bmap))        << 
438                         access |= i;           << 
439         }                                      << 
440         return access;                         << 
441 }                                              << 
442                                                << 
443 /* set share access for a given stateid */     << 
444 static inline void                             << 
445 set_access(u32 access, struct nfs4_ol_stateid  << 
446 {                                              << 
447         unsigned char mask = 1 << access;      << 
448                                                << 
449         WARN_ON_ONCE(access > NFS4_SHARE_ACCES << 
450         stp->st_access_bmap |= mask;           << 
451 }                                              << 
452                                                << 
453 /* clear share access for a given stateid */   << 
454 static inline void                             << 
455 clear_access(u32 access, struct nfs4_ol_statei << 
456 {                                              << 
457         unsigned char mask = 1 << access;      << 
458                                                << 
459         WARN_ON_ONCE(access > NFS4_SHARE_ACCES << 
460         stp->st_access_bmap &= ~mask;          << 
461 }                                              << 
462                                                << 
463 /* test whether a given stateid has access */  << 
464 static inline bool                             << 
465 test_access(u32 access, struct nfs4_ol_stateid << 
466 {                                              << 
467         unsigned char mask = 1 << access;      << 
468                                                << 
469         return (bool)(stp->st_access_bmap & ma << 
470 }                                              << 
471                                                << 
472 /* set share deny for a given stateid */       << 
473 static inline void                             << 
474 set_deny(u32 deny, struct nfs4_ol_stateid *stp << 
475 {                                              << 
476         unsigned char mask = 1 << deny;        << 
477                                                << 
478         WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BO << 
479         stp->st_deny_bmap |= mask;             << 
480 }                                              << 
481                                                << 
482 /* clear share deny for a given stateid */     << 
483 static inline void                             << 
484 clear_deny(u32 deny, struct nfs4_ol_stateid *s << 
485 {                                              << 
486         unsigned char mask = 1 << deny;        << 
487                                                << 
488         WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BO << 
489         stp->st_deny_bmap &= ~mask;            << 
490 }                                              << 
491                                                << 
492 /* test whether a given stateid is denying spe << 
493 static inline bool                             << 
494 test_deny(u32 deny, struct nfs4_ol_stateid *st << 
495 {                                              << 
496         unsigned char mask = 1 << deny;        << 
497                                                << 
498         return (bool)(stp->st_deny_bmap & mask << 
499 }                                              << 
500                                                << 
501 static int nfs4_access_to_omode(u32 access)    << 
502 {                                              << 
503         switch (access & NFS4_SHARE_ACCESS_BOT << 
504         case NFS4_SHARE_ACCESS_READ:           << 
505                 return O_RDONLY;               << 
506         case NFS4_SHARE_ACCESS_WRITE:          << 
507                 return O_WRONLY;               << 
508         case NFS4_SHARE_ACCESS_BOTH:           << 
509                 return O_RDWR;                 << 
510         }                                      << 
511         WARN_ON_ONCE(1);                       << 
512         return O_RDONLY;                       << 
513 }                                              << 
514                                                << 
515 static inline int                              << 
516 access_permit_read(struct nfs4_ol_stateid *stp << 
517 {                                              << 
518         return test_access(NFS4_SHARE_ACCESS_R << 
519                 test_access(NFS4_SHARE_ACCESS_ << 
520                 test_access(NFS4_SHARE_ACCESS_ << 
521 }                                              << 
522                                                << 
523 static inline int                              << 
524 access_permit_write(struct nfs4_ol_stateid *st << 
525 {                                              << 
526         return test_access(NFS4_SHARE_ACCESS_W << 
527                 test_access(NFS4_SHARE_ACCESS_ << 
528 }                                              << 
529                                                << 
530 static inline struct nfs4_stateowner *            328 static inline struct nfs4_stateowner *
531 nfs4_get_stateowner(struct nfs4_stateowner *so    329 nfs4_get_stateowner(struct nfs4_stateowner *sop)
532 {                                                 330 {
533         atomic_inc(&sop->so_count);               331         atomic_inc(&sop->so_count);
534         return sop;                               332         return sop;
535 }                                                 333 }
536                                                   334 
537 static int                                        335 static int
538 same_owner_str(struct nfs4_stateowner *sop, st    336 same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
539 {                                                 337 {
540         return (sop->so_owner.len == owner->le    338         return (sop->so_owner.len == owner->len) &&
541                 0 == memcmp(sop->so_owner.data    339                 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
542 }                                                 340 }
543                                                   341 
544 static struct nfs4_openowner *                    342 static struct nfs4_openowner *
545 find_openstateowner_str(unsigned int hashval,  !! 343 find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
546                         struct nfs4_client *cl    344                         struct nfs4_client *clp)
547 {                                                 345 {
548         struct nfs4_stateowner *so;               346         struct nfs4_stateowner *so;
549                                                   347 
550         lockdep_assert_held(&clp->cl_lock);       348         lockdep_assert_held(&clp->cl_lock);
551                                                   349 
552         list_for_each_entry(so, &clp->cl_owner    350         list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
553                             so_strhash) {         351                             so_strhash) {
554                 if (!so->so_is_open_owner)        352                 if (!so->so_is_open_owner)
555                         continue;                 353                         continue;
556                 if (same_owner_str(so, &open->    354                 if (same_owner_str(so, &open->op_owner))
557                         return openowner(nfs4_    355                         return openowner(nfs4_get_stateowner(so));
558         }                                         356         }
559         return NULL;                              357         return NULL;
560 }                                                 358 }
561                                                   359 
                                                   >> 360 static struct nfs4_openowner *
                                                   >> 361 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
                                                   >> 362                         struct nfs4_client *clp)
                                                   >> 363 {
                                                   >> 364         struct nfs4_openowner *oo;
                                                   >> 365 
                                                   >> 366         spin_lock(&clp->cl_lock);
                                                   >> 367         oo = find_openstateowner_str_locked(hashval, open, clp);
                                                   >> 368         spin_unlock(&clp->cl_lock);
                                                   >> 369         return oo;
                                                   >> 370 }
                                                   >> 371 
562 static inline u32                                 372 static inline u32
563 opaque_hashval(const void *ptr, int nbytes)       373 opaque_hashval(const void *ptr, int nbytes)
564 {                                                 374 {
565         unsigned char *cptr = (unsigned char *    375         unsigned char *cptr = (unsigned char *) ptr;
566                                                   376 
567         u32 x = 0;                                377         u32 x = 0;
568         while (nbytes--) {                        378         while (nbytes--) {
569                 x *= 37;                          379                 x *= 37;
570                 x += *cptr++;                     380                 x += *cptr++;
571         }                                         381         }
572         return x;                                 382         return x;
573 }                                                 383 }
574                                                   384 
575 static void nfsd4_free_file_rcu(struct rcu_hea    385 static void nfsd4_free_file_rcu(struct rcu_head *rcu)
576 {                                                 386 {
577         struct nfs4_file *fp = container_of(rc    387         struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
578                                                   388 
579         kmem_cache_free(file_slab, fp);           389         kmem_cache_free(file_slab, fp);
580 }                                                 390 }
581                                                   391 
582 void                                              392 void
583 put_nfs4_file(struct nfs4_file *fi)               393 put_nfs4_file(struct nfs4_file *fi)
584 {                                                 394 {
585         if (refcount_dec_and_test(&fi->fi_ref) !! 395         might_lock(&state_lock);
586                 nfsd4_file_hash_remove(fi);    !! 396 
                                                   >> 397         if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) {
                                                   >> 398                 hlist_del_rcu(&fi->fi_hash);
                                                   >> 399                 spin_unlock(&state_lock);
587                 WARN_ON_ONCE(!list_empty(&fi->    400                 WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
588                 WARN_ON_ONCE(!list_empty(&fi->    401                 WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
589                 call_rcu(&fi->fi_rcu, nfsd4_fr    402                 call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
590         }                                         403         }
591 }                                                 404 }
592                                                   405 
593 static struct nfsd_file *                      !! 406 static struct file *
                                                   >> 407 __nfs4_get_fd(struct nfs4_file *f, int oflag)
                                                   >> 408 {
                                                   >> 409         if (f->fi_fds[oflag])
                                                   >> 410                 return get_file(f->fi_fds[oflag]);
                                                   >> 411         return NULL;
                                                   >> 412 }
                                                   >> 413 
                                                   >> 414 static struct file *
594 find_writeable_file_locked(struct nfs4_file *f    415 find_writeable_file_locked(struct nfs4_file *f)
595 {                                                 416 {
596         struct nfsd_file *ret;                 !! 417         struct file *ret;
597                                                   418 
598         lockdep_assert_held(&f->fi_lock);         419         lockdep_assert_held(&f->fi_lock);
599                                                   420 
600         ret = nfsd_file_get(f->fi_fds[O_WRONLY !! 421         ret = __nfs4_get_fd(f, O_WRONLY);
601         if (!ret)                                 422         if (!ret)
602                 ret = nfsd_file_get(f->fi_fds[ !! 423                 ret = __nfs4_get_fd(f, O_RDWR);
603         return ret;                               424         return ret;
604 }                                                 425 }
605                                                   426 
606 static struct nfsd_file *                      !! 427 static struct file *
607 find_writeable_file(struct nfs4_file *f)          428 find_writeable_file(struct nfs4_file *f)
608 {                                                 429 {
609         struct nfsd_file *ret;                 !! 430         struct file *ret;
610                                                   431 
611         spin_lock(&f->fi_lock);                   432         spin_lock(&f->fi_lock);
612         ret = find_writeable_file_locked(f);      433         ret = find_writeable_file_locked(f);
613         spin_unlock(&f->fi_lock);                 434         spin_unlock(&f->fi_lock);
614                                                   435 
615         return ret;                               436         return ret;
616 }                                                 437 }
617                                                   438 
618 static struct nfsd_file *                      !! 439 static struct file *find_readable_file_locked(struct nfs4_file *f)
619 find_readable_file_locked(struct nfs4_file *f) << 
620 {                                                 440 {
621         struct nfsd_file *ret;                 !! 441         struct file *ret;
622                                                   442 
623         lockdep_assert_held(&f->fi_lock);         443         lockdep_assert_held(&f->fi_lock);
624                                                   444 
625         ret = nfsd_file_get(f->fi_fds[O_RDONLY !! 445         ret = __nfs4_get_fd(f, O_RDONLY);
626         if (!ret)                                 446         if (!ret)
627                 ret = nfsd_file_get(f->fi_fds[ !! 447                 ret = __nfs4_get_fd(f, O_RDWR);
628         return ret;                               448         return ret;
629 }                                                 449 }
630                                                   450 
631 static struct nfsd_file *                      !! 451 static struct file *
632 find_readable_file(struct nfs4_file *f)           452 find_readable_file(struct nfs4_file *f)
633 {                                                 453 {
634         struct nfsd_file *ret;                 !! 454         struct file *ret;
635                                                   455 
636         spin_lock(&f->fi_lock);                   456         spin_lock(&f->fi_lock);
637         ret = find_readable_file_locked(f);       457         ret = find_readable_file_locked(f);
638         spin_unlock(&f->fi_lock);                 458         spin_unlock(&f->fi_lock);
639                                                   459 
640         return ret;                               460         return ret;
641 }                                                 461 }
642                                                   462 
643 static struct nfsd_file *                      !! 463 struct file *
644 find_rw_file(struct nfs4_file *f)              << 
645 {                                              << 
646         struct nfsd_file *ret;                 << 
647                                                << 
648         spin_lock(&f->fi_lock);                << 
649         ret = nfsd_file_get(f->fi_fds[O_RDWR]) << 
650         spin_unlock(&f->fi_lock);              << 
651                                                << 
652         return ret;                            << 
653 }                                              << 
654                                                << 
655 struct nfsd_file *                             << 
656 find_any_file(struct nfs4_file *f)                464 find_any_file(struct nfs4_file *f)
657 {                                                 465 {
658         struct nfsd_file *ret;                 !! 466         struct file *ret;
659                                                   467 
660         if (!f)                                << 
661                 return NULL;                   << 
662         spin_lock(&f->fi_lock);                   468         spin_lock(&f->fi_lock);
663         ret = nfsd_file_get(f->fi_fds[O_RDWR]) !! 469         ret = __nfs4_get_fd(f, O_RDWR);
664         if (!ret) {                               470         if (!ret) {
665                 ret = nfsd_file_get(f->fi_fds[ !! 471                 ret = __nfs4_get_fd(f, O_WRONLY);
666                 if (!ret)                         472                 if (!ret)
667                         ret = nfsd_file_get(f- !! 473                         ret = __nfs4_get_fd(f, O_RDONLY);
668         }                                         474         }
669         spin_unlock(&f->fi_lock);                 475         spin_unlock(&f->fi_lock);
670         return ret;                               476         return ret;
671 }                                                 477 }
672                                                   478 
673 static struct nfsd_file *find_any_file_locked( << 
674 {                                              << 
675         lockdep_assert_held(&f->fi_lock);      << 
676                                                << 
677         if (f->fi_fds[O_RDWR])                 << 
678                 return f->fi_fds[O_RDWR];      << 
679         if (f->fi_fds[O_WRONLY])               << 
680                 return f->fi_fds[O_WRONLY];    << 
681         if (f->fi_fds[O_RDONLY])               << 
682                 return f->fi_fds[O_RDONLY];    << 
683         return NULL;                           << 
684 }                                              << 
685                                                << 
686 static atomic_long_t num_delegations;             479 static atomic_long_t num_delegations;
687 unsigned long max_delegations;                    480 unsigned long max_delegations;
688                                                   481 
689 /*                                                482 /*
690  * Open owner state (share locks)                 483  * Open owner state (share locks)
691  */                                               484  */
692                                                   485 
693 /* hash tables for lock and open owners */        486 /* hash tables for lock and open owners */
694 #define OWNER_HASH_BITS              8            487 #define OWNER_HASH_BITS              8
695 #define OWNER_HASH_SIZE             (1 << OWNE    488 #define OWNER_HASH_SIZE             (1 << OWNER_HASH_BITS)
696 #define OWNER_HASH_MASK             (OWNER_HAS    489 #define OWNER_HASH_MASK             (OWNER_HASH_SIZE - 1)
697                                                   490 
698 static unsigned int ownerstr_hashval(struct xd    491 static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
699 {                                                 492 {
700         unsigned int ret;                         493         unsigned int ret;
701                                                   494 
702         ret = opaque_hashval(ownername->data,     495         ret = opaque_hashval(ownername->data, ownername->len);
703         return ret & OWNER_HASH_MASK;             496         return ret & OWNER_HASH_MASK;
704 }                                                 497 }
705                                                   498 
706 static struct rhltable nfs4_file_rhltable ____ !! 499 /* hash table for nfs4_file */
707                                                !! 500 #define FILE_HASH_BITS                   8
708 static const struct rhashtable_params nfs4_fil !! 501 #define FILE_HASH_SIZE                  (1 << FILE_HASH_BITS)
709         .key_len                = sizeof_field << 
710         .key_offset             = offsetof(str << 
711         .head_offset            = offsetof(str << 
712                                                   502 
713         /*                                     !! 503 static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
714          * Start with a single page hash table << 
715          * on light workloads.                 << 
716          */                                    << 
717         .min_size               = 256,         << 
718         .automatic_shrinking    = true,        << 
719 };                                             << 
720                                                << 
721 /*                                             << 
722  * Check if courtesy clients have conflicting  << 
723  *                                             << 
724  * access:  is op_share_access if share_access << 
725  *          Check if access mode, op_share_acc << 
726  *          the current deny mode of the file  << 
727  * access:  is op_share_deny if share_access i << 
728  *          Check if the deny mode, op_share_d << 
729  *          current access of the file 'fp'.   << 
730  * stp:     skip checking this entry.          << 
731  * new_stp: normal open, not open upgrade.     << 
732  *                                             << 
733  * Function returns:                           << 
734  *      false - access/deny mode conflict with << 
735  *      true  - no conflict or conflict with c << 
736  */                                            << 
737 static bool                                    << 
738 nfs4_resolve_deny_conflicts_locked(struct nfs4 << 
739                 struct nfs4_ol_stateid *stp, u << 
740 {                                                 504 {
741         struct nfs4_ol_stateid *st;            !! 505         return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
742         bool resolvable = true;                !! 506 }
743         unsigned char bmap;                    << 
744         struct nfsd_net *nn;                   << 
745         struct nfs4_client *clp;               << 
746                                                   507 
747         lockdep_assert_held(&fp->fi_lock);     !! 508 static unsigned int file_hashval(struct knfsd_fh *fh)
748         list_for_each_entry(st, &fp->fi_statei !! 509 {
749                 /* ignore lock stateid */      !! 510         return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
750                 if (st->st_openstp)            << 
751                         continue;              << 
752                 if (st == stp && new_stp)      << 
753                         continue;              << 
754                 /* check file access against d << 
755                 bmap = share_access ? st->st_d << 
756                 if (!(access & bmap_to_share_m << 
757                         continue;              << 
758                 clp = st->st_stid.sc_client;   << 
759                 if (try_to_expire_client(clp)) << 
760                         continue;              << 
761                 resolvable = false;            << 
762                 break;                         << 
763         }                                      << 
764         if (resolvable) {                      << 
765                 clp = stp->st_stid.sc_client;  << 
766                 nn = net_generic(clp->net, nfs << 
767                 mod_delayed_work(laundry_wq, & << 
768         }                                      << 
769         return resolvable;                     << 
770 }                                                 511 }
771                                                   512 
                                                   >> 513 static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
                                                   >> 514 
772 static void                                       515 static void
773 __nfs4_file_get_access(struct nfs4_file *fp, u    516 __nfs4_file_get_access(struct nfs4_file *fp, u32 access)
774 {                                                 517 {
775         lockdep_assert_held(&fp->fi_lock);        518         lockdep_assert_held(&fp->fi_lock);
776                                                   519 
777         if (access & NFS4_SHARE_ACCESS_WRITE)     520         if (access & NFS4_SHARE_ACCESS_WRITE)
778                 atomic_inc(&fp->fi_access[O_WR    521                 atomic_inc(&fp->fi_access[O_WRONLY]);
779         if (access & NFS4_SHARE_ACCESS_READ)      522         if (access & NFS4_SHARE_ACCESS_READ)
780                 atomic_inc(&fp->fi_access[O_RD    523                 atomic_inc(&fp->fi_access[O_RDONLY]);
781 }                                                 524 }
782                                                   525 
783 static __be32                                     526 static __be32
784 nfs4_file_get_access(struct nfs4_file *fp, u32    527 nfs4_file_get_access(struct nfs4_file *fp, u32 access)
785 {                                                 528 {
786         lockdep_assert_held(&fp->fi_lock);        529         lockdep_assert_held(&fp->fi_lock);
787                                                   530 
788         /* Does this access mode make sense? *    531         /* Does this access mode make sense? */
789         if (access & ~NFS4_SHARE_ACCESS_BOTH)     532         if (access & ~NFS4_SHARE_ACCESS_BOTH)
790                 return nfserr_inval;              533                 return nfserr_inval;
791                                                   534 
792         /* Does it conflict with a deny mode a    535         /* Does it conflict with a deny mode already set? */
793         if ((access & fp->fi_share_deny) != 0)    536         if ((access & fp->fi_share_deny) != 0)
794                 return nfserr_share_denied;       537                 return nfserr_share_denied;
795                                                   538 
796         __nfs4_file_get_access(fp, access);       539         __nfs4_file_get_access(fp, access);
797         return nfs_ok;                            540         return nfs_ok;
798 }                                                 541 }
799                                                   542 
800 static __be32 nfs4_file_check_deny(struct nfs4    543 static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
801 {                                                 544 {
802         /* Common case is that there is no den    545         /* Common case is that there is no deny mode. */
803         if (deny) {                               546         if (deny) {
804                 /* Does this deny mode make se    547                 /* Does this deny mode make sense? */
805                 if (deny & ~NFS4_SHARE_DENY_BO    548                 if (deny & ~NFS4_SHARE_DENY_BOTH)
806                         return nfserr_inval;      549                         return nfserr_inval;
807                                                   550 
808                 if ((deny & NFS4_SHARE_DENY_RE    551                 if ((deny & NFS4_SHARE_DENY_READ) &&
809                     atomic_read(&fp->fi_access    552                     atomic_read(&fp->fi_access[O_RDONLY]))
810                         return nfserr_share_de    553                         return nfserr_share_denied;
811                                                   554 
812                 if ((deny & NFS4_SHARE_DENY_WR    555                 if ((deny & NFS4_SHARE_DENY_WRITE) &&
813                     atomic_read(&fp->fi_access    556                     atomic_read(&fp->fi_access[O_WRONLY]))
814                         return nfserr_share_de    557                         return nfserr_share_denied;
815         }                                         558         }
816         return nfs_ok;                            559         return nfs_ok;
817 }                                                 560 }
818                                                   561 
819 static void __nfs4_file_put_access(struct nfs4    562 static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
820 {                                                 563 {
821         might_lock(&fp->fi_lock);                 564         might_lock(&fp->fi_lock);
822                                                   565 
823         if (atomic_dec_and_lock(&fp->fi_access    566         if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
824                 struct nfsd_file *f1 = NULL;   !! 567                 struct file *f1 = NULL;
825                 struct nfsd_file *f2 = NULL;   !! 568                 struct file *f2 = NULL;
826                                                   569 
827                 swap(f1, fp->fi_fds[oflag]);      570                 swap(f1, fp->fi_fds[oflag]);
828                 if (atomic_read(&fp->fi_access    571                 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
829                         swap(f2, fp->fi_fds[O_    572                         swap(f2, fp->fi_fds[O_RDWR]);
830                 spin_unlock(&fp->fi_lock);        573                 spin_unlock(&fp->fi_lock);
831                 if (f1)                           574                 if (f1)
832                         nfsd_file_put(f1);     !! 575                         fput(f1);
833                 if (f2)                           576                 if (f2)
834                         nfsd_file_put(f2);     !! 577                         fput(f2);
835         }                                         578         }
836 }                                                 579 }
837                                                   580 
838 static void nfs4_file_put_access(struct nfs4_f    581 static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
839 {                                                 582 {
840         WARN_ON_ONCE(access & ~NFS4_SHARE_ACCE    583         WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
841                                                   584 
842         if (access & NFS4_SHARE_ACCESS_WRITE)     585         if (access & NFS4_SHARE_ACCESS_WRITE)
843                 __nfs4_file_put_access(fp, O_W    586                 __nfs4_file_put_access(fp, O_WRONLY);
844         if (access & NFS4_SHARE_ACCESS_READ)      587         if (access & NFS4_SHARE_ACCESS_READ)
845                 __nfs4_file_put_access(fp, O_R    588                 __nfs4_file_put_access(fp, O_RDONLY);
846 }                                                 589 }
847                                                   590 
848 /*                                                591 /*
849  * Allocate a new open/delegation state counte    592  * Allocate a new open/delegation state counter. This is needed for
850  * pNFS for proper return on close semantics.     593  * pNFS for proper return on close semantics.
851  *                                                594  *
852  * Note that we only allocate it for pNFS-enab    595  * Note that we only allocate it for pNFS-enabled exports, otherwise
853  * all pointers to struct nfs4_clnt_odstate ar    596  * all pointers to struct nfs4_clnt_odstate are always NULL.
854  */                                               597  */
855 static struct nfs4_clnt_odstate *                 598 static struct nfs4_clnt_odstate *
856 alloc_clnt_odstate(struct nfs4_client *clp)       599 alloc_clnt_odstate(struct nfs4_client *clp)
857 {                                                 600 {
858         struct nfs4_clnt_odstate *co;             601         struct nfs4_clnt_odstate *co;
859                                                   602 
860         co = kmem_cache_zalloc(odstate_slab, G    603         co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
861         if (co) {                                 604         if (co) {
862                 co->co_client = clp;              605                 co->co_client = clp;
863                 refcount_set(&co->co_odcount,  !! 606                 atomic_set(&co->co_odcount, 1);
864         }                                         607         }
865         return co;                                608         return co;
866 }                                                 609 }
867                                                   610 
868 static void                                       611 static void
869 hash_clnt_odstate_locked(struct nfs4_clnt_odst    612 hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
870 {                                                 613 {
871         struct nfs4_file *fp = co->co_file;       614         struct nfs4_file *fp = co->co_file;
872                                                   615 
873         lockdep_assert_held(&fp->fi_lock);        616         lockdep_assert_held(&fp->fi_lock);
874         list_add(&co->co_perfile, &fp->fi_clnt    617         list_add(&co->co_perfile, &fp->fi_clnt_odstate);
875 }                                                 618 }
876                                                   619 
877 static inline void                                620 static inline void
878 get_clnt_odstate(struct nfs4_clnt_odstate *co)    621 get_clnt_odstate(struct nfs4_clnt_odstate *co)
879 {                                                 622 {
880         if (co)                                   623         if (co)
881                 refcount_inc(&co->co_odcount); !! 624                 atomic_inc(&co->co_odcount);
882 }                                                 625 }
883                                                   626 
884 static void                                       627 static void
885 put_clnt_odstate(struct nfs4_clnt_odstate *co)    628 put_clnt_odstate(struct nfs4_clnt_odstate *co)
886 {                                                 629 {
887         struct nfs4_file *fp;                     630         struct nfs4_file *fp;
888                                                   631 
889         if (!co)                                  632         if (!co)
890                 return;                           633                 return;
891                                                   634 
892         fp = co->co_file;                         635         fp = co->co_file;
893         if (refcount_dec_and_lock(&co->co_odco !! 636         if (atomic_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
894                 list_del(&co->co_perfile);        637                 list_del(&co->co_perfile);
895                 spin_unlock(&fp->fi_lock);        638                 spin_unlock(&fp->fi_lock);
896                                                   639 
897                 nfsd4_return_all_file_layouts(    640                 nfsd4_return_all_file_layouts(co->co_client, fp);
898                 kmem_cache_free(odstate_slab,     641                 kmem_cache_free(odstate_slab, co);
899         }                                         642         }
900 }                                                 643 }
901                                                   644 
902 static struct nfs4_clnt_odstate *                 645 static struct nfs4_clnt_odstate *
903 find_or_hash_clnt_odstate(struct nfs4_file *fp    646 find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
904 {                                                 647 {
905         struct nfs4_clnt_odstate *co;             648         struct nfs4_clnt_odstate *co;
906         struct nfs4_client *cl;                   649         struct nfs4_client *cl;
907                                                   650 
908         if (!new)                                 651         if (!new)
909                 return NULL;                      652                 return NULL;
910                                                   653 
911         cl = new->co_client;                      654         cl = new->co_client;
912                                                   655 
913         spin_lock(&fp->fi_lock);                  656         spin_lock(&fp->fi_lock);
914         list_for_each_entry(co, &fp->fi_clnt_o    657         list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
915                 if (co->co_client == cl) {        658                 if (co->co_client == cl) {
916                         get_clnt_odstate(co);     659                         get_clnt_odstate(co);
917                         goto out;                 660                         goto out;
918                 }                                 661                 }
919         }                                         662         }
920         co = new;                                 663         co = new;
921         co->co_file = fp;                         664         co->co_file = fp;
922         hash_clnt_odstate_locked(new);            665         hash_clnt_odstate_locked(new);
923 out:                                              666 out:
924         spin_unlock(&fp->fi_lock);                667         spin_unlock(&fp->fi_lock);
925         return co;                                668         return co;
926 }                                                 669 }
927                                                   670 
928 struct nfs4_stid *nfs4_alloc_stid(struct nfs4_    671 struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab,
929                                   void (*sc_fr    672                                   void (*sc_free)(struct nfs4_stid *))
930 {                                                 673 {
931         struct nfs4_stid *stid;                   674         struct nfs4_stid *stid;
932         int new_id;                               675         int new_id;
933                                                   676 
934         stid = kmem_cache_zalloc(slab, GFP_KER    677         stid = kmem_cache_zalloc(slab, GFP_KERNEL);
935         if (!stid)                                678         if (!stid)
936                 return NULL;                      679                 return NULL;
937                                                   680 
938         idr_preload(GFP_KERNEL);                  681         idr_preload(GFP_KERNEL);
939         spin_lock(&cl->cl_lock);                  682         spin_lock(&cl->cl_lock);
940         /* Reserving 0 for start of file in nf !! 683         new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 0, 0, GFP_NOWAIT);
941         new_id = idr_alloc_cyclic(&cl->cl_stat << 
942         spin_unlock(&cl->cl_lock);                684         spin_unlock(&cl->cl_lock);
943         idr_preload_end();                        685         idr_preload_end();
944         if (new_id < 0)                           686         if (new_id < 0)
945                 goto out_free;                    687                 goto out_free;
946                                                   688 
947         stid->sc_free = sc_free;                  689         stid->sc_free = sc_free;
948         stid->sc_client = cl;                     690         stid->sc_client = cl;
949         stid->sc_stateid.si_opaque.so_id = new    691         stid->sc_stateid.si_opaque.so_id = new_id;
950         stid->sc_stateid.si_opaque.so_clid = c    692         stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
951         /* Will be incremented before return t    693         /* Will be incremented before return to client: */
952         refcount_set(&stid->sc_count, 1);      !! 694         atomic_set(&stid->sc_count, 1);
953         spin_lock_init(&stid->sc_lock);           695         spin_lock_init(&stid->sc_lock);
954         INIT_LIST_HEAD(&stid->sc_cp_list);     << 
955                                                   696 
956         /*                                        697         /*
957          * It shouldn't be a problem to reuse     698          * It shouldn't be a problem to reuse an opaque stateid value.
958          * I don't think it is for 4.1.  But w    699          * I don't think it is for 4.1.  But with 4.0 I worry that, for
959          * example, a stray write retransmissi    700          * example, a stray write retransmission could be accepted by
960          * the server when it should have been    701          * the server when it should have been rejected.  Therefore,
961          * adopt a trick from the sctp code to    702          * adopt a trick from the sctp code to attempt to maximize the
962          * amount of time until an id is reuse    703          * amount of time until an id is reused, by ensuring they always
963          * "increase" (mod INT_MAX):              704          * "increase" (mod INT_MAX):
964          */                                       705          */
965         return stid;                              706         return stid;
966 out_free:                                         707 out_free:
967         kmem_cache_free(slab, stid);              708         kmem_cache_free(slab, stid);
968         return NULL;                              709         return NULL;
969 }                                                 710 }
970                                                   711 
971 /*                                             << 
972  * Create a unique stateid_t to represent each << 
973  */                                            << 
974 static int nfs4_init_cp_state(struct nfsd_net  << 
975                               unsigned char cs << 
976 {                                              << 
977         int new_id;                            << 
978                                                << 
979         stid->cs_stid.si_opaque.so_clid.cl_boo << 
980         stid->cs_stid.si_opaque.so_clid.cl_id  << 
981                                                << 
982         idr_preload(GFP_KERNEL);               << 
983         spin_lock(&nn->s2s_cp_lock);           << 
984         new_id = idr_alloc_cyclic(&nn->s2s_cp_ << 
985         stid->cs_stid.si_opaque.so_id = new_id << 
986         stid->cs_stid.si_generation = 1;       << 
987         spin_unlock(&nn->s2s_cp_lock);         << 
988         idr_preload_end();                     << 
989         if (new_id < 0)                        << 
990                 return 0;                      << 
991         stid->cs_type = cs_type;               << 
992         return 1;                              << 
993 }                                              << 
994                                                << 
995 int nfs4_init_copy_state(struct nfsd_net *nn,  << 
996 {                                              << 
997         return nfs4_init_cp_state(nn, &copy->c << 
998 }                                              << 
999                                                << 
1000 struct nfs4_cpntf_state *nfs4_alloc_init_cpnt << 
1001                                               << 
1002 {                                             << 
1003         struct nfs4_cpntf_state *cps;         << 
1004                                               << 
1005         cps = kzalloc(sizeof(struct nfs4_cpnt << 
1006         if (!cps)                             << 
1007                 return NULL;                  << 
1008         cps->cpntf_time = ktime_get_boottime_ << 
1009         refcount_set(&cps->cp_stateid.cs_coun << 
1010         if (!nfs4_init_cp_state(nn, &cps->cp_ << 
1011                 goto out_free;                << 
1012         spin_lock(&nn->s2s_cp_lock);          << 
1013         list_add(&cps->cp_list, &p_stid->sc_c << 
1014         spin_unlock(&nn->s2s_cp_lock);        << 
1015         return cps;                           << 
1016 out_free:                                     << 
1017         kfree(cps);                           << 
1018         return NULL;                          << 
1019 }                                             << 
1020                                               << 
1021 void nfs4_free_copy_state(struct nfsd4_copy * << 
1022 {                                             << 
1023         struct nfsd_net *nn;                  << 
1024                                               << 
1025         if (copy->cp_stateid.cs_type != NFS4_ << 
1026                 return;                       << 
1027         nn = net_generic(copy->cp_clp->net, n << 
1028         spin_lock(&nn->s2s_cp_lock);          << 
1029         idr_remove(&nn->s2s_cp_stateids,      << 
1030                    copy->cp_stateid.cs_stid.s << 
1031         spin_unlock(&nn->s2s_cp_lock);        << 
1032 }                                             << 
1033                                               << 
1034 static void nfs4_free_cpntf_statelist(struct  << 
1035 {                                             << 
1036         struct nfs4_cpntf_state *cps;         << 
1037         struct nfsd_net *nn;                  << 
1038                                               << 
1039         nn = net_generic(net, nfsd_net_id);   << 
1040         spin_lock(&nn->s2s_cp_lock);          << 
1041         while (!list_empty(&stid->sc_cp_list) << 
1042                 cps = list_first_entry(&stid- << 
1043                                        struct << 
1044                 _free_cpntf_state_locked(nn,  << 
1045         }                                     << 
1046         spin_unlock(&nn->s2s_cp_lock);        << 
1047 }                                             << 
1048                                               << 
1049 static struct nfs4_ol_stateid * nfs4_alloc_op    712 static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
1050 {                                                713 {
1051         struct nfs4_stid *stid;                  714         struct nfs4_stid *stid;
1052                                                  715 
1053         stid = nfs4_alloc_stid(clp, stateid_s    716         stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid);
1054         if (!stid)                               717         if (!stid)
1055                 return NULL;                     718                 return NULL;
1056                                                  719 
1057         return openlockstateid(stid);            720         return openlockstateid(stid);
1058 }                                                721 }
1059                                                  722 
1060 static void nfs4_free_deleg(struct nfs4_stid     723 static void nfs4_free_deleg(struct nfs4_stid *stid)
1061 {                                                724 {
1062         struct nfs4_delegation *dp = delegsta << 
1063                                               << 
1064         WARN_ON_ONCE(!list_empty(&stid->sc_cp << 
1065         WARN_ON_ONCE(!list_empty(&dp->dl_perf << 
1066         WARN_ON_ONCE(!list_empty(&dp->dl_perc << 
1067         WARN_ON_ONCE(!list_empty(&dp->dl_reca << 
1068         kmem_cache_free(deleg_slab, stid);       725         kmem_cache_free(deleg_slab, stid);
1069         atomic_long_dec(&num_delegations);       726         atomic_long_dec(&num_delegations);
1070 }                                                727 }
1071                                                  728 
1072 /*                                               729 /*
1073  * When we recall a delegation, we should be     730  * When we recall a delegation, we should be careful not to hand it
1074  * out again straight away.                      731  * out again straight away.
1075  * To ensure this we keep a pair of bloom fil    732  * To ensure this we keep a pair of bloom filters ('new' and 'old')
1076  * in which the filehandles of recalled deleg    733  * in which the filehandles of recalled delegations are "stored".
1077  * If a filehandle appear in either filter, a    734  * If a filehandle appear in either filter, a delegation is blocked.
1078  * When a delegation is recalled, the filehan    735  * When a delegation is recalled, the filehandle is stored in the "new"
1079  * filter.                                       736  * filter.
1080  * Every 30 seconds we swap the filters and c    737  * Every 30 seconds we swap the filters and clear the "new" one,
1081  * unless both are empty of course.  This res !! 738  * unless both are empty of course.
1082  * given filehandle being blocked for between << 
1083  *                                               739  *
1084  * Each filter is 256 bits.  We hash the file    740  * Each filter is 256 bits.  We hash the filehandle to 32bit and use the
1085  * low 3 bytes as hash-table indices.            741  * low 3 bytes as hash-table indices.
1086  *                                               742  *
1087  * 'blocked_delegations_lock', which is alway    743  * 'blocked_delegations_lock', which is always taken in block_delegations(),
1088  * is used to manage concurrent access.  Test    744  * is used to manage concurrent access.  Testing does not need the lock
1089  * except when swapping the two filters.         745  * except when swapping the two filters.
1090  */                                              746  */
1091 static DEFINE_SPINLOCK(blocked_delegations_lo    747 static DEFINE_SPINLOCK(blocked_delegations_lock);
1092 static struct bloom_pair {                       748 static struct bloom_pair {
1093         int     entries, old_entries;            749         int     entries, old_entries;
1094         time64_t swap_time;                   !! 750         time_t  swap_time;
1095         int     new; /* index into 'set' */      751         int     new; /* index into 'set' */
1096         DECLARE_BITMAP(set[2], 256);             752         DECLARE_BITMAP(set[2], 256);
1097 } blocked_delegations;                           753 } blocked_delegations;
1098                                                  754 
1099 static int delegation_blocked(struct knfsd_fh    755 static int delegation_blocked(struct knfsd_fh *fh)
1100 {                                                756 {
1101         u32 hash;                                757         u32 hash;
1102         struct bloom_pair *bd = &blocked_dele    758         struct bloom_pair *bd = &blocked_delegations;
1103                                                  759 
1104         if (bd->entries == 0)                    760         if (bd->entries == 0)
1105                 return 0;                        761                 return 0;
1106         if (ktime_get_seconds() - bd->swap_ti !! 762         if (seconds_since_boot() - bd->swap_time > 30) {
1107                 spin_lock(&blocked_delegation    763                 spin_lock(&blocked_delegations_lock);
1108                 if (ktime_get_seconds() - bd- !! 764                 if (seconds_since_boot() - bd->swap_time > 30) {
1109                         bd->entries -= bd->ol    765                         bd->entries -= bd->old_entries;
1110                         bd->old_entries = bd-    766                         bd->old_entries = bd->entries;
1111                         bd->new = 1-bd->new;  << 
1112                         memset(bd->set[bd->ne    767                         memset(bd->set[bd->new], 0,
1113                                sizeof(bd->set    768                                sizeof(bd->set[0]));
1114                         bd->swap_time = ktime !! 769                         bd->new = 1-bd->new;
                                                   >> 770                         bd->swap_time = seconds_since_boot();
1115                 }                                771                 }
1116                 spin_unlock(&blocked_delegati    772                 spin_unlock(&blocked_delegations_lock);
1117         }                                        773         }
1118         hash = jhash(&fh->fh_raw, fh->fh_size !! 774         hash = jhash(&fh->fh_base, fh->fh_size, 0);
1119         if (test_bit(hash&255, bd->set[0]) &&    775         if (test_bit(hash&255, bd->set[0]) &&
1120             test_bit((hash>>8)&255, bd->set[0    776             test_bit((hash>>8)&255, bd->set[0]) &&
1121             test_bit((hash>>16)&255, bd->set[    777             test_bit((hash>>16)&255, bd->set[0]))
1122                 return 1;                        778                 return 1;
1123                                                  779 
1124         if (test_bit(hash&255, bd->set[1]) &&    780         if (test_bit(hash&255, bd->set[1]) &&
1125             test_bit((hash>>8)&255, bd->set[1    781             test_bit((hash>>8)&255, bd->set[1]) &&
1126             test_bit((hash>>16)&255, bd->set[    782             test_bit((hash>>16)&255, bd->set[1]))
1127                 return 1;                        783                 return 1;
1128                                                  784 
1129         return 0;                                785         return 0;
1130 }                                                786 }
1131                                                  787 
1132 static void block_delegations(struct knfsd_fh    788 static void block_delegations(struct knfsd_fh *fh)
1133 {                                                789 {
1134         u32 hash;                                790         u32 hash;
1135         struct bloom_pair *bd = &blocked_dele    791         struct bloom_pair *bd = &blocked_delegations;
1136                                                  792 
1137         hash = jhash(&fh->fh_raw, fh->fh_size !! 793         hash = jhash(&fh->fh_base, fh->fh_size, 0);
1138                                                  794 
1139         spin_lock(&blocked_delegations_lock);    795         spin_lock(&blocked_delegations_lock);
1140         __set_bit(hash&255, bd->set[bd->new])    796         __set_bit(hash&255, bd->set[bd->new]);
1141         __set_bit((hash>>8)&255, bd->set[bd->    797         __set_bit((hash>>8)&255, bd->set[bd->new]);
1142         __set_bit((hash>>16)&255, bd->set[bd-    798         __set_bit((hash>>16)&255, bd->set[bd->new]);
1143         if (bd->entries == 0)                    799         if (bd->entries == 0)
1144                 bd->swap_time = ktime_get_sec !! 800                 bd->swap_time = seconds_since_boot();
1145         bd->entries += 1;                        801         bd->entries += 1;
1146         spin_unlock(&blocked_delegations_lock    802         spin_unlock(&blocked_delegations_lock);
1147 }                                                803 }
1148                                                  804 
1149 static struct nfs4_delegation *                  805 static struct nfs4_delegation *
1150 alloc_init_deleg(struct nfs4_client *clp, str !! 806 alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh,
1151                  struct nfs4_clnt_odstate *od !! 807                  struct nfs4_clnt_odstate *odstate)
1152 {                                                808 {
1153         struct nfs4_delegation *dp;              809         struct nfs4_delegation *dp;
1154         struct nfs4_stid *stid;               << 
1155         long n;                                  810         long n;
1156                                                  811 
1157         dprintk("NFSD alloc_init_deleg\n");      812         dprintk("NFSD alloc_init_deleg\n");
1158         n = atomic_long_inc_return(&num_deleg    813         n = atomic_long_inc_return(&num_delegations);
1159         if (n < 0 || n > max_delegations)        814         if (n < 0 || n > max_delegations)
1160                 goto out_dec;                    815                 goto out_dec;
1161         if (delegation_blocked(&fp->fi_fhandl !! 816         if (delegation_blocked(&current_fh->fh_handle))
1162                 goto out_dec;                    817                 goto out_dec;
1163         stid = nfs4_alloc_stid(clp, deleg_sla !! 818         dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg));
1164         if (stid == NULL)                     !! 819         if (dp == NULL)
1165                 goto out_dec;                    820                 goto out_dec;
1166         dp = delegstateid(stid);              << 
1167                                                  821 
1168         /*                                       822         /*
1169          * delegation seqid's are never incre    823          * delegation seqid's are never incremented.  The 4.1 special
1170          * meaning of seqid 0 isn't meaningfu    824          * meaning of seqid 0 isn't meaningful, really, but let's avoid
1171          * 0 anyway just for consistency and     825          * 0 anyway just for consistency and use 1:
1172          */                                      826          */
1173         dp->dl_stid.sc_stateid.si_generation     827         dp->dl_stid.sc_stateid.si_generation = 1;
1174         INIT_LIST_HEAD(&dp->dl_perfile);         828         INIT_LIST_HEAD(&dp->dl_perfile);
1175         INIT_LIST_HEAD(&dp->dl_perclnt);         829         INIT_LIST_HEAD(&dp->dl_perclnt);
1176         INIT_LIST_HEAD(&dp->dl_recall_lru);      830         INIT_LIST_HEAD(&dp->dl_recall_lru);
1177         dp->dl_clnt_odstate = odstate;           831         dp->dl_clnt_odstate = odstate;
1178         get_clnt_odstate(odstate);               832         get_clnt_odstate(odstate);
1179         dp->dl_type = dl_type;                !! 833         dp->dl_type = NFS4_OPEN_DELEGATE_READ;
1180         dp->dl_retries = 1;                      834         dp->dl_retries = 1;
1181         dp->dl_recalled = false;              << 
1182         nfsd4_init_cb(&dp->dl_recall, dp->dl_    835         nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
1183                       &nfsd4_cb_recall_ops, N    836                       &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);                    << 
1189         dp->dl_stid.sc_file = fp;             << 
1190         return dp;                               837         return dp;
1191 out_dec:                                         838 out_dec:
1192         atomic_long_dec(&num_delegations);       839         atomic_long_dec(&num_delegations);
1193         return NULL;                             840         return NULL;
1194 }                                                841 }
1195                                                  842 
1196 void                                             843 void
1197 nfs4_put_stid(struct nfs4_stid *s)               844 nfs4_put_stid(struct nfs4_stid *s)
1198 {                                                845 {
1199         struct nfs4_file *fp = s->sc_file;       846         struct nfs4_file *fp = s->sc_file;
1200         struct nfs4_client *clp = s->sc_clien    847         struct nfs4_client *clp = s->sc_client;
1201                                                  848 
1202         might_lock(&clp->cl_lock);               849         might_lock(&clp->cl_lock);
1203                                                  850 
1204         if (!refcount_dec_and_lock(&s->sc_cou !! 851         if (!atomic_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
1205                 wake_up_all(&close_wq);          852                 wake_up_all(&close_wq);
1206                 return;                          853                 return;
1207         }                                        854         }
1208         idr_remove(&clp->cl_stateids, s->sc_s    855         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 << 
1212         spin_unlock(&clp->cl_lock);              856         spin_unlock(&clp->cl_lock);
1213         s->sc_free(s);                           857         s->sc_free(s);
1214         if (fp)                                  858         if (fp)
1215                 put_nfs4_file(fp);               859                 put_nfs4_file(fp);
1216 }                                                860 }
1217                                                  861 
1218 void                                             862 void
1219 nfs4_inc_and_copy_stateid(stateid_t *dst, str    863 nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
1220 {                                                864 {
1221         stateid_t *src = &stid->sc_stateid;      865         stateid_t *src = &stid->sc_stateid;
1222                                                  866 
1223         spin_lock(&stid->sc_lock);               867         spin_lock(&stid->sc_lock);
1224         if (unlikely(++src->si_generation ==     868         if (unlikely(++src->si_generation == 0))
1225                 src->si_generation = 1;          869                 src->si_generation = 1;
1226         memcpy(dst, src, sizeof(*dst));          870         memcpy(dst, src, sizeof(*dst));
1227         spin_unlock(&stid->sc_lock);             871         spin_unlock(&stid->sc_lock);
1228 }                                                872 }
1229                                                  873 
1230 static void put_deleg_file(struct nfs4_file * !! 874 static void nfs4_put_deleg_lease(struct nfs4_file *fp)
1231 {                                                875 {
1232         struct nfsd_file *nf = NULL;          !! 876         struct file *filp = NULL;
1233                                                  877 
1234         spin_lock(&fp->fi_lock);                 878         spin_lock(&fp->fi_lock);
1235         if (--fp->fi_delegees == 0)           !! 879         if (fp->fi_deleg_file && --fp->fi_delegees == 0)
1236                 swap(nf, fp->fi_deleg_file);  !! 880                 swap(filp, fp->fi_deleg_file);
1237         spin_unlock(&fp->fi_lock);               881         spin_unlock(&fp->fi_lock);
1238                                                  882 
1239         if (nf)                               !! 883         if (filp) {
1240                 nfsd_file_put(nf);            !! 884                 vfs_setlease(filp, F_UNLCK, NULL, (void **)&fp);
1241 }                                             !! 885                 fput(filp);
1242                                               !! 886         }
1243 static void nfs4_unlock_deleg_lease(struct nf << 
1244 {                                             << 
1245         struct nfs4_file *fp = dp->dl_stid.sc << 
1246         struct nfsd_file *nf = fp->fi_deleg_f << 
1247                                               << 
1248         WARN_ON_ONCE(!fp->fi_delegees);       << 
1249                                               << 
1250         kernel_setlease(nf->nf_file, F_UNLCK, << 
1251         put_deleg_file(fp);                   << 
1252 }                                                887 }
1253                                                  888 
1254 static void destroy_unhashed_deleg(struct nfs !! 889 void nfs4_unhash_stid(struct nfs4_stid *s)
1255 {                                                890 {
1256         put_clnt_odstate(dp->dl_clnt_odstate) !! 891         s->sc_type = 0;
1257         nfs4_unlock_deleg_lease(dp);          << 
1258         nfs4_put_stid(&dp->dl_stid);          << 
1259 }                                                892 }
1260                                                  893 
1261 /**                                              894 /**
1262  * nfs4_delegation_exists - Discover if this  !! 895  * nfs4_get_existing_delegation - Discover if this delegation already exists
1263  * @clp:     a pointer to the nfs4_client we'    896  * @clp:     a pointer to the nfs4_client we're granting a delegation to
1264  * @fp:      a pointer to the nfs4_file we're    897  * @fp:      a pointer to the nfs4_file we're granting a delegation on
1265  *                                               898  *
1266  * Return:                                       899  * Return:
1267  *      On success: true iff an existing dele !! 900  *      On success: NULL if an existing delegation was not found.
                                                   >> 901  *
                                                   >> 902  *      On error: -EAGAIN if one was previously granted to this nfs4_client
                                                   >> 903  *                 for this nfs4_file.
                                                   >> 904  *
1268  */                                              905  */
1269                                                  906 
1270 static bool                                   !! 907 static int
1271 nfs4_delegation_exists(struct nfs4_client *cl !! 908 nfs4_get_existing_delegation(struct nfs4_client *clp, struct nfs4_file *fp)
1272 {                                                909 {
1273         struct nfs4_delegation *searchdp = NU    910         struct nfs4_delegation *searchdp = NULL;
1274         struct nfs4_client *searchclp = NULL;    911         struct nfs4_client *searchclp = NULL;
1275                                                  912 
1276         lockdep_assert_held(&state_lock);        913         lockdep_assert_held(&state_lock);
1277         lockdep_assert_held(&fp->fi_lock);       914         lockdep_assert_held(&fp->fi_lock);
1278                                                  915 
1279         list_for_each_entry(searchdp, &fp->fi    916         list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
1280                 searchclp = searchdp->dl_stid    917                 searchclp = searchdp->dl_stid.sc_client;
1281                 if (clp == searchclp) {          918                 if (clp == searchclp) {
1282                         return true;          !! 919                         return -EAGAIN;
1283                 }                                920                 }
1284         }                                        921         }
1285         return false;                         !! 922         return 0;
1286 }                                                923 }
1287                                                  924 
1288 /**                                              925 /**
1289  * hash_delegation_locked - Add a delegation     926  * hash_delegation_locked - Add a delegation to the appropriate lists
1290  * @dp:     a pointer to the nfs4_delegation     927  * @dp:     a pointer to the nfs4_delegation we are adding.
1291  * @fp:     a pointer to the nfs4_file we're     928  * @fp:     a pointer to the nfs4_file we're granting a delegation on
1292  *                                               929  *
1293  * Return:                                       930  * Return:
1294  *      On success: NULL if the delegation wa    931  *      On success: NULL if the delegation was successfully hashed.
1295  *                                               932  *
1296  *      On error: -EAGAIN if one was previous    933  *      On error: -EAGAIN if one was previously granted to this
1297  *                 nfs4_client for this nfs4_    934  *                 nfs4_client for this nfs4_file. Delegation is not hashed.
1298  *                                               935  *
1299  */                                              936  */
1300                                                  937 
1301 static int                                       938 static int
1302 hash_delegation_locked(struct nfs4_delegation    939 hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
1303 {                                                940 {
                                                   >> 941         int status;
1304         struct nfs4_client *clp = dp->dl_stid    942         struct nfs4_client *clp = dp->dl_stid.sc_client;
1305                                                  943 
1306         lockdep_assert_held(&state_lock);        944         lockdep_assert_held(&state_lock);
1307         lockdep_assert_held(&fp->fi_lock);       945         lockdep_assert_held(&fp->fi_lock);
1308         lockdep_assert_held(&clp->cl_lock);   << 
1309                                                  946 
1310         if (nfs4_delegation_exists(clp, fp))  !! 947         status = nfs4_get_existing_delegation(clp, fp);
1311                 return -EAGAIN;               !! 948         if (status)
1312         refcount_inc(&dp->dl_stid.sc_count);  !! 949                 return status;
1313         dp->dl_stid.sc_type = SC_TYPE_DELEG;  !! 950         ++fp->fi_delegees;
                                                   >> 951         atomic_inc(&dp->dl_stid.sc_count);
                                                   >> 952         dp->dl_stid.sc_type = NFS4_DELEG_STID;
1314         list_add(&dp->dl_perfile, &fp->fi_del    953         list_add(&dp->dl_perfile, &fp->fi_delegations);
1315         list_add(&dp->dl_perclnt, &clp->cl_de    954         list_add(&dp->dl_perclnt, &clp->cl_delegations);
1316         return 0;                                955         return 0;
1317 }                                                956 }
1318                                                  957 
1319 static bool delegation_hashed(struct nfs4_del    958 static bool delegation_hashed(struct nfs4_delegation *dp)
1320 {                                                959 {
1321         return !(list_empty(&dp->dl_perfile))    960         return !(list_empty(&dp->dl_perfile));
1322 }                                                961 }
1323                                                  962 
1324 static bool                                      963 static bool
1325 unhash_delegation_locked(struct nfs4_delegati !! 964 unhash_delegation_locked(struct nfs4_delegation *dp)
1326 {                                                965 {
1327         struct nfs4_file *fp = dp->dl_stid.sc    966         struct nfs4_file *fp = dp->dl_stid.sc_file;
1328                                                  967 
1329         lockdep_assert_held(&state_lock);        968         lockdep_assert_held(&state_lock);
1330                                                  969 
1331         if (!delegation_hashed(dp))              970         if (!delegation_hashed(dp))
1332                 return false;                    971                 return false;
1333                                                  972 
1334         if (statusmask == SC_STATUS_REVOKED & !! 973         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     974         /* Ensure that deleg break won't try to requeue it */
1342         ++dp->dl_time;                           975         ++dp->dl_time;
1343         spin_lock(&fp->fi_lock);                 976         spin_lock(&fp->fi_lock);
1344         list_del_init(&dp->dl_perclnt);          977         list_del_init(&dp->dl_perclnt);
1345         list_del_init(&dp->dl_recall_lru);       978         list_del_init(&dp->dl_recall_lru);
1346         list_del_init(&dp->dl_perfile);          979         list_del_init(&dp->dl_perfile);
1347         spin_unlock(&fp->fi_lock);               980         spin_unlock(&fp->fi_lock);
1348         return true;                             981         return true;
1349 }                                                982 }
1350                                                  983 
1351 static void destroy_delegation(struct nfs4_de    984 static void destroy_delegation(struct nfs4_delegation *dp)
1352 {                                                985 {
1353         bool unhashed;                           986         bool unhashed;
1354                                                  987 
1355         spin_lock(&state_lock);                  988         spin_lock(&state_lock);
1356         unhashed = unhash_delegation_locked(d !! 989         unhashed = unhash_delegation_locked(dp);
1357         spin_unlock(&state_lock);                990         spin_unlock(&state_lock);
1358         if (unhashed)                         !! 991         if (unhashed) {
1359                 destroy_unhashed_deleg(dp);   !! 992                 put_clnt_odstate(dp->dl_clnt_odstate);
                                                   >> 993                 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
                                                   >> 994                 nfs4_put_stid(&dp->dl_stid);
                                                   >> 995         }
1360 }                                                996 }
1361                                                  997 
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    998 static void revoke_delegation(struct nfs4_delegation *dp)
1385 {                                                999 {
1386         struct nfs4_client *clp = dp->dl_stid    1000         struct nfs4_client *clp = dp->dl_stid.sc_client;
1387                                                  1001 
1388         WARN_ON(!list_empty(&dp->dl_recall_lr    1002         WARN_ON(!list_empty(&dp->dl_recall_lru));
1389         WARN_ON_ONCE(!(dp->dl_stid.sc_status  << 
1390                      (SC_STATUS_REVOKED | SC_ << 
1391                                                  1003 
1392         trace_nfsd_stid_revoke(&dp->dl_stid); !! 1004         put_clnt_odstate(dp->dl_clnt_odstate);
                                                   >> 1005         nfs4_put_deleg_lease(dp->dl_stid.sc_file);
1393                                                  1006 
1394         spin_lock(&clp->cl_lock);             !! 1007         if (clp->cl_minorversion == 0)
1395         if (dp->dl_stid.sc_status & SC_STATUS !! 1008                 nfs4_put_stid(&dp->dl_stid);
1396                 list_del_init(&dp->dl_recall_ !! 1009         else {
1397                 goto out;                     !! 1010                 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
                                                   >> 1011                 spin_lock(&clp->cl_lock);
                                                   >> 1012                 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
                                                   >> 1013                 spin_unlock(&clp->cl_lock);
1398         }                                        1014         }
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);           << 
1404 }                                                1015 }
1405                                                  1016 
1406 /*                                            !! 1017 /* 
1407  * SETCLIENTID state                          !! 1018  * SETCLIENTID state 
1408  */                                              1019  */
1409                                                  1020 
1410 static unsigned int clientid_hashval(u32 id)     1021 static unsigned int clientid_hashval(u32 id)
1411 {                                                1022 {
1412         return id & CLIENT_HASH_MASK;            1023         return id & CLIENT_HASH_MASK;
1413 }                                                1024 }
1414                                                  1025 
1415 static unsigned int clientstr_hashval(struct  !! 1026 static unsigned int clientstr_hashval(const char *name)
1416 {                                                1027 {
1417         return opaque_hashval(name.data, 8) & !! 1028         return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
                                                   >> 1029 }
                                                   >> 1030 
                                                   >> 1031 /*
                                                   >> 1032  * We store the NONE, READ, WRITE, and BOTH bits separately in the
                                                   >> 1033  * st_{access,deny}_bmap field of the stateid, in order to track not
                                                   >> 1034  * only what share bits are currently in force, but also what
                                                   >> 1035  * combinations of share bits previous opens have used.  This allows us
                                                   >> 1036  * to enforce the recommendation of rfc 3530 14.2.19 that the server
                                                   >> 1037  * return an error if the client attempt to downgrade to a combination
                                                   >> 1038  * of share bits not explicable by closing some of its previous opens.
                                                   >> 1039  *
                                                   >> 1040  * XXX: This enforcement is actually incomplete, since we don't keep
                                                   >> 1041  * track of access/deny bit combinations; so, e.g., we allow:
                                                   >> 1042  *
                                                   >> 1043  *      OPEN allow read, deny write
                                                   >> 1044  *      OPEN allow both, deny none
                                                   >> 1045  *      DOWNGRADE allow read, deny none
                                                   >> 1046  *
                                                   >> 1047  * which we should reject.
                                                   >> 1048  */
                                                   >> 1049 static unsigned int
                                                   >> 1050 bmap_to_share_mode(unsigned long bmap) {
                                                   >> 1051         int i;
                                                   >> 1052         unsigned int access = 0;
                                                   >> 1053 
                                                   >> 1054         for (i = 1; i < 4; i++) {
                                                   >> 1055                 if (test_bit(i, &bmap))
                                                   >> 1056                         access |= i;
                                                   >> 1057         }
                                                   >> 1058         return access;
                                                   >> 1059 }
                                                   >> 1060 
                                                   >> 1061 /* set share access for a given stateid */
                                                   >> 1062 static inline void
                                                   >> 1063 set_access(u32 access, struct nfs4_ol_stateid *stp)
                                                   >> 1064 {
                                                   >> 1065         unsigned char mask = 1 << access;
                                                   >> 1066 
                                                   >> 1067         WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
                                                   >> 1068         stp->st_access_bmap |= mask;
                                                   >> 1069 }
                                                   >> 1070 
                                                   >> 1071 /* clear share access for a given stateid */
                                                   >> 1072 static inline void
                                                   >> 1073 clear_access(u32 access, struct nfs4_ol_stateid *stp)
                                                   >> 1074 {
                                                   >> 1075         unsigned char mask = 1 << access;
                                                   >> 1076 
                                                   >> 1077         WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
                                                   >> 1078         stp->st_access_bmap &= ~mask;
                                                   >> 1079 }
                                                   >> 1080 
                                                   >> 1081 /* test whether a given stateid has access */
                                                   >> 1082 static inline bool
                                                   >> 1083 test_access(u32 access, struct nfs4_ol_stateid *stp)
                                                   >> 1084 {
                                                   >> 1085         unsigned char mask = 1 << access;
                                                   >> 1086 
                                                   >> 1087         return (bool)(stp->st_access_bmap & mask);
                                                   >> 1088 }
                                                   >> 1089 
                                                   >> 1090 /* set share deny for a given stateid */
                                                   >> 1091 static inline void
                                                   >> 1092 set_deny(u32 deny, struct nfs4_ol_stateid *stp)
                                                   >> 1093 {
                                                   >> 1094         unsigned char mask = 1 << deny;
                                                   >> 1095 
                                                   >> 1096         WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
                                                   >> 1097         stp->st_deny_bmap |= mask;
                                                   >> 1098 }
                                                   >> 1099 
                                                   >> 1100 /* clear share deny for a given stateid */
                                                   >> 1101 static inline void
                                                   >> 1102 clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
                                                   >> 1103 {
                                                   >> 1104         unsigned char mask = 1 << deny;
                                                   >> 1105 
                                                   >> 1106         WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
                                                   >> 1107         stp->st_deny_bmap &= ~mask;
                                                   >> 1108 }
                                                   >> 1109 
                                                   >> 1110 /* test whether a given stateid is denying specific access */
                                                   >> 1111 static inline bool
                                                   >> 1112 test_deny(u32 deny, struct nfs4_ol_stateid *stp)
                                                   >> 1113 {
                                                   >> 1114         unsigned char mask = 1 << deny;
                                                   >> 1115 
                                                   >> 1116         return (bool)(stp->st_deny_bmap & mask);
                                                   >> 1117 }
                                                   >> 1118 
                                                   >> 1119 static int nfs4_access_to_omode(u32 access)
                                                   >> 1120 {
                                                   >> 1121         switch (access & NFS4_SHARE_ACCESS_BOTH) {
                                                   >> 1122         case NFS4_SHARE_ACCESS_READ:
                                                   >> 1123                 return O_RDONLY;
                                                   >> 1124         case NFS4_SHARE_ACCESS_WRITE:
                                                   >> 1125                 return O_WRONLY;
                                                   >> 1126         case NFS4_SHARE_ACCESS_BOTH:
                                                   >> 1127                 return O_RDWR;
                                                   >> 1128         }
                                                   >> 1129         WARN_ON_ONCE(1);
                                                   >> 1130         return O_RDONLY;
1418 }                                                1131 }
1419                                                  1132 
1420 /*                                               1133 /*
1421  * A stateid that had a deny mode associated     1134  * A stateid that had a deny mode associated with it is being released
1422  * or downgraded. Recalculate the deny mode o    1135  * or downgraded. Recalculate the deny mode on the file.
1423  */                                              1136  */
1424 static void                                      1137 static void
1425 recalculate_deny_mode(struct nfs4_file *fp)      1138 recalculate_deny_mode(struct nfs4_file *fp)
1426 {                                                1139 {
1427         struct nfs4_ol_stateid *stp;             1140         struct nfs4_ol_stateid *stp;
1428         u32 old_deny;                         << 
1429                                                  1141 
1430         spin_lock(&fp->fi_lock);                 1142         spin_lock(&fp->fi_lock);
1431         old_deny = fp->fi_share_deny;         << 
1432         fp->fi_share_deny = 0;                   1143         fp->fi_share_deny = 0;
1433         list_for_each_entry(stp, &fp->fi_stat !! 1144         list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
1434                 fp->fi_share_deny |= bmap_to_    1145                 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);               1146         spin_unlock(&fp->fi_lock);
1439 }                                                1147 }
1440                                                  1148 
1441 static void                                      1149 static void
1442 reset_union_bmap_deny(u32 deny, struct nfs4_o    1150 reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
1443 {                                                1151 {
1444         int i;                                   1152         int i;
1445         bool change = false;                     1153         bool change = false;
1446                                                  1154 
1447         for (i = 1; i < 4; i++) {                1155         for (i = 1; i < 4; i++) {
1448                 if ((i & deny) != i) {           1156                 if ((i & deny) != i) {
1449                         change = true;           1157                         change = true;
1450                         clear_deny(i, stp);      1158                         clear_deny(i, stp);
1451                 }                                1159                 }
1452         }                                        1160         }
1453                                                  1161 
1454         /* Recalculate per-file deny mode if     1162         /* Recalculate per-file deny mode if there was a change */
1455         if (change)                              1163         if (change)
1456                 recalculate_deny_mode(stp->st    1164                 recalculate_deny_mode(stp->st_stid.sc_file);
1457 }                                                1165 }
1458                                                  1166 
1459 /* release all access and file references for    1167 /* release all access and file references for a given stateid */
1460 static void                                      1168 static void
1461 release_all_access(struct nfs4_ol_stateid *st    1169 release_all_access(struct nfs4_ol_stateid *stp)
1462 {                                                1170 {
1463         int i;                                   1171         int i;
1464         struct nfs4_file *fp = stp->st_stid.s    1172         struct nfs4_file *fp = stp->st_stid.sc_file;
1465                                                  1173 
1466         if (fp && stp->st_deny_bmap != 0)        1174         if (fp && stp->st_deny_bmap != 0)
1467                 recalculate_deny_mode(fp);       1175                 recalculate_deny_mode(fp);
1468                                                  1176 
1469         for (i = 1; i < 4; i++) {                1177         for (i = 1; i < 4; i++) {
1470                 if (test_access(i, stp))         1178                 if (test_access(i, stp))
1471                         nfs4_file_put_access(    1179                         nfs4_file_put_access(stp->st_stid.sc_file, i);
1472                 clear_access(i, stp);            1180                 clear_access(i, stp);
1473         }                                        1181         }
1474 }                                                1182 }
1475                                                  1183 
1476 static inline void nfs4_free_stateowner(struc    1184 static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1477 {                                                1185 {
1478         kfree(sop->so_owner.data);               1186         kfree(sop->so_owner.data);
1479         sop->so_ops->so_free(sop);               1187         sop->so_ops->so_free(sop);
1480 }                                                1188 }
1481                                                  1189 
1482 static void nfs4_put_stateowner(struct nfs4_s    1190 static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1483 {                                                1191 {
1484         struct nfs4_client *clp = sop->so_cli    1192         struct nfs4_client *clp = sop->so_client;
1485                                                  1193 
1486         might_lock(&clp->cl_lock);               1194         might_lock(&clp->cl_lock);
1487                                                  1195 
1488         if (!atomic_dec_and_lock(&sop->so_cou    1196         if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
1489                 return;                          1197                 return;
1490         sop->so_ops->so_unhash(sop);             1198         sop->so_ops->so_unhash(sop);
1491         spin_unlock(&clp->cl_lock);              1199         spin_unlock(&clp->cl_lock);
1492         nfs4_free_stateowner(sop);               1200         nfs4_free_stateowner(sop);
1493 }                                                1201 }
1494                                                  1202 
1495 static bool                                   << 
1496 nfs4_ol_stateid_unhashed(const struct nfs4_ol << 
1497 {                                             << 
1498         return list_empty(&stp->st_perfile);  << 
1499 }                                             << 
1500                                               << 
1501 static bool unhash_ol_stateid(struct nfs4_ol_    1203 static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
1502 {                                                1204 {
1503         struct nfs4_file *fp = stp->st_stid.s    1205         struct nfs4_file *fp = stp->st_stid.sc_file;
1504                                                  1206 
1505         lockdep_assert_held(&stp->st_stateown    1207         lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1506                                                  1208 
1507         if (list_empty(&stp->st_perfile))        1209         if (list_empty(&stp->st_perfile))
1508                 return false;                    1210                 return false;
1509                                                  1211 
1510         spin_lock(&fp->fi_lock);                 1212         spin_lock(&fp->fi_lock);
1511         list_del_init(&stp->st_perfile);         1213         list_del_init(&stp->st_perfile);
1512         spin_unlock(&fp->fi_lock);               1214         spin_unlock(&fp->fi_lock);
1513         list_del(&stp->st_perstateowner);        1215         list_del(&stp->st_perstateowner);
1514         return true;                             1216         return true;
1515 }                                                1217 }
1516                                                  1218 
1517 static void nfs4_free_ol_stateid(struct nfs4_    1219 static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
1518 {                                                1220 {
1519         struct nfs4_ol_stateid *stp = openloc    1221         struct nfs4_ol_stateid *stp = openlockstateid(stid);
1520                                                  1222 
1521         put_clnt_odstate(stp->st_clnt_odstate    1223         put_clnt_odstate(stp->st_clnt_odstate);
1522         release_all_access(stp);                 1224         release_all_access(stp);
1523         if (stp->st_stateowner)                  1225         if (stp->st_stateowner)
1524                 nfs4_put_stateowner(stp->st_s    1226                 nfs4_put_stateowner(stp->st_stateowner);
1525         WARN_ON(!list_empty(&stid->sc_cp_list << 
1526         kmem_cache_free(stateid_slab, stid);     1227         kmem_cache_free(stateid_slab, stid);
1527 }                                                1228 }
1528                                                  1229 
1529 static void nfs4_free_lock_stateid(struct nfs    1230 static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
1530 {                                                1231 {
1531         struct nfs4_ol_stateid *stp = openloc    1232         struct nfs4_ol_stateid *stp = openlockstateid(stid);
1532         struct nfs4_lockowner *lo = lockowner    1233         struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
1533         struct nfsd_file *nf;                 !! 1234         struct file *file;
1534                                                  1235 
1535         nf = find_any_file(stp->st_stid.sc_fi !! 1236         file = find_any_file(stp->st_stid.sc_file);
1536         if (nf) {                             !! 1237         if (file)
1537                 get_file(nf->nf_file);        !! 1238                 filp_close(file, (fl_owner_t)lo);
1538                 filp_close(nf->nf_file, (fl_o << 
1539                 nfsd_file_put(nf);            << 
1540         }                                     << 
1541         nfs4_free_ol_stateid(stid);              1239         nfs4_free_ol_stateid(stid);
1542 }                                                1240 }
1543                                                  1241 
1544 /*                                               1242 /*
1545  * Put the persistent reference to an already    1243  * Put the persistent reference to an already unhashed generic stateid, while
1546  * holding the cl_lock. If it's the last refe    1244  * holding the cl_lock. If it's the last reference, then put it onto the
1547  * reaplist for later destruction.               1245  * reaplist for later destruction.
1548  */                                              1246  */
1549 static void put_ol_stateid_locked(struct nfs4    1247 static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1550                                        struct    1248                                        struct list_head *reaplist)
1551 {                                                1249 {
1552         struct nfs4_stid *s = &stp->st_stid;     1250         struct nfs4_stid *s = &stp->st_stid;
1553         struct nfs4_client *clp = s->sc_clien    1251         struct nfs4_client *clp = s->sc_client;
1554                                                  1252 
1555         lockdep_assert_held(&clp->cl_lock);      1253         lockdep_assert_held(&clp->cl_lock);
1556                                                  1254 
1557         WARN_ON_ONCE(!list_empty(&stp->st_loc    1255         WARN_ON_ONCE(!list_empty(&stp->st_locks));
1558                                                  1256 
1559         if (!refcount_dec_and_test(&s->sc_cou !! 1257         if (!atomic_dec_and_test(&s->sc_count)) {
1560                 wake_up_all(&close_wq);          1258                 wake_up_all(&close_wq);
1561                 return;                          1259                 return;
1562         }                                        1260         }
1563                                                  1261 
1564         idr_remove(&clp->cl_stateids, s->sc_s    1262         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);      1263         list_add(&stp->st_locks, reaplist);
1568 }                                                1264 }
1569                                                  1265 
1570 static bool unhash_lock_stateid(struct nfs4_o    1266 static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
1571 {                                                1267 {
1572         lockdep_assert_held(&stp->st_stid.sc_    1268         lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1573                                                  1269 
1574         if (!unhash_ol_stateid(stp))          << 
1575                 return false;                 << 
1576         list_del_init(&stp->st_locks);           1270         list_del_init(&stp->st_locks);
1577         stp->st_stid.sc_status |= SC_STATUS_C !! 1271         nfs4_unhash_stid(&stp->st_stid);
1578         return true;                          !! 1272         return unhash_ol_stateid(stp);
1579 }                                                1273 }
1580                                                  1274 
1581 static void release_lock_stateid(struct nfs4_    1275 static void release_lock_stateid(struct nfs4_ol_stateid *stp)
1582 {                                                1276 {
1583         struct nfs4_client *clp = stp->st_sti    1277         struct nfs4_client *clp = stp->st_stid.sc_client;
1584         bool unhashed;                           1278         bool unhashed;
1585                                                  1279 
1586         spin_lock(&clp->cl_lock);                1280         spin_lock(&clp->cl_lock);
1587         unhashed = unhash_lock_stateid(stp);     1281         unhashed = unhash_lock_stateid(stp);
1588         spin_unlock(&clp->cl_lock);              1282         spin_unlock(&clp->cl_lock);
1589         if (unhashed)                            1283         if (unhashed)
1590                 nfs4_put_stid(&stp->st_stid);    1284                 nfs4_put_stid(&stp->st_stid);
1591 }                                                1285 }
1592                                                  1286 
1593 static void unhash_lockowner_locked(struct nf    1287 static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
1594 {                                                1288 {
1595         struct nfs4_client *clp = lo->lo_owne    1289         struct nfs4_client *clp = lo->lo_owner.so_client;
1596                                                  1290 
1597         lockdep_assert_held(&clp->cl_lock);      1291         lockdep_assert_held(&clp->cl_lock);
1598                                                  1292 
1599         list_del_init(&lo->lo_owner.so_strhas    1293         list_del_init(&lo->lo_owner.so_strhash);
1600 }                                                1294 }
1601                                                  1295 
1602 /*                                               1296 /*
1603  * Free a list of generic stateids that were     1297  * Free a list of generic stateids that were collected earlier after being
1604  * fully unhashed.                               1298  * fully unhashed.
1605  */                                              1299  */
1606 static void                                      1300 static void
1607 free_ol_stateid_reaplist(struct list_head *re    1301 free_ol_stateid_reaplist(struct list_head *reaplist)
1608 {                                                1302 {
1609         struct nfs4_ol_stateid *stp;             1303         struct nfs4_ol_stateid *stp;
1610         struct nfs4_file *fp;                    1304         struct nfs4_file *fp;
1611                                                  1305 
1612         might_sleep();                           1306         might_sleep();
1613                                                  1307 
1614         while (!list_empty(reaplist)) {          1308         while (!list_empty(reaplist)) {
1615                 stp = list_first_entry(reapli    1309                 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1616                                        st_loc    1310                                        st_locks);
1617                 list_del(&stp->st_locks);        1311                 list_del(&stp->st_locks);
1618                 fp = stp->st_stid.sc_file;       1312                 fp = stp->st_stid.sc_file;
1619                 stp->st_stid.sc_free(&stp->st    1313                 stp->st_stid.sc_free(&stp->st_stid);
1620                 if (fp)                          1314                 if (fp)
1621                         put_nfs4_file(fp);       1315                         put_nfs4_file(fp);
1622         }                                        1316         }
1623 }                                                1317 }
1624                                                  1318 
1625 static void release_open_stateid_locks(struct    1319 static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1626                                        struct    1320                                        struct list_head *reaplist)
1627 {                                                1321 {
1628         struct nfs4_ol_stateid *stp;             1322         struct nfs4_ol_stateid *stp;
1629                                                  1323 
1630         lockdep_assert_held(&open_stp->st_sti    1324         lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1631                                                  1325 
1632         while (!list_empty(&open_stp->st_lock    1326         while (!list_empty(&open_stp->st_locks)) {
1633                 stp = list_entry(open_stp->st    1327                 stp = list_entry(open_stp->st_locks.next,
1634                                 struct nfs4_o    1328                                 struct nfs4_ol_stateid, st_locks);
1635                 unhash_lock_stateid(stp);     !! 1329                 WARN_ON(!unhash_lock_stateid(stp));
1636                 put_ol_stateid_locked(stp, re    1330                 put_ol_stateid_locked(stp, reaplist);
1637         }                                        1331         }
1638 }                                                1332 }
1639                                                  1333 
1640 static bool unhash_open_stateid(struct nfs4_o    1334 static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
1641                                 struct list_h    1335                                 struct list_head *reaplist)
1642 {                                                1336 {
                                                   >> 1337         bool unhashed;
                                                   >> 1338 
1643         lockdep_assert_held(&stp->st_stid.sc_    1339         lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1644                                                  1340 
1645         if (!unhash_ol_stateid(stp))          !! 1341         unhashed = unhash_ol_stateid(stp);
1646                 return false;                 << 
1647         release_open_stateid_locks(stp, reapl    1342         release_open_stateid_locks(stp, reaplist);
1648         return true;                          !! 1343         return unhashed;
1649 }                                                1344 }
1650                                                  1345 
1651 static void release_open_stateid(struct nfs4_    1346 static void release_open_stateid(struct nfs4_ol_stateid *stp)
1652 {                                                1347 {
1653         LIST_HEAD(reaplist);                     1348         LIST_HEAD(reaplist);
1654                                                  1349 
1655         spin_lock(&stp->st_stid.sc_client->cl    1350         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    1351         if (unhash_open_stateid(stp, &reaplist))
1658                 put_ol_stateid_locked(stp, &r    1352                 put_ol_stateid_locked(stp, &reaplist);
1659         spin_unlock(&stp->st_stid.sc_client->    1353         spin_unlock(&stp->st_stid.sc_client->cl_lock);
1660         free_ol_stateid_reaplist(&reaplist);     1354         free_ol_stateid_reaplist(&reaplist);
1661 }                                                1355 }
1662                                                  1356 
1663 static void unhash_openowner_locked(struct nf    1357 static void unhash_openowner_locked(struct nfs4_openowner *oo)
1664 {                                                1358 {
1665         struct nfs4_client *clp = oo->oo_owne    1359         struct nfs4_client *clp = oo->oo_owner.so_client;
1666                                                  1360 
1667         lockdep_assert_held(&clp->cl_lock);      1361         lockdep_assert_held(&clp->cl_lock);
1668                                                  1362 
1669         list_del_init(&oo->oo_owner.so_strhas    1363         list_del_init(&oo->oo_owner.so_strhash);
1670         list_del_init(&oo->oo_perclient);        1364         list_del_init(&oo->oo_perclient);
1671 }                                                1365 }
1672                                                  1366 
1673 static void release_last_closed_stateid(struc    1367 static void release_last_closed_stateid(struct nfs4_openowner *oo)
1674 {                                                1368 {
1675         struct nfsd_net *nn = net_generic(oo-    1369         struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1676                                           nfs    1370                                           nfsd_net_id);
1677         struct nfs4_ol_stateid *s;               1371         struct nfs4_ol_stateid *s;
1678                                                  1372 
1679         spin_lock(&nn->client_lock);             1373         spin_lock(&nn->client_lock);
1680         s = oo->oo_last_closed_stid;             1374         s = oo->oo_last_closed_stid;
1681         if (s) {                                 1375         if (s) {
1682                 list_del_init(&oo->oo_close_l    1376                 list_del_init(&oo->oo_close_lru);
1683                 oo->oo_last_closed_stid = NUL    1377                 oo->oo_last_closed_stid = NULL;
1684         }                                        1378         }
1685         spin_unlock(&nn->client_lock);           1379         spin_unlock(&nn->client_lock);
1686         if (s)                                   1380         if (s)
1687                 nfs4_put_stid(&s->st_stid);      1381                 nfs4_put_stid(&s->st_stid);
1688 }                                                1382 }
1689                                                  1383 
1690 static void release_openowner(struct nfs4_ope    1384 static void release_openowner(struct nfs4_openowner *oo)
1691 {                                                1385 {
1692         struct nfs4_ol_stateid *stp;             1386         struct nfs4_ol_stateid *stp;
1693         struct nfs4_client *clp = oo->oo_owne    1387         struct nfs4_client *clp = oo->oo_owner.so_client;
1694         LIST_HEAD(reaplist);                  !! 1388         struct list_head reaplist;
                                                   >> 1389 
                                                   >> 1390         INIT_LIST_HEAD(&reaplist);
1695                                                  1391 
1696         spin_lock(&clp->cl_lock);                1392         spin_lock(&clp->cl_lock);
1697         unhash_openowner_locked(oo);             1393         unhash_openowner_locked(oo);
1698         while (!list_empty(&oo->oo_owner.so_s    1394         while (!list_empty(&oo->oo_owner.so_stateids)) {
1699                 stp = list_first_entry(&oo->o    1395                 stp = list_first_entry(&oo->oo_owner.so_stateids,
1700                                 struct nfs4_o    1396                                 struct nfs4_ol_stateid, st_perstateowner);
1701                 if (unhash_open_stateid(stp,     1397                 if (unhash_open_stateid(stp, &reaplist))
1702                         put_ol_stateid_locked    1398                         put_ol_stateid_locked(stp, &reaplist);
1703         }                                        1399         }
1704         spin_unlock(&clp->cl_lock);              1400         spin_unlock(&clp->cl_lock);
1705         free_ol_stateid_reaplist(&reaplist);     1401         free_ol_stateid_reaplist(&reaplist);
1706         release_last_closed_stateid(oo);         1402         release_last_closed_stateid(oo);
1707         nfs4_put_stateowner(&oo->oo_owner);      1403         nfs4_put_stateowner(&oo->oo_owner);
1708 }                                                1404 }
1709                                                  1405 
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                                1406 static inline int
1842 hash_sessionid(struct nfs4_sessionid *session    1407 hash_sessionid(struct nfs4_sessionid *sessionid)
1843 {                                                1408 {
1844         struct nfsd4_sessionid *sid = (struct    1409         struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1845                                                  1410 
1846         return sid->sequence % SESSION_HASH_S    1411         return sid->sequence % SESSION_HASH_SIZE;
1847 }                                                1412 }
1848                                                  1413 
1849 #ifdef CONFIG_SUNRPC_DEBUG                       1414 #ifdef CONFIG_SUNRPC_DEBUG
1850 static inline void                               1415 static inline void
1851 dump_sessionid(const char *fn, struct nfs4_se    1416 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1852 {                                                1417 {
1853         u32 *ptr = (u32 *)(&sessionid->data[0    1418         u32 *ptr = (u32 *)(&sessionid->data[0]);
1854         dprintk("%s: %u:%u:%u:%u\n", fn, ptr[    1419         dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1855 }                                                1420 }
1856 #else                                            1421 #else
1857 static inline void                               1422 static inline void
1858 dump_sessionid(const char *fn, struct nfs4_se    1423 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1859 {                                                1424 {
1860 }                                                1425 }
1861 #endif                                           1426 #endif
1862                                                  1427 
1863 /*                                               1428 /*
1864  * Bump the seqid on cstate->replay_owner, an    1429  * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1865  * won't be used for replay.                     1430  * won't be used for replay.
1866  */                                              1431  */
1867 void nfsd4_bump_seqid(struct nfsd4_compound_s    1432 void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1868 {                                                1433 {
1869         struct nfs4_stateowner *so = cstate->    1434         struct nfs4_stateowner *so = cstate->replay_owner;
1870                                                  1435 
1871         if (nfserr == nfserr_replay_me)          1436         if (nfserr == nfserr_replay_me)
1872                 return;                          1437                 return;
1873                                                  1438 
1874         if (!seqid_mutating_err(ntohl(nfserr)    1439         if (!seqid_mutating_err(ntohl(nfserr))) {
1875                 nfsd4_cstate_clear_replay(cst    1440                 nfsd4_cstate_clear_replay(cstate);
1876                 return;                          1441                 return;
1877         }                                        1442         }
1878         if (!so)                                 1443         if (!so)
1879                 return;                          1444                 return;
1880         if (so->so_is_open_owner)                1445         if (so->so_is_open_owner)
1881                 release_last_closed_stateid(o    1446                 release_last_closed_stateid(openowner(so));
1882         so->so_seqid++;                          1447         so->so_seqid++;
1883         return;                                  1448         return;
1884 }                                                1449 }
1885                                                  1450 
1886 static void                                      1451 static void
1887 gen_sessionid(struct nfsd4_session *ses)         1452 gen_sessionid(struct nfsd4_session *ses)
1888 {                                                1453 {
1889         struct nfs4_client *clp = ses->se_cli    1454         struct nfs4_client *clp = ses->se_client;
1890         struct nfsd4_sessionid *sid;             1455         struct nfsd4_sessionid *sid;
1891                                                  1456 
1892         sid = (struct nfsd4_sessionid *)ses->    1457         sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1893         sid->clientid = clp->cl_clientid;        1458         sid->clientid = clp->cl_clientid;
1894         sid->sequence = current_sessionid++;     1459         sid->sequence = current_sessionid++;
1895         sid->reserved = 0;                       1460         sid->reserved = 0;
1896 }                                                1461 }
1897                                                  1462 
1898 /*                                               1463 /*
1899  * The protocol defines ca_maxresponssize_cac    1464  * The protocol defines ca_maxresponssize_cached to include the size of
1900  * the rpc header, but all we need to cache i    1465  * the rpc header, but all we need to cache is the data starting after
1901  * the end of the initial SEQUENCE operation-    1466  * the end of the initial SEQUENCE operation--the rest we regenerate
1902  * each time.  Therefore we can advertise a c    1467  * each time.  Therefore we can advertise a ca_maxresponssize_cached
1903  * value that is the number of bytes in our c    1468  * value that is the number of bytes in our cache plus a few additional
1904  * bytes.  In order to stay on the safe side,    1469  * bytes.  In order to stay on the safe side, and not promise more than
1905  * we can cache, those additional bytes must     1470  * we can cache, those additional bytes must be the minimum possible: 24
1906  * bytes of rpc header (xid through accept st    1471  * bytes of rpc header (xid through accept state, with AUTH_NULL
1907  * verifier), 12 for the compound header (wit    1472  * verifier), 12 for the compound header (with zero-length tag), and 44
1908  * for the SEQUENCE op response:                 1473  * for the SEQUENCE op response:
1909  */                                              1474  */
1910 #define NFSD_MIN_HDR_SEQ_SZ  (24 + 12 + 44)      1475 #define NFSD_MIN_HDR_SEQ_SZ  (24 + 12 + 44)
1911                                                  1476 
1912 static void                                      1477 static void
1913 free_session_slots(struct nfsd4_session *ses)    1478 free_session_slots(struct nfsd4_session *ses)
1914 {                                                1479 {
1915         int i;                                   1480         int i;
1916                                                  1481 
1917         for (i = 0; i < ses->se_fchannel.maxr    1482         for (i = 0; i < ses->se_fchannel.maxreqs; i++) {
1918                 free_svc_cred(&ses->se_slots[    1483                 free_svc_cred(&ses->se_slots[i]->sl_cred);
1919                 kfree(ses->se_slots[i]);         1484                 kfree(ses->se_slots[i]);
1920         }                                        1485         }
1921 }                                                1486 }
1922                                                  1487 
1923 /*                                               1488 /*
1924  * We don't actually need to cache the rpc an    1489  * We don't actually need to cache the rpc and session headers, so we
1925  * can allocate a little less for each slot:     1490  * can allocate a little less for each slot:
1926  */                                              1491  */
1927 static inline u32 slot_bytes(struct nfsd4_cha    1492 static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
1928 {                                                1493 {
1929         u32 size;                                1494         u32 size;
1930                                                  1495 
1931         if (ca->maxresp_cached < NFSD_MIN_HDR    1496         if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1932                 size = 0;                        1497                 size = 0;
1933         else                                     1498         else
1934                 size = ca->maxresp_cached - N    1499                 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1935         return size + sizeof(struct nfsd4_slo    1500         return size + sizeof(struct nfsd4_slot);
1936 }                                                1501 }
1937                                                  1502 
1938 /*                                               1503 /*
1939  * XXX: If we run out of reserved DRC memory     1504  * XXX: If we run out of reserved DRC memory we could (up to a point)
1940  * re-negotiate active sessions and reduce th    1505  * re-negotiate active sessions and reduce their slot usage to make
1941  * room for new connections. For now we just     1506  * room for new connections. For now we just fail the create session.
1942  */                                              1507  */
1943 static u32 nfsd4_get_drc_mem(struct nfsd4_cha !! 1508 static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
1944 {                                                1509 {
1945         u32 slotsize = slot_bytes(ca);           1510         u32 slotsize = slot_bytes(ca);
1946         u32 num = ca->maxreqs;                   1511         u32 num = ca->maxreqs;
1947         unsigned long avail, total_avail;        1512         unsigned long avail, total_avail;
1948         unsigned int scale_factor;            << 
1949                                                  1513 
1950         spin_lock(&nfsd_drc_lock);               1514         spin_lock(&nfsd_drc_lock);
1951         if (nfsd_drc_max_mem > nfsd_drc_mem_u !! 1515         total_avail = nfsd_drc_max_mem - nfsd_drc_mem_used;
1952                 total_avail = nfsd_drc_max_me << 
1953         else                                  << 
1954                 /* We have handed out more sp << 
1955                  * set_max_drc() to allow.  T << 
1956                  * problem as long as that do << 
1957                  * have lots more due to inte << 
1958                  */                           << 
1959                 total_avail = 0;              << 
1960         avail = min((unsigned long)NFSD_MAX_M    1516         avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, total_avail);
1961         /*                                       1517         /*
1962          * Never use more than a fraction of  !! 1518          * Never use more than a third of the remaining memory,
1963          * unless it's the only way to give t !! 1519          * unless it's the only way to give this client a slot:
1964          * The chosen fraction is either 1/8  << 
1965          * whichever is smaller.  This ensure << 
1966          * slots to support multiple clients  << 
1967          * Give the client one slot even if t << 
1968          * over-allocation--it is better than << 
1969          */                                      1520          */
1970         scale_factor = max_t(unsigned int, 8, !! 1521         avail = clamp_t(unsigned long, avail, slotsize, total_avail/3);
1971                                               << 
1972         avail = clamp_t(unsigned long, avail, << 
1973                         total_avail/scale_fac << 
1974         num = min_t(int, num, avail / slotsiz    1522         num = min_t(int, num, avail / slotsize);
1975         num = max_t(int, num, 1);             << 
1976         nfsd_drc_mem_used += num * slotsize;     1523         nfsd_drc_mem_used += num * slotsize;
1977         spin_unlock(&nfsd_drc_lock);             1524         spin_unlock(&nfsd_drc_lock);
1978                                                  1525 
1979         return num;                              1526         return num;
1980 }                                                1527 }
1981                                                  1528 
1982 static void nfsd4_put_drc_mem(struct nfsd4_ch    1529 static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
1983 {                                                1530 {
1984         int slotsize = slot_bytes(ca);           1531         int slotsize = slot_bytes(ca);
1985                                                  1532 
1986         spin_lock(&nfsd_drc_lock);               1533         spin_lock(&nfsd_drc_lock);
1987         nfsd_drc_mem_used -= slotsize * ca->m    1534         nfsd_drc_mem_used -= slotsize * ca->maxreqs;
1988         spin_unlock(&nfsd_drc_lock);             1535         spin_unlock(&nfsd_drc_lock);
1989 }                                                1536 }
1990                                                  1537 
1991 static struct nfsd4_session *alloc_session(st    1538 static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1992                                            st    1539                                            struct nfsd4_channel_attrs *battrs)
1993 {                                                1540 {
1994         int numslots = fattrs->maxreqs;          1541         int numslots = fattrs->maxreqs;
1995         int slotsize = slot_bytes(fattrs);       1542         int slotsize = slot_bytes(fattrs);
1996         struct nfsd4_session *new;               1543         struct nfsd4_session *new;
1997         int i;                                !! 1544         int mem, i;
1998                                                  1545 
1999         BUILD_BUG_ON(struct_size(new, se_slot !! 1546         BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
2000                      > PAGE_SIZE);            !! 1547                         + sizeof(struct nfsd4_session) > PAGE_SIZE);
                                                   >> 1548         mem = numslots * sizeof(struct nfsd4_slot *);
2001                                                  1549 
2002         new = kzalloc(struct_size(new, se_slo !! 1550         new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
2003         if (!new)                                1551         if (!new)
2004                 return NULL;                     1552                 return NULL;
2005         /* allocate each struct nfsd4_slot an    1553         /* allocate each struct nfsd4_slot and data cache in one piece */
2006         for (i = 0; i < numslots; i++) {         1554         for (i = 0; i < numslots; i++) {
2007                 new->se_slots[i] = kzalloc(sl    1555                 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
2008                 if (!new->se_slots[i])           1556                 if (!new->se_slots[i])
2009                         goto out_free;           1557                         goto out_free;
2010         }                                        1558         }
2011                                                  1559 
2012         memcpy(&new->se_fchannel, fattrs, siz    1560         memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
2013         memcpy(&new->se_bchannel, battrs, siz    1561         memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
2014                                                  1562 
2015         return new;                              1563         return new;
2016 out_free:                                        1564 out_free:
2017         while (i--)                              1565         while (i--)
2018                 kfree(new->se_slots[i]);         1566                 kfree(new->se_slots[i]);
2019         kfree(new);                              1567         kfree(new);
2020         return NULL;                             1568         return NULL;
2021 }                                                1569 }
2022                                                  1570 
2023 static void free_conn(struct nfsd4_conn *c)      1571 static void free_conn(struct nfsd4_conn *c)
2024 {                                                1572 {
2025         svc_xprt_put(c->cn_xprt);                1573         svc_xprt_put(c->cn_xprt);
2026         kfree(c);                                1574         kfree(c);
2027 }                                                1575 }
2028                                                  1576 
2029 static void nfsd4_conn_lost(struct svc_xpt_us    1577 static void nfsd4_conn_lost(struct svc_xpt_user *u)
2030 {                                                1578 {
2031         struct nfsd4_conn *c = container_of(u    1579         struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
2032         struct nfs4_client *clp = c->cn_sessi    1580         struct nfs4_client *clp = c->cn_session->se_client;
2033                                                  1581 
2034         trace_nfsd_cb_lost(clp);              << 
2035                                               << 
2036         spin_lock(&clp->cl_lock);                1582         spin_lock(&clp->cl_lock);
2037         if (!list_empty(&c->cn_persession)) {    1583         if (!list_empty(&c->cn_persession)) {
2038                 list_del(&c->cn_persession);     1584                 list_del(&c->cn_persession);
2039                 free_conn(c);                    1585                 free_conn(c);
2040         }                                        1586         }
2041         nfsd4_probe_callback(clp);               1587         nfsd4_probe_callback(clp);
2042         spin_unlock(&clp->cl_lock);              1588         spin_unlock(&clp->cl_lock);
2043 }                                                1589 }
2044                                                  1590 
2045 static struct nfsd4_conn *alloc_conn(struct s    1591 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
2046 {                                                1592 {
2047         struct nfsd4_conn *conn;                 1593         struct nfsd4_conn *conn;
2048                                                  1594 
2049         conn = kmalloc(sizeof(struct nfsd4_co    1595         conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
2050         if (!conn)                               1596         if (!conn)
2051                 return NULL;                     1597                 return NULL;
2052         svc_xprt_get(rqstp->rq_xprt);            1598         svc_xprt_get(rqstp->rq_xprt);
2053         conn->cn_xprt = rqstp->rq_xprt;          1599         conn->cn_xprt = rqstp->rq_xprt;
2054         conn->cn_flags = flags;                  1600         conn->cn_flags = flags;
2055         INIT_LIST_HEAD(&conn->cn_xpt_user.lis    1601         INIT_LIST_HEAD(&conn->cn_xpt_user.list);
2056         return conn;                             1602         return conn;
2057 }                                                1603 }
2058                                                  1604 
2059 static void __nfsd4_hash_conn(struct nfsd4_co    1605 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
2060 {                                                1606 {
2061         conn->cn_session = ses;                  1607         conn->cn_session = ses;
2062         list_add(&conn->cn_persession, &ses->    1608         list_add(&conn->cn_persession, &ses->se_conns);
2063 }                                                1609 }
2064                                                  1610 
2065 static void nfsd4_hash_conn(struct nfsd4_conn    1611 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
2066 {                                                1612 {
2067         struct nfs4_client *clp = ses->se_cli    1613         struct nfs4_client *clp = ses->se_client;
2068                                                  1614 
2069         spin_lock(&clp->cl_lock);                1615         spin_lock(&clp->cl_lock);
2070         __nfsd4_hash_conn(conn, ses);            1616         __nfsd4_hash_conn(conn, ses);
2071         spin_unlock(&clp->cl_lock);              1617         spin_unlock(&clp->cl_lock);
2072 }                                                1618 }
2073                                                  1619 
2074 static int nfsd4_register_conn(struct nfsd4_c    1620 static int nfsd4_register_conn(struct nfsd4_conn *conn)
2075 {                                                1621 {
2076         conn->cn_xpt_user.callback = nfsd4_co    1622         conn->cn_xpt_user.callback = nfsd4_conn_lost;
2077         return register_xpt_user(conn->cn_xpr    1623         return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
2078 }                                                1624 }
2079                                                  1625 
2080 static void nfsd4_init_conn(struct svc_rqst *    1626 static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
2081 {                                                1627 {
2082         int ret;                                 1628         int ret;
2083                                                  1629 
2084         nfsd4_hash_conn(conn, ses);              1630         nfsd4_hash_conn(conn, ses);
2085         ret = nfsd4_register_conn(conn);         1631         ret = nfsd4_register_conn(conn);
2086         if (ret)                                 1632         if (ret)
2087                 /* oops; xprt is already down    1633                 /* oops; xprt is already down: */
2088                 nfsd4_conn_lost(&conn->cn_xpt    1634                 nfsd4_conn_lost(&conn->cn_xpt_user);
2089         /* We may have gained or lost a callb    1635         /* We may have gained or lost a callback channel: */
2090         nfsd4_probe_callback_sync(ses->se_cli    1636         nfsd4_probe_callback_sync(ses->se_client);
2091 }                                                1637 }
2092                                                  1638 
2093 static struct nfsd4_conn *alloc_conn_from_crs    1639 static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
2094 {                                                1640 {
2095         u32 dir = NFS4_CDFC4_FORE;               1641         u32 dir = NFS4_CDFC4_FORE;
2096                                                  1642 
2097         if (cses->flags & SESSION4_BACK_CHAN)    1643         if (cses->flags & SESSION4_BACK_CHAN)
2098                 dir |= NFS4_CDFC4_BACK;          1644                 dir |= NFS4_CDFC4_BACK;
2099         return alloc_conn(rqstp, dir);           1645         return alloc_conn(rqstp, dir);
2100 }                                                1646 }
2101                                                  1647 
2102 /* must be called under client_lock */           1648 /* must be called under client_lock */
2103 static void nfsd4_del_conns(struct nfsd4_sess    1649 static void nfsd4_del_conns(struct nfsd4_session *s)
2104 {                                                1650 {
2105         struct nfs4_client *clp = s->se_clien    1651         struct nfs4_client *clp = s->se_client;
2106         struct nfsd4_conn *c;                    1652         struct nfsd4_conn *c;
2107                                                  1653 
2108         spin_lock(&clp->cl_lock);                1654         spin_lock(&clp->cl_lock);
2109         while (!list_empty(&s->se_conns)) {      1655         while (!list_empty(&s->se_conns)) {
2110                 c = list_first_entry(&s->se_c    1656                 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
2111                 list_del_init(&c->cn_persessi    1657                 list_del_init(&c->cn_persession);
2112                 spin_unlock(&clp->cl_lock);      1658                 spin_unlock(&clp->cl_lock);
2113                                                  1659 
2114                 unregister_xpt_user(c->cn_xpr    1660                 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
2115                 free_conn(c);                    1661                 free_conn(c);
2116                                                  1662 
2117                 spin_lock(&clp->cl_lock);        1663                 spin_lock(&clp->cl_lock);
2118         }                                        1664         }
2119         spin_unlock(&clp->cl_lock);              1665         spin_unlock(&clp->cl_lock);
2120 }                                                1666 }
2121                                                  1667 
2122 static void __free_session(struct nfsd4_sessi    1668 static void __free_session(struct nfsd4_session *ses)
2123 {                                                1669 {
2124         free_session_slots(ses);                 1670         free_session_slots(ses);
2125         kfree(ses);                              1671         kfree(ses);
2126 }                                                1672 }
2127                                                  1673 
2128 static void free_session(struct nfsd4_session    1674 static void free_session(struct nfsd4_session *ses)
2129 {                                                1675 {
2130         nfsd4_del_conns(ses);                    1676         nfsd4_del_conns(ses);
2131         nfsd4_put_drc_mem(&ses->se_fchannel);    1677         nfsd4_put_drc_mem(&ses->se_fchannel);
2132         __free_session(ses);                     1678         __free_session(ses);
2133 }                                                1679 }
2134                                                  1680 
2135 static void init_session(struct svc_rqst *rqs    1681 static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
2136 {                                                1682 {
2137         int idx;                                 1683         int idx;
2138         struct nfsd_net *nn = net_generic(SVC    1684         struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2139                                                  1685 
2140         new->se_client = clp;                    1686         new->se_client = clp;
2141         gen_sessionid(new);                      1687         gen_sessionid(new);
2142                                                  1688 
2143         INIT_LIST_HEAD(&new->se_conns);          1689         INIT_LIST_HEAD(&new->se_conns);
2144                                                  1690 
2145         new->se_cb_seq_nr = 1;                   1691         new->se_cb_seq_nr = 1;
2146         new->se_flags = cses->flags;             1692         new->se_flags = cses->flags;
2147         new->se_cb_prog = cses->callback_prog    1693         new->se_cb_prog = cses->callback_prog;
2148         new->se_cb_sec = cses->cb_sec;           1694         new->se_cb_sec = cses->cb_sec;
2149         atomic_set(&new->se_ref, 0);             1695         atomic_set(&new->se_ref, 0);
2150         idx = hash_sessionid(&new->se_session    1696         idx = hash_sessionid(&new->se_sessionid);
2151         list_add(&new->se_hash, &nn->sessioni    1697         list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
2152         spin_lock(&clp->cl_lock);                1698         spin_lock(&clp->cl_lock);
2153         list_add(&new->se_perclnt, &clp->cl_s    1699         list_add(&new->se_perclnt, &clp->cl_sessions);
2154         spin_unlock(&clp->cl_lock);              1700         spin_unlock(&clp->cl_lock);
2155                                                  1701 
2156         {                                        1702         {
2157                 struct sockaddr *sa = svc_add    1703                 struct sockaddr *sa = svc_addr(rqstp);
2158                 /*                               1704                 /*
2159                  * This is a little silly; wi    1705                  * This is a little silly; with sessions there's no real
2160                  * use for the callback addre    1706                  * use for the callback address.  Use the peer address
2161                  * as a reasonable default fo    1707                  * as a reasonable default for now, but consider fixing
2162                  * the rpc client not to requ    1708                  * the rpc client not to require an address in the
2163                  * future:                       1709                  * future:
2164                  */                              1710                  */
2165                 rpc_copy_addr((struct sockadd    1711                 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
2166                 clp->cl_cb_conn.cb_addrlen =     1712                 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
2167         }                                        1713         }
2168 }                                                1714 }
2169                                                  1715 
2170 /* caller must hold client_lock */               1716 /* caller must hold client_lock */
2171 static struct nfsd4_session *                    1717 static struct nfsd4_session *
2172 __find_in_sessionid_hashtbl(struct nfs4_sessi    1718 __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
2173 {                                                1719 {
2174         struct nfsd4_session *elem;              1720         struct nfsd4_session *elem;
2175         int idx;                                 1721         int idx;
2176         struct nfsd_net *nn = net_generic(net    1722         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2177                                                  1723 
2178         lockdep_assert_held(&nn->client_lock)    1724         lockdep_assert_held(&nn->client_lock);
2179                                                  1725 
2180         dump_sessionid(__func__, sessionid);     1726         dump_sessionid(__func__, sessionid);
2181         idx = hash_sessionid(sessionid);         1727         idx = hash_sessionid(sessionid);
2182         /* Search in the appropriate list */     1728         /* Search in the appropriate list */
2183         list_for_each_entry(elem, &nn->sessio    1729         list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
2184                 if (!memcmp(elem->se_sessioni    1730                 if (!memcmp(elem->se_sessionid.data, sessionid->data,
2185                             NFS4_MAX_SESSIONI    1731                             NFS4_MAX_SESSIONID_LEN)) {
2186                         return elem;             1732                         return elem;
2187                 }                                1733                 }
2188         }                                        1734         }
2189                                                  1735 
2190         dprintk("%s: session not found\n", __    1736         dprintk("%s: session not found\n", __func__);
2191         return NULL;                             1737         return NULL;
2192 }                                                1738 }
2193                                                  1739 
2194 static struct nfsd4_session *                    1740 static struct nfsd4_session *
2195 find_in_sessionid_hashtbl(struct nfs4_session    1741 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
2196                 __be32 *ret)                     1742                 __be32 *ret)
2197 {                                                1743 {
2198         struct nfsd4_session *session;           1744         struct nfsd4_session *session;
2199         __be32 status = nfserr_badsession;       1745         __be32 status = nfserr_badsession;
2200                                                  1746 
2201         session = __find_in_sessionid_hashtbl    1747         session = __find_in_sessionid_hashtbl(sessionid, net);
2202         if (!session)                            1748         if (!session)
2203                 goto out;                        1749                 goto out;
2204         status = nfsd4_get_session_locked(ses    1750         status = nfsd4_get_session_locked(session);
2205         if (status)                              1751         if (status)
2206                 session = NULL;                  1752                 session = NULL;
2207 out:                                             1753 out:
2208         *ret = status;                           1754         *ret = status;
2209         return session;                          1755         return session;
2210 }                                                1756 }
2211                                                  1757 
2212 /* caller must hold client_lock */               1758 /* caller must hold client_lock */
2213 static void                                      1759 static void
2214 unhash_session(struct nfsd4_session *ses)        1760 unhash_session(struct nfsd4_session *ses)
2215 {                                                1761 {
2216         struct nfs4_client *clp = ses->se_cli    1762         struct nfs4_client *clp = ses->se_client;
2217         struct nfsd_net *nn = net_generic(clp    1763         struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2218                                                  1764 
2219         lockdep_assert_held(&nn->client_lock)    1765         lockdep_assert_held(&nn->client_lock);
2220                                                  1766 
2221         list_del(&ses->se_hash);                 1767         list_del(&ses->se_hash);
2222         spin_lock(&ses->se_client->cl_lock);     1768         spin_lock(&ses->se_client->cl_lock);
2223         list_del(&ses->se_perclnt);              1769         list_del(&ses->se_perclnt);
2224         spin_unlock(&ses->se_client->cl_lock)    1770         spin_unlock(&ses->se_client->cl_lock);
2225 }                                                1771 }
2226                                                  1772 
2227 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper    1773 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
2228 static int                                       1774 static int
2229 STALE_CLIENTID(clientid_t *clid, struct nfsd_    1775 STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
2230 {                                                1776 {
2231         /*                                       1777         /*
2232          * We're assuming the clid was not gi    1778          * We're assuming the clid was not given out from a boot
2233          * precisely 2^32 (about 136 years) b    1779          * precisely 2^32 (about 136 years) before this one.  That seems
2234          * a safe assumption:                    1780          * a safe assumption:
2235          */                                      1781          */
2236         if (clid->cl_boot == (u32)nn->boot_ti    1782         if (clid->cl_boot == (u32)nn->boot_time)
2237                 return 0;                        1783                 return 0;
2238         trace_nfsd_clid_stale(clid);          !! 1784         dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
                                                   >> 1785                 clid->cl_boot, clid->cl_id, nn->boot_time);
2239         return 1;                                1786         return 1;
2240 }                                                1787 }
2241                                                  1788 
2242 /*                                               1789 /* 
2243  * XXX Should we use a slab cache ?              1790  * XXX Should we use a slab cache ?
2244  * This type of memory management is somewhat    1791  * This type of memory management is somewhat inefficient, but we use it
2245  * anyway since SETCLIENTID is not a common o    1792  * anyway since SETCLIENTID is not a common operation.
2246  */                                              1793  */
2247 static struct nfs4_client *alloc_client(struc !! 1794 static struct nfs4_client *alloc_client(struct xdr_netobj name)
2248                                 struct nfsd_n << 
2249 {                                                1795 {
2250         struct nfs4_client *clp;                 1796         struct nfs4_client *clp;
2251         int i;                                   1797         int i;
2252                                                  1798 
2253         if (atomic_read(&nn->nfs4_client_coun !! 1799         clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
2254                 mod_delayed_work(laundry_wq,  << 
2255                 return NULL;                  << 
2256         }                                     << 
2257         clp = kmem_cache_zalloc(client_slab,  << 
2258         if (clp == NULL)                         1800         if (clp == NULL)
2259                 return NULL;                     1801                 return NULL;
2260         xdr_netobj_dup(&clp->cl_name, &name,  !! 1802         clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
2261         if (clp->cl_name.data == NULL)           1803         if (clp->cl_name.data == NULL)
2262                 goto err_no_name;                1804                 goto err_no_name;
2263         clp->cl_ownerstr_hashtbl = kmalloc_ar !! 1805         clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
2264                                               !! 1806                         OWNER_HASH_SIZE, GFP_KERNEL);
2265                                               << 
2266         if (!clp->cl_ownerstr_hashtbl)           1807         if (!clp->cl_ownerstr_hashtbl)
2267                 goto err_no_hashtbl;             1808                 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++)    1809         for (i = 0; i < OWNER_HASH_SIZE; i++)
2273                 INIT_LIST_HEAD(&clp->cl_owner    1810                 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
                                                   >> 1811         clp->cl_name.len = name.len;
2274         INIT_LIST_HEAD(&clp->cl_sessions);       1812         INIT_LIST_HEAD(&clp->cl_sessions);
2275         idr_init(&clp->cl_stateids);             1813         idr_init(&clp->cl_stateids);
2276         atomic_set(&clp->cl_rpc_users, 0);    !! 1814         atomic_set(&clp->cl_refcount, 0);
2277         clp->cl_cb_state = NFSD4_CB_UNKNOWN;     1815         clp->cl_cb_state = NFSD4_CB_UNKNOWN;
2278         clp->cl_state = NFSD4_ACTIVE;         << 
2279         atomic_inc(&nn->nfs4_client_count);   << 
2280         atomic_set(&clp->cl_delegs_in_recall, << 
2281         INIT_LIST_HEAD(&clp->cl_idhash);         1816         INIT_LIST_HEAD(&clp->cl_idhash);
2282         INIT_LIST_HEAD(&clp->cl_openowners);     1817         INIT_LIST_HEAD(&clp->cl_openowners);
2283         INIT_LIST_HEAD(&clp->cl_delegations);    1818         INIT_LIST_HEAD(&clp->cl_delegations);
2284         INIT_LIST_HEAD(&clp->cl_lru);            1819         INIT_LIST_HEAD(&clp->cl_lru);
2285         INIT_LIST_HEAD(&clp->cl_revoked);        1820         INIT_LIST_HEAD(&clp->cl_revoked);
2286 #ifdef CONFIG_NFSD_PNFS                          1821 #ifdef CONFIG_NFSD_PNFS
2287         INIT_LIST_HEAD(&clp->cl_lo_states);      1822         INIT_LIST_HEAD(&clp->cl_lo_states);
2288 #endif                                           1823 #endif
2289         INIT_LIST_HEAD(&clp->async_copies);   << 
2290         spin_lock_init(&clp->async_lock);     << 
2291         spin_lock_init(&clp->cl_lock);           1824         spin_lock_init(&clp->cl_lock);
2292         rpc_init_wait_queue(&clp->cl_cb_waitq    1825         rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
2293         return clp;                              1826         return clp;
2294 err_no_callback_wq:                           << 
2295         kfree(clp->cl_ownerstr_hashtbl);      << 
2296 err_no_hashtbl:                                  1827 err_no_hashtbl:
2297         kfree(clp->cl_name.data);                1828         kfree(clp->cl_name.data);
2298 err_no_name:                                     1829 err_no_name:
2299         kmem_cache_free(client_slab, clp);    !! 1830         kfree(clp);
2300         return NULL;                             1831         return NULL;
2301 }                                                1832 }
2302                                                  1833 
2303 static void __free_client(struct kref *k)     << 
2304 {                                             << 
2305         struct nfsdfs_client *c = container_o << 
2306         struct nfs4_client *clp = container_o << 
2307                                               << 
2308         free_svc_cred(&clp->cl_cred);         << 
2309         destroy_workqueue(clp->cl_callback_wq << 
2310         kfree(clp->cl_ownerstr_hashtbl);      << 
2311         kfree(clp->cl_name.data);             << 
2312         kfree(clp->cl_nii_domain.data);       << 
2313         kfree(clp->cl_nii_name.data);         << 
2314         idr_destroy(&clp->cl_stateids);       << 
2315         kfree(clp->cl_ra);                    << 
2316         kmem_cache_free(client_slab, clp);    << 
2317 }                                             << 
2318                                               << 
2319 static void drop_client(struct nfs4_client *c << 
2320 {                                             << 
2321         kref_put(&clp->cl_nfsdfs.cl_ref, __fr << 
2322 }                                             << 
2323                                               << 
2324 static void                                      1834 static void
2325 free_client(struct nfs4_client *clp)             1835 free_client(struct nfs4_client *clp)
2326 {                                                1836 {
2327         while (!list_empty(&clp->cl_sessions)    1837         while (!list_empty(&clp->cl_sessions)) {
2328                 struct nfsd4_session *ses;       1838                 struct nfsd4_session *ses;
2329                 ses = list_entry(clp->cl_sess    1839                 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
2330                                 se_perclnt);     1840                                 se_perclnt);
2331                 list_del(&ses->se_perclnt);      1841                 list_del(&ses->se_perclnt);
2332                 WARN_ON_ONCE(atomic_read(&ses    1842                 WARN_ON_ONCE(atomic_read(&ses->se_ref));
2333                 free_session(ses);               1843                 free_session(ses);
2334         }                                        1844         }
2335         rpc_destroy_wait_queue(&clp->cl_cb_wa    1845         rpc_destroy_wait_queue(&clp->cl_cb_waitq);
2336         if (clp->cl_nfsd_dentry) {            !! 1846         free_svc_cred(&clp->cl_cred);
2337                 nfsd_client_rmdir(clp->cl_nfs !! 1847         kfree(clp->cl_ownerstr_hashtbl);
2338                 clp->cl_nfsd_dentry = NULL;   !! 1848         kfree(clp->cl_name.data);
2339                 wake_up_all(&expiry_wq);      !! 1849         idr_destroy(&clp->cl_stateids);
2340         }                                     !! 1850         kfree(clp);
2341         drop_client(clp);                     << 
2342 }                                                1851 }
2343                                                  1852 
2344 /* must be called under the client_lock */       1853 /* must be called under the client_lock */
2345 static void                                      1854 static void
2346 unhash_client_locked(struct nfs4_client *clp)    1855 unhash_client_locked(struct nfs4_client *clp)
2347 {                                                1856 {
2348         struct nfsd_net *nn = net_generic(clp    1857         struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2349         struct nfsd4_session *ses;               1858         struct nfsd4_session *ses;
2350                                                  1859 
2351         lockdep_assert_held(&nn->client_lock)    1860         lockdep_assert_held(&nn->client_lock);
2352                                                  1861 
2353         /* Mark the client as expired! */        1862         /* Mark the client as expired! */
2354         clp->cl_time = 0;                        1863         clp->cl_time = 0;
2355         /* Make it invisible */                  1864         /* Make it invisible */
2356         if (!list_empty(&clp->cl_idhash)) {      1865         if (!list_empty(&clp->cl_idhash)) {
2357                 list_del_init(&clp->cl_idhash    1866                 list_del_init(&clp->cl_idhash);
2358                 if (test_bit(NFSD4_CLIENT_CON    1867                 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
2359                         rb_erase(&clp->cl_nam    1868                         rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
2360                 else                             1869                 else
2361                         rb_erase(&clp->cl_nam    1870                         rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
2362         }                                        1871         }
2363         list_del_init(&clp->cl_lru);             1872         list_del_init(&clp->cl_lru);
2364         spin_lock(&clp->cl_lock);                1873         spin_lock(&clp->cl_lock);
2365         list_for_each_entry(ses, &clp->cl_ses    1874         list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
2366                 list_del_init(&ses->se_hash);    1875                 list_del_init(&ses->se_hash);
2367         spin_unlock(&clp->cl_lock);              1876         spin_unlock(&clp->cl_lock);
2368 }                                                1877 }
2369                                                  1878 
2370 static void                                      1879 static void
2371 unhash_client(struct nfs4_client *clp)           1880 unhash_client(struct nfs4_client *clp)
2372 {                                                1881 {
2373         struct nfsd_net *nn = net_generic(clp    1882         struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2374                                                  1883 
2375         spin_lock(&nn->client_lock);             1884         spin_lock(&nn->client_lock);
2376         unhash_client_locked(clp);               1885         unhash_client_locked(clp);
2377         spin_unlock(&nn->client_lock);           1886         spin_unlock(&nn->client_lock);
2378 }                                                1887 }
2379                                                  1888 
2380 static __be32 mark_client_expired_locked(stru    1889 static __be32 mark_client_expired_locked(struct nfs4_client *clp)
2381 {                                                1890 {
2382         int users = atomic_read(&clp->cl_rpc_ !! 1891         if (atomic_read(&clp->cl_refcount))
2383                                               << 
2384         trace_nfsd_mark_client_expired(clp, u << 
2385                                               << 
2386         if (users)                            << 
2387                 return nfserr_jukebox;           1892                 return nfserr_jukebox;
2388         unhash_client_locked(clp);               1893         unhash_client_locked(clp);
2389         return nfs_ok;                           1894         return nfs_ok;
2390 }                                                1895 }
2391                                                  1896 
2392 static void                                      1897 static void
2393 __destroy_client(struct nfs4_client *clp)        1898 __destroy_client(struct nfs4_client *clp)
2394 {                                                1899 {
2395         struct nfsd_net *nn = net_generic(clp << 
2396         int i;                                   1900         int i;
2397         struct nfs4_openowner *oo;               1901         struct nfs4_openowner *oo;
2398         struct nfs4_delegation *dp;              1902         struct nfs4_delegation *dp;
2399         LIST_HEAD(reaplist);                  !! 1903         struct list_head reaplist;
2400                                                  1904 
                                                   >> 1905         INIT_LIST_HEAD(&reaplist);
2401         spin_lock(&state_lock);                  1906         spin_lock(&state_lock);
2402         while (!list_empty(&clp->cl_delegatio    1907         while (!list_empty(&clp->cl_delegations)) {
2403                 dp = list_entry(clp->cl_deleg    1908                 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
2404                 unhash_delegation_locked(dp,  !! 1909                 WARN_ON(!unhash_delegation_locked(dp));
2405                 list_add(&dp->dl_recall_lru,     1910                 list_add(&dp->dl_recall_lru, &reaplist);
2406         }                                        1911         }
2407         spin_unlock(&state_lock);                1912         spin_unlock(&state_lock);
2408         while (!list_empty(&reaplist)) {         1913         while (!list_empty(&reaplist)) {
2409                 dp = list_entry(reaplist.next    1914                 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
2410                 list_del_init(&dp->dl_recall_    1915                 list_del_init(&dp->dl_recall_lru);
2411                 destroy_unhashed_deleg(dp);   !! 1916                 put_clnt_odstate(dp->dl_clnt_odstate);
                                                   >> 1917                 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
                                                   >> 1918                 nfs4_put_stid(&dp->dl_stid);
2412         }                                        1919         }
2413         while (!list_empty(&clp->cl_revoked))    1920         while (!list_empty(&clp->cl_revoked)) {
2414                 dp = list_entry(clp->cl_revok    1921                 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
2415                 list_del_init(&dp->dl_recall_    1922                 list_del_init(&dp->dl_recall_lru);
2416                 nfs4_put_stid(&dp->dl_stid);     1923                 nfs4_put_stid(&dp->dl_stid);
2417         }                                        1924         }
2418         while (!list_empty(&clp->cl_openowner    1925         while (!list_empty(&clp->cl_openowners)) {
2419                 oo = list_entry(clp->cl_openo    1926                 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
2420                 nfs4_get_stateowner(&oo->oo_o    1927                 nfs4_get_stateowner(&oo->oo_owner);
2421                 release_openowner(oo);           1928                 release_openowner(oo);
2422         }                                        1929         }
2423         for (i = 0; i < OWNER_HASH_SIZE; i++)    1930         for (i = 0; i < OWNER_HASH_SIZE; i++) {
2424                 struct nfs4_stateowner *so, *    1931                 struct nfs4_stateowner *so, *tmp;
2425                                                  1932 
2426                 list_for_each_entry_safe(so,     1933                 list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i],
2427                                          so_s    1934                                          so_strhash) {
2428                         /* Should be no openo    1935                         /* Should be no openowners at this point */
2429                         WARN_ON_ONCE(so->so_i    1936                         WARN_ON_ONCE(so->so_is_open_owner);
2430                         remove_blocked_locks(    1937                         remove_blocked_locks(lockowner(so));
2431                 }                                1938                 }
2432         }                                        1939         }
2433         nfsd4_return_all_client_layouts(clp);    1940         nfsd4_return_all_client_layouts(clp);
2434         nfsd4_shutdown_copy(clp);             << 
2435         nfsd4_shutdown_callback(clp);            1941         nfsd4_shutdown_callback(clp);
2436         if (clp->cl_cb_conn.cb_xprt)             1942         if (clp->cl_cb_conn.cb_xprt)
2437                 svc_xprt_put(clp->cl_cb_conn.    1943                 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
2438         atomic_add_unless(&nn->nfs4_client_co << 
2439         nfsd4_dec_courtesy_client_count(nn, c << 
2440         free_client(clp);                        1944         free_client(clp);
2441         wake_up_all(&expiry_wq);              << 
2442 }                                                1945 }
2443                                                  1946 
2444 static void                                      1947 static void
2445 destroy_client(struct nfs4_client *clp)          1948 destroy_client(struct nfs4_client *clp)
2446 {                                                1949 {
2447         unhash_client(clp);                      1950         unhash_client(clp);
2448         __destroy_client(clp);                   1951         __destroy_client(clp);
2449 }                                                1952 }
2450                                                  1953 
2451 static void inc_reclaim_complete(struct nfs4_ << 
2452 {                                             << 
2453         struct nfsd_net *nn = net_generic(clp << 
2454                                               << 
2455         if (!nn->track_reclaim_completes)     << 
2456                 return;                       << 
2457         if (!nfsd4_find_reclaim_client(clp->c << 
2458                 return;                       << 
2459         if (atomic_inc_return(&nn->nr_reclaim << 
2460                         nn->reclaim_str_hasht << 
2461                 printk(KERN_INFO "NFSD: all c << 
2462                                 clp->net->ns. << 
2463                 nfsd4_end_grace(nn);          << 
2464         }                                     << 
2465 }                                             << 
2466                                               << 
2467 static void expire_client(struct nfs4_client     1954 static void expire_client(struct nfs4_client *clp)
2468 {                                                1955 {
2469         unhash_client(clp);                      1956         unhash_client(clp);
2470         nfsd4_client_record_remove(clp);         1957         nfsd4_client_record_remove(clp);
2471         __destroy_client(clp);                   1958         __destroy_client(clp);
2472 }                                                1959 }
2473                                                  1960 
2474 static void copy_verf(struct nfs4_client *tar    1961 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
2475 {                                                1962 {
2476         memcpy(target->cl_verifier.data, sour    1963         memcpy(target->cl_verifier.data, source->data,
2477                         sizeof(target->cl_ver    1964                         sizeof(target->cl_verifier.data));
2478 }                                                1965 }
2479                                                  1966 
2480 static void copy_clid(struct nfs4_client *tar    1967 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
2481 {                                                1968 {
2482         target->cl_clientid.cl_boot = source-    1969         target->cl_clientid.cl_boot = source->cl_clientid.cl_boot; 
2483         target->cl_clientid.cl_id = source->c    1970         target->cl_clientid.cl_id = source->cl_clientid.cl_id; 
2484 }                                                1971 }
2485                                                  1972 
                                                   >> 1973 int strdup_if_nonnull(char **target, char *source)
                                                   >> 1974 {
                                                   >> 1975         if (source) {
                                                   >> 1976                 *target = kstrdup(source, GFP_KERNEL);
                                                   >> 1977                 if (!*target)
                                                   >> 1978                         return -ENOMEM;
                                                   >> 1979         } else
                                                   >> 1980                 *target = NULL;
                                                   >> 1981         return 0;
                                                   >> 1982 }
                                                   >> 1983 
2486 static int copy_cred(struct svc_cred *target,    1984 static int copy_cred(struct svc_cred *target, struct svc_cred *source)
2487 {                                                1985 {
2488         target->cr_principal = kstrdup(source !! 1986         int ret;
2489         target->cr_raw_principal = kstrdup(so << 
2490                                               << 
2491         target->cr_targ_princ = kstrdup(sourc << 
2492         if ((source->cr_principal && !target- << 
2493             (source->cr_raw_principal && !tar << 
2494             (source->cr_targ_princ && !target << 
2495                 return -ENOMEM;               << 
2496                                                  1987 
                                                   >> 1988         ret = strdup_if_nonnull(&target->cr_principal, source->cr_principal);
                                                   >> 1989         if (ret)
                                                   >> 1990                 return ret;
                                                   >> 1991         ret = strdup_if_nonnull(&target->cr_raw_principal,
                                                   >> 1992                                         source->cr_raw_principal);
                                                   >> 1993         if (ret)
                                                   >> 1994                 return ret;
2497         target->cr_flavor = source->cr_flavor    1995         target->cr_flavor = source->cr_flavor;
2498         target->cr_uid = source->cr_uid;         1996         target->cr_uid = source->cr_uid;
2499         target->cr_gid = source->cr_gid;         1997         target->cr_gid = source->cr_gid;
2500         target->cr_group_info = source->cr_gr    1998         target->cr_group_info = source->cr_group_info;
2501         get_group_info(target->cr_group_info)    1999         get_group_info(target->cr_group_info);
2502         target->cr_gss_mech = source->cr_gss_    2000         target->cr_gss_mech = source->cr_gss_mech;
2503         if (source->cr_gss_mech)                 2001         if (source->cr_gss_mech)
2504                 gss_mech_get(source->cr_gss_m    2002                 gss_mech_get(source->cr_gss_mech);
2505         return 0;                                2003         return 0;
2506 }                                                2004 }
2507                                                  2005 
2508 static int                                       2006 static int
2509 compare_blob(const struct xdr_netobj *o1, con    2007 compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
2510 {                                                2008 {
2511         if (o1->len < o2->len)                   2009         if (o1->len < o2->len)
2512                 return -1;                       2010                 return -1;
2513         if (o1->len > o2->len)                   2011         if (o1->len > o2->len)
2514                 return 1;                        2012                 return 1;
2515         return memcmp(o1->data, o2->data, o1-    2013         return memcmp(o1->data, o2->data, o1->len);
2516 }                                                2014 }
2517                                                  2015 
                                                   >> 2016 static int same_name(const char *n1, const char *n2)
                                                   >> 2017 {
                                                   >> 2018         return 0 == memcmp(n1, n2, HEXDIR_LEN);
                                                   >> 2019 }
                                                   >> 2020 
2518 static int                                       2021 static int
2519 same_verf(nfs4_verifier *v1, nfs4_verifier *v    2022 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
2520 {                                                2023 {
2521         return 0 == memcmp(v1->data, v2->data    2024         return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
2522 }                                                2025 }
2523                                                  2026 
2524 static int                                       2027 static int
2525 same_clid(clientid_t *cl1, clientid_t *cl2)      2028 same_clid(clientid_t *cl1, clientid_t *cl2)
2526 {                                                2029 {
2527         return (cl1->cl_boot == cl2->cl_boot)    2030         return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
2528 }                                                2031 }
2529                                                  2032 
2530 static bool groups_equal(struct group_info *g    2033 static bool groups_equal(struct group_info *g1, struct group_info *g2)
2531 {                                                2034 {
2532         int i;                                   2035         int i;
2533                                                  2036 
2534         if (g1->ngroups != g2->ngroups)          2037         if (g1->ngroups != g2->ngroups)
2535                 return false;                    2038                 return false;
2536         for (i=0; i<g1->ngroups; i++)            2039         for (i=0; i<g1->ngroups; i++)
2537                 if (!gid_eq(g1->gid[i], g2->g    2040                 if (!gid_eq(g1->gid[i], g2->gid[i]))
2538                         return false;            2041                         return false;
2539         return true;                             2042         return true;
2540 }                                                2043 }
2541                                                  2044 
2542 /*                                               2045 /*
2543  * RFC 3530 language requires clid_inuse be r    2046  * RFC 3530 language requires clid_inuse be returned when the
2544  * "principal" associated with a requests dif    2047  * "principal" associated with a requests differs from that previously
2545  * used.  We use uid, gid's, and gss principa    2048  * used.  We use uid, gid's, and gss principal string as our best
2546  * approximation.  We also don't want to allo    2049  * approximation.  We also don't want to allow non-gss use of a client
2547  * established using gss: in theory cr_princi    2050  * established using gss: in theory cr_principal should catch that
2548  * change, but in practice cr_principal can b    2051  * change, but in practice cr_principal can be null even in the gss case
2549  * since gssd doesn't always pass down a prin    2052  * since gssd doesn't always pass down a principal string.
2550  */                                              2053  */
2551 static bool is_gss_cred(struct svc_cred *cr)     2054 static bool is_gss_cred(struct svc_cred *cr)
2552 {                                                2055 {
2553         /* Is cr_flavor one of the gss "pseud    2056         /* Is cr_flavor one of the gss "pseudoflavors"?: */
2554         return (cr->cr_flavor > RPC_AUTH_MAXF    2057         return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
2555 }                                                2058 }
2556                                                  2059 
2557                                                  2060 
2558 static bool                                      2061 static bool
2559 same_creds(struct svc_cred *cr1, struct svc_c    2062 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
2560 {                                                2063 {
2561         if ((is_gss_cred(cr1) != is_gss_cred(    2064         if ((is_gss_cred(cr1) != is_gss_cred(cr2))
2562                 || (!uid_eq(cr1->cr_uid, cr2-    2065                 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
2563                 || (!gid_eq(cr1->cr_gid, cr2-    2066                 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
2564                 || !groups_equal(cr1->cr_grou    2067                 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
2565                 return false;                    2068                 return false;
2566         /* XXX: check that cr_targ_princ fiel << 
2567         if (cr1->cr_principal == cr2->cr_prin    2069         if (cr1->cr_principal == cr2->cr_principal)
2568                 return true;                     2070                 return true;
2569         if (!cr1->cr_principal || !cr2->cr_pr    2071         if (!cr1->cr_principal || !cr2->cr_principal)
2570                 return false;                    2072                 return false;
2571         return 0 == strcmp(cr1->cr_principal,    2073         return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
2572 }                                                2074 }
2573                                                  2075 
2574 static bool svc_rqst_integrity_protected(stru    2076 static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
2575 {                                                2077 {
2576         struct svc_cred *cr = &rqstp->rq_cred    2078         struct svc_cred *cr = &rqstp->rq_cred;
2577         u32 service;                             2079         u32 service;
2578                                                  2080 
2579         if (!cr->cr_gss_mech)                    2081         if (!cr->cr_gss_mech)
2580                 return false;                    2082                 return false;
2581         service = gss_pseudoflavor_to_service    2083         service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
2582         return service == RPC_GSS_SVC_INTEGRI    2084         return service == RPC_GSS_SVC_INTEGRITY ||
2583                service == RPC_GSS_SVC_PRIVACY    2085                service == RPC_GSS_SVC_PRIVACY;
2584 }                                                2086 }
2585                                                  2087 
2586 bool nfsd4_mach_creds_match(struct nfs4_clien    2088 bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
2587 {                                                2089 {
2588         struct svc_cred *cr = &rqstp->rq_cred    2090         struct svc_cred *cr = &rqstp->rq_cred;
2589                                                  2091 
2590         if (!cl->cl_mach_cred)                   2092         if (!cl->cl_mach_cred)
2591                 return true;                     2093                 return true;
2592         if (cl->cl_cred.cr_gss_mech != cr->cr    2094         if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
2593                 return false;                    2095                 return false;
2594         if (!svc_rqst_integrity_protected(rqs    2096         if (!svc_rqst_integrity_protected(rqstp))
2595                 return false;                    2097                 return false;
2596         if (cl->cl_cred.cr_raw_principal)        2098         if (cl->cl_cred.cr_raw_principal)
2597                 return 0 == strcmp(cl->cl_cre    2099                 return 0 == strcmp(cl->cl_cred.cr_raw_principal,
2598                                                  2100                                                 cr->cr_raw_principal);
2599         if (!cr->cr_principal)                   2101         if (!cr->cr_principal)
2600                 return false;                    2102                 return false;
2601         return 0 == strcmp(cl->cl_cred.cr_pri    2103         return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
2602 }                                                2104 }
2603                                                  2105 
2604 static void gen_confirm(struct nfs4_client *c    2106 static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
2605 {                                                2107 {
2606         __be32 verf[2];                          2108         __be32 verf[2];
2607                                                  2109 
2608         /*                                       2110         /*
2609          * This is opaque to client, so no ne    2111          * This is opaque to client, so no need to byte-swap. Use
2610          * __force to keep sparse happy          2112          * __force to keep sparse happy
2611          */                                      2113          */
2612         verf[0] = (__force __be32)(u32)ktime_ !! 2114         verf[0] = (__force __be32)get_seconds();
2613         verf[1] = (__force __be32)nn->clverif    2115         verf[1] = (__force __be32)nn->clverifier_counter++;
2614         memcpy(clp->cl_confirm.data, verf, si    2116         memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
2615 }                                                2117 }
2616                                                  2118 
2617 static void gen_clid(struct nfs4_client *clp,    2119 static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
2618 {                                                2120 {
2619         clp->cl_clientid.cl_boot = (u32)nn->b !! 2121         clp->cl_clientid.cl_boot = nn->boot_time;
2620         clp->cl_clientid.cl_id = nn->clientid    2122         clp->cl_clientid.cl_id = nn->clientid_counter++;
2621         gen_confirm(clp, nn);                    2123         gen_confirm(clp, nn);
2622 }                                                2124 }
2623                                                  2125 
2624 static struct nfs4_stid *                        2126 static struct nfs4_stid *
2625 find_stateid_locked(struct nfs4_client *cl, s    2127 find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
2626 {                                                2128 {
2627         struct nfs4_stid *ret;                   2129         struct nfs4_stid *ret;
2628                                                  2130 
2629         ret = idr_find(&cl->cl_stateids, t->s    2131         ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
2630         if (!ret || !ret->sc_type)               2132         if (!ret || !ret->sc_type)
2631                 return NULL;                     2133                 return NULL;
2632         return ret;                              2134         return ret;
2633 }                                                2135 }
2634                                                  2136 
2635 static struct nfs4_stid *                        2137 static struct nfs4_stid *
2636 find_stateid_by_type(struct nfs4_client *cl,  !! 2138 find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
2637                      unsigned short typemask, << 
2638 {                                                2139 {
2639         struct nfs4_stid *s;                     2140         struct nfs4_stid *s;
2640                                                  2141 
2641         spin_lock(&cl->cl_lock);                 2142         spin_lock(&cl->cl_lock);
2642         s = find_stateid_locked(cl, t);          2143         s = find_stateid_locked(cl, t);
2643         if (s != NULL) {                         2144         if (s != NULL) {
2644                 if ((s->sc_status & ~ok_state !! 2145                 if (typemask & s->sc_type)
2645                     (typemask & s->sc_type))  !! 2146                         atomic_inc(&s->sc_count);
2646                         refcount_inc(&s->sc_c << 
2647                 else                             2147                 else
2648                         s = NULL;                2148                         s = NULL;
2649         }                                        2149         }
2650         spin_unlock(&cl->cl_lock);               2150         spin_unlock(&cl->cl_lock);
2651         return s;                                2151         return s;
2652 }                                                2152 }
2653                                                  2153 
2654 static struct nfs4_client *get_nfsdfs_clp(str << 
2655 {                                             << 
2656         struct nfsdfs_client *nc;             << 
2657         nc = get_nfsdfs_client(inode);        << 
2658         if (!nc)                              << 
2659                 return NULL;                  << 
2660         return container_of(nc, struct nfs4_c << 
2661 }                                             << 
2662                                               << 
2663 static void seq_quote_mem(struct seq_file *m, << 
2664 {                                             << 
2665         seq_puts(m, "\"");                    << 
2666         seq_escape_mem(m, data, len, ESCAPE_H << 
2667         seq_puts(m, "\"");                    << 
2668 }                                             << 
2669                                               << 
2670 static const char *cb_state2str(int state)    << 
2671 {                                             << 
2672         switch (state) {                      << 
2673         case NFSD4_CB_UP:                     << 
2674                 return "UP";                  << 
2675         case NFSD4_CB_UNKNOWN:                << 
2676                 return "UNKNOWN";             << 
2677         case NFSD4_CB_DOWN:                   << 
2678                 return "DOWN";                << 
2679         case NFSD4_CB_FAULT:                  << 
2680                 return "FAULT";               << 
2681         }                                     << 
2682         return "UNDEFINED";                   << 
2683 }                                             << 
2684                                               << 
2685 static int client_info_show(struct seq_file * << 
2686 {                                             << 
2687         struct inode *inode = file_inode(m->f << 
2688         struct nfs4_client *clp;              << 
2689         u64 clid;                             << 
2690                                               << 
2691         clp = get_nfsdfs_clp(inode);          << 
2692         if (!clp)                             << 
2693                 return -ENXIO;                << 
2694         memcpy(&clid, &clp->cl_clientid, size << 
2695         seq_printf(m, "clientid: 0x%llx\n", c << 
2696         seq_printf(m, "address: \"%pISpc\"\n" << 
2697                                               << 
2698         if (clp->cl_state == NFSD4_COURTESY)  << 
2699                 seq_puts(m, "status: courtesy << 
2700         else if (clp->cl_state == NFSD4_EXPIR << 
2701                 seq_puts(m, "status: expirabl << 
2702         else if (test_bit(NFSD4_CLIENT_CONFIR << 
2703                 seq_puts(m, "status: confirme << 
2704         else                                  << 
2705                 seq_puts(m, "status: unconfir << 
2706         seq_printf(m, "seconds from last rene << 
2707                 ktime_get_boottime_seconds()  << 
2708         seq_puts(m, "name: ");                << 
2709         seq_quote_mem(m, clp->cl_name.data, c << 
2710         seq_printf(m, "\nminor version: %d\n" << 
2711         if (clp->cl_nii_domain.data) {        << 
2712                 seq_puts(m, "Implementation d << 
2713                 seq_quote_mem(m, clp->cl_nii_ << 
2714                                         clp-> << 
2715                 seq_puts(m, "\nImplementation << 
2716                 seq_quote_mem(m, clp->cl_nii_ << 
2717                 seq_printf(m, "\nImplementati << 
2718                         clp->cl_nii_time.tv_s << 
2719         }                                     << 
2720         seq_printf(m, "callback state: %s\n", << 
2721         seq_printf(m, "callback address: \"%p << 
2722         seq_printf(m, "admin-revoked states:  << 
2723                    atomic_read(&clp->cl_admin << 
2724         drop_client(clp);                     << 
2725                                               << 
2726         return 0;                             << 
2727 }                                             << 
2728                                               << 
2729 DEFINE_SHOW_ATTRIBUTE(client_info);           << 
2730                                               << 
2731 static void *states_start(struct seq_file *s, << 
2732         __acquires(&clp->cl_lock)             << 
2733 {                                             << 
2734         struct nfs4_client *clp = s->private; << 
2735         unsigned long id = *pos;              << 
2736         void *ret;                            << 
2737                                               << 
2738         spin_lock(&clp->cl_lock);             << 
2739         ret = idr_get_next_ul(&clp->cl_statei << 
2740         *pos = id;                            << 
2741         return ret;                           << 
2742 }                                             << 
2743                                               << 
2744 static void *states_next(struct seq_file *s,  << 
2745 {                                             << 
2746         struct nfs4_client *clp = s->private; << 
2747         unsigned long id = *pos;              << 
2748         void *ret;                            << 
2749                                               << 
2750         id = *pos;                            << 
2751         id++;                                 << 
2752         ret = idr_get_next_ul(&clp->cl_statei << 
2753         *pos = id;                            << 
2754         return ret;                           << 
2755 }                                             << 
2756                                               << 
2757 static void states_stop(struct seq_file *s, v << 
2758         __releases(&clp->cl_lock)             << 
2759 {                                             << 
2760         struct nfs4_client *clp = s->private; << 
2761                                               << 
2762         spin_unlock(&clp->cl_lock);           << 
2763 }                                             << 
2764                                               << 
2765 static void nfs4_show_fname(struct seq_file * << 
2766 {                                             << 
2767          seq_printf(s, "filename: \"%pD2\"",  << 
2768 }                                             << 
2769                                               << 
2770 static void nfs4_show_superblock(struct seq_f << 
2771 {                                             << 
2772         struct inode *inode = file_inode(f->n << 
2773                                               << 
2774         seq_printf(s, "superblock: \"%02x:%02 << 
2775                                         MAJOR << 
2776                                          MINO << 
2777                                          inod << 
2778 }                                             << 
2779                                               << 
2780 static void nfs4_show_owner(struct seq_file * << 
2781 {                                             << 
2782         seq_puts(s, "owner: ");               << 
2783         seq_quote_mem(s, oo->so_owner.data, o << 
2784 }                                             << 
2785                                               << 
2786 static void nfs4_show_stateid(struct seq_file << 
2787 {                                             << 
2788         seq_printf(s, "0x%.8x", stid->si_gene << 
2789         seq_printf(s, "%12phN", &stid->si_opa << 
2790 }                                             << 
2791                                               << 
2792 static int nfs4_show_open(struct seq_file *s, << 
2793 {                                             << 
2794         struct nfs4_ol_stateid *ols;          << 
2795         struct nfs4_file *nf;                 << 
2796         struct nfsd_file *file;               << 
2797         struct nfs4_stateowner *oo;           << 
2798         unsigned int access, deny;            << 
2799                                               << 
2800         ols = openlockstateid(st);            << 
2801         oo = ols->st_stateowner;              << 
2802         nf = st->sc_file;                     << 
2803                                               << 
2804         seq_puts(s, "- ");                    << 
2805         nfs4_show_stateid(s, &st->sc_stateid) << 
2806         seq_puts(s, ": { type: open, ");      << 
2807                                               << 
2808         access = bmap_to_share_mode(ols->st_a << 
2809         deny   = bmap_to_share_mode(ols->st_d << 
2810                                               << 
2811         seq_printf(s, "access: %s%s, ",       << 
2812                 access & NFS4_SHARE_ACCESS_RE << 
2813                 access & NFS4_SHARE_ACCESS_WR << 
2814         seq_printf(s, "deny: %s%s, ",         << 
2815                 deny & NFS4_SHARE_ACCESS_READ << 
2816                 deny & NFS4_SHARE_ACCESS_WRIT << 
2817                                               << 
2818         if (nf) {                             << 
2819                 spin_lock(&nf->fi_lock);      << 
2820                 file = find_any_file_locked(n << 
2821                 if (file) {                   << 
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);               << 
2831         if (st->sc_status & SC_STATUS_ADMIN_R << 
2832                 seq_puts(s, ", admin-revoked" << 
2833         seq_puts(s, " }\n");                  << 
2834         return 0;                             << 
2835 }                                             << 
2836                                               << 
2837 static int nfs4_show_lock(struct seq_file *s, << 
2838 {                                             << 
2839         struct nfs4_ol_stateid *ols;          << 
2840         struct nfs4_file *nf;                 << 
2841         struct nfsd_file *file;               << 
2842         struct nfs4_stateowner *oo;           << 
2843                                               << 
2844         ols = openlockstateid(st);            << 
2845         oo = ols->st_stateowner;              << 
2846         nf = st->sc_file;                     << 
2847                                               << 
2848         seq_puts(s, "- ");                    << 
2849         nfs4_show_stateid(s, &st->sc_stateid) << 
2850         seq_puts(s, ": { type: lock, ");      << 
2851                                               << 
2852         spin_lock(&nf->fi_lock);              << 
2853         file = find_any_file_locked(nf);      << 
2854         if (file) {                           << 
2855                 /*                            << 
2856                  * Note: a lock stateid isn't << 
2857                  * it's the locking state hel << 
2858                  * may be multiple (or no) lo << 
2859                  * (Same for the matter is tr << 
2860                  */                           << 
2861                                               << 
2862                 nfs4_show_superblock(s, file) << 
2863                 /* XXX: open stateid? */      << 
2864                 seq_puts(s, ", ");            << 
2865                 nfs4_show_fname(s, file);     << 
2866                 seq_puts(s, ", ");            << 
2867         }                                     << 
2868         nfs4_show_owner(s, oo);               << 
2869         if (st->sc_status & SC_STATUS_ADMIN_R << 
2870                 seq_puts(s, ", admin-revoked" << 
2871         seq_puts(s, " }\n");                  << 
2872         spin_unlock(&nf->fi_lock);            << 
2873         return 0;                             << 
2874 }                                             << 
2875                                               << 
2876 static int nfs4_show_deleg(struct seq_file *s << 
2877 {                                             << 
2878         struct nfs4_delegation *ds;           << 
2879         struct nfs4_file *nf;                 << 
2880         struct nfsd_file *file;               << 
2881                                               << 
2882         ds = delegstateid(st);                << 
2883         nf = st->sc_file;                     << 
2884                                               << 
2885         seq_puts(s, "- ");                    << 
2886         nfs4_show_stateid(s, &st->sc_stateid) << 
2887         seq_puts(s, ": { type: deleg, ");     << 
2888                                               << 
2889         seq_printf(s, "access: %s",           << 
2890                    ds->dl_type == NFS4_OPEN_D << 
2891                                               << 
2892         /* XXX: lease time, whether it's bein << 
2893                                               << 
2894         spin_lock(&nf->fi_lock);              << 
2895         file = nf->fi_deleg_file;             << 
2896         if (file) {                           << 
2897                 seq_puts(s, ", ");            << 
2898                 nfs4_show_superblock(s, file) << 
2899                 seq_puts(s, ", ");            << 
2900                 nfs4_show_fname(s, file);     << 
2901         }                                     << 
2902         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;                             << 
2907 }                                             << 
2908                                               << 
2909 static int nfs4_show_layout(struct seq_file * << 
2910 {                                             << 
2911         struct nfs4_layout_stateid *ls;       << 
2912         struct nfsd_file *file;               << 
2913                                               << 
2914         ls = container_of(st, struct nfs4_lay << 
2915                                               << 
2916         seq_puts(s, "- ");                    << 
2917         nfs4_show_stateid(s, &st->sc_stateid) << 
2918         seq_puts(s, ": { type: layout");      << 
2919                                               << 
2920         /* XXX: What else would be useful? */ << 
2921                                               << 
2922         spin_lock(&ls->ls_stid.sc_file->fi_lo << 
2923         file = ls->ls_file;                   << 
2924         if (file) {                           << 
2925                 seq_puts(s, ", ");            << 
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                                               << 
2935         return 0;                             << 
2936 }                                             << 
2937                                               << 
2938 static int states_show(struct seq_file *s, vo << 
2939 {                                             << 
2940         struct nfs4_stid *st = v;             << 
2941                                               << 
2942         switch (st->sc_type) {                << 
2943         case SC_TYPE_OPEN:                    << 
2944                 return nfs4_show_open(s, st); << 
2945         case SC_TYPE_LOCK:                    << 
2946                 return nfs4_show_lock(s, st); << 
2947         case SC_TYPE_DELEG:                   << 
2948                 return nfs4_show_deleg(s, st) << 
2949         case SC_TYPE_LAYOUT:                  << 
2950                 return nfs4_show_layout(s, st << 
2951         default:                              << 
2952                 return 0; /* XXX: or SEQ_SKIP << 
2953         }                                     << 
2954         /* XXX: copy stateids? */             << 
2955 }                                             << 
2956                                               << 
2957 static struct seq_operations states_seq_ops = << 
2958         .start = states_start,                << 
2959         .next = states_next,                  << 
2960         .stop = states_stop,                  << 
2961         .show = states_show                   << 
2962 };                                            << 
2963                                               << 
2964 static int client_states_open(struct inode *i << 
2965 {                                             << 
2966         struct seq_file *s;                   << 
2967         struct nfs4_client *clp;              << 
2968         int ret;                              << 
2969                                               << 
2970         clp = get_nfsdfs_clp(inode);          << 
2971         if (!clp)                             << 
2972                 return -ENXIO;                << 
2973                                               << 
2974         ret = seq_open(file, &states_seq_ops) << 
2975         if (ret)                              << 
2976                 return ret;                   << 
2977         s = file->private_data;               << 
2978         s->private = clp;                     << 
2979         return 0;                             << 
2980 }                                             << 
2981                                               << 
2982 static int client_opens_release(struct inode  << 
2983 {                                             << 
2984         struct seq_file *m = file->private_da << 
2985         struct nfs4_client *clp = m->private; << 
2986                                               << 
2987         /* XXX: alternatively, we could get/d << 
2988         drop_client(clp);                     << 
2989         return seq_release(inode, file);      << 
2990 }                                             << 
2991                                               << 
2992 static const struct file_operations client_st << 
2993         .open           = client_states_open, << 
2994         .read           = seq_read,           << 
2995         .llseek         = seq_lseek,          << 
2996         .release        = client_opens_releas << 
2997 };                                            << 
2998                                               << 
2999 /*                                            << 
3000  * Normally we refuse to destroy clients that << 
3001  * administrator is telling us to just do it. << 
3002  * so the caller has a guarantee that the cli << 
3003  * the time the write returns:                << 
3004  */                                           << 
3005 static void force_expire_client(struct nfs4_c << 
3006 {                                             << 
3007         struct nfsd_net *nn = net_generic(clp << 
3008         bool already_expired;                 << 
3009                                               << 
3010         trace_nfsd_clid_admin_expired(&clp->c << 
3011                                               << 
3012         spin_lock(&nn->client_lock);          << 
3013         clp->cl_time = 0;                     << 
3014         spin_unlock(&nn->client_lock);        << 
3015                                               << 
3016         wait_event(expiry_wq, atomic_read(&cl << 
3017         spin_lock(&nn->client_lock);          << 
3018         already_expired = list_empty(&clp->cl << 
3019         if (!already_expired)                 << 
3020                 unhash_client_locked(clp);    << 
3021         spin_unlock(&nn->client_lock);        << 
3022                                               << 
3023         if (!already_expired)                 << 
3024                 expire_client(clp);           << 
3025         else                                  << 
3026                 wait_event(expiry_wq, clp->cl << 
3027 }                                             << 
3028                                               << 
3029 static ssize_t client_ctl_write(struct file * << 
3030                                    size_t siz << 
3031 {                                             << 
3032         char *data;                           << 
3033         struct nfs4_client *clp;              << 
3034                                               << 
3035         data = simple_transaction_get(file, b << 
3036         if (IS_ERR(data))                     << 
3037                 return PTR_ERR(data);         << 
3038         if (size != 7 || 0 != memcmp(data, "e << 
3039                 return -EINVAL;               << 
3040         clp = get_nfsdfs_clp(file_inode(file) << 
3041         if (!clp)                             << 
3042                 return -ENXIO;                << 
3043         force_expire_client(clp);             << 
3044         drop_client(clp);                     << 
3045         return 7;                             << 
3046 }                                             << 
3047                                               << 
3048 static const struct file_operations client_ct << 
3049         .write          = client_ctl_write,   << 
3050         .release        = simple_transaction_ << 
3051 };                                            << 
3052                                               << 
3053 static const struct tree_descr client_files[] << 
3054         [0] = {"info", &client_info_fops, S_I << 
3055         [1] = {"states", &client_states_fops, << 
3056         [2] = {"ctl", &client_ctl_fops, S_IWU << 
3057         [3] = {""},                           << 
3058 };                                            << 
3059                                               << 
3060 static int                                    << 
3061 nfsd4_cb_recall_any_done(struct nfsd4_callbac << 
3062                                 struct rpc_ta << 
3063 {                                             << 
3064         trace_nfsd_cb_recall_any_done(cb, tas << 
3065         switch (task->tk_status) {            << 
3066         case -NFS4ERR_DELAY:                  << 
3067                 rpc_delay(task, 2 * HZ);      << 
3068                 return 0;                     << 
3069         default:                              << 
3070                 return 1;                     << 
3071         }                                     << 
3072 }                                             << 
3073                                               << 
3074 static void                                   << 
3075 nfsd4_cb_recall_any_release(struct nfsd4_call << 
3076 {                                             << 
3077         struct nfs4_client *clp = cb->cb_clp; << 
3078                                               << 
3079         clear_bit(NFSD4_CLIENT_CB_RECALL_ANY, << 
3080         drop_client(clp);                     << 
3081 }                                             << 
3082                                               << 
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_ << 
3115         .done           = nfsd4_cb_recall_any << 
3116         .release        = nfsd4_cb_recall_any << 
3117         .opcode         = OP_CB_RECALL_ANY,   << 
3118 };                                            << 
3119                                               << 
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    2154 static struct nfs4_client *create_client(struct xdr_netobj name,
3141                 struct svc_rqst *rqstp, nfs4_    2155                 struct svc_rqst *rqstp, nfs4_verifier *verf)
3142 {                                                2156 {
3143         struct nfs4_client *clp;                 2157         struct nfs4_client *clp;
3144         struct sockaddr *sa = svc_addr(rqstp)    2158         struct sockaddr *sa = svc_addr(rqstp);
3145         int ret;                                 2159         int ret;
3146         struct net *net = SVC_NET(rqstp);        2160         struct net *net = SVC_NET(rqstp);
3147         struct nfsd_net *nn = net_generic(net << 
3148         struct dentry *dentries[ARRAY_SIZE(cl << 
3149                                                  2161 
3150         clp = alloc_client(name, nn);         !! 2162         clp = alloc_client(name);
3151         if (clp == NULL)                         2163         if (clp == NULL)
3152                 return NULL;                     2164                 return NULL;
3153                                                  2165 
3154         ret = copy_cred(&clp->cl_cred, &rqstp    2166         ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
3155         if (ret) {                               2167         if (ret) {
3156                 free_client(clp);                2168                 free_client(clp);
3157                 return NULL;                     2169                 return NULL;
3158         }                                        2170         }
3159         gen_clid(clp, nn);                    << 
3160         kref_init(&clp->cl_nfsdfs.cl_ref);    << 
3161         nfsd4_init_cb(&clp->cl_cb_null, clp,     2171         nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
3162         clp->cl_time = ktime_get_boottime_sec !! 2172         clp->cl_time = get_seconds();
3163         clear_bit(0, &clp->cl_cb_slot_busy);     2173         clear_bit(0, &clp->cl_cb_slot_busy);
3164         copy_verf(clp, verf);                    2174         copy_verf(clp, verf);
3165         memcpy(&clp->cl_addr, sa, sizeof(stru !! 2175         rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
3166         clp->cl_cb_session = NULL;               2176         clp->cl_cb_session = NULL;
3167         clp->net = net;                          2177         clp->net = net;
3168         clp->cl_nfsd_dentry = nfsd_client_mkd << 
3169                 nn, &clp->cl_nfsdfs,          << 
3170                 clp->cl_clientid.cl_id - nn-> << 
3171                 client_files, dentries);      << 
3172         clp->cl_nfsd_info_dentry = dentries[0 << 
3173         if (!clp->cl_nfsd_dentry) {           << 
3174                 free_client(clp);             << 
3175                 return NULL;                  << 
3176         }                                     << 
3177         clp->cl_ra = kzalloc(sizeof(*clp->cl_ << 
3178         if (!clp->cl_ra) {                    << 
3179                 free_client(clp);             << 
3180                 return NULL;                  << 
3181         }                                     << 
3182         clp->cl_ra_time = 0;                  << 
3183         nfsd4_init_cb(&clp->cl_ra->ra_cb, clp << 
3184                         NFSPROC4_CLNT_CB_RECA << 
3185         return clp;                              2178         return clp;
3186 }                                                2179 }
3187                                                  2180 
3188 static void                                      2181 static void
3189 add_clp_to_name_tree(struct nfs4_client *new_    2182 add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
3190 {                                                2183 {
3191         struct rb_node **new = &(root->rb_nod    2184         struct rb_node **new = &(root->rb_node), *parent = NULL;
3192         struct nfs4_client *clp;                 2185         struct nfs4_client *clp;
3193                                                  2186 
3194         while (*new) {                           2187         while (*new) {
3195                 clp = rb_entry(*new, struct n    2188                 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
3196                 parent = *new;                   2189                 parent = *new;
3197                                                  2190 
3198                 if (compare_blob(&clp->cl_nam    2191                 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
3199                         new = &((*new)->rb_le    2192                         new = &((*new)->rb_left);
3200                 else                             2193                 else
3201                         new = &((*new)->rb_ri    2194                         new = &((*new)->rb_right);
3202         }                                        2195         }
3203                                                  2196 
3204         rb_link_node(&new_clp->cl_namenode, p    2197         rb_link_node(&new_clp->cl_namenode, parent, new);
3205         rb_insert_color(&new_clp->cl_namenode    2198         rb_insert_color(&new_clp->cl_namenode, root);
3206 }                                                2199 }
3207                                                  2200 
3208 static struct nfs4_client *                      2201 static struct nfs4_client *
3209 find_clp_in_name_tree(struct xdr_netobj *name    2202 find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
3210 {                                                2203 {
3211         int cmp;                                 2204         int cmp;
3212         struct rb_node *node = root->rb_node;    2205         struct rb_node *node = root->rb_node;
3213         struct nfs4_client *clp;                 2206         struct nfs4_client *clp;
3214                                                  2207 
3215         while (node) {                           2208         while (node) {
3216                 clp = rb_entry(node, struct n    2209                 clp = rb_entry(node, struct nfs4_client, cl_namenode);
3217                 cmp = compare_blob(&clp->cl_n    2210                 cmp = compare_blob(&clp->cl_name, name);
3218                 if (cmp > 0)                     2211                 if (cmp > 0)
3219                         node = node->rb_left;    2212                         node = node->rb_left;
3220                 else if (cmp < 0)                2213                 else if (cmp < 0)
3221                         node = node->rb_right    2214                         node = node->rb_right;
3222                 else                             2215                 else
3223                         return clp;              2216                         return clp;
3224         }                                        2217         }
3225         return NULL;                             2218         return NULL;
3226 }                                                2219 }
3227                                                  2220 
3228 static void                                      2221 static void
3229 add_to_unconfirmed(struct nfs4_client *clp)      2222 add_to_unconfirmed(struct nfs4_client *clp)
3230 {                                                2223 {
3231         unsigned int idhashval;                  2224         unsigned int idhashval;
3232         struct nfsd_net *nn = net_generic(clp    2225         struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
3233                                                  2226 
3234         lockdep_assert_held(&nn->client_lock)    2227         lockdep_assert_held(&nn->client_lock);
3235                                                  2228 
3236         clear_bit(NFSD4_CLIENT_CONFIRMED, &cl    2229         clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
3237         add_clp_to_name_tree(clp, &nn->unconf    2230         add_clp_to_name_tree(clp, &nn->unconf_name_tree);
3238         idhashval = clientid_hashval(clp->cl_    2231         idhashval = clientid_hashval(clp->cl_clientid.cl_id);
3239         list_add(&clp->cl_idhash, &nn->unconf    2232         list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
3240         renew_client_locked(clp);                2233         renew_client_locked(clp);
3241 }                                                2234 }
3242                                                  2235 
3243 static void                                      2236 static void
3244 move_to_confirmed(struct nfs4_client *clp)       2237 move_to_confirmed(struct nfs4_client *clp)
3245 {                                                2238 {
3246         unsigned int idhashval = clientid_has    2239         unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
3247         struct nfsd_net *nn = net_generic(clp    2240         struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
3248                                                  2241 
3249         lockdep_assert_held(&nn->client_lock)    2242         lockdep_assert_held(&nn->client_lock);
3250                                                  2243 
                                                   >> 2244         dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
3251         list_move(&clp->cl_idhash, &nn->conf_    2245         list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
3252         rb_erase(&clp->cl_namenode, &nn->unco    2246         rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
3253         add_clp_to_name_tree(clp, &nn->conf_n    2247         add_clp_to_name_tree(clp, &nn->conf_name_tree);
3254         set_bit(NFSD4_CLIENT_CONFIRMED, &clp-    2248         set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
3255         trace_nfsd_clid_confirmed(&clp->cl_cl << 
3256         renew_client_locked(clp);                2249         renew_client_locked(clp);
3257 }                                                2250 }
3258                                                  2251 
3259 static struct nfs4_client *                      2252 static struct nfs4_client *
3260 find_client_in_id_table(struct list_head *tbl    2253 find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
3261 {                                                2254 {
3262         struct nfs4_client *clp;                 2255         struct nfs4_client *clp;
3263         unsigned int idhashval = clientid_has    2256         unsigned int idhashval = clientid_hashval(clid->cl_id);
3264                                                  2257 
3265         list_for_each_entry(clp, &tbl[idhashv    2258         list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
3266                 if (same_clid(&clp->cl_client    2259                 if (same_clid(&clp->cl_clientid, clid)) {
3267                         if ((bool)clp->cl_min    2260                         if ((bool)clp->cl_minorversion != sessions)
3268                                 return NULL;     2261                                 return NULL;
3269                         renew_client_locked(c    2262                         renew_client_locked(clp);
3270                         return clp;              2263                         return clp;
3271                 }                                2264                 }
3272         }                                        2265         }
3273         return NULL;                             2266         return NULL;
3274 }                                                2267 }
3275                                                  2268 
3276 static struct nfs4_client *                      2269 static struct nfs4_client *
3277 find_confirmed_client(clientid_t *clid, bool     2270 find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
3278 {                                                2271 {
3279         struct list_head *tbl = nn->conf_id_h    2272         struct list_head *tbl = nn->conf_id_hashtbl;
3280                                                  2273 
3281         lockdep_assert_held(&nn->client_lock)    2274         lockdep_assert_held(&nn->client_lock);
3282         return find_client_in_id_table(tbl, c    2275         return find_client_in_id_table(tbl, clid, sessions);
3283 }                                                2276 }
3284                                                  2277 
3285 static struct nfs4_client *                      2278 static struct nfs4_client *
3286 find_unconfirmed_client(clientid_t *clid, boo    2279 find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
3287 {                                                2280 {
3288         struct list_head *tbl = nn->unconf_id    2281         struct list_head *tbl = nn->unconf_id_hashtbl;
3289                                                  2282 
3290         lockdep_assert_held(&nn->client_lock)    2283         lockdep_assert_held(&nn->client_lock);
3291         return find_client_in_id_table(tbl, c    2284         return find_client_in_id_table(tbl, clid, sessions);
3292 }                                                2285 }
3293                                                  2286 
3294 static bool clp_used_exchangeid(struct nfs4_c    2287 static bool clp_used_exchangeid(struct nfs4_client *clp)
3295 {                                                2288 {
3296         return clp->cl_exchange_flags != 0;      2289         return clp->cl_exchange_flags != 0;
3297 }                                                2290 } 
3298                                                  2291 
3299 static struct nfs4_client *                      2292 static struct nfs4_client *
3300 find_confirmed_client_by_name(struct xdr_neto    2293 find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
3301 {                                                2294 {
3302         lockdep_assert_held(&nn->client_lock)    2295         lockdep_assert_held(&nn->client_lock);
3303         return find_clp_in_name_tree(name, &n    2296         return find_clp_in_name_tree(name, &nn->conf_name_tree);
3304 }                                                2297 }
3305                                                  2298 
3306 static struct nfs4_client *                      2299 static struct nfs4_client *
3307 find_unconfirmed_client_by_name(struct xdr_ne    2300 find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
3308 {                                                2301 {
3309         lockdep_assert_held(&nn->client_lock)    2302         lockdep_assert_held(&nn->client_lock);
3310         return find_clp_in_name_tree(name, &n    2303         return find_clp_in_name_tree(name, &nn->unconf_name_tree);
3311 }                                                2304 }
3312                                                  2305 
3313 static void                                      2306 static void
3314 gen_callback(struct nfs4_client *clp, struct     2307 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
3315 {                                                2308 {
3316         struct nfs4_cb_conn *conn = &clp->cl_    2309         struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
3317         struct sockaddr *sa = svc_addr(rqstp)    2310         struct sockaddr *sa = svc_addr(rqstp);
3318         u32 scopeid = rpc_get_scope_id(sa);      2311         u32 scopeid = rpc_get_scope_id(sa);
3319         unsigned short expected_family;          2312         unsigned short expected_family;
3320                                                  2313 
3321         /* Currently, we only support tcp and    2314         /* Currently, we only support tcp and tcp6 for the callback channel */
3322         if (se->se_callback_netid_len == 3 &&    2315         if (se->se_callback_netid_len == 3 &&
3323             !memcmp(se->se_callback_netid_val    2316             !memcmp(se->se_callback_netid_val, "tcp", 3))
3324                 expected_family = AF_INET;       2317                 expected_family = AF_INET;
3325         else if (se->se_callback_netid_len ==    2318         else if (se->se_callback_netid_len == 4 &&
3326                  !memcmp(se->se_callback_neti    2319                  !memcmp(se->se_callback_netid_val, "tcp6", 4))
3327                 expected_family = AF_INET6;      2320                 expected_family = AF_INET6;
3328         else                                     2321         else
3329                 goto out_err;                    2322                 goto out_err;
3330                                                  2323 
3331         conn->cb_addrlen = rpc_uaddr2sockaddr    2324         conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
3332                                             s    2325                                             se->se_callback_addr_len,
3333                                             (    2326                                             (struct sockaddr *)&conn->cb_addr,
3334                                             s    2327                                             sizeof(conn->cb_addr));
3335                                                  2328 
3336         if (!conn->cb_addrlen || conn->cb_add    2329         if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
3337                 goto out_err;                    2330                 goto out_err;
3338                                                  2331 
3339         if (conn->cb_addr.ss_family == AF_INE    2332         if (conn->cb_addr.ss_family == AF_INET6)
3340                 ((struct sockaddr_in6 *)&conn    2333                 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
3341                                                  2334 
3342         conn->cb_prog = se->se_callback_prog;    2335         conn->cb_prog = se->se_callback_prog;
3343         conn->cb_ident = se->se_callback_iden    2336         conn->cb_ident = se->se_callback_ident;
3344         memcpy(&conn->cb_saddr, &rqstp->rq_da    2337         memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
3345         trace_nfsd_cb_args(clp, conn);        << 
3346         return;                                  2338         return;
3347 out_err:                                         2339 out_err:
3348         conn->cb_addr.ss_family = AF_UNSPEC;     2340         conn->cb_addr.ss_family = AF_UNSPEC;
3349         conn->cb_addrlen = 0;                    2341         conn->cb_addrlen = 0;
3350         trace_nfsd_cb_nodelegs(clp);          !! 2342         dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
                                                   >> 2343                 "will not receive delegations\n",
                                                   >> 2344                 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
                                                   >> 2345 
3351         return;                                  2346         return;
3352 }                                                2347 }
3353                                                  2348 
3354 /*                                               2349 /*
3355  * Cache a reply. nfsd4_check_resp_size() has    2350  * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
3356  */                                              2351  */
3357 static void                                      2352 static void
3358 nfsd4_store_cache_entry(struct nfsd4_compound    2353 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
3359 {                                                2354 {
3360         struct xdr_buf *buf = resp->xdr->buf; !! 2355         struct xdr_buf *buf = resp->xdr.buf;
3361         struct nfsd4_slot *slot = resp->cstat    2356         struct nfsd4_slot *slot = resp->cstate.slot;
3362         unsigned int base;                       2357         unsigned int base;
3363                                                  2358 
3364         dprintk("--> %s slot %p\n", __func__,    2359         dprintk("--> %s slot %p\n", __func__, slot);
3365                                                  2360 
3366         slot->sl_flags |= NFSD4_SLOT_INITIALI    2361         slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
3367         slot->sl_opcnt = resp->opcnt;            2362         slot->sl_opcnt = resp->opcnt;
3368         slot->sl_status = resp->cstate.status    2363         slot->sl_status = resp->cstate.status;
3369         free_svc_cred(&slot->sl_cred);           2364         free_svc_cred(&slot->sl_cred);
3370         copy_cred(&slot->sl_cred, &resp->rqst    2365         copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred);
3371                                                  2366 
3372         if (!nfsd4_cache_this(resp)) {           2367         if (!nfsd4_cache_this(resp)) {
3373                 slot->sl_flags &= ~NFSD4_SLOT    2368                 slot->sl_flags &= ~NFSD4_SLOT_CACHED;
3374                 return;                          2369                 return;
3375         }                                        2370         }
3376         slot->sl_flags |= NFSD4_SLOT_CACHED;     2371         slot->sl_flags |= NFSD4_SLOT_CACHED;
3377                                                  2372 
3378         base = resp->cstate.data_offset;         2373         base = resp->cstate.data_offset;
3379         slot->sl_datalen = buf->len - base;      2374         slot->sl_datalen = buf->len - base;
3380         if (read_bytes_from_xdr_buf(buf, base    2375         if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
3381                 WARN(1, "%s: sessions DRC cou    2376                 WARN(1, "%s: sessions DRC could not cache compound\n",
3382                      __func__);                  2377                      __func__);
3383         return;                                  2378         return;
3384 }                                                2379 }
3385                                                  2380 
3386 /*                                               2381 /*
3387  * Encode the replay sequence operation from     2382  * Encode the replay sequence operation from the slot values.
3388  * If cachethis is FALSE encode the uncached     2383  * If cachethis is FALSE encode the uncached rep error on the next
3389  * operation which sets resp->p and increment    2384  * operation which sets resp->p and increments resp->opcnt for
3390  * nfs4svc_encode_compoundres.                   2385  * nfs4svc_encode_compoundres.
3391  *                                               2386  *
3392  */                                              2387  */
3393 static __be32                                    2388 static __be32
3394 nfsd4_enc_sequence_replay(struct nfsd4_compou    2389 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
3395                           struct nfsd4_compou    2390                           struct nfsd4_compoundres *resp)
3396 {                                                2391 {
3397         struct nfsd4_op *op;                     2392         struct nfsd4_op *op;
3398         struct nfsd4_slot *slot = resp->cstat    2393         struct nfsd4_slot *slot = resp->cstate.slot;
3399                                                  2394 
3400         /* Encode the replayed sequence opera    2395         /* Encode the replayed sequence operation */
3401         op = &args->ops[resp->opcnt - 1];        2396         op = &args->ops[resp->opcnt - 1];
3402         nfsd4_encode_operation(resp, op);        2397         nfsd4_encode_operation(resp, op);
3403                                                  2398 
3404         if (slot->sl_flags & NFSD4_SLOT_CACHE    2399         if (slot->sl_flags & NFSD4_SLOT_CACHED)
3405                 return op->status;               2400                 return op->status;
3406         if (args->opcnt == 1) {                  2401         if (args->opcnt == 1) {
3407                 /*                               2402                 /*
3408                  * The original operation was    2403                  * The original operation wasn't a solo sequence--we
3409                  * always cache those--so thi    2404                  * always cache those--so this retry must not match the
3410                  * original:                     2405                  * original:
3411                  */                              2406                  */
3412                 op->status = nfserr_seq_false    2407                 op->status = nfserr_seq_false_retry;
3413         } else {                                 2408         } else {
3414                 op = &args->ops[resp->opcnt++    2409                 op = &args->ops[resp->opcnt++];
3415                 op->status = nfserr_retry_unc    2410                 op->status = nfserr_retry_uncached_rep;
3416                 nfsd4_encode_operation(resp,     2411                 nfsd4_encode_operation(resp, op);
3417         }                                        2412         }
3418         return op->status;                       2413         return op->status;
3419 }                                                2414 }
3420                                                  2415 
3421 /*                                               2416 /*
3422  * The sequence operation is not cached becau    2417  * The sequence operation is not cached because we can use the slot and
3423  * session values.                               2418  * session values.
3424  */                                              2419  */
3425 static __be32                                    2420 static __be32
3426 nfsd4_replay_cache_entry(struct nfsd4_compoun    2421 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
3427                          struct nfsd4_sequenc    2422                          struct nfsd4_sequence *seq)
3428 {                                                2423 {
3429         struct nfsd4_slot *slot = resp->cstat    2424         struct nfsd4_slot *slot = resp->cstate.slot;
3430         struct xdr_stream *xdr = resp->xdr;   !! 2425         struct xdr_stream *xdr = &resp->xdr;
3431         __be32 *p;                               2426         __be32 *p;
3432         __be32 status;                           2427         __be32 status;
3433                                                  2428 
3434         dprintk("--> %s slot %p\n", __func__,    2429         dprintk("--> %s slot %p\n", __func__, slot);
3435                                                  2430 
3436         status = nfsd4_enc_sequence_replay(re    2431         status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
3437         if (status)                              2432         if (status)
3438                 return status;                   2433                 return status;
3439                                                  2434 
3440         p = xdr_reserve_space(xdr, slot->sl_d    2435         p = xdr_reserve_space(xdr, slot->sl_datalen);
3441         if (!p) {                                2436         if (!p) {
3442                 WARN_ON_ONCE(1);                 2437                 WARN_ON_ONCE(1);
3443                 return nfserr_serverfault;       2438                 return nfserr_serverfault;
3444         }                                        2439         }
3445         xdr_encode_opaque_fixed(p, slot->sl_d    2440         xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
3446         xdr_commit_encode(xdr);                  2441         xdr_commit_encode(xdr);
3447                                                  2442 
3448         resp->opcnt = slot->sl_opcnt;            2443         resp->opcnt = slot->sl_opcnt;
3449         return slot->sl_status;                  2444         return slot->sl_status;
3450 }                                                2445 }
3451                                                  2446 
3452 /*                                               2447 /*
3453  * Set the exchange_id flags returned by the     2448  * Set the exchange_id flags returned by the server.
3454  */                                              2449  */
3455 static void                                      2450 static void
3456 nfsd4_set_ex_flags(struct nfs4_client *new, s    2451 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
3457 {                                                2452 {
3458 #ifdef CONFIG_NFSD_PNFS                          2453 #ifdef CONFIG_NFSD_PNFS
3459         new->cl_exchange_flags |= EXCHGID4_FL    2454         new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
3460 #else                                            2455 #else
3461         new->cl_exchange_flags |= EXCHGID4_FL    2456         new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
3462 #endif                                           2457 #endif
3463                                                  2458 
3464         /* Referrals are supported, Migration    2459         /* Referrals are supported, Migration is not. */
3465         new->cl_exchange_flags |= EXCHGID4_FL    2460         new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
3466                                                  2461 
3467         /* set the wire flags to return to cl    2462         /* set the wire flags to return to client. */
3468         clid->flags = new->cl_exchange_flags;    2463         clid->flags = new->cl_exchange_flags;
3469 }                                                2464 }
3470                                                  2465 
3471 static bool client_has_openowners(struct nfs4    2466 static bool client_has_openowners(struct nfs4_client *clp)
3472 {                                                2467 {
3473         struct nfs4_openowner *oo;               2468         struct nfs4_openowner *oo;
3474                                                  2469 
3475         list_for_each_entry(oo, &clp->cl_open    2470         list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
3476                 if (!list_empty(&oo->oo_owner    2471                 if (!list_empty(&oo->oo_owner.so_stateids))
3477                         return true;             2472                         return true;
3478         }                                        2473         }
3479         return false;                            2474         return false;
3480 }                                                2475 }
3481                                                  2476 
3482 static bool client_has_state(struct nfs4_clie    2477 static bool client_has_state(struct nfs4_client *clp)
3483 {                                                2478 {
3484         return client_has_openowners(clp)        2479         return client_has_openowners(clp)
3485 #ifdef CONFIG_NFSD_PNFS                          2480 #ifdef CONFIG_NFSD_PNFS
3486                 || !list_empty(&clp->cl_lo_st    2481                 || !list_empty(&clp->cl_lo_states)
3487 #endif                                           2482 #endif
3488                 || !list_empty(&clp->cl_deleg    2483                 || !list_empty(&clp->cl_delegations)
3489                 || !list_empty(&clp->cl_sessi !! 2484                 || !list_empty(&clp->cl_sessions);
3490                 || !list_empty(&clp->async_co << 
3491 }                                             << 
3492                                               << 
3493 static __be32 copy_impl_id(struct nfs4_client << 
3494                                 struct nfsd4_ << 
3495 {                                             << 
3496         if (!exid->nii_domain.data)           << 
3497                 return 0;                     << 
3498         xdr_netobj_dup(&clp->cl_nii_domain, & << 
3499         if (!clp->cl_nii_domain.data)         << 
3500                 return nfserr_jukebox;        << 
3501         xdr_netobj_dup(&clp->cl_nii_name, &ex << 
3502         if (!clp->cl_nii_name.data)           << 
3503                 return nfserr_jukebox;        << 
3504         clp->cl_nii_time = exid->nii_time;    << 
3505         return 0;                             << 
3506 }                                                2485 }
3507                                                  2486 
3508 __be32                                           2487 __be32
3509 nfsd4_exchange_id(struct svc_rqst *rqstp, str !! 2488 nfsd4_exchange_id(struct svc_rqst *rqstp,
3510                 union nfsd4_op_u *u)          !! 2489                   struct nfsd4_compound_state *cstate,
                                                   >> 2490                   struct nfsd4_exchange_id *exid)
3511 {                                                2491 {
3512         struct nfsd4_exchange_id *exid = &u-> << 
3513         struct nfs4_client *conf, *new;          2492         struct nfs4_client *conf, *new;
3514         struct nfs4_client *unconf = NULL;       2493         struct nfs4_client *unconf = NULL;
3515         __be32 status;                           2494         __be32 status;
3516         char                    addr_str[INET    2495         char                    addr_str[INET6_ADDRSTRLEN];
3517         nfs4_verifier           verf = exid->    2496         nfs4_verifier           verf = exid->verifier;
3518         struct sockaddr         *sa = svc_add    2497         struct sockaddr         *sa = svc_addr(rqstp);
3519         bool    update = exid->flags & EXCHGI    2498         bool    update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
3520         struct nfsd_net         *nn = net_gen    2499         struct nfsd_net         *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3521                                                  2500 
3522         rpc_ntop(sa, addr_str, sizeof(addr_st    2501         rpc_ntop(sa, addr_str, sizeof(addr_str));
3523         dprintk("%s rqstp=%p exid=%p clname.l    2502         dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
3524                 "ip_addr=%s flags %x, spa_how !! 2503                 "ip_addr=%s flags %x, spa_how %d\n",
3525                 __func__, rqstp, exid, exid->    2504                 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
3526                 addr_str, exid->flags, exid->    2505                 addr_str, exid->flags, exid->spa_how);
3527                                                  2506 
3528         if (exid->flags & ~EXCHGID4_FLAG_MASK    2507         if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
3529                 return nfserr_inval;             2508                 return nfserr_inval;
3530                                                  2509 
3531         new = create_client(exid->clname, rqs    2510         new = create_client(exid->clname, rqstp, &verf);
3532         if (new == NULL)                         2511         if (new == NULL)
3533                 return nfserr_jukebox;           2512                 return nfserr_jukebox;
3534         status = copy_impl_id(new, exid);     << 
3535         if (status)                           << 
3536                 goto out_nolock;              << 
3537                                                  2513 
3538         switch (exid->spa_how) {                 2514         switch (exid->spa_how) {
3539         case SP4_MACH_CRED:                      2515         case SP4_MACH_CRED:
3540                 exid->spo_must_enforce[0] = 0    2516                 exid->spo_must_enforce[0] = 0;
3541                 exid->spo_must_enforce[1] = (    2517                 exid->spo_must_enforce[1] = (
3542                         1 << (OP_BIND_CONN_TO    2518                         1 << (OP_BIND_CONN_TO_SESSION - 32) |
3543                         1 << (OP_EXCHANGE_ID     2519                         1 << (OP_EXCHANGE_ID - 32) |
3544                         1 << (OP_CREATE_SESSI    2520                         1 << (OP_CREATE_SESSION - 32) |
3545                         1 << (OP_DESTROY_SESS    2521                         1 << (OP_DESTROY_SESSION - 32) |
3546                         1 << (OP_DESTROY_CLIE    2522                         1 << (OP_DESTROY_CLIENTID - 32));
3547                                                  2523 
3548                 exid->spo_must_allow[0] &= (1    2524                 exid->spo_must_allow[0] &= (1 << (OP_CLOSE) |
3549                                         1 <<     2525                                         1 << (OP_OPEN_DOWNGRADE) |
3550                                         1 <<     2526                                         1 << (OP_LOCKU) |
3551                                         1 <<     2527                                         1 << (OP_DELEGRETURN));
3552                                                  2528 
3553                 exid->spo_must_allow[1] &= (     2529                 exid->spo_must_allow[1] &= (
3554                                         1 <<     2530                                         1 << (OP_TEST_STATEID - 32) |
3555                                         1 <<     2531                                         1 << (OP_FREE_STATEID - 32));
3556                 if (!svc_rqst_integrity_prote    2532                 if (!svc_rqst_integrity_protected(rqstp)) {
3557                         status = nfserr_inval    2533                         status = nfserr_inval;
3558                         goto out_nolock;         2534                         goto out_nolock;
3559                 }                                2535                 }
3560                 /*                               2536                 /*
3561                  * Sometimes userspace doesn'    2537                  * Sometimes userspace doesn't give us a principal.
3562                  * Which is a bug, really.  A    2538                  * Which is a bug, really.  Anyway, we can't enforce
3563                  * MACH_CRED in that case, be    2539                  * MACH_CRED in that case, better to give up now:
3564                  */                              2540                  */
3565                 if (!new->cl_cred.cr_principa    2541                 if (!new->cl_cred.cr_principal &&
3566                                         !new-    2542                                         !new->cl_cred.cr_raw_principal) {
3567                         status = nfserr_serve    2543                         status = nfserr_serverfault;
3568                         goto out_nolock;         2544                         goto out_nolock;
3569                 }                                2545                 }
3570                 new->cl_mach_cred = true;        2546                 new->cl_mach_cred = true;
3571                 break;                        << 
3572         case SP4_NONE:                           2547         case SP4_NONE:
3573                 break;                           2548                 break;
3574         default:                                 2549         default:                                /* checked by xdr code */
3575                 WARN_ON_ONCE(1);                 2550                 WARN_ON_ONCE(1);
3576                 fallthrough;                  << 
3577         case SP4_SSV:                            2551         case SP4_SSV:
3578                 status = nfserr_encr_alg_unsu    2552                 status = nfserr_encr_alg_unsupp;
3579                 goto out_nolock;                 2553                 goto out_nolock;
3580         }                                        2554         }
3581                                                  2555 
3582         /* Cases below refer to rfc 5661 sect    2556         /* Cases below refer to rfc 5661 section 18.35.4: */
3583         spin_lock(&nn->client_lock);             2557         spin_lock(&nn->client_lock);
3584         conf = find_confirmed_client_by_name(    2558         conf = find_confirmed_client_by_name(&exid->clname, nn);
3585         if (conf) {                              2559         if (conf) {
3586                 bool creds_match = same_creds    2560                 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
3587                 bool verfs_match = same_verf(    2561                 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
3588                                                  2562 
3589                 if (update) {                    2563                 if (update) {
3590                         if (!clp_used_exchang    2564                         if (!clp_used_exchangeid(conf)) { /* buggy client */
3591                                 status = nfse    2565                                 status = nfserr_inval;
3592                                 goto out;        2566                                 goto out;
3593                         }                        2567                         }
3594                         if (!nfsd4_mach_creds    2568                         if (!nfsd4_mach_creds_match(conf, rqstp)) {
3595                                 status = nfse    2569                                 status = nfserr_wrong_cred;
3596                                 goto out;        2570                                 goto out;
3597                         }                        2571                         }
3598                         if (!creds_match) { /    2572                         if (!creds_match) { /* case 9 */
3599                                 status = nfse    2573                                 status = nfserr_perm;
3600                                 goto out;        2574                                 goto out;
3601                         }                        2575                         }
3602                         if (!verfs_match) { /    2576                         if (!verfs_match) { /* case 8 */
3603                                 status = nfse    2577                                 status = nfserr_not_same;
3604                                 goto out;        2578                                 goto out;
3605                         }                        2579                         }
3606                         /* case 6 */             2580                         /* case 6 */
3607                         exid->flags |= EXCHGI    2581                         exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
3608                         trace_nfsd_clid_confi << 
3609                         goto out_copy;           2582                         goto out_copy;
3610                 }                                2583                 }
3611                 if (!creds_match) { /* case 3    2584                 if (!creds_match) { /* case 3 */
3612                         if (client_has_state(    2585                         if (client_has_state(conf)) {
3613                                 status = nfse    2586                                 status = nfserr_clid_inuse;
3614                                 trace_nfsd_cl << 
3615                                 goto out;        2587                                 goto out;
3616                         }                        2588                         }
3617                         goto out_new;            2589                         goto out_new;
3618                 }                                2590                 }
3619                 if (verfs_match) { /* case 2     2591                 if (verfs_match) { /* case 2 */
3620                         conf->cl_exchange_fla    2592                         conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
3621                         trace_nfsd_clid_confi << 
3622                         goto out_copy;           2593                         goto out_copy;
3623                 }                                2594                 }
3624                 /* case 5, client reboot */      2595                 /* case 5, client reboot */
3625                 trace_nfsd_clid_verf_mismatch << 
3626                 conf = NULL;                     2596                 conf = NULL;
3627                 goto out_new;                    2597                 goto out_new;
3628         }                                        2598         }
3629                                                  2599 
3630         if (update) { /* case 7 */               2600         if (update) { /* case 7 */
3631                 status = nfserr_noent;           2601                 status = nfserr_noent;
3632                 goto out;                        2602                 goto out;
3633         }                                        2603         }
3634                                                  2604 
3635         unconf = find_unconfirmed_client_by_n !! 2605         unconf  = find_unconfirmed_client_by_name(&exid->clname, nn);
3636         if (unconf) /* case 4, possible retry    2606         if (unconf) /* case 4, possible retry or client restart */
3637                 unhash_client_locked(unconf);    2607                 unhash_client_locked(unconf);
3638                                                  2608 
3639         /* case 1, new owner ID */            !! 2609         /* case 1 (normal case) */
3640         trace_nfsd_clid_fresh(new);           << 
3641                                               << 
3642 out_new:                                         2610 out_new:
3643         if (conf) {                              2611         if (conf) {
3644                 status = mark_client_expired_    2612                 status = mark_client_expired_locked(conf);
3645                 if (status)                      2613                 if (status)
3646                         goto out;                2614                         goto out;
3647                 trace_nfsd_clid_replaced(&con << 
3648         }                                        2615         }
3649         new->cl_minorversion = cstate->minorv    2616         new->cl_minorversion = cstate->minorversion;
3650         new->cl_spo_must_allow.u.words[0] = e    2617         new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0];
3651         new->cl_spo_must_allow.u.words[1] = e    2618         new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1];
3652                                                  2619 
3653         /* Contrived initial CREATE_SESSION r !! 2620         gen_clid(new, nn);
3654         new->cl_cs_slot.sl_status = nfserr_se << 
3655                                               << 
3656         add_to_unconfirmed(new);                 2621         add_to_unconfirmed(new);
3657         swap(new, conf);                         2622         swap(new, conf);
3658 out_copy:                                        2623 out_copy:
3659         exid->clientid.cl_boot = conf->cl_cli    2624         exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
3660         exid->clientid.cl_id = conf->cl_clien    2625         exid->clientid.cl_id = conf->cl_clientid.cl_id;
3661                                                  2626 
3662         exid->seqid = conf->cl_cs_slot.sl_seq    2627         exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
3663         nfsd4_set_ex_flags(conf, exid);          2628         nfsd4_set_ex_flags(conf, exid);
3664                                                  2629 
3665         dprintk("nfsd4_exchange_id seqid %d f    2630         dprintk("nfsd4_exchange_id seqid %d flags %x\n",
3666                 conf->cl_cs_slot.sl_seqid, co    2631                 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
3667         status = nfs_ok;                         2632         status = nfs_ok;
3668                                                  2633 
3669 out:                                             2634 out:
3670         spin_unlock(&nn->client_lock);           2635         spin_unlock(&nn->client_lock);
3671 out_nolock:                                      2636 out_nolock:
3672         if (new)                                 2637         if (new)
3673                 expire_client(new);              2638                 expire_client(new);
3674         if (unconf) {                         !! 2639         if (unconf)
3675                 trace_nfsd_clid_expire_unconf << 
3676                 expire_client(unconf);           2640                 expire_client(unconf);
3677         }                                     << 
3678         return status;                           2641         return status;
3679 }                                                2642 }
3680                                                  2643 
3681 static __be32 check_slot_seqid(u32 seqid, u32 !! 2644 static __be32
                                                   >> 2645 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
3682 {                                                2646 {
                                                   >> 2647         dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
                                                   >> 2648                 slot_seqid);
                                                   >> 2649 
3683         /* The slot is in use, and no respons    2650         /* The slot is in use, and no response has been sent. */
3684         if (slot_inuse) {                        2651         if (slot_inuse) {
3685                 if (seqid == slot_seqid)         2652                 if (seqid == slot_seqid)
3686                         return nfserr_jukebox    2653                         return nfserr_jukebox;
3687                 else                             2654                 else
3688                         return nfserr_seq_mis    2655                         return nfserr_seq_misordered;
3689         }                                        2656         }
3690         /* Note unsigned 32-bit arithmetic ha    2657         /* Note unsigned 32-bit arithmetic handles wraparound: */
3691         if (likely(seqid == slot_seqid + 1))     2658         if (likely(seqid == slot_seqid + 1))
3692                 return nfs_ok;                   2659                 return nfs_ok;
3693         if (seqid == slot_seqid)                 2660         if (seqid == slot_seqid)
3694                 return nfserr_replay_cache;      2661                 return nfserr_replay_cache;
3695         return nfserr_seq_misordered;            2662         return nfserr_seq_misordered;
3696 }                                                2663 }
3697                                                  2664 
3698 /*                                               2665 /*
3699  * Cache the create session result into the c    2666  * Cache the create session result into the create session single DRC
3700  * slot cache by saving the xdr structure. sl    2667  * slot cache by saving the xdr structure. sl_seqid has been set.
3701  * Do this for solo or embedded create sessio    2668  * Do this for solo or embedded create session operations.
3702  */                                              2669  */
3703 static void                                      2670 static void
3704 nfsd4_cache_create_session(struct nfsd4_creat    2671 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
3705                            struct nfsd4_clid_    2672                            struct nfsd4_clid_slot *slot, __be32 nfserr)
3706 {                                                2673 {
3707         slot->sl_status = nfserr;                2674         slot->sl_status = nfserr;
3708         memcpy(&slot->sl_cr_ses, cr_ses, size    2675         memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
3709 }                                                2676 }
3710                                                  2677 
3711 static __be32                                    2678 static __be32
3712 nfsd4_replay_create_session(struct nfsd4_crea    2679 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
3713                             struct nfsd4_clid    2680                             struct nfsd4_clid_slot *slot)
3714 {                                                2681 {
3715         memcpy(cr_ses, &slot->sl_cr_ses, size    2682         memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
3716         return slot->sl_status;                  2683         return slot->sl_status;
3717 }                                                2684 }
3718                                                  2685 
3719 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\              2686 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
3720                         2 * 2 + /* credential    2687                         2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
3721                         1 +     /* MIN tag is    2688                         1 +     /* MIN tag is length with zero, only length */ \
3722                         3 +     /* version, o    2689                         3 +     /* version, opcount, opcode */ \
3723                         XDR_QUADLEN(NFS4_MAX_    2690                         XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3724                                 /* seqid, slo    2691                                 /* seqid, slotID, slotID, cache */ \
3725                         4 ) * sizeof(__be32))    2692                         4 ) * sizeof(__be32))
3726                                                  2693 
3727 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\             2694 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
3728                         2 +     /* verifier:     2695                         2 +     /* verifier: AUTH_NULL, length 0 */\
3729                         1 +     /* status */     2696                         1 +     /* status */ \
3730                         1 +     /* MIN tag is    2697                         1 +     /* MIN tag is length with zero, only length */ \
3731                         3 +     /* opcount, o    2698                         3 +     /* opcount, opcode, opstatus*/ \
3732                         XDR_QUADLEN(NFS4_MAX_    2699                         XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3733                                 /* seqid, slo    2700                                 /* seqid, slotID, slotID, slotID, status */ \
3734                         5 ) * sizeof(__be32))    2701                         5 ) * sizeof(__be32))
3735                                                  2702 
3736 static __be32 check_forechannel_attrs(struct     2703 static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
3737 {                                                2704 {
3738         u32 maxrpc = nn->nfsd_serv->sv_max_me    2705         u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
3739                                                  2706 
3740         if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_    2707         if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
3741                 return nfserr_toosmall;          2708                 return nfserr_toosmall;
3742         if (ca->maxresp_sz < NFSD_MIN_RESP_HD    2709         if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
3743                 return nfserr_toosmall;          2710                 return nfserr_toosmall;
3744         ca->headerpadsz = 0;                     2711         ca->headerpadsz = 0;
3745         ca->maxreq_sz = min_t(u32, ca->maxreq    2712         ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
3746         ca->maxresp_sz = min_t(u32, ca->maxre    2713         ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
3747         ca->maxops = min_t(u32, ca->maxops, N    2714         ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
3748         ca->maxresp_cached = min_t(u32, ca->m    2715         ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
3749                         NFSD_SLOT_CACHE_SIZE     2716                         NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
3750         ca->maxreqs = min_t(u32, ca->maxreqs,    2717         ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
3751         /*                                       2718         /*
3752          * Note decreasing slot size below cl    2719          * Note decreasing slot size below client's request may make it
3753          * difficult for client to function c    2720          * difficult for client to function correctly, whereas
3754          * decreasing the number of slots wil    2721          * decreasing the number of slots will (just?) affect
3755          * performance.  When short on memory    2722          * performance.  When short on memory we therefore prefer to
3756          * decrease number of slots instead o    2723          * decrease number of slots instead of their size.  Clients that
3757          * request larger slots than they nee    2724          * request larger slots than they need will get poor results:
3758          * Note that we always allow at least << 
3759          * accounting is soft and provides no << 
3760          */                                      2725          */
3761         ca->maxreqs = nfsd4_get_drc_mem(ca, n !! 2726         ca->maxreqs = nfsd4_get_drc_mem(ca);
                                                   >> 2727         if (!ca->maxreqs)
                                                   >> 2728                 return nfserr_jukebox;
3762                                                  2729 
3763         return nfs_ok;                           2730         return nfs_ok;
3764 }                                                2731 }
3765                                                  2732 
3766 /*                                               2733 /*
3767  * Server's NFSv4.1 backchannel support is AU    2734  * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
3768  * These are based on similar macros in linux    2735  * These are based on similar macros in linux/sunrpc/msg_prot.h .
3769  */                                              2736  */
3770 #define RPC_MAX_HEADER_WITH_AUTH_SYS \           2737 #define RPC_MAX_HEADER_WITH_AUTH_SYS \
3771         (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLS    2738         (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
3772                                                  2739 
3773 #define RPC_MAX_REPHEADER_WITH_AUTH_SYS \        2740 #define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
3774         (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK    2741         (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
3775                                                  2742 
3776 #define NFSD_CB_MAX_REQ_SZ      ((NFS4_enc_cb    2743 #define NFSD_CB_MAX_REQ_SZ      ((NFS4_enc_cb_recall_sz + \
3777                                  RPC_MAX_HEAD    2744                                  RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
3778 #define NFSD_CB_MAX_RESP_SZ     ((NFS4_dec_cb    2745 #define NFSD_CB_MAX_RESP_SZ     ((NFS4_dec_cb_recall_sz + \
3779                                  RPC_MAX_REPH    2746                                  RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
3780                                  sizeof(__be3    2747                                  sizeof(__be32))
3781                                                  2748 
3782 static __be32 check_backchannel_attrs(struct     2749 static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
3783 {                                                2750 {
3784         ca->headerpadsz = 0;                     2751         ca->headerpadsz = 0;
3785                                                  2752 
3786         if (ca->maxreq_sz < NFSD_CB_MAX_REQ_S    2753         if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
3787                 return nfserr_toosmall;          2754                 return nfserr_toosmall;
3788         if (ca->maxresp_sz < NFSD_CB_MAX_RESP    2755         if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
3789                 return nfserr_toosmall;          2756                 return nfserr_toosmall;
3790         ca->maxresp_cached = 0;                  2757         ca->maxresp_cached = 0;
3791         if (ca->maxops < 2)                      2758         if (ca->maxops < 2)
3792                 return nfserr_toosmall;          2759                 return nfserr_toosmall;
3793                                                  2760 
3794         return nfs_ok;                           2761         return nfs_ok;
3795 }                                                2762 }
3796                                                  2763 
3797 static __be32 nfsd4_check_cb_sec(struct nfsd4    2764 static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
3798 {                                                2765 {
3799         switch (cbs->flavor) {                   2766         switch (cbs->flavor) {
3800         case RPC_AUTH_NULL:                      2767         case RPC_AUTH_NULL:
3801         case RPC_AUTH_UNIX:                      2768         case RPC_AUTH_UNIX:
3802                 return nfs_ok;                   2769                 return nfs_ok;
3803         default:                                 2770         default:
3804                 /*                               2771                 /*
3805                  * GSS case: the spec doesn't    2772                  * GSS case: the spec doesn't allow us to return this
3806                  * error.  But it also doesn'    2773                  * error.  But it also doesn't allow us not to support
3807                  * GSS.                          2774                  * GSS.
3808                  * I'd rather this fail hard     2775                  * I'd rather this fail hard than return some error the
3809                  * client might think it can     2776                  * client might think it can already handle:
3810                  */                              2777                  */
3811                 return nfserr_encr_alg_unsupp    2778                 return nfserr_encr_alg_unsupp;
3812         }                                        2779         }
3813 }                                                2780 }
3814                                                  2781 
3815 __be32                                           2782 __be32
3816 nfsd4_create_session(struct svc_rqst *rqstp,     2783 nfsd4_create_session(struct svc_rqst *rqstp,
3817                 struct nfsd4_compound_state * !! 2784                      struct nfsd4_compound_state *cstate,
                                                   >> 2785                      struct nfsd4_create_session *cr_ses)
3818 {                                                2786 {
3819         struct nfsd4_create_session *cr_ses = << 
3820         struct sockaddr *sa = svc_addr(rqstp)    2787         struct sockaddr *sa = svc_addr(rqstp);
3821         struct nfs4_client *conf, *unconf;       2788         struct nfs4_client *conf, *unconf;
3822         struct nfsd4_clid_slot *cs_slot;      << 
3823         struct nfs4_client *old = NULL;          2789         struct nfs4_client *old = NULL;
3824         struct nfsd4_session *new;               2790         struct nfsd4_session *new;
3825         struct nfsd4_conn *conn;                 2791         struct nfsd4_conn *conn;
                                                   >> 2792         struct nfsd4_clid_slot *cs_slot = NULL;
3826         __be32 status = 0;                       2793         __be32 status = 0;
3827         struct nfsd_net *nn = net_generic(SVC    2794         struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3828                                                  2795 
3829         if (cr_ses->flags & ~SESSION4_FLAG_MA    2796         if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
3830                 return nfserr_inval;             2797                 return nfserr_inval;
3831         status = nfsd4_check_cb_sec(&cr_ses->    2798         status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
3832         if (status)                              2799         if (status)
3833                 return status;                   2800                 return status;
3834         status = check_forechannel_attrs(&cr_    2801         status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
3835         if (status)                              2802         if (status)
3836                 return status;                   2803                 return status;
3837         status = check_backchannel_attrs(&cr_    2804         status = check_backchannel_attrs(&cr_ses->back_channel);
3838         if (status)                              2805         if (status)
3839                 goto out_release_drc_mem;        2806                 goto out_release_drc_mem;
3840         status = nfserr_jukebox;                 2807         status = nfserr_jukebox;
3841         new = alloc_session(&cr_ses->fore_cha    2808         new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
3842         if (!new)                                2809         if (!new)
3843                 goto out_release_drc_mem;        2810                 goto out_release_drc_mem;
3844         conn = alloc_conn_from_crses(rqstp, c    2811         conn = alloc_conn_from_crses(rqstp, cr_ses);
3845         if (!conn)                               2812         if (!conn)
3846                 goto out_free_session;           2813                 goto out_free_session;
3847                                                  2814 
3848         spin_lock(&nn->client_lock);             2815         spin_lock(&nn->client_lock);
3849                                               << 
3850         /* RFC 8881 Section 18.36.4 Phase 1:  << 
3851         unconf = find_unconfirmed_client(&cr_    2816         unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
3852         conf = find_confirmed_client(&cr_ses-    2817         conf = find_confirmed_client(&cr_ses->clientid, true, nn);
3853         if (!conf && !unconf) {               !! 2818         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                                                  2819 
3882         /* RFC 8881 Section 18.36.4 Phase 3:  << 
3883         if (conf) {                              2820         if (conf) {
3884                 status = nfserr_wrong_cred;      2821                 status = nfserr_wrong_cred;
3885                 if (!nfsd4_mach_creds_match(c    2822                 if (!nfsd4_mach_creds_match(conf, rqstp))
3886                         goto out_cache_error; !! 2823                         goto out_free_conn;
3887         } else {                              !! 2824                 cs_slot = &conf->cl_cs_slot;
3888                 status = nfserr_clid_inuse;   !! 2825                 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
                                                   >> 2826                 if (status) {
                                                   >> 2827                         if (status == nfserr_replay_cache)
                                                   >> 2828                                 status = nfsd4_replay_create_session(cr_ses, cs_slot);
                                                   >> 2829                         goto out_free_conn;
                                                   >> 2830                 }
                                                   >> 2831         } else if (unconf) {
3889                 if (!same_creds(&unconf->cl_c    2832                 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
3890                     !rpc_cmp_addr(sa, (struct    2833                     !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
3891                         trace_nfsd_clid_cred_ !! 2834                         status = nfserr_clid_inuse;
3892                         goto out_cache_error; !! 2835                         goto out_free_conn;
3893                 }                                2836                 }
3894                 status = nfserr_wrong_cred;      2837                 status = nfserr_wrong_cred;
3895                 if (!nfsd4_mach_creds_match(u    2838                 if (!nfsd4_mach_creds_match(unconf, rqstp))
3896                         goto out_cache_error; !! 2839                         goto out_free_conn;
                                                   >> 2840                 cs_slot = &unconf->cl_cs_slot;
                                                   >> 2841                 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
                                                   >> 2842                 if (status) {
                                                   >> 2843                         /* an unconfirmed replay returns misordered */
                                                   >> 2844                         status = nfserr_seq_misordered;
                                                   >> 2845                         goto out_free_conn;
                                                   >> 2846                 }
3897                 old = find_confirmed_client_b    2847                 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3898                 if (old) {                       2848                 if (old) {
3899                         status = mark_client_    2849                         status = mark_client_expired_locked(old);
3900                         if (status)           !! 2850                         if (status) {
3901                                 goto out_expi !! 2851                                 old = NULL;
3902                         trace_nfsd_clid_repla !! 2852                                 goto out_free_conn;
                                                   >> 2853                         }
3903                 }                                2854                 }
3904                 move_to_confirmed(unconf);       2855                 move_to_confirmed(unconf);
3905                 conf = unconf;                   2856                 conf = unconf;
                                                   >> 2857         } else {
                                                   >> 2858                 status = nfserr_stale_clientid;
                                                   >> 2859                 goto out_free_conn;
3906         }                                        2860         }
3907                                               << 
3908         /* RFC 8881 Section 18.36.4 Phase 4:  << 
3909         status = nfs_ok;                         2861         status = nfs_ok;
3910         /* Persistent sessions are not suppor    2862         /* Persistent sessions are not supported */
3911         cr_ses->flags &= ~SESSION4_PERSIST;      2863         cr_ses->flags &= ~SESSION4_PERSIST;
3912         /* Upshifting from TCP to RDMA is not    2864         /* Upshifting from TCP to RDMA is not supported */
3913         cr_ses->flags &= ~SESSION4_RDMA;         2865         cr_ses->flags &= ~SESSION4_RDMA;
3914                                                  2866 
3915         init_session(rqstp, new, conf, cr_ses    2867         init_session(rqstp, new, conf, cr_ses);
3916         nfsd4_get_session_locked(new);           2868         nfsd4_get_session_locked(new);
3917                                                  2869 
3918         memcpy(cr_ses->sessionid.data, new->s    2870         memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
3919                NFS4_MAX_SESSIONID_LEN);          2871                NFS4_MAX_SESSIONID_LEN);
                                                   >> 2872         cs_slot->sl_seqid++;
                                                   >> 2873         cr_ses->seqid = cs_slot->sl_seqid;
3920                                                  2874 
3921         /* cache solo and embedded create ses    2875         /* cache solo and embedded create sessions under the client_lock */
3922         nfsd4_cache_create_session(cr_ses, cs    2876         nfsd4_cache_create_session(cr_ses, cs_slot, status);
3923         spin_unlock(&nn->client_lock);           2877         spin_unlock(&nn->client_lock);
3924         if (conf == unconf)                   << 
3925                 fsnotify_dentry(conf->cl_nfsd << 
3926         /* init connection and backchannel */    2878         /* init connection and backchannel */
3927         nfsd4_init_conn(rqstp, conn, new);       2879         nfsd4_init_conn(rqstp, conn, new);
3928         nfsd4_put_session(new);                  2880         nfsd4_put_session(new);
3929         if (old)                                 2881         if (old)
3930                 expire_client(old);              2882                 expire_client(old);
3931         return status;                           2883         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:                                   2884 out_free_conn:
3947         spin_unlock(&nn->client_lock);           2885         spin_unlock(&nn->client_lock);
3948         free_conn(conn);                         2886         free_conn(conn);
3949         if (old)                                 2887         if (old)
3950                 expire_client(old);              2888                 expire_client(old);
3951 out_free_session:                                2889 out_free_session:
3952         __free_session(new);                     2890         __free_session(new);
3953 out_release_drc_mem:                             2891 out_release_drc_mem:
3954         nfsd4_put_drc_mem(&cr_ses->fore_chann    2892         nfsd4_put_drc_mem(&cr_ses->fore_channel);
3955         return status;                           2893         return status;
3956 }                                                2894 }
3957                                                  2895 
3958 static __be32 nfsd4_map_bcts_dir(u32 *dir)       2896 static __be32 nfsd4_map_bcts_dir(u32 *dir)
3959 {                                                2897 {
3960         switch (*dir) {                          2898         switch (*dir) {
3961         case NFS4_CDFC4_FORE:                    2899         case NFS4_CDFC4_FORE:
3962         case NFS4_CDFC4_BACK:                    2900         case NFS4_CDFC4_BACK:
3963                 return nfs_ok;                   2901                 return nfs_ok;
3964         case NFS4_CDFC4_FORE_OR_BOTH:            2902         case NFS4_CDFC4_FORE_OR_BOTH:
3965         case NFS4_CDFC4_BACK_OR_BOTH:            2903         case NFS4_CDFC4_BACK_OR_BOTH:
3966                 *dir = NFS4_CDFC4_BOTH;          2904                 *dir = NFS4_CDFC4_BOTH;
3967                 return nfs_ok;                   2905                 return nfs_ok;
3968         }                                     !! 2906         };
3969         return nfserr_inval;                     2907         return nfserr_inval;
3970 }                                                2908 }
3971                                                  2909 
3972 __be32 nfsd4_backchannel_ctl(struct svc_rqst  !! 2910 __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
3973                 struct nfsd4_compound_state * << 
3974                 union nfsd4_op_u *u)          << 
3975 {                                                2911 {
3976         struct nfsd4_backchannel_ctl *bc = &u << 
3977         struct nfsd4_session *session = cstat    2912         struct nfsd4_session *session = cstate->session;
3978         struct nfsd_net *nn = net_generic(SVC    2913         struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3979         __be32 status;                           2914         __be32 status;
3980                                                  2915 
3981         status = nfsd4_check_cb_sec(&bc->bc_c    2916         status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
3982         if (status)                              2917         if (status)
3983                 return status;                   2918                 return status;
3984         spin_lock(&nn->client_lock);             2919         spin_lock(&nn->client_lock);
3985         session->se_cb_prog = bc->bc_cb_progr    2920         session->se_cb_prog = bc->bc_cb_program;
3986         session->se_cb_sec = bc->bc_cb_sec;      2921         session->se_cb_sec = bc->bc_cb_sec;
3987         spin_unlock(&nn->client_lock);           2922         spin_unlock(&nn->client_lock);
3988                                                  2923 
3989         nfsd4_probe_callback(session->se_clie    2924         nfsd4_probe_callback(session->se_client);
3990                                                  2925 
3991         return nfs_ok;                           2926         return nfs_ok;
3992 }                                                2927 }
3993                                                  2928 
3994 static struct nfsd4_conn *__nfsd4_find_conn(s << 
3995 {                                             << 
3996         struct nfsd4_conn *c;                 << 
3997                                               << 
3998         list_for_each_entry(c, &s->se_conns,  << 
3999                 if (c->cn_xprt == xpt) {      << 
4000                         return c;             << 
4001                 }                             << 
4002         }                                     << 
4003         return NULL;                          << 
4004 }                                             << 
4005                                               << 
4006 static __be32 nfsd4_match_existing_connection << 
4007                 struct nfsd4_session *session << 
4008 {                                             << 
4009         struct nfs4_client *clp = session->se << 
4010         struct svc_xprt *xpt = rqst->rq_xprt; << 
4011         struct nfsd4_conn *c;                 << 
4012         __be32 status;                        << 
4013                                               << 
4014         /* Following the last paragraph of RF << 
4015         spin_lock(&clp->cl_lock);             << 
4016         c = __nfsd4_find_conn(xpt, session);  << 
4017         if (!c)                               << 
4018                 status = nfserr_noent;        << 
4019         else if (req == c->cn_flags)          << 
4020                 status = nfs_ok;              << 
4021         else if (req == NFS4_CDFC4_FORE_OR_BO << 
4022                                 c->cn_flags ! << 
4023                 status = nfs_ok;              << 
4024         else if (req == NFS4_CDFC4_BACK_OR_BO << 
4025                                 c->cn_flags ! << 
4026                 status = nfs_ok;              << 
4027         else                                  << 
4028                 status = nfserr_inval;        << 
4029         spin_unlock(&clp->cl_lock);           << 
4030         if (status == nfs_ok && conn)         << 
4031                 *conn = c;                    << 
4032         return status;                        << 
4033 }                                             << 
4034                                               << 
4035 __be32 nfsd4_bind_conn_to_session(struct svc_    2929 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
4036                      struct nfsd4_compound_st    2930                      struct nfsd4_compound_state *cstate,
4037                      union nfsd4_op_u *u)     !! 2931                      struct nfsd4_bind_conn_to_session *bcts)
4038 {                                                2932 {
4039         struct nfsd4_bind_conn_to_session *bc << 
4040         __be32 status;                           2933         __be32 status;
4041         struct nfsd4_conn *conn;                 2934         struct nfsd4_conn *conn;
4042         struct nfsd4_session *session;           2935         struct nfsd4_session *session;
4043         struct net *net = SVC_NET(rqstp);        2936         struct net *net = SVC_NET(rqstp);
4044         struct nfsd_net *nn = net_generic(net    2937         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4045                                                  2938 
4046         if (!nfsd4_last_compound_op(rqstp))      2939         if (!nfsd4_last_compound_op(rqstp))
4047                 return nfserr_not_only_op;       2940                 return nfserr_not_only_op;
4048         spin_lock(&nn->client_lock);             2941         spin_lock(&nn->client_lock);
4049         session = find_in_sessionid_hashtbl(&    2942         session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
4050         spin_unlock(&nn->client_lock);           2943         spin_unlock(&nn->client_lock);
4051         if (!session)                            2944         if (!session)
4052                 goto out_no_session;             2945                 goto out_no_session;
4053         status = nfserr_wrong_cred;              2946         status = nfserr_wrong_cred;
4054         if (!nfsd4_mach_creds_match(session->    2947         if (!nfsd4_mach_creds_match(session->se_client, rqstp))
4055                 goto out;                        2948                 goto out;
4056         status = nfsd4_match_existing_connect << 
4057                         bcts->dir, &conn);    << 
4058         if (status == nfs_ok) {               << 
4059                 if (bcts->dir == NFS4_CDFC4_F << 
4060                                 bcts->dir ==  << 
4061                         conn->cn_flags |= NFS << 
4062                 nfsd4_probe_callback(session- << 
4063                 goto out;                     << 
4064         }                                     << 
4065         if (status == nfserr_inval)           << 
4066                 goto out;                     << 
4067         status = nfsd4_map_bcts_dir(&bcts->di    2949         status = nfsd4_map_bcts_dir(&bcts->dir);
4068         if (status)                              2950         if (status)
4069                 goto out;                        2951                 goto out;
4070         conn = alloc_conn(rqstp, bcts->dir);     2952         conn = alloc_conn(rqstp, bcts->dir);
4071         status = nfserr_jukebox;                 2953         status = nfserr_jukebox;
4072         if (!conn)                               2954         if (!conn)
4073                 goto out;                        2955                 goto out;
4074         nfsd4_init_conn(rqstp, conn, session)    2956         nfsd4_init_conn(rqstp, conn, session);
4075         status = nfs_ok;                         2957         status = nfs_ok;
4076 out:                                             2958 out:
4077         nfsd4_put_session(session);              2959         nfsd4_put_session(session);
4078 out_no_session:                                  2960 out_no_session:
4079         return status;                           2961         return status;
4080 }                                                2962 }
4081                                                  2963 
4082 static bool nfsd4_compound_in_session(struct  !! 2964 static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
4083 {                                                2965 {
4084         if (!cstate->session)                 !! 2966         if (!session)
4085                 return false;                 !! 2967                 return 0;
4086         return !memcmp(sid, &cstate->session- !! 2968         return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
4087 }                                                2969 }
4088                                                  2970 
4089 __be32                                           2971 __be32
4090 nfsd4_destroy_session(struct svc_rqst *r, str !! 2972 nfsd4_destroy_session(struct svc_rqst *r,
4091                 union nfsd4_op_u *u)          !! 2973                       struct nfsd4_compound_state *cstate,
                                                   >> 2974                       struct nfsd4_destroy_session *sessionid)
4092 {                                                2975 {
4093         struct nfs4_sessionid *sessionid = &u << 
4094         struct nfsd4_session *ses;               2976         struct nfsd4_session *ses;
4095         __be32 status;                           2977         __be32 status;
4096         int ref_held_by_me = 0;                  2978         int ref_held_by_me = 0;
4097         struct net *net = SVC_NET(r);            2979         struct net *net = SVC_NET(r);
4098         struct nfsd_net *nn = net_generic(net    2980         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4099                                                  2981 
4100         status = nfserr_not_only_op;             2982         status = nfserr_not_only_op;
4101         if (nfsd4_compound_in_session(cstate, !! 2983         if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
4102                 if (!nfsd4_last_compound_op(r    2984                 if (!nfsd4_last_compound_op(r))
4103                         goto out;                2985                         goto out;
4104                 ref_held_by_me++;                2986                 ref_held_by_me++;
4105         }                                        2987         }
4106         dump_sessionid(__func__, sessionid);  !! 2988         dump_sessionid(__func__, &sessionid->sessionid);
4107         spin_lock(&nn->client_lock);             2989         spin_lock(&nn->client_lock);
4108         ses = find_in_sessionid_hashtbl(sessi !! 2990         ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
4109         if (!ses)                                2991         if (!ses)
4110                 goto out_client_lock;            2992                 goto out_client_lock;
4111         status = nfserr_wrong_cred;              2993         status = nfserr_wrong_cred;
4112         if (!nfsd4_mach_creds_match(ses->se_c    2994         if (!nfsd4_mach_creds_match(ses->se_client, r))
4113                 goto out_put_session;            2995                 goto out_put_session;
4114         status = mark_session_dead_locked(ses    2996         status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
4115         if (status)                              2997         if (status)
4116                 goto out_put_session;            2998                 goto out_put_session;
4117         unhash_session(ses);                     2999         unhash_session(ses);
4118         spin_unlock(&nn->client_lock);           3000         spin_unlock(&nn->client_lock);
4119                                                  3001 
4120         nfsd4_probe_callback_sync(ses->se_cli    3002         nfsd4_probe_callback_sync(ses->se_client);
4121                                                  3003 
4122         spin_lock(&nn->client_lock);             3004         spin_lock(&nn->client_lock);
4123         status = nfs_ok;                         3005         status = nfs_ok;
4124 out_put_session:                                 3006 out_put_session:
4125         nfsd4_put_session_locked(ses);           3007         nfsd4_put_session_locked(ses);
4126 out_client_lock:                                 3008 out_client_lock:
4127         spin_unlock(&nn->client_lock);           3009         spin_unlock(&nn->client_lock);
4128 out:                                             3010 out:
4129         return status;                           3011         return status;
4130 }                                                3012 }
4131                                                  3013 
                                                   >> 3014 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
                                                   >> 3015 {
                                                   >> 3016         struct nfsd4_conn *c;
                                                   >> 3017 
                                                   >> 3018         list_for_each_entry(c, &s->se_conns, cn_persession) {
                                                   >> 3019                 if (c->cn_xprt == xpt) {
                                                   >> 3020                         return c;
                                                   >> 3021                 }
                                                   >> 3022         }
                                                   >> 3023         return NULL;
                                                   >> 3024 }
                                                   >> 3025 
4132 static __be32 nfsd4_sequence_check_conn(struc    3026 static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
4133 {                                                3027 {
4134         struct nfs4_client *clp = ses->se_cli    3028         struct nfs4_client *clp = ses->se_client;
4135         struct nfsd4_conn *c;                    3029         struct nfsd4_conn *c;
4136         __be32 status = nfs_ok;                  3030         __be32 status = nfs_ok;
4137         int ret;                                 3031         int ret;
4138                                                  3032 
4139         spin_lock(&clp->cl_lock);                3033         spin_lock(&clp->cl_lock);
4140         c = __nfsd4_find_conn(new->cn_xprt, s    3034         c = __nfsd4_find_conn(new->cn_xprt, ses);
4141         if (c)                                   3035         if (c)
4142                 goto out_free;                   3036                 goto out_free;
4143         status = nfserr_conn_not_bound_to_ses    3037         status = nfserr_conn_not_bound_to_session;
4144         if (clp->cl_mach_cred)                   3038         if (clp->cl_mach_cred)
4145                 goto out_free;                   3039                 goto out_free;
4146         __nfsd4_hash_conn(new, ses);             3040         __nfsd4_hash_conn(new, ses);
4147         spin_unlock(&clp->cl_lock);              3041         spin_unlock(&clp->cl_lock);
4148         ret = nfsd4_register_conn(new);          3042         ret = nfsd4_register_conn(new);
4149         if (ret)                                 3043         if (ret)
4150                 /* oops; xprt is already down    3044                 /* oops; xprt is already down: */
4151                 nfsd4_conn_lost(&new->cn_xpt_    3045                 nfsd4_conn_lost(&new->cn_xpt_user);
4152         return nfs_ok;                           3046         return nfs_ok;
4153 out_free:                                        3047 out_free:
4154         spin_unlock(&clp->cl_lock);              3048         spin_unlock(&clp->cl_lock);
4155         free_conn(new);                          3049         free_conn(new);
4156         return status;                           3050         return status;
4157 }                                                3051 }
4158                                                  3052 
4159 static bool nfsd4_session_too_many_ops(struct    3053 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
4160 {                                                3054 {
4161         struct nfsd4_compoundargs *args = rqs    3055         struct nfsd4_compoundargs *args = rqstp->rq_argp;
4162                                                  3056 
4163         return args->opcnt > session->se_fcha    3057         return args->opcnt > session->se_fchannel.maxops;
4164 }                                                3058 }
4165                                                  3059 
4166 static bool nfsd4_request_too_big(struct svc_    3060 static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
4167                                   struct nfsd    3061                                   struct nfsd4_session *session)
4168 {                                                3062 {
4169         struct xdr_buf *xb = &rqstp->rq_arg;     3063         struct xdr_buf *xb = &rqstp->rq_arg;
4170                                                  3064 
4171         return xb->len > session->se_fchannel    3065         return xb->len > session->se_fchannel.maxreq_sz;
4172 }                                                3066 }
4173                                                  3067 
4174 static bool replay_matches_cache(struct svc_r    3068 static bool replay_matches_cache(struct svc_rqst *rqstp,
4175                  struct nfsd4_sequence *seq,     3069                  struct nfsd4_sequence *seq, struct nfsd4_slot *slot)
4176 {                                                3070 {
4177         struct nfsd4_compoundargs *argp = rqs    3071         struct nfsd4_compoundargs *argp = rqstp->rq_argp;
4178                                                  3072 
4179         if ((bool)(slot->sl_flags & NFSD4_SLO    3073         if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) !=
4180             (bool)seq->cachethis)                3074             (bool)seq->cachethis)
4181                 return false;                    3075                 return false;
4182         /*                                       3076         /*
4183          * If there's an error then the reply    3077          * If there's an error then the reply can have fewer ops than
4184          * the call.                             3078          * the call.
4185          */                                      3079          */
4186         if (slot->sl_opcnt < argp->opcnt && !    3080         if (slot->sl_opcnt < argp->opcnt && !slot->sl_status)
4187                 return false;                    3081                 return false;
4188         /*                                       3082         /*
4189          * But if we cached a reply with *mor    3083          * But if we cached a reply with *more* ops than the call you're
4190          * sending us now, then this new call    3084          * sending us now, then this new call is clearly not really a
4191          * replay of the old one:                3085          * replay of the old one:
4192          */                                      3086          */
4193         if (slot->sl_opcnt > argp->opcnt)        3087         if (slot->sl_opcnt > argp->opcnt)
4194                 return false;                    3088                 return false;
4195         /* This is the only check explicitly     3089         /* This is the only check explicitly called by spec: */
4196         if (!same_creds(&rqstp->rq_cred, &slo    3090         if (!same_creds(&rqstp->rq_cred, &slot->sl_cred))
4197                 return false;                    3091                 return false;
4198         /*                                       3092         /*
4199          * There may be more comparisons we c    3093          * There may be more comparisons we could actually do, but the
4200          * spec doesn't require us to catch e    3094          * spec doesn't require us to catch every case where the calls
4201          * don't match (that would require ca    3095          * don't match (that would require caching the call as well as
4202          * the reply), so we don't bother.       3096          * the reply), so we don't bother.
4203          */                                      3097          */
4204         return true;                             3098         return true;
4205 }                                                3099 }
4206                                                  3100 
4207 __be32                                           3101 __be32
4208 nfsd4_sequence(struct svc_rqst *rqstp, struct !! 3102 nfsd4_sequence(struct svc_rqst *rqstp,
4209                 union nfsd4_op_u *u)          !! 3103                struct nfsd4_compound_state *cstate,
                                                   >> 3104                struct nfsd4_sequence *seq)
4210 {                                                3105 {
4211         struct nfsd4_sequence *seq = &u->sequ << 
4212         struct nfsd4_compoundres *resp = rqst    3106         struct nfsd4_compoundres *resp = rqstp->rq_resp;
4213         struct xdr_stream *xdr = resp->xdr;   !! 3107         struct xdr_stream *xdr = &resp->xdr;
4214         struct nfsd4_session *session;           3108         struct nfsd4_session *session;
4215         struct nfs4_client *clp;                 3109         struct nfs4_client *clp;
4216         struct nfsd4_slot *slot;                 3110         struct nfsd4_slot *slot;
4217         struct nfsd4_conn *conn;                 3111         struct nfsd4_conn *conn;
4218         __be32 status;                           3112         __be32 status;
4219         int buflen;                              3113         int buflen;
4220         struct net *net = SVC_NET(rqstp);        3114         struct net *net = SVC_NET(rqstp);
4221         struct nfsd_net *nn = net_generic(net    3115         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4222                                                  3116 
4223         if (resp->opcnt != 1)                    3117         if (resp->opcnt != 1)
4224                 return nfserr_sequence_pos;      3118                 return nfserr_sequence_pos;
4225                                                  3119 
4226         /*                                       3120         /*
4227          * Will be either used or freed by nf    3121          * Will be either used or freed by nfsd4_sequence_check_conn
4228          * below.                                3122          * below.
4229          */                                      3123          */
4230         conn = alloc_conn(rqstp, NFS4_CDFC4_F    3124         conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
4231         if (!conn)                               3125         if (!conn)
4232                 return nfserr_jukebox;           3126                 return nfserr_jukebox;
4233                                                  3127 
4234         spin_lock(&nn->client_lock);             3128         spin_lock(&nn->client_lock);
4235         session = find_in_sessionid_hashtbl(&    3129         session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
4236         if (!session)                            3130         if (!session)
4237                 goto out_no_session;             3131                 goto out_no_session;
4238         clp = session->se_client;                3132         clp = session->se_client;
4239                                                  3133 
4240         status = nfserr_too_many_ops;            3134         status = nfserr_too_many_ops;
4241         if (nfsd4_session_too_many_ops(rqstp,    3135         if (nfsd4_session_too_many_ops(rqstp, session))
4242                 goto out_put_session;            3136                 goto out_put_session;
4243                                                  3137 
4244         status = nfserr_req_too_big;             3138         status = nfserr_req_too_big;
4245         if (nfsd4_request_too_big(rqstp, sess    3139         if (nfsd4_request_too_big(rqstp, session))
4246                 goto out_put_session;            3140                 goto out_put_session;
4247                                                  3141 
4248         status = nfserr_badslot;                 3142         status = nfserr_badslot;
4249         if (seq->slotid >= session->se_fchann    3143         if (seq->slotid >= session->se_fchannel.maxreqs)
4250                 goto out_put_session;            3144                 goto out_put_session;
4251                                                  3145 
4252         slot = session->se_slots[seq->slotid]    3146         slot = session->se_slots[seq->slotid];
4253         dprintk("%s: slotid %d\n", __func__,     3147         dprintk("%s: slotid %d\n", __func__, seq->slotid);
4254                                                  3148 
4255         /* We do not negotiate the number of     3149         /* We do not negotiate the number of slots yet, so set the
4256          * maxslots to the session maxreqs wh    3150          * maxslots to the session maxreqs which is used to encode
4257          * sr_highest_slotid and the sr_targe    3151          * sr_highest_slotid and the sr_target_slot id to maxslots */
4258         seq->maxslots = session->se_fchannel.    3152         seq->maxslots = session->se_fchannel.maxreqs;
4259                                                  3153 
4260         trace_nfsd_slot_seqid_sequence(clp, s << 
4261         status = check_slot_seqid(seq->seqid,    3154         status = check_slot_seqid(seq->seqid, slot->sl_seqid,
4262                                         slot-    3155                                         slot->sl_flags & NFSD4_SLOT_INUSE);
4263         if (status == nfserr_replay_cache) {     3156         if (status == nfserr_replay_cache) {
4264                 status = nfserr_seq_misordere    3157                 status = nfserr_seq_misordered;
4265                 if (!(slot->sl_flags & NFSD4_    3158                 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
4266                         goto out_put_session;    3159                         goto out_put_session;
4267                 status = nfserr_seq_false_ret    3160                 status = nfserr_seq_false_retry;
4268                 if (!replay_matches_cache(rqs    3161                 if (!replay_matches_cache(rqstp, seq, slot))
4269                         goto out_put_session;    3162                         goto out_put_session;
4270                 cstate->slot = slot;             3163                 cstate->slot = slot;
4271                 cstate->session = session;       3164                 cstate->session = session;
4272                 cstate->clp = clp;               3165                 cstate->clp = clp;
4273                 /* Return the cached reply st    3166                 /* Return the cached reply status and set cstate->status
4274                  * for nfsd4_proc_compound pr    3167                  * for nfsd4_proc_compound processing */
4275                 status = nfsd4_replay_cache_e    3168                 status = nfsd4_replay_cache_entry(resp, seq);
4276                 cstate->status = nfserr_repla    3169                 cstate->status = nfserr_replay_cache;
4277                 goto out;                        3170                 goto out;
4278         }                                        3171         }
4279         if (status)                              3172         if (status)
4280                 goto out_put_session;            3173                 goto out_put_session;
4281                                                  3174 
4282         status = nfsd4_sequence_check_conn(co    3175         status = nfsd4_sequence_check_conn(conn, session);
4283         conn = NULL;                             3176         conn = NULL;
4284         if (status)                              3177         if (status)
4285                 goto out_put_session;            3178                 goto out_put_session;
4286                                                  3179 
4287         buflen = (seq->cachethis) ?              3180         buflen = (seq->cachethis) ?
4288                         session->se_fchannel.    3181                         session->se_fchannel.maxresp_cached :
4289                         session->se_fchannel.    3182                         session->se_fchannel.maxresp_sz;
4290         status = (seq->cachethis) ? nfserr_re    3183         status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
4291                                     nfserr_re    3184                                     nfserr_rep_too_big;
4292         if (xdr_restrict_buflen(xdr, buflen -    3185         if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
4293                 goto out_put_session;            3186                 goto out_put_session;
4294         svc_reserve(rqstp, buflen);              3187         svc_reserve(rqstp, buflen);
4295                                                  3188 
4296         status = nfs_ok;                         3189         status = nfs_ok;
4297         /* Success! bump slot seqid */           3190         /* Success! bump slot seqid */
4298         slot->sl_seqid = seq->seqid;             3191         slot->sl_seqid = seq->seqid;
4299         slot->sl_flags |= NFSD4_SLOT_INUSE;      3192         slot->sl_flags |= NFSD4_SLOT_INUSE;
4300         if (seq->cachethis)                      3193         if (seq->cachethis)
4301                 slot->sl_flags |= NFSD4_SLOT_    3194                 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
4302         else                                     3195         else
4303                 slot->sl_flags &= ~NFSD4_SLOT    3196                 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
4304                                                  3197 
4305         cstate->slot = slot;                     3198         cstate->slot = slot;
4306         cstate->session = session;               3199         cstate->session = session;
4307         cstate->clp = clp;                       3200         cstate->clp = clp;
4308                                                  3201 
4309 out:                                             3202 out:
4310         switch (clp->cl_cb_state) {              3203         switch (clp->cl_cb_state) {
4311         case NFSD4_CB_DOWN:                      3204         case NFSD4_CB_DOWN:
4312                 seq->status_flags = SEQ4_STAT    3205                 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
4313                 break;                           3206                 break;
4314         case NFSD4_CB_FAULT:                     3207         case NFSD4_CB_FAULT:
4315                 seq->status_flags = SEQ4_STAT    3208                 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
4316                 break;                           3209                 break;
4317         default:                                 3210         default:
4318                 seq->status_flags = 0;           3211                 seq->status_flags = 0;
4319         }                                        3212         }
4320         if (!list_empty(&clp->cl_revoked))       3213         if (!list_empty(&clp->cl_revoked))
4321                 seq->status_flags |= SEQ4_STA    3214                 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:                                  3215 out_no_session:
4326         if (conn)                                3216         if (conn)
4327                 free_conn(conn);                 3217                 free_conn(conn);
4328         spin_unlock(&nn->client_lock);           3218         spin_unlock(&nn->client_lock);
4329         return status;                           3219         return status;
4330 out_put_session:                                 3220 out_put_session:
4331         nfsd4_put_session_locked(session);       3221         nfsd4_put_session_locked(session);
4332         goto out_no_session;                     3222         goto out_no_session;
4333 }                                                3223 }
4334                                                  3224 
4335 void                                             3225 void
4336 nfsd4_sequence_done(struct nfsd4_compoundres     3226 nfsd4_sequence_done(struct nfsd4_compoundres *resp)
4337 {                                                3227 {
4338         struct nfsd4_compound_state *cs = &re    3228         struct nfsd4_compound_state *cs = &resp->cstate;
4339                                                  3229 
4340         if (nfsd4_has_session(cs)) {             3230         if (nfsd4_has_session(cs)) {
4341                 if (cs->status != nfserr_repl    3231                 if (cs->status != nfserr_replay_cache) {
4342                         nfsd4_store_cache_ent    3232                         nfsd4_store_cache_entry(resp);
4343                         cs->slot->sl_flags &=    3233                         cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
4344                 }                                3234                 }
4345                 /* Drop session reference tha    3235                 /* Drop session reference that was taken in nfsd4_sequence() */
4346                 nfsd4_put_session(cs->session    3236                 nfsd4_put_session(cs->session);
4347         } else if (cs->clp)                      3237         } else if (cs->clp)
4348                 put_client_renew(cs->clp);       3238                 put_client_renew(cs->clp);
4349 }                                                3239 }
4350                                                  3240 
4351 __be32                                           3241 __be32
4352 nfsd4_destroy_clientid(struct svc_rqst *rqstp !! 3242 nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
4353                 struct nfsd4_compound_state * << 
4354                 union nfsd4_op_u *u)          << 
4355 {                                                3243 {
4356         struct nfsd4_destroy_clientid *dc = & << 
4357         struct nfs4_client *conf, *unconf;       3244         struct nfs4_client *conf, *unconf;
4358         struct nfs4_client *clp = NULL;          3245         struct nfs4_client *clp = NULL;
4359         __be32 status = 0;                       3246         __be32 status = 0;
4360         struct nfsd_net *nn = net_generic(SVC    3247         struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4361                                                  3248 
4362         spin_lock(&nn->client_lock);             3249         spin_lock(&nn->client_lock);
4363         unconf = find_unconfirmed_client(&dc-    3250         unconf = find_unconfirmed_client(&dc->clientid, true, nn);
4364         conf = find_confirmed_client(&dc->cli    3251         conf = find_confirmed_client(&dc->clientid, true, nn);
4365         WARN_ON_ONCE(conf && unconf);            3252         WARN_ON_ONCE(conf && unconf);
4366                                                  3253 
4367         if (conf) {                              3254         if (conf) {
4368                 if (client_has_state(conf)) {    3255                 if (client_has_state(conf)) {
4369                         status = nfserr_clien    3256                         status = nfserr_clientid_busy;
4370                         goto out;                3257                         goto out;
4371                 }                                3258                 }
4372                 status = mark_client_expired_    3259                 status = mark_client_expired_locked(conf);
4373                 if (status)                      3260                 if (status)
4374                         goto out;                3261                         goto out;
4375                 clp = conf;                      3262                 clp = conf;
4376         } else if (unconf)                       3263         } else if (unconf)
4377                 clp = unconf;                    3264                 clp = unconf;
4378         else {                                   3265         else {
4379                 status = nfserr_stale_clienti    3266                 status = nfserr_stale_clientid;
4380                 goto out;                        3267                 goto out;
4381         }                                        3268         }
4382         if (!nfsd4_mach_creds_match(clp, rqst    3269         if (!nfsd4_mach_creds_match(clp, rqstp)) {
4383                 clp = NULL;                      3270                 clp = NULL;
4384                 status = nfserr_wrong_cred;      3271                 status = nfserr_wrong_cred;
4385                 goto out;                        3272                 goto out;
4386         }                                        3273         }
4387         trace_nfsd_clid_destroyed(&clp->cl_cl << 
4388         unhash_client_locked(clp);               3274         unhash_client_locked(clp);
4389 out:                                             3275 out:
4390         spin_unlock(&nn->client_lock);           3276         spin_unlock(&nn->client_lock);
4391         if (clp)                                 3277         if (clp)
4392                 expire_client(clp);              3278                 expire_client(clp);
4393         return status;                           3279         return status;
4394 }                                                3280 }
4395                                                  3281 
4396 __be32                                           3282 __be32
4397 nfsd4_reclaim_complete(struct svc_rqst *rqstp !! 3283 nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
4398                 struct nfsd4_compound_state * << 
4399 {                                                3284 {
4400         struct nfsd4_reclaim_complete *rc = & << 
4401         struct nfs4_client *clp = cstate->clp << 
4402         __be32 status = 0;                       3285         __be32 status = 0;
4403                                                  3286 
4404         if (rc->rca_one_fs) {                    3287         if (rc->rca_one_fs) {
4405                 if (!cstate->current_fh.fh_de    3288                 if (!cstate->current_fh.fh_dentry)
4406                         return nfserr_nofileh    3289                         return nfserr_nofilehandle;
4407                 /*                               3290                 /*
4408                  * We don't take advantage of    3291                  * We don't take advantage of the rca_one_fs case.
4409                  * That's OK, it's optional,     3292                  * That's OK, it's optional, we can safely ignore it.
4410                  */                              3293                  */
4411                 return nfs_ok;                   3294                 return nfs_ok;
4412         }                                        3295         }
4413                                                  3296 
4414         status = nfserr_complete_already;        3297         status = nfserr_complete_already;
4415         if (test_and_set_bit(NFSD4_CLIENT_REC !! 3298         if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
                                                   >> 3299                              &cstate->session->se_client->cl_flags))
4416                 goto out;                        3300                 goto out;
4417                                                  3301 
4418         status = nfserr_stale_clientid;          3302         status = nfserr_stale_clientid;
4419         if (is_client_expired(clp))           !! 3303         if (is_client_expired(cstate->session->se_client))
4420                 /*                               3304                 /*
4421                  * The following error isn't     3305                  * The following error isn't really legal.
4422                  * But we only get here if th    3306                  * But we only get here if the client just explicitly
4423                  * destroyed the client.  Sur    3307                  * destroyed the client.  Surely it no longer cares what
4424                  * error it gets back on an o    3308                  * error it gets back on an operation for the dead
4425                  * client.                       3309                  * client.
4426                  */                              3310                  */
4427                 goto out;                        3311                 goto out;
4428                                                  3312 
4429         status = nfs_ok;                         3313         status = nfs_ok;
4430         trace_nfsd_clid_reclaim_complete(&clp !! 3314         nfsd4_client_record_create(cstate->session->se_client);
4431         nfsd4_client_record_create(clp);      << 
4432         inc_reclaim_complete(clp);            << 
4433 out:                                             3315 out:
4434         return status;                           3316         return status;
4435 }                                                3317 }
4436                                                  3318 
4437 __be32                                           3319 __be32
4438 nfsd4_setclientid(struct svc_rqst *rqstp, str    3320 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4439                   union nfsd4_op_u *u)        !! 3321                   struct nfsd4_setclientid *setclid)
4440 {                                                3322 {
4441         struct nfsd4_setclientid *setclid = & << 
4442         struct xdr_netobj       clname = setc    3323         struct xdr_netobj       clname = setclid->se_name;
4443         nfs4_verifier           clverifier =     3324         nfs4_verifier           clverifier = setclid->se_verf;
4444         struct nfs4_client      *conf, *new;     3325         struct nfs4_client      *conf, *new;
4445         struct nfs4_client      *unconf = NUL    3326         struct nfs4_client      *unconf = NULL;
4446         __be32                  status;          3327         __be32                  status;
4447         struct nfsd_net         *nn = net_gen    3328         struct nfsd_net         *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4448                                                  3329 
4449         new = create_client(clname, rqstp, &c    3330         new = create_client(clname, rqstp, &clverifier);
4450         if (new == NULL)                         3331         if (new == NULL)
4451                 return nfserr_jukebox;           3332                 return nfserr_jukebox;
                                                   >> 3333         /* Cases below refer to rfc 3530 section 14.2.33: */
4452         spin_lock(&nn->client_lock);             3334         spin_lock(&nn->client_lock);
4453         conf = find_confirmed_client_by_name(    3335         conf = find_confirmed_client_by_name(&clname, nn);
4454         if (conf && client_has_state(conf)) {    3336         if (conf && client_has_state(conf)) {
                                                   >> 3337                 /* case 0: */
4455                 status = nfserr_clid_inuse;      3338                 status = nfserr_clid_inuse;
4456                 if (clp_used_exchangeid(conf)    3339                 if (clp_used_exchangeid(conf))
4457                         goto out;                3340                         goto out;
4458                 if (!same_creds(&conf->cl_cre    3341                 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
4459                         trace_nfsd_clid_cred_ !! 3342                         char addr_str[INET6_ADDRSTRLEN];
                                                   >> 3343                         rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
                                                   >> 3344                                  sizeof(addr_str));
                                                   >> 3345                         dprintk("NFSD: setclientid: string in use by client "
                                                   >> 3346                                 "at %s\n", addr_str);
4460                         goto out;                3347                         goto out;
4461                 }                                3348                 }
4462         }                                        3349         }
4463         unconf = find_unconfirmed_client_by_n    3350         unconf = find_unconfirmed_client_by_name(&clname, nn);
4464         if (unconf)                              3351         if (unconf)
4465                 unhash_client_locked(unconf);    3352                 unhash_client_locked(unconf);
4466         if (conf) {                           !! 3353         if (conf && same_verf(&conf->cl_verifier, &clverifier)) {
4467                 if (same_verf(&conf->cl_verif !! 3354                 /* case 1: probable callback update */
4468                         copy_clid(new, conf); !! 3355                 copy_clid(new, conf);
4469                         gen_confirm(new, nn); !! 3356                 gen_confirm(new, nn);
4470                 } else                        !! 3357         } else /* case 4 (new client) or cases 2, 3 (client reboot): */
4471                         trace_nfsd_clid_verf_ !! 3358                 gen_clid(new, nn);
4472                                               << 
4473         } else                                << 
4474                 trace_nfsd_clid_fresh(new);   << 
4475         new->cl_minorversion = 0;                3359         new->cl_minorversion = 0;
4476         gen_callback(new, setclid, rqstp);       3360         gen_callback(new, setclid, rqstp);
4477         add_to_unconfirmed(new);                 3361         add_to_unconfirmed(new);
4478         setclid->se_clientid.cl_boot = new->c    3362         setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
4479         setclid->se_clientid.cl_id = new->cl_    3363         setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
4480         memcpy(setclid->se_confirm.data, new-    3364         memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
4481         new = NULL;                              3365         new = NULL;
4482         status = nfs_ok;                         3366         status = nfs_ok;
4483 out:                                             3367 out:
4484         spin_unlock(&nn->client_lock);           3368         spin_unlock(&nn->client_lock);
4485         if (new)                                 3369         if (new)
4486                 free_client(new);                3370                 free_client(new);
4487         if (unconf) {                         !! 3371         if (unconf)
4488                 trace_nfsd_clid_expire_unconf << 
4489                 expire_client(unconf);           3372                 expire_client(unconf);
4490         }                                     << 
4491         return status;                           3373         return status;
4492 }                                                3374 }
4493                                                  3375 
                                                   >> 3376 
4494 __be32                                           3377 __be32
4495 nfsd4_setclientid_confirm(struct svc_rqst *rq    3378 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
4496                         struct nfsd4_compound !! 3379                          struct nfsd4_compound_state *cstate,
4497                         union nfsd4_op_u *u)  !! 3380                          struct nfsd4_setclientid_confirm *setclientid_confirm)
4498 {                                                3381 {
4499         struct nfsd4_setclientid_confirm *set << 
4500                         &u->setclientid_confi << 
4501         struct nfs4_client *conf, *unconf;       3382         struct nfs4_client *conf, *unconf;
4502         struct nfs4_client *old = NULL;          3383         struct nfs4_client *old = NULL;
4503         nfs4_verifier confirm = setclientid_c    3384         nfs4_verifier confirm = setclientid_confirm->sc_confirm; 
4504         clientid_t * clid = &setclientid_conf    3385         clientid_t * clid = &setclientid_confirm->sc_clientid;
4505         __be32 status;                           3386         __be32 status;
4506         struct nfsd_net *nn = net_generic(SVC    3387         struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4507                                                  3388 
4508         if (STALE_CLIENTID(clid, nn))            3389         if (STALE_CLIENTID(clid, nn))
4509                 return nfserr_stale_clientid;    3390                 return nfserr_stale_clientid;
4510                                                  3391 
4511         spin_lock(&nn->client_lock);             3392         spin_lock(&nn->client_lock);
4512         conf = find_confirmed_client(clid, fa    3393         conf = find_confirmed_client(clid, false, nn);
4513         unconf = find_unconfirmed_client(clid    3394         unconf = find_unconfirmed_client(clid, false, nn);
4514         /*                                       3395         /*
4515          * We try hard to give out unique cli    3396          * We try hard to give out unique clientid's, so if we get an
4516          * attempt to confirm the same client    3397          * attempt to confirm the same clientid with a different cred,
4517          * the client may be buggy; this shou    3398          * the client may be buggy; this should never happen.
4518          *                                       3399          *
4519          * Nevertheless, RFC 7530 recommends     3400          * Nevertheless, RFC 7530 recommends INUSE for this case:
4520          */                                      3401          */
4521         status = nfserr_clid_inuse;              3402         status = nfserr_clid_inuse;
4522         if (unconf && !same_creds(&unconf->cl !! 3403         if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
4523                 trace_nfsd_clid_cred_mismatch << 
4524                 goto out;                        3404                 goto out;
4525         }                                     !! 3405         if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
4526         if (conf && !same_creds(&conf->cl_cre << 
4527                 trace_nfsd_clid_cred_mismatch << 
4528                 goto out;                        3406                 goto out;
4529         }                                     !! 3407         /* cases below refer to rfc 3530 section 14.2.34: */
4530         if (!unconf || !same_verf(&confirm, &    3408         if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
4531                 if (conf && same_verf(&confir    3409                 if (conf && same_verf(&confirm, &conf->cl_confirm)) {
                                                   >> 3410                         /* case 2: probable retransmit */
4532                         status = nfs_ok;         3411                         status = nfs_ok;
4533                 } else                        !! 3412                 } else /* case 4: client hasn't noticed we rebooted yet? */
4534                         status = nfserr_stale    3413                         status = nfserr_stale_clientid;
4535                 goto out;                        3414                 goto out;
4536         }                                        3415         }
4537         status = nfs_ok;                         3416         status = nfs_ok;
4538         if (conf) {                           !! 3417         if (conf) { /* case 1: callback update */
4539                 old = unconf;                    3418                 old = unconf;
4540                 unhash_client_locked(old);       3419                 unhash_client_locked(old);
4541                 nfsd4_change_callback(conf, &    3420                 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
4542         } else {                              !! 3421         } else { /* case 3: normal case; new or rebooted client */
4543                 old = find_confirmed_client_b    3422                 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
4544                 if (old) {                       3423                 if (old) {
4545                         status = nfserr_clid_    3424                         status = nfserr_clid_inuse;
4546                         if (client_has_state(    3425                         if (client_has_state(old)
4547                                         && !s    3426                                         && !same_creds(&unconf->cl_cred,
4548                                                  3427                                                         &old->cl_cred)) {
4549                                 old = NULL;      3428                                 old = NULL;
4550                                 goto out;        3429                                 goto out;
4551                         }                        3430                         }
4552                         status = mark_client_    3431                         status = mark_client_expired_locked(old);
4553                         if (status) {            3432                         if (status) {
4554                                 old = NULL;      3433                                 old = NULL;
4555                                 goto out;        3434                                 goto out;
4556                         }                        3435                         }
4557                         trace_nfsd_clid_repla << 
4558                 }                                3436                 }
4559                 move_to_confirmed(unconf);       3437                 move_to_confirmed(unconf);
4560                 conf = unconf;                   3438                 conf = unconf;
4561         }                                        3439         }
4562         get_client_locked(conf);                 3440         get_client_locked(conf);
4563         spin_unlock(&nn->client_lock);           3441         spin_unlock(&nn->client_lock);
4564         if (conf == unconf)                   << 
4565                 fsnotify_dentry(conf->cl_nfsd << 
4566         nfsd4_probe_callback(conf);              3442         nfsd4_probe_callback(conf);
4567         spin_lock(&nn->client_lock);             3443         spin_lock(&nn->client_lock);
4568         put_client_renew_locked(conf);           3444         put_client_renew_locked(conf);
4569 out:                                             3445 out:
4570         spin_unlock(&nn->client_lock);           3446         spin_unlock(&nn->client_lock);
4571         if (old)                                 3447         if (old)
4572                 expire_client(old);              3448                 expire_client(old);
4573         return status;                           3449         return status;
4574 }                                                3450 }
4575                                                  3451 
4576 static struct nfs4_file *nfsd4_alloc_file(voi    3452 static struct nfs4_file *nfsd4_alloc_file(void)
4577 {                                                3453 {
4578         return kmem_cache_alloc(file_slab, GF    3454         return kmem_cache_alloc(file_slab, GFP_KERNEL);
4579 }                                                3455 }
4580                                                  3456 
4581 /* OPEN Share state helper functions */          3457 /* OPEN Share state helper functions */
4582                                               !! 3458 static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
4583 static void nfsd4_file_init(const struct svc_ !! 3459                                 struct nfs4_file *fp)
4584 {                                                3460 {
4585         refcount_set(&fp->fi_ref, 1);         !! 3461         lockdep_assert_held(&state_lock);
                                                   >> 3462 
                                                   >> 3463         atomic_set(&fp->fi_ref, 1);
4586         spin_lock_init(&fp->fi_lock);            3464         spin_lock_init(&fp->fi_lock);
4587         INIT_LIST_HEAD(&fp->fi_stateids);        3465         INIT_LIST_HEAD(&fp->fi_stateids);
4588         INIT_LIST_HEAD(&fp->fi_delegations);     3466         INIT_LIST_HEAD(&fp->fi_delegations);
4589         INIT_LIST_HEAD(&fp->fi_clnt_odstate);    3467         INIT_LIST_HEAD(&fp->fi_clnt_odstate);
4590         fh_copy_shallow(&fp->fi_fhandle, &fh- !! 3468         fh_copy_shallow(&fp->fi_fhandle, fh);
4591         fp->fi_deleg_file = NULL;                3469         fp->fi_deleg_file = NULL;
4592         fp->fi_had_conflict = false;             3470         fp->fi_had_conflict = false;
4593         fp->fi_share_deny = 0;                   3471         fp->fi_share_deny = 0;
4594         memset(fp->fi_fds, 0, sizeof(fp->fi_f    3472         memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
4595         memset(fp->fi_access, 0, sizeof(fp->f    3473         memset(fp->fi_access, 0, sizeof(fp->fi_access));
4596         fp->fi_aliased = false;               << 
4597         fp->fi_inode = d_inode(fh->fh_dentry) << 
4598 #ifdef CONFIG_NFSD_PNFS                          3474 #ifdef CONFIG_NFSD_PNFS
4599         INIT_LIST_HEAD(&fp->fi_lo_states);       3475         INIT_LIST_HEAD(&fp->fi_lo_states);
4600         atomic_set(&fp->fi_lo_recalls, 0);       3476         atomic_set(&fp->fi_lo_recalls, 0);
4601 #endif                                           3477 #endif
                                                   >> 3478         hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
4602 }                                                3479 }
4603                                                  3480 
4604 void                                             3481 void
4605 nfsd4_free_slabs(void)                           3482 nfsd4_free_slabs(void)
4606 {                                                3483 {
4607         kmem_cache_destroy(client_slab);      !! 3484         kmem_cache_destroy(odstate_slab);
4608         kmem_cache_destroy(openowner_slab);      3485         kmem_cache_destroy(openowner_slab);
4609         kmem_cache_destroy(lockowner_slab);      3486         kmem_cache_destroy(lockowner_slab);
4610         kmem_cache_destroy(file_slab);           3487         kmem_cache_destroy(file_slab);
4611         kmem_cache_destroy(stateid_slab);        3488         kmem_cache_destroy(stateid_slab);
4612         kmem_cache_destroy(deleg_slab);          3489         kmem_cache_destroy(deleg_slab);
4613         kmem_cache_destroy(odstate_slab);     << 
4614 }                                                3490 }
4615                                                  3491 
4616 int                                              3492 int
4617 nfsd4_init_slabs(void)                           3493 nfsd4_init_slabs(void)
4618 {                                                3494 {
4619         client_slab = KMEM_CACHE(nfs4_client, !! 3495         openowner_slab = kmem_cache_create("nfsd4_openowners",
4620         if (client_slab == NULL)              !! 3496                         sizeof(struct nfs4_openowner), 0, 0, NULL);
4621                 goto out;                     << 
4622         openowner_slab = KMEM_CACHE(nfs4_open << 
4623         if (openowner_slab == NULL)              3497         if (openowner_slab == NULL)
4624                 goto out_free_client_slab;    !! 3498                 goto out;
4625         lockowner_slab = KMEM_CACHE(nfs4_lock !! 3499         lockowner_slab = kmem_cache_create("nfsd4_lockowners",
                                                   >> 3500                         sizeof(struct nfs4_lockowner), 0, 0, NULL);
4626         if (lockowner_slab == NULL)              3501         if (lockowner_slab == NULL)
4627                 goto out_free_openowner_slab;    3502                 goto out_free_openowner_slab;
4628         file_slab = KMEM_CACHE(nfs4_file, 0); !! 3503         file_slab = kmem_cache_create("nfsd4_files",
                                                   >> 3504                         sizeof(struct nfs4_file), 0, 0, NULL);
4629         if (file_slab == NULL)                   3505         if (file_slab == NULL)
4630                 goto out_free_lockowner_slab;    3506                 goto out_free_lockowner_slab;
4631         stateid_slab = KMEM_CACHE(nfs4_ol_sta !! 3507         stateid_slab = kmem_cache_create("nfsd4_stateids",
                                                   >> 3508                         sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
4632         if (stateid_slab == NULL)                3509         if (stateid_slab == NULL)
4633                 goto out_free_file_slab;         3510                 goto out_free_file_slab;
4634         deleg_slab = KMEM_CACHE(nfs4_delegati !! 3511         deleg_slab = kmem_cache_create("nfsd4_delegations",
                                                   >> 3512                         sizeof(struct nfs4_delegation), 0, 0, NULL);
4635         if (deleg_slab == NULL)                  3513         if (deleg_slab == NULL)
4636                 goto out_free_stateid_slab;      3514                 goto out_free_stateid_slab;
4637         odstate_slab = KMEM_CACHE(nfs4_clnt_o !! 3515         odstate_slab = kmem_cache_create("nfsd4_odstate",
                                                   >> 3516                         sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
4638         if (odstate_slab == NULL)                3517         if (odstate_slab == NULL)
4639                 goto out_free_deleg_slab;        3518                 goto out_free_deleg_slab;
4640         return 0;                                3519         return 0;
4641                                                  3520 
4642 out_free_deleg_slab:                             3521 out_free_deleg_slab:
4643         kmem_cache_destroy(deleg_slab);          3522         kmem_cache_destroy(deleg_slab);
4644 out_free_stateid_slab:                           3523 out_free_stateid_slab:
4645         kmem_cache_destroy(stateid_slab);        3524         kmem_cache_destroy(stateid_slab);
4646 out_free_file_slab:                              3525 out_free_file_slab:
4647         kmem_cache_destroy(file_slab);           3526         kmem_cache_destroy(file_slab);
4648 out_free_lockowner_slab:                         3527 out_free_lockowner_slab:
4649         kmem_cache_destroy(lockowner_slab);      3528         kmem_cache_destroy(lockowner_slab);
4650 out_free_openowner_slab:                         3529 out_free_openowner_slab:
4651         kmem_cache_destroy(openowner_slab);      3530         kmem_cache_destroy(openowner_slab);
4652 out_free_client_slab:                         << 
4653         kmem_cache_destroy(client_slab);      << 
4654 out:                                             3531 out:
                                                   >> 3532         dprintk("nfsd4: out of memory while initializing nfsv4\n");
4655         return -ENOMEM;                          3533         return -ENOMEM;
4656 }                                                3534 }
4657                                                  3535 
4658 static unsigned long                          << 
4659 nfsd4_state_shrinker_count(struct shrinker *s << 
4660 {                                             << 
4661         int count;                            << 
4662         struct nfsd_net *nn = shrink->private << 
4663                                               << 
4664         count = atomic_read(&nn->nfsd_courtes << 
4665         if (!count)                           << 
4666                 count = atomic_long_read(&num << 
4667         if (count)                            << 
4668                 queue_work(laundry_wq, &nn->n << 
4669         return (unsigned long)count;          << 
4670 }                                             << 
4671                                               << 
4672 static unsigned long                          << 
4673 nfsd4_state_shrinker_scan(struct shrinker *sh << 
4674 {                                             << 
4675         return SHRINK_STOP;                   << 
4676 }                                             << 
4677                                               << 
4678 void                                          << 
4679 nfsd4_init_leases_net(struct nfsd_net *nn)    << 
4680 {                                             << 
4681         struct sysinfo si;                    << 
4682         u64 max_clients;                      << 
4683                                               << 
4684         nn->nfsd4_lease = 90;   /* default le << 
4685         nn->nfsd4_grace = 90;                 << 
4686         nn->somebody_reclaimed = false;       << 
4687         nn->track_reclaim_completes = false;  << 
4688         nn->clverifier_counter = get_random_u << 
4689         nn->clientid_base = get_random_u32(); << 
4690         nn->clientid_counter = nn->clientid_b << 
4691         nn->s2s_cp_cl_id = nn->clientid_count << 
4692                                               << 
4693         atomic_set(&nn->nfs4_client_count, 0) << 
4694         si_meminfo(&si);                      << 
4695         max_clients = (u64)si.totalram * si.m << 
4696         max_clients *= NFS4_CLIENTS_PER_GB;   << 
4697         nn->nfs4_max_clients = max_t(int, max << 
4698                                               << 
4699         atomic_set(&nn->nfsd_courtesy_clients << 
4700 }                                             << 
4701                                               << 
4702 enum rp_lock {                                << 
4703         RP_UNLOCKED,                          << 
4704         RP_LOCKED,                            << 
4705         RP_UNHASHED,                          << 
4706 };                                            << 
4707                                               << 
4708 static void init_nfs4_replay(struct nfs4_repl    3536 static void init_nfs4_replay(struct nfs4_replay *rp)
4709 {                                                3537 {
4710         rp->rp_status = nfserr_serverfault;      3538         rp->rp_status = nfserr_serverfault;
4711         rp->rp_buflen = 0;                       3539         rp->rp_buflen = 0;
4712         rp->rp_buf = rp->rp_ibuf;                3540         rp->rp_buf = rp->rp_ibuf;
4713         atomic_set(&rp->rp_locked, RP_UNLOCKE !! 3541         mutex_init(&rp->rp_mutex);
4714 }                                                3542 }
4715                                                  3543 
4716 static int nfsd4_cstate_assign_replay(struct  !! 3544 static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
4717                                       struct  !! 3545                 struct nfs4_stateowner *so)
4718 {                                                3546 {
4719         if (!nfsd4_has_session(cstate)) {        3547         if (!nfsd4_has_session(cstate)) {
4720                 wait_var_event(&so->so_replay !! 3548                 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    3549                 cstate->replay_owner = nfs4_get_stateowner(so);
4726         }                                        3550         }
4727         return 0;                             << 
4728 }                                                3551 }
4729                                                  3552 
4730 void nfsd4_cstate_clear_replay(struct nfsd4_c    3553 void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
4731 {                                                3554 {
4732         struct nfs4_stateowner *so = cstate->    3555         struct nfs4_stateowner *so = cstate->replay_owner;
4733                                                  3556 
4734         if (so != NULL) {                        3557         if (so != NULL) {
4735                 cstate->replay_owner = NULL;     3558                 cstate->replay_owner = NULL;
4736                 atomic_set(&so->so_replay.rp_ !! 3559                 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);         3560                 nfs4_put_stateowner(so);
4740         }                                        3561         }
4741 }                                                3562 }
4742                                                  3563 
4743 static inline void *alloc_stateowner(struct k    3564 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
4744 {                                                3565 {
4745         struct nfs4_stateowner *sop;             3566         struct nfs4_stateowner *sop;
4746                                                  3567 
4747         sop = kmem_cache_alloc(slab, GFP_KERN    3568         sop = kmem_cache_alloc(slab, GFP_KERNEL);
4748         if (!sop)                                3569         if (!sop)
4749                 return NULL;                     3570                 return NULL;
4750                                                  3571 
4751         xdr_netobj_dup(&sop->so_owner, owner, !! 3572         sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
4752         if (!sop->so_owner.data) {               3573         if (!sop->so_owner.data) {
4753                 kmem_cache_free(slab, sop);      3574                 kmem_cache_free(slab, sop);
4754                 return NULL;                     3575                 return NULL;
4755         }                                        3576         }
                                                   >> 3577         sop->so_owner.len = owner->len;
4756                                                  3578 
4757         INIT_LIST_HEAD(&sop->so_stateids);       3579         INIT_LIST_HEAD(&sop->so_stateids);
4758         sop->so_client = clp;                    3580         sop->so_client = clp;
4759         init_nfs4_replay(&sop->so_replay);       3581         init_nfs4_replay(&sop->so_replay);
4760         atomic_set(&sop->so_count, 1);           3582         atomic_set(&sop->so_count, 1);
4761         return sop;                              3583         return sop;
4762 }                                                3584 }
4763                                                  3585 
4764 static void hash_openowner(struct nfs4_openow    3586 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
4765 {                                                3587 {
4766         lockdep_assert_held(&clp->cl_lock);      3588         lockdep_assert_held(&clp->cl_lock);
4767                                                  3589 
4768         list_add(&oo->oo_owner.so_strhash,       3590         list_add(&oo->oo_owner.so_strhash,
4769                  &clp->cl_ownerstr_hashtbl[st    3591                  &clp->cl_ownerstr_hashtbl[strhashval]);
4770         list_add(&oo->oo_perclient, &clp->cl_    3592         list_add(&oo->oo_perclient, &clp->cl_openowners);
4771 }                                                3593 }
4772                                                  3594 
4773 static void nfs4_unhash_openowner(struct nfs4    3595 static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
4774 {                                                3596 {
4775         unhash_openowner_locked(openowner(so)    3597         unhash_openowner_locked(openowner(so));
4776 }                                                3598 }
4777                                                  3599 
4778 static void nfs4_free_openowner(struct nfs4_s    3600 static void nfs4_free_openowner(struct nfs4_stateowner *so)
4779 {                                                3601 {
4780         struct nfs4_openowner *oo = openowner    3602         struct nfs4_openowner *oo = openowner(so);
4781                                                  3603 
4782         kmem_cache_free(openowner_slab, oo);     3604         kmem_cache_free(openowner_slab, oo);
4783 }                                                3605 }
4784                                                  3606 
4785 static const struct nfs4_stateowner_operation    3607 static const struct nfs4_stateowner_operations openowner_ops = {
4786         .so_unhash =    nfs4_unhash_openowner    3608         .so_unhash =    nfs4_unhash_openowner,
4787         .so_free =      nfs4_free_openowner,     3609         .so_free =      nfs4_free_openowner,
4788 };                                               3610 };
4789                                                  3611 
4790 static struct nfs4_ol_stateid *                  3612 static struct nfs4_ol_stateid *
4791 nfsd4_find_existing_open(struct nfs4_file *fp    3613 nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4792 {                                                3614 {
4793         struct nfs4_ol_stateid *local, *ret =    3615         struct nfs4_ol_stateid *local, *ret = NULL;
4794         struct nfs4_openowner *oo = open->op_    3616         struct nfs4_openowner *oo = open->op_openowner;
4795                                                  3617 
4796         lockdep_assert_held(&fp->fi_lock);       3618         lockdep_assert_held(&fp->fi_lock);
4797                                                  3619 
4798         list_for_each_entry(local, &fp->fi_st    3620         list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
4799                 /* ignore lock owners */         3621                 /* ignore lock owners */
4800                 if (local->st_stateowner->so_    3622                 if (local->st_stateowner->so_is_open_owner == 0)
4801                         continue;                3623                         continue;
4802                 if (local->st_stateowner != &    3624                 if (local->st_stateowner != &oo->oo_owner)
4803                         continue;                3625                         continue;
4804                 if (local->st_stid.sc_type == !! 3626                 if (local->st_stid.sc_type == NFS4_OPEN_STID) {
4805                     !local->st_stid.sc_status << 
4806                         ret = local;             3627                         ret = local;
4807                         refcount_inc(&ret->st !! 3628                         atomic_inc(&ret->st_stid.sc_count);
4808                         break;                   3629                         break;
4809                 }                                3630                 }
4810         }                                        3631         }
4811         return ret;                              3632         return ret;
4812 }                                                3633 }
4813                                                  3634 
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                                    3635 static __be32
4873 nfsd4_verify_open_stid(struct nfs4_stid *s)      3636 nfsd4_verify_open_stid(struct nfs4_stid *s)
4874 {                                                3637 {
4875         __be32 ret = nfs_ok;                     3638         __be32 ret = nfs_ok;
4876                                                  3639 
4877         if (s->sc_status & SC_STATUS_ADMIN_RE !! 3640         switch (s->sc_type) {
4878                 ret = nfserr_admin_revoked;   !! 3641         default:
4879         else if (s->sc_status & SC_STATUS_REV !! 3642                 break;
4880                 ret = nfserr_deleg_revoked;   !! 3643         case NFS4_CLOSED_STID:
4881         else if (s->sc_status & SC_STATUS_CLO !! 3644         case NFS4_CLOSED_DELEG_STID:
4882                 ret = nfserr_bad_stateid;        3645                 ret = nfserr_bad_stateid;
                                                   >> 3646                 break;
                                                   >> 3647         case NFS4_REVOKED_DELEG_STID:
                                                   >> 3648                 ret = nfserr_deleg_revoked;
                                                   >> 3649         }
4883         return ret;                              3650         return ret;
4884 }                                                3651 }
4885                                                  3652 
4886 /* Lock the stateid st_mutex, and deal with r    3653 /* Lock the stateid st_mutex, and deal with races with CLOSE */
4887 static __be32                                    3654 static __be32
4888 nfsd4_lock_ol_stateid(struct nfs4_ol_stateid     3655 nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp)
4889 {                                                3656 {
4890         __be32 ret;                              3657         __be32 ret;
4891                                                  3658 
4892         mutex_lock_nested(&stp->st_mutex, LOC !! 3659         mutex_lock(&stp->st_mutex);
4893         ret = nfsd4_verify_open_stid(&stp->st    3660         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)                       3661         if (ret != nfs_ok)
4899                 mutex_unlock(&stp->st_mutex);    3662                 mutex_unlock(&stp->st_mutex);
4900         return ret;                              3663         return ret;
4901 }                                                3664 }
4902                                                  3665 
4903 static struct nfs4_ol_stateid *                  3666 static struct nfs4_ol_stateid *
4904 nfsd4_find_and_lock_existing_open(struct nfs4    3667 nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4905 {                                                3668 {
4906         struct nfs4_ol_stateid *stp;             3669         struct nfs4_ol_stateid *stp;
4907         for (;;) {                               3670         for (;;) {
4908                 spin_lock(&fp->fi_lock);         3671                 spin_lock(&fp->fi_lock);
4909                 stp = nfsd4_find_existing_ope    3672                 stp = nfsd4_find_existing_open(fp, open);
4910                 spin_unlock(&fp->fi_lock);       3673                 spin_unlock(&fp->fi_lock);
4911                 if (!stp || nfsd4_lock_ol_sta    3674                 if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok)
4912                         break;                   3675                         break;
4913                 nfs4_put_stid(&stp->st_stid);    3676                 nfs4_put_stid(&stp->st_stid);
4914         }                                        3677         }
4915         return stp;                              3678         return stp;
4916 }                                                3679 }
4917                                                  3680 
4918 static struct nfs4_openowner *                   3681 static struct nfs4_openowner *
4919 find_or_alloc_open_stateowner(unsigned int st !! 3682 alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
4920                               struct nfsd4_co !! 3683                            struct nfsd4_compound_state *cstate)
4921 {                                                3684 {
4922         struct nfs4_client *clp = cstate->clp    3685         struct nfs4_client *clp = cstate->clp;
4923         struct nfs4_openowner *oo, *new = NUL !! 3686         struct nfs4_openowner *oo, *ret;
4924                                                  3687 
4925 retry:                                        !! 3688         oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
4926         spin_lock(&clp->cl_lock);             !! 3689         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;                     3690                 return NULL;
4949         new->oo_owner.so_ops = &openowner_ops !! 3691         oo->oo_owner.so_ops = &openowner_ops;
4950         new->oo_owner.so_is_open_owner = 1;   !! 3692         oo->oo_owner.so_is_open_owner = 1;
4951         new->oo_owner.so_seqid = open->op_seq !! 3693         oo->oo_owner.so_seqid = open->op_seqid;
4952         new->oo_flags = 0;                    !! 3694         oo->oo_flags = 0;
4953         if (nfsd4_has_session(cstate))           3695         if (nfsd4_has_session(cstate))
4954                 new->oo_flags |= NFS4_OO_CONF !! 3696                 oo->oo_flags |= NFS4_OO_CONFIRMED;
4955         new->oo_time = 0;                     !! 3697         oo->oo_time = 0;
4956         new->oo_last_closed_stid = NULL;      !! 3698         oo->oo_last_closed_stid = NULL;
4957         INIT_LIST_HEAD(&new->oo_close_lru);   !! 3699         INIT_LIST_HEAD(&oo->oo_close_lru);
4958         goto retry;                           !! 3700         spin_lock(&clp->cl_lock);
                                                   >> 3701         ret = find_openstateowner_str_locked(strhashval, open, clp);
                                                   >> 3702         if (ret == NULL) {
                                                   >> 3703                 hash_openowner(oo, clp, strhashval);
                                                   >> 3704                 ret = oo;
                                                   >> 3705         } else
                                                   >> 3706                 nfs4_free_stateowner(&oo->oo_owner);
                                                   >> 3707 
                                                   >> 3708         spin_unlock(&clp->cl_lock);
                                                   >> 3709         return ret;
4959 }                                                3710 }
4960                                                  3711 
4961 static struct nfs4_ol_stateid *                  3712 static struct nfs4_ol_stateid *
4962 init_open_stateid(struct nfs4_file *fp, struc    3713 init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
4963 {                                                3714 {
4964                                                  3715 
4965         struct nfs4_openowner *oo = open->op_    3716         struct nfs4_openowner *oo = open->op_openowner;
4966         struct nfs4_ol_stateid *retstp = NULL    3717         struct nfs4_ol_stateid *retstp = NULL;
4967         struct nfs4_ol_stateid *stp;             3718         struct nfs4_ol_stateid *stp;
4968                                                  3719 
4969         stp = open->op_stp;                      3720         stp = open->op_stp;
4970         /* We are moving these outside of the    3721         /* We are moving these outside of the spinlocks to avoid the warnings */
4971         mutex_init(&stp->st_mutex);              3722         mutex_init(&stp->st_mutex);
4972         mutex_lock_nested(&stp->st_mutex, OPE !! 3723         mutex_lock(&stp->st_mutex);
4973                                                  3724 
4974 retry:                                           3725 retry:
4975         spin_lock(&oo->oo_owner.so_client->cl    3726         spin_lock(&oo->oo_owner.so_client->cl_lock);
4976         spin_lock(&fp->fi_lock);                 3727         spin_lock(&fp->fi_lock);
4977                                                  3728 
4978         retstp = nfsd4_find_existing_open(fp,    3729         retstp = nfsd4_find_existing_open(fp, open);
4979         if (retstp)                              3730         if (retstp)
4980                 goto out_unlock;                 3731                 goto out_unlock;
4981                                                  3732 
4982         open->op_stp = NULL;                     3733         open->op_stp = NULL;
4983         refcount_inc(&stp->st_stid.sc_count); !! 3734         atomic_inc(&stp->st_stid.sc_count);
4984         stp->st_stid.sc_type = SC_TYPE_OPEN;  !! 3735         stp->st_stid.sc_type = NFS4_OPEN_STID;
4985         INIT_LIST_HEAD(&stp->st_locks);          3736         INIT_LIST_HEAD(&stp->st_locks);
4986         stp->st_stateowner = nfs4_get_stateow    3737         stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
4987         get_nfs4_file(fp);                       3738         get_nfs4_file(fp);
4988         stp->st_stid.sc_file = fp;               3739         stp->st_stid.sc_file = fp;
4989         stp->st_access_bmap = 0;                 3740         stp->st_access_bmap = 0;
4990         stp->st_deny_bmap = 0;                   3741         stp->st_deny_bmap = 0;
4991         stp->st_openstp = NULL;                  3742         stp->st_openstp = NULL;
4992         list_add(&stp->st_perstateowner, &oo-    3743         list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
4993         list_add(&stp->st_perfile, &fp->fi_st    3744         list_add(&stp->st_perfile, &fp->fi_stateids);
4994                                                  3745 
4995 out_unlock:                                      3746 out_unlock:
4996         spin_unlock(&fp->fi_lock);               3747         spin_unlock(&fp->fi_lock);
4997         spin_unlock(&oo->oo_owner.so_client->    3748         spin_unlock(&oo->oo_owner.so_client->cl_lock);
4998         if (retstp) {                            3749         if (retstp) {
4999                 /* Handle races with CLOSE */    3750                 /* Handle races with CLOSE */
5000                 if (nfsd4_lock_ol_stateid(ret    3751                 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
5001                         nfs4_put_stid(&retstp    3752                         nfs4_put_stid(&retstp->st_stid);
5002                         goto retry;              3753                         goto retry;
5003                 }                                3754                 }
5004                 /* To keep mutex tracking hap    3755                 /* To keep mutex tracking happy */
5005                 mutex_unlock(&stp->st_mutex);    3756                 mutex_unlock(&stp->st_mutex);
5006                 stp = retstp;                    3757                 stp = retstp;
5007         }                                        3758         }
5008         return stp;                              3759         return stp;
5009 }                                                3760 }
5010                                                  3761 
5011 /*                                               3762 /*
5012  * In the 4.0 case we need to keep the owners    3763  * 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     3764  * CLOSE replay. We still do need to release any file access that is held by
5014  * them before returning however.                3765  * them before returning however.
5015  */                                              3766  */
5016 static void                                      3767 static void
5017 move_to_close_lru(struct nfs4_ol_stateid *s,     3768 move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
5018 {                                                3769 {
5019         struct nfs4_ol_stateid *last;            3770         struct nfs4_ol_stateid *last;
5020         struct nfs4_openowner *oo = openowner    3771         struct nfs4_openowner *oo = openowner(s->st_stateowner);
5021         struct nfsd_net *nn = net_generic(s->    3772         struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
5022                                                  3773                                                 nfsd_net_id);
5023                                                  3774 
5024         dprintk("NFSD: move_to_close_lru nfs4    3775         dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
5025                                                  3776 
5026         /*                                       3777         /*
5027          * We know that we hold one reference    3778          * We know that we hold one reference via nfsd4_close, and another
5028          * "persistent" reference for the cli    3779          * "persistent" reference for the client. If the refcount is higher
5029          * than 2, then there are still calls    3780          * than 2, then there are still calls in progress that are using this
5030          * stateid. We can't put the sc_file     3781          * stateid. We can't put the sc_file reference until they are finished.
5031          * Wait for the refcount to drop to 2    3782          * Wait for the refcount to drop to 2. Since it has been unhashed,
5032          * there should be no danger of the r    3783          * there should be no danger of the refcount going back up again at
5033          * this point.                           3784          * this point.
5034          * Some threads with a reference migh << 
5035          * so tell them to stop waiting.      << 
5036          */                                      3785          */
5037         atomic_set(&oo->oo_owner.so_replay.rp !! 3786         wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
5038         smp_mb__after_atomic();               << 
5039         wake_up_var(&oo->oo_owner.so_replay.r << 
5040         wait_event(close_wq, refcount_read(&s << 
5041                                                  3787 
5042         release_all_access(s);                   3788         release_all_access(s);
5043         if (s->st_stid.sc_file) {                3789         if (s->st_stid.sc_file) {
5044                 put_nfs4_file(s->st_stid.sc_f    3790                 put_nfs4_file(s->st_stid.sc_file);
5045                 s->st_stid.sc_file = NULL;       3791                 s->st_stid.sc_file = NULL;
5046         }                                        3792         }
5047                                                  3793 
5048         spin_lock(&nn->client_lock);             3794         spin_lock(&nn->client_lock);
5049         last = oo->oo_last_closed_stid;          3795         last = oo->oo_last_closed_stid;
5050         oo->oo_last_closed_stid = s;             3796         oo->oo_last_closed_stid = s;
5051         list_move_tail(&oo->oo_close_lru, &nn    3797         list_move_tail(&oo->oo_close_lru, &nn->close_lru);
5052         oo->oo_time = ktime_get_boottime_seco !! 3798         oo->oo_time = get_seconds();
5053         spin_unlock(&nn->client_lock);           3799         spin_unlock(&nn->client_lock);
5054         if (last)                                3800         if (last)
5055                 nfs4_put_stid(&last->st_stid)    3801                 nfs4_put_stid(&last->st_stid);
5056 }                                                3802 }
5057                                                  3803 
5058 static noinline_for_stack struct nfs4_file *  !! 3804 /* search file_hashtbl[] for file */
5059 nfsd4_file_hash_lookup(const struct svc_fh *f !! 3805 static struct nfs4_file *
                                                   >> 3806 find_file_locked(struct knfsd_fh *fh, unsigned int hashval)
5060 {                                                3807 {
5061         struct inode *inode = d_inode(fhp->fh !! 3808         struct nfs4_file *fp;
5062         struct rhlist_head *tmp, *list;       << 
5063         struct nfs4_file *fi;                 << 
5064                                                  3809 
5065         rcu_read_lock();                      !! 3810         hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash) {
5066         list = rhltable_lookup(&nfs4_file_rhl !! 3811                 if (fh_match(&fp->fi_fhandle, fh)) {
5067                                nfs4_file_rhas !! 3812                         if (atomic_inc_not_zero(&fp->fi_ref))
5068         rhl_for_each_entry_rcu(fi, tmp, list, !! 3813                                 return fp;
5069                 if (fh_match(&fi->fi_fhandle, << 
5070                         if (refcount_inc_not_ << 
5071                                 rcu_read_unlo << 
5072                                 return fi;    << 
5073                         }                     << 
5074                 }                                3814                 }
5075         }                                        3815         }
5076         rcu_read_unlock();                    << 
5077         return NULL;                             3816         return NULL;
5078 }                                                3817 }
5079                                                  3818 
5080 /*                                            !! 3819 struct nfs4_file *
5081  * On hash insertion, identify entries with t !! 3820 find_file(struct knfsd_fh *fh)
5082  * distinct filehandles. They will all be on  << 
5083  * by rhltable_lookup().                      << 
5084  *                                            << 
5085  * inode->i_lock prevents racing insertions f << 
5086  * for the same inode/fhp pair twice.         << 
5087  */                                           << 
5088 static noinline_for_stack struct nfs4_file *  << 
5089 nfsd4_file_hash_insert(struct nfs4_file *new, << 
5090 {                                                3821 {
5091         struct inode *inode = d_inode(fhp->fh !! 3822         struct nfs4_file *fp;
5092         struct rhlist_head *tmp, *list;       !! 3823         unsigned int hashval = file_hashval(fh);
5093         struct nfs4_file *ret = NULL;         << 
5094         bool alias_found = false;             << 
5095         struct nfs4_file *fi;                 << 
5096         int err;                              << 
5097                                                  3824 
5098         rcu_read_lock();                         3825         rcu_read_lock();
5099         spin_lock(&inode->i_lock);            !! 3826         fp = find_file_locked(fh, hashval);
5100                                               << 
5101         list = rhltable_lookup(&nfs4_file_rhl << 
5102                                nfs4_file_rhas << 
5103         rhl_for_each_entry_rcu(fi, tmp, list, << 
5104                 if (fh_match(&fi->fi_fhandle, << 
5105                         if (refcount_inc_not_ << 
5106                                 ret = fi;     << 
5107                 } else                        << 
5108                         fi->fi_aliased = alia << 
5109         }                                     << 
5110         if (ret)                              << 
5111                 goto out_unlock;              << 
5112                                               << 
5113         nfsd4_file_init(fhp, new);            << 
5114         err = rhltable_insert(&nfs4_file_rhlt << 
5115                               nfs4_file_rhash << 
5116         if (err)                              << 
5117                 goto out_unlock;              << 
5118                                               << 
5119         new->fi_aliased = alias_found;        << 
5120         ret = new;                            << 
5121                                               << 
5122 out_unlock:                                   << 
5123         spin_unlock(&inode->i_lock);          << 
5124         rcu_read_unlock();                       3827         rcu_read_unlock();
5125         return ret;                           !! 3828         return fp;
5126 }                                                3829 }
5127                                                  3830 
5128 static noinline_for_stack void nfsd4_file_has !! 3831 static struct nfs4_file *
                                                   >> 3832 find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
5129 {                                                3833 {
5130         rhltable_remove(&nfs4_file_rhltable,  !! 3834         struct nfs4_file *fp;
5131                         nfs4_file_rhash_param !! 3835         unsigned int hashval = file_hashval(fh);
                                                   >> 3836 
                                                   >> 3837         rcu_read_lock();
                                                   >> 3838         fp = find_file_locked(fh, hashval);
                                                   >> 3839         rcu_read_unlock();
                                                   >> 3840         if (fp)
                                                   >> 3841                 return fp;
                                                   >> 3842 
                                                   >> 3843         spin_lock(&state_lock);
                                                   >> 3844         fp = find_file_locked(fh, hashval);
                                                   >> 3845         if (likely(fp == NULL)) {
                                                   >> 3846                 nfsd4_init_file(fh, hashval, new);
                                                   >> 3847                 fp = new;
                                                   >> 3848         }
                                                   >> 3849         spin_unlock(&state_lock);
                                                   >> 3850 
                                                   >> 3851         return fp;
5132 }                                                3852 }
5133                                                  3853 
5134 /*                                               3854 /*
5135  * Called to check deny when READ with all ze    3855  * Called to check deny when READ with all zero stateid or
5136  * WRITE with all zero or all one stateid        3856  * WRITE with all zero or all one stateid
5137  */                                              3857  */
5138 static __be32                                    3858 static __be32
5139 nfs4_share_conflict(struct svc_fh *current_fh    3859 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
5140 {                                                3860 {
5141         struct nfs4_file *fp;                    3861         struct nfs4_file *fp;
5142         __be32 ret = nfs_ok;                     3862         __be32 ret = nfs_ok;
5143                                                  3863 
5144         fp = nfsd4_file_hash_lookup(current_f !! 3864         fp = find_file(&current_fh->fh_handle);
5145         if (!fp)                                 3865         if (!fp)
5146                 return ret;                      3866                 return ret;
5147                                               << 
5148         /* Check for conflicting share reserv    3867         /* Check for conflicting share reservations */
5149         spin_lock(&fp->fi_lock);                 3868         spin_lock(&fp->fi_lock);
5150         if (fp->fi_share_deny & deny_type)       3869         if (fp->fi_share_deny & deny_type)
5151                 ret = nfserr_locked;             3870                 ret = nfserr_locked;
5152         spin_unlock(&fp->fi_lock);               3871         spin_unlock(&fp->fi_lock);
5153         put_nfs4_file(fp);                       3872         put_nfs4_file(fp);
5154         return ret;                              3873         return ret;
5155 }                                                3874 }
5156                                                  3875 
5157 static bool nfsd4_deleg_present(const struct  << 
5158 {                                             << 
5159         struct file_lock_context *ctx = locks << 
5160                                               << 
5161         return ctx && !list_empty_careful(&ct << 
5162 }                                             << 
5163                                               << 
5164 /**                                           << 
5165  * nfsd_wait_for_delegreturn - wait for deleg << 
5166  * @rqstp: the RPC transaction being executed << 
5167  * @inode: in-core inode of the file being wa << 
5168  *                                            << 
5169  * The timeout prevents deadlock if all nfsd  << 
5170  * tied up waiting for returning delegations. << 
5171  *                                            << 
5172  * Return values:                             << 
5173  *   %true: delegation was returned           << 
5174  *   %false: timed out waiting for delegretur << 
5175  */                                           << 
5176 bool nfsd_wait_for_delegreturn(struct svc_rqs << 
5177 {                                             << 
5178         long __maybe_unused timeo;            << 
5179                                               << 
5180         timeo = wait_var_event_timeout(inode, << 
5181                                        NFSD_D << 
5182         trace_nfsd_delegret_wakeup(rqstp, ino << 
5183         return timeo > 0;                     << 
5184 }                                             << 
5185                                               << 
5186 static void nfsd4_cb_recall_prepare(struct nf    3876 static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
5187 {                                                3877 {
5188         struct nfs4_delegation *dp = cb_to_de    3878         struct nfs4_delegation *dp = cb_to_delegation(cb);
5189         struct nfsd_net *nn = net_generic(dp-    3879         struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
5190                                           nfs    3880                                           nfsd_net_id);
5191                                                  3881 
5192         block_delegations(&dp->dl_stid.sc_fil    3882         block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
5193                                                  3883 
5194         /*                                       3884         /*
5195          * We can't do this in nfsd_break_del    3885          * We can't do this in nfsd_break_deleg_cb because it is
5196          * already holding inode->i_lock.        3886          * already holding inode->i_lock.
5197          *                                       3887          *
5198          * If the dl_time != 0, then we know     3888          * If the dl_time != 0, then we know that it has already been
5199          * queued for a lease break. Don't qu    3889          * queued for a lease break. Don't queue it again.
5200          */                                      3890          */
5201         spin_lock(&state_lock);                  3891         spin_lock(&state_lock);
5202         if (delegation_hashed(dp) && dp->dl_t    3892         if (delegation_hashed(dp) && dp->dl_time == 0) {
5203                 dp->dl_time = ktime_get_boott !! 3893                 dp->dl_time = get_seconds();
5204                 list_add_tail(&dp->dl_recall_    3894                 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
5205         }                                        3895         }
5206         spin_unlock(&state_lock);                3896         spin_unlock(&state_lock);
5207 }                                                3897 }
5208                                                  3898 
5209 static int nfsd4_cb_recall_done(struct nfsd4_    3899 static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
5210                 struct rpc_task *task)           3900                 struct rpc_task *task)
5211 {                                                3901 {
5212         struct nfs4_delegation *dp = cb_to_de    3902         struct nfs4_delegation *dp = cb_to_delegation(cb);
5213                                                  3903 
5214         trace_nfsd_cb_recall_done(&dp->dl_sti !! 3904         if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID)
5215                                               !! 3905                 return 1;
5216         if (dp->dl_stid.sc_status)            << 
5217                 /* CLOSED or REVOKED */       << 
5218                 return 1;                     << 
5219                                                  3906 
5220         switch (task->tk_status) {               3907         switch (task->tk_status) {
5221         case 0:                                  3908         case 0:
5222                 return 1;                        3909                 return 1;
5223         case -NFS4ERR_DELAY:                  << 
5224                 rpc_delay(task, 2 * HZ);      << 
5225                 return 0;                     << 
5226         case -EBADHANDLE:                        3910         case -EBADHANDLE:
5227         case -NFS4ERR_BAD_STATEID:               3911         case -NFS4ERR_BAD_STATEID:
5228                 /*                               3912                 /*
5229                  * Race: client probably got     3913                  * Race: client probably got cb_recall before open reply
5230                  * granting delegation.          3914                  * granting delegation.
5231                  */                              3915                  */
5232                 if (dp->dl_retries--) {          3916                 if (dp->dl_retries--) {
5233                         rpc_delay(task, 2 * H    3917                         rpc_delay(task, 2 * HZ);
5234                         return 0;                3918                         return 0;
5235                 }                                3919                 }
5236                 fallthrough;                  !! 3920                 /*FALLTHRU*/
5237         default:                                 3921         default:
5238                 return 1;                     !! 3922                 return -1;
5239         }                                        3923         }
5240 }                                                3924 }
5241                                                  3925 
5242 static void nfsd4_cb_recall_release(struct nf    3926 static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
5243 {                                                3927 {
5244         struct nfs4_delegation *dp = cb_to_de    3928         struct nfs4_delegation *dp = cb_to_delegation(cb);
5245                                                  3929 
5246         nfs4_put_stid(&dp->dl_stid);             3930         nfs4_put_stid(&dp->dl_stid);
5247 }                                                3931 }
5248                                                  3932 
5249 static const struct nfsd4_callback_ops nfsd4_    3933 static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
5250         .prepare        = nfsd4_cb_recall_pre    3934         .prepare        = nfsd4_cb_recall_prepare,
5251         .done           = nfsd4_cb_recall_don    3935         .done           = nfsd4_cb_recall_done,
5252         .release        = nfsd4_cb_recall_rel    3936         .release        = nfsd4_cb_recall_release,
5253         .opcode         = OP_CB_RECALL,       << 
5254 };                                               3937 };
5255                                                  3938 
5256 static void nfsd_break_one_deleg(struct nfs4_    3939 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
5257 {                                                3940 {
5258         /*                                       3941         /*
5259          * We're assuming the state code neve    3942          * We're assuming the state code never drops its reference
5260          * without first removing the lease.     3943          * without first removing the lease.  Since we're in this lease
5261          * callback (and since the lease code !! 3944          * callback (and since the lease code is serialized by the kernel
5262          * flc_lock) we know the server hasn' !! 3945          * lock) we know the server hasn't removed the lease yet, we know
5263          * we know it's safe to take a refere !! 3946          * it's safe to take a reference.
5264          */                                      3947          */
5265         refcount_inc(&dp->dl_stid.sc_count);  !! 3948         atomic_inc(&dp->dl_stid.sc_count);
5266         WARN_ON_ONCE(!nfsd4_run_cb(&dp->dl_re !! 3949         nfsd4_run_cb(&dp->dl_recall);
5267 }                                                3950 }
5268                                                  3951 
5269 /* Called from break_lease() with flc_lock he !! 3952 /* Called from break_lease() with i_lock held. */
5270 static bool                                      3953 static bool
5271 nfsd_break_deleg_cb(struct file_lease *fl)    !! 3954 nfsd_break_deleg_cb(struct file_lock *fl)
5272 {                                                3955 {
5273         struct nfs4_delegation *dp = (struct  !! 3956         bool ret = false;
5274         struct nfs4_file *fp = dp->dl_stid.sc !! 3957         struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
5275         struct nfs4_client *clp = dp->dl_stid !! 3958         struct nfs4_delegation *dp;
5276         struct nfsd_net *nn;                  << 
5277                                               << 
5278         trace_nfsd_cb_recall(&dp->dl_stid);   << 
5279                                                  3959 
5280         dp->dl_recalled = true;               !! 3960         if (!fp) {
5281         atomic_inc(&clp->cl_delegs_in_recall) !! 3961                 WARN(1, "(%p)->fl_owner NULL\n", fl);
5282         if (try_to_expire_client(clp)) {      !! 3962                 return ret;
5283                 nn = net_generic(clp->net, nf !! 3963         }
5284                 mod_delayed_work(laundry_wq,  !! 3964         if (fp->fi_had_conflict) {
                                                   >> 3965                 WARN(1, "duplicate break on %p\n", fp);
                                                   >> 3966                 return ret;
5285         }                                        3967         }
5286                                               << 
5287         /*                                       3968         /*
5288          * We don't want the locks code to ti    3969          * We don't want the locks code to timeout the lease for us;
5289          * we'll remove it ourself if a deleg    3970          * we'll remove it ourself if a delegation isn't returned
5290          * in time:                              3971          * in time:
5291          */                                      3972          */
5292         fl->fl_break_time = 0;                   3973         fl->fl_break_time = 0;
5293                                                  3974 
                                                   >> 3975         spin_lock(&fp->fi_lock);
5294         fp->fi_had_conflict = true;              3976         fp->fi_had_conflict = true;
5295         nfsd_break_one_deleg(dp);             !! 3977         /*
5296         return false;                         !! 3978          * If there are no delegations on the list, then return true
5297 }                                             !! 3979          * so that the lease code will go ahead and delete it.
5298                                               !! 3980          */
5299 /**                                           !! 3981         if (list_empty(&fp->fi_delegations))
5300  * nfsd_breaker_owns_lease - Check if lease c !! 3982                 ret = true;
5301  * @fl: Lock state to check                   !! 3983         else
5302  *                                            !! 3984                 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
5303  * Return values:                             !! 3985                         nfsd_break_one_deleg(dp);
5304  *   %true: Lease conflict was resolved       !! 3986         spin_unlock(&fp->fi_lock);
5305  *   %false: Lease conflict was not resolved. !! 3987         return ret;
5306  */                                           << 
5307 static bool nfsd_breaker_owns_lease(struct fi << 
5308 {                                             << 
5309         struct nfs4_delegation *dl = fl->c.fl << 
5310         struct svc_rqst *rqst;                << 
5311         struct nfs4_client *clp;              << 
5312                                               << 
5313         rqst = nfsd_current_rqst();           << 
5314         if (!nfsd_v4client(rqst))             << 
5315                 return false;                 << 
5316         clp = *(rqst->rq_lease_breaker);      << 
5317         return dl->dl_stid.sc_client == clp;  << 
5318 }                                                3988 }
5319                                                  3989 
5320 static int                                       3990 static int
5321 nfsd_change_deleg_cb(struct file_lease *onlis !! 3991 nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
5322                      struct list_head *dispos    3992                      struct list_head *dispose)
5323 {                                                3993 {
5324         struct nfs4_delegation *dp = (struct  !! 3994         if (arg & F_UNLCK)
5325         struct nfs4_client *clp = dp->dl_stid << 
5326                                               << 
5327         if (arg & F_UNLCK) {                  << 
5328                 if (dp->dl_recalled)          << 
5329                         atomic_dec(&clp->cl_d << 
5330                 return lease_modify(onlist, a    3995                 return lease_modify(onlist, arg, dispose);
5331         } else                                !! 3996         else
5332                 return -EAGAIN;                  3997                 return -EAGAIN;
5333 }                                                3998 }
5334                                                  3999 
5335 static const struct lease_manager_operations  !! 4000 static const struct lock_manager_operations nfsd_lease_mng_ops = {
5336         .lm_breaker_owns_lease = nfsd_breaker << 
5337         .lm_break = nfsd_break_deleg_cb,         4001         .lm_break = nfsd_break_deleg_cb,
5338         .lm_change = nfsd_change_deleg_cb,       4002         .lm_change = nfsd_change_deleg_cb,
5339 };                                               4003 };
5340                                                  4004 
5341 static __be32 nfsd4_check_seqid(struct nfsd4_    4005 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
5342 {                                                4006 {
5343         if (nfsd4_has_session(cstate))           4007         if (nfsd4_has_session(cstate))
5344                 return nfs_ok;                   4008                 return nfs_ok;
5345         if (seqid == so->so_seqid - 1)           4009         if (seqid == so->so_seqid - 1)
5346                 return nfserr_replay_me;         4010                 return nfserr_replay_me;
5347         if (seqid == so->so_seqid)               4011         if (seqid == so->so_seqid)
5348                 return nfs_ok;                   4012                 return nfs_ok;
5349         return nfserr_bad_seqid;                 4013         return nfserr_bad_seqid;
5350 }                                                4014 }
5351                                                  4015 
5352 static struct nfs4_client *lookup_clientid(cl !! 4016 static __be32 lookup_clientid(clientid_t *clid,
5353                                               << 
5354 {                                             << 
5355         struct nfs4_client *found;            << 
5356                                               << 
5357         spin_lock(&nn->client_lock);          << 
5358         found = find_confirmed_client(clid, s << 
5359         if (found)                            << 
5360                 atomic_inc(&found->cl_rpc_use << 
5361         spin_unlock(&nn->client_lock);        << 
5362         return found;                         << 
5363 }                                             << 
5364                                               << 
5365 static __be32 set_client(clientid_t *clid,    << 
5366                 struct nfsd4_compound_state *    4017                 struct nfsd4_compound_state *cstate,
5367                 struct nfsd_net *nn)             4018                 struct nfsd_net *nn)
5368 {                                                4019 {
                                                   >> 4020         struct nfs4_client *found;
                                                   >> 4021 
5369         if (cstate->clp) {                       4022         if (cstate->clp) {
5370                 if (!same_clid(&cstate->clp-> !! 4023                 found = cstate->clp;
                                                   >> 4024                 if (!same_clid(&found->cl_clientid, clid))
5371                         return nfserr_stale_c    4025                         return nfserr_stale_clientid;
5372                 return nfs_ok;                   4026                 return nfs_ok;
5373         }                                        4027         }
                                                   >> 4028 
5374         if (STALE_CLIENTID(clid, nn))            4029         if (STALE_CLIENTID(clid, nn))
5375                 return nfserr_stale_clientid;    4030                 return nfserr_stale_clientid;
                                                   >> 4031 
5376         /*                                       4032         /*
5377          * We're in the 4.0 case (otherwise t !! 4033          * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
5378          * set cstate->clp), so session = fal !! 4034          * cached already then we know this is for is for v4.0 and "sessions"
                                                   >> 4035          * will be false.
5379          */                                      4036          */
5380         cstate->clp = lookup_clientid(clid, f !! 4037         WARN_ON_ONCE(cstate->session);
5381         if (!cstate->clp)                     !! 4038         spin_lock(&nn->client_lock);
                                                   >> 4039         found = find_confirmed_client(clid, false, nn);
                                                   >> 4040         if (!found) {
                                                   >> 4041                 spin_unlock(&nn->client_lock);
5382                 return nfserr_expired;           4042                 return nfserr_expired;
                                                   >> 4043         }
                                                   >> 4044         atomic_inc(&found->cl_refcount);
                                                   >> 4045         spin_unlock(&nn->client_lock);
                                                   >> 4046 
                                                   >> 4047         /* Cache the nfs4_client in cstate! */
                                                   >> 4048         cstate->clp = found;
5383         return nfs_ok;                           4049         return nfs_ok;
5384 }                                                4050 }
5385                                                  4051 
5386 __be32                                           4052 __be32
5387 nfsd4_process_open1(struct nfsd4_compound_sta    4053 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
5388                     struct nfsd4_open *open,     4054                     struct nfsd4_open *open, struct nfsd_net *nn)
5389 {                                                4055 {
5390         clientid_t *clientid = &open->op_clie    4056         clientid_t *clientid = &open->op_clientid;
5391         struct nfs4_client *clp = NULL;          4057         struct nfs4_client *clp = NULL;
5392         unsigned int strhashval;                 4058         unsigned int strhashval;
5393         struct nfs4_openowner *oo = NULL;        4059         struct nfs4_openowner *oo = NULL;
5394         __be32 status;                           4060         __be32 status;
5395                                                  4061 
                                                   >> 4062         if (STALE_CLIENTID(&open->op_clientid, nn))
                                                   >> 4063                 return nfserr_stale_clientid;
5396         /*                                       4064         /*
5397          * In case we need it later, after we    4065          * In case we need it later, after we've already created the
5398          * file and don't want to risk a furt    4066          * file and don't want to risk a further failure:
5399          */                                      4067          */
5400         open->op_file = nfsd4_alloc_file();      4068         open->op_file = nfsd4_alloc_file();
5401         if (open->op_file == NULL)               4069         if (open->op_file == NULL)
5402                 return nfserr_jukebox;           4070                 return nfserr_jukebox;
5403                                                  4071 
5404         status = set_client(clientid, cstate, !! 4072         status = lookup_clientid(clientid, cstate, nn);
5405         if (status)                              4073         if (status)
5406                 return status;                   4074                 return status;
5407         clp = cstate->clp;                       4075         clp = cstate->clp;
5408                                                  4076 
5409         strhashval = ownerstr_hashval(&open->    4077         strhashval = ownerstr_hashval(&open->op_owner);
5410 retry:                                        !! 4078         oo = find_openstateowner_str(strhashval, open, clp);
5411         oo = find_or_alloc_open_stateowner(st << 
5412         open->op_openowner = oo;                 4079         open->op_openowner = oo;
5413         if (!oo)                              !! 4080         if (!oo) {
5414                 return nfserr_jukebox;        !! 4081                 goto new_owner;
5415         if (nfsd4_cstate_assign_replay(cstate !! 4082         }
5416                 nfs4_put_stateowner(&oo->oo_o !! 4083         if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
5417                 goto retry;                   !! 4084                 /* Replace unconfirmed owners without checking for replay. */
                                                   >> 4085                 release_openowner(oo);
                                                   >> 4086                 open->op_openowner = NULL;
                                                   >> 4087                 goto new_owner;
5418         }                                        4088         }
5419         status = nfsd4_check_seqid(cstate, &o    4089         status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
5420         if (status)                              4090         if (status)
5421                 return status;                   4091                 return status;
5422                                               !! 4092         goto alloc_stateid;
                                                   >> 4093 new_owner:
                                                   >> 4094         oo = alloc_init_open_stateowner(strhashval, open, cstate);
                                                   >> 4095         if (oo == NULL)
                                                   >> 4096                 return nfserr_jukebox;
                                                   >> 4097         open->op_openowner = oo;
                                                   >> 4098 alloc_stateid:
5423         open->op_stp = nfs4_alloc_open_statei    4099         open->op_stp = nfs4_alloc_open_stateid(clp);
5424         if (!open->op_stp)                       4100         if (!open->op_stp)
5425                 return nfserr_jukebox;           4101                 return nfserr_jukebox;
5426                                                  4102 
5427         if (nfsd4_has_session(cstate) &&         4103         if (nfsd4_has_session(cstate) &&
5428             (cstate->current_fh.fh_export->ex    4104             (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
5429                 open->op_odstate = alloc_clnt    4105                 open->op_odstate = alloc_clnt_odstate(clp);
5430                 if (!open->op_odstate)           4106                 if (!open->op_odstate)
5431                         return nfserr_jukebox    4107                         return nfserr_jukebox;
5432         }                                        4108         }
5433                                                  4109 
5434         return nfs_ok;                           4110         return nfs_ok;
5435 }                                                4111 }
5436                                                  4112 
5437 static inline __be32                             4113 static inline __be32
5438 nfs4_check_delegmode(struct nfs4_delegation *    4114 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
5439 {                                                4115 {
5440         if ((flags & WR_STATE) && (dp->dl_typ    4116         if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
5441                 return nfserr_openmode;          4117                 return nfserr_openmode;
5442         else                                     4118         else
5443                 return nfs_ok;                   4119                 return nfs_ok;
5444 }                                                4120 }
5445                                                  4121 
5446 static int share_access_to_flags(u32 share_ac    4122 static int share_access_to_flags(u32 share_access)
5447 {                                                4123 {
5448         return share_access == NFS4_SHARE_ACC    4124         return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
5449 }                                                4125 }
5450                                                  4126 
5451 static struct nfs4_delegation *find_deleg_sta !! 4127 static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
5452                                               << 
5453 {                                                4128 {
5454         struct nfs4_stid *ret;                   4129         struct nfs4_stid *ret;
5455                                                  4130 
5456         ret = find_stateid_by_type(cl, s, SC_ !! 4131         ret = find_stateid_by_type(cl, s,
                                                   >> 4132                                 NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID);
5457         if (!ret)                                4133         if (!ret)
5458                 return NULL;                     4134                 return NULL;
5459         return delegstateid(ret);                4135         return delegstateid(ret);
5460 }                                                4136 }
5461                                                  4137 
5462 static bool nfsd4_is_deleg_cur(struct nfsd4_o    4138 static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
5463 {                                                4139 {
5464         return open->op_claim_type == NFS4_OP    4140         return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
5465                open->op_claim_type == NFS4_OP    4141                open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
5466 }                                                4142 }
5467                                                  4143 
5468 static __be32                                    4144 static __be32
5469 nfs4_check_deleg(struct nfs4_client *cl, stru    4145 nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
5470                 struct nfs4_delegation **dp)     4146                 struct nfs4_delegation **dp)
5471 {                                                4147 {
5472         int flags;                               4148         int flags;
5473         __be32 status = nfserr_bad_stateid;      4149         __be32 status = nfserr_bad_stateid;
5474         struct nfs4_delegation *deleg;           4150         struct nfs4_delegation *deleg;
5475                                                  4151 
5476         deleg = find_deleg_stateid(cl, &open-    4152         deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
5477         if (deleg == NULL)                       4153         if (deleg == NULL)
5478                 goto out;                        4154                 goto out;
5479         if (deleg->dl_stid.sc_status & SC_STA !! 4155         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    4156                 nfs4_put_stid(&deleg->dl_stid);
5486                 nfsd40_drop_revoked_stid(cl,  !! 4157                 if (cl->cl_minorversion)
5487                 status = nfserr_deleg_revoked !! 4158                         status = nfserr_deleg_revoked;
5488                 goto out;                        4159                 goto out;
5489         }                                        4160         }
5490         flags = share_access_to_flags(open->o    4161         flags = share_access_to_flags(open->op_share_access);
5491         status = nfs4_check_delegmode(deleg,     4162         status = nfs4_check_delegmode(deleg, flags);
5492         if (status) {                            4163         if (status) {
5493                 nfs4_put_stid(&deleg->dl_stid    4164                 nfs4_put_stid(&deleg->dl_stid);
5494                 goto out;                        4165                 goto out;
5495         }                                        4166         }
5496         *dp = deleg;                             4167         *dp = deleg;
5497 out:                                             4168 out:
5498         if (!nfsd4_is_deleg_cur(open))           4169         if (!nfsd4_is_deleg_cur(open))
5499                 return nfs_ok;                   4170                 return nfs_ok;
5500         if (status)                              4171         if (status)
5501                 return status;                   4172                 return status;
5502         open->op_openowner->oo_flags |= NFS4_    4173         open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
5503         return nfs_ok;                           4174         return nfs_ok;
5504 }                                                4175 }
5505                                                  4176 
5506 static inline int nfs4_access_to_access(u32 n    4177 static inline int nfs4_access_to_access(u32 nfs4_access)
5507 {                                                4178 {
5508         int flags = 0;                           4179         int flags = 0;
5509                                                  4180 
5510         if (nfs4_access & NFS4_SHARE_ACCESS_R    4181         if (nfs4_access & NFS4_SHARE_ACCESS_READ)
5511                 flags |= NFSD_MAY_READ;          4182                 flags |= NFSD_MAY_READ;
5512         if (nfs4_access & NFS4_SHARE_ACCESS_W    4183         if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
5513                 flags |= NFSD_MAY_WRITE;         4184                 flags |= NFSD_MAY_WRITE;
5514         return flags;                            4185         return flags;
5515 }                                                4186 }
5516                                                  4187 
5517 static inline __be32                             4188 static inline __be32
5518 nfsd4_truncate(struct svc_rqst *rqstp, struct    4189 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
5519                 struct nfsd4_open *open)         4190                 struct nfsd4_open *open)
5520 {                                                4191 {
5521         struct iattr iattr = {                   4192         struct iattr iattr = {
5522                 .ia_valid = ATTR_SIZE,           4193                 .ia_valid = ATTR_SIZE,
5523                 .ia_size = 0,                    4194                 .ia_size = 0,
5524         };                                       4195         };
5525         struct nfsd_attrs attrs = {           << 
5526                 .na_iattr       = &iattr,     << 
5527         };                                    << 
5528         if (!open->op_truncate)                  4196         if (!open->op_truncate)
5529                 return 0;                        4197                 return 0;
5530         if (!(open->op_share_access & NFS4_SH    4198         if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
5531                 return nfserr_inval;             4199                 return nfserr_inval;
5532         return nfsd_setattr(rqstp, fh, &attrs !! 4200         return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
5533 }                                                4201 }
5534                                                  4202 
5535 static __be32 nfs4_get_vfs_file(struct svc_rq    4203 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
5536                 struct svc_fh *cur_fh, struct    4204                 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
5537                 struct nfsd4_open *open, bool !! 4205                 struct nfsd4_open *open)
5538 {                                                4206 {
5539         struct nfsd_file *nf = NULL;          !! 4207         struct file *filp = NULL;
5540         __be32 status;                           4208         __be32 status;
5541         int oflag = nfs4_access_to_omode(open    4209         int oflag = nfs4_access_to_omode(open->op_share_access);
5542         int access = nfs4_access_to_access(op    4210         int access = nfs4_access_to_access(open->op_share_access);
5543         unsigned char old_access_bmap, old_de    4211         unsigned char old_access_bmap, old_deny_bmap;
5544                                                  4212 
5545         spin_lock(&fp->fi_lock);                 4213         spin_lock(&fp->fi_lock);
5546                                                  4214 
5547         /*                                       4215         /*
5548          * Are we trying to set a deny mode t    4216          * Are we trying to set a deny mode that would conflict with
5549          * current access?                       4217          * current access?
5550          */                                      4218          */
5551         status = nfs4_file_check_deny(fp, ope    4219         status = nfs4_file_check_deny(fp, open->op_share_deny);
5552         if (status != nfs_ok) {                  4220         if (status != nfs_ok) {
5553                 if (status != nfserr_share_de << 
5554                         spin_unlock(&fp->fi_l << 
5555                         goto out;             << 
5556                 }                             << 
5557                 if (nfs4_resolve_deny_conflic << 
5558                                 stp, open->op << 
5559                         status = nfserr_jukeb << 
5560                 spin_unlock(&fp->fi_lock);       4221                 spin_unlock(&fp->fi_lock);
5561                 goto out;                        4222                 goto out;
5562         }                                        4223         }
5563                                                  4224 
5564         /* set access to the file */             4225         /* set access to the file */
5565         status = nfs4_file_get_access(fp, ope    4226         status = nfs4_file_get_access(fp, open->op_share_access);
5566         if (status != nfs_ok) {                  4227         if (status != nfs_ok) {
5567                 if (status != nfserr_share_de << 
5568                         spin_unlock(&fp->fi_l << 
5569                         goto out;             << 
5570                 }                             << 
5571                 if (nfs4_resolve_deny_conflic << 
5572                                 stp, open->op << 
5573                         status = nfserr_jukeb << 
5574                 spin_unlock(&fp->fi_lock);       4228                 spin_unlock(&fp->fi_lock);
5575                 goto out;                        4229                 goto out;
5576         }                                        4230         }
5577                                                  4231 
5578         /* Set access bits in stateid */         4232         /* Set access bits in stateid */
5579         old_access_bmap = stp->st_access_bmap    4233         old_access_bmap = stp->st_access_bmap;
5580         set_access(open->op_share_access, stp    4234         set_access(open->op_share_access, stp);
5581                                                  4235 
5582         /* Set new deny mask */                  4236         /* Set new deny mask */
5583         old_deny_bmap = stp->st_deny_bmap;       4237         old_deny_bmap = stp->st_deny_bmap;
5584         set_deny(open->op_share_deny, stp);      4238         set_deny(open->op_share_deny, stp);
5585         fp->fi_share_deny |= (open->op_share_    4239         fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
5586                                                  4240 
5587         if (!fp->fi_fds[oflag]) {                4241         if (!fp->fi_fds[oflag]) {
5588                 spin_unlock(&fp->fi_lock);       4242                 spin_unlock(&fp->fi_lock);
5589                                               !! 4243                 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
5590                 status = nfsd_file_acquire_op !! 4244                 if (status)
5591                                               << 
5592                 if (status != nfs_ok)         << 
5593                         goto out_put_access;     4245                         goto out_put_access;
5594                                               << 
5595                 spin_lock(&fp->fi_lock);         4246                 spin_lock(&fp->fi_lock);
5596                 if (!fp->fi_fds[oflag]) {        4247                 if (!fp->fi_fds[oflag]) {
5597                         fp->fi_fds[oflag] = n !! 4248                         fp->fi_fds[oflag] = filp;
5598                         nf = NULL;            !! 4249                         filp = NULL;
5599                 }                                4250                 }
5600         }                                        4251         }
5601         spin_unlock(&fp->fi_lock);               4252         spin_unlock(&fp->fi_lock);
5602         if (nf)                               !! 4253         if (filp)
5603                 nfsd_file_put(nf);            !! 4254                 fput(filp);
5604                                               << 
5605         status = nfserrno(nfsd_open_break_lea << 
5606                                               << 
5607         if (status)                           << 
5608                 goto out_put_access;          << 
5609                                                  4255 
5610         status = nfsd4_truncate(rqstp, cur_fh    4256         status = nfsd4_truncate(rqstp, cur_fh, open);
5611         if (status)                              4257         if (status)
5612                 goto out_put_access;             4258                 goto out_put_access;
5613 out:                                             4259 out:
5614         return status;                           4260         return status;
5615 out_put_access:                                  4261 out_put_access:
5616         stp->st_access_bmap = old_access_bmap    4262         stp->st_access_bmap = old_access_bmap;
5617         nfs4_file_put_access(fp, open->op_sha    4263         nfs4_file_put_access(fp, open->op_share_access);
5618         reset_union_bmap_deny(bmap_to_share_m    4264         reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
5619         goto out;                                4265         goto out;
5620 }                                                4266 }
5621                                                  4267 
5622 static __be32                                    4268 static __be32
5623 nfs4_upgrade_open(struct svc_rqst *rqstp, str !! 4269 nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp, struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp, struct nfsd4_open *open)
5624                 struct svc_fh *cur_fh, struct << 
5625                 struct nfsd4_open *open)      << 
5626 {                                                4270 {
5627         __be32 status;                           4271         __be32 status;
5628         unsigned char old_deny_bmap = stp->st    4272         unsigned char old_deny_bmap = stp->st_deny_bmap;
5629                                                  4273 
5630         if (!test_access(open->op_share_acces    4274         if (!test_access(open->op_share_access, stp))
5631                 return nfs4_get_vfs_file(rqst !! 4275                 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
5632                                                  4276 
5633         /* test and set deny mode */             4277         /* test and set deny mode */
5634         spin_lock(&fp->fi_lock);                 4278         spin_lock(&fp->fi_lock);
5635         status = nfs4_file_check_deny(fp, ope    4279         status = nfs4_file_check_deny(fp, open->op_share_deny);
5636         switch (status) {                     !! 4280         if (status == nfs_ok) {
5637         case nfs_ok:                          << 
5638                 set_deny(open->op_share_deny,    4281                 set_deny(open->op_share_deny, stp);
5639                 fp->fi_share_deny |=             4282                 fp->fi_share_deny |=
5640                         (open->op_share_deny  !! 4283                                 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
5641                 break;                        << 
5642         case nfserr_share_denied:             << 
5643                 if (nfs4_resolve_deny_conflic << 
5644                                 stp, open->op << 
5645                         status = nfserr_jukeb << 
5646                 break;                        << 
5647         }                                        4284         }
5648         spin_unlock(&fp->fi_lock);               4285         spin_unlock(&fp->fi_lock);
5649                                                  4286 
5650         if (status != nfs_ok)                    4287         if (status != nfs_ok)
5651                 return status;                   4288                 return status;
5652                                                  4289 
5653         status = nfsd4_truncate(rqstp, cur_fh    4290         status = nfsd4_truncate(rqstp, cur_fh, open);
5654         if (status != nfs_ok)                    4291         if (status != nfs_ok)
5655                 reset_union_bmap_deny(old_den    4292                 reset_union_bmap_deny(old_deny_bmap, stp);
5656         return status;                           4293         return status;
5657 }                                                4294 }
5658                                                  4295 
5659 /* Should we give out recallable state?: */      4296 /* Should we give out recallable state?: */
5660 static bool nfsd4_cb_channel_good(struct nfs4    4297 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
5661 {                                                4298 {
5662         if (clp->cl_cb_state == NFSD4_CB_UP)     4299         if (clp->cl_cb_state == NFSD4_CB_UP)
5663                 return true;                     4300                 return true;
5664         /*                                       4301         /*
5665          * In the sessions case, since we don    4302          * In the sessions case, since we don't have to establish a
5666          * separate connection for callbacks,    4303          * separate connection for callbacks, we assume it's OK
5667          * until we hear otherwise:              4304          * until we hear otherwise:
5668          */                                      4305          */
5669         return clp->cl_minorversion && clp->c    4306         return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
5670 }                                                4307 }
5671                                                  4308 
5672 static struct file_lease *nfs4_alloc_init_lea !! 4309 static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
5673                                               << 
5674 {                                                4310 {
5675         struct file_lease *fl;                !! 4311         struct file_lock *fl;
5676                                                  4312 
5677         fl = locks_alloc_lease();             !! 4313         fl = locks_alloc_lock();
5678         if (!fl)                                 4314         if (!fl)
5679                 return NULL;                     4315                 return NULL;
5680         fl->fl_lmops = &nfsd_lease_mng_ops;      4316         fl->fl_lmops = &nfsd_lease_mng_ops;
5681         fl->c.flc_flags = FL_DELEG;           !! 4317         fl->fl_flags = FL_DELEG;
5682         fl->c.flc_type = flag == NFS4_OPEN_DE !! 4318         fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
5683         fl->c.flc_owner = (fl_owner_t)dp;     !! 4319         fl->fl_end = OFFSET_MAX;
5684         fl->c.flc_pid = current->tgid;        !! 4320         fl->fl_owner = (fl_owner_t)fp;
5685         fl->c.flc_file = dp->dl_stid.sc_file- !! 4321         fl->fl_pid = current->tgid;
5686         return fl;                               4322         return fl;
5687 }                                                4323 }
5688                                                  4324 
5689 static int nfsd4_check_conflicting_opens(stru !! 4325 /**
5690                                          stru !! 4326  * nfs4_setlease - Obtain a delegation by requesting lease from vfs layer
5691 {                                             !! 4327  * @dp:   a pointer to the nfs4_delegation we're adding.
5692         struct nfs4_ol_stateid *st;           !! 4328  *
5693         struct file *f = fp->fi_deleg_file->n !! 4329  * Return:
5694         struct inode *ino = file_inode(f);    !! 4330  *      On success: Return code will be 0 on success.
5695         int writes;                           !! 4331  *
                                                   >> 4332  *      On error: -EAGAIN if there was an existing delegation.
                                                   >> 4333  *                 nonzero if there is an error in other cases.
                                                   >> 4334  *
                                                   >> 4335  */
5696                                                  4336 
5697         writes = atomic_read(&ino->i_writecou !! 4337 static int nfs4_setlease(struct nfs4_delegation *dp)
5698         if (!writes)                          !! 4338 {
5699                 return 0;                     !! 4339         struct nfs4_file *fp = dp->dl_stid.sc_file;
5700         /*                                    !! 4340         struct file_lock *fl;
5701          * There could be multiple filehandle !! 4341         struct file *filp;
5702          * nfs4_files) referencing this file, !! 4342         int status = 0;
5703          * common; let's just give up in that << 
5704          * trying to go look up all the clien << 
5705          * nfs4_file as well:                 << 
5706          */                                   << 
5707         if (fp->fi_aliased)                   << 
5708                 return -EAGAIN;               << 
5709         /*                                    << 
5710          * If there's a close in progress, ma << 
5711          * clear any fi_fds[] entries before  << 
5712          * i_writecount:                      << 
5713          */                                   << 
5714         smp_mb__after_atomic();               << 
5715                                                  4343 
5716         if (fp->fi_fds[O_WRONLY])             !! 4344         fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
5717                 writes--;                     !! 4345         if (!fl)
5718         if (fp->fi_fds[O_RDWR])               !! 4346                 return -ENOMEM;
5719                 writes--;                     !! 4347         filp = find_readable_file(fp);
5720         if (writes > 0)                       !! 4348         if (!filp) {
5721                 return -EAGAIN; /* There may  !! 4349                 /* We should always have a readable file here */
5722         /*                                    !! 4350                 WARN_ON_ONCE(1);
5723          * It's possible there are non-NFSv4  !! 4351                 locks_free_lock(fl);
5724          * but if they haven't incremented i_ !! 4352                 return -EBADF;
5725          * also haven't called break lease ye !! 4353         }
5726          * lease soon enough.  So, all that's !! 4354         fl->fl_file = filp;
5727          * opens:                             !! 4355         status = vfs_setlease(filp, fl->fl_type, &fl, NULL);
5728          */                                   !! 4356         if (fl)
                                                   >> 4357                 locks_free_lock(fl);
                                                   >> 4358         if (status)
                                                   >> 4359                 goto out_fput;
                                                   >> 4360         spin_lock(&state_lock);
5729         spin_lock(&fp->fi_lock);                 4361         spin_lock(&fp->fi_lock);
5730         list_for_each_entry(st, &fp->fi_state !! 4362         /* Did the lease get broken before we took the lock? */
5731                 if (st->st_openstp == NULL /* !! 4363         status = -EAGAIN;
5732                     access_permit_write(st) & !! 4364         if (fp->fi_had_conflict)
5733                     st->st_stid.sc_client !=  !! 4365                 goto out_unlock;
5734                         spin_unlock(&fp->fi_l !! 4366         /* Race breaker */
5735                         return -EAGAIN;       !! 4367         if (fp->fi_deleg_file) {
5736                 }                             !! 4368                 status = hash_delegation_locked(dp, fp);
                                                   >> 4369                 goto out_unlock;
5737         }                                        4370         }
                                                   >> 4371         fp->fi_deleg_file = filp;
                                                   >> 4372         fp->fi_delegees = 0;
                                                   >> 4373         status = hash_delegation_locked(dp, fp);
5738         spin_unlock(&fp->fi_lock);               4374         spin_unlock(&fp->fi_lock);
5739         /*                                    !! 4375         spin_unlock(&state_lock);
5740          * There's a small chance that we cou !! 4376         if (status) {
5741          * NFSv4 open.  However, any open tha !! 4377                 /* Should never happen, this is a new fi_deleg_file  */
5742          * the fi_stateids list also hasn't c !! 4378                 WARN_ON_ONCE(1);
5743          * they'll break this lease soon enou !! 4379                 goto out_fput;
5744          */                                   !! 4380         }
5745         return 0;                             << 
5746 }                                             << 
5747                                               << 
5748 /*                                            << 
5749  * It's possible that between opening the den << 
5750  * that it has been renamed or unlinked. Redo << 
5751  * hasn't happened.                           << 
5752  */                                           << 
5753 static int                                    << 
5754 nfsd4_verify_deleg_dentry(struct nfsd4_open * << 
5755                           struct svc_fh *pare << 
5756 {                                             << 
5757         struct svc_export *exp;               << 
5758         struct dentry *child;                 << 
5759         __be32 err;                           << 
5760                                               << 
5761         err = nfsd_lookup_dentry(open->op_rqs << 
5762                                  open->op_fna << 
5763                                  &exp, &child << 
5764                                               << 
5765         if (err)                              << 
5766                 return -EAGAIN;               << 
5767                                               << 
5768         exp_put(exp);                         << 
5769         dput(child);                          << 
5770         if (child != file_dentry(fp->fi_deleg << 
5771                 return -EAGAIN;               << 
5772                                               << 
5773         return 0;                             << 
5774 }                                             << 
5775                                               << 
5776 /*                                            << 
5777  * We avoid breaking delegations held by a cl << 
5778  * clearing setuid/setgid bits on a write is  << 
5779  * may not notice and continue using the old  << 
5780  * on setuid/setgid files when the client is  << 
5781  */                                           << 
5782 static int                                    << 
5783 nfsd4_verify_setuid_write(struct nfsd4_open * << 
5784 {                                             << 
5785         struct inode *inode = file_inode(nf-> << 
5786                                               << 
5787         if ((open->op_share_access & NFS4_SHA << 
5788             (inode->i_mode & (S_ISUID|S_ISGID << 
5789                 return -EAGAIN;               << 
5790         return 0;                                4381         return 0;
                                                   >> 4382 out_unlock:
                                                   >> 4383         spin_unlock(&fp->fi_lock);
                                                   >> 4384         spin_unlock(&state_lock);
                                                   >> 4385 out_fput:
                                                   >> 4386         fput(filp);
                                                   >> 4387         return status;
5791 }                                                4388 }
5792                                                  4389 
5793 static struct nfs4_delegation *                  4390 static struct nfs4_delegation *
5794 nfs4_set_delegation(struct nfsd4_open *open,  !! 4391 nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
5795                     struct svc_fh *parent)    !! 4392                     struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate)
5796 {                                                4393 {
5797         int status = 0;                       !! 4394         int status;
5798         struct nfs4_client *clp = stp->st_sti << 
5799         struct nfs4_file *fp = stp->st_stid.s << 
5800         struct nfs4_clnt_odstate *odstate = s << 
5801         struct nfs4_delegation *dp;              4395         struct nfs4_delegation *dp;
5802         struct nfsd_file *nf = NULL;          << 
5803         struct file_lease *fl;                << 
5804         u32 dl_type;                          << 
5805                                                  4396 
5806         /*                                    << 
5807          * The fi_had_conflict and nfs_get_ex << 
5808          * here are just optimizations; we'll << 
5809          * the end:                           << 
5810          */                                   << 
5811         if (fp->fi_had_conflict)                 4397         if (fp->fi_had_conflict)
5812                 return ERR_PTR(-EAGAIN);         4398                 return ERR_PTR(-EAGAIN);
5813                                                  4399 
5814         /*                                    << 
5815          * Try for a write delegation first.  << 
5816          *                                    << 
5817          *  "An OPEN_DELEGATE_WRITE delegatio << 
5818          *   on its own, all opens."          << 
5819          *                                    << 
5820          * Furthermore the client can use a w << 
5821          * operations as well, so we require  << 
5822          *                                    << 
5823          * Offer a write delegation in the ca << 
5824          * we get the O_RDWR descriptor.      << 
5825          */                                   << 
5826         if ((open->op_share_access & NFS4_SHA << 
5827                 nf = find_rw_file(fp);        << 
5828                 dl_type = NFS4_OPEN_DELEGATE_ << 
5829         }                                     << 
5830                                               << 
5831         /*                                    << 
5832          * If the file is being opened O_RDON << 
5833          * file for some reason, then try for << 
5834          */                                   << 
5835         if (!nf && (open->op_share_access & N << 
5836                 nf = find_readable_file(fp);  << 
5837                 dl_type = NFS4_OPEN_DELEGATE_ << 
5838         }                                     << 
5839                                               << 
5840         if (!nf)                              << 
5841                 return ERR_PTR(-EAGAIN);      << 
5842                                               << 
5843         spin_lock(&state_lock);                  4400         spin_lock(&state_lock);
5844         spin_lock(&fp->fi_lock);                 4401         spin_lock(&fp->fi_lock);
5845         if (nfs4_delegation_exists(clp, fp))  !! 4402         status = nfs4_get_existing_delegation(clp, fp);
5846                 status = -EAGAIN;             << 
5847         else if (nfsd4_verify_setuid_write(op << 
5848                 status = -EAGAIN;             << 
5849         else if (!fp->fi_deleg_file) {        << 
5850                 fp->fi_deleg_file = nf;       << 
5851                 /* increment early to prevent << 
5852                  * cleared */                 << 
5853                 fp->fi_delegees = 1;          << 
5854                 nf = NULL;                    << 
5855         } else                                << 
5856                 fp->fi_delegees++;            << 
5857         spin_unlock(&fp->fi_lock);               4403         spin_unlock(&fp->fi_lock);
5858         spin_unlock(&state_lock);                4404         spin_unlock(&state_lock);
5859         if (nf)                               !! 4405 
5860                 nfsd_file_put(nf);            << 
5861         if (status)                              4406         if (status)
5862                 return ERR_PTR(status);          4407                 return ERR_PTR(status);
5863                                                  4408 
5864         status = -ENOMEM;                     !! 4409         dp = alloc_init_deleg(clp, fh, odstate);
5865         dp = alloc_init_deleg(clp, fp, odstat << 
5866         if (!dp)                                 4410         if (!dp)
5867                 goto out_delegees;            !! 4411                 return ERR_PTR(-ENOMEM);
5868                                               << 
5869         fl = nfs4_alloc_init_lease(dp, dl_typ << 
5870         if (!fl)                              << 
5871                 goto out_clnt_odstate;        << 
5872                                               << 
5873         status = kernel_setlease(fp->fi_deleg << 
5874                                       fl->c.f << 
5875         if (fl)                               << 
5876                 locks_free_lease(fl);         << 
5877         if (status)                           << 
5878                 goto out_clnt_odstate;        << 
5879                                               << 
5880         if (parent) {                         << 
5881                 status = nfsd4_verify_deleg_d << 
5882                 if (status)                   << 
5883                         goto out_unlock;      << 
5884         }                                     << 
5885                                               << 
5886         status = nfsd4_check_conflicting_open << 
5887         if (status)                           << 
5888                 goto out_unlock;              << 
5889                                               << 
5890         /*                                    << 
5891          * Now that the deleg is set, check a << 
5892          * raced in and changed the mode whil << 
5893          */                                   << 
5894         status = nfsd4_verify_setuid_write(op << 
5895         if (status)                           << 
5896                 goto out_unlock;              << 
5897                                               << 
5898         status = -EAGAIN;                     << 
5899         if (fp->fi_had_conflict)              << 
5900                 goto out_unlock;              << 
5901                                                  4412 
                                                   >> 4413         get_nfs4_file(fp);
5902         spin_lock(&state_lock);                  4414         spin_lock(&state_lock);
5903         spin_lock(&clp->cl_lock);             << 
5904         spin_lock(&fp->fi_lock);                 4415         spin_lock(&fp->fi_lock);
                                                   >> 4416         dp->dl_stid.sc_file = fp;
                                                   >> 4417         if (!fp->fi_deleg_file) {
                                                   >> 4418                 spin_unlock(&fp->fi_lock);
                                                   >> 4419                 spin_unlock(&state_lock);
                                                   >> 4420                 status = nfs4_setlease(dp);
                                                   >> 4421                 goto out;
                                                   >> 4422         }
                                                   >> 4423         if (fp->fi_had_conflict) {
                                                   >> 4424                 status = -EAGAIN;
                                                   >> 4425                 goto out_unlock;
                                                   >> 4426         }
5905         status = hash_delegation_locked(dp, f    4427         status = hash_delegation_locked(dp, fp);
                                                   >> 4428 out_unlock:
5906         spin_unlock(&fp->fi_lock);               4429         spin_unlock(&fp->fi_lock);
5907         spin_unlock(&clp->cl_lock);           << 
5908         spin_unlock(&state_lock);                4430         spin_unlock(&state_lock);
5909                                               !! 4431 out:
5910         if (status)                           !! 4432         if (status) {
5911                 goto out_unlock;              !! 4433                 put_clnt_odstate(dp->dl_clnt_odstate);
5912                                               !! 4434                 nfs4_put_stid(&dp->dl_stid);
                                                   >> 4435                 return ERR_PTR(status);
                                                   >> 4436         }
5913         return dp;                               4437         return dp;
5914 out_unlock:                                   << 
5915         kernel_setlease(fp->fi_deleg_file->nf << 
5916 out_clnt_odstate:                             << 
5917         put_clnt_odstate(dp->dl_clnt_odstate) << 
5918         nfs4_put_stid(&dp->dl_stid);          << 
5919 out_delegees:                                 << 
5920         put_deleg_file(fp);                   << 
5921         return ERR_PTR(status);               << 
5922 }                                                4438 }
5923                                                  4439 
5924 static void nfsd4_open_deleg_none_ext(struct     4440 static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
5925 {                                                4441 {
5926         open->op_delegate_type = NFS4_OPEN_DE    4442         open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5927         if (status == -EAGAIN)                   4443         if (status == -EAGAIN)
5928                 open->op_why_no_deleg = WND4_    4444                 open->op_why_no_deleg = WND4_CONTENTION;
5929         else {                                   4445         else {
5930                 open->op_why_no_deleg = WND4_    4446                 open->op_why_no_deleg = WND4_RESOURCE;
5931                 switch (open->op_deleg_want)     4447                 switch (open->op_deleg_want) {
5932                 case NFS4_SHARE_WANT_READ_DEL    4448                 case NFS4_SHARE_WANT_READ_DELEG:
5933                 case NFS4_SHARE_WANT_WRITE_DE    4449                 case NFS4_SHARE_WANT_WRITE_DELEG:
5934                 case NFS4_SHARE_WANT_ANY_DELE    4450                 case NFS4_SHARE_WANT_ANY_DELEG:
5935                         break;                   4451                         break;
5936                 case NFS4_SHARE_WANT_CANCEL:     4452                 case NFS4_SHARE_WANT_CANCEL:
5937                         open->op_why_no_deleg    4453                         open->op_why_no_deleg = WND4_CANCELLED;
5938                         break;                   4454                         break;
5939                 case NFS4_SHARE_WANT_NO_DELEG    4455                 case NFS4_SHARE_WANT_NO_DELEG:
5940                         WARN_ON_ONCE(1);         4456                         WARN_ON_ONCE(1);
5941                 }                                4457                 }
5942         }                                        4458         }
5943 }                                                4459 }
5944                                                  4460 
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 /*                                               4461 /*
5968  * The Linux NFS server does not offer write  !! 4462  * Attempt to hand out a delegation.
5969  * clients in order to avoid conflicts betwee << 
5970  * GETATTRs requesting CHANGE or SIZE attribu << 
5971  *                                            << 
5972  * With NFSv4.1 and later minorversions, the  << 
5973  * begins each COMPOUND contains a client ID. << 
5974  * be avoided when the server recognizes the  << 
5975  * GETATTR also holds write delegation it con << 
5976  *                                               4463  *
5977  * However, the NFSv4.0 protocol does not ena !! 4464  * Note we don't support write delegations, and won't until the vfs has
5978  * determine that a GETATTR originated from t !! 4465  * proper support for them.
5979  * conflicting delegation versus coming from  << 
5980  * RFC 7530 Section 16.7.5, the server must r << 
5981  * CB_GETATTR even when the GETATTR originate << 
5982  * holds the conflicting delegation.          << 
5983  *                                            << 
5984  * An NFSv4.0 client can trigger a pathologic << 
5985  * always sends a DELEGRETURN preceded by a c << 
5986  * the same COMPOUND. COMPOUND execution will << 
5987  * GETATTR and the DELEGRETURN will never get << 
5988  * eventually revokes the delegation, which c << 
5989  * open or lock state.                        << 
5990  */                                              4466  */
5991 static void                                      4467 static void
5992 nfs4_open_delegation(struct nfsd4_open *open, !! 4468 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
5993                      struct svc_fh *currentfh !! 4469                         struct nfs4_ol_stateid *stp)
5994 {                                                4470 {
5995         struct nfs4_delegation *dp;              4471         struct nfs4_delegation *dp;
5996         struct nfs4_openowner *oo = openowner    4472         struct nfs4_openowner *oo = openowner(stp->st_stateowner);
5997         struct nfs4_client *clp = stp->st_sti    4473         struct nfs4_client *clp = stp->st_stid.sc_client;
5998         struct svc_fh *parent = NULL;         << 
5999         int cb_up;                               4474         int cb_up;
6000         int status = 0;                          4475         int status = 0;
6001         struct kstat stat;                    << 
6002                                                  4476 
6003         cb_up = nfsd4_cb_channel_good(oo->oo_    4477         cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
6004         open->op_recall = false;              !! 4478         open->op_recall = 0;
6005         switch (open->op_claim_type) {           4479         switch (open->op_claim_type) {
6006                 case NFS4_OPEN_CLAIM_PREVIOUS    4480                 case NFS4_OPEN_CLAIM_PREVIOUS:
6007                         if (!cb_up)              4481                         if (!cb_up)
6008                                 open->op_reca !! 4482                                 open->op_recall = 1;
                                                   >> 4483                         if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
                                                   >> 4484                                 goto out_no_deleg;
6009                         break;                   4485                         break;
6010                 case NFS4_OPEN_CLAIM_NULL:       4486                 case NFS4_OPEN_CLAIM_NULL:
6011                         parent = currentfh;   << 
6012                         fallthrough;          << 
6013                 case NFS4_OPEN_CLAIM_FH:         4487                 case NFS4_OPEN_CLAIM_FH:
6014                         /*                       4488                         /*
6015                          * Let's not give out    4489                          * Let's not give out any delegations till everyone's
6016                          * had the chance to     4490                          * had the chance to reclaim theirs, *and* until
6017                          * NLM locks have all    4491                          * NLM locks have all been reclaimed:
6018                          */                      4492                          */
6019                         if (locks_in_grace(cl    4493                         if (locks_in_grace(clp->net))
6020                                 goto out_no_d    4494                                 goto out_no_deleg;
6021                         if (!cb_up || !(oo->o    4495                         if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
6022                                 goto out_no_d    4496                                 goto out_no_deleg;
6023                         if (open->op_share_ac !! 4497                         /*
6024                                         !clp- !! 4498                          * Also, if the file was opened for write or
                                                   >> 4499                          * create, there's a good chance the client's
                                                   >> 4500                          * about to write to it, resulting in an
                                                   >> 4501                          * immediate recall (since we don't support
                                                   >> 4502                          * write delegations):
                                                   >> 4503                          */
                                                   >> 4504                         if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
                                                   >> 4505                                 goto out_no_deleg;
                                                   >> 4506                         if (open->op_create == NFS4_OPEN_CREATE)
6025                                 goto out_no_d    4507                                 goto out_no_deleg;
6026                         break;                   4508                         break;
6027                 default:                         4509                 default:
6028                         goto out_no_deleg;       4510                         goto out_no_deleg;
6029         }                                        4511         }
6030         dp = nfs4_set_delegation(open, stp, p !! 4512         dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate);
6031         if (IS_ERR(dp))                          4513         if (IS_ERR(dp))
6032                 goto out_no_deleg;               4514                 goto out_no_deleg;
6033                                                  4515 
6034         memcpy(&open->op_delegate_stateid, &d    4516         memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
6035                                                  4517 
6036         if (open->op_share_access & NFS4_SHAR !! 4518         dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
6037                 if (!nfs4_delegation_stat(dp, !! 4519                 STATEID_VAL(&dp->dl_stid.sc_stateid));
6038                         nfs4_put_stid(&dp->dl !! 4520         open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
6039                         destroy_delegation(dp << 
6040                         goto out_no_deleg;    << 
6041                 }                             << 
6042                 open->op_delegate_type = NFS4 << 
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 << 
6047         } else {                              << 
6048                 open->op_delegate_type = NFS4 << 
6049                 trace_nfsd_deleg_read(&dp->dl << 
6050         }                                     << 
6051         nfs4_put_stid(&dp->dl_stid);             4521         nfs4_put_stid(&dp->dl_stid);
6052         return;                                  4522         return;
6053 out_no_deleg:                                    4523 out_no_deleg:
6054         open->op_delegate_type = NFS4_OPEN_DE    4524         open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
6055         if (open->op_claim_type == NFS4_OPEN_    4525         if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
6056             open->op_delegate_type != NFS4_OP    4526             open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
6057                 dprintk("NFSD: WARNING: refus    4527                 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
6058                 open->op_recall = true;       !! 4528                 open->op_recall = 1;
6059         }                                        4529         }
6060                                                  4530 
6061         /* 4.1 client asking for a delegation    4531         /* 4.1 client asking for a delegation? */
6062         if (open->op_deleg_want)                 4532         if (open->op_deleg_want)
6063                 nfsd4_open_deleg_none_ext(ope    4533                 nfsd4_open_deleg_none_ext(open, status);
6064         return;                                  4534         return;
6065 }                                                4535 }
6066                                                  4536 
6067 static void nfsd4_deleg_xgrade_none_ext(struc    4537 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
6068                                         struc    4538                                         struct nfs4_delegation *dp)
6069 {                                                4539 {
6070         if (open->op_deleg_want == NFS4_SHARE    4540         if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
6071             dp->dl_type == NFS4_OPEN_DELEGATE    4541             dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
6072                 open->op_delegate_type = NFS4    4542                 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
6073                 open->op_why_no_deleg = WND4_    4543                 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
6074         } else if (open->op_deleg_want == NFS    4544         } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
6075                    dp->dl_type == NFS4_OPEN_D    4545                    dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
6076                 open->op_delegate_type = NFS4    4546                 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
6077                 open->op_why_no_deleg = WND4_    4547                 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
6078         }                                        4548         }
6079         /* Otherwise the client must be confu    4549         /* Otherwise the client must be confused wanting a delegation
6080          * it already has, therefore we don't    4550          * it already has, therefore we don't return
6081          * NFS4_OPEN_DELEGATE_NONE_EXT and re    4551          * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
6082          */                                      4552          */
6083 }                                                4553 }
6084                                                  4554 
6085 /**                                           << 
6086  * nfsd4_process_open2 - finish open processi << 
6087  * @rqstp: the RPC transaction being executed << 
6088  * @current_fh: NFSv4 COMPOUND's current file << 
6089  * @open: OPEN arguments                      << 
6090  *                                            << 
6091  * If successful, (1) truncate the file if op << 
6092  * set, (2) set open->op_stateid, (3) set ope << 
6093  *                                            << 
6094  * Returns %nfs_ok on success; otherwise an n << 
6095  * network byte order is returned.            << 
6096  */                                           << 
6097 __be32                                           4555 __be32
6098 nfsd4_process_open2(struct svc_rqst *rqstp, s    4556 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
6099 {                                                4557 {
6100         struct nfsd4_compoundres *resp = rqst    4558         struct nfsd4_compoundres *resp = rqstp->rq_resp;
6101         struct nfs4_client *cl = open->op_ope    4559         struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
6102         struct nfs4_file *fp = NULL;             4560         struct nfs4_file *fp = NULL;
6103         struct nfs4_ol_stateid *stp = NULL;      4561         struct nfs4_ol_stateid *stp = NULL;
6104         struct nfs4_delegation *dp = NULL;       4562         struct nfs4_delegation *dp = NULL;
6105         __be32 status;                           4563         __be32 status;
6106         bool new_stp = false;                    4564         bool new_stp = false;
6107                                                  4565 
6108         /*                                       4566         /*
6109          * Lookup file; if found, lookup stat    4567          * Lookup file; if found, lookup stateid and check open request,
6110          * and check for delegations in the p    4568          * and check for delegations in the process of being recalled.
6111          * If not found, create the nfs4_file    4569          * If not found, create the nfs4_file struct
6112          */                                      4570          */
6113         fp = nfsd4_file_hash_insert(open->op_ !! 4571         fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
6114         if (unlikely(!fp))                    << 
6115                 return nfserr_jukebox;        << 
6116         if (fp != open->op_file) {               4572         if (fp != open->op_file) {
6117                 status = nfs4_check_deleg(cl,    4573                 status = nfs4_check_deleg(cl, open, &dp);
6118                 if (status)                      4574                 if (status)
6119                         goto out;                4575                         goto out;
6120                 stp = nfsd4_find_and_lock_exi    4576                 stp = nfsd4_find_and_lock_existing_open(fp, open);
6121         } else {                                 4577         } else {
6122                 open->op_file = NULL;            4578                 open->op_file = NULL;
6123                 status = nfserr_bad_stateid;     4579                 status = nfserr_bad_stateid;
6124                 if (nfsd4_is_deleg_cur(open))    4580                 if (nfsd4_is_deleg_cur(open))
6125                         goto out;                4581                         goto out;
6126         }                                        4582         }
6127                                                  4583 
6128         if (!stp) {                              4584         if (!stp) {
6129                 stp = init_open_stateid(fp, o    4585                 stp = init_open_stateid(fp, open);
6130                 if (!open->op_stp)               4586                 if (!open->op_stp)
6131                         new_stp = true;          4587                         new_stp = true;
6132         }                                        4588         }
6133                                                  4589 
6134         /*                                       4590         /*
6135          * OPEN the file, or upgrade an exist    4591          * OPEN the file, or upgrade an existing OPEN.
6136          * If truncate fails, the OPEN fails.    4592          * If truncate fails, the OPEN fails.
6137          *                                       4593          *
6138          * stp is already locked.                4594          * stp is already locked.
6139          */                                      4595          */
6140         if (!new_stp) {                          4596         if (!new_stp) {
6141                 /* Stateid was found, this is    4597                 /* Stateid was found, this is an OPEN upgrade */
6142                 status = nfs4_upgrade_open(rq    4598                 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
6143                 if (status) {                    4599                 if (status) {
6144                         mutex_unlock(&stp->st    4600                         mutex_unlock(&stp->st_mutex);
6145                         goto out;                4601                         goto out;
6146                 }                                4602                 }
6147         } else {                                 4603         } else {
6148                 status = nfs4_get_vfs_file(rq !! 4604                 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
6149                 if (status) {                    4605                 if (status) {
                                                   >> 4606                         stp->st_stid.sc_type = NFS4_CLOSED_STID;
6150                         release_open_stateid(    4607                         release_open_stateid(stp);
6151                         mutex_unlock(&stp->st    4608                         mutex_unlock(&stp->st_mutex);
6152                         goto out;                4609                         goto out;
6153                 }                                4610                 }
6154                                                  4611 
6155                 stp->st_clnt_odstate = find_o    4612                 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
6156                                                  4613                                                         open->op_odstate);
6157                 if (stp->st_clnt_odstate == o    4614                 if (stp->st_clnt_odstate == open->op_odstate)
6158                         open->op_odstate = NU    4615                         open->op_odstate = NULL;
6159         }                                        4616         }
6160                                                  4617 
6161         nfs4_inc_and_copy_stateid(&open->op_s    4618         nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
6162         mutex_unlock(&stp->st_mutex);            4619         mutex_unlock(&stp->st_mutex);
6163                                                  4620 
6164         if (nfsd4_has_session(&resp->cstate))    4621         if (nfsd4_has_session(&resp->cstate)) {
6165                 if (open->op_deleg_want & NFS    4622                 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
6166                         open->op_delegate_typ    4623                         open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
6167                         open->op_why_no_deleg    4624                         open->op_why_no_deleg = WND4_NOT_WANTED;
6168                         goto nodeleg;            4625                         goto nodeleg;
6169                 }                                4626                 }
6170         }                                        4627         }
6171                                                  4628 
6172         /*                                       4629         /*
6173         * Attempt to hand out a delegation. N    4630         * Attempt to hand out a delegation. No error return, because the
6174         * OPEN succeeds even if we fail.         4631         * OPEN succeeds even if we fail.
6175         */                                       4632         */
6176         nfs4_open_delegation(open, stp, &resp !! 4633         nfs4_open_delegation(current_fh, open, stp);
6177 nodeleg:                                         4634 nodeleg:
6178         status = nfs_ok;                         4635         status = nfs_ok;
6179         trace_nfsd_open(&stp->st_stid.sc_stat !! 4636 
                                                   >> 4637         dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
                                                   >> 4638                 STATEID_VAL(&stp->st_stid.sc_stateid));
6180 out:                                             4639 out:
6181         /* 4.1 client trying to upgrade/downg    4640         /* 4.1 client trying to upgrade/downgrade delegation? */
6182         if (open->op_delegate_type == NFS4_OP    4641         if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
6183             open->op_deleg_want)                 4642             open->op_deleg_want)
6184                 nfsd4_deleg_xgrade_none_ext(o    4643                 nfsd4_deleg_xgrade_none_ext(open, dp);
6185                                                  4644 
6186         if (fp)                                  4645         if (fp)
6187                 put_nfs4_file(fp);               4646                 put_nfs4_file(fp);
6188         if (status == 0 && open->op_claim_typ    4647         if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
6189                 open->op_openowner->oo_flags     4648                 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
6190         /*                                       4649         /*
6191         * To finish the open response, we jus    4650         * To finish the open response, we just need to set the rflags.
6192         */                                       4651         */
6193         open->op_rflags = NFS4_OPEN_RESULT_LO    4652         open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
6194         if (nfsd4_has_session(&resp->cstate))    4653         if (nfsd4_has_session(&resp->cstate))
6195                 open->op_rflags |= NFS4_OPEN_    4654                 open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK;
6196         else if (!(open->op_openowner->oo_fla    4655         else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED))
6197                 open->op_rflags |= NFS4_OPEN_    4656                 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
6198                                                  4657 
6199         if (dp)                                  4658         if (dp)
6200                 nfs4_put_stid(&dp->dl_stid);     4659                 nfs4_put_stid(&dp->dl_stid);
6201         if (stp)                                 4660         if (stp)
6202                 nfs4_put_stid(&stp->st_stid);    4661                 nfs4_put_stid(&stp->st_stid);
6203                                                  4662 
6204         return status;                           4663         return status;
6205 }                                                4664 }
6206                                                  4665 
6207 void nfsd4_cleanup_open_state(struct nfsd4_co    4666 void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
6208                               struct nfsd4_op    4667                               struct nfsd4_open *open)
6209 {                                                4668 {
6210         if (open->op_openowner)               !! 4669         if (open->op_openowner) {
6211                 nfs4_put_stateowner(&open->op !! 4670                 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
                                                   >> 4671 
                                                   >> 4672                 nfsd4_cstate_assign_replay(cstate, so);
                                                   >> 4673                 nfs4_put_stateowner(so);
                                                   >> 4674         }
6212         if (open->op_file)                       4675         if (open->op_file)
6213                 kmem_cache_free(file_slab, op    4676                 kmem_cache_free(file_slab, open->op_file);
6214         if (open->op_stp)                        4677         if (open->op_stp)
6215                 nfs4_put_stid(&open->op_stp->    4678                 nfs4_put_stid(&open->op_stp->st_stid);
6216         if (open->op_odstate)                    4679         if (open->op_odstate)
6217                 kmem_cache_free(odstate_slab,    4680                 kmem_cache_free(odstate_slab, open->op_odstate);
6218 }                                                4681 }
6219                                                  4682 
6220 __be32                                           4683 __be32
6221 nfsd4_renew(struct svc_rqst *rqstp, struct nf    4684 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6222             union nfsd4_op_u *u)              !! 4685             clientid_t *clid)
6223 {                                                4686 {
6224         clientid_t *clid = &u->renew;         << 
6225         struct nfs4_client *clp;                 4687         struct nfs4_client *clp;
6226         __be32 status;                           4688         __be32 status;
6227         struct nfsd_net *nn = net_generic(SVC    4689         struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6228                                                  4690 
6229         trace_nfsd_clid_renew(clid);          !! 4691         dprintk("process_renew(%08x/%08x): starting\n", 
6230         status = set_client(clid, cstate, nn) !! 4692                         clid->cl_boot, clid->cl_id);
                                                   >> 4693         status = lookup_clientid(clid, cstate, nn);
6231         if (status)                              4694         if (status)
6232                 return status;                !! 4695                 goto out;
6233         clp = cstate->clp;                       4696         clp = cstate->clp;
                                                   >> 4697         status = nfserr_cb_path_down;
6234         if (!list_empty(&clp->cl_delegations)    4698         if (!list_empty(&clp->cl_delegations)
6235                         && clp->cl_cb_state !    4699                         && clp->cl_cb_state != NFSD4_CB_UP)
6236                 return nfserr_cb_path_down;   !! 4700                 goto out;
6237         return nfs_ok;                        !! 4701         status = nfs_ok;
                                                   >> 4702 out:
                                                   >> 4703         return status;
6238 }                                                4704 }
6239                                                  4705 
6240 void                                             4706 void
6241 nfsd4_end_grace(struct nfsd_net *nn)             4707 nfsd4_end_grace(struct nfsd_net *nn)
6242 {                                                4708 {
6243         /* do nothing if grace period already    4709         /* do nothing if grace period already ended */
6244         if (nn->grace_ended)                     4710         if (nn->grace_ended)
6245                 return;                          4711                 return;
6246                                                  4712 
6247         trace_nfsd_grace_complete(nn);        !! 4713         dprintk("NFSD: end of grace period\n");
6248         nn->grace_ended = true;                  4714         nn->grace_ended = true;
6249         /*                                       4715         /*
6250          * If the server goes down again righ    4716          * If the server goes down again right now, an NFSv4
6251          * client will still be allowed to re    4717          * client will still be allowed to reclaim after it comes back up,
6252          * even if it hasn't yet had a chance    4718          * even if it hasn't yet had a chance to reclaim state this time.
6253          *                                       4719          *
6254          */                                      4720          */
6255         nfsd4_record_grace_done(nn);             4721         nfsd4_record_grace_done(nn);
6256         /*                                       4722         /*
6257          * At this point, NFSv4 clients can s    4723          * At this point, NFSv4 clients can still reclaim.  But if the
6258          * server crashes, any that have not     4724          * server crashes, any that have not yet reclaimed will be out
6259          * of luck on the next boot.             4725          * of luck on the next boot.
6260          *                                       4726          *
6261          * (NFSv4.1+ clients are considered t    4727          * (NFSv4.1+ clients are considered to have reclaimed once they
6262          * call RECLAIM_COMPLETE.  NFSv4.0 cl    4728          * call RECLAIM_COMPLETE.  NFSv4.0 clients are considered to
6263          * have reclaimed after their first O    4729          * have reclaimed after their first OPEN.)
6264          */                                      4730          */
6265         locks_end_grace(&nn->nfsd4_manager);     4731         locks_end_grace(&nn->nfsd4_manager);
6266         /*                                       4732         /*
6267          * At this point, and once lockd and/    4733          * At this point, and once lockd and/or any other containers
6268          * exit their grace period, further r    4734          * exit their grace period, further reclaims will fail and
6269          * regular locking can resume.           4735          * regular locking can resume.
6270          */                                      4736          */
6271 }                                                4737 }
6272                                                  4738 
6273 /*                                            !! 4739 static time_t
6274  * If we've waited a lease period but there a !! 4740 nfs4_laundromat(struct nfsd_net *nn)
6275  * reclaim, wait a little longer to give them << 
6276  */                                           << 
6277 static bool clients_still_reclaiming(struct n << 
6278 {                                             << 
6279         time64_t double_grace_period_end = nn << 
6280                                            2  << 
6281                                               << 
6282         if (nn->track_reclaim_completes &&    << 
6283                         atomic_read(&nn->nr_r << 
6284                         nn->reclaim_str_hasht << 
6285                 return false;                 << 
6286         if (!nn->somebody_reclaimed)          << 
6287                 return false;                 << 
6288         nn->somebody_reclaimed = false;       << 
6289         /*                                    << 
6290          * If we've given them *two* lease ti << 
6291          * still not done, give up:           << 
6292          */                                   << 
6293         if (ktime_get_boottime_seconds() > do << 
6294                 return false;                 << 
6295         return true;                          << 
6296 }                                             << 
6297                                               << 
6298 struct laundry_time {                         << 
6299         time64_t cutoff;                      << 
6300         time64_t new_timeo;                   << 
6301 };                                            << 
6302                                               << 
6303 static bool state_expired(struct laundry_time << 
6304 {                                             << 
6305         time64_t time_remaining;              << 
6306                                               << 
6307         if (last_refresh < lt->cutoff)        << 
6308                 return true;                  << 
6309         time_remaining = last_refresh - lt->c << 
6310         lt->new_timeo = min(lt->new_timeo, ti << 
6311         return false;                         << 
6312 }                                             << 
6313                                               << 
6314 #ifdef CONFIG_NFSD_V4_2_INTER_SSC             << 
6315 void nfsd4_ssc_init_umount_work(struct nfsd_n << 
6316 {                                             << 
6317         spin_lock_init(&nn->nfsd_ssc_lock);   << 
6318         INIT_LIST_HEAD(&nn->nfsd_ssc_mount_li << 
6319         init_waitqueue_head(&nn->nfsd_ssc_wai << 
6320 }                                             << 
6321                                               << 
6322 /*                                            << 
6323  * This is called when nfsd is being shutdown << 
6324  * cleanup were done, to destroy the ssc dela << 
6325  */                                           << 
6326 static void nfsd4_ssc_shutdown_umount(struct  << 
6327 {                                             << 
6328         struct nfsd4_ssc_umount_item *ni = NU << 
6329         struct nfsd4_ssc_umount_item *tmp;    << 
6330                                               << 
6331         spin_lock(&nn->nfsd_ssc_lock);        << 
6332         list_for_each_entry_safe(ni, tmp, &nn << 
6333                 list_del(&ni->nsui_list);     << 
6334                 spin_unlock(&nn->nfsd_ssc_loc << 
6335                 mntput(ni->nsui_vfsmount);    << 
6336                 kfree(ni);                    << 
6337                 spin_lock(&nn->nfsd_ssc_lock) << 
6338         }                                     << 
6339         spin_unlock(&nn->nfsd_ssc_lock);      << 
6340 }                                             << 
6341                                               << 
6342 static void nfsd4_ssc_expire_umount(struct nf << 
6343 {                                             << 
6344         bool do_wakeup = false;               << 
6345         struct nfsd4_ssc_umount_item *ni = NU << 
6346         struct nfsd4_ssc_umount_item *tmp;    << 
6347                                               << 
6348         spin_lock(&nn->nfsd_ssc_lock);        << 
6349         list_for_each_entry_safe(ni, tmp, &nn << 
6350                 if (time_after(jiffies, ni->n << 
6351                         if (refcount_read(&ni << 
6352                                 continue;     << 
6353                                               << 
6354                         /* mark being unmount << 
6355                         ni->nsui_busy = true; << 
6356                         spin_unlock(&nn->nfsd << 
6357                         mntput(ni->nsui_vfsmo << 
6358                         spin_lock(&nn->nfsd_s << 
6359                                               << 
6360                         /* waiters need to st << 
6361                         list_del(&ni->nsui_li << 
6362                         kfree(ni);            << 
6363                                               << 
6364                         /* wakeup ssc_connect << 
6365                         do_wakeup = true;     << 
6366                         continue;             << 
6367                 }                             << 
6368                 break;                        << 
6369         }                                     << 
6370         if (do_wakeup)                        << 
6371                 wake_up_all(&nn->nfsd_ssc_wai << 
6372         spin_unlock(&nn->nfsd_ssc_lock);      << 
6373 }                                             << 
6374 #endif                                        << 
6375                                               << 
6376 /* Check if any lock belonging to this lockow << 
6377 static bool                                   << 
6378 nfs4_lockowner_has_blockers(struct nfs4_locko << 
6379 {                                             << 
6380         struct file_lock_context *ctx;        << 
6381         struct nfs4_ol_stateid *stp;          << 
6382         struct nfs4_file *nf;                 << 
6383                                               << 
6384         list_for_each_entry(stp, &lo->lo_owne << 
6385                 nf = stp->st_stid.sc_file;    << 
6386                 ctx = locks_inode_context(nf- << 
6387                 if (!ctx)                     << 
6388                         continue;             << 
6389                 if (locks_owner_has_blockers( << 
6390                         return true;          << 
6391         }                                     << 
6392         return false;                         << 
6393 }                                             << 
6394                                               << 
6395 static bool                                   << 
6396 nfs4_anylock_blockers(struct nfs4_client *clp << 
6397 {                                             << 
6398         int i;                                << 
6399         struct nfs4_stateowner *so;           << 
6400         struct nfs4_lockowner *lo;            << 
6401                                               << 
6402         if (atomic_read(&clp->cl_delegs_in_re << 
6403                 return true;                  << 
6404         spin_lock(&clp->cl_lock);             << 
6405         for (i = 0; i < OWNER_HASH_SIZE; i++) << 
6406                 list_for_each_entry(so, &clp- << 
6407                                 so_strhash) { << 
6408                         if (so->so_is_open_ow << 
6409                                 continue;     << 
6410                         lo = lockowner(so);   << 
6411                         if (nfs4_lockowner_ha << 
6412                                 spin_unlock(& << 
6413                                 return true;  << 
6414                         }                     << 
6415                 }                             << 
6416         }                                     << 
6417         spin_unlock(&clp->cl_lock);           << 
6418         return false;                         << 
6419 }                                             << 
6420                                               << 
6421 static void                                   << 
6422 nfs4_get_client_reaplist(struct nfsd_net *nn, << 
6423                                 struct laundr << 
6424 {                                                4741 {
6425         unsigned int maxreap, reapcnt = 0;    << 
6426         struct list_head *pos, *next;         << 
6427         struct nfs4_client *clp;                 4742         struct nfs4_client *clp;
                                                   >> 4743         struct nfs4_openowner *oo;
                                                   >> 4744         struct nfs4_delegation *dp;
                                                   >> 4745         struct nfs4_ol_stateid *stp;
                                                   >> 4746         struct nfsd4_blocked_lock *nbl;
                                                   >> 4747         struct list_head *pos, *next, reaplist;
                                                   >> 4748         time_t cutoff = get_seconds() - nn->nfsd4_lease;
                                                   >> 4749         time_t t, new_timeo = nn->nfsd4_lease;
6428                                                  4750 
6429         maxreap = (atomic_read(&nn->nfs4_clie !! 4751         dprintk("NFSD: laundromat service - starting\n");
6430                         NFSD_CLIENT_MAX_TRIM_ !! 4752         nfsd4_end_grace(nn);
6431         INIT_LIST_HEAD(reaplist);             !! 4753         INIT_LIST_HEAD(&reaplist);
6432         spin_lock(&nn->client_lock);             4754         spin_lock(&nn->client_lock);
6433         list_for_each_safe(pos, next, &nn->cl    4755         list_for_each_safe(pos, next, &nn->client_lru) {
6434                 clp = list_entry(pos, struct     4756                 clp = list_entry(pos, struct nfs4_client, cl_lru);
6435                 if (clp->cl_state == NFSD4_EX !! 4757                 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
6436                         goto exp_client;      !! 4758                         t = clp->cl_time - cutoff;
6437                 if (!state_expired(lt, clp->c !! 4759                         new_timeo = min(new_timeo, t);
6438                         break;                   4760                         break;
6439                 if (!atomic_read(&clp->cl_rpc << 
6440                         if (clp->cl_state ==  << 
6441                                 atomic_inc(&n << 
6442                         clp->cl_state = NFSD4 << 
6443                 }                             << 
6444                 if (!client_has_state(clp))   << 
6445                         goto exp_client;      << 
6446                 if (!nfs4_anylock_blockers(cl << 
6447                         if (reapcnt >= maxrea << 
6448                                 continue;     << 
6449 exp_client:                                   << 
6450                 if (!mark_client_expired_lock << 
6451                         list_add(&clp->cl_lru << 
6452                         reapcnt++;            << 
6453                 }                                4761                 }
6454         }                                     !! 4762                 if (mark_client_expired_locked(clp)) {
6455         spin_unlock(&nn->client_lock);        !! 4763                         dprintk("NFSD: client in use (clientid %08x)\n",
6456 }                                             !! 4764                                 clp->cl_clientid.cl_id);
6457                                               !! 4765                         continue;
6458 static void                                   << 
6459 nfs4_get_courtesy_client_reaplist(struct nfsd << 
6460                                 struct list_h << 
6461 {                                             << 
6462         unsigned int maxreap = 0, reapcnt = 0 << 
6463         struct list_head *pos, *next;         << 
6464         struct nfs4_client *clp;              << 
6465                                               << 
6466         maxreap = NFSD_CLIENT_MAX_TRIM_PER_RU << 
6467         INIT_LIST_HEAD(reaplist);             << 
6468                                               << 
6469         spin_lock(&nn->client_lock);          << 
6470         list_for_each_safe(pos, next, &nn->cl << 
6471                 clp = list_entry(pos, struct  << 
6472                 if (clp->cl_state == NFSD4_AC << 
6473                         break;                << 
6474                 if (reapcnt >= maxreap)       << 
6475                         break;                << 
6476                 if (!mark_client_expired_lock << 
6477                         list_add(&clp->cl_lru << 
6478                         reapcnt++;            << 
6479                 }                                4766                 }
                                                   >> 4767                 list_add(&clp->cl_lru, &reaplist);
6480         }                                        4768         }
6481         spin_unlock(&nn->client_lock);           4769         spin_unlock(&nn->client_lock);
6482 }                                             !! 4770         list_for_each_safe(pos, next, &reaplist) {
6483                                               << 
6484 static void                                   << 
6485 nfs4_process_client_reaplist(struct list_head << 
6486 {                                             << 
6487         struct list_head *pos, *next;         << 
6488         struct nfs4_client *clp;              << 
6489                                               << 
6490         list_for_each_safe(pos, next, reaplis << 
6491                 clp = list_entry(pos, struct     4771                 clp = list_entry(pos, struct nfs4_client, cl_lru);
6492                 trace_nfsd_clid_purged(&clp-> !! 4772                 dprintk("NFSD: purging unused client (clientid %08x)\n",
                                                   >> 4773                         clp->cl_clientid.cl_id);
6493                 list_del_init(&clp->cl_lru);     4774                 list_del_init(&clp->cl_lru);
6494                 expire_client(clp);              4775                 expire_client(clp);
6495         }                                        4776         }
6496 }                                             << 
6497                                               << 
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                               << 
6536 nfs4_laundromat(struct nfsd_net *nn)          << 
6537 {                                             << 
6538         struct nfs4_openowner *oo;            << 
6539         struct nfs4_delegation *dp;           << 
6540         struct nfs4_ol_stateid *stp;          << 
6541         struct nfsd4_blocked_lock *nbl;       << 
6542         struct list_head *pos, *next, reaplis << 
6543         struct laundry_time lt = {            << 
6544                 .cutoff = ktime_get_boottime_ << 
6545                 .new_timeo = nn->nfsd4_lease  << 
6546         };                                    << 
6547         struct nfs4_cpntf_state *cps;         << 
6548         copy_stateid_t *cps_t;                << 
6549         int i;                                << 
6550                                               << 
6551         if (clients_still_reclaiming(nn)) {   << 
6552                 lt.new_timeo = 0;             << 
6553                 goto out;                     << 
6554         }                                     << 
6555         nfsd4_end_grace(nn);                  << 
6556                                               << 
6557         spin_lock(&nn->s2s_cp_lock);          << 
6558         idr_for_each_entry(&nn->s2s_cp_statei << 
6559                 cps = container_of(cps_t, str << 
6560                 if (cps->cp_stateid.cs_type = << 
6561                                 state_expired << 
6562                         _free_cpntf_state_loc << 
6563         }                                     << 
6564         spin_unlock(&nn->s2s_cp_lock);        << 
6565         nfs4_get_client_reaplist(nn, &reaplis << 
6566         nfs4_process_client_reaplist(&reaplis << 
6567                                               << 
6568         nfs40_clean_admin_revoked(nn, &lt);   << 
6569                                               << 
6570         spin_lock(&state_lock);                  4777         spin_lock(&state_lock);
6571         list_for_each_safe(pos, next, &nn->de    4778         list_for_each_safe(pos, next, &nn->del_recall_lru) {
6572                 dp = list_entry (pos, struct     4779                 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
6573                 if (!state_expired(&lt, dp->d !! 4780                 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
                                                   >> 4781                         t = dp->dl_time - cutoff;
                                                   >> 4782                         new_timeo = min(new_timeo, t);
6574                         break;                   4783                         break;
6575                 refcount_inc(&dp->dl_stid.sc_ !! 4784                 }
6576                 unhash_delegation_locked(dp,  !! 4785                 WARN_ON(!unhash_delegation_locked(dp));
6577                 list_add(&dp->dl_recall_lru,     4786                 list_add(&dp->dl_recall_lru, &reaplist);
6578         }                                        4787         }
6579         spin_unlock(&state_lock);                4788         spin_unlock(&state_lock);
6580         while (!list_empty(&reaplist)) {         4789         while (!list_empty(&reaplist)) {
6581                 dp = list_first_entry(&reapli    4790                 dp = list_first_entry(&reaplist, struct nfs4_delegation,
6582                                         dl_re    4791                                         dl_recall_lru);
6583                 list_del_init(&dp->dl_recall_    4792                 list_del_init(&dp->dl_recall_lru);
6584                 revoke_delegation(dp);           4793                 revoke_delegation(dp);
6585         }                                        4794         }
6586                                                  4795 
6587         spin_lock(&nn->client_lock);             4796         spin_lock(&nn->client_lock);
6588         while (!list_empty(&nn->close_lru)) {    4797         while (!list_empty(&nn->close_lru)) {
6589                 oo = list_first_entry(&nn->cl    4798                 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
6590                                         oo_cl    4799                                         oo_close_lru);
6591                 if (!state_expired(&lt, oo->o !! 4800                 if (time_after((unsigned long)oo->oo_time,
                                                   >> 4801                                (unsigned long)cutoff)) {
                                                   >> 4802                         t = oo->oo_time - cutoff;
                                                   >> 4803                         new_timeo = min(new_timeo, t);
6592                         break;                   4804                         break;
                                                   >> 4805                 }
6593                 list_del_init(&oo->oo_close_l    4806                 list_del_init(&oo->oo_close_lru);
6594                 stp = oo->oo_last_closed_stid    4807                 stp = oo->oo_last_closed_stid;
6595                 oo->oo_last_closed_stid = NUL    4808                 oo->oo_last_closed_stid = NULL;
6596                 spin_unlock(&nn->client_lock)    4809                 spin_unlock(&nn->client_lock);
6597                 nfs4_put_stid(&stp->st_stid);    4810                 nfs4_put_stid(&stp->st_stid);
6598                 spin_lock(&nn->client_lock);     4811                 spin_lock(&nn->client_lock);
6599         }                                        4812         }
6600         spin_unlock(&nn->client_lock);           4813         spin_unlock(&nn->client_lock);
6601                                                  4814 
6602         /*                                       4815         /*
6603          * It's possible for a client to try     4816          * It's possible for a client to try and acquire an already held lock
6604          * that is being held for a long time    4817          * that is being held for a long time, and then lose interest in it.
6605          * So, we clean out any un-revisited     4818          * So, we clean out any un-revisited request after a lease period
6606          * under the assumption that the clie    4819          * under the assumption that the client is no longer interested.
6607          *                                       4820          *
6608          * RFC5661, sec. 9.6 states that the     4821          * RFC5661, sec. 9.6 states that the client must not rely on getting
6609          * notifications and must continue to    4822          * notifications and must continue to poll for locks, even when the
6610          * server supports them. Thus this sh    4823          * server supports them. Thus this shouldn't lead to clients blocking
6611          * indefinitely once the lock does be    4824          * indefinitely once the lock does become free.
6612          */                                      4825          */
6613         BUG_ON(!list_empty(&reaplist));          4826         BUG_ON(!list_empty(&reaplist));
6614         spin_lock(&nn->blocked_locks_lock);      4827         spin_lock(&nn->blocked_locks_lock);
6615         while (!list_empty(&nn->blocked_locks    4828         while (!list_empty(&nn->blocked_locks_lru)) {
6616                 nbl = list_first_entry(&nn->b    4829                 nbl = list_first_entry(&nn->blocked_locks_lru,
6617                                         struc    4830                                         struct nfsd4_blocked_lock, nbl_lru);
6618                 if (!state_expired(&lt, nbl-> !! 4831                 if (time_after((unsigned long)nbl->nbl_time,
                                                   >> 4832                                (unsigned long)cutoff)) {
                                                   >> 4833                         t = nbl->nbl_time - cutoff;
                                                   >> 4834                         new_timeo = min(new_timeo, t);
6619                         break;                   4835                         break;
                                                   >> 4836                 }
6620                 list_move(&nbl->nbl_lru, &rea    4837                 list_move(&nbl->nbl_lru, &reaplist);
6621                 list_del_init(&nbl->nbl_list)    4838                 list_del_init(&nbl->nbl_list);
6622         }                                        4839         }
6623         spin_unlock(&nn->blocked_locks_lock);    4840         spin_unlock(&nn->blocked_locks_lock);
6624                                                  4841 
6625         while (!list_empty(&reaplist)) {         4842         while (!list_empty(&reaplist)) {
6626                 nbl = list_first_entry(&reapl    4843                 nbl = list_first_entry(&reaplist,
6627                                         struc    4844                                         struct nfsd4_blocked_lock, nbl_lru);
6628                 list_del_init(&nbl->nbl_lru);    4845                 list_del_init(&nbl->nbl_lru);
                                                   >> 4846                 posix_unblock_lock(&nbl->nbl_lock);
6629                 free_blocked_lock(nbl);          4847                 free_blocked_lock(nbl);
6630         }                                        4848         }
6631 #ifdef CONFIG_NFSD_V4_2_INTER_SSC             !! 4849 
6632         /* service the server-to-server copy  !! 4850         new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
6633         nfsd4_ssc_expire_umount(nn);          !! 4851         return new_timeo;
6634 #endif                                        << 
6635         if (atomic_long_read(&num_delegations << 
6636                 deleg_reaper(nn);             << 
6637 out:                                          << 
6638         return max_t(time64_t, lt.new_timeo,  << 
6639 }                                                4852 }
6640                                                  4853 
                                                   >> 4854 static struct workqueue_struct *laundry_wq;
6641 static void laundromat_main(struct work_struc    4855 static void laundromat_main(struct work_struct *);
6642                                                  4856 
6643 static void                                      4857 static void
6644 laundromat_main(struct work_struct *laundry)     4858 laundromat_main(struct work_struct *laundry)
6645 {                                                4859 {
6646         time64_t t;                           !! 4860         time_t t;
6647         struct delayed_work *dwork = to_delay    4861         struct delayed_work *dwork = to_delayed_work(laundry);
6648         struct nfsd_net *nn = container_of(dw    4862         struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
6649                                            la    4863                                            laundromat_work);
6650                                                  4864 
6651         t = nfs4_laundromat(nn);                 4865         t = nfs4_laundromat(nn);
                                                   >> 4866         dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
6652         queue_delayed_work(laundry_wq, &nn->l    4867         queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
6653 }                                                4868 }
6654                                                  4869 
6655 static void                                   !! 4870 static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
6656 courtesy_client_reaper(struct nfsd_net *nn)   << 
6657 {                                             << 
6658         struct list_head reaplist;            << 
6659                                               << 
6660         nfs4_get_courtesy_client_reaplist(nn, << 
6661         nfs4_process_client_reaplist(&reaplis << 
6662 }                                             << 
6663                                               << 
6664 static void                                   << 
6665 deleg_reaper(struct nfsd_net *nn)             << 
6666 {                                                4871 {
6667         struct list_head *pos, *next;         !! 4872         if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
6668         struct nfs4_client *clp;              !! 4873                 return nfserr_bad_stateid;
6669         LIST_HEAD(cblist);                    !! 4874         return nfs_ok;
6670                                               << 
6671         spin_lock(&nn->client_lock);          << 
6672         list_for_each_safe(pos, next, &nn->cl << 
6673                 clp = list_entry(pos, struct  << 
6674                 if (clp->cl_state != NFSD4_AC << 
6675                         list_empty(&clp->cl_d << 
6676                         atomic_read(&clp->cl_ << 
6677                         test_bit(NFSD4_CLIENT << 
6678                         (ktime_get_boottime_s << 
6679                                 clp->cl_ra_ti << 
6680                         continue;             << 
6681                 }                             << 
6682                 list_add(&clp->cl_ra_cblist,  << 
6683                                               << 
6684                 /* release in nfsd4_cb_recall << 
6685                 kref_get(&clp->cl_nfsdfs.cl_r << 
6686                 set_bit(NFSD4_CLIENT_CB_RECAL << 
6687                 clp->cl_ra_time = ktime_get_b << 
6688         }                                     << 
6689         spin_unlock(&nn->client_lock);        << 
6690                                               << 
6691         while (!list_empty(&cblist)) {        << 
6692                 clp = list_first_entry(&cblis << 
6693                                         cl_ra << 
6694                 list_del_init(&clp->cl_ra_cbl << 
6695                 clp->cl_ra->ra_keep = 0;      << 
6696                 clp->cl_ra->ra_bmval[0] = BIT << 
6697                                               << 
6698                 trace_nfsd_cb_recall_any(clp- << 
6699                 nfsd4_run_cb(&clp->cl_ra->ra_ << 
6700         }                                     << 
6701 }                                                4875 }
6702                                                  4876 
6703 static void                                   !! 4877 static inline int
6704 nfsd4_state_shrinker_worker(struct work_struc !! 4878 access_permit_read(struct nfs4_ol_stateid *stp)
6705 {                                                4879 {
6706         struct nfsd_net *nn = container_of(wo !! 4880         return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
6707                                 nfsd_shrinker !! 4881                 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
6708                                               !! 4882                 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
6709         courtesy_client_reaper(nn);           << 
6710         deleg_reaper(nn);                     << 
6711 }                                                4883 }
6712                                                  4884 
6713 static inline __be32 nfs4_check_fh(struct svc !! 4885 static inline int
                                                   >> 4886 access_permit_write(struct nfs4_ol_stateid *stp)
6714 {                                                4887 {
6715         if (!fh_match(&fhp->fh_handle, &stp-> !! 4888         return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
6716                 return nfserr_bad_stateid;    !! 4889                 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
6717         return nfs_ok;                        << 
6718 }                                                4890 }
6719                                                  4891 
6720 static                                           4892 static
6721 __be32 nfs4_check_openmode(struct nfs4_ol_sta    4893 __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
6722 {                                                4894 {
6723         __be32 status = nfserr_openmode;         4895         __be32 status = nfserr_openmode;
6724                                                  4896 
6725         /* For lock stateid's, we test the pa    4897         /* For lock stateid's, we test the parent open, not the lock: */
6726         if (stp->st_openstp)                     4898         if (stp->st_openstp)
6727                 stp = stp->st_openstp;           4899                 stp = stp->st_openstp;
6728         if ((flags & WR_STATE) && !access_per    4900         if ((flags & WR_STATE) && !access_permit_write(stp))
6729                 goto out;                        4901                 goto out;
6730         if ((flags & RD_STATE) && !access_per    4902         if ((flags & RD_STATE) && !access_permit_read(stp))
6731                 goto out;                        4903                 goto out;
6732         status = nfs_ok;                         4904         status = nfs_ok;
6733 out:                                             4905 out:
6734         return status;                           4906         return status;
6735 }                                                4907 }
6736                                                  4908 
6737 static inline __be32                             4909 static inline __be32
6738 check_special_stateids(struct net *net, svc_f    4910 check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
6739 {                                                4911 {
6740         if (ONE_STATEID(stateid) && (flags &     4912         if (ONE_STATEID(stateid) && (flags & RD_STATE))
6741                 return nfs_ok;                   4913                 return nfs_ok;
6742         else if (opens_in_grace(net)) {          4914         else if (opens_in_grace(net)) {
6743                 /* Answer in remaining cases     4915                 /* Answer in remaining cases depends on existence of
6744                  * conflicting state; so we m    4916                  * conflicting state; so we must wait out the grace period. */
6745                 return nfserr_grace;             4917                 return nfserr_grace;
6746         } else if (flags & WR_STATE)             4918         } else if (flags & WR_STATE)
6747                 return nfs4_share_conflict(cu    4919                 return nfs4_share_conflict(current_fh,
6748                                 NFS4_SHARE_DE    4920                                 NFS4_SHARE_DENY_WRITE);
6749         else /* (flags & RD_STATE) && ZERO_ST    4921         else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
6750                 return nfs4_share_conflict(cu    4922                 return nfs4_share_conflict(current_fh,
6751                                 NFS4_SHARE_DE    4923                                 NFS4_SHARE_DENY_READ);
6752 }                                                4924 }
6753                                                  4925 
                                                   >> 4926 /*
                                                   >> 4927  * Allow READ/WRITE during grace period on recovered state only for files
                                                   >> 4928  * that are not able to provide mandatory locking.
                                                   >> 4929  */
                                                   >> 4930 static inline int
                                                   >> 4931 grace_disallows_io(struct net *net, struct inode *inode)
                                                   >> 4932 {
                                                   >> 4933         return opens_in_grace(net) && mandatory_lock(inode);
                                                   >> 4934 }
                                                   >> 4935 
6754 static __be32 check_stateid_generation(statei    4936 static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
6755 {                                                4937 {
6756         /*                                       4938         /*
6757          * When sessions are used the stateid    4939          * When sessions are used the stateid generation number is ignored
6758          * when it is zero.                      4940          * when it is zero.
6759          */                                      4941          */
6760         if (has_session && in->si_generation     4942         if (has_session && in->si_generation == 0)
6761                 return nfs_ok;                   4943                 return nfs_ok;
6762                                                  4944 
6763         if (in->si_generation == ref->si_gene    4945         if (in->si_generation == ref->si_generation)
6764                 return nfs_ok;                   4946                 return nfs_ok;
6765                                                  4947 
6766         /* If the client sends us a stateid f    4948         /* If the client sends us a stateid from the future, it's buggy: */
6767         if (nfsd4_stateid_generation_after(in    4949         if (nfsd4_stateid_generation_after(in, ref))
6768                 return nfserr_bad_stateid;       4950                 return nfserr_bad_stateid;
6769         /*                                       4951         /*
6770          * However, we could see a stateid fr    4952          * However, we could see a stateid from the past, even from a
6771          * non-buggy client.  For example, if    4953          * non-buggy client.  For example, if the client sends a lock
6772          * while some IO is outstanding, the     4954          * while some IO is outstanding, the lock may bump si_generation
6773          * while the IO is still in flight.      4955          * while the IO is still in flight.  The client could avoid that
6774          * situation by waiting for responses    4956          * situation by waiting for responses on all the IO requests,
6775          * but better performance may result     4957          * but better performance may result in retrying IO that
6776          * receives an old_stateid error if r    4958          * receives an old_stateid error if requests are rarely
6777          * reordered in flight:                  4959          * reordered in flight:
6778          */                                      4960          */
6779         return nfserr_old_stateid;               4961         return nfserr_old_stateid;
6780 }                                                4962 }
6781                                                  4963 
6782 static __be32 nfsd4_stid_check_stateid_genera << 
6783 {                                             << 
6784         __be32 ret;                           << 
6785                                               << 
6786         spin_lock(&s->sc_lock);               << 
6787         ret = nfsd4_verify_open_stid(s);      << 
6788         if (ret == nfs_ok)                    << 
6789                 ret = check_stateid_generatio << 
6790         spin_unlock(&s->sc_lock);             << 
6791         if (ret == nfserr_admin_revoked)      << 
6792                 nfsd40_drop_revoked_stid(s->s << 
6793                                         &s->s << 
6794         return ret;                           << 
6795 }                                             << 
6796                                               << 
6797 static __be32 nfsd4_check_openowner_confirmed    4964 static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
6798 {                                                4965 {
6799         if (ols->st_stateowner->so_is_open_ow    4966         if (ols->st_stateowner->so_is_open_owner &&
6800             !(openowner(ols->st_stateowner)->    4967             !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
6801                 return nfserr_bad_stateid;       4968                 return nfserr_bad_stateid;
6802         return nfs_ok;                           4969         return nfs_ok;
6803 }                                                4970 }
6804                                                  4971 
6805 static __be32 nfsd4_validate_stateid(struct n    4972 static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
6806 {                                                4973 {
6807         struct nfs4_stid *s;                     4974         struct nfs4_stid *s;
6808         __be32 status = nfserr_bad_stateid;      4975         __be32 status = nfserr_bad_stateid;
6809                                                  4976 
6810         if (ZERO_STATEID(stateid) || ONE_STAT    4977         if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
6811                 CLOSE_STATEID(stateid))          4978                 CLOSE_STATEID(stateid))
6812                 return status;                   4979                 return status;
                                                   >> 4980         /* Client debugging aid. */
                                                   >> 4981         if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
                                                   >> 4982                 char addr_str[INET6_ADDRSTRLEN];
                                                   >> 4983                 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
                                                   >> 4984                                  sizeof(addr_str));
                                                   >> 4985                 pr_warn_ratelimited("NFSD: client %s testing state ID "
                                                   >> 4986                                         "with incorrect client ID\n", addr_str);
                                                   >> 4987                 return status;
                                                   >> 4988         }
6813         spin_lock(&cl->cl_lock);                 4989         spin_lock(&cl->cl_lock);
6814         s = find_stateid_locked(cl, stateid);    4990         s = find_stateid_locked(cl, stateid);
6815         if (!s)                                  4991         if (!s)
6816                 goto out_unlock;                 4992                 goto out_unlock;
6817         status = nfsd4_stid_check_stateid_gen !! 4993         status = check_stateid_generation(stateid, &s->sc_stateid, 1);
6818         if (status)                           << 
6819                 goto out_unlock;              << 
6820         status = nfsd4_verify_open_stid(s);   << 
6821         if (status)                              4994         if (status)
6822                 goto out_unlock;                 4995                 goto out_unlock;
6823                                               << 
6824         switch (s->sc_type) {                    4996         switch (s->sc_type) {
6825         case SC_TYPE_DELEG:                   !! 4997         case NFS4_DELEG_STID:
6826                 status = nfs_ok;                 4998                 status = nfs_ok;
6827                 break;                           4999                 break;
6828         case SC_TYPE_OPEN:                    !! 5000         case NFS4_REVOKED_DELEG_STID:
6829         case SC_TYPE_LOCK:                    !! 5001                 status = nfserr_deleg_revoked;
                                                   >> 5002                 break;
                                                   >> 5003         case NFS4_OPEN_STID:
                                                   >> 5004         case NFS4_LOCK_STID:
6830                 status = nfsd4_check_openowne    5005                 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
6831                 break;                           5006                 break;
6832         default:                                 5007         default:
6833                 printk("unknown stateid type     5008                 printk("unknown stateid type %x\n", s->sc_type);
                                                   >> 5009                 /* Fallthrough */
                                                   >> 5010         case NFS4_CLOSED_STID:
                                                   >> 5011         case NFS4_CLOSED_DELEG_STID:
6834                 status = nfserr_bad_stateid;     5012                 status = nfserr_bad_stateid;
6835         }                                        5013         }
6836 out_unlock:                                      5014 out_unlock:
6837         spin_unlock(&cl->cl_lock);               5015         spin_unlock(&cl->cl_lock);
6838         if (status == nfserr_admin_revoked)   << 
6839                 nfsd40_drop_revoked_stid(cl,  << 
6840         return status;                           5016         return status;
6841 }                                                5017 }
6842                                                  5018 
6843 __be32                                           5019 __be32
6844 nfsd4_lookup_stateid(struct nfsd4_compound_st    5020 nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
6845                      stateid_t *stateid,      !! 5021                      stateid_t *stateid, unsigned char typemask,
6846                      unsigned short typemask, << 
6847                      struct nfs4_stid **s, st    5022                      struct nfs4_stid **s, struct nfsd_net *nn)
6848 {                                                5023 {
6849         __be32 status;                           5024         __be32 status;
6850         struct nfs4_stid *stid;               << 
6851         bool return_revoked = false;             5025         bool return_revoked = false;
6852                                                  5026 
6853         /*                                       5027         /*
6854          *  only return revoked delegations i    5028          *  only return revoked delegations if explicitly asked.
6855          *  otherwise we report revoked or ba    5029          *  otherwise we report revoked or bad_stateid status.
6856          */                                      5030          */
6857         if (statusmask & SC_STATUS_REVOKED)   !! 5031         if (typemask & NFS4_REVOKED_DELEG_STID)
6858                 return_revoked = true;           5032                 return_revoked = true;
6859         if (typemask & SC_TYPE_DELEG)         !! 5033         else if (typemask & NFS4_DELEG_STID)
6860                 /* Always allow REVOKED for D !! 5034                 typemask |= NFS4_REVOKED_DELEG_STID;
6861                  * retturn the appropriate er << 
6862                  */                           << 
6863                 statusmask |= SC_STATUS_REVOK << 
6864                                               << 
6865         statusmask |= SC_STATUS_ADMIN_REVOKED << 
6866                                                  5035 
6867         if (ZERO_STATEID(stateid) || ONE_STAT    5036         if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
6868                 CLOSE_STATEID(stateid))          5037                 CLOSE_STATEID(stateid))
6869                 return nfserr_bad_stateid;       5038                 return nfserr_bad_stateid;
6870         status = set_client(&stateid->si_opaq !! 5039         status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
6871         if (status == nfserr_stale_clientid)     5040         if (status == nfserr_stale_clientid) {
6872                 if (cstate->session)             5041                 if (cstate->session)
6873                         return nfserr_bad_sta    5042                         return nfserr_bad_stateid;
6874                 return nfserr_stale_stateid;     5043                 return nfserr_stale_stateid;
6875         }                                        5044         }
6876         if (status)                              5045         if (status)
6877                 return status;                   5046                 return status;
6878         stid = find_stateid_by_type(cstate->c !! 5047         *s = find_stateid_by_type(cstate->clp, stateid, typemask);
6879         if (!stid)                            !! 5048         if (!*s)
                                                   >> 5049                 return nfserr_bad_stateid;
                                                   >> 5050         if (((*s)->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) {
                                                   >> 5051                 nfs4_put_stid(*s);
                                                   >> 5052                 if (cstate->minorversion)
                                                   >> 5053                         return nfserr_deleg_revoked;
6880                 return nfserr_bad_stateid;       5054                 return nfserr_bad_stateid;
6881         if ((stid->sc_status & SC_STATUS_REVO << 
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);          << 
6888                 return nfserr_admin_revoked;  << 
6889         }                                        5055         }
6890         *s = stid;                            << 
6891         return nfs_ok;                           5056         return nfs_ok;
6892 }                                                5057 }
6893                                                  5058 
6894 static struct nfsd_file *                     !! 5059 static struct file *
6895 nfs4_find_file(struct nfs4_stid *s, int flags    5060 nfs4_find_file(struct nfs4_stid *s, int flags)
6896 {                                                5061 {
6897         struct nfsd_file *ret = NULL;         !! 5062         if (!s)
6898                                               << 
6899         if (!s || s->sc_status)               << 
6900                 return NULL;                     5063                 return NULL;
6901                                                  5064 
6902         switch (s->sc_type) {                    5065         switch (s->sc_type) {
6903         case SC_TYPE_DELEG:                   !! 5066         case NFS4_DELEG_STID:
6904                 spin_lock(&s->sc_file->fi_loc !! 5067                 if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
6905                 ret = nfsd_file_get(s->sc_fil !! 5068                         return NULL;
6906                 spin_unlock(&s->sc_file->fi_l !! 5069                 return get_file(s->sc_file->fi_deleg_file);
6907                 break;                        !! 5070         case NFS4_OPEN_STID:
6908         case SC_TYPE_OPEN:                    !! 5071         case NFS4_LOCK_STID:
6909         case SC_TYPE_LOCK:                    << 
6910                 if (flags & RD_STATE)            5072                 if (flags & RD_STATE)
6911                         ret = find_readable_f !! 5073                         return find_readable_file(s->sc_file);
6912                 else                             5074                 else
6913                         ret = find_writeable_ !! 5075                         return find_writeable_file(s->sc_file);
                                                   >> 5076                 break;
6914         }                                        5077         }
6915                                                  5078 
6916         return ret;                           !! 5079         return NULL;
6917 }                                                5080 }
6918                                                  5081 
6919 static __be32                                    5082 static __be32
6920 nfs4_check_olstateid(struct nfs4_ol_stateid * !! 5083 nfs4_check_olstateid(struct svc_fh *fhp, struct nfs4_ol_stateid *ols, int flags)
6921 {                                                5084 {
6922         __be32 status;                           5085         __be32 status;
6923                                                  5086 
6924         status = nfsd4_check_openowner_confir    5087         status = nfsd4_check_openowner_confirmed(ols);
6925         if (status)                              5088         if (status)
6926                 return status;                   5089                 return status;
6927         return nfs4_check_openmode(ols, flags    5090         return nfs4_check_openmode(ols, flags);
6928 }                                                5091 }
6929                                                  5092 
6930 static __be32                                    5093 static __be32
6931 nfs4_check_file(struct svc_rqst *rqstp, struc    5094 nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
6932                 struct nfsd_file **nfp, int f !! 5095                 struct file **filpp, bool *tmp_file, int flags)
6933 {                                                5096 {
6934         int acc = (flags & RD_STATE) ? NFSD_M    5097         int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
6935         struct nfsd_file *nf;                 !! 5098         struct file *file;
6936         __be32 status;                           5099         __be32 status;
6937                                                  5100 
6938         nf = nfs4_find_file(s, flags);        !! 5101         file = nfs4_find_file(s, flags);
6939         if (nf) {                             !! 5102         if (file) {
6940                 status = nfsd_permission(&rqs !! 5103                 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
6941                                          fhp- << 
6942                                 acc | NFSD_MA    5104                                 acc | NFSD_MAY_OWNER_OVERRIDE);
6943                 if (status) {                    5105                 if (status) {
6944                         nfsd_file_put(nf);    !! 5106                         fput(file);
6945                         goto out;             !! 5107                         return status;
6946                 }                                5108                 }
                                                   >> 5109 
                                                   >> 5110                 *filpp = file;
6947         } else {                                 5111         } else {
6948                 status = nfsd_file_acquire(rq !! 5112                 status = nfsd_open(rqstp, fhp, S_IFREG, acc, filpp);
6949                 if (status)                      5113                 if (status)
6950                         return status;           5114                         return status;
6951         }                                     << 
6952         *nfp = nf;                            << 
6953 out:                                          << 
6954         return status;                        << 
6955 }                                             << 
6956 static void                                   << 
6957 _free_cpntf_state_locked(struct nfsd_net *nn, << 
6958 {                                             << 
6959         WARN_ON_ONCE(cps->cp_stateid.cs_type  << 
6960         if (!refcount_dec_and_test(&cps->cp_s << 
6961                 return;                       << 
6962         list_del(&cps->cp_list);              << 
6963         idr_remove(&nn->s2s_cp_stateids,      << 
6964                    cps->cp_stateid.cs_stid.si << 
6965         kfree(cps);                           << 
6966 }                                             << 
6967 /*                                            << 
6968  * A READ from an inter server to server COPY << 
6969  * copy stateid. Look up the copy notify stat << 
6970  * idr structure and take a reference on it.  << 
6971  */                                           << 
6972 __be32 manage_cpntf_state(struct nfsd_net *nn << 
6973                           struct nfs4_client  << 
6974                           struct nfs4_cpntf_s << 
6975 {                                             << 
6976         copy_stateid_t *cps_t;                << 
6977         struct nfs4_cpntf_state *state = NULL << 
6978                                                  5115 
6979         if (st->si_opaque.so_clid.cl_id != nn !! 5116                 if (tmp_file)
6980                 return nfserr_bad_stateid;    !! 5117                         *tmp_file = true;
6981         spin_lock(&nn->s2s_cp_lock);          << 
6982         cps_t = idr_find(&nn->s2s_cp_stateids << 
6983         if (cps_t) {                          << 
6984                 state = container_of(cps_t, s << 
6985                                      cp_state << 
6986                 if (state->cp_stateid.cs_type << 
6987                         state = NULL;         << 
6988                         goto unlock;          << 
6989                 }                             << 
6990                 if (!clp)                     << 
6991                         refcount_inc(&state-> << 
6992                 else                          << 
6993                         _free_cpntf_state_loc << 
6994         }                                        5118         }
6995 unlock:                                       << 
6996         spin_unlock(&nn->s2s_cp_lock);        << 
6997         if (!state)                           << 
6998                 return nfserr_bad_stateid;    << 
6999         if (!clp)                             << 
7000                 *cps = state;                 << 
7001         return 0;                             << 
7002 }                                             << 
7003                                               << 
7004 static __be32 find_cpntf_state(struct nfsd_ne << 
7005                                struct nfs4_st << 
7006 {                                             << 
7007         __be32 status;                        << 
7008         struct nfs4_cpntf_state *cps = NULL;  << 
7009         struct nfs4_client *found;            << 
7010                                               << 
7011         status = manage_cpntf_state(nn, st, N << 
7012         if (status)                           << 
7013                 return status;                << 
7014                                               << 
7015         cps->cpntf_time = ktime_get_boottime_ << 
7016                                               << 
7017         status = nfserr_expired;              << 
7018         found = lookup_clientid(&cps->cp_p_cl << 
7019         if (!found)                           << 
7020                 goto out;                     << 
7021                                               << 
7022         *stid = find_stateid_by_type(found, & << 
7023                                      SC_TYPE_ << 
7024                                      0);      << 
7025         if (*stid)                            << 
7026                 status = nfs_ok;              << 
7027         else                                  << 
7028                 status = nfserr_bad_stateid;  << 
7029                                               << 
7030         put_client_renew(found);              << 
7031 out:                                          << 
7032         nfs4_put_cpntf_state(nn, cps);        << 
7033         return status;                        << 
7034 }                                             << 
7035                                                  5119 
7036 void nfs4_put_cpntf_state(struct nfsd_net *nn !! 5120         return 0;
7037 {                                             << 
7038         spin_lock(&nn->s2s_cp_lock);          << 
7039         _free_cpntf_state_locked(nn, cps);    << 
7040         spin_unlock(&nn->s2s_cp_lock);        << 
7041 }                                                5121 }
7042                                                  5122 
7043 /**                                           !! 5123 /*
7044  * nfs4_preprocess_stateid_op - find and prep !! 5124  * Checks for stateid operations
7045  * @rqstp: incoming request from client       << 
7046  * @cstate: current compound state            << 
7047  * @fhp: filehandle associated with requested << 
7048  * @stateid: stateid (provided by client)     << 
7049  * @flags: flags describing type of operation << 
7050  * @nfp: optional nfsd_file return pointer (m << 
7051  * @cstid: optional returned nfs4_stid pointe << 
7052  *                                            << 
7053  * Given info from the client, look up a nfs4 << 
7054  * success, it returns a reference to the nfs << 
7055  * associated with it.                        << 
7056  */                                              5125  */
7057 __be32                                           5126 __be32
7058 nfs4_preprocess_stateid_op(struct svc_rqst *r    5127 nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
7059                 struct nfsd4_compound_state *    5128                 struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
7060                 stateid_t *stateid, int flags !! 5129                 stateid_t *stateid, int flags, struct file **filpp, bool *tmp_file)
7061                 struct nfs4_stid **cstid)     << 
7062 {                                                5130 {
                                                   >> 5131         struct inode *ino = d_inode(fhp->fh_dentry);
7063         struct net *net = SVC_NET(rqstp);        5132         struct net *net = SVC_NET(rqstp);
7064         struct nfsd_net *nn = net_generic(net    5133         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7065         struct nfs4_stid *s = NULL;              5134         struct nfs4_stid *s = NULL;
7066         __be32 status;                           5135         __be32 status;
7067                                                  5136 
7068         if (nfp)                              !! 5137         if (filpp)
7069                 *nfp = NULL;                  !! 5138                 *filpp = NULL;
                                                   >> 5139         if (tmp_file)
                                                   >> 5140                 *tmp_file = false;
                                                   >> 5141 
                                                   >> 5142         if (grace_disallows_io(net, ino))
                                                   >> 5143                 return nfserr_grace;
7070                                                  5144 
7071         if (ZERO_STATEID(stateid) || ONE_STAT    5145         if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
7072                 status = check_special_statei    5146                 status = check_special_stateids(net, fhp, stateid, flags);
7073                 goto done;                       5147                 goto done;
7074         }                                        5148         }
7075                                                  5149 
7076         status = nfsd4_lookup_stateid(cstate,    5150         status = nfsd4_lookup_stateid(cstate, stateid,
7077                                 SC_TYPE_DELEG !! 5151                                 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
7078                                 0, &s, nn);   !! 5152                                 &s, nn);
7079         if (status == nfserr_bad_stateid)     << 
7080                 status = find_cpntf_state(nn, << 
7081         if (status)                              5153         if (status)
7082                 return status;                   5154                 return status;
7083         status = nfsd4_stid_check_stateid_gen !! 5155         status = check_stateid_generation(stateid, &s->sc_stateid,
7084                         nfsd4_has_session(cst    5156                         nfsd4_has_session(cstate));
7085         if (status)                              5157         if (status)
7086                 goto out;                        5158                 goto out;
7087                                                  5159 
7088         switch (s->sc_type) {                    5160         switch (s->sc_type) {
7089         case SC_TYPE_DELEG:                   !! 5161         case NFS4_DELEG_STID:
7090                 status = nfs4_check_delegmode    5162                 status = nfs4_check_delegmode(delegstateid(s), flags);
7091                 break;                           5163                 break;
7092         case SC_TYPE_OPEN:                    !! 5164         case NFS4_OPEN_STID:
7093         case SC_TYPE_LOCK:                    !! 5165         case NFS4_LOCK_STID:
7094                 status = nfs4_check_olstateid !! 5166                 status = nfs4_check_olstateid(fhp, openlockstateid(s), flags);
                                                   >> 5167                 break;
                                                   >> 5168         default:
                                                   >> 5169                 status = nfserr_bad_stateid;
7095                 break;                           5170                 break;
7096         }                                        5171         }
7097         if (status)                              5172         if (status)
7098                 goto out;                        5173                 goto out;
7099         status = nfs4_check_fh(fhp, s);          5174         status = nfs4_check_fh(fhp, s);
7100                                                  5175 
7101 done:                                            5176 done:
7102         if (status == nfs_ok && nfp)          !! 5177         if (!status && filpp)
7103                 status = nfs4_check_file(rqst !! 5178                 status = nfs4_check_file(rqstp, fhp, s, filpp, tmp_file, flags);
7104 out:                                             5179 out:
7105         if (s) {                              !! 5180         if (s)
7106                 if (!status && cstid)         !! 5181                 nfs4_put_stid(s);
7107                         *cstid = s;           << 
7108                 else                          << 
7109                         nfs4_put_stid(s);     << 
7110         }                                     << 
7111         return status;                           5182         return status;
7112 }                                                5183 }
7113                                                  5184 
7114 /*                                               5185 /*
7115  * Test if the stateid is valid                  5186  * Test if the stateid is valid
7116  */                                              5187  */
7117 __be32                                           5188 __be32
7118 nfsd4_test_stateid(struct svc_rqst *rqstp, st    5189 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7119                    union nfsd4_op_u *u)       !! 5190                    struct nfsd4_test_stateid *test_stateid)
7120 {                                                5191 {
7121         struct nfsd4_test_stateid *test_state << 
7122         struct nfsd4_test_stateid_id *stateid    5192         struct nfsd4_test_stateid_id *stateid;
7123         struct nfs4_client *cl = cstate->clp; !! 5193         struct nfs4_client *cl = cstate->session->se_client;
7124                                                  5194 
7125         list_for_each_entry(stateid, &test_st    5195         list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
7126                 stateid->ts_id_status =          5196                 stateid->ts_id_status =
7127                         nfsd4_validate_statei    5197                         nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
7128                                                  5198 
7129         return nfs_ok;                           5199         return nfs_ok;
7130 }                                                5200 }
7131                                                  5201 
7132 static __be32                                    5202 static __be32
7133 nfsd4_free_lock_stateid(stateid_t *stateid, s    5203 nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
7134 {                                                5204 {
7135         struct nfs4_ol_stateid *stp = openloc    5205         struct nfs4_ol_stateid *stp = openlockstateid(s);
7136         __be32 ret;                              5206         __be32 ret;
7137                                                  5207 
7138         ret = nfsd4_lock_ol_stateid(stp);     !! 5208         mutex_lock(&stp->st_mutex);
7139         if (ret)                              << 
7140                 goto out_put_stid;            << 
7141                                                  5209 
7142         ret = check_stateid_generation(statei    5210         ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
7143         if (ret)                                 5211         if (ret)
7144                 goto out;                        5212                 goto out;
7145                                                  5213 
7146         ret = nfserr_locks_held;                 5214         ret = nfserr_locks_held;
7147         if (check_for_locks(stp->st_stid.sc_f    5215         if (check_for_locks(stp->st_stid.sc_file,
7148                             lockowner(stp->st    5216                             lockowner(stp->st_stateowner)))
7149                 goto out;                        5217                 goto out;
7150                                                  5218 
7151         release_lock_stateid(stp);               5219         release_lock_stateid(stp);
7152         ret = nfs_ok;                            5220         ret = nfs_ok;
7153                                                  5221 
7154 out:                                             5222 out:
7155         mutex_unlock(&stp->st_mutex);            5223         mutex_unlock(&stp->st_mutex);
7156 out_put_stid:                                 << 
7157         nfs4_put_stid(s);                        5224         nfs4_put_stid(s);
7158         return ret;                              5225         return ret;
7159 }                                                5226 }
7160                                                  5227 
7161 __be32                                           5228 __be32
7162 nfsd4_free_stateid(struct svc_rqst *rqstp, st    5229 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7163                    union nfsd4_op_u *u)       !! 5230                    struct nfsd4_free_stateid *free_stateid)
7164 {                                                5231 {
7165         struct nfsd4_free_stateid *free_state << 
7166         stateid_t *stateid = &free_stateid->f    5232         stateid_t *stateid = &free_stateid->fr_stateid;
7167         struct nfs4_stid *s;                     5233         struct nfs4_stid *s;
7168         struct nfs4_delegation *dp;              5234         struct nfs4_delegation *dp;
7169         struct nfs4_client *cl = cstate->clp; !! 5235         struct nfs4_client *cl = cstate->session->se_client;
7170         __be32 ret = nfserr_bad_stateid;         5236         __be32 ret = nfserr_bad_stateid;
7171                                                  5237 
7172         spin_lock(&cl->cl_lock);                 5238         spin_lock(&cl->cl_lock);
7173         s = find_stateid_locked(cl, stateid);    5239         s = find_stateid_locked(cl, stateid);
7174         if (!s || s->sc_status & SC_STATUS_CL !! 5240         if (!s)
7175                 goto out_unlock;                 5241                 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);               << 
7182         switch (s->sc_type) {                    5242         switch (s->sc_type) {
7183         case SC_TYPE_DELEG:                   !! 5243         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;         5244                 ret = nfserr_locks_held;
7197                 break;                           5245                 break;
7198         case SC_TYPE_OPEN:                    !! 5246         case NFS4_OPEN_STID:
7199                 ret = check_stateid_generatio    5247                 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
7200                 if (ret)                         5248                 if (ret)
7201                         break;                   5249                         break;
7202                 ret = nfserr_locks_held;         5250                 ret = nfserr_locks_held;
7203                 break;                           5251                 break;
7204         case SC_TYPE_LOCK:                    !! 5252         case NFS4_LOCK_STID:
7205                 spin_unlock(&s->sc_lock);     !! 5253                 atomic_inc(&s->sc_count);
7206                 refcount_inc(&s->sc_count);   << 
7207                 spin_unlock(&cl->cl_lock);       5254                 spin_unlock(&cl->cl_lock);
7208                 ret = nfsd4_free_lock_stateid    5255                 ret = nfsd4_free_lock_stateid(stateid, s);
7209                 goto out;                        5256                 goto out;
                                                   >> 5257         case NFS4_REVOKED_DELEG_STID:
                                                   >> 5258                 dp = delegstateid(s);
                                                   >> 5259                 list_del_init(&dp->dl_recall_lru);
                                                   >> 5260                 spin_unlock(&cl->cl_lock);
                                                   >> 5261                 nfs4_put_stid(s);
                                                   >> 5262                 ret = nfs_ok;
                                                   >> 5263                 goto out;
                                                   >> 5264         /* Default falls through and returns nfserr_bad_stateid */
7210         }                                        5265         }
7211         spin_unlock(&s->sc_lock);             << 
7212 out_unlock:                                      5266 out_unlock:
7213         spin_unlock(&cl->cl_lock);               5267         spin_unlock(&cl->cl_lock);
7214 out:                                             5268 out:
7215         return ret;                              5269         return ret;
7216 }                                                5270 }
7217                                                  5271 
7218 static inline int                                5272 static inline int
7219 setlkflg (int type)                              5273 setlkflg (int type)
7220 {                                                5274 {
7221         return (type == NFS4_READW_LT || type    5275         return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
7222                 RD_STATE : WR_STATE;             5276                 RD_STATE : WR_STATE;
7223 }                                                5277 }
7224                                                  5278 
7225 static __be32 nfs4_seqid_op_checks(struct nfs    5279 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
7226 {                                                5280 {
7227         struct svc_fh *current_fh = &cstate->    5281         struct svc_fh *current_fh = &cstate->current_fh;
7228         struct nfs4_stateowner *sop = stp->st    5282         struct nfs4_stateowner *sop = stp->st_stateowner;
7229         __be32 status;                           5283         __be32 status;
7230                                                  5284 
7231         status = nfsd4_check_seqid(cstate, so    5285         status = nfsd4_check_seqid(cstate, sop, seqid);
7232         if (status)                              5286         if (status)
7233                 return status;                   5287                 return status;
7234         status = nfsd4_lock_ol_stateid(stp);     5288         status = nfsd4_lock_ol_stateid(stp);
7235         if (status != nfs_ok)                    5289         if (status != nfs_ok)
7236                 return status;                   5290                 return status;
7237         status = check_stateid_generation(sta    5291         status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
7238         if (status == nfs_ok)                    5292         if (status == nfs_ok)
7239                 status = nfs4_check_fh(curren    5293                 status = nfs4_check_fh(current_fh, &stp->st_stid);
7240         if (status != nfs_ok)                    5294         if (status != nfs_ok)
7241                 mutex_unlock(&stp->st_mutex);    5295                 mutex_unlock(&stp->st_mutex);
7242         return status;                           5296         return status;
7243 }                                                5297 }
7244                                                  5298 
7245 /**                                           !! 5299 /* 
7246  * nfs4_preprocess_seqid_op - find and prep a !! 5300  * Checks for sequence id mutating operations. 
7247  * @cstate: compund state                     << 
7248  * @seqid: seqid (provided by client)         << 
7249  * @stateid: stateid (provided by client)     << 
7250  * @typemask: mask of allowable types for thi << 
7251  * @statusmask: mask of allowed states: 0 or  << 
7252  * @stpp: return pointer for the stateid foun << 
7253  * @nn: net namespace for request             << 
7254  *                                            << 
7255  * Given a stateid+seqid from a client, look  << 
7256  * return it in @stpp. On a nfs_ok return, th << 
7257  * have its st_mutex locked.                  << 
7258  */                                              5301  */
7259 static __be32                                    5302 static __be32
7260 nfs4_preprocess_seqid_op(struct nfsd4_compoun    5303 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
7261                          stateid_t *stateid,  !! 5304                          stateid_t *stateid, char typemask,
7262                          unsigned short typem << 
7263                          struct nfs4_ol_state    5305                          struct nfs4_ol_stateid **stpp,
7264                          struct nfsd_net *nn)    5306                          struct nfsd_net *nn)
7265 {                                                5307 {
7266         __be32 status;                           5308         __be32 status;
7267         struct nfs4_stid *s;                     5309         struct nfs4_stid *s;
7268         struct nfs4_ol_stateid *stp = NULL;      5310         struct nfs4_ol_stateid *stp = NULL;
7269                                                  5311 
7270         trace_nfsd_preprocess(seqid, stateid) !! 5312         dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
                                                   >> 5313                 seqid, STATEID_VAL(stateid));
7271                                                  5314 
7272         *stpp = NULL;                            5315         *stpp = NULL;
7273 retry:                                        !! 5316         status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
7274         status = nfsd4_lookup_stateid(cstate, << 
7275                                       typemas << 
7276         if (status)                              5317         if (status)
7277                 return status;                   5318                 return status;
7278         stp = openlockstateid(s);                5319         stp = openlockstateid(s);
7279         if (nfsd4_cstate_assign_replay(cstate !! 5320         nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
7280                 nfs4_put_stateowner(stp->st_s << 
7281                 goto retry;                   << 
7282         }                                     << 
7283                                                  5321 
7284         status = nfs4_seqid_op_checks(cstate,    5322         status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
7285         if (!status)                             5323         if (!status)
7286                 *stpp = stp;                     5324                 *stpp = stp;
7287         else                                     5325         else
7288                 nfs4_put_stid(&stp->st_stid);    5326                 nfs4_put_stid(&stp->st_stid);
7289         return status;                           5327         return status;
7290 }                                                5328 }
7291                                                  5329 
7292 static __be32 nfs4_preprocess_confirmed_seqid    5330 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
7293                                                  5331                                                  stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
7294 {                                                5332 {
7295         __be32 status;                           5333         __be32 status;
7296         struct nfs4_openowner *oo;               5334         struct nfs4_openowner *oo;
7297         struct nfs4_ol_stateid *stp;             5335         struct nfs4_ol_stateid *stp;
7298                                                  5336 
7299         status = nfs4_preprocess_seqid_op(cst    5337         status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
7300                                           SC_ !! 5338                                                 NFS4_OPEN_STID, &stp, nn);
7301         if (status)                              5339         if (status)
7302                 return status;                   5340                 return status;
7303         oo = openowner(stp->st_stateowner);      5341         oo = openowner(stp->st_stateowner);
7304         if (!(oo->oo_flags & NFS4_OO_CONFIRME    5342         if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
7305                 mutex_unlock(&stp->st_mutex);    5343                 mutex_unlock(&stp->st_mutex);
7306                 nfs4_put_stid(&stp->st_stid);    5344                 nfs4_put_stid(&stp->st_stid);
7307                 return nfserr_bad_stateid;       5345                 return nfserr_bad_stateid;
7308         }                                        5346         }
7309         *stpp = stp;                             5347         *stpp = stp;
7310         return nfs_ok;                           5348         return nfs_ok;
7311 }                                                5349 }
7312                                                  5350 
7313 __be32                                           5351 __be32
7314 nfsd4_open_confirm(struct svc_rqst *rqstp, st    5352 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7315                    union nfsd4_op_u *u)       !! 5353                    struct nfsd4_open_confirm *oc)
7316 {                                                5354 {
7317         struct nfsd4_open_confirm *oc = &u->o << 
7318         __be32 status;                           5355         __be32 status;
7319         struct nfs4_openowner *oo;               5356         struct nfs4_openowner *oo;
7320         struct nfs4_ol_stateid *stp;             5357         struct nfs4_ol_stateid *stp;
7321         struct nfsd_net *nn = net_generic(SVC    5358         struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
7322                                                  5359 
7323         dprintk("NFSD: nfsd4_open_confirm on     5360         dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
7324                         cstate->current_fh.fh    5361                         cstate->current_fh.fh_dentry);
7325                                                  5362 
7326         status = fh_verify(rqstp, &cstate->cu    5363         status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
7327         if (status)                              5364         if (status)
7328                 return status;                   5365                 return status;
7329                                                  5366 
7330         status = nfs4_preprocess_seqid_op(cst    5367         status = nfs4_preprocess_seqid_op(cstate,
7331                                           oc- !! 5368                                         oc->oc_seqid, &oc->oc_req_stateid,
7332                                           SC_ !! 5369                                         NFS4_OPEN_STID, &stp, nn);
7333         if (status)                              5370         if (status)
7334                 goto out;                        5371                 goto out;
7335         oo = openowner(stp->st_stateowner);      5372         oo = openowner(stp->st_stateowner);
7336         status = nfserr_bad_stateid;             5373         status = nfserr_bad_stateid;
7337         if (oo->oo_flags & NFS4_OO_CONFIRMED)    5374         if (oo->oo_flags & NFS4_OO_CONFIRMED) {
7338                 mutex_unlock(&stp->st_mutex);    5375                 mutex_unlock(&stp->st_mutex);
7339                 goto put_stateid;                5376                 goto put_stateid;
7340         }                                        5377         }
7341         oo->oo_flags |= NFS4_OO_CONFIRMED;       5378         oo->oo_flags |= NFS4_OO_CONFIRMED;
7342         nfs4_inc_and_copy_stateid(&oc->oc_res    5379         nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
7343         mutex_unlock(&stp->st_mutex);            5380         mutex_unlock(&stp->st_mutex);
7344         trace_nfsd_open_confirm(oc->oc_seqid, !! 5381         dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
                                                   >> 5382                 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
                                                   >> 5383 
7345         nfsd4_client_record_create(oo->oo_own    5384         nfsd4_client_record_create(oo->oo_owner.so_client);
7346         status = nfs_ok;                         5385         status = nfs_ok;
7347 put_stateid:                                     5386 put_stateid:
7348         nfs4_put_stid(&stp->st_stid);            5387         nfs4_put_stid(&stp->st_stid);
7349 out:                                             5388 out:
7350         nfsd4_bump_seqid(cstate, status);        5389         nfsd4_bump_seqid(cstate, status);
7351         return status;                           5390         return status;
7352 }                                                5391 }
7353                                                  5392 
7354 static inline void nfs4_stateid_downgrade_bit    5393 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
7355 {                                                5394 {
7356         if (!test_access(access, stp))           5395         if (!test_access(access, stp))
7357                 return;                          5396                 return;
7358         nfs4_file_put_access(stp->st_stid.sc_    5397         nfs4_file_put_access(stp->st_stid.sc_file, access);
7359         clear_access(access, stp);               5398         clear_access(access, stp);
7360 }                                                5399 }
7361                                                  5400 
7362 static inline void nfs4_stateid_downgrade(str    5401 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
7363 {                                                5402 {
7364         switch (to_access) {                     5403         switch (to_access) {
7365         case NFS4_SHARE_ACCESS_READ:             5404         case NFS4_SHARE_ACCESS_READ:
7366                 nfs4_stateid_downgrade_bit(st    5405                 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
7367                 nfs4_stateid_downgrade_bit(st    5406                 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
7368                 break;                           5407                 break;
7369         case NFS4_SHARE_ACCESS_WRITE:            5408         case NFS4_SHARE_ACCESS_WRITE:
7370                 nfs4_stateid_downgrade_bit(st    5409                 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
7371                 nfs4_stateid_downgrade_bit(st    5410                 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
7372                 break;                           5411                 break;
7373         case NFS4_SHARE_ACCESS_BOTH:             5412         case NFS4_SHARE_ACCESS_BOTH:
7374                 break;                           5413                 break;
7375         default:                                 5414         default:
7376                 WARN_ON_ONCE(1);                 5415                 WARN_ON_ONCE(1);
7377         }                                        5416         }
7378 }                                                5417 }
7379                                                  5418 
7380 __be32                                           5419 __be32
7381 nfsd4_open_downgrade(struct svc_rqst *rqstp,     5420 nfsd4_open_downgrade(struct svc_rqst *rqstp,
7382                      struct nfsd4_compound_st !! 5421                      struct nfsd4_compound_state *cstate,
                                                   >> 5422                      struct nfsd4_open_downgrade *od)
7383 {                                                5423 {
7384         struct nfsd4_open_downgrade *od = &u- << 
7385         __be32 status;                           5424         __be32 status;
7386         struct nfs4_ol_stateid *stp;             5425         struct nfs4_ol_stateid *stp;
7387         struct nfsd_net *nn = net_generic(SVC    5426         struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
7388                                                  5427 
7389         dprintk("NFSD: nfsd4_open_downgrade o    5428         dprintk("NFSD: nfsd4_open_downgrade on file %pd\n", 
7390                         cstate->current_fh.fh    5429                         cstate->current_fh.fh_dentry);
7391                                                  5430 
7392         /* We don't yet support WANT bits: */    5431         /* We don't yet support WANT bits: */
7393         if (od->od_deleg_want)                   5432         if (od->od_deleg_want)
7394                 dprintk("NFSD: %s: od_deleg_w    5433                 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
7395                         od->od_deleg_want);      5434                         od->od_deleg_want);
7396                                                  5435 
7397         status = nfs4_preprocess_confirmed_se    5436         status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
7398                                         &od->    5437                                         &od->od_stateid, &stp, nn);
7399         if (status)                              5438         if (status)
7400                 goto out;                        5439                 goto out; 
7401         status = nfserr_inval;                   5440         status = nfserr_inval;
7402         if (!test_access(od->od_share_access,    5441         if (!test_access(od->od_share_access, stp)) {
7403                 dprintk("NFSD: access not a s    5442                 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
7404                         stp->st_access_bmap,     5443                         stp->st_access_bmap, od->od_share_access);
7405                 goto put_stateid;                5444                 goto put_stateid;
7406         }                                        5445         }
7407         if (!test_deny(od->od_share_deny, stp    5446         if (!test_deny(od->od_share_deny, stp)) {
7408                 dprintk("NFSD: deny not a sub    5447                 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
7409                         stp->st_deny_bmap, od    5448                         stp->st_deny_bmap, od->od_share_deny);
7410                 goto put_stateid;                5449                 goto put_stateid;
7411         }                                        5450         }
7412         nfs4_stateid_downgrade(stp, od->od_sh    5451         nfs4_stateid_downgrade(stp, od->od_share_access);
7413         reset_union_bmap_deny(od->od_share_de    5452         reset_union_bmap_deny(od->od_share_deny, stp);
7414         nfs4_inc_and_copy_stateid(&od->od_sta    5453         nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
7415         status = nfs_ok;                         5454         status = nfs_ok;
7416 put_stateid:                                     5455 put_stateid:
7417         mutex_unlock(&stp->st_mutex);            5456         mutex_unlock(&stp->st_mutex);
7418         nfs4_put_stid(&stp->st_stid);            5457         nfs4_put_stid(&stp->st_stid);
7419 out:                                             5458 out:
7420         nfsd4_bump_seqid(cstate, status);        5459         nfsd4_bump_seqid(cstate, status);
7421         return status;                           5460         return status;
7422 }                                                5461 }
7423                                                  5462 
7424 static bool nfsd4_close_open_stateid(struct n !! 5463 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
7425 {                                                5464 {
7426         struct nfs4_client *clp = s->st_stid.    5465         struct nfs4_client *clp = s->st_stid.sc_client;
7427         bool unhashed;                           5466         bool unhashed;
7428         LIST_HEAD(reaplist);                     5467         LIST_HEAD(reaplist);
7429         struct nfs4_ol_stateid *stp;          << 
7430                                                  5468 
7431         spin_lock(&clp->cl_lock);                5469         spin_lock(&clp->cl_lock);
7432         unhashed = unhash_open_stateid(s, &re    5470         unhashed = unhash_open_stateid(s, &reaplist);
7433                                                  5471 
7434         if (clp->cl_minorversion) {              5472         if (clp->cl_minorversion) {
7435                 if (unhashed)                    5473                 if (unhashed)
7436                         put_ol_stateid_locked    5474                         put_ol_stateid_locked(s, &reaplist);
7437                 spin_unlock(&clp->cl_lock);      5475                 spin_unlock(&clp->cl_lock);
7438                 list_for_each_entry(stp, &rea << 
7439                         nfs4_free_cpntf_state << 
7440                 free_ol_stateid_reaplist(&rea    5476                 free_ol_stateid_reaplist(&reaplist);
7441                 return false;                 << 
7442         } else {                                 5477         } else {
7443                 spin_unlock(&clp->cl_lock);      5478                 spin_unlock(&clp->cl_lock);
7444                 free_ol_stateid_reaplist(&rea    5479                 free_ol_stateid_reaplist(&reaplist);
7445                 return unhashed;              !! 5480                 if (unhashed)
                                                   >> 5481                         move_to_close_lru(s, clp->net);
7446         }                                        5482         }
7447 }                                                5483 }
7448                                                  5484 
7449 /*                                               5485 /*
7450  * nfs4_unlock_state() called after encode       5486  * nfs4_unlock_state() called after encode
7451  */                                              5487  */
7452 __be32                                           5488 __be32
7453 nfsd4_close(struct svc_rqst *rqstp, struct nf    5489 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7454                 union nfsd4_op_u *u)          !! 5490             struct nfsd4_close *close)
7455 {                                                5491 {
7456         struct nfsd4_close *close = &u->close << 
7457         __be32 status;                           5492         __be32 status;
7458         struct nfs4_ol_stateid *stp;             5493         struct nfs4_ol_stateid *stp;
7459         struct net *net = SVC_NET(rqstp);        5494         struct net *net = SVC_NET(rqstp);
7460         struct nfsd_net *nn = net_generic(net    5495         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7461         bool need_move_to_close_list;         << 
7462                                                  5496 
7463         dprintk("NFSD: nfsd4_close on file %p !! 5497         dprintk("NFSD: nfsd4_close on file %pd\n", 
7464                         cstate->current_fh.fh    5498                         cstate->current_fh.fh_dentry);
7465                                                  5499 
7466         status = nfs4_preprocess_seqid_op(cst    5500         status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
7467                                           &cl !! 5501                                         &close->cl_stateid,
7468                                           SC_ !! 5502                                         NFS4_OPEN_STID|NFS4_CLOSED_STID,
7469                                           &st !! 5503                                         &stp, nn);
7470         nfsd4_bump_seqid(cstate, status);        5504         nfsd4_bump_seqid(cstate, status);
7471         if (status)                              5505         if (status)
7472                 goto out;                     !! 5506                 goto out; 
7473                                               << 
7474         spin_lock(&stp->st_stid.sc_client->cl << 
7475         stp->st_stid.sc_status |= SC_STATUS_C << 
7476         spin_unlock(&stp->st_stid.sc_client-> << 
7477                                                  5507 
7478         /*                                    !! 5508         stp->st_stid.sc_type = NFS4_CLOSED_STID;
7479          * Technically we don't _really_ have << 
7480          * it should just be gone after this  << 
7481          * copied value below, but we continu << 
7482          * that racing ops see that there was << 
7483          */                                   << 
7484         nfs4_inc_and_copy_stateid(&close->cl_    5509         nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
7485                                                  5510 
7486         need_move_to_close_list = nfsd4_close !! 5511         nfsd4_close_open_stateid(stp);
7487         mutex_unlock(&stp->st_mutex);            5512         mutex_unlock(&stp->st_mutex);
7488         if (need_move_to_close_list)          << 
7489                 move_to_close_lru(stp, net);  << 
7490                                                  5513 
7491         /* v4.1+ suggests that we send a spec !! 5514         /* See RFC5661 sectionm 18.2.4 */
7492          * clients should just ignore this an !! 5515         if (stp->st_stid.sc_client->cl_minorversion)
7493          * for v4.0 clients either, we set it !! 5516                 memcpy(&close->cl_stateid, &close_stateid,
7494          * universally.                       !! 5517                                 sizeof(close->cl_stateid));
7495          *                                    << 
7496          * See RFC5661 section 18.2.4, and RF << 
7497          */                                   << 
7498         memcpy(&close->cl_stateid, &close_sta << 
7499                                                  5518 
7500         /* put reference from nfs4_preprocess    5519         /* put reference from nfs4_preprocess_seqid_op */
7501         nfs4_put_stid(&stp->st_stid);            5520         nfs4_put_stid(&stp->st_stid);
7502 out:                                             5521 out:
7503         return status;                           5522         return status;
7504 }                                                5523 }
7505                                                  5524 
7506 __be32                                           5525 __be32
7507 nfsd4_delegreturn(struct svc_rqst *rqstp, str    5526 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7508                   union nfsd4_op_u *u)        !! 5527                   struct nfsd4_delegreturn *dr)
7509 {                                                5528 {
7510         struct nfsd4_delegreturn *dr = &u->de << 
7511         struct nfs4_delegation *dp;              5529         struct nfs4_delegation *dp;
7512         stateid_t *stateid = &dr->dr_stateid;    5530         stateid_t *stateid = &dr->dr_stateid;
7513         struct nfs4_stid *s;                     5531         struct nfs4_stid *s;
7514         __be32 status;                           5532         __be32 status;
7515         struct nfsd_net *nn = net_generic(SVC    5533         struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
7516                                                  5534 
7517         if ((status = fh_verify(rqstp, &cstat    5535         if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
7518                 return status;                   5536                 return status;
7519                                                  5537 
7520         status = nfsd4_lookup_stateid(cstate, !! 5538         status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
7521                                       SC_STAT << 
7522                                       &s, nn) << 
7523         if (status)                              5539         if (status)
7524                 goto out;                        5540                 goto out;
7525         dp = delegstateid(s);                    5541         dp = delegstateid(s);
7526         status = nfsd4_stid_check_stateid_gen !! 5542         status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
7527         if (status)                              5543         if (status)
7528                 goto put_stateid;                5544                 goto put_stateid;
7529                                                  5545 
7530         trace_nfsd_deleg_return(stateid);     << 
7531         destroy_delegation(dp);                  5546         destroy_delegation(dp);
7532         smp_mb__after_atomic();               << 
7533         wake_up_var(d_inode(cstate->current_f << 
7534 put_stateid:                                     5547 put_stateid:
7535         nfs4_put_stid(&dp->dl_stid);             5548         nfs4_put_stid(&dp->dl_stid);
7536 out:                                             5549 out:
7537         return status;                           5550         return status;
7538 }                                                5551 }
7539                                                  5552 
                                                   >> 5553 static inline u64
                                                   >> 5554 end_offset(u64 start, u64 len)
                                                   >> 5555 {
                                                   >> 5556         u64 end;
                                                   >> 5557 
                                                   >> 5558         end = start + len;
                                                   >> 5559         return end >= start ? end: NFS4_MAX_UINT64;
                                                   >> 5560 }
                                                   >> 5561 
7540 /* last octet in a range */                      5562 /* last octet in a range */
7541 static inline u64                                5563 static inline u64
7542 last_byte_offset(u64 start, u64 len)             5564 last_byte_offset(u64 start, u64 len)
7543 {                                                5565 {
7544         u64 end;                                 5566         u64 end;
7545                                                  5567 
7546         WARN_ON_ONCE(!len);                      5568         WARN_ON_ONCE(!len);
7547         end = start + len;                       5569         end = start + len;
7548         return end > start ? end - 1: NFS4_MA    5570         return end > start ? end - 1: NFS4_MAX_UINT64;
7549 }                                                5571 }
7550                                                  5572 
7551 /*                                               5573 /*
7552  * TODO: Linux file offsets are _signed_ 64-b    5574  * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
7553  * we can't properly handle lock requests tha    5575  * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
7554  * byte, because of sign extension problems.     5576  * byte, because of sign extension problems.  Since NFSv4 calls for 64-bit
7555  * locking, this prevents us from being compl    5577  * locking, this prevents us from being completely protocol-compliant.  The
7556  * real solution to this problem is to start     5578  * real solution to this problem is to start using unsigned file offsets in
7557  * the VFS, but this is a very deep change!      5579  * the VFS, but this is a very deep change!
7558  */                                              5580  */
7559 static inline void                               5581 static inline void
7560 nfs4_transform_lock_offset(struct file_lock *    5582 nfs4_transform_lock_offset(struct file_lock *lock)
7561 {                                                5583 {
7562         if (lock->fl_start < 0)                  5584         if (lock->fl_start < 0)
7563                 lock->fl_start = OFFSET_MAX;     5585                 lock->fl_start = OFFSET_MAX;
7564         if (lock->fl_end < 0)                    5586         if (lock->fl_end < 0)
7565                 lock->fl_end = OFFSET_MAX;       5587                 lock->fl_end = OFFSET_MAX;
7566 }                                                5588 }
7567                                                  5589 
7568 static fl_owner_t                                5590 static fl_owner_t
7569 nfsd4_lm_get_owner(fl_owner_t owner)          !! 5591 nfsd4_fl_get_owner(fl_owner_t owner)
7570 {                                                5592 {
7571         struct nfs4_lockowner *lo = (struct n    5593         struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
7572                                                  5594 
7573         nfs4_get_stateowner(&lo->lo_owner);      5595         nfs4_get_stateowner(&lo->lo_owner);
7574         return owner;                            5596         return owner;
7575 }                                                5597 }
7576                                                  5598 
7577 static void                                      5599 static void
7578 nfsd4_lm_put_owner(fl_owner_t owner)          !! 5600 nfsd4_fl_put_owner(fl_owner_t owner)
7579 {                                                5601 {
7580         struct nfs4_lockowner *lo = (struct n    5602         struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
7581                                                  5603 
7582         if (lo)                                  5604         if (lo)
7583                 nfs4_put_stateowner(&lo->lo_o    5605                 nfs4_put_stateowner(&lo->lo_owner);
7584 }                                                5606 }
7585                                                  5607 
7586 /* return pointer to struct nfs4_client if cl << 
7587 static bool                                   << 
7588 nfsd4_lm_lock_expirable(struct file_lock *cfl << 
7589 {                                             << 
7590         struct nfs4_lockowner *lo = (struct n << 
7591         struct nfs4_client *clp = lo->lo_owne << 
7592         struct nfsd_net *nn;                  << 
7593                                               << 
7594         if (try_to_expire_client(clp)) {      << 
7595                 nn = net_generic(clp->net, nf << 
7596                 mod_delayed_work(laundry_wq,  << 
7597                 return true;                  << 
7598         }                                     << 
7599         return false;                         << 
7600 }                                             << 
7601                                               << 
7602 /* schedule laundromat to run immediately and << 
7603 static void                                   << 
7604 nfsd4_lm_expire_lock(void)                    << 
7605 {                                             << 
7606         flush_workqueue(laundry_wq);          << 
7607 }                                             << 
7608                                               << 
7609 static void                                      5608 static void
7610 nfsd4_lm_notify(struct file_lock *fl)            5609 nfsd4_lm_notify(struct file_lock *fl)
7611 {                                                5610 {
7612         struct nfs4_lockowner           *lo = !! 5611         struct nfs4_lockowner           *lo = (struct nfs4_lockowner *)fl->fl_owner;
7613         struct net                      *net     5612         struct net                      *net = lo->lo_owner.so_client->net;
7614         struct nfsd_net                 *nn =    5613         struct nfsd_net                 *nn = net_generic(net, nfsd_net_id);
7615         struct nfsd4_blocked_lock       *nbl     5614         struct nfsd4_blocked_lock       *nbl = container_of(fl,
7616                                                  5615                                                 struct nfsd4_blocked_lock, nbl_lock);
7617         bool queue = false;                      5616         bool queue = false;
7618                                                  5617 
7619         /* An empty list means that something    5618         /* An empty list means that something else is going to be using it */
7620         spin_lock(&nn->blocked_locks_lock);      5619         spin_lock(&nn->blocked_locks_lock);
7621         if (!list_empty(&nbl->nbl_list)) {       5620         if (!list_empty(&nbl->nbl_list)) {
7622                 list_del_init(&nbl->nbl_list)    5621                 list_del_init(&nbl->nbl_list);
7623                 list_del_init(&nbl->nbl_lru);    5622                 list_del_init(&nbl->nbl_lru);
7624                 queue = true;                    5623                 queue = true;
7625         }                                        5624         }
7626         spin_unlock(&nn->blocked_locks_lock);    5625         spin_unlock(&nn->blocked_locks_lock);
7627                                                  5626 
7628         if (queue) {                          !! 5627         if (queue)
7629                 trace_nfsd_cb_notify_lock(lo, << 
7630                 nfsd4_run_cb(&nbl->nbl_cb);      5628                 nfsd4_run_cb(&nbl->nbl_cb);
7631         }                                     << 
7632 }                                                5629 }
7633                                                  5630 
7634 static const struct lock_manager_operations n    5631 static const struct lock_manager_operations nfsd_posix_mng_ops  = {
7635         .lm_mod_owner = THIS_MODULE,          << 
7636         .lm_notify = nfsd4_lm_notify,            5632         .lm_notify = nfsd4_lm_notify,
7637         .lm_get_owner = nfsd4_lm_get_owner,   !! 5633         .lm_get_owner = nfsd4_fl_get_owner,
7638         .lm_put_owner = nfsd4_lm_put_owner,   !! 5634         .lm_put_owner = nfsd4_fl_put_owner,
7639         .lm_lock_expirable = nfsd4_lm_lock_ex << 
7640         .lm_expire_lock = nfsd4_lm_expire_loc << 
7641 };                                               5635 };
7642                                                  5636 
7643 static inline void                               5637 static inline void
7644 nfs4_set_lock_denied(struct file_lock *fl, st    5638 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
7645 {                                                5639 {
7646         struct nfs4_lockowner *lo;               5640         struct nfs4_lockowner *lo;
7647                                                  5641 
7648         if (fl->fl_lmops == &nfsd_posix_mng_o    5642         if (fl->fl_lmops == &nfsd_posix_mng_ops) {
7649                 lo = (struct nfs4_lockowner * !! 5643                 lo = (struct nfs4_lockowner *) fl->fl_owner;
7650                 xdr_netobj_dup(&deny->ld_owne !! 5644                 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
7651                                               !! 5645                                         lo->lo_owner.so_owner.len, GFP_KERNEL);
7652                 if (!deny->ld_owner.data)        5646                 if (!deny->ld_owner.data)
7653                         /* We just don't care    5647                         /* We just don't care that much */
7654                         goto nevermind;          5648                         goto nevermind;
                                                   >> 5649                 deny->ld_owner.len = lo->lo_owner.so_owner.len;
7655                 deny->ld_clientid = lo->lo_ow    5650                 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
7656         } else {                                 5651         } else {
7657 nevermind:                                       5652 nevermind:
7658                 deny->ld_owner.len = 0;          5653                 deny->ld_owner.len = 0;
7659                 deny->ld_owner.data = NULL;      5654                 deny->ld_owner.data = NULL;
7660                 deny->ld_clientid.cl_boot = 0    5655                 deny->ld_clientid.cl_boot = 0;
7661                 deny->ld_clientid.cl_id = 0;     5656                 deny->ld_clientid.cl_id = 0;
7662         }                                        5657         }
7663         deny->ld_start = fl->fl_start;           5658         deny->ld_start = fl->fl_start;
7664         deny->ld_length = NFS4_MAX_UINT64;       5659         deny->ld_length = NFS4_MAX_UINT64;
7665         if (fl->fl_end != NFS4_MAX_UINT64)       5660         if (fl->fl_end != NFS4_MAX_UINT64)
7666                 deny->ld_length = fl->fl_end     5661                 deny->ld_length = fl->fl_end - fl->fl_start + 1;        
7667         deny->ld_type = NFS4_READ_LT;            5662         deny->ld_type = NFS4_READ_LT;
7668         if (fl->c.flc_type != F_RDLCK)        !! 5663         if (fl->fl_type != F_RDLCK)
7669                 deny->ld_type = NFS4_WRITE_LT    5664                 deny->ld_type = NFS4_WRITE_LT;
7670 }                                                5665 }
7671                                                  5666 
7672 static struct nfs4_lockowner *                   5667 static struct nfs4_lockowner *
7673 find_lockowner_str_locked(struct nfs4_client     5668 find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
7674 {                                                5669 {
7675         unsigned int strhashval = ownerstr_ha    5670         unsigned int strhashval = ownerstr_hashval(owner);
7676         struct nfs4_stateowner *so;              5671         struct nfs4_stateowner *so;
7677                                                  5672 
7678         lockdep_assert_held(&clp->cl_lock);      5673         lockdep_assert_held(&clp->cl_lock);
7679                                                  5674 
7680         list_for_each_entry(so, &clp->cl_owne    5675         list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
7681                             so_strhash) {        5676                             so_strhash) {
7682                 if (so->so_is_open_owner)        5677                 if (so->so_is_open_owner)
7683                         continue;                5678                         continue;
7684                 if (same_owner_str(so, owner)    5679                 if (same_owner_str(so, owner))
7685                         return lockowner(nfs4    5680                         return lockowner(nfs4_get_stateowner(so));
7686         }                                        5681         }
7687         return NULL;                             5682         return NULL;
7688 }                                                5683 }
7689                                                  5684 
7690 static struct nfs4_lockowner *                   5685 static struct nfs4_lockowner *
7691 find_lockowner_str(struct nfs4_client *clp, s    5686 find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
7692 {                                                5687 {
7693         struct nfs4_lockowner *lo;               5688         struct nfs4_lockowner *lo;
7694                                                  5689 
7695         spin_lock(&clp->cl_lock);                5690         spin_lock(&clp->cl_lock);
7696         lo = find_lockowner_str_locked(clp, o    5691         lo = find_lockowner_str_locked(clp, owner);
7697         spin_unlock(&clp->cl_lock);              5692         spin_unlock(&clp->cl_lock);
7698         return lo;                               5693         return lo;
7699 }                                                5694 }
7700                                                  5695 
7701 static void nfs4_unhash_lockowner(struct nfs4    5696 static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
7702 {                                                5697 {
7703         unhash_lockowner_locked(lockowner(sop    5698         unhash_lockowner_locked(lockowner(sop));
7704 }                                                5699 }
7705                                                  5700 
7706 static void nfs4_free_lockowner(struct nfs4_s    5701 static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
7707 {                                                5702 {
7708         struct nfs4_lockowner *lo = lockowner    5703         struct nfs4_lockowner *lo = lockowner(sop);
7709                                                  5704 
7710         kmem_cache_free(lockowner_slab, lo);     5705         kmem_cache_free(lockowner_slab, lo);
7711 }                                                5706 }
7712                                                  5707 
7713 static const struct nfs4_stateowner_operation    5708 static const struct nfs4_stateowner_operations lockowner_ops = {
7714         .so_unhash =    nfs4_unhash_lockowner    5709         .so_unhash =    nfs4_unhash_lockowner,
7715         .so_free =      nfs4_free_lockowner,     5710         .so_free =      nfs4_free_lockowner,
7716 };                                               5711 };
7717                                                  5712 
7718 /*                                               5713 /*
7719  * Alloc a lock owner structure.                 5714  * Alloc a lock owner structure.
7720  * Called in nfsd4_lock - therefore, OPEN and    5715  * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has 
7721  * occurred.                                     5716  * occurred. 
7722  *                                               5717  *
7723  * strhashval = ownerstr_hashval                 5718  * strhashval = ownerstr_hashval
7724  */                                              5719  */
7725 static struct nfs4_lockowner *                   5720 static struct nfs4_lockowner *
7726 alloc_init_lock_stateowner(unsigned int strha    5721 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
7727                            struct nfs4_ol_sta    5722                            struct nfs4_ol_stateid *open_stp,
7728                            struct nfsd4_lock     5723                            struct nfsd4_lock *lock)
7729 {                                                5724 {
7730         struct nfs4_lockowner *lo, *ret;         5725         struct nfs4_lockowner *lo, *ret;
7731                                                  5726 
7732         lo = alloc_stateowner(lockowner_slab,    5727         lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
7733         if (!lo)                                 5728         if (!lo)
7734                 return NULL;                     5729                 return NULL;
7735         INIT_LIST_HEAD(&lo->lo_blocked);         5730         INIT_LIST_HEAD(&lo->lo_blocked);
7736         INIT_LIST_HEAD(&lo->lo_owner.so_state    5731         INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
7737         lo->lo_owner.so_is_open_owner = 0;       5732         lo->lo_owner.so_is_open_owner = 0;
7738         lo->lo_owner.so_seqid = lock->lk_new_    5733         lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
7739         lo->lo_owner.so_ops = &lockowner_ops;    5734         lo->lo_owner.so_ops = &lockowner_ops;
7740         spin_lock(&clp->cl_lock);                5735         spin_lock(&clp->cl_lock);
7741         ret = find_lockowner_str_locked(clp,     5736         ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
7742         if (ret == NULL) {                       5737         if (ret == NULL) {
7743                 list_add(&lo->lo_owner.so_str    5738                 list_add(&lo->lo_owner.so_strhash,
7744                          &clp->cl_ownerstr_ha    5739                          &clp->cl_ownerstr_hashtbl[strhashval]);
7745                 ret = lo;                        5740                 ret = lo;
7746         } else                                   5741         } else
7747                 nfs4_free_stateowner(&lo->lo_    5742                 nfs4_free_stateowner(&lo->lo_owner);
7748                                                  5743 
7749         spin_unlock(&clp->cl_lock);              5744         spin_unlock(&clp->cl_lock);
7750         return ret;                              5745         return ret;
7751 }                                                5746 }
7752                                                  5747 
7753 static struct nfs4_ol_stateid *               !! 5748 static void
7754 find_lock_stateid(const struct nfs4_lockowner << 
7755                   const struct nfs4_ol_statei << 
7756 {                                             << 
7757         struct nfs4_ol_stateid *lst;          << 
7758                                               << 
7759         lockdep_assert_held(&ost->st_stid.sc_ << 
7760                                               << 
7761         /* If ost is not hashed, ost->st_lock << 
7762         if (!nfs4_ol_stateid_unhashed(ost))   << 
7763                 list_for_each_entry(lst, &ost << 
7764                         if (lst->st_stateowne << 
7765                                 refcount_inc( << 
7766                                 return lst;   << 
7767                         }                     << 
7768                 }                             << 
7769         return NULL;                          << 
7770 }                                             << 
7771                                               << 
7772 static struct nfs4_ol_stateid *               << 
7773 init_lock_stateid(struct nfs4_ol_stateid *stp    5749 init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
7774                   struct nfs4_file *fp, struc    5750                   struct nfs4_file *fp, struct inode *inode,
7775                   struct nfs4_ol_stateid *ope    5751                   struct nfs4_ol_stateid *open_stp)
7776 {                                                5752 {
7777         struct nfs4_client *clp = lo->lo_owne    5753         struct nfs4_client *clp = lo->lo_owner.so_client;
7778         struct nfs4_ol_stateid *retstp;       << 
7779                                                  5754 
7780         mutex_init(&stp->st_mutex);           !! 5755         lockdep_assert_held(&clp->cl_lock);
7781         mutex_lock_nested(&stp->st_mutex, OPE !! 5756 
7782 retry:                                        !! 5757         atomic_inc(&stp->st_stid.sc_count);
7783         spin_lock(&clp->cl_lock);             !! 5758         stp->st_stid.sc_type = NFS4_LOCK_STID;
7784         if (nfs4_ol_stateid_unhashed(open_stp << 
7785                 goto out_close;               << 
7786         retstp = find_lock_stateid(lo, open_s << 
7787         if (retstp)                           << 
7788                 goto out_found;               << 
7789         refcount_inc(&stp->st_stid.sc_count); << 
7790         stp->st_stid.sc_type = SC_TYPE_LOCK;  << 
7791         stp->st_stateowner = nfs4_get_stateow    5759         stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
7792         get_nfs4_file(fp);                       5760         get_nfs4_file(fp);
7793         stp->st_stid.sc_file = fp;               5761         stp->st_stid.sc_file = fp;
7794         stp->st_access_bmap = 0;                 5762         stp->st_access_bmap = 0;
7795         stp->st_deny_bmap = open_stp->st_deny    5763         stp->st_deny_bmap = open_stp->st_deny_bmap;
7796         stp->st_openstp = open_stp;              5764         stp->st_openstp = open_stp;
7797         spin_lock(&fp->fi_lock);              !! 5765         mutex_init(&stp->st_mutex);
7798         list_add(&stp->st_locks, &open_stp->s    5766         list_add(&stp->st_locks, &open_stp->st_locks);
7799         list_add(&stp->st_perstateowner, &lo-    5767         list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
                                                   >> 5768         spin_lock(&fp->fi_lock);
7800         list_add(&stp->st_perfile, &fp->fi_st    5769         list_add(&stp->st_perfile, &fp->fi_stateids);
7801         spin_unlock(&fp->fi_lock);               5770         spin_unlock(&fp->fi_lock);
7802         spin_unlock(&clp->cl_lock);           !! 5771 }
7803         return stp;                           !! 5772 
7804 out_found:                                    !! 5773 static struct nfs4_ol_stateid *
7805         spin_unlock(&clp->cl_lock);           !! 5774 find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
7806         if (nfsd4_lock_ol_stateid(retstp) !=  !! 5775 {
7807                 nfs4_put_stid(&retstp->st_sti !! 5776         struct nfs4_ol_stateid *lst;
7808                 goto retry;                   !! 5777         struct nfs4_client *clp = lo->lo_owner.so_client;
                                                   >> 5778 
                                                   >> 5779         lockdep_assert_held(&clp->cl_lock);
                                                   >> 5780 
                                                   >> 5781         list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
                                                   >> 5782                 if (lst->st_stid.sc_file == fp) {
                                                   >> 5783                         atomic_inc(&lst->st_stid.sc_count);
                                                   >> 5784                         return lst;
                                                   >> 5785                 }
7809         }                                        5786         }
7810         /* To keep mutex tracking happy */    << 
7811         mutex_unlock(&stp->st_mutex);         << 
7812         return retstp;                        << 
7813 out_close:                                    << 
7814         spin_unlock(&clp->cl_lock);           << 
7815         mutex_unlock(&stp->st_mutex);         << 
7816         return NULL;                             5787         return NULL;
7817 }                                                5788 }
7818                                                  5789 
7819 static struct nfs4_ol_stateid *                  5790 static struct nfs4_ol_stateid *
7820 find_or_create_lock_stateid(struct nfs4_locko    5791 find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
7821                             struct inode *ino    5792                             struct inode *inode, struct nfs4_ol_stateid *ost,
7822                             bool *new)           5793                             bool *new)
7823 {                                                5794 {
7824         struct nfs4_stid *ns = NULL;             5795         struct nfs4_stid *ns = NULL;
7825         struct nfs4_ol_stateid *lst;             5796         struct nfs4_ol_stateid *lst;
7826         struct nfs4_openowner *oo = openowner    5797         struct nfs4_openowner *oo = openowner(ost->st_stateowner);
7827         struct nfs4_client *clp = oo->oo_owne    5798         struct nfs4_client *clp = oo->oo_owner.so_client;
7828                                                  5799 
7829         *new = false;                         << 
7830         spin_lock(&clp->cl_lock);                5800         spin_lock(&clp->cl_lock);
7831         lst = find_lock_stateid(lo, ost);     !! 5801         lst = find_lock_stateid(lo, fi);
7832         spin_unlock(&clp->cl_lock);           !! 5802         if (lst == NULL) {
7833         if (lst != NULL) {                    !! 5803                 spin_unlock(&clp->cl_lock);
7834                 if (nfsd4_lock_ol_stateid(lst !! 5804                 ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid);
7835                         goto out;             !! 5805                 if (ns == NULL)
7836                 nfs4_put_stid(&lst->st_stid); !! 5806                         return NULL;
7837         }                                     << 
7838         ns = nfs4_alloc_stid(clp, stateid_sla << 
7839         if (ns == NULL)                       << 
7840                 return NULL;                  << 
7841                                                  5807 
7842         lst = init_lock_stateid(openlockstate !! 5808                 spin_lock(&clp->cl_lock);
7843         if (lst == openlockstateid(ns))       !! 5809                 lst = find_lock_stateid(lo, fi);
7844                 *new = true;                  !! 5810                 if (likely(!lst)) {
7845         else                                  !! 5811                         lst = openlockstateid(ns);
                                                   >> 5812                         init_lock_stateid(lst, lo, fi, inode, ost);
                                                   >> 5813                         ns = NULL;
                                                   >> 5814                         *new = true;
                                                   >> 5815                 }
                                                   >> 5816         }
                                                   >> 5817         spin_unlock(&clp->cl_lock);
                                                   >> 5818         if (ns)
7846                 nfs4_put_stid(ns);               5819                 nfs4_put_stid(ns);
7847 out:                                          << 
7848         return lst;                              5820         return lst;
7849 }                                                5821 }
7850                                                  5822 
7851 static int                                       5823 static int
7852 check_lock_length(u64 offset, u64 length)        5824 check_lock_length(u64 offset, u64 length)
7853 {                                                5825 {
7854         return ((length == 0) || ((length !=     5826         return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
7855                 (length > ~offset)));            5827                 (length > ~offset)));
7856 }                                                5828 }
7857                                                  5829 
7858 static void get_lock_access(struct nfs4_ol_st    5830 static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
7859 {                                                5831 {
7860         struct nfs4_file *fp = lock_stp->st_s    5832         struct nfs4_file *fp = lock_stp->st_stid.sc_file;
7861                                                  5833 
7862         lockdep_assert_held(&fp->fi_lock);       5834         lockdep_assert_held(&fp->fi_lock);
7863                                                  5835 
7864         if (test_access(access, lock_stp))       5836         if (test_access(access, lock_stp))
7865                 return;                          5837                 return;
7866         __nfs4_file_get_access(fp, access);      5838         __nfs4_file_get_access(fp, access);
7867         set_access(access, lock_stp);            5839         set_access(access, lock_stp);
7868 }                                                5840 }
7869                                                  5841 
7870 static __be32                                    5842 static __be32
7871 lookup_or_create_lock_state(struct nfsd4_comp    5843 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
7872                             struct nfs4_ol_st    5844                             struct nfs4_ol_stateid *ost,
7873                             struct nfsd4_lock    5845                             struct nfsd4_lock *lock,
7874                             struct nfs4_ol_st    5846                             struct nfs4_ol_stateid **plst, bool *new)
7875 {                                                5847 {
7876         __be32 status;                           5848         __be32 status;
7877         struct nfs4_file *fi = ost->st_stid.s    5849         struct nfs4_file *fi = ost->st_stid.sc_file;
7878         struct nfs4_openowner *oo = openowner    5850         struct nfs4_openowner *oo = openowner(ost->st_stateowner);
7879         struct nfs4_client *cl = oo->oo_owner    5851         struct nfs4_client *cl = oo->oo_owner.so_client;
7880         struct inode *inode = d_inode(cstate-    5852         struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
7881         struct nfs4_lockowner *lo;               5853         struct nfs4_lockowner *lo;
7882         struct nfs4_ol_stateid *lst;             5854         struct nfs4_ol_stateid *lst;
7883         unsigned int strhashval;                 5855         unsigned int strhashval;
                                                   >> 5856         bool hashed;
7884                                                  5857 
7885         lo = find_lockowner_str(cl, &lock->lk    5858         lo = find_lockowner_str(cl, &lock->lk_new_owner);
7886         if (!lo) {                               5859         if (!lo) {
7887                 strhashval = ownerstr_hashval    5860                 strhashval = ownerstr_hashval(&lock->lk_new_owner);
7888                 lo = alloc_init_lock_stateown    5861                 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
7889                 if (lo == NULL)                  5862                 if (lo == NULL)
7890                         return nfserr_jukebox    5863                         return nfserr_jukebox;
7891         } else {                                 5864         } else {
7892                 /* with an existing lockowner    5865                 /* with an existing lockowner, seqids must be the same */
7893                 status = nfserr_bad_seqid;       5866                 status = nfserr_bad_seqid;
7894                 if (!cstate->minorversion &&     5867                 if (!cstate->minorversion &&
7895                     lock->lk_new_lock_seqid !    5868                     lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
7896                         goto out;                5869                         goto out;
7897         }                                        5870         }
7898                                                  5871 
                                                   >> 5872 retry:
7899         lst = find_or_create_lock_stateid(lo,    5873         lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
7900         if (lst == NULL) {                       5874         if (lst == NULL) {
7901                 status = nfserr_jukebox;         5875                 status = nfserr_jukebox;
7902                 goto out;                        5876                 goto out;
7903         }                                        5877         }
7904                                                  5878 
                                                   >> 5879         mutex_lock(&lst->st_mutex);
                                                   >> 5880 
                                                   >> 5881         /* See if it's still hashed to avoid race with FREE_STATEID */
                                                   >> 5882         spin_lock(&cl->cl_lock);
                                                   >> 5883         hashed = !list_empty(&lst->st_perfile);
                                                   >> 5884         spin_unlock(&cl->cl_lock);
                                                   >> 5885 
                                                   >> 5886         if (!hashed) {
                                                   >> 5887                 mutex_unlock(&lst->st_mutex);
                                                   >> 5888                 nfs4_put_stid(&lst->st_stid);
                                                   >> 5889                 goto retry;
                                                   >> 5890         }
7905         status = nfs_ok;                         5891         status = nfs_ok;
7906         *plst = lst;                             5892         *plst = lst;
7907 out:                                             5893 out:
7908         nfs4_put_stateowner(&lo->lo_owner);      5894         nfs4_put_stateowner(&lo->lo_owner);
7909         return status;                           5895         return status;
7910 }                                                5896 }
7911                                                  5897 
7912 /*                                               5898 /*
7913  *  LOCK operation                               5899  *  LOCK operation 
7914  */                                              5900  */
7915 __be32                                           5901 __be32
7916 nfsd4_lock(struct svc_rqst *rqstp, struct nfs    5902 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7917            union nfsd4_op_u *u)               !! 5903            struct nfsd4_lock *lock)
7918 {                                                5904 {
7919         struct nfsd4_lock *lock = &u->lock;   << 
7920         struct nfs4_openowner *open_sop = NUL    5905         struct nfs4_openowner *open_sop = NULL;
7921         struct nfs4_lockowner *lock_sop = NUL    5906         struct nfs4_lockowner *lock_sop = NULL;
7922         struct nfs4_ol_stateid *lock_stp = NU    5907         struct nfs4_ol_stateid *lock_stp = NULL;
7923         struct nfs4_ol_stateid *open_stp = NU    5908         struct nfs4_ol_stateid *open_stp = NULL;
7924         struct nfs4_file *fp;                    5909         struct nfs4_file *fp;
7925         struct nfsd_file *nf = NULL;          !! 5910         struct file *filp = NULL;
7926         struct nfsd4_blocked_lock *nbl = NULL    5911         struct nfsd4_blocked_lock *nbl = NULL;
7927         struct file_lock *file_lock = NULL;      5912         struct file_lock *file_lock = NULL;
7928         struct file_lock *conflock = NULL;       5913         struct file_lock *conflock = NULL;
7929         struct super_block *sb;               << 
7930         __be32 status = 0;                       5914         __be32 status = 0;
7931         int lkflg;                               5915         int lkflg;
7932         int err;                                 5916         int err;
7933         bool new = false;                        5917         bool new = false;
7934         unsigned char type;                   !! 5918         unsigned char fl_type;
7935         unsigned int flags = FL_POSIX;        !! 5919         unsigned int fl_flags = FL_POSIX;
7936         struct net *net = SVC_NET(rqstp);        5920         struct net *net = SVC_NET(rqstp);
7937         struct nfsd_net *nn = net_generic(net    5921         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7938                                                  5922 
7939         dprintk("NFSD: nfsd4_lock: start=%Ld     5923         dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
7940                 (long long) lock->lk_offset,     5924                 (long long) lock->lk_offset,
7941                 (long long) lock->lk_length);    5925                 (long long) lock->lk_length);
7942                                                  5926 
7943         if (check_lock_length(lock->lk_offset    5927         if (check_lock_length(lock->lk_offset, lock->lk_length))
7944                  return nfserr_inval;            5928                  return nfserr_inval;
7945                                                  5929 
7946         if ((status = fh_verify(rqstp, &cstat    5930         if ((status = fh_verify(rqstp, &cstate->current_fh,
7947                                 S_IFREG, NFSD    5931                                 S_IFREG, NFSD_MAY_LOCK))) {
7948                 dprintk("NFSD: nfsd4_lock: pe    5932                 dprintk("NFSD: nfsd4_lock: permission denied!\n");
7949                 return status;                   5933                 return status;
7950         }                                        5934         }
7951         sb = cstate->current_fh.fh_dentry->d_ << 
7952                                                  5935 
7953         if (lock->lk_is_new) {                   5936         if (lock->lk_is_new) {
7954                 if (nfsd4_has_session(cstate)    5937                 if (nfsd4_has_session(cstate))
7955                         /* See rfc 5661 18.10    5938                         /* See rfc 5661 18.10.3: given clientid is ignored: */
7956                         memcpy(&lock->lk_new_    5939                         memcpy(&lock->lk_new_clientid,
7957                                 &cstate->clp- !! 5940                                 &cstate->session->se_client->cl_clientid,
7958                                 sizeof(client    5941                                 sizeof(clientid_t));
7959                                                  5942 
                                                   >> 5943                 status = nfserr_stale_clientid;
                                                   >> 5944                 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
                                                   >> 5945                         goto out;
                                                   >> 5946 
7960                 /* validate and update open s    5947                 /* validate and update open stateid and open seqid */
7961                 status = nfs4_preprocess_conf    5948                 status = nfs4_preprocess_confirmed_seqid_op(cstate,
7962                                         lock-    5949                                         lock->lk_new_open_seqid,
7963                                         &lock    5950                                         &lock->lk_new_open_stateid,
7964                                         &open    5951                                         &open_stp, nn);
7965                 if (status)                      5952                 if (status)
7966                         goto out;                5953                         goto out;
7967                 mutex_unlock(&open_stp->st_mu    5954                 mutex_unlock(&open_stp->st_mutex);
7968                 open_sop = openowner(open_stp    5955                 open_sop = openowner(open_stp->st_stateowner);
7969                 status = nfserr_bad_stateid;     5956                 status = nfserr_bad_stateid;
7970                 if (!same_clid(&open_sop->oo_    5957                 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
7971                                                  5958                                                 &lock->lk_new_clientid))
7972                         goto out;                5959                         goto out;
7973                 status = lookup_or_create_loc    5960                 status = lookup_or_create_lock_state(cstate, open_stp, lock,
7974                                                  5961                                                         &lock_stp, &new);
7975         } else {                                 5962         } else {
7976                 status = nfs4_preprocess_seqi    5963                 status = nfs4_preprocess_seqid_op(cstate,
7977                                               !! 5964                                        lock->lk_old_lock_seqid,
7978                                               !! 5965                                        &lock->lk_old_lock_stateid,
7979                                               !! 5966                                        NFS4_LOCK_STID, &lock_stp, nn);
7980                                               << 
7981         }                                        5967         }
7982         if (status)                              5968         if (status)
7983                 goto out;                        5969                 goto out;
7984         lock_sop = lockowner(lock_stp->st_sta    5970         lock_sop = lockowner(lock_stp->st_stateowner);
7985                                                  5971 
7986         lkflg = setlkflg(lock->lk_type);         5972         lkflg = setlkflg(lock->lk_type);
7987         status = nfs4_check_openmode(lock_stp    5973         status = nfs4_check_openmode(lock_stp, lkflg);
7988         if (status)                              5974         if (status)
7989                 goto out;                        5975                 goto out;
7990                                                  5976 
7991         status = nfserr_grace;                   5977         status = nfserr_grace;
7992         if (locks_in_grace(net) && !lock->lk_    5978         if (locks_in_grace(net) && !lock->lk_reclaim)
7993                 goto out;                        5979                 goto out;
7994         status = nfserr_no_grace;                5980         status = nfserr_no_grace;
7995         if (!locks_in_grace(net) && lock->lk_    5981         if (!locks_in_grace(net) && lock->lk_reclaim)
7996                 goto out;                        5982                 goto out;
7997                                                  5983 
7998         if (lock->lk_reclaim)                 << 
7999                 flags |= FL_RECLAIM;          << 
8000                                               << 
8001         fp = lock_stp->st_stid.sc_file;          5984         fp = lock_stp->st_stid.sc_file;
8002         switch (lock->lk_type) {                 5985         switch (lock->lk_type) {
8003                 case NFS4_READW_LT:              5986                 case NFS4_READW_LT:
8004                         if (nfsd4_has_session !! 5987                         if (nfsd4_has_session(cstate))
8005                             exportfs_lock_op_ !! 5988                                 fl_flags |= FL_SLEEP;
8006                                 flags |= FL_S !! 5989                         /* Fallthrough */
8007                         fallthrough;          << 
8008                 case NFS4_READ_LT:               5990                 case NFS4_READ_LT:
8009                         spin_lock(&fp->fi_loc    5991                         spin_lock(&fp->fi_lock);
8010                         nf = find_readable_fi !! 5992                         filp = find_readable_file_locked(fp);
8011                         if (nf)               !! 5993                         if (filp)
8012                                 get_lock_acce    5994                                 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
8013                         spin_unlock(&fp->fi_l    5995                         spin_unlock(&fp->fi_lock);
8014                         type = F_RDLCK;       !! 5996                         fl_type = F_RDLCK;
8015                         break;                   5997                         break;
8016                 case NFS4_WRITEW_LT:             5998                 case NFS4_WRITEW_LT:
8017                         if (nfsd4_has_session !! 5999                         if (nfsd4_has_session(cstate))
8018                             exportfs_lock_op_ !! 6000                                 fl_flags |= FL_SLEEP;
8019                                 flags |= FL_S !! 6001                         /* Fallthrough */
8020                         fallthrough;          << 
8021                 case NFS4_WRITE_LT:              6002                 case NFS4_WRITE_LT:
8022                         spin_lock(&fp->fi_loc    6003                         spin_lock(&fp->fi_lock);
8023                         nf = find_writeable_f !! 6004                         filp = find_writeable_file_locked(fp);
8024                         if (nf)               !! 6005                         if (filp)
8025                                 get_lock_acce    6006                                 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
8026                         spin_unlock(&fp->fi_l    6007                         spin_unlock(&fp->fi_lock);
8027                         type = F_WRLCK;       !! 6008                         fl_type = F_WRLCK;
8028                         break;                   6009                         break;
8029                 default:                         6010                 default:
8030                         status = nfserr_inval    6011                         status = nfserr_inval;
8031                 goto out;                        6012                 goto out;
8032         }                                        6013         }
8033                                                  6014 
8034         if (!nf) {                            !! 6015         if (!filp) {
8035                 status = nfserr_openmode;        6016                 status = nfserr_openmode;
8036                 goto out;                        6017                 goto out;
8037         }                                        6018         }
8038                                                  6019 
8039         /*                                    << 
8040          * Most filesystems with their own -> << 
8041          * the nfsd thread waiting to acquire << 
8042          * deadlocks (we don't want every nfs << 
8043          * for file locks), so don't attempt  << 
8044          * on those filesystems:              << 
8045          */                                   << 
8046         if (!exportfs_lock_op_is_async(sb->s_ << 
8047                 flags &= ~FL_SLEEP;           << 
8048                                               << 
8049         nbl = find_or_allocate_block(lock_sop    6020         nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
8050         if (!nbl) {                              6021         if (!nbl) {
8051                 dprintk("NFSD: %s: unable to     6022                 dprintk("NFSD: %s: unable to allocate block!\n", __func__);
8052                 status = nfserr_jukebox;         6023                 status = nfserr_jukebox;
8053                 goto out;                        6024                 goto out;
8054         }                                        6025         }
8055                                                  6026 
8056         file_lock = &nbl->nbl_lock;              6027         file_lock = &nbl->nbl_lock;
8057         file_lock->c.flc_type = type;         !! 6028         file_lock->fl_type = fl_type;
8058         file_lock->c.flc_owner = (fl_owner_t) !! 6029         file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
8059         file_lock->c.flc_pid = current->tgid; !! 6030         file_lock->fl_pid = current->tgid;
8060         file_lock->c.flc_file = nf->nf_file;  !! 6031         file_lock->fl_file = filp;
8061         file_lock->c.flc_flags = flags;       !! 6032         file_lock->fl_flags = fl_flags;
8062         file_lock->fl_lmops = &nfsd_posix_mng    6033         file_lock->fl_lmops = &nfsd_posix_mng_ops;
8063         file_lock->fl_start = lock->lk_offset    6034         file_lock->fl_start = lock->lk_offset;
8064         file_lock->fl_end = last_byte_offset(    6035         file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
8065         nfs4_transform_lock_offset(file_lock)    6036         nfs4_transform_lock_offset(file_lock);
8066                                                  6037 
8067         conflock = locks_alloc_lock();           6038         conflock = locks_alloc_lock();
8068         if (!conflock) {                         6039         if (!conflock) {
8069                 dprintk("NFSD: %s: unable to     6040                 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
8070                 status = nfserr_jukebox;         6041                 status = nfserr_jukebox;
8071                 goto out;                        6042                 goto out;
8072         }                                        6043         }
8073                                                  6044 
8074         if (flags & FL_SLEEP) {               !! 6045         if (fl_flags & FL_SLEEP) {
8075                 nbl->nbl_time = ktime_get_boo !! 6046                 nbl->nbl_time = get_seconds();
8076                 spin_lock(&nn->blocked_locks_    6047                 spin_lock(&nn->blocked_locks_lock);
8077                 list_add_tail(&nbl->nbl_list,    6048                 list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
8078                 list_add_tail(&nbl->nbl_lru,     6049                 list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
8079                 kref_get(&nbl->nbl_kref);     << 
8080                 spin_unlock(&nn->blocked_lock    6050                 spin_unlock(&nn->blocked_locks_lock);
8081         }                                        6051         }
8082                                                  6052 
8083         err = vfs_lock_file(nf->nf_file, F_SE !! 6053         err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
8084         switch (err) {                           6054         switch (err) {
8085         case 0: /* success! */                   6055         case 0: /* success! */
8086                 nfs4_inc_and_copy_stateid(&lo    6056                 nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
8087                 status = 0;                      6057                 status = 0;
8088                 if (lock->lk_reclaim)         << 
8089                         nn->somebody_reclaime << 
8090                 break;                           6058                 break;
8091         case FILE_LOCK_DEFERRED:                 6059         case FILE_LOCK_DEFERRED:
8092                 kref_put(&nbl->nbl_kref, free << 
8093                 nbl = NULL;                      6060                 nbl = NULL;
8094                 fallthrough;                  !! 6061                 /* Fallthrough */
8095         case -EAGAIN:           /* conflock h    6062         case -EAGAIN:           /* conflock holds conflicting lock */
8096                 status = nfserr_denied;          6063                 status = nfserr_denied;
8097                 dprintk("NFSD: nfsd4_lock: co    6064                 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
8098                 nfs4_set_lock_denied(conflock    6065                 nfs4_set_lock_denied(conflock, &lock->lk_denied);
8099                 break;                           6066                 break;
8100         case -EDEADLK:                           6067         case -EDEADLK:
8101                 status = nfserr_deadlock;        6068                 status = nfserr_deadlock;
8102                 break;                           6069                 break;
8103         default:                                 6070         default:
8104                 dprintk("NFSD: nfsd4_lock: vf    6071                 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
8105                 status = nfserrno(err);          6072                 status = nfserrno(err);
8106                 break;                           6073                 break;
8107         }                                        6074         }
8108 out:                                             6075 out:
8109         if (nbl) {                               6076         if (nbl) {
8110                 /* dequeue it if we queued it    6077                 /* dequeue it if we queued it before */
8111                 if (flags & FL_SLEEP) {       !! 6078                 if (fl_flags & FL_SLEEP) {
8112                         spin_lock(&nn->blocke    6079                         spin_lock(&nn->blocked_locks_lock);
8113                         if (!list_empty(&nbl- !! 6080                         list_del_init(&nbl->nbl_list);
8114                             !list_empty(&nbl- !! 6081                         list_del_init(&nbl->nbl_lru);
8115                                 list_del_init << 
8116                                 list_del_init << 
8117                                 kref_put(&nbl << 
8118                         }                     << 
8119                         /* nbl can use one of << 
8120                         spin_unlock(&nn->bloc    6082                         spin_unlock(&nn->blocked_locks_lock);
8121                 }                                6083                 }
8122                 free_blocked_lock(nbl);          6084                 free_blocked_lock(nbl);
8123         }                                        6085         }
8124         if (nf)                               !! 6086         if (filp)
8125                 nfsd_file_put(nf);            !! 6087                 fput(filp);
8126         if (lock_stp) {                          6088         if (lock_stp) {
8127                 /* Bump seqid manually if the    6089                 /* Bump seqid manually if the 4.0 replay owner is openowner */
8128                 if (cstate->replay_owner &&      6090                 if (cstate->replay_owner &&
8129                     cstate->replay_owner != &    6091                     cstate->replay_owner != &lock_sop->lo_owner &&
8130                     seqid_mutating_err(ntohl(    6092                     seqid_mutating_err(ntohl(status)))
8131                         lock_sop->lo_owner.so    6093                         lock_sop->lo_owner.so_seqid++;
8132                                                  6094 
                                                   >> 6095                 mutex_unlock(&lock_stp->st_mutex);
                                                   >> 6096 
8133                 /*                               6097                 /*
8134                  * If this is a new, never-be    6098                  * If this is a new, never-before-used stateid, and we are
8135                  * returning an error, then j    6099                  * returning an error, then just go ahead and release it.
8136                  */                              6100                  */
8137                 if (status && new)               6101                 if (status && new)
8138                         release_lock_stateid(    6102                         release_lock_stateid(lock_stp);
8139                                                  6103 
8140                 mutex_unlock(&lock_stp->st_mu << 
8141                                               << 
8142                 nfs4_put_stid(&lock_stp->st_s    6104                 nfs4_put_stid(&lock_stp->st_stid);
8143         }                                        6105         }
8144         if (open_stp)                            6106         if (open_stp)
8145                 nfs4_put_stid(&open_stp->st_s    6107                 nfs4_put_stid(&open_stp->st_stid);
8146         nfsd4_bump_seqid(cstate, status);        6108         nfsd4_bump_seqid(cstate, status);
8147         if (conflock)                            6109         if (conflock)
8148                 locks_free_lock(conflock);       6110                 locks_free_lock(conflock);
8149         return status;                           6111         return status;
8150 }                                                6112 }
8151                                                  6113 
8152 void nfsd4_lock_release(union nfsd4_op_u *u)  << 
8153 {                                             << 
8154         struct nfsd4_lock *lock = &u->lock;   << 
8155         struct nfsd4_lock_denied *deny = &loc << 
8156                                               << 
8157         kfree(deny->ld_owner.data);           << 
8158 }                                             << 
8159                                               << 
8160 /*                                               6114 /*
8161  * The NFSv4 spec allows a client to do a LOC    6115  * 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    6116  * so we do a temporary open here just to get an open file to pass to
8163  * vfs_test_lock.                             !! 6117  * vfs_test_lock.  (Arguably perhaps test_lock should be done with an
                                                   >> 6118  * inode operation.)
8164  */                                              6119  */
8165 static __be32 nfsd_test_lock(struct svc_rqst     6120 static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
8166 {                                                6121 {
8167         struct nfsd_file *nf;                 !! 6122         struct file *file;
8168         struct inode *inode;                  !! 6123         __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
8169         __be32 err;                           !! 6124         if (!err) {
8170                                               !! 6125                 err = nfserrno(vfs_test_lock(file, lock));
8171         err = nfsd_file_acquire(rqstp, fhp, N !! 6126                 fput(file);
8172         if (err)                              !! 6127         }
8173                 return err;                   << 
8174         inode = fhp->fh_dentry->d_inode;      << 
8175         inode_lock(inode); /* to block new le << 
8176         err = nfserrno(nfsd_open_break_lease( << 
8177         if (err)                              << 
8178                 goto out;                     << 
8179         lock->c.flc_file = nf->nf_file;       << 
8180         err = nfserrno(vfs_test_lock(nf->nf_f << 
8181         lock->c.flc_file = NULL;              << 
8182 out:                                          << 
8183         inode_unlock(inode);                  << 
8184         nfsd_file_put(nf);                    << 
8185         return err;                              6128         return err;
8186 }                                                6129 }
8187                                                  6130 
8188 /*                                               6131 /*
8189  * LOCKT operation                               6132  * LOCKT operation
8190  */                                              6133  */
8191 __be32                                           6134 __be32
8192 nfsd4_lockt(struct svc_rqst *rqstp, struct nf    6135 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
8193             union nfsd4_op_u *u)              !! 6136             struct nfsd4_lockt *lockt)
8194 {                                                6137 {
8195         struct nfsd4_lockt *lockt = &u->lockt << 
8196         struct file_lock *file_lock = NULL;      6138         struct file_lock *file_lock = NULL;
8197         struct nfs4_lockowner *lo = NULL;        6139         struct nfs4_lockowner *lo = NULL;
8198         __be32 status;                           6140         __be32 status;
8199         struct nfsd_net *nn = net_generic(SVC    6141         struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
8200                                                  6142 
8201         if (locks_in_grace(SVC_NET(rqstp)))      6143         if (locks_in_grace(SVC_NET(rqstp)))
8202                 return nfserr_grace;             6144                 return nfserr_grace;
8203                                                  6145 
8204         if (check_lock_length(lockt->lt_offse    6146         if (check_lock_length(lockt->lt_offset, lockt->lt_length))
8205                  return nfserr_inval;            6147                  return nfserr_inval;
8206                                                  6148 
8207         if (!nfsd4_has_session(cstate)) {        6149         if (!nfsd4_has_session(cstate)) {
8208                 status = set_client(&lockt->l !! 6150                 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
8209                 if (status)                      6151                 if (status)
8210                         goto out;                6152                         goto out;
8211         }                                        6153         }
8212                                                  6154 
8213         if ((status = fh_verify(rqstp, &cstat    6155         if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
8214                 goto out;                        6156                 goto out;
8215                                                  6157 
8216         file_lock = locks_alloc_lock();          6158         file_lock = locks_alloc_lock();
8217         if (!file_lock) {                        6159         if (!file_lock) {
8218                 dprintk("NFSD: %s: unable to     6160                 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
8219                 status = nfserr_jukebox;         6161                 status = nfserr_jukebox;
8220                 goto out;                        6162                 goto out;
8221         }                                        6163         }
8222                                                  6164 
8223         switch (lockt->lt_type) {                6165         switch (lockt->lt_type) {
8224                 case NFS4_READ_LT:               6166                 case NFS4_READ_LT:
8225                 case NFS4_READW_LT:              6167                 case NFS4_READW_LT:
8226                         file_lock->c.flc_type !! 6168                         file_lock->fl_type = F_RDLCK;
8227                         break;                !! 6169                 break;
8228                 case NFS4_WRITE_LT:              6170                 case NFS4_WRITE_LT:
8229                 case NFS4_WRITEW_LT:             6171                 case NFS4_WRITEW_LT:
8230                         file_lock->c.flc_type !! 6172                         file_lock->fl_type = F_WRLCK;
8231                         break;                !! 6173                 break;
8232                 default:                         6174                 default:
8233                         dprintk("NFSD: nfs4_l    6175                         dprintk("NFSD: nfs4_lockt: bad lock type!\n");
8234                         status = nfserr_inval    6176                         status = nfserr_inval;
8235                         goto out;             !! 6177                 goto out;
8236         }                                        6178         }
8237                                                  6179 
8238         lo = find_lockowner_str(cstate->clp,     6180         lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
8239         if (lo)                                  6181         if (lo)
8240                 file_lock->c.flc_owner = (fl_ !! 6182                 file_lock->fl_owner = (fl_owner_t)lo;
8241         file_lock->c.flc_pid = current->tgid; !! 6183         file_lock->fl_pid = current->tgid;
8242         file_lock->c.flc_flags = FL_POSIX;    !! 6184         file_lock->fl_flags = FL_POSIX;
8243                                                  6185 
8244         file_lock->fl_start = lockt->lt_offse    6186         file_lock->fl_start = lockt->lt_offset;
8245         file_lock->fl_end = last_byte_offset(    6187         file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
8246                                                  6188 
8247         nfs4_transform_lock_offset(file_lock)    6189         nfs4_transform_lock_offset(file_lock);
8248                                                  6190 
8249         status = nfsd_test_lock(rqstp, &cstat    6191         status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
8250         if (status)                              6192         if (status)
8251                 goto out;                        6193                 goto out;
8252                                                  6194 
8253         if (file_lock->c.flc_type != F_UNLCK) !! 6195         if (file_lock->fl_type != F_UNLCK) {
8254                 status = nfserr_denied;          6196                 status = nfserr_denied;
8255                 nfs4_set_lock_denied(file_loc    6197                 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
8256         }                                        6198         }
8257 out:                                             6199 out:
8258         if (lo)                                  6200         if (lo)
8259                 nfs4_put_stateowner(&lo->lo_o    6201                 nfs4_put_stateowner(&lo->lo_owner);
8260         if (file_lock)                           6202         if (file_lock)
8261                 locks_free_lock(file_lock);      6203                 locks_free_lock(file_lock);
8262         return status;                           6204         return status;
8263 }                                                6205 }
8264                                                  6206 
8265 void nfsd4_lockt_release(union nfsd4_op_u *u) << 
8266 {                                             << 
8267         struct nfsd4_lockt *lockt = &u->lockt << 
8268         struct nfsd4_lock_denied *deny = &loc << 
8269                                               << 
8270         kfree(deny->ld_owner.data);           << 
8271 }                                             << 
8272                                               << 
8273 __be32                                           6207 __be32
8274 nfsd4_locku(struct svc_rqst *rqstp, struct nf    6208 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
8275             union nfsd4_op_u *u)              !! 6209             struct nfsd4_locku *locku)
8276 {                                                6210 {
8277         struct nfsd4_locku *locku = &u->locku << 
8278         struct nfs4_ol_stateid *stp;             6211         struct nfs4_ol_stateid *stp;
8279         struct nfsd_file *nf = NULL;          !! 6212         struct file *filp = NULL;
8280         struct file_lock *file_lock = NULL;      6213         struct file_lock *file_lock = NULL;
8281         __be32 status;                           6214         __be32 status;
8282         int err;                                 6215         int err;
8283         struct nfsd_net *nn = net_generic(SVC    6216         struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
8284                                                  6217 
8285         dprintk("NFSD: nfsd4_locku: start=%Ld    6218         dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
8286                 (long long) locku->lu_offset,    6219                 (long long) locku->lu_offset,
8287                 (long long) locku->lu_length)    6220                 (long long) locku->lu_length);
8288                                                  6221 
8289         if (check_lock_length(locku->lu_offse    6222         if (check_lock_length(locku->lu_offset, locku->lu_length))
8290                  return nfserr_inval;            6223                  return nfserr_inval;
8291                                                  6224 
8292         status = nfs4_preprocess_seqid_op(cst    6225         status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
8293                                           &lo !! 6226                                         &locku->lu_stateid, NFS4_LOCK_STID,
8294                                           &st !! 6227                                         &stp, nn);
8295         if (status)                              6228         if (status)
8296                 goto out;                        6229                 goto out;
8297         nf = find_any_file(stp->st_stid.sc_fi !! 6230         filp = find_any_file(stp->st_stid.sc_file);
8298         if (!nf) {                            !! 6231         if (!filp) {
8299                 status = nfserr_lock_range;      6232                 status = nfserr_lock_range;
8300                 goto put_stateid;                6233                 goto put_stateid;
8301         }                                        6234         }
8302         file_lock = locks_alloc_lock();          6235         file_lock = locks_alloc_lock();
8303         if (!file_lock) {                        6236         if (!file_lock) {
8304                 dprintk("NFSD: %s: unable to     6237                 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
8305                 status = nfserr_jukebox;         6238                 status = nfserr_jukebox;
8306                 goto put_file;                !! 6239                 goto fput;
8307         }                                        6240         }
8308                                                  6241 
8309         file_lock->c.flc_type = F_UNLCK;      !! 6242         file_lock->fl_type = F_UNLCK;
8310         file_lock->c.flc_owner = (fl_owner_t) !! 6243         file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
8311         file_lock->c.flc_pid = current->tgid; !! 6244         file_lock->fl_pid = current->tgid;
8312         file_lock->c.flc_file = nf->nf_file;  !! 6245         file_lock->fl_file = filp;
8313         file_lock->c.flc_flags = FL_POSIX;    !! 6246         file_lock->fl_flags = FL_POSIX;
8314         file_lock->fl_lmops = &nfsd_posix_mng    6247         file_lock->fl_lmops = &nfsd_posix_mng_ops;
8315         file_lock->fl_start = locku->lu_offse    6248         file_lock->fl_start = locku->lu_offset;
8316                                                  6249 
8317         file_lock->fl_end = last_byte_offset(    6250         file_lock->fl_end = last_byte_offset(locku->lu_offset,
8318                                                  6251                                                 locku->lu_length);
8319         nfs4_transform_lock_offset(file_lock)    6252         nfs4_transform_lock_offset(file_lock);
8320                                                  6253 
8321         err = vfs_lock_file(nf->nf_file, F_SE !! 6254         err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
8322         if (err) {                               6255         if (err) {
8323                 dprintk("NFSD: nfs4_locku: vf    6256                 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
8324                 goto out_nfserr;                 6257                 goto out_nfserr;
8325         }                                        6258         }
8326         nfs4_inc_and_copy_stateid(&locku->lu_    6259         nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
8327 put_file:                                     !! 6260 fput:
8328         nfsd_file_put(nf);                    !! 6261         fput(filp);
8329 put_stateid:                                     6262 put_stateid:
8330         mutex_unlock(&stp->st_mutex);            6263         mutex_unlock(&stp->st_mutex);
8331         nfs4_put_stid(&stp->st_stid);            6264         nfs4_put_stid(&stp->st_stid);
8332 out:                                             6265 out:
8333         nfsd4_bump_seqid(cstate, status);        6266         nfsd4_bump_seqid(cstate, status);
8334         if (file_lock)                           6267         if (file_lock)
8335                 locks_free_lock(file_lock);      6268                 locks_free_lock(file_lock);
8336         return status;                           6269         return status;
8337                                                  6270 
8338 out_nfserr:                                      6271 out_nfserr:
8339         status = nfserrno(err);                  6272         status = nfserrno(err);
8340         goto put_file;                        !! 6273         goto fput;
8341 }                                                6274 }
8342                                                  6275 
8343 /*                                               6276 /*
8344  * returns                                       6277  * returns
8345  *      true:  locks held by lockowner           6278  *      true:  locks held by lockowner
8346  *      false: no locks held by lockowner        6279  *      false: no locks held by lockowner
8347  */                                              6280  */
8348 static bool                                      6281 static bool
8349 check_for_locks(struct nfs4_file *fp, struct     6282 check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
8350 {                                                6283 {
8351         struct file_lock *fl;                    6284         struct file_lock *fl;
8352         int status = false;                      6285         int status = false;
8353         struct nfsd_file *nf;                 !! 6286         struct file *filp = find_any_file(fp);
8354         struct inode *inode;                     6287         struct inode *inode;
8355         struct file_lock_context *flctx;         6288         struct file_lock_context *flctx;
8356                                                  6289 
8357         spin_lock(&fp->fi_lock);              !! 6290         if (!filp) {
8358         nf = find_any_file_locked(fp);        << 
8359         if (!nf) {                            << 
8360                 /* Any valid lock stateid sho    6291                 /* Any valid lock stateid should have some sort of access */
8361                 WARN_ON_ONCE(1);                 6292                 WARN_ON_ONCE(1);
8362                 goto out;                     !! 6293                 return status;
8363         }                                        6294         }
8364                                                  6295 
8365         inode = file_inode(nf->nf_file);      !! 6296         inode = file_inode(filp);
8366         flctx = locks_inode_context(inode);   !! 6297         flctx = inode->i_flctx;
8367                                                  6298 
8368         if (flctx && !list_empty_careful(&flc    6299         if (flctx && !list_empty_careful(&flctx->flc_posix)) {
8369                 spin_lock(&flctx->flc_lock);     6300                 spin_lock(&flctx->flc_lock);
8370                 for_each_file_lock(fl, &flctx !! 6301                 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
8371                         if (fl->c.flc_owner = !! 6302                         if (fl->fl_owner == (fl_owner_t)lowner) {
8372                                 status = true    6303                                 status = true;
8373                                 break;           6304                                 break;
8374                         }                        6305                         }
8375                 }                                6306                 }
8376                 spin_unlock(&flctx->flc_lock)    6307                 spin_unlock(&flctx->flc_lock);
8377         }                                        6308         }
8378 out:                                          !! 6309         fput(filp);
8379         spin_unlock(&fp->fi_lock);            << 
8380         return status;                           6310         return status;
8381 }                                                6311 }
8382                                                  6312 
8383 /**                                           << 
8384  * nfsd4_release_lockowner - process NFSv4.0  << 
8385  * @rqstp: RPC transaction                    << 
8386  * @cstate: NFSv4 COMPOUND state              << 
8387  * @u: RELEASE_LOCKOWNER arguments            << 
8388  *                                            << 
8389  * Check if there are any locks still held an << 
8390  * and any lock state that is owned.          << 
8391  *                                            << 
8392  * Return values:                             << 
8393  *   %nfs_ok: lockowner released or not found << 
8394  *   %nfserr_locks_held: lockowner still in u << 
8395  *   %nfserr_stale_clientid: clientid no long << 
8396  *   %nfserr_expired: clientid not recognized << 
8397  */                                           << 
8398 __be32                                           6313 __be32
8399 nfsd4_release_lockowner(struct svc_rqst *rqst    6314 nfsd4_release_lockowner(struct svc_rqst *rqstp,
8400                         struct nfsd4_compound    6315                         struct nfsd4_compound_state *cstate,
8401                         union nfsd4_op_u *u)  !! 6316                         struct nfsd4_release_lockowner *rlockowner)
8402 {                                                6317 {
8403         struct nfsd4_release_lockowner *rlock << 
8404         struct nfsd_net *nn = net_generic(SVC << 
8405         clientid_t *clid = &rlockowner->rl_cl    6318         clientid_t *clid = &rlockowner->rl_clientid;
                                                   >> 6319         struct nfs4_stateowner *sop;
                                                   >> 6320         struct nfs4_lockowner *lo = NULL;
8406         struct nfs4_ol_stateid *stp;             6321         struct nfs4_ol_stateid *stp;
8407         struct nfs4_lockowner *lo;            !! 6322         struct xdr_netobj *owner = &rlockowner->rl_owner;
8408         struct nfs4_client *clp;              !! 6323         unsigned int hashval = ownerstr_hashval(owner);
8409         LIST_HEAD(reaplist);                  << 
8410         __be32 status;                           6324         __be32 status;
                                                   >> 6325         struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
                                                   >> 6326         struct nfs4_client *clp;
                                                   >> 6327         LIST_HEAD (reaplist);
8411                                                  6328 
8412         dprintk("nfsd4_release_lockowner clie    6329         dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
8413                 clid->cl_boot, clid->cl_id);     6330                 clid->cl_boot, clid->cl_id);
8414                                                  6331 
8415         status = set_client(clid, cstate, nn) !! 6332         status = lookup_clientid(clid, cstate, nn);
8416         if (status)                              6333         if (status)
8417                 return status;                   6334                 return status;
8418         clp = cstate->clp;                    << 
8419                                                  6335 
                                                   >> 6336         clp = cstate->clp;
                                                   >> 6337         /* Find the matching lock stateowner */
8420         spin_lock(&clp->cl_lock);                6338         spin_lock(&clp->cl_lock);
8421         lo = find_lockowner_str_locked(clp, & !! 6339         list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
8422         if (!lo) {                            !! 6340                             so_strhash) {
8423                 spin_unlock(&clp->cl_lock);   << 
8424                 return nfs_ok;                << 
8425         }                                     << 
8426                                                  6341 
8427         list_for_each_entry(stp, &lo->lo_owne !! 6342                 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
8428                 if (check_for_locks(stp->st_s !! 6343                         continue;
                                                   >> 6344 
                                                   >> 6345                 if (atomic_read(&sop->so_count) != 1) {
8429                         spin_unlock(&clp->cl_    6346                         spin_unlock(&clp->cl_lock);
8430                         nfs4_put_stateowner(& << 
8431                         return nfserr_locks_h    6347                         return nfserr_locks_held;
8432                 }                                6348                 }
                                                   >> 6349 
                                                   >> 6350                 lo = lockowner(sop);
                                                   >> 6351                 nfs4_get_stateowner(sop);
                                                   >> 6352                 break;
                                                   >> 6353         }
                                                   >> 6354         if (!lo) {
                                                   >> 6355                 spin_unlock(&clp->cl_lock);
                                                   >> 6356                 return status;
8433         }                                        6357         }
                                                   >> 6358 
8434         unhash_lockowner_locked(lo);             6359         unhash_lockowner_locked(lo);
8435         while (!list_empty(&lo->lo_owner.so_s    6360         while (!list_empty(&lo->lo_owner.so_stateids)) {
8436                 stp = list_first_entry(&lo->l    6361                 stp = list_first_entry(&lo->lo_owner.so_stateids,
8437                                        struct    6362                                        struct nfs4_ol_stateid,
8438                                        st_per    6363                                        st_perstateowner);
8439                 unhash_lock_stateid(stp);     !! 6364                 WARN_ON(!unhash_lock_stateid(stp));
8440                 put_ol_stateid_locked(stp, &r    6365                 put_ol_stateid_locked(stp, &reaplist);
8441         }                                        6366         }
8442         spin_unlock(&clp->cl_lock);              6367         spin_unlock(&clp->cl_lock);
8443                                               << 
8444         free_ol_stateid_reaplist(&reaplist);     6368         free_ol_stateid_reaplist(&reaplist);
8445         remove_blocked_locks(lo);                6369         remove_blocked_locks(lo);
8446         nfs4_put_stateowner(&lo->lo_owner);      6370         nfs4_put_stateowner(&lo->lo_owner);
8447         return nfs_ok;                        !! 6371 
                                                   >> 6372         return status;
8448 }                                                6373 }
8449                                                  6374 
8450 static inline struct nfs4_client_reclaim *       6375 static inline struct nfs4_client_reclaim *
8451 alloc_reclaim(void)                              6376 alloc_reclaim(void)
8452 {                                                6377 {
8453         return kmalloc(sizeof(struct nfs4_cli    6378         return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
8454 }                                                6379 }
8455                                                  6380 
8456 bool                                             6381 bool
8457 nfs4_has_reclaimed_state(struct xdr_netobj na !! 6382 nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
8458 {                                                6383 {
8459         struct nfs4_client_reclaim *crp;         6384         struct nfs4_client_reclaim *crp;
8460                                                  6385 
8461         crp = nfsd4_find_reclaim_client(name,    6386         crp = nfsd4_find_reclaim_client(name, nn);
8462         return (crp && crp->cr_clp);             6387         return (crp && crp->cr_clp);
8463 }                                                6388 }
8464                                                  6389 
8465 /*                                               6390 /*
8466  * failure => all reset bets are off, nfserr_    6391  * failure => all reset bets are off, nfserr_no_grace...
8467  *                                            << 
8468  * The caller is responsible for freeing name << 
8469  * will be freed in nfs4_remove_reclaim_recor << 
8470  */                                              6392  */
8471 struct nfs4_client_reclaim *                     6393 struct nfs4_client_reclaim *
8472 nfs4_client_to_reclaim(struct xdr_netobj name !! 6394 nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
8473                 struct nfsd_net *nn)          << 
8474 {                                                6395 {
8475         unsigned int strhashval;                 6396         unsigned int strhashval;
8476         struct nfs4_client_reclaim *crp;         6397         struct nfs4_client_reclaim *crp;
8477                                                  6398 
                                                   >> 6399         dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
8478         crp = alloc_reclaim();                   6400         crp = alloc_reclaim();
8479         if (crp) {                               6401         if (crp) {
8480                 strhashval = clientstr_hashva    6402                 strhashval = clientstr_hashval(name);
8481                 INIT_LIST_HEAD(&crp->cr_strha    6403                 INIT_LIST_HEAD(&crp->cr_strhash);
8482                 list_add(&crp->cr_strhash, &n    6404                 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
8483                 crp->cr_name.data = name.data !! 6405                 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
8484                 crp->cr_name.len = name.len;  << 
8485                 crp->cr_princhash.data = prin << 
8486                 crp->cr_princhash.len = princ << 
8487                 crp->cr_clp = NULL;              6406                 crp->cr_clp = NULL;
8488                 nn->reclaim_str_hashtbl_size+    6407                 nn->reclaim_str_hashtbl_size++;
8489         }                                        6408         }
8490         return crp;                              6409         return crp;
8491 }                                                6410 }
8492                                                  6411 
8493 void                                             6412 void
8494 nfs4_remove_reclaim_record(struct nfs4_client    6413 nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
8495 {                                                6414 {
8496         list_del(&crp->cr_strhash);              6415         list_del(&crp->cr_strhash);
8497         kfree(crp->cr_name.data);             << 
8498         kfree(crp->cr_princhash.data);        << 
8499         kfree(crp);                              6416         kfree(crp);
8500         nn->reclaim_str_hashtbl_size--;          6417         nn->reclaim_str_hashtbl_size--;
8501 }                                                6418 }
8502                                                  6419 
8503 void                                             6420 void
8504 nfs4_release_reclaim(struct nfsd_net *nn)        6421 nfs4_release_reclaim(struct nfsd_net *nn)
8505 {                                                6422 {
8506         struct nfs4_client_reclaim *crp = NUL    6423         struct nfs4_client_reclaim *crp = NULL;
8507         int i;                                   6424         int i;
8508                                                  6425 
8509         for (i = 0; i < CLIENT_HASH_SIZE; i++    6426         for (i = 0; i < CLIENT_HASH_SIZE; i++) {
8510                 while (!list_empty(&nn->recla    6427                 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
8511                         crp = list_entry(nn->    6428                         crp = list_entry(nn->reclaim_str_hashtbl[i].next,
8512                                         struc    6429                                         struct nfs4_client_reclaim, cr_strhash);
8513                         nfs4_remove_reclaim_r    6430                         nfs4_remove_reclaim_record(crp, nn);
8514                 }                                6431                 }
8515         }                                        6432         }
8516         WARN_ON_ONCE(nn->reclaim_str_hashtbl_    6433         WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
8517 }                                                6434 }
8518                                                  6435 
8519 /*                                               6436 /*
8520  * called from OPEN, CLAIM_PREVIOUS with a ne    6437  * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
8521 struct nfs4_client_reclaim *                     6438 struct nfs4_client_reclaim *
8522 nfsd4_find_reclaim_client(struct xdr_netobj n !! 6439 nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
8523 {                                                6440 {
8524         unsigned int strhashval;                 6441         unsigned int strhashval;
8525         struct nfs4_client_reclaim *crp = NUL    6442         struct nfs4_client_reclaim *crp = NULL;
8526                                                  6443 
8527         strhashval = clientstr_hashval(name); !! 6444         dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
                                                   >> 6445 
                                                   >> 6446         strhashval = clientstr_hashval(recdir);
8528         list_for_each_entry(crp, &nn->reclaim    6447         list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
8529                 if (compare_blob(&crp->cr_nam !! 6448                 if (same_name(crp->cr_recdir, recdir)) {
8530                         return crp;              6449                         return crp;
8531                 }                                6450                 }
8532         }                                        6451         }
8533         return NULL;                             6452         return NULL;
8534 }                                                6453 }
8535                                                  6454 
                                                   >> 6455 /*
                                                   >> 6456 * Called from OPEN. Look for clientid in reclaim list.
                                                   >> 6457 */
8536 __be32                                           6458 __be32
8537 nfs4_check_open_reclaim(struct nfs4_client *c !! 6459 nfs4_check_open_reclaim(clientid_t *clid,
                                                   >> 6460                 struct nfsd4_compound_state *cstate,
                                                   >> 6461                 struct nfsd_net *nn)
8538 {                                                6462 {
8539         if (test_bit(NFSD4_CLIENT_RECLAIM_COM !! 6463         __be32 status;
                                                   >> 6464 
                                                   >> 6465         /* find clientid in conf_id_hashtbl */
                                                   >> 6466         status = lookup_clientid(clid, cstate, nn);
                                                   >> 6467         if (status)
                                                   >> 6468                 return nfserr_reclaim_bad;
                                                   >> 6469 
                                                   >> 6470         if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
8540                 return nfserr_no_grace;          6471                 return nfserr_no_grace;
8541                                                  6472 
8542         if (nfsd4_client_record_check(clp))   !! 6473         if (nfsd4_client_record_check(cstate->clp))
8543                 return nfserr_reclaim_bad;       6474                 return nfserr_reclaim_bad;
8544                                                  6475 
8545         return nfs_ok;                           6476         return nfs_ok;
8546 }                                                6477 }
8547                                                  6478 
                                                   >> 6479 #ifdef CONFIG_NFSD_FAULT_INJECTION
                                                   >> 6480 static inline void
                                                   >> 6481 put_client(struct nfs4_client *clp)
                                                   >> 6482 {
                                                   >> 6483         atomic_dec(&clp->cl_refcount);
                                                   >> 6484 }
                                                   >> 6485 
                                                   >> 6486 static struct nfs4_client *
                                                   >> 6487 nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
                                                   >> 6488 {
                                                   >> 6489         struct nfs4_client *clp;
                                                   >> 6490         struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
                                                   >> 6491                                           nfsd_net_id);
                                                   >> 6492 
                                                   >> 6493         if (!nfsd_netns_ready(nn))
                                                   >> 6494                 return NULL;
                                                   >> 6495 
                                                   >> 6496         list_for_each_entry(clp, &nn->client_lru, cl_lru) {
                                                   >> 6497                 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
                                                   >> 6498                         return clp;
                                                   >> 6499         }
                                                   >> 6500         return NULL;
                                                   >> 6501 }
                                                   >> 6502 
                                                   >> 6503 u64
                                                   >> 6504 nfsd_inject_print_clients(void)
                                                   >> 6505 {
                                                   >> 6506         struct nfs4_client *clp;
                                                   >> 6507         u64 count = 0;
                                                   >> 6508         struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
                                                   >> 6509                                           nfsd_net_id);
                                                   >> 6510         char buf[INET6_ADDRSTRLEN];
                                                   >> 6511 
                                                   >> 6512         if (!nfsd_netns_ready(nn))
                                                   >> 6513                 return 0;
                                                   >> 6514 
                                                   >> 6515         spin_lock(&nn->client_lock);
                                                   >> 6516         list_for_each_entry(clp, &nn->client_lru, cl_lru) {
                                                   >> 6517                 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
                                                   >> 6518                 pr_info("NFS Client: %s\n", buf);
                                                   >> 6519                 ++count;
                                                   >> 6520         }
                                                   >> 6521         spin_unlock(&nn->client_lock);
                                                   >> 6522 
                                                   >> 6523         return count;
                                                   >> 6524 }
                                                   >> 6525 
                                                   >> 6526 u64
                                                   >> 6527 nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
                                                   >> 6528 {
                                                   >> 6529         u64 count = 0;
                                                   >> 6530         struct nfs4_client *clp;
                                                   >> 6531         struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
                                                   >> 6532                                           nfsd_net_id);
                                                   >> 6533 
                                                   >> 6534         if (!nfsd_netns_ready(nn))
                                                   >> 6535                 return count;
                                                   >> 6536 
                                                   >> 6537         spin_lock(&nn->client_lock);
                                                   >> 6538         clp = nfsd_find_client(addr, addr_size);
                                                   >> 6539         if (clp) {
                                                   >> 6540                 if (mark_client_expired_locked(clp) == nfs_ok)
                                                   >> 6541                         ++count;
                                                   >> 6542                 else
                                                   >> 6543                         clp = NULL;
                                                   >> 6544         }
                                                   >> 6545         spin_unlock(&nn->client_lock);
                                                   >> 6546 
                                                   >> 6547         if (clp)
                                                   >> 6548                 expire_client(clp);
                                                   >> 6549 
                                                   >> 6550         return count;
                                                   >> 6551 }
                                                   >> 6552 
                                                   >> 6553 u64
                                                   >> 6554 nfsd_inject_forget_clients(u64 max)
                                                   >> 6555 {
                                                   >> 6556         u64 count = 0;
                                                   >> 6557         struct nfs4_client *clp, *next;
                                                   >> 6558         struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
                                                   >> 6559                                                 nfsd_net_id);
                                                   >> 6560         LIST_HEAD(reaplist);
                                                   >> 6561 
                                                   >> 6562         if (!nfsd_netns_ready(nn))
                                                   >> 6563                 return count;
                                                   >> 6564 
                                                   >> 6565         spin_lock(&nn->client_lock);
                                                   >> 6566         list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
                                                   >> 6567                 if (mark_client_expired_locked(clp) == nfs_ok) {
                                                   >> 6568                         list_add(&clp->cl_lru, &reaplist);
                                                   >> 6569                         if (max != 0 && ++count >= max)
                                                   >> 6570                                 break;
                                                   >> 6571                 }
                                                   >> 6572         }
                                                   >> 6573         spin_unlock(&nn->client_lock);
                                                   >> 6574 
                                                   >> 6575         list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
                                                   >> 6576                 expire_client(clp);
                                                   >> 6577 
                                                   >> 6578         return count;
                                                   >> 6579 }
                                                   >> 6580 
                                                   >> 6581 static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
                                                   >> 6582                              const char *type)
                                                   >> 6583 {
                                                   >> 6584         char buf[INET6_ADDRSTRLEN];
                                                   >> 6585         rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
                                                   >> 6586         printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
                                                   >> 6587 }
                                                   >> 6588 
                                                   >> 6589 static void
                                                   >> 6590 nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
                                                   >> 6591                              struct list_head *collect)
                                                   >> 6592 {
                                                   >> 6593         struct nfs4_client *clp = lst->st_stid.sc_client;
                                                   >> 6594         struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
                                                   >> 6595                                           nfsd_net_id);
                                                   >> 6596 
                                                   >> 6597         if (!collect)
                                                   >> 6598                 return;
                                                   >> 6599 
                                                   >> 6600         lockdep_assert_held(&nn->client_lock);
                                                   >> 6601         atomic_inc(&clp->cl_refcount);
                                                   >> 6602         list_add(&lst->st_locks, collect);
                                                   >> 6603 }
                                                   >> 6604 
                                                   >> 6605 static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
                                                   >> 6606                                     struct list_head *collect,
                                                   >> 6607                                     bool (*func)(struct nfs4_ol_stateid *))
                                                   >> 6608 {
                                                   >> 6609         struct nfs4_openowner *oop;
                                                   >> 6610         struct nfs4_ol_stateid *stp, *st_next;
                                                   >> 6611         struct nfs4_ol_stateid *lst, *lst_next;
                                                   >> 6612         u64 count = 0;
                                                   >> 6613 
                                                   >> 6614         spin_lock(&clp->cl_lock);
                                                   >> 6615         list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
                                                   >> 6616                 list_for_each_entry_safe(stp, st_next,
                                                   >> 6617                                 &oop->oo_owner.so_stateids, st_perstateowner) {
                                                   >> 6618                         list_for_each_entry_safe(lst, lst_next,
                                                   >> 6619                                         &stp->st_locks, st_locks) {
                                                   >> 6620                                 if (func) {
                                                   >> 6621                                         if (func(lst))
                                                   >> 6622                                                 nfsd_inject_add_lock_to_list(lst,
                                                   >> 6623                                                                         collect);
                                                   >> 6624                                 }
                                                   >> 6625                                 ++count;
                                                   >> 6626                                 /*
                                                   >> 6627                                  * Despite the fact that these functions deal
                                                   >> 6628                                  * with 64-bit integers for "count", we must
                                                   >> 6629                                  * ensure that it doesn't blow up the
                                                   >> 6630                                  * clp->cl_refcount. Throw a warning if we
                                                   >> 6631                                  * start to approach INT_MAX here.
                                                   >> 6632                                  */
                                                   >> 6633                                 WARN_ON_ONCE(count == (INT_MAX / 2));
                                                   >> 6634                                 if (count == max)
                                                   >> 6635                                         goto out;
                                                   >> 6636                         }
                                                   >> 6637                 }
                                                   >> 6638         }
                                                   >> 6639 out:
                                                   >> 6640         spin_unlock(&clp->cl_lock);
                                                   >> 6641 
                                                   >> 6642         return count;
                                                   >> 6643 }
                                                   >> 6644 
                                                   >> 6645 static u64
                                                   >> 6646 nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
                                                   >> 6647                           u64 max)
                                                   >> 6648 {
                                                   >> 6649         return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
                                                   >> 6650 }
                                                   >> 6651 
                                                   >> 6652 static u64
                                                   >> 6653 nfsd_print_client_locks(struct nfs4_client *clp)
                                                   >> 6654 {
                                                   >> 6655         u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
                                                   >> 6656         nfsd_print_count(clp, count, "locked files");
                                                   >> 6657         return count;
                                                   >> 6658 }
                                                   >> 6659 
                                                   >> 6660 u64
                                                   >> 6661 nfsd_inject_print_locks(void)
                                                   >> 6662 {
                                                   >> 6663         struct nfs4_client *clp;
                                                   >> 6664         u64 count = 0;
                                                   >> 6665         struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
                                                   >> 6666                                                 nfsd_net_id);
                                                   >> 6667 
                                                   >> 6668         if (!nfsd_netns_ready(nn))
                                                   >> 6669                 return 0;
                                                   >> 6670 
                                                   >> 6671         spin_lock(&nn->client_lock);
                                                   >> 6672         list_for_each_entry(clp, &nn->client_lru, cl_lru)
                                                   >> 6673                 count += nfsd_print_client_locks(clp);
                                                   >> 6674         spin_unlock(&nn->client_lock);
                                                   >> 6675 
                                                   >> 6676         return count;
                                                   >> 6677 }
                                                   >> 6678 
                                                   >> 6679 static void
                                                   >> 6680 nfsd_reap_locks(struct list_head *reaplist)
                                                   >> 6681 {
                                                   >> 6682         struct nfs4_client *clp;
                                                   >> 6683         struct nfs4_ol_stateid *stp, *next;
                                                   >> 6684 
                                                   >> 6685         list_for_each_entry_safe(stp, next, reaplist, st_locks) {
                                                   >> 6686                 list_del_init(&stp->st_locks);
                                                   >> 6687                 clp = stp->st_stid.sc_client;
                                                   >> 6688                 nfs4_put_stid(&stp->st_stid);
                                                   >> 6689                 put_client(clp);
                                                   >> 6690         }
                                                   >> 6691 }
                                                   >> 6692 
                                                   >> 6693 u64
                                                   >> 6694 nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
                                                   >> 6695 {
                                                   >> 6696         unsigned int count = 0;
                                                   >> 6697         struct nfs4_client *clp;
                                                   >> 6698         struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
                                                   >> 6699                                                 nfsd_net_id);
                                                   >> 6700         LIST_HEAD(reaplist);
                                                   >> 6701 
                                                   >> 6702         if (!nfsd_netns_ready(nn))
                                                   >> 6703                 return count;
                                                   >> 6704 
                                                   >> 6705         spin_lock(&nn->client_lock);
                                                   >> 6706         clp = nfsd_find_client(addr, addr_size);
                                                   >> 6707         if (clp)
                                                   >> 6708                 count = nfsd_collect_client_locks(clp, &reaplist, 0);
                                                   >> 6709         spin_unlock(&nn->client_lock);
                                                   >> 6710         nfsd_reap_locks(&reaplist);
                                                   >> 6711         return count;
                                                   >> 6712 }
                                                   >> 6713 
                                                   >> 6714 u64
                                                   >> 6715 nfsd_inject_forget_locks(u64 max)
                                                   >> 6716 {
                                                   >> 6717         u64 count = 0;
                                                   >> 6718         struct nfs4_client *clp;
                                                   >> 6719         struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
                                                   >> 6720                                                 nfsd_net_id);
                                                   >> 6721         LIST_HEAD(reaplist);
                                                   >> 6722 
                                                   >> 6723         if (!nfsd_netns_ready(nn))
                                                   >> 6724                 return count;
                                                   >> 6725 
                                                   >> 6726         spin_lock(&nn->client_lock);
                                                   >> 6727         list_for_each_entry(clp, &nn->client_lru, cl_lru) {
                                                   >> 6728                 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
                                                   >> 6729                 if (max != 0 && count >= max)
                                                   >> 6730                         break;
                                                   >> 6731         }
                                                   >> 6732         spin_unlock(&nn->client_lock);
                                                   >> 6733         nfsd_reap_locks(&reaplist);
                                                   >> 6734         return count;
                                                   >> 6735 }
                                                   >> 6736 
                                                   >> 6737 static u64
                                                   >> 6738 nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
                                                   >> 6739                               struct list_head *collect,
                                                   >> 6740                               void (*func)(struct nfs4_openowner *))
                                                   >> 6741 {
                                                   >> 6742         struct nfs4_openowner *oop, *next;
                                                   >> 6743         struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
                                                   >> 6744                                                 nfsd_net_id);
                                                   >> 6745         u64 count = 0;
                                                   >> 6746 
                                                   >> 6747         lockdep_assert_held(&nn->client_lock);
                                                   >> 6748 
                                                   >> 6749         spin_lock(&clp->cl_lock);
                                                   >> 6750         list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
                                                   >> 6751                 if (func) {
                                                   >> 6752                         func(oop);
                                                   >> 6753                         if (collect) {
                                                   >> 6754                                 atomic_inc(&clp->cl_refcount);
                                                   >> 6755                                 list_add(&oop->oo_perclient, collect);
                                                   >> 6756                         }
                                                   >> 6757                 }
                                                   >> 6758                 ++count;
                                                   >> 6759                 /*
                                                   >> 6760                  * Despite the fact that these functions deal with
                                                   >> 6761                  * 64-bit integers for "count", we must ensure that
                                                   >> 6762                  * it doesn't blow up the clp->cl_refcount. Throw a
                                                   >> 6763                  * warning if we start to approach INT_MAX here.
                                                   >> 6764                  */
                                                   >> 6765                 WARN_ON_ONCE(count == (INT_MAX / 2));
                                                   >> 6766                 if (count == max)
                                                   >> 6767                         break;
                                                   >> 6768         }
                                                   >> 6769         spin_unlock(&clp->cl_lock);
                                                   >> 6770 
                                                   >> 6771         return count;
                                                   >> 6772 }
                                                   >> 6773 
                                                   >> 6774 static u64
                                                   >> 6775 nfsd_print_client_openowners(struct nfs4_client *clp)
                                                   >> 6776 {
                                                   >> 6777         u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
                                                   >> 6778 
                                                   >> 6779         nfsd_print_count(clp, count, "openowners");
                                                   >> 6780         return count;
                                                   >> 6781 }
                                                   >> 6782 
                                                   >> 6783 static u64
                                                   >> 6784 nfsd_collect_client_openowners(struct nfs4_client *clp,
                                                   >> 6785                                struct list_head *collect, u64 max)
                                                   >> 6786 {
                                                   >> 6787         return nfsd_foreach_client_openowner(clp, max, collect,
                                                   >> 6788                                                 unhash_openowner_locked);
                                                   >> 6789 }
                                                   >> 6790 
                                                   >> 6791 u64
                                                   >> 6792 nfsd_inject_print_openowners(void)
                                                   >> 6793 {
                                                   >> 6794         struct nfs4_client *clp;
                                                   >> 6795         u64 count = 0;
                                                   >> 6796         struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
                                                   >> 6797                                                 nfsd_net_id);
                                                   >> 6798 
                                                   >> 6799         if (!nfsd_netns_ready(nn))
                                                   >> 6800                 return 0;
                                                   >> 6801 
                                                   >> 6802         spin_lock(&nn->client_lock);
                                                   >> 6803         list_for_each_entry(clp, &nn->client_lru, cl_lru)
                                                   >> 6804                 count += nfsd_print_client_openowners(clp);
                                                   >> 6805         spin_unlock(&nn->client_lock);
                                                   >> 6806 
                                                   >> 6807         return count;
                                                   >> 6808 }
                                                   >> 6809 
                                                   >> 6810 static void
                                                   >> 6811 nfsd_reap_openowners(struct list_head *reaplist)
                                                   >> 6812 {
                                                   >> 6813         struct nfs4_client *clp;
                                                   >> 6814         struct nfs4_openowner *oop, *next;
                                                   >> 6815 
                                                   >> 6816         list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
                                                   >> 6817                 list_del_init(&oop->oo_perclient);
                                                   >> 6818                 clp = oop->oo_owner.so_client;
                                                   >> 6819                 release_openowner(oop);
                                                   >> 6820                 put_client(clp);
                                                   >> 6821         }
                                                   >> 6822 }
                                                   >> 6823 
                                                   >> 6824 u64
                                                   >> 6825 nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
                                                   >> 6826                                      size_t addr_size)
                                                   >> 6827 {
                                                   >> 6828         unsigned int count = 0;
                                                   >> 6829         struct nfs4_client *clp;
                                                   >> 6830         struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
                                                   >> 6831                                                 nfsd_net_id);
                                                   >> 6832         LIST_HEAD(reaplist);
                                                   >> 6833 
                                                   >> 6834         if (!nfsd_netns_ready(nn))
                                                   >> 6835                 return count;
                                                   >> 6836 
                                                   >> 6837         spin_lock(&nn->client_lock);
                                                   >> 6838         clp = nfsd_find_client(addr, addr_size);
                                                   >> 6839         if (clp)
                                                   >> 6840                 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
                                                   >> 6841         spin_unlock(&nn->client_lock);
                                                   >> 6842         nfsd_reap_openowners(&reaplist);
                                                   >> 6843         return count;
                                                   >> 6844 }
                                                   >> 6845 
                                                   >> 6846 u64
                                                   >> 6847 nfsd_inject_forget_openowners(u64 max)
                                                   >> 6848 {
                                                   >> 6849         u64 count = 0;
                                                   >> 6850         struct nfs4_client *clp;
                                                   >> 6851         struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
                                                   >> 6852                                                 nfsd_net_id);
                                                   >> 6853         LIST_HEAD(reaplist);
                                                   >> 6854 
                                                   >> 6855         if (!nfsd_netns_ready(nn))
                                                   >> 6856                 return count;
                                                   >> 6857 
                                                   >> 6858         spin_lock(&nn->client_lock);
                                                   >> 6859         list_for_each_entry(clp, &nn->client_lru, cl_lru) {
                                                   >> 6860                 count += nfsd_collect_client_openowners(clp, &reaplist,
                                                   >> 6861                                                         max - count);
                                                   >> 6862                 if (max != 0 && count >= max)
                                                   >> 6863                         break;
                                                   >> 6864         }
                                                   >> 6865         spin_unlock(&nn->client_lock);
                                                   >> 6866         nfsd_reap_openowners(&reaplist);
                                                   >> 6867         return count;
                                                   >> 6868 }
                                                   >> 6869 
                                                   >> 6870 static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
                                                   >> 6871                                      struct list_head *victims)
                                                   >> 6872 {
                                                   >> 6873         struct nfs4_delegation *dp, *next;
                                                   >> 6874         struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
                                                   >> 6875                                                 nfsd_net_id);
                                                   >> 6876         u64 count = 0;
                                                   >> 6877 
                                                   >> 6878         lockdep_assert_held(&nn->client_lock);
                                                   >> 6879 
                                                   >> 6880         spin_lock(&state_lock);
                                                   >> 6881         list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
                                                   >> 6882                 if (victims) {
                                                   >> 6883                         /*
                                                   >> 6884                          * It's not safe to mess with delegations that have a
                                                   >> 6885                          * non-zero dl_time. They might have already been broken
                                                   >> 6886                          * and could be processed by the laundromat outside of
                                                   >> 6887                          * the state_lock. Just leave them be.
                                                   >> 6888                          */
                                                   >> 6889                         if (dp->dl_time != 0)
                                                   >> 6890                                 continue;
                                                   >> 6891 
                                                   >> 6892                         atomic_inc(&clp->cl_refcount);
                                                   >> 6893                         WARN_ON(!unhash_delegation_locked(dp));
                                                   >> 6894                         list_add(&dp->dl_recall_lru, victims);
                                                   >> 6895                 }
                                                   >> 6896                 ++count;
                                                   >> 6897                 /*
                                                   >> 6898                  * Despite the fact that these functions deal with
                                                   >> 6899                  * 64-bit integers for "count", we must ensure that
                                                   >> 6900                  * it doesn't blow up the clp->cl_refcount. Throw a
                                                   >> 6901                  * warning if we start to approach INT_MAX here.
                                                   >> 6902                  */
                                                   >> 6903                 WARN_ON_ONCE(count == (INT_MAX / 2));
                                                   >> 6904                 if (count == max)
                                                   >> 6905                         break;
                                                   >> 6906         }
                                                   >> 6907         spin_unlock(&state_lock);
                                                   >> 6908         return count;
                                                   >> 6909 }
                                                   >> 6910 
                                                   >> 6911 static u64
                                                   >> 6912 nfsd_print_client_delegations(struct nfs4_client *clp)
                                                   >> 6913 {
                                                   >> 6914         u64 count = nfsd_find_all_delegations(clp, 0, NULL);
                                                   >> 6915 
                                                   >> 6916         nfsd_print_count(clp, count, "delegations");
                                                   >> 6917         return count;
                                                   >> 6918 }
                                                   >> 6919 
                                                   >> 6920 u64
                                                   >> 6921 nfsd_inject_print_delegations(void)
                                                   >> 6922 {
                                                   >> 6923         struct nfs4_client *clp;
                                                   >> 6924         u64 count = 0;
                                                   >> 6925         struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
                                                   >> 6926                                                 nfsd_net_id);
                                                   >> 6927 
                                                   >> 6928         if (!nfsd_netns_ready(nn))
                                                   >> 6929                 return 0;
                                                   >> 6930 
                                                   >> 6931         spin_lock(&nn->client_lock);
                                                   >> 6932         list_for_each_entry(clp, &nn->client_lru, cl_lru)
                                                   >> 6933                 count += nfsd_print_client_delegations(clp);
                                                   >> 6934         spin_unlock(&nn->client_lock);
                                                   >> 6935 
                                                   >> 6936         return count;
                                                   >> 6937 }
                                                   >> 6938 
                                                   >> 6939 static void
                                                   >> 6940 nfsd_forget_delegations(struct list_head *reaplist)
                                                   >> 6941 {
                                                   >> 6942         struct nfs4_client *clp;
                                                   >> 6943         struct nfs4_delegation *dp, *next;
                                                   >> 6944 
                                                   >> 6945         list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
                                                   >> 6946                 list_del_init(&dp->dl_recall_lru);
                                                   >> 6947                 clp = dp->dl_stid.sc_client;
                                                   >> 6948                 revoke_delegation(dp);
                                                   >> 6949                 put_client(clp);
                                                   >> 6950         }
                                                   >> 6951 }
                                                   >> 6952 
                                                   >> 6953 u64
                                                   >> 6954 nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
                                                   >> 6955                                       size_t addr_size)
                                                   >> 6956 {
                                                   >> 6957         u64 count = 0;
                                                   >> 6958         struct nfs4_client *clp;
                                                   >> 6959         struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
                                                   >> 6960                                                 nfsd_net_id);
                                                   >> 6961         LIST_HEAD(reaplist);
                                                   >> 6962 
                                                   >> 6963         if (!nfsd_netns_ready(nn))
                                                   >> 6964                 return count;
                                                   >> 6965 
                                                   >> 6966         spin_lock(&nn->client_lock);
                                                   >> 6967         clp = nfsd_find_client(addr, addr_size);
                                                   >> 6968         if (clp)
                                                   >> 6969                 count = nfsd_find_all_delegations(clp, 0, &reaplist);
                                                   >> 6970         spin_unlock(&nn->client_lock);
                                                   >> 6971 
                                                   >> 6972         nfsd_forget_delegations(&reaplist);
                                                   >> 6973         return count;
                                                   >> 6974 }
                                                   >> 6975 
                                                   >> 6976 u64
                                                   >> 6977 nfsd_inject_forget_delegations(u64 max)
                                                   >> 6978 {
                                                   >> 6979         u64 count = 0;
                                                   >> 6980         struct nfs4_client *clp;
                                                   >> 6981         struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
                                                   >> 6982                                                 nfsd_net_id);
                                                   >> 6983         LIST_HEAD(reaplist);
                                                   >> 6984 
                                                   >> 6985         if (!nfsd_netns_ready(nn))
                                                   >> 6986                 return count;
                                                   >> 6987 
                                                   >> 6988         spin_lock(&nn->client_lock);
                                                   >> 6989         list_for_each_entry(clp, &nn->client_lru, cl_lru) {
                                                   >> 6990                 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
                                                   >> 6991                 if (max != 0 && count >= max)
                                                   >> 6992                         break;
                                                   >> 6993         }
                                                   >> 6994         spin_unlock(&nn->client_lock);
                                                   >> 6995         nfsd_forget_delegations(&reaplist);
                                                   >> 6996         return count;
                                                   >> 6997 }
                                                   >> 6998 
                                                   >> 6999 static void
                                                   >> 7000 nfsd_recall_delegations(struct list_head *reaplist)
                                                   >> 7001 {
                                                   >> 7002         struct nfs4_client *clp;
                                                   >> 7003         struct nfs4_delegation *dp, *next;
                                                   >> 7004 
                                                   >> 7005         list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
                                                   >> 7006                 list_del_init(&dp->dl_recall_lru);
                                                   >> 7007                 clp = dp->dl_stid.sc_client;
                                                   >> 7008                 /*
                                                   >> 7009                  * We skipped all entries that had a zero dl_time before,
                                                   >> 7010                  * so we can now reset the dl_time back to 0. If a delegation
                                                   >> 7011                  * break comes in now, then it won't make any difference since
                                                   >> 7012                  * we're recalling it either way.
                                                   >> 7013                  */
                                                   >> 7014                 spin_lock(&state_lock);
                                                   >> 7015                 dp->dl_time = 0;
                                                   >> 7016                 spin_unlock(&state_lock);
                                                   >> 7017                 nfsd_break_one_deleg(dp);
                                                   >> 7018                 put_client(clp);
                                                   >> 7019         }
                                                   >> 7020 }
                                                   >> 7021 
                                                   >> 7022 u64
                                                   >> 7023 nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
                                                   >> 7024                                       size_t addr_size)
                                                   >> 7025 {
                                                   >> 7026         u64 count = 0;
                                                   >> 7027         struct nfs4_client *clp;
                                                   >> 7028         struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
                                                   >> 7029                                                 nfsd_net_id);
                                                   >> 7030         LIST_HEAD(reaplist);
                                                   >> 7031 
                                                   >> 7032         if (!nfsd_netns_ready(nn))
                                                   >> 7033                 return count;
                                                   >> 7034 
                                                   >> 7035         spin_lock(&nn->client_lock);
                                                   >> 7036         clp = nfsd_find_client(addr, addr_size);
                                                   >> 7037         if (clp)
                                                   >> 7038                 count = nfsd_find_all_delegations(clp, 0, &reaplist);
                                                   >> 7039         spin_unlock(&nn->client_lock);
                                                   >> 7040 
                                                   >> 7041         nfsd_recall_delegations(&reaplist);
                                                   >> 7042         return count;
                                                   >> 7043 }
                                                   >> 7044 
                                                   >> 7045 u64
                                                   >> 7046 nfsd_inject_recall_delegations(u64 max)
                                                   >> 7047 {
                                                   >> 7048         u64 count = 0;
                                                   >> 7049         struct nfs4_client *clp, *next;
                                                   >> 7050         struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
                                                   >> 7051                                                 nfsd_net_id);
                                                   >> 7052         LIST_HEAD(reaplist);
                                                   >> 7053 
                                                   >> 7054         if (!nfsd_netns_ready(nn))
                                                   >> 7055                 return count;
                                                   >> 7056 
                                                   >> 7057         spin_lock(&nn->client_lock);
                                                   >> 7058         list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
                                                   >> 7059                 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
                                                   >> 7060                 if (max != 0 && ++count >= max)
                                                   >> 7061                         break;
                                                   >> 7062         }
                                                   >> 7063         spin_unlock(&nn->client_lock);
                                                   >> 7064         nfsd_recall_delegations(&reaplist);
                                                   >> 7065         return count;
                                                   >> 7066 }
                                                   >> 7067 #endif /* CONFIG_NFSD_FAULT_INJECTION */
                                                   >> 7068 
8548 /*                                               7069 /*
8549  * Since the lifetime of a delegation isn't l    7070  * 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    7071  * client may quite reasonably hang on to a delegation as long as it has
8551  * the inode cached.  This becomes an obvious    7072  * the inode cached.  This becomes an obvious problem the first time a
8552  * client's inode cache approaches the size o    7073  * client's inode cache approaches the size of the server's total memory.
8553  *                                               7074  *
8554  * For now we avoid this problem by imposing     7075  * For now we avoid this problem by imposing a hard limit on the number
8555  * of delegations, which varies according to     7076  * of delegations, which varies according to the server's memory size.
8556  */                                              7077  */
8557 static void                                      7078 static void
8558 set_max_delegations(void)                        7079 set_max_delegations(void)
8559 {                                                7080 {
8560         /*                                       7081         /*
8561          * Allow at most 4 delegations per me    7082          * Allow at most 4 delegations per megabyte of RAM.  Quick
8562          * estimates suggest that in the wors    7083          * estimates suggest that in the worst case (where every delegation
8563          * is for a different inode), a deleg    7084          * is for a different inode), a delegation could take about 1.5K,
8564          * giving a worst case usage of about    7085          * giving a worst case usage of about 6% of memory.
8565          */                                      7086          */
8566         max_delegations = nr_free_buffer_page    7087         max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
8567 }                                                7088 }
8568                                                  7089 
8569 static int nfs4_state_create_net(struct net *    7090 static int nfs4_state_create_net(struct net *net)
8570 {                                                7091 {
8571         struct nfsd_net *nn = net_generic(net    7092         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
8572         int i;                                   7093         int i;
8573                                                  7094 
8574         nn->conf_id_hashtbl = kmalloc_array(C !! 7095         nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
8575                                             s !! 7096                         CLIENT_HASH_SIZE, GFP_KERNEL);
8576                                             G << 
8577         if (!nn->conf_id_hashtbl)                7097         if (!nn->conf_id_hashtbl)
8578                 goto err;                        7098                 goto err;
8579         nn->unconf_id_hashtbl = kmalloc_array !! 7099         nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
8580                                               !! 7100                         CLIENT_HASH_SIZE, GFP_KERNEL);
8581                                               << 
8582         if (!nn->unconf_id_hashtbl)              7101         if (!nn->unconf_id_hashtbl)
8583                 goto err_unconf_id;              7102                 goto err_unconf_id;
8584         nn->sessionid_hashtbl = kmalloc_array !! 7103         nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
8585                                               !! 7104                         SESSION_HASH_SIZE, GFP_KERNEL);
8586                                               << 
8587         if (!nn->sessionid_hashtbl)              7105         if (!nn->sessionid_hashtbl)
8588                 goto err_sessionid;              7106                 goto err_sessionid;
8589                                                  7107 
8590         for (i = 0; i < CLIENT_HASH_SIZE; i++    7108         for (i = 0; i < CLIENT_HASH_SIZE; i++) {
8591                 INIT_LIST_HEAD(&nn->conf_id_h    7109                 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
8592                 INIT_LIST_HEAD(&nn->unconf_id    7110                 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
8593         }                                        7111         }
8594         for (i = 0; i < SESSION_HASH_SIZE; i+    7112         for (i = 0; i < SESSION_HASH_SIZE; i++)
8595                 INIT_LIST_HEAD(&nn->sessionid    7113                 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
8596         nn->conf_name_tree = RB_ROOT;            7114         nn->conf_name_tree = RB_ROOT;
8597         nn->unconf_name_tree = RB_ROOT;          7115         nn->unconf_name_tree = RB_ROOT;
8598         nn->boot_time = ktime_get_real_second !! 7116         nn->boot_time = get_seconds();
8599         nn->grace_ended = false;                 7117         nn->grace_ended = false;
8600         nn->nfsd4_manager.block_opens = true;    7118         nn->nfsd4_manager.block_opens = true;
8601         INIT_LIST_HEAD(&nn->nfsd4_manager.lis    7119         INIT_LIST_HEAD(&nn->nfsd4_manager.list);
8602         INIT_LIST_HEAD(&nn->client_lru);         7120         INIT_LIST_HEAD(&nn->client_lru);
8603         INIT_LIST_HEAD(&nn->close_lru);          7121         INIT_LIST_HEAD(&nn->close_lru);
8604         INIT_LIST_HEAD(&nn->del_recall_lru);     7122         INIT_LIST_HEAD(&nn->del_recall_lru);
8605         spin_lock_init(&nn->client_lock);        7123         spin_lock_init(&nn->client_lock);
8606         spin_lock_init(&nn->s2s_cp_lock);     << 
8607         idr_init(&nn->s2s_cp_stateids);       << 
8608         atomic_set(&nn->pending_async_copies, << 
8609                                                  7124 
8610         spin_lock_init(&nn->blocked_locks_loc    7125         spin_lock_init(&nn->blocked_locks_lock);
8611         INIT_LIST_HEAD(&nn->blocked_locks_lru    7126         INIT_LIST_HEAD(&nn->blocked_locks_lru);
8612                                                  7127 
8613         INIT_DELAYED_WORK(&nn->laundromat_wor    7128         INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
8614         INIT_WORK(&nn->nfsd_shrinker_work, nf << 
8615         get_net(net);                            7129         get_net(net);
8616                                                  7130 
8617         nn->nfsd_client_shrinker = shrinker_a << 
8618         if (!nn->nfsd_client_shrinker)        << 
8619                 goto err_shrinker;            << 
8620                                               << 
8621         nn->nfsd_client_shrinker->scan_object << 
8622         nn->nfsd_client_shrinker->count_objec << 
8623         nn->nfsd_client_shrinker->private_dat << 
8624                                               << 
8625         shrinker_register(nn->nfsd_client_shr << 
8626                                               << 
8627         return 0;                                7131         return 0;
8628                                                  7132 
8629 err_shrinker:                                 << 
8630         put_net(net);                         << 
8631         kfree(nn->sessionid_hashtbl);         << 
8632 err_sessionid:                                   7133 err_sessionid:
8633         kfree(nn->unconf_id_hashtbl);            7134         kfree(nn->unconf_id_hashtbl);
8634 err_unconf_id:                                   7135 err_unconf_id:
8635         kfree(nn->conf_id_hashtbl);              7136         kfree(nn->conf_id_hashtbl);
8636 err:                                             7137 err:
8637         return -ENOMEM;                          7138         return -ENOMEM;
8638 }                                                7139 }
8639                                                  7140 
8640 static void                                      7141 static void
8641 nfs4_state_destroy_net(struct net *net)          7142 nfs4_state_destroy_net(struct net *net)
8642 {                                                7143 {
8643         int i;                                   7144         int i;
8644         struct nfs4_client *clp = NULL;          7145         struct nfs4_client *clp = NULL;
8645         struct nfsd_net *nn = net_generic(net    7146         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
8646                                                  7147 
8647         for (i = 0; i < CLIENT_HASH_SIZE; i++    7148         for (i = 0; i < CLIENT_HASH_SIZE; i++) {
8648                 while (!list_empty(&nn->conf_    7149                 while (!list_empty(&nn->conf_id_hashtbl[i])) {
8649                         clp = list_entry(nn->    7150                         clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
8650                         destroy_client(clp);     7151                         destroy_client(clp);
8651                 }                                7152                 }
8652         }                                        7153         }
8653                                                  7154 
8654         WARN_ON(!list_empty(&nn->blocked_lock    7155         WARN_ON(!list_empty(&nn->blocked_locks_lru));
8655                                                  7156 
8656         for (i = 0; i < CLIENT_HASH_SIZE; i++    7157         for (i = 0; i < CLIENT_HASH_SIZE; i++) {
8657                 while (!list_empty(&nn->uncon    7158                 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
8658                         clp = list_entry(nn->    7159                         clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
8659                         destroy_client(clp);     7160                         destroy_client(clp);
8660                 }                                7161                 }
8661         }                                        7162         }
8662                                                  7163 
8663         kfree(nn->sessionid_hashtbl);            7164         kfree(nn->sessionid_hashtbl);
8664         kfree(nn->unconf_id_hashtbl);            7165         kfree(nn->unconf_id_hashtbl);
8665         kfree(nn->conf_id_hashtbl);              7166         kfree(nn->conf_id_hashtbl);
8666         put_net(net);                            7167         put_net(net);
8667 }                                                7168 }
8668                                                  7169 
8669 int                                              7170 int
8670 nfs4_state_start_net(struct net *net)            7171 nfs4_state_start_net(struct net *net)
8671 {                                                7172 {
8672         struct nfsd_net *nn = net_generic(net    7173         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
8673         int ret;                                 7174         int ret;
8674                                                  7175 
8675         ret = nfs4_state_create_net(net);        7176         ret = nfs4_state_create_net(net);
8676         if (ret)                                 7177         if (ret)
8677                 return ret;                      7178                 return ret;
8678         locks_start_grace(net, &nn->nfsd4_man    7179         locks_start_grace(net, &nn->nfsd4_manager);
8679         nfsd4_client_tracking_init(net);         7180         nfsd4_client_tracking_init(net);
8680         if (nn->track_reclaim_completes && nn !! 7181         printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
8681                 goto skip_grace;              !! 7182                nn->nfsd4_grace, net);
8682         printk(KERN_INFO "NFSD: starting %lld << 
8683                nn->nfsd4_grace, net->ns.inum) << 
8684         trace_nfsd_grace_start(nn);           << 
8685         queue_delayed_work(laundry_wq, &nn->l    7183         queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
8686         return 0;                                7184         return 0;
8687                                               << 
8688 skip_grace:                                   << 
8689         printk(KERN_INFO "NFSD: no clients to << 
8690                         net->ns.inum);        << 
8691         queue_delayed_work(laundry_wq, &nn->l << 
8692         nfsd4_end_grace(nn);                  << 
8693         return 0;                             << 
8694 }                                                7185 }
8695                                                  7186 
8696 /* initialization to perform when the nfsd se    7187 /* initialization to perform when the nfsd service is started: */
8697                                                  7188 
8698 int                                              7189 int
8699 nfs4_state_start(void)                           7190 nfs4_state_start(void)
8700 {                                                7191 {
8701         int ret;                                 7192         int ret;
8702                                                  7193 
8703         ret = rhltable_init(&nfs4_file_rhltab !! 7194         ret = set_callback_cred();
8704         if (ret)                                 7195         if (ret)
8705                 return ret;                      7196                 return ret;
8706                                                  7197 
                                                   >> 7198         laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
                                                   >> 7199         if (laundry_wq == NULL) {
                                                   >> 7200                 ret = -ENOMEM;
                                                   >> 7201                 goto out_cleanup_cred;
                                                   >> 7202         }
                                                   >> 7203         ret = nfsd4_create_callback_queue();
                                                   >> 7204         if (ret)
                                                   >> 7205                 goto out_free_laundry;
                                                   >> 7206 
8707         set_max_delegations();                   7207         set_max_delegations();
8708         return 0;                                7208         return 0;
                                                   >> 7209 
                                                   >> 7210 out_free_laundry:
                                                   >> 7211         destroy_workqueue(laundry_wq);
                                                   >> 7212 out_cleanup_cred:
                                                   >> 7213         cleanup_callback_cred();
                                                   >> 7214         return ret;
8709 }                                                7215 }
8710                                                  7216 
8711 void                                             7217 void
8712 nfs4_state_shutdown_net(struct net *net)         7218 nfs4_state_shutdown_net(struct net *net)
8713 {                                                7219 {
8714         struct nfs4_delegation *dp = NULL;       7220         struct nfs4_delegation *dp = NULL;
8715         struct list_head *pos, *next, reaplis    7221         struct list_head *pos, *next, reaplist;
8716         struct nfsd_net *nn = net_generic(net    7222         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
8717                                                  7223 
8718         shrinker_free(nn->nfsd_client_shrinke << 
8719         cancel_work_sync(&nn->nfsd_shrinker_w << 
8720         cancel_delayed_work_sync(&nn->laundro    7224         cancel_delayed_work_sync(&nn->laundromat_work);
8721         locks_end_grace(&nn->nfsd4_manager);     7225         locks_end_grace(&nn->nfsd4_manager);
8722                                                  7226 
8723         INIT_LIST_HEAD(&reaplist);               7227         INIT_LIST_HEAD(&reaplist);
8724         spin_lock(&state_lock);                  7228         spin_lock(&state_lock);
8725         list_for_each_safe(pos, next, &nn->de    7229         list_for_each_safe(pos, next, &nn->del_recall_lru) {
8726                 dp = list_entry (pos, struct     7230                 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
8727                 unhash_delegation_locked(dp,  !! 7231                 WARN_ON(!unhash_delegation_locked(dp));
8728                 list_add(&dp->dl_recall_lru,     7232                 list_add(&dp->dl_recall_lru, &reaplist);
8729         }                                        7233         }
8730         spin_unlock(&state_lock);                7234         spin_unlock(&state_lock);
8731         list_for_each_safe(pos, next, &reapli    7235         list_for_each_safe(pos, next, &reaplist) {
8732                 dp = list_entry (pos, struct     7236                 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
8733                 list_del_init(&dp->dl_recall_    7237                 list_del_init(&dp->dl_recall_lru);
8734                 destroy_unhashed_deleg(dp);   !! 7238                 put_clnt_odstate(dp->dl_clnt_odstate);
                                                   >> 7239                 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
                                                   >> 7240                 nfs4_put_stid(&dp->dl_stid);
8735         }                                        7241         }
8736                                                  7242 
8737         nfsd4_client_tracking_exit(net);         7243         nfsd4_client_tracking_exit(net);
8738         nfs4_state_destroy_net(net);             7244         nfs4_state_destroy_net(net);
8739 #ifdef CONFIG_NFSD_V4_2_INTER_SSC             << 
8740         nfsd4_ssc_shutdown_umount(nn);        << 
8741 #endif                                        << 
8742 }                                                7245 }
8743                                                  7246 
8744 void                                             7247 void
8745 nfs4_state_shutdown(void)                        7248 nfs4_state_shutdown(void)
8746 {                                                7249 {
8747         rhltable_destroy(&nfs4_file_rhltable) !! 7250         destroy_workqueue(laundry_wq);
                                                   >> 7251         nfsd4_destroy_callback_queue();
                                                   >> 7252         cleanup_callback_cred();
8748 }                                                7253 }
8749                                                  7254 
8750 static void                                      7255 static void
8751 get_stateid(struct nfsd4_compound_state *csta    7256 get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
8752 {                                                7257 {
8753         if (HAS_CSTATE_FLAG(cstate, CURRENT_S !! 7258         if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
8754             CURRENT_STATEID(stateid))         << 
8755                 memcpy(stateid, &cstate->curr    7259                 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
8756 }                                                7260 }
8757                                                  7261 
8758 static void                                      7262 static void
8759 put_stateid(struct nfsd4_compound_state *csta    7263 put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
8760 {                                                7264 {
8761         if (cstate->minorversion) {              7265         if (cstate->minorversion) {
8762                 memcpy(&cstate->current_state    7266                 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
8763                 SET_CSTATE_FLAG(cstate, CURRE !! 7267                 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
8764         }                                        7268         }
8765 }                                                7269 }
8766                                                  7270 
8767 void                                             7271 void
8768 clear_current_stateid(struct nfsd4_compound_s    7272 clear_current_stateid(struct nfsd4_compound_state *cstate)
8769 {                                                7273 {
8770         CLEAR_CSTATE_FLAG(cstate, CURRENT_STA !! 7274         CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
8771 }                                                7275 }
8772                                                  7276 
8773 /*                                               7277 /*
8774  * functions to set current state id             7278  * functions to set current state id
8775  */                                              7279  */
8776 void                                             7280 void
8777 nfsd4_set_opendowngradestateid(struct nfsd4_c !! 7281 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
8778                 union nfsd4_op_u *u)          << 
8779 {                                                7282 {
8780         put_stateid(cstate, &u->open_downgrad !! 7283         put_stateid(cstate, &odp->od_stateid);
8781 }                                                7284 }
8782                                                  7285 
8783 void                                             7286 void
8784 nfsd4_set_openstateid(struct nfsd4_compound_s !! 7287 nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
8785                 union nfsd4_op_u *u)          << 
8786 {                                                7288 {
8787         put_stateid(cstate, &u->open.op_state !! 7289         put_stateid(cstate, &open->op_stateid);
8788 }                                                7290 }
8789                                                  7291 
8790 void                                             7292 void
8791 nfsd4_set_closestateid(struct nfsd4_compound_ !! 7293 nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
8792                 union nfsd4_op_u *u)          << 
8793 {                                                7294 {
8794         put_stateid(cstate, &u->close.cl_stat !! 7295         put_stateid(cstate, &close->cl_stateid);
8795 }                                                7296 }
8796                                                  7297 
8797 void                                             7298 void
8798 nfsd4_set_lockstateid(struct nfsd4_compound_s !! 7299 nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
8799                 union nfsd4_op_u *u)          << 
8800 {                                                7300 {
8801         put_stateid(cstate, &u->lock.lk_resp_ !! 7301         put_stateid(cstate, &lock->lk_resp_stateid);
8802 }                                                7302 }
8803                                                  7303 
8804 /*                                               7304 /*
8805  * functions to consume current state id         7305  * functions to consume current state id
8806  */                                              7306  */
8807                                                  7307 
8808 void                                             7308 void
8809 nfsd4_get_opendowngradestateid(struct nfsd4_c !! 7309 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
8810                 union nfsd4_op_u *u)          << 
8811 {                                                7310 {
8812         get_stateid(cstate, &u->open_downgrad !! 7311         get_stateid(cstate, &odp->od_stateid);
8813 }                                                7312 }
8814                                                  7313 
8815 void                                             7314 void
8816 nfsd4_get_delegreturnstateid(struct nfsd4_com !! 7315 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
8817                 union nfsd4_op_u *u)          << 
8818 {                                                7316 {
8819         get_stateid(cstate, &u->delegreturn.d !! 7317         get_stateid(cstate, &drp->dr_stateid);
8820 }                                                7318 }
8821                                                  7319 
8822 void                                             7320 void
8823 nfsd4_get_freestateid(struct nfsd4_compound_s !! 7321 nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
8824                 union nfsd4_op_u *u)          << 
8825 {                                                7322 {
8826         get_stateid(cstate, &u->free_stateid. !! 7323         get_stateid(cstate, &fsp->fr_stateid);
8827 }                                                7324 }
8828                                                  7325 
8829 void                                             7326 void
8830 nfsd4_get_setattrstateid(struct nfsd4_compoun !! 7327 nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
8831                 union nfsd4_op_u *u)          << 
8832 {                                                7328 {
8833         get_stateid(cstate, &u->setattr.sa_st !! 7329         get_stateid(cstate, &setattr->sa_stateid);
8834 }                                                7330 }
8835                                                  7331 
8836 void                                             7332 void
8837 nfsd4_get_closestateid(struct nfsd4_compound_ !! 7333 nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
8838                 union nfsd4_op_u *u)          << 
8839 {                                                7334 {
8840         get_stateid(cstate, &u->close.cl_stat !! 7335         get_stateid(cstate, &close->cl_stateid);
8841 }                                                7336 }
8842                                                  7337 
8843 void                                             7338 void
8844 nfsd4_get_lockustateid(struct nfsd4_compound_ !! 7339 nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
8845                 union nfsd4_op_u *u)          << 
8846 {                                                7340 {
8847         get_stateid(cstate, &u->locku.lu_stat !! 7341         get_stateid(cstate, &locku->lu_stateid);
8848 }                                                7342 }
8849                                                  7343 
8850 void                                             7344 void
8851 nfsd4_get_readstateid(struct nfsd4_compound_s !! 7345 nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
8852                 union nfsd4_op_u *u)          << 
8853 {                                                7346 {
8854         get_stateid(cstate, &u->read.rd_state !! 7347         get_stateid(cstate, &read->rd_stateid);
8855 }                                                7348 }
8856                                                  7349 
8857 void                                             7350 void
8858 nfsd4_get_writestateid(struct nfsd4_compound_ !! 7351 nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
8859                 union nfsd4_op_u *u)          << 
8860 {                                                7352 {
8861         get_stateid(cstate, &u->write.wr_stat !! 7353         get_stateid(cstate, &write->wr_stateid);
8862 }                                             << 
8863                                               << 
8864 /**                                           << 
8865  * nfsd4_deleg_getattr_conflict - Recall if G << 
8866  * @rqstp: RPC transaction context            << 
8867  * @dentry: dentry of inode to be checked for << 
8868  * @modified: return true if file was modifie << 
8869  * @size: new size of file if modified is tru << 
8870  *                                            << 
8871  * This function is called when there is a co << 
8872  * delegation and a change/size GETATTR from  << 
8873  * must either use the CB_GETATTR to get the  << 
8874  * attributes from the client that holds the  << 
8875  * delegation before replying to the GETATTR. << 
8876  * 18.7.4.                                    << 
8877  *                                            << 
8878  * Returns 0 if there is no conflict; otherwi << 
8879  * code is returned.                          << 
8880  */                                           << 
8881 __be32                                        << 
8882 nfsd4_deleg_getattr_conflict(struct svc_rqst  << 
8883                                 bool *modifie << 
8884 {                                             << 
8885         __be32 status;                        << 
8886         struct nfsd_net *nn = net_generic(SVC << 
8887         struct file_lock_context *ctx;        << 
8888         struct nfs4_delegation *dp = NULL;    << 
8889         struct file_lease *fl;                << 
8890         struct iattr attrs;                   << 
8891         struct nfs4_cb_fattr *ncf;            << 
8892         struct inode *inode = d_inode(dentry) << 
8893                                               << 
8894         *modified = false;                    << 
8895         ctx = locks_inode_context(inode);     << 
8896         if (!ctx)                             << 
8897                 return 0;                     << 
8898                                               << 
8899 #define NON_NFSD_LEASE ((void *)1)            << 
8900                                               << 
8901         spin_lock(&ctx->flc_lock);            << 
8902         for_each_file_lock(fl, &ctx->flc_leas << 
8903                 if (fl->c.flc_flags == FL_LAY << 
8904                         continue;             << 
8905                 if (fl->c.flc_type == F_WRLCK << 
8906                         if (fl->fl_lmops == & << 
8907                                 dp = fl->c.fl << 
8908                         else                  << 
8909                                 dp = NON_NFSD << 
8910                 }                             << 
8911                 break;                        << 
8912         }                                     << 
8913         if (dp == NULL || dp == NON_NFSD_LEAS << 
8914             dp->dl_recall.cb_clp == *(rqstp-> << 
8915                 spin_unlock(&ctx->flc_lock);  << 
8916                 if (dp == NON_NFSD_LEASE) {   << 
8917                         status = nfserrno(nfs << 
8918                                               << 
8919                         if (status != nfserr_ << 
8920                             !nfsd_wait_for_de << 
8921                                 return status << 
8922                 }                             << 
8923                 return 0;                     << 
8924         }                                     << 
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);          << 
8931                                               << 
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 }                                                7354 }
8971                                                  7355 

~ [ 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