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