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