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