2 * Copyright (c) 2001 The Regents of the University of Michigan.
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
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>
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>
48 #include "current_stateid.h"
53 #define NFSDDBG_FACILITY NFSDDBG_PROC
55 #define all_ones {{~0,~0},~0}
56 static const stateid_t one_stateid = {
58 .si_opaque = all_ones,
60 static const stateid_t zero_stateid = {
63 static const stateid_t currentstateid = {
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), ¤tstateid, 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);
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);
90 enum nfsd4_st_mutex_lock_subclass {
91 OPEN_STATEID_MUTEX = 0,
92 LOCK_STATEID_MUTEX = 1,
96 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
97 * the refcount on the open stateid to drop.
99 static DECLARE_WAIT_QUEUE_HEAD(close_wq);
101 static struct kmem_cache *openowner_slab;
102 static struct kmem_cache *lockowner_slab;
103 static struct kmem_cache *file_slab;
104 static struct kmem_cache *stateid_slab;
105 static struct kmem_cache *deleg_slab;
106 static struct kmem_cache *odstate_slab;
108 static void free_session(struct nfsd4_session *);
110 static const struct nfsd4_callback_ops nfsd4_cb_recall_ops;
111 static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops;
113 static bool is_session_dead(struct nfsd4_session *ses)
115 return ses->se_flags & NFS4_SESSION_DEAD;
118 static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
120 if (atomic_read(&ses->se_ref) > ref_held_by_me)
121 return nfserr_jukebox;
122 ses->se_flags |= NFS4_SESSION_DEAD;
126 static bool is_client_expired(struct nfs4_client *clp)
128 return clp->cl_time == 0;
131 static __be32 get_client_locked(struct nfs4_client *clp)
133 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
135 lockdep_assert_held(&nn->client_lock);
137 if (is_client_expired(clp))
138 return nfserr_expired;
139 atomic_inc(&clp->cl_refcount);
143 /* must be called under the client_lock */
145 renew_client_locked(struct nfs4_client *clp)
147 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
149 if (is_client_expired(clp)) {
151 printk("%s: client (clientid %08x/%08x) already expired\n",
153 clp->cl_clientid.cl_boot,
154 clp->cl_clientid.cl_id);
158 dprintk("renewing client (clientid %08x/%08x)\n",
159 clp->cl_clientid.cl_boot,
160 clp->cl_clientid.cl_id);
161 list_move_tail(&clp->cl_lru, &nn->client_lru);
162 clp->cl_time = get_seconds();
165 static void put_client_renew_locked(struct nfs4_client *clp)
167 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
169 lockdep_assert_held(&nn->client_lock);
171 if (!atomic_dec_and_test(&clp->cl_refcount))
173 if (!is_client_expired(clp))
174 renew_client_locked(clp);
177 static void put_client_renew(struct nfs4_client *clp)
179 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
181 if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
183 if (!is_client_expired(clp))
184 renew_client_locked(clp);
185 spin_unlock(&nn->client_lock);
188 static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
192 if (is_session_dead(ses))
193 return nfserr_badsession;
194 status = get_client_locked(ses->se_client);
197 atomic_inc(&ses->se_ref);
201 static void nfsd4_put_session_locked(struct nfsd4_session *ses)
203 struct nfs4_client *clp = ses->se_client;
204 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
206 lockdep_assert_held(&nn->client_lock);
208 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
210 put_client_renew_locked(clp);
213 static void nfsd4_put_session(struct nfsd4_session *ses)
215 struct nfs4_client *clp = ses->se_client;
216 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
218 spin_lock(&nn->client_lock);
219 nfsd4_put_session_locked(ses);
220 spin_unlock(&nn->client_lock);
223 static struct nfsd4_blocked_lock *
224 find_blocked_lock(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
227 struct nfsd4_blocked_lock *cur, *found = NULL;
229 spin_lock(&nn->blocked_locks_lock);
230 list_for_each_entry(cur, &lo->lo_blocked, nbl_list) {
231 if (fh_match(fh, &cur->nbl_fh)) {
232 list_del_init(&cur->nbl_list);
233 list_del_init(&cur->nbl_lru);
238 spin_unlock(&nn->blocked_locks_lock);
240 posix_unblock_lock(&found->nbl_lock);
244 static struct nfsd4_blocked_lock *
245 find_or_allocate_block(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
248 struct nfsd4_blocked_lock *nbl;
250 nbl = find_blocked_lock(lo, fh, nn);
252 nbl= kmalloc(sizeof(*nbl), GFP_KERNEL);
254 fh_copy_shallow(&nbl->nbl_fh, fh);
255 locks_init_lock(&nbl->nbl_lock);
256 nfsd4_init_cb(&nbl->nbl_cb, lo->lo_owner.so_client,
257 &nfsd4_cb_notify_lock_ops,
258 NFSPROC4_CLNT_CB_NOTIFY_LOCK);
265 free_blocked_lock(struct nfsd4_blocked_lock *nbl)
267 locks_release_private(&nbl->nbl_lock);
272 nfsd4_cb_notify_lock_done(struct nfsd4_callback *cb, struct rpc_task *task)
275 * Since this is just an optimization, we don't try very hard if it
276 * turns out not to succeed. We'll requeue it on NFS4ERR_DELAY, and
277 * just quit trying on anything else.
279 switch (task->tk_status) {
281 rpc_delay(task, 1 * HZ);
289 nfsd4_cb_notify_lock_release(struct nfsd4_callback *cb)
291 struct nfsd4_blocked_lock *nbl = container_of(cb,
292 struct nfsd4_blocked_lock, nbl_cb);
294 free_blocked_lock(nbl);
297 static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops = {
298 .done = nfsd4_cb_notify_lock_done,
299 .release = nfsd4_cb_notify_lock_release,
302 static inline struct nfs4_stateowner *
303 nfs4_get_stateowner(struct nfs4_stateowner *sop)
305 atomic_inc(&sop->so_count);
310 same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
312 return (sop->so_owner.len == owner->len) &&
313 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
316 static struct nfs4_openowner *
317 find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
318 struct nfs4_client *clp)
320 struct nfs4_stateowner *so;
322 lockdep_assert_held(&clp->cl_lock);
324 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
326 if (!so->so_is_open_owner)
328 if (same_owner_str(so, &open->op_owner))
329 return openowner(nfs4_get_stateowner(so));
334 static struct nfs4_openowner *
335 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
336 struct nfs4_client *clp)
338 struct nfs4_openowner *oo;
340 spin_lock(&clp->cl_lock);
341 oo = find_openstateowner_str_locked(hashval, open, clp);
342 spin_unlock(&clp->cl_lock);
347 opaque_hashval(const void *ptr, int nbytes)
349 unsigned char *cptr = (unsigned char *) ptr;
359 static void nfsd4_free_file_rcu(struct rcu_head *rcu)
361 struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
363 kmem_cache_free(file_slab, fp);
367 put_nfs4_file(struct nfs4_file *fi)
369 might_lock(&state_lock);
371 if (refcount_dec_and_lock(&fi->fi_ref, &state_lock)) {
372 hlist_del_rcu(&fi->fi_hash);
373 spin_unlock(&state_lock);
374 WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
375 WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
376 call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
381 __nfs4_get_fd(struct nfs4_file *f, int oflag)
383 if (f->fi_fds[oflag])
384 return get_file(f->fi_fds[oflag]);
389 find_writeable_file_locked(struct nfs4_file *f)
393 lockdep_assert_held(&f->fi_lock);
395 ret = __nfs4_get_fd(f, O_WRONLY);
397 ret = __nfs4_get_fd(f, O_RDWR);
402 find_writeable_file(struct nfs4_file *f)
406 spin_lock(&f->fi_lock);
407 ret = find_writeable_file_locked(f);
408 spin_unlock(&f->fi_lock);
413 static struct file *find_readable_file_locked(struct nfs4_file *f)
417 lockdep_assert_held(&f->fi_lock);
419 ret = __nfs4_get_fd(f, O_RDONLY);
421 ret = __nfs4_get_fd(f, O_RDWR);
426 find_readable_file(struct nfs4_file *f)
430 spin_lock(&f->fi_lock);
431 ret = find_readable_file_locked(f);
432 spin_unlock(&f->fi_lock);
438 find_any_file(struct nfs4_file *f)
442 spin_lock(&f->fi_lock);
443 ret = __nfs4_get_fd(f, O_RDWR);
445 ret = __nfs4_get_fd(f, O_WRONLY);
447 ret = __nfs4_get_fd(f, O_RDONLY);
449 spin_unlock(&f->fi_lock);
453 static atomic_long_t num_delegations;
454 unsigned long max_delegations;
457 * Open owner state (share locks)
460 /* hash tables for lock and open owners */
461 #define OWNER_HASH_BITS 8
462 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
463 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
465 static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
469 ret = opaque_hashval(ownername->data, ownername->len);
470 return ret & OWNER_HASH_MASK;
473 /* hash table for nfs4_file */
474 #define FILE_HASH_BITS 8
475 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
477 static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
479 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
482 static unsigned int file_hashval(struct knfsd_fh *fh)
484 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
487 static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
490 __nfs4_file_get_access(struct nfs4_file *fp, u32 access)
492 lockdep_assert_held(&fp->fi_lock);
494 if (access & NFS4_SHARE_ACCESS_WRITE)
495 atomic_inc(&fp->fi_access[O_WRONLY]);
496 if (access & NFS4_SHARE_ACCESS_READ)
497 atomic_inc(&fp->fi_access[O_RDONLY]);
501 nfs4_file_get_access(struct nfs4_file *fp, u32 access)
503 lockdep_assert_held(&fp->fi_lock);
505 /* Does this access mode make sense? */
506 if (access & ~NFS4_SHARE_ACCESS_BOTH)
509 /* Does it conflict with a deny mode already set? */
510 if ((access & fp->fi_share_deny) != 0)
511 return nfserr_share_denied;
513 __nfs4_file_get_access(fp, access);
517 static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
519 /* Common case is that there is no deny mode. */
521 /* Does this deny mode make sense? */
522 if (deny & ~NFS4_SHARE_DENY_BOTH)
525 if ((deny & NFS4_SHARE_DENY_READ) &&
526 atomic_read(&fp->fi_access[O_RDONLY]))
527 return nfserr_share_denied;
529 if ((deny & NFS4_SHARE_DENY_WRITE) &&
530 atomic_read(&fp->fi_access[O_WRONLY]))
531 return nfserr_share_denied;
536 static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
538 might_lock(&fp->fi_lock);
540 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
541 struct file *f1 = NULL;
542 struct file *f2 = NULL;
544 swap(f1, fp->fi_fds[oflag]);
545 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
546 swap(f2, fp->fi_fds[O_RDWR]);
547 spin_unlock(&fp->fi_lock);
555 static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
557 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
559 if (access & NFS4_SHARE_ACCESS_WRITE)
560 __nfs4_file_put_access(fp, O_WRONLY);
561 if (access & NFS4_SHARE_ACCESS_READ)
562 __nfs4_file_put_access(fp, O_RDONLY);
566 * Allocate a new open/delegation state counter. This is needed for
567 * pNFS for proper return on close semantics.
569 * Note that we only allocate it for pNFS-enabled exports, otherwise
570 * all pointers to struct nfs4_clnt_odstate are always NULL.
572 static struct nfs4_clnt_odstate *
573 alloc_clnt_odstate(struct nfs4_client *clp)
575 struct nfs4_clnt_odstate *co;
577 co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
580 refcount_set(&co->co_odcount, 1);
586 hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
588 struct nfs4_file *fp = co->co_file;
590 lockdep_assert_held(&fp->fi_lock);
591 list_add(&co->co_perfile, &fp->fi_clnt_odstate);
595 get_clnt_odstate(struct nfs4_clnt_odstate *co)
598 refcount_inc(&co->co_odcount);
602 put_clnt_odstate(struct nfs4_clnt_odstate *co)
604 struct nfs4_file *fp;
610 if (refcount_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
611 list_del(&co->co_perfile);
612 spin_unlock(&fp->fi_lock);
614 nfsd4_return_all_file_layouts(co->co_client, fp);
615 kmem_cache_free(odstate_slab, co);
619 static struct nfs4_clnt_odstate *
620 find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
622 struct nfs4_clnt_odstate *co;
623 struct nfs4_client *cl;
630 spin_lock(&fp->fi_lock);
631 list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
632 if (co->co_client == cl) {
633 get_clnt_odstate(co);
639 hash_clnt_odstate_locked(new);
641 spin_unlock(&fp->fi_lock);
645 struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab,
646 void (*sc_free)(struct nfs4_stid *))
648 struct nfs4_stid *stid;
651 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
655 idr_preload(GFP_KERNEL);
656 spin_lock(&cl->cl_lock);
657 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 0, 0, GFP_NOWAIT);
658 spin_unlock(&cl->cl_lock);
663 stid->sc_free = sc_free;
664 stid->sc_client = cl;
665 stid->sc_stateid.si_opaque.so_id = new_id;
666 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
667 /* Will be incremented before return to client: */
668 refcount_set(&stid->sc_count, 1);
669 spin_lock_init(&stid->sc_lock);
672 * It shouldn't be a problem to reuse an opaque stateid value.
673 * I don't think it is for 4.1. But with 4.0 I worry that, for
674 * example, a stray write retransmission could be accepted by
675 * the server when it should have been rejected. Therefore,
676 * adopt a trick from the sctp code to attempt to maximize the
677 * amount of time until an id is reused, by ensuring they always
678 * "increase" (mod INT_MAX):
682 kmem_cache_free(slab, stid);
686 static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
688 struct nfs4_stid *stid;
690 stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid);
694 return openlockstateid(stid);
697 static void nfs4_free_deleg(struct nfs4_stid *stid)
699 kmem_cache_free(deleg_slab, stid);
700 atomic_long_dec(&num_delegations);
704 * When we recall a delegation, we should be careful not to hand it
705 * out again straight away.
706 * To ensure this we keep a pair of bloom filters ('new' and 'old')
707 * in which the filehandles of recalled delegations are "stored".
708 * If a filehandle appear in either filter, a delegation is blocked.
709 * When a delegation is recalled, the filehandle is stored in the "new"
711 * Every 30 seconds we swap the filters and clear the "new" one,
712 * unless both are empty of course.
714 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
715 * low 3 bytes as hash-table indices.
717 * 'blocked_delegations_lock', which is always taken in block_delegations(),
718 * is used to manage concurrent access. Testing does not need the lock
719 * except when swapping the two filters.
721 static DEFINE_SPINLOCK(blocked_delegations_lock);
722 static struct bloom_pair {
723 int entries, old_entries;
725 int new; /* index into 'set' */
726 DECLARE_BITMAP(set[2], 256);
727 } blocked_delegations;
729 static int delegation_blocked(struct knfsd_fh *fh)
732 struct bloom_pair *bd = &blocked_delegations;
734 if (bd->entries == 0)
736 if (seconds_since_boot() - bd->swap_time > 30) {
737 spin_lock(&blocked_delegations_lock);
738 if (seconds_since_boot() - bd->swap_time > 30) {
739 bd->entries -= bd->old_entries;
740 bd->old_entries = bd->entries;
741 memset(bd->set[bd->new], 0,
744 bd->swap_time = seconds_since_boot();
746 spin_unlock(&blocked_delegations_lock);
748 hash = jhash(&fh->fh_base, fh->fh_size, 0);
749 if (test_bit(hash&255, bd->set[0]) &&
750 test_bit((hash>>8)&255, bd->set[0]) &&
751 test_bit((hash>>16)&255, bd->set[0]))
754 if (test_bit(hash&255, bd->set[1]) &&
755 test_bit((hash>>8)&255, bd->set[1]) &&
756 test_bit((hash>>16)&255, bd->set[1]))
762 static void block_delegations(struct knfsd_fh *fh)
765 struct bloom_pair *bd = &blocked_delegations;
767 hash = jhash(&fh->fh_base, fh->fh_size, 0);
769 spin_lock(&blocked_delegations_lock);
770 __set_bit(hash&255, bd->set[bd->new]);
771 __set_bit((hash>>8)&255, bd->set[bd->new]);
772 __set_bit((hash>>16)&255, bd->set[bd->new]);
773 if (bd->entries == 0)
774 bd->swap_time = seconds_since_boot();
776 spin_unlock(&blocked_delegations_lock);
779 static struct nfs4_delegation *
780 alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh,
781 struct nfs4_clnt_odstate *odstate)
783 struct nfs4_delegation *dp;
786 dprintk("NFSD alloc_init_deleg\n");
787 n = atomic_long_inc_return(&num_delegations);
788 if (n < 0 || n > max_delegations)
790 if (delegation_blocked(¤t_fh->fh_handle))
792 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg));
797 * delegation seqid's are never incremented. The 4.1 special
798 * meaning of seqid 0 isn't meaningful, really, but let's avoid
799 * 0 anyway just for consistency and use 1:
801 dp->dl_stid.sc_stateid.si_generation = 1;
802 INIT_LIST_HEAD(&dp->dl_perfile);
803 INIT_LIST_HEAD(&dp->dl_perclnt);
804 INIT_LIST_HEAD(&dp->dl_recall_lru);
805 dp->dl_clnt_odstate = odstate;
806 get_clnt_odstate(odstate);
807 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
809 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
810 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
813 atomic_long_dec(&num_delegations);
818 nfs4_put_stid(struct nfs4_stid *s)
820 struct nfs4_file *fp = s->sc_file;
821 struct nfs4_client *clp = s->sc_client;
823 might_lock(&clp->cl_lock);
825 if (!refcount_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
826 wake_up_all(&close_wq);
829 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
830 spin_unlock(&clp->cl_lock);
837 nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
839 stateid_t *src = &stid->sc_stateid;
841 spin_lock(&stid->sc_lock);
842 if (unlikely(++src->si_generation == 0))
843 src->si_generation = 1;
844 memcpy(dst, src, sizeof(*dst));
845 spin_unlock(&stid->sc_lock);
848 static void nfs4_put_deleg_lease(struct nfs4_file *fp)
850 struct file *filp = NULL;
852 spin_lock(&fp->fi_lock);
853 if (fp->fi_deleg_file && --fp->fi_delegees == 0)
854 swap(filp, fp->fi_deleg_file);
855 spin_unlock(&fp->fi_lock);
858 vfs_setlease(filp, F_UNLCK, NULL, (void **)&fp);
863 void nfs4_unhash_stid(struct nfs4_stid *s)
869 * nfs4_get_existing_delegation - Discover if this delegation already exists
870 * @clp: a pointer to the nfs4_client we're granting a delegation to
871 * @fp: a pointer to the nfs4_file we're granting a delegation on
874 * On success: NULL if an existing delegation was not found.
876 * On error: -EAGAIN if one was previously granted to this nfs4_client
877 * for this nfs4_file.
882 nfs4_get_existing_delegation(struct nfs4_client *clp, struct nfs4_file *fp)
884 struct nfs4_delegation *searchdp = NULL;
885 struct nfs4_client *searchclp = NULL;
887 lockdep_assert_held(&state_lock);
888 lockdep_assert_held(&fp->fi_lock);
890 list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
891 searchclp = searchdp->dl_stid.sc_client;
892 if (clp == searchclp) {
900 * hash_delegation_locked - Add a delegation to the appropriate lists
901 * @dp: a pointer to the nfs4_delegation we are adding.
902 * @fp: a pointer to the nfs4_file we're granting a delegation on
905 * On success: NULL if the delegation was successfully hashed.
907 * On error: -EAGAIN if one was previously granted to this
908 * nfs4_client for this nfs4_file. Delegation is not hashed.
913 hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
916 struct nfs4_client *clp = dp->dl_stid.sc_client;
918 lockdep_assert_held(&state_lock);
919 lockdep_assert_held(&fp->fi_lock);
921 status = nfs4_get_existing_delegation(clp, fp);
925 refcount_inc(&dp->dl_stid.sc_count);
926 dp->dl_stid.sc_type = NFS4_DELEG_STID;
927 list_add(&dp->dl_perfile, &fp->fi_delegations);
928 list_add(&dp->dl_perclnt, &clp->cl_delegations);
933 unhash_delegation_locked(struct nfs4_delegation *dp)
935 struct nfs4_file *fp = dp->dl_stid.sc_file;
937 lockdep_assert_held(&state_lock);
939 if (list_empty(&dp->dl_perfile))
942 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
943 /* Ensure that deleg break won't try to requeue it */
945 spin_lock(&fp->fi_lock);
946 list_del_init(&dp->dl_perclnt);
947 list_del_init(&dp->dl_recall_lru);
948 list_del_init(&dp->dl_perfile);
949 spin_unlock(&fp->fi_lock);
953 static void destroy_delegation(struct nfs4_delegation *dp)
957 spin_lock(&state_lock);
958 unhashed = unhash_delegation_locked(dp);
959 spin_unlock(&state_lock);
961 put_clnt_odstate(dp->dl_clnt_odstate);
962 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
963 nfs4_put_stid(&dp->dl_stid);
967 static void revoke_delegation(struct nfs4_delegation *dp)
969 struct nfs4_client *clp = dp->dl_stid.sc_client;
971 WARN_ON(!list_empty(&dp->dl_recall_lru));
973 put_clnt_odstate(dp->dl_clnt_odstate);
974 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
976 if (clp->cl_minorversion == 0)
977 nfs4_put_stid(&dp->dl_stid);
979 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
980 spin_lock(&clp->cl_lock);
981 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
982 spin_unlock(&clp->cl_lock);
990 static unsigned int clientid_hashval(u32 id)
992 return id & CLIENT_HASH_MASK;
995 static unsigned int clientstr_hashval(const char *name)
997 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
1001 * We store the NONE, READ, WRITE, and BOTH bits separately in the
1002 * st_{access,deny}_bmap field of the stateid, in order to track not
1003 * only what share bits are currently in force, but also what
1004 * combinations of share bits previous opens have used. This allows us
1005 * to enforce the recommendation of rfc 3530 14.2.19 that the server
1006 * return an error if the client attempt to downgrade to a combination
1007 * of share bits not explicable by closing some of its previous opens.
1009 * XXX: This enforcement is actually incomplete, since we don't keep
1010 * track of access/deny bit combinations; so, e.g., we allow:
1012 * OPEN allow read, deny write
1013 * OPEN allow both, deny none
1014 * DOWNGRADE allow read, deny none
1016 * which we should reject.
1019 bmap_to_share_mode(unsigned long bmap) {
1021 unsigned int access = 0;
1023 for (i = 1; i < 4; i++) {
1024 if (test_bit(i, &bmap))
1030 /* set share access for a given stateid */
1032 set_access(u32 access, struct nfs4_ol_stateid *stp)
1034 unsigned char mask = 1 << access;
1036 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1037 stp->st_access_bmap |= mask;
1040 /* clear share access for a given stateid */
1042 clear_access(u32 access, struct nfs4_ol_stateid *stp)
1044 unsigned char mask = 1 << access;
1046 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1047 stp->st_access_bmap &= ~mask;
1050 /* test whether a given stateid has access */
1052 test_access(u32 access, struct nfs4_ol_stateid *stp)
1054 unsigned char mask = 1 << access;
1056 return (bool)(stp->st_access_bmap & mask);
1059 /* set share deny for a given stateid */
1061 set_deny(u32 deny, struct nfs4_ol_stateid *stp)
1063 unsigned char mask = 1 << deny;
1065 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1066 stp->st_deny_bmap |= mask;
1069 /* clear share deny for a given stateid */
1071 clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
1073 unsigned char mask = 1 << deny;
1075 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1076 stp->st_deny_bmap &= ~mask;
1079 /* test whether a given stateid is denying specific access */
1081 test_deny(u32 deny, struct nfs4_ol_stateid *stp)
1083 unsigned char mask = 1 << deny;
1085 return (bool)(stp->st_deny_bmap & mask);
1088 static int nfs4_access_to_omode(u32 access)
1090 switch (access & NFS4_SHARE_ACCESS_BOTH) {
1091 case NFS4_SHARE_ACCESS_READ:
1093 case NFS4_SHARE_ACCESS_WRITE:
1095 case NFS4_SHARE_ACCESS_BOTH:
1103 * A stateid that had a deny mode associated with it is being released
1104 * or downgraded. Recalculate the deny mode on the file.
1107 recalculate_deny_mode(struct nfs4_file *fp)
1109 struct nfs4_ol_stateid *stp;
1111 spin_lock(&fp->fi_lock);
1112 fp->fi_share_deny = 0;
1113 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
1114 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
1115 spin_unlock(&fp->fi_lock);
1119 reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
1122 bool change = false;
1124 for (i = 1; i < 4; i++) {
1125 if ((i & deny) != i) {
1131 /* Recalculate per-file deny mode if there was a change */
1133 recalculate_deny_mode(stp->st_stid.sc_file);
1136 /* release all access and file references for a given stateid */
1138 release_all_access(struct nfs4_ol_stateid *stp)
1141 struct nfs4_file *fp = stp->st_stid.sc_file;
1143 if (fp && stp->st_deny_bmap != 0)
1144 recalculate_deny_mode(fp);
1146 for (i = 1; i < 4; i++) {
1147 if (test_access(i, stp))
1148 nfs4_file_put_access(stp->st_stid.sc_file, i);
1149 clear_access(i, stp);
1153 static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1155 kfree(sop->so_owner.data);
1156 sop->so_ops->so_free(sop);
1159 static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1161 struct nfs4_client *clp = sop->so_client;
1163 might_lock(&clp->cl_lock);
1165 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
1167 sop->so_ops->so_unhash(sop);
1168 spin_unlock(&clp->cl_lock);
1169 nfs4_free_stateowner(sop);
1172 static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
1174 struct nfs4_file *fp = stp->st_stid.sc_file;
1176 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1178 if (list_empty(&stp->st_perfile))
1181 spin_lock(&fp->fi_lock);
1182 list_del_init(&stp->st_perfile);
1183 spin_unlock(&fp->fi_lock);
1184 list_del(&stp->st_perstateowner);
1188 static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
1190 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1192 put_clnt_odstate(stp->st_clnt_odstate);
1193 release_all_access(stp);
1194 if (stp->st_stateowner)
1195 nfs4_put_stateowner(stp->st_stateowner);
1196 kmem_cache_free(stateid_slab, stid);
1199 static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
1201 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1202 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
1205 file = find_any_file(stp->st_stid.sc_file);
1207 filp_close(file, (fl_owner_t)lo);
1208 nfs4_free_ol_stateid(stid);
1212 * Put the persistent reference to an already unhashed generic stateid, while
1213 * holding the cl_lock. If it's the last reference, then put it onto the
1214 * reaplist for later destruction.
1216 static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1217 struct list_head *reaplist)
1219 struct nfs4_stid *s = &stp->st_stid;
1220 struct nfs4_client *clp = s->sc_client;
1222 lockdep_assert_held(&clp->cl_lock);
1224 WARN_ON_ONCE(!list_empty(&stp->st_locks));
1226 if (!refcount_dec_and_test(&s->sc_count)) {
1227 wake_up_all(&close_wq);
1231 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1232 list_add(&stp->st_locks, reaplist);
1235 static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
1237 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1239 list_del_init(&stp->st_locks);
1240 nfs4_unhash_stid(&stp->st_stid);
1241 return unhash_ol_stateid(stp);
1244 static void release_lock_stateid(struct nfs4_ol_stateid *stp)
1246 struct nfs4_client *clp = stp->st_stid.sc_client;
1249 spin_lock(&clp->cl_lock);
1250 unhashed = unhash_lock_stateid(stp);
1251 spin_unlock(&clp->cl_lock);
1253 nfs4_put_stid(&stp->st_stid);
1256 static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
1258 struct nfs4_client *clp = lo->lo_owner.so_client;
1260 lockdep_assert_held(&clp->cl_lock);
1262 list_del_init(&lo->lo_owner.so_strhash);
1266 * Free a list of generic stateids that were collected earlier after being
1270 free_ol_stateid_reaplist(struct list_head *reaplist)
1272 struct nfs4_ol_stateid *stp;
1273 struct nfs4_file *fp;
1277 while (!list_empty(reaplist)) {
1278 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1280 list_del(&stp->st_locks);
1281 fp = stp->st_stid.sc_file;
1282 stp->st_stid.sc_free(&stp->st_stid);
1288 static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1289 struct list_head *reaplist)
1291 struct nfs4_ol_stateid *stp;
1293 lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1295 while (!list_empty(&open_stp->st_locks)) {
1296 stp = list_entry(open_stp->st_locks.next,
1297 struct nfs4_ol_stateid, st_locks);
1298 WARN_ON(!unhash_lock_stateid(stp));
1299 put_ol_stateid_locked(stp, reaplist);
1303 static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
1304 struct list_head *reaplist)
1308 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1310 unhashed = unhash_ol_stateid(stp);
1311 release_open_stateid_locks(stp, reaplist);
1315 static void release_open_stateid(struct nfs4_ol_stateid *stp)
1317 LIST_HEAD(reaplist);
1319 spin_lock(&stp->st_stid.sc_client->cl_lock);
1320 if (unhash_open_stateid(stp, &reaplist))
1321 put_ol_stateid_locked(stp, &reaplist);
1322 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1323 free_ol_stateid_reaplist(&reaplist);
1326 static void unhash_openowner_locked(struct nfs4_openowner *oo)
1328 struct nfs4_client *clp = oo->oo_owner.so_client;
1330 lockdep_assert_held(&clp->cl_lock);
1332 list_del_init(&oo->oo_owner.so_strhash);
1333 list_del_init(&oo->oo_perclient);
1336 static void release_last_closed_stateid(struct nfs4_openowner *oo)
1338 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1340 struct nfs4_ol_stateid *s;
1342 spin_lock(&nn->client_lock);
1343 s = oo->oo_last_closed_stid;
1345 list_del_init(&oo->oo_close_lru);
1346 oo->oo_last_closed_stid = NULL;
1348 spin_unlock(&nn->client_lock);
1350 nfs4_put_stid(&s->st_stid);
1353 static void release_openowner(struct nfs4_openowner *oo)
1355 struct nfs4_ol_stateid *stp;
1356 struct nfs4_client *clp = oo->oo_owner.so_client;
1357 struct list_head reaplist;
1359 INIT_LIST_HEAD(&reaplist);
1361 spin_lock(&clp->cl_lock);
1362 unhash_openowner_locked(oo);
1363 while (!list_empty(&oo->oo_owner.so_stateids)) {
1364 stp = list_first_entry(&oo->oo_owner.so_stateids,
1365 struct nfs4_ol_stateid, st_perstateowner);
1366 if (unhash_open_stateid(stp, &reaplist))
1367 put_ol_stateid_locked(stp, &reaplist);
1369 spin_unlock(&clp->cl_lock);
1370 free_ol_stateid_reaplist(&reaplist);
1371 release_last_closed_stateid(oo);
1372 nfs4_put_stateowner(&oo->oo_owner);
1376 hash_sessionid(struct nfs4_sessionid *sessionid)
1378 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1380 return sid->sequence % SESSION_HASH_SIZE;
1383 #ifdef CONFIG_SUNRPC_DEBUG
1385 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1387 u32 *ptr = (u32 *)(&sessionid->data[0]);
1388 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1392 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1398 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1399 * won't be used for replay.
1401 void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1403 struct nfs4_stateowner *so = cstate->replay_owner;
1405 if (nfserr == nfserr_replay_me)
1408 if (!seqid_mutating_err(ntohl(nfserr))) {
1409 nfsd4_cstate_clear_replay(cstate);
1414 if (so->so_is_open_owner)
1415 release_last_closed_stateid(openowner(so));
1421 gen_sessionid(struct nfsd4_session *ses)
1423 struct nfs4_client *clp = ses->se_client;
1424 struct nfsd4_sessionid *sid;
1426 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1427 sid->clientid = clp->cl_clientid;
1428 sid->sequence = current_sessionid++;
1433 * The protocol defines ca_maxresponssize_cached to include the size of
1434 * the rpc header, but all we need to cache is the data starting after
1435 * the end of the initial SEQUENCE operation--the rest we regenerate
1436 * each time. Therefore we can advertise a ca_maxresponssize_cached
1437 * value that is the number of bytes in our cache plus a few additional
1438 * bytes. In order to stay on the safe side, and not promise more than
1439 * we can cache, those additional bytes must be the minimum possible: 24
1440 * bytes of rpc header (xid through accept state, with AUTH_NULL
1441 * verifier), 12 for the compound header (with zero-length tag), and 44
1442 * for the SEQUENCE op response:
1444 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1447 free_session_slots(struct nfsd4_session *ses)
1451 for (i = 0; i < ses->se_fchannel.maxreqs; i++) {
1452 free_svc_cred(&ses->se_slots[i]->sl_cred);
1453 kfree(ses->se_slots[i]);
1458 * We don't actually need to cache the rpc and session headers, so we
1459 * can allocate a little less for each slot:
1461 static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
1465 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1468 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1469 return size + sizeof(struct nfsd4_slot);
1473 * XXX: If we run out of reserved DRC memory we could (up to a point)
1474 * re-negotiate active sessions and reduce their slot usage to make
1475 * room for new connections. For now we just fail the create session.
1477 static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
1479 u32 slotsize = slot_bytes(ca);
1480 u32 num = ca->maxreqs;
1483 spin_lock(&nfsd_drc_lock);
1484 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1485 nfsd_drc_max_mem - nfsd_drc_mem_used);
1487 * Never use more than a third of the remaining memory,
1488 * unless it's the only way to give this client a slot:
1490 avail = clamp_t(int, avail, slotsize, avail/3);
1491 num = min_t(int, num, avail / slotsize);
1492 nfsd_drc_mem_used += num * slotsize;
1493 spin_unlock(&nfsd_drc_lock);
1498 static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
1500 int slotsize = slot_bytes(ca);
1502 spin_lock(&nfsd_drc_lock);
1503 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
1504 spin_unlock(&nfsd_drc_lock);
1507 static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1508 struct nfsd4_channel_attrs *battrs)
1510 int numslots = fattrs->maxreqs;
1511 int slotsize = slot_bytes(fattrs);
1512 struct nfsd4_session *new;
1515 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
1516 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1517 mem = numslots * sizeof(struct nfsd4_slot *);
1519 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
1522 /* allocate each struct nfsd4_slot and data cache in one piece */
1523 for (i = 0; i < numslots; i++) {
1524 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
1525 if (!new->se_slots[i])
1529 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1530 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1535 kfree(new->se_slots[i]);
1540 static void free_conn(struct nfsd4_conn *c)
1542 svc_xprt_put(c->cn_xprt);
1546 static void nfsd4_conn_lost(struct svc_xpt_user *u)
1548 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1549 struct nfs4_client *clp = c->cn_session->se_client;
1551 spin_lock(&clp->cl_lock);
1552 if (!list_empty(&c->cn_persession)) {
1553 list_del(&c->cn_persession);
1556 nfsd4_probe_callback(clp);
1557 spin_unlock(&clp->cl_lock);
1560 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
1562 struct nfsd4_conn *conn;
1564 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1567 svc_xprt_get(rqstp->rq_xprt);
1568 conn->cn_xprt = rqstp->rq_xprt;
1569 conn->cn_flags = flags;
1570 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1574 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1576 conn->cn_session = ses;
1577 list_add(&conn->cn_persession, &ses->se_conns);
1580 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1582 struct nfs4_client *clp = ses->se_client;
1584 spin_lock(&clp->cl_lock);
1585 __nfsd4_hash_conn(conn, ses);
1586 spin_unlock(&clp->cl_lock);
1589 static int nfsd4_register_conn(struct nfsd4_conn *conn)
1591 conn->cn_xpt_user.callback = nfsd4_conn_lost;
1592 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
1595 static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
1599 nfsd4_hash_conn(conn, ses);
1600 ret = nfsd4_register_conn(conn);
1602 /* oops; xprt is already down: */
1603 nfsd4_conn_lost(&conn->cn_xpt_user);
1604 /* We may have gained or lost a callback channel: */
1605 nfsd4_probe_callback_sync(ses->se_client);
1608 static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
1610 u32 dir = NFS4_CDFC4_FORE;
1612 if (cses->flags & SESSION4_BACK_CHAN)
1613 dir |= NFS4_CDFC4_BACK;
1614 return alloc_conn(rqstp, dir);
1617 /* must be called under client_lock */
1618 static void nfsd4_del_conns(struct nfsd4_session *s)
1620 struct nfs4_client *clp = s->se_client;
1621 struct nfsd4_conn *c;
1623 spin_lock(&clp->cl_lock);
1624 while (!list_empty(&s->se_conns)) {
1625 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1626 list_del_init(&c->cn_persession);
1627 spin_unlock(&clp->cl_lock);
1629 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1632 spin_lock(&clp->cl_lock);
1634 spin_unlock(&clp->cl_lock);
1637 static void __free_session(struct nfsd4_session *ses)
1639 free_session_slots(ses);
1643 static void free_session(struct nfsd4_session *ses)
1645 nfsd4_del_conns(ses);
1646 nfsd4_put_drc_mem(&ses->se_fchannel);
1647 __free_session(ses);
1650 static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
1653 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1655 new->se_client = clp;
1658 INIT_LIST_HEAD(&new->se_conns);
1660 new->se_cb_seq_nr = 1;
1661 new->se_flags = cses->flags;
1662 new->se_cb_prog = cses->callback_prog;
1663 new->se_cb_sec = cses->cb_sec;
1664 atomic_set(&new->se_ref, 0);
1665 idx = hash_sessionid(&new->se_sessionid);
1666 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
1667 spin_lock(&clp->cl_lock);
1668 list_add(&new->se_perclnt, &clp->cl_sessions);
1669 spin_unlock(&clp->cl_lock);
1672 struct sockaddr *sa = svc_addr(rqstp);
1674 * This is a little silly; with sessions there's no real
1675 * use for the callback address. Use the peer address
1676 * as a reasonable default for now, but consider fixing
1677 * the rpc client not to require an address in the
1680 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1681 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
1685 /* caller must hold client_lock */
1686 static struct nfsd4_session *
1687 __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
1689 struct nfsd4_session *elem;
1691 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1693 lockdep_assert_held(&nn->client_lock);
1695 dump_sessionid(__func__, sessionid);
1696 idx = hash_sessionid(sessionid);
1697 /* Search in the appropriate list */
1698 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
1699 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1700 NFS4_MAX_SESSIONID_LEN)) {
1705 dprintk("%s: session not found\n", __func__);
1709 static struct nfsd4_session *
1710 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1713 struct nfsd4_session *session;
1714 __be32 status = nfserr_badsession;
1716 session = __find_in_sessionid_hashtbl(sessionid, net);
1719 status = nfsd4_get_session_locked(session);
1727 /* caller must hold client_lock */
1729 unhash_session(struct nfsd4_session *ses)
1731 struct nfs4_client *clp = ses->se_client;
1732 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1734 lockdep_assert_held(&nn->client_lock);
1736 list_del(&ses->se_hash);
1737 spin_lock(&ses->se_client->cl_lock);
1738 list_del(&ses->se_perclnt);
1739 spin_unlock(&ses->se_client->cl_lock);
1742 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1744 STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
1747 * We're assuming the clid was not given out from a boot
1748 * precisely 2^32 (about 136 years) before this one. That seems
1749 * a safe assumption:
1751 if (clid->cl_boot == (u32)nn->boot_time)
1753 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
1754 clid->cl_boot, clid->cl_id, nn->boot_time);
1759 * XXX Should we use a slab cache ?
1760 * This type of memory management is somewhat inefficient, but we use it
1761 * anyway since SETCLIENTID is not a common operation.
1763 static struct nfs4_client *alloc_client(struct xdr_netobj name)
1765 struct nfs4_client *clp;
1768 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1771 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
1772 if (clp->cl_name.data == NULL)
1774 clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
1775 OWNER_HASH_SIZE, GFP_KERNEL);
1776 if (!clp->cl_ownerstr_hashtbl)
1777 goto err_no_hashtbl;
1778 for (i = 0; i < OWNER_HASH_SIZE; i++)
1779 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
1780 clp->cl_name.len = name.len;
1781 INIT_LIST_HEAD(&clp->cl_sessions);
1782 idr_init(&clp->cl_stateids);
1783 atomic_set(&clp->cl_refcount, 0);
1784 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1785 INIT_LIST_HEAD(&clp->cl_idhash);
1786 INIT_LIST_HEAD(&clp->cl_openowners);
1787 INIT_LIST_HEAD(&clp->cl_delegations);
1788 INIT_LIST_HEAD(&clp->cl_lru);
1789 INIT_LIST_HEAD(&clp->cl_revoked);
1790 #ifdef CONFIG_NFSD_PNFS
1791 INIT_LIST_HEAD(&clp->cl_lo_states);
1793 spin_lock_init(&clp->cl_lock);
1794 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1797 kfree(clp->cl_name.data);
1804 free_client(struct nfs4_client *clp)
1806 while (!list_empty(&clp->cl_sessions)) {
1807 struct nfsd4_session *ses;
1808 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1810 list_del(&ses->se_perclnt);
1811 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1814 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
1815 free_svc_cred(&clp->cl_cred);
1816 kfree(clp->cl_ownerstr_hashtbl);
1817 kfree(clp->cl_name.data);
1818 idr_destroy(&clp->cl_stateids);
1822 /* must be called under the client_lock */
1824 unhash_client_locked(struct nfs4_client *clp)
1826 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1827 struct nfsd4_session *ses;
1829 lockdep_assert_held(&nn->client_lock);
1831 /* Mark the client as expired! */
1833 /* Make it invisible */
1834 if (!list_empty(&clp->cl_idhash)) {
1835 list_del_init(&clp->cl_idhash);
1836 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1837 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1839 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1841 list_del_init(&clp->cl_lru);
1842 spin_lock(&clp->cl_lock);
1843 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1844 list_del_init(&ses->se_hash);
1845 spin_unlock(&clp->cl_lock);
1849 unhash_client(struct nfs4_client *clp)
1851 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1853 spin_lock(&nn->client_lock);
1854 unhash_client_locked(clp);
1855 spin_unlock(&nn->client_lock);
1858 static __be32 mark_client_expired_locked(struct nfs4_client *clp)
1860 if (atomic_read(&clp->cl_refcount))
1861 return nfserr_jukebox;
1862 unhash_client_locked(clp);
1867 __destroy_client(struct nfs4_client *clp)
1869 struct nfs4_openowner *oo;
1870 struct nfs4_delegation *dp;
1871 struct list_head reaplist;
1873 INIT_LIST_HEAD(&reaplist);
1874 spin_lock(&state_lock);
1875 while (!list_empty(&clp->cl_delegations)) {
1876 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
1877 WARN_ON(!unhash_delegation_locked(dp));
1878 list_add(&dp->dl_recall_lru, &reaplist);
1880 spin_unlock(&state_lock);
1881 while (!list_empty(&reaplist)) {
1882 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
1883 list_del_init(&dp->dl_recall_lru);
1884 put_clnt_odstate(dp->dl_clnt_odstate);
1885 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
1886 nfs4_put_stid(&dp->dl_stid);
1888 while (!list_empty(&clp->cl_revoked)) {
1889 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
1890 list_del_init(&dp->dl_recall_lru);
1891 nfs4_put_stid(&dp->dl_stid);
1893 while (!list_empty(&clp->cl_openowners)) {
1894 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1895 nfs4_get_stateowner(&oo->oo_owner);
1896 release_openowner(oo);
1898 nfsd4_return_all_client_layouts(clp);
1899 nfsd4_shutdown_callback(clp);
1900 if (clp->cl_cb_conn.cb_xprt)
1901 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
1906 destroy_client(struct nfs4_client *clp)
1909 __destroy_client(clp);
1912 static void expire_client(struct nfs4_client *clp)
1915 nfsd4_client_record_remove(clp);
1916 __destroy_client(clp);
1919 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1921 memcpy(target->cl_verifier.data, source->data,
1922 sizeof(target->cl_verifier.data));
1925 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1927 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1928 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1931 static int copy_cred(struct svc_cred *target, struct svc_cred *source)
1933 target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL);
1934 target->cr_raw_principal = kstrdup(source->cr_raw_principal,
1936 if ((source->cr_principal && ! target->cr_principal) ||
1937 (source->cr_raw_principal && ! target->cr_raw_principal))
1940 target->cr_flavor = source->cr_flavor;
1941 target->cr_uid = source->cr_uid;
1942 target->cr_gid = source->cr_gid;
1943 target->cr_group_info = source->cr_group_info;
1944 get_group_info(target->cr_group_info);
1945 target->cr_gss_mech = source->cr_gss_mech;
1946 if (source->cr_gss_mech)
1947 gss_mech_get(source->cr_gss_mech);
1952 compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1954 if (o1->len < o2->len)
1956 if (o1->len > o2->len)
1958 return memcmp(o1->data, o2->data, o1->len);
1961 static int same_name(const char *n1, const char *n2)
1963 return 0 == memcmp(n1, n2, HEXDIR_LEN);
1967 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1969 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
1973 same_clid(clientid_t *cl1, clientid_t *cl2)
1975 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
1978 static bool groups_equal(struct group_info *g1, struct group_info *g2)
1982 if (g1->ngroups != g2->ngroups)
1984 for (i=0; i<g1->ngroups; i++)
1985 if (!gid_eq(g1->gid[i], g2->gid[i]))
1991 * RFC 3530 language requires clid_inuse be returned when the
1992 * "principal" associated with a requests differs from that previously
1993 * used. We use uid, gid's, and gss principal string as our best
1994 * approximation. We also don't want to allow non-gss use of a client
1995 * established using gss: in theory cr_principal should catch that
1996 * change, but in practice cr_principal can be null even in the gss case
1997 * since gssd doesn't always pass down a principal string.
1999 static bool is_gss_cred(struct svc_cred *cr)
2001 /* Is cr_flavor one of the gss "pseudoflavors"?: */
2002 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
2007 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
2009 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
2010 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
2011 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
2012 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
2014 if (cr1->cr_principal == cr2->cr_principal)
2016 if (!cr1->cr_principal || !cr2->cr_principal)
2018 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
2021 static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
2023 struct svc_cred *cr = &rqstp->rq_cred;
2026 if (!cr->cr_gss_mech)
2028 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
2029 return service == RPC_GSS_SVC_INTEGRITY ||
2030 service == RPC_GSS_SVC_PRIVACY;
2033 bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
2035 struct svc_cred *cr = &rqstp->rq_cred;
2037 if (!cl->cl_mach_cred)
2039 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
2041 if (!svc_rqst_integrity_protected(rqstp))
2043 if (cl->cl_cred.cr_raw_principal)
2044 return 0 == strcmp(cl->cl_cred.cr_raw_principal,
2045 cr->cr_raw_principal);
2046 if (!cr->cr_principal)
2048 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
2051 static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
2056 * This is opaque to client, so no need to byte-swap. Use
2057 * __force to keep sparse happy
2059 verf[0] = (__force __be32)get_seconds();
2060 verf[1] = (__force __be32)nn->clverifier_counter++;
2061 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
2064 static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
2066 clp->cl_clientid.cl_boot = nn->boot_time;
2067 clp->cl_clientid.cl_id = nn->clientid_counter++;
2068 gen_confirm(clp, nn);
2071 static struct nfs4_stid *
2072 find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
2074 struct nfs4_stid *ret;
2076 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
2077 if (!ret || !ret->sc_type)
2082 static struct nfs4_stid *
2083 find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
2085 struct nfs4_stid *s;
2087 spin_lock(&cl->cl_lock);
2088 s = find_stateid_locked(cl, t);
2090 if (typemask & s->sc_type)
2091 refcount_inc(&s->sc_count);
2095 spin_unlock(&cl->cl_lock);
2099 static struct nfs4_client *create_client(struct xdr_netobj name,
2100 struct svc_rqst *rqstp, nfs4_verifier *verf)
2102 struct nfs4_client *clp;
2103 struct sockaddr *sa = svc_addr(rqstp);
2105 struct net *net = SVC_NET(rqstp);
2107 clp = alloc_client(name);
2111 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
2116 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
2117 clp->cl_time = get_seconds();
2118 clear_bit(0, &clp->cl_cb_slot_busy);
2119 copy_verf(clp, verf);
2120 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
2121 clp->cl_cb_session = NULL;
2127 add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
2129 struct rb_node **new = &(root->rb_node), *parent = NULL;
2130 struct nfs4_client *clp;
2133 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
2136 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
2137 new = &((*new)->rb_left);
2139 new = &((*new)->rb_right);
2142 rb_link_node(&new_clp->cl_namenode, parent, new);
2143 rb_insert_color(&new_clp->cl_namenode, root);
2146 static struct nfs4_client *
2147 find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2150 struct rb_node *node = root->rb_node;
2151 struct nfs4_client *clp;
2154 clp = rb_entry(node, struct nfs4_client, cl_namenode);
2155 cmp = compare_blob(&clp->cl_name, name);
2157 node = node->rb_left;
2159 node = node->rb_right;
2167 add_to_unconfirmed(struct nfs4_client *clp)
2169 unsigned int idhashval;
2170 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2172 lockdep_assert_held(&nn->client_lock);
2174 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2175 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
2176 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
2177 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
2178 renew_client_locked(clp);
2182 move_to_confirmed(struct nfs4_client *clp)
2184 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
2185 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2187 lockdep_assert_held(&nn->client_lock);
2189 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
2190 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
2191 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
2192 add_clp_to_name_tree(clp, &nn->conf_name_tree);
2193 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2194 renew_client_locked(clp);
2197 static struct nfs4_client *
2198 find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
2200 struct nfs4_client *clp;
2201 unsigned int idhashval = clientid_hashval(clid->cl_id);
2203 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
2204 if (same_clid(&clp->cl_clientid, clid)) {
2205 if ((bool)clp->cl_minorversion != sessions)
2207 renew_client_locked(clp);
2214 static struct nfs4_client *
2215 find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2217 struct list_head *tbl = nn->conf_id_hashtbl;
2219 lockdep_assert_held(&nn->client_lock);
2220 return find_client_in_id_table(tbl, clid, sessions);
2223 static struct nfs4_client *
2224 find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2226 struct list_head *tbl = nn->unconf_id_hashtbl;
2228 lockdep_assert_held(&nn->client_lock);
2229 return find_client_in_id_table(tbl, clid, sessions);
2232 static bool clp_used_exchangeid(struct nfs4_client *clp)
2234 return clp->cl_exchange_flags != 0;
2237 static struct nfs4_client *
2238 find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
2240 lockdep_assert_held(&nn->client_lock);
2241 return find_clp_in_name_tree(name, &nn->conf_name_tree);
2244 static struct nfs4_client *
2245 find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
2247 lockdep_assert_held(&nn->client_lock);
2248 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
2252 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
2254 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
2255 struct sockaddr *sa = svc_addr(rqstp);
2256 u32 scopeid = rpc_get_scope_id(sa);
2257 unsigned short expected_family;
2259 /* Currently, we only support tcp and tcp6 for the callback channel */
2260 if (se->se_callback_netid_len == 3 &&
2261 !memcmp(se->se_callback_netid_val, "tcp", 3))
2262 expected_family = AF_INET;
2263 else if (se->se_callback_netid_len == 4 &&
2264 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2265 expected_family = AF_INET6;
2269 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
2270 se->se_callback_addr_len,
2271 (struct sockaddr *)&conn->cb_addr,
2272 sizeof(conn->cb_addr));
2274 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
2277 if (conn->cb_addr.ss_family == AF_INET6)
2278 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
2280 conn->cb_prog = se->se_callback_prog;
2281 conn->cb_ident = se->se_callback_ident;
2282 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
2285 conn->cb_addr.ss_family = AF_UNSPEC;
2286 conn->cb_addrlen = 0;
2287 dprintk("NFSD: this client (clientid %08x/%08x) "
2288 "will not receive delegations\n",
2289 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2295 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
2298 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2300 struct xdr_buf *buf = resp->xdr.buf;
2301 struct nfsd4_slot *slot = resp->cstate.slot;
2304 dprintk("--> %s slot %p\n", __func__, slot);
2306 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
2307 slot->sl_opcnt = resp->opcnt;
2308 slot->sl_status = resp->cstate.status;
2309 free_svc_cred(&slot->sl_cred);
2310 copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred);
2312 if (!nfsd4_cache_this(resp)) {
2313 slot->sl_flags &= ~NFSD4_SLOT_CACHED;
2316 slot->sl_flags |= NFSD4_SLOT_CACHED;
2318 base = resp->cstate.data_offset;
2319 slot->sl_datalen = buf->len - base;
2320 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
2321 WARN(1, "%s: sessions DRC could not cache compound\n",
2327 * Encode the replay sequence operation from the slot values.
2328 * If cachethis is FALSE encode the uncached rep error on the next
2329 * operation which sets resp->p and increments resp->opcnt for
2330 * nfs4svc_encode_compoundres.
2334 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2335 struct nfsd4_compoundres *resp)
2337 struct nfsd4_op *op;
2338 struct nfsd4_slot *slot = resp->cstate.slot;
2340 /* Encode the replayed sequence operation */
2341 op = &args->ops[resp->opcnt - 1];
2342 nfsd4_encode_operation(resp, op);
2344 if (slot->sl_flags & NFSD4_SLOT_CACHED)
2346 if (args->opcnt == 1) {
2348 * The original operation wasn't a solo sequence--we
2349 * always cache those--so this retry must not match the
2352 op->status = nfserr_seq_false_retry;
2354 op = &args->ops[resp->opcnt++];
2355 op->status = nfserr_retry_uncached_rep;
2356 nfsd4_encode_operation(resp, op);
2362 * The sequence operation is not cached because we can use the slot and
2366 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2367 struct nfsd4_sequence *seq)
2369 struct nfsd4_slot *slot = resp->cstate.slot;
2370 struct xdr_stream *xdr = &resp->xdr;
2374 dprintk("--> %s slot %p\n", __func__, slot);
2376 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
2380 p = xdr_reserve_space(xdr, slot->sl_datalen);
2383 return nfserr_serverfault;
2385 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2386 xdr_commit_encode(xdr);
2388 resp->opcnt = slot->sl_opcnt;
2389 return slot->sl_status;
2393 * Set the exchange_id flags returned by the server.
2396 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2398 #ifdef CONFIG_NFSD_PNFS
2399 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
2401 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
2404 /* Referrals are supported, Migration is not. */
2405 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2407 /* set the wire flags to return to client. */
2408 clid->flags = new->cl_exchange_flags;
2411 static bool client_has_openowners(struct nfs4_client *clp)
2413 struct nfs4_openowner *oo;
2415 list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
2416 if (!list_empty(&oo->oo_owner.so_stateids))
2422 static bool client_has_state(struct nfs4_client *clp)
2424 return client_has_openowners(clp)
2425 #ifdef CONFIG_NFSD_PNFS
2426 || !list_empty(&clp->cl_lo_states)
2428 || !list_empty(&clp->cl_delegations)
2429 || !list_empty(&clp->cl_sessions);
2433 nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2434 union nfsd4_op_u *u)
2436 struct nfsd4_exchange_id *exid = &u->exchange_id;
2437 struct nfs4_client *conf, *new;
2438 struct nfs4_client *unconf = NULL;
2440 char addr_str[INET6_ADDRSTRLEN];
2441 nfs4_verifier verf = exid->verifier;
2442 struct sockaddr *sa = svc_addr(rqstp);
2443 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
2444 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2446 rpc_ntop(sa, addr_str, sizeof(addr_str));
2447 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
2448 "ip_addr=%s flags %x, spa_how %d\n",
2449 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
2450 addr_str, exid->flags, exid->spa_how);
2452 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
2453 return nfserr_inval;
2455 new = create_client(exid->clname, rqstp, &verf);
2457 return nfserr_jukebox;
2459 switch (exid->spa_how) {
2461 exid->spo_must_enforce[0] = 0;
2462 exid->spo_must_enforce[1] = (
2463 1 << (OP_BIND_CONN_TO_SESSION - 32) |
2464 1 << (OP_EXCHANGE_ID - 32) |
2465 1 << (OP_CREATE_SESSION - 32) |
2466 1 << (OP_DESTROY_SESSION - 32) |
2467 1 << (OP_DESTROY_CLIENTID - 32));
2469 exid->spo_must_allow[0] &= (1 << (OP_CLOSE) |
2470 1 << (OP_OPEN_DOWNGRADE) |
2472 1 << (OP_DELEGRETURN));
2474 exid->spo_must_allow[1] &= (
2475 1 << (OP_TEST_STATEID - 32) |
2476 1 << (OP_FREE_STATEID - 32));
2477 if (!svc_rqst_integrity_protected(rqstp)) {
2478 status = nfserr_inval;
2482 * Sometimes userspace doesn't give us a principal.
2483 * Which is a bug, really. Anyway, we can't enforce
2484 * MACH_CRED in that case, better to give up now:
2486 if (!new->cl_cred.cr_principal &&
2487 !new->cl_cred.cr_raw_principal) {
2488 status = nfserr_serverfault;
2491 new->cl_mach_cred = true;
2494 default: /* checked by xdr code */
2497 status = nfserr_encr_alg_unsupp;
2501 /* Cases below refer to rfc 5661 section 18.35.4: */
2502 spin_lock(&nn->client_lock);
2503 conf = find_confirmed_client_by_name(&exid->clname, nn);
2505 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2506 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2509 if (!clp_used_exchangeid(conf)) { /* buggy client */
2510 status = nfserr_inval;
2513 if (!nfsd4_mach_creds_match(conf, rqstp)) {
2514 status = nfserr_wrong_cred;
2517 if (!creds_match) { /* case 9 */
2518 status = nfserr_perm;
2521 if (!verfs_match) { /* case 8 */
2522 status = nfserr_not_same;
2526 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
2529 if (!creds_match) { /* case 3 */
2530 if (client_has_state(conf)) {
2531 status = nfserr_clid_inuse;
2536 if (verfs_match) { /* case 2 */
2537 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
2540 /* case 5, client reboot */
2545 if (update) { /* case 7 */
2546 status = nfserr_noent;
2550 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
2551 if (unconf) /* case 4, possible retry or client restart */
2552 unhash_client_locked(unconf);
2554 /* case 1 (normal case) */
2557 status = mark_client_expired_locked(conf);
2561 new->cl_minorversion = cstate->minorversion;
2562 new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0];
2563 new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1];
2566 add_to_unconfirmed(new);
2569 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
2570 exid->clientid.cl_id = conf->cl_clientid.cl_id;
2572 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
2573 nfsd4_set_ex_flags(conf, exid);
2575 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
2576 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
2580 spin_unlock(&nn->client_lock);
2585 expire_client(unconf);
2590 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
2592 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2595 /* The slot is in use, and no response has been sent. */
2597 if (seqid == slot_seqid)
2598 return nfserr_jukebox;
2600 return nfserr_seq_misordered;
2602 /* Note unsigned 32-bit arithmetic handles wraparound: */
2603 if (likely(seqid == slot_seqid + 1))
2605 if (seqid == slot_seqid)
2606 return nfserr_replay_cache;
2607 return nfserr_seq_misordered;
2611 * Cache the create session result into the create session single DRC
2612 * slot cache by saving the xdr structure. sl_seqid has been set.
2613 * Do this for solo or embedded create session operations.
2616 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
2617 struct nfsd4_clid_slot *slot, __be32 nfserr)
2619 slot->sl_status = nfserr;
2620 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2624 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2625 struct nfsd4_clid_slot *slot)
2627 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2628 return slot->sl_status;
2631 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2632 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2633 1 + /* MIN tag is length with zero, only length */ \
2634 3 + /* version, opcount, opcode */ \
2635 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2636 /* seqid, slotID, slotID, cache */ \
2637 4 ) * sizeof(__be32))
2639 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2640 2 + /* verifier: AUTH_NULL, length 0 */\
2642 1 + /* MIN tag is length with zero, only length */ \
2643 3 + /* opcount, opcode, opstatus*/ \
2644 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2645 /* seqid, slotID, slotID, slotID, status */ \
2646 5 ) * sizeof(__be32))
2648 static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
2650 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2652 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2653 return nfserr_toosmall;
2654 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2655 return nfserr_toosmall;
2656 ca->headerpadsz = 0;
2657 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2658 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2659 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2660 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2661 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2662 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2664 * Note decreasing slot size below client's request may make it
2665 * difficult for client to function correctly, whereas
2666 * decreasing the number of slots will (just?) affect
2667 * performance. When short on memory we therefore prefer to
2668 * decrease number of slots instead of their size. Clients that
2669 * request larger slots than they need will get poor results:
2671 ca->maxreqs = nfsd4_get_drc_mem(ca);
2673 return nfserr_jukebox;
2679 * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
2680 * These are based on similar macros in linux/sunrpc/msg_prot.h .
2682 #define RPC_MAX_HEADER_WITH_AUTH_SYS \
2683 (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
2685 #define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
2686 (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
2688 #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2689 RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
2690 #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2691 RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
2694 static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2696 ca->headerpadsz = 0;
2698 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
2699 return nfserr_toosmall;
2700 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
2701 return nfserr_toosmall;
2702 ca->maxresp_cached = 0;
2704 return nfserr_toosmall;
2709 static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2711 switch (cbs->flavor) {
2717 * GSS case: the spec doesn't allow us to return this
2718 * error. But it also doesn't allow us not to support
2720 * I'd rather this fail hard than return some error the
2721 * client might think it can already handle:
2723 return nfserr_encr_alg_unsupp;
2728 nfsd4_create_session(struct svc_rqst *rqstp,
2729 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
2731 struct nfsd4_create_session *cr_ses = &u->create_session;
2732 struct sockaddr *sa = svc_addr(rqstp);
2733 struct nfs4_client *conf, *unconf;
2734 struct nfs4_client *old = NULL;
2735 struct nfsd4_session *new;
2736 struct nfsd4_conn *conn;
2737 struct nfsd4_clid_slot *cs_slot = NULL;
2739 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2741 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2742 return nfserr_inval;
2743 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2746 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
2749 status = check_backchannel_attrs(&cr_ses->back_channel);
2751 goto out_release_drc_mem;
2752 status = nfserr_jukebox;
2753 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
2755 goto out_release_drc_mem;
2756 conn = alloc_conn_from_crses(rqstp, cr_ses);
2758 goto out_free_session;
2760 spin_lock(&nn->client_lock);
2761 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
2762 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
2763 WARN_ON_ONCE(conf && unconf);
2766 status = nfserr_wrong_cred;
2767 if (!nfsd4_mach_creds_match(conf, rqstp))
2769 cs_slot = &conf->cl_cs_slot;
2770 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2772 if (status == nfserr_replay_cache)
2773 status = nfsd4_replay_create_session(cr_ses, cs_slot);
2776 } else if (unconf) {
2777 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
2778 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
2779 status = nfserr_clid_inuse;
2782 status = nfserr_wrong_cred;
2783 if (!nfsd4_mach_creds_match(unconf, rqstp))
2785 cs_slot = &unconf->cl_cs_slot;
2786 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2788 /* an unconfirmed replay returns misordered */
2789 status = nfserr_seq_misordered;
2792 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
2794 status = mark_client_expired_locked(old);
2800 move_to_confirmed(unconf);
2803 status = nfserr_stale_clientid;
2807 /* Persistent sessions are not supported */
2808 cr_ses->flags &= ~SESSION4_PERSIST;
2809 /* Upshifting from TCP to RDMA is not supported */
2810 cr_ses->flags &= ~SESSION4_RDMA;
2812 init_session(rqstp, new, conf, cr_ses);
2813 nfsd4_get_session_locked(new);
2815 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
2816 NFS4_MAX_SESSIONID_LEN);
2817 cs_slot->sl_seqid++;
2818 cr_ses->seqid = cs_slot->sl_seqid;
2820 /* cache solo and embedded create sessions under the client_lock */
2821 nfsd4_cache_create_session(cr_ses, cs_slot, status);
2822 spin_unlock(&nn->client_lock);
2823 /* init connection and backchannel */
2824 nfsd4_init_conn(rqstp, conn, new);
2825 nfsd4_put_session(new);
2830 spin_unlock(&nn->client_lock);
2835 __free_session(new);
2836 out_release_drc_mem:
2837 nfsd4_put_drc_mem(&cr_ses->fore_channel);
2841 static __be32 nfsd4_map_bcts_dir(u32 *dir)
2844 case NFS4_CDFC4_FORE:
2845 case NFS4_CDFC4_BACK:
2847 case NFS4_CDFC4_FORE_OR_BOTH:
2848 case NFS4_CDFC4_BACK_OR_BOTH:
2849 *dir = NFS4_CDFC4_BOTH;
2852 return nfserr_inval;
2855 __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp,
2856 struct nfsd4_compound_state *cstate,
2857 union nfsd4_op_u *u)
2859 struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl;
2860 struct nfsd4_session *session = cstate->session;
2861 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2864 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2867 spin_lock(&nn->client_lock);
2868 session->se_cb_prog = bc->bc_cb_program;
2869 session->se_cb_sec = bc->bc_cb_sec;
2870 spin_unlock(&nn->client_lock);
2872 nfsd4_probe_callback(session->se_client);
2877 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2878 struct nfsd4_compound_state *cstate,
2879 union nfsd4_op_u *u)
2881 struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session;
2883 struct nfsd4_conn *conn;
2884 struct nfsd4_session *session;
2885 struct net *net = SVC_NET(rqstp);
2886 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2888 if (!nfsd4_last_compound_op(rqstp))
2889 return nfserr_not_only_op;
2890 spin_lock(&nn->client_lock);
2891 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
2892 spin_unlock(&nn->client_lock);
2894 goto out_no_session;
2895 status = nfserr_wrong_cred;
2896 if (!nfsd4_mach_creds_match(session->se_client, rqstp))
2898 status = nfsd4_map_bcts_dir(&bcts->dir);
2901 conn = alloc_conn(rqstp, bcts->dir);
2902 status = nfserr_jukebox;
2905 nfsd4_init_conn(rqstp, conn, session);
2908 nfsd4_put_session(session);
2913 static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2917 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2921 nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate,
2922 union nfsd4_op_u *u)
2924 struct nfsd4_destroy_session *sessionid = &u->destroy_session;
2925 struct nfsd4_session *ses;
2927 int ref_held_by_me = 0;
2928 struct net *net = SVC_NET(r);
2929 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2931 status = nfserr_not_only_op;
2932 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
2933 if (!nfsd4_last_compound_op(r))
2937 dump_sessionid(__func__, &sessionid->sessionid);
2938 spin_lock(&nn->client_lock);
2939 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
2941 goto out_client_lock;
2942 status = nfserr_wrong_cred;
2943 if (!nfsd4_mach_creds_match(ses->se_client, r))
2944 goto out_put_session;
2945 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
2947 goto out_put_session;
2948 unhash_session(ses);
2949 spin_unlock(&nn->client_lock);
2951 nfsd4_probe_callback_sync(ses->se_client);
2953 spin_lock(&nn->client_lock);
2956 nfsd4_put_session_locked(ses);
2958 spin_unlock(&nn->client_lock);
2963 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
2965 struct nfsd4_conn *c;
2967 list_for_each_entry(c, &s->se_conns, cn_persession) {
2968 if (c->cn_xprt == xpt) {
2975 static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
2977 struct nfs4_client *clp = ses->se_client;
2978 struct nfsd4_conn *c;
2979 __be32 status = nfs_ok;
2982 spin_lock(&clp->cl_lock);
2983 c = __nfsd4_find_conn(new->cn_xprt, ses);
2986 status = nfserr_conn_not_bound_to_session;
2987 if (clp->cl_mach_cred)
2989 __nfsd4_hash_conn(new, ses);
2990 spin_unlock(&clp->cl_lock);
2991 ret = nfsd4_register_conn(new);
2993 /* oops; xprt is already down: */
2994 nfsd4_conn_lost(&new->cn_xpt_user);
2997 spin_unlock(&clp->cl_lock);
3002 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
3004 struct nfsd4_compoundargs *args = rqstp->rq_argp;
3006 return args->opcnt > session->se_fchannel.maxops;
3009 static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
3010 struct nfsd4_session *session)
3012 struct xdr_buf *xb = &rqstp->rq_arg;
3014 return xb->len > session->se_fchannel.maxreq_sz;
3017 static bool replay_matches_cache(struct svc_rqst *rqstp,
3018 struct nfsd4_sequence *seq, struct nfsd4_slot *slot)
3020 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
3022 if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) !=
3023 (bool)seq->cachethis)
3026 * If there's an error than the reply can have fewer ops than
3027 * the call. But if we cached a reply with *more* ops than the
3028 * call you're sending us now, then this new call is clearly not
3029 * really a replay of the old one:
3031 if (slot->sl_opcnt < argp->opcnt)
3033 /* This is the only check explicitly called by spec: */
3034 if (!same_creds(&rqstp->rq_cred, &slot->sl_cred))
3037 * There may be more comparisons we could actually do, but the
3038 * spec doesn't require us to catch every case where the calls
3039 * don't match (that would require caching the call as well as
3040 * the reply), so we don't bother.
3046 nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3047 union nfsd4_op_u *u)
3049 struct nfsd4_sequence *seq = &u->sequence;
3050 struct nfsd4_compoundres *resp = rqstp->rq_resp;
3051 struct xdr_stream *xdr = &resp->xdr;
3052 struct nfsd4_session *session;
3053 struct nfs4_client *clp;
3054 struct nfsd4_slot *slot;
3055 struct nfsd4_conn *conn;
3058 struct net *net = SVC_NET(rqstp);
3059 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3061 if (resp->opcnt != 1)
3062 return nfserr_sequence_pos;
3065 * Will be either used or freed by nfsd4_sequence_check_conn
3068 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
3070 return nfserr_jukebox;
3072 spin_lock(&nn->client_lock);
3073 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
3075 goto out_no_session;
3076 clp = session->se_client;
3078 status = nfserr_too_many_ops;
3079 if (nfsd4_session_too_many_ops(rqstp, session))
3080 goto out_put_session;
3082 status = nfserr_req_too_big;
3083 if (nfsd4_request_too_big(rqstp, session))
3084 goto out_put_session;
3086 status = nfserr_badslot;
3087 if (seq->slotid >= session->se_fchannel.maxreqs)
3088 goto out_put_session;
3090 slot = session->se_slots[seq->slotid];
3091 dprintk("%s: slotid %d\n", __func__, seq->slotid);
3093 /* We do not negotiate the number of slots yet, so set the
3094 * maxslots to the session maxreqs which is used to encode
3095 * sr_highest_slotid and the sr_target_slot id to maxslots */
3096 seq->maxslots = session->se_fchannel.maxreqs;
3098 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
3099 slot->sl_flags & NFSD4_SLOT_INUSE);
3100 if (status == nfserr_replay_cache) {
3101 status = nfserr_seq_misordered;
3102 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
3103 goto out_put_session;
3104 status = nfserr_seq_false_retry;
3105 if (!replay_matches_cache(rqstp, seq, slot))
3106 goto out_put_session;
3107 cstate->slot = slot;
3108 cstate->session = session;
3110 /* Return the cached reply status and set cstate->status
3111 * for nfsd4_proc_compound processing */
3112 status = nfsd4_replay_cache_entry(resp, seq);
3113 cstate->status = nfserr_replay_cache;
3117 goto out_put_session;
3119 status = nfsd4_sequence_check_conn(conn, session);
3122 goto out_put_session;
3124 buflen = (seq->cachethis) ?
3125 session->se_fchannel.maxresp_cached :
3126 session->se_fchannel.maxresp_sz;
3127 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
3129 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
3130 goto out_put_session;
3131 svc_reserve(rqstp, buflen);
3134 /* Success! bump slot seqid */
3135 slot->sl_seqid = seq->seqid;
3136 slot->sl_flags |= NFSD4_SLOT_INUSE;
3138 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
3140 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
3142 cstate->slot = slot;
3143 cstate->session = session;
3147 switch (clp->cl_cb_state) {
3149 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
3151 case NFSD4_CB_FAULT:
3152 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
3155 seq->status_flags = 0;
3157 if (!list_empty(&clp->cl_revoked))
3158 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
3162 spin_unlock(&nn->client_lock);
3165 nfsd4_put_session_locked(session);
3166 goto out_no_session;
3170 nfsd4_sequence_done(struct nfsd4_compoundres *resp)
3172 struct nfsd4_compound_state *cs = &resp->cstate;
3174 if (nfsd4_has_session(cs)) {
3175 if (cs->status != nfserr_replay_cache) {
3176 nfsd4_store_cache_entry(resp);
3177 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
3179 /* Drop session reference that was taken in nfsd4_sequence() */
3180 nfsd4_put_session(cs->session);
3182 put_client_renew(cs->clp);
3186 nfsd4_destroy_clientid(struct svc_rqst *rqstp,
3187 struct nfsd4_compound_state *cstate,
3188 union nfsd4_op_u *u)
3190 struct nfsd4_destroy_clientid *dc = &u->destroy_clientid;
3191 struct nfs4_client *conf, *unconf;
3192 struct nfs4_client *clp = NULL;
3194 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3196 spin_lock(&nn->client_lock);
3197 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
3198 conf = find_confirmed_client(&dc->clientid, true, nn);
3199 WARN_ON_ONCE(conf && unconf);
3202 if (client_has_state(conf)) {
3203 status = nfserr_clientid_busy;
3206 status = mark_client_expired_locked(conf);
3213 status = nfserr_stale_clientid;
3216 if (!nfsd4_mach_creds_match(clp, rqstp)) {
3218 status = nfserr_wrong_cred;
3221 unhash_client_locked(clp);
3223 spin_unlock(&nn->client_lock);
3230 nfsd4_reclaim_complete(struct svc_rqst *rqstp,
3231 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
3233 struct nfsd4_reclaim_complete *rc = &u->reclaim_complete;
3236 if (rc->rca_one_fs) {
3237 if (!cstate->current_fh.fh_dentry)
3238 return nfserr_nofilehandle;
3240 * We don't take advantage of the rca_one_fs case.
3241 * That's OK, it's optional, we can safely ignore it.
3246 status = nfserr_complete_already;
3247 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
3248 &cstate->session->se_client->cl_flags))
3251 status = nfserr_stale_clientid;
3252 if (is_client_expired(cstate->session->se_client))
3254 * The following error isn't really legal.
3255 * But we only get here if the client just explicitly
3256 * destroyed the client. Surely it no longer cares what
3257 * error it gets back on an operation for the dead
3263 nfsd4_client_record_create(cstate->session->se_client);
3269 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3270 union nfsd4_op_u *u)
3272 struct nfsd4_setclientid *setclid = &u->setclientid;
3273 struct xdr_netobj clname = setclid->se_name;
3274 nfs4_verifier clverifier = setclid->se_verf;
3275 struct nfs4_client *conf, *new;
3276 struct nfs4_client *unconf = NULL;
3278 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3280 new = create_client(clname, rqstp, &clverifier);
3282 return nfserr_jukebox;
3283 /* Cases below refer to rfc 3530 section 14.2.33: */
3284 spin_lock(&nn->client_lock);
3285 conf = find_confirmed_client_by_name(&clname, nn);
3286 if (conf && client_has_state(conf)) {
3288 status = nfserr_clid_inuse;
3289 if (clp_used_exchangeid(conf))
3291 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
3292 char addr_str[INET6_ADDRSTRLEN];
3293 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
3295 dprintk("NFSD: setclientid: string in use by client "
3296 "at %s\n", addr_str);
3300 unconf = find_unconfirmed_client_by_name(&clname, nn);
3302 unhash_client_locked(unconf);
3303 if (conf && same_verf(&conf->cl_verifier, &clverifier)) {
3304 /* case 1: probable callback update */
3305 copy_clid(new, conf);
3306 gen_confirm(new, nn);
3307 } else /* case 4 (new client) or cases 2, 3 (client reboot): */
3309 new->cl_minorversion = 0;
3310 gen_callback(new, setclid, rqstp);
3311 add_to_unconfirmed(new);
3312 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
3313 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
3314 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
3318 spin_unlock(&nn->client_lock);
3322 expire_client(unconf);
3328 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
3329 struct nfsd4_compound_state *cstate,
3330 union nfsd4_op_u *u)
3332 struct nfsd4_setclientid_confirm *setclientid_confirm =
3333 &u->setclientid_confirm;
3334 struct nfs4_client *conf, *unconf;
3335 struct nfs4_client *old = NULL;
3336 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
3337 clientid_t * clid = &setclientid_confirm->sc_clientid;
3339 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3341 if (STALE_CLIENTID(clid, nn))
3342 return nfserr_stale_clientid;
3344 spin_lock(&nn->client_lock);
3345 conf = find_confirmed_client(clid, false, nn);
3346 unconf = find_unconfirmed_client(clid, false, nn);
3348 * We try hard to give out unique clientid's, so if we get an
3349 * attempt to confirm the same clientid with a different cred,
3350 * the client may be buggy; this should never happen.
3352 * Nevertheless, RFC 7530 recommends INUSE for this case:
3354 status = nfserr_clid_inuse;
3355 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3357 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3359 /* cases below refer to rfc 3530 section 14.2.34: */
3360 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
3361 if (conf && same_verf(&confirm, &conf->cl_confirm)) {
3362 /* case 2: probable retransmit */
3364 } else /* case 4: client hasn't noticed we rebooted yet? */
3365 status = nfserr_stale_clientid;
3369 if (conf) { /* case 1: callback update */
3371 unhash_client_locked(old);
3372 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
3373 } else { /* case 3: normal case; new or rebooted client */
3374 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3376 status = nfserr_clid_inuse;
3377 if (client_has_state(old)
3378 && !same_creds(&unconf->cl_cred,
3381 status = mark_client_expired_locked(old);
3387 move_to_confirmed(unconf);
3390 get_client_locked(conf);
3391 spin_unlock(&nn->client_lock);
3392 nfsd4_probe_callback(conf);
3393 spin_lock(&nn->client_lock);
3394 put_client_renew_locked(conf);
3396 spin_unlock(&nn->client_lock);
3402 static struct nfs4_file *nfsd4_alloc_file(void)
3404 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3407 /* OPEN Share state helper functions */
3408 static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
3409 struct nfs4_file *fp)
3411 lockdep_assert_held(&state_lock);
3413 refcount_set(&fp->fi_ref, 1);
3414 spin_lock_init(&fp->fi_lock);
3415 INIT_LIST_HEAD(&fp->fi_stateids);
3416 INIT_LIST_HEAD(&fp->fi_delegations);
3417 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
3418 fh_copy_shallow(&fp->fi_fhandle, fh);
3419 fp->fi_deleg_file = NULL;
3420 fp->fi_had_conflict = false;
3421 fp->fi_share_deny = 0;
3422 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3423 memset(fp->fi_access, 0, sizeof(fp->fi_access));
3424 #ifdef CONFIG_NFSD_PNFS
3425 INIT_LIST_HEAD(&fp->fi_lo_states);
3426 atomic_set(&fp->fi_lo_recalls, 0);
3428 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
3432 nfsd4_free_slabs(void)
3434 kmem_cache_destroy(odstate_slab);
3435 kmem_cache_destroy(openowner_slab);
3436 kmem_cache_destroy(lockowner_slab);
3437 kmem_cache_destroy(file_slab);
3438 kmem_cache_destroy(stateid_slab);
3439 kmem_cache_destroy(deleg_slab);
3443 nfsd4_init_slabs(void)
3445 openowner_slab = kmem_cache_create("nfsd4_openowners",
3446 sizeof(struct nfs4_openowner), 0, 0, NULL);
3447 if (openowner_slab == NULL)
3449 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
3450 sizeof(struct nfs4_lockowner), 0, 0, NULL);
3451 if (lockowner_slab == NULL)
3452 goto out_free_openowner_slab;
3453 file_slab = kmem_cache_create("nfsd4_files",
3454 sizeof(struct nfs4_file), 0, 0, NULL);
3455 if (file_slab == NULL)
3456 goto out_free_lockowner_slab;
3457 stateid_slab = kmem_cache_create("nfsd4_stateids",
3458 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
3459 if (stateid_slab == NULL)
3460 goto out_free_file_slab;
3461 deleg_slab = kmem_cache_create("nfsd4_delegations",
3462 sizeof(struct nfs4_delegation), 0, 0, NULL);
3463 if (deleg_slab == NULL)
3464 goto out_free_stateid_slab;
3465 odstate_slab = kmem_cache_create("nfsd4_odstate",
3466 sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
3467 if (odstate_slab == NULL)
3468 goto out_free_deleg_slab;
3471 out_free_deleg_slab:
3472 kmem_cache_destroy(deleg_slab);
3473 out_free_stateid_slab:
3474 kmem_cache_destroy(stateid_slab);
3476 kmem_cache_destroy(file_slab);
3477 out_free_lockowner_slab:
3478 kmem_cache_destroy(lockowner_slab);
3479 out_free_openowner_slab:
3480 kmem_cache_destroy(openowner_slab);
3482 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3486 static void init_nfs4_replay(struct nfs4_replay *rp)
3488 rp->rp_status = nfserr_serverfault;
3490 rp->rp_buf = rp->rp_ibuf;
3491 mutex_init(&rp->rp_mutex);
3494 static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3495 struct nfs4_stateowner *so)
3497 if (!nfsd4_has_session(cstate)) {
3498 mutex_lock(&so->so_replay.rp_mutex);
3499 cstate->replay_owner = nfs4_get_stateowner(so);
3503 void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3505 struct nfs4_stateowner *so = cstate->replay_owner;
3508 cstate->replay_owner = NULL;
3509 mutex_unlock(&so->so_replay.rp_mutex);
3510 nfs4_put_stateowner(so);
3514 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
3516 struct nfs4_stateowner *sop;
3518 sop = kmem_cache_alloc(slab, GFP_KERNEL);
3522 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3523 if (!sop->so_owner.data) {
3524 kmem_cache_free(slab, sop);
3527 sop->so_owner.len = owner->len;
3529 INIT_LIST_HEAD(&sop->so_stateids);
3530 sop->so_client = clp;
3531 init_nfs4_replay(&sop->so_replay);
3532 atomic_set(&sop->so_count, 1);
3536 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
3538 lockdep_assert_held(&clp->cl_lock);
3540 list_add(&oo->oo_owner.so_strhash,
3541 &clp->cl_ownerstr_hashtbl[strhashval]);
3542 list_add(&oo->oo_perclient, &clp->cl_openowners);
3545 static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3547 unhash_openowner_locked(openowner(so));
3550 static void nfs4_free_openowner(struct nfs4_stateowner *so)
3552 struct nfs4_openowner *oo = openowner(so);
3554 kmem_cache_free(openowner_slab, oo);
3557 static const struct nfs4_stateowner_operations openowner_ops = {
3558 .so_unhash = nfs4_unhash_openowner,
3559 .so_free = nfs4_free_openowner,
3562 static struct nfs4_ol_stateid *
3563 nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
3565 struct nfs4_ol_stateid *local, *ret = NULL;
3566 struct nfs4_openowner *oo = open->op_openowner;
3568 lockdep_assert_held(&fp->fi_lock);
3570 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
3571 /* ignore lock owners */
3572 if (local->st_stateowner->so_is_open_owner == 0)
3574 if (local->st_stateowner != &oo->oo_owner)
3576 if (local->st_stid.sc_type == NFS4_OPEN_STID) {
3578 refcount_inc(&ret->st_stid.sc_count);
3586 nfsd4_verify_open_stid(struct nfs4_stid *s)
3588 __be32 ret = nfs_ok;
3590 switch (s->sc_type) {
3594 case NFS4_CLOSED_STID:
3595 case NFS4_CLOSED_DELEG_STID:
3596 ret = nfserr_bad_stateid;
3598 case NFS4_REVOKED_DELEG_STID:
3599 ret = nfserr_deleg_revoked;
3604 /* Lock the stateid st_mutex, and deal with races with CLOSE */
3606 nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp)
3610 mutex_lock_nested(&stp->st_mutex, LOCK_STATEID_MUTEX);
3611 ret = nfsd4_verify_open_stid(&stp->st_stid);
3613 mutex_unlock(&stp->st_mutex);
3617 static struct nfs4_ol_stateid *
3618 nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
3620 struct nfs4_ol_stateid *stp;
3622 spin_lock(&fp->fi_lock);
3623 stp = nfsd4_find_existing_open(fp, open);
3624 spin_unlock(&fp->fi_lock);
3625 if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok)
3627 nfs4_put_stid(&stp->st_stid);