2 * Copyright (c) 2001 The Regents of the University of Michigan.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <linux/file.h>
37 #include <linux/slab.h>
38 #include <linux/namei.h>
39 #include <linux/swap.h>
40 #include <linux/pagemap.h>
41 #include <linux/ratelimit.h>
42 #include <linux/sunrpc/svcauth_gss.h>
43 #include <linux/sunrpc/addr.h>
44 #include <linux/jhash.h>
48 #include "current_stateid.h"
53 #define NFSDDBG_FACILITY NFSDDBG_PROC
55 #define all_ones {{~0,~0},~0}
56 static const stateid_t one_stateid = {
58 .si_opaque = all_ones,
60 static const stateid_t zero_stateid = {
63 static const stateid_t currentstateid = {
66 static const stateid_t close_stateid = {
67 .si_generation = 0xffffffffU,
70 static u64 current_sessionid = 1;
72 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
73 #define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
74 #define CURRENT_STATEID(stateid) (!memcmp((stateid), ¤tstateid, sizeof(stateid_t)))
75 #define CLOSE_STATEID(stateid) (!memcmp((stateid), &close_stateid, sizeof(stateid_t)))
77 /* forward declarations */
78 static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
79 static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
84 * Currently used for the del_recall_lru and file hash table. In an
85 * effort to decrease the scope of the client_mutex, this spinlock may
86 * eventually cover more:
88 static DEFINE_SPINLOCK(state_lock);
90 enum nfsd4_st_mutex_lock_subclass {
91 OPEN_STATEID_MUTEX = 0,
92 LOCK_STATEID_MUTEX = 1,
96 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
97 * the refcount on the open stateid to drop.
99 static DECLARE_WAIT_QUEUE_HEAD(close_wq);
101 static struct kmem_cache *client_slab;
102 static struct kmem_cache *openowner_slab;
103 static struct kmem_cache *lockowner_slab;
104 static struct kmem_cache *file_slab;
105 static struct kmem_cache *stateid_slab;
106 static struct kmem_cache *deleg_slab;
107 static struct kmem_cache *odstate_slab;
109 static void free_session(struct nfsd4_session *);
111 static const struct nfsd4_callback_ops nfsd4_cb_recall_ops;
112 static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops;
114 static bool is_session_dead(struct nfsd4_session *ses)
116 return ses->se_flags & NFS4_SESSION_DEAD;
119 static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
121 if (atomic_read(&ses->se_ref) > ref_held_by_me)
122 return nfserr_jukebox;
123 ses->se_flags |= NFS4_SESSION_DEAD;
127 static bool is_client_expired(struct nfs4_client *clp)
129 return clp->cl_time == 0;
132 static __be32 get_client_locked(struct nfs4_client *clp)
134 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
136 lockdep_assert_held(&nn->client_lock);
138 if (is_client_expired(clp))
139 return nfserr_expired;
140 atomic_inc(&clp->cl_refcount);
144 /* must be called under the client_lock */
146 renew_client_locked(struct nfs4_client *clp)
148 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
150 if (is_client_expired(clp)) {
152 printk("%s: client (clientid %08x/%08x) already expired\n",
154 clp->cl_clientid.cl_boot,
155 clp->cl_clientid.cl_id);
159 dprintk("renewing client (clientid %08x/%08x)\n",
160 clp->cl_clientid.cl_boot,
161 clp->cl_clientid.cl_id);
162 list_move_tail(&clp->cl_lru, &nn->client_lru);
163 clp->cl_time = get_seconds();
166 static void put_client_renew_locked(struct nfs4_client *clp)
168 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
170 lockdep_assert_held(&nn->client_lock);
172 if (!atomic_dec_and_test(&clp->cl_refcount))
174 if (!is_client_expired(clp))
175 renew_client_locked(clp);
178 static void put_client_renew(struct nfs4_client *clp)
180 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
182 if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
184 if (!is_client_expired(clp))
185 renew_client_locked(clp);
186 spin_unlock(&nn->client_lock);
189 static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
193 if (is_session_dead(ses))
194 return nfserr_badsession;
195 status = get_client_locked(ses->se_client);
198 atomic_inc(&ses->se_ref);
202 static void nfsd4_put_session_locked(struct nfsd4_session *ses)
204 struct nfs4_client *clp = ses->se_client;
205 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
207 lockdep_assert_held(&nn->client_lock);
209 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
211 put_client_renew_locked(clp);
214 static void nfsd4_put_session(struct nfsd4_session *ses)
216 struct nfs4_client *clp = ses->se_client;
217 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
219 spin_lock(&nn->client_lock);
220 nfsd4_put_session_locked(ses);
221 spin_unlock(&nn->client_lock);
224 static struct nfsd4_blocked_lock *
225 find_blocked_lock(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
228 struct nfsd4_blocked_lock *cur, *found = NULL;
230 spin_lock(&nn->blocked_locks_lock);
231 list_for_each_entry(cur, &lo->lo_blocked, nbl_list) {
232 if (fh_match(fh, &cur->nbl_fh)) {
233 list_del_init(&cur->nbl_list);
234 list_del_init(&cur->nbl_lru);
239 spin_unlock(&nn->blocked_locks_lock);
241 locks_delete_block(&found->nbl_lock);
245 static struct nfsd4_blocked_lock *
246 find_or_allocate_block(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
249 struct nfsd4_blocked_lock *nbl;
251 nbl = find_blocked_lock(lo, fh, nn);
253 nbl= kmalloc(sizeof(*nbl), GFP_KERNEL);
255 fh_copy_shallow(&nbl->nbl_fh, fh);
256 locks_init_lock(&nbl->nbl_lock);
257 nfsd4_init_cb(&nbl->nbl_cb, lo->lo_owner.so_client,
258 &nfsd4_cb_notify_lock_ops,
259 NFSPROC4_CLNT_CB_NOTIFY_LOCK);
266 free_blocked_lock(struct nfsd4_blocked_lock *nbl)
268 locks_release_private(&nbl->nbl_lock);
273 remove_blocked_locks(struct nfs4_lockowner *lo)
275 struct nfs4_client *clp = lo->lo_owner.so_client;
276 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
277 struct nfsd4_blocked_lock *nbl;
280 /* Dequeue all blocked locks */
281 spin_lock(&nn->blocked_locks_lock);
282 while (!list_empty(&lo->lo_blocked)) {
283 nbl = list_first_entry(&lo->lo_blocked,
284 struct nfsd4_blocked_lock,
286 list_del_init(&nbl->nbl_list);
287 list_move(&nbl->nbl_lru, &reaplist);
289 spin_unlock(&nn->blocked_locks_lock);
292 while (!list_empty(&reaplist)) {
293 nbl = list_first_entry(&reaplist, struct nfsd4_blocked_lock,
295 list_del_init(&nbl->nbl_lru);
296 locks_delete_block(&nbl->nbl_lock);
297 free_blocked_lock(nbl);
302 nfsd4_cb_notify_lock_done(struct nfsd4_callback *cb, struct rpc_task *task)
305 * Since this is just an optimization, we don't try very hard if it
306 * turns out not to succeed. We'll requeue it on NFS4ERR_DELAY, and
307 * just quit trying on anything else.
309 switch (task->tk_status) {
311 rpc_delay(task, 1 * HZ);
319 nfsd4_cb_notify_lock_release(struct nfsd4_callback *cb)
321 struct nfsd4_blocked_lock *nbl = container_of(cb,
322 struct nfsd4_blocked_lock, nbl_cb);
324 free_blocked_lock(nbl);
327 static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops = {
328 .done = nfsd4_cb_notify_lock_done,
329 .release = nfsd4_cb_notify_lock_release,
332 static inline struct nfs4_stateowner *
333 nfs4_get_stateowner(struct nfs4_stateowner *sop)
335 atomic_inc(&sop->so_count);
340 same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
342 return (sop->so_owner.len == owner->len) &&
343 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
346 static struct nfs4_openowner *
347 find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
348 struct nfs4_client *clp)
350 struct nfs4_stateowner *so;
352 lockdep_assert_held(&clp->cl_lock);
354 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
356 if (!so->so_is_open_owner)
358 if (same_owner_str(so, &open->op_owner))
359 return openowner(nfs4_get_stateowner(so));
364 static struct nfs4_openowner *
365 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
366 struct nfs4_client *clp)
368 struct nfs4_openowner *oo;
370 spin_lock(&clp->cl_lock);
371 oo = find_openstateowner_str_locked(hashval, open, clp);
372 spin_unlock(&clp->cl_lock);
377 opaque_hashval(const void *ptr, int nbytes)
379 unsigned char *cptr = (unsigned char *) ptr;
389 static void nfsd4_free_file_rcu(struct rcu_head *rcu)
391 struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
393 kmem_cache_free(file_slab, fp);
397 put_nfs4_file(struct nfs4_file *fi)
399 might_lock(&state_lock);
401 if (refcount_dec_and_lock(&fi->fi_ref, &state_lock)) {
402 hlist_del_rcu(&fi->fi_hash);
403 spin_unlock(&state_lock);
404 WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
405 WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
406 call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
411 __nfs4_get_fd(struct nfs4_file *f, int oflag)
413 if (f->fi_fds[oflag])
414 return get_file(f->fi_fds[oflag]);
419 find_writeable_file_locked(struct nfs4_file *f)
423 lockdep_assert_held(&f->fi_lock);
425 ret = __nfs4_get_fd(f, O_WRONLY);
427 ret = __nfs4_get_fd(f, O_RDWR);
432 find_writeable_file(struct nfs4_file *f)
436 spin_lock(&f->fi_lock);
437 ret = find_writeable_file_locked(f);
438 spin_unlock(&f->fi_lock);
443 static struct file *find_readable_file_locked(struct nfs4_file *f)
447 lockdep_assert_held(&f->fi_lock);
449 ret = __nfs4_get_fd(f, O_RDONLY);
451 ret = __nfs4_get_fd(f, O_RDWR);
456 find_readable_file(struct nfs4_file *f)
460 spin_lock(&f->fi_lock);
461 ret = find_readable_file_locked(f);
462 spin_unlock(&f->fi_lock);
468 find_any_file(struct nfs4_file *f)
472 spin_lock(&f->fi_lock);
473 ret = __nfs4_get_fd(f, O_RDWR);
475 ret = __nfs4_get_fd(f, O_WRONLY);
477 ret = __nfs4_get_fd(f, O_RDONLY);
479 spin_unlock(&f->fi_lock);
483 static atomic_long_t num_delegations;
484 unsigned long max_delegations;
487 * Open owner state (share locks)
490 /* hash tables for lock and open owners */
491 #define OWNER_HASH_BITS 8
492 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
493 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
495 static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
499 ret = opaque_hashval(ownername->data, ownername->len);
500 return ret & OWNER_HASH_MASK;
503 /* hash table for nfs4_file */
504 #define FILE_HASH_BITS 8
505 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
507 static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
509 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
512 static unsigned int file_hashval(struct knfsd_fh *fh)
514 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
517 static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
520 __nfs4_file_get_access(struct nfs4_file *fp, u32 access)
522 lockdep_assert_held(&fp->fi_lock);
524 if (access & NFS4_SHARE_ACCESS_WRITE)
525 atomic_inc(&fp->fi_access[O_WRONLY]);
526 if (access & NFS4_SHARE_ACCESS_READ)
527 atomic_inc(&fp->fi_access[O_RDONLY]);
531 nfs4_file_get_access(struct nfs4_file *fp, u32 access)
533 lockdep_assert_held(&fp->fi_lock);
535 /* Does this access mode make sense? */
536 if (access & ~NFS4_SHARE_ACCESS_BOTH)
539 /* Does it conflict with a deny mode already set? */
540 if ((access & fp->fi_share_deny) != 0)
541 return nfserr_share_denied;
543 __nfs4_file_get_access(fp, access);
547 static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
549 /* Common case is that there is no deny mode. */
551 /* Does this deny mode make sense? */
552 if (deny & ~NFS4_SHARE_DENY_BOTH)
555 if ((deny & NFS4_SHARE_DENY_READ) &&
556 atomic_read(&fp->fi_access[O_RDONLY]))
557 return nfserr_share_denied;
559 if ((deny & NFS4_SHARE_DENY_WRITE) &&
560 atomic_read(&fp->fi_access[O_WRONLY]))
561 return nfserr_share_denied;
566 static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
568 might_lock(&fp->fi_lock);
570 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
571 struct file *f1 = NULL;
572 struct file *f2 = NULL;
574 swap(f1, fp->fi_fds[oflag]);
575 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
576 swap(f2, fp->fi_fds[O_RDWR]);
577 spin_unlock(&fp->fi_lock);
585 static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
587 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
589 if (access & NFS4_SHARE_ACCESS_WRITE)
590 __nfs4_file_put_access(fp, O_WRONLY);
591 if (access & NFS4_SHARE_ACCESS_READ)
592 __nfs4_file_put_access(fp, O_RDONLY);
596 * Allocate a new open/delegation state counter. This is needed for
597 * pNFS for proper return on close semantics.
599 * Note that we only allocate it for pNFS-enabled exports, otherwise
600 * all pointers to struct nfs4_clnt_odstate are always NULL.
602 static struct nfs4_clnt_odstate *
603 alloc_clnt_odstate(struct nfs4_client *clp)
605 struct nfs4_clnt_odstate *co;
607 co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
610 refcount_set(&co->co_odcount, 1);
616 hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
618 struct nfs4_file *fp = co->co_file;
620 lockdep_assert_held(&fp->fi_lock);
621 list_add(&co->co_perfile, &fp->fi_clnt_odstate);
625 get_clnt_odstate(struct nfs4_clnt_odstate *co)
628 refcount_inc(&co->co_odcount);
632 put_clnt_odstate(struct nfs4_clnt_odstate *co)
634 struct nfs4_file *fp;
640 if (refcount_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
641 list_del(&co->co_perfile);
642 spin_unlock(&fp->fi_lock);
644 nfsd4_return_all_file_layouts(co->co_client, fp);
645 kmem_cache_free(odstate_slab, co);
649 static struct nfs4_clnt_odstate *
650 find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
652 struct nfs4_clnt_odstate *co;
653 struct nfs4_client *cl;
660 spin_lock(&fp->fi_lock);
661 list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
662 if (co->co_client == cl) {
663 get_clnt_odstate(co);
669 hash_clnt_odstate_locked(new);
671 spin_unlock(&fp->fi_lock);
675 struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab,
676 void (*sc_free)(struct nfs4_stid *))
678 struct nfs4_stid *stid;
681 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
685 idr_preload(GFP_KERNEL);
686 spin_lock(&cl->cl_lock);
687 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 0, 0, GFP_NOWAIT);
688 spin_unlock(&cl->cl_lock);
693 stid->sc_free = sc_free;
694 stid->sc_client = cl;
695 stid->sc_stateid.si_opaque.so_id = new_id;
696 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
697 /* Will be incremented before return to client: */
698 refcount_set(&stid->sc_count, 1);
699 spin_lock_init(&stid->sc_lock);
702 * It shouldn't be a problem to reuse an opaque stateid value.
703 * I don't think it is for 4.1. But with 4.0 I worry that, for
704 * example, a stray write retransmission could be accepted by
705 * the server when it should have been rejected. Therefore,
706 * adopt a trick from the sctp code to attempt to maximize the
707 * amount of time until an id is reused, by ensuring they always
708 * "increase" (mod INT_MAX):
712 kmem_cache_free(slab, stid);
717 * Create a unique stateid_t to represent each COPY.
719 int nfs4_init_cp_state(struct nfsd_net *nn, struct nfsd4_copy *copy)
723 idr_preload(GFP_KERNEL);
724 spin_lock(&nn->s2s_cp_lock);
725 new_id = idr_alloc_cyclic(&nn->s2s_cp_stateids, copy, 0, 0, GFP_NOWAIT);
726 spin_unlock(&nn->s2s_cp_lock);
730 copy->cp_stateid.si_opaque.so_id = new_id;
731 copy->cp_stateid.si_opaque.so_clid.cl_boot = nn->boot_time;
732 copy->cp_stateid.si_opaque.so_clid.cl_id = nn->s2s_cp_cl_id;
736 void nfs4_free_cp_state(struct nfsd4_copy *copy)
740 nn = net_generic(copy->cp_clp->net, nfsd_net_id);
741 spin_lock(&nn->s2s_cp_lock);
742 idr_remove(&nn->s2s_cp_stateids, copy->cp_stateid.si_opaque.so_id);
743 spin_unlock(&nn->s2s_cp_lock);
746 static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
748 struct nfs4_stid *stid;
750 stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid);
754 return openlockstateid(stid);
757 static void nfs4_free_deleg(struct nfs4_stid *stid)
759 kmem_cache_free(deleg_slab, stid);
760 atomic_long_dec(&num_delegations);
764 * When we recall a delegation, we should be careful not to hand it
765 * out again straight away.
766 * To ensure this we keep a pair of bloom filters ('new' and 'old')
767 * in which the filehandles of recalled delegations are "stored".
768 * If a filehandle appear in either filter, a delegation is blocked.
769 * When a delegation is recalled, the filehandle is stored in the "new"
771 * Every 30 seconds we swap the filters and clear the "new" one,
772 * unless both are empty of course.
774 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
775 * low 3 bytes as hash-table indices.
777 * 'blocked_delegations_lock', which is always taken in block_delegations(),
778 * is used to manage concurrent access. Testing does not need the lock
779 * except when swapping the two filters.
781 static DEFINE_SPINLOCK(blocked_delegations_lock);
782 static struct bloom_pair {
783 int entries, old_entries;
785 int new; /* index into 'set' */
786 DECLARE_BITMAP(set[2], 256);
787 } blocked_delegations;
789 static int delegation_blocked(struct knfsd_fh *fh)
792 struct bloom_pair *bd = &blocked_delegations;
794 if (bd->entries == 0)
796 if (seconds_since_boot() - bd->swap_time > 30) {
797 spin_lock(&blocked_delegations_lock);
798 if (seconds_since_boot() - bd->swap_time > 30) {
799 bd->entries -= bd->old_entries;
800 bd->old_entries = bd->entries;
801 memset(bd->set[bd->new], 0,
804 bd->swap_time = seconds_since_boot();
806 spin_unlock(&blocked_delegations_lock);
808 hash = jhash(&fh->fh_base, fh->fh_size, 0);
809 if (test_bit(hash&255, bd->set[0]) &&
810 test_bit((hash>>8)&255, bd->set[0]) &&
811 test_bit((hash>>16)&255, bd->set[0]))
814 if (test_bit(hash&255, bd->set[1]) &&
815 test_bit((hash>>8)&255, bd->set[1]) &&
816 test_bit((hash>>16)&255, bd->set[1]))
822 static void block_delegations(struct knfsd_fh *fh)
825 struct bloom_pair *bd = &blocked_delegations;
827 hash = jhash(&fh->fh_base, fh->fh_size, 0);
829 spin_lock(&blocked_delegations_lock);
830 __set_bit(hash&255, bd->set[bd->new]);
831 __set_bit((hash>>8)&255, bd->set[bd->new]);
832 __set_bit((hash>>16)&255, bd->set[bd->new]);
833 if (bd->entries == 0)
834 bd->swap_time = seconds_since_boot();
836 spin_unlock(&blocked_delegations_lock);
839 static struct nfs4_delegation *
840 alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
841 struct svc_fh *current_fh,
842 struct nfs4_clnt_odstate *odstate)
844 struct nfs4_delegation *dp;
847 dprintk("NFSD alloc_init_deleg\n");
848 n = atomic_long_inc_return(&num_delegations);
849 if (n < 0 || n > max_delegations)
851 if (delegation_blocked(¤t_fh->fh_handle))
853 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg));
858 * delegation seqid's are never incremented. The 4.1 special
859 * meaning of seqid 0 isn't meaningful, really, but let's avoid
860 * 0 anyway just for consistency and use 1:
862 dp->dl_stid.sc_stateid.si_generation = 1;
863 INIT_LIST_HEAD(&dp->dl_perfile);
864 INIT_LIST_HEAD(&dp->dl_perclnt);
865 INIT_LIST_HEAD(&dp->dl_recall_lru);
866 dp->dl_clnt_odstate = odstate;
867 get_clnt_odstate(odstate);
868 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
870 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
871 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
873 dp->dl_stid.sc_file = fp;
876 atomic_long_dec(&num_delegations);
881 nfs4_put_stid(struct nfs4_stid *s)
883 struct nfs4_file *fp = s->sc_file;
884 struct nfs4_client *clp = s->sc_client;
886 might_lock(&clp->cl_lock);
888 if (!refcount_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
889 wake_up_all(&close_wq);
892 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
893 spin_unlock(&clp->cl_lock);
900 nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
902 stateid_t *src = &stid->sc_stateid;
904 spin_lock(&stid->sc_lock);
905 if (unlikely(++src->si_generation == 0))
906 src->si_generation = 1;
907 memcpy(dst, src, sizeof(*dst));
908 spin_unlock(&stid->sc_lock);
911 static void put_deleg_file(struct nfs4_file *fp)
913 struct file *filp = NULL;
915 spin_lock(&fp->fi_lock);
916 if (--fp->fi_delegees == 0)
917 swap(filp, fp->fi_deleg_file);
918 spin_unlock(&fp->fi_lock);
924 static void nfs4_unlock_deleg_lease(struct nfs4_delegation *dp)
926 struct nfs4_file *fp = dp->dl_stid.sc_file;
927 struct file *filp = fp->fi_deleg_file;
929 WARN_ON_ONCE(!fp->fi_delegees);
931 vfs_setlease(filp, F_UNLCK, NULL, (void **)&dp);
935 static void destroy_unhashed_deleg(struct nfs4_delegation *dp)
937 put_clnt_odstate(dp->dl_clnt_odstate);
938 nfs4_unlock_deleg_lease(dp);
939 nfs4_put_stid(&dp->dl_stid);
942 void nfs4_unhash_stid(struct nfs4_stid *s)
948 * nfs4_delegation_exists - Discover if this delegation already exists
949 * @clp: a pointer to the nfs4_client we're granting a delegation to
950 * @fp: a pointer to the nfs4_file we're granting a delegation on
953 * On success: true iff an existing delegation is found
957 nfs4_delegation_exists(struct nfs4_client *clp, struct nfs4_file *fp)
959 struct nfs4_delegation *searchdp = NULL;
960 struct nfs4_client *searchclp = NULL;
962 lockdep_assert_held(&state_lock);
963 lockdep_assert_held(&fp->fi_lock);
965 list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
966 searchclp = searchdp->dl_stid.sc_client;
967 if (clp == searchclp) {
975 * hash_delegation_locked - Add a delegation to the appropriate lists
976 * @dp: a pointer to the nfs4_delegation we are adding.
977 * @fp: a pointer to the nfs4_file we're granting a delegation on
980 * On success: NULL if the delegation was successfully hashed.
982 * On error: -EAGAIN if one was previously granted to this
983 * nfs4_client for this nfs4_file. Delegation is not hashed.
988 hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
990 struct nfs4_client *clp = dp->dl_stid.sc_client;
992 lockdep_assert_held(&state_lock);
993 lockdep_assert_held(&fp->fi_lock);
995 if (nfs4_delegation_exists(clp, fp))
997 refcount_inc(&dp->dl_stid.sc_count);
998 dp->dl_stid.sc_type = NFS4_DELEG_STID;
999 list_add(&dp->dl_perfile, &fp->fi_delegations);
1000 list_add(&dp->dl_perclnt, &clp->cl_delegations);
1005 unhash_delegation_locked(struct nfs4_delegation *dp)
1007 struct nfs4_file *fp = dp->dl_stid.sc_file;
1009 lockdep_assert_held(&state_lock);
1011 if (list_empty(&dp->dl_perfile))
1014 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
1015 /* Ensure that deleg break won't try to requeue it */
1017 spin_lock(&fp->fi_lock);
1018 list_del_init(&dp->dl_perclnt);
1019 list_del_init(&dp->dl_recall_lru);
1020 list_del_init(&dp->dl_perfile);
1021 spin_unlock(&fp->fi_lock);
1025 static void destroy_delegation(struct nfs4_delegation *dp)
1029 spin_lock(&state_lock);
1030 unhashed = unhash_delegation_locked(dp);
1031 spin_unlock(&state_lock);
1033 destroy_unhashed_deleg(dp);
1036 static void revoke_delegation(struct nfs4_delegation *dp)
1038 struct nfs4_client *clp = dp->dl_stid.sc_client;
1040 WARN_ON(!list_empty(&dp->dl_recall_lru));
1042 if (clp->cl_minorversion) {
1043 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
1044 refcount_inc(&dp->dl_stid.sc_count);
1045 spin_lock(&clp->cl_lock);
1046 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
1047 spin_unlock(&clp->cl_lock);
1049 destroy_unhashed_deleg(dp);
1056 static unsigned int clientid_hashval(u32 id)
1058 return id & CLIENT_HASH_MASK;
1061 static unsigned int clientstr_hashval(const char *name)
1063 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
1067 * We store the NONE, READ, WRITE, and BOTH bits separately in the
1068 * st_{access,deny}_bmap field of the stateid, in order to track not
1069 * only what share bits are currently in force, but also what
1070 * combinations of share bits previous opens have used. This allows us
1071 * to enforce the recommendation of rfc 3530 14.2.19 that the server
1072 * return an error if the client attempt to downgrade to a combination
1073 * of share bits not explicable by closing some of its previous opens.
1075 * XXX: This enforcement is actually incomplete, since we don't keep
1076 * track of access/deny bit combinations; so, e.g., we allow:
1078 * OPEN allow read, deny write
1079 * OPEN allow both, deny none
1080 * DOWNGRADE allow read, deny none
1082 * which we should reject.
1085 bmap_to_share_mode(unsigned long bmap) {
1087 unsigned int access = 0;
1089 for (i = 1; i < 4; i++) {
1090 if (test_bit(i, &bmap))
1096 /* set share access for a given stateid */
1098 set_access(u32 access, struct nfs4_ol_stateid *stp)
1100 unsigned char mask = 1 << access;
1102 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1103 stp->st_access_bmap |= mask;
1106 /* clear share access for a given stateid */
1108 clear_access(u32 access, struct nfs4_ol_stateid *stp)
1110 unsigned char mask = 1 << access;
1112 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1113 stp->st_access_bmap &= ~mask;
1116 /* test whether a given stateid has access */
1118 test_access(u32 access, struct nfs4_ol_stateid *stp)
1120 unsigned char mask = 1 << access;
1122 return (bool)(stp->st_access_bmap & mask);
1125 /* set share deny for a given stateid */
1127 set_deny(u32 deny, struct nfs4_ol_stateid *stp)
1129 unsigned char mask = 1 << deny;
1131 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1132 stp->st_deny_bmap |= mask;
1135 /* clear share deny for a given stateid */
1137 clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
1139 unsigned char mask = 1 << deny;
1141 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1142 stp->st_deny_bmap &= ~mask;
1145 /* test whether a given stateid is denying specific access */
1147 test_deny(u32 deny, struct nfs4_ol_stateid *stp)
1149 unsigned char mask = 1 << deny;
1151 return (bool)(stp->st_deny_bmap & mask);
1154 static int nfs4_access_to_omode(u32 access)
1156 switch (access & NFS4_SHARE_ACCESS_BOTH) {
1157 case NFS4_SHARE_ACCESS_READ:
1159 case NFS4_SHARE_ACCESS_WRITE:
1161 case NFS4_SHARE_ACCESS_BOTH:
1169 * A stateid that had a deny mode associated with it is being released
1170 * or downgraded. Recalculate the deny mode on the file.
1173 recalculate_deny_mode(struct nfs4_file *fp)
1175 struct nfs4_ol_stateid *stp;
1177 spin_lock(&fp->fi_lock);
1178 fp->fi_share_deny = 0;
1179 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
1180 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
1181 spin_unlock(&fp->fi_lock);
1185 reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
1188 bool change = false;
1190 for (i = 1; i < 4; i++) {
1191 if ((i & deny) != i) {
1197 /* Recalculate per-file deny mode if there was a change */
1199 recalculate_deny_mode(stp->st_stid.sc_file);
1202 /* release all access and file references for a given stateid */
1204 release_all_access(struct nfs4_ol_stateid *stp)
1207 struct nfs4_file *fp = stp->st_stid.sc_file;
1209 if (fp && stp->st_deny_bmap != 0)
1210 recalculate_deny_mode(fp);
1212 for (i = 1; i < 4; i++) {
1213 if (test_access(i, stp))
1214 nfs4_file_put_access(stp->st_stid.sc_file, i);
1215 clear_access(i, stp);
1219 static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1221 kfree(sop->so_owner.data);
1222 sop->so_ops->so_free(sop);
1225 static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1227 struct nfs4_client *clp = sop->so_client;
1229 might_lock(&clp->cl_lock);
1231 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
1233 sop->so_ops->so_unhash(sop);
1234 spin_unlock(&clp->cl_lock);
1235 nfs4_free_stateowner(sop);
1238 static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
1240 struct nfs4_file *fp = stp->st_stid.sc_file;
1242 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1244 if (list_empty(&stp->st_perfile))
1247 spin_lock(&fp->fi_lock);
1248 list_del_init(&stp->st_perfile);
1249 spin_unlock(&fp->fi_lock);
1250 list_del(&stp->st_perstateowner);
1254 static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
1256 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1258 put_clnt_odstate(stp->st_clnt_odstate);
1259 release_all_access(stp);
1260 if (stp->st_stateowner)
1261 nfs4_put_stateowner(stp->st_stateowner);
1262 kmem_cache_free(stateid_slab, stid);
1265 static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
1267 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1268 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
1271 file = find_any_file(stp->st_stid.sc_file);
1273 filp_close(file, (fl_owner_t)lo);
1274 nfs4_free_ol_stateid(stid);
1278 * Put the persistent reference to an already unhashed generic stateid, while
1279 * holding the cl_lock. If it's the last reference, then put it onto the
1280 * reaplist for later destruction.
1282 static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1283 struct list_head *reaplist)
1285 struct nfs4_stid *s = &stp->st_stid;
1286 struct nfs4_client *clp = s->sc_client;
1288 lockdep_assert_held(&clp->cl_lock);
1290 WARN_ON_ONCE(!list_empty(&stp->st_locks));
1292 if (!refcount_dec_and_test(&s->sc_count)) {
1293 wake_up_all(&close_wq);
1297 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1298 list_add(&stp->st_locks, reaplist);
1301 static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
1303 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1305 list_del_init(&stp->st_locks);
1306 nfs4_unhash_stid(&stp->st_stid);
1307 return unhash_ol_stateid(stp);
1310 static void release_lock_stateid(struct nfs4_ol_stateid *stp)
1312 struct nfs4_client *clp = stp->st_stid.sc_client;
1315 spin_lock(&clp->cl_lock);
1316 unhashed = unhash_lock_stateid(stp);
1317 spin_unlock(&clp->cl_lock);
1319 nfs4_put_stid(&stp->st_stid);
1322 static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
1324 struct nfs4_client *clp = lo->lo_owner.so_client;
1326 lockdep_assert_held(&clp->cl_lock);
1328 list_del_init(&lo->lo_owner.so_strhash);
1332 * Free a list of generic stateids that were collected earlier after being
1336 free_ol_stateid_reaplist(struct list_head *reaplist)
1338 struct nfs4_ol_stateid *stp;
1339 struct nfs4_file *fp;
1343 while (!list_empty(reaplist)) {
1344 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1346 list_del(&stp->st_locks);
1347 fp = stp->st_stid.sc_file;
1348 stp->st_stid.sc_free(&stp->st_stid);
1354 static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1355 struct list_head *reaplist)
1357 struct nfs4_ol_stateid *stp;
1359 lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1361 while (!list_empty(&open_stp->st_locks)) {
1362 stp = list_entry(open_stp->st_locks.next,
1363 struct nfs4_ol_stateid, st_locks);
1364 WARN_ON(!unhash_lock_stateid(stp));
1365 put_ol_stateid_locked(stp, reaplist);
1369 static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
1370 struct list_head *reaplist)
1374 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1376 unhashed = unhash_ol_stateid(stp);
1377 release_open_stateid_locks(stp, reaplist);
1381 static void release_open_stateid(struct nfs4_ol_stateid *stp)
1383 LIST_HEAD(reaplist);
1385 spin_lock(&stp->st_stid.sc_client->cl_lock);
1386 if (unhash_open_stateid(stp, &reaplist))
1387 put_ol_stateid_locked(stp, &reaplist);
1388 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1389 free_ol_stateid_reaplist(&reaplist);
1392 static void unhash_openowner_locked(struct nfs4_openowner *oo)
1394 struct nfs4_client *clp = oo->oo_owner.so_client;
1396 lockdep_assert_held(&clp->cl_lock);
1398 list_del_init(&oo->oo_owner.so_strhash);
1399 list_del_init(&oo->oo_perclient);
1402 static void release_last_closed_stateid(struct nfs4_openowner *oo)
1404 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1406 struct nfs4_ol_stateid *s;
1408 spin_lock(&nn->client_lock);
1409 s = oo->oo_last_closed_stid;
1411 list_del_init(&oo->oo_close_lru);
1412 oo->oo_last_closed_stid = NULL;
1414 spin_unlock(&nn->client_lock);
1416 nfs4_put_stid(&s->st_stid);
1419 static void release_openowner(struct nfs4_openowner *oo)
1421 struct nfs4_ol_stateid *stp;
1422 struct nfs4_client *clp = oo->oo_owner.so_client;
1423 struct list_head reaplist;
1425 INIT_LIST_HEAD(&reaplist);
1427 spin_lock(&clp->cl_lock);
1428 unhash_openowner_locked(oo);
1429 while (!list_empty(&oo->oo_owner.so_stateids)) {
1430 stp = list_first_entry(&oo->oo_owner.so_stateids,
1431 struct nfs4_ol_stateid, st_perstateowner);
1432 if (unhash_open_stateid(stp, &reaplist))
1433 put_ol_stateid_locked(stp, &reaplist);
1435 spin_unlock(&clp->cl_lock);
1436 free_ol_stateid_reaplist(&reaplist);
1437 release_last_closed_stateid(oo);
1438 nfs4_put_stateowner(&oo->oo_owner);
1442 hash_sessionid(struct nfs4_sessionid *sessionid)
1444 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1446 return sid->sequence % SESSION_HASH_SIZE;
1449 #ifdef CONFIG_SUNRPC_DEBUG
1451 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1453 u32 *ptr = (u32 *)(&sessionid->data[0]);
1454 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1458 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1464 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1465 * won't be used for replay.
1467 void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1469 struct nfs4_stateowner *so = cstate->replay_owner;
1471 if (nfserr == nfserr_replay_me)
1474 if (!seqid_mutating_err(ntohl(nfserr))) {
1475 nfsd4_cstate_clear_replay(cstate);
1480 if (so->so_is_open_owner)
1481 release_last_closed_stateid(openowner(so));
1487 gen_sessionid(struct nfsd4_session *ses)
1489 struct nfs4_client *clp = ses->se_client;
1490 struct nfsd4_sessionid *sid;
1492 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1493 sid->clientid = clp->cl_clientid;
1494 sid->sequence = current_sessionid++;
1499 * The protocol defines ca_maxresponssize_cached to include the size of
1500 * the rpc header, but all we need to cache is the data starting after
1501 * the end of the initial SEQUENCE operation--the rest we regenerate
1502 * each time. Therefore we can advertise a ca_maxresponssize_cached
1503 * value that is the number of bytes in our cache plus a few additional
1504 * bytes. In order to stay on the safe side, and not promise more than
1505 * we can cache, those additional bytes must be the minimum possible: 24
1506 * bytes of rpc header (xid through accept state, with AUTH_NULL
1507 * verifier), 12 for the compound header (with zero-length tag), and 44
1508 * for the SEQUENCE op response:
1510 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1513 free_session_slots(struct nfsd4_session *ses)
1517 for (i = 0; i < ses->se_fchannel.maxreqs; i++) {
1518 free_svc_cred(&ses->se_slots[i]->sl_cred);
1519 kfree(ses->se_slots[i]);
1524 * We don't actually need to cache the rpc and session headers, so we
1525 * can allocate a little less for each slot:
1527 static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
1531 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1534 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1535 return size + sizeof(struct nfsd4_slot);
1539 * XXX: If we run out of reserved DRC memory we could (up to a point)
1540 * re-negotiate active sessions and reduce their slot usage to make
1541 * room for new connections. For now we just fail the create session.
1543 static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
1545 u32 slotsize = slot_bytes(ca);
1546 u32 num = ca->maxreqs;
1549 spin_lock(&nfsd_drc_lock);
1550 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1551 nfsd_drc_max_mem - nfsd_drc_mem_used);
1553 * Never use more than a third of the remaining memory,
1554 * unless it's the only way to give this client a slot:
1556 avail = clamp_t(int, avail, slotsize, avail/3);
1557 num = min_t(int, num, avail / slotsize);
1558 nfsd_drc_mem_used += num * slotsize;
1559 spin_unlock(&nfsd_drc_lock);
1564 static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
1566 int slotsize = slot_bytes(ca);
1568 spin_lock(&nfsd_drc_lock);
1569 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
1570 spin_unlock(&nfsd_drc_lock);
1573 static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1574 struct nfsd4_channel_attrs *battrs)
1576 int numslots = fattrs->maxreqs;
1577 int slotsize = slot_bytes(fattrs);
1578 struct nfsd4_session *new;
1581 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
1582 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1583 mem = numslots * sizeof(struct nfsd4_slot *);
1585 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
1588 /* allocate each struct nfsd4_slot and data cache in one piece */
1589 for (i = 0; i < numslots; i++) {
1590 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
1591 if (!new->se_slots[i])
1595 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1596 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1601 kfree(new->se_slots[i]);
1606 static void free_conn(struct nfsd4_conn *c)
1608 svc_xprt_put(c->cn_xprt);
1612 static void nfsd4_conn_lost(struct svc_xpt_user *u)
1614 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1615 struct nfs4_client *clp = c->cn_session->se_client;
1617 spin_lock(&clp->cl_lock);
1618 if (!list_empty(&c->cn_persession)) {
1619 list_del(&c->cn_persession);
1622 nfsd4_probe_callback(clp);
1623 spin_unlock(&clp->cl_lock);
1626 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
1628 struct nfsd4_conn *conn;
1630 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1633 svc_xprt_get(rqstp->rq_xprt);
1634 conn->cn_xprt = rqstp->rq_xprt;
1635 conn->cn_flags = flags;
1636 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1640 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1642 conn->cn_session = ses;
1643 list_add(&conn->cn_persession, &ses->se_conns);
1646 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1648 struct nfs4_client *clp = ses->se_client;
1650 spin_lock(&clp->cl_lock);
1651 __nfsd4_hash_conn(conn, ses);
1652 spin_unlock(&clp->cl_lock);
1655 static int nfsd4_register_conn(struct nfsd4_conn *conn)
1657 conn->cn_xpt_user.callback = nfsd4_conn_lost;
1658 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
1661 static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
1665 nfsd4_hash_conn(conn, ses);
1666 ret = nfsd4_register_conn(conn);
1668 /* oops; xprt is already down: */
1669 nfsd4_conn_lost(&conn->cn_xpt_user);
1670 /* We may have gained or lost a callback channel: */
1671 nfsd4_probe_callback_sync(ses->se_client);
1674 static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
1676 u32 dir = NFS4_CDFC4_FORE;
1678 if (cses->flags & SESSION4_BACK_CHAN)
1679 dir |= NFS4_CDFC4_BACK;
1680 return alloc_conn(rqstp, dir);
1683 /* must be called under client_lock */
1684 static void nfsd4_del_conns(struct nfsd4_session *s)
1686 struct nfs4_client *clp = s->se_client;
1687 struct nfsd4_conn *c;
1689 spin_lock(&clp->cl_lock);
1690 while (!list_empty(&s->se_conns)) {
1691 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1692 list_del_init(&c->cn_persession);
1693 spin_unlock(&clp->cl_lock);
1695 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1698 spin_lock(&clp->cl_lock);
1700 spin_unlock(&clp->cl_lock);
1703 static void __free_session(struct nfsd4_session *ses)
1705 free_session_slots(ses);
1709 static void free_session(struct nfsd4_session *ses)
1711 nfsd4_del_conns(ses);
1712 nfsd4_put_drc_mem(&ses->se_fchannel);
1713 __free_session(ses);
1716 static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
1719 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1721 new->se_client = clp;
1724 INIT_LIST_HEAD(&new->se_conns);
1726 new->se_cb_seq_nr = 1;
1727 new->se_flags = cses->flags;
1728 new->se_cb_prog = cses->callback_prog;
1729 new->se_cb_sec = cses->cb_sec;
1730 atomic_set(&new->se_ref, 0);
1731 idx = hash_sessionid(&new->se_sessionid);
1732 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
1733 spin_lock(&clp->cl_lock);
1734 list_add(&new->se_perclnt, &clp->cl_sessions);
1735 spin_unlock(&clp->cl_lock);
1738 struct sockaddr *sa = svc_addr(rqstp);
1740 * This is a little silly; with sessions there's no real
1741 * use for the callback address. Use the peer address
1742 * as a reasonable default for now, but consider fixing
1743 * the rpc client not to require an address in the
1746 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1747 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
1751 /* caller must hold client_lock */
1752 static struct nfsd4_session *
1753 __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
1755 struct nfsd4_session *elem;
1757 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1759 lockdep_assert_held(&nn->client_lock);
1761 dump_sessionid(__func__, sessionid);
1762 idx = hash_sessionid(sessionid);
1763 /* Search in the appropriate list */
1764 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
1765 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1766 NFS4_MAX_SESSIONID_LEN)) {
1771 dprintk("%s: session not found\n", __func__);
1775 static struct nfsd4_session *
1776 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1779 struct nfsd4_session *session;
1780 __be32 status = nfserr_badsession;
1782 session = __find_in_sessionid_hashtbl(sessionid, net);
1785 status = nfsd4_get_session_locked(session);
1793 /* caller must hold client_lock */
1795 unhash_session(struct nfsd4_session *ses)
1797 struct nfs4_client *clp = ses->se_client;
1798 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1800 lockdep_assert_held(&nn->client_lock);
1802 list_del(&ses->se_hash);
1803 spin_lock(&ses->se_client->cl_lock);
1804 list_del(&ses->se_perclnt);
1805 spin_unlock(&ses->se_client->cl_lock);
1808 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1810 STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
1813 * We're assuming the clid was not given out from a boot
1814 * precisely 2^32 (about 136 years) before this one. That seems
1815 * a safe assumption:
1817 if (clid->cl_boot == (u32)nn->boot_time)
1819 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
1820 clid->cl_boot, clid->cl_id, nn->boot_time);
1825 * XXX Should we use a slab cache ?
1826 * This type of memory management is somewhat inefficient, but we use it
1827 * anyway since SETCLIENTID is not a common operation.
1829 static struct nfs4_client *alloc_client(struct xdr_netobj name)
1831 struct nfs4_client *clp;
1834 clp = kmem_cache_zalloc(client_slab, GFP_KERNEL);
1837 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
1838 if (clp->cl_name.data == NULL)
1840 clp->cl_ownerstr_hashtbl = kmalloc_array(OWNER_HASH_SIZE,
1841 sizeof(struct list_head),
1843 if (!clp->cl_ownerstr_hashtbl)
1844 goto err_no_hashtbl;
1845 for (i = 0; i < OWNER_HASH_SIZE; i++)
1846 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
1847 clp->cl_name.len = name.len;
1848 INIT_LIST_HEAD(&clp->cl_sessions);
1849 idr_init(&clp->cl_stateids);
1850 atomic_set(&clp->cl_refcount, 0);
1851 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1852 INIT_LIST_HEAD(&clp->cl_idhash);
1853 INIT_LIST_HEAD(&clp->cl_openowners);
1854 INIT_LIST_HEAD(&clp->cl_delegations);
1855 INIT_LIST_HEAD(&clp->cl_lru);
1856 INIT_LIST_HEAD(&clp->cl_revoked);
1857 #ifdef CONFIG_NFSD_PNFS
1858 INIT_LIST_HEAD(&clp->cl_lo_states);
1860 INIT_LIST_HEAD(&clp->async_copies);
1861 spin_lock_init(&clp->async_lock);
1862 spin_lock_init(&clp->cl_lock);
1863 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1866 kfree(clp->cl_name.data);
1868 kmem_cache_free(client_slab, clp);
1873 free_client(struct nfs4_client *clp)
1875 while (!list_empty(&clp->cl_sessions)) {
1876 struct nfsd4_session *ses;
1877 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1879 list_del(&ses->se_perclnt);
1880 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1883 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
1884 free_svc_cred(&clp->cl_cred);
1885 kfree(clp->cl_ownerstr_hashtbl);
1886 kfree(clp->cl_name.data);
1887 idr_destroy(&clp->cl_stateids);
1888 kmem_cache_free(client_slab, clp);
1891 /* must be called under the client_lock */
1893 unhash_client_locked(struct nfs4_client *clp)
1895 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1896 struct nfsd4_session *ses;
1898 lockdep_assert_held(&nn->client_lock);
1900 /* Mark the client as expired! */
1902 /* Make it invisible */
1903 if (!list_empty(&clp->cl_idhash)) {
1904 list_del_init(&clp->cl_idhash);
1905 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1906 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1908 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1910 list_del_init(&clp->cl_lru);
1911 spin_lock(&clp->cl_lock);
1912 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1913 list_del_init(&ses->se_hash);
1914 spin_unlock(&clp->cl_lock);
1918 unhash_client(struct nfs4_client *clp)
1920 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1922 spin_lock(&nn->client_lock);
1923 unhash_client_locked(clp);
1924 spin_unlock(&nn->client_lock);
1927 static __be32 mark_client_expired_locked(struct nfs4_client *clp)
1929 if (atomic_read(&clp->cl_refcount))
1930 return nfserr_jukebox;
1931 unhash_client_locked(clp);
1936 __destroy_client(struct nfs4_client *clp)
1939 struct nfs4_openowner *oo;
1940 struct nfs4_delegation *dp;
1941 struct list_head reaplist;
1943 INIT_LIST_HEAD(&reaplist);
1944 spin_lock(&state_lock);
1945 while (!list_empty(&clp->cl_delegations)) {
1946 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
1947 WARN_ON(!unhash_delegation_locked(dp));
1948 list_add(&dp->dl_recall_lru, &reaplist);
1950 spin_unlock(&state_lock);
1951 while (!list_empty(&reaplist)) {
1952 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
1953 list_del_init(&dp->dl_recall_lru);
1954 destroy_unhashed_deleg(dp);
1956 while (!list_empty(&clp->cl_revoked)) {
1957 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
1958 list_del_init(&dp->dl_recall_lru);
1959 nfs4_put_stid(&dp->dl_stid);
1961 while (!list_empty(&clp->cl_openowners)) {
1962 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1963 nfs4_get_stateowner(&oo->oo_owner);
1964 release_openowner(oo);
1966 for (i = 0; i < OWNER_HASH_SIZE; i++) {
1967 struct nfs4_stateowner *so, *tmp;
1969 list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i],
1971 /* Should be no openowners at this point */
1972 WARN_ON_ONCE(so->so_is_open_owner);
1973 remove_blocked_locks(lockowner(so));
1976 nfsd4_return_all_client_layouts(clp);
1977 nfsd4_shutdown_copy(clp);
1978 nfsd4_shutdown_callback(clp);
1979 if (clp->cl_cb_conn.cb_xprt)
1980 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
1985 destroy_client(struct nfs4_client *clp)
1988 __destroy_client(clp);
1991 static void expire_client(struct nfs4_client *clp)
1994 nfsd4_client_record_remove(clp);
1995 __destroy_client(clp);
1998 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
2000 memcpy(target->cl_verifier.data, source->data,
2001 sizeof(target->cl_verifier.data));
2004 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
2006 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
2007 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
2010 static int copy_cred(struct svc_cred *target, struct svc_cred *source)
2012 target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL);
2013 target->cr_raw_principal = kstrdup(source->cr_raw_principal,
2015 target->cr_targ_princ = kstrdup(source->cr_targ_princ, GFP_KERNEL);
2016 if ((source->cr_principal && !target->cr_principal) ||
2017 (source->cr_raw_principal && !target->cr_raw_principal) ||
2018 (source->cr_targ_princ && !target->cr_targ_princ))
2021 target->cr_flavor = source->cr_flavor;
2022 target->cr_uid = source->cr_uid;
2023 target->cr_gid = source->cr_gid;
2024 target->cr_group_info = source->cr_group_info;
2025 get_group_info(target->cr_group_info);
2026 target->cr_gss_mech = source->cr_gss_mech;
2027 if (source->cr_gss_mech)
2028 gss_mech_get(source->cr_gss_mech);
2033 compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
2035 if (o1->len < o2->len)
2037 if (o1->len > o2->len)
2039 return memcmp(o1->data, o2->data, o1->len);
2042 static int same_name(const char *n1, const char *n2)
2044 return 0 == memcmp(n1, n2, HEXDIR_LEN);
2048 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
2050 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
2054 same_clid(clientid_t *cl1, clientid_t *cl2)
2056 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
2059 static bool groups_equal(struct group_info *g1, struct group_info *g2)
2063 if (g1->ngroups != g2->ngroups)
2065 for (i=0; i<g1->ngroups; i++)
2066 if (!gid_eq(g1->gid[i], g2->gid[i]))
2072 * RFC 3530 language requires clid_inuse be returned when the
2073 * "principal" associated with a requests differs from that previously
2074 * used. We use uid, gid's, and gss principal string as our best
2075 * approximation. We also don't want to allow non-gss use of a client
2076 * established using gss: in theory cr_principal should catch that
2077 * change, but in practice cr_principal can be null even in the gss case
2078 * since gssd doesn't always pass down a principal string.
2080 static bool is_gss_cred(struct svc_cred *cr)
2082 /* Is cr_flavor one of the gss "pseudoflavors"?: */
2083 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
2088 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
2090 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
2091 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
2092 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
2093 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
2095 /* XXX: check that cr_targ_princ fields match ? */
2096 if (cr1->cr_principal == cr2->cr_principal)
2098 if (!cr1->cr_principal || !cr2->cr_principal)
2100 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
2103 static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
2105 struct svc_cred *cr = &rqstp->rq_cred;
2108 if (!cr->cr_gss_mech)
2110 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
2111 return service == RPC_GSS_SVC_INTEGRITY ||
2112 service == RPC_GSS_SVC_PRIVACY;
2115 bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
2117 struct svc_cred *cr = &rqstp->rq_cred;
2119 if (!cl->cl_mach_cred)
2121 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
2123 if (!svc_rqst_integrity_protected(rqstp))
2125 if (cl->cl_cred.cr_raw_principal)
2126 return 0 == strcmp(cl->cl_cred.cr_raw_principal,
2127 cr->cr_raw_principal);
2128 if (!cr->cr_principal)
2130 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
2133 static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
2138 * This is opaque to client, so no need to byte-swap. Use
2139 * __force to keep sparse happy
2141 verf[0] = (__force __be32)get_seconds();
2142 verf[1] = (__force __be32)nn->clverifier_counter++;
2143 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
2146 static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
2148 clp->cl_clientid.cl_boot = nn->boot_time;
2149 clp->cl_clientid.cl_id = nn->clientid_counter++;
2150 gen_confirm(clp, nn);
2153 static struct nfs4_stid *
2154 find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
2156 struct nfs4_stid *ret;
2158 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
2159 if (!ret || !ret->sc_type)
2164 static struct nfs4_stid *
2165 find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
2167 struct nfs4_stid *s;
2169 spin_lock(&cl->cl_lock);
2170 s = find_stateid_locked(cl, t);
2172 if (typemask & s->sc_type)
2173 refcount_inc(&s->sc_count);
2177 spin_unlock(&cl->cl_lock);
2181 static struct nfs4_client *create_client(struct xdr_netobj name,
2182 struct svc_rqst *rqstp, nfs4_verifier *verf)
2184 struct nfs4_client *clp;
2185 struct sockaddr *sa = svc_addr(rqstp);
2187 struct net *net = SVC_NET(rqstp);
2189 clp = alloc_client(name);
2193 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
2198 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
2199 clp->cl_time = get_seconds();
2200 clear_bit(0, &clp->cl_cb_slot_busy);
2201 copy_verf(clp, verf);
2202 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
2203 clp->cl_cb_session = NULL;
2209 add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
2211 struct rb_node **new = &(root->rb_node), *parent = NULL;
2212 struct nfs4_client *clp;
2215 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
2218 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
2219 new = &((*new)->rb_left);
2221 new = &((*new)->rb_right);
2224 rb_link_node(&new_clp->cl_namenode, parent, new);
2225 rb_insert_color(&new_clp->cl_namenode, root);
2228 static struct nfs4_client *
2229 find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2232 struct rb_node *node = root->rb_node;
2233 struct nfs4_client *clp;
2236 clp = rb_entry(node, struct nfs4_client, cl_namenode);
2237 cmp = compare_blob(&clp->cl_name, name);
2239 node = node->rb_left;
2241 node = node->rb_right;
2249 add_to_unconfirmed(struct nfs4_client *clp)
2251 unsigned int idhashval;
2252 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2254 lockdep_assert_held(&nn->client_lock);
2256 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2257 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
2258 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
2259 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
2260 renew_client_locked(clp);
2264 move_to_confirmed(struct nfs4_client *clp)
2266 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
2267 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2269 lockdep_assert_held(&nn->client_lock);
2271 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
2272 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
2273 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
2274 add_clp_to_name_tree(clp, &nn->conf_name_tree);
2275 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2276 renew_client_locked(clp);
2279 static struct nfs4_client *
2280 find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
2282 struct nfs4_client *clp;
2283 unsigned int idhashval = clientid_hashval(clid->cl_id);
2285 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
2286 if (same_clid(&clp->cl_clientid, clid)) {
2287 if ((bool)clp->cl_minorversion != sessions)
2289 renew_client_locked(clp);
2296 static struct nfs4_client *
2297 find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2299 struct list_head *tbl = nn->conf_id_hashtbl;
2301 lockdep_assert_held(&nn->client_lock);
2302 return find_client_in_id_table(tbl, clid, sessions);
2305 static struct nfs4_client *
2306 find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2308 struct list_head *tbl = nn->unconf_id_hashtbl;
2310 lockdep_assert_held(&nn->client_lock);
2311 return find_client_in_id_table(tbl, clid, sessions);
2314 static bool clp_used_exchangeid(struct nfs4_client *clp)
2316 return clp->cl_exchange_flags != 0;
2319 static struct nfs4_client *
2320 find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
2322 lockdep_assert_held(&nn->client_lock);
2323 return find_clp_in_name_tree(name, &nn->conf_name_tree);
2326 static struct nfs4_client *
2327 find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
2329 lockdep_assert_held(&nn->client_lock);
2330 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
2334 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
2336 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
2337 struct sockaddr *sa = svc_addr(rqstp);
2338 u32 scopeid = rpc_get_scope_id(sa);
2339 unsigned short expected_family;
2341 /* Currently, we only support tcp and tcp6 for the callback channel */
2342 if (se->se_callback_netid_len == 3 &&
2343 !memcmp(se->se_callback_netid_val, "tcp", 3))
2344 expected_family = AF_INET;
2345 else if (se->se_callback_netid_len == 4 &&
2346 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2347 expected_family = AF_INET6;
2351 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
2352 se->se_callback_addr_len,
2353 (struct sockaddr *)&conn->cb_addr,
2354 sizeof(conn->cb_addr));
2356 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
2359 if (conn->cb_addr.ss_family == AF_INET6)
2360 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
2362 conn->cb_prog = se->se_callback_prog;
2363 conn->cb_ident = se->se_callback_ident;
2364 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
2367 conn->cb_addr.ss_family = AF_UNSPEC;
2368 conn->cb_addrlen = 0;
2369 dprintk("NFSD: this client (clientid %08x/%08x) "
2370 "will not receive delegations\n",
2371 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2377 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
2380 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2382 struct xdr_buf *buf = resp->xdr.buf;
2383 struct nfsd4_slot *slot = resp->cstate.slot;
2386 dprintk("--> %s slot %p\n", __func__, slot);
2388 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
2389 slot->sl_opcnt = resp->opcnt;
2390 slot->sl_status = resp->cstate.status;
2391 free_svc_cred(&slot->sl_cred);
2392 copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred);
2394 if (!nfsd4_cache_this(resp)) {
2395 slot->sl_flags &= ~NFSD4_SLOT_CACHED;
2398 slot->sl_flags |= NFSD4_SLOT_CACHED;
2400 base = resp->cstate.data_offset;
2401 slot->sl_datalen = buf->len - base;
2402 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
2403 WARN(1, "%s: sessions DRC could not cache compound\n",
2409 * Encode the replay sequence operation from the slot values.
2410 * If cachethis is FALSE encode the uncached rep error on the next
2411 * operation which sets resp->p and increments resp->opcnt for
2412 * nfs4svc_encode_compoundres.
2416 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2417 struct nfsd4_compoundres *resp)
2419 struct nfsd4_op *op;
2420 struct nfsd4_slot *slot = resp->cstate.slot;
2422 /* Encode the replayed sequence operation */
2423 op = &args->ops[resp->opcnt - 1];
2424 nfsd4_encode_operation(resp, op);
2426 if (slot->sl_flags & NFSD4_SLOT_CACHED)
2428 if (args->opcnt == 1) {
2430 * The original operation wasn't a solo sequence--we
2431 * always cache those--so this retry must not match the
2434 op->status = nfserr_seq_false_retry;
2436 op = &args->ops[resp->opcnt++];
2437 op->status = nfserr_retry_uncached_rep;
2438 nfsd4_encode_operation(resp, op);
2444 * The sequence operation is not cached because we can use the slot and
2448 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2449 struct nfsd4_sequence *seq)
2451 struct nfsd4_slot *slot = resp->cstate.slot;
2452 struct xdr_stream *xdr = &resp->xdr;
2456 dprintk("--> %s slot %p\n", __func__, slot);
2458 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
2462 p = xdr_reserve_space(xdr, slot->sl_datalen);
2465 return nfserr_serverfault;
2467 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2468 xdr_commit_encode(xdr);
2470 resp->opcnt = slot->sl_opcnt;
2471 return slot->sl_status;
2475 * Set the exchange_id flags returned by the server.
2478 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2480 #ifdef CONFIG_NFSD_PNFS
2481 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
2483 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
2486 /* Referrals are supported, Migration is not. */
2487 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2489 /* set the wire flags to return to client. */
2490 clid->flags = new->cl_exchange_flags;
2493 static bool client_has_openowners(struct nfs4_client *clp)
2495 struct nfs4_openowner *oo;
2497 list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
2498 if (!list_empty(&oo->oo_owner.so_stateids))
2504 static bool client_has_state(struct nfs4_client *clp)
2506 return client_has_openowners(clp)
2507 #ifdef CONFIG_NFSD_PNFS
2508 || !list_empty(&clp->cl_lo_states)
2510 || !list_empty(&clp->cl_delegations)
2511 || !list_empty(&clp->cl_sessions)
2512 || !list_empty(&clp->async_copies);
2516 nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2517 union nfsd4_op_u *u)
2519 struct nfsd4_exchange_id *exid = &u->exchange_id;
2520 struct nfs4_client *conf, *new;
2521 struct nfs4_client *unconf = NULL;
2523 char addr_str[INET6_ADDRSTRLEN];
2524 nfs4_verifier verf = exid->verifier;
2525 struct sockaddr *sa = svc_addr(rqstp);
2526 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
2527 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2529 rpc_ntop(sa, addr_str, sizeof(addr_str));
2530 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
2531 "ip_addr=%s flags %x, spa_how %d\n",
2532 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
2533 addr_str, exid->flags, exid->spa_how);
2535 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
2536 return nfserr_inval;
2538 new = create_client(exid->clname, rqstp, &verf);
2540 return nfserr_jukebox;
2542 switch (exid->spa_how) {
2544 exid->spo_must_enforce[0] = 0;
2545 exid->spo_must_enforce[1] = (
2546 1 << (OP_BIND_CONN_TO_SESSION - 32) |
2547 1 << (OP_EXCHANGE_ID - 32) |
2548 1 << (OP_CREATE_SESSION - 32) |
2549 1 << (OP_DESTROY_SESSION - 32) |
2550 1 << (OP_DESTROY_CLIENTID - 32));
2552 exid->spo_must_allow[0] &= (1 << (OP_CLOSE) |
2553 1 << (OP_OPEN_DOWNGRADE) |
2555 1 << (OP_DELEGRETURN));
2557 exid->spo_must_allow[1] &= (
2558 1 << (OP_TEST_STATEID - 32) |
2559 1 << (OP_FREE_STATEID - 32));
2560 if (!svc_rqst_integrity_protected(rqstp)) {
2561 status = nfserr_inval;
2565 * Sometimes userspace doesn't give us a principal.
2566 * Which is a bug, really. Anyway, we can't enforce
2567 * MACH_CRED in that case, better to give up now:
2569 if (!new->cl_cred.cr_principal &&
2570 !new->cl_cred.cr_raw_principal) {
2571 status = nfserr_serverfault;
2574 new->cl_mach_cred = true;
2577 default: /* checked by xdr code */
2580 status = nfserr_encr_alg_unsupp;
2584 /* Cases below refer to rfc 5661 section 18.35.4: */
2585 spin_lock(&nn->client_lock);
2586 conf = find_confirmed_client_by_name(&exid->clname, nn);
2588 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2589 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2592 if (!clp_used_exchangeid(conf)) { /* buggy client */
2593 status = nfserr_inval;
2596 if (!nfsd4_mach_creds_match(conf, rqstp)) {
2597 status = nfserr_wrong_cred;
2600 if (!creds_match) { /* case 9 */
2601 status = nfserr_perm;
2604 if (!verfs_match) { /* case 8 */
2605 status = nfserr_not_same;
2609 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
2612 if (!creds_match) { /* case 3 */
2613 if (client_has_state(conf)) {
2614 status = nfserr_clid_inuse;
2619 if (verfs_match) { /* case 2 */
2620 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
2623 /* case 5, client reboot */
2628 if (update) { /* case 7 */
2629 status = nfserr_noent;
2633 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
2634 if (unconf) /* case 4, possible retry or client restart */
2635 unhash_client_locked(unconf);
2637 /* case 1 (normal case) */
2640 status = mark_client_expired_locked(conf);
2644 new->cl_minorversion = cstate->minorversion;
2645 new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0];
2646 new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1];
2649 add_to_unconfirmed(new);
2652 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
2653 exid->clientid.cl_id = conf->cl_clientid.cl_id;
2655 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
2656 nfsd4_set_ex_flags(conf, exid);
2658 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
2659 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
2663 spin_unlock(&nn->client_lock);
2668 expire_client(unconf);
2673 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
2675 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2678 /* The slot is in use, and no response has been sent. */
2680 if (seqid == slot_seqid)
2681 return nfserr_jukebox;
2683 return nfserr_seq_misordered;
2685 /* Note unsigned 32-bit arithmetic handles wraparound: */
2686 if (likely(seqid == slot_seqid + 1))
2688 if (seqid == slot_seqid)
2689 return nfserr_replay_cache;
2690 return nfserr_seq_misordered;
2694 * Cache the create session result into the create session single DRC
2695 * slot cache by saving the xdr structure. sl_seqid has been set.
2696 * Do this for solo or embedded create session operations.
2699 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
2700 struct nfsd4_clid_slot *slot, __be32 nfserr)
2702 slot->sl_status = nfserr;
2703 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2707 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2708 struct nfsd4_clid_slot *slot)
2710 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2711 return slot->sl_status;
2714 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2715 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2716 1 + /* MIN tag is length with zero, only length */ \
2717 3 + /* version, opcount, opcode */ \
2718 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2719 /* seqid, slotID, slotID, cache */ \
2720 4 ) * sizeof(__be32))
2722 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2723 2 + /* verifier: AUTH_NULL, length 0 */\
2725 1 + /* MIN tag is length with zero, only length */ \
2726 3 + /* opcount, opcode, opstatus*/ \
2727 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2728 /* seqid, slotID, slotID, slotID, status */ \
2729 5 ) * sizeof(__be32))
2731 static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
2733 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2735 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2736 return nfserr_toosmall;
2737 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2738 return nfserr_toosmall;
2739 ca->headerpadsz = 0;
2740 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2741 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2742 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2743 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2744 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2745 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2747 * Note decreasing slot size below client's request may make it
2748 * difficult for client to function correctly, whereas
2749 * decreasing the number of slots will (just?) affect
2750 * performance. When short on memory we therefore prefer to
2751 * decrease number of slots instead of their size. Clients that
2752 * request larger slots than they need will get poor results:
2754 ca->maxreqs = nfsd4_get_drc_mem(ca);
2756 return nfserr_jukebox;
2762 * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
2763 * These are based on similar macros in linux/sunrpc/msg_prot.h .
2765 #define RPC_MAX_HEADER_WITH_AUTH_SYS \
2766 (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
2768 #define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
2769 (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
2771 #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2772 RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
2773 #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2774 RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
2777 static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2779 ca->headerpadsz = 0;
2781 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
2782 return nfserr_toosmall;
2783 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
2784 return nfserr_toosmall;
2785 ca->maxresp_cached = 0;
2787 return nfserr_toosmall;
2792 static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2794 switch (cbs->flavor) {
2800 * GSS case: the spec doesn't allow us to return this
2801 * error. But it also doesn't allow us not to support
2803 * I'd rather this fail hard than return some error the
2804 * client might think it can already handle:
2806 return nfserr_encr_alg_unsupp;
2811 nfsd4_create_session(struct svc_rqst *rqstp,
2812 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
2814 struct nfsd4_create_session *cr_ses = &u->create_session;
2815 struct sockaddr *sa = svc_addr(rqstp);
2816 struct nfs4_client *conf, *unconf;
2817 struct nfs4_client *old = NULL;
2818 struct nfsd4_session *new;
2819 struct nfsd4_conn *conn;
2820 struct nfsd4_clid_slot *cs_slot = NULL;
2822 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2824 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2825 return nfserr_inval;
2826 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2829 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
2832 status = check_backchannel_attrs(&cr_ses->back_channel);
2834 goto out_release_drc_mem;
2835 status = nfserr_jukebox;
2836 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
2838 goto out_release_drc_mem;
2839 conn = alloc_conn_from_crses(rqstp, cr_ses);
2841 goto out_free_session;
2843 spin_lock(&nn->client_lock);
2844 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
2845 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
2846 WARN_ON_ONCE(conf && unconf);
2849 status = nfserr_wrong_cred;
2850 if (!nfsd4_mach_creds_match(conf, rqstp))
2852 cs_slot = &conf->cl_cs_slot;
2853 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2855 if (status == nfserr_replay_cache)
2856 status = nfsd4_replay_create_session(cr_ses, cs_slot);
2859 } else if (unconf) {
2860 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
2861 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
2862 status = nfserr_clid_inuse;
2865 status = nfserr_wrong_cred;
2866 if (!nfsd4_mach_creds_match(unconf, rqstp))
2868 cs_slot = &unconf->cl_cs_slot;
2869 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2871 /* an unconfirmed replay returns misordered */
2872 status = nfserr_seq_misordered;
2875 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
2877 status = mark_client_expired_locked(old);
2883 move_to_confirmed(unconf);
2886 status = nfserr_stale_clientid;
2890 /* Persistent sessions are not supported */
2891 cr_ses->flags &= ~SESSION4_PERSIST;
2892 /* Upshifting from TCP to RDMA is not supported */
2893 cr_ses->flags &= ~SESSION4_RDMA;
2895 init_session(rqstp, new, conf, cr_ses);
2896 nfsd4_get_session_locked(new);
2898 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
2899 NFS4_MAX_SESSIONID_LEN);
2900 cs_slot->sl_seqid++;
2901 cr_ses->seqid = cs_slot->sl_seqid;
2903 /* cache solo and embedded create sessions under the client_lock */
2904 nfsd4_cache_create_session(cr_ses, cs_slot, status);
2905 spin_unlock(&nn->client_lock);
2906 /* init connection and backchannel */
2907 nfsd4_init_conn(rqstp, conn, new);
2908 nfsd4_put_session(new);
2913 spin_unlock(&nn->client_lock);
2918 __free_session(new);
2919 out_release_drc_mem:
2920 nfsd4_put_drc_mem(&cr_ses->fore_channel);
2924 static __be32 nfsd4_map_bcts_dir(u32 *dir)
2927 case NFS4_CDFC4_FORE:
2928 case NFS4_CDFC4_BACK:
2930 case NFS4_CDFC4_FORE_OR_BOTH:
2931 case NFS4_CDFC4_BACK_OR_BOTH:
2932 *dir = NFS4_CDFC4_BOTH;
2935 return nfserr_inval;
2938 __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp,
2939 struct nfsd4_compound_state *cstate,
2940 union nfsd4_op_u *u)
2942 struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl;
2943 struct nfsd4_session *session = cstate->session;
2944 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2947 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2950 spin_lock(&nn->client_lock);
2951 session->se_cb_prog = bc->bc_cb_program;
2952 session->se_cb_sec = bc->bc_cb_sec;
2953 spin_unlock(&nn->client_lock);
2955 nfsd4_probe_callback(session->se_client);
2960 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2961 struct nfsd4_compound_state *cstate,
2962 union nfsd4_op_u *u)
2964 struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session;
2966 struct nfsd4_conn *conn;
2967 struct nfsd4_session *session;
2968 struct net *net = SVC_NET(rqstp);
2969 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2971 if (!nfsd4_last_compound_op(rqstp))
2972 return nfserr_not_only_op;
2973 spin_lock(&nn->client_lock);
2974 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
2975 spin_unlock(&nn->client_lock);
2977 goto out_no_session;
2978 status = nfserr_wrong_cred;
2979 if (!nfsd4_mach_creds_match(session->se_client, rqstp))
2981 status = nfsd4_map_bcts_dir(&bcts->dir);
2984 conn = alloc_conn(rqstp, bcts->dir);
2985 status = nfserr_jukebox;
2988 nfsd4_init_conn(rqstp, conn, session);
2991 nfsd4_put_session(session);
2996 static bool nfsd4_compound_in_session(struct nfsd4_compound_state *cstate, struct nfs4_sessionid *sid)
2998 if (!cstate->session)
3000 return !memcmp(sid, &cstate->session->se_sessionid, sizeof(*sid));
3004 nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate,
3005 union nfsd4_op_u *u)
3007 struct nfs4_sessionid *sessionid = &u->destroy_session.sessionid;
3008 struct nfsd4_session *ses;
3010 int ref_held_by_me = 0;
3011 struct net *net = SVC_NET(r);
3012 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3014 status = nfserr_not_only_op;
3015 if (nfsd4_compound_in_session(cstate, sessionid)) {
3016 if (!nfsd4_last_compound_op(r))
3020 dump_sessionid(__func__, sessionid);
3021 spin_lock(&nn->client_lock);
3022 ses = find_in_sessionid_hashtbl(sessionid, net, &status);
3024 goto out_client_lock;
3025 status = nfserr_wrong_cred;
3026 if (!nfsd4_mach_creds_match(ses->se_client, r))
3027 goto out_put_session;
3028 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
3030 goto out_put_session;
3031 unhash_session(ses);
3032 spin_unlock(&nn->client_lock);
3034 nfsd4_probe_callback_sync(ses->se_client);
3036 spin_lock(&nn->client_lock);
3039 nfsd4_put_session_locked(ses);
3041 spin_unlock(&nn->client_lock);
3046 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
3048 struct nfsd4_conn *c;
3050 list_for_each_entry(c, &s->se_conns, cn_persession) {
3051 if (c->cn_xprt == xpt) {
3058 static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
3060 struct nfs4_client *clp = ses->se_client;
3061 struct nfsd4_conn *c;
3062 __be32 status = nfs_ok;
3065 spin_lock(&clp->cl_lock);
3066 c = __nfsd4_find_conn(new->cn_xprt, ses);
3069 status = nfserr_conn_not_bound_to_session;
3070 if (clp->cl_mach_cred)
3072 __nfsd4_hash_conn(new, ses);
3073 spin_unlock(&clp->cl_lock);
3074 ret = nfsd4_register_conn(new);
3076 /* oops; xprt is already down: */
3077 nfsd4_conn_lost(&new->cn_xpt_user);
3080 spin_unlock(&clp->cl_lock);
3085 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
3087 struct nfsd4_compoundargs *args = rqstp->rq_argp;
3089 return args->opcnt > session->se_fchannel.maxops;
3092 static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
3093 struct nfsd4_session *session)
3095 struct xdr_buf *xb = &rqstp->rq_arg;
3097 return xb->len > session->se_fchannel.maxreq_sz;
3100 static bool replay_matches_cache(struct svc_rqst *rqstp,
3101 struct nfsd4_sequence *seq, struct nfsd4_slot *slot)
3103 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
3105 if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) !=
3106 (bool)seq->cachethis)
3109 * If there's an error than the reply can have fewer ops than
3110 * the call. But if we cached a reply with *more* ops than the
3111 * call you're sending us now, then this new call is clearly not
3112 * really a replay of the old one:
3114 if (slot->sl_opcnt < argp->opcnt)
3116 /* This is the only check explicitly called by spec: */
3117 if (!same_creds(&rqstp->rq_cred, &slot->sl_cred))
3120 * There may be more comparisons we could actually do, but the
3121 * spec doesn't require us to catch every case where the calls
3122 * don't match (that would require caching the call as well as
3123 * the reply), so we don't bother.
3129 nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3130 union nfsd4_op_u *u)
3132 struct nfsd4_sequence *seq = &u->sequence;
3133 struct nfsd4_compoundres *resp = rqstp->rq_resp;
3134 struct xdr_stream *xdr = &resp->xdr;
3135 struct nfsd4_session *session;
3136 struct nfs4_client *clp;
3137 struct nfsd4_slot *slot;
3138 struct nfsd4_conn *conn;
3141 struct net *net = SVC_NET(rqstp);
3142 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3144 if (resp->opcnt != 1)
3145 return nfserr_sequence_pos;
3148 * Will be either used or freed by nfsd4_sequence_check_conn
3151 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
3153 return nfserr_jukebox;
3155 spin_lock(&nn->client_lock);
3156 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
3158 goto out_no_session;
3159 clp = session->se_client;
3161 status = nfserr_too_many_ops;
3162 if (nfsd4_session_too_many_ops(rqstp, session))
3163 goto out_put_session;
3165 status = nfserr_req_too_big;
3166 if (nfsd4_request_too_big(rqstp, session))
3167 goto out_put_session;
3169 status = nfserr_badslot;
3170 if (seq->slotid >= session->se_fchannel.maxreqs)
3171 goto out_put_session;
3173 slot = session->se_slots[seq->slotid];
3174 dprintk("%s: slotid %d\n", __func__, seq->slotid);
3176 /* We do not negotiate the number of slots yet, so set the
3177 * maxslots to the session maxreqs which is used to encode
3178 * sr_highest_slotid and the sr_target_slot id to maxslots */
3179 seq->maxslots = session->se_fchannel.maxreqs;
3181 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
3182 slot->sl_flags & NFSD4_SLOT_INUSE);
3183 if (status == nfserr_replay_cache) {
3184 status = nfserr_seq_misordered;
3185 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
3186 goto out_put_session;
3187 status = nfserr_seq_false_retry;
3188 if (!replay_matches_cache(rqstp, seq, slot))
3189 goto out_put_session;
3190 cstate->slot = slot;
3191 cstate->session = session;
3193 /* Return the cached reply status and set cstate->status
3194 * for nfsd4_proc_compound processing */
3195 status = nfsd4_replay_cache_entry(resp, seq);
3196 cstate->status = nfserr_replay_cache;
3200 goto out_put_session;
3202 status = nfsd4_sequence_check_conn(conn, session);
3205 goto out_put_session;
3207 buflen = (seq->cachethis) ?
3208 session->se_fchannel.maxresp_cached :
3209 session->se_fchannel.maxresp_sz;
3210 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
3212 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
3213 goto out_put_session;
3214 svc_reserve(rqstp, buflen);
3217 /* Success! bump slot seqid */
3218 slot->sl_seqid = seq->seqid;
3219 slot->sl_flags |= NFSD4_SLOT_INUSE;
3221 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
3223 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
3225 cstate->slot = slot;
3226 cstate->session = session;
3230 switch (clp->cl_cb_state) {
3232 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
3234 case NFSD4_CB_FAULT:
3235 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
3238 seq->status_flags = 0;
3240 if (!list_empty(&clp->cl_revoked))
3241 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
3245 spin_unlock(&nn->client_lock);
3248 nfsd4_put_session_locked(session);
3249 goto out_no_session;
3253 nfsd4_sequence_done(struct nfsd4_compoundres *resp)
3255 struct nfsd4_compound_state *cs = &resp->cstate;
3257 if (nfsd4_has_session(cs)) {
3258 if (cs->status != nfserr_replay_cache) {
3259 nfsd4_store_cache_entry(resp);
3260 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
3262 /* Drop session reference that was taken in nfsd4_sequence() */
3263 nfsd4_put_session(cs->session);
3265 put_client_renew(cs->clp);
3269 nfsd4_destroy_clientid(struct svc_rqst *rqstp,
3270 struct nfsd4_compound_state *cstate,
3271 union nfsd4_op_u *u)
3273 struct nfsd4_destroy_clientid *dc = &u->destroy_clientid;
3274 struct nfs4_client *conf, *unconf;
3275 struct nfs4_client *clp = NULL;
3277 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3279 spin_lock(&nn->client_lock);
3280 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
3281 conf = find_confirmed_client(&dc->clientid, true, nn);
3282 WARN_ON_ONCE(conf && unconf);
3285 if (client_has_state(conf)) {
3286 status = nfserr_clientid_busy;
3289 status = mark_client_expired_locked(conf);
3296 status = nfserr_stale_clientid;
3299 if (!nfsd4_mach_creds_match(clp, rqstp)) {
3301 status = nfserr_wrong_cred;
3304 unhash_client_locked(clp);
3306 spin_unlock(&nn->client_lock);
3313 nfsd4_reclaim_complete(struct svc_rqst *rqstp,
3314 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
3316 struct nfsd4_reclaim_complete *rc = &u->reclaim_complete;
3319 if (rc->rca_one_fs) {
3320 if (!cstate->current_fh.fh_dentry)
3321 return nfserr_nofilehandle;
3323 * We don't take advantage of the rca_one_fs case.
3324 * That's OK, it's optional, we can safely ignore it.
3329 status = nfserr_complete_already;
3330 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
3331 &cstate->session->se_client->cl_flags))
3334 status = nfserr_stale_clientid;
3335 if (is_client_expired(cstate->session->se_client))
3337 * The following error isn't really legal.
3338 * But we only get here if the client just explicitly
3339 * destroyed the client. Surely it no longer cares what
3340 * error it gets back on an operation for the dead
3346 nfsd4_client_record_create(cstate->session->se_client);
3352 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3353 union nfsd4_op_u *u)
3355 struct nfsd4_setclientid *setclid = &u->setclientid;
3356 struct xdr_netobj clname = setclid->se_name;
3357 nfs4_verifier clverifier = setclid->se_verf;
3358 struct nfs4_client *conf, *new;
3359 struct nfs4_client *unconf = NULL;
3361 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3363 new = create_client(clname, rqstp, &clverifier);
3365 return nfserr_jukebox;
3366 /* Cases below refer to rfc 3530 section 14.2.33: */
3367 spin_lock(&nn->client_lock);
3368 conf = find_confirmed_client_by_name(&clname, nn);
3369 if (conf && client_has_state(conf)) {
3371 status = nfserr_clid_inuse;
3372 if (clp_used_exchangeid(conf))
3374 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
3375 char addr_str[INET6_ADDRSTRLEN];
3376 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
3378 dprintk("NFSD: setclientid: string in use by client "
3379 "at %s\n", addr_str);
3383 unconf = find_unconfirmed_client_by_name(&clname, nn);
3385 unhash_client_locked(unconf);
3386 if (conf && same_verf(&conf->cl_verifier, &clverifier)) {
3387 /* case 1: probable callback update */
3388 copy_clid(new, conf);
3389 gen_confirm(new, nn);
3390 } else /* case 4 (new client) or cases 2, 3 (client reboot): */
3392 new->cl_minorversion = 0;
3393 gen_callback(new, setclid, rqstp);
3394 add_to_unconfirmed(new);
3395 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
3396 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
3397 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
3401 spin_unlock(&nn->client_lock);
3405 expire_client(unconf);
3411 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
3412 struct nfsd4_compound_state *cstate,
3413 union nfsd4_op_u *u)
3415 struct nfsd4_setclientid_confirm *setclientid_confirm =
3416 &u->setclientid_confirm;
3417 struct nfs4_client *conf, *unconf;
3418 struct nfs4_client *old = NULL;
3419 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
3420 clientid_t * clid = &setclientid_confirm->sc_clientid;
3422 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3424 if (STALE_CLIENTID(clid, nn))
3425 return nfserr_stale_clientid;
3427 spin_lock(&nn->client_lock);
3428 conf = find_confirmed_client(clid, false, nn);
3429 unconf = find_unconfirmed_client(clid, false, nn);
3431 * We try hard to give out unique clientid's, so if we get an
3432 * attempt to confirm the same clientid with a different cred,
3433 * the client may be buggy; this should never happen.
3435 * Nevertheless, RFC 7530 recommends INUSE for this case:
3437 status = nfserr_clid_inuse;
3438 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3440 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3442 /* cases below refer to rfc 3530 section 14.2.34: */
3443 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
3444 if (conf && same_verf(&confirm, &conf->cl_confirm)) {
3445 /* case 2: probable retransmit */
3447 } else /* case 4: client hasn't noticed we rebooted yet? */
3448 status = nfserr_stale_clientid;
3452 if (conf) { /* case 1: callback update */
3454 unhash_client_locked(old);
3455 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
3456 } else { /* case 3: normal case; new or rebooted client */
3457 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3459 status = nfserr_clid_inuse;
3460 if (client_has_state(old)
3461 && !same_creds(&unconf->cl_cred,
3464 status = mark_client_expired_locked(old);
3470 move_to_confirmed(unconf);
3473 get_client_locked(conf);
3474 spin_unlock(&nn->client_lock);
3475 nfsd4_probe_callback(conf);
3476 spin_lock(&nn->client_lock);
3477 put_client_renew_locked(conf);
3479 spin_unlock(&nn->client_lock);
3485 static struct nfs4_file *nfsd4_alloc_file(void)
3487 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3490 /* OPEN Share state helper functions */
3491 static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
3492 struct nfs4_file *fp)
3494 lockdep_assert_held(&state_lock);
3496 refcount_set(&fp->fi_ref, 1);
3497 spin_lock_init(&fp->fi_lock);
3498 INIT_LIST_HEAD(&fp->fi_stateids);
3499 INIT_LIST_HEAD(&fp->fi_delegations);
3500 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
3501 fh_copy_shallow(&fp->fi_fhandle, fh);
3502 fp->fi_deleg_file = NULL;
3503 fp->fi_had_conflict = false;
3504 fp->fi_share_deny = 0;
3505 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3506 memset(fp->fi_access, 0, sizeof(fp->fi_access));
3507 #ifdef CONFIG_NFSD_PNFS
3508 INIT_LIST_HEAD(&fp->fi_lo_states);
3509 atomic_set(&fp->fi_lo_recalls, 0);
3511 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
3515 nfsd4_free_slabs(void)
3517 kmem_cache_destroy(client_slab);
3518 kmem_cache_destroy(openowner_slab);
3519 kmem_cache_destroy(lockowner_slab);
3520 kmem_cache_destroy(file_slab);
3521 kmem_cache_destroy(stateid_slab);
3522 kmem_cache_destroy(deleg_slab);
3523 kmem_cache_destroy(odstate_slab);
3527 nfsd4_init_slabs(void)
3529 client_slab = kmem_cache_create("nfsd4_clients",
3530 sizeof(struct nfs4_client), 0, 0, NULL);
3531 if (client_slab == NULL)
3533 openowner_slab = kmem_cache_create("nfsd4_openowners",
3534 sizeof(struct nfs4_openowner), 0, 0, NULL);
3535 if (openowner_slab == NULL)
3536 goto out_free_client_slab;
3537 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
3538 sizeof(struct nfs4_lockowner), 0, 0, NULL);
3539 if (lockowner_slab == NULL)
3540 goto out_free_openowner_slab;
3541 file_slab = kmem_cache_create("nfsd4_files",
3542 sizeof(struct nfs4_file), 0, 0, NULL);
3543 if (file_slab == NULL)
3544 goto out_free_lockowner_slab;
3545 stateid_slab = kmem_cache_create("nfsd4_stateids",
3546 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
3547 if (stateid_slab == NULL)
3548 goto out_free_file_slab;
3549 deleg_slab = kmem_cache_create("nfsd4_delegations",
3550 sizeof(struct nfs4_delegation), 0, 0, NULL);
3551 if (deleg_slab == NULL)
3552 goto out_free_stateid_slab;
3553 odstate_slab = kmem_cache_create("nfsd4_odstate",
3554 sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
3555 if (odstate_slab == NULL)
3556 goto out_free_deleg_slab;
3559 out_free_deleg_slab:
3560 kmem_cache_destroy(deleg_slab);
3561 out_free_stateid_slab:
3562 kmem_cache_destroy(stateid_slab);
3564 kmem_cache_destroy(file_slab);
3565 out_free_lockowner_slab:
3566 kmem_cache_destroy(lockowner_slab);
3567 out_free_openowner_slab:
3568 kmem_cache_destroy(openowner_slab);
3569 out_free_client_slab:
3570 kmem_cache_destroy(client_slab);
3572 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3576 static void init_nfs4_replay(struct nfs4_replay *rp)
3578 rp->rp_status = nfserr_serverfault;
3580 rp->rp_buf = rp->rp_ibuf;
3581 mutex_init(&rp->rp_mutex);
3584 static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3585 struct nfs4_stateowner *so)
3587 if (!nfsd4_has_session(cstate)) {
3588 mutex_lock(&so->so_replay.rp_mutex);
3589 cstate->replay_owner = nfs4_get_stateowner(so);
3593 void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3595 struct nfs4_stateowner *so = cstate->replay_owner;
3598 cstate->replay_owner = NULL;
3599 mutex_unlock(&so->so_replay.rp_mutex);
3600 nfs4_put_stateowner(so);
3604 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
3606 struct nfs4_stateowner *sop;
3608 sop = kmem_cache_alloc(slab, GFP_KERNEL);
3612 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3613 if (!sop->so_owner.data) {
3614 kmem_cache_free(slab, sop);
3617 sop->so_owner.len = owner->len;
3619 INIT_LIST_HEAD(&sop->so_stateids);
3620 sop->so_client = clp;
3621 init_nfs4_replay(&sop->so_replay);
3622 atomic_set(&sop->so_count, 1);
3626 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
3628 lockdep_assert_held(&clp->cl_lock);
3630 list_add(&oo->oo_owner.so_strhash,
3631 &clp->cl_ownerstr_hashtbl[strhashval]);
3632 list_add(&oo->oo_perclient, &clp->cl_openowners);
3635 static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3637 unhash_openowner_locked(openowner(so));
3640 static void nfs4_free_openowner(struct nfs4_stateowner *so)
3642 struct nfs4_openowner *oo = openowner(so);
3644 kmem_cache_free(openowner_slab, oo);
3647 static const struct nfs4_stateowner_operations openowner_ops = {
3648 .so_unhash = nfs4_unhash_openowner,
3649 .so_free = nfs4_free_openowner,
3652 static struct nfs4_ol_stateid *
3653 nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
3655 struct nfs4_ol_stateid *local, *ret = NULL;
3656 struct nfs4_openowner *oo = open->op_openowner;
3658 lockdep_assert_held(&fp->fi_lock);
3660 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
3661 /* ignore lock owners */
3662 if (local->st_stateowner->so_is_open_owner == 0)
3664 if (local->st_stateowner != &oo->oo_owner)
3666 if (local->st_stid.sc_type == NFS4_OPEN_STID) {
3668 refcount_inc(&ret->st_stid.sc_count);
3676 nfsd4_verify_open_stid(struct nfs4_stid *s)
3678 __be32 ret = nfs_ok;
3680 switch (s->sc_type) {
3684 case NFS4_CLOSED_STID:
3685 case NFS4_CLOSED_DELEG_STID:
3686 ret = nfserr_bad_stateid;
3688 case NFS4_REVOKED_DELEG_STID:
3689 ret = nfserr_deleg_revoked;
3694 /* Lock the stateid st_mutex, and deal with races with CLOSE */
3696 nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp)
3700 mutex_lock_nested(&stp->st_mutex, LOCK_STATEID_MUTEX);
3701 ret = nfsd4_verify_open_stid(&stp->st_stid);
3703 mutex_unlock(&stp->st_mutex);
3707 static struct nfs4_ol_stateid *
3708 nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
3710 struct nfs4_ol_stateid *stp;
3712 spin_lock(&fp->fi_lock);
3713 stp = nfsd4_find_existing_open(fp, open);
3714 spin_unlock(&fp->fi_lock);
3715 if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok)
3717 nfs4_put_stid(&stp->st_stid);
3722 static struct nfs4_openowner *
3723 alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
3724 struct nfsd4_compound_state *cstate)
3726 struct nfs4_client *clp = cstate->clp;
3727 struct nfs4_openowner *oo, *ret;
3729 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3732 oo->oo_owner.so_ops = &openowner_ops;
3733 oo->oo_owner.so_is_open_owner = 1;
3734 oo->oo_owner.so_seqid = open->op_seqid;
3736 if (nfsd4_has_session(cstate))
3737 oo->oo_flags |= NFS4_OO_CONFIRMED;
3739 oo->oo_last_closed_stid = NULL;
3740 INIT_LIST_HEAD(&oo->oo_close_lru);
3741 spin_lock(&clp->cl_lock);
3742 ret = find_openstateowner_str_locked(strhashval, open, clp);
3744 hash_openowner(oo, clp, strhashval);
3747 nfs4_free_stateowner(&oo->oo_owner);
3749 spin_unlock(&clp->cl_lock);
3753 static struct nfs4_ol_stateid *
3754 init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
3757 struct nfs4_openowner *oo = open->op_openowner;
3758 struct nfs4_ol_stateid *retstp = NULL;
3759 struct nfs4_ol_stateid *stp;
3762 /* We are moving these outside of the spinlocks to avoid the warnings */
3763 mutex_init(&stp->st_mutex);
3764 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
3767 spin_lock(&oo->oo_owner.so_client->cl_lock);
3768 spin_lock(&fp->fi_lock);
3770 retstp = nfsd4_find_existing_open(fp, open);
3774 open->op_stp = NULL;
3775 refcount_inc(&stp->st_stid.sc_count);
3776 stp->st_stid.sc_type = NFS4_OPEN_STID;
3777 INIT_LIST_HEAD(&stp->st_locks);
3778 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
3780 stp->st_stid.sc_file = fp;
3781 stp->st_access_bmap = 0;
3782 stp->st_deny_bmap = 0;
3783 stp->st_openstp = NULL;
3784 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
3785 list_add(&stp->st_perfile, &fp->fi_stateids);
3788 spin_unlock(&fp->fi_lock);
3789 spin_unlock(&oo->oo_owner.so_client->cl_lock);
3791 /* Handle races with CLOSE */
3792 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
3793 nfs4_put_stid(&retstp->st_stid);
3796 /* To keep mutex tracking happy */
3797 mutex_unlock(&stp->st_mutex);
3804 * In the 4.0 case we need to keep the owners around a little while to handle
3805 * CLOSE replay. We still do need to release any file access that is held by
3806 * them before returning however.
3809 move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
3811 struct nfs4_ol_stateid *last;
3812 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3813 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3816 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
3819 * We know that we hold one reference via nfsd4_close, and another
3820 * "persistent" reference for the client. If the refcount is higher
3821 * than 2, then there are still calls in progress that are using this
3822 * stateid. We can't put the sc_file reference until they are finished.
3823 * Wait for the refcount to drop to 2. Since it has been unhashed,
3824 * there should be no danger of the refcount going back up again at
3827 wait_event(close_wq, refcount_read(&s->st_stid.sc_count) == 2);
3829 release_all_access(s);
3830 if (s->st_stid.sc_file) {
3831 put_nfs4_file(s->st_stid.sc_file);
3832 s->st_stid.sc_file = NULL;
3835 spin_lock(&nn->client_lock);
3836 last = oo->oo_last_closed_stid;
3837 oo->oo_last_closed_stid = s;
3838 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
3839 oo->oo_time = get_seconds();
3840 spin_unlock(&nn->client_lock);
3842 nfs4_put_stid(&last->st_stid);
3845 /* search file_hashtbl[] for file */
3846 static struct nfs4_file *
3847 find_file_locked(struct knfsd_fh *fh, unsigned int hashval)
3849 struct nfs4_file *fp;
3851 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash) {
3852 if (fh_match(&fp->fi_fhandle, fh)) {
3853 if (refcount_inc_not_zero(&fp->fi_ref))
3861 find_file(struct knfsd_fh *fh)
3863 struct nfs4_file *fp;
3864 unsigned int hashval = file_hashval(fh);
3867 fp = find_file_locked(fh, hashval);
3872 static struct nfs4_file *
3873 find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
3875 struct nfs4_file *fp;
3876 unsigned int hashval = file_hashval(fh);
3879 fp = find_file_locked(fh, hashval);
3884 spin_lock(&state_lock);
3885 fp = find_file_locked(fh, hashval);
3886 if (likely(fp == NULL)) {
3887 nfsd4_init_file(fh, hashval, new);
3890 spin_unlock(&state_lock);
3896 * Called to check deny when READ with all zero stateid or
3897 * WRITE with all zero or all one stateid
3900 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3902 struct nfs4_file *fp;
3903 __be32 ret = nfs_ok;
3905 fp = find_file(¤t_fh->fh_handle);
3908 /* Check for conflicting share reservations */
3909 spin_lock(&fp->fi_lock);
3910 if (fp->fi_share_deny & deny_type)
3911 ret = nfserr_locked;
3912 spin_unlock(&fp->fi_lock);
3917 static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
3919 struct nfs4_delegation *dp = cb_to_delegation(cb);
3920 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3923 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
3926 * We can't do this in nfsd_break_deleg_cb because it is
3927 * already holding inode->i_lock.
3929 * If the dl_time != 0, then we know that it has already been
3930 * queued for a lease break. Don't queue it again.
3932 spin_lock(&state_lock);
3933 if (dp->dl_time == 0) {
3934 dp->dl_time = get_seconds();
3935 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
3937 spin_unlock(&state_lock);
3940 static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
3941 struct rpc_task *task)
3943 struct nfs4_delegation *dp = cb_to_delegation(cb);
3945 if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID)
3948 switch (task->tk_status) {
3952 case -NFS4ERR_BAD_STATEID:
3954 * Race: client probably got cb_recall before open reply
3955 * granting delegation.
3957 if (dp->dl_retries--) {
3958 rpc_delay(task, 2 * HZ);
3967 static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
3969 struct nfs4_delegation *dp = cb_to_delegation(cb);
3971 nfs4_put_stid(&dp->dl_stid);
3974 static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
3975 .prepare = nfsd4_cb_recall_prepare,
3976 .done = nfsd4_cb_recall_done,
3977 .release = nfsd4_cb_recall_release,
3980 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3983 * We're assuming the state code never drops its reference
3984 * without first removing the lease. Since we're in this lease
3985 * callback (and since the lease code is serialized by the
3986 * i_lock) we know the server hasn't removed the lease yet, and
3987 * we know it's safe to take a reference.
3989 refcount_inc(&dp->dl_stid.sc_count);
3990 nfsd4_run_cb(&dp->dl_recall);
3993 /* Called from break_lease() with i_lock held. */
3995 nfsd_break_deleg_cb(struct file_lock *fl)
3998 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
3999 struct nfs4_file *fp = dp->dl_stid.sc_file;
4002 * We don't want the locks code to timeout the lease for us;
4003 * we'll remove it ourself if a delegation isn't returned
4006 fl->fl_break_time = 0;
4008 spin_lock(&fp->fi_lock);
4009 fp->fi_had_conflict = true;
4010 nfsd_break_one_deleg(dp);
4011 spin_unlock(&fp->fi_lock);
4016 nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
4017 struct list_head *dispose)
4020 return lease_modify(onlist, arg, dispose);
4025 static const struct lock_manager_operations nfsd_lease_mng_ops = {
4026 .lm_break = nfsd_break_deleg_cb,
4027 .lm_change = nfsd_change_deleg_cb,
4030 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
4032 if (nfsd4_has_session(cstate))
4034 if (seqid == so->so_seqid - 1)
4035 return nfserr_replay_me;
4036 if (seqid == so->so_seqid)
4038 return nfserr_bad_seqid;
4041 static __be32 lookup_clientid(clientid_t *clid,
4042 struct nfsd4_compound_state *cstate,
4043 struct nfsd_net *nn)
4045 struct nfs4_client *found;
4048 found = cstate->clp;
4049 if (!same_clid(&found->cl_clientid, clid))
4050 return nfserr_stale_clientid;
4054 if (STALE_CLIENTID(clid, nn))
4055 return nfserr_stale_clientid;
4058 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
4059 * cached already then we know this is for is for v4.0 and "sessions"
4062 WARN_ON_ONCE(cstate->session);
4063 spin_lock(&nn->client_lock);
4064 found = find_confirmed_client(clid, false, nn);
4066 spin_unlock(&nn->client_lock);
4067 return nfserr_expired;
4069 atomic_inc(&found->cl_refcount);
4070 spin_unlock(&nn->client_lock);
4072 /* Cache the nfs4_client in cstate! */
4073 cstate->clp = found;
4078 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
4079 struct nfsd4_open *open, struct nfsd_net *nn)
4081 clientid_t *clientid = &open->op_clientid;
4082 struct nfs4_client *clp = NULL;
4083 unsigned int strhashval;
4084 struct nfs4_openowner *oo = NULL;
4087 if (STALE_CLIENTID(&open->op_clientid, nn))
4088 return nfserr_stale_clientid;
4090 * In case we need it later, after we've already created the
4091 * file and don't want to risk a further failure:
4093 open->op_file = nfsd4_alloc_file();
4094 if (open->op_file == NULL)
4095 return nfserr_jukebox;
4097 status = lookup_clientid(clientid, cstate, nn);
4102 strhashval = ownerstr_hashval(&open->op_owner);
4103 oo = find_openstateowner_str(strhashval, open, clp);
4104 open->op_openowner = oo;
4108 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4109 /* Replace unconfirmed owners without checking for replay. */
4110 release_openowner(oo);
4111 open->op_openowner = NULL;
4114 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
4119 oo = alloc_init_open_stateowner(strhashval, open, cstate);
4121 return nfserr_jukebox;
4122 open->op_openowner = oo;
4124 open->op_stp = nfs4_alloc_open_stateid(clp);
4126 return nfserr_jukebox;
4128 if (nfsd4_has_session(cstate) &&
4129 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
4130 open->op_odstate = alloc_clnt_odstate(clp);
4131 if (!open->op_odstate)
4132 return nfserr_jukebox;
4138 static inline __be32
4139 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
4141 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
4142 return nfserr_openmode;
4147 static int share_access_to_flags(u32 share_access)
4149 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
4152 static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
4154 struct nfs4_stid *ret;
4156 ret = find_stateid_by_type(cl, s,
4157 NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID);
4160 return delegstateid(ret);
4163 static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
4165 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
4166 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
4170 nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
4171 struct nfs4_delegation **dp)
4174 __be32 status = nfserr_bad_stateid;
4175 struct nfs4_delegation *deleg;
4177 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
4180 if (deleg->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) {
4181 nfs4_put_stid(&deleg->dl_stid);
4182 if (cl->cl_minorversion)
4183 status = nfserr_deleg_revoked;
4186 flags = share_access_to_flags(open->op_share_access);
4187 status = nfs4_check_delegmode(deleg, flags);
4189 nfs4_put_stid(&deleg->dl_stid);
4194 if (!nfsd4_is_deleg_cur(open))
4198 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
4202 static inline int nfs4_access_to_access(u32 nfs4_access)
4206 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
4207 flags |= NFSD_MAY_READ;
4208 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
4209 flags |= NFSD_MAY_WRITE;
4213 static inline __be32
4214 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
4215 struct nfsd4_open *open)
4217 struct iattr iattr = {
4218 .ia_valid = ATTR_SIZE,
4221 if (!open->op_truncate)
4223 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
4224 return nfserr_inval;
4225 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
4228 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
4229 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
4230 struct nfsd4_open *open)
4232 struct file *filp = NULL;
4234 int oflag = nfs4_access_to_omode(open->op_share_access);
4235 int access = nfs4_access_to_access(open->op_share_access);
4236 unsigned char old_access_bmap, old_deny_bmap;
4238 spin_lock(&fp->fi_lock);
4241 * Are we trying to set a deny mode that would conflict with
4244 status = nfs4_file_check_deny(fp, open->op_share_deny);
4245 if (status != nfs_ok) {
4246 spin_unlock(&fp->fi_lock);
4250 /* set access to the file */
4251 status = nfs4_file_get_access(fp, open->op_share_access);
4252 if (status != nfs_ok) {
4253 spin_unlock(&fp->fi_lock);
4257 /* Set access bits in stateid */
4258 old_access_bmap = stp->st_access_bmap;
4259 set_access(open->op_share_access, stp);
4261 /* Set new deny mask */
4262 old_deny_bmap = stp->st_deny_bmap;
4263 set_deny(open->op_share_deny, stp);
4264 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4266 if (!fp->fi_fds[oflag]) {
4267 spin_unlock(&fp->fi_lock);
4268 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
4270 goto out_put_access;
4271 spin_lock(&fp->fi_lock);
4272 if (!fp->fi_fds[oflag]) {
4273 fp->fi_fds[oflag] = filp;
4277 spin_unlock(&fp->fi_lock);
4281 status = nfsd4_truncate(rqstp, cur_fh, open);
4283 goto out_put_access;
4287 stp->st_access_bmap = old_access_bmap;
4288 nfs4_file_put_access(fp, open->op_share_access);
4289 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
4294 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)
4297 unsigned char old_deny_bmap = stp->st_deny_bmap;
4299 if (!test_access(open->op_share_access, stp))
4300 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
4302 /* test and set deny mode */
4303 spin_lock(&fp->fi_lock);
4304 status = nfs4_file_check_deny(fp, open->op_share_deny);
4305 if (status == nfs_ok) {
4306 set_deny(open->op_share_deny, stp);
4307 fp->fi_share_deny |=
4308 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4310 spin_unlock(&fp->fi_lock);
4312 if (status != nfs_ok)
4315 status = nfsd4_truncate(rqstp, cur_fh, open);
4316 if (status != nfs_ok)
4317 reset_union_bmap_deny(old_deny_bmap, stp);
4321 /* Should we give out recallable state?: */
4322 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
4324 if (clp->cl_cb_state == NFSD4_CB_UP)
4327 * In the sessions case, since we don't have to establish a
4328 * separate connection for callbacks, we assume it's OK
4329 * until we hear otherwise:
4331 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
4334 static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp,
4337 struct file_lock *fl;
4339 fl = locks_alloc_lock();
4342 fl->fl_lmops = &nfsd_lease_mng_ops;
4343 fl->fl_flags = FL_DELEG;
4344 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
4345 fl->fl_end = OFFSET_MAX;
4346 fl->fl_owner = (fl_owner_t)dp;
4347 fl->fl_pid = current->tgid;
4348 fl->fl_file = dp->dl_stid.sc_file->fi_deleg_file;
4352 static struct nfs4_delegation *
4353 nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
4354 struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate)
4357 struct nfs4_delegation *dp;
4359 struct file_lock *fl;
4362 * The fi_had_conflict and nfs_get_existing_delegation checks
4363 * here are just optimizations; we'll need to recheck them at
4366 if (fp->fi_had_conflict)
4367 return ERR_PTR(-EAGAIN);
4369 filp = find_readable_file(fp);
4371 /* We should always have a readable file here */
4373 return ERR_PTR(-EBADF);
4375 spin_lock(&state_lock);
4376 spin_lock(&fp->fi_lock);
4377 if (nfs4_delegation_exists(clp, fp))
4379 else if (!fp->fi_deleg_file) {
4380 fp->fi_deleg_file = filp;
4381 /* increment early to prevent fi_deleg_file from being
4383 fp->fi_delegees = 1;
4387 spin_unlock(&fp->fi_lock);
4388 spin_unlock(&state_lock);
4392 return ERR_PTR(status);
4395 dp = alloc_init_deleg(clp, fp, fh, odstate);
4399 fl = nfs4_alloc_init_lease(dp, NFS4_OPEN_DELEGATE_READ);
4401 goto out_clnt_odstate;
4403 status = vfs_setlease(fp->fi_deleg_file, fl->fl_type, &fl, NULL);
4405 locks_free_lock(fl);
4407 goto out_clnt_odstate;
4409 spin_lock(&state_lock);
4410 spin_lock(&fp->fi_lock);
4411 if (fp->fi_had_conflict)
4414 status = hash_delegation_locked(dp, fp);
4415 spin_unlock(&fp->fi_lock);
4416 spin_unlock(&state_lock);
4423 vfs_setlease(fp->fi_deleg_file, F_UNLCK, NULL, (void **)&dp);
4425 put_clnt_odstate(dp->dl_clnt_odstate);
4426 nfs4_put_stid(&dp->dl_stid);
4429 return ERR_PTR(status);
4432 static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
4434 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4435 if (status == -EAGAIN)
4436 open->op_why_no_deleg = WND4_CONTENTION;
4438 open->op_why_no_deleg = WND4_RESOURCE;
4439 switch (open->op_deleg_want) {
4440 case NFS4_SHARE_WANT_READ_DELEG:
4441 case NFS4_SHARE_WANT_WRITE_DELEG:
4442 case NFS4_SHARE_WANT_ANY_DELEG:
4444 case NFS4_SHARE_WANT_CANCEL:
4445 open->op_why_no_deleg = WND4_CANCELLED;
4447 case NFS4_SHARE_WANT_NO_DELEG:
4454 * Attempt to hand out a delegation.
4456 * Note we don't support write delegations, and won't until the vfs has
4457 * proper support for them.
4460 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
4461 struct nfs4_ol_stateid *stp)
4463 struct nfs4_delegation *dp;
4464 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
4465 struct nfs4_client *clp = stp->st_stid.sc_client;
4469 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
4470 open->op_recall = 0;
4471 switch (open->op_claim_type) {
4472 case NFS4_OPEN_CLAIM_PREVIOUS:
4474 open->op_recall = 1;
4475 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
4478 case NFS4_OPEN_CLAIM_NULL:
4479 case NFS4_OPEN_CLAIM_FH:
4481 * Let's not give out any delegations till everyone's
4482 * had the chance to reclaim theirs, *and* until
4483 * NLM locks have all been reclaimed:
4485 if (locks_in_grace(clp->net))
4487 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
4490 * Also, if the file was opened for write or
4491 * create, there's a good chance the client's
4492 * about to write to it, resulting in an
4493 * immediate recall (since we don't support
4494 * write delegations):
4496 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
4498 if (open->op_create == NFS4_OPEN_CREATE)
4504 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate);
4508 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
4510 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
4511 STATEID_VAL(&dp->dl_stid.sc_stateid));
4512 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
4513 nfs4_put_stid(&dp->dl_stid);
4516 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
4517 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
4518 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
4519 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
4520 open->op_recall = 1;
4523 /* 4.1 client asking for a delegation? */
4524 if (open->op_deleg_want)
4525 nfsd4_open_deleg_none_ext(open, status);
4529 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
4530 struct nfs4_delegation *dp)
4532 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
4533 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4534 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4535 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
4536 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
4537 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4538 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4539 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
4541 /* Otherwise the client must be confused wanting a delegation
4542 * it already has, therefore we don't return
4543 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
4548 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
4550 struct nfsd4_compoundres *resp = rqstp->rq_resp;
4551 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
4552 struct nfs4_file *fp = NULL;
4553 struct nfs4_ol_stateid *stp = NULL;
4554 struct nfs4_delegation *dp = NULL;
4556 bool new_stp = false;
4559 * Lookup file; if found, lookup stateid and check open request,
4560 * and check for delegations in the process of being recalled.
4561 * If not found, create the nfs4_file struct
4563 fp = find_or_add_file(open->op_file, ¤t_fh->fh_handle);
4564 if (fp != open->op_file) {
4565 status = nfs4_check_deleg(cl, open, &dp);
4568 stp = nfsd4_find_and_lock_existing_open(fp, open);
4570 open->op_file = NULL;
4571 status = nfserr_bad_stateid;
4572 if (nfsd4_is_deleg_cur(open))
4577 stp = init_open_stateid(fp, open);
4583 * OPEN the file, or upgrade an existing OPEN.
4584 * If truncate fails, the OPEN fails.
4586 * stp is already locked.
4589 /* Stateid was found, this is an OPEN upgrade */
4590 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
4592 mutex_unlock(&stp->st_mutex);
4596 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4598 stp->st_stid.sc_type = NFS4_CLOSED_STID;
4599 release_open_stateid(stp);
4600 mutex_unlock(&stp->st_mutex);
4604 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
4606 if (stp->st_clnt_odstate == open->op_odstate)
4607 open->op_odstate = NULL;
4610 nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
4611 mutex_unlock(&stp->st_mutex);
4613 if (nfsd4_has_session(&resp->cstate)) {
4614 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4615 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4616 open->op_why_no_deleg = WND4_NOT_WANTED;
4622 * Attempt to hand out a delegation. No error return, because the
4623 * OPEN succeeds even if we fail.
4625 nfs4_open_delegation(current_fh, open, stp);
4629 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
4630 STATEID_VAL(&stp->st_stid.sc_stateid));
4632 /* 4.1 client trying to upgrade/downgrade delegation? */
4633 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
4634 open->op_deleg_want)
4635 nfsd4_deleg_xgrade_none_ext(open, dp);
4639 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
4640 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
4642 * To finish the open response, we just need to set the rflags.
4644 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
4645 if (nfsd4_has_session(&resp->cstate))
4646 open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK;
4647 else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED))
4648 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
4651 nfs4_put_stid(&dp->dl_stid);
4653 nfs4_put_stid(&stp->st_stid);
4658 void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
4659 struct nfsd4_open *open)
4661 if (open->op_openowner) {
4662 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
4664 nfsd4_cstate_assign_replay(cstate, so);
4665 nfs4_put_stateowner(so);
4668 kmem_cache_free(file_slab, open->op_file);
4670 nfs4_put_stid(&open->op_stp->st_stid);
4671 if (open->op_odstate)
4672 kmem_cache_free(odstate_slab, open->op_odstate);
4676 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4677 union nfsd4_op_u *u)
4679 clientid_t *clid = &u->renew;
4680 struct nfs4_client *clp;
4682 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4684 dprintk("process_renew(%08x/%08x): starting\n",
4685 clid->cl_boot, clid->cl_id);
4686 status = lookup_clientid(clid, cstate, nn);
4690 status = nfserr_cb_path_down;
4691 if (!list_empty(&clp->cl_delegations)
4692 && clp->cl_cb_state != NFSD4_CB_UP)
4700 nfsd4_end_grace(struct nfsd_net *nn)
4702 /* do nothing if grace period already ended */
4703 if (nn->grace_ended)
4706 dprintk("NFSD: end of grace period\n");
4707 nn->grace_ended = true;
4709 * If the server goes down again right now, an NFSv4
4710 * client will still be allowed to reclaim after it comes back up,
4711 * even if it hasn't yet had a chance to reclaim state this time.
4714 nfsd4_record_grace_done(nn);
4716 * At this point, NFSv4 clients can still reclaim. But if the
4717 * server crashes, any that have not yet reclaimed will be out
4718 * of luck on the next boot.
4720 * (NFSv4.1+ clients are considered to have reclaimed once they
4721 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
4722 * have reclaimed after their first OPEN.)
4724 locks_end_grace(&nn->nfsd4_manager);
4726 * At this point, and once lockd and/or any other containers
4727 * exit their grace period, further reclaims will fail and
4728 * regular locking can resume.
4733 * If we've waited a lease period but there are still clients trying to
4734 * reclaim, wait a little longer to give them a chance to finish.
4736 static bool clients_still_reclaiming(struct nfsd_net *nn)
4738 unsigned long now = get_seconds();
4739 unsigned long double_grace_period_end = nn->boot_time +
4740 2 * nn->nfsd4_lease;
4742 if (!nn->somebody_reclaimed)
4744 nn->somebody_reclaimed = false;
4746 * If we've given them *two* lease times to reclaim, and they're
4747 * still not done, give up:
4749 if (time_after(now, double_grace_period_end))
4755 nfs4_laundromat(struct nfsd_net *nn)
4757 struct nfs4_client *clp;
4758 struct nfs4_openowner *oo;
4759 struct nfs4_delegation *dp;
4760 struct nfs4_ol_stateid *stp;
4761 struct nfsd4_blocked_lock *nbl;
4762 struct list_head *pos, *next, reaplist;
4763 time_t cutoff = get_seconds() - nn->nfsd4_lease;
4764 time_t t, new_timeo = nn->nfsd4_lease;
4766 dprintk("NFSD: laundromat service - starting\n");
4768 if (clients_still_reclaiming(nn)) {
4772 nfsd4_end_grace(nn);
4773 INIT_LIST_HEAD(&reaplist);
4774 spin_lock(&nn->client_lock);
4775 list_for_each_safe(pos, next, &nn->client_lru) {
4776 clp = list_entry(pos, struct nfs4_client, cl_lru);
4777 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4778 t = clp->cl_time - cutoff;
4779 new_timeo = min(new_timeo, t);
4782 if (mark_client_expired_locked(clp)) {
4783 dprintk("NFSD: client in use (clientid %08x)\n",
4784 clp->cl_clientid.cl_id);
4787 list_add(&clp->cl_lru, &reaplist);
4789 spin_unlock(&nn->client_lock);
4790 list_for_each_safe(pos, next, &reaplist) {
4791 clp = list_entry(pos, struct nfs4_client, cl_lru);
4792 dprintk("NFSD: purging unused client (clientid %08x)\n",
4793 clp->cl_clientid.cl_id);
4794 list_del_init(&clp->cl_lru);
4797 spin_lock(&state_lock);
4798 list_for_each_safe(pos, next, &nn->del_recall_lru) {
4799 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4800 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
4801 t = dp->dl_time - cutoff;
4802 new_timeo = min(new_timeo, t);
4805 WARN_ON(!unhash_delegation_locked(dp));
4806 list_add(&dp->dl_recall_lru, &reaplist);
4808 spin_unlock(&state_lock);
4809 while (!list_empty(&reaplist)) {
4810 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4812 list_del_init(&dp->dl_recall_lru);
4813 revoke_delegation(dp);
4816 spin_lock(&nn->client_lock);
4817 while (!list_empty(&nn->close_lru)) {
4818 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
4820 if (time_after((unsigned long)oo->oo_time,
4821 (unsigned long)cutoff)) {
4822 t = oo->oo_time - cutoff;
4823 new_timeo = min(new_timeo, t);
4826 list_del_init(&oo->oo_close_lru);
4827 stp = oo->oo_last_closed_stid;
4828 oo->oo_last_closed_stid = NULL;
4829 spin_unlock(&nn->client_lock);
4830 nfs4_put_stid(&stp->st_stid);
4831 spin_lock(&nn->client_lock);
4833 spin_unlock(&nn->client_lock);
4836 * It's possible for a client to try and acquire an already held lock
4837 * that is being held for a long time, and then lose interest in it.
4838 * So, we clean out any un-revisited request after a lease period
4839 * under the assumption that the client is no longer interested.
4841 * RFC5661, sec. 9.6 states that the client must not rely on getting
4842 * notifications and must continue to poll for locks, even when the
4843 * server supports them. Thus this shouldn't lead to clients blocking
4844 * indefinitely once the lock does become free.
4846 BUG_ON(!list_empty(&reaplist));
4847 spin_lock(&nn->blocked_locks_lock);
4848 while (!list_empty(&nn->blocked_locks_lru)) {
4849 nbl = list_first_entry(&nn->blocked_locks_lru,
4850 struct nfsd4_blocked_lock, nbl_lru);
4851 if (time_after((unsigned long)nbl->nbl_time,
4852 (unsigned long)cutoff)) {
4853 t = nbl->nbl_time - cutoff;
4854 new_timeo = min(new_timeo, t);
4857 list_move(&nbl->nbl_lru, &reaplist);
4858 list_del_init(&nbl->nbl_list);
4860 spin_unlock(&nn->blocked_locks_lock);
4862 while (!list_empty(&reaplist)) {
4863 nbl = list_first_entry(&reaplist,
4864 struct nfsd4_blocked_lock, nbl_lru);
4865 list_del_init(&nbl->nbl_lru);
4866 locks_delete_block(&nbl->nbl_lock);
4867 free_blocked_lock(nbl);
4870 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
4874 static struct workqueue_struct *laundry_wq;
4875 static void laundromat_main(struct work_struct *);
4878 laundromat_main(struct work_struct *laundry)
4881 struct delayed_work *dwork = to_delayed_work(laundry);
4882 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4885 t = nfs4_laundromat(nn);
4886 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
4887 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
4890 static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
4892 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
4893 return nfserr_bad_stateid;
4898 access_permit_read(struct nfs4_ol_stateid *stp)
4900 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4901 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4902 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
4906 access_permit_write(struct nfs4_ol_stateid *stp)
4908 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4909 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
4913 __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
4915 __be32 status = nfserr_openmode;
4917 /* For lock stateid's, we test the parent open, not the lock: */
4918 if (stp->st_openstp)
4919 stp = stp->st_openstp;
4920 if ((flags & WR_STATE) && !access_permit_write(stp))
4922 if ((flags & RD_STATE) && !access_permit_read(stp))
4929 static inline __be32
4930 check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
4932 if (ONE_STATEID(stateid) && (flags & RD_STATE))
4934 else if (opens_in_grace(net)) {
4935 /* Answer in remaining cases depends on existence of
4936 * conflicting state; so we must wait out the grace period. */
4937 return nfserr_grace;
4938 } else if (flags & WR_STATE)
4939 return nfs4_share_conflict(current_fh,
4940 NFS4_SHARE_DENY_WRITE);
4941 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4942 return nfs4_share_conflict(current_fh,
4943 NFS4_SHARE_DENY_READ);
4947 * Allow READ/WRITE during grace period on recovered state only for files
4948 * that are not able to provide mandatory locking.
4951 grace_disallows_io(struct net *net, struct inode *inode)
4953 return opens_in_grace(net) && mandatory_lock(inode);
4956 static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
4959 * When sessions are used the stateid generation number is ignored
4962 if (has_session && in->si_generation == 0)
4965 if (in->si_generation == ref->si_generation)
4968 /* If the client sends us a stateid from the future, it's buggy: */
4969 if (nfsd4_stateid_generation_after(in, ref))
4970 return nfserr_bad_stateid;
4972 * However, we could see a stateid from the past, even from a
4973 * non-buggy client. For example, if the client sends a lock
4974 * while some IO is outstanding, the lock may bump si_generation
4975 * while the IO is still in flight. The client could avoid that
4976 * situation by waiting for responses on all the IO requests,
4977 * but better performance may result in retrying IO that
4978 * receives an old_stateid error if requests are rarely
4979 * reordered in flight:
4981 return nfserr_old_stateid;
4984 static __be32 nfsd4_stid_check_stateid_generation(stateid_t *in, struct nfs4_stid *s, bool has_session)
4988 spin_lock(&s->sc_lock);
4989 ret = nfsd4_verify_open_stid(s);
4991 ret = check_stateid_generation(in, &s->sc_stateid, has_session);
4992 spin_unlock(&s->sc_lock);
4996 static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
4998 if (ols->st_stateowner->so_is_open_owner &&
4999 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
5000 return nfserr_bad_stateid;
5004 static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
5006 struct nfs4_stid *s;
5007 __be32 status = nfserr_bad_stateid;
5009 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5010 CLOSE_STATEID(stateid))
5012 /* Client debugging aid. */
5013 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
5014 char addr_str[INET6_ADDRSTRLEN];
5015 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
5017 pr_warn_ratelimited("NFSD: client %s testing state ID "
5018 "with incorrect client ID\n", addr_str);
5021 spin_lock(&cl->cl_lock);
5022 s = find_stateid_locked(cl, stateid);
5025 status = nfsd4_stid_check_stateid_generation(stateid, s, 1);
5028 switch (s->sc_type) {
5029 case NFS4_DELEG_STID:
5032 case NFS4_REVOKED_DELEG_STID:
5033 status = nfserr_deleg_revoked;
5035 case NFS4_OPEN_STID:
5036 case NFS4_LOCK_STID:
5037 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
5040 printk("unknown stateid type %x\n", s->sc_type);
5042 case NFS4_CLOSED_STID:
5043 case NFS4_CLOSED_DELEG_STID:
5044 status = nfserr_bad_stateid;
5047 spin_unlock(&cl->cl_lock);
5052 nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
5053 stateid_t *stateid, unsigned char typemask,
5054 struct nfs4_stid **s, struct nfsd_net *nn)
5057 bool return_revoked = false;
5060 * only return revoked delegations if explicitly asked.
5061 * otherwise we report revoked or bad_stateid status.
5063 if (typemask & NFS4_REVOKED_DELEG_STID)
5064 return_revoked = true;
5065 else if (typemask & NFS4_DELEG_STID)
5066 typemask |= NFS4_REVOKED_DELEG_STID;
5068 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5069 CLOSE_STATEID(stateid))
5070 return nfserr_bad_stateid;
5071 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
5072 if (status == nfserr_stale_clientid) {
5073 if (cstate->session)
5074 return nfserr_bad_stateid;
5075 return nfserr_stale_stateid;
5079 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
5081 return nfserr_bad_stateid;
5082 if (((*s)->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) {
5084 if (cstate->minorversion)
5085 return nfserr_deleg_revoked;
5086 return nfserr_bad_stateid;
5091 static struct file *
5092 nfs4_find_file(struct nfs4_stid *s, int flags)
5097 switch (s->sc_type) {
5098 case NFS4_DELEG_STID:
5099 if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
5101 return get_file(s->sc_file->fi_deleg_file);
5102 case NFS4_OPEN_STID:
5103 case NFS4_LOCK_STID:
5104 if (flags & RD_STATE)
5105 return find_readable_file(s->sc_file);
5107 return find_writeable_file(s->sc_file);
5115 nfs4_check_olstateid(struct nfs4_ol_stateid *ols, int flags)
5119 status = nfsd4_check_openowner_confirmed(ols);
5122 return nfs4_check_openmode(ols, flags);
5126 nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
5127 struct file **filpp, bool *tmp_file, int flags)
5129 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
5133 file = nfs4_find_file(s, flags);
5135 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
5136 acc | NFSD_MAY_OWNER_OVERRIDE);
5144 status = nfsd_open(rqstp, fhp, S_IFREG, acc, filpp);
5156 * Checks for stateid operations
5159 nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
5160 struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
5161 stateid_t *stateid, int flags, struct file **filpp, bool *tmp_file)
5163 struct inode *ino = d_inode(fhp->fh_dentry);
5164 struct net *net = SVC_NET(rqstp);
5165 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5166 struct nfs4_stid *s = NULL;
5174 if (grace_disallows_io(net, ino))
5175 return nfserr_grace;
5177 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
5178 status = check_special_stateids(net, fhp, stateid, flags);
5182 status = nfsd4_lookup_stateid(cstate, stateid,
5183 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
5187 status = nfsd4_stid_check_stateid_generation(stateid, s,
5188 nfsd4_has_session(cstate));
5192 switch (s->sc_type) {
5193 case NFS4_DELEG_STID:
5194 status = nfs4_check_delegmode(delegstateid(s), flags);
5196 case NFS4_OPEN_STID:
5197 case NFS4_LOCK_STID:
5198 status = nfs4_check_olstateid(openlockstateid(s), flags);
5201 status = nfserr_bad_stateid;
5206 status = nfs4_check_fh(fhp, s);
5209 if (!status && filpp)
5210 status = nfs4_check_file(rqstp, fhp, s, filpp, tmp_file, flags);
5218 * Test if the stateid is valid
5221 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5222 union nfsd4_op_u *u)
5224 struct nfsd4_test_stateid *test_stateid = &u->test_stateid;
5225 struct nfsd4_test_stateid_id *stateid;
5226 struct nfs4_client *cl = cstate->session->se_client;
5228 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
5229 stateid->ts_id_status =
5230 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
5236 nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
5238 struct nfs4_ol_stateid *stp = openlockstateid(s);
5241 ret = nfsd4_lock_ol_stateid(stp);
5245 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5249 ret = nfserr_locks_held;
5250 if (check_for_locks(stp->st_stid.sc_file,
5251 lockowner(stp->st_stateowner)))
5254 release_lock_stateid(stp);
5258 mutex_unlock(&stp->st_mutex);
5265 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5266 union nfsd4_op_u *u)
5268 struct nfsd4_free_stateid *free_stateid = &u->free_stateid;
5269 stateid_t *stateid = &free_stateid->fr_stateid;
5270 struct nfs4_stid *s;
5271 struct nfs4_delegation *dp;
5272 struct nfs4_client *cl = cstate->session->se_client;
5273 __be32 ret = nfserr_bad_stateid;
5275 spin_lock(&cl->cl_lock);
5276 s = find_stateid_locked(cl, stateid);
5279 spin_lock(&s->sc_lock);
5280 switch (s->sc_type) {
5281 case NFS4_DELEG_STID:
5282 ret = nfserr_locks_held;
5284 case NFS4_OPEN_STID:
5285 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5288 ret = nfserr_locks_held;
5290 case NFS4_LOCK_STID:
5291 spin_unlock(&s->sc_lock);
5292 refcount_inc(&s->sc_count);
5293 spin_unlock(&cl->cl_lock);
5294 ret = nfsd4_free_lock_stateid(stateid, s);
5296 case NFS4_REVOKED_DELEG_STID:
5297 spin_unlock(&s->sc_lock);
5298 dp = delegstateid(s);
5299 list_del_init(&dp->dl_recall_lru);
5300 spin_unlock(&cl->cl_lock);
5304 /* Default falls through and returns nfserr_bad_stateid */
5306 spin_unlock(&s->sc_lock);
5308 spin_unlock(&cl->cl_lock);
5316 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
5317 RD_STATE : WR_STATE;
5320 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
5322 struct svc_fh *current_fh = &cstate->current_fh;
5323 struct nfs4_stateowner *sop = stp->st_stateowner;
5326 status = nfsd4_check_seqid(cstate, sop, seqid);
5329 status = nfsd4_lock_ol_stateid(stp);
5330 if (status != nfs_ok)
5332 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
5333 if (status == nfs_ok)
5334 status = nfs4_check_fh(current_fh, &stp->st_stid);
5335 if (status != nfs_ok)
5336 mutex_unlock(&stp->st_mutex);
5341 * Checks for sequence id mutating operations.
5344 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
5345 stateid_t *stateid, char typemask,
5346 struct nfs4_ol_stateid **stpp,
5347 struct nfsd_net *nn)
5350 struct nfs4_stid *s;
5351 struct nfs4_ol_stateid *stp = NULL;
5353 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
5354 seqid, STATEID_VAL(stateid));
5357 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
5360 stp = openlockstateid(s);
5361 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
5363 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
5367 nfs4_put_stid(&stp->st_stid);
5371 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
5372 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
5375 struct nfs4_openowner *oo;
5376 struct nfs4_ol_stateid *stp;
5378 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
5379 NFS4_OPEN_STID, &stp, nn);
5382 oo = openowner(stp->st_stateowner);
5383 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
5384 mutex_unlock(&stp->st_mutex);
5385 nfs4_put_stid(&stp->st_stid);
5386 return nfserr_bad_stateid;
5393 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5394 union nfsd4_op_u *u)
5396 struct nfsd4_open_confirm *oc = &u->open_confirm;
5398 struct nfs4_openowner *oo;
5399 struct nfs4_ol_stateid *stp;
5400 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5402 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
5403 cstate->current_fh.fh_dentry);
5405 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
5409 status = nfs4_preprocess_seqid_op(cstate,
5410 oc->oc_seqid, &oc->oc_req_stateid,
5411 NFS4_OPEN_STID, &stp, nn);
5414 oo = openowner(stp->st_stateowner);
5415 status = nfserr_bad_stateid;
5416 if (oo->oo_flags & NFS4_OO_CONFIRMED) {
5417 mutex_unlock(&stp->st_mutex);
5420 oo->oo_flags |= NFS4_OO_CONFIRMED;
5421 nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
5422 mutex_unlock(&stp->st_mutex);
5423 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
5424 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
5426 nfsd4_client_record_create(oo->oo_owner.so_client);
5429 nfs4_put_stid(&stp->st_stid);
5431 nfsd4_bump_seqid(cstate, status);
5435 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
5437 if (!test_access(access, stp))
5439 nfs4_file_put_access(stp->st_stid.sc_file, access);
5440 clear_access(access, stp);
5443 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
5445 switch (to_access) {
5446 case NFS4_SHARE_ACCESS_READ:
5447 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
5448 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
5450 case NFS4_SHARE_ACCESS_WRITE:
5451 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
5452 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
5454 case NFS4_SHARE_ACCESS_BOTH:
5462 nfsd4_open_downgrade(struct svc_rqst *rqstp,
5463 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
5465 struct nfsd4_open_downgrade *od = &u->open_downgrade;
5467 struct nfs4_ol_stateid *stp;
5468 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5470 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
5471 cstate->current_fh.fh_dentry);
5473 /* We don't yet support WANT bits: */
5474 if (od->od_deleg_want)
5475 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
5478 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
5479 &od->od_stateid, &stp, nn);
5482 status = nfserr_inval;
5483 if (!test_access(od->od_share_access, stp)) {
5484 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
5485 stp->st_access_bmap, od->od_share_access);
5488 if (!test_deny(od->od_share_deny, stp)) {
5489 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
5490 stp->st_deny_bmap, od->od_share_deny);
5493 nfs4_stateid_downgrade(stp, od->od_share_access);
5494 reset_union_bmap_deny(od->od_share_deny, stp);
5495 nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
5498 mutex_unlock(&stp->st_mutex);
5499 nfs4_put_stid(&stp->st_stid);
5501 nfsd4_bump_seqid(cstate, status);
5505 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
5507 struct nfs4_client *clp = s->st_stid.sc_client;
5509 LIST_HEAD(reaplist);
5511 spin_lock(&clp->cl_lock);
5512 unhashed = unhash_open_stateid(s, &reaplist);
5514 if (clp->cl_minorversion) {
5516 put_ol_stateid_locked(s, &reaplist);
5517 spin_unlock(&clp->cl_lock);
5518 free_ol_stateid_reaplist(&reaplist);
5520 spin_unlock(&clp->cl_lock);
5521 free_ol_stateid_reaplist(&reaplist);
5523 move_to_close_lru(s, clp->net);
5528 * nfs4_unlock_state() called after encode
5531 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5532 union nfsd4_op_u *u)
5534 struct nfsd4_close *close = &u->close;
5536 struct nfs4_ol_stateid *stp;
5537 struct net *net = SVC_NET(rqstp);
5538 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5540 dprintk("NFSD: nfsd4_close on file %pd\n",
5541 cstate->current_fh.fh_dentry);
5543 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
5545 NFS4_OPEN_STID|NFS4_CLOSED_STID,
5547 nfsd4_bump_seqid(cstate, status);
5551 stp->st_stid.sc_type = NFS4_CLOSED_STID;
5554 * Technically we don't _really_ have to increment or copy it, since
5555 * it should just be gone after this operation and we clobber the
5556 * copied value below, but we continue to do so here just to ensure
5557 * that racing ops see that there was a state change.
5559 nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
5561 nfsd4_close_open_stateid(stp);
5562 mutex_unlock(&stp->st_mutex);
5564 /* v4.1+ suggests that we send a special stateid in here, since the
5565 * clients should just ignore this anyway. Since this is not useful
5566 * for v4.0 clients either, we set it to the special close_stateid
5569 * See RFC5661 section 18.2.4, and RFC7530 section 16.2.5
5571 memcpy(&close->cl_stateid, &close_stateid, sizeof(close->cl_stateid));
5573 /* put reference from nfs4_preprocess_seqid_op */
5574 nfs4_put_stid(&stp->st_stid);
5580 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5581 union nfsd4_op_u *u)
5583 struct nfsd4_delegreturn *dr = &u->delegreturn;
5584 struct nfs4_delegation *dp;
5585 stateid_t *stateid = &dr->dr_stateid;
5586 struct nfs4_stid *s;
5588 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5590 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
5593 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
5596 dp = delegstateid(s);
5597 status = nfsd4_stid_check_stateid_generation(stateid, &dp->dl_stid, nfsd4_has_session(cstate));
5601 destroy_delegation(dp);
5603 nfs4_put_stid(&dp->dl_stid);
5609 end_offset(u64 start, u64 len)
5614 return end >= start ? end: NFS4_MAX_UINT64;
5617 /* last octet in a range */
5619 last_byte_offset(u64 start, u64 len)
5625 return end > start ? end - 1: NFS4_MAX_UINT64;
5629 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
5630 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
5631 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
5632 * locking, this prevents us from being completely protocol-compliant. The
5633 * real solution to this problem is to start using unsigned file offsets in
5634 * the VFS, but this is a very deep change!
5637 nfs4_transform_lock_offset(struct file_lock *lock)
5639 if (lock->fl_start < 0)
5640 lock->fl_start = OFFSET_MAX;
5641 if (lock->fl_end < 0)
5642 lock->fl_end = OFFSET_MAX;
5646 nfsd4_fl_get_owner(fl_owner_t owner)
5648 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
5650 nfs4_get_stateowner(&lo->lo_owner);
5655 nfsd4_fl_put_owner(fl_owner_t owner)
5657 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
5660 nfs4_put_stateowner(&lo->lo_owner);
5664 nfsd4_lm_notify(struct file_lock *fl)
5666 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
5667 struct net *net = lo->lo_owner.so_client->net;
5668 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5669 struct nfsd4_blocked_lock *nbl = container_of(fl,
5670 struct nfsd4_blocked_lock, nbl_lock);
5673 /* An empty list means that something else is going to be using it */
5674 spin_lock(&nn->blocked_locks_lock);
5675 if (!list_empty(&nbl->nbl_list)) {
5676 list_del_init(&nbl->nbl_list);
5677 list_del_init(&nbl->nbl_lru);
5680 spin_unlock(&nn->blocked_locks_lock);
5683 nfsd4_run_cb(&nbl->nbl_cb);
5686 static const struct lock_manager_operations nfsd_posix_mng_ops = {
5687 .lm_notify = nfsd4_lm_notify,
5688 .lm_get_owner = nfsd4_fl_get_owner,
5689 .lm_put_owner = nfsd4_fl_put_owner,
5693 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
5695 struct nfs4_lockowner *lo;
5697 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
5698 lo = (struct nfs4_lockowner *) fl->fl_owner;
5699 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
5700 lo->lo_owner.so_owner.len, GFP_KERNEL);
5701 if (!deny->ld_owner.data)
5702 /* We just don't care that much */
5704 deny->ld_owner.len = lo->lo_owner.so_owner.len;
5705 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
5708 deny->ld_owner.len = 0;
5709 deny->ld_owner.data = NULL;
5710 deny->ld_clientid.cl_boot = 0;
5711 deny->ld_clientid.cl_id = 0;
5713 deny->ld_start = fl->fl_start;
5714 deny->ld_length = NFS4_MAX_UINT64;
5715 if (fl->fl_end != NFS4_MAX_UINT64)
5716 deny->ld_length = fl->fl_end - fl->fl_start + 1;
5717 deny->ld_type = NFS4_READ_LT;
5718 if (fl->fl_type != F_RDLCK)
5719 deny->ld_type = NFS4_WRITE_LT;
5722 static struct nfs4_lockowner *
5723 find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
5725 unsigned int strhashval = ownerstr_hashval(owner);
5726 struct nfs4_stateowner *so;
5728 lockdep_assert_held(&clp->cl_lock);
5730 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
5732 if (so->so_is_open_owner)
5734 if (same_owner_str(so, owner))
5735 return lockowner(nfs4_get_stateowner(so));
5740 static struct nfs4_lockowner *
5741 find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
5743 struct nfs4_lockowner *lo;
5745 spin_lock(&clp->cl_lock);
5746 lo = find_lockowner_str_locked(clp, owner);
5747 spin_unlock(&clp->cl_lock);
5751 static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
5753 unhash_lockowner_locked(lockowner(sop));
5756 static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
5758 struct nfs4_lockowner *lo = lockowner(sop);
5760 kmem_cache_free(lockowner_slab, lo);
5763 static const struct nfs4_stateowner_operations lockowner_ops = {
5764 .so_unhash = nfs4_unhash_lockowner,
5765 .so_free = nfs4_free_lockowner,
5769 * Alloc a lock owner structure.
5770 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
5773 * strhashval = ownerstr_hashval
5775 static struct nfs4_lockowner *
5776 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
5777 struct nfs4_ol_stateid *open_stp,
5778 struct nfsd4_lock *lock)
5780 struct nfs4_lockowner *lo, *ret;
5782 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
5785 INIT_LIST_HEAD(&lo->lo_blocked);
5786 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
5787 lo->lo_owner.so_is_open_owner = 0;
5788 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
5789 lo->lo_owner.so_ops = &lockowner_ops;
5790 spin_lock(&clp->cl_lock);
5791 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
5793 list_add(&lo->lo_owner.so_strhash,
5794 &clp->cl_ownerstr_hashtbl[strhashval]);
5797 nfs4_free_stateowner(&lo->lo_owner);
5799 spin_unlock(&clp->cl_lock);
5803 static struct nfs4_ol_stateid *
5804 find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5806 struct nfs4_ol_stateid *lst;
5807 struct nfs4_client *clp = lo->lo_owner.so_client;
5809 lockdep_assert_held(&clp->cl_lock);
5811 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
5812 if (lst->st_stid.sc_type != NFS4_LOCK_STID)
5814 if (lst->st_stid.sc_file == fp) {
5815 refcount_inc(&lst->st_stid.sc_count);
5822 static struct nfs4_ol_stateid *
5823 init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
5824 struct nfs4_file *fp, struct inode *inode,
5825 struct nfs4_ol_stateid *open_stp)
5827 struct nfs4_client *clp = lo->lo_owner.so_client;
5828 struct nfs4_ol_stateid *retstp;
5830 mutex_init(&stp->st_mutex);
5831 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
5833 spin_lock(&clp->cl_lock);
5834 spin_lock(&fp->fi_lock);
5835 retstp = find_lock_stateid(lo, fp);
5839 refcount_inc(&stp->st_stid.sc_count);
5840 stp->st_stid.sc_type = NFS4_LOCK_STID;
5841 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
5843 stp->st_stid.sc_file = fp;
5844 stp->st_access_bmap = 0;
5845 stp->st_deny_bmap = open_stp->st_deny_bmap;
5846 stp->st_openstp = open_stp;
5847 list_add(&stp->st_locks, &open_stp->st_locks);
5848 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
5849 list_add(&stp->st_perfile, &fp->fi_stateids);
5851 spin_unlock(&fp->fi_lock);
5852 spin_unlock(&clp->cl_lock);
5854 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
5855 nfs4_put_stid(&retstp->st_stid);
5858 /* To keep mutex tracking happy */
5859 mutex_unlock(&stp->st_mutex);
5865 static struct nfs4_ol_stateid *
5866 find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5867 struct inode *inode, struct nfs4_ol_stateid *ost,
5870 struct nfs4_stid *ns = NULL;
5871 struct nfs4_ol_stateid *lst;
5872 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5873 struct nfs4_client *clp = oo->oo_owner.so_client;
5876 spin_lock(&clp->cl_lock);
5877 lst = find_lock_stateid(lo, fi);
5878 spin_unlock(&clp->cl_lock);
5880 if (nfsd4_lock_ol_stateid(lst) == nfs_ok)
5882 nfs4_put_stid(&lst->st_stid);
5884 ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid);
5888 lst = init_lock_stateid(openlockstateid(ns), lo, fi, inode, ost);
5889 if (lst == openlockstateid(ns))
5898 check_lock_length(u64 offset, u64 length)
5900 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
5901 (length > ~offset)));
5904 static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
5906 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
5908 lockdep_assert_held(&fp->fi_lock);
5910 if (test_access(access, lock_stp))
5912 __nfs4_file_get_access(fp, access);
5913 set_access(access, lock_stp);
5917 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5918 struct nfs4_ol_stateid *ost,
5919 struct nfsd4_lock *lock,
5920 struct nfs4_ol_stateid **plst, bool *new)
5923 struct nfs4_file *fi = ost->st_stid.sc_file;
5924 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5925 struct nfs4_client *cl = oo->oo_owner.so_client;
5926 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
5927 struct nfs4_lockowner *lo;
5928 struct nfs4_ol_stateid *lst;
5929 unsigned int strhashval;
5931 lo = find_lockowner_str(cl, &lock->lk_new_owner);
5933 strhashval = ownerstr_hashval(&lock->lk_new_owner);
5934 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5936 return nfserr_jukebox;
5938 /* with an existing lockowner, seqids must be the same */
5939 status = nfserr_bad_seqid;
5940 if (!cstate->minorversion &&
5941 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
5945 lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
5947 status = nfserr_jukebox;
5954 nfs4_put_stateowner(&lo->lo_owner);
5962 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5963 union nfsd4_op_u *u)
5965 struct nfsd4_lock *lock = &u->lock;
5966 struct nfs4_openowner *open_sop = NULL;
5967 struct nfs4_lockowner *lock_sop = NULL;
5968 struct nfs4_ol_stateid *lock_stp = NULL;
5969 struct nfs4_ol_stateid *open_stp = NULL;
5970 struct nfs4_file *fp;
5971 struct file *filp = NULL;
5972 struct nfsd4_blocked_lock *nbl = NULL;
5973 struct file_lock *file_lock = NULL;
5974 struct file_lock *conflock = NULL;
5979 unsigned char fl_type;
5980 unsigned int fl_flags = FL_POSIX;
5981 struct net *net = SVC_NET(rqstp);
5982 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5984 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5985 (long long) lock->lk_offset,
5986 (long long) lock->lk_length);
5988 if (check_lock_length(lock->lk_offset, lock->lk_length))
5989 return nfserr_inval;
5991 if ((status = fh_verify(rqstp, &cstate->current_fh,
5992 S_IFREG, NFSD_MAY_LOCK))) {
5993 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5997 if (lock->lk_is_new) {
5998 if (nfsd4_has_session(cstate))
5999 /* See rfc 5661 18.10.3: given clientid is ignored: */
6000 memcpy(&lock->lk_new_clientid,
6001 &cstate->session->se_client->cl_clientid,
6002 sizeof(clientid_t));
6004 status = nfserr_stale_clientid;
6005 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
6008 /* validate and update open stateid and open seqid */
6009 status = nfs4_preprocess_confirmed_seqid_op(cstate,
6010 lock->lk_new_open_seqid,
6011 &lock->lk_new_open_stateid,
6015 mutex_unlock(&open_stp->st_mutex);
6016 open_sop = openowner(open_stp->st_stateowner);
6017 status = nfserr_bad_stateid;
6018 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
6019 &lock->lk_new_clientid))
6021 status = lookup_or_create_lock_state(cstate, open_stp, lock,
6024 status = nfs4_preprocess_seqid_op(cstate,
6025 lock->lk_old_lock_seqid,
6026 &lock->lk_old_lock_stateid,
6027 NFS4_LOCK_STID, &lock_stp, nn);
6031 lock_sop = lockowner(lock_stp->st_stateowner);
6033 lkflg = setlkflg(lock->lk_type);
6034 status = nfs4_check_openmode(lock_stp, lkflg);
6038 status = nfserr_grace;
6039 if (locks_in_grace(net) && !lock->lk_reclaim)
6041 status = nfserr_no_grace;
6042 if (!locks_in_grace(net) && lock->lk_reclaim)
6045 fp = lock_stp->st_stid.sc_file;
6046 switch (lock->lk_type) {
6048 if (nfsd4_has_session(cstate))
6049 fl_flags |= FL_SLEEP;
6052 spin_lock(&fp->fi_lock);
6053 filp = find_readable_file_locked(fp);
6055 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
6056 spin_unlock(&fp->fi_lock);
6059 case NFS4_WRITEW_LT:
6060 if (nfsd4_has_session(cstate))
6061 fl_flags |= FL_SLEEP;
6064 spin_lock(&fp->fi_lock);
6065 filp = find_writeable_file_locked(fp);
6067 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
6068 spin_unlock(&fp->fi_lock);
6072 status = nfserr_inval;
6077 status = nfserr_openmode;
6081 nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
6083 dprintk("NFSD: %s: unable to allocate block!\n", __func__);
6084 status = nfserr_jukebox;
6088 file_lock = &nbl->nbl_lock;
6089 file_lock->fl_type = fl_type;
6090 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
6091 file_lock->fl_pid = current->tgid;
6092 file_lock->fl_file = filp;
6093 file_lock->fl_flags = fl_flags;
6094 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6095 file_lock->fl_start = lock->lk_offset;
6096 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
6097 nfs4_transform_lock_offset(file_lock);
6099 conflock = locks_alloc_lock();
6101 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6102 status = nfserr_jukebox;
6106 if (fl_flags & FL_SLEEP) {
6107 nbl->nbl_time = jiffies;
6108 spin_lock(&nn->blocked_locks_lock);
6109 list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
6110 list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
6111 spin_unlock(&nn->blocked_locks_lock);
6114 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
6116 case 0: /* success! */
6117 nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
6119 if (lock->lk_reclaim)
6120 nn->somebody_reclaimed = true;
6122 case FILE_LOCK_DEFERRED:
6125 case -EAGAIN: /* conflock holds conflicting lock */
6126 status = nfserr_denied;
6127 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
6128 nfs4_set_lock_denied(conflock, &lock->lk_denied);
6131 status = nfserr_deadlock;
6134 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
6135 status = nfserrno(err);
6140 /* dequeue it if we queued it before */
6141 if (fl_flags & FL_SLEEP) {
6142 spin_lock(&nn->blocked_locks_lock);
6143 list_del_init(&nbl->nbl_list);
6144 list_del_init(&nbl->nbl_lru);
6145 spin_unlock(&nn->blocked_locks_lock);
6147 free_blocked_lock(nbl);
6152 /* Bump seqid manually if the 4.0 replay owner is openowner */
6153 if (cstate->replay_owner &&
6154 cstate->replay_owner != &lock_sop->lo_owner &&
6155 seqid_mutating_err(ntohl(status)))
6156 lock_sop->lo_owner.so_seqid++;
6159 * If this is a new, never-before-used stateid, and we are
6160 * returning an error, then just go ahead and release it.
6163 release_lock_stateid(lock_stp);
6165 mutex_unlock(&lock_stp->st_mutex);
6167 nfs4_put_stid(&lock_stp->st_stid);
6170 nfs4_put_stid(&open_stp->st_stid);
6171 nfsd4_bump_seqid(cstate, status);
6173 locks_free_lock(conflock);
6178 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
6179 * so we do a temporary open here just to get an open file to pass to
6180 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
6183 static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
6186 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
6188 err = nfserrno(vfs_test_lock(file, lock));
6198 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6199 union nfsd4_op_u *u)
6201 struct nfsd4_lockt *lockt = &u->lockt;
6202 struct file_lock *file_lock = NULL;
6203 struct nfs4_lockowner *lo = NULL;
6205 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6207 if (locks_in_grace(SVC_NET(rqstp)))
6208 return nfserr_grace;
6210 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
6211 return nfserr_inval;
6213 if (!nfsd4_has_session(cstate)) {
6214 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
6219 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
6222 file_lock = locks_alloc_lock();
6224 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6225 status = nfserr_jukebox;
6229 switch (lockt->lt_type) {
6232 file_lock->fl_type = F_RDLCK;
6235 case NFS4_WRITEW_LT:
6236 file_lock->fl_type = F_WRLCK;
6239 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
6240 status = nfserr_inval;
6244 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
6246 file_lock->fl_owner = (fl_owner_t)lo;
6247 file_lock->fl_pid = current->tgid;
6248 file_lock->fl_flags = FL_POSIX;
6250 file_lock->fl_start = lockt->lt_offset;
6251 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
6253 nfs4_transform_lock_offset(file_lock);
6255 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
6259 if (file_lock->fl_type != F_UNLCK) {
6260 status = nfserr_denied;
6261 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
6265 nfs4_put_stateowner(&lo->lo_owner);
6267 locks_free_lock(file_lock);
6272 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6273 union nfsd4_op_u *u)
6275 struct nfsd4_locku *locku = &u->locku;
6276 struct nfs4_ol_stateid *stp;
6277 struct file *filp = NULL;
6278 struct file_lock *file_lock = NULL;
6281 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6283 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
6284 (long long) locku->lu_offset,
6285 (long long) locku->lu_length);
6287 if (check_lock_length(locku->lu_offset, locku->lu_length))
6288 return nfserr_inval;
6290 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
6291 &locku->lu_stateid, NFS4_LOCK_STID,
6295 filp = find_any_file(stp->st_stid.sc_file);
6297 status = nfserr_lock_range;
6300 file_lock = locks_alloc_lock();
6302 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6303 status = nfserr_jukebox;
6307 file_lock->fl_type = F_UNLCK;
6308 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
6309 file_lock->fl_pid = current->tgid;
6310 file_lock->fl_file = filp;
6311 file_lock->fl_flags = FL_POSIX;
6312 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6313 file_lock->fl_start = locku->lu_offset;
6315 file_lock->fl_end = last_byte_offset(locku->lu_offset,
6317 nfs4_transform_lock_offset(file_lock);
6319 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
6321 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
6324 nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
6328 mutex_unlock(&stp->st_mutex);
6329 nfs4_put_stid(&stp->st_stid);
6331 nfsd4_bump_seqid(cstate, status);
6333 locks_free_lock(file_lock);
6337 status = nfserrno(err);
6343 * true: locks held by lockowner
6344 * false: no locks held by lockowner
6347 check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
6349 struct file_lock *fl;
6351 struct file *filp = find_any_file(fp);
6352 struct inode *inode;
6353 struct file_lock_context *flctx;
6356 /* Any valid lock stateid should have some sort of access */
6361 inode = locks_inode(filp);
6362 flctx = inode->i_flctx;
6364 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
6365 spin_lock(&flctx->flc_lock);
6366 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
6367 if (fl->fl_owner == (fl_owner_t)lowner) {
6372 spin_unlock(&flctx->flc_lock);
6379 nfsd4_release_lockowner(struct svc_rqst *rqstp,
6380 struct nfsd4_compound_state *cstate,
6381 union nfsd4_op_u *u)
6383 struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner;
6384 clientid_t *clid = &rlockowner->rl_clientid;
6385 struct nfs4_stateowner *sop;
6386 struct nfs4_lockowner *lo = NULL;
6387 struct nfs4_ol_stateid *stp;
6388 struct xdr_netobj *owner = &rlockowner->rl_owner;
6389 unsigned int hashval = ownerstr_hashval(owner);
6391 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6392 struct nfs4_client *clp;
6393 LIST_HEAD (reaplist);
6395 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
6396 clid->cl_boot, clid->cl_id);
6398 status = lookup_clientid(clid, cstate, nn);
6403 /* Find the matching lock stateowner */
6404 spin_lock(&clp->cl_lock);
6405 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
6408 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
6411 /* see if there are still any locks associated with it */
6412 lo = lockowner(sop);
6413 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
6414 if (check_for_locks(stp->st_stid.sc_file, lo)) {
6415 status = nfserr_locks_held;
6416 spin_unlock(&clp->cl_lock);
6421 nfs4_get_stateowner(sop);
6425 spin_unlock(&clp->cl_lock);
6429 unhash_lockowner_locked(lo);
6430 while (!list_empty(&lo->lo_owner.so_stateids)) {
6431 stp = list_first_entry(&lo->lo_owner.so_stateids,
6432 struct nfs4_ol_stateid,
6434 WARN_ON(!unhash_lock_stateid(stp));
6435 put_ol_stateid_locked(stp, &reaplist);
6437 spin_unlock(&clp->cl_lock);
6438 free_ol_stateid_reaplist(&reaplist);
6439 remove_blocked_locks(lo);
6440 nfs4_put_stateowner(&lo->lo_owner);
6445 static inline struct nfs4_client_reclaim *
6448 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
6452 nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
6454 struct nfs4_client_reclaim *crp;
6456 crp = nfsd4_find_reclaim_client(name, nn);
6457 return (crp && crp->cr_clp);
6461 * failure => all reset bets are off, nfserr_no_grace...
6463 struct nfs4_client_reclaim *
6464 nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
6466 unsigned int strhashval;
6467 struct nfs4_client_reclaim *crp;
6469 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
6470 crp = alloc_reclaim();
6472 strhashval = clientstr_hashval(name);
6473 INIT_LIST_HEAD(&crp->cr_strhash);
6474 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
6475 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
6477 nn->reclaim_str_hashtbl_size++;
6483 nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
6485 list_del(&crp->cr_strhash);
6487 nn->reclaim_str_hashtbl_size--;
6491 nfs4_release_reclaim(struct nfsd_net *nn)
6493 struct nfs4_client_reclaim *crp = NULL;
6496 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6497 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
6498 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
6499 struct nfs4_client_reclaim, cr_strhash);
6500 nfs4_remove_reclaim_record(crp, nn);
6503 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
6507 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
6508 struct nfs4_client_reclaim *
6509 nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
6511 unsigned int strhashval;
6512 struct nfs4_client_reclaim *crp = NULL;
6514 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
6516 strhashval = clientstr_hashval(recdir);
6517 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
6518 if (same_name(crp->cr_recdir, recdir)) {
6526 * Called from OPEN. Look for clientid in reclaim list.
6529 nfs4_check_open_reclaim(clientid_t *clid,
6530 struct nfsd4_compound_state *cstate,
6531 struct nfsd_net *nn)
6535 /* find clientid in conf_id_hashtbl */
6536 status = lookup_clientid(clid, cstate, nn);
6538 return nfserr_reclaim_bad;
6540 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
6541 return nfserr_no_grace;
6543 if (nfsd4_client_record_check(cstate->clp))
6544 return nfserr_reclaim_bad;
6549 #ifdef CONFIG_NFSD_FAULT_INJECTION
6551 put_client(struct nfs4_client *clp)
6553 atomic_dec(&clp->cl_refcount);
6556 static struct nfs4_client *
6557 nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
6559 struct nfs4_client *clp;
6560 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6563 if (!nfsd_netns_ready(nn))
6566 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6567 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
6574 nfsd_inject_print_clients(void)
6576 struct nfs4_client *clp;
6578 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6580 char buf[INET6_ADDRSTRLEN];
6582 if (!nfsd_netns_ready(nn))
6585 spin_lock(&nn->client_lock);
6586 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6587 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
6588 pr_info("NFS Client: %s\n", buf);
6591 spin_unlock(&nn->client_lock);
6597 nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
6600 struct nfs4_client *clp;
6601 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6604 if (!nfsd_netns_ready(nn))
6607 spin_lock(&nn->client_lock);
6608 clp = nfsd_find_client(addr, addr_size);
6610 if (mark_client_expired_locked(clp) == nfs_ok)
6615 spin_unlock(&nn->client_lock);
6624 nfsd_inject_forget_clients(u64 max)
6627 struct nfs4_client *clp, *next;
6628 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6630 LIST_HEAD(reaplist);
6632 if (!nfsd_netns_ready(nn))
6635 spin_lock(&nn->client_lock);
6636 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6637 if (mark_client_expired_locked(clp) == nfs_ok) {
6638 list_add(&clp->cl_lru, &reaplist);
6639 if (max != 0 && ++count >= max)
6643 spin_unlock(&nn->client_lock);
6645 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
6651 static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
6654 char buf[INET6_ADDRSTRLEN];
6655 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
6656 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
6660 nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
6661 struct list_head *collect)
6663 struct nfs4_client *clp = lst->st_stid.sc_client;
6664 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6670 lockdep_assert_held(&nn->client_lock);
6671 atomic_inc(&clp->cl_refcount);
6672 list_add(&lst->st_locks, collect);
6675 static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
6676 struct list_head *collect,
6677 bool (*func)(struct nfs4_ol_stateid *))
6679 struct nfs4_openowner *oop;
6680 struct nfs4_ol_stateid *stp, *st_next;
6681 struct nfs4_ol_stateid *lst, *lst_next;
6684 spin_lock(&clp->cl_lock);
6685 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
6686 list_for_each_entry_safe(stp, st_next,
6687 &oop->oo_owner.so_stateids, st_perstateowner) {
6688 list_for_each_entry_safe(lst, lst_next,
6689 &stp->st_locks, st_locks) {
6692 nfsd_inject_add_lock_to_list(lst,
6697 * Despite the fact that these functions deal
6698 * with 64-bit integers for "count", we must
6699 * ensure that it doesn't blow up the
6700 * clp->cl_refcount. Throw a warning if we
6701 * start to approach INT_MAX here.
6703 WARN_ON_ONCE(count == (INT_MAX / 2));
6710 spin_unlock(&clp->cl_lock);
6716 nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
6719 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
6723 nfsd_print_client_locks(struct nfs4_client *clp)
6725 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
6726 nfsd_print_count(clp, count, "locked files");
6731 nfsd_inject_print_locks(void)
6733 struct nfs4_client *clp;
6735 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6738 if (!nfsd_netns_ready(nn))
6741 spin_lock(&nn->client_lock);
6742 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6743 count += nfsd_print_client_locks(clp);
6744 spin_unlock(&nn->client_lock);
6750 nfsd_reap_locks(struct list_head *reaplist)
6752 struct nfs4_client *clp;
6753 struct nfs4_ol_stateid *stp, *next;
6755 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
6756 list_del_init(&stp->st_locks);
6757 clp = stp->st_stid.sc_client;
6758 nfs4_put_stid(&stp->st_stid);
6764 nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
6766 unsigned int count = 0;
6767 struct nfs4_client *clp;
6768 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6770 LIST_HEAD(reaplist);
6772 if (!nfsd_netns_ready(nn))
6775 spin_lock(&nn->client_lock);
6776 clp = nfsd_find_client(addr, addr_size);
6778 count = nfsd_collect_client_locks(clp, &reaplist, 0);
6779 spin_unlock(&nn->client_lock);
6780 nfsd_reap_locks(&reaplist);
6785 nfsd_inject_forget_locks(u64 max)
6788 struct nfs4_client *clp;
6789 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6791 LIST_HEAD(reaplist);
6793 if (!nfsd_netns_ready(nn))
6796 spin_lock(&nn->client_lock);
6797 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6798 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
6799 if (max != 0 && count >= max)
6802 spin_unlock(&nn->client_lock);
6803 nfsd_reap_locks(&reaplist);
6808 nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
6809 struct list_head *collect,
6810 void (*func)(struct nfs4_openowner *))
6812 struct nfs4_openowner *oop, *next;
6813 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6817 lockdep_assert_held(&nn->client_lock);
6819 spin_lock(&clp->cl_lock);
6820 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
6824 atomic_inc(&clp->cl_refcount);
6825 list_add(&oop->oo_perclient, collect);
6830 * Despite the fact that these functions deal with
6831 * 64-bit integers for "count", we must ensure that
6832 * it doesn't blow up the clp->cl_refcount. Throw a
6833 * warning if we start to approach INT_MAX here.
6835 WARN_ON_ONCE(count == (INT_MAX / 2));
6839 spin_unlock(&clp->cl_lock);
6845 nfsd_print_client_openowners(struct nfs4_client *clp)
6847 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
6849 nfsd_print_count(clp, count, "openowners");
6854 nfsd_collect_client_openowners(struct nfs4_client *clp,
6855 struct list_head *collect, u64 max)
6857 return nfsd_foreach_client_openowner(clp, max, collect,
6858 unhash_openowner_locked);
6862 nfsd_inject_print_openowners(void)
6864 struct nfs4_client *clp;
6866 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6869 if (!nfsd_netns_ready(nn))
6872 spin_lock(&nn->client_lock);
6873 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6874 count += nfsd_print_client_openowners(clp);
6875 spin_unlock(&nn->client_lock);
6881 nfsd_reap_openowners(struct list_head *reaplist)
6883 struct nfs4_client *clp;
6884 struct nfs4_openowner *oop, *next;
6886 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
6887 list_del_init(&oop->oo_perclient);
6888 clp = oop->oo_owner.so_client;
6889 release_openowner(oop);
6895 nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
6898 unsigned int count = 0;
6899 struct nfs4_client *clp;
6900 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6902 LIST_HEAD(reaplist);
6904 if (!nfsd_netns_ready(nn))
6907 spin_lock(&nn->client_lock);
6908 clp = nfsd_find_client(addr, addr_size);
6910 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
6911 spin_unlock(&nn->client_lock);
6912 nfsd_reap_openowners(&reaplist);
6917 nfsd_inject_forget_openowners(u64 max)
6920 struct nfs4_client *clp;
6921 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6923 LIST_HEAD(reaplist);
6925 if (!nfsd_netns_ready(nn))
6928 spin_lock(&nn->client_lock);
6929 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6930 count += nfsd_collect_client_openowners(clp, &reaplist,
6932 if (max != 0 && count >= max)
6935 spin_unlock(&nn->client_lock);
6936 nfsd_reap_openowners(&reaplist);
6940 static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
6941 struct list_head *victims)
6943 struct nfs4_delegation *dp, *next;
6944 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6948 lockdep_assert_held(&nn->client_lock);
6950 spin_lock(&state_lock);
6951 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
6954 * It's not safe to mess with delegations that have a
6955 * non-zero dl_time. They might have already been broken
6956 * and could be processed by the laundromat outside of
6957 * the state_lock. Just leave them be.
6959 if (dp->dl_time != 0)
6962 atomic_inc(&clp->cl_refcount);
6963 WARN_ON(!unhash_delegation_locked(dp));
6964 list_add(&dp->dl_recall_lru, victims);
6968 * Despite the fact that these functions deal with
6969 * 64-bit integers for "count", we must ensure that
6970 * it doesn't blow up the clp->cl_refcount. Throw a
6971 * warning if we start to approach INT_MAX here.
6973 WARN_ON_ONCE(count == (INT_MAX / 2));
6977 spin_unlock(&state_lock);
6982 nfsd_print_client_delegations(struct nfs4_client *clp)
6984 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
6986 nfsd_print_count(clp, count, "delegations");
6991 nfsd_inject_print_delegations(void)
6993 struct nfs4_client *clp;
6995 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6998 if (!nfsd_netns_ready(nn))
7001 spin_lock(&nn->client_lock);
7002 list_for_each_entry(clp, &nn->client_lru, cl_lru)
7003 count += nfsd_print_client_delegations(clp);
7004 spin_unlock(&nn->client_lock);
7010 nfsd_forget_delegations(struct list_head *reaplist)
7012 struct nfs4_client *clp;
7013 struct nfs4_delegation *dp, *next;
7015 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
7016 list_del_init(&dp->dl_recall_lru);
7017 clp = dp->dl_stid.sc_client;
7018 revoke_delegation(dp);
7024 nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
7028 struct nfs4_client *clp;
7029 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7031 LIST_HEAD(reaplist);
7033 if (!nfsd_netns_ready(nn))
7036 spin_lock(&nn->client_lock);
7037 clp = nfsd_find_client(addr, addr_size);
7039 count = nfsd_find_all_delegations(clp, 0, &reaplist);
7040 spin_unlock(&nn->client_lock);
7042 nfsd_forget_delegations(&reaplist);
7047 nfsd_inject_forget_delegations(u64 max)
7050 struct nfs4_client *clp;
7051 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7053 LIST_HEAD(reaplist);
7055 if (!nfsd_netns_ready(nn))
7058 spin_lock(&nn->client_lock);
7059 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
7060 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
7061 if (max != 0 && count >= max)
7064 spin_unlock(&nn->client_lock);
7065 nfsd_forget_delegations(&reaplist);
7070 nfsd_recall_delegations(struct list_head *reaplist)
7072 struct nfs4_client *clp;
7073 struct nfs4_delegation *dp, *next;
7075 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
7076 list_del_init(&dp->dl_recall_lru);
7077 clp = dp->dl_stid.sc_client;
7079 * We skipped all entries that had a zero dl_time before,
7080 * so we can now reset the dl_time back to 0. If a delegation
7081 * break comes in now, then it won't make any difference since
7082 * we're recalling it either way.
7084 spin_lock(&state_lock);
7086 spin_unlock(&state_lock);
7087 nfsd_break_one_deleg(dp);
7093 nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
7097 struct nfs4_client *clp;
7098 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7100 LIST_HEAD(reaplist);
7102 if (!nfsd_netns_ready(nn))
7105 spin_lock(&nn->client_lock);
7106 clp = nfsd_find_client(addr, addr_size);
7108 count = nfsd_find_all_delegations(clp, 0, &reaplist);
7109 spin_unlock(&nn->client_lock);
7111 nfsd_recall_delegations(&reaplist);
7116 nfsd_inject_recall_delegations(u64 max)
7119 struct nfs4_client *clp, *next;
7120 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7122 LIST_HEAD(reaplist);
7124 if (!nfsd_netns_ready(nn))
7127 spin_lock(&nn->client_lock);
7128 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
7129 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
7130 if (max != 0 && ++count >= max)
7133 spin_unlock(&nn->client_lock);
7134 nfsd_recall_delegations(&reaplist);
7137 #endif /* CONFIG_NFSD_FAULT_INJECTION */
7140 * Since the lifetime of a delegation isn't limited to that of an open, a
7141 * client may quite reasonably hang on to a delegation as long as it has
7142 * the inode cached. This becomes an obvious problem the first time a
7143 * client's inode cache approaches the size of the server's total memory.
7145 * For now we avoid this problem by imposing a hard limit on the number
7146 * of delegations, which varies according to the server's memory size.
7149 set_max_delegations(void)
7152 * Allow at most 4 delegations per megabyte of RAM. Quick
7153 * estimates suggest that in the worst case (where every delegation
7154 * is for a different inode), a delegation could take about 1.5K,
7155 * giving a worst case usage of about 6% of memory.
7157 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
7160 static int nfs4_state_create_net(struct net *net)
7162 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7165 nn->conf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7166 sizeof(struct list_head),
7168 if (!nn->conf_id_hashtbl)
7170 nn->unconf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7171 sizeof(struct list_head),
7173 if (!nn->unconf_id_hashtbl)
7175 nn->sessionid_hashtbl = kmalloc_array(SESSION_HASH_SIZE,
7176 sizeof(struct list_head),
7178 if (!nn->sessionid_hashtbl)
7181 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7182 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
7183 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
7185 for (i = 0; i < SESSION_HASH_SIZE; i++)
7186 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
7187 nn->conf_name_tree = RB_ROOT;
7188 nn->unconf_name_tree = RB_ROOT;
7189 nn->boot_time = get_seconds();
7190 nn->grace_ended = false;
7191 nn->nfsd4_manager.block_opens = true;
7192 INIT_LIST_HEAD(&nn->nfsd4_manager.list);
7193 INIT_LIST_HEAD(&nn->client_lru);
7194 INIT_LIST_HEAD(&nn->close_lru);
7195 INIT_LIST_HEAD(&nn->del_recall_lru);
7196 spin_lock_init(&nn->client_lock);
7197 spin_lock_init(&nn->s2s_cp_lock);
7198 idr_init(&nn->s2s_cp_stateids);
7200 spin_lock_init(&nn->blocked_locks_lock);
7201 INIT_LIST_HEAD(&nn->blocked_locks_lru);
7203 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
7209 kfree(nn->unconf_id_hashtbl);
7211 kfree(nn->conf_id_hashtbl);
7217 nfs4_state_destroy_net(struct net *net)
7220 struct nfs4_client *clp = NULL;
7221 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7223 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7224 while (!list_empty(&nn->conf_id_hashtbl[i])) {
7225 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7226 destroy_client(clp);
7230 WARN_ON(!list_empty(&nn->blocked_locks_lru));
7232 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7233 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
7234 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7235 destroy_client(clp);
7239 kfree(nn->sessionid_hashtbl);
7240 kfree(nn->unconf_id_hashtbl);
7241 kfree(nn->conf_id_hashtbl);
7246 nfs4_state_start_net(struct net *net)
7248 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7251 ret = nfs4_state_create_net(net);
7254 locks_start_grace(net, &nn->nfsd4_manager);
7255 nfsd4_client_tracking_init(net);
7256 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %x)\n",
7257 nn->nfsd4_grace, net->ns.inum);
7258 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
7262 /* initialization to perform when the nfsd service is started: */
7265 nfs4_state_start(void)
7269 laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
7270 if (laundry_wq == NULL) {
7274 ret = nfsd4_create_callback_queue();
7276 goto out_free_laundry;
7278 set_max_delegations();
7282 destroy_workqueue(laundry_wq);
7288 nfs4_state_shutdown_net(struct net *net)
7290 struct nfs4_delegation *dp = NULL;
7291 struct list_head *pos, *next, reaplist;
7292 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7294 cancel_delayed_work_sync(&nn->laundromat_work);
7295 locks_end_grace(&nn->nfsd4_manager);
7297 INIT_LIST_HEAD(&reaplist);
7298 spin_lock(&state_lock);
7299 list_for_each_safe(pos, next, &nn->del_recall_lru) {
7300 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
7301 WARN_ON(!unhash_delegation_locked(dp));
7302 list_add(&dp->dl_recall_lru, &reaplist);
7304 spin_unlock(&state_lock);
7305 list_for_each_safe(pos, next, &reaplist) {
7306 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
7307 list_del_init(&dp->dl_recall_lru);
7308 destroy_unhashed_deleg(dp);
7311 nfsd4_client_tracking_exit(net);
7312 nfs4_state_destroy_net(net);
7316 nfs4_state_shutdown(void)
7318 destroy_workqueue(laundry_wq);
7319 nfsd4_destroy_callback_queue();
7323 get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7325 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
7326 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
7330 put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7332 if (cstate->minorversion) {
7333 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
7334 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
7339 clear_current_stateid(struct nfsd4_compound_state *cstate)
7341 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
7345 * functions to set current state id
7348 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate,
7349 union nfsd4_op_u *u)
7351 put_stateid(cstate, &u->open_downgrade.od_stateid);
7355 nfsd4_set_openstateid(struct nfsd4_compound_state *cstate,
7356 union nfsd4_op_u *u)
7358 put_stateid(cstate, &u->open.op_stateid);
7362 nfsd4_set_closestateid(struct nfsd4_compound_state *cstate,
7363 union nfsd4_op_u *u)
7365 put_stateid(cstate, &u->close.cl_stateid);
7369 nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate,
7370 union nfsd4_op_u *u)
7372 put_stateid(cstate, &u->lock.lk_resp_stateid);
7376 * functions to consume current state id
7380 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate,
7381 union nfsd4_op_u *u)
7383 get_stateid(cstate, &u->open_downgrade.od_stateid);
7387 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate,
7388 union nfsd4_op_u *u)
7390 get_stateid(cstate, &u->delegreturn.dr_stateid);
7394 nfsd4_get_freestateid(struct nfsd4_compound_state *cstate,
7395 union nfsd4_op_u *u)
7397 get_stateid(cstate, &u->free_stateid.fr_stateid);
7401 nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate,
7402 union nfsd4_op_u *u)
7404 get_stateid(cstate, &u->setattr.sa_stateid);
7408 nfsd4_get_closestateid(struct nfsd4_compound_state *cstate,
7409 union nfsd4_op_u *u)
7411 get_stateid(cstate, &u->close.cl_stateid);
7415 nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate,
7416 union nfsd4_op_u *u)
7418 get_stateid(cstate, &u->locku.lu_stateid);
7422 nfsd4_get_readstateid(struct nfsd4_compound_state *cstate,
7423 union nfsd4_op_u *u)
7425 get_stateid(cstate, &u->read.rd_stateid);
7429 nfsd4_get_writestateid(struct nfsd4_compound_state *cstate,
7430 union nfsd4_op_u *u)
7432 get_stateid(cstate, &u->write.wr_stateid);