Merge tag 'spi-fix-v6.7-rc7' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie/spi
[linux-2.6-microblaze.git] / fs / nfsd / nfs4state.c
1 /*
2 *  Copyright (c) 2001 The Regents of the University of Michigan.
3 *  All rights reserved.
4 *
5 *  Kendrick Smith <kmsmith@umich.edu>
6 *  Andy Adamson <kandros@umich.edu>
7 *
8 *  Redistribution and use in source and binary forms, with or without
9 *  modification, are permitted provided that the following conditions
10 *  are met:
11 *
12 *  1. Redistributions of source code must retain the above copyright
13 *     notice, this list of conditions and the following disclaimer.
14 *  2. Redistributions in binary form must reproduce the above copyright
15 *     notice, this list of conditions and the following disclaimer in the
16 *     documentation and/or other materials provided with the distribution.
17 *  3. Neither the name of the University nor the names of its
18 *     contributors may be used to endorse or promote products derived
19 *     from this software without specific prior written permission.
20 *
21 *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22 *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 */
34
35 #include <linux/file.h>
36 #include <linux/fs.h>
37 #include <linux/slab.h>
38 #include <linux/namei.h>
39 #include <linux/swap.h>
40 #include <linux/pagemap.h>
41 #include <linux/ratelimit.h>
42 #include <linux/sunrpc/svcauth_gss.h>
43 #include <linux/sunrpc/addr.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"
51 #include "xdr4cb.h"
52 #include "vfs.h"
53 #include "current_stateid.h"
54
55 #include "netns.h"
56 #include "pnfs.h"
57 #include "filecache.h"
58 #include "trace.h"
59
60 #define NFSDDBG_FACILITY                NFSDDBG_PROC
61
62 #define all_ones {{ ~0, ~0}, ~0}
63 static const stateid_t one_stateid = {
64         .si_generation = ~0,
65         .si_opaque = all_ones,
66 };
67 static const stateid_t zero_stateid = {
68         /* all fields zero */
69 };
70 static const stateid_t currentstateid = {
71         .si_generation = 1,
72 };
73 static const stateid_t close_stateid = {
74         .si_generation = 0xffffffffU,
75 };
76
77 static u64 current_sessionid = 1;
78
79 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
80 #define ONE_STATEID(stateid)  (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
81 #define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
82 #define CLOSE_STATEID(stateid)  (!memcmp((stateid), &close_stateid, sizeof(stateid_t)))
83
84 /* forward declarations */
85 static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
86 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 nfsd_net *nn, struct nfs4_cpntf_state *cps);
89 static void nfsd4_file_hash_remove(struct nfs4_file *fi);
90
91 /* Locking: */
92
93 /*
94  * Currently used for the del_recall_lru and file hash table.  In an
95  * effort to decrease the scope of the client_mutex, this spinlock may
96  * eventually cover more:
97  */
98 static DEFINE_SPINLOCK(state_lock);
99
100 enum nfsd4_st_mutex_lock_subclass {
101         OPEN_STATEID_MUTEX = 0,
102         LOCK_STATEID_MUTEX = 1,
103 };
104
105 /*
106  * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
107  * the refcount on the open stateid to drop.
108  */
109 static DECLARE_WAIT_QUEUE_HEAD(close_wq);
110
111 /*
112  * A waitqueue where a writer to clients/#/ctl destroying a client can
113  * wait for cl_rpc_users to drop to 0 and then for the client to be
114  * unhashed.
115  */
116 static DECLARE_WAIT_QUEUE_HEAD(expiry_wq);
117
118 static struct kmem_cache *client_slab;
119 static struct kmem_cache *openowner_slab;
120 static struct kmem_cache *lockowner_slab;
121 static struct kmem_cache *file_slab;
122 static struct kmem_cache *stateid_slab;
123 static struct kmem_cache *deleg_slab;
124 static struct kmem_cache *odstate_slab;
125
126 static void free_session(struct nfsd4_session *);
127
128 static const struct nfsd4_callback_ops nfsd4_cb_recall_ops;
129 static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops;
130 static const struct nfsd4_callback_ops nfsd4_cb_getattr_ops;
131
132 static struct workqueue_struct *laundry_wq;
133
134 int nfsd4_create_laundry_wq(void)
135 {
136         int rc = 0;
137
138         laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
139         if (laundry_wq == NULL)
140                 rc = -ENOMEM;
141         return rc;
142 }
143
144 void nfsd4_destroy_laundry_wq(void)
145 {
146         destroy_workqueue(laundry_wq);
147 }
148
149 static bool is_session_dead(struct nfsd4_session *ses)
150 {
151         return ses->se_flags & NFS4_SESSION_DEAD;
152 }
153
154 static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
155 {
156         if (atomic_read(&ses->se_ref) > ref_held_by_me)
157                 return nfserr_jukebox;
158         ses->se_flags |= NFS4_SESSION_DEAD;
159         return nfs_ok;
160 }
161
162 static bool is_client_expired(struct nfs4_client *clp)
163 {
164         return clp->cl_time == 0;
165 }
166
167 static void nfsd4_dec_courtesy_client_count(struct nfsd_net *nn,
168                                         struct nfs4_client *clp)
169 {
170         if (clp->cl_state != NFSD4_ACTIVE)
171                 atomic_add_unless(&nn->nfsd_courtesy_clients, -1, 0);
172 }
173
174 static __be32 get_client_locked(struct nfs4_client *clp)
175 {
176         struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
177
178         lockdep_assert_held(&nn->client_lock);
179
180         if (is_client_expired(clp))
181                 return nfserr_expired;
182         atomic_inc(&clp->cl_rpc_users);
183         nfsd4_dec_courtesy_client_count(nn, clp);
184         clp->cl_state = NFSD4_ACTIVE;
185         return nfs_ok;
186 }
187
188 /* must be called under the client_lock */
189 static inline void
190 renew_client_locked(struct nfs4_client *clp)
191 {
192         struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
193
194         if (is_client_expired(clp)) {
195                 WARN_ON(1);
196                 printk("%s: client (clientid %08x/%08x) already expired\n",
197                         __func__,
198                         clp->cl_clientid.cl_boot,
199                         clp->cl_clientid.cl_id);
200                 return;
201         }
202
203         list_move_tail(&clp->cl_lru, &nn->client_lru);
204         clp->cl_time = ktime_get_boottime_seconds();
205         nfsd4_dec_courtesy_client_count(nn, clp);
206         clp->cl_state = NFSD4_ACTIVE;
207 }
208
209 static void put_client_renew_locked(struct nfs4_client *clp)
210 {
211         struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
212
213         lockdep_assert_held(&nn->client_lock);
214
215         if (!atomic_dec_and_test(&clp->cl_rpc_users))
216                 return;
217         if (!is_client_expired(clp))
218                 renew_client_locked(clp);
219         else
220                 wake_up_all(&expiry_wq);
221 }
222
223 static void put_client_renew(struct nfs4_client *clp)
224 {
225         struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
226
227         if (!atomic_dec_and_lock(&clp->cl_rpc_users, &nn->client_lock))
228                 return;
229         if (!is_client_expired(clp))
230                 renew_client_locked(clp);
231         else
232                 wake_up_all(&expiry_wq);
233         spin_unlock(&nn->client_lock);
234 }
235
236 static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
237 {
238         __be32 status;
239
240         if (is_session_dead(ses))
241                 return nfserr_badsession;
242         status = get_client_locked(ses->se_client);
243         if (status)
244                 return status;
245         atomic_inc(&ses->se_ref);
246         return nfs_ok;
247 }
248
249 static void nfsd4_put_session_locked(struct nfsd4_session *ses)
250 {
251         struct nfs4_client *clp = ses->se_client;
252         struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
253
254         lockdep_assert_held(&nn->client_lock);
255
256         if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
257                 free_session(ses);
258         put_client_renew_locked(clp);
259 }
260
261 static void nfsd4_put_session(struct nfsd4_session *ses)
262 {
263         struct nfs4_client *clp = ses->se_client;
264         struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
265
266         spin_lock(&nn->client_lock);
267         nfsd4_put_session_locked(ses);
268         spin_unlock(&nn->client_lock);
269 }
270
271 static struct nfsd4_blocked_lock *
272 find_blocked_lock(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
273                         struct nfsd_net *nn)
274 {
275         struct nfsd4_blocked_lock *cur, *found = NULL;
276
277         spin_lock(&nn->blocked_locks_lock);
278         list_for_each_entry(cur, &lo->lo_blocked, nbl_list) {
279                 if (fh_match(fh, &cur->nbl_fh)) {
280                         list_del_init(&cur->nbl_list);
281                         WARN_ON(list_empty(&cur->nbl_lru));
282                         list_del_init(&cur->nbl_lru);
283                         found = cur;
284                         break;
285                 }
286         }
287         spin_unlock(&nn->blocked_locks_lock);
288         if (found)
289                 locks_delete_block(&found->nbl_lock);
290         return found;
291 }
292
293 static struct nfsd4_blocked_lock *
294 find_or_allocate_block(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
295                         struct nfsd_net *nn)
296 {
297         struct nfsd4_blocked_lock *nbl;
298
299         nbl = find_blocked_lock(lo, fh, nn);
300         if (!nbl) {
301                 nbl = kmalloc(sizeof(*nbl), GFP_KERNEL);
302                 if (nbl) {
303                         INIT_LIST_HEAD(&nbl->nbl_list);
304                         INIT_LIST_HEAD(&nbl->nbl_lru);
305                         fh_copy_shallow(&nbl->nbl_fh, fh);
306                         locks_init_lock(&nbl->nbl_lock);
307                         kref_init(&nbl->nbl_kref);
308                         nfsd4_init_cb(&nbl->nbl_cb, lo->lo_owner.so_client,
309                                         &nfsd4_cb_notify_lock_ops,
310                                         NFSPROC4_CLNT_CB_NOTIFY_LOCK);
311                 }
312         }
313         return nbl;
314 }
315
316 static void
317 free_nbl(struct kref *kref)
318 {
319         struct nfsd4_blocked_lock *nbl;
320
321         nbl = container_of(kref, struct nfsd4_blocked_lock, nbl_kref);
322         kfree(nbl);
323 }
324
325 static void
326 free_blocked_lock(struct nfsd4_blocked_lock *nbl)
327 {
328         locks_delete_block(&nbl->nbl_lock);
329         locks_release_private(&nbl->nbl_lock);
330         kref_put(&nbl->nbl_kref, free_nbl);
331 }
332
333 static void
334 remove_blocked_locks(struct nfs4_lockowner *lo)
335 {
336         struct nfs4_client *clp = lo->lo_owner.so_client;
337         struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
338         struct nfsd4_blocked_lock *nbl;
339         LIST_HEAD(reaplist);
340
341         /* Dequeue all blocked locks */
342         spin_lock(&nn->blocked_locks_lock);
343         while (!list_empty(&lo->lo_blocked)) {
344                 nbl = list_first_entry(&lo->lo_blocked,
345                                         struct nfsd4_blocked_lock,
346                                         nbl_list);
347                 list_del_init(&nbl->nbl_list);
348                 WARN_ON(list_empty(&nbl->nbl_lru));
349                 list_move(&nbl->nbl_lru, &reaplist);
350         }
351         spin_unlock(&nn->blocked_locks_lock);
352
353         /* Now free them */
354         while (!list_empty(&reaplist)) {
355                 nbl = list_first_entry(&reaplist, struct nfsd4_blocked_lock,
356                                         nbl_lru);
357                 list_del_init(&nbl->nbl_lru);
358                 free_blocked_lock(nbl);
359         }
360 }
361
362 static void
363 nfsd4_cb_notify_lock_prepare(struct nfsd4_callback *cb)
364 {
365         struct nfsd4_blocked_lock       *nbl = container_of(cb,
366                                                 struct nfsd4_blocked_lock, nbl_cb);
367         locks_delete_block(&nbl->nbl_lock);
368 }
369
370 static int
371 nfsd4_cb_notify_lock_done(struct nfsd4_callback *cb, struct rpc_task *task)
372 {
373         trace_nfsd_cb_notify_lock_done(&zero_stateid, task);
374
375         /*
376          * Since this is just an optimization, we don't try very hard if it
377          * turns out not to succeed. We'll requeue it on NFS4ERR_DELAY, and
378          * just quit trying on anything else.
379          */
380         switch (task->tk_status) {
381         case -NFS4ERR_DELAY:
382                 rpc_delay(task, 1 * HZ);
383                 return 0;
384         default:
385                 return 1;
386         }
387 }
388
389 static void
390 nfsd4_cb_notify_lock_release(struct nfsd4_callback *cb)
391 {
392         struct nfsd4_blocked_lock       *nbl = container_of(cb,
393                                                 struct nfsd4_blocked_lock, nbl_cb);
394
395         free_blocked_lock(nbl);
396 }
397
398 static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops = {
399         .prepare        = nfsd4_cb_notify_lock_prepare,
400         .done           = nfsd4_cb_notify_lock_done,
401         .release        = nfsd4_cb_notify_lock_release,
402 };
403
404 /*
405  * We store the NONE, READ, WRITE, and BOTH bits separately in the
406  * st_{access,deny}_bmap field of the stateid, in order to track not
407  * only what share bits are currently in force, but also what
408  * combinations of share bits previous opens have used.  This allows us
409  * to enforce the recommendation in
410  * https://datatracker.ietf.org/doc/html/rfc7530#section-16.19.4 that
411  * the server return an error if the client attempt to downgrade to a
412  * combination of share bits not explicable by closing some of its
413  * previous opens.
414  *
415  * This enforcement is arguably incomplete, since we don't keep
416  * track of access/deny bit combinations; so, e.g., we allow:
417  *
418  *      OPEN allow read, deny write
419  *      OPEN allow both, deny none
420  *      DOWNGRADE allow read, deny none
421  *
422  * which we should reject.
423  *
424  * But you could also argue that our current code is already overkill,
425  * since it only exists to return NFS4ERR_INVAL on incorrect client
426  * behavior.
427  */
428 static unsigned int
429 bmap_to_share_mode(unsigned long bmap)
430 {
431         int i;
432         unsigned int access = 0;
433
434         for (i = 1; i < 4; i++) {
435                 if (test_bit(i, &bmap))
436                         access |= i;
437         }
438         return access;
439 }
440
441 /* set share access for a given stateid */
442 static inline void
443 set_access(u32 access, struct nfs4_ol_stateid *stp)
444 {
445         unsigned char mask = 1 << access;
446
447         WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
448         stp->st_access_bmap |= mask;
449 }
450
451 /* clear share access for a given stateid */
452 static inline void
453 clear_access(u32 access, struct nfs4_ol_stateid *stp)
454 {
455         unsigned char mask = 1 << access;
456
457         WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
458         stp->st_access_bmap &= ~mask;
459 }
460
461 /* test whether a given stateid has access */
462 static inline bool
463 test_access(u32 access, struct nfs4_ol_stateid *stp)
464 {
465         unsigned char mask = 1 << access;
466
467         return (bool)(stp->st_access_bmap & mask);
468 }
469
470 /* set share deny for a given stateid */
471 static inline void
472 set_deny(u32 deny, struct nfs4_ol_stateid *stp)
473 {
474         unsigned char mask = 1 << deny;
475
476         WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
477         stp->st_deny_bmap |= mask;
478 }
479
480 /* clear share deny for a given stateid */
481 static inline void
482 clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
483 {
484         unsigned char mask = 1 << deny;
485
486         WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
487         stp->st_deny_bmap &= ~mask;
488 }
489
490 /* test whether a given stateid is denying specific access */
491 static inline bool
492 test_deny(u32 deny, struct nfs4_ol_stateid *stp)
493 {
494         unsigned char mask = 1 << deny;
495
496         return (bool)(stp->st_deny_bmap & mask);
497 }
498
499 static int nfs4_access_to_omode(u32 access)
500 {
501         switch (access & NFS4_SHARE_ACCESS_BOTH) {
502         case NFS4_SHARE_ACCESS_READ:
503                 return O_RDONLY;
504         case NFS4_SHARE_ACCESS_WRITE:
505                 return O_WRONLY;
506         case NFS4_SHARE_ACCESS_BOTH:
507                 return O_RDWR;
508         }
509         WARN_ON_ONCE(1);
510         return O_RDONLY;
511 }
512
513 static inline int
514 access_permit_read(struct nfs4_ol_stateid *stp)
515 {
516         return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
517                 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
518                 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
519 }
520
521 static inline int
522 access_permit_write(struct nfs4_ol_stateid *stp)
523 {
524         return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
525                 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
526 }
527
528 static inline struct nfs4_stateowner *
529 nfs4_get_stateowner(struct nfs4_stateowner *sop)
530 {
531         atomic_inc(&sop->so_count);
532         return sop;
533 }
534
535 static int
536 same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
537 {
538         return (sop->so_owner.len == owner->len) &&
539                 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
540 }
541
542 static struct nfs4_openowner *
543 find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
544                         struct nfs4_client *clp)
545 {
546         struct nfs4_stateowner *so;
547
548         lockdep_assert_held(&clp->cl_lock);
549
550         list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
551                             so_strhash) {
552                 if (!so->so_is_open_owner)
553                         continue;
554                 if (same_owner_str(so, &open->op_owner))
555                         return openowner(nfs4_get_stateowner(so));
556         }
557         return NULL;
558 }
559
560 static struct nfs4_openowner *
561 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
562                         struct nfs4_client *clp)
563 {
564         struct nfs4_openowner *oo;
565
566         spin_lock(&clp->cl_lock);
567         oo = find_openstateowner_str_locked(hashval, open, clp);
568         spin_unlock(&clp->cl_lock);
569         return oo;
570 }
571
572 static inline u32
573 opaque_hashval(const void *ptr, int nbytes)
574 {
575         unsigned char *cptr = (unsigned char *) ptr;
576
577         u32 x = 0;
578         while (nbytes--) {
579                 x *= 37;
580                 x += *cptr++;
581         }
582         return x;
583 }
584
585 static void nfsd4_free_file_rcu(struct rcu_head *rcu)
586 {
587         struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
588
589         kmem_cache_free(file_slab, fp);
590 }
591
592 void
593 put_nfs4_file(struct nfs4_file *fi)
594 {
595         if (refcount_dec_and_test(&fi->fi_ref)) {
596                 nfsd4_file_hash_remove(fi);
597                 WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
598                 WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
599                 call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
600         }
601 }
602
603 static struct nfsd_file *
604 find_writeable_file_locked(struct nfs4_file *f)
605 {
606         struct nfsd_file *ret;
607
608         lockdep_assert_held(&f->fi_lock);
609
610         ret = nfsd_file_get(f->fi_fds[O_WRONLY]);
611         if (!ret)
612                 ret = nfsd_file_get(f->fi_fds[O_RDWR]);
613         return ret;
614 }
615
616 static struct nfsd_file *
617 find_writeable_file(struct nfs4_file *f)
618 {
619         struct nfsd_file *ret;
620
621         spin_lock(&f->fi_lock);
622         ret = find_writeable_file_locked(f);
623         spin_unlock(&f->fi_lock);
624
625         return ret;
626 }
627
628 static struct nfsd_file *
629 find_readable_file_locked(struct nfs4_file *f)
630 {
631         struct nfsd_file *ret;
632
633         lockdep_assert_held(&f->fi_lock);
634
635         ret = nfsd_file_get(f->fi_fds[O_RDONLY]);
636         if (!ret)
637                 ret = nfsd_file_get(f->fi_fds[O_RDWR]);
638         return ret;
639 }
640
641 static struct nfsd_file *
642 find_readable_file(struct nfs4_file *f)
643 {
644         struct nfsd_file *ret;
645
646         spin_lock(&f->fi_lock);
647         ret = find_readable_file_locked(f);
648         spin_unlock(&f->fi_lock);
649
650         return ret;
651 }
652
653 static struct nfsd_file *
654 find_rw_file(struct nfs4_file *f)
655 {
656         struct nfsd_file *ret;
657
658         spin_lock(&f->fi_lock);
659         ret = nfsd_file_get(f->fi_fds[O_RDWR]);
660         spin_unlock(&f->fi_lock);
661
662         return ret;
663 }
664
665 struct nfsd_file *
666 find_any_file(struct nfs4_file *f)
667 {
668         struct nfsd_file *ret;
669
670         if (!f)
671                 return NULL;
672         spin_lock(&f->fi_lock);
673         ret = nfsd_file_get(f->fi_fds[O_RDWR]);
674         if (!ret) {
675                 ret = nfsd_file_get(f->fi_fds[O_WRONLY]);
676                 if (!ret)
677                         ret = nfsd_file_get(f->fi_fds[O_RDONLY]);
678         }
679         spin_unlock(&f->fi_lock);
680         return ret;
681 }
682
683 static struct nfsd_file *find_any_file_locked(struct nfs4_file *f)
684 {
685         lockdep_assert_held(&f->fi_lock);
686
687         if (f->fi_fds[O_RDWR])
688                 return f->fi_fds[O_RDWR];
689         if (f->fi_fds[O_WRONLY])
690                 return f->fi_fds[O_WRONLY];
691         if (f->fi_fds[O_RDONLY])
692                 return f->fi_fds[O_RDONLY];
693         return NULL;
694 }
695
696 static atomic_long_t num_delegations;
697 unsigned long max_delegations;
698
699 /*
700  * Open owner state (share locks)
701  */
702
703 /* hash tables for lock and open owners */
704 #define OWNER_HASH_BITS              8
705 #define OWNER_HASH_SIZE             (1 << OWNER_HASH_BITS)
706 #define OWNER_HASH_MASK             (OWNER_HASH_SIZE - 1)
707
708 static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
709 {
710         unsigned int ret;
711
712         ret = opaque_hashval(ownername->data, ownername->len);
713         return ret & OWNER_HASH_MASK;
714 }
715
716 static struct rhltable nfs4_file_rhltable ____cacheline_aligned_in_smp;
717
718 static const struct rhashtable_params nfs4_file_rhash_params = {
719         .key_len                = sizeof_field(struct nfs4_file, fi_inode),
720         .key_offset             = offsetof(struct nfs4_file, fi_inode),
721         .head_offset            = offsetof(struct nfs4_file, fi_rlist),
722
723         /*
724          * Start with a single page hash table to reduce resizing churn
725          * on light workloads.
726          */
727         .min_size               = 256,
728         .automatic_shrinking    = true,
729 };
730
731 /*
732  * Check if courtesy clients have conflicting access and resolve it if possible
733  *
734  * access:  is op_share_access if share_access is true.
735  *          Check if access mode, op_share_access, would conflict with
736  *          the current deny mode of the file 'fp'.
737  * access:  is op_share_deny if share_access is false.
738  *          Check if the deny mode, op_share_deny, would conflict with
739  *          current access of the file 'fp'.
740  * stp:     skip checking this entry.
741  * new_stp: normal open, not open upgrade.
742  *
743  * Function returns:
744  *      false - access/deny mode conflict with normal client.
745  *      true  - no conflict or conflict with courtesy client(s) is resolved.
746  */
747 static bool
748 nfs4_resolve_deny_conflicts_locked(struct nfs4_file *fp, bool new_stp,
749                 struct nfs4_ol_stateid *stp, u32 access, bool share_access)
750 {
751         struct nfs4_ol_stateid *st;
752         bool resolvable = true;
753         unsigned char bmap;
754         struct nfsd_net *nn;
755         struct nfs4_client *clp;
756
757         lockdep_assert_held(&fp->fi_lock);
758         list_for_each_entry(st, &fp->fi_stateids, st_perfile) {
759                 /* ignore lock stateid */
760                 if (st->st_openstp)
761                         continue;
762                 if (st == stp && new_stp)
763                         continue;
764                 /* check file access against deny mode or vice versa */
765                 bmap = share_access ? st->st_deny_bmap : st->st_access_bmap;
766                 if (!(access & bmap_to_share_mode(bmap)))
767                         continue;
768                 clp = st->st_stid.sc_client;
769                 if (try_to_expire_client(clp))
770                         continue;
771                 resolvable = false;
772                 break;
773         }
774         if (resolvable) {
775                 clp = stp->st_stid.sc_client;
776                 nn = net_generic(clp->net, nfsd_net_id);
777                 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
778         }
779         return resolvable;
780 }
781
782 static void
783 __nfs4_file_get_access(struct nfs4_file *fp, u32 access)
784 {
785         lockdep_assert_held(&fp->fi_lock);
786
787         if (access & NFS4_SHARE_ACCESS_WRITE)
788                 atomic_inc(&fp->fi_access[O_WRONLY]);
789         if (access & NFS4_SHARE_ACCESS_READ)
790                 atomic_inc(&fp->fi_access[O_RDONLY]);
791 }
792
793 static __be32
794 nfs4_file_get_access(struct nfs4_file *fp, u32 access)
795 {
796         lockdep_assert_held(&fp->fi_lock);
797
798         /* Does this access mode make sense? */
799         if (access & ~NFS4_SHARE_ACCESS_BOTH)
800                 return nfserr_inval;
801
802         /* Does it conflict with a deny mode already set? */
803         if ((access & fp->fi_share_deny) != 0)
804                 return nfserr_share_denied;
805
806         __nfs4_file_get_access(fp, access);
807         return nfs_ok;
808 }
809
810 static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
811 {
812         /* Common case is that there is no deny mode. */
813         if (deny) {
814                 /* Does this deny mode make sense? */
815                 if (deny & ~NFS4_SHARE_DENY_BOTH)
816                         return nfserr_inval;
817
818                 if ((deny & NFS4_SHARE_DENY_READ) &&
819                     atomic_read(&fp->fi_access[O_RDONLY]))
820                         return nfserr_share_denied;
821
822                 if ((deny & NFS4_SHARE_DENY_WRITE) &&
823                     atomic_read(&fp->fi_access[O_WRONLY]))
824                         return nfserr_share_denied;
825         }
826         return nfs_ok;
827 }
828
829 static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
830 {
831         might_lock(&fp->fi_lock);
832
833         if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
834                 struct nfsd_file *f1 = NULL;
835                 struct nfsd_file *f2 = NULL;
836
837                 swap(f1, fp->fi_fds[oflag]);
838                 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
839                         swap(f2, fp->fi_fds[O_RDWR]);
840                 spin_unlock(&fp->fi_lock);
841                 if (f1)
842                         nfsd_file_put(f1);
843                 if (f2)
844                         nfsd_file_put(f2);
845         }
846 }
847
848 static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
849 {
850         WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
851
852         if (access & NFS4_SHARE_ACCESS_WRITE)
853                 __nfs4_file_put_access(fp, O_WRONLY);
854         if (access & NFS4_SHARE_ACCESS_READ)
855                 __nfs4_file_put_access(fp, O_RDONLY);
856 }
857
858 /*
859  * Allocate a new open/delegation state counter. This is needed for
860  * pNFS for proper return on close semantics.
861  *
862  * Note that we only allocate it for pNFS-enabled exports, otherwise
863  * all pointers to struct nfs4_clnt_odstate are always NULL.
864  */
865 static struct nfs4_clnt_odstate *
866 alloc_clnt_odstate(struct nfs4_client *clp)
867 {
868         struct nfs4_clnt_odstate *co;
869
870         co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
871         if (co) {
872                 co->co_client = clp;
873                 refcount_set(&co->co_odcount, 1);
874         }
875         return co;
876 }
877
878 static void
879 hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
880 {
881         struct nfs4_file *fp = co->co_file;
882
883         lockdep_assert_held(&fp->fi_lock);
884         list_add(&co->co_perfile, &fp->fi_clnt_odstate);
885 }
886
887 static inline void
888 get_clnt_odstate(struct nfs4_clnt_odstate *co)
889 {
890         if (co)
891                 refcount_inc(&co->co_odcount);
892 }
893
894 static void
895 put_clnt_odstate(struct nfs4_clnt_odstate *co)
896 {
897         struct nfs4_file *fp;
898
899         if (!co)
900                 return;
901
902         fp = co->co_file;
903         if (refcount_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
904                 list_del(&co->co_perfile);
905                 spin_unlock(&fp->fi_lock);
906
907                 nfsd4_return_all_file_layouts(co->co_client, fp);
908                 kmem_cache_free(odstate_slab, co);
909         }
910 }
911
912 static struct nfs4_clnt_odstate *
913 find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
914 {
915         struct nfs4_clnt_odstate *co;
916         struct nfs4_client *cl;
917
918         if (!new)
919                 return NULL;
920
921         cl = new->co_client;
922
923         spin_lock(&fp->fi_lock);
924         list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
925                 if (co->co_client == cl) {
926                         get_clnt_odstate(co);
927                         goto out;
928                 }
929         }
930         co = new;
931         co->co_file = fp;
932         hash_clnt_odstate_locked(new);
933 out:
934         spin_unlock(&fp->fi_lock);
935         return co;
936 }
937
938 struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab,
939                                   void (*sc_free)(struct nfs4_stid *))
940 {
941         struct nfs4_stid *stid;
942         int new_id;
943
944         stid = kmem_cache_zalloc(slab, GFP_KERNEL);
945         if (!stid)
946                 return NULL;
947
948         idr_preload(GFP_KERNEL);
949         spin_lock(&cl->cl_lock);
950         /* Reserving 0 for start of file in nfsdfs "states" file: */
951         new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 1, 0, GFP_NOWAIT);
952         spin_unlock(&cl->cl_lock);
953         idr_preload_end();
954         if (new_id < 0)
955                 goto out_free;
956
957         stid->sc_free = sc_free;
958         stid->sc_client = cl;
959         stid->sc_stateid.si_opaque.so_id = new_id;
960         stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
961         /* Will be incremented before return to client: */
962         refcount_set(&stid->sc_count, 1);
963         spin_lock_init(&stid->sc_lock);
964         INIT_LIST_HEAD(&stid->sc_cp_list);
965
966         /*
967          * It shouldn't be a problem to reuse an opaque stateid value.
968          * I don't think it is for 4.1.  But with 4.0 I worry that, for
969          * example, a stray write retransmission could be accepted by
970          * the server when it should have been rejected.  Therefore,
971          * adopt a trick from the sctp code to attempt to maximize the
972          * amount of time until an id is reused, by ensuring they always
973          * "increase" (mod INT_MAX):
974          */
975         return stid;
976 out_free:
977         kmem_cache_free(slab, stid);
978         return NULL;
979 }
980
981 /*
982  * Create a unique stateid_t to represent each COPY.
983  */
984 static int nfs4_init_cp_state(struct nfsd_net *nn, copy_stateid_t *stid,
985                               unsigned char cs_type)
986 {
987         int new_id;
988
989         stid->cs_stid.si_opaque.so_clid.cl_boot = (u32)nn->boot_time;
990         stid->cs_stid.si_opaque.so_clid.cl_id = nn->s2s_cp_cl_id;
991
992         idr_preload(GFP_KERNEL);
993         spin_lock(&nn->s2s_cp_lock);
994         new_id = idr_alloc_cyclic(&nn->s2s_cp_stateids, stid, 0, 0, GFP_NOWAIT);
995         stid->cs_stid.si_opaque.so_id = new_id;
996         stid->cs_stid.si_generation = 1;
997         spin_unlock(&nn->s2s_cp_lock);
998         idr_preload_end();
999         if (new_id < 0)
1000                 return 0;
1001         stid->cs_type = cs_type;
1002         return 1;
1003 }
1004
1005 int nfs4_init_copy_state(struct nfsd_net *nn, struct nfsd4_copy *copy)
1006 {
1007         return nfs4_init_cp_state(nn, &copy->cp_stateid, NFS4_COPY_STID);
1008 }
1009
1010 struct nfs4_cpntf_state *nfs4_alloc_init_cpntf_state(struct nfsd_net *nn,
1011                                                      struct nfs4_stid *p_stid)
1012 {
1013         struct nfs4_cpntf_state *cps;
1014
1015         cps = kzalloc(sizeof(struct nfs4_cpntf_state), GFP_KERNEL);
1016         if (!cps)
1017                 return NULL;
1018         cps->cpntf_time = ktime_get_boottime_seconds();
1019         refcount_set(&cps->cp_stateid.cs_count, 1);
1020         if (!nfs4_init_cp_state(nn, &cps->cp_stateid, NFS4_COPYNOTIFY_STID))
1021                 goto out_free;
1022         spin_lock(&nn->s2s_cp_lock);
1023         list_add(&cps->cp_list, &p_stid->sc_cp_list);
1024         spin_unlock(&nn->s2s_cp_lock);
1025         return cps;
1026 out_free:
1027         kfree(cps);
1028         return NULL;
1029 }
1030
1031 void nfs4_free_copy_state(struct nfsd4_copy *copy)
1032 {
1033         struct nfsd_net *nn;
1034
1035         if (copy->cp_stateid.cs_type != NFS4_COPY_STID)
1036                 return;
1037         nn = net_generic(copy->cp_clp->net, nfsd_net_id);
1038         spin_lock(&nn->s2s_cp_lock);
1039         idr_remove(&nn->s2s_cp_stateids,
1040                    copy->cp_stateid.cs_stid.si_opaque.so_id);
1041         spin_unlock(&nn->s2s_cp_lock);
1042 }
1043
1044 static void nfs4_free_cpntf_statelist(struct net *net, struct nfs4_stid *stid)
1045 {
1046         struct nfs4_cpntf_state *cps;
1047         struct nfsd_net *nn;
1048
1049         nn = net_generic(net, nfsd_net_id);
1050         spin_lock(&nn->s2s_cp_lock);
1051         while (!list_empty(&stid->sc_cp_list)) {
1052                 cps = list_first_entry(&stid->sc_cp_list,
1053                                        struct nfs4_cpntf_state, cp_list);
1054                 _free_cpntf_state_locked(nn, cps);
1055         }
1056         spin_unlock(&nn->s2s_cp_lock);
1057 }
1058
1059 static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
1060 {
1061         struct nfs4_stid *stid;
1062
1063         stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid);
1064         if (!stid)
1065                 return NULL;
1066
1067         return openlockstateid(stid);
1068 }
1069
1070 static void nfs4_free_deleg(struct nfs4_stid *stid)
1071 {
1072         struct nfs4_delegation *dp = delegstateid(stid);
1073
1074         WARN_ON_ONCE(!list_empty(&stid->sc_cp_list));
1075         WARN_ON_ONCE(!list_empty(&dp->dl_perfile));
1076         WARN_ON_ONCE(!list_empty(&dp->dl_perclnt));
1077         WARN_ON_ONCE(!list_empty(&dp->dl_recall_lru));
1078         kmem_cache_free(deleg_slab, stid);
1079         atomic_long_dec(&num_delegations);
1080 }
1081
1082 /*
1083  * When we recall a delegation, we should be careful not to hand it
1084  * out again straight away.
1085  * To ensure this we keep a pair of bloom filters ('new' and 'old')
1086  * in which the filehandles of recalled delegations are "stored".
1087  * If a filehandle appear in either filter, a delegation is blocked.
1088  * When a delegation is recalled, the filehandle is stored in the "new"
1089  * filter.
1090  * Every 30 seconds we swap the filters and clear the "new" one,
1091  * unless both are empty of course.
1092  *
1093  * Each filter is 256 bits.  We hash the filehandle to 32bit and use the
1094  * low 3 bytes as hash-table indices.
1095  *
1096  * 'blocked_delegations_lock', which is always taken in block_delegations(),
1097  * is used to manage concurrent access.  Testing does not need the lock
1098  * except when swapping the two filters.
1099  */
1100 static DEFINE_SPINLOCK(blocked_delegations_lock);
1101 static struct bloom_pair {
1102         int     entries, old_entries;
1103         time64_t swap_time;
1104         int     new; /* index into 'set' */
1105         DECLARE_BITMAP(set[2], 256);
1106 } blocked_delegations;
1107
1108 static int delegation_blocked(struct knfsd_fh *fh)
1109 {
1110         u32 hash;
1111         struct bloom_pair *bd = &blocked_delegations;
1112
1113         if (bd->entries == 0)
1114                 return 0;
1115         if (ktime_get_seconds() - bd->swap_time > 30) {
1116                 spin_lock(&blocked_delegations_lock);
1117                 if (ktime_get_seconds() - bd->swap_time > 30) {
1118                         bd->entries -= bd->old_entries;
1119                         bd->old_entries = bd->entries;
1120                         memset(bd->set[bd->new], 0,
1121                                sizeof(bd->set[0]));
1122                         bd->new = 1-bd->new;
1123                         bd->swap_time = ktime_get_seconds();
1124                 }
1125                 spin_unlock(&blocked_delegations_lock);
1126         }
1127         hash = jhash(&fh->fh_raw, fh->fh_size, 0);
1128         if (test_bit(hash&255, bd->set[0]) &&
1129             test_bit((hash>>8)&255, bd->set[0]) &&
1130             test_bit((hash>>16)&255, bd->set[0]))
1131                 return 1;
1132
1133         if (test_bit(hash&255, bd->set[1]) &&
1134             test_bit((hash>>8)&255, bd->set[1]) &&
1135             test_bit((hash>>16)&255, bd->set[1]))
1136                 return 1;
1137
1138         return 0;
1139 }
1140
1141 static void block_delegations(struct knfsd_fh *fh)
1142 {
1143         u32 hash;
1144         struct bloom_pair *bd = &blocked_delegations;
1145
1146         hash = jhash(&fh->fh_raw, fh->fh_size, 0);
1147
1148         spin_lock(&blocked_delegations_lock);
1149         __set_bit(hash&255, bd->set[bd->new]);
1150         __set_bit((hash>>8)&255, bd->set[bd->new]);
1151         __set_bit((hash>>16)&255, bd->set[bd->new]);
1152         if (bd->entries == 0)
1153                 bd->swap_time = ktime_get_seconds();
1154         bd->entries += 1;
1155         spin_unlock(&blocked_delegations_lock);
1156 }
1157
1158 static struct nfs4_delegation *
1159 alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
1160                  struct nfs4_clnt_odstate *odstate, u32 dl_type)
1161 {
1162         struct nfs4_delegation *dp;
1163         struct nfs4_stid *stid;
1164         long n;
1165
1166         dprintk("NFSD alloc_init_deleg\n");
1167         n = atomic_long_inc_return(&num_delegations);
1168         if (n < 0 || n > max_delegations)
1169                 goto out_dec;
1170         if (delegation_blocked(&fp->fi_fhandle))
1171                 goto out_dec;
1172         stid = nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg);
1173         if (stid == NULL)
1174                 goto out_dec;
1175         dp = delegstateid(stid);
1176
1177         /*
1178          * delegation seqid's are never incremented.  The 4.1 special
1179          * meaning of seqid 0 isn't meaningful, really, but let's avoid
1180          * 0 anyway just for consistency and use 1:
1181          */
1182         dp->dl_stid.sc_stateid.si_generation = 1;
1183         INIT_LIST_HEAD(&dp->dl_perfile);
1184         INIT_LIST_HEAD(&dp->dl_perclnt);
1185         INIT_LIST_HEAD(&dp->dl_recall_lru);
1186         dp->dl_clnt_odstate = odstate;
1187         get_clnt_odstate(odstate);
1188         dp->dl_type = dl_type;
1189         dp->dl_retries = 1;
1190         dp->dl_recalled = false;
1191         nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
1192                       &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
1193         nfsd4_init_cb(&dp->dl_cb_fattr.ncf_getattr, dp->dl_stid.sc_client,
1194                         &nfsd4_cb_getattr_ops, NFSPROC4_CLNT_CB_GETATTR);
1195         dp->dl_cb_fattr.ncf_file_modified = false;
1196         dp->dl_cb_fattr.ncf_cb_bmap[0] = FATTR4_WORD0_CHANGE | FATTR4_WORD0_SIZE;
1197         get_nfs4_file(fp);
1198         dp->dl_stid.sc_file = fp;
1199         return dp;
1200 out_dec:
1201         atomic_long_dec(&num_delegations);
1202         return NULL;
1203 }
1204
1205 void
1206 nfs4_put_stid(struct nfs4_stid *s)
1207 {
1208         struct nfs4_file *fp = s->sc_file;
1209         struct nfs4_client *clp = s->sc_client;
1210
1211         might_lock(&clp->cl_lock);
1212
1213         if (!refcount_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
1214                 wake_up_all(&close_wq);
1215                 return;
1216         }
1217         idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1218         nfs4_free_cpntf_statelist(clp->net, s);
1219         spin_unlock(&clp->cl_lock);
1220         s->sc_free(s);
1221         if (fp)
1222                 put_nfs4_file(fp);
1223 }
1224
1225 void
1226 nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
1227 {
1228         stateid_t *src = &stid->sc_stateid;
1229
1230         spin_lock(&stid->sc_lock);
1231         if (unlikely(++src->si_generation == 0))
1232                 src->si_generation = 1;
1233         memcpy(dst, src, sizeof(*dst));
1234         spin_unlock(&stid->sc_lock);
1235 }
1236
1237 static void put_deleg_file(struct nfs4_file *fp)
1238 {
1239         struct nfsd_file *nf = NULL;
1240
1241         spin_lock(&fp->fi_lock);
1242         if (--fp->fi_delegees == 0)
1243                 swap(nf, fp->fi_deleg_file);
1244         spin_unlock(&fp->fi_lock);
1245
1246         if (nf)
1247                 nfsd_file_put(nf);
1248 }
1249
1250 static void nfs4_unlock_deleg_lease(struct nfs4_delegation *dp)
1251 {
1252         struct nfs4_file *fp = dp->dl_stid.sc_file;
1253         struct nfsd_file *nf = fp->fi_deleg_file;
1254
1255         WARN_ON_ONCE(!fp->fi_delegees);
1256
1257         vfs_setlease(nf->nf_file, F_UNLCK, NULL, (void **)&dp);
1258         put_deleg_file(fp);
1259 }
1260
1261 static void destroy_unhashed_deleg(struct nfs4_delegation *dp)
1262 {
1263         put_clnt_odstate(dp->dl_clnt_odstate);
1264         nfs4_unlock_deleg_lease(dp);
1265         nfs4_put_stid(&dp->dl_stid);
1266 }
1267
1268 void nfs4_unhash_stid(struct nfs4_stid *s)
1269 {
1270         s->sc_type = 0;
1271 }
1272
1273 /**
1274  * nfs4_delegation_exists - Discover if this delegation already exists
1275  * @clp:     a pointer to the nfs4_client we're granting a delegation to
1276  * @fp:      a pointer to the nfs4_file we're granting a delegation on
1277  *
1278  * Return:
1279  *      On success: true iff an existing delegation is found
1280  */
1281
1282 static bool
1283 nfs4_delegation_exists(struct nfs4_client *clp, struct nfs4_file *fp)
1284 {
1285         struct nfs4_delegation *searchdp = NULL;
1286         struct nfs4_client *searchclp = NULL;
1287
1288         lockdep_assert_held(&state_lock);
1289         lockdep_assert_held(&fp->fi_lock);
1290
1291         list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
1292                 searchclp = searchdp->dl_stid.sc_client;
1293                 if (clp == searchclp) {
1294                         return true;
1295                 }
1296         }
1297         return false;
1298 }
1299
1300 /**
1301  * hash_delegation_locked - Add a delegation to the appropriate lists
1302  * @dp:     a pointer to the nfs4_delegation we are adding.
1303  * @fp:     a pointer to the nfs4_file we're granting a delegation on
1304  *
1305  * Return:
1306  *      On success: NULL if the delegation was successfully hashed.
1307  *
1308  *      On error: -EAGAIN if one was previously granted to this
1309  *                 nfs4_client for this nfs4_file. Delegation is not hashed.
1310  *
1311  */
1312
1313 static int
1314 hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
1315 {
1316         struct nfs4_client *clp = dp->dl_stid.sc_client;
1317
1318         lockdep_assert_held(&state_lock);
1319         lockdep_assert_held(&fp->fi_lock);
1320
1321         if (nfs4_delegation_exists(clp, fp))
1322                 return -EAGAIN;
1323         refcount_inc(&dp->dl_stid.sc_count);
1324         dp->dl_stid.sc_type = NFS4_DELEG_STID;
1325         list_add(&dp->dl_perfile, &fp->fi_delegations);
1326         list_add(&dp->dl_perclnt, &clp->cl_delegations);
1327         return 0;
1328 }
1329
1330 static bool delegation_hashed(struct nfs4_delegation *dp)
1331 {
1332         return !(list_empty(&dp->dl_perfile));
1333 }
1334
1335 static bool
1336 unhash_delegation_locked(struct nfs4_delegation *dp)
1337 {
1338         struct nfs4_file *fp = dp->dl_stid.sc_file;
1339
1340         lockdep_assert_held(&state_lock);
1341
1342         if (!delegation_hashed(dp))
1343                 return false;
1344
1345         dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
1346         /* Ensure that deleg break won't try to requeue it */
1347         ++dp->dl_time;
1348         spin_lock(&fp->fi_lock);
1349         list_del_init(&dp->dl_perclnt);
1350         list_del_init(&dp->dl_recall_lru);
1351         list_del_init(&dp->dl_perfile);
1352         spin_unlock(&fp->fi_lock);
1353         return true;
1354 }
1355
1356 static void destroy_delegation(struct nfs4_delegation *dp)
1357 {
1358         bool unhashed;
1359
1360         spin_lock(&state_lock);
1361         unhashed = unhash_delegation_locked(dp);
1362         spin_unlock(&state_lock);
1363         if (unhashed)
1364                 destroy_unhashed_deleg(dp);
1365 }
1366
1367 static void revoke_delegation(struct nfs4_delegation *dp)
1368 {
1369         struct nfs4_client *clp = dp->dl_stid.sc_client;
1370
1371         WARN_ON(!list_empty(&dp->dl_recall_lru));
1372
1373         trace_nfsd_stid_revoke(&dp->dl_stid);
1374
1375         if (clp->cl_minorversion) {
1376                 spin_lock(&clp->cl_lock);
1377                 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
1378                 refcount_inc(&dp->dl_stid.sc_count);
1379                 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
1380                 spin_unlock(&clp->cl_lock);
1381         }
1382         destroy_unhashed_deleg(dp);
1383 }
1384
1385 /* 
1386  * SETCLIENTID state 
1387  */
1388
1389 static unsigned int clientid_hashval(u32 id)
1390 {
1391         return id & CLIENT_HASH_MASK;
1392 }
1393
1394 static unsigned int clientstr_hashval(struct xdr_netobj name)
1395 {
1396         return opaque_hashval(name.data, 8) & CLIENT_HASH_MASK;
1397 }
1398
1399 /*
1400  * A stateid that had a deny mode associated with it is being released
1401  * or downgraded. Recalculate the deny mode on the file.
1402  */
1403 static void
1404 recalculate_deny_mode(struct nfs4_file *fp)
1405 {
1406         struct nfs4_ol_stateid *stp;
1407
1408         spin_lock(&fp->fi_lock);
1409         fp->fi_share_deny = 0;
1410         list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
1411                 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
1412         spin_unlock(&fp->fi_lock);
1413 }
1414
1415 static void
1416 reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
1417 {
1418         int i;
1419         bool change = false;
1420
1421         for (i = 1; i < 4; i++) {
1422                 if ((i & deny) != i) {
1423                         change = true;
1424                         clear_deny(i, stp);
1425                 }
1426         }
1427
1428         /* Recalculate per-file deny mode if there was a change */
1429         if (change)
1430                 recalculate_deny_mode(stp->st_stid.sc_file);
1431 }
1432
1433 /* release all access and file references for a given stateid */
1434 static void
1435 release_all_access(struct nfs4_ol_stateid *stp)
1436 {
1437         int i;
1438         struct nfs4_file *fp = stp->st_stid.sc_file;
1439
1440         if (fp && stp->st_deny_bmap != 0)
1441                 recalculate_deny_mode(fp);
1442
1443         for (i = 1; i < 4; i++) {
1444                 if (test_access(i, stp))
1445                         nfs4_file_put_access(stp->st_stid.sc_file, i);
1446                 clear_access(i, stp);
1447         }
1448 }
1449
1450 static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1451 {
1452         kfree(sop->so_owner.data);
1453         sop->so_ops->so_free(sop);
1454 }
1455
1456 static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1457 {
1458         struct nfs4_client *clp = sop->so_client;
1459
1460         might_lock(&clp->cl_lock);
1461
1462         if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
1463                 return;
1464         sop->so_ops->so_unhash(sop);
1465         spin_unlock(&clp->cl_lock);
1466         nfs4_free_stateowner(sop);
1467 }
1468
1469 static bool
1470 nfs4_ol_stateid_unhashed(const struct nfs4_ol_stateid *stp)
1471 {
1472         return list_empty(&stp->st_perfile);
1473 }
1474
1475 static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
1476 {
1477         struct nfs4_file *fp = stp->st_stid.sc_file;
1478
1479         lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1480
1481         if (list_empty(&stp->st_perfile))
1482                 return false;
1483
1484         spin_lock(&fp->fi_lock);
1485         list_del_init(&stp->st_perfile);
1486         spin_unlock(&fp->fi_lock);
1487         list_del(&stp->st_perstateowner);
1488         return true;
1489 }
1490
1491 static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
1492 {
1493         struct nfs4_ol_stateid *stp = openlockstateid(stid);
1494
1495         put_clnt_odstate(stp->st_clnt_odstate);
1496         release_all_access(stp);
1497         if (stp->st_stateowner)
1498                 nfs4_put_stateowner(stp->st_stateowner);
1499         WARN_ON(!list_empty(&stid->sc_cp_list));
1500         kmem_cache_free(stateid_slab, stid);
1501 }
1502
1503 static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
1504 {
1505         struct nfs4_ol_stateid *stp = openlockstateid(stid);
1506         struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
1507         struct nfsd_file *nf;
1508
1509         nf = find_any_file(stp->st_stid.sc_file);
1510         if (nf) {
1511                 get_file(nf->nf_file);
1512                 filp_close(nf->nf_file, (fl_owner_t)lo);
1513                 nfsd_file_put(nf);
1514         }
1515         nfs4_free_ol_stateid(stid);
1516 }
1517
1518 /*
1519  * Put the persistent reference to an already unhashed generic stateid, while
1520  * holding the cl_lock. If it's the last reference, then put it onto the
1521  * reaplist for later destruction.
1522  */
1523 static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1524                                        struct list_head *reaplist)
1525 {
1526         struct nfs4_stid *s = &stp->st_stid;
1527         struct nfs4_client *clp = s->sc_client;
1528
1529         lockdep_assert_held(&clp->cl_lock);
1530
1531         WARN_ON_ONCE(!list_empty(&stp->st_locks));
1532
1533         if (!refcount_dec_and_test(&s->sc_count)) {
1534                 wake_up_all(&close_wq);
1535                 return;
1536         }
1537
1538         idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1539         list_add(&stp->st_locks, reaplist);
1540 }
1541
1542 static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
1543 {
1544         lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1545
1546         if (!unhash_ol_stateid(stp))
1547                 return false;
1548         list_del_init(&stp->st_locks);
1549         nfs4_unhash_stid(&stp->st_stid);
1550         return true;
1551 }
1552
1553 static void release_lock_stateid(struct nfs4_ol_stateid *stp)
1554 {
1555         struct nfs4_client *clp = stp->st_stid.sc_client;
1556         bool unhashed;
1557
1558         spin_lock(&clp->cl_lock);
1559         unhashed = unhash_lock_stateid(stp);
1560         spin_unlock(&clp->cl_lock);
1561         if (unhashed)
1562                 nfs4_put_stid(&stp->st_stid);
1563 }
1564
1565 static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
1566 {
1567         struct nfs4_client *clp = lo->lo_owner.so_client;
1568
1569         lockdep_assert_held(&clp->cl_lock);
1570
1571         list_del_init(&lo->lo_owner.so_strhash);
1572 }
1573
1574 /*
1575  * Free a list of generic stateids that were collected earlier after being
1576  * fully unhashed.
1577  */
1578 static void
1579 free_ol_stateid_reaplist(struct list_head *reaplist)
1580 {
1581         struct nfs4_ol_stateid *stp;
1582         struct nfs4_file *fp;
1583
1584         might_sleep();
1585
1586         while (!list_empty(reaplist)) {
1587                 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1588                                        st_locks);
1589                 list_del(&stp->st_locks);
1590                 fp = stp->st_stid.sc_file;
1591                 stp->st_stid.sc_free(&stp->st_stid);
1592                 if (fp)
1593                         put_nfs4_file(fp);
1594         }
1595 }
1596
1597 static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1598                                        struct list_head *reaplist)
1599 {
1600         struct nfs4_ol_stateid *stp;
1601
1602         lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1603
1604         while (!list_empty(&open_stp->st_locks)) {
1605                 stp = list_entry(open_stp->st_locks.next,
1606                                 struct nfs4_ol_stateid, st_locks);
1607                 WARN_ON(!unhash_lock_stateid(stp));
1608                 put_ol_stateid_locked(stp, reaplist);
1609         }
1610 }
1611
1612 static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
1613                                 struct list_head *reaplist)
1614 {
1615         lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1616
1617         if (!unhash_ol_stateid(stp))
1618                 return false;
1619         release_open_stateid_locks(stp, reaplist);
1620         return true;
1621 }
1622
1623 static void release_open_stateid(struct nfs4_ol_stateid *stp)
1624 {
1625         LIST_HEAD(reaplist);
1626
1627         spin_lock(&stp->st_stid.sc_client->cl_lock);
1628         if (unhash_open_stateid(stp, &reaplist))
1629                 put_ol_stateid_locked(stp, &reaplist);
1630         spin_unlock(&stp->st_stid.sc_client->cl_lock);
1631         free_ol_stateid_reaplist(&reaplist);
1632 }
1633
1634 static void unhash_openowner_locked(struct nfs4_openowner *oo)
1635 {
1636         struct nfs4_client *clp = oo->oo_owner.so_client;
1637
1638         lockdep_assert_held(&clp->cl_lock);
1639
1640         list_del_init(&oo->oo_owner.so_strhash);
1641         list_del_init(&oo->oo_perclient);
1642 }
1643
1644 static void release_last_closed_stateid(struct nfs4_openowner *oo)
1645 {
1646         struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1647                                           nfsd_net_id);
1648         struct nfs4_ol_stateid *s;
1649
1650         spin_lock(&nn->client_lock);
1651         s = oo->oo_last_closed_stid;
1652         if (s) {
1653                 list_del_init(&oo->oo_close_lru);
1654                 oo->oo_last_closed_stid = NULL;
1655         }
1656         spin_unlock(&nn->client_lock);
1657         if (s)
1658                 nfs4_put_stid(&s->st_stid);
1659 }
1660
1661 static void release_openowner(struct nfs4_openowner *oo)
1662 {
1663         struct nfs4_ol_stateid *stp;
1664         struct nfs4_client *clp = oo->oo_owner.so_client;
1665         struct list_head reaplist;
1666
1667         INIT_LIST_HEAD(&reaplist);
1668
1669         spin_lock(&clp->cl_lock);
1670         unhash_openowner_locked(oo);
1671         while (!list_empty(&oo->oo_owner.so_stateids)) {
1672                 stp = list_first_entry(&oo->oo_owner.so_stateids,
1673                                 struct nfs4_ol_stateid, st_perstateowner);
1674                 if (unhash_open_stateid(stp, &reaplist))
1675                         put_ol_stateid_locked(stp, &reaplist);
1676         }
1677         spin_unlock(&clp->cl_lock);
1678         free_ol_stateid_reaplist(&reaplist);
1679         release_last_closed_stateid(oo);
1680         nfs4_put_stateowner(&oo->oo_owner);
1681 }
1682
1683 static inline int
1684 hash_sessionid(struct nfs4_sessionid *sessionid)
1685 {
1686         struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1687
1688         return sid->sequence % SESSION_HASH_SIZE;
1689 }
1690
1691 #ifdef CONFIG_SUNRPC_DEBUG
1692 static inline void
1693 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1694 {
1695         u32 *ptr = (u32 *)(&sessionid->data[0]);
1696         dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1697 }
1698 #else
1699 static inline void
1700 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1701 {
1702 }
1703 #endif
1704
1705 /*
1706  * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1707  * won't be used for replay.
1708  */
1709 void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1710 {
1711         struct nfs4_stateowner *so = cstate->replay_owner;
1712
1713         if (nfserr == nfserr_replay_me)
1714                 return;
1715
1716         if (!seqid_mutating_err(ntohl(nfserr))) {
1717                 nfsd4_cstate_clear_replay(cstate);
1718                 return;
1719         }
1720         if (!so)
1721                 return;
1722         if (so->so_is_open_owner)
1723                 release_last_closed_stateid(openowner(so));
1724         so->so_seqid++;
1725         return;
1726 }
1727
1728 static void
1729 gen_sessionid(struct nfsd4_session *ses)
1730 {
1731         struct nfs4_client *clp = ses->se_client;
1732         struct nfsd4_sessionid *sid;
1733
1734         sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1735         sid->clientid = clp->cl_clientid;
1736         sid->sequence = current_sessionid++;
1737         sid->reserved = 0;
1738 }
1739
1740 /*
1741  * The protocol defines ca_maxresponssize_cached to include the size of
1742  * the rpc header, but all we need to cache is the data starting after
1743  * the end of the initial SEQUENCE operation--the rest we regenerate
1744  * each time.  Therefore we can advertise a ca_maxresponssize_cached
1745  * value that is the number of bytes in our cache plus a few additional
1746  * bytes.  In order to stay on the safe side, and not promise more than
1747  * we can cache, those additional bytes must be the minimum possible: 24
1748  * bytes of rpc header (xid through accept state, with AUTH_NULL
1749  * verifier), 12 for the compound header (with zero-length tag), and 44
1750  * for the SEQUENCE op response:
1751  */
1752 #define NFSD_MIN_HDR_SEQ_SZ  (24 + 12 + 44)
1753
1754 static void
1755 free_session_slots(struct nfsd4_session *ses)
1756 {
1757         int i;
1758
1759         for (i = 0; i < ses->se_fchannel.maxreqs; i++) {
1760                 free_svc_cred(&ses->se_slots[i]->sl_cred);
1761                 kfree(ses->se_slots[i]);
1762         }
1763 }
1764
1765 /*
1766  * We don't actually need to cache the rpc and session headers, so we
1767  * can allocate a little less for each slot:
1768  */
1769 static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
1770 {
1771         u32 size;
1772
1773         if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1774                 size = 0;
1775         else
1776                 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1777         return size + sizeof(struct nfsd4_slot);
1778 }
1779
1780 /*
1781  * XXX: If we run out of reserved DRC memory we could (up to a point)
1782  * re-negotiate active sessions and reduce their slot usage to make
1783  * room for new connections. For now we just fail the create session.
1784  */
1785 static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
1786 {
1787         u32 slotsize = slot_bytes(ca);
1788         u32 num = ca->maxreqs;
1789         unsigned long avail, total_avail;
1790         unsigned int scale_factor;
1791
1792         spin_lock(&nfsd_drc_lock);
1793         if (nfsd_drc_max_mem > nfsd_drc_mem_used)
1794                 total_avail = nfsd_drc_max_mem - nfsd_drc_mem_used;
1795         else
1796                 /* We have handed out more space than we chose in
1797                  * set_max_drc() to allow.  That isn't really a
1798                  * problem as long as that doesn't make us think we
1799                  * have lots more due to integer overflow.
1800                  */
1801                 total_avail = 0;
1802         avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, total_avail);
1803         /*
1804          * Never use more than a fraction of the remaining memory,
1805          * unless it's the only way to give this client a slot.
1806          * The chosen fraction is either 1/8 or 1/number of threads,
1807          * whichever is smaller.  This ensures there are adequate
1808          * slots to support multiple clients per thread.
1809          * Give the client one slot even if that would require
1810          * over-allocation--it is better than failure.
1811          */
1812         scale_factor = max_t(unsigned int, 8, nn->nfsd_serv->sv_nrthreads);
1813
1814         avail = clamp_t(unsigned long, avail, slotsize,
1815                         total_avail/scale_factor);
1816         num = min_t(int, num, avail / slotsize);
1817         num = max_t(int, num, 1);
1818         nfsd_drc_mem_used += num * slotsize;
1819         spin_unlock(&nfsd_drc_lock);
1820
1821         return num;
1822 }
1823
1824 static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
1825 {
1826         int slotsize = slot_bytes(ca);
1827
1828         spin_lock(&nfsd_drc_lock);
1829         nfsd_drc_mem_used -= slotsize * ca->maxreqs;
1830         spin_unlock(&nfsd_drc_lock);
1831 }
1832
1833 static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1834                                            struct nfsd4_channel_attrs *battrs)
1835 {
1836         int numslots = fattrs->maxreqs;
1837         int slotsize = slot_bytes(fattrs);
1838         struct nfsd4_session *new;
1839         int i;
1840
1841         BUILD_BUG_ON(struct_size(new, se_slots, NFSD_MAX_SLOTS_PER_SESSION)
1842                      > PAGE_SIZE);
1843
1844         new = kzalloc(struct_size(new, se_slots, numslots), GFP_KERNEL);
1845         if (!new)
1846                 return NULL;
1847         /* allocate each struct nfsd4_slot and data cache in one piece */
1848         for (i = 0; i < numslots; i++) {
1849                 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
1850                 if (!new->se_slots[i])
1851                         goto out_free;
1852         }
1853
1854         memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1855         memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1856
1857         return new;
1858 out_free:
1859         while (i--)
1860                 kfree(new->se_slots[i]);
1861         kfree(new);
1862         return NULL;
1863 }
1864
1865 static void free_conn(struct nfsd4_conn *c)
1866 {
1867         svc_xprt_put(c->cn_xprt);
1868         kfree(c);
1869 }
1870
1871 static void nfsd4_conn_lost(struct svc_xpt_user *u)
1872 {
1873         struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1874         struct nfs4_client *clp = c->cn_session->se_client;
1875
1876         trace_nfsd_cb_lost(clp);
1877
1878         spin_lock(&clp->cl_lock);
1879         if (!list_empty(&c->cn_persession)) {
1880                 list_del(&c->cn_persession);
1881                 free_conn(c);
1882         }
1883         nfsd4_probe_callback(clp);
1884         spin_unlock(&clp->cl_lock);
1885 }
1886
1887 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
1888 {
1889         struct nfsd4_conn *conn;
1890
1891         conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1892         if (!conn)
1893                 return NULL;
1894         svc_xprt_get(rqstp->rq_xprt);
1895         conn->cn_xprt = rqstp->rq_xprt;
1896         conn->cn_flags = flags;
1897         INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1898         return conn;
1899 }
1900
1901 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1902 {
1903         conn->cn_session = ses;
1904         list_add(&conn->cn_persession, &ses->se_conns);
1905 }
1906
1907 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1908 {
1909         struct nfs4_client *clp = ses->se_client;
1910
1911         spin_lock(&clp->cl_lock);
1912         __nfsd4_hash_conn(conn, ses);
1913         spin_unlock(&clp->cl_lock);
1914 }
1915
1916 static int nfsd4_register_conn(struct nfsd4_conn *conn)
1917 {
1918         conn->cn_xpt_user.callback = nfsd4_conn_lost;
1919         return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
1920 }
1921
1922 static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
1923 {
1924         int ret;
1925
1926         nfsd4_hash_conn(conn, ses);
1927         ret = nfsd4_register_conn(conn);
1928         if (ret)
1929                 /* oops; xprt is already down: */
1930                 nfsd4_conn_lost(&conn->cn_xpt_user);
1931         /* We may have gained or lost a callback channel: */
1932         nfsd4_probe_callback_sync(ses->se_client);
1933 }
1934
1935 static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
1936 {
1937         u32 dir = NFS4_CDFC4_FORE;
1938
1939         if (cses->flags & SESSION4_BACK_CHAN)
1940                 dir |= NFS4_CDFC4_BACK;
1941         return alloc_conn(rqstp, dir);
1942 }
1943
1944 /* must be called under client_lock */
1945 static void nfsd4_del_conns(struct nfsd4_session *s)
1946 {
1947         struct nfs4_client *clp = s->se_client;
1948         struct nfsd4_conn *c;
1949
1950         spin_lock(&clp->cl_lock);
1951         while (!list_empty(&s->se_conns)) {
1952                 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1953                 list_del_init(&c->cn_persession);
1954                 spin_unlock(&clp->cl_lock);
1955
1956                 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1957                 free_conn(c);
1958
1959                 spin_lock(&clp->cl_lock);
1960         }
1961         spin_unlock(&clp->cl_lock);
1962 }
1963
1964 static void __free_session(struct nfsd4_session *ses)
1965 {
1966         free_session_slots(ses);
1967         kfree(ses);
1968 }
1969
1970 static void free_session(struct nfsd4_session *ses)
1971 {
1972         nfsd4_del_conns(ses);
1973         nfsd4_put_drc_mem(&ses->se_fchannel);
1974         __free_session(ses);
1975 }
1976
1977 static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
1978 {
1979         int idx;
1980         struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1981
1982         new->se_client = clp;
1983         gen_sessionid(new);
1984
1985         INIT_LIST_HEAD(&new->se_conns);
1986
1987         new->se_cb_seq_nr = 1;
1988         new->se_flags = cses->flags;
1989         new->se_cb_prog = cses->callback_prog;
1990         new->se_cb_sec = cses->cb_sec;
1991         atomic_set(&new->se_ref, 0);
1992         idx = hash_sessionid(&new->se_sessionid);
1993         list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
1994         spin_lock(&clp->cl_lock);
1995         list_add(&new->se_perclnt, &clp->cl_sessions);
1996         spin_unlock(&clp->cl_lock);
1997
1998         {
1999                 struct sockaddr *sa = svc_addr(rqstp);
2000                 /*
2001                  * This is a little silly; with sessions there's no real
2002                  * use for the callback address.  Use the peer address
2003                  * as a reasonable default for now, but consider fixing
2004                  * the rpc client not to require an address in the
2005                  * future:
2006                  */
2007                 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
2008                 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
2009         }
2010 }
2011
2012 /* caller must hold client_lock */
2013 static struct nfsd4_session *
2014 __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
2015 {
2016         struct nfsd4_session *elem;
2017         int idx;
2018         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2019
2020         lockdep_assert_held(&nn->client_lock);
2021
2022         dump_sessionid(__func__, sessionid);
2023         idx = hash_sessionid(sessionid);
2024         /* Search in the appropriate list */
2025         list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
2026                 if (!memcmp(elem->se_sessionid.data, sessionid->data,
2027                             NFS4_MAX_SESSIONID_LEN)) {
2028                         return elem;
2029                 }
2030         }
2031
2032         dprintk("%s: session not found\n", __func__);
2033         return NULL;
2034 }
2035
2036 static struct nfsd4_session *
2037 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
2038                 __be32 *ret)
2039 {
2040         struct nfsd4_session *session;
2041         __be32 status = nfserr_badsession;
2042
2043         session = __find_in_sessionid_hashtbl(sessionid, net);
2044         if (!session)
2045                 goto out;
2046         status = nfsd4_get_session_locked(session);
2047         if (status)
2048                 session = NULL;
2049 out:
2050         *ret = status;
2051         return session;
2052 }
2053
2054 /* caller must hold client_lock */
2055 static void
2056 unhash_session(struct nfsd4_session *ses)
2057 {
2058         struct nfs4_client *clp = ses->se_client;
2059         struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2060
2061         lockdep_assert_held(&nn->client_lock);
2062
2063         list_del(&ses->se_hash);
2064         spin_lock(&ses->se_client->cl_lock);
2065         list_del(&ses->se_perclnt);
2066         spin_unlock(&ses->se_client->cl_lock);
2067 }
2068
2069 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
2070 static int
2071 STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
2072 {
2073         /*
2074          * We're assuming the clid was not given out from a boot
2075          * precisely 2^32 (about 136 years) before this one.  That seems
2076          * a safe assumption:
2077          */
2078         if (clid->cl_boot == (u32)nn->boot_time)
2079                 return 0;
2080         trace_nfsd_clid_stale(clid);
2081         return 1;
2082 }
2083
2084 /* 
2085  * XXX Should we use a slab cache ?
2086  * This type of memory management is somewhat inefficient, but we use it
2087  * anyway since SETCLIENTID is not a common operation.
2088  */
2089 static struct nfs4_client *alloc_client(struct xdr_netobj name,
2090                                 struct nfsd_net *nn)
2091 {
2092         struct nfs4_client *clp;
2093         int i;
2094
2095         if (atomic_read(&nn->nfs4_client_count) >= nn->nfs4_max_clients) {
2096                 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
2097                 return NULL;
2098         }
2099         clp = kmem_cache_zalloc(client_slab, GFP_KERNEL);
2100         if (clp == NULL)
2101                 return NULL;
2102         xdr_netobj_dup(&clp->cl_name, &name, GFP_KERNEL);
2103         if (clp->cl_name.data == NULL)
2104                 goto err_no_name;
2105         clp->cl_ownerstr_hashtbl = kmalloc_array(OWNER_HASH_SIZE,
2106                                                  sizeof(struct list_head),
2107                                                  GFP_KERNEL);
2108         if (!clp->cl_ownerstr_hashtbl)
2109                 goto err_no_hashtbl;
2110         for (i = 0; i < OWNER_HASH_SIZE; i++)
2111                 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
2112         INIT_LIST_HEAD(&clp->cl_sessions);
2113         idr_init(&clp->cl_stateids);
2114         atomic_set(&clp->cl_rpc_users, 0);
2115         clp->cl_cb_state = NFSD4_CB_UNKNOWN;
2116         clp->cl_state = NFSD4_ACTIVE;
2117         atomic_inc(&nn->nfs4_client_count);
2118         atomic_set(&clp->cl_delegs_in_recall, 0);
2119         INIT_LIST_HEAD(&clp->cl_idhash);
2120         INIT_LIST_HEAD(&clp->cl_openowners);
2121         INIT_LIST_HEAD(&clp->cl_delegations);
2122         INIT_LIST_HEAD(&clp->cl_lru);
2123         INIT_LIST_HEAD(&clp->cl_revoked);
2124 #ifdef CONFIG_NFSD_PNFS
2125         INIT_LIST_HEAD(&clp->cl_lo_states);
2126 #endif
2127         INIT_LIST_HEAD(&clp->async_copies);
2128         spin_lock_init(&clp->async_lock);
2129         spin_lock_init(&clp->cl_lock);
2130         rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
2131         return clp;
2132 err_no_hashtbl:
2133         kfree(clp->cl_name.data);
2134 err_no_name:
2135         kmem_cache_free(client_slab, clp);
2136         return NULL;
2137 }
2138
2139 static void __free_client(struct kref *k)
2140 {
2141         struct nfsdfs_client *c = container_of(k, struct nfsdfs_client, cl_ref);
2142         struct nfs4_client *clp = container_of(c, struct nfs4_client, cl_nfsdfs);
2143
2144         free_svc_cred(&clp->cl_cred);
2145         kfree(clp->cl_ownerstr_hashtbl);
2146         kfree(clp->cl_name.data);
2147         kfree(clp->cl_nii_domain.data);
2148         kfree(clp->cl_nii_name.data);
2149         idr_destroy(&clp->cl_stateids);
2150         kfree(clp->cl_ra);
2151         kmem_cache_free(client_slab, clp);
2152 }
2153
2154 static void drop_client(struct nfs4_client *clp)
2155 {
2156         kref_put(&clp->cl_nfsdfs.cl_ref, __free_client);
2157 }
2158
2159 static void
2160 free_client(struct nfs4_client *clp)
2161 {
2162         while (!list_empty(&clp->cl_sessions)) {
2163                 struct nfsd4_session *ses;
2164                 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
2165                                 se_perclnt);
2166                 list_del(&ses->se_perclnt);
2167                 WARN_ON_ONCE(atomic_read(&ses->se_ref));
2168                 free_session(ses);
2169         }
2170         rpc_destroy_wait_queue(&clp->cl_cb_waitq);
2171         if (clp->cl_nfsd_dentry) {
2172                 nfsd_client_rmdir(clp->cl_nfsd_dentry);
2173                 clp->cl_nfsd_dentry = NULL;
2174                 wake_up_all(&expiry_wq);
2175         }
2176         drop_client(clp);
2177 }
2178
2179 /* must be called under the client_lock */
2180 static void
2181 unhash_client_locked(struct nfs4_client *clp)
2182 {
2183         struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2184         struct nfsd4_session *ses;
2185
2186         lockdep_assert_held(&nn->client_lock);
2187
2188         /* Mark the client as expired! */
2189         clp->cl_time = 0;
2190         /* Make it invisible */
2191         if (!list_empty(&clp->cl_idhash)) {
2192                 list_del_init(&clp->cl_idhash);
2193                 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
2194                         rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
2195                 else
2196                         rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
2197         }
2198         list_del_init(&clp->cl_lru);
2199         spin_lock(&clp->cl_lock);
2200         list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
2201                 list_del_init(&ses->se_hash);
2202         spin_unlock(&clp->cl_lock);
2203 }
2204
2205 static void
2206 unhash_client(struct nfs4_client *clp)
2207 {
2208         struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2209
2210         spin_lock(&nn->client_lock);
2211         unhash_client_locked(clp);
2212         spin_unlock(&nn->client_lock);
2213 }
2214
2215 static __be32 mark_client_expired_locked(struct nfs4_client *clp)
2216 {
2217         if (atomic_read(&clp->cl_rpc_users))
2218                 return nfserr_jukebox;
2219         unhash_client_locked(clp);
2220         return nfs_ok;
2221 }
2222
2223 static void
2224 __destroy_client(struct nfs4_client *clp)
2225 {
2226         struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2227         int i;
2228         struct nfs4_openowner *oo;
2229         struct nfs4_delegation *dp;
2230         struct list_head reaplist;
2231
2232         INIT_LIST_HEAD(&reaplist);
2233         spin_lock(&state_lock);
2234         while (!list_empty(&clp->cl_delegations)) {
2235                 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
2236                 WARN_ON(!unhash_delegation_locked(dp));
2237                 list_add(&dp->dl_recall_lru, &reaplist);
2238         }
2239         spin_unlock(&state_lock);
2240         while (!list_empty(&reaplist)) {
2241                 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
2242                 list_del_init(&dp->dl_recall_lru);
2243                 destroy_unhashed_deleg(dp);
2244         }
2245         while (!list_empty(&clp->cl_revoked)) {
2246                 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
2247                 list_del_init(&dp->dl_recall_lru);
2248                 nfs4_put_stid(&dp->dl_stid);
2249         }
2250         while (!list_empty(&clp->cl_openowners)) {
2251                 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
2252                 nfs4_get_stateowner(&oo->oo_owner);
2253                 release_openowner(oo);
2254         }
2255         for (i = 0; i < OWNER_HASH_SIZE; i++) {
2256                 struct nfs4_stateowner *so, *tmp;
2257
2258                 list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i],
2259                                          so_strhash) {
2260                         /* Should be no openowners at this point */
2261                         WARN_ON_ONCE(so->so_is_open_owner);
2262                         remove_blocked_locks(lockowner(so));
2263                 }
2264         }
2265         nfsd4_return_all_client_layouts(clp);
2266         nfsd4_shutdown_copy(clp);
2267         nfsd4_shutdown_callback(clp);
2268         if (clp->cl_cb_conn.cb_xprt)
2269                 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
2270         atomic_add_unless(&nn->nfs4_client_count, -1, 0);
2271         nfsd4_dec_courtesy_client_count(nn, clp);
2272         free_client(clp);
2273         wake_up_all(&expiry_wq);
2274 }
2275
2276 static void
2277 destroy_client(struct nfs4_client *clp)
2278 {
2279         unhash_client(clp);
2280         __destroy_client(clp);
2281 }
2282
2283 static void inc_reclaim_complete(struct nfs4_client *clp)
2284 {
2285         struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2286
2287         if (!nn->track_reclaim_completes)
2288                 return;
2289         if (!nfsd4_find_reclaim_client(clp->cl_name, nn))
2290                 return;
2291         if (atomic_inc_return(&nn->nr_reclaim_complete) ==
2292                         nn->reclaim_str_hashtbl_size) {
2293                 printk(KERN_INFO "NFSD: all clients done reclaiming, ending NFSv4 grace period (net %x)\n",
2294                                 clp->net->ns.inum);
2295                 nfsd4_end_grace(nn);
2296         }
2297 }
2298
2299 static void expire_client(struct nfs4_client *clp)
2300 {
2301         unhash_client(clp);
2302         nfsd4_client_record_remove(clp);
2303         __destroy_client(clp);
2304 }
2305
2306 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
2307 {
2308         memcpy(target->cl_verifier.data, source->data,
2309                         sizeof(target->cl_verifier.data));
2310 }
2311
2312 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
2313 {
2314         target->cl_clientid.cl_boot = source->cl_clientid.cl_boot; 
2315         target->cl_clientid.cl_id = source->cl_clientid.cl_id; 
2316 }
2317
2318 static int copy_cred(struct svc_cred *target, struct svc_cred *source)
2319 {
2320         target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL);
2321         target->cr_raw_principal = kstrdup(source->cr_raw_principal,
2322                                                                 GFP_KERNEL);
2323         target->cr_targ_princ = kstrdup(source->cr_targ_princ, GFP_KERNEL);
2324         if ((source->cr_principal && !target->cr_principal) ||
2325             (source->cr_raw_principal && !target->cr_raw_principal) ||
2326             (source->cr_targ_princ && !target->cr_targ_princ))
2327                 return -ENOMEM;
2328
2329         target->cr_flavor = source->cr_flavor;
2330         target->cr_uid = source->cr_uid;
2331         target->cr_gid = source->cr_gid;
2332         target->cr_group_info = source->cr_group_info;
2333         get_group_info(target->cr_group_info);
2334         target->cr_gss_mech = source->cr_gss_mech;
2335         if (source->cr_gss_mech)
2336                 gss_mech_get(source->cr_gss_mech);
2337         return 0;
2338 }
2339
2340 static int
2341 compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
2342 {
2343         if (o1->len < o2->len)
2344                 return -1;
2345         if (o1->len > o2->len)
2346                 return 1;
2347         return memcmp(o1->data, o2->data, o1->len);
2348 }
2349
2350 static int
2351 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
2352 {
2353         return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
2354 }
2355
2356 static int
2357 same_clid(clientid_t *cl1, clientid_t *cl2)
2358 {
2359         return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
2360 }
2361
2362 static bool groups_equal(struct group_info *g1, struct group_info *g2)
2363 {
2364         int i;
2365
2366         if (g1->ngroups != g2->ngroups)
2367                 return false;
2368         for (i=0; i<g1->ngroups; i++)
2369                 if (!gid_eq(g1->gid[i], g2->gid[i]))
2370                         return false;
2371         return true;
2372 }
2373
2374 /*
2375  * RFC 3530 language requires clid_inuse be returned when the
2376  * "principal" associated with a requests differs from that previously
2377  * used.  We use uid, gid's, and gss principal string as our best
2378  * approximation.  We also don't want to allow non-gss use of a client
2379  * established using gss: in theory cr_principal should catch that
2380  * change, but in practice cr_principal can be null even in the gss case
2381  * since gssd doesn't always pass down a principal string.
2382  */
2383 static bool is_gss_cred(struct svc_cred *cr)
2384 {
2385         /* Is cr_flavor one of the gss "pseudoflavors"?: */
2386         return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
2387 }
2388
2389
2390 static bool
2391 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
2392 {
2393         if ((is_gss_cred(cr1) != is_gss_cred(cr2))
2394                 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
2395                 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
2396                 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
2397                 return false;
2398         /* XXX: check that cr_targ_princ fields match ? */
2399         if (cr1->cr_principal == cr2->cr_principal)
2400                 return true;
2401         if (!cr1->cr_principal || !cr2->cr_principal)
2402                 return false;
2403         return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
2404 }
2405
2406 static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
2407 {
2408         struct svc_cred *cr = &rqstp->rq_cred;
2409         u32 service;
2410
2411         if (!cr->cr_gss_mech)
2412                 return false;
2413         service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
2414         return service == RPC_GSS_SVC_INTEGRITY ||
2415                service == RPC_GSS_SVC_PRIVACY;
2416 }
2417
2418 bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
2419 {
2420         struct svc_cred *cr = &rqstp->rq_cred;
2421
2422         if (!cl->cl_mach_cred)
2423                 return true;
2424         if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
2425                 return false;
2426         if (!svc_rqst_integrity_protected(rqstp))
2427                 return false;
2428         if (cl->cl_cred.cr_raw_principal)
2429                 return 0 == strcmp(cl->cl_cred.cr_raw_principal,
2430                                                 cr->cr_raw_principal);
2431         if (!cr->cr_principal)
2432                 return false;
2433         return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
2434 }
2435
2436 static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
2437 {
2438         __be32 verf[2];
2439
2440         /*
2441          * This is opaque to client, so no need to byte-swap. Use
2442          * __force to keep sparse happy
2443          */
2444         verf[0] = (__force __be32)(u32)ktime_get_real_seconds();
2445         verf[1] = (__force __be32)nn->clverifier_counter++;
2446         memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
2447 }
2448
2449 static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
2450 {
2451         clp->cl_clientid.cl_boot = (u32)nn->boot_time;
2452         clp->cl_clientid.cl_id = nn->clientid_counter++;
2453         gen_confirm(clp, nn);
2454 }
2455
2456 static struct nfs4_stid *
2457 find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
2458 {
2459         struct nfs4_stid *ret;
2460
2461         ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
2462         if (!ret || !ret->sc_type)
2463                 return NULL;
2464         return ret;
2465 }
2466
2467 static struct nfs4_stid *
2468 find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
2469 {
2470         struct nfs4_stid *s;
2471
2472         spin_lock(&cl->cl_lock);
2473         s = find_stateid_locked(cl, t);
2474         if (s != NULL) {
2475                 if (typemask & s->sc_type)
2476                         refcount_inc(&s->sc_count);
2477                 else
2478                         s = NULL;
2479         }
2480         spin_unlock(&cl->cl_lock);
2481         return s;
2482 }
2483
2484 static struct nfs4_client *get_nfsdfs_clp(struct inode *inode)
2485 {
2486         struct nfsdfs_client *nc;
2487         nc = get_nfsdfs_client(inode);
2488         if (!nc)
2489                 return NULL;
2490         return container_of(nc, struct nfs4_client, cl_nfsdfs);
2491 }
2492
2493 static void seq_quote_mem(struct seq_file *m, char *data, int len)
2494 {
2495         seq_printf(m, "\"");
2496         seq_escape_mem(m, data, len, ESCAPE_HEX | ESCAPE_NAP | ESCAPE_APPEND, "\"\\");
2497         seq_printf(m, "\"");
2498 }
2499
2500 static const char *cb_state2str(int state)
2501 {
2502         switch (state) {
2503         case NFSD4_CB_UP:
2504                 return "UP";
2505         case NFSD4_CB_UNKNOWN:
2506                 return "UNKNOWN";
2507         case NFSD4_CB_DOWN:
2508                 return "DOWN";
2509         case NFSD4_CB_FAULT:
2510                 return "FAULT";
2511         }
2512         return "UNDEFINED";
2513 }
2514
2515 static int client_info_show(struct seq_file *m, void *v)
2516 {
2517         struct inode *inode = file_inode(m->file);
2518         struct nfs4_client *clp;
2519         u64 clid;
2520
2521         clp = get_nfsdfs_clp(inode);
2522         if (!clp)
2523                 return -ENXIO;
2524         memcpy(&clid, &clp->cl_clientid, sizeof(clid));
2525         seq_printf(m, "clientid: 0x%llx\n", clid);
2526         seq_printf(m, "address: \"%pISpc\"\n", (struct sockaddr *)&clp->cl_addr);
2527
2528         if (clp->cl_state == NFSD4_COURTESY)
2529                 seq_puts(m, "status: courtesy\n");
2530         else if (clp->cl_state == NFSD4_EXPIRABLE)
2531                 seq_puts(m, "status: expirable\n");
2532         else if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
2533                 seq_puts(m, "status: confirmed\n");
2534         else
2535                 seq_puts(m, "status: unconfirmed\n");
2536         seq_printf(m, "seconds from last renew: %lld\n",
2537                 ktime_get_boottime_seconds() - clp->cl_time);
2538         seq_printf(m, "name: ");
2539         seq_quote_mem(m, clp->cl_name.data, clp->cl_name.len);
2540         seq_printf(m, "\nminor version: %d\n", clp->cl_minorversion);
2541         if (clp->cl_nii_domain.data) {
2542                 seq_printf(m, "Implementation domain: ");
2543                 seq_quote_mem(m, clp->cl_nii_domain.data,
2544                                         clp->cl_nii_domain.len);
2545                 seq_printf(m, "\nImplementation name: ");
2546                 seq_quote_mem(m, clp->cl_nii_name.data, clp->cl_nii_name.len);
2547                 seq_printf(m, "\nImplementation time: [%lld, %ld]\n",
2548                         clp->cl_nii_time.tv_sec, clp->cl_nii_time.tv_nsec);
2549         }
2550         seq_printf(m, "callback state: %s\n", cb_state2str(clp->cl_cb_state));
2551         seq_printf(m, "callback address: %pISpc\n", &clp->cl_cb_conn.cb_addr);
2552         drop_client(clp);
2553
2554         return 0;
2555 }
2556
2557 DEFINE_SHOW_ATTRIBUTE(client_info);
2558
2559 static void *states_start(struct seq_file *s, loff_t *pos)
2560         __acquires(&clp->cl_lock)
2561 {
2562         struct nfs4_client *clp = s->private;
2563         unsigned long id = *pos;
2564         void *ret;
2565
2566         spin_lock(&clp->cl_lock);
2567         ret = idr_get_next_ul(&clp->cl_stateids, &id);
2568         *pos = id;
2569         return ret;
2570 }
2571
2572 static void *states_next(struct seq_file *s, void *v, loff_t *pos)
2573 {
2574         struct nfs4_client *clp = s->private;
2575         unsigned long id = *pos;
2576         void *ret;
2577
2578         id = *pos;
2579         id++;
2580         ret = idr_get_next_ul(&clp->cl_stateids, &id);
2581         *pos = id;
2582         return ret;
2583 }
2584
2585 static void states_stop(struct seq_file *s, void *v)
2586         __releases(&clp->cl_lock)
2587 {
2588         struct nfs4_client *clp = s->private;
2589
2590         spin_unlock(&clp->cl_lock);
2591 }
2592
2593 static void nfs4_show_fname(struct seq_file *s, struct nfsd_file *f)
2594 {
2595          seq_printf(s, "filename: \"%pD2\"", f->nf_file);
2596 }
2597
2598 static void nfs4_show_superblock(struct seq_file *s, struct nfsd_file *f)
2599 {
2600         struct inode *inode = file_inode(f->nf_file);
2601
2602         seq_printf(s, "superblock: \"%02x:%02x:%ld\"",
2603                                         MAJOR(inode->i_sb->s_dev),
2604                                          MINOR(inode->i_sb->s_dev),
2605                                          inode->i_ino);
2606 }
2607
2608 static void nfs4_show_owner(struct seq_file *s, struct nfs4_stateowner *oo)
2609 {
2610         seq_printf(s, "owner: ");
2611         seq_quote_mem(s, oo->so_owner.data, oo->so_owner.len);
2612 }
2613
2614 static void nfs4_show_stateid(struct seq_file *s, stateid_t *stid)
2615 {
2616         seq_printf(s, "0x%.8x", stid->si_generation);
2617         seq_printf(s, "%12phN", &stid->si_opaque);
2618 }
2619
2620 static int nfs4_show_open(struct seq_file *s, struct nfs4_stid *st)
2621 {
2622         struct nfs4_ol_stateid *ols;
2623         struct nfs4_file *nf;
2624         struct nfsd_file *file;
2625         struct nfs4_stateowner *oo;
2626         unsigned int access, deny;
2627
2628         if (st->sc_type != NFS4_OPEN_STID && st->sc_type != NFS4_LOCK_STID)
2629                 return 0; /* XXX: or SEQ_SKIP? */
2630         ols = openlockstateid(st);
2631         oo = ols->st_stateowner;
2632         nf = st->sc_file;
2633
2634         spin_lock(&nf->fi_lock);
2635         file = find_any_file_locked(nf);
2636         if (!file)
2637                 goto out;
2638
2639         seq_printf(s, "- ");
2640         nfs4_show_stateid(s, &st->sc_stateid);
2641         seq_printf(s, ": { type: open, ");
2642
2643         access = bmap_to_share_mode(ols->st_access_bmap);
2644         deny   = bmap_to_share_mode(ols->st_deny_bmap);
2645
2646         seq_printf(s, "access: %s%s, ",
2647                 access & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2648                 access & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2649         seq_printf(s, "deny: %s%s, ",
2650                 deny & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2651                 deny & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2652
2653         nfs4_show_superblock(s, file);
2654         seq_printf(s, ", ");
2655         nfs4_show_fname(s, file);
2656         seq_printf(s, ", ");
2657         nfs4_show_owner(s, oo);
2658         seq_printf(s, " }\n");
2659 out:
2660         spin_unlock(&nf->fi_lock);
2661         return 0;
2662 }
2663
2664 static int nfs4_show_lock(struct seq_file *s, struct nfs4_stid *st)
2665 {
2666         struct nfs4_ol_stateid *ols;
2667         struct nfs4_file *nf;
2668         struct nfsd_file *file;
2669         struct nfs4_stateowner *oo;
2670
2671         ols = openlockstateid(st);
2672         oo = ols->st_stateowner;
2673         nf = st->sc_file;
2674         spin_lock(&nf->fi_lock);
2675         file = find_any_file_locked(nf);
2676         if (!file)
2677                 goto out;
2678
2679         seq_printf(s, "- ");
2680         nfs4_show_stateid(s, &st->sc_stateid);
2681         seq_printf(s, ": { type: lock, ");
2682
2683         /*
2684          * Note: a lock stateid isn't really the same thing as a lock,
2685          * it's the locking state held by one owner on a file, and there
2686          * may be multiple (or no) lock ranges associated with it.
2687          * (Same for the matter is true of open stateids.)
2688          */
2689
2690         nfs4_show_superblock(s, file);
2691         /* XXX: open stateid? */
2692         seq_printf(s, ", ");
2693         nfs4_show_fname(s, file);
2694         seq_printf(s, ", ");
2695         nfs4_show_owner(s, oo);
2696         seq_printf(s, " }\n");
2697 out:
2698         spin_unlock(&nf->fi_lock);
2699         return 0;
2700 }
2701
2702 static int nfs4_show_deleg(struct seq_file *s, struct nfs4_stid *st)
2703 {
2704         struct nfs4_delegation *ds;
2705         struct nfs4_file *nf;
2706         struct nfsd_file *file;
2707
2708         ds = delegstateid(st);
2709         nf = st->sc_file;
2710         spin_lock(&nf->fi_lock);
2711         file = nf->fi_deleg_file;
2712         if (!file)
2713                 goto out;
2714
2715         seq_printf(s, "- ");
2716         nfs4_show_stateid(s, &st->sc_stateid);
2717         seq_printf(s, ": { type: deleg, ");
2718
2719         /* Kinda dead code as long as we only support read delegs: */
2720         seq_printf(s, "access: %s, ",
2721                 ds->dl_type == NFS4_OPEN_DELEGATE_READ ? "r" : "w");
2722
2723         /* XXX: lease time, whether it's being recalled. */
2724
2725         nfs4_show_superblock(s, file);
2726         seq_printf(s, ", ");
2727         nfs4_show_fname(s, file);
2728         seq_printf(s, " }\n");
2729 out:
2730         spin_unlock(&nf->fi_lock);
2731         return 0;
2732 }
2733
2734 static int nfs4_show_layout(struct seq_file *s, struct nfs4_stid *st)
2735 {
2736         struct nfs4_layout_stateid *ls;
2737         struct nfsd_file *file;
2738
2739         ls = container_of(st, struct nfs4_layout_stateid, ls_stid);
2740         file = ls->ls_file;
2741
2742         seq_printf(s, "- ");
2743         nfs4_show_stateid(s, &st->sc_stateid);
2744         seq_printf(s, ": { type: layout, ");
2745
2746         /* XXX: What else would be useful? */
2747
2748         nfs4_show_superblock(s, file);
2749         seq_printf(s, ", ");
2750         nfs4_show_fname(s, file);
2751         seq_printf(s, " }\n");
2752
2753         return 0;
2754 }
2755
2756 static int states_show(struct seq_file *s, void *v)
2757 {
2758         struct nfs4_stid *st = v;
2759
2760         switch (st->sc_type) {
2761         case NFS4_OPEN_STID:
2762                 return nfs4_show_open(s, st);
2763         case NFS4_LOCK_STID:
2764                 return nfs4_show_lock(s, st);
2765         case NFS4_DELEG_STID:
2766                 return nfs4_show_deleg(s, st);
2767         case NFS4_LAYOUT_STID:
2768                 return nfs4_show_layout(s, st);
2769         default:
2770                 return 0; /* XXX: or SEQ_SKIP? */
2771         }
2772         /* XXX: copy stateids? */
2773 }
2774
2775 static struct seq_operations states_seq_ops = {
2776         .start = states_start,
2777         .next = states_next,
2778         .stop = states_stop,
2779         .show = states_show
2780 };
2781
2782 static int client_states_open(struct inode *inode, struct file *file)
2783 {
2784         struct seq_file *s;
2785         struct nfs4_client *clp;
2786         int ret;
2787
2788         clp = get_nfsdfs_clp(inode);
2789         if (!clp)
2790                 return -ENXIO;
2791
2792         ret = seq_open(file, &states_seq_ops);
2793         if (ret)
2794                 return ret;
2795         s = file->private_data;
2796         s->private = clp;
2797         return 0;
2798 }
2799
2800 static int client_opens_release(struct inode *inode, struct file *file)
2801 {
2802         struct seq_file *m = file->private_data;
2803         struct nfs4_client *clp = m->private;
2804
2805         /* XXX: alternatively, we could get/drop in seq start/stop */
2806         drop_client(clp);
2807         return seq_release(inode, file);
2808 }
2809
2810 static const struct file_operations client_states_fops = {
2811         .open           = client_states_open,
2812         .read           = seq_read,
2813         .llseek         = seq_lseek,
2814         .release        = client_opens_release,
2815 };
2816
2817 /*
2818  * Normally we refuse to destroy clients that are in use, but here the
2819  * administrator is telling us to just do it.  We also want to wait
2820  * so the caller has a guarantee that the client's locks are gone by
2821  * the time the write returns:
2822  */
2823 static void force_expire_client(struct nfs4_client *clp)
2824 {
2825         struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2826         bool already_expired;
2827
2828         trace_nfsd_clid_admin_expired(&clp->cl_clientid);
2829
2830         spin_lock(&nn->client_lock);
2831         clp->cl_time = 0;
2832         spin_unlock(&nn->client_lock);
2833
2834         wait_event(expiry_wq, atomic_read(&clp->cl_rpc_users) == 0);
2835         spin_lock(&nn->client_lock);
2836         already_expired = list_empty(&clp->cl_lru);
2837         if (!already_expired)
2838                 unhash_client_locked(clp);
2839         spin_unlock(&nn->client_lock);
2840
2841         if (!already_expired)
2842                 expire_client(clp);
2843         else
2844                 wait_event(expiry_wq, clp->cl_nfsd_dentry == NULL);
2845 }
2846
2847 static ssize_t client_ctl_write(struct file *file, const char __user *buf,
2848                                    size_t size, loff_t *pos)
2849 {
2850         char *data;
2851         struct nfs4_client *clp;
2852
2853         data = simple_transaction_get(file, buf, size);
2854         if (IS_ERR(data))
2855                 return PTR_ERR(data);
2856         if (size != 7 || 0 != memcmp(data, "expire\n", 7))
2857                 return -EINVAL;
2858         clp = get_nfsdfs_clp(file_inode(file));
2859         if (!clp)
2860                 return -ENXIO;
2861         force_expire_client(clp);
2862         drop_client(clp);
2863         return 7;
2864 }
2865
2866 static const struct file_operations client_ctl_fops = {
2867         .write          = client_ctl_write,
2868         .release        = simple_transaction_release,
2869 };
2870
2871 static const struct tree_descr client_files[] = {
2872         [0] = {"info", &client_info_fops, S_IRUSR},
2873         [1] = {"states", &client_states_fops, S_IRUSR},
2874         [2] = {"ctl", &client_ctl_fops, S_IWUSR},
2875         [3] = {""},
2876 };
2877
2878 static int
2879 nfsd4_cb_recall_any_done(struct nfsd4_callback *cb,
2880                                 struct rpc_task *task)
2881 {
2882         trace_nfsd_cb_recall_any_done(cb, task);
2883         switch (task->tk_status) {
2884         case -NFS4ERR_DELAY:
2885                 rpc_delay(task, 2 * HZ);
2886                 return 0;
2887         default:
2888                 return 1;
2889         }
2890 }
2891
2892 static void
2893 nfsd4_cb_recall_any_release(struct nfsd4_callback *cb)
2894 {
2895         struct nfs4_client *clp = cb->cb_clp;
2896         struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2897
2898         spin_lock(&nn->client_lock);
2899         clear_bit(NFSD4_CLIENT_CB_RECALL_ANY, &clp->cl_flags);
2900         put_client_renew_locked(clp);
2901         spin_unlock(&nn->client_lock);
2902 }
2903
2904 static int
2905 nfsd4_cb_getattr_done(struct nfsd4_callback *cb, struct rpc_task *task)
2906 {
2907         struct nfs4_cb_fattr *ncf =
2908                         container_of(cb, struct nfs4_cb_fattr, ncf_getattr);
2909
2910         ncf->ncf_cb_status = task->tk_status;
2911         switch (task->tk_status) {
2912         case -NFS4ERR_DELAY:
2913                 rpc_delay(task, 2 * HZ);
2914                 return 0;
2915         default:
2916                 return 1;
2917         }
2918 }
2919
2920 static void
2921 nfsd4_cb_getattr_release(struct nfsd4_callback *cb)
2922 {
2923         struct nfs4_cb_fattr *ncf =
2924                         container_of(cb, struct nfs4_cb_fattr, ncf_getattr);
2925         struct nfs4_delegation *dp =
2926                         container_of(ncf, struct nfs4_delegation, dl_cb_fattr);
2927
2928         nfs4_put_stid(&dp->dl_stid);
2929         clear_bit(CB_GETATTR_BUSY, &ncf->ncf_cb_flags);
2930         wake_up_bit(&ncf->ncf_cb_flags, CB_GETATTR_BUSY);
2931 }
2932
2933 static const struct nfsd4_callback_ops nfsd4_cb_recall_any_ops = {
2934         .done           = nfsd4_cb_recall_any_done,
2935         .release        = nfsd4_cb_recall_any_release,
2936 };
2937
2938 static const struct nfsd4_callback_ops nfsd4_cb_getattr_ops = {
2939         .done           = nfsd4_cb_getattr_done,
2940         .release        = nfsd4_cb_getattr_release,
2941 };
2942
2943 void nfs4_cb_getattr(struct nfs4_cb_fattr *ncf)
2944 {
2945         struct nfs4_delegation *dp =
2946                         container_of(ncf, struct nfs4_delegation, dl_cb_fattr);
2947
2948         if (test_and_set_bit(CB_GETATTR_BUSY, &ncf->ncf_cb_flags))
2949                 return;
2950         refcount_inc(&dp->dl_stid.sc_count);
2951         nfsd4_run_cb(&ncf->ncf_getattr);
2952 }
2953
2954 static struct nfs4_client *create_client(struct xdr_netobj name,
2955                 struct svc_rqst *rqstp, nfs4_verifier *verf)
2956 {
2957         struct nfs4_client *clp;
2958         struct sockaddr *sa = svc_addr(rqstp);
2959         int ret;
2960         struct net *net = SVC_NET(rqstp);
2961         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2962         struct dentry *dentries[ARRAY_SIZE(client_files)];
2963
2964         clp = alloc_client(name, nn);
2965         if (clp == NULL)
2966                 return NULL;
2967
2968         ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
2969         if (ret) {
2970                 free_client(clp);
2971                 return NULL;
2972         }
2973         gen_clid(clp, nn);
2974         kref_init(&clp->cl_nfsdfs.cl_ref);
2975         nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
2976         clp->cl_time = ktime_get_boottime_seconds();
2977         clear_bit(0, &clp->cl_cb_slot_busy);
2978         copy_verf(clp, verf);
2979         memcpy(&clp->cl_addr, sa, sizeof(struct sockaddr_storage));
2980         clp->cl_cb_session = NULL;
2981         clp->net = net;
2982         clp->cl_nfsd_dentry = nfsd_client_mkdir(
2983                 nn, &clp->cl_nfsdfs,
2984                 clp->cl_clientid.cl_id - nn->clientid_base,
2985                 client_files, dentries);
2986         clp->cl_nfsd_info_dentry = dentries[0];
2987         if (!clp->cl_nfsd_dentry) {
2988                 free_client(clp);
2989                 return NULL;
2990         }
2991         clp->cl_ra = kzalloc(sizeof(*clp->cl_ra), GFP_KERNEL);
2992         if (!clp->cl_ra) {
2993                 free_client(clp);
2994                 return NULL;
2995         }
2996         clp->cl_ra_time = 0;
2997         nfsd4_init_cb(&clp->cl_ra->ra_cb, clp, &nfsd4_cb_recall_any_ops,
2998                         NFSPROC4_CLNT_CB_RECALL_ANY);
2999         return clp;
3000 }
3001
3002 static void
3003 add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
3004 {
3005         struct rb_node **new = &(root->rb_node), *parent = NULL;
3006         struct nfs4_client *clp;
3007
3008         while (*new) {
3009                 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
3010                 parent = *new;
3011
3012                 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
3013                         new = &((*new)->rb_left);
3014                 else
3015                         new = &((*new)->rb_right);
3016         }
3017
3018         rb_link_node(&new_clp->cl_namenode, parent, new);
3019         rb_insert_color(&new_clp->cl_namenode, root);
3020 }
3021
3022 static struct nfs4_client *
3023 find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
3024 {
3025         int cmp;
3026         struct rb_node *node = root->rb_node;
3027         struct nfs4_client *clp;
3028
3029         while (node) {
3030                 clp = rb_entry(node, struct nfs4_client, cl_namenode);
3031                 cmp = compare_blob(&clp->cl_name, name);
3032                 if (cmp > 0)
3033                         node = node->rb_left;
3034                 else if (cmp < 0)
3035                         node = node->rb_right;
3036                 else
3037                         return clp;
3038         }
3039         return NULL;
3040 }
3041
3042 static void
3043 add_to_unconfirmed(struct nfs4_client *clp)
3044 {
3045         unsigned int idhashval;
3046         struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
3047
3048         lockdep_assert_held(&nn->client_lock);
3049
3050         clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
3051         add_clp_to_name_tree(clp, &nn->unconf_name_tree);
3052         idhashval = clientid_hashval(clp->cl_clientid.cl_id);
3053         list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
3054         renew_client_locked(clp);
3055 }
3056
3057 static void
3058 move_to_confirmed(struct nfs4_client *clp)
3059 {
3060         unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
3061         struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
3062
3063         lockdep_assert_held(&nn->client_lock);
3064
3065         list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
3066         rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
3067         add_clp_to_name_tree(clp, &nn->conf_name_tree);
3068         set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
3069         trace_nfsd_clid_confirmed(&clp->cl_clientid);
3070         renew_client_locked(clp);
3071 }
3072
3073 static struct nfs4_client *
3074 find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
3075 {
3076         struct nfs4_client *clp;
3077         unsigned int idhashval = clientid_hashval(clid->cl_id);
3078
3079         list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
3080                 if (same_clid(&clp->cl_clientid, clid)) {
3081                         if ((bool)clp->cl_minorversion != sessions)
3082                                 return NULL;
3083                         renew_client_locked(clp);
3084                         return clp;
3085                 }
3086         }
3087         return NULL;
3088 }
3089
3090 static struct nfs4_client *
3091 find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
3092 {
3093         struct list_head *tbl = nn->conf_id_hashtbl;
3094
3095         lockdep_assert_held(&nn->client_lock);
3096         return find_client_in_id_table(tbl, clid, sessions);
3097 }
3098
3099 static struct nfs4_client *
3100 find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
3101 {
3102         struct list_head *tbl = nn->unconf_id_hashtbl;
3103
3104         lockdep_assert_held(&nn->client_lock);
3105         return find_client_in_id_table(tbl, clid, sessions);
3106 }
3107
3108 static bool clp_used_exchangeid(struct nfs4_client *clp)
3109 {
3110         return clp->cl_exchange_flags != 0;
3111
3112
3113 static struct nfs4_client *
3114 find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
3115 {
3116         lockdep_assert_held(&nn->client_lock);
3117         return find_clp_in_name_tree(name, &nn->conf_name_tree);
3118 }
3119
3120 static struct nfs4_client *
3121 find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
3122 {
3123         lockdep_assert_held(&nn->client_lock);
3124         return find_clp_in_name_tree(name, &nn->unconf_name_tree);
3125 }
3126
3127 static void
3128 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
3129 {
3130         struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
3131         struct sockaddr *sa = svc_addr(rqstp);
3132         u32 scopeid = rpc_get_scope_id(sa);
3133         unsigned short expected_family;
3134
3135         /* Currently, we only support tcp and tcp6 for the callback channel */
3136         if (se->se_callback_netid_len == 3 &&
3137             !memcmp(se->se_callback_netid_val, "tcp", 3))
3138                 expected_family = AF_INET;
3139         else if (se->se_callback_netid_len == 4 &&
3140                  !memcmp(se->se_callback_netid_val, "tcp6", 4))
3141                 expected_family = AF_INET6;
3142         else
3143                 goto out_err;
3144
3145         conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
3146                                             se->se_callback_addr_len,
3147                                             (struct sockaddr *)&conn->cb_addr,
3148                                             sizeof(conn->cb_addr));
3149
3150         if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
3151                 goto out_err;
3152
3153         if (conn->cb_addr.ss_family == AF_INET6)
3154                 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
3155
3156         conn->cb_prog = se->se_callback_prog;
3157         conn->cb_ident = se->se_callback_ident;
3158         memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
3159         trace_nfsd_cb_args(clp, conn);
3160         return;
3161 out_err:
3162         conn->cb_addr.ss_family = AF_UNSPEC;
3163         conn->cb_addrlen = 0;
3164         trace_nfsd_cb_nodelegs(clp);
3165         return;
3166 }
3167
3168 /*
3169  * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
3170  */
3171 static void
3172 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
3173 {
3174         struct xdr_buf *buf = resp->xdr->buf;
3175         struct nfsd4_slot *slot = resp->cstate.slot;
3176         unsigned int base;
3177
3178         dprintk("--> %s slot %p\n", __func__, slot);
3179
3180         slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
3181         slot->sl_opcnt = resp->opcnt;
3182         slot->sl_status = resp->cstate.status;
3183         free_svc_cred(&slot->sl_cred);
3184         copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred);
3185
3186         if (!nfsd4_cache_this(resp)) {
3187                 slot->sl_flags &= ~NFSD4_SLOT_CACHED;
3188                 return;
3189         }
3190         slot->sl_flags |= NFSD4_SLOT_CACHED;
3191
3192         base = resp->cstate.data_offset;
3193         slot->sl_datalen = buf->len - base;
3194         if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
3195                 WARN(1, "%s: sessions DRC could not cache compound\n",
3196                      __func__);
3197         return;
3198 }
3199
3200 /*
3201  * Encode the replay sequence operation from the slot values.
3202  * If cachethis is FALSE encode the uncached rep error on the next
3203  * operation which sets resp->p and increments resp->opcnt for
3204  * nfs4svc_encode_compoundres.
3205  *
3206  */
3207 static __be32
3208 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
3209                           struct nfsd4_compoundres *resp)
3210 {
3211         struct nfsd4_op *op;
3212         struct nfsd4_slot *slot = resp->cstate.slot;
3213
3214         /* Encode the replayed sequence operation */
3215         op = &args->ops[resp->opcnt - 1];
3216         nfsd4_encode_operation(resp, op);
3217
3218         if (slot->sl_flags & NFSD4_SLOT_CACHED)
3219                 return op->status;
3220         if (args->opcnt == 1) {
3221                 /*
3222                  * The original operation wasn't a solo sequence--we
3223                  * always cache those--so this retry must not match the
3224                  * original:
3225                  */
3226                 op->status = nfserr_seq_false_retry;
3227         } else {
3228                 op = &args->ops[resp->opcnt++];
3229                 op->status = nfserr_retry_uncached_rep;
3230                 nfsd4_encode_operation(resp, op);
3231         }
3232         return op->status;
3233 }
3234
3235 /*
3236  * The sequence operation is not cached because we can use the slot and
3237  * session values.
3238  */
3239 static __be32
3240 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
3241                          struct nfsd4_sequence *seq)
3242 {
3243         struct nfsd4_slot *slot = resp->cstate.slot;
3244         struct xdr_stream *xdr = resp->xdr;
3245         __be32 *p;
3246         __be32 status;
3247
3248         dprintk("--> %s slot %p\n", __func__, slot);
3249
3250         status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
3251         if (status)
3252                 return status;
3253
3254         p = xdr_reserve_space(xdr, slot->sl_datalen);
3255         if (!p) {
3256                 WARN_ON_ONCE(1);
3257                 return nfserr_serverfault;
3258         }
3259         xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
3260         xdr_commit_encode(xdr);
3261
3262         resp->opcnt = slot->sl_opcnt;
3263         return slot->sl_status;
3264 }
3265
3266 /*
3267  * Set the exchange_id flags returned by the server.
3268  */
3269 static void
3270 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
3271 {
3272 #ifdef CONFIG_NFSD_PNFS
3273         new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
3274 #else
3275         new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
3276 #endif
3277
3278         /* Referrals are supported, Migration is not. */
3279         new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
3280
3281         /* set the wire flags to return to client. */
3282         clid->flags = new->cl_exchange_flags;
3283 }
3284
3285 static bool client_has_openowners(struct nfs4_client *clp)
3286 {
3287         struct nfs4_openowner *oo;
3288
3289         list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
3290                 if (!list_empty(&oo->oo_owner.so_stateids))
3291                         return true;
3292         }
3293         return false;
3294 }
3295
3296 static bool client_has_state(struct nfs4_client *clp)
3297 {
3298         return client_has_openowners(clp)
3299 #ifdef CONFIG_NFSD_PNFS
3300                 || !list_empty(&clp->cl_lo_states)
3301 #endif
3302                 || !list_empty(&clp->cl_delegations)
3303                 || !list_empty(&clp->cl_sessions)
3304                 || !list_empty(&clp->async_copies);
3305 }
3306
3307 static __be32 copy_impl_id(struct nfs4_client *clp,
3308                                 struct nfsd4_exchange_id *exid)
3309 {
3310         if (!exid->nii_domain.data)
3311                 return 0;
3312         xdr_netobj_dup(&clp->cl_nii_domain, &exid->nii_domain, GFP_KERNEL);
3313         if (!clp->cl_nii_domain.data)
3314                 return nfserr_jukebox;
3315         xdr_netobj_dup(&clp->cl_nii_name, &exid->nii_name, GFP_KERNEL);
3316         if (!clp->cl_nii_name.data)
3317                 return nfserr_jukebox;
3318         clp->cl_nii_time = exid->nii_time;
3319         return 0;
3320 }
3321
3322 __be32
3323 nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3324                 union nfsd4_op_u *u)
3325 {
3326         struct nfsd4_exchange_id *exid = &u->exchange_id;
3327         struct nfs4_client *conf, *new;
3328         struct nfs4_client *unconf = NULL;
3329         __be32 status;
3330         char                    addr_str[INET6_ADDRSTRLEN];
3331         nfs4_verifier           verf = exid->verifier;
3332         struct sockaddr         *sa = svc_addr(rqstp);
3333         bool    update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
3334         struct nfsd_net         *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3335
3336         rpc_ntop(sa, addr_str, sizeof(addr_str));
3337         dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
3338                 "ip_addr=%s flags %x, spa_how %u\n",
3339                 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
3340                 addr_str, exid->flags, exid->spa_how);
3341
3342         if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
3343                 return nfserr_inval;
3344
3345         new = create_client(exid->clname, rqstp, &verf);
3346         if (new == NULL)
3347                 return nfserr_jukebox;
3348         status = copy_impl_id(new, exid);
3349         if (status)
3350                 goto out_nolock;
3351
3352         switch (exid->spa_how) {
3353         case SP4_MACH_CRED:
3354                 exid->spo_must_enforce[0] = 0;
3355                 exid->spo_must_enforce[1] = (
3356                         1 << (OP_BIND_CONN_TO_SESSION - 32) |
3357                         1 << (OP_EXCHANGE_ID - 32) |
3358                         1 << (OP_CREATE_SESSION - 32) |
3359                         1 << (OP_DESTROY_SESSION - 32) |
3360                         1 << (OP_DESTROY_CLIENTID - 32));
3361
3362                 exid->spo_must_allow[0] &= (1 << (OP_CLOSE) |
3363                                         1 << (OP_OPEN_DOWNGRADE) |
3364                                         1 << (OP_LOCKU) |
3365                                         1 << (OP_DELEGRETURN));
3366
3367                 exid->spo_must_allow[1] &= (
3368                                         1 << (OP_TEST_STATEID - 32) |
3369                                         1 << (OP_FREE_STATEID - 32));
3370                 if (!svc_rqst_integrity_protected(rqstp)) {
3371                         status = nfserr_inval;
3372                         goto out_nolock;
3373                 }
3374                 /*
3375                  * Sometimes userspace doesn't give us a principal.
3376                  * Which is a bug, really.  Anyway, we can't enforce
3377                  * MACH_CRED in that case, better to give up now:
3378                  */
3379                 if (!new->cl_cred.cr_principal &&
3380                                         !new->cl_cred.cr_raw_principal) {
3381                         status = nfserr_serverfault;
3382                         goto out_nolock;
3383                 }
3384                 new->cl_mach_cred = true;
3385                 break;
3386         case SP4_NONE:
3387                 break;
3388         default:                                /* checked by xdr code */
3389                 WARN_ON_ONCE(1);
3390                 fallthrough;
3391         case SP4_SSV:
3392                 status = nfserr_encr_alg_unsupp;
3393                 goto out_nolock;
3394         }
3395
3396         /* Cases below refer to rfc 5661 section 18.35.4: */
3397         spin_lock(&nn->client_lock);
3398         conf = find_confirmed_client_by_name(&exid->clname, nn);
3399         if (conf) {
3400                 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
3401                 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
3402
3403                 if (update) {
3404                         if (!clp_used_exchangeid(conf)) { /* buggy client */
3405                                 status = nfserr_inval;
3406                                 goto out;
3407                         }
3408                         if (!nfsd4_mach_creds_match(conf, rqstp)) {
3409                                 status = nfserr_wrong_cred;
3410                                 goto out;
3411                         }
3412                         if (!creds_match) { /* case 9 */
3413                                 status = nfserr_perm;
3414                                 goto out;
3415                         }
3416                         if (!verfs_match) { /* case 8 */
3417                                 status = nfserr_not_same;
3418                                 goto out;
3419                         }
3420                         /* case 6 */
3421                         exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
3422                         trace_nfsd_clid_confirmed_r(conf);
3423                         goto out_copy;
3424                 }
3425                 if (!creds_match) { /* case 3 */
3426                         if (client_has_state(conf)) {
3427                                 status = nfserr_clid_inuse;
3428                                 trace_nfsd_clid_cred_mismatch(conf, rqstp);
3429                                 goto out;
3430                         }
3431                         goto out_new;
3432                 }
3433                 if (verfs_match) { /* case 2 */
3434                         conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
3435                         trace_nfsd_clid_confirmed_r(conf);
3436                         goto out_copy;
3437                 }
3438                 /* case 5, client reboot */
3439                 trace_nfsd_clid_verf_mismatch(conf, rqstp, &verf);
3440                 conf = NULL;
3441                 goto out_new;
3442         }
3443
3444         if (update) { /* case 7 */
3445                 status = nfserr_noent;
3446                 goto out;
3447         }
3448
3449         unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
3450         if (unconf) /* case 4, possible retry or client restart */
3451                 unhash_client_locked(unconf);
3452
3453         /* case 1, new owner ID */
3454         trace_nfsd_clid_fresh(new);
3455
3456 out_new:
3457         if (conf) {
3458                 status = mark_client_expired_locked(conf);
3459                 if (status)
3460                         goto out;
3461                 trace_nfsd_clid_replaced(&conf->cl_clientid);
3462         }
3463         new->cl_minorversion = cstate->minorversion;
3464         new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0];
3465         new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1];
3466
3467         add_to_unconfirmed(new);
3468         swap(new, conf);
3469 out_copy:
3470         exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
3471         exid->clientid.cl_id = conf->cl_clientid.cl_id;
3472
3473         exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
3474         nfsd4_set_ex_flags(conf, exid);
3475
3476         dprintk("nfsd4_exchange_id seqid %d flags %x\n",
3477                 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
3478         status = nfs_ok;
3479
3480 out:
3481         spin_unlock(&nn->client_lock);
3482 out_nolock:
3483         if (new)
3484                 expire_client(new);
3485         if (unconf) {
3486                 trace_nfsd_clid_expire_unconf(&unconf->cl_clientid);
3487                 expire_client(unconf);
3488         }
3489         return status;
3490 }
3491
3492 static __be32
3493 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
3494 {
3495         dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
3496                 slot_seqid);
3497
3498         /* The slot is in use, and no response has been sent. */
3499         if (slot_inuse) {
3500                 if (seqid == slot_seqid)
3501                         return nfserr_jukebox;
3502                 else
3503                         return nfserr_seq_misordered;
3504         }
3505         /* Note unsigned 32-bit arithmetic handles wraparound: */
3506         if (likely(seqid == slot_seqid + 1))
3507                 return nfs_ok;
3508         if (seqid == slot_seqid)
3509                 return nfserr_replay_cache;
3510         return nfserr_seq_misordered;
3511 }
3512
3513 /*
3514  * Cache the create session result into the create session single DRC
3515  * slot cache by saving the xdr structure. sl_seqid has been set.
3516  * Do this for solo or embedded create session operations.
3517  */
3518 static void
3519 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
3520                            struct nfsd4_clid_slot *slot, __be32 nfserr)
3521 {
3522         slot->sl_status = nfserr;
3523         memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
3524 }
3525
3526 static __be32
3527 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
3528                             struct nfsd4_clid_slot *slot)
3529 {
3530         memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
3531         return slot->sl_status;
3532 }
3533
3534 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
3535                         2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
3536                         1 +     /* MIN tag is length with zero, only length */ \
3537                         3 +     /* version, opcount, opcode */ \
3538                         XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3539                                 /* seqid, slotID, slotID, cache */ \
3540                         4 ) * sizeof(__be32))
3541
3542 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
3543                         2 +     /* verifier: AUTH_NULL, length 0 */\
3544                         1 +     /* status */ \
3545                         1 +     /* MIN tag is length with zero, only length */ \
3546                         3 +     /* opcount, opcode, opstatus*/ \
3547                         XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3548                                 /* seqid, slotID, slotID, slotID, status */ \
3549                         5 ) * sizeof(__be32))
3550
3551 static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
3552 {
3553         u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
3554
3555         if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
3556                 return nfserr_toosmall;
3557         if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
3558                 return nfserr_toosmall;
3559         ca->headerpadsz = 0;
3560         ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
3561         ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
3562         ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
3563         ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
3564                         NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
3565         ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
3566         /*
3567          * Note decreasing slot size below client's request may make it
3568          * difficult for client to function correctly, whereas
3569          * decreasing the number of slots will (just?) affect
3570          * performance.  When short on memory we therefore prefer to
3571          * decrease number of slots instead of their size.  Clients that
3572          * request larger slots than they need will get poor results:
3573          * Note that we always allow at least one slot, because our
3574          * accounting is soft and provides no guarantees either way.
3575          */
3576         ca->maxreqs = nfsd4_get_drc_mem(ca, nn);
3577
3578         return nfs_ok;
3579 }
3580
3581 /*
3582  * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
3583  * These are based on similar macros in linux/sunrpc/msg_prot.h .
3584  */
3585 #define RPC_MAX_HEADER_WITH_AUTH_SYS \
3586         (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
3587
3588 #define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
3589         (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
3590
3591 #define NFSD_CB_MAX_REQ_SZ      ((NFS4_enc_cb_recall_sz + \
3592                                  RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
3593 #define NFSD_CB_MAX_RESP_SZ     ((NFS4_dec_cb_recall_sz + \
3594                                  RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
3595                                  sizeof(__be32))
3596
3597 static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
3598 {
3599         ca->headerpadsz = 0;
3600
3601         if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
3602                 return nfserr_toosmall;
3603         if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
3604                 return nfserr_toosmall;
3605         ca->maxresp_cached = 0;
3606         if (ca->maxops < 2)
3607                 return nfserr_toosmall;
3608
3609         return nfs_ok;
3610 }
3611
3612 static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
3613 {
3614         switch (cbs->flavor) {
3615         case RPC_AUTH_NULL:
3616         case RPC_AUTH_UNIX:
3617                 return nfs_ok;
3618         default:
3619                 /*
3620                  * GSS case: the spec doesn't allow us to return this
3621                  * error.  But it also doesn't allow us not to support
3622                  * GSS.
3623                  * I'd rather this fail hard than return some error the
3624                  * client might think it can already handle:
3625                  */
3626                 return nfserr_encr_alg_unsupp;
3627         }
3628 }
3629
3630 __be32
3631 nfsd4_create_session(struct svc_rqst *rqstp,
3632                 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
3633 {
3634         struct nfsd4_create_session *cr_ses = &u->create_session;
3635         struct sockaddr *sa = svc_addr(rqstp);
3636         struct nfs4_client *conf, *unconf;
3637         struct nfs4_client *old = NULL;
3638         struct nfsd4_session *new;
3639         struct nfsd4_conn *conn;
3640         struct nfsd4_clid_slot *cs_slot = NULL;
3641         __be32 status = 0;
3642         struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3643
3644         if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
3645                 return nfserr_inval;
3646         status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
3647         if (status)
3648                 return status;
3649         status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
3650         if (status)
3651                 return status;
3652         status = check_backchannel_attrs(&cr_ses->back_channel);
3653         if (status)
3654                 goto out_release_drc_mem;
3655         status = nfserr_jukebox;
3656         new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
3657         if (!new)
3658                 goto out_release_drc_mem;
3659         conn = alloc_conn_from_crses(rqstp, cr_ses);
3660         if (!conn)
3661                 goto out_free_session;
3662
3663         spin_lock(&nn->client_lock);
3664         unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
3665         conf = find_confirmed_client(&cr_ses->clientid, true, nn);
3666         WARN_ON_ONCE(conf && unconf);
3667
3668         if (conf) {
3669                 status = nfserr_wrong_cred;
3670                 if (!nfsd4_mach_creds_match(conf, rqstp))
3671                         goto out_free_conn;
3672                 cs_slot = &conf->cl_cs_slot;
3673                 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
3674                 if (status) {
3675                         if (status == nfserr_replay_cache)
3676                                 status = nfsd4_replay_create_session(cr_ses, cs_slot);
3677                         goto out_free_conn;
3678                 }
3679         } else if (unconf) {
3680                 status = nfserr_clid_inuse;
3681                 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
3682                     !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
3683                         trace_nfsd_clid_cred_mismatch(unconf, rqstp);
3684                         goto out_free_conn;
3685                 }
3686                 status = nfserr_wrong_cred;
3687                 if (!nfsd4_mach_creds_match(unconf, rqstp))
3688                         goto out_free_conn;
3689                 cs_slot = &unconf->cl_cs_slot;
3690                 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
3691                 if (status) {
3692                         /* an unconfirmed replay returns misordered */
3693                         status = nfserr_seq_misordered;
3694                         goto out_free_conn;
3695                 }
3696                 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3697                 if (old) {
3698                         status = mark_client_expired_locked(old);
3699                         if (status) {
3700                                 old = NULL;
3701                                 goto out_free_conn;
3702                         }
3703                         trace_nfsd_clid_replaced(&old->cl_clientid);
3704                 }
3705                 move_to_confirmed(unconf);
3706                 conf = unconf;
3707         } else {
3708                 status = nfserr_stale_clientid;
3709                 goto out_free_conn;
3710         }
3711         status = nfs_ok;
3712         /* Persistent sessions are not supported */
3713         cr_ses->flags &= ~SESSION4_PERSIST;
3714         /* Upshifting from TCP to RDMA is not supported */
3715         cr_ses->flags &= ~SESSION4_RDMA;
3716
3717         init_session(rqstp, new, conf, cr_ses);
3718         nfsd4_get_session_locked(new);
3719
3720         memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
3721                NFS4_MAX_SESSIONID_LEN);
3722         cs_slot->sl_seqid++;
3723         cr_ses->seqid = cs_slot->sl_seqid;
3724
3725         /* cache solo and embedded create sessions under the client_lock */
3726         nfsd4_cache_create_session(cr_ses, cs_slot, status);
3727         spin_unlock(&nn->client_lock);
3728         if (conf == unconf)
3729                 fsnotify_dentry(conf->cl_nfsd_info_dentry, FS_MODIFY);
3730         /* init connection and backchannel */
3731         nfsd4_init_conn(rqstp, conn, new);
3732         nfsd4_put_session(new);
3733         if (old)
3734                 expire_client(old);
3735         return status;
3736 out_free_conn:
3737         spin_unlock(&nn->client_lock);
3738         free_conn(conn);
3739         if (old)
3740                 expire_client(old);
3741 out_free_session:
3742         __free_session(new);
3743 out_release_drc_mem:
3744         nfsd4_put_drc_mem(&cr_ses->fore_channel);
3745         return status;
3746 }
3747
3748 static __be32 nfsd4_map_bcts_dir(u32 *dir)
3749 {
3750         switch (*dir) {
3751         case NFS4_CDFC4_FORE:
3752         case NFS4_CDFC4_BACK:
3753                 return nfs_ok;
3754         case NFS4_CDFC4_FORE_OR_BOTH:
3755         case NFS4_CDFC4_BACK_OR_BOTH:
3756                 *dir = NFS4_CDFC4_BOTH;
3757                 return nfs_ok;
3758         }
3759         return nfserr_inval;
3760 }
3761
3762 __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp,
3763                 struct nfsd4_compound_state *cstate,
3764                 union nfsd4_op_u *u)
3765 {
3766         struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl;
3767         struct nfsd4_session *session = cstate->session;
3768         struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3769         __be32 status;
3770
3771         status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
3772         if (status)
3773                 return status;
3774         spin_lock(&nn->client_lock);
3775         session->se_cb_prog = bc->bc_cb_program;
3776         session->se_cb_sec = bc->bc_cb_sec;
3777         spin_unlock(&nn->client_lock);
3778
3779         nfsd4_probe_callback(session->se_client);
3780
3781         return nfs_ok;
3782 }
3783
3784 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
3785 {
3786         struct nfsd4_conn *c;
3787
3788         list_for_each_entry(c, &s->se_conns, cn_persession) {
3789                 if (c->cn_xprt == xpt) {
3790                         return c;
3791                 }
3792         }
3793         return NULL;
3794 }
3795
3796 static __be32 nfsd4_match_existing_connection(struct svc_rqst *rqst,
3797                 struct nfsd4_session *session, u32 req, struct nfsd4_conn **conn)
3798 {
3799         struct nfs4_client *clp = session->se_client;
3800         struct svc_xprt *xpt = rqst->rq_xprt;
3801         struct nfsd4_conn *c;
3802         __be32 status;
3803
3804         /* Following the last paragraph of RFC 5661 Section 18.34.3: */
3805         spin_lock(&clp->cl_lock);
3806         c = __nfsd4_find_conn(xpt, session);
3807         if (!c)
3808                 status = nfserr_noent;
3809         else if (req == c->cn_flags)
3810                 status = nfs_ok;
3811         else if (req == NFS4_CDFC4_FORE_OR_BOTH &&
3812                                 c->cn_flags != NFS4_CDFC4_BACK)
3813                 status = nfs_ok;
3814         else if (req == NFS4_CDFC4_BACK_OR_BOTH &&
3815                                 c->cn_flags != NFS4_CDFC4_FORE)
3816                 status = nfs_ok;
3817         else
3818                 status = nfserr_inval;
3819         spin_unlock(&clp->cl_lock);
3820         if (status == nfs_ok && conn)
3821                 *conn = c;
3822         return status;
3823 }
3824
3825 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
3826                      struct nfsd4_compound_state *cstate,
3827                      union nfsd4_op_u *u)
3828 {
3829         struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session;
3830         __be32 status;
3831         struct nfsd4_conn *conn;
3832         struct nfsd4_session *session;
3833         struct net *net = SVC_NET(rqstp);
3834         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3835
3836         if (!nfsd4_last_compound_op(rqstp))
3837                 return nfserr_not_only_op;
3838         spin_lock(&nn->client_lock);
3839         session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
3840         spin_unlock(&nn->client_lock);
3841         if (!session)
3842                 goto out_no_session;
3843         status = nfserr_wrong_cred;
3844         if (!nfsd4_mach_creds_match(session->se_client, rqstp))
3845                 goto out;
3846         status = nfsd4_match_existing_connection(rqstp, session,
3847                         bcts->dir, &conn);
3848         if (status == nfs_ok) {
3849                 if (bcts->dir == NFS4_CDFC4_FORE_OR_BOTH ||
3850                                 bcts->dir == NFS4_CDFC4_BACK)
3851                         conn->cn_flags |= NFS4_CDFC4_BACK;
3852                 nfsd4_probe_callback(session->se_client);
3853                 goto out;
3854         }
3855         if (status == nfserr_inval)
3856                 goto out;
3857         status = nfsd4_map_bcts_dir(&bcts->dir);
3858         if (status)
3859                 goto out;
3860         conn = alloc_conn(rqstp, bcts->dir);
3861         status = nfserr_jukebox;
3862         if (!conn)
3863                 goto out;
3864         nfsd4_init_conn(rqstp, conn, session);
3865         status = nfs_ok;
3866 out:
3867         nfsd4_put_session(session);
3868 out_no_session:
3869         return status;
3870 }
3871
3872 static bool nfsd4_compound_in_session(struct nfsd4_compound_state *cstate, struct nfs4_sessionid *sid)
3873 {
3874         if (!cstate->session)
3875                 return false;
3876         return !memcmp(sid, &cstate->session->se_sessionid, sizeof(*sid));
3877 }
3878
3879 __be32
3880 nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate,
3881                 union nfsd4_op_u *u)
3882 {
3883         struct nfs4_sessionid *sessionid = &u->destroy_session.sessionid;
3884         struct nfsd4_session *ses;
3885         __be32 status;
3886         int ref_held_by_me = 0;
3887         struct net *net = SVC_NET(r);
3888         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3889
3890         status = nfserr_not_only_op;
3891         if (nfsd4_compound_in_session(cstate, sessionid)) {
3892                 if (!nfsd4_last_compound_op(r))
3893                         goto out;
3894                 ref_held_by_me++;
3895         }
3896         dump_sessionid(__func__, sessionid);
3897         spin_lock(&nn->client_lock);
3898         ses = find_in_sessionid_hashtbl(sessionid, net, &status);
3899         if (!ses)
3900                 goto out_client_lock;
3901         status = nfserr_wrong_cred;
3902         if (!nfsd4_mach_creds_match(ses->se_client, r))
3903                 goto out_put_session;
3904         status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
3905         if (status)
3906                 goto out_put_session;
3907         unhash_session(ses);
3908         spin_unlock(&nn->client_lock);
3909
3910         nfsd4_probe_callback_sync(ses->se_client);
3911
3912         spin_lock(&nn->client_lock);
3913         status = nfs_ok;
3914 out_put_session:
3915         nfsd4_put_session_locked(ses);
3916 out_client_lock:
3917         spin_unlock(&nn->client_lock);
3918 out:
3919         return status;
3920 }
3921
3922 static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
3923 {
3924         struct nfs4_client *clp = ses->se_client;
3925         struct nfsd4_conn *c;
3926         __be32 status = nfs_ok;
3927         int ret;
3928
3929         spin_lock(&clp->cl_lock);
3930         c = __nfsd4_find_conn(new->cn_xprt, ses);
3931         if (c)
3932                 goto out_free;
3933         status = nfserr_conn_not_bound_to_session;
3934         if (clp->cl_mach_cred)
3935                 goto out_free;
3936         __nfsd4_hash_conn(new, ses);
3937         spin_unlock(&clp->cl_lock);
3938         ret = nfsd4_register_conn(new);
3939         if (ret)
3940                 /* oops; xprt is already down: */
3941                 nfsd4_conn_lost(&new->cn_xpt_user);
3942         return nfs_ok;
3943 out_free:
3944         spin_unlock(&clp->cl_lock);
3945         free_conn(new);
3946         return status;
3947 }
3948
3949 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
3950 {
3951         struct nfsd4_compoundargs *args = rqstp->rq_argp;
3952
3953         return args->opcnt > session->se_fchannel.maxops;
3954 }
3955
3956 static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
3957                                   struct nfsd4_session *session)
3958 {
3959         struct xdr_buf *xb = &rqstp->rq_arg;
3960
3961         return xb->len > session->se_fchannel.maxreq_sz;
3962 }
3963
3964 static bool replay_matches_cache(struct svc_rqst *rqstp,
3965                  struct nfsd4_sequence *seq, struct nfsd4_slot *slot)
3966 {
3967         struct nfsd4_compoundargs *argp = rqstp->rq_argp;
3968
3969         if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) !=
3970             (bool)seq->cachethis)
3971                 return false;
3972         /*
3973          * If there's an error then the reply can have fewer ops than
3974          * the call.
3975          */
3976         if (slot->sl_opcnt < argp->opcnt && !slot->sl_status)
3977                 return false;
3978         /*
3979          * But if we cached a reply with *more* ops than the call you're
3980          * sending us now, then this new call is clearly not really a
3981          * replay of the old one:
3982          */
3983         if (slot->sl_opcnt > argp->opcnt)
3984                 return false;
3985         /* This is the only check explicitly called by spec: */
3986         if (!same_creds(&rqstp->rq_cred, &slot->sl_cred))
3987                 return false;
3988         /*
3989          * There may be more comparisons we could actually do, but the
3990          * spec doesn't require us to catch every case where the calls
3991          * don't match (that would require caching the call as well as
3992          * the reply), so we don't bother.
3993          */
3994         return true;
3995 }
3996
3997 __be32
3998 nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3999                 union nfsd4_op_u *u)
4000 {
4001         struct nfsd4_sequence *seq = &u->sequence;
4002         struct nfsd4_compoundres *resp = rqstp->rq_resp;
4003         struct xdr_stream *xdr = resp->xdr;
4004         struct nfsd4_session *session;
4005         struct nfs4_client *clp;
4006         struct nfsd4_slot *slot;
4007         struct nfsd4_conn *conn;
4008         __be32 status;
4009         int buflen;
4010         struct net *net = SVC_NET(rqstp);
4011         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4012
4013         if (resp->opcnt != 1)
4014                 return nfserr_sequence_pos;
4015
4016         /*
4017          * Will be either used or freed by nfsd4_sequence_check_conn
4018          * below.
4019          */
4020         conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
4021         if (!conn)
4022                 return nfserr_jukebox;
4023
4024         spin_lock(&nn->client_lock);
4025         session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
4026         if (!session)
4027                 goto out_no_session;
4028         clp = session->se_client;
4029
4030         status = nfserr_too_many_ops;
4031         if (nfsd4_session_too_many_ops(rqstp, session))
4032                 goto out_put_session;
4033
4034         status = nfserr_req_too_big;
4035         if (nfsd4_request_too_big(rqstp, session))
4036                 goto out_put_session;
4037
4038         status = nfserr_badslot;
4039         if (seq->slotid >= session->se_fchannel.maxreqs)
4040                 goto out_put_session;
4041
4042         slot = session->se_slots[seq->slotid];
4043         dprintk("%s: slotid %d\n", __func__, seq->slotid);
4044
4045         /* We do not negotiate the number of slots yet, so set the
4046          * maxslots to the session maxreqs which is used to encode
4047          * sr_highest_slotid and the sr_target_slot id to maxslots */
4048         seq->maxslots = session->se_fchannel.maxreqs;
4049
4050         status = check_slot_seqid(seq->seqid, slot->sl_seqid,
4051                                         slot->sl_flags & NFSD4_SLOT_INUSE);
4052         if (status == nfserr_replay_cache) {
4053                 status = nfserr_seq_misordered;
4054                 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
4055                         goto out_put_session;
4056                 status = nfserr_seq_false_retry;
4057                 if (!replay_matches_cache(rqstp, seq, slot))
4058                         goto out_put_session;
4059                 cstate->slot = slot;
4060                 cstate->session = session;
4061                 cstate->clp = clp;
4062                 /* Return the cached reply status and set cstate->status
4063                  * for nfsd4_proc_compound processing */
4064                 status = nfsd4_replay_cache_entry(resp, seq);
4065                 cstate->status = nfserr_replay_cache;
4066                 goto out;
4067         }
4068         if (status)
4069                 goto out_put_session;
4070
4071         status = nfsd4_sequence_check_conn(conn, session);
4072         conn = NULL;
4073         if (status)
4074                 goto out_put_session;
4075
4076         buflen = (seq->cachethis) ?
4077                         session->se_fchannel.maxresp_cached :
4078                         session->se_fchannel.maxresp_sz;
4079         status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
4080                                     nfserr_rep_too_big;
4081         if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
4082                 goto out_put_session;
4083         svc_reserve(rqstp, buflen);
4084
4085         status = nfs_ok;
4086         /* Success! bump slot seqid */
4087         slot->sl_seqid = seq->seqid;
4088         slot->sl_flags |= NFSD4_SLOT_INUSE;
4089         if (seq->cachethis)
4090                 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
4091         else
4092                 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
4093
4094         cstate->slot = slot;
4095         cstate->session = session;
4096         cstate->clp = clp;
4097
4098 out:
4099         switch (clp->cl_cb_state) {
4100         case NFSD4_CB_DOWN:
4101                 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
4102                 break;
4103         case NFSD4_CB_FAULT:
4104                 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
4105                 break;
4106         default:
4107                 seq->status_flags = 0;
4108         }
4109         if (!list_empty(&clp->cl_revoked))
4110                 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
4111 out_no_session:
4112         if (conn)
4113                 free_conn(conn);
4114         spin_unlock(&nn->client_lock);
4115         return status;
4116 out_put_session:
4117         nfsd4_put_session_locked(session);
4118         goto out_no_session;
4119 }
4120
4121 void
4122 nfsd4_sequence_done(struct nfsd4_compoundres *resp)
4123 {
4124         struct nfsd4_compound_state *cs = &resp->cstate;
4125
4126         if (nfsd4_has_session(cs)) {
4127                 if (cs->status != nfserr_replay_cache) {
4128                         nfsd4_store_cache_entry(resp);
4129                         cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
4130                 }
4131                 /* Drop session reference that was taken in nfsd4_sequence() */
4132                 nfsd4_put_session(cs->session);
4133         } else if (cs->clp)
4134                 put_client_renew(cs->clp);
4135 }
4136
4137 __be32
4138 nfsd4_destroy_clientid(struct svc_rqst *rqstp,
4139                 struct nfsd4_compound_state *cstate,
4140                 union nfsd4_op_u *u)
4141 {
4142         struct nfsd4_destroy_clientid *dc = &u->destroy_clientid;
4143         struct nfs4_client *conf, *unconf;
4144         struct nfs4_client *clp = NULL;
4145         __be32 status = 0;
4146         struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4147
4148         spin_lock(&nn->client_lock);
4149         unconf = find_unconfirmed_client(&dc->clientid, true, nn);
4150         conf = find_confirmed_client(&dc->clientid, true, nn);
4151         WARN_ON_ONCE(conf && unconf);
4152
4153         if (conf) {
4154                 if (client_has_state(conf)) {
4155                         status = nfserr_clientid_busy;
4156                         goto out;
4157                 }
4158                 status = mark_client_expired_locked(conf);
4159                 if (status)
4160                         goto out;
4161                 clp = conf;
4162         } else if (unconf)
4163                 clp = unconf;
4164         else {
4165                 status = nfserr_stale_clientid;
4166                 goto out;
4167         }
4168         if (!nfsd4_mach_creds_match(clp, rqstp)) {
4169                 clp = NULL;
4170                 status = nfserr_wrong_cred;
4171                 goto out;
4172         }
4173         trace_nfsd_clid_destroyed(&clp->cl_clientid);
4174         unhash_client_locked(clp);
4175 out:
4176         spin_unlock(&nn->client_lock);
4177         if (clp)
4178                 expire_client(clp);
4179         return status;
4180 }
4181
4182 __be32
4183 nfsd4_reclaim_complete(struct svc_rqst *rqstp,
4184                 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
4185 {
4186         struct nfsd4_reclaim_complete *rc = &u->reclaim_complete;
4187         struct nfs4_client *clp = cstate->clp;
4188         __be32 status = 0;
4189
4190         if (rc->rca_one_fs) {
4191                 if (!cstate->current_fh.fh_dentry)
4192                         return nfserr_nofilehandle;
4193                 /*
4194                  * We don't take advantage of the rca_one_fs case.
4195                  * That's OK, it's optional, we can safely ignore it.
4196                  */
4197                 return nfs_ok;
4198         }
4199
4200         status = nfserr_complete_already;
4201         if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &clp->cl_flags))
4202                 goto out;
4203
4204         status = nfserr_stale_clientid;
4205         if (is_client_expired(clp))
4206                 /*
4207                  * The following error isn't really legal.
4208                  * But we only get here if the client just explicitly
4209                  * destroyed the client.  Surely it no longer cares what
4210                  * error it gets back on an operation for the dead
4211                  * client.
4212                  */
4213                 goto out;
4214
4215         status = nfs_ok;
4216         trace_nfsd_clid_reclaim_complete(&clp->cl_clientid);
4217         nfsd4_client_record_create(clp);
4218         inc_reclaim_complete(clp);
4219 out:
4220         return status;
4221 }
4222
4223 __be32
4224 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4225                   union nfsd4_op_u *u)
4226 {
4227         struct nfsd4_setclientid *setclid = &u->setclientid;
4228         struct xdr_netobj       clname = setclid->se_name;
4229         nfs4_verifier           clverifier = setclid->se_verf;
4230         struct nfs4_client      *conf, *new;
4231         struct nfs4_client      *unconf = NULL;
4232         __be32                  status;
4233         struct nfsd_net         *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4234
4235         new = create_client(clname, rqstp, &clverifier);
4236         if (new == NULL)
4237                 return nfserr_jukebox;
4238         spin_lock(&nn->client_lock);
4239         conf = find_confirmed_client_by_name(&clname, nn);
4240         if (conf && client_has_state(conf)) {
4241                 status = nfserr_clid_inuse;
4242                 if (clp_used_exchangeid(conf))
4243                         goto out;
4244                 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
4245                         trace_nfsd_clid_cred_mismatch(conf, rqstp);
4246                         goto out;
4247                 }
4248         }
4249         unconf = find_unconfirmed_client_by_name(&clname, nn);
4250         if (unconf)
4251                 unhash_client_locked(unconf);
4252         if (conf) {
4253                 if (same_verf(&conf->cl_verifier, &clverifier)) {
4254                         copy_clid(new, conf);
4255                         gen_confirm(new, nn);
4256                 } else
4257                         trace_nfsd_clid_verf_mismatch(conf, rqstp,
4258                                                       &clverifier);
4259         } else
4260                 trace_nfsd_clid_fresh(new);
4261         new->cl_minorversion = 0;
4262         gen_callback(new, setclid, rqstp);
4263         add_to_unconfirmed(new);
4264         setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
4265         setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
4266         memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
4267         new = NULL;
4268         status = nfs_ok;
4269 out:
4270         spin_unlock(&nn->client_lock);
4271         if (new)
4272                 free_client(new);
4273         if (unconf) {
4274                 trace_nfsd_clid_expire_unconf(&unconf->cl_clientid);
4275                 expire_client(unconf);
4276         }
4277         return status;
4278 }
4279
4280 __be32
4281 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
4282                         struct nfsd4_compound_state *cstate,
4283                         union nfsd4_op_u *u)
4284 {
4285         struct nfsd4_setclientid_confirm *setclientid_confirm =
4286                         &u->setclientid_confirm;
4287         struct nfs4_client *conf, *unconf;
4288         struct nfs4_client *old = NULL;
4289         nfs4_verifier confirm = setclientid_confirm->sc_confirm; 
4290         clientid_t * clid = &setclientid_confirm->sc_clientid;
4291         __be32 status;
4292         struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4293
4294         if (STALE_CLIENTID(clid, nn))
4295                 return nfserr_stale_clientid;
4296
4297         spin_lock(&nn->client_lock);
4298         conf = find_confirmed_client(clid, false, nn);
4299         unconf = find_unconfirmed_client(clid, false, nn);
4300         /*
4301          * We try hard to give out unique clientid's, so if we get an
4302          * attempt to confirm the same clientid with a different cred,
4303          * the client may be buggy; this should never happen.
4304          *
4305          * Nevertheless, RFC 7530 recommends INUSE for this case:
4306          */
4307         status = nfserr_clid_inuse;
4308         if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
4309                 trace_nfsd_clid_cred_mismatch(unconf, rqstp);
4310                 goto out;
4311         }
4312         if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
4313                 trace_nfsd_clid_cred_mismatch(conf, rqstp);
4314                 goto out;
4315         }
4316         if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
4317                 if (conf && same_verf(&confirm, &conf->cl_confirm)) {
4318                         status = nfs_ok;
4319                 } else
4320                         status = nfserr_stale_clientid;
4321                 goto out;
4322         }
4323         status = nfs_ok;
4324         if (conf) {
4325                 old = unconf;
4326                 unhash_client_locked(old);
4327                 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
4328         } else {
4329                 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
4330                 if (old) {
4331                         status = nfserr_clid_inuse;
4332                         if (client_has_state(old)
4333                                         && !same_creds(&unconf->cl_cred,
4334                                                         &old->cl_cred)) {
4335                                 old = NULL;
4336                                 goto out;
4337                         }
4338                         status = mark_client_expired_locked(old);
4339                         if (status) {
4340                                 old = NULL;
4341                                 goto out;
4342                         }
4343                         trace_nfsd_clid_replaced(&old->cl_clientid);
4344                 }
4345                 move_to_confirmed(unconf);
4346                 conf = unconf;
4347         }
4348         get_client_locked(conf);
4349         spin_unlock(&nn->client_lock);
4350         if (conf == unconf)
4351                 fsnotify_dentry(conf->cl_nfsd_info_dentry, FS_MODIFY);
4352         nfsd4_probe_callback(conf);
4353         spin_lock(&nn->client_lock);
4354         put_client_renew_locked(conf);
4355 out:
4356         spin_unlock(&nn->client_lock);
4357         if (old)
4358                 expire_client(old);
4359         return status;
4360 }
4361
4362 static struct nfs4_file *nfsd4_alloc_file(void)
4363 {
4364         return kmem_cache_alloc(file_slab, GFP_KERNEL);
4365 }
4366
4367 /* OPEN Share state helper functions */
4368
4369 static void nfsd4_file_init(const struct svc_fh *fh, struct nfs4_file *fp)
4370 {
4371         refcount_set(&fp->fi_ref, 1);
4372         spin_lock_init(&fp->fi_lock);
4373         INIT_LIST_HEAD(&fp->fi_stateids);
4374         INIT_LIST_HEAD(&fp->fi_delegations);
4375         INIT_LIST_HEAD(&fp->fi_clnt_odstate);
4376         fh_copy_shallow(&fp->fi_fhandle, &fh->fh_handle);
4377         fp->fi_deleg_file = NULL;
4378         fp->fi_had_conflict = false;
4379         fp->fi_share_deny = 0;
4380         memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
4381         memset(fp->fi_access, 0, sizeof(fp->fi_access));
4382         fp->fi_aliased = false;
4383         fp->fi_inode = d_inode(fh->fh_dentry);
4384 #ifdef CONFIG_NFSD_PNFS
4385         INIT_LIST_HEAD(&fp->fi_lo_states);
4386         atomic_set(&fp->fi_lo_recalls, 0);
4387 #endif
4388 }
4389
4390 void
4391 nfsd4_free_slabs(void)
4392 {
4393         kmem_cache_destroy(client_slab);
4394         kmem_cache_destroy(openowner_slab);
4395         kmem_cache_destroy(lockowner_slab);
4396         kmem_cache_destroy(file_slab);
4397         kmem_cache_destroy(stateid_slab);
4398         kmem_cache_destroy(deleg_slab);
4399         kmem_cache_destroy(odstate_slab);
4400 }
4401
4402 int
4403 nfsd4_init_slabs(void)
4404 {
4405         client_slab = kmem_cache_create("nfsd4_clients",
4406                         sizeof(struct nfs4_client), 0, 0, NULL);
4407         if (client_slab == NULL)
4408                 goto out;
4409         openowner_slab = kmem_cache_create("nfsd4_openowners",
4410                         sizeof(struct nfs4_openowner), 0, 0, NULL);
4411         if (openowner_slab == NULL)
4412                 goto out_free_client_slab;
4413         lockowner_slab = kmem_cache_create("nfsd4_lockowners",
4414                         sizeof(struct nfs4_lockowner), 0, 0, NULL);
4415         if (lockowner_slab == NULL)
4416                 goto out_free_openowner_slab;
4417         file_slab = kmem_cache_create("nfsd4_files",
4418                         sizeof(struct nfs4_file), 0, 0, NULL);
4419         if (file_slab == NULL)
4420                 goto out_free_lockowner_slab;
4421         stateid_slab = kmem_cache_create("nfsd4_stateids",
4422                         sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
4423         if (stateid_slab == NULL)
4424                 goto out_free_file_slab;
4425         deleg_slab = kmem_cache_create("nfsd4_delegations",
4426                         sizeof(struct nfs4_delegation), 0, 0, NULL);
4427         if (deleg_slab == NULL)
4428                 goto out_free_stateid_slab;
4429         odstate_slab = kmem_cache_create("nfsd4_odstate",
4430                         sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
4431         if (odstate_slab == NULL)
4432                 goto out_free_deleg_slab;
4433         return 0;
4434
4435 out_free_deleg_slab:
4436         kmem_cache_destroy(deleg_slab);
4437 out_free_stateid_slab:
4438         kmem_cache_destroy(stateid_slab);
4439 out_free_file_slab:
4440         kmem_cache_destroy(file_slab);
4441 out_free_lockowner_slab:
4442         kmem_cache_destroy(lockowner_slab);
4443 out_free_openowner_slab:
4444         kmem_cache_destroy(openowner_slab);
4445 out_free_client_slab:
4446         kmem_cache_destroy(client_slab);
4447 out:
4448         return -ENOMEM;
4449 }
4450
4451 static unsigned long
4452 nfsd4_state_shrinker_count(struct shrinker *shrink, struct shrink_control *sc)
4453 {
4454         int count;
4455         struct nfsd_net *nn = shrink->private_data;
4456
4457         count = atomic_read(&nn->nfsd_courtesy_clients);
4458         if (!count)
4459                 count = atomic_long_read(&num_delegations);
4460         if (count)
4461                 queue_work(laundry_wq, &nn->nfsd_shrinker_work);
4462         return (unsigned long)count;
4463 }
4464
4465 static unsigned long
4466 nfsd4_state_shrinker_scan(struct shrinker *shrink, struct shrink_control *sc)
4467 {
4468         return SHRINK_STOP;
4469 }
4470
4471 void
4472 nfsd4_init_leases_net(struct nfsd_net *nn)
4473 {
4474         struct sysinfo si;
4475         u64 max_clients;
4476
4477         nn->nfsd4_lease = 90;   /* default lease time */
4478         nn->nfsd4_grace = 90;
4479         nn->somebody_reclaimed = false;
4480         nn->track_reclaim_completes = false;
4481         nn->clverifier_counter = get_random_u32();
4482         nn->clientid_base = get_random_u32();
4483         nn->clientid_counter = nn->clientid_base + 1;
4484         nn->s2s_cp_cl_id = nn->clientid_counter++;
4485
4486         atomic_set(&nn->nfs4_client_count, 0);
4487         si_meminfo(&si);
4488         max_clients = (u64)si.totalram * si.mem_unit / (1024 * 1024 * 1024);
4489         max_clients *= NFS4_CLIENTS_PER_GB;
4490         nn->nfs4_max_clients = max_t(int, max_clients, NFS4_CLIENTS_PER_GB);
4491
4492         atomic_set(&nn->nfsd_courtesy_clients, 0);
4493 }
4494
4495 static void init_nfs4_replay(struct nfs4_replay *rp)
4496 {
4497         rp->rp_status = nfserr_serverfault;
4498         rp->rp_buflen = 0;
4499         rp->rp_buf = rp->rp_ibuf;
4500         mutex_init(&rp->rp_mutex);
4501 }
4502
4503 static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
4504                 struct nfs4_stateowner *so)
4505 {
4506         if (!nfsd4_has_session(cstate)) {
4507                 mutex_lock(&so->so_replay.rp_mutex);
4508                 cstate->replay_owner = nfs4_get_stateowner(so);
4509         }
4510 }
4511
4512 void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
4513 {
4514         struct nfs4_stateowner *so = cstate->replay_owner;
4515
4516         if (so != NULL) {
4517                 cstate->replay_owner = NULL;
4518                 mutex_unlock(&so->so_replay.rp_mutex);
4519                 nfs4_put_stateowner(so);
4520         }
4521 }
4522
4523 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
4524 {
4525         struct nfs4_stateowner *sop;
4526
4527         sop = kmem_cache_alloc(slab, GFP_KERNEL);
4528         if (!sop)
4529                 return NULL;
4530
4531         xdr_netobj_dup(&sop->so_owner, owner, GFP_KERNEL);
4532         if (!sop->so_owner.data) {
4533                 kmem_cache_free(slab, sop);
4534                 return NULL;
4535         }
4536
4537         INIT_LIST_HEAD(&sop->so_stateids);
4538         sop->so_client = clp;
4539         init_nfs4_replay(&sop->so_replay);
4540         atomic_set(&sop->so_count, 1);
4541         return sop;
4542 }
4543
4544 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
4545 {
4546         lockdep_assert_held(&clp->cl_lock);
4547
4548         list_add(&oo->oo_owner.so_strhash,
4549                  &clp->cl_ownerstr_hashtbl[strhashval]);
4550         list_add(&oo->oo_perclient, &clp->cl_openowners);
4551 }
4552
4553 static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
4554 {
4555         unhash_openowner_locked(openowner(so));
4556 }
4557
4558 static void nfs4_free_openowner(struct nfs4_stateowner *so)
4559 {
4560         struct nfs4_openowner *oo = openowner(so);
4561
4562         kmem_cache_free(openowner_slab, oo);
4563 }
4564
4565 static const struct nfs4_stateowner_operations openowner_ops = {
4566         .so_unhash =    nfs4_unhash_openowner,
4567         .so_free =      nfs4_free_openowner,
4568 };
4569
4570 static struct nfs4_ol_stateid *
4571 nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4572 {
4573         struct nfs4_ol_stateid *local, *ret = NULL;
4574         struct nfs4_openowner *oo = open->op_openowner;
4575
4576         lockdep_assert_held(&fp->fi_lock);
4577
4578         list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
4579                 /* ignore lock owners */
4580                 if (local->st_stateowner->so_is_open_owner == 0)
4581                         continue;
4582                 if (local->st_stateowner != &oo->oo_owner)
4583                         continue;
4584                 if (local->st_stid.sc_type == NFS4_OPEN_STID) {
4585                         ret = local;
4586                         refcount_inc(&ret->st_stid.sc_count);
4587                         break;
4588                 }
4589         }
4590         return ret;
4591 }
4592
4593 static __be32
4594 nfsd4_verify_open_stid(struct nfs4_stid *s)
4595 {
4596         __be32 ret = nfs_ok;
4597
4598         switch (s->sc_type) {
4599         default:
4600                 break;
4601         case 0:
4602         case NFS4_CLOSED_STID:
4603         case NFS4_CLOSED_DELEG_STID:
4604                 ret = nfserr_bad_stateid;
4605                 break;
4606         case NFS4_REVOKED_DELEG_STID:
4607                 ret = nfserr_deleg_revoked;
4608         }
4609         return ret;
4610 }
4611
4612 /* Lock the stateid st_mutex, and deal with races with CLOSE */
4613 static __be32
4614 nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp)
4615 {
4616         __be32 ret;
4617
4618         mutex_lock_nested(&stp->st_mutex, LOCK_STATEID_MUTEX);
4619         ret = nfsd4_verify_open_stid(&stp->st_stid);
4620         if (ret != nfs_ok)
4621                 mutex_unlock(&stp->st_mutex);
4622         return ret;
4623 }
4624
4625 static struct nfs4_ol_stateid *
4626 nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4627 {
4628         struct nfs4_ol_stateid *stp;
4629         for (;;) {
4630                 spin_lock(&fp->fi_lock);
4631                 stp = nfsd4_find_existing_open(fp, open);
4632                 spin_unlock(&fp->fi_lock);
4633                 if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok)
4634                         break;
4635                 nfs4_put_stid(&stp->st_stid);
4636         }
4637         return stp;
4638 }
4639
4640 static struct nfs4_openowner *
4641 alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
4642                            struct nfsd4_compound_state *cstate)
4643 {
4644         struct nfs4_client *clp = cstate->clp;
4645         struct nfs4_openowner *oo, *ret;
4646
4647         oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
4648         if (!oo)
4649                 return NULL;
4650         oo->oo_owner.so_ops = &openowner_ops;
4651         oo->oo_owner.so_is_open_owner = 1;
4652         oo->oo_owner.so_seqid = open->op_seqid;
4653         oo->oo_flags = 0;
4654         if (nfsd4_has_session(cstate))
4655                 oo->oo_flags |= NFS4_OO_CONFIRMED;
4656         oo->oo_time = 0;
4657         oo->oo_last_closed_stid = NULL;
4658         INIT_LIST_HEAD(&oo->oo_close_lru);
4659         spin_lock(&clp->cl_lock);
4660         ret = find_openstateowner_str_locked(strhashval, open, clp);
4661         if (ret == NULL) {
4662                 hash_openowner(oo, clp, strhashval);
4663                 ret = oo;
4664         } else
4665                 nfs4_free_stateowner(&oo->oo_owner);
4666
4667         spin_unlock(&clp->cl_lock);
4668         return ret;
4669 }
4670
4671 static struct nfs4_ol_stateid *
4672 init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
4673 {
4674
4675         struct nfs4_openowner *oo = open->op_openowner;
4676         struct nfs4_ol_stateid *retstp = NULL;
4677         struct nfs4_ol_stateid *stp;
4678
4679         stp = open->op_stp;
4680         /* We are moving these outside of the spinlocks to avoid the warnings */
4681         mutex_init(&stp->st_mutex);
4682         mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
4683
4684 retry:
4685         spin_lock(&oo->oo_owner.so_client->cl_lock);
4686         spin_lock(&fp->fi_lock);
4687
4688         retstp = nfsd4_find_existing_open(fp, open);
4689         if (retstp)
4690                 goto out_unlock;
4691
4692         open->op_stp = NULL;
4693         refcount_inc(&stp->st_stid.sc_count);
4694         stp->st_stid.sc_type = NFS4_OPEN_STID;
4695         INIT_LIST_HEAD(&stp->st_locks);
4696         stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
4697         get_nfs4_file(fp);
4698         stp->st_stid.sc_file = fp;
4699         stp->st_access_bmap = 0;
4700         stp->st_deny_bmap = 0;
4701         stp->st_openstp = NULL;
4702         list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
4703         list_add(&stp->st_perfile, &fp->fi_stateids);
4704
4705 out_unlock:
4706         spin_unlock(&fp->fi_lock);
4707         spin_unlock(&oo->oo_owner.so_client->cl_lock);
4708         if (retstp) {
4709                 /* Handle races with CLOSE */
4710                 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
4711                         nfs4_put_stid(&retstp->st_stid);
4712                         goto retry;
4713                 }
4714                 /* To keep mutex tracking happy */
4715                 mutex_unlock(&stp->st_mutex);
4716                 stp = retstp;
4717         }
4718         return stp;
4719 }
4720
4721 /*
4722  * In the 4.0 case we need to keep the owners around a little while to handle
4723  * CLOSE replay. We still do need to release any file access that is held by
4724  * them before returning however.
4725  */
4726 static void
4727 move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
4728 {
4729         struct nfs4_ol_stateid *last;
4730         struct nfs4_openowner *oo = openowner(s->st_stateowner);
4731         struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
4732                                                 nfsd_net_id);
4733
4734         dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
4735
4736         /*
4737          * We know that we hold one reference via nfsd4_close, and another
4738          * "persistent" reference for the client. If the refcount is higher
4739          * than 2, then there are still calls in progress that are using this
4740          * stateid. We can't put the sc_file reference until they are finished.
4741          * Wait for the refcount to drop to 2. Since it has been unhashed,
4742          * there should be no danger of the refcount going back up again at
4743          * this point.
4744          */
4745         wait_event(close_wq, refcount_read(&s->st_stid.sc_count) == 2);
4746
4747         release_all_access(s);
4748         if (s->st_stid.sc_file) {
4749                 put_nfs4_file(s->st_stid.sc_file);
4750                 s->st_stid.sc_file = NULL;
4751         }
4752
4753         spin_lock(&nn->client_lock);
4754         last = oo->oo_last_closed_stid;
4755         oo->oo_last_closed_stid = s;
4756         list_move_tail(&oo->oo_close_lru, &nn->close_lru);
4757         oo->oo_time = ktime_get_boottime_seconds();
4758         spin_unlock(&nn->client_lock);
4759         if (last)
4760                 nfs4_put_stid(&last->st_stid);
4761 }
4762
4763 static noinline_for_stack struct nfs4_file *
4764 nfsd4_file_hash_lookup(const struct svc_fh *fhp)
4765 {
4766         struct inode *inode = d_inode(fhp->fh_dentry);
4767         struct rhlist_head *tmp, *list;
4768         struct nfs4_file *fi;
4769
4770         rcu_read_lock();
4771         list = rhltable_lookup(&nfs4_file_rhltable, &inode,
4772                                nfs4_file_rhash_params);
4773         rhl_for_each_entry_rcu(fi, tmp, list, fi_rlist) {
4774                 if (fh_match(&fi->fi_fhandle, &fhp->fh_handle)) {
4775                         if (refcount_inc_not_zero(&fi->fi_ref)) {
4776                                 rcu_read_unlock();
4777                                 return fi;
4778                         }
4779                 }
4780         }
4781         rcu_read_unlock();
4782         return NULL;
4783 }
4784
4785 /*
4786  * On hash insertion, identify entries with the same inode but
4787  * distinct filehandles. They will all be on the list returned
4788  * by rhltable_lookup().
4789  *
4790  * inode->i_lock prevents racing insertions from adding an entry
4791  * for the same inode/fhp pair twice.
4792  */
4793 static noinline_for_stack struct nfs4_file *
4794 nfsd4_file_hash_insert(struct nfs4_file *new, const struct svc_fh *fhp)
4795 {
4796         struct inode *inode = d_inode(fhp->fh_dentry);
4797         struct rhlist_head *tmp, *list;
4798         struct nfs4_file *ret = NULL;
4799         bool alias_found = false;
4800         struct nfs4_file *fi;
4801         int err;
4802
4803         rcu_read_lock();
4804         spin_lock(&inode->i_lock);
4805
4806         list = rhltable_lookup(&nfs4_file_rhltable, &inode,
4807                                nfs4_file_rhash_params);
4808         rhl_for_each_entry_rcu(fi, tmp, list, fi_rlist) {
4809                 if (fh_match(&fi->fi_fhandle, &fhp->fh_handle)) {
4810                         if (refcount_inc_not_zero(&fi->fi_ref))
4811                                 ret = fi;
4812                 } else
4813                         fi->fi_aliased = alias_found = true;
4814         }
4815         if (ret)
4816                 goto out_unlock;
4817
4818         nfsd4_file_init(fhp, new);
4819         err = rhltable_insert(&nfs4_file_rhltable, &new->fi_rlist,
4820                               nfs4_file_rhash_params);
4821         if (err)
4822                 goto out_unlock;
4823
4824         new->fi_aliased = alias_found;
4825         ret = new;
4826
4827 out_unlock:
4828         spin_unlock(&inode->i_lock);
4829         rcu_read_unlock();
4830         return ret;
4831 }
4832
4833 static noinline_for_stack void nfsd4_file_hash_remove(struct nfs4_file *fi)
4834 {
4835         rhltable_remove(&nfs4_file_rhltable, &fi->fi_rlist,
4836                         nfs4_file_rhash_params);
4837 }
4838
4839 /*
4840  * Called to check deny when READ with all zero stateid or
4841  * WRITE with all zero or all one stateid
4842  */
4843 static __be32
4844 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
4845 {
4846         struct nfs4_file *fp;
4847         __be32 ret = nfs_ok;
4848
4849         fp = nfsd4_file_hash_lookup(current_fh);
4850         if (!fp)
4851                 return ret;
4852
4853         /* Check for conflicting share reservations */
4854         spin_lock(&fp->fi_lock);
4855         if (fp->fi_share_deny & deny_type)
4856                 ret = nfserr_locked;
4857         spin_unlock(&fp->fi_lock);
4858         put_nfs4_file(fp);
4859         return ret;
4860 }
4861
4862 static bool nfsd4_deleg_present(const struct inode *inode)
4863 {
4864         struct file_lock_context *ctx = locks_inode_context(inode);
4865
4866         return ctx && !list_empty_careful(&ctx->flc_lease);
4867 }
4868
4869 /**
4870  * nfsd_wait_for_delegreturn - wait for delegations to be returned
4871  * @rqstp: the RPC transaction being executed
4872  * @inode: in-core inode of the file being waited for
4873  *
4874  * The timeout prevents deadlock if all nfsd threads happen to be
4875  * tied up waiting for returning delegations.
4876  *
4877  * Return values:
4878  *   %true: delegation was returned
4879  *   %false: timed out waiting for delegreturn
4880  */
4881 bool nfsd_wait_for_delegreturn(struct svc_rqst *rqstp, struct inode *inode)
4882 {
4883         long __maybe_unused timeo;
4884
4885         timeo = wait_var_event_timeout(inode, !nfsd4_deleg_present(inode),
4886                                        NFSD_DELEGRETURN_TIMEOUT);
4887         trace_nfsd_delegret_wakeup(rqstp, inode, timeo);
4888         return timeo > 0;
4889 }
4890
4891 static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
4892 {
4893         struct nfs4_delegation *dp = cb_to_delegation(cb);
4894         struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
4895                                           nfsd_net_id);
4896
4897         block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
4898
4899         /*
4900          * We can't do this in nfsd_break_deleg_cb because it is
4901          * already holding inode->i_lock.
4902          *
4903          * If the dl_time != 0, then we know that it has already been
4904          * queued for a lease break. Don't queue it again.
4905          */
4906         spin_lock(&state_lock);
4907         if (delegation_hashed(dp) && dp->dl_time == 0) {
4908                 dp->dl_time = ktime_get_boottime_seconds();
4909                 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
4910         }
4911         spin_unlock(&state_lock);
4912 }
4913
4914 static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
4915                 struct rpc_task *task)
4916 {
4917         struct nfs4_delegation *dp = cb_to_delegation(cb);
4918
4919         trace_nfsd_cb_recall_done(&dp->dl_stid.sc_stateid, task);
4920
4921         if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID ||
4922             dp->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID)
4923                 return 1;
4924
4925         switch (task->tk_status) {
4926         case 0:
4927                 return 1;
4928         case -NFS4ERR_DELAY:
4929                 rpc_delay(task, 2 * HZ);
4930                 return 0;
4931         case -EBADHANDLE:
4932         case -NFS4ERR_BAD_STATEID:
4933                 /*
4934                  * Race: client probably got cb_recall before open reply
4935                  * granting delegation.
4936                  */
4937                 if (dp->dl_retries--) {
4938                         rpc_delay(task, 2 * HZ);
4939                         return 0;
4940                 }
4941                 fallthrough;
4942         default:
4943                 return 1;
4944         }
4945 }
4946
4947 static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
4948 {
4949         struct nfs4_delegation *dp = cb_to_delegation(cb);
4950
4951         nfs4_put_stid(&dp->dl_stid);
4952 }
4953
4954 static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
4955         .prepare        = nfsd4_cb_recall_prepare,
4956         .done           = nfsd4_cb_recall_done,
4957         .release        = nfsd4_cb_recall_release,
4958 };
4959
4960 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
4961 {
4962         /*
4963          * We're assuming the state code never drops its reference
4964          * without first removing the lease.  Since we're in this lease
4965          * callback (and since the lease code is serialized by the
4966          * flc_lock) we know the server hasn't removed the lease yet, and
4967          * we know it's safe to take a reference.
4968          */
4969         refcount_inc(&dp->dl_stid.sc_count);
4970         WARN_ON_ONCE(!nfsd4_run_cb(&dp->dl_recall));
4971 }
4972
4973 /* Called from break_lease() with flc_lock held. */
4974 static bool
4975 nfsd_break_deleg_cb(struct file_lock *fl)
4976 {
4977         struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
4978         struct nfs4_file *fp = dp->dl_stid.sc_file;
4979         struct nfs4_client *clp = dp->dl_stid.sc_client;
4980         struct nfsd_net *nn;
4981
4982         trace_nfsd_cb_recall(&dp->dl_stid);
4983
4984         dp->dl_recalled = true;
4985         atomic_inc(&clp->cl_delegs_in_recall);
4986         if (try_to_expire_client(clp)) {
4987                 nn = net_generic(clp->net, nfsd_net_id);
4988                 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
4989         }
4990
4991         /*
4992          * We don't want the locks code to timeout the lease for us;
4993          * we'll remove it ourself if a delegation isn't returned
4994          * in time:
4995          */
4996         fl->fl_break_time = 0;
4997
4998         spin_lock(&fp->fi_lock);
4999         fp->fi_had_conflict = true;
5000         nfsd_break_one_deleg(dp);
5001         spin_unlock(&fp->fi_lock);
5002         return false;
5003 }
5004
5005 /**
5006  * nfsd_breaker_owns_lease - Check if lease conflict was resolved
5007  * @fl: Lock state to check
5008  *
5009  * Return values:
5010  *   %true: Lease conflict was resolved
5011  *   %false: Lease conflict was not resolved.
5012  */
5013 static bool nfsd_breaker_owns_lease(struct file_lock *fl)
5014 {
5015         struct nfs4_delegation *dl = fl->fl_owner;
5016         struct svc_rqst *rqst;
5017         struct nfs4_client *clp;
5018
5019         if (!i_am_nfsd())
5020                 return false;
5021         rqst = kthread_data(current);
5022         /* Note rq_prog == NFS_ACL_PROGRAM is also possible: */
5023         if (rqst->rq_prog != NFS_PROGRAM || rqst->rq_vers < 4)
5024                 return false;
5025         clp = *(rqst->rq_lease_breaker);
5026         return dl->dl_stid.sc_client == clp;
5027 }
5028
5029 static int
5030 nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
5031                      struct list_head *dispose)
5032 {
5033         struct nfs4_delegation *dp = (struct nfs4_delegation *)onlist->fl_owner;
5034         struct nfs4_client *clp = dp->dl_stid.sc_client;
5035
5036         if (arg & F_UNLCK) {
5037                 if (dp->dl_recalled)
5038                         atomic_dec(&clp->cl_delegs_in_recall);
5039                 return lease_modify(onlist, arg, dispose);
5040         } else
5041                 return -EAGAIN;
5042 }
5043
5044 static const struct lock_manager_operations nfsd_lease_mng_ops = {
5045         .lm_breaker_owns_lease = nfsd_breaker_owns_lease,
5046         .lm_break = nfsd_break_deleg_cb,
5047         .lm_change = nfsd_change_deleg_cb,
5048 };
5049
5050 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
5051 {
5052         if (nfsd4_has_session(cstate))
5053                 return nfs_ok;
5054         if (seqid == so->so_seqid - 1)
5055                 return nfserr_replay_me;
5056         if (seqid == so->so_seqid)
5057                 return nfs_ok;
5058         return nfserr_bad_seqid;
5059 }
5060
5061 static struct nfs4_client *lookup_clientid(clientid_t *clid, bool sessions,
5062                                                 struct nfsd_net *nn)
5063 {
5064         struct nfs4_client *found;
5065
5066         spin_lock(&nn->client_lock);
5067         found = find_confirmed_client(clid, sessions, nn);
5068         if (found)
5069                 atomic_inc(&found->cl_rpc_users);
5070         spin_unlock(&nn->client_lock);
5071         return found;
5072 }
5073
5074 static __be32 set_client(clientid_t *clid,
5075                 struct nfsd4_compound_state *cstate,
5076                 struct nfsd_net *nn)
5077 {
5078         if (cstate->clp) {
5079                 if (!same_clid(&cstate->clp->cl_clientid, clid))
5080                         return nfserr_stale_clientid;
5081                 return nfs_ok;
5082         }
5083         if (STALE_CLIENTID(clid, nn))
5084                 return nfserr_stale_clientid;
5085         /*
5086          * We're in the 4.0 case (otherwise the SEQUENCE op would have
5087          * set cstate->clp), so session = false:
5088          */
5089         cstate->clp = lookup_clientid(clid, false, nn);
5090         if (!cstate->clp)
5091                 return nfserr_expired;
5092         return nfs_ok;
5093 }
5094
5095 __be32
5096 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
5097                     struct nfsd4_open *open, struct nfsd_net *nn)
5098 {
5099         clientid_t *clientid = &open->op_clientid;
5100         struct nfs4_client *clp = NULL;
5101         unsigned int strhashval;
5102         struct nfs4_openowner *oo = NULL;
5103         __be32 status;
5104
5105         /*
5106          * In case we need it later, after we've already created the
5107          * file and don't want to risk a further failure:
5108          */
5109         open->op_file = nfsd4_alloc_file();
5110         if (open->op_file == NULL)
5111                 return nfserr_jukebox;
5112
5113         status = set_client(clientid, cstate, nn);
5114         if (status)
5115                 return status;
5116         clp = cstate->clp;
5117
5118         strhashval = ownerstr_hashval(&open->op_owner);
5119         oo = find_openstateowner_str(strhashval, open, clp);
5120         open->op_openowner = oo;
5121         if (!oo) {
5122                 goto new_owner;
5123         }
5124         if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
5125                 /* Replace unconfirmed owners without checking for replay. */
5126                 release_openowner(oo);
5127                 open->op_openowner = NULL;
5128                 goto new_owner;
5129         }
5130         status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
5131         if (status)
5132                 return status;
5133         goto alloc_stateid;
5134 new_owner:
5135         oo = alloc_init_open_stateowner(strhashval, open, cstate);
5136         if (oo == NULL)
5137                 return nfserr_jukebox;
5138         open->op_openowner = oo;
5139 alloc_stateid:
5140         open->op_stp = nfs4_alloc_open_stateid(clp);
5141         if (!open->op_stp)
5142                 return nfserr_jukebox;
5143
5144         if (nfsd4_has_session(cstate) &&
5145             (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
5146                 open->op_odstate = alloc_clnt_odstate(clp);
5147                 if (!open->op_odstate)
5148                         return nfserr_jukebox;
5149         }
5150
5151         return nfs_ok;
5152 }
5153
5154 static inline __be32
5155 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
5156 {
5157         if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
5158                 return nfserr_openmode;
5159         else
5160                 return nfs_ok;
5161 }
5162
5163 static int share_access_to_flags(u32 share_access)
5164 {
5165         return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
5166 }
5167
5168 static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
5169 {
5170         struct nfs4_stid *ret;
5171
5172         ret = find_stateid_by_type(cl, s,
5173                                 NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID);
5174         if (!ret)
5175                 return NULL;
5176         return delegstateid(ret);
5177 }
5178
5179 static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
5180 {
5181         return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
5182                open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
5183 }
5184
5185 static __be32
5186 nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
5187                 struct nfs4_delegation **dp)
5188 {
5189         int flags;
5190         __be32 status = nfserr_bad_stateid;
5191         struct nfs4_delegation *deleg;
5192
5193         deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
5194         if (deleg == NULL)
5195                 goto out;
5196         if (deleg->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) {
5197                 nfs4_put_stid(&deleg->dl_stid);
5198                 if (cl->cl_minorversion)
5199                         status = nfserr_deleg_revoked;
5200                 goto out;
5201         }
5202         flags = share_access_to_flags(open->op_share_access);
5203         status = nfs4_check_delegmode(deleg, flags);
5204         if (status) {
5205                 nfs4_put_stid(&deleg->dl_stid);
5206                 goto out;
5207         }
5208         *dp = deleg;
5209 out:
5210         if (!nfsd4_is_deleg_cur(open))
5211                 return nfs_ok;
5212         if (status)
5213                 return status;
5214         open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
5215         return nfs_ok;
5216 }
5217
5218 static inline int nfs4_access_to_access(u32 nfs4_access)
5219 {
5220         int flags = 0;
5221
5222         if (nfs4_access & NFS4_SHARE_ACCESS_READ)
5223                 flags |= NFSD_MAY_READ;
5224         if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
5225                 flags |= NFSD_MAY_WRITE;
5226         return flags;
5227 }
5228
5229 static inline __be32
5230 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
5231                 struct nfsd4_open *open)
5232 {
5233         struct iattr iattr = {
5234                 .ia_valid = ATTR_SIZE,
5235                 .ia_size = 0,
5236         };
5237         struct nfsd_attrs attrs = {
5238                 .na_iattr       = &iattr,
5239         };
5240         if (!open->op_truncate)
5241                 return 0;
5242         if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
5243                 return nfserr_inval;
5244         return nfsd_setattr(rqstp, fh, &attrs, 0, (time64_t)0);
5245 }
5246
5247 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
5248                 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
5249                 struct nfsd4_open *open, bool new_stp)
5250 {
5251         struct nfsd_file *nf = NULL;
5252         __be32 status;
5253         int oflag = nfs4_access_to_omode(open->op_share_access);
5254         int access = nfs4_access_to_access(open->op_share_access);
5255         unsigned char old_access_bmap, old_deny_bmap;
5256
5257         spin_lock(&fp->fi_lock);
5258
5259         /*
5260          * Are we trying to set a deny mode that would conflict with
5261          * current access?
5262          */
5263         status = nfs4_file_check_deny(fp, open->op_share_deny);
5264         if (status != nfs_ok) {
5265                 if (status != nfserr_share_denied) {
5266                         spin_unlock(&fp->fi_lock);
5267                         goto out;
5268                 }
5269                 if (nfs4_resolve_deny_conflicts_locked(fp, new_stp,
5270                                 stp, open->op_share_deny, false))
5271                         status = nfserr_jukebox;
5272                 spin_unlock(&fp->fi_lock);
5273                 goto out;
5274         }
5275
5276         /* set access to the file */
5277         status = nfs4_file_get_access(fp, open->op_share_access);
5278         if (status != nfs_ok) {
5279                 if (status != nfserr_share_denied) {
5280                         spin_unlock(&fp->fi_lock);
5281                         goto out;
5282                 }
5283                 if (nfs4_resolve_deny_conflicts_locked(fp, new_stp,
5284                                 stp, open->op_share_access, true))
5285                         status = nfserr_jukebox;
5286                 spin_unlock(&fp->fi_lock);
5287                 goto out;
5288         }
5289
5290         /* Set access bits in stateid */
5291         old_access_bmap = stp->st_access_bmap;
5292         set_access(open->op_share_access, stp);
5293
5294         /* Set new deny mask */
5295         old_deny_bmap = stp->st_deny_bmap;
5296         set_deny(open->op_share_deny, stp);
5297         fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
5298
5299         if (!fp->fi_fds[oflag]) {
5300                 spin_unlock(&fp->fi_lock);
5301
5302                 status = nfsd_file_acquire_opened(rqstp, cur_fh, access,
5303                                                   open->op_filp, &nf);
5304                 if (status != nfs_ok)
5305                         goto out_put_access;
5306
5307                 spin_lock(&fp->fi_lock);
5308                 if (!fp->fi_fds[oflag]) {
5309                         fp->fi_fds[oflag] = nf;
5310                         nf = NULL;
5311                 }
5312         }
5313         spin_unlock(&fp->fi_lock);
5314         if (nf)
5315                 nfsd_file_put(nf);
5316
5317         status = nfserrno(nfsd_open_break_lease(cur_fh->fh_dentry->d_inode,
5318                                                                 access));
5319         if (status)
5320                 goto out_put_access;
5321
5322         status = nfsd4_truncate(rqstp, cur_fh, open);
5323         if (status)
5324                 goto out_put_access;
5325 out:
5326         return status;
5327 out_put_access:
5328         stp->st_access_bmap = old_access_bmap;
5329         nfs4_file_put_access(fp, open->op_share_access);
5330         reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
5331         goto out;
5332 }
5333
5334 static __be32
5335 nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp,
5336                 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
5337                 struct nfsd4_open *open)
5338 {
5339         __be32 status;
5340         unsigned char old_deny_bmap = stp->st_deny_bmap;
5341
5342         if (!test_access(open->op_share_access, stp))
5343                 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open, false);
5344
5345         /* test and set deny mode */
5346         spin_lock(&fp->fi_lock);
5347         status = nfs4_file_check_deny(fp, open->op_share_deny);
5348         switch (status) {
5349         case nfs_ok:
5350                 set_deny(open->op_share_deny, stp);
5351                 fp->fi_share_deny |=
5352                         (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
5353                 break;
5354         case nfserr_share_denied:
5355                 if (nfs4_resolve_deny_conflicts_locked(fp, false,
5356                                 stp, open->op_share_deny, false))
5357                         status = nfserr_jukebox;
5358                 break;
5359         }
5360         spin_unlock(&fp->fi_lock);
5361
5362         if (status != nfs_ok)
5363                 return status;
5364
5365         status = nfsd4_truncate(rqstp, cur_fh, open);
5366         if (status != nfs_ok)
5367                 reset_union_bmap_deny(old_deny_bmap, stp);
5368         return status;
5369 }
5370
5371 /* Should we give out recallable state?: */
5372 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
5373 {
5374         if (clp->cl_cb_state == NFSD4_CB_UP)
5375                 return true;
5376         /*
5377          * In the sessions case, since we don't have to establish a
5378          * separate connection for callbacks, we assume it's OK
5379          * until we hear otherwise:
5380          */
5381         return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
5382 }
5383
5384 static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp,
5385                                                 int flag)
5386 {
5387         struct file_lock *fl;
5388
5389         fl = locks_alloc_lock();
5390         if (!fl)
5391                 return NULL;
5392         fl->fl_lmops = &nfsd_lease_mng_ops;
5393         fl->fl_flags = FL_DELEG;
5394         fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
5395         fl->fl_end = OFFSET_MAX;
5396         fl->fl_owner = (fl_owner_t)dp;
5397         fl->fl_pid = current->tgid;
5398         fl->fl_file = dp->dl_stid.sc_file->fi_deleg_file->nf_file;
5399         return fl;
5400 }
5401
5402 static int nfsd4_check_conflicting_opens(struct nfs4_client *clp,
5403                                          struct nfs4_file *fp)
5404 {
5405         struct nfs4_ol_stateid *st;
5406         struct file *f = fp->fi_deleg_file->nf_file;
5407         struct inode *ino = file_inode(f);
5408         int writes;
5409
5410         writes = atomic_read(&ino->i_writecount);
5411         if (!writes)
5412                 return 0;
5413         /*
5414          * There could be multiple filehandles (hence multiple
5415          * nfs4_files) referencing this file, but that's not too
5416          * common; let's just give up in that case rather than
5417          * trying to go look up all the clients using that other
5418          * nfs4_file as well:
5419          */
5420         if (fp->fi_aliased)
5421                 return -EAGAIN;
5422         /*
5423          * If there's a close in progress, make sure that we see it
5424          * clear any fi_fds[] entries before we see it decrement
5425          * i_writecount:
5426          */
5427         smp_mb__after_atomic();
5428
5429         if (fp->fi_fds[O_WRONLY])
5430                 writes--;
5431         if (fp->fi_fds[O_RDWR])
5432                 writes--;
5433         if (writes > 0)
5434                 return -EAGAIN; /* There may be non-NFSv4 writers */
5435         /*
5436          * It's possible there are non-NFSv4 write opens in progress,
5437          * but if they haven't incremented i_writecount yet then they
5438          * also haven't called break lease yet; so, they'll break this
5439          * lease soon enough.  So, all that's left to check for is NFSv4
5440          * opens:
5441          */
5442         spin_lock(&fp->fi_lock);
5443         list_for_each_entry(st, &fp->fi_stateids, st_perfile) {
5444                 if (st->st_openstp == NULL /* it's an open */ &&
5445                     access_permit_write(st) &&
5446                     st->st_stid.sc_client != clp) {
5447                         spin_unlock(&fp->fi_lock);
5448                         return -EAGAIN;
5449                 }
5450         }
5451         spin_unlock(&fp->fi_lock);
5452         /*
5453          * There's a small chance that we could be racing with another
5454          * NFSv4 open.  However, any open that hasn't added itself to
5455          * the fi_stateids list also hasn't called break_lease yet; so,
5456          * they'll break this lease soon enough.
5457          */
5458         return 0;
5459 }
5460
5461 /*
5462  * It's possible that between opening the dentry and setting the delegation,
5463  * that it has been renamed or unlinked. Redo the lookup to verify that this
5464  * hasn't happened.
5465  */
5466 static int
5467 nfsd4_verify_deleg_dentry(struct nfsd4_open *open, struct nfs4_file *fp,
5468                           struct svc_fh *parent)
5469 {
5470         struct svc_export *exp;
5471         struct dentry *child;
5472         __be32 err;
5473
5474         err = nfsd_lookup_dentry(open->op_rqstp, parent,
5475                                  open->op_fname, open->op_fnamelen,
5476                                  &exp, &child);
5477
5478         if (err)
5479                 return -EAGAIN;
5480
5481         exp_put(exp);
5482         dput(child);
5483         if (child != file_dentry(fp->fi_deleg_file->nf_file))
5484                 return -EAGAIN;
5485
5486         return 0;
5487 }
5488
5489 /*
5490  * We avoid breaking delegations held by a client due to its own activity, but
5491  * clearing setuid/setgid bits on a write is an implicit activity and the client
5492  * may not notice and continue using the old mode. Avoid giving out a delegation
5493  * on setuid/setgid files when the client is requesting an open for write.
5494  */
5495 static int
5496 nfsd4_verify_setuid_write(struct nfsd4_open *open, struct nfsd_file *nf)
5497 {
5498         struct inode *inode = file_inode(nf->nf_file);
5499
5500         if ((open->op_share_access & NFS4_SHARE_ACCESS_WRITE) &&
5501             (inode->i_mode & (S_ISUID|S_ISGID)))
5502                 return -EAGAIN;
5503         return 0;
5504 }
5505
5506 static struct nfs4_delegation *
5507 nfs4_set_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp,
5508                     struct svc_fh *parent)
5509 {
5510         int status = 0;
5511         struct nfs4_client *clp = stp->st_stid.sc_client;
5512         struct nfs4_file *fp = stp->st_stid.sc_file;
5513         struct nfs4_clnt_odstate *odstate = stp->st_clnt_odstate;
5514         struct nfs4_delegation *dp;
5515         struct nfsd_file *nf = NULL;
5516         struct file_lock *fl;
5517         u32 dl_type;
5518
5519         /*
5520          * The fi_had_conflict and nfs_get_existing_delegation checks
5521          * here are just optimizations; we'll need to recheck them at
5522          * the end:
5523          */
5524         if (fp->fi_had_conflict)
5525                 return ERR_PTR(-EAGAIN);
5526
5527         /*
5528          * Try for a write delegation first. RFC8881 section 10.4 says:
5529          *
5530          *  "An OPEN_DELEGATE_WRITE delegation allows the client to handle,
5531          *   on its own, all opens."
5532          *
5533          * Furthermore the client can use a write delegation for most READ
5534          * operations as well, so we require a O_RDWR file here.
5535          *
5536          * Offer a write delegation in the case of a BOTH open, and ensure
5537          * we get the O_RDWR descriptor.
5538          */
5539         if ((open->op_share_access & NFS4_SHARE_ACCESS_BOTH) == NFS4_SHARE_ACCESS_BOTH) {
5540                 nf = find_rw_file(fp);
5541                 dl_type = NFS4_OPEN_DELEGATE_WRITE;
5542         }
5543
5544         /*
5545          * If the file is being opened O_RDONLY or we couldn't get a O_RDWR
5546          * file for some reason, then try for a read delegation instead.
5547          */
5548         if (!nf && (open->op_share_access & NFS4_SHARE_ACCESS_READ)) {
5549                 nf = find_readable_file(fp);
5550                 dl_type = NFS4_OPEN_DELEGATE_READ;
5551         }
5552
5553         if (!nf)
5554                 return ERR_PTR(-EAGAIN);
5555
5556         spin_lock(&state_lock);
5557         spin_lock(&fp->fi_lock);
5558         if (nfs4_delegation_exists(clp, fp))
5559                 status = -EAGAIN;
5560         else if (nfsd4_verify_setuid_write(open, nf))
5561                 status = -EAGAIN;
5562         else if (!fp->fi_deleg_file) {
5563                 fp->fi_deleg_file = nf;
5564                 /* increment early to prevent fi_deleg_file from being
5565                  * cleared */
5566                 fp->fi_delegees = 1;
5567                 nf = NULL;
5568         } else
5569                 fp->fi_delegees++;
5570         spin_unlock(&fp->fi_lock);
5571         spin_unlock(&state_lock);
5572         if (nf)
5573                 nfsd_file_put(nf);
5574         if (status)
5575                 return ERR_PTR(status);
5576
5577         status = -ENOMEM;
5578         dp = alloc_init_deleg(clp, fp, odstate, dl_type);
5579         if (!dp)
5580                 goto out_delegees;
5581
5582         fl = nfs4_alloc_init_lease(dp, dl_type);
5583         if (!fl)
5584                 goto out_clnt_odstate;
5585
5586         status = vfs_setlease(fp->fi_deleg_file->nf_file, fl->fl_type, &fl, NULL);
5587         if (fl)
5588                 locks_free_lock(fl);
5589         if (status)
5590                 goto out_clnt_odstate;
5591
5592         if (parent) {
5593                 status = nfsd4_verify_deleg_dentry(open, fp, parent);
5594                 if (status)
5595                         goto out_unlock;
5596         }
5597
5598         status = nfsd4_check_conflicting_opens(clp, fp);
5599         if (status)
5600                 goto out_unlock;
5601
5602         /*
5603          * Now that the deleg is set, check again to ensure that nothing
5604          * raced in and changed the mode while we weren't lookng.
5605          */
5606         status = nfsd4_verify_setuid_write(open, fp->fi_deleg_file);
5607         if (status)
5608                 goto out_unlock;
5609
5610         spin_lock(&state_lock);
5611         spin_lock(&fp->fi_lock);
5612         if (fp->fi_had_conflict)
5613                 status = -EAGAIN;
5614         else
5615                 status = hash_delegation_locked(dp, fp);
5616         spin_unlock(&fp->fi_lock);
5617         spin_unlock(&state_lock);
5618
5619         if (status)
5620                 goto out_unlock;
5621
5622         return dp;
5623 out_unlock:
5624         vfs_setlease(fp->fi_deleg_file->nf_file, F_UNLCK, NULL, (void **)&dp);
5625 out_clnt_odstate:
5626         put_clnt_odstate(dp->dl_clnt_odstate);
5627         nfs4_put_stid(&dp->dl_stid);
5628 out_delegees:
5629         put_deleg_file(fp);
5630         return ERR_PTR(status);
5631 }
5632
5633 static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
5634 {
5635         open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5636         if (status == -EAGAIN)
5637                 open->op_why_no_deleg = WND4_CONTENTION;
5638         else {
5639                 open->op_why_no_deleg = WND4_RESOURCE;
5640                 switch (open->op_deleg_want) {
5641                 case NFS4_SHARE_WANT_READ_DELEG:
5642                 case NFS4_SHARE_WANT_WRITE_DELEG:
5643                 case NFS4_SHARE_WANT_ANY_DELEG:
5644                         break;
5645                 case NFS4_SHARE_WANT_CANCEL:
5646                         open->op_why_no_deleg = WND4_CANCELLED;
5647                         break;
5648                 case NFS4_SHARE_WANT_NO_DELEG:
5649                         WARN_ON_ONCE(1);
5650                 }
5651         }
5652 }
5653
5654 /*
5655  * The Linux NFS server does not offer write delegations to NFSv4.0
5656  * clients in order to avoid conflicts between write delegations and
5657  * GETATTRs requesting CHANGE or SIZE attributes.
5658  *
5659  * With NFSv4.1 and later minorversions, the SEQUENCE operation that
5660  * begins each COMPOUND contains a client ID. Delegation recall can
5661  * be avoided when the server recognizes the client sending a
5662  * GETATTR also holds write delegation it conflicts with.
5663  *
5664  * However, the NFSv4.0 protocol does not enable a server to
5665  * determine that a GETATTR originated from the client holding the
5666  * conflicting delegation versus coming from some other client. Per
5667  * RFC 7530 Section 16.7.5, the server must recall or send a
5668  * CB_GETATTR even when the GETATTR originates from the client that
5669  * holds the conflicting delegation.
5670  *
5671  * An NFSv4.0 client can trigger a pathological situation if it
5672  * always sends a DELEGRETURN preceded by a conflicting GETATTR in
5673  * the same COMPOUND. COMPOUND execution will always stop at the
5674  * GETATTR and the DELEGRETURN will never get executed. The server
5675  * eventually revokes the delegation, which can result in loss of
5676  * open or lock state.
5677  */
5678 static void
5679 nfs4_open_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp,
5680                      struct svc_fh *currentfh)
5681 {
5682         struct nfs4_delegation *dp;
5683         struct nfs4_openowner *oo = openowner(stp->st_stateowner);
5684         struct nfs4_client *clp = stp->st_stid.sc_client;
5685         struct svc_fh *parent = NULL;
5686         int cb_up;
5687         int status = 0;
5688         struct kstat stat;
5689         struct path path;
5690
5691         cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
5692         open->op_recall = false;
5693         switch (open->op_claim_type) {
5694                 case NFS4_OPEN_CLAIM_PREVIOUS:
5695                         if (!cb_up)
5696                                 open->op_recall = true;
5697                         break;
5698                 case NFS4_OPEN_CLAIM_NULL:
5699                         parent = currentfh;
5700                         fallthrough;
5701                 case NFS4_OPEN_CLAIM_FH:
5702                         /*
5703                          * Let's not give out any delegations till everyone's
5704                          * had the chance to reclaim theirs, *and* until
5705                          * NLM locks have all been reclaimed:
5706                          */
5707                         if (locks_in_grace(clp->net))
5708                                 goto out_no_deleg;
5709                         if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
5710                                 goto out_no_deleg;
5711                         if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE &&
5712                                         !clp->cl_minorversion)
5713                                 goto out_no_deleg;
5714                         break;
5715                 default:
5716                         goto out_no_deleg;
5717         }
5718         dp = nfs4_set_delegation(open, stp, parent);
5719         if (IS_ERR(dp))
5720                 goto out_no_deleg;
5721
5722         memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
5723
5724         if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE) {
5725                 open->op_delegate_type = NFS4_OPEN_DELEGATE_WRITE;
5726                 trace_nfsd_deleg_write(&dp->dl_stid.sc_stateid);
5727                 path.mnt = currentfh->fh_export->ex_path.mnt;
5728                 path.dentry = currentfh->fh_dentry;
5729                 if (vfs_getattr(&path, &stat,
5730                                 (STATX_SIZE | STATX_CTIME | STATX_CHANGE_COOKIE),
5731                                 AT_STATX_SYNC_AS_STAT)) {
5732                         nfs4_put_stid(&dp->dl_stid);
5733                         destroy_delegation(dp);
5734                         goto out_no_deleg;
5735                 }
5736                 dp->dl_cb_fattr.ncf_cur_fsize = stat.size;
5737                 dp->dl_cb_fattr.ncf_initial_cinfo =
5738                         nfsd4_change_attribute(&stat, d_inode(currentfh->fh_dentry));
5739         } else {
5740                 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
5741                 trace_nfsd_deleg_read(&dp->dl_stid.sc_stateid);
5742         }
5743         nfs4_put_stid(&dp->dl_stid);
5744         return;
5745 out_no_deleg:
5746         open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
5747         if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
5748             open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
5749                 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
5750                 open->op_recall = true;
5751         }
5752
5753         /* 4.1 client asking for a delegation? */
5754         if (open->op_deleg_want)
5755                 nfsd4_open_deleg_none_ext(open, status);
5756         return;
5757 }
5758
5759 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
5760                                         struct nfs4_delegation *dp)
5761 {
5762         if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
5763             dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
5764                 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5765                 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
5766         } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
5767                    dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
5768                 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5769                 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
5770         }
5771         /* Otherwise the client must be confused wanting a delegation
5772          * it already has, therefore we don't return
5773          * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
5774          */
5775 }
5776
5777 /**
5778  * nfsd4_process_open2 - finish open processing
5779  * @rqstp: the RPC transaction being executed
5780  * @current_fh: NFSv4 COMPOUND's current filehandle
5781  * @open: OPEN arguments
5782  *
5783  * If successful, (1) truncate the file if open->op_truncate was
5784  * set, (2) set open->op_stateid, (3) set open->op_delegation.
5785  *
5786  * Returns %nfs_ok on success; otherwise an nfs4stat value in
5787  * network byte order is returned.
5788  */
5789 __be32
5790 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
5791 {
5792         struct nfsd4_compoundres *resp = rqstp->rq_resp;
5793         struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
5794         struct nfs4_file *fp = NULL;
5795         struct nfs4_ol_stateid *stp = NULL;
5796         struct nfs4_delegation *dp = NULL;
5797         __be32 status;
5798         bool new_stp = false;
5799
5800         /*
5801          * Lookup file; if found, lookup stateid and check open request,
5802          * and check for delegations in the process of being recalled.
5803          * If not found, create the nfs4_file struct
5804          */
5805         fp = nfsd4_file_hash_insert(open->op_file, current_fh);
5806         if (unlikely(!fp))
5807                 return nfserr_jukebox;
5808         if (fp != open->op_file) {
5809                 status = nfs4_check_deleg(cl, open, &dp);
5810                 if (status)
5811                         goto out;
5812                 stp = nfsd4_find_and_lock_existing_open(fp, open);
5813         } else {
5814                 open->op_file = NULL;
5815                 status = nfserr_bad_stateid;
5816                 if (nfsd4_is_deleg_cur(open))
5817                         goto out;
5818         }
5819
5820         if (!stp) {
5821                 stp = init_open_stateid(fp, open);
5822                 if (!open->op_stp)
5823                         new_stp = true;
5824         }
5825
5826         /*
5827          * OPEN the file, or upgrade an existing OPEN.
5828          * If truncate fails, the OPEN fails.
5829          *
5830          * stp is already locked.
5831          */
5832         if (!new_stp) {
5833                 /* Stateid was found, this is an OPEN upgrade */
5834                 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
5835                 if (status) {
5836                         mutex_unlock(&stp->st_mutex);
5837                         goto out;
5838                 }
5839         } else {
5840                 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open, true);
5841                 if (status) {
5842                         stp->st_stid.sc_type = NFS4_CLOSED_STID;
5843                         release_open_stateid(stp);
5844                         mutex_unlock(&stp->st_mutex);
5845                         goto out;
5846                 }
5847
5848                 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
5849                                                         open->op_odstate);
5850                 if (stp->st_clnt_odstate == open->op_odstate)
5851                         open->op_odstate = NULL;
5852         }
5853
5854         nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
5855         mutex_unlock(&stp->st_mutex);
5856
5857         if (nfsd4_has_session(&resp->cstate)) {
5858                 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
5859                         open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5860                         open->op_why_no_deleg = WND4_NOT_WANTED;
5861                         goto nodeleg;
5862                 }
5863         }
5864
5865         /*
5866         * Attempt to hand out a delegation. No error return, because the
5867         * OPEN succeeds even if we fail.
5868         */
5869         nfs4_open_delegation(open, stp, &resp->cstate.current_fh);
5870 nodeleg:
5871         status = nfs_ok;
5872         trace_nfsd_open(&stp->st_stid.sc_stateid);
5873 out:
5874         /* 4.1 client trying to upgrade/downgrade delegation? */
5875         if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
5876             open->op_deleg_want)
5877                 nfsd4_deleg_xgrade_none_ext(open, dp);
5878
5879         if (fp)
5880                 put_nfs4_file(fp);
5881         if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
5882                 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
5883         /*
5884         * To finish the open response, we just need to set the rflags.
5885         */
5886         open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
5887         if (nfsd4_has_session(&resp->cstate))
5888                 open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK;
5889         else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED))
5890                 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
5891
5892         if (dp)
5893                 nfs4_put_stid(&dp->dl_stid);
5894         if (stp)
5895                 nfs4_put_stid(&stp->st_stid);
5896
5897         return status;
5898 }
5899
5900 void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
5901                               struct nfsd4_open *open)
5902 {
5903         if (open->op_openowner) {
5904                 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
5905
5906                 nfsd4_cstate_assign_replay(cstate, so);
5907                 nfs4_put_stateowner(so);
5908         }
5909         if (open->op_file)
5910                 kmem_cache_free(file_slab, open->op_file);
5911         if (open->op_stp)
5912                 nfs4_put_stid(&open->op_stp->st_stid);
5913         if (open->op_odstate)
5914                 kmem_cache_free(odstate_slab, open->op_odstate);
5915 }
5916
5917 __be32
5918 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5919             union nfsd4_op_u *u)
5920 {
5921         clientid_t *clid = &u->renew;
5922         struct nfs4_client *clp;
5923         __be32 status;
5924         struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5925
5926         trace_nfsd_clid_renew(clid);
5927         status = set_client(clid, cstate, nn);
5928         if (status)
5929                 return status;
5930         clp = cstate->clp;
5931         if (!list_empty(&clp->cl_delegations)
5932                         && clp->cl_cb_state != NFSD4_CB_UP)
5933                 return nfserr_cb_path_down;
5934         return nfs_ok;
5935 }
5936
5937 void
5938 nfsd4_end_grace(struct nfsd_net *nn)
5939 {
5940         /* do nothing if grace period already ended */
5941         if (nn->grace_ended)
5942                 return;
5943
5944         trace_nfsd_grace_complete(nn);
5945         nn->grace_ended = true;
5946         /*
5947          * If the server goes down again right now, an NFSv4
5948          * client will still be allowed to reclaim after it comes back up,
5949          * even if it hasn't yet had a chance to reclaim state this time.
5950          *
5951          */
5952         nfsd4_record_grace_done(nn);
5953         /*
5954          * At this point, NFSv4 clients can still reclaim.  But if the
5955          * server crashes, any that have not yet reclaimed will be out
5956          * of luck on the next boot.
5957          *
5958          * (NFSv4.1+ clients are considered to have reclaimed once they
5959          * call RECLAIM_COMPLETE.  NFSv4.0 clients are considered to
5960          * have reclaimed after their first OPEN.)
5961          */
5962         locks_end_grace(&nn->nfsd4_manager);
5963         /*
5964          * At this point, and once lockd and/or any other containers
5965          * exit their grace period, further reclaims will fail and
5966          * regular locking can resume.
5967          */
5968 }
5969
5970 /*
5971  * If we've waited a lease period but there are still clients trying to
5972  * reclaim, wait a little longer to give them a chance to finish.
5973  */
5974 static bool clients_still_reclaiming(struct nfsd_net *nn)
5975 {
5976         time64_t double_grace_period_end = nn->boot_time +
5977                                            2 * nn->nfsd4_lease;
5978
5979         if (nn->track_reclaim_completes &&
5980                         atomic_read(&nn->nr_reclaim_complete) ==
5981                         nn->reclaim_str_hashtbl_size)
5982                 return false;
5983         if (!nn->somebody_reclaimed)
5984                 return false;
5985         nn->somebody_reclaimed = false;
5986         /*
5987          * If we've given them *two* lease times to reclaim, and they're
5988          * still not done, give up:
5989          */
5990         if (ktime_get_boottime_seconds() > double_grace_period_end)
5991                 return false;
5992         return true;
5993 }
5994
5995 struct laundry_time {
5996         time64_t cutoff;
5997         time64_t new_timeo;
5998 };
5999
6000 static bool state_expired(struct laundry_time *lt, time64_t last_refresh)
6001 {
6002         time64_t time_remaining;
6003
6004         if (last_refresh < lt->cutoff)
6005                 return true;
6006         time_remaining = last_refresh - lt->cutoff;
6007         lt->new_timeo = min(lt->new_timeo, time_remaining);
6008         return false;
6009 }
6010
6011 #ifdef CONFIG_NFSD_V4_2_INTER_SSC
6012 void nfsd4_ssc_init_umount_work(struct nfsd_net *nn)
6013 {
6014         spin_lock_init(&nn->nfsd_ssc_lock);
6015         INIT_LIST_HEAD(&nn->nfsd_ssc_mount_list);
6016         init_waitqueue_head(&nn->nfsd_ssc_waitq);
6017 }
6018 EXPORT_SYMBOL_GPL(nfsd4_ssc_init_umount_work);
6019
6020 /*
6021  * This is called when nfsd is being shutdown, after all inter_ssc
6022  * cleanup were done, to destroy the ssc delayed unmount list.
6023  */
6024 static void nfsd4_ssc_shutdown_umount(struct nfsd_net *nn)
6025 {
6026         struct nfsd4_ssc_umount_item *ni = NULL;
6027         struct nfsd4_ssc_umount_item *tmp;
6028
6029         spin_lock(&nn->nfsd_ssc_lock);
6030         list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) {
6031                 list_del(&ni->nsui_list);
6032                 spin_unlock(&nn->nfsd_ssc_lock);
6033                 mntput(ni->nsui_vfsmount);
6034                 kfree(ni);
6035                 spin_lock(&nn->nfsd_ssc_lock);
6036         }
6037         spin_unlock(&nn->nfsd_ssc_lock);
6038 }
6039
6040 static void nfsd4_ssc_expire_umount(struct nfsd_net *nn)
6041 {
6042         bool do_wakeup = false;
6043         struct nfsd4_ssc_umount_item *ni = NULL;
6044         struct nfsd4_ssc_umount_item *tmp;
6045
6046         spin_lock(&nn->nfsd_ssc_lock);
6047         list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) {
6048                 if (time_after(jiffies, ni->nsui_expire)) {
6049                         if (refcount_read(&ni->nsui_refcnt) > 1)
6050                                 continue;
6051
6052                         /* mark being unmount */
6053                         ni->nsui_busy = true;
6054                         spin_unlock(&nn->nfsd_ssc_lock);
6055                         mntput(ni->nsui_vfsmount);
6056                         spin_lock(&nn->nfsd_ssc_lock);
6057
6058                         /* waiters need to start from begin of list */
6059                         list_del(&ni->nsui_list);
6060                         kfree(ni);
6061
6062                         /* wakeup ssc_connect waiters */
6063                         do_wakeup = true;
6064                         continue;
6065                 }
6066                 break;
6067         }
6068         if (do_wakeup)
6069                 wake_up_all(&nn->nfsd_ssc_waitq);
6070         spin_unlock(&nn->nfsd_ssc_lock);
6071 }
6072 #endif
6073
6074 /* Check if any lock belonging to this lockowner has any blockers */
6075 static bool
6076 nfs4_lockowner_has_blockers(struct nfs4_lockowner *lo)
6077 {
6078         struct file_lock_context *ctx;
6079         struct nfs4_ol_stateid *stp;
6080         struct nfs4_file *nf;
6081
6082         list_for_each_entry(stp, &lo->lo_owner.so_stateids, st_perstateowner) {
6083                 nf = stp->st_stid.sc_file;
6084                 ctx = locks_inode_context(nf->fi_inode);
6085                 if (!ctx)
6086                         continue;
6087                 if (locks_owner_has_blockers(ctx, lo))
6088                         return true;
6089         }
6090         return false;
6091 }
6092
6093 static bool
6094 nfs4_anylock_blockers(struct nfs4_client *clp)
6095 {
6096         int i;
6097         struct nfs4_stateowner *so;
6098         struct nfs4_lockowner *lo;
6099
6100         if (atomic_read(&clp->cl_delegs_in_recall))
6101                 return true;
6102         spin_lock(&clp->cl_lock);
6103         for (i = 0; i < OWNER_HASH_SIZE; i++) {
6104                 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[i],
6105                                 so_strhash) {
6106                         if (so->so_is_open_owner)
6107                                 continue;
6108                         lo = lockowner(so);
6109                         if (nfs4_lockowner_has_blockers(lo)) {
6110                                 spin_unlock(&clp->cl_lock);
6111                                 return true;
6112                         }
6113                 }
6114         }
6115         spin_unlock(&clp->cl_lock);
6116         return false;
6117 }
6118
6119 static void
6120 nfs4_get_client_reaplist(struct nfsd_net *nn, struct list_head *reaplist,
6121                                 struct laundry_time *lt)
6122 {
6123         unsigned int maxreap, reapcnt = 0;
6124         struct list_head *pos, *next;
6125         struct nfs4_client *clp;
6126
6127         maxreap = (atomic_read(&nn->nfs4_client_count) >= nn->nfs4_max_clients) ?
6128                         NFSD_CLIENT_MAX_TRIM_PER_RUN : 0;
6129         INIT_LIST_HEAD(reaplist);
6130         spin_lock(&nn->client_lock);
6131         list_for_each_safe(pos, next, &nn->client_lru) {
6132                 clp = list_entry(pos, struct nfs4_client, cl_lru);
6133                 if (clp->cl_state == NFSD4_EXPIRABLE)
6134                         goto exp_client;
6135                 if (!state_expired(lt, clp->cl_time))
6136                         break;
6137                 if (!atomic_read(&clp->cl_rpc_users)) {
6138                         if (clp->cl_state == NFSD4_ACTIVE)
6139                                 atomic_inc(&nn->nfsd_courtesy_clients);
6140                         clp->cl_state = NFSD4_COURTESY;
6141                 }
6142                 if (!client_has_state(clp))
6143                         goto exp_client;
6144                 if (!nfs4_anylock_blockers(clp))
6145                         if (reapcnt >= maxreap)
6146                                 continue;
6147 exp_client:
6148                 if (!mark_client_expired_locked(clp)) {
6149                         list_add(&clp->cl_lru, reaplist);
6150                         reapcnt++;
6151                 }
6152         }
6153         spin_unlock(&nn->client_lock);
6154 }
6155
6156 static void
6157 nfs4_get_courtesy_client_reaplist(struct nfsd_net *nn,
6158                                 struct list_head *reaplist)
6159 {
6160         unsigned int maxreap = 0, reapcnt = 0;
6161         struct list_head *pos, *next;
6162         struct nfs4_client *clp;
6163
6164         maxreap = NFSD_CLIENT_MAX_TRIM_PER_RUN;
6165         INIT_LIST_HEAD(reaplist);
6166
6167         spin_lock(&nn->client_lock);
6168         list_for_each_safe(pos, next, &nn->client_lru) {
6169                 clp = list_entry(pos, struct nfs4_client, cl_lru);
6170                 if (clp->cl_state == NFSD4_ACTIVE)
6171                         break;
6172                 if (reapcnt >= maxreap)
6173                         break;
6174                 if (!mark_client_expired_locked(clp)) {
6175                         list_add(&clp->cl_lru, reaplist);
6176                         reapcnt++;
6177                 }
6178         }
6179         spin_unlock(&nn->client_lock);
6180 }
6181
6182 static void
6183 nfs4_process_client_reaplist(struct list_head *reaplist)
6184 {
6185         struct list_head *pos, *next;
6186         struct nfs4_client *clp;
6187
6188         list_for_each_safe(pos, next, reaplist) {
6189                 clp = list_entry(pos, struct nfs4_client, cl_lru);
6190                 trace_nfsd_clid_purged(&clp->cl_clientid);
6191                 list_del_init(&clp->cl_lru);
6192                 expire_client(clp);
6193         }
6194 }
6195
6196 static time64_t
6197 nfs4_laundromat(struct nfsd_net *nn)
6198 {
6199         struct nfs4_openowner *oo;
6200         struct nfs4_delegation *dp;
6201         struct nfs4_ol_stateid *stp;
6202         struct nfsd4_blocked_lock *nbl;
6203         struct list_head *pos, *next, reaplist;
6204         struct laundry_time lt = {
6205                 .cutoff = ktime_get_boottime_seconds() - nn->nfsd4_lease,
6206                 .new_timeo = nn->nfsd4_lease
6207         };
6208         struct nfs4_cpntf_state *cps;
6209         copy_stateid_t *cps_t;
6210         int i;
6211
6212         if (clients_still_reclaiming(nn)) {
6213                 lt.new_timeo = 0;
6214                 goto out;
6215         }
6216         nfsd4_end_grace(nn);
6217
6218         spin_lock(&nn->s2s_cp_lock);
6219         idr_for_each_entry(&nn->s2s_cp_stateids, cps_t, i) {
6220                 cps = container_of(cps_t, struct nfs4_cpntf_state, cp_stateid);
6221                 if (cps->cp_stateid.cs_type == NFS4_COPYNOTIFY_STID &&
6222                                 state_expired(&lt, cps->cpntf_time))
6223                         _free_cpntf_state_locked(nn, cps);
6224         }
6225         spin_unlock(&nn->s2s_cp_lock);
6226         nfs4_get_client_reaplist(nn, &reaplist, &lt);
6227         nfs4_process_client_reaplist(&reaplist);
6228
6229         spin_lock(&state_lock);
6230         list_for_each_safe(pos, next, &nn->del_recall_lru) {
6231                 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
6232                 if (!state_expired(&lt, dp->dl_time))
6233                         break;
6234                 WARN_ON(!unhash_delegation_locked(dp));
6235                 list_add(&dp->dl_recall_lru, &reaplist);
6236         }
6237         spin_unlock(&state_lock);
6238         while (!list_empty(&reaplist)) {
6239                 dp = list_first_entry(&reaplist, struct nfs4_delegation,
6240                                         dl_recall_lru);
6241                 list_del_init(&dp->dl_recall_lru);
6242                 revoke_delegation(dp);
6243         }
6244
6245         spin_lock(&nn->client_lock);
6246         while (!list_empty(&nn->close_lru)) {
6247                 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
6248                                         oo_close_lru);
6249                 if (!state_expired(&lt, oo->oo_time))
6250                         break;
6251                 list_del_init(&oo->oo_close_lru);
6252                 stp = oo->oo_last_closed_stid;
6253                 oo->oo_last_closed_stid = NULL;
6254                 spin_unlock(&nn->client_lock);
6255                 nfs4_put_stid(&stp->st_stid);
6256                 spin_lock(&nn->client_lock);
6257         }
6258         spin_unlock(&nn->client_lock);
6259
6260         /*
6261          * It's possible for a client to try and acquire an already held lock
6262          * that is being held for a long time, and then lose interest in it.
6263          * So, we clean out any un-revisited request after a lease period
6264          * under the assumption that the client is no longer interested.
6265          *
6266          * RFC5661, sec. 9.6 states that the client must not rely on getting
6267          * notifications and must continue to poll for locks, even when the
6268          * server supports them. Thus this shouldn't lead to clients blocking
6269          * indefinitely once the lock does become free.
6270          */
6271         BUG_ON(!list_empty(&reaplist));
6272         spin_lock(&nn->blocked_locks_lock);
6273         while (!list_empty(&nn->blocked_locks_lru)) {
6274                 nbl = list_first_entry(&nn->blocked_locks_lru,
6275                                         struct nfsd4_blocked_lock, nbl_lru);
6276                 if (!state_expired(&lt, nbl->nbl_time))
6277                         break;
6278                 list_move(&nbl->nbl_lru, &reaplist);
6279                 list_del_init(&nbl->nbl_list);
6280         }
6281         spin_unlock(&nn->blocked_locks_lock);
6282
6283         while (!list_empty(&reaplist)) {
6284                 nbl = list_first_entry(&reaplist,
6285                                         struct nfsd4_blocked_lock, nbl_lru);
6286                 list_del_init(&nbl->nbl_lru);
6287                 free_blocked_lock(nbl);
6288         }
6289 #ifdef CONFIG_NFSD_V4_2_INTER_SSC
6290         /* service the server-to-server copy delayed unmount list */
6291         nfsd4_ssc_expire_umount(nn);
6292 #endif
6293 out:
6294         return max_t(time64_t, lt.new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
6295 }
6296
6297 static void laundromat_main(struct work_struct *);
6298
6299 static void
6300 laundromat_main(struct work_struct *laundry)
6301 {
6302         time64_t t;
6303         struct delayed_work *dwork = to_delayed_work(laundry);
6304         struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
6305                                            laundromat_work);
6306
6307         t = nfs4_laundromat(nn);
6308         queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
6309 }
6310
6311 static void
6312 courtesy_client_reaper(struct nfsd_net *nn)
6313 {
6314         struct list_head reaplist;
6315
6316         nfs4_get_courtesy_client_reaplist(nn, &reaplist);
6317         nfs4_process_client_reaplist(&reaplist);
6318 }
6319
6320 static void
6321 deleg_reaper(struct nfsd_net *nn)
6322 {
6323         struct list_head *pos, *next;
6324         struct nfs4_client *clp;
6325         struct list_head cblist;
6326
6327         INIT_LIST_HEAD(&cblist);
6328         spin_lock(&nn->client_lock);
6329         list_for_each_safe(pos, next, &nn->client_lru) {
6330                 clp = list_entry(pos, struct nfs4_client, cl_lru);
6331                 if (clp->cl_state != NFSD4_ACTIVE ||
6332                         list_empty(&clp->cl_delegations) ||
6333                         atomic_read(&clp->cl_delegs_in_recall) ||
6334                         test_bit(NFSD4_CLIENT_CB_RECALL_ANY, &clp->cl_flags) ||
6335                         (ktime_get_boottime_seconds() -
6336                                 clp->cl_ra_time < 5)) {
6337                         continue;
6338                 }
6339                 list_add(&clp->cl_ra_cblist, &cblist);
6340
6341                 /* release in nfsd4_cb_recall_any_release */
6342                 atomic_inc(&clp->cl_rpc_users);
6343                 set_bit(NFSD4_CLIENT_CB_RECALL_ANY, &clp->cl_flags);
6344                 clp->cl_ra_time = ktime_get_boottime_seconds();
6345         }
6346         spin_unlock(&nn->client_lock);
6347
6348         while (!list_empty(&cblist)) {
6349                 clp = list_first_entry(&cblist, struct nfs4_client,
6350                                         cl_ra_cblist);
6351                 list_del_init(&clp->cl_ra_cblist);
6352                 clp->cl_ra->ra_keep = 0;
6353                 clp->cl_ra->ra_bmval[0] = BIT(RCA4_TYPE_MASK_RDATA_DLG);
6354                 trace_nfsd_cb_recall_any(clp->cl_ra);
6355                 nfsd4_run_cb(&clp->cl_ra->ra_cb);
6356         }
6357 }
6358
6359 static void
6360 nfsd4_state_shrinker_worker(struct work_struct *work)
6361 {
6362         struct nfsd_net *nn = container_of(work, struct nfsd_net,
6363                                 nfsd_shrinker_work);
6364
6365         courtesy_client_reaper(nn);
6366         deleg_reaper(nn);
6367 }
6368
6369 static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
6370 {
6371         if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
6372                 return nfserr_bad_stateid;
6373         return nfs_ok;
6374 }
6375
6376 static
6377 __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
6378 {
6379         __be32 status = nfserr_openmode;
6380
6381         /* For lock stateid's, we test the parent open, not the lock: */
6382         if (stp->st_openstp)
6383                 stp = stp->st_openstp;
6384         if ((flags & WR_STATE) && !access_permit_write(stp))
6385                 goto out;
6386         if ((flags & RD_STATE) && !access_permit_read(stp))
6387                 goto out;
6388         status = nfs_ok;
6389 out:
6390         return status;
6391 }
6392
6393 static inline __be32
6394 check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
6395 {
6396         if (ONE_STATEID(stateid) && (flags & RD_STATE))
6397                 return nfs_ok;
6398         else if (opens_in_grace(net)) {
6399                 /* Answer in remaining cases depends on existence of
6400                  * conflicting state; so we must wait out the grace period. */
6401                 return nfserr_grace;
6402         } else if (flags & WR_STATE)
6403                 return nfs4_share_conflict(current_fh,
6404                                 NFS4_SHARE_DENY_WRITE);
6405         else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
6406                 return nfs4_share_conflict(current_fh,
6407                                 NFS4_SHARE_DENY_READ);
6408 }
6409
6410 static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
6411 {
6412         /*
6413          * When sessions are used the stateid generation number is ignored
6414          * when it is zero.
6415          */
6416         if (has_session && in->si_generation == 0)
6417                 return nfs_ok;
6418
6419         if (in->si_generation == ref->si_generation)
6420                 return nfs_ok;
6421
6422         /* If the client sends us a stateid from the future, it's buggy: */
6423         if (nfsd4_stateid_generation_after(in, ref))
6424                 return nfserr_bad_stateid;
6425         /*
6426          * However, we could see a stateid from the past, even from a
6427          * non-buggy client.  For example, if the client sends a lock
6428          * while some IO is outstanding, the lock may bump si_generation
6429          * while the IO is still in flight.  The client could avoid that
6430          * situation by waiting for responses on all the IO requests,
6431          * but better performance may result in retrying IO that
6432          * receives an old_stateid error if requests are rarely
6433          * reordered in flight:
6434          */
6435         return nfserr_old_stateid;
6436 }
6437
6438 static __be32 nfsd4_stid_check_stateid_generation(stateid_t *in, struct nfs4_stid *s, bool has_session)
6439 {
6440         __be32 ret;
6441
6442         spin_lock(&s->sc_lock);
6443         ret = nfsd4_verify_open_stid(s);
6444         if (ret == nfs_ok)
6445                 ret = check_stateid_generation(in, &s->sc_stateid, has_session);
6446         spin_unlock(&s->sc_lock);
6447         return ret;
6448 }
6449
6450 static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
6451 {
6452         if (ols->st_stateowner->so_is_open_owner &&
6453             !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
6454                 return nfserr_bad_stateid;
6455         return nfs_ok;
6456 }
6457
6458 static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
6459 {
6460         struct nfs4_stid *s;
6461         __be32 status = nfserr_bad_stateid;
6462
6463         if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
6464                 CLOSE_STATEID(stateid))
6465                 return status;
6466         spin_lock(&cl->cl_lock);
6467         s = find_stateid_locked(cl, stateid);
6468         if (!s)
6469                 goto out_unlock;
6470         status = nfsd4_stid_check_stateid_generation(stateid, s, 1);
6471         if (status)
6472                 goto out_unlock;
6473         switch (s->sc_type) {
6474         case NFS4_DELEG_STID:
6475                 status = nfs_ok;
6476                 break;
6477         case NFS4_REVOKED_DELEG_STID:
6478                 status = nfserr_deleg_revoked;
6479                 break;
6480         case NFS4_OPEN_STID:
6481         case NFS4_LOCK_STID:
6482                 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
6483                 break;
6484         default:
6485                 printk("unknown stateid type %x\n", s->sc_type);
6486                 fallthrough;
6487         case NFS4_CLOSED_STID:
6488         case NFS4_CLOSED_DELEG_STID:
6489                 status = nfserr_bad_stateid;
6490         }
6491 out_unlock:
6492         spin_unlock(&cl->cl_lock);
6493         return status;
6494 }
6495
6496 __be32
6497 nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
6498                      stateid_t *stateid, unsigned char typemask,
6499                      struct nfs4_stid **s, struct nfsd_net *nn)
6500 {
6501         __be32 status;
6502         struct nfs4_stid *stid;
6503         bool return_revoked = false;
6504
6505         /*
6506          *  only return revoked delegations if explicitly asked.
6507          *  otherwise we report revoked or bad_stateid status.
6508          */
6509         if (typemask & NFS4_REVOKED_DELEG_STID)
6510                 return_revoked = true;
6511         else if (typemask & NFS4_DELEG_STID)
6512                 typemask |= NFS4_REVOKED_DELEG_STID;
6513
6514         if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
6515                 CLOSE_STATEID(stateid))
6516                 return nfserr_bad_stateid;
6517         status = set_client(&stateid->si_opaque.so_clid, cstate, nn);
6518         if (status == nfserr_stale_clientid) {
6519                 if (cstate->session)
6520                         return nfserr_bad_stateid;
6521                 return nfserr_stale_stateid;
6522         }
6523         if (status)
6524                 return status;
6525         stid = find_stateid_by_type(cstate->clp, stateid, typemask);
6526         if (!stid)
6527                 return nfserr_bad_stateid;
6528         if ((stid->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) {
6529                 nfs4_put_stid(stid);
6530                 if (cstate->minorversion)
6531                         return nfserr_deleg_revoked;
6532                 return nfserr_bad_stateid;
6533         }
6534         *s = stid;
6535         return nfs_ok;
6536 }
6537
6538 static struct nfsd_file *
6539 nfs4_find_file(struct nfs4_stid *s, int flags)
6540 {
6541         struct nfsd_file *ret = NULL;
6542
6543         if (!s)
6544                 return NULL;
6545
6546         switch (s->sc_type) {
6547         case NFS4_DELEG_STID:
6548                 spin_lock(&s->sc_file->fi_lock);
6549                 ret = nfsd_file_get(s->sc_file->fi_deleg_file);
6550                 spin_unlock(&s->sc_file->fi_lock);
6551                 break;
6552         case NFS4_OPEN_STID:
6553         case NFS4_LOCK_STID:
6554                 if (flags & RD_STATE)
6555                         ret = find_readable_file(s->sc_file);
6556                 else
6557                         ret = find_writeable_file(s->sc_file);
6558         }
6559
6560         return ret;
6561 }
6562
6563 static __be32
6564 nfs4_check_olstateid(struct nfs4_ol_stateid *ols, int flags)
6565 {
6566         __be32 status;
6567
6568         status = nfsd4_check_openowner_confirmed(ols);
6569         if (status)
6570                 return status;
6571         return nfs4_check_openmode(ols, flags);
6572 }
6573
6574 static __be32
6575 nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
6576                 struct nfsd_file **nfp, int flags)
6577 {
6578         int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
6579         struct nfsd_file *nf;
6580         __be32 status;
6581
6582         nf = nfs4_find_file(s, flags);
6583         if (nf) {
6584                 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
6585                                 acc | NFSD_MAY_OWNER_OVERRIDE);
6586                 if (status) {
6587                         nfsd_file_put(nf);
6588                         goto out;
6589                 }
6590         } else {
6591                 status = nfsd_file_acquire(rqstp, fhp, acc, &nf);
6592                 if (status)
6593                         return status;
6594         }
6595         *nfp = nf;
6596 out:
6597         return status;
6598 }
6599 static void
6600 _free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
6601 {
6602         WARN_ON_ONCE(cps->cp_stateid.cs_type != NFS4_COPYNOTIFY_STID);
6603         if (!refcount_dec_and_test(&cps->cp_stateid.cs_count))
6604                 return;
6605         list_del(&cps->cp_list);
6606         idr_remove(&nn->s2s_cp_stateids,
6607                    cps->cp_stateid.cs_stid.si_opaque.so_id);
6608         kfree(cps);
6609 }
6610 /*
6611  * A READ from an inter server to server COPY will have a
6612  * copy stateid. Look up the copy notify stateid from the
6613  * idr structure and take a reference on it.
6614  */
6615 __be32 manage_cpntf_state(struct nfsd_net *nn, stateid_t *st,
6616                           struct nfs4_client *clp,
6617                           struct nfs4_cpntf_state **cps)
6618 {
6619         copy_stateid_t *cps_t;
6620         struct nfs4_cpntf_state *state = NULL;
6621
6622         if (st->si_opaque.so_clid.cl_id != nn->s2s_cp_cl_id)
6623                 return nfserr_bad_stateid;
6624         spin_lock(&nn->s2s_cp_lock);
6625         cps_t = idr_find(&nn->s2s_cp_stateids, st->si_opaque.so_id);
6626         if (cps_t) {
6627                 state = container_of(cps_t, struct nfs4_cpntf_state,
6628                                      cp_stateid);
6629                 if (state->cp_stateid.cs_type != NFS4_COPYNOTIFY_STID) {
6630                         state = NULL;
6631                         goto unlock;
6632                 }
6633                 if (!clp)
6634                         refcount_inc(&state->cp_stateid.cs_count);
6635                 else
6636                         _free_cpntf_state_locked(nn, state);
6637         }
6638 unlock:
6639         spin_unlock(&nn->s2s_cp_lock);
6640         if (!state)
6641                 return nfserr_bad_stateid;
6642         if (!clp && state)
6643                 *cps = state;
6644         return 0;
6645 }
6646
6647 static __be32 find_cpntf_state(struct nfsd_net *nn, stateid_t *st,
6648                                struct nfs4_stid **stid)
6649 {
6650         __be32 status;
6651         struct nfs4_cpntf_state *cps = NULL;
6652         struct nfs4_client *found;
6653
6654         status = manage_cpntf_state(nn, st, NULL, &cps);
6655         if (status)
6656                 return status;
6657
6658         cps->cpntf_time = ktime_get_boottime_seconds();
6659
6660         status = nfserr_expired;
6661         found = lookup_clientid(&cps->cp_p_clid, true, nn);
6662         if (!found)
6663                 goto out;
6664
6665         *stid = find_stateid_by_type(found, &cps->cp_p_stateid,
6666                         NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID);
6667         if (*stid)
6668                 status = nfs_ok;
6669         else
6670                 status = nfserr_bad_stateid;
6671
6672         put_client_renew(found);
6673 out:
6674         nfs4_put_cpntf_state(nn, cps);
6675         return status;
6676 }
6677
6678 void nfs4_put_cpntf_state(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
6679 {
6680         spin_lock(&nn->s2s_cp_lock);
6681         _free_cpntf_state_locked(nn, cps);
6682         spin_unlock(&nn->s2s_cp_lock);
6683 }
6684
6685 /**
6686  * nfs4_preprocess_stateid_op - find and prep stateid for an operation
6687  * @rqstp: incoming request from client
6688  * @cstate: current compound state
6689  * @fhp: filehandle associated with requested stateid
6690  * @stateid: stateid (provided by client)
6691  * @flags: flags describing type of operation to be done
6692  * @nfp: optional nfsd_file return pointer (may be NULL)
6693  * @cstid: optional returned nfs4_stid pointer (may be NULL)
6694  *
6695  * Given info from the client, look up a nfs4_stid for the operation. On
6696  * success, it returns a reference to the nfs4_stid and/or the nfsd_file
6697  * associated with it.
6698  */
6699 __be32
6700 nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
6701                 struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
6702                 stateid_t *stateid, int flags, struct nfsd_file **nfp,
6703                 struct nfs4_stid **cstid)
6704 {
6705         struct net *net = SVC_NET(rqstp);
6706         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6707         struct nfs4_stid *s = NULL;
6708         __be32 status;
6709
6710         if (nfp)
6711                 *nfp = NULL;
6712
6713         if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
6714                 if (cstid)
6715                         status = nfserr_bad_stateid;
6716                 else
6717                         status = check_special_stateids(net, fhp, stateid,
6718                                                                         flags);
6719                 goto done;
6720         }
6721
6722         status = nfsd4_lookup_stateid(cstate, stateid,
6723                                 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
6724                                 &s, nn);
6725         if (status == nfserr_bad_stateid)
6726                 status = find_cpntf_state(nn, stateid, &s);
6727         if (status)
6728                 return status;
6729         status = nfsd4_stid_check_stateid_generation(stateid, s,
6730                         nfsd4_has_session(cstate));
6731         if (status)
6732                 goto out;
6733
6734         switch (s->sc_type) {
6735         case NFS4_DELEG_STID:
6736                 status = nfs4_check_delegmode(delegstateid(s), flags);
6737                 break;
6738         case NFS4_OPEN_STID:
6739         case NFS4_LOCK_STID:
6740                 status = nfs4_check_olstateid(openlockstateid(s), flags);
6741                 break;
6742         default:
6743                 status = nfserr_bad_stateid;
6744                 break;
6745         }
6746         if (status)
6747                 goto out;
6748         status = nfs4_check_fh(fhp, s);
6749
6750 done:
6751         if (status == nfs_ok && nfp)
6752                 status = nfs4_check_file(rqstp, fhp, s, nfp, flags);
6753 out:
6754         if (s) {
6755                 if (!status && cstid)
6756                         *cstid = s;
6757                 else
6758                         nfs4_put_stid(s);
6759         }
6760         return status;
6761 }
6762
6763 /*
6764  * Test if the stateid is valid
6765  */
6766 __be32
6767 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6768                    union nfsd4_op_u *u)
6769 {
6770         struct nfsd4_test_stateid *test_stateid = &u->test_stateid;
6771         struct nfsd4_test_stateid_id *stateid;
6772         struct nfs4_client *cl = cstate->clp;
6773
6774         list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
6775                 stateid->ts_id_status =
6776                         nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
6777
6778         return nfs_ok;
6779 }
6780
6781 static __be32
6782 nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
6783 {
6784         struct nfs4_ol_stateid *stp = openlockstateid(s);
6785         __be32 ret;
6786
6787         ret = nfsd4_lock_ol_stateid(stp);
6788         if (ret)
6789                 goto out_put_stid;
6790
6791         ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
6792         if (ret)
6793                 goto out;
6794
6795         ret = nfserr_locks_held;
6796         if (check_for_locks(stp->st_stid.sc_file,
6797                             lockowner(stp->st_stateowner)))
6798                 goto out;
6799
6800         release_lock_stateid(stp);
6801         ret = nfs_ok;
6802
6803 out:
6804         mutex_unlock(&stp->st_mutex);
6805 out_put_stid:
6806         nfs4_put_stid(s);
6807         return ret;
6808 }
6809
6810 __be32
6811 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6812                    union nfsd4_op_u *u)
6813 {
6814         struct nfsd4_free_stateid *free_stateid = &u->free_stateid;
6815         stateid_t *stateid = &free_stateid->fr_stateid;
6816         struct nfs4_stid *s;
6817         struct nfs4_delegation *dp;
6818         struct nfs4_client *cl = cstate->clp;
6819         __be32 ret = nfserr_bad_stateid;
6820
6821         spin_lock(&cl->cl_lock);
6822         s = find_stateid_locked(cl, stateid);
6823         if (!s)
6824                 goto out_unlock;
6825         spin_lock(&s->sc_lock);
6826         switch (s->sc_type) {
6827         case NFS4_DELEG_STID:
6828                 ret = nfserr_locks_held;
6829                 break;
6830         case NFS4_OPEN_STID:
6831                 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
6832                 if (ret)
6833                         break;
6834                 ret = nfserr_locks_held;
6835                 break;
6836         case NFS4_LOCK_STID:
6837                 spin_unlock(&s->sc_lock);
6838                 refcount_inc(&s->sc_count);
6839                 spin_unlock(&cl->cl_lock);
6840                 ret = nfsd4_free_lock_stateid(stateid, s);
6841                 goto out;
6842         case NFS4_REVOKED_DELEG_STID:
6843                 spin_unlock(&s->sc_lock);
6844                 dp = delegstateid(s);
6845                 list_del_init(&dp->dl_recall_lru);
6846                 spin_unlock(&cl->cl_lock);
6847                 nfs4_put_stid(s);
6848                 ret = nfs_ok;
6849                 goto out;
6850         /* Default falls through and returns nfserr_bad_stateid */
6851         }
6852         spin_unlock(&s->sc_lock);
6853 out_unlock:
6854         spin_unlock(&cl->cl_lock);
6855 out:
6856         return ret;
6857 }
6858
6859 static inline int
6860 setlkflg (int type)
6861 {
6862         return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
6863                 RD_STATE : WR_STATE;
6864 }
6865
6866 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
6867 {
6868         struct svc_fh *current_fh = &cstate->current_fh;
6869         struct nfs4_stateowner *sop = stp->st_stateowner;
6870         __be32 status;
6871
6872         status = nfsd4_check_seqid(cstate, sop, seqid);
6873         if (status)
6874                 return status;
6875         status = nfsd4_lock_ol_stateid(stp);
6876         if (status != nfs_ok)
6877                 return status;
6878         status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
6879         if (status == nfs_ok)
6880                 status = nfs4_check_fh(current_fh, &stp->st_stid);
6881         if (status != nfs_ok)
6882                 mutex_unlock(&stp->st_mutex);
6883         return status;
6884 }
6885
6886 /**
6887  * nfs4_preprocess_seqid_op - find and prep an ol_stateid for a seqid-morphing op
6888  * @cstate: compund state
6889  * @seqid: seqid (provided by client)
6890  * @stateid: stateid (provided by client)
6891  * @typemask: mask of allowable types for this operation
6892  * @stpp: return pointer for the stateid found
6893  * @nn: net namespace for request
6894  *
6895  * Given a stateid+seqid from a client, look up an nfs4_ol_stateid and
6896  * return it in @stpp. On a nfs_ok return, the returned stateid will
6897  * have its st_mutex locked.
6898  */
6899 static __be32
6900 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
6901                          stateid_t *stateid, char typemask,
6902                          struct nfs4_ol_stateid **stpp,
6903                          struct nfsd_net *nn)
6904 {
6905         __be32 status;
6906         struct nfs4_stid *s;
6907         struct nfs4_ol_stateid *stp = NULL;
6908
6909         trace_nfsd_preprocess(seqid, stateid);
6910
6911         *stpp = NULL;
6912         status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
6913         if (status)
6914                 return status;
6915         stp = openlockstateid(s);
6916         nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
6917
6918         status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
6919         if (!status)
6920                 *stpp = stp;
6921         else
6922                 nfs4_put_stid(&stp->st_stid);
6923         return status;
6924 }
6925
6926 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
6927                                                  stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
6928 {
6929         __be32 status;
6930         struct nfs4_openowner *oo;
6931         struct nfs4_ol_stateid *stp;
6932
6933         status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
6934                                                 NFS4_OPEN_STID, &stp, nn);
6935         if (status)
6936                 return status;
6937         oo = openowner(stp->st_stateowner);
6938         if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
6939                 mutex_unlock(&stp->st_mutex);
6940                 nfs4_put_stid(&stp->st_stid);
6941                 return nfserr_bad_stateid;
6942         }
6943         *stpp = stp;
6944         return nfs_ok;
6945 }
6946
6947 __be32
6948 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6949                    union nfsd4_op_u *u)
6950 {
6951         struct nfsd4_open_confirm *oc = &u->open_confirm;
6952         __be32 status;
6953         struct nfs4_openowner *oo;
6954         struct nfs4_ol_stateid *stp;
6955         struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6956
6957         dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
6958                         cstate->current_fh.fh_dentry);
6959
6960         status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
6961         if (status)
6962                 return status;
6963
6964         status = nfs4_preprocess_seqid_op(cstate,
6965                                         oc->oc_seqid, &oc->oc_req_stateid,
6966                                         NFS4_OPEN_STID, &stp, nn);
6967         if (status)
6968                 goto out;
6969         oo = openowner(stp->st_stateowner);
6970         status = nfserr_bad_stateid;
6971         if (oo->oo_flags & NFS4_OO_CONFIRMED) {
6972                 mutex_unlock(&stp->st_mutex);
6973                 goto put_stateid;
6974         }
6975         oo->oo_flags |= NFS4_OO_CONFIRMED;
6976         nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
6977         mutex_unlock(&stp->st_mutex);
6978         trace_nfsd_open_confirm(oc->oc_seqid, &stp->st_stid.sc_stateid);
6979         nfsd4_client_record_create(oo->oo_owner.so_client);
6980         status = nfs_ok;
6981 put_stateid:
6982         nfs4_put_stid(&stp->st_stid);
6983 out:
6984         nfsd4_bump_seqid(cstate, status);
6985         return status;
6986 }
6987
6988 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
6989 {
6990         if (!test_access(access, stp))
6991                 return;
6992         nfs4_file_put_access(stp->st_stid.sc_file, access);
6993         clear_access(access, stp);
6994 }
6995
6996 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
6997 {
6998         switch (to_access) {
6999         case NFS4_SHARE_ACCESS_READ:
7000                 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
7001                 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
7002                 break;
7003         case NFS4_SHARE_ACCESS_WRITE:
7004                 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
7005                 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
7006                 break;
7007         case NFS4_SHARE_ACCESS_BOTH:
7008                 break;
7009         default:
7010                 WARN_ON_ONCE(1);
7011         }
7012 }
7013
7014 __be32
7015 nfsd4_open_downgrade(struct svc_rqst *rqstp,
7016                      struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
7017 {
7018         struct nfsd4_open_downgrade *od = &u->open_downgrade;
7019         __be32 status;
7020         struct nfs4_ol_stateid *stp;
7021         struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
7022
7023         dprintk("NFSD: nfsd4_open_downgrade on file %pd\n", 
7024                         cstate->current_fh.fh_dentry);
7025
7026         /* We don't yet support WANT bits: */
7027         if (od->od_deleg_want)
7028                 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
7029                         od->od_deleg_want);
7030
7031         status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
7032                                         &od->od_stateid, &stp, nn);
7033         if (status)
7034                 goto out; 
7035         status = nfserr_inval;
7036         if (!test_access(od->od_share_access, stp)) {
7037                 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
7038                         stp->st_access_bmap, od->od_share_access);
7039                 goto put_stateid;
7040         }
7041         if (!test_deny(od->od_share_deny, stp)) {
7042                 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
7043                         stp->st_deny_bmap, od->od_share_deny);
7044                 goto put_stateid;
7045         }
7046         nfs4_stateid_downgrade(stp, od->od_share_access);
7047         reset_union_bmap_deny(od->od_share_deny, stp);
7048         nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
7049         status = nfs_ok;
7050 put_stateid:
7051         mutex_unlock(&stp->st_mutex);
7052         nfs4_put_stid(&stp->st_stid);
7053 out:
7054         nfsd4_bump_seqid(cstate, status);
7055         return status;
7056 }
7057
7058 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
7059 {
7060         struct nfs4_client *clp = s->st_stid.sc_client;
7061         bool unhashed;
7062         LIST_HEAD(reaplist);
7063         struct nfs4_ol_stateid *stp;
7064
7065         spin_lock(&clp->cl_lock);
7066         unhashed = unhash_open_stateid(s, &reaplist);
7067
7068         if (clp->cl_minorversion) {
7069                 if (unhashed)
7070                         put_ol_stateid_locked(s, &reaplist);
7071                 spin_unlock(&clp->cl_lock);
7072                 list_for_each_entry(stp, &reaplist, st_locks)
7073                         nfs4_free_cpntf_statelist(clp->net, &stp->st_stid);
7074                 free_ol_stateid_reaplist(&reaplist);
7075         } else {
7076                 spin_unlock(&clp->cl_lock);
7077                 free_ol_stateid_reaplist(&reaplist);
7078                 if (unhashed)
7079                         move_to_close_lru(s, clp->net);
7080         }
7081 }
7082
7083 /*
7084  * nfs4_unlock_state() called after encode
7085  */
7086 __be32
7087 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7088                 union nfsd4_op_u *u)
7089 {
7090         struct nfsd4_close *close = &u->close;
7091         __be32 status;
7092         struct nfs4_ol_stateid *stp;
7093         struct net *net = SVC_NET(rqstp);
7094         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7095
7096         dprintk("NFSD: nfsd4_close on file %pd\n", 
7097                         cstate->current_fh.fh_dentry);
7098
7099         status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
7100                                         &close->cl_stateid,
7101                                         NFS4_OPEN_STID|NFS4_CLOSED_STID,
7102                                         &stp, nn);
7103         nfsd4_bump_seqid(cstate, status);
7104         if (status)
7105                 goto out; 
7106
7107         stp->st_stid.sc_type = NFS4_CLOSED_STID;
7108
7109         /*
7110          * Technically we don't _really_ have to increment or copy it, since
7111          * it should just be gone after this operation and we clobber the
7112          * copied value below, but we continue to do so here just to ensure
7113          * that racing ops see that there was a state change.
7114          */
7115         nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
7116
7117         nfsd4_close_open_stateid(stp);
7118         mutex_unlock(&stp->st_mutex);
7119
7120         /* v4.1+ suggests that we send a special stateid in here, since the
7121          * clients should just ignore this anyway. Since this is not useful
7122          * for v4.0 clients either, we set it to the special close_stateid
7123          * universally.
7124          *
7125          * See RFC5661 section 18.2.4, and RFC7530 section 16.2.5
7126          */
7127         memcpy(&close->cl_stateid, &close_stateid, sizeof(close->cl_stateid));
7128
7129         /* put reference from nfs4_preprocess_seqid_op */
7130         nfs4_put_stid(&stp->st_stid);
7131 out:
7132         return status;
7133 }
7134
7135 __be32
7136 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7137                   union nfsd4_op_u *u)
7138 {
7139         struct nfsd4_delegreturn *dr = &u->delegreturn;
7140         struct nfs4_delegation *dp;
7141         stateid_t *stateid = &dr->dr_stateid;
7142         struct nfs4_stid *s;
7143         __be32 status;
7144         struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
7145
7146         if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
7147                 return status;
7148
7149         status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
7150         if (status)
7151                 goto out;
7152         dp = delegstateid(s);
7153         status = nfsd4_stid_check_stateid_generation(stateid, &dp->dl_stid, nfsd4_has_session(cstate));
7154         if (status)
7155                 goto put_stateid;
7156
7157         trace_nfsd_deleg_return(stateid);
7158         wake_up_var(d_inode(cstate->current_fh.fh_dentry));
7159         destroy_delegation(dp);
7160 put_stateid:
7161         nfs4_put_stid(&dp->dl_stid);
7162 out:
7163         return status;
7164 }
7165
7166 /* last octet in a range */
7167 static inline u64
7168 last_byte_offset(u64 start, u64 len)
7169 {
7170         u64 end;
7171
7172         WARN_ON_ONCE(!len);
7173         end = start + len;
7174         return end > start ? end - 1: NFS4_MAX_UINT64;
7175 }
7176
7177 /*
7178  * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
7179  * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
7180  * byte, because of sign extension problems.  Since NFSv4 calls for 64-bit
7181  * locking, this prevents us from being completely protocol-compliant.  The
7182  * real solution to this problem is to start using unsigned file offsets in
7183  * the VFS, but this is a very deep change!
7184  */
7185 static inline void
7186 nfs4_transform_lock_offset(struct file_lock *lock)
7187 {
7188         if (lock->fl_start < 0)
7189                 lock->fl_start = OFFSET_MAX;
7190         if (lock->fl_end < 0)
7191                 lock->fl_end = OFFSET_MAX;
7192 }
7193
7194 static fl_owner_t
7195 nfsd4_lm_get_owner(fl_owner_t owner)
7196 {
7197         struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
7198
7199         nfs4_get_stateowner(&lo->lo_owner);
7200         return owner;
7201 }
7202
7203 static void
7204 nfsd4_lm_put_owner(fl_owner_t owner)
7205 {
7206         struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
7207
7208         if (lo)
7209                 nfs4_put_stateowner(&lo->lo_owner);
7210 }
7211
7212 /* return pointer to struct nfs4_client if client is expirable */
7213 static bool
7214 nfsd4_lm_lock_expirable(struct file_lock *cfl)
7215 {
7216         struct nfs4_lockowner *lo = (struct nfs4_lockowner *)cfl->fl_owner;
7217         struct nfs4_client *clp = lo->lo_owner.so_client;
7218         struct nfsd_net *nn;
7219
7220         if (try_to_expire_client(clp)) {
7221                 nn = net_generic(clp->net, nfsd_net_id);
7222                 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
7223                 return true;
7224         }
7225         return false;
7226 }
7227
7228 /* schedule laundromat to run immediately and wait for it to complete */
7229 static void
7230 nfsd4_lm_expire_lock(void)
7231 {
7232         flush_workqueue(laundry_wq);
7233 }
7234
7235 static void
7236 nfsd4_lm_notify(struct file_lock *fl)
7237 {
7238         struct nfs4_lockowner           *lo = (struct nfs4_lockowner *)fl->fl_owner;
7239         struct net                      *net = lo->lo_owner.so_client->net;
7240         struct nfsd_net                 *nn = net_generic(net, nfsd_net_id);
7241         struct nfsd4_blocked_lock       *nbl = container_of(fl,
7242                                                 struct nfsd4_blocked_lock, nbl_lock);
7243         bool queue = false;
7244
7245         /* An empty list means that something else is going to be using it */
7246         spin_lock(&nn->blocked_locks_lock);
7247         if (!list_empty(&nbl->nbl_list)) {
7248                 list_del_init(&nbl->nbl_list);
7249                 list_del_init(&nbl->nbl_lru);
7250                 queue = true;
7251         }
7252         spin_unlock(&nn->blocked_locks_lock);
7253
7254         if (queue) {
7255                 trace_nfsd_cb_notify_lock(lo, nbl);
7256                 nfsd4_run_cb(&nbl->nbl_cb);
7257         }
7258 }
7259
7260 static const struct lock_manager_operations nfsd_posix_mng_ops  = {
7261         .lm_mod_owner = THIS_MODULE,
7262         .lm_notify = nfsd4_lm_notify,
7263         .lm_get_owner = nfsd4_lm_get_owner,
7264         .lm_put_owner = nfsd4_lm_put_owner,
7265         .lm_lock_expirable = nfsd4_lm_lock_expirable,
7266         .lm_expire_lock = nfsd4_lm_expire_lock,
7267 };
7268
7269 static inline void
7270 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
7271 {
7272         struct nfs4_lockowner *lo;
7273
7274         if (fl->fl_lmops == &nfsd_posix_mng_ops) {
7275                 lo = (struct nfs4_lockowner *) fl->fl_owner;
7276                 xdr_netobj_dup(&deny->ld_owner, &lo->lo_owner.so_owner,
7277                                                 GFP_KERNEL);
7278                 if (!deny->ld_owner.data)
7279                         /* We just don't care that much */
7280                         goto nevermind;
7281                 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
7282         } else {
7283 nevermind:
7284                 deny->ld_owner.len = 0;
7285                 deny->ld_owner.data = NULL;
7286                 deny->ld_clientid.cl_boot = 0;
7287                 deny->ld_clientid.cl_id = 0;
7288         }
7289         deny->ld_start = fl->fl_start;
7290         deny->ld_length = NFS4_MAX_UINT64;
7291         if (fl->fl_end != NFS4_MAX_UINT64)
7292                 deny->ld_length = fl->fl_end - fl->fl_start + 1;        
7293         deny->ld_type = NFS4_READ_LT;
7294         if (fl->fl_type != F_RDLCK)
7295                 deny->ld_type = NFS4_WRITE_LT;
7296 }
7297
7298 static struct nfs4_lockowner *
7299 find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
7300 {
7301         unsigned int strhashval = ownerstr_hashval(owner);
7302         struct nfs4_stateowner *so;
7303
7304         lockdep_assert_held(&clp->cl_lock);
7305
7306         list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
7307                             so_strhash) {
7308                 if (so->so_is_open_owner)
7309                         continue;
7310                 if (same_owner_str(so, owner))
7311                         return lockowner(nfs4_get_stateowner(so));
7312         }
7313         return NULL;
7314 }
7315
7316 static struct nfs4_lockowner *
7317 find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
7318 {
7319         struct nfs4_lockowner *lo;
7320
7321         spin_lock(&clp->cl_lock);
7322         lo = find_lockowner_str_locked(clp, owner);
7323         spin_unlock(&clp->cl_lock);
7324         return lo;
7325 }
7326
7327 static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
7328 {
7329         unhash_lockowner_locked(lockowner(sop));
7330 }
7331
7332 static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
7333 {
7334         struct nfs4_lockowner *lo = lockowner(sop);
7335
7336         kmem_cache_free(lockowner_slab, lo);
7337 }
7338
7339 static const struct nfs4_stateowner_operations lockowner_ops = {
7340         .so_unhash =    nfs4_unhash_lockowner,
7341         .so_free =      nfs4_free_lockowner,
7342 };
7343
7344 /*
7345  * Alloc a lock owner structure.
7346  * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has 
7347  * occurred. 
7348  *
7349  * strhashval = ownerstr_hashval
7350  */
7351 static struct nfs4_lockowner *
7352 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
7353                            struct nfs4_ol_stateid *open_stp,
7354                            struct nfsd4_lock *lock)
7355 {
7356         struct nfs4_lockowner *lo, *ret;
7357
7358         lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
7359         if (!lo)
7360                 return NULL;
7361         INIT_LIST_HEAD(&lo->lo_blocked);
7362         INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
7363         lo->lo_owner.so_is_open_owner = 0;
7364         lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
7365         lo->lo_owner.so_ops = &lockowner_ops;
7366         spin_lock(&clp->cl_lock);
7367         ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
7368         if (ret == NULL) {
7369                 list_add(&lo->lo_owner.so_strhash,
7370                          &clp->cl_ownerstr_hashtbl[strhashval]);
7371                 ret = lo;
7372         } else
7373                 nfs4_free_stateowner(&lo->lo_owner);
7374
7375         spin_unlock(&clp->cl_lock);
7376         return ret;
7377 }
7378
7379 static struct nfs4_ol_stateid *
7380 find_lock_stateid(const struct nfs4_lockowner *lo,
7381                   const struct nfs4_ol_stateid *ost)
7382 {
7383         struct nfs4_ol_stateid *lst;
7384
7385         lockdep_assert_held(&ost->st_stid.sc_client->cl_lock);
7386
7387         /* If ost is not hashed, ost->st_locks will not be valid */
7388         if (!nfs4_ol_stateid_unhashed(ost))
7389                 list_for_each_entry(lst, &ost->st_locks, st_locks) {
7390                         if (lst->st_stateowner == &lo->lo_owner) {
7391                                 refcount_inc(&lst->st_stid.sc_count);
7392                                 return lst;
7393                         }
7394                 }
7395         return NULL;
7396 }
7397
7398 static struct nfs4_ol_stateid *
7399 init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
7400                   struct nfs4_file *fp, struct inode *inode,
7401                   struct nfs4_ol_stateid *open_stp)
7402 {
7403         struct nfs4_client *clp = lo->lo_owner.so_client;
7404         struct nfs4_ol_stateid *retstp;
7405
7406         mutex_init(&stp->st_mutex);
7407         mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
7408 retry:
7409         spin_lock(&clp->cl_lock);
7410         if (nfs4_ol_stateid_unhashed(open_stp))
7411                 goto out_close;
7412         retstp = find_lock_stateid(lo, open_stp);
7413         if (retstp)
7414                 goto out_found;
7415         refcount_inc(&stp->st_stid.sc_count);
7416         stp->st_stid.sc_type = NFS4_LOCK_STID;
7417         stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
7418         get_nfs4_file(fp);
7419         stp->st_stid.sc_file = fp;
7420         stp->st_access_bmap = 0;
7421         stp->st_deny_bmap = open_stp->st_deny_bmap;
7422         stp->st_openstp = open_stp;
7423         spin_lock(&fp->fi_lock);
7424         list_add(&stp->st_locks, &open_stp->st_locks);
7425         list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
7426         list_add(&stp->st_perfile, &fp->fi_stateids);
7427         spin_unlock(&fp->fi_lock);
7428         spin_unlock(&clp->cl_lock);
7429         return stp;
7430 out_found:
7431         spin_unlock(&clp->cl_lock);
7432         if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
7433                 nfs4_put_stid(&retstp->st_stid);
7434                 goto retry;
7435         }
7436         /* To keep mutex tracking happy */
7437         mutex_unlock(&stp->st_mutex);
7438         return retstp;
7439 out_close:
7440         spin_unlock(&clp->cl_lock);
7441         mutex_unlock(&stp->st_mutex);
7442         return NULL;
7443 }
7444
7445 static struct nfs4_ol_stateid *
7446 find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
7447                             struct inode *inode, struct nfs4_ol_stateid *ost,
7448                             bool *new)
7449 {
7450         struct nfs4_stid *ns = NULL;
7451         struct nfs4_ol_stateid *lst;
7452         struct nfs4_openowner *oo = openowner(ost->st_stateowner);
7453         struct nfs4_client *clp = oo->oo_owner.so_client;
7454
7455         *new = false;
7456         spin_lock(&clp->cl_lock);
7457         lst = find_lock_stateid(lo, ost);
7458         spin_unlock(&clp->cl_lock);
7459         if (lst != NULL) {
7460                 if (nfsd4_lock_ol_stateid(lst) == nfs_ok)
7461                         goto out;
7462                 nfs4_put_stid(&lst->st_stid);
7463         }
7464         ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid);
7465         if (ns == NULL)
7466                 return NULL;
7467
7468         lst = init_lock_stateid(openlockstateid(ns), lo, fi, inode, ost);
7469         if (lst == openlockstateid(ns))
7470                 *new = true;
7471         else
7472                 nfs4_put_stid(ns);
7473 out:
7474         return lst;
7475 }
7476
7477 static int
7478 check_lock_length(u64 offset, u64 length)
7479 {
7480         return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
7481                 (length > ~offset)));
7482 }
7483
7484 static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
7485 {
7486         struct nfs4_file *fp = lock_stp->st_stid.sc_file;
7487
7488         lockdep_assert_held(&fp->fi_lock);
7489
7490         if (test_access(access, lock_stp))
7491                 return;
7492         __nfs4_file_get_access(fp, access);
7493         set_access(access, lock_stp);
7494 }
7495
7496 static __be32
7497 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
7498                             struct nfs4_ol_stateid *ost,
7499                             struct nfsd4_lock *lock,
7500                             struct nfs4_ol_stateid **plst, bool *new)
7501 {
7502         __be32 status;
7503         struct nfs4_file *fi = ost->st_stid.sc_file;
7504         struct nfs4_openowner *oo = openowner(ost->st_stateowner);
7505         struct nfs4_client *cl = oo->oo_owner.so_client;
7506         struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
7507         struct nfs4_lockowner *lo;
7508         struct nfs4_ol_stateid *lst;
7509         unsigned int strhashval;
7510
7511         lo = find_lockowner_str(cl, &lock->lk_new_owner);
7512         if (!lo) {
7513                 strhashval = ownerstr_hashval(&lock->lk_new_owner);
7514                 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
7515                 if (lo == NULL)
7516                         return nfserr_jukebox;
7517         } else {
7518                 /* with an existing lockowner, seqids must be the same */
7519                 status = nfserr_bad_seqid;
7520                 if (!cstate->minorversion &&
7521                     lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
7522                         goto out;
7523         }
7524
7525         lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
7526         if (lst == NULL) {
7527                 status = nfserr_jukebox;
7528                 goto out;
7529         }
7530
7531         status = nfs_ok;
7532         *plst = lst;
7533 out:
7534         nfs4_put_stateowner(&lo->lo_owner);
7535         return status;
7536 }
7537
7538 /*
7539  *  LOCK operation 
7540  */
7541 __be32
7542 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7543            union nfsd4_op_u *u)
7544 {
7545         struct nfsd4_lock *lock = &u->lock;
7546         struct nfs4_openowner *open_sop = NULL;
7547         struct nfs4_lockowner *lock_sop = NULL;
7548         struct nfs4_ol_stateid *lock_stp = NULL;
7549         struct nfs4_ol_stateid *open_stp = NULL;
7550         struct nfs4_file *fp;
7551         struct nfsd_file *nf = NULL;
7552         struct nfsd4_blocked_lock *nbl = NULL;
7553         struct file_lock *file_lock = NULL;
7554         struct file_lock *conflock = NULL;
7555         struct super_block *sb;
7556         __be32 status = 0;
7557         int lkflg;
7558         int err;
7559         bool new = false;
7560         unsigned char fl_type;
7561         unsigned int fl_flags = FL_POSIX;
7562         struct net *net = SVC_NET(rqstp);
7563         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7564
7565         dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
7566                 (long long) lock->lk_offset,
7567                 (long long) lock->lk_length);
7568
7569         if (check_lock_length(lock->lk_offset, lock->lk_length))
7570                  return nfserr_inval;
7571
7572         if ((status = fh_verify(rqstp, &cstate->current_fh,
7573                                 S_IFREG, NFSD_MAY_LOCK))) {
7574                 dprintk("NFSD: nfsd4_lock: permission denied!\n");
7575                 return status;
7576         }
7577         sb = cstate->current_fh.fh_dentry->d_sb;
7578
7579         if (lock->lk_is_new) {
7580                 if (nfsd4_has_session(cstate))
7581                         /* See rfc 5661 18.10.3: given clientid is ignored: */
7582                         memcpy(&lock->lk_new_clientid,
7583                                 &cstate->clp->cl_clientid,
7584                                 sizeof(clientid_t));
7585
7586                 /* validate and update open stateid and open seqid */
7587                 status = nfs4_preprocess_confirmed_seqid_op(cstate,
7588                                         lock->lk_new_open_seqid,
7589                                         &lock->lk_new_open_stateid,
7590                                         &open_stp, nn);
7591                 if (status)
7592                         goto out;
7593                 mutex_unlock(&open_stp->st_mutex);
7594                 open_sop = openowner(open_stp->st_stateowner);
7595                 status = nfserr_bad_stateid;
7596                 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
7597                                                 &lock->lk_new_clientid))
7598                         goto out;
7599                 status = lookup_or_create_lock_state(cstate, open_stp, lock,
7600                                                         &lock_stp, &new);
7601         } else {
7602                 status = nfs4_preprocess_seqid_op(cstate,
7603                                        lock->lk_old_lock_seqid,
7604                                        &lock->lk_old_lock_stateid,
7605                                        NFS4_LOCK_STID, &lock_stp, nn);
7606         }
7607         if (status)
7608                 goto out;
7609         lock_sop = lockowner(lock_stp->st_stateowner);
7610
7611         lkflg = setlkflg(lock->lk_type);
7612         status = nfs4_check_openmode(lock_stp, lkflg);
7613         if (status)
7614                 goto out;
7615
7616         status = nfserr_grace;
7617         if (locks_in_grace(net) && !lock->lk_reclaim)
7618                 goto out;
7619         status = nfserr_no_grace;
7620         if (!locks_in_grace(net) && lock->lk_reclaim)
7621                 goto out;
7622
7623         if (lock->lk_reclaim)
7624                 fl_flags |= FL_RECLAIM;
7625
7626         fp = lock_stp->st_stid.sc_file;
7627         switch (lock->lk_type) {
7628                 case NFS4_READW_LT:
7629                         if (nfsd4_has_session(cstate) ||
7630                             exportfs_lock_op_is_async(sb->s_export_op))
7631                                 fl_flags |= FL_SLEEP;
7632                         fallthrough;
7633                 case NFS4_READ_LT:
7634                         spin_lock(&fp->fi_lock);
7635                         nf = find_readable_file_locked(fp);
7636                         if (nf)
7637                                 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
7638                         spin_unlock(&fp->fi_lock);
7639                         fl_type = F_RDLCK;
7640                         break;
7641                 case NFS4_WRITEW_LT:
7642                         if (nfsd4_has_session(cstate) ||
7643                             exportfs_lock_op_is_async(sb->s_export_op))
7644                                 fl_flags |= FL_SLEEP;
7645                         fallthrough;
7646                 case NFS4_WRITE_LT:
7647                         spin_lock(&fp->fi_lock);
7648                         nf = find_writeable_file_locked(fp);
7649                         if (nf)
7650                                 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
7651                         spin_unlock(&fp->fi_lock);
7652                         fl_type = F_WRLCK;
7653                         break;
7654                 default:
7655                         status = nfserr_inval;
7656                 goto out;
7657         }
7658
7659         if (!nf) {
7660                 status = nfserr_openmode;
7661                 goto out;
7662         }
7663
7664         /*
7665          * Most filesystems with their own ->lock operations will block
7666          * the nfsd thread waiting to acquire the lock.  That leads to
7667          * deadlocks (we don't want every nfsd thread tied up waiting
7668          * for file locks), so don't attempt blocking lock notifications
7669          * on those filesystems:
7670          */
7671         if (!exportfs_lock_op_is_async(sb->s_export_op))
7672                 fl_flags &= ~FL_SLEEP;
7673
7674         nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
7675         if (!nbl) {
7676                 dprintk("NFSD: %s: unable to allocate block!\n", __func__);
7677                 status = nfserr_jukebox;
7678                 goto out;
7679         }
7680
7681         file_lock = &nbl->nbl_lock;
7682         file_lock->fl_type = fl_type;
7683         file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
7684         file_lock->fl_pid = current->tgid;
7685         file_lock->fl_file = nf->nf_file;
7686         file_lock->fl_flags = fl_flags;
7687         file_lock->fl_lmops = &nfsd_posix_mng_ops;
7688         file_lock->fl_start = lock->lk_offset;
7689         file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
7690         nfs4_transform_lock_offset(file_lock);
7691
7692         conflock = locks_alloc_lock();
7693         if (!conflock) {
7694                 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
7695                 status = nfserr_jukebox;
7696                 goto out;
7697         }
7698
7699         if (fl_flags & FL_SLEEP) {
7700                 nbl->nbl_time = ktime_get_boottime_seconds();
7701                 spin_lock(&nn->blocked_locks_lock);
7702                 list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
7703                 list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
7704                 kref_get(&nbl->nbl_kref);
7705                 spin_unlock(&nn->blocked_locks_lock);
7706         }
7707
7708         err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, conflock);
7709         switch (err) {
7710         case 0: /* success! */
7711                 nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
7712                 status = 0;
7713                 if (lock->lk_reclaim)
7714                         nn->somebody_reclaimed = true;
7715                 break;
7716         case FILE_LOCK_DEFERRED:
7717                 kref_put(&nbl->nbl_kref, free_nbl);
7718                 nbl = NULL;
7719                 fallthrough;
7720         case -EAGAIN:           /* conflock holds conflicting lock */
7721                 status = nfserr_denied;
7722                 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
7723                 nfs4_set_lock_denied(conflock, &lock->lk_denied);
7724                 break;
7725         case -EDEADLK:
7726                 status = nfserr_deadlock;
7727                 break;
7728         default:
7729                 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
7730                 status = nfserrno(err);
7731                 break;
7732         }
7733 out:
7734         if (nbl) {
7735                 /* dequeue it if we queued it before */
7736                 if (fl_flags & FL_SLEEP) {
7737                         spin_lock(&nn->blocked_locks_lock);
7738                         if (!list_empty(&nbl->nbl_list) &&
7739                             !list_empty(&nbl->nbl_lru)) {
7740                                 list_del_init(&nbl->nbl_list);
7741                                 list_del_init(&nbl->nbl_lru);
7742                                 kref_put(&nbl->nbl_kref, free_nbl);
7743                         }
7744                         /* nbl can use one of lists to be linked to reaplist */
7745                         spin_unlock(&nn->blocked_locks_lock);
7746                 }
7747                 free_blocked_lock(nbl);
7748         }
7749         if (nf)
7750                 nfsd_file_put(nf);
7751         if (lock_stp) {
7752                 /* Bump seqid manually if the 4.0 replay owner is openowner */
7753                 if (cstate->replay_owner &&
7754                     cstate->replay_owner != &lock_sop->lo_owner &&
7755                     seqid_mutating_err(ntohl(status)))
7756                         lock_sop->lo_owner.so_seqid++;
7757
7758                 /*
7759                  * If this is a new, never-before-used stateid, and we are
7760                  * returning an error, then just go ahead and release it.
7761                  */
7762                 if (status && new)
7763                         release_lock_stateid(lock_stp);
7764
7765                 mutex_unlock(&lock_stp->st_mutex);
7766
7767                 nfs4_put_stid(&lock_stp->st_stid);
7768         }
7769         if (open_stp)
7770                 nfs4_put_stid(&open_stp->st_stid);
7771         nfsd4_bump_seqid(cstate, status);
7772         if (conflock)
7773                 locks_free_lock(conflock);
7774         return status;
7775 }
7776
7777 void nfsd4_lock_release(union nfsd4_op_u *u)
7778 {
7779         struct nfsd4_lock *lock = &u->lock;
7780         struct nfsd4_lock_denied *deny = &lock->lk_denied;
7781
7782         kfree(deny->ld_owner.data);
7783 }
7784
7785 /*
7786  * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
7787  * so we do a temporary open here just to get an open file to pass to
7788  * vfs_test_lock.
7789  */
7790 static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
7791 {
7792         struct nfsd_file *nf;
7793         struct inode *inode;
7794         __be32 err;
7795
7796         err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf);
7797         if (err)
7798                 return err;
7799         inode = fhp->fh_dentry->d_inode;
7800         inode_lock(inode); /* to block new leases till after test_lock: */
7801         err = nfserrno(nfsd_open_break_lease(inode, NFSD_MAY_READ));
7802         if (err)
7803                 goto out;
7804         lock->fl_file = nf->nf_file;
7805         err = nfserrno(vfs_test_lock(nf->nf_file, lock));
7806         lock->fl_file = NULL;
7807 out:
7808         inode_unlock(inode);
7809         nfsd_file_put(nf);
7810         return err;
7811 }
7812
7813 /*
7814  * LOCKT operation
7815  */
7816 __be32
7817 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7818             union nfsd4_op_u *u)
7819 {
7820         struct nfsd4_lockt *lockt = &u->lockt;
7821         struct file_lock *file_lock = NULL;
7822         struct nfs4_lockowner *lo = NULL;
7823         __be32 status;
7824         struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
7825
7826         if (locks_in_grace(SVC_NET(rqstp)))
7827                 return nfserr_grace;
7828
7829         if (check_lock_length(lockt->lt_offset, lockt->lt_length))
7830                  return nfserr_inval;
7831
7832         if (!nfsd4_has_session(cstate)) {
7833                 status = set_client(&lockt->lt_clientid, cstate, nn);
7834                 if (status)
7835                         goto out;
7836         }
7837
7838         if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
7839                 goto out;
7840
7841         file_lock = locks_alloc_lock();
7842         if (!file_lock) {
7843                 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
7844                 status = nfserr_jukebox;
7845                 goto out;
7846         }
7847
7848         switch (lockt->lt_type) {
7849                 case NFS4_READ_LT:
7850                 case NFS4_READW_LT:
7851                         file_lock->fl_type = F_RDLCK;
7852                         break;
7853                 case NFS4_WRITE_LT:
7854                 case NFS4_WRITEW_LT:
7855                         file_lock->fl_type = F_WRLCK;
7856                         break;
7857                 default:
7858                         dprintk("NFSD: nfs4_lockt: bad lock type!\n");
7859                         status = nfserr_inval;
7860                         goto out;
7861         }
7862
7863         lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
7864         if (lo)
7865                 file_lock->fl_owner = (fl_owner_t)lo;
7866         file_lock->fl_pid = current->tgid;
7867         file_lock->fl_flags = FL_POSIX;
7868
7869         file_lock->fl_start = lockt->lt_offset;
7870         file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
7871
7872         nfs4_transform_lock_offset(file_lock);
7873
7874         status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
7875         if (status)
7876                 goto out;
7877
7878         if (file_lock->fl_type != F_UNLCK) {
7879                 status = nfserr_denied;
7880                 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
7881         }
7882 out:
7883         if (lo)
7884                 nfs4_put_stateowner(&lo->lo_owner);
7885         if (file_lock)
7886                 locks_free_lock(file_lock);
7887         return status;
7888 }
7889
7890 void nfsd4_lockt_release(union nfsd4_op_u *u)
7891 {
7892         struct nfsd4_lockt *lockt = &u->lockt;
7893         struct nfsd4_lock_denied *deny = &lockt->lt_denied;
7894
7895         kfree(deny->ld_owner.data);
7896 }
7897
7898 __be32
7899 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7900             union nfsd4_op_u *u)
7901 {
7902         struct nfsd4_locku *locku = &u->locku;
7903         struct nfs4_ol_stateid *stp;
7904         struct nfsd_file *nf = NULL;
7905         struct file_lock *file_lock = NULL;
7906         __be32 status;
7907         int err;
7908         struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
7909
7910         dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
7911                 (long long) locku->lu_offset,
7912                 (long long) locku->lu_length);
7913
7914         if (check_lock_length(locku->lu_offset, locku->lu_length))
7915                  return nfserr_inval;
7916
7917         status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
7918                                         &locku->lu_stateid, NFS4_LOCK_STID,
7919                                         &stp, nn);
7920         if (status)
7921                 goto out;
7922         nf = find_any_file(stp->st_stid.sc_file);
7923         if (!nf) {
7924                 status = nfserr_lock_range;
7925                 goto put_stateid;
7926         }
7927         file_lock = locks_alloc_lock();
7928         if (!file_lock) {
7929                 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
7930                 status = nfserr_jukebox;
7931                 goto put_file;
7932         }
7933
7934         file_lock->fl_type = F_UNLCK;
7935         file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
7936         file_lock->fl_pid = current->tgid;
7937         file_lock->fl_file = nf->nf_file;
7938         file_lock->fl_flags = FL_POSIX;
7939         file_lock->fl_lmops = &nfsd_posix_mng_ops;
7940         file_lock->fl_start = locku->lu_offset;
7941
7942         file_lock->fl_end = last_byte_offset(locku->lu_offset,
7943                                                 locku->lu_length);
7944         nfs4_transform_lock_offset(file_lock);
7945
7946         err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, NULL);
7947         if (err) {
7948                 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
7949                 goto out_nfserr;
7950         }
7951         nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
7952 put_file:
7953         nfsd_file_put(nf);
7954 put_stateid:
7955         mutex_unlock(&stp->st_mutex);
7956         nfs4_put_stid(&stp->st_stid);
7957 out:
7958         nfsd4_bump_seqid(cstate, status);
7959         if (file_lock)
7960                 locks_free_lock(file_lock);
7961         return status;
7962
7963 out_nfserr:
7964         status = nfserrno(err);
7965         goto put_file;
7966 }
7967
7968 /*
7969  * returns
7970  *      true:  locks held by lockowner
7971  *      false: no locks held by lockowner
7972  */
7973 static bool
7974 check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
7975 {
7976         struct file_lock *fl;
7977         int status = false;
7978         struct nfsd_file *nf = find_any_file(fp);
7979         struct inode *inode;
7980         struct file_lock_context *flctx;
7981
7982         if (!nf) {
7983                 /* Any valid lock stateid should have some sort of access */
7984                 WARN_ON_ONCE(1);
7985                 return status;
7986         }
7987
7988         inode = file_inode(nf->nf_file);
7989         flctx = locks_inode_context(inode);
7990
7991         if (flctx && !list_empty_careful(&flctx->flc_posix)) {
7992                 spin_lock(&flctx->flc_lock);
7993                 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
7994                         if (fl->fl_owner == (fl_owner_t)lowner) {
7995                                 status = true;
7996                                 break;
7997                         }
7998                 }
7999                 spin_unlock(&flctx->flc_lock);
8000         }
8001         nfsd_file_put(nf);
8002         return status;
8003 }
8004
8005 /**
8006  * nfsd4_release_lockowner - process NFSv4.0 RELEASE_LOCKOWNER operations
8007  * @rqstp: RPC transaction
8008  * @cstate: NFSv4 COMPOUND state
8009  * @u: RELEASE_LOCKOWNER arguments
8010  *
8011  * The lockowner's so_count is bumped when a lock record is added
8012  * or when copying a conflicting lock. The latter case is brief,
8013  * but can lead to fleeting false positives when looking for
8014  * locks-in-use.
8015  *
8016  * Return values:
8017  *   %nfs_ok: lockowner released or not found
8018  *   %nfserr_locks_held: lockowner still in use
8019  *   %nfserr_stale_clientid: clientid no longer active
8020  *   %nfserr_expired: clientid not recognized
8021  */
8022 __be32
8023 nfsd4_release_lockowner(struct svc_rqst *rqstp,
8024                         struct nfsd4_compound_state *cstate,
8025                         union nfsd4_op_u *u)
8026 {
8027         struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner;
8028         struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
8029         clientid_t *clid = &rlockowner->rl_clientid;
8030         struct nfs4_ol_stateid *stp;
8031         struct nfs4_lockowner *lo;
8032         struct nfs4_client *clp;
8033         LIST_HEAD(reaplist);
8034         __be32 status;
8035
8036         dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
8037                 clid->cl_boot, clid->cl_id);
8038
8039         status = set_client(clid, cstate, nn);
8040         if (status)
8041                 return status;
8042         clp = cstate->clp;
8043
8044         spin_lock(&clp->cl_lock);
8045         lo = find_lockowner_str_locked(clp, &rlockowner->rl_owner);
8046         if (!lo) {
8047                 spin_unlock(&clp->cl_lock);
8048                 return nfs_ok;
8049         }
8050         if (atomic_read(&lo->lo_owner.so_count) != 2) {
8051                 spin_unlock(&clp->cl_lock);
8052                 nfs4_put_stateowner(&lo->lo_owner);
8053                 return nfserr_locks_held;
8054         }
8055         unhash_lockowner_locked(lo);
8056         while (!list_empty(&lo->lo_owner.so_stateids)) {
8057                 stp = list_first_entry(&lo->lo_owner.so_stateids,
8058                                        struct nfs4_ol_stateid,
8059                                        st_perstateowner);
8060                 WARN_ON(!unhash_lock_stateid(stp));
8061                 put_ol_stateid_locked(stp, &reaplist);
8062         }
8063         spin_unlock(&clp->cl_lock);
8064
8065         free_ol_stateid_reaplist(&reaplist);
8066         remove_blocked_locks(lo);
8067         nfs4_put_stateowner(&lo->lo_owner);
8068         return nfs_ok;
8069 }
8070
8071 static inline struct nfs4_client_reclaim *
8072 alloc_reclaim(void)
8073 {
8074         return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
8075 }
8076
8077 bool
8078 nfs4_has_reclaimed_state(struct xdr_netobj name, struct nfsd_net *nn)
8079 {
8080         struct nfs4_client_reclaim *crp;
8081
8082         crp = nfsd4_find_reclaim_client(name, nn);
8083         return (crp && crp->cr_clp);
8084 }
8085
8086 /*
8087  * failure => all reset bets are off, nfserr_no_grace...
8088  *
8089  * The caller is responsible for freeing name.data if NULL is returned (it
8090  * will be freed in nfs4_remove_reclaim_record in the normal case).
8091  */
8092 struct nfs4_client_reclaim *
8093 nfs4_client_to_reclaim(struct xdr_netobj name, struct xdr_netobj princhash,
8094                 struct nfsd_net *nn)
8095 {
8096         unsigned int strhashval;
8097         struct nfs4_client_reclaim *crp;
8098
8099         crp = alloc_reclaim();
8100         if (crp) {
8101                 strhashval = clientstr_hashval(name);
8102                 INIT_LIST_HEAD(&crp->cr_strhash);
8103                 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
8104                 crp->cr_name.data = name.data;
8105                 crp->cr_name.len = name.len;
8106                 crp->cr_princhash.data = princhash.data;
8107                 crp->cr_princhash.len = princhash.len;
8108                 crp->cr_clp = NULL;
8109                 nn->reclaim_str_hashtbl_size++;
8110         }
8111         return crp;
8112 }
8113
8114 void
8115 nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
8116 {
8117         list_del(&crp->cr_strhash);
8118         kfree(crp->cr_name.data);
8119         kfree(crp->cr_princhash.data);
8120         kfree(crp);
8121         nn->reclaim_str_hashtbl_size--;
8122 }
8123
8124 void
8125 nfs4_release_reclaim(struct nfsd_net *nn)
8126 {
8127         struct nfs4_client_reclaim *crp = NULL;
8128         int i;
8129
8130         for (i = 0; i < CLIENT_HASH_SIZE; i++) {
8131                 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
8132                         crp = list_entry(nn->reclaim_str_hashtbl[i].next,
8133                                         struct nfs4_client_reclaim, cr_strhash);
8134                         nfs4_remove_reclaim_record(crp, nn);
8135                 }
8136         }
8137         WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
8138 }
8139
8140 /*
8141  * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
8142 struct nfs4_client_reclaim *
8143 nfsd4_find_reclaim_client(struct xdr_netobj name, struct nfsd_net *nn)
8144 {
8145         unsigned int strhashval;
8146         struct nfs4_client_reclaim *crp = NULL;
8147
8148         strhashval = clientstr_hashval(name);
8149         list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
8150                 if (compare_blob(&crp->cr_name, &name) == 0) {
8151                         return crp;
8152                 }
8153         }
8154         return NULL;
8155 }
8156
8157 __be32
8158 nfs4_check_open_reclaim(struct nfs4_client *clp)
8159 {
8160         if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &clp->cl_flags))
8161                 return nfserr_no_grace;
8162
8163         if (nfsd4_client_record_check(clp))
8164                 return nfserr_reclaim_bad;
8165
8166         return nfs_ok;
8167 }
8168
8169 /*
8170  * Since the lifetime of a delegation isn't limited to that of an open, a
8171  * client may quite reasonably hang on to a delegation as long as it has
8172  * the inode cached.  This becomes an obvious problem the first time a
8173  * client's inode cache approaches the size of the server's total memory.
8174  *
8175  * For now we avoid this problem by imposing a hard limit on the number
8176  * of delegations, which varies according to the server's memory size.
8177  */
8178 static void
8179 set_max_delegations(void)
8180 {
8181         /*
8182          * Allow at most 4 delegations per megabyte of RAM.  Quick
8183          * estimates suggest that in the worst case (where every delegation
8184          * is for a different inode), a delegation could take about 1.5K,
8185          * giving a worst case usage of about 6% of memory.
8186          */
8187         max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
8188 }
8189
8190 static int nfs4_state_create_net(struct net *net)
8191 {
8192         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
8193         int i;
8194
8195         nn->conf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
8196                                             sizeof(struct list_head),
8197                                             GFP_KERNEL);
8198         if (!nn->conf_id_hashtbl)
8199                 goto err;
8200         nn->unconf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
8201                                               sizeof(struct list_head),
8202                                               GFP_KERNEL);
8203         if (!nn->unconf_id_hashtbl)
8204                 goto err_unconf_id;
8205         nn->sessionid_hashtbl = kmalloc_array(SESSION_HASH_SIZE,
8206                                               sizeof(struct list_head),
8207                                               GFP_KERNEL);
8208         if (!nn->sessionid_hashtbl)
8209                 goto err_sessionid;
8210
8211         for (i = 0; i < CLIENT_HASH_SIZE; i++) {
8212                 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
8213                 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
8214         }
8215         for (i = 0; i < SESSION_HASH_SIZE; i++)
8216                 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
8217         nn->conf_name_tree = RB_ROOT;
8218         nn->unconf_name_tree = RB_ROOT;
8219         nn->boot_time = ktime_get_real_seconds();
8220         nn->grace_ended = false;
8221         nn->nfsd4_manager.block_opens = true;
8222         INIT_LIST_HEAD(&nn->nfsd4_manager.list);
8223         INIT_LIST_HEAD(&nn->client_lru);
8224         INIT_LIST_HEAD(&nn->close_lru);
8225         INIT_LIST_HEAD(&nn->del_recall_lru);
8226         spin_lock_init(&nn->client_lock);
8227         spin_lock_init(&nn->s2s_cp_lock);
8228         idr_init(&nn->s2s_cp_stateids);
8229
8230         spin_lock_init(&nn->blocked_locks_lock);
8231         INIT_LIST_HEAD(&nn->blocked_locks_lru);
8232
8233         INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
8234         INIT_WORK(&nn->nfsd_shrinker_work, nfsd4_state_shrinker_worker);
8235         get_net(net);
8236
8237         nn->nfsd_client_shrinker = shrinker_alloc(0, "nfsd-client");
8238         if (!nn->nfsd_client_shrinker)
8239                 goto err_shrinker;
8240
8241         nn->nfsd_client_shrinker->scan_objects = nfsd4_state_shrinker_scan;
8242         nn->nfsd_client_shrinker->count_objects = nfsd4_state_shrinker_count;
8243         nn->nfsd_client_shrinker->private_data = nn;
8244
8245         shrinker_register(nn->nfsd_client_shrinker);
8246
8247         return 0;
8248
8249 err_shrinker:
8250         put_net(net);
8251         kfree(nn->sessionid_hashtbl);
8252 err_sessionid:
8253         kfree(nn->unconf_id_hashtbl);
8254 err_unconf_id:
8255         kfree(nn->conf_id_hashtbl);
8256 err:
8257         return -ENOMEM;
8258 }
8259
8260 static void
8261 nfs4_state_destroy_net(struct net *net)
8262 {
8263         int i;
8264         struct nfs4_client *clp = NULL;
8265         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
8266
8267         for (i = 0; i < CLIENT_HASH_SIZE; i++) {
8268                 while (!list_empty(&nn->conf_id_hashtbl[i])) {
8269                         clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
8270                         destroy_client(clp);
8271                 }
8272         }
8273
8274         WARN_ON(!list_empty(&nn->blocked_locks_lru));
8275
8276         for (i = 0; i < CLIENT_HASH_SIZE; i++) {
8277                 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
8278                         clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
8279                         destroy_client(clp);
8280                 }
8281         }
8282
8283         kfree(nn->sessionid_hashtbl);
8284         kfree(nn->unconf_id_hashtbl);
8285         kfree(nn->conf_id_hashtbl);
8286         put_net(net);
8287 }
8288
8289 int
8290 nfs4_state_start_net(struct net *net)
8291 {
8292         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
8293         int ret;
8294
8295         ret = nfs4_state_create_net(net);
8296         if (ret)
8297                 return ret;
8298         locks_start_grace(net, &nn->nfsd4_manager);
8299         nfsd4_client_tracking_init(net);
8300         if (nn->track_reclaim_completes && nn->reclaim_str_hashtbl_size == 0)
8301                 goto skip_grace;
8302         printk(KERN_INFO "NFSD: starting %lld-second grace period (net %x)\n",
8303                nn->nfsd4_grace, net->ns.inum);
8304         trace_nfsd_grace_start(nn);
8305         queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
8306         return 0;
8307
8308 skip_grace:
8309         printk(KERN_INFO "NFSD: no clients to reclaim, skipping NFSv4 grace period (net %x)\n",
8310                         net->ns.inum);
8311         queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_lease * HZ);
8312         nfsd4_end_grace(nn);
8313         return 0;
8314 }
8315
8316 /* initialization to perform when the nfsd service is started: */
8317
8318 int
8319 nfs4_state_start(void)
8320 {
8321         int ret;
8322
8323         ret = rhltable_init(&nfs4_file_rhltable, &nfs4_file_rhash_params);
8324         if (ret)
8325                 return ret;
8326
8327         ret = nfsd4_create_callback_queue();
8328         if (ret) {
8329                 rhltable_destroy(&nfs4_file_rhltable);
8330                 return ret;
8331         }
8332
8333         set_max_delegations();
8334         return 0;
8335 }
8336
8337 void
8338 nfs4_state_shutdown_net(struct net *net)
8339 {
8340         struct nfs4_delegation *dp = NULL;
8341         struct list_head *pos, *next, reaplist;
8342         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
8343
8344         shrinker_free(nn->nfsd_client_shrinker);
8345         cancel_work(&nn->nfsd_shrinker_work);
8346         cancel_delayed_work_sync(&nn->laundromat_work);
8347         locks_end_grace(&nn->nfsd4_manager);
8348
8349         INIT_LIST_HEAD(&reaplist);
8350         spin_lock(&state_lock);
8351         list_for_each_safe(pos, next, &nn->del_recall_lru) {
8352                 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
8353                 WARN_ON(!unhash_delegation_locked(dp));
8354                 list_add(&dp->dl_recall_lru, &reaplist);
8355         }
8356         spin_unlock(&state_lock);
8357         list_for_each_safe(pos, next, &reaplist) {
8358                 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
8359                 list_del_init(&dp->dl_recall_lru);
8360                 destroy_unhashed_deleg(dp);
8361         }
8362
8363         nfsd4_client_tracking_exit(net);
8364         nfs4_state_destroy_net(net);
8365 #ifdef CONFIG_NFSD_V4_2_INTER_SSC
8366         nfsd4_ssc_shutdown_umount(nn);
8367 #endif
8368 }
8369
8370 void
8371 nfs4_state_shutdown(void)
8372 {
8373         nfsd4_destroy_callback_queue();
8374         rhltable_destroy(&nfs4_file_rhltable);
8375 }
8376
8377 static void
8378 get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
8379 {
8380         if (HAS_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG) &&
8381             CURRENT_STATEID(stateid))
8382                 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
8383 }
8384
8385 static void
8386 put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
8387 {
8388         if (cstate->minorversion) {
8389                 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
8390                 SET_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
8391         }
8392 }
8393
8394 void
8395 clear_current_stateid(struct nfsd4_compound_state *cstate)
8396 {
8397         CLEAR_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
8398 }
8399
8400 /*
8401  * functions to set current state id
8402  */
8403 void
8404 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate,
8405                 union nfsd4_op_u *u)
8406 {
8407         put_stateid(cstate, &u->open_downgrade.od_stateid);
8408 }
8409
8410 void
8411 nfsd4_set_openstateid(struct nfsd4_compound_state *cstate,
8412                 union nfsd4_op_u *u)
8413 {
8414         put_stateid(cstate, &u->open.op_stateid);
8415 }
8416
8417 void
8418 nfsd4_set_closestateid(struct nfsd4_compound_state *cstate,
8419                 union nfsd4_op_u *u)
8420 {
8421         put_stateid(cstate, &u->close.cl_stateid);
8422 }
8423
8424 void
8425 nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate,
8426                 union nfsd4_op_u *u)
8427 {
8428         put_stateid(cstate, &u->lock.lk_resp_stateid);
8429 }
8430
8431 /*
8432  * functions to consume current state id
8433  */
8434
8435 void
8436 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate,
8437                 union nfsd4_op_u *u)
8438 {
8439         get_stateid(cstate, &u->open_downgrade.od_stateid);
8440 }
8441
8442 void
8443 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate,
8444                 union nfsd4_op_u *u)
8445 {
8446         get_stateid(cstate, &u->delegreturn.dr_stateid);
8447 }
8448
8449 void
8450 nfsd4_get_freestateid(struct nfsd4_compound_state *cstate,
8451                 union nfsd4_op_u *u)
8452 {
8453         get_stateid(cstate, &u->free_stateid.fr_stateid);
8454 }
8455
8456 void
8457 nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate,
8458                 union nfsd4_op_u *u)
8459 {
8460         get_stateid(cstate, &u->setattr.sa_stateid);
8461 }
8462
8463 void
8464 nfsd4_get_closestateid(struct nfsd4_compound_state *cstate,
8465                 union nfsd4_op_u *u)
8466 {
8467         get_stateid(cstate, &u->close.cl_stateid);
8468 }
8469
8470 void
8471 nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate,
8472                 union nfsd4_op_u *u)
8473 {
8474         get_stateid(cstate, &u->locku.lu_stateid);
8475 }
8476
8477 void
8478 nfsd4_get_readstateid(struct nfsd4_compound_state *cstate,
8479                 union nfsd4_op_u *u)
8480 {
8481         get_stateid(cstate, &u->read.rd_stateid);
8482 }
8483
8484 void
8485 nfsd4_get_writestateid(struct nfsd4_compound_state *cstate,
8486                 union nfsd4_op_u *u)
8487 {
8488         get_stateid(cstate, &u->write.wr_stateid);
8489 }
8490
8491 /**
8492  * nfsd4_deleg_getattr_conflict - Recall if GETATTR causes conflict
8493  * @rqstp: RPC transaction context
8494  * @inode: file to be checked for a conflict
8495  * @modified: return true if file was modified
8496  * @size: new size of file if modified is true
8497  *
8498  * This function is called when there is a conflict between a write
8499  * delegation and a change/size GETATTR from another client. The server
8500  * must either use the CB_GETATTR to get the current values of the
8501  * attributes from the client that holds the delegation or recall the
8502  * delegation before replying to the GETATTR. See RFC 8881 section
8503  * 18.7.4.
8504  *
8505  * Returns 0 if there is no conflict; otherwise an nfs_stat
8506  * code is returned.
8507  */
8508 __be32
8509 nfsd4_deleg_getattr_conflict(struct svc_rqst *rqstp, struct inode *inode,
8510                              bool *modified, u64 *size)
8511 {
8512         struct file_lock_context *ctx;
8513         struct nfs4_delegation *dp;
8514         struct nfs4_cb_fattr *ncf;
8515         struct file_lock *fl;
8516         struct iattr attrs;
8517         __be32 status;
8518
8519         might_sleep();
8520
8521         *modified = false;
8522         ctx = locks_inode_context(inode);
8523         if (!ctx)
8524                 return 0;
8525         spin_lock(&ctx->flc_lock);
8526         list_for_each_entry(fl, &ctx->flc_lease, fl_list) {
8527                 if (fl->fl_flags == FL_LAYOUT)
8528                         continue;
8529                 if (fl->fl_lmops != &nfsd_lease_mng_ops) {
8530                         /*
8531                          * non-nfs lease, if it's a lease with F_RDLCK then
8532                          * we are done; there isn't any write delegation
8533                          * on this inode
8534                          */
8535                         if (fl->fl_type == F_RDLCK)
8536                                 break;
8537                         goto break_lease;
8538                 }
8539                 if (fl->fl_type == F_WRLCK) {
8540                         dp = fl->fl_owner;
8541                         if (dp->dl_recall.cb_clp == *(rqstp->rq_lease_breaker)) {
8542                                 spin_unlock(&ctx->flc_lock);
8543                                 return 0;
8544                         }
8545 break_lease:
8546                         spin_unlock(&ctx->flc_lock);
8547                         nfsd_stats_wdeleg_getattr_inc();
8548
8549                         dp = fl->fl_owner;
8550                         ncf = &dp->dl_cb_fattr;
8551                         nfs4_cb_getattr(&dp->dl_cb_fattr);
8552                         wait_on_bit(&ncf->ncf_cb_flags, CB_GETATTR_BUSY, TASK_INTERRUPTIBLE);
8553                         if (ncf->ncf_cb_status) {
8554                                 status = nfserrno(nfsd_open_break_lease(inode, NFSD_MAY_READ));
8555                                 if (status != nfserr_jukebox ||
8556                                                 !nfsd_wait_for_delegreturn(rqstp, inode))
8557                                         return status;
8558                         }
8559                         if (!ncf->ncf_file_modified &&
8560                                         (ncf->ncf_initial_cinfo != ncf->ncf_cb_change ||
8561                                         ncf->ncf_cur_fsize != ncf->ncf_cb_fsize))
8562                                 ncf->ncf_file_modified = true;
8563                         if (ncf->ncf_file_modified) {
8564                                 /*
8565                                  * The server would not update the file's metadata
8566                                  * with the client's modified size.
8567                                  */
8568                                 attrs.ia_mtime = attrs.ia_ctime = current_time(inode);
8569                                 attrs.ia_valid = ATTR_MTIME | ATTR_CTIME;
8570                                 setattr_copy(&nop_mnt_idmap, inode, &attrs);
8571                                 mark_inode_dirty(inode);
8572                                 ncf->ncf_cur_fsize = ncf->ncf_cb_fsize;
8573                                 *size = ncf->ncf_cur_fsize;
8574                                 *modified = true;
8575                         }
8576                         return 0;
8577                 }
8578                 break;
8579         }
8580         spin_unlock(&ctx->flc_lock);
8581         return 0;
8582 }