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>
45 #include <linux/string_helpers.h>
49 #include "current_stateid.h"
53 #include "filecache.h"
55 #define NFSDDBG_FACILITY NFSDDBG_PROC
57 #define all_ones {{~0,~0},~0}
58 static const stateid_t one_stateid = {
60 .si_opaque = all_ones,
62 static const stateid_t zero_stateid = {
65 static const stateid_t currentstateid = {
68 static const stateid_t close_stateid = {
69 .si_generation = 0xffffffffU,
72 static u64 current_sessionid = 1;
74 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
75 #define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
76 #define CURRENT_STATEID(stateid) (!memcmp((stateid), ¤tstateid, sizeof(stateid_t)))
77 #define CLOSE_STATEID(stateid) (!memcmp((stateid), &close_stateid, sizeof(stateid_t)))
79 /* forward declarations */
80 static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
81 static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
82 void nfsd4_end_grace(struct nfsd_net *nn);
87 * Currently used for the del_recall_lru and file hash table. In an
88 * effort to decrease the scope of the client_mutex, this spinlock may
89 * eventually cover more:
91 static DEFINE_SPINLOCK(state_lock);
93 enum nfsd4_st_mutex_lock_subclass {
94 OPEN_STATEID_MUTEX = 0,
95 LOCK_STATEID_MUTEX = 1,
99 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
100 * the refcount on the open stateid to drop.
102 static DECLARE_WAIT_QUEUE_HEAD(close_wq);
105 * A waitqueue where a writer to clients/#/ctl destroying a client can
106 * wait for cl_rpc_users to drop to 0 and then for the client to be
109 static DECLARE_WAIT_QUEUE_HEAD(expiry_wq);
111 static struct kmem_cache *client_slab;
112 static struct kmem_cache *openowner_slab;
113 static struct kmem_cache *lockowner_slab;
114 static struct kmem_cache *file_slab;
115 static struct kmem_cache *stateid_slab;
116 static struct kmem_cache *deleg_slab;
117 static struct kmem_cache *odstate_slab;
119 static void free_session(struct nfsd4_session *);
121 static const struct nfsd4_callback_ops nfsd4_cb_recall_ops;
122 static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops;
124 static bool is_session_dead(struct nfsd4_session *ses)
126 return ses->se_flags & NFS4_SESSION_DEAD;
129 static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
131 if (atomic_read(&ses->se_ref) > ref_held_by_me)
132 return nfserr_jukebox;
133 ses->se_flags |= NFS4_SESSION_DEAD;
137 static bool is_client_expired(struct nfs4_client *clp)
139 return clp->cl_time == 0;
142 static __be32 get_client_locked(struct nfs4_client *clp)
144 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
146 lockdep_assert_held(&nn->client_lock);
148 if (is_client_expired(clp))
149 return nfserr_expired;
150 atomic_inc(&clp->cl_rpc_users);
154 /* must be called under the client_lock */
156 renew_client_locked(struct nfs4_client *clp)
158 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
160 if (is_client_expired(clp)) {
162 printk("%s: client (clientid %08x/%08x) already expired\n",
164 clp->cl_clientid.cl_boot,
165 clp->cl_clientid.cl_id);
169 dprintk("renewing client (clientid %08x/%08x)\n",
170 clp->cl_clientid.cl_boot,
171 clp->cl_clientid.cl_id);
172 list_move_tail(&clp->cl_lru, &nn->client_lru);
173 clp->cl_time = get_seconds();
176 static void put_client_renew_locked(struct nfs4_client *clp)
178 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
180 lockdep_assert_held(&nn->client_lock);
182 if (!atomic_dec_and_test(&clp->cl_rpc_users))
184 if (!is_client_expired(clp))
185 renew_client_locked(clp);
187 wake_up_all(&expiry_wq);
190 static void put_client_renew(struct nfs4_client *clp)
192 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
194 if (!atomic_dec_and_lock(&clp->cl_rpc_users, &nn->client_lock))
196 if (!is_client_expired(clp))
197 renew_client_locked(clp);
199 wake_up_all(&expiry_wq);
200 spin_unlock(&nn->client_lock);
203 static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
207 if (is_session_dead(ses))
208 return nfserr_badsession;
209 status = get_client_locked(ses->se_client);
212 atomic_inc(&ses->se_ref);
216 static void nfsd4_put_session_locked(struct nfsd4_session *ses)
218 struct nfs4_client *clp = ses->se_client;
219 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
221 lockdep_assert_held(&nn->client_lock);
223 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
225 put_client_renew_locked(clp);
228 static void nfsd4_put_session(struct nfsd4_session *ses)
230 struct nfs4_client *clp = ses->se_client;
231 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
233 spin_lock(&nn->client_lock);
234 nfsd4_put_session_locked(ses);
235 spin_unlock(&nn->client_lock);
238 static struct nfsd4_blocked_lock *
239 find_blocked_lock(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
242 struct nfsd4_blocked_lock *cur, *found = NULL;
244 spin_lock(&nn->blocked_locks_lock);
245 list_for_each_entry(cur, &lo->lo_blocked, nbl_list) {
246 if (fh_match(fh, &cur->nbl_fh)) {
247 list_del_init(&cur->nbl_list);
248 list_del_init(&cur->nbl_lru);
253 spin_unlock(&nn->blocked_locks_lock);
255 locks_delete_block(&found->nbl_lock);
259 static struct nfsd4_blocked_lock *
260 find_or_allocate_block(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
263 struct nfsd4_blocked_lock *nbl;
265 nbl = find_blocked_lock(lo, fh, nn);
267 nbl= kmalloc(sizeof(*nbl), GFP_KERNEL);
269 INIT_LIST_HEAD(&nbl->nbl_list);
270 INIT_LIST_HEAD(&nbl->nbl_lru);
271 fh_copy_shallow(&nbl->nbl_fh, fh);
272 locks_init_lock(&nbl->nbl_lock);
273 nfsd4_init_cb(&nbl->nbl_cb, lo->lo_owner.so_client,
274 &nfsd4_cb_notify_lock_ops,
275 NFSPROC4_CLNT_CB_NOTIFY_LOCK);
282 free_blocked_lock(struct nfsd4_blocked_lock *nbl)
284 locks_delete_block(&nbl->nbl_lock);
285 locks_release_private(&nbl->nbl_lock);
290 remove_blocked_locks(struct nfs4_lockowner *lo)
292 struct nfs4_client *clp = lo->lo_owner.so_client;
293 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
294 struct nfsd4_blocked_lock *nbl;
297 /* Dequeue all blocked locks */
298 spin_lock(&nn->blocked_locks_lock);
299 while (!list_empty(&lo->lo_blocked)) {
300 nbl = list_first_entry(&lo->lo_blocked,
301 struct nfsd4_blocked_lock,
303 list_del_init(&nbl->nbl_list);
304 list_move(&nbl->nbl_lru, &reaplist);
306 spin_unlock(&nn->blocked_locks_lock);
309 while (!list_empty(&reaplist)) {
310 nbl = list_first_entry(&reaplist, struct nfsd4_blocked_lock,
312 list_del_init(&nbl->nbl_lru);
313 free_blocked_lock(nbl);
318 nfsd4_cb_notify_lock_prepare(struct nfsd4_callback *cb)
320 struct nfsd4_blocked_lock *nbl = container_of(cb,
321 struct nfsd4_blocked_lock, nbl_cb);
322 locks_delete_block(&nbl->nbl_lock);
326 nfsd4_cb_notify_lock_done(struct nfsd4_callback *cb, struct rpc_task *task)
329 * Since this is just an optimization, we don't try very hard if it
330 * turns out not to succeed. We'll requeue it on NFS4ERR_DELAY, and
331 * just quit trying on anything else.
333 switch (task->tk_status) {
335 rpc_delay(task, 1 * HZ);
343 nfsd4_cb_notify_lock_release(struct nfsd4_callback *cb)
345 struct nfsd4_blocked_lock *nbl = container_of(cb,
346 struct nfsd4_blocked_lock, nbl_cb);
348 free_blocked_lock(nbl);
351 static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops = {
352 .prepare = nfsd4_cb_notify_lock_prepare,
353 .done = nfsd4_cb_notify_lock_done,
354 .release = nfsd4_cb_notify_lock_release,
357 static inline struct nfs4_stateowner *
358 nfs4_get_stateowner(struct nfs4_stateowner *sop)
360 atomic_inc(&sop->so_count);
365 same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
367 return (sop->so_owner.len == owner->len) &&
368 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
371 static struct nfs4_openowner *
372 find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
373 struct nfs4_client *clp)
375 struct nfs4_stateowner *so;
377 lockdep_assert_held(&clp->cl_lock);
379 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
381 if (!so->so_is_open_owner)
383 if (same_owner_str(so, &open->op_owner))
384 return openowner(nfs4_get_stateowner(so));
389 static struct nfs4_openowner *
390 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
391 struct nfs4_client *clp)
393 struct nfs4_openowner *oo;
395 spin_lock(&clp->cl_lock);
396 oo = find_openstateowner_str_locked(hashval, open, clp);
397 spin_unlock(&clp->cl_lock);
402 opaque_hashval(const void *ptr, int nbytes)
404 unsigned char *cptr = (unsigned char *) ptr;
414 static void nfsd4_free_file_rcu(struct rcu_head *rcu)
416 struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
418 kmem_cache_free(file_slab, fp);
422 put_nfs4_file(struct nfs4_file *fi)
424 might_lock(&state_lock);
426 if (refcount_dec_and_lock(&fi->fi_ref, &state_lock)) {
427 hlist_del_rcu(&fi->fi_hash);
428 spin_unlock(&state_lock);
429 WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
430 WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
431 call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
435 static struct nfsd_file *
436 __nfs4_get_fd(struct nfs4_file *f, int oflag)
438 if (f->fi_fds[oflag])
439 return nfsd_file_get(f->fi_fds[oflag]);
443 static struct nfsd_file *
444 find_writeable_file_locked(struct nfs4_file *f)
446 struct nfsd_file *ret;
448 lockdep_assert_held(&f->fi_lock);
450 ret = __nfs4_get_fd(f, O_WRONLY);
452 ret = __nfs4_get_fd(f, O_RDWR);
456 static struct nfsd_file *
457 find_writeable_file(struct nfs4_file *f)
459 struct nfsd_file *ret;
461 spin_lock(&f->fi_lock);
462 ret = find_writeable_file_locked(f);
463 spin_unlock(&f->fi_lock);
468 static struct nfsd_file *
469 find_readable_file_locked(struct nfs4_file *f)
471 struct nfsd_file *ret;
473 lockdep_assert_held(&f->fi_lock);
475 ret = __nfs4_get_fd(f, O_RDONLY);
477 ret = __nfs4_get_fd(f, O_RDWR);
481 static struct nfsd_file *
482 find_readable_file(struct nfs4_file *f)
484 struct nfsd_file *ret;
486 spin_lock(&f->fi_lock);
487 ret = find_readable_file_locked(f);
488 spin_unlock(&f->fi_lock);
494 find_any_file(struct nfs4_file *f)
496 struct nfsd_file *ret;
498 spin_lock(&f->fi_lock);
499 ret = __nfs4_get_fd(f, O_RDWR);
501 ret = __nfs4_get_fd(f, O_WRONLY);
503 ret = __nfs4_get_fd(f, O_RDONLY);
505 spin_unlock(&f->fi_lock);
509 static atomic_long_t num_delegations;
510 unsigned long max_delegations;
513 * Open owner state (share locks)
516 /* hash tables for lock and open owners */
517 #define OWNER_HASH_BITS 8
518 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
519 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
521 static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
525 ret = opaque_hashval(ownername->data, ownername->len);
526 return ret & OWNER_HASH_MASK;
529 /* hash table for nfs4_file */
530 #define FILE_HASH_BITS 8
531 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
533 static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
535 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
538 static unsigned int file_hashval(struct knfsd_fh *fh)
540 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
543 static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
546 __nfs4_file_get_access(struct nfs4_file *fp, u32 access)
548 lockdep_assert_held(&fp->fi_lock);
550 if (access & NFS4_SHARE_ACCESS_WRITE)
551 atomic_inc(&fp->fi_access[O_WRONLY]);
552 if (access & NFS4_SHARE_ACCESS_READ)
553 atomic_inc(&fp->fi_access[O_RDONLY]);
557 nfs4_file_get_access(struct nfs4_file *fp, u32 access)
559 lockdep_assert_held(&fp->fi_lock);
561 /* Does this access mode make sense? */
562 if (access & ~NFS4_SHARE_ACCESS_BOTH)
565 /* Does it conflict with a deny mode already set? */
566 if ((access & fp->fi_share_deny) != 0)
567 return nfserr_share_denied;
569 __nfs4_file_get_access(fp, access);
573 static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
575 /* Common case is that there is no deny mode. */
577 /* Does this deny mode make sense? */
578 if (deny & ~NFS4_SHARE_DENY_BOTH)
581 if ((deny & NFS4_SHARE_DENY_READ) &&
582 atomic_read(&fp->fi_access[O_RDONLY]))
583 return nfserr_share_denied;
585 if ((deny & NFS4_SHARE_DENY_WRITE) &&
586 atomic_read(&fp->fi_access[O_WRONLY]))
587 return nfserr_share_denied;
592 static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
594 might_lock(&fp->fi_lock);
596 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
597 struct nfsd_file *f1 = NULL;
598 struct nfsd_file *f2 = NULL;
600 swap(f1, fp->fi_fds[oflag]);
601 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
602 swap(f2, fp->fi_fds[O_RDWR]);
603 spin_unlock(&fp->fi_lock);
611 static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
613 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
615 if (access & NFS4_SHARE_ACCESS_WRITE)
616 __nfs4_file_put_access(fp, O_WRONLY);
617 if (access & NFS4_SHARE_ACCESS_READ)
618 __nfs4_file_put_access(fp, O_RDONLY);
622 * Allocate a new open/delegation state counter. This is needed for
623 * pNFS for proper return on close semantics.
625 * Note that we only allocate it for pNFS-enabled exports, otherwise
626 * all pointers to struct nfs4_clnt_odstate are always NULL.
628 static struct nfs4_clnt_odstate *
629 alloc_clnt_odstate(struct nfs4_client *clp)
631 struct nfs4_clnt_odstate *co;
633 co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
636 refcount_set(&co->co_odcount, 1);
642 hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
644 struct nfs4_file *fp = co->co_file;
646 lockdep_assert_held(&fp->fi_lock);
647 list_add(&co->co_perfile, &fp->fi_clnt_odstate);
651 get_clnt_odstate(struct nfs4_clnt_odstate *co)
654 refcount_inc(&co->co_odcount);
658 put_clnt_odstate(struct nfs4_clnt_odstate *co)
660 struct nfs4_file *fp;
666 if (refcount_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
667 list_del(&co->co_perfile);
668 spin_unlock(&fp->fi_lock);
670 nfsd4_return_all_file_layouts(co->co_client, fp);
671 kmem_cache_free(odstate_slab, co);
675 static struct nfs4_clnt_odstate *
676 find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
678 struct nfs4_clnt_odstate *co;
679 struct nfs4_client *cl;
686 spin_lock(&fp->fi_lock);
687 list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
688 if (co->co_client == cl) {
689 get_clnt_odstate(co);
695 hash_clnt_odstate_locked(new);
697 spin_unlock(&fp->fi_lock);
701 struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab,
702 void (*sc_free)(struct nfs4_stid *))
704 struct nfs4_stid *stid;
707 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
711 idr_preload(GFP_KERNEL);
712 spin_lock(&cl->cl_lock);
713 /* Reserving 0 for start of file in nfsdfs "states" file: */
714 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 1, 0, GFP_NOWAIT);
715 spin_unlock(&cl->cl_lock);
720 stid->sc_free = sc_free;
721 stid->sc_client = cl;
722 stid->sc_stateid.si_opaque.so_id = new_id;
723 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
724 /* Will be incremented before return to client: */
725 refcount_set(&stid->sc_count, 1);
726 spin_lock_init(&stid->sc_lock);
729 * It shouldn't be a problem to reuse an opaque stateid value.
730 * I don't think it is for 4.1. But with 4.0 I worry that, for
731 * example, a stray write retransmission could be accepted by
732 * the server when it should have been rejected. Therefore,
733 * adopt a trick from the sctp code to attempt to maximize the
734 * amount of time until an id is reused, by ensuring they always
735 * "increase" (mod INT_MAX):
739 kmem_cache_free(slab, stid);
744 * Create a unique stateid_t to represent each COPY.
746 int nfs4_init_cp_state(struct nfsd_net *nn, struct nfsd4_copy *copy)
750 idr_preload(GFP_KERNEL);
751 spin_lock(&nn->s2s_cp_lock);
752 new_id = idr_alloc_cyclic(&nn->s2s_cp_stateids, copy, 0, 0, GFP_NOWAIT);
753 spin_unlock(&nn->s2s_cp_lock);
757 copy->cp_stateid.si_opaque.so_id = new_id;
758 copy->cp_stateid.si_opaque.so_clid.cl_boot = nn->boot_time;
759 copy->cp_stateid.si_opaque.so_clid.cl_id = nn->s2s_cp_cl_id;
763 void nfs4_free_cp_state(struct nfsd4_copy *copy)
767 nn = net_generic(copy->cp_clp->net, nfsd_net_id);
768 spin_lock(&nn->s2s_cp_lock);
769 idr_remove(&nn->s2s_cp_stateids, copy->cp_stateid.si_opaque.so_id);
770 spin_unlock(&nn->s2s_cp_lock);
773 static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
775 struct nfs4_stid *stid;
777 stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid);
781 return openlockstateid(stid);
784 static void nfs4_free_deleg(struct nfs4_stid *stid)
786 kmem_cache_free(deleg_slab, stid);
787 atomic_long_dec(&num_delegations);
791 * When we recall a delegation, we should be careful not to hand it
792 * out again straight away.
793 * To ensure this we keep a pair of bloom filters ('new' and 'old')
794 * in which the filehandles of recalled delegations are "stored".
795 * If a filehandle appear in either filter, a delegation is blocked.
796 * When a delegation is recalled, the filehandle is stored in the "new"
798 * Every 30 seconds we swap the filters and clear the "new" one,
799 * unless both are empty of course.
801 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
802 * low 3 bytes as hash-table indices.
804 * 'blocked_delegations_lock', which is always taken in block_delegations(),
805 * is used to manage concurrent access. Testing does not need the lock
806 * except when swapping the two filters.
808 static DEFINE_SPINLOCK(blocked_delegations_lock);
809 static struct bloom_pair {
810 int entries, old_entries;
812 int new; /* index into 'set' */
813 DECLARE_BITMAP(set[2], 256);
814 } blocked_delegations;
816 static int delegation_blocked(struct knfsd_fh *fh)
819 struct bloom_pair *bd = &blocked_delegations;
821 if (bd->entries == 0)
823 if (seconds_since_boot() - bd->swap_time > 30) {
824 spin_lock(&blocked_delegations_lock);
825 if (seconds_since_boot() - bd->swap_time > 30) {
826 bd->entries -= bd->old_entries;
827 bd->old_entries = bd->entries;
828 memset(bd->set[bd->new], 0,
831 bd->swap_time = seconds_since_boot();
833 spin_unlock(&blocked_delegations_lock);
835 hash = jhash(&fh->fh_base, fh->fh_size, 0);
836 if (test_bit(hash&255, bd->set[0]) &&
837 test_bit((hash>>8)&255, bd->set[0]) &&
838 test_bit((hash>>16)&255, bd->set[0]))
841 if (test_bit(hash&255, bd->set[1]) &&
842 test_bit((hash>>8)&255, bd->set[1]) &&
843 test_bit((hash>>16)&255, bd->set[1]))
849 static void block_delegations(struct knfsd_fh *fh)
852 struct bloom_pair *bd = &blocked_delegations;
854 hash = jhash(&fh->fh_base, fh->fh_size, 0);
856 spin_lock(&blocked_delegations_lock);
857 __set_bit(hash&255, bd->set[bd->new]);
858 __set_bit((hash>>8)&255, bd->set[bd->new]);
859 __set_bit((hash>>16)&255, bd->set[bd->new]);
860 if (bd->entries == 0)
861 bd->swap_time = seconds_since_boot();
863 spin_unlock(&blocked_delegations_lock);
866 static struct nfs4_delegation *
867 alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
868 struct svc_fh *current_fh,
869 struct nfs4_clnt_odstate *odstate)
871 struct nfs4_delegation *dp;
874 dprintk("NFSD alloc_init_deleg\n");
875 n = atomic_long_inc_return(&num_delegations);
876 if (n < 0 || n > max_delegations)
878 if (delegation_blocked(¤t_fh->fh_handle))
880 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg));
885 * delegation seqid's are never incremented. The 4.1 special
886 * meaning of seqid 0 isn't meaningful, really, but let's avoid
887 * 0 anyway just for consistency and use 1:
889 dp->dl_stid.sc_stateid.si_generation = 1;
890 INIT_LIST_HEAD(&dp->dl_perfile);
891 INIT_LIST_HEAD(&dp->dl_perclnt);
892 INIT_LIST_HEAD(&dp->dl_recall_lru);
893 dp->dl_clnt_odstate = odstate;
894 get_clnt_odstate(odstate);
895 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
897 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
898 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
900 dp->dl_stid.sc_file = fp;
903 atomic_long_dec(&num_delegations);
908 nfs4_put_stid(struct nfs4_stid *s)
910 struct nfs4_file *fp = s->sc_file;
911 struct nfs4_client *clp = s->sc_client;
913 might_lock(&clp->cl_lock);
915 if (!refcount_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
916 wake_up_all(&close_wq);
919 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
920 spin_unlock(&clp->cl_lock);
927 nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
929 stateid_t *src = &stid->sc_stateid;
931 spin_lock(&stid->sc_lock);
932 if (unlikely(++src->si_generation == 0))
933 src->si_generation = 1;
934 memcpy(dst, src, sizeof(*dst));
935 spin_unlock(&stid->sc_lock);
938 static void put_deleg_file(struct nfs4_file *fp)
940 struct nfsd_file *nf = NULL;
942 spin_lock(&fp->fi_lock);
943 if (--fp->fi_delegees == 0)
944 swap(nf, fp->fi_deleg_file);
945 spin_unlock(&fp->fi_lock);
951 static void nfs4_unlock_deleg_lease(struct nfs4_delegation *dp)
953 struct nfs4_file *fp = dp->dl_stid.sc_file;
954 struct nfsd_file *nf = fp->fi_deleg_file;
956 WARN_ON_ONCE(!fp->fi_delegees);
958 vfs_setlease(nf->nf_file, F_UNLCK, NULL, (void **)&dp);
962 static void destroy_unhashed_deleg(struct nfs4_delegation *dp)
964 put_clnt_odstate(dp->dl_clnt_odstate);
965 nfs4_unlock_deleg_lease(dp);
966 nfs4_put_stid(&dp->dl_stid);
969 void nfs4_unhash_stid(struct nfs4_stid *s)
975 * nfs4_delegation_exists - Discover if this delegation already exists
976 * @clp: a pointer to the nfs4_client we're granting a delegation to
977 * @fp: a pointer to the nfs4_file we're granting a delegation on
980 * On success: true iff an existing delegation is found
984 nfs4_delegation_exists(struct nfs4_client *clp, struct nfs4_file *fp)
986 struct nfs4_delegation *searchdp = NULL;
987 struct nfs4_client *searchclp = NULL;
989 lockdep_assert_held(&state_lock);
990 lockdep_assert_held(&fp->fi_lock);
992 list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
993 searchclp = searchdp->dl_stid.sc_client;
994 if (clp == searchclp) {
1002 * hash_delegation_locked - Add a delegation to the appropriate lists
1003 * @dp: a pointer to the nfs4_delegation we are adding.
1004 * @fp: a pointer to the nfs4_file we're granting a delegation on
1007 * On success: NULL if the delegation was successfully hashed.
1009 * On error: -EAGAIN if one was previously granted to this
1010 * nfs4_client for this nfs4_file. Delegation is not hashed.
1015 hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
1017 struct nfs4_client *clp = dp->dl_stid.sc_client;
1019 lockdep_assert_held(&state_lock);
1020 lockdep_assert_held(&fp->fi_lock);
1022 if (nfs4_delegation_exists(clp, fp))
1024 refcount_inc(&dp->dl_stid.sc_count);
1025 dp->dl_stid.sc_type = NFS4_DELEG_STID;
1026 list_add(&dp->dl_perfile, &fp->fi_delegations);
1027 list_add(&dp->dl_perclnt, &clp->cl_delegations);
1032 unhash_delegation_locked(struct nfs4_delegation *dp)
1034 struct nfs4_file *fp = dp->dl_stid.sc_file;
1036 lockdep_assert_held(&state_lock);
1038 if (list_empty(&dp->dl_perfile))
1041 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
1042 /* Ensure that deleg break won't try to requeue it */
1044 spin_lock(&fp->fi_lock);
1045 list_del_init(&dp->dl_perclnt);
1046 list_del_init(&dp->dl_recall_lru);
1047 list_del_init(&dp->dl_perfile);
1048 spin_unlock(&fp->fi_lock);
1052 static void destroy_delegation(struct nfs4_delegation *dp)
1056 spin_lock(&state_lock);
1057 unhashed = unhash_delegation_locked(dp);
1058 spin_unlock(&state_lock);
1060 destroy_unhashed_deleg(dp);
1063 static void revoke_delegation(struct nfs4_delegation *dp)
1065 struct nfs4_client *clp = dp->dl_stid.sc_client;
1067 WARN_ON(!list_empty(&dp->dl_recall_lru));
1069 if (clp->cl_minorversion) {
1070 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
1071 refcount_inc(&dp->dl_stid.sc_count);
1072 spin_lock(&clp->cl_lock);
1073 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
1074 spin_unlock(&clp->cl_lock);
1076 destroy_unhashed_deleg(dp);
1083 static unsigned int clientid_hashval(u32 id)
1085 return id & CLIENT_HASH_MASK;
1088 static unsigned int clientstr_hashval(struct xdr_netobj name)
1090 return opaque_hashval(name.data, 8) & CLIENT_HASH_MASK;
1094 * We store the NONE, READ, WRITE, and BOTH bits separately in the
1095 * st_{access,deny}_bmap field of the stateid, in order to track not
1096 * only what share bits are currently in force, but also what
1097 * combinations of share bits previous opens have used. This allows us
1098 * to enforce the recommendation of rfc 3530 14.2.19 that the server
1099 * return an error if the client attempt to downgrade to a combination
1100 * of share bits not explicable by closing some of its previous opens.
1102 * XXX: This enforcement is actually incomplete, since we don't keep
1103 * track of access/deny bit combinations; so, e.g., we allow:
1105 * OPEN allow read, deny write
1106 * OPEN allow both, deny none
1107 * DOWNGRADE allow read, deny none
1109 * which we should reject.
1112 bmap_to_share_mode(unsigned long bmap) {
1114 unsigned int access = 0;
1116 for (i = 1; i < 4; i++) {
1117 if (test_bit(i, &bmap))
1123 /* set share access for a given stateid */
1125 set_access(u32 access, struct nfs4_ol_stateid *stp)
1127 unsigned char mask = 1 << access;
1129 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1130 stp->st_access_bmap |= mask;
1133 /* clear share access for a given stateid */
1135 clear_access(u32 access, struct nfs4_ol_stateid *stp)
1137 unsigned char mask = 1 << access;
1139 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1140 stp->st_access_bmap &= ~mask;
1143 /* test whether a given stateid has access */
1145 test_access(u32 access, struct nfs4_ol_stateid *stp)
1147 unsigned char mask = 1 << access;
1149 return (bool)(stp->st_access_bmap & mask);
1152 /* set share deny for a given stateid */
1154 set_deny(u32 deny, struct nfs4_ol_stateid *stp)
1156 unsigned char mask = 1 << deny;
1158 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1159 stp->st_deny_bmap |= mask;
1162 /* clear share deny for a given stateid */
1164 clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
1166 unsigned char mask = 1 << deny;
1168 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1169 stp->st_deny_bmap &= ~mask;
1172 /* test whether a given stateid is denying specific access */
1174 test_deny(u32 deny, struct nfs4_ol_stateid *stp)
1176 unsigned char mask = 1 << deny;
1178 return (bool)(stp->st_deny_bmap & mask);
1181 static int nfs4_access_to_omode(u32 access)
1183 switch (access & NFS4_SHARE_ACCESS_BOTH) {
1184 case NFS4_SHARE_ACCESS_READ:
1186 case NFS4_SHARE_ACCESS_WRITE:
1188 case NFS4_SHARE_ACCESS_BOTH:
1196 * A stateid that had a deny mode associated with it is being released
1197 * or downgraded. Recalculate the deny mode on the file.
1200 recalculate_deny_mode(struct nfs4_file *fp)
1202 struct nfs4_ol_stateid *stp;
1204 spin_lock(&fp->fi_lock);
1205 fp->fi_share_deny = 0;
1206 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
1207 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
1208 spin_unlock(&fp->fi_lock);
1212 reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
1215 bool change = false;
1217 for (i = 1; i < 4; i++) {
1218 if ((i & deny) != i) {
1224 /* Recalculate per-file deny mode if there was a change */
1226 recalculate_deny_mode(stp->st_stid.sc_file);
1229 /* release all access and file references for a given stateid */
1231 release_all_access(struct nfs4_ol_stateid *stp)
1234 struct nfs4_file *fp = stp->st_stid.sc_file;
1236 if (fp && stp->st_deny_bmap != 0)
1237 recalculate_deny_mode(fp);
1239 for (i = 1; i < 4; i++) {
1240 if (test_access(i, stp))
1241 nfs4_file_put_access(stp->st_stid.sc_file, i);
1242 clear_access(i, stp);
1246 static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1248 kfree(sop->so_owner.data);
1249 sop->so_ops->so_free(sop);
1252 static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1254 struct nfs4_client *clp = sop->so_client;
1256 might_lock(&clp->cl_lock);
1258 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
1260 sop->so_ops->so_unhash(sop);
1261 spin_unlock(&clp->cl_lock);
1262 nfs4_free_stateowner(sop);
1265 static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
1267 struct nfs4_file *fp = stp->st_stid.sc_file;
1269 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1271 if (list_empty(&stp->st_perfile))
1274 spin_lock(&fp->fi_lock);
1275 list_del_init(&stp->st_perfile);
1276 spin_unlock(&fp->fi_lock);
1277 list_del(&stp->st_perstateowner);
1281 static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
1283 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1285 put_clnt_odstate(stp->st_clnt_odstate);
1286 release_all_access(stp);
1287 if (stp->st_stateowner)
1288 nfs4_put_stateowner(stp->st_stateowner);
1289 kmem_cache_free(stateid_slab, stid);
1292 static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
1294 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1295 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
1296 struct nfsd_file *nf;
1298 nf = find_any_file(stp->st_stid.sc_file);
1300 get_file(nf->nf_file);
1301 filp_close(nf->nf_file, (fl_owner_t)lo);
1304 nfs4_free_ol_stateid(stid);
1308 * Put the persistent reference to an already unhashed generic stateid, while
1309 * holding the cl_lock. If it's the last reference, then put it onto the
1310 * reaplist for later destruction.
1312 static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1313 struct list_head *reaplist)
1315 struct nfs4_stid *s = &stp->st_stid;
1316 struct nfs4_client *clp = s->sc_client;
1318 lockdep_assert_held(&clp->cl_lock);
1320 WARN_ON_ONCE(!list_empty(&stp->st_locks));
1322 if (!refcount_dec_and_test(&s->sc_count)) {
1323 wake_up_all(&close_wq);
1327 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1328 list_add(&stp->st_locks, reaplist);
1331 static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
1333 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1335 list_del_init(&stp->st_locks);
1336 nfs4_unhash_stid(&stp->st_stid);
1337 return unhash_ol_stateid(stp);
1340 static void release_lock_stateid(struct nfs4_ol_stateid *stp)
1342 struct nfs4_client *clp = stp->st_stid.sc_client;
1345 spin_lock(&clp->cl_lock);
1346 unhashed = unhash_lock_stateid(stp);
1347 spin_unlock(&clp->cl_lock);
1349 nfs4_put_stid(&stp->st_stid);
1352 static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
1354 struct nfs4_client *clp = lo->lo_owner.so_client;
1356 lockdep_assert_held(&clp->cl_lock);
1358 list_del_init(&lo->lo_owner.so_strhash);
1362 * Free a list of generic stateids that were collected earlier after being
1366 free_ol_stateid_reaplist(struct list_head *reaplist)
1368 struct nfs4_ol_stateid *stp;
1369 struct nfs4_file *fp;
1373 while (!list_empty(reaplist)) {
1374 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1376 list_del(&stp->st_locks);
1377 fp = stp->st_stid.sc_file;
1378 stp->st_stid.sc_free(&stp->st_stid);
1384 static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1385 struct list_head *reaplist)
1387 struct nfs4_ol_stateid *stp;
1389 lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1391 while (!list_empty(&open_stp->st_locks)) {
1392 stp = list_entry(open_stp->st_locks.next,
1393 struct nfs4_ol_stateid, st_locks);
1394 WARN_ON(!unhash_lock_stateid(stp));
1395 put_ol_stateid_locked(stp, reaplist);
1399 static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
1400 struct list_head *reaplist)
1404 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1406 unhashed = unhash_ol_stateid(stp);
1407 release_open_stateid_locks(stp, reaplist);
1411 static void release_open_stateid(struct nfs4_ol_stateid *stp)
1413 LIST_HEAD(reaplist);
1415 spin_lock(&stp->st_stid.sc_client->cl_lock);
1416 if (unhash_open_stateid(stp, &reaplist))
1417 put_ol_stateid_locked(stp, &reaplist);
1418 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1419 free_ol_stateid_reaplist(&reaplist);
1422 static void unhash_openowner_locked(struct nfs4_openowner *oo)
1424 struct nfs4_client *clp = oo->oo_owner.so_client;
1426 lockdep_assert_held(&clp->cl_lock);
1428 list_del_init(&oo->oo_owner.so_strhash);
1429 list_del_init(&oo->oo_perclient);
1432 static void release_last_closed_stateid(struct nfs4_openowner *oo)
1434 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1436 struct nfs4_ol_stateid *s;
1438 spin_lock(&nn->client_lock);
1439 s = oo->oo_last_closed_stid;
1441 list_del_init(&oo->oo_close_lru);
1442 oo->oo_last_closed_stid = NULL;
1444 spin_unlock(&nn->client_lock);
1446 nfs4_put_stid(&s->st_stid);
1449 static void release_openowner(struct nfs4_openowner *oo)
1451 struct nfs4_ol_stateid *stp;
1452 struct nfs4_client *clp = oo->oo_owner.so_client;
1453 struct list_head reaplist;
1455 INIT_LIST_HEAD(&reaplist);
1457 spin_lock(&clp->cl_lock);
1458 unhash_openowner_locked(oo);
1459 while (!list_empty(&oo->oo_owner.so_stateids)) {
1460 stp = list_first_entry(&oo->oo_owner.so_stateids,
1461 struct nfs4_ol_stateid, st_perstateowner);
1462 if (unhash_open_stateid(stp, &reaplist))
1463 put_ol_stateid_locked(stp, &reaplist);
1465 spin_unlock(&clp->cl_lock);
1466 free_ol_stateid_reaplist(&reaplist);
1467 release_last_closed_stateid(oo);
1468 nfs4_put_stateowner(&oo->oo_owner);
1472 hash_sessionid(struct nfs4_sessionid *sessionid)
1474 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1476 return sid->sequence % SESSION_HASH_SIZE;
1479 #ifdef CONFIG_SUNRPC_DEBUG
1481 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1483 u32 *ptr = (u32 *)(&sessionid->data[0]);
1484 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1488 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1494 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1495 * won't be used for replay.
1497 void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1499 struct nfs4_stateowner *so = cstate->replay_owner;
1501 if (nfserr == nfserr_replay_me)
1504 if (!seqid_mutating_err(ntohl(nfserr))) {
1505 nfsd4_cstate_clear_replay(cstate);
1510 if (so->so_is_open_owner)
1511 release_last_closed_stateid(openowner(so));
1517 gen_sessionid(struct nfsd4_session *ses)
1519 struct nfs4_client *clp = ses->se_client;
1520 struct nfsd4_sessionid *sid;
1522 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1523 sid->clientid = clp->cl_clientid;
1524 sid->sequence = current_sessionid++;
1529 * The protocol defines ca_maxresponssize_cached to include the size of
1530 * the rpc header, but all we need to cache is the data starting after
1531 * the end of the initial SEQUENCE operation--the rest we regenerate
1532 * each time. Therefore we can advertise a ca_maxresponssize_cached
1533 * value that is the number of bytes in our cache plus a few additional
1534 * bytes. In order to stay on the safe side, and not promise more than
1535 * we can cache, those additional bytes must be the minimum possible: 24
1536 * bytes of rpc header (xid through accept state, with AUTH_NULL
1537 * verifier), 12 for the compound header (with zero-length tag), and 44
1538 * for the SEQUENCE op response:
1540 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1543 free_session_slots(struct nfsd4_session *ses)
1547 for (i = 0; i < ses->se_fchannel.maxreqs; i++) {
1548 free_svc_cred(&ses->se_slots[i]->sl_cred);
1549 kfree(ses->se_slots[i]);
1554 * We don't actually need to cache the rpc and session headers, so we
1555 * can allocate a little less for each slot:
1557 static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
1561 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1564 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1565 return size + sizeof(struct nfsd4_slot);
1569 * XXX: If we run out of reserved DRC memory we could (up to a point)
1570 * re-negotiate active sessions and reduce their slot usage to make
1571 * room for new connections. For now we just fail the create session.
1573 static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
1575 u32 slotsize = slot_bytes(ca);
1576 u32 num = ca->maxreqs;
1577 unsigned long avail, total_avail;
1578 unsigned int scale_factor;
1580 spin_lock(&nfsd_drc_lock);
1581 if (nfsd_drc_max_mem > nfsd_drc_mem_used)
1582 total_avail = nfsd_drc_max_mem - nfsd_drc_mem_used;
1584 /* We have handed out more space than we chose in
1585 * set_max_drc() to allow. That isn't really a
1586 * problem as long as that doesn't make us think we
1587 * have lots more due to integer overflow.
1590 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, total_avail);
1592 * Never use more than a fraction of the remaining memory,
1593 * unless it's the only way to give this client a slot.
1594 * The chosen fraction is either 1/8 or 1/number of threads,
1595 * whichever is smaller. This ensures there are adequate
1596 * slots to support multiple clients per thread.
1597 * Give the client one slot even if that would require
1598 * over-allocation--it is better than failure.
1600 scale_factor = max_t(unsigned int, 8, nn->nfsd_serv->sv_nrthreads);
1602 avail = clamp_t(unsigned long, avail, slotsize,
1603 total_avail/scale_factor);
1604 num = min_t(int, num, avail / slotsize);
1605 num = max_t(int, num, 1);
1606 nfsd_drc_mem_used += num * slotsize;
1607 spin_unlock(&nfsd_drc_lock);
1612 static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
1614 int slotsize = slot_bytes(ca);
1616 spin_lock(&nfsd_drc_lock);
1617 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
1618 spin_unlock(&nfsd_drc_lock);
1621 static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1622 struct nfsd4_channel_attrs *battrs)
1624 int numslots = fattrs->maxreqs;
1625 int slotsize = slot_bytes(fattrs);
1626 struct nfsd4_session *new;
1629 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
1630 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1631 mem = numslots * sizeof(struct nfsd4_slot *);
1633 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
1636 /* allocate each struct nfsd4_slot and data cache in one piece */
1637 for (i = 0; i < numslots; i++) {
1638 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
1639 if (!new->se_slots[i])
1643 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1644 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1649 kfree(new->se_slots[i]);
1654 static void free_conn(struct nfsd4_conn *c)
1656 svc_xprt_put(c->cn_xprt);
1660 static void nfsd4_conn_lost(struct svc_xpt_user *u)
1662 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1663 struct nfs4_client *clp = c->cn_session->se_client;
1665 spin_lock(&clp->cl_lock);
1666 if (!list_empty(&c->cn_persession)) {
1667 list_del(&c->cn_persession);
1670 nfsd4_probe_callback(clp);
1671 spin_unlock(&clp->cl_lock);
1674 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
1676 struct nfsd4_conn *conn;
1678 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1681 svc_xprt_get(rqstp->rq_xprt);
1682 conn->cn_xprt = rqstp->rq_xprt;
1683 conn->cn_flags = flags;
1684 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1688 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1690 conn->cn_session = ses;
1691 list_add(&conn->cn_persession, &ses->se_conns);
1694 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1696 struct nfs4_client *clp = ses->se_client;
1698 spin_lock(&clp->cl_lock);
1699 __nfsd4_hash_conn(conn, ses);
1700 spin_unlock(&clp->cl_lock);
1703 static int nfsd4_register_conn(struct nfsd4_conn *conn)
1705 conn->cn_xpt_user.callback = nfsd4_conn_lost;
1706 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
1709 static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
1713 nfsd4_hash_conn(conn, ses);
1714 ret = nfsd4_register_conn(conn);
1716 /* oops; xprt is already down: */
1717 nfsd4_conn_lost(&conn->cn_xpt_user);
1718 /* We may have gained or lost a callback channel: */
1719 nfsd4_probe_callback_sync(ses->se_client);
1722 static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
1724 u32 dir = NFS4_CDFC4_FORE;
1726 if (cses->flags & SESSION4_BACK_CHAN)
1727 dir |= NFS4_CDFC4_BACK;
1728 return alloc_conn(rqstp, dir);
1731 /* must be called under client_lock */
1732 static void nfsd4_del_conns(struct nfsd4_session *s)
1734 struct nfs4_client *clp = s->se_client;
1735 struct nfsd4_conn *c;
1737 spin_lock(&clp->cl_lock);
1738 while (!list_empty(&s->se_conns)) {
1739 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1740 list_del_init(&c->cn_persession);
1741 spin_unlock(&clp->cl_lock);
1743 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1746 spin_lock(&clp->cl_lock);
1748 spin_unlock(&clp->cl_lock);
1751 static void __free_session(struct nfsd4_session *ses)
1753 free_session_slots(ses);
1757 static void free_session(struct nfsd4_session *ses)
1759 nfsd4_del_conns(ses);
1760 nfsd4_put_drc_mem(&ses->se_fchannel);
1761 __free_session(ses);
1764 static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
1767 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1769 new->se_client = clp;
1772 INIT_LIST_HEAD(&new->se_conns);
1774 new->se_cb_seq_nr = 1;
1775 new->se_flags = cses->flags;
1776 new->se_cb_prog = cses->callback_prog;
1777 new->se_cb_sec = cses->cb_sec;
1778 atomic_set(&new->se_ref, 0);
1779 idx = hash_sessionid(&new->se_sessionid);
1780 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
1781 spin_lock(&clp->cl_lock);
1782 list_add(&new->se_perclnt, &clp->cl_sessions);
1783 spin_unlock(&clp->cl_lock);
1786 struct sockaddr *sa = svc_addr(rqstp);
1788 * This is a little silly; with sessions there's no real
1789 * use for the callback address. Use the peer address
1790 * as a reasonable default for now, but consider fixing
1791 * the rpc client not to require an address in the
1794 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1795 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
1799 /* caller must hold client_lock */
1800 static struct nfsd4_session *
1801 __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
1803 struct nfsd4_session *elem;
1805 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1807 lockdep_assert_held(&nn->client_lock);
1809 dump_sessionid(__func__, sessionid);
1810 idx = hash_sessionid(sessionid);
1811 /* Search in the appropriate list */
1812 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
1813 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1814 NFS4_MAX_SESSIONID_LEN)) {
1819 dprintk("%s: session not found\n", __func__);
1823 static struct nfsd4_session *
1824 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1827 struct nfsd4_session *session;
1828 __be32 status = nfserr_badsession;
1830 session = __find_in_sessionid_hashtbl(sessionid, net);
1833 status = nfsd4_get_session_locked(session);
1841 /* caller must hold client_lock */
1843 unhash_session(struct nfsd4_session *ses)
1845 struct nfs4_client *clp = ses->se_client;
1846 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1848 lockdep_assert_held(&nn->client_lock);
1850 list_del(&ses->se_hash);
1851 spin_lock(&ses->se_client->cl_lock);
1852 list_del(&ses->se_perclnt);
1853 spin_unlock(&ses->se_client->cl_lock);
1856 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1858 STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
1861 * We're assuming the clid was not given out from a boot
1862 * precisely 2^32 (about 136 years) before this one. That seems
1863 * a safe assumption:
1865 if (clid->cl_boot == (u32)nn->boot_time)
1867 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
1868 clid->cl_boot, clid->cl_id, nn->boot_time);
1873 * XXX Should we use a slab cache ?
1874 * This type of memory management is somewhat inefficient, but we use it
1875 * anyway since SETCLIENTID is not a common operation.
1877 static struct nfs4_client *alloc_client(struct xdr_netobj name)
1879 struct nfs4_client *clp;
1882 clp = kmem_cache_zalloc(client_slab, GFP_KERNEL);
1885 xdr_netobj_dup(&clp->cl_name, &name, GFP_KERNEL);
1886 if (clp->cl_name.data == NULL)
1888 clp->cl_ownerstr_hashtbl = kmalloc_array(OWNER_HASH_SIZE,
1889 sizeof(struct list_head),
1891 if (!clp->cl_ownerstr_hashtbl)
1892 goto err_no_hashtbl;
1893 for (i = 0; i < OWNER_HASH_SIZE; i++)
1894 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
1895 INIT_LIST_HEAD(&clp->cl_sessions);
1896 idr_init(&clp->cl_stateids);
1897 atomic_set(&clp->cl_rpc_users, 0);
1898 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1899 INIT_LIST_HEAD(&clp->cl_idhash);
1900 INIT_LIST_HEAD(&clp->cl_openowners);
1901 INIT_LIST_HEAD(&clp->cl_delegations);
1902 INIT_LIST_HEAD(&clp->cl_lru);
1903 INIT_LIST_HEAD(&clp->cl_revoked);
1904 #ifdef CONFIG_NFSD_PNFS
1905 INIT_LIST_HEAD(&clp->cl_lo_states);
1907 INIT_LIST_HEAD(&clp->async_copies);
1908 spin_lock_init(&clp->async_lock);
1909 spin_lock_init(&clp->cl_lock);
1910 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1913 kfree(clp->cl_name.data);
1915 kmem_cache_free(client_slab, clp);
1919 static void __free_client(struct kref *k)
1921 struct nfsdfs_client *c = container_of(k, struct nfsdfs_client, cl_ref);
1922 struct nfs4_client *clp = container_of(c, struct nfs4_client, cl_nfsdfs);
1924 free_svc_cred(&clp->cl_cred);
1925 kfree(clp->cl_ownerstr_hashtbl);
1926 kfree(clp->cl_name.data);
1927 kfree(clp->cl_nii_domain.data);
1928 kfree(clp->cl_nii_name.data);
1929 idr_destroy(&clp->cl_stateids);
1930 kmem_cache_free(client_slab, clp);
1933 static void drop_client(struct nfs4_client *clp)
1935 kref_put(&clp->cl_nfsdfs.cl_ref, __free_client);
1939 free_client(struct nfs4_client *clp)
1941 while (!list_empty(&clp->cl_sessions)) {
1942 struct nfsd4_session *ses;
1943 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1945 list_del(&ses->se_perclnt);
1946 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1949 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
1950 if (clp->cl_nfsd_dentry) {
1951 nfsd_client_rmdir(clp->cl_nfsd_dentry);
1952 clp->cl_nfsd_dentry = NULL;
1953 wake_up_all(&expiry_wq);
1958 /* must be called under the client_lock */
1960 unhash_client_locked(struct nfs4_client *clp)
1962 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1963 struct nfsd4_session *ses;
1965 lockdep_assert_held(&nn->client_lock);
1967 /* Mark the client as expired! */
1969 /* Make it invisible */
1970 if (!list_empty(&clp->cl_idhash)) {
1971 list_del_init(&clp->cl_idhash);
1972 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1973 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1975 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1977 list_del_init(&clp->cl_lru);
1978 spin_lock(&clp->cl_lock);
1979 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1980 list_del_init(&ses->se_hash);
1981 spin_unlock(&clp->cl_lock);
1985 unhash_client(struct nfs4_client *clp)
1987 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1989 spin_lock(&nn->client_lock);
1990 unhash_client_locked(clp);
1991 spin_unlock(&nn->client_lock);
1994 static __be32 mark_client_expired_locked(struct nfs4_client *clp)
1996 if (atomic_read(&clp->cl_rpc_users))
1997 return nfserr_jukebox;
1998 unhash_client_locked(clp);
2003 __destroy_client(struct nfs4_client *clp)
2006 struct nfs4_openowner *oo;
2007 struct nfs4_delegation *dp;
2008 struct list_head reaplist;
2010 INIT_LIST_HEAD(&reaplist);
2011 spin_lock(&state_lock);
2012 while (!list_empty(&clp->cl_delegations)) {
2013 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
2014 WARN_ON(!unhash_delegation_locked(dp));
2015 list_add(&dp->dl_recall_lru, &reaplist);
2017 spin_unlock(&state_lock);
2018 while (!list_empty(&reaplist)) {
2019 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
2020 list_del_init(&dp->dl_recall_lru);
2021 destroy_unhashed_deleg(dp);
2023 while (!list_empty(&clp->cl_revoked)) {
2024 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
2025 list_del_init(&dp->dl_recall_lru);
2026 nfs4_put_stid(&dp->dl_stid);
2028 while (!list_empty(&clp->cl_openowners)) {
2029 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
2030 nfs4_get_stateowner(&oo->oo_owner);
2031 release_openowner(oo);
2033 for (i = 0; i < OWNER_HASH_SIZE; i++) {
2034 struct nfs4_stateowner *so, *tmp;
2036 list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i],
2038 /* Should be no openowners at this point */
2039 WARN_ON_ONCE(so->so_is_open_owner);
2040 remove_blocked_locks(lockowner(so));
2043 nfsd4_return_all_client_layouts(clp);
2044 nfsd4_shutdown_copy(clp);
2045 nfsd4_shutdown_callback(clp);
2046 if (clp->cl_cb_conn.cb_xprt)
2047 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
2049 wake_up_all(&expiry_wq);
2053 destroy_client(struct nfs4_client *clp)
2056 __destroy_client(clp);
2059 static void inc_reclaim_complete(struct nfs4_client *clp)
2061 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2063 if (!nn->track_reclaim_completes)
2065 if (!nfsd4_find_reclaim_client(clp->cl_name, nn))
2067 if (atomic_inc_return(&nn->nr_reclaim_complete) ==
2068 nn->reclaim_str_hashtbl_size) {
2069 printk(KERN_INFO "NFSD: all clients done reclaiming, ending NFSv4 grace period (net %x)\n",
2071 nfsd4_end_grace(nn);
2075 static void expire_client(struct nfs4_client *clp)
2078 nfsd4_client_record_remove(clp);
2079 __destroy_client(clp);
2082 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
2084 memcpy(target->cl_verifier.data, source->data,
2085 sizeof(target->cl_verifier.data));
2088 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
2090 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
2091 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
2094 static int copy_cred(struct svc_cred *target, struct svc_cred *source)
2096 target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL);
2097 target->cr_raw_principal = kstrdup(source->cr_raw_principal,
2099 target->cr_targ_princ = kstrdup(source->cr_targ_princ, GFP_KERNEL);
2100 if ((source->cr_principal && !target->cr_principal) ||
2101 (source->cr_raw_principal && !target->cr_raw_principal) ||
2102 (source->cr_targ_princ && !target->cr_targ_princ))
2105 target->cr_flavor = source->cr_flavor;
2106 target->cr_uid = source->cr_uid;
2107 target->cr_gid = source->cr_gid;
2108 target->cr_group_info = source->cr_group_info;
2109 get_group_info(target->cr_group_info);
2110 target->cr_gss_mech = source->cr_gss_mech;
2111 if (source->cr_gss_mech)
2112 gss_mech_get(source->cr_gss_mech);
2117 compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
2119 if (o1->len < o2->len)
2121 if (o1->len > o2->len)
2123 return memcmp(o1->data, o2->data, o1->len);
2127 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
2129 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
2133 same_clid(clientid_t *cl1, clientid_t *cl2)
2135 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
2138 static bool groups_equal(struct group_info *g1, struct group_info *g2)
2142 if (g1->ngroups != g2->ngroups)
2144 for (i=0; i<g1->ngroups; i++)
2145 if (!gid_eq(g1->gid[i], g2->gid[i]))
2151 * RFC 3530 language requires clid_inuse be returned when the
2152 * "principal" associated with a requests differs from that previously
2153 * used. We use uid, gid's, and gss principal string as our best
2154 * approximation. We also don't want to allow non-gss use of a client
2155 * established using gss: in theory cr_principal should catch that
2156 * change, but in practice cr_principal can be null even in the gss case
2157 * since gssd doesn't always pass down a principal string.
2159 static bool is_gss_cred(struct svc_cred *cr)
2161 /* Is cr_flavor one of the gss "pseudoflavors"?: */
2162 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
2167 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
2169 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
2170 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
2171 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
2172 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
2174 /* XXX: check that cr_targ_princ fields match ? */
2175 if (cr1->cr_principal == cr2->cr_principal)
2177 if (!cr1->cr_principal || !cr2->cr_principal)
2179 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
2182 static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
2184 struct svc_cred *cr = &rqstp->rq_cred;
2187 if (!cr->cr_gss_mech)
2189 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
2190 return service == RPC_GSS_SVC_INTEGRITY ||
2191 service == RPC_GSS_SVC_PRIVACY;
2194 bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
2196 struct svc_cred *cr = &rqstp->rq_cred;
2198 if (!cl->cl_mach_cred)
2200 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
2202 if (!svc_rqst_integrity_protected(rqstp))
2204 if (cl->cl_cred.cr_raw_principal)
2205 return 0 == strcmp(cl->cl_cred.cr_raw_principal,
2206 cr->cr_raw_principal);
2207 if (!cr->cr_principal)
2209 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
2212 static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
2217 * This is opaque to client, so no need to byte-swap. Use
2218 * __force to keep sparse happy
2220 verf[0] = (__force __be32)get_seconds();
2221 verf[1] = (__force __be32)nn->clverifier_counter++;
2222 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
2225 static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
2227 clp->cl_clientid.cl_boot = nn->boot_time;
2228 clp->cl_clientid.cl_id = nn->clientid_counter++;
2229 gen_confirm(clp, nn);
2232 static struct nfs4_stid *
2233 find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
2235 struct nfs4_stid *ret;
2237 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
2238 if (!ret || !ret->sc_type)
2243 static struct nfs4_stid *
2244 find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
2246 struct nfs4_stid *s;
2248 spin_lock(&cl->cl_lock);
2249 s = find_stateid_locked(cl, t);
2251 if (typemask & s->sc_type)
2252 refcount_inc(&s->sc_count);
2256 spin_unlock(&cl->cl_lock);
2260 static struct nfs4_client *get_nfsdfs_clp(struct inode *inode)
2262 struct nfsdfs_client *nc;
2263 nc = get_nfsdfs_client(inode);
2266 return container_of(nc, struct nfs4_client, cl_nfsdfs);
2269 static void seq_quote_mem(struct seq_file *m, char *data, int len)
2271 seq_printf(m, "\"");
2272 seq_escape_mem_ascii(m, data, len);
2273 seq_printf(m, "\"");
2276 static int client_info_show(struct seq_file *m, void *v)
2278 struct inode *inode = m->private;
2279 struct nfs4_client *clp;
2282 clp = get_nfsdfs_clp(inode);
2285 memcpy(&clid, &clp->cl_clientid, sizeof(clid));
2286 seq_printf(m, "clientid: 0x%llx\n", clid);
2287 seq_printf(m, "address: \"%pISpc\"\n", (struct sockaddr *)&clp->cl_addr);
2288 seq_printf(m, "name: ");
2289 seq_quote_mem(m, clp->cl_name.data, clp->cl_name.len);
2290 seq_printf(m, "\nminor version: %d\n", clp->cl_minorversion);
2291 if (clp->cl_nii_domain.data) {
2292 seq_printf(m, "Implementation domain: ");
2293 seq_quote_mem(m, clp->cl_nii_domain.data,
2294 clp->cl_nii_domain.len);
2295 seq_printf(m, "\nImplementation name: ");
2296 seq_quote_mem(m, clp->cl_nii_name.data, clp->cl_nii_name.len);
2297 seq_printf(m, "\nImplementation time: [%ld, %ld]\n",
2298 clp->cl_nii_time.tv_sec, clp->cl_nii_time.tv_nsec);
2305 static int client_info_open(struct inode *inode, struct file *file)
2307 return single_open(file, client_info_show, inode);
2310 static const struct file_operations client_info_fops = {
2311 .open = client_info_open,
2313 .llseek = seq_lseek,
2314 .release = single_release,
2317 static void *states_start(struct seq_file *s, loff_t *pos)
2318 __acquires(&clp->cl_lock)
2320 struct nfs4_client *clp = s->private;
2321 unsigned long id = *pos;
2324 spin_lock(&clp->cl_lock);
2325 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2330 static void *states_next(struct seq_file *s, void *v, loff_t *pos)
2332 struct nfs4_client *clp = s->private;
2333 unsigned long id = *pos;
2338 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2343 static void states_stop(struct seq_file *s, void *v)
2344 __releases(&clp->cl_lock)
2346 struct nfs4_client *clp = s->private;
2348 spin_unlock(&clp->cl_lock);
2351 static void nfs4_show_superblock(struct seq_file *s, struct nfsd_file *f)
2353 struct inode *inode = f->nf_inode;
2355 seq_printf(s, "superblock: \"%02x:%02x:%ld\"",
2356 MAJOR(inode->i_sb->s_dev),
2357 MINOR(inode->i_sb->s_dev),
2361 static void nfs4_show_owner(struct seq_file *s, struct nfs4_stateowner *oo)
2363 seq_printf(s, "owner: ");
2364 seq_quote_mem(s, oo->so_owner.data, oo->so_owner.len);
2367 static int nfs4_show_open(struct seq_file *s, struct nfs4_stid *st)
2369 struct nfs4_ol_stateid *ols;
2370 struct nfs4_file *nf;
2371 struct nfsd_file *file;
2372 struct nfs4_stateowner *oo;
2373 unsigned int access, deny;
2375 if (st->sc_type != NFS4_OPEN_STID && st->sc_type != NFS4_LOCK_STID)
2376 return 0; /* XXX: or SEQ_SKIP? */
2377 ols = openlockstateid(st);
2378 oo = ols->st_stateowner;
2380 file = find_any_file(nf);
2382 seq_printf(s, "- 0x%16phN: { type: open, ", &st->sc_stateid);
2384 access = bmap_to_share_mode(ols->st_access_bmap);
2385 deny = bmap_to_share_mode(ols->st_deny_bmap);
2387 seq_printf(s, "access: \%s\%s, ",
2388 access & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2389 access & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2390 seq_printf(s, "deny: \%s\%s, ",
2391 deny & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2392 deny & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2394 nfs4_show_superblock(s, file);
2395 seq_printf(s, ", ");
2396 nfs4_show_owner(s, oo);
2397 seq_printf(s, " }\n");
2398 nfsd_file_put(file);
2403 static int nfs4_show_lock(struct seq_file *s, struct nfs4_stid *st)
2405 struct nfs4_ol_stateid *ols;
2406 struct nfs4_file *nf;
2407 struct nfsd_file *file;
2408 struct nfs4_stateowner *oo;
2410 ols = openlockstateid(st);
2411 oo = ols->st_stateowner;
2413 file = find_any_file(nf);
2415 seq_printf(s, "- 0x%16phN: { type: lock, ", &st->sc_stateid);
2418 * Note: a lock stateid isn't really the same thing as a lock,
2419 * it's the locking state held by one owner on a file, and there
2420 * may be multiple (or no) lock ranges associated with it.
2421 * (Same for the matter is true of open stateids.)
2424 nfs4_show_superblock(s, file);
2425 /* XXX: open stateid? */
2426 seq_printf(s, ", ");
2427 nfs4_show_owner(s, oo);
2428 seq_printf(s, " }\n");
2429 nfsd_file_put(file);
2434 static int nfs4_show_deleg(struct seq_file *s, struct nfs4_stid *st)
2436 struct nfs4_delegation *ds;
2437 struct nfs4_file *nf;
2438 struct nfsd_file *file;
2440 ds = delegstateid(st);
2442 file = nf->fi_deleg_file;
2444 seq_printf(s, "- 0x%16phN: { type: deleg, ", &st->sc_stateid);
2446 /* Kinda dead code as long as we only support read delegs: */
2447 seq_printf(s, "access: %s, ",
2448 ds->dl_type == NFS4_OPEN_DELEGATE_READ ? "r" : "w");
2450 /* XXX: lease time, whether it's being recalled. */
2452 nfs4_show_superblock(s, file);
2453 seq_printf(s, " }\n");
2458 static int nfs4_show_layout(struct seq_file *s, struct nfs4_stid *st)
2460 struct nfs4_layout_stateid *ls;
2461 struct nfsd_file *file;
2463 ls = container_of(st, struct nfs4_layout_stateid, ls_stid);
2466 seq_printf(s, "- 0x%16phN: { type: layout, ", &st->sc_stateid);
2468 /* XXX: What else would be useful? */
2470 nfs4_show_superblock(s, file);
2471 seq_printf(s, " }\n");
2476 static int states_show(struct seq_file *s, void *v)
2478 struct nfs4_stid *st = v;
2480 switch (st->sc_type) {
2481 case NFS4_OPEN_STID:
2482 return nfs4_show_open(s, st);
2483 case NFS4_LOCK_STID:
2484 return nfs4_show_lock(s, st);
2485 case NFS4_DELEG_STID:
2486 return nfs4_show_deleg(s, st);
2487 case NFS4_LAYOUT_STID:
2488 return nfs4_show_layout(s, st);
2490 return 0; /* XXX: or SEQ_SKIP? */
2492 /* XXX: copy stateids? */
2495 static struct seq_operations states_seq_ops = {
2496 .start = states_start,
2497 .next = states_next,
2498 .stop = states_stop,
2502 static int client_states_open(struct inode *inode, struct file *file)
2505 struct nfs4_client *clp;
2508 clp = get_nfsdfs_clp(inode);
2512 ret = seq_open(file, &states_seq_ops);
2515 s = file->private_data;
2520 static int client_opens_release(struct inode *inode, struct file *file)
2522 struct seq_file *m = file->private_data;
2523 struct nfs4_client *clp = m->private;
2525 /* XXX: alternatively, we could get/drop in seq start/stop */
2530 static const struct file_operations client_states_fops = {
2531 .open = client_states_open,
2533 .llseek = seq_lseek,
2534 .release = client_opens_release,
2538 * Normally we refuse to destroy clients that are in use, but here the
2539 * administrator is telling us to just do it. We also want to wait
2540 * so the caller has a guarantee that the client's locks are gone by
2541 * the time the write returns:
2543 static void force_expire_client(struct nfs4_client *clp)
2545 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2546 bool already_expired;
2548 spin_lock(&clp->cl_lock);
2550 spin_unlock(&clp->cl_lock);
2552 wait_event(expiry_wq, atomic_read(&clp->cl_rpc_users) == 0);
2553 spin_lock(&nn->client_lock);
2554 already_expired = list_empty(&clp->cl_lru);
2555 if (!already_expired)
2556 unhash_client_locked(clp);
2557 spin_unlock(&nn->client_lock);
2559 if (!already_expired)
2562 wait_event(expiry_wq, clp->cl_nfsd_dentry == NULL);
2565 static ssize_t client_ctl_write(struct file *file, const char __user *buf,
2566 size_t size, loff_t *pos)
2569 struct nfs4_client *clp;
2571 data = simple_transaction_get(file, buf, size);
2573 return PTR_ERR(data);
2574 if (size != 7 || 0 != memcmp(data, "expire\n", 7))
2576 clp = get_nfsdfs_clp(file_inode(file));
2579 force_expire_client(clp);
2584 static const struct file_operations client_ctl_fops = {
2585 .write = client_ctl_write,
2586 .release = simple_transaction_release,
2589 static const struct tree_descr client_files[] = {
2590 [0] = {"info", &client_info_fops, S_IRUSR},
2591 [1] = {"states", &client_states_fops, S_IRUSR},
2592 [2] = {"ctl", &client_ctl_fops, S_IRUSR|S_IWUSR},
2596 static struct nfs4_client *create_client(struct xdr_netobj name,
2597 struct svc_rqst *rqstp, nfs4_verifier *verf)
2599 struct nfs4_client *clp;
2600 struct sockaddr *sa = svc_addr(rqstp);
2602 struct net *net = SVC_NET(rqstp);
2603 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2605 clp = alloc_client(name);
2609 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
2615 kref_init(&clp->cl_nfsdfs.cl_ref);
2616 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
2617 clp->cl_time = get_seconds();
2618 clear_bit(0, &clp->cl_cb_slot_busy);
2619 copy_verf(clp, verf);
2620 memcpy(&clp->cl_addr, sa, sizeof(struct sockaddr_storage));
2621 clp->cl_cb_session = NULL;
2623 clp->cl_nfsd_dentry = nfsd_client_mkdir(nn, &clp->cl_nfsdfs,
2624 clp->cl_clientid.cl_id - nn->clientid_base,
2626 if (!clp->cl_nfsd_dentry) {
2634 add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
2636 struct rb_node **new = &(root->rb_node), *parent = NULL;
2637 struct nfs4_client *clp;
2640 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
2643 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
2644 new = &((*new)->rb_left);
2646 new = &((*new)->rb_right);
2649 rb_link_node(&new_clp->cl_namenode, parent, new);
2650 rb_insert_color(&new_clp->cl_namenode, root);
2653 static struct nfs4_client *
2654 find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2657 struct rb_node *node = root->rb_node;
2658 struct nfs4_client *clp;
2661 clp = rb_entry(node, struct nfs4_client, cl_namenode);
2662 cmp = compare_blob(&clp->cl_name, name);
2664 node = node->rb_left;
2666 node = node->rb_right;
2674 add_to_unconfirmed(struct nfs4_client *clp)
2676 unsigned int idhashval;
2677 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2679 lockdep_assert_held(&nn->client_lock);
2681 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2682 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
2683 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
2684 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
2685 renew_client_locked(clp);
2689 move_to_confirmed(struct nfs4_client *clp)
2691 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
2692 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2694 lockdep_assert_held(&nn->client_lock);
2696 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
2697 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
2698 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
2699 add_clp_to_name_tree(clp, &nn->conf_name_tree);
2700 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2701 renew_client_locked(clp);
2704 static struct nfs4_client *
2705 find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
2707 struct nfs4_client *clp;
2708 unsigned int idhashval = clientid_hashval(clid->cl_id);
2710 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
2711 if (same_clid(&clp->cl_clientid, clid)) {
2712 if ((bool)clp->cl_minorversion != sessions)
2714 renew_client_locked(clp);
2721 static struct nfs4_client *
2722 find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2724 struct list_head *tbl = nn->conf_id_hashtbl;
2726 lockdep_assert_held(&nn->client_lock);
2727 return find_client_in_id_table(tbl, clid, sessions);
2730 static struct nfs4_client *
2731 find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2733 struct list_head *tbl = nn->unconf_id_hashtbl;
2735 lockdep_assert_held(&nn->client_lock);
2736 return find_client_in_id_table(tbl, clid, sessions);
2739 static bool clp_used_exchangeid(struct nfs4_client *clp)
2741 return clp->cl_exchange_flags != 0;
2744 static struct nfs4_client *
2745 find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
2747 lockdep_assert_held(&nn->client_lock);
2748 return find_clp_in_name_tree(name, &nn->conf_name_tree);
2751 static struct nfs4_client *
2752 find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
2754 lockdep_assert_held(&nn->client_lock);
2755 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
2759 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
2761 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
2762 struct sockaddr *sa = svc_addr(rqstp);
2763 u32 scopeid = rpc_get_scope_id(sa);
2764 unsigned short expected_family;
2766 /* Currently, we only support tcp and tcp6 for the callback channel */
2767 if (se->se_callback_netid_len == 3 &&
2768 !memcmp(se->se_callback_netid_val, "tcp", 3))
2769 expected_family = AF_INET;
2770 else if (se->se_callback_netid_len == 4 &&
2771 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2772 expected_family = AF_INET6;
2776 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
2777 se->se_callback_addr_len,
2778 (struct sockaddr *)&conn->cb_addr,
2779 sizeof(conn->cb_addr));
2781 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
2784 if (conn->cb_addr.ss_family == AF_INET6)
2785 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
2787 conn->cb_prog = se->se_callback_prog;
2788 conn->cb_ident = se->se_callback_ident;
2789 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
2792 conn->cb_addr.ss_family = AF_UNSPEC;
2793 conn->cb_addrlen = 0;
2794 dprintk("NFSD: this client (clientid %08x/%08x) "
2795 "will not receive delegations\n",
2796 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2802 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
2805 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2807 struct xdr_buf *buf = resp->xdr.buf;
2808 struct nfsd4_slot *slot = resp->cstate.slot;
2811 dprintk("--> %s slot %p\n", __func__, slot);
2813 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
2814 slot->sl_opcnt = resp->opcnt;
2815 slot->sl_status = resp->cstate.status;
2816 free_svc_cred(&slot->sl_cred);
2817 copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred);
2819 if (!nfsd4_cache_this(resp)) {
2820 slot->sl_flags &= ~NFSD4_SLOT_CACHED;
2823 slot->sl_flags |= NFSD4_SLOT_CACHED;
2825 base = resp->cstate.data_offset;
2826 slot->sl_datalen = buf->len - base;
2827 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
2828 WARN(1, "%s: sessions DRC could not cache compound\n",
2834 * Encode the replay sequence operation from the slot values.
2835 * If cachethis is FALSE encode the uncached rep error on the next
2836 * operation which sets resp->p and increments resp->opcnt for
2837 * nfs4svc_encode_compoundres.
2841 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2842 struct nfsd4_compoundres *resp)
2844 struct nfsd4_op *op;
2845 struct nfsd4_slot *slot = resp->cstate.slot;
2847 /* Encode the replayed sequence operation */
2848 op = &args->ops[resp->opcnt - 1];
2849 nfsd4_encode_operation(resp, op);
2851 if (slot->sl_flags & NFSD4_SLOT_CACHED)
2853 if (args->opcnt == 1) {
2855 * The original operation wasn't a solo sequence--we
2856 * always cache those--so this retry must not match the
2859 op->status = nfserr_seq_false_retry;
2861 op = &args->ops[resp->opcnt++];
2862 op->status = nfserr_retry_uncached_rep;
2863 nfsd4_encode_operation(resp, op);
2869 * The sequence operation is not cached because we can use the slot and
2873 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2874 struct nfsd4_sequence *seq)
2876 struct nfsd4_slot *slot = resp->cstate.slot;
2877 struct xdr_stream *xdr = &resp->xdr;
2881 dprintk("--> %s slot %p\n", __func__, slot);
2883 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
2887 p = xdr_reserve_space(xdr, slot->sl_datalen);
2890 return nfserr_serverfault;
2892 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2893 xdr_commit_encode(xdr);
2895 resp->opcnt = slot->sl_opcnt;
2896 return slot->sl_status;
2900 * Set the exchange_id flags returned by the server.
2903 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2905 #ifdef CONFIG_NFSD_PNFS
2906 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
2908 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
2911 /* Referrals are supported, Migration is not. */
2912 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2914 /* set the wire flags to return to client. */
2915 clid->flags = new->cl_exchange_flags;
2918 static bool client_has_openowners(struct nfs4_client *clp)
2920 struct nfs4_openowner *oo;
2922 list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
2923 if (!list_empty(&oo->oo_owner.so_stateids))
2929 static bool client_has_state(struct nfs4_client *clp)
2931 return client_has_openowners(clp)
2932 #ifdef CONFIG_NFSD_PNFS
2933 || !list_empty(&clp->cl_lo_states)
2935 || !list_empty(&clp->cl_delegations)
2936 || !list_empty(&clp->cl_sessions)
2937 || !list_empty(&clp->async_copies);
2940 static __be32 copy_impl_id(struct nfs4_client *clp,
2941 struct nfsd4_exchange_id *exid)
2943 if (!exid->nii_domain.data)
2945 xdr_netobj_dup(&clp->cl_nii_domain, &exid->nii_domain, GFP_KERNEL);
2946 if (!clp->cl_nii_domain.data)
2947 return nfserr_jukebox;
2948 xdr_netobj_dup(&clp->cl_nii_name, &exid->nii_name, GFP_KERNEL);
2949 if (!clp->cl_nii_name.data)
2950 return nfserr_jukebox;
2951 clp->cl_nii_time.tv_sec = exid->nii_time.tv_sec;
2952 clp->cl_nii_time.tv_nsec = exid->nii_time.tv_nsec;
2957 nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2958 union nfsd4_op_u *u)
2960 struct nfsd4_exchange_id *exid = &u->exchange_id;
2961 struct nfs4_client *conf, *new;
2962 struct nfs4_client *unconf = NULL;
2964 char addr_str[INET6_ADDRSTRLEN];
2965 nfs4_verifier verf = exid->verifier;
2966 struct sockaddr *sa = svc_addr(rqstp);
2967 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
2968 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2970 rpc_ntop(sa, addr_str, sizeof(addr_str));
2971 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
2972 "ip_addr=%s flags %x, spa_how %d\n",
2973 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
2974 addr_str, exid->flags, exid->spa_how);
2976 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
2977 return nfserr_inval;
2979 new = create_client(exid->clname, rqstp, &verf);
2981 return nfserr_jukebox;
2982 status = copy_impl_id(new, exid);
2986 switch (exid->spa_how) {
2988 exid->spo_must_enforce[0] = 0;
2989 exid->spo_must_enforce[1] = (
2990 1 << (OP_BIND_CONN_TO_SESSION - 32) |
2991 1 << (OP_EXCHANGE_ID - 32) |
2992 1 << (OP_CREATE_SESSION - 32) |
2993 1 << (OP_DESTROY_SESSION - 32) |
2994 1 << (OP_DESTROY_CLIENTID - 32));
2996 exid->spo_must_allow[0] &= (1 << (OP_CLOSE) |
2997 1 << (OP_OPEN_DOWNGRADE) |
2999 1 << (OP_DELEGRETURN));
3001 exid->spo_must_allow[1] &= (
3002 1 << (OP_TEST_STATEID - 32) |
3003 1 << (OP_FREE_STATEID - 32));
3004 if (!svc_rqst_integrity_protected(rqstp)) {
3005 status = nfserr_inval;
3009 * Sometimes userspace doesn't give us a principal.
3010 * Which is a bug, really. Anyway, we can't enforce
3011 * MACH_CRED in that case, better to give up now:
3013 if (!new->cl_cred.cr_principal &&
3014 !new->cl_cred.cr_raw_principal) {
3015 status = nfserr_serverfault;
3018 new->cl_mach_cred = true;
3021 default: /* checked by xdr code */
3025 status = nfserr_encr_alg_unsupp;
3029 /* Cases below refer to rfc 5661 section 18.35.4: */
3030 spin_lock(&nn->client_lock);
3031 conf = find_confirmed_client_by_name(&exid->clname, nn);
3033 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
3034 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
3037 if (!clp_used_exchangeid(conf)) { /* buggy client */
3038 status = nfserr_inval;
3041 if (!nfsd4_mach_creds_match(conf, rqstp)) {
3042 status = nfserr_wrong_cred;
3045 if (!creds_match) { /* case 9 */
3046 status = nfserr_perm;
3049 if (!verfs_match) { /* case 8 */
3050 status = nfserr_not_same;
3054 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
3057 if (!creds_match) { /* case 3 */
3058 if (client_has_state(conf)) {
3059 status = nfserr_clid_inuse;
3064 if (verfs_match) { /* case 2 */
3065 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
3068 /* case 5, client reboot */
3073 if (update) { /* case 7 */
3074 status = nfserr_noent;
3078 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
3079 if (unconf) /* case 4, possible retry or client restart */
3080 unhash_client_locked(unconf);
3082 /* case 1 (normal case) */
3085 status = mark_client_expired_locked(conf);
3089 new->cl_minorversion = cstate->minorversion;
3090 new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0];
3091 new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1];
3093 add_to_unconfirmed(new);
3096 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
3097 exid->clientid.cl_id = conf->cl_clientid.cl_id;
3099 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
3100 nfsd4_set_ex_flags(conf, exid);
3102 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
3103 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
3107 spin_unlock(&nn->client_lock);
3112 expire_client(unconf);
3117 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
3119 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
3122 /* The slot is in use, and no response has been sent. */
3124 if (seqid == slot_seqid)
3125 return nfserr_jukebox;
3127 return nfserr_seq_misordered;
3129 /* Note unsigned 32-bit arithmetic handles wraparound: */
3130 if (likely(seqid == slot_seqid + 1))
3132 if (seqid == slot_seqid)
3133 return nfserr_replay_cache;
3134 return nfserr_seq_misordered;
3138 * Cache the create session result into the create session single DRC
3139 * slot cache by saving the xdr structure. sl_seqid has been set.
3140 * Do this for solo or embedded create session operations.
3143 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
3144 struct nfsd4_clid_slot *slot, __be32 nfserr)
3146 slot->sl_status = nfserr;
3147 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
3151 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
3152 struct nfsd4_clid_slot *slot)
3154 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
3155 return slot->sl_status;
3158 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
3159 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
3160 1 + /* MIN tag is length with zero, only length */ \
3161 3 + /* version, opcount, opcode */ \
3162 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3163 /* seqid, slotID, slotID, cache */ \
3164 4 ) * sizeof(__be32))
3166 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
3167 2 + /* verifier: AUTH_NULL, length 0 */\
3169 1 + /* MIN tag is length with zero, only length */ \
3170 3 + /* opcount, opcode, opstatus*/ \
3171 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3172 /* seqid, slotID, slotID, slotID, status */ \
3173 5 ) * sizeof(__be32))
3175 static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
3177 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
3179 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
3180 return nfserr_toosmall;
3181 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
3182 return nfserr_toosmall;
3183 ca->headerpadsz = 0;
3184 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
3185 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
3186 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
3187 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
3188 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
3189 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
3191 * Note decreasing slot size below client's request may make it
3192 * difficult for client to function correctly, whereas
3193 * decreasing the number of slots will (just?) affect
3194 * performance. When short on memory we therefore prefer to
3195 * decrease number of slots instead of their size. Clients that
3196 * request larger slots than they need will get poor results:
3197 * Note that we always allow at least one slot, because our
3198 * accounting is soft and provides no guarantees either way.
3200 ca->maxreqs = nfsd4_get_drc_mem(ca, nn);
3206 * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
3207 * These are based on similar macros in linux/sunrpc/msg_prot.h .
3209 #define RPC_MAX_HEADER_WITH_AUTH_SYS \
3210 (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
3212 #define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
3213 (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
3215 #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
3216 RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
3217 #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
3218 RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
3221 static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
3223 ca->headerpadsz = 0;
3225 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
3226 return nfserr_toosmall;
3227 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
3228 return nfserr_toosmall;
3229 ca->maxresp_cached = 0;
3231 return nfserr_toosmall;
3236 static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
3238 switch (cbs->flavor) {
3244 * GSS case: the spec doesn't allow us to return this
3245 * error. But it also doesn't allow us not to support
3247 * I'd rather this fail hard than return some error the
3248 * client might think it can already handle:
3250 return nfserr_encr_alg_unsupp;
3255 nfsd4_create_session(struct svc_rqst *rqstp,
3256 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
3258 struct nfsd4_create_session *cr_ses = &u->create_session;
3259 struct sockaddr *sa = svc_addr(rqstp);
3260 struct nfs4_client *conf, *unconf;
3261 struct nfs4_client *old = NULL;
3262 struct nfsd4_session *new;
3263 struct nfsd4_conn *conn;
3264 struct nfsd4_clid_slot *cs_slot = NULL;
3266 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3268 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
3269 return nfserr_inval;
3270 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
3273 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
3276 status = check_backchannel_attrs(&cr_ses->back_channel);
3278 goto out_release_drc_mem;
3279 status = nfserr_jukebox;
3280 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
3282 goto out_release_drc_mem;
3283 conn = alloc_conn_from_crses(rqstp, cr_ses);
3285 goto out_free_session;
3287 spin_lock(&nn->client_lock);
3288 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
3289 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
3290 WARN_ON_ONCE(conf && unconf);
3293 status = nfserr_wrong_cred;
3294 if (!nfsd4_mach_creds_match(conf, rqstp))
3296 cs_slot = &conf->cl_cs_slot;
3297 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
3299 if (status == nfserr_replay_cache)
3300 status = nfsd4_replay_create_session(cr_ses, cs_slot);
3303 } else if (unconf) {
3304 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
3305 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
3306 status = nfserr_clid_inuse;
3309 status = nfserr_wrong_cred;
3310 if (!nfsd4_mach_creds_match(unconf, rqstp))
3312 cs_slot = &unconf->cl_cs_slot;
3313 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
3315 /* an unconfirmed replay returns misordered */
3316 status = nfserr_seq_misordered;
3319 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3321 status = mark_client_expired_locked(old);
3327 move_to_confirmed(unconf);
3330 status = nfserr_stale_clientid;
3334 /* Persistent sessions are not supported */
3335 cr_ses->flags &= ~SESSION4_PERSIST;
3336 /* Upshifting from TCP to RDMA is not supported */
3337 cr_ses->flags &= ~SESSION4_RDMA;
3339 init_session(rqstp, new, conf, cr_ses);
3340 nfsd4_get_session_locked(new);
3342 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
3343 NFS4_MAX_SESSIONID_LEN);
3344 cs_slot->sl_seqid++;
3345 cr_ses->seqid = cs_slot->sl_seqid;
3347 /* cache solo and embedded create sessions under the client_lock */
3348 nfsd4_cache_create_session(cr_ses, cs_slot, status);
3349 spin_unlock(&nn->client_lock);
3350 /* init connection and backchannel */
3351 nfsd4_init_conn(rqstp, conn, new);
3352 nfsd4_put_session(new);
3357 spin_unlock(&nn->client_lock);
3362 __free_session(new);
3363 out_release_drc_mem:
3364 nfsd4_put_drc_mem(&cr_ses->fore_channel);
3368 static __be32 nfsd4_map_bcts_dir(u32 *dir)
3371 case NFS4_CDFC4_FORE:
3372 case NFS4_CDFC4_BACK:
3374 case NFS4_CDFC4_FORE_OR_BOTH:
3375 case NFS4_CDFC4_BACK_OR_BOTH:
3376 *dir = NFS4_CDFC4_BOTH;
3379 return nfserr_inval;
3382 __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp,
3383 struct nfsd4_compound_state *cstate,
3384 union nfsd4_op_u *u)
3386 struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl;
3387 struct nfsd4_session *session = cstate->session;
3388 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3391 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
3394 spin_lock(&nn->client_lock);
3395 session->se_cb_prog = bc->bc_cb_program;
3396 session->se_cb_sec = bc->bc_cb_sec;
3397 spin_unlock(&nn->client_lock);
3399 nfsd4_probe_callback(session->se_client);
3404 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
3405 struct nfsd4_compound_state *cstate,
3406 union nfsd4_op_u *u)
3408 struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session;
3410 struct nfsd4_conn *conn;
3411 struct nfsd4_session *session;
3412 struct net *net = SVC_NET(rqstp);
3413 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3415 if (!nfsd4_last_compound_op(rqstp))
3416 return nfserr_not_only_op;
3417 spin_lock(&nn->client_lock);
3418 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
3419 spin_unlock(&nn->client_lock);
3421 goto out_no_session;
3422 status = nfserr_wrong_cred;
3423 if (!nfsd4_mach_creds_match(session->se_client, rqstp))
3425 status = nfsd4_map_bcts_dir(&bcts->dir);
3428 conn = alloc_conn(rqstp, bcts->dir);
3429 status = nfserr_jukebox;
3432 nfsd4_init_conn(rqstp, conn, session);
3435 nfsd4_put_session(session);
3440 static bool nfsd4_compound_in_session(struct nfsd4_compound_state *cstate, struct nfs4_sessionid *sid)
3442 if (!cstate->session)
3444 return !memcmp(sid, &cstate->session->se_sessionid, sizeof(*sid));
3448 nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate,
3449 union nfsd4_op_u *u)
3451 struct nfs4_sessionid *sessionid = &u->destroy_session.sessionid;
3452 struct nfsd4_session *ses;
3454 int ref_held_by_me = 0;
3455 struct net *net = SVC_NET(r);
3456 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3458 status = nfserr_not_only_op;
3459 if (nfsd4_compound_in_session(cstate, sessionid)) {
3460 if (!nfsd4_last_compound_op(r))
3464 dump_sessionid(__func__, sessionid);
3465 spin_lock(&nn->client_lock);
3466 ses = find_in_sessionid_hashtbl(sessionid, net, &status);
3468 goto out_client_lock;
3469 status = nfserr_wrong_cred;
3470 if (!nfsd4_mach_creds_match(ses->se_client, r))
3471 goto out_put_session;
3472 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
3474 goto out_put_session;
3475 unhash_session(ses);
3476 spin_unlock(&nn->client_lock);
3478 nfsd4_probe_callback_sync(ses->se_client);
3480 spin_lock(&nn->client_lock);
3483 nfsd4_put_session_locked(ses);
3485 spin_unlock(&nn->client_lock);
3490 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
3492 struct nfsd4_conn *c;
3494 list_for_each_entry(c, &s->se_conns, cn_persession) {
3495 if (c->cn_xprt == xpt) {
3502 static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
3504 struct nfs4_client *clp = ses->se_client;
3505 struct nfsd4_conn *c;
3506 __be32 status = nfs_ok;
3509 spin_lock(&clp->cl_lock);
3510 c = __nfsd4_find_conn(new->cn_xprt, ses);
3513 status = nfserr_conn_not_bound_to_session;
3514 if (clp->cl_mach_cred)
3516 __nfsd4_hash_conn(new, ses);
3517 spin_unlock(&clp->cl_lock);
3518 ret = nfsd4_register_conn(new);
3520 /* oops; xprt is already down: */
3521 nfsd4_conn_lost(&new->cn_xpt_user);
3524 spin_unlock(&clp->cl_lock);
3529 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
3531 struct nfsd4_compoundargs *args = rqstp->rq_argp;
3533 return args->opcnt > session->se_fchannel.maxops;
3536 static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
3537 struct nfsd4_session *session)
3539 struct xdr_buf *xb = &rqstp->rq_arg;
3541 return xb->len > session->se_fchannel.maxreq_sz;
3544 static bool replay_matches_cache(struct svc_rqst *rqstp,
3545 struct nfsd4_sequence *seq, struct nfsd4_slot *slot)
3547 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
3549 if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) !=
3550 (bool)seq->cachethis)
3553 * If there's an error then the reply can have fewer ops than
3556 if (slot->sl_opcnt < argp->opcnt && !slot->sl_status)
3559 * But if we cached a reply with *more* ops than the call you're
3560 * sending us now, then this new call is clearly not really a
3561 * replay of the old one:
3563 if (slot->sl_opcnt > argp->opcnt)
3565 /* This is the only check explicitly called by spec: */
3566 if (!same_creds(&rqstp->rq_cred, &slot->sl_cred))
3569 * There may be more comparisons we could actually do, but the
3570 * spec doesn't require us to catch every case where the calls
3571 * don't match (that would require caching the call as well as
3572 * the reply), so we don't bother.
3578 nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3579 union nfsd4_op_u *u)
3581 struct nfsd4_sequence *seq = &u->sequence;
3582 struct nfsd4_compoundres *resp = rqstp->rq_resp;
3583 struct xdr_stream *xdr = &resp->xdr;
3584 struct nfsd4_session *session;
3585 struct nfs4_client *clp;
3586 struct nfsd4_slot *slot;
3587 struct nfsd4_conn *conn;
3590 struct net *net = SVC_NET(rqstp);
3591 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3593 if (resp->opcnt != 1)
3594 return nfserr_sequence_pos;
3597 * Will be either used or freed by nfsd4_sequence_check_conn
3600 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
3602 return nfserr_jukebox;
3604 spin_lock(&nn->client_lock);
3605 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
3607 goto out_no_session;
3608 clp = session->se_client;
3610 status = nfserr_too_many_ops;
3611 if (nfsd4_session_too_many_ops(rqstp, session))
3612 goto out_put_session;
3614 status = nfserr_req_too_big;
3615 if (nfsd4_request_too_big(rqstp, session))
3616 goto out_put_session;
3618 status = nfserr_badslot;
3619 if (seq->slotid >= session->se_fchannel.maxreqs)
3620 goto out_put_session;
3622 slot = session->se_slots[seq->slotid];
3623 dprintk("%s: slotid %d\n", __func__, seq->slotid);
3625 /* We do not negotiate the number of slots yet, so set the
3626 * maxslots to the session maxreqs which is used to encode
3627 * sr_highest_slotid and the sr_target_slot id to maxslots */
3628 seq->maxslots = session->se_fchannel.maxreqs;
3630 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
3631 slot->sl_flags & NFSD4_SLOT_INUSE);
3632 if (status == nfserr_replay_cache) {
3633 status = nfserr_seq_misordered;
3634 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
3635 goto out_put_session;
3636 status = nfserr_seq_false_retry;
3637 if (!replay_matches_cache(rqstp, seq, slot))
3638 goto out_put_session;
3639 cstate->slot = slot;
3640 cstate->session = session;
3642 /* Return the cached reply status and set cstate->status
3643 * for nfsd4_proc_compound processing */
3644 status = nfsd4_replay_cache_entry(resp, seq);
3645 cstate->status = nfserr_replay_cache;
3649 goto out_put_session;
3651 status = nfsd4_sequence_check_conn(conn, session);
3654 goto out_put_session;
3656 buflen = (seq->cachethis) ?
3657 session->se_fchannel.maxresp_cached :
3658 session->se_fchannel.maxresp_sz;
3659 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
3661 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
3662 goto out_put_session;
3663 svc_reserve(rqstp, buflen);
3666 /* Success! bump slot seqid */
3667 slot->sl_seqid = seq->seqid;
3668 slot->sl_flags |= NFSD4_SLOT_INUSE;
3670 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
3672 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
3674 cstate->slot = slot;
3675 cstate->session = session;
3679 switch (clp->cl_cb_state) {
3681 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
3683 case NFSD4_CB_FAULT:
3684 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
3687 seq->status_flags = 0;
3689 if (!list_empty(&clp->cl_revoked))
3690 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
3694 spin_unlock(&nn->client_lock);
3697 nfsd4_put_session_locked(session);
3698 goto out_no_session;
3702 nfsd4_sequence_done(struct nfsd4_compoundres *resp)
3704 struct nfsd4_compound_state *cs = &resp->cstate;
3706 if (nfsd4_has_session(cs)) {
3707 if (cs->status != nfserr_replay_cache) {
3708 nfsd4_store_cache_entry(resp);
3709 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
3711 /* Drop session reference that was taken in nfsd4_sequence() */
3712 nfsd4_put_session(cs->session);
3714 put_client_renew(cs->clp);
3718 nfsd4_destroy_clientid(struct svc_rqst *rqstp,
3719 struct nfsd4_compound_state *cstate,
3720 union nfsd4_op_u *u)
3722 struct nfsd4_destroy_clientid *dc = &u->destroy_clientid;
3723 struct nfs4_client *conf, *unconf;
3724 struct nfs4_client *clp = NULL;
3726 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3728 spin_lock(&nn->client_lock);
3729 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
3730 conf = find_confirmed_client(&dc->clientid, true, nn);
3731 WARN_ON_ONCE(conf && unconf);
3734 if (client_has_state(conf)) {
3735 status = nfserr_clientid_busy;
3738 status = mark_client_expired_locked(conf);
3745 status = nfserr_stale_clientid;
3748 if (!nfsd4_mach_creds_match(clp, rqstp)) {
3750 status = nfserr_wrong_cred;
3753 unhash_client_locked(clp);
3755 spin_unlock(&nn->client_lock);
3762 nfsd4_reclaim_complete(struct svc_rqst *rqstp,
3763 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
3765 struct nfsd4_reclaim_complete *rc = &u->reclaim_complete;
3768 if (rc->rca_one_fs) {
3769 if (!cstate->current_fh.fh_dentry)
3770 return nfserr_nofilehandle;
3772 * We don't take advantage of the rca_one_fs case.
3773 * That's OK, it's optional, we can safely ignore it.
3778 status = nfserr_complete_already;
3779 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
3780 &cstate->session->se_client->cl_flags))
3783 status = nfserr_stale_clientid;
3784 if (is_client_expired(cstate->session->se_client))
3786 * The following error isn't really legal.
3787 * But we only get here if the client just explicitly
3788 * destroyed the client. Surely it no longer cares what
3789 * error it gets back on an operation for the dead
3795 nfsd4_client_record_create(cstate->session->se_client);
3796 inc_reclaim_complete(cstate->session->se_client);
3802 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3803 union nfsd4_op_u *u)
3805 struct nfsd4_setclientid *setclid = &u->setclientid;
3806 struct xdr_netobj clname = setclid->se_name;
3807 nfs4_verifier clverifier = setclid->se_verf;
3808 struct nfs4_client *conf, *new;
3809 struct nfs4_client *unconf = NULL;
3811 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3813 new = create_client(clname, rqstp, &clverifier);
3815 return nfserr_jukebox;
3816 /* Cases below refer to rfc 3530 section 14.2.33: */
3817 spin_lock(&nn->client_lock);
3818 conf = find_confirmed_client_by_name(&clname, nn);
3819 if (conf && client_has_state(conf)) {
3821 status = nfserr_clid_inuse;
3822 if (clp_used_exchangeid(conf))
3824 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
3825 char addr_str[INET6_ADDRSTRLEN];
3826 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
3828 dprintk("NFSD: setclientid: string in use by client "
3829 "at %s\n", addr_str);
3833 unconf = find_unconfirmed_client_by_name(&clname, nn);
3835 unhash_client_locked(unconf);
3836 if (conf && same_verf(&conf->cl_verifier, &clverifier)) {
3837 /* case 1: probable callback update */
3838 copy_clid(new, conf);
3839 gen_confirm(new, nn);
3840 } else /* case 4 (new client) or cases 2, 3 (client reboot): */
3842 new->cl_minorversion = 0;
3843 gen_callback(new, setclid, rqstp);
3844 add_to_unconfirmed(new);
3845 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
3846 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
3847 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
3851 spin_unlock(&nn->client_lock);
3855 expire_client(unconf);
3861 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
3862 struct nfsd4_compound_state *cstate,
3863 union nfsd4_op_u *u)
3865 struct nfsd4_setclientid_confirm *setclientid_confirm =
3866 &u->setclientid_confirm;
3867 struct nfs4_client *conf, *unconf;
3868 struct nfs4_client *old = NULL;
3869 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
3870 clientid_t * clid = &setclientid_confirm->sc_clientid;
3872 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3874 if (STALE_CLIENTID(clid, nn))
3875 return nfserr_stale_clientid;
3877 spin_lock(&nn->client_lock);
3878 conf = find_confirmed_client(clid, false, nn);
3879 unconf = find_unconfirmed_client(clid, false, nn);
3881 * We try hard to give out unique clientid's, so if we get an
3882 * attempt to confirm the same clientid with a different cred,
3883 * the client may be buggy; this should never happen.
3885 * Nevertheless, RFC 7530 recommends INUSE for this case:
3887 status = nfserr_clid_inuse;
3888 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3890 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3892 /* cases below refer to rfc 3530 section 14.2.34: */
3893 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
3894 if (conf && same_verf(&confirm, &conf->cl_confirm)) {
3895 /* case 2: probable retransmit */
3897 } else /* case 4: client hasn't noticed we rebooted yet? */
3898 status = nfserr_stale_clientid;
3902 if (conf) { /* case 1: callback update */
3904 unhash_client_locked(old);
3905 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
3906 } else { /* case 3: normal case; new or rebooted client */
3907 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3909 status = nfserr_clid_inuse;
3910 if (client_has_state(old)
3911 && !same_creds(&unconf->cl_cred,
3914 status = mark_client_expired_locked(old);
3920 move_to_confirmed(unconf);
3923 get_client_locked(conf);
3924 spin_unlock(&nn->client_lock);
3925 nfsd4_probe_callback(conf);
3926 spin_lock(&nn->client_lock);
3927 put_client_renew_locked(conf);
3929 spin_unlock(&nn->client_lock);
3935 static struct nfs4_file *nfsd4_alloc_file(void)
3937 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3940 /* OPEN Share state helper functions */
3941 static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
3942 struct nfs4_file *fp)
3944 lockdep_assert_held(&state_lock);
3946 refcount_set(&fp->fi_ref, 1);
3947 spin_lock_init(&fp->fi_lock);
3948 INIT_LIST_HEAD(&fp->fi_stateids);
3949 INIT_LIST_HEAD(&fp->fi_delegations);
3950 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
3951 fh_copy_shallow(&fp->fi_fhandle, fh);
3952 fp->fi_deleg_file = NULL;
3953 fp->fi_had_conflict = false;
3954 fp->fi_share_deny = 0;
3955 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3956 memset(fp->fi_access, 0, sizeof(fp->fi_access));
3957 #ifdef CONFIG_NFSD_PNFS
3958 INIT_LIST_HEAD(&fp->fi_lo_states);
3959 atomic_set(&fp->fi_lo_recalls, 0);
3961 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
3965 nfsd4_free_slabs(void)
3967 kmem_cache_destroy(client_slab);
3968 kmem_cache_destroy(openowner_slab);
3969 kmem_cache_destroy(lockowner_slab);
3970 kmem_cache_destroy(file_slab);
3971 kmem_cache_destroy(stateid_slab);
3972 kmem_cache_destroy(deleg_slab);
3973 kmem_cache_destroy(odstate_slab);
3977 nfsd4_init_slabs(void)
3979 client_slab = kmem_cache_create("nfsd4_clients",
3980 sizeof(struct nfs4_client), 0, 0, NULL);
3981 if (client_slab == NULL)
3983 openowner_slab = kmem_cache_create("nfsd4_openowners",
3984 sizeof(struct nfs4_openowner), 0, 0, NULL);
3985 if (openowner_slab == NULL)
3986 goto out_free_client_slab;
3987 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
3988 sizeof(struct nfs4_lockowner), 0, 0, NULL);
3989 if (lockowner_slab == NULL)
3990 goto out_free_openowner_slab;
3991 file_slab = kmem_cache_create("nfsd4_files",
3992 sizeof(struct nfs4_file), 0, 0, NULL);
3993 if (file_slab == NULL)
3994 goto out_free_lockowner_slab;
3995 stateid_slab = kmem_cache_create("nfsd4_stateids",
3996 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
3997 if (stateid_slab == NULL)
3998 goto out_free_file_slab;
3999 deleg_slab = kmem_cache_create("nfsd4_delegations",
4000 sizeof(struct nfs4_delegation), 0, 0, NULL);
4001 if (deleg_slab == NULL)
4002 goto out_free_stateid_slab;
4003 odstate_slab = kmem_cache_create("nfsd4_odstate",
4004 sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
4005 if (odstate_slab == NULL)
4006 goto out_free_deleg_slab;
4009 out_free_deleg_slab:
4010 kmem_cache_destroy(deleg_slab);
4011 out_free_stateid_slab:
4012 kmem_cache_destroy(stateid_slab);
4014 kmem_cache_destroy(file_slab);
4015 out_free_lockowner_slab:
4016 kmem_cache_destroy(lockowner_slab);
4017 out_free_openowner_slab:
4018 kmem_cache_destroy(openowner_slab);
4019 out_free_client_slab:
4020 kmem_cache_destroy(client_slab);
4022 dprintk("nfsd4: out of memory while initializing nfsv4\n");
4026 static void init_nfs4_replay(struct nfs4_replay *rp)
4028 rp->rp_status = nfserr_serverfault;
4030 rp->rp_buf = rp->rp_ibuf;
4031 mutex_init(&rp->rp_mutex);
4034 static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
4035 struct nfs4_stateowner *so)
4037 if (!nfsd4_has_session(cstate)) {
4038 mutex_lock(&so->so_replay.rp_mutex);
4039 cstate->replay_owner = nfs4_get_stateowner(so);
4043 void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
4045 struct nfs4_stateowner *so = cstate->replay_owner;
4048 cstate->replay_owner = NULL;
4049 mutex_unlock(&so->so_replay.rp_mutex);
4050 nfs4_put_stateowner(so);
4054 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
4056 struct nfs4_stateowner *sop;
4058 sop = kmem_cache_alloc(slab, GFP_KERNEL);
4062 xdr_netobj_dup(&sop->so_owner, owner, GFP_KERNEL);
4063 if (!sop->so_owner.data) {
4064 kmem_cache_free(slab, sop);
4068 INIT_LIST_HEAD(&sop->so_stateids);
4069 sop->so_client = clp;
4070 init_nfs4_replay(&sop->so_replay);
4071 atomic_set(&sop->so_count, 1);
4075 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
4077 lockdep_assert_held(&clp->cl_lock);
4079 list_add(&oo->oo_owner.so_strhash,
4080 &clp->cl_ownerstr_hashtbl[strhashval]);
4081 list_add(&oo->oo_perclient, &clp->cl_openowners);
4084 static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
4086 unhash_openowner_locked(openowner(so));
4089 static void nfs4_free_openowner(struct nfs4_stateowner *so)
4091 struct nfs4_openowner *oo = openowner(so);
4093 kmem_cache_free(openowner_slab, oo);
4096 static const struct nfs4_stateowner_operations openowner_ops = {
4097 .so_unhash = nfs4_unhash_openowner,
4098 .so_free = nfs4_free_openowner,
4101 static struct nfs4_ol_stateid *
4102 nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4104 struct nfs4_ol_stateid *local, *ret = NULL;
4105 struct nfs4_openowner *oo = open->op_openowner;
4107 lockdep_assert_held(&fp->fi_lock);
4109 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
4110 /* ignore lock owners */
4111 if (local->st_stateowner->so_is_open_owner == 0)
4113 if (local->st_stateowner != &oo->oo_owner)
4115 if (local->st_stid.sc_type == NFS4_OPEN_STID) {
4117 refcount_inc(&ret->st_stid.sc_count);
4125 nfsd4_verify_open_stid(struct nfs4_stid *s)
4127 __be32 ret = nfs_ok;
4129 switch (s->sc_type) {
4133 case NFS4_CLOSED_STID:
4134 case NFS4_CLOSED_DELEG_STID:
4135 ret = nfserr_bad_stateid;
4137 case NFS4_REVOKED_DELEG_STID:
4138 ret = nfserr_deleg_revoked;
4143 /* Lock the stateid st_mutex, and deal with races with CLOSE */
4145 nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp)
4149 mutex_lock_nested(&stp->st_mutex, LOCK_STATEID_MUTEX);
4150 ret = nfsd4_verify_open_stid(&stp->st_stid);
4152 mutex_unlock(&stp->st_mutex);
4156 static struct nfs4_ol_stateid *
4157 nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4159 struct nfs4_ol_stateid *stp;
4161 spin_lock(&fp->fi_lock);
4162 stp = nfsd4_find_existing_open(fp, open);
4163 spin_unlock(&fp->fi_lock);
4164 if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok)
4166 nfs4_put_stid(&stp->st_stid);
4171 static struct nfs4_openowner *
4172 alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
4173 struct nfsd4_compound_state *cstate)
4175 struct nfs4_client *clp = cstate->clp;
4176 struct nfs4_openowner *oo, *ret;
4178 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
4181 oo->oo_owner.so_ops = &openowner_ops;
4182 oo->oo_owner.so_is_open_owner = 1;
4183 oo->oo_owner.so_seqid = open->op_seqid;
4185 if (nfsd4_has_session(cstate))
4186 oo->oo_flags |= NFS4_OO_CONFIRMED;
4188 oo->oo_last_closed_stid = NULL;
4189 INIT_LIST_HEAD(&oo->oo_close_lru);
4190 spin_lock(&clp->cl_lock);
4191 ret = find_openstateowner_str_locked(strhashval, open, clp);
4193 hash_openowner(oo, clp, strhashval);
4196 nfs4_free_stateowner(&oo->oo_owner);
4198 spin_unlock(&clp->cl_lock);
4202 static struct nfs4_ol_stateid *
4203 init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
4206 struct nfs4_openowner *oo = open->op_openowner;
4207 struct nfs4_ol_stateid *retstp = NULL;
4208 struct nfs4_ol_stateid *stp;
4211 /* We are moving these outside of the spinlocks to avoid the warnings */
4212 mutex_init(&stp->st_mutex);
4213 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
4216 spin_lock(&oo->oo_owner.so_client->cl_lock);
4217 spin_lock(&fp->fi_lock);
4219 retstp = nfsd4_find_existing_open(fp, open);
4223 open->op_stp = NULL;
4224 refcount_inc(&stp->st_stid.sc_count);
4225 stp->st_stid.sc_type = NFS4_OPEN_STID;
4226 INIT_LIST_HEAD(&stp->st_locks);
4227 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
4229 stp->st_stid.sc_file = fp;
4230 stp->st_access_bmap = 0;
4231 stp->st_deny_bmap = 0;
4232 stp->st_openstp = NULL;
4233 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
4234 list_add(&stp->st_perfile, &fp->fi_stateids);
4237 spin_unlock(&fp->fi_lock);
4238 spin_unlock(&oo->oo_owner.so_client->cl_lock);
4240 /* Handle races with CLOSE */
4241 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
4242 nfs4_put_stid(&retstp->st_stid);
4245 /* To keep mutex tracking happy */
4246 mutex_unlock(&stp->st_mutex);
4253 * In the 4.0 case we need to keep the owners around a little while to handle
4254 * CLOSE replay. We still do need to release any file access that is held by
4255 * them before returning however.
4258 move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
4260 struct nfs4_ol_stateid *last;
4261 struct nfs4_openowner *oo = openowner(s->st_stateowner);
4262 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
4265 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
4268 * We know that we hold one reference via nfsd4_close, and another
4269 * "persistent" reference for the client. If the refcount is higher
4270 * than 2, then there are still calls in progress that are using this
4271 * stateid. We can't put the sc_file reference until they are finished.
4272 * Wait for the refcount to drop to 2. Since it has been unhashed,
4273 * there should be no danger of the refcount going back up again at
4276 wait_event(close_wq, refcount_read(&s->st_stid.sc_count) == 2);
4278 release_all_access(s);
4279 if (s->st_stid.sc_file) {
4280 put_nfs4_file(s->st_stid.sc_file);
4281 s->st_stid.sc_file = NULL;
4284 spin_lock(&nn->client_lock);
4285 last = oo->oo_last_closed_stid;
4286 oo->oo_last_closed_stid = s;
4287 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
4288 oo->oo_time = get_seconds();
4289 spin_unlock(&nn->client_lock);
4291 nfs4_put_stid(&last->st_stid);
4294 /* search file_hashtbl[] for file */
4295 static struct nfs4_file *
4296 find_file_locked(struct knfsd_fh *fh, unsigned int hashval)
4298 struct nfs4_file *fp;
4300 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash) {
4301 if (fh_match(&fp->fi_fhandle, fh)) {
4302 if (refcount_inc_not_zero(&fp->fi_ref))
4310 find_file(struct knfsd_fh *fh)
4312 struct nfs4_file *fp;
4313 unsigned int hashval = file_hashval(fh);
4316 fp = find_file_locked(fh, hashval);
4321 static struct nfs4_file *
4322 find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
4324 struct nfs4_file *fp;
4325 unsigned int hashval = file_hashval(fh);
4328 fp = find_file_locked(fh, hashval);
4333 spin_lock(&state_lock);
4334 fp = find_file_locked(fh, hashval);
4335 if (likely(fp == NULL)) {
4336 nfsd4_init_file(fh, hashval, new);
4339 spin_unlock(&state_lock);
4345 * Called to check deny when READ with all zero stateid or
4346 * WRITE with all zero or all one stateid
4349 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
4351 struct nfs4_file *fp;
4352 __be32 ret = nfs_ok;
4354 fp = find_file(¤t_fh->fh_handle);
4357 /* Check for conflicting share reservations */
4358 spin_lock(&fp->fi_lock);
4359 if (fp->fi_share_deny & deny_type)
4360 ret = nfserr_locked;
4361 spin_unlock(&fp->fi_lock);
4366 static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
4368 struct nfs4_delegation *dp = cb_to_delegation(cb);
4369 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
4372 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
4375 * We can't do this in nfsd_break_deleg_cb because it is
4376 * already holding inode->i_lock.
4378 * If the dl_time != 0, then we know that it has already been
4379 * queued for a lease break. Don't queue it again.
4381 spin_lock(&state_lock);
4382 if (dp->dl_time == 0) {
4383 dp->dl_time = get_seconds();
4384 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
4386 spin_unlock(&state_lock);
4389 static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
4390 struct rpc_task *task)
4392 struct nfs4_delegation *dp = cb_to_delegation(cb);
4394 if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID)
4397 switch (task->tk_status) {
4400 case -NFS4ERR_DELAY:
4401 rpc_delay(task, 2 * HZ);
4404 case -NFS4ERR_BAD_STATEID:
4406 * Race: client probably got cb_recall before open reply
4407 * granting delegation.
4409 if (dp->dl_retries--) {
4410 rpc_delay(task, 2 * HZ);
4419 static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
4421 struct nfs4_delegation *dp = cb_to_delegation(cb);
4423 nfs4_put_stid(&dp->dl_stid);
4426 static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
4427 .prepare = nfsd4_cb_recall_prepare,
4428 .done = nfsd4_cb_recall_done,
4429 .release = nfsd4_cb_recall_release,
4432 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
4435 * We're assuming the state code never drops its reference
4436 * without first removing the lease. Since we're in this lease
4437 * callback (and since the lease code is serialized by the
4438 * i_lock) we know the server hasn't removed the lease yet, and
4439 * we know it's safe to take a reference.
4441 refcount_inc(&dp->dl_stid.sc_count);
4442 nfsd4_run_cb(&dp->dl_recall);
4445 /* Called from break_lease() with i_lock held. */
4447 nfsd_break_deleg_cb(struct file_lock *fl)
4450 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
4451 struct nfs4_file *fp = dp->dl_stid.sc_file;
4454 * We don't want the locks code to timeout the lease for us;
4455 * we'll remove it ourself if a delegation isn't returned
4458 fl->fl_break_time = 0;
4460 spin_lock(&fp->fi_lock);
4461 fp->fi_had_conflict = true;
4462 nfsd_break_one_deleg(dp);
4463 spin_unlock(&fp->fi_lock);
4468 nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
4469 struct list_head *dispose)
4472 return lease_modify(onlist, arg, dispose);
4477 static const struct lock_manager_operations nfsd_lease_mng_ops = {
4478 .lm_break = nfsd_break_deleg_cb,
4479 .lm_change = nfsd_change_deleg_cb,
4482 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
4484 if (nfsd4_has_session(cstate))
4486 if (seqid == so->so_seqid - 1)
4487 return nfserr_replay_me;
4488 if (seqid == so->so_seqid)
4490 return nfserr_bad_seqid;
4493 static __be32 lookup_clientid(clientid_t *clid,
4494 struct nfsd4_compound_state *cstate,
4495 struct nfsd_net *nn)
4497 struct nfs4_client *found;
4500 found = cstate->clp;
4501 if (!same_clid(&found->cl_clientid, clid))
4502 return nfserr_stale_clientid;
4506 if (STALE_CLIENTID(clid, nn))
4507 return nfserr_stale_clientid;
4510 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
4511 * cached already then we know this is for is for v4.0 and "sessions"
4514 WARN_ON_ONCE(cstate->session);
4515 spin_lock(&nn->client_lock);
4516 found = find_confirmed_client(clid, false, nn);
4518 spin_unlock(&nn->client_lock);
4519 return nfserr_expired;
4521 atomic_inc(&found->cl_rpc_users);
4522 spin_unlock(&nn->client_lock);
4524 /* Cache the nfs4_client in cstate! */
4525 cstate->clp = found;
4530 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
4531 struct nfsd4_open *open, struct nfsd_net *nn)
4533 clientid_t *clientid = &open->op_clientid;
4534 struct nfs4_client *clp = NULL;
4535 unsigned int strhashval;
4536 struct nfs4_openowner *oo = NULL;
4539 if (STALE_CLIENTID(&open->op_clientid, nn))
4540 return nfserr_stale_clientid;
4542 * In case we need it later, after we've already created the
4543 * file and don't want to risk a further failure:
4545 open->op_file = nfsd4_alloc_file();
4546 if (open->op_file == NULL)
4547 return nfserr_jukebox;
4549 status = lookup_clientid(clientid, cstate, nn);
4554 strhashval = ownerstr_hashval(&open->op_owner);
4555 oo = find_openstateowner_str(strhashval, open, clp);
4556 open->op_openowner = oo;
4560 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4561 /* Replace unconfirmed owners without checking for replay. */
4562 release_openowner(oo);
4563 open->op_openowner = NULL;
4566 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
4571 oo = alloc_init_open_stateowner(strhashval, open, cstate);
4573 return nfserr_jukebox;
4574 open->op_openowner = oo;
4576 open->op_stp = nfs4_alloc_open_stateid(clp);
4578 return nfserr_jukebox;
4580 if (nfsd4_has_session(cstate) &&
4581 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
4582 open->op_odstate = alloc_clnt_odstate(clp);
4583 if (!open->op_odstate)
4584 return nfserr_jukebox;
4590 static inline __be32
4591 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
4593 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
4594 return nfserr_openmode;
4599 static int share_access_to_flags(u32 share_access)
4601 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
4604 static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
4606 struct nfs4_stid *ret;
4608 ret = find_stateid_by_type(cl, s,
4609 NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID);
4612 return delegstateid(ret);
4615 static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
4617 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
4618 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
4622 nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
4623 struct nfs4_delegation **dp)
4626 __be32 status = nfserr_bad_stateid;
4627 struct nfs4_delegation *deleg;
4629 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
4632 if (deleg->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) {
4633 nfs4_put_stid(&deleg->dl_stid);
4634 if (cl->cl_minorversion)
4635 status = nfserr_deleg_revoked;
4638 flags = share_access_to_flags(open->op_share_access);
4639 status = nfs4_check_delegmode(deleg, flags);
4641 nfs4_put_stid(&deleg->dl_stid);
4646 if (!nfsd4_is_deleg_cur(open))
4650 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
4654 static inline int nfs4_access_to_access(u32 nfs4_access)
4658 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
4659 flags |= NFSD_MAY_READ;
4660 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
4661 flags |= NFSD_MAY_WRITE;
4665 static inline __be32
4666 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
4667 struct nfsd4_open *open)
4669 struct iattr iattr = {
4670 .ia_valid = ATTR_SIZE,
4673 if (!open->op_truncate)
4675 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
4676 return nfserr_inval;
4677 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
4680 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
4681 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
4682 struct nfsd4_open *open)
4684 struct nfsd_file *nf = NULL;
4686 int oflag = nfs4_access_to_omode(open->op_share_access);
4687 int access = nfs4_access_to_access(open->op_share_access);
4688 unsigned char old_access_bmap, old_deny_bmap;
4690 spin_lock(&fp->fi_lock);
4693 * Are we trying to set a deny mode that would conflict with
4696 status = nfs4_file_check_deny(fp, open->op_share_deny);
4697 if (status != nfs_ok) {
4698 spin_unlock(&fp->fi_lock);
4702 /* set access to the file */
4703 status = nfs4_file_get_access(fp, open->op_share_access);
4704 if (status != nfs_ok) {
4705 spin_unlock(&fp->fi_lock);
4709 /* Set access bits in stateid */
4710 old_access_bmap = stp->st_access_bmap;
4711 set_access(open->op_share_access, stp);
4713 /* Set new deny mask */
4714 old_deny_bmap = stp->st_deny_bmap;
4715 set_deny(open->op_share_deny, stp);
4716 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4718 if (!fp->fi_fds[oflag]) {
4719 spin_unlock(&fp->fi_lock);
4720 status = nfsd_file_acquire(rqstp, cur_fh, access, &nf);
4722 goto out_put_access;
4723 spin_lock(&fp->fi_lock);
4724 if (!fp->fi_fds[oflag]) {
4725 fp->fi_fds[oflag] = nf;
4729 spin_unlock(&fp->fi_lock);
4733 status = nfsd4_truncate(rqstp, cur_fh, open);
4735 goto out_put_access;
4739 stp->st_access_bmap = old_access_bmap;
4740 nfs4_file_put_access(fp, open->op_share_access);
4741 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
4746 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)
4749 unsigned char old_deny_bmap = stp->st_deny_bmap;
4751 if (!test_access(open->op_share_access, stp))
4752 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
4754 /* test and set deny mode */
4755 spin_lock(&fp->fi_lock);
4756 status = nfs4_file_check_deny(fp, open->op_share_deny);
4757 if (status == nfs_ok) {
4758 set_deny(open->op_share_deny, stp);
4759 fp->fi_share_deny |=
4760 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4762 spin_unlock(&fp->fi_lock);
4764 if (status != nfs_ok)
4767 status = nfsd4_truncate(rqstp, cur_fh, open);
4768 if (status != nfs_ok)
4769 reset_union_bmap_deny(old_deny_bmap, stp);
4773 /* Should we give out recallable state?: */
4774 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
4776 if (clp->cl_cb_state == NFSD4_CB_UP)
4779 * In the sessions case, since we don't have to establish a
4780 * separate connection for callbacks, we assume it's OK
4781 * until we hear otherwise:
4783 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
4786 static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp,
4789 struct file_lock *fl;
4791 fl = locks_alloc_lock();
4794 fl->fl_lmops = &nfsd_lease_mng_ops;
4795 fl->fl_flags = FL_DELEG;
4796 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
4797 fl->fl_end = OFFSET_MAX;
4798 fl->fl_owner = (fl_owner_t)dp;
4799 fl->fl_pid = current->tgid;
4800 fl->fl_file = dp->dl_stid.sc_file->fi_deleg_file->nf_file;
4804 static struct nfs4_delegation *
4805 nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
4806 struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate)
4809 struct nfs4_delegation *dp;
4810 struct nfsd_file *nf;
4811 struct file_lock *fl;
4814 * The fi_had_conflict and nfs_get_existing_delegation checks
4815 * here are just optimizations; we'll need to recheck them at
4818 if (fp->fi_had_conflict)
4819 return ERR_PTR(-EAGAIN);
4821 nf = find_readable_file(fp);
4823 /* We should always have a readable file here */
4825 return ERR_PTR(-EBADF);
4827 spin_lock(&state_lock);
4828 spin_lock(&fp->fi_lock);
4829 if (nfs4_delegation_exists(clp, fp))
4831 else if (!fp->fi_deleg_file) {
4832 fp->fi_deleg_file = nf;
4833 /* increment early to prevent fi_deleg_file from being
4835 fp->fi_delegees = 1;
4839 spin_unlock(&fp->fi_lock);
4840 spin_unlock(&state_lock);
4844 return ERR_PTR(status);
4847 dp = alloc_init_deleg(clp, fp, fh, odstate);
4851 fl = nfs4_alloc_init_lease(dp, NFS4_OPEN_DELEGATE_READ);
4853 goto out_clnt_odstate;
4855 status = vfs_setlease(fp->fi_deleg_file->nf_file, fl->fl_type, &fl, NULL);
4857 locks_free_lock(fl);
4859 goto out_clnt_odstate;
4861 spin_lock(&state_lock);
4862 spin_lock(&fp->fi_lock);
4863 if (fp->fi_had_conflict)
4866 status = hash_delegation_locked(dp, fp);
4867 spin_unlock(&fp->fi_lock);
4868 spin_unlock(&state_lock);
4875 vfs_setlease(fp->fi_deleg_file->nf_file, F_UNLCK, NULL, (void **)&dp);
4877 put_clnt_odstate(dp->dl_clnt_odstate);
4878 nfs4_put_stid(&dp->dl_stid);
4881 return ERR_PTR(status);
4884 static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
4886 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4887 if (status == -EAGAIN)
4888 open->op_why_no_deleg = WND4_CONTENTION;
4890 open->op_why_no_deleg = WND4_RESOURCE;
4891 switch (open->op_deleg_want) {
4892 case NFS4_SHARE_WANT_READ_DELEG:
4893 case NFS4_SHARE_WANT_WRITE_DELEG:
4894 case NFS4_SHARE_WANT_ANY_DELEG:
4896 case NFS4_SHARE_WANT_CANCEL:
4897 open->op_why_no_deleg = WND4_CANCELLED;
4899 case NFS4_SHARE_WANT_NO_DELEG:
4906 * Attempt to hand out a delegation.
4908 * Note we don't support write delegations, and won't until the vfs has
4909 * proper support for them.
4912 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
4913 struct nfs4_ol_stateid *stp)
4915 struct nfs4_delegation *dp;
4916 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
4917 struct nfs4_client *clp = stp->st_stid.sc_client;
4921 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
4922 open->op_recall = 0;
4923 switch (open->op_claim_type) {
4924 case NFS4_OPEN_CLAIM_PREVIOUS:
4926 open->op_recall = 1;
4927 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
4930 case NFS4_OPEN_CLAIM_NULL:
4931 case NFS4_OPEN_CLAIM_FH:
4933 * Let's not give out any delegations till everyone's
4934 * had the chance to reclaim theirs, *and* until
4935 * NLM locks have all been reclaimed:
4937 if (locks_in_grace(clp->net))
4939 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
4942 * Also, if the file was opened for write or
4943 * create, there's a good chance the client's
4944 * about to write to it, resulting in an
4945 * immediate recall (since we don't support
4946 * write delegations):
4948 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
4950 if (open->op_create == NFS4_OPEN_CREATE)
4956 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate);
4960 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
4962 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
4963 STATEID_VAL(&dp->dl_stid.sc_stateid));
4964 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
4965 nfs4_put_stid(&dp->dl_stid);
4968 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
4969 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
4970 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
4971 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
4972 open->op_recall = 1;
4975 /* 4.1 client asking for a delegation? */
4976 if (open->op_deleg_want)
4977 nfsd4_open_deleg_none_ext(open, status);
4981 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
4982 struct nfs4_delegation *dp)
4984 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
4985 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4986 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4987 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
4988 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
4989 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4990 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4991 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
4993 /* Otherwise the client must be confused wanting a delegation
4994 * it already has, therefore we don't return
4995 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
5000 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
5002 struct nfsd4_compoundres *resp = rqstp->rq_resp;
5003 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
5004 struct nfs4_file *fp = NULL;
5005 struct nfs4_ol_stateid *stp = NULL;
5006 struct nfs4_delegation *dp = NULL;
5008 bool new_stp = false;
5011 * Lookup file; if found, lookup stateid and check open request,
5012 * and check for delegations in the process of being recalled.
5013 * If not found, create the nfs4_file struct
5015 fp = find_or_add_file(open->op_file, ¤t_fh->fh_handle);
5016 if (fp != open->op_file) {
5017 status = nfs4_check_deleg(cl, open, &dp);
5020 stp = nfsd4_find_and_lock_existing_open(fp, open);
5022 open->op_file = NULL;
5023 status = nfserr_bad_stateid;
5024 if (nfsd4_is_deleg_cur(open))
5029 stp = init_open_stateid(fp, open);
5035 * OPEN the file, or upgrade an existing OPEN.
5036 * If truncate fails, the OPEN fails.
5038 * stp is already locked.
5041 /* Stateid was found, this is an OPEN upgrade */
5042 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
5044 mutex_unlock(&stp->st_mutex);
5048 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
5050 stp->st_stid.sc_type = NFS4_CLOSED_STID;
5051 release_open_stateid(stp);
5052 mutex_unlock(&stp->st_mutex);
5056 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
5058 if (stp->st_clnt_odstate == open->op_odstate)
5059 open->op_odstate = NULL;
5062 nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
5063 mutex_unlock(&stp->st_mutex);
5065 if (nfsd4_has_session(&resp->cstate)) {
5066 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
5067 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5068 open->op_why_no_deleg = WND4_NOT_WANTED;
5074 * Attempt to hand out a delegation. No error return, because the
5075 * OPEN succeeds even if we fail.
5077 nfs4_open_delegation(current_fh, open, stp);
5081 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
5082 STATEID_VAL(&stp->st_stid.sc_stateid));
5084 /* 4.1 client trying to upgrade/downgrade delegation? */
5085 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
5086 open->op_deleg_want)
5087 nfsd4_deleg_xgrade_none_ext(open, dp);
5091 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
5092 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
5094 * To finish the open response, we just need to set the rflags.
5096 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
5097 if (nfsd4_has_session(&resp->cstate))
5098 open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK;
5099 else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED))
5100 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
5103 nfs4_put_stid(&dp->dl_stid);
5105 nfs4_put_stid(&stp->st_stid);
5110 void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
5111 struct nfsd4_open *open)
5113 if (open->op_openowner) {
5114 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
5116 nfsd4_cstate_assign_replay(cstate, so);
5117 nfs4_put_stateowner(so);
5120 kmem_cache_free(file_slab, open->op_file);
5122 nfs4_put_stid(&open->op_stp->st_stid);
5123 if (open->op_odstate)
5124 kmem_cache_free(odstate_slab, open->op_odstate);
5128 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5129 union nfsd4_op_u *u)
5131 clientid_t *clid = &u->renew;
5132 struct nfs4_client *clp;
5134 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5136 dprintk("process_renew(%08x/%08x): starting\n",
5137 clid->cl_boot, clid->cl_id);
5138 status = lookup_clientid(clid, cstate, nn);
5142 status = nfserr_cb_path_down;
5143 if (!list_empty(&clp->cl_delegations)
5144 && clp->cl_cb_state != NFSD4_CB_UP)
5152 nfsd4_end_grace(struct nfsd_net *nn)
5154 /* do nothing if grace period already ended */
5155 if (nn->grace_ended)
5158 nn->grace_ended = true;
5160 * If the server goes down again right now, an NFSv4
5161 * client will still be allowed to reclaim after it comes back up,
5162 * even if it hasn't yet had a chance to reclaim state this time.
5165 nfsd4_record_grace_done(nn);
5167 * At this point, NFSv4 clients can still reclaim. But if the
5168 * server crashes, any that have not yet reclaimed will be out
5169 * of luck on the next boot.
5171 * (NFSv4.1+ clients are considered to have reclaimed once they
5172 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
5173 * have reclaimed after their first OPEN.)
5175 locks_end_grace(&nn->nfsd4_manager);
5177 * At this point, and once lockd and/or any other containers
5178 * exit their grace period, further reclaims will fail and
5179 * regular locking can resume.
5184 * If we've waited a lease period but there are still clients trying to
5185 * reclaim, wait a little longer to give them a chance to finish.
5187 static bool clients_still_reclaiming(struct nfsd_net *nn)
5189 unsigned long now = get_seconds();
5190 unsigned long double_grace_period_end = nn->boot_time +
5191 2 * nn->nfsd4_lease;
5193 if (nn->track_reclaim_completes &&
5194 atomic_read(&nn->nr_reclaim_complete) ==
5195 nn->reclaim_str_hashtbl_size)
5197 if (!nn->somebody_reclaimed)
5199 nn->somebody_reclaimed = false;
5201 * If we've given them *two* lease times to reclaim, and they're
5202 * still not done, give up:
5204 if (time_after(now, double_grace_period_end))
5210 nfs4_laundromat(struct nfsd_net *nn)
5212 struct nfs4_client *clp;
5213 struct nfs4_openowner *oo;
5214 struct nfs4_delegation *dp;
5215 struct nfs4_ol_stateid *stp;
5216 struct nfsd4_blocked_lock *nbl;
5217 struct list_head *pos, *next, reaplist;
5218 time_t cutoff = get_seconds() - nn->nfsd4_lease;
5219 time_t t, new_timeo = nn->nfsd4_lease;
5221 dprintk("NFSD: laundromat service - starting\n");
5223 if (clients_still_reclaiming(nn)) {
5227 dprintk("NFSD: end of grace period\n");
5228 nfsd4_end_grace(nn);
5229 INIT_LIST_HEAD(&reaplist);
5230 spin_lock(&nn->client_lock);
5231 list_for_each_safe(pos, next, &nn->client_lru) {
5232 clp = list_entry(pos, struct nfs4_client, cl_lru);
5233 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
5234 t = clp->cl_time - cutoff;
5235 new_timeo = min(new_timeo, t);
5238 if (mark_client_expired_locked(clp)) {
5239 dprintk("NFSD: client in use (clientid %08x)\n",
5240 clp->cl_clientid.cl_id);
5243 list_add(&clp->cl_lru, &reaplist);
5245 spin_unlock(&nn->client_lock);
5246 list_for_each_safe(pos, next, &reaplist) {
5247 clp = list_entry(pos, struct nfs4_client, cl_lru);
5248 dprintk("NFSD: purging unused client (clientid %08x)\n",
5249 clp->cl_clientid.cl_id);
5250 list_del_init(&clp->cl_lru);
5253 spin_lock(&state_lock);
5254 list_for_each_safe(pos, next, &nn->del_recall_lru) {
5255 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
5256 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
5257 t = dp->dl_time - cutoff;
5258 new_timeo = min(new_timeo, t);
5261 WARN_ON(!unhash_delegation_locked(dp));
5262 list_add(&dp->dl_recall_lru, &reaplist);
5264 spin_unlock(&state_lock);
5265 while (!list_empty(&reaplist)) {
5266 dp = list_first_entry(&reaplist, struct nfs4_delegation,
5268 list_del_init(&dp->dl_recall_lru);
5269 revoke_delegation(dp);
5272 spin_lock(&nn->client_lock);
5273 while (!list_empty(&nn->close_lru)) {
5274 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
5276 if (time_after((unsigned long)oo->oo_time,
5277 (unsigned long)cutoff)) {
5278 t = oo->oo_time - cutoff;
5279 new_timeo = min(new_timeo, t);
5282 list_del_init(&oo->oo_close_lru);
5283 stp = oo->oo_last_closed_stid;
5284 oo->oo_last_closed_stid = NULL;
5285 spin_unlock(&nn->client_lock);
5286 nfs4_put_stid(&stp->st_stid);
5287 spin_lock(&nn->client_lock);
5289 spin_unlock(&nn->client_lock);
5292 * It's possible for a client to try and acquire an already held lock
5293 * that is being held for a long time, and then lose interest in it.
5294 * So, we clean out any un-revisited request after a lease period
5295 * under the assumption that the client is no longer interested.
5297 * RFC5661, sec. 9.6 states that the client must not rely on getting
5298 * notifications and must continue to poll for locks, even when the
5299 * server supports them. Thus this shouldn't lead to clients blocking
5300 * indefinitely once the lock does become free.
5302 BUG_ON(!list_empty(&reaplist));
5303 spin_lock(&nn->blocked_locks_lock);
5304 while (!list_empty(&nn->blocked_locks_lru)) {
5305 nbl = list_first_entry(&nn->blocked_locks_lru,
5306 struct nfsd4_blocked_lock, nbl_lru);
5307 if (time_after((unsigned long)nbl->nbl_time,
5308 (unsigned long)cutoff)) {
5309 t = nbl->nbl_time - cutoff;
5310 new_timeo = min(new_timeo, t);
5313 list_move(&nbl->nbl_lru, &reaplist);
5314 list_del_init(&nbl->nbl_list);
5316 spin_unlock(&nn->blocked_locks_lock);
5318 while (!list_empty(&reaplist)) {
5319 nbl = list_first_entry(&reaplist,
5320 struct nfsd4_blocked_lock, nbl_lru);
5321 list_del_init(&nbl->nbl_lru);
5322 free_blocked_lock(nbl);
5325 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
5329 static struct workqueue_struct *laundry_wq;
5330 static void laundromat_main(struct work_struct *);
5333 laundromat_main(struct work_struct *laundry)
5336 struct delayed_work *dwork = to_delayed_work(laundry);
5337 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
5340 t = nfs4_laundromat(nn);
5341 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
5342 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
5345 static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
5347 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
5348 return nfserr_bad_stateid;
5353 access_permit_read(struct nfs4_ol_stateid *stp)
5355 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
5356 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
5357 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
5361 access_permit_write(struct nfs4_ol_stateid *stp)
5363 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
5364 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
5368 __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
5370 __be32 status = nfserr_openmode;
5372 /* For lock stateid's, we test the parent open, not the lock: */
5373 if (stp->st_openstp)
5374 stp = stp->st_openstp;
5375 if ((flags & WR_STATE) && !access_permit_write(stp))
5377 if ((flags & RD_STATE) && !access_permit_read(stp))
5384 static inline __be32
5385 check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
5387 if (ONE_STATEID(stateid) && (flags & RD_STATE))
5389 else if (opens_in_grace(net)) {
5390 /* Answer in remaining cases depends on existence of
5391 * conflicting state; so we must wait out the grace period. */
5392 return nfserr_grace;
5393 } else if (flags & WR_STATE)
5394 return nfs4_share_conflict(current_fh,
5395 NFS4_SHARE_DENY_WRITE);
5396 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
5397 return nfs4_share_conflict(current_fh,
5398 NFS4_SHARE_DENY_READ);
5402 * Allow READ/WRITE during grace period on recovered state only for files
5403 * that are not able to provide mandatory locking.
5406 grace_disallows_io(struct net *net, struct inode *inode)
5408 return opens_in_grace(net) && mandatory_lock(inode);
5411 static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
5414 * When sessions are used the stateid generation number is ignored
5417 if (has_session && in->si_generation == 0)
5420 if (in->si_generation == ref->si_generation)
5423 /* If the client sends us a stateid from the future, it's buggy: */
5424 if (nfsd4_stateid_generation_after(in, ref))
5425 return nfserr_bad_stateid;
5427 * However, we could see a stateid from the past, even from a
5428 * non-buggy client. For example, if the client sends a lock
5429 * while some IO is outstanding, the lock may bump si_generation
5430 * while the IO is still in flight. The client could avoid that
5431 * situation by waiting for responses on all the IO requests,
5432 * but better performance may result in retrying IO that
5433 * receives an old_stateid error if requests are rarely
5434 * reordered in flight:
5436 return nfserr_old_stateid;
5439 static __be32 nfsd4_stid_check_stateid_generation(stateid_t *in, struct nfs4_stid *s, bool has_session)
5443 spin_lock(&s->sc_lock);
5444 ret = nfsd4_verify_open_stid(s);
5446 ret = check_stateid_generation(in, &s->sc_stateid, has_session);
5447 spin_unlock(&s->sc_lock);
5451 static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
5453 if (ols->st_stateowner->so_is_open_owner &&
5454 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
5455 return nfserr_bad_stateid;
5459 static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
5461 struct nfs4_stid *s;
5462 __be32 status = nfserr_bad_stateid;
5464 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5465 CLOSE_STATEID(stateid))
5467 /* Client debugging aid. */
5468 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
5469 char addr_str[INET6_ADDRSTRLEN];
5470 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
5472 pr_warn_ratelimited("NFSD: client %s testing state ID "
5473 "with incorrect client ID\n", addr_str);
5476 spin_lock(&cl->cl_lock);
5477 s = find_stateid_locked(cl, stateid);
5480 status = nfsd4_stid_check_stateid_generation(stateid, s, 1);
5483 switch (s->sc_type) {
5484 case NFS4_DELEG_STID:
5487 case NFS4_REVOKED_DELEG_STID:
5488 status = nfserr_deleg_revoked;
5490 case NFS4_OPEN_STID:
5491 case NFS4_LOCK_STID:
5492 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
5495 printk("unknown stateid type %x\n", s->sc_type);
5497 case NFS4_CLOSED_STID:
5498 case NFS4_CLOSED_DELEG_STID:
5499 status = nfserr_bad_stateid;
5502 spin_unlock(&cl->cl_lock);
5507 nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
5508 stateid_t *stateid, unsigned char typemask,
5509 struct nfs4_stid **s, struct nfsd_net *nn)
5512 bool return_revoked = false;
5515 * only return revoked delegations if explicitly asked.
5516 * otherwise we report revoked or bad_stateid status.
5518 if (typemask & NFS4_REVOKED_DELEG_STID)
5519 return_revoked = true;
5520 else if (typemask & NFS4_DELEG_STID)
5521 typemask |= NFS4_REVOKED_DELEG_STID;
5523 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5524 CLOSE_STATEID(stateid))
5525 return nfserr_bad_stateid;
5526 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
5527 if (status == nfserr_stale_clientid) {
5528 if (cstate->session)
5529 return nfserr_bad_stateid;
5530 return nfserr_stale_stateid;
5534 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
5536 return nfserr_bad_stateid;
5537 if (((*s)->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) {
5539 if (cstate->minorversion)
5540 return nfserr_deleg_revoked;
5541 return nfserr_bad_stateid;
5546 static struct nfsd_file *
5547 nfs4_find_file(struct nfs4_stid *s, int flags)
5552 switch (s->sc_type) {
5553 case NFS4_DELEG_STID:
5554 if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
5556 return nfsd_file_get(s->sc_file->fi_deleg_file);
5557 case NFS4_OPEN_STID:
5558 case NFS4_LOCK_STID:
5559 if (flags & RD_STATE)
5560 return find_readable_file(s->sc_file);
5562 return find_writeable_file(s->sc_file);
5570 nfs4_check_olstateid(struct nfs4_ol_stateid *ols, int flags)
5574 status = nfsd4_check_openowner_confirmed(ols);
5577 return nfs4_check_openmode(ols, flags);
5581 nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
5582 struct nfsd_file **nfp, int flags)
5584 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
5585 struct nfsd_file *nf;
5588 nf = nfs4_find_file(s, flags);
5590 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
5591 acc | NFSD_MAY_OWNER_OVERRIDE);
5597 status = nfsd_file_acquire(rqstp, fhp, acc, &nf);
5607 * Checks for stateid operations
5610 nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
5611 struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
5612 stateid_t *stateid, int flags, struct nfsd_file **nfp)
5614 struct inode *ino = d_inode(fhp->fh_dentry);
5615 struct net *net = SVC_NET(rqstp);
5616 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5617 struct nfs4_stid *s = NULL;
5623 if (grace_disallows_io(net, ino))
5624 return nfserr_grace;
5626 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
5627 status = check_special_stateids(net, fhp, stateid, flags);
5631 status = nfsd4_lookup_stateid(cstate, stateid,
5632 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
5636 status = nfsd4_stid_check_stateid_generation(stateid, s,
5637 nfsd4_has_session(cstate));
5641 switch (s->sc_type) {
5642 case NFS4_DELEG_STID:
5643 status = nfs4_check_delegmode(delegstateid(s), flags);
5645 case NFS4_OPEN_STID:
5646 case NFS4_LOCK_STID:
5647 status = nfs4_check_olstateid(openlockstateid(s), flags);
5650 status = nfserr_bad_stateid;
5655 status = nfs4_check_fh(fhp, s);
5658 if (status == nfs_ok && nfp)
5659 status = nfs4_check_file(rqstp, fhp, s, nfp, flags);
5667 * Test if the stateid is valid
5670 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5671 union nfsd4_op_u *u)
5673 struct nfsd4_test_stateid *test_stateid = &u->test_stateid;
5674 struct nfsd4_test_stateid_id *stateid;
5675 struct nfs4_client *cl = cstate->session->se_client;
5677 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
5678 stateid->ts_id_status =
5679 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
5685 nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
5687 struct nfs4_ol_stateid *stp = openlockstateid(s);
5690 ret = nfsd4_lock_ol_stateid(stp);
5694 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5698 ret = nfserr_locks_held;
5699 if (check_for_locks(stp->st_stid.sc_file,
5700 lockowner(stp->st_stateowner)))
5703 release_lock_stateid(stp);
5707 mutex_unlock(&stp->st_mutex);
5714 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5715 union nfsd4_op_u *u)
5717 struct nfsd4_free_stateid *free_stateid = &u->free_stateid;
5718 stateid_t *stateid = &free_stateid->fr_stateid;
5719 struct nfs4_stid *s;
5720 struct nfs4_delegation *dp;
5721 struct nfs4_client *cl = cstate->session->se_client;
5722 __be32 ret = nfserr_bad_stateid;
5724 spin_lock(&cl->cl_lock);
5725 s = find_stateid_locked(cl, stateid);
5728 spin_lock(&s->sc_lock);
5729 switch (s->sc_type) {
5730 case NFS4_DELEG_STID:
5731 ret = nfserr_locks_held;
5733 case NFS4_OPEN_STID:
5734 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5737 ret = nfserr_locks_held;
5739 case NFS4_LOCK_STID:
5740 spin_unlock(&s->sc_lock);
5741 refcount_inc(&s->sc_count);
5742 spin_unlock(&cl->cl_lock);
5743 ret = nfsd4_free_lock_stateid(stateid, s);
5745 case NFS4_REVOKED_DELEG_STID:
5746 spin_unlock(&s->sc_lock);
5747 dp = delegstateid(s);
5748 list_del_init(&dp->dl_recall_lru);
5749 spin_unlock(&cl->cl_lock);
5753 /* Default falls through and returns nfserr_bad_stateid */
5755 spin_unlock(&s->sc_lock);
5757 spin_unlock(&cl->cl_lock);
5765 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
5766 RD_STATE : WR_STATE;
5769 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
5771 struct svc_fh *current_fh = &cstate->current_fh;
5772 struct nfs4_stateowner *sop = stp->st_stateowner;
5775 status = nfsd4_check_seqid(cstate, sop, seqid);
5778 status = nfsd4_lock_ol_stateid(stp);
5779 if (status != nfs_ok)
5781 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
5782 if (status == nfs_ok)
5783 status = nfs4_check_fh(current_fh, &stp->st_stid);
5784 if (status != nfs_ok)
5785 mutex_unlock(&stp->st_mutex);
5790 * Checks for sequence id mutating operations.
5793 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
5794 stateid_t *stateid, char typemask,
5795 struct nfs4_ol_stateid **stpp,
5796 struct nfsd_net *nn)
5799 struct nfs4_stid *s;
5800 struct nfs4_ol_stateid *stp = NULL;
5802 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
5803 seqid, STATEID_VAL(stateid));
5806 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
5809 stp = openlockstateid(s);
5810 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
5812 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
5816 nfs4_put_stid(&stp->st_stid);
5820 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
5821 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
5824 struct nfs4_openowner *oo;
5825 struct nfs4_ol_stateid *stp;
5827 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
5828 NFS4_OPEN_STID, &stp, nn);
5831 oo = openowner(stp->st_stateowner);
5832 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
5833 mutex_unlock(&stp->st_mutex);
5834 nfs4_put_stid(&stp->st_stid);
5835 return nfserr_bad_stateid;
5842 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5843 union nfsd4_op_u *u)
5845 struct nfsd4_open_confirm *oc = &u->open_confirm;
5847 struct nfs4_openowner *oo;
5848 struct nfs4_ol_stateid *stp;
5849 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5851 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
5852 cstate->current_fh.fh_dentry);
5854 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
5858 status = nfs4_preprocess_seqid_op(cstate,
5859 oc->oc_seqid, &oc->oc_req_stateid,
5860 NFS4_OPEN_STID, &stp, nn);
5863 oo = openowner(stp->st_stateowner);
5864 status = nfserr_bad_stateid;
5865 if (oo->oo_flags & NFS4_OO_CONFIRMED) {
5866 mutex_unlock(&stp->st_mutex);
5869 oo->oo_flags |= NFS4_OO_CONFIRMED;
5870 nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
5871 mutex_unlock(&stp->st_mutex);
5872 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
5873 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
5875 nfsd4_client_record_create(oo->oo_owner.so_client);
5878 nfs4_put_stid(&stp->st_stid);
5880 nfsd4_bump_seqid(cstate, status);
5884 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
5886 if (!test_access(access, stp))
5888 nfs4_file_put_access(stp->st_stid.sc_file, access);
5889 clear_access(access, stp);
5892 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
5894 switch (to_access) {
5895 case NFS4_SHARE_ACCESS_READ:
5896 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
5897 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
5899 case NFS4_SHARE_ACCESS_WRITE:
5900 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
5901 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
5903 case NFS4_SHARE_ACCESS_BOTH:
5911 nfsd4_open_downgrade(struct svc_rqst *rqstp,
5912 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
5914 struct nfsd4_open_downgrade *od = &u->open_downgrade;
5916 struct nfs4_ol_stateid *stp;
5917 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5919 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
5920 cstate->current_fh.fh_dentry);
5922 /* We don't yet support WANT bits: */
5923 if (od->od_deleg_want)
5924 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
5927 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
5928 &od->od_stateid, &stp, nn);
5931 status = nfserr_inval;
5932 if (!test_access(od->od_share_access, stp)) {
5933 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
5934 stp->st_access_bmap, od->od_share_access);
5937 if (!test_deny(od->od_share_deny, stp)) {
5938 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
5939 stp->st_deny_bmap, od->od_share_deny);
5942 nfs4_stateid_downgrade(stp, od->od_share_access);
5943 reset_union_bmap_deny(od->od_share_deny, stp);
5944 nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
5947 mutex_unlock(&stp->st_mutex);
5948 nfs4_put_stid(&stp->st_stid);
5950 nfsd4_bump_seqid(cstate, status);
5954 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
5956 struct nfs4_client *clp = s->st_stid.sc_client;
5958 LIST_HEAD(reaplist);
5960 spin_lock(&clp->cl_lock);
5961 unhashed = unhash_open_stateid(s, &reaplist);
5963 if (clp->cl_minorversion) {
5965 put_ol_stateid_locked(s, &reaplist);
5966 spin_unlock(&clp->cl_lock);
5967 free_ol_stateid_reaplist(&reaplist);
5969 spin_unlock(&clp->cl_lock);
5970 free_ol_stateid_reaplist(&reaplist);
5972 move_to_close_lru(s, clp->net);
5977 * nfs4_unlock_state() called after encode
5980 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5981 union nfsd4_op_u *u)
5983 struct nfsd4_close *close = &u->close;
5985 struct nfs4_ol_stateid *stp;
5986 struct net *net = SVC_NET(rqstp);
5987 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5989 dprintk("NFSD: nfsd4_close on file %pd\n",
5990 cstate->current_fh.fh_dentry);
5992 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
5994 NFS4_OPEN_STID|NFS4_CLOSED_STID,
5996 nfsd4_bump_seqid(cstate, status);
6000 stp->st_stid.sc_type = NFS4_CLOSED_STID;
6003 * Technically we don't _really_ have to increment or copy it, since
6004 * it should just be gone after this operation and we clobber the
6005 * copied value below, but we continue to do so here just to ensure
6006 * that racing ops see that there was a state change.
6008 nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
6010 nfsd4_close_open_stateid(stp);
6011 mutex_unlock(&stp->st_mutex);
6013 /* v4.1+ suggests that we send a special stateid in here, since the
6014 * clients should just ignore this anyway. Since this is not useful
6015 * for v4.0 clients either, we set it to the special close_stateid
6018 * See RFC5661 section 18.2.4, and RFC7530 section 16.2.5
6020 memcpy(&close->cl_stateid, &close_stateid, sizeof(close->cl_stateid));
6022 /* put reference from nfs4_preprocess_seqid_op */
6023 nfs4_put_stid(&stp->st_stid);
6029 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6030 union nfsd4_op_u *u)
6032 struct nfsd4_delegreturn *dr = &u->delegreturn;
6033 struct nfs4_delegation *dp;
6034 stateid_t *stateid = &dr->dr_stateid;
6035 struct nfs4_stid *s;
6037 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6039 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
6042 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
6045 dp = delegstateid(s);
6046 status = nfsd4_stid_check_stateid_generation(stateid, &dp->dl_stid, nfsd4_has_session(cstate));
6050 destroy_delegation(dp);
6052 nfs4_put_stid(&dp->dl_stid);
6058 end_offset(u64 start, u64 len)
6063 return end >= start ? end: NFS4_MAX_UINT64;
6066 /* last octet in a range */
6068 last_byte_offset(u64 start, u64 len)
6074 return end > start ? end - 1: NFS4_MAX_UINT64;
6078 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
6079 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
6080 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
6081 * locking, this prevents us from being completely protocol-compliant. The
6082 * real solution to this problem is to start using unsigned file offsets in
6083 * the VFS, but this is a very deep change!
6086 nfs4_transform_lock_offset(struct file_lock *lock)
6088 if (lock->fl_start < 0)
6089 lock->fl_start = OFFSET_MAX;
6090 if (lock->fl_end < 0)
6091 lock->fl_end = OFFSET_MAX;
6095 nfsd4_fl_get_owner(fl_owner_t owner)
6097 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
6099 nfs4_get_stateowner(&lo->lo_owner);
6104 nfsd4_fl_put_owner(fl_owner_t owner)
6106 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
6109 nfs4_put_stateowner(&lo->lo_owner);
6113 nfsd4_lm_notify(struct file_lock *fl)
6115 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
6116 struct net *net = lo->lo_owner.so_client->net;
6117 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6118 struct nfsd4_blocked_lock *nbl = container_of(fl,
6119 struct nfsd4_blocked_lock, nbl_lock);
6122 /* An empty list means that something else is going to be using it */
6123 spin_lock(&nn->blocked_locks_lock);
6124 if (!list_empty(&nbl->nbl_list)) {
6125 list_del_init(&nbl->nbl_list);
6126 list_del_init(&nbl->nbl_lru);
6129 spin_unlock(&nn->blocked_locks_lock);
6132 nfsd4_run_cb(&nbl->nbl_cb);
6135 static const struct lock_manager_operations nfsd_posix_mng_ops = {
6136 .lm_notify = nfsd4_lm_notify,
6137 .lm_get_owner = nfsd4_fl_get_owner,
6138 .lm_put_owner = nfsd4_fl_put_owner,
6142 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
6144 struct nfs4_lockowner *lo;
6146 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
6147 lo = (struct nfs4_lockowner *) fl->fl_owner;
6148 xdr_netobj_dup(&deny->ld_owner, &lo->lo_owner.so_owner,
6150 if (!deny->ld_owner.data)
6151 /* We just don't care that much */
6153 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
6156 deny->ld_owner.len = 0;
6157 deny->ld_owner.data = NULL;
6158 deny->ld_clientid.cl_boot = 0;
6159 deny->ld_clientid.cl_id = 0;
6161 deny->ld_start = fl->fl_start;
6162 deny->ld_length = NFS4_MAX_UINT64;
6163 if (fl->fl_end != NFS4_MAX_UINT64)
6164 deny->ld_length = fl->fl_end - fl->fl_start + 1;
6165 deny->ld_type = NFS4_READ_LT;
6166 if (fl->fl_type != F_RDLCK)
6167 deny->ld_type = NFS4_WRITE_LT;
6170 static struct nfs4_lockowner *
6171 find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
6173 unsigned int strhashval = ownerstr_hashval(owner);
6174 struct nfs4_stateowner *so;
6176 lockdep_assert_held(&clp->cl_lock);
6178 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
6180 if (so->so_is_open_owner)
6182 if (same_owner_str(so, owner))
6183 return lockowner(nfs4_get_stateowner(so));
6188 static struct nfs4_lockowner *
6189 find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
6191 struct nfs4_lockowner *lo;
6193 spin_lock(&clp->cl_lock);
6194 lo = find_lockowner_str_locked(clp, owner);
6195 spin_unlock(&clp->cl_lock);
6199 static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
6201 unhash_lockowner_locked(lockowner(sop));
6204 static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
6206 struct nfs4_lockowner *lo = lockowner(sop);
6208 kmem_cache_free(lockowner_slab, lo);
6211 static const struct nfs4_stateowner_operations lockowner_ops = {
6212 .so_unhash = nfs4_unhash_lockowner,
6213 .so_free = nfs4_free_lockowner,
6217 * Alloc a lock owner structure.
6218 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
6221 * strhashval = ownerstr_hashval
6223 static struct nfs4_lockowner *
6224 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
6225 struct nfs4_ol_stateid *open_stp,
6226 struct nfsd4_lock *lock)
6228 struct nfs4_lockowner *lo, *ret;
6230 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
6233 INIT_LIST_HEAD(&lo->lo_blocked);
6234 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
6235 lo->lo_owner.so_is_open_owner = 0;
6236 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
6237 lo->lo_owner.so_ops = &lockowner_ops;
6238 spin_lock(&clp->cl_lock);
6239 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
6241 list_add(&lo->lo_owner.so_strhash,
6242 &clp->cl_ownerstr_hashtbl[strhashval]);
6245 nfs4_free_stateowner(&lo->lo_owner);
6247 spin_unlock(&clp->cl_lock);
6251 static struct nfs4_ol_stateid *
6252 find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
6254 struct nfs4_ol_stateid *lst;
6255 struct nfs4_client *clp = lo->lo_owner.so_client;
6257 lockdep_assert_held(&clp->cl_lock);
6259 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
6260 if (lst->st_stid.sc_type != NFS4_LOCK_STID)
6262 if (lst->st_stid.sc_file == fp) {
6263 refcount_inc(&lst->st_stid.sc_count);
6270 static struct nfs4_ol_stateid *
6271 init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
6272 struct nfs4_file *fp, struct inode *inode,
6273 struct nfs4_ol_stateid *open_stp)
6275 struct nfs4_client *clp = lo->lo_owner.so_client;
6276 struct nfs4_ol_stateid *retstp;
6278 mutex_init(&stp->st_mutex);
6279 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
6281 spin_lock(&clp->cl_lock);
6282 spin_lock(&fp->fi_lock);
6283 retstp = find_lock_stateid(lo, fp);
6287 refcount_inc(&stp->st_stid.sc_count);
6288 stp->st_stid.sc_type = NFS4_LOCK_STID;
6289 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
6291 stp->st_stid.sc_file = fp;
6292 stp->st_access_bmap = 0;
6293 stp->st_deny_bmap = open_stp->st_deny_bmap;
6294 stp->st_openstp = open_stp;
6295 list_add(&stp->st_locks, &open_stp->st_locks);
6296 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
6297 list_add(&stp->st_perfile, &fp->fi_stateids);
6299 spin_unlock(&fp->fi_lock);
6300 spin_unlock(&clp->cl_lock);
6302 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
6303 nfs4_put_stid(&retstp->st_stid);
6306 /* To keep mutex tracking happy */
6307 mutex_unlock(&stp->st_mutex);
6313 static struct nfs4_ol_stateid *
6314 find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
6315 struct inode *inode, struct nfs4_ol_stateid *ost,
6318 struct nfs4_stid *ns = NULL;
6319 struct nfs4_ol_stateid *lst;
6320 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
6321 struct nfs4_client *clp = oo->oo_owner.so_client;
6324 spin_lock(&clp->cl_lock);
6325 lst = find_lock_stateid(lo, fi);
6326 spin_unlock(&clp->cl_lock);
6328 if (nfsd4_lock_ol_stateid(lst) == nfs_ok)
6330 nfs4_put_stid(&lst->st_stid);
6332 ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid);
6336 lst = init_lock_stateid(openlockstateid(ns), lo, fi, inode, ost);
6337 if (lst == openlockstateid(ns))
6346 check_lock_length(u64 offset, u64 length)
6348 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
6349 (length > ~offset)));
6352 static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
6354 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
6356 lockdep_assert_held(&fp->fi_lock);
6358 if (test_access(access, lock_stp))
6360 __nfs4_file_get_access(fp, access);
6361 set_access(access, lock_stp);
6365 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
6366 struct nfs4_ol_stateid *ost,
6367 struct nfsd4_lock *lock,
6368 struct nfs4_ol_stateid **plst, bool *new)
6371 struct nfs4_file *fi = ost->st_stid.sc_file;
6372 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
6373 struct nfs4_client *cl = oo->oo_owner.so_client;
6374 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
6375 struct nfs4_lockowner *lo;
6376 struct nfs4_ol_stateid *lst;
6377 unsigned int strhashval;
6379 lo = find_lockowner_str(cl, &lock->lk_new_owner);
6381 strhashval = ownerstr_hashval(&lock->lk_new_owner);
6382 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
6384 return nfserr_jukebox;
6386 /* with an existing lockowner, seqids must be the same */
6387 status = nfserr_bad_seqid;
6388 if (!cstate->minorversion &&
6389 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
6393 lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
6395 status = nfserr_jukebox;
6402 nfs4_put_stateowner(&lo->lo_owner);
6410 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6411 union nfsd4_op_u *u)
6413 struct nfsd4_lock *lock = &u->lock;
6414 struct nfs4_openowner *open_sop = NULL;
6415 struct nfs4_lockowner *lock_sop = NULL;
6416 struct nfs4_ol_stateid *lock_stp = NULL;
6417 struct nfs4_ol_stateid *open_stp = NULL;
6418 struct nfs4_file *fp;
6419 struct nfsd_file *nf = NULL;
6420 struct nfsd4_blocked_lock *nbl = NULL;
6421 struct file_lock *file_lock = NULL;
6422 struct file_lock *conflock = NULL;
6427 unsigned char fl_type;
6428 unsigned int fl_flags = FL_POSIX;
6429 struct net *net = SVC_NET(rqstp);
6430 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6432 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
6433 (long long) lock->lk_offset,
6434 (long long) lock->lk_length);
6436 if (check_lock_length(lock->lk_offset, lock->lk_length))
6437 return nfserr_inval;
6439 if ((status = fh_verify(rqstp, &cstate->current_fh,
6440 S_IFREG, NFSD_MAY_LOCK))) {
6441 dprintk("NFSD: nfsd4_lock: permission denied!\n");
6445 if (lock->lk_is_new) {
6446 if (nfsd4_has_session(cstate))
6447 /* See rfc 5661 18.10.3: given clientid is ignored: */
6448 memcpy(&lock->lk_new_clientid,
6449 &cstate->session->se_client->cl_clientid,
6450 sizeof(clientid_t));
6452 status = nfserr_stale_clientid;
6453 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
6456 /* validate and update open stateid and open seqid */
6457 status = nfs4_preprocess_confirmed_seqid_op(cstate,
6458 lock->lk_new_open_seqid,
6459 &lock->lk_new_open_stateid,
6463 mutex_unlock(&open_stp->st_mutex);
6464 open_sop = openowner(open_stp->st_stateowner);
6465 status = nfserr_bad_stateid;
6466 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
6467 &lock->lk_new_clientid))
6469 status = lookup_or_create_lock_state(cstate, open_stp, lock,
6472 status = nfs4_preprocess_seqid_op(cstate,
6473 lock->lk_old_lock_seqid,
6474 &lock->lk_old_lock_stateid,
6475 NFS4_LOCK_STID, &lock_stp, nn);
6479 lock_sop = lockowner(lock_stp->st_stateowner);
6481 lkflg = setlkflg(lock->lk_type);
6482 status = nfs4_check_openmode(lock_stp, lkflg);
6486 status = nfserr_grace;
6487 if (locks_in_grace(net) && !lock->lk_reclaim)
6489 status = nfserr_no_grace;
6490 if (!locks_in_grace(net) && lock->lk_reclaim)
6493 fp = lock_stp->st_stid.sc_file;
6494 switch (lock->lk_type) {
6496 if (nfsd4_has_session(cstate))
6497 fl_flags |= FL_SLEEP;
6500 spin_lock(&fp->fi_lock);
6501 nf = find_readable_file_locked(fp);
6503 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
6504 spin_unlock(&fp->fi_lock);
6507 case NFS4_WRITEW_LT:
6508 if (nfsd4_has_session(cstate))
6509 fl_flags |= FL_SLEEP;
6512 spin_lock(&fp->fi_lock);
6513 nf = find_writeable_file_locked(fp);
6515 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
6516 spin_unlock(&fp->fi_lock);
6520 status = nfserr_inval;
6525 status = nfserr_openmode;
6529 nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
6531 dprintk("NFSD: %s: unable to allocate block!\n", __func__);
6532 status = nfserr_jukebox;
6536 file_lock = &nbl->nbl_lock;
6537 file_lock->fl_type = fl_type;
6538 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
6539 file_lock->fl_pid = current->tgid;
6540 file_lock->fl_file = nf->nf_file;
6541 file_lock->fl_flags = fl_flags;
6542 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6543 file_lock->fl_start = lock->lk_offset;
6544 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
6545 nfs4_transform_lock_offset(file_lock);
6547 conflock = locks_alloc_lock();
6549 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6550 status = nfserr_jukebox;
6554 if (fl_flags & FL_SLEEP) {
6555 nbl->nbl_time = get_seconds();
6556 spin_lock(&nn->blocked_locks_lock);
6557 list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
6558 list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
6559 spin_unlock(&nn->blocked_locks_lock);
6562 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, conflock);
6564 case 0: /* success! */
6565 nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
6567 if (lock->lk_reclaim)
6568 nn->somebody_reclaimed = true;
6570 case FILE_LOCK_DEFERRED:
6573 case -EAGAIN: /* conflock holds conflicting lock */
6574 status = nfserr_denied;
6575 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
6576 nfs4_set_lock_denied(conflock, &lock->lk_denied);
6579 status = nfserr_deadlock;
6582 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
6583 status = nfserrno(err);
6588 /* dequeue it if we queued it before */
6589 if (fl_flags & FL_SLEEP) {
6590 spin_lock(&nn->blocked_locks_lock);
6591 list_del_init(&nbl->nbl_list);
6592 list_del_init(&nbl->nbl_lru);
6593 spin_unlock(&nn->blocked_locks_lock);
6595 free_blocked_lock(nbl);
6600 /* Bump seqid manually if the 4.0 replay owner is openowner */
6601 if (cstate->replay_owner &&
6602 cstate->replay_owner != &lock_sop->lo_owner &&
6603 seqid_mutating_err(ntohl(status)))
6604 lock_sop->lo_owner.so_seqid++;
6607 * If this is a new, never-before-used stateid, and we are
6608 * returning an error, then just go ahead and release it.
6611 release_lock_stateid(lock_stp);
6613 mutex_unlock(&lock_stp->st_mutex);
6615 nfs4_put_stid(&lock_stp->st_stid);
6618 nfs4_put_stid(&open_stp->st_stid);
6619 nfsd4_bump_seqid(cstate, status);
6621 locks_free_lock(conflock);
6626 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
6627 * so we do a temporary open here just to get an open file to pass to
6628 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
6631 static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
6633 struct nfsd_file *nf;
6634 __be32 err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf);
6636 err = nfserrno(vfs_test_lock(nf->nf_file, lock));
6646 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6647 union nfsd4_op_u *u)
6649 struct nfsd4_lockt *lockt = &u->lockt;
6650 struct file_lock *file_lock = NULL;
6651 struct nfs4_lockowner *lo = NULL;
6653 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6655 if (locks_in_grace(SVC_NET(rqstp)))
6656 return nfserr_grace;
6658 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
6659 return nfserr_inval;
6661 if (!nfsd4_has_session(cstate)) {
6662 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
6667 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
6670 file_lock = locks_alloc_lock();
6672 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6673 status = nfserr_jukebox;
6677 switch (lockt->lt_type) {
6680 file_lock->fl_type = F_RDLCK;
6683 case NFS4_WRITEW_LT:
6684 file_lock->fl_type = F_WRLCK;
6687 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
6688 status = nfserr_inval;
6692 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
6694 file_lock->fl_owner = (fl_owner_t)lo;
6695 file_lock->fl_pid = current->tgid;
6696 file_lock->fl_flags = FL_POSIX;
6698 file_lock->fl_start = lockt->lt_offset;
6699 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
6701 nfs4_transform_lock_offset(file_lock);
6703 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
6707 if (file_lock->fl_type != F_UNLCK) {
6708 status = nfserr_denied;
6709 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
6713 nfs4_put_stateowner(&lo->lo_owner);
6715 locks_free_lock(file_lock);
6720 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6721 union nfsd4_op_u *u)
6723 struct nfsd4_locku *locku = &u->locku;
6724 struct nfs4_ol_stateid *stp;
6725 struct nfsd_file *nf = NULL;
6726 struct file_lock *file_lock = NULL;
6729 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6731 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
6732 (long long) locku->lu_offset,
6733 (long long) locku->lu_length);
6735 if (check_lock_length(locku->lu_offset, locku->lu_length))
6736 return nfserr_inval;
6738 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
6739 &locku->lu_stateid, NFS4_LOCK_STID,
6743 nf = find_any_file(stp->st_stid.sc_file);
6745 status = nfserr_lock_range;
6748 file_lock = locks_alloc_lock();
6750 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6751 status = nfserr_jukebox;
6755 file_lock->fl_type = F_UNLCK;
6756 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
6757 file_lock->fl_pid = current->tgid;
6758 file_lock->fl_file = nf->nf_file;
6759 file_lock->fl_flags = FL_POSIX;
6760 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6761 file_lock->fl_start = locku->lu_offset;
6763 file_lock->fl_end = last_byte_offset(locku->lu_offset,
6765 nfs4_transform_lock_offset(file_lock);
6767 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, NULL);
6769 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
6772 nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
6776 mutex_unlock(&stp->st_mutex);
6777 nfs4_put_stid(&stp->st_stid);
6779 nfsd4_bump_seqid(cstate, status);
6781 locks_free_lock(file_lock);
6785 status = nfserrno(err);
6791 * true: locks held by lockowner
6792 * false: no locks held by lockowner
6795 check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
6797 struct file_lock *fl;
6799 struct nfsd_file *nf = find_any_file(fp);
6800 struct inode *inode;
6801 struct file_lock_context *flctx;
6804 /* Any valid lock stateid should have some sort of access */
6809 inode = locks_inode(nf->nf_file);
6810 flctx = inode->i_flctx;
6812 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
6813 spin_lock(&flctx->flc_lock);
6814 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
6815 if (fl->fl_owner == (fl_owner_t)lowner) {
6820 spin_unlock(&flctx->flc_lock);
6827 nfsd4_release_lockowner(struct svc_rqst *rqstp,
6828 struct nfsd4_compound_state *cstate,
6829 union nfsd4_op_u *u)
6831 struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner;
6832 clientid_t *clid = &rlockowner->rl_clientid;
6833 struct nfs4_stateowner *sop;
6834 struct nfs4_lockowner *lo = NULL;
6835 struct nfs4_ol_stateid *stp;
6836 struct xdr_netobj *owner = &rlockowner->rl_owner;
6837 unsigned int hashval = ownerstr_hashval(owner);
6839 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6840 struct nfs4_client *clp;
6841 LIST_HEAD (reaplist);
6843 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
6844 clid->cl_boot, clid->cl_id);
6846 status = lookup_clientid(clid, cstate, nn);
6851 /* Find the matching lock stateowner */
6852 spin_lock(&clp->cl_lock);
6853 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
6856 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
6859 /* see if there are still any locks associated with it */
6860 lo = lockowner(sop);
6861 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
6862 if (check_for_locks(stp->st_stid.sc_file, lo)) {
6863 status = nfserr_locks_held;
6864 spin_unlock(&clp->cl_lock);
6869 nfs4_get_stateowner(sop);
6873 spin_unlock(&clp->cl_lock);
6877 unhash_lockowner_locked(lo);
6878 while (!list_empty(&lo->lo_owner.so_stateids)) {
6879 stp = list_first_entry(&lo->lo_owner.so_stateids,
6880 struct nfs4_ol_stateid,
6882 WARN_ON(!unhash_lock_stateid(stp));
6883 put_ol_stateid_locked(stp, &reaplist);
6885 spin_unlock(&clp->cl_lock);
6886 free_ol_stateid_reaplist(&reaplist);
6887 remove_blocked_locks(lo);
6888 nfs4_put_stateowner(&lo->lo_owner);
6893 static inline struct nfs4_client_reclaim *
6896 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
6900 nfs4_has_reclaimed_state(struct xdr_netobj name, struct nfsd_net *nn)
6902 struct nfs4_client_reclaim *crp;
6904 crp = nfsd4_find_reclaim_client(name, nn);
6905 return (crp && crp->cr_clp);
6909 * failure => all reset bets are off, nfserr_no_grace...
6911 * The caller is responsible for freeing name.data if NULL is returned (it
6912 * will be freed in nfs4_remove_reclaim_record in the normal case).
6914 struct nfs4_client_reclaim *
6915 nfs4_client_to_reclaim(struct xdr_netobj name, struct xdr_netobj princhash,
6916 struct nfsd_net *nn)
6918 unsigned int strhashval;
6919 struct nfs4_client_reclaim *crp;
6921 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", name.len, name.data);
6922 crp = alloc_reclaim();
6924 strhashval = clientstr_hashval(name);
6925 INIT_LIST_HEAD(&crp->cr_strhash);
6926 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
6927 crp->cr_name.data = name.data;
6928 crp->cr_name.len = name.len;
6929 crp->cr_princhash.data = princhash.data;
6930 crp->cr_princhash.len = princhash.len;
6932 nn->reclaim_str_hashtbl_size++;
6938 nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
6940 list_del(&crp->cr_strhash);
6941 kfree(crp->cr_name.data);
6942 kfree(crp->cr_princhash.data);
6944 nn->reclaim_str_hashtbl_size--;
6948 nfs4_release_reclaim(struct nfsd_net *nn)
6950 struct nfs4_client_reclaim *crp = NULL;
6953 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6954 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
6955 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
6956 struct nfs4_client_reclaim, cr_strhash);
6957 nfs4_remove_reclaim_record(crp, nn);
6960 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
6964 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
6965 struct nfs4_client_reclaim *
6966 nfsd4_find_reclaim_client(struct xdr_netobj name, struct nfsd_net *nn)
6968 unsigned int strhashval;
6969 struct nfs4_client_reclaim *crp = NULL;
6971 dprintk("NFSD: nfs4_find_reclaim_client for name %.*s\n", name.len, name.data);
6973 strhashval = clientstr_hashval(name);
6974 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
6975 if (compare_blob(&crp->cr_name, &name) == 0) {
6983 * Called from OPEN. Look for clientid in reclaim list.
6986 nfs4_check_open_reclaim(clientid_t *clid,
6987 struct nfsd4_compound_state *cstate,
6988 struct nfsd_net *nn)
6992 /* find clientid in conf_id_hashtbl */
6993 status = lookup_clientid(clid, cstate, nn);
6995 return nfserr_reclaim_bad;
6997 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
6998 return nfserr_no_grace;
7000 if (nfsd4_client_record_check(cstate->clp))
7001 return nfserr_reclaim_bad;
7006 #ifdef CONFIG_NFSD_FAULT_INJECTION
7008 put_client(struct nfs4_client *clp)
7010 atomic_dec(&clp->cl_rpc_users);
7013 static struct nfs4_client *
7014 nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
7016 struct nfs4_client *clp;
7017 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7020 if (!nfsd_netns_ready(nn))
7023 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
7024 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
7031 nfsd_inject_print_clients(void)
7033 struct nfs4_client *clp;
7035 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7037 char buf[INET6_ADDRSTRLEN];
7039 if (!nfsd_netns_ready(nn))
7042 spin_lock(&nn->client_lock);
7043 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
7044 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
7045 pr_info("NFS Client: %s\n", buf);
7048 spin_unlock(&nn->client_lock);
7054 nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
7057 struct nfs4_client *clp;
7058 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7061 if (!nfsd_netns_ready(nn))
7064 spin_lock(&nn->client_lock);
7065 clp = nfsd_find_client(addr, addr_size);
7067 if (mark_client_expired_locked(clp) == nfs_ok)
7072 spin_unlock(&nn->client_lock);
7081 nfsd_inject_forget_clients(u64 max)
7084 struct nfs4_client *clp, *next;
7085 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7087 LIST_HEAD(reaplist);
7089 if (!nfsd_netns_ready(nn))
7092 spin_lock(&nn->client_lock);
7093 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
7094 if (mark_client_expired_locked(clp) == nfs_ok) {
7095 list_add(&clp->cl_lru, &reaplist);
7096 if (max != 0 && ++count >= max)
7100 spin_unlock(&nn->client_lock);
7102 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
7108 static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
7111 char buf[INET6_ADDRSTRLEN];
7112 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
7113 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
7117 nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
7118 struct list_head *collect)
7120 struct nfs4_client *clp = lst->st_stid.sc_client;
7121 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7127 lockdep_assert_held(&nn->client_lock);
7128 atomic_inc(&clp->cl_rpc_users);
7129 list_add(&lst->st_locks, collect);
7132 static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
7133 struct list_head *collect,
7134 bool (*func)(struct nfs4_ol_stateid *))
7136 struct nfs4_openowner *oop;
7137 struct nfs4_ol_stateid *stp, *st_next;
7138 struct nfs4_ol_stateid *lst, *lst_next;
7141 spin_lock(&clp->cl_lock);
7142 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
7143 list_for_each_entry_safe(stp, st_next,
7144 &oop->oo_owner.so_stateids, st_perstateowner) {
7145 list_for_each_entry_safe(lst, lst_next,
7146 &stp->st_locks, st_locks) {
7149 nfsd_inject_add_lock_to_list(lst,
7154 * Despite the fact that these functions deal
7155 * with 64-bit integers for "count", we must
7156 * ensure that it doesn't blow up the
7157 * clp->cl_rpc_users. Throw a warning if we
7158 * start to approach INT_MAX here.
7160 WARN_ON_ONCE(count == (INT_MAX / 2));
7167 spin_unlock(&clp->cl_lock);
7173 nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
7176 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
7180 nfsd_print_client_locks(struct nfs4_client *clp)
7182 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
7183 nfsd_print_count(clp, count, "locked files");
7188 nfsd_inject_print_locks(void)
7190 struct nfs4_client *clp;
7192 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7195 if (!nfsd_netns_ready(nn))
7198 spin_lock(&nn->client_lock);
7199 list_for_each_entry(clp, &nn->client_lru, cl_lru)
7200 count += nfsd_print_client_locks(clp);
7201 spin_unlock(&nn->client_lock);
7207 nfsd_reap_locks(struct list_head *reaplist)
7209 struct nfs4_client *clp;
7210 struct nfs4_ol_stateid *stp, *next;
7212 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
7213 list_del_init(&stp->st_locks);
7214 clp = stp->st_stid.sc_client;
7215 nfs4_put_stid(&stp->st_stid);
7221 nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
7223 unsigned int count = 0;
7224 struct nfs4_client *clp;
7225 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7227 LIST_HEAD(reaplist);
7229 if (!nfsd_netns_ready(nn))
7232 spin_lock(&nn->client_lock);
7233 clp = nfsd_find_client(addr, addr_size);
7235 count = nfsd_collect_client_locks(clp, &reaplist, 0);
7236 spin_unlock(&nn->client_lock);
7237 nfsd_reap_locks(&reaplist);
7242 nfsd_inject_forget_locks(u64 max)
7245 struct nfs4_client *clp;
7246 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7248 LIST_HEAD(reaplist);
7250 if (!nfsd_netns_ready(nn))
7253 spin_lock(&nn->client_lock);
7254 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
7255 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
7256 if (max != 0 && count >= max)
7259 spin_unlock(&nn->client_lock);
7260 nfsd_reap_locks(&reaplist);
7265 nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
7266 struct list_head *collect,
7267 void (*func)(struct nfs4_openowner *))
7269 struct nfs4_openowner *oop, *next;
7270 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7274 lockdep_assert_held(&nn->client_lock);
7276 spin_lock(&clp->cl_lock);
7277 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
7281 atomic_inc(&clp->cl_rpc_users);
7282 list_add(&oop->oo_perclient, collect);
7287 * Despite the fact that these functions deal with
7288 * 64-bit integers for "count", we must ensure that
7289 * it doesn't blow up the clp->cl_rpc_users. Throw a
7290 * warning if we start to approach INT_MAX here.
7292 WARN_ON_ONCE(count == (INT_MAX / 2));
7296 spin_unlock(&clp->cl_lock);
7302 nfsd_print_client_openowners(struct nfs4_client *clp)
7304 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
7306 nfsd_print_count(clp, count, "openowners");
7311 nfsd_collect_client_openowners(struct nfs4_client *clp,
7312 struct list_head *collect, u64 max)
7314 return nfsd_foreach_client_openowner(clp, max, collect,
7315 unhash_openowner_locked);
7319 nfsd_inject_print_openowners(void)
7321 struct nfs4_client *clp;
7323 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7326 if (!nfsd_netns_ready(nn))
7329 spin_lock(&nn->client_lock);
7330 list_for_each_entry(clp, &nn->client_lru, cl_lru)
7331 count += nfsd_print_client_openowners(clp);
7332 spin_unlock(&nn->client_lock);
7338 nfsd_reap_openowners(struct list_head *reaplist)
7340 struct nfs4_client *clp;
7341 struct nfs4_openowner *oop, *next;
7343 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
7344 list_del_init(&oop->oo_perclient);
7345 clp = oop->oo_owner.so_client;
7346 release_openowner(oop);
7352 nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
7355 unsigned int count = 0;
7356 struct nfs4_client *clp;
7357 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7359 LIST_HEAD(reaplist);
7361 if (!nfsd_netns_ready(nn))
7364 spin_lock(&nn->client_lock);
7365 clp = nfsd_find_client(addr, addr_size);
7367 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
7368 spin_unlock(&nn->client_lock);
7369 nfsd_reap_openowners(&reaplist);
7374 nfsd_inject_forget_openowners(u64 max)
7377 struct nfs4_client *clp;
7378 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7380 LIST_HEAD(reaplist);
7382 if (!nfsd_netns_ready(nn))
7385 spin_lock(&nn->client_lock);
7386 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
7387 count += nfsd_collect_client_openowners(clp, &reaplist,
7389 if (max != 0 && count >= max)
7392 spin_unlock(&nn->client_lock);
7393 nfsd_reap_openowners(&reaplist);
7397 static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
7398 struct list_head *victims)
7400 struct nfs4_delegation *dp, *next;
7401 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7405 lockdep_assert_held(&nn->client_lock);
7407 spin_lock(&state_lock);
7408 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
7411 * It's not safe to mess with delegations that have a
7412 * non-zero dl_time. They might have already been broken
7413 * and could be processed by the laundromat outside of
7414 * the state_lock. Just leave them be.
7416 if (dp->dl_time != 0)
7419 atomic_inc(&clp->cl_rpc_users);
7420 WARN_ON(!unhash_delegation_locked(dp));
7421 list_add(&dp->dl_recall_lru, victims);
7425 * Despite the fact that these functions deal with
7426 * 64-bit integers for "count", we must ensure that
7427 * it doesn't blow up the clp->cl_rpc_users. Throw a
7428 * warning if we start to approach INT_MAX here.
7430 WARN_ON_ONCE(count == (INT_MAX / 2));
7434 spin_unlock(&state_lock);
7439 nfsd_print_client_delegations(struct nfs4_client *clp)
7441 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
7443 nfsd_print_count(clp, count, "delegations");
7448 nfsd_inject_print_delegations(void)
7450 struct nfs4_client *clp;
7452 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7455 if (!nfsd_netns_ready(nn))
7458 spin_lock(&nn->client_lock);
7459 list_for_each_entry(clp, &nn->client_lru, cl_lru)
7460 count += nfsd_print_client_delegations(clp);
7461 spin_unlock(&nn->client_lock);
7467 nfsd_forget_delegations(struct list_head *reaplist)
7469 struct nfs4_client *clp;
7470 struct nfs4_delegation *dp, *next;
7472 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
7473 list_del_init(&dp->dl_recall_lru);
7474 clp = dp->dl_stid.sc_client;
7475 revoke_delegation(dp);
7481 nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
7485 struct nfs4_client *clp;
7486 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7488 LIST_HEAD(reaplist);
7490 if (!nfsd_netns_ready(nn))
7493 spin_lock(&nn->client_lock);
7494 clp = nfsd_find_client(addr, addr_size);
7496 count = nfsd_find_all_delegations(clp, 0, &reaplist);
7497 spin_unlock(&nn->client_lock);
7499 nfsd_forget_delegations(&reaplist);
7504 nfsd_inject_forget_delegations(u64 max)
7507 struct nfs4_client *clp;
7508 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7510 LIST_HEAD(reaplist);
7512 if (!nfsd_netns_ready(nn))
7515 spin_lock(&nn->client_lock);
7516 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
7517 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
7518 if (max != 0 && count >= max)
7521 spin_unlock(&nn->client_lock);
7522 nfsd_forget_delegations(&reaplist);
7527 nfsd_recall_delegations(struct list_head *reaplist)
7529 struct nfs4_client *clp;
7530 struct nfs4_delegation *dp, *next;
7532 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
7533 list_del_init(&dp->dl_recall_lru);
7534 clp = dp->dl_stid.sc_client;
7536 * We skipped all entries that had a zero dl_time before,
7537 * so we can now reset the dl_time back to 0. If a delegation
7538 * break comes in now, then it won't make any difference since
7539 * we're recalling it either way.
7541 spin_lock(&state_lock);
7543 spin_unlock(&state_lock);
7544 nfsd_break_one_deleg(dp);
7550 nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
7554 struct nfs4_client *clp;
7555 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7557 LIST_HEAD(reaplist);
7559 if (!nfsd_netns_ready(nn))
7562 spin_lock(&nn->client_lock);
7563 clp = nfsd_find_client(addr, addr_size);
7565 count = nfsd_find_all_delegations(clp, 0, &reaplist);
7566 spin_unlock(&nn->client_lock);
7568 nfsd_recall_delegations(&reaplist);
7573 nfsd_inject_recall_delegations(u64 max)
7576 struct nfs4_client *clp, *next;
7577 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7579 LIST_HEAD(reaplist);
7581 if (!nfsd_netns_ready(nn))
7584 spin_lock(&nn->client_lock);
7585 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
7586 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
7587 if (max != 0 && ++count >= max)
7590 spin_unlock(&nn->client_lock);
7591 nfsd_recall_delegations(&reaplist);
7594 #endif /* CONFIG_NFSD_FAULT_INJECTION */
7597 * Since the lifetime of a delegation isn't limited to that of an open, a
7598 * client may quite reasonably hang on to a delegation as long as it has
7599 * the inode cached. This becomes an obvious problem the first time a
7600 * client's inode cache approaches the size of the server's total memory.
7602 * For now we avoid this problem by imposing a hard limit on the number
7603 * of delegations, which varies according to the server's memory size.
7606 set_max_delegations(void)
7609 * Allow at most 4 delegations per megabyte of RAM. Quick
7610 * estimates suggest that in the worst case (where every delegation
7611 * is for a different inode), a delegation could take about 1.5K,
7612 * giving a worst case usage of about 6% of memory.
7614 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
7617 static int nfs4_state_create_net(struct net *net)
7619 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7622 nn->conf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7623 sizeof(struct list_head),
7625 if (!nn->conf_id_hashtbl)
7627 nn->unconf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7628 sizeof(struct list_head),
7630 if (!nn->unconf_id_hashtbl)
7632 nn->sessionid_hashtbl = kmalloc_array(SESSION_HASH_SIZE,
7633 sizeof(struct list_head),
7635 if (!nn->sessionid_hashtbl)
7638 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7639 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
7640 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
7642 for (i = 0; i < SESSION_HASH_SIZE; i++)
7643 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
7644 nn->conf_name_tree = RB_ROOT;
7645 nn->unconf_name_tree = RB_ROOT;
7646 nn->boot_time = get_seconds();
7647 nn->grace_ended = false;
7648 nn->nfsd4_manager.block_opens = true;
7649 INIT_LIST_HEAD(&nn->nfsd4_manager.list);
7650 INIT_LIST_HEAD(&nn->client_lru);
7651 INIT_LIST_HEAD(&nn->close_lru);
7652 INIT_LIST_HEAD(&nn->del_recall_lru);
7653 spin_lock_init(&nn->client_lock);
7654 spin_lock_init(&nn->s2s_cp_lock);
7655 idr_init(&nn->s2s_cp_stateids);
7657 spin_lock_init(&nn->blocked_locks_lock);
7658 INIT_LIST_HEAD(&nn->blocked_locks_lru);
7660 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
7666 kfree(nn->unconf_id_hashtbl);
7668 kfree(nn->conf_id_hashtbl);
7674 nfs4_state_destroy_net(struct net *net)
7677 struct nfs4_client *clp = NULL;
7678 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7680 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7681 while (!list_empty(&nn->conf_id_hashtbl[i])) {
7682 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7683 destroy_client(clp);
7687 WARN_ON(!list_empty(&nn->blocked_locks_lru));
7689 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7690 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
7691 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7692 destroy_client(clp);
7696 kfree(nn->sessionid_hashtbl);
7697 kfree(nn->unconf_id_hashtbl);
7698 kfree(nn->conf_id_hashtbl);
7703 nfs4_state_start_net(struct net *net)
7705 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7708 ret = nfs4_state_create_net(net);
7711 locks_start_grace(net, &nn->nfsd4_manager);
7712 nfsd4_client_tracking_init(net);
7713 if (nn->track_reclaim_completes && nn->reclaim_str_hashtbl_size == 0)
7715 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %x)\n",
7716 nn->nfsd4_grace, net->ns.inum);
7717 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
7721 printk(KERN_INFO "NFSD: no clients to reclaim, skipping NFSv4 grace period (net %x)\n",
7723 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_lease * HZ);
7724 nfsd4_end_grace(nn);
7728 /* initialization to perform when the nfsd service is started: */
7731 nfs4_state_start(void)
7735 laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
7736 if (laundry_wq == NULL) {
7740 ret = nfsd4_create_callback_queue();
7742 goto out_free_laundry;
7744 set_max_delegations();
7748 destroy_workqueue(laundry_wq);
7754 nfs4_state_shutdown_net(struct net *net)
7756 struct nfs4_delegation *dp = NULL;
7757 struct list_head *pos, *next, reaplist;
7758 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7760 cancel_delayed_work_sync(&nn->laundromat_work);
7761 locks_end_grace(&nn->nfsd4_manager);
7763 INIT_LIST_HEAD(&reaplist);
7764 spin_lock(&state_lock);
7765 list_for_each_safe(pos, next, &nn->del_recall_lru) {
7766 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
7767 WARN_ON(!unhash_delegation_locked(dp));
7768 list_add(&dp->dl_recall_lru, &reaplist);
7770 spin_unlock(&state_lock);
7771 list_for_each_safe(pos, next, &reaplist) {
7772 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
7773 list_del_init(&dp->dl_recall_lru);
7774 destroy_unhashed_deleg(dp);
7777 nfsd4_client_tracking_exit(net);
7778 nfs4_state_destroy_net(net);
7782 nfs4_state_shutdown(void)
7784 destroy_workqueue(laundry_wq);
7785 nfsd4_destroy_callback_queue();
7789 get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7791 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
7792 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
7796 put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7798 if (cstate->minorversion) {
7799 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
7800 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
7805 clear_current_stateid(struct nfsd4_compound_state *cstate)
7807 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
7811 * functions to set current state id
7814 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate,
7815 union nfsd4_op_u *u)
7817 put_stateid(cstate, &u->open_downgrade.od_stateid);
7821 nfsd4_set_openstateid(struct nfsd4_compound_state *cstate,
7822 union nfsd4_op_u *u)
7824 put_stateid(cstate, &u->open.op_stateid);
7828 nfsd4_set_closestateid(struct nfsd4_compound_state *cstate,
7829 union nfsd4_op_u *u)
7831 put_stateid(cstate, &u->close.cl_stateid);
7835 nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate,
7836 union nfsd4_op_u *u)
7838 put_stateid(cstate, &u->lock.lk_resp_stateid);
7842 * functions to consume current state id
7846 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate,
7847 union nfsd4_op_u *u)
7849 get_stateid(cstate, &u->open_downgrade.od_stateid);
7853 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate,
7854 union nfsd4_op_u *u)
7856 get_stateid(cstate, &u->delegreturn.dr_stateid);
7860 nfsd4_get_freestateid(struct nfsd4_compound_state *cstate,
7861 union nfsd4_op_u *u)
7863 get_stateid(cstate, &u->free_stateid.fr_stateid);
7867 nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate,
7868 union nfsd4_op_u *u)
7870 get_stateid(cstate, &u->setattr.sa_stateid);
7874 nfsd4_get_closestateid(struct nfsd4_compound_state *cstate,
7875 union nfsd4_op_u *u)
7877 get_stateid(cstate, &u->close.cl_stateid);
7881 nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate,
7882 union nfsd4_op_u *u)
7884 get_stateid(cstate, &u->locku.lu_stateid);
7888 nfsd4_get_readstateid(struct nfsd4_compound_state *cstate,
7889 union nfsd4_op_u *u)
7891 get_stateid(cstate, &u->read.rd_stateid);
7895 nfsd4_get_writestateid(struct nfsd4_compound_state *cstate,
7896 union nfsd4_op_u *u)
7898 get_stateid(cstate, &u->write.wr_stateid);