2 * Copyright (c) 2001 The Regents of the University of Michigan.
5 * Kendrick Smith <kmsmith@umich.edu>
6 * Andy Adamson <kandros@umich.edu>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <linux/file.h>
37 #include <linux/slab.h>
38 #include <linux/namei.h>
39 #include <linux/swap.h>
40 #include <linux/pagemap.h>
41 #include <linux/ratelimit.h>
42 #include <linux/sunrpc/svcauth_gss.h>
43 #include <linux/sunrpc/addr.h>
44 #include <linux/jhash.h>
45 #include <linux/string_helpers.h>
49 #include "current_stateid.h"
54 #define NFSDDBG_FACILITY NFSDDBG_PROC
56 #define all_ones {{~0,~0},~0}
57 static const stateid_t one_stateid = {
59 .si_opaque = all_ones,
61 static const stateid_t zero_stateid = {
64 static const stateid_t currentstateid = {
67 static const stateid_t close_stateid = {
68 .si_generation = 0xffffffffU,
71 static u64 current_sessionid = 1;
73 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
74 #define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
75 #define CURRENT_STATEID(stateid) (!memcmp((stateid), ¤tstateid, sizeof(stateid_t)))
76 #define CLOSE_STATEID(stateid) (!memcmp((stateid), &close_stateid, sizeof(stateid_t)))
78 /* forward declarations */
79 static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
80 static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
81 void nfsd4_end_grace(struct nfsd_net *nn);
86 * Currently used for the del_recall_lru and file hash table. In an
87 * effort to decrease the scope of the client_mutex, this spinlock may
88 * eventually cover more:
90 static DEFINE_SPINLOCK(state_lock);
92 enum nfsd4_st_mutex_lock_subclass {
93 OPEN_STATEID_MUTEX = 0,
94 LOCK_STATEID_MUTEX = 1,
98 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
99 * the refcount on the open stateid to drop.
101 static DECLARE_WAIT_QUEUE_HEAD(close_wq);
104 * A waitqueue where a writer to clients/#/ctl destroying a client can
105 * wait for cl_rpc_users to drop to 0 and then for the client to be
108 static DECLARE_WAIT_QUEUE_HEAD(expiry_wq);
110 static struct kmem_cache *client_slab;
111 static struct kmem_cache *openowner_slab;
112 static struct kmem_cache *lockowner_slab;
113 static struct kmem_cache *file_slab;
114 static struct kmem_cache *stateid_slab;
115 static struct kmem_cache *deleg_slab;
116 static struct kmem_cache *odstate_slab;
118 static void free_session(struct nfsd4_session *);
120 static const struct nfsd4_callback_ops nfsd4_cb_recall_ops;
121 static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops;
123 static bool is_session_dead(struct nfsd4_session *ses)
125 return ses->se_flags & NFS4_SESSION_DEAD;
128 static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
130 if (atomic_read(&ses->se_ref) > ref_held_by_me)
131 return nfserr_jukebox;
132 ses->se_flags |= NFS4_SESSION_DEAD;
136 static bool is_client_expired(struct nfs4_client *clp)
138 return clp->cl_time == 0;
141 static __be32 get_client_locked(struct nfs4_client *clp)
143 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
145 lockdep_assert_held(&nn->client_lock);
147 if (is_client_expired(clp))
148 return nfserr_expired;
149 atomic_inc(&clp->cl_rpc_users);
153 /* must be called under the client_lock */
155 renew_client_locked(struct nfs4_client *clp)
157 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
159 if (is_client_expired(clp)) {
161 printk("%s: client (clientid %08x/%08x) already expired\n",
163 clp->cl_clientid.cl_boot,
164 clp->cl_clientid.cl_id);
168 dprintk("renewing client (clientid %08x/%08x)\n",
169 clp->cl_clientid.cl_boot,
170 clp->cl_clientid.cl_id);
171 list_move_tail(&clp->cl_lru, &nn->client_lru);
172 clp->cl_time = get_seconds();
175 static void put_client_renew_locked(struct nfs4_client *clp)
177 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
179 lockdep_assert_held(&nn->client_lock);
181 if (!atomic_dec_and_test(&clp->cl_rpc_users))
183 if (!is_client_expired(clp))
184 renew_client_locked(clp);
186 wake_up_all(&expiry_wq);
189 static void put_client_renew(struct nfs4_client *clp)
191 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
193 if (!atomic_dec_and_lock(&clp->cl_rpc_users, &nn->client_lock))
195 if (!is_client_expired(clp))
196 renew_client_locked(clp);
198 wake_up_all(&expiry_wq);
199 spin_unlock(&nn->client_lock);
202 static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
206 if (is_session_dead(ses))
207 return nfserr_badsession;
208 status = get_client_locked(ses->se_client);
211 atomic_inc(&ses->se_ref);
215 static void nfsd4_put_session_locked(struct nfsd4_session *ses)
217 struct nfs4_client *clp = ses->se_client;
218 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
220 lockdep_assert_held(&nn->client_lock);
222 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
224 put_client_renew_locked(clp);
227 static void nfsd4_put_session(struct nfsd4_session *ses)
229 struct nfs4_client *clp = ses->se_client;
230 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
232 spin_lock(&nn->client_lock);
233 nfsd4_put_session_locked(ses);
234 spin_unlock(&nn->client_lock);
237 static struct nfsd4_blocked_lock *
238 find_blocked_lock(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
241 struct nfsd4_blocked_lock *cur, *found = NULL;
243 spin_lock(&nn->blocked_locks_lock);
244 list_for_each_entry(cur, &lo->lo_blocked, nbl_list) {
245 if (fh_match(fh, &cur->nbl_fh)) {
246 list_del_init(&cur->nbl_list);
247 list_del_init(&cur->nbl_lru);
252 spin_unlock(&nn->blocked_locks_lock);
254 locks_delete_block(&found->nbl_lock);
258 static struct nfsd4_blocked_lock *
259 find_or_allocate_block(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
262 struct nfsd4_blocked_lock *nbl;
264 nbl = find_blocked_lock(lo, fh, nn);
266 nbl= kmalloc(sizeof(*nbl), GFP_KERNEL);
268 fh_copy_shallow(&nbl->nbl_fh, fh);
269 locks_init_lock(&nbl->nbl_lock);
270 nfsd4_init_cb(&nbl->nbl_cb, lo->lo_owner.so_client,
271 &nfsd4_cb_notify_lock_ops,
272 NFSPROC4_CLNT_CB_NOTIFY_LOCK);
279 free_blocked_lock(struct nfsd4_blocked_lock *nbl)
281 locks_delete_block(&nbl->nbl_lock);
282 locks_release_private(&nbl->nbl_lock);
287 remove_blocked_locks(struct nfs4_lockowner *lo)
289 struct nfs4_client *clp = lo->lo_owner.so_client;
290 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
291 struct nfsd4_blocked_lock *nbl;
294 /* Dequeue all blocked locks */
295 spin_lock(&nn->blocked_locks_lock);
296 while (!list_empty(&lo->lo_blocked)) {
297 nbl = list_first_entry(&lo->lo_blocked,
298 struct nfsd4_blocked_lock,
300 list_del_init(&nbl->nbl_list);
301 list_move(&nbl->nbl_lru, &reaplist);
303 spin_unlock(&nn->blocked_locks_lock);
306 while (!list_empty(&reaplist)) {
307 nbl = list_first_entry(&reaplist, struct nfsd4_blocked_lock,
309 list_del_init(&nbl->nbl_lru);
310 free_blocked_lock(nbl);
315 nfsd4_cb_notify_lock_prepare(struct nfsd4_callback *cb)
317 struct nfsd4_blocked_lock *nbl = container_of(cb,
318 struct nfsd4_blocked_lock, nbl_cb);
319 locks_delete_block(&nbl->nbl_lock);
323 nfsd4_cb_notify_lock_done(struct nfsd4_callback *cb, struct rpc_task *task)
326 * Since this is just an optimization, we don't try very hard if it
327 * turns out not to succeed. We'll requeue it on NFS4ERR_DELAY, and
328 * just quit trying on anything else.
330 switch (task->tk_status) {
332 rpc_delay(task, 1 * HZ);
340 nfsd4_cb_notify_lock_release(struct nfsd4_callback *cb)
342 struct nfsd4_blocked_lock *nbl = container_of(cb,
343 struct nfsd4_blocked_lock, nbl_cb);
345 free_blocked_lock(nbl);
348 static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops = {
349 .prepare = nfsd4_cb_notify_lock_prepare,
350 .done = nfsd4_cb_notify_lock_done,
351 .release = nfsd4_cb_notify_lock_release,
354 static inline struct nfs4_stateowner *
355 nfs4_get_stateowner(struct nfs4_stateowner *sop)
357 atomic_inc(&sop->so_count);
362 same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
364 return (sop->so_owner.len == owner->len) &&
365 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
368 static struct nfs4_openowner *
369 find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
370 struct nfs4_client *clp)
372 struct nfs4_stateowner *so;
374 lockdep_assert_held(&clp->cl_lock);
376 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
378 if (!so->so_is_open_owner)
380 if (same_owner_str(so, &open->op_owner))
381 return openowner(nfs4_get_stateowner(so));
386 static struct nfs4_openowner *
387 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
388 struct nfs4_client *clp)
390 struct nfs4_openowner *oo;
392 spin_lock(&clp->cl_lock);
393 oo = find_openstateowner_str_locked(hashval, open, clp);
394 spin_unlock(&clp->cl_lock);
399 opaque_hashval(const void *ptr, int nbytes)
401 unsigned char *cptr = (unsigned char *) ptr;
411 static void nfsd4_free_file_rcu(struct rcu_head *rcu)
413 struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
415 kmem_cache_free(file_slab, fp);
419 put_nfs4_file(struct nfs4_file *fi)
421 might_lock(&state_lock);
423 if (refcount_dec_and_lock(&fi->fi_ref, &state_lock)) {
424 hlist_del_rcu(&fi->fi_hash);
425 spin_unlock(&state_lock);
426 WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
427 WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
428 call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
433 __nfs4_get_fd(struct nfs4_file *f, int oflag)
435 if (f->fi_fds[oflag])
436 return get_file(f->fi_fds[oflag]);
441 find_writeable_file_locked(struct nfs4_file *f)
445 lockdep_assert_held(&f->fi_lock);
447 ret = __nfs4_get_fd(f, O_WRONLY);
449 ret = __nfs4_get_fd(f, O_RDWR);
454 find_writeable_file(struct nfs4_file *f)
458 spin_lock(&f->fi_lock);
459 ret = find_writeable_file_locked(f);
460 spin_unlock(&f->fi_lock);
465 static struct file *find_readable_file_locked(struct nfs4_file *f)
469 lockdep_assert_held(&f->fi_lock);
471 ret = __nfs4_get_fd(f, O_RDONLY);
473 ret = __nfs4_get_fd(f, O_RDWR);
478 find_readable_file(struct nfs4_file *f)
482 spin_lock(&f->fi_lock);
483 ret = find_readable_file_locked(f);
484 spin_unlock(&f->fi_lock);
490 find_any_file(struct nfs4_file *f)
494 spin_lock(&f->fi_lock);
495 ret = __nfs4_get_fd(f, O_RDWR);
497 ret = __nfs4_get_fd(f, O_WRONLY);
499 ret = __nfs4_get_fd(f, O_RDONLY);
501 spin_unlock(&f->fi_lock);
505 static atomic_long_t num_delegations;
506 unsigned long max_delegations;
509 * Open owner state (share locks)
512 /* hash tables for lock and open owners */
513 #define OWNER_HASH_BITS 8
514 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
515 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
517 static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
521 ret = opaque_hashval(ownername->data, ownername->len);
522 return ret & OWNER_HASH_MASK;
525 /* hash table for nfs4_file */
526 #define FILE_HASH_BITS 8
527 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
529 static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
531 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
534 static unsigned int file_hashval(struct knfsd_fh *fh)
536 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
539 static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
542 __nfs4_file_get_access(struct nfs4_file *fp, u32 access)
544 lockdep_assert_held(&fp->fi_lock);
546 if (access & NFS4_SHARE_ACCESS_WRITE)
547 atomic_inc(&fp->fi_access[O_WRONLY]);
548 if (access & NFS4_SHARE_ACCESS_READ)
549 atomic_inc(&fp->fi_access[O_RDONLY]);
553 nfs4_file_get_access(struct nfs4_file *fp, u32 access)
555 lockdep_assert_held(&fp->fi_lock);
557 /* Does this access mode make sense? */
558 if (access & ~NFS4_SHARE_ACCESS_BOTH)
561 /* Does it conflict with a deny mode already set? */
562 if ((access & fp->fi_share_deny) != 0)
563 return nfserr_share_denied;
565 __nfs4_file_get_access(fp, access);
569 static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
571 /* Common case is that there is no deny mode. */
573 /* Does this deny mode make sense? */
574 if (deny & ~NFS4_SHARE_DENY_BOTH)
577 if ((deny & NFS4_SHARE_DENY_READ) &&
578 atomic_read(&fp->fi_access[O_RDONLY]))
579 return nfserr_share_denied;
581 if ((deny & NFS4_SHARE_DENY_WRITE) &&
582 atomic_read(&fp->fi_access[O_WRONLY]))
583 return nfserr_share_denied;
588 static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
590 might_lock(&fp->fi_lock);
592 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
593 struct file *f1 = NULL;
594 struct file *f2 = NULL;
596 swap(f1, fp->fi_fds[oflag]);
597 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
598 swap(f2, fp->fi_fds[O_RDWR]);
599 spin_unlock(&fp->fi_lock);
607 static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
609 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
611 if (access & NFS4_SHARE_ACCESS_WRITE)
612 __nfs4_file_put_access(fp, O_WRONLY);
613 if (access & NFS4_SHARE_ACCESS_READ)
614 __nfs4_file_put_access(fp, O_RDONLY);
618 * Allocate a new open/delegation state counter. This is needed for
619 * pNFS for proper return on close semantics.
621 * Note that we only allocate it for pNFS-enabled exports, otherwise
622 * all pointers to struct nfs4_clnt_odstate are always NULL.
624 static struct nfs4_clnt_odstate *
625 alloc_clnt_odstate(struct nfs4_client *clp)
627 struct nfs4_clnt_odstate *co;
629 co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
632 refcount_set(&co->co_odcount, 1);
638 hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
640 struct nfs4_file *fp = co->co_file;
642 lockdep_assert_held(&fp->fi_lock);
643 list_add(&co->co_perfile, &fp->fi_clnt_odstate);
647 get_clnt_odstate(struct nfs4_clnt_odstate *co)
650 refcount_inc(&co->co_odcount);
654 put_clnt_odstate(struct nfs4_clnt_odstate *co)
656 struct nfs4_file *fp;
662 if (refcount_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
663 list_del(&co->co_perfile);
664 spin_unlock(&fp->fi_lock);
666 nfsd4_return_all_file_layouts(co->co_client, fp);
667 kmem_cache_free(odstate_slab, co);
671 static struct nfs4_clnt_odstate *
672 find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
674 struct nfs4_clnt_odstate *co;
675 struct nfs4_client *cl;
682 spin_lock(&fp->fi_lock);
683 list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
684 if (co->co_client == cl) {
685 get_clnt_odstate(co);
691 hash_clnt_odstate_locked(new);
693 spin_unlock(&fp->fi_lock);
697 struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab,
698 void (*sc_free)(struct nfs4_stid *))
700 struct nfs4_stid *stid;
703 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
707 idr_preload(GFP_KERNEL);
708 spin_lock(&cl->cl_lock);
709 /* Reserving 0 for start of file in nfsdfs "states" file: */
710 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 1, 0, GFP_NOWAIT);
711 spin_unlock(&cl->cl_lock);
716 stid->sc_free = sc_free;
717 stid->sc_client = cl;
718 stid->sc_stateid.si_opaque.so_id = new_id;
719 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
720 /* Will be incremented before return to client: */
721 refcount_set(&stid->sc_count, 1);
722 spin_lock_init(&stid->sc_lock);
725 * It shouldn't be a problem to reuse an opaque stateid value.
726 * I don't think it is for 4.1. But with 4.0 I worry that, for
727 * example, a stray write retransmission could be accepted by
728 * the server when it should have been rejected. Therefore,
729 * adopt a trick from the sctp code to attempt to maximize the
730 * amount of time until an id is reused, by ensuring they always
731 * "increase" (mod INT_MAX):
735 kmem_cache_free(slab, stid);
740 * Create a unique stateid_t to represent each COPY.
742 int nfs4_init_cp_state(struct nfsd_net *nn, struct nfsd4_copy *copy)
746 idr_preload(GFP_KERNEL);
747 spin_lock(&nn->s2s_cp_lock);
748 new_id = idr_alloc_cyclic(&nn->s2s_cp_stateids, copy, 0, 0, GFP_NOWAIT);
749 spin_unlock(&nn->s2s_cp_lock);
753 copy->cp_stateid.si_opaque.so_id = new_id;
754 copy->cp_stateid.si_opaque.so_clid.cl_boot = nn->boot_time;
755 copy->cp_stateid.si_opaque.so_clid.cl_id = nn->s2s_cp_cl_id;
759 void nfs4_free_cp_state(struct nfsd4_copy *copy)
763 nn = net_generic(copy->cp_clp->net, nfsd_net_id);
764 spin_lock(&nn->s2s_cp_lock);
765 idr_remove(&nn->s2s_cp_stateids, copy->cp_stateid.si_opaque.so_id);
766 spin_unlock(&nn->s2s_cp_lock);
769 static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
771 struct nfs4_stid *stid;
773 stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid);
777 return openlockstateid(stid);
780 static void nfs4_free_deleg(struct nfs4_stid *stid)
782 kmem_cache_free(deleg_slab, stid);
783 atomic_long_dec(&num_delegations);
787 * When we recall a delegation, we should be careful not to hand it
788 * out again straight away.
789 * To ensure this we keep a pair of bloom filters ('new' and 'old')
790 * in which the filehandles of recalled delegations are "stored".
791 * If a filehandle appear in either filter, a delegation is blocked.
792 * When a delegation is recalled, the filehandle is stored in the "new"
794 * Every 30 seconds we swap the filters and clear the "new" one,
795 * unless both are empty of course.
797 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
798 * low 3 bytes as hash-table indices.
800 * 'blocked_delegations_lock', which is always taken in block_delegations(),
801 * is used to manage concurrent access. Testing does not need the lock
802 * except when swapping the two filters.
804 static DEFINE_SPINLOCK(blocked_delegations_lock);
805 static struct bloom_pair {
806 int entries, old_entries;
808 int new; /* index into 'set' */
809 DECLARE_BITMAP(set[2], 256);
810 } blocked_delegations;
812 static int delegation_blocked(struct knfsd_fh *fh)
815 struct bloom_pair *bd = &blocked_delegations;
817 if (bd->entries == 0)
819 if (seconds_since_boot() - bd->swap_time > 30) {
820 spin_lock(&blocked_delegations_lock);
821 if (seconds_since_boot() - bd->swap_time > 30) {
822 bd->entries -= bd->old_entries;
823 bd->old_entries = bd->entries;
824 memset(bd->set[bd->new], 0,
827 bd->swap_time = seconds_since_boot();
829 spin_unlock(&blocked_delegations_lock);
831 hash = jhash(&fh->fh_base, fh->fh_size, 0);
832 if (test_bit(hash&255, bd->set[0]) &&
833 test_bit((hash>>8)&255, bd->set[0]) &&
834 test_bit((hash>>16)&255, bd->set[0]))
837 if (test_bit(hash&255, bd->set[1]) &&
838 test_bit((hash>>8)&255, bd->set[1]) &&
839 test_bit((hash>>16)&255, bd->set[1]))
845 static void block_delegations(struct knfsd_fh *fh)
848 struct bloom_pair *bd = &blocked_delegations;
850 hash = jhash(&fh->fh_base, fh->fh_size, 0);
852 spin_lock(&blocked_delegations_lock);
853 __set_bit(hash&255, bd->set[bd->new]);
854 __set_bit((hash>>8)&255, bd->set[bd->new]);
855 __set_bit((hash>>16)&255, bd->set[bd->new]);
856 if (bd->entries == 0)
857 bd->swap_time = seconds_since_boot();
859 spin_unlock(&blocked_delegations_lock);
862 static struct nfs4_delegation *
863 alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
864 struct svc_fh *current_fh,
865 struct nfs4_clnt_odstate *odstate)
867 struct nfs4_delegation *dp;
870 dprintk("NFSD alloc_init_deleg\n");
871 n = atomic_long_inc_return(&num_delegations);
872 if (n < 0 || n > max_delegations)
874 if (delegation_blocked(¤t_fh->fh_handle))
876 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg));
881 * delegation seqid's are never incremented. The 4.1 special
882 * meaning of seqid 0 isn't meaningful, really, but let's avoid
883 * 0 anyway just for consistency and use 1:
885 dp->dl_stid.sc_stateid.si_generation = 1;
886 INIT_LIST_HEAD(&dp->dl_perfile);
887 INIT_LIST_HEAD(&dp->dl_perclnt);
888 INIT_LIST_HEAD(&dp->dl_recall_lru);
889 dp->dl_clnt_odstate = odstate;
890 get_clnt_odstate(odstate);
891 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
893 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
894 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
896 dp->dl_stid.sc_file = fp;
899 atomic_long_dec(&num_delegations);
904 nfs4_put_stid(struct nfs4_stid *s)
906 struct nfs4_file *fp = s->sc_file;
907 struct nfs4_client *clp = s->sc_client;
909 might_lock(&clp->cl_lock);
911 if (!refcount_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
912 wake_up_all(&close_wq);
915 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
916 spin_unlock(&clp->cl_lock);
923 nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
925 stateid_t *src = &stid->sc_stateid;
927 spin_lock(&stid->sc_lock);
928 if (unlikely(++src->si_generation == 0))
929 src->si_generation = 1;
930 memcpy(dst, src, sizeof(*dst));
931 spin_unlock(&stid->sc_lock);
934 static void put_deleg_file(struct nfs4_file *fp)
936 struct file *filp = NULL;
938 spin_lock(&fp->fi_lock);
939 if (--fp->fi_delegees == 0)
940 swap(filp, fp->fi_deleg_file);
941 spin_unlock(&fp->fi_lock);
947 static void nfs4_unlock_deleg_lease(struct nfs4_delegation *dp)
949 struct nfs4_file *fp = dp->dl_stid.sc_file;
950 struct file *filp = fp->fi_deleg_file;
952 WARN_ON_ONCE(!fp->fi_delegees);
954 vfs_setlease(filp, F_UNLCK, NULL, (void **)&dp);
958 static void destroy_unhashed_deleg(struct nfs4_delegation *dp)
960 put_clnt_odstate(dp->dl_clnt_odstate);
961 nfs4_unlock_deleg_lease(dp);
962 nfs4_put_stid(&dp->dl_stid);
965 void nfs4_unhash_stid(struct nfs4_stid *s)
971 * nfs4_delegation_exists - Discover if this delegation already exists
972 * @clp: a pointer to the nfs4_client we're granting a delegation to
973 * @fp: a pointer to the nfs4_file we're granting a delegation on
976 * On success: true iff an existing delegation is found
980 nfs4_delegation_exists(struct nfs4_client *clp, struct nfs4_file *fp)
982 struct nfs4_delegation *searchdp = NULL;
983 struct nfs4_client *searchclp = NULL;
985 lockdep_assert_held(&state_lock);
986 lockdep_assert_held(&fp->fi_lock);
988 list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
989 searchclp = searchdp->dl_stid.sc_client;
990 if (clp == searchclp) {
998 * hash_delegation_locked - Add a delegation to the appropriate lists
999 * @dp: a pointer to the nfs4_delegation we are adding.
1000 * @fp: a pointer to the nfs4_file we're granting a delegation on
1003 * On success: NULL if the delegation was successfully hashed.
1005 * On error: -EAGAIN if one was previously granted to this
1006 * nfs4_client for this nfs4_file. Delegation is not hashed.
1011 hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
1013 struct nfs4_client *clp = dp->dl_stid.sc_client;
1015 lockdep_assert_held(&state_lock);
1016 lockdep_assert_held(&fp->fi_lock);
1018 if (nfs4_delegation_exists(clp, fp))
1020 refcount_inc(&dp->dl_stid.sc_count);
1021 dp->dl_stid.sc_type = NFS4_DELEG_STID;
1022 list_add(&dp->dl_perfile, &fp->fi_delegations);
1023 list_add(&dp->dl_perclnt, &clp->cl_delegations);
1028 unhash_delegation_locked(struct nfs4_delegation *dp)
1030 struct nfs4_file *fp = dp->dl_stid.sc_file;
1032 lockdep_assert_held(&state_lock);
1034 if (list_empty(&dp->dl_perfile))
1037 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
1038 /* Ensure that deleg break won't try to requeue it */
1040 spin_lock(&fp->fi_lock);
1041 list_del_init(&dp->dl_perclnt);
1042 list_del_init(&dp->dl_recall_lru);
1043 list_del_init(&dp->dl_perfile);
1044 spin_unlock(&fp->fi_lock);
1048 static void destroy_delegation(struct nfs4_delegation *dp)
1052 spin_lock(&state_lock);
1053 unhashed = unhash_delegation_locked(dp);
1054 spin_unlock(&state_lock);
1056 destroy_unhashed_deleg(dp);
1059 static void revoke_delegation(struct nfs4_delegation *dp)
1061 struct nfs4_client *clp = dp->dl_stid.sc_client;
1063 WARN_ON(!list_empty(&dp->dl_recall_lru));
1065 if (clp->cl_minorversion) {
1066 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
1067 refcount_inc(&dp->dl_stid.sc_count);
1068 spin_lock(&clp->cl_lock);
1069 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
1070 spin_unlock(&clp->cl_lock);
1072 destroy_unhashed_deleg(dp);
1079 static unsigned int clientid_hashval(u32 id)
1081 return id & CLIENT_HASH_MASK;
1084 static unsigned int clientstr_hashval(struct xdr_netobj name)
1086 return opaque_hashval(name.data, 8) & CLIENT_HASH_MASK;
1090 * We store the NONE, READ, WRITE, and BOTH bits separately in the
1091 * st_{access,deny}_bmap field of the stateid, in order to track not
1092 * only what share bits are currently in force, but also what
1093 * combinations of share bits previous opens have used. This allows us
1094 * to enforce the recommendation of rfc 3530 14.2.19 that the server
1095 * return an error if the client attempt to downgrade to a combination
1096 * of share bits not explicable by closing some of its previous opens.
1098 * XXX: This enforcement is actually incomplete, since we don't keep
1099 * track of access/deny bit combinations; so, e.g., we allow:
1101 * OPEN allow read, deny write
1102 * OPEN allow both, deny none
1103 * DOWNGRADE allow read, deny none
1105 * which we should reject.
1108 bmap_to_share_mode(unsigned long bmap) {
1110 unsigned int access = 0;
1112 for (i = 1; i < 4; i++) {
1113 if (test_bit(i, &bmap))
1119 /* set share access for a given stateid */
1121 set_access(u32 access, struct nfs4_ol_stateid *stp)
1123 unsigned char mask = 1 << access;
1125 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1126 stp->st_access_bmap |= mask;
1129 /* clear share access for a given stateid */
1131 clear_access(u32 access, struct nfs4_ol_stateid *stp)
1133 unsigned char mask = 1 << access;
1135 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1136 stp->st_access_bmap &= ~mask;
1139 /* test whether a given stateid has access */
1141 test_access(u32 access, struct nfs4_ol_stateid *stp)
1143 unsigned char mask = 1 << access;
1145 return (bool)(stp->st_access_bmap & mask);
1148 /* set share deny for a given stateid */
1150 set_deny(u32 deny, struct nfs4_ol_stateid *stp)
1152 unsigned char mask = 1 << deny;
1154 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1155 stp->st_deny_bmap |= mask;
1158 /* clear share deny for a given stateid */
1160 clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
1162 unsigned char mask = 1 << deny;
1164 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1165 stp->st_deny_bmap &= ~mask;
1168 /* test whether a given stateid is denying specific access */
1170 test_deny(u32 deny, struct nfs4_ol_stateid *stp)
1172 unsigned char mask = 1 << deny;
1174 return (bool)(stp->st_deny_bmap & mask);
1177 static int nfs4_access_to_omode(u32 access)
1179 switch (access & NFS4_SHARE_ACCESS_BOTH) {
1180 case NFS4_SHARE_ACCESS_READ:
1182 case NFS4_SHARE_ACCESS_WRITE:
1184 case NFS4_SHARE_ACCESS_BOTH:
1192 * A stateid that had a deny mode associated with it is being released
1193 * or downgraded. Recalculate the deny mode on the file.
1196 recalculate_deny_mode(struct nfs4_file *fp)
1198 struct nfs4_ol_stateid *stp;
1200 spin_lock(&fp->fi_lock);
1201 fp->fi_share_deny = 0;
1202 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
1203 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
1204 spin_unlock(&fp->fi_lock);
1208 reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
1211 bool change = false;
1213 for (i = 1; i < 4; i++) {
1214 if ((i & deny) != i) {
1220 /* Recalculate per-file deny mode if there was a change */
1222 recalculate_deny_mode(stp->st_stid.sc_file);
1225 /* release all access and file references for a given stateid */
1227 release_all_access(struct nfs4_ol_stateid *stp)
1230 struct nfs4_file *fp = stp->st_stid.sc_file;
1232 if (fp && stp->st_deny_bmap != 0)
1233 recalculate_deny_mode(fp);
1235 for (i = 1; i < 4; i++) {
1236 if (test_access(i, stp))
1237 nfs4_file_put_access(stp->st_stid.sc_file, i);
1238 clear_access(i, stp);
1242 static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1244 kfree(sop->so_owner.data);
1245 sop->so_ops->so_free(sop);
1248 static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1250 struct nfs4_client *clp = sop->so_client;
1252 might_lock(&clp->cl_lock);
1254 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
1256 sop->so_ops->so_unhash(sop);
1257 spin_unlock(&clp->cl_lock);
1258 nfs4_free_stateowner(sop);
1261 static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
1263 struct nfs4_file *fp = stp->st_stid.sc_file;
1265 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1267 if (list_empty(&stp->st_perfile))
1270 spin_lock(&fp->fi_lock);
1271 list_del_init(&stp->st_perfile);
1272 spin_unlock(&fp->fi_lock);
1273 list_del(&stp->st_perstateowner);
1277 static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
1279 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1281 put_clnt_odstate(stp->st_clnt_odstate);
1282 release_all_access(stp);
1283 if (stp->st_stateowner)
1284 nfs4_put_stateowner(stp->st_stateowner);
1285 kmem_cache_free(stateid_slab, stid);
1288 static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
1290 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1291 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
1294 file = find_any_file(stp->st_stid.sc_file);
1296 filp_close(file, (fl_owner_t)lo);
1297 nfs4_free_ol_stateid(stid);
1301 * Put the persistent reference to an already unhashed generic stateid, while
1302 * holding the cl_lock. If it's the last reference, then put it onto the
1303 * reaplist for later destruction.
1305 static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1306 struct list_head *reaplist)
1308 struct nfs4_stid *s = &stp->st_stid;
1309 struct nfs4_client *clp = s->sc_client;
1311 lockdep_assert_held(&clp->cl_lock);
1313 WARN_ON_ONCE(!list_empty(&stp->st_locks));
1315 if (!refcount_dec_and_test(&s->sc_count)) {
1316 wake_up_all(&close_wq);
1320 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1321 list_add(&stp->st_locks, reaplist);
1324 static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
1326 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1328 list_del_init(&stp->st_locks);
1329 nfs4_unhash_stid(&stp->st_stid);
1330 return unhash_ol_stateid(stp);
1333 static void release_lock_stateid(struct nfs4_ol_stateid *stp)
1335 struct nfs4_client *clp = stp->st_stid.sc_client;
1338 spin_lock(&clp->cl_lock);
1339 unhashed = unhash_lock_stateid(stp);
1340 spin_unlock(&clp->cl_lock);
1342 nfs4_put_stid(&stp->st_stid);
1345 static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
1347 struct nfs4_client *clp = lo->lo_owner.so_client;
1349 lockdep_assert_held(&clp->cl_lock);
1351 list_del_init(&lo->lo_owner.so_strhash);
1355 * Free a list of generic stateids that were collected earlier after being
1359 free_ol_stateid_reaplist(struct list_head *reaplist)
1361 struct nfs4_ol_stateid *stp;
1362 struct nfs4_file *fp;
1366 while (!list_empty(reaplist)) {
1367 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1369 list_del(&stp->st_locks);
1370 fp = stp->st_stid.sc_file;
1371 stp->st_stid.sc_free(&stp->st_stid);
1377 static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1378 struct list_head *reaplist)
1380 struct nfs4_ol_stateid *stp;
1382 lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1384 while (!list_empty(&open_stp->st_locks)) {
1385 stp = list_entry(open_stp->st_locks.next,
1386 struct nfs4_ol_stateid, st_locks);
1387 WARN_ON(!unhash_lock_stateid(stp));
1388 put_ol_stateid_locked(stp, reaplist);
1392 static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
1393 struct list_head *reaplist)
1397 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1399 unhashed = unhash_ol_stateid(stp);
1400 release_open_stateid_locks(stp, reaplist);
1404 static void release_open_stateid(struct nfs4_ol_stateid *stp)
1406 LIST_HEAD(reaplist);
1408 spin_lock(&stp->st_stid.sc_client->cl_lock);
1409 if (unhash_open_stateid(stp, &reaplist))
1410 put_ol_stateid_locked(stp, &reaplist);
1411 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1412 free_ol_stateid_reaplist(&reaplist);
1415 static void unhash_openowner_locked(struct nfs4_openowner *oo)
1417 struct nfs4_client *clp = oo->oo_owner.so_client;
1419 lockdep_assert_held(&clp->cl_lock);
1421 list_del_init(&oo->oo_owner.so_strhash);
1422 list_del_init(&oo->oo_perclient);
1425 static void release_last_closed_stateid(struct nfs4_openowner *oo)
1427 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1429 struct nfs4_ol_stateid *s;
1431 spin_lock(&nn->client_lock);
1432 s = oo->oo_last_closed_stid;
1434 list_del_init(&oo->oo_close_lru);
1435 oo->oo_last_closed_stid = NULL;
1437 spin_unlock(&nn->client_lock);
1439 nfs4_put_stid(&s->st_stid);
1442 static void release_openowner(struct nfs4_openowner *oo)
1444 struct nfs4_ol_stateid *stp;
1445 struct nfs4_client *clp = oo->oo_owner.so_client;
1446 struct list_head reaplist;
1448 INIT_LIST_HEAD(&reaplist);
1450 spin_lock(&clp->cl_lock);
1451 unhash_openowner_locked(oo);
1452 while (!list_empty(&oo->oo_owner.so_stateids)) {
1453 stp = list_first_entry(&oo->oo_owner.so_stateids,
1454 struct nfs4_ol_stateid, st_perstateowner);
1455 if (unhash_open_stateid(stp, &reaplist))
1456 put_ol_stateid_locked(stp, &reaplist);
1458 spin_unlock(&clp->cl_lock);
1459 free_ol_stateid_reaplist(&reaplist);
1460 release_last_closed_stateid(oo);
1461 nfs4_put_stateowner(&oo->oo_owner);
1465 hash_sessionid(struct nfs4_sessionid *sessionid)
1467 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1469 return sid->sequence % SESSION_HASH_SIZE;
1472 #ifdef CONFIG_SUNRPC_DEBUG
1474 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1476 u32 *ptr = (u32 *)(&sessionid->data[0]);
1477 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1481 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1487 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1488 * won't be used for replay.
1490 void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1492 struct nfs4_stateowner *so = cstate->replay_owner;
1494 if (nfserr == nfserr_replay_me)
1497 if (!seqid_mutating_err(ntohl(nfserr))) {
1498 nfsd4_cstate_clear_replay(cstate);
1503 if (so->so_is_open_owner)
1504 release_last_closed_stateid(openowner(so));
1510 gen_sessionid(struct nfsd4_session *ses)
1512 struct nfs4_client *clp = ses->se_client;
1513 struct nfsd4_sessionid *sid;
1515 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1516 sid->clientid = clp->cl_clientid;
1517 sid->sequence = current_sessionid++;
1522 * The protocol defines ca_maxresponssize_cached to include the size of
1523 * the rpc header, but all we need to cache is the data starting after
1524 * the end of the initial SEQUENCE operation--the rest we regenerate
1525 * each time. Therefore we can advertise a ca_maxresponssize_cached
1526 * value that is the number of bytes in our cache plus a few additional
1527 * bytes. In order to stay on the safe side, and not promise more than
1528 * we can cache, those additional bytes must be the minimum possible: 24
1529 * bytes of rpc header (xid through accept state, with AUTH_NULL
1530 * verifier), 12 for the compound header (with zero-length tag), and 44
1531 * for the SEQUENCE op response:
1533 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1536 free_session_slots(struct nfsd4_session *ses)
1540 for (i = 0; i < ses->se_fchannel.maxreqs; i++) {
1541 free_svc_cred(&ses->se_slots[i]->sl_cred);
1542 kfree(ses->se_slots[i]);
1547 * We don't actually need to cache the rpc and session headers, so we
1548 * can allocate a little less for each slot:
1550 static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
1554 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1557 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1558 return size + sizeof(struct nfsd4_slot);
1562 * XXX: If we run out of reserved DRC memory we could (up to a point)
1563 * re-negotiate active sessions and reduce their slot usage to make
1564 * room for new connections. For now we just fail the create session.
1566 static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
1568 u32 slotsize = slot_bytes(ca);
1569 u32 num = ca->maxreqs;
1570 unsigned long avail, total_avail;
1572 spin_lock(&nfsd_drc_lock);
1573 total_avail = nfsd_drc_max_mem - nfsd_drc_mem_used;
1574 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, total_avail);
1576 * Never use more than a third of the remaining memory,
1577 * unless it's the only way to give this client a slot:
1579 avail = clamp_t(unsigned long, avail, slotsize, total_avail/3);
1580 num = min_t(int, num, avail / slotsize);
1581 nfsd_drc_mem_used += num * slotsize;
1582 spin_unlock(&nfsd_drc_lock);
1587 static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
1589 int slotsize = slot_bytes(ca);
1591 spin_lock(&nfsd_drc_lock);
1592 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
1593 spin_unlock(&nfsd_drc_lock);
1596 static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1597 struct nfsd4_channel_attrs *battrs)
1599 int numslots = fattrs->maxreqs;
1600 int slotsize = slot_bytes(fattrs);
1601 struct nfsd4_session *new;
1604 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
1605 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1606 mem = numslots * sizeof(struct nfsd4_slot *);
1608 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
1611 /* allocate each struct nfsd4_slot and data cache in one piece */
1612 for (i = 0; i < numslots; i++) {
1613 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
1614 if (!new->se_slots[i])
1618 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1619 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1624 kfree(new->se_slots[i]);
1629 static void free_conn(struct nfsd4_conn *c)
1631 svc_xprt_put(c->cn_xprt);
1635 static void nfsd4_conn_lost(struct svc_xpt_user *u)
1637 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1638 struct nfs4_client *clp = c->cn_session->se_client;
1640 spin_lock(&clp->cl_lock);
1641 if (!list_empty(&c->cn_persession)) {
1642 list_del(&c->cn_persession);
1645 nfsd4_probe_callback(clp);
1646 spin_unlock(&clp->cl_lock);
1649 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
1651 struct nfsd4_conn *conn;
1653 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1656 svc_xprt_get(rqstp->rq_xprt);
1657 conn->cn_xprt = rqstp->rq_xprt;
1658 conn->cn_flags = flags;
1659 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1663 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1665 conn->cn_session = ses;
1666 list_add(&conn->cn_persession, &ses->se_conns);
1669 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1671 struct nfs4_client *clp = ses->se_client;
1673 spin_lock(&clp->cl_lock);
1674 __nfsd4_hash_conn(conn, ses);
1675 spin_unlock(&clp->cl_lock);
1678 static int nfsd4_register_conn(struct nfsd4_conn *conn)
1680 conn->cn_xpt_user.callback = nfsd4_conn_lost;
1681 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
1684 static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
1688 nfsd4_hash_conn(conn, ses);
1689 ret = nfsd4_register_conn(conn);
1691 /* oops; xprt is already down: */
1692 nfsd4_conn_lost(&conn->cn_xpt_user);
1693 /* We may have gained or lost a callback channel: */
1694 nfsd4_probe_callback_sync(ses->se_client);
1697 static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
1699 u32 dir = NFS4_CDFC4_FORE;
1701 if (cses->flags & SESSION4_BACK_CHAN)
1702 dir |= NFS4_CDFC4_BACK;
1703 return alloc_conn(rqstp, dir);
1706 /* must be called under client_lock */
1707 static void nfsd4_del_conns(struct nfsd4_session *s)
1709 struct nfs4_client *clp = s->se_client;
1710 struct nfsd4_conn *c;
1712 spin_lock(&clp->cl_lock);
1713 while (!list_empty(&s->se_conns)) {
1714 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1715 list_del_init(&c->cn_persession);
1716 spin_unlock(&clp->cl_lock);
1718 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1721 spin_lock(&clp->cl_lock);
1723 spin_unlock(&clp->cl_lock);
1726 static void __free_session(struct nfsd4_session *ses)
1728 free_session_slots(ses);
1732 static void free_session(struct nfsd4_session *ses)
1734 nfsd4_del_conns(ses);
1735 nfsd4_put_drc_mem(&ses->se_fchannel);
1736 __free_session(ses);
1739 static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
1742 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1744 new->se_client = clp;
1747 INIT_LIST_HEAD(&new->se_conns);
1749 new->se_cb_seq_nr = 1;
1750 new->se_flags = cses->flags;
1751 new->se_cb_prog = cses->callback_prog;
1752 new->se_cb_sec = cses->cb_sec;
1753 atomic_set(&new->se_ref, 0);
1754 idx = hash_sessionid(&new->se_sessionid);
1755 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
1756 spin_lock(&clp->cl_lock);
1757 list_add(&new->se_perclnt, &clp->cl_sessions);
1758 spin_unlock(&clp->cl_lock);
1761 struct sockaddr *sa = svc_addr(rqstp);
1763 * This is a little silly; with sessions there's no real
1764 * use for the callback address. Use the peer address
1765 * as a reasonable default for now, but consider fixing
1766 * the rpc client not to require an address in the
1769 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1770 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
1774 /* caller must hold client_lock */
1775 static struct nfsd4_session *
1776 __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
1778 struct nfsd4_session *elem;
1780 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1782 lockdep_assert_held(&nn->client_lock);
1784 dump_sessionid(__func__, sessionid);
1785 idx = hash_sessionid(sessionid);
1786 /* Search in the appropriate list */
1787 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
1788 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1789 NFS4_MAX_SESSIONID_LEN)) {
1794 dprintk("%s: session not found\n", __func__);
1798 static struct nfsd4_session *
1799 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1802 struct nfsd4_session *session;
1803 __be32 status = nfserr_badsession;
1805 session = __find_in_sessionid_hashtbl(sessionid, net);
1808 status = nfsd4_get_session_locked(session);
1816 /* caller must hold client_lock */
1818 unhash_session(struct nfsd4_session *ses)
1820 struct nfs4_client *clp = ses->se_client;
1821 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1823 lockdep_assert_held(&nn->client_lock);
1825 list_del(&ses->se_hash);
1826 spin_lock(&ses->se_client->cl_lock);
1827 list_del(&ses->se_perclnt);
1828 spin_unlock(&ses->se_client->cl_lock);
1831 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1833 STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
1836 * We're assuming the clid was not given out from a boot
1837 * precisely 2^32 (about 136 years) before this one. That seems
1838 * a safe assumption:
1840 if (clid->cl_boot == (u32)nn->boot_time)
1842 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
1843 clid->cl_boot, clid->cl_id, nn->boot_time);
1848 * XXX Should we use a slab cache ?
1849 * This type of memory management is somewhat inefficient, but we use it
1850 * anyway since SETCLIENTID is not a common operation.
1852 static struct nfs4_client *alloc_client(struct xdr_netobj name)
1854 struct nfs4_client *clp;
1857 clp = kmem_cache_zalloc(client_slab, GFP_KERNEL);
1860 xdr_netobj_dup(&clp->cl_name, &name, GFP_KERNEL);
1861 if (clp->cl_name.data == NULL)
1863 clp->cl_ownerstr_hashtbl = kmalloc_array(OWNER_HASH_SIZE,
1864 sizeof(struct list_head),
1866 if (!clp->cl_ownerstr_hashtbl)
1867 goto err_no_hashtbl;
1868 for (i = 0; i < OWNER_HASH_SIZE; i++)
1869 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
1870 INIT_LIST_HEAD(&clp->cl_sessions);
1871 idr_init(&clp->cl_stateids);
1872 atomic_set(&clp->cl_rpc_users, 0);
1873 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1874 INIT_LIST_HEAD(&clp->cl_idhash);
1875 INIT_LIST_HEAD(&clp->cl_openowners);
1876 INIT_LIST_HEAD(&clp->cl_delegations);
1877 INIT_LIST_HEAD(&clp->cl_lru);
1878 INIT_LIST_HEAD(&clp->cl_revoked);
1879 #ifdef CONFIG_NFSD_PNFS
1880 INIT_LIST_HEAD(&clp->cl_lo_states);
1882 INIT_LIST_HEAD(&clp->async_copies);
1883 spin_lock_init(&clp->async_lock);
1884 spin_lock_init(&clp->cl_lock);
1885 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1888 kfree(clp->cl_name.data);
1890 kmem_cache_free(client_slab, clp);
1894 static void __free_client(struct kref *k)
1896 struct nfsdfs_client *c = container_of(k, struct nfsdfs_client, cl_ref);
1897 struct nfs4_client *clp = container_of(c, struct nfs4_client, cl_nfsdfs);
1899 free_svc_cred(&clp->cl_cred);
1900 kfree(clp->cl_ownerstr_hashtbl);
1901 kfree(clp->cl_name.data);
1902 kfree(clp->cl_nii_domain.data);
1903 kfree(clp->cl_nii_name.data);
1904 idr_destroy(&clp->cl_stateids);
1905 kmem_cache_free(client_slab, clp);
1908 static void drop_client(struct nfs4_client *clp)
1910 kref_put(&clp->cl_nfsdfs.cl_ref, __free_client);
1914 free_client(struct nfs4_client *clp)
1916 while (!list_empty(&clp->cl_sessions)) {
1917 struct nfsd4_session *ses;
1918 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1920 list_del(&ses->se_perclnt);
1921 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1924 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
1925 if (clp->cl_nfsd_dentry) {
1926 nfsd_client_rmdir(clp->cl_nfsd_dentry);
1927 clp->cl_nfsd_dentry = NULL;
1928 wake_up_all(&expiry_wq);
1933 /* must be called under the client_lock */
1935 unhash_client_locked(struct nfs4_client *clp)
1937 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1938 struct nfsd4_session *ses;
1940 lockdep_assert_held(&nn->client_lock);
1942 /* Mark the client as expired! */
1944 /* Make it invisible */
1945 if (!list_empty(&clp->cl_idhash)) {
1946 list_del_init(&clp->cl_idhash);
1947 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1948 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1950 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1952 list_del_init(&clp->cl_lru);
1953 spin_lock(&clp->cl_lock);
1954 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1955 list_del_init(&ses->se_hash);
1956 spin_unlock(&clp->cl_lock);
1960 unhash_client(struct nfs4_client *clp)
1962 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1964 spin_lock(&nn->client_lock);
1965 unhash_client_locked(clp);
1966 spin_unlock(&nn->client_lock);
1969 static __be32 mark_client_expired_locked(struct nfs4_client *clp)
1971 if (atomic_read(&clp->cl_rpc_users))
1972 return nfserr_jukebox;
1973 unhash_client_locked(clp);
1978 __destroy_client(struct nfs4_client *clp)
1981 struct nfs4_openowner *oo;
1982 struct nfs4_delegation *dp;
1983 struct list_head reaplist;
1985 INIT_LIST_HEAD(&reaplist);
1986 spin_lock(&state_lock);
1987 while (!list_empty(&clp->cl_delegations)) {
1988 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
1989 WARN_ON(!unhash_delegation_locked(dp));
1990 list_add(&dp->dl_recall_lru, &reaplist);
1992 spin_unlock(&state_lock);
1993 while (!list_empty(&reaplist)) {
1994 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
1995 list_del_init(&dp->dl_recall_lru);
1996 destroy_unhashed_deleg(dp);
1998 while (!list_empty(&clp->cl_revoked)) {
1999 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
2000 list_del_init(&dp->dl_recall_lru);
2001 nfs4_put_stid(&dp->dl_stid);
2003 while (!list_empty(&clp->cl_openowners)) {
2004 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
2005 nfs4_get_stateowner(&oo->oo_owner);
2006 release_openowner(oo);
2008 for (i = 0; i < OWNER_HASH_SIZE; i++) {
2009 struct nfs4_stateowner *so, *tmp;
2011 list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i],
2013 /* Should be no openowners at this point */
2014 WARN_ON_ONCE(so->so_is_open_owner);
2015 remove_blocked_locks(lockowner(so));
2018 nfsd4_return_all_client_layouts(clp);
2019 nfsd4_shutdown_copy(clp);
2020 nfsd4_shutdown_callback(clp);
2021 if (clp->cl_cb_conn.cb_xprt)
2022 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
2024 wake_up_all(&expiry_wq);
2028 destroy_client(struct nfs4_client *clp)
2031 __destroy_client(clp);
2034 static void inc_reclaim_complete(struct nfs4_client *clp)
2036 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2038 if (!nn->track_reclaim_completes)
2040 if (!nfsd4_find_reclaim_client(clp->cl_name, nn))
2042 if (atomic_inc_return(&nn->nr_reclaim_complete) ==
2043 nn->reclaim_str_hashtbl_size) {
2044 printk(KERN_INFO "NFSD: all clients done reclaiming, ending NFSv4 grace period (net %x)\n",
2046 nfsd4_end_grace(nn);
2050 static void expire_client(struct nfs4_client *clp)
2053 nfsd4_client_record_remove(clp);
2054 __destroy_client(clp);
2057 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
2059 memcpy(target->cl_verifier.data, source->data,
2060 sizeof(target->cl_verifier.data));
2063 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
2065 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
2066 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
2069 static int copy_cred(struct svc_cred *target, struct svc_cred *source)
2071 target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL);
2072 target->cr_raw_principal = kstrdup(source->cr_raw_principal,
2074 target->cr_targ_princ = kstrdup(source->cr_targ_princ, GFP_KERNEL);
2075 if ((source->cr_principal && !target->cr_principal) ||
2076 (source->cr_raw_principal && !target->cr_raw_principal) ||
2077 (source->cr_targ_princ && !target->cr_targ_princ))
2080 target->cr_flavor = source->cr_flavor;
2081 target->cr_uid = source->cr_uid;
2082 target->cr_gid = source->cr_gid;
2083 target->cr_group_info = source->cr_group_info;
2084 get_group_info(target->cr_group_info);
2085 target->cr_gss_mech = source->cr_gss_mech;
2086 if (source->cr_gss_mech)
2087 gss_mech_get(source->cr_gss_mech);
2092 compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
2094 if (o1->len < o2->len)
2096 if (o1->len > o2->len)
2098 return memcmp(o1->data, o2->data, o1->len);
2102 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
2104 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
2108 same_clid(clientid_t *cl1, clientid_t *cl2)
2110 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
2113 static bool groups_equal(struct group_info *g1, struct group_info *g2)
2117 if (g1->ngroups != g2->ngroups)
2119 for (i=0; i<g1->ngroups; i++)
2120 if (!gid_eq(g1->gid[i], g2->gid[i]))
2126 * RFC 3530 language requires clid_inuse be returned when the
2127 * "principal" associated with a requests differs from that previously
2128 * used. We use uid, gid's, and gss principal string as our best
2129 * approximation. We also don't want to allow non-gss use of a client
2130 * established using gss: in theory cr_principal should catch that
2131 * change, but in practice cr_principal can be null even in the gss case
2132 * since gssd doesn't always pass down a principal string.
2134 static bool is_gss_cred(struct svc_cred *cr)
2136 /* Is cr_flavor one of the gss "pseudoflavors"?: */
2137 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
2142 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
2144 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
2145 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
2146 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
2147 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
2149 /* XXX: check that cr_targ_princ fields match ? */
2150 if (cr1->cr_principal == cr2->cr_principal)
2152 if (!cr1->cr_principal || !cr2->cr_principal)
2154 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
2157 static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
2159 struct svc_cred *cr = &rqstp->rq_cred;
2162 if (!cr->cr_gss_mech)
2164 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
2165 return service == RPC_GSS_SVC_INTEGRITY ||
2166 service == RPC_GSS_SVC_PRIVACY;
2169 bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
2171 struct svc_cred *cr = &rqstp->rq_cred;
2173 if (!cl->cl_mach_cred)
2175 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
2177 if (!svc_rqst_integrity_protected(rqstp))
2179 if (cl->cl_cred.cr_raw_principal)
2180 return 0 == strcmp(cl->cl_cred.cr_raw_principal,
2181 cr->cr_raw_principal);
2182 if (!cr->cr_principal)
2184 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
2187 static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
2192 * This is opaque to client, so no need to byte-swap. Use
2193 * __force to keep sparse happy
2195 verf[0] = (__force __be32)get_seconds();
2196 verf[1] = (__force __be32)nn->clverifier_counter++;
2197 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
2200 static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
2202 clp->cl_clientid.cl_boot = nn->boot_time;
2203 clp->cl_clientid.cl_id = nn->clientid_counter++;
2204 gen_confirm(clp, nn);
2207 static struct nfs4_stid *
2208 find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
2210 struct nfs4_stid *ret;
2212 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
2213 if (!ret || !ret->sc_type)
2218 static struct nfs4_stid *
2219 find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
2221 struct nfs4_stid *s;
2223 spin_lock(&cl->cl_lock);
2224 s = find_stateid_locked(cl, t);
2226 if (typemask & s->sc_type)
2227 refcount_inc(&s->sc_count);
2231 spin_unlock(&cl->cl_lock);
2235 static struct nfs4_client *get_nfsdfs_clp(struct inode *inode)
2237 struct nfsdfs_client *nc;
2238 nc = get_nfsdfs_client(inode);
2241 return container_of(nc, struct nfs4_client, cl_nfsdfs);
2244 static void seq_quote_mem(struct seq_file *m, char *data, int len)
2246 seq_printf(m, "\"");
2247 seq_escape_mem_ascii(m, data, len);
2248 seq_printf(m, "\"");
2251 static int client_info_show(struct seq_file *m, void *v)
2253 struct inode *inode = m->private;
2254 struct nfs4_client *clp;
2257 clp = get_nfsdfs_clp(inode);
2260 memcpy(&clid, &clp->cl_clientid, sizeof(clid));
2261 seq_printf(m, "clientid: 0x%llx\n", clid);
2262 seq_printf(m, "address: \"%pISpc\"\n", (struct sockaddr *)&clp->cl_addr);
2263 seq_printf(m, "name: ");
2264 seq_quote_mem(m, clp->cl_name.data, clp->cl_name.len);
2265 seq_printf(m, "\nminor version: %d\n", clp->cl_minorversion);
2266 if (clp->cl_nii_domain.data) {
2267 seq_printf(m, "Implementation domain: ");
2268 seq_quote_mem(m, clp->cl_nii_domain.data,
2269 clp->cl_nii_domain.len);
2270 seq_printf(m, "\nImplementation name: ");
2271 seq_quote_mem(m, clp->cl_nii_name.data, clp->cl_nii_name.len);
2272 seq_printf(m, "\nImplementation time: [%ld, %ld]\n",
2273 clp->cl_nii_time.tv_sec, clp->cl_nii_time.tv_nsec);
2280 static int client_info_open(struct inode *inode, struct file *file)
2282 return single_open(file, client_info_show, inode);
2285 static const struct file_operations client_info_fops = {
2286 .open = client_info_open,
2288 .llseek = seq_lseek,
2289 .release = single_release,
2292 static void *states_start(struct seq_file *s, loff_t *pos)
2293 __acquires(&clp->cl_lock)
2295 struct nfs4_client *clp = s->private;
2296 unsigned long id = *pos;
2299 spin_lock(&clp->cl_lock);
2300 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2305 static void *states_next(struct seq_file *s, void *v, loff_t *pos)
2307 struct nfs4_client *clp = s->private;
2308 unsigned long id = *pos;
2313 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2318 static void states_stop(struct seq_file *s, void *v)
2319 __releases(&clp->cl_lock)
2321 struct nfs4_client *clp = s->private;
2323 spin_unlock(&clp->cl_lock);
2326 static void nfs4_show_superblock(struct seq_file *s, struct file *f)
2328 struct inode *inode = file_inode(f);
2330 seq_printf(s, "superblock: \"%02x:%02x:%ld\"",
2331 MAJOR(inode->i_sb->s_dev),
2332 MINOR(inode->i_sb->s_dev),
2336 static void nfs4_show_owner(struct seq_file *s, struct nfs4_stateowner *oo)
2338 seq_printf(s, "owner: ");
2339 seq_quote_mem(s, oo->so_owner.data, oo->so_owner.len);
2342 static int nfs4_show_open(struct seq_file *s, struct nfs4_stid *st)
2344 struct nfs4_ol_stateid *ols;
2345 struct nfs4_file *nf;
2347 struct nfs4_stateowner *oo;
2348 unsigned int access, deny;
2350 if (st->sc_type != NFS4_OPEN_STID && st->sc_type != NFS4_LOCK_STID)
2351 return 0; /* XXX: or SEQ_SKIP? */
2352 ols = openlockstateid(st);
2353 oo = ols->st_stateowner;
2355 file = find_any_file(nf);
2357 seq_printf(s, "- 0x%16phN: { type: open, ", &st->sc_stateid);
2359 access = bmap_to_share_mode(ols->st_access_bmap);
2360 deny = bmap_to_share_mode(ols->st_deny_bmap);
2362 seq_printf(s, "access: \%s\%s, ",
2363 access & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2364 access & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2365 seq_printf(s, "deny: \%s\%s, ",
2366 deny & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2367 deny & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2369 nfs4_show_superblock(s, file);
2370 seq_printf(s, ", ");
2371 nfs4_show_owner(s, oo);
2372 seq_printf(s, " }\n");
2378 static int nfs4_show_lock(struct seq_file *s, struct nfs4_stid *st)
2380 struct nfs4_ol_stateid *ols;
2381 struct nfs4_file *nf;
2383 struct nfs4_stateowner *oo;
2385 ols = openlockstateid(st);
2386 oo = ols->st_stateowner;
2388 file = find_any_file(nf);
2390 seq_printf(s, "- 0x%16phN: { type: lock, ", &st->sc_stateid);
2393 * Note: a lock stateid isn't really the same thing as a lock,
2394 * it's the locking state held by one owner on a file, and there
2395 * may be multiple (or no) lock ranges associated with it.
2396 * (Same for the matter is true of open stateids.)
2399 nfs4_show_superblock(s, file);
2400 /* XXX: open stateid? */
2401 seq_printf(s, ", ");
2402 nfs4_show_owner(s, oo);
2403 seq_printf(s, " }\n");
2409 static int nfs4_show_deleg(struct seq_file *s, struct nfs4_stid *st)
2411 struct nfs4_delegation *ds;
2412 struct nfs4_file *nf;
2415 ds = delegstateid(st);
2417 file = nf->fi_deleg_file;
2419 seq_printf(s, "- 0x%16phN: { type: deleg, ", &st->sc_stateid);
2421 /* Kinda dead code as long as we only support read delegs: */
2422 seq_printf(s, "access: %s, ",
2423 ds->dl_type == NFS4_OPEN_DELEGATE_READ ? "r" : "w");
2425 /* XXX: lease time, whether it's being recalled. */
2427 nfs4_show_superblock(s, file);
2428 seq_printf(s, " }\n");
2433 static int nfs4_show_layout(struct seq_file *s, struct nfs4_stid *st)
2435 struct nfs4_layout_stateid *ls;
2438 ls = container_of(st, struct nfs4_layout_stateid, ls_stid);
2441 seq_printf(s, "- 0x%16phN: { type: layout, ", &st->sc_stateid);
2443 /* XXX: What else would be useful? */
2445 nfs4_show_superblock(s, file);
2446 seq_printf(s, " }\n");
2451 static int states_show(struct seq_file *s, void *v)
2453 struct nfs4_stid *st = v;
2455 switch (st->sc_type) {
2456 case NFS4_OPEN_STID:
2457 return nfs4_show_open(s, st);
2458 case NFS4_LOCK_STID:
2459 return nfs4_show_lock(s, st);
2460 case NFS4_DELEG_STID:
2461 return nfs4_show_deleg(s, st);
2462 case NFS4_LAYOUT_STID:
2463 return nfs4_show_layout(s, st);
2465 return 0; /* XXX: or SEQ_SKIP? */
2467 /* XXX: copy stateids? */
2470 static struct seq_operations states_seq_ops = {
2471 .start = states_start,
2472 .next = states_next,
2473 .stop = states_stop,
2477 static int client_states_open(struct inode *inode, struct file *file)
2480 struct nfs4_client *clp;
2483 clp = get_nfsdfs_clp(inode);
2487 ret = seq_open(file, &states_seq_ops);
2490 s = file->private_data;
2495 static int client_opens_release(struct inode *inode, struct file *file)
2497 struct seq_file *m = file->private_data;
2498 struct nfs4_client *clp = m->private;
2500 /* XXX: alternatively, we could get/drop in seq start/stop */
2505 static const struct file_operations client_states_fops = {
2506 .open = client_states_open,
2508 .llseek = seq_lseek,
2509 .release = client_opens_release,
2513 * Normally we refuse to destroy clients that are in use, but here the
2514 * administrator is telling us to just do it. We also want to wait
2515 * so the caller has a guarantee that the client's locks are gone by
2516 * the time the write returns:
2518 static void force_expire_client(struct nfs4_client *clp)
2520 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2521 bool already_expired;
2523 spin_lock(&clp->cl_lock);
2525 spin_unlock(&clp->cl_lock);
2527 wait_event(expiry_wq, atomic_read(&clp->cl_rpc_users) == 0);
2528 spin_lock(&nn->client_lock);
2529 already_expired = list_empty(&clp->cl_lru);
2530 if (!already_expired)
2531 unhash_client_locked(clp);
2532 spin_unlock(&nn->client_lock);
2534 if (!already_expired)
2537 wait_event(expiry_wq, clp->cl_nfsd_dentry == NULL);
2540 static ssize_t client_ctl_write(struct file *file, const char __user *buf,
2541 size_t size, loff_t *pos)
2544 struct nfs4_client *clp;
2546 data = simple_transaction_get(file, buf, size);
2548 return PTR_ERR(data);
2549 if (size != 7 || 0 != memcmp(data, "expire\n", 7))
2551 clp = get_nfsdfs_clp(file_inode(file));
2554 force_expire_client(clp);
2559 static const struct file_operations client_ctl_fops = {
2560 .write = client_ctl_write,
2561 .release = simple_transaction_release,
2564 static const struct tree_descr client_files[] = {
2565 [0] = {"info", &client_info_fops, S_IRUSR},
2566 [1] = {"states", &client_states_fops, S_IRUSR},
2567 [2] = {"ctl", &client_ctl_fops, S_IRUSR|S_IWUSR},
2571 static struct nfs4_client *create_client(struct xdr_netobj name,
2572 struct svc_rqst *rqstp, nfs4_verifier *verf)
2574 struct nfs4_client *clp;
2575 struct sockaddr *sa = svc_addr(rqstp);
2577 struct net *net = SVC_NET(rqstp);
2578 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2580 clp = alloc_client(name);
2584 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
2590 kref_init(&clp->cl_nfsdfs.cl_ref);
2591 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
2592 clp->cl_time = get_seconds();
2593 clear_bit(0, &clp->cl_cb_slot_busy);
2594 copy_verf(clp, verf);
2595 memcpy(&clp->cl_addr, sa, sizeof(struct sockaddr_storage));
2596 clp->cl_cb_session = NULL;
2598 clp->cl_nfsd_dentry = nfsd_client_mkdir(nn, &clp->cl_nfsdfs,
2599 clp->cl_clientid.cl_id - nn->clientid_base,
2601 if (!clp->cl_nfsd_dentry) {
2609 add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
2611 struct rb_node **new = &(root->rb_node), *parent = NULL;
2612 struct nfs4_client *clp;
2615 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
2618 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
2619 new = &((*new)->rb_left);
2621 new = &((*new)->rb_right);
2624 rb_link_node(&new_clp->cl_namenode, parent, new);
2625 rb_insert_color(&new_clp->cl_namenode, root);
2628 static struct nfs4_client *
2629 find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2632 struct rb_node *node = root->rb_node;
2633 struct nfs4_client *clp;
2636 clp = rb_entry(node, struct nfs4_client, cl_namenode);
2637 cmp = compare_blob(&clp->cl_name, name);
2639 node = node->rb_left;
2641 node = node->rb_right;
2649 add_to_unconfirmed(struct nfs4_client *clp)
2651 unsigned int idhashval;
2652 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2654 lockdep_assert_held(&nn->client_lock);
2656 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2657 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
2658 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
2659 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
2660 renew_client_locked(clp);
2664 move_to_confirmed(struct nfs4_client *clp)
2666 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
2667 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2669 lockdep_assert_held(&nn->client_lock);
2671 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
2672 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
2673 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
2674 add_clp_to_name_tree(clp, &nn->conf_name_tree);
2675 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2676 renew_client_locked(clp);
2679 static struct nfs4_client *
2680 find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
2682 struct nfs4_client *clp;
2683 unsigned int idhashval = clientid_hashval(clid->cl_id);
2685 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
2686 if (same_clid(&clp->cl_clientid, clid)) {
2687 if ((bool)clp->cl_minorversion != sessions)
2689 renew_client_locked(clp);
2696 static struct nfs4_client *
2697 find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2699 struct list_head *tbl = nn->conf_id_hashtbl;
2701 lockdep_assert_held(&nn->client_lock);
2702 return find_client_in_id_table(tbl, clid, sessions);
2705 static struct nfs4_client *
2706 find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2708 struct list_head *tbl = nn->unconf_id_hashtbl;
2710 lockdep_assert_held(&nn->client_lock);
2711 return find_client_in_id_table(tbl, clid, sessions);
2714 static bool clp_used_exchangeid(struct nfs4_client *clp)
2716 return clp->cl_exchange_flags != 0;
2719 static struct nfs4_client *
2720 find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
2722 lockdep_assert_held(&nn->client_lock);
2723 return find_clp_in_name_tree(name, &nn->conf_name_tree);
2726 static struct nfs4_client *
2727 find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
2729 lockdep_assert_held(&nn->client_lock);
2730 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
2734 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
2736 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
2737 struct sockaddr *sa = svc_addr(rqstp);
2738 u32 scopeid = rpc_get_scope_id(sa);
2739 unsigned short expected_family;
2741 /* Currently, we only support tcp and tcp6 for the callback channel */
2742 if (se->se_callback_netid_len == 3 &&
2743 !memcmp(se->se_callback_netid_val, "tcp", 3))
2744 expected_family = AF_INET;
2745 else if (se->se_callback_netid_len == 4 &&
2746 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2747 expected_family = AF_INET6;
2751 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
2752 se->se_callback_addr_len,
2753 (struct sockaddr *)&conn->cb_addr,
2754 sizeof(conn->cb_addr));
2756 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
2759 if (conn->cb_addr.ss_family == AF_INET6)
2760 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
2762 conn->cb_prog = se->se_callback_prog;
2763 conn->cb_ident = se->se_callback_ident;
2764 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
2767 conn->cb_addr.ss_family = AF_UNSPEC;
2768 conn->cb_addrlen = 0;
2769 dprintk("NFSD: this client (clientid %08x/%08x) "
2770 "will not receive delegations\n",
2771 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2777 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
2780 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2782 struct xdr_buf *buf = resp->xdr.buf;
2783 struct nfsd4_slot *slot = resp->cstate.slot;
2786 dprintk("--> %s slot %p\n", __func__, slot);
2788 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
2789 slot->sl_opcnt = resp->opcnt;
2790 slot->sl_status = resp->cstate.status;
2791 free_svc_cred(&slot->sl_cred);
2792 copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred);
2794 if (!nfsd4_cache_this(resp)) {
2795 slot->sl_flags &= ~NFSD4_SLOT_CACHED;
2798 slot->sl_flags |= NFSD4_SLOT_CACHED;
2800 base = resp->cstate.data_offset;
2801 slot->sl_datalen = buf->len - base;
2802 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
2803 WARN(1, "%s: sessions DRC could not cache compound\n",
2809 * Encode the replay sequence operation from the slot values.
2810 * If cachethis is FALSE encode the uncached rep error on the next
2811 * operation which sets resp->p and increments resp->opcnt for
2812 * nfs4svc_encode_compoundres.
2816 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2817 struct nfsd4_compoundres *resp)
2819 struct nfsd4_op *op;
2820 struct nfsd4_slot *slot = resp->cstate.slot;
2822 /* Encode the replayed sequence operation */
2823 op = &args->ops[resp->opcnt - 1];
2824 nfsd4_encode_operation(resp, op);
2826 if (slot->sl_flags & NFSD4_SLOT_CACHED)
2828 if (args->opcnt == 1) {
2830 * The original operation wasn't a solo sequence--we
2831 * always cache those--so this retry must not match the
2834 op->status = nfserr_seq_false_retry;
2836 op = &args->ops[resp->opcnt++];
2837 op->status = nfserr_retry_uncached_rep;
2838 nfsd4_encode_operation(resp, op);
2844 * The sequence operation is not cached because we can use the slot and
2848 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2849 struct nfsd4_sequence *seq)
2851 struct nfsd4_slot *slot = resp->cstate.slot;
2852 struct xdr_stream *xdr = &resp->xdr;
2856 dprintk("--> %s slot %p\n", __func__, slot);
2858 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
2862 p = xdr_reserve_space(xdr, slot->sl_datalen);
2865 return nfserr_serverfault;
2867 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2868 xdr_commit_encode(xdr);
2870 resp->opcnt = slot->sl_opcnt;
2871 return slot->sl_status;
2875 * Set the exchange_id flags returned by the server.
2878 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2880 #ifdef CONFIG_NFSD_PNFS
2881 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
2883 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
2886 /* Referrals are supported, Migration is not. */
2887 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2889 /* set the wire flags to return to client. */
2890 clid->flags = new->cl_exchange_flags;
2893 static bool client_has_openowners(struct nfs4_client *clp)
2895 struct nfs4_openowner *oo;
2897 list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
2898 if (!list_empty(&oo->oo_owner.so_stateids))
2904 static bool client_has_state(struct nfs4_client *clp)
2906 return client_has_openowners(clp)
2907 #ifdef CONFIG_NFSD_PNFS
2908 || !list_empty(&clp->cl_lo_states)
2910 || !list_empty(&clp->cl_delegations)
2911 || !list_empty(&clp->cl_sessions)
2912 || !list_empty(&clp->async_copies);
2915 static __be32 copy_impl_id(struct nfs4_client *clp,
2916 struct nfsd4_exchange_id *exid)
2918 if (!exid->nii_domain.data)
2920 xdr_netobj_dup(&clp->cl_nii_domain, &exid->nii_domain, GFP_KERNEL);
2921 if (!clp->cl_nii_domain.data)
2922 return nfserr_jukebox;
2923 xdr_netobj_dup(&clp->cl_nii_name, &exid->nii_name, GFP_KERNEL);
2924 if (!clp->cl_nii_name.data)
2925 return nfserr_jukebox;
2926 clp->cl_nii_time.tv_sec = exid->nii_time.tv_sec;
2927 clp->cl_nii_time.tv_nsec = exid->nii_time.tv_nsec;
2932 nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2933 union nfsd4_op_u *u)
2935 struct nfsd4_exchange_id *exid = &u->exchange_id;
2936 struct nfs4_client *conf, *new;
2937 struct nfs4_client *unconf = NULL;
2939 char addr_str[INET6_ADDRSTRLEN];
2940 nfs4_verifier verf = exid->verifier;
2941 struct sockaddr *sa = svc_addr(rqstp);
2942 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
2943 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2945 rpc_ntop(sa, addr_str, sizeof(addr_str));
2946 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
2947 "ip_addr=%s flags %x, spa_how %d\n",
2948 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
2949 addr_str, exid->flags, exid->spa_how);
2951 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
2952 return nfserr_inval;
2954 new = create_client(exid->clname, rqstp, &verf);
2956 return nfserr_jukebox;
2957 status = copy_impl_id(new, exid);
2961 switch (exid->spa_how) {
2963 exid->spo_must_enforce[0] = 0;
2964 exid->spo_must_enforce[1] = (
2965 1 << (OP_BIND_CONN_TO_SESSION - 32) |
2966 1 << (OP_EXCHANGE_ID - 32) |
2967 1 << (OP_CREATE_SESSION - 32) |
2968 1 << (OP_DESTROY_SESSION - 32) |
2969 1 << (OP_DESTROY_CLIENTID - 32));
2971 exid->spo_must_allow[0] &= (1 << (OP_CLOSE) |
2972 1 << (OP_OPEN_DOWNGRADE) |
2974 1 << (OP_DELEGRETURN));
2976 exid->spo_must_allow[1] &= (
2977 1 << (OP_TEST_STATEID - 32) |
2978 1 << (OP_FREE_STATEID - 32));
2979 if (!svc_rqst_integrity_protected(rqstp)) {
2980 status = nfserr_inval;
2984 * Sometimes userspace doesn't give us a principal.
2985 * Which is a bug, really. Anyway, we can't enforce
2986 * MACH_CRED in that case, better to give up now:
2988 if (!new->cl_cred.cr_principal &&
2989 !new->cl_cred.cr_raw_principal) {
2990 status = nfserr_serverfault;
2993 new->cl_mach_cred = true;
2996 default: /* checked by xdr code */
3000 status = nfserr_encr_alg_unsupp;
3004 /* Cases below refer to rfc 5661 section 18.35.4: */
3005 spin_lock(&nn->client_lock);
3006 conf = find_confirmed_client_by_name(&exid->clname, nn);
3008 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
3009 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
3012 if (!clp_used_exchangeid(conf)) { /* buggy client */
3013 status = nfserr_inval;
3016 if (!nfsd4_mach_creds_match(conf, rqstp)) {
3017 status = nfserr_wrong_cred;
3020 if (!creds_match) { /* case 9 */
3021 status = nfserr_perm;
3024 if (!verfs_match) { /* case 8 */
3025 status = nfserr_not_same;
3029 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
3032 if (!creds_match) { /* case 3 */
3033 if (client_has_state(conf)) {
3034 status = nfserr_clid_inuse;
3039 if (verfs_match) { /* case 2 */
3040 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
3043 /* case 5, client reboot */
3048 if (update) { /* case 7 */
3049 status = nfserr_noent;
3053 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
3054 if (unconf) /* case 4, possible retry or client restart */
3055 unhash_client_locked(unconf);
3057 /* case 1 (normal case) */
3060 status = mark_client_expired_locked(conf);
3064 new->cl_minorversion = cstate->minorversion;
3065 new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0];
3066 new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1];
3068 add_to_unconfirmed(new);
3071 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
3072 exid->clientid.cl_id = conf->cl_clientid.cl_id;
3074 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
3075 nfsd4_set_ex_flags(conf, exid);
3077 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
3078 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
3082 spin_unlock(&nn->client_lock);
3087 expire_client(unconf);
3092 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
3094 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
3097 /* The slot is in use, and no response has been sent. */
3099 if (seqid == slot_seqid)
3100 return nfserr_jukebox;
3102 return nfserr_seq_misordered;
3104 /* Note unsigned 32-bit arithmetic handles wraparound: */
3105 if (likely(seqid == slot_seqid + 1))
3107 if (seqid == slot_seqid)
3108 return nfserr_replay_cache;
3109 return nfserr_seq_misordered;
3113 * Cache the create session result into the create session single DRC
3114 * slot cache by saving the xdr structure. sl_seqid has been set.
3115 * Do this for solo or embedded create session operations.
3118 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
3119 struct nfsd4_clid_slot *slot, __be32 nfserr)
3121 slot->sl_status = nfserr;
3122 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
3126 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
3127 struct nfsd4_clid_slot *slot)
3129 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
3130 return slot->sl_status;
3133 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
3134 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
3135 1 + /* MIN tag is length with zero, only length */ \
3136 3 + /* version, opcount, opcode */ \
3137 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3138 /* seqid, slotID, slotID, cache */ \
3139 4 ) * sizeof(__be32))
3141 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
3142 2 + /* verifier: AUTH_NULL, length 0 */\
3144 1 + /* MIN tag is length with zero, only length */ \
3145 3 + /* opcount, opcode, opstatus*/ \
3146 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3147 /* seqid, slotID, slotID, slotID, status */ \
3148 5 ) * sizeof(__be32))
3150 static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
3152 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
3154 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
3155 return nfserr_toosmall;
3156 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
3157 return nfserr_toosmall;
3158 ca->headerpadsz = 0;
3159 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
3160 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
3161 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
3162 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
3163 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
3164 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
3166 * Note decreasing slot size below client's request may make it
3167 * difficult for client to function correctly, whereas
3168 * decreasing the number of slots will (just?) affect
3169 * performance. When short on memory we therefore prefer to
3170 * decrease number of slots instead of their size. Clients that
3171 * request larger slots than they need will get poor results:
3173 ca->maxreqs = nfsd4_get_drc_mem(ca);
3175 return nfserr_jukebox;
3181 * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
3182 * These are based on similar macros in linux/sunrpc/msg_prot.h .
3184 #define RPC_MAX_HEADER_WITH_AUTH_SYS \
3185 (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
3187 #define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
3188 (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
3190 #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
3191 RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
3192 #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
3193 RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
3196 static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
3198 ca->headerpadsz = 0;
3200 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
3201 return nfserr_toosmall;
3202 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
3203 return nfserr_toosmall;
3204 ca->maxresp_cached = 0;
3206 return nfserr_toosmall;
3211 static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
3213 switch (cbs->flavor) {
3219 * GSS case: the spec doesn't allow us to return this
3220 * error. But it also doesn't allow us not to support
3222 * I'd rather this fail hard than return some error the
3223 * client might think it can already handle:
3225 return nfserr_encr_alg_unsupp;
3230 nfsd4_create_session(struct svc_rqst *rqstp,
3231 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
3233 struct nfsd4_create_session *cr_ses = &u->create_session;
3234 struct sockaddr *sa = svc_addr(rqstp);
3235 struct nfs4_client *conf, *unconf;
3236 struct nfs4_client *old = NULL;
3237 struct nfsd4_session *new;
3238 struct nfsd4_conn *conn;
3239 struct nfsd4_clid_slot *cs_slot = NULL;
3241 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3243 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
3244 return nfserr_inval;
3245 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
3248 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
3251 status = check_backchannel_attrs(&cr_ses->back_channel);
3253 goto out_release_drc_mem;
3254 status = nfserr_jukebox;
3255 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
3257 goto out_release_drc_mem;
3258 conn = alloc_conn_from_crses(rqstp, cr_ses);
3260 goto out_free_session;
3262 spin_lock(&nn->client_lock);
3263 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
3264 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
3265 WARN_ON_ONCE(conf && unconf);
3268 status = nfserr_wrong_cred;
3269 if (!nfsd4_mach_creds_match(conf, rqstp))
3271 cs_slot = &conf->cl_cs_slot;
3272 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
3274 if (status == nfserr_replay_cache)
3275 status = nfsd4_replay_create_session(cr_ses, cs_slot);
3278 } else if (unconf) {
3279 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
3280 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
3281 status = nfserr_clid_inuse;
3284 status = nfserr_wrong_cred;
3285 if (!nfsd4_mach_creds_match(unconf, rqstp))
3287 cs_slot = &unconf->cl_cs_slot;
3288 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
3290 /* an unconfirmed replay returns misordered */
3291 status = nfserr_seq_misordered;
3294 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3296 status = mark_client_expired_locked(old);
3302 move_to_confirmed(unconf);
3305 status = nfserr_stale_clientid;
3309 /* Persistent sessions are not supported */
3310 cr_ses->flags &= ~SESSION4_PERSIST;
3311 /* Upshifting from TCP to RDMA is not supported */
3312 cr_ses->flags &= ~SESSION4_RDMA;
3314 init_session(rqstp, new, conf, cr_ses);
3315 nfsd4_get_session_locked(new);
3317 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
3318 NFS4_MAX_SESSIONID_LEN);
3319 cs_slot->sl_seqid++;
3320 cr_ses->seqid = cs_slot->sl_seqid;
3322 /* cache solo and embedded create sessions under the client_lock */
3323 nfsd4_cache_create_session(cr_ses, cs_slot, status);
3324 spin_unlock(&nn->client_lock);
3325 /* init connection and backchannel */
3326 nfsd4_init_conn(rqstp, conn, new);
3327 nfsd4_put_session(new);
3332 spin_unlock(&nn->client_lock);
3337 __free_session(new);
3338 out_release_drc_mem:
3339 nfsd4_put_drc_mem(&cr_ses->fore_channel);
3343 static __be32 nfsd4_map_bcts_dir(u32 *dir)
3346 case NFS4_CDFC4_FORE:
3347 case NFS4_CDFC4_BACK:
3349 case NFS4_CDFC4_FORE_OR_BOTH:
3350 case NFS4_CDFC4_BACK_OR_BOTH:
3351 *dir = NFS4_CDFC4_BOTH;
3354 return nfserr_inval;
3357 __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp,
3358 struct nfsd4_compound_state *cstate,
3359 union nfsd4_op_u *u)
3361 struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl;
3362 struct nfsd4_session *session = cstate->session;
3363 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3366 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
3369 spin_lock(&nn->client_lock);
3370 session->se_cb_prog = bc->bc_cb_program;
3371 session->se_cb_sec = bc->bc_cb_sec;
3372 spin_unlock(&nn->client_lock);
3374 nfsd4_probe_callback(session->se_client);
3379 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
3380 struct nfsd4_compound_state *cstate,
3381 union nfsd4_op_u *u)
3383 struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session;
3385 struct nfsd4_conn *conn;
3386 struct nfsd4_session *session;
3387 struct net *net = SVC_NET(rqstp);
3388 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3390 if (!nfsd4_last_compound_op(rqstp))
3391 return nfserr_not_only_op;
3392 spin_lock(&nn->client_lock);
3393 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
3394 spin_unlock(&nn->client_lock);
3396 goto out_no_session;
3397 status = nfserr_wrong_cred;
3398 if (!nfsd4_mach_creds_match(session->se_client, rqstp))
3400 status = nfsd4_map_bcts_dir(&bcts->dir);
3403 conn = alloc_conn(rqstp, bcts->dir);
3404 status = nfserr_jukebox;
3407 nfsd4_init_conn(rqstp, conn, session);
3410 nfsd4_put_session(session);
3415 static bool nfsd4_compound_in_session(struct nfsd4_compound_state *cstate, struct nfs4_sessionid *sid)
3417 if (!cstate->session)
3419 return !memcmp(sid, &cstate->session->se_sessionid, sizeof(*sid));
3423 nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate,
3424 union nfsd4_op_u *u)
3426 struct nfs4_sessionid *sessionid = &u->destroy_session.sessionid;
3427 struct nfsd4_session *ses;
3429 int ref_held_by_me = 0;
3430 struct net *net = SVC_NET(r);
3431 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3433 status = nfserr_not_only_op;
3434 if (nfsd4_compound_in_session(cstate, sessionid)) {
3435 if (!nfsd4_last_compound_op(r))
3439 dump_sessionid(__func__, sessionid);
3440 spin_lock(&nn->client_lock);
3441 ses = find_in_sessionid_hashtbl(sessionid, net, &status);
3443 goto out_client_lock;
3444 status = nfserr_wrong_cred;
3445 if (!nfsd4_mach_creds_match(ses->se_client, r))
3446 goto out_put_session;
3447 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
3449 goto out_put_session;
3450 unhash_session(ses);
3451 spin_unlock(&nn->client_lock);
3453 nfsd4_probe_callback_sync(ses->se_client);
3455 spin_lock(&nn->client_lock);
3458 nfsd4_put_session_locked(ses);
3460 spin_unlock(&nn->client_lock);
3465 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
3467 struct nfsd4_conn *c;
3469 list_for_each_entry(c, &s->se_conns, cn_persession) {
3470 if (c->cn_xprt == xpt) {
3477 static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
3479 struct nfs4_client *clp = ses->se_client;
3480 struct nfsd4_conn *c;
3481 __be32 status = nfs_ok;
3484 spin_lock(&clp->cl_lock);
3485 c = __nfsd4_find_conn(new->cn_xprt, ses);
3488 status = nfserr_conn_not_bound_to_session;
3489 if (clp->cl_mach_cred)
3491 __nfsd4_hash_conn(new, ses);
3492 spin_unlock(&clp->cl_lock);
3493 ret = nfsd4_register_conn(new);
3495 /* oops; xprt is already down: */
3496 nfsd4_conn_lost(&new->cn_xpt_user);
3499 spin_unlock(&clp->cl_lock);
3504 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
3506 struct nfsd4_compoundargs *args = rqstp->rq_argp;
3508 return args->opcnt > session->se_fchannel.maxops;
3511 static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
3512 struct nfsd4_session *session)
3514 struct xdr_buf *xb = &rqstp->rq_arg;
3516 return xb->len > session->se_fchannel.maxreq_sz;
3519 static bool replay_matches_cache(struct svc_rqst *rqstp,
3520 struct nfsd4_sequence *seq, struct nfsd4_slot *slot)
3522 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
3524 if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) !=
3525 (bool)seq->cachethis)
3528 * If there's an error than the reply can have fewer ops than
3529 * the call. But if we cached a reply with *more* ops than the
3530 * call you're sending us now, then this new call is clearly not
3531 * really a replay of the old one:
3533 if (slot->sl_opcnt < argp->opcnt)
3535 /* This is the only check explicitly called by spec: */
3536 if (!same_creds(&rqstp->rq_cred, &slot->sl_cred))
3539 * There may be more comparisons we could actually do, but the
3540 * spec doesn't require us to catch every case where the calls
3541 * don't match (that would require caching the call as well as
3542 * the reply), so we don't bother.
3548 nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3549 union nfsd4_op_u *u)
3551 struct nfsd4_sequence *seq = &u->sequence;
3552 struct nfsd4_compoundres *resp = rqstp->rq_resp;
3553 struct xdr_stream *xdr = &resp->xdr;
3554 struct nfsd4_session *session;
3555 struct nfs4_client *clp;
3556 struct nfsd4_slot *slot;
3557 struct nfsd4_conn *conn;
3560 struct net *net = SVC_NET(rqstp);
3561 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3563 if (resp->opcnt != 1)
3564 return nfserr_sequence_pos;
3567 * Will be either used or freed by nfsd4_sequence_check_conn
3570 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
3572 return nfserr_jukebox;
3574 spin_lock(&nn->client_lock);
3575 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
3577 goto out_no_session;
3578 clp = session->se_client;
3580 status = nfserr_too_many_ops;
3581 if (nfsd4_session_too_many_ops(rqstp, session))
3582 goto out_put_session;
3584 status = nfserr_req_too_big;
3585 if (nfsd4_request_too_big(rqstp, session))
3586 goto out_put_session;
3588 status = nfserr_badslot;
3589 if (seq->slotid >= session->se_fchannel.maxreqs)
3590 goto out_put_session;
3592 slot = session->se_slots[seq->slotid];
3593 dprintk("%s: slotid %d\n", __func__, seq->slotid);
3595 /* We do not negotiate the number of slots yet, so set the
3596 * maxslots to the session maxreqs which is used to encode
3597 * sr_highest_slotid and the sr_target_slot id to maxslots */
3598 seq->maxslots = session->se_fchannel.maxreqs;
3600 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
3601 slot->sl_flags & NFSD4_SLOT_INUSE);
3602 if (status == nfserr_replay_cache) {
3603 status = nfserr_seq_misordered;
3604 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
3605 goto out_put_session;
3606 status = nfserr_seq_false_retry;
3607 if (!replay_matches_cache(rqstp, seq, slot))
3608 goto out_put_session;
3609 cstate->slot = slot;
3610 cstate->session = session;
3612 /* Return the cached reply status and set cstate->status
3613 * for nfsd4_proc_compound processing */
3614 status = nfsd4_replay_cache_entry(resp, seq);
3615 cstate->status = nfserr_replay_cache;
3619 goto out_put_session;
3621 status = nfsd4_sequence_check_conn(conn, session);
3624 goto out_put_session;
3626 buflen = (seq->cachethis) ?
3627 session->se_fchannel.maxresp_cached :
3628 session->se_fchannel.maxresp_sz;
3629 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
3631 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
3632 goto out_put_session;
3633 svc_reserve(rqstp, buflen);
3636 /* Success! bump slot seqid */
3637 slot->sl_seqid = seq->seqid;
3638 slot->sl_flags |= NFSD4_SLOT_INUSE;
3640 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
3642 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
3644 cstate->slot = slot;
3645 cstate->session = session;
3649 switch (clp->cl_cb_state) {
3651 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
3653 case NFSD4_CB_FAULT:
3654 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
3657 seq->status_flags = 0;
3659 if (!list_empty(&clp->cl_revoked))
3660 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
3664 spin_unlock(&nn->client_lock);
3667 nfsd4_put_session_locked(session);
3668 goto out_no_session;
3672 nfsd4_sequence_done(struct nfsd4_compoundres *resp)
3674 struct nfsd4_compound_state *cs = &resp->cstate;
3676 if (nfsd4_has_session(cs)) {
3677 if (cs->status != nfserr_replay_cache) {
3678 nfsd4_store_cache_entry(resp);
3679 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
3681 /* Drop session reference that was taken in nfsd4_sequence() */
3682 nfsd4_put_session(cs->session);
3684 put_client_renew(cs->clp);
3688 nfsd4_destroy_clientid(struct svc_rqst *rqstp,
3689 struct nfsd4_compound_state *cstate,
3690 union nfsd4_op_u *u)
3692 struct nfsd4_destroy_clientid *dc = &u->destroy_clientid;
3693 struct nfs4_client *conf, *unconf;
3694 struct nfs4_client *clp = NULL;
3696 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3698 spin_lock(&nn->client_lock);
3699 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
3700 conf = find_confirmed_client(&dc->clientid, true, nn);
3701 WARN_ON_ONCE(conf && unconf);
3704 if (client_has_state(conf)) {
3705 status = nfserr_clientid_busy;
3708 status = mark_client_expired_locked(conf);
3715 status = nfserr_stale_clientid;
3718 if (!nfsd4_mach_creds_match(clp, rqstp)) {
3720 status = nfserr_wrong_cred;
3723 unhash_client_locked(clp);
3725 spin_unlock(&nn->client_lock);
3732 nfsd4_reclaim_complete(struct svc_rqst *rqstp,
3733 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
3735 struct nfsd4_reclaim_complete *rc = &u->reclaim_complete;
3738 if (rc->rca_one_fs) {
3739 if (!cstate->current_fh.fh_dentry)
3740 return nfserr_nofilehandle;
3742 * We don't take advantage of the rca_one_fs case.
3743 * That's OK, it's optional, we can safely ignore it.
3748 status = nfserr_complete_already;
3749 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
3750 &cstate->session->se_client->cl_flags))
3753 status = nfserr_stale_clientid;
3754 if (is_client_expired(cstate->session->se_client))
3756 * The following error isn't really legal.
3757 * But we only get here if the client just explicitly
3758 * destroyed the client. Surely it no longer cares what
3759 * error it gets back on an operation for the dead
3765 nfsd4_client_record_create(cstate->session->se_client);
3766 inc_reclaim_complete(cstate->session->se_client);
3772 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3773 union nfsd4_op_u *u)
3775 struct nfsd4_setclientid *setclid = &u->setclientid;
3776 struct xdr_netobj clname = setclid->se_name;
3777 nfs4_verifier clverifier = setclid->se_verf;
3778 struct nfs4_client *conf, *new;
3779 struct nfs4_client *unconf = NULL;
3781 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3783 new = create_client(clname, rqstp, &clverifier);
3785 return nfserr_jukebox;
3786 /* Cases below refer to rfc 3530 section 14.2.33: */
3787 spin_lock(&nn->client_lock);
3788 conf = find_confirmed_client_by_name(&clname, nn);
3789 if (conf && client_has_state(conf)) {
3791 status = nfserr_clid_inuse;
3792 if (clp_used_exchangeid(conf))
3794 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
3795 char addr_str[INET6_ADDRSTRLEN];
3796 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
3798 dprintk("NFSD: setclientid: string in use by client "
3799 "at %s\n", addr_str);
3803 unconf = find_unconfirmed_client_by_name(&clname, nn);
3805 unhash_client_locked(unconf);
3806 if (conf && same_verf(&conf->cl_verifier, &clverifier)) {
3807 /* case 1: probable callback update */
3808 copy_clid(new, conf);
3809 gen_confirm(new, nn);
3810 } else /* case 4 (new client) or cases 2, 3 (client reboot): */
3812 new->cl_minorversion = 0;
3813 gen_callback(new, setclid, rqstp);
3814 add_to_unconfirmed(new);
3815 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
3816 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
3817 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
3821 spin_unlock(&nn->client_lock);
3825 expire_client(unconf);
3831 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
3832 struct nfsd4_compound_state *cstate,
3833 union nfsd4_op_u *u)
3835 struct nfsd4_setclientid_confirm *setclientid_confirm =
3836 &u->setclientid_confirm;
3837 struct nfs4_client *conf, *unconf;
3838 struct nfs4_client *old = NULL;
3839 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
3840 clientid_t * clid = &setclientid_confirm->sc_clientid;
3842 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3844 if (STALE_CLIENTID(clid, nn))
3845 return nfserr_stale_clientid;
3847 spin_lock(&nn->client_lock);
3848 conf = find_confirmed_client(clid, false, nn);
3849 unconf = find_unconfirmed_client(clid, false, nn);
3851 * We try hard to give out unique clientid's, so if we get an
3852 * attempt to confirm the same clientid with a different cred,
3853 * the client may be buggy; this should never happen.
3855 * Nevertheless, RFC 7530 recommends INUSE for this case:
3857 status = nfserr_clid_inuse;
3858 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3860 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3862 /* cases below refer to rfc 3530 section 14.2.34: */
3863 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
3864 if (conf && same_verf(&confirm, &conf->cl_confirm)) {
3865 /* case 2: probable retransmit */
3867 } else /* case 4: client hasn't noticed we rebooted yet? */
3868 status = nfserr_stale_clientid;
3872 if (conf) { /* case 1: callback update */
3874 unhash_client_locked(old);
3875 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
3876 } else { /* case 3: normal case; new or rebooted client */
3877 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3879 status = nfserr_clid_inuse;
3880 if (client_has_state(old)
3881 && !same_creds(&unconf->cl_cred,
3884 status = mark_client_expired_locked(old);
3890 move_to_confirmed(unconf);
3893 get_client_locked(conf);
3894 spin_unlock(&nn->client_lock);
3895 nfsd4_probe_callback(conf);
3896 spin_lock(&nn->client_lock);
3897 put_client_renew_locked(conf);
3899 spin_unlock(&nn->client_lock);
3905 static struct nfs4_file *nfsd4_alloc_file(void)
3907 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3910 /* OPEN Share state helper functions */
3911 static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
3912 struct nfs4_file *fp)
3914 lockdep_assert_held(&state_lock);
3916 refcount_set(&fp->fi_ref, 1);
3917 spin_lock_init(&fp->fi_lock);
3918 INIT_LIST_HEAD(&fp->fi_stateids);
3919 INIT_LIST_HEAD(&fp->fi_delegations);
3920 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
3921 fh_copy_shallow(&fp->fi_fhandle, fh);
3922 fp->fi_deleg_file = NULL;
3923 fp->fi_had_conflict = false;
3924 fp->fi_share_deny = 0;
3925 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3926 memset(fp->fi_access, 0, sizeof(fp->fi_access));
3927 #ifdef CONFIG_NFSD_PNFS
3928 INIT_LIST_HEAD(&fp->fi_lo_states);
3929 atomic_set(&fp->fi_lo_recalls, 0);
3931 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
3935 nfsd4_free_slabs(void)
3937 kmem_cache_destroy(client_slab);
3938 kmem_cache_destroy(openowner_slab);
3939 kmem_cache_destroy(lockowner_slab);
3940 kmem_cache_destroy(file_slab);
3941 kmem_cache_destroy(stateid_slab);
3942 kmem_cache_destroy(deleg_slab);
3943 kmem_cache_destroy(odstate_slab);
3947 nfsd4_init_slabs(void)
3949 client_slab = kmem_cache_create("nfsd4_clients",
3950 sizeof(struct nfs4_client), 0, 0, NULL);
3951 if (client_slab == NULL)
3953 openowner_slab = kmem_cache_create("nfsd4_openowners",
3954 sizeof(struct nfs4_openowner), 0, 0, NULL);
3955 if (openowner_slab == NULL)
3956 goto out_free_client_slab;
3957 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
3958 sizeof(struct nfs4_lockowner), 0, 0, NULL);
3959 if (lockowner_slab == NULL)
3960 goto out_free_openowner_slab;
3961 file_slab = kmem_cache_create("nfsd4_files",
3962 sizeof(struct nfs4_file), 0, 0, NULL);
3963 if (file_slab == NULL)
3964 goto out_free_lockowner_slab;
3965 stateid_slab = kmem_cache_create("nfsd4_stateids",
3966 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
3967 if (stateid_slab == NULL)
3968 goto out_free_file_slab;
3969 deleg_slab = kmem_cache_create("nfsd4_delegations",
3970 sizeof(struct nfs4_delegation), 0, 0, NULL);
3971 if (deleg_slab == NULL)
3972 goto out_free_stateid_slab;
3973 odstate_slab = kmem_cache_create("nfsd4_odstate",
3974 sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
3975 if (odstate_slab == NULL)
3976 goto out_free_deleg_slab;
3979 out_free_deleg_slab:
3980 kmem_cache_destroy(deleg_slab);
3981 out_free_stateid_slab:
3982 kmem_cache_destroy(stateid_slab);
3984 kmem_cache_destroy(file_slab);
3985 out_free_lockowner_slab:
3986 kmem_cache_destroy(lockowner_slab);
3987 out_free_openowner_slab:
3988 kmem_cache_destroy(openowner_slab);
3989 out_free_client_slab:
3990 kmem_cache_destroy(client_slab);
3992 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3996 static void init_nfs4_replay(struct nfs4_replay *rp)
3998 rp->rp_status = nfserr_serverfault;
4000 rp->rp_buf = rp->rp_ibuf;
4001 mutex_init(&rp->rp_mutex);
4004 static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
4005 struct nfs4_stateowner *so)
4007 if (!nfsd4_has_session(cstate)) {
4008 mutex_lock(&so->so_replay.rp_mutex);
4009 cstate->replay_owner = nfs4_get_stateowner(so);
4013 void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
4015 struct nfs4_stateowner *so = cstate->replay_owner;
4018 cstate->replay_owner = NULL;
4019 mutex_unlock(&so->so_replay.rp_mutex);
4020 nfs4_put_stateowner(so);
4024 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
4026 struct nfs4_stateowner *sop;
4028 sop = kmem_cache_alloc(slab, GFP_KERNEL);
4032 xdr_netobj_dup(&sop->so_owner, owner, GFP_KERNEL);
4033 if (!sop->so_owner.data) {
4034 kmem_cache_free(slab, sop);
4038 INIT_LIST_HEAD(&sop->so_stateids);
4039 sop->so_client = clp;
4040 init_nfs4_replay(&sop->so_replay);
4041 atomic_set(&sop->so_count, 1);
4045 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
4047 lockdep_assert_held(&clp->cl_lock);
4049 list_add(&oo->oo_owner.so_strhash,
4050 &clp->cl_ownerstr_hashtbl[strhashval]);
4051 list_add(&oo->oo_perclient, &clp->cl_openowners);
4054 static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
4056 unhash_openowner_locked(openowner(so));
4059 static void nfs4_free_openowner(struct nfs4_stateowner *so)
4061 struct nfs4_openowner *oo = openowner(so);
4063 kmem_cache_free(openowner_slab, oo);
4066 static const struct nfs4_stateowner_operations openowner_ops = {
4067 .so_unhash = nfs4_unhash_openowner,
4068 .so_free = nfs4_free_openowner,
4071 static struct nfs4_ol_stateid *
4072 nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4074 struct nfs4_ol_stateid *local, *ret = NULL;
4075 struct nfs4_openowner *oo = open->op_openowner;
4077 lockdep_assert_held(&fp->fi_lock);
4079 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
4080 /* ignore lock owners */
4081 if (local->st_stateowner->so_is_open_owner == 0)
4083 if (local->st_stateowner != &oo->oo_owner)
4085 if (local->st_stid.sc_type == NFS4_OPEN_STID) {
4087 refcount_inc(&ret->st_stid.sc_count);
4095 nfsd4_verify_open_stid(struct nfs4_stid *s)
4097 __be32 ret = nfs_ok;
4099 switch (s->sc_type) {
4103 case NFS4_CLOSED_STID:
4104 case NFS4_CLOSED_DELEG_STID:
4105 ret = nfserr_bad_stateid;
4107 case NFS4_REVOKED_DELEG_STID:
4108 ret = nfserr_deleg_revoked;
4113 /* Lock the stateid st_mutex, and deal with races with CLOSE */
4115 nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp)
4119 mutex_lock_nested(&stp->st_mutex, LOCK_STATEID_MUTEX);
4120 ret = nfsd4_verify_open_stid(&stp->st_stid);
4122 mutex_unlock(&stp->st_mutex);
4126 static struct nfs4_ol_stateid *
4127 nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4129 struct nfs4_ol_stateid *stp;
4131 spin_lock(&fp->fi_lock);
4132 stp = nfsd4_find_existing_open(fp, open);
4133 spin_unlock(&fp->fi_lock);
4134 if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok)
4136 nfs4_put_stid(&stp->st_stid);
4141 static struct nfs4_openowner *
4142 alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
4143 struct nfsd4_compound_state *cstate)
4145 struct nfs4_client *clp = cstate->clp;
4146 struct nfs4_openowner *oo, *ret;
4148 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
4151 oo->oo_owner.so_ops = &openowner_ops;
4152 oo->oo_owner.so_is_open_owner = 1;
4153 oo->oo_owner.so_seqid = open->op_seqid;
4155 if (nfsd4_has_session(cstate))
4156 oo->oo_flags |= NFS4_OO_CONFIRMED;
4158 oo->oo_last_closed_stid = NULL;
4159 INIT_LIST_HEAD(&oo->oo_close_lru);
4160 spin_lock(&clp->cl_lock);
4161 ret = find_openstateowner_str_locked(strhashval, open, clp);
4163 hash_openowner(oo, clp, strhashval);
4166 nfs4_free_stateowner(&oo->oo_owner);
4168 spin_unlock(&clp->cl_lock);
4172 static struct nfs4_ol_stateid *
4173 init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
4176 struct nfs4_openowner *oo = open->op_openowner;
4177 struct nfs4_ol_stateid *retstp = NULL;
4178 struct nfs4_ol_stateid *stp;
4181 /* We are moving these outside of the spinlocks to avoid the warnings */
4182 mutex_init(&stp->st_mutex);
4183 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
4186 spin_lock(&oo->oo_owner.so_client->cl_lock);
4187 spin_lock(&fp->fi_lock);
4189 retstp = nfsd4_find_existing_open(fp, open);
4193 open->op_stp = NULL;
4194 refcount_inc(&stp->st_stid.sc_count);
4195 stp->st_stid.sc_type = NFS4_OPEN_STID;
4196 INIT_LIST_HEAD(&stp->st_locks);
4197 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
4199 stp->st_stid.sc_file = fp;
4200 stp->st_access_bmap = 0;
4201 stp->st_deny_bmap = 0;
4202 stp->st_openstp = NULL;
4203 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
4204 list_add(&stp->st_perfile, &fp->fi_stateids);
4207 spin_unlock(&fp->fi_lock);
4208 spin_unlock(&oo->oo_owner.so_client->cl_lock);
4210 /* Handle races with CLOSE */
4211 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
4212 nfs4_put_stid(&retstp->st_stid);
4215 /* To keep mutex tracking happy */
4216 mutex_unlock(&stp->st_mutex);
4223 * In the 4.0 case we need to keep the owners around a little while to handle
4224 * CLOSE replay. We still do need to release any file access that is held by
4225 * them before returning however.
4228 move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
4230 struct nfs4_ol_stateid *last;
4231 struct nfs4_openowner *oo = openowner(s->st_stateowner);
4232 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
4235 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
4238 * We know that we hold one reference via nfsd4_close, and another
4239 * "persistent" reference for the client. If the refcount is higher
4240 * than 2, then there are still calls in progress that are using this
4241 * stateid. We can't put the sc_file reference until they are finished.
4242 * Wait for the refcount to drop to 2. Since it has been unhashed,
4243 * there should be no danger of the refcount going back up again at
4246 wait_event(close_wq, refcount_read(&s->st_stid.sc_count) == 2);
4248 release_all_access(s);
4249 if (s->st_stid.sc_file) {
4250 put_nfs4_file(s->st_stid.sc_file);
4251 s->st_stid.sc_file = NULL;
4254 spin_lock(&nn->client_lock);
4255 last = oo->oo_last_closed_stid;
4256 oo->oo_last_closed_stid = s;
4257 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
4258 oo->oo_time = get_seconds();
4259 spin_unlock(&nn->client_lock);
4261 nfs4_put_stid(&last->st_stid);
4264 /* search file_hashtbl[] for file */
4265 static struct nfs4_file *
4266 find_file_locked(struct knfsd_fh *fh, unsigned int hashval)
4268 struct nfs4_file *fp;
4270 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash) {
4271 if (fh_match(&fp->fi_fhandle, fh)) {
4272 if (refcount_inc_not_zero(&fp->fi_ref))
4280 find_file(struct knfsd_fh *fh)
4282 struct nfs4_file *fp;
4283 unsigned int hashval = file_hashval(fh);
4286 fp = find_file_locked(fh, hashval);
4291 static struct nfs4_file *
4292 find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
4294 struct nfs4_file *fp;
4295 unsigned int hashval = file_hashval(fh);
4298 fp = find_file_locked(fh, hashval);
4303 spin_lock(&state_lock);
4304 fp = find_file_locked(fh, hashval);
4305 if (likely(fp == NULL)) {
4306 nfsd4_init_file(fh, hashval, new);
4309 spin_unlock(&state_lock);
4315 * Called to check deny when READ with all zero stateid or
4316 * WRITE with all zero or all one stateid
4319 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
4321 struct nfs4_file *fp;
4322 __be32 ret = nfs_ok;
4324 fp = find_file(¤t_fh->fh_handle);
4327 /* Check for conflicting share reservations */
4328 spin_lock(&fp->fi_lock);
4329 if (fp->fi_share_deny & deny_type)
4330 ret = nfserr_locked;
4331 spin_unlock(&fp->fi_lock);
4336 static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
4338 struct nfs4_delegation *dp = cb_to_delegation(cb);
4339 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
4342 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
4345 * We can't do this in nfsd_break_deleg_cb because it is
4346 * already holding inode->i_lock.
4348 * If the dl_time != 0, then we know that it has already been
4349 * queued for a lease break. Don't queue it again.
4351 spin_lock(&state_lock);
4352 if (dp->dl_time == 0) {
4353 dp->dl_time = get_seconds();
4354 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
4356 spin_unlock(&state_lock);
4359 static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
4360 struct rpc_task *task)
4362 struct nfs4_delegation *dp = cb_to_delegation(cb);
4364 if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID)
4367 switch (task->tk_status) {
4370 case -NFS4ERR_DELAY:
4371 rpc_delay(task, 2 * HZ);
4374 case -NFS4ERR_BAD_STATEID:
4376 * Race: client probably got cb_recall before open reply
4377 * granting delegation.
4379 if (dp->dl_retries--) {
4380 rpc_delay(task, 2 * HZ);
4389 static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
4391 struct nfs4_delegation *dp = cb_to_delegation(cb);
4393 nfs4_put_stid(&dp->dl_stid);
4396 static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
4397 .prepare = nfsd4_cb_recall_prepare,
4398 .done = nfsd4_cb_recall_done,
4399 .release = nfsd4_cb_recall_release,
4402 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
4405 * We're assuming the state code never drops its reference
4406 * without first removing the lease. Since we're in this lease
4407 * callback (and since the lease code is serialized by the
4408 * i_lock) we know the server hasn't removed the lease yet, and
4409 * we know it's safe to take a reference.
4411 refcount_inc(&dp->dl_stid.sc_count);
4412 nfsd4_run_cb(&dp->dl_recall);
4415 /* Called from break_lease() with i_lock held. */
4417 nfsd_break_deleg_cb(struct file_lock *fl)
4420 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
4421 struct nfs4_file *fp = dp->dl_stid.sc_file;
4424 * We don't want the locks code to timeout the lease for us;
4425 * we'll remove it ourself if a delegation isn't returned
4428 fl->fl_break_time = 0;
4430 spin_lock(&fp->fi_lock);
4431 fp->fi_had_conflict = true;
4432 nfsd_break_one_deleg(dp);
4433 spin_unlock(&fp->fi_lock);
4438 nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
4439 struct list_head *dispose)
4442 return lease_modify(onlist, arg, dispose);
4447 static const struct lock_manager_operations nfsd_lease_mng_ops = {
4448 .lm_break = nfsd_break_deleg_cb,
4449 .lm_change = nfsd_change_deleg_cb,
4452 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
4454 if (nfsd4_has_session(cstate))
4456 if (seqid == so->so_seqid - 1)
4457 return nfserr_replay_me;
4458 if (seqid == so->so_seqid)
4460 return nfserr_bad_seqid;
4463 static __be32 lookup_clientid(clientid_t *clid,
4464 struct nfsd4_compound_state *cstate,
4465 struct nfsd_net *nn)
4467 struct nfs4_client *found;
4470 found = cstate->clp;
4471 if (!same_clid(&found->cl_clientid, clid))
4472 return nfserr_stale_clientid;
4476 if (STALE_CLIENTID(clid, nn))
4477 return nfserr_stale_clientid;
4480 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
4481 * cached already then we know this is for is for v4.0 and "sessions"
4484 WARN_ON_ONCE(cstate->session);
4485 spin_lock(&nn->client_lock);
4486 found = find_confirmed_client(clid, false, nn);
4488 spin_unlock(&nn->client_lock);
4489 return nfserr_expired;
4491 atomic_inc(&found->cl_rpc_users);
4492 spin_unlock(&nn->client_lock);
4494 /* Cache the nfs4_client in cstate! */
4495 cstate->clp = found;
4500 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
4501 struct nfsd4_open *open, struct nfsd_net *nn)
4503 clientid_t *clientid = &open->op_clientid;
4504 struct nfs4_client *clp = NULL;
4505 unsigned int strhashval;
4506 struct nfs4_openowner *oo = NULL;
4509 if (STALE_CLIENTID(&open->op_clientid, nn))
4510 return nfserr_stale_clientid;
4512 * In case we need it later, after we've already created the
4513 * file and don't want to risk a further failure:
4515 open->op_file = nfsd4_alloc_file();
4516 if (open->op_file == NULL)
4517 return nfserr_jukebox;
4519 status = lookup_clientid(clientid, cstate, nn);
4524 strhashval = ownerstr_hashval(&open->op_owner);
4525 oo = find_openstateowner_str(strhashval, open, clp);
4526 open->op_openowner = oo;
4530 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4531 /* Replace unconfirmed owners without checking for replay. */
4532 release_openowner(oo);
4533 open->op_openowner = NULL;
4536 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
4541 oo = alloc_init_open_stateowner(strhashval, open, cstate);
4543 return nfserr_jukebox;
4544 open->op_openowner = oo;
4546 open->op_stp = nfs4_alloc_open_stateid(clp);
4548 return nfserr_jukebox;
4550 if (nfsd4_has_session(cstate) &&
4551 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
4552 open->op_odstate = alloc_clnt_odstate(clp);
4553 if (!open->op_odstate)
4554 return nfserr_jukebox;
4560 static inline __be32
4561 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
4563 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
4564 return nfserr_openmode;
4569 static int share_access_to_flags(u32 share_access)
4571 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
4574 static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
4576 struct nfs4_stid *ret;
4578 ret = find_stateid_by_type(cl, s,
4579 NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID);
4582 return delegstateid(ret);
4585 static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
4587 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
4588 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
4592 nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
4593 struct nfs4_delegation **dp)
4596 __be32 status = nfserr_bad_stateid;
4597 struct nfs4_delegation *deleg;
4599 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
4602 if (deleg->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) {
4603 nfs4_put_stid(&deleg->dl_stid);
4604 if (cl->cl_minorversion)
4605 status = nfserr_deleg_revoked;
4608 flags = share_access_to_flags(open->op_share_access);
4609 status = nfs4_check_delegmode(deleg, flags);
4611 nfs4_put_stid(&deleg->dl_stid);
4616 if (!nfsd4_is_deleg_cur(open))
4620 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
4624 static inline int nfs4_access_to_access(u32 nfs4_access)
4628 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
4629 flags |= NFSD_MAY_READ;
4630 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
4631 flags |= NFSD_MAY_WRITE;
4635 static inline __be32
4636 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
4637 struct nfsd4_open *open)
4639 struct iattr iattr = {
4640 .ia_valid = ATTR_SIZE,
4643 if (!open->op_truncate)
4645 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
4646 return nfserr_inval;
4647 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
4650 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
4651 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
4652 struct nfsd4_open *open)
4654 struct file *filp = NULL;
4656 int oflag = nfs4_access_to_omode(open->op_share_access);
4657 int access = nfs4_access_to_access(open->op_share_access);
4658 unsigned char old_access_bmap, old_deny_bmap;
4660 spin_lock(&fp->fi_lock);
4663 * Are we trying to set a deny mode that would conflict with
4666 status = nfs4_file_check_deny(fp, open->op_share_deny);
4667 if (status != nfs_ok) {
4668 spin_unlock(&fp->fi_lock);
4672 /* set access to the file */
4673 status = nfs4_file_get_access(fp, open->op_share_access);
4674 if (status != nfs_ok) {
4675 spin_unlock(&fp->fi_lock);
4679 /* Set access bits in stateid */
4680 old_access_bmap = stp->st_access_bmap;
4681 set_access(open->op_share_access, stp);
4683 /* Set new deny mask */
4684 old_deny_bmap = stp->st_deny_bmap;
4685 set_deny(open->op_share_deny, stp);
4686 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4688 if (!fp->fi_fds[oflag]) {
4689 spin_unlock(&fp->fi_lock);
4690 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
4692 goto out_put_access;
4693 spin_lock(&fp->fi_lock);
4694 if (!fp->fi_fds[oflag]) {
4695 fp->fi_fds[oflag] = filp;
4699 spin_unlock(&fp->fi_lock);
4703 status = nfsd4_truncate(rqstp, cur_fh, open);
4705 goto out_put_access;
4709 stp->st_access_bmap = old_access_bmap;
4710 nfs4_file_put_access(fp, open->op_share_access);
4711 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
4716 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)
4719 unsigned char old_deny_bmap = stp->st_deny_bmap;
4721 if (!test_access(open->op_share_access, stp))
4722 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
4724 /* test and set deny mode */
4725 spin_lock(&fp->fi_lock);
4726 status = nfs4_file_check_deny(fp, open->op_share_deny);
4727 if (status == nfs_ok) {
4728 set_deny(open->op_share_deny, stp);
4729 fp->fi_share_deny |=
4730 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4732 spin_unlock(&fp->fi_lock);
4734 if (status != nfs_ok)
4737 status = nfsd4_truncate(rqstp, cur_fh, open);
4738 if (status != nfs_ok)
4739 reset_union_bmap_deny(old_deny_bmap, stp);
4743 /* Should we give out recallable state?: */
4744 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
4746 if (clp->cl_cb_state == NFSD4_CB_UP)
4749 * In the sessions case, since we don't have to establish a
4750 * separate connection for callbacks, we assume it's OK
4751 * until we hear otherwise:
4753 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
4756 static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp,
4759 struct file_lock *fl;
4761 fl = locks_alloc_lock();
4764 fl->fl_lmops = &nfsd_lease_mng_ops;
4765 fl->fl_flags = FL_DELEG;
4766 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
4767 fl->fl_end = OFFSET_MAX;
4768 fl->fl_owner = (fl_owner_t)dp;
4769 fl->fl_pid = current->tgid;
4770 fl->fl_file = dp->dl_stid.sc_file->fi_deleg_file;
4774 static struct nfs4_delegation *
4775 nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
4776 struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate)
4779 struct nfs4_delegation *dp;
4781 struct file_lock *fl;
4784 * The fi_had_conflict and nfs_get_existing_delegation checks
4785 * here are just optimizations; we'll need to recheck them at
4788 if (fp->fi_had_conflict)
4789 return ERR_PTR(-EAGAIN);
4791 filp = find_readable_file(fp);
4793 /* We should always have a readable file here */
4795 return ERR_PTR(-EBADF);
4797 spin_lock(&state_lock);
4798 spin_lock(&fp->fi_lock);
4799 if (nfs4_delegation_exists(clp, fp))
4801 else if (!fp->fi_deleg_file) {
4802 fp->fi_deleg_file = filp;
4803 /* increment early to prevent fi_deleg_file from being
4805 fp->fi_delegees = 1;
4809 spin_unlock(&fp->fi_lock);
4810 spin_unlock(&state_lock);
4814 return ERR_PTR(status);
4817 dp = alloc_init_deleg(clp, fp, fh, odstate);
4821 fl = nfs4_alloc_init_lease(dp, NFS4_OPEN_DELEGATE_READ);
4823 goto out_clnt_odstate;
4825 status = vfs_setlease(fp->fi_deleg_file, fl->fl_type, &fl, NULL);
4827 locks_free_lock(fl);
4829 goto out_clnt_odstate;
4831 spin_lock(&state_lock);
4832 spin_lock(&fp->fi_lock);
4833 if (fp->fi_had_conflict)
4836 status = hash_delegation_locked(dp, fp);
4837 spin_unlock(&fp->fi_lock);
4838 spin_unlock(&state_lock);
4845 vfs_setlease(fp->fi_deleg_file, F_UNLCK, NULL, (void **)&dp);
4847 put_clnt_odstate(dp->dl_clnt_odstate);
4848 nfs4_put_stid(&dp->dl_stid);
4851 return ERR_PTR(status);
4854 static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
4856 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4857 if (status == -EAGAIN)
4858 open->op_why_no_deleg = WND4_CONTENTION;
4860 open->op_why_no_deleg = WND4_RESOURCE;
4861 switch (open->op_deleg_want) {
4862 case NFS4_SHARE_WANT_READ_DELEG:
4863 case NFS4_SHARE_WANT_WRITE_DELEG:
4864 case NFS4_SHARE_WANT_ANY_DELEG:
4866 case NFS4_SHARE_WANT_CANCEL:
4867 open->op_why_no_deleg = WND4_CANCELLED;
4869 case NFS4_SHARE_WANT_NO_DELEG:
4876 * Attempt to hand out a delegation.
4878 * Note we don't support write delegations, and won't until the vfs has
4879 * proper support for them.
4882 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
4883 struct nfs4_ol_stateid *stp)
4885 struct nfs4_delegation *dp;
4886 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
4887 struct nfs4_client *clp = stp->st_stid.sc_client;
4891 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
4892 open->op_recall = 0;
4893 switch (open->op_claim_type) {
4894 case NFS4_OPEN_CLAIM_PREVIOUS:
4896 open->op_recall = 1;
4897 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
4900 case NFS4_OPEN_CLAIM_NULL:
4901 case NFS4_OPEN_CLAIM_FH:
4903 * Let's not give out any delegations till everyone's
4904 * had the chance to reclaim theirs, *and* until
4905 * NLM locks have all been reclaimed:
4907 if (locks_in_grace(clp->net))
4909 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
4912 * Also, if the file was opened for write or
4913 * create, there's a good chance the client's
4914 * about to write to it, resulting in an
4915 * immediate recall (since we don't support
4916 * write delegations):
4918 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
4920 if (open->op_create == NFS4_OPEN_CREATE)
4926 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate);
4930 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
4932 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
4933 STATEID_VAL(&dp->dl_stid.sc_stateid));
4934 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
4935 nfs4_put_stid(&dp->dl_stid);
4938 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
4939 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
4940 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
4941 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
4942 open->op_recall = 1;
4945 /* 4.1 client asking for a delegation? */
4946 if (open->op_deleg_want)
4947 nfsd4_open_deleg_none_ext(open, status);
4951 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
4952 struct nfs4_delegation *dp)
4954 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
4955 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4956 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4957 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
4958 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
4959 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4960 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4961 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
4963 /* Otherwise the client must be confused wanting a delegation
4964 * it already has, therefore we don't return
4965 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
4970 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
4972 struct nfsd4_compoundres *resp = rqstp->rq_resp;
4973 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
4974 struct nfs4_file *fp = NULL;
4975 struct nfs4_ol_stateid *stp = NULL;
4976 struct nfs4_delegation *dp = NULL;
4978 bool new_stp = false;
4981 * Lookup file; if found, lookup stateid and check open request,
4982 * and check for delegations in the process of being recalled.
4983 * If not found, create the nfs4_file struct
4985 fp = find_or_add_file(open->op_file, ¤t_fh->fh_handle);
4986 if (fp != open->op_file) {
4987 status = nfs4_check_deleg(cl, open, &dp);
4990 stp = nfsd4_find_and_lock_existing_open(fp, open);
4992 open->op_file = NULL;
4993 status = nfserr_bad_stateid;
4994 if (nfsd4_is_deleg_cur(open))
4999 stp = init_open_stateid(fp, open);
5005 * OPEN the file, or upgrade an existing OPEN.
5006 * If truncate fails, the OPEN fails.
5008 * stp is already locked.
5011 /* Stateid was found, this is an OPEN upgrade */
5012 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
5014 mutex_unlock(&stp->st_mutex);
5018 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
5020 stp->st_stid.sc_type = NFS4_CLOSED_STID;
5021 release_open_stateid(stp);
5022 mutex_unlock(&stp->st_mutex);
5026 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
5028 if (stp->st_clnt_odstate == open->op_odstate)
5029 open->op_odstate = NULL;
5032 nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
5033 mutex_unlock(&stp->st_mutex);
5035 if (nfsd4_has_session(&resp->cstate)) {
5036 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
5037 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5038 open->op_why_no_deleg = WND4_NOT_WANTED;
5044 * Attempt to hand out a delegation. No error return, because the
5045 * OPEN succeeds even if we fail.
5047 nfs4_open_delegation(current_fh, open, stp);
5051 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
5052 STATEID_VAL(&stp->st_stid.sc_stateid));
5054 /* 4.1 client trying to upgrade/downgrade delegation? */
5055 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
5056 open->op_deleg_want)
5057 nfsd4_deleg_xgrade_none_ext(open, dp);
5061 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
5062 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
5064 * To finish the open response, we just need to set the rflags.
5066 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
5067 if (nfsd4_has_session(&resp->cstate))
5068 open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK;
5069 else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED))
5070 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
5073 nfs4_put_stid(&dp->dl_stid);
5075 nfs4_put_stid(&stp->st_stid);
5080 void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
5081 struct nfsd4_open *open)
5083 if (open->op_openowner) {
5084 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
5086 nfsd4_cstate_assign_replay(cstate, so);
5087 nfs4_put_stateowner(so);
5090 kmem_cache_free(file_slab, open->op_file);
5092 nfs4_put_stid(&open->op_stp->st_stid);
5093 if (open->op_odstate)
5094 kmem_cache_free(odstate_slab, open->op_odstate);
5098 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5099 union nfsd4_op_u *u)
5101 clientid_t *clid = &u->renew;
5102 struct nfs4_client *clp;
5104 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5106 dprintk("process_renew(%08x/%08x): starting\n",
5107 clid->cl_boot, clid->cl_id);
5108 status = lookup_clientid(clid, cstate, nn);
5112 status = nfserr_cb_path_down;
5113 if (!list_empty(&clp->cl_delegations)
5114 && clp->cl_cb_state != NFSD4_CB_UP)
5122 nfsd4_end_grace(struct nfsd_net *nn)
5124 /* do nothing if grace period already ended */
5125 if (nn->grace_ended)
5128 nn->grace_ended = true;
5130 * If the server goes down again right now, an NFSv4
5131 * client will still be allowed to reclaim after it comes back up,
5132 * even if it hasn't yet had a chance to reclaim state this time.
5135 nfsd4_record_grace_done(nn);
5137 * At this point, NFSv4 clients can still reclaim. But if the
5138 * server crashes, any that have not yet reclaimed will be out
5139 * of luck on the next boot.
5141 * (NFSv4.1+ clients are considered to have reclaimed once they
5142 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
5143 * have reclaimed after their first OPEN.)
5145 locks_end_grace(&nn->nfsd4_manager);
5147 * At this point, and once lockd and/or any other containers
5148 * exit their grace period, further reclaims will fail and
5149 * regular locking can resume.
5154 * If we've waited a lease period but there are still clients trying to
5155 * reclaim, wait a little longer to give them a chance to finish.
5157 static bool clients_still_reclaiming(struct nfsd_net *nn)
5159 unsigned long now = get_seconds();
5160 unsigned long double_grace_period_end = nn->boot_time +
5161 2 * nn->nfsd4_lease;
5163 if (nn->track_reclaim_completes &&
5164 atomic_read(&nn->nr_reclaim_complete) ==
5165 nn->reclaim_str_hashtbl_size)
5167 if (!nn->somebody_reclaimed)
5169 nn->somebody_reclaimed = false;
5171 * If we've given them *two* lease times to reclaim, and they're
5172 * still not done, give up:
5174 if (time_after(now, double_grace_period_end))
5180 nfs4_laundromat(struct nfsd_net *nn)
5182 struct nfs4_client *clp;
5183 struct nfs4_openowner *oo;
5184 struct nfs4_delegation *dp;
5185 struct nfs4_ol_stateid *stp;
5186 struct nfsd4_blocked_lock *nbl;
5187 struct list_head *pos, *next, reaplist;
5188 time_t cutoff = get_seconds() - nn->nfsd4_lease;
5189 time_t t, new_timeo = nn->nfsd4_lease;
5191 dprintk("NFSD: laundromat service - starting\n");
5193 if (clients_still_reclaiming(nn)) {
5197 dprintk("NFSD: end of grace period\n");
5198 nfsd4_end_grace(nn);
5199 INIT_LIST_HEAD(&reaplist);
5200 spin_lock(&nn->client_lock);
5201 list_for_each_safe(pos, next, &nn->client_lru) {
5202 clp = list_entry(pos, struct nfs4_client, cl_lru);
5203 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
5204 t = clp->cl_time - cutoff;
5205 new_timeo = min(new_timeo, t);
5208 if (mark_client_expired_locked(clp)) {
5209 dprintk("NFSD: client in use (clientid %08x)\n",
5210 clp->cl_clientid.cl_id);
5213 list_add(&clp->cl_lru, &reaplist);
5215 spin_unlock(&nn->client_lock);
5216 list_for_each_safe(pos, next, &reaplist) {
5217 clp = list_entry(pos, struct nfs4_client, cl_lru);
5218 dprintk("NFSD: purging unused client (clientid %08x)\n",
5219 clp->cl_clientid.cl_id);
5220 list_del_init(&clp->cl_lru);
5223 spin_lock(&state_lock);
5224 list_for_each_safe(pos, next, &nn->del_recall_lru) {
5225 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
5226 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
5227 t = dp->dl_time - cutoff;
5228 new_timeo = min(new_timeo, t);
5231 WARN_ON(!unhash_delegation_locked(dp));
5232 list_add(&dp->dl_recall_lru, &reaplist);
5234 spin_unlock(&state_lock);
5235 while (!list_empty(&reaplist)) {
5236 dp = list_first_entry(&reaplist, struct nfs4_delegation,
5238 list_del_init(&dp->dl_recall_lru);
5239 revoke_delegation(dp);
5242 spin_lock(&nn->client_lock);
5243 while (!list_empty(&nn->close_lru)) {
5244 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
5246 if (time_after((unsigned long)oo->oo_time,
5247 (unsigned long)cutoff)) {
5248 t = oo->oo_time - cutoff;
5249 new_timeo = min(new_timeo, t);
5252 list_del_init(&oo->oo_close_lru);
5253 stp = oo->oo_last_closed_stid;
5254 oo->oo_last_closed_stid = NULL;
5255 spin_unlock(&nn->client_lock);
5256 nfs4_put_stid(&stp->st_stid);
5257 spin_lock(&nn->client_lock);
5259 spin_unlock(&nn->client_lock);
5262 * It's possible for a client to try and acquire an already held lock
5263 * that is being held for a long time, and then lose interest in it.
5264 * So, we clean out any un-revisited request after a lease period
5265 * under the assumption that the client is no longer interested.
5267 * RFC5661, sec. 9.6 states that the client must not rely on getting
5268 * notifications and must continue to poll for locks, even when the
5269 * server supports them. Thus this shouldn't lead to clients blocking
5270 * indefinitely once the lock does become free.
5272 BUG_ON(!list_empty(&reaplist));
5273 spin_lock(&nn->blocked_locks_lock);
5274 while (!list_empty(&nn->blocked_locks_lru)) {
5275 nbl = list_first_entry(&nn->blocked_locks_lru,
5276 struct nfsd4_blocked_lock, nbl_lru);
5277 if (time_after((unsigned long)nbl->nbl_time,
5278 (unsigned long)cutoff)) {
5279 t = nbl->nbl_time - cutoff;
5280 new_timeo = min(new_timeo, t);
5283 list_move(&nbl->nbl_lru, &reaplist);
5284 list_del_init(&nbl->nbl_list);
5286 spin_unlock(&nn->blocked_locks_lock);
5288 while (!list_empty(&reaplist)) {
5289 nbl = list_first_entry(&reaplist,
5290 struct nfsd4_blocked_lock, nbl_lru);
5291 list_del_init(&nbl->nbl_lru);
5292 free_blocked_lock(nbl);
5295 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
5299 static struct workqueue_struct *laundry_wq;
5300 static void laundromat_main(struct work_struct *);
5303 laundromat_main(struct work_struct *laundry)
5306 struct delayed_work *dwork = to_delayed_work(laundry);
5307 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
5310 t = nfs4_laundromat(nn);
5311 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
5312 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
5315 static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
5317 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
5318 return nfserr_bad_stateid;
5323 access_permit_read(struct nfs4_ol_stateid *stp)
5325 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
5326 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
5327 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
5331 access_permit_write(struct nfs4_ol_stateid *stp)
5333 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
5334 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
5338 __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
5340 __be32 status = nfserr_openmode;
5342 /* For lock stateid's, we test the parent open, not the lock: */
5343 if (stp->st_openstp)
5344 stp = stp->st_openstp;
5345 if ((flags & WR_STATE) && !access_permit_write(stp))
5347 if ((flags & RD_STATE) && !access_permit_read(stp))
5354 static inline __be32
5355 check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
5357 if (ONE_STATEID(stateid) && (flags & RD_STATE))
5359 else if (opens_in_grace(net)) {
5360 /* Answer in remaining cases depends on existence of
5361 * conflicting state; so we must wait out the grace period. */
5362 return nfserr_grace;
5363 } else if (flags & WR_STATE)
5364 return nfs4_share_conflict(current_fh,
5365 NFS4_SHARE_DENY_WRITE);
5366 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
5367 return nfs4_share_conflict(current_fh,
5368 NFS4_SHARE_DENY_READ);
5372 * Allow READ/WRITE during grace period on recovered state only for files
5373 * that are not able to provide mandatory locking.
5376 grace_disallows_io(struct net *net, struct inode *inode)
5378 return opens_in_grace(net) && mandatory_lock(inode);
5381 static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
5384 * When sessions are used the stateid generation number is ignored
5387 if (has_session && in->si_generation == 0)
5390 if (in->si_generation == ref->si_generation)
5393 /* If the client sends us a stateid from the future, it's buggy: */
5394 if (nfsd4_stateid_generation_after(in, ref))
5395 return nfserr_bad_stateid;
5397 * However, we could see a stateid from the past, even from a
5398 * non-buggy client. For example, if the client sends a lock
5399 * while some IO is outstanding, the lock may bump si_generation
5400 * while the IO is still in flight. The client could avoid that
5401 * situation by waiting for responses on all the IO requests,
5402 * but better performance may result in retrying IO that
5403 * receives an old_stateid error if requests are rarely
5404 * reordered in flight:
5406 return nfserr_old_stateid;
5409 static __be32 nfsd4_stid_check_stateid_generation(stateid_t *in, struct nfs4_stid *s, bool has_session)
5413 spin_lock(&s->sc_lock);
5414 ret = nfsd4_verify_open_stid(s);
5416 ret = check_stateid_generation(in, &s->sc_stateid, has_session);
5417 spin_unlock(&s->sc_lock);
5421 static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
5423 if (ols->st_stateowner->so_is_open_owner &&
5424 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
5425 return nfserr_bad_stateid;
5429 static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
5431 struct nfs4_stid *s;
5432 __be32 status = nfserr_bad_stateid;
5434 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5435 CLOSE_STATEID(stateid))
5437 /* Client debugging aid. */
5438 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
5439 char addr_str[INET6_ADDRSTRLEN];
5440 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
5442 pr_warn_ratelimited("NFSD: client %s testing state ID "
5443 "with incorrect client ID\n", addr_str);
5446 spin_lock(&cl->cl_lock);
5447 s = find_stateid_locked(cl, stateid);
5450 status = nfsd4_stid_check_stateid_generation(stateid, s, 1);
5453 switch (s->sc_type) {
5454 case NFS4_DELEG_STID:
5457 case NFS4_REVOKED_DELEG_STID:
5458 status = nfserr_deleg_revoked;
5460 case NFS4_OPEN_STID:
5461 case NFS4_LOCK_STID:
5462 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
5465 printk("unknown stateid type %x\n", s->sc_type);
5467 case NFS4_CLOSED_STID:
5468 case NFS4_CLOSED_DELEG_STID:
5469 status = nfserr_bad_stateid;
5472 spin_unlock(&cl->cl_lock);
5477 nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
5478 stateid_t *stateid, unsigned char typemask,
5479 struct nfs4_stid **s, struct nfsd_net *nn)
5482 bool return_revoked = false;
5485 * only return revoked delegations if explicitly asked.
5486 * otherwise we report revoked or bad_stateid status.
5488 if (typemask & NFS4_REVOKED_DELEG_STID)
5489 return_revoked = true;
5490 else if (typemask & NFS4_DELEG_STID)
5491 typemask |= NFS4_REVOKED_DELEG_STID;
5493 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5494 CLOSE_STATEID(stateid))
5495 return nfserr_bad_stateid;
5496 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
5497 if (status == nfserr_stale_clientid) {
5498 if (cstate->session)
5499 return nfserr_bad_stateid;
5500 return nfserr_stale_stateid;
5504 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
5506 return nfserr_bad_stateid;
5507 if (((*s)->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) {
5509 if (cstate->minorversion)
5510 return nfserr_deleg_revoked;
5511 return nfserr_bad_stateid;
5516 static struct file *
5517 nfs4_find_file(struct nfs4_stid *s, int flags)
5522 switch (s->sc_type) {
5523 case NFS4_DELEG_STID:
5524 if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
5526 return get_file(s->sc_file->fi_deleg_file);
5527 case NFS4_OPEN_STID:
5528 case NFS4_LOCK_STID:
5529 if (flags & RD_STATE)
5530 return find_readable_file(s->sc_file);
5532 return find_writeable_file(s->sc_file);
5540 nfs4_check_olstateid(struct nfs4_ol_stateid *ols, int flags)
5544 status = nfsd4_check_openowner_confirmed(ols);
5547 return nfs4_check_openmode(ols, flags);
5551 nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
5552 struct file **filpp, bool *tmp_file, int flags)
5554 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
5558 file = nfs4_find_file(s, flags);
5560 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
5561 acc | NFSD_MAY_OWNER_OVERRIDE);
5569 status = nfsd_open(rqstp, fhp, S_IFREG, acc, filpp);
5581 * Checks for stateid operations
5584 nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
5585 struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
5586 stateid_t *stateid, int flags, struct file **filpp, bool *tmp_file)
5588 struct inode *ino = d_inode(fhp->fh_dentry);
5589 struct net *net = SVC_NET(rqstp);
5590 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5591 struct nfs4_stid *s = NULL;
5599 if (grace_disallows_io(net, ino))
5600 return nfserr_grace;
5602 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
5603 status = check_special_stateids(net, fhp, stateid, flags);
5607 status = nfsd4_lookup_stateid(cstate, stateid,
5608 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
5612 status = nfsd4_stid_check_stateid_generation(stateid, s,
5613 nfsd4_has_session(cstate));
5617 switch (s->sc_type) {
5618 case NFS4_DELEG_STID:
5619 status = nfs4_check_delegmode(delegstateid(s), flags);
5621 case NFS4_OPEN_STID:
5622 case NFS4_LOCK_STID:
5623 status = nfs4_check_olstateid(openlockstateid(s), flags);
5626 status = nfserr_bad_stateid;
5631 status = nfs4_check_fh(fhp, s);
5634 if (!status && filpp)
5635 status = nfs4_check_file(rqstp, fhp, s, filpp, tmp_file, flags);
5643 * Test if the stateid is valid
5646 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5647 union nfsd4_op_u *u)
5649 struct nfsd4_test_stateid *test_stateid = &u->test_stateid;
5650 struct nfsd4_test_stateid_id *stateid;
5651 struct nfs4_client *cl = cstate->session->se_client;
5653 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
5654 stateid->ts_id_status =
5655 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
5661 nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
5663 struct nfs4_ol_stateid *stp = openlockstateid(s);
5666 ret = nfsd4_lock_ol_stateid(stp);
5670 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5674 ret = nfserr_locks_held;
5675 if (check_for_locks(stp->st_stid.sc_file,
5676 lockowner(stp->st_stateowner)))
5679 release_lock_stateid(stp);
5683 mutex_unlock(&stp->st_mutex);
5690 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5691 union nfsd4_op_u *u)
5693 struct nfsd4_free_stateid *free_stateid = &u->free_stateid;
5694 stateid_t *stateid = &free_stateid->fr_stateid;
5695 struct nfs4_stid *s;
5696 struct nfs4_delegation *dp;
5697 struct nfs4_client *cl = cstate->session->se_client;
5698 __be32 ret = nfserr_bad_stateid;
5700 spin_lock(&cl->cl_lock);
5701 s = find_stateid_locked(cl, stateid);
5704 spin_lock(&s->sc_lock);
5705 switch (s->sc_type) {
5706 case NFS4_DELEG_STID:
5707 ret = nfserr_locks_held;
5709 case NFS4_OPEN_STID:
5710 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5713 ret = nfserr_locks_held;
5715 case NFS4_LOCK_STID:
5716 spin_unlock(&s->sc_lock);
5717 refcount_inc(&s->sc_count);
5718 spin_unlock(&cl->cl_lock);
5719 ret = nfsd4_free_lock_stateid(stateid, s);
5721 case NFS4_REVOKED_DELEG_STID:
5722 spin_unlock(&s->sc_lock);
5723 dp = delegstateid(s);
5724 list_del_init(&dp->dl_recall_lru);
5725 spin_unlock(&cl->cl_lock);
5729 /* Default falls through and returns nfserr_bad_stateid */
5731 spin_unlock(&s->sc_lock);
5733 spin_unlock(&cl->cl_lock);
5741 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
5742 RD_STATE : WR_STATE;
5745 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
5747 struct svc_fh *current_fh = &cstate->current_fh;
5748 struct nfs4_stateowner *sop = stp->st_stateowner;
5751 status = nfsd4_check_seqid(cstate, sop, seqid);
5754 status = nfsd4_lock_ol_stateid(stp);
5755 if (status != nfs_ok)
5757 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
5758 if (status == nfs_ok)
5759 status = nfs4_check_fh(current_fh, &stp->st_stid);
5760 if (status != nfs_ok)
5761 mutex_unlock(&stp->st_mutex);
5766 * Checks for sequence id mutating operations.
5769 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
5770 stateid_t *stateid, char typemask,
5771 struct nfs4_ol_stateid **stpp,
5772 struct nfsd_net *nn)
5775 struct nfs4_stid *s;
5776 struct nfs4_ol_stateid *stp = NULL;
5778 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
5779 seqid, STATEID_VAL(stateid));
5782 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
5785 stp = openlockstateid(s);
5786 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
5788 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
5792 nfs4_put_stid(&stp->st_stid);
5796 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
5797 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
5800 struct nfs4_openowner *oo;
5801 struct nfs4_ol_stateid *stp;
5803 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
5804 NFS4_OPEN_STID, &stp, nn);
5807 oo = openowner(stp->st_stateowner);
5808 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
5809 mutex_unlock(&stp->st_mutex);
5810 nfs4_put_stid(&stp->st_stid);
5811 return nfserr_bad_stateid;
5818 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5819 union nfsd4_op_u *u)
5821 struct nfsd4_open_confirm *oc = &u->open_confirm;
5823 struct nfs4_openowner *oo;
5824 struct nfs4_ol_stateid *stp;
5825 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5827 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
5828 cstate->current_fh.fh_dentry);
5830 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
5834 status = nfs4_preprocess_seqid_op(cstate,
5835 oc->oc_seqid, &oc->oc_req_stateid,
5836 NFS4_OPEN_STID, &stp, nn);
5839 oo = openowner(stp->st_stateowner);
5840 status = nfserr_bad_stateid;
5841 if (oo->oo_flags & NFS4_OO_CONFIRMED) {
5842 mutex_unlock(&stp->st_mutex);
5845 oo->oo_flags |= NFS4_OO_CONFIRMED;
5846 nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
5847 mutex_unlock(&stp->st_mutex);
5848 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
5849 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
5851 nfsd4_client_record_create(oo->oo_owner.so_client);
5854 nfs4_put_stid(&stp->st_stid);
5856 nfsd4_bump_seqid(cstate, status);
5860 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
5862 if (!test_access(access, stp))
5864 nfs4_file_put_access(stp->st_stid.sc_file, access);
5865 clear_access(access, stp);
5868 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
5870 switch (to_access) {
5871 case NFS4_SHARE_ACCESS_READ:
5872 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
5873 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
5875 case NFS4_SHARE_ACCESS_WRITE:
5876 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
5877 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
5879 case NFS4_SHARE_ACCESS_BOTH:
5887 nfsd4_open_downgrade(struct svc_rqst *rqstp,
5888 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
5890 struct nfsd4_open_downgrade *od = &u->open_downgrade;
5892 struct nfs4_ol_stateid *stp;
5893 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5895 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
5896 cstate->current_fh.fh_dentry);
5898 /* We don't yet support WANT bits: */
5899 if (od->od_deleg_want)
5900 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
5903 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
5904 &od->od_stateid, &stp, nn);
5907 status = nfserr_inval;
5908 if (!test_access(od->od_share_access, stp)) {
5909 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
5910 stp->st_access_bmap, od->od_share_access);
5913 if (!test_deny(od->od_share_deny, stp)) {
5914 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
5915 stp->st_deny_bmap, od->od_share_deny);
5918 nfs4_stateid_downgrade(stp, od->od_share_access);
5919 reset_union_bmap_deny(od->od_share_deny, stp);
5920 nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
5923 mutex_unlock(&stp->st_mutex);
5924 nfs4_put_stid(&stp->st_stid);
5926 nfsd4_bump_seqid(cstate, status);
5930 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
5932 struct nfs4_client *clp = s->st_stid.sc_client;
5934 LIST_HEAD(reaplist);
5936 spin_lock(&clp->cl_lock);
5937 unhashed = unhash_open_stateid(s, &reaplist);
5939 if (clp->cl_minorversion) {
5941 put_ol_stateid_locked(s, &reaplist);
5942 spin_unlock(&clp->cl_lock);
5943 free_ol_stateid_reaplist(&reaplist);
5945 spin_unlock(&clp->cl_lock);
5946 free_ol_stateid_reaplist(&reaplist);
5948 move_to_close_lru(s, clp->net);
5953 * nfs4_unlock_state() called after encode
5956 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5957 union nfsd4_op_u *u)
5959 struct nfsd4_close *close = &u->close;
5961 struct nfs4_ol_stateid *stp;
5962 struct net *net = SVC_NET(rqstp);
5963 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5965 dprintk("NFSD: nfsd4_close on file %pd\n",
5966 cstate->current_fh.fh_dentry);
5968 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
5970 NFS4_OPEN_STID|NFS4_CLOSED_STID,
5972 nfsd4_bump_seqid(cstate, status);
5976 stp->st_stid.sc_type = NFS4_CLOSED_STID;
5979 * Technically we don't _really_ have to increment or copy it, since
5980 * it should just be gone after this operation and we clobber the
5981 * copied value below, but we continue to do so here just to ensure
5982 * that racing ops see that there was a state change.
5984 nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
5986 nfsd4_close_open_stateid(stp);
5987 mutex_unlock(&stp->st_mutex);
5989 /* v4.1+ suggests that we send a special stateid in here, since the
5990 * clients should just ignore this anyway. Since this is not useful
5991 * for v4.0 clients either, we set it to the special close_stateid
5994 * See RFC5661 section 18.2.4, and RFC7530 section 16.2.5
5996 memcpy(&close->cl_stateid, &close_stateid, sizeof(close->cl_stateid));
5998 /* put reference from nfs4_preprocess_seqid_op */
5999 nfs4_put_stid(&stp->st_stid);
6005 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6006 union nfsd4_op_u *u)
6008 struct nfsd4_delegreturn *dr = &u->delegreturn;
6009 struct nfs4_delegation *dp;
6010 stateid_t *stateid = &dr->dr_stateid;
6011 struct nfs4_stid *s;
6013 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6015 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
6018 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
6021 dp = delegstateid(s);
6022 status = nfsd4_stid_check_stateid_generation(stateid, &dp->dl_stid, nfsd4_has_session(cstate));
6026 destroy_delegation(dp);
6028 nfs4_put_stid(&dp->dl_stid);
6034 end_offset(u64 start, u64 len)
6039 return end >= start ? end: NFS4_MAX_UINT64;
6042 /* last octet in a range */
6044 last_byte_offset(u64 start, u64 len)
6050 return end > start ? end - 1: NFS4_MAX_UINT64;
6054 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
6055 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
6056 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
6057 * locking, this prevents us from being completely protocol-compliant. The
6058 * real solution to this problem is to start using unsigned file offsets in
6059 * the VFS, but this is a very deep change!
6062 nfs4_transform_lock_offset(struct file_lock *lock)
6064 if (lock->fl_start < 0)
6065 lock->fl_start = OFFSET_MAX;
6066 if (lock->fl_end < 0)
6067 lock->fl_end = OFFSET_MAX;
6071 nfsd4_fl_get_owner(fl_owner_t owner)
6073 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
6075 nfs4_get_stateowner(&lo->lo_owner);
6080 nfsd4_fl_put_owner(fl_owner_t owner)
6082 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
6085 nfs4_put_stateowner(&lo->lo_owner);
6089 nfsd4_lm_notify(struct file_lock *fl)
6091 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
6092 struct net *net = lo->lo_owner.so_client->net;
6093 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6094 struct nfsd4_blocked_lock *nbl = container_of(fl,
6095 struct nfsd4_blocked_lock, nbl_lock);
6098 /* An empty list means that something else is going to be using it */
6099 spin_lock(&nn->blocked_locks_lock);
6100 if (!list_empty(&nbl->nbl_list)) {
6101 list_del_init(&nbl->nbl_list);
6102 list_del_init(&nbl->nbl_lru);
6105 spin_unlock(&nn->blocked_locks_lock);
6108 nfsd4_run_cb(&nbl->nbl_cb);
6111 static const struct lock_manager_operations nfsd_posix_mng_ops = {
6112 .lm_notify = nfsd4_lm_notify,
6113 .lm_get_owner = nfsd4_fl_get_owner,
6114 .lm_put_owner = nfsd4_fl_put_owner,
6118 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
6120 struct nfs4_lockowner *lo;
6122 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
6123 lo = (struct nfs4_lockowner *) fl->fl_owner;
6124 xdr_netobj_dup(&deny->ld_owner, &lo->lo_owner.so_owner,
6126 if (!deny->ld_owner.data)
6127 /* We just don't care that much */
6129 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
6132 deny->ld_owner.len = 0;
6133 deny->ld_owner.data = NULL;
6134 deny->ld_clientid.cl_boot = 0;
6135 deny->ld_clientid.cl_id = 0;
6137 deny->ld_start = fl->fl_start;
6138 deny->ld_length = NFS4_MAX_UINT64;
6139 if (fl->fl_end != NFS4_MAX_UINT64)
6140 deny->ld_length = fl->fl_end - fl->fl_start + 1;
6141 deny->ld_type = NFS4_READ_LT;
6142 if (fl->fl_type != F_RDLCK)
6143 deny->ld_type = NFS4_WRITE_LT;
6146 static struct nfs4_lockowner *
6147 find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
6149 unsigned int strhashval = ownerstr_hashval(owner);
6150 struct nfs4_stateowner *so;
6152 lockdep_assert_held(&clp->cl_lock);
6154 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
6156 if (so->so_is_open_owner)
6158 if (same_owner_str(so, owner))
6159 return lockowner(nfs4_get_stateowner(so));
6164 static struct nfs4_lockowner *
6165 find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
6167 struct nfs4_lockowner *lo;
6169 spin_lock(&clp->cl_lock);
6170 lo = find_lockowner_str_locked(clp, owner);
6171 spin_unlock(&clp->cl_lock);
6175 static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
6177 unhash_lockowner_locked(lockowner(sop));
6180 static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
6182 struct nfs4_lockowner *lo = lockowner(sop);
6184 kmem_cache_free(lockowner_slab, lo);
6187 static const struct nfs4_stateowner_operations lockowner_ops = {
6188 .so_unhash = nfs4_unhash_lockowner,
6189 .so_free = nfs4_free_lockowner,
6193 * Alloc a lock owner structure.
6194 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
6197 * strhashval = ownerstr_hashval
6199 static struct nfs4_lockowner *
6200 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
6201 struct nfs4_ol_stateid *open_stp,
6202 struct nfsd4_lock *lock)
6204 struct nfs4_lockowner *lo, *ret;
6206 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
6209 INIT_LIST_HEAD(&lo->lo_blocked);
6210 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
6211 lo->lo_owner.so_is_open_owner = 0;
6212 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
6213 lo->lo_owner.so_ops = &lockowner_ops;
6214 spin_lock(&clp->cl_lock);
6215 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
6217 list_add(&lo->lo_owner.so_strhash,
6218 &clp->cl_ownerstr_hashtbl[strhashval]);
6221 nfs4_free_stateowner(&lo->lo_owner);
6223 spin_unlock(&clp->cl_lock);
6227 static struct nfs4_ol_stateid *
6228 find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
6230 struct nfs4_ol_stateid *lst;
6231 struct nfs4_client *clp = lo->lo_owner.so_client;
6233 lockdep_assert_held(&clp->cl_lock);
6235 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
6236 if (lst->st_stid.sc_type != NFS4_LOCK_STID)
6238 if (lst->st_stid.sc_file == fp) {
6239 refcount_inc(&lst->st_stid.sc_count);
6246 static struct nfs4_ol_stateid *
6247 init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
6248 struct nfs4_file *fp, struct inode *inode,
6249 struct nfs4_ol_stateid *open_stp)
6251 struct nfs4_client *clp = lo->lo_owner.so_client;
6252 struct nfs4_ol_stateid *retstp;
6254 mutex_init(&stp->st_mutex);
6255 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
6257 spin_lock(&clp->cl_lock);
6258 spin_lock(&fp->fi_lock);
6259 retstp = find_lock_stateid(lo, fp);
6263 refcount_inc(&stp->st_stid.sc_count);
6264 stp->st_stid.sc_type = NFS4_LOCK_STID;
6265 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
6267 stp->st_stid.sc_file = fp;
6268 stp->st_access_bmap = 0;
6269 stp->st_deny_bmap = open_stp->st_deny_bmap;
6270 stp->st_openstp = open_stp;
6271 list_add(&stp->st_locks, &open_stp->st_locks);
6272 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
6273 list_add(&stp->st_perfile, &fp->fi_stateids);
6275 spin_unlock(&fp->fi_lock);
6276 spin_unlock(&clp->cl_lock);
6278 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
6279 nfs4_put_stid(&retstp->st_stid);
6282 /* To keep mutex tracking happy */
6283 mutex_unlock(&stp->st_mutex);
6289 static struct nfs4_ol_stateid *
6290 find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
6291 struct inode *inode, struct nfs4_ol_stateid *ost,
6294 struct nfs4_stid *ns = NULL;
6295 struct nfs4_ol_stateid *lst;
6296 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
6297 struct nfs4_client *clp = oo->oo_owner.so_client;
6300 spin_lock(&clp->cl_lock);
6301 lst = find_lock_stateid(lo, fi);
6302 spin_unlock(&clp->cl_lock);
6304 if (nfsd4_lock_ol_stateid(lst) == nfs_ok)
6306 nfs4_put_stid(&lst->st_stid);
6308 ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid);
6312 lst = init_lock_stateid(openlockstateid(ns), lo, fi, inode, ost);
6313 if (lst == openlockstateid(ns))
6322 check_lock_length(u64 offset, u64 length)
6324 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
6325 (length > ~offset)));
6328 static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
6330 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
6332 lockdep_assert_held(&fp->fi_lock);
6334 if (test_access(access, lock_stp))
6336 __nfs4_file_get_access(fp, access);
6337 set_access(access, lock_stp);
6341 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
6342 struct nfs4_ol_stateid *ost,
6343 struct nfsd4_lock *lock,
6344 struct nfs4_ol_stateid **plst, bool *new)
6347 struct nfs4_file *fi = ost->st_stid.sc_file;
6348 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
6349 struct nfs4_client *cl = oo->oo_owner.so_client;
6350 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
6351 struct nfs4_lockowner *lo;
6352 struct nfs4_ol_stateid *lst;
6353 unsigned int strhashval;
6355 lo = find_lockowner_str(cl, &lock->lk_new_owner);
6357 strhashval = ownerstr_hashval(&lock->lk_new_owner);
6358 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
6360 return nfserr_jukebox;
6362 /* with an existing lockowner, seqids must be the same */
6363 status = nfserr_bad_seqid;
6364 if (!cstate->minorversion &&
6365 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
6369 lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
6371 status = nfserr_jukebox;
6378 nfs4_put_stateowner(&lo->lo_owner);
6386 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6387 union nfsd4_op_u *u)
6389 struct nfsd4_lock *lock = &u->lock;
6390 struct nfs4_openowner *open_sop = NULL;
6391 struct nfs4_lockowner *lock_sop = NULL;
6392 struct nfs4_ol_stateid *lock_stp = NULL;
6393 struct nfs4_ol_stateid *open_stp = NULL;
6394 struct nfs4_file *fp;
6395 struct file *filp = NULL;
6396 struct nfsd4_blocked_lock *nbl = NULL;
6397 struct file_lock *file_lock = NULL;
6398 struct file_lock *conflock = NULL;
6403 unsigned char fl_type;
6404 unsigned int fl_flags = FL_POSIX;
6405 struct net *net = SVC_NET(rqstp);
6406 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6408 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
6409 (long long) lock->lk_offset,
6410 (long long) lock->lk_length);
6412 if (check_lock_length(lock->lk_offset, lock->lk_length))
6413 return nfserr_inval;
6415 if ((status = fh_verify(rqstp, &cstate->current_fh,
6416 S_IFREG, NFSD_MAY_LOCK))) {
6417 dprintk("NFSD: nfsd4_lock: permission denied!\n");
6421 if (lock->lk_is_new) {
6422 if (nfsd4_has_session(cstate))
6423 /* See rfc 5661 18.10.3: given clientid is ignored: */
6424 memcpy(&lock->lk_new_clientid,
6425 &cstate->session->se_client->cl_clientid,
6426 sizeof(clientid_t));
6428 status = nfserr_stale_clientid;
6429 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
6432 /* validate and update open stateid and open seqid */
6433 status = nfs4_preprocess_confirmed_seqid_op(cstate,
6434 lock->lk_new_open_seqid,
6435 &lock->lk_new_open_stateid,
6439 mutex_unlock(&open_stp->st_mutex);
6440 open_sop = openowner(open_stp->st_stateowner);
6441 status = nfserr_bad_stateid;
6442 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
6443 &lock->lk_new_clientid))
6445 status = lookup_or_create_lock_state(cstate, open_stp, lock,
6448 status = nfs4_preprocess_seqid_op(cstate,
6449 lock->lk_old_lock_seqid,
6450 &lock->lk_old_lock_stateid,
6451 NFS4_LOCK_STID, &lock_stp, nn);
6455 lock_sop = lockowner(lock_stp->st_stateowner);
6457 lkflg = setlkflg(lock->lk_type);
6458 status = nfs4_check_openmode(lock_stp, lkflg);
6462 status = nfserr_grace;
6463 if (locks_in_grace(net) && !lock->lk_reclaim)
6465 status = nfserr_no_grace;
6466 if (!locks_in_grace(net) && lock->lk_reclaim)
6469 fp = lock_stp->st_stid.sc_file;
6470 switch (lock->lk_type) {
6472 if (nfsd4_has_session(cstate))
6473 fl_flags |= FL_SLEEP;
6476 spin_lock(&fp->fi_lock);
6477 filp = find_readable_file_locked(fp);
6479 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
6480 spin_unlock(&fp->fi_lock);
6483 case NFS4_WRITEW_LT:
6484 if (nfsd4_has_session(cstate))
6485 fl_flags |= FL_SLEEP;
6488 spin_lock(&fp->fi_lock);
6489 filp = find_writeable_file_locked(fp);
6491 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
6492 spin_unlock(&fp->fi_lock);
6496 status = nfserr_inval;
6501 status = nfserr_openmode;
6505 nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
6507 dprintk("NFSD: %s: unable to allocate block!\n", __func__);
6508 status = nfserr_jukebox;
6512 file_lock = &nbl->nbl_lock;
6513 file_lock->fl_type = fl_type;
6514 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
6515 file_lock->fl_pid = current->tgid;
6516 file_lock->fl_file = filp;
6517 file_lock->fl_flags = fl_flags;
6518 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6519 file_lock->fl_start = lock->lk_offset;
6520 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
6521 nfs4_transform_lock_offset(file_lock);
6523 conflock = locks_alloc_lock();
6525 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6526 status = nfserr_jukebox;
6530 if (fl_flags & FL_SLEEP) {
6531 nbl->nbl_time = jiffies;
6532 spin_lock(&nn->blocked_locks_lock);
6533 list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
6534 list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
6535 spin_unlock(&nn->blocked_locks_lock);
6538 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
6540 case 0: /* success! */
6541 nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
6543 if (lock->lk_reclaim)
6544 nn->somebody_reclaimed = true;
6546 case FILE_LOCK_DEFERRED:
6549 case -EAGAIN: /* conflock holds conflicting lock */
6550 status = nfserr_denied;
6551 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
6552 nfs4_set_lock_denied(conflock, &lock->lk_denied);
6555 status = nfserr_deadlock;
6558 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
6559 status = nfserrno(err);
6564 /* dequeue it if we queued it before */
6565 if (fl_flags & FL_SLEEP) {
6566 spin_lock(&nn->blocked_locks_lock);
6567 list_del_init(&nbl->nbl_list);
6568 list_del_init(&nbl->nbl_lru);
6569 spin_unlock(&nn->blocked_locks_lock);
6571 free_blocked_lock(nbl);
6576 /* Bump seqid manually if the 4.0 replay owner is openowner */
6577 if (cstate->replay_owner &&
6578 cstate->replay_owner != &lock_sop->lo_owner &&
6579 seqid_mutating_err(ntohl(status)))
6580 lock_sop->lo_owner.so_seqid++;
6583 * If this is a new, never-before-used stateid, and we are
6584 * returning an error, then just go ahead and release it.
6587 release_lock_stateid(lock_stp);
6589 mutex_unlock(&lock_stp->st_mutex);
6591 nfs4_put_stid(&lock_stp->st_stid);
6594 nfs4_put_stid(&open_stp->st_stid);
6595 nfsd4_bump_seqid(cstate, status);
6597 locks_free_lock(conflock);
6602 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
6603 * so we do a temporary open here just to get an open file to pass to
6604 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
6607 static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
6610 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
6612 err = nfserrno(vfs_test_lock(file, lock));
6622 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6623 union nfsd4_op_u *u)
6625 struct nfsd4_lockt *lockt = &u->lockt;
6626 struct file_lock *file_lock = NULL;
6627 struct nfs4_lockowner *lo = NULL;
6629 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6631 if (locks_in_grace(SVC_NET(rqstp)))
6632 return nfserr_grace;
6634 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
6635 return nfserr_inval;
6637 if (!nfsd4_has_session(cstate)) {
6638 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
6643 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
6646 file_lock = locks_alloc_lock();
6648 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6649 status = nfserr_jukebox;
6653 switch (lockt->lt_type) {
6656 file_lock->fl_type = F_RDLCK;
6659 case NFS4_WRITEW_LT:
6660 file_lock->fl_type = F_WRLCK;
6663 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
6664 status = nfserr_inval;
6668 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
6670 file_lock->fl_owner = (fl_owner_t)lo;
6671 file_lock->fl_pid = current->tgid;
6672 file_lock->fl_flags = FL_POSIX;
6674 file_lock->fl_start = lockt->lt_offset;
6675 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
6677 nfs4_transform_lock_offset(file_lock);
6679 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
6683 if (file_lock->fl_type != F_UNLCK) {
6684 status = nfserr_denied;
6685 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
6689 nfs4_put_stateowner(&lo->lo_owner);
6691 locks_free_lock(file_lock);
6696 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6697 union nfsd4_op_u *u)
6699 struct nfsd4_locku *locku = &u->locku;
6700 struct nfs4_ol_stateid *stp;
6701 struct file *filp = NULL;
6702 struct file_lock *file_lock = NULL;
6705 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6707 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
6708 (long long) locku->lu_offset,
6709 (long long) locku->lu_length);
6711 if (check_lock_length(locku->lu_offset, locku->lu_length))
6712 return nfserr_inval;
6714 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
6715 &locku->lu_stateid, NFS4_LOCK_STID,
6719 filp = find_any_file(stp->st_stid.sc_file);
6721 status = nfserr_lock_range;
6724 file_lock = locks_alloc_lock();
6726 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6727 status = nfserr_jukebox;
6731 file_lock->fl_type = F_UNLCK;
6732 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
6733 file_lock->fl_pid = current->tgid;
6734 file_lock->fl_file = filp;
6735 file_lock->fl_flags = FL_POSIX;
6736 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6737 file_lock->fl_start = locku->lu_offset;
6739 file_lock->fl_end = last_byte_offset(locku->lu_offset,
6741 nfs4_transform_lock_offset(file_lock);
6743 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
6745 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
6748 nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
6752 mutex_unlock(&stp->st_mutex);
6753 nfs4_put_stid(&stp->st_stid);
6755 nfsd4_bump_seqid(cstate, status);
6757 locks_free_lock(file_lock);
6761 status = nfserrno(err);
6767 * true: locks held by lockowner
6768 * false: no locks held by lockowner
6771 check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
6773 struct file_lock *fl;
6775 struct file *filp = find_any_file(fp);
6776 struct inode *inode;
6777 struct file_lock_context *flctx;
6780 /* Any valid lock stateid should have some sort of access */
6785 inode = locks_inode(filp);
6786 flctx = inode->i_flctx;
6788 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
6789 spin_lock(&flctx->flc_lock);
6790 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
6791 if (fl->fl_owner == (fl_owner_t)lowner) {
6796 spin_unlock(&flctx->flc_lock);
6803 nfsd4_release_lockowner(struct svc_rqst *rqstp,
6804 struct nfsd4_compound_state *cstate,
6805 union nfsd4_op_u *u)
6807 struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner;
6808 clientid_t *clid = &rlockowner->rl_clientid;
6809 struct nfs4_stateowner *sop;
6810 struct nfs4_lockowner *lo = NULL;
6811 struct nfs4_ol_stateid *stp;
6812 struct xdr_netobj *owner = &rlockowner->rl_owner;
6813 unsigned int hashval = ownerstr_hashval(owner);
6815 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6816 struct nfs4_client *clp;
6817 LIST_HEAD (reaplist);
6819 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
6820 clid->cl_boot, clid->cl_id);
6822 status = lookup_clientid(clid, cstate, nn);
6827 /* Find the matching lock stateowner */
6828 spin_lock(&clp->cl_lock);
6829 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
6832 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
6835 /* see if there are still any locks associated with it */
6836 lo = lockowner(sop);
6837 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
6838 if (check_for_locks(stp->st_stid.sc_file, lo)) {
6839 status = nfserr_locks_held;
6840 spin_unlock(&clp->cl_lock);
6845 nfs4_get_stateowner(sop);
6849 spin_unlock(&clp->cl_lock);
6853 unhash_lockowner_locked(lo);
6854 while (!list_empty(&lo->lo_owner.so_stateids)) {
6855 stp = list_first_entry(&lo->lo_owner.so_stateids,
6856 struct nfs4_ol_stateid,
6858 WARN_ON(!unhash_lock_stateid(stp));
6859 put_ol_stateid_locked(stp, &reaplist);
6861 spin_unlock(&clp->cl_lock);
6862 free_ol_stateid_reaplist(&reaplist);
6863 remove_blocked_locks(lo);
6864 nfs4_put_stateowner(&lo->lo_owner);
6869 static inline struct nfs4_client_reclaim *
6872 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
6876 nfs4_has_reclaimed_state(struct xdr_netobj name, struct nfsd_net *nn)
6878 struct nfs4_client_reclaim *crp;
6880 crp = nfsd4_find_reclaim_client(name, nn);
6881 return (crp && crp->cr_clp);
6885 * failure => all reset bets are off, nfserr_no_grace...
6887 * The caller is responsible for freeing name.data if NULL is returned (it
6888 * will be freed in nfs4_remove_reclaim_record in the normal case).
6890 struct nfs4_client_reclaim *
6891 nfs4_client_to_reclaim(struct xdr_netobj name, struct nfsd_net *nn)
6893 unsigned int strhashval;
6894 struct nfs4_client_reclaim *crp;
6896 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", name.len, name.data);
6897 crp = alloc_reclaim();
6899 strhashval = clientstr_hashval(name);
6900 INIT_LIST_HEAD(&crp->cr_strhash);
6901 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
6902 crp->cr_name.data = name.data;
6903 crp->cr_name.len = name.len;
6905 nn->reclaim_str_hashtbl_size++;
6911 nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
6913 list_del(&crp->cr_strhash);
6914 kfree(crp->cr_name.data);
6916 nn->reclaim_str_hashtbl_size--;
6920 nfs4_release_reclaim(struct nfsd_net *nn)
6922 struct nfs4_client_reclaim *crp = NULL;
6925 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6926 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
6927 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
6928 struct nfs4_client_reclaim, cr_strhash);
6929 nfs4_remove_reclaim_record(crp, nn);
6932 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
6936 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
6937 struct nfs4_client_reclaim *
6938 nfsd4_find_reclaim_client(struct xdr_netobj name, struct nfsd_net *nn)
6940 unsigned int strhashval;
6941 struct nfs4_client_reclaim *crp = NULL;
6943 dprintk("NFSD: nfs4_find_reclaim_client for name %.*s\n", name.len, name.data);
6945 strhashval = clientstr_hashval(name);
6946 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
6947 if (compare_blob(&crp->cr_name, &name) == 0) {
6955 * Called from OPEN. Look for clientid in reclaim list.
6958 nfs4_check_open_reclaim(clientid_t *clid,
6959 struct nfsd4_compound_state *cstate,
6960 struct nfsd_net *nn)
6964 /* find clientid in conf_id_hashtbl */
6965 status = lookup_clientid(clid, cstate, nn);
6967 return nfserr_reclaim_bad;
6969 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
6970 return nfserr_no_grace;
6972 if (nfsd4_client_record_check(cstate->clp))
6973 return nfserr_reclaim_bad;
6978 #ifdef CONFIG_NFSD_FAULT_INJECTION
6980 put_client(struct nfs4_client *clp)
6982 atomic_dec(&clp->cl_rpc_users);
6985 static struct nfs4_client *
6986 nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
6988 struct nfs4_client *clp;
6989 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6992 if (!nfsd_netns_ready(nn))
6995 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6996 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
7003 nfsd_inject_print_clients(void)
7005 struct nfs4_client *clp;
7007 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7009 char buf[INET6_ADDRSTRLEN];
7011 if (!nfsd_netns_ready(nn))
7014 spin_lock(&nn->client_lock);
7015 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
7016 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
7017 pr_info("NFS Client: %s\n", buf);
7020 spin_unlock(&nn->client_lock);
7026 nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
7029 struct nfs4_client *clp;
7030 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7033 if (!nfsd_netns_ready(nn))
7036 spin_lock(&nn->client_lock);
7037 clp = nfsd_find_client(addr, addr_size);
7039 if (mark_client_expired_locked(clp) == nfs_ok)
7044 spin_unlock(&nn->client_lock);
7053 nfsd_inject_forget_clients(u64 max)
7056 struct nfs4_client *clp, *next;
7057 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7059 LIST_HEAD(reaplist);
7061 if (!nfsd_netns_ready(nn))
7064 spin_lock(&nn->client_lock);
7065 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
7066 if (mark_client_expired_locked(clp) == nfs_ok) {
7067 list_add(&clp->cl_lru, &reaplist);
7068 if (max != 0 && ++count >= max)
7072 spin_unlock(&nn->client_lock);
7074 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
7080 static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
7083 char buf[INET6_ADDRSTRLEN];
7084 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
7085 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
7089 nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
7090 struct list_head *collect)
7092 struct nfs4_client *clp = lst->st_stid.sc_client;
7093 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7099 lockdep_assert_held(&nn->client_lock);
7100 atomic_inc(&clp->cl_rpc_users);
7101 list_add(&lst->st_locks, collect);
7104 static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
7105 struct list_head *collect,
7106 bool (*func)(struct nfs4_ol_stateid *))
7108 struct nfs4_openowner *oop;
7109 struct nfs4_ol_stateid *stp, *st_next;
7110 struct nfs4_ol_stateid *lst, *lst_next;
7113 spin_lock(&clp->cl_lock);
7114 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
7115 list_for_each_entry_safe(stp, st_next,
7116 &oop->oo_owner.so_stateids, st_perstateowner) {
7117 list_for_each_entry_safe(lst, lst_next,
7118 &stp->st_locks, st_locks) {
7121 nfsd_inject_add_lock_to_list(lst,
7126 * Despite the fact that these functions deal
7127 * with 64-bit integers for "count", we must
7128 * ensure that it doesn't blow up the
7129 * clp->cl_rpc_users. Throw a warning if we
7130 * start to approach INT_MAX here.
7132 WARN_ON_ONCE(count == (INT_MAX / 2));
7139 spin_unlock(&clp->cl_lock);
7145 nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
7148 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
7152 nfsd_print_client_locks(struct nfs4_client *clp)
7154 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
7155 nfsd_print_count(clp, count, "locked files");
7160 nfsd_inject_print_locks(void)
7162 struct nfs4_client *clp;
7164 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7167 if (!nfsd_netns_ready(nn))
7170 spin_lock(&nn->client_lock);
7171 list_for_each_entry(clp, &nn->client_lru, cl_lru)
7172 count += nfsd_print_client_locks(clp);
7173 spin_unlock(&nn->client_lock);
7179 nfsd_reap_locks(struct list_head *reaplist)
7181 struct nfs4_client *clp;
7182 struct nfs4_ol_stateid *stp, *next;
7184 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
7185 list_del_init(&stp->st_locks);
7186 clp = stp->st_stid.sc_client;
7187 nfs4_put_stid(&stp->st_stid);
7193 nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
7195 unsigned int count = 0;
7196 struct nfs4_client *clp;
7197 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7199 LIST_HEAD(reaplist);
7201 if (!nfsd_netns_ready(nn))
7204 spin_lock(&nn->client_lock);
7205 clp = nfsd_find_client(addr, addr_size);
7207 count = nfsd_collect_client_locks(clp, &reaplist, 0);
7208 spin_unlock(&nn->client_lock);
7209 nfsd_reap_locks(&reaplist);
7214 nfsd_inject_forget_locks(u64 max)
7217 struct nfs4_client *clp;
7218 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7220 LIST_HEAD(reaplist);
7222 if (!nfsd_netns_ready(nn))
7225 spin_lock(&nn->client_lock);
7226 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
7227 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
7228 if (max != 0 && count >= max)
7231 spin_unlock(&nn->client_lock);
7232 nfsd_reap_locks(&reaplist);
7237 nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
7238 struct list_head *collect,
7239 void (*func)(struct nfs4_openowner *))
7241 struct nfs4_openowner *oop, *next;
7242 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7246 lockdep_assert_held(&nn->client_lock);
7248 spin_lock(&clp->cl_lock);
7249 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
7253 atomic_inc(&clp->cl_rpc_users);
7254 list_add(&oop->oo_perclient, collect);
7259 * Despite the fact that these functions deal with
7260 * 64-bit integers for "count", we must ensure that
7261 * it doesn't blow up the clp->cl_rpc_users. Throw a
7262 * warning if we start to approach INT_MAX here.
7264 WARN_ON_ONCE(count == (INT_MAX / 2));
7268 spin_unlock(&clp->cl_lock);
7274 nfsd_print_client_openowners(struct nfs4_client *clp)
7276 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
7278 nfsd_print_count(clp, count, "openowners");
7283 nfsd_collect_client_openowners(struct nfs4_client *clp,
7284 struct list_head *collect, u64 max)
7286 return nfsd_foreach_client_openowner(clp, max, collect,
7287 unhash_openowner_locked);
7291 nfsd_inject_print_openowners(void)
7293 struct nfs4_client *clp;
7295 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7298 if (!nfsd_netns_ready(nn))
7301 spin_lock(&nn->client_lock);
7302 list_for_each_entry(clp, &nn->client_lru, cl_lru)
7303 count += nfsd_print_client_openowners(clp);
7304 spin_unlock(&nn->client_lock);
7310 nfsd_reap_openowners(struct list_head *reaplist)
7312 struct nfs4_client *clp;
7313 struct nfs4_openowner *oop, *next;
7315 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
7316 list_del_init(&oop->oo_perclient);
7317 clp = oop->oo_owner.so_client;
7318 release_openowner(oop);
7324 nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
7327 unsigned int count = 0;
7328 struct nfs4_client *clp;
7329 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7331 LIST_HEAD(reaplist);
7333 if (!nfsd_netns_ready(nn))
7336 spin_lock(&nn->client_lock);
7337 clp = nfsd_find_client(addr, addr_size);
7339 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
7340 spin_unlock(&nn->client_lock);
7341 nfsd_reap_openowners(&reaplist);
7346 nfsd_inject_forget_openowners(u64 max)
7349 struct nfs4_client *clp;
7350 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7352 LIST_HEAD(reaplist);
7354 if (!nfsd_netns_ready(nn))
7357 spin_lock(&nn->client_lock);
7358 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
7359 count += nfsd_collect_client_openowners(clp, &reaplist,
7361 if (max != 0 && count >= max)
7364 spin_unlock(&nn->client_lock);
7365 nfsd_reap_openowners(&reaplist);
7369 static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
7370 struct list_head *victims)
7372 struct nfs4_delegation *dp, *next;
7373 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7377 lockdep_assert_held(&nn->client_lock);
7379 spin_lock(&state_lock);
7380 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
7383 * It's not safe to mess with delegations that have a
7384 * non-zero dl_time. They might have already been broken
7385 * and could be processed by the laundromat outside of
7386 * the state_lock. Just leave them be.
7388 if (dp->dl_time != 0)
7391 atomic_inc(&clp->cl_rpc_users);
7392 WARN_ON(!unhash_delegation_locked(dp));
7393 list_add(&dp->dl_recall_lru, victims);
7397 * Despite the fact that these functions deal with
7398 * 64-bit integers for "count", we must ensure that
7399 * it doesn't blow up the clp->cl_rpc_users. Throw a
7400 * warning if we start to approach INT_MAX here.
7402 WARN_ON_ONCE(count == (INT_MAX / 2));
7406 spin_unlock(&state_lock);
7411 nfsd_print_client_delegations(struct nfs4_client *clp)
7413 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
7415 nfsd_print_count(clp, count, "delegations");
7420 nfsd_inject_print_delegations(void)
7422 struct nfs4_client *clp;
7424 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7427 if (!nfsd_netns_ready(nn))
7430 spin_lock(&nn->client_lock);
7431 list_for_each_entry(clp, &nn->client_lru, cl_lru)
7432 count += nfsd_print_client_delegations(clp);
7433 spin_unlock(&nn->client_lock);
7439 nfsd_forget_delegations(struct list_head *reaplist)
7441 struct nfs4_client *clp;
7442 struct nfs4_delegation *dp, *next;
7444 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
7445 list_del_init(&dp->dl_recall_lru);
7446 clp = dp->dl_stid.sc_client;
7447 revoke_delegation(dp);
7453 nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
7457 struct nfs4_client *clp;
7458 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7460 LIST_HEAD(reaplist);
7462 if (!nfsd_netns_ready(nn))
7465 spin_lock(&nn->client_lock);
7466 clp = nfsd_find_client(addr, addr_size);
7468 count = nfsd_find_all_delegations(clp, 0, &reaplist);
7469 spin_unlock(&nn->client_lock);
7471 nfsd_forget_delegations(&reaplist);
7476 nfsd_inject_forget_delegations(u64 max)
7479 struct nfs4_client *clp;
7480 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7482 LIST_HEAD(reaplist);
7484 if (!nfsd_netns_ready(nn))
7487 spin_lock(&nn->client_lock);
7488 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
7489 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
7490 if (max != 0 && count >= max)
7493 spin_unlock(&nn->client_lock);
7494 nfsd_forget_delegations(&reaplist);
7499 nfsd_recall_delegations(struct list_head *reaplist)
7501 struct nfs4_client *clp;
7502 struct nfs4_delegation *dp, *next;
7504 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
7505 list_del_init(&dp->dl_recall_lru);
7506 clp = dp->dl_stid.sc_client;
7508 * We skipped all entries that had a zero dl_time before,
7509 * so we can now reset the dl_time back to 0. If a delegation
7510 * break comes in now, then it won't make any difference since
7511 * we're recalling it either way.
7513 spin_lock(&state_lock);
7515 spin_unlock(&state_lock);
7516 nfsd_break_one_deleg(dp);
7522 nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
7526 struct nfs4_client *clp;
7527 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7529 LIST_HEAD(reaplist);
7531 if (!nfsd_netns_ready(nn))
7534 spin_lock(&nn->client_lock);
7535 clp = nfsd_find_client(addr, addr_size);
7537 count = nfsd_find_all_delegations(clp, 0, &reaplist);
7538 spin_unlock(&nn->client_lock);
7540 nfsd_recall_delegations(&reaplist);
7545 nfsd_inject_recall_delegations(u64 max)
7548 struct nfs4_client *clp, *next;
7549 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7551 LIST_HEAD(reaplist);
7553 if (!nfsd_netns_ready(nn))
7556 spin_lock(&nn->client_lock);
7557 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
7558 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
7559 if (max != 0 && ++count >= max)
7562 spin_unlock(&nn->client_lock);
7563 nfsd_recall_delegations(&reaplist);
7566 #endif /* CONFIG_NFSD_FAULT_INJECTION */
7569 * Since the lifetime of a delegation isn't limited to that of an open, a
7570 * client may quite reasonably hang on to a delegation as long as it has
7571 * the inode cached. This becomes an obvious problem the first time a
7572 * client's inode cache approaches the size of the server's total memory.
7574 * For now we avoid this problem by imposing a hard limit on the number
7575 * of delegations, which varies according to the server's memory size.
7578 set_max_delegations(void)
7581 * Allow at most 4 delegations per megabyte of RAM. Quick
7582 * estimates suggest that in the worst case (where every delegation
7583 * is for a different inode), a delegation could take about 1.5K,
7584 * giving a worst case usage of about 6% of memory.
7586 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
7589 static int nfs4_state_create_net(struct net *net)
7591 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7594 nn->conf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7595 sizeof(struct list_head),
7597 if (!nn->conf_id_hashtbl)
7599 nn->unconf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7600 sizeof(struct list_head),
7602 if (!nn->unconf_id_hashtbl)
7604 nn->sessionid_hashtbl = kmalloc_array(SESSION_HASH_SIZE,
7605 sizeof(struct list_head),
7607 if (!nn->sessionid_hashtbl)
7610 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7611 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
7612 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
7614 for (i = 0; i < SESSION_HASH_SIZE; i++)
7615 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
7616 nn->conf_name_tree = RB_ROOT;
7617 nn->unconf_name_tree = RB_ROOT;
7618 nn->boot_time = get_seconds();
7619 nn->grace_ended = false;
7620 nn->nfsd4_manager.block_opens = true;
7621 INIT_LIST_HEAD(&nn->nfsd4_manager.list);
7622 INIT_LIST_HEAD(&nn->client_lru);
7623 INIT_LIST_HEAD(&nn->close_lru);
7624 INIT_LIST_HEAD(&nn->del_recall_lru);
7625 spin_lock_init(&nn->client_lock);
7626 spin_lock_init(&nn->s2s_cp_lock);
7627 idr_init(&nn->s2s_cp_stateids);
7629 spin_lock_init(&nn->blocked_locks_lock);
7630 INIT_LIST_HEAD(&nn->blocked_locks_lru);
7632 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
7638 kfree(nn->unconf_id_hashtbl);
7640 kfree(nn->conf_id_hashtbl);
7646 nfs4_state_destroy_net(struct net *net)
7649 struct nfs4_client *clp = NULL;
7650 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7652 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7653 while (!list_empty(&nn->conf_id_hashtbl[i])) {
7654 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7655 destroy_client(clp);
7659 WARN_ON(!list_empty(&nn->blocked_locks_lru));
7661 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7662 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
7663 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7664 destroy_client(clp);
7668 kfree(nn->sessionid_hashtbl);
7669 kfree(nn->unconf_id_hashtbl);
7670 kfree(nn->conf_id_hashtbl);
7675 nfs4_state_start_net(struct net *net)
7677 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7680 ret = nfs4_state_create_net(net);
7683 locks_start_grace(net, &nn->nfsd4_manager);
7684 nfsd4_client_tracking_init(net);
7685 if (nn->track_reclaim_completes && nn->reclaim_str_hashtbl_size == 0)
7687 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %x)\n",
7688 nn->nfsd4_grace, net->ns.inum);
7689 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
7693 printk(KERN_INFO "NFSD: no clients to reclaim, skipping NFSv4 grace period (net %x)\n",
7695 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_lease * HZ);
7696 nfsd4_end_grace(nn);
7700 /* initialization to perform when the nfsd service is started: */
7703 nfs4_state_start(void)
7707 laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
7708 if (laundry_wq == NULL) {
7712 ret = nfsd4_create_callback_queue();
7714 goto out_free_laundry;
7716 set_max_delegations();
7720 destroy_workqueue(laundry_wq);
7726 nfs4_state_shutdown_net(struct net *net)
7728 struct nfs4_delegation *dp = NULL;
7729 struct list_head *pos, *next, reaplist;
7730 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7732 cancel_delayed_work_sync(&nn->laundromat_work);
7733 locks_end_grace(&nn->nfsd4_manager);
7735 INIT_LIST_HEAD(&reaplist);
7736 spin_lock(&state_lock);
7737 list_for_each_safe(pos, next, &nn->del_recall_lru) {
7738 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
7739 WARN_ON(!unhash_delegation_locked(dp));
7740 list_add(&dp->dl_recall_lru, &reaplist);
7742 spin_unlock(&state_lock);
7743 list_for_each_safe(pos, next, &reaplist) {
7744 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
7745 list_del_init(&dp->dl_recall_lru);
7746 destroy_unhashed_deleg(dp);
7749 nfsd4_client_tracking_exit(net);
7750 nfs4_state_destroy_net(net);
7754 nfs4_state_shutdown(void)
7756 destroy_workqueue(laundry_wq);
7757 nfsd4_destroy_callback_queue();
7761 get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7763 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
7764 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
7768 put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7770 if (cstate->minorversion) {
7771 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
7772 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
7777 clear_current_stateid(struct nfsd4_compound_state *cstate)
7779 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
7783 * functions to set current state id
7786 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate,
7787 union nfsd4_op_u *u)
7789 put_stateid(cstate, &u->open_downgrade.od_stateid);
7793 nfsd4_set_openstateid(struct nfsd4_compound_state *cstate,
7794 union nfsd4_op_u *u)
7796 put_stateid(cstate, &u->open.op_stateid);
7800 nfsd4_set_closestateid(struct nfsd4_compound_state *cstate,
7801 union nfsd4_op_u *u)
7803 put_stateid(cstate, &u->close.cl_stateid);
7807 nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate,
7808 union nfsd4_op_u *u)
7810 put_stateid(cstate, &u->lock.lk_resp_stateid);
7814 * functions to consume current state id
7818 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate,
7819 union nfsd4_op_u *u)
7821 get_stateid(cstate, &u->open_downgrade.od_stateid);
7825 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate,
7826 union nfsd4_op_u *u)
7828 get_stateid(cstate, &u->delegreturn.dr_stateid);
7832 nfsd4_get_freestateid(struct nfsd4_compound_state *cstate,
7833 union nfsd4_op_u *u)
7835 get_stateid(cstate, &u->free_stateid.fr_stateid);
7839 nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate,
7840 union nfsd4_op_u *u)
7842 get_stateid(cstate, &u->setattr.sa_stateid);
7846 nfsd4_get_closestateid(struct nfsd4_compound_state *cstate,
7847 union nfsd4_op_u *u)
7849 get_stateid(cstate, &u->close.cl_stateid);
7853 nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate,
7854 union nfsd4_op_u *u)
7856 get_stateid(cstate, &u->locku.lu_stateid);
7860 nfsd4_get_readstateid(struct nfsd4_compound_state *cstate,
7861 union nfsd4_op_u *u)
7863 get_stateid(cstate, &u->read.rd_stateid);
7867 nfsd4_get_writestateid(struct nfsd4_compound_state *cstate,
7868 union nfsd4_op_u *u)
7870 get_stateid(cstate, &u->write.wr_stateid);