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