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