dm thin metadata: fix __udivdi3 undefined on 32-bit
[linux/fpc-iii.git] / fs / nfsd / nfs4state.c
blobba27a5ff867750645abcc3e7e36987cd97e11c2f
1 /*
2 * Copyright (c) 2001 The Regents of the University of Michigan.
3 * All rights reserved.
5 * Kendrick Smith <kmsmith@umich.edu>
6 * Andy Adamson <kandros@umich.edu>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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.
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.
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/jhash.h>
45 #include "xdr4.h"
46 #include "xdr4cb.h"
47 #include "vfs.h"
48 #include "current_stateid.h"
50 #include "netns.h"
51 #include "pnfs.h"
53 #define NFSDDBG_FACILITY NFSDDBG_PROC
55 #define all_ones {{~0,~0},~0}
56 static const stateid_t one_stateid = {
57 .si_generation = ~0,
58 .si_opaque = all_ones,
60 static const stateid_t zero_stateid = {
61 /* all fields zero */
63 static const stateid_t currentstateid = {
64 .si_generation = 1,
66 static const stateid_t close_stateid = {
67 .si_generation = 0xffffffffU,
70 static u64 current_sessionid = 1;
72 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
73 #define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
74 #define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
75 #define CLOSE_STATEID(stateid) (!memcmp((stateid), &close_stateid, sizeof(stateid_t)))
77 /* forward declarations */
78 static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
79 static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
81 /* Locking: */
84 * Currently used for the del_recall_lru and file hash table. In an
85 * effort to decrease the scope of the client_mutex, this spinlock may
86 * eventually cover more:
88 static DEFINE_SPINLOCK(state_lock);
91 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
92 * the refcount on the open stateid to drop.
94 static DECLARE_WAIT_QUEUE_HEAD(close_wq);
96 static struct kmem_cache *openowner_slab;
97 static struct kmem_cache *lockowner_slab;
98 static struct kmem_cache *file_slab;
99 static struct kmem_cache *stateid_slab;
100 static struct kmem_cache *deleg_slab;
101 static struct kmem_cache *odstate_slab;
103 static void free_session(struct nfsd4_session *);
105 static struct nfsd4_callback_ops nfsd4_cb_recall_ops;
107 static bool is_session_dead(struct nfsd4_session *ses)
109 return ses->se_flags & NFS4_SESSION_DEAD;
112 static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
114 if (atomic_read(&ses->se_ref) > ref_held_by_me)
115 return nfserr_jukebox;
116 ses->se_flags |= NFS4_SESSION_DEAD;
117 return nfs_ok;
120 static bool is_client_expired(struct nfs4_client *clp)
122 return clp->cl_time == 0;
125 static __be32 get_client_locked(struct nfs4_client *clp)
127 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
129 lockdep_assert_held(&nn->client_lock);
131 if (is_client_expired(clp))
132 return nfserr_expired;
133 atomic_inc(&clp->cl_refcount);
134 return nfs_ok;
137 /* must be called under the client_lock */
138 static inline void
139 renew_client_locked(struct nfs4_client *clp)
141 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
143 if (is_client_expired(clp)) {
144 WARN_ON(1);
145 printk("%s: client (clientid %08x/%08x) already expired\n",
146 __func__,
147 clp->cl_clientid.cl_boot,
148 clp->cl_clientid.cl_id);
149 return;
152 dprintk("renewing client (clientid %08x/%08x)\n",
153 clp->cl_clientid.cl_boot,
154 clp->cl_clientid.cl_id);
155 list_move_tail(&clp->cl_lru, &nn->client_lru);
156 clp->cl_time = get_seconds();
159 static void put_client_renew_locked(struct nfs4_client *clp)
161 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
163 lockdep_assert_held(&nn->client_lock);
165 if (!atomic_dec_and_test(&clp->cl_refcount))
166 return;
167 if (!is_client_expired(clp))
168 renew_client_locked(clp);
171 static void put_client_renew(struct nfs4_client *clp)
173 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
175 if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
176 return;
177 if (!is_client_expired(clp))
178 renew_client_locked(clp);
179 spin_unlock(&nn->client_lock);
182 static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
184 __be32 status;
186 if (is_session_dead(ses))
187 return nfserr_badsession;
188 status = get_client_locked(ses->se_client);
189 if (status)
190 return status;
191 atomic_inc(&ses->se_ref);
192 return nfs_ok;
195 static void nfsd4_put_session_locked(struct nfsd4_session *ses)
197 struct nfs4_client *clp = ses->se_client;
198 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
200 lockdep_assert_held(&nn->client_lock);
202 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
203 free_session(ses);
204 put_client_renew_locked(clp);
207 static void nfsd4_put_session(struct nfsd4_session *ses)
209 struct nfs4_client *clp = ses->se_client;
210 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
212 spin_lock(&nn->client_lock);
213 nfsd4_put_session_locked(ses);
214 spin_unlock(&nn->client_lock);
217 static inline struct nfs4_stateowner *
218 nfs4_get_stateowner(struct nfs4_stateowner *sop)
220 atomic_inc(&sop->so_count);
221 return sop;
224 static int
225 same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
227 return (sop->so_owner.len == owner->len) &&
228 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
231 static struct nfs4_openowner *
232 find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
233 struct nfs4_client *clp)
235 struct nfs4_stateowner *so;
237 lockdep_assert_held(&clp->cl_lock);
239 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
240 so_strhash) {
241 if (!so->so_is_open_owner)
242 continue;
243 if (same_owner_str(so, &open->op_owner))
244 return openowner(nfs4_get_stateowner(so));
246 return NULL;
249 static struct nfs4_openowner *
250 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
251 struct nfs4_client *clp)
253 struct nfs4_openowner *oo;
255 spin_lock(&clp->cl_lock);
256 oo = find_openstateowner_str_locked(hashval, open, clp);
257 spin_unlock(&clp->cl_lock);
258 return oo;
261 static inline u32
262 opaque_hashval(const void *ptr, int nbytes)
264 unsigned char *cptr = (unsigned char *) ptr;
266 u32 x = 0;
267 while (nbytes--) {
268 x *= 37;
269 x += *cptr++;
271 return x;
274 static void nfsd4_free_file_rcu(struct rcu_head *rcu)
276 struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
278 kmem_cache_free(file_slab, fp);
281 void
282 put_nfs4_file(struct nfs4_file *fi)
284 might_lock(&state_lock);
286 if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) {
287 hlist_del_rcu(&fi->fi_hash);
288 spin_unlock(&state_lock);
289 WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
290 WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
291 call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
295 static struct file *
296 __nfs4_get_fd(struct nfs4_file *f, int oflag)
298 if (f->fi_fds[oflag])
299 return get_file(f->fi_fds[oflag]);
300 return NULL;
303 static struct file *
304 find_writeable_file_locked(struct nfs4_file *f)
306 struct file *ret;
308 lockdep_assert_held(&f->fi_lock);
310 ret = __nfs4_get_fd(f, O_WRONLY);
311 if (!ret)
312 ret = __nfs4_get_fd(f, O_RDWR);
313 return ret;
316 static struct file *
317 find_writeable_file(struct nfs4_file *f)
319 struct file *ret;
321 spin_lock(&f->fi_lock);
322 ret = find_writeable_file_locked(f);
323 spin_unlock(&f->fi_lock);
325 return ret;
328 static struct file *find_readable_file_locked(struct nfs4_file *f)
330 struct file *ret;
332 lockdep_assert_held(&f->fi_lock);
334 ret = __nfs4_get_fd(f, O_RDONLY);
335 if (!ret)
336 ret = __nfs4_get_fd(f, O_RDWR);
337 return ret;
340 static struct file *
341 find_readable_file(struct nfs4_file *f)
343 struct file *ret;
345 spin_lock(&f->fi_lock);
346 ret = find_readable_file_locked(f);
347 spin_unlock(&f->fi_lock);
349 return ret;
352 struct file *
353 find_any_file(struct nfs4_file *f)
355 struct file *ret;
357 spin_lock(&f->fi_lock);
358 ret = __nfs4_get_fd(f, O_RDWR);
359 if (!ret) {
360 ret = __nfs4_get_fd(f, O_WRONLY);
361 if (!ret)
362 ret = __nfs4_get_fd(f, O_RDONLY);
364 spin_unlock(&f->fi_lock);
365 return ret;
368 static atomic_long_t num_delegations;
369 unsigned long max_delegations;
372 * Open owner state (share locks)
375 /* hash tables for lock and open owners */
376 #define OWNER_HASH_BITS 8
377 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
378 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
380 static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
382 unsigned int ret;
384 ret = opaque_hashval(ownername->data, ownername->len);
385 return ret & OWNER_HASH_MASK;
388 /* hash table for nfs4_file */
389 #define FILE_HASH_BITS 8
390 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
392 static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
394 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
397 static unsigned int file_hashval(struct knfsd_fh *fh)
399 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
402 static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
404 static void
405 __nfs4_file_get_access(struct nfs4_file *fp, u32 access)
407 lockdep_assert_held(&fp->fi_lock);
409 if (access & NFS4_SHARE_ACCESS_WRITE)
410 atomic_inc(&fp->fi_access[O_WRONLY]);
411 if (access & NFS4_SHARE_ACCESS_READ)
412 atomic_inc(&fp->fi_access[O_RDONLY]);
415 static __be32
416 nfs4_file_get_access(struct nfs4_file *fp, u32 access)
418 lockdep_assert_held(&fp->fi_lock);
420 /* Does this access mode make sense? */
421 if (access & ~NFS4_SHARE_ACCESS_BOTH)
422 return nfserr_inval;
424 /* Does it conflict with a deny mode already set? */
425 if ((access & fp->fi_share_deny) != 0)
426 return nfserr_share_denied;
428 __nfs4_file_get_access(fp, access);
429 return nfs_ok;
432 static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
434 /* Common case is that there is no deny mode. */
435 if (deny) {
436 /* Does this deny mode make sense? */
437 if (deny & ~NFS4_SHARE_DENY_BOTH)
438 return nfserr_inval;
440 if ((deny & NFS4_SHARE_DENY_READ) &&
441 atomic_read(&fp->fi_access[O_RDONLY]))
442 return nfserr_share_denied;
444 if ((deny & NFS4_SHARE_DENY_WRITE) &&
445 atomic_read(&fp->fi_access[O_WRONLY]))
446 return nfserr_share_denied;
448 return nfs_ok;
451 static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
453 might_lock(&fp->fi_lock);
455 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
456 struct file *f1 = NULL;
457 struct file *f2 = NULL;
459 swap(f1, fp->fi_fds[oflag]);
460 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
461 swap(f2, fp->fi_fds[O_RDWR]);
462 spin_unlock(&fp->fi_lock);
463 if (f1)
464 fput(f1);
465 if (f2)
466 fput(f2);
470 static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
472 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
474 if (access & NFS4_SHARE_ACCESS_WRITE)
475 __nfs4_file_put_access(fp, O_WRONLY);
476 if (access & NFS4_SHARE_ACCESS_READ)
477 __nfs4_file_put_access(fp, O_RDONLY);
481 * Allocate a new open/delegation state counter. This is needed for
482 * pNFS for proper return on close semantics.
484 * Note that we only allocate it for pNFS-enabled exports, otherwise
485 * all pointers to struct nfs4_clnt_odstate are always NULL.
487 static struct nfs4_clnt_odstate *
488 alloc_clnt_odstate(struct nfs4_client *clp)
490 struct nfs4_clnt_odstate *co;
492 co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
493 if (co) {
494 co->co_client = clp;
495 atomic_set(&co->co_odcount, 1);
497 return co;
500 static void
501 hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
503 struct nfs4_file *fp = co->co_file;
505 lockdep_assert_held(&fp->fi_lock);
506 list_add(&co->co_perfile, &fp->fi_clnt_odstate);
509 static inline void
510 get_clnt_odstate(struct nfs4_clnt_odstate *co)
512 if (co)
513 atomic_inc(&co->co_odcount);
516 static void
517 put_clnt_odstate(struct nfs4_clnt_odstate *co)
519 struct nfs4_file *fp;
521 if (!co)
522 return;
524 fp = co->co_file;
525 if (atomic_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
526 list_del(&co->co_perfile);
527 spin_unlock(&fp->fi_lock);
529 nfsd4_return_all_file_layouts(co->co_client, fp);
530 kmem_cache_free(odstate_slab, co);
534 static struct nfs4_clnt_odstate *
535 find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
537 struct nfs4_clnt_odstate *co;
538 struct nfs4_client *cl;
540 if (!new)
541 return NULL;
543 cl = new->co_client;
545 spin_lock(&fp->fi_lock);
546 list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
547 if (co->co_client == cl) {
548 get_clnt_odstate(co);
549 goto out;
552 co = new;
553 co->co_file = fp;
554 hash_clnt_odstate_locked(new);
555 out:
556 spin_unlock(&fp->fi_lock);
557 return co;
560 struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab,
561 void (*sc_free)(struct nfs4_stid *))
563 struct nfs4_stid *stid;
564 int new_id;
566 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
567 if (!stid)
568 return NULL;
570 idr_preload(GFP_KERNEL);
571 spin_lock(&cl->cl_lock);
572 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 0, 0, GFP_NOWAIT);
573 spin_unlock(&cl->cl_lock);
574 idr_preload_end();
575 if (new_id < 0)
576 goto out_free;
578 stid->sc_free = sc_free;
579 stid->sc_client = cl;
580 stid->sc_stateid.si_opaque.so_id = new_id;
581 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
582 /* Will be incremented before return to client: */
583 atomic_set(&stid->sc_count, 1);
584 spin_lock_init(&stid->sc_lock);
587 * It shouldn't be a problem to reuse an opaque stateid value.
588 * I don't think it is for 4.1. But with 4.0 I worry that, for
589 * example, a stray write retransmission could be accepted by
590 * the server when it should have been rejected. Therefore,
591 * adopt a trick from the sctp code to attempt to maximize the
592 * amount of time until an id is reused, by ensuring they always
593 * "increase" (mod INT_MAX):
595 return stid;
596 out_free:
597 kmem_cache_free(slab, stid);
598 return NULL;
601 static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
603 struct nfs4_stid *stid;
605 stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid);
606 if (!stid)
607 return NULL;
609 return openlockstateid(stid);
612 static void nfs4_free_deleg(struct nfs4_stid *stid)
614 kmem_cache_free(deleg_slab, stid);
615 atomic_long_dec(&num_delegations);
619 * When we recall a delegation, we should be careful not to hand it
620 * out again straight away.
621 * To ensure this we keep a pair of bloom filters ('new' and 'old')
622 * in which the filehandles of recalled delegations are "stored".
623 * If a filehandle appear in either filter, a delegation is blocked.
624 * When a delegation is recalled, the filehandle is stored in the "new"
625 * filter.
626 * Every 30 seconds we swap the filters and clear the "new" one,
627 * unless both are empty of course.
629 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
630 * low 3 bytes as hash-table indices.
632 * 'blocked_delegations_lock', which is always taken in block_delegations(),
633 * is used to manage concurrent access. Testing does not need the lock
634 * except when swapping the two filters.
636 static DEFINE_SPINLOCK(blocked_delegations_lock);
637 static struct bloom_pair {
638 int entries, old_entries;
639 time_t swap_time;
640 int new; /* index into 'set' */
641 DECLARE_BITMAP(set[2], 256);
642 } blocked_delegations;
644 static int delegation_blocked(struct knfsd_fh *fh)
646 u32 hash;
647 struct bloom_pair *bd = &blocked_delegations;
649 if (bd->entries == 0)
650 return 0;
651 if (seconds_since_boot() - bd->swap_time > 30) {
652 spin_lock(&blocked_delegations_lock);
653 if (seconds_since_boot() - bd->swap_time > 30) {
654 bd->entries -= bd->old_entries;
655 bd->old_entries = bd->entries;
656 memset(bd->set[bd->new], 0,
657 sizeof(bd->set[0]));
658 bd->new = 1-bd->new;
659 bd->swap_time = seconds_since_boot();
661 spin_unlock(&blocked_delegations_lock);
663 hash = jhash(&fh->fh_base, fh->fh_size, 0);
664 if (test_bit(hash&255, bd->set[0]) &&
665 test_bit((hash>>8)&255, bd->set[0]) &&
666 test_bit((hash>>16)&255, bd->set[0]))
667 return 1;
669 if (test_bit(hash&255, bd->set[1]) &&
670 test_bit((hash>>8)&255, bd->set[1]) &&
671 test_bit((hash>>16)&255, bd->set[1]))
672 return 1;
674 return 0;
677 static void block_delegations(struct knfsd_fh *fh)
679 u32 hash;
680 struct bloom_pair *bd = &blocked_delegations;
682 hash = jhash(&fh->fh_base, fh->fh_size, 0);
684 spin_lock(&blocked_delegations_lock);
685 __set_bit(hash&255, bd->set[bd->new]);
686 __set_bit((hash>>8)&255, bd->set[bd->new]);
687 __set_bit((hash>>16)&255, bd->set[bd->new]);
688 if (bd->entries == 0)
689 bd->swap_time = seconds_since_boot();
690 bd->entries += 1;
691 spin_unlock(&blocked_delegations_lock);
694 static struct nfs4_delegation *
695 alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh,
696 struct nfs4_clnt_odstate *odstate)
698 struct nfs4_delegation *dp;
699 long n;
701 dprintk("NFSD alloc_init_deleg\n");
702 n = atomic_long_inc_return(&num_delegations);
703 if (n < 0 || n > max_delegations)
704 goto out_dec;
705 if (delegation_blocked(&current_fh->fh_handle))
706 goto out_dec;
707 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg));
708 if (dp == NULL)
709 goto out_dec;
712 * delegation seqid's are never incremented. The 4.1 special
713 * meaning of seqid 0 isn't meaningful, really, but let's avoid
714 * 0 anyway just for consistency and use 1:
716 dp->dl_stid.sc_stateid.si_generation = 1;
717 INIT_LIST_HEAD(&dp->dl_perfile);
718 INIT_LIST_HEAD(&dp->dl_perclnt);
719 INIT_LIST_HEAD(&dp->dl_recall_lru);
720 dp->dl_clnt_odstate = odstate;
721 get_clnt_odstate(odstate);
722 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
723 dp->dl_retries = 1;
724 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
725 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
726 return dp;
727 out_dec:
728 atomic_long_dec(&num_delegations);
729 return NULL;
732 void
733 nfs4_put_stid(struct nfs4_stid *s)
735 struct nfs4_file *fp = s->sc_file;
736 struct nfs4_client *clp = s->sc_client;
738 might_lock(&clp->cl_lock);
740 if (!atomic_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
741 wake_up_all(&close_wq);
742 return;
744 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
745 spin_unlock(&clp->cl_lock);
746 s->sc_free(s);
747 if (fp)
748 put_nfs4_file(fp);
751 void
752 nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
754 stateid_t *src = &stid->sc_stateid;
756 spin_lock(&stid->sc_lock);
757 if (unlikely(++src->si_generation == 0))
758 src->si_generation = 1;
759 memcpy(dst, src, sizeof(*dst));
760 spin_unlock(&stid->sc_lock);
763 static void nfs4_put_deleg_lease(struct nfs4_file *fp)
765 struct file *filp = NULL;
767 spin_lock(&fp->fi_lock);
768 if (fp->fi_deleg_file && --fp->fi_delegees == 0)
769 swap(filp, fp->fi_deleg_file);
770 spin_unlock(&fp->fi_lock);
772 if (filp) {
773 vfs_setlease(filp, F_UNLCK, NULL, (void **)&fp);
774 fput(filp);
778 void nfs4_unhash_stid(struct nfs4_stid *s)
780 s->sc_type = 0;
784 * nfs4_get_existing_delegation - Discover if this delegation already exists
785 * @clp: a pointer to the nfs4_client we're granting a delegation to
786 * @fp: a pointer to the nfs4_file we're granting a delegation on
788 * Return:
789 * On success: NULL if an existing delegation was not found.
791 * On error: -EAGAIN if one was previously granted to this nfs4_client
792 * for this nfs4_file.
796 static int
797 nfs4_get_existing_delegation(struct nfs4_client *clp, struct nfs4_file *fp)
799 struct nfs4_delegation *searchdp = NULL;
800 struct nfs4_client *searchclp = NULL;
802 lockdep_assert_held(&state_lock);
803 lockdep_assert_held(&fp->fi_lock);
805 list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
806 searchclp = searchdp->dl_stid.sc_client;
807 if (clp == searchclp) {
808 return -EAGAIN;
811 return 0;
815 * hash_delegation_locked - Add a delegation to the appropriate lists
816 * @dp: a pointer to the nfs4_delegation we are adding.
817 * @fp: a pointer to the nfs4_file we're granting a delegation on
819 * Return:
820 * On success: NULL if the delegation was successfully hashed.
822 * On error: -EAGAIN if one was previously granted to this
823 * nfs4_client for this nfs4_file. Delegation is not hashed.
827 static int
828 hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
830 int status;
831 struct nfs4_client *clp = dp->dl_stid.sc_client;
833 lockdep_assert_held(&state_lock);
834 lockdep_assert_held(&fp->fi_lock);
836 status = nfs4_get_existing_delegation(clp, fp);
837 if (status)
838 return status;
839 ++fp->fi_delegees;
840 atomic_inc(&dp->dl_stid.sc_count);
841 dp->dl_stid.sc_type = NFS4_DELEG_STID;
842 list_add(&dp->dl_perfile, &fp->fi_delegations);
843 list_add(&dp->dl_perclnt, &clp->cl_delegations);
844 return 0;
847 static bool
848 unhash_delegation_locked(struct nfs4_delegation *dp)
850 struct nfs4_file *fp = dp->dl_stid.sc_file;
852 lockdep_assert_held(&state_lock);
854 if (list_empty(&dp->dl_perfile))
855 return false;
857 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
858 /* Ensure that deleg break won't try to requeue it */
859 ++dp->dl_time;
860 spin_lock(&fp->fi_lock);
861 list_del_init(&dp->dl_perclnt);
862 list_del_init(&dp->dl_recall_lru);
863 list_del_init(&dp->dl_perfile);
864 spin_unlock(&fp->fi_lock);
865 return true;
868 static void destroy_delegation(struct nfs4_delegation *dp)
870 bool unhashed;
872 spin_lock(&state_lock);
873 unhashed = unhash_delegation_locked(dp);
874 spin_unlock(&state_lock);
875 if (unhashed) {
876 put_clnt_odstate(dp->dl_clnt_odstate);
877 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
878 nfs4_put_stid(&dp->dl_stid);
882 static void revoke_delegation(struct nfs4_delegation *dp)
884 struct nfs4_client *clp = dp->dl_stid.sc_client;
886 WARN_ON(!list_empty(&dp->dl_recall_lru));
888 put_clnt_odstate(dp->dl_clnt_odstate);
889 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
891 if (clp->cl_minorversion == 0)
892 nfs4_put_stid(&dp->dl_stid);
893 else {
894 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
895 spin_lock(&clp->cl_lock);
896 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
897 spin_unlock(&clp->cl_lock);
902 * SETCLIENTID state
905 static unsigned int clientid_hashval(u32 id)
907 return id & CLIENT_HASH_MASK;
910 static unsigned int clientstr_hashval(const char *name)
912 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
916 * We store the NONE, READ, WRITE, and BOTH bits separately in the
917 * st_{access,deny}_bmap field of the stateid, in order to track not
918 * only what share bits are currently in force, but also what
919 * combinations of share bits previous opens have used. This allows us
920 * to enforce the recommendation of rfc 3530 14.2.19 that the server
921 * return an error if the client attempt to downgrade to a combination
922 * of share bits not explicable by closing some of its previous opens.
924 * XXX: This enforcement is actually incomplete, since we don't keep
925 * track of access/deny bit combinations; so, e.g., we allow:
927 * OPEN allow read, deny write
928 * OPEN allow both, deny none
929 * DOWNGRADE allow read, deny none
931 * which we should reject.
933 static unsigned int
934 bmap_to_share_mode(unsigned long bmap) {
935 int i;
936 unsigned int access = 0;
938 for (i = 1; i < 4; i++) {
939 if (test_bit(i, &bmap))
940 access |= i;
942 return access;
945 /* set share access for a given stateid */
946 static inline void
947 set_access(u32 access, struct nfs4_ol_stateid *stp)
949 unsigned char mask = 1 << access;
951 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
952 stp->st_access_bmap |= mask;
955 /* clear share access for a given stateid */
956 static inline void
957 clear_access(u32 access, struct nfs4_ol_stateid *stp)
959 unsigned char mask = 1 << access;
961 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
962 stp->st_access_bmap &= ~mask;
965 /* test whether a given stateid has access */
966 static inline bool
967 test_access(u32 access, struct nfs4_ol_stateid *stp)
969 unsigned char mask = 1 << access;
971 return (bool)(stp->st_access_bmap & mask);
974 /* set share deny for a given stateid */
975 static inline void
976 set_deny(u32 deny, struct nfs4_ol_stateid *stp)
978 unsigned char mask = 1 << deny;
980 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
981 stp->st_deny_bmap |= mask;
984 /* clear share deny for a given stateid */
985 static inline void
986 clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
988 unsigned char mask = 1 << deny;
990 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
991 stp->st_deny_bmap &= ~mask;
994 /* test whether a given stateid is denying specific access */
995 static inline bool
996 test_deny(u32 deny, struct nfs4_ol_stateid *stp)
998 unsigned char mask = 1 << deny;
1000 return (bool)(stp->st_deny_bmap & mask);
1003 static int nfs4_access_to_omode(u32 access)
1005 switch (access & NFS4_SHARE_ACCESS_BOTH) {
1006 case NFS4_SHARE_ACCESS_READ:
1007 return O_RDONLY;
1008 case NFS4_SHARE_ACCESS_WRITE:
1009 return O_WRONLY;
1010 case NFS4_SHARE_ACCESS_BOTH:
1011 return O_RDWR;
1013 WARN_ON_ONCE(1);
1014 return O_RDONLY;
1018 * A stateid that had a deny mode associated with it is being released
1019 * or downgraded. Recalculate the deny mode on the file.
1021 static void
1022 recalculate_deny_mode(struct nfs4_file *fp)
1024 struct nfs4_ol_stateid *stp;
1026 spin_lock(&fp->fi_lock);
1027 fp->fi_share_deny = 0;
1028 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
1029 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
1030 spin_unlock(&fp->fi_lock);
1033 static void
1034 reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
1036 int i;
1037 bool change = false;
1039 for (i = 1; i < 4; i++) {
1040 if ((i & deny) != i) {
1041 change = true;
1042 clear_deny(i, stp);
1046 /* Recalculate per-file deny mode if there was a change */
1047 if (change)
1048 recalculate_deny_mode(stp->st_stid.sc_file);
1051 /* release all access and file references for a given stateid */
1052 static void
1053 release_all_access(struct nfs4_ol_stateid *stp)
1055 int i;
1056 struct nfs4_file *fp = stp->st_stid.sc_file;
1058 if (fp && stp->st_deny_bmap != 0)
1059 recalculate_deny_mode(fp);
1061 for (i = 1; i < 4; i++) {
1062 if (test_access(i, stp))
1063 nfs4_file_put_access(stp->st_stid.sc_file, i);
1064 clear_access(i, stp);
1068 static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1070 kfree(sop->so_owner.data);
1071 sop->so_ops->so_free(sop);
1074 static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1076 struct nfs4_client *clp = sop->so_client;
1078 might_lock(&clp->cl_lock);
1080 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
1081 return;
1082 sop->so_ops->so_unhash(sop);
1083 spin_unlock(&clp->cl_lock);
1084 nfs4_free_stateowner(sop);
1087 static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
1089 struct nfs4_file *fp = stp->st_stid.sc_file;
1091 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1093 if (list_empty(&stp->st_perfile))
1094 return false;
1096 spin_lock(&fp->fi_lock);
1097 list_del_init(&stp->st_perfile);
1098 spin_unlock(&fp->fi_lock);
1099 list_del(&stp->st_perstateowner);
1100 return true;
1103 static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
1105 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1107 put_clnt_odstate(stp->st_clnt_odstate);
1108 release_all_access(stp);
1109 if (stp->st_stateowner)
1110 nfs4_put_stateowner(stp->st_stateowner);
1111 kmem_cache_free(stateid_slab, stid);
1114 static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
1116 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1117 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
1118 struct file *file;
1120 file = find_any_file(stp->st_stid.sc_file);
1121 if (file)
1122 filp_close(file, (fl_owner_t)lo);
1123 nfs4_free_ol_stateid(stid);
1127 * Put the persistent reference to an already unhashed generic stateid, while
1128 * holding the cl_lock. If it's the last reference, then put it onto the
1129 * reaplist for later destruction.
1131 static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1132 struct list_head *reaplist)
1134 struct nfs4_stid *s = &stp->st_stid;
1135 struct nfs4_client *clp = s->sc_client;
1137 lockdep_assert_held(&clp->cl_lock);
1139 WARN_ON_ONCE(!list_empty(&stp->st_locks));
1141 if (!atomic_dec_and_test(&s->sc_count)) {
1142 wake_up_all(&close_wq);
1143 return;
1146 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1147 list_add(&stp->st_locks, reaplist);
1150 static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
1152 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1154 list_del_init(&stp->st_locks);
1155 nfs4_unhash_stid(&stp->st_stid);
1156 return unhash_ol_stateid(stp);
1159 static void release_lock_stateid(struct nfs4_ol_stateid *stp)
1161 struct nfs4_client *clp = stp->st_stid.sc_client;
1162 bool unhashed;
1164 spin_lock(&clp->cl_lock);
1165 unhashed = unhash_lock_stateid(stp);
1166 spin_unlock(&clp->cl_lock);
1167 if (unhashed)
1168 nfs4_put_stid(&stp->st_stid);
1171 static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
1173 struct nfs4_client *clp = lo->lo_owner.so_client;
1175 lockdep_assert_held(&clp->cl_lock);
1177 list_del_init(&lo->lo_owner.so_strhash);
1181 * Free a list of generic stateids that were collected earlier after being
1182 * fully unhashed.
1184 static void
1185 free_ol_stateid_reaplist(struct list_head *reaplist)
1187 struct nfs4_ol_stateid *stp;
1188 struct nfs4_file *fp;
1190 might_sleep();
1192 while (!list_empty(reaplist)) {
1193 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1194 st_locks);
1195 list_del(&stp->st_locks);
1196 fp = stp->st_stid.sc_file;
1197 stp->st_stid.sc_free(&stp->st_stid);
1198 if (fp)
1199 put_nfs4_file(fp);
1203 static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1204 struct list_head *reaplist)
1206 struct nfs4_ol_stateid *stp;
1208 lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1210 while (!list_empty(&open_stp->st_locks)) {
1211 stp = list_entry(open_stp->st_locks.next,
1212 struct nfs4_ol_stateid, st_locks);
1213 WARN_ON(!unhash_lock_stateid(stp));
1214 put_ol_stateid_locked(stp, reaplist);
1218 static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
1219 struct list_head *reaplist)
1221 bool unhashed;
1223 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1225 unhashed = unhash_ol_stateid(stp);
1226 release_open_stateid_locks(stp, reaplist);
1227 return unhashed;
1230 static void release_open_stateid(struct nfs4_ol_stateid *stp)
1232 LIST_HEAD(reaplist);
1234 spin_lock(&stp->st_stid.sc_client->cl_lock);
1235 if (unhash_open_stateid(stp, &reaplist))
1236 put_ol_stateid_locked(stp, &reaplist);
1237 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1238 free_ol_stateid_reaplist(&reaplist);
1241 static void unhash_openowner_locked(struct nfs4_openowner *oo)
1243 struct nfs4_client *clp = oo->oo_owner.so_client;
1245 lockdep_assert_held(&clp->cl_lock);
1247 list_del_init(&oo->oo_owner.so_strhash);
1248 list_del_init(&oo->oo_perclient);
1251 static void release_last_closed_stateid(struct nfs4_openowner *oo)
1253 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1254 nfsd_net_id);
1255 struct nfs4_ol_stateid *s;
1257 spin_lock(&nn->client_lock);
1258 s = oo->oo_last_closed_stid;
1259 if (s) {
1260 list_del_init(&oo->oo_close_lru);
1261 oo->oo_last_closed_stid = NULL;
1263 spin_unlock(&nn->client_lock);
1264 if (s)
1265 nfs4_put_stid(&s->st_stid);
1268 static void release_openowner(struct nfs4_openowner *oo)
1270 struct nfs4_ol_stateid *stp;
1271 struct nfs4_client *clp = oo->oo_owner.so_client;
1272 struct list_head reaplist;
1274 INIT_LIST_HEAD(&reaplist);
1276 spin_lock(&clp->cl_lock);
1277 unhash_openowner_locked(oo);
1278 while (!list_empty(&oo->oo_owner.so_stateids)) {
1279 stp = list_first_entry(&oo->oo_owner.so_stateids,
1280 struct nfs4_ol_stateid, st_perstateowner);
1281 if (unhash_open_stateid(stp, &reaplist))
1282 put_ol_stateid_locked(stp, &reaplist);
1284 spin_unlock(&clp->cl_lock);
1285 free_ol_stateid_reaplist(&reaplist);
1286 release_last_closed_stateid(oo);
1287 nfs4_put_stateowner(&oo->oo_owner);
1290 static inline int
1291 hash_sessionid(struct nfs4_sessionid *sessionid)
1293 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1295 return sid->sequence % SESSION_HASH_SIZE;
1298 #ifdef CONFIG_SUNRPC_DEBUG
1299 static inline void
1300 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1302 u32 *ptr = (u32 *)(&sessionid->data[0]);
1303 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1305 #else
1306 static inline void
1307 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1310 #endif
1313 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1314 * won't be used for replay.
1316 void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1318 struct nfs4_stateowner *so = cstate->replay_owner;
1320 if (nfserr == nfserr_replay_me)
1321 return;
1323 if (!seqid_mutating_err(ntohl(nfserr))) {
1324 nfsd4_cstate_clear_replay(cstate);
1325 return;
1327 if (!so)
1328 return;
1329 if (so->so_is_open_owner)
1330 release_last_closed_stateid(openowner(so));
1331 so->so_seqid++;
1332 return;
1335 static void
1336 gen_sessionid(struct nfsd4_session *ses)
1338 struct nfs4_client *clp = ses->se_client;
1339 struct nfsd4_sessionid *sid;
1341 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1342 sid->clientid = clp->cl_clientid;
1343 sid->sequence = current_sessionid++;
1344 sid->reserved = 0;
1348 * The protocol defines ca_maxresponssize_cached to include the size of
1349 * the rpc header, but all we need to cache is the data starting after
1350 * the end of the initial SEQUENCE operation--the rest we regenerate
1351 * each time. Therefore we can advertise a ca_maxresponssize_cached
1352 * value that is the number of bytes in our cache plus a few additional
1353 * bytes. In order to stay on the safe side, and not promise more than
1354 * we can cache, those additional bytes must be the minimum possible: 24
1355 * bytes of rpc header (xid through accept state, with AUTH_NULL
1356 * verifier), 12 for the compound header (with zero-length tag), and 44
1357 * for the SEQUENCE op response:
1359 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1361 static void
1362 free_session_slots(struct nfsd4_session *ses)
1364 int i;
1366 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1367 kfree(ses->se_slots[i]);
1371 * We don't actually need to cache the rpc and session headers, so we
1372 * can allocate a little less for each slot:
1374 static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
1376 u32 size;
1378 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1379 size = 0;
1380 else
1381 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1382 return size + sizeof(struct nfsd4_slot);
1386 * XXX: If we run out of reserved DRC memory we could (up to a point)
1387 * re-negotiate active sessions and reduce their slot usage to make
1388 * room for new connections. For now we just fail the create session.
1390 static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
1392 u32 slotsize = slot_bytes(ca);
1393 u32 num = ca->maxreqs;
1394 int avail;
1396 spin_lock(&nfsd_drc_lock);
1397 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1398 nfsd_drc_max_mem - nfsd_drc_mem_used);
1399 num = min_t(int, num, avail / slotsize);
1400 nfsd_drc_mem_used += num * slotsize;
1401 spin_unlock(&nfsd_drc_lock);
1403 return num;
1406 static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
1408 int slotsize = slot_bytes(ca);
1410 spin_lock(&nfsd_drc_lock);
1411 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
1412 spin_unlock(&nfsd_drc_lock);
1415 static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1416 struct nfsd4_channel_attrs *battrs)
1418 int numslots = fattrs->maxreqs;
1419 int slotsize = slot_bytes(fattrs);
1420 struct nfsd4_session *new;
1421 int mem, i;
1423 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
1424 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1425 mem = numslots * sizeof(struct nfsd4_slot *);
1427 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
1428 if (!new)
1429 return NULL;
1430 /* allocate each struct nfsd4_slot and data cache in one piece */
1431 for (i = 0; i < numslots; i++) {
1432 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
1433 if (!new->se_slots[i])
1434 goto out_free;
1437 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1438 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1440 return new;
1441 out_free:
1442 while (i--)
1443 kfree(new->se_slots[i]);
1444 kfree(new);
1445 return NULL;
1448 static void free_conn(struct nfsd4_conn *c)
1450 svc_xprt_put(c->cn_xprt);
1451 kfree(c);
1454 static void nfsd4_conn_lost(struct svc_xpt_user *u)
1456 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1457 struct nfs4_client *clp = c->cn_session->se_client;
1459 spin_lock(&clp->cl_lock);
1460 if (!list_empty(&c->cn_persession)) {
1461 list_del(&c->cn_persession);
1462 free_conn(c);
1464 nfsd4_probe_callback(clp);
1465 spin_unlock(&clp->cl_lock);
1468 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
1470 struct nfsd4_conn *conn;
1472 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1473 if (!conn)
1474 return NULL;
1475 svc_xprt_get(rqstp->rq_xprt);
1476 conn->cn_xprt = rqstp->rq_xprt;
1477 conn->cn_flags = flags;
1478 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1479 return conn;
1482 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1484 conn->cn_session = ses;
1485 list_add(&conn->cn_persession, &ses->se_conns);
1488 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1490 struct nfs4_client *clp = ses->se_client;
1492 spin_lock(&clp->cl_lock);
1493 __nfsd4_hash_conn(conn, ses);
1494 spin_unlock(&clp->cl_lock);
1497 static int nfsd4_register_conn(struct nfsd4_conn *conn)
1499 conn->cn_xpt_user.callback = nfsd4_conn_lost;
1500 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
1503 static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
1505 int ret;
1507 nfsd4_hash_conn(conn, ses);
1508 ret = nfsd4_register_conn(conn);
1509 if (ret)
1510 /* oops; xprt is already down: */
1511 nfsd4_conn_lost(&conn->cn_xpt_user);
1512 /* We may have gained or lost a callback channel: */
1513 nfsd4_probe_callback_sync(ses->se_client);
1516 static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
1518 u32 dir = NFS4_CDFC4_FORE;
1520 if (cses->flags & SESSION4_BACK_CHAN)
1521 dir |= NFS4_CDFC4_BACK;
1522 return alloc_conn(rqstp, dir);
1525 /* must be called under client_lock */
1526 static void nfsd4_del_conns(struct nfsd4_session *s)
1528 struct nfs4_client *clp = s->se_client;
1529 struct nfsd4_conn *c;
1531 spin_lock(&clp->cl_lock);
1532 while (!list_empty(&s->se_conns)) {
1533 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1534 list_del_init(&c->cn_persession);
1535 spin_unlock(&clp->cl_lock);
1537 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1538 free_conn(c);
1540 spin_lock(&clp->cl_lock);
1542 spin_unlock(&clp->cl_lock);
1545 static void __free_session(struct nfsd4_session *ses)
1547 free_session_slots(ses);
1548 kfree(ses);
1551 static void free_session(struct nfsd4_session *ses)
1553 nfsd4_del_conns(ses);
1554 nfsd4_put_drc_mem(&ses->se_fchannel);
1555 __free_session(ses);
1558 static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
1560 int idx;
1561 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1563 new->se_client = clp;
1564 gen_sessionid(new);
1566 INIT_LIST_HEAD(&new->se_conns);
1568 new->se_cb_seq_nr = 1;
1569 new->se_flags = cses->flags;
1570 new->se_cb_prog = cses->callback_prog;
1571 new->se_cb_sec = cses->cb_sec;
1572 atomic_set(&new->se_ref, 0);
1573 idx = hash_sessionid(&new->se_sessionid);
1574 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
1575 spin_lock(&clp->cl_lock);
1576 list_add(&new->se_perclnt, &clp->cl_sessions);
1577 spin_unlock(&clp->cl_lock);
1580 struct sockaddr *sa = svc_addr(rqstp);
1582 * This is a little silly; with sessions there's no real
1583 * use for the callback address. Use the peer address
1584 * as a reasonable default for now, but consider fixing
1585 * the rpc client not to require an address in the
1586 * future:
1588 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1589 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
1593 /* caller must hold client_lock */
1594 static struct nfsd4_session *
1595 __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
1597 struct nfsd4_session *elem;
1598 int idx;
1599 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1601 lockdep_assert_held(&nn->client_lock);
1603 dump_sessionid(__func__, sessionid);
1604 idx = hash_sessionid(sessionid);
1605 /* Search in the appropriate list */
1606 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
1607 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1608 NFS4_MAX_SESSIONID_LEN)) {
1609 return elem;
1613 dprintk("%s: session not found\n", __func__);
1614 return NULL;
1617 static struct nfsd4_session *
1618 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1619 __be32 *ret)
1621 struct nfsd4_session *session;
1622 __be32 status = nfserr_badsession;
1624 session = __find_in_sessionid_hashtbl(sessionid, net);
1625 if (!session)
1626 goto out;
1627 status = nfsd4_get_session_locked(session);
1628 if (status)
1629 session = NULL;
1630 out:
1631 *ret = status;
1632 return session;
1635 /* caller must hold client_lock */
1636 static void
1637 unhash_session(struct nfsd4_session *ses)
1639 struct nfs4_client *clp = ses->se_client;
1640 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1642 lockdep_assert_held(&nn->client_lock);
1644 list_del(&ses->se_hash);
1645 spin_lock(&ses->se_client->cl_lock);
1646 list_del(&ses->se_perclnt);
1647 spin_unlock(&ses->se_client->cl_lock);
1650 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1651 static int
1652 STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
1655 * We're assuming the clid was not given out from a boot
1656 * precisely 2^32 (about 136 years) before this one. That seems
1657 * a safe assumption:
1659 if (clid->cl_boot == (u32)nn->boot_time)
1660 return 0;
1661 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
1662 clid->cl_boot, clid->cl_id, nn->boot_time);
1663 return 1;
1667 * XXX Should we use a slab cache ?
1668 * This type of memory management is somewhat inefficient, but we use it
1669 * anyway since SETCLIENTID is not a common operation.
1671 static struct nfs4_client *alloc_client(struct xdr_netobj name)
1673 struct nfs4_client *clp;
1674 int i;
1676 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1677 if (clp == NULL)
1678 return NULL;
1679 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
1680 if (clp->cl_name.data == NULL)
1681 goto err_no_name;
1682 clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
1683 OWNER_HASH_SIZE, GFP_KERNEL);
1684 if (!clp->cl_ownerstr_hashtbl)
1685 goto err_no_hashtbl;
1686 for (i = 0; i < OWNER_HASH_SIZE; i++)
1687 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
1688 clp->cl_name.len = name.len;
1689 INIT_LIST_HEAD(&clp->cl_sessions);
1690 idr_init(&clp->cl_stateids);
1691 atomic_set(&clp->cl_refcount, 0);
1692 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1693 INIT_LIST_HEAD(&clp->cl_idhash);
1694 INIT_LIST_HEAD(&clp->cl_openowners);
1695 INIT_LIST_HEAD(&clp->cl_delegations);
1696 INIT_LIST_HEAD(&clp->cl_lru);
1697 INIT_LIST_HEAD(&clp->cl_revoked);
1698 #ifdef CONFIG_NFSD_PNFS
1699 INIT_LIST_HEAD(&clp->cl_lo_states);
1700 #endif
1701 spin_lock_init(&clp->cl_lock);
1702 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1703 return clp;
1704 err_no_hashtbl:
1705 kfree(clp->cl_name.data);
1706 err_no_name:
1707 kfree(clp);
1708 return NULL;
1711 static void
1712 free_client(struct nfs4_client *clp)
1714 while (!list_empty(&clp->cl_sessions)) {
1715 struct nfsd4_session *ses;
1716 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1717 se_perclnt);
1718 list_del(&ses->se_perclnt);
1719 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1720 free_session(ses);
1722 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
1723 free_svc_cred(&clp->cl_cred);
1724 kfree(clp->cl_ownerstr_hashtbl);
1725 kfree(clp->cl_name.data);
1726 idr_destroy(&clp->cl_stateids);
1727 kfree(clp);
1730 /* must be called under the client_lock */
1731 static void
1732 unhash_client_locked(struct nfs4_client *clp)
1734 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1735 struct nfsd4_session *ses;
1737 lockdep_assert_held(&nn->client_lock);
1739 /* Mark the client as expired! */
1740 clp->cl_time = 0;
1741 /* Make it invisible */
1742 if (!list_empty(&clp->cl_idhash)) {
1743 list_del_init(&clp->cl_idhash);
1744 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1745 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1746 else
1747 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1749 list_del_init(&clp->cl_lru);
1750 spin_lock(&clp->cl_lock);
1751 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1752 list_del_init(&ses->se_hash);
1753 spin_unlock(&clp->cl_lock);
1756 static void
1757 unhash_client(struct nfs4_client *clp)
1759 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1761 spin_lock(&nn->client_lock);
1762 unhash_client_locked(clp);
1763 spin_unlock(&nn->client_lock);
1766 static __be32 mark_client_expired_locked(struct nfs4_client *clp)
1768 if (atomic_read(&clp->cl_refcount))
1769 return nfserr_jukebox;
1770 unhash_client_locked(clp);
1771 return nfs_ok;
1774 static void
1775 __destroy_client(struct nfs4_client *clp)
1777 struct nfs4_openowner *oo;
1778 struct nfs4_delegation *dp;
1779 struct list_head reaplist;
1781 INIT_LIST_HEAD(&reaplist);
1782 spin_lock(&state_lock);
1783 while (!list_empty(&clp->cl_delegations)) {
1784 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
1785 WARN_ON(!unhash_delegation_locked(dp));
1786 list_add(&dp->dl_recall_lru, &reaplist);
1788 spin_unlock(&state_lock);
1789 while (!list_empty(&reaplist)) {
1790 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
1791 list_del_init(&dp->dl_recall_lru);
1792 put_clnt_odstate(dp->dl_clnt_odstate);
1793 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
1794 nfs4_put_stid(&dp->dl_stid);
1796 while (!list_empty(&clp->cl_revoked)) {
1797 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
1798 list_del_init(&dp->dl_recall_lru);
1799 nfs4_put_stid(&dp->dl_stid);
1801 while (!list_empty(&clp->cl_openowners)) {
1802 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1803 nfs4_get_stateowner(&oo->oo_owner);
1804 release_openowner(oo);
1806 nfsd4_return_all_client_layouts(clp);
1807 nfsd4_shutdown_callback(clp);
1808 if (clp->cl_cb_conn.cb_xprt)
1809 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
1810 free_client(clp);
1813 static void
1814 destroy_client(struct nfs4_client *clp)
1816 unhash_client(clp);
1817 __destroy_client(clp);
1820 static void expire_client(struct nfs4_client *clp)
1822 unhash_client(clp);
1823 nfsd4_client_record_remove(clp);
1824 __destroy_client(clp);
1827 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1829 memcpy(target->cl_verifier.data, source->data,
1830 sizeof(target->cl_verifier.data));
1833 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1835 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1836 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1839 static int copy_cred(struct svc_cred *target, struct svc_cred *source)
1841 if (source->cr_principal) {
1842 target->cr_principal =
1843 kstrdup(source->cr_principal, GFP_KERNEL);
1844 if (target->cr_principal == NULL)
1845 return -ENOMEM;
1846 } else
1847 target->cr_principal = NULL;
1848 target->cr_flavor = source->cr_flavor;
1849 target->cr_uid = source->cr_uid;
1850 target->cr_gid = source->cr_gid;
1851 target->cr_group_info = source->cr_group_info;
1852 get_group_info(target->cr_group_info);
1853 target->cr_gss_mech = source->cr_gss_mech;
1854 if (source->cr_gss_mech)
1855 gss_mech_get(source->cr_gss_mech);
1856 return 0;
1859 static int
1860 compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1862 if (o1->len < o2->len)
1863 return -1;
1864 if (o1->len > o2->len)
1865 return 1;
1866 return memcmp(o1->data, o2->data, o1->len);
1869 static int same_name(const char *n1, const char *n2)
1871 return 0 == memcmp(n1, n2, HEXDIR_LEN);
1874 static int
1875 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1877 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
1880 static int
1881 same_clid(clientid_t *cl1, clientid_t *cl2)
1883 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
1886 static bool groups_equal(struct group_info *g1, struct group_info *g2)
1888 int i;
1890 if (g1->ngroups != g2->ngroups)
1891 return false;
1892 for (i=0; i<g1->ngroups; i++)
1893 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
1894 return false;
1895 return true;
1899 * RFC 3530 language requires clid_inuse be returned when the
1900 * "principal" associated with a requests differs from that previously
1901 * used. We use uid, gid's, and gss principal string as our best
1902 * approximation. We also don't want to allow non-gss use of a client
1903 * established using gss: in theory cr_principal should catch that
1904 * change, but in practice cr_principal can be null even in the gss case
1905 * since gssd doesn't always pass down a principal string.
1907 static bool is_gss_cred(struct svc_cred *cr)
1909 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1910 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1914 static bool
1915 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1917 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
1918 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1919 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
1920 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1921 return false;
1922 if (cr1->cr_principal == cr2->cr_principal)
1923 return true;
1924 if (!cr1->cr_principal || !cr2->cr_principal)
1925 return false;
1926 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
1929 static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1931 struct svc_cred *cr = &rqstp->rq_cred;
1932 u32 service;
1934 if (!cr->cr_gss_mech)
1935 return false;
1936 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1937 return service == RPC_GSS_SVC_INTEGRITY ||
1938 service == RPC_GSS_SVC_PRIVACY;
1941 static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1943 struct svc_cred *cr = &rqstp->rq_cred;
1945 if (!cl->cl_mach_cred)
1946 return true;
1947 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1948 return false;
1949 if (!svc_rqst_integrity_protected(rqstp))
1950 return false;
1951 if (!cr->cr_principal)
1952 return false;
1953 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1956 static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
1958 __be32 verf[2];
1961 * This is opaque to client, so no need to byte-swap. Use
1962 * __force to keep sparse happy
1964 verf[0] = (__force __be32)get_seconds();
1965 verf[1] = (__force __be32)nn->clverifier_counter++;
1966 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
1969 static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
1971 clp->cl_clientid.cl_boot = nn->boot_time;
1972 clp->cl_clientid.cl_id = nn->clientid_counter++;
1973 gen_confirm(clp, nn);
1976 static struct nfs4_stid *
1977 find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
1979 struct nfs4_stid *ret;
1981 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1982 if (!ret || !ret->sc_type)
1983 return NULL;
1984 return ret;
1987 static struct nfs4_stid *
1988 find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
1990 struct nfs4_stid *s;
1992 spin_lock(&cl->cl_lock);
1993 s = find_stateid_locked(cl, t);
1994 if (s != NULL) {
1995 if (typemask & s->sc_type)
1996 atomic_inc(&s->sc_count);
1997 else
1998 s = NULL;
2000 spin_unlock(&cl->cl_lock);
2001 return s;
2004 static struct nfs4_client *create_client(struct xdr_netobj name,
2005 struct svc_rqst *rqstp, nfs4_verifier *verf)
2007 struct nfs4_client *clp;
2008 struct sockaddr *sa = svc_addr(rqstp);
2009 int ret;
2010 struct net *net = SVC_NET(rqstp);
2012 clp = alloc_client(name);
2013 if (clp == NULL)
2014 return NULL;
2016 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
2017 if (ret) {
2018 free_client(clp);
2019 return NULL;
2021 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
2022 clp->cl_time = get_seconds();
2023 clear_bit(0, &clp->cl_cb_slot_busy);
2024 copy_verf(clp, verf);
2025 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
2026 clp->cl_cb_session = NULL;
2027 clp->net = net;
2028 return clp;
2031 static void
2032 add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
2034 struct rb_node **new = &(root->rb_node), *parent = NULL;
2035 struct nfs4_client *clp;
2037 while (*new) {
2038 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
2039 parent = *new;
2041 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
2042 new = &((*new)->rb_left);
2043 else
2044 new = &((*new)->rb_right);
2047 rb_link_node(&new_clp->cl_namenode, parent, new);
2048 rb_insert_color(&new_clp->cl_namenode, root);
2051 static struct nfs4_client *
2052 find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2054 int cmp;
2055 struct rb_node *node = root->rb_node;
2056 struct nfs4_client *clp;
2058 while (node) {
2059 clp = rb_entry(node, struct nfs4_client, cl_namenode);
2060 cmp = compare_blob(&clp->cl_name, name);
2061 if (cmp > 0)
2062 node = node->rb_left;
2063 else if (cmp < 0)
2064 node = node->rb_right;
2065 else
2066 return clp;
2068 return NULL;
2071 static void
2072 add_to_unconfirmed(struct nfs4_client *clp)
2074 unsigned int idhashval;
2075 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2077 lockdep_assert_held(&nn->client_lock);
2079 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2080 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
2081 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
2082 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
2083 renew_client_locked(clp);
2086 static void
2087 move_to_confirmed(struct nfs4_client *clp)
2089 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
2090 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2092 lockdep_assert_held(&nn->client_lock);
2094 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
2095 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
2096 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
2097 add_clp_to_name_tree(clp, &nn->conf_name_tree);
2098 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2099 renew_client_locked(clp);
2102 static struct nfs4_client *
2103 find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
2105 struct nfs4_client *clp;
2106 unsigned int idhashval = clientid_hashval(clid->cl_id);
2108 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
2109 if (same_clid(&clp->cl_clientid, clid)) {
2110 if ((bool)clp->cl_minorversion != sessions)
2111 return NULL;
2112 renew_client_locked(clp);
2113 return clp;
2116 return NULL;
2119 static struct nfs4_client *
2120 find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2122 struct list_head *tbl = nn->conf_id_hashtbl;
2124 lockdep_assert_held(&nn->client_lock);
2125 return find_client_in_id_table(tbl, clid, sessions);
2128 static struct nfs4_client *
2129 find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2131 struct list_head *tbl = nn->unconf_id_hashtbl;
2133 lockdep_assert_held(&nn->client_lock);
2134 return find_client_in_id_table(tbl, clid, sessions);
2137 static bool clp_used_exchangeid(struct nfs4_client *clp)
2139 return clp->cl_exchange_flags != 0;
2142 static struct nfs4_client *
2143 find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
2145 lockdep_assert_held(&nn->client_lock);
2146 return find_clp_in_name_tree(name, &nn->conf_name_tree);
2149 static struct nfs4_client *
2150 find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
2152 lockdep_assert_held(&nn->client_lock);
2153 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
2156 static void
2157 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
2159 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
2160 struct sockaddr *sa = svc_addr(rqstp);
2161 u32 scopeid = rpc_get_scope_id(sa);
2162 unsigned short expected_family;
2164 /* Currently, we only support tcp and tcp6 for the callback channel */
2165 if (se->se_callback_netid_len == 3 &&
2166 !memcmp(se->se_callback_netid_val, "tcp", 3))
2167 expected_family = AF_INET;
2168 else if (se->se_callback_netid_len == 4 &&
2169 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2170 expected_family = AF_INET6;
2171 else
2172 goto out_err;
2174 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
2175 se->se_callback_addr_len,
2176 (struct sockaddr *)&conn->cb_addr,
2177 sizeof(conn->cb_addr));
2179 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
2180 goto out_err;
2182 if (conn->cb_addr.ss_family == AF_INET6)
2183 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
2185 conn->cb_prog = se->se_callback_prog;
2186 conn->cb_ident = se->se_callback_ident;
2187 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
2188 return;
2189 out_err:
2190 conn->cb_addr.ss_family = AF_UNSPEC;
2191 conn->cb_addrlen = 0;
2192 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
2193 "will not receive delegations\n",
2194 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2196 return;
2200 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
2202 static void
2203 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2205 struct xdr_buf *buf = resp->xdr.buf;
2206 struct nfsd4_slot *slot = resp->cstate.slot;
2207 unsigned int base;
2209 dprintk("--> %s slot %p\n", __func__, slot);
2211 slot->sl_opcnt = resp->opcnt;
2212 slot->sl_status = resp->cstate.status;
2214 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
2215 if (nfsd4_not_cached(resp)) {
2216 slot->sl_datalen = 0;
2217 return;
2219 base = resp->cstate.data_offset;
2220 slot->sl_datalen = buf->len - base;
2221 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
2222 WARN("%s: sessions DRC could not cache compound\n", __func__);
2223 return;
2227 * Encode the replay sequence operation from the slot values.
2228 * If cachethis is FALSE encode the uncached rep error on the next
2229 * operation which sets resp->p and increments resp->opcnt for
2230 * nfs4svc_encode_compoundres.
2233 static __be32
2234 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2235 struct nfsd4_compoundres *resp)
2237 struct nfsd4_op *op;
2238 struct nfsd4_slot *slot = resp->cstate.slot;
2240 /* Encode the replayed sequence operation */
2241 op = &args->ops[resp->opcnt - 1];
2242 nfsd4_encode_operation(resp, op);
2244 /* Return nfserr_retry_uncached_rep in next operation. */
2245 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
2246 op = &args->ops[resp->opcnt++];
2247 op->status = nfserr_retry_uncached_rep;
2248 nfsd4_encode_operation(resp, op);
2250 return op->status;
2254 * The sequence operation is not cached because we can use the slot and
2255 * session values.
2257 static __be32
2258 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2259 struct nfsd4_sequence *seq)
2261 struct nfsd4_slot *slot = resp->cstate.slot;
2262 struct xdr_stream *xdr = &resp->xdr;
2263 __be32 *p;
2264 __be32 status;
2266 dprintk("--> %s slot %p\n", __func__, slot);
2268 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
2269 if (status)
2270 return status;
2272 p = xdr_reserve_space(xdr, slot->sl_datalen);
2273 if (!p) {
2274 WARN_ON_ONCE(1);
2275 return nfserr_serverfault;
2277 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2278 xdr_commit_encode(xdr);
2280 resp->opcnt = slot->sl_opcnt;
2281 return slot->sl_status;
2285 * Set the exchange_id flags returned by the server.
2287 static void
2288 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2290 #ifdef CONFIG_NFSD_PNFS
2291 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
2292 #else
2293 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
2294 #endif
2296 /* Referrals are supported, Migration is not. */
2297 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2299 /* set the wire flags to return to client. */
2300 clid->flags = new->cl_exchange_flags;
2303 static bool client_has_openowners(struct nfs4_client *clp)
2305 struct nfs4_openowner *oo;
2307 list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
2308 if (!list_empty(&oo->oo_owner.so_stateids))
2309 return true;
2311 return false;
2314 static bool client_has_state(struct nfs4_client *clp)
2316 return client_has_openowners(clp)
2317 #ifdef CONFIG_NFSD_PNFS
2318 || !list_empty(&clp->cl_lo_states)
2319 #endif
2320 || !list_empty(&clp->cl_delegations)
2321 || !list_empty(&clp->cl_sessions);
2324 __be32
2325 nfsd4_exchange_id(struct svc_rqst *rqstp,
2326 struct nfsd4_compound_state *cstate,
2327 struct nfsd4_exchange_id *exid)
2329 struct nfs4_client *conf, *new;
2330 struct nfs4_client *unconf = NULL;
2331 __be32 status;
2332 char addr_str[INET6_ADDRSTRLEN];
2333 nfs4_verifier verf = exid->verifier;
2334 struct sockaddr *sa = svc_addr(rqstp);
2335 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
2336 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2338 rpc_ntop(sa, addr_str, sizeof(addr_str));
2339 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
2340 "ip_addr=%s flags %x, spa_how %d\n",
2341 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
2342 addr_str, exid->flags, exid->spa_how);
2344 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
2345 return nfserr_inval;
2347 switch (exid->spa_how) {
2348 case SP4_MACH_CRED:
2349 if (!svc_rqst_integrity_protected(rqstp))
2350 return nfserr_inval;
2351 case SP4_NONE:
2352 break;
2353 default: /* checked by xdr code */
2354 WARN_ON_ONCE(1);
2355 case SP4_SSV:
2356 return nfserr_encr_alg_unsupp;
2359 new = create_client(exid->clname, rqstp, &verf);
2360 if (new == NULL)
2361 return nfserr_jukebox;
2363 /* Cases below refer to rfc 5661 section 18.35.4: */
2364 spin_lock(&nn->client_lock);
2365 conf = find_confirmed_client_by_name(&exid->clname, nn);
2366 if (conf) {
2367 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2368 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2370 if (update) {
2371 if (!clp_used_exchangeid(conf)) { /* buggy client */
2372 status = nfserr_inval;
2373 goto out;
2375 if (!mach_creds_match(conf, rqstp)) {
2376 status = nfserr_wrong_cred;
2377 goto out;
2379 if (!creds_match) { /* case 9 */
2380 status = nfserr_perm;
2381 goto out;
2383 if (!verfs_match) { /* case 8 */
2384 status = nfserr_not_same;
2385 goto out;
2387 /* case 6 */
2388 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
2389 goto out_copy;
2391 if (!creds_match) { /* case 3 */
2392 if (client_has_state(conf)) {
2393 status = nfserr_clid_inuse;
2394 goto out;
2396 goto out_new;
2398 if (verfs_match) { /* case 2 */
2399 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
2400 goto out_copy;
2402 /* case 5, client reboot */
2403 conf = NULL;
2404 goto out_new;
2407 if (update) { /* case 7 */
2408 status = nfserr_noent;
2409 goto out;
2412 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
2413 if (unconf) /* case 4, possible retry or client restart */
2414 unhash_client_locked(unconf);
2416 /* case 1 (normal case) */
2417 out_new:
2418 if (conf) {
2419 status = mark_client_expired_locked(conf);
2420 if (status)
2421 goto out;
2423 new->cl_minorversion = cstate->minorversion;
2424 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
2426 gen_clid(new, nn);
2427 add_to_unconfirmed(new);
2428 swap(new, conf);
2429 out_copy:
2430 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
2431 exid->clientid.cl_id = conf->cl_clientid.cl_id;
2433 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
2434 nfsd4_set_ex_flags(conf, exid);
2436 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
2437 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
2438 status = nfs_ok;
2440 out:
2441 spin_unlock(&nn->client_lock);
2442 if (new)
2443 expire_client(new);
2444 if (unconf)
2445 expire_client(unconf);
2446 return status;
2449 static __be32
2450 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
2452 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2453 slot_seqid);
2455 /* The slot is in use, and no response has been sent. */
2456 if (slot_inuse) {
2457 if (seqid == slot_seqid)
2458 return nfserr_jukebox;
2459 else
2460 return nfserr_seq_misordered;
2462 /* Note unsigned 32-bit arithmetic handles wraparound: */
2463 if (likely(seqid == slot_seqid + 1))
2464 return nfs_ok;
2465 if (seqid == slot_seqid)
2466 return nfserr_replay_cache;
2467 return nfserr_seq_misordered;
2471 * Cache the create session result into the create session single DRC
2472 * slot cache by saving the xdr structure. sl_seqid has been set.
2473 * Do this for solo or embedded create session operations.
2475 static void
2476 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
2477 struct nfsd4_clid_slot *slot, __be32 nfserr)
2479 slot->sl_status = nfserr;
2480 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2483 static __be32
2484 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2485 struct nfsd4_clid_slot *slot)
2487 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2488 return slot->sl_status;
2491 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2492 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2493 1 + /* MIN tag is length with zero, only length */ \
2494 3 + /* version, opcount, opcode */ \
2495 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2496 /* seqid, slotID, slotID, cache */ \
2497 4 ) * sizeof(__be32))
2499 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2500 2 + /* verifier: AUTH_NULL, length 0 */\
2501 1 + /* status */ \
2502 1 + /* MIN tag is length with zero, only length */ \
2503 3 + /* opcount, opcode, opstatus*/ \
2504 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2505 /* seqid, slotID, slotID, slotID, status */ \
2506 5 ) * sizeof(__be32))
2508 static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
2510 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2512 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2513 return nfserr_toosmall;
2514 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2515 return nfserr_toosmall;
2516 ca->headerpadsz = 0;
2517 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2518 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2519 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2520 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2521 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2522 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2524 * Note decreasing slot size below client's request may make it
2525 * difficult for client to function correctly, whereas
2526 * decreasing the number of slots will (just?) affect
2527 * performance. When short on memory we therefore prefer to
2528 * decrease number of slots instead of their size. Clients that
2529 * request larger slots than they need will get poor results:
2531 ca->maxreqs = nfsd4_get_drc_mem(ca);
2532 if (!ca->maxreqs)
2533 return nfserr_jukebox;
2535 return nfs_ok;
2538 #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2539 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2540 #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2541 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2543 static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2545 ca->headerpadsz = 0;
2548 * These RPC_MAX_HEADER macros are overkill, especially since we
2549 * don't even do gss on the backchannel yet. But this is still
2550 * less than 1k. Tighten up this estimate in the unlikely event
2551 * it turns out to be a problem for some client:
2553 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
2554 return nfserr_toosmall;
2555 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
2556 return nfserr_toosmall;
2557 ca->maxresp_cached = 0;
2558 if (ca->maxops < 2)
2559 return nfserr_toosmall;
2561 return nfs_ok;
2564 static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2566 switch (cbs->flavor) {
2567 case RPC_AUTH_NULL:
2568 case RPC_AUTH_UNIX:
2569 return nfs_ok;
2570 default:
2572 * GSS case: the spec doesn't allow us to return this
2573 * error. But it also doesn't allow us not to support
2574 * GSS.
2575 * I'd rather this fail hard than return some error the
2576 * client might think it can already handle:
2578 return nfserr_encr_alg_unsupp;
2582 __be32
2583 nfsd4_create_session(struct svc_rqst *rqstp,
2584 struct nfsd4_compound_state *cstate,
2585 struct nfsd4_create_session *cr_ses)
2587 struct sockaddr *sa = svc_addr(rqstp);
2588 struct nfs4_client *conf, *unconf;
2589 struct nfs4_client *old = NULL;
2590 struct nfsd4_session *new;
2591 struct nfsd4_conn *conn;
2592 struct nfsd4_clid_slot *cs_slot = NULL;
2593 __be32 status = 0;
2594 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2596 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2597 return nfserr_inval;
2598 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2599 if (status)
2600 return status;
2601 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
2602 if (status)
2603 return status;
2604 status = check_backchannel_attrs(&cr_ses->back_channel);
2605 if (status)
2606 goto out_release_drc_mem;
2607 status = nfserr_jukebox;
2608 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
2609 if (!new)
2610 goto out_release_drc_mem;
2611 conn = alloc_conn_from_crses(rqstp, cr_ses);
2612 if (!conn)
2613 goto out_free_session;
2615 spin_lock(&nn->client_lock);
2616 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
2617 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
2618 WARN_ON_ONCE(conf && unconf);
2620 if (conf) {
2621 status = nfserr_wrong_cred;
2622 if (!mach_creds_match(conf, rqstp))
2623 goto out_free_conn;
2624 cs_slot = &conf->cl_cs_slot;
2625 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2626 if (status) {
2627 if (status == nfserr_replay_cache)
2628 status = nfsd4_replay_create_session(cr_ses, cs_slot);
2629 goto out_free_conn;
2631 } else if (unconf) {
2632 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
2633 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
2634 status = nfserr_clid_inuse;
2635 goto out_free_conn;
2637 status = nfserr_wrong_cred;
2638 if (!mach_creds_match(unconf, rqstp))
2639 goto out_free_conn;
2640 cs_slot = &unconf->cl_cs_slot;
2641 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2642 if (status) {
2643 /* an unconfirmed replay returns misordered */
2644 status = nfserr_seq_misordered;
2645 goto out_free_conn;
2647 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
2648 if (old) {
2649 status = mark_client_expired_locked(old);
2650 if (status) {
2651 old = NULL;
2652 goto out_free_conn;
2655 move_to_confirmed(unconf);
2656 conf = unconf;
2657 } else {
2658 status = nfserr_stale_clientid;
2659 goto out_free_conn;
2661 status = nfs_ok;
2663 * We do not support RDMA or persistent sessions
2665 cr_ses->flags &= ~SESSION4_PERSIST;
2666 cr_ses->flags &= ~SESSION4_RDMA;
2668 init_session(rqstp, new, conf, cr_ses);
2669 nfsd4_get_session_locked(new);
2671 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
2672 NFS4_MAX_SESSIONID_LEN);
2673 cs_slot->sl_seqid++;
2674 cr_ses->seqid = cs_slot->sl_seqid;
2676 /* cache solo and embedded create sessions under the client_lock */
2677 nfsd4_cache_create_session(cr_ses, cs_slot, status);
2678 spin_unlock(&nn->client_lock);
2679 /* init connection and backchannel */
2680 nfsd4_init_conn(rqstp, conn, new);
2681 nfsd4_put_session(new);
2682 if (old)
2683 expire_client(old);
2684 return status;
2685 out_free_conn:
2686 spin_unlock(&nn->client_lock);
2687 free_conn(conn);
2688 if (old)
2689 expire_client(old);
2690 out_free_session:
2691 __free_session(new);
2692 out_release_drc_mem:
2693 nfsd4_put_drc_mem(&cr_ses->fore_channel);
2694 return status;
2697 static __be32 nfsd4_map_bcts_dir(u32 *dir)
2699 switch (*dir) {
2700 case NFS4_CDFC4_FORE:
2701 case NFS4_CDFC4_BACK:
2702 return nfs_ok;
2703 case NFS4_CDFC4_FORE_OR_BOTH:
2704 case NFS4_CDFC4_BACK_OR_BOTH:
2705 *dir = NFS4_CDFC4_BOTH;
2706 return nfs_ok;
2708 return nfserr_inval;
2711 __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2713 struct nfsd4_session *session = cstate->session;
2714 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2715 __be32 status;
2717 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2718 if (status)
2719 return status;
2720 spin_lock(&nn->client_lock);
2721 session->se_cb_prog = bc->bc_cb_program;
2722 session->se_cb_sec = bc->bc_cb_sec;
2723 spin_unlock(&nn->client_lock);
2725 nfsd4_probe_callback(session->se_client);
2727 return nfs_ok;
2730 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2731 struct nfsd4_compound_state *cstate,
2732 struct nfsd4_bind_conn_to_session *bcts)
2734 __be32 status;
2735 struct nfsd4_conn *conn;
2736 struct nfsd4_session *session;
2737 struct net *net = SVC_NET(rqstp);
2738 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2740 if (!nfsd4_last_compound_op(rqstp))
2741 return nfserr_not_only_op;
2742 spin_lock(&nn->client_lock);
2743 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
2744 spin_unlock(&nn->client_lock);
2745 if (!session)
2746 goto out_no_session;
2747 status = nfserr_wrong_cred;
2748 if (!mach_creds_match(session->se_client, rqstp))
2749 goto out;
2750 status = nfsd4_map_bcts_dir(&bcts->dir);
2751 if (status)
2752 goto out;
2753 conn = alloc_conn(rqstp, bcts->dir);
2754 status = nfserr_jukebox;
2755 if (!conn)
2756 goto out;
2757 nfsd4_init_conn(rqstp, conn, session);
2758 status = nfs_ok;
2759 out:
2760 nfsd4_put_session(session);
2761 out_no_session:
2762 return status;
2765 static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2767 if (!session)
2768 return 0;
2769 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2772 __be32
2773 nfsd4_destroy_session(struct svc_rqst *r,
2774 struct nfsd4_compound_state *cstate,
2775 struct nfsd4_destroy_session *sessionid)
2777 struct nfsd4_session *ses;
2778 __be32 status;
2779 int ref_held_by_me = 0;
2780 struct net *net = SVC_NET(r);
2781 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2783 status = nfserr_not_only_op;
2784 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
2785 if (!nfsd4_last_compound_op(r))
2786 goto out;
2787 ref_held_by_me++;
2789 dump_sessionid(__func__, &sessionid->sessionid);
2790 spin_lock(&nn->client_lock);
2791 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
2792 if (!ses)
2793 goto out_client_lock;
2794 status = nfserr_wrong_cred;
2795 if (!mach_creds_match(ses->se_client, r))
2796 goto out_put_session;
2797 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
2798 if (status)
2799 goto out_put_session;
2800 unhash_session(ses);
2801 spin_unlock(&nn->client_lock);
2803 nfsd4_probe_callback_sync(ses->se_client);
2805 spin_lock(&nn->client_lock);
2806 status = nfs_ok;
2807 out_put_session:
2808 nfsd4_put_session_locked(ses);
2809 out_client_lock:
2810 spin_unlock(&nn->client_lock);
2811 out:
2812 return status;
2815 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
2817 struct nfsd4_conn *c;
2819 list_for_each_entry(c, &s->se_conns, cn_persession) {
2820 if (c->cn_xprt == xpt) {
2821 return c;
2824 return NULL;
2827 static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
2829 struct nfs4_client *clp = ses->se_client;
2830 struct nfsd4_conn *c;
2831 __be32 status = nfs_ok;
2832 int ret;
2834 spin_lock(&clp->cl_lock);
2835 c = __nfsd4_find_conn(new->cn_xprt, ses);
2836 if (c)
2837 goto out_free;
2838 status = nfserr_conn_not_bound_to_session;
2839 if (clp->cl_mach_cred)
2840 goto out_free;
2841 __nfsd4_hash_conn(new, ses);
2842 spin_unlock(&clp->cl_lock);
2843 ret = nfsd4_register_conn(new);
2844 if (ret)
2845 /* oops; xprt is already down: */
2846 nfsd4_conn_lost(&new->cn_xpt_user);
2847 return nfs_ok;
2848 out_free:
2849 spin_unlock(&clp->cl_lock);
2850 free_conn(new);
2851 return status;
2854 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2856 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2858 return args->opcnt > session->se_fchannel.maxops;
2861 static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2862 struct nfsd4_session *session)
2864 struct xdr_buf *xb = &rqstp->rq_arg;
2866 return xb->len > session->se_fchannel.maxreq_sz;
2869 __be32
2870 nfsd4_sequence(struct svc_rqst *rqstp,
2871 struct nfsd4_compound_state *cstate,
2872 struct nfsd4_sequence *seq)
2874 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2875 struct xdr_stream *xdr = &resp->xdr;
2876 struct nfsd4_session *session;
2877 struct nfs4_client *clp;
2878 struct nfsd4_slot *slot;
2879 struct nfsd4_conn *conn;
2880 __be32 status;
2881 int buflen;
2882 struct net *net = SVC_NET(rqstp);
2883 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2885 if (resp->opcnt != 1)
2886 return nfserr_sequence_pos;
2889 * Will be either used or freed by nfsd4_sequence_check_conn
2890 * below.
2892 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2893 if (!conn)
2894 return nfserr_jukebox;
2896 spin_lock(&nn->client_lock);
2897 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
2898 if (!session)
2899 goto out_no_session;
2900 clp = session->se_client;
2902 status = nfserr_too_many_ops;
2903 if (nfsd4_session_too_many_ops(rqstp, session))
2904 goto out_put_session;
2906 status = nfserr_req_too_big;
2907 if (nfsd4_request_too_big(rqstp, session))
2908 goto out_put_session;
2910 status = nfserr_badslot;
2911 if (seq->slotid >= session->se_fchannel.maxreqs)
2912 goto out_put_session;
2914 slot = session->se_slots[seq->slotid];
2915 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2917 /* We do not negotiate the number of slots yet, so set the
2918 * maxslots to the session maxreqs which is used to encode
2919 * sr_highest_slotid and the sr_target_slot id to maxslots */
2920 seq->maxslots = session->se_fchannel.maxreqs;
2922 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2923 slot->sl_flags & NFSD4_SLOT_INUSE);
2924 if (status == nfserr_replay_cache) {
2925 status = nfserr_seq_misordered;
2926 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
2927 goto out_put_session;
2928 cstate->slot = slot;
2929 cstate->session = session;
2930 cstate->clp = clp;
2931 /* Return the cached reply status and set cstate->status
2932 * for nfsd4_proc_compound processing */
2933 status = nfsd4_replay_cache_entry(resp, seq);
2934 cstate->status = nfserr_replay_cache;
2935 goto out;
2937 if (status)
2938 goto out_put_session;
2940 status = nfsd4_sequence_check_conn(conn, session);
2941 conn = NULL;
2942 if (status)
2943 goto out_put_session;
2945 buflen = (seq->cachethis) ?
2946 session->se_fchannel.maxresp_cached :
2947 session->se_fchannel.maxresp_sz;
2948 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2949 nfserr_rep_too_big;
2950 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
2951 goto out_put_session;
2952 svc_reserve(rqstp, buflen);
2954 status = nfs_ok;
2955 /* Success! bump slot seqid */
2956 slot->sl_seqid = seq->seqid;
2957 slot->sl_flags |= NFSD4_SLOT_INUSE;
2958 if (seq->cachethis)
2959 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
2960 else
2961 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
2963 cstate->slot = slot;
2964 cstate->session = session;
2965 cstate->clp = clp;
2967 out:
2968 switch (clp->cl_cb_state) {
2969 case NFSD4_CB_DOWN:
2970 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2971 break;
2972 case NFSD4_CB_FAULT:
2973 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2974 break;
2975 default:
2976 seq->status_flags = 0;
2978 if (!list_empty(&clp->cl_revoked))
2979 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
2980 out_no_session:
2981 if (conn)
2982 free_conn(conn);
2983 spin_unlock(&nn->client_lock);
2984 return status;
2985 out_put_session:
2986 nfsd4_put_session_locked(session);
2987 goto out_no_session;
2990 void
2991 nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2993 struct nfsd4_compound_state *cs = &resp->cstate;
2995 if (nfsd4_has_session(cs)) {
2996 if (cs->status != nfserr_replay_cache) {
2997 nfsd4_store_cache_entry(resp);
2998 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
3000 /* Drop session reference that was taken in nfsd4_sequence() */
3001 nfsd4_put_session(cs->session);
3002 } else if (cs->clp)
3003 put_client_renew(cs->clp);
3006 __be32
3007 nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
3009 struct nfs4_client *conf, *unconf;
3010 struct nfs4_client *clp = NULL;
3011 __be32 status = 0;
3012 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3014 spin_lock(&nn->client_lock);
3015 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
3016 conf = find_confirmed_client(&dc->clientid, true, nn);
3017 WARN_ON_ONCE(conf && unconf);
3019 if (conf) {
3020 if (client_has_state(conf)) {
3021 status = nfserr_clientid_busy;
3022 goto out;
3024 status = mark_client_expired_locked(conf);
3025 if (status)
3026 goto out;
3027 clp = conf;
3028 } else if (unconf)
3029 clp = unconf;
3030 else {
3031 status = nfserr_stale_clientid;
3032 goto out;
3034 if (!mach_creds_match(clp, rqstp)) {
3035 clp = NULL;
3036 status = nfserr_wrong_cred;
3037 goto out;
3039 unhash_client_locked(clp);
3040 out:
3041 spin_unlock(&nn->client_lock);
3042 if (clp)
3043 expire_client(clp);
3044 return status;
3047 __be32
3048 nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
3050 __be32 status = 0;
3052 if (rc->rca_one_fs) {
3053 if (!cstate->current_fh.fh_dentry)
3054 return nfserr_nofilehandle;
3056 * We don't take advantage of the rca_one_fs case.
3057 * That's OK, it's optional, we can safely ignore it.
3059 return nfs_ok;
3062 status = nfserr_complete_already;
3063 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
3064 &cstate->session->se_client->cl_flags))
3065 goto out;
3067 status = nfserr_stale_clientid;
3068 if (is_client_expired(cstate->session->se_client))
3070 * The following error isn't really legal.
3071 * But we only get here if the client just explicitly
3072 * destroyed the client. Surely it no longer cares what
3073 * error it gets back on an operation for the dead
3074 * client.
3076 goto out;
3078 status = nfs_ok;
3079 nfsd4_client_record_create(cstate->session->se_client);
3080 out:
3081 return status;
3084 __be32
3085 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3086 struct nfsd4_setclientid *setclid)
3088 struct xdr_netobj clname = setclid->se_name;
3089 nfs4_verifier clverifier = setclid->se_verf;
3090 struct nfs4_client *conf, *new;
3091 struct nfs4_client *unconf = NULL;
3092 __be32 status;
3093 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3095 new = create_client(clname, rqstp, &clverifier);
3096 if (new == NULL)
3097 return nfserr_jukebox;
3098 /* Cases below refer to rfc 3530 section 14.2.33: */
3099 spin_lock(&nn->client_lock);
3100 conf = find_confirmed_client_by_name(&clname, nn);
3101 if (conf && client_has_state(conf)) {
3102 /* case 0: */
3103 status = nfserr_clid_inuse;
3104 if (clp_used_exchangeid(conf))
3105 goto out;
3106 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
3107 char addr_str[INET6_ADDRSTRLEN];
3108 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
3109 sizeof(addr_str));
3110 dprintk("NFSD: setclientid: string in use by client "
3111 "at %s\n", addr_str);
3112 goto out;
3115 unconf = find_unconfirmed_client_by_name(&clname, nn);
3116 if (unconf)
3117 unhash_client_locked(unconf);
3118 if (conf && same_verf(&conf->cl_verifier, &clverifier)) {
3119 /* case 1: probable callback update */
3120 copy_clid(new, conf);
3121 gen_confirm(new, nn);
3122 } else /* case 4 (new client) or cases 2, 3 (client reboot): */
3123 gen_clid(new, nn);
3124 new->cl_minorversion = 0;
3125 gen_callback(new, setclid, rqstp);
3126 add_to_unconfirmed(new);
3127 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
3128 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
3129 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
3130 new = NULL;
3131 status = nfs_ok;
3132 out:
3133 spin_unlock(&nn->client_lock);
3134 if (new)
3135 free_client(new);
3136 if (unconf)
3137 expire_client(unconf);
3138 return status;
3142 __be32
3143 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
3144 struct nfsd4_compound_state *cstate,
3145 struct nfsd4_setclientid_confirm *setclientid_confirm)
3147 struct nfs4_client *conf, *unconf;
3148 struct nfs4_client *old = NULL;
3149 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
3150 clientid_t * clid = &setclientid_confirm->sc_clientid;
3151 __be32 status;
3152 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3154 if (STALE_CLIENTID(clid, nn))
3155 return nfserr_stale_clientid;
3157 spin_lock(&nn->client_lock);
3158 conf = find_confirmed_client(clid, false, nn);
3159 unconf = find_unconfirmed_client(clid, false, nn);
3161 * We try hard to give out unique clientid's, so if we get an
3162 * attempt to confirm the same clientid with a different cred,
3163 * the client may be buggy; this should never happen.
3165 * Nevertheless, RFC 7530 recommends INUSE for this case:
3167 status = nfserr_clid_inuse;
3168 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3169 goto out;
3170 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3171 goto out;
3172 /* cases below refer to rfc 3530 section 14.2.34: */
3173 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
3174 if (conf && !unconf) /* case 2: probable retransmit */
3175 status = nfs_ok;
3176 else /* case 4: client hasn't noticed we rebooted yet? */
3177 status = nfserr_stale_clientid;
3178 goto out;
3180 status = nfs_ok;
3181 if (conf) { /* case 1: callback update */
3182 old = unconf;
3183 unhash_client_locked(old);
3184 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
3185 } else { /* case 3: normal case; new or rebooted client */
3186 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3187 if (old) {
3188 status = nfserr_clid_inuse;
3189 if (client_has_state(old)
3190 && !same_creds(&unconf->cl_cred,
3191 &old->cl_cred))
3192 goto out;
3193 status = mark_client_expired_locked(old);
3194 if (status) {
3195 old = NULL;
3196 goto out;
3199 move_to_confirmed(unconf);
3200 conf = unconf;
3202 get_client_locked(conf);
3203 spin_unlock(&nn->client_lock);
3204 nfsd4_probe_callback(conf);
3205 spin_lock(&nn->client_lock);
3206 put_client_renew_locked(conf);
3207 out:
3208 spin_unlock(&nn->client_lock);
3209 if (old)
3210 expire_client(old);
3211 return status;
3214 static struct nfs4_file *nfsd4_alloc_file(void)
3216 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3219 /* OPEN Share state helper functions */
3220 static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
3221 struct nfs4_file *fp)
3223 lockdep_assert_held(&state_lock);
3225 atomic_set(&fp->fi_ref, 1);
3226 spin_lock_init(&fp->fi_lock);
3227 INIT_LIST_HEAD(&fp->fi_stateids);
3228 INIT_LIST_HEAD(&fp->fi_delegations);
3229 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
3230 fh_copy_shallow(&fp->fi_fhandle, fh);
3231 fp->fi_deleg_file = NULL;
3232 fp->fi_had_conflict = false;
3233 fp->fi_share_deny = 0;
3234 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3235 memset(fp->fi_access, 0, sizeof(fp->fi_access));
3236 #ifdef CONFIG_NFSD_PNFS
3237 INIT_LIST_HEAD(&fp->fi_lo_states);
3238 atomic_set(&fp->fi_lo_recalls, 0);
3239 #endif
3240 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
3243 void
3244 nfsd4_free_slabs(void)
3246 kmem_cache_destroy(odstate_slab);
3247 kmem_cache_destroy(openowner_slab);
3248 kmem_cache_destroy(lockowner_slab);
3249 kmem_cache_destroy(file_slab);
3250 kmem_cache_destroy(stateid_slab);
3251 kmem_cache_destroy(deleg_slab);
3255 nfsd4_init_slabs(void)
3257 openowner_slab = kmem_cache_create("nfsd4_openowners",
3258 sizeof(struct nfs4_openowner), 0, 0, NULL);
3259 if (openowner_slab == NULL)
3260 goto out;
3261 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
3262 sizeof(struct nfs4_lockowner), 0, 0, NULL);
3263 if (lockowner_slab == NULL)
3264 goto out_free_openowner_slab;
3265 file_slab = kmem_cache_create("nfsd4_files",
3266 sizeof(struct nfs4_file), 0, 0, NULL);
3267 if (file_slab == NULL)
3268 goto out_free_lockowner_slab;
3269 stateid_slab = kmem_cache_create("nfsd4_stateids",
3270 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
3271 if (stateid_slab == NULL)
3272 goto out_free_file_slab;
3273 deleg_slab = kmem_cache_create("nfsd4_delegations",
3274 sizeof(struct nfs4_delegation), 0, 0, NULL);
3275 if (deleg_slab == NULL)
3276 goto out_free_stateid_slab;
3277 odstate_slab = kmem_cache_create("nfsd4_odstate",
3278 sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
3279 if (odstate_slab == NULL)
3280 goto out_free_deleg_slab;
3281 return 0;
3283 out_free_deleg_slab:
3284 kmem_cache_destroy(deleg_slab);
3285 out_free_stateid_slab:
3286 kmem_cache_destroy(stateid_slab);
3287 out_free_file_slab:
3288 kmem_cache_destroy(file_slab);
3289 out_free_lockowner_slab:
3290 kmem_cache_destroy(lockowner_slab);
3291 out_free_openowner_slab:
3292 kmem_cache_destroy(openowner_slab);
3293 out:
3294 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3295 return -ENOMEM;
3298 static void init_nfs4_replay(struct nfs4_replay *rp)
3300 rp->rp_status = nfserr_serverfault;
3301 rp->rp_buflen = 0;
3302 rp->rp_buf = rp->rp_ibuf;
3303 mutex_init(&rp->rp_mutex);
3306 static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3307 struct nfs4_stateowner *so)
3309 if (!nfsd4_has_session(cstate)) {
3310 mutex_lock(&so->so_replay.rp_mutex);
3311 cstate->replay_owner = nfs4_get_stateowner(so);
3315 void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3317 struct nfs4_stateowner *so = cstate->replay_owner;
3319 if (so != NULL) {
3320 cstate->replay_owner = NULL;
3321 mutex_unlock(&so->so_replay.rp_mutex);
3322 nfs4_put_stateowner(so);
3326 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
3328 struct nfs4_stateowner *sop;
3330 sop = kmem_cache_alloc(slab, GFP_KERNEL);
3331 if (!sop)
3332 return NULL;
3334 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3335 if (!sop->so_owner.data) {
3336 kmem_cache_free(slab, sop);
3337 return NULL;
3339 sop->so_owner.len = owner->len;
3341 INIT_LIST_HEAD(&sop->so_stateids);
3342 sop->so_client = clp;
3343 init_nfs4_replay(&sop->so_replay);
3344 atomic_set(&sop->so_count, 1);
3345 return sop;
3348 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
3350 lockdep_assert_held(&clp->cl_lock);
3352 list_add(&oo->oo_owner.so_strhash,
3353 &clp->cl_ownerstr_hashtbl[strhashval]);
3354 list_add(&oo->oo_perclient, &clp->cl_openowners);
3357 static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3359 unhash_openowner_locked(openowner(so));
3362 static void nfs4_free_openowner(struct nfs4_stateowner *so)
3364 struct nfs4_openowner *oo = openowner(so);
3366 kmem_cache_free(openowner_slab, oo);
3369 static const struct nfs4_stateowner_operations openowner_ops = {
3370 .so_unhash = nfs4_unhash_openowner,
3371 .so_free = nfs4_free_openowner,
3374 static struct nfs4_ol_stateid *
3375 nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
3377 struct nfs4_ol_stateid *local, *ret = NULL;
3378 struct nfs4_openowner *oo = open->op_openowner;
3380 lockdep_assert_held(&fp->fi_lock);
3382 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
3383 /* ignore lock owners */
3384 if (local->st_stateowner->so_is_open_owner == 0)
3385 continue;
3386 if (local->st_stateowner != &oo->oo_owner)
3387 continue;
3388 if (local->st_stid.sc_type == NFS4_OPEN_STID) {
3389 ret = local;
3390 atomic_inc(&ret->st_stid.sc_count);
3391 break;
3394 return ret;
3397 static __be32
3398 nfsd4_verify_open_stid(struct nfs4_stid *s)
3400 __be32 ret = nfs_ok;
3402 switch (s->sc_type) {
3403 default:
3404 break;
3405 case NFS4_CLOSED_STID:
3406 case NFS4_CLOSED_DELEG_STID:
3407 ret = nfserr_bad_stateid;
3408 break;
3409 case NFS4_REVOKED_DELEG_STID:
3410 ret = nfserr_deleg_revoked;
3412 return ret;
3415 /* Lock the stateid st_mutex, and deal with races with CLOSE */
3416 static __be32
3417 nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp)
3419 __be32 ret;
3421 mutex_lock(&stp->st_mutex);
3422 ret = nfsd4_verify_open_stid(&stp->st_stid);
3423 if (ret != nfs_ok)
3424 mutex_unlock(&stp->st_mutex);
3425 return ret;
3428 static struct nfs4_ol_stateid *
3429 nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
3431 struct nfs4_ol_stateid *stp;
3432 for (;;) {
3433 spin_lock(&fp->fi_lock);
3434 stp = nfsd4_find_existing_open(fp, open);
3435 spin_unlock(&fp->fi_lock);
3436 if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok)
3437 break;
3438 nfs4_put_stid(&stp->st_stid);
3440 return stp;
3443 static struct nfs4_openowner *
3444 alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
3445 struct nfsd4_compound_state *cstate)
3447 struct nfs4_client *clp = cstate->clp;
3448 struct nfs4_openowner *oo, *ret;
3450 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3451 if (!oo)
3452 return NULL;
3453 oo->oo_owner.so_ops = &openowner_ops;
3454 oo->oo_owner.so_is_open_owner = 1;
3455 oo->oo_owner.so_seqid = open->op_seqid;
3456 oo->oo_flags = 0;
3457 if (nfsd4_has_session(cstate))
3458 oo->oo_flags |= NFS4_OO_CONFIRMED;
3459 oo->oo_time = 0;
3460 oo->oo_last_closed_stid = NULL;
3461 INIT_LIST_HEAD(&oo->oo_close_lru);
3462 spin_lock(&clp->cl_lock);
3463 ret = find_openstateowner_str_locked(strhashval, open, clp);
3464 if (ret == NULL) {
3465 hash_openowner(oo, clp, strhashval);
3466 ret = oo;
3467 } else
3468 nfs4_free_stateowner(&oo->oo_owner);
3470 spin_unlock(&clp->cl_lock);
3471 return ret;
3474 static struct nfs4_ol_stateid *
3475 init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
3478 struct nfs4_openowner *oo = open->op_openowner;
3479 struct nfs4_ol_stateid *retstp = NULL;
3480 struct nfs4_ol_stateid *stp;
3482 stp = open->op_stp;
3483 /* We are moving these outside of the spinlocks to avoid the warnings */
3484 mutex_init(&stp->st_mutex);
3485 mutex_lock(&stp->st_mutex);
3487 retry:
3488 spin_lock(&oo->oo_owner.so_client->cl_lock);
3489 spin_lock(&fp->fi_lock);
3491 retstp = nfsd4_find_existing_open(fp, open);
3492 if (retstp)
3493 goto out_unlock;
3495 open->op_stp = NULL;
3496 atomic_inc(&stp->st_stid.sc_count);
3497 stp->st_stid.sc_type = NFS4_OPEN_STID;
3498 INIT_LIST_HEAD(&stp->st_locks);
3499 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
3500 get_nfs4_file(fp);
3501 stp->st_stid.sc_file = fp;
3502 stp->st_access_bmap = 0;
3503 stp->st_deny_bmap = 0;
3504 stp->st_openstp = NULL;
3505 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
3506 list_add(&stp->st_perfile, &fp->fi_stateids);
3508 out_unlock:
3509 spin_unlock(&fp->fi_lock);
3510 spin_unlock(&oo->oo_owner.so_client->cl_lock);
3511 if (retstp) {
3512 /* Handle races with CLOSE */
3513 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
3514 nfs4_put_stid(&retstp->st_stid);
3515 goto retry;
3517 /* To keep mutex tracking happy */
3518 mutex_unlock(&stp->st_mutex);
3519 stp = retstp;
3521 return stp;
3525 * In the 4.0 case we need to keep the owners around a little while to handle
3526 * CLOSE replay. We still do need to release any file access that is held by
3527 * them before returning however.
3529 static void
3530 move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
3532 struct nfs4_ol_stateid *last;
3533 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3534 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3535 nfsd_net_id);
3537 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
3540 * We know that we hold one reference via nfsd4_close, and another
3541 * "persistent" reference for the client. If the refcount is higher
3542 * than 2, then there are still calls in progress that are using this
3543 * stateid. We can't put the sc_file reference until they are finished.
3544 * Wait for the refcount to drop to 2. Since it has been unhashed,
3545 * there should be no danger of the refcount going back up again at
3546 * this point.
3548 wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3550 release_all_access(s);
3551 if (s->st_stid.sc_file) {
3552 put_nfs4_file(s->st_stid.sc_file);
3553 s->st_stid.sc_file = NULL;
3556 spin_lock(&nn->client_lock);
3557 last = oo->oo_last_closed_stid;
3558 oo->oo_last_closed_stid = s;
3559 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
3560 oo->oo_time = get_seconds();
3561 spin_unlock(&nn->client_lock);
3562 if (last)
3563 nfs4_put_stid(&last->st_stid);
3566 /* search file_hashtbl[] for file */
3567 static struct nfs4_file *
3568 find_file_locked(struct knfsd_fh *fh, unsigned int hashval)
3570 struct nfs4_file *fp;
3572 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash) {
3573 if (fh_match(&fp->fi_fhandle, fh)) {
3574 if (atomic_inc_not_zero(&fp->fi_ref))
3575 return fp;
3578 return NULL;
3581 struct nfs4_file *
3582 find_file(struct knfsd_fh *fh)
3584 struct nfs4_file *fp;
3585 unsigned int hashval = file_hashval(fh);
3587 rcu_read_lock();
3588 fp = find_file_locked(fh, hashval);
3589 rcu_read_unlock();
3590 return fp;
3593 static struct nfs4_file *
3594 find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
3596 struct nfs4_file *fp;
3597 unsigned int hashval = file_hashval(fh);
3599 rcu_read_lock();
3600 fp = find_file_locked(fh, hashval);
3601 rcu_read_unlock();
3602 if (fp)
3603 return fp;
3605 spin_lock(&state_lock);
3606 fp = find_file_locked(fh, hashval);
3607 if (likely(fp == NULL)) {
3608 nfsd4_init_file(fh, hashval, new);
3609 fp = new;
3611 spin_unlock(&state_lock);
3613 return fp;
3617 * Called to check deny when READ with all zero stateid or
3618 * WRITE with all zero or all one stateid
3620 static __be32
3621 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3623 struct nfs4_file *fp;
3624 __be32 ret = nfs_ok;
3626 fp = find_file(&current_fh->fh_handle);
3627 if (!fp)
3628 return ret;
3629 /* Check for conflicting share reservations */
3630 spin_lock(&fp->fi_lock);
3631 if (fp->fi_share_deny & deny_type)
3632 ret = nfserr_locked;
3633 spin_unlock(&fp->fi_lock);
3634 put_nfs4_file(fp);
3635 return ret;
3638 static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
3640 struct nfs4_delegation *dp = cb_to_delegation(cb);
3641 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3642 nfsd_net_id);
3644 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
3647 * We can't do this in nfsd_break_deleg_cb because it is
3648 * already holding inode->i_lock.
3650 * If the dl_time != 0, then we know that it has already been
3651 * queued for a lease break. Don't queue it again.
3653 spin_lock(&state_lock);
3654 if (dp->dl_time == 0) {
3655 dp->dl_time = get_seconds();
3656 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
3658 spin_unlock(&state_lock);
3661 static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
3662 struct rpc_task *task)
3664 struct nfs4_delegation *dp = cb_to_delegation(cb);
3666 if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID)
3667 return 1;
3669 switch (task->tk_status) {
3670 case 0:
3671 return 1;
3672 case -EBADHANDLE:
3673 case -NFS4ERR_BAD_STATEID:
3675 * Race: client probably got cb_recall before open reply
3676 * granting delegation.
3678 if (dp->dl_retries--) {
3679 rpc_delay(task, 2 * HZ);
3680 return 0;
3682 /*FALLTHRU*/
3683 default:
3684 return -1;
3688 static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
3690 struct nfs4_delegation *dp = cb_to_delegation(cb);
3692 nfs4_put_stid(&dp->dl_stid);
3695 static struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
3696 .prepare = nfsd4_cb_recall_prepare,
3697 .done = nfsd4_cb_recall_done,
3698 .release = nfsd4_cb_recall_release,
3701 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3704 * We're assuming the state code never drops its reference
3705 * without first removing the lease. Since we're in this lease
3706 * callback (and since the lease code is serialized by the kernel
3707 * lock) we know the server hasn't removed the lease yet, we know
3708 * it's safe to take a reference.
3710 atomic_inc(&dp->dl_stid.sc_count);
3711 nfsd4_run_cb(&dp->dl_recall);
3714 /* Called from break_lease() with i_lock held. */
3715 static bool
3716 nfsd_break_deleg_cb(struct file_lock *fl)
3718 bool ret = false;
3719 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3720 struct nfs4_delegation *dp;
3722 if (!fp) {
3723 WARN(1, "(%p)->fl_owner NULL\n", fl);
3724 return ret;
3726 if (fp->fi_had_conflict) {
3727 WARN(1, "duplicate break on %p\n", fp);
3728 return ret;
3731 * We don't want the locks code to timeout the lease for us;
3732 * we'll remove it ourself if a delegation isn't returned
3733 * in time:
3735 fl->fl_break_time = 0;
3737 spin_lock(&fp->fi_lock);
3738 fp->fi_had_conflict = true;
3740 * If there are no delegations on the list, then return true
3741 * so that the lease code will go ahead and delete it.
3743 if (list_empty(&fp->fi_delegations))
3744 ret = true;
3745 else
3746 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3747 nfsd_break_one_deleg(dp);
3748 spin_unlock(&fp->fi_lock);
3749 return ret;
3752 static int
3753 nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
3754 struct list_head *dispose)
3756 if (arg & F_UNLCK)
3757 return lease_modify(onlist, arg, dispose);
3758 else
3759 return -EAGAIN;
3762 static const struct lock_manager_operations nfsd_lease_mng_ops = {
3763 .lm_break = nfsd_break_deleg_cb,
3764 .lm_change = nfsd_change_deleg_cb,
3767 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3769 if (nfsd4_has_session(cstate))
3770 return nfs_ok;
3771 if (seqid == so->so_seqid - 1)
3772 return nfserr_replay_me;
3773 if (seqid == so->so_seqid)
3774 return nfs_ok;
3775 return nfserr_bad_seqid;
3778 static __be32 lookup_clientid(clientid_t *clid,
3779 struct nfsd4_compound_state *cstate,
3780 struct nfsd_net *nn)
3782 struct nfs4_client *found;
3784 if (cstate->clp) {
3785 found = cstate->clp;
3786 if (!same_clid(&found->cl_clientid, clid))
3787 return nfserr_stale_clientid;
3788 return nfs_ok;
3791 if (STALE_CLIENTID(clid, nn))
3792 return nfserr_stale_clientid;
3795 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3796 * cached already then we know this is for is for v4.0 and "sessions"
3797 * will be false.
3799 WARN_ON_ONCE(cstate->session);
3800 spin_lock(&nn->client_lock);
3801 found = find_confirmed_client(clid, false, nn);
3802 if (!found) {
3803 spin_unlock(&nn->client_lock);
3804 return nfserr_expired;
3806 atomic_inc(&found->cl_refcount);
3807 spin_unlock(&nn->client_lock);
3809 /* Cache the nfs4_client in cstate! */
3810 cstate->clp = found;
3811 return nfs_ok;
3814 __be32
3815 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
3816 struct nfsd4_open *open, struct nfsd_net *nn)
3818 clientid_t *clientid = &open->op_clientid;
3819 struct nfs4_client *clp = NULL;
3820 unsigned int strhashval;
3821 struct nfs4_openowner *oo = NULL;
3822 __be32 status;
3824 if (STALE_CLIENTID(&open->op_clientid, nn))
3825 return nfserr_stale_clientid;
3827 * In case we need it later, after we've already created the
3828 * file and don't want to risk a further failure:
3830 open->op_file = nfsd4_alloc_file();
3831 if (open->op_file == NULL)
3832 return nfserr_jukebox;
3834 status = lookup_clientid(clientid, cstate, nn);
3835 if (status)
3836 return status;
3837 clp = cstate->clp;
3839 strhashval = ownerstr_hashval(&open->op_owner);
3840 oo = find_openstateowner_str(strhashval, open, clp);
3841 open->op_openowner = oo;
3842 if (!oo) {
3843 goto new_owner;
3845 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
3846 /* Replace unconfirmed owners without checking for replay. */
3847 release_openowner(oo);
3848 open->op_openowner = NULL;
3849 goto new_owner;
3851 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3852 if (status)
3853 return status;
3854 goto alloc_stateid;
3855 new_owner:
3856 oo = alloc_init_open_stateowner(strhashval, open, cstate);
3857 if (oo == NULL)
3858 return nfserr_jukebox;
3859 open->op_openowner = oo;
3860 alloc_stateid:
3861 open->op_stp = nfs4_alloc_open_stateid(clp);
3862 if (!open->op_stp)
3863 return nfserr_jukebox;
3865 if (nfsd4_has_session(cstate) &&
3866 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
3867 open->op_odstate = alloc_clnt_odstate(clp);
3868 if (!open->op_odstate)
3869 return nfserr_jukebox;
3872 return nfs_ok;
3875 static inline __be32
3876 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3878 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3879 return nfserr_openmode;
3880 else
3881 return nfs_ok;
3884 static int share_access_to_flags(u32 share_access)
3886 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3889 static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
3891 struct nfs4_stid *ret;
3893 ret = find_stateid_by_type(cl, s,
3894 NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID);
3895 if (!ret)
3896 return NULL;
3897 return delegstateid(ret);
3900 static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3902 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3903 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3906 static __be32
3907 nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
3908 struct nfs4_delegation **dp)
3910 int flags;
3911 __be32 status = nfserr_bad_stateid;
3912 struct nfs4_delegation *deleg;
3914 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3915 if (deleg == NULL)
3916 goto out;
3917 if (deleg->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) {
3918 nfs4_put_stid(&deleg->dl_stid);
3919 if (cl->cl_minorversion)
3920 status = nfserr_deleg_revoked;
3921 goto out;
3923 flags = share_access_to_flags(open->op_share_access);
3924 status = nfs4_check_delegmode(deleg, flags);
3925 if (status) {
3926 nfs4_put_stid(&deleg->dl_stid);
3927 goto out;
3929 *dp = deleg;
3930 out:
3931 if (!nfsd4_is_deleg_cur(open))
3932 return nfs_ok;
3933 if (status)
3934 return status;
3935 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
3936 return nfs_ok;
3939 static inline int nfs4_access_to_access(u32 nfs4_access)
3941 int flags = 0;
3943 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3944 flags |= NFSD_MAY_READ;
3945 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3946 flags |= NFSD_MAY_WRITE;
3947 return flags;
3950 static inline __be32
3951 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3952 struct nfsd4_open *open)
3954 struct iattr iattr = {
3955 .ia_valid = ATTR_SIZE,
3956 .ia_size = 0,
3958 if (!open->op_truncate)
3959 return 0;
3960 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
3961 return nfserr_inval;
3962 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3965 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
3966 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3967 struct nfsd4_open *open)
3969 struct file *filp = NULL;
3970 __be32 status;
3971 int oflag = nfs4_access_to_omode(open->op_share_access);
3972 int access = nfs4_access_to_access(open->op_share_access);
3973 unsigned char old_access_bmap, old_deny_bmap;
3975 spin_lock(&fp->fi_lock);
3978 * Are we trying to set a deny mode that would conflict with
3979 * current access?
3981 status = nfs4_file_check_deny(fp, open->op_share_deny);
3982 if (status != nfs_ok) {
3983 spin_unlock(&fp->fi_lock);
3984 goto out;
3987 /* set access to the file */
3988 status = nfs4_file_get_access(fp, open->op_share_access);
3989 if (status != nfs_ok) {
3990 spin_unlock(&fp->fi_lock);
3991 goto out;
3994 /* Set access bits in stateid */
3995 old_access_bmap = stp->st_access_bmap;
3996 set_access(open->op_share_access, stp);
3998 /* Set new deny mask */
3999 old_deny_bmap = stp->st_deny_bmap;
4000 set_deny(open->op_share_deny, stp);
4001 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4003 if (!fp->fi_fds[oflag]) {
4004 spin_unlock(&fp->fi_lock);
4005 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
4006 if (status)
4007 goto out_put_access;
4008 spin_lock(&fp->fi_lock);
4009 if (!fp->fi_fds[oflag]) {
4010 fp->fi_fds[oflag] = filp;
4011 filp = NULL;
4014 spin_unlock(&fp->fi_lock);
4015 if (filp)
4016 fput(filp);
4018 status = nfsd4_truncate(rqstp, cur_fh, open);
4019 if (status)
4020 goto out_put_access;
4021 out:
4022 return status;
4023 out_put_access:
4024 stp->st_access_bmap = old_access_bmap;
4025 nfs4_file_put_access(fp, open->op_share_access);
4026 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
4027 goto out;
4030 static __be32
4031 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)
4033 __be32 status;
4034 unsigned char old_deny_bmap = stp->st_deny_bmap;
4036 if (!test_access(open->op_share_access, stp))
4037 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
4039 /* test and set deny mode */
4040 spin_lock(&fp->fi_lock);
4041 status = nfs4_file_check_deny(fp, open->op_share_deny);
4042 if (status == nfs_ok) {
4043 set_deny(open->op_share_deny, stp);
4044 fp->fi_share_deny |=
4045 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4047 spin_unlock(&fp->fi_lock);
4049 if (status != nfs_ok)
4050 return status;
4052 status = nfsd4_truncate(rqstp, cur_fh, open);
4053 if (status != nfs_ok)
4054 reset_union_bmap_deny(old_deny_bmap, stp);
4055 return status;
4058 /* Should we give out recallable state?: */
4059 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
4061 if (clp->cl_cb_state == NFSD4_CB_UP)
4062 return true;
4064 * In the sessions case, since we don't have to establish a
4065 * separate connection for callbacks, we assume it's OK
4066 * until we hear otherwise:
4068 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
4071 static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
4073 struct file_lock *fl;
4075 fl = locks_alloc_lock();
4076 if (!fl)
4077 return NULL;
4078 fl->fl_lmops = &nfsd_lease_mng_ops;
4079 fl->fl_flags = FL_DELEG;
4080 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
4081 fl->fl_end = OFFSET_MAX;
4082 fl->fl_owner = (fl_owner_t)fp;
4083 fl->fl_pid = current->tgid;
4084 return fl;
4088 * nfs4_setlease - Obtain a delegation by requesting lease from vfs layer
4089 * @dp: a pointer to the nfs4_delegation we're adding.
4091 * Return:
4092 * On success: Return code will be 0 on success.
4094 * On error: -EAGAIN if there was an existing delegation.
4095 * nonzero if there is an error in other cases.
4099 static int nfs4_setlease(struct nfs4_delegation *dp)
4101 struct nfs4_file *fp = dp->dl_stid.sc_file;
4102 struct file_lock *fl;
4103 struct file *filp;
4104 int status = 0;
4106 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
4107 if (!fl)
4108 return -ENOMEM;
4109 filp = find_readable_file(fp);
4110 if (!filp) {
4111 /* We should always have a readable file here */
4112 WARN_ON_ONCE(1);
4113 locks_free_lock(fl);
4114 return -EBADF;
4116 fl->fl_file = filp;
4117 status = vfs_setlease(filp, fl->fl_type, &fl, NULL);
4118 if (fl)
4119 locks_free_lock(fl);
4120 if (status)
4121 goto out_fput;
4122 spin_lock(&state_lock);
4123 spin_lock(&fp->fi_lock);
4124 /* Did the lease get broken before we took the lock? */
4125 status = -EAGAIN;
4126 if (fp->fi_had_conflict)
4127 goto out_unlock;
4128 /* Race breaker */
4129 if (fp->fi_deleg_file) {
4130 status = hash_delegation_locked(dp, fp);
4131 goto out_unlock;
4133 fp->fi_deleg_file = filp;
4134 fp->fi_delegees = 0;
4135 status = hash_delegation_locked(dp, fp);
4136 spin_unlock(&fp->fi_lock);
4137 spin_unlock(&state_lock);
4138 if (status) {
4139 /* Should never happen, this is a new fi_deleg_file */
4140 WARN_ON_ONCE(1);
4141 goto out_fput;
4143 return 0;
4144 out_unlock:
4145 spin_unlock(&fp->fi_lock);
4146 spin_unlock(&state_lock);
4147 out_fput:
4148 fput(filp);
4149 return status;
4152 static struct nfs4_delegation *
4153 nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
4154 struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate)
4156 int status;
4157 struct nfs4_delegation *dp;
4159 if (fp->fi_had_conflict)
4160 return ERR_PTR(-EAGAIN);
4162 spin_lock(&state_lock);
4163 spin_lock(&fp->fi_lock);
4164 status = nfs4_get_existing_delegation(clp, fp);
4165 spin_unlock(&fp->fi_lock);
4166 spin_unlock(&state_lock);
4168 if (status)
4169 return ERR_PTR(status);
4171 dp = alloc_init_deleg(clp, fh, odstate);
4172 if (!dp)
4173 return ERR_PTR(-ENOMEM);
4175 get_nfs4_file(fp);
4176 spin_lock(&state_lock);
4177 spin_lock(&fp->fi_lock);
4178 dp->dl_stid.sc_file = fp;
4179 if (!fp->fi_deleg_file) {
4180 spin_unlock(&fp->fi_lock);
4181 spin_unlock(&state_lock);
4182 status = nfs4_setlease(dp);
4183 goto out;
4185 if (fp->fi_had_conflict) {
4186 status = -EAGAIN;
4187 goto out_unlock;
4189 status = hash_delegation_locked(dp, fp);
4190 out_unlock:
4191 spin_unlock(&fp->fi_lock);
4192 spin_unlock(&state_lock);
4193 out:
4194 if (status) {
4195 put_clnt_odstate(dp->dl_clnt_odstate);
4196 nfs4_put_stid(&dp->dl_stid);
4197 return ERR_PTR(status);
4199 return dp;
4202 static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
4204 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4205 if (status == -EAGAIN)
4206 open->op_why_no_deleg = WND4_CONTENTION;
4207 else {
4208 open->op_why_no_deleg = WND4_RESOURCE;
4209 switch (open->op_deleg_want) {
4210 case NFS4_SHARE_WANT_READ_DELEG:
4211 case NFS4_SHARE_WANT_WRITE_DELEG:
4212 case NFS4_SHARE_WANT_ANY_DELEG:
4213 break;
4214 case NFS4_SHARE_WANT_CANCEL:
4215 open->op_why_no_deleg = WND4_CANCELLED;
4216 break;
4217 case NFS4_SHARE_WANT_NO_DELEG:
4218 WARN_ON_ONCE(1);
4224 * Attempt to hand out a delegation.
4226 * Note we don't support write delegations, and won't until the vfs has
4227 * proper support for them.
4229 static void
4230 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
4231 struct nfs4_ol_stateid *stp)
4233 struct nfs4_delegation *dp;
4234 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
4235 struct nfs4_client *clp = stp->st_stid.sc_client;
4236 int cb_up;
4237 int status = 0;
4239 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
4240 open->op_recall = 0;
4241 switch (open->op_claim_type) {
4242 case NFS4_OPEN_CLAIM_PREVIOUS:
4243 if (!cb_up)
4244 open->op_recall = 1;
4245 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
4246 goto out_no_deleg;
4247 break;
4248 case NFS4_OPEN_CLAIM_NULL:
4249 case NFS4_OPEN_CLAIM_FH:
4251 * Let's not give out any delegations till everyone's
4252 * had the chance to reclaim theirs, *and* until
4253 * NLM locks have all been reclaimed:
4255 if (locks_in_grace(clp->net))
4256 goto out_no_deleg;
4257 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
4258 goto out_no_deleg;
4260 * Also, if the file was opened for write or
4261 * create, there's a good chance the client's
4262 * about to write to it, resulting in an
4263 * immediate recall (since we don't support
4264 * write delegations):
4266 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
4267 goto out_no_deleg;
4268 if (open->op_create == NFS4_OPEN_CREATE)
4269 goto out_no_deleg;
4270 break;
4271 default:
4272 goto out_no_deleg;
4274 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate);
4275 if (IS_ERR(dp))
4276 goto out_no_deleg;
4278 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
4280 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
4281 STATEID_VAL(&dp->dl_stid.sc_stateid));
4282 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
4283 nfs4_put_stid(&dp->dl_stid);
4284 return;
4285 out_no_deleg:
4286 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
4287 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
4288 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
4289 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
4290 open->op_recall = 1;
4293 /* 4.1 client asking for a delegation? */
4294 if (open->op_deleg_want)
4295 nfsd4_open_deleg_none_ext(open, status);
4296 return;
4299 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
4300 struct nfs4_delegation *dp)
4302 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
4303 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4304 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4305 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
4306 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
4307 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4308 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4309 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
4311 /* Otherwise the client must be confused wanting a delegation
4312 * it already has, therefore we don't return
4313 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
4317 __be32
4318 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
4320 struct nfsd4_compoundres *resp = rqstp->rq_resp;
4321 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
4322 struct nfs4_file *fp = NULL;
4323 struct nfs4_ol_stateid *stp = NULL;
4324 struct nfs4_delegation *dp = NULL;
4325 __be32 status;
4326 bool new_stp = false;
4329 * Lookup file; if found, lookup stateid and check open request,
4330 * and check for delegations in the process of being recalled.
4331 * If not found, create the nfs4_file struct
4333 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
4334 if (fp != open->op_file) {
4335 status = nfs4_check_deleg(cl, open, &dp);
4336 if (status)
4337 goto out;
4338 stp = nfsd4_find_and_lock_existing_open(fp, open);
4339 } else {
4340 open->op_file = NULL;
4341 status = nfserr_bad_stateid;
4342 if (nfsd4_is_deleg_cur(open))
4343 goto out;
4346 if (!stp) {
4347 stp = init_open_stateid(fp, open);
4348 if (!open->op_stp)
4349 new_stp = true;
4353 * OPEN the file, or upgrade an existing OPEN.
4354 * If truncate fails, the OPEN fails.
4356 * stp is already locked.
4358 if (!new_stp) {
4359 /* Stateid was found, this is an OPEN upgrade */
4360 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
4361 if (status) {
4362 mutex_unlock(&stp->st_mutex);
4363 goto out;
4365 } else {
4366 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4367 if (status) {
4368 stp->st_stid.sc_type = NFS4_CLOSED_STID;
4369 release_open_stateid(stp);
4370 mutex_unlock(&stp->st_mutex);
4371 goto out;
4374 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
4375 open->op_odstate);
4376 if (stp->st_clnt_odstate == open->op_odstate)
4377 open->op_odstate = NULL;
4380 nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
4381 mutex_unlock(&stp->st_mutex);
4383 if (nfsd4_has_session(&resp->cstate)) {
4384 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4385 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4386 open->op_why_no_deleg = WND4_NOT_WANTED;
4387 goto nodeleg;
4392 * Attempt to hand out a delegation. No error return, because the
4393 * OPEN succeeds even if we fail.
4395 nfs4_open_delegation(current_fh, open, stp);
4396 nodeleg:
4397 status = nfs_ok;
4399 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
4400 STATEID_VAL(&stp->st_stid.sc_stateid));
4401 out:
4402 /* 4.1 client trying to upgrade/downgrade delegation? */
4403 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
4404 open->op_deleg_want)
4405 nfsd4_deleg_xgrade_none_ext(open, dp);
4407 if (fp)
4408 put_nfs4_file(fp);
4409 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
4410 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
4412 * To finish the open response, we just need to set the rflags.
4414 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
4415 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
4416 !nfsd4_has_session(&resp->cstate))
4417 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
4418 if (dp)
4419 nfs4_put_stid(&dp->dl_stid);
4420 if (stp)
4421 nfs4_put_stid(&stp->st_stid);
4423 return status;
4426 void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
4427 struct nfsd4_open *open)
4429 if (open->op_openowner) {
4430 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
4432 nfsd4_cstate_assign_replay(cstate, so);
4433 nfs4_put_stateowner(so);
4435 if (open->op_file)
4436 kmem_cache_free(file_slab, open->op_file);
4437 if (open->op_stp)
4438 nfs4_put_stid(&open->op_stp->st_stid);
4439 if (open->op_odstate)
4440 kmem_cache_free(odstate_slab, open->op_odstate);
4443 __be32
4444 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4445 clientid_t *clid)
4447 struct nfs4_client *clp;
4448 __be32 status;
4449 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4451 dprintk("process_renew(%08x/%08x): starting\n",
4452 clid->cl_boot, clid->cl_id);
4453 status = lookup_clientid(clid, cstate, nn);
4454 if (status)
4455 goto out;
4456 clp = cstate->clp;
4457 status = nfserr_cb_path_down;
4458 if (!list_empty(&clp->cl_delegations)
4459 && clp->cl_cb_state != NFSD4_CB_UP)
4460 goto out;
4461 status = nfs_ok;
4462 out:
4463 return status;
4466 void
4467 nfsd4_end_grace(struct nfsd_net *nn)
4469 /* do nothing if grace period already ended */
4470 if (nn->grace_ended)
4471 return;
4473 dprintk("NFSD: end of grace period\n");
4474 nn->grace_ended = true;
4476 * If the server goes down again right now, an NFSv4
4477 * client will still be allowed to reclaim after it comes back up,
4478 * even if it hasn't yet had a chance to reclaim state this time.
4481 nfsd4_record_grace_done(nn);
4483 * At this point, NFSv4 clients can still reclaim. But if the
4484 * server crashes, any that have not yet reclaimed will be out
4485 * of luck on the next boot.
4487 * (NFSv4.1+ clients are considered to have reclaimed once they
4488 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
4489 * have reclaimed after their first OPEN.)
4491 locks_end_grace(&nn->nfsd4_manager);
4493 * At this point, and once lockd and/or any other containers
4494 * exit their grace period, further reclaims will fail and
4495 * regular locking can resume.
4499 static time_t
4500 nfs4_laundromat(struct nfsd_net *nn)
4502 struct nfs4_client *clp;
4503 struct nfs4_openowner *oo;
4504 struct nfs4_delegation *dp;
4505 struct nfs4_ol_stateid *stp;
4506 struct list_head *pos, *next, reaplist;
4507 time_t cutoff = get_seconds() - nn->nfsd4_lease;
4508 time_t t, new_timeo = nn->nfsd4_lease;
4510 dprintk("NFSD: laundromat service - starting\n");
4511 nfsd4_end_grace(nn);
4512 INIT_LIST_HEAD(&reaplist);
4513 spin_lock(&nn->client_lock);
4514 list_for_each_safe(pos, next, &nn->client_lru) {
4515 clp = list_entry(pos, struct nfs4_client, cl_lru);
4516 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4517 t = clp->cl_time - cutoff;
4518 new_timeo = min(new_timeo, t);
4519 break;
4521 if (mark_client_expired_locked(clp)) {
4522 dprintk("NFSD: client in use (clientid %08x)\n",
4523 clp->cl_clientid.cl_id);
4524 continue;
4526 list_add(&clp->cl_lru, &reaplist);
4528 spin_unlock(&nn->client_lock);
4529 list_for_each_safe(pos, next, &reaplist) {
4530 clp = list_entry(pos, struct nfs4_client, cl_lru);
4531 dprintk("NFSD: purging unused client (clientid %08x)\n",
4532 clp->cl_clientid.cl_id);
4533 list_del_init(&clp->cl_lru);
4534 expire_client(clp);
4536 spin_lock(&state_lock);
4537 list_for_each_safe(pos, next, &nn->del_recall_lru) {
4538 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4539 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
4540 t = dp->dl_time - cutoff;
4541 new_timeo = min(new_timeo, t);
4542 break;
4544 WARN_ON(!unhash_delegation_locked(dp));
4545 list_add(&dp->dl_recall_lru, &reaplist);
4547 spin_unlock(&state_lock);
4548 while (!list_empty(&reaplist)) {
4549 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4550 dl_recall_lru);
4551 list_del_init(&dp->dl_recall_lru);
4552 revoke_delegation(dp);
4555 spin_lock(&nn->client_lock);
4556 while (!list_empty(&nn->close_lru)) {
4557 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
4558 oo_close_lru);
4559 if (time_after((unsigned long)oo->oo_time,
4560 (unsigned long)cutoff)) {
4561 t = oo->oo_time - cutoff;
4562 new_timeo = min(new_timeo, t);
4563 break;
4565 list_del_init(&oo->oo_close_lru);
4566 stp = oo->oo_last_closed_stid;
4567 oo->oo_last_closed_stid = NULL;
4568 spin_unlock(&nn->client_lock);
4569 nfs4_put_stid(&stp->st_stid);
4570 spin_lock(&nn->client_lock);
4572 spin_unlock(&nn->client_lock);
4574 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
4575 return new_timeo;
4578 static struct workqueue_struct *laundry_wq;
4579 static void laundromat_main(struct work_struct *);
4581 static void
4582 laundromat_main(struct work_struct *laundry)
4584 time_t t;
4585 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
4586 work);
4587 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4588 laundromat_work);
4590 t = nfs4_laundromat(nn);
4591 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
4592 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
4595 static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
4597 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
4598 return nfserr_bad_stateid;
4599 return nfs_ok;
4602 static inline int
4603 access_permit_read(struct nfs4_ol_stateid *stp)
4605 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4606 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4607 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
4610 static inline int
4611 access_permit_write(struct nfs4_ol_stateid *stp)
4613 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4614 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
4617 static
4618 __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
4620 __be32 status = nfserr_openmode;
4622 /* For lock stateid's, we test the parent open, not the lock: */
4623 if (stp->st_openstp)
4624 stp = stp->st_openstp;
4625 if ((flags & WR_STATE) && !access_permit_write(stp))
4626 goto out;
4627 if ((flags & RD_STATE) && !access_permit_read(stp))
4628 goto out;
4629 status = nfs_ok;
4630 out:
4631 return status;
4634 static inline __be32
4635 check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
4637 if (ONE_STATEID(stateid) && (flags & RD_STATE))
4638 return nfs_ok;
4639 else if (opens_in_grace(net)) {
4640 /* Answer in remaining cases depends on existence of
4641 * conflicting state; so we must wait out the grace period. */
4642 return nfserr_grace;
4643 } else if (flags & WR_STATE)
4644 return nfs4_share_conflict(current_fh,
4645 NFS4_SHARE_DENY_WRITE);
4646 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4647 return nfs4_share_conflict(current_fh,
4648 NFS4_SHARE_DENY_READ);
4652 * Allow READ/WRITE during grace period on recovered state only for files
4653 * that are not able to provide mandatory locking.
4655 static inline int
4656 grace_disallows_io(struct net *net, struct inode *inode)
4658 return opens_in_grace(net) && mandatory_lock(inode);
4661 /* Returns true iff a is later than b: */
4662 static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4664 return (s32)(a->si_generation - b->si_generation) > 0;
4667 static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
4670 * When sessions are used the stateid generation number is ignored
4671 * when it is zero.
4673 if (has_session && in->si_generation == 0)
4674 return nfs_ok;
4676 if (in->si_generation == ref->si_generation)
4677 return nfs_ok;
4679 /* If the client sends us a stateid from the future, it's buggy: */
4680 if (stateid_generation_after(in, ref))
4681 return nfserr_bad_stateid;
4683 * However, we could see a stateid from the past, even from a
4684 * non-buggy client. For example, if the client sends a lock
4685 * while some IO is outstanding, the lock may bump si_generation
4686 * while the IO is still in flight. The client could avoid that
4687 * situation by waiting for responses on all the IO requests,
4688 * but better performance may result in retrying IO that
4689 * receives an old_stateid error if requests are rarely
4690 * reordered in flight:
4692 return nfserr_old_stateid;
4695 static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
4697 if (ols->st_stateowner->so_is_open_owner &&
4698 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
4699 return nfserr_bad_stateid;
4700 return nfs_ok;
4703 static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
4705 struct nfs4_stid *s;
4706 __be32 status = nfserr_bad_stateid;
4708 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
4709 CLOSE_STATEID(stateid))
4710 return status;
4711 /* Client debugging aid. */
4712 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4713 char addr_str[INET6_ADDRSTRLEN];
4714 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4715 sizeof(addr_str));
4716 pr_warn_ratelimited("NFSD: client %s testing state ID "
4717 "with incorrect client ID\n", addr_str);
4718 return status;
4720 spin_lock(&cl->cl_lock);
4721 s = find_stateid_locked(cl, stateid);
4722 if (!s)
4723 goto out_unlock;
4724 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
4725 if (status)
4726 goto out_unlock;
4727 switch (s->sc_type) {
4728 case NFS4_DELEG_STID:
4729 status = nfs_ok;
4730 break;
4731 case NFS4_REVOKED_DELEG_STID:
4732 status = nfserr_deleg_revoked;
4733 break;
4734 case NFS4_OPEN_STID:
4735 case NFS4_LOCK_STID:
4736 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
4737 break;
4738 default:
4739 printk("unknown stateid type %x\n", s->sc_type);
4740 /* Fallthrough */
4741 case NFS4_CLOSED_STID:
4742 case NFS4_CLOSED_DELEG_STID:
4743 status = nfserr_bad_stateid;
4745 out_unlock:
4746 spin_unlock(&cl->cl_lock);
4747 return status;
4750 __be32
4751 nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4752 stateid_t *stateid, unsigned char typemask,
4753 struct nfs4_stid **s, struct nfsd_net *nn)
4755 __be32 status;
4756 bool return_revoked = false;
4759 * only return revoked delegations if explicitly asked.
4760 * otherwise we report revoked or bad_stateid status.
4762 if (typemask & NFS4_REVOKED_DELEG_STID)
4763 return_revoked = true;
4764 else if (typemask & NFS4_DELEG_STID)
4765 typemask |= NFS4_REVOKED_DELEG_STID;
4767 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
4768 CLOSE_STATEID(stateid))
4769 return nfserr_bad_stateid;
4770 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
4771 if (status == nfserr_stale_clientid) {
4772 if (cstate->session)
4773 return nfserr_bad_stateid;
4774 return nfserr_stale_stateid;
4776 if (status)
4777 return status;
4778 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
4779 if (!*s)
4780 return nfserr_bad_stateid;
4781 if (((*s)->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) {
4782 nfs4_put_stid(*s);
4783 if (cstate->minorversion)
4784 return nfserr_deleg_revoked;
4785 return nfserr_bad_stateid;
4787 return nfs_ok;
4790 static struct file *
4791 nfs4_find_file(struct nfs4_stid *s, int flags)
4793 if (!s)
4794 return NULL;
4796 switch (s->sc_type) {
4797 case NFS4_DELEG_STID:
4798 if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
4799 return NULL;
4800 return get_file(s->sc_file->fi_deleg_file);
4801 case NFS4_OPEN_STID:
4802 case NFS4_LOCK_STID:
4803 if (flags & RD_STATE)
4804 return find_readable_file(s->sc_file);
4805 else
4806 return find_writeable_file(s->sc_file);
4807 break;
4810 return NULL;
4813 static __be32
4814 nfs4_check_olstateid(struct svc_fh *fhp, struct nfs4_ol_stateid *ols, int flags)
4816 __be32 status;
4818 status = nfsd4_check_openowner_confirmed(ols);
4819 if (status)
4820 return status;
4821 return nfs4_check_openmode(ols, flags);
4824 static __be32
4825 nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
4826 struct file **filpp, bool *tmp_file, int flags)
4828 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
4829 struct file *file;
4830 __be32 status;
4832 file = nfs4_find_file(s, flags);
4833 if (file) {
4834 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
4835 acc | NFSD_MAY_OWNER_OVERRIDE);
4836 if (status) {
4837 fput(file);
4838 return status;
4841 *filpp = file;
4842 } else {
4843 status = nfsd_open(rqstp, fhp, S_IFREG, acc, filpp);
4844 if (status)
4845 return status;
4847 if (tmp_file)
4848 *tmp_file = true;
4851 return 0;
4855 * Checks for stateid operations
4857 __be32
4858 nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
4859 struct nfsd4_compound_state *cstate, stateid_t *stateid,
4860 int flags, struct file **filpp, bool *tmp_file)
4862 struct svc_fh *fhp = &cstate->current_fh;
4863 struct inode *ino = d_inode(fhp->fh_dentry);
4864 struct net *net = SVC_NET(rqstp);
4865 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4866 struct nfs4_stid *s = NULL;
4867 __be32 status;
4869 if (filpp)
4870 *filpp = NULL;
4871 if (tmp_file)
4872 *tmp_file = false;
4874 if (grace_disallows_io(net, ino))
4875 return nfserr_grace;
4877 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
4878 status = check_special_stateids(net, fhp, stateid, flags);
4879 goto done;
4882 status = nfsd4_lookup_stateid(cstate, stateid,
4883 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
4884 &s, nn);
4885 if (status)
4886 return status;
4887 status = check_stateid_generation(stateid, &s->sc_stateid,
4888 nfsd4_has_session(cstate));
4889 if (status)
4890 goto out;
4892 switch (s->sc_type) {
4893 case NFS4_DELEG_STID:
4894 status = nfs4_check_delegmode(delegstateid(s), flags);
4895 break;
4896 case NFS4_OPEN_STID:
4897 case NFS4_LOCK_STID:
4898 status = nfs4_check_olstateid(fhp, openlockstateid(s), flags);
4899 break;
4900 default:
4901 status = nfserr_bad_stateid;
4902 break;
4904 if (status)
4905 goto out;
4906 status = nfs4_check_fh(fhp, s);
4908 done:
4909 if (!status && filpp)
4910 status = nfs4_check_file(rqstp, fhp, s, filpp, tmp_file, flags);
4911 out:
4912 if (s)
4913 nfs4_put_stid(s);
4914 return status;
4918 * Test if the stateid is valid
4920 __be32
4921 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4922 struct nfsd4_test_stateid *test_stateid)
4924 struct nfsd4_test_stateid_id *stateid;
4925 struct nfs4_client *cl = cstate->session->se_client;
4927 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
4928 stateid->ts_id_status =
4929 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
4931 return nfs_ok;
4934 static __be32
4935 nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
4937 struct nfs4_ol_stateid *stp = openlockstateid(s);
4938 __be32 ret;
4940 mutex_lock(&stp->st_mutex);
4942 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4943 if (ret)
4944 goto out;
4946 ret = nfserr_locks_held;
4947 if (check_for_locks(stp->st_stid.sc_file,
4948 lockowner(stp->st_stateowner)))
4949 goto out;
4951 release_lock_stateid(stp);
4952 ret = nfs_ok;
4954 out:
4955 mutex_unlock(&stp->st_mutex);
4956 nfs4_put_stid(s);
4957 return ret;
4960 __be32
4961 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4962 struct nfsd4_free_stateid *free_stateid)
4964 stateid_t *stateid = &free_stateid->fr_stateid;
4965 struct nfs4_stid *s;
4966 struct nfs4_delegation *dp;
4967 struct nfs4_client *cl = cstate->session->se_client;
4968 __be32 ret = nfserr_bad_stateid;
4970 spin_lock(&cl->cl_lock);
4971 s = find_stateid_locked(cl, stateid);
4972 if (!s)
4973 goto out_unlock;
4974 switch (s->sc_type) {
4975 case NFS4_DELEG_STID:
4976 ret = nfserr_locks_held;
4977 break;
4978 case NFS4_OPEN_STID:
4979 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4980 if (ret)
4981 break;
4982 ret = nfserr_locks_held;
4983 break;
4984 case NFS4_LOCK_STID:
4985 atomic_inc(&s->sc_count);
4986 spin_unlock(&cl->cl_lock);
4987 ret = nfsd4_free_lock_stateid(stateid, s);
4988 goto out;
4989 case NFS4_REVOKED_DELEG_STID:
4990 dp = delegstateid(s);
4991 list_del_init(&dp->dl_recall_lru);
4992 spin_unlock(&cl->cl_lock);
4993 nfs4_put_stid(s);
4994 ret = nfs_ok;
4995 goto out;
4996 /* Default falls through and returns nfserr_bad_stateid */
4998 out_unlock:
4999 spin_unlock(&cl->cl_lock);
5000 out:
5001 return ret;
5004 static inline int
5005 setlkflg (int type)
5007 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
5008 RD_STATE : WR_STATE;
5011 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
5013 struct svc_fh *current_fh = &cstate->current_fh;
5014 struct nfs4_stateowner *sop = stp->st_stateowner;
5015 __be32 status;
5017 status = nfsd4_check_seqid(cstate, sop, seqid);
5018 if (status)
5019 return status;
5020 status = nfsd4_lock_ol_stateid(stp);
5021 if (status != nfs_ok)
5022 return status;
5023 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
5024 if (status == nfs_ok)
5025 status = nfs4_check_fh(current_fh, &stp->st_stid);
5026 if (status != nfs_ok)
5027 mutex_unlock(&stp->st_mutex);
5028 return status;
5032 * Checks for sequence id mutating operations.
5034 static __be32
5035 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
5036 stateid_t *stateid, char typemask,
5037 struct nfs4_ol_stateid **stpp,
5038 struct nfsd_net *nn)
5040 __be32 status;
5041 struct nfs4_stid *s;
5042 struct nfs4_ol_stateid *stp = NULL;
5044 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
5045 seqid, STATEID_VAL(stateid));
5047 *stpp = NULL;
5048 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
5049 if (status)
5050 return status;
5051 stp = openlockstateid(s);
5052 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
5054 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
5055 if (!status)
5056 *stpp = stp;
5057 else
5058 nfs4_put_stid(&stp->st_stid);
5059 return status;
5062 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
5063 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
5065 __be32 status;
5066 struct nfs4_openowner *oo;
5067 struct nfs4_ol_stateid *stp;
5069 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
5070 NFS4_OPEN_STID, &stp, nn);
5071 if (status)
5072 return status;
5073 oo = openowner(stp->st_stateowner);
5074 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
5075 mutex_unlock(&stp->st_mutex);
5076 nfs4_put_stid(&stp->st_stid);
5077 return nfserr_bad_stateid;
5079 *stpp = stp;
5080 return nfs_ok;
5083 __be32
5084 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5085 struct nfsd4_open_confirm *oc)
5087 __be32 status;
5088 struct nfs4_openowner *oo;
5089 struct nfs4_ol_stateid *stp;
5090 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5092 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
5093 cstate->current_fh.fh_dentry);
5095 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
5096 if (status)
5097 return status;
5099 status = nfs4_preprocess_seqid_op(cstate,
5100 oc->oc_seqid, &oc->oc_req_stateid,
5101 NFS4_OPEN_STID, &stp, nn);
5102 if (status)
5103 goto out;
5104 oo = openowner(stp->st_stateowner);
5105 status = nfserr_bad_stateid;
5106 if (oo->oo_flags & NFS4_OO_CONFIRMED) {
5107 mutex_unlock(&stp->st_mutex);
5108 goto put_stateid;
5110 oo->oo_flags |= NFS4_OO_CONFIRMED;
5111 nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
5112 mutex_unlock(&stp->st_mutex);
5113 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
5114 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
5116 nfsd4_client_record_create(oo->oo_owner.so_client);
5117 status = nfs_ok;
5118 put_stateid:
5119 nfs4_put_stid(&stp->st_stid);
5120 out:
5121 nfsd4_bump_seqid(cstate, status);
5122 return status;
5125 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
5127 if (!test_access(access, stp))
5128 return;
5129 nfs4_file_put_access(stp->st_stid.sc_file, access);
5130 clear_access(access, stp);
5133 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
5135 switch (to_access) {
5136 case NFS4_SHARE_ACCESS_READ:
5137 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
5138 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
5139 break;
5140 case NFS4_SHARE_ACCESS_WRITE:
5141 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
5142 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
5143 break;
5144 case NFS4_SHARE_ACCESS_BOTH:
5145 break;
5146 default:
5147 WARN_ON_ONCE(1);
5151 __be32
5152 nfsd4_open_downgrade(struct svc_rqst *rqstp,
5153 struct nfsd4_compound_state *cstate,
5154 struct nfsd4_open_downgrade *od)
5156 __be32 status;
5157 struct nfs4_ol_stateid *stp;
5158 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5160 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
5161 cstate->current_fh.fh_dentry);
5163 /* We don't yet support WANT bits: */
5164 if (od->od_deleg_want)
5165 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
5166 od->od_deleg_want);
5168 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
5169 &od->od_stateid, &stp, nn);
5170 if (status)
5171 goto out;
5172 status = nfserr_inval;
5173 if (!test_access(od->od_share_access, stp)) {
5174 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
5175 stp->st_access_bmap, od->od_share_access);
5176 goto put_stateid;
5178 if (!test_deny(od->od_share_deny, stp)) {
5179 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
5180 stp->st_deny_bmap, od->od_share_deny);
5181 goto put_stateid;
5183 nfs4_stateid_downgrade(stp, od->od_share_access);
5184 reset_union_bmap_deny(od->od_share_deny, stp);
5185 nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
5186 status = nfs_ok;
5187 put_stateid:
5188 mutex_unlock(&stp->st_mutex);
5189 nfs4_put_stid(&stp->st_stid);
5190 out:
5191 nfsd4_bump_seqid(cstate, status);
5192 return status;
5195 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
5197 struct nfs4_client *clp = s->st_stid.sc_client;
5198 bool unhashed;
5199 LIST_HEAD(reaplist);
5201 spin_lock(&clp->cl_lock);
5202 unhashed = unhash_open_stateid(s, &reaplist);
5204 if (clp->cl_minorversion) {
5205 if (unhashed)
5206 put_ol_stateid_locked(s, &reaplist);
5207 spin_unlock(&clp->cl_lock);
5208 free_ol_stateid_reaplist(&reaplist);
5209 } else {
5210 spin_unlock(&clp->cl_lock);
5211 free_ol_stateid_reaplist(&reaplist);
5212 if (unhashed)
5213 move_to_close_lru(s, clp->net);
5218 * nfs4_unlock_state() called after encode
5220 __be32
5221 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5222 struct nfsd4_close *close)
5224 __be32 status;
5225 struct nfs4_ol_stateid *stp;
5226 struct net *net = SVC_NET(rqstp);
5227 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5229 dprintk("NFSD: nfsd4_close on file %pd\n",
5230 cstate->current_fh.fh_dentry);
5232 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
5233 &close->cl_stateid,
5234 NFS4_OPEN_STID|NFS4_CLOSED_STID,
5235 &stp, nn);
5236 nfsd4_bump_seqid(cstate, status);
5237 if (status)
5238 goto out;
5240 stp->st_stid.sc_type = NFS4_CLOSED_STID;
5241 nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
5243 nfsd4_close_open_stateid(stp);
5244 mutex_unlock(&stp->st_mutex);
5246 /* See RFC5661 sectionm 18.2.4 */
5247 if (stp->st_stid.sc_client->cl_minorversion)
5248 memcpy(&close->cl_stateid, &close_stateid,
5249 sizeof(close->cl_stateid));
5251 /* put reference from nfs4_preprocess_seqid_op */
5252 nfs4_put_stid(&stp->st_stid);
5253 out:
5254 return status;
5257 __be32
5258 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5259 struct nfsd4_delegreturn *dr)
5261 struct nfs4_delegation *dp;
5262 stateid_t *stateid = &dr->dr_stateid;
5263 struct nfs4_stid *s;
5264 __be32 status;
5265 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5267 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
5268 return status;
5270 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
5271 if (status)
5272 goto out;
5273 dp = delegstateid(s);
5274 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
5275 if (status)
5276 goto put_stateid;
5278 destroy_delegation(dp);
5279 put_stateid:
5280 nfs4_put_stid(&dp->dl_stid);
5281 out:
5282 return status;
5285 static inline u64
5286 end_offset(u64 start, u64 len)
5288 u64 end;
5290 end = start + len;
5291 return end >= start ? end: NFS4_MAX_UINT64;
5294 /* last octet in a range */
5295 static inline u64
5296 last_byte_offset(u64 start, u64 len)
5298 u64 end;
5300 WARN_ON_ONCE(!len);
5301 end = start + len;
5302 return end > start ? end - 1: NFS4_MAX_UINT64;
5306 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
5307 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
5308 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
5309 * locking, this prevents us from being completely protocol-compliant. The
5310 * real solution to this problem is to start using unsigned file offsets in
5311 * the VFS, but this is a very deep change!
5313 static inline void
5314 nfs4_transform_lock_offset(struct file_lock *lock)
5316 if (lock->fl_start < 0)
5317 lock->fl_start = OFFSET_MAX;
5318 if (lock->fl_end < 0)
5319 lock->fl_end = OFFSET_MAX;
5322 static fl_owner_t
5323 nfsd4_fl_get_owner(fl_owner_t owner)
5325 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
5327 nfs4_get_stateowner(&lo->lo_owner);
5328 return owner;
5331 static void
5332 nfsd4_fl_put_owner(fl_owner_t owner)
5334 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
5336 if (lo)
5337 nfs4_put_stateowner(&lo->lo_owner);
5340 static const struct lock_manager_operations nfsd_posix_mng_ops = {
5341 .lm_get_owner = nfsd4_fl_get_owner,
5342 .lm_put_owner = nfsd4_fl_put_owner,
5345 static inline void
5346 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
5348 struct nfs4_lockowner *lo;
5350 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
5351 lo = (struct nfs4_lockowner *) fl->fl_owner;
5352 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
5353 lo->lo_owner.so_owner.len, GFP_KERNEL);
5354 if (!deny->ld_owner.data)
5355 /* We just don't care that much */
5356 goto nevermind;
5357 deny->ld_owner.len = lo->lo_owner.so_owner.len;
5358 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
5359 } else {
5360 nevermind:
5361 deny->ld_owner.len = 0;
5362 deny->ld_owner.data = NULL;
5363 deny->ld_clientid.cl_boot = 0;
5364 deny->ld_clientid.cl_id = 0;
5366 deny->ld_start = fl->fl_start;
5367 deny->ld_length = NFS4_MAX_UINT64;
5368 if (fl->fl_end != NFS4_MAX_UINT64)
5369 deny->ld_length = fl->fl_end - fl->fl_start + 1;
5370 deny->ld_type = NFS4_READ_LT;
5371 if (fl->fl_type != F_RDLCK)
5372 deny->ld_type = NFS4_WRITE_LT;
5375 static struct nfs4_lockowner *
5376 find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
5378 unsigned int strhashval = ownerstr_hashval(owner);
5379 struct nfs4_stateowner *so;
5381 lockdep_assert_held(&clp->cl_lock);
5383 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
5384 so_strhash) {
5385 if (so->so_is_open_owner)
5386 continue;
5387 if (same_owner_str(so, owner))
5388 return lockowner(nfs4_get_stateowner(so));
5390 return NULL;
5393 static struct nfs4_lockowner *
5394 find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
5396 struct nfs4_lockowner *lo;
5398 spin_lock(&clp->cl_lock);
5399 lo = find_lockowner_str_locked(clp, owner);
5400 spin_unlock(&clp->cl_lock);
5401 return lo;
5404 static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
5406 unhash_lockowner_locked(lockowner(sop));
5409 static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
5411 struct nfs4_lockowner *lo = lockowner(sop);
5413 kmem_cache_free(lockowner_slab, lo);
5416 static const struct nfs4_stateowner_operations lockowner_ops = {
5417 .so_unhash = nfs4_unhash_lockowner,
5418 .so_free = nfs4_free_lockowner,
5422 * Alloc a lock owner structure.
5423 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
5424 * occurred.
5426 * strhashval = ownerstr_hashval
5428 static struct nfs4_lockowner *
5429 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
5430 struct nfs4_ol_stateid *open_stp,
5431 struct nfsd4_lock *lock)
5433 struct nfs4_lockowner *lo, *ret;
5435 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
5436 if (!lo)
5437 return NULL;
5438 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
5439 lo->lo_owner.so_is_open_owner = 0;
5440 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
5441 lo->lo_owner.so_ops = &lockowner_ops;
5442 spin_lock(&clp->cl_lock);
5443 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
5444 if (ret == NULL) {
5445 list_add(&lo->lo_owner.so_strhash,
5446 &clp->cl_ownerstr_hashtbl[strhashval]);
5447 ret = lo;
5448 } else
5449 nfs4_free_stateowner(&lo->lo_owner);
5451 spin_unlock(&clp->cl_lock);
5452 return ret;
5455 static void
5456 init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
5457 struct nfs4_file *fp, struct inode *inode,
5458 struct nfs4_ol_stateid *open_stp)
5460 struct nfs4_client *clp = lo->lo_owner.so_client;
5462 lockdep_assert_held(&clp->cl_lock);
5464 atomic_inc(&stp->st_stid.sc_count);
5465 stp->st_stid.sc_type = NFS4_LOCK_STID;
5466 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
5467 get_nfs4_file(fp);
5468 stp->st_stid.sc_file = fp;
5469 stp->st_access_bmap = 0;
5470 stp->st_deny_bmap = open_stp->st_deny_bmap;
5471 stp->st_openstp = open_stp;
5472 mutex_init(&stp->st_mutex);
5473 list_add(&stp->st_locks, &open_stp->st_locks);
5474 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
5475 spin_lock(&fp->fi_lock);
5476 list_add(&stp->st_perfile, &fp->fi_stateids);
5477 spin_unlock(&fp->fi_lock);
5480 static struct nfs4_ol_stateid *
5481 find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5483 struct nfs4_ol_stateid *lst;
5484 struct nfs4_client *clp = lo->lo_owner.so_client;
5486 lockdep_assert_held(&clp->cl_lock);
5488 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
5489 if (lst->st_stid.sc_file == fp) {
5490 atomic_inc(&lst->st_stid.sc_count);
5491 return lst;
5494 return NULL;
5497 static struct nfs4_ol_stateid *
5498 find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5499 struct inode *inode, struct nfs4_ol_stateid *ost,
5500 bool *new)
5502 struct nfs4_stid *ns = NULL;
5503 struct nfs4_ol_stateid *lst;
5504 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5505 struct nfs4_client *clp = oo->oo_owner.so_client;
5507 spin_lock(&clp->cl_lock);
5508 lst = find_lock_stateid(lo, fi);
5509 if (lst == NULL) {
5510 spin_unlock(&clp->cl_lock);
5511 ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid);
5512 if (ns == NULL)
5513 return NULL;
5515 spin_lock(&clp->cl_lock);
5516 lst = find_lock_stateid(lo, fi);
5517 if (likely(!lst)) {
5518 lst = openlockstateid(ns);
5519 init_lock_stateid(lst, lo, fi, inode, ost);
5520 ns = NULL;
5521 *new = true;
5524 spin_unlock(&clp->cl_lock);
5525 if (ns)
5526 nfs4_put_stid(ns);
5527 return lst;
5530 static int
5531 check_lock_length(u64 offset, u64 length)
5533 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
5534 (length > ~offset)));
5537 static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
5539 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
5541 lockdep_assert_held(&fp->fi_lock);
5543 if (test_access(access, lock_stp))
5544 return;
5545 __nfs4_file_get_access(fp, access);
5546 set_access(access, lock_stp);
5549 static __be32
5550 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5551 struct nfs4_ol_stateid *ost,
5552 struct nfsd4_lock *lock,
5553 struct nfs4_ol_stateid **plst, bool *new)
5555 __be32 status;
5556 struct nfs4_file *fi = ost->st_stid.sc_file;
5557 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5558 struct nfs4_client *cl = oo->oo_owner.so_client;
5559 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
5560 struct nfs4_lockowner *lo;
5561 struct nfs4_ol_stateid *lst;
5562 unsigned int strhashval;
5563 bool hashed;
5565 lo = find_lockowner_str(cl, &lock->lk_new_owner);
5566 if (!lo) {
5567 strhashval = ownerstr_hashval(&lock->lk_new_owner);
5568 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5569 if (lo == NULL)
5570 return nfserr_jukebox;
5571 } else {
5572 /* with an existing lockowner, seqids must be the same */
5573 status = nfserr_bad_seqid;
5574 if (!cstate->minorversion &&
5575 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
5576 goto out;
5579 retry:
5580 lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
5581 if (lst == NULL) {
5582 status = nfserr_jukebox;
5583 goto out;
5586 mutex_lock(&lst->st_mutex);
5588 /* See if it's still hashed to avoid race with FREE_STATEID */
5589 spin_lock(&cl->cl_lock);
5590 hashed = !list_empty(&lst->st_perfile);
5591 spin_unlock(&cl->cl_lock);
5593 if (!hashed) {
5594 mutex_unlock(&lst->st_mutex);
5595 nfs4_put_stid(&lst->st_stid);
5596 goto retry;
5598 status = nfs_ok;
5599 *plst = lst;
5600 out:
5601 nfs4_put_stateowner(&lo->lo_owner);
5602 return status;
5606 * LOCK operation
5608 __be32
5609 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5610 struct nfsd4_lock *lock)
5612 struct nfs4_openowner *open_sop = NULL;
5613 struct nfs4_lockowner *lock_sop = NULL;
5614 struct nfs4_ol_stateid *lock_stp = NULL;
5615 struct nfs4_ol_stateid *open_stp = NULL;
5616 struct nfs4_file *fp;
5617 struct file *filp = NULL;
5618 struct file_lock *file_lock = NULL;
5619 struct file_lock *conflock = NULL;
5620 __be32 status = 0;
5621 int lkflg;
5622 int err;
5623 bool new = false;
5624 struct net *net = SVC_NET(rqstp);
5625 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5627 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5628 (long long) lock->lk_offset,
5629 (long long) lock->lk_length);
5631 if (check_lock_length(lock->lk_offset, lock->lk_length))
5632 return nfserr_inval;
5634 if ((status = fh_verify(rqstp, &cstate->current_fh,
5635 S_IFREG, NFSD_MAY_LOCK))) {
5636 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5637 return status;
5640 if (lock->lk_is_new) {
5641 if (nfsd4_has_session(cstate))
5642 /* See rfc 5661 18.10.3: given clientid is ignored: */
5643 memcpy(&lock->lk_new_clientid,
5644 &cstate->session->se_client->cl_clientid,
5645 sizeof(clientid_t));
5647 status = nfserr_stale_clientid;
5648 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
5649 goto out;
5651 /* validate and update open stateid and open seqid */
5652 status = nfs4_preprocess_confirmed_seqid_op(cstate,
5653 lock->lk_new_open_seqid,
5654 &lock->lk_new_open_stateid,
5655 &open_stp, nn);
5656 if (status)
5657 goto out;
5658 mutex_unlock(&open_stp->st_mutex);
5659 open_sop = openowner(open_stp->st_stateowner);
5660 status = nfserr_bad_stateid;
5661 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
5662 &lock->lk_new_clientid))
5663 goto out;
5664 status = lookup_or_create_lock_state(cstate, open_stp, lock,
5665 &lock_stp, &new);
5666 } else {
5667 status = nfs4_preprocess_seqid_op(cstate,
5668 lock->lk_old_lock_seqid,
5669 &lock->lk_old_lock_stateid,
5670 NFS4_LOCK_STID, &lock_stp, nn);
5672 if (status)
5673 goto out;
5674 lock_sop = lockowner(lock_stp->st_stateowner);
5676 lkflg = setlkflg(lock->lk_type);
5677 status = nfs4_check_openmode(lock_stp, lkflg);
5678 if (status)
5679 goto out;
5681 status = nfserr_grace;
5682 if (locks_in_grace(net) && !lock->lk_reclaim)
5683 goto out;
5684 status = nfserr_no_grace;
5685 if (!locks_in_grace(net) && lock->lk_reclaim)
5686 goto out;
5688 file_lock = locks_alloc_lock();
5689 if (!file_lock) {
5690 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5691 status = nfserr_jukebox;
5692 goto out;
5695 fp = lock_stp->st_stid.sc_file;
5696 switch (lock->lk_type) {
5697 case NFS4_READ_LT:
5698 case NFS4_READW_LT:
5699 spin_lock(&fp->fi_lock);
5700 filp = find_readable_file_locked(fp);
5701 if (filp)
5702 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
5703 spin_unlock(&fp->fi_lock);
5704 file_lock->fl_type = F_RDLCK;
5705 break;
5706 case NFS4_WRITE_LT:
5707 case NFS4_WRITEW_LT:
5708 spin_lock(&fp->fi_lock);
5709 filp = find_writeable_file_locked(fp);
5710 if (filp)
5711 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
5712 spin_unlock(&fp->fi_lock);
5713 file_lock->fl_type = F_WRLCK;
5714 break;
5715 default:
5716 status = nfserr_inval;
5717 goto out;
5719 if (!filp) {
5720 status = nfserr_openmode;
5721 goto out;
5724 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
5725 file_lock->fl_pid = current->tgid;
5726 file_lock->fl_file = filp;
5727 file_lock->fl_flags = FL_POSIX;
5728 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5729 file_lock->fl_start = lock->lk_offset;
5730 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5731 nfs4_transform_lock_offset(file_lock);
5733 conflock = locks_alloc_lock();
5734 if (!conflock) {
5735 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5736 status = nfserr_jukebox;
5737 goto out;
5740 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
5741 switch (-err) {
5742 case 0: /* success! */
5743 nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
5744 status = 0;
5745 break;
5746 case (EAGAIN): /* conflock holds conflicting lock */
5747 status = nfserr_denied;
5748 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
5749 nfs4_set_lock_denied(conflock, &lock->lk_denied);
5750 break;
5751 case (EDEADLK):
5752 status = nfserr_deadlock;
5753 break;
5754 default:
5755 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
5756 status = nfserrno(err);
5757 break;
5759 out:
5760 if (filp)
5761 fput(filp);
5762 if (lock_stp) {
5763 /* Bump seqid manually if the 4.0 replay owner is openowner */
5764 if (cstate->replay_owner &&
5765 cstate->replay_owner != &lock_sop->lo_owner &&
5766 seqid_mutating_err(ntohl(status)))
5767 lock_sop->lo_owner.so_seqid++;
5769 mutex_unlock(&lock_stp->st_mutex);
5772 * If this is a new, never-before-used stateid, and we are
5773 * returning an error, then just go ahead and release it.
5775 if (status && new)
5776 release_lock_stateid(lock_stp);
5778 nfs4_put_stid(&lock_stp->st_stid);
5780 if (open_stp)
5781 nfs4_put_stid(&open_stp->st_stid);
5782 nfsd4_bump_seqid(cstate, status);
5783 if (file_lock)
5784 locks_free_lock(file_lock);
5785 if (conflock)
5786 locks_free_lock(conflock);
5787 return status;
5791 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5792 * so we do a temporary open here just to get an open file to pass to
5793 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5794 * inode operation.)
5796 static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
5798 struct file *file;
5799 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5800 if (!err) {
5801 err = nfserrno(vfs_test_lock(file, lock));
5802 fput(file);
5804 return err;
5808 * LOCKT operation
5810 __be32
5811 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5812 struct nfsd4_lockt *lockt)
5814 struct file_lock *file_lock = NULL;
5815 struct nfs4_lockowner *lo = NULL;
5816 __be32 status;
5817 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5819 if (locks_in_grace(SVC_NET(rqstp)))
5820 return nfserr_grace;
5822 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5823 return nfserr_inval;
5825 if (!nfsd4_has_session(cstate)) {
5826 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
5827 if (status)
5828 goto out;
5831 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
5832 goto out;
5834 file_lock = locks_alloc_lock();
5835 if (!file_lock) {
5836 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5837 status = nfserr_jukebox;
5838 goto out;
5841 switch (lockt->lt_type) {
5842 case NFS4_READ_LT:
5843 case NFS4_READW_LT:
5844 file_lock->fl_type = F_RDLCK;
5845 break;
5846 case NFS4_WRITE_LT:
5847 case NFS4_WRITEW_LT:
5848 file_lock->fl_type = F_WRLCK;
5849 break;
5850 default:
5851 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
5852 status = nfserr_inval;
5853 goto out;
5856 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
5857 if (lo)
5858 file_lock->fl_owner = (fl_owner_t)lo;
5859 file_lock->fl_pid = current->tgid;
5860 file_lock->fl_flags = FL_POSIX;
5862 file_lock->fl_start = lockt->lt_offset;
5863 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
5865 nfs4_transform_lock_offset(file_lock);
5867 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
5868 if (status)
5869 goto out;
5871 if (file_lock->fl_type != F_UNLCK) {
5872 status = nfserr_denied;
5873 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
5875 out:
5876 if (lo)
5877 nfs4_put_stateowner(&lo->lo_owner);
5878 if (file_lock)
5879 locks_free_lock(file_lock);
5880 return status;
5883 __be32
5884 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5885 struct nfsd4_locku *locku)
5887 struct nfs4_ol_stateid *stp;
5888 struct file *filp = NULL;
5889 struct file_lock *file_lock = NULL;
5890 __be32 status;
5891 int err;
5892 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5894 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5895 (long long) locku->lu_offset,
5896 (long long) locku->lu_length);
5898 if (check_lock_length(locku->lu_offset, locku->lu_length))
5899 return nfserr_inval;
5901 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
5902 &locku->lu_stateid, NFS4_LOCK_STID,
5903 &stp, nn);
5904 if (status)
5905 goto out;
5906 filp = find_any_file(stp->st_stid.sc_file);
5907 if (!filp) {
5908 status = nfserr_lock_range;
5909 goto put_stateid;
5911 file_lock = locks_alloc_lock();
5912 if (!file_lock) {
5913 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5914 status = nfserr_jukebox;
5915 goto fput;
5918 file_lock->fl_type = F_UNLCK;
5919 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
5920 file_lock->fl_pid = current->tgid;
5921 file_lock->fl_file = filp;
5922 file_lock->fl_flags = FL_POSIX;
5923 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5924 file_lock->fl_start = locku->lu_offset;
5926 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5927 locku->lu_length);
5928 nfs4_transform_lock_offset(file_lock);
5930 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
5931 if (err) {
5932 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
5933 goto out_nfserr;
5935 nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
5936 fput:
5937 fput(filp);
5938 put_stateid:
5939 mutex_unlock(&stp->st_mutex);
5940 nfs4_put_stid(&stp->st_stid);
5941 out:
5942 nfsd4_bump_seqid(cstate, status);
5943 if (file_lock)
5944 locks_free_lock(file_lock);
5945 return status;
5947 out_nfserr:
5948 status = nfserrno(err);
5949 goto fput;
5953 * returns
5954 * true: locks held by lockowner
5955 * false: no locks held by lockowner
5957 static bool
5958 check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
5960 struct file_lock *fl;
5961 int status = false;
5962 struct file *filp = find_any_file(fp);
5963 struct inode *inode;
5964 struct file_lock_context *flctx;
5966 if (!filp) {
5967 /* Any valid lock stateid should have some sort of access */
5968 WARN_ON_ONCE(1);
5969 return status;
5972 inode = file_inode(filp);
5973 flctx = inode->i_flctx;
5975 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
5976 spin_lock(&flctx->flc_lock);
5977 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
5978 if (fl->fl_owner == (fl_owner_t)lowner) {
5979 status = true;
5980 break;
5983 spin_unlock(&flctx->flc_lock);
5985 fput(filp);
5986 return status;
5989 __be32
5990 nfsd4_release_lockowner(struct svc_rqst *rqstp,
5991 struct nfsd4_compound_state *cstate,
5992 struct nfsd4_release_lockowner *rlockowner)
5994 clientid_t *clid = &rlockowner->rl_clientid;
5995 struct nfs4_stateowner *sop;
5996 struct nfs4_lockowner *lo = NULL;
5997 struct nfs4_ol_stateid *stp;
5998 struct xdr_netobj *owner = &rlockowner->rl_owner;
5999 unsigned int hashval = ownerstr_hashval(owner);
6000 __be32 status;
6001 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6002 struct nfs4_client *clp;
6003 LIST_HEAD (reaplist);
6005 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
6006 clid->cl_boot, clid->cl_id);
6008 status = lookup_clientid(clid, cstate, nn);
6009 if (status)
6010 return status;
6012 clp = cstate->clp;
6013 /* Find the matching lock stateowner */
6014 spin_lock(&clp->cl_lock);
6015 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
6016 so_strhash) {
6018 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
6019 continue;
6021 /* see if there are still any locks associated with it */
6022 lo = lockowner(sop);
6023 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
6024 if (check_for_locks(stp->st_stid.sc_file, lo)) {
6025 status = nfserr_locks_held;
6026 spin_unlock(&clp->cl_lock);
6027 return status;
6031 nfs4_get_stateowner(sop);
6032 break;
6034 if (!lo) {
6035 spin_unlock(&clp->cl_lock);
6036 return status;
6039 unhash_lockowner_locked(lo);
6040 while (!list_empty(&lo->lo_owner.so_stateids)) {
6041 stp = list_first_entry(&lo->lo_owner.so_stateids,
6042 struct nfs4_ol_stateid,
6043 st_perstateowner);
6044 WARN_ON(!unhash_lock_stateid(stp));
6045 put_ol_stateid_locked(stp, &reaplist);
6047 spin_unlock(&clp->cl_lock);
6048 free_ol_stateid_reaplist(&reaplist);
6049 nfs4_put_stateowner(&lo->lo_owner);
6051 return status;
6054 static inline struct nfs4_client_reclaim *
6055 alloc_reclaim(void)
6057 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
6060 bool
6061 nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
6063 struct nfs4_client_reclaim *crp;
6065 crp = nfsd4_find_reclaim_client(name, nn);
6066 return (crp && crp->cr_clp);
6070 * failure => all reset bets are off, nfserr_no_grace...
6072 struct nfs4_client_reclaim *
6073 nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
6075 unsigned int strhashval;
6076 struct nfs4_client_reclaim *crp;
6078 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
6079 crp = alloc_reclaim();
6080 if (crp) {
6081 strhashval = clientstr_hashval(name);
6082 INIT_LIST_HEAD(&crp->cr_strhash);
6083 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
6084 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
6085 crp->cr_clp = NULL;
6086 nn->reclaim_str_hashtbl_size++;
6088 return crp;
6091 void
6092 nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
6094 list_del(&crp->cr_strhash);
6095 kfree(crp);
6096 nn->reclaim_str_hashtbl_size--;
6099 void
6100 nfs4_release_reclaim(struct nfsd_net *nn)
6102 struct nfs4_client_reclaim *crp = NULL;
6103 int i;
6105 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6106 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
6107 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
6108 struct nfs4_client_reclaim, cr_strhash);
6109 nfs4_remove_reclaim_record(crp, nn);
6112 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
6116 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
6117 struct nfs4_client_reclaim *
6118 nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
6120 unsigned int strhashval;
6121 struct nfs4_client_reclaim *crp = NULL;
6123 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
6125 strhashval = clientstr_hashval(recdir);
6126 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
6127 if (same_name(crp->cr_recdir, recdir)) {
6128 return crp;
6131 return NULL;
6135 * Called from OPEN. Look for clientid in reclaim list.
6137 __be32
6138 nfs4_check_open_reclaim(clientid_t *clid,
6139 struct nfsd4_compound_state *cstate,
6140 struct nfsd_net *nn)
6142 __be32 status;
6144 /* find clientid in conf_id_hashtbl */
6145 status = lookup_clientid(clid, cstate, nn);
6146 if (status)
6147 return nfserr_reclaim_bad;
6149 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
6150 return nfserr_no_grace;
6152 if (nfsd4_client_record_check(cstate->clp))
6153 return nfserr_reclaim_bad;
6155 return nfs_ok;
6158 #ifdef CONFIG_NFSD_FAULT_INJECTION
6159 static inline void
6160 put_client(struct nfs4_client *clp)
6162 atomic_dec(&clp->cl_refcount);
6165 static struct nfs4_client *
6166 nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
6168 struct nfs4_client *clp;
6169 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6170 nfsd_net_id);
6172 if (!nfsd_netns_ready(nn))
6173 return NULL;
6175 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6176 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
6177 return clp;
6179 return NULL;
6183 nfsd_inject_print_clients(void)
6185 struct nfs4_client *clp;
6186 u64 count = 0;
6187 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6188 nfsd_net_id);
6189 char buf[INET6_ADDRSTRLEN];
6191 if (!nfsd_netns_ready(nn))
6192 return 0;
6194 spin_lock(&nn->client_lock);
6195 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6196 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
6197 pr_info("NFS Client: %s\n", buf);
6198 ++count;
6200 spin_unlock(&nn->client_lock);
6202 return count;
6206 nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
6208 u64 count = 0;
6209 struct nfs4_client *clp;
6210 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6211 nfsd_net_id);
6213 if (!nfsd_netns_ready(nn))
6214 return count;
6216 spin_lock(&nn->client_lock);
6217 clp = nfsd_find_client(addr, addr_size);
6218 if (clp) {
6219 if (mark_client_expired_locked(clp) == nfs_ok)
6220 ++count;
6221 else
6222 clp = NULL;
6224 spin_unlock(&nn->client_lock);
6226 if (clp)
6227 expire_client(clp);
6229 return count;
6233 nfsd_inject_forget_clients(u64 max)
6235 u64 count = 0;
6236 struct nfs4_client *clp, *next;
6237 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6238 nfsd_net_id);
6239 LIST_HEAD(reaplist);
6241 if (!nfsd_netns_ready(nn))
6242 return count;
6244 spin_lock(&nn->client_lock);
6245 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6246 if (mark_client_expired_locked(clp) == nfs_ok) {
6247 list_add(&clp->cl_lru, &reaplist);
6248 if (max != 0 && ++count >= max)
6249 break;
6252 spin_unlock(&nn->client_lock);
6254 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
6255 expire_client(clp);
6257 return count;
6260 static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
6261 const char *type)
6263 char buf[INET6_ADDRSTRLEN];
6264 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
6265 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
6268 static void
6269 nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
6270 struct list_head *collect)
6272 struct nfs4_client *clp = lst->st_stid.sc_client;
6273 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6274 nfsd_net_id);
6276 if (!collect)
6277 return;
6279 lockdep_assert_held(&nn->client_lock);
6280 atomic_inc(&clp->cl_refcount);
6281 list_add(&lst->st_locks, collect);
6284 static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
6285 struct list_head *collect,
6286 bool (*func)(struct nfs4_ol_stateid *))
6288 struct nfs4_openowner *oop;
6289 struct nfs4_ol_stateid *stp, *st_next;
6290 struct nfs4_ol_stateid *lst, *lst_next;
6291 u64 count = 0;
6293 spin_lock(&clp->cl_lock);
6294 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
6295 list_for_each_entry_safe(stp, st_next,
6296 &oop->oo_owner.so_stateids, st_perstateowner) {
6297 list_for_each_entry_safe(lst, lst_next,
6298 &stp->st_locks, st_locks) {
6299 if (func) {
6300 if (func(lst))
6301 nfsd_inject_add_lock_to_list(lst,
6302 collect);
6304 ++count;
6306 * Despite the fact that these functions deal
6307 * with 64-bit integers for "count", we must
6308 * ensure that it doesn't blow up the
6309 * clp->cl_refcount. Throw a warning if we
6310 * start to approach INT_MAX here.
6312 WARN_ON_ONCE(count == (INT_MAX / 2));
6313 if (count == max)
6314 goto out;
6318 out:
6319 spin_unlock(&clp->cl_lock);
6321 return count;
6324 static u64
6325 nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
6326 u64 max)
6328 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
6331 static u64
6332 nfsd_print_client_locks(struct nfs4_client *clp)
6334 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
6335 nfsd_print_count(clp, count, "locked files");
6336 return count;
6340 nfsd_inject_print_locks(void)
6342 struct nfs4_client *clp;
6343 u64 count = 0;
6344 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6345 nfsd_net_id);
6347 if (!nfsd_netns_ready(nn))
6348 return 0;
6350 spin_lock(&nn->client_lock);
6351 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6352 count += nfsd_print_client_locks(clp);
6353 spin_unlock(&nn->client_lock);
6355 return count;
6358 static void
6359 nfsd_reap_locks(struct list_head *reaplist)
6361 struct nfs4_client *clp;
6362 struct nfs4_ol_stateid *stp, *next;
6364 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
6365 list_del_init(&stp->st_locks);
6366 clp = stp->st_stid.sc_client;
6367 nfs4_put_stid(&stp->st_stid);
6368 put_client(clp);
6373 nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
6375 unsigned int count = 0;
6376 struct nfs4_client *clp;
6377 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6378 nfsd_net_id);
6379 LIST_HEAD(reaplist);
6381 if (!nfsd_netns_ready(nn))
6382 return count;
6384 spin_lock(&nn->client_lock);
6385 clp = nfsd_find_client(addr, addr_size);
6386 if (clp)
6387 count = nfsd_collect_client_locks(clp, &reaplist, 0);
6388 spin_unlock(&nn->client_lock);
6389 nfsd_reap_locks(&reaplist);
6390 return count;
6394 nfsd_inject_forget_locks(u64 max)
6396 u64 count = 0;
6397 struct nfs4_client *clp;
6398 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6399 nfsd_net_id);
6400 LIST_HEAD(reaplist);
6402 if (!nfsd_netns_ready(nn))
6403 return count;
6405 spin_lock(&nn->client_lock);
6406 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6407 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
6408 if (max != 0 && count >= max)
6409 break;
6411 spin_unlock(&nn->client_lock);
6412 nfsd_reap_locks(&reaplist);
6413 return count;
6416 static u64
6417 nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
6418 struct list_head *collect,
6419 void (*func)(struct nfs4_openowner *))
6421 struct nfs4_openowner *oop, *next;
6422 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6423 nfsd_net_id);
6424 u64 count = 0;
6426 lockdep_assert_held(&nn->client_lock);
6428 spin_lock(&clp->cl_lock);
6429 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
6430 if (func) {
6431 func(oop);
6432 if (collect) {
6433 atomic_inc(&clp->cl_refcount);
6434 list_add(&oop->oo_perclient, collect);
6437 ++count;
6439 * Despite the fact that these functions deal with
6440 * 64-bit integers for "count", we must ensure that
6441 * it doesn't blow up the clp->cl_refcount. Throw a
6442 * warning if we start to approach INT_MAX here.
6444 WARN_ON_ONCE(count == (INT_MAX / 2));
6445 if (count == max)
6446 break;
6448 spin_unlock(&clp->cl_lock);
6450 return count;
6453 static u64
6454 nfsd_print_client_openowners(struct nfs4_client *clp)
6456 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
6458 nfsd_print_count(clp, count, "openowners");
6459 return count;
6462 static u64
6463 nfsd_collect_client_openowners(struct nfs4_client *clp,
6464 struct list_head *collect, u64 max)
6466 return nfsd_foreach_client_openowner(clp, max, collect,
6467 unhash_openowner_locked);
6471 nfsd_inject_print_openowners(void)
6473 struct nfs4_client *clp;
6474 u64 count = 0;
6475 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6476 nfsd_net_id);
6478 if (!nfsd_netns_ready(nn))
6479 return 0;
6481 spin_lock(&nn->client_lock);
6482 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6483 count += nfsd_print_client_openowners(clp);
6484 spin_unlock(&nn->client_lock);
6486 return count;
6489 static void
6490 nfsd_reap_openowners(struct list_head *reaplist)
6492 struct nfs4_client *clp;
6493 struct nfs4_openowner *oop, *next;
6495 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
6496 list_del_init(&oop->oo_perclient);
6497 clp = oop->oo_owner.so_client;
6498 release_openowner(oop);
6499 put_client(clp);
6504 nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
6505 size_t addr_size)
6507 unsigned int count = 0;
6508 struct nfs4_client *clp;
6509 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6510 nfsd_net_id);
6511 LIST_HEAD(reaplist);
6513 if (!nfsd_netns_ready(nn))
6514 return count;
6516 spin_lock(&nn->client_lock);
6517 clp = nfsd_find_client(addr, addr_size);
6518 if (clp)
6519 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
6520 spin_unlock(&nn->client_lock);
6521 nfsd_reap_openowners(&reaplist);
6522 return count;
6526 nfsd_inject_forget_openowners(u64 max)
6528 u64 count = 0;
6529 struct nfs4_client *clp;
6530 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6531 nfsd_net_id);
6532 LIST_HEAD(reaplist);
6534 if (!nfsd_netns_ready(nn))
6535 return count;
6537 spin_lock(&nn->client_lock);
6538 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6539 count += nfsd_collect_client_openowners(clp, &reaplist,
6540 max - count);
6541 if (max != 0 && count >= max)
6542 break;
6544 spin_unlock(&nn->client_lock);
6545 nfsd_reap_openowners(&reaplist);
6546 return count;
6549 static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
6550 struct list_head *victims)
6552 struct nfs4_delegation *dp, *next;
6553 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6554 nfsd_net_id);
6555 u64 count = 0;
6557 lockdep_assert_held(&nn->client_lock);
6559 spin_lock(&state_lock);
6560 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
6561 if (victims) {
6563 * It's not safe to mess with delegations that have a
6564 * non-zero dl_time. They might have already been broken
6565 * and could be processed by the laundromat outside of
6566 * the state_lock. Just leave them be.
6568 if (dp->dl_time != 0)
6569 continue;
6571 atomic_inc(&clp->cl_refcount);
6572 WARN_ON(!unhash_delegation_locked(dp));
6573 list_add(&dp->dl_recall_lru, victims);
6575 ++count;
6577 * Despite the fact that these functions deal with
6578 * 64-bit integers for "count", we must ensure that
6579 * it doesn't blow up the clp->cl_refcount. Throw a
6580 * warning if we start to approach INT_MAX here.
6582 WARN_ON_ONCE(count == (INT_MAX / 2));
6583 if (count == max)
6584 break;
6586 spin_unlock(&state_lock);
6587 return count;
6590 static u64
6591 nfsd_print_client_delegations(struct nfs4_client *clp)
6593 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
6595 nfsd_print_count(clp, count, "delegations");
6596 return count;
6600 nfsd_inject_print_delegations(void)
6602 struct nfs4_client *clp;
6603 u64 count = 0;
6604 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6605 nfsd_net_id);
6607 if (!nfsd_netns_ready(nn))
6608 return 0;
6610 spin_lock(&nn->client_lock);
6611 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6612 count += nfsd_print_client_delegations(clp);
6613 spin_unlock(&nn->client_lock);
6615 return count;
6618 static void
6619 nfsd_forget_delegations(struct list_head *reaplist)
6621 struct nfs4_client *clp;
6622 struct nfs4_delegation *dp, *next;
6624 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6625 list_del_init(&dp->dl_recall_lru);
6626 clp = dp->dl_stid.sc_client;
6627 revoke_delegation(dp);
6628 put_client(clp);
6633 nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
6634 size_t addr_size)
6636 u64 count = 0;
6637 struct nfs4_client *clp;
6638 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6639 nfsd_net_id);
6640 LIST_HEAD(reaplist);
6642 if (!nfsd_netns_ready(nn))
6643 return count;
6645 spin_lock(&nn->client_lock);
6646 clp = nfsd_find_client(addr, addr_size);
6647 if (clp)
6648 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6649 spin_unlock(&nn->client_lock);
6651 nfsd_forget_delegations(&reaplist);
6652 return count;
6656 nfsd_inject_forget_delegations(u64 max)
6658 u64 count = 0;
6659 struct nfs4_client *clp;
6660 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6661 nfsd_net_id);
6662 LIST_HEAD(reaplist);
6664 if (!nfsd_netns_ready(nn))
6665 return count;
6667 spin_lock(&nn->client_lock);
6668 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6669 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6670 if (max != 0 && count >= max)
6671 break;
6673 spin_unlock(&nn->client_lock);
6674 nfsd_forget_delegations(&reaplist);
6675 return count;
6678 static void
6679 nfsd_recall_delegations(struct list_head *reaplist)
6681 struct nfs4_client *clp;
6682 struct nfs4_delegation *dp, *next;
6684 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6685 list_del_init(&dp->dl_recall_lru);
6686 clp = dp->dl_stid.sc_client;
6688 * We skipped all entries that had a zero dl_time before,
6689 * so we can now reset the dl_time back to 0. If a delegation
6690 * break comes in now, then it won't make any difference since
6691 * we're recalling it either way.
6693 spin_lock(&state_lock);
6694 dp->dl_time = 0;
6695 spin_unlock(&state_lock);
6696 nfsd_break_one_deleg(dp);
6697 put_client(clp);
6702 nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
6703 size_t addr_size)
6705 u64 count = 0;
6706 struct nfs4_client *clp;
6707 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6708 nfsd_net_id);
6709 LIST_HEAD(reaplist);
6711 if (!nfsd_netns_ready(nn))
6712 return count;
6714 spin_lock(&nn->client_lock);
6715 clp = nfsd_find_client(addr, addr_size);
6716 if (clp)
6717 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6718 spin_unlock(&nn->client_lock);
6720 nfsd_recall_delegations(&reaplist);
6721 return count;
6725 nfsd_inject_recall_delegations(u64 max)
6727 u64 count = 0;
6728 struct nfs4_client *clp, *next;
6729 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6730 nfsd_net_id);
6731 LIST_HEAD(reaplist);
6733 if (!nfsd_netns_ready(nn))
6734 return count;
6736 spin_lock(&nn->client_lock);
6737 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6738 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6739 if (max != 0 && ++count >= max)
6740 break;
6742 spin_unlock(&nn->client_lock);
6743 nfsd_recall_delegations(&reaplist);
6744 return count;
6746 #endif /* CONFIG_NFSD_FAULT_INJECTION */
6749 * Since the lifetime of a delegation isn't limited to that of an open, a
6750 * client may quite reasonably hang on to a delegation as long as it has
6751 * the inode cached. This becomes an obvious problem the first time a
6752 * client's inode cache approaches the size of the server's total memory.
6754 * For now we avoid this problem by imposing a hard limit on the number
6755 * of delegations, which varies according to the server's memory size.
6757 static void
6758 set_max_delegations(void)
6761 * Allow at most 4 delegations per megabyte of RAM. Quick
6762 * estimates suggest that in the worst case (where every delegation
6763 * is for a different inode), a delegation could take about 1.5K,
6764 * giving a worst case usage of about 6% of memory.
6766 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
6769 static int nfs4_state_create_net(struct net *net)
6771 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6772 int i;
6774 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6775 CLIENT_HASH_SIZE, GFP_KERNEL);
6776 if (!nn->conf_id_hashtbl)
6777 goto err;
6778 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6779 CLIENT_HASH_SIZE, GFP_KERNEL);
6780 if (!nn->unconf_id_hashtbl)
6781 goto err_unconf_id;
6782 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
6783 SESSION_HASH_SIZE, GFP_KERNEL);
6784 if (!nn->sessionid_hashtbl)
6785 goto err_sessionid;
6787 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6788 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
6789 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
6791 for (i = 0; i < SESSION_HASH_SIZE; i++)
6792 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
6793 nn->conf_name_tree = RB_ROOT;
6794 nn->unconf_name_tree = RB_ROOT;
6795 nn->boot_time = get_seconds();
6796 nn->grace_ended = false;
6797 nn->nfsd4_manager.block_opens = true;
6798 INIT_LIST_HEAD(&nn->nfsd4_manager.list);
6799 INIT_LIST_HEAD(&nn->client_lru);
6800 INIT_LIST_HEAD(&nn->close_lru);
6801 INIT_LIST_HEAD(&nn->del_recall_lru);
6802 spin_lock_init(&nn->client_lock);
6804 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
6805 get_net(net);
6807 return 0;
6809 err_sessionid:
6810 kfree(nn->unconf_id_hashtbl);
6811 err_unconf_id:
6812 kfree(nn->conf_id_hashtbl);
6813 err:
6814 return -ENOMEM;
6817 static void
6818 nfs4_state_destroy_net(struct net *net)
6820 int i;
6821 struct nfs4_client *clp = NULL;
6822 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6824 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6825 while (!list_empty(&nn->conf_id_hashtbl[i])) {
6826 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6827 destroy_client(clp);
6831 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6832 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
6833 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6834 destroy_client(clp);
6838 kfree(nn->sessionid_hashtbl);
6839 kfree(nn->unconf_id_hashtbl);
6840 kfree(nn->conf_id_hashtbl);
6841 put_net(net);
6845 nfs4_state_start_net(struct net *net)
6847 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6848 int ret;
6850 ret = nfs4_state_create_net(net);
6851 if (ret)
6852 return ret;
6853 locks_start_grace(net, &nn->nfsd4_manager);
6854 nfsd4_client_tracking_init(net);
6855 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
6856 nn->nfsd4_grace, net);
6857 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
6858 return 0;
6861 /* initialization to perform when the nfsd service is started: */
6864 nfs4_state_start(void)
6866 int ret;
6868 ret = set_callback_cred();
6869 if (ret)
6870 return ret;
6872 laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
6873 if (laundry_wq == NULL) {
6874 ret = -ENOMEM;
6875 goto out_cleanup_cred;
6877 ret = nfsd4_create_callback_queue();
6878 if (ret)
6879 goto out_free_laundry;
6881 set_max_delegations();
6882 return 0;
6884 out_free_laundry:
6885 destroy_workqueue(laundry_wq);
6886 out_cleanup_cred:
6887 cleanup_callback_cred();
6888 return ret;
6891 void
6892 nfs4_state_shutdown_net(struct net *net)
6894 struct nfs4_delegation *dp = NULL;
6895 struct list_head *pos, *next, reaplist;
6896 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6898 cancel_delayed_work_sync(&nn->laundromat_work);
6899 locks_end_grace(&nn->nfsd4_manager);
6901 INIT_LIST_HEAD(&reaplist);
6902 spin_lock(&state_lock);
6903 list_for_each_safe(pos, next, &nn->del_recall_lru) {
6904 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
6905 WARN_ON(!unhash_delegation_locked(dp));
6906 list_add(&dp->dl_recall_lru, &reaplist);
6908 spin_unlock(&state_lock);
6909 list_for_each_safe(pos, next, &reaplist) {
6910 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
6911 list_del_init(&dp->dl_recall_lru);
6912 put_clnt_odstate(dp->dl_clnt_odstate);
6913 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
6914 nfs4_put_stid(&dp->dl_stid);
6917 nfsd4_client_tracking_exit(net);
6918 nfs4_state_destroy_net(net);
6921 void
6922 nfs4_state_shutdown(void)
6924 destroy_workqueue(laundry_wq);
6925 nfsd4_destroy_callback_queue();
6926 cleanup_callback_cred();
6929 static void
6930 get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6932 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
6933 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
6936 static void
6937 put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6939 if (cstate->minorversion) {
6940 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
6941 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6945 void
6946 clear_current_stateid(struct nfsd4_compound_state *cstate)
6948 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6952 * functions to set current state id
6954 void
6955 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6957 put_stateid(cstate, &odp->od_stateid);
6960 void
6961 nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
6963 put_stateid(cstate, &open->op_stateid);
6966 void
6967 nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6969 put_stateid(cstate, &close->cl_stateid);
6972 void
6973 nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
6975 put_stateid(cstate, &lock->lk_resp_stateid);
6979 * functions to consume current state id
6982 void
6983 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6985 get_stateid(cstate, &odp->od_stateid);
6988 void
6989 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
6991 get_stateid(cstate, &drp->dr_stateid);
6994 void
6995 nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
6997 get_stateid(cstate, &fsp->fr_stateid);
7000 void
7001 nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
7003 get_stateid(cstate, &setattr->sa_stateid);
7006 void
7007 nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
7009 get_stateid(cstate, &close->cl_stateid);
7012 void
7013 nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
7015 get_stateid(cstate, &locku->lu_stateid);
7018 void
7019 nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
7021 get_stateid(cstate, &read->rd_stateid);
7024 void
7025 nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
7027 get_stateid(cstate, &write->wr_stateid);