2 * Copyright (c) 2001 The Regents of the University of Michigan.
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"
56 #define NFSDDBG_FACILITY NFSDDBG_PROC
58 #define all_ones {{~0,~0},~0}
59 static const stateid_t one_stateid = {
61 .si_opaque = all_ones,
63 static const stateid_t zero_stateid = {
66 static const stateid_t currentstateid = {
69 static const stateid_t close_stateid = {
70 .si_generation = 0xffffffffU,
73 static u64 current_sessionid = 1;
75 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
76 #define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
77 #define CURRENT_STATEID(stateid) (!memcmp((stateid), ¤tstateid, sizeof(stateid_t)))
78 #define CLOSE_STATEID(stateid) (!memcmp((stateid), &close_stateid, sizeof(stateid_t)))
80 /* forward declarations */
81 static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
82 static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
83 void nfsd4_end_grace(struct nfsd_net *nn);
84 static void _free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps);
89 * Currently used for the del_recall_lru and file hash table. In an
90 * effort to decrease the scope of the client_mutex, this spinlock may
91 * eventually cover more:
93 static DEFINE_SPINLOCK(state_lock);
95 enum nfsd4_st_mutex_lock_subclass {
96 OPEN_STATEID_MUTEX = 0,
97 LOCK_STATEID_MUTEX = 1,
101 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
102 * the refcount on the open stateid to drop.
104 static DECLARE_WAIT_QUEUE_HEAD(close_wq);
107 * A waitqueue where a writer to clients/#/ctl destroying a client can
108 * wait for cl_rpc_users to drop to 0 and then for the client to be
111 static DECLARE_WAIT_QUEUE_HEAD(expiry_wq);
113 static struct kmem_cache *client_slab;
114 static struct kmem_cache *openowner_slab;
115 static struct kmem_cache *lockowner_slab;
116 static struct kmem_cache *file_slab;
117 static struct kmem_cache *stateid_slab;
118 static struct kmem_cache *deleg_slab;
119 static struct kmem_cache *odstate_slab;
121 static void free_session(struct nfsd4_session *);
123 static const struct nfsd4_callback_ops nfsd4_cb_recall_ops;
124 static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops;
126 static bool is_session_dead(struct nfsd4_session *ses)
128 return ses->se_flags & NFS4_SESSION_DEAD;
131 static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
133 if (atomic_read(&ses->se_ref) > ref_held_by_me)
134 return nfserr_jukebox;
135 ses->se_flags |= NFS4_SESSION_DEAD;
139 static bool is_client_expired(struct nfs4_client *clp)
141 return clp->cl_time == 0;
144 static __be32 get_client_locked(struct nfs4_client *clp)
146 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
148 lockdep_assert_held(&nn->client_lock);
150 if (is_client_expired(clp))
151 return nfserr_expired;
152 atomic_inc(&clp->cl_rpc_users);
156 /* must be called under the client_lock */
158 renew_client_locked(struct nfs4_client *clp)
160 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
162 if (is_client_expired(clp)) {
164 printk("%s: client (clientid %08x/%08x) already expired\n",
166 clp->cl_clientid.cl_boot,
167 clp->cl_clientid.cl_id);
171 list_move_tail(&clp->cl_lru, &nn->client_lru);
172 clp->cl_time = ktime_get_boottime_seconds();
175 static void put_client_renew_locked(struct nfs4_client *clp)
177 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
179 lockdep_assert_held(&nn->client_lock);
181 if (!atomic_dec_and_test(&clp->cl_rpc_users))
183 if (!is_client_expired(clp))
184 renew_client_locked(clp);
186 wake_up_all(&expiry_wq);
189 static void put_client_renew(struct nfs4_client *clp)
191 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
193 if (!atomic_dec_and_lock(&clp->cl_rpc_users, &nn->client_lock))
195 if (!is_client_expired(clp))
196 renew_client_locked(clp);
198 wake_up_all(&expiry_wq);
199 spin_unlock(&nn->client_lock);
202 static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
206 if (is_session_dead(ses))
207 return nfserr_badsession;
208 status = get_client_locked(ses->se_client);
211 atomic_inc(&ses->se_ref);
215 static void nfsd4_put_session_locked(struct nfsd4_session *ses)
217 struct nfs4_client *clp = ses->se_client;
218 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
220 lockdep_assert_held(&nn->client_lock);
222 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
224 put_client_renew_locked(clp);
227 static void nfsd4_put_session(struct nfsd4_session *ses)
229 struct nfs4_client *clp = ses->se_client;
230 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
232 spin_lock(&nn->client_lock);
233 nfsd4_put_session_locked(ses);
234 spin_unlock(&nn->client_lock);
237 static struct nfsd4_blocked_lock *
238 find_blocked_lock(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
241 struct nfsd4_blocked_lock *cur, *found = NULL;
243 spin_lock(&nn->blocked_locks_lock);
244 list_for_each_entry(cur, &lo->lo_blocked, nbl_list) {
245 if (fh_match(fh, &cur->nbl_fh)) {
246 list_del_init(&cur->nbl_list);
247 list_del_init(&cur->nbl_lru);
252 spin_unlock(&nn->blocked_locks_lock);
254 locks_delete_block(&found->nbl_lock);
258 static struct nfsd4_blocked_lock *
259 find_or_allocate_block(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
262 struct nfsd4_blocked_lock *nbl;
264 nbl = find_blocked_lock(lo, fh, nn);
266 nbl= kmalloc(sizeof(*nbl), GFP_KERNEL);
268 INIT_LIST_HEAD(&nbl->nbl_list);
269 INIT_LIST_HEAD(&nbl->nbl_lru);
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;
499 spin_lock(&f->fi_lock);
500 ret = __nfs4_get_fd(f, O_RDWR);
502 ret = __nfs4_get_fd(f, O_WRONLY);
504 ret = __nfs4_get_fd(f, O_RDONLY);
506 spin_unlock(&f->fi_lock);
510 static struct nfsd_file *find_deleg_file(struct nfs4_file *f)
512 struct nfsd_file *ret = NULL;
514 spin_lock(&f->fi_lock);
515 if (f->fi_deleg_file)
516 ret = nfsd_file_get(f->fi_deleg_file);
517 spin_unlock(&f->fi_lock);
521 static atomic_long_t num_delegations;
522 unsigned long max_delegations;
525 * Open owner state (share locks)
528 /* hash tables for lock and open owners */
529 #define OWNER_HASH_BITS 8
530 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
531 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
533 static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
537 ret = opaque_hashval(ownername->data, ownername->len);
538 return ret & OWNER_HASH_MASK;
541 /* hash table for nfs4_file */
542 #define FILE_HASH_BITS 8
543 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
545 static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
547 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
550 static unsigned int file_hashval(struct knfsd_fh *fh)
552 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
555 static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
558 __nfs4_file_get_access(struct nfs4_file *fp, u32 access)
560 lockdep_assert_held(&fp->fi_lock);
562 if (access & NFS4_SHARE_ACCESS_WRITE)
563 atomic_inc(&fp->fi_access[O_WRONLY]);
564 if (access & NFS4_SHARE_ACCESS_READ)
565 atomic_inc(&fp->fi_access[O_RDONLY]);
569 nfs4_file_get_access(struct nfs4_file *fp, u32 access)
571 lockdep_assert_held(&fp->fi_lock);
573 /* Does this access mode make sense? */
574 if (access & ~NFS4_SHARE_ACCESS_BOTH)
577 /* Does it conflict with a deny mode already set? */
578 if ((access & fp->fi_share_deny) != 0)
579 return nfserr_share_denied;
581 __nfs4_file_get_access(fp, access);
585 static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
587 /* Common case is that there is no deny mode. */
589 /* Does this deny mode make sense? */
590 if (deny & ~NFS4_SHARE_DENY_BOTH)
593 if ((deny & NFS4_SHARE_DENY_READ) &&
594 atomic_read(&fp->fi_access[O_RDONLY]))
595 return nfserr_share_denied;
597 if ((deny & NFS4_SHARE_DENY_WRITE) &&
598 atomic_read(&fp->fi_access[O_WRONLY]))
599 return nfserr_share_denied;
604 static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
606 might_lock(&fp->fi_lock);
608 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
609 struct nfsd_file *f1 = NULL;
610 struct nfsd_file *f2 = NULL;
612 swap(f1, fp->fi_fds[oflag]);
613 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
614 swap(f2, fp->fi_fds[O_RDWR]);
615 spin_unlock(&fp->fi_lock);
623 static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
625 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
627 if (access & NFS4_SHARE_ACCESS_WRITE)
628 __nfs4_file_put_access(fp, O_WRONLY);
629 if (access & NFS4_SHARE_ACCESS_READ)
630 __nfs4_file_put_access(fp, O_RDONLY);
634 * Allocate a new open/delegation state counter. This is needed for
635 * pNFS for proper return on close semantics.
637 * Note that we only allocate it for pNFS-enabled exports, otherwise
638 * all pointers to struct nfs4_clnt_odstate are always NULL.
640 static struct nfs4_clnt_odstate *
641 alloc_clnt_odstate(struct nfs4_client *clp)
643 struct nfs4_clnt_odstate *co;
645 co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
648 refcount_set(&co->co_odcount, 1);
654 hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
656 struct nfs4_file *fp = co->co_file;
658 lockdep_assert_held(&fp->fi_lock);
659 list_add(&co->co_perfile, &fp->fi_clnt_odstate);
663 get_clnt_odstate(struct nfs4_clnt_odstate *co)
666 refcount_inc(&co->co_odcount);
670 put_clnt_odstate(struct nfs4_clnt_odstate *co)
672 struct nfs4_file *fp;
678 if (refcount_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
679 list_del(&co->co_perfile);
680 spin_unlock(&fp->fi_lock);
682 nfsd4_return_all_file_layouts(co->co_client, fp);
683 kmem_cache_free(odstate_slab, co);
687 static struct nfs4_clnt_odstate *
688 find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
690 struct nfs4_clnt_odstate *co;
691 struct nfs4_client *cl;
698 spin_lock(&fp->fi_lock);
699 list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
700 if (co->co_client == cl) {
701 get_clnt_odstate(co);
707 hash_clnt_odstate_locked(new);
709 spin_unlock(&fp->fi_lock);
713 struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab,
714 void (*sc_free)(struct nfs4_stid *))
716 struct nfs4_stid *stid;
719 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
723 idr_preload(GFP_KERNEL);
724 spin_lock(&cl->cl_lock);
725 /* Reserving 0 for start of file in nfsdfs "states" file: */
726 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 1, 0, GFP_NOWAIT);
727 spin_unlock(&cl->cl_lock);
732 stid->sc_free = sc_free;
733 stid->sc_client = cl;
734 stid->sc_stateid.si_opaque.so_id = new_id;
735 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
736 /* Will be incremented before return to client: */
737 refcount_set(&stid->sc_count, 1);
738 spin_lock_init(&stid->sc_lock);
739 INIT_LIST_HEAD(&stid->sc_cp_list);
742 * It shouldn't be a problem to reuse an opaque stateid value.
743 * I don't think it is for 4.1. But with 4.0 I worry that, for
744 * example, a stray write retransmission could be accepted by
745 * the server when it should have been rejected. Therefore,
746 * adopt a trick from the sctp code to attempt to maximize the
747 * amount of time until an id is reused, by ensuring they always
748 * "increase" (mod INT_MAX):
752 kmem_cache_free(slab, stid);
757 * Create a unique stateid_t to represent each COPY.
759 static int nfs4_init_cp_state(struct nfsd_net *nn, copy_stateid_t *stid,
760 unsigned char sc_type)
764 stid->stid.si_opaque.so_clid.cl_boot = (u32)nn->boot_time;
765 stid->stid.si_opaque.so_clid.cl_id = nn->s2s_cp_cl_id;
766 stid->sc_type = sc_type;
768 idr_preload(GFP_KERNEL);
769 spin_lock(&nn->s2s_cp_lock);
770 new_id = idr_alloc_cyclic(&nn->s2s_cp_stateids, stid, 0, 0, GFP_NOWAIT);
771 stid->stid.si_opaque.so_id = new_id;
772 spin_unlock(&nn->s2s_cp_lock);
779 int nfs4_init_copy_state(struct nfsd_net *nn, struct nfsd4_copy *copy)
781 return nfs4_init_cp_state(nn, ©->cp_stateid, NFS4_COPY_STID);
784 struct nfs4_cpntf_state *nfs4_alloc_init_cpntf_state(struct nfsd_net *nn,
785 struct nfs4_stid *p_stid)
787 struct nfs4_cpntf_state *cps;
789 cps = kzalloc(sizeof(struct nfs4_cpntf_state), GFP_KERNEL);
792 cps->cpntf_time = ktime_get_boottime_seconds();
793 refcount_set(&cps->cp_stateid.sc_count, 1);
794 if (!nfs4_init_cp_state(nn, &cps->cp_stateid, NFS4_COPYNOTIFY_STID))
796 spin_lock(&nn->s2s_cp_lock);
797 list_add(&cps->cp_list, &p_stid->sc_cp_list);
798 spin_unlock(&nn->s2s_cp_lock);
805 void nfs4_free_copy_state(struct nfsd4_copy *copy)
809 WARN_ON_ONCE(copy->cp_stateid.sc_type != NFS4_COPY_STID);
810 nn = net_generic(copy->cp_clp->net, nfsd_net_id);
811 spin_lock(&nn->s2s_cp_lock);
812 idr_remove(&nn->s2s_cp_stateids,
813 copy->cp_stateid.stid.si_opaque.so_id);
814 spin_unlock(&nn->s2s_cp_lock);
817 static void nfs4_free_cpntf_statelist(struct net *net, struct nfs4_stid *stid)
819 struct nfs4_cpntf_state *cps;
822 nn = net_generic(net, nfsd_net_id);
823 spin_lock(&nn->s2s_cp_lock);
824 while (!list_empty(&stid->sc_cp_list)) {
825 cps = list_first_entry(&stid->sc_cp_list,
826 struct nfs4_cpntf_state, cp_list);
827 _free_cpntf_state_locked(nn, cps);
829 spin_unlock(&nn->s2s_cp_lock);
832 static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
834 struct nfs4_stid *stid;
836 stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid);
840 return openlockstateid(stid);
843 static void nfs4_free_deleg(struct nfs4_stid *stid)
845 kmem_cache_free(deleg_slab, stid);
846 atomic_long_dec(&num_delegations);
850 * When we recall a delegation, we should be careful not to hand it
851 * out again straight away.
852 * To ensure this we keep a pair of bloom filters ('new' and 'old')
853 * in which the filehandles of recalled delegations are "stored".
854 * If a filehandle appear in either filter, a delegation is blocked.
855 * When a delegation is recalled, the filehandle is stored in the "new"
857 * Every 30 seconds we swap the filters and clear the "new" one,
858 * unless both are empty of course.
860 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
861 * low 3 bytes as hash-table indices.
863 * 'blocked_delegations_lock', which is always taken in block_delegations(),
864 * is used to manage concurrent access. Testing does not need the lock
865 * except when swapping the two filters.
867 static DEFINE_SPINLOCK(blocked_delegations_lock);
868 static struct bloom_pair {
869 int entries, old_entries;
871 int new; /* index into 'set' */
872 DECLARE_BITMAP(set[2], 256);
873 } blocked_delegations;
875 static int delegation_blocked(struct knfsd_fh *fh)
878 struct bloom_pair *bd = &blocked_delegations;
880 if (bd->entries == 0)
882 if (ktime_get_seconds() - bd->swap_time > 30) {
883 spin_lock(&blocked_delegations_lock);
884 if (ktime_get_seconds() - bd->swap_time > 30) {
885 bd->entries -= bd->old_entries;
886 bd->old_entries = bd->entries;
887 memset(bd->set[bd->new], 0,
890 bd->swap_time = ktime_get_seconds();
892 spin_unlock(&blocked_delegations_lock);
894 hash = jhash(&fh->fh_base, fh->fh_size, 0);
895 if (test_bit(hash&255, bd->set[0]) &&
896 test_bit((hash>>8)&255, bd->set[0]) &&
897 test_bit((hash>>16)&255, bd->set[0]))
900 if (test_bit(hash&255, bd->set[1]) &&
901 test_bit((hash>>8)&255, bd->set[1]) &&
902 test_bit((hash>>16)&255, bd->set[1]))
908 static void block_delegations(struct knfsd_fh *fh)
911 struct bloom_pair *bd = &blocked_delegations;
913 hash = jhash(&fh->fh_base, fh->fh_size, 0);
915 spin_lock(&blocked_delegations_lock);
916 __set_bit(hash&255, bd->set[bd->new]);
917 __set_bit((hash>>8)&255, bd->set[bd->new]);
918 __set_bit((hash>>16)&255, bd->set[bd->new]);
919 if (bd->entries == 0)
920 bd->swap_time = ktime_get_seconds();
922 spin_unlock(&blocked_delegations_lock);
925 static struct nfs4_delegation *
926 alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
927 struct svc_fh *current_fh,
928 struct nfs4_clnt_odstate *odstate)
930 struct nfs4_delegation *dp;
933 dprintk("NFSD alloc_init_deleg\n");
934 n = atomic_long_inc_return(&num_delegations);
935 if (n < 0 || n > max_delegations)
937 if (delegation_blocked(¤t_fh->fh_handle))
939 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg));
944 * delegation seqid's are never incremented. The 4.1 special
945 * meaning of seqid 0 isn't meaningful, really, but let's avoid
946 * 0 anyway just for consistency and use 1:
948 dp->dl_stid.sc_stateid.si_generation = 1;
949 INIT_LIST_HEAD(&dp->dl_perfile);
950 INIT_LIST_HEAD(&dp->dl_perclnt);
951 INIT_LIST_HEAD(&dp->dl_recall_lru);
952 dp->dl_clnt_odstate = odstate;
953 get_clnt_odstate(odstate);
954 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
956 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
957 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
959 dp->dl_stid.sc_file = fp;
962 atomic_long_dec(&num_delegations);
967 nfs4_put_stid(struct nfs4_stid *s)
969 struct nfs4_file *fp = s->sc_file;
970 struct nfs4_client *clp = s->sc_client;
972 might_lock(&clp->cl_lock);
974 if (!refcount_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
975 wake_up_all(&close_wq);
978 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
979 nfs4_free_cpntf_statelist(clp->net, s);
980 spin_unlock(&clp->cl_lock);
987 nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
989 stateid_t *src = &stid->sc_stateid;
991 spin_lock(&stid->sc_lock);
992 if (unlikely(++src->si_generation == 0))
993 src->si_generation = 1;
994 memcpy(dst, src, sizeof(*dst));
995 spin_unlock(&stid->sc_lock);
998 static void put_deleg_file(struct nfs4_file *fp)
1000 struct nfsd_file *nf = NULL;
1002 spin_lock(&fp->fi_lock);
1003 if (--fp->fi_delegees == 0)
1004 swap(nf, fp->fi_deleg_file);
1005 spin_unlock(&fp->fi_lock);
1011 static void nfs4_unlock_deleg_lease(struct nfs4_delegation *dp)
1013 struct nfs4_file *fp = dp->dl_stid.sc_file;
1014 struct nfsd_file *nf = fp->fi_deleg_file;
1016 WARN_ON_ONCE(!fp->fi_delegees);
1018 vfs_setlease(nf->nf_file, F_UNLCK, NULL, (void **)&dp);
1022 static void destroy_unhashed_deleg(struct nfs4_delegation *dp)
1024 put_clnt_odstate(dp->dl_clnt_odstate);
1025 nfs4_unlock_deleg_lease(dp);
1026 nfs4_put_stid(&dp->dl_stid);
1029 void nfs4_unhash_stid(struct nfs4_stid *s)
1035 * nfs4_delegation_exists - Discover if this delegation already exists
1036 * @clp: a pointer to the nfs4_client we're granting a delegation to
1037 * @fp: a pointer to the nfs4_file we're granting a delegation on
1040 * On success: true iff an existing delegation is found
1044 nfs4_delegation_exists(struct nfs4_client *clp, struct nfs4_file *fp)
1046 struct nfs4_delegation *searchdp = NULL;
1047 struct nfs4_client *searchclp = NULL;
1049 lockdep_assert_held(&state_lock);
1050 lockdep_assert_held(&fp->fi_lock);
1052 list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
1053 searchclp = searchdp->dl_stid.sc_client;
1054 if (clp == searchclp) {
1062 * hash_delegation_locked - Add a delegation to the appropriate lists
1063 * @dp: a pointer to the nfs4_delegation we are adding.
1064 * @fp: a pointer to the nfs4_file we're granting a delegation on
1067 * On success: NULL if the delegation was successfully hashed.
1069 * On error: -EAGAIN if one was previously granted to this
1070 * nfs4_client for this nfs4_file. Delegation is not hashed.
1075 hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
1077 struct nfs4_client *clp = dp->dl_stid.sc_client;
1079 lockdep_assert_held(&state_lock);
1080 lockdep_assert_held(&fp->fi_lock);
1082 if (nfs4_delegation_exists(clp, fp))
1084 refcount_inc(&dp->dl_stid.sc_count);
1085 dp->dl_stid.sc_type = NFS4_DELEG_STID;
1086 list_add(&dp->dl_perfile, &fp->fi_delegations);
1087 list_add(&dp->dl_perclnt, &clp->cl_delegations);
1092 unhash_delegation_locked(struct nfs4_delegation *dp)
1094 struct nfs4_file *fp = dp->dl_stid.sc_file;
1096 lockdep_assert_held(&state_lock);
1098 if (list_empty(&dp->dl_perfile))
1101 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
1102 /* Ensure that deleg break won't try to requeue it */
1104 spin_lock(&fp->fi_lock);
1105 list_del_init(&dp->dl_perclnt);
1106 list_del_init(&dp->dl_recall_lru);
1107 list_del_init(&dp->dl_perfile);
1108 spin_unlock(&fp->fi_lock);
1112 static void destroy_delegation(struct nfs4_delegation *dp)
1116 spin_lock(&state_lock);
1117 unhashed = unhash_delegation_locked(dp);
1118 spin_unlock(&state_lock);
1120 destroy_unhashed_deleg(dp);
1123 static void revoke_delegation(struct nfs4_delegation *dp)
1125 struct nfs4_client *clp = dp->dl_stid.sc_client;
1127 WARN_ON(!list_empty(&dp->dl_recall_lru));
1129 if (clp->cl_minorversion) {
1130 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
1131 refcount_inc(&dp->dl_stid.sc_count);
1132 spin_lock(&clp->cl_lock);
1133 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
1134 spin_unlock(&clp->cl_lock);
1136 destroy_unhashed_deleg(dp);
1143 static unsigned int clientid_hashval(u32 id)
1145 return id & CLIENT_HASH_MASK;
1148 static unsigned int clientstr_hashval(struct xdr_netobj name)
1150 return opaque_hashval(name.data, 8) & CLIENT_HASH_MASK;
1154 * We store the NONE, READ, WRITE, and BOTH bits separately in the
1155 * st_{access,deny}_bmap field of the stateid, in order to track not
1156 * only what share bits are currently in force, but also what
1157 * combinations of share bits previous opens have used. This allows us
1158 * to enforce the recommendation of rfc 3530 14.2.19 that the server
1159 * return an error if the client attempt to downgrade to a combination
1160 * of share bits not explicable by closing some of its previous opens.
1162 * XXX: This enforcement is actually incomplete, since we don't keep
1163 * track of access/deny bit combinations; so, e.g., we allow:
1165 * OPEN allow read, deny write
1166 * OPEN allow both, deny none
1167 * DOWNGRADE allow read, deny none
1169 * which we should reject.
1172 bmap_to_share_mode(unsigned long bmap) {
1174 unsigned int access = 0;
1176 for (i = 1; i < 4; i++) {
1177 if (test_bit(i, &bmap))
1183 /* set share access for a given stateid */
1185 set_access(u32 access, struct nfs4_ol_stateid *stp)
1187 unsigned char mask = 1 << access;
1189 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1190 stp->st_access_bmap |= mask;
1193 /* clear share access for a given stateid */
1195 clear_access(u32 access, struct nfs4_ol_stateid *stp)
1197 unsigned char mask = 1 << access;
1199 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1200 stp->st_access_bmap &= ~mask;
1203 /* test whether a given stateid has access */
1205 test_access(u32 access, struct nfs4_ol_stateid *stp)
1207 unsigned char mask = 1 << access;
1209 return (bool)(stp->st_access_bmap & mask);
1212 /* set share deny for a given stateid */
1214 set_deny(u32 deny, struct nfs4_ol_stateid *stp)
1216 unsigned char mask = 1 << deny;
1218 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1219 stp->st_deny_bmap |= mask;
1222 /* clear share deny for a given stateid */
1224 clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
1226 unsigned char mask = 1 << deny;
1228 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1229 stp->st_deny_bmap &= ~mask;
1232 /* test whether a given stateid is denying specific access */
1234 test_deny(u32 deny, struct nfs4_ol_stateid *stp)
1236 unsigned char mask = 1 << deny;
1238 return (bool)(stp->st_deny_bmap & mask);
1241 static int nfs4_access_to_omode(u32 access)
1243 switch (access & NFS4_SHARE_ACCESS_BOTH) {
1244 case NFS4_SHARE_ACCESS_READ:
1246 case NFS4_SHARE_ACCESS_WRITE:
1248 case NFS4_SHARE_ACCESS_BOTH:
1256 * A stateid that had a deny mode associated with it is being released
1257 * or downgraded. Recalculate the deny mode on the file.
1260 recalculate_deny_mode(struct nfs4_file *fp)
1262 struct nfs4_ol_stateid *stp;
1264 spin_lock(&fp->fi_lock);
1265 fp->fi_share_deny = 0;
1266 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
1267 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
1268 spin_unlock(&fp->fi_lock);
1272 reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
1275 bool change = false;
1277 for (i = 1; i < 4; i++) {
1278 if ((i & deny) != i) {
1284 /* Recalculate per-file deny mode if there was a change */
1286 recalculate_deny_mode(stp->st_stid.sc_file);
1289 /* release all access and file references for a given stateid */
1291 release_all_access(struct nfs4_ol_stateid *stp)
1294 struct nfs4_file *fp = stp->st_stid.sc_file;
1296 if (fp && stp->st_deny_bmap != 0)
1297 recalculate_deny_mode(fp);
1299 for (i = 1; i < 4; i++) {
1300 if (test_access(i, stp))
1301 nfs4_file_put_access(stp->st_stid.sc_file, i);
1302 clear_access(i, stp);
1306 static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1308 kfree(sop->so_owner.data);
1309 sop->so_ops->so_free(sop);
1312 static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1314 struct nfs4_client *clp = sop->so_client;
1316 might_lock(&clp->cl_lock);
1318 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
1320 sop->so_ops->so_unhash(sop);
1321 spin_unlock(&clp->cl_lock);
1322 nfs4_free_stateowner(sop);
1326 nfs4_ol_stateid_unhashed(const struct nfs4_ol_stateid *stp)
1328 return list_empty(&stp->st_perfile);
1331 static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
1333 struct nfs4_file *fp = stp->st_stid.sc_file;
1335 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1337 if (list_empty(&stp->st_perfile))
1340 spin_lock(&fp->fi_lock);
1341 list_del_init(&stp->st_perfile);
1342 spin_unlock(&fp->fi_lock);
1343 list_del(&stp->st_perstateowner);
1347 static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
1349 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1351 put_clnt_odstate(stp->st_clnt_odstate);
1352 release_all_access(stp);
1353 if (stp->st_stateowner)
1354 nfs4_put_stateowner(stp->st_stateowner);
1355 kmem_cache_free(stateid_slab, stid);
1358 static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
1360 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1361 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
1362 struct nfsd_file *nf;
1364 nf = find_any_file(stp->st_stid.sc_file);
1366 get_file(nf->nf_file);
1367 filp_close(nf->nf_file, (fl_owner_t)lo);
1370 nfs4_free_ol_stateid(stid);
1374 * Put the persistent reference to an already unhashed generic stateid, while
1375 * holding the cl_lock. If it's the last reference, then put it onto the
1376 * reaplist for later destruction.
1378 static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1379 struct list_head *reaplist)
1381 struct nfs4_stid *s = &stp->st_stid;
1382 struct nfs4_client *clp = s->sc_client;
1384 lockdep_assert_held(&clp->cl_lock);
1386 WARN_ON_ONCE(!list_empty(&stp->st_locks));
1388 if (!refcount_dec_and_test(&s->sc_count)) {
1389 wake_up_all(&close_wq);
1393 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1394 list_add(&stp->st_locks, reaplist);
1397 static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
1399 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1401 if (!unhash_ol_stateid(stp))
1403 list_del_init(&stp->st_locks);
1404 nfs4_unhash_stid(&stp->st_stid);
1408 static void release_lock_stateid(struct nfs4_ol_stateid *stp)
1410 struct nfs4_client *clp = stp->st_stid.sc_client;
1413 spin_lock(&clp->cl_lock);
1414 unhashed = unhash_lock_stateid(stp);
1415 spin_unlock(&clp->cl_lock);
1417 nfs4_put_stid(&stp->st_stid);
1420 static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
1422 struct nfs4_client *clp = lo->lo_owner.so_client;
1424 lockdep_assert_held(&clp->cl_lock);
1426 list_del_init(&lo->lo_owner.so_strhash);
1430 * Free a list of generic stateids that were collected earlier after being
1434 free_ol_stateid_reaplist(struct list_head *reaplist)
1436 struct nfs4_ol_stateid *stp;
1437 struct nfs4_file *fp;
1441 while (!list_empty(reaplist)) {
1442 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1444 list_del(&stp->st_locks);
1445 fp = stp->st_stid.sc_file;
1446 stp->st_stid.sc_free(&stp->st_stid);
1452 static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1453 struct list_head *reaplist)
1455 struct nfs4_ol_stateid *stp;
1457 lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1459 while (!list_empty(&open_stp->st_locks)) {
1460 stp = list_entry(open_stp->st_locks.next,
1461 struct nfs4_ol_stateid, st_locks);
1462 WARN_ON(!unhash_lock_stateid(stp));
1463 put_ol_stateid_locked(stp, reaplist);
1467 static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
1468 struct list_head *reaplist)
1470 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1472 if (!unhash_ol_stateid(stp))
1474 release_open_stateid_locks(stp, reaplist);
1478 static void release_open_stateid(struct nfs4_ol_stateid *stp)
1480 LIST_HEAD(reaplist);
1482 spin_lock(&stp->st_stid.sc_client->cl_lock);
1483 if (unhash_open_stateid(stp, &reaplist))
1484 put_ol_stateid_locked(stp, &reaplist);
1485 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1486 free_ol_stateid_reaplist(&reaplist);
1489 static void unhash_openowner_locked(struct nfs4_openowner *oo)
1491 struct nfs4_client *clp = oo->oo_owner.so_client;
1493 lockdep_assert_held(&clp->cl_lock);
1495 list_del_init(&oo->oo_owner.so_strhash);
1496 list_del_init(&oo->oo_perclient);
1499 static void release_last_closed_stateid(struct nfs4_openowner *oo)
1501 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1503 struct nfs4_ol_stateid *s;
1505 spin_lock(&nn->client_lock);
1506 s = oo->oo_last_closed_stid;
1508 list_del_init(&oo->oo_close_lru);
1509 oo->oo_last_closed_stid = NULL;
1511 spin_unlock(&nn->client_lock);
1513 nfs4_put_stid(&s->st_stid);
1516 static void release_openowner(struct nfs4_openowner *oo)
1518 struct nfs4_ol_stateid *stp;
1519 struct nfs4_client *clp = oo->oo_owner.so_client;
1520 struct list_head reaplist;
1522 INIT_LIST_HEAD(&reaplist);
1524 spin_lock(&clp->cl_lock);
1525 unhash_openowner_locked(oo);
1526 while (!list_empty(&oo->oo_owner.so_stateids)) {
1527 stp = list_first_entry(&oo->oo_owner.so_stateids,
1528 struct nfs4_ol_stateid, st_perstateowner);
1529 if (unhash_open_stateid(stp, &reaplist))
1530 put_ol_stateid_locked(stp, &reaplist);
1532 spin_unlock(&clp->cl_lock);
1533 free_ol_stateid_reaplist(&reaplist);
1534 release_last_closed_stateid(oo);
1535 nfs4_put_stateowner(&oo->oo_owner);
1539 hash_sessionid(struct nfs4_sessionid *sessionid)
1541 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1543 return sid->sequence % SESSION_HASH_SIZE;
1546 #ifdef CONFIG_SUNRPC_DEBUG
1548 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1550 u32 *ptr = (u32 *)(&sessionid->data[0]);
1551 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1555 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1561 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1562 * won't be used for replay.
1564 void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1566 struct nfs4_stateowner *so = cstate->replay_owner;
1568 if (nfserr == nfserr_replay_me)
1571 if (!seqid_mutating_err(ntohl(nfserr))) {
1572 nfsd4_cstate_clear_replay(cstate);
1577 if (so->so_is_open_owner)
1578 release_last_closed_stateid(openowner(so));
1584 gen_sessionid(struct nfsd4_session *ses)
1586 struct nfs4_client *clp = ses->se_client;
1587 struct nfsd4_sessionid *sid;
1589 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1590 sid->clientid = clp->cl_clientid;
1591 sid->sequence = current_sessionid++;
1596 * The protocol defines ca_maxresponssize_cached to include the size of
1597 * the rpc header, but all we need to cache is the data starting after
1598 * the end of the initial SEQUENCE operation--the rest we regenerate
1599 * each time. Therefore we can advertise a ca_maxresponssize_cached
1600 * value that is the number of bytes in our cache plus a few additional
1601 * bytes. In order to stay on the safe side, and not promise more than
1602 * we can cache, those additional bytes must be the minimum possible: 24
1603 * bytes of rpc header (xid through accept state, with AUTH_NULL
1604 * verifier), 12 for the compound header (with zero-length tag), and 44
1605 * for the SEQUENCE op response:
1607 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1610 free_session_slots(struct nfsd4_session *ses)
1614 for (i = 0; i < ses->se_fchannel.maxreqs; i++) {
1615 free_svc_cred(&ses->se_slots[i]->sl_cred);
1616 kfree(ses->se_slots[i]);
1621 * We don't actually need to cache the rpc and session headers, so we
1622 * can allocate a little less for each slot:
1624 static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
1628 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1631 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1632 return size + sizeof(struct nfsd4_slot);
1636 * XXX: If we run out of reserved DRC memory we could (up to a point)
1637 * re-negotiate active sessions and reduce their slot usage to make
1638 * room for new connections. For now we just fail the create session.
1640 static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
1642 u32 slotsize = slot_bytes(ca);
1643 u32 num = ca->maxreqs;
1644 unsigned long avail, total_avail;
1645 unsigned int scale_factor;
1647 spin_lock(&nfsd_drc_lock);
1648 if (nfsd_drc_max_mem > nfsd_drc_mem_used)
1649 total_avail = nfsd_drc_max_mem - nfsd_drc_mem_used;
1651 /* We have handed out more space than we chose in
1652 * set_max_drc() to allow. That isn't really a
1653 * problem as long as that doesn't make us think we
1654 * have lots more due to integer overflow.
1657 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, total_avail);
1659 * Never use more than a fraction of the remaining memory,
1660 * unless it's the only way to give this client a slot.
1661 * The chosen fraction is either 1/8 or 1/number of threads,
1662 * whichever is smaller. This ensures there are adequate
1663 * slots to support multiple clients per thread.
1664 * Give the client one slot even if that would require
1665 * over-allocation--it is better than failure.
1667 scale_factor = max_t(unsigned int, 8, nn->nfsd_serv->sv_nrthreads);
1669 avail = clamp_t(unsigned long, avail, slotsize,
1670 total_avail/scale_factor);
1671 num = min_t(int, num, avail / slotsize);
1672 num = max_t(int, num, 1);
1673 nfsd_drc_mem_used += num * slotsize;
1674 spin_unlock(&nfsd_drc_lock);
1679 static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
1681 int slotsize = slot_bytes(ca);
1683 spin_lock(&nfsd_drc_lock);
1684 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
1685 spin_unlock(&nfsd_drc_lock);
1688 static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1689 struct nfsd4_channel_attrs *battrs)
1691 int numslots = fattrs->maxreqs;
1692 int slotsize = slot_bytes(fattrs);
1693 struct nfsd4_session *new;
1696 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
1697 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1698 mem = numslots * sizeof(struct nfsd4_slot *);
1700 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
1703 /* allocate each struct nfsd4_slot and data cache in one piece */
1704 for (i = 0; i < numslots; i++) {
1705 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
1706 if (!new->se_slots[i])
1710 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1711 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1716 kfree(new->se_slots[i]);
1721 static void free_conn(struct nfsd4_conn *c)
1723 svc_xprt_put(c->cn_xprt);
1727 static void nfsd4_conn_lost(struct svc_xpt_user *u)
1729 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1730 struct nfs4_client *clp = c->cn_session->se_client;
1732 spin_lock(&clp->cl_lock);
1733 if (!list_empty(&c->cn_persession)) {
1734 list_del(&c->cn_persession);
1737 nfsd4_probe_callback(clp);
1738 spin_unlock(&clp->cl_lock);
1741 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
1743 struct nfsd4_conn *conn;
1745 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1748 svc_xprt_get(rqstp->rq_xprt);
1749 conn->cn_xprt = rqstp->rq_xprt;
1750 conn->cn_flags = flags;
1751 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1755 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1757 conn->cn_session = ses;
1758 list_add(&conn->cn_persession, &ses->se_conns);
1761 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1763 struct nfs4_client *clp = ses->se_client;
1765 spin_lock(&clp->cl_lock);
1766 __nfsd4_hash_conn(conn, ses);
1767 spin_unlock(&clp->cl_lock);
1770 static int nfsd4_register_conn(struct nfsd4_conn *conn)
1772 conn->cn_xpt_user.callback = nfsd4_conn_lost;
1773 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
1776 static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
1780 nfsd4_hash_conn(conn, ses);
1781 ret = nfsd4_register_conn(conn);
1783 /* oops; xprt is already down: */
1784 nfsd4_conn_lost(&conn->cn_xpt_user);
1785 /* We may have gained or lost a callback channel: */
1786 nfsd4_probe_callback_sync(ses->se_client);
1789 static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
1791 u32 dir = NFS4_CDFC4_FORE;
1793 if (cses->flags & SESSION4_BACK_CHAN)
1794 dir |= NFS4_CDFC4_BACK;
1795 return alloc_conn(rqstp, dir);
1798 /* must be called under client_lock */
1799 static void nfsd4_del_conns(struct nfsd4_session *s)
1801 struct nfs4_client *clp = s->se_client;
1802 struct nfsd4_conn *c;
1804 spin_lock(&clp->cl_lock);
1805 while (!list_empty(&s->se_conns)) {
1806 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1807 list_del_init(&c->cn_persession);
1808 spin_unlock(&clp->cl_lock);
1810 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1813 spin_lock(&clp->cl_lock);
1815 spin_unlock(&clp->cl_lock);
1818 static void __free_session(struct nfsd4_session *ses)
1820 free_session_slots(ses);
1824 static void free_session(struct nfsd4_session *ses)
1826 nfsd4_del_conns(ses);
1827 nfsd4_put_drc_mem(&ses->se_fchannel);
1828 __free_session(ses);
1831 static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
1834 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1836 new->se_client = clp;
1839 INIT_LIST_HEAD(&new->se_conns);
1841 new->se_cb_seq_nr = 1;
1842 new->se_flags = cses->flags;
1843 new->se_cb_prog = cses->callback_prog;
1844 new->se_cb_sec = cses->cb_sec;
1845 atomic_set(&new->se_ref, 0);
1846 idx = hash_sessionid(&new->se_sessionid);
1847 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
1848 spin_lock(&clp->cl_lock);
1849 list_add(&new->se_perclnt, &clp->cl_sessions);
1850 spin_unlock(&clp->cl_lock);
1853 struct sockaddr *sa = svc_addr(rqstp);
1855 * This is a little silly; with sessions there's no real
1856 * use for the callback address. Use the peer address
1857 * as a reasonable default for now, but consider fixing
1858 * the rpc client not to require an address in the
1861 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1862 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
1866 /* caller must hold client_lock */
1867 static struct nfsd4_session *
1868 __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
1870 struct nfsd4_session *elem;
1872 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1874 lockdep_assert_held(&nn->client_lock);
1876 dump_sessionid(__func__, sessionid);
1877 idx = hash_sessionid(sessionid);
1878 /* Search in the appropriate list */
1879 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
1880 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1881 NFS4_MAX_SESSIONID_LEN)) {
1886 dprintk("%s: session not found\n", __func__);
1890 static struct nfsd4_session *
1891 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1894 struct nfsd4_session *session;
1895 __be32 status = nfserr_badsession;
1897 session = __find_in_sessionid_hashtbl(sessionid, net);
1900 status = nfsd4_get_session_locked(session);
1908 /* caller must hold client_lock */
1910 unhash_session(struct nfsd4_session *ses)
1912 struct nfs4_client *clp = ses->se_client;
1913 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1915 lockdep_assert_held(&nn->client_lock);
1917 list_del(&ses->se_hash);
1918 spin_lock(&ses->se_client->cl_lock);
1919 list_del(&ses->se_perclnt);
1920 spin_unlock(&ses->se_client->cl_lock);
1923 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1925 STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
1928 * We're assuming the clid was not given out from a boot
1929 * precisely 2^32 (about 136 years) before this one. That seems
1930 * a safe assumption:
1932 if (clid->cl_boot == (u32)nn->boot_time)
1934 trace_nfsd_clid_stale(clid);
1939 * XXX Should we use a slab cache ?
1940 * This type of memory management is somewhat inefficient, but we use it
1941 * anyway since SETCLIENTID is not a common operation.
1943 static struct nfs4_client *alloc_client(struct xdr_netobj name)
1945 struct nfs4_client *clp;
1948 clp = kmem_cache_zalloc(client_slab, GFP_KERNEL);
1951 xdr_netobj_dup(&clp->cl_name, &name, GFP_KERNEL);
1952 if (clp->cl_name.data == NULL)
1954 clp->cl_ownerstr_hashtbl = kmalloc_array(OWNER_HASH_SIZE,
1955 sizeof(struct list_head),
1957 if (!clp->cl_ownerstr_hashtbl)
1958 goto err_no_hashtbl;
1959 for (i = 0; i < OWNER_HASH_SIZE; i++)
1960 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
1961 INIT_LIST_HEAD(&clp->cl_sessions);
1962 idr_init(&clp->cl_stateids);
1963 atomic_set(&clp->cl_rpc_users, 0);
1964 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1965 INIT_LIST_HEAD(&clp->cl_idhash);
1966 INIT_LIST_HEAD(&clp->cl_openowners);
1967 INIT_LIST_HEAD(&clp->cl_delegations);
1968 INIT_LIST_HEAD(&clp->cl_lru);
1969 INIT_LIST_HEAD(&clp->cl_revoked);
1970 #ifdef CONFIG_NFSD_PNFS
1971 INIT_LIST_HEAD(&clp->cl_lo_states);
1973 INIT_LIST_HEAD(&clp->async_copies);
1974 spin_lock_init(&clp->async_lock);
1975 spin_lock_init(&clp->cl_lock);
1976 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1979 kfree(clp->cl_name.data);
1981 kmem_cache_free(client_slab, clp);
1985 static void __free_client(struct kref *k)
1987 struct nfsdfs_client *c = container_of(k, struct nfsdfs_client, cl_ref);
1988 struct nfs4_client *clp = container_of(c, struct nfs4_client, cl_nfsdfs);
1990 free_svc_cred(&clp->cl_cred);
1991 kfree(clp->cl_ownerstr_hashtbl);
1992 kfree(clp->cl_name.data);
1993 kfree(clp->cl_nii_domain.data);
1994 kfree(clp->cl_nii_name.data);
1995 idr_destroy(&clp->cl_stateids);
1996 kmem_cache_free(client_slab, clp);
1999 static void drop_client(struct nfs4_client *clp)
2001 kref_put(&clp->cl_nfsdfs.cl_ref, __free_client);
2005 free_client(struct nfs4_client *clp)
2007 while (!list_empty(&clp->cl_sessions)) {
2008 struct nfsd4_session *ses;
2009 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
2011 list_del(&ses->se_perclnt);
2012 WARN_ON_ONCE(atomic_read(&ses->se_ref));
2015 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
2016 if (clp->cl_nfsd_dentry) {
2017 nfsd_client_rmdir(clp->cl_nfsd_dentry);
2018 clp->cl_nfsd_dentry = NULL;
2019 wake_up_all(&expiry_wq);
2024 /* must be called under the client_lock */
2026 unhash_client_locked(struct nfs4_client *clp)
2028 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2029 struct nfsd4_session *ses;
2031 lockdep_assert_held(&nn->client_lock);
2033 /* Mark the client as expired! */
2035 /* Make it invisible */
2036 if (!list_empty(&clp->cl_idhash)) {
2037 list_del_init(&clp->cl_idhash);
2038 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
2039 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
2041 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
2043 list_del_init(&clp->cl_lru);
2044 spin_lock(&clp->cl_lock);
2045 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
2046 list_del_init(&ses->se_hash);
2047 spin_unlock(&clp->cl_lock);
2051 unhash_client(struct nfs4_client *clp)
2053 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2055 spin_lock(&nn->client_lock);
2056 unhash_client_locked(clp);
2057 spin_unlock(&nn->client_lock);
2060 static __be32 mark_client_expired_locked(struct nfs4_client *clp)
2062 if (atomic_read(&clp->cl_rpc_users))
2063 return nfserr_jukebox;
2064 unhash_client_locked(clp);
2069 __destroy_client(struct nfs4_client *clp)
2072 struct nfs4_openowner *oo;
2073 struct nfs4_delegation *dp;
2074 struct list_head reaplist;
2076 INIT_LIST_HEAD(&reaplist);
2077 spin_lock(&state_lock);
2078 while (!list_empty(&clp->cl_delegations)) {
2079 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
2080 WARN_ON(!unhash_delegation_locked(dp));
2081 list_add(&dp->dl_recall_lru, &reaplist);
2083 spin_unlock(&state_lock);
2084 while (!list_empty(&reaplist)) {
2085 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
2086 list_del_init(&dp->dl_recall_lru);
2087 destroy_unhashed_deleg(dp);
2089 while (!list_empty(&clp->cl_revoked)) {
2090 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
2091 list_del_init(&dp->dl_recall_lru);
2092 nfs4_put_stid(&dp->dl_stid);
2094 while (!list_empty(&clp->cl_openowners)) {
2095 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
2096 nfs4_get_stateowner(&oo->oo_owner);
2097 release_openowner(oo);
2099 for (i = 0; i < OWNER_HASH_SIZE; i++) {
2100 struct nfs4_stateowner *so, *tmp;
2102 list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i],
2104 /* Should be no openowners at this point */
2105 WARN_ON_ONCE(so->so_is_open_owner);
2106 remove_blocked_locks(lockowner(so));
2109 nfsd4_return_all_client_layouts(clp);
2110 nfsd4_shutdown_copy(clp);
2111 nfsd4_shutdown_callback(clp);
2112 if (clp->cl_cb_conn.cb_xprt)
2113 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
2115 wake_up_all(&expiry_wq);
2119 destroy_client(struct nfs4_client *clp)
2122 __destroy_client(clp);
2125 static void inc_reclaim_complete(struct nfs4_client *clp)
2127 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2129 if (!nn->track_reclaim_completes)
2131 if (!nfsd4_find_reclaim_client(clp->cl_name, nn))
2133 if (atomic_inc_return(&nn->nr_reclaim_complete) ==
2134 nn->reclaim_str_hashtbl_size) {
2135 printk(KERN_INFO "NFSD: all clients done reclaiming, ending NFSv4 grace period (net %x)\n",
2137 nfsd4_end_grace(nn);
2141 static void expire_client(struct nfs4_client *clp)
2144 nfsd4_client_record_remove(clp);
2145 __destroy_client(clp);
2148 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
2150 memcpy(target->cl_verifier.data, source->data,
2151 sizeof(target->cl_verifier.data));
2154 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
2156 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
2157 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
2160 static int copy_cred(struct svc_cred *target, struct svc_cred *source)
2162 target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL);
2163 target->cr_raw_principal = kstrdup(source->cr_raw_principal,
2165 target->cr_targ_princ = kstrdup(source->cr_targ_princ, GFP_KERNEL);
2166 if ((source->cr_principal && !target->cr_principal) ||
2167 (source->cr_raw_principal && !target->cr_raw_principal) ||
2168 (source->cr_targ_princ && !target->cr_targ_princ))
2171 target->cr_flavor = source->cr_flavor;
2172 target->cr_uid = source->cr_uid;
2173 target->cr_gid = source->cr_gid;
2174 target->cr_group_info = source->cr_group_info;
2175 get_group_info(target->cr_group_info);
2176 target->cr_gss_mech = source->cr_gss_mech;
2177 if (source->cr_gss_mech)
2178 gss_mech_get(source->cr_gss_mech);
2183 compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
2185 if (o1->len < o2->len)
2187 if (o1->len > o2->len)
2189 return memcmp(o1->data, o2->data, o1->len);
2193 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
2195 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
2199 same_clid(clientid_t *cl1, clientid_t *cl2)
2201 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
2204 static bool groups_equal(struct group_info *g1, struct group_info *g2)
2208 if (g1->ngroups != g2->ngroups)
2210 for (i=0; i<g1->ngroups; i++)
2211 if (!gid_eq(g1->gid[i], g2->gid[i]))
2217 * RFC 3530 language requires clid_inuse be returned when the
2218 * "principal" associated with a requests differs from that previously
2219 * used. We use uid, gid's, and gss principal string as our best
2220 * approximation. We also don't want to allow non-gss use of a client
2221 * established using gss: in theory cr_principal should catch that
2222 * change, but in practice cr_principal can be null even in the gss case
2223 * since gssd doesn't always pass down a principal string.
2225 static bool is_gss_cred(struct svc_cred *cr)
2227 /* Is cr_flavor one of the gss "pseudoflavors"?: */
2228 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
2233 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
2235 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
2236 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
2237 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
2238 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
2240 /* XXX: check that cr_targ_princ fields match ? */
2241 if (cr1->cr_principal == cr2->cr_principal)
2243 if (!cr1->cr_principal || !cr2->cr_principal)
2245 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
2248 static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
2250 struct svc_cred *cr = &rqstp->rq_cred;
2253 if (!cr->cr_gss_mech)
2255 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
2256 return service == RPC_GSS_SVC_INTEGRITY ||
2257 service == RPC_GSS_SVC_PRIVACY;
2260 bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
2262 struct svc_cred *cr = &rqstp->rq_cred;
2264 if (!cl->cl_mach_cred)
2266 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
2268 if (!svc_rqst_integrity_protected(rqstp))
2270 if (cl->cl_cred.cr_raw_principal)
2271 return 0 == strcmp(cl->cl_cred.cr_raw_principal,
2272 cr->cr_raw_principal);
2273 if (!cr->cr_principal)
2275 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
2278 static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
2283 * This is opaque to client, so no need to byte-swap. Use
2284 * __force to keep sparse happy
2286 verf[0] = (__force __be32)(u32)ktime_get_real_seconds();
2287 verf[1] = (__force __be32)nn->clverifier_counter++;
2288 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
2291 static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
2293 clp->cl_clientid.cl_boot = (u32)nn->boot_time;
2294 clp->cl_clientid.cl_id = nn->clientid_counter++;
2295 gen_confirm(clp, nn);
2298 static struct nfs4_stid *
2299 find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
2301 struct nfs4_stid *ret;
2303 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
2304 if (!ret || !ret->sc_type)
2309 static struct nfs4_stid *
2310 find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
2312 struct nfs4_stid *s;
2314 spin_lock(&cl->cl_lock);
2315 s = find_stateid_locked(cl, t);
2317 if (typemask & s->sc_type)
2318 refcount_inc(&s->sc_count);
2322 spin_unlock(&cl->cl_lock);
2326 static struct nfs4_client *get_nfsdfs_clp(struct inode *inode)
2328 struct nfsdfs_client *nc;
2329 nc = get_nfsdfs_client(inode);
2332 return container_of(nc, struct nfs4_client, cl_nfsdfs);
2335 static void seq_quote_mem(struct seq_file *m, char *data, int len)
2337 seq_printf(m, "\"");
2338 seq_escape_mem_ascii(m, data, len);
2339 seq_printf(m, "\"");
2342 static int client_info_show(struct seq_file *m, void *v)
2344 struct inode *inode = m->private;
2345 struct nfs4_client *clp;
2348 clp = get_nfsdfs_clp(inode);
2351 memcpy(&clid, &clp->cl_clientid, sizeof(clid));
2352 seq_printf(m, "clientid: 0x%llx\n", clid);
2353 seq_printf(m, "address: \"%pISpc\"\n", (struct sockaddr *)&clp->cl_addr);
2354 seq_printf(m, "name: ");
2355 seq_quote_mem(m, clp->cl_name.data, clp->cl_name.len);
2356 seq_printf(m, "\nminor version: %d\n", clp->cl_minorversion);
2357 if (clp->cl_nii_domain.data) {
2358 seq_printf(m, "Implementation domain: ");
2359 seq_quote_mem(m, clp->cl_nii_domain.data,
2360 clp->cl_nii_domain.len);
2361 seq_printf(m, "\nImplementation name: ");
2362 seq_quote_mem(m, clp->cl_nii_name.data, clp->cl_nii_name.len);
2363 seq_printf(m, "\nImplementation time: [%lld, %ld]\n",
2364 clp->cl_nii_time.tv_sec, clp->cl_nii_time.tv_nsec);
2371 static int client_info_open(struct inode *inode, struct file *file)
2373 return single_open(file, client_info_show, inode);
2376 static const struct file_operations client_info_fops = {
2377 .open = client_info_open,
2379 .llseek = seq_lseek,
2380 .release = single_release,
2383 static void *states_start(struct seq_file *s, loff_t *pos)
2384 __acquires(&clp->cl_lock)
2386 struct nfs4_client *clp = s->private;
2387 unsigned long id = *pos;
2390 spin_lock(&clp->cl_lock);
2391 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2396 static void *states_next(struct seq_file *s, void *v, loff_t *pos)
2398 struct nfs4_client *clp = s->private;
2399 unsigned long id = *pos;
2404 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2409 static void states_stop(struct seq_file *s, void *v)
2410 __releases(&clp->cl_lock)
2412 struct nfs4_client *clp = s->private;
2414 spin_unlock(&clp->cl_lock);
2417 static void nfs4_show_fname(struct seq_file *s, struct nfsd_file *f)
2419 seq_printf(s, "filename: \"%pD2\"", f->nf_file);
2422 static void nfs4_show_superblock(struct seq_file *s, struct nfsd_file *f)
2424 struct inode *inode = f->nf_inode;
2426 seq_printf(s, "superblock: \"%02x:%02x:%ld\"",
2427 MAJOR(inode->i_sb->s_dev),
2428 MINOR(inode->i_sb->s_dev),
2432 static void nfs4_show_owner(struct seq_file *s, struct nfs4_stateowner *oo)
2434 seq_printf(s, "owner: ");
2435 seq_quote_mem(s, oo->so_owner.data, oo->so_owner.len);
2438 static void nfs4_show_stateid(struct seq_file *s, stateid_t *stid)
2440 seq_printf(s, "0x%.8x", stid->si_generation);
2441 seq_printf(s, "%12phN", &stid->si_opaque);
2444 static int nfs4_show_open(struct seq_file *s, struct nfs4_stid *st)
2446 struct nfs4_ol_stateid *ols;
2447 struct nfs4_file *nf;
2448 struct nfsd_file *file;
2449 struct nfs4_stateowner *oo;
2450 unsigned int access, deny;
2452 if (st->sc_type != NFS4_OPEN_STID && st->sc_type != NFS4_LOCK_STID)
2453 return 0; /* XXX: or SEQ_SKIP? */
2454 ols = openlockstateid(st);
2455 oo = ols->st_stateowner;
2457 file = find_any_file(nf);
2461 seq_printf(s, "- ");
2462 nfs4_show_stateid(s, &st->sc_stateid);
2463 seq_printf(s, ": { type: open, ");
2465 access = bmap_to_share_mode(ols->st_access_bmap);
2466 deny = bmap_to_share_mode(ols->st_deny_bmap);
2468 seq_printf(s, "access: %s%s, ",
2469 access & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2470 access & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2471 seq_printf(s, "deny: %s%s, ",
2472 deny & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2473 deny & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2475 nfs4_show_superblock(s, file);
2476 seq_printf(s, ", ");
2477 nfs4_show_fname(s, file);
2478 seq_printf(s, ", ");
2479 nfs4_show_owner(s, oo);
2480 seq_printf(s, " }\n");
2481 nfsd_file_put(file);
2486 static int nfs4_show_lock(struct seq_file *s, struct nfs4_stid *st)
2488 struct nfs4_ol_stateid *ols;
2489 struct nfs4_file *nf;
2490 struct nfsd_file *file;
2491 struct nfs4_stateowner *oo;
2493 ols = openlockstateid(st);
2494 oo = ols->st_stateowner;
2496 file = find_any_file(nf);
2500 seq_printf(s, "- ");
2501 nfs4_show_stateid(s, &st->sc_stateid);
2502 seq_printf(s, ": { type: lock, ");
2505 * Note: a lock stateid isn't really the same thing as a lock,
2506 * it's the locking state held by one owner on a file, and there
2507 * may be multiple (or no) lock ranges associated with it.
2508 * (Same for the matter is true of open stateids.)
2511 nfs4_show_superblock(s, file);
2512 /* XXX: open stateid? */
2513 seq_printf(s, ", ");
2514 nfs4_show_fname(s, file);
2515 seq_printf(s, ", ");
2516 nfs4_show_owner(s, oo);
2517 seq_printf(s, " }\n");
2518 nfsd_file_put(file);
2523 static int nfs4_show_deleg(struct seq_file *s, struct nfs4_stid *st)
2525 struct nfs4_delegation *ds;
2526 struct nfs4_file *nf;
2527 struct nfsd_file *file;
2529 ds = delegstateid(st);
2531 file = find_deleg_file(nf);
2535 seq_printf(s, "- ");
2536 nfs4_show_stateid(s, &st->sc_stateid);
2537 seq_printf(s, ": { type: deleg, ");
2539 /* Kinda dead code as long as we only support read delegs: */
2540 seq_printf(s, "access: %s, ",
2541 ds->dl_type == NFS4_OPEN_DELEGATE_READ ? "r" : "w");
2543 /* XXX: lease time, whether it's being recalled. */
2545 nfs4_show_superblock(s, file);
2546 seq_printf(s, ", ");
2547 nfs4_show_fname(s, file);
2548 seq_printf(s, " }\n");
2549 nfsd_file_put(file);
2554 static int nfs4_show_layout(struct seq_file *s, struct nfs4_stid *st)
2556 struct nfs4_layout_stateid *ls;
2557 struct nfsd_file *file;
2559 ls = container_of(st, struct nfs4_layout_stateid, ls_stid);
2562 seq_printf(s, "- ");
2563 nfs4_show_stateid(s, &st->sc_stateid);
2564 seq_printf(s, ": { type: layout, ");
2566 /* XXX: What else would be useful? */
2568 nfs4_show_superblock(s, file);
2569 seq_printf(s, ", ");
2570 nfs4_show_fname(s, file);
2571 seq_printf(s, " }\n");
2576 static int states_show(struct seq_file *s, void *v)
2578 struct nfs4_stid *st = v;
2580 switch (st->sc_type) {
2581 case NFS4_OPEN_STID:
2582 return nfs4_show_open(s, st);
2583 case NFS4_LOCK_STID:
2584 return nfs4_show_lock(s, st);
2585 case NFS4_DELEG_STID:
2586 return nfs4_show_deleg(s, st);
2587 case NFS4_LAYOUT_STID:
2588 return nfs4_show_layout(s, st);
2590 return 0; /* XXX: or SEQ_SKIP? */
2592 /* XXX: copy stateids? */
2595 static struct seq_operations states_seq_ops = {
2596 .start = states_start,
2597 .next = states_next,
2598 .stop = states_stop,
2602 static int client_states_open(struct inode *inode, struct file *file)
2605 struct nfs4_client *clp;
2608 clp = get_nfsdfs_clp(inode);
2612 ret = seq_open(file, &states_seq_ops);
2615 s = file->private_data;
2620 static int client_opens_release(struct inode *inode, struct file *file)
2622 struct seq_file *m = file->private_data;
2623 struct nfs4_client *clp = m->private;
2625 /* XXX: alternatively, we could get/drop in seq start/stop */
2630 static const struct file_operations client_states_fops = {
2631 .open = client_states_open,
2633 .llseek = seq_lseek,
2634 .release = client_opens_release,
2638 * Normally we refuse to destroy clients that are in use, but here the
2639 * administrator is telling us to just do it. We also want to wait
2640 * so the caller has a guarantee that the client's locks are gone by
2641 * the time the write returns:
2643 static void force_expire_client(struct nfs4_client *clp)
2645 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2646 bool already_expired;
2648 spin_lock(&clp->cl_lock);
2650 spin_unlock(&clp->cl_lock);
2652 wait_event(expiry_wq, atomic_read(&clp->cl_rpc_users) == 0);
2653 spin_lock(&nn->client_lock);
2654 already_expired = list_empty(&clp->cl_lru);
2655 if (!already_expired)
2656 unhash_client_locked(clp);
2657 spin_unlock(&nn->client_lock);
2659 if (!already_expired)
2662 wait_event(expiry_wq, clp->cl_nfsd_dentry == NULL);
2665 static ssize_t client_ctl_write(struct file *file, const char __user *buf,
2666 size_t size, loff_t *pos)
2669 struct nfs4_client *clp;
2671 data = simple_transaction_get(file, buf, size);
2673 return PTR_ERR(data);
2674 if (size != 7 || 0 != memcmp(data, "expire\n", 7))
2676 clp = get_nfsdfs_clp(file_inode(file));
2679 force_expire_client(clp);
2684 static const struct file_operations client_ctl_fops = {
2685 .write = client_ctl_write,
2686 .release = simple_transaction_release,
2689 static const struct tree_descr client_files[] = {
2690 [0] = {"info", &client_info_fops, S_IRUSR},
2691 [1] = {"states", &client_states_fops, S_IRUSR},
2692 [2] = {"ctl", &client_ctl_fops, S_IWUSR},
2696 static struct nfs4_client *create_client(struct xdr_netobj name,
2697 struct svc_rqst *rqstp, nfs4_verifier *verf)
2699 struct nfs4_client *clp;
2700 struct sockaddr *sa = svc_addr(rqstp);
2702 struct net *net = SVC_NET(rqstp);
2703 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2705 clp = alloc_client(name);
2709 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
2715 kref_init(&clp->cl_nfsdfs.cl_ref);
2716 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
2717 clp->cl_time = ktime_get_boottime_seconds();
2718 clear_bit(0, &clp->cl_cb_slot_busy);
2719 copy_verf(clp, verf);
2720 memcpy(&clp->cl_addr, sa, sizeof(struct sockaddr_storage));
2721 clp->cl_cb_session = NULL;
2723 clp->cl_nfsd_dentry = nfsd_client_mkdir(nn, &clp->cl_nfsdfs,
2724 clp->cl_clientid.cl_id - nn->clientid_base,
2726 if (!clp->cl_nfsd_dentry) {
2734 add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
2736 struct rb_node **new = &(root->rb_node), *parent = NULL;
2737 struct nfs4_client *clp;
2740 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
2743 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
2744 new = &((*new)->rb_left);
2746 new = &((*new)->rb_right);
2749 rb_link_node(&new_clp->cl_namenode, parent, new);
2750 rb_insert_color(&new_clp->cl_namenode, root);
2753 static struct nfs4_client *
2754 find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2757 struct rb_node *node = root->rb_node;
2758 struct nfs4_client *clp;
2761 clp = rb_entry(node, struct nfs4_client, cl_namenode);
2762 cmp = compare_blob(&clp->cl_name, name);
2764 node = node->rb_left;
2766 node = node->rb_right;
2774 add_to_unconfirmed(struct nfs4_client *clp)
2776 unsigned int idhashval;
2777 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2779 lockdep_assert_held(&nn->client_lock);
2781 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2782 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
2783 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
2784 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
2785 renew_client_locked(clp);
2789 move_to_confirmed(struct nfs4_client *clp)
2791 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
2792 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2794 lockdep_assert_held(&nn->client_lock);
2796 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
2797 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
2798 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
2799 add_clp_to_name_tree(clp, &nn->conf_name_tree);
2800 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2801 renew_client_locked(clp);
2804 static struct nfs4_client *
2805 find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
2807 struct nfs4_client *clp;
2808 unsigned int idhashval = clientid_hashval(clid->cl_id);
2810 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
2811 if (same_clid(&clp->cl_clientid, clid)) {
2812 if ((bool)clp->cl_minorversion != sessions)
2814 renew_client_locked(clp);
2821 static struct nfs4_client *
2822 find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2824 struct list_head *tbl = nn->conf_id_hashtbl;
2826 lockdep_assert_held(&nn->client_lock);
2827 return find_client_in_id_table(tbl, clid, sessions);
2830 static struct nfs4_client *
2831 find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2833 struct list_head *tbl = nn->unconf_id_hashtbl;
2835 lockdep_assert_held(&nn->client_lock);
2836 return find_client_in_id_table(tbl, clid, sessions);
2839 static bool clp_used_exchangeid(struct nfs4_client *clp)
2841 return clp->cl_exchange_flags != 0;
2844 static struct nfs4_client *
2845 find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
2847 lockdep_assert_held(&nn->client_lock);
2848 return find_clp_in_name_tree(name, &nn->conf_name_tree);
2851 static struct nfs4_client *
2852 find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
2854 lockdep_assert_held(&nn->client_lock);
2855 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
2859 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
2861 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
2862 struct sockaddr *sa = svc_addr(rqstp);
2863 u32 scopeid = rpc_get_scope_id(sa);
2864 unsigned short expected_family;
2866 /* Currently, we only support tcp and tcp6 for the callback channel */
2867 if (se->se_callback_netid_len == 3 &&
2868 !memcmp(se->se_callback_netid_val, "tcp", 3))
2869 expected_family = AF_INET;
2870 else if (se->se_callback_netid_len == 4 &&
2871 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2872 expected_family = AF_INET6;
2876 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
2877 se->se_callback_addr_len,
2878 (struct sockaddr *)&conn->cb_addr,
2879 sizeof(conn->cb_addr));
2881 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
2884 if (conn->cb_addr.ss_family == AF_INET6)
2885 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
2887 conn->cb_prog = se->se_callback_prog;
2888 conn->cb_ident = se->se_callback_ident;
2889 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
2890 trace_nfsd_cb_args(clp, conn);
2893 conn->cb_addr.ss_family = AF_UNSPEC;
2894 conn->cb_addrlen = 0;
2895 trace_nfsd_cb_nodelegs(clp);
2900 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
2903 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2905 struct xdr_buf *buf = resp->xdr.buf;
2906 struct nfsd4_slot *slot = resp->cstate.slot;
2909 dprintk("--> %s slot %p\n", __func__, slot);
2911 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
2912 slot->sl_opcnt = resp->opcnt;
2913 slot->sl_status = resp->cstate.status;
2914 free_svc_cred(&slot->sl_cred);
2915 copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred);
2917 if (!nfsd4_cache_this(resp)) {
2918 slot->sl_flags &= ~NFSD4_SLOT_CACHED;
2921 slot->sl_flags |= NFSD4_SLOT_CACHED;
2923 base = resp->cstate.data_offset;
2924 slot->sl_datalen = buf->len - base;
2925 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
2926 WARN(1, "%s: sessions DRC could not cache compound\n",
2932 * Encode the replay sequence operation from the slot values.
2933 * If cachethis is FALSE encode the uncached rep error on the next
2934 * operation which sets resp->p and increments resp->opcnt for
2935 * nfs4svc_encode_compoundres.
2939 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2940 struct nfsd4_compoundres *resp)
2942 struct nfsd4_op *op;
2943 struct nfsd4_slot *slot = resp->cstate.slot;
2945 /* Encode the replayed sequence operation */
2946 op = &args->ops[resp->opcnt - 1];
2947 nfsd4_encode_operation(resp, op);
2949 if (slot->sl_flags & NFSD4_SLOT_CACHED)
2951 if (args->opcnt == 1) {
2953 * The original operation wasn't a solo sequence--we
2954 * always cache those--so this retry must not match the
2957 op->status = nfserr_seq_false_retry;
2959 op = &args->ops[resp->opcnt++];
2960 op->status = nfserr_retry_uncached_rep;
2961 nfsd4_encode_operation(resp, op);
2967 * The sequence operation is not cached because we can use the slot and
2971 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2972 struct nfsd4_sequence *seq)
2974 struct nfsd4_slot *slot = resp->cstate.slot;
2975 struct xdr_stream *xdr = &resp->xdr;
2979 dprintk("--> %s slot %p\n", __func__, slot);
2981 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
2985 p = xdr_reserve_space(xdr, slot->sl_datalen);
2988 return nfserr_serverfault;
2990 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2991 xdr_commit_encode(xdr);
2993 resp->opcnt = slot->sl_opcnt;
2994 return slot->sl_status;
2998 * Set the exchange_id flags returned by the server.
3001 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
3003 #ifdef CONFIG_NFSD_PNFS
3004 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
3006 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
3009 /* Referrals are supported, Migration is not. */
3010 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
3012 /* set the wire flags to return to client. */
3013 clid->flags = new->cl_exchange_flags;
3016 static bool client_has_openowners(struct nfs4_client *clp)
3018 struct nfs4_openowner *oo;
3020 list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
3021 if (!list_empty(&oo->oo_owner.so_stateids))
3027 static bool client_has_state(struct nfs4_client *clp)
3029 return client_has_openowners(clp)
3030 #ifdef CONFIG_NFSD_PNFS
3031 || !list_empty(&clp->cl_lo_states)
3033 || !list_empty(&clp->cl_delegations)
3034 || !list_empty(&clp->cl_sessions)
3035 || !list_empty(&clp->async_copies);
3038 static __be32 copy_impl_id(struct nfs4_client *clp,
3039 struct nfsd4_exchange_id *exid)
3041 if (!exid->nii_domain.data)
3043 xdr_netobj_dup(&clp->cl_nii_domain, &exid->nii_domain, GFP_KERNEL);
3044 if (!clp->cl_nii_domain.data)
3045 return nfserr_jukebox;
3046 xdr_netobj_dup(&clp->cl_nii_name, &exid->nii_name, GFP_KERNEL);
3047 if (!clp->cl_nii_name.data)
3048 return nfserr_jukebox;
3049 clp->cl_nii_time = exid->nii_time;
3054 nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3055 union nfsd4_op_u *u)
3057 struct nfsd4_exchange_id *exid = &u->exchange_id;
3058 struct nfs4_client *conf, *new;
3059 struct nfs4_client *unconf = NULL;
3061 char addr_str[INET6_ADDRSTRLEN];
3062 nfs4_verifier verf = exid->verifier;
3063 struct sockaddr *sa = svc_addr(rqstp);
3064 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
3065 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3067 rpc_ntop(sa, addr_str, sizeof(addr_str));
3068 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
3069 "ip_addr=%s flags %x, spa_how %d\n",
3070 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
3071 addr_str, exid->flags, exid->spa_how);
3073 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
3074 return nfserr_inval;
3076 new = create_client(exid->clname, rqstp, &verf);
3078 return nfserr_jukebox;
3079 status = copy_impl_id(new, exid);
3083 switch (exid->spa_how) {
3085 exid->spo_must_enforce[0] = 0;
3086 exid->spo_must_enforce[1] = (
3087 1 << (OP_BIND_CONN_TO_SESSION - 32) |
3088 1 << (OP_EXCHANGE_ID - 32) |
3089 1 << (OP_CREATE_SESSION - 32) |
3090 1 << (OP_DESTROY_SESSION - 32) |
3091 1 << (OP_DESTROY_CLIENTID - 32));
3093 exid->spo_must_allow[0] &= (1 << (OP_CLOSE) |
3094 1 << (OP_OPEN_DOWNGRADE) |
3096 1 << (OP_DELEGRETURN));
3098 exid->spo_must_allow[1] &= (
3099 1 << (OP_TEST_STATEID - 32) |
3100 1 << (OP_FREE_STATEID - 32));
3101 if (!svc_rqst_integrity_protected(rqstp)) {
3102 status = nfserr_inval;
3106 * Sometimes userspace doesn't give us a principal.
3107 * Which is a bug, really. Anyway, we can't enforce
3108 * MACH_CRED in that case, better to give up now:
3110 if (!new->cl_cred.cr_principal &&
3111 !new->cl_cred.cr_raw_principal) {
3112 status = nfserr_serverfault;
3115 new->cl_mach_cred = true;
3118 default: /* checked by xdr code */
3122 status = nfserr_encr_alg_unsupp;
3126 /* Cases below refer to rfc 5661 section 18.35.4: */
3127 spin_lock(&nn->client_lock);
3128 conf = find_confirmed_client_by_name(&exid->clname, nn);
3130 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
3131 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
3134 if (!clp_used_exchangeid(conf)) { /* buggy client */
3135 status = nfserr_inval;
3138 if (!nfsd4_mach_creds_match(conf, rqstp)) {
3139 status = nfserr_wrong_cred;
3142 if (!creds_match) { /* case 9 */
3143 status = nfserr_perm;
3146 if (!verfs_match) { /* case 8 */
3147 status = nfserr_not_same;
3151 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
3154 if (!creds_match) { /* case 3 */
3155 if (client_has_state(conf)) {
3156 status = nfserr_clid_inuse;
3161 if (verfs_match) { /* case 2 */
3162 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
3165 /* case 5, client reboot */
3170 if (update) { /* case 7 */
3171 status = nfserr_noent;
3175 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
3176 if (unconf) /* case 4, possible retry or client restart */
3177 unhash_client_locked(unconf);
3179 /* case 1 (normal case) */
3182 status = mark_client_expired_locked(conf);
3186 new->cl_minorversion = cstate->minorversion;
3187 new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0];
3188 new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1];
3190 add_to_unconfirmed(new);
3193 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
3194 exid->clientid.cl_id = conf->cl_clientid.cl_id;
3196 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
3197 nfsd4_set_ex_flags(conf, exid);
3199 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
3200 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
3204 spin_unlock(&nn->client_lock);
3209 expire_client(unconf);
3214 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
3216 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
3219 /* The slot is in use, and no response has been sent. */
3221 if (seqid == slot_seqid)
3222 return nfserr_jukebox;
3224 return nfserr_seq_misordered;
3226 /* Note unsigned 32-bit arithmetic handles wraparound: */
3227 if (likely(seqid == slot_seqid + 1))
3229 if (seqid == slot_seqid)
3230 return nfserr_replay_cache;
3231 return nfserr_seq_misordered;
3235 * Cache the create session result into the create session single DRC
3236 * slot cache by saving the xdr structure. sl_seqid has been set.
3237 * Do this for solo or embedded create session operations.
3240 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
3241 struct nfsd4_clid_slot *slot, __be32 nfserr)
3243 slot->sl_status = nfserr;
3244 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
3248 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
3249 struct nfsd4_clid_slot *slot)
3251 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
3252 return slot->sl_status;
3255 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
3256 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
3257 1 + /* MIN tag is length with zero, only length */ \
3258 3 + /* version, opcount, opcode */ \
3259 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3260 /* seqid, slotID, slotID, cache */ \
3261 4 ) * sizeof(__be32))
3263 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
3264 2 + /* verifier: AUTH_NULL, length 0 */\
3266 1 + /* MIN tag is length with zero, only length */ \
3267 3 + /* opcount, opcode, opstatus*/ \
3268 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3269 /* seqid, slotID, slotID, slotID, status */ \
3270 5 ) * sizeof(__be32))
3272 static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
3274 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
3276 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
3277 return nfserr_toosmall;
3278 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
3279 return nfserr_toosmall;
3280 ca->headerpadsz = 0;
3281 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
3282 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
3283 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
3284 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
3285 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
3286 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
3288 * Note decreasing slot size below client's request may make it
3289 * difficult for client to function correctly, whereas
3290 * decreasing the number of slots will (just?) affect
3291 * performance. When short on memory we therefore prefer to
3292 * decrease number of slots instead of their size. Clients that
3293 * request larger slots than they need will get poor results:
3294 * Note that we always allow at least one slot, because our
3295 * accounting is soft and provides no guarantees either way.
3297 ca->maxreqs = nfsd4_get_drc_mem(ca, nn);
3303 * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
3304 * These are based on similar macros in linux/sunrpc/msg_prot.h .
3306 #define RPC_MAX_HEADER_WITH_AUTH_SYS \
3307 (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
3309 #define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
3310 (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
3312 #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
3313 RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
3314 #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
3315 RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
3318 static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
3320 ca->headerpadsz = 0;
3322 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
3323 return nfserr_toosmall;
3324 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
3325 return nfserr_toosmall;
3326 ca->maxresp_cached = 0;
3328 return nfserr_toosmall;
3333 static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
3335 switch (cbs->flavor) {
3341 * GSS case: the spec doesn't allow us to return this
3342 * error. But it also doesn't allow us not to support
3344 * I'd rather this fail hard than return some error the
3345 * client might think it can already handle:
3347 return nfserr_encr_alg_unsupp;
3352 nfsd4_create_session(struct svc_rqst *rqstp,
3353 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
3355 struct nfsd4_create_session *cr_ses = &u->create_session;
3356 struct sockaddr *sa = svc_addr(rqstp);
3357 struct nfs4_client *conf, *unconf;
3358 struct nfs4_client *old = NULL;
3359 struct nfsd4_session *new;
3360 struct nfsd4_conn *conn;
3361 struct nfsd4_clid_slot *cs_slot = NULL;
3363 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3365 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
3366 return nfserr_inval;
3367 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
3370 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
3373 status = check_backchannel_attrs(&cr_ses->back_channel);
3375 goto out_release_drc_mem;
3376 status = nfserr_jukebox;
3377 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
3379 goto out_release_drc_mem;
3380 conn = alloc_conn_from_crses(rqstp, cr_ses);
3382 goto out_free_session;
3384 spin_lock(&nn->client_lock);
3385 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
3386 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
3387 WARN_ON_ONCE(conf && unconf);
3390 status = nfserr_wrong_cred;
3391 if (!nfsd4_mach_creds_match(conf, rqstp))
3393 cs_slot = &conf->cl_cs_slot;
3394 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
3396 if (status == nfserr_replay_cache)
3397 status = nfsd4_replay_create_session(cr_ses, cs_slot);
3400 } else if (unconf) {
3401 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
3402 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
3403 status = nfserr_clid_inuse;
3406 status = nfserr_wrong_cred;
3407 if (!nfsd4_mach_creds_match(unconf, rqstp))
3409 cs_slot = &unconf->cl_cs_slot;
3410 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
3412 /* an unconfirmed replay returns misordered */
3413 status = nfserr_seq_misordered;
3416 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3418 status = mark_client_expired_locked(old);
3424 move_to_confirmed(unconf);
3427 status = nfserr_stale_clientid;
3431 /* Persistent sessions are not supported */
3432 cr_ses->flags &= ~SESSION4_PERSIST;
3433 /* Upshifting from TCP to RDMA is not supported */
3434 cr_ses->flags &= ~SESSION4_RDMA;
3436 init_session(rqstp, new, conf, cr_ses);
3437 nfsd4_get_session_locked(new);
3439 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
3440 NFS4_MAX_SESSIONID_LEN);
3441 cs_slot->sl_seqid++;
3442 cr_ses->seqid = cs_slot->sl_seqid;
3444 /* cache solo and embedded create sessions under the client_lock */
3445 nfsd4_cache_create_session(cr_ses, cs_slot, status);
3446 spin_unlock(&nn->client_lock);
3447 /* init connection and backchannel */
3448 nfsd4_init_conn(rqstp, conn, new);
3449 nfsd4_put_session(new);
3454 spin_unlock(&nn->client_lock);
3459 __free_session(new);
3460 out_release_drc_mem:
3461 nfsd4_put_drc_mem(&cr_ses->fore_channel);
3465 static __be32 nfsd4_map_bcts_dir(u32 *dir)
3468 case NFS4_CDFC4_FORE:
3469 case NFS4_CDFC4_BACK:
3471 case NFS4_CDFC4_FORE_OR_BOTH:
3472 case NFS4_CDFC4_BACK_OR_BOTH:
3473 *dir = NFS4_CDFC4_BOTH;
3476 return nfserr_inval;
3479 __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp,
3480 struct nfsd4_compound_state *cstate,
3481 union nfsd4_op_u *u)
3483 struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl;
3484 struct nfsd4_session *session = cstate->session;
3485 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3488 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
3491 spin_lock(&nn->client_lock);
3492 session->se_cb_prog = bc->bc_cb_program;
3493 session->se_cb_sec = bc->bc_cb_sec;
3494 spin_unlock(&nn->client_lock);
3496 nfsd4_probe_callback(session->se_client);
3501 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
3503 struct nfsd4_conn *c;
3505 list_for_each_entry(c, &s->se_conns, cn_persession) {
3506 if (c->cn_xprt == xpt) {
3513 static __be32 nfsd4_match_existing_connection(struct svc_rqst *rqst,
3514 struct nfsd4_session *session, u32 req)
3516 struct nfs4_client *clp = session->se_client;
3517 struct svc_xprt *xpt = rqst->rq_xprt;
3518 struct nfsd4_conn *c;
3521 /* Following the last paragraph of RFC 5661 Section 18.34.3: */
3522 spin_lock(&clp->cl_lock);
3523 c = __nfsd4_find_conn(xpt, session);
3525 status = nfserr_noent;
3526 else if (req == c->cn_flags)
3528 else if (req == NFS4_CDFC4_FORE_OR_BOTH &&
3529 c->cn_flags != NFS4_CDFC4_BACK)
3531 else if (req == NFS4_CDFC4_BACK_OR_BOTH &&
3532 c->cn_flags != NFS4_CDFC4_FORE)
3535 status = nfserr_inval;
3536 spin_unlock(&clp->cl_lock);
3540 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
3541 struct nfsd4_compound_state *cstate,
3542 union nfsd4_op_u *u)
3544 struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session;
3546 struct nfsd4_conn *conn;
3547 struct nfsd4_session *session;
3548 struct net *net = SVC_NET(rqstp);
3549 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3551 if (!nfsd4_last_compound_op(rqstp))
3552 return nfserr_not_only_op;
3553 spin_lock(&nn->client_lock);
3554 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
3555 spin_unlock(&nn->client_lock);
3557 goto out_no_session;
3558 status = nfserr_wrong_cred;
3559 if (!nfsd4_mach_creds_match(session->se_client, rqstp))
3561 status = nfsd4_match_existing_connection(rqstp, session, bcts->dir);
3562 if (status == nfs_ok || status == nfserr_inval)
3564 status = nfsd4_map_bcts_dir(&bcts->dir);
3567 conn = alloc_conn(rqstp, bcts->dir);
3568 status = nfserr_jukebox;
3571 nfsd4_init_conn(rqstp, conn, session);
3574 nfsd4_put_session(session);
3579 static bool nfsd4_compound_in_session(struct nfsd4_compound_state *cstate, struct nfs4_sessionid *sid)
3581 if (!cstate->session)
3583 return !memcmp(sid, &cstate->session->se_sessionid, sizeof(*sid));
3587 nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate,
3588 union nfsd4_op_u *u)
3590 struct nfs4_sessionid *sessionid = &u->destroy_session.sessionid;
3591 struct nfsd4_session *ses;
3593 int ref_held_by_me = 0;
3594 struct net *net = SVC_NET(r);
3595 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3597 status = nfserr_not_only_op;
3598 if (nfsd4_compound_in_session(cstate, sessionid)) {
3599 if (!nfsd4_last_compound_op(r))
3603 dump_sessionid(__func__, sessionid);
3604 spin_lock(&nn->client_lock);
3605 ses = find_in_sessionid_hashtbl(sessionid, net, &status);
3607 goto out_client_lock;
3608 status = nfserr_wrong_cred;
3609 if (!nfsd4_mach_creds_match(ses->se_client, r))
3610 goto out_put_session;
3611 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
3613 goto out_put_session;
3614 unhash_session(ses);
3615 spin_unlock(&nn->client_lock);
3617 nfsd4_probe_callback_sync(ses->se_client);
3619 spin_lock(&nn->client_lock);
3622 nfsd4_put_session_locked(ses);
3624 spin_unlock(&nn->client_lock);
3629 static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
3631 struct nfs4_client *clp = ses->se_client;
3632 struct nfsd4_conn *c;
3633 __be32 status = nfs_ok;
3636 spin_lock(&clp->cl_lock);
3637 c = __nfsd4_find_conn(new->cn_xprt, ses);
3640 status = nfserr_conn_not_bound_to_session;
3641 if (clp->cl_mach_cred)
3643 __nfsd4_hash_conn(new, ses);
3644 spin_unlock(&clp->cl_lock);
3645 ret = nfsd4_register_conn(new);
3647 /* oops; xprt is already down: */
3648 nfsd4_conn_lost(&new->cn_xpt_user);
3651 spin_unlock(&clp->cl_lock);
3656 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
3658 struct nfsd4_compoundargs *args = rqstp->rq_argp;
3660 return args->opcnt > session->se_fchannel.maxops;
3663 static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
3664 struct nfsd4_session *session)
3666 struct xdr_buf *xb = &rqstp->rq_arg;
3668 return xb->len > session->se_fchannel.maxreq_sz;
3671 static bool replay_matches_cache(struct svc_rqst *rqstp,
3672 struct nfsd4_sequence *seq, struct nfsd4_slot *slot)
3674 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
3676 if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) !=
3677 (bool)seq->cachethis)
3680 * If there's an error then the reply can have fewer ops than
3683 if (slot->sl_opcnt < argp->opcnt && !slot->sl_status)
3686 * But if we cached a reply with *more* ops than the call you're
3687 * sending us now, then this new call is clearly not really a
3688 * replay of the old one:
3690 if (slot->sl_opcnt > argp->opcnt)
3692 /* This is the only check explicitly called by spec: */
3693 if (!same_creds(&rqstp->rq_cred, &slot->sl_cred))
3696 * There may be more comparisons we could actually do, but the
3697 * spec doesn't require us to catch every case where the calls
3698 * don't match (that would require caching the call as well as
3699 * the reply), so we don't bother.
3705 nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3706 union nfsd4_op_u *u)
3708 struct nfsd4_sequence *seq = &u->sequence;
3709 struct nfsd4_compoundres *resp = rqstp->rq_resp;
3710 struct xdr_stream *xdr = &resp->xdr;
3711 struct nfsd4_session *session;
3712 struct nfs4_client *clp;
3713 struct nfsd4_slot *slot;
3714 struct nfsd4_conn *conn;
3717 struct net *net = SVC_NET(rqstp);
3718 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3720 if (resp->opcnt != 1)
3721 return nfserr_sequence_pos;
3724 * Will be either used or freed by nfsd4_sequence_check_conn
3727 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
3729 return nfserr_jukebox;
3731 spin_lock(&nn->client_lock);
3732 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
3734 goto out_no_session;
3735 clp = session->se_client;
3737 status = nfserr_too_many_ops;
3738 if (nfsd4_session_too_many_ops(rqstp, session))
3739 goto out_put_session;
3741 status = nfserr_req_too_big;
3742 if (nfsd4_request_too_big(rqstp, session))
3743 goto out_put_session;
3745 status = nfserr_badslot;
3746 if (seq->slotid >= session->se_fchannel.maxreqs)
3747 goto out_put_session;
3749 slot = session->se_slots[seq->slotid];
3750 dprintk("%s: slotid %d\n", __func__, seq->slotid);
3752 /* We do not negotiate the number of slots yet, so set the
3753 * maxslots to the session maxreqs which is used to encode
3754 * sr_highest_slotid and the sr_target_slot id to maxslots */
3755 seq->maxslots = session->se_fchannel.maxreqs;
3757 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
3758 slot->sl_flags & NFSD4_SLOT_INUSE);
3759 if (status == nfserr_replay_cache) {
3760 status = nfserr_seq_misordered;
3761 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
3762 goto out_put_session;
3763 status = nfserr_seq_false_retry;
3764 if (!replay_matches_cache(rqstp, seq, slot))
3765 goto out_put_session;
3766 cstate->slot = slot;
3767 cstate->session = session;
3769 /* Return the cached reply status and set cstate->status
3770 * for nfsd4_proc_compound processing */
3771 status = nfsd4_replay_cache_entry(resp, seq);
3772 cstate->status = nfserr_replay_cache;
3776 goto out_put_session;
3778 status = nfsd4_sequence_check_conn(conn, session);
3781 goto out_put_session;
3783 buflen = (seq->cachethis) ?
3784 session->se_fchannel.maxresp_cached :
3785 session->se_fchannel.maxresp_sz;
3786 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
3788 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
3789 goto out_put_session;
3790 svc_reserve(rqstp, buflen);
3793 /* Success! bump slot seqid */
3794 slot->sl_seqid = seq->seqid;
3795 slot->sl_flags |= NFSD4_SLOT_INUSE;
3797 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
3799 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
3801 cstate->slot = slot;
3802 cstate->session = session;
3806 switch (clp->cl_cb_state) {
3808 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
3810 case NFSD4_CB_FAULT:
3811 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
3814 seq->status_flags = 0;
3816 if (!list_empty(&clp->cl_revoked))
3817 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
3821 spin_unlock(&nn->client_lock);
3824 nfsd4_put_session_locked(session);
3825 goto out_no_session;
3829 nfsd4_sequence_done(struct nfsd4_compoundres *resp)
3831 struct nfsd4_compound_state *cs = &resp->cstate;
3833 if (nfsd4_has_session(cs)) {
3834 if (cs->status != nfserr_replay_cache) {
3835 nfsd4_store_cache_entry(resp);
3836 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
3838 /* Drop session reference that was taken in nfsd4_sequence() */
3839 nfsd4_put_session(cs->session);
3841 put_client_renew(cs->clp);
3845 nfsd4_destroy_clientid(struct svc_rqst *rqstp,
3846 struct nfsd4_compound_state *cstate,
3847 union nfsd4_op_u *u)
3849 struct nfsd4_destroy_clientid *dc = &u->destroy_clientid;
3850 struct nfs4_client *conf, *unconf;
3851 struct nfs4_client *clp = NULL;
3853 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3855 spin_lock(&nn->client_lock);
3856 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
3857 conf = find_confirmed_client(&dc->clientid, true, nn);
3858 WARN_ON_ONCE(conf && unconf);
3861 if (client_has_state(conf)) {
3862 status = nfserr_clientid_busy;
3865 status = mark_client_expired_locked(conf);
3872 status = nfserr_stale_clientid;
3875 if (!nfsd4_mach_creds_match(clp, rqstp)) {
3877 status = nfserr_wrong_cred;
3880 unhash_client_locked(clp);
3882 spin_unlock(&nn->client_lock);
3889 nfsd4_reclaim_complete(struct svc_rqst *rqstp,
3890 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
3892 struct nfsd4_reclaim_complete *rc = &u->reclaim_complete;
3895 if (rc->rca_one_fs) {
3896 if (!cstate->current_fh.fh_dentry)
3897 return nfserr_nofilehandle;
3899 * We don't take advantage of the rca_one_fs case.
3900 * That's OK, it's optional, we can safely ignore it.
3905 status = nfserr_complete_already;
3906 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
3907 &cstate->session->se_client->cl_flags))
3910 status = nfserr_stale_clientid;
3911 if (is_client_expired(cstate->session->se_client))
3913 * The following error isn't really legal.
3914 * But we only get here if the client just explicitly
3915 * destroyed the client. Surely it no longer cares what
3916 * error it gets back on an operation for the dead
3922 nfsd4_client_record_create(cstate->session->se_client);
3923 inc_reclaim_complete(cstate->session->se_client);
3929 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3930 union nfsd4_op_u *u)
3932 struct nfsd4_setclientid *setclid = &u->setclientid;
3933 struct xdr_netobj clname = setclid->se_name;
3934 nfs4_verifier clverifier = setclid->se_verf;
3935 struct nfs4_client *conf, *new;
3936 struct nfs4_client *unconf = NULL;
3938 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3940 new = create_client(clname, rqstp, &clverifier);
3942 return nfserr_jukebox;
3943 /* Cases below refer to rfc 3530 section 14.2.33: */
3944 spin_lock(&nn->client_lock);
3945 conf = find_confirmed_client_by_name(&clname, nn);
3946 if (conf && client_has_state(conf)) {
3948 status = nfserr_clid_inuse;
3949 if (clp_used_exchangeid(conf))
3951 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
3952 trace_nfsd_clid_inuse_err(conf);
3956 unconf = find_unconfirmed_client_by_name(&clname, nn);
3958 unhash_client_locked(unconf);
3959 /* We need to handle only case 1: probable callback update */
3960 if (conf && same_verf(&conf->cl_verifier, &clverifier)) {
3961 copy_clid(new, conf);
3962 gen_confirm(new, nn);
3964 new->cl_minorversion = 0;
3965 gen_callback(new, setclid, rqstp);
3966 add_to_unconfirmed(new);
3967 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
3968 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
3969 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
3973 spin_unlock(&nn->client_lock);
3977 expire_client(unconf);
3983 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
3984 struct nfsd4_compound_state *cstate,
3985 union nfsd4_op_u *u)
3987 struct nfsd4_setclientid_confirm *setclientid_confirm =
3988 &u->setclientid_confirm;
3989 struct nfs4_client *conf, *unconf;
3990 struct nfs4_client *old = NULL;
3991 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
3992 clientid_t * clid = &setclientid_confirm->sc_clientid;
3994 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3996 if (STALE_CLIENTID(clid, nn))
3997 return nfserr_stale_clientid;
3999 spin_lock(&nn->client_lock);
4000 conf = find_confirmed_client(clid, false, nn);
4001 unconf = find_unconfirmed_client(clid, false, nn);
4003 * We try hard to give out unique clientid's, so if we get an
4004 * attempt to confirm the same clientid with a different cred,
4005 * the client may be buggy; this should never happen.
4007 * Nevertheless, RFC 7530 recommends INUSE for this case:
4009 status = nfserr_clid_inuse;
4010 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
4012 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
4014 /* cases below refer to rfc 3530 section 14.2.34: */
4015 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
4016 if (conf && same_verf(&confirm, &conf->cl_confirm)) {
4017 /* case 2: probable retransmit */
4019 } else /* case 4: client hasn't noticed we rebooted yet? */
4020 status = nfserr_stale_clientid;
4024 if (conf) { /* case 1: callback update */
4026 unhash_client_locked(old);
4027 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
4028 } else { /* case 3: normal case; new or rebooted client */
4029 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
4031 status = nfserr_clid_inuse;
4032 if (client_has_state(old)
4033 && !same_creds(&unconf->cl_cred,
4036 status = mark_client_expired_locked(old);
4042 move_to_confirmed(unconf);
4045 get_client_locked(conf);
4046 spin_unlock(&nn->client_lock);
4047 nfsd4_probe_callback(conf);
4048 spin_lock(&nn->client_lock);
4049 put_client_renew_locked(conf);
4051 spin_unlock(&nn->client_lock);
4057 static struct nfs4_file *nfsd4_alloc_file(void)
4059 return kmem_cache_alloc(file_slab, GFP_KERNEL);
4062 /* OPEN Share state helper functions */
4063 static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
4064 struct nfs4_file *fp)
4066 lockdep_assert_held(&state_lock);
4068 refcount_set(&fp->fi_ref, 1);
4069 spin_lock_init(&fp->fi_lock);
4070 INIT_LIST_HEAD(&fp->fi_stateids);
4071 INIT_LIST_HEAD(&fp->fi_delegations);
4072 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
4073 fh_copy_shallow(&fp->fi_fhandle, fh);
4074 fp->fi_deleg_file = NULL;
4075 fp->fi_had_conflict = false;
4076 fp->fi_share_deny = 0;
4077 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
4078 memset(fp->fi_access, 0, sizeof(fp->fi_access));
4079 #ifdef CONFIG_NFSD_PNFS
4080 INIT_LIST_HEAD(&fp->fi_lo_states);
4081 atomic_set(&fp->fi_lo_recalls, 0);
4083 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
4087 nfsd4_free_slabs(void)
4089 kmem_cache_destroy(client_slab);
4090 kmem_cache_destroy(openowner_slab);
4091 kmem_cache_destroy(lockowner_slab);
4092 kmem_cache_destroy(file_slab);
4093 kmem_cache_destroy(stateid_slab);
4094 kmem_cache_destroy(deleg_slab);
4095 kmem_cache_destroy(odstate_slab);
4099 nfsd4_init_slabs(void)
4101 client_slab = kmem_cache_create("nfsd4_clients",
4102 sizeof(struct nfs4_client), 0, 0, NULL);
4103 if (client_slab == NULL)
4105 openowner_slab = kmem_cache_create("nfsd4_openowners",
4106 sizeof(struct nfs4_openowner), 0, 0, NULL);
4107 if (openowner_slab == NULL)
4108 goto out_free_client_slab;
4109 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
4110 sizeof(struct nfs4_lockowner), 0, 0, NULL);
4111 if (lockowner_slab == NULL)
4112 goto out_free_openowner_slab;
4113 file_slab = kmem_cache_create("nfsd4_files",
4114 sizeof(struct nfs4_file), 0, 0, NULL);
4115 if (file_slab == NULL)
4116 goto out_free_lockowner_slab;
4117 stateid_slab = kmem_cache_create("nfsd4_stateids",
4118 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
4119 if (stateid_slab == NULL)
4120 goto out_free_file_slab;
4121 deleg_slab = kmem_cache_create("nfsd4_delegations",
4122 sizeof(struct nfs4_delegation), 0, 0, NULL);
4123 if (deleg_slab == NULL)
4124 goto out_free_stateid_slab;
4125 odstate_slab = kmem_cache_create("nfsd4_odstate",
4126 sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
4127 if (odstate_slab == NULL)
4128 goto out_free_deleg_slab;
4131 out_free_deleg_slab:
4132 kmem_cache_destroy(deleg_slab);
4133 out_free_stateid_slab:
4134 kmem_cache_destroy(stateid_slab);
4136 kmem_cache_destroy(file_slab);
4137 out_free_lockowner_slab:
4138 kmem_cache_destroy(lockowner_slab);
4139 out_free_openowner_slab:
4140 kmem_cache_destroy(openowner_slab);
4141 out_free_client_slab:
4142 kmem_cache_destroy(client_slab);
4147 static void init_nfs4_replay(struct nfs4_replay *rp)
4149 rp->rp_status = nfserr_serverfault;
4151 rp->rp_buf = rp->rp_ibuf;
4152 mutex_init(&rp->rp_mutex);
4155 static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
4156 struct nfs4_stateowner *so)
4158 if (!nfsd4_has_session(cstate)) {
4159 mutex_lock(&so->so_replay.rp_mutex);
4160 cstate->replay_owner = nfs4_get_stateowner(so);
4164 void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
4166 struct nfs4_stateowner *so = cstate->replay_owner;
4169 cstate->replay_owner = NULL;
4170 mutex_unlock(&so->so_replay.rp_mutex);
4171 nfs4_put_stateowner(so);
4175 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
4177 struct nfs4_stateowner *sop;
4179 sop = kmem_cache_alloc(slab, GFP_KERNEL);
4183 xdr_netobj_dup(&sop->so_owner, owner, GFP_KERNEL);
4184 if (!sop->so_owner.data) {
4185 kmem_cache_free(slab, sop);
4189 INIT_LIST_HEAD(&sop->so_stateids);
4190 sop->so_client = clp;
4191 init_nfs4_replay(&sop->so_replay);
4192 atomic_set(&sop->so_count, 1);
4196 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
4198 lockdep_assert_held(&clp->cl_lock);
4200 list_add(&oo->oo_owner.so_strhash,
4201 &clp->cl_ownerstr_hashtbl[strhashval]);
4202 list_add(&oo->oo_perclient, &clp->cl_openowners);
4205 static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
4207 unhash_openowner_locked(openowner(so));
4210 static void nfs4_free_openowner(struct nfs4_stateowner *so)
4212 struct nfs4_openowner *oo = openowner(so);
4214 kmem_cache_free(openowner_slab, oo);
4217 static const struct nfs4_stateowner_operations openowner_ops = {
4218 .so_unhash = nfs4_unhash_openowner,
4219 .so_free = nfs4_free_openowner,
4222 static struct nfs4_ol_stateid *
4223 nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4225 struct nfs4_ol_stateid *local, *ret = NULL;
4226 struct nfs4_openowner *oo = open->op_openowner;
4228 lockdep_assert_held(&fp->fi_lock);
4230 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
4231 /* ignore lock owners */
4232 if (local->st_stateowner->so_is_open_owner == 0)
4234 if (local->st_stateowner != &oo->oo_owner)
4236 if (local->st_stid.sc_type == NFS4_OPEN_STID) {
4238 refcount_inc(&ret->st_stid.sc_count);
4246 nfsd4_verify_open_stid(struct nfs4_stid *s)
4248 __be32 ret = nfs_ok;
4250 switch (s->sc_type) {
4254 case NFS4_CLOSED_STID:
4255 case NFS4_CLOSED_DELEG_STID:
4256 ret = nfserr_bad_stateid;
4258 case NFS4_REVOKED_DELEG_STID:
4259 ret = nfserr_deleg_revoked;
4264 /* Lock the stateid st_mutex, and deal with races with CLOSE */
4266 nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp)
4270 mutex_lock_nested(&stp->st_mutex, LOCK_STATEID_MUTEX);
4271 ret = nfsd4_verify_open_stid(&stp->st_stid);
4273 mutex_unlock(&stp->st_mutex);
4277 static struct nfs4_ol_stateid *
4278 nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4280 struct nfs4_ol_stateid *stp;
4282 spin_lock(&fp->fi_lock);
4283 stp = nfsd4_find_existing_open(fp, open);
4284 spin_unlock(&fp->fi_lock);
4285 if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok)
4287 nfs4_put_stid(&stp->st_stid);
4292 static struct nfs4_openowner *
4293 alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
4294 struct nfsd4_compound_state *cstate)
4296 struct nfs4_client *clp = cstate->clp;
4297 struct nfs4_openowner *oo, *ret;
4299 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
4302 oo->oo_owner.so_ops = &openowner_ops;
4303 oo->oo_owner.so_is_open_owner = 1;
4304 oo->oo_owner.so_seqid = open->op_seqid;
4306 if (nfsd4_has_session(cstate))
4307 oo->oo_flags |= NFS4_OO_CONFIRMED;
4309 oo->oo_last_closed_stid = NULL;
4310 INIT_LIST_HEAD(&oo->oo_close_lru);
4311 spin_lock(&clp->cl_lock);
4312 ret = find_openstateowner_str_locked(strhashval, open, clp);
4314 hash_openowner(oo, clp, strhashval);
4317 nfs4_free_stateowner(&oo->oo_owner);
4319 spin_unlock(&clp->cl_lock);
4323 static struct nfs4_ol_stateid *
4324 init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
4327 struct nfs4_openowner *oo = open->op_openowner;
4328 struct nfs4_ol_stateid *retstp = NULL;
4329 struct nfs4_ol_stateid *stp;
4332 /* We are moving these outside of the spinlocks to avoid the warnings */
4333 mutex_init(&stp->st_mutex);
4334 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
4337 spin_lock(&oo->oo_owner.so_client->cl_lock);
4338 spin_lock(&fp->fi_lock);
4340 retstp = nfsd4_find_existing_open(fp, open);
4344 open->op_stp = NULL;
4345 refcount_inc(&stp->st_stid.sc_count);
4346 stp->st_stid.sc_type = NFS4_OPEN_STID;
4347 INIT_LIST_HEAD(&stp->st_locks);
4348 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
4350 stp->st_stid.sc_file = fp;
4351 stp->st_access_bmap = 0;
4352 stp->st_deny_bmap = 0;
4353 stp->st_openstp = NULL;
4354 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
4355 list_add(&stp->st_perfile, &fp->fi_stateids);
4358 spin_unlock(&fp->fi_lock);
4359 spin_unlock(&oo->oo_owner.so_client->cl_lock);
4361 /* Handle races with CLOSE */
4362 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
4363 nfs4_put_stid(&retstp->st_stid);
4366 /* To keep mutex tracking happy */
4367 mutex_unlock(&stp->st_mutex);
4374 * In the 4.0 case we need to keep the owners around a little while to handle
4375 * CLOSE replay. We still do need to release any file access that is held by
4376 * them before returning however.
4379 move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
4381 struct nfs4_ol_stateid *last;
4382 struct nfs4_openowner *oo = openowner(s->st_stateowner);
4383 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
4386 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
4389 * We know that we hold one reference via nfsd4_close, and another
4390 * "persistent" reference for the client. If the refcount is higher
4391 * than 2, then there are still calls in progress that are using this
4392 * stateid. We can't put the sc_file reference until they are finished.
4393 * Wait for the refcount to drop to 2. Since it has been unhashed,
4394 * there should be no danger of the refcount going back up again at
4397 wait_event(close_wq, refcount_read(&s->st_stid.sc_count) == 2);
4399 release_all_access(s);
4400 if (s->st_stid.sc_file) {
4401 put_nfs4_file(s->st_stid.sc_file);
4402 s->st_stid.sc_file = NULL;
4405 spin_lock(&nn->client_lock);
4406 last = oo->oo_last_closed_stid;
4407 oo->oo_last_closed_stid = s;
4408 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
4409 oo->oo_time = ktime_get_boottime_seconds();
4410 spin_unlock(&nn->client_lock);
4412 nfs4_put_stid(&last->st_stid);
4415 /* search file_hashtbl[] for file */
4416 static struct nfs4_file *
4417 find_file_locked(struct knfsd_fh *fh, unsigned int hashval)
4419 struct nfs4_file *fp;
4421 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash,
4422 lockdep_is_held(&state_lock)) {
4423 if (fh_match(&fp->fi_fhandle, fh)) {
4424 if (refcount_inc_not_zero(&fp->fi_ref))
4432 find_file(struct knfsd_fh *fh)
4434 struct nfs4_file *fp;
4435 unsigned int hashval = file_hashval(fh);
4438 fp = find_file_locked(fh, hashval);
4443 static struct nfs4_file *
4444 find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
4446 struct nfs4_file *fp;
4447 unsigned int hashval = file_hashval(fh);
4450 fp = find_file_locked(fh, hashval);
4455 spin_lock(&state_lock);
4456 fp = find_file_locked(fh, hashval);
4457 if (likely(fp == NULL)) {
4458 nfsd4_init_file(fh, hashval, new);
4461 spin_unlock(&state_lock);
4467 * Called to check deny when READ with all zero stateid or
4468 * WRITE with all zero or all one stateid
4471 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
4473 struct nfs4_file *fp;
4474 __be32 ret = nfs_ok;
4476 fp = find_file(¤t_fh->fh_handle);
4479 /* Check for conflicting share reservations */
4480 spin_lock(&fp->fi_lock);
4481 if (fp->fi_share_deny & deny_type)
4482 ret = nfserr_locked;
4483 spin_unlock(&fp->fi_lock);
4488 static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
4490 struct nfs4_delegation *dp = cb_to_delegation(cb);
4491 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
4494 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
4497 * We can't do this in nfsd_break_deleg_cb because it is
4498 * already holding inode->i_lock.
4500 * If the dl_time != 0, then we know that it has already been
4501 * queued for a lease break. Don't queue it again.
4503 spin_lock(&state_lock);
4504 if (dp->dl_time == 0) {
4505 dp->dl_time = ktime_get_boottime_seconds();
4506 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
4508 spin_unlock(&state_lock);
4511 static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
4512 struct rpc_task *task)
4514 struct nfs4_delegation *dp = cb_to_delegation(cb);
4516 if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID ||
4517 dp->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID)
4520 switch (task->tk_status) {
4523 case -NFS4ERR_DELAY:
4524 rpc_delay(task, 2 * HZ);
4527 case -NFS4ERR_BAD_STATEID:
4529 * Race: client probably got cb_recall before open reply
4530 * granting delegation.
4532 if (dp->dl_retries--) {
4533 rpc_delay(task, 2 * HZ);
4542 static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
4544 struct nfs4_delegation *dp = cb_to_delegation(cb);
4546 nfs4_put_stid(&dp->dl_stid);
4549 static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
4550 .prepare = nfsd4_cb_recall_prepare,
4551 .done = nfsd4_cb_recall_done,
4552 .release = nfsd4_cb_recall_release,
4555 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
4558 * We're assuming the state code never drops its reference
4559 * without first removing the lease. Since we're in this lease
4560 * callback (and since the lease code is serialized by the
4561 * i_lock) we know the server hasn't removed the lease yet, and
4562 * we know it's safe to take a reference.
4564 refcount_inc(&dp->dl_stid.sc_count);
4565 nfsd4_run_cb(&dp->dl_recall);
4568 /* Called from break_lease() with i_lock held. */
4570 nfsd_break_deleg_cb(struct file_lock *fl)
4573 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
4574 struct nfs4_file *fp = dp->dl_stid.sc_file;
4576 trace_nfsd_deleg_break(&dp->dl_stid.sc_stateid);
4579 * We don't want the locks code to timeout the lease for us;
4580 * we'll remove it ourself if a delegation isn't returned
4583 fl->fl_break_time = 0;
4585 spin_lock(&fp->fi_lock);
4586 fp->fi_had_conflict = true;
4587 nfsd_break_one_deleg(dp);
4588 spin_unlock(&fp->fi_lock);
4592 static bool nfsd_breaker_owns_lease(struct file_lock *fl)
4594 struct nfs4_delegation *dl = fl->fl_owner;
4595 struct svc_rqst *rqst;
4596 struct nfs4_client *clp;
4600 rqst = kthread_data(current);
4601 /* Note rq_prog == NFS_ACL_PROGRAM is also possible: */
4602 if (rqst->rq_prog != NFS_PROGRAM || rqst->rq_vers < 4)
4604 clp = *(rqst->rq_lease_breaker);
4605 return dl->dl_stid.sc_client == clp;
4609 nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
4610 struct list_head *dispose)
4613 return lease_modify(onlist, arg, dispose);
4618 static const struct lock_manager_operations nfsd_lease_mng_ops = {
4619 .lm_breaker_owns_lease = nfsd_breaker_owns_lease,
4620 .lm_break = nfsd_break_deleg_cb,
4621 .lm_change = nfsd_change_deleg_cb,
4624 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
4626 if (nfsd4_has_session(cstate))
4628 if (seqid == so->so_seqid - 1)
4629 return nfserr_replay_me;
4630 if (seqid == so->so_seqid)
4632 return nfserr_bad_seqid;
4635 static __be32 lookup_clientid(clientid_t *clid,
4636 struct nfsd4_compound_state *cstate,
4637 struct nfsd_net *nn,
4640 struct nfs4_client *found;
4643 found = cstate->clp;
4644 if (!same_clid(&found->cl_clientid, clid))
4645 return nfserr_stale_clientid;
4649 if (STALE_CLIENTID(clid, nn))
4650 return nfserr_stale_clientid;
4653 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
4654 * cached already then we know this is for is for v4.0 and "sessions"
4657 WARN_ON_ONCE(cstate->session);
4658 spin_lock(&nn->client_lock);
4659 found = find_confirmed_client(clid, sessions, nn);
4661 spin_unlock(&nn->client_lock);
4662 return nfserr_expired;
4664 atomic_inc(&found->cl_rpc_users);
4665 spin_unlock(&nn->client_lock);
4667 /* Cache the nfs4_client in cstate! */
4668 cstate->clp = found;
4673 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
4674 struct nfsd4_open *open, struct nfsd_net *nn)
4676 clientid_t *clientid = &open->op_clientid;
4677 struct nfs4_client *clp = NULL;
4678 unsigned int strhashval;
4679 struct nfs4_openowner *oo = NULL;
4682 if (STALE_CLIENTID(&open->op_clientid, nn))
4683 return nfserr_stale_clientid;
4685 * In case we need it later, after we've already created the
4686 * file and don't want to risk a further failure:
4688 open->op_file = nfsd4_alloc_file();
4689 if (open->op_file == NULL)
4690 return nfserr_jukebox;
4692 status = lookup_clientid(clientid, cstate, nn, false);
4697 strhashval = ownerstr_hashval(&open->op_owner);
4698 oo = find_openstateowner_str(strhashval, open, clp);
4699 open->op_openowner = oo;
4703 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4704 /* Replace unconfirmed owners without checking for replay. */
4705 release_openowner(oo);
4706 open->op_openowner = NULL;
4709 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
4714 oo = alloc_init_open_stateowner(strhashval, open, cstate);
4716 return nfserr_jukebox;
4717 open->op_openowner = oo;
4719 open->op_stp = nfs4_alloc_open_stateid(clp);
4721 return nfserr_jukebox;
4723 if (nfsd4_has_session(cstate) &&
4724 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
4725 open->op_odstate = alloc_clnt_odstate(clp);
4726 if (!open->op_odstate)
4727 return nfserr_jukebox;
4733 static inline __be32
4734 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
4736 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
4737 return nfserr_openmode;
4742 static int share_access_to_flags(u32 share_access)
4744 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
4747 static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
4749 struct nfs4_stid *ret;
4751 ret = find_stateid_by_type(cl, s,
4752 NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID);
4755 return delegstateid(ret);
4758 static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
4760 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
4761 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
4765 nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
4766 struct nfs4_delegation **dp)
4769 __be32 status = nfserr_bad_stateid;
4770 struct nfs4_delegation *deleg;
4772 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
4775 if (deleg->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) {
4776 nfs4_put_stid(&deleg->dl_stid);
4777 if (cl->cl_minorversion)
4778 status = nfserr_deleg_revoked;
4781 flags = share_access_to_flags(open->op_share_access);
4782 status = nfs4_check_delegmode(deleg, flags);
4784 nfs4_put_stid(&deleg->dl_stid);
4789 if (!nfsd4_is_deleg_cur(open))
4793 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
4797 static inline int nfs4_access_to_access(u32 nfs4_access)
4801 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
4802 flags |= NFSD_MAY_READ;
4803 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
4804 flags |= NFSD_MAY_WRITE;
4808 static inline __be32
4809 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
4810 struct nfsd4_open *open)
4812 struct iattr iattr = {
4813 .ia_valid = ATTR_SIZE,
4816 if (!open->op_truncate)
4818 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
4819 return nfserr_inval;
4820 return nfsd_setattr(rqstp, fh, &iattr, 0, (time64_t)0);
4823 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
4824 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
4825 struct nfsd4_open *open)
4827 struct nfsd_file *nf = NULL;
4829 int oflag = nfs4_access_to_omode(open->op_share_access);
4830 int access = nfs4_access_to_access(open->op_share_access);
4831 unsigned char old_access_bmap, old_deny_bmap;
4833 spin_lock(&fp->fi_lock);
4836 * Are we trying to set a deny mode that would conflict with
4839 status = nfs4_file_check_deny(fp, open->op_share_deny);
4840 if (status != nfs_ok) {
4841 spin_unlock(&fp->fi_lock);
4845 /* set access to the file */
4846 status = nfs4_file_get_access(fp, open->op_share_access);
4847 if (status != nfs_ok) {
4848 spin_unlock(&fp->fi_lock);
4852 /* Set access bits in stateid */
4853 old_access_bmap = stp->st_access_bmap;
4854 set_access(open->op_share_access, stp);
4856 /* Set new deny mask */
4857 old_deny_bmap = stp->st_deny_bmap;
4858 set_deny(open->op_share_deny, stp);
4859 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4861 if (!fp->fi_fds[oflag]) {
4862 spin_unlock(&fp->fi_lock);
4863 status = nfsd_file_acquire(rqstp, cur_fh, access, &nf);
4865 goto out_put_access;
4866 spin_lock(&fp->fi_lock);
4867 if (!fp->fi_fds[oflag]) {
4868 fp->fi_fds[oflag] = nf;
4872 spin_unlock(&fp->fi_lock);
4876 status = nfsd4_truncate(rqstp, cur_fh, open);
4878 goto out_put_access;
4882 stp->st_access_bmap = old_access_bmap;
4883 nfs4_file_put_access(fp, open->op_share_access);
4884 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
4889 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)
4892 unsigned char old_deny_bmap = stp->st_deny_bmap;
4894 if (!test_access(open->op_share_access, stp))
4895 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
4897 /* test and set deny mode */
4898 spin_lock(&fp->fi_lock);
4899 status = nfs4_file_check_deny(fp, open->op_share_deny);
4900 if (status == nfs_ok) {
4901 set_deny(open->op_share_deny, stp);
4902 fp->fi_share_deny |=
4903 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4905 spin_unlock(&fp->fi_lock);
4907 if (status != nfs_ok)
4910 status = nfsd4_truncate(rqstp, cur_fh, open);
4911 if (status != nfs_ok)
4912 reset_union_bmap_deny(old_deny_bmap, stp);
4916 /* Should we give out recallable state?: */
4917 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
4919 if (clp->cl_cb_state == NFSD4_CB_UP)
4922 * In the sessions case, since we don't have to establish a
4923 * separate connection for callbacks, we assume it's OK
4924 * until we hear otherwise:
4926 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
4929 static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp,
4932 struct file_lock *fl;
4934 fl = locks_alloc_lock();
4937 fl->fl_lmops = &nfsd_lease_mng_ops;
4938 fl->fl_flags = FL_DELEG;
4939 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
4940 fl->fl_end = OFFSET_MAX;
4941 fl->fl_owner = (fl_owner_t)dp;
4942 fl->fl_pid = current->tgid;
4943 fl->fl_file = dp->dl_stid.sc_file->fi_deleg_file->nf_file;
4947 static int nfsd4_check_conflicting_opens(struct nfs4_client *clp,
4948 struct nfs4_file *fp)
4950 struct nfs4_clnt_odstate *co;
4951 struct file *f = fp->fi_deleg_file->nf_file;
4952 struct inode *ino = locks_inode(f);
4953 int writes = atomic_read(&ino->i_writecount);
4955 if (fp->fi_fds[O_WRONLY])
4957 if (fp->fi_fds[O_RDWR])
4961 spin_lock(&fp->fi_lock);
4962 list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
4963 if (co->co_client != clp) {
4964 spin_unlock(&fp->fi_lock);
4968 spin_unlock(&fp->fi_lock);
4972 static struct nfs4_delegation *
4973 nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
4974 struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate)
4977 struct nfs4_delegation *dp;
4978 struct nfsd_file *nf;
4979 struct file_lock *fl;
4982 * The fi_had_conflict and nfs_get_existing_delegation checks
4983 * here are just optimizations; we'll need to recheck them at
4986 if (fp->fi_had_conflict)
4987 return ERR_PTR(-EAGAIN);
4989 nf = find_readable_file(fp);
4992 * We probably could attempt another open and get a read
4993 * delegation, but for now, don't bother until the
4994 * client actually sends us one.
4996 return ERR_PTR(-EAGAIN);
4998 spin_lock(&state_lock);
4999 spin_lock(&fp->fi_lock);
5000 if (nfs4_delegation_exists(clp, fp))
5002 else if (!fp->fi_deleg_file) {
5003 fp->fi_deleg_file = nf;
5004 /* increment early to prevent fi_deleg_file from being
5006 fp->fi_delegees = 1;
5010 spin_unlock(&fp->fi_lock);
5011 spin_unlock(&state_lock);
5015 return ERR_PTR(status);
5018 dp = alloc_init_deleg(clp, fp, fh, odstate);
5022 fl = nfs4_alloc_init_lease(dp, NFS4_OPEN_DELEGATE_READ);
5024 goto out_clnt_odstate;
5026 status = nfsd4_check_conflicting_opens(clp, fp);
5028 locks_free_lock(fl);
5029 goto out_clnt_odstate;
5031 status = vfs_setlease(fp->fi_deleg_file->nf_file, fl->fl_type, &fl, NULL);
5033 locks_free_lock(fl);
5035 goto out_clnt_odstate;
5036 status = nfsd4_check_conflicting_opens(clp, fp);
5038 goto out_clnt_odstate;
5040 spin_lock(&state_lock);
5041 spin_lock(&fp->fi_lock);
5042 if (fp->fi_had_conflict)
5045 status = hash_delegation_locked(dp, fp);
5046 spin_unlock(&fp->fi_lock);
5047 spin_unlock(&state_lock);
5054 vfs_setlease(fp->fi_deleg_file->nf_file, F_UNLCK, NULL, (void **)&dp);
5056 put_clnt_odstate(dp->dl_clnt_odstate);
5057 nfs4_put_stid(&dp->dl_stid);
5060 return ERR_PTR(status);
5063 static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
5065 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5066 if (status == -EAGAIN)
5067 open->op_why_no_deleg = WND4_CONTENTION;
5069 open->op_why_no_deleg = WND4_RESOURCE;
5070 switch (open->op_deleg_want) {
5071 case NFS4_SHARE_WANT_READ_DELEG:
5072 case NFS4_SHARE_WANT_WRITE_DELEG:
5073 case NFS4_SHARE_WANT_ANY_DELEG:
5075 case NFS4_SHARE_WANT_CANCEL:
5076 open->op_why_no_deleg = WND4_CANCELLED;
5078 case NFS4_SHARE_WANT_NO_DELEG:
5085 * Attempt to hand out a delegation.
5087 * Note we don't support write delegations, and won't until the vfs has
5088 * proper support for them.
5091 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
5092 struct nfs4_ol_stateid *stp)
5094 struct nfs4_delegation *dp;
5095 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
5096 struct nfs4_client *clp = stp->st_stid.sc_client;
5100 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
5101 open->op_recall = 0;
5102 switch (open->op_claim_type) {
5103 case NFS4_OPEN_CLAIM_PREVIOUS:
5105 open->op_recall = 1;
5106 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
5109 case NFS4_OPEN_CLAIM_NULL:
5110 case NFS4_OPEN_CLAIM_FH:
5112 * Let's not give out any delegations till everyone's
5113 * had the chance to reclaim theirs, *and* until
5114 * NLM locks have all been reclaimed:
5116 if (locks_in_grace(clp->net))
5118 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
5124 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate);
5128 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
5130 trace_nfsd_deleg_read(&dp->dl_stid.sc_stateid);
5131 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
5132 nfs4_put_stid(&dp->dl_stid);
5135 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
5136 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
5137 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
5138 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
5139 open->op_recall = 1;
5142 /* 4.1 client asking for a delegation? */
5143 if (open->op_deleg_want)
5144 nfsd4_open_deleg_none_ext(open, status);
5148 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
5149 struct nfs4_delegation *dp)
5151 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
5152 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
5153 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5154 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
5155 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
5156 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
5157 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5158 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
5160 /* Otherwise the client must be confused wanting a delegation
5161 * it already has, therefore we don't return
5162 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
5167 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
5169 struct nfsd4_compoundres *resp = rqstp->rq_resp;
5170 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
5171 struct nfs4_file *fp = NULL;
5172 struct nfs4_ol_stateid *stp = NULL;
5173 struct nfs4_delegation *dp = NULL;
5175 bool new_stp = false;
5178 * Lookup file; if found, lookup stateid and check open request,
5179 * and check for delegations in the process of being recalled.
5180 * If not found, create the nfs4_file struct
5182 fp = find_or_add_file(open->op_file, ¤t_fh->fh_handle);
5183 if (fp != open->op_file) {
5184 status = nfs4_check_deleg(cl, open, &dp);
5187 stp = nfsd4_find_and_lock_existing_open(fp, open);
5189 open->op_file = NULL;
5190 status = nfserr_bad_stateid;
5191 if (nfsd4_is_deleg_cur(open))
5196 stp = init_open_stateid(fp, open);
5202 * OPEN the file, or upgrade an existing OPEN.
5203 * If truncate fails, the OPEN fails.
5205 * stp is already locked.
5208 /* Stateid was found, this is an OPEN upgrade */
5209 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
5211 mutex_unlock(&stp->st_mutex);
5215 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
5217 stp->st_stid.sc_type = NFS4_CLOSED_STID;
5218 release_open_stateid(stp);
5219 mutex_unlock(&stp->st_mutex);
5223 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
5225 if (stp->st_clnt_odstate == open->op_odstate)
5226 open->op_odstate = NULL;
5229 nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
5230 mutex_unlock(&stp->st_mutex);
5232 if (nfsd4_has_session(&resp->cstate)) {
5233 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
5234 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5235 open->op_why_no_deleg = WND4_NOT_WANTED;
5241 * Attempt to hand out a delegation. No error return, because the
5242 * OPEN succeeds even if we fail.
5244 nfs4_open_delegation(current_fh, open, stp);
5247 trace_nfsd_open(&stp->st_stid.sc_stateid);
5249 /* 4.1 client trying to upgrade/downgrade delegation? */
5250 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
5251 open->op_deleg_want)
5252 nfsd4_deleg_xgrade_none_ext(open, dp);
5256 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
5257 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
5259 * To finish the open response, we just need to set the rflags.
5261 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
5262 if (nfsd4_has_session(&resp->cstate))
5263 open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK;
5264 else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED))
5265 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
5268 nfs4_put_stid(&dp->dl_stid);
5270 nfs4_put_stid(&stp->st_stid);
5275 void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
5276 struct nfsd4_open *open)
5278 if (open->op_openowner) {
5279 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
5281 nfsd4_cstate_assign_replay(cstate, so);
5282 nfs4_put_stateowner(so);
5285 kmem_cache_free(file_slab, open->op_file);
5287 nfs4_put_stid(&open->op_stp->st_stid);
5288 if (open->op_odstate)
5289 kmem_cache_free(odstate_slab, open->op_odstate);
5293 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5294 union nfsd4_op_u *u)
5296 clientid_t *clid = &u->renew;
5297 struct nfs4_client *clp;
5299 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5301 trace_nfsd_clid_renew(clid);
5302 status = lookup_clientid(clid, cstate, nn, false);
5306 status = nfserr_cb_path_down;
5307 if (!list_empty(&clp->cl_delegations)
5308 && clp->cl_cb_state != NFSD4_CB_UP)
5316 nfsd4_end_grace(struct nfsd_net *nn)
5318 /* do nothing if grace period already ended */
5319 if (nn->grace_ended)
5322 trace_nfsd_grace_complete(nn);
5323 nn->grace_ended = true;
5325 * If the server goes down again right now, an NFSv4
5326 * client will still be allowed to reclaim after it comes back up,
5327 * even if it hasn't yet had a chance to reclaim state this time.
5330 nfsd4_record_grace_done(nn);
5332 * At this point, NFSv4 clients can still reclaim. But if the
5333 * server crashes, any that have not yet reclaimed will be out
5334 * of luck on the next boot.
5336 * (NFSv4.1+ clients are considered to have reclaimed once they
5337 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
5338 * have reclaimed after their first OPEN.)
5340 locks_end_grace(&nn->nfsd4_manager);
5342 * At this point, and once lockd and/or any other containers
5343 * exit their grace period, further reclaims will fail and
5344 * regular locking can resume.
5349 * If we've waited a lease period but there are still clients trying to
5350 * reclaim, wait a little longer to give them a chance to finish.
5352 static bool clients_still_reclaiming(struct nfsd_net *nn)
5354 time64_t double_grace_period_end = nn->boot_time +
5355 2 * nn->nfsd4_lease;
5357 if (nn->track_reclaim_completes &&
5358 atomic_read(&nn->nr_reclaim_complete) ==
5359 nn->reclaim_str_hashtbl_size)
5361 if (!nn->somebody_reclaimed)
5363 nn->somebody_reclaimed = false;
5365 * If we've given them *two* lease times to reclaim, and they're
5366 * still not done, give up:
5368 if (ktime_get_boottime_seconds() > double_grace_period_end)
5374 nfs4_laundromat(struct nfsd_net *nn)
5376 struct nfs4_client *clp;
5377 struct nfs4_openowner *oo;
5378 struct nfs4_delegation *dp;
5379 struct nfs4_ol_stateid *stp;
5380 struct nfsd4_blocked_lock *nbl;
5381 struct list_head *pos, *next, reaplist;
5382 time64_t cutoff = ktime_get_boottime_seconds() - nn->nfsd4_lease;
5383 time64_t t, new_timeo = nn->nfsd4_lease;
5384 struct nfs4_cpntf_state *cps;
5385 copy_stateid_t *cps_t;
5388 if (clients_still_reclaiming(nn)) {
5392 nfsd4_end_grace(nn);
5393 INIT_LIST_HEAD(&reaplist);
5395 spin_lock(&nn->s2s_cp_lock);
5396 idr_for_each_entry(&nn->s2s_cp_stateids, cps_t, i) {
5397 cps = container_of(cps_t, struct nfs4_cpntf_state, cp_stateid);
5398 if (cps->cp_stateid.sc_type == NFS4_COPYNOTIFY_STID &&
5399 cps->cpntf_time > cutoff)
5400 _free_cpntf_state_locked(nn, cps);
5402 spin_unlock(&nn->s2s_cp_lock);
5404 spin_lock(&nn->client_lock);
5405 list_for_each_safe(pos, next, &nn->client_lru) {
5406 clp = list_entry(pos, struct nfs4_client, cl_lru);
5407 if (clp->cl_time > cutoff) {
5408 t = clp->cl_time - cutoff;
5409 new_timeo = min(new_timeo, t);
5412 if (mark_client_expired_locked(clp)) {
5413 trace_nfsd_clid_expired(&clp->cl_clientid);
5416 list_add(&clp->cl_lru, &reaplist);
5418 spin_unlock(&nn->client_lock);
5419 list_for_each_safe(pos, next, &reaplist) {
5420 clp = list_entry(pos, struct nfs4_client, cl_lru);
5421 trace_nfsd_clid_purged(&clp->cl_clientid);
5422 list_del_init(&clp->cl_lru);
5425 spin_lock(&state_lock);
5426 list_for_each_safe(pos, next, &nn->del_recall_lru) {
5427 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
5428 if (dp->dl_time > cutoff) {
5429 t = dp->dl_time - cutoff;
5430 new_timeo = min(new_timeo, t);
5433 WARN_ON(!unhash_delegation_locked(dp));
5434 list_add(&dp->dl_recall_lru, &reaplist);
5436 spin_unlock(&state_lock);
5437 while (!list_empty(&reaplist)) {
5438 dp = list_first_entry(&reaplist, struct nfs4_delegation,
5440 list_del_init(&dp->dl_recall_lru);
5441 revoke_delegation(dp);
5444 spin_lock(&nn->client_lock);
5445 while (!list_empty(&nn->close_lru)) {
5446 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
5448 if (oo->oo_time > cutoff) {
5449 t = oo->oo_time - cutoff;
5450 new_timeo = min(new_timeo, t);
5453 list_del_init(&oo->oo_close_lru);
5454 stp = oo->oo_last_closed_stid;
5455 oo->oo_last_closed_stid = NULL;
5456 spin_unlock(&nn->client_lock);
5457 nfs4_put_stid(&stp->st_stid);
5458 spin_lock(&nn->client_lock);
5460 spin_unlock(&nn->client_lock);
5463 * It's possible for a client to try and acquire an already held lock
5464 * that is being held for a long time, and then lose interest in it.
5465 * So, we clean out any un-revisited request after a lease period
5466 * under the assumption that the client is no longer interested.
5468 * RFC5661, sec. 9.6 states that the client must not rely on getting
5469 * notifications and must continue to poll for locks, even when the
5470 * server supports them. Thus this shouldn't lead to clients blocking
5471 * indefinitely once the lock does become free.
5473 BUG_ON(!list_empty(&reaplist));
5474 spin_lock(&nn->blocked_locks_lock);
5475 while (!list_empty(&nn->blocked_locks_lru)) {
5476 nbl = list_first_entry(&nn->blocked_locks_lru,
5477 struct nfsd4_blocked_lock, nbl_lru);
5478 if (nbl->nbl_time > cutoff) {
5479 t = nbl->nbl_time - cutoff;
5480 new_timeo = min(new_timeo, t);
5483 list_move(&nbl->nbl_lru, &reaplist);
5484 list_del_init(&nbl->nbl_list);
5486 spin_unlock(&nn->blocked_locks_lock);
5488 while (!list_empty(&reaplist)) {
5489 nbl = list_first_entry(&reaplist,
5490 struct nfsd4_blocked_lock, nbl_lru);
5491 list_del_init(&nbl->nbl_lru);
5492 free_blocked_lock(nbl);
5495 new_timeo = max_t(time64_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
5499 static struct workqueue_struct *laundry_wq;
5500 static void laundromat_main(struct work_struct *);
5503 laundromat_main(struct work_struct *laundry)
5506 struct delayed_work *dwork = to_delayed_work(laundry);
5507 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
5510 t = nfs4_laundromat(nn);
5511 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
5514 static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
5516 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
5517 return nfserr_bad_stateid;
5522 access_permit_read(struct nfs4_ol_stateid *stp)
5524 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
5525 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
5526 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
5530 access_permit_write(struct nfs4_ol_stateid *stp)
5532 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
5533 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
5537 __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
5539 __be32 status = nfserr_openmode;
5541 /* For lock stateid's, we test the parent open, not the lock: */
5542 if (stp->st_openstp)
5543 stp = stp->st_openstp;
5544 if ((flags & WR_STATE) && !access_permit_write(stp))
5546 if ((flags & RD_STATE) && !access_permit_read(stp))
5553 static inline __be32
5554 check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
5556 if (ONE_STATEID(stateid) && (flags & RD_STATE))
5558 else if (opens_in_grace(net)) {
5559 /* Answer in remaining cases depends on existence of
5560 * conflicting state; so we must wait out the grace period. */
5561 return nfserr_grace;
5562 } else if (flags & WR_STATE)
5563 return nfs4_share_conflict(current_fh,
5564 NFS4_SHARE_DENY_WRITE);
5565 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
5566 return nfs4_share_conflict(current_fh,
5567 NFS4_SHARE_DENY_READ);
5571 * Allow READ/WRITE during grace period on recovered state only for files
5572 * that are not able to provide mandatory locking.
5575 grace_disallows_io(struct net *net, struct inode *inode)
5577 return opens_in_grace(net) && mandatory_lock(inode);
5580 static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
5583 * When sessions are used the stateid generation number is ignored
5586 if (has_session && in->si_generation == 0)
5589 if (in->si_generation == ref->si_generation)
5592 /* If the client sends us a stateid from the future, it's buggy: */
5593 if (nfsd4_stateid_generation_after(in, ref))
5594 return nfserr_bad_stateid;
5596 * However, we could see a stateid from the past, even from a
5597 * non-buggy client. For example, if the client sends a lock
5598 * while some IO is outstanding, the lock may bump si_generation
5599 * while the IO is still in flight. The client could avoid that
5600 * situation by waiting for responses on all the IO requests,
5601 * but better performance may result in retrying IO that
5602 * receives an old_stateid error if requests are rarely
5603 * reordered in flight:
5605 return nfserr_old_stateid;
5608 static __be32 nfsd4_stid_check_stateid_generation(stateid_t *in, struct nfs4_stid *s, bool has_session)
5612 spin_lock(&s->sc_lock);
5613 ret = nfsd4_verify_open_stid(s);
5615 ret = check_stateid_generation(in, &s->sc_stateid, has_session);
5616 spin_unlock(&s->sc_lock);
5620 static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
5622 if (ols->st_stateowner->so_is_open_owner &&
5623 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
5624 return nfserr_bad_stateid;
5628 static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
5630 struct nfs4_stid *s;
5631 __be32 status = nfserr_bad_stateid;
5633 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5634 CLOSE_STATEID(stateid))
5636 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid))
5638 spin_lock(&cl->cl_lock);
5639 s = find_stateid_locked(cl, stateid);
5642 status = nfsd4_stid_check_stateid_generation(stateid, s, 1);
5645 switch (s->sc_type) {
5646 case NFS4_DELEG_STID:
5649 case NFS4_REVOKED_DELEG_STID:
5650 status = nfserr_deleg_revoked;
5652 case NFS4_OPEN_STID:
5653 case NFS4_LOCK_STID:
5654 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
5657 printk("unknown stateid type %x\n", s->sc_type);
5659 case NFS4_CLOSED_STID:
5660 case NFS4_CLOSED_DELEG_STID:
5661 status = nfserr_bad_stateid;
5664 spin_unlock(&cl->cl_lock);
5669 nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
5670 stateid_t *stateid, unsigned char typemask,
5671 struct nfs4_stid **s, struct nfsd_net *nn)
5674 bool return_revoked = false;
5677 * only return revoked delegations if explicitly asked.
5678 * otherwise we report revoked or bad_stateid status.
5680 if (typemask & NFS4_REVOKED_DELEG_STID)
5681 return_revoked = true;
5682 else if (typemask & NFS4_DELEG_STID)
5683 typemask |= NFS4_REVOKED_DELEG_STID;
5685 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5686 CLOSE_STATEID(stateid))
5687 return nfserr_bad_stateid;
5688 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn,
5690 if (status == nfserr_stale_clientid) {
5691 if (cstate->session)
5692 return nfserr_bad_stateid;
5693 return nfserr_stale_stateid;
5697 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
5699 return nfserr_bad_stateid;
5700 if (((*s)->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) {
5702 if (cstate->minorversion)
5703 return nfserr_deleg_revoked;
5704 return nfserr_bad_stateid;
5709 static struct nfsd_file *
5710 nfs4_find_file(struct nfs4_stid *s, int flags)
5715 switch (s->sc_type) {
5716 case NFS4_DELEG_STID:
5717 if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
5719 return nfsd_file_get(s->sc_file->fi_deleg_file);
5720 case NFS4_OPEN_STID:
5721 case NFS4_LOCK_STID:
5722 if (flags & RD_STATE)
5723 return find_readable_file(s->sc_file);
5725 return find_writeable_file(s->sc_file);
5732 nfs4_check_olstateid(struct nfs4_ol_stateid *ols, int flags)
5736 status = nfsd4_check_openowner_confirmed(ols);
5739 return nfs4_check_openmode(ols, flags);
5743 nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
5744 struct nfsd_file **nfp, int flags)
5746 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
5747 struct nfsd_file *nf;
5750 nf = nfs4_find_file(s, flags);
5752 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
5753 acc | NFSD_MAY_OWNER_OVERRIDE);
5759 status = nfsd_file_acquire(rqstp, fhp, acc, &nf);
5768 _free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
5770 WARN_ON_ONCE(cps->cp_stateid.sc_type != NFS4_COPYNOTIFY_STID);
5771 if (!refcount_dec_and_test(&cps->cp_stateid.sc_count))
5773 list_del(&cps->cp_list);
5774 idr_remove(&nn->s2s_cp_stateids,
5775 cps->cp_stateid.stid.si_opaque.so_id);
5779 * A READ from an inter server to server COPY will have a
5780 * copy stateid. Look up the copy notify stateid from the
5781 * idr structure and take a reference on it.
5783 __be32 manage_cpntf_state(struct nfsd_net *nn, stateid_t *st,
5784 struct nfs4_client *clp,
5785 struct nfs4_cpntf_state **cps)
5787 copy_stateid_t *cps_t;
5788 struct nfs4_cpntf_state *state = NULL;
5790 if (st->si_opaque.so_clid.cl_id != nn->s2s_cp_cl_id)
5791 return nfserr_bad_stateid;
5792 spin_lock(&nn->s2s_cp_lock);
5793 cps_t = idr_find(&nn->s2s_cp_stateids, st->si_opaque.so_id);
5795 state = container_of(cps_t, struct nfs4_cpntf_state,
5797 if (state->cp_stateid.sc_type != NFS4_COPYNOTIFY_STID) {
5802 refcount_inc(&state->cp_stateid.sc_count);
5804 _free_cpntf_state_locked(nn, state);
5807 spin_unlock(&nn->s2s_cp_lock);
5809 return nfserr_bad_stateid;
5815 static __be32 find_cpntf_state(struct nfsd_net *nn, stateid_t *st,
5816 struct nfs4_stid **stid)
5819 struct nfs4_cpntf_state *cps = NULL;
5820 struct nfsd4_compound_state cstate;
5822 status = manage_cpntf_state(nn, st, NULL, &cps);
5826 cps->cpntf_time = ktime_get_boottime_seconds();
5827 memset(&cstate, 0, sizeof(cstate));
5828 status = lookup_clientid(&cps->cp_p_clid, &cstate, nn, true);
5831 status = nfsd4_lookup_stateid(&cstate, &cps->cp_p_stateid,
5832 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
5834 put_client_renew(cstate.clp);
5836 nfs4_put_cpntf_state(nn, cps);
5840 void nfs4_put_cpntf_state(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
5842 spin_lock(&nn->s2s_cp_lock);
5843 _free_cpntf_state_locked(nn, cps);
5844 spin_unlock(&nn->s2s_cp_lock);
5848 * Checks for stateid operations
5851 nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
5852 struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
5853 stateid_t *stateid, int flags, struct nfsd_file **nfp,
5854 struct nfs4_stid **cstid)
5856 struct inode *ino = d_inode(fhp->fh_dentry);
5857 struct net *net = SVC_NET(rqstp);
5858 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5859 struct nfs4_stid *s = NULL;
5865 if (grace_disallows_io(net, ino))
5866 return nfserr_grace;
5868 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
5869 status = check_special_stateids(net, fhp, stateid, flags);
5873 status = nfsd4_lookup_stateid(cstate, stateid,
5874 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
5876 if (status == nfserr_bad_stateid)
5877 status = find_cpntf_state(nn, stateid, &s);
5880 status = nfsd4_stid_check_stateid_generation(stateid, s,
5881 nfsd4_has_session(cstate));
5885 switch (s->sc_type) {
5886 case NFS4_DELEG_STID:
5887 status = nfs4_check_delegmode(delegstateid(s), flags);
5889 case NFS4_OPEN_STID:
5890 case NFS4_LOCK_STID:
5891 status = nfs4_check_olstateid(openlockstateid(s), flags);
5894 status = nfserr_bad_stateid;
5899 status = nfs4_check_fh(fhp, s);
5902 if (status == nfs_ok && nfp)
5903 status = nfs4_check_file(rqstp, fhp, s, nfp, flags);
5906 if (!status && cstid)
5915 * Test if the stateid is valid
5918 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5919 union nfsd4_op_u *u)
5921 struct nfsd4_test_stateid *test_stateid = &u->test_stateid;
5922 struct nfsd4_test_stateid_id *stateid;
5923 struct nfs4_client *cl = cstate->session->se_client;
5925 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
5926 stateid->ts_id_status =
5927 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
5933 nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
5935 struct nfs4_ol_stateid *stp = openlockstateid(s);
5938 ret = nfsd4_lock_ol_stateid(stp);
5942 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5946 ret = nfserr_locks_held;
5947 if (check_for_locks(stp->st_stid.sc_file,
5948 lockowner(stp->st_stateowner)))
5951 release_lock_stateid(stp);
5955 mutex_unlock(&stp->st_mutex);
5962 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5963 union nfsd4_op_u *u)
5965 struct nfsd4_free_stateid *free_stateid = &u->free_stateid;
5966 stateid_t *stateid = &free_stateid->fr_stateid;
5967 struct nfs4_stid *s;
5968 struct nfs4_delegation *dp;
5969 struct nfs4_client *cl = cstate->session->se_client;
5970 __be32 ret = nfserr_bad_stateid;
5972 spin_lock(&cl->cl_lock);
5973 s = find_stateid_locked(cl, stateid);
5976 spin_lock(&s->sc_lock);
5977 switch (s->sc_type) {
5978 case NFS4_DELEG_STID:
5979 ret = nfserr_locks_held;
5981 case NFS4_OPEN_STID:
5982 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5985 ret = nfserr_locks_held;
5987 case NFS4_LOCK_STID:
5988 spin_unlock(&s->sc_lock);
5989 refcount_inc(&s->sc_count);
5990 spin_unlock(&cl->cl_lock);
5991 ret = nfsd4_free_lock_stateid(stateid, s);
5993 case NFS4_REVOKED_DELEG_STID:
5994 spin_unlock(&s->sc_lock);
5995 dp = delegstateid(s);
5996 list_del_init(&dp->dl_recall_lru);
5997 spin_unlock(&cl->cl_lock);
6001 /* Default falls through and returns nfserr_bad_stateid */
6003 spin_unlock(&s->sc_lock);
6005 spin_unlock(&cl->cl_lock);
6013 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
6014 RD_STATE : WR_STATE;
6017 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
6019 struct svc_fh *current_fh = &cstate->current_fh;
6020 struct nfs4_stateowner *sop = stp->st_stateowner;
6023 status = nfsd4_check_seqid(cstate, sop, seqid);
6026 status = nfsd4_lock_ol_stateid(stp);
6027 if (status != nfs_ok)
6029 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
6030 if (status == nfs_ok)
6031 status = nfs4_check_fh(current_fh, &stp->st_stid);
6032 if (status != nfs_ok)
6033 mutex_unlock(&stp->st_mutex);
6038 * Checks for sequence id mutating operations.
6041 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
6042 stateid_t *stateid, char typemask,
6043 struct nfs4_ol_stateid **stpp,
6044 struct nfsd_net *nn)
6047 struct nfs4_stid *s;
6048 struct nfs4_ol_stateid *stp = NULL;
6050 trace_nfsd_preprocess(seqid, stateid);
6053 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
6056 stp = openlockstateid(s);
6057 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
6059 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
6063 nfs4_put_stid(&stp->st_stid);
6067 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
6068 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
6071 struct nfs4_openowner *oo;
6072 struct nfs4_ol_stateid *stp;
6074 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
6075 NFS4_OPEN_STID, &stp, nn);
6078 oo = openowner(stp->st_stateowner);
6079 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
6080 mutex_unlock(&stp->st_mutex);
6081 nfs4_put_stid(&stp->st_stid);
6082 return nfserr_bad_stateid;
6089 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6090 union nfsd4_op_u *u)
6092 struct nfsd4_open_confirm *oc = &u->open_confirm;
6094 struct nfs4_openowner *oo;
6095 struct nfs4_ol_stateid *stp;
6096 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6098 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
6099 cstate->current_fh.fh_dentry);
6101 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
6105 status = nfs4_preprocess_seqid_op(cstate,
6106 oc->oc_seqid, &oc->oc_req_stateid,
6107 NFS4_OPEN_STID, &stp, nn);
6110 oo = openowner(stp->st_stateowner);
6111 status = nfserr_bad_stateid;
6112 if (oo->oo_flags & NFS4_OO_CONFIRMED) {
6113 mutex_unlock(&stp->st_mutex);
6116 oo->oo_flags |= NFS4_OO_CONFIRMED;
6117 nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
6118 mutex_unlock(&stp->st_mutex);
6119 trace_nfsd_open_confirm(oc->oc_seqid, &stp->st_stid.sc_stateid);
6120 nfsd4_client_record_create(oo->oo_owner.so_client);
6123 nfs4_put_stid(&stp->st_stid);
6125 nfsd4_bump_seqid(cstate, status);
6129 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
6131 if (!test_access(access, stp))
6133 nfs4_file_put_access(stp->st_stid.sc_file, access);
6134 clear_access(access, stp);
6137 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
6139 switch (to_access) {
6140 case NFS4_SHARE_ACCESS_READ:
6141 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
6142 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
6144 case NFS4_SHARE_ACCESS_WRITE:
6145 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
6146 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
6148 case NFS4_SHARE_ACCESS_BOTH:
6156 nfsd4_open_downgrade(struct svc_rqst *rqstp,
6157 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
6159 struct nfsd4_open_downgrade *od = &u->open_downgrade;
6161 struct nfs4_ol_stateid *stp;
6162 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6164 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
6165 cstate->current_fh.fh_dentry);
6167 /* We don't yet support WANT bits: */
6168 if (od->od_deleg_want)
6169 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
6172 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
6173 &od->od_stateid, &stp, nn);
6176 status = nfserr_inval;
6177 if (!test_access(od->od_share_access, stp)) {
6178 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
6179 stp->st_access_bmap, od->od_share_access);
6182 if (!test_deny(od->od_share_deny, stp)) {
6183 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
6184 stp->st_deny_bmap, od->od_share_deny);
6187 nfs4_stateid_downgrade(stp, od->od_share_access);
6188 reset_union_bmap_deny(od->od_share_deny, stp);
6189 nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
6192 mutex_unlock(&stp->st_mutex);
6193 nfs4_put_stid(&stp->st_stid);
6195 nfsd4_bump_seqid(cstate, status);
6199 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
6201 struct nfs4_client *clp = s->st_stid.sc_client;
6203 LIST_HEAD(reaplist);
6205 spin_lock(&clp->cl_lock);
6206 unhashed = unhash_open_stateid(s, &reaplist);
6208 if (clp->cl_minorversion) {
6210 put_ol_stateid_locked(s, &reaplist);
6211 spin_unlock(&clp->cl_lock);
6212 free_ol_stateid_reaplist(&reaplist);
6214 spin_unlock(&clp->cl_lock);
6215 free_ol_stateid_reaplist(&reaplist);
6217 move_to_close_lru(s, clp->net);
6222 * nfs4_unlock_state() called after encode
6225 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6226 union nfsd4_op_u *u)
6228 struct nfsd4_close *close = &u->close;
6230 struct nfs4_ol_stateid *stp;
6231 struct net *net = SVC_NET(rqstp);
6232 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6234 dprintk("NFSD: nfsd4_close on file %pd\n",
6235 cstate->current_fh.fh_dentry);
6237 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
6239 NFS4_OPEN_STID|NFS4_CLOSED_STID,
6241 nfsd4_bump_seqid(cstate, status);
6245 stp->st_stid.sc_type = NFS4_CLOSED_STID;
6248 * Technically we don't _really_ have to increment or copy it, since
6249 * it should just be gone after this operation and we clobber the
6250 * copied value below, but we continue to do so here just to ensure
6251 * that racing ops see that there was a state change.
6253 nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
6255 nfsd4_close_open_stateid(stp);
6256 mutex_unlock(&stp->st_mutex);
6258 /* v4.1+ suggests that we send a special stateid in here, since the
6259 * clients should just ignore this anyway. Since this is not useful
6260 * for v4.0 clients either, we set it to the special close_stateid
6263 * See RFC5661 section 18.2.4, and RFC7530 section 16.2.5
6265 memcpy(&close->cl_stateid, &close_stateid, sizeof(close->cl_stateid));
6267 /* put reference from nfs4_preprocess_seqid_op */
6268 nfs4_put_stid(&stp->st_stid);
6274 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6275 union nfsd4_op_u *u)
6277 struct nfsd4_delegreturn *dr = &u->delegreturn;
6278 struct nfs4_delegation *dp;
6279 stateid_t *stateid = &dr->dr_stateid;
6280 struct nfs4_stid *s;
6282 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6284 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
6287 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
6290 dp = delegstateid(s);
6291 status = nfsd4_stid_check_stateid_generation(stateid, &dp->dl_stid, nfsd4_has_session(cstate));
6295 destroy_delegation(dp);
6297 nfs4_put_stid(&dp->dl_stid);
6303 end_offset(u64 start, u64 len)
6308 return end >= start ? end: NFS4_MAX_UINT64;
6311 /* last octet in a range */
6313 last_byte_offset(u64 start, u64 len)
6319 return end > start ? end - 1: NFS4_MAX_UINT64;
6323 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
6324 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
6325 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
6326 * locking, this prevents us from being completely protocol-compliant. The
6327 * real solution to this problem is to start using unsigned file offsets in
6328 * the VFS, but this is a very deep change!
6331 nfs4_transform_lock_offset(struct file_lock *lock)
6333 if (lock->fl_start < 0)
6334 lock->fl_start = OFFSET_MAX;
6335 if (lock->fl_end < 0)
6336 lock->fl_end = OFFSET_MAX;
6340 nfsd4_fl_get_owner(fl_owner_t owner)
6342 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
6344 nfs4_get_stateowner(&lo->lo_owner);
6349 nfsd4_fl_put_owner(fl_owner_t owner)
6351 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
6354 nfs4_put_stateowner(&lo->lo_owner);
6358 nfsd4_lm_notify(struct file_lock *fl)
6360 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
6361 struct net *net = lo->lo_owner.so_client->net;
6362 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6363 struct nfsd4_blocked_lock *nbl = container_of(fl,
6364 struct nfsd4_blocked_lock, nbl_lock);
6367 /* An empty list means that something else is going to be using it */
6368 spin_lock(&nn->blocked_locks_lock);
6369 if (!list_empty(&nbl->nbl_list)) {
6370 list_del_init(&nbl->nbl_list);
6371 list_del_init(&nbl->nbl_lru);
6374 spin_unlock(&nn->blocked_locks_lock);
6377 nfsd4_run_cb(&nbl->nbl_cb);
6380 static const struct lock_manager_operations nfsd_posix_mng_ops = {
6381 .lm_notify = nfsd4_lm_notify,
6382 .lm_get_owner = nfsd4_fl_get_owner,
6383 .lm_put_owner = nfsd4_fl_put_owner,
6387 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
6389 struct nfs4_lockowner *lo;
6391 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
6392 lo = (struct nfs4_lockowner *) fl->fl_owner;
6393 xdr_netobj_dup(&deny->ld_owner, &lo->lo_owner.so_owner,
6395 if (!deny->ld_owner.data)
6396 /* We just don't care that much */
6398 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
6401 deny->ld_owner.len = 0;
6402 deny->ld_owner.data = NULL;
6403 deny->ld_clientid.cl_boot = 0;
6404 deny->ld_clientid.cl_id = 0;
6406 deny->ld_start = fl->fl_start;
6407 deny->ld_length = NFS4_MAX_UINT64;
6408 if (fl->fl_end != NFS4_MAX_UINT64)
6409 deny->ld_length = fl->fl_end - fl->fl_start + 1;
6410 deny->ld_type = NFS4_READ_LT;
6411 if (fl->fl_type != F_RDLCK)
6412 deny->ld_type = NFS4_WRITE_LT;
6415 static struct nfs4_lockowner *
6416 find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
6418 unsigned int strhashval = ownerstr_hashval(owner);
6419 struct nfs4_stateowner *so;
6421 lockdep_assert_held(&clp->cl_lock);
6423 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
6425 if (so->so_is_open_owner)
6427 if (same_owner_str(so, owner))
6428 return lockowner(nfs4_get_stateowner(so));
6433 static struct nfs4_lockowner *
6434 find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
6436 struct nfs4_lockowner *lo;
6438 spin_lock(&clp->cl_lock);
6439 lo = find_lockowner_str_locked(clp, owner);
6440 spin_unlock(&clp->cl_lock);
6444 static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
6446 unhash_lockowner_locked(lockowner(sop));
6449 static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
6451 struct nfs4_lockowner *lo = lockowner(sop);
6453 kmem_cache_free(lockowner_slab, lo);
6456 static const struct nfs4_stateowner_operations lockowner_ops = {
6457 .so_unhash = nfs4_unhash_lockowner,
6458 .so_free = nfs4_free_lockowner,
6462 * Alloc a lock owner structure.
6463 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
6466 * strhashval = ownerstr_hashval
6468 static struct nfs4_lockowner *
6469 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
6470 struct nfs4_ol_stateid *open_stp,
6471 struct nfsd4_lock *lock)
6473 struct nfs4_lockowner *lo, *ret;
6475 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
6478 INIT_LIST_HEAD(&lo->lo_blocked);
6479 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
6480 lo->lo_owner.so_is_open_owner = 0;
6481 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
6482 lo->lo_owner.so_ops = &lockowner_ops;
6483 spin_lock(&clp->cl_lock);
6484 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
6486 list_add(&lo->lo_owner.so_strhash,
6487 &clp->cl_ownerstr_hashtbl[strhashval]);
6490 nfs4_free_stateowner(&lo->lo_owner);
6492 spin_unlock(&clp->cl_lock);
6496 static struct nfs4_ol_stateid *
6497 find_lock_stateid(const struct nfs4_lockowner *lo,
6498 const struct nfs4_ol_stateid *ost)
6500 struct nfs4_ol_stateid *lst;
6502 lockdep_assert_held(&ost->st_stid.sc_client->cl_lock);
6504 /* If ost is not hashed, ost->st_locks will not be valid */
6505 if (!nfs4_ol_stateid_unhashed(ost))
6506 list_for_each_entry(lst, &ost->st_locks, st_locks) {
6507 if (lst->st_stateowner == &lo->lo_owner) {
6508 refcount_inc(&lst->st_stid.sc_count);
6515 static struct nfs4_ol_stateid *
6516 init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
6517 struct nfs4_file *fp, struct inode *inode,
6518 struct nfs4_ol_stateid *open_stp)
6520 struct nfs4_client *clp = lo->lo_owner.so_client;
6521 struct nfs4_ol_stateid *retstp;
6523 mutex_init(&stp->st_mutex);
6524 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
6526 spin_lock(&clp->cl_lock);
6527 if (nfs4_ol_stateid_unhashed(open_stp))
6529 retstp = find_lock_stateid(lo, open_stp);
6532 refcount_inc(&stp->st_stid.sc_count);
6533 stp->st_stid.sc_type = NFS4_LOCK_STID;
6534 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
6536 stp->st_stid.sc_file = fp;
6537 stp->st_access_bmap = 0;
6538 stp->st_deny_bmap = open_stp->st_deny_bmap;
6539 stp->st_openstp = open_stp;
6540 spin_lock(&fp->fi_lock);
6541 list_add(&stp->st_locks, &open_stp->st_locks);
6542 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
6543 list_add(&stp->st_perfile, &fp->fi_stateids);
6544 spin_unlock(&fp->fi_lock);
6545 spin_unlock(&clp->cl_lock);
6548 spin_unlock(&clp->cl_lock);
6549 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
6550 nfs4_put_stid(&retstp->st_stid);
6553 /* To keep mutex tracking happy */
6554 mutex_unlock(&stp->st_mutex);
6557 spin_unlock(&clp->cl_lock);
6558 mutex_unlock(&stp->st_mutex);
6562 static struct nfs4_ol_stateid *
6563 find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
6564 struct inode *inode, struct nfs4_ol_stateid *ost,
6567 struct nfs4_stid *ns = NULL;
6568 struct nfs4_ol_stateid *lst;
6569 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
6570 struct nfs4_client *clp = oo->oo_owner.so_client;
6573 spin_lock(&clp->cl_lock);
6574 lst = find_lock_stateid(lo, ost);
6575 spin_unlock(&clp->cl_lock);
6577 if (nfsd4_lock_ol_stateid(lst) == nfs_ok)
6579 nfs4_put_stid(&lst->st_stid);
6581 ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid);
6585 lst = init_lock_stateid(openlockstateid(ns), lo, fi, inode, ost);
6586 if (lst == openlockstateid(ns))
6595 check_lock_length(u64 offset, u64 length)
6597 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
6598 (length > ~offset)));
6601 static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
6603 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
6605 lockdep_assert_held(&fp->fi_lock);
6607 if (test_access(access, lock_stp))
6609 __nfs4_file_get_access(fp, access);
6610 set_access(access, lock_stp);
6614 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
6615 struct nfs4_ol_stateid *ost,
6616 struct nfsd4_lock *lock,
6617 struct nfs4_ol_stateid **plst, bool *new)
6620 struct nfs4_file *fi = ost->st_stid.sc_file;
6621 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
6622 struct nfs4_client *cl = oo->oo_owner.so_client;
6623 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
6624 struct nfs4_lockowner *lo;
6625 struct nfs4_ol_stateid *lst;
6626 unsigned int strhashval;
6628 lo = find_lockowner_str(cl, &lock->lk_new_owner);
6630 strhashval = ownerstr_hashval(&lock->lk_new_owner);
6631 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
6633 return nfserr_jukebox;
6635 /* with an existing lockowner, seqids must be the same */
6636 status = nfserr_bad_seqid;
6637 if (!cstate->minorversion &&
6638 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
6642 lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
6644 status = nfserr_jukebox;
6651 nfs4_put_stateowner(&lo->lo_owner);
6659 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6660 union nfsd4_op_u *u)
6662 struct nfsd4_lock *lock = &u->lock;
6663 struct nfs4_openowner *open_sop = NULL;
6664 struct nfs4_lockowner *lock_sop = NULL;
6665 struct nfs4_ol_stateid *lock_stp = NULL;
6666 struct nfs4_ol_stateid *open_stp = NULL;
6667 struct nfs4_file *fp;
6668 struct nfsd_file *nf = NULL;
6669 struct nfsd4_blocked_lock *nbl = NULL;
6670 struct file_lock *file_lock = NULL;
6671 struct file_lock *conflock = NULL;
6676 unsigned char fl_type;
6677 unsigned int fl_flags = FL_POSIX;
6678 struct net *net = SVC_NET(rqstp);
6679 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6681 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
6682 (long long) lock->lk_offset,
6683 (long long) lock->lk_length);
6685 if (check_lock_length(lock->lk_offset, lock->lk_length))
6686 return nfserr_inval;
6688 if ((status = fh_verify(rqstp, &cstate->current_fh,
6689 S_IFREG, NFSD_MAY_LOCK))) {
6690 dprintk("NFSD: nfsd4_lock: permission denied!\n");
6694 if (lock->lk_is_new) {
6695 if (nfsd4_has_session(cstate))
6696 /* See rfc 5661 18.10.3: given clientid is ignored: */
6697 memcpy(&lock->lk_new_clientid,
6698 &cstate->session->se_client->cl_clientid,
6699 sizeof(clientid_t));
6701 status = nfserr_stale_clientid;
6702 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
6705 /* validate and update open stateid and open seqid */
6706 status = nfs4_preprocess_confirmed_seqid_op(cstate,
6707 lock->lk_new_open_seqid,
6708 &lock->lk_new_open_stateid,
6712 mutex_unlock(&open_stp->st_mutex);
6713 open_sop = openowner(open_stp->st_stateowner);
6714 status = nfserr_bad_stateid;
6715 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
6716 &lock->lk_new_clientid))
6718 status = lookup_or_create_lock_state(cstate, open_stp, lock,
6721 status = nfs4_preprocess_seqid_op(cstate,
6722 lock->lk_old_lock_seqid,
6723 &lock->lk_old_lock_stateid,
6724 NFS4_LOCK_STID, &lock_stp, nn);
6728 lock_sop = lockowner(lock_stp->st_stateowner);
6730 lkflg = setlkflg(lock->lk_type);
6731 status = nfs4_check_openmode(lock_stp, lkflg);
6735 status = nfserr_grace;
6736 if (locks_in_grace(net) && !lock->lk_reclaim)
6738 status = nfserr_no_grace;
6739 if (!locks_in_grace(net) && lock->lk_reclaim)
6742 fp = lock_stp->st_stid.sc_file;
6743 switch (lock->lk_type) {
6745 if (nfsd4_has_session(cstate))
6746 fl_flags |= FL_SLEEP;
6749 spin_lock(&fp->fi_lock);
6750 nf = find_readable_file_locked(fp);
6752 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
6753 spin_unlock(&fp->fi_lock);
6756 case NFS4_WRITEW_LT:
6757 if (nfsd4_has_session(cstate))
6758 fl_flags |= FL_SLEEP;
6761 spin_lock(&fp->fi_lock);
6762 nf = find_writeable_file_locked(fp);
6764 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
6765 spin_unlock(&fp->fi_lock);
6769 status = nfserr_inval;
6774 status = nfserr_openmode;
6778 nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
6780 dprintk("NFSD: %s: unable to allocate block!\n", __func__);
6781 status = nfserr_jukebox;
6785 file_lock = &nbl->nbl_lock;
6786 file_lock->fl_type = fl_type;
6787 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
6788 file_lock->fl_pid = current->tgid;
6789 file_lock->fl_file = nf->nf_file;
6790 file_lock->fl_flags = fl_flags;
6791 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6792 file_lock->fl_start = lock->lk_offset;
6793 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
6794 nfs4_transform_lock_offset(file_lock);
6796 conflock = locks_alloc_lock();
6798 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6799 status = nfserr_jukebox;
6803 if (fl_flags & FL_SLEEP) {
6804 nbl->nbl_time = ktime_get_boottime_seconds();
6805 spin_lock(&nn->blocked_locks_lock);
6806 list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
6807 list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
6808 spin_unlock(&nn->blocked_locks_lock);
6811 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, conflock);
6813 case 0: /* success! */
6814 nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
6816 if (lock->lk_reclaim)
6817 nn->somebody_reclaimed = true;
6819 case FILE_LOCK_DEFERRED:
6822 case -EAGAIN: /* conflock holds conflicting lock */
6823 status = nfserr_denied;
6824 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
6825 nfs4_set_lock_denied(conflock, &lock->lk_denied);
6828 status = nfserr_deadlock;
6831 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
6832 status = nfserrno(err);
6837 /* dequeue it if we queued it before */
6838 if (fl_flags & FL_SLEEP) {
6839 spin_lock(&nn->blocked_locks_lock);
6840 list_del_init(&nbl->nbl_list);
6841 list_del_init(&nbl->nbl_lru);
6842 spin_unlock(&nn->blocked_locks_lock);
6844 free_blocked_lock(nbl);
6849 /* Bump seqid manually if the 4.0 replay owner is openowner */
6850 if (cstate->replay_owner &&
6851 cstate->replay_owner != &lock_sop->lo_owner &&
6852 seqid_mutating_err(ntohl(status)))
6853 lock_sop->lo_owner.so_seqid++;
6856 * If this is a new, never-before-used stateid, and we are
6857 * returning an error, then just go ahead and release it.
6860 release_lock_stateid(lock_stp);
6862 mutex_unlock(&lock_stp->st_mutex);
6864 nfs4_put_stid(&lock_stp->st_stid);
6867 nfs4_put_stid(&open_stp->st_stid);
6868 nfsd4_bump_seqid(cstate, status);
6870 locks_free_lock(conflock);
6875 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
6876 * so we do a temporary open here just to get an open file to pass to
6877 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
6880 static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
6882 struct nfsd_file *nf;
6883 __be32 err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf);
6885 err = nfserrno(vfs_test_lock(nf->nf_file, lock));
6895 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6896 union nfsd4_op_u *u)
6898 struct nfsd4_lockt *lockt = &u->lockt;
6899 struct file_lock *file_lock = NULL;
6900 struct nfs4_lockowner *lo = NULL;
6902 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6904 if (locks_in_grace(SVC_NET(rqstp)))
6905 return nfserr_grace;
6907 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
6908 return nfserr_inval;
6910 if (!nfsd4_has_session(cstate)) {
6911 status = lookup_clientid(&lockt->lt_clientid, cstate, nn,
6917 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
6920 file_lock = locks_alloc_lock();
6922 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6923 status = nfserr_jukebox;
6927 switch (lockt->lt_type) {
6930 file_lock->fl_type = F_RDLCK;
6933 case NFS4_WRITEW_LT:
6934 file_lock->fl_type = F_WRLCK;
6937 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
6938 status = nfserr_inval;
6942 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
6944 file_lock->fl_owner = (fl_owner_t)lo;
6945 file_lock->fl_pid = current->tgid;
6946 file_lock->fl_flags = FL_POSIX;
6948 file_lock->fl_start = lockt->lt_offset;
6949 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
6951 nfs4_transform_lock_offset(file_lock);
6953 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
6957 if (file_lock->fl_type != F_UNLCK) {
6958 status = nfserr_denied;
6959 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
6963 nfs4_put_stateowner(&lo->lo_owner);
6965 locks_free_lock(file_lock);
6970 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6971 union nfsd4_op_u *u)
6973 struct nfsd4_locku *locku = &u->locku;
6974 struct nfs4_ol_stateid *stp;
6975 struct nfsd_file *nf = NULL;
6976 struct file_lock *file_lock = NULL;
6979 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6981 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
6982 (long long) locku->lu_offset,
6983 (long long) locku->lu_length);
6985 if (check_lock_length(locku->lu_offset, locku->lu_length))
6986 return nfserr_inval;
6988 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
6989 &locku->lu_stateid, NFS4_LOCK_STID,
6993 nf = find_any_file(stp->st_stid.sc_file);
6995 status = nfserr_lock_range;
6998 file_lock = locks_alloc_lock();
7000 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
7001 status = nfserr_jukebox;
7005 file_lock->fl_type = F_UNLCK;
7006 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
7007 file_lock->fl_pid = current->tgid;
7008 file_lock->fl_file = nf->nf_file;
7009 file_lock->fl_flags = FL_POSIX;
7010 file_lock->fl_lmops = &nfsd_posix_mng_ops;
7011 file_lock->fl_start = locku->lu_offset;
7013 file_lock->fl_end = last_byte_offset(locku->lu_offset,
7015 nfs4_transform_lock_offset(file_lock);
7017 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, NULL);
7019 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
7022 nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
7026 mutex_unlock(&stp->st_mutex);
7027 nfs4_put_stid(&stp->st_stid);
7029 nfsd4_bump_seqid(cstate, status);
7031 locks_free_lock(file_lock);
7035 status = nfserrno(err);
7041 * true: locks held by lockowner
7042 * false: no locks held by lockowner
7045 check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
7047 struct file_lock *fl;
7049 struct nfsd_file *nf = find_any_file(fp);
7050 struct inode *inode;
7051 struct file_lock_context *flctx;
7054 /* Any valid lock stateid should have some sort of access */
7059 inode = locks_inode(nf->nf_file);
7060 flctx = inode->i_flctx;
7062 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
7063 spin_lock(&flctx->flc_lock);
7064 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
7065 if (fl->fl_owner == (fl_owner_t)lowner) {
7070 spin_unlock(&flctx->flc_lock);
7077 nfsd4_release_lockowner(struct svc_rqst *rqstp,
7078 struct nfsd4_compound_state *cstate,
7079 union nfsd4_op_u *u)
7081 struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner;
7082 clientid_t *clid = &rlockowner->rl_clientid;
7083 struct nfs4_stateowner *sop;
7084 struct nfs4_lockowner *lo = NULL;
7085 struct nfs4_ol_stateid *stp;
7086 struct xdr_netobj *owner = &rlockowner->rl_owner;
7087 unsigned int hashval = ownerstr_hashval(owner);
7089 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
7090 struct nfs4_client *clp;
7091 LIST_HEAD (reaplist);
7093 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
7094 clid->cl_boot, clid->cl_id);
7096 status = lookup_clientid(clid, cstate, nn, false);
7101 /* Find the matching lock stateowner */
7102 spin_lock(&clp->cl_lock);
7103 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
7106 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
7109 /* see if there are still any locks associated with it */
7110 lo = lockowner(sop);
7111 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
7112 if (check_for_locks(stp->st_stid.sc_file, lo)) {
7113 status = nfserr_locks_held;
7114 spin_unlock(&clp->cl_lock);
7119 nfs4_get_stateowner(sop);
7123 spin_unlock(&clp->cl_lock);
7127 unhash_lockowner_locked(lo);
7128 while (!list_empty(&lo->lo_owner.so_stateids)) {
7129 stp = list_first_entry(&lo->lo_owner.so_stateids,
7130 struct nfs4_ol_stateid,
7132 WARN_ON(!unhash_lock_stateid(stp));
7133 put_ol_stateid_locked(stp, &reaplist);
7135 spin_unlock(&clp->cl_lock);
7136 free_ol_stateid_reaplist(&reaplist);
7137 remove_blocked_locks(lo);
7138 nfs4_put_stateowner(&lo->lo_owner);
7143 static inline struct nfs4_client_reclaim *
7146 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
7150 nfs4_has_reclaimed_state(struct xdr_netobj name, struct nfsd_net *nn)
7152 struct nfs4_client_reclaim *crp;
7154 crp = nfsd4_find_reclaim_client(name, nn);
7155 return (crp && crp->cr_clp);
7159 * failure => all reset bets are off, nfserr_no_grace...
7161 * The caller is responsible for freeing name.data if NULL is returned (it
7162 * will be freed in nfs4_remove_reclaim_record in the normal case).
7164 struct nfs4_client_reclaim *
7165 nfs4_client_to_reclaim(struct xdr_netobj name, struct xdr_netobj princhash,
7166 struct nfsd_net *nn)
7168 unsigned int strhashval;
7169 struct nfs4_client_reclaim *crp;
7171 trace_nfsd_clid_reclaim(nn, name.len, name.data);
7172 crp = alloc_reclaim();
7174 strhashval = clientstr_hashval(name);
7175 INIT_LIST_HEAD(&crp->cr_strhash);
7176 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
7177 crp->cr_name.data = name.data;
7178 crp->cr_name.len = name.len;
7179 crp->cr_princhash.data = princhash.data;
7180 crp->cr_princhash.len = princhash.len;
7182 nn->reclaim_str_hashtbl_size++;
7188 nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
7190 list_del(&crp->cr_strhash);
7191 kfree(crp->cr_name.data);
7192 kfree(crp->cr_princhash.data);
7194 nn->reclaim_str_hashtbl_size--;
7198 nfs4_release_reclaim(struct nfsd_net *nn)
7200 struct nfs4_client_reclaim *crp = NULL;
7203 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7204 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
7205 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
7206 struct nfs4_client_reclaim, cr_strhash);
7207 nfs4_remove_reclaim_record(crp, nn);
7210 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
7214 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
7215 struct nfs4_client_reclaim *
7216 nfsd4_find_reclaim_client(struct xdr_netobj name, struct nfsd_net *nn)
7218 unsigned int strhashval;
7219 struct nfs4_client_reclaim *crp = NULL;
7221 trace_nfsd_clid_find(nn, name.len, name.data);
7223 strhashval = clientstr_hashval(name);
7224 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
7225 if (compare_blob(&crp->cr_name, &name) == 0) {
7233 * Called from OPEN. Look for clientid in reclaim list.
7236 nfs4_check_open_reclaim(clientid_t *clid,
7237 struct nfsd4_compound_state *cstate,
7238 struct nfsd_net *nn)
7242 /* find clientid in conf_id_hashtbl */
7243 status = lookup_clientid(clid, cstate, nn, false);
7245 return nfserr_reclaim_bad;
7247 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
7248 return nfserr_no_grace;
7250 if (nfsd4_client_record_check(cstate->clp))
7251 return nfserr_reclaim_bad;
7257 * Since the lifetime of a delegation isn't limited to that of an open, a
7258 * client may quite reasonably hang on to a delegation as long as it has
7259 * the inode cached. This becomes an obvious problem the first time a
7260 * client's inode cache approaches the size of the server's total memory.
7262 * For now we avoid this problem by imposing a hard limit on the number
7263 * of delegations, which varies according to the server's memory size.
7266 set_max_delegations(void)
7269 * Allow at most 4 delegations per megabyte of RAM. Quick
7270 * estimates suggest that in the worst case (where every delegation
7271 * is for a different inode), a delegation could take about 1.5K,
7272 * giving a worst case usage of about 6% of memory.
7274 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
7277 static int nfs4_state_create_net(struct net *net)
7279 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7282 nn->conf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7283 sizeof(struct list_head),
7285 if (!nn->conf_id_hashtbl)
7287 nn->unconf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7288 sizeof(struct list_head),
7290 if (!nn->unconf_id_hashtbl)
7292 nn->sessionid_hashtbl = kmalloc_array(SESSION_HASH_SIZE,
7293 sizeof(struct list_head),
7295 if (!nn->sessionid_hashtbl)
7298 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7299 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
7300 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
7302 for (i = 0; i < SESSION_HASH_SIZE; i++)
7303 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
7304 nn->conf_name_tree = RB_ROOT;
7305 nn->unconf_name_tree = RB_ROOT;
7306 nn->boot_time = ktime_get_real_seconds();
7307 nn->grace_ended = false;
7308 nn->nfsd4_manager.block_opens = true;
7309 INIT_LIST_HEAD(&nn->nfsd4_manager.list);
7310 INIT_LIST_HEAD(&nn->client_lru);
7311 INIT_LIST_HEAD(&nn->close_lru);
7312 INIT_LIST_HEAD(&nn->del_recall_lru);
7313 spin_lock_init(&nn->client_lock);
7314 spin_lock_init(&nn->s2s_cp_lock);
7315 idr_init(&nn->s2s_cp_stateids);
7317 spin_lock_init(&nn->blocked_locks_lock);
7318 INIT_LIST_HEAD(&nn->blocked_locks_lru);
7320 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
7326 kfree(nn->unconf_id_hashtbl);
7328 kfree(nn->conf_id_hashtbl);
7334 nfs4_state_destroy_net(struct net *net)
7337 struct nfs4_client *clp = NULL;
7338 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7340 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7341 while (!list_empty(&nn->conf_id_hashtbl[i])) {
7342 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7343 destroy_client(clp);
7347 WARN_ON(!list_empty(&nn->blocked_locks_lru));
7349 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7350 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
7351 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7352 destroy_client(clp);
7356 kfree(nn->sessionid_hashtbl);
7357 kfree(nn->unconf_id_hashtbl);
7358 kfree(nn->conf_id_hashtbl);
7363 nfs4_state_start_net(struct net *net)
7365 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7368 ret = get_nfsdfs(net);
7371 ret = nfs4_state_create_net(net);
7373 mntput(nn->nfsd_mnt);
7376 locks_start_grace(net, &nn->nfsd4_manager);
7377 nfsd4_client_tracking_init(net);
7378 if (nn->track_reclaim_completes && nn->reclaim_str_hashtbl_size == 0)
7380 printk(KERN_INFO "NFSD: starting %lld-second grace period (net %x)\n",
7381 nn->nfsd4_grace, net->ns.inum);
7382 trace_nfsd_grace_start(nn);
7383 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
7387 printk(KERN_INFO "NFSD: no clients to reclaim, skipping NFSv4 grace period (net %x)\n",
7389 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_lease * HZ);
7390 nfsd4_end_grace(nn);
7394 /* initialization to perform when the nfsd service is started: */
7397 nfs4_state_start(void)
7401 laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
7402 if (laundry_wq == NULL) {
7406 ret = nfsd4_create_callback_queue();
7408 goto out_free_laundry;
7410 set_max_delegations();
7414 destroy_workqueue(laundry_wq);
7420 nfs4_state_shutdown_net(struct net *net)
7422 struct nfs4_delegation *dp = NULL;
7423 struct list_head *pos, *next, reaplist;
7424 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7426 cancel_delayed_work_sync(&nn->laundromat_work);
7427 locks_end_grace(&nn->nfsd4_manager);
7429 INIT_LIST_HEAD(&reaplist);
7430 spin_lock(&state_lock);
7431 list_for_each_safe(pos, next, &nn->del_recall_lru) {
7432 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
7433 WARN_ON(!unhash_delegation_locked(dp));
7434 list_add(&dp->dl_recall_lru, &reaplist);
7436 spin_unlock(&state_lock);
7437 list_for_each_safe(pos, next, &reaplist) {
7438 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
7439 list_del_init(&dp->dl_recall_lru);
7440 destroy_unhashed_deleg(dp);
7443 nfsd4_client_tracking_exit(net);
7444 nfs4_state_destroy_net(net);
7445 mntput(nn->nfsd_mnt);
7449 nfs4_state_shutdown(void)
7451 destroy_workqueue(laundry_wq);
7452 nfsd4_destroy_callback_queue();
7456 get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7458 if (HAS_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG) &&
7459 CURRENT_STATEID(stateid))
7460 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
7464 put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7466 if (cstate->minorversion) {
7467 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
7468 SET_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
7473 clear_current_stateid(struct nfsd4_compound_state *cstate)
7475 CLEAR_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
7479 * functions to set current state id
7482 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate,
7483 union nfsd4_op_u *u)
7485 put_stateid(cstate, &u->open_downgrade.od_stateid);
7489 nfsd4_set_openstateid(struct nfsd4_compound_state *cstate,
7490 union nfsd4_op_u *u)
7492 put_stateid(cstate, &u->open.op_stateid);
7496 nfsd4_set_closestateid(struct nfsd4_compound_state *cstate,
7497 union nfsd4_op_u *u)
7499 put_stateid(cstate, &u->close.cl_stateid);
7503 nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate,
7504 union nfsd4_op_u *u)
7506 put_stateid(cstate, &u->lock.lk_resp_stateid);
7510 * functions to consume current state id
7514 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate,
7515 union nfsd4_op_u *u)
7517 get_stateid(cstate, &u->open_downgrade.od_stateid);
7521 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate,
7522 union nfsd4_op_u *u)
7524 get_stateid(cstate, &u->delegreturn.dr_stateid);
7528 nfsd4_get_freestateid(struct nfsd4_compound_state *cstate,
7529 union nfsd4_op_u *u)
7531 get_stateid(cstate, &u->free_stateid.fr_stateid);
7535 nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate,
7536 union nfsd4_op_u *u)
7538 get_stateid(cstate, &u->setattr.sa_stateid);
7542 nfsd4_get_closestateid(struct nfsd4_compound_state *cstate,
7543 union nfsd4_op_u *u)
7545 get_stateid(cstate, &u->close.cl_stateid);
7549 nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate,
7550 union nfsd4_op_u *u)
7552 get_stateid(cstate, &u->locku.lu_stateid);
7556 nfsd4_get_readstateid(struct nfsd4_compound_state *cstate,
7557 union nfsd4_op_u *u)
7559 get_stateid(cstate, &u->read.rd_stateid);
7563 nfsd4_get_writestateid(struct nfsd4_compound_state *cstate,
7564 union nfsd4_op_u *u)
7566 get_stateid(cstate, &u->write.wr_stateid);