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