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