2 * Copyright (c) 2001 The Regents of the University of Michigan.
5 * Kendrick Smith <kmsmith@umich.edu>
6 * Andy Adamson <kandros@umich.edu>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <linux/file.h>
37 #include <linux/slab.h>
38 #include <linux/namei.h>
39 #include <linux/swap.h>
40 #include <linux/pagemap.h>
41 #include <linux/ratelimit.h>
42 #include <linux/sunrpc/svcauth_gss.h>
43 #include <linux/sunrpc/addr.h>
44 #include <linux/jhash.h>
45 #include <linux/string_helpers.h>
46 #include <linux/fsnotify.h>
47 #include <linux/nfs_ssc.h>
51 #include "current_stateid.h"
55 #include "filecache.h"
58 #define NFSDDBG_FACILITY NFSDDBG_PROC
60 #define all_ones {{~0,~0},~0}
61 static const stateid_t one_stateid = {
63 .si_opaque = all_ones,
65 static const stateid_t zero_stateid = {
68 static const stateid_t currentstateid = {
71 static const stateid_t close_stateid = {
72 .si_generation = 0xffffffffU,
75 static u64 current_sessionid = 1;
77 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
78 #define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
79 #define CURRENT_STATEID(stateid) (!memcmp((stateid), ¤tstateid, sizeof(stateid_t)))
80 #define CLOSE_STATEID(stateid) (!memcmp((stateid), &close_stateid, sizeof(stateid_t)))
82 /* forward declarations */
83 static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
84 static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
85 void nfsd4_end_grace(struct nfsd_net *nn);
86 static void _free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps);
91 * Currently used for the del_recall_lru and file hash table. In an
92 * effort to decrease the scope of the client_mutex, this spinlock may
93 * eventually cover more:
95 static DEFINE_SPINLOCK(state_lock);
97 enum nfsd4_st_mutex_lock_subclass {
98 OPEN_STATEID_MUTEX = 0,
99 LOCK_STATEID_MUTEX = 1,
103 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
104 * the refcount on the open stateid to drop.
106 static DECLARE_WAIT_QUEUE_HEAD(close_wq);
109 * A waitqueue where a writer to clients/#/ctl destroying a client can
110 * wait for cl_rpc_users to drop to 0 and then for the client to be
113 static DECLARE_WAIT_QUEUE_HEAD(expiry_wq);
115 static struct kmem_cache *client_slab;
116 static struct kmem_cache *openowner_slab;
117 static struct kmem_cache *lockowner_slab;
118 static struct kmem_cache *file_slab;
119 static struct kmem_cache *stateid_slab;
120 static struct kmem_cache *deleg_slab;
121 static struct kmem_cache *odstate_slab;
123 static void free_session(struct nfsd4_session *);
125 static const struct nfsd4_callback_ops nfsd4_cb_recall_ops;
126 static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops;
128 static bool is_session_dead(struct nfsd4_session *ses)
130 return ses->se_flags & NFS4_SESSION_DEAD;
133 static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
135 if (atomic_read(&ses->se_ref) > ref_held_by_me)
136 return nfserr_jukebox;
137 ses->se_flags |= NFS4_SESSION_DEAD;
141 static bool is_client_expired(struct nfs4_client *clp)
143 return clp->cl_time == 0;
146 static __be32 get_client_locked(struct nfs4_client *clp)
148 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
150 lockdep_assert_held(&nn->client_lock);
152 if (is_client_expired(clp))
153 return nfserr_expired;
154 atomic_inc(&clp->cl_rpc_users);
158 /* must be called under the client_lock */
160 renew_client_locked(struct nfs4_client *clp)
162 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
164 if (is_client_expired(clp)) {
166 printk("%s: client (clientid %08x/%08x) already expired\n",
168 clp->cl_clientid.cl_boot,
169 clp->cl_clientid.cl_id);
173 list_move_tail(&clp->cl_lru, &nn->client_lru);
174 clp->cl_time = ktime_get_boottime_seconds();
177 static void put_client_renew_locked(struct nfs4_client *clp)
179 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
181 lockdep_assert_held(&nn->client_lock);
183 if (!atomic_dec_and_test(&clp->cl_rpc_users))
185 if (!is_client_expired(clp))
186 renew_client_locked(clp);
188 wake_up_all(&expiry_wq);
191 static void put_client_renew(struct nfs4_client *clp)
193 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
195 if (!atomic_dec_and_lock(&clp->cl_rpc_users, &nn->client_lock))
197 if (!is_client_expired(clp))
198 renew_client_locked(clp);
200 wake_up_all(&expiry_wq);
201 spin_unlock(&nn->client_lock);
204 static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
208 if (is_session_dead(ses))
209 return nfserr_badsession;
210 status = get_client_locked(ses->se_client);
213 atomic_inc(&ses->se_ref);
217 static void nfsd4_put_session_locked(struct nfsd4_session *ses)
219 struct nfs4_client *clp = ses->se_client;
220 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
222 lockdep_assert_held(&nn->client_lock);
224 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
226 put_client_renew_locked(clp);
229 static void nfsd4_put_session(struct nfsd4_session *ses)
231 struct nfs4_client *clp = ses->se_client;
232 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
234 spin_lock(&nn->client_lock);
235 nfsd4_put_session_locked(ses);
236 spin_unlock(&nn->client_lock);
239 static struct nfsd4_blocked_lock *
240 find_blocked_lock(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
243 struct nfsd4_blocked_lock *cur, *found = NULL;
245 spin_lock(&nn->blocked_locks_lock);
246 list_for_each_entry(cur, &lo->lo_blocked, nbl_list) {
247 if (fh_match(fh, &cur->nbl_fh)) {
248 list_del_init(&cur->nbl_list);
249 list_del_init(&cur->nbl_lru);
254 spin_unlock(&nn->blocked_locks_lock);
256 locks_delete_block(&found->nbl_lock);
260 static struct nfsd4_blocked_lock *
261 find_or_allocate_block(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
264 struct nfsd4_blocked_lock *nbl;
266 nbl = find_blocked_lock(lo, fh, nn);
268 nbl= kmalloc(sizeof(*nbl), GFP_KERNEL);
270 INIT_LIST_HEAD(&nbl->nbl_list);
271 INIT_LIST_HEAD(&nbl->nbl_lru);
272 fh_copy_shallow(&nbl->nbl_fh, fh);
273 locks_init_lock(&nbl->nbl_lock);
274 nfsd4_init_cb(&nbl->nbl_cb, lo->lo_owner.so_client,
275 &nfsd4_cb_notify_lock_ops,
276 NFSPROC4_CLNT_CB_NOTIFY_LOCK);
283 free_blocked_lock(struct nfsd4_blocked_lock *nbl)
285 locks_delete_block(&nbl->nbl_lock);
286 locks_release_private(&nbl->nbl_lock);
291 remove_blocked_locks(struct nfs4_lockowner *lo)
293 struct nfs4_client *clp = lo->lo_owner.so_client;
294 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
295 struct nfsd4_blocked_lock *nbl;
298 /* Dequeue all blocked locks */
299 spin_lock(&nn->blocked_locks_lock);
300 while (!list_empty(&lo->lo_blocked)) {
301 nbl = list_first_entry(&lo->lo_blocked,
302 struct nfsd4_blocked_lock,
304 list_del_init(&nbl->nbl_list);
305 list_move(&nbl->nbl_lru, &reaplist);
307 spin_unlock(&nn->blocked_locks_lock);
310 while (!list_empty(&reaplist)) {
311 nbl = list_first_entry(&reaplist, struct nfsd4_blocked_lock,
313 list_del_init(&nbl->nbl_lru);
314 free_blocked_lock(nbl);
319 nfsd4_cb_notify_lock_prepare(struct nfsd4_callback *cb)
321 struct nfsd4_blocked_lock *nbl = container_of(cb,
322 struct nfsd4_blocked_lock, nbl_cb);
323 locks_delete_block(&nbl->nbl_lock);
327 nfsd4_cb_notify_lock_done(struct nfsd4_callback *cb, struct rpc_task *task)
330 * Since this is just an optimization, we don't try very hard if it
331 * turns out not to succeed. We'll requeue it on NFS4ERR_DELAY, and
332 * just quit trying on anything else.
334 switch (task->tk_status) {
336 rpc_delay(task, 1 * HZ);
344 nfsd4_cb_notify_lock_release(struct nfsd4_callback *cb)
346 struct nfsd4_blocked_lock *nbl = container_of(cb,
347 struct nfsd4_blocked_lock, nbl_cb);
349 free_blocked_lock(nbl);
352 static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops = {
353 .prepare = nfsd4_cb_notify_lock_prepare,
354 .done = nfsd4_cb_notify_lock_done,
355 .release = nfsd4_cb_notify_lock_release,
359 * We store the NONE, READ, WRITE, and BOTH bits separately in the
360 * st_{access,deny}_bmap field of the stateid, in order to track not
361 * only what share bits are currently in force, but also what
362 * combinations of share bits previous opens have used. This allows us
363 * to enforce the recommendation of rfc 3530 14.2.19 that the server
364 * return an error if the client attempt to downgrade to a combination
365 * of share bits not explicable by closing some of its previous opens.
367 * XXX: This enforcement is actually incomplete, since we don't keep
368 * track of access/deny bit combinations; so, e.g., we allow:
370 * OPEN allow read, deny write
371 * OPEN allow both, deny none
372 * DOWNGRADE allow read, deny none
374 * which we should reject.
377 bmap_to_share_mode(unsigned long bmap)
380 unsigned int access = 0;
382 for (i = 1; i < 4; i++) {
383 if (test_bit(i, &bmap))
389 /* set share access for a given stateid */
391 set_access(u32 access, struct nfs4_ol_stateid *stp)
393 unsigned char mask = 1 << access;
395 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
396 stp->st_access_bmap |= mask;
399 /* clear share access for a given stateid */
401 clear_access(u32 access, struct nfs4_ol_stateid *stp)
403 unsigned char mask = 1 << access;
405 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
406 stp->st_access_bmap &= ~mask;
409 /* test whether a given stateid has access */
411 test_access(u32 access, struct nfs4_ol_stateid *stp)
413 unsigned char mask = 1 << access;
415 return (bool)(stp->st_access_bmap & mask);
418 /* set share deny for a given stateid */
420 set_deny(u32 deny, struct nfs4_ol_stateid *stp)
422 unsigned char mask = 1 << deny;
424 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
425 stp->st_deny_bmap |= mask;
428 /* clear share deny for a given stateid */
430 clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
432 unsigned char mask = 1 << deny;
434 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
435 stp->st_deny_bmap &= ~mask;
438 /* test whether a given stateid is denying specific access */
440 test_deny(u32 deny, struct nfs4_ol_stateid *stp)
442 unsigned char mask = 1 << deny;
444 return (bool)(stp->st_deny_bmap & mask);
447 static int nfs4_access_to_omode(u32 access)
449 switch (access & NFS4_SHARE_ACCESS_BOTH) {
450 case NFS4_SHARE_ACCESS_READ:
452 case NFS4_SHARE_ACCESS_WRITE:
454 case NFS4_SHARE_ACCESS_BOTH:
462 access_permit_read(struct nfs4_ol_stateid *stp)
464 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
465 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
466 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
470 access_permit_write(struct nfs4_ol_stateid *stp)
472 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
473 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
476 static inline struct nfs4_stateowner *
477 nfs4_get_stateowner(struct nfs4_stateowner *sop)
479 atomic_inc(&sop->so_count);
484 same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
486 return (sop->so_owner.len == owner->len) &&
487 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
490 static struct nfs4_openowner *
491 find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
492 struct nfs4_client *clp)
494 struct nfs4_stateowner *so;
496 lockdep_assert_held(&clp->cl_lock);
498 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
500 if (!so->so_is_open_owner)
502 if (same_owner_str(so, &open->op_owner))
503 return openowner(nfs4_get_stateowner(so));
508 static struct nfs4_openowner *
509 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
510 struct nfs4_client *clp)
512 struct nfs4_openowner *oo;
514 spin_lock(&clp->cl_lock);
515 oo = find_openstateowner_str_locked(hashval, open, clp);
516 spin_unlock(&clp->cl_lock);
521 opaque_hashval(const void *ptr, int nbytes)
523 unsigned char *cptr = (unsigned char *) ptr;
533 static void nfsd4_free_file_rcu(struct rcu_head *rcu)
535 struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
537 kmem_cache_free(file_slab, fp);
541 put_nfs4_file(struct nfs4_file *fi)
543 might_lock(&state_lock);
545 if (refcount_dec_and_lock(&fi->fi_ref, &state_lock)) {
546 hlist_del_rcu(&fi->fi_hash);
547 spin_unlock(&state_lock);
548 WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
549 WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
550 call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
554 static struct nfsd_file *
555 __nfs4_get_fd(struct nfs4_file *f, int oflag)
557 if (f->fi_fds[oflag])
558 return nfsd_file_get(f->fi_fds[oflag]);
562 static struct nfsd_file *
563 find_writeable_file_locked(struct nfs4_file *f)
565 struct nfsd_file *ret;
567 lockdep_assert_held(&f->fi_lock);
569 ret = __nfs4_get_fd(f, O_WRONLY);
571 ret = __nfs4_get_fd(f, O_RDWR);
575 static struct nfsd_file *
576 find_writeable_file(struct nfs4_file *f)
578 struct nfsd_file *ret;
580 spin_lock(&f->fi_lock);
581 ret = find_writeable_file_locked(f);
582 spin_unlock(&f->fi_lock);
587 static struct nfsd_file *
588 find_readable_file_locked(struct nfs4_file *f)
590 struct nfsd_file *ret;
592 lockdep_assert_held(&f->fi_lock);
594 ret = __nfs4_get_fd(f, O_RDONLY);
596 ret = __nfs4_get_fd(f, O_RDWR);
600 static struct nfsd_file *
601 find_readable_file(struct nfs4_file *f)
603 struct nfsd_file *ret;
605 spin_lock(&f->fi_lock);
606 ret = find_readable_file_locked(f);
607 spin_unlock(&f->fi_lock);
613 find_any_file(struct nfs4_file *f)
615 struct nfsd_file *ret;
619 spin_lock(&f->fi_lock);
620 ret = __nfs4_get_fd(f, O_RDWR);
622 ret = __nfs4_get_fd(f, O_WRONLY);
624 ret = __nfs4_get_fd(f, O_RDONLY);
626 spin_unlock(&f->fi_lock);
630 static struct nfsd_file *find_any_file_locked(struct nfs4_file *f)
632 lockdep_assert_held(&f->fi_lock);
634 if (f->fi_fds[O_RDWR])
635 return f->fi_fds[O_RDWR];
636 if (f->fi_fds[O_WRONLY])
637 return f->fi_fds[O_WRONLY];
638 if (f->fi_fds[O_RDONLY])
639 return f->fi_fds[O_RDONLY];
643 static struct nfsd_file *find_deleg_file_locked(struct nfs4_file *f)
645 lockdep_assert_held(&f->fi_lock);
647 if (f->fi_deleg_file)
648 return f->fi_deleg_file;
652 static atomic_long_t num_delegations;
653 unsigned long max_delegations;
656 * Open owner state (share locks)
659 /* hash tables for lock and open owners */
660 #define OWNER_HASH_BITS 8
661 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
662 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
664 static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
668 ret = opaque_hashval(ownername->data, ownername->len);
669 return ret & OWNER_HASH_MASK;
672 /* hash table for nfs4_file */
673 #define FILE_HASH_BITS 8
674 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
676 static unsigned int file_hashval(struct svc_fh *fh)
678 struct inode *inode = d_inode(fh->fh_dentry);
680 /* XXX: why not (here & in file cache) use inode? */
681 return (unsigned int)hash_long(inode->i_ino, FILE_HASH_BITS);
684 static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
687 __nfs4_file_get_access(struct nfs4_file *fp, u32 access)
689 lockdep_assert_held(&fp->fi_lock);
691 if (access & NFS4_SHARE_ACCESS_WRITE)
692 atomic_inc(&fp->fi_access[O_WRONLY]);
693 if (access & NFS4_SHARE_ACCESS_READ)
694 atomic_inc(&fp->fi_access[O_RDONLY]);
698 nfs4_file_get_access(struct nfs4_file *fp, u32 access)
700 lockdep_assert_held(&fp->fi_lock);
702 /* Does this access mode make sense? */
703 if (access & ~NFS4_SHARE_ACCESS_BOTH)
706 /* Does it conflict with a deny mode already set? */
707 if ((access & fp->fi_share_deny) != 0)
708 return nfserr_share_denied;
710 __nfs4_file_get_access(fp, access);
714 static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
716 /* Common case is that there is no deny mode. */
718 /* Does this deny mode make sense? */
719 if (deny & ~NFS4_SHARE_DENY_BOTH)
722 if ((deny & NFS4_SHARE_DENY_READ) &&
723 atomic_read(&fp->fi_access[O_RDONLY]))
724 return nfserr_share_denied;
726 if ((deny & NFS4_SHARE_DENY_WRITE) &&
727 atomic_read(&fp->fi_access[O_WRONLY]))
728 return nfserr_share_denied;
733 static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
735 might_lock(&fp->fi_lock);
737 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
738 struct nfsd_file *f1 = NULL;
739 struct nfsd_file *f2 = NULL;
741 swap(f1, fp->fi_fds[oflag]);
742 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
743 swap(f2, fp->fi_fds[O_RDWR]);
744 spin_unlock(&fp->fi_lock);
752 static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
754 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
756 if (access & NFS4_SHARE_ACCESS_WRITE)
757 __nfs4_file_put_access(fp, O_WRONLY);
758 if (access & NFS4_SHARE_ACCESS_READ)
759 __nfs4_file_put_access(fp, O_RDONLY);
763 * Allocate a new open/delegation state counter. This is needed for
764 * pNFS for proper return on close semantics.
766 * Note that we only allocate it for pNFS-enabled exports, otherwise
767 * all pointers to struct nfs4_clnt_odstate are always NULL.
769 static struct nfs4_clnt_odstate *
770 alloc_clnt_odstate(struct nfs4_client *clp)
772 struct nfs4_clnt_odstate *co;
774 co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
777 refcount_set(&co->co_odcount, 1);
783 hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
785 struct nfs4_file *fp = co->co_file;
787 lockdep_assert_held(&fp->fi_lock);
788 list_add(&co->co_perfile, &fp->fi_clnt_odstate);
792 get_clnt_odstate(struct nfs4_clnt_odstate *co)
795 refcount_inc(&co->co_odcount);
799 put_clnt_odstate(struct nfs4_clnt_odstate *co)
801 struct nfs4_file *fp;
807 if (refcount_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
808 list_del(&co->co_perfile);
809 spin_unlock(&fp->fi_lock);
811 nfsd4_return_all_file_layouts(co->co_client, fp);
812 kmem_cache_free(odstate_slab, co);
816 static struct nfs4_clnt_odstate *
817 find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
819 struct nfs4_clnt_odstate *co;
820 struct nfs4_client *cl;
827 spin_lock(&fp->fi_lock);
828 list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
829 if (co->co_client == cl) {
830 get_clnt_odstate(co);
836 hash_clnt_odstate_locked(new);
838 spin_unlock(&fp->fi_lock);
842 struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab,
843 void (*sc_free)(struct nfs4_stid *))
845 struct nfs4_stid *stid;
848 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
852 idr_preload(GFP_KERNEL);
853 spin_lock(&cl->cl_lock);
854 /* Reserving 0 for start of file in nfsdfs "states" file: */
855 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 1, 0, GFP_NOWAIT);
856 spin_unlock(&cl->cl_lock);
861 stid->sc_free = sc_free;
862 stid->sc_client = cl;
863 stid->sc_stateid.si_opaque.so_id = new_id;
864 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
865 /* Will be incremented before return to client: */
866 refcount_set(&stid->sc_count, 1);
867 spin_lock_init(&stid->sc_lock);
868 INIT_LIST_HEAD(&stid->sc_cp_list);
871 * It shouldn't be a problem to reuse an opaque stateid value.
872 * I don't think it is for 4.1. But with 4.0 I worry that, for
873 * example, a stray write retransmission could be accepted by
874 * the server when it should have been rejected. Therefore,
875 * adopt a trick from the sctp code to attempt to maximize the
876 * amount of time until an id is reused, by ensuring they always
877 * "increase" (mod INT_MAX):
881 kmem_cache_free(slab, stid);
886 * Create a unique stateid_t to represent each COPY.
888 static int nfs4_init_cp_state(struct nfsd_net *nn, copy_stateid_t *stid,
889 unsigned char sc_type)
893 stid->stid.si_opaque.so_clid.cl_boot = (u32)nn->boot_time;
894 stid->stid.si_opaque.so_clid.cl_id = nn->s2s_cp_cl_id;
895 stid->sc_type = sc_type;
897 idr_preload(GFP_KERNEL);
898 spin_lock(&nn->s2s_cp_lock);
899 new_id = idr_alloc_cyclic(&nn->s2s_cp_stateids, stid, 0, 0, GFP_NOWAIT);
900 stid->stid.si_opaque.so_id = new_id;
901 stid->stid.si_generation = 1;
902 spin_unlock(&nn->s2s_cp_lock);
909 int nfs4_init_copy_state(struct nfsd_net *nn, struct nfsd4_copy *copy)
911 return nfs4_init_cp_state(nn, ©->cp_stateid, NFS4_COPY_STID);
914 struct nfs4_cpntf_state *nfs4_alloc_init_cpntf_state(struct nfsd_net *nn,
915 struct nfs4_stid *p_stid)
917 struct nfs4_cpntf_state *cps;
919 cps = kzalloc(sizeof(struct nfs4_cpntf_state), GFP_KERNEL);
922 cps->cpntf_time = ktime_get_boottime_seconds();
923 refcount_set(&cps->cp_stateid.sc_count, 1);
924 if (!nfs4_init_cp_state(nn, &cps->cp_stateid, NFS4_COPYNOTIFY_STID))
926 spin_lock(&nn->s2s_cp_lock);
927 list_add(&cps->cp_list, &p_stid->sc_cp_list);
928 spin_unlock(&nn->s2s_cp_lock);
935 void nfs4_free_copy_state(struct nfsd4_copy *copy)
939 WARN_ON_ONCE(copy->cp_stateid.sc_type != NFS4_COPY_STID);
940 nn = net_generic(copy->cp_clp->net, nfsd_net_id);
941 spin_lock(&nn->s2s_cp_lock);
942 idr_remove(&nn->s2s_cp_stateids,
943 copy->cp_stateid.stid.si_opaque.so_id);
944 spin_unlock(&nn->s2s_cp_lock);
947 static void nfs4_free_cpntf_statelist(struct net *net, struct nfs4_stid *stid)
949 struct nfs4_cpntf_state *cps;
952 nn = net_generic(net, nfsd_net_id);
953 spin_lock(&nn->s2s_cp_lock);
954 while (!list_empty(&stid->sc_cp_list)) {
955 cps = list_first_entry(&stid->sc_cp_list,
956 struct nfs4_cpntf_state, cp_list);
957 _free_cpntf_state_locked(nn, cps);
959 spin_unlock(&nn->s2s_cp_lock);
962 static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
964 struct nfs4_stid *stid;
966 stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid);
970 return openlockstateid(stid);
973 static void nfs4_free_deleg(struct nfs4_stid *stid)
975 WARN_ON(!list_empty(&stid->sc_cp_list));
976 kmem_cache_free(deleg_slab, stid);
977 atomic_long_dec(&num_delegations);
981 * When we recall a delegation, we should be careful not to hand it
982 * out again straight away.
983 * To ensure this we keep a pair of bloom filters ('new' and 'old')
984 * in which the filehandles of recalled delegations are "stored".
985 * If a filehandle appear in either filter, a delegation is blocked.
986 * When a delegation is recalled, the filehandle is stored in the "new"
988 * Every 30 seconds we swap the filters and clear the "new" one,
989 * unless both are empty of course.
991 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
992 * low 3 bytes as hash-table indices.
994 * 'blocked_delegations_lock', which is always taken in block_delegations(),
995 * is used to manage concurrent access. Testing does not need the lock
996 * except when swapping the two filters.
998 static DEFINE_SPINLOCK(blocked_delegations_lock);
999 static struct bloom_pair {
1000 int entries, old_entries;
1002 int new; /* index into 'set' */
1003 DECLARE_BITMAP(set[2], 256);
1004 } blocked_delegations;
1006 static int delegation_blocked(struct knfsd_fh *fh)
1009 struct bloom_pair *bd = &blocked_delegations;
1011 if (bd->entries == 0)
1013 if (ktime_get_seconds() - bd->swap_time > 30) {
1014 spin_lock(&blocked_delegations_lock);
1015 if (ktime_get_seconds() - bd->swap_time > 30) {
1016 bd->entries -= bd->old_entries;
1017 bd->old_entries = bd->entries;
1018 memset(bd->set[bd->new], 0,
1019 sizeof(bd->set[0]));
1020 bd->new = 1-bd->new;
1021 bd->swap_time = ktime_get_seconds();
1023 spin_unlock(&blocked_delegations_lock);
1025 hash = jhash(&fh->fh_base, fh->fh_size, 0);
1026 if (test_bit(hash&255, bd->set[0]) &&
1027 test_bit((hash>>8)&255, bd->set[0]) &&
1028 test_bit((hash>>16)&255, bd->set[0]))
1031 if (test_bit(hash&255, bd->set[1]) &&
1032 test_bit((hash>>8)&255, bd->set[1]) &&
1033 test_bit((hash>>16)&255, bd->set[1]))
1039 static void block_delegations(struct knfsd_fh *fh)
1042 struct bloom_pair *bd = &blocked_delegations;
1044 hash = jhash(&fh->fh_base, fh->fh_size, 0);
1046 spin_lock(&blocked_delegations_lock);
1047 __set_bit(hash&255, bd->set[bd->new]);
1048 __set_bit((hash>>8)&255, bd->set[bd->new]);
1049 __set_bit((hash>>16)&255, bd->set[bd->new]);
1050 if (bd->entries == 0)
1051 bd->swap_time = ktime_get_seconds();
1053 spin_unlock(&blocked_delegations_lock);
1056 static struct nfs4_delegation *
1057 alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
1058 struct svc_fh *current_fh,
1059 struct nfs4_clnt_odstate *odstate)
1061 struct nfs4_delegation *dp;
1064 dprintk("NFSD alloc_init_deleg\n");
1065 n = atomic_long_inc_return(&num_delegations);
1066 if (n < 0 || n > max_delegations)
1068 if (delegation_blocked(¤t_fh->fh_handle))
1070 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg));
1075 * delegation seqid's are never incremented. The 4.1 special
1076 * meaning of seqid 0 isn't meaningful, really, but let's avoid
1077 * 0 anyway just for consistency and use 1:
1079 dp->dl_stid.sc_stateid.si_generation = 1;
1080 INIT_LIST_HEAD(&dp->dl_perfile);
1081 INIT_LIST_HEAD(&dp->dl_perclnt);
1082 INIT_LIST_HEAD(&dp->dl_recall_lru);
1083 dp->dl_clnt_odstate = odstate;
1084 get_clnt_odstate(odstate);
1085 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
1087 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
1088 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
1090 dp->dl_stid.sc_file = fp;
1093 atomic_long_dec(&num_delegations);
1098 nfs4_put_stid(struct nfs4_stid *s)
1100 struct nfs4_file *fp = s->sc_file;
1101 struct nfs4_client *clp = s->sc_client;
1103 might_lock(&clp->cl_lock);
1105 if (!refcount_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
1106 wake_up_all(&close_wq);
1109 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1110 nfs4_free_cpntf_statelist(clp->net, s);
1111 spin_unlock(&clp->cl_lock);
1118 nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
1120 stateid_t *src = &stid->sc_stateid;
1122 spin_lock(&stid->sc_lock);
1123 if (unlikely(++src->si_generation == 0))
1124 src->si_generation = 1;
1125 memcpy(dst, src, sizeof(*dst));
1126 spin_unlock(&stid->sc_lock);
1129 static void put_deleg_file(struct nfs4_file *fp)
1131 struct nfsd_file *nf = NULL;
1133 spin_lock(&fp->fi_lock);
1134 if (--fp->fi_delegees == 0)
1135 swap(nf, fp->fi_deleg_file);
1136 spin_unlock(&fp->fi_lock);
1142 static void nfs4_unlock_deleg_lease(struct nfs4_delegation *dp)
1144 struct nfs4_file *fp = dp->dl_stid.sc_file;
1145 struct nfsd_file *nf = fp->fi_deleg_file;
1147 WARN_ON_ONCE(!fp->fi_delegees);
1149 vfs_setlease(nf->nf_file, F_UNLCK, NULL, (void **)&dp);
1153 static void destroy_unhashed_deleg(struct nfs4_delegation *dp)
1155 put_clnt_odstate(dp->dl_clnt_odstate);
1156 nfs4_unlock_deleg_lease(dp);
1157 nfs4_put_stid(&dp->dl_stid);
1160 void nfs4_unhash_stid(struct nfs4_stid *s)
1166 * nfs4_delegation_exists - Discover if this delegation already exists
1167 * @clp: a pointer to the nfs4_client we're granting a delegation to
1168 * @fp: a pointer to the nfs4_file we're granting a delegation on
1171 * On success: true iff an existing delegation is found
1175 nfs4_delegation_exists(struct nfs4_client *clp, struct nfs4_file *fp)
1177 struct nfs4_delegation *searchdp = NULL;
1178 struct nfs4_client *searchclp = NULL;
1180 lockdep_assert_held(&state_lock);
1181 lockdep_assert_held(&fp->fi_lock);
1183 list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
1184 searchclp = searchdp->dl_stid.sc_client;
1185 if (clp == searchclp) {
1193 * hash_delegation_locked - Add a delegation to the appropriate lists
1194 * @dp: a pointer to the nfs4_delegation we are adding.
1195 * @fp: a pointer to the nfs4_file we're granting a delegation on
1198 * On success: NULL if the delegation was successfully hashed.
1200 * On error: -EAGAIN if one was previously granted to this
1201 * nfs4_client for this nfs4_file. Delegation is not hashed.
1206 hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
1208 struct nfs4_client *clp = dp->dl_stid.sc_client;
1210 lockdep_assert_held(&state_lock);
1211 lockdep_assert_held(&fp->fi_lock);
1213 if (nfs4_delegation_exists(clp, fp))
1215 refcount_inc(&dp->dl_stid.sc_count);
1216 dp->dl_stid.sc_type = NFS4_DELEG_STID;
1217 list_add(&dp->dl_perfile, &fp->fi_delegations);
1218 list_add(&dp->dl_perclnt, &clp->cl_delegations);
1222 static bool delegation_hashed(struct nfs4_delegation *dp)
1224 return !(list_empty(&dp->dl_perfile));
1228 unhash_delegation_locked(struct nfs4_delegation *dp)
1230 struct nfs4_file *fp = dp->dl_stid.sc_file;
1232 lockdep_assert_held(&state_lock);
1234 if (!delegation_hashed(dp))
1237 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
1238 /* Ensure that deleg break won't try to requeue it */
1240 spin_lock(&fp->fi_lock);
1241 list_del_init(&dp->dl_perclnt);
1242 list_del_init(&dp->dl_recall_lru);
1243 list_del_init(&dp->dl_perfile);
1244 spin_unlock(&fp->fi_lock);
1248 static void destroy_delegation(struct nfs4_delegation *dp)
1252 spin_lock(&state_lock);
1253 unhashed = unhash_delegation_locked(dp);
1254 spin_unlock(&state_lock);
1256 destroy_unhashed_deleg(dp);
1259 static void revoke_delegation(struct nfs4_delegation *dp)
1261 struct nfs4_client *clp = dp->dl_stid.sc_client;
1263 WARN_ON(!list_empty(&dp->dl_recall_lru));
1265 if (clp->cl_minorversion) {
1266 spin_lock(&clp->cl_lock);
1267 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
1268 refcount_inc(&dp->dl_stid.sc_count);
1269 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
1270 spin_unlock(&clp->cl_lock);
1272 destroy_unhashed_deleg(dp);
1279 static unsigned int clientid_hashval(u32 id)
1281 return id & CLIENT_HASH_MASK;
1284 static unsigned int clientstr_hashval(struct xdr_netobj name)
1286 return opaque_hashval(name.data, 8) & CLIENT_HASH_MASK;
1290 * A stateid that had a deny mode associated with it is being released
1291 * or downgraded. Recalculate the deny mode on the file.
1294 recalculate_deny_mode(struct nfs4_file *fp)
1296 struct nfs4_ol_stateid *stp;
1298 spin_lock(&fp->fi_lock);
1299 fp->fi_share_deny = 0;
1300 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
1301 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
1302 spin_unlock(&fp->fi_lock);
1306 reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
1309 bool change = false;
1311 for (i = 1; i < 4; i++) {
1312 if ((i & deny) != i) {
1318 /* Recalculate per-file deny mode if there was a change */
1320 recalculate_deny_mode(stp->st_stid.sc_file);
1323 /* release all access and file references for a given stateid */
1325 release_all_access(struct nfs4_ol_stateid *stp)
1328 struct nfs4_file *fp = stp->st_stid.sc_file;
1330 if (fp && stp->st_deny_bmap != 0)
1331 recalculate_deny_mode(fp);
1333 for (i = 1; i < 4; i++) {
1334 if (test_access(i, stp))
1335 nfs4_file_put_access(stp->st_stid.sc_file, i);
1336 clear_access(i, stp);
1340 static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1342 kfree(sop->so_owner.data);
1343 sop->so_ops->so_free(sop);
1346 static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1348 struct nfs4_client *clp = sop->so_client;
1350 might_lock(&clp->cl_lock);
1352 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
1354 sop->so_ops->so_unhash(sop);
1355 spin_unlock(&clp->cl_lock);
1356 nfs4_free_stateowner(sop);
1360 nfs4_ol_stateid_unhashed(const struct nfs4_ol_stateid *stp)
1362 return list_empty(&stp->st_perfile);
1365 static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
1367 struct nfs4_file *fp = stp->st_stid.sc_file;
1369 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1371 if (list_empty(&stp->st_perfile))
1374 spin_lock(&fp->fi_lock);
1375 list_del_init(&stp->st_perfile);
1376 spin_unlock(&fp->fi_lock);
1377 list_del(&stp->st_perstateowner);
1381 static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
1383 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1385 put_clnt_odstate(stp->st_clnt_odstate);
1386 release_all_access(stp);
1387 if (stp->st_stateowner)
1388 nfs4_put_stateowner(stp->st_stateowner);
1389 WARN_ON(!list_empty(&stid->sc_cp_list));
1390 kmem_cache_free(stateid_slab, stid);
1393 static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
1395 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1396 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
1397 struct nfsd_file *nf;
1399 nf = find_any_file(stp->st_stid.sc_file);
1401 get_file(nf->nf_file);
1402 filp_close(nf->nf_file, (fl_owner_t)lo);
1405 nfs4_free_ol_stateid(stid);
1409 * Put the persistent reference to an already unhashed generic stateid, while
1410 * holding the cl_lock. If it's the last reference, then put it onto the
1411 * reaplist for later destruction.
1413 static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1414 struct list_head *reaplist)
1416 struct nfs4_stid *s = &stp->st_stid;
1417 struct nfs4_client *clp = s->sc_client;
1419 lockdep_assert_held(&clp->cl_lock);
1421 WARN_ON_ONCE(!list_empty(&stp->st_locks));
1423 if (!refcount_dec_and_test(&s->sc_count)) {
1424 wake_up_all(&close_wq);
1428 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1429 list_add(&stp->st_locks, reaplist);
1432 static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
1434 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1436 if (!unhash_ol_stateid(stp))
1438 list_del_init(&stp->st_locks);
1439 nfs4_unhash_stid(&stp->st_stid);
1443 static void release_lock_stateid(struct nfs4_ol_stateid *stp)
1445 struct nfs4_client *clp = stp->st_stid.sc_client;
1448 spin_lock(&clp->cl_lock);
1449 unhashed = unhash_lock_stateid(stp);
1450 spin_unlock(&clp->cl_lock);
1452 nfs4_put_stid(&stp->st_stid);
1455 static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
1457 struct nfs4_client *clp = lo->lo_owner.so_client;
1459 lockdep_assert_held(&clp->cl_lock);
1461 list_del_init(&lo->lo_owner.so_strhash);
1465 * Free a list of generic stateids that were collected earlier after being
1469 free_ol_stateid_reaplist(struct list_head *reaplist)
1471 struct nfs4_ol_stateid *stp;
1472 struct nfs4_file *fp;
1476 while (!list_empty(reaplist)) {
1477 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1479 list_del(&stp->st_locks);
1480 fp = stp->st_stid.sc_file;
1481 stp->st_stid.sc_free(&stp->st_stid);
1487 static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1488 struct list_head *reaplist)
1490 struct nfs4_ol_stateid *stp;
1492 lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1494 while (!list_empty(&open_stp->st_locks)) {
1495 stp = list_entry(open_stp->st_locks.next,
1496 struct nfs4_ol_stateid, st_locks);
1497 WARN_ON(!unhash_lock_stateid(stp));
1498 put_ol_stateid_locked(stp, reaplist);
1502 static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
1503 struct list_head *reaplist)
1505 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1507 if (!unhash_ol_stateid(stp))
1509 release_open_stateid_locks(stp, reaplist);
1513 static void release_open_stateid(struct nfs4_ol_stateid *stp)
1515 LIST_HEAD(reaplist);
1517 spin_lock(&stp->st_stid.sc_client->cl_lock);
1518 if (unhash_open_stateid(stp, &reaplist))
1519 put_ol_stateid_locked(stp, &reaplist);
1520 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1521 free_ol_stateid_reaplist(&reaplist);
1524 static void unhash_openowner_locked(struct nfs4_openowner *oo)
1526 struct nfs4_client *clp = oo->oo_owner.so_client;
1528 lockdep_assert_held(&clp->cl_lock);
1530 list_del_init(&oo->oo_owner.so_strhash);
1531 list_del_init(&oo->oo_perclient);
1534 static void release_last_closed_stateid(struct nfs4_openowner *oo)
1536 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1538 struct nfs4_ol_stateid *s;
1540 spin_lock(&nn->client_lock);
1541 s = oo->oo_last_closed_stid;
1543 list_del_init(&oo->oo_close_lru);
1544 oo->oo_last_closed_stid = NULL;
1546 spin_unlock(&nn->client_lock);
1548 nfs4_put_stid(&s->st_stid);
1551 static void release_openowner(struct nfs4_openowner *oo)
1553 struct nfs4_ol_stateid *stp;
1554 struct nfs4_client *clp = oo->oo_owner.so_client;
1555 struct list_head reaplist;
1557 INIT_LIST_HEAD(&reaplist);
1559 spin_lock(&clp->cl_lock);
1560 unhash_openowner_locked(oo);
1561 while (!list_empty(&oo->oo_owner.so_stateids)) {
1562 stp = list_first_entry(&oo->oo_owner.so_stateids,
1563 struct nfs4_ol_stateid, st_perstateowner);
1564 if (unhash_open_stateid(stp, &reaplist))
1565 put_ol_stateid_locked(stp, &reaplist);
1567 spin_unlock(&clp->cl_lock);
1568 free_ol_stateid_reaplist(&reaplist);
1569 release_last_closed_stateid(oo);
1570 nfs4_put_stateowner(&oo->oo_owner);
1574 hash_sessionid(struct nfs4_sessionid *sessionid)
1576 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1578 return sid->sequence % SESSION_HASH_SIZE;
1581 #ifdef CONFIG_SUNRPC_DEBUG
1583 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1585 u32 *ptr = (u32 *)(&sessionid->data[0]);
1586 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1590 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1596 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1597 * won't be used for replay.
1599 void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1601 struct nfs4_stateowner *so = cstate->replay_owner;
1603 if (nfserr == nfserr_replay_me)
1606 if (!seqid_mutating_err(ntohl(nfserr))) {
1607 nfsd4_cstate_clear_replay(cstate);
1612 if (so->so_is_open_owner)
1613 release_last_closed_stateid(openowner(so));
1619 gen_sessionid(struct nfsd4_session *ses)
1621 struct nfs4_client *clp = ses->se_client;
1622 struct nfsd4_sessionid *sid;
1624 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1625 sid->clientid = clp->cl_clientid;
1626 sid->sequence = current_sessionid++;
1631 * The protocol defines ca_maxresponssize_cached to include the size of
1632 * the rpc header, but all we need to cache is the data starting after
1633 * the end of the initial SEQUENCE operation--the rest we regenerate
1634 * each time. Therefore we can advertise a ca_maxresponssize_cached
1635 * value that is the number of bytes in our cache plus a few additional
1636 * bytes. In order to stay on the safe side, and not promise more than
1637 * we can cache, those additional bytes must be the minimum possible: 24
1638 * bytes of rpc header (xid through accept state, with AUTH_NULL
1639 * verifier), 12 for the compound header (with zero-length tag), and 44
1640 * for the SEQUENCE op response:
1642 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1645 free_session_slots(struct nfsd4_session *ses)
1649 for (i = 0; i < ses->se_fchannel.maxreqs; i++) {
1650 free_svc_cred(&ses->se_slots[i]->sl_cred);
1651 kfree(ses->se_slots[i]);
1656 * We don't actually need to cache the rpc and session headers, so we
1657 * can allocate a little less for each slot:
1659 static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
1663 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1666 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1667 return size + sizeof(struct nfsd4_slot);
1671 * XXX: If we run out of reserved DRC memory we could (up to a point)
1672 * re-negotiate active sessions and reduce their slot usage to make
1673 * room for new connections. For now we just fail the create session.
1675 static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
1677 u32 slotsize = slot_bytes(ca);
1678 u32 num = ca->maxreqs;
1679 unsigned long avail, total_avail;
1680 unsigned int scale_factor;
1682 spin_lock(&nfsd_drc_lock);
1683 if (nfsd_drc_max_mem > nfsd_drc_mem_used)
1684 total_avail = nfsd_drc_max_mem - nfsd_drc_mem_used;
1686 /* We have handed out more space than we chose in
1687 * set_max_drc() to allow. That isn't really a
1688 * problem as long as that doesn't make us think we
1689 * have lots more due to integer overflow.
1692 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, total_avail);
1694 * Never use more than a fraction of the remaining memory,
1695 * unless it's the only way to give this client a slot.
1696 * The chosen fraction is either 1/8 or 1/number of threads,
1697 * whichever is smaller. This ensures there are adequate
1698 * slots to support multiple clients per thread.
1699 * Give the client one slot even if that would require
1700 * over-allocation--it is better than failure.
1702 scale_factor = max_t(unsigned int, 8, nn->nfsd_serv->sv_nrthreads);
1704 avail = clamp_t(unsigned long, avail, slotsize,
1705 total_avail/scale_factor);
1706 num = min_t(int, num, avail / slotsize);
1707 num = max_t(int, num, 1);
1708 nfsd_drc_mem_used += num * slotsize;
1709 spin_unlock(&nfsd_drc_lock);
1714 static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
1716 int slotsize = slot_bytes(ca);
1718 spin_lock(&nfsd_drc_lock);
1719 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
1720 spin_unlock(&nfsd_drc_lock);
1723 static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1724 struct nfsd4_channel_attrs *battrs)
1726 int numslots = fattrs->maxreqs;
1727 int slotsize = slot_bytes(fattrs);
1728 struct nfsd4_session *new;
1731 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
1732 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1733 mem = numslots * sizeof(struct nfsd4_slot *);
1735 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
1738 /* allocate each struct nfsd4_slot and data cache in one piece */
1739 for (i = 0; i < numslots; i++) {
1740 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
1741 if (!new->se_slots[i])
1745 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1746 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1751 kfree(new->se_slots[i]);
1756 static void free_conn(struct nfsd4_conn *c)
1758 svc_xprt_put(c->cn_xprt);
1762 static void nfsd4_conn_lost(struct svc_xpt_user *u)
1764 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1765 struct nfs4_client *clp = c->cn_session->se_client;
1767 trace_nfsd_cb_lost(clp);
1769 spin_lock(&clp->cl_lock);
1770 if (!list_empty(&c->cn_persession)) {
1771 list_del(&c->cn_persession);
1774 nfsd4_probe_callback(clp);
1775 spin_unlock(&clp->cl_lock);
1778 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
1780 struct nfsd4_conn *conn;
1782 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1785 svc_xprt_get(rqstp->rq_xprt);
1786 conn->cn_xprt = rqstp->rq_xprt;
1787 conn->cn_flags = flags;
1788 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1792 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1794 conn->cn_session = ses;
1795 list_add(&conn->cn_persession, &ses->se_conns);
1798 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1800 struct nfs4_client *clp = ses->se_client;
1802 spin_lock(&clp->cl_lock);
1803 __nfsd4_hash_conn(conn, ses);
1804 spin_unlock(&clp->cl_lock);
1807 static int nfsd4_register_conn(struct nfsd4_conn *conn)
1809 conn->cn_xpt_user.callback = nfsd4_conn_lost;
1810 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
1813 static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
1817 nfsd4_hash_conn(conn, ses);
1818 ret = nfsd4_register_conn(conn);
1820 /* oops; xprt is already down: */
1821 nfsd4_conn_lost(&conn->cn_xpt_user);
1822 /* We may have gained or lost a callback channel: */
1823 nfsd4_probe_callback_sync(ses->se_client);
1826 static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
1828 u32 dir = NFS4_CDFC4_FORE;
1830 if (cses->flags & SESSION4_BACK_CHAN)
1831 dir |= NFS4_CDFC4_BACK;
1832 return alloc_conn(rqstp, dir);
1835 /* must be called under client_lock */
1836 static void nfsd4_del_conns(struct nfsd4_session *s)
1838 struct nfs4_client *clp = s->se_client;
1839 struct nfsd4_conn *c;
1841 spin_lock(&clp->cl_lock);
1842 while (!list_empty(&s->se_conns)) {
1843 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1844 list_del_init(&c->cn_persession);
1845 spin_unlock(&clp->cl_lock);
1847 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1850 spin_lock(&clp->cl_lock);
1852 spin_unlock(&clp->cl_lock);
1855 static void __free_session(struct nfsd4_session *ses)
1857 free_session_slots(ses);
1861 static void free_session(struct nfsd4_session *ses)
1863 nfsd4_del_conns(ses);
1864 nfsd4_put_drc_mem(&ses->se_fchannel);
1865 __free_session(ses);
1868 static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
1871 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1873 new->se_client = clp;
1876 INIT_LIST_HEAD(&new->se_conns);
1878 new->se_cb_seq_nr = 1;
1879 new->se_flags = cses->flags;
1880 new->se_cb_prog = cses->callback_prog;
1881 new->se_cb_sec = cses->cb_sec;
1882 atomic_set(&new->se_ref, 0);
1883 idx = hash_sessionid(&new->se_sessionid);
1884 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
1885 spin_lock(&clp->cl_lock);
1886 list_add(&new->se_perclnt, &clp->cl_sessions);
1887 spin_unlock(&clp->cl_lock);
1890 struct sockaddr *sa = svc_addr(rqstp);
1892 * This is a little silly; with sessions there's no real
1893 * use for the callback address. Use the peer address
1894 * as a reasonable default for now, but consider fixing
1895 * the rpc client not to require an address in the
1898 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1899 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
1903 /* caller must hold client_lock */
1904 static struct nfsd4_session *
1905 __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
1907 struct nfsd4_session *elem;
1909 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1911 lockdep_assert_held(&nn->client_lock);
1913 dump_sessionid(__func__, sessionid);
1914 idx = hash_sessionid(sessionid);
1915 /* Search in the appropriate list */
1916 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
1917 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1918 NFS4_MAX_SESSIONID_LEN)) {
1923 dprintk("%s: session not found\n", __func__);
1927 static struct nfsd4_session *
1928 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1931 struct nfsd4_session *session;
1932 __be32 status = nfserr_badsession;
1934 session = __find_in_sessionid_hashtbl(sessionid, net);
1937 status = nfsd4_get_session_locked(session);
1945 /* caller must hold client_lock */
1947 unhash_session(struct nfsd4_session *ses)
1949 struct nfs4_client *clp = ses->se_client;
1950 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1952 lockdep_assert_held(&nn->client_lock);
1954 list_del(&ses->se_hash);
1955 spin_lock(&ses->se_client->cl_lock);
1956 list_del(&ses->se_perclnt);
1957 spin_unlock(&ses->se_client->cl_lock);
1960 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1962 STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
1965 * We're assuming the clid was not given out from a boot
1966 * precisely 2^32 (about 136 years) before this one. That seems
1967 * a safe assumption:
1969 if (clid->cl_boot == (u32)nn->boot_time)
1971 trace_nfsd_clid_stale(clid);
1976 * XXX Should we use a slab cache ?
1977 * This type of memory management is somewhat inefficient, but we use it
1978 * anyway since SETCLIENTID is not a common operation.
1980 static struct nfs4_client *alloc_client(struct xdr_netobj name)
1982 struct nfs4_client *clp;
1985 clp = kmem_cache_zalloc(client_slab, GFP_KERNEL);
1988 xdr_netobj_dup(&clp->cl_name, &name, GFP_KERNEL);
1989 if (clp->cl_name.data == NULL)
1991 clp->cl_ownerstr_hashtbl = kmalloc_array(OWNER_HASH_SIZE,
1992 sizeof(struct list_head),
1994 if (!clp->cl_ownerstr_hashtbl)
1995 goto err_no_hashtbl;
1996 for (i = 0; i < OWNER_HASH_SIZE; i++)
1997 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
1998 INIT_LIST_HEAD(&clp->cl_sessions);
1999 idr_init(&clp->cl_stateids);
2000 atomic_set(&clp->cl_rpc_users, 0);
2001 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
2002 INIT_LIST_HEAD(&clp->cl_idhash);
2003 INIT_LIST_HEAD(&clp->cl_openowners);
2004 INIT_LIST_HEAD(&clp->cl_delegations);
2005 INIT_LIST_HEAD(&clp->cl_lru);
2006 INIT_LIST_HEAD(&clp->cl_revoked);
2007 #ifdef CONFIG_NFSD_PNFS
2008 INIT_LIST_HEAD(&clp->cl_lo_states);
2010 INIT_LIST_HEAD(&clp->async_copies);
2011 spin_lock_init(&clp->async_lock);
2012 spin_lock_init(&clp->cl_lock);
2013 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
2016 kfree(clp->cl_name.data);
2018 kmem_cache_free(client_slab, clp);
2022 static void __free_client(struct kref *k)
2024 struct nfsdfs_client *c = container_of(k, struct nfsdfs_client, cl_ref);
2025 struct nfs4_client *clp = container_of(c, struct nfs4_client, cl_nfsdfs);
2027 free_svc_cred(&clp->cl_cred);
2028 kfree(clp->cl_ownerstr_hashtbl);
2029 kfree(clp->cl_name.data);
2030 kfree(clp->cl_nii_domain.data);
2031 kfree(clp->cl_nii_name.data);
2032 idr_destroy(&clp->cl_stateids);
2033 kmem_cache_free(client_slab, clp);
2036 static void drop_client(struct nfs4_client *clp)
2038 kref_put(&clp->cl_nfsdfs.cl_ref, __free_client);
2042 free_client(struct nfs4_client *clp)
2044 while (!list_empty(&clp->cl_sessions)) {
2045 struct nfsd4_session *ses;
2046 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
2048 list_del(&ses->se_perclnt);
2049 WARN_ON_ONCE(atomic_read(&ses->se_ref));
2052 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
2053 if (clp->cl_nfsd_dentry) {
2054 nfsd_client_rmdir(clp->cl_nfsd_dentry);
2055 clp->cl_nfsd_dentry = NULL;
2056 wake_up_all(&expiry_wq);
2061 /* must be called under the client_lock */
2063 unhash_client_locked(struct nfs4_client *clp)
2065 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2066 struct nfsd4_session *ses;
2068 lockdep_assert_held(&nn->client_lock);
2070 /* Mark the client as expired! */
2072 /* Make it invisible */
2073 if (!list_empty(&clp->cl_idhash)) {
2074 list_del_init(&clp->cl_idhash);
2075 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
2076 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
2078 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
2080 list_del_init(&clp->cl_lru);
2081 spin_lock(&clp->cl_lock);
2082 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
2083 list_del_init(&ses->se_hash);
2084 spin_unlock(&clp->cl_lock);
2088 unhash_client(struct nfs4_client *clp)
2090 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2092 spin_lock(&nn->client_lock);
2093 unhash_client_locked(clp);
2094 spin_unlock(&nn->client_lock);
2097 static __be32 mark_client_expired_locked(struct nfs4_client *clp)
2099 if (atomic_read(&clp->cl_rpc_users))
2100 return nfserr_jukebox;
2101 unhash_client_locked(clp);
2106 __destroy_client(struct nfs4_client *clp)
2109 struct nfs4_openowner *oo;
2110 struct nfs4_delegation *dp;
2111 struct list_head reaplist;
2113 INIT_LIST_HEAD(&reaplist);
2114 spin_lock(&state_lock);
2115 while (!list_empty(&clp->cl_delegations)) {
2116 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
2117 WARN_ON(!unhash_delegation_locked(dp));
2118 list_add(&dp->dl_recall_lru, &reaplist);
2120 spin_unlock(&state_lock);
2121 while (!list_empty(&reaplist)) {
2122 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
2123 list_del_init(&dp->dl_recall_lru);
2124 destroy_unhashed_deleg(dp);
2126 while (!list_empty(&clp->cl_revoked)) {
2127 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
2128 list_del_init(&dp->dl_recall_lru);
2129 nfs4_put_stid(&dp->dl_stid);
2131 while (!list_empty(&clp->cl_openowners)) {
2132 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
2133 nfs4_get_stateowner(&oo->oo_owner);
2134 release_openowner(oo);
2136 for (i = 0; i < OWNER_HASH_SIZE; i++) {
2137 struct nfs4_stateowner *so, *tmp;
2139 list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i],
2141 /* Should be no openowners at this point */
2142 WARN_ON_ONCE(so->so_is_open_owner);
2143 remove_blocked_locks(lockowner(so));
2146 nfsd4_return_all_client_layouts(clp);
2147 nfsd4_shutdown_copy(clp);
2148 nfsd4_shutdown_callback(clp);
2149 if (clp->cl_cb_conn.cb_xprt)
2150 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
2152 wake_up_all(&expiry_wq);
2156 destroy_client(struct nfs4_client *clp)
2159 __destroy_client(clp);
2162 static void inc_reclaim_complete(struct nfs4_client *clp)
2164 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2166 if (!nn->track_reclaim_completes)
2168 if (!nfsd4_find_reclaim_client(clp->cl_name, nn))
2170 if (atomic_inc_return(&nn->nr_reclaim_complete) ==
2171 nn->reclaim_str_hashtbl_size) {
2172 printk(KERN_INFO "NFSD: all clients done reclaiming, ending NFSv4 grace period (net %x)\n",
2174 nfsd4_end_grace(nn);
2178 static void expire_client(struct nfs4_client *clp)
2181 nfsd4_client_record_remove(clp);
2182 __destroy_client(clp);
2185 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
2187 memcpy(target->cl_verifier.data, source->data,
2188 sizeof(target->cl_verifier.data));
2191 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
2193 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
2194 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
2197 static int copy_cred(struct svc_cred *target, struct svc_cred *source)
2199 target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL);
2200 target->cr_raw_principal = kstrdup(source->cr_raw_principal,
2202 target->cr_targ_princ = kstrdup(source->cr_targ_princ, GFP_KERNEL);
2203 if ((source->cr_principal && !target->cr_principal) ||
2204 (source->cr_raw_principal && !target->cr_raw_principal) ||
2205 (source->cr_targ_princ && !target->cr_targ_princ))
2208 target->cr_flavor = source->cr_flavor;
2209 target->cr_uid = source->cr_uid;
2210 target->cr_gid = source->cr_gid;
2211 target->cr_group_info = source->cr_group_info;
2212 get_group_info(target->cr_group_info);
2213 target->cr_gss_mech = source->cr_gss_mech;
2214 if (source->cr_gss_mech)
2215 gss_mech_get(source->cr_gss_mech);
2220 compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
2222 if (o1->len < o2->len)
2224 if (o1->len > o2->len)
2226 return memcmp(o1->data, o2->data, o1->len);
2230 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
2232 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
2236 same_clid(clientid_t *cl1, clientid_t *cl2)
2238 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
2241 static bool groups_equal(struct group_info *g1, struct group_info *g2)
2245 if (g1->ngroups != g2->ngroups)
2247 for (i=0; i<g1->ngroups; i++)
2248 if (!gid_eq(g1->gid[i], g2->gid[i]))
2254 * RFC 3530 language requires clid_inuse be returned when the
2255 * "principal" associated with a requests differs from that previously
2256 * used. We use uid, gid's, and gss principal string as our best
2257 * approximation. We also don't want to allow non-gss use of a client
2258 * established using gss: in theory cr_principal should catch that
2259 * change, but in practice cr_principal can be null even in the gss case
2260 * since gssd doesn't always pass down a principal string.
2262 static bool is_gss_cred(struct svc_cred *cr)
2264 /* Is cr_flavor one of the gss "pseudoflavors"?: */
2265 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
2270 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
2272 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
2273 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
2274 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
2275 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
2277 /* XXX: check that cr_targ_princ fields match ? */
2278 if (cr1->cr_principal == cr2->cr_principal)
2280 if (!cr1->cr_principal || !cr2->cr_principal)
2282 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
2285 static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
2287 struct svc_cred *cr = &rqstp->rq_cred;
2290 if (!cr->cr_gss_mech)
2292 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
2293 return service == RPC_GSS_SVC_INTEGRITY ||
2294 service == RPC_GSS_SVC_PRIVACY;
2297 bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
2299 struct svc_cred *cr = &rqstp->rq_cred;
2301 if (!cl->cl_mach_cred)
2303 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
2305 if (!svc_rqst_integrity_protected(rqstp))
2307 if (cl->cl_cred.cr_raw_principal)
2308 return 0 == strcmp(cl->cl_cred.cr_raw_principal,
2309 cr->cr_raw_principal);
2310 if (!cr->cr_principal)
2312 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
2315 static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
2320 * This is opaque to client, so no need to byte-swap. Use
2321 * __force to keep sparse happy
2323 verf[0] = (__force __be32)(u32)ktime_get_real_seconds();
2324 verf[1] = (__force __be32)nn->clverifier_counter++;
2325 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
2328 static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
2330 clp->cl_clientid.cl_boot = (u32)nn->boot_time;
2331 clp->cl_clientid.cl_id = nn->clientid_counter++;
2332 gen_confirm(clp, nn);
2335 static struct nfs4_stid *
2336 find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
2338 struct nfs4_stid *ret;
2340 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
2341 if (!ret || !ret->sc_type)
2346 static struct nfs4_stid *
2347 find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
2349 struct nfs4_stid *s;
2351 spin_lock(&cl->cl_lock);
2352 s = find_stateid_locked(cl, t);
2354 if (typemask & s->sc_type)
2355 refcount_inc(&s->sc_count);
2359 spin_unlock(&cl->cl_lock);
2363 static struct nfs4_client *get_nfsdfs_clp(struct inode *inode)
2365 struct nfsdfs_client *nc;
2366 nc = get_nfsdfs_client(inode);
2369 return container_of(nc, struct nfs4_client, cl_nfsdfs);
2372 static void seq_quote_mem(struct seq_file *m, char *data, int len)
2374 seq_printf(m, "\"");
2375 seq_escape_mem(m, data, len, ESCAPE_HEX | ESCAPE_NAP | ESCAPE_APPEND, "\"\\");
2376 seq_printf(m, "\"");
2379 static const char *cb_state2str(int state)
2384 case NFSD4_CB_UNKNOWN:
2388 case NFSD4_CB_FAULT:
2394 static int client_info_show(struct seq_file *m, void *v)
2396 struct inode *inode = m->private;
2397 struct nfs4_client *clp;
2400 clp = get_nfsdfs_clp(inode);
2403 memcpy(&clid, &clp->cl_clientid, sizeof(clid));
2404 seq_printf(m, "clientid: 0x%llx\n", clid);
2405 seq_printf(m, "address: \"%pISpc\"\n", (struct sockaddr *)&clp->cl_addr);
2406 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
2407 seq_puts(m, "status: confirmed\n");
2409 seq_puts(m, "status: unconfirmed\n");
2410 seq_printf(m, "name: ");
2411 seq_quote_mem(m, clp->cl_name.data, clp->cl_name.len);
2412 seq_printf(m, "\nminor version: %d\n", clp->cl_minorversion);
2413 if (clp->cl_nii_domain.data) {
2414 seq_printf(m, "Implementation domain: ");
2415 seq_quote_mem(m, clp->cl_nii_domain.data,
2416 clp->cl_nii_domain.len);
2417 seq_printf(m, "\nImplementation name: ");
2418 seq_quote_mem(m, clp->cl_nii_name.data, clp->cl_nii_name.len);
2419 seq_printf(m, "\nImplementation time: [%lld, %ld]\n",
2420 clp->cl_nii_time.tv_sec, clp->cl_nii_time.tv_nsec);
2422 seq_printf(m, "callback state: %s\n", cb_state2str(clp->cl_cb_state));
2423 seq_printf(m, "callback address: %pISpc\n", &clp->cl_cb_conn.cb_addr);
2429 static int client_info_open(struct inode *inode, struct file *file)
2431 return single_open(file, client_info_show, inode);
2434 static const struct file_operations client_info_fops = {
2435 .open = client_info_open,
2437 .llseek = seq_lseek,
2438 .release = single_release,
2441 static void *states_start(struct seq_file *s, loff_t *pos)
2442 __acquires(&clp->cl_lock)
2444 struct nfs4_client *clp = s->private;
2445 unsigned long id = *pos;
2448 spin_lock(&clp->cl_lock);
2449 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2454 static void *states_next(struct seq_file *s, void *v, loff_t *pos)
2456 struct nfs4_client *clp = s->private;
2457 unsigned long id = *pos;
2462 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2467 static void states_stop(struct seq_file *s, void *v)
2468 __releases(&clp->cl_lock)
2470 struct nfs4_client *clp = s->private;
2472 spin_unlock(&clp->cl_lock);
2475 static void nfs4_show_fname(struct seq_file *s, struct nfsd_file *f)
2477 seq_printf(s, "filename: \"%pD2\"", f->nf_file);
2480 static void nfs4_show_superblock(struct seq_file *s, struct nfsd_file *f)
2482 struct inode *inode = f->nf_inode;
2484 seq_printf(s, "superblock: \"%02x:%02x:%ld\"",
2485 MAJOR(inode->i_sb->s_dev),
2486 MINOR(inode->i_sb->s_dev),
2490 static void nfs4_show_owner(struct seq_file *s, struct nfs4_stateowner *oo)
2492 seq_printf(s, "owner: ");
2493 seq_quote_mem(s, oo->so_owner.data, oo->so_owner.len);
2496 static void nfs4_show_stateid(struct seq_file *s, stateid_t *stid)
2498 seq_printf(s, "0x%.8x", stid->si_generation);
2499 seq_printf(s, "%12phN", &stid->si_opaque);
2502 static int nfs4_show_open(struct seq_file *s, struct nfs4_stid *st)
2504 struct nfs4_ol_stateid *ols;
2505 struct nfs4_file *nf;
2506 struct nfsd_file *file;
2507 struct nfs4_stateowner *oo;
2508 unsigned int access, deny;
2510 if (st->sc_type != NFS4_OPEN_STID && st->sc_type != NFS4_LOCK_STID)
2511 return 0; /* XXX: or SEQ_SKIP? */
2512 ols = openlockstateid(st);
2513 oo = ols->st_stateowner;
2516 spin_lock(&nf->fi_lock);
2517 file = find_any_file_locked(nf);
2521 seq_printf(s, "- ");
2522 nfs4_show_stateid(s, &st->sc_stateid);
2523 seq_printf(s, ": { type: open, ");
2525 access = bmap_to_share_mode(ols->st_access_bmap);
2526 deny = bmap_to_share_mode(ols->st_deny_bmap);
2528 seq_printf(s, "access: %s%s, ",
2529 access & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2530 access & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2531 seq_printf(s, "deny: %s%s, ",
2532 deny & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2533 deny & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2535 nfs4_show_superblock(s, file);
2536 seq_printf(s, ", ");
2537 nfs4_show_fname(s, file);
2538 seq_printf(s, ", ");
2539 nfs4_show_owner(s, oo);
2540 seq_printf(s, " }\n");
2542 spin_unlock(&nf->fi_lock);
2546 static int nfs4_show_lock(struct seq_file *s, struct nfs4_stid *st)
2548 struct nfs4_ol_stateid *ols;
2549 struct nfs4_file *nf;
2550 struct nfsd_file *file;
2551 struct nfs4_stateowner *oo;
2553 ols = openlockstateid(st);
2554 oo = ols->st_stateowner;
2556 spin_lock(&nf->fi_lock);
2557 file = find_any_file_locked(nf);
2561 seq_printf(s, "- ");
2562 nfs4_show_stateid(s, &st->sc_stateid);
2563 seq_printf(s, ": { type: lock, ");
2566 * Note: a lock stateid isn't really the same thing as a lock,
2567 * it's the locking state held by one owner on a file, and there
2568 * may be multiple (or no) lock ranges associated with it.
2569 * (Same for the matter is true of open stateids.)
2572 nfs4_show_superblock(s, file);
2573 /* XXX: open stateid? */
2574 seq_printf(s, ", ");
2575 nfs4_show_fname(s, file);
2576 seq_printf(s, ", ");
2577 nfs4_show_owner(s, oo);
2578 seq_printf(s, " }\n");
2580 spin_unlock(&nf->fi_lock);
2584 static int nfs4_show_deleg(struct seq_file *s, struct nfs4_stid *st)
2586 struct nfs4_delegation *ds;
2587 struct nfs4_file *nf;
2588 struct nfsd_file *file;
2590 ds = delegstateid(st);
2592 spin_lock(&nf->fi_lock);
2593 file = find_deleg_file_locked(nf);
2597 seq_printf(s, "- ");
2598 nfs4_show_stateid(s, &st->sc_stateid);
2599 seq_printf(s, ": { type: deleg, ");
2601 /* Kinda dead code as long as we only support read delegs: */
2602 seq_printf(s, "access: %s, ",
2603 ds->dl_type == NFS4_OPEN_DELEGATE_READ ? "r" : "w");
2605 /* XXX: lease time, whether it's being recalled. */
2607 nfs4_show_superblock(s, file);
2608 seq_printf(s, ", ");
2609 nfs4_show_fname(s, file);
2610 seq_printf(s, " }\n");
2612 spin_unlock(&nf->fi_lock);
2616 static int nfs4_show_layout(struct seq_file *s, struct nfs4_stid *st)
2618 struct nfs4_layout_stateid *ls;
2619 struct nfsd_file *file;
2621 ls = container_of(st, struct nfs4_layout_stateid, ls_stid);
2624 seq_printf(s, "- ");
2625 nfs4_show_stateid(s, &st->sc_stateid);
2626 seq_printf(s, ": { type: layout, ");
2628 /* XXX: What else would be useful? */
2630 nfs4_show_superblock(s, file);
2631 seq_printf(s, ", ");
2632 nfs4_show_fname(s, file);
2633 seq_printf(s, " }\n");
2638 static int states_show(struct seq_file *s, void *v)
2640 struct nfs4_stid *st = v;
2642 switch (st->sc_type) {
2643 case NFS4_OPEN_STID:
2644 return nfs4_show_open(s, st);
2645 case NFS4_LOCK_STID:
2646 return nfs4_show_lock(s, st);
2647 case NFS4_DELEG_STID:
2648 return nfs4_show_deleg(s, st);
2649 case NFS4_LAYOUT_STID:
2650 return nfs4_show_layout(s, st);
2652 return 0; /* XXX: or SEQ_SKIP? */
2654 /* XXX: copy stateids? */
2657 static struct seq_operations states_seq_ops = {
2658 .start = states_start,
2659 .next = states_next,
2660 .stop = states_stop,
2664 static int client_states_open(struct inode *inode, struct file *file)
2667 struct nfs4_client *clp;
2670 clp = get_nfsdfs_clp(inode);
2674 ret = seq_open(file, &states_seq_ops);
2677 s = file->private_data;
2682 static int client_opens_release(struct inode *inode, struct file *file)
2684 struct seq_file *m = file->private_data;
2685 struct nfs4_client *clp = m->private;
2687 /* XXX: alternatively, we could get/drop in seq start/stop */
2692 static const struct file_operations client_states_fops = {
2693 .open = client_states_open,
2695 .llseek = seq_lseek,
2696 .release = client_opens_release,
2700 * Normally we refuse to destroy clients that are in use, but here the
2701 * administrator is telling us to just do it. We also want to wait
2702 * so the caller has a guarantee that the client's locks are gone by
2703 * the time the write returns:
2705 static void force_expire_client(struct nfs4_client *clp)
2707 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2708 bool already_expired;
2710 trace_nfsd_clid_admin_expired(&clp->cl_clientid);
2712 spin_lock(&nn->client_lock);
2714 spin_unlock(&nn->client_lock);
2716 wait_event(expiry_wq, atomic_read(&clp->cl_rpc_users) == 0);
2717 spin_lock(&nn->client_lock);
2718 already_expired = list_empty(&clp->cl_lru);
2719 if (!already_expired)
2720 unhash_client_locked(clp);
2721 spin_unlock(&nn->client_lock);
2723 if (!already_expired)
2726 wait_event(expiry_wq, clp->cl_nfsd_dentry == NULL);
2729 static ssize_t client_ctl_write(struct file *file, const char __user *buf,
2730 size_t size, loff_t *pos)
2733 struct nfs4_client *clp;
2735 data = simple_transaction_get(file, buf, size);
2737 return PTR_ERR(data);
2738 if (size != 7 || 0 != memcmp(data, "expire\n", 7))
2740 clp = get_nfsdfs_clp(file_inode(file));
2743 force_expire_client(clp);
2748 static const struct file_operations client_ctl_fops = {
2749 .write = client_ctl_write,
2750 .release = simple_transaction_release,
2753 static const struct tree_descr client_files[] = {
2754 [0] = {"info", &client_info_fops, S_IRUSR},
2755 [1] = {"states", &client_states_fops, S_IRUSR},
2756 [2] = {"ctl", &client_ctl_fops, S_IWUSR},
2760 static struct nfs4_client *create_client(struct xdr_netobj name,
2761 struct svc_rqst *rqstp, nfs4_verifier *verf)
2763 struct nfs4_client *clp;
2764 struct sockaddr *sa = svc_addr(rqstp);
2766 struct net *net = SVC_NET(rqstp);
2767 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2768 struct dentry *dentries[ARRAY_SIZE(client_files)];
2770 clp = alloc_client(name);
2774 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
2780 kref_init(&clp->cl_nfsdfs.cl_ref);
2781 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
2782 clp->cl_time = ktime_get_boottime_seconds();
2783 clear_bit(0, &clp->cl_cb_slot_busy);
2784 copy_verf(clp, verf);
2785 memcpy(&clp->cl_addr, sa, sizeof(struct sockaddr_storage));
2786 clp->cl_cb_session = NULL;
2788 clp->cl_nfsd_dentry = nfsd_client_mkdir(
2789 nn, &clp->cl_nfsdfs,
2790 clp->cl_clientid.cl_id - nn->clientid_base,
2791 client_files, dentries);
2792 clp->cl_nfsd_info_dentry = dentries[0];
2793 if (!clp->cl_nfsd_dentry) {
2801 add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
2803 struct rb_node **new = &(root->rb_node), *parent = NULL;
2804 struct nfs4_client *clp;
2807 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
2810 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
2811 new = &((*new)->rb_left);
2813 new = &((*new)->rb_right);
2816 rb_link_node(&new_clp->cl_namenode, parent, new);
2817 rb_insert_color(&new_clp->cl_namenode, root);
2820 static struct nfs4_client *
2821 find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2824 struct rb_node *node = root->rb_node;
2825 struct nfs4_client *clp;
2828 clp = rb_entry(node, struct nfs4_client, cl_namenode);
2829 cmp = compare_blob(&clp->cl_name, name);
2831 node = node->rb_left;
2833 node = node->rb_right;
2841 add_to_unconfirmed(struct nfs4_client *clp)
2843 unsigned int idhashval;
2844 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2846 lockdep_assert_held(&nn->client_lock);
2848 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2849 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
2850 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
2851 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
2852 renew_client_locked(clp);
2856 move_to_confirmed(struct nfs4_client *clp)
2858 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
2859 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2861 lockdep_assert_held(&nn->client_lock);
2863 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
2864 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
2865 add_clp_to_name_tree(clp, &nn->conf_name_tree);
2866 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2867 trace_nfsd_clid_confirmed(&clp->cl_clientid);
2868 renew_client_locked(clp);
2871 static struct nfs4_client *
2872 find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
2874 struct nfs4_client *clp;
2875 unsigned int idhashval = clientid_hashval(clid->cl_id);
2877 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
2878 if (same_clid(&clp->cl_clientid, clid)) {
2879 if ((bool)clp->cl_minorversion != sessions)
2881 renew_client_locked(clp);
2888 static struct nfs4_client *
2889 find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2891 struct list_head *tbl = nn->conf_id_hashtbl;
2893 lockdep_assert_held(&nn->client_lock);
2894 return find_client_in_id_table(tbl, clid, sessions);
2897 static struct nfs4_client *
2898 find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2900 struct list_head *tbl = nn->unconf_id_hashtbl;
2902 lockdep_assert_held(&nn->client_lock);
2903 return find_client_in_id_table(tbl, clid, sessions);
2906 static bool clp_used_exchangeid(struct nfs4_client *clp)
2908 return clp->cl_exchange_flags != 0;
2911 static struct nfs4_client *
2912 find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
2914 lockdep_assert_held(&nn->client_lock);
2915 return find_clp_in_name_tree(name, &nn->conf_name_tree);
2918 static struct nfs4_client *
2919 find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
2921 lockdep_assert_held(&nn->client_lock);
2922 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
2926 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
2928 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
2929 struct sockaddr *sa = svc_addr(rqstp);
2930 u32 scopeid = rpc_get_scope_id(sa);
2931 unsigned short expected_family;
2933 /* Currently, we only support tcp and tcp6 for the callback channel */
2934 if (se->se_callback_netid_len == 3 &&
2935 !memcmp(se->se_callback_netid_val, "tcp", 3))
2936 expected_family = AF_INET;
2937 else if (se->se_callback_netid_len == 4 &&
2938 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2939 expected_family = AF_INET6;
2943 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
2944 se->se_callback_addr_len,
2945 (struct sockaddr *)&conn->cb_addr,
2946 sizeof(conn->cb_addr));
2948 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
2951 if (conn->cb_addr.ss_family == AF_INET6)
2952 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
2954 conn->cb_prog = se->se_callback_prog;
2955 conn->cb_ident = se->se_callback_ident;
2956 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
2957 trace_nfsd_cb_args(clp, conn);
2960 conn->cb_addr.ss_family = AF_UNSPEC;
2961 conn->cb_addrlen = 0;
2962 trace_nfsd_cb_nodelegs(clp);
2967 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
2970 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2972 struct xdr_buf *buf = resp->xdr->buf;
2973 struct nfsd4_slot *slot = resp->cstate.slot;
2976 dprintk("--> %s slot %p\n", __func__, slot);
2978 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
2979 slot->sl_opcnt = resp->opcnt;
2980 slot->sl_status = resp->cstate.status;
2981 free_svc_cred(&slot->sl_cred);
2982 copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred);
2984 if (!nfsd4_cache_this(resp)) {
2985 slot->sl_flags &= ~NFSD4_SLOT_CACHED;
2988 slot->sl_flags |= NFSD4_SLOT_CACHED;
2990 base = resp->cstate.data_offset;
2991 slot->sl_datalen = buf->len - base;
2992 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
2993 WARN(1, "%s: sessions DRC could not cache compound\n",
2999 * Encode the replay sequence operation from the slot values.
3000 * If cachethis is FALSE encode the uncached rep error on the next
3001 * operation which sets resp->p and increments resp->opcnt for
3002 * nfs4svc_encode_compoundres.
3006 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
3007 struct nfsd4_compoundres *resp)
3009 struct nfsd4_op *op;
3010 struct nfsd4_slot *slot = resp->cstate.slot;
3012 /* Encode the replayed sequence operation */
3013 op = &args->ops[resp->opcnt - 1];
3014 nfsd4_encode_operation(resp, op);
3016 if (slot->sl_flags & NFSD4_SLOT_CACHED)
3018 if (args->opcnt == 1) {
3020 * The original operation wasn't a solo sequence--we
3021 * always cache those--so this retry must not match the
3024 op->status = nfserr_seq_false_retry;
3026 op = &args->ops[resp->opcnt++];
3027 op->status = nfserr_retry_uncached_rep;
3028 nfsd4_encode_operation(resp, op);
3034 * The sequence operation is not cached because we can use the slot and
3038 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
3039 struct nfsd4_sequence *seq)
3041 struct nfsd4_slot *slot = resp->cstate.slot;
3042 struct xdr_stream *xdr = resp->xdr;
3046 dprintk("--> %s slot %p\n", __func__, slot);
3048 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
3052 p = xdr_reserve_space(xdr, slot->sl_datalen);
3055 return nfserr_serverfault;
3057 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
3058 xdr_commit_encode(xdr);
3060 resp->opcnt = slot->sl_opcnt;
3061 return slot->sl_status;
3065 * Set the exchange_id flags returned by the server.
3068 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
3070 #ifdef CONFIG_NFSD_PNFS
3071 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
3073 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
3076 /* Referrals are supported, Migration is not. */
3077 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
3079 /* set the wire flags to return to client. */
3080 clid->flags = new->cl_exchange_flags;
3083 static bool client_has_openowners(struct nfs4_client *clp)
3085 struct nfs4_openowner *oo;
3087 list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
3088 if (!list_empty(&oo->oo_owner.so_stateids))
3094 static bool client_has_state(struct nfs4_client *clp)
3096 return client_has_openowners(clp)
3097 #ifdef CONFIG_NFSD_PNFS
3098 || !list_empty(&clp->cl_lo_states)
3100 || !list_empty(&clp->cl_delegations)
3101 || !list_empty(&clp->cl_sessions)
3102 || !list_empty(&clp->async_copies);
3105 static __be32 copy_impl_id(struct nfs4_client *clp,
3106 struct nfsd4_exchange_id *exid)
3108 if (!exid->nii_domain.data)
3110 xdr_netobj_dup(&clp->cl_nii_domain, &exid->nii_domain, GFP_KERNEL);
3111 if (!clp->cl_nii_domain.data)
3112 return nfserr_jukebox;
3113 xdr_netobj_dup(&clp->cl_nii_name, &exid->nii_name, GFP_KERNEL);
3114 if (!clp->cl_nii_name.data)
3115 return nfserr_jukebox;
3116 clp->cl_nii_time = exid->nii_time;
3121 nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3122 union nfsd4_op_u *u)
3124 struct nfsd4_exchange_id *exid = &u->exchange_id;
3125 struct nfs4_client *conf, *new;
3126 struct nfs4_client *unconf = NULL;
3128 char addr_str[INET6_ADDRSTRLEN];
3129 nfs4_verifier verf = exid->verifier;
3130 struct sockaddr *sa = svc_addr(rqstp);
3131 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
3132 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3134 rpc_ntop(sa, addr_str, sizeof(addr_str));
3135 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
3136 "ip_addr=%s flags %x, spa_how %u\n",
3137 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
3138 addr_str, exid->flags, exid->spa_how);
3140 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
3141 return nfserr_inval;
3143 new = create_client(exid->clname, rqstp, &verf);
3145 return nfserr_jukebox;
3146 status = copy_impl_id(new, exid);
3150 switch (exid->spa_how) {
3152 exid->spo_must_enforce[0] = 0;
3153 exid->spo_must_enforce[1] = (
3154 1 << (OP_BIND_CONN_TO_SESSION - 32) |
3155 1 << (OP_EXCHANGE_ID - 32) |
3156 1 << (OP_CREATE_SESSION - 32) |
3157 1 << (OP_DESTROY_SESSION - 32) |
3158 1 << (OP_DESTROY_CLIENTID - 32));
3160 exid->spo_must_allow[0] &= (1 << (OP_CLOSE) |
3161 1 << (OP_OPEN_DOWNGRADE) |
3163 1 << (OP_DELEGRETURN));
3165 exid->spo_must_allow[1] &= (
3166 1 << (OP_TEST_STATEID - 32) |
3167 1 << (OP_FREE_STATEID - 32));
3168 if (!svc_rqst_integrity_protected(rqstp)) {
3169 status = nfserr_inval;
3173 * Sometimes userspace doesn't give us a principal.
3174 * Which is a bug, really. Anyway, we can't enforce
3175 * MACH_CRED in that case, better to give up now:
3177 if (!new->cl_cred.cr_principal &&
3178 !new->cl_cred.cr_raw_principal) {
3179 status = nfserr_serverfault;
3182 new->cl_mach_cred = true;
3186 default: /* checked by xdr code */
3190 status = nfserr_encr_alg_unsupp;
3194 /* Cases below refer to rfc 5661 section 18.35.4: */
3195 spin_lock(&nn->client_lock);
3196 conf = find_confirmed_client_by_name(&exid->clname, nn);
3198 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
3199 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
3202 if (!clp_used_exchangeid(conf)) { /* buggy client */
3203 status = nfserr_inval;
3206 if (!nfsd4_mach_creds_match(conf, rqstp)) {
3207 status = nfserr_wrong_cred;
3210 if (!creds_match) { /* case 9 */
3211 status = nfserr_perm;
3214 if (!verfs_match) { /* case 8 */
3215 status = nfserr_not_same;
3219 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
3220 trace_nfsd_clid_confirmed_r(conf);
3223 if (!creds_match) { /* case 3 */
3224 if (client_has_state(conf)) {
3225 status = nfserr_clid_inuse;
3226 trace_nfsd_clid_cred_mismatch(conf, rqstp);
3231 if (verfs_match) { /* case 2 */
3232 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
3233 trace_nfsd_clid_confirmed_r(conf);
3236 /* case 5, client reboot */
3237 trace_nfsd_clid_verf_mismatch(conf, rqstp, &verf);
3242 if (update) { /* case 7 */
3243 status = nfserr_noent;
3247 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
3248 if (unconf) /* case 4, possible retry or client restart */
3249 unhash_client_locked(unconf);
3251 /* case 1, new owner ID */
3252 trace_nfsd_clid_fresh(new);
3256 status = mark_client_expired_locked(conf);
3259 trace_nfsd_clid_replaced(&conf->cl_clientid);
3261 new->cl_minorversion = cstate->minorversion;
3262 new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0];
3263 new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1];
3265 add_to_unconfirmed(new);
3268 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
3269 exid->clientid.cl_id = conf->cl_clientid.cl_id;
3271 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
3272 nfsd4_set_ex_flags(conf, exid);
3274 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
3275 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
3279 spin_unlock(&nn->client_lock);
3284 trace_nfsd_clid_expire_unconf(&unconf->cl_clientid);
3285 expire_client(unconf);
3291 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
3293 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
3296 /* The slot is in use, and no response has been sent. */
3298 if (seqid == slot_seqid)
3299 return nfserr_jukebox;
3301 return nfserr_seq_misordered;
3303 /* Note unsigned 32-bit arithmetic handles wraparound: */
3304 if (likely(seqid == slot_seqid + 1))
3306 if (seqid == slot_seqid)
3307 return nfserr_replay_cache;
3308 return nfserr_seq_misordered;
3312 * Cache the create session result into the create session single DRC
3313 * slot cache by saving the xdr structure. sl_seqid has been set.
3314 * Do this for solo or embedded create session operations.
3317 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
3318 struct nfsd4_clid_slot *slot, __be32 nfserr)
3320 slot->sl_status = nfserr;
3321 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
3325 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
3326 struct nfsd4_clid_slot *slot)
3328 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
3329 return slot->sl_status;
3332 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
3333 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
3334 1 + /* MIN tag is length with zero, only length */ \
3335 3 + /* version, opcount, opcode */ \
3336 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3337 /* seqid, slotID, slotID, cache */ \
3338 4 ) * sizeof(__be32))
3340 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
3341 2 + /* verifier: AUTH_NULL, length 0 */\
3343 1 + /* MIN tag is length with zero, only length */ \
3344 3 + /* opcount, opcode, opstatus*/ \
3345 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3346 /* seqid, slotID, slotID, slotID, status */ \
3347 5 ) * sizeof(__be32))
3349 static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
3351 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
3353 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
3354 return nfserr_toosmall;
3355 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
3356 return nfserr_toosmall;
3357 ca->headerpadsz = 0;
3358 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
3359 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
3360 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
3361 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
3362 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
3363 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
3365 * Note decreasing slot size below client's request may make it
3366 * difficult for client to function correctly, whereas
3367 * decreasing the number of slots will (just?) affect
3368 * performance. When short on memory we therefore prefer to
3369 * decrease number of slots instead of their size. Clients that
3370 * request larger slots than they need will get poor results:
3371 * Note that we always allow at least one slot, because our
3372 * accounting is soft and provides no guarantees either way.
3374 ca->maxreqs = nfsd4_get_drc_mem(ca, nn);
3380 * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
3381 * These are based on similar macros in linux/sunrpc/msg_prot.h .
3383 #define RPC_MAX_HEADER_WITH_AUTH_SYS \
3384 (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
3386 #define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
3387 (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
3389 #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
3390 RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
3391 #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
3392 RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
3395 static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
3397 ca->headerpadsz = 0;
3399 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
3400 return nfserr_toosmall;
3401 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
3402 return nfserr_toosmall;
3403 ca->maxresp_cached = 0;
3405 return nfserr_toosmall;
3410 static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
3412 switch (cbs->flavor) {
3418 * GSS case: the spec doesn't allow us to return this
3419 * error. But it also doesn't allow us not to support
3421 * I'd rather this fail hard than return some error the
3422 * client might think it can already handle:
3424 return nfserr_encr_alg_unsupp;
3429 nfsd4_create_session(struct svc_rqst *rqstp,
3430 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
3432 struct nfsd4_create_session *cr_ses = &u->create_session;
3433 struct sockaddr *sa = svc_addr(rqstp);
3434 struct nfs4_client *conf, *unconf;
3435 struct nfs4_client *old = NULL;
3436 struct nfsd4_session *new;
3437 struct nfsd4_conn *conn;
3438 struct nfsd4_clid_slot *cs_slot = NULL;
3440 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3442 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
3443 return nfserr_inval;
3444 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
3447 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
3450 status = check_backchannel_attrs(&cr_ses->back_channel);
3452 goto out_release_drc_mem;
3453 status = nfserr_jukebox;
3454 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
3456 goto out_release_drc_mem;
3457 conn = alloc_conn_from_crses(rqstp, cr_ses);
3459 goto out_free_session;
3461 spin_lock(&nn->client_lock);
3462 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
3463 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
3464 WARN_ON_ONCE(conf && unconf);
3467 status = nfserr_wrong_cred;
3468 if (!nfsd4_mach_creds_match(conf, rqstp))
3470 cs_slot = &conf->cl_cs_slot;
3471 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
3473 if (status == nfserr_replay_cache)
3474 status = nfsd4_replay_create_session(cr_ses, cs_slot);
3477 } else if (unconf) {
3478 status = nfserr_clid_inuse;
3479 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
3480 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
3481 trace_nfsd_clid_cred_mismatch(unconf, rqstp);
3484 status = nfserr_wrong_cred;
3485 if (!nfsd4_mach_creds_match(unconf, rqstp))
3487 cs_slot = &unconf->cl_cs_slot;
3488 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
3490 /* an unconfirmed replay returns misordered */
3491 status = nfserr_seq_misordered;
3494 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3496 status = mark_client_expired_locked(old);
3501 trace_nfsd_clid_replaced(&old->cl_clientid);
3503 move_to_confirmed(unconf);
3506 status = nfserr_stale_clientid;
3510 /* Persistent sessions are not supported */
3511 cr_ses->flags &= ~SESSION4_PERSIST;
3512 /* Upshifting from TCP to RDMA is not supported */
3513 cr_ses->flags &= ~SESSION4_RDMA;
3515 init_session(rqstp, new, conf, cr_ses);
3516 nfsd4_get_session_locked(new);
3518 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
3519 NFS4_MAX_SESSIONID_LEN);
3520 cs_slot->sl_seqid++;
3521 cr_ses->seqid = cs_slot->sl_seqid;
3523 /* cache solo and embedded create sessions under the client_lock */
3524 nfsd4_cache_create_session(cr_ses, cs_slot, status);
3525 spin_unlock(&nn->client_lock);
3527 fsnotify_dentry(conf->cl_nfsd_info_dentry, FS_MODIFY);
3528 /* init connection and backchannel */
3529 nfsd4_init_conn(rqstp, conn, new);
3530 nfsd4_put_session(new);
3535 spin_unlock(&nn->client_lock);
3540 __free_session(new);
3541 out_release_drc_mem:
3542 nfsd4_put_drc_mem(&cr_ses->fore_channel);
3546 static __be32 nfsd4_map_bcts_dir(u32 *dir)
3549 case NFS4_CDFC4_FORE:
3550 case NFS4_CDFC4_BACK:
3552 case NFS4_CDFC4_FORE_OR_BOTH:
3553 case NFS4_CDFC4_BACK_OR_BOTH:
3554 *dir = NFS4_CDFC4_BOTH;
3557 return nfserr_inval;
3560 __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp,
3561 struct nfsd4_compound_state *cstate,
3562 union nfsd4_op_u *u)
3564 struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl;
3565 struct nfsd4_session *session = cstate->session;
3566 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3569 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
3572 spin_lock(&nn->client_lock);
3573 session->se_cb_prog = bc->bc_cb_program;
3574 session->se_cb_sec = bc->bc_cb_sec;
3575 spin_unlock(&nn->client_lock);
3577 nfsd4_probe_callback(session->se_client);
3582 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
3584 struct nfsd4_conn *c;
3586 list_for_each_entry(c, &s->se_conns, cn_persession) {
3587 if (c->cn_xprt == xpt) {
3594 static __be32 nfsd4_match_existing_connection(struct svc_rqst *rqst,
3595 struct nfsd4_session *session, u32 req, struct nfsd4_conn **conn)
3597 struct nfs4_client *clp = session->se_client;
3598 struct svc_xprt *xpt = rqst->rq_xprt;
3599 struct nfsd4_conn *c;
3602 /* Following the last paragraph of RFC 5661 Section 18.34.3: */
3603 spin_lock(&clp->cl_lock);
3604 c = __nfsd4_find_conn(xpt, session);
3606 status = nfserr_noent;
3607 else if (req == c->cn_flags)
3609 else if (req == NFS4_CDFC4_FORE_OR_BOTH &&
3610 c->cn_flags != NFS4_CDFC4_BACK)
3612 else if (req == NFS4_CDFC4_BACK_OR_BOTH &&
3613 c->cn_flags != NFS4_CDFC4_FORE)
3616 status = nfserr_inval;
3617 spin_unlock(&clp->cl_lock);
3618 if (status == nfs_ok && conn)
3623 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
3624 struct nfsd4_compound_state *cstate,
3625 union nfsd4_op_u *u)
3627 struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session;
3629 struct nfsd4_conn *conn;
3630 struct nfsd4_session *session;
3631 struct net *net = SVC_NET(rqstp);
3632 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3634 if (!nfsd4_last_compound_op(rqstp))
3635 return nfserr_not_only_op;
3636 spin_lock(&nn->client_lock);
3637 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
3638 spin_unlock(&nn->client_lock);
3640 goto out_no_session;
3641 status = nfserr_wrong_cred;
3642 if (!nfsd4_mach_creds_match(session->se_client, rqstp))
3644 status = nfsd4_match_existing_connection(rqstp, session,
3646 if (status == nfs_ok) {
3647 if (bcts->dir == NFS4_CDFC4_FORE_OR_BOTH ||
3648 bcts->dir == NFS4_CDFC4_BACK)
3649 conn->cn_flags |= NFS4_CDFC4_BACK;
3650 nfsd4_probe_callback(session->se_client);
3653 if (status == nfserr_inval)
3655 status = nfsd4_map_bcts_dir(&bcts->dir);
3658 conn = alloc_conn(rqstp, bcts->dir);
3659 status = nfserr_jukebox;
3662 nfsd4_init_conn(rqstp, conn, session);
3665 nfsd4_put_session(session);
3670 static bool nfsd4_compound_in_session(struct nfsd4_compound_state *cstate, struct nfs4_sessionid *sid)
3672 if (!cstate->session)
3674 return !memcmp(sid, &cstate->session->se_sessionid, sizeof(*sid));
3678 nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate,
3679 union nfsd4_op_u *u)
3681 struct nfs4_sessionid *sessionid = &u->destroy_session.sessionid;
3682 struct nfsd4_session *ses;
3684 int ref_held_by_me = 0;
3685 struct net *net = SVC_NET(r);
3686 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3688 status = nfserr_not_only_op;
3689 if (nfsd4_compound_in_session(cstate, sessionid)) {
3690 if (!nfsd4_last_compound_op(r))
3694 dump_sessionid(__func__, sessionid);
3695 spin_lock(&nn->client_lock);
3696 ses = find_in_sessionid_hashtbl(sessionid, net, &status);
3698 goto out_client_lock;
3699 status = nfserr_wrong_cred;
3700 if (!nfsd4_mach_creds_match(ses->se_client, r))
3701 goto out_put_session;
3702 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
3704 goto out_put_session;
3705 unhash_session(ses);
3706 spin_unlock(&nn->client_lock);
3708 nfsd4_probe_callback_sync(ses->se_client);
3710 spin_lock(&nn->client_lock);
3713 nfsd4_put_session_locked(ses);
3715 spin_unlock(&nn->client_lock);
3720 static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
3722 struct nfs4_client *clp = ses->se_client;
3723 struct nfsd4_conn *c;
3724 __be32 status = nfs_ok;
3727 spin_lock(&clp->cl_lock);
3728 c = __nfsd4_find_conn(new->cn_xprt, ses);
3731 status = nfserr_conn_not_bound_to_session;
3732 if (clp->cl_mach_cred)
3734 __nfsd4_hash_conn(new, ses);
3735 spin_unlock(&clp->cl_lock);
3736 ret = nfsd4_register_conn(new);
3738 /* oops; xprt is already down: */
3739 nfsd4_conn_lost(&new->cn_xpt_user);
3742 spin_unlock(&clp->cl_lock);
3747 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
3749 struct nfsd4_compoundargs *args = rqstp->rq_argp;
3751 return args->opcnt > session->se_fchannel.maxops;
3754 static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
3755 struct nfsd4_session *session)
3757 struct xdr_buf *xb = &rqstp->rq_arg;
3759 return xb->len > session->se_fchannel.maxreq_sz;
3762 static bool replay_matches_cache(struct svc_rqst *rqstp,
3763 struct nfsd4_sequence *seq, struct nfsd4_slot *slot)
3765 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
3767 if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) !=
3768 (bool)seq->cachethis)
3771 * If there's an error then the reply can have fewer ops than
3774 if (slot->sl_opcnt < argp->opcnt && !slot->sl_status)
3777 * But if we cached a reply with *more* ops than the call you're
3778 * sending us now, then this new call is clearly not really a
3779 * replay of the old one:
3781 if (slot->sl_opcnt > argp->opcnt)
3783 /* This is the only check explicitly called by spec: */
3784 if (!same_creds(&rqstp->rq_cred, &slot->sl_cred))
3787 * There may be more comparisons we could actually do, but the
3788 * spec doesn't require us to catch every case where the calls
3789 * don't match (that would require caching the call as well as
3790 * the reply), so we don't bother.
3796 nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3797 union nfsd4_op_u *u)
3799 struct nfsd4_sequence *seq = &u->sequence;
3800 struct nfsd4_compoundres *resp = rqstp->rq_resp;
3801 struct xdr_stream *xdr = resp->xdr;
3802 struct nfsd4_session *session;
3803 struct nfs4_client *clp;
3804 struct nfsd4_slot *slot;
3805 struct nfsd4_conn *conn;
3808 struct net *net = SVC_NET(rqstp);
3809 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3811 if (resp->opcnt != 1)
3812 return nfserr_sequence_pos;
3815 * Will be either used or freed by nfsd4_sequence_check_conn
3818 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
3820 return nfserr_jukebox;
3822 spin_lock(&nn->client_lock);
3823 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
3825 goto out_no_session;
3826 clp = session->se_client;
3828 status = nfserr_too_many_ops;
3829 if (nfsd4_session_too_many_ops(rqstp, session))
3830 goto out_put_session;
3832 status = nfserr_req_too_big;
3833 if (nfsd4_request_too_big(rqstp, session))
3834 goto out_put_session;
3836 status = nfserr_badslot;
3837 if (seq->slotid >= session->se_fchannel.maxreqs)
3838 goto out_put_session;
3840 slot = session->se_slots[seq->slotid];
3841 dprintk("%s: slotid %d\n", __func__, seq->slotid);
3843 /* We do not negotiate the number of slots yet, so set the
3844 * maxslots to the session maxreqs which is used to encode
3845 * sr_highest_slotid and the sr_target_slot id to maxslots */
3846 seq->maxslots = session->se_fchannel.maxreqs;
3848 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
3849 slot->sl_flags & NFSD4_SLOT_INUSE);
3850 if (status == nfserr_replay_cache) {
3851 status = nfserr_seq_misordered;
3852 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
3853 goto out_put_session;
3854 status = nfserr_seq_false_retry;
3855 if (!replay_matches_cache(rqstp, seq, slot))
3856 goto out_put_session;
3857 cstate->slot = slot;
3858 cstate->session = session;
3860 /* Return the cached reply status and set cstate->status
3861 * for nfsd4_proc_compound processing */
3862 status = nfsd4_replay_cache_entry(resp, seq);
3863 cstate->status = nfserr_replay_cache;
3867 goto out_put_session;
3869 status = nfsd4_sequence_check_conn(conn, session);
3872 goto out_put_session;
3874 buflen = (seq->cachethis) ?
3875 session->se_fchannel.maxresp_cached :
3876 session->se_fchannel.maxresp_sz;
3877 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
3879 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
3880 goto out_put_session;
3881 svc_reserve(rqstp, buflen);
3884 /* Success! bump slot seqid */
3885 slot->sl_seqid = seq->seqid;
3886 slot->sl_flags |= NFSD4_SLOT_INUSE;
3888 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
3890 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
3892 cstate->slot = slot;
3893 cstate->session = session;
3897 switch (clp->cl_cb_state) {
3899 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
3901 case NFSD4_CB_FAULT:
3902 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
3905 seq->status_flags = 0;
3907 if (!list_empty(&clp->cl_revoked))
3908 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
3912 spin_unlock(&nn->client_lock);
3915 nfsd4_put_session_locked(session);
3916 goto out_no_session;
3920 nfsd4_sequence_done(struct nfsd4_compoundres *resp)
3922 struct nfsd4_compound_state *cs = &resp->cstate;
3924 if (nfsd4_has_session(cs)) {
3925 if (cs->status != nfserr_replay_cache) {
3926 nfsd4_store_cache_entry(resp);
3927 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
3929 /* Drop session reference that was taken in nfsd4_sequence() */
3930 nfsd4_put_session(cs->session);
3932 put_client_renew(cs->clp);
3936 nfsd4_destroy_clientid(struct svc_rqst *rqstp,
3937 struct nfsd4_compound_state *cstate,
3938 union nfsd4_op_u *u)
3940 struct nfsd4_destroy_clientid *dc = &u->destroy_clientid;
3941 struct nfs4_client *conf, *unconf;
3942 struct nfs4_client *clp = NULL;
3944 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3946 spin_lock(&nn->client_lock);
3947 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
3948 conf = find_confirmed_client(&dc->clientid, true, nn);
3949 WARN_ON_ONCE(conf && unconf);
3952 if (client_has_state(conf)) {
3953 status = nfserr_clientid_busy;
3956 status = mark_client_expired_locked(conf);
3963 status = nfserr_stale_clientid;
3966 if (!nfsd4_mach_creds_match(clp, rqstp)) {
3968 status = nfserr_wrong_cred;
3971 trace_nfsd_clid_destroyed(&clp->cl_clientid);
3972 unhash_client_locked(clp);
3974 spin_unlock(&nn->client_lock);
3981 nfsd4_reclaim_complete(struct svc_rqst *rqstp,
3982 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
3984 struct nfsd4_reclaim_complete *rc = &u->reclaim_complete;
3985 struct nfs4_client *clp = cstate->clp;
3988 if (rc->rca_one_fs) {
3989 if (!cstate->current_fh.fh_dentry)
3990 return nfserr_nofilehandle;
3992 * We don't take advantage of the rca_one_fs case.
3993 * That's OK, it's optional, we can safely ignore it.
3998 status = nfserr_complete_already;
3999 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &clp->cl_flags))
4002 status = nfserr_stale_clientid;
4003 if (is_client_expired(clp))
4005 * The following error isn't really legal.
4006 * But we only get here if the client just explicitly
4007 * destroyed the client. Surely it no longer cares what
4008 * error it gets back on an operation for the dead
4014 trace_nfsd_clid_reclaim_complete(&clp->cl_clientid);
4015 nfsd4_client_record_create(clp);
4016 inc_reclaim_complete(clp);
4022 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4023 union nfsd4_op_u *u)
4025 struct nfsd4_setclientid *setclid = &u->setclientid;
4026 struct xdr_netobj clname = setclid->se_name;
4027 nfs4_verifier clverifier = setclid->se_verf;
4028 struct nfs4_client *conf, *new;
4029 struct nfs4_client *unconf = NULL;
4031 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4033 new = create_client(clname, rqstp, &clverifier);
4035 return nfserr_jukebox;
4036 spin_lock(&nn->client_lock);
4037 conf = find_confirmed_client_by_name(&clname, nn);
4038 if (conf && client_has_state(conf)) {
4039 status = nfserr_clid_inuse;
4040 if (clp_used_exchangeid(conf))
4042 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
4043 trace_nfsd_clid_cred_mismatch(conf, rqstp);
4047 unconf = find_unconfirmed_client_by_name(&clname, nn);
4049 unhash_client_locked(unconf);
4051 if (same_verf(&conf->cl_verifier, &clverifier)) {
4052 copy_clid(new, conf);
4053 gen_confirm(new, nn);
4055 trace_nfsd_clid_verf_mismatch(conf, rqstp,
4058 trace_nfsd_clid_fresh(new);
4059 new->cl_minorversion = 0;
4060 gen_callback(new, setclid, rqstp);
4061 add_to_unconfirmed(new);
4062 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
4063 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
4064 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
4068 spin_unlock(&nn->client_lock);
4072 trace_nfsd_clid_expire_unconf(&unconf->cl_clientid);
4073 expire_client(unconf);
4079 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
4080 struct nfsd4_compound_state *cstate,
4081 union nfsd4_op_u *u)
4083 struct nfsd4_setclientid_confirm *setclientid_confirm =
4084 &u->setclientid_confirm;
4085 struct nfs4_client *conf, *unconf;
4086 struct nfs4_client *old = NULL;
4087 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
4088 clientid_t * clid = &setclientid_confirm->sc_clientid;
4090 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4092 if (STALE_CLIENTID(clid, nn))
4093 return nfserr_stale_clientid;
4095 spin_lock(&nn->client_lock);
4096 conf = find_confirmed_client(clid, false, nn);
4097 unconf = find_unconfirmed_client(clid, false, nn);
4099 * We try hard to give out unique clientid's, so if we get an
4100 * attempt to confirm the same clientid with a different cred,
4101 * the client may be buggy; this should never happen.
4103 * Nevertheless, RFC 7530 recommends INUSE for this case:
4105 status = nfserr_clid_inuse;
4106 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
4107 trace_nfsd_clid_cred_mismatch(unconf, rqstp);
4110 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
4111 trace_nfsd_clid_cred_mismatch(conf, rqstp);
4114 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
4115 if (conf && same_verf(&confirm, &conf->cl_confirm)) {
4118 status = nfserr_stale_clientid;
4124 unhash_client_locked(old);
4125 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
4127 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
4129 status = nfserr_clid_inuse;
4130 if (client_has_state(old)
4131 && !same_creds(&unconf->cl_cred,
4136 status = mark_client_expired_locked(old);
4141 trace_nfsd_clid_replaced(&old->cl_clientid);
4143 move_to_confirmed(unconf);
4146 get_client_locked(conf);
4147 spin_unlock(&nn->client_lock);
4149 fsnotify_dentry(conf->cl_nfsd_info_dentry, FS_MODIFY);
4150 nfsd4_probe_callback(conf);
4151 spin_lock(&nn->client_lock);
4152 put_client_renew_locked(conf);
4154 spin_unlock(&nn->client_lock);
4160 static struct nfs4_file *nfsd4_alloc_file(void)
4162 return kmem_cache_alloc(file_slab, GFP_KERNEL);
4165 /* OPEN Share state helper functions */
4166 static void nfsd4_init_file(struct svc_fh *fh, unsigned int hashval,
4167 struct nfs4_file *fp)
4169 lockdep_assert_held(&state_lock);
4171 refcount_set(&fp->fi_ref, 1);
4172 spin_lock_init(&fp->fi_lock);
4173 INIT_LIST_HEAD(&fp->fi_stateids);
4174 INIT_LIST_HEAD(&fp->fi_delegations);
4175 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
4176 fh_copy_shallow(&fp->fi_fhandle, &fh->fh_handle);
4177 fp->fi_deleg_file = NULL;
4178 fp->fi_had_conflict = false;
4179 fp->fi_share_deny = 0;
4180 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
4181 memset(fp->fi_access, 0, sizeof(fp->fi_access));
4182 fp->fi_aliased = false;
4183 fp->fi_inode = d_inode(fh->fh_dentry);
4184 #ifdef CONFIG_NFSD_PNFS
4185 INIT_LIST_HEAD(&fp->fi_lo_states);
4186 atomic_set(&fp->fi_lo_recalls, 0);
4188 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
4192 nfsd4_free_slabs(void)
4194 kmem_cache_destroy(client_slab);
4195 kmem_cache_destroy(openowner_slab);
4196 kmem_cache_destroy(lockowner_slab);
4197 kmem_cache_destroy(file_slab);
4198 kmem_cache_destroy(stateid_slab);
4199 kmem_cache_destroy(deleg_slab);
4200 kmem_cache_destroy(odstate_slab);
4204 nfsd4_init_slabs(void)
4206 client_slab = kmem_cache_create("nfsd4_clients",
4207 sizeof(struct nfs4_client), 0, 0, NULL);
4208 if (client_slab == NULL)
4210 openowner_slab = kmem_cache_create("nfsd4_openowners",
4211 sizeof(struct nfs4_openowner), 0, 0, NULL);
4212 if (openowner_slab == NULL)
4213 goto out_free_client_slab;
4214 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
4215 sizeof(struct nfs4_lockowner), 0, 0, NULL);
4216 if (lockowner_slab == NULL)
4217 goto out_free_openowner_slab;
4218 file_slab = kmem_cache_create("nfsd4_files",
4219 sizeof(struct nfs4_file), 0, 0, NULL);
4220 if (file_slab == NULL)
4221 goto out_free_lockowner_slab;
4222 stateid_slab = kmem_cache_create("nfsd4_stateids",
4223 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
4224 if (stateid_slab == NULL)
4225 goto out_free_file_slab;
4226 deleg_slab = kmem_cache_create("nfsd4_delegations",
4227 sizeof(struct nfs4_delegation), 0, 0, NULL);
4228 if (deleg_slab == NULL)
4229 goto out_free_stateid_slab;
4230 odstate_slab = kmem_cache_create("nfsd4_odstate",
4231 sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
4232 if (odstate_slab == NULL)
4233 goto out_free_deleg_slab;
4236 out_free_deleg_slab:
4237 kmem_cache_destroy(deleg_slab);
4238 out_free_stateid_slab:
4239 kmem_cache_destroy(stateid_slab);
4241 kmem_cache_destroy(file_slab);
4242 out_free_lockowner_slab:
4243 kmem_cache_destroy(lockowner_slab);
4244 out_free_openowner_slab:
4245 kmem_cache_destroy(openowner_slab);
4246 out_free_client_slab:
4247 kmem_cache_destroy(client_slab);
4252 static void init_nfs4_replay(struct nfs4_replay *rp)
4254 rp->rp_status = nfserr_serverfault;
4256 rp->rp_buf = rp->rp_ibuf;
4257 mutex_init(&rp->rp_mutex);
4260 static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
4261 struct nfs4_stateowner *so)
4263 if (!nfsd4_has_session(cstate)) {
4264 mutex_lock(&so->so_replay.rp_mutex);
4265 cstate->replay_owner = nfs4_get_stateowner(so);
4269 void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
4271 struct nfs4_stateowner *so = cstate->replay_owner;
4274 cstate->replay_owner = NULL;
4275 mutex_unlock(&so->so_replay.rp_mutex);
4276 nfs4_put_stateowner(so);
4280 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
4282 struct nfs4_stateowner *sop;
4284 sop = kmem_cache_alloc(slab, GFP_KERNEL);
4288 xdr_netobj_dup(&sop->so_owner, owner, GFP_KERNEL);
4289 if (!sop->so_owner.data) {
4290 kmem_cache_free(slab, sop);
4294 INIT_LIST_HEAD(&sop->so_stateids);
4295 sop->so_client = clp;
4296 init_nfs4_replay(&sop->so_replay);
4297 atomic_set(&sop->so_count, 1);
4301 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
4303 lockdep_assert_held(&clp->cl_lock);
4305 list_add(&oo->oo_owner.so_strhash,
4306 &clp->cl_ownerstr_hashtbl[strhashval]);
4307 list_add(&oo->oo_perclient, &clp->cl_openowners);
4310 static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
4312 unhash_openowner_locked(openowner(so));
4315 static void nfs4_free_openowner(struct nfs4_stateowner *so)
4317 struct nfs4_openowner *oo = openowner(so);
4319 kmem_cache_free(openowner_slab, oo);
4322 static const struct nfs4_stateowner_operations openowner_ops = {
4323 .so_unhash = nfs4_unhash_openowner,
4324 .so_free = nfs4_free_openowner,
4327 static struct nfs4_ol_stateid *
4328 nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4330 struct nfs4_ol_stateid *local, *ret = NULL;
4331 struct nfs4_openowner *oo = open->op_openowner;
4333 lockdep_assert_held(&fp->fi_lock);
4335 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
4336 /* ignore lock owners */
4337 if (local->st_stateowner->so_is_open_owner == 0)
4339 if (local->st_stateowner != &oo->oo_owner)
4341 if (local->st_stid.sc_type == NFS4_OPEN_STID) {
4343 refcount_inc(&ret->st_stid.sc_count);
4351 nfsd4_verify_open_stid(struct nfs4_stid *s)
4353 __be32 ret = nfs_ok;
4355 switch (s->sc_type) {
4359 case NFS4_CLOSED_STID:
4360 case NFS4_CLOSED_DELEG_STID:
4361 ret = nfserr_bad_stateid;
4363 case NFS4_REVOKED_DELEG_STID:
4364 ret = nfserr_deleg_revoked;
4369 /* Lock the stateid st_mutex, and deal with races with CLOSE */
4371 nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp)
4375 mutex_lock_nested(&stp->st_mutex, LOCK_STATEID_MUTEX);
4376 ret = nfsd4_verify_open_stid(&stp->st_stid);
4378 mutex_unlock(&stp->st_mutex);
4382 static struct nfs4_ol_stateid *
4383 nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4385 struct nfs4_ol_stateid *stp;
4387 spin_lock(&fp->fi_lock);
4388 stp = nfsd4_find_existing_open(fp, open);
4389 spin_unlock(&fp->fi_lock);
4390 if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok)
4392 nfs4_put_stid(&stp->st_stid);
4397 static struct nfs4_openowner *
4398 alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
4399 struct nfsd4_compound_state *cstate)
4401 struct nfs4_client *clp = cstate->clp;
4402 struct nfs4_openowner *oo, *ret;
4404 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
4407 oo->oo_owner.so_ops = &openowner_ops;
4408 oo->oo_owner.so_is_open_owner = 1;
4409 oo->oo_owner.so_seqid = open->op_seqid;
4411 if (nfsd4_has_session(cstate))
4412 oo->oo_flags |= NFS4_OO_CONFIRMED;
4414 oo->oo_last_closed_stid = NULL;
4415 INIT_LIST_HEAD(&oo->oo_close_lru);
4416 spin_lock(&clp->cl_lock);
4417 ret = find_openstateowner_str_locked(strhashval, open, clp);
4419 hash_openowner(oo, clp, strhashval);
4422 nfs4_free_stateowner(&oo->oo_owner);
4424 spin_unlock(&clp->cl_lock);
4428 static struct nfs4_ol_stateid *
4429 init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
4432 struct nfs4_openowner *oo = open->op_openowner;
4433 struct nfs4_ol_stateid *retstp = NULL;
4434 struct nfs4_ol_stateid *stp;
4437 /* We are moving these outside of the spinlocks to avoid the warnings */
4438 mutex_init(&stp->st_mutex);
4439 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
4442 spin_lock(&oo->oo_owner.so_client->cl_lock);
4443 spin_lock(&fp->fi_lock);
4445 retstp = nfsd4_find_existing_open(fp, open);
4449 open->op_stp = NULL;
4450 refcount_inc(&stp->st_stid.sc_count);
4451 stp->st_stid.sc_type = NFS4_OPEN_STID;
4452 INIT_LIST_HEAD(&stp->st_locks);
4453 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
4455 stp->st_stid.sc_file = fp;
4456 stp->st_access_bmap = 0;
4457 stp->st_deny_bmap = 0;
4458 stp->st_openstp = NULL;
4459 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
4460 list_add(&stp->st_perfile, &fp->fi_stateids);
4463 spin_unlock(&fp->fi_lock);
4464 spin_unlock(&oo->oo_owner.so_client->cl_lock);
4466 /* Handle races with CLOSE */
4467 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
4468 nfs4_put_stid(&retstp->st_stid);
4471 /* To keep mutex tracking happy */
4472 mutex_unlock(&stp->st_mutex);
4479 * In the 4.0 case we need to keep the owners around a little while to handle
4480 * CLOSE replay. We still do need to release any file access that is held by
4481 * them before returning however.
4484 move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
4486 struct nfs4_ol_stateid *last;
4487 struct nfs4_openowner *oo = openowner(s->st_stateowner);
4488 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
4491 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
4494 * We know that we hold one reference via nfsd4_close, and another
4495 * "persistent" reference for the client. If the refcount is higher
4496 * than 2, then there are still calls in progress that are using this
4497 * stateid. We can't put the sc_file reference until they are finished.
4498 * Wait for the refcount to drop to 2. Since it has been unhashed,
4499 * there should be no danger of the refcount going back up again at
4502 wait_event(close_wq, refcount_read(&s->st_stid.sc_count) == 2);
4504 release_all_access(s);
4505 if (s->st_stid.sc_file) {
4506 put_nfs4_file(s->st_stid.sc_file);
4507 s->st_stid.sc_file = NULL;
4510 spin_lock(&nn->client_lock);
4511 last = oo->oo_last_closed_stid;
4512 oo->oo_last_closed_stid = s;
4513 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
4514 oo->oo_time = ktime_get_boottime_seconds();
4515 spin_unlock(&nn->client_lock);
4517 nfs4_put_stid(&last->st_stid);
4520 /* search file_hashtbl[] for file */
4521 static struct nfs4_file *
4522 find_file_locked(struct svc_fh *fh, unsigned int hashval)
4524 struct nfs4_file *fp;
4526 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash,
4527 lockdep_is_held(&state_lock)) {
4528 if (fh_match(&fp->fi_fhandle, &fh->fh_handle)) {
4529 if (refcount_inc_not_zero(&fp->fi_ref))
4536 static struct nfs4_file *insert_file(struct nfs4_file *new, struct svc_fh *fh,
4537 unsigned int hashval)
4539 struct nfs4_file *fp;
4540 struct nfs4_file *ret = NULL;
4541 bool alias_found = false;
4543 spin_lock(&state_lock);
4544 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash,
4545 lockdep_is_held(&state_lock)) {
4546 if (fh_match(&fp->fi_fhandle, &fh->fh_handle)) {
4547 if (refcount_inc_not_zero(&fp->fi_ref))
4549 } else if (d_inode(fh->fh_dentry) == fp->fi_inode)
4550 fp->fi_aliased = alias_found = true;
4552 if (likely(ret == NULL)) {
4553 nfsd4_init_file(fh, hashval, new);
4554 new->fi_aliased = alias_found;
4557 spin_unlock(&state_lock);
4561 static struct nfs4_file * find_file(struct svc_fh *fh)
4563 struct nfs4_file *fp;
4564 unsigned int hashval = file_hashval(fh);
4567 fp = find_file_locked(fh, hashval);
4572 static struct nfs4_file *
4573 find_or_add_file(struct nfs4_file *new, struct svc_fh *fh)
4575 struct nfs4_file *fp;
4576 unsigned int hashval = file_hashval(fh);
4579 fp = find_file_locked(fh, hashval);
4584 return insert_file(new, fh, hashval);
4588 * Called to check deny when READ with all zero stateid or
4589 * WRITE with all zero or all one stateid
4592 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
4594 struct nfs4_file *fp;
4595 __be32 ret = nfs_ok;
4597 fp = find_file(current_fh);
4600 /* Check for conflicting share reservations */
4601 spin_lock(&fp->fi_lock);
4602 if (fp->fi_share_deny & deny_type)
4603 ret = nfserr_locked;
4604 spin_unlock(&fp->fi_lock);
4609 static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
4611 struct nfs4_delegation *dp = cb_to_delegation(cb);
4612 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
4615 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
4618 * We can't do this in nfsd_break_deleg_cb because it is
4619 * already holding inode->i_lock.
4621 * If the dl_time != 0, then we know that it has already been
4622 * queued for a lease break. Don't queue it again.
4624 spin_lock(&state_lock);
4625 if (delegation_hashed(dp) && dp->dl_time == 0) {
4626 dp->dl_time = ktime_get_boottime_seconds();
4627 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
4629 spin_unlock(&state_lock);
4632 static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
4633 struct rpc_task *task)
4635 struct nfs4_delegation *dp = cb_to_delegation(cb);
4637 if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID ||
4638 dp->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID)
4641 switch (task->tk_status) {
4644 case -NFS4ERR_DELAY:
4645 rpc_delay(task, 2 * HZ);
4648 case -NFS4ERR_BAD_STATEID:
4650 * Race: client probably got cb_recall before open reply
4651 * granting delegation.
4653 if (dp->dl_retries--) {
4654 rpc_delay(task, 2 * HZ);
4663 static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
4665 struct nfs4_delegation *dp = cb_to_delegation(cb);
4667 nfs4_put_stid(&dp->dl_stid);
4670 static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
4671 .prepare = nfsd4_cb_recall_prepare,
4672 .done = nfsd4_cb_recall_done,
4673 .release = nfsd4_cb_recall_release,
4676 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
4679 * We're assuming the state code never drops its reference
4680 * without first removing the lease. Since we're in this lease
4681 * callback (and since the lease code is serialized by the
4682 * i_lock) we know the server hasn't removed the lease yet, and
4683 * we know it's safe to take a reference.
4685 refcount_inc(&dp->dl_stid.sc_count);
4686 nfsd4_run_cb(&dp->dl_recall);
4689 /* Called from break_lease() with i_lock held. */
4691 nfsd_break_deleg_cb(struct file_lock *fl)
4694 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
4695 struct nfs4_file *fp = dp->dl_stid.sc_file;
4697 trace_nfsd_cb_recall(&dp->dl_stid);
4700 * We don't want the locks code to timeout the lease for us;
4701 * we'll remove it ourself if a delegation isn't returned
4704 fl->fl_break_time = 0;
4706 spin_lock(&fp->fi_lock);
4707 fp->fi_had_conflict = true;
4708 nfsd_break_one_deleg(dp);
4709 spin_unlock(&fp->fi_lock);
4714 * nfsd_breaker_owns_lease - Check if lease conflict was resolved
4715 * @fl: Lock state to check
4718 * %true: Lease conflict was resolved
4719 * %false: Lease conflict was not resolved.
4721 static bool nfsd_breaker_owns_lease(struct file_lock *fl)
4723 struct nfs4_delegation *dl = fl->fl_owner;
4724 struct svc_rqst *rqst;
4725 struct nfs4_client *clp;
4729 rqst = kthread_data(current);
4730 /* Note rq_prog == NFS_ACL_PROGRAM is also possible: */
4731 if (rqst->rq_prog != NFS_PROGRAM || rqst->rq_vers < 4)
4733 clp = *(rqst->rq_lease_breaker);
4734 return dl->dl_stid.sc_client == clp;
4738 nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
4739 struct list_head *dispose)
4742 return lease_modify(onlist, arg, dispose);
4747 static const struct lock_manager_operations nfsd_lease_mng_ops = {
4748 .lm_breaker_owns_lease = nfsd_breaker_owns_lease,
4749 .lm_break = nfsd_break_deleg_cb,
4750 .lm_change = nfsd_change_deleg_cb,
4753 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
4755 if (nfsd4_has_session(cstate))
4757 if (seqid == so->so_seqid - 1)
4758 return nfserr_replay_me;
4759 if (seqid == so->so_seqid)
4761 return nfserr_bad_seqid;
4764 static struct nfs4_client *lookup_clientid(clientid_t *clid, bool sessions,
4765 struct nfsd_net *nn)
4767 struct nfs4_client *found;
4769 spin_lock(&nn->client_lock);
4770 found = find_confirmed_client(clid, sessions, nn);
4772 atomic_inc(&found->cl_rpc_users);
4773 spin_unlock(&nn->client_lock);
4777 static __be32 set_client(clientid_t *clid,
4778 struct nfsd4_compound_state *cstate,
4779 struct nfsd_net *nn)
4782 if (!same_clid(&cstate->clp->cl_clientid, clid))
4783 return nfserr_stale_clientid;
4786 if (STALE_CLIENTID(clid, nn))
4787 return nfserr_stale_clientid;
4789 * We're in the 4.0 case (otherwise the SEQUENCE op would have
4790 * set cstate->clp), so session = false:
4792 cstate->clp = lookup_clientid(clid, false, nn);
4794 return nfserr_expired;
4799 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
4800 struct nfsd4_open *open, struct nfsd_net *nn)
4802 clientid_t *clientid = &open->op_clientid;
4803 struct nfs4_client *clp = NULL;
4804 unsigned int strhashval;
4805 struct nfs4_openowner *oo = NULL;
4809 * In case we need it later, after we've already created the
4810 * file and don't want to risk a further failure:
4812 open->op_file = nfsd4_alloc_file();
4813 if (open->op_file == NULL)
4814 return nfserr_jukebox;
4816 status = set_client(clientid, cstate, nn);
4821 strhashval = ownerstr_hashval(&open->op_owner);
4822 oo = find_openstateowner_str(strhashval, open, clp);
4823 open->op_openowner = oo;
4827 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4828 /* Replace unconfirmed owners without checking for replay. */
4829 release_openowner(oo);
4830 open->op_openowner = NULL;
4833 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
4838 oo = alloc_init_open_stateowner(strhashval, open, cstate);
4840 return nfserr_jukebox;
4841 open->op_openowner = oo;
4843 open->op_stp = nfs4_alloc_open_stateid(clp);
4845 return nfserr_jukebox;
4847 if (nfsd4_has_session(cstate) &&
4848 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
4849 open->op_odstate = alloc_clnt_odstate(clp);
4850 if (!open->op_odstate)
4851 return nfserr_jukebox;
4857 static inline __be32
4858 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
4860 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
4861 return nfserr_openmode;
4866 static int share_access_to_flags(u32 share_access)
4868 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
4871 static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
4873 struct nfs4_stid *ret;
4875 ret = find_stateid_by_type(cl, s,
4876 NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID);
4879 return delegstateid(ret);
4882 static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
4884 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
4885 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
4889 nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
4890 struct nfs4_delegation **dp)
4893 __be32 status = nfserr_bad_stateid;
4894 struct nfs4_delegation *deleg;
4896 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
4899 if (deleg->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) {
4900 nfs4_put_stid(&deleg->dl_stid);
4901 if (cl->cl_minorversion)
4902 status = nfserr_deleg_revoked;
4905 flags = share_access_to_flags(open->op_share_access);
4906 status = nfs4_check_delegmode(deleg, flags);
4908 nfs4_put_stid(&deleg->dl_stid);
4913 if (!nfsd4_is_deleg_cur(open))
4917 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
4921 static inline int nfs4_access_to_access(u32 nfs4_access)
4925 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
4926 flags |= NFSD_MAY_READ;
4927 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
4928 flags |= NFSD_MAY_WRITE;
4932 static inline __be32
4933 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
4934 struct nfsd4_open *open)
4936 struct iattr iattr = {
4937 .ia_valid = ATTR_SIZE,
4940 if (!open->op_truncate)
4942 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
4943 return nfserr_inval;
4944 return nfsd_setattr(rqstp, fh, &iattr, 0, (time64_t)0);
4947 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
4948 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
4949 struct nfsd4_open *open)
4951 struct nfsd_file *nf = NULL;
4953 int oflag = nfs4_access_to_omode(open->op_share_access);
4954 int access = nfs4_access_to_access(open->op_share_access);
4955 unsigned char old_access_bmap, old_deny_bmap;
4957 spin_lock(&fp->fi_lock);
4960 * Are we trying to set a deny mode that would conflict with
4963 status = nfs4_file_check_deny(fp, open->op_share_deny);
4964 if (status != nfs_ok) {
4965 spin_unlock(&fp->fi_lock);
4969 /* set access to the file */
4970 status = nfs4_file_get_access(fp, open->op_share_access);
4971 if (status != nfs_ok) {
4972 spin_unlock(&fp->fi_lock);
4976 /* Set access bits in stateid */
4977 old_access_bmap = stp->st_access_bmap;
4978 set_access(open->op_share_access, stp);
4980 /* Set new deny mask */
4981 old_deny_bmap = stp->st_deny_bmap;
4982 set_deny(open->op_share_deny, stp);
4983 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4985 if (!fp->fi_fds[oflag]) {
4986 spin_unlock(&fp->fi_lock);
4987 status = nfsd_file_acquire(rqstp, cur_fh, access, &nf);
4989 goto out_put_access;
4990 spin_lock(&fp->fi_lock);
4991 if (!fp->fi_fds[oflag]) {
4992 fp->fi_fds[oflag] = nf;
4996 spin_unlock(&fp->fi_lock);
5000 status = nfserrno(nfsd_open_break_lease(cur_fh->fh_dentry->d_inode,
5003 goto out_put_access;
5005 status = nfsd4_truncate(rqstp, cur_fh, open);
5007 goto out_put_access;
5011 stp->st_access_bmap = old_access_bmap;
5012 nfs4_file_put_access(fp, open->op_share_access);
5013 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
5018 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)
5021 unsigned char old_deny_bmap = stp->st_deny_bmap;
5023 if (!test_access(open->op_share_access, stp))
5024 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
5026 /* test and set deny mode */
5027 spin_lock(&fp->fi_lock);
5028 status = nfs4_file_check_deny(fp, open->op_share_deny);
5029 if (status == nfs_ok) {
5030 set_deny(open->op_share_deny, stp);
5031 fp->fi_share_deny |=
5032 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
5034 spin_unlock(&fp->fi_lock);
5036 if (status != nfs_ok)
5039 status = nfsd4_truncate(rqstp, cur_fh, open);
5040 if (status != nfs_ok)
5041 reset_union_bmap_deny(old_deny_bmap, stp);
5045 /* Should we give out recallable state?: */
5046 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
5048 if (clp->cl_cb_state == NFSD4_CB_UP)
5051 * In the sessions case, since we don't have to establish a
5052 * separate connection for callbacks, we assume it's OK
5053 * until we hear otherwise:
5055 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
5058 static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp,
5061 struct file_lock *fl;
5063 fl = locks_alloc_lock();
5066 fl->fl_lmops = &nfsd_lease_mng_ops;
5067 fl->fl_flags = FL_DELEG;
5068 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
5069 fl->fl_end = OFFSET_MAX;
5070 fl->fl_owner = (fl_owner_t)dp;
5071 fl->fl_pid = current->tgid;
5072 fl->fl_file = dp->dl_stid.sc_file->fi_deleg_file->nf_file;
5076 static int nfsd4_check_conflicting_opens(struct nfs4_client *clp,
5077 struct nfs4_file *fp)
5079 struct nfs4_ol_stateid *st;
5080 struct file *f = fp->fi_deleg_file->nf_file;
5081 struct inode *ino = locks_inode(f);
5084 writes = atomic_read(&ino->i_writecount);
5088 * There could be multiple filehandles (hence multiple
5089 * nfs4_files) referencing this file, but that's not too
5090 * common; let's just give up in that case rather than
5091 * trying to go look up all the clients using that other
5092 * nfs4_file as well:
5097 * If there's a close in progress, make sure that we see it
5098 * clear any fi_fds[] entries before we see it decrement
5101 smp_mb__after_atomic();
5103 if (fp->fi_fds[O_WRONLY])
5105 if (fp->fi_fds[O_RDWR])
5108 return -EAGAIN; /* There may be non-NFSv4 writers */
5110 * It's possible there are non-NFSv4 write opens in progress,
5111 * but if they haven't incremented i_writecount yet then they
5112 * also haven't called break lease yet; so, they'll break this
5113 * lease soon enough. So, all that's left to check for is NFSv4
5116 spin_lock(&fp->fi_lock);
5117 list_for_each_entry(st, &fp->fi_stateids, st_perfile) {
5118 if (st->st_openstp == NULL /* it's an open */ &&
5119 access_permit_write(st) &&
5120 st->st_stid.sc_client != clp) {
5121 spin_unlock(&fp->fi_lock);
5125 spin_unlock(&fp->fi_lock);
5127 * There's a small chance that we could be racing with another
5128 * NFSv4 open. However, any open that hasn't added itself to
5129 * the fi_stateids list also hasn't called break_lease yet; so,
5130 * they'll break this lease soon enough.
5135 static struct nfs4_delegation *
5136 nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
5137 struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate)
5140 struct nfs4_delegation *dp;
5141 struct nfsd_file *nf;
5142 struct file_lock *fl;
5145 * The fi_had_conflict and nfs_get_existing_delegation checks
5146 * here are just optimizations; we'll need to recheck them at
5149 if (fp->fi_had_conflict)
5150 return ERR_PTR(-EAGAIN);
5152 nf = find_readable_file(fp);
5155 * We probably could attempt another open and get a read
5156 * delegation, but for now, don't bother until the
5157 * client actually sends us one.
5159 return ERR_PTR(-EAGAIN);
5161 spin_lock(&state_lock);
5162 spin_lock(&fp->fi_lock);
5163 if (nfs4_delegation_exists(clp, fp))
5165 else if (!fp->fi_deleg_file) {
5166 fp->fi_deleg_file = nf;
5167 /* increment early to prevent fi_deleg_file from being
5169 fp->fi_delegees = 1;
5173 spin_unlock(&fp->fi_lock);
5174 spin_unlock(&state_lock);
5178 return ERR_PTR(status);
5181 dp = alloc_init_deleg(clp, fp, fh, odstate);
5185 fl = nfs4_alloc_init_lease(dp, NFS4_OPEN_DELEGATE_READ);
5187 goto out_clnt_odstate;
5189 status = vfs_setlease(fp->fi_deleg_file->nf_file, fl->fl_type, &fl, NULL);
5191 locks_free_lock(fl);
5193 goto out_clnt_odstate;
5194 status = nfsd4_check_conflicting_opens(clp, fp);
5198 spin_lock(&state_lock);
5199 spin_lock(&fp->fi_lock);
5200 if (fp->fi_had_conflict)
5203 status = hash_delegation_locked(dp, fp);
5204 spin_unlock(&fp->fi_lock);
5205 spin_unlock(&state_lock);
5212 vfs_setlease(fp->fi_deleg_file->nf_file, F_UNLCK, NULL, (void **)&dp);
5214 put_clnt_odstate(dp->dl_clnt_odstate);
5215 nfs4_put_stid(&dp->dl_stid);
5218 return ERR_PTR(status);
5221 static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
5223 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5224 if (status == -EAGAIN)
5225 open->op_why_no_deleg = WND4_CONTENTION;
5227 open->op_why_no_deleg = WND4_RESOURCE;
5228 switch (open->op_deleg_want) {
5229 case NFS4_SHARE_WANT_READ_DELEG:
5230 case NFS4_SHARE_WANT_WRITE_DELEG:
5231 case NFS4_SHARE_WANT_ANY_DELEG:
5233 case NFS4_SHARE_WANT_CANCEL:
5234 open->op_why_no_deleg = WND4_CANCELLED;
5236 case NFS4_SHARE_WANT_NO_DELEG:
5243 * Attempt to hand out a delegation.
5245 * Note we don't support write delegations, and won't until the vfs has
5246 * proper support for them.
5249 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
5250 struct nfs4_ol_stateid *stp)
5252 struct nfs4_delegation *dp;
5253 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
5254 struct nfs4_client *clp = stp->st_stid.sc_client;
5258 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
5259 open->op_recall = 0;
5260 switch (open->op_claim_type) {
5261 case NFS4_OPEN_CLAIM_PREVIOUS:
5263 open->op_recall = 1;
5264 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
5267 case NFS4_OPEN_CLAIM_NULL:
5268 case NFS4_OPEN_CLAIM_FH:
5270 * Let's not give out any delegations till everyone's
5271 * had the chance to reclaim theirs, *and* until
5272 * NLM locks have all been reclaimed:
5274 if (locks_in_grace(clp->net))
5276 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
5282 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate);
5286 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
5288 trace_nfsd_deleg_read(&dp->dl_stid.sc_stateid);
5289 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
5290 nfs4_put_stid(&dp->dl_stid);
5293 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
5294 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
5295 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
5296 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
5297 open->op_recall = 1;
5300 /* 4.1 client asking for a delegation? */
5301 if (open->op_deleg_want)
5302 nfsd4_open_deleg_none_ext(open, status);
5306 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
5307 struct nfs4_delegation *dp)
5309 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
5310 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
5311 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5312 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
5313 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
5314 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
5315 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5316 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
5318 /* Otherwise the client must be confused wanting a delegation
5319 * it already has, therefore we don't return
5320 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
5325 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
5327 struct nfsd4_compoundres *resp = rqstp->rq_resp;
5328 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
5329 struct nfs4_file *fp = NULL;
5330 struct nfs4_ol_stateid *stp = NULL;
5331 struct nfs4_delegation *dp = NULL;
5333 bool new_stp = false;
5336 * Lookup file; if found, lookup stateid and check open request,
5337 * and check for delegations in the process of being recalled.
5338 * If not found, create the nfs4_file struct
5340 fp = find_or_add_file(open->op_file, current_fh);
5341 if (fp != open->op_file) {
5342 status = nfs4_check_deleg(cl, open, &dp);
5345 stp = nfsd4_find_and_lock_existing_open(fp, open);
5347 open->op_file = NULL;
5348 status = nfserr_bad_stateid;
5349 if (nfsd4_is_deleg_cur(open))
5354 stp = init_open_stateid(fp, open);
5360 * OPEN the file, or upgrade an existing OPEN.
5361 * If truncate fails, the OPEN fails.
5363 * stp is already locked.
5366 /* Stateid was found, this is an OPEN upgrade */
5367 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
5369 mutex_unlock(&stp->st_mutex);
5373 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
5375 stp->st_stid.sc_type = NFS4_CLOSED_STID;
5376 release_open_stateid(stp);
5377 mutex_unlock(&stp->st_mutex);
5381 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
5383 if (stp->st_clnt_odstate == open->op_odstate)
5384 open->op_odstate = NULL;
5387 nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
5388 mutex_unlock(&stp->st_mutex);
5390 if (nfsd4_has_session(&resp->cstate)) {
5391 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
5392 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5393 open->op_why_no_deleg = WND4_NOT_WANTED;
5399 * Attempt to hand out a delegation. No error return, because the
5400 * OPEN succeeds even if we fail.
5402 nfs4_open_delegation(current_fh, open, stp);
5405 trace_nfsd_open(&stp->st_stid.sc_stateid);
5407 /* 4.1 client trying to upgrade/downgrade delegation? */
5408 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
5409 open->op_deleg_want)
5410 nfsd4_deleg_xgrade_none_ext(open, dp);
5414 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
5415 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
5417 * To finish the open response, we just need to set the rflags.
5419 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
5420 if (nfsd4_has_session(&resp->cstate))
5421 open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK;
5422 else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED))
5423 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
5426 nfs4_put_stid(&dp->dl_stid);
5428 nfs4_put_stid(&stp->st_stid);
5433 void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
5434 struct nfsd4_open *open)
5436 if (open->op_openowner) {
5437 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
5439 nfsd4_cstate_assign_replay(cstate, so);
5440 nfs4_put_stateowner(so);
5443 kmem_cache_free(file_slab, open->op_file);
5445 nfs4_put_stid(&open->op_stp->st_stid);
5446 if (open->op_odstate)
5447 kmem_cache_free(odstate_slab, open->op_odstate);
5451 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5452 union nfsd4_op_u *u)
5454 clientid_t *clid = &u->renew;
5455 struct nfs4_client *clp;
5457 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5459 trace_nfsd_clid_renew(clid);
5460 status = set_client(clid, cstate, nn);
5464 if (!list_empty(&clp->cl_delegations)
5465 && clp->cl_cb_state != NFSD4_CB_UP)
5466 return nfserr_cb_path_down;
5471 nfsd4_end_grace(struct nfsd_net *nn)
5473 /* do nothing if grace period already ended */
5474 if (nn->grace_ended)
5477 trace_nfsd_grace_complete(nn);
5478 nn->grace_ended = true;
5480 * If the server goes down again right now, an NFSv4
5481 * client will still be allowed to reclaim after it comes back up,
5482 * even if it hasn't yet had a chance to reclaim state this time.
5485 nfsd4_record_grace_done(nn);
5487 * At this point, NFSv4 clients can still reclaim. But if the
5488 * server crashes, any that have not yet reclaimed will be out
5489 * of luck on the next boot.
5491 * (NFSv4.1+ clients are considered to have reclaimed once they
5492 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
5493 * have reclaimed after their first OPEN.)
5495 locks_end_grace(&nn->nfsd4_manager);
5497 * At this point, and once lockd and/or any other containers
5498 * exit their grace period, further reclaims will fail and
5499 * regular locking can resume.
5504 * If we've waited a lease period but there are still clients trying to
5505 * reclaim, wait a little longer to give them a chance to finish.
5507 static bool clients_still_reclaiming(struct nfsd_net *nn)
5509 time64_t double_grace_period_end = nn->boot_time +
5510 2 * nn->nfsd4_lease;
5512 if (nn->track_reclaim_completes &&
5513 atomic_read(&nn->nr_reclaim_complete) ==
5514 nn->reclaim_str_hashtbl_size)
5516 if (!nn->somebody_reclaimed)
5518 nn->somebody_reclaimed = false;
5520 * If we've given them *two* lease times to reclaim, and they're
5521 * still not done, give up:
5523 if (ktime_get_boottime_seconds() > double_grace_period_end)
5528 struct laundry_time {
5533 static bool state_expired(struct laundry_time *lt, time64_t last_refresh)
5535 time64_t time_remaining;
5537 if (last_refresh < lt->cutoff)
5539 time_remaining = last_refresh - lt->cutoff;
5540 lt->new_timeo = min(lt->new_timeo, time_remaining);
5544 #ifdef CONFIG_NFSD_V4_2_INTER_SSC
5545 void nfsd4_ssc_init_umount_work(struct nfsd_net *nn)
5547 spin_lock_init(&nn->nfsd_ssc_lock);
5548 INIT_LIST_HEAD(&nn->nfsd_ssc_mount_list);
5549 init_waitqueue_head(&nn->nfsd_ssc_waitq);
5551 EXPORT_SYMBOL_GPL(nfsd4_ssc_init_umount_work);
5554 * This is called when nfsd is being shutdown, after all inter_ssc
5555 * cleanup were done, to destroy the ssc delayed unmount list.
5557 static void nfsd4_ssc_shutdown_umount(struct nfsd_net *nn)
5559 struct nfsd4_ssc_umount_item *ni = NULL;
5560 struct nfsd4_ssc_umount_item *tmp;
5562 spin_lock(&nn->nfsd_ssc_lock);
5563 list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) {
5564 list_del(&ni->nsui_list);
5565 spin_unlock(&nn->nfsd_ssc_lock);
5566 mntput(ni->nsui_vfsmount);
5568 spin_lock(&nn->nfsd_ssc_lock);
5570 spin_unlock(&nn->nfsd_ssc_lock);
5573 static void nfsd4_ssc_expire_umount(struct nfsd_net *nn)
5575 bool do_wakeup = false;
5576 struct nfsd4_ssc_umount_item *ni = 0;
5577 struct nfsd4_ssc_umount_item *tmp;
5579 spin_lock(&nn->nfsd_ssc_lock);
5580 list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) {
5581 if (time_after(jiffies, ni->nsui_expire)) {
5582 if (refcount_read(&ni->nsui_refcnt) > 1)
5585 /* mark being unmount */
5586 ni->nsui_busy = true;
5587 spin_unlock(&nn->nfsd_ssc_lock);
5588 mntput(ni->nsui_vfsmount);
5589 spin_lock(&nn->nfsd_ssc_lock);
5591 /* waiters need to start from begin of list */
5592 list_del(&ni->nsui_list);
5595 /* wakeup ssc_connect waiters */
5602 wake_up_all(&nn->nfsd_ssc_waitq);
5603 spin_unlock(&nn->nfsd_ssc_lock);
5608 nfs4_laundromat(struct nfsd_net *nn)
5610 struct nfs4_client *clp;
5611 struct nfs4_openowner *oo;
5612 struct nfs4_delegation *dp;
5613 struct nfs4_ol_stateid *stp;
5614 struct nfsd4_blocked_lock *nbl;
5615 struct list_head *pos, *next, reaplist;
5616 struct laundry_time lt = {
5617 .cutoff = ktime_get_boottime_seconds() - nn->nfsd4_lease,
5618 .new_timeo = nn->nfsd4_lease
5620 struct nfs4_cpntf_state *cps;
5621 copy_stateid_t *cps_t;
5624 if (clients_still_reclaiming(nn)) {
5628 nfsd4_end_grace(nn);
5629 INIT_LIST_HEAD(&reaplist);
5631 spin_lock(&nn->s2s_cp_lock);
5632 idr_for_each_entry(&nn->s2s_cp_stateids, cps_t, i) {
5633 cps = container_of(cps_t, struct nfs4_cpntf_state, cp_stateid);
5634 if (cps->cp_stateid.sc_type == NFS4_COPYNOTIFY_STID &&
5635 state_expired(<, cps->cpntf_time))
5636 _free_cpntf_state_locked(nn, cps);
5638 spin_unlock(&nn->s2s_cp_lock);
5640 spin_lock(&nn->client_lock);
5641 list_for_each_safe(pos, next, &nn->client_lru) {
5642 clp = list_entry(pos, struct nfs4_client, cl_lru);
5643 if (!state_expired(<, clp->cl_time))
5645 if (mark_client_expired_locked(clp))
5647 list_add(&clp->cl_lru, &reaplist);
5649 spin_unlock(&nn->client_lock);
5650 list_for_each_safe(pos, next, &reaplist) {
5651 clp = list_entry(pos, struct nfs4_client, cl_lru);
5652 trace_nfsd_clid_purged(&clp->cl_clientid);
5653 list_del_init(&clp->cl_lru);
5656 spin_lock(&state_lock);
5657 list_for_each_safe(pos, next, &nn->del_recall_lru) {
5658 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
5659 if (!state_expired(<, dp->dl_time))
5661 WARN_ON(!unhash_delegation_locked(dp));
5662 list_add(&dp->dl_recall_lru, &reaplist);
5664 spin_unlock(&state_lock);
5665 while (!list_empty(&reaplist)) {
5666 dp = list_first_entry(&reaplist, struct nfs4_delegation,
5668 list_del_init(&dp->dl_recall_lru);
5669 revoke_delegation(dp);
5672 spin_lock(&nn->client_lock);
5673 while (!list_empty(&nn->close_lru)) {
5674 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
5676 if (!state_expired(<, oo->oo_time))
5678 list_del_init(&oo->oo_close_lru);
5679 stp = oo->oo_last_closed_stid;
5680 oo->oo_last_closed_stid = NULL;
5681 spin_unlock(&nn->client_lock);
5682 nfs4_put_stid(&stp->st_stid);
5683 spin_lock(&nn->client_lock);
5685 spin_unlock(&nn->client_lock);
5688 * It's possible for a client to try and acquire an already held lock
5689 * that is being held for a long time, and then lose interest in it.
5690 * So, we clean out any un-revisited request after a lease period
5691 * under the assumption that the client is no longer interested.
5693 * RFC5661, sec. 9.6 states that the client must not rely on getting
5694 * notifications and must continue to poll for locks, even when the
5695 * server supports them. Thus this shouldn't lead to clients blocking
5696 * indefinitely once the lock does become free.
5698 BUG_ON(!list_empty(&reaplist));
5699 spin_lock(&nn->blocked_locks_lock);
5700 while (!list_empty(&nn->blocked_locks_lru)) {
5701 nbl = list_first_entry(&nn->blocked_locks_lru,
5702 struct nfsd4_blocked_lock, nbl_lru);
5703 if (!state_expired(<, nbl->nbl_time))
5705 list_move(&nbl->nbl_lru, &reaplist);
5706 list_del_init(&nbl->nbl_list);
5708 spin_unlock(&nn->blocked_locks_lock);
5710 while (!list_empty(&reaplist)) {
5711 nbl = list_first_entry(&reaplist,
5712 struct nfsd4_blocked_lock, nbl_lru);
5713 list_del_init(&nbl->nbl_lru);
5714 free_blocked_lock(nbl);
5716 #ifdef CONFIG_NFSD_V4_2_INTER_SSC
5717 /* service the server-to-server copy delayed unmount list */
5718 nfsd4_ssc_expire_umount(nn);
5721 return max_t(time64_t, lt.new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
5724 static struct workqueue_struct *laundry_wq;
5725 static void laundromat_main(struct work_struct *);
5728 laundromat_main(struct work_struct *laundry)
5731 struct delayed_work *dwork = to_delayed_work(laundry);
5732 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
5735 t = nfs4_laundromat(nn);
5736 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
5739 static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
5741 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
5742 return nfserr_bad_stateid;
5747 __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
5749 __be32 status = nfserr_openmode;
5751 /* For lock stateid's, we test the parent open, not the lock: */
5752 if (stp->st_openstp)
5753 stp = stp->st_openstp;
5754 if ((flags & WR_STATE) && !access_permit_write(stp))
5756 if ((flags & RD_STATE) && !access_permit_read(stp))
5763 static inline __be32
5764 check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
5766 if (ONE_STATEID(stateid) && (flags & RD_STATE))
5768 else if (opens_in_grace(net)) {
5769 /* Answer in remaining cases depends on existence of
5770 * conflicting state; so we must wait out the grace period. */
5771 return nfserr_grace;
5772 } else if (flags & WR_STATE)
5773 return nfs4_share_conflict(current_fh,
5774 NFS4_SHARE_DENY_WRITE);
5775 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
5776 return nfs4_share_conflict(current_fh,
5777 NFS4_SHARE_DENY_READ);
5780 static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
5783 * When sessions are used the stateid generation number is ignored
5786 if (has_session && in->si_generation == 0)
5789 if (in->si_generation == ref->si_generation)
5792 /* If the client sends us a stateid from the future, it's buggy: */
5793 if (nfsd4_stateid_generation_after(in, ref))
5794 return nfserr_bad_stateid;
5796 * However, we could see a stateid from the past, even from a
5797 * non-buggy client. For example, if the client sends a lock
5798 * while some IO is outstanding, the lock may bump si_generation
5799 * while the IO is still in flight. The client could avoid that
5800 * situation by waiting for responses on all the IO requests,
5801 * but better performance may result in retrying IO that
5802 * receives an old_stateid error if requests are rarely
5803 * reordered in flight:
5805 return nfserr_old_stateid;
5808 static __be32 nfsd4_stid_check_stateid_generation(stateid_t *in, struct nfs4_stid *s, bool has_session)
5812 spin_lock(&s->sc_lock);
5813 ret = nfsd4_verify_open_stid(s);
5815 ret = check_stateid_generation(in, &s->sc_stateid, has_session);
5816 spin_unlock(&s->sc_lock);
5820 static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
5822 if (ols->st_stateowner->so_is_open_owner &&
5823 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
5824 return nfserr_bad_stateid;
5828 static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
5830 struct nfs4_stid *s;
5831 __be32 status = nfserr_bad_stateid;
5833 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5834 CLOSE_STATEID(stateid))
5836 spin_lock(&cl->cl_lock);
5837 s = find_stateid_locked(cl, stateid);
5840 status = nfsd4_stid_check_stateid_generation(stateid, s, 1);
5843 switch (s->sc_type) {
5844 case NFS4_DELEG_STID:
5847 case NFS4_REVOKED_DELEG_STID:
5848 status = nfserr_deleg_revoked;
5850 case NFS4_OPEN_STID:
5851 case NFS4_LOCK_STID:
5852 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
5855 printk("unknown stateid type %x\n", s->sc_type);
5857 case NFS4_CLOSED_STID:
5858 case NFS4_CLOSED_DELEG_STID:
5859 status = nfserr_bad_stateid;
5862 spin_unlock(&cl->cl_lock);
5867 nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
5868 stateid_t *stateid, unsigned char typemask,
5869 struct nfs4_stid **s, struct nfsd_net *nn)
5872 bool return_revoked = false;
5875 * only return revoked delegations if explicitly asked.
5876 * otherwise we report revoked or bad_stateid status.
5878 if (typemask & NFS4_REVOKED_DELEG_STID)
5879 return_revoked = true;
5880 else if (typemask & NFS4_DELEG_STID)
5881 typemask |= NFS4_REVOKED_DELEG_STID;
5883 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5884 CLOSE_STATEID(stateid))
5885 return nfserr_bad_stateid;
5886 status = set_client(&stateid->si_opaque.so_clid, cstate, nn);
5887 if (status == nfserr_stale_clientid) {
5888 if (cstate->session)
5889 return nfserr_bad_stateid;
5890 return nfserr_stale_stateid;
5894 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
5896 return nfserr_bad_stateid;
5897 if (((*s)->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) {
5899 if (cstate->minorversion)
5900 return nfserr_deleg_revoked;
5901 return nfserr_bad_stateid;
5906 static struct nfsd_file *
5907 nfs4_find_file(struct nfs4_stid *s, int flags)
5912 switch (s->sc_type) {
5913 case NFS4_DELEG_STID:
5914 if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
5916 return nfsd_file_get(s->sc_file->fi_deleg_file);
5917 case NFS4_OPEN_STID:
5918 case NFS4_LOCK_STID:
5919 if (flags & RD_STATE)
5920 return find_readable_file(s->sc_file);
5922 return find_writeable_file(s->sc_file);
5929 nfs4_check_olstateid(struct nfs4_ol_stateid *ols, int flags)
5933 status = nfsd4_check_openowner_confirmed(ols);
5936 return nfs4_check_openmode(ols, flags);
5940 nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
5941 struct nfsd_file **nfp, int flags)
5943 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
5944 struct nfsd_file *nf;
5947 nf = nfs4_find_file(s, flags);
5949 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
5950 acc | NFSD_MAY_OWNER_OVERRIDE);
5956 status = nfsd_file_acquire(rqstp, fhp, acc, &nf);
5965 _free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
5967 WARN_ON_ONCE(cps->cp_stateid.sc_type != NFS4_COPYNOTIFY_STID);
5968 if (!refcount_dec_and_test(&cps->cp_stateid.sc_count))
5970 list_del(&cps->cp_list);
5971 idr_remove(&nn->s2s_cp_stateids,
5972 cps->cp_stateid.stid.si_opaque.so_id);
5976 * A READ from an inter server to server COPY will have a
5977 * copy stateid. Look up the copy notify stateid from the
5978 * idr structure and take a reference on it.
5980 __be32 manage_cpntf_state(struct nfsd_net *nn, stateid_t *st,
5981 struct nfs4_client *clp,
5982 struct nfs4_cpntf_state **cps)
5984 copy_stateid_t *cps_t;
5985 struct nfs4_cpntf_state *state = NULL;
5987 if (st->si_opaque.so_clid.cl_id != nn->s2s_cp_cl_id)
5988 return nfserr_bad_stateid;
5989 spin_lock(&nn->s2s_cp_lock);
5990 cps_t = idr_find(&nn->s2s_cp_stateids, st->si_opaque.so_id);
5992 state = container_of(cps_t, struct nfs4_cpntf_state,
5994 if (state->cp_stateid.sc_type != NFS4_COPYNOTIFY_STID) {
5999 refcount_inc(&state->cp_stateid.sc_count);
6001 _free_cpntf_state_locked(nn, state);
6004 spin_unlock(&nn->s2s_cp_lock);
6006 return nfserr_bad_stateid;
6012 static __be32 find_cpntf_state(struct nfsd_net *nn, stateid_t *st,
6013 struct nfs4_stid **stid)
6016 struct nfs4_cpntf_state *cps = NULL;
6017 struct nfs4_client *found;
6019 status = manage_cpntf_state(nn, st, NULL, &cps);
6023 cps->cpntf_time = ktime_get_boottime_seconds();
6025 status = nfserr_expired;
6026 found = lookup_clientid(&cps->cp_p_clid, true, nn);
6030 *stid = find_stateid_by_type(found, &cps->cp_p_stateid,
6031 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID);
6035 status = nfserr_bad_stateid;
6037 put_client_renew(found);
6039 nfs4_put_cpntf_state(nn, cps);
6043 void nfs4_put_cpntf_state(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
6045 spin_lock(&nn->s2s_cp_lock);
6046 _free_cpntf_state_locked(nn, cps);
6047 spin_unlock(&nn->s2s_cp_lock);
6051 * Checks for stateid operations
6054 nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
6055 struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
6056 stateid_t *stateid, int flags, struct nfsd_file **nfp,
6057 struct nfs4_stid **cstid)
6059 struct net *net = SVC_NET(rqstp);
6060 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6061 struct nfs4_stid *s = NULL;
6067 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
6069 status = nfserr_bad_stateid;
6071 status = check_special_stateids(net, fhp, stateid,
6076 status = nfsd4_lookup_stateid(cstate, stateid,
6077 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
6079 if (status == nfserr_bad_stateid)
6080 status = find_cpntf_state(nn, stateid, &s);
6083 status = nfsd4_stid_check_stateid_generation(stateid, s,
6084 nfsd4_has_session(cstate));
6088 switch (s->sc_type) {
6089 case NFS4_DELEG_STID:
6090 status = nfs4_check_delegmode(delegstateid(s), flags);
6092 case NFS4_OPEN_STID:
6093 case NFS4_LOCK_STID:
6094 status = nfs4_check_olstateid(openlockstateid(s), flags);
6097 status = nfserr_bad_stateid;
6102 status = nfs4_check_fh(fhp, s);
6105 if (status == nfs_ok && nfp)
6106 status = nfs4_check_file(rqstp, fhp, s, nfp, flags);
6109 if (!status && cstid)
6118 * Test if the stateid is valid
6121 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6122 union nfsd4_op_u *u)
6124 struct nfsd4_test_stateid *test_stateid = &u->test_stateid;
6125 struct nfsd4_test_stateid_id *stateid;
6126 struct nfs4_client *cl = cstate->clp;
6128 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
6129 stateid->ts_id_status =
6130 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
6136 nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
6138 struct nfs4_ol_stateid *stp = openlockstateid(s);
6141 ret = nfsd4_lock_ol_stateid(stp);
6145 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
6149 ret = nfserr_locks_held;
6150 if (check_for_locks(stp->st_stid.sc_file,
6151 lockowner(stp->st_stateowner)))
6154 release_lock_stateid(stp);
6158 mutex_unlock(&stp->st_mutex);
6165 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6166 union nfsd4_op_u *u)
6168 struct nfsd4_free_stateid *free_stateid = &u->free_stateid;
6169 stateid_t *stateid = &free_stateid->fr_stateid;
6170 struct nfs4_stid *s;
6171 struct nfs4_delegation *dp;
6172 struct nfs4_client *cl = cstate->clp;
6173 __be32 ret = nfserr_bad_stateid;
6175 spin_lock(&cl->cl_lock);
6176 s = find_stateid_locked(cl, stateid);
6179 spin_lock(&s->sc_lock);
6180 switch (s->sc_type) {
6181 case NFS4_DELEG_STID:
6182 ret = nfserr_locks_held;
6184 case NFS4_OPEN_STID:
6185 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
6188 ret = nfserr_locks_held;
6190 case NFS4_LOCK_STID:
6191 spin_unlock(&s->sc_lock);
6192 refcount_inc(&s->sc_count);
6193 spin_unlock(&cl->cl_lock);
6194 ret = nfsd4_free_lock_stateid(stateid, s);
6196 case NFS4_REVOKED_DELEG_STID:
6197 spin_unlock(&s->sc_lock);
6198 dp = delegstateid(s);
6199 list_del_init(&dp->dl_recall_lru);
6200 spin_unlock(&cl->cl_lock);
6204 /* Default falls through and returns nfserr_bad_stateid */
6206 spin_unlock(&s->sc_lock);
6208 spin_unlock(&cl->cl_lock);
6216 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
6217 RD_STATE : WR_STATE;
6220 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
6222 struct svc_fh *current_fh = &cstate->current_fh;
6223 struct nfs4_stateowner *sop = stp->st_stateowner;
6226 status = nfsd4_check_seqid(cstate, sop, seqid);
6229 status = nfsd4_lock_ol_stateid(stp);
6230 if (status != nfs_ok)
6232 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
6233 if (status == nfs_ok)
6234 status = nfs4_check_fh(current_fh, &stp->st_stid);
6235 if (status != nfs_ok)
6236 mutex_unlock(&stp->st_mutex);
6241 * Checks for sequence id mutating operations.
6244 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
6245 stateid_t *stateid, char typemask,
6246 struct nfs4_ol_stateid **stpp,
6247 struct nfsd_net *nn)
6250 struct nfs4_stid *s;
6251 struct nfs4_ol_stateid *stp = NULL;
6253 trace_nfsd_preprocess(seqid, stateid);
6256 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
6259 stp = openlockstateid(s);
6260 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
6262 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
6266 nfs4_put_stid(&stp->st_stid);
6270 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
6271 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
6274 struct nfs4_openowner *oo;
6275 struct nfs4_ol_stateid *stp;
6277 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
6278 NFS4_OPEN_STID, &stp, nn);
6281 oo = openowner(stp->st_stateowner);
6282 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
6283 mutex_unlock(&stp->st_mutex);
6284 nfs4_put_stid(&stp->st_stid);
6285 return nfserr_bad_stateid;
6292 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6293 union nfsd4_op_u *u)
6295 struct nfsd4_open_confirm *oc = &u->open_confirm;
6297 struct nfs4_openowner *oo;
6298 struct nfs4_ol_stateid *stp;
6299 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6301 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
6302 cstate->current_fh.fh_dentry);
6304 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
6308 status = nfs4_preprocess_seqid_op(cstate,
6309 oc->oc_seqid, &oc->oc_req_stateid,
6310 NFS4_OPEN_STID, &stp, nn);
6313 oo = openowner(stp->st_stateowner);
6314 status = nfserr_bad_stateid;
6315 if (oo->oo_flags & NFS4_OO_CONFIRMED) {
6316 mutex_unlock(&stp->st_mutex);
6319 oo->oo_flags |= NFS4_OO_CONFIRMED;
6320 nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
6321 mutex_unlock(&stp->st_mutex);
6322 trace_nfsd_open_confirm(oc->oc_seqid, &stp->st_stid.sc_stateid);
6323 nfsd4_client_record_create(oo->oo_owner.so_client);
6326 nfs4_put_stid(&stp->st_stid);
6328 nfsd4_bump_seqid(cstate, status);
6332 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
6334 if (!test_access(access, stp))
6336 nfs4_file_put_access(stp->st_stid.sc_file, access);
6337 clear_access(access, stp);
6340 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
6342 switch (to_access) {
6343 case NFS4_SHARE_ACCESS_READ:
6344 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
6345 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
6347 case NFS4_SHARE_ACCESS_WRITE:
6348 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
6349 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
6351 case NFS4_SHARE_ACCESS_BOTH:
6359 nfsd4_open_downgrade(struct svc_rqst *rqstp,
6360 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
6362 struct nfsd4_open_downgrade *od = &u->open_downgrade;
6364 struct nfs4_ol_stateid *stp;
6365 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6367 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
6368 cstate->current_fh.fh_dentry);
6370 /* We don't yet support WANT bits: */
6371 if (od->od_deleg_want)
6372 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
6375 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
6376 &od->od_stateid, &stp, nn);
6379 status = nfserr_inval;
6380 if (!test_access(od->od_share_access, stp)) {
6381 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
6382 stp->st_access_bmap, od->od_share_access);
6385 if (!test_deny(od->od_share_deny, stp)) {
6386 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
6387 stp->st_deny_bmap, od->od_share_deny);
6390 nfs4_stateid_downgrade(stp, od->od_share_access);
6391 reset_union_bmap_deny(od->od_share_deny, stp);
6392 nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
6395 mutex_unlock(&stp->st_mutex);
6396 nfs4_put_stid(&stp->st_stid);
6398 nfsd4_bump_seqid(cstate, status);
6402 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
6404 struct nfs4_client *clp = s->st_stid.sc_client;
6406 LIST_HEAD(reaplist);
6407 struct nfs4_ol_stateid *stp;
6409 spin_lock(&clp->cl_lock);
6410 unhashed = unhash_open_stateid(s, &reaplist);
6412 if (clp->cl_minorversion) {
6414 put_ol_stateid_locked(s, &reaplist);
6415 spin_unlock(&clp->cl_lock);
6416 list_for_each_entry(stp, &reaplist, st_locks)
6417 nfs4_free_cpntf_statelist(clp->net, &stp->st_stid);
6418 free_ol_stateid_reaplist(&reaplist);
6420 spin_unlock(&clp->cl_lock);
6421 free_ol_stateid_reaplist(&reaplist);
6423 move_to_close_lru(s, clp->net);
6428 * nfs4_unlock_state() called after encode
6431 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6432 union nfsd4_op_u *u)
6434 struct nfsd4_close *close = &u->close;
6436 struct nfs4_ol_stateid *stp;
6437 struct net *net = SVC_NET(rqstp);
6438 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6440 dprintk("NFSD: nfsd4_close on file %pd\n",
6441 cstate->current_fh.fh_dentry);
6443 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
6445 NFS4_OPEN_STID|NFS4_CLOSED_STID,
6447 nfsd4_bump_seqid(cstate, status);
6451 stp->st_stid.sc_type = NFS4_CLOSED_STID;
6454 * Technically we don't _really_ have to increment or copy it, since
6455 * it should just be gone after this operation and we clobber the
6456 * copied value below, but we continue to do so here just to ensure
6457 * that racing ops see that there was a state change.
6459 nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
6461 nfsd4_close_open_stateid(stp);
6462 mutex_unlock(&stp->st_mutex);
6464 /* v4.1+ suggests that we send a special stateid in here, since the
6465 * clients should just ignore this anyway. Since this is not useful
6466 * for v4.0 clients either, we set it to the special close_stateid
6469 * See RFC5661 section 18.2.4, and RFC7530 section 16.2.5
6471 memcpy(&close->cl_stateid, &close_stateid, sizeof(close->cl_stateid));
6473 /* put reference from nfs4_preprocess_seqid_op */
6474 nfs4_put_stid(&stp->st_stid);
6480 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6481 union nfsd4_op_u *u)
6483 struct nfsd4_delegreturn *dr = &u->delegreturn;
6484 struct nfs4_delegation *dp;
6485 stateid_t *stateid = &dr->dr_stateid;
6486 struct nfs4_stid *s;
6488 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6490 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
6493 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
6496 dp = delegstateid(s);
6497 status = nfsd4_stid_check_stateid_generation(stateid, &dp->dl_stid, nfsd4_has_session(cstate));
6501 destroy_delegation(dp);
6503 nfs4_put_stid(&dp->dl_stid);
6508 /* last octet in a range */
6510 last_byte_offset(u64 start, u64 len)
6516 return end > start ? end - 1: NFS4_MAX_UINT64;
6520 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
6521 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
6522 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
6523 * locking, this prevents us from being completely protocol-compliant. The
6524 * real solution to this problem is to start using unsigned file offsets in
6525 * the VFS, but this is a very deep change!
6528 nfs4_transform_lock_offset(struct file_lock *lock)
6530 if (lock->fl_start < 0)
6531 lock->fl_start = OFFSET_MAX;
6532 if (lock->fl_end < 0)
6533 lock->fl_end = OFFSET_MAX;
6537 nfsd4_fl_get_owner(fl_owner_t owner)
6539 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
6541 nfs4_get_stateowner(&lo->lo_owner);
6546 nfsd4_fl_put_owner(fl_owner_t owner)
6548 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
6551 nfs4_put_stateowner(&lo->lo_owner);
6555 nfsd4_lm_notify(struct file_lock *fl)
6557 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
6558 struct net *net = lo->lo_owner.so_client->net;
6559 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6560 struct nfsd4_blocked_lock *nbl = container_of(fl,
6561 struct nfsd4_blocked_lock, nbl_lock);
6564 /* An empty list means that something else is going to be using it */
6565 spin_lock(&nn->blocked_locks_lock);
6566 if (!list_empty(&nbl->nbl_list)) {
6567 list_del_init(&nbl->nbl_list);
6568 list_del_init(&nbl->nbl_lru);
6571 spin_unlock(&nn->blocked_locks_lock);
6574 trace_nfsd_cb_notify_lock(lo, nbl);
6575 nfsd4_run_cb(&nbl->nbl_cb);
6579 static const struct lock_manager_operations nfsd_posix_mng_ops = {
6580 .lm_notify = nfsd4_lm_notify,
6581 .lm_get_owner = nfsd4_fl_get_owner,
6582 .lm_put_owner = nfsd4_fl_put_owner,
6586 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
6588 struct nfs4_lockowner *lo;
6590 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
6591 lo = (struct nfs4_lockowner *) fl->fl_owner;
6592 xdr_netobj_dup(&deny->ld_owner, &lo->lo_owner.so_owner,
6594 if (!deny->ld_owner.data)
6595 /* We just don't care that much */
6597 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
6600 deny->ld_owner.len = 0;
6601 deny->ld_owner.data = NULL;
6602 deny->ld_clientid.cl_boot = 0;
6603 deny->ld_clientid.cl_id = 0;
6605 deny->ld_start = fl->fl_start;
6606 deny->ld_length = NFS4_MAX_UINT64;
6607 if (fl->fl_end != NFS4_MAX_UINT64)
6608 deny->ld_length = fl->fl_end - fl->fl_start + 1;
6609 deny->ld_type = NFS4_READ_LT;
6610 if (fl->fl_type != F_RDLCK)
6611 deny->ld_type = NFS4_WRITE_LT;
6614 static struct nfs4_lockowner *
6615 find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
6617 unsigned int strhashval = ownerstr_hashval(owner);
6618 struct nfs4_stateowner *so;
6620 lockdep_assert_held(&clp->cl_lock);
6622 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
6624 if (so->so_is_open_owner)
6626 if (same_owner_str(so, owner))
6627 return lockowner(nfs4_get_stateowner(so));
6632 static struct nfs4_lockowner *
6633 find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
6635 struct nfs4_lockowner *lo;
6637 spin_lock(&clp->cl_lock);
6638 lo = find_lockowner_str_locked(clp, owner);
6639 spin_unlock(&clp->cl_lock);
6643 static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
6645 unhash_lockowner_locked(lockowner(sop));
6648 static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
6650 struct nfs4_lockowner *lo = lockowner(sop);
6652 kmem_cache_free(lockowner_slab, lo);
6655 static const struct nfs4_stateowner_operations lockowner_ops = {
6656 .so_unhash = nfs4_unhash_lockowner,
6657 .so_free = nfs4_free_lockowner,
6661 * Alloc a lock owner structure.
6662 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
6665 * strhashval = ownerstr_hashval
6667 static struct nfs4_lockowner *
6668 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
6669 struct nfs4_ol_stateid *open_stp,
6670 struct nfsd4_lock *lock)
6672 struct nfs4_lockowner *lo, *ret;
6674 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
6677 INIT_LIST_HEAD(&lo->lo_blocked);
6678 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
6679 lo->lo_owner.so_is_open_owner = 0;
6680 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
6681 lo->lo_owner.so_ops = &lockowner_ops;
6682 spin_lock(&clp->cl_lock);
6683 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
6685 list_add(&lo->lo_owner.so_strhash,
6686 &clp->cl_ownerstr_hashtbl[strhashval]);
6689 nfs4_free_stateowner(&lo->lo_owner);
6691 spin_unlock(&clp->cl_lock);
6695 static struct nfs4_ol_stateid *
6696 find_lock_stateid(const struct nfs4_lockowner *lo,
6697 const struct nfs4_ol_stateid *ost)
6699 struct nfs4_ol_stateid *lst;
6701 lockdep_assert_held(&ost->st_stid.sc_client->cl_lock);
6703 /* If ost is not hashed, ost->st_locks will not be valid */
6704 if (!nfs4_ol_stateid_unhashed(ost))
6705 list_for_each_entry(lst, &ost->st_locks, st_locks) {
6706 if (lst->st_stateowner == &lo->lo_owner) {
6707 refcount_inc(&lst->st_stid.sc_count);
6714 static struct nfs4_ol_stateid *
6715 init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
6716 struct nfs4_file *fp, struct inode *inode,
6717 struct nfs4_ol_stateid *open_stp)
6719 struct nfs4_client *clp = lo->lo_owner.so_client;
6720 struct nfs4_ol_stateid *retstp;
6722 mutex_init(&stp->st_mutex);
6723 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
6725 spin_lock(&clp->cl_lock);
6726 if (nfs4_ol_stateid_unhashed(open_stp))
6728 retstp = find_lock_stateid(lo, open_stp);
6731 refcount_inc(&stp->st_stid.sc_count);
6732 stp->st_stid.sc_type = NFS4_LOCK_STID;
6733 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
6735 stp->st_stid.sc_file = fp;
6736 stp->st_access_bmap = 0;
6737 stp->st_deny_bmap = open_stp->st_deny_bmap;
6738 stp->st_openstp = open_stp;
6739 spin_lock(&fp->fi_lock);
6740 list_add(&stp->st_locks, &open_stp->st_locks);
6741 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
6742 list_add(&stp->st_perfile, &fp->fi_stateids);
6743 spin_unlock(&fp->fi_lock);
6744 spin_unlock(&clp->cl_lock);
6747 spin_unlock(&clp->cl_lock);
6748 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
6749 nfs4_put_stid(&retstp->st_stid);
6752 /* To keep mutex tracking happy */
6753 mutex_unlock(&stp->st_mutex);
6756 spin_unlock(&clp->cl_lock);
6757 mutex_unlock(&stp->st_mutex);
6761 static struct nfs4_ol_stateid *
6762 find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
6763 struct inode *inode, struct nfs4_ol_stateid *ost,
6766 struct nfs4_stid *ns = NULL;
6767 struct nfs4_ol_stateid *lst;
6768 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
6769 struct nfs4_client *clp = oo->oo_owner.so_client;
6772 spin_lock(&clp->cl_lock);
6773 lst = find_lock_stateid(lo, ost);
6774 spin_unlock(&clp->cl_lock);
6776 if (nfsd4_lock_ol_stateid(lst) == nfs_ok)
6778 nfs4_put_stid(&lst->st_stid);
6780 ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid);
6784 lst = init_lock_stateid(openlockstateid(ns), lo, fi, inode, ost);
6785 if (lst == openlockstateid(ns))
6794 check_lock_length(u64 offset, u64 length)
6796 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
6797 (length > ~offset)));
6800 static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
6802 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
6804 lockdep_assert_held(&fp->fi_lock);
6806 if (test_access(access, lock_stp))
6808 __nfs4_file_get_access(fp, access);
6809 set_access(access, lock_stp);
6813 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
6814 struct nfs4_ol_stateid *ost,
6815 struct nfsd4_lock *lock,
6816 struct nfs4_ol_stateid **plst, bool *new)
6819 struct nfs4_file *fi = ost->st_stid.sc_file;
6820 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
6821 struct nfs4_client *cl = oo->oo_owner.so_client;
6822 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
6823 struct nfs4_lockowner *lo;
6824 struct nfs4_ol_stateid *lst;
6825 unsigned int strhashval;
6827 lo = find_lockowner_str(cl, &lock->lk_new_owner);
6829 strhashval = ownerstr_hashval(&lock->lk_new_owner);
6830 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
6832 return nfserr_jukebox;
6834 /* with an existing lockowner, seqids must be the same */
6835 status = nfserr_bad_seqid;
6836 if (!cstate->minorversion &&
6837 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
6841 lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
6843 status = nfserr_jukebox;
6850 nfs4_put_stateowner(&lo->lo_owner);
6858 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6859 union nfsd4_op_u *u)
6861 struct nfsd4_lock *lock = &u->lock;
6862 struct nfs4_openowner *open_sop = NULL;
6863 struct nfs4_lockowner *lock_sop = NULL;
6864 struct nfs4_ol_stateid *lock_stp = NULL;
6865 struct nfs4_ol_stateid *open_stp = NULL;
6866 struct nfs4_file *fp;
6867 struct nfsd_file *nf = NULL;
6868 struct nfsd4_blocked_lock *nbl = NULL;
6869 struct file_lock *file_lock = NULL;
6870 struct file_lock *conflock = NULL;
6871 struct super_block *sb;
6876 unsigned char fl_type;
6877 unsigned int fl_flags = FL_POSIX;
6878 struct net *net = SVC_NET(rqstp);
6879 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6881 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
6882 (long long) lock->lk_offset,
6883 (long long) lock->lk_length);
6885 if (check_lock_length(lock->lk_offset, lock->lk_length))
6886 return nfserr_inval;
6888 if ((status = fh_verify(rqstp, &cstate->current_fh,
6889 S_IFREG, NFSD_MAY_LOCK))) {
6890 dprintk("NFSD: nfsd4_lock: permission denied!\n");
6893 sb = cstate->current_fh.fh_dentry->d_sb;
6895 if (lock->lk_is_new) {
6896 if (nfsd4_has_session(cstate))
6897 /* See rfc 5661 18.10.3: given clientid is ignored: */
6898 memcpy(&lock->lk_new_clientid,
6899 &cstate->clp->cl_clientid,
6900 sizeof(clientid_t));
6902 /* validate and update open stateid and open seqid */
6903 status = nfs4_preprocess_confirmed_seqid_op(cstate,
6904 lock->lk_new_open_seqid,
6905 &lock->lk_new_open_stateid,
6909 mutex_unlock(&open_stp->st_mutex);
6910 open_sop = openowner(open_stp->st_stateowner);
6911 status = nfserr_bad_stateid;
6912 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
6913 &lock->lk_new_clientid))
6915 status = lookup_or_create_lock_state(cstate, open_stp, lock,
6918 status = nfs4_preprocess_seqid_op(cstate,
6919 lock->lk_old_lock_seqid,
6920 &lock->lk_old_lock_stateid,
6921 NFS4_LOCK_STID, &lock_stp, nn);
6925 lock_sop = lockowner(lock_stp->st_stateowner);
6927 lkflg = setlkflg(lock->lk_type);
6928 status = nfs4_check_openmode(lock_stp, lkflg);
6932 status = nfserr_grace;
6933 if (locks_in_grace(net) && !lock->lk_reclaim)
6935 status = nfserr_no_grace;
6936 if (!locks_in_grace(net) && lock->lk_reclaim)
6939 if (lock->lk_reclaim)
6940 fl_flags |= FL_RECLAIM;
6942 fp = lock_stp->st_stid.sc_file;
6943 switch (lock->lk_type) {
6945 if (nfsd4_has_session(cstate) &&
6946 !(sb->s_export_op->flags & EXPORT_OP_SYNC_LOCKS))
6947 fl_flags |= FL_SLEEP;
6950 spin_lock(&fp->fi_lock);
6951 nf = find_readable_file_locked(fp);
6953 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
6954 spin_unlock(&fp->fi_lock);
6957 case NFS4_WRITEW_LT:
6958 if (nfsd4_has_session(cstate) &&
6959 !(sb->s_export_op->flags & EXPORT_OP_SYNC_LOCKS))
6960 fl_flags |= FL_SLEEP;
6963 spin_lock(&fp->fi_lock);
6964 nf = find_writeable_file_locked(fp);
6966 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
6967 spin_unlock(&fp->fi_lock);
6971 status = nfserr_inval;
6976 status = nfserr_openmode;
6980 nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
6982 dprintk("NFSD: %s: unable to allocate block!\n", __func__);
6983 status = nfserr_jukebox;
6987 file_lock = &nbl->nbl_lock;
6988 file_lock->fl_type = fl_type;
6989 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
6990 file_lock->fl_pid = current->tgid;
6991 file_lock->fl_file = nf->nf_file;
6992 file_lock->fl_flags = fl_flags;
6993 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6994 file_lock->fl_start = lock->lk_offset;
6995 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
6996 nfs4_transform_lock_offset(file_lock);
6998 conflock = locks_alloc_lock();
7000 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
7001 status = nfserr_jukebox;
7005 if (fl_flags & FL_SLEEP) {
7006 nbl->nbl_time = ktime_get_boottime_seconds();
7007 spin_lock(&nn->blocked_locks_lock);
7008 list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
7009 list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
7010 spin_unlock(&nn->blocked_locks_lock);
7013 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, conflock);
7015 case 0: /* success! */
7016 nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
7018 if (lock->lk_reclaim)
7019 nn->somebody_reclaimed = true;
7021 case FILE_LOCK_DEFERRED:
7024 case -EAGAIN: /* conflock holds conflicting lock */
7025 status = nfserr_denied;
7026 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
7027 nfs4_set_lock_denied(conflock, &lock->lk_denied);
7030 status = nfserr_deadlock;
7033 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
7034 status = nfserrno(err);
7039 /* dequeue it if we queued it before */
7040 if (fl_flags & FL_SLEEP) {
7041 spin_lock(&nn->blocked_locks_lock);
7042 list_del_init(&nbl->nbl_list);
7043 list_del_init(&nbl->nbl_lru);
7044 spin_unlock(&nn->blocked_locks_lock);
7046 free_blocked_lock(nbl);
7051 /* Bump seqid manually if the 4.0 replay owner is openowner */
7052 if (cstate->replay_owner &&
7053 cstate->replay_owner != &lock_sop->lo_owner &&
7054 seqid_mutating_err(ntohl(status)))
7055 lock_sop->lo_owner.so_seqid++;
7058 * If this is a new, never-before-used stateid, and we are
7059 * returning an error, then just go ahead and release it.
7062 release_lock_stateid(lock_stp);
7064 mutex_unlock(&lock_stp->st_mutex);
7066 nfs4_put_stid(&lock_stp->st_stid);
7069 nfs4_put_stid(&open_stp->st_stid);
7070 nfsd4_bump_seqid(cstate, status);
7072 locks_free_lock(conflock);
7077 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
7078 * so we do a temporary open here just to get an open file to pass to
7081 static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
7083 struct nfsd_file *nf;
7086 err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf);
7089 fh_lock(fhp); /* to block new leases till after test_lock: */
7090 err = nfserrno(nfsd_open_break_lease(fhp->fh_dentry->d_inode,
7094 lock->fl_file = nf->nf_file;
7095 err = nfserrno(vfs_test_lock(nf->nf_file, lock));
7096 lock->fl_file = NULL;
7107 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7108 union nfsd4_op_u *u)
7110 struct nfsd4_lockt *lockt = &u->lockt;
7111 struct file_lock *file_lock = NULL;
7112 struct nfs4_lockowner *lo = NULL;
7114 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
7116 if (locks_in_grace(SVC_NET(rqstp)))
7117 return nfserr_grace;
7119 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
7120 return nfserr_inval;
7122 if (!nfsd4_has_session(cstate)) {
7123 status = set_client(&lockt->lt_clientid, cstate, nn);
7128 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
7131 file_lock = locks_alloc_lock();
7133 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
7134 status = nfserr_jukebox;
7138 switch (lockt->lt_type) {
7141 file_lock->fl_type = F_RDLCK;
7144 case NFS4_WRITEW_LT:
7145 file_lock->fl_type = F_WRLCK;
7148 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
7149 status = nfserr_inval;
7153 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
7155 file_lock->fl_owner = (fl_owner_t)lo;
7156 file_lock->fl_pid = current->tgid;
7157 file_lock->fl_flags = FL_POSIX;
7159 file_lock->fl_start = lockt->lt_offset;
7160 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
7162 nfs4_transform_lock_offset(file_lock);
7164 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
7168 if (file_lock->fl_type != F_UNLCK) {
7169 status = nfserr_denied;
7170 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
7174 nfs4_put_stateowner(&lo->lo_owner);
7176 locks_free_lock(file_lock);
7181 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7182 union nfsd4_op_u *u)
7184 struct nfsd4_locku *locku = &u->locku;
7185 struct nfs4_ol_stateid *stp;
7186 struct nfsd_file *nf = NULL;
7187 struct file_lock *file_lock = NULL;
7190 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
7192 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
7193 (long long) locku->lu_offset,
7194 (long long) locku->lu_length);
7196 if (check_lock_length(locku->lu_offset, locku->lu_length))
7197 return nfserr_inval;
7199 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
7200 &locku->lu_stateid, NFS4_LOCK_STID,
7204 nf = find_any_file(stp->st_stid.sc_file);
7206 status = nfserr_lock_range;
7209 file_lock = locks_alloc_lock();
7211 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
7212 status = nfserr_jukebox;
7216 file_lock->fl_type = F_UNLCK;
7217 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
7218 file_lock->fl_pid = current->tgid;
7219 file_lock->fl_file = nf->nf_file;
7220 file_lock->fl_flags = FL_POSIX;
7221 file_lock->fl_lmops = &nfsd_posix_mng_ops;
7222 file_lock->fl_start = locku->lu_offset;
7224 file_lock->fl_end = last_byte_offset(locku->lu_offset,
7226 nfs4_transform_lock_offset(file_lock);
7228 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, NULL);
7230 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
7233 nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
7237 mutex_unlock(&stp->st_mutex);
7238 nfs4_put_stid(&stp->st_stid);
7240 nfsd4_bump_seqid(cstate, status);
7242 locks_free_lock(file_lock);
7246 status = nfserrno(err);
7252 * true: locks held by lockowner
7253 * false: no locks held by lockowner
7256 check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
7258 struct file_lock *fl;
7260 struct nfsd_file *nf = find_any_file(fp);
7261 struct inode *inode;
7262 struct file_lock_context *flctx;
7265 /* Any valid lock stateid should have some sort of access */
7270 inode = locks_inode(nf->nf_file);
7271 flctx = inode->i_flctx;
7273 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
7274 spin_lock(&flctx->flc_lock);
7275 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
7276 if (fl->fl_owner == (fl_owner_t)lowner) {
7281 spin_unlock(&flctx->flc_lock);
7288 nfsd4_release_lockowner(struct svc_rqst *rqstp,
7289 struct nfsd4_compound_state *cstate,
7290 union nfsd4_op_u *u)
7292 struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner;
7293 clientid_t *clid = &rlockowner->rl_clientid;
7294 struct nfs4_stateowner *sop;
7295 struct nfs4_lockowner *lo = NULL;
7296 struct nfs4_ol_stateid *stp;
7297 struct xdr_netobj *owner = &rlockowner->rl_owner;
7298 unsigned int hashval = ownerstr_hashval(owner);
7300 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
7301 struct nfs4_client *clp;
7302 LIST_HEAD (reaplist);
7304 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
7305 clid->cl_boot, clid->cl_id);
7307 status = set_client(clid, cstate, nn);
7312 /* Find the matching lock stateowner */
7313 spin_lock(&clp->cl_lock);
7314 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
7317 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
7320 if (atomic_read(&sop->so_count) != 1) {
7321 spin_unlock(&clp->cl_lock);
7322 return nfserr_locks_held;
7325 lo = lockowner(sop);
7326 nfs4_get_stateowner(sop);
7330 spin_unlock(&clp->cl_lock);
7334 unhash_lockowner_locked(lo);
7335 while (!list_empty(&lo->lo_owner.so_stateids)) {
7336 stp = list_first_entry(&lo->lo_owner.so_stateids,
7337 struct nfs4_ol_stateid,
7339 WARN_ON(!unhash_lock_stateid(stp));
7340 put_ol_stateid_locked(stp, &reaplist);
7342 spin_unlock(&clp->cl_lock);
7343 free_ol_stateid_reaplist(&reaplist);
7344 remove_blocked_locks(lo);
7345 nfs4_put_stateowner(&lo->lo_owner);
7350 static inline struct nfs4_client_reclaim *
7353 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
7357 nfs4_has_reclaimed_state(struct xdr_netobj name, struct nfsd_net *nn)
7359 struct nfs4_client_reclaim *crp;
7361 crp = nfsd4_find_reclaim_client(name, nn);
7362 return (crp && crp->cr_clp);
7366 * failure => all reset bets are off, nfserr_no_grace...
7368 * The caller is responsible for freeing name.data if NULL is returned (it
7369 * will be freed in nfs4_remove_reclaim_record in the normal case).
7371 struct nfs4_client_reclaim *
7372 nfs4_client_to_reclaim(struct xdr_netobj name, struct xdr_netobj princhash,
7373 struct nfsd_net *nn)
7375 unsigned int strhashval;
7376 struct nfs4_client_reclaim *crp;
7378 crp = alloc_reclaim();
7380 strhashval = clientstr_hashval(name);
7381 INIT_LIST_HEAD(&crp->cr_strhash);
7382 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
7383 crp->cr_name.data = name.data;
7384 crp->cr_name.len = name.len;
7385 crp->cr_princhash.data = princhash.data;
7386 crp->cr_princhash.len = princhash.len;
7388 nn->reclaim_str_hashtbl_size++;
7394 nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
7396 list_del(&crp->cr_strhash);
7397 kfree(crp->cr_name.data);
7398 kfree(crp->cr_princhash.data);
7400 nn->reclaim_str_hashtbl_size--;
7404 nfs4_release_reclaim(struct nfsd_net *nn)
7406 struct nfs4_client_reclaim *crp = NULL;
7409 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7410 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
7411 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
7412 struct nfs4_client_reclaim, cr_strhash);
7413 nfs4_remove_reclaim_record(crp, nn);
7416 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
7420 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
7421 struct nfs4_client_reclaim *
7422 nfsd4_find_reclaim_client(struct xdr_netobj name, struct nfsd_net *nn)
7424 unsigned int strhashval;
7425 struct nfs4_client_reclaim *crp = NULL;
7427 strhashval = clientstr_hashval(name);
7428 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
7429 if (compare_blob(&crp->cr_name, &name) == 0) {
7437 nfs4_check_open_reclaim(struct nfs4_client *clp)
7439 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &clp->cl_flags))
7440 return nfserr_no_grace;
7442 if (nfsd4_client_record_check(clp))
7443 return nfserr_reclaim_bad;
7449 * Since the lifetime of a delegation isn't limited to that of an open, a
7450 * client may quite reasonably hang on to a delegation as long as it has
7451 * the inode cached. This becomes an obvious problem the first time a
7452 * client's inode cache approaches the size of the server's total memory.
7454 * For now we avoid this problem by imposing a hard limit on the number
7455 * of delegations, which varies according to the server's memory size.
7458 set_max_delegations(void)
7461 * Allow at most 4 delegations per megabyte of RAM. Quick
7462 * estimates suggest that in the worst case (where every delegation
7463 * is for a different inode), a delegation could take about 1.5K,
7464 * giving a worst case usage of about 6% of memory.
7466 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
7469 static int nfs4_state_create_net(struct net *net)
7471 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7474 nn->conf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7475 sizeof(struct list_head),
7477 if (!nn->conf_id_hashtbl)
7479 nn->unconf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7480 sizeof(struct list_head),
7482 if (!nn->unconf_id_hashtbl)
7484 nn->sessionid_hashtbl = kmalloc_array(SESSION_HASH_SIZE,
7485 sizeof(struct list_head),
7487 if (!nn->sessionid_hashtbl)
7490 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7491 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
7492 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
7494 for (i = 0; i < SESSION_HASH_SIZE; i++)
7495 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
7496 nn->conf_name_tree = RB_ROOT;
7497 nn->unconf_name_tree = RB_ROOT;
7498 nn->boot_time = ktime_get_real_seconds();
7499 nn->grace_ended = false;
7500 nn->nfsd4_manager.block_opens = true;
7501 INIT_LIST_HEAD(&nn->nfsd4_manager.list);
7502 INIT_LIST_HEAD(&nn->client_lru);
7503 INIT_LIST_HEAD(&nn->close_lru);
7504 INIT_LIST_HEAD(&nn->del_recall_lru);
7505 spin_lock_init(&nn->client_lock);
7506 spin_lock_init(&nn->s2s_cp_lock);
7507 idr_init(&nn->s2s_cp_stateids);
7509 spin_lock_init(&nn->blocked_locks_lock);
7510 INIT_LIST_HEAD(&nn->blocked_locks_lru);
7512 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
7518 kfree(nn->unconf_id_hashtbl);
7520 kfree(nn->conf_id_hashtbl);
7526 nfs4_state_destroy_net(struct net *net)
7529 struct nfs4_client *clp = NULL;
7530 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7532 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7533 while (!list_empty(&nn->conf_id_hashtbl[i])) {
7534 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7535 destroy_client(clp);
7539 WARN_ON(!list_empty(&nn->blocked_locks_lru));
7541 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7542 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
7543 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7544 destroy_client(clp);
7548 kfree(nn->sessionid_hashtbl);
7549 kfree(nn->unconf_id_hashtbl);
7550 kfree(nn->conf_id_hashtbl);
7555 nfs4_state_start_net(struct net *net)
7557 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7560 ret = nfs4_state_create_net(net);
7563 locks_start_grace(net, &nn->nfsd4_manager);
7564 nfsd4_client_tracking_init(net);
7565 if (nn->track_reclaim_completes && nn->reclaim_str_hashtbl_size == 0)
7567 printk(KERN_INFO "NFSD: starting %lld-second grace period (net %x)\n",
7568 nn->nfsd4_grace, net->ns.inum);
7569 trace_nfsd_grace_start(nn);
7570 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
7574 printk(KERN_INFO "NFSD: no clients to reclaim, skipping NFSv4 grace period (net %x)\n",
7576 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_lease * HZ);
7577 nfsd4_end_grace(nn);
7581 /* initialization to perform when the nfsd service is started: */
7584 nfs4_state_start(void)
7588 laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
7589 if (laundry_wq == NULL) {
7593 ret = nfsd4_create_callback_queue();
7595 goto out_free_laundry;
7597 set_max_delegations();
7601 destroy_workqueue(laundry_wq);
7607 nfs4_state_shutdown_net(struct net *net)
7609 struct nfs4_delegation *dp = NULL;
7610 struct list_head *pos, *next, reaplist;
7611 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7613 cancel_delayed_work_sync(&nn->laundromat_work);
7614 locks_end_grace(&nn->nfsd4_manager);
7616 INIT_LIST_HEAD(&reaplist);
7617 spin_lock(&state_lock);
7618 list_for_each_safe(pos, next, &nn->del_recall_lru) {
7619 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
7620 WARN_ON(!unhash_delegation_locked(dp));
7621 list_add(&dp->dl_recall_lru, &reaplist);
7623 spin_unlock(&state_lock);
7624 list_for_each_safe(pos, next, &reaplist) {
7625 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
7626 list_del_init(&dp->dl_recall_lru);
7627 destroy_unhashed_deleg(dp);
7630 nfsd4_client_tracking_exit(net);
7631 nfs4_state_destroy_net(net);
7632 #ifdef CONFIG_NFSD_V4_2_INTER_SSC
7633 nfsd4_ssc_shutdown_umount(nn);
7638 nfs4_state_shutdown(void)
7640 destroy_workqueue(laundry_wq);
7641 nfsd4_destroy_callback_queue();
7645 get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7647 if (HAS_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG) &&
7648 CURRENT_STATEID(stateid))
7649 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
7653 put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7655 if (cstate->minorversion) {
7656 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
7657 SET_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
7662 clear_current_stateid(struct nfsd4_compound_state *cstate)
7664 CLEAR_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
7668 * functions to set current state id
7671 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate,
7672 union nfsd4_op_u *u)
7674 put_stateid(cstate, &u->open_downgrade.od_stateid);
7678 nfsd4_set_openstateid(struct nfsd4_compound_state *cstate,
7679 union nfsd4_op_u *u)
7681 put_stateid(cstate, &u->open.op_stateid);
7685 nfsd4_set_closestateid(struct nfsd4_compound_state *cstate,
7686 union nfsd4_op_u *u)
7688 put_stateid(cstate, &u->close.cl_stateid);
7692 nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate,
7693 union nfsd4_op_u *u)
7695 put_stateid(cstate, &u->lock.lk_resp_stateid);
7699 * functions to consume current state id
7703 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate,
7704 union nfsd4_op_u *u)
7706 get_stateid(cstate, &u->open_downgrade.od_stateid);
7710 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate,
7711 union nfsd4_op_u *u)
7713 get_stateid(cstate, &u->delegreturn.dr_stateid);
7717 nfsd4_get_freestateid(struct nfsd4_compound_state *cstate,
7718 union nfsd4_op_u *u)
7720 get_stateid(cstate, &u->free_stateid.fr_stateid);
7724 nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate,
7725 union nfsd4_op_u *u)
7727 get_stateid(cstate, &u->setattr.sa_stateid);
7731 nfsd4_get_closestateid(struct nfsd4_compound_state *cstate,
7732 union nfsd4_op_u *u)
7734 get_stateid(cstate, &u->close.cl_stateid);
7738 nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate,
7739 union nfsd4_op_u *u)
7741 get_stateid(cstate, &u->locku.lu_stateid);
7745 nfsd4_get_readstateid(struct nfsd4_compound_state *cstate,
7746 union nfsd4_op_u *u)
7748 get_stateid(cstate, &u->read.rd_stateid);
7752 nfsd4_get_writestateid(struct nfsd4_compound_state *cstate,
7753 union nfsd4_op_u *u)
7755 get_stateid(cstate, &u->write.wr_stateid);