nfsd: eliminate nfsd4_init_callback
[firefly-linux-kernel-4.4.55.git] / fs / nfsd / nfs4state.c
1 /*
2 *  Copyright (c) 2001 The Regents of the University of Michigan.
3 *  All rights reserved.
4 *
5 *  Kendrick Smith <kmsmith@umich.edu>
6 *  Andy Adamson <kandros@umich.edu>
7 *
8 *  Redistribution and use in source and binary forms, with or without
9 *  modification, are permitted provided that the following conditions
10 *  are met:
11 *
12 *  1. Redistributions of source code must retain the above copyright
13 *     notice, this list of conditions and the following disclaimer.
14 *  2. Redistributions in binary form must reproduce the above copyright
15 *     notice, this list of conditions and the following disclaimer in the
16 *     documentation and/or other materials provided with the distribution.
17 *  3. Neither the name of the University nor the names of its
18 *     contributors may be used to endorse or promote products derived
19 *     from this software without specific prior written permission.
20 *
21 *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22 *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 */
34
35 #include <linux/file.h>
36 #include <linux/fs.h>
37 #include <linux/slab.h>
38 #include <linux/namei.h>
39 #include <linux/swap.h>
40 #include <linux/pagemap.h>
41 #include <linux/ratelimit.h>
42 #include <linux/sunrpc/svcauth_gss.h>
43 #include <linux/sunrpc/addr.h>
44 #include <linux/hash.h>
45 #include "xdr4.h"
46 #include "xdr4cb.h"
47 #include "vfs.h"
48 #include "current_stateid.h"
49
50 #include "netns.h"
51
52 #define NFSDDBG_FACILITY                NFSDDBG_PROC
53
54 #define all_ones {{~0,~0},~0}
55 static const stateid_t one_stateid = {
56         .si_generation = ~0,
57         .si_opaque = all_ones,
58 };
59 static const stateid_t zero_stateid = {
60         /* all fields zero */
61 };
62 static const stateid_t currentstateid = {
63         .si_generation = 1,
64 };
65
66 static u64 current_sessionid = 1;
67
68 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
69 #define ONE_STATEID(stateid)  (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
70 #define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
71
72 /* forward declarations */
73 static 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         INIT_WORK(&dp->dl_recall.cb_work, nfsd4_do_callback_rpc);
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         INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_do_callback_rpc);
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 struct nfs4_ol_stateid *
3301 nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
3302 {
3303         struct nfs4_ol_stateid *local, *ret = NULL;
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                 if (local->st_stateowner == &oo->oo_owner) {
3312                         ret = local;
3313                         break;
3314                 }
3315         }
3316         spin_unlock(&fp->fi_lock);
3317         return ret;
3318 }
3319
3320 static inline int nfs4_access_to_access(u32 nfs4_access)
3321 {
3322         int flags = 0;
3323
3324         if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3325                 flags |= NFSD_MAY_READ;
3326         if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3327                 flags |= NFSD_MAY_WRITE;
3328         return flags;
3329 }
3330
3331 static inline __be32
3332 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3333                 struct nfsd4_open *open)
3334 {
3335         struct iattr iattr = {
3336                 .ia_valid = ATTR_SIZE,
3337                 .ia_size = 0,
3338         };
3339         if (!open->op_truncate)
3340                 return 0;
3341         if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
3342                 return nfserr_inval;
3343         return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3344 }
3345
3346 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
3347                 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3348                 struct nfsd4_open *open)
3349 {
3350         struct file *filp = NULL;
3351         __be32 status;
3352         int oflag = nfs4_access_to_omode(open->op_share_access);
3353         int access = nfs4_access_to_access(open->op_share_access);
3354         unsigned char old_access_bmap, old_deny_bmap;
3355
3356         spin_lock(&fp->fi_lock);
3357
3358         /*
3359          * Are we trying to set a deny mode that would conflict with
3360          * current access?
3361          */
3362         status = nfs4_file_check_deny(fp, open->op_share_deny);
3363         if (status != nfs_ok) {
3364                 spin_unlock(&fp->fi_lock);
3365                 goto out;
3366         }
3367
3368         /* set access to the file */
3369         status = nfs4_file_get_access(fp, open->op_share_access);
3370         if (status != nfs_ok) {
3371                 spin_unlock(&fp->fi_lock);
3372                 goto out;
3373         }
3374
3375         /* Set access bits in stateid */
3376         old_access_bmap = stp->st_access_bmap;
3377         set_access(open->op_share_access, stp);
3378
3379         /* Set new deny mask */
3380         old_deny_bmap = stp->st_deny_bmap;
3381         set_deny(open->op_share_deny, stp);
3382         fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3383
3384         if (!fp->fi_fds[oflag]) {
3385                 spin_unlock(&fp->fi_lock);
3386                 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
3387                 if (status)
3388                         goto out_put_access;
3389                 spin_lock(&fp->fi_lock);
3390                 if (!fp->fi_fds[oflag]) {
3391                         fp->fi_fds[oflag] = filp;
3392                         filp = NULL;
3393                 }
3394         }
3395         spin_unlock(&fp->fi_lock);
3396         if (filp)
3397                 fput(filp);
3398
3399         status = nfsd4_truncate(rqstp, cur_fh, open);
3400         if (status)
3401                 goto out_put_access;
3402 out:
3403         return status;
3404 out_put_access:
3405         stp->st_access_bmap = old_access_bmap;
3406         nfs4_file_put_access(fp, open->op_share_access);
3407         reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3408         goto out;
3409 }
3410
3411 static __be32
3412 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)
3413 {
3414         __be32 status;
3415         unsigned char old_deny_bmap;
3416
3417         if (!test_access(open->op_share_access, stp))
3418                 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
3419
3420         /* test and set deny mode */
3421         spin_lock(&fp->fi_lock);
3422         status = nfs4_file_check_deny(fp, open->op_share_deny);
3423         if (status == nfs_ok) {
3424                 old_deny_bmap = stp->st_deny_bmap;
3425                 set_deny(open->op_share_deny, stp);
3426                 fp->fi_share_deny |=
3427                                 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3428         }
3429         spin_unlock(&fp->fi_lock);
3430
3431         if (status != nfs_ok)
3432                 return status;
3433
3434         status = nfsd4_truncate(rqstp, cur_fh, open);
3435         if (status != nfs_ok)
3436                 reset_union_bmap_deny(old_deny_bmap, stp);
3437         return status;
3438 }
3439
3440 static void
3441 nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
3442 {
3443         open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
3444 }
3445
3446 /* Should we give out recallable state?: */
3447 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3448 {
3449         if (clp->cl_cb_state == NFSD4_CB_UP)
3450                 return true;
3451         /*
3452          * In the sessions case, since we don't have to establish a
3453          * separate connection for callbacks, we assume it's OK
3454          * until we hear otherwise:
3455          */
3456         return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3457 }
3458
3459 static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp, int flag)
3460 {
3461         struct file_lock *fl;
3462
3463         fl = locks_alloc_lock();
3464         if (!fl)
3465                 return NULL;
3466         locks_init_lock(fl);
3467         fl->fl_lmops = &nfsd_lease_mng_ops;
3468         fl->fl_flags = FL_DELEG;
3469         fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3470         fl->fl_end = OFFSET_MAX;
3471         fl->fl_owner = (fl_owner_t)(dp->dl_file);
3472         fl->fl_pid = current->tgid;
3473         return fl;
3474 }
3475
3476 static int nfs4_setlease(struct nfs4_delegation *dp)
3477 {
3478         struct nfs4_file *fp = dp->dl_file;
3479         struct file_lock *fl;
3480         int status;
3481
3482         fl = nfs4_alloc_init_lease(dp, NFS4_OPEN_DELEGATE_READ);
3483         if (!fl)
3484                 return -ENOMEM;
3485         fl->fl_file = find_readable_file(fp);
3486         status = vfs_setlease(fl->fl_file, fl->fl_type, &fl);
3487         if (status)
3488                 goto out_free;
3489         fp->fi_lease = fl;
3490         fp->fi_deleg_file = fl->fl_file;
3491         atomic_set(&fp->fi_delegees, 1);
3492         spin_lock(&state_lock);
3493         hash_delegation_locked(dp, fp);
3494         spin_unlock(&state_lock);
3495         return 0;
3496 out_free:
3497         if (fl->fl_file)
3498                 fput(fl->fl_file);
3499         locks_free_lock(fl);
3500         return status;
3501 }
3502
3503 static int nfs4_set_delegation(struct nfs4_delegation *dp, struct nfs4_file *fp)
3504 {
3505         if (fp->fi_had_conflict)
3506                 return -EAGAIN;
3507         get_nfs4_file(fp);
3508         dp->dl_file = fp;
3509         if (!fp->fi_lease)
3510                 return nfs4_setlease(dp);
3511         spin_lock(&state_lock);
3512         atomic_inc(&fp->fi_delegees);
3513         if (fp->fi_had_conflict) {
3514                 spin_unlock(&state_lock);
3515                 return -EAGAIN;
3516         }
3517         hash_delegation_locked(dp, fp);
3518         spin_unlock(&state_lock);
3519         return 0;
3520 }
3521
3522 static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3523 {
3524         open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3525         if (status == -EAGAIN)
3526                 open->op_why_no_deleg = WND4_CONTENTION;
3527         else {
3528                 open->op_why_no_deleg = WND4_RESOURCE;
3529                 switch (open->op_deleg_want) {
3530                 case NFS4_SHARE_WANT_READ_DELEG:
3531                 case NFS4_SHARE_WANT_WRITE_DELEG:
3532                 case NFS4_SHARE_WANT_ANY_DELEG:
3533                         break;
3534                 case NFS4_SHARE_WANT_CANCEL:
3535                         open->op_why_no_deleg = WND4_CANCELLED;
3536                         break;
3537                 case NFS4_SHARE_WANT_NO_DELEG:
3538                         WARN_ON_ONCE(1);
3539                 }
3540         }
3541 }
3542
3543 /*
3544  * Attempt to hand out a delegation.
3545  *
3546  * Note we don't support write delegations, and won't until the vfs has
3547  * proper support for them.
3548  */
3549 static void
3550 nfs4_open_delegation(struct net *net, struct svc_fh *fh,
3551                      struct nfsd4_open *open, struct nfs4_ol_stateid *stp)
3552 {
3553         struct nfs4_delegation *dp;
3554         struct nfs4_openowner *oo = container_of(stp->st_stateowner, struct nfs4_openowner, oo_owner);
3555         int cb_up;
3556         int status = 0;
3557
3558         cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
3559         open->op_recall = 0;
3560         switch (open->op_claim_type) {
3561                 case NFS4_OPEN_CLAIM_PREVIOUS:
3562                         if (!cb_up)
3563                                 open->op_recall = 1;
3564                         if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3565                                 goto out_no_deleg;
3566                         break;
3567                 case NFS4_OPEN_CLAIM_NULL:
3568                 case NFS4_OPEN_CLAIM_FH:
3569                         /*
3570                          * Let's not give out any delegations till everyone's
3571                          * had the chance to reclaim theirs....
3572                          */
3573                         if (locks_in_grace(net))
3574                                 goto out_no_deleg;
3575                         if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
3576                                 goto out_no_deleg;
3577                         /*
3578                          * Also, if the file was opened for write or
3579                          * create, there's a good chance the client's
3580                          * about to write to it, resulting in an
3581                          * immediate recall (since we don't support
3582                          * write delegations):
3583                          */
3584                         if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
3585                                 goto out_no_deleg;
3586                         if (open->op_create == NFS4_OPEN_CREATE)
3587                                 goto out_no_deleg;
3588                         break;
3589                 default:
3590                         goto out_no_deleg;
3591         }
3592         dp = alloc_init_deleg(oo->oo_owner.so_client, stp, fh);
3593         if (dp == NULL)
3594                 goto out_no_deleg;
3595         status = nfs4_set_delegation(dp, stp->st_file);
3596         if (status)
3597                 goto out_free;
3598
3599         memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
3600
3601         dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
3602                 STATEID_VAL(&dp->dl_stid.sc_stateid));
3603         open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
3604         return;
3605 out_free:
3606         destroy_delegation(dp);
3607 out_no_deleg:
3608         open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3609         if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
3610             open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
3611                 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
3612                 open->op_recall = 1;
3613         }
3614
3615         /* 4.1 client asking for a delegation? */
3616         if (open->op_deleg_want)
3617                 nfsd4_open_deleg_none_ext(open, status);
3618         return;
3619 }
3620
3621 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3622                                         struct nfs4_delegation *dp)
3623 {
3624         if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3625             dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3626                 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3627                 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3628         } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3629                    dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3630                 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3631                 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3632         }
3633         /* Otherwise the client must be confused wanting a delegation
3634          * it already has, therefore we don't return
3635          * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3636          */
3637 }
3638
3639 /*
3640  * called with nfs4_lock_state() held.
3641  */
3642 __be32
3643 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3644 {
3645         struct nfsd4_compoundres *resp = rqstp->rq_resp;
3646         struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
3647         struct nfs4_file *fp = NULL;
3648         struct inode *ino = current_fh->fh_dentry->d_inode;
3649         struct nfs4_ol_stateid *stp = NULL;
3650         struct nfs4_delegation *dp = NULL;
3651         __be32 status;
3652
3653         /*
3654          * Lookup file; if found, lookup stateid and check open request,
3655          * and check for delegations in the process of being recalled.
3656          * If not found, create the nfs4_file struct
3657          */
3658         fp = find_or_add_file(ino, open->op_file);
3659         if (fp != open->op_file) {
3660                 status = nfs4_check_deleg(cl, open, &dp);
3661                 if (status)
3662                         goto out;
3663                 stp = nfsd4_find_existing_open(fp, open);
3664         } else {
3665                 open->op_file = NULL;
3666                 status = nfserr_bad_stateid;
3667                 if (nfsd4_is_deleg_cur(open))
3668                         goto out;
3669                 status = nfserr_jukebox;
3670         }
3671
3672         /*
3673          * OPEN the file, or upgrade an existing OPEN.
3674          * If truncate fails, the OPEN fails.
3675          */
3676         if (stp) {
3677                 /* Stateid was found, this is an OPEN upgrade */
3678                 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
3679                 if (status)
3680                         goto out;
3681         } else {
3682                 stp = open->op_stp;
3683                 open->op_stp = NULL;
3684                 init_open_stateid(stp, fp, open);
3685                 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
3686                 if (status) {
3687                         release_open_stateid(stp);
3688                         goto out;
3689                 }
3690         }
3691         update_stateid(&stp->st_stid.sc_stateid);
3692         memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
3693
3694         if (nfsd4_has_session(&resp->cstate)) {
3695                 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
3696                         open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3697                         open->op_why_no_deleg = WND4_NOT_WANTED;
3698                         goto nodeleg;
3699                 }
3700         }
3701
3702         /*
3703         * Attempt to hand out a delegation. No error return, because the
3704         * OPEN succeeds even if we fail.
3705         */
3706         nfs4_open_delegation(SVC_NET(rqstp), current_fh, open, stp);
3707 nodeleg:
3708         status = nfs_ok;
3709
3710         dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
3711                 STATEID_VAL(&stp->st_stid.sc_stateid));
3712 out:
3713         /* 4.1 client trying to upgrade/downgrade delegation? */
3714         if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
3715             open->op_deleg_want)
3716                 nfsd4_deleg_xgrade_none_ext(open, dp);
3717
3718         if (fp)
3719                 put_nfs4_file(fp);
3720         if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
3721                 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
3722         /*
3723         * To finish the open response, we just need to set the rflags.
3724         */
3725         open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
3726         if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
3727             !nfsd4_has_session(&resp->cstate))
3728                 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
3729
3730         return status;
3731 }
3732
3733 void nfsd4_cleanup_open_state(struct nfsd4_open *open, __be32 status)
3734 {
3735         if (open->op_openowner) {
3736                 struct nfs4_openowner *oo = open->op_openowner;
3737
3738                 if (!list_empty(&oo->oo_owner.so_stateids))
3739                         list_del_init(&oo->oo_close_lru);
3740                 if (oo->oo_flags & NFS4_OO_NEW) {
3741                         if (status) {
3742                                 release_openowner(oo);
3743                                 open->op_openowner = NULL;
3744                         } else
3745                                 oo->oo_flags &= ~NFS4_OO_NEW;
3746                 }
3747         }
3748         if (open->op_file)
3749                 nfsd4_free_file(open->op_file);
3750         if (open->op_stp)
3751                 free_generic_stateid(open->op_stp);
3752 }
3753
3754 __be32
3755 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3756             clientid_t *clid)
3757 {
3758         struct nfs4_client *clp;
3759         __be32 status;
3760         struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3761
3762         nfs4_lock_state();
3763         dprintk("process_renew(%08x/%08x): starting\n", 
3764                         clid->cl_boot, clid->cl_id);
3765         status = lookup_clientid(clid, cstate, nn);
3766         if (status)
3767                 goto out;
3768         clp = cstate->clp;
3769         status = nfserr_cb_path_down;
3770         if (!list_empty(&clp->cl_delegations)
3771                         && clp->cl_cb_state != NFSD4_CB_UP)
3772                 goto out;
3773         status = nfs_ok;
3774 out:
3775         nfs4_unlock_state();
3776         return status;
3777 }
3778
3779 static void
3780 nfsd4_end_grace(struct nfsd_net *nn)
3781 {
3782         /* do nothing if grace period already ended */
3783         if (nn->grace_ended)
3784                 return;
3785
3786         dprintk("NFSD: end of grace period\n");
3787         nn->grace_ended = true;
3788         nfsd4_record_grace_done(nn, nn->boot_time);
3789         locks_end_grace(&nn->nfsd4_manager);
3790         /*
3791          * Now that every NFSv4 client has had the chance to recover and
3792          * to see the (possibly new, possibly shorter) lease time, we
3793          * can safely set the next grace time to the current lease time:
3794          */
3795         nn->nfsd4_grace = nn->nfsd4_lease;
3796 }
3797
3798 static time_t
3799 nfs4_laundromat(struct nfsd_net *nn)
3800 {
3801         struct nfs4_client *clp;
3802         struct nfs4_openowner *oo;
3803         struct nfs4_delegation *dp;
3804         struct list_head *pos, *next, reaplist;
3805         time_t cutoff = get_seconds() - nn->nfsd4_lease;
3806         time_t t, new_timeo = nn->nfsd4_lease;
3807
3808         nfs4_lock_state();
3809
3810         dprintk("NFSD: laundromat service - starting\n");
3811         nfsd4_end_grace(nn);
3812         INIT_LIST_HEAD(&reaplist);
3813         spin_lock(&nn->client_lock);
3814         list_for_each_safe(pos, next, &nn->client_lru) {
3815                 clp = list_entry(pos, struct nfs4_client, cl_lru);
3816                 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
3817                         t = clp->cl_time - cutoff;
3818                         new_timeo = min(new_timeo, t);
3819                         break;
3820                 }
3821                 if (mark_client_expired_locked(clp)) {
3822                         dprintk("NFSD: client in use (clientid %08x)\n",
3823                                 clp->cl_clientid.cl_id);
3824                         continue;
3825                 }
3826                 list_move(&clp->cl_lru, &reaplist);
3827         }
3828         spin_unlock(&nn->client_lock);
3829         list_for_each_safe(pos, next, &reaplist) {
3830                 clp = list_entry(pos, struct nfs4_client, cl_lru);
3831                 dprintk("NFSD: purging unused client (clientid %08x)\n",
3832                         clp->cl_clientid.cl_id);
3833                 expire_client(clp);
3834         }
3835         spin_lock(&state_lock);
3836         list_for_each_safe(pos, next, &nn->del_recall_lru) {
3837                 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3838                 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
3839                         continue;
3840                 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
3841                         t = dp->dl_time - cutoff;
3842                         new_timeo = min(new_timeo, t);
3843                         break;
3844                 }
3845                 list_move(&dp->dl_recall_lru, &reaplist);
3846         }
3847         spin_unlock(&state_lock);
3848         list_for_each_safe(pos, next, &reaplist) {
3849                 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3850                 revoke_delegation(dp);
3851         }
3852         list_for_each_safe(pos, next, &nn->close_lru) {
3853                 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
3854                 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
3855                         t = oo->oo_time - cutoff;
3856                         new_timeo = min(new_timeo, t);
3857                         break;
3858                 }
3859                 release_openowner(oo);
3860         }
3861         new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
3862         nfs4_unlock_state();
3863         return new_timeo;
3864 }
3865
3866 static struct workqueue_struct *laundry_wq;
3867 static void laundromat_main(struct work_struct *);
3868
3869 static void
3870 laundromat_main(struct work_struct *laundry)
3871 {
3872         time_t t;
3873         struct delayed_work *dwork = container_of(laundry, struct delayed_work,
3874                                                   work);
3875         struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
3876                                            laundromat_work);
3877
3878         t = nfs4_laundromat(nn);
3879         dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
3880         queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
3881 }
3882
3883 static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
3884 {
3885         if (fhp->fh_dentry->d_inode != stp->st_file->fi_inode)
3886                 return nfserr_bad_stateid;
3887         return nfs_ok;
3888 }
3889
3890 static inline int
3891 access_permit_read(struct nfs4_ol_stateid *stp)
3892 {
3893         return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
3894                 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
3895                 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
3896 }
3897
3898 static inline int
3899 access_permit_write(struct nfs4_ol_stateid *stp)
3900 {
3901         return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
3902                 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
3903 }
3904
3905 static
3906 __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
3907 {
3908         __be32 status = nfserr_openmode;
3909
3910         /* For lock stateid's, we test the parent open, not the lock: */
3911         if (stp->st_openstp)
3912                 stp = stp->st_openstp;
3913         if ((flags & WR_STATE) && !access_permit_write(stp))
3914                 goto out;
3915         if ((flags & RD_STATE) && !access_permit_read(stp))
3916                 goto out;
3917         status = nfs_ok;
3918 out:
3919         return status;
3920 }
3921
3922 static inline __be32
3923 check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
3924 {
3925         if (ONE_STATEID(stateid) && (flags & RD_STATE))
3926                 return nfs_ok;
3927         else if (locks_in_grace(net)) {
3928                 /* Answer in remaining cases depends on existence of
3929                  * conflicting state; so we must wait out the grace period. */
3930                 return nfserr_grace;
3931         } else if (flags & WR_STATE)
3932                 return nfs4_share_conflict(current_fh,
3933                                 NFS4_SHARE_DENY_WRITE);
3934         else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
3935                 return nfs4_share_conflict(current_fh,
3936                                 NFS4_SHARE_DENY_READ);
3937 }
3938
3939 /*
3940  * Allow READ/WRITE during grace period on recovered state only for files
3941  * that are not able to provide mandatory locking.
3942  */
3943 static inline int
3944 grace_disallows_io(struct net *net, struct inode *inode)
3945 {
3946         return locks_in_grace(net) && mandatory_lock(inode);
3947 }
3948
3949 /* Returns true iff a is later than b: */
3950 static bool stateid_generation_after(stateid_t *a, stateid_t *b)
3951 {
3952         return (s32)(a->si_generation - b->si_generation) > 0;
3953 }
3954
3955 static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
3956 {
3957         /*
3958          * When sessions are used the stateid generation number is ignored
3959          * when it is zero.
3960          */
3961         if (has_session && in->si_generation == 0)
3962                 return nfs_ok;
3963
3964         if (in->si_generation == ref->si_generation)
3965                 return nfs_ok;
3966
3967         /* If the client sends us a stateid from the future, it's buggy: */
3968         if (stateid_generation_after(in, ref))
3969                 return nfserr_bad_stateid;
3970         /*
3971          * However, we could see a stateid from the past, even from a
3972          * non-buggy client.  For example, if the client sends a lock
3973          * while some IO is outstanding, the lock may bump si_generation
3974          * while the IO is still in flight.  The client could avoid that
3975          * situation by waiting for responses on all the IO requests,
3976          * but better performance may result in retrying IO that
3977          * receives an old_stateid error if requests are rarely
3978          * reordered in flight:
3979          */
3980         return nfserr_old_stateid;
3981 }
3982
3983 static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
3984 {
3985         struct nfs4_stid *s;
3986         struct nfs4_ol_stateid *ols;
3987         __be32 status;
3988
3989         if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3990                 return nfserr_bad_stateid;
3991         /* Client debugging aid. */
3992         if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
3993                 char addr_str[INET6_ADDRSTRLEN];
3994                 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
3995                                  sizeof(addr_str));
3996                 pr_warn_ratelimited("NFSD: client %s testing state ID "
3997                                         "with incorrect client ID\n", addr_str);
3998                 return nfserr_bad_stateid;
3999         }
4000         s = find_stateid(cl, stateid);
4001         if (!s)
4002                 return nfserr_bad_stateid;
4003         status = check_stateid_generation(stateid, &s->sc_stateid, 1);
4004         if (status)
4005                 return status;
4006         switch (s->sc_type) {
4007         case NFS4_DELEG_STID:
4008                 return nfs_ok;
4009         case NFS4_REVOKED_DELEG_STID:
4010                 return nfserr_deleg_revoked;
4011         case NFS4_OPEN_STID:
4012         case NFS4_LOCK_STID:
4013                 ols = openlockstateid(s);
4014                 if (ols->st_stateowner->so_is_open_owner
4015                                 && !(openowner(ols->st_stateowner)->oo_flags
4016                                                 & NFS4_OO_CONFIRMED))
4017                         return nfserr_bad_stateid;
4018                 return nfs_ok;
4019         default:
4020                 printk("unknown stateid type %x\n", s->sc_type);
4021         case NFS4_CLOSED_STID:
4022                 return nfserr_bad_stateid;
4023         }
4024 }
4025
4026 static __be32
4027 nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4028                      stateid_t *stateid, unsigned char typemask,
4029                      struct nfs4_stid **s, struct nfsd_net *nn)
4030 {
4031         __be32 status;
4032
4033         if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4034                 return nfserr_bad_stateid;
4035         status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
4036         if (status == nfserr_stale_clientid) {
4037                 if (cstate->session)
4038                         return nfserr_bad_stateid;
4039                 return nfserr_stale_stateid;
4040         }
4041         if (status)
4042                 return status;
4043         *s = find_stateid_by_type(cstate->clp, stateid, typemask);
4044         if (!*s)
4045                 return nfserr_bad_stateid;
4046         return nfs_ok;
4047 }
4048
4049 /*
4050 * Checks for stateid operations
4051 */
4052 __be32
4053 nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
4054                            stateid_t *stateid, int flags, struct file **filpp)
4055 {
4056         struct nfs4_stid *s;
4057         struct nfs4_ol_stateid *stp = NULL;
4058         struct nfs4_delegation *dp = NULL;
4059         struct svc_fh *current_fh = &cstate->current_fh;
4060         struct inode *ino = current_fh->fh_dentry->d_inode;
4061         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4062         struct file *file = NULL;
4063         __be32 status;
4064
4065         if (filpp)
4066                 *filpp = NULL;
4067
4068         if (grace_disallows_io(net, ino))
4069                 return nfserr_grace;
4070
4071         if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4072                 return check_special_stateids(net, current_fh, stateid, flags);
4073
4074         nfs4_lock_state();
4075
4076         status = nfsd4_lookup_stateid(cstate, stateid,
4077                                 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
4078                                 &s, nn);
4079         if (status)
4080                 goto out;
4081         status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4082         if (status)
4083                 goto out;
4084         switch (s->sc_type) {
4085         case NFS4_DELEG_STID:
4086                 dp = delegstateid(s);
4087                 status = nfs4_check_delegmode(dp, flags);
4088                 if (status)
4089                         goto out;
4090                 if (filpp) {
4091                         file = dp->dl_file->fi_deleg_file;
4092                         if (!file) {
4093                                 WARN_ON_ONCE(1);
4094                                 status = nfserr_serverfault;
4095                                 goto out;
4096                         }
4097                         get_file(file);
4098                 }
4099                 break;
4100         case NFS4_OPEN_STID:
4101         case NFS4_LOCK_STID:
4102                 stp = openlockstateid(s);
4103                 status = nfs4_check_fh(current_fh, stp);
4104                 if (status)
4105                         goto out;
4106                 if (stp->st_stateowner->so_is_open_owner
4107                     && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
4108                         goto out;
4109                 status = nfs4_check_openmode(stp, flags);
4110                 if (status)
4111                         goto out;
4112                 if (filpp) {
4113                         if (flags & RD_STATE)
4114                                 file = find_readable_file(stp->st_file);
4115                         else
4116                                 file = find_writeable_file(stp->st_file);
4117                 }
4118                 break;
4119         default:
4120                 status = nfserr_bad_stateid;
4121                 goto out;
4122         }
4123         status = nfs_ok;
4124         if (file)
4125                 *filpp = file;
4126 out:
4127         nfs4_unlock_state();
4128         return status;
4129 }
4130
4131 static __be32
4132 nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
4133 {
4134         struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
4135
4136         if (check_for_locks(stp->st_file, lo))
4137                 return nfserr_locks_held;
4138         release_lockowner_if_empty(lo);
4139         return nfs_ok;
4140 }
4141
4142 /*
4143  * Test if the stateid is valid
4144  */
4145 __be32
4146 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4147                    struct nfsd4_test_stateid *test_stateid)
4148 {
4149         struct nfsd4_test_stateid_id *stateid;
4150         struct nfs4_client *cl = cstate->session->se_client;
4151
4152         nfs4_lock_state();
4153         list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
4154                 stateid->ts_id_status =
4155                         nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
4156         nfs4_unlock_state();
4157
4158         return nfs_ok;
4159 }
4160
4161 __be32
4162 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4163                    struct nfsd4_free_stateid *free_stateid)
4164 {
4165         stateid_t *stateid = &free_stateid->fr_stateid;
4166         struct nfs4_stid *s;
4167         struct nfs4_delegation *dp;
4168         struct nfs4_client *cl = cstate->session->se_client;
4169         __be32 ret = nfserr_bad_stateid;
4170
4171         nfs4_lock_state();
4172         s = find_stateid(cl, stateid);
4173         if (!s)
4174                 goto out;
4175         switch (s->sc_type) {
4176         case NFS4_DELEG_STID:
4177                 ret = nfserr_locks_held;
4178                 goto out;
4179         case NFS4_OPEN_STID:
4180         case NFS4_LOCK_STID:
4181                 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4182                 if (ret)
4183                         goto out;
4184                 if (s->sc_type == NFS4_LOCK_STID)
4185                         ret = nfsd4_free_lock_stateid(openlockstateid(s));
4186                 else
4187                         ret = nfserr_locks_held;
4188                 break;
4189         case NFS4_REVOKED_DELEG_STID:
4190                 dp = delegstateid(s);
4191                 destroy_revoked_delegation(dp);
4192                 ret = nfs_ok;
4193                 break;
4194         default:
4195                 ret = nfserr_bad_stateid;
4196         }
4197 out:
4198         nfs4_unlock_state();
4199         return ret;
4200 }
4201
4202 static inline int
4203 setlkflg (int type)
4204 {
4205         return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4206                 RD_STATE : WR_STATE;
4207 }
4208
4209 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
4210 {
4211         struct svc_fh *current_fh = &cstate->current_fh;
4212         struct nfs4_stateowner *sop = stp->st_stateowner;
4213         __be32 status;
4214
4215         status = nfsd4_check_seqid(cstate, sop, seqid);
4216         if (status)
4217                 return status;
4218         if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4219                 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
4220                 /*
4221                  * "Closed" stateid's exist *only* to return
4222                  * nfserr_replay_me from the previous step, and
4223                  * revoked delegations are kept only for free_stateid.
4224                  */
4225                 return nfserr_bad_stateid;
4226         status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4227         if (status)
4228                 return status;
4229         return nfs4_check_fh(current_fh, stp);
4230 }
4231
4232 /* 
4233  * Checks for sequence id mutating operations. 
4234  */
4235 static __be32
4236 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4237                          stateid_t *stateid, char typemask,
4238                          struct nfs4_ol_stateid **stpp,
4239                          struct nfsd_net *nn)
4240 {
4241         __be32 status;
4242         struct nfs4_stid *s;
4243         struct nfs4_ol_stateid *stp = NULL;
4244
4245         dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4246                 seqid, STATEID_VAL(stateid));
4247
4248         *stpp = NULL;
4249         status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
4250         if (status)
4251                 return status;
4252         stp = openlockstateid(s);
4253         if (!nfsd4_has_session(cstate))
4254                 cstate->replay_owner = stp->st_stateowner;
4255
4256         status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
4257         if (!status)
4258                 *stpp = stp;
4259         return status;
4260 }
4261
4262 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4263                                                  stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
4264 {
4265         __be32 status;
4266         struct nfs4_openowner *oo;
4267
4268         status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
4269                                                 NFS4_OPEN_STID, stpp, nn);
4270         if (status)
4271                 return status;
4272         oo = openowner((*stpp)->st_stateowner);
4273         if (!(oo->oo_flags & NFS4_OO_CONFIRMED))
4274                 return nfserr_bad_stateid;
4275         return nfs_ok;
4276 }
4277
4278 __be32
4279 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4280                    struct nfsd4_open_confirm *oc)
4281 {
4282         __be32 status;
4283         struct nfs4_openowner *oo;
4284         struct nfs4_ol_stateid *stp;
4285         struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4286
4287         dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4288                         cstate->current_fh.fh_dentry);
4289
4290         status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
4291         if (status)
4292                 return status;
4293
4294         nfs4_lock_state();
4295
4296         status = nfs4_preprocess_seqid_op(cstate,
4297                                         oc->oc_seqid, &oc->oc_req_stateid,
4298                                         NFS4_OPEN_STID, &stp, nn);
4299         if (status)
4300                 goto out;
4301         oo = openowner(stp->st_stateowner);
4302         status = nfserr_bad_stateid;
4303         if (oo->oo_flags & NFS4_OO_CONFIRMED)
4304                 goto out;
4305         oo->oo_flags |= NFS4_OO_CONFIRMED;
4306         update_stateid(&stp->st_stid.sc_stateid);
4307         memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4308         dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
4309                 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
4310
4311         nfsd4_client_record_create(oo->oo_owner.so_client);
4312         status = nfs_ok;
4313 out:
4314         nfsd4_bump_seqid(cstate, status);
4315         if (!cstate->replay_owner)
4316                 nfs4_unlock_state();
4317         return status;
4318 }
4319
4320 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
4321 {
4322         if (!test_access(access, stp))
4323                 return;
4324         nfs4_file_put_access(stp->st_file, access);
4325         clear_access(access, stp);
4326 }
4327
4328 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4329 {
4330         switch (to_access) {
4331         case NFS4_SHARE_ACCESS_READ:
4332                 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4333                 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4334                 break;
4335         case NFS4_SHARE_ACCESS_WRITE:
4336                 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4337                 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4338                 break;
4339         case NFS4_SHARE_ACCESS_BOTH:
4340                 break;
4341         default:
4342                 WARN_ON_ONCE(1);
4343         }
4344 }
4345
4346 __be32
4347 nfsd4_open_downgrade(struct svc_rqst *rqstp,
4348                      struct nfsd4_compound_state *cstate,
4349                      struct nfsd4_open_downgrade *od)
4350 {
4351         __be32 status;
4352         struct nfs4_ol_stateid *stp;
4353         struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4354
4355         dprintk("NFSD: nfsd4_open_downgrade on file %pd\n", 
4356                         cstate->current_fh.fh_dentry);
4357
4358         /* We don't yet support WANT bits: */
4359         if (od->od_deleg_want)
4360                 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4361                         od->od_deleg_want);
4362
4363         nfs4_lock_state();
4364         status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
4365                                         &od->od_stateid, &stp, nn);
4366         if (status)
4367                 goto out; 
4368         status = nfserr_inval;
4369         if (!test_access(od->od_share_access, stp)) {
4370                 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
4371                         stp->st_access_bmap, od->od_share_access);
4372                 goto out;
4373         }
4374         if (!test_deny(od->od_share_deny, stp)) {
4375                 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
4376                         stp->st_deny_bmap, od->od_share_deny);
4377                 goto out;
4378         }
4379         nfs4_stateid_downgrade(stp, od->od_share_access);
4380
4381         reset_union_bmap_deny(od->od_share_deny, stp);
4382
4383         update_stateid(&stp->st_stid.sc_stateid);
4384         memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4385         status = nfs_ok;
4386 out:
4387         nfsd4_bump_seqid(cstate, status);
4388         if (!cstate->replay_owner)
4389                 nfs4_unlock_state();
4390         return status;
4391 }
4392
4393 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4394 {
4395         struct nfs4_client *clp = s->st_stid.sc_client;
4396         struct nfs4_openowner *oo = openowner(s->st_stateowner);
4397
4398         s->st_stid.sc_type = NFS4_CLOSED_STID;
4399         unhash_open_stateid(s);
4400
4401         if (clp->cl_minorversion) {
4402                 free_generic_stateid(s);
4403                 if (list_empty(&oo->oo_owner.so_stateids))
4404                         release_openowner(oo);
4405         } else {
4406                 oo->oo_last_closed_stid = s;
4407                 /*
4408                  * In the 4.0 case we need to keep the owners around a
4409                  * little while to handle CLOSE replay.
4410                  */
4411                 if (list_empty(&oo->oo_owner.so_stateids))
4412                         move_to_close_lru(oo, clp->net);
4413         }
4414 }
4415
4416 /*
4417  * nfs4_unlock_state() called after encode
4418  */
4419 __be32
4420 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4421             struct nfsd4_close *close)
4422 {
4423         __be32 status;
4424         struct nfs4_ol_stateid *stp;
4425         struct net *net = SVC_NET(rqstp);
4426         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4427
4428         dprintk("NFSD: nfsd4_close on file %pd\n", 
4429                         cstate->current_fh.fh_dentry);
4430
4431         nfs4_lock_state();
4432         status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4433                                         &close->cl_stateid,
4434                                         NFS4_OPEN_STID|NFS4_CLOSED_STID,
4435                                         &stp, nn);
4436         nfsd4_bump_seqid(cstate, status);
4437         if (status)
4438                 goto out; 
4439         update_stateid(&stp->st_stid.sc_stateid);
4440         memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4441
4442         nfsd4_close_open_stateid(stp);
4443 out:
4444         if (!cstate->replay_owner)
4445                 nfs4_unlock_state();
4446         return status;
4447 }
4448
4449 __be32
4450 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4451                   struct nfsd4_delegreturn *dr)
4452 {
4453         struct nfs4_delegation *dp;
4454         stateid_t *stateid = &dr->dr_stateid;
4455         struct nfs4_stid *s;
4456         __be32 status;
4457         struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4458
4459         if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
4460                 return status;
4461
4462         nfs4_lock_state();
4463         status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
4464         if (status)
4465                 goto out;
4466         dp = delegstateid(s);
4467         status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
4468         if (status)
4469                 goto out;
4470
4471         destroy_delegation(dp);
4472 out:
4473         nfs4_unlock_state();
4474
4475         return status;
4476 }
4477
4478
4479 #define LOFF_OVERFLOW(start, len)      ((u64)(len) > ~(u64)(start))
4480
4481 static inline u64
4482 end_offset(u64 start, u64 len)
4483 {
4484         u64 end;
4485
4486         end = start + len;
4487         return end >= start ? end: NFS4_MAX_UINT64;
4488 }
4489
4490 /* last octet in a range */
4491 static inline u64
4492 last_byte_offset(u64 start, u64 len)
4493 {
4494         u64 end;
4495
4496         WARN_ON_ONCE(!len);
4497         end = start + len;
4498         return end > start ? end - 1: NFS4_MAX_UINT64;
4499 }
4500
4501 /*
4502  * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4503  * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4504  * byte, because of sign extension problems.  Since NFSv4 calls for 64-bit
4505  * locking, this prevents us from being completely protocol-compliant.  The
4506  * real solution to this problem is to start using unsigned file offsets in
4507  * the VFS, but this is a very deep change!
4508  */
4509 static inline void
4510 nfs4_transform_lock_offset(struct file_lock *lock)
4511 {
4512         if (lock->fl_start < 0)
4513                 lock->fl_start = OFFSET_MAX;
4514         if (lock->fl_end < 0)
4515                 lock->fl_end = OFFSET_MAX;
4516 }
4517
4518 /* Hack!: For now, we're defining this just so we can use a pointer to it
4519  * as a unique cookie to identify our (NFSv4's) posix locks. */
4520 static const struct lock_manager_operations nfsd_posix_mng_ops  = {
4521 };
4522
4523 static inline void
4524 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4525 {
4526         struct nfs4_lockowner *lo;
4527
4528         if (fl->fl_lmops == &nfsd_posix_mng_ops) {
4529                 lo = (struct nfs4_lockowner *) fl->fl_owner;
4530                 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4531                                         lo->lo_owner.so_owner.len, GFP_KERNEL);
4532                 if (!deny->ld_owner.data)
4533                         /* We just don't care that much */
4534                         goto nevermind;
4535                 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4536                 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
4537         } else {
4538 nevermind:
4539                 deny->ld_owner.len = 0;
4540                 deny->ld_owner.data = NULL;
4541                 deny->ld_clientid.cl_boot = 0;
4542                 deny->ld_clientid.cl_id = 0;
4543         }
4544         deny->ld_start = fl->fl_start;
4545         deny->ld_length = NFS4_MAX_UINT64;
4546         if (fl->fl_end != NFS4_MAX_UINT64)
4547                 deny->ld_length = fl->fl_end - fl->fl_start + 1;        
4548         deny->ld_type = NFS4_READ_LT;
4549         if (fl->fl_type != F_RDLCK)
4550                 deny->ld_type = NFS4_WRITE_LT;
4551 }
4552
4553 static struct nfs4_lockowner *
4554 find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
4555                 struct nfsd_net *nn)
4556 {
4557         unsigned int strhashval = ownerstr_hashval(clid->cl_id, owner);
4558         struct nfs4_stateowner *so;
4559
4560         list_for_each_entry(so, &nn->ownerstr_hashtbl[strhashval], so_strhash) {
4561                 if (so->so_is_open_owner)
4562                         continue;
4563                 if (!same_owner_str(so, owner, clid))
4564                         continue;
4565                 return lockowner(so);
4566         }
4567         return NULL;
4568 }
4569
4570 /*
4571  * Alloc a lock owner structure.
4572  * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has 
4573  * occurred. 
4574  *
4575  * strhashval = ownerstr_hashval
4576  */
4577 static struct nfs4_lockowner *
4578 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_ol_stateid *open_stp, struct nfsd4_lock *lock) {
4579         struct nfs4_lockowner *lo;
4580         struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
4581
4582         lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4583         if (!lo)
4584                 return NULL;
4585         INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4586         lo->lo_owner.so_is_open_owner = 0;
4587         /* It is the openowner seqid that will be incremented in encode in the
4588          * case of new lockowners; so increment the lock seqid manually: */
4589         lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1;
4590         list_add(&lo->lo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
4591         return lo;
4592 }
4593
4594 static struct nfs4_ol_stateid *
4595 alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp, struct nfs4_ol_stateid *open_stp)
4596 {
4597         struct nfs4_ol_stateid *stp;
4598         struct nfs4_client *clp = lo->lo_owner.so_client;
4599
4600         stp = nfs4_alloc_stateid(clp);
4601         if (stp == NULL)
4602                 return NULL;
4603         stp->st_stid.sc_type = NFS4_LOCK_STID;
4604         list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
4605         stp->st_stateowner = &lo->lo_owner;
4606         get_nfs4_file(fp);
4607         stp->st_file = fp;
4608         stp->st_access_bmap = 0;
4609         stp->st_deny_bmap = open_stp->st_deny_bmap;
4610         stp->st_openstp = open_stp;
4611         list_add(&stp->st_locks, &open_stp->st_locks);
4612         spin_lock(&fp->fi_lock);
4613         list_add(&stp->st_perfile, &fp->fi_stateids);
4614         spin_unlock(&fp->fi_lock);
4615         return stp;
4616 }
4617
4618 static struct nfs4_ol_stateid *
4619 find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
4620 {
4621         struct nfs4_ol_stateid *lst;
4622
4623         list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
4624                 if (lst->st_file == fp)
4625                         return lst;
4626         }
4627         return NULL;
4628 }
4629
4630
4631 static int
4632 check_lock_length(u64 offset, u64 length)
4633 {
4634         return ((length == 0)  || ((length != NFS4_MAX_UINT64) &&
4635              LOFF_OVERFLOW(offset, length)));
4636 }
4637
4638 static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
4639 {
4640         struct nfs4_file *fp = lock_stp->st_file;
4641
4642         lockdep_assert_held(&fp->fi_lock);
4643
4644         if (test_access(access, lock_stp))
4645                 return;
4646         __nfs4_file_get_access(fp, access);
4647         set_access(access, lock_stp);
4648 }
4649
4650 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)
4651 {
4652         struct nfs4_file *fi = ost->st_file;
4653         struct nfs4_openowner *oo = openowner(ost->st_stateowner);
4654         struct nfs4_client *cl = oo->oo_owner.so_client;
4655         struct nfs4_lockowner *lo;
4656         unsigned int strhashval;
4657         struct nfsd_net *nn = net_generic(cl->net, nfsd_net_id);
4658
4659         lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, nn);
4660         if (!lo) {
4661                 strhashval = ownerstr_hashval(cl->cl_clientid.cl_id,
4662                                 &lock->v.new.owner);
4663                 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
4664                 if (lo == NULL)
4665                         return nfserr_jukebox;
4666         } else {
4667                 /* with an existing lockowner, seqids must be the same */
4668                 if (!cstate->minorversion &&
4669                     lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
4670                         return nfserr_bad_seqid;
4671         }
4672
4673         *lst = find_lock_stateid(lo, fi);
4674         if (*lst == NULL) {
4675                 *lst = alloc_init_lock_stateid(lo, fi, ost);
4676                 if (*lst == NULL) {
4677                         release_lockowner_if_empty(lo);
4678                         return nfserr_jukebox;
4679                 }
4680                 *new = true;
4681         }
4682         return nfs_ok;
4683 }
4684
4685 /*
4686  *  LOCK operation 
4687  */
4688 __be32
4689 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4690            struct nfsd4_lock *lock)
4691 {
4692         struct nfs4_openowner *open_sop = NULL;
4693         struct nfs4_lockowner *lock_sop = NULL;
4694         struct nfs4_ol_stateid *lock_stp;
4695         struct nfs4_file *fp;
4696         struct file *filp = NULL;
4697         struct file_lock *file_lock = NULL;
4698         struct file_lock *conflock = NULL;
4699         __be32 status = 0;
4700         bool new_state = false;
4701         int lkflg;
4702         int err;
4703         struct net *net = SVC_NET(rqstp);
4704         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4705
4706         dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
4707                 (long long) lock->lk_offset,
4708                 (long long) lock->lk_length);
4709
4710         if (check_lock_length(lock->lk_offset, lock->lk_length))
4711                  return nfserr_inval;
4712
4713         if ((status = fh_verify(rqstp, &cstate->current_fh,
4714                                 S_IFREG, NFSD_MAY_LOCK))) {
4715                 dprintk("NFSD: nfsd4_lock: permission denied!\n");
4716                 return status;
4717         }
4718
4719         nfs4_lock_state();
4720
4721         if (lock->lk_is_new) {
4722                 struct nfs4_ol_stateid *open_stp = NULL;
4723
4724                 if (nfsd4_has_session(cstate))
4725                         /* See rfc 5661 18.10.3: given clientid is ignored: */
4726                         memcpy(&lock->v.new.clientid,
4727                                 &cstate->session->se_client->cl_clientid,
4728                                 sizeof(clientid_t));
4729
4730                 status = nfserr_stale_clientid;
4731                 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
4732                         goto out;
4733
4734                 /* validate and update open stateid and open seqid */
4735                 status = nfs4_preprocess_confirmed_seqid_op(cstate,
4736                                         lock->lk_new_open_seqid,
4737                                         &lock->lk_new_open_stateid,
4738                                         &open_stp, nn);
4739                 if (status)
4740                         goto out;
4741                 open_sop = openowner(open_stp->st_stateowner);
4742                 status = nfserr_bad_stateid;
4743                 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
4744                                                 &lock->v.new.clientid))
4745                         goto out;
4746                 status = lookup_or_create_lock_state(cstate, open_stp, lock,
4747                                                         &lock_stp, &new_state);
4748         } else
4749                 status = nfs4_preprocess_seqid_op(cstate,
4750                                        lock->lk_old_lock_seqid,
4751                                        &lock->lk_old_lock_stateid,
4752                                        NFS4_LOCK_STID, &lock_stp, nn);
4753         if (status)
4754                 goto out;
4755         lock_sop = lockowner(lock_stp->st_stateowner);
4756
4757         lkflg = setlkflg(lock->lk_type);
4758         status = nfs4_check_openmode(lock_stp, lkflg);
4759         if (status)
4760                 goto out;
4761
4762         status = nfserr_grace;
4763         if (locks_in_grace(net) && !lock->lk_reclaim)
4764                 goto out;
4765         status = nfserr_no_grace;
4766         if (!locks_in_grace(net) && lock->lk_reclaim)
4767                 goto out;
4768
4769         file_lock = locks_alloc_lock();
4770         if (!file_lock) {
4771                 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4772                 status = nfserr_jukebox;
4773                 goto out;
4774         }
4775
4776         fp = lock_stp->st_file;
4777         locks_init_lock(file_lock);
4778         switch (lock->lk_type) {
4779                 case NFS4_READ_LT:
4780                 case NFS4_READW_LT:
4781                         spin_lock(&fp->fi_lock);
4782                         filp = find_readable_file_locked(fp);
4783                         if (filp)
4784                                 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
4785                         spin_unlock(&fp->fi_lock);
4786                         file_lock->fl_type = F_RDLCK;
4787                         break;
4788                 case NFS4_WRITE_LT:
4789                 case NFS4_WRITEW_LT:
4790                         spin_lock(&fp->fi_lock);
4791                         filp = find_writeable_file_locked(fp);
4792                         if (filp)
4793                                 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
4794                         spin_unlock(&fp->fi_lock);
4795                         file_lock->fl_type = F_WRLCK;
4796                         break;
4797                 default:
4798                         status = nfserr_inval;
4799                 goto out;
4800         }
4801         if (!filp) {
4802                 status = nfserr_openmode;
4803                 goto out;
4804         }
4805         file_lock->fl_owner = (fl_owner_t)lock_sop;
4806         file_lock->fl_pid = current->tgid;
4807         file_lock->fl_file = filp;
4808         file_lock->fl_flags = FL_POSIX;
4809         file_lock->fl_lmops = &nfsd_posix_mng_ops;
4810         file_lock->fl_start = lock->lk_offset;
4811         file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
4812         nfs4_transform_lock_offset(file_lock);
4813
4814         conflock = locks_alloc_lock();
4815         if (!conflock) {
4816                 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4817                 status = nfserr_jukebox;
4818                 goto out;
4819         }
4820
4821         err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
4822         switch (-err) {
4823         case 0: /* success! */
4824                 update_stateid(&lock_stp->st_stid.sc_stateid);
4825                 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid, 
4826                                 sizeof(stateid_t));
4827                 status = 0;
4828                 break;
4829         case (EAGAIN):          /* conflock holds conflicting lock */
4830                 status = nfserr_denied;
4831                 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
4832                 nfs4_set_lock_denied(conflock, &lock->lk_denied);
4833                 break;
4834         case (EDEADLK):
4835                 status = nfserr_deadlock;
4836                 break;
4837         default:
4838                 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
4839                 status = nfserrno(err);
4840                 break;
4841         }
4842 out:
4843         if (filp)
4844                 fput(filp);
4845         if (status && new_state)
4846                 release_lock_stateid(lock_stp);
4847         nfsd4_bump_seqid(cstate, status);
4848         if (!cstate->replay_owner)
4849                 nfs4_unlock_state();
4850         if (file_lock)
4851                 locks_free_lock(file_lock);
4852         if (conflock)
4853                 locks_free_lock(conflock);
4854         return status;
4855 }
4856
4857 /*
4858  * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
4859  * so we do a temporary open here just to get an open file to pass to
4860  * vfs_test_lock.  (Arguably perhaps test_lock should be done with an
4861  * inode operation.)
4862  */
4863 static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
4864 {
4865         struct file *file;
4866         __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
4867         if (!err) {
4868                 err = nfserrno(vfs_test_lock(file, lock));
4869                 nfsd_close(file);
4870         }
4871         return err;
4872 }
4873
4874 /*
4875  * LOCKT operation
4876  */
4877 __be32
4878 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4879             struct nfsd4_lockt *lockt)
4880 {
4881         struct file_lock *file_lock = NULL;
4882         struct nfs4_lockowner *lo;
4883         __be32 status;
4884         struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4885
4886         if (locks_in_grace(SVC_NET(rqstp)))
4887                 return nfserr_grace;
4888
4889         if (check_lock_length(lockt->lt_offset, lockt->lt_length))
4890                  return nfserr_inval;
4891
4892         nfs4_lock_state();
4893
4894         if (!nfsd4_has_session(cstate)) {
4895                 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
4896                 if (status)
4897                         goto out;
4898         }
4899
4900         if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
4901                 goto out;
4902
4903         file_lock = locks_alloc_lock();
4904         if (!file_lock) {
4905                 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4906                 status = nfserr_jukebox;
4907                 goto out;
4908         }
4909         locks_init_lock(file_lock);
4910         switch (lockt->lt_type) {
4911                 case NFS4_READ_LT:
4912                 case NFS4_READW_LT:
4913                         file_lock->fl_type = F_RDLCK;
4914                 break;
4915                 case NFS4_WRITE_LT:
4916                 case NFS4_WRITEW_LT:
4917                         file_lock->fl_type = F_WRLCK;
4918                 break;
4919                 default:
4920                         dprintk("NFSD: nfs4_lockt: bad lock type!\n");
4921                         status = nfserr_inval;
4922                 goto out;
4923         }
4924
4925         lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner, nn);
4926         if (lo)
4927                 file_lock->fl_owner = (fl_owner_t)lo;
4928         file_lock->fl_pid = current->tgid;
4929         file_lock->fl_flags = FL_POSIX;
4930
4931         file_lock->fl_start = lockt->lt_offset;
4932         file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
4933
4934         nfs4_transform_lock_offset(file_lock);
4935
4936         status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
4937         if (status)
4938                 goto out;
4939
4940         if (file_lock->fl_type != F_UNLCK) {
4941                 status = nfserr_denied;
4942                 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
4943         }
4944 out:
4945         nfs4_unlock_state();
4946         if (file_lock)
4947                 locks_free_lock(file_lock);
4948         return status;
4949 }
4950
4951 __be32
4952 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4953             struct nfsd4_locku *locku)
4954 {
4955         struct nfs4_ol_stateid *stp;
4956         struct file *filp = NULL;
4957         struct file_lock *file_lock = NULL;
4958         __be32 status;
4959         int err;
4960         struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4961
4962         dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
4963                 (long long) locku->lu_offset,
4964                 (long long) locku->lu_length);
4965
4966         if (check_lock_length(locku->lu_offset, locku->lu_length))
4967                  return nfserr_inval;
4968
4969         nfs4_lock_state();
4970                                                                                 
4971         status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
4972                                         &locku->lu_stateid, NFS4_LOCK_STID,
4973                                         &stp, nn);
4974         if (status)
4975                 goto out;
4976         filp = find_any_file(stp->st_file);
4977         if (!filp) {
4978                 status = nfserr_lock_range;
4979                 goto out;
4980         }
4981         file_lock = locks_alloc_lock();
4982         if (!file_lock) {
4983                 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4984                 status = nfserr_jukebox;
4985                 goto fput;
4986         }
4987         locks_init_lock(file_lock);
4988         file_lock->fl_type = F_UNLCK;
4989         file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
4990         file_lock->fl_pid = current->tgid;
4991         file_lock->fl_file = filp;
4992         file_lock->fl_flags = FL_POSIX;
4993         file_lock->fl_lmops = &nfsd_posix_mng_ops;
4994         file_lock->fl_start = locku->lu_offset;
4995
4996         file_lock->fl_end = last_byte_offset(locku->lu_offset,
4997                                                 locku->lu_length);
4998         nfs4_transform_lock_offset(file_lock);
4999
5000         err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
5001         if (err) {
5002                 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
5003                 goto out_nfserr;
5004         }
5005         update_stateid(&stp->st_stid.sc_stateid);
5006         memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
5007 fput:
5008         fput(filp);
5009 out:
5010         nfsd4_bump_seqid(cstate, status);
5011         if (!cstate->replay_owner)
5012                 nfs4_unlock_state();
5013         if (file_lock)
5014                 locks_free_lock(file_lock);
5015         return status;
5016
5017 out_nfserr:
5018         status = nfserrno(err);
5019         goto fput;
5020 }
5021
5022 /*
5023  * returns
5024  *      1: locks held by lockowner
5025  *      0: no locks held by lockowner
5026  */
5027 static int
5028 check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner)
5029 {
5030         struct file_lock **flpp;
5031         struct inode *inode = filp->fi_inode;
5032         int status = 0;
5033
5034         spin_lock(&inode->i_lock);
5035         for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
5036                 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
5037                         status = 1;
5038                         goto out;
5039                 }
5040         }
5041 out:
5042         spin_unlock(&inode->i_lock);
5043         return status;
5044 }
5045
5046 __be32
5047 nfsd4_release_lockowner(struct svc_rqst *rqstp,
5048                         struct nfsd4_compound_state *cstate,
5049                         struct nfsd4_release_lockowner *rlockowner)
5050 {
5051         clientid_t *clid = &rlockowner->rl_clientid;
5052         struct nfs4_stateowner *sop = NULL, *tmp;
5053         struct nfs4_lockowner *lo;
5054         struct nfs4_ol_stateid *stp;
5055         struct xdr_netobj *owner = &rlockowner->rl_owner;
5056         unsigned int hashval = ownerstr_hashval(clid->cl_id, owner);
5057         __be32 status;
5058         struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5059
5060         dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5061                 clid->cl_boot, clid->cl_id);
5062
5063         nfs4_lock_state();
5064
5065         status = lookup_clientid(clid, cstate, nn);
5066         if (status)
5067                 goto out;
5068
5069         status = nfserr_locks_held;
5070
5071         /* Find the matching lock stateowner */
5072         list_for_each_entry(tmp, &nn->ownerstr_hashtbl[hashval], so_strhash) {
5073                 if (tmp->so_is_open_owner)
5074                         continue;
5075                 if (same_owner_str(tmp, owner, clid)) {
5076                         sop = tmp;
5077                         break;
5078                 }
5079         }
5080
5081         /* No matching owner found, maybe a replay? Just declare victory... */
5082         if (!sop) {
5083                 status = nfs_ok;
5084                 goto out;
5085         }
5086
5087         lo = lockowner(sop);
5088         /* see if there are still any locks associated with it */
5089         list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5090                 if (check_for_locks(stp->st_file, lo))
5091                         goto out;
5092         }
5093
5094         status = nfs_ok;
5095         release_lockowner(lo);
5096 out:
5097         nfs4_unlock_state();
5098         return status;
5099 }
5100
5101 static inline struct nfs4_client_reclaim *
5102 alloc_reclaim(void)
5103 {
5104         return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
5105 }
5106
5107 bool
5108 nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
5109 {
5110         struct nfs4_client_reclaim *crp;
5111
5112         crp = nfsd4_find_reclaim_client(name, nn);
5113         return (crp && crp->cr_clp);
5114 }
5115
5116 /*
5117  * failure => all reset bets are off, nfserr_no_grace...
5118  */
5119 struct nfs4_client_reclaim *
5120 nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
5121 {
5122         unsigned int strhashval;
5123         struct nfs4_client_reclaim *crp;
5124
5125         dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5126         crp = alloc_reclaim();
5127         if (crp) {
5128                 strhashval = clientstr_hashval(name);
5129                 INIT_LIST_HEAD(&crp->cr_strhash);
5130                 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
5131                 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
5132                 crp->cr_clp = NULL;
5133                 nn->reclaim_str_hashtbl_size++;
5134         }
5135         return crp;
5136 }
5137
5138 void
5139 nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
5140 {
5141         list_del(&crp->cr_strhash);
5142         kfree(crp);
5143         nn->reclaim_str_hashtbl_size--;
5144 }
5145
5146 void
5147 nfs4_release_reclaim(struct nfsd_net *nn)
5148 {
5149         struct nfs4_client_reclaim *crp = NULL;
5150         int i;
5151
5152         for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5153                 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5154                         crp = list_entry(nn->reclaim_str_hashtbl[i].next,
5155                                         struct nfs4_client_reclaim, cr_strhash);
5156                         nfs4_remove_reclaim_record(crp, nn);
5157                 }
5158         }
5159         WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
5160 }
5161
5162 /*
5163  * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
5164 struct nfs4_client_reclaim *
5165 nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
5166 {
5167         unsigned int strhashval;
5168         struct nfs4_client_reclaim *crp = NULL;
5169
5170         dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
5171
5172         strhashval = clientstr_hashval(recdir);
5173         list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
5174                 if (same_name(crp->cr_recdir, recdir)) {
5175                         return crp;
5176                 }
5177         }
5178         return NULL;
5179 }
5180
5181 /*
5182 * Called from OPEN. Look for clientid in reclaim list.
5183 */
5184 __be32
5185 nfs4_check_open_reclaim(clientid_t *clid,
5186                 struct nfsd4_compound_state *cstate,
5187                 struct nfsd_net *nn)
5188 {
5189         __be32 status;
5190
5191         /* find clientid in conf_id_hashtbl */
5192         status = lookup_clientid(clid, cstate, nn);
5193         if (status)
5194                 return nfserr_reclaim_bad;
5195
5196         if (nfsd4_client_record_check(cstate->clp))
5197                 return nfserr_reclaim_bad;
5198
5199         return nfs_ok;
5200 }
5201
5202 #ifdef CONFIG_NFSD_FAULT_INJECTION
5203
5204 u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
5205 {
5206         if (mark_client_expired(clp))
5207                 return 0;
5208         expire_client(clp);
5209         return 1;
5210 }
5211
5212 u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
5213 {
5214         char buf[INET6_ADDRSTRLEN];
5215         rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5216         printk(KERN_INFO "NFS Client: %s\n", buf);
5217         return 1;
5218 }
5219
5220 static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5221                              const char *type)
5222 {
5223         char buf[INET6_ADDRSTRLEN];
5224         rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5225         printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5226 }
5227
5228 static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
5229                                     void (*func)(struct nfs4_ol_stateid *))
5230 {
5231         struct nfs4_openowner *oop;
5232         struct nfs4_ol_stateid *stp, *st_next;
5233         struct nfs4_ol_stateid *lst, *lst_next;
5234         u64 count = 0;
5235
5236         list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
5237                 list_for_each_entry_safe(stp, st_next,
5238                                 &oop->oo_owner.so_stateids, st_perstateowner) {
5239                         list_for_each_entry_safe(lst, lst_next,
5240                                         &stp->st_locks, st_locks) {
5241                                 if (func)
5242                                         func(lst);
5243                                 if (++count == max)
5244                                         return count;
5245                         }
5246                 }
5247         }
5248
5249         return count;
5250 }
5251
5252 u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
5253 {
5254         return nfsd_foreach_client_lock(clp, max, release_lock_stateid);
5255 }
5256
5257 u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
5258 {
5259         u64 count = nfsd_foreach_client_lock(clp, max, NULL);
5260         nfsd_print_count(clp, count, "locked files");
5261         return count;
5262 }
5263
5264 static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
5265 {
5266         struct nfs4_openowner *oop, *next;
5267         u64 count = 0;
5268
5269         list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
5270                 if (func)
5271                         func(oop);
5272                 if (++count == max)
5273                         break;
5274         }
5275
5276         return count;
5277 }
5278
5279 u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
5280 {
5281         return nfsd_foreach_client_open(clp, max, release_openowner);
5282 }
5283
5284 u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
5285 {
5286         u64 count = nfsd_foreach_client_open(clp, max, NULL);
5287         nfsd_print_count(clp, count, "open files");
5288         return count;
5289 }
5290
5291 static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
5292                                      struct list_head *victims)
5293 {
5294         struct nfs4_delegation *dp, *next;
5295         u64 count = 0;
5296
5297         lockdep_assert_held(&state_lock);
5298         list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
5299                 if (victims) {
5300                         /*
5301                          * It's not safe to mess with delegations that have a
5302                          * non-zero dl_time. They might have already been broken
5303                          * and could be processed by the laundromat outside of
5304                          * the state_lock. Just leave them be.
5305                          */
5306                         if (dp->dl_time != 0)
5307                                 continue;
5308
5309                         /*
5310                          * Increment dl_time to ensure that delegation breaks
5311                          * don't monkey with it now that we are.
5312                          */
5313                         ++dp->dl_time;
5314                         list_move(&dp->dl_recall_lru, victims);
5315                 }
5316                 if (++count == max)
5317                         break;
5318         }
5319         return count;
5320 }
5321
5322 u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
5323 {
5324         struct nfs4_delegation *dp, *next;
5325         LIST_HEAD(victims);
5326         u64 count;
5327
5328         spin_lock(&state_lock);
5329         count = nfsd_find_all_delegations(clp, max, &victims);
5330         spin_unlock(&state_lock);
5331
5332         list_for_each_entry_safe(dp, next, &victims, dl_recall_lru)
5333                 revoke_delegation(dp);
5334
5335         return count;
5336 }
5337
5338 u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
5339 {
5340         struct nfs4_delegation *dp;
5341         LIST_HEAD(victims);
5342         u64 count;
5343
5344         spin_lock(&state_lock);
5345         count = nfsd_find_all_delegations(clp, max, &victims);
5346         while (!list_empty(&victims)) {
5347                 dp = list_first_entry(&victims, struct nfs4_delegation,
5348                                         dl_recall_lru);
5349                 list_del_init(&dp->dl_recall_lru);
5350                 dp->dl_time = 0;
5351                 nfsd_break_one_deleg(dp);
5352         }
5353         spin_unlock(&state_lock);
5354
5355         return count;
5356 }
5357
5358 u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
5359 {
5360         u64 count = 0;
5361
5362         spin_lock(&state_lock);
5363         count = nfsd_find_all_delegations(clp, max, NULL);
5364         spin_unlock(&state_lock);
5365
5366         nfsd_print_count(clp, count, "delegations");
5367         return count;
5368 }
5369
5370 u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
5371 {
5372         struct nfs4_client *clp, *next;
5373         u64 count = 0;
5374         struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
5375
5376         if (!nfsd_netns_ready(nn))
5377                 return 0;
5378
5379         list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
5380                 count += func(clp, max - count);
5381                 if ((max != 0) && (count >= max))
5382                         break;
5383         }
5384
5385         return count;
5386 }
5387
5388 struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5389 {
5390         struct nfs4_client *clp;
5391         struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
5392
5393         if (!nfsd_netns_ready(nn))
5394                 return NULL;
5395
5396         list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5397                 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5398                         return clp;
5399         }
5400         return NULL;
5401 }
5402
5403 #endif /* CONFIG_NFSD_FAULT_INJECTION */
5404
5405 /*
5406  * Since the lifetime of a delegation isn't limited to that of an open, a
5407  * client may quite reasonably hang on to a delegation as long as it has
5408  * the inode cached.  This becomes an obvious problem the first time a
5409  * client's inode cache approaches the size of the server's total memory.
5410  *
5411  * For now we avoid this problem by imposing a hard limit on the number
5412  * of delegations, which varies according to the server's memory size.
5413  */
5414 static void
5415 set_max_delegations(void)
5416 {
5417         /*
5418          * Allow at most 4 delegations per megabyte of RAM.  Quick
5419          * estimates suggest that in the worst case (where every delegation
5420          * is for a different inode), a delegation could take about 1.5K,
5421          * giving a worst case usage of about 6% of memory.
5422          */
5423         max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
5424 }
5425
5426 static int nfs4_state_create_net(struct net *net)
5427 {
5428         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5429         int i;
5430
5431         nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5432                         CLIENT_HASH_SIZE, GFP_KERNEL);
5433         if (!nn->conf_id_hashtbl)
5434                 goto err;
5435         nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5436                         CLIENT_HASH_SIZE, GFP_KERNEL);
5437         if (!nn->unconf_id_hashtbl)
5438                 goto err_unconf_id;
5439         nn->ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
5440                         OWNER_HASH_SIZE, GFP_KERNEL);
5441         if (!nn->ownerstr_hashtbl)
5442                 goto err_ownerstr;
5443         nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
5444                         SESSION_HASH_SIZE, GFP_KERNEL);
5445         if (!nn->sessionid_hashtbl)
5446                 goto err_sessionid;
5447
5448         for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5449                 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
5450                 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
5451         }
5452         for (i = 0; i < OWNER_HASH_SIZE; i++)
5453                 INIT_LIST_HEAD(&nn->ownerstr_hashtbl[i]);
5454         for (i = 0; i < SESSION_HASH_SIZE; i++)
5455                 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
5456         nn->conf_name_tree = RB_ROOT;
5457         nn->unconf_name_tree = RB_ROOT;
5458         INIT_LIST_HEAD(&nn->client_lru);
5459         INIT_LIST_HEAD(&nn->close_lru);
5460         INIT_LIST_HEAD(&nn->del_recall_lru);
5461         spin_lock_init(&nn->client_lock);
5462
5463         INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
5464         get_net(net);
5465
5466         return 0;
5467
5468 err_sessionid:
5469         kfree(nn->ownerstr_hashtbl);
5470 err_ownerstr:
5471         kfree(nn->unconf_id_hashtbl);
5472 err_unconf_id:
5473         kfree(nn->conf_id_hashtbl);
5474 err:
5475         return -ENOMEM;
5476 }
5477
5478 static void
5479 nfs4_state_destroy_net(struct net *net)
5480 {
5481         int i;
5482         struct nfs4_client *clp = NULL;
5483         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5484
5485         for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5486                 while (!list_empty(&nn->conf_id_hashtbl[i])) {
5487                         clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5488                         destroy_client(clp);
5489                 }
5490         }
5491
5492         for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5493                 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
5494                         clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5495                         destroy_client(clp);
5496                 }
5497         }
5498
5499         kfree(nn->sessionid_hashtbl);
5500         kfree(nn->ownerstr_hashtbl);
5501         kfree(nn->unconf_id_hashtbl);
5502         kfree(nn->conf_id_hashtbl);
5503         put_net(net);
5504 }
5505
5506 int
5507 nfs4_state_start_net(struct net *net)
5508 {
5509         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5510         int ret;
5511
5512         ret = nfs4_state_create_net(net);
5513         if (ret)
5514                 return ret;
5515         nfsd4_client_tracking_init(net);
5516         nn->boot_time = get_seconds();
5517         locks_start_grace(net, &nn->nfsd4_manager);
5518         nn->grace_ended = false;
5519         printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
5520                nn->nfsd4_grace, net);
5521         queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
5522         return 0;
5523 }
5524
5525 /* initialization to perform when the nfsd service is started: */
5526
5527 int
5528 nfs4_state_start(void)
5529 {
5530         int ret;
5531
5532         ret = set_callback_cred();
5533         if (ret)
5534                 return -ENOMEM;
5535         laundry_wq = create_singlethread_workqueue("nfsd4");
5536         if (laundry_wq == NULL) {
5537                 ret = -ENOMEM;
5538                 goto out_recovery;
5539         }
5540         ret = nfsd4_create_callback_queue();
5541         if (ret)
5542                 goto out_free_laundry;
5543
5544         set_max_delegations();
5545
5546         return 0;
5547
5548 out_free_laundry:
5549         destroy_workqueue(laundry_wq);
5550 out_recovery:
5551         return ret;
5552 }
5553
5554 void
5555 nfs4_state_shutdown_net(struct net *net)
5556 {
5557         struct nfs4_delegation *dp = NULL;
5558         struct list_head *pos, *next, reaplist;
5559         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5560
5561         cancel_delayed_work_sync(&nn->laundromat_work);
5562         locks_end_grace(&nn->nfsd4_manager);
5563
5564         nfs4_lock_state();
5565         INIT_LIST_HEAD(&reaplist);
5566         spin_lock(&state_lock);
5567         list_for_each_safe(pos, next, &nn->del_recall_lru) {
5568                 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
5569                 list_move(&dp->dl_recall_lru, &reaplist);
5570         }
5571         spin_unlock(&state_lock);
5572         list_for_each_safe(pos, next, &reaplist) {
5573                 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
5574                 destroy_delegation(dp);
5575         }
5576
5577         nfsd4_client_tracking_exit(net);
5578         nfs4_state_destroy_net(net);
5579         nfs4_unlock_state();
5580 }
5581
5582 void
5583 nfs4_state_shutdown(void)
5584 {
5585         destroy_workqueue(laundry_wq);
5586         nfsd4_destroy_callback_queue();
5587 }
5588
5589 static void
5590 get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5591 {
5592         if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
5593                 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
5594 }
5595
5596 static void
5597 put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5598 {
5599         if (cstate->minorversion) {
5600                 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
5601                 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
5602         }
5603 }
5604
5605 void
5606 clear_current_stateid(struct nfsd4_compound_state *cstate)
5607 {
5608         CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
5609 }
5610
5611 /*
5612  * functions to set current state id
5613  */
5614 void
5615 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5616 {
5617         put_stateid(cstate, &odp->od_stateid);
5618 }
5619
5620 void
5621 nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
5622 {
5623         put_stateid(cstate, &open->op_stateid);
5624 }
5625
5626 void
5627 nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5628 {
5629         put_stateid(cstate, &close->cl_stateid);
5630 }
5631
5632 void
5633 nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
5634 {
5635         put_stateid(cstate, &lock->lk_resp_stateid);
5636 }
5637
5638 /*
5639  * functions to consume current state id
5640  */
5641
5642 void
5643 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5644 {
5645         get_stateid(cstate, &odp->od_stateid);
5646 }
5647
5648 void
5649 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
5650 {
5651         get_stateid(cstate, &drp->dr_stateid);
5652 }
5653
5654 void
5655 nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
5656 {
5657         get_stateid(cstate, &fsp->fr_stateid);
5658 }
5659
5660 void
5661 nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
5662 {
5663         get_stateid(cstate, &setattr->sa_stateid);
5664 }
5665
5666 void
5667 nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5668 {
5669         get_stateid(cstate, &close->cl_stateid);
5670 }
5671
5672 void
5673 nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
5674 {
5675         get_stateid(cstate, &locku->lu_stateid);
5676 }
5677
5678 void
5679 nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
5680 {
5681         get_stateid(cstate, &read->rd_stateid);
5682 }
5683
5684 void
5685 nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
5686 {
5687         get_stateid(cstate, &write->wr_stateid);
5688 }