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;
500 spin_lock(&f->fi_lock);
501 ret = __nfs4_get_fd(f, O_RDWR);
503 ret = __nfs4_get_fd(f, O_WRONLY);
505 ret = __nfs4_get_fd(f, O_RDONLY);
507 spin_unlock(&f->fi_lock);
511 static struct nfsd_file *find_deleg_file(struct nfs4_file *f)
513 struct nfsd_file *ret = NULL;
515 spin_lock(&f->fi_lock);
516 if (f->fi_deleg_file)
517 ret = nfsd_file_get(f->fi_deleg_file);
518 spin_unlock(&f->fi_lock);
522 static atomic_long_t num_delegations;
523 unsigned long max_delegations;
526 * Open owner state (share locks)
529 /* hash tables for lock and open owners */
530 #define OWNER_HASH_BITS 8
531 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
532 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
534 static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
538 ret = opaque_hashval(ownername->data, ownername->len);
539 return ret & OWNER_HASH_MASK;
542 /* hash table for nfs4_file */
543 #define FILE_HASH_BITS 8
544 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
546 static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
548 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
551 static unsigned int file_hashval(struct knfsd_fh *fh)
553 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
556 static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
559 __nfs4_file_get_access(struct nfs4_file *fp, u32 access)
561 lockdep_assert_held(&fp->fi_lock);
563 if (access & NFS4_SHARE_ACCESS_WRITE)
564 atomic_inc(&fp->fi_access[O_WRONLY]);
565 if (access & NFS4_SHARE_ACCESS_READ)
566 atomic_inc(&fp->fi_access[O_RDONLY]);
570 nfs4_file_get_access(struct nfs4_file *fp, u32 access)
572 lockdep_assert_held(&fp->fi_lock);
574 /* Does this access mode make sense? */
575 if (access & ~NFS4_SHARE_ACCESS_BOTH)
578 /* Does it conflict with a deny mode already set? */
579 if ((access & fp->fi_share_deny) != 0)
580 return nfserr_share_denied;
582 __nfs4_file_get_access(fp, access);
586 static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
588 /* Common case is that there is no deny mode. */
590 /* Does this deny mode make sense? */
591 if (deny & ~NFS4_SHARE_DENY_BOTH)
594 if ((deny & NFS4_SHARE_DENY_READ) &&
595 atomic_read(&fp->fi_access[O_RDONLY]))
596 return nfserr_share_denied;
598 if ((deny & NFS4_SHARE_DENY_WRITE) &&
599 atomic_read(&fp->fi_access[O_WRONLY]))
600 return nfserr_share_denied;
605 static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
607 might_lock(&fp->fi_lock);
609 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
610 struct nfsd_file *f1 = NULL;
611 struct nfsd_file *f2 = NULL;
613 swap(f1, fp->fi_fds[oflag]);
614 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
615 swap(f2, fp->fi_fds[O_RDWR]);
616 spin_unlock(&fp->fi_lock);
624 static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
626 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
628 if (access & NFS4_SHARE_ACCESS_WRITE)
629 __nfs4_file_put_access(fp, O_WRONLY);
630 if (access & NFS4_SHARE_ACCESS_READ)
631 __nfs4_file_put_access(fp, O_RDONLY);
635 * Allocate a new open/delegation state counter. This is needed for
636 * pNFS for proper return on close semantics.
638 * Note that we only allocate it for pNFS-enabled exports, otherwise
639 * all pointers to struct nfs4_clnt_odstate are always NULL.
641 static struct nfs4_clnt_odstate *
642 alloc_clnt_odstate(struct nfs4_client *clp)
644 struct nfs4_clnt_odstate *co;
646 co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
649 refcount_set(&co->co_odcount, 1);
655 hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
657 struct nfs4_file *fp = co->co_file;
659 lockdep_assert_held(&fp->fi_lock);
660 list_add(&co->co_perfile, &fp->fi_clnt_odstate);
664 get_clnt_odstate(struct nfs4_clnt_odstate *co)
667 refcount_inc(&co->co_odcount);
671 put_clnt_odstate(struct nfs4_clnt_odstate *co)
673 struct nfs4_file *fp;
679 if (refcount_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
680 list_del(&co->co_perfile);
681 spin_unlock(&fp->fi_lock);
683 nfsd4_return_all_file_layouts(co->co_client, fp);
684 kmem_cache_free(odstate_slab, co);
688 static struct nfs4_clnt_odstate *
689 find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
691 struct nfs4_clnt_odstate *co;
692 struct nfs4_client *cl;
699 spin_lock(&fp->fi_lock);
700 list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
701 if (co->co_client == cl) {
702 get_clnt_odstate(co);
708 hash_clnt_odstate_locked(new);
710 spin_unlock(&fp->fi_lock);
714 struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab,
715 void (*sc_free)(struct nfs4_stid *))
717 struct nfs4_stid *stid;
720 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
724 idr_preload(GFP_KERNEL);
725 spin_lock(&cl->cl_lock);
726 /* Reserving 0 for start of file in nfsdfs "states" file: */
727 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 1, 0, GFP_NOWAIT);
728 spin_unlock(&cl->cl_lock);
733 stid->sc_free = sc_free;
734 stid->sc_client = cl;
735 stid->sc_stateid.si_opaque.so_id = new_id;
736 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
737 /* Will be incremented before return to client: */
738 refcount_set(&stid->sc_count, 1);
739 spin_lock_init(&stid->sc_lock);
742 * It shouldn't be a problem to reuse an opaque stateid value.
743 * I don't think it is for 4.1. But with 4.0 I worry that, for
744 * example, a stray write retransmission could be accepted by
745 * the server when it should have been rejected. Therefore,
746 * adopt a trick from the sctp code to attempt to maximize the
747 * amount of time until an id is reused, by ensuring they always
748 * "increase" (mod INT_MAX):
752 kmem_cache_free(slab, stid);
757 * Create a unique stateid_t to represent each COPY.
759 int nfs4_init_cp_state(struct nfsd_net *nn, struct nfsd4_copy *copy)
763 idr_preload(GFP_KERNEL);
764 spin_lock(&nn->s2s_cp_lock);
765 new_id = idr_alloc_cyclic(&nn->s2s_cp_stateids, copy, 0, 0, GFP_NOWAIT);
766 spin_unlock(&nn->s2s_cp_lock);
770 copy->cp_stateid.si_opaque.so_id = new_id;
771 copy->cp_stateid.si_opaque.so_clid.cl_boot = nn->boot_time;
772 copy->cp_stateid.si_opaque.so_clid.cl_id = nn->s2s_cp_cl_id;
776 void nfs4_free_cp_state(struct nfsd4_copy *copy)
780 nn = net_generic(copy->cp_clp->net, nfsd_net_id);
781 spin_lock(&nn->s2s_cp_lock);
782 idr_remove(&nn->s2s_cp_stateids, copy->cp_stateid.si_opaque.so_id);
783 spin_unlock(&nn->s2s_cp_lock);
786 static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
788 struct nfs4_stid *stid;
790 stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid);
794 return openlockstateid(stid);
797 static void nfs4_free_deleg(struct nfs4_stid *stid)
799 kmem_cache_free(deleg_slab, stid);
800 atomic_long_dec(&num_delegations);
804 * When we recall a delegation, we should be careful not to hand it
805 * out again straight away.
806 * To ensure this we keep a pair of bloom filters ('new' and 'old')
807 * in which the filehandles of recalled delegations are "stored".
808 * If a filehandle appear in either filter, a delegation is blocked.
809 * When a delegation is recalled, the filehandle is stored in the "new"
811 * Every 30 seconds we swap the filters and clear the "new" one,
812 * unless both are empty of course.
814 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
815 * low 3 bytes as hash-table indices.
817 * 'blocked_delegations_lock', which is always taken in block_delegations(),
818 * is used to manage concurrent access. Testing does not need the lock
819 * except when swapping the two filters.
821 static DEFINE_SPINLOCK(blocked_delegations_lock);
822 static struct bloom_pair {
823 int entries, old_entries;
825 int new; /* index into 'set' */
826 DECLARE_BITMAP(set[2], 256);
827 } blocked_delegations;
829 static int delegation_blocked(struct knfsd_fh *fh)
832 struct bloom_pair *bd = &blocked_delegations;
834 if (bd->entries == 0)
836 if (seconds_since_boot() - bd->swap_time > 30) {
837 spin_lock(&blocked_delegations_lock);
838 if (seconds_since_boot() - bd->swap_time > 30) {
839 bd->entries -= bd->old_entries;
840 bd->old_entries = bd->entries;
841 memset(bd->set[bd->new], 0,
844 bd->swap_time = seconds_since_boot();
846 spin_unlock(&blocked_delegations_lock);
848 hash = jhash(&fh->fh_base, fh->fh_size, 0);
849 if (test_bit(hash&255, bd->set[0]) &&
850 test_bit((hash>>8)&255, bd->set[0]) &&
851 test_bit((hash>>16)&255, bd->set[0]))
854 if (test_bit(hash&255, bd->set[1]) &&
855 test_bit((hash>>8)&255, bd->set[1]) &&
856 test_bit((hash>>16)&255, bd->set[1]))
862 static void block_delegations(struct knfsd_fh *fh)
865 struct bloom_pair *bd = &blocked_delegations;
867 hash = jhash(&fh->fh_base, fh->fh_size, 0);
869 spin_lock(&blocked_delegations_lock);
870 __set_bit(hash&255, bd->set[bd->new]);
871 __set_bit((hash>>8)&255, bd->set[bd->new]);
872 __set_bit((hash>>16)&255, bd->set[bd->new]);
873 if (bd->entries == 0)
874 bd->swap_time = seconds_since_boot();
876 spin_unlock(&blocked_delegations_lock);
879 static struct nfs4_delegation *
880 alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
881 struct svc_fh *current_fh,
882 struct nfs4_clnt_odstate *odstate)
884 struct nfs4_delegation *dp;
887 dprintk("NFSD alloc_init_deleg\n");
888 n = atomic_long_inc_return(&num_delegations);
889 if (n < 0 || n > max_delegations)
891 if (delegation_blocked(¤t_fh->fh_handle))
893 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg));
898 * delegation seqid's are never incremented. The 4.1 special
899 * meaning of seqid 0 isn't meaningful, really, but let's avoid
900 * 0 anyway just for consistency and use 1:
902 dp->dl_stid.sc_stateid.si_generation = 1;
903 INIT_LIST_HEAD(&dp->dl_perfile);
904 INIT_LIST_HEAD(&dp->dl_perclnt);
905 INIT_LIST_HEAD(&dp->dl_recall_lru);
906 dp->dl_clnt_odstate = odstate;
907 get_clnt_odstate(odstate);
908 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
910 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
911 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
913 dp->dl_stid.sc_file = fp;
916 atomic_long_dec(&num_delegations);
921 nfs4_put_stid(struct nfs4_stid *s)
923 struct nfs4_file *fp = s->sc_file;
924 struct nfs4_client *clp = s->sc_client;
926 might_lock(&clp->cl_lock);
928 if (!refcount_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
929 wake_up_all(&close_wq);
932 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
933 spin_unlock(&clp->cl_lock);
940 nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
942 stateid_t *src = &stid->sc_stateid;
944 spin_lock(&stid->sc_lock);
945 if (unlikely(++src->si_generation == 0))
946 src->si_generation = 1;
947 memcpy(dst, src, sizeof(*dst));
948 spin_unlock(&stid->sc_lock);
951 static void put_deleg_file(struct nfs4_file *fp)
953 struct nfsd_file *nf = NULL;
955 spin_lock(&fp->fi_lock);
956 if (--fp->fi_delegees == 0)
957 swap(nf, fp->fi_deleg_file);
958 spin_unlock(&fp->fi_lock);
964 static void nfs4_unlock_deleg_lease(struct nfs4_delegation *dp)
966 struct nfs4_file *fp = dp->dl_stid.sc_file;
967 struct nfsd_file *nf = fp->fi_deleg_file;
969 WARN_ON_ONCE(!fp->fi_delegees);
971 vfs_setlease(nf->nf_file, F_UNLCK, NULL, (void **)&dp);
975 static void destroy_unhashed_deleg(struct nfs4_delegation *dp)
977 put_clnt_odstate(dp->dl_clnt_odstate);
978 nfs4_unlock_deleg_lease(dp);
979 nfs4_put_stid(&dp->dl_stid);
982 void nfs4_unhash_stid(struct nfs4_stid *s)
988 * nfs4_delegation_exists - Discover if this delegation already exists
989 * @clp: a pointer to the nfs4_client we're granting a delegation to
990 * @fp: a pointer to the nfs4_file we're granting a delegation on
993 * On success: true iff an existing delegation is found
997 nfs4_delegation_exists(struct nfs4_client *clp, struct nfs4_file *fp)
999 struct nfs4_delegation *searchdp = NULL;
1000 struct nfs4_client *searchclp = NULL;
1002 lockdep_assert_held(&state_lock);
1003 lockdep_assert_held(&fp->fi_lock);
1005 list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
1006 searchclp = searchdp->dl_stid.sc_client;
1007 if (clp == searchclp) {
1015 * hash_delegation_locked - Add a delegation to the appropriate lists
1016 * @dp: a pointer to the nfs4_delegation we are adding.
1017 * @fp: a pointer to the nfs4_file we're granting a delegation on
1020 * On success: NULL if the delegation was successfully hashed.
1022 * On error: -EAGAIN if one was previously granted to this
1023 * nfs4_client for this nfs4_file. Delegation is not hashed.
1028 hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
1030 struct nfs4_client *clp = dp->dl_stid.sc_client;
1032 lockdep_assert_held(&state_lock);
1033 lockdep_assert_held(&fp->fi_lock);
1035 if (nfs4_delegation_exists(clp, fp))
1037 refcount_inc(&dp->dl_stid.sc_count);
1038 dp->dl_stid.sc_type = NFS4_DELEG_STID;
1039 list_add(&dp->dl_perfile, &fp->fi_delegations);
1040 list_add(&dp->dl_perclnt, &clp->cl_delegations);
1044 static bool delegation_hashed(struct nfs4_delegation *dp)
1046 return !(list_empty(&dp->dl_perfile));
1050 unhash_delegation_locked(struct nfs4_delegation *dp)
1052 struct nfs4_file *fp = dp->dl_stid.sc_file;
1054 lockdep_assert_held(&state_lock);
1056 if (!delegation_hashed(dp))
1059 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
1060 /* Ensure that deleg break won't try to requeue it */
1062 spin_lock(&fp->fi_lock);
1063 list_del_init(&dp->dl_perclnt);
1064 list_del_init(&dp->dl_recall_lru);
1065 list_del_init(&dp->dl_perfile);
1066 spin_unlock(&fp->fi_lock);
1070 static void destroy_delegation(struct nfs4_delegation *dp)
1074 spin_lock(&state_lock);
1075 unhashed = unhash_delegation_locked(dp);
1076 spin_unlock(&state_lock);
1078 destroy_unhashed_deleg(dp);
1081 static void revoke_delegation(struct nfs4_delegation *dp)
1083 struct nfs4_client *clp = dp->dl_stid.sc_client;
1085 WARN_ON(!list_empty(&dp->dl_recall_lru));
1087 if (clp->cl_minorversion) {
1088 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
1089 refcount_inc(&dp->dl_stid.sc_count);
1090 spin_lock(&clp->cl_lock);
1091 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
1092 spin_unlock(&clp->cl_lock);
1094 destroy_unhashed_deleg(dp);
1101 static unsigned int clientid_hashval(u32 id)
1103 return id & CLIENT_HASH_MASK;
1106 static unsigned int clientstr_hashval(struct xdr_netobj name)
1108 return opaque_hashval(name.data, 8) & CLIENT_HASH_MASK;
1112 * We store the NONE, READ, WRITE, and BOTH bits separately in the
1113 * st_{access,deny}_bmap field of the stateid, in order to track not
1114 * only what share bits are currently in force, but also what
1115 * combinations of share bits previous opens have used. This allows us
1116 * to enforce the recommendation of rfc 3530 14.2.19 that the server
1117 * return an error if the client attempt to downgrade to a combination
1118 * of share bits not explicable by closing some of its previous opens.
1120 * XXX: This enforcement is actually incomplete, since we don't keep
1121 * track of access/deny bit combinations; so, e.g., we allow:
1123 * OPEN allow read, deny write
1124 * OPEN allow both, deny none
1125 * DOWNGRADE allow read, deny none
1127 * which we should reject.
1130 bmap_to_share_mode(unsigned long bmap) {
1132 unsigned int access = 0;
1134 for (i = 1; i < 4; i++) {
1135 if (test_bit(i, &bmap))
1141 /* set share access for a given stateid */
1143 set_access(u32 access, struct nfs4_ol_stateid *stp)
1145 unsigned char mask = 1 << access;
1147 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1148 stp->st_access_bmap |= mask;
1151 /* clear share access for a given stateid */
1153 clear_access(u32 access, struct nfs4_ol_stateid *stp)
1155 unsigned char mask = 1 << access;
1157 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1158 stp->st_access_bmap &= ~mask;
1161 /* test whether a given stateid has access */
1163 test_access(u32 access, struct nfs4_ol_stateid *stp)
1165 unsigned char mask = 1 << access;
1167 return (bool)(stp->st_access_bmap & mask);
1170 /* set share deny for a given stateid */
1172 set_deny(u32 deny, struct nfs4_ol_stateid *stp)
1174 unsigned char mask = 1 << deny;
1176 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1177 stp->st_deny_bmap |= mask;
1180 /* clear share deny for a given stateid */
1182 clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
1184 unsigned char mask = 1 << deny;
1186 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1187 stp->st_deny_bmap &= ~mask;
1190 /* test whether a given stateid is denying specific access */
1192 test_deny(u32 deny, struct nfs4_ol_stateid *stp)
1194 unsigned char mask = 1 << deny;
1196 return (bool)(stp->st_deny_bmap & mask);
1199 static int nfs4_access_to_omode(u32 access)
1201 switch (access & NFS4_SHARE_ACCESS_BOTH) {
1202 case NFS4_SHARE_ACCESS_READ:
1204 case NFS4_SHARE_ACCESS_WRITE:
1206 case NFS4_SHARE_ACCESS_BOTH:
1214 * A stateid that had a deny mode associated with it is being released
1215 * or downgraded. Recalculate the deny mode on the file.
1218 recalculate_deny_mode(struct nfs4_file *fp)
1220 struct nfs4_ol_stateid *stp;
1222 spin_lock(&fp->fi_lock);
1223 fp->fi_share_deny = 0;
1224 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
1225 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
1226 spin_unlock(&fp->fi_lock);
1230 reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
1233 bool change = false;
1235 for (i = 1; i < 4; i++) {
1236 if ((i & deny) != i) {
1242 /* Recalculate per-file deny mode if there was a change */
1244 recalculate_deny_mode(stp->st_stid.sc_file);
1247 /* release all access and file references for a given stateid */
1249 release_all_access(struct nfs4_ol_stateid *stp)
1252 struct nfs4_file *fp = stp->st_stid.sc_file;
1254 if (fp && stp->st_deny_bmap != 0)
1255 recalculate_deny_mode(fp);
1257 for (i = 1; i < 4; i++) {
1258 if (test_access(i, stp))
1259 nfs4_file_put_access(stp->st_stid.sc_file, i);
1260 clear_access(i, stp);
1264 static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1266 kfree(sop->so_owner.data);
1267 sop->so_ops->so_free(sop);
1270 static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1272 struct nfs4_client *clp = sop->so_client;
1274 might_lock(&clp->cl_lock);
1276 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
1278 sop->so_ops->so_unhash(sop);
1279 spin_unlock(&clp->cl_lock);
1280 nfs4_free_stateowner(sop);
1284 nfs4_ol_stateid_unhashed(const struct nfs4_ol_stateid *stp)
1286 return list_empty(&stp->st_perfile);
1289 static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
1291 struct nfs4_file *fp = stp->st_stid.sc_file;
1293 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1295 if (list_empty(&stp->st_perfile))
1298 spin_lock(&fp->fi_lock);
1299 list_del_init(&stp->st_perfile);
1300 spin_unlock(&fp->fi_lock);
1301 list_del(&stp->st_perstateowner);
1305 static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
1307 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1309 put_clnt_odstate(stp->st_clnt_odstate);
1310 release_all_access(stp);
1311 if (stp->st_stateowner)
1312 nfs4_put_stateowner(stp->st_stateowner);
1313 kmem_cache_free(stateid_slab, stid);
1316 static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
1318 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1319 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
1320 struct nfsd_file *nf;
1322 nf = find_any_file(stp->st_stid.sc_file);
1324 get_file(nf->nf_file);
1325 filp_close(nf->nf_file, (fl_owner_t)lo);
1328 nfs4_free_ol_stateid(stid);
1332 * Put the persistent reference to an already unhashed generic stateid, while
1333 * holding the cl_lock. If it's the last reference, then put it onto the
1334 * reaplist for later destruction.
1336 static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1337 struct list_head *reaplist)
1339 struct nfs4_stid *s = &stp->st_stid;
1340 struct nfs4_client *clp = s->sc_client;
1342 lockdep_assert_held(&clp->cl_lock);
1344 WARN_ON_ONCE(!list_empty(&stp->st_locks));
1346 if (!refcount_dec_and_test(&s->sc_count)) {
1347 wake_up_all(&close_wq);
1351 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1352 list_add(&stp->st_locks, reaplist);
1355 static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
1357 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1359 if (!unhash_ol_stateid(stp))
1361 list_del_init(&stp->st_locks);
1362 nfs4_unhash_stid(&stp->st_stid);
1366 static void release_lock_stateid(struct nfs4_ol_stateid *stp)
1368 struct nfs4_client *clp = stp->st_stid.sc_client;
1371 spin_lock(&clp->cl_lock);
1372 unhashed = unhash_lock_stateid(stp);
1373 spin_unlock(&clp->cl_lock);
1375 nfs4_put_stid(&stp->st_stid);
1378 static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
1380 struct nfs4_client *clp = lo->lo_owner.so_client;
1382 lockdep_assert_held(&clp->cl_lock);
1384 list_del_init(&lo->lo_owner.so_strhash);
1388 * Free a list of generic stateids that were collected earlier after being
1392 free_ol_stateid_reaplist(struct list_head *reaplist)
1394 struct nfs4_ol_stateid *stp;
1395 struct nfs4_file *fp;
1399 while (!list_empty(reaplist)) {
1400 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1402 list_del(&stp->st_locks);
1403 fp = stp->st_stid.sc_file;
1404 stp->st_stid.sc_free(&stp->st_stid);
1410 static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1411 struct list_head *reaplist)
1413 struct nfs4_ol_stateid *stp;
1415 lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1417 while (!list_empty(&open_stp->st_locks)) {
1418 stp = list_entry(open_stp->st_locks.next,
1419 struct nfs4_ol_stateid, st_locks);
1420 WARN_ON(!unhash_lock_stateid(stp));
1421 put_ol_stateid_locked(stp, reaplist);
1425 static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
1426 struct list_head *reaplist)
1428 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1430 if (!unhash_ol_stateid(stp))
1432 release_open_stateid_locks(stp, reaplist);
1436 static void release_open_stateid(struct nfs4_ol_stateid *stp)
1438 LIST_HEAD(reaplist);
1440 spin_lock(&stp->st_stid.sc_client->cl_lock);
1441 if (unhash_open_stateid(stp, &reaplist))
1442 put_ol_stateid_locked(stp, &reaplist);
1443 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1444 free_ol_stateid_reaplist(&reaplist);
1447 static void unhash_openowner_locked(struct nfs4_openowner *oo)
1449 struct nfs4_client *clp = oo->oo_owner.so_client;
1451 lockdep_assert_held(&clp->cl_lock);
1453 list_del_init(&oo->oo_owner.so_strhash);
1454 list_del_init(&oo->oo_perclient);
1457 static void release_last_closed_stateid(struct nfs4_openowner *oo)
1459 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1461 struct nfs4_ol_stateid *s;
1463 spin_lock(&nn->client_lock);
1464 s = oo->oo_last_closed_stid;
1466 list_del_init(&oo->oo_close_lru);
1467 oo->oo_last_closed_stid = NULL;
1469 spin_unlock(&nn->client_lock);
1471 nfs4_put_stid(&s->st_stid);
1474 static void release_openowner(struct nfs4_openowner *oo)
1476 struct nfs4_ol_stateid *stp;
1477 struct nfs4_client *clp = oo->oo_owner.so_client;
1478 struct list_head reaplist;
1480 INIT_LIST_HEAD(&reaplist);
1482 spin_lock(&clp->cl_lock);
1483 unhash_openowner_locked(oo);
1484 while (!list_empty(&oo->oo_owner.so_stateids)) {
1485 stp = list_first_entry(&oo->oo_owner.so_stateids,
1486 struct nfs4_ol_stateid, st_perstateowner);
1487 if (unhash_open_stateid(stp, &reaplist))
1488 put_ol_stateid_locked(stp, &reaplist);
1490 spin_unlock(&clp->cl_lock);
1491 free_ol_stateid_reaplist(&reaplist);
1492 release_last_closed_stateid(oo);
1493 nfs4_put_stateowner(&oo->oo_owner);
1497 hash_sessionid(struct nfs4_sessionid *sessionid)
1499 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1501 return sid->sequence % SESSION_HASH_SIZE;
1504 #ifdef CONFIG_SUNRPC_DEBUG
1506 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1508 u32 *ptr = (u32 *)(&sessionid->data[0]);
1509 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1513 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1519 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1520 * won't be used for replay.
1522 void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1524 struct nfs4_stateowner *so = cstate->replay_owner;
1526 if (nfserr == nfserr_replay_me)
1529 if (!seqid_mutating_err(ntohl(nfserr))) {
1530 nfsd4_cstate_clear_replay(cstate);
1535 if (so->so_is_open_owner)
1536 release_last_closed_stateid(openowner(so));
1542 gen_sessionid(struct nfsd4_session *ses)
1544 struct nfs4_client *clp = ses->se_client;
1545 struct nfsd4_sessionid *sid;
1547 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1548 sid->clientid = clp->cl_clientid;
1549 sid->sequence = current_sessionid++;
1554 * The protocol defines ca_maxresponssize_cached to include the size of
1555 * the rpc header, but all we need to cache is the data starting after
1556 * the end of the initial SEQUENCE operation--the rest we regenerate
1557 * each time. Therefore we can advertise a ca_maxresponssize_cached
1558 * value that is the number of bytes in our cache plus a few additional
1559 * bytes. In order to stay on the safe side, and not promise more than
1560 * we can cache, those additional bytes must be the minimum possible: 24
1561 * bytes of rpc header (xid through accept state, with AUTH_NULL
1562 * verifier), 12 for the compound header (with zero-length tag), and 44
1563 * for the SEQUENCE op response:
1565 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1568 free_session_slots(struct nfsd4_session *ses)
1572 for (i = 0; i < ses->se_fchannel.maxreqs; i++) {
1573 free_svc_cred(&ses->se_slots[i]->sl_cred);
1574 kfree(ses->se_slots[i]);
1579 * We don't actually need to cache the rpc and session headers, so we
1580 * can allocate a little less for each slot:
1582 static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
1586 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1589 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1590 return size + sizeof(struct nfsd4_slot);
1594 * XXX: If we run out of reserved DRC memory we could (up to a point)
1595 * re-negotiate active sessions and reduce their slot usage to make
1596 * room for new connections. For now we just fail the create session.
1598 static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
1600 u32 slotsize = slot_bytes(ca);
1601 u32 num = ca->maxreqs;
1602 unsigned long avail, total_avail;
1603 unsigned int scale_factor;
1605 spin_lock(&nfsd_drc_lock);
1606 if (nfsd_drc_max_mem > nfsd_drc_mem_used)
1607 total_avail = nfsd_drc_max_mem - nfsd_drc_mem_used;
1609 /* We have handed out more space than we chose in
1610 * set_max_drc() to allow. That isn't really a
1611 * problem as long as that doesn't make us think we
1612 * have lots more due to integer overflow.
1615 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, total_avail);
1617 * Never use more than a fraction of the remaining memory,
1618 * unless it's the only way to give this client a slot.
1619 * The chosen fraction is either 1/8 or 1/number of threads,
1620 * whichever is smaller. This ensures there are adequate
1621 * slots to support multiple clients per thread.
1622 * Give the client one slot even if that would require
1623 * over-allocation--it is better than failure.
1625 scale_factor = max_t(unsigned int, 8, nn->nfsd_serv->sv_nrthreads);
1627 avail = clamp_t(unsigned long, avail, slotsize,
1628 total_avail/scale_factor);
1629 num = min_t(int, num, avail / slotsize);
1630 num = max_t(int, num, 1);
1631 nfsd_drc_mem_used += num * slotsize;
1632 spin_unlock(&nfsd_drc_lock);
1637 static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
1639 int slotsize = slot_bytes(ca);
1641 spin_lock(&nfsd_drc_lock);
1642 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
1643 spin_unlock(&nfsd_drc_lock);
1646 static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1647 struct nfsd4_channel_attrs *battrs)
1649 int numslots = fattrs->maxreqs;
1650 int slotsize = slot_bytes(fattrs);
1651 struct nfsd4_session *new;
1654 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
1655 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1656 mem = numslots * sizeof(struct nfsd4_slot *);
1658 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
1661 /* allocate each struct nfsd4_slot and data cache in one piece */
1662 for (i = 0; i < numslots; i++) {
1663 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
1664 if (!new->se_slots[i])
1668 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1669 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1674 kfree(new->se_slots[i]);
1679 static void free_conn(struct nfsd4_conn *c)
1681 svc_xprt_put(c->cn_xprt);
1685 static void nfsd4_conn_lost(struct svc_xpt_user *u)
1687 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1688 struct nfs4_client *clp = c->cn_session->se_client;
1690 spin_lock(&clp->cl_lock);
1691 if (!list_empty(&c->cn_persession)) {
1692 list_del(&c->cn_persession);
1695 nfsd4_probe_callback(clp);
1696 spin_unlock(&clp->cl_lock);
1699 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
1701 struct nfsd4_conn *conn;
1703 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1706 svc_xprt_get(rqstp->rq_xprt);
1707 conn->cn_xprt = rqstp->rq_xprt;
1708 conn->cn_flags = flags;
1709 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1713 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1715 conn->cn_session = ses;
1716 list_add(&conn->cn_persession, &ses->se_conns);
1719 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1721 struct nfs4_client *clp = ses->se_client;
1723 spin_lock(&clp->cl_lock);
1724 __nfsd4_hash_conn(conn, ses);
1725 spin_unlock(&clp->cl_lock);
1728 static int nfsd4_register_conn(struct nfsd4_conn *conn)
1730 conn->cn_xpt_user.callback = nfsd4_conn_lost;
1731 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
1734 static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
1738 nfsd4_hash_conn(conn, ses);
1739 ret = nfsd4_register_conn(conn);
1741 /* oops; xprt is already down: */
1742 nfsd4_conn_lost(&conn->cn_xpt_user);
1743 /* We may have gained or lost a callback channel: */
1744 nfsd4_probe_callback_sync(ses->se_client);
1747 static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
1749 u32 dir = NFS4_CDFC4_FORE;
1751 if (cses->flags & SESSION4_BACK_CHAN)
1752 dir |= NFS4_CDFC4_BACK;
1753 return alloc_conn(rqstp, dir);
1756 /* must be called under client_lock */
1757 static void nfsd4_del_conns(struct nfsd4_session *s)
1759 struct nfs4_client *clp = s->se_client;
1760 struct nfsd4_conn *c;
1762 spin_lock(&clp->cl_lock);
1763 while (!list_empty(&s->se_conns)) {
1764 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1765 list_del_init(&c->cn_persession);
1766 spin_unlock(&clp->cl_lock);
1768 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1771 spin_lock(&clp->cl_lock);
1773 spin_unlock(&clp->cl_lock);
1776 static void __free_session(struct nfsd4_session *ses)
1778 free_session_slots(ses);
1782 static void free_session(struct nfsd4_session *ses)
1784 nfsd4_del_conns(ses);
1785 nfsd4_put_drc_mem(&ses->se_fchannel);
1786 __free_session(ses);
1789 static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
1792 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1794 new->se_client = clp;
1797 INIT_LIST_HEAD(&new->se_conns);
1799 new->se_cb_seq_nr = 1;
1800 new->se_flags = cses->flags;
1801 new->se_cb_prog = cses->callback_prog;
1802 new->se_cb_sec = cses->cb_sec;
1803 atomic_set(&new->se_ref, 0);
1804 idx = hash_sessionid(&new->se_sessionid);
1805 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
1806 spin_lock(&clp->cl_lock);
1807 list_add(&new->se_perclnt, &clp->cl_sessions);
1808 spin_unlock(&clp->cl_lock);
1811 struct sockaddr *sa = svc_addr(rqstp);
1813 * This is a little silly; with sessions there's no real
1814 * use for the callback address. Use the peer address
1815 * as a reasonable default for now, but consider fixing
1816 * the rpc client not to require an address in the
1819 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1820 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
1824 /* caller must hold client_lock */
1825 static struct nfsd4_session *
1826 __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
1828 struct nfsd4_session *elem;
1830 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1832 lockdep_assert_held(&nn->client_lock);
1834 dump_sessionid(__func__, sessionid);
1835 idx = hash_sessionid(sessionid);
1836 /* Search in the appropriate list */
1837 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
1838 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1839 NFS4_MAX_SESSIONID_LEN)) {
1844 dprintk("%s: session not found\n", __func__);
1848 static struct nfsd4_session *
1849 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1852 struct nfsd4_session *session;
1853 __be32 status = nfserr_badsession;
1855 session = __find_in_sessionid_hashtbl(sessionid, net);
1858 status = nfsd4_get_session_locked(session);
1866 /* caller must hold client_lock */
1868 unhash_session(struct nfsd4_session *ses)
1870 struct nfs4_client *clp = ses->se_client;
1871 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1873 lockdep_assert_held(&nn->client_lock);
1875 list_del(&ses->se_hash);
1876 spin_lock(&ses->se_client->cl_lock);
1877 list_del(&ses->se_perclnt);
1878 spin_unlock(&ses->se_client->cl_lock);
1881 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1883 STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
1886 * We're assuming the clid was not given out from a boot
1887 * precisely 2^32 (about 136 years) before this one. That seems
1888 * a safe assumption:
1890 if (clid->cl_boot == (u32)nn->boot_time)
1892 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
1893 clid->cl_boot, clid->cl_id, nn->boot_time);
1898 * XXX Should we use a slab cache ?
1899 * This type of memory management is somewhat inefficient, but we use it
1900 * anyway since SETCLIENTID is not a common operation.
1902 static struct nfs4_client *alloc_client(struct xdr_netobj name)
1904 struct nfs4_client *clp;
1907 clp = kmem_cache_zalloc(client_slab, GFP_KERNEL);
1910 xdr_netobj_dup(&clp->cl_name, &name, GFP_KERNEL);
1911 if (clp->cl_name.data == NULL)
1913 clp->cl_ownerstr_hashtbl = kmalloc_array(OWNER_HASH_SIZE,
1914 sizeof(struct list_head),
1916 if (!clp->cl_ownerstr_hashtbl)
1917 goto err_no_hashtbl;
1918 for (i = 0; i < OWNER_HASH_SIZE; i++)
1919 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
1920 INIT_LIST_HEAD(&clp->cl_sessions);
1921 idr_init(&clp->cl_stateids);
1922 atomic_set(&clp->cl_rpc_users, 0);
1923 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1924 INIT_LIST_HEAD(&clp->cl_idhash);
1925 INIT_LIST_HEAD(&clp->cl_openowners);
1926 INIT_LIST_HEAD(&clp->cl_delegations);
1927 INIT_LIST_HEAD(&clp->cl_lru);
1928 INIT_LIST_HEAD(&clp->cl_revoked);
1929 #ifdef CONFIG_NFSD_PNFS
1930 INIT_LIST_HEAD(&clp->cl_lo_states);
1932 INIT_LIST_HEAD(&clp->async_copies);
1933 spin_lock_init(&clp->async_lock);
1934 spin_lock_init(&clp->cl_lock);
1935 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1938 kfree(clp->cl_name.data);
1940 kmem_cache_free(client_slab, clp);
1944 static void __free_client(struct kref *k)
1946 struct nfsdfs_client *c = container_of(k, struct nfsdfs_client, cl_ref);
1947 struct nfs4_client *clp = container_of(c, struct nfs4_client, cl_nfsdfs);
1949 free_svc_cred(&clp->cl_cred);
1950 kfree(clp->cl_ownerstr_hashtbl);
1951 kfree(clp->cl_name.data);
1952 kfree(clp->cl_nii_domain.data);
1953 kfree(clp->cl_nii_name.data);
1954 idr_destroy(&clp->cl_stateids);
1955 kmem_cache_free(client_slab, clp);
1958 static void drop_client(struct nfs4_client *clp)
1960 kref_put(&clp->cl_nfsdfs.cl_ref, __free_client);
1964 free_client(struct nfs4_client *clp)
1966 while (!list_empty(&clp->cl_sessions)) {
1967 struct nfsd4_session *ses;
1968 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1970 list_del(&ses->se_perclnt);
1971 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1974 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
1975 if (clp->cl_nfsd_dentry) {
1976 nfsd_client_rmdir(clp->cl_nfsd_dentry);
1977 clp->cl_nfsd_dentry = NULL;
1978 wake_up_all(&expiry_wq);
1983 /* must be called under the client_lock */
1985 unhash_client_locked(struct nfs4_client *clp)
1987 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1988 struct nfsd4_session *ses;
1990 lockdep_assert_held(&nn->client_lock);
1992 /* Mark the client as expired! */
1994 /* Make it invisible */
1995 if (!list_empty(&clp->cl_idhash)) {
1996 list_del_init(&clp->cl_idhash);
1997 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1998 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
2000 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
2002 list_del_init(&clp->cl_lru);
2003 spin_lock(&clp->cl_lock);
2004 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
2005 list_del_init(&ses->se_hash);
2006 spin_unlock(&clp->cl_lock);
2010 unhash_client(struct nfs4_client *clp)
2012 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2014 spin_lock(&nn->client_lock);
2015 unhash_client_locked(clp);
2016 spin_unlock(&nn->client_lock);
2019 static __be32 mark_client_expired_locked(struct nfs4_client *clp)
2021 if (atomic_read(&clp->cl_rpc_users))
2022 return nfserr_jukebox;
2023 unhash_client_locked(clp);
2028 __destroy_client(struct nfs4_client *clp)
2031 struct nfs4_openowner *oo;
2032 struct nfs4_delegation *dp;
2033 struct list_head reaplist;
2035 INIT_LIST_HEAD(&reaplist);
2036 spin_lock(&state_lock);
2037 while (!list_empty(&clp->cl_delegations)) {
2038 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
2039 WARN_ON(!unhash_delegation_locked(dp));
2040 list_add(&dp->dl_recall_lru, &reaplist);
2042 spin_unlock(&state_lock);
2043 while (!list_empty(&reaplist)) {
2044 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
2045 list_del_init(&dp->dl_recall_lru);
2046 destroy_unhashed_deleg(dp);
2048 while (!list_empty(&clp->cl_revoked)) {
2049 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
2050 list_del_init(&dp->dl_recall_lru);
2051 nfs4_put_stid(&dp->dl_stid);
2053 while (!list_empty(&clp->cl_openowners)) {
2054 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
2055 nfs4_get_stateowner(&oo->oo_owner);
2056 release_openowner(oo);
2058 for (i = 0; i < OWNER_HASH_SIZE; i++) {
2059 struct nfs4_stateowner *so, *tmp;
2061 list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i],
2063 /* Should be no openowners at this point */
2064 WARN_ON_ONCE(so->so_is_open_owner);
2065 remove_blocked_locks(lockowner(so));
2068 nfsd4_return_all_client_layouts(clp);
2069 nfsd4_shutdown_copy(clp);
2070 nfsd4_shutdown_callback(clp);
2071 if (clp->cl_cb_conn.cb_xprt)
2072 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
2074 wake_up_all(&expiry_wq);
2078 destroy_client(struct nfs4_client *clp)
2081 __destroy_client(clp);
2084 static void inc_reclaim_complete(struct nfs4_client *clp)
2086 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2088 if (!nn->track_reclaim_completes)
2090 if (!nfsd4_find_reclaim_client(clp->cl_name, nn))
2092 if (atomic_inc_return(&nn->nr_reclaim_complete) ==
2093 nn->reclaim_str_hashtbl_size) {
2094 printk(KERN_INFO "NFSD: all clients done reclaiming, ending NFSv4 grace period (net %x)\n",
2096 nfsd4_end_grace(nn);
2100 static void expire_client(struct nfs4_client *clp)
2103 nfsd4_client_record_remove(clp);
2104 __destroy_client(clp);
2107 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
2109 memcpy(target->cl_verifier.data, source->data,
2110 sizeof(target->cl_verifier.data));
2113 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
2115 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
2116 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
2119 static int copy_cred(struct svc_cred *target, struct svc_cred *source)
2121 target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL);
2122 target->cr_raw_principal = kstrdup(source->cr_raw_principal,
2124 target->cr_targ_princ = kstrdup(source->cr_targ_princ, GFP_KERNEL);
2125 if ((source->cr_principal && !target->cr_principal) ||
2126 (source->cr_raw_principal && !target->cr_raw_principal) ||
2127 (source->cr_targ_princ && !target->cr_targ_princ))
2130 target->cr_flavor = source->cr_flavor;
2131 target->cr_uid = source->cr_uid;
2132 target->cr_gid = source->cr_gid;
2133 target->cr_group_info = source->cr_group_info;
2134 get_group_info(target->cr_group_info);
2135 target->cr_gss_mech = source->cr_gss_mech;
2136 if (source->cr_gss_mech)
2137 gss_mech_get(source->cr_gss_mech);
2142 compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
2144 if (o1->len < o2->len)
2146 if (o1->len > o2->len)
2148 return memcmp(o1->data, o2->data, o1->len);
2152 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
2154 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
2158 same_clid(clientid_t *cl1, clientid_t *cl2)
2160 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
2163 static bool groups_equal(struct group_info *g1, struct group_info *g2)
2167 if (g1->ngroups != g2->ngroups)
2169 for (i=0; i<g1->ngroups; i++)
2170 if (!gid_eq(g1->gid[i], g2->gid[i]))
2176 * RFC 3530 language requires clid_inuse be returned when the
2177 * "principal" associated with a requests differs from that previously
2178 * used. We use uid, gid's, and gss principal string as our best
2179 * approximation. We also don't want to allow non-gss use of a client
2180 * established using gss: in theory cr_principal should catch that
2181 * change, but in practice cr_principal can be null even in the gss case
2182 * since gssd doesn't always pass down a principal string.
2184 static bool is_gss_cred(struct svc_cred *cr)
2186 /* Is cr_flavor one of the gss "pseudoflavors"?: */
2187 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
2192 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
2194 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
2195 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
2196 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
2197 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
2199 /* XXX: check that cr_targ_princ fields match ? */
2200 if (cr1->cr_principal == cr2->cr_principal)
2202 if (!cr1->cr_principal || !cr2->cr_principal)
2204 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
2207 static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
2209 struct svc_cred *cr = &rqstp->rq_cred;
2212 if (!cr->cr_gss_mech)
2214 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
2215 return service == RPC_GSS_SVC_INTEGRITY ||
2216 service == RPC_GSS_SVC_PRIVACY;
2219 bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
2221 struct svc_cred *cr = &rqstp->rq_cred;
2223 if (!cl->cl_mach_cred)
2225 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
2227 if (!svc_rqst_integrity_protected(rqstp))
2229 if (cl->cl_cred.cr_raw_principal)
2230 return 0 == strcmp(cl->cl_cred.cr_raw_principal,
2231 cr->cr_raw_principal);
2232 if (!cr->cr_principal)
2234 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
2237 static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
2242 * This is opaque to client, so no need to byte-swap. Use
2243 * __force to keep sparse happy
2245 verf[0] = (__force __be32)get_seconds();
2246 verf[1] = (__force __be32)nn->clverifier_counter++;
2247 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
2250 static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
2252 clp->cl_clientid.cl_boot = nn->boot_time;
2253 clp->cl_clientid.cl_id = nn->clientid_counter++;
2254 gen_confirm(clp, nn);
2257 static struct nfs4_stid *
2258 find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
2260 struct nfs4_stid *ret;
2262 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
2263 if (!ret || !ret->sc_type)
2268 static struct nfs4_stid *
2269 find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
2271 struct nfs4_stid *s;
2273 spin_lock(&cl->cl_lock);
2274 s = find_stateid_locked(cl, t);
2276 if (typemask & s->sc_type)
2277 refcount_inc(&s->sc_count);
2281 spin_unlock(&cl->cl_lock);
2285 static struct nfs4_client *get_nfsdfs_clp(struct inode *inode)
2287 struct nfsdfs_client *nc;
2288 nc = get_nfsdfs_client(inode);
2291 return container_of(nc, struct nfs4_client, cl_nfsdfs);
2294 static void seq_quote_mem(struct seq_file *m, char *data, int len)
2296 seq_printf(m, "\"");
2297 seq_escape_mem_ascii(m, data, len);
2298 seq_printf(m, "\"");
2301 static int client_info_show(struct seq_file *m, void *v)
2303 struct inode *inode = m->private;
2304 struct nfs4_client *clp;
2307 clp = get_nfsdfs_clp(inode);
2310 memcpy(&clid, &clp->cl_clientid, sizeof(clid));
2311 seq_printf(m, "clientid: 0x%llx\n", clid);
2312 seq_printf(m, "address: \"%pISpc\"\n", (struct sockaddr *)&clp->cl_addr);
2313 seq_printf(m, "name: ");
2314 seq_quote_mem(m, clp->cl_name.data, clp->cl_name.len);
2315 seq_printf(m, "\nminor version: %d\n", clp->cl_minorversion);
2316 if (clp->cl_nii_domain.data) {
2317 seq_printf(m, "Implementation domain: ");
2318 seq_quote_mem(m, clp->cl_nii_domain.data,
2319 clp->cl_nii_domain.len);
2320 seq_printf(m, "\nImplementation name: ");
2321 seq_quote_mem(m, clp->cl_nii_name.data, clp->cl_nii_name.len);
2322 seq_printf(m, "\nImplementation time: [%ld, %ld]\n",
2323 clp->cl_nii_time.tv_sec, clp->cl_nii_time.tv_nsec);
2330 static int client_info_open(struct inode *inode, struct file *file)
2332 return single_open(file, client_info_show, inode);
2335 static const struct file_operations client_info_fops = {
2336 .open = client_info_open,
2338 .llseek = seq_lseek,
2339 .release = single_release,
2342 static void *states_start(struct seq_file *s, loff_t *pos)
2343 __acquires(&clp->cl_lock)
2345 struct nfs4_client *clp = s->private;
2346 unsigned long id = *pos;
2349 spin_lock(&clp->cl_lock);
2350 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2355 static void *states_next(struct seq_file *s, void *v, loff_t *pos)
2357 struct nfs4_client *clp = s->private;
2358 unsigned long id = *pos;
2363 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2368 static void states_stop(struct seq_file *s, void *v)
2369 __releases(&clp->cl_lock)
2371 struct nfs4_client *clp = s->private;
2373 spin_unlock(&clp->cl_lock);
2376 static void nfs4_show_superblock(struct seq_file *s, struct nfsd_file *f)
2378 struct inode *inode = f->nf_inode;
2380 seq_printf(s, "superblock: \"%02x:%02x:%ld\"",
2381 MAJOR(inode->i_sb->s_dev),
2382 MINOR(inode->i_sb->s_dev),
2386 static void nfs4_show_owner(struct seq_file *s, struct nfs4_stateowner *oo)
2388 seq_printf(s, "owner: ");
2389 seq_quote_mem(s, oo->so_owner.data, oo->so_owner.len);
2392 static int nfs4_show_open(struct seq_file *s, struct nfs4_stid *st)
2394 struct nfs4_ol_stateid *ols;
2395 struct nfs4_file *nf;
2396 struct nfsd_file *file;
2397 struct nfs4_stateowner *oo;
2398 unsigned int access, deny;
2400 if (st->sc_type != NFS4_OPEN_STID && st->sc_type != NFS4_LOCK_STID)
2401 return 0; /* XXX: or SEQ_SKIP? */
2402 ols = openlockstateid(st);
2403 oo = ols->st_stateowner;
2405 file = find_any_file(nf);
2409 seq_printf(s, "- 0x%16phN: { type: open, ", &st->sc_stateid);
2411 access = bmap_to_share_mode(ols->st_access_bmap);
2412 deny = bmap_to_share_mode(ols->st_deny_bmap);
2414 seq_printf(s, "access: \%s\%s, ",
2415 access & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2416 access & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2417 seq_printf(s, "deny: \%s\%s, ",
2418 deny & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2419 deny & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2421 nfs4_show_superblock(s, file);
2422 seq_printf(s, ", ");
2423 nfs4_show_owner(s, oo);
2424 seq_printf(s, " }\n");
2425 nfsd_file_put(file);
2430 static int nfs4_show_lock(struct seq_file *s, struct nfs4_stid *st)
2432 struct nfs4_ol_stateid *ols;
2433 struct nfs4_file *nf;
2434 struct nfsd_file *file;
2435 struct nfs4_stateowner *oo;
2437 ols = openlockstateid(st);
2438 oo = ols->st_stateowner;
2440 file = find_any_file(nf);
2444 seq_printf(s, "- 0x%16phN: { type: lock, ", &st->sc_stateid);
2447 * Note: a lock stateid isn't really the same thing as a lock,
2448 * it's the locking state held by one owner on a file, and there
2449 * may be multiple (or no) lock ranges associated with it.
2450 * (Same for the matter is true of open stateids.)
2453 nfs4_show_superblock(s, file);
2454 /* XXX: open stateid? */
2455 seq_printf(s, ", ");
2456 nfs4_show_owner(s, oo);
2457 seq_printf(s, " }\n");
2458 nfsd_file_put(file);
2463 static int nfs4_show_deleg(struct seq_file *s, struct nfs4_stid *st)
2465 struct nfs4_delegation *ds;
2466 struct nfs4_file *nf;
2467 struct nfsd_file *file;
2469 ds = delegstateid(st);
2471 file = find_deleg_file(nf);
2475 seq_printf(s, "- 0x%16phN: { type: deleg, ", &st->sc_stateid);
2477 /* Kinda dead code as long as we only support read delegs: */
2478 seq_printf(s, "access: %s, ",
2479 ds->dl_type == NFS4_OPEN_DELEGATE_READ ? "r" : "w");
2481 /* XXX: lease time, whether it's being recalled. */
2483 nfs4_show_superblock(s, file);
2484 seq_printf(s, " }\n");
2485 nfsd_file_put(file);
2490 static int nfs4_show_layout(struct seq_file *s, struct nfs4_stid *st)
2492 struct nfs4_layout_stateid *ls;
2493 struct nfsd_file *file;
2495 ls = container_of(st, struct nfs4_layout_stateid, ls_stid);
2498 seq_printf(s, "- 0x%16phN: { type: layout, ", &st->sc_stateid);
2500 /* XXX: What else would be useful? */
2502 nfs4_show_superblock(s, file);
2503 seq_printf(s, " }\n");
2508 static int states_show(struct seq_file *s, void *v)
2510 struct nfs4_stid *st = v;
2512 switch (st->sc_type) {
2513 case NFS4_OPEN_STID:
2514 return nfs4_show_open(s, st);
2515 case NFS4_LOCK_STID:
2516 return nfs4_show_lock(s, st);
2517 case NFS4_DELEG_STID:
2518 return nfs4_show_deleg(s, st);
2519 case NFS4_LAYOUT_STID:
2520 return nfs4_show_layout(s, st);
2522 return 0; /* XXX: or SEQ_SKIP? */
2524 /* XXX: copy stateids? */
2527 static struct seq_operations states_seq_ops = {
2528 .start = states_start,
2529 .next = states_next,
2530 .stop = states_stop,
2534 static int client_states_open(struct inode *inode, struct file *file)
2537 struct nfs4_client *clp;
2540 clp = get_nfsdfs_clp(inode);
2544 ret = seq_open(file, &states_seq_ops);
2547 s = file->private_data;
2552 static int client_opens_release(struct inode *inode, struct file *file)
2554 struct seq_file *m = file->private_data;
2555 struct nfs4_client *clp = m->private;
2557 /* XXX: alternatively, we could get/drop in seq start/stop */
2562 static const struct file_operations client_states_fops = {
2563 .open = client_states_open,
2565 .llseek = seq_lseek,
2566 .release = client_opens_release,
2570 * Normally we refuse to destroy clients that are in use, but here the
2571 * administrator is telling us to just do it. We also want to wait
2572 * so the caller has a guarantee that the client's locks are gone by
2573 * the time the write returns:
2575 static void force_expire_client(struct nfs4_client *clp)
2577 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2578 bool already_expired;
2580 spin_lock(&nn->client_lock);
2582 spin_unlock(&nn->client_lock);
2584 wait_event(expiry_wq, atomic_read(&clp->cl_rpc_users) == 0);
2585 spin_lock(&nn->client_lock);
2586 already_expired = list_empty(&clp->cl_lru);
2587 if (!already_expired)
2588 unhash_client_locked(clp);
2589 spin_unlock(&nn->client_lock);
2591 if (!already_expired)
2594 wait_event(expiry_wq, clp->cl_nfsd_dentry == NULL);
2597 static ssize_t client_ctl_write(struct file *file, const char __user *buf,
2598 size_t size, loff_t *pos)
2601 struct nfs4_client *clp;
2603 data = simple_transaction_get(file, buf, size);
2605 return PTR_ERR(data);
2606 if (size != 7 || 0 != memcmp(data, "expire\n", 7))
2608 clp = get_nfsdfs_clp(file_inode(file));
2611 force_expire_client(clp);
2616 static const struct file_operations client_ctl_fops = {
2617 .write = client_ctl_write,
2618 .release = simple_transaction_release,
2621 static const struct tree_descr client_files[] = {
2622 [0] = {"info", &client_info_fops, S_IRUSR},
2623 [1] = {"states", &client_states_fops, S_IRUSR},
2624 [2] = {"ctl", &client_ctl_fops, S_IRUSR|S_IWUSR},
2628 static struct nfs4_client *create_client(struct xdr_netobj name,
2629 struct svc_rqst *rqstp, nfs4_verifier *verf)
2631 struct nfs4_client *clp;
2632 struct sockaddr *sa = svc_addr(rqstp);
2634 struct net *net = SVC_NET(rqstp);
2635 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2637 clp = alloc_client(name);
2641 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
2647 kref_init(&clp->cl_nfsdfs.cl_ref);
2648 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
2649 clp->cl_time = get_seconds();
2650 clear_bit(0, &clp->cl_cb_slot_busy);
2651 copy_verf(clp, verf);
2652 memcpy(&clp->cl_addr, sa, sizeof(struct sockaddr_storage));
2653 clp->cl_cb_session = NULL;
2655 clp->cl_nfsd_dentry = nfsd_client_mkdir(nn, &clp->cl_nfsdfs,
2656 clp->cl_clientid.cl_id - nn->clientid_base,
2658 if (!clp->cl_nfsd_dentry) {
2666 add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
2668 struct rb_node **new = &(root->rb_node), *parent = NULL;
2669 struct nfs4_client *clp;
2672 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
2675 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
2676 new = &((*new)->rb_left);
2678 new = &((*new)->rb_right);
2681 rb_link_node(&new_clp->cl_namenode, parent, new);
2682 rb_insert_color(&new_clp->cl_namenode, root);
2685 static struct nfs4_client *
2686 find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2689 struct rb_node *node = root->rb_node;
2690 struct nfs4_client *clp;
2693 clp = rb_entry(node, struct nfs4_client, cl_namenode);
2694 cmp = compare_blob(&clp->cl_name, name);
2696 node = node->rb_left;
2698 node = node->rb_right;
2706 add_to_unconfirmed(struct nfs4_client *clp)
2708 unsigned int idhashval;
2709 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2711 lockdep_assert_held(&nn->client_lock);
2713 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2714 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
2715 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
2716 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
2717 renew_client_locked(clp);
2721 move_to_confirmed(struct nfs4_client *clp)
2723 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
2724 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2726 lockdep_assert_held(&nn->client_lock);
2728 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
2729 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
2730 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
2731 add_clp_to_name_tree(clp, &nn->conf_name_tree);
2732 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2733 renew_client_locked(clp);
2736 static struct nfs4_client *
2737 find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
2739 struct nfs4_client *clp;
2740 unsigned int idhashval = clientid_hashval(clid->cl_id);
2742 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
2743 if (same_clid(&clp->cl_clientid, clid)) {
2744 if ((bool)clp->cl_minorversion != sessions)
2746 renew_client_locked(clp);
2753 static struct nfs4_client *
2754 find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2756 struct list_head *tbl = nn->conf_id_hashtbl;
2758 lockdep_assert_held(&nn->client_lock);
2759 return find_client_in_id_table(tbl, clid, sessions);
2762 static struct nfs4_client *
2763 find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2765 struct list_head *tbl = nn->unconf_id_hashtbl;
2767 lockdep_assert_held(&nn->client_lock);
2768 return find_client_in_id_table(tbl, clid, sessions);
2771 static bool clp_used_exchangeid(struct nfs4_client *clp)
2773 return clp->cl_exchange_flags != 0;
2776 static struct nfs4_client *
2777 find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
2779 lockdep_assert_held(&nn->client_lock);
2780 return find_clp_in_name_tree(name, &nn->conf_name_tree);
2783 static struct nfs4_client *
2784 find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
2786 lockdep_assert_held(&nn->client_lock);
2787 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
2791 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
2793 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
2794 struct sockaddr *sa = svc_addr(rqstp);
2795 u32 scopeid = rpc_get_scope_id(sa);
2796 unsigned short expected_family;
2798 /* Currently, we only support tcp and tcp6 for the callback channel */
2799 if (se->se_callback_netid_len == 3 &&
2800 !memcmp(se->se_callback_netid_val, "tcp", 3))
2801 expected_family = AF_INET;
2802 else if (se->se_callback_netid_len == 4 &&
2803 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2804 expected_family = AF_INET6;
2808 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
2809 se->se_callback_addr_len,
2810 (struct sockaddr *)&conn->cb_addr,
2811 sizeof(conn->cb_addr));
2813 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
2816 if (conn->cb_addr.ss_family == AF_INET6)
2817 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
2819 conn->cb_prog = se->se_callback_prog;
2820 conn->cb_ident = se->se_callback_ident;
2821 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
2824 conn->cb_addr.ss_family = AF_UNSPEC;
2825 conn->cb_addrlen = 0;
2826 dprintk("NFSD: this client (clientid %08x/%08x) "
2827 "will not receive delegations\n",
2828 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2834 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
2837 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2839 struct xdr_buf *buf = resp->xdr.buf;
2840 struct nfsd4_slot *slot = resp->cstate.slot;
2843 dprintk("--> %s slot %p\n", __func__, slot);
2845 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
2846 slot->sl_opcnt = resp->opcnt;
2847 slot->sl_status = resp->cstate.status;
2848 free_svc_cred(&slot->sl_cred);
2849 copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred);
2851 if (!nfsd4_cache_this(resp)) {
2852 slot->sl_flags &= ~NFSD4_SLOT_CACHED;
2855 slot->sl_flags |= NFSD4_SLOT_CACHED;
2857 base = resp->cstate.data_offset;
2858 slot->sl_datalen = buf->len - base;
2859 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
2860 WARN(1, "%s: sessions DRC could not cache compound\n",
2866 * Encode the replay sequence operation from the slot values.
2867 * If cachethis is FALSE encode the uncached rep error on the next
2868 * operation which sets resp->p and increments resp->opcnt for
2869 * nfs4svc_encode_compoundres.
2873 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2874 struct nfsd4_compoundres *resp)
2876 struct nfsd4_op *op;
2877 struct nfsd4_slot *slot = resp->cstate.slot;
2879 /* Encode the replayed sequence operation */
2880 op = &args->ops[resp->opcnt - 1];
2881 nfsd4_encode_operation(resp, op);
2883 if (slot->sl_flags & NFSD4_SLOT_CACHED)
2885 if (args->opcnt == 1) {
2887 * The original operation wasn't a solo sequence--we
2888 * always cache those--so this retry must not match the
2891 op->status = nfserr_seq_false_retry;
2893 op = &args->ops[resp->opcnt++];
2894 op->status = nfserr_retry_uncached_rep;
2895 nfsd4_encode_operation(resp, op);
2901 * The sequence operation is not cached because we can use the slot and
2905 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2906 struct nfsd4_sequence *seq)
2908 struct nfsd4_slot *slot = resp->cstate.slot;
2909 struct xdr_stream *xdr = &resp->xdr;
2913 dprintk("--> %s slot %p\n", __func__, slot);
2915 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
2919 p = xdr_reserve_space(xdr, slot->sl_datalen);
2922 return nfserr_serverfault;
2924 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2925 xdr_commit_encode(xdr);
2927 resp->opcnt = slot->sl_opcnt;
2928 return slot->sl_status;
2932 * Set the exchange_id flags returned by the server.
2935 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2937 #ifdef CONFIG_NFSD_PNFS
2938 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
2940 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
2943 /* Referrals are supported, Migration is not. */
2944 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2946 /* set the wire flags to return to client. */
2947 clid->flags = new->cl_exchange_flags;
2950 static bool client_has_openowners(struct nfs4_client *clp)
2952 struct nfs4_openowner *oo;
2954 list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
2955 if (!list_empty(&oo->oo_owner.so_stateids))
2961 static bool client_has_state(struct nfs4_client *clp)
2963 return client_has_openowners(clp)
2964 #ifdef CONFIG_NFSD_PNFS
2965 || !list_empty(&clp->cl_lo_states)
2967 || !list_empty(&clp->cl_delegations)
2968 || !list_empty(&clp->cl_sessions)
2969 || !list_empty(&clp->async_copies);
2972 static __be32 copy_impl_id(struct nfs4_client *clp,
2973 struct nfsd4_exchange_id *exid)
2975 if (!exid->nii_domain.data)
2977 xdr_netobj_dup(&clp->cl_nii_domain, &exid->nii_domain, GFP_KERNEL);
2978 if (!clp->cl_nii_domain.data)
2979 return nfserr_jukebox;
2980 xdr_netobj_dup(&clp->cl_nii_name, &exid->nii_name, GFP_KERNEL);
2981 if (!clp->cl_nii_name.data)
2982 return nfserr_jukebox;
2983 clp->cl_nii_time.tv_sec = exid->nii_time.tv_sec;
2984 clp->cl_nii_time.tv_nsec = exid->nii_time.tv_nsec;
2989 nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2990 union nfsd4_op_u *u)
2992 struct nfsd4_exchange_id *exid = &u->exchange_id;
2993 struct nfs4_client *conf, *new;
2994 struct nfs4_client *unconf = NULL;
2996 char addr_str[INET6_ADDRSTRLEN];
2997 nfs4_verifier verf = exid->verifier;
2998 struct sockaddr *sa = svc_addr(rqstp);
2999 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
3000 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3002 rpc_ntop(sa, addr_str, sizeof(addr_str));
3003 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
3004 "ip_addr=%s flags %x, spa_how %d\n",
3005 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
3006 addr_str, exid->flags, exid->spa_how);
3008 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
3009 return nfserr_inval;
3011 new = create_client(exid->clname, rqstp, &verf);
3013 return nfserr_jukebox;
3014 status = copy_impl_id(new, exid);
3018 switch (exid->spa_how) {
3020 exid->spo_must_enforce[0] = 0;
3021 exid->spo_must_enforce[1] = (
3022 1 << (OP_BIND_CONN_TO_SESSION - 32) |
3023 1 << (OP_EXCHANGE_ID - 32) |
3024 1 << (OP_CREATE_SESSION - 32) |
3025 1 << (OP_DESTROY_SESSION - 32) |
3026 1 << (OP_DESTROY_CLIENTID - 32));
3028 exid->spo_must_allow[0] &= (1 << (OP_CLOSE) |
3029 1 << (OP_OPEN_DOWNGRADE) |
3031 1 << (OP_DELEGRETURN));
3033 exid->spo_must_allow[1] &= (
3034 1 << (OP_TEST_STATEID - 32) |
3035 1 << (OP_FREE_STATEID - 32));
3036 if (!svc_rqst_integrity_protected(rqstp)) {
3037 status = nfserr_inval;
3041 * Sometimes userspace doesn't give us a principal.
3042 * Which is a bug, really. Anyway, we can't enforce
3043 * MACH_CRED in that case, better to give up now:
3045 if (!new->cl_cred.cr_principal &&
3046 !new->cl_cred.cr_raw_principal) {
3047 status = nfserr_serverfault;
3050 new->cl_mach_cred = true;
3053 default: /* checked by xdr code */
3057 status = nfserr_encr_alg_unsupp;
3061 /* Cases below refer to rfc 5661 section 18.35.4: */
3062 spin_lock(&nn->client_lock);
3063 conf = find_confirmed_client_by_name(&exid->clname, nn);
3065 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
3066 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
3069 if (!clp_used_exchangeid(conf)) { /* buggy client */
3070 status = nfserr_inval;
3073 if (!nfsd4_mach_creds_match(conf, rqstp)) {
3074 status = nfserr_wrong_cred;
3077 if (!creds_match) { /* case 9 */
3078 status = nfserr_perm;
3081 if (!verfs_match) { /* case 8 */
3082 status = nfserr_not_same;
3086 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
3089 if (!creds_match) { /* case 3 */
3090 if (client_has_state(conf)) {
3091 status = nfserr_clid_inuse;
3096 if (verfs_match) { /* case 2 */
3097 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
3100 /* case 5, client reboot */
3105 if (update) { /* case 7 */
3106 status = nfserr_noent;
3110 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
3111 if (unconf) /* case 4, possible retry or client restart */
3112 unhash_client_locked(unconf);
3114 /* case 1 (normal case) */
3117 status = mark_client_expired_locked(conf);
3121 new->cl_minorversion = cstate->minorversion;
3122 new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0];
3123 new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1];
3125 add_to_unconfirmed(new);
3128 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
3129 exid->clientid.cl_id = conf->cl_clientid.cl_id;
3131 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
3132 nfsd4_set_ex_flags(conf, exid);
3134 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
3135 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
3139 spin_unlock(&nn->client_lock);
3144 expire_client(unconf);
3149 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
3151 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
3154 /* The slot is in use, and no response has been sent. */
3156 if (seqid == slot_seqid)
3157 return nfserr_jukebox;
3159 return nfserr_seq_misordered;
3161 /* Note unsigned 32-bit arithmetic handles wraparound: */
3162 if (likely(seqid == slot_seqid + 1))
3164 if (seqid == slot_seqid)
3165 return nfserr_replay_cache;
3166 return nfserr_seq_misordered;
3170 * Cache the create session result into the create session single DRC
3171 * slot cache by saving the xdr structure. sl_seqid has been set.
3172 * Do this for solo or embedded create session operations.
3175 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
3176 struct nfsd4_clid_slot *slot, __be32 nfserr)
3178 slot->sl_status = nfserr;
3179 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
3183 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
3184 struct nfsd4_clid_slot *slot)
3186 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
3187 return slot->sl_status;
3190 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
3191 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
3192 1 + /* MIN tag is length with zero, only length */ \
3193 3 + /* version, opcount, opcode */ \
3194 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3195 /* seqid, slotID, slotID, cache */ \
3196 4 ) * sizeof(__be32))
3198 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
3199 2 + /* verifier: AUTH_NULL, length 0 */\
3201 1 + /* MIN tag is length with zero, only length */ \
3202 3 + /* opcount, opcode, opstatus*/ \
3203 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3204 /* seqid, slotID, slotID, slotID, status */ \
3205 5 ) * sizeof(__be32))
3207 static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
3209 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
3211 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
3212 return nfserr_toosmall;
3213 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
3214 return nfserr_toosmall;
3215 ca->headerpadsz = 0;
3216 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
3217 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
3218 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
3219 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
3220 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
3221 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
3223 * Note decreasing slot size below client's request may make it
3224 * difficult for client to function correctly, whereas
3225 * decreasing the number of slots will (just?) affect
3226 * performance. When short on memory we therefore prefer to
3227 * decrease number of slots instead of their size. Clients that
3228 * request larger slots than they need will get poor results:
3229 * Note that we always allow at least one slot, because our
3230 * accounting is soft and provides no guarantees either way.
3232 ca->maxreqs = nfsd4_get_drc_mem(ca, nn);
3238 * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
3239 * These are based on similar macros in linux/sunrpc/msg_prot.h .
3241 #define RPC_MAX_HEADER_WITH_AUTH_SYS \
3242 (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
3244 #define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
3245 (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
3247 #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
3248 RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
3249 #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
3250 RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
3253 static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
3255 ca->headerpadsz = 0;
3257 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
3258 return nfserr_toosmall;
3259 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
3260 return nfserr_toosmall;
3261 ca->maxresp_cached = 0;
3263 return nfserr_toosmall;
3268 static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
3270 switch (cbs->flavor) {
3276 * GSS case: the spec doesn't allow us to return this
3277 * error. But it also doesn't allow us not to support
3279 * I'd rather this fail hard than return some error the
3280 * client might think it can already handle:
3282 return nfserr_encr_alg_unsupp;
3287 nfsd4_create_session(struct svc_rqst *rqstp,
3288 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
3290 struct nfsd4_create_session *cr_ses = &u->create_session;
3291 struct sockaddr *sa = svc_addr(rqstp);
3292 struct nfs4_client *conf, *unconf;
3293 struct nfs4_client *old = NULL;
3294 struct nfsd4_session *new;
3295 struct nfsd4_conn *conn;
3296 struct nfsd4_clid_slot *cs_slot = NULL;
3298 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3300 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
3301 return nfserr_inval;
3302 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
3305 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
3308 status = check_backchannel_attrs(&cr_ses->back_channel);
3310 goto out_release_drc_mem;
3311 status = nfserr_jukebox;
3312 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
3314 goto out_release_drc_mem;
3315 conn = alloc_conn_from_crses(rqstp, cr_ses);
3317 goto out_free_session;
3319 spin_lock(&nn->client_lock);
3320 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
3321 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
3322 WARN_ON_ONCE(conf && unconf);
3325 status = nfserr_wrong_cred;
3326 if (!nfsd4_mach_creds_match(conf, rqstp))
3328 cs_slot = &conf->cl_cs_slot;
3329 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
3331 if (status == nfserr_replay_cache)
3332 status = nfsd4_replay_create_session(cr_ses, cs_slot);
3335 } else if (unconf) {
3336 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
3337 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
3338 status = nfserr_clid_inuse;
3341 status = nfserr_wrong_cred;
3342 if (!nfsd4_mach_creds_match(unconf, rqstp))
3344 cs_slot = &unconf->cl_cs_slot;
3345 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
3347 /* an unconfirmed replay returns misordered */
3348 status = nfserr_seq_misordered;
3351 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3353 status = mark_client_expired_locked(old);
3359 move_to_confirmed(unconf);
3362 status = nfserr_stale_clientid;
3366 /* Persistent sessions are not supported */
3367 cr_ses->flags &= ~SESSION4_PERSIST;
3368 /* Upshifting from TCP to RDMA is not supported */
3369 cr_ses->flags &= ~SESSION4_RDMA;
3371 init_session(rqstp, new, conf, cr_ses);
3372 nfsd4_get_session_locked(new);
3374 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
3375 NFS4_MAX_SESSIONID_LEN);
3376 cs_slot->sl_seqid++;
3377 cr_ses->seqid = cs_slot->sl_seqid;
3379 /* cache solo and embedded create sessions under the client_lock */
3380 nfsd4_cache_create_session(cr_ses, cs_slot, status);
3381 spin_unlock(&nn->client_lock);
3382 /* init connection and backchannel */
3383 nfsd4_init_conn(rqstp, conn, new);
3384 nfsd4_put_session(new);
3389 spin_unlock(&nn->client_lock);
3394 __free_session(new);
3395 out_release_drc_mem:
3396 nfsd4_put_drc_mem(&cr_ses->fore_channel);
3400 static __be32 nfsd4_map_bcts_dir(u32 *dir)
3403 case NFS4_CDFC4_FORE:
3404 case NFS4_CDFC4_BACK:
3406 case NFS4_CDFC4_FORE_OR_BOTH:
3407 case NFS4_CDFC4_BACK_OR_BOTH:
3408 *dir = NFS4_CDFC4_BOTH;
3411 return nfserr_inval;
3414 __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp,
3415 struct nfsd4_compound_state *cstate,
3416 union nfsd4_op_u *u)
3418 struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl;
3419 struct nfsd4_session *session = cstate->session;
3420 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3423 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
3426 spin_lock(&nn->client_lock);
3427 session->se_cb_prog = bc->bc_cb_program;
3428 session->se_cb_sec = bc->bc_cb_sec;
3429 spin_unlock(&nn->client_lock);
3431 nfsd4_probe_callback(session->se_client);
3436 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
3437 struct nfsd4_compound_state *cstate,
3438 union nfsd4_op_u *u)
3440 struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session;
3442 struct nfsd4_conn *conn;
3443 struct nfsd4_session *session;
3444 struct net *net = SVC_NET(rqstp);
3445 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3447 if (!nfsd4_last_compound_op(rqstp))
3448 return nfserr_not_only_op;
3449 spin_lock(&nn->client_lock);
3450 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
3451 spin_unlock(&nn->client_lock);
3453 goto out_no_session;
3454 status = nfserr_wrong_cred;
3455 if (!nfsd4_mach_creds_match(session->se_client, rqstp))
3457 status = nfsd4_map_bcts_dir(&bcts->dir);
3460 conn = alloc_conn(rqstp, bcts->dir);
3461 status = nfserr_jukebox;
3464 nfsd4_init_conn(rqstp, conn, session);
3467 nfsd4_put_session(session);
3472 static bool nfsd4_compound_in_session(struct nfsd4_compound_state *cstate, struct nfs4_sessionid *sid)
3474 if (!cstate->session)
3476 return !memcmp(sid, &cstate->session->se_sessionid, sizeof(*sid));
3480 nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate,
3481 union nfsd4_op_u *u)
3483 struct nfs4_sessionid *sessionid = &u->destroy_session.sessionid;
3484 struct nfsd4_session *ses;
3486 int ref_held_by_me = 0;
3487 struct net *net = SVC_NET(r);
3488 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3490 status = nfserr_not_only_op;
3491 if (nfsd4_compound_in_session(cstate, sessionid)) {
3492 if (!nfsd4_last_compound_op(r))
3496 dump_sessionid(__func__, sessionid);
3497 spin_lock(&nn->client_lock);
3498 ses = find_in_sessionid_hashtbl(sessionid, net, &status);
3500 goto out_client_lock;
3501 status = nfserr_wrong_cred;
3502 if (!nfsd4_mach_creds_match(ses->se_client, r))
3503 goto out_put_session;
3504 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
3506 goto out_put_session;
3507 unhash_session(ses);
3508 spin_unlock(&nn->client_lock);
3510 nfsd4_probe_callback_sync(ses->se_client);
3512 spin_lock(&nn->client_lock);
3515 nfsd4_put_session_locked(ses);
3517 spin_unlock(&nn->client_lock);
3522 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
3524 struct nfsd4_conn *c;
3526 list_for_each_entry(c, &s->se_conns, cn_persession) {
3527 if (c->cn_xprt == xpt) {
3534 static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
3536 struct nfs4_client *clp = ses->se_client;
3537 struct nfsd4_conn *c;
3538 __be32 status = nfs_ok;
3541 spin_lock(&clp->cl_lock);
3542 c = __nfsd4_find_conn(new->cn_xprt, ses);
3545 status = nfserr_conn_not_bound_to_session;
3546 if (clp->cl_mach_cred)
3548 __nfsd4_hash_conn(new, ses);
3549 spin_unlock(&clp->cl_lock);
3550 ret = nfsd4_register_conn(new);
3552 /* oops; xprt is already down: */
3553 nfsd4_conn_lost(&new->cn_xpt_user);
3556 spin_unlock(&clp->cl_lock);
3561 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
3563 struct nfsd4_compoundargs *args = rqstp->rq_argp;
3565 return args->opcnt > session->se_fchannel.maxops;
3568 static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
3569 struct nfsd4_session *session)
3571 struct xdr_buf *xb = &rqstp->rq_arg;
3573 return xb->len > session->se_fchannel.maxreq_sz;
3576 static bool replay_matches_cache(struct svc_rqst *rqstp,
3577 struct nfsd4_sequence *seq, struct nfsd4_slot *slot)
3579 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
3581 if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) !=
3582 (bool)seq->cachethis)
3585 * If there's an error then the reply can have fewer ops than
3588 if (slot->sl_opcnt < argp->opcnt && !slot->sl_status)
3591 * But if we cached a reply with *more* ops than the call you're
3592 * sending us now, then this new call is clearly not really a
3593 * replay of the old one:
3595 if (slot->sl_opcnt > argp->opcnt)
3597 /* This is the only check explicitly called by spec: */
3598 if (!same_creds(&rqstp->rq_cred, &slot->sl_cred))
3601 * There may be more comparisons we could actually do, but the
3602 * spec doesn't require us to catch every case where the calls
3603 * don't match (that would require caching the call as well as
3604 * the reply), so we don't bother.
3610 nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3611 union nfsd4_op_u *u)
3613 struct nfsd4_sequence *seq = &u->sequence;
3614 struct nfsd4_compoundres *resp = rqstp->rq_resp;
3615 struct xdr_stream *xdr = &resp->xdr;
3616 struct nfsd4_session *session;
3617 struct nfs4_client *clp;
3618 struct nfsd4_slot *slot;
3619 struct nfsd4_conn *conn;
3622 struct net *net = SVC_NET(rqstp);
3623 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3625 if (resp->opcnt != 1)
3626 return nfserr_sequence_pos;
3629 * Will be either used or freed by nfsd4_sequence_check_conn
3632 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
3634 return nfserr_jukebox;
3636 spin_lock(&nn->client_lock);
3637 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
3639 goto out_no_session;
3640 clp = session->se_client;
3642 status = nfserr_too_many_ops;
3643 if (nfsd4_session_too_many_ops(rqstp, session))
3644 goto out_put_session;
3646 status = nfserr_req_too_big;
3647 if (nfsd4_request_too_big(rqstp, session))
3648 goto out_put_session;
3650 status = nfserr_badslot;
3651 if (seq->slotid >= session->se_fchannel.maxreqs)
3652 goto out_put_session;
3654 slot = session->se_slots[seq->slotid];
3655 dprintk("%s: slotid %d\n", __func__, seq->slotid);
3657 /* We do not negotiate the number of slots yet, so set the
3658 * maxslots to the session maxreqs which is used to encode
3659 * sr_highest_slotid and the sr_target_slot id to maxslots */
3660 seq->maxslots = session->se_fchannel.maxreqs;
3662 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
3663 slot->sl_flags & NFSD4_SLOT_INUSE);
3664 if (status == nfserr_replay_cache) {
3665 status = nfserr_seq_misordered;
3666 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
3667 goto out_put_session;
3668 status = nfserr_seq_false_retry;
3669 if (!replay_matches_cache(rqstp, seq, slot))
3670 goto out_put_session;
3671 cstate->slot = slot;
3672 cstate->session = session;
3674 /* Return the cached reply status and set cstate->status
3675 * for nfsd4_proc_compound processing */
3676 status = nfsd4_replay_cache_entry(resp, seq);
3677 cstate->status = nfserr_replay_cache;
3681 goto out_put_session;
3683 status = nfsd4_sequence_check_conn(conn, session);
3686 goto out_put_session;
3688 buflen = (seq->cachethis) ?
3689 session->se_fchannel.maxresp_cached :
3690 session->se_fchannel.maxresp_sz;
3691 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
3693 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
3694 goto out_put_session;
3695 svc_reserve(rqstp, buflen);
3698 /* Success! bump slot seqid */
3699 slot->sl_seqid = seq->seqid;
3700 slot->sl_flags |= NFSD4_SLOT_INUSE;
3702 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
3704 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
3706 cstate->slot = slot;
3707 cstate->session = session;
3711 switch (clp->cl_cb_state) {
3713 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
3715 case NFSD4_CB_FAULT:
3716 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
3719 seq->status_flags = 0;
3721 if (!list_empty(&clp->cl_revoked))
3722 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
3726 spin_unlock(&nn->client_lock);
3729 nfsd4_put_session_locked(session);
3730 goto out_no_session;
3734 nfsd4_sequence_done(struct nfsd4_compoundres *resp)
3736 struct nfsd4_compound_state *cs = &resp->cstate;
3738 if (nfsd4_has_session(cs)) {
3739 if (cs->status != nfserr_replay_cache) {
3740 nfsd4_store_cache_entry(resp);
3741 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
3743 /* Drop session reference that was taken in nfsd4_sequence() */
3744 nfsd4_put_session(cs->session);
3746 put_client_renew(cs->clp);
3750 nfsd4_destroy_clientid(struct svc_rqst *rqstp,
3751 struct nfsd4_compound_state *cstate,
3752 union nfsd4_op_u *u)
3754 struct nfsd4_destroy_clientid *dc = &u->destroy_clientid;
3755 struct nfs4_client *conf, *unconf;
3756 struct nfs4_client *clp = NULL;
3758 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3760 spin_lock(&nn->client_lock);
3761 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
3762 conf = find_confirmed_client(&dc->clientid, true, nn);
3763 WARN_ON_ONCE(conf && unconf);
3766 if (client_has_state(conf)) {
3767 status = nfserr_clientid_busy;
3770 status = mark_client_expired_locked(conf);
3777 status = nfserr_stale_clientid;
3780 if (!nfsd4_mach_creds_match(clp, rqstp)) {
3782 status = nfserr_wrong_cred;
3785 unhash_client_locked(clp);
3787 spin_unlock(&nn->client_lock);
3794 nfsd4_reclaim_complete(struct svc_rqst *rqstp,
3795 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
3797 struct nfsd4_reclaim_complete *rc = &u->reclaim_complete;
3800 if (rc->rca_one_fs) {
3801 if (!cstate->current_fh.fh_dentry)
3802 return nfserr_nofilehandle;
3804 * We don't take advantage of the rca_one_fs case.
3805 * That's OK, it's optional, we can safely ignore it.
3810 status = nfserr_complete_already;
3811 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
3812 &cstate->session->se_client->cl_flags))
3815 status = nfserr_stale_clientid;
3816 if (is_client_expired(cstate->session->se_client))
3818 * The following error isn't really legal.
3819 * But we only get here if the client just explicitly
3820 * destroyed the client. Surely it no longer cares what
3821 * error it gets back on an operation for the dead
3827 nfsd4_client_record_create(cstate->session->se_client);
3828 inc_reclaim_complete(cstate->session->se_client);
3834 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3835 union nfsd4_op_u *u)
3837 struct nfsd4_setclientid *setclid = &u->setclientid;
3838 struct xdr_netobj clname = setclid->se_name;
3839 nfs4_verifier clverifier = setclid->se_verf;
3840 struct nfs4_client *conf, *new;
3841 struct nfs4_client *unconf = NULL;
3843 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3845 new = create_client(clname, rqstp, &clverifier);
3847 return nfserr_jukebox;
3848 /* Cases below refer to rfc 3530 section 14.2.33: */
3849 spin_lock(&nn->client_lock);
3850 conf = find_confirmed_client_by_name(&clname, nn);
3851 if (conf && client_has_state(conf)) {
3853 status = nfserr_clid_inuse;
3854 if (clp_used_exchangeid(conf))
3856 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
3857 char addr_str[INET6_ADDRSTRLEN];
3858 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
3860 dprintk("NFSD: setclientid: string in use by client "
3861 "at %s\n", addr_str);
3865 unconf = find_unconfirmed_client_by_name(&clname, nn);
3867 unhash_client_locked(unconf);
3868 if (conf && same_verf(&conf->cl_verifier, &clverifier)) {
3869 /* case 1: probable callback update */
3870 copy_clid(new, conf);
3871 gen_confirm(new, nn);
3872 } else /* case 4 (new client) or cases 2, 3 (client reboot): */
3874 new->cl_minorversion = 0;
3875 gen_callback(new, setclid, rqstp);
3876 add_to_unconfirmed(new);
3877 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
3878 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
3879 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
3883 spin_unlock(&nn->client_lock);
3887 expire_client(unconf);
3893 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
3894 struct nfsd4_compound_state *cstate,
3895 union nfsd4_op_u *u)
3897 struct nfsd4_setclientid_confirm *setclientid_confirm =
3898 &u->setclientid_confirm;
3899 struct nfs4_client *conf, *unconf;
3900 struct nfs4_client *old = NULL;
3901 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
3902 clientid_t * clid = &setclientid_confirm->sc_clientid;
3904 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3906 if (STALE_CLIENTID(clid, nn))
3907 return nfserr_stale_clientid;
3909 spin_lock(&nn->client_lock);
3910 conf = find_confirmed_client(clid, false, nn);
3911 unconf = find_unconfirmed_client(clid, false, nn);
3913 * We try hard to give out unique clientid's, so if we get an
3914 * attempt to confirm the same clientid with a different cred,
3915 * the client may be buggy; this should never happen.
3917 * Nevertheless, RFC 7530 recommends INUSE for this case:
3919 status = nfserr_clid_inuse;
3920 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3922 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3924 /* cases below refer to rfc 3530 section 14.2.34: */
3925 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
3926 if (conf && same_verf(&confirm, &conf->cl_confirm)) {
3927 /* case 2: probable retransmit */
3929 } else /* case 4: client hasn't noticed we rebooted yet? */
3930 status = nfserr_stale_clientid;
3934 if (conf) { /* case 1: callback update */
3936 unhash_client_locked(old);
3937 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
3938 } else { /* case 3: normal case; new or rebooted client */
3939 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3941 status = nfserr_clid_inuse;
3942 if (client_has_state(old)
3943 && !same_creds(&unconf->cl_cred,
3948 status = mark_client_expired_locked(old);
3954 move_to_confirmed(unconf);
3957 get_client_locked(conf);
3958 spin_unlock(&nn->client_lock);
3959 nfsd4_probe_callback(conf);
3960 spin_lock(&nn->client_lock);
3961 put_client_renew_locked(conf);
3963 spin_unlock(&nn->client_lock);
3969 static struct nfs4_file *nfsd4_alloc_file(void)
3971 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3974 /* OPEN Share state helper functions */
3975 static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
3976 struct nfs4_file *fp)
3978 lockdep_assert_held(&state_lock);
3980 refcount_set(&fp->fi_ref, 1);
3981 spin_lock_init(&fp->fi_lock);
3982 INIT_LIST_HEAD(&fp->fi_stateids);
3983 INIT_LIST_HEAD(&fp->fi_delegations);
3984 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
3985 fh_copy_shallow(&fp->fi_fhandle, fh);
3986 fp->fi_deleg_file = NULL;
3987 fp->fi_had_conflict = false;
3988 fp->fi_share_deny = 0;
3989 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3990 memset(fp->fi_access, 0, sizeof(fp->fi_access));
3991 #ifdef CONFIG_NFSD_PNFS
3992 INIT_LIST_HEAD(&fp->fi_lo_states);
3993 atomic_set(&fp->fi_lo_recalls, 0);
3995 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
3999 nfsd4_free_slabs(void)
4001 kmem_cache_destroy(client_slab);
4002 kmem_cache_destroy(openowner_slab);
4003 kmem_cache_destroy(lockowner_slab);
4004 kmem_cache_destroy(file_slab);
4005 kmem_cache_destroy(stateid_slab);
4006 kmem_cache_destroy(deleg_slab);
4007 kmem_cache_destroy(odstate_slab);
4011 nfsd4_init_slabs(void)
4013 client_slab = kmem_cache_create("nfsd4_clients",
4014 sizeof(struct nfs4_client), 0, 0, NULL);
4015 if (client_slab == NULL)
4017 openowner_slab = kmem_cache_create("nfsd4_openowners",
4018 sizeof(struct nfs4_openowner), 0, 0, NULL);
4019 if (openowner_slab == NULL)
4020 goto out_free_client_slab;
4021 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
4022 sizeof(struct nfs4_lockowner), 0, 0, NULL);
4023 if (lockowner_slab == NULL)
4024 goto out_free_openowner_slab;
4025 file_slab = kmem_cache_create("nfsd4_files",
4026 sizeof(struct nfs4_file), 0, 0, NULL);
4027 if (file_slab == NULL)
4028 goto out_free_lockowner_slab;
4029 stateid_slab = kmem_cache_create("nfsd4_stateids",
4030 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
4031 if (stateid_slab == NULL)
4032 goto out_free_file_slab;
4033 deleg_slab = kmem_cache_create("nfsd4_delegations",
4034 sizeof(struct nfs4_delegation), 0, 0, NULL);
4035 if (deleg_slab == NULL)
4036 goto out_free_stateid_slab;
4037 odstate_slab = kmem_cache_create("nfsd4_odstate",
4038 sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
4039 if (odstate_slab == NULL)
4040 goto out_free_deleg_slab;
4043 out_free_deleg_slab:
4044 kmem_cache_destroy(deleg_slab);
4045 out_free_stateid_slab:
4046 kmem_cache_destroy(stateid_slab);
4048 kmem_cache_destroy(file_slab);
4049 out_free_lockowner_slab:
4050 kmem_cache_destroy(lockowner_slab);
4051 out_free_openowner_slab:
4052 kmem_cache_destroy(openowner_slab);
4053 out_free_client_slab:
4054 kmem_cache_destroy(client_slab);
4056 dprintk("nfsd4: out of memory while initializing nfsv4\n");
4060 static void init_nfs4_replay(struct nfs4_replay *rp)
4062 rp->rp_status = nfserr_serverfault;
4064 rp->rp_buf = rp->rp_ibuf;
4065 mutex_init(&rp->rp_mutex);
4068 static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
4069 struct nfs4_stateowner *so)
4071 if (!nfsd4_has_session(cstate)) {
4072 mutex_lock(&so->so_replay.rp_mutex);
4073 cstate->replay_owner = nfs4_get_stateowner(so);
4077 void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
4079 struct nfs4_stateowner *so = cstate->replay_owner;
4082 cstate->replay_owner = NULL;
4083 mutex_unlock(&so->so_replay.rp_mutex);
4084 nfs4_put_stateowner(so);
4088 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
4090 struct nfs4_stateowner *sop;
4092 sop = kmem_cache_alloc(slab, GFP_KERNEL);
4096 xdr_netobj_dup(&sop->so_owner, owner, GFP_KERNEL);
4097 if (!sop->so_owner.data) {
4098 kmem_cache_free(slab, sop);
4102 INIT_LIST_HEAD(&sop->so_stateids);
4103 sop->so_client = clp;
4104 init_nfs4_replay(&sop->so_replay);
4105 atomic_set(&sop->so_count, 1);
4109 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
4111 lockdep_assert_held(&clp->cl_lock);
4113 list_add(&oo->oo_owner.so_strhash,
4114 &clp->cl_ownerstr_hashtbl[strhashval]);
4115 list_add(&oo->oo_perclient, &clp->cl_openowners);
4118 static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
4120 unhash_openowner_locked(openowner(so));
4123 static void nfs4_free_openowner(struct nfs4_stateowner *so)
4125 struct nfs4_openowner *oo = openowner(so);
4127 kmem_cache_free(openowner_slab, oo);
4130 static const struct nfs4_stateowner_operations openowner_ops = {
4131 .so_unhash = nfs4_unhash_openowner,
4132 .so_free = nfs4_free_openowner,
4135 static struct nfs4_ol_stateid *
4136 nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4138 struct nfs4_ol_stateid *local, *ret = NULL;
4139 struct nfs4_openowner *oo = open->op_openowner;
4141 lockdep_assert_held(&fp->fi_lock);
4143 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
4144 /* ignore lock owners */
4145 if (local->st_stateowner->so_is_open_owner == 0)
4147 if (local->st_stateowner != &oo->oo_owner)
4149 if (local->st_stid.sc_type == NFS4_OPEN_STID) {
4151 refcount_inc(&ret->st_stid.sc_count);
4159 nfsd4_verify_open_stid(struct nfs4_stid *s)
4161 __be32 ret = nfs_ok;
4163 switch (s->sc_type) {
4167 case NFS4_CLOSED_STID:
4168 case NFS4_CLOSED_DELEG_STID:
4169 ret = nfserr_bad_stateid;
4171 case NFS4_REVOKED_DELEG_STID:
4172 ret = nfserr_deleg_revoked;
4177 /* Lock the stateid st_mutex, and deal with races with CLOSE */
4179 nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp)
4183 mutex_lock_nested(&stp->st_mutex, LOCK_STATEID_MUTEX);
4184 ret = nfsd4_verify_open_stid(&stp->st_stid);
4186 mutex_unlock(&stp->st_mutex);
4190 static struct nfs4_ol_stateid *
4191 nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4193 struct nfs4_ol_stateid *stp;
4195 spin_lock(&fp->fi_lock);
4196 stp = nfsd4_find_existing_open(fp, open);
4197 spin_unlock(&fp->fi_lock);
4198 if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok)
4200 nfs4_put_stid(&stp->st_stid);
4205 static struct nfs4_openowner *
4206 alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
4207 struct nfsd4_compound_state *cstate)
4209 struct nfs4_client *clp = cstate->clp;
4210 struct nfs4_openowner *oo, *ret;
4212 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
4215 oo->oo_owner.so_ops = &openowner_ops;
4216 oo->oo_owner.so_is_open_owner = 1;
4217 oo->oo_owner.so_seqid = open->op_seqid;
4219 if (nfsd4_has_session(cstate))
4220 oo->oo_flags |= NFS4_OO_CONFIRMED;
4222 oo->oo_last_closed_stid = NULL;
4223 INIT_LIST_HEAD(&oo->oo_close_lru);
4224 spin_lock(&clp->cl_lock);
4225 ret = find_openstateowner_str_locked(strhashval, open, clp);
4227 hash_openowner(oo, clp, strhashval);
4230 nfs4_free_stateowner(&oo->oo_owner);
4232 spin_unlock(&clp->cl_lock);
4236 static struct nfs4_ol_stateid *
4237 init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
4240 struct nfs4_openowner *oo = open->op_openowner;
4241 struct nfs4_ol_stateid *retstp = NULL;
4242 struct nfs4_ol_stateid *stp;
4245 /* We are moving these outside of the spinlocks to avoid the warnings */
4246 mutex_init(&stp->st_mutex);
4247 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
4250 spin_lock(&oo->oo_owner.so_client->cl_lock);
4251 spin_lock(&fp->fi_lock);
4253 retstp = nfsd4_find_existing_open(fp, open);
4257 open->op_stp = NULL;
4258 refcount_inc(&stp->st_stid.sc_count);
4259 stp->st_stid.sc_type = NFS4_OPEN_STID;
4260 INIT_LIST_HEAD(&stp->st_locks);
4261 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
4263 stp->st_stid.sc_file = fp;
4264 stp->st_access_bmap = 0;
4265 stp->st_deny_bmap = 0;
4266 stp->st_openstp = NULL;
4267 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
4268 list_add(&stp->st_perfile, &fp->fi_stateids);
4271 spin_unlock(&fp->fi_lock);
4272 spin_unlock(&oo->oo_owner.so_client->cl_lock);
4274 /* Handle races with CLOSE */
4275 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
4276 nfs4_put_stid(&retstp->st_stid);
4279 /* To keep mutex tracking happy */
4280 mutex_unlock(&stp->st_mutex);
4287 * In the 4.0 case we need to keep the owners around a little while to handle
4288 * CLOSE replay. We still do need to release any file access that is held by
4289 * them before returning however.
4292 move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
4294 struct nfs4_ol_stateid *last;
4295 struct nfs4_openowner *oo = openowner(s->st_stateowner);
4296 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
4299 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
4302 * We know that we hold one reference via nfsd4_close, and another
4303 * "persistent" reference for the client. If the refcount is higher
4304 * than 2, then there are still calls in progress that are using this
4305 * stateid. We can't put the sc_file reference until they are finished.
4306 * Wait for the refcount to drop to 2. Since it has been unhashed,
4307 * there should be no danger of the refcount going back up again at
4310 wait_event(close_wq, refcount_read(&s->st_stid.sc_count) == 2);
4312 release_all_access(s);
4313 if (s->st_stid.sc_file) {
4314 put_nfs4_file(s->st_stid.sc_file);
4315 s->st_stid.sc_file = NULL;
4318 spin_lock(&nn->client_lock);
4319 last = oo->oo_last_closed_stid;
4320 oo->oo_last_closed_stid = s;
4321 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
4322 oo->oo_time = get_seconds();
4323 spin_unlock(&nn->client_lock);
4325 nfs4_put_stid(&last->st_stid);
4328 /* search file_hashtbl[] for file */
4329 static struct nfs4_file *
4330 find_file_locked(struct knfsd_fh *fh, unsigned int hashval)
4332 struct nfs4_file *fp;
4334 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash) {
4335 if (fh_match(&fp->fi_fhandle, fh)) {
4336 if (refcount_inc_not_zero(&fp->fi_ref))
4344 find_file(struct knfsd_fh *fh)
4346 struct nfs4_file *fp;
4347 unsigned int hashval = file_hashval(fh);
4350 fp = find_file_locked(fh, hashval);
4355 static struct nfs4_file *
4356 find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
4358 struct nfs4_file *fp;
4359 unsigned int hashval = file_hashval(fh);
4362 fp = find_file_locked(fh, hashval);
4367 spin_lock(&state_lock);
4368 fp = find_file_locked(fh, hashval);
4369 if (likely(fp == NULL)) {
4370 nfsd4_init_file(fh, hashval, new);
4373 spin_unlock(&state_lock);
4379 * Called to check deny when READ with all zero stateid or
4380 * WRITE with all zero or all one stateid
4383 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
4385 struct nfs4_file *fp;
4386 __be32 ret = nfs_ok;
4388 fp = find_file(¤t_fh->fh_handle);
4391 /* Check for conflicting share reservations */
4392 spin_lock(&fp->fi_lock);
4393 if (fp->fi_share_deny & deny_type)
4394 ret = nfserr_locked;
4395 spin_unlock(&fp->fi_lock);
4400 static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
4402 struct nfs4_delegation *dp = cb_to_delegation(cb);
4403 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
4406 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
4409 * We can't do this in nfsd_break_deleg_cb because it is
4410 * already holding inode->i_lock.
4412 * If the dl_time != 0, then we know that it has already been
4413 * queued for a lease break. Don't queue it again.
4415 spin_lock(&state_lock);
4416 if (delegation_hashed(dp) && dp->dl_time == 0) {
4417 dp->dl_time = get_seconds();
4418 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
4420 spin_unlock(&state_lock);
4423 static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
4424 struct rpc_task *task)
4426 struct nfs4_delegation *dp = cb_to_delegation(cb);
4428 if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID)
4431 switch (task->tk_status) {
4434 case -NFS4ERR_DELAY:
4435 rpc_delay(task, 2 * HZ);
4438 case -NFS4ERR_BAD_STATEID:
4440 * Race: client probably got cb_recall before open reply
4441 * granting delegation.
4443 if (dp->dl_retries--) {
4444 rpc_delay(task, 2 * HZ);
4453 static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
4455 struct nfs4_delegation *dp = cb_to_delegation(cb);
4457 nfs4_put_stid(&dp->dl_stid);
4460 static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
4461 .prepare = nfsd4_cb_recall_prepare,
4462 .done = nfsd4_cb_recall_done,
4463 .release = nfsd4_cb_recall_release,
4466 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
4469 * We're assuming the state code never drops its reference
4470 * without first removing the lease. Since we're in this lease
4471 * callback (and since the lease code is serialized by the
4472 * i_lock) we know the server hasn't removed the lease yet, and
4473 * we know it's safe to take a reference.
4475 refcount_inc(&dp->dl_stid.sc_count);
4476 nfsd4_run_cb(&dp->dl_recall);
4479 /* Called from break_lease() with i_lock held. */
4481 nfsd_break_deleg_cb(struct file_lock *fl)
4484 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
4485 struct nfs4_file *fp = dp->dl_stid.sc_file;
4488 * We don't want the locks code to timeout the lease for us;
4489 * we'll remove it ourself if a delegation isn't returned
4492 fl->fl_break_time = 0;
4494 spin_lock(&fp->fi_lock);
4495 fp->fi_had_conflict = true;
4496 nfsd_break_one_deleg(dp);
4497 spin_unlock(&fp->fi_lock);
4502 nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
4503 struct list_head *dispose)
4506 return lease_modify(onlist, arg, dispose);
4511 static const struct lock_manager_operations nfsd_lease_mng_ops = {
4512 .lm_break = nfsd_break_deleg_cb,
4513 .lm_change = nfsd_change_deleg_cb,
4516 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
4518 if (nfsd4_has_session(cstate))
4520 if (seqid == so->so_seqid - 1)
4521 return nfserr_replay_me;
4522 if (seqid == so->so_seqid)
4524 return nfserr_bad_seqid;
4527 static __be32 lookup_clientid(clientid_t *clid,
4528 struct nfsd4_compound_state *cstate,
4529 struct nfsd_net *nn)
4531 struct nfs4_client *found;
4534 found = cstate->clp;
4535 if (!same_clid(&found->cl_clientid, clid))
4536 return nfserr_stale_clientid;
4540 if (STALE_CLIENTID(clid, nn))
4541 return nfserr_stale_clientid;
4544 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
4545 * cached already then we know this is for is for v4.0 and "sessions"
4548 WARN_ON_ONCE(cstate->session);
4549 spin_lock(&nn->client_lock);
4550 found = find_confirmed_client(clid, false, nn);
4552 spin_unlock(&nn->client_lock);
4553 return nfserr_expired;
4555 atomic_inc(&found->cl_rpc_users);
4556 spin_unlock(&nn->client_lock);
4558 /* Cache the nfs4_client in cstate! */
4559 cstate->clp = found;
4564 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
4565 struct nfsd4_open *open, struct nfsd_net *nn)
4567 clientid_t *clientid = &open->op_clientid;
4568 struct nfs4_client *clp = NULL;
4569 unsigned int strhashval;
4570 struct nfs4_openowner *oo = NULL;
4573 if (STALE_CLIENTID(&open->op_clientid, nn))
4574 return nfserr_stale_clientid;
4576 * In case we need it later, after we've already created the
4577 * file and don't want to risk a further failure:
4579 open->op_file = nfsd4_alloc_file();
4580 if (open->op_file == NULL)
4581 return nfserr_jukebox;
4583 status = lookup_clientid(clientid, cstate, nn);
4588 strhashval = ownerstr_hashval(&open->op_owner);
4589 oo = find_openstateowner_str(strhashval, open, clp);
4590 open->op_openowner = oo;
4594 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4595 /* Replace unconfirmed owners without checking for replay. */
4596 release_openowner(oo);
4597 open->op_openowner = NULL;
4600 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
4605 oo = alloc_init_open_stateowner(strhashval, open, cstate);
4607 return nfserr_jukebox;
4608 open->op_openowner = oo;
4610 open->op_stp = nfs4_alloc_open_stateid(clp);
4612 return nfserr_jukebox;
4614 if (nfsd4_has_session(cstate) &&
4615 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
4616 open->op_odstate = alloc_clnt_odstate(clp);
4617 if (!open->op_odstate)
4618 return nfserr_jukebox;
4624 static inline __be32
4625 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
4627 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
4628 return nfserr_openmode;
4633 static int share_access_to_flags(u32 share_access)
4635 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
4638 static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
4640 struct nfs4_stid *ret;
4642 ret = find_stateid_by_type(cl, s,
4643 NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID);
4646 return delegstateid(ret);
4649 static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
4651 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
4652 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
4656 nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
4657 struct nfs4_delegation **dp)
4660 __be32 status = nfserr_bad_stateid;
4661 struct nfs4_delegation *deleg;
4663 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
4666 if (deleg->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) {
4667 nfs4_put_stid(&deleg->dl_stid);
4668 if (cl->cl_minorversion)
4669 status = nfserr_deleg_revoked;
4672 flags = share_access_to_flags(open->op_share_access);
4673 status = nfs4_check_delegmode(deleg, flags);
4675 nfs4_put_stid(&deleg->dl_stid);
4680 if (!nfsd4_is_deleg_cur(open))
4684 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
4688 static inline int nfs4_access_to_access(u32 nfs4_access)
4692 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
4693 flags |= NFSD_MAY_READ;
4694 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
4695 flags |= NFSD_MAY_WRITE;
4699 static inline __be32
4700 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
4701 struct nfsd4_open *open)
4703 struct iattr iattr = {
4704 .ia_valid = ATTR_SIZE,
4707 if (!open->op_truncate)
4709 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
4710 return nfserr_inval;
4711 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
4714 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
4715 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
4716 struct nfsd4_open *open)
4718 struct nfsd_file *nf = NULL;
4720 int oflag = nfs4_access_to_omode(open->op_share_access);
4721 int access = nfs4_access_to_access(open->op_share_access);
4722 unsigned char old_access_bmap, old_deny_bmap;
4724 spin_lock(&fp->fi_lock);
4727 * Are we trying to set a deny mode that would conflict with
4730 status = nfs4_file_check_deny(fp, open->op_share_deny);
4731 if (status != nfs_ok) {
4732 spin_unlock(&fp->fi_lock);
4736 /* set access to the file */
4737 status = nfs4_file_get_access(fp, open->op_share_access);
4738 if (status != nfs_ok) {
4739 spin_unlock(&fp->fi_lock);
4743 /* Set access bits in stateid */
4744 old_access_bmap = stp->st_access_bmap;
4745 set_access(open->op_share_access, stp);
4747 /* Set new deny mask */
4748 old_deny_bmap = stp->st_deny_bmap;
4749 set_deny(open->op_share_deny, stp);
4750 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4752 if (!fp->fi_fds[oflag]) {
4753 spin_unlock(&fp->fi_lock);
4754 status = nfsd_file_acquire(rqstp, cur_fh, access, &nf);
4756 goto out_put_access;
4757 spin_lock(&fp->fi_lock);
4758 if (!fp->fi_fds[oflag]) {
4759 fp->fi_fds[oflag] = nf;
4763 spin_unlock(&fp->fi_lock);
4767 status = nfsd4_truncate(rqstp, cur_fh, open);
4769 goto out_put_access;
4773 stp->st_access_bmap = old_access_bmap;
4774 nfs4_file_put_access(fp, open->op_share_access);
4775 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
4780 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)
4783 unsigned char old_deny_bmap = stp->st_deny_bmap;
4785 if (!test_access(open->op_share_access, stp))
4786 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
4788 /* test and set deny mode */
4789 spin_lock(&fp->fi_lock);
4790 status = nfs4_file_check_deny(fp, open->op_share_deny);
4791 if (status == nfs_ok) {
4792 set_deny(open->op_share_deny, stp);
4793 fp->fi_share_deny |=
4794 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4796 spin_unlock(&fp->fi_lock);
4798 if (status != nfs_ok)
4801 status = nfsd4_truncate(rqstp, cur_fh, open);
4802 if (status != nfs_ok)
4803 reset_union_bmap_deny(old_deny_bmap, stp);
4807 /* Should we give out recallable state?: */
4808 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
4810 if (clp->cl_cb_state == NFSD4_CB_UP)
4813 * In the sessions case, since we don't have to establish a
4814 * separate connection for callbacks, we assume it's OK
4815 * until we hear otherwise:
4817 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
4820 static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp,
4823 struct file_lock *fl;
4825 fl = locks_alloc_lock();
4828 fl->fl_lmops = &nfsd_lease_mng_ops;
4829 fl->fl_flags = FL_DELEG;
4830 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
4831 fl->fl_end = OFFSET_MAX;
4832 fl->fl_owner = (fl_owner_t)dp;
4833 fl->fl_pid = current->tgid;
4834 fl->fl_file = dp->dl_stid.sc_file->fi_deleg_file->nf_file;
4838 static struct nfs4_delegation *
4839 nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
4840 struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate)
4843 struct nfs4_delegation *dp;
4844 struct nfsd_file *nf;
4845 struct file_lock *fl;
4848 * The fi_had_conflict and nfs_get_existing_delegation checks
4849 * here are just optimizations; we'll need to recheck them at
4852 if (fp->fi_had_conflict)
4853 return ERR_PTR(-EAGAIN);
4855 nf = find_readable_file(fp);
4857 /* We should always have a readable file here */
4859 return ERR_PTR(-EBADF);
4861 spin_lock(&state_lock);
4862 spin_lock(&fp->fi_lock);
4863 if (nfs4_delegation_exists(clp, fp))
4865 else if (!fp->fi_deleg_file) {
4866 fp->fi_deleg_file = nf;
4867 /* increment early to prevent fi_deleg_file from being
4869 fp->fi_delegees = 1;
4873 spin_unlock(&fp->fi_lock);
4874 spin_unlock(&state_lock);
4878 return ERR_PTR(status);
4881 dp = alloc_init_deleg(clp, fp, fh, odstate);
4885 fl = nfs4_alloc_init_lease(dp, NFS4_OPEN_DELEGATE_READ);
4887 goto out_clnt_odstate;
4889 status = vfs_setlease(fp->fi_deleg_file->nf_file, fl->fl_type, &fl, NULL);
4891 locks_free_lock(fl);
4893 goto out_clnt_odstate;
4895 spin_lock(&state_lock);
4896 spin_lock(&fp->fi_lock);
4897 if (fp->fi_had_conflict)
4900 status = hash_delegation_locked(dp, fp);
4901 spin_unlock(&fp->fi_lock);
4902 spin_unlock(&state_lock);
4909 vfs_setlease(fp->fi_deleg_file->nf_file, F_UNLCK, NULL, (void **)&dp);
4911 put_clnt_odstate(dp->dl_clnt_odstate);
4912 nfs4_put_stid(&dp->dl_stid);
4915 return ERR_PTR(status);
4918 static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
4920 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4921 if (status == -EAGAIN)
4922 open->op_why_no_deleg = WND4_CONTENTION;
4924 open->op_why_no_deleg = WND4_RESOURCE;
4925 switch (open->op_deleg_want) {
4926 case NFS4_SHARE_WANT_READ_DELEG:
4927 case NFS4_SHARE_WANT_WRITE_DELEG:
4928 case NFS4_SHARE_WANT_ANY_DELEG:
4930 case NFS4_SHARE_WANT_CANCEL:
4931 open->op_why_no_deleg = WND4_CANCELLED;
4933 case NFS4_SHARE_WANT_NO_DELEG:
4940 * Attempt to hand out a delegation.
4942 * Note we don't support write delegations, and won't until the vfs has
4943 * proper support for them.
4946 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
4947 struct nfs4_ol_stateid *stp)
4949 struct nfs4_delegation *dp;
4950 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
4951 struct nfs4_client *clp = stp->st_stid.sc_client;
4955 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
4956 open->op_recall = 0;
4957 switch (open->op_claim_type) {
4958 case NFS4_OPEN_CLAIM_PREVIOUS:
4960 open->op_recall = 1;
4961 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
4964 case NFS4_OPEN_CLAIM_NULL:
4965 case NFS4_OPEN_CLAIM_FH:
4967 * Let's not give out any delegations till everyone's
4968 * had the chance to reclaim theirs, *and* until
4969 * NLM locks have all been reclaimed:
4971 if (locks_in_grace(clp->net))
4973 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
4976 * Also, if the file was opened for write or
4977 * create, there's a good chance the client's
4978 * about to write to it, resulting in an
4979 * immediate recall (since we don't support
4980 * write delegations):
4982 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
4984 if (open->op_create == NFS4_OPEN_CREATE)
4990 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate);
4994 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
4996 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
4997 STATEID_VAL(&dp->dl_stid.sc_stateid));
4998 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
4999 nfs4_put_stid(&dp->dl_stid);
5002 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
5003 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
5004 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
5005 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
5006 open->op_recall = 1;
5009 /* 4.1 client asking for a delegation? */
5010 if (open->op_deleg_want)
5011 nfsd4_open_deleg_none_ext(open, status);
5015 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
5016 struct nfs4_delegation *dp)
5018 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
5019 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
5020 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5021 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
5022 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
5023 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
5024 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5025 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
5027 /* Otherwise the client must be confused wanting a delegation
5028 * it already has, therefore we don't return
5029 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
5034 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
5036 struct nfsd4_compoundres *resp = rqstp->rq_resp;
5037 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
5038 struct nfs4_file *fp = NULL;
5039 struct nfs4_ol_stateid *stp = NULL;
5040 struct nfs4_delegation *dp = NULL;
5042 bool new_stp = false;
5045 * Lookup file; if found, lookup stateid and check open request,
5046 * and check for delegations in the process of being recalled.
5047 * If not found, create the nfs4_file struct
5049 fp = find_or_add_file(open->op_file, ¤t_fh->fh_handle);
5050 if (fp != open->op_file) {
5051 status = nfs4_check_deleg(cl, open, &dp);
5054 stp = nfsd4_find_and_lock_existing_open(fp, open);
5056 open->op_file = NULL;
5057 status = nfserr_bad_stateid;
5058 if (nfsd4_is_deleg_cur(open))
5063 stp = init_open_stateid(fp, open);
5069 * OPEN the file, or upgrade an existing OPEN.
5070 * If truncate fails, the OPEN fails.
5072 * stp is already locked.
5075 /* Stateid was found, this is an OPEN upgrade */
5076 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
5078 mutex_unlock(&stp->st_mutex);
5082 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
5084 stp->st_stid.sc_type = NFS4_CLOSED_STID;
5085 release_open_stateid(stp);
5086 mutex_unlock(&stp->st_mutex);
5090 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
5092 if (stp->st_clnt_odstate == open->op_odstate)
5093 open->op_odstate = NULL;
5096 nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
5097 mutex_unlock(&stp->st_mutex);
5099 if (nfsd4_has_session(&resp->cstate)) {
5100 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
5101 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5102 open->op_why_no_deleg = WND4_NOT_WANTED;
5108 * Attempt to hand out a delegation. No error return, because the
5109 * OPEN succeeds even if we fail.
5111 nfs4_open_delegation(current_fh, open, stp);
5115 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
5116 STATEID_VAL(&stp->st_stid.sc_stateid));
5118 /* 4.1 client trying to upgrade/downgrade delegation? */
5119 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
5120 open->op_deleg_want)
5121 nfsd4_deleg_xgrade_none_ext(open, dp);
5125 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
5126 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
5128 * To finish the open response, we just need to set the rflags.
5130 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
5131 if (nfsd4_has_session(&resp->cstate))
5132 open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK;
5133 else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED))
5134 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
5137 nfs4_put_stid(&dp->dl_stid);
5139 nfs4_put_stid(&stp->st_stid);
5144 void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
5145 struct nfsd4_open *open)
5147 if (open->op_openowner) {
5148 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
5150 nfsd4_cstate_assign_replay(cstate, so);
5151 nfs4_put_stateowner(so);
5154 kmem_cache_free(file_slab, open->op_file);
5156 nfs4_put_stid(&open->op_stp->st_stid);
5157 if (open->op_odstate)
5158 kmem_cache_free(odstate_slab, open->op_odstate);
5162 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5163 union nfsd4_op_u *u)
5165 clientid_t *clid = &u->renew;
5166 struct nfs4_client *clp;
5168 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5170 dprintk("process_renew(%08x/%08x): starting\n",
5171 clid->cl_boot, clid->cl_id);
5172 status = lookup_clientid(clid, cstate, nn);
5176 status = nfserr_cb_path_down;
5177 if (!list_empty(&clp->cl_delegations)
5178 && clp->cl_cb_state != NFSD4_CB_UP)
5186 nfsd4_end_grace(struct nfsd_net *nn)
5188 /* do nothing if grace period already ended */
5189 if (nn->grace_ended)
5192 nn->grace_ended = true;
5194 * If the server goes down again right now, an NFSv4
5195 * client will still be allowed to reclaim after it comes back up,
5196 * even if it hasn't yet had a chance to reclaim state this time.
5199 nfsd4_record_grace_done(nn);
5201 * At this point, NFSv4 clients can still reclaim. But if the
5202 * server crashes, any that have not yet reclaimed will be out
5203 * of luck on the next boot.
5205 * (NFSv4.1+ clients are considered to have reclaimed once they
5206 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
5207 * have reclaimed after their first OPEN.)
5209 locks_end_grace(&nn->nfsd4_manager);
5211 * At this point, and once lockd and/or any other containers
5212 * exit their grace period, further reclaims will fail and
5213 * regular locking can resume.
5218 * If we've waited a lease period but there are still clients trying to
5219 * reclaim, wait a little longer to give them a chance to finish.
5221 static bool clients_still_reclaiming(struct nfsd_net *nn)
5223 unsigned long now = get_seconds();
5224 unsigned long double_grace_period_end = nn->boot_time +
5225 2 * nn->nfsd4_lease;
5227 if (nn->track_reclaim_completes &&
5228 atomic_read(&nn->nr_reclaim_complete) ==
5229 nn->reclaim_str_hashtbl_size)
5231 if (!nn->somebody_reclaimed)
5233 nn->somebody_reclaimed = false;
5235 * If we've given them *two* lease times to reclaim, and they're
5236 * still not done, give up:
5238 if (time_after(now, double_grace_period_end))
5244 nfs4_laundromat(struct nfsd_net *nn)
5246 struct nfs4_client *clp;
5247 struct nfs4_openowner *oo;
5248 struct nfs4_delegation *dp;
5249 struct nfs4_ol_stateid *stp;
5250 struct nfsd4_blocked_lock *nbl;
5251 struct list_head *pos, *next, reaplist;
5252 time_t cutoff = get_seconds() - nn->nfsd4_lease;
5253 time_t t, new_timeo = nn->nfsd4_lease;
5255 dprintk("NFSD: laundromat service - starting\n");
5257 if (clients_still_reclaiming(nn)) {
5261 dprintk("NFSD: end of grace period\n");
5262 nfsd4_end_grace(nn);
5263 INIT_LIST_HEAD(&reaplist);
5264 spin_lock(&nn->client_lock);
5265 list_for_each_safe(pos, next, &nn->client_lru) {
5266 clp = list_entry(pos, struct nfs4_client, cl_lru);
5267 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
5268 t = clp->cl_time - cutoff;
5269 new_timeo = min(new_timeo, t);
5272 if (mark_client_expired_locked(clp)) {
5273 dprintk("NFSD: client in use (clientid %08x)\n",
5274 clp->cl_clientid.cl_id);
5277 list_add(&clp->cl_lru, &reaplist);
5279 spin_unlock(&nn->client_lock);
5280 list_for_each_safe(pos, next, &reaplist) {
5281 clp = list_entry(pos, struct nfs4_client, cl_lru);
5282 dprintk("NFSD: purging unused client (clientid %08x)\n",
5283 clp->cl_clientid.cl_id);
5284 list_del_init(&clp->cl_lru);
5287 spin_lock(&state_lock);
5288 list_for_each_safe(pos, next, &nn->del_recall_lru) {
5289 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
5290 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
5291 t = dp->dl_time - cutoff;
5292 new_timeo = min(new_timeo, t);
5295 WARN_ON(!unhash_delegation_locked(dp));
5296 list_add(&dp->dl_recall_lru, &reaplist);
5298 spin_unlock(&state_lock);
5299 while (!list_empty(&reaplist)) {
5300 dp = list_first_entry(&reaplist, struct nfs4_delegation,
5302 list_del_init(&dp->dl_recall_lru);
5303 revoke_delegation(dp);
5306 spin_lock(&nn->client_lock);
5307 while (!list_empty(&nn->close_lru)) {
5308 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
5310 if (time_after((unsigned long)oo->oo_time,
5311 (unsigned long)cutoff)) {
5312 t = oo->oo_time - cutoff;
5313 new_timeo = min(new_timeo, t);
5316 list_del_init(&oo->oo_close_lru);
5317 stp = oo->oo_last_closed_stid;
5318 oo->oo_last_closed_stid = NULL;
5319 spin_unlock(&nn->client_lock);
5320 nfs4_put_stid(&stp->st_stid);
5321 spin_lock(&nn->client_lock);
5323 spin_unlock(&nn->client_lock);
5326 * It's possible for a client to try and acquire an already held lock
5327 * that is being held for a long time, and then lose interest in it.
5328 * So, we clean out any un-revisited request after a lease period
5329 * under the assumption that the client is no longer interested.
5331 * RFC5661, sec. 9.6 states that the client must not rely on getting
5332 * notifications and must continue to poll for locks, even when the
5333 * server supports them. Thus this shouldn't lead to clients blocking
5334 * indefinitely once the lock does become free.
5336 BUG_ON(!list_empty(&reaplist));
5337 spin_lock(&nn->blocked_locks_lock);
5338 while (!list_empty(&nn->blocked_locks_lru)) {
5339 nbl = list_first_entry(&nn->blocked_locks_lru,
5340 struct nfsd4_blocked_lock, nbl_lru);
5341 if (time_after((unsigned long)nbl->nbl_time,
5342 (unsigned long)cutoff)) {
5343 t = nbl->nbl_time - cutoff;
5344 new_timeo = min(new_timeo, t);
5347 list_move(&nbl->nbl_lru, &reaplist);
5348 list_del_init(&nbl->nbl_list);
5350 spin_unlock(&nn->blocked_locks_lock);
5352 while (!list_empty(&reaplist)) {
5353 nbl = list_first_entry(&reaplist,
5354 struct nfsd4_blocked_lock, nbl_lru);
5355 list_del_init(&nbl->nbl_lru);
5356 free_blocked_lock(nbl);
5359 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
5363 static struct workqueue_struct *laundry_wq;
5364 static void laundromat_main(struct work_struct *);
5367 laundromat_main(struct work_struct *laundry)
5370 struct delayed_work *dwork = to_delayed_work(laundry);
5371 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
5374 t = nfs4_laundromat(nn);
5375 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
5376 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
5379 static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
5381 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
5382 return nfserr_bad_stateid;
5387 access_permit_read(struct nfs4_ol_stateid *stp)
5389 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
5390 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
5391 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
5395 access_permit_write(struct nfs4_ol_stateid *stp)
5397 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
5398 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
5402 __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
5404 __be32 status = nfserr_openmode;
5406 /* For lock stateid's, we test the parent open, not the lock: */
5407 if (stp->st_openstp)
5408 stp = stp->st_openstp;
5409 if ((flags & WR_STATE) && !access_permit_write(stp))
5411 if ((flags & RD_STATE) && !access_permit_read(stp))
5418 static inline __be32
5419 check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
5421 if (ONE_STATEID(stateid) && (flags & RD_STATE))
5423 else if (opens_in_grace(net)) {
5424 /* Answer in remaining cases depends on existence of
5425 * conflicting state; so we must wait out the grace period. */
5426 return nfserr_grace;
5427 } else if (flags & WR_STATE)
5428 return nfs4_share_conflict(current_fh,
5429 NFS4_SHARE_DENY_WRITE);
5430 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
5431 return nfs4_share_conflict(current_fh,
5432 NFS4_SHARE_DENY_READ);
5436 * Allow READ/WRITE during grace period on recovered state only for files
5437 * that are not able to provide mandatory locking.
5440 grace_disallows_io(struct net *net, struct inode *inode)
5442 return opens_in_grace(net) && mandatory_lock(inode);
5445 static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
5448 * When sessions are used the stateid generation number is ignored
5451 if (has_session && in->si_generation == 0)
5454 if (in->si_generation == ref->si_generation)
5457 /* If the client sends us a stateid from the future, it's buggy: */
5458 if (nfsd4_stateid_generation_after(in, ref))
5459 return nfserr_bad_stateid;
5461 * However, we could see a stateid from the past, even from a
5462 * non-buggy client. For example, if the client sends a lock
5463 * while some IO is outstanding, the lock may bump si_generation
5464 * while the IO is still in flight. The client could avoid that
5465 * situation by waiting for responses on all the IO requests,
5466 * but better performance may result in retrying IO that
5467 * receives an old_stateid error if requests are rarely
5468 * reordered in flight:
5470 return nfserr_old_stateid;
5473 static __be32 nfsd4_stid_check_stateid_generation(stateid_t *in, struct nfs4_stid *s, bool has_session)
5477 spin_lock(&s->sc_lock);
5478 ret = nfsd4_verify_open_stid(s);
5480 ret = check_stateid_generation(in, &s->sc_stateid, has_session);
5481 spin_unlock(&s->sc_lock);
5485 static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
5487 if (ols->st_stateowner->so_is_open_owner &&
5488 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
5489 return nfserr_bad_stateid;
5493 static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
5495 struct nfs4_stid *s;
5496 __be32 status = nfserr_bad_stateid;
5498 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5499 CLOSE_STATEID(stateid))
5501 /* Client debugging aid. */
5502 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
5503 char addr_str[INET6_ADDRSTRLEN];
5504 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
5506 pr_warn_ratelimited("NFSD: client %s testing state ID "
5507 "with incorrect client ID\n", addr_str);
5510 spin_lock(&cl->cl_lock);
5511 s = find_stateid_locked(cl, stateid);
5514 status = nfsd4_stid_check_stateid_generation(stateid, s, 1);
5517 switch (s->sc_type) {
5518 case NFS4_DELEG_STID:
5521 case NFS4_REVOKED_DELEG_STID:
5522 status = nfserr_deleg_revoked;
5524 case NFS4_OPEN_STID:
5525 case NFS4_LOCK_STID:
5526 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
5529 printk("unknown stateid type %x\n", s->sc_type);
5531 case NFS4_CLOSED_STID:
5532 case NFS4_CLOSED_DELEG_STID:
5533 status = nfserr_bad_stateid;
5536 spin_unlock(&cl->cl_lock);
5541 nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
5542 stateid_t *stateid, unsigned char typemask,
5543 struct nfs4_stid **s, struct nfsd_net *nn)
5546 bool return_revoked = false;
5549 * only return revoked delegations if explicitly asked.
5550 * otherwise we report revoked or bad_stateid status.
5552 if (typemask & NFS4_REVOKED_DELEG_STID)
5553 return_revoked = true;
5554 else if (typemask & NFS4_DELEG_STID)
5555 typemask |= NFS4_REVOKED_DELEG_STID;
5557 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5558 CLOSE_STATEID(stateid))
5559 return nfserr_bad_stateid;
5560 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
5561 if (status == nfserr_stale_clientid) {
5562 if (cstate->session)
5563 return nfserr_bad_stateid;
5564 return nfserr_stale_stateid;
5568 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
5570 return nfserr_bad_stateid;
5571 if (((*s)->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) {
5573 if (cstate->minorversion)
5574 return nfserr_deleg_revoked;
5575 return nfserr_bad_stateid;
5580 static struct nfsd_file *
5581 nfs4_find_file(struct nfs4_stid *s, int flags)
5586 switch (s->sc_type) {
5587 case NFS4_DELEG_STID:
5588 if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
5590 return nfsd_file_get(s->sc_file->fi_deleg_file);
5591 case NFS4_OPEN_STID:
5592 case NFS4_LOCK_STID:
5593 if (flags & RD_STATE)
5594 return find_readable_file(s->sc_file);
5596 return find_writeable_file(s->sc_file);
5604 nfs4_check_olstateid(struct nfs4_ol_stateid *ols, int flags)
5608 status = nfsd4_check_openowner_confirmed(ols);
5611 return nfs4_check_openmode(ols, flags);
5615 nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
5616 struct nfsd_file **nfp, int flags)
5618 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
5619 struct nfsd_file *nf;
5622 nf = nfs4_find_file(s, flags);
5624 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
5625 acc | NFSD_MAY_OWNER_OVERRIDE);
5631 status = nfsd_file_acquire(rqstp, fhp, acc, &nf);
5641 * Checks for stateid operations
5644 nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
5645 struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
5646 stateid_t *stateid, int flags, struct nfsd_file **nfp)
5648 struct inode *ino = d_inode(fhp->fh_dentry);
5649 struct net *net = SVC_NET(rqstp);
5650 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5651 struct nfs4_stid *s = NULL;
5657 if (grace_disallows_io(net, ino))
5658 return nfserr_grace;
5660 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
5661 status = check_special_stateids(net, fhp, stateid, flags);
5665 status = nfsd4_lookup_stateid(cstate, stateid,
5666 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
5670 status = nfsd4_stid_check_stateid_generation(stateid, s,
5671 nfsd4_has_session(cstate));
5675 switch (s->sc_type) {
5676 case NFS4_DELEG_STID:
5677 status = nfs4_check_delegmode(delegstateid(s), flags);
5679 case NFS4_OPEN_STID:
5680 case NFS4_LOCK_STID:
5681 status = nfs4_check_olstateid(openlockstateid(s), flags);
5684 status = nfserr_bad_stateid;
5689 status = nfs4_check_fh(fhp, s);
5692 if (status == nfs_ok && nfp)
5693 status = nfs4_check_file(rqstp, fhp, s, nfp, flags);
5701 * Test if the stateid is valid
5704 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5705 union nfsd4_op_u *u)
5707 struct nfsd4_test_stateid *test_stateid = &u->test_stateid;
5708 struct nfsd4_test_stateid_id *stateid;
5709 struct nfs4_client *cl = cstate->session->se_client;
5711 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
5712 stateid->ts_id_status =
5713 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
5719 nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
5721 struct nfs4_ol_stateid *stp = openlockstateid(s);
5724 ret = nfsd4_lock_ol_stateid(stp);
5728 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5732 ret = nfserr_locks_held;
5733 if (check_for_locks(stp->st_stid.sc_file,
5734 lockowner(stp->st_stateowner)))
5737 release_lock_stateid(stp);
5741 mutex_unlock(&stp->st_mutex);
5748 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5749 union nfsd4_op_u *u)
5751 struct nfsd4_free_stateid *free_stateid = &u->free_stateid;
5752 stateid_t *stateid = &free_stateid->fr_stateid;
5753 struct nfs4_stid *s;
5754 struct nfs4_delegation *dp;
5755 struct nfs4_client *cl = cstate->session->se_client;
5756 __be32 ret = nfserr_bad_stateid;
5758 spin_lock(&cl->cl_lock);
5759 s = find_stateid_locked(cl, stateid);
5762 spin_lock(&s->sc_lock);
5763 switch (s->sc_type) {
5764 case NFS4_DELEG_STID:
5765 ret = nfserr_locks_held;
5767 case NFS4_OPEN_STID:
5768 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5771 ret = nfserr_locks_held;
5773 case NFS4_LOCK_STID:
5774 spin_unlock(&s->sc_lock);
5775 refcount_inc(&s->sc_count);
5776 spin_unlock(&cl->cl_lock);
5777 ret = nfsd4_free_lock_stateid(stateid, s);
5779 case NFS4_REVOKED_DELEG_STID:
5780 spin_unlock(&s->sc_lock);
5781 dp = delegstateid(s);
5782 list_del_init(&dp->dl_recall_lru);
5783 spin_unlock(&cl->cl_lock);
5787 /* Default falls through and returns nfserr_bad_stateid */
5789 spin_unlock(&s->sc_lock);
5791 spin_unlock(&cl->cl_lock);
5799 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
5800 RD_STATE : WR_STATE;
5803 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
5805 struct svc_fh *current_fh = &cstate->current_fh;
5806 struct nfs4_stateowner *sop = stp->st_stateowner;
5809 status = nfsd4_check_seqid(cstate, sop, seqid);
5812 status = nfsd4_lock_ol_stateid(stp);
5813 if (status != nfs_ok)
5815 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
5816 if (status == nfs_ok)
5817 status = nfs4_check_fh(current_fh, &stp->st_stid);
5818 if (status != nfs_ok)
5819 mutex_unlock(&stp->st_mutex);
5824 * Checks for sequence id mutating operations.
5827 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
5828 stateid_t *stateid, char typemask,
5829 struct nfs4_ol_stateid **stpp,
5830 struct nfsd_net *nn)
5833 struct nfs4_stid *s;
5834 struct nfs4_ol_stateid *stp = NULL;
5836 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
5837 seqid, STATEID_VAL(stateid));
5840 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
5843 stp = openlockstateid(s);
5844 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
5846 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
5850 nfs4_put_stid(&stp->st_stid);
5854 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
5855 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
5858 struct nfs4_openowner *oo;
5859 struct nfs4_ol_stateid *stp;
5861 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
5862 NFS4_OPEN_STID, &stp, nn);
5865 oo = openowner(stp->st_stateowner);
5866 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
5867 mutex_unlock(&stp->st_mutex);
5868 nfs4_put_stid(&stp->st_stid);
5869 return nfserr_bad_stateid;
5876 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5877 union nfsd4_op_u *u)
5879 struct nfsd4_open_confirm *oc = &u->open_confirm;
5881 struct nfs4_openowner *oo;
5882 struct nfs4_ol_stateid *stp;
5883 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5885 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
5886 cstate->current_fh.fh_dentry);
5888 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
5892 status = nfs4_preprocess_seqid_op(cstate,
5893 oc->oc_seqid, &oc->oc_req_stateid,
5894 NFS4_OPEN_STID, &stp, nn);
5897 oo = openowner(stp->st_stateowner);
5898 status = nfserr_bad_stateid;
5899 if (oo->oo_flags & NFS4_OO_CONFIRMED) {
5900 mutex_unlock(&stp->st_mutex);
5903 oo->oo_flags |= NFS4_OO_CONFIRMED;
5904 nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
5905 mutex_unlock(&stp->st_mutex);
5906 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
5907 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
5909 nfsd4_client_record_create(oo->oo_owner.so_client);
5912 nfs4_put_stid(&stp->st_stid);
5914 nfsd4_bump_seqid(cstate, status);
5918 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
5920 if (!test_access(access, stp))
5922 nfs4_file_put_access(stp->st_stid.sc_file, access);
5923 clear_access(access, stp);
5926 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
5928 switch (to_access) {
5929 case NFS4_SHARE_ACCESS_READ:
5930 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
5931 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
5933 case NFS4_SHARE_ACCESS_WRITE:
5934 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
5935 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
5937 case NFS4_SHARE_ACCESS_BOTH:
5945 nfsd4_open_downgrade(struct svc_rqst *rqstp,
5946 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
5948 struct nfsd4_open_downgrade *od = &u->open_downgrade;
5950 struct nfs4_ol_stateid *stp;
5951 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5953 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
5954 cstate->current_fh.fh_dentry);
5956 /* We don't yet support WANT bits: */
5957 if (od->od_deleg_want)
5958 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
5961 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
5962 &od->od_stateid, &stp, nn);
5965 status = nfserr_inval;
5966 if (!test_access(od->od_share_access, stp)) {
5967 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
5968 stp->st_access_bmap, od->od_share_access);
5971 if (!test_deny(od->od_share_deny, stp)) {
5972 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
5973 stp->st_deny_bmap, od->od_share_deny);
5976 nfs4_stateid_downgrade(stp, od->od_share_access);
5977 reset_union_bmap_deny(od->od_share_deny, stp);
5978 nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
5981 mutex_unlock(&stp->st_mutex);
5982 nfs4_put_stid(&stp->st_stid);
5984 nfsd4_bump_seqid(cstate, status);
5988 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
5990 struct nfs4_client *clp = s->st_stid.sc_client;
5992 LIST_HEAD(reaplist);
5994 spin_lock(&clp->cl_lock);
5995 unhashed = unhash_open_stateid(s, &reaplist);
5997 if (clp->cl_minorversion) {
5999 put_ol_stateid_locked(s, &reaplist);
6000 spin_unlock(&clp->cl_lock);
6001 free_ol_stateid_reaplist(&reaplist);
6003 spin_unlock(&clp->cl_lock);
6004 free_ol_stateid_reaplist(&reaplist);
6006 move_to_close_lru(s, clp->net);
6011 * nfs4_unlock_state() called after encode
6014 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6015 union nfsd4_op_u *u)
6017 struct nfsd4_close *close = &u->close;
6019 struct nfs4_ol_stateid *stp;
6020 struct net *net = SVC_NET(rqstp);
6021 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6023 dprintk("NFSD: nfsd4_close on file %pd\n",
6024 cstate->current_fh.fh_dentry);
6026 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
6028 NFS4_OPEN_STID|NFS4_CLOSED_STID,
6030 nfsd4_bump_seqid(cstate, status);
6034 stp->st_stid.sc_type = NFS4_CLOSED_STID;
6037 * Technically we don't _really_ have to increment or copy it, since
6038 * it should just be gone after this operation and we clobber the
6039 * copied value below, but we continue to do so here just to ensure
6040 * that racing ops see that there was a state change.
6042 nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
6044 nfsd4_close_open_stateid(stp);
6045 mutex_unlock(&stp->st_mutex);
6047 /* v4.1+ suggests that we send a special stateid in here, since the
6048 * clients should just ignore this anyway. Since this is not useful
6049 * for v4.0 clients either, we set it to the special close_stateid
6052 * See RFC5661 section 18.2.4, and RFC7530 section 16.2.5
6054 memcpy(&close->cl_stateid, &close_stateid, sizeof(close->cl_stateid));
6056 /* put reference from nfs4_preprocess_seqid_op */
6057 nfs4_put_stid(&stp->st_stid);
6063 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6064 union nfsd4_op_u *u)
6066 struct nfsd4_delegreturn *dr = &u->delegreturn;
6067 struct nfs4_delegation *dp;
6068 stateid_t *stateid = &dr->dr_stateid;
6069 struct nfs4_stid *s;
6071 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6073 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
6076 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
6079 dp = delegstateid(s);
6080 status = nfsd4_stid_check_stateid_generation(stateid, &dp->dl_stid, nfsd4_has_session(cstate));
6084 destroy_delegation(dp);
6086 nfs4_put_stid(&dp->dl_stid);
6092 end_offset(u64 start, u64 len)
6097 return end >= start ? end: NFS4_MAX_UINT64;
6100 /* last octet in a range */
6102 last_byte_offset(u64 start, u64 len)
6108 return end > start ? end - 1: NFS4_MAX_UINT64;
6112 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
6113 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
6114 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
6115 * locking, this prevents us from being completely protocol-compliant. The
6116 * real solution to this problem is to start using unsigned file offsets in
6117 * the VFS, but this is a very deep change!
6120 nfs4_transform_lock_offset(struct file_lock *lock)
6122 if (lock->fl_start < 0)
6123 lock->fl_start = OFFSET_MAX;
6124 if (lock->fl_end < 0)
6125 lock->fl_end = OFFSET_MAX;
6129 nfsd4_fl_get_owner(fl_owner_t owner)
6131 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
6133 nfs4_get_stateowner(&lo->lo_owner);
6138 nfsd4_fl_put_owner(fl_owner_t owner)
6140 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
6143 nfs4_put_stateowner(&lo->lo_owner);
6147 nfsd4_lm_notify(struct file_lock *fl)
6149 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
6150 struct net *net = lo->lo_owner.so_client->net;
6151 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6152 struct nfsd4_blocked_lock *nbl = container_of(fl,
6153 struct nfsd4_blocked_lock, nbl_lock);
6156 /* An empty list means that something else is going to be using it */
6157 spin_lock(&nn->blocked_locks_lock);
6158 if (!list_empty(&nbl->nbl_list)) {
6159 list_del_init(&nbl->nbl_list);
6160 list_del_init(&nbl->nbl_lru);
6163 spin_unlock(&nn->blocked_locks_lock);
6166 nfsd4_run_cb(&nbl->nbl_cb);
6169 static const struct lock_manager_operations nfsd_posix_mng_ops = {
6170 .lm_notify = nfsd4_lm_notify,
6171 .lm_get_owner = nfsd4_fl_get_owner,
6172 .lm_put_owner = nfsd4_fl_put_owner,
6176 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
6178 struct nfs4_lockowner *lo;
6180 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
6181 lo = (struct nfs4_lockowner *) fl->fl_owner;
6182 xdr_netobj_dup(&deny->ld_owner, &lo->lo_owner.so_owner,
6184 if (!deny->ld_owner.data)
6185 /* We just don't care that much */
6187 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
6190 deny->ld_owner.len = 0;
6191 deny->ld_owner.data = NULL;
6192 deny->ld_clientid.cl_boot = 0;
6193 deny->ld_clientid.cl_id = 0;
6195 deny->ld_start = fl->fl_start;
6196 deny->ld_length = NFS4_MAX_UINT64;
6197 if (fl->fl_end != NFS4_MAX_UINT64)
6198 deny->ld_length = fl->fl_end - fl->fl_start + 1;
6199 deny->ld_type = NFS4_READ_LT;
6200 if (fl->fl_type != F_RDLCK)
6201 deny->ld_type = NFS4_WRITE_LT;
6204 static struct nfs4_lockowner *
6205 find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
6207 unsigned int strhashval = ownerstr_hashval(owner);
6208 struct nfs4_stateowner *so;
6210 lockdep_assert_held(&clp->cl_lock);
6212 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
6214 if (so->so_is_open_owner)
6216 if (same_owner_str(so, owner))
6217 return lockowner(nfs4_get_stateowner(so));
6222 static struct nfs4_lockowner *
6223 find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
6225 struct nfs4_lockowner *lo;
6227 spin_lock(&clp->cl_lock);
6228 lo = find_lockowner_str_locked(clp, owner);
6229 spin_unlock(&clp->cl_lock);
6233 static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
6235 unhash_lockowner_locked(lockowner(sop));
6238 static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
6240 struct nfs4_lockowner *lo = lockowner(sop);
6242 kmem_cache_free(lockowner_slab, lo);
6245 static const struct nfs4_stateowner_operations lockowner_ops = {
6246 .so_unhash = nfs4_unhash_lockowner,
6247 .so_free = nfs4_free_lockowner,
6251 * Alloc a lock owner structure.
6252 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
6255 * strhashval = ownerstr_hashval
6257 static struct nfs4_lockowner *
6258 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
6259 struct nfs4_ol_stateid *open_stp,
6260 struct nfsd4_lock *lock)
6262 struct nfs4_lockowner *lo, *ret;
6264 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
6267 INIT_LIST_HEAD(&lo->lo_blocked);
6268 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
6269 lo->lo_owner.so_is_open_owner = 0;
6270 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
6271 lo->lo_owner.so_ops = &lockowner_ops;
6272 spin_lock(&clp->cl_lock);
6273 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
6275 list_add(&lo->lo_owner.so_strhash,
6276 &clp->cl_ownerstr_hashtbl[strhashval]);
6279 nfs4_free_stateowner(&lo->lo_owner);
6281 spin_unlock(&clp->cl_lock);
6285 static struct nfs4_ol_stateid *
6286 find_lock_stateid(const struct nfs4_lockowner *lo,
6287 const struct nfs4_ol_stateid *ost)
6289 struct nfs4_ol_stateid *lst;
6291 lockdep_assert_held(&ost->st_stid.sc_client->cl_lock);
6293 /* If ost is not hashed, ost->st_locks will not be valid */
6294 if (!nfs4_ol_stateid_unhashed(ost))
6295 list_for_each_entry(lst, &ost->st_locks, st_locks) {
6296 if (lst->st_stateowner == &lo->lo_owner) {
6297 refcount_inc(&lst->st_stid.sc_count);
6304 static struct nfs4_ol_stateid *
6305 init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
6306 struct nfs4_file *fp, struct inode *inode,
6307 struct nfs4_ol_stateid *open_stp)
6309 struct nfs4_client *clp = lo->lo_owner.so_client;
6310 struct nfs4_ol_stateid *retstp;
6312 mutex_init(&stp->st_mutex);
6313 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
6315 spin_lock(&clp->cl_lock);
6316 if (nfs4_ol_stateid_unhashed(open_stp))
6318 retstp = find_lock_stateid(lo, open_stp);
6321 refcount_inc(&stp->st_stid.sc_count);
6322 stp->st_stid.sc_type = NFS4_LOCK_STID;
6323 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
6325 stp->st_stid.sc_file = fp;
6326 stp->st_access_bmap = 0;
6327 stp->st_deny_bmap = open_stp->st_deny_bmap;
6328 stp->st_openstp = open_stp;
6329 spin_lock(&fp->fi_lock);
6330 list_add(&stp->st_locks, &open_stp->st_locks);
6331 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
6332 list_add(&stp->st_perfile, &fp->fi_stateids);
6333 spin_unlock(&fp->fi_lock);
6334 spin_unlock(&clp->cl_lock);
6337 spin_unlock(&clp->cl_lock);
6338 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
6339 nfs4_put_stid(&retstp->st_stid);
6342 /* To keep mutex tracking happy */
6343 mutex_unlock(&stp->st_mutex);
6346 spin_unlock(&clp->cl_lock);
6347 mutex_unlock(&stp->st_mutex);
6351 static struct nfs4_ol_stateid *
6352 find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
6353 struct inode *inode, struct nfs4_ol_stateid *ost,
6356 struct nfs4_stid *ns = NULL;
6357 struct nfs4_ol_stateid *lst;
6358 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
6359 struct nfs4_client *clp = oo->oo_owner.so_client;
6362 spin_lock(&clp->cl_lock);
6363 lst = find_lock_stateid(lo, ost);
6364 spin_unlock(&clp->cl_lock);
6366 if (nfsd4_lock_ol_stateid(lst) == nfs_ok)
6368 nfs4_put_stid(&lst->st_stid);
6370 ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid);
6374 lst = init_lock_stateid(openlockstateid(ns), lo, fi, inode, ost);
6375 if (lst == openlockstateid(ns))
6384 check_lock_length(u64 offset, u64 length)
6386 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
6387 (length > ~offset)));
6390 static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
6392 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
6394 lockdep_assert_held(&fp->fi_lock);
6396 if (test_access(access, lock_stp))
6398 __nfs4_file_get_access(fp, access);
6399 set_access(access, lock_stp);
6403 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
6404 struct nfs4_ol_stateid *ost,
6405 struct nfsd4_lock *lock,
6406 struct nfs4_ol_stateid **plst, bool *new)
6409 struct nfs4_file *fi = ost->st_stid.sc_file;
6410 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
6411 struct nfs4_client *cl = oo->oo_owner.so_client;
6412 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
6413 struct nfs4_lockowner *lo;
6414 struct nfs4_ol_stateid *lst;
6415 unsigned int strhashval;
6417 lo = find_lockowner_str(cl, &lock->lk_new_owner);
6419 strhashval = ownerstr_hashval(&lock->lk_new_owner);
6420 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
6422 return nfserr_jukebox;
6424 /* with an existing lockowner, seqids must be the same */
6425 status = nfserr_bad_seqid;
6426 if (!cstate->minorversion &&
6427 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
6431 lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
6433 status = nfserr_jukebox;
6440 nfs4_put_stateowner(&lo->lo_owner);
6448 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6449 union nfsd4_op_u *u)
6451 struct nfsd4_lock *lock = &u->lock;
6452 struct nfs4_openowner *open_sop = NULL;
6453 struct nfs4_lockowner *lock_sop = NULL;
6454 struct nfs4_ol_stateid *lock_stp = NULL;
6455 struct nfs4_ol_stateid *open_stp = NULL;
6456 struct nfs4_file *fp;
6457 struct nfsd_file *nf = NULL;
6458 struct nfsd4_blocked_lock *nbl = NULL;
6459 struct file_lock *file_lock = NULL;
6460 struct file_lock *conflock = NULL;
6465 unsigned char fl_type;
6466 unsigned int fl_flags = FL_POSIX;
6467 struct net *net = SVC_NET(rqstp);
6468 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6470 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
6471 (long long) lock->lk_offset,
6472 (long long) lock->lk_length);
6474 if (check_lock_length(lock->lk_offset, lock->lk_length))
6475 return nfserr_inval;
6477 if ((status = fh_verify(rqstp, &cstate->current_fh,
6478 S_IFREG, NFSD_MAY_LOCK))) {
6479 dprintk("NFSD: nfsd4_lock: permission denied!\n");
6483 if (lock->lk_is_new) {
6484 if (nfsd4_has_session(cstate))
6485 /* See rfc 5661 18.10.3: given clientid is ignored: */
6486 memcpy(&lock->lk_new_clientid,
6487 &cstate->session->se_client->cl_clientid,
6488 sizeof(clientid_t));
6490 status = nfserr_stale_clientid;
6491 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
6494 /* validate and update open stateid and open seqid */
6495 status = nfs4_preprocess_confirmed_seqid_op(cstate,
6496 lock->lk_new_open_seqid,
6497 &lock->lk_new_open_stateid,
6501 mutex_unlock(&open_stp->st_mutex);
6502 open_sop = openowner(open_stp->st_stateowner);
6503 status = nfserr_bad_stateid;
6504 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
6505 &lock->lk_new_clientid))
6507 status = lookup_or_create_lock_state(cstate, open_stp, lock,
6510 status = nfs4_preprocess_seqid_op(cstate,
6511 lock->lk_old_lock_seqid,
6512 &lock->lk_old_lock_stateid,
6513 NFS4_LOCK_STID, &lock_stp, nn);
6517 lock_sop = lockowner(lock_stp->st_stateowner);
6519 lkflg = setlkflg(lock->lk_type);
6520 status = nfs4_check_openmode(lock_stp, lkflg);
6524 status = nfserr_grace;
6525 if (locks_in_grace(net) && !lock->lk_reclaim)
6527 status = nfserr_no_grace;
6528 if (!locks_in_grace(net) && lock->lk_reclaim)
6531 fp = lock_stp->st_stid.sc_file;
6532 switch (lock->lk_type) {
6534 if (nfsd4_has_session(cstate))
6535 fl_flags |= FL_SLEEP;
6538 spin_lock(&fp->fi_lock);
6539 nf = find_readable_file_locked(fp);
6541 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
6542 spin_unlock(&fp->fi_lock);
6545 case NFS4_WRITEW_LT:
6546 if (nfsd4_has_session(cstate))
6547 fl_flags |= FL_SLEEP;
6550 spin_lock(&fp->fi_lock);
6551 nf = find_writeable_file_locked(fp);
6553 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
6554 spin_unlock(&fp->fi_lock);
6558 status = nfserr_inval;
6563 status = nfserr_openmode;
6567 nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
6569 dprintk("NFSD: %s: unable to allocate block!\n", __func__);
6570 status = nfserr_jukebox;
6574 file_lock = &nbl->nbl_lock;
6575 file_lock->fl_type = fl_type;
6576 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
6577 file_lock->fl_pid = current->tgid;
6578 file_lock->fl_file = nf->nf_file;
6579 file_lock->fl_flags = fl_flags;
6580 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6581 file_lock->fl_start = lock->lk_offset;
6582 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
6583 nfs4_transform_lock_offset(file_lock);
6585 conflock = locks_alloc_lock();
6587 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6588 status = nfserr_jukebox;
6592 if (fl_flags & FL_SLEEP) {
6593 nbl->nbl_time = get_seconds();
6594 spin_lock(&nn->blocked_locks_lock);
6595 list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
6596 list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
6597 spin_unlock(&nn->blocked_locks_lock);
6600 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, conflock);
6602 case 0: /* success! */
6603 nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
6605 if (lock->lk_reclaim)
6606 nn->somebody_reclaimed = true;
6608 case FILE_LOCK_DEFERRED:
6611 case -EAGAIN: /* conflock holds conflicting lock */
6612 status = nfserr_denied;
6613 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
6614 nfs4_set_lock_denied(conflock, &lock->lk_denied);
6617 status = nfserr_deadlock;
6620 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
6621 status = nfserrno(err);
6626 /* dequeue it if we queued it before */
6627 if (fl_flags & FL_SLEEP) {
6628 spin_lock(&nn->blocked_locks_lock);
6629 list_del_init(&nbl->nbl_list);
6630 list_del_init(&nbl->nbl_lru);
6631 spin_unlock(&nn->blocked_locks_lock);
6633 free_blocked_lock(nbl);
6638 /* Bump seqid manually if the 4.0 replay owner is openowner */
6639 if (cstate->replay_owner &&
6640 cstate->replay_owner != &lock_sop->lo_owner &&
6641 seqid_mutating_err(ntohl(status)))
6642 lock_sop->lo_owner.so_seqid++;
6645 * If this is a new, never-before-used stateid, and we are
6646 * returning an error, then just go ahead and release it.
6649 release_lock_stateid(lock_stp);
6651 mutex_unlock(&lock_stp->st_mutex);
6653 nfs4_put_stid(&lock_stp->st_stid);
6656 nfs4_put_stid(&open_stp->st_stid);
6657 nfsd4_bump_seqid(cstate, status);
6659 locks_free_lock(conflock);
6664 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
6665 * so we do a temporary open here just to get an open file to pass to
6666 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
6669 static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
6671 struct nfsd_file *nf;
6672 __be32 err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf);
6674 err = nfserrno(vfs_test_lock(nf->nf_file, lock));
6684 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6685 union nfsd4_op_u *u)
6687 struct nfsd4_lockt *lockt = &u->lockt;
6688 struct file_lock *file_lock = NULL;
6689 struct nfs4_lockowner *lo = NULL;
6691 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6693 if (locks_in_grace(SVC_NET(rqstp)))
6694 return nfserr_grace;
6696 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
6697 return nfserr_inval;
6699 if (!nfsd4_has_session(cstate)) {
6700 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
6705 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
6708 file_lock = locks_alloc_lock();
6710 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6711 status = nfserr_jukebox;
6715 switch (lockt->lt_type) {
6718 file_lock->fl_type = F_RDLCK;
6721 case NFS4_WRITEW_LT:
6722 file_lock->fl_type = F_WRLCK;
6725 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
6726 status = nfserr_inval;
6730 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
6732 file_lock->fl_owner = (fl_owner_t)lo;
6733 file_lock->fl_pid = current->tgid;
6734 file_lock->fl_flags = FL_POSIX;
6736 file_lock->fl_start = lockt->lt_offset;
6737 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
6739 nfs4_transform_lock_offset(file_lock);
6741 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
6745 if (file_lock->fl_type != F_UNLCK) {
6746 status = nfserr_denied;
6747 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
6751 nfs4_put_stateowner(&lo->lo_owner);
6753 locks_free_lock(file_lock);
6758 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6759 union nfsd4_op_u *u)
6761 struct nfsd4_locku *locku = &u->locku;
6762 struct nfs4_ol_stateid *stp;
6763 struct nfsd_file *nf = NULL;
6764 struct file_lock *file_lock = NULL;
6767 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6769 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
6770 (long long) locku->lu_offset,
6771 (long long) locku->lu_length);
6773 if (check_lock_length(locku->lu_offset, locku->lu_length))
6774 return nfserr_inval;
6776 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
6777 &locku->lu_stateid, NFS4_LOCK_STID,
6781 nf = find_any_file(stp->st_stid.sc_file);
6783 status = nfserr_lock_range;
6786 file_lock = locks_alloc_lock();
6788 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6789 status = nfserr_jukebox;
6793 file_lock->fl_type = F_UNLCK;
6794 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
6795 file_lock->fl_pid = current->tgid;
6796 file_lock->fl_file = nf->nf_file;
6797 file_lock->fl_flags = FL_POSIX;
6798 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6799 file_lock->fl_start = locku->lu_offset;
6801 file_lock->fl_end = last_byte_offset(locku->lu_offset,
6803 nfs4_transform_lock_offset(file_lock);
6805 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, NULL);
6807 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
6810 nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
6814 mutex_unlock(&stp->st_mutex);
6815 nfs4_put_stid(&stp->st_stid);
6817 nfsd4_bump_seqid(cstate, status);
6819 locks_free_lock(file_lock);
6823 status = nfserrno(err);
6829 * true: locks held by lockowner
6830 * false: no locks held by lockowner
6833 check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
6835 struct file_lock *fl;
6837 struct nfsd_file *nf = find_any_file(fp);
6838 struct inode *inode;
6839 struct file_lock_context *flctx;
6842 /* Any valid lock stateid should have some sort of access */
6847 inode = locks_inode(nf->nf_file);
6848 flctx = inode->i_flctx;
6850 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
6851 spin_lock(&flctx->flc_lock);
6852 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
6853 if (fl->fl_owner == (fl_owner_t)lowner) {
6858 spin_unlock(&flctx->flc_lock);
6865 nfsd4_release_lockowner(struct svc_rqst *rqstp,
6866 struct nfsd4_compound_state *cstate,
6867 union nfsd4_op_u *u)
6869 struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner;
6870 clientid_t *clid = &rlockowner->rl_clientid;
6871 struct nfs4_stateowner *sop;
6872 struct nfs4_lockowner *lo = NULL;
6873 struct nfs4_ol_stateid *stp;
6874 struct xdr_netobj *owner = &rlockowner->rl_owner;
6875 unsigned int hashval = ownerstr_hashval(owner);
6877 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6878 struct nfs4_client *clp;
6879 LIST_HEAD (reaplist);
6881 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
6882 clid->cl_boot, clid->cl_id);
6884 status = lookup_clientid(clid, cstate, nn);
6889 /* Find the matching lock stateowner */
6890 spin_lock(&clp->cl_lock);
6891 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
6894 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
6897 if (atomic_read(&sop->so_count) != 1) {
6898 spin_unlock(&clp->cl_lock);
6899 return nfserr_locks_held;
6902 lo = lockowner(sop);
6903 nfs4_get_stateowner(sop);
6907 spin_unlock(&clp->cl_lock);
6911 unhash_lockowner_locked(lo);
6912 while (!list_empty(&lo->lo_owner.so_stateids)) {
6913 stp = list_first_entry(&lo->lo_owner.so_stateids,
6914 struct nfs4_ol_stateid,
6916 WARN_ON(!unhash_lock_stateid(stp));
6917 put_ol_stateid_locked(stp, &reaplist);
6919 spin_unlock(&clp->cl_lock);
6920 free_ol_stateid_reaplist(&reaplist);
6921 remove_blocked_locks(lo);
6922 nfs4_put_stateowner(&lo->lo_owner);
6927 static inline struct nfs4_client_reclaim *
6930 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
6934 nfs4_has_reclaimed_state(struct xdr_netobj name, struct nfsd_net *nn)
6936 struct nfs4_client_reclaim *crp;
6938 crp = nfsd4_find_reclaim_client(name, nn);
6939 return (crp && crp->cr_clp);
6943 * failure => all reset bets are off, nfserr_no_grace...
6945 * The caller is responsible for freeing name.data if NULL is returned (it
6946 * will be freed in nfs4_remove_reclaim_record in the normal case).
6948 struct nfs4_client_reclaim *
6949 nfs4_client_to_reclaim(struct xdr_netobj name, struct xdr_netobj princhash,
6950 struct nfsd_net *nn)
6952 unsigned int strhashval;
6953 struct nfs4_client_reclaim *crp;
6955 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", name.len, name.data);
6956 crp = alloc_reclaim();
6958 strhashval = clientstr_hashval(name);
6959 INIT_LIST_HEAD(&crp->cr_strhash);
6960 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
6961 crp->cr_name.data = name.data;
6962 crp->cr_name.len = name.len;
6963 crp->cr_princhash.data = princhash.data;
6964 crp->cr_princhash.len = princhash.len;
6966 nn->reclaim_str_hashtbl_size++;
6972 nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
6974 list_del(&crp->cr_strhash);
6975 kfree(crp->cr_name.data);
6976 kfree(crp->cr_princhash.data);
6978 nn->reclaim_str_hashtbl_size--;
6982 nfs4_release_reclaim(struct nfsd_net *nn)
6984 struct nfs4_client_reclaim *crp = NULL;
6987 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6988 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
6989 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
6990 struct nfs4_client_reclaim, cr_strhash);
6991 nfs4_remove_reclaim_record(crp, nn);
6994 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
6998 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
6999 struct nfs4_client_reclaim *
7000 nfsd4_find_reclaim_client(struct xdr_netobj name, struct nfsd_net *nn)
7002 unsigned int strhashval;
7003 struct nfs4_client_reclaim *crp = NULL;
7005 dprintk("NFSD: nfs4_find_reclaim_client for name %.*s\n", name.len, name.data);
7007 strhashval = clientstr_hashval(name);
7008 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
7009 if (compare_blob(&crp->cr_name, &name) == 0) {
7017 * Called from OPEN. Look for clientid in reclaim list.
7020 nfs4_check_open_reclaim(clientid_t *clid,
7021 struct nfsd4_compound_state *cstate,
7022 struct nfsd_net *nn)
7026 /* find clientid in conf_id_hashtbl */
7027 status = lookup_clientid(clid, cstate, nn);
7029 return nfserr_reclaim_bad;
7031 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
7032 return nfserr_no_grace;
7034 if (nfsd4_client_record_check(cstate->clp))
7035 return nfserr_reclaim_bad;
7040 #ifdef CONFIG_NFSD_FAULT_INJECTION
7042 put_client(struct nfs4_client *clp)
7044 atomic_dec(&clp->cl_rpc_users);
7047 static struct nfs4_client *
7048 nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
7050 struct nfs4_client *clp;
7051 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7054 if (!nfsd_netns_ready(nn))
7057 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
7058 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
7065 nfsd_inject_print_clients(void)
7067 struct nfs4_client *clp;
7069 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7071 char buf[INET6_ADDRSTRLEN];
7073 if (!nfsd_netns_ready(nn))
7076 spin_lock(&nn->client_lock);
7077 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
7078 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
7079 pr_info("NFS Client: %s\n", buf);
7082 spin_unlock(&nn->client_lock);
7088 nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
7091 struct nfs4_client *clp;
7092 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7095 if (!nfsd_netns_ready(nn))
7098 spin_lock(&nn->client_lock);
7099 clp = nfsd_find_client(addr, addr_size);
7101 if (mark_client_expired_locked(clp) == nfs_ok)
7106 spin_unlock(&nn->client_lock);
7115 nfsd_inject_forget_clients(u64 max)
7118 struct nfs4_client *clp, *next;
7119 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7121 LIST_HEAD(reaplist);
7123 if (!nfsd_netns_ready(nn))
7126 spin_lock(&nn->client_lock);
7127 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
7128 if (mark_client_expired_locked(clp) == nfs_ok) {
7129 list_add(&clp->cl_lru, &reaplist);
7130 if (max != 0 && ++count >= max)
7134 spin_unlock(&nn->client_lock);
7136 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
7142 static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
7145 char buf[INET6_ADDRSTRLEN];
7146 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
7147 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
7151 nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
7152 struct list_head *collect)
7154 struct nfs4_client *clp = lst->st_stid.sc_client;
7155 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7161 lockdep_assert_held(&nn->client_lock);
7162 atomic_inc(&clp->cl_rpc_users);
7163 list_add(&lst->st_locks, collect);
7166 static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
7167 struct list_head *collect,
7168 bool (*func)(struct nfs4_ol_stateid *))
7170 struct nfs4_openowner *oop;
7171 struct nfs4_ol_stateid *stp, *st_next;
7172 struct nfs4_ol_stateid *lst, *lst_next;
7175 spin_lock(&clp->cl_lock);
7176 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
7177 list_for_each_entry_safe(stp, st_next,
7178 &oop->oo_owner.so_stateids, st_perstateowner) {
7179 list_for_each_entry_safe(lst, lst_next,
7180 &stp->st_locks, st_locks) {
7183 nfsd_inject_add_lock_to_list(lst,
7188 * Despite the fact that these functions deal
7189 * with 64-bit integers for "count", we must
7190 * ensure that it doesn't blow up the
7191 * clp->cl_rpc_users. Throw a warning if we
7192 * start to approach INT_MAX here.
7194 WARN_ON_ONCE(count == (INT_MAX / 2));
7201 spin_unlock(&clp->cl_lock);
7207 nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
7210 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
7214 nfsd_print_client_locks(struct nfs4_client *clp)
7216 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
7217 nfsd_print_count(clp, count, "locked files");
7222 nfsd_inject_print_locks(void)
7224 struct nfs4_client *clp;
7226 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7229 if (!nfsd_netns_ready(nn))
7232 spin_lock(&nn->client_lock);
7233 list_for_each_entry(clp, &nn->client_lru, cl_lru)
7234 count += nfsd_print_client_locks(clp);
7235 spin_unlock(&nn->client_lock);
7241 nfsd_reap_locks(struct list_head *reaplist)
7243 struct nfs4_client *clp;
7244 struct nfs4_ol_stateid *stp, *next;
7246 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
7247 list_del_init(&stp->st_locks);
7248 clp = stp->st_stid.sc_client;
7249 nfs4_put_stid(&stp->st_stid);
7255 nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
7257 unsigned int count = 0;
7258 struct nfs4_client *clp;
7259 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7261 LIST_HEAD(reaplist);
7263 if (!nfsd_netns_ready(nn))
7266 spin_lock(&nn->client_lock);
7267 clp = nfsd_find_client(addr, addr_size);
7269 count = nfsd_collect_client_locks(clp, &reaplist, 0);
7270 spin_unlock(&nn->client_lock);
7271 nfsd_reap_locks(&reaplist);
7276 nfsd_inject_forget_locks(u64 max)
7279 struct nfs4_client *clp;
7280 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7282 LIST_HEAD(reaplist);
7284 if (!nfsd_netns_ready(nn))
7287 spin_lock(&nn->client_lock);
7288 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
7289 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
7290 if (max != 0 && count >= max)
7293 spin_unlock(&nn->client_lock);
7294 nfsd_reap_locks(&reaplist);
7299 nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
7300 struct list_head *collect,
7301 void (*func)(struct nfs4_openowner *))
7303 struct nfs4_openowner *oop, *next;
7304 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7308 lockdep_assert_held(&nn->client_lock);
7310 spin_lock(&clp->cl_lock);
7311 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
7315 atomic_inc(&clp->cl_rpc_users);
7316 list_add(&oop->oo_perclient, collect);
7321 * Despite the fact that these functions deal with
7322 * 64-bit integers for "count", we must ensure that
7323 * it doesn't blow up the clp->cl_rpc_users. Throw a
7324 * warning if we start to approach INT_MAX here.
7326 WARN_ON_ONCE(count == (INT_MAX / 2));
7330 spin_unlock(&clp->cl_lock);
7336 nfsd_print_client_openowners(struct nfs4_client *clp)
7338 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
7340 nfsd_print_count(clp, count, "openowners");
7345 nfsd_collect_client_openowners(struct nfs4_client *clp,
7346 struct list_head *collect, u64 max)
7348 return nfsd_foreach_client_openowner(clp, max, collect,
7349 unhash_openowner_locked);
7353 nfsd_inject_print_openowners(void)
7355 struct nfs4_client *clp;
7357 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7360 if (!nfsd_netns_ready(nn))
7363 spin_lock(&nn->client_lock);
7364 list_for_each_entry(clp, &nn->client_lru, cl_lru)
7365 count += nfsd_print_client_openowners(clp);
7366 spin_unlock(&nn->client_lock);
7372 nfsd_reap_openowners(struct list_head *reaplist)
7374 struct nfs4_client *clp;
7375 struct nfs4_openowner *oop, *next;
7377 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
7378 list_del_init(&oop->oo_perclient);
7379 clp = oop->oo_owner.so_client;
7380 release_openowner(oop);
7386 nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
7389 unsigned int count = 0;
7390 struct nfs4_client *clp;
7391 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7393 LIST_HEAD(reaplist);
7395 if (!nfsd_netns_ready(nn))
7398 spin_lock(&nn->client_lock);
7399 clp = nfsd_find_client(addr, addr_size);
7401 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
7402 spin_unlock(&nn->client_lock);
7403 nfsd_reap_openowners(&reaplist);
7408 nfsd_inject_forget_openowners(u64 max)
7411 struct nfs4_client *clp;
7412 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7414 LIST_HEAD(reaplist);
7416 if (!nfsd_netns_ready(nn))
7419 spin_lock(&nn->client_lock);
7420 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
7421 count += nfsd_collect_client_openowners(clp, &reaplist,
7423 if (max != 0 && count >= max)
7426 spin_unlock(&nn->client_lock);
7427 nfsd_reap_openowners(&reaplist);
7431 static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
7432 struct list_head *victims)
7434 struct nfs4_delegation *dp, *next;
7435 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7439 lockdep_assert_held(&nn->client_lock);
7441 spin_lock(&state_lock);
7442 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
7445 * It's not safe to mess with delegations that have a
7446 * non-zero dl_time. They might have already been broken
7447 * and could be processed by the laundromat outside of
7448 * the state_lock. Just leave them be.
7450 if (dp->dl_time != 0)
7453 atomic_inc(&clp->cl_rpc_users);
7454 WARN_ON(!unhash_delegation_locked(dp));
7455 list_add(&dp->dl_recall_lru, victims);
7459 * Despite the fact that these functions deal with
7460 * 64-bit integers for "count", we must ensure that
7461 * it doesn't blow up the clp->cl_rpc_users. Throw a
7462 * warning if we start to approach INT_MAX here.
7464 WARN_ON_ONCE(count == (INT_MAX / 2));
7468 spin_unlock(&state_lock);
7473 nfsd_print_client_delegations(struct nfs4_client *clp)
7475 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
7477 nfsd_print_count(clp, count, "delegations");
7482 nfsd_inject_print_delegations(void)
7484 struct nfs4_client *clp;
7486 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7489 if (!nfsd_netns_ready(nn))
7492 spin_lock(&nn->client_lock);
7493 list_for_each_entry(clp, &nn->client_lru, cl_lru)
7494 count += nfsd_print_client_delegations(clp);
7495 spin_unlock(&nn->client_lock);
7501 nfsd_forget_delegations(struct list_head *reaplist)
7503 struct nfs4_client *clp;
7504 struct nfs4_delegation *dp, *next;
7506 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
7507 list_del_init(&dp->dl_recall_lru);
7508 clp = dp->dl_stid.sc_client;
7509 revoke_delegation(dp);
7515 nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
7519 struct nfs4_client *clp;
7520 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7522 LIST_HEAD(reaplist);
7524 if (!nfsd_netns_ready(nn))
7527 spin_lock(&nn->client_lock);
7528 clp = nfsd_find_client(addr, addr_size);
7530 count = nfsd_find_all_delegations(clp, 0, &reaplist);
7531 spin_unlock(&nn->client_lock);
7533 nfsd_forget_delegations(&reaplist);
7538 nfsd_inject_forget_delegations(u64 max)
7541 struct nfs4_client *clp;
7542 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7544 LIST_HEAD(reaplist);
7546 if (!nfsd_netns_ready(nn))
7549 spin_lock(&nn->client_lock);
7550 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
7551 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
7552 if (max != 0 && count >= max)
7555 spin_unlock(&nn->client_lock);
7556 nfsd_forget_delegations(&reaplist);
7561 nfsd_recall_delegations(struct list_head *reaplist)
7563 struct nfs4_client *clp;
7564 struct nfs4_delegation *dp, *next;
7566 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
7567 list_del_init(&dp->dl_recall_lru);
7568 clp = dp->dl_stid.sc_client;
7570 * We skipped all entries that had a zero dl_time before,
7571 * so we can now reset the dl_time back to 0. If a delegation
7572 * break comes in now, then it won't make any difference since
7573 * we're recalling it either way.
7575 spin_lock(&state_lock);
7577 spin_unlock(&state_lock);
7578 nfsd_break_one_deleg(dp);
7584 nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
7588 struct nfs4_client *clp;
7589 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7591 LIST_HEAD(reaplist);
7593 if (!nfsd_netns_ready(nn))
7596 spin_lock(&nn->client_lock);
7597 clp = nfsd_find_client(addr, addr_size);
7599 count = nfsd_find_all_delegations(clp, 0, &reaplist);
7600 spin_unlock(&nn->client_lock);
7602 nfsd_recall_delegations(&reaplist);
7607 nfsd_inject_recall_delegations(u64 max)
7610 struct nfs4_client *clp, *next;
7611 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7613 LIST_HEAD(reaplist);
7615 if (!nfsd_netns_ready(nn))
7618 spin_lock(&nn->client_lock);
7619 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
7620 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
7621 if (max != 0 && ++count >= max)
7624 spin_unlock(&nn->client_lock);
7625 nfsd_recall_delegations(&reaplist);
7628 #endif /* CONFIG_NFSD_FAULT_INJECTION */
7631 * Since the lifetime of a delegation isn't limited to that of an open, a
7632 * client may quite reasonably hang on to a delegation as long as it has
7633 * the inode cached. This becomes an obvious problem the first time a
7634 * client's inode cache approaches the size of the server's total memory.
7636 * For now we avoid this problem by imposing a hard limit on the number
7637 * of delegations, which varies according to the server's memory size.
7640 set_max_delegations(void)
7643 * Allow at most 4 delegations per megabyte of RAM. Quick
7644 * estimates suggest that in the worst case (where every delegation
7645 * is for a different inode), a delegation could take about 1.5K,
7646 * giving a worst case usage of about 6% of memory.
7648 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
7651 static int nfs4_state_create_net(struct net *net)
7653 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7656 nn->conf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7657 sizeof(struct list_head),
7659 if (!nn->conf_id_hashtbl)
7661 nn->unconf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7662 sizeof(struct list_head),
7664 if (!nn->unconf_id_hashtbl)
7666 nn->sessionid_hashtbl = kmalloc_array(SESSION_HASH_SIZE,
7667 sizeof(struct list_head),
7669 if (!nn->sessionid_hashtbl)
7672 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7673 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
7674 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
7676 for (i = 0; i < SESSION_HASH_SIZE; i++)
7677 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
7678 nn->conf_name_tree = RB_ROOT;
7679 nn->unconf_name_tree = RB_ROOT;
7680 nn->boot_time = get_seconds();
7681 nn->grace_ended = false;
7682 nn->nfsd4_manager.block_opens = true;
7683 INIT_LIST_HEAD(&nn->nfsd4_manager.list);
7684 INIT_LIST_HEAD(&nn->client_lru);
7685 INIT_LIST_HEAD(&nn->close_lru);
7686 INIT_LIST_HEAD(&nn->del_recall_lru);
7687 spin_lock_init(&nn->client_lock);
7688 spin_lock_init(&nn->s2s_cp_lock);
7689 idr_init(&nn->s2s_cp_stateids);
7691 spin_lock_init(&nn->blocked_locks_lock);
7692 INIT_LIST_HEAD(&nn->blocked_locks_lru);
7694 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
7700 kfree(nn->unconf_id_hashtbl);
7702 kfree(nn->conf_id_hashtbl);
7708 nfs4_state_destroy_net(struct net *net)
7711 struct nfs4_client *clp = NULL;
7712 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7714 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7715 while (!list_empty(&nn->conf_id_hashtbl[i])) {
7716 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7717 destroy_client(clp);
7721 WARN_ON(!list_empty(&nn->blocked_locks_lru));
7723 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7724 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
7725 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7726 destroy_client(clp);
7730 kfree(nn->sessionid_hashtbl);
7731 kfree(nn->unconf_id_hashtbl);
7732 kfree(nn->conf_id_hashtbl);
7737 nfs4_state_start_net(struct net *net)
7739 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7742 ret = get_nfsdfs(net);
7745 ret = nfs4_state_create_net(net);
7747 mntput(nn->nfsd_mnt);
7750 locks_start_grace(net, &nn->nfsd4_manager);
7751 nfsd4_client_tracking_init(net);
7752 if (nn->track_reclaim_completes && nn->reclaim_str_hashtbl_size == 0)
7754 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %x)\n",
7755 nn->nfsd4_grace, net->ns.inum);
7756 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
7760 printk(KERN_INFO "NFSD: no clients to reclaim, skipping NFSv4 grace period (net %x)\n",
7762 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_lease * HZ);
7763 nfsd4_end_grace(nn);
7767 /* initialization to perform when the nfsd service is started: */
7770 nfs4_state_start(void)
7774 laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
7775 if (laundry_wq == NULL) {
7779 ret = nfsd4_create_callback_queue();
7781 goto out_free_laundry;
7783 set_max_delegations();
7787 destroy_workqueue(laundry_wq);
7793 nfs4_state_shutdown_net(struct net *net)
7795 struct nfs4_delegation *dp = NULL;
7796 struct list_head *pos, *next, reaplist;
7797 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7799 cancel_delayed_work_sync(&nn->laundromat_work);
7800 locks_end_grace(&nn->nfsd4_manager);
7802 INIT_LIST_HEAD(&reaplist);
7803 spin_lock(&state_lock);
7804 list_for_each_safe(pos, next, &nn->del_recall_lru) {
7805 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
7806 WARN_ON(!unhash_delegation_locked(dp));
7807 list_add(&dp->dl_recall_lru, &reaplist);
7809 spin_unlock(&state_lock);
7810 list_for_each_safe(pos, next, &reaplist) {
7811 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
7812 list_del_init(&dp->dl_recall_lru);
7813 destroy_unhashed_deleg(dp);
7816 nfsd4_client_tracking_exit(net);
7817 nfs4_state_destroy_net(net);
7818 mntput(nn->nfsd_mnt);
7822 nfs4_state_shutdown(void)
7824 destroy_workqueue(laundry_wq);
7825 nfsd4_destroy_callback_queue();
7829 get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7831 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
7832 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
7836 put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7838 if (cstate->minorversion) {
7839 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
7840 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
7845 clear_current_stateid(struct nfsd4_compound_state *cstate)
7847 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
7851 * functions to set current state id
7854 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate,
7855 union nfsd4_op_u *u)
7857 put_stateid(cstate, &u->open_downgrade.od_stateid);
7861 nfsd4_set_openstateid(struct nfsd4_compound_state *cstate,
7862 union nfsd4_op_u *u)
7864 put_stateid(cstate, &u->open.op_stateid);
7868 nfsd4_set_closestateid(struct nfsd4_compound_state *cstate,
7869 union nfsd4_op_u *u)
7871 put_stateid(cstate, &u->close.cl_stateid);
7875 nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate,
7876 union nfsd4_op_u *u)
7878 put_stateid(cstate, &u->lock.lk_resp_stateid);
7882 * functions to consume current state id
7886 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate,
7887 union nfsd4_op_u *u)
7889 get_stateid(cstate, &u->open_downgrade.od_stateid);
7893 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate,
7894 union nfsd4_op_u *u)
7896 get_stateid(cstate, &u->delegreturn.dr_stateid);
7900 nfsd4_get_freestateid(struct nfsd4_compound_state *cstate,
7901 union nfsd4_op_u *u)
7903 get_stateid(cstate, &u->free_stateid.fr_stateid);
7907 nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate,
7908 union nfsd4_op_u *u)
7910 get_stateid(cstate, &u->setattr.sa_stateid);
7914 nfsd4_get_closestateid(struct nfsd4_compound_state *cstate,
7915 union nfsd4_op_u *u)
7917 get_stateid(cstate, &u->close.cl_stateid);
7921 nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate,
7922 union nfsd4_op_u *u)
7924 get_stateid(cstate, &u->locku.lu_stateid);
7928 nfsd4_get_readstateid(struct nfsd4_compound_state *cstate,
7929 union nfsd4_op_u *u)
7931 get_stateid(cstate, &u->read.rd_stateid);
7935 nfsd4_get_writestateid(struct nfsd4_compound_state *cstate,
7936 union nfsd4_op_u *u)
7938 get_stateid(cstate, &u->write.wr_stateid);