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