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