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);
83 static void _free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps);
88 * Currently used for the del_recall_lru and file hash table. In an
89 * effort to decrease the scope of the client_mutex, this spinlock may
90 * eventually cover more:
92 static DEFINE_SPINLOCK(state_lock);
94 enum nfsd4_st_mutex_lock_subclass {
95 OPEN_STATEID_MUTEX = 0,
96 LOCK_STATEID_MUTEX = 1,
100 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
101 * the refcount on the open stateid to drop.
103 static DECLARE_WAIT_QUEUE_HEAD(close_wq);
106 * A waitqueue where a writer to clients/#/ctl destroying a client can
107 * wait for cl_rpc_users to drop to 0 and then for the client to be
110 static DECLARE_WAIT_QUEUE_HEAD(expiry_wq);
112 static struct kmem_cache *client_slab;
113 static struct kmem_cache *openowner_slab;
114 static struct kmem_cache *lockowner_slab;
115 static struct kmem_cache *file_slab;
116 static struct kmem_cache *stateid_slab;
117 static struct kmem_cache *deleg_slab;
118 static struct kmem_cache *odstate_slab;
120 static void free_session(struct nfsd4_session *);
122 static const struct nfsd4_callback_ops nfsd4_cb_recall_ops;
123 static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops;
125 static bool is_session_dead(struct nfsd4_session *ses)
127 return ses->se_flags & NFS4_SESSION_DEAD;
130 static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
132 if (atomic_read(&ses->se_ref) > ref_held_by_me)
133 return nfserr_jukebox;
134 ses->se_flags |= NFS4_SESSION_DEAD;
138 static bool is_client_expired(struct nfs4_client *clp)
140 return clp->cl_time == 0;
143 static __be32 get_client_locked(struct nfs4_client *clp)
145 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
147 lockdep_assert_held(&nn->client_lock);
149 if (is_client_expired(clp))
150 return nfserr_expired;
151 atomic_inc(&clp->cl_rpc_users);
155 /* must be called under the client_lock */
157 renew_client_locked(struct nfs4_client *clp)
159 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
161 if (is_client_expired(clp)) {
163 printk("%s: client (clientid %08x/%08x) already expired\n",
165 clp->cl_clientid.cl_boot,
166 clp->cl_clientid.cl_id);
170 dprintk("renewing client (clientid %08x/%08x)\n",
171 clp->cl_clientid.cl_boot,
172 clp->cl_clientid.cl_id);
173 list_move_tail(&clp->cl_lru, &nn->client_lru);
174 clp->cl_time = get_seconds();
177 static void put_client_renew_locked(struct nfs4_client *clp)
179 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
181 lockdep_assert_held(&nn->client_lock);
183 if (!atomic_dec_and_test(&clp->cl_rpc_users))
185 if (!is_client_expired(clp))
186 renew_client_locked(clp);
188 wake_up_all(&expiry_wq);
191 static void put_client_renew(struct nfs4_client *clp)
193 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
195 if (!atomic_dec_and_lock(&clp->cl_rpc_users, &nn->client_lock))
197 if (!is_client_expired(clp))
198 renew_client_locked(clp);
200 wake_up_all(&expiry_wq);
201 spin_unlock(&nn->client_lock);
204 static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
208 if (is_session_dead(ses))
209 return nfserr_badsession;
210 status = get_client_locked(ses->se_client);
213 atomic_inc(&ses->se_ref);
217 static void nfsd4_put_session_locked(struct nfsd4_session *ses)
219 struct nfs4_client *clp = ses->se_client;
220 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
222 lockdep_assert_held(&nn->client_lock);
224 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
226 put_client_renew_locked(clp);
229 static void nfsd4_put_session(struct nfsd4_session *ses)
231 struct nfs4_client *clp = ses->se_client;
232 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
234 spin_lock(&nn->client_lock);
235 nfsd4_put_session_locked(ses);
236 spin_unlock(&nn->client_lock);
239 static struct nfsd4_blocked_lock *
240 find_blocked_lock(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
243 struct nfsd4_blocked_lock *cur, *found = NULL;
245 spin_lock(&nn->blocked_locks_lock);
246 list_for_each_entry(cur, &lo->lo_blocked, nbl_list) {
247 if (fh_match(fh, &cur->nbl_fh)) {
248 list_del_init(&cur->nbl_list);
249 list_del_init(&cur->nbl_lru);
254 spin_unlock(&nn->blocked_locks_lock);
256 locks_delete_block(&found->nbl_lock);
260 static struct nfsd4_blocked_lock *
261 find_or_allocate_block(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
264 struct nfsd4_blocked_lock *nbl;
266 nbl = find_blocked_lock(lo, fh, nn);
268 nbl= kmalloc(sizeof(*nbl), GFP_KERNEL);
270 fh_copy_shallow(&nbl->nbl_fh, fh);
271 locks_init_lock(&nbl->nbl_lock);
272 nfsd4_init_cb(&nbl->nbl_cb, lo->lo_owner.so_client,
273 &nfsd4_cb_notify_lock_ops,
274 NFSPROC4_CLNT_CB_NOTIFY_LOCK);
281 free_blocked_lock(struct nfsd4_blocked_lock *nbl)
283 locks_delete_block(&nbl->nbl_lock);
284 locks_release_private(&nbl->nbl_lock);
289 remove_blocked_locks(struct nfs4_lockowner *lo)
291 struct nfs4_client *clp = lo->lo_owner.so_client;
292 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
293 struct nfsd4_blocked_lock *nbl;
296 /* Dequeue all blocked locks */
297 spin_lock(&nn->blocked_locks_lock);
298 while (!list_empty(&lo->lo_blocked)) {
299 nbl = list_first_entry(&lo->lo_blocked,
300 struct nfsd4_blocked_lock,
302 list_del_init(&nbl->nbl_list);
303 list_move(&nbl->nbl_lru, &reaplist);
305 spin_unlock(&nn->blocked_locks_lock);
308 while (!list_empty(&reaplist)) {
309 nbl = list_first_entry(&reaplist, struct nfsd4_blocked_lock,
311 list_del_init(&nbl->nbl_lru);
312 free_blocked_lock(nbl);
317 nfsd4_cb_notify_lock_prepare(struct nfsd4_callback *cb)
319 struct nfsd4_blocked_lock *nbl = container_of(cb,
320 struct nfsd4_blocked_lock, nbl_cb);
321 locks_delete_block(&nbl->nbl_lock);
325 nfsd4_cb_notify_lock_done(struct nfsd4_callback *cb, struct rpc_task *task)
328 * Since this is just an optimization, we don't try very hard if it
329 * turns out not to succeed. We'll requeue it on NFS4ERR_DELAY, and
330 * just quit trying on anything else.
332 switch (task->tk_status) {
334 rpc_delay(task, 1 * HZ);
342 nfsd4_cb_notify_lock_release(struct nfsd4_callback *cb)
344 struct nfsd4_blocked_lock *nbl = container_of(cb,
345 struct nfsd4_blocked_lock, nbl_cb);
347 free_blocked_lock(nbl);
350 static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops = {
351 .prepare = nfsd4_cb_notify_lock_prepare,
352 .done = nfsd4_cb_notify_lock_done,
353 .release = nfsd4_cb_notify_lock_release,
356 static inline struct nfs4_stateowner *
357 nfs4_get_stateowner(struct nfs4_stateowner *sop)
359 atomic_inc(&sop->so_count);
364 same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
366 return (sop->so_owner.len == owner->len) &&
367 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
370 static struct nfs4_openowner *
371 find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
372 struct nfs4_client *clp)
374 struct nfs4_stateowner *so;
376 lockdep_assert_held(&clp->cl_lock);
378 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
380 if (!so->so_is_open_owner)
382 if (same_owner_str(so, &open->op_owner))
383 return openowner(nfs4_get_stateowner(so));
388 static struct nfs4_openowner *
389 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
390 struct nfs4_client *clp)
392 struct nfs4_openowner *oo;
394 spin_lock(&clp->cl_lock);
395 oo = find_openstateowner_str_locked(hashval, open, clp);
396 spin_unlock(&clp->cl_lock);
401 opaque_hashval(const void *ptr, int nbytes)
403 unsigned char *cptr = (unsigned char *) ptr;
413 static void nfsd4_free_file_rcu(struct rcu_head *rcu)
415 struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
417 kmem_cache_free(file_slab, fp);
421 put_nfs4_file(struct nfs4_file *fi)
423 might_lock(&state_lock);
425 if (refcount_dec_and_lock(&fi->fi_ref, &state_lock)) {
426 hlist_del_rcu(&fi->fi_hash);
427 spin_unlock(&state_lock);
428 WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
429 WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
430 call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
434 static struct nfsd_file *
435 __nfs4_get_fd(struct nfs4_file *f, int oflag)
437 if (f->fi_fds[oflag])
438 return nfsd_file_get(f->fi_fds[oflag]);
442 static struct nfsd_file *
443 find_writeable_file_locked(struct nfs4_file *f)
445 struct nfsd_file *ret;
447 lockdep_assert_held(&f->fi_lock);
449 ret = __nfs4_get_fd(f, O_WRONLY);
451 ret = __nfs4_get_fd(f, O_RDWR);
455 static struct nfsd_file *
456 find_writeable_file(struct nfs4_file *f)
458 struct nfsd_file *ret;
460 spin_lock(&f->fi_lock);
461 ret = find_writeable_file_locked(f);
462 spin_unlock(&f->fi_lock);
467 static struct nfsd_file *
468 find_readable_file_locked(struct nfs4_file *f)
470 struct nfsd_file *ret;
472 lockdep_assert_held(&f->fi_lock);
474 ret = __nfs4_get_fd(f, O_RDONLY);
476 ret = __nfs4_get_fd(f, O_RDWR);
480 static struct nfsd_file *
481 find_readable_file(struct nfs4_file *f)
483 struct nfsd_file *ret;
485 spin_lock(&f->fi_lock);
486 ret = find_readable_file_locked(f);
487 spin_unlock(&f->fi_lock);
493 find_any_file(struct nfs4_file *f)
495 struct nfsd_file *ret;
497 spin_lock(&f->fi_lock);
498 ret = __nfs4_get_fd(f, O_RDWR);
500 ret = __nfs4_get_fd(f, O_WRONLY);
502 ret = __nfs4_get_fd(f, O_RDONLY);
504 spin_unlock(&f->fi_lock);
508 static atomic_long_t num_delegations;
509 unsigned long max_delegations;
512 * Open owner state (share locks)
515 /* hash tables for lock and open owners */
516 #define OWNER_HASH_BITS 8
517 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
518 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
520 static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
524 ret = opaque_hashval(ownername->data, ownername->len);
525 return ret & OWNER_HASH_MASK;
528 /* hash table for nfs4_file */
529 #define FILE_HASH_BITS 8
530 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
532 static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
534 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
537 static unsigned int file_hashval(struct knfsd_fh *fh)
539 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
542 static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
545 __nfs4_file_get_access(struct nfs4_file *fp, u32 access)
547 lockdep_assert_held(&fp->fi_lock);
549 if (access & NFS4_SHARE_ACCESS_WRITE)
550 atomic_inc(&fp->fi_access[O_WRONLY]);
551 if (access & NFS4_SHARE_ACCESS_READ)
552 atomic_inc(&fp->fi_access[O_RDONLY]);
556 nfs4_file_get_access(struct nfs4_file *fp, u32 access)
558 lockdep_assert_held(&fp->fi_lock);
560 /* Does this access mode make sense? */
561 if (access & ~NFS4_SHARE_ACCESS_BOTH)
564 /* Does it conflict with a deny mode already set? */
565 if ((access & fp->fi_share_deny) != 0)
566 return nfserr_share_denied;
568 __nfs4_file_get_access(fp, access);
572 static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
574 /* Common case is that there is no deny mode. */
576 /* Does this deny mode make sense? */
577 if (deny & ~NFS4_SHARE_DENY_BOTH)
580 if ((deny & NFS4_SHARE_DENY_READ) &&
581 atomic_read(&fp->fi_access[O_RDONLY]))
582 return nfserr_share_denied;
584 if ((deny & NFS4_SHARE_DENY_WRITE) &&
585 atomic_read(&fp->fi_access[O_WRONLY]))
586 return nfserr_share_denied;
591 static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
593 might_lock(&fp->fi_lock);
595 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
596 struct nfsd_file *f1 = NULL;
597 struct nfsd_file *f2 = NULL;
599 swap(f1, fp->fi_fds[oflag]);
600 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
601 swap(f2, fp->fi_fds[O_RDWR]);
602 spin_unlock(&fp->fi_lock);
610 static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
612 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
614 if (access & NFS4_SHARE_ACCESS_WRITE)
615 __nfs4_file_put_access(fp, O_WRONLY);
616 if (access & NFS4_SHARE_ACCESS_READ)
617 __nfs4_file_put_access(fp, O_RDONLY);
621 * Allocate a new open/delegation state counter. This is needed for
622 * pNFS for proper return on close semantics.
624 * Note that we only allocate it for pNFS-enabled exports, otherwise
625 * all pointers to struct nfs4_clnt_odstate are always NULL.
627 static struct nfs4_clnt_odstate *
628 alloc_clnt_odstate(struct nfs4_client *clp)
630 struct nfs4_clnt_odstate *co;
632 co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
635 refcount_set(&co->co_odcount, 1);
641 hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
643 struct nfs4_file *fp = co->co_file;
645 lockdep_assert_held(&fp->fi_lock);
646 list_add(&co->co_perfile, &fp->fi_clnt_odstate);
650 get_clnt_odstate(struct nfs4_clnt_odstate *co)
653 refcount_inc(&co->co_odcount);
657 put_clnt_odstate(struct nfs4_clnt_odstate *co)
659 struct nfs4_file *fp;
665 if (refcount_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
666 list_del(&co->co_perfile);
667 spin_unlock(&fp->fi_lock);
669 nfsd4_return_all_file_layouts(co->co_client, fp);
670 kmem_cache_free(odstate_slab, co);
674 static struct nfs4_clnt_odstate *
675 find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
677 struct nfs4_clnt_odstate *co;
678 struct nfs4_client *cl;
685 spin_lock(&fp->fi_lock);
686 list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
687 if (co->co_client == cl) {
688 get_clnt_odstate(co);
694 hash_clnt_odstate_locked(new);
696 spin_unlock(&fp->fi_lock);
700 struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab,
701 void (*sc_free)(struct nfs4_stid *))
703 struct nfs4_stid *stid;
706 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
710 idr_preload(GFP_KERNEL);
711 spin_lock(&cl->cl_lock);
712 /* Reserving 0 for start of file in nfsdfs "states" file: */
713 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 1, 0, GFP_NOWAIT);
714 spin_unlock(&cl->cl_lock);
719 stid->sc_free = sc_free;
720 stid->sc_client = cl;
721 stid->sc_stateid.si_opaque.so_id = new_id;
722 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
723 /* Will be incremented before return to client: */
724 refcount_set(&stid->sc_count, 1);
725 spin_lock_init(&stid->sc_lock);
726 INIT_LIST_HEAD(&stid->sc_cp_list);
729 * It shouldn't be a problem to reuse an opaque stateid value.
730 * I don't think it is for 4.1. But with 4.0 I worry that, for
731 * example, a stray write retransmission could be accepted by
732 * the server when it should have been rejected. Therefore,
733 * adopt a trick from the sctp code to attempt to maximize the
734 * amount of time until an id is reused, by ensuring they always
735 * "increase" (mod INT_MAX):
739 kmem_cache_free(slab, stid);
744 * Create a unique stateid_t to represent each COPY.
746 static int nfs4_init_cp_state(struct nfsd_net *nn, copy_stateid_t *stid,
747 unsigned char sc_type)
751 stid->stid.si_opaque.so_clid.cl_boot = nn->boot_time;
752 stid->stid.si_opaque.so_clid.cl_id = nn->s2s_cp_cl_id;
753 stid->sc_type = sc_type;
755 idr_preload(GFP_KERNEL);
756 spin_lock(&nn->s2s_cp_lock);
757 new_id = idr_alloc_cyclic(&nn->s2s_cp_stateids, stid, 0, 0, GFP_NOWAIT);
758 stid->stid.si_opaque.so_id = new_id;
759 spin_unlock(&nn->s2s_cp_lock);
766 int nfs4_init_copy_state(struct nfsd_net *nn, struct nfsd4_copy *copy)
768 return nfs4_init_cp_state(nn, ©->cp_stateid, NFS4_COPY_STID);
771 struct nfs4_cpntf_state *nfs4_alloc_init_cpntf_state(struct nfsd_net *nn,
772 struct nfs4_stid *p_stid)
774 struct nfs4_cpntf_state *cps;
776 cps = kzalloc(sizeof(struct nfs4_cpntf_state), GFP_KERNEL);
779 cps->cpntf_time = get_seconds();
780 refcount_set(&cps->cp_stateid.sc_count, 1);
781 if (!nfs4_init_cp_state(nn, &cps->cp_stateid, NFS4_COPYNOTIFY_STID))
783 spin_lock(&nn->s2s_cp_lock);
784 list_add(&cps->cp_list, &p_stid->sc_cp_list);
785 spin_unlock(&nn->s2s_cp_lock);
792 void nfs4_free_copy_state(struct nfsd4_copy *copy)
796 WARN_ON_ONCE(copy->cp_stateid.sc_type != NFS4_COPY_STID);
797 nn = net_generic(copy->cp_clp->net, nfsd_net_id);
798 spin_lock(&nn->s2s_cp_lock);
799 idr_remove(&nn->s2s_cp_stateids,
800 copy->cp_stateid.stid.si_opaque.so_id);
801 spin_unlock(&nn->s2s_cp_lock);
804 static void nfs4_free_cpntf_statelist(struct net *net, struct nfs4_stid *stid)
806 struct nfs4_cpntf_state *cps;
809 nn = net_generic(net, nfsd_net_id);
810 spin_lock(&nn->s2s_cp_lock);
811 while (!list_empty(&stid->sc_cp_list)) {
812 cps = list_first_entry(&stid->sc_cp_list,
813 struct nfs4_cpntf_state, cp_list);
814 _free_cpntf_state_locked(nn, cps);
816 spin_unlock(&nn->s2s_cp_lock);
819 static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
821 struct nfs4_stid *stid;
823 stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid);
827 return openlockstateid(stid);
830 static void nfs4_free_deleg(struct nfs4_stid *stid)
832 kmem_cache_free(deleg_slab, stid);
833 atomic_long_dec(&num_delegations);
837 * When we recall a delegation, we should be careful not to hand it
838 * out again straight away.
839 * To ensure this we keep a pair of bloom filters ('new' and 'old')
840 * in which the filehandles of recalled delegations are "stored".
841 * If a filehandle appear in either filter, a delegation is blocked.
842 * When a delegation is recalled, the filehandle is stored in the "new"
844 * Every 30 seconds we swap the filters and clear the "new" one,
845 * unless both are empty of course.
847 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
848 * low 3 bytes as hash-table indices.
850 * 'blocked_delegations_lock', which is always taken in block_delegations(),
851 * is used to manage concurrent access. Testing does not need the lock
852 * except when swapping the two filters.
854 static DEFINE_SPINLOCK(blocked_delegations_lock);
855 static struct bloom_pair {
856 int entries, old_entries;
858 int new; /* index into 'set' */
859 DECLARE_BITMAP(set[2], 256);
860 } blocked_delegations;
862 static int delegation_blocked(struct knfsd_fh *fh)
865 struct bloom_pair *bd = &blocked_delegations;
867 if (bd->entries == 0)
869 if (ktime_get_seconds() - bd->swap_time > 30) {
870 spin_lock(&blocked_delegations_lock);
871 if (ktime_get_seconds() - bd->swap_time > 30) {
872 bd->entries -= bd->old_entries;
873 bd->old_entries = bd->entries;
874 memset(bd->set[bd->new], 0,
877 bd->swap_time = ktime_get_seconds();
879 spin_unlock(&blocked_delegations_lock);
881 hash = jhash(&fh->fh_base, fh->fh_size, 0);
882 if (test_bit(hash&255, bd->set[0]) &&
883 test_bit((hash>>8)&255, bd->set[0]) &&
884 test_bit((hash>>16)&255, bd->set[0]))
887 if (test_bit(hash&255, bd->set[1]) &&
888 test_bit((hash>>8)&255, bd->set[1]) &&
889 test_bit((hash>>16)&255, bd->set[1]))
895 static void block_delegations(struct knfsd_fh *fh)
898 struct bloom_pair *bd = &blocked_delegations;
900 hash = jhash(&fh->fh_base, fh->fh_size, 0);
902 spin_lock(&blocked_delegations_lock);
903 __set_bit(hash&255, bd->set[bd->new]);
904 __set_bit((hash>>8)&255, bd->set[bd->new]);
905 __set_bit((hash>>16)&255, bd->set[bd->new]);
906 if (bd->entries == 0)
907 bd->swap_time = ktime_get_seconds();
909 spin_unlock(&blocked_delegations_lock);
912 static struct nfs4_delegation *
913 alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
914 struct svc_fh *current_fh,
915 struct nfs4_clnt_odstate *odstate)
917 struct nfs4_delegation *dp;
920 dprintk("NFSD alloc_init_deleg\n");
921 n = atomic_long_inc_return(&num_delegations);
922 if (n < 0 || n > max_delegations)
924 if (delegation_blocked(¤t_fh->fh_handle))
926 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg));
931 * delegation seqid's are never incremented. The 4.1 special
932 * meaning of seqid 0 isn't meaningful, really, but let's avoid
933 * 0 anyway just for consistency and use 1:
935 dp->dl_stid.sc_stateid.si_generation = 1;
936 INIT_LIST_HEAD(&dp->dl_perfile);
937 INIT_LIST_HEAD(&dp->dl_perclnt);
938 INIT_LIST_HEAD(&dp->dl_recall_lru);
939 dp->dl_clnt_odstate = odstate;
940 get_clnt_odstate(odstate);
941 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
943 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
944 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
946 dp->dl_stid.sc_file = fp;
949 atomic_long_dec(&num_delegations);
954 nfs4_put_stid(struct nfs4_stid *s)
956 struct nfs4_file *fp = s->sc_file;
957 struct nfs4_client *clp = s->sc_client;
959 might_lock(&clp->cl_lock);
961 if (!refcount_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
962 wake_up_all(&close_wq);
965 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
966 nfs4_free_cpntf_statelist(clp->net, s);
967 spin_unlock(&clp->cl_lock);
974 nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
976 stateid_t *src = &stid->sc_stateid;
978 spin_lock(&stid->sc_lock);
979 if (unlikely(++src->si_generation == 0))
980 src->si_generation = 1;
981 memcpy(dst, src, sizeof(*dst));
982 spin_unlock(&stid->sc_lock);
985 static void put_deleg_file(struct nfs4_file *fp)
987 struct nfsd_file *nf = NULL;
989 spin_lock(&fp->fi_lock);
990 if (--fp->fi_delegees == 0)
991 swap(nf, fp->fi_deleg_file);
992 spin_unlock(&fp->fi_lock);
998 static void nfs4_unlock_deleg_lease(struct nfs4_delegation *dp)
1000 struct nfs4_file *fp = dp->dl_stid.sc_file;
1001 struct nfsd_file *nf = fp->fi_deleg_file;
1003 WARN_ON_ONCE(!fp->fi_delegees);
1005 vfs_setlease(nf->nf_file, F_UNLCK, NULL, (void **)&dp);
1009 static void destroy_unhashed_deleg(struct nfs4_delegation *dp)
1011 put_clnt_odstate(dp->dl_clnt_odstate);
1012 nfs4_unlock_deleg_lease(dp);
1013 nfs4_put_stid(&dp->dl_stid);
1016 void nfs4_unhash_stid(struct nfs4_stid *s)
1022 * nfs4_delegation_exists - Discover if this delegation already exists
1023 * @clp: a pointer to the nfs4_client we're granting a delegation to
1024 * @fp: a pointer to the nfs4_file we're granting a delegation on
1027 * On success: true iff an existing delegation is found
1031 nfs4_delegation_exists(struct nfs4_client *clp, struct nfs4_file *fp)
1033 struct nfs4_delegation *searchdp = NULL;
1034 struct nfs4_client *searchclp = NULL;
1036 lockdep_assert_held(&state_lock);
1037 lockdep_assert_held(&fp->fi_lock);
1039 list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
1040 searchclp = searchdp->dl_stid.sc_client;
1041 if (clp == searchclp) {
1049 * hash_delegation_locked - Add a delegation to the appropriate lists
1050 * @dp: a pointer to the nfs4_delegation we are adding.
1051 * @fp: a pointer to the nfs4_file we're granting a delegation on
1054 * On success: NULL if the delegation was successfully hashed.
1056 * On error: -EAGAIN if one was previously granted to this
1057 * nfs4_client for this nfs4_file. Delegation is not hashed.
1062 hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
1064 struct nfs4_client *clp = dp->dl_stid.sc_client;
1066 lockdep_assert_held(&state_lock);
1067 lockdep_assert_held(&fp->fi_lock);
1069 if (nfs4_delegation_exists(clp, fp))
1071 refcount_inc(&dp->dl_stid.sc_count);
1072 dp->dl_stid.sc_type = NFS4_DELEG_STID;
1073 list_add(&dp->dl_perfile, &fp->fi_delegations);
1074 list_add(&dp->dl_perclnt, &clp->cl_delegations);
1079 unhash_delegation_locked(struct nfs4_delegation *dp)
1081 struct nfs4_file *fp = dp->dl_stid.sc_file;
1083 lockdep_assert_held(&state_lock);
1085 if (list_empty(&dp->dl_perfile))
1088 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
1089 /* Ensure that deleg break won't try to requeue it */
1091 spin_lock(&fp->fi_lock);
1092 list_del_init(&dp->dl_perclnt);
1093 list_del_init(&dp->dl_recall_lru);
1094 list_del_init(&dp->dl_perfile);
1095 spin_unlock(&fp->fi_lock);
1099 static void destroy_delegation(struct nfs4_delegation *dp)
1103 spin_lock(&state_lock);
1104 unhashed = unhash_delegation_locked(dp);
1105 spin_unlock(&state_lock);
1107 destroy_unhashed_deleg(dp);
1110 static void revoke_delegation(struct nfs4_delegation *dp)
1112 struct nfs4_client *clp = dp->dl_stid.sc_client;
1114 WARN_ON(!list_empty(&dp->dl_recall_lru));
1116 if (clp->cl_minorversion) {
1117 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
1118 refcount_inc(&dp->dl_stid.sc_count);
1119 spin_lock(&clp->cl_lock);
1120 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
1121 spin_unlock(&clp->cl_lock);
1123 destroy_unhashed_deleg(dp);
1130 static unsigned int clientid_hashval(u32 id)
1132 return id & CLIENT_HASH_MASK;
1135 static unsigned int clientstr_hashval(struct xdr_netobj name)
1137 return opaque_hashval(name.data, 8) & CLIENT_HASH_MASK;
1141 * We store the NONE, READ, WRITE, and BOTH bits separately in the
1142 * st_{access,deny}_bmap field of the stateid, in order to track not
1143 * only what share bits are currently in force, but also what
1144 * combinations of share bits previous opens have used. This allows us
1145 * to enforce the recommendation of rfc 3530 14.2.19 that the server
1146 * return an error if the client attempt to downgrade to a combination
1147 * of share bits not explicable by closing some of its previous opens.
1149 * XXX: This enforcement is actually incomplete, since we don't keep
1150 * track of access/deny bit combinations; so, e.g., we allow:
1152 * OPEN allow read, deny write
1153 * OPEN allow both, deny none
1154 * DOWNGRADE allow read, deny none
1156 * which we should reject.
1159 bmap_to_share_mode(unsigned long bmap) {
1161 unsigned int access = 0;
1163 for (i = 1; i < 4; i++) {
1164 if (test_bit(i, &bmap))
1170 /* set share access for a given stateid */
1172 set_access(u32 access, struct nfs4_ol_stateid *stp)
1174 unsigned char mask = 1 << access;
1176 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1177 stp->st_access_bmap |= mask;
1180 /* clear share access for a given stateid */
1182 clear_access(u32 access, struct nfs4_ol_stateid *stp)
1184 unsigned char mask = 1 << access;
1186 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1187 stp->st_access_bmap &= ~mask;
1190 /* test whether a given stateid has access */
1192 test_access(u32 access, struct nfs4_ol_stateid *stp)
1194 unsigned char mask = 1 << access;
1196 return (bool)(stp->st_access_bmap & mask);
1199 /* set share deny for a given stateid */
1201 set_deny(u32 deny, struct nfs4_ol_stateid *stp)
1203 unsigned char mask = 1 << deny;
1205 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1206 stp->st_deny_bmap |= mask;
1209 /* clear share deny for a given stateid */
1211 clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
1213 unsigned char mask = 1 << deny;
1215 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1216 stp->st_deny_bmap &= ~mask;
1219 /* test whether a given stateid is denying specific access */
1221 test_deny(u32 deny, struct nfs4_ol_stateid *stp)
1223 unsigned char mask = 1 << deny;
1225 return (bool)(stp->st_deny_bmap & mask);
1228 static int nfs4_access_to_omode(u32 access)
1230 switch (access & NFS4_SHARE_ACCESS_BOTH) {
1231 case NFS4_SHARE_ACCESS_READ:
1233 case NFS4_SHARE_ACCESS_WRITE:
1235 case NFS4_SHARE_ACCESS_BOTH:
1243 * A stateid that had a deny mode associated with it is being released
1244 * or downgraded. Recalculate the deny mode on the file.
1247 recalculate_deny_mode(struct nfs4_file *fp)
1249 struct nfs4_ol_stateid *stp;
1251 spin_lock(&fp->fi_lock);
1252 fp->fi_share_deny = 0;
1253 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
1254 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
1255 spin_unlock(&fp->fi_lock);
1259 reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
1262 bool change = false;
1264 for (i = 1; i < 4; i++) {
1265 if ((i & deny) != i) {
1271 /* Recalculate per-file deny mode if there was a change */
1273 recalculate_deny_mode(stp->st_stid.sc_file);
1276 /* release all access and file references for a given stateid */
1278 release_all_access(struct nfs4_ol_stateid *stp)
1281 struct nfs4_file *fp = stp->st_stid.sc_file;
1283 if (fp && stp->st_deny_bmap != 0)
1284 recalculate_deny_mode(fp);
1286 for (i = 1; i < 4; i++) {
1287 if (test_access(i, stp))
1288 nfs4_file_put_access(stp->st_stid.sc_file, i);
1289 clear_access(i, stp);
1293 static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1295 kfree(sop->so_owner.data);
1296 sop->so_ops->so_free(sop);
1299 static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1301 struct nfs4_client *clp = sop->so_client;
1303 might_lock(&clp->cl_lock);
1305 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
1307 sop->so_ops->so_unhash(sop);
1308 spin_unlock(&clp->cl_lock);
1309 nfs4_free_stateowner(sop);
1312 static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
1314 struct nfs4_file *fp = stp->st_stid.sc_file;
1316 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1318 if (list_empty(&stp->st_perfile))
1321 spin_lock(&fp->fi_lock);
1322 list_del_init(&stp->st_perfile);
1323 spin_unlock(&fp->fi_lock);
1324 list_del(&stp->st_perstateowner);
1328 static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
1330 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1332 put_clnt_odstate(stp->st_clnt_odstate);
1333 release_all_access(stp);
1334 if (stp->st_stateowner)
1335 nfs4_put_stateowner(stp->st_stateowner);
1336 kmem_cache_free(stateid_slab, stid);
1339 static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
1341 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1342 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
1343 struct nfsd_file *nf;
1345 nf = find_any_file(stp->st_stid.sc_file);
1347 get_file(nf->nf_file);
1348 filp_close(nf->nf_file, (fl_owner_t)lo);
1351 nfs4_free_ol_stateid(stid);
1355 * Put the persistent reference to an already unhashed generic stateid, while
1356 * holding the cl_lock. If it's the last reference, then put it onto the
1357 * reaplist for later destruction.
1359 static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1360 struct list_head *reaplist)
1362 struct nfs4_stid *s = &stp->st_stid;
1363 struct nfs4_client *clp = s->sc_client;
1365 lockdep_assert_held(&clp->cl_lock);
1367 WARN_ON_ONCE(!list_empty(&stp->st_locks));
1369 if (!refcount_dec_and_test(&s->sc_count)) {
1370 wake_up_all(&close_wq);
1374 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1375 list_add(&stp->st_locks, reaplist);
1378 static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
1380 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1382 list_del_init(&stp->st_locks);
1383 nfs4_unhash_stid(&stp->st_stid);
1384 return unhash_ol_stateid(stp);
1387 static void release_lock_stateid(struct nfs4_ol_stateid *stp)
1389 struct nfs4_client *clp = stp->st_stid.sc_client;
1392 spin_lock(&clp->cl_lock);
1393 unhashed = unhash_lock_stateid(stp);
1394 spin_unlock(&clp->cl_lock);
1396 nfs4_put_stid(&stp->st_stid);
1399 static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
1401 struct nfs4_client *clp = lo->lo_owner.so_client;
1403 lockdep_assert_held(&clp->cl_lock);
1405 list_del_init(&lo->lo_owner.so_strhash);
1409 * Free a list of generic stateids that were collected earlier after being
1413 free_ol_stateid_reaplist(struct list_head *reaplist)
1415 struct nfs4_ol_stateid *stp;
1416 struct nfs4_file *fp;
1420 while (!list_empty(reaplist)) {
1421 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1423 list_del(&stp->st_locks);
1424 fp = stp->st_stid.sc_file;
1425 stp->st_stid.sc_free(&stp->st_stid);
1431 static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1432 struct list_head *reaplist)
1434 struct nfs4_ol_stateid *stp;
1436 lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1438 while (!list_empty(&open_stp->st_locks)) {
1439 stp = list_entry(open_stp->st_locks.next,
1440 struct nfs4_ol_stateid, st_locks);
1441 WARN_ON(!unhash_lock_stateid(stp));
1442 put_ol_stateid_locked(stp, reaplist);
1446 static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
1447 struct list_head *reaplist)
1451 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1453 unhashed = unhash_ol_stateid(stp);
1454 release_open_stateid_locks(stp, reaplist);
1458 static void release_open_stateid(struct nfs4_ol_stateid *stp)
1460 LIST_HEAD(reaplist);
1462 spin_lock(&stp->st_stid.sc_client->cl_lock);
1463 if (unhash_open_stateid(stp, &reaplist))
1464 put_ol_stateid_locked(stp, &reaplist);
1465 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1466 free_ol_stateid_reaplist(&reaplist);
1469 static void unhash_openowner_locked(struct nfs4_openowner *oo)
1471 struct nfs4_client *clp = oo->oo_owner.so_client;
1473 lockdep_assert_held(&clp->cl_lock);
1475 list_del_init(&oo->oo_owner.so_strhash);
1476 list_del_init(&oo->oo_perclient);
1479 static void release_last_closed_stateid(struct nfs4_openowner *oo)
1481 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1483 struct nfs4_ol_stateid *s;
1485 spin_lock(&nn->client_lock);
1486 s = oo->oo_last_closed_stid;
1488 list_del_init(&oo->oo_close_lru);
1489 oo->oo_last_closed_stid = NULL;
1491 spin_unlock(&nn->client_lock);
1493 nfs4_put_stid(&s->st_stid);
1496 static void release_openowner(struct nfs4_openowner *oo)
1498 struct nfs4_ol_stateid *stp;
1499 struct nfs4_client *clp = oo->oo_owner.so_client;
1500 struct list_head reaplist;
1502 INIT_LIST_HEAD(&reaplist);
1504 spin_lock(&clp->cl_lock);
1505 unhash_openowner_locked(oo);
1506 while (!list_empty(&oo->oo_owner.so_stateids)) {
1507 stp = list_first_entry(&oo->oo_owner.so_stateids,
1508 struct nfs4_ol_stateid, st_perstateowner);
1509 if (unhash_open_stateid(stp, &reaplist))
1510 put_ol_stateid_locked(stp, &reaplist);
1512 spin_unlock(&clp->cl_lock);
1513 free_ol_stateid_reaplist(&reaplist);
1514 release_last_closed_stateid(oo);
1515 nfs4_put_stateowner(&oo->oo_owner);
1519 hash_sessionid(struct nfs4_sessionid *sessionid)
1521 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1523 return sid->sequence % SESSION_HASH_SIZE;
1526 #ifdef CONFIG_SUNRPC_DEBUG
1528 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1530 u32 *ptr = (u32 *)(&sessionid->data[0]);
1531 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1535 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1541 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1542 * won't be used for replay.
1544 void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1546 struct nfs4_stateowner *so = cstate->replay_owner;
1548 if (nfserr == nfserr_replay_me)
1551 if (!seqid_mutating_err(ntohl(nfserr))) {
1552 nfsd4_cstate_clear_replay(cstate);
1557 if (so->so_is_open_owner)
1558 release_last_closed_stateid(openowner(so));
1564 gen_sessionid(struct nfsd4_session *ses)
1566 struct nfs4_client *clp = ses->se_client;
1567 struct nfsd4_sessionid *sid;
1569 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1570 sid->clientid = clp->cl_clientid;
1571 sid->sequence = current_sessionid++;
1576 * The protocol defines ca_maxresponssize_cached to include the size of
1577 * the rpc header, but all we need to cache is the data starting after
1578 * the end of the initial SEQUENCE operation--the rest we regenerate
1579 * each time. Therefore we can advertise a ca_maxresponssize_cached
1580 * value that is the number of bytes in our cache plus a few additional
1581 * bytes. In order to stay on the safe side, and not promise more than
1582 * we can cache, those additional bytes must be the minimum possible: 24
1583 * bytes of rpc header (xid through accept state, with AUTH_NULL
1584 * verifier), 12 for the compound header (with zero-length tag), and 44
1585 * for the SEQUENCE op response:
1587 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1590 free_session_slots(struct nfsd4_session *ses)
1594 for (i = 0; i < ses->se_fchannel.maxreqs; i++) {
1595 free_svc_cred(&ses->se_slots[i]->sl_cred);
1596 kfree(ses->se_slots[i]);
1601 * We don't actually need to cache the rpc and session headers, so we
1602 * can allocate a little less for each slot:
1604 static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
1608 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1611 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1612 return size + sizeof(struct nfsd4_slot);
1616 * XXX: If we run out of reserved DRC memory we could (up to a point)
1617 * re-negotiate active sessions and reduce their slot usage to make
1618 * room for new connections. For now we just fail the create session.
1620 static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
1622 u32 slotsize = slot_bytes(ca);
1623 u32 num = ca->maxreqs;
1624 unsigned long avail, total_avail;
1625 unsigned int scale_factor;
1627 spin_lock(&nfsd_drc_lock);
1628 if (nfsd_drc_max_mem > nfsd_drc_mem_used)
1629 total_avail = nfsd_drc_max_mem - nfsd_drc_mem_used;
1631 /* We have handed out more space than we chose in
1632 * set_max_drc() to allow. That isn't really a
1633 * problem as long as that doesn't make us think we
1634 * have lots more due to integer overflow.
1637 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, total_avail);
1639 * Never use more than a fraction of the remaining memory,
1640 * unless it's the only way to give this client a slot.
1641 * The chosen fraction is either 1/8 or 1/number of threads,
1642 * whichever is smaller. This ensures there are adequate
1643 * slots to support multiple clients per thread.
1644 * Give the client one slot even if that would require
1645 * over-allocation--it is better than failure.
1647 scale_factor = max_t(unsigned int, 8, nn->nfsd_serv->sv_nrthreads);
1649 avail = clamp_t(unsigned long, avail, slotsize,
1650 total_avail/scale_factor);
1651 num = min_t(int, num, avail / slotsize);
1652 num = max_t(int, num, 1);
1653 nfsd_drc_mem_used += num * slotsize;
1654 spin_unlock(&nfsd_drc_lock);
1659 static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
1661 int slotsize = slot_bytes(ca);
1663 spin_lock(&nfsd_drc_lock);
1664 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
1665 spin_unlock(&nfsd_drc_lock);
1668 static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1669 struct nfsd4_channel_attrs *battrs)
1671 int numslots = fattrs->maxreqs;
1672 int slotsize = slot_bytes(fattrs);
1673 struct nfsd4_session *new;
1676 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
1677 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1678 mem = numslots * sizeof(struct nfsd4_slot *);
1680 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
1683 /* allocate each struct nfsd4_slot and data cache in one piece */
1684 for (i = 0; i < numslots; i++) {
1685 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
1686 if (!new->se_slots[i])
1690 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1691 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1696 kfree(new->se_slots[i]);
1701 static void free_conn(struct nfsd4_conn *c)
1703 svc_xprt_put(c->cn_xprt);
1707 static void nfsd4_conn_lost(struct svc_xpt_user *u)
1709 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1710 struct nfs4_client *clp = c->cn_session->se_client;
1712 spin_lock(&clp->cl_lock);
1713 if (!list_empty(&c->cn_persession)) {
1714 list_del(&c->cn_persession);
1717 nfsd4_probe_callback(clp);
1718 spin_unlock(&clp->cl_lock);
1721 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
1723 struct nfsd4_conn *conn;
1725 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1728 svc_xprt_get(rqstp->rq_xprt);
1729 conn->cn_xprt = rqstp->rq_xprt;
1730 conn->cn_flags = flags;
1731 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1735 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1737 conn->cn_session = ses;
1738 list_add(&conn->cn_persession, &ses->se_conns);
1741 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1743 struct nfs4_client *clp = ses->se_client;
1745 spin_lock(&clp->cl_lock);
1746 __nfsd4_hash_conn(conn, ses);
1747 spin_unlock(&clp->cl_lock);
1750 static int nfsd4_register_conn(struct nfsd4_conn *conn)
1752 conn->cn_xpt_user.callback = nfsd4_conn_lost;
1753 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
1756 static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
1760 nfsd4_hash_conn(conn, ses);
1761 ret = nfsd4_register_conn(conn);
1763 /* oops; xprt is already down: */
1764 nfsd4_conn_lost(&conn->cn_xpt_user);
1765 /* We may have gained or lost a callback channel: */
1766 nfsd4_probe_callback_sync(ses->se_client);
1769 static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
1771 u32 dir = NFS4_CDFC4_FORE;
1773 if (cses->flags & SESSION4_BACK_CHAN)
1774 dir |= NFS4_CDFC4_BACK;
1775 return alloc_conn(rqstp, dir);
1778 /* must be called under client_lock */
1779 static void nfsd4_del_conns(struct nfsd4_session *s)
1781 struct nfs4_client *clp = s->se_client;
1782 struct nfsd4_conn *c;
1784 spin_lock(&clp->cl_lock);
1785 while (!list_empty(&s->se_conns)) {
1786 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1787 list_del_init(&c->cn_persession);
1788 spin_unlock(&clp->cl_lock);
1790 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1793 spin_lock(&clp->cl_lock);
1795 spin_unlock(&clp->cl_lock);
1798 static void __free_session(struct nfsd4_session *ses)
1800 free_session_slots(ses);
1804 static void free_session(struct nfsd4_session *ses)
1806 nfsd4_del_conns(ses);
1807 nfsd4_put_drc_mem(&ses->se_fchannel);
1808 __free_session(ses);
1811 static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
1814 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1816 new->se_client = clp;
1819 INIT_LIST_HEAD(&new->se_conns);
1821 new->se_cb_seq_nr = 1;
1822 new->se_flags = cses->flags;
1823 new->se_cb_prog = cses->callback_prog;
1824 new->se_cb_sec = cses->cb_sec;
1825 atomic_set(&new->se_ref, 0);
1826 idx = hash_sessionid(&new->se_sessionid);
1827 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
1828 spin_lock(&clp->cl_lock);
1829 list_add(&new->se_perclnt, &clp->cl_sessions);
1830 spin_unlock(&clp->cl_lock);
1833 struct sockaddr *sa = svc_addr(rqstp);
1835 * This is a little silly; with sessions there's no real
1836 * use for the callback address. Use the peer address
1837 * as a reasonable default for now, but consider fixing
1838 * the rpc client not to require an address in the
1841 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1842 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
1846 /* caller must hold client_lock */
1847 static struct nfsd4_session *
1848 __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
1850 struct nfsd4_session *elem;
1852 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1854 lockdep_assert_held(&nn->client_lock);
1856 dump_sessionid(__func__, sessionid);
1857 idx = hash_sessionid(sessionid);
1858 /* Search in the appropriate list */
1859 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
1860 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1861 NFS4_MAX_SESSIONID_LEN)) {
1866 dprintk("%s: session not found\n", __func__);
1870 static struct nfsd4_session *
1871 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1874 struct nfsd4_session *session;
1875 __be32 status = nfserr_badsession;
1877 session = __find_in_sessionid_hashtbl(sessionid, net);
1880 status = nfsd4_get_session_locked(session);
1888 /* caller must hold client_lock */
1890 unhash_session(struct nfsd4_session *ses)
1892 struct nfs4_client *clp = ses->se_client;
1893 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1895 lockdep_assert_held(&nn->client_lock);
1897 list_del(&ses->se_hash);
1898 spin_lock(&ses->se_client->cl_lock);
1899 list_del(&ses->se_perclnt);
1900 spin_unlock(&ses->se_client->cl_lock);
1903 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1905 STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
1908 * We're assuming the clid was not given out from a boot
1909 * precisely 2^32 (about 136 years) before this one. That seems
1910 * a safe assumption:
1912 if (clid->cl_boot == (u32)nn->boot_time)
1914 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
1915 clid->cl_boot, clid->cl_id, nn->boot_time);
1920 * XXX Should we use a slab cache ?
1921 * This type of memory management is somewhat inefficient, but we use it
1922 * anyway since SETCLIENTID is not a common operation.
1924 static struct nfs4_client *alloc_client(struct xdr_netobj name)
1926 struct nfs4_client *clp;
1929 clp = kmem_cache_zalloc(client_slab, GFP_KERNEL);
1932 xdr_netobj_dup(&clp->cl_name, &name, GFP_KERNEL);
1933 if (clp->cl_name.data == NULL)
1935 clp->cl_ownerstr_hashtbl = kmalloc_array(OWNER_HASH_SIZE,
1936 sizeof(struct list_head),
1938 if (!clp->cl_ownerstr_hashtbl)
1939 goto err_no_hashtbl;
1940 for (i = 0; i < OWNER_HASH_SIZE; i++)
1941 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
1942 INIT_LIST_HEAD(&clp->cl_sessions);
1943 idr_init(&clp->cl_stateids);
1944 atomic_set(&clp->cl_rpc_users, 0);
1945 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1946 INIT_LIST_HEAD(&clp->cl_idhash);
1947 INIT_LIST_HEAD(&clp->cl_openowners);
1948 INIT_LIST_HEAD(&clp->cl_delegations);
1949 INIT_LIST_HEAD(&clp->cl_lru);
1950 INIT_LIST_HEAD(&clp->cl_revoked);
1951 #ifdef CONFIG_NFSD_PNFS
1952 INIT_LIST_HEAD(&clp->cl_lo_states);
1954 INIT_LIST_HEAD(&clp->async_copies);
1955 spin_lock_init(&clp->async_lock);
1956 spin_lock_init(&clp->cl_lock);
1957 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1960 kfree(clp->cl_name.data);
1962 kmem_cache_free(client_slab, clp);
1966 static void __free_client(struct kref *k)
1968 struct nfsdfs_client *c = container_of(k, struct nfsdfs_client, cl_ref);
1969 struct nfs4_client *clp = container_of(c, struct nfs4_client, cl_nfsdfs);
1971 free_svc_cred(&clp->cl_cred);
1972 kfree(clp->cl_ownerstr_hashtbl);
1973 kfree(clp->cl_name.data);
1974 kfree(clp->cl_nii_domain.data);
1975 kfree(clp->cl_nii_name.data);
1976 idr_destroy(&clp->cl_stateids);
1977 kmem_cache_free(client_slab, clp);
1980 static void drop_client(struct nfs4_client *clp)
1982 kref_put(&clp->cl_nfsdfs.cl_ref, __free_client);
1986 free_client(struct nfs4_client *clp)
1988 while (!list_empty(&clp->cl_sessions)) {
1989 struct nfsd4_session *ses;
1990 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1992 list_del(&ses->se_perclnt);
1993 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1996 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
1997 if (clp->cl_nfsd_dentry) {
1998 nfsd_client_rmdir(clp->cl_nfsd_dentry);
1999 clp->cl_nfsd_dentry = NULL;
2000 wake_up_all(&expiry_wq);
2005 /* must be called under the client_lock */
2007 unhash_client_locked(struct nfs4_client *clp)
2009 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2010 struct nfsd4_session *ses;
2012 lockdep_assert_held(&nn->client_lock);
2014 /* Mark the client as expired! */
2016 /* Make it invisible */
2017 if (!list_empty(&clp->cl_idhash)) {
2018 list_del_init(&clp->cl_idhash);
2019 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
2020 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
2022 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
2024 list_del_init(&clp->cl_lru);
2025 spin_lock(&clp->cl_lock);
2026 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
2027 list_del_init(&ses->se_hash);
2028 spin_unlock(&clp->cl_lock);
2032 unhash_client(struct nfs4_client *clp)
2034 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2036 spin_lock(&nn->client_lock);
2037 unhash_client_locked(clp);
2038 spin_unlock(&nn->client_lock);
2041 static __be32 mark_client_expired_locked(struct nfs4_client *clp)
2043 if (atomic_read(&clp->cl_rpc_users))
2044 return nfserr_jukebox;
2045 unhash_client_locked(clp);
2050 __destroy_client(struct nfs4_client *clp)
2053 struct nfs4_openowner *oo;
2054 struct nfs4_delegation *dp;
2055 struct list_head reaplist;
2057 INIT_LIST_HEAD(&reaplist);
2058 spin_lock(&state_lock);
2059 while (!list_empty(&clp->cl_delegations)) {
2060 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
2061 WARN_ON(!unhash_delegation_locked(dp));
2062 list_add(&dp->dl_recall_lru, &reaplist);
2064 spin_unlock(&state_lock);
2065 while (!list_empty(&reaplist)) {
2066 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
2067 list_del_init(&dp->dl_recall_lru);
2068 destroy_unhashed_deleg(dp);
2070 while (!list_empty(&clp->cl_revoked)) {
2071 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
2072 list_del_init(&dp->dl_recall_lru);
2073 nfs4_put_stid(&dp->dl_stid);
2075 while (!list_empty(&clp->cl_openowners)) {
2076 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
2077 nfs4_get_stateowner(&oo->oo_owner);
2078 release_openowner(oo);
2080 for (i = 0; i < OWNER_HASH_SIZE; i++) {
2081 struct nfs4_stateowner *so, *tmp;
2083 list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i],
2085 /* Should be no openowners at this point */
2086 WARN_ON_ONCE(so->so_is_open_owner);
2087 remove_blocked_locks(lockowner(so));
2090 nfsd4_return_all_client_layouts(clp);
2091 nfsd4_shutdown_copy(clp);
2092 nfsd4_shutdown_callback(clp);
2093 if (clp->cl_cb_conn.cb_xprt)
2094 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
2096 wake_up_all(&expiry_wq);
2100 destroy_client(struct nfs4_client *clp)
2103 __destroy_client(clp);
2106 static void inc_reclaim_complete(struct nfs4_client *clp)
2108 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2110 if (!nn->track_reclaim_completes)
2112 if (!nfsd4_find_reclaim_client(clp->cl_name, nn))
2114 if (atomic_inc_return(&nn->nr_reclaim_complete) ==
2115 nn->reclaim_str_hashtbl_size) {
2116 printk(KERN_INFO "NFSD: all clients done reclaiming, ending NFSv4 grace period (net %x)\n",
2118 nfsd4_end_grace(nn);
2122 static void expire_client(struct nfs4_client *clp)
2125 nfsd4_client_record_remove(clp);
2126 __destroy_client(clp);
2129 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
2131 memcpy(target->cl_verifier.data, source->data,
2132 sizeof(target->cl_verifier.data));
2135 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
2137 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
2138 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
2141 static int copy_cred(struct svc_cred *target, struct svc_cred *source)
2143 target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL);
2144 target->cr_raw_principal = kstrdup(source->cr_raw_principal,
2146 target->cr_targ_princ = kstrdup(source->cr_targ_princ, GFP_KERNEL);
2147 if ((source->cr_principal && !target->cr_principal) ||
2148 (source->cr_raw_principal && !target->cr_raw_principal) ||
2149 (source->cr_targ_princ && !target->cr_targ_princ))
2152 target->cr_flavor = source->cr_flavor;
2153 target->cr_uid = source->cr_uid;
2154 target->cr_gid = source->cr_gid;
2155 target->cr_group_info = source->cr_group_info;
2156 get_group_info(target->cr_group_info);
2157 target->cr_gss_mech = source->cr_gss_mech;
2158 if (source->cr_gss_mech)
2159 gss_mech_get(source->cr_gss_mech);
2164 compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
2166 if (o1->len < o2->len)
2168 if (o1->len > o2->len)
2170 return memcmp(o1->data, o2->data, o1->len);
2174 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
2176 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
2180 same_clid(clientid_t *cl1, clientid_t *cl2)
2182 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
2185 static bool groups_equal(struct group_info *g1, struct group_info *g2)
2189 if (g1->ngroups != g2->ngroups)
2191 for (i=0; i<g1->ngroups; i++)
2192 if (!gid_eq(g1->gid[i], g2->gid[i]))
2198 * RFC 3530 language requires clid_inuse be returned when the
2199 * "principal" associated with a requests differs from that previously
2200 * used. We use uid, gid's, and gss principal string as our best
2201 * approximation. We also don't want to allow non-gss use of a client
2202 * established using gss: in theory cr_principal should catch that
2203 * change, but in practice cr_principal can be null even in the gss case
2204 * since gssd doesn't always pass down a principal string.
2206 static bool is_gss_cred(struct svc_cred *cr)
2208 /* Is cr_flavor one of the gss "pseudoflavors"?: */
2209 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
2214 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
2216 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
2217 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
2218 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
2219 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
2221 /* XXX: check that cr_targ_princ fields match ? */
2222 if (cr1->cr_principal == cr2->cr_principal)
2224 if (!cr1->cr_principal || !cr2->cr_principal)
2226 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
2229 static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
2231 struct svc_cred *cr = &rqstp->rq_cred;
2234 if (!cr->cr_gss_mech)
2236 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
2237 return service == RPC_GSS_SVC_INTEGRITY ||
2238 service == RPC_GSS_SVC_PRIVACY;
2241 bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
2243 struct svc_cred *cr = &rqstp->rq_cred;
2245 if (!cl->cl_mach_cred)
2247 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
2249 if (!svc_rqst_integrity_protected(rqstp))
2251 if (cl->cl_cred.cr_raw_principal)
2252 return 0 == strcmp(cl->cl_cred.cr_raw_principal,
2253 cr->cr_raw_principal);
2254 if (!cr->cr_principal)
2256 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
2259 static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
2264 * This is opaque to client, so no need to byte-swap. Use
2265 * __force to keep sparse happy
2267 verf[0] = (__force __be32)get_seconds();
2268 verf[1] = (__force __be32)nn->clverifier_counter++;
2269 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
2272 static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
2274 clp->cl_clientid.cl_boot = nn->boot_time;
2275 clp->cl_clientid.cl_id = nn->clientid_counter++;
2276 gen_confirm(clp, nn);
2279 static struct nfs4_stid *
2280 find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
2282 struct nfs4_stid *ret;
2284 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
2285 if (!ret || !ret->sc_type)
2290 static struct nfs4_stid *
2291 find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
2293 struct nfs4_stid *s;
2295 spin_lock(&cl->cl_lock);
2296 s = find_stateid_locked(cl, t);
2298 if (typemask & s->sc_type)
2299 refcount_inc(&s->sc_count);
2303 spin_unlock(&cl->cl_lock);
2307 static struct nfs4_client *get_nfsdfs_clp(struct inode *inode)
2309 struct nfsdfs_client *nc;
2310 nc = get_nfsdfs_client(inode);
2313 return container_of(nc, struct nfs4_client, cl_nfsdfs);
2316 static void seq_quote_mem(struct seq_file *m, char *data, int len)
2318 seq_printf(m, "\"");
2319 seq_escape_mem_ascii(m, data, len);
2320 seq_printf(m, "\"");
2323 static int client_info_show(struct seq_file *m, void *v)
2325 struct inode *inode = m->private;
2326 struct nfs4_client *clp;
2329 clp = get_nfsdfs_clp(inode);
2332 memcpy(&clid, &clp->cl_clientid, sizeof(clid));
2333 seq_printf(m, "clientid: 0x%llx\n", clid);
2334 seq_printf(m, "address: \"%pISpc\"\n", (struct sockaddr *)&clp->cl_addr);
2335 seq_printf(m, "name: ");
2336 seq_quote_mem(m, clp->cl_name.data, clp->cl_name.len);
2337 seq_printf(m, "\nminor version: %d\n", clp->cl_minorversion);
2338 if (clp->cl_nii_domain.data) {
2339 seq_printf(m, "Implementation domain: ");
2340 seq_quote_mem(m, clp->cl_nii_domain.data,
2341 clp->cl_nii_domain.len);
2342 seq_printf(m, "\nImplementation name: ");
2343 seq_quote_mem(m, clp->cl_nii_name.data, clp->cl_nii_name.len);
2344 seq_printf(m, "\nImplementation time: [%ld, %ld]\n",
2345 clp->cl_nii_time.tv_sec, clp->cl_nii_time.tv_nsec);
2352 static int client_info_open(struct inode *inode, struct file *file)
2354 return single_open(file, client_info_show, inode);
2357 static const struct file_operations client_info_fops = {
2358 .open = client_info_open,
2360 .llseek = seq_lseek,
2361 .release = single_release,
2364 static void *states_start(struct seq_file *s, loff_t *pos)
2365 __acquires(&clp->cl_lock)
2367 struct nfs4_client *clp = s->private;
2368 unsigned long id = *pos;
2371 spin_lock(&clp->cl_lock);
2372 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2377 static void *states_next(struct seq_file *s, void *v, loff_t *pos)
2379 struct nfs4_client *clp = s->private;
2380 unsigned long id = *pos;
2385 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2390 static void states_stop(struct seq_file *s, void *v)
2391 __releases(&clp->cl_lock)
2393 struct nfs4_client *clp = s->private;
2395 spin_unlock(&clp->cl_lock);
2398 static void nfs4_show_superblock(struct seq_file *s, struct nfsd_file *f)
2400 struct inode *inode = f->nf_inode;
2402 seq_printf(s, "superblock: \"%02x:%02x:%ld\"",
2403 MAJOR(inode->i_sb->s_dev),
2404 MINOR(inode->i_sb->s_dev),
2408 static void nfs4_show_owner(struct seq_file *s, struct nfs4_stateowner *oo)
2410 seq_printf(s, "owner: ");
2411 seq_quote_mem(s, oo->so_owner.data, oo->so_owner.len);
2414 static int nfs4_show_open(struct seq_file *s, struct nfs4_stid *st)
2416 struct nfs4_ol_stateid *ols;
2417 struct nfs4_file *nf;
2418 struct nfsd_file *file;
2419 struct nfs4_stateowner *oo;
2420 unsigned int access, deny;
2422 if (st->sc_type != NFS4_OPEN_STID && st->sc_type != NFS4_LOCK_STID)
2423 return 0; /* XXX: or SEQ_SKIP? */
2424 ols = openlockstateid(st);
2425 oo = ols->st_stateowner;
2427 file = find_any_file(nf);
2429 seq_printf(s, "- 0x%16phN: { type: open, ", &st->sc_stateid);
2431 access = bmap_to_share_mode(ols->st_access_bmap);
2432 deny = bmap_to_share_mode(ols->st_deny_bmap);
2434 seq_printf(s, "access: %s%s, ",
2435 access & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2436 access & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2437 seq_printf(s, "deny: %s%s, ",
2438 deny & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2439 deny & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2441 nfs4_show_superblock(s, file);
2442 seq_printf(s, ", ");
2443 nfs4_show_owner(s, oo);
2444 seq_printf(s, " }\n");
2445 nfsd_file_put(file);
2450 static int nfs4_show_lock(struct seq_file *s, struct nfs4_stid *st)
2452 struct nfs4_ol_stateid *ols;
2453 struct nfs4_file *nf;
2454 struct nfsd_file *file;
2455 struct nfs4_stateowner *oo;
2457 ols = openlockstateid(st);
2458 oo = ols->st_stateowner;
2460 file = find_any_file(nf);
2462 seq_printf(s, "- 0x%16phN: { type: lock, ", &st->sc_stateid);
2465 * Note: a lock stateid isn't really the same thing as a lock,
2466 * it's the locking state held by one owner on a file, and there
2467 * may be multiple (or no) lock ranges associated with it.
2468 * (Same for the matter is true of open stateids.)
2471 nfs4_show_superblock(s, file);
2472 /* XXX: open stateid? */
2473 seq_printf(s, ", ");
2474 nfs4_show_owner(s, oo);
2475 seq_printf(s, " }\n");
2476 nfsd_file_put(file);
2481 static int nfs4_show_deleg(struct seq_file *s, struct nfs4_stid *st)
2483 struct nfs4_delegation *ds;
2484 struct nfs4_file *nf;
2485 struct nfsd_file *file;
2487 ds = delegstateid(st);
2489 file = nf->fi_deleg_file;
2491 seq_printf(s, "- 0x%16phN: { type: deleg, ", &st->sc_stateid);
2493 /* Kinda dead code as long as we only support read delegs: */
2494 seq_printf(s, "access: %s, ",
2495 ds->dl_type == NFS4_OPEN_DELEGATE_READ ? "r" : "w");
2497 /* XXX: lease time, whether it's being recalled. */
2499 nfs4_show_superblock(s, file);
2500 seq_printf(s, " }\n");
2505 static int nfs4_show_layout(struct seq_file *s, struct nfs4_stid *st)
2507 struct nfs4_layout_stateid *ls;
2508 struct nfsd_file *file;
2510 ls = container_of(st, struct nfs4_layout_stateid, ls_stid);
2513 seq_printf(s, "- 0x%16phN: { type: layout, ", &st->sc_stateid);
2515 /* XXX: What else would be useful? */
2517 nfs4_show_superblock(s, file);
2518 seq_printf(s, " }\n");
2523 static int states_show(struct seq_file *s, void *v)
2525 struct nfs4_stid *st = v;
2527 switch (st->sc_type) {
2528 case NFS4_OPEN_STID:
2529 return nfs4_show_open(s, st);
2530 case NFS4_LOCK_STID:
2531 return nfs4_show_lock(s, st);
2532 case NFS4_DELEG_STID:
2533 return nfs4_show_deleg(s, st);
2534 case NFS4_LAYOUT_STID:
2535 return nfs4_show_layout(s, st);
2537 return 0; /* XXX: or SEQ_SKIP? */
2539 /* XXX: copy stateids? */
2542 static struct seq_operations states_seq_ops = {
2543 .start = states_start,
2544 .next = states_next,
2545 .stop = states_stop,
2549 static int client_states_open(struct inode *inode, struct file *file)
2552 struct nfs4_client *clp;
2555 clp = get_nfsdfs_clp(inode);
2559 ret = seq_open(file, &states_seq_ops);
2562 s = file->private_data;
2567 static int client_opens_release(struct inode *inode, struct file *file)
2569 struct seq_file *m = file->private_data;
2570 struct nfs4_client *clp = m->private;
2572 /* XXX: alternatively, we could get/drop in seq start/stop */
2577 static const struct file_operations client_states_fops = {
2578 .open = client_states_open,
2580 .llseek = seq_lseek,
2581 .release = client_opens_release,
2585 * Normally we refuse to destroy clients that are in use, but here the
2586 * administrator is telling us to just do it. We also want to wait
2587 * so the caller has a guarantee that the client's locks are gone by
2588 * the time the write returns:
2590 static void force_expire_client(struct nfs4_client *clp)
2592 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2593 bool already_expired;
2595 spin_lock(&clp->cl_lock);
2597 spin_unlock(&clp->cl_lock);
2599 wait_event(expiry_wq, atomic_read(&clp->cl_rpc_users) == 0);
2600 spin_lock(&nn->client_lock);
2601 already_expired = list_empty(&clp->cl_lru);
2602 if (!already_expired)
2603 unhash_client_locked(clp);
2604 spin_unlock(&nn->client_lock);
2606 if (!already_expired)
2609 wait_event(expiry_wq, clp->cl_nfsd_dentry == NULL);
2612 static ssize_t client_ctl_write(struct file *file, const char __user *buf,
2613 size_t size, loff_t *pos)
2616 struct nfs4_client *clp;
2618 data = simple_transaction_get(file, buf, size);
2620 return PTR_ERR(data);
2621 if (size != 7 || 0 != memcmp(data, "expire\n", 7))
2623 clp = get_nfsdfs_clp(file_inode(file));
2626 force_expire_client(clp);
2631 static const struct file_operations client_ctl_fops = {
2632 .write = client_ctl_write,
2633 .release = simple_transaction_release,
2636 static const struct tree_descr client_files[] = {
2637 [0] = {"info", &client_info_fops, S_IRUSR},
2638 [1] = {"states", &client_states_fops, S_IRUSR},
2639 [2] = {"ctl", &client_ctl_fops, S_IRUSR|S_IWUSR},
2643 static struct nfs4_client *create_client(struct xdr_netobj name,
2644 struct svc_rqst *rqstp, nfs4_verifier *verf)
2646 struct nfs4_client *clp;
2647 struct sockaddr *sa = svc_addr(rqstp);
2649 struct net *net = SVC_NET(rqstp);
2650 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2652 clp = alloc_client(name);
2656 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
2662 kref_init(&clp->cl_nfsdfs.cl_ref);
2663 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
2664 clp->cl_time = get_seconds();
2665 clear_bit(0, &clp->cl_cb_slot_busy);
2666 copy_verf(clp, verf);
2667 memcpy(&clp->cl_addr, sa, sizeof(struct sockaddr_storage));
2668 clp->cl_cb_session = NULL;
2670 clp->cl_nfsd_dentry = nfsd_client_mkdir(nn, &clp->cl_nfsdfs,
2671 clp->cl_clientid.cl_id - nn->clientid_base,
2673 if (!clp->cl_nfsd_dentry) {
2681 add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
2683 struct rb_node **new = &(root->rb_node), *parent = NULL;
2684 struct nfs4_client *clp;
2687 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
2690 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
2691 new = &((*new)->rb_left);
2693 new = &((*new)->rb_right);
2696 rb_link_node(&new_clp->cl_namenode, parent, new);
2697 rb_insert_color(&new_clp->cl_namenode, root);
2700 static struct nfs4_client *
2701 find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2704 struct rb_node *node = root->rb_node;
2705 struct nfs4_client *clp;
2708 clp = rb_entry(node, struct nfs4_client, cl_namenode);
2709 cmp = compare_blob(&clp->cl_name, name);
2711 node = node->rb_left;
2713 node = node->rb_right;
2721 add_to_unconfirmed(struct nfs4_client *clp)
2723 unsigned int idhashval;
2724 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2726 lockdep_assert_held(&nn->client_lock);
2728 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2729 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
2730 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
2731 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
2732 renew_client_locked(clp);
2736 move_to_confirmed(struct nfs4_client *clp)
2738 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
2739 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2741 lockdep_assert_held(&nn->client_lock);
2743 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
2744 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
2745 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
2746 add_clp_to_name_tree(clp, &nn->conf_name_tree);
2747 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2748 renew_client_locked(clp);
2751 static struct nfs4_client *
2752 find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
2754 struct nfs4_client *clp;
2755 unsigned int idhashval = clientid_hashval(clid->cl_id);
2757 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
2758 if (same_clid(&clp->cl_clientid, clid)) {
2759 if ((bool)clp->cl_minorversion != sessions)
2761 renew_client_locked(clp);
2768 static struct nfs4_client *
2769 find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2771 struct list_head *tbl = nn->conf_id_hashtbl;
2773 lockdep_assert_held(&nn->client_lock);
2774 return find_client_in_id_table(tbl, clid, sessions);
2777 static struct nfs4_client *
2778 find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2780 struct list_head *tbl = nn->unconf_id_hashtbl;
2782 lockdep_assert_held(&nn->client_lock);
2783 return find_client_in_id_table(tbl, clid, sessions);
2786 static bool clp_used_exchangeid(struct nfs4_client *clp)
2788 return clp->cl_exchange_flags != 0;
2791 static struct nfs4_client *
2792 find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
2794 lockdep_assert_held(&nn->client_lock);
2795 return find_clp_in_name_tree(name, &nn->conf_name_tree);
2798 static struct nfs4_client *
2799 find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
2801 lockdep_assert_held(&nn->client_lock);
2802 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
2806 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
2808 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
2809 struct sockaddr *sa = svc_addr(rqstp);
2810 u32 scopeid = rpc_get_scope_id(sa);
2811 unsigned short expected_family;
2813 /* Currently, we only support tcp and tcp6 for the callback channel */
2814 if (se->se_callback_netid_len == 3 &&
2815 !memcmp(se->se_callback_netid_val, "tcp", 3))
2816 expected_family = AF_INET;
2817 else if (se->se_callback_netid_len == 4 &&
2818 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2819 expected_family = AF_INET6;
2823 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
2824 se->se_callback_addr_len,
2825 (struct sockaddr *)&conn->cb_addr,
2826 sizeof(conn->cb_addr));
2828 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
2831 if (conn->cb_addr.ss_family == AF_INET6)
2832 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
2834 conn->cb_prog = se->se_callback_prog;
2835 conn->cb_ident = se->se_callback_ident;
2836 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
2839 conn->cb_addr.ss_family = AF_UNSPEC;
2840 conn->cb_addrlen = 0;
2841 dprintk("NFSD: this client (clientid %08x/%08x) "
2842 "will not receive delegations\n",
2843 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2849 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
2852 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2854 struct xdr_buf *buf = resp->xdr.buf;
2855 struct nfsd4_slot *slot = resp->cstate.slot;
2858 dprintk("--> %s slot %p\n", __func__, slot);
2860 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
2861 slot->sl_opcnt = resp->opcnt;
2862 slot->sl_status = resp->cstate.status;
2863 free_svc_cred(&slot->sl_cred);
2864 copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred);
2866 if (!nfsd4_cache_this(resp)) {
2867 slot->sl_flags &= ~NFSD4_SLOT_CACHED;
2870 slot->sl_flags |= NFSD4_SLOT_CACHED;
2872 base = resp->cstate.data_offset;
2873 slot->sl_datalen = buf->len - base;
2874 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
2875 WARN(1, "%s: sessions DRC could not cache compound\n",
2881 * Encode the replay sequence operation from the slot values.
2882 * If cachethis is FALSE encode the uncached rep error on the next
2883 * operation which sets resp->p and increments resp->opcnt for
2884 * nfs4svc_encode_compoundres.
2888 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2889 struct nfsd4_compoundres *resp)
2891 struct nfsd4_op *op;
2892 struct nfsd4_slot *slot = resp->cstate.slot;
2894 /* Encode the replayed sequence operation */
2895 op = &args->ops[resp->opcnt - 1];
2896 nfsd4_encode_operation(resp, op);
2898 if (slot->sl_flags & NFSD4_SLOT_CACHED)
2900 if (args->opcnt == 1) {
2902 * The original operation wasn't a solo sequence--we
2903 * always cache those--so this retry must not match the
2906 op->status = nfserr_seq_false_retry;
2908 op = &args->ops[resp->opcnt++];
2909 op->status = nfserr_retry_uncached_rep;
2910 nfsd4_encode_operation(resp, op);
2916 * The sequence operation is not cached because we can use the slot and
2920 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2921 struct nfsd4_sequence *seq)
2923 struct nfsd4_slot *slot = resp->cstate.slot;
2924 struct xdr_stream *xdr = &resp->xdr;
2928 dprintk("--> %s slot %p\n", __func__, slot);
2930 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
2934 p = xdr_reserve_space(xdr, slot->sl_datalen);
2937 return nfserr_serverfault;
2939 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2940 xdr_commit_encode(xdr);
2942 resp->opcnt = slot->sl_opcnt;
2943 return slot->sl_status;
2947 * Set the exchange_id flags returned by the server.
2950 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2952 #ifdef CONFIG_NFSD_PNFS
2953 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
2955 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
2958 /* Referrals are supported, Migration is not. */
2959 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2961 /* set the wire flags to return to client. */
2962 clid->flags = new->cl_exchange_flags;
2965 static bool client_has_openowners(struct nfs4_client *clp)
2967 struct nfs4_openowner *oo;
2969 list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
2970 if (!list_empty(&oo->oo_owner.so_stateids))
2976 static bool client_has_state(struct nfs4_client *clp)
2978 return client_has_openowners(clp)
2979 #ifdef CONFIG_NFSD_PNFS
2980 || !list_empty(&clp->cl_lo_states)
2982 || !list_empty(&clp->cl_delegations)
2983 || !list_empty(&clp->cl_sessions)
2984 || !list_empty(&clp->async_copies);
2987 static __be32 copy_impl_id(struct nfs4_client *clp,
2988 struct nfsd4_exchange_id *exid)
2990 if (!exid->nii_domain.data)
2992 xdr_netobj_dup(&clp->cl_nii_domain, &exid->nii_domain, GFP_KERNEL);
2993 if (!clp->cl_nii_domain.data)
2994 return nfserr_jukebox;
2995 xdr_netobj_dup(&clp->cl_nii_name, &exid->nii_name, GFP_KERNEL);
2996 if (!clp->cl_nii_name.data)
2997 return nfserr_jukebox;
2998 clp->cl_nii_time.tv_sec = exid->nii_time.tv_sec;
2999 clp->cl_nii_time.tv_nsec = exid->nii_time.tv_nsec;
3004 nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3005 union nfsd4_op_u *u)
3007 struct nfsd4_exchange_id *exid = &u->exchange_id;
3008 struct nfs4_client *conf, *new;
3009 struct nfs4_client *unconf = NULL;
3011 char addr_str[INET6_ADDRSTRLEN];
3012 nfs4_verifier verf = exid->verifier;
3013 struct sockaddr *sa = svc_addr(rqstp);
3014 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
3015 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3017 rpc_ntop(sa, addr_str, sizeof(addr_str));
3018 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
3019 "ip_addr=%s flags %x, spa_how %d\n",
3020 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
3021 addr_str, exid->flags, exid->spa_how);
3023 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
3024 return nfserr_inval;
3026 new = create_client(exid->clname, rqstp, &verf);
3028 return nfserr_jukebox;
3029 status = copy_impl_id(new, exid);
3033 switch (exid->spa_how) {
3035 exid->spo_must_enforce[0] = 0;
3036 exid->spo_must_enforce[1] = (
3037 1 << (OP_BIND_CONN_TO_SESSION - 32) |
3038 1 << (OP_EXCHANGE_ID - 32) |
3039 1 << (OP_CREATE_SESSION - 32) |
3040 1 << (OP_DESTROY_SESSION - 32) |
3041 1 << (OP_DESTROY_CLIENTID - 32));
3043 exid->spo_must_allow[0] &= (1 << (OP_CLOSE) |
3044 1 << (OP_OPEN_DOWNGRADE) |
3046 1 << (OP_DELEGRETURN));
3048 exid->spo_must_allow[1] &= (
3049 1 << (OP_TEST_STATEID - 32) |
3050 1 << (OP_FREE_STATEID - 32));
3051 if (!svc_rqst_integrity_protected(rqstp)) {
3052 status = nfserr_inval;
3056 * Sometimes userspace doesn't give us a principal.
3057 * Which is a bug, really. Anyway, we can't enforce
3058 * MACH_CRED in that case, better to give up now:
3060 if (!new->cl_cred.cr_principal &&
3061 !new->cl_cred.cr_raw_principal) {
3062 status = nfserr_serverfault;
3065 new->cl_mach_cred = true;
3068 default: /* checked by xdr code */
3072 status = nfserr_encr_alg_unsupp;
3076 /* Cases below refer to rfc 5661 section 18.35.4: */
3077 spin_lock(&nn->client_lock);
3078 conf = find_confirmed_client_by_name(&exid->clname, nn);
3080 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
3081 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
3084 if (!clp_used_exchangeid(conf)) { /* buggy client */
3085 status = nfserr_inval;
3088 if (!nfsd4_mach_creds_match(conf, rqstp)) {
3089 status = nfserr_wrong_cred;
3092 if (!creds_match) { /* case 9 */
3093 status = nfserr_perm;
3096 if (!verfs_match) { /* case 8 */
3097 status = nfserr_not_same;
3101 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
3104 if (!creds_match) { /* case 3 */
3105 if (client_has_state(conf)) {
3106 status = nfserr_clid_inuse;
3111 if (verfs_match) { /* case 2 */
3112 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
3115 /* case 5, client reboot */
3120 if (update) { /* case 7 */
3121 status = nfserr_noent;
3125 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
3126 if (unconf) /* case 4, possible retry or client restart */
3127 unhash_client_locked(unconf);
3129 /* case 1 (normal case) */
3132 status = mark_client_expired_locked(conf);
3136 new->cl_minorversion = cstate->minorversion;
3137 new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0];
3138 new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1];
3140 add_to_unconfirmed(new);
3143 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
3144 exid->clientid.cl_id = conf->cl_clientid.cl_id;
3146 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
3147 nfsd4_set_ex_flags(conf, exid);
3149 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
3150 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
3154 spin_unlock(&nn->client_lock);
3159 expire_client(unconf);
3164 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
3166 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
3169 /* The slot is in use, and no response has been sent. */
3171 if (seqid == slot_seqid)
3172 return nfserr_jukebox;
3174 return nfserr_seq_misordered;
3176 /* Note unsigned 32-bit arithmetic handles wraparound: */
3177 if (likely(seqid == slot_seqid + 1))
3179 if (seqid == slot_seqid)
3180 return nfserr_replay_cache;
3181 return nfserr_seq_misordered;
3185 * Cache the create session result into the create session single DRC
3186 * slot cache by saving the xdr structure. sl_seqid has been set.
3187 * Do this for solo or embedded create session operations.
3190 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
3191 struct nfsd4_clid_slot *slot, __be32 nfserr)
3193 slot->sl_status = nfserr;
3194 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
3198 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
3199 struct nfsd4_clid_slot *slot)
3201 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
3202 return slot->sl_status;
3205 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
3206 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
3207 1 + /* MIN tag is length with zero, only length */ \
3208 3 + /* version, opcount, opcode */ \
3209 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3210 /* seqid, slotID, slotID, cache */ \
3211 4 ) * sizeof(__be32))
3213 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
3214 2 + /* verifier: AUTH_NULL, length 0 */\
3216 1 + /* MIN tag is length with zero, only length */ \
3217 3 + /* opcount, opcode, opstatus*/ \
3218 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3219 /* seqid, slotID, slotID, slotID, status */ \
3220 5 ) * sizeof(__be32))
3222 static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
3224 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
3226 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
3227 return nfserr_toosmall;
3228 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
3229 return nfserr_toosmall;
3230 ca->headerpadsz = 0;
3231 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
3232 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
3233 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
3234 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
3235 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
3236 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
3238 * Note decreasing slot size below client's request may make it
3239 * difficult for client to function correctly, whereas
3240 * decreasing the number of slots will (just?) affect
3241 * performance. When short on memory we therefore prefer to
3242 * decrease number of slots instead of their size. Clients that
3243 * request larger slots than they need will get poor results:
3244 * Note that we always allow at least one slot, because our
3245 * accounting is soft and provides no guarantees either way.
3247 ca->maxreqs = nfsd4_get_drc_mem(ca, nn);
3253 * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
3254 * These are based on similar macros in linux/sunrpc/msg_prot.h .
3256 #define RPC_MAX_HEADER_WITH_AUTH_SYS \
3257 (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
3259 #define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
3260 (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
3262 #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
3263 RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
3264 #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
3265 RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
3268 static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
3270 ca->headerpadsz = 0;
3272 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
3273 return nfserr_toosmall;
3274 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
3275 return nfserr_toosmall;
3276 ca->maxresp_cached = 0;
3278 return nfserr_toosmall;
3283 static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
3285 switch (cbs->flavor) {
3291 * GSS case: the spec doesn't allow us to return this
3292 * error. But it also doesn't allow us not to support
3294 * I'd rather this fail hard than return some error the
3295 * client might think it can already handle:
3297 return nfserr_encr_alg_unsupp;
3302 nfsd4_create_session(struct svc_rqst *rqstp,
3303 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
3305 struct nfsd4_create_session *cr_ses = &u->create_session;
3306 struct sockaddr *sa = svc_addr(rqstp);
3307 struct nfs4_client *conf, *unconf;
3308 struct nfs4_client *old = NULL;
3309 struct nfsd4_session *new;
3310 struct nfsd4_conn *conn;
3311 struct nfsd4_clid_slot *cs_slot = NULL;
3313 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3315 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
3316 return nfserr_inval;
3317 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
3320 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
3323 status = check_backchannel_attrs(&cr_ses->back_channel);
3325 goto out_release_drc_mem;
3326 status = nfserr_jukebox;
3327 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
3329 goto out_release_drc_mem;
3330 conn = alloc_conn_from_crses(rqstp, cr_ses);
3332 goto out_free_session;
3334 spin_lock(&nn->client_lock);
3335 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
3336 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
3337 WARN_ON_ONCE(conf && unconf);
3340 status = nfserr_wrong_cred;
3341 if (!nfsd4_mach_creds_match(conf, rqstp))
3343 cs_slot = &conf->cl_cs_slot;
3344 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
3346 if (status == nfserr_replay_cache)
3347 status = nfsd4_replay_create_session(cr_ses, cs_slot);
3350 } else if (unconf) {
3351 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
3352 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
3353 status = nfserr_clid_inuse;
3356 status = nfserr_wrong_cred;
3357 if (!nfsd4_mach_creds_match(unconf, rqstp))
3359 cs_slot = &unconf->cl_cs_slot;
3360 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
3362 /* an unconfirmed replay returns misordered */
3363 status = nfserr_seq_misordered;
3366 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3368 status = mark_client_expired_locked(old);
3374 move_to_confirmed(unconf);
3377 status = nfserr_stale_clientid;
3381 /* Persistent sessions are not supported */
3382 cr_ses->flags &= ~SESSION4_PERSIST;
3383 /* Upshifting from TCP to RDMA is not supported */
3384 cr_ses->flags &= ~SESSION4_RDMA;
3386 init_session(rqstp, new, conf, cr_ses);
3387 nfsd4_get_session_locked(new);
3389 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
3390 NFS4_MAX_SESSIONID_LEN);
3391 cs_slot->sl_seqid++;
3392 cr_ses->seqid = cs_slot->sl_seqid;
3394 /* cache solo and embedded create sessions under the client_lock */
3395 nfsd4_cache_create_session(cr_ses, cs_slot, status);
3396 spin_unlock(&nn->client_lock);
3397 /* init connection and backchannel */
3398 nfsd4_init_conn(rqstp, conn, new);
3399 nfsd4_put_session(new);
3404 spin_unlock(&nn->client_lock);
3409 __free_session(new);
3410 out_release_drc_mem:
3411 nfsd4_put_drc_mem(&cr_ses->fore_channel);
3415 static __be32 nfsd4_map_bcts_dir(u32 *dir)
3418 case NFS4_CDFC4_FORE:
3419 case NFS4_CDFC4_BACK:
3421 case NFS4_CDFC4_FORE_OR_BOTH:
3422 case NFS4_CDFC4_BACK_OR_BOTH:
3423 *dir = NFS4_CDFC4_BOTH;
3426 return nfserr_inval;
3429 __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp,
3430 struct nfsd4_compound_state *cstate,
3431 union nfsd4_op_u *u)
3433 struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl;
3434 struct nfsd4_session *session = cstate->session;
3435 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3438 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
3441 spin_lock(&nn->client_lock);
3442 session->se_cb_prog = bc->bc_cb_program;
3443 session->se_cb_sec = bc->bc_cb_sec;
3444 spin_unlock(&nn->client_lock);
3446 nfsd4_probe_callback(session->se_client);
3451 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
3452 struct nfsd4_compound_state *cstate,
3453 union nfsd4_op_u *u)
3455 struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session;
3457 struct nfsd4_conn *conn;
3458 struct nfsd4_session *session;
3459 struct net *net = SVC_NET(rqstp);
3460 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3462 if (!nfsd4_last_compound_op(rqstp))
3463 return nfserr_not_only_op;
3464 spin_lock(&nn->client_lock);
3465 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
3466 spin_unlock(&nn->client_lock);
3468 goto out_no_session;
3469 status = nfserr_wrong_cred;
3470 if (!nfsd4_mach_creds_match(session->se_client, rqstp))
3472 status = nfsd4_map_bcts_dir(&bcts->dir);
3475 conn = alloc_conn(rqstp, bcts->dir);
3476 status = nfserr_jukebox;
3479 nfsd4_init_conn(rqstp, conn, session);
3482 nfsd4_put_session(session);
3487 static bool nfsd4_compound_in_session(struct nfsd4_compound_state *cstate, struct nfs4_sessionid *sid)
3489 if (!cstate->session)
3491 return !memcmp(sid, &cstate->session->se_sessionid, sizeof(*sid));
3495 nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate,
3496 union nfsd4_op_u *u)
3498 struct nfs4_sessionid *sessionid = &u->destroy_session.sessionid;
3499 struct nfsd4_session *ses;
3501 int ref_held_by_me = 0;
3502 struct net *net = SVC_NET(r);
3503 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3505 status = nfserr_not_only_op;
3506 if (nfsd4_compound_in_session(cstate, sessionid)) {
3507 if (!nfsd4_last_compound_op(r))
3511 dump_sessionid(__func__, sessionid);
3512 spin_lock(&nn->client_lock);
3513 ses = find_in_sessionid_hashtbl(sessionid, net, &status);
3515 goto out_client_lock;
3516 status = nfserr_wrong_cred;
3517 if (!nfsd4_mach_creds_match(ses->se_client, r))
3518 goto out_put_session;
3519 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
3521 goto out_put_session;
3522 unhash_session(ses);
3523 spin_unlock(&nn->client_lock);
3525 nfsd4_probe_callback_sync(ses->se_client);
3527 spin_lock(&nn->client_lock);
3530 nfsd4_put_session_locked(ses);
3532 spin_unlock(&nn->client_lock);
3537 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
3539 struct nfsd4_conn *c;
3541 list_for_each_entry(c, &s->se_conns, cn_persession) {
3542 if (c->cn_xprt == xpt) {
3549 static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
3551 struct nfs4_client *clp = ses->se_client;
3552 struct nfsd4_conn *c;
3553 __be32 status = nfs_ok;
3556 spin_lock(&clp->cl_lock);
3557 c = __nfsd4_find_conn(new->cn_xprt, ses);
3560 status = nfserr_conn_not_bound_to_session;
3561 if (clp->cl_mach_cred)
3563 __nfsd4_hash_conn(new, ses);
3564 spin_unlock(&clp->cl_lock);
3565 ret = nfsd4_register_conn(new);
3567 /* oops; xprt is already down: */
3568 nfsd4_conn_lost(&new->cn_xpt_user);
3571 spin_unlock(&clp->cl_lock);
3576 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
3578 struct nfsd4_compoundargs *args = rqstp->rq_argp;
3580 return args->opcnt > session->se_fchannel.maxops;
3583 static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
3584 struct nfsd4_session *session)
3586 struct xdr_buf *xb = &rqstp->rq_arg;
3588 return xb->len > session->se_fchannel.maxreq_sz;
3591 static bool replay_matches_cache(struct svc_rqst *rqstp,
3592 struct nfsd4_sequence *seq, struct nfsd4_slot *slot)
3594 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
3596 if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) !=
3597 (bool)seq->cachethis)
3600 * If there's an error then the reply can have fewer ops than
3603 if (slot->sl_opcnt < argp->opcnt && !slot->sl_status)
3606 * But if we cached a reply with *more* ops than the call you're
3607 * sending us now, then this new call is clearly not really a
3608 * replay of the old one:
3610 if (slot->sl_opcnt > argp->opcnt)
3612 /* This is the only check explicitly called by spec: */
3613 if (!same_creds(&rqstp->rq_cred, &slot->sl_cred))
3616 * There may be more comparisons we could actually do, but the
3617 * spec doesn't require us to catch every case where the calls
3618 * don't match (that would require caching the call as well as
3619 * the reply), so we don't bother.
3625 nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3626 union nfsd4_op_u *u)
3628 struct nfsd4_sequence *seq = &u->sequence;
3629 struct nfsd4_compoundres *resp = rqstp->rq_resp;
3630 struct xdr_stream *xdr = &resp->xdr;
3631 struct nfsd4_session *session;
3632 struct nfs4_client *clp;
3633 struct nfsd4_slot *slot;
3634 struct nfsd4_conn *conn;
3637 struct net *net = SVC_NET(rqstp);
3638 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3640 if (resp->opcnt != 1)
3641 return nfserr_sequence_pos;
3644 * Will be either used or freed by nfsd4_sequence_check_conn
3647 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
3649 return nfserr_jukebox;
3651 spin_lock(&nn->client_lock);
3652 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
3654 goto out_no_session;
3655 clp = session->se_client;
3657 status = nfserr_too_many_ops;
3658 if (nfsd4_session_too_many_ops(rqstp, session))
3659 goto out_put_session;
3661 status = nfserr_req_too_big;
3662 if (nfsd4_request_too_big(rqstp, session))
3663 goto out_put_session;
3665 status = nfserr_badslot;
3666 if (seq->slotid >= session->se_fchannel.maxreqs)
3667 goto out_put_session;
3669 slot = session->se_slots[seq->slotid];
3670 dprintk("%s: slotid %d\n", __func__, seq->slotid);
3672 /* We do not negotiate the number of slots yet, so set the
3673 * maxslots to the session maxreqs which is used to encode
3674 * sr_highest_slotid and the sr_target_slot id to maxslots */
3675 seq->maxslots = session->se_fchannel.maxreqs;
3677 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
3678 slot->sl_flags & NFSD4_SLOT_INUSE);
3679 if (status == nfserr_replay_cache) {
3680 status = nfserr_seq_misordered;
3681 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
3682 goto out_put_session;
3683 status = nfserr_seq_false_retry;
3684 if (!replay_matches_cache(rqstp, seq, slot))
3685 goto out_put_session;
3686 cstate->slot = slot;
3687 cstate->session = session;
3689 /* Return the cached reply status and set cstate->status
3690 * for nfsd4_proc_compound processing */
3691 status = nfsd4_replay_cache_entry(resp, seq);
3692 cstate->status = nfserr_replay_cache;
3696 goto out_put_session;
3698 status = nfsd4_sequence_check_conn(conn, session);
3701 goto out_put_session;
3703 buflen = (seq->cachethis) ?
3704 session->se_fchannel.maxresp_cached :
3705 session->se_fchannel.maxresp_sz;
3706 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
3708 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
3709 goto out_put_session;
3710 svc_reserve(rqstp, buflen);
3713 /* Success! bump slot seqid */
3714 slot->sl_seqid = seq->seqid;
3715 slot->sl_flags |= NFSD4_SLOT_INUSE;
3717 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
3719 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
3721 cstate->slot = slot;
3722 cstate->session = session;
3726 switch (clp->cl_cb_state) {
3728 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
3730 case NFSD4_CB_FAULT:
3731 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
3734 seq->status_flags = 0;
3736 if (!list_empty(&clp->cl_revoked))
3737 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
3741 spin_unlock(&nn->client_lock);
3744 nfsd4_put_session_locked(session);
3745 goto out_no_session;
3749 nfsd4_sequence_done(struct nfsd4_compoundres *resp)
3751 struct nfsd4_compound_state *cs = &resp->cstate;
3753 if (nfsd4_has_session(cs)) {
3754 if (cs->status != nfserr_replay_cache) {
3755 nfsd4_store_cache_entry(resp);
3756 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
3758 /* Drop session reference that was taken in nfsd4_sequence() */
3759 nfsd4_put_session(cs->session);
3761 put_client_renew(cs->clp);
3765 nfsd4_destroy_clientid(struct svc_rqst *rqstp,
3766 struct nfsd4_compound_state *cstate,
3767 union nfsd4_op_u *u)
3769 struct nfsd4_destroy_clientid *dc = &u->destroy_clientid;
3770 struct nfs4_client *conf, *unconf;
3771 struct nfs4_client *clp = NULL;
3773 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3775 spin_lock(&nn->client_lock);
3776 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
3777 conf = find_confirmed_client(&dc->clientid, true, nn);
3778 WARN_ON_ONCE(conf && unconf);
3781 if (client_has_state(conf)) {
3782 status = nfserr_clientid_busy;
3785 status = mark_client_expired_locked(conf);
3792 status = nfserr_stale_clientid;
3795 if (!nfsd4_mach_creds_match(clp, rqstp)) {
3797 status = nfserr_wrong_cred;
3800 unhash_client_locked(clp);
3802 spin_unlock(&nn->client_lock);
3809 nfsd4_reclaim_complete(struct svc_rqst *rqstp,
3810 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
3812 struct nfsd4_reclaim_complete *rc = &u->reclaim_complete;
3815 if (rc->rca_one_fs) {
3816 if (!cstate->current_fh.fh_dentry)
3817 return nfserr_nofilehandle;
3819 * We don't take advantage of the rca_one_fs case.
3820 * That's OK, it's optional, we can safely ignore it.
3825 status = nfserr_complete_already;
3826 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
3827 &cstate->session->se_client->cl_flags))
3830 status = nfserr_stale_clientid;
3831 if (is_client_expired(cstate->session->se_client))
3833 * The following error isn't really legal.
3834 * But we only get here if the client just explicitly
3835 * destroyed the client. Surely it no longer cares what
3836 * error it gets back on an operation for the dead
3842 nfsd4_client_record_create(cstate->session->se_client);
3843 inc_reclaim_complete(cstate->session->se_client);
3849 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3850 union nfsd4_op_u *u)
3852 struct nfsd4_setclientid *setclid = &u->setclientid;
3853 struct xdr_netobj clname = setclid->se_name;
3854 nfs4_verifier clverifier = setclid->se_verf;
3855 struct nfs4_client *conf, *new;
3856 struct nfs4_client *unconf = NULL;
3858 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3860 new = create_client(clname, rqstp, &clverifier);
3862 return nfserr_jukebox;
3863 /* Cases below refer to rfc 3530 section 14.2.33: */
3864 spin_lock(&nn->client_lock);
3865 conf = find_confirmed_client_by_name(&clname, nn);
3866 if (conf && client_has_state(conf)) {
3868 status = nfserr_clid_inuse;
3869 if (clp_used_exchangeid(conf))
3871 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
3872 char addr_str[INET6_ADDRSTRLEN];
3873 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
3875 dprintk("NFSD: setclientid: string in use by client "
3876 "at %s\n", addr_str);
3880 unconf = find_unconfirmed_client_by_name(&clname, nn);
3882 unhash_client_locked(unconf);
3883 if (conf && same_verf(&conf->cl_verifier, &clverifier)) {
3884 /* case 1: probable callback update */
3885 copy_clid(new, conf);
3886 gen_confirm(new, nn);
3887 } else /* case 4 (new client) or cases 2, 3 (client reboot): */
3889 new->cl_minorversion = 0;
3890 gen_callback(new, setclid, rqstp);
3891 add_to_unconfirmed(new);
3892 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
3893 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
3894 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
3898 spin_unlock(&nn->client_lock);
3902 expire_client(unconf);
3908 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
3909 struct nfsd4_compound_state *cstate,
3910 union nfsd4_op_u *u)
3912 struct nfsd4_setclientid_confirm *setclientid_confirm =
3913 &u->setclientid_confirm;
3914 struct nfs4_client *conf, *unconf;
3915 struct nfs4_client *old = NULL;
3916 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
3917 clientid_t * clid = &setclientid_confirm->sc_clientid;
3919 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3921 if (STALE_CLIENTID(clid, nn))
3922 return nfserr_stale_clientid;
3924 spin_lock(&nn->client_lock);
3925 conf = find_confirmed_client(clid, false, nn);
3926 unconf = find_unconfirmed_client(clid, false, nn);
3928 * We try hard to give out unique clientid's, so if we get an
3929 * attempt to confirm the same clientid with a different cred,
3930 * the client may be buggy; this should never happen.
3932 * Nevertheless, RFC 7530 recommends INUSE for this case:
3934 status = nfserr_clid_inuse;
3935 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3937 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3939 /* cases below refer to rfc 3530 section 14.2.34: */
3940 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
3941 if (conf && same_verf(&confirm, &conf->cl_confirm)) {
3942 /* case 2: probable retransmit */
3944 } else /* case 4: client hasn't noticed we rebooted yet? */
3945 status = nfserr_stale_clientid;
3949 if (conf) { /* case 1: callback update */
3951 unhash_client_locked(old);
3952 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
3953 } else { /* case 3: normal case; new or rebooted client */
3954 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3956 status = nfserr_clid_inuse;
3957 if (client_has_state(old)
3958 && !same_creds(&unconf->cl_cred,
3961 status = mark_client_expired_locked(old);
3967 move_to_confirmed(unconf);
3970 get_client_locked(conf);
3971 spin_unlock(&nn->client_lock);
3972 nfsd4_probe_callback(conf);
3973 spin_lock(&nn->client_lock);
3974 put_client_renew_locked(conf);
3976 spin_unlock(&nn->client_lock);
3982 static struct nfs4_file *nfsd4_alloc_file(void)
3984 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3987 /* OPEN Share state helper functions */
3988 static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
3989 struct nfs4_file *fp)
3991 lockdep_assert_held(&state_lock);
3993 refcount_set(&fp->fi_ref, 1);
3994 spin_lock_init(&fp->fi_lock);
3995 INIT_LIST_HEAD(&fp->fi_stateids);
3996 INIT_LIST_HEAD(&fp->fi_delegations);
3997 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
3998 fh_copy_shallow(&fp->fi_fhandle, fh);
3999 fp->fi_deleg_file = NULL;
4000 fp->fi_had_conflict = false;
4001 fp->fi_share_deny = 0;
4002 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
4003 memset(fp->fi_access, 0, sizeof(fp->fi_access));
4004 #ifdef CONFIG_NFSD_PNFS
4005 INIT_LIST_HEAD(&fp->fi_lo_states);
4006 atomic_set(&fp->fi_lo_recalls, 0);
4008 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
4012 nfsd4_free_slabs(void)
4014 kmem_cache_destroy(client_slab);
4015 kmem_cache_destroy(openowner_slab);
4016 kmem_cache_destroy(lockowner_slab);
4017 kmem_cache_destroy(file_slab);
4018 kmem_cache_destroy(stateid_slab);
4019 kmem_cache_destroy(deleg_slab);
4020 kmem_cache_destroy(odstate_slab);
4024 nfsd4_init_slabs(void)
4026 client_slab = kmem_cache_create("nfsd4_clients",
4027 sizeof(struct nfs4_client), 0, 0, NULL);
4028 if (client_slab == NULL)
4030 openowner_slab = kmem_cache_create("nfsd4_openowners",
4031 sizeof(struct nfs4_openowner), 0, 0, NULL);
4032 if (openowner_slab == NULL)
4033 goto out_free_client_slab;
4034 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
4035 sizeof(struct nfs4_lockowner), 0, 0, NULL);
4036 if (lockowner_slab == NULL)
4037 goto out_free_openowner_slab;
4038 file_slab = kmem_cache_create("nfsd4_files",
4039 sizeof(struct nfs4_file), 0, 0, NULL);
4040 if (file_slab == NULL)
4041 goto out_free_lockowner_slab;
4042 stateid_slab = kmem_cache_create("nfsd4_stateids",
4043 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
4044 if (stateid_slab == NULL)
4045 goto out_free_file_slab;
4046 deleg_slab = kmem_cache_create("nfsd4_delegations",
4047 sizeof(struct nfs4_delegation), 0, 0, NULL);
4048 if (deleg_slab == NULL)
4049 goto out_free_stateid_slab;
4050 odstate_slab = kmem_cache_create("nfsd4_odstate",
4051 sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
4052 if (odstate_slab == NULL)
4053 goto out_free_deleg_slab;
4056 out_free_deleg_slab:
4057 kmem_cache_destroy(deleg_slab);
4058 out_free_stateid_slab:
4059 kmem_cache_destroy(stateid_slab);
4061 kmem_cache_destroy(file_slab);
4062 out_free_lockowner_slab:
4063 kmem_cache_destroy(lockowner_slab);
4064 out_free_openowner_slab:
4065 kmem_cache_destroy(openowner_slab);
4066 out_free_client_slab:
4067 kmem_cache_destroy(client_slab);
4069 dprintk("nfsd4: out of memory while initializing nfsv4\n");
4073 static void init_nfs4_replay(struct nfs4_replay *rp)
4075 rp->rp_status = nfserr_serverfault;
4077 rp->rp_buf = rp->rp_ibuf;
4078 mutex_init(&rp->rp_mutex);
4081 static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
4082 struct nfs4_stateowner *so)
4084 if (!nfsd4_has_session(cstate)) {
4085 mutex_lock(&so->so_replay.rp_mutex);
4086 cstate->replay_owner = nfs4_get_stateowner(so);
4090 void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
4092 struct nfs4_stateowner *so = cstate->replay_owner;
4095 cstate->replay_owner = NULL;
4096 mutex_unlock(&so->so_replay.rp_mutex);
4097 nfs4_put_stateowner(so);
4101 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
4103 struct nfs4_stateowner *sop;
4105 sop = kmem_cache_alloc(slab, GFP_KERNEL);
4109 xdr_netobj_dup(&sop->so_owner, owner, GFP_KERNEL);
4110 if (!sop->so_owner.data) {
4111 kmem_cache_free(slab, sop);
4115 INIT_LIST_HEAD(&sop->so_stateids);
4116 sop->so_client = clp;
4117 init_nfs4_replay(&sop->so_replay);
4118 atomic_set(&sop->so_count, 1);
4122 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
4124 lockdep_assert_held(&clp->cl_lock);
4126 list_add(&oo->oo_owner.so_strhash,
4127 &clp->cl_ownerstr_hashtbl[strhashval]);
4128 list_add(&oo->oo_perclient, &clp->cl_openowners);
4131 static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
4133 unhash_openowner_locked(openowner(so));
4136 static void nfs4_free_openowner(struct nfs4_stateowner *so)
4138 struct nfs4_openowner *oo = openowner(so);
4140 kmem_cache_free(openowner_slab, oo);
4143 static const struct nfs4_stateowner_operations openowner_ops = {
4144 .so_unhash = nfs4_unhash_openowner,
4145 .so_free = nfs4_free_openowner,
4148 static struct nfs4_ol_stateid *
4149 nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4151 struct nfs4_ol_stateid *local, *ret = NULL;
4152 struct nfs4_openowner *oo = open->op_openowner;
4154 lockdep_assert_held(&fp->fi_lock);
4156 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
4157 /* ignore lock owners */
4158 if (local->st_stateowner->so_is_open_owner == 0)
4160 if (local->st_stateowner != &oo->oo_owner)
4162 if (local->st_stid.sc_type == NFS4_OPEN_STID) {
4164 refcount_inc(&ret->st_stid.sc_count);
4172 nfsd4_verify_open_stid(struct nfs4_stid *s)
4174 __be32 ret = nfs_ok;
4176 switch (s->sc_type) {
4180 case NFS4_CLOSED_STID:
4181 case NFS4_CLOSED_DELEG_STID:
4182 ret = nfserr_bad_stateid;
4184 case NFS4_REVOKED_DELEG_STID:
4185 ret = nfserr_deleg_revoked;
4190 /* Lock the stateid st_mutex, and deal with races with CLOSE */
4192 nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp)
4196 mutex_lock_nested(&stp->st_mutex, LOCK_STATEID_MUTEX);
4197 ret = nfsd4_verify_open_stid(&stp->st_stid);
4199 mutex_unlock(&stp->st_mutex);
4203 static struct nfs4_ol_stateid *
4204 nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4206 struct nfs4_ol_stateid *stp;
4208 spin_lock(&fp->fi_lock);
4209 stp = nfsd4_find_existing_open(fp, open);
4210 spin_unlock(&fp->fi_lock);
4211 if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok)
4213 nfs4_put_stid(&stp->st_stid);
4218 static struct nfs4_openowner *
4219 alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
4220 struct nfsd4_compound_state *cstate)
4222 struct nfs4_client *clp = cstate->clp;
4223 struct nfs4_openowner *oo, *ret;
4225 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
4228 oo->oo_owner.so_ops = &openowner_ops;
4229 oo->oo_owner.so_is_open_owner = 1;
4230 oo->oo_owner.so_seqid = open->op_seqid;
4232 if (nfsd4_has_session(cstate))
4233 oo->oo_flags |= NFS4_OO_CONFIRMED;
4235 oo->oo_last_closed_stid = NULL;
4236 INIT_LIST_HEAD(&oo->oo_close_lru);
4237 spin_lock(&clp->cl_lock);
4238 ret = find_openstateowner_str_locked(strhashval, open, clp);
4240 hash_openowner(oo, clp, strhashval);
4243 nfs4_free_stateowner(&oo->oo_owner);
4245 spin_unlock(&clp->cl_lock);
4249 static struct nfs4_ol_stateid *
4250 init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
4253 struct nfs4_openowner *oo = open->op_openowner;
4254 struct nfs4_ol_stateid *retstp = NULL;
4255 struct nfs4_ol_stateid *stp;
4258 /* We are moving these outside of the spinlocks to avoid the warnings */
4259 mutex_init(&stp->st_mutex);
4260 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
4263 spin_lock(&oo->oo_owner.so_client->cl_lock);
4264 spin_lock(&fp->fi_lock);
4266 retstp = nfsd4_find_existing_open(fp, open);
4270 open->op_stp = NULL;
4271 refcount_inc(&stp->st_stid.sc_count);
4272 stp->st_stid.sc_type = NFS4_OPEN_STID;
4273 INIT_LIST_HEAD(&stp->st_locks);
4274 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
4276 stp->st_stid.sc_file = fp;
4277 stp->st_access_bmap = 0;
4278 stp->st_deny_bmap = 0;
4279 stp->st_openstp = NULL;
4280 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
4281 list_add(&stp->st_perfile, &fp->fi_stateids);
4284 spin_unlock(&fp->fi_lock);
4285 spin_unlock(&oo->oo_owner.so_client->cl_lock);
4287 /* Handle races with CLOSE */
4288 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
4289 nfs4_put_stid(&retstp->st_stid);
4292 /* To keep mutex tracking happy */
4293 mutex_unlock(&stp->st_mutex);
4300 * In the 4.0 case we need to keep the owners around a little while to handle
4301 * CLOSE replay. We still do need to release any file access that is held by
4302 * them before returning however.
4305 move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
4307 struct nfs4_ol_stateid *last;
4308 struct nfs4_openowner *oo = openowner(s->st_stateowner);
4309 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
4312 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
4315 * We know that we hold one reference via nfsd4_close, and another
4316 * "persistent" reference for the client. If the refcount is higher
4317 * than 2, then there are still calls in progress that are using this
4318 * stateid. We can't put the sc_file reference until they are finished.
4319 * Wait for the refcount to drop to 2. Since it has been unhashed,
4320 * there should be no danger of the refcount going back up again at
4323 wait_event(close_wq, refcount_read(&s->st_stid.sc_count) == 2);
4325 release_all_access(s);
4326 if (s->st_stid.sc_file) {
4327 put_nfs4_file(s->st_stid.sc_file);
4328 s->st_stid.sc_file = NULL;
4331 spin_lock(&nn->client_lock);
4332 last = oo->oo_last_closed_stid;
4333 oo->oo_last_closed_stid = s;
4334 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
4335 oo->oo_time = get_seconds();
4336 spin_unlock(&nn->client_lock);
4338 nfs4_put_stid(&last->st_stid);
4341 /* search file_hashtbl[] for file */
4342 static struct nfs4_file *
4343 find_file_locked(struct knfsd_fh *fh, unsigned int hashval)
4345 struct nfs4_file *fp;
4347 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash) {
4348 if (fh_match(&fp->fi_fhandle, fh)) {
4349 if (refcount_inc_not_zero(&fp->fi_ref))
4357 find_file(struct knfsd_fh *fh)
4359 struct nfs4_file *fp;
4360 unsigned int hashval = file_hashval(fh);
4363 fp = find_file_locked(fh, hashval);
4368 static struct nfs4_file *
4369 find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
4371 struct nfs4_file *fp;
4372 unsigned int hashval = file_hashval(fh);
4375 fp = find_file_locked(fh, hashval);
4380 spin_lock(&state_lock);
4381 fp = find_file_locked(fh, hashval);
4382 if (likely(fp == NULL)) {
4383 nfsd4_init_file(fh, hashval, new);
4386 spin_unlock(&state_lock);
4392 * Called to check deny when READ with all zero stateid or
4393 * WRITE with all zero or all one stateid
4396 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
4398 struct nfs4_file *fp;
4399 __be32 ret = nfs_ok;
4401 fp = find_file(¤t_fh->fh_handle);
4404 /* Check for conflicting share reservations */
4405 spin_lock(&fp->fi_lock);
4406 if (fp->fi_share_deny & deny_type)
4407 ret = nfserr_locked;
4408 spin_unlock(&fp->fi_lock);
4413 static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
4415 struct nfs4_delegation *dp = cb_to_delegation(cb);
4416 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
4419 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
4422 * We can't do this in nfsd_break_deleg_cb because it is
4423 * already holding inode->i_lock.
4425 * If the dl_time != 0, then we know that it has already been
4426 * queued for a lease break. Don't queue it again.
4428 spin_lock(&state_lock);
4429 if (dp->dl_time == 0) {
4430 dp->dl_time = get_seconds();
4431 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
4433 spin_unlock(&state_lock);
4436 static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
4437 struct rpc_task *task)
4439 struct nfs4_delegation *dp = cb_to_delegation(cb);
4441 if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID)
4444 switch (task->tk_status) {
4447 case -NFS4ERR_DELAY:
4448 rpc_delay(task, 2 * HZ);
4451 case -NFS4ERR_BAD_STATEID:
4453 * Race: client probably got cb_recall before open reply
4454 * granting delegation.
4456 if (dp->dl_retries--) {
4457 rpc_delay(task, 2 * HZ);
4466 static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
4468 struct nfs4_delegation *dp = cb_to_delegation(cb);
4470 nfs4_put_stid(&dp->dl_stid);
4473 static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
4474 .prepare = nfsd4_cb_recall_prepare,
4475 .done = nfsd4_cb_recall_done,
4476 .release = nfsd4_cb_recall_release,
4479 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
4482 * We're assuming the state code never drops its reference
4483 * without first removing the lease. Since we're in this lease
4484 * callback (and since the lease code is serialized by the
4485 * i_lock) we know the server hasn't removed the lease yet, and
4486 * we know it's safe to take a reference.
4488 refcount_inc(&dp->dl_stid.sc_count);
4489 nfsd4_run_cb(&dp->dl_recall);
4492 /* Called from break_lease() with i_lock held. */
4494 nfsd_break_deleg_cb(struct file_lock *fl)
4497 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
4498 struct nfs4_file *fp = dp->dl_stid.sc_file;
4501 * We don't want the locks code to timeout the lease for us;
4502 * we'll remove it ourself if a delegation isn't returned
4505 fl->fl_break_time = 0;
4507 spin_lock(&fp->fi_lock);
4508 fp->fi_had_conflict = true;
4509 nfsd_break_one_deleg(dp);
4510 spin_unlock(&fp->fi_lock);
4515 nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
4516 struct list_head *dispose)
4519 return lease_modify(onlist, arg, dispose);
4524 static const struct lock_manager_operations nfsd_lease_mng_ops = {
4525 .lm_break = nfsd_break_deleg_cb,
4526 .lm_change = nfsd_change_deleg_cb,
4529 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
4531 if (nfsd4_has_session(cstate))
4533 if (seqid == so->so_seqid - 1)
4534 return nfserr_replay_me;
4535 if (seqid == so->so_seqid)
4537 return nfserr_bad_seqid;
4540 static __be32 lookup_clientid(clientid_t *clid,
4541 struct nfsd4_compound_state *cstate,
4542 struct nfsd_net *nn,
4545 struct nfs4_client *found;
4548 found = cstate->clp;
4549 if (!same_clid(&found->cl_clientid, clid))
4550 return nfserr_stale_clientid;
4554 if (STALE_CLIENTID(clid, nn))
4555 return nfserr_stale_clientid;
4558 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
4559 * cached already then we know this is for is for v4.0 and "sessions"
4562 WARN_ON_ONCE(cstate->session);
4563 spin_lock(&nn->client_lock);
4564 found = find_confirmed_client(clid, sessions, nn);
4566 spin_unlock(&nn->client_lock);
4567 return nfserr_expired;
4569 atomic_inc(&found->cl_rpc_users);
4570 spin_unlock(&nn->client_lock);
4572 /* Cache the nfs4_client in cstate! */
4573 cstate->clp = found;
4578 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
4579 struct nfsd4_open *open, struct nfsd_net *nn)
4581 clientid_t *clientid = &open->op_clientid;
4582 struct nfs4_client *clp = NULL;
4583 unsigned int strhashval;
4584 struct nfs4_openowner *oo = NULL;
4587 if (STALE_CLIENTID(&open->op_clientid, nn))
4588 return nfserr_stale_clientid;
4590 * In case we need it later, after we've already created the
4591 * file and don't want to risk a further failure:
4593 open->op_file = nfsd4_alloc_file();
4594 if (open->op_file == NULL)
4595 return nfserr_jukebox;
4597 status = lookup_clientid(clientid, cstate, nn, false);
4602 strhashval = ownerstr_hashval(&open->op_owner);
4603 oo = find_openstateowner_str(strhashval, open, clp);
4604 open->op_openowner = oo;
4608 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4609 /* Replace unconfirmed owners without checking for replay. */
4610 release_openowner(oo);
4611 open->op_openowner = NULL;
4614 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
4619 oo = alloc_init_open_stateowner(strhashval, open, cstate);
4621 return nfserr_jukebox;
4622 open->op_openowner = oo;
4624 open->op_stp = nfs4_alloc_open_stateid(clp);
4626 return nfserr_jukebox;
4628 if (nfsd4_has_session(cstate) &&
4629 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
4630 open->op_odstate = alloc_clnt_odstate(clp);
4631 if (!open->op_odstate)
4632 return nfserr_jukebox;
4638 static inline __be32
4639 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
4641 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
4642 return nfserr_openmode;
4647 static int share_access_to_flags(u32 share_access)
4649 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
4652 static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
4654 struct nfs4_stid *ret;
4656 ret = find_stateid_by_type(cl, s,
4657 NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID);
4660 return delegstateid(ret);
4663 static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
4665 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
4666 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
4670 nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
4671 struct nfs4_delegation **dp)
4674 __be32 status = nfserr_bad_stateid;
4675 struct nfs4_delegation *deleg;
4677 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
4680 if (deleg->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) {
4681 nfs4_put_stid(&deleg->dl_stid);
4682 if (cl->cl_minorversion)
4683 status = nfserr_deleg_revoked;
4686 flags = share_access_to_flags(open->op_share_access);
4687 status = nfs4_check_delegmode(deleg, flags);
4689 nfs4_put_stid(&deleg->dl_stid);
4694 if (!nfsd4_is_deleg_cur(open))
4698 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
4702 static inline int nfs4_access_to_access(u32 nfs4_access)
4706 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
4707 flags |= NFSD_MAY_READ;
4708 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
4709 flags |= NFSD_MAY_WRITE;
4713 static inline __be32
4714 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
4715 struct nfsd4_open *open)
4717 struct iattr iattr = {
4718 .ia_valid = ATTR_SIZE,
4721 if (!open->op_truncate)
4723 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
4724 return nfserr_inval;
4725 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
4728 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
4729 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
4730 struct nfsd4_open *open)
4732 struct nfsd_file *nf = NULL;
4734 int oflag = nfs4_access_to_omode(open->op_share_access);
4735 int access = nfs4_access_to_access(open->op_share_access);
4736 unsigned char old_access_bmap, old_deny_bmap;
4738 spin_lock(&fp->fi_lock);
4741 * Are we trying to set a deny mode that would conflict with
4744 status = nfs4_file_check_deny(fp, open->op_share_deny);
4745 if (status != nfs_ok) {
4746 spin_unlock(&fp->fi_lock);
4750 /* set access to the file */
4751 status = nfs4_file_get_access(fp, open->op_share_access);
4752 if (status != nfs_ok) {
4753 spin_unlock(&fp->fi_lock);
4757 /* Set access bits in stateid */
4758 old_access_bmap = stp->st_access_bmap;
4759 set_access(open->op_share_access, stp);
4761 /* Set new deny mask */
4762 old_deny_bmap = stp->st_deny_bmap;
4763 set_deny(open->op_share_deny, stp);
4764 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4766 if (!fp->fi_fds[oflag]) {
4767 spin_unlock(&fp->fi_lock);
4768 status = nfsd_file_acquire(rqstp, cur_fh, access, &nf);
4770 goto out_put_access;
4771 spin_lock(&fp->fi_lock);
4772 if (!fp->fi_fds[oflag]) {
4773 fp->fi_fds[oflag] = nf;
4777 spin_unlock(&fp->fi_lock);
4781 status = nfsd4_truncate(rqstp, cur_fh, open);
4783 goto out_put_access;
4787 stp->st_access_bmap = old_access_bmap;
4788 nfs4_file_put_access(fp, open->op_share_access);
4789 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
4794 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)
4797 unsigned char old_deny_bmap = stp->st_deny_bmap;
4799 if (!test_access(open->op_share_access, stp))
4800 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
4802 /* test and set deny mode */
4803 spin_lock(&fp->fi_lock);
4804 status = nfs4_file_check_deny(fp, open->op_share_deny);
4805 if (status == nfs_ok) {
4806 set_deny(open->op_share_deny, stp);
4807 fp->fi_share_deny |=
4808 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4810 spin_unlock(&fp->fi_lock);
4812 if (status != nfs_ok)
4815 status = nfsd4_truncate(rqstp, cur_fh, open);
4816 if (status != nfs_ok)
4817 reset_union_bmap_deny(old_deny_bmap, stp);
4821 /* Should we give out recallable state?: */
4822 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
4824 if (clp->cl_cb_state == NFSD4_CB_UP)
4827 * In the sessions case, since we don't have to establish a
4828 * separate connection for callbacks, we assume it's OK
4829 * until we hear otherwise:
4831 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
4834 static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp,
4837 struct file_lock *fl;
4839 fl = locks_alloc_lock();
4842 fl->fl_lmops = &nfsd_lease_mng_ops;
4843 fl->fl_flags = FL_DELEG;
4844 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
4845 fl->fl_end = OFFSET_MAX;
4846 fl->fl_owner = (fl_owner_t)dp;
4847 fl->fl_pid = current->tgid;
4848 fl->fl_file = dp->dl_stid.sc_file->fi_deleg_file->nf_file;
4852 static struct nfs4_delegation *
4853 nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
4854 struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate)
4857 struct nfs4_delegation *dp;
4858 struct nfsd_file *nf;
4859 struct file_lock *fl;
4862 * The fi_had_conflict and nfs_get_existing_delegation checks
4863 * here are just optimizations; we'll need to recheck them at
4866 if (fp->fi_had_conflict)
4867 return ERR_PTR(-EAGAIN);
4869 nf = find_readable_file(fp);
4871 /* We should always have a readable file here */
4873 return ERR_PTR(-EBADF);
4875 spin_lock(&state_lock);
4876 spin_lock(&fp->fi_lock);
4877 if (nfs4_delegation_exists(clp, fp))
4879 else if (!fp->fi_deleg_file) {
4880 fp->fi_deleg_file = nf;
4881 /* increment early to prevent fi_deleg_file from being
4883 fp->fi_delegees = 1;
4887 spin_unlock(&fp->fi_lock);
4888 spin_unlock(&state_lock);
4892 return ERR_PTR(status);
4895 dp = alloc_init_deleg(clp, fp, fh, odstate);
4899 fl = nfs4_alloc_init_lease(dp, NFS4_OPEN_DELEGATE_READ);
4901 goto out_clnt_odstate;
4903 status = vfs_setlease(fp->fi_deleg_file->nf_file, fl->fl_type, &fl, NULL);
4905 locks_free_lock(fl);
4907 goto out_clnt_odstate;
4909 spin_lock(&state_lock);
4910 spin_lock(&fp->fi_lock);
4911 if (fp->fi_had_conflict)
4914 status = hash_delegation_locked(dp, fp);
4915 spin_unlock(&fp->fi_lock);
4916 spin_unlock(&state_lock);
4923 vfs_setlease(fp->fi_deleg_file->nf_file, F_UNLCK, NULL, (void **)&dp);
4925 put_clnt_odstate(dp->dl_clnt_odstate);
4926 nfs4_put_stid(&dp->dl_stid);
4929 return ERR_PTR(status);
4932 static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
4934 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4935 if (status == -EAGAIN)
4936 open->op_why_no_deleg = WND4_CONTENTION;
4938 open->op_why_no_deleg = WND4_RESOURCE;
4939 switch (open->op_deleg_want) {
4940 case NFS4_SHARE_WANT_READ_DELEG:
4941 case NFS4_SHARE_WANT_WRITE_DELEG:
4942 case NFS4_SHARE_WANT_ANY_DELEG:
4944 case NFS4_SHARE_WANT_CANCEL:
4945 open->op_why_no_deleg = WND4_CANCELLED;
4947 case NFS4_SHARE_WANT_NO_DELEG:
4954 * Attempt to hand out a delegation.
4956 * Note we don't support write delegations, and won't until the vfs has
4957 * proper support for them.
4960 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
4961 struct nfs4_ol_stateid *stp)
4963 struct nfs4_delegation *dp;
4964 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
4965 struct nfs4_client *clp = stp->st_stid.sc_client;
4969 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
4970 open->op_recall = 0;
4971 switch (open->op_claim_type) {
4972 case NFS4_OPEN_CLAIM_PREVIOUS:
4974 open->op_recall = 1;
4975 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
4978 case NFS4_OPEN_CLAIM_NULL:
4979 case NFS4_OPEN_CLAIM_FH:
4981 * Let's not give out any delegations till everyone's
4982 * had the chance to reclaim theirs, *and* until
4983 * NLM locks have all been reclaimed:
4985 if (locks_in_grace(clp->net))
4987 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
4990 * Also, if the file was opened for write or
4991 * create, there's a good chance the client's
4992 * about to write to it, resulting in an
4993 * immediate recall (since we don't support
4994 * write delegations):
4996 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
4998 if (open->op_create == NFS4_OPEN_CREATE)
5004 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate);
5008 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
5010 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
5011 STATEID_VAL(&dp->dl_stid.sc_stateid));
5012 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
5013 nfs4_put_stid(&dp->dl_stid);
5016 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
5017 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
5018 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
5019 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
5020 open->op_recall = 1;
5023 /* 4.1 client asking for a delegation? */
5024 if (open->op_deleg_want)
5025 nfsd4_open_deleg_none_ext(open, status);
5029 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
5030 struct nfs4_delegation *dp)
5032 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
5033 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
5034 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5035 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
5036 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
5037 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
5038 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5039 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
5041 /* Otherwise the client must be confused wanting a delegation
5042 * it already has, therefore we don't return
5043 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
5048 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
5050 struct nfsd4_compoundres *resp = rqstp->rq_resp;
5051 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
5052 struct nfs4_file *fp = NULL;
5053 struct nfs4_ol_stateid *stp = NULL;
5054 struct nfs4_delegation *dp = NULL;
5056 bool new_stp = false;
5059 * Lookup file; if found, lookup stateid and check open request,
5060 * and check for delegations in the process of being recalled.
5061 * If not found, create the nfs4_file struct
5063 fp = find_or_add_file(open->op_file, ¤t_fh->fh_handle);
5064 if (fp != open->op_file) {
5065 status = nfs4_check_deleg(cl, open, &dp);
5068 stp = nfsd4_find_and_lock_existing_open(fp, open);
5070 open->op_file = NULL;
5071 status = nfserr_bad_stateid;
5072 if (nfsd4_is_deleg_cur(open))
5077 stp = init_open_stateid(fp, open);
5083 * OPEN the file, or upgrade an existing OPEN.
5084 * If truncate fails, the OPEN fails.
5086 * stp is already locked.
5089 /* Stateid was found, this is an OPEN upgrade */
5090 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
5092 mutex_unlock(&stp->st_mutex);
5096 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
5098 stp->st_stid.sc_type = NFS4_CLOSED_STID;
5099 release_open_stateid(stp);
5100 mutex_unlock(&stp->st_mutex);
5104 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
5106 if (stp->st_clnt_odstate == open->op_odstate)
5107 open->op_odstate = NULL;
5110 nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
5111 mutex_unlock(&stp->st_mutex);
5113 if (nfsd4_has_session(&resp->cstate)) {
5114 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
5115 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5116 open->op_why_no_deleg = WND4_NOT_WANTED;
5122 * Attempt to hand out a delegation. No error return, because the
5123 * OPEN succeeds even if we fail.
5125 nfs4_open_delegation(current_fh, open, stp);
5129 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
5130 STATEID_VAL(&stp->st_stid.sc_stateid));
5132 /* 4.1 client trying to upgrade/downgrade delegation? */
5133 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
5134 open->op_deleg_want)
5135 nfsd4_deleg_xgrade_none_ext(open, dp);
5139 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
5140 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
5142 * To finish the open response, we just need to set the rflags.
5144 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
5145 if (nfsd4_has_session(&resp->cstate))
5146 open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK;
5147 else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED))
5148 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
5151 nfs4_put_stid(&dp->dl_stid);
5153 nfs4_put_stid(&stp->st_stid);
5158 void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
5159 struct nfsd4_open *open)
5161 if (open->op_openowner) {
5162 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
5164 nfsd4_cstate_assign_replay(cstate, so);
5165 nfs4_put_stateowner(so);
5168 kmem_cache_free(file_slab, open->op_file);
5170 nfs4_put_stid(&open->op_stp->st_stid);
5171 if (open->op_odstate)
5172 kmem_cache_free(odstate_slab, open->op_odstate);
5176 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5177 union nfsd4_op_u *u)
5179 clientid_t *clid = &u->renew;
5180 struct nfs4_client *clp;
5182 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5184 dprintk("process_renew(%08x/%08x): starting\n",
5185 clid->cl_boot, clid->cl_id);
5186 status = lookup_clientid(clid, cstate, nn, false);
5190 status = nfserr_cb_path_down;
5191 if (!list_empty(&clp->cl_delegations)
5192 && clp->cl_cb_state != NFSD4_CB_UP)
5200 nfsd4_end_grace(struct nfsd_net *nn)
5202 /* do nothing if grace period already ended */
5203 if (nn->grace_ended)
5206 nn->grace_ended = true;
5208 * If the server goes down again right now, an NFSv4
5209 * client will still be allowed to reclaim after it comes back up,
5210 * even if it hasn't yet had a chance to reclaim state this time.
5213 nfsd4_record_grace_done(nn);
5215 * At this point, NFSv4 clients can still reclaim. But if the
5216 * server crashes, any that have not yet reclaimed will be out
5217 * of luck on the next boot.
5219 * (NFSv4.1+ clients are considered to have reclaimed once they
5220 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
5221 * have reclaimed after their first OPEN.)
5223 locks_end_grace(&nn->nfsd4_manager);
5225 * At this point, and once lockd and/or any other containers
5226 * exit their grace period, further reclaims will fail and
5227 * regular locking can resume.
5232 * If we've waited a lease period but there are still clients trying to
5233 * reclaim, wait a little longer to give them a chance to finish.
5235 static bool clients_still_reclaiming(struct nfsd_net *nn)
5237 unsigned long now = get_seconds();
5238 unsigned long double_grace_period_end = nn->boot_time +
5239 2 * nn->nfsd4_lease;
5241 if (nn->track_reclaim_completes &&
5242 atomic_read(&nn->nr_reclaim_complete) ==
5243 nn->reclaim_str_hashtbl_size)
5245 if (!nn->somebody_reclaimed)
5247 nn->somebody_reclaimed = false;
5249 * If we've given them *two* lease times to reclaim, and they're
5250 * still not done, give up:
5252 if (time_after(now, double_grace_period_end))
5258 nfs4_laundromat(struct nfsd_net *nn)
5260 struct nfs4_client *clp;
5261 struct nfs4_openowner *oo;
5262 struct nfs4_delegation *dp;
5263 struct nfs4_ol_stateid *stp;
5264 struct nfsd4_blocked_lock *nbl;
5265 struct list_head *pos, *next, reaplist;
5266 time_t cutoff = get_seconds() - nn->nfsd4_lease;
5267 time_t t, new_timeo = nn->nfsd4_lease;
5268 struct nfs4_cpntf_state *cps;
5269 copy_stateid_t *cps_t;
5272 dprintk("NFSD: laundromat service - starting\n");
5274 if (clients_still_reclaiming(nn)) {
5278 dprintk("NFSD: end of grace period\n");
5279 nfsd4_end_grace(nn);
5280 INIT_LIST_HEAD(&reaplist);
5282 spin_lock(&nn->s2s_cp_lock);
5283 idr_for_each_entry(&nn->s2s_cp_stateids, cps_t, i) {
5284 cps = container_of(cps_t, struct nfs4_cpntf_state, cp_stateid);
5285 if (cps->cp_stateid.sc_type == NFS4_COPYNOTIFY_STID &&
5286 !time_after((unsigned long)cps->cpntf_time,
5287 (unsigned long)cutoff))
5288 _free_cpntf_state_locked(nn, cps);
5290 spin_unlock(&nn->s2s_cp_lock);
5292 spin_lock(&nn->client_lock);
5293 list_for_each_safe(pos, next, &nn->client_lru) {
5294 clp = list_entry(pos, struct nfs4_client, cl_lru);
5295 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
5296 t = clp->cl_time - cutoff;
5297 new_timeo = min(new_timeo, t);
5300 if (mark_client_expired_locked(clp)) {
5301 dprintk("NFSD: client in use (clientid %08x)\n",
5302 clp->cl_clientid.cl_id);
5305 list_add(&clp->cl_lru, &reaplist);
5307 spin_unlock(&nn->client_lock);
5308 list_for_each_safe(pos, next, &reaplist) {
5309 clp = list_entry(pos, struct nfs4_client, cl_lru);
5310 dprintk("NFSD: purging unused client (clientid %08x)\n",
5311 clp->cl_clientid.cl_id);
5312 list_del_init(&clp->cl_lru);
5315 spin_lock(&state_lock);
5316 list_for_each_safe(pos, next, &nn->del_recall_lru) {
5317 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
5318 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
5319 t = dp->dl_time - cutoff;
5320 new_timeo = min(new_timeo, t);
5323 WARN_ON(!unhash_delegation_locked(dp));
5324 list_add(&dp->dl_recall_lru, &reaplist);
5326 spin_unlock(&state_lock);
5327 while (!list_empty(&reaplist)) {
5328 dp = list_first_entry(&reaplist, struct nfs4_delegation,
5330 list_del_init(&dp->dl_recall_lru);
5331 revoke_delegation(dp);
5334 spin_lock(&nn->client_lock);
5335 while (!list_empty(&nn->close_lru)) {
5336 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
5338 if (time_after((unsigned long)oo->oo_time,
5339 (unsigned long)cutoff)) {
5340 t = oo->oo_time - cutoff;
5341 new_timeo = min(new_timeo, t);
5344 list_del_init(&oo->oo_close_lru);
5345 stp = oo->oo_last_closed_stid;
5346 oo->oo_last_closed_stid = NULL;
5347 spin_unlock(&nn->client_lock);
5348 nfs4_put_stid(&stp->st_stid);
5349 spin_lock(&nn->client_lock);
5351 spin_unlock(&nn->client_lock);
5354 * It's possible for a client to try and acquire an already held lock
5355 * that is being held for a long time, and then lose interest in it.
5356 * So, we clean out any un-revisited request after a lease period
5357 * under the assumption that the client is no longer interested.
5359 * RFC5661, sec. 9.6 states that the client must not rely on getting
5360 * notifications and must continue to poll for locks, even when the
5361 * server supports them. Thus this shouldn't lead to clients blocking
5362 * indefinitely once the lock does become free.
5364 BUG_ON(!list_empty(&reaplist));
5365 spin_lock(&nn->blocked_locks_lock);
5366 while (!list_empty(&nn->blocked_locks_lru)) {
5367 nbl = list_first_entry(&nn->blocked_locks_lru,
5368 struct nfsd4_blocked_lock, nbl_lru);
5369 if (time_after((unsigned long)nbl->nbl_time,
5370 (unsigned long)cutoff)) {
5371 t = nbl->nbl_time - cutoff;
5372 new_timeo = min(new_timeo, t);
5375 list_move(&nbl->nbl_lru, &reaplist);
5376 list_del_init(&nbl->nbl_list);
5378 spin_unlock(&nn->blocked_locks_lock);
5380 while (!list_empty(&reaplist)) {
5381 nbl = list_first_entry(&reaplist,
5382 struct nfsd4_blocked_lock, nbl_lru);
5383 list_del_init(&nbl->nbl_lru);
5384 free_blocked_lock(nbl);
5387 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
5391 static struct workqueue_struct *laundry_wq;
5392 static void laundromat_main(struct work_struct *);
5395 laundromat_main(struct work_struct *laundry)
5398 struct delayed_work *dwork = to_delayed_work(laundry);
5399 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
5402 t = nfs4_laundromat(nn);
5403 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
5404 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
5407 static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
5409 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
5410 return nfserr_bad_stateid;
5415 access_permit_read(struct nfs4_ol_stateid *stp)
5417 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
5418 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
5419 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
5423 access_permit_write(struct nfs4_ol_stateid *stp)
5425 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
5426 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
5430 __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
5432 __be32 status = nfserr_openmode;
5434 /* For lock stateid's, we test the parent open, not the lock: */
5435 if (stp->st_openstp)
5436 stp = stp->st_openstp;
5437 if ((flags & WR_STATE) && !access_permit_write(stp))
5439 if ((flags & RD_STATE) && !access_permit_read(stp))
5446 static inline __be32
5447 check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
5449 if (ONE_STATEID(stateid) && (flags & RD_STATE))
5451 else if (opens_in_grace(net)) {
5452 /* Answer in remaining cases depends on existence of
5453 * conflicting state; so we must wait out the grace period. */
5454 return nfserr_grace;
5455 } else if (flags & WR_STATE)
5456 return nfs4_share_conflict(current_fh,
5457 NFS4_SHARE_DENY_WRITE);
5458 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
5459 return nfs4_share_conflict(current_fh,
5460 NFS4_SHARE_DENY_READ);
5464 * Allow READ/WRITE during grace period on recovered state only for files
5465 * that are not able to provide mandatory locking.
5468 grace_disallows_io(struct net *net, struct inode *inode)
5470 return opens_in_grace(net) && mandatory_lock(inode);
5473 static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
5476 * When sessions are used the stateid generation number is ignored
5479 if (has_session && in->si_generation == 0)
5482 if (in->si_generation == ref->si_generation)
5485 /* If the client sends us a stateid from the future, it's buggy: */
5486 if (nfsd4_stateid_generation_after(in, ref))
5487 return nfserr_bad_stateid;
5489 * However, we could see a stateid from the past, even from a
5490 * non-buggy client. For example, if the client sends a lock
5491 * while some IO is outstanding, the lock may bump si_generation
5492 * while the IO is still in flight. The client could avoid that
5493 * situation by waiting for responses on all the IO requests,
5494 * but better performance may result in retrying IO that
5495 * receives an old_stateid error if requests are rarely
5496 * reordered in flight:
5498 return nfserr_old_stateid;
5501 static __be32 nfsd4_stid_check_stateid_generation(stateid_t *in, struct nfs4_stid *s, bool has_session)
5505 spin_lock(&s->sc_lock);
5506 ret = nfsd4_verify_open_stid(s);
5508 ret = check_stateid_generation(in, &s->sc_stateid, has_session);
5509 spin_unlock(&s->sc_lock);
5513 static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
5515 if (ols->st_stateowner->so_is_open_owner &&
5516 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
5517 return nfserr_bad_stateid;
5521 static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
5523 struct nfs4_stid *s;
5524 __be32 status = nfserr_bad_stateid;
5526 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5527 CLOSE_STATEID(stateid))
5529 /* Client debugging aid. */
5530 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
5531 char addr_str[INET6_ADDRSTRLEN];
5532 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
5534 pr_warn_ratelimited("NFSD: client %s testing state ID "
5535 "with incorrect client ID\n", addr_str);
5538 spin_lock(&cl->cl_lock);
5539 s = find_stateid_locked(cl, stateid);
5542 status = nfsd4_stid_check_stateid_generation(stateid, s, 1);
5545 switch (s->sc_type) {
5546 case NFS4_DELEG_STID:
5549 case NFS4_REVOKED_DELEG_STID:
5550 status = nfserr_deleg_revoked;
5552 case NFS4_OPEN_STID:
5553 case NFS4_LOCK_STID:
5554 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
5557 printk("unknown stateid type %x\n", s->sc_type);
5559 case NFS4_CLOSED_STID:
5560 case NFS4_CLOSED_DELEG_STID:
5561 status = nfserr_bad_stateid;
5564 spin_unlock(&cl->cl_lock);
5569 nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
5570 stateid_t *stateid, unsigned char typemask,
5571 struct nfs4_stid **s, struct nfsd_net *nn)
5574 bool return_revoked = false;
5577 * only return revoked delegations if explicitly asked.
5578 * otherwise we report revoked or bad_stateid status.
5580 if (typemask & NFS4_REVOKED_DELEG_STID)
5581 return_revoked = true;
5582 else if (typemask & NFS4_DELEG_STID)
5583 typemask |= NFS4_REVOKED_DELEG_STID;
5585 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5586 CLOSE_STATEID(stateid))
5587 return nfserr_bad_stateid;
5588 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn,
5590 if (status == nfserr_stale_clientid) {
5591 if (cstate->session)
5592 return nfserr_bad_stateid;
5593 return nfserr_stale_stateid;
5597 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
5599 return nfserr_bad_stateid;
5600 if (((*s)->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) {
5602 if (cstate->minorversion)
5603 return nfserr_deleg_revoked;
5604 return nfserr_bad_stateid;
5609 static struct nfsd_file *
5610 nfs4_find_file(struct nfs4_stid *s, int flags)
5615 switch (s->sc_type) {
5616 case NFS4_DELEG_STID:
5617 if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
5619 return nfsd_file_get(s->sc_file->fi_deleg_file);
5620 case NFS4_OPEN_STID:
5621 case NFS4_LOCK_STID:
5622 if (flags & RD_STATE)
5623 return find_readable_file(s->sc_file);
5625 return find_writeable_file(s->sc_file);
5633 nfs4_check_olstateid(struct nfs4_ol_stateid *ols, int flags)
5637 status = nfsd4_check_openowner_confirmed(ols);
5640 return nfs4_check_openmode(ols, flags);
5644 nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
5645 struct nfsd_file **nfp, int flags)
5647 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
5648 struct nfsd_file *nf;
5651 nf = nfs4_find_file(s, flags);
5653 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
5654 acc | NFSD_MAY_OWNER_OVERRIDE);
5660 status = nfsd_file_acquire(rqstp, fhp, acc, &nf);
5669 _free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
5671 WARN_ON_ONCE(cps->cp_stateid.sc_type != NFS4_COPYNOTIFY_STID);
5672 if (!refcount_dec_and_test(&cps->cp_stateid.sc_count))
5674 list_del(&cps->cp_list);
5675 idr_remove(&nn->s2s_cp_stateids,
5676 cps->cp_stateid.stid.si_opaque.so_id);
5680 * A READ from an inter server to server COPY will have a
5681 * copy stateid. Look up the copy notify stateid from the
5682 * idr structure and take a reference on it.
5684 __be32 manage_cpntf_state(struct nfsd_net *nn, stateid_t *st,
5685 struct nfs4_client *clp,
5686 struct nfs4_cpntf_state **cps)
5688 copy_stateid_t *cps_t;
5689 struct nfs4_cpntf_state *state = NULL;
5691 if (st->si_opaque.so_clid.cl_id != nn->s2s_cp_cl_id)
5692 return nfserr_bad_stateid;
5693 spin_lock(&nn->s2s_cp_lock);
5694 cps_t = idr_find(&nn->s2s_cp_stateids, st->si_opaque.so_id);
5696 state = container_of(cps_t, struct nfs4_cpntf_state,
5698 if (state->cp_stateid.sc_type != NFS4_COPYNOTIFY_STID) {
5703 refcount_inc(&state->cp_stateid.sc_count);
5705 _free_cpntf_state_locked(nn, state);
5708 spin_unlock(&nn->s2s_cp_lock);
5710 return nfserr_bad_stateid;
5716 static __be32 find_cpntf_state(struct nfsd_net *nn, stateid_t *st,
5717 struct nfs4_stid **stid)
5720 struct nfs4_cpntf_state *cps = NULL;
5721 struct nfsd4_compound_state cstate;
5723 status = manage_cpntf_state(nn, st, NULL, &cps);
5727 cps->cpntf_time = get_seconds();
5728 memset(&cstate, 0, sizeof(cstate));
5729 status = lookup_clientid(&cps->cp_p_clid, &cstate, nn, true);
5732 status = nfsd4_lookup_stateid(&cstate, &cps->cp_p_stateid,
5733 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
5735 put_client_renew(cstate.clp);
5737 nfs4_put_cpntf_state(nn, cps);
5741 void nfs4_put_cpntf_state(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
5743 spin_lock(&nn->s2s_cp_lock);
5744 _free_cpntf_state_locked(nn, cps);
5745 spin_unlock(&nn->s2s_cp_lock);
5749 * Checks for stateid operations
5752 nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
5753 struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
5754 stateid_t *stateid, int flags, struct nfsd_file **nfp,
5755 struct nfs4_stid **cstid)
5757 struct inode *ino = d_inode(fhp->fh_dentry);
5758 struct net *net = SVC_NET(rqstp);
5759 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5760 struct nfs4_stid *s = NULL;
5766 if (grace_disallows_io(net, ino))
5767 return nfserr_grace;
5769 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
5770 status = check_special_stateids(net, fhp, stateid, flags);
5774 status = nfsd4_lookup_stateid(cstate, stateid,
5775 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
5777 if (status == nfserr_bad_stateid)
5778 status = find_cpntf_state(nn, stateid, &s);
5781 status = nfsd4_stid_check_stateid_generation(stateid, s,
5782 nfsd4_has_session(cstate));
5786 switch (s->sc_type) {
5787 case NFS4_DELEG_STID:
5788 status = nfs4_check_delegmode(delegstateid(s), flags);
5790 case NFS4_OPEN_STID:
5791 case NFS4_LOCK_STID:
5792 status = nfs4_check_olstateid(openlockstateid(s), flags);
5795 status = nfserr_bad_stateid;
5800 status = nfs4_check_fh(fhp, s);
5803 if (status == nfs_ok && nfp)
5804 status = nfs4_check_file(rqstp, fhp, s, nfp, flags);
5807 if (!status && cstid)
5816 * Test if the stateid is valid
5819 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5820 union nfsd4_op_u *u)
5822 struct nfsd4_test_stateid *test_stateid = &u->test_stateid;
5823 struct nfsd4_test_stateid_id *stateid;
5824 struct nfs4_client *cl = cstate->session->se_client;
5826 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
5827 stateid->ts_id_status =
5828 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
5834 nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
5836 struct nfs4_ol_stateid *stp = openlockstateid(s);
5839 ret = nfsd4_lock_ol_stateid(stp);
5843 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5847 ret = nfserr_locks_held;
5848 if (check_for_locks(stp->st_stid.sc_file,
5849 lockowner(stp->st_stateowner)))
5852 release_lock_stateid(stp);
5856 mutex_unlock(&stp->st_mutex);
5863 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5864 union nfsd4_op_u *u)
5866 struct nfsd4_free_stateid *free_stateid = &u->free_stateid;
5867 stateid_t *stateid = &free_stateid->fr_stateid;
5868 struct nfs4_stid *s;
5869 struct nfs4_delegation *dp;
5870 struct nfs4_client *cl = cstate->session->se_client;
5871 __be32 ret = nfserr_bad_stateid;
5873 spin_lock(&cl->cl_lock);
5874 s = find_stateid_locked(cl, stateid);
5877 spin_lock(&s->sc_lock);
5878 switch (s->sc_type) {
5879 case NFS4_DELEG_STID:
5880 ret = nfserr_locks_held;
5882 case NFS4_OPEN_STID:
5883 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5886 ret = nfserr_locks_held;
5888 case NFS4_LOCK_STID:
5889 spin_unlock(&s->sc_lock);
5890 refcount_inc(&s->sc_count);
5891 spin_unlock(&cl->cl_lock);
5892 ret = nfsd4_free_lock_stateid(stateid, s);
5894 case NFS4_REVOKED_DELEG_STID:
5895 spin_unlock(&s->sc_lock);
5896 dp = delegstateid(s);
5897 list_del_init(&dp->dl_recall_lru);
5898 spin_unlock(&cl->cl_lock);
5902 /* Default falls through and returns nfserr_bad_stateid */
5904 spin_unlock(&s->sc_lock);
5906 spin_unlock(&cl->cl_lock);
5914 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
5915 RD_STATE : WR_STATE;
5918 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
5920 struct svc_fh *current_fh = &cstate->current_fh;
5921 struct nfs4_stateowner *sop = stp->st_stateowner;
5924 status = nfsd4_check_seqid(cstate, sop, seqid);
5927 status = nfsd4_lock_ol_stateid(stp);
5928 if (status != nfs_ok)
5930 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
5931 if (status == nfs_ok)
5932 status = nfs4_check_fh(current_fh, &stp->st_stid);
5933 if (status != nfs_ok)
5934 mutex_unlock(&stp->st_mutex);
5939 * Checks for sequence id mutating operations.
5942 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
5943 stateid_t *stateid, char typemask,
5944 struct nfs4_ol_stateid **stpp,
5945 struct nfsd_net *nn)
5948 struct nfs4_stid *s;
5949 struct nfs4_ol_stateid *stp = NULL;
5951 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
5952 seqid, STATEID_VAL(stateid));
5955 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
5958 stp = openlockstateid(s);
5959 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
5961 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
5965 nfs4_put_stid(&stp->st_stid);
5969 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
5970 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
5973 struct nfs4_openowner *oo;
5974 struct nfs4_ol_stateid *stp;
5976 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
5977 NFS4_OPEN_STID, &stp, nn);
5980 oo = openowner(stp->st_stateowner);
5981 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
5982 mutex_unlock(&stp->st_mutex);
5983 nfs4_put_stid(&stp->st_stid);
5984 return nfserr_bad_stateid;
5991 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5992 union nfsd4_op_u *u)
5994 struct nfsd4_open_confirm *oc = &u->open_confirm;
5996 struct nfs4_openowner *oo;
5997 struct nfs4_ol_stateid *stp;
5998 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6000 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
6001 cstate->current_fh.fh_dentry);
6003 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
6007 status = nfs4_preprocess_seqid_op(cstate,
6008 oc->oc_seqid, &oc->oc_req_stateid,
6009 NFS4_OPEN_STID, &stp, nn);
6012 oo = openowner(stp->st_stateowner);
6013 status = nfserr_bad_stateid;
6014 if (oo->oo_flags & NFS4_OO_CONFIRMED) {
6015 mutex_unlock(&stp->st_mutex);
6018 oo->oo_flags |= NFS4_OO_CONFIRMED;
6019 nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
6020 mutex_unlock(&stp->st_mutex);
6021 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
6022 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
6024 nfsd4_client_record_create(oo->oo_owner.so_client);
6027 nfs4_put_stid(&stp->st_stid);
6029 nfsd4_bump_seqid(cstate, status);
6033 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
6035 if (!test_access(access, stp))
6037 nfs4_file_put_access(stp->st_stid.sc_file, access);
6038 clear_access(access, stp);
6041 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
6043 switch (to_access) {
6044 case NFS4_SHARE_ACCESS_READ:
6045 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
6046 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
6048 case NFS4_SHARE_ACCESS_WRITE:
6049 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
6050 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
6052 case NFS4_SHARE_ACCESS_BOTH:
6060 nfsd4_open_downgrade(struct svc_rqst *rqstp,
6061 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
6063 struct nfsd4_open_downgrade *od = &u->open_downgrade;
6065 struct nfs4_ol_stateid *stp;
6066 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6068 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
6069 cstate->current_fh.fh_dentry);
6071 /* We don't yet support WANT bits: */
6072 if (od->od_deleg_want)
6073 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
6076 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
6077 &od->od_stateid, &stp, nn);
6080 status = nfserr_inval;
6081 if (!test_access(od->od_share_access, stp)) {
6082 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
6083 stp->st_access_bmap, od->od_share_access);
6086 if (!test_deny(od->od_share_deny, stp)) {
6087 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
6088 stp->st_deny_bmap, od->od_share_deny);
6091 nfs4_stateid_downgrade(stp, od->od_share_access);
6092 reset_union_bmap_deny(od->od_share_deny, stp);
6093 nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
6096 mutex_unlock(&stp->st_mutex);
6097 nfs4_put_stid(&stp->st_stid);
6099 nfsd4_bump_seqid(cstate, status);
6103 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
6105 struct nfs4_client *clp = s->st_stid.sc_client;
6107 LIST_HEAD(reaplist);
6109 spin_lock(&clp->cl_lock);
6110 unhashed = unhash_open_stateid(s, &reaplist);
6112 if (clp->cl_minorversion) {
6114 put_ol_stateid_locked(s, &reaplist);
6115 spin_unlock(&clp->cl_lock);
6116 free_ol_stateid_reaplist(&reaplist);
6118 spin_unlock(&clp->cl_lock);
6119 free_ol_stateid_reaplist(&reaplist);
6121 move_to_close_lru(s, clp->net);
6126 * nfs4_unlock_state() called after encode
6129 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6130 union nfsd4_op_u *u)
6132 struct nfsd4_close *close = &u->close;
6134 struct nfs4_ol_stateid *stp;
6135 struct net *net = SVC_NET(rqstp);
6136 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6138 dprintk("NFSD: nfsd4_close on file %pd\n",
6139 cstate->current_fh.fh_dentry);
6141 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
6143 NFS4_OPEN_STID|NFS4_CLOSED_STID,
6145 nfsd4_bump_seqid(cstate, status);
6149 stp->st_stid.sc_type = NFS4_CLOSED_STID;
6152 * Technically we don't _really_ have to increment or copy it, since
6153 * it should just be gone after this operation and we clobber the
6154 * copied value below, but we continue to do so here just to ensure
6155 * that racing ops see that there was a state change.
6157 nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
6159 nfsd4_close_open_stateid(stp);
6160 mutex_unlock(&stp->st_mutex);
6162 /* v4.1+ suggests that we send a special stateid in here, since the
6163 * clients should just ignore this anyway. Since this is not useful
6164 * for v4.0 clients either, we set it to the special close_stateid
6167 * See RFC5661 section 18.2.4, and RFC7530 section 16.2.5
6169 memcpy(&close->cl_stateid, &close_stateid, sizeof(close->cl_stateid));
6171 /* put reference from nfs4_preprocess_seqid_op */
6172 nfs4_put_stid(&stp->st_stid);
6178 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6179 union nfsd4_op_u *u)
6181 struct nfsd4_delegreturn *dr = &u->delegreturn;
6182 struct nfs4_delegation *dp;
6183 stateid_t *stateid = &dr->dr_stateid;
6184 struct nfs4_stid *s;
6186 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6188 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
6191 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
6194 dp = delegstateid(s);
6195 status = nfsd4_stid_check_stateid_generation(stateid, &dp->dl_stid, nfsd4_has_session(cstate));
6199 destroy_delegation(dp);
6201 nfs4_put_stid(&dp->dl_stid);
6207 end_offset(u64 start, u64 len)
6212 return end >= start ? end: NFS4_MAX_UINT64;
6215 /* last octet in a range */
6217 last_byte_offset(u64 start, u64 len)
6223 return end > start ? end - 1: NFS4_MAX_UINT64;
6227 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
6228 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
6229 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
6230 * locking, this prevents us from being completely protocol-compliant. The
6231 * real solution to this problem is to start using unsigned file offsets in
6232 * the VFS, but this is a very deep change!
6235 nfs4_transform_lock_offset(struct file_lock *lock)
6237 if (lock->fl_start < 0)
6238 lock->fl_start = OFFSET_MAX;
6239 if (lock->fl_end < 0)
6240 lock->fl_end = OFFSET_MAX;
6244 nfsd4_fl_get_owner(fl_owner_t owner)
6246 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
6248 nfs4_get_stateowner(&lo->lo_owner);
6253 nfsd4_fl_put_owner(fl_owner_t owner)
6255 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
6258 nfs4_put_stateowner(&lo->lo_owner);
6262 nfsd4_lm_notify(struct file_lock *fl)
6264 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
6265 struct net *net = lo->lo_owner.so_client->net;
6266 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6267 struct nfsd4_blocked_lock *nbl = container_of(fl,
6268 struct nfsd4_blocked_lock, nbl_lock);
6271 /* An empty list means that something else is going to be using it */
6272 spin_lock(&nn->blocked_locks_lock);
6273 if (!list_empty(&nbl->nbl_list)) {
6274 list_del_init(&nbl->nbl_list);
6275 list_del_init(&nbl->nbl_lru);
6278 spin_unlock(&nn->blocked_locks_lock);
6281 nfsd4_run_cb(&nbl->nbl_cb);
6284 static const struct lock_manager_operations nfsd_posix_mng_ops = {
6285 .lm_notify = nfsd4_lm_notify,
6286 .lm_get_owner = nfsd4_fl_get_owner,
6287 .lm_put_owner = nfsd4_fl_put_owner,
6291 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
6293 struct nfs4_lockowner *lo;
6295 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
6296 lo = (struct nfs4_lockowner *) fl->fl_owner;
6297 xdr_netobj_dup(&deny->ld_owner, &lo->lo_owner.so_owner,
6299 if (!deny->ld_owner.data)
6300 /* We just don't care that much */
6302 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
6305 deny->ld_owner.len = 0;
6306 deny->ld_owner.data = NULL;
6307 deny->ld_clientid.cl_boot = 0;
6308 deny->ld_clientid.cl_id = 0;
6310 deny->ld_start = fl->fl_start;
6311 deny->ld_length = NFS4_MAX_UINT64;
6312 if (fl->fl_end != NFS4_MAX_UINT64)
6313 deny->ld_length = fl->fl_end - fl->fl_start + 1;
6314 deny->ld_type = NFS4_READ_LT;
6315 if (fl->fl_type != F_RDLCK)
6316 deny->ld_type = NFS4_WRITE_LT;
6319 static struct nfs4_lockowner *
6320 find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
6322 unsigned int strhashval = ownerstr_hashval(owner);
6323 struct nfs4_stateowner *so;
6325 lockdep_assert_held(&clp->cl_lock);
6327 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
6329 if (so->so_is_open_owner)
6331 if (same_owner_str(so, owner))
6332 return lockowner(nfs4_get_stateowner(so));
6337 static struct nfs4_lockowner *
6338 find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
6340 struct nfs4_lockowner *lo;
6342 spin_lock(&clp->cl_lock);
6343 lo = find_lockowner_str_locked(clp, owner);
6344 spin_unlock(&clp->cl_lock);
6348 static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
6350 unhash_lockowner_locked(lockowner(sop));
6353 static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
6355 struct nfs4_lockowner *lo = lockowner(sop);
6357 kmem_cache_free(lockowner_slab, lo);
6360 static const struct nfs4_stateowner_operations lockowner_ops = {
6361 .so_unhash = nfs4_unhash_lockowner,
6362 .so_free = nfs4_free_lockowner,
6366 * Alloc a lock owner structure.
6367 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
6370 * strhashval = ownerstr_hashval
6372 static struct nfs4_lockowner *
6373 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
6374 struct nfs4_ol_stateid *open_stp,
6375 struct nfsd4_lock *lock)
6377 struct nfs4_lockowner *lo, *ret;
6379 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
6382 INIT_LIST_HEAD(&lo->lo_blocked);
6383 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
6384 lo->lo_owner.so_is_open_owner = 0;
6385 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
6386 lo->lo_owner.so_ops = &lockowner_ops;
6387 spin_lock(&clp->cl_lock);
6388 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
6390 list_add(&lo->lo_owner.so_strhash,
6391 &clp->cl_ownerstr_hashtbl[strhashval]);
6394 nfs4_free_stateowner(&lo->lo_owner);
6396 spin_unlock(&clp->cl_lock);
6400 static struct nfs4_ol_stateid *
6401 find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
6403 struct nfs4_ol_stateid *lst;
6404 struct nfs4_client *clp = lo->lo_owner.so_client;
6406 lockdep_assert_held(&clp->cl_lock);
6408 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
6409 if (lst->st_stid.sc_type != NFS4_LOCK_STID)
6411 if (lst->st_stid.sc_file == fp) {
6412 refcount_inc(&lst->st_stid.sc_count);
6419 static struct nfs4_ol_stateid *
6420 init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
6421 struct nfs4_file *fp, struct inode *inode,
6422 struct nfs4_ol_stateid *open_stp)
6424 struct nfs4_client *clp = lo->lo_owner.so_client;
6425 struct nfs4_ol_stateid *retstp;
6427 mutex_init(&stp->st_mutex);
6428 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
6430 spin_lock(&clp->cl_lock);
6431 spin_lock(&fp->fi_lock);
6432 retstp = find_lock_stateid(lo, fp);
6436 refcount_inc(&stp->st_stid.sc_count);
6437 stp->st_stid.sc_type = NFS4_LOCK_STID;
6438 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
6440 stp->st_stid.sc_file = fp;
6441 stp->st_access_bmap = 0;
6442 stp->st_deny_bmap = open_stp->st_deny_bmap;
6443 stp->st_openstp = open_stp;
6444 list_add(&stp->st_locks, &open_stp->st_locks);
6445 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
6446 list_add(&stp->st_perfile, &fp->fi_stateids);
6448 spin_unlock(&fp->fi_lock);
6449 spin_unlock(&clp->cl_lock);
6451 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
6452 nfs4_put_stid(&retstp->st_stid);
6455 /* To keep mutex tracking happy */
6456 mutex_unlock(&stp->st_mutex);
6462 static struct nfs4_ol_stateid *
6463 find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
6464 struct inode *inode, struct nfs4_ol_stateid *ost,
6467 struct nfs4_stid *ns = NULL;
6468 struct nfs4_ol_stateid *lst;
6469 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
6470 struct nfs4_client *clp = oo->oo_owner.so_client;
6473 spin_lock(&clp->cl_lock);
6474 lst = find_lock_stateid(lo, fi);
6475 spin_unlock(&clp->cl_lock);
6477 if (nfsd4_lock_ol_stateid(lst) == nfs_ok)
6479 nfs4_put_stid(&lst->st_stid);
6481 ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid);
6485 lst = init_lock_stateid(openlockstateid(ns), lo, fi, inode, ost);
6486 if (lst == openlockstateid(ns))
6495 check_lock_length(u64 offset, u64 length)
6497 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
6498 (length > ~offset)));
6501 static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
6503 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
6505 lockdep_assert_held(&fp->fi_lock);
6507 if (test_access(access, lock_stp))
6509 __nfs4_file_get_access(fp, access);
6510 set_access(access, lock_stp);
6514 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
6515 struct nfs4_ol_stateid *ost,
6516 struct nfsd4_lock *lock,
6517 struct nfs4_ol_stateid **plst, bool *new)
6520 struct nfs4_file *fi = ost->st_stid.sc_file;
6521 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
6522 struct nfs4_client *cl = oo->oo_owner.so_client;
6523 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
6524 struct nfs4_lockowner *lo;
6525 struct nfs4_ol_stateid *lst;
6526 unsigned int strhashval;
6528 lo = find_lockowner_str(cl, &lock->lk_new_owner);
6530 strhashval = ownerstr_hashval(&lock->lk_new_owner);
6531 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
6533 return nfserr_jukebox;
6535 /* with an existing lockowner, seqids must be the same */
6536 status = nfserr_bad_seqid;
6537 if (!cstate->minorversion &&
6538 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
6542 lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
6544 status = nfserr_jukebox;
6551 nfs4_put_stateowner(&lo->lo_owner);
6559 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6560 union nfsd4_op_u *u)
6562 struct nfsd4_lock *lock = &u->lock;
6563 struct nfs4_openowner *open_sop = NULL;
6564 struct nfs4_lockowner *lock_sop = NULL;
6565 struct nfs4_ol_stateid *lock_stp = NULL;
6566 struct nfs4_ol_stateid *open_stp = NULL;
6567 struct nfs4_file *fp;
6568 struct nfsd_file *nf = NULL;
6569 struct nfsd4_blocked_lock *nbl = NULL;
6570 struct file_lock *file_lock = NULL;
6571 struct file_lock *conflock = NULL;
6576 unsigned char fl_type;
6577 unsigned int fl_flags = FL_POSIX;
6578 struct net *net = SVC_NET(rqstp);
6579 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6581 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
6582 (long long) lock->lk_offset,
6583 (long long) lock->lk_length);
6585 if (check_lock_length(lock->lk_offset, lock->lk_length))
6586 return nfserr_inval;
6588 if ((status = fh_verify(rqstp, &cstate->current_fh,
6589 S_IFREG, NFSD_MAY_LOCK))) {
6590 dprintk("NFSD: nfsd4_lock: permission denied!\n");
6594 if (lock->lk_is_new) {
6595 if (nfsd4_has_session(cstate))
6596 /* See rfc 5661 18.10.3: given clientid is ignored: */
6597 memcpy(&lock->lk_new_clientid,
6598 &cstate->session->se_client->cl_clientid,
6599 sizeof(clientid_t));
6601 status = nfserr_stale_clientid;
6602 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
6605 /* validate and update open stateid and open seqid */
6606 status = nfs4_preprocess_confirmed_seqid_op(cstate,
6607 lock->lk_new_open_seqid,
6608 &lock->lk_new_open_stateid,
6612 mutex_unlock(&open_stp->st_mutex);
6613 open_sop = openowner(open_stp->st_stateowner);
6614 status = nfserr_bad_stateid;
6615 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
6616 &lock->lk_new_clientid))
6618 status = lookup_or_create_lock_state(cstate, open_stp, lock,
6621 status = nfs4_preprocess_seqid_op(cstate,
6622 lock->lk_old_lock_seqid,
6623 &lock->lk_old_lock_stateid,
6624 NFS4_LOCK_STID, &lock_stp, nn);
6628 lock_sop = lockowner(lock_stp->st_stateowner);
6630 lkflg = setlkflg(lock->lk_type);
6631 status = nfs4_check_openmode(lock_stp, lkflg);
6635 status = nfserr_grace;
6636 if (locks_in_grace(net) && !lock->lk_reclaim)
6638 status = nfserr_no_grace;
6639 if (!locks_in_grace(net) && lock->lk_reclaim)
6642 fp = lock_stp->st_stid.sc_file;
6643 switch (lock->lk_type) {
6645 if (nfsd4_has_session(cstate))
6646 fl_flags |= FL_SLEEP;
6649 spin_lock(&fp->fi_lock);
6650 nf = find_readable_file_locked(fp);
6652 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
6653 spin_unlock(&fp->fi_lock);
6656 case NFS4_WRITEW_LT:
6657 if (nfsd4_has_session(cstate))
6658 fl_flags |= FL_SLEEP;
6661 spin_lock(&fp->fi_lock);
6662 nf = find_writeable_file_locked(fp);
6664 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
6665 spin_unlock(&fp->fi_lock);
6669 status = nfserr_inval;
6674 status = nfserr_openmode;
6678 nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
6680 dprintk("NFSD: %s: unable to allocate block!\n", __func__);
6681 status = nfserr_jukebox;
6685 file_lock = &nbl->nbl_lock;
6686 file_lock->fl_type = fl_type;
6687 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
6688 file_lock->fl_pid = current->tgid;
6689 file_lock->fl_file = nf->nf_file;
6690 file_lock->fl_flags = fl_flags;
6691 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6692 file_lock->fl_start = lock->lk_offset;
6693 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
6694 nfs4_transform_lock_offset(file_lock);
6696 conflock = locks_alloc_lock();
6698 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6699 status = nfserr_jukebox;
6703 if (fl_flags & FL_SLEEP) {
6704 nbl->nbl_time = jiffies;
6705 spin_lock(&nn->blocked_locks_lock);
6706 list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
6707 list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
6708 spin_unlock(&nn->blocked_locks_lock);
6711 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, conflock);
6713 case 0: /* success! */
6714 nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
6716 if (lock->lk_reclaim)
6717 nn->somebody_reclaimed = true;
6719 case FILE_LOCK_DEFERRED:
6722 case -EAGAIN: /* conflock holds conflicting lock */
6723 status = nfserr_denied;
6724 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
6725 nfs4_set_lock_denied(conflock, &lock->lk_denied);
6728 status = nfserr_deadlock;
6731 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
6732 status = nfserrno(err);
6737 /* dequeue it if we queued it before */
6738 if (fl_flags & FL_SLEEP) {
6739 spin_lock(&nn->blocked_locks_lock);
6740 list_del_init(&nbl->nbl_list);
6741 list_del_init(&nbl->nbl_lru);
6742 spin_unlock(&nn->blocked_locks_lock);
6744 free_blocked_lock(nbl);
6749 /* Bump seqid manually if the 4.0 replay owner is openowner */
6750 if (cstate->replay_owner &&
6751 cstate->replay_owner != &lock_sop->lo_owner &&
6752 seqid_mutating_err(ntohl(status)))
6753 lock_sop->lo_owner.so_seqid++;
6756 * If this is a new, never-before-used stateid, and we are
6757 * returning an error, then just go ahead and release it.
6760 release_lock_stateid(lock_stp);
6762 mutex_unlock(&lock_stp->st_mutex);
6764 nfs4_put_stid(&lock_stp->st_stid);
6767 nfs4_put_stid(&open_stp->st_stid);
6768 nfsd4_bump_seqid(cstate, status);
6770 locks_free_lock(conflock);
6775 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
6776 * so we do a temporary open here just to get an open file to pass to
6777 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
6780 static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
6782 struct nfsd_file *nf;
6783 __be32 err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf);
6785 err = nfserrno(vfs_test_lock(nf->nf_file, lock));
6795 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6796 union nfsd4_op_u *u)
6798 struct nfsd4_lockt *lockt = &u->lockt;
6799 struct file_lock *file_lock = NULL;
6800 struct nfs4_lockowner *lo = NULL;
6802 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6804 if (locks_in_grace(SVC_NET(rqstp)))
6805 return nfserr_grace;
6807 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
6808 return nfserr_inval;
6810 if (!nfsd4_has_session(cstate)) {
6811 status = lookup_clientid(&lockt->lt_clientid, cstate, nn,
6817 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
6820 file_lock = locks_alloc_lock();
6822 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6823 status = nfserr_jukebox;
6827 switch (lockt->lt_type) {
6830 file_lock->fl_type = F_RDLCK;
6833 case NFS4_WRITEW_LT:
6834 file_lock->fl_type = F_WRLCK;
6837 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
6838 status = nfserr_inval;
6842 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
6844 file_lock->fl_owner = (fl_owner_t)lo;
6845 file_lock->fl_pid = current->tgid;
6846 file_lock->fl_flags = FL_POSIX;
6848 file_lock->fl_start = lockt->lt_offset;
6849 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
6851 nfs4_transform_lock_offset(file_lock);
6853 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
6857 if (file_lock->fl_type != F_UNLCK) {
6858 status = nfserr_denied;
6859 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
6863 nfs4_put_stateowner(&lo->lo_owner);
6865 locks_free_lock(file_lock);
6870 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6871 union nfsd4_op_u *u)
6873 struct nfsd4_locku *locku = &u->locku;
6874 struct nfs4_ol_stateid *stp;
6875 struct nfsd_file *nf = NULL;
6876 struct file_lock *file_lock = NULL;
6879 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6881 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
6882 (long long) locku->lu_offset,
6883 (long long) locku->lu_length);
6885 if (check_lock_length(locku->lu_offset, locku->lu_length))
6886 return nfserr_inval;
6888 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
6889 &locku->lu_stateid, NFS4_LOCK_STID,
6893 nf = find_any_file(stp->st_stid.sc_file);
6895 status = nfserr_lock_range;
6898 file_lock = locks_alloc_lock();
6900 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6901 status = nfserr_jukebox;
6905 file_lock->fl_type = F_UNLCK;
6906 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
6907 file_lock->fl_pid = current->tgid;
6908 file_lock->fl_file = nf->nf_file;
6909 file_lock->fl_flags = FL_POSIX;
6910 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6911 file_lock->fl_start = locku->lu_offset;
6913 file_lock->fl_end = last_byte_offset(locku->lu_offset,
6915 nfs4_transform_lock_offset(file_lock);
6917 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, NULL);
6919 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
6922 nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
6926 mutex_unlock(&stp->st_mutex);
6927 nfs4_put_stid(&stp->st_stid);
6929 nfsd4_bump_seqid(cstate, status);
6931 locks_free_lock(file_lock);
6935 status = nfserrno(err);
6941 * true: locks held by lockowner
6942 * false: no locks held by lockowner
6945 check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
6947 struct file_lock *fl;
6949 struct nfsd_file *nf = find_any_file(fp);
6950 struct inode *inode;
6951 struct file_lock_context *flctx;
6954 /* Any valid lock stateid should have some sort of access */
6959 inode = locks_inode(nf->nf_file);
6960 flctx = inode->i_flctx;
6962 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
6963 spin_lock(&flctx->flc_lock);
6964 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
6965 if (fl->fl_owner == (fl_owner_t)lowner) {
6970 spin_unlock(&flctx->flc_lock);
6977 nfsd4_release_lockowner(struct svc_rqst *rqstp,
6978 struct nfsd4_compound_state *cstate,
6979 union nfsd4_op_u *u)
6981 struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner;
6982 clientid_t *clid = &rlockowner->rl_clientid;
6983 struct nfs4_stateowner *sop;
6984 struct nfs4_lockowner *lo = NULL;
6985 struct nfs4_ol_stateid *stp;
6986 struct xdr_netobj *owner = &rlockowner->rl_owner;
6987 unsigned int hashval = ownerstr_hashval(owner);
6989 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6990 struct nfs4_client *clp;
6991 LIST_HEAD (reaplist);
6993 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
6994 clid->cl_boot, clid->cl_id);
6996 status = lookup_clientid(clid, cstate, nn, false);
7001 /* Find the matching lock stateowner */
7002 spin_lock(&clp->cl_lock);
7003 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
7006 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
7009 /* see if there are still any locks associated with it */
7010 lo = lockowner(sop);
7011 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
7012 if (check_for_locks(stp->st_stid.sc_file, lo)) {
7013 status = nfserr_locks_held;
7014 spin_unlock(&clp->cl_lock);
7019 nfs4_get_stateowner(sop);
7023 spin_unlock(&clp->cl_lock);
7027 unhash_lockowner_locked(lo);
7028 while (!list_empty(&lo->lo_owner.so_stateids)) {
7029 stp = list_first_entry(&lo->lo_owner.so_stateids,
7030 struct nfs4_ol_stateid,
7032 WARN_ON(!unhash_lock_stateid(stp));
7033 put_ol_stateid_locked(stp, &reaplist);
7035 spin_unlock(&clp->cl_lock);
7036 free_ol_stateid_reaplist(&reaplist);
7037 remove_blocked_locks(lo);
7038 nfs4_put_stateowner(&lo->lo_owner);
7043 static inline struct nfs4_client_reclaim *
7046 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
7050 nfs4_has_reclaimed_state(struct xdr_netobj name, struct nfsd_net *nn)
7052 struct nfs4_client_reclaim *crp;
7054 crp = nfsd4_find_reclaim_client(name, nn);
7055 return (crp && crp->cr_clp);
7059 * failure => all reset bets are off, nfserr_no_grace...
7061 * The caller is responsible for freeing name.data if NULL is returned (it
7062 * will be freed in nfs4_remove_reclaim_record in the normal case).
7064 struct nfs4_client_reclaim *
7065 nfs4_client_to_reclaim(struct xdr_netobj name, struct xdr_netobj princhash,
7066 struct nfsd_net *nn)
7068 unsigned int strhashval;
7069 struct nfs4_client_reclaim *crp;
7071 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", name.len, name.data);
7072 crp = alloc_reclaim();
7074 strhashval = clientstr_hashval(name);
7075 INIT_LIST_HEAD(&crp->cr_strhash);
7076 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
7077 crp->cr_name.data = name.data;
7078 crp->cr_name.len = name.len;
7079 crp->cr_princhash.data = princhash.data;
7080 crp->cr_princhash.len = princhash.len;
7082 nn->reclaim_str_hashtbl_size++;
7088 nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
7090 list_del(&crp->cr_strhash);
7091 kfree(crp->cr_name.data);
7092 kfree(crp->cr_princhash.data);
7094 nn->reclaim_str_hashtbl_size--;
7098 nfs4_release_reclaim(struct nfsd_net *nn)
7100 struct nfs4_client_reclaim *crp = NULL;
7103 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7104 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
7105 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
7106 struct nfs4_client_reclaim, cr_strhash);
7107 nfs4_remove_reclaim_record(crp, nn);
7110 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
7114 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
7115 struct nfs4_client_reclaim *
7116 nfsd4_find_reclaim_client(struct xdr_netobj name, struct nfsd_net *nn)
7118 unsigned int strhashval;
7119 struct nfs4_client_reclaim *crp = NULL;
7121 dprintk("NFSD: nfs4_find_reclaim_client for name %.*s\n", name.len, name.data);
7123 strhashval = clientstr_hashval(name);
7124 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
7125 if (compare_blob(&crp->cr_name, &name) == 0) {
7133 * Called from OPEN. Look for clientid in reclaim list.
7136 nfs4_check_open_reclaim(clientid_t *clid,
7137 struct nfsd4_compound_state *cstate,
7138 struct nfsd_net *nn)
7142 /* find clientid in conf_id_hashtbl */
7143 status = lookup_clientid(clid, cstate, nn, false);
7145 return nfserr_reclaim_bad;
7147 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
7148 return nfserr_no_grace;
7150 if (nfsd4_client_record_check(cstate->clp))
7151 return nfserr_reclaim_bad;
7156 #ifdef CONFIG_NFSD_FAULT_INJECTION
7158 put_client(struct nfs4_client *clp)
7160 atomic_dec(&clp->cl_rpc_users);
7163 static struct nfs4_client *
7164 nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
7166 struct nfs4_client *clp;
7167 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7170 if (!nfsd_netns_ready(nn))
7173 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
7174 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
7181 nfsd_inject_print_clients(void)
7183 struct nfs4_client *clp;
7185 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7187 char buf[INET6_ADDRSTRLEN];
7189 if (!nfsd_netns_ready(nn))
7192 spin_lock(&nn->client_lock);
7193 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
7194 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
7195 pr_info("NFS Client: %s\n", buf);
7198 spin_unlock(&nn->client_lock);
7204 nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
7207 struct nfs4_client *clp;
7208 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7211 if (!nfsd_netns_ready(nn))
7214 spin_lock(&nn->client_lock);
7215 clp = nfsd_find_client(addr, addr_size);
7217 if (mark_client_expired_locked(clp) == nfs_ok)
7222 spin_unlock(&nn->client_lock);
7231 nfsd_inject_forget_clients(u64 max)
7234 struct nfs4_client *clp, *next;
7235 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7237 LIST_HEAD(reaplist);
7239 if (!nfsd_netns_ready(nn))
7242 spin_lock(&nn->client_lock);
7243 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
7244 if (mark_client_expired_locked(clp) == nfs_ok) {
7245 list_add(&clp->cl_lru, &reaplist);
7246 if (max != 0 && ++count >= max)
7250 spin_unlock(&nn->client_lock);
7252 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
7258 static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
7261 char buf[INET6_ADDRSTRLEN];
7262 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
7263 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
7267 nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
7268 struct list_head *collect)
7270 struct nfs4_client *clp = lst->st_stid.sc_client;
7271 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7277 lockdep_assert_held(&nn->client_lock);
7278 atomic_inc(&clp->cl_rpc_users);
7279 list_add(&lst->st_locks, collect);
7282 static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
7283 struct list_head *collect,
7284 bool (*func)(struct nfs4_ol_stateid *))
7286 struct nfs4_openowner *oop;
7287 struct nfs4_ol_stateid *stp, *st_next;
7288 struct nfs4_ol_stateid *lst, *lst_next;
7291 spin_lock(&clp->cl_lock);
7292 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
7293 list_for_each_entry_safe(stp, st_next,
7294 &oop->oo_owner.so_stateids, st_perstateowner) {
7295 list_for_each_entry_safe(lst, lst_next,
7296 &stp->st_locks, st_locks) {
7299 nfsd_inject_add_lock_to_list(lst,
7304 * Despite the fact that these functions deal
7305 * with 64-bit integers for "count", we must
7306 * ensure that it doesn't blow up the
7307 * clp->cl_rpc_users. Throw a warning if we
7308 * start to approach INT_MAX here.
7310 WARN_ON_ONCE(count == (INT_MAX / 2));
7317 spin_unlock(&clp->cl_lock);
7323 nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
7326 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
7330 nfsd_print_client_locks(struct nfs4_client *clp)
7332 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
7333 nfsd_print_count(clp, count, "locked files");
7338 nfsd_inject_print_locks(void)
7340 struct nfs4_client *clp;
7342 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7345 if (!nfsd_netns_ready(nn))
7348 spin_lock(&nn->client_lock);
7349 list_for_each_entry(clp, &nn->client_lru, cl_lru)
7350 count += nfsd_print_client_locks(clp);
7351 spin_unlock(&nn->client_lock);
7357 nfsd_reap_locks(struct list_head *reaplist)
7359 struct nfs4_client *clp;
7360 struct nfs4_ol_stateid *stp, *next;
7362 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
7363 list_del_init(&stp->st_locks);
7364 clp = stp->st_stid.sc_client;
7365 nfs4_put_stid(&stp->st_stid);
7371 nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
7373 unsigned int count = 0;
7374 struct nfs4_client *clp;
7375 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7377 LIST_HEAD(reaplist);
7379 if (!nfsd_netns_ready(nn))
7382 spin_lock(&nn->client_lock);
7383 clp = nfsd_find_client(addr, addr_size);
7385 count = nfsd_collect_client_locks(clp, &reaplist, 0);
7386 spin_unlock(&nn->client_lock);
7387 nfsd_reap_locks(&reaplist);
7392 nfsd_inject_forget_locks(u64 max)
7395 struct nfs4_client *clp;
7396 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7398 LIST_HEAD(reaplist);
7400 if (!nfsd_netns_ready(nn))
7403 spin_lock(&nn->client_lock);
7404 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
7405 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
7406 if (max != 0 && count >= max)
7409 spin_unlock(&nn->client_lock);
7410 nfsd_reap_locks(&reaplist);
7415 nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
7416 struct list_head *collect,
7417 void (*func)(struct nfs4_openowner *))
7419 struct nfs4_openowner *oop, *next;
7420 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7424 lockdep_assert_held(&nn->client_lock);
7426 spin_lock(&clp->cl_lock);
7427 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
7431 atomic_inc(&clp->cl_rpc_users);
7432 list_add(&oop->oo_perclient, collect);
7437 * Despite the fact that these functions deal with
7438 * 64-bit integers for "count", we must ensure that
7439 * it doesn't blow up the clp->cl_rpc_users. Throw a
7440 * warning if we start to approach INT_MAX here.
7442 WARN_ON_ONCE(count == (INT_MAX / 2));
7446 spin_unlock(&clp->cl_lock);
7452 nfsd_print_client_openowners(struct nfs4_client *clp)
7454 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
7456 nfsd_print_count(clp, count, "openowners");
7461 nfsd_collect_client_openowners(struct nfs4_client *clp,
7462 struct list_head *collect, u64 max)
7464 return nfsd_foreach_client_openowner(clp, max, collect,
7465 unhash_openowner_locked);
7469 nfsd_inject_print_openowners(void)
7471 struct nfs4_client *clp;
7473 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7476 if (!nfsd_netns_ready(nn))
7479 spin_lock(&nn->client_lock);
7480 list_for_each_entry(clp, &nn->client_lru, cl_lru)
7481 count += nfsd_print_client_openowners(clp);
7482 spin_unlock(&nn->client_lock);
7488 nfsd_reap_openowners(struct list_head *reaplist)
7490 struct nfs4_client *clp;
7491 struct nfs4_openowner *oop, *next;
7493 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
7494 list_del_init(&oop->oo_perclient);
7495 clp = oop->oo_owner.so_client;
7496 release_openowner(oop);
7502 nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
7505 unsigned int count = 0;
7506 struct nfs4_client *clp;
7507 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7509 LIST_HEAD(reaplist);
7511 if (!nfsd_netns_ready(nn))
7514 spin_lock(&nn->client_lock);
7515 clp = nfsd_find_client(addr, addr_size);
7517 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
7518 spin_unlock(&nn->client_lock);
7519 nfsd_reap_openowners(&reaplist);
7524 nfsd_inject_forget_openowners(u64 max)
7527 struct nfs4_client *clp;
7528 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7530 LIST_HEAD(reaplist);
7532 if (!nfsd_netns_ready(nn))
7535 spin_lock(&nn->client_lock);
7536 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
7537 count += nfsd_collect_client_openowners(clp, &reaplist,
7539 if (max != 0 && count >= max)
7542 spin_unlock(&nn->client_lock);
7543 nfsd_reap_openowners(&reaplist);
7547 static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
7548 struct list_head *victims)
7550 struct nfs4_delegation *dp, *next;
7551 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7555 lockdep_assert_held(&nn->client_lock);
7557 spin_lock(&state_lock);
7558 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
7561 * It's not safe to mess with delegations that have a
7562 * non-zero dl_time. They might have already been broken
7563 * and could be processed by the laundromat outside of
7564 * the state_lock. Just leave them be.
7566 if (dp->dl_time != 0)
7569 atomic_inc(&clp->cl_rpc_users);
7570 WARN_ON(!unhash_delegation_locked(dp));
7571 list_add(&dp->dl_recall_lru, victims);
7575 * Despite the fact that these functions deal with
7576 * 64-bit integers for "count", we must ensure that
7577 * it doesn't blow up the clp->cl_rpc_users. Throw a
7578 * warning if we start to approach INT_MAX here.
7580 WARN_ON_ONCE(count == (INT_MAX / 2));
7584 spin_unlock(&state_lock);
7589 nfsd_print_client_delegations(struct nfs4_client *clp)
7591 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
7593 nfsd_print_count(clp, count, "delegations");
7598 nfsd_inject_print_delegations(void)
7600 struct nfs4_client *clp;
7602 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7605 if (!nfsd_netns_ready(nn))
7608 spin_lock(&nn->client_lock);
7609 list_for_each_entry(clp, &nn->client_lru, cl_lru)
7610 count += nfsd_print_client_delegations(clp);
7611 spin_unlock(&nn->client_lock);
7617 nfsd_forget_delegations(struct list_head *reaplist)
7619 struct nfs4_client *clp;
7620 struct nfs4_delegation *dp, *next;
7622 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
7623 list_del_init(&dp->dl_recall_lru);
7624 clp = dp->dl_stid.sc_client;
7625 revoke_delegation(dp);
7631 nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
7635 struct nfs4_client *clp;
7636 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7638 LIST_HEAD(reaplist);
7640 if (!nfsd_netns_ready(nn))
7643 spin_lock(&nn->client_lock);
7644 clp = nfsd_find_client(addr, addr_size);
7646 count = nfsd_find_all_delegations(clp, 0, &reaplist);
7647 spin_unlock(&nn->client_lock);
7649 nfsd_forget_delegations(&reaplist);
7654 nfsd_inject_forget_delegations(u64 max)
7657 struct nfs4_client *clp;
7658 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7660 LIST_HEAD(reaplist);
7662 if (!nfsd_netns_ready(nn))
7665 spin_lock(&nn->client_lock);
7666 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
7667 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
7668 if (max != 0 && count >= max)
7671 spin_unlock(&nn->client_lock);
7672 nfsd_forget_delegations(&reaplist);
7677 nfsd_recall_delegations(struct list_head *reaplist)
7679 struct nfs4_client *clp;
7680 struct nfs4_delegation *dp, *next;
7682 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
7683 list_del_init(&dp->dl_recall_lru);
7684 clp = dp->dl_stid.sc_client;
7686 * We skipped all entries that had a zero dl_time before,
7687 * so we can now reset the dl_time back to 0. If a delegation
7688 * break comes in now, then it won't make any difference since
7689 * we're recalling it either way.
7691 spin_lock(&state_lock);
7693 spin_unlock(&state_lock);
7694 nfsd_break_one_deleg(dp);
7700 nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
7704 struct nfs4_client *clp;
7705 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7707 LIST_HEAD(reaplist);
7709 if (!nfsd_netns_ready(nn))
7712 spin_lock(&nn->client_lock);
7713 clp = nfsd_find_client(addr, addr_size);
7715 count = nfsd_find_all_delegations(clp, 0, &reaplist);
7716 spin_unlock(&nn->client_lock);
7718 nfsd_recall_delegations(&reaplist);
7723 nfsd_inject_recall_delegations(u64 max)
7726 struct nfs4_client *clp, *next;
7727 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7729 LIST_HEAD(reaplist);
7731 if (!nfsd_netns_ready(nn))
7734 spin_lock(&nn->client_lock);
7735 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
7736 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
7737 if (max != 0 && ++count >= max)
7740 spin_unlock(&nn->client_lock);
7741 nfsd_recall_delegations(&reaplist);
7744 #endif /* CONFIG_NFSD_FAULT_INJECTION */
7747 * Since the lifetime of a delegation isn't limited to that of an open, a
7748 * client may quite reasonably hang on to a delegation as long as it has
7749 * the inode cached. This becomes an obvious problem the first time a
7750 * client's inode cache approaches the size of the server's total memory.
7752 * For now we avoid this problem by imposing a hard limit on the number
7753 * of delegations, which varies according to the server's memory size.
7756 set_max_delegations(void)
7759 * Allow at most 4 delegations per megabyte of RAM. Quick
7760 * estimates suggest that in the worst case (where every delegation
7761 * is for a different inode), a delegation could take about 1.5K,
7762 * giving a worst case usage of about 6% of memory.
7764 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
7767 static int nfs4_state_create_net(struct net *net)
7769 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7772 nn->conf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7773 sizeof(struct list_head),
7775 if (!nn->conf_id_hashtbl)
7777 nn->unconf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7778 sizeof(struct list_head),
7780 if (!nn->unconf_id_hashtbl)
7782 nn->sessionid_hashtbl = kmalloc_array(SESSION_HASH_SIZE,
7783 sizeof(struct list_head),
7785 if (!nn->sessionid_hashtbl)
7788 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7789 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
7790 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
7792 for (i = 0; i < SESSION_HASH_SIZE; i++)
7793 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
7794 nn->conf_name_tree = RB_ROOT;
7795 nn->unconf_name_tree = RB_ROOT;
7796 nn->boot_time = get_seconds();
7797 nn->grace_ended = false;
7798 nn->nfsd4_manager.block_opens = true;
7799 INIT_LIST_HEAD(&nn->nfsd4_manager.list);
7800 INIT_LIST_HEAD(&nn->client_lru);
7801 INIT_LIST_HEAD(&nn->close_lru);
7802 INIT_LIST_HEAD(&nn->del_recall_lru);
7803 spin_lock_init(&nn->client_lock);
7804 spin_lock_init(&nn->s2s_cp_lock);
7805 idr_init(&nn->s2s_cp_stateids);
7807 spin_lock_init(&nn->blocked_locks_lock);
7808 INIT_LIST_HEAD(&nn->blocked_locks_lru);
7810 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
7816 kfree(nn->unconf_id_hashtbl);
7818 kfree(nn->conf_id_hashtbl);
7824 nfs4_state_destroy_net(struct net *net)
7827 struct nfs4_client *clp = NULL;
7828 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7830 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7831 while (!list_empty(&nn->conf_id_hashtbl[i])) {
7832 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7833 destroy_client(clp);
7837 WARN_ON(!list_empty(&nn->blocked_locks_lru));
7839 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7840 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
7841 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7842 destroy_client(clp);
7846 kfree(nn->sessionid_hashtbl);
7847 kfree(nn->unconf_id_hashtbl);
7848 kfree(nn->conf_id_hashtbl);
7853 nfs4_state_start_net(struct net *net)
7855 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7858 ret = nfs4_state_create_net(net);
7861 locks_start_grace(net, &nn->nfsd4_manager);
7862 nfsd4_client_tracking_init(net);
7863 if (nn->track_reclaim_completes && nn->reclaim_str_hashtbl_size == 0)
7865 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %x)\n",
7866 nn->nfsd4_grace, net->ns.inum);
7867 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
7871 printk(KERN_INFO "NFSD: no clients to reclaim, skipping NFSv4 grace period (net %x)\n",
7873 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_lease * HZ);
7874 nfsd4_end_grace(nn);
7878 /* initialization to perform when the nfsd service is started: */
7881 nfs4_state_start(void)
7885 laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
7886 if (laundry_wq == NULL) {
7890 ret = nfsd4_create_callback_queue();
7892 goto out_free_laundry;
7894 set_max_delegations();
7898 destroy_workqueue(laundry_wq);
7904 nfs4_state_shutdown_net(struct net *net)
7906 struct nfs4_delegation *dp = NULL;
7907 struct list_head *pos, *next, reaplist;
7908 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7910 cancel_delayed_work_sync(&nn->laundromat_work);
7911 locks_end_grace(&nn->nfsd4_manager);
7913 INIT_LIST_HEAD(&reaplist);
7914 spin_lock(&state_lock);
7915 list_for_each_safe(pos, next, &nn->del_recall_lru) {
7916 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
7917 WARN_ON(!unhash_delegation_locked(dp));
7918 list_add(&dp->dl_recall_lru, &reaplist);
7920 spin_unlock(&state_lock);
7921 list_for_each_safe(pos, next, &reaplist) {
7922 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
7923 list_del_init(&dp->dl_recall_lru);
7924 destroy_unhashed_deleg(dp);
7927 nfsd4_client_tracking_exit(net);
7928 nfs4_state_destroy_net(net);
7932 nfs4_state_shutdown(void)
7934 destroy_workqueue(laundry_wq);
7935 nfsd4_destroy_callback_queue();
7939 get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7941 if (HAS_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG) &&
7942 CURRENT_STATEID(stateid))
7943 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
7947 put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7949 if (cstate->minorversion) {
7950 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
7951 SET_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
7956 clear_current_stateid(struct nfsd4_compound_state *cstate)
7958 CLEAR_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
7962 * functions to set current state id
7965 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate,
7966 union nfsd4_op_u *u)
7968 put_stateid(cstate, &u->open_downgrade.od_stateid);
7972 nfsd4_set_openstateid(struct nfsd4_compound_state *cstate,
7973 union nfsd4_op_u *u)
7975 put_stateid(cstate, &u->open.op_stateid);
7979 nfsd4_set_closestateid(struct nfsd4_compound_state *cstate,
7980 union nfsd4_op_u *u)
7982 put_stateid(cstate, &u->close.cl_stateid);
7986 nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate,
7987 union nfsd4_op_u *u)
7989 put_stateid(cstate, &u->lock.lk_resp_stateid);
7993 * functions to consume current state id
7997 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate,
7998 union nfsd4_op_u *u)
8000 get_stateid(cstate, &u->open_downgrade.od_stateid);
8004 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate,
8005 union nfsd4_op_u *u)
8007 get_stateid(cstate, &u->delegreturn.dr_stateid);
8011 nfsd4_get_freestateid(struct nfsd4_compound_state *cstate,
8012 union nfsd4_op_u *u)
8014 get_stateid(cstate, &u->free_stateid.fr_stateid);
8018 nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate,
8019 union nfsd4_op_u *u)
8021 get_stateid(cstate, &u->setattr.sa_stateid);
8025 nfsd4_get_closestateid(struct nfsd4_compound_state *cstate,
8026 union nfsd4_op_u *u)
8028 get_stateid(cstate, &u->close.cl_stateid);
8032 nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate,
8033 union nfsd4_op_u *u)
8035 get_stateid(cstate, &u->locku.lu_stateid);
8039 nfsd4_get_readstateid(struct nfsd4_compound_state *cstate,
8040 union nfsd4_op_u *u)
8042 get_stateid(cstate, &u->read.rd_stateid);
8046 nfsd4_get_writestateid(struct nfsd4_compound_state *cstate,
8047 union nfsd4_op_u *u)
8049 get_stateid(cstate, &u->write.wr_stateid);