2 * Copyright (c) 2001 The Regents of the University of Michigan.
5 * Kendrick Smith <kmsmith@umich.edu>
6 * Andy Adamson <kandros@umich.edu>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <linux/file.h>
37 #include <linux/slab.h>
38 #include <linux/namei.h>
39 #include <linux/swap.h>
40 #include <linux/pagemap.h>
41 #include <linux/ratelimit.h>
42 #include <linux/sunrpc/svcauth_gss.h>
43 #include <linux/sunrpc/addr.h>
44 #include <linux/jhash.h>
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 posix_unblock_lock(&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 INIT_LIST_HEAD(&nbl->nbl_list);
256 INIT_LIST_HEAD(&nbl->nbl_lru);
257 fh_copy_shallow(&nbl->nbl_fh, fh);
258 locks_init_lock(&nbl->nbl_lock);
259 nfsd4_init_cb(&nbl->nbl_cb, lo->lo_owner.so_client,
260 &nfsd4_cb_notify_lock_ops,
261 NFSPROC4_CLNT_CB_NOTIFY_LOCK);
268 free_blocked_lock(struct nfsd4_blocked_lock *nbl)
270 locks_release_private(&nbl->nbl_lock);
275 remove_blocked_locks(struct nfs4_lockowner *lo)
277 struct nfs4_client *clp = lo->lo_owner.so_client;
278 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
279 struct nfsd4_blocked_lock *nbl;
282 /* Dequeue all blocked locks */
283 spin_lock(&nn->blocked_locks_lock);
284 while (!list_empty(&lo->lo_blocked)) {
285 nbl = list_first_entry(&lo->lo_blocked,
286 struct nfsd4_blocked_lock,
288 list_del_init(&nbl->nbl_list);
289 list_move(&nbl->nbl_lru, &reaplist);
291 spin_unlock(&nn->blocked_locks_lock);
294 while (!list_empty(&reaplist)) {
295 nbl = list_first_entry(&reaplist, struct nfsd4_blocked_lock,
297 list_del_init(&nbl->nbl_lru);
298 posix_unblock_lock(&nbl->nbl_lock);
299 free_blocked_lock(nbl);
304 nfsd4_cb_notify_lock_done(struct nfsd4_callback *cb, struct rpc_task *task)
307 * Since this is just an optimization, we don't try very hard if it
308 * turns out not to succeed. We'll requeue it on NFS4ERR_DELAY, and
309 * just quit trying on anything else.
311 switch (task->tk_status) {
313 rpc_delay(task, 1 * HZ);
321 nfsd4_cb_notify_lock_release(struct nfsd4_callback *cb)
323 struct nfsd4_blocked_lock *nbl = container_of(cb,
324 struct nfsd4_blocked_lock, nbl_cb);
326 free_blocked_lock(nbl);
329 static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops = {
330 .done = nfsd4_cb_notify_lock_done,
331 .release = nfsd4_cb_notify_lock_release,
334 static inline struct nfs4_stateowner *
335 nfs4_get_stateowner(struct nfs4_stateowner *sop)
337 atomic_inc(&sop->so_count);
342 same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
344 return (sop->so_owner.len == owner->len) &&
345 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
348 static struct nfs4_openowner *
349 find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
350 struct nfs4_client *clp)
352 struct nfs4_stateowner *so;
354 lockdep_assert_held(&clp->cl_lock);
356 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
358 if (!so->so_is_open_owner)
360 if (same_owner_str(so, &open->op_owner))
361 return openowner(nfs4_get_stateowner(so));
366 static struct nfs4_openowner *
367 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
368 struct nfs4_client *clp)
370 struct nfs4_openowner *oo;
372 spin_lock(&clp->cl_lock);
373 oo = find_openstateowner_str_locked(hashval, open, clp);
374 spin_unlock(&clp->cl_lock);
379 opaque_hashval(const void *ptr, int nbytes)
381 unsigned char *cptr = (unsigned char *) ptr;
391 static void nfsd4_free_file_rcu(struct rcu_head *rcu)
393 struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
395 kmem_cache_free(file_slab, fp);
399 put_nfs4_file(struct nfs4_file *fi)
401 might_lock(&state_lock);
403 if (refcount_dec_and_lock(&fi->fi_ref, &state_lock)) {
404 hlist_del_rcu(&fi->fi_hash);
405 spin_unlock(&state_lock);
406 WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
407 WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
408 call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
413 __nfs4_get_fd(struct nfs4_file *f, int oflag)
415 if (f->fi_fds[oflag])
416 return get_file(f->fi_fds[oflag]);
421 find_writeable_file_locked(struct nfs4_file *f)
425 lockdep_assert_held(&f->fi_lock);
427 ret = __nfs4_get_fd(f, O_WRONLY);
429 ret = __nfs4_get_fd(f, O_RDWR);
434 find_writeable_file(struct nfs4_file *f)
438 spin_lock(&f->fi_lock);
439 ret = find_writeable_file_locked(f);
440 spin_unlock(&f->fi_lock);
445 static struct file *find_readable_file_locked(struct nfs4_file *f)
449 lockdep_assert_held(&f->fi_lock);
451 ret = __nfs4_get_fd(f, O_RDONLY);
453 ret = __nfs4_get_fd(f, O_RDWR);
458 find_readable_file(struct nfs4_file *f)
462 spin_lock(&f->fi_lock);
463 ret = find_readable_file_locked(f);
464 spin_unlock(&f->fi_lock);
470 find_any_file(struct nfs4_file *f)
476 spin_lock(&f->fi_lock);
477 ret = __nfs4_get_fd(f, O_RDWR);
479 ret = __nfs4_get_fd(f, O_WRONLY);
481 ret = __nfs4_get_fd(f, O_RDONLY);
483 spin_unlock(&f->fi_lock);
487 static atomic_long_t num_delegations;
488 unsigned long max_delegations;
491 * Open owner state (share locks)
494 /* hash tables for lock and open owners */
495 #define OWNER_HASH_BITS 8
496 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
497 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
499 static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
503 ret = opaque_hashval(ownername->data, ownername->len);
504 return ret & OWNER_HASH_MASK;
507 /* hash table for nfs4_file */
508 #define FILE_HASH_BITS 8
509 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
511 static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
513 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
516 static unsigned int file_hashval(struct knfsd_fh *fh)
518 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
521 static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
524 __nfs4_file_get_access(struct nfs4_file *fp, u32 access)
526 lockdep_assert_held(&fp->fi_lock);
528 if (access & NFS4_SHARE_ACCESS_WRITE)
529 atomic_inc(&fp->fi_access[O_WRONLY]);
530 if (access & NFS4_SHARE_ACCESS_READ)
531 atomic_inc(&fp->fi_access[O_RDONLY]);
535 nfs4_file_get_access(struct nfs4_file *fp, u32 access)
537 lockdep_assert_held(&fp->fi_lock);
539 /* Does this access mode make sense? */
540 if (access & ~NFS4_SHARE_ACCESS_BOTH)
543 /* Does it conflict with a deny mode already set? */
544 if ((access & fp->fi_share_deny) != 0)
545 return nfserr_share_denied;
547 __nfs4_file_get_access(fp, access);
551 static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
553 /* Common case is that there is no deny mode. */
555 /* Does this deny mode make sense? */
556 if (deny & ~NFS4_SHARE_DENY_BOTH)
559 if ((deny & NFS4_SHARE_DENY_READ) &&
560 atomic_read(&fp->fi_access[O_RDONLY]))
561 return nfserr_share_denied;
563 if ((deny & NFS4_SHARE_DENY_WRITE) &&
564 atomic_read(&fp->fi_access[O_WRONLY]))
565 return nfserr_share_denied;
570 static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
572 might_lock(&fp->fi_lock);
574 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
575 struct file *f1 = NULL;
576 struct file *f2 = NULL;
578 swap(f1, fp->fi_fds[oflag]);
579 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
580 swap(f2, fp->fi_fds[O_RDWR]);
581 spin_unlock(&fp->fi_lock);
589 static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
591 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
593 if (access & NFS4_SHARE_ACCESS_WRITE)
594 __nfs4_file_put_access(fp, O_WRONLY);
595 if (access & NFS4_SHARE_ACCESS_READ)
596 __nfs4_file_put_access(fp, O_RDONLY);
600 * Allocate a new open/delegation state counter. This is needed for
601 * pNFS for proper return on close semantics.
603 * Note that we only allocate it for pNFS-enabled exports, otherwise
604 * all pointers to struct nfs4_clnt_odstate are always NULL.
606 static struct nfs4_clnt_odstate *
607 alloc_clnt_odstate(struct nfs4_client *clp)
609 struct nfs4_clnt_odstate *co;
611 co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
614 refcount_set(&co->co_odcount, 1);
620 hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
622 struct nfs4_file *fp = co->co_file;
624 lockdep_assert_held(&fp->fi_lock);
625 list_add(&co->co_perfile, &fp->fi_clnt_odstate);
629 get_clnt_odstate(struct nfs4_clnt_odstate *co)
632 refcount_inc(&co->co_odcount);
636 put_clnt_odstate(struct nfs4_clnt_odstate *co)
638 struct nfs4_file *fp;
644 if (refcount_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
645 list_del(&co->co_perfile);
646 spin_unlock(&fp->fi_lock);
648 nfsd4_return_all_file_layouts(co->co_client, fp);
649 kmem_cache_free(odstate_slab, co);
653 static struct nfs4_clnt_odstate *
654 find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
656 struct nfs4_clnt_odstate *co;
657 struct nfs4_client *cl;
664 spin_lock(&fp->fi_lock);
665 list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
666 if (co->co_client == cl) {
667 get_clnt_odstate(co);
673 hash_clnt_odstate_locked(new);
675 spin_unlock(&fp->fi_lock);
679 struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab,
680 void (*sc_free)(struct nfs4_stid *))
682 struct nfs4_stid *stid;
685 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
689 idr_preload(GFP_KERNEL);
690 spin_lock(&cl->cl_lock);
691 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 0, 0, GFP_NOWAIT);
692 spin_unlock(&cl->cl_lock);
697 stid->sc_free = sc_free;
698 stid->sc_client = cl;
699 stid->sc_stateid.si_opaque.so_id = new_id;
700 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
701 /* Will be incremented before return to client: */
702 refcount_set(&stid->sc_count, 1);
703 spin_lock_init(&stid->sc_lock);
706 * It shouldn't be a problem to reuse an opaque stateid value.
707 * I don't think it is for 4.1. But with 4.0 I worry that, for
708 * example, a stray write retransmission could be accepted by
709 * the server when it should have been rejected. Therefore,
710 * adopt a trick from the sctp code to attempt to maximize the
711 * amount of time until an id is reused, by ensuring they always
712 * "increase" (mod INT_MAX):
716 kmem_cache_free(slab, stid);
720 static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
722 struct nfs4_stid *stid;
724 stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid);
728 return openlockstateid(stid);
731 static void nfs4_free_deleg(struct nfs4_stid *stid)
733 kmem_cache_free(deleg_slab, stid);
734 atomic_long_dec(&num_delegations);
738 * When we recall a delegation, we should be careful not to hand it
739 * out again straight away.
740 * To ensure this we keep a pair of bloom filters ('new' and 'old')
741 * in which the filehandles of recalled delegations are "stored".
742 * If a filehandle appear in either filter, a delegation is blocked.
743 * When a delegation is recalled, the filehandle is stored in the "new"
745 * Every 30 seconds we swap the filters and clear the "new" one,
746 * unless both are empty of course.
748 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
749 * low 3 bytes as hash-table indices.
751 * 'blocked_delegations_lock', which is always taken in block_delegations(),
752 * is used to manage concurrent access. Testing does not need the lock
753 * except when swapping the two filters.
755 static DEFINE_SPINLOCK(blocked_delegations_lock);
756 static struct bloom_pair {
757 int entries, old_entries;
759 int new; /* index into 'set' */
760 DECLARE_BITMAP(set[2], 256);
761 } blocked_delegations;
763 static int delegation_blocked(struct knfsd_fh *fh)
766 struct bloom_pair *bd = &blocked_delegations;
768 if (bd->entries == 0)
770 if (seconds_since_boot() - bd->swap_time > 30) {
771 spin_lock(&blocked_delegations_lock);
772 if (seconds_since_boot() - bd->swap_time > 30) {
773 bd->entries -= bd->old_entries;
774 bd->old_entries = bd->entries;
775 memset(bd->set[bd->new], 0,
778 bd->swap_time = seconds_since_boot();
780 spin_unlock(&blocked_delegations_lock);
782 hash = jhash(&fh->fh_base, fh->fh_size, 0);
783 if (test_bit(hash&255, bd->set[0]) &&
784 test_bit((hash>>8)&255, bd->set[0]) &&
785 test_bit((hash>>16)&255, bd->set[0]))
788 if (test_bit(hash&255, bd->set[1]) &&
789 test_bit((hash>>8)&255, bd->set[1]) &&
790 test_bit((hash>>16)&255, bd->set[1]))
796 static void block_delegations(struct knfsd_fh *fh)
799 struct bloom_pair *bd = &blocked_delegations;
801 hash = jhash(&fh->fh_base, fh->fh_size, 0);
803 spin_lock(&blocked_delegations_lock);
804 __set_bit(hash&255, bd->set[bd->new]);
805 __set_bit((hash>>8)&255, bd->set[bd->new]);
806 __set_bit((hash>>16)&255, bd->set[bd->new]);
807 if (bd->entries == 0)
808 bd->swap_time = seconds_since_boot();
810 spin_unlock(&blocked_delegations_lock);
813 static struct nfs4_delegation *
814 alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
815 struct svc_fh *current_fh,
816 struct nfs4_clnt_odstate *odstate)
818 struct nfs4_delegation *dp;
821 dprintk("NFSD alloc_init_deleg\n");
822 n = atomic_long_inc_return(&num_delegations);
823 if (n < 0 || n > max_delegations)
825 if (delegation_blocked(¤t_fh->fh_handle))
827 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg));
832 * delegation seqid's are never incremented. The 4.1 special
833 * meaning of seqid 0 isn't meaningful, really, but let's avoid
834 * 0 anyway just for consistency and use 1:
836 dp->dl_stid.sc_stateid.si_generation = 1;
837 INIT_LIST_HEAD(&dp->dl_perfile);
838 INIT_LIST_HEAD(&dp->dl_perclnt);
839 INIT_LIST_HEAD(&dp->dl_recall_lru);
840 dp->dl_clnt_odstate = odstate;
841 get_clnt_odstate(odstate);
842 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
844 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
845 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
847 dp->dl_stid.sc_file = fp;
850 atomic_long_dec(&num_delegations);
855 nfs4_put_stid(struct nfs4_stid *s)
857 struct nfs4_file *fp = s->sc_file;
858 struct nfs4_client *clp = s->sc_client;
860 might_lock(&clp->cl_lock);
862 if (!refcount_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
863 wake_up_all(&close_wq);
866 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
867 spin_unlock(&clp->cl_lock);
874 nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
876 stateid_t *src = &stid->sc_stateid;
878 spin_lock(&stid->sc_lock);
879 if (unlikely(++src->si_generation == 0))
880 src->si_generation = 1;
881 memcpy(dst, src, sizeof(*dst));
882 spin_unlock(&stid->sc_lock);
885 static void put_deleg_file(struct nfs4_file *fp)
887 struct file *filp = NULL;
889 spin_lock(&fp->fi_lock);
890 if (--fp->fi_delegees == 0)
891 swap(filp, fp->fi_deleg_file);
892 spin_unlock(&fp->fi_lock);
898 static void nfs4_unlock_deleg_lease(struct nfs4_delegation *dp)
900 struct nfs4_file *fp = dp->dl_stid.sc_file;
901 struct file *filp = fp->fi_deleg_file;
903 WARN_ON_ONCE(!fp->fi_delegees);
905 vfs_setlease(filp, F_UNLCK, NULL, (void **)&dp);
909 static void destroy_unhashed_deleg(struct nfs4_delegation *dp)
911 put_clnt_odstate(dp->dl_clnt_odstate);
912 nfs4_unlock_deleg_lease(dp);
913 nfs4_put_stid(&dp->dl_stid);
916 void nfs4_unhash_stid(struct nfs4_stid *s)
922 * nfs4_delegation_exists - Discover if this delegation already exists
923 * @clp: a pointer to the nfs4_client we're granting a delegation to
924 * @fp: a pointer to the nfs4_file we're granting a delegation on
927 * On success: true iff an existing delegation is found
931 nfs4_delegation_exists(struct nfs4_client *clp, struct nfs4_file *fp)
933 struct nfs4_delegation *searchdp = NULL;
934 struct nfs4_client *searchclp = NULL;
936 lockdep_assert_held(&state_lock);
937 lockdep_assert_held(&fp->fi_lock);
939 list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
940 searchclp = searchdp->dl_stid.sc_client;
941 if (clp == searchclp) {
949 * hash_delegation_locked - Add a delegation to the appropriate lists
950 * @dp: a pointer to the nfs4_delegation we are adding.
951 * @fp: a pointer to the nfs4_file we're granting a delegation on
954 * On success: NULL if the delegation was successfully hashed.
956 * On error: -EAGAIN if one was previously granted to this
957 * nfs4_client for this nfs4_file. Delegation is not hashed.
962 hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
964 struct nfs4_client *clp = dp->dl_stid.sc_client;
966 lockdep_assert_held(&state_lock);
967 lockdep_assert_held(&fp->fi_lock);
969 if (nfs4_delegation_exists(clp, fp))
971 refcount_inc(&dp->dl_stid.sc_count);
972 dp->dl_stid.sc_type = NFS4_DELEG_STID;
973 list_add(&dp->dl_perfile, &fp->fi_delegations);
974 list_add(&dp->dl_perclnt, &clp->cl_delegations);
979 unhash_delegation_locked(struct nfs4_delegation *dp)
981 struct nfs4_file *fp = dp->dl_stid.sc_file;
983 lockdep_assert_held(&state_lock);
985 if (list_empty(&dp->dl_perfile))
988 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
989 /* Ensure that deleg break won't try to requeue it */
991 spin_lock(&fp->fi_lock);
992 list_del_init(&dp->dl_perclnt);
993 list_del_init(&dp->dl_recall_lru);
994 list_del_init(&dp->dl_perfile);
995 spin_unlock(&fp->fi_lock);
999 static void destroy_delegation(struct nfs4_delegation *dp)
1003 spin_lock(&state_lock);
1004 unhashed = unhash_delegation_locked(dp);
1005 spin_unlock(&state_lock);
1007 destroy_unhashed_deleg(dp);
1010 static void revoke_delegation(struct nfs4_delegation *dp)
1012 struct nfs4_client *clp = dp->dl_stid.sc_client;
1014 WARN_ON(!list_empty(&dp->dl_recall_lru));
1016 if (clp->cl_minorversion) {
1017 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
1018 refcount_inc(&dp->dl_stid.sc_count);
1019 spin_lock(&clp->cl_lock);
1020 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
1021 spin_unlock(&clp->cl_lock);
1023 destroy_unhashed_deleg(dp);
1030 static unsigned int clientid_hashval(u32 id)
1032 return id & CLIENT_HASH_MASK;
1035 static unsigned int clientstr_hashval(const char *name)
1037 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
1041 * We store the NONE, READ, WRITE, and BOTH bits separately in the
1042 * st_{access,deny}_bmap field of the stateid, in order to track not
1043 * only what share bits are currently in force, but also what
1044 * combinations of share bits previous opens have used. This allows us
1045 * to enforce the recommendation of rfc 3530 14.2.19 that the server
1046 * return an error if the client attempt to downgrade to a combination
1047 * of share bits not explicable by closing some of its previous opens.
1049 * XXX: This enforcement is actually incomplete, since we don't keep
1050 * track of access/deny bit combinations; so, e.g., we allow:
1052 * OPEN allow read, deny write
1053 * OPEN allow both, deny none
1054 * DOWNGRADE allow read, deny none
1056 * which we should reject.
1059 bmap_to_share_mode(unsigned long bmap) {
1061 unsigned int access = 0;
1063 for (i = 1; i < 4; i++) {
1064 if (test_bit(i, &bmap))
1070 /* set share access for a given stateid */
1072 set_access(u32 access, struct nfs4_ol_stateid *stp)
1074 unsigned char mask = 1 << access;
1076 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1077 stp->st_access_bmap |= mask;
1080 /* clear share access for a given stateid */
1082 clear_access(u32 access, struct nfs4_ol_stateid *stp)
1084 unsigned char mask = 1 << access;
1086 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1087 stp->st_access_bmap &= ~mask;
1090 /* test whether a given stateid has access */
1092 test_access(u32 access, struct nfs4_ol_stateid *stp)
1094 unsigned char mask = 1 << access;
1096 return (bool)(stp->st_access_bmap & mask);
1099 /* set share deny for a given stateid */
1101 set_deny(u32 deny, struct nfs4_ol_stateid *stp)
1103 unsigned char mask = 1 << deny;
1105 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1106 stp->st_deny_bmap |= mask;
1109 /* clear share deny for a given stateid */
1111 clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
1113 unsigned char mask = 1 << deny;
1115 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1116 stp->st_deny_bmap &= ~mask;
1119 /* test whether a given stateid is denying specific access */
1121 test_deny(u32 deny, struct nfs4_ol_stateid *stp)
1123 unsigned char mask = 1 << deny;
1125 return (bool)(stp->st_deny_bmap & mask);
1128 static int nfs4_access_to_omode(u32 access)
1130 switch (access & NFS4_SHARE_ACCESS_BOTH) {
1131 case NFS4_SHARE_ACCESS_READ:
1133 case NFS4_SHARE_ACCESS_WRITE:
1135 case NFS4_SHARE_ACCESS_BOTH:
1143 * A stateid that had a deny mode associated with it is being released
1144 * or downgraded. Recalculate the deny mode on the file.
1147 recalculate_deny_mode(struct nfs4_file *fp)
1149 struct nfs4_ol_stateid *stp;
1151 spin_lock(&fp->fi_lock);
1152 fp->fi_share_deny = 0;
1153 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
1154 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
1155 spin_unlock(&fp->fi_lock);
1159 reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
1162 bool change = false;
1164 for (i = 1; i < 4; i++) {
1165 if ((i & deny) != i) {
1171 /* Recalculate per-file deny mode if there was a change */
1173 recalculate_deny_mode(stp->st_stid.sc_file);
1176 /* release all access and file references for a given stateid */
1178 release_all_access(struct nfs4_ol_stateid *stp)
1181 struct nfs4_file *fp = stp->st_stid.sc_file;
1183 if (fp && stp->st_deny_bmap != 0)
1184 recalculate_deny_mode(fp);
1186 for (i = 1; i < 4; i++) {
1187 if (test_access(i, stp))
1188 nfs4_file_put_access(stp->st_stid.sc_file, i);
1189 clear_access(i, stp);
1193 static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1195 kfree(sop->so_owner.data);
1196 sop->so_ops->so_free(sop);
1199 static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1201 struct nfs4_client *clp = sop->so_client;
1203 might_lock(&clp->cl_lock);
1205 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
1207 sop->so_ops->so_unhash(sop);
1208 spin_unlock(&clp->cl_lock);
1209 nfs4_free_stateowner(sop);
1213 nfs4_ol_stateid_unhashed(const struct nfs4_ol_stateid *stp)
1215 return list_empty(&stp->st_perfile);
1218 static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
1220 struct nfs4_file *fp = stp->st_stid.sc_file;
1222 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1224 if (list_empty(&stp->st_perfile))
1227 spin_lock(&fp->fi_lock);
1228 list_del_init(&stp->st_perfile);
1229 spin_unlock(&fp->fi_lock);
1230 list_del(&stp->st_perstateowner);
1234 static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
1236 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1238 put_clnt_odstate(stp->st_clnt_odstate);
1239 release_all_access(stp);
1240 if (stp->st_stateowner)
1241 nfs4_put_stateowner(stp->st_stateowner);
1242 kmem_cache_free(stateid_slab, stid);
1245 static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
1247 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1248 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
1251 file = find_any_file(stp->st_stid.sc_file);
1253 filp_close(file, (fl_owner_t)lo);
1254 nfs4_free_ol_stateid(stid);
1258 * Put the persistent reference to an already unhashed generic stateid, while
1259 * holding the cl_lock. If it's the last reference, then put it onto the
1260 * reaplist for later destruction.
1262 static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1263 struct list_head *reaplist)
1265 struct nfs4_stid *s = &stp->st_stid;
1266 struct nfs4_client *clp = s->sc_client;
1268 lockdep_assert_held(&clp->cl_lock);
1270 WARN_ON_ONCE(!list_empty(&stp->st_locks));
1272 if (!refcount_dec_and_test(&s->sc_count)) {
1273 wake_up_all(&close_wq);
1277 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1278 list_add(&stp->st_locks, reaplist);
1281 static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
1283 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1285 if (!unhash_ol_stateid(stp))
1287 list_del_init(&stp->st_locks);
1288 nfs4_unhash_stid(&stp->st_stid);
1292 static void release_lock_stateid(struct nfs4_ol_stateid *stp)
1294 struct nfs4_client *clp = stp->st_stid.sc_client;
1297 spin_lock(&clp->cl_lock);
1298 unhashed = unhash_lock_stateid(stp);
1299 spin_unlock(&clp->cl_lock);
1301 nfs4_put_stid(&stp->st_stid);
1304 static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
1306 struct nfs4_client *clp = lo->lo_owner.so_client;
1308 lockdep_assert_held(&clp->cl_lock);
1310 list_del_init(&lo->lo_owner.so_strhash);
1314 * Free a list of generic stateids that were collected earlier after being
1318 free_ol_stateid_reaplist(struct list_head *reaplist)
1320 struct nfs4_ol_stateid *stp;
1321 struct nfs4_file *fp;
1325 while (!list_empty(reaplist)) {
1326 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1328 list_del(&stp->st_locks);
1329 fp = stp->st_stid.sc_file;
1330 stp->st_stid.sc_free(&stp->st_stid);
1336 static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1337 struct list_head *reaplist)
1339 struct nfs4_ol_stateid *stp;
1341 lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1343 while (!list_empty(&open_stp->st_locks)) {
1344 stp = list_entry(open_stp->st_locks.next,
1345 struct nfs4_ol_stateid, st_locks);
1346 WARN_ON(!unhash_lock_stateid(stp));
1347 put_ol_stateid_locked(stp, reaplist);
1351 static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
1352 struct list_head *reaplist)
1354 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1356 if (!unhash_ol_stateid(stp))
1358 release_open_stateid_locks(stp, reaplist);
1362 static void release_open_stateid(struct nfs4_ol_stateid *stp)
1364 LIST_HEAD(reaplist);
1366 spin_lock(&stp->st_stid.sc_client->cl_lock);
1367 if (unhash_open_stateid(stp, &reaplist))
1368 put_ol_stateid_locked(stp, &reaplist);
1369 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1370 free_ol_stateid_reaplist(&reaplist);
1373 static void unhash_openowner_locked(struct nfs4_openowner *oo)
1375 struct nfs4_client *clp = oo->oo_owner.so_client;
1377 lockdep_assert_held(&clp->cl_lock);
1379 list_del_init(&oo->oo_owner.so_strhash);
1380 list_del_init(&oo->oo_perclient);
1383 static void release_last_closed_stateid(struct nfs4_openowner *oo)
1385 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1387 struct nfs4_ol_stateid *s;
1389 spin_lock(&nn->client_lock);
1390 s = oo->oo_last_closed_stid;
1392 list_del_init(&oo->oo_close_lru);
1393 oo->oo_last_closed_stid = NULL;
1395 spin_unlock(&nn->client_lock);
1397 nfs4_put_stid(&s->st_stid);
1400 static void release_openowner(struct nfs4_openowner *oo)
1402 struct nfs4_ol_stateid *stp;
1403 struct nfs4_client *clp = oo->oo_owner.so_client;
1404 struct list_head reaplist;
1406 INIT_LIST_HEAD(&reaplist);
1408 spin_lock(&clp->cl_lock);
1409 unhash_openowner_locked(oo);
1410 while (!list_empty(&oo->oo_owner.so_stateids)) {
1411 stp = list_first_entry(&oo->oo_owner.so_stateids,
1412 struct nfs4_ol_stateid, st_perstateowner);
1413 if (unhash_open_stateid(stp, &reaplist))
1414 put_ol_stateid_locked(stp, &reaplist);
1416 spin_unlock(&clp->cl_lock);
1417 free_ol_stateid_reaplist(&reaplist);
1418 release_last_closed_stateid(oo);
1419 nfs4_put_stateowner(&oo->oo_owner);
1423 hash_sessionid(struct nfs4_sessionid *sessionid)
1425 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1427 return sid->sequence % SESSION_HASH_SIZE;
1430 #ifdef CONFIG_SUNRPC_DEBUG
1432 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1434 u32 *ptr = (u32 *)(&sessionid->data[0]);
1435 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1439 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1445 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1446 * won't be used for replay.
1448 void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1450 struct nfs4_stateowner *so = cstate->replay_owner;
1452 if (nfserr == nfserr_replay_me)
1455 if (!seqid_mutating_err(ntohl(nfserr))) {
1456 nfsd4_cstate_clear_replay(cstate);
1461 if (so->so_is_open_owner)
1462 release_last_closed_stateid(openowner(so));
1468 gen_sessionid(struct nfsd4_session *ses)
1470 struct nfs4_client *clp = ses->se_client;
1471 struct nfsd4_sessionid *sid;
1473 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1474 sid->clientid = clp->cl_clientid;
1475 sid->sequence = current_sessionid++;
1480 * The protocol defines ca_maxresponssize_cached to include the size of
1481 * the rpc header, but all we need to cache is the data starting after
1482 * the end of the initial SEQUENCE operation--the rest we regenerate
1483 * each time. Therefore we can advertise a ca_maxresponssize_cached
1484 * value that is the number of bytes in our cache plus a few additional
1485 * bytes. In order to stay on the safe side, and not promise more than
1486 * we can cache, those additional bytes must be the minimum possible: 24
1487 * bytes of rpc header (xid through accept state, with AUTH_NULL
1488 * verifier), 12 for the compound header (with zero-length tag), and 44
1489 * for the SEQUENCE op response:
1491 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1494 free_session_slots(struct nfsd4_session *ses)
1498 for (i = 0; i < ses->se_fchannel.maxreqs; i++) {
1499 free_svc_cred(&ses->se_slots[i]->sl_cred);
1500 kfree(ses->se_slots[i]);
1505 * We don't actually need to cache the rpc and session headers, so we
1506 * can allocate a little less for each slot:
1508 static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
1512 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1515 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1516 return size + sizeof(struct nfsd4_slot);
1520 * XXX: If we run out of reserved DRC memory we could (up to a point)
1521 * re-negotiate active sessions and reduce their slot usage to make
1522 * room for new connections. For now we just fail the create session.
1524 static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
1526 u32 slotsize = slot_bytes(ca);
1527 u32 num = ca->maxreqs;
1528 unsigned long avail, total_avail;
1530 spin_lock(&nfsd_drc_lock);
1531 total_avail = nfsd_drc_max_mem - nfsd_drc_mem_used;
1532 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, total_avail);
1534 * Never use more than a third of the remaining memory,
1535 * unless it's the only way to give this client a slot:
1537 avail = clamp_t(unsigned long, avail, slotsize, total_avail/3);
1538 num = min_t(int, num, avail / slotsize);
1539 nfsd_drc_mem_used += num * slotsize;
1540 spin_unlock(&nfsd_drc_lock);
1545 static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
1547 int slotsize = slot_bytes(ca);
1549 spin_lock(&nfsd_drc_lock);
1550 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
1551 spin_unlock(&nfsd_drc_lock);
1554 static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1555 struct nfsd4_channel_attrs *battrs)
1557 int numslots = fattrs->maxreqs;
1558 int slotsize = slot_bytes(fattrs);
1559 struct nfsd4_session *new;
1562 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
1563 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1564 mem = numslots * sizeof(struct nfsd4_slot *);
1566 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
1569 /* allocate each struct nfsd4_slot and data cache in one piece */
1570 for (i = 0; i < numslots; i++) {
1571 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
1572 if (!new->se_slots[i])
1576 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1577 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1582 kfree(new->se_slots[i]);
1587 static void free_conn(struct nfsd4_conn *c)
1589 svc_xprt_put(c->cn_xprt);
1593 static void nfsd4_conn_lost(struct svc_xpt_user *u)
1595 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1596 struct nfs4_client *clp = c->cn_session->se_client;
1598 spin_lock(&clp->cl_lock);
1599 if (!list_empty(&c->cn_persession)) {
1600 list_del(&c->cn_persession);
1603 nfsd4_probe_callback(clp);
1604 spin_unlock(&clp->cl_lock);
1607 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
1609 struct nfsd4_conn *conn;
1611 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1614 svc_xprt_get(rqstp->rq_xprt);
1615 conn->cn_xprt = rqstp->rq_xprt;
1616 conn->cn_flags = flags;
1617 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1621 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1623 conn->cn_session = ses;
1624 list_add(&conn->cn_persession, &ses->se_conns);
1627 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1629 struct nfs4_client *clp = ses->se_client;
1631 spin_lock(&clp->cl_lock);
1632 __nfsd4_hash_conn(conn, ses);
1633 spin_unlock(&clp->cl_lock);
1636 static int nfsd4_register_conn(struct nfsd4_conn *conn)
1638 conn->cn_xpt_user.callback = nfsd4_conn_lost;
1639 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
1642 static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
1646 nfsd4_hash_conn(conn, ses);
1647 ret = nfsd4_register_conn(conn);
1649 /* oops; xprt is already down: */
1650 nfsd4_conn_lost(&conn->cn_xpt_user);
1651 /* We may have gained or lost a callback channel: */
1652 nfsd4_probe_callback_sync(ses->se_client);
1655 static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
1657 u32 dir = NFS4_CDFC4_FORE;
1659 if (cses->flags & SESSION4_BACK_CHAN)
1660 dir |= NFS4_CDFC4_BACK;
1661 return alloc_conn(rqstp, dir);
1664 /* must be called under client_lock */
1665 static void nfsd4_del_conns(struct nfsd4_session *s)
1667 struct nfs4_client *clp = s->se_client;
1668 struct nfsd4_conn *c;
1670 spin_lock(&clp->cl_lock);
1671 while (!list_empty(&s->se_conns)) {
1672 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1673 list_del_init(&c->cn_persession);
1674 spin_unlock(&clp->cl_lock);
1676 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1679 spin_lock(&clp->cl_lock);
1681 spin_unlock(&clp->cl_lock);
1684 static void __free_session(struct nfsd4_session *ses)
1686 free_session_slots(ses);
1690 static void free_session(struct nfsd4_session *ses)
1692 nfsd4_del_conns(ses);
1693 nfsd4_put_drc_mem(&ses->se_fchannel);
1694 __free_session(ses);
1697 static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
1700 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1702 new->se_client = clp;
1705 INIT_LIST_HEAD(&new->se_conns);
1707 new->se_cb_seq_nr = 1;
1708 new->se_flags = cses->flags;
1709 new->se_cb_prog = cses->callback_prog;
1710 new->se_cb_sec = cses->cb_sec;
1711 atomic_set(&new->se_ref, 0);
1712 idx = hash_sessionid(&new->se_sessionid);
1713 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
1714 spin_lock(&clp->cl_lock);
1715 list_add(&new->se_perclnt, &clp->cl_sessions);
1716 spin_unlock(&clp->cl_lock);
1719 struct sockaddr *sa = svc_addr(rqstp);
1721 * This is a little silly; with sessions there's no real
1722 * use for the callback address. Use the peer address
1723 * as a reasonable default for now, but consider fixing
1724 * the rpc client not to require an address in the
1727 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1728 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
1732 /* caller must hold client_lock */
1733 static struct nfsd4_session *
1734 __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
1736 struct nfsd4_session *elem;
1738 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1740 lockdep_assert_held(&nn->client_lock);
1742 dump_sessionid(__func__, sessionid);
1743 idx = hash_sessionid(sessionid);
1744 /* Search in the appropriate list */
1745 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
1746 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1747 NFS4_MAX_SESSIONID_LEN)) {
1752 dprintk("%s: session not found\n", __func__);
1756 static struct nfsd4_session *
1757 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1760 struct nfsd4_session *session;
1761 __be32 status = nfserr_badsession;
1763 session = __find_in_sessionid_hashtbl(sessionid, net);
1766 status = nfsd4_get_session_locked(session);
1774 /* caller must hold client_lock */
1776 unhash_session(struct nfsd4_session *ses)
1778 struct nfs4_client *clp = ses->se_client;
1779 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1781 lockdep_assert_held(&nn->client_lock);
1783 list_del(&ses->se_hash);
1784 spin_lock(&ses->se_client->cl_lock);
1785 list_del(&ses->se_perclnt);
1786 spin_unlock(&ses->se_client->cl_lock);
1789 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1791 STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
1794 * We're assuming the clid was not given out from a boot
1795 * precisely 2^32 (about 136 years) before this one. That seems
1796 * a safe assumption:
1798 if (clid->cl_boot == (u32)nn->boot_time)
1800 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
1801 clid->cl_boot, clid->cl_id, nn->boot_time);
1806 * XXX Should we use a slab cache ?
1807 * This type of memory management is somewhat inefficient, but we use it
1808 * anyway since SETCLIENTID is not a common operation.
1810 static struct nfs4_client *alloc_client(struct xdr_netobj name)
1812 struct nfs4_client *clp;
1815 clp = kmem_cache_zalloc(client_slab, GFP_KERNEL);
1818 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
1819 if (clp->cl_name.data == NULL)
1821 clp->cl_ownerstr_hashtbl = kmalloc_array(OWNER_HASH_SIZE,
1822 sizeof(struct list_head),
1824 if (!clp->cl_ownerstr_hashtbl)
1825 goto err_no_hashtbl;
1826 for (i = 0; i < OWNER_HASH_SIZE; i++)
1827 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
1828 clp->cl_name.len = name.len;
1829 INIT_LIST_HEAD(&clp->cl_sessions);
1830 idr_init(&clp->cl_stateids);
1831 atomic_set(&clp->cl_refcount, 0);
1832 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1833 INIT_LIST_HEAD(&clp->cl_idhash);
1834 INIT_LIST_HEAD(&clp->cl_openowners);
1835 INIT_LIST_HEAD(&clp->cl_delegations);
1836 INIT_LIST_HEAD(&clp->cl_lru);
1837 INIT_LIST_HEAD(&clp->cl_revoked);
1838 #ifdef CONFIG_NFSD_PNFS
1839 INIT_LIST_HEAD(&clp->cl_lo_states);
1841 spin_lock_init(&clp->cl_lock);
1842 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1845 kfree(clp->cl_name.data);
1847 kmem_cache_free(client_slab, clp);
1852 free_client(struct nfs4_client *clp)
1854 while (!list_empty(&clp->cl_sessions)) {
1855 struct nfsd4_session *ses;
1856 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1858 list_del(&ses->se_perclnt);
1859 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1862 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
1863 free_svc_cred(&clp->cl_cred);
1864 kfree(clp->cl_ownerstr_hashtbl);
1865 kfree(clp->cl_name.data);
1866 idr_destroy(&clp->cl_stateids);
1867 kmem_cache_free(client_slab, clp);
1870 /* must be called under the client_lock */
1872 unhash_client_locked(struct nfs4_client *clp)
1874 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1875 struct nfsd4_session *ses;
1877 lockdep_assert_held(&nn->client_lock);
1879 /* Mark the client as expired! */
1881 /* Make it invisible */
1882 if (!list_empty(&clp->cl_idhash)) {
1883 list_del_init(&clp->cl_idhash);
1884 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1885 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1887 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1889 list_del_init(&clp->cl_lru);
1890 spin_lock(&clp->cl_lock);
1891 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1892 list_del_init(&ses->se_hash);
1893 spin_unlock(&clp->cl_lock);
1897 unhash_client(struct nfs4_client *clp)
1899 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1901 spin_lock(&nn->client_lock);
1902 unhash_client_locked(clp);
1903 spin_unlock(&nn->client_lock);
1906 static __be32 mark_client_expired_locked(struct nfs4_client *clp)
1908 if (atomic_read(&clp->cl_refcount))
1909 return nfserr_jukebox;
1910 unhash_client_locked(clp);
1915 __destroy_client(struct nfs4_client *clp)
1918 struct nfs4_openowner *oo;
1919 struct nfs4_delegation *dp;
1920 struct list_head reaplist;
1922 INIT_LIST_HEAD(&reaplist);
1923 spin_lock(&state_lock);
1924 while (!list_empty(&clp->cl_delegations)) {
1925 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
1926 WARN_ON(!unhash_delegation_locked(dp));
1927 list_add(&dp->dl_recall_lru, &reaplist);
1929 spin_unlock(&state_lock);
1930 while (!list_empty(&reaplist)) {
1931 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
1932 list_del_init(&dp->dl_recall_lru);
1933 destroy_unhashed_deleg(dp);
1935 while (!list_empty(&clp->cl_revoked)) {
1936 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
1937 list_del_init(&dp->dl_recall_lru);
1938 nfs4_put_stid(&dp->dl_stid);
1940 while (!list_empty(&clp->cl_openowners)) {
1941 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1942 nfs4_get_stateowner(&oo->oo_owner);
1943 release_openowner(oo);
1945 for (i = 0; i < OWNER_HASH_SIZE; i++) {
1946 struct nfs4_stateowner *so, *tmp;
1948 list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i],
1950 /* Should be no openowners at this point */
1951 WARN_ON_ONCE(so->so_is_open_owner);
1952 remove_blocked_locks(lockowner(so));
1955 nfsd4_return_all_client_layouts(clp);
1956 nfsd4_shutdown_callback(clp);
1957 if (clp->cl_cb_conn.cb_xprt)
1958 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
1963 destroy_client(struct nfs4_client *clp)
1966 __destroy_client(clp);
1969 static void expire_client(struct nfs4_client *clp)
1972 nfsd4_client_record_remove(clp);
1973 __destroy_client(clp);
1976 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1978 memcpy(target->cl_verifier.data, source->data,
1979 sizeof(target->cl_verifier.data));
1982 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1984 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1985 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1988 static int copy_cred(struct svc_cred *target, struct svc_cred *source)
1990 target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL);
1991 target->cr_raw_principal = kstrdup(source->cr_raw_principal,
1993 target->cr_targ_princ = kstrdup(source->cr_targ_princ, GFP_KERNEL);
1994 if ((source->cr_principal && !target->cr_principal) ||
1995 (source->cr_raw_principal && !target->cr_raw_principal) ||
1996 (source->cr_targ_princ && !target->cr_targ_princ))
1999 target->cr_flavor = source->cr_flavor;
2000 target->cr_uid = source->cr_uid;
2001 target->cr_gid = source->cr_gid;
2002 target->cr_group_info = source->cr_group_info;
2003 get_group_info(target->cr_group_info);
2004 target->cr_gss_mech = source->cr_gss_mech;
2005 if (source->cr_gss_mech)
2006 gss_mech_get(source->cr_gss_mech);
2011 compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
2013 if (o1->len < o2->len)
2015 if (o1->len > o2->len)
2017 return memcmp(o1->data, o2->data, o1->len);
2020 static int same_name(const char *n1, const char *n2)
2022 return 0 == memcmp(n1, n2, HEXDIR_LEN);
2026 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
2028 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
2032 same_clid(clientid_t *cl1, clientid_t *cl2)
2034 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
2037 static bool groups_equal(struct group_info *g1, struct group_info *g2)
2041 if (g1->ngroups != g2->ngroups)
2043 for (i=0; i<g1->ngroups; i++)
2044 if (!gid_eq(g1->gid[i], g2->gid[i]))
2050 * RFC 3530 language requires clid_inuse be returned when the
2051 * "principal" associated with a requests differs from that previously
2052 * used. We use uid, gid's, and gss principal string as our best
2053 * approximation. We also don't want to allow non-gss use of a client
2054 * established using gss: in theory cr_principal should catch that
2055 * change, but in practice cr_principal can be null even in the gss case
2056 * since gssd doesn't always pass down a principal string.
2058 static bool is_gss_cred(struct svc_cred *cr)
2060 /* Is cr_flavor one of the gss "pseudoflavors"?: */
2061 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
2066 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
2068 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
2069 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
2070 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
2071 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
2073 /* XXX: check that cr_targ_princ fields match ? */
2074 if (cr1->cr_principal == cr2->cr_principal)
2076 if (!cr1->cr_principal || !cr2->cr_principal)
2078 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
2081 static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
2083 struct svc_cred *cr = &rqstp->rq_cred;
2086 if (!cr->cr_gss_mech)
2088 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
2089 return service == RPC_GSS_SVC_INTEGRITY ||
2090 service == RPC_GSS_SVC_PRIVACY;
2093 bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
2095 struct svc_cred *cr = &rqstp->rq_cred;
2097 if (!cl->cl_mach_cred)
2099 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
2101 if (!svc_rqst_integrity_protected(rqstp))
2103 if (cl->cl_cred.cr_raw_principal)
2104 return 0 == strcmp(cl->cl_cred.cr_raw_principal,
2105 cr->cr_raw_principal);
2106 if (!cr->cr_principal)
2108 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
2111 static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
2116 * This is opaque to client, so no need to byte-swap. Use
2117 * __force to keep sparse happy
2119 verf[0] = (__force __be32)get_seconds();
2120 verf[1] = (__force __be32)nn->clverifier_counter++;
2121 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
2124 static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
2126 clp->cl_clientid.cl_boot = nn->boot_time;
2127 clp->cl_clientid.cl_id = nn->clientid_counter++;
2128 gen_confirm(clp, nn);
2131 static struct nfs4_stid *
2132 find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
2134 struct nfs4_stid *ret;
2136 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
2137 if (!ret || !ret->sc_type)
2142 static struct nfs4_stid *
2143 find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
2145 struct nfs4_stid *s;
2147 spin_lock(&cl->cl_lock);
2148 s = find_stateid_locked(cl, t);
2150 if (typemask & s->sc_type)
2151 refcount_inc(&s->sc_count);
2155 spin_unlock(&cl->cl_lock);
2159 static struct nfs4_client *create_client(struct xdr_netobj name,
2160 struct svc_rqst *rqstp, nfs4_verifier *verf)
2162 struct nfs4_client *clp;
2163 struct sockaddr *sa = svc_addr(rqstp);
2165 struct net *net = SVC_NET(rqstp);
2167 clp = alloc_client(name);
2171 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
2176 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
2177 clp->cl_time = get_seconds();
2178 clear_bit(0, &clp->cl_cb_slot_busy);
2179 copy_verf(clp, verf);
2180 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
2181 clp->cl_cb_session = NULL;
2187 add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
2189 struct rb_node **new = &(root->rb_node), *parent = NULL;
2190 struct nfs4_client *clp;
2193 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
2196 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
2197 new = &((*new)->rb_left);
2199 new = &((*new)->rb_right);
2202 rb_link_node(&new_clp->cl_namenode, parent, new);
2203 rb_insert_color(&new_clp->cl_namenode, root);
2206 static struct nfs4_client *
2207 find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2210 struct rb_node *node = root->rb_node;
2211 struct nfs4_client *clp;
2214 clp = rb_entry(node, struct nfs4_client, cl_namenode);
2215 cmp = compare_blob(&clp->cl_name, name);
2217 node = node->rb_left;
2219 node = node->rb_right;
2227 add_to_unconfirmed(struct nfs4_client *clp)
2229 unsigned int idhashval;
2230 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2232 lockdep_assert_held(&nn->client_lock);
2234 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2235 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
2236 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
2237 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
2238 renew_client_locked(clp);
2242 move_to_confirmed(struct nfs4_client *clp)
2244 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
2245 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2247 lockdep_assert_held(&nn->client_lock);
2249 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
2250 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
2251 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
2252 add_clp_to_name_tree(clp, &nn->conf_name_tree);
2253 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2254 renew_client_locked(clp);
2257 static struct nfs4_client *
2258 find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
2260 struct nfs4_client *clp;
2261 unsigned int idhashval = clientid_hashval(clid->cl_id);
2263 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
2264 if (same_clid(&clp->cl_clientid, clid)) {
2265 if ((bool)clp->cl_minorversion != sessions)
2267 renew_client_locked(clp);
2274 static struct nfs4_client *
2275 find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2277 struct list_head *tbl = nn->conf_id_hashtbl;
2279 lockdep_assert_held(&nn->client_lock);
2280 return find_client_in_id_table(tbl, clid, sessions);
2283 static struct nfs4_client *
2284 find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2286 struct list_head *tbl = nn->unconf_id_hashtbl;
2288 lockdep_assert_held(&nn->client_lock);
2289 return find_client_in_id_table(tbl, clid, sessions);
2292 static bool clp_used_exchangeid(struct nfs4_client *clp)
2294 return clp->cl_exchange_flags != 0;
2297 static struct nfs4_client *
2298 find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
2300 lockdep_assert_held(&nn->client_lock);
2301 return find_clp_in_name_tree(name, &nn->conf_name_tree);
2304 static struct nfs4_client *
2305 find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
2307 lockdep_assert_held(&nn->client_lock);
2308 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
2312 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
2314 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
2315 struct sockaddr *sa = svc_addr(rqstp);
2316 u32 scopeid = rpc_get_scope_id(sa);
2317 unsigned short expected_family;
2319 /* Currently, we only support tcp and tcp6 for the callback channel */
2320 if (se->se_callback_netid_len == 3 &&
2321 !memcmp(se->se_callback_netid_val, "tcp", 3))
2322 expected_family = AF_INET;
2323 else if (se->se_callback_netid_len == 4 &&
2324 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2325 expected_family = AF_INET6;
2329 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
2330 se->se_callback_addr_len,
2331 (struct sockaddr *)&conn->cb_addr,
2332 sizeof(conn->cb_addr));
2334 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
2337 if (conn->cb_addr.ss_family == AF_INET6)
2338 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
2340 conn->cb_prog = se->se_callback_prog;
2341 conn->cb_ident = se->se_callback_ident;
2342 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
2345 conn->cb_addr.ss_family = AF_UNSPEC;
2346 conn->cb_addrlen = 0;
2347 dprintk("NFSD: this client (clientid %08x/%08x) "
2348 "will not receive delegations\n",
2349 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2355 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
2358 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2360 struct xdr_buf *buf = resp->xdr.buf;
2361 struct nfsd4_slot *slot = resp->cstate.slot;
2364 dprintk("--> %s slot %p\n", __func__, slot);
2366 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
2367 slot->sl_opcnt = resp->opcnt;
2368 slot->sl_status = resp->cstate.status;
2369 free_svc_cred(&slot->sl_cred);
2370 copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred);
2372 if (!nfsd4_cache_this(resp)) {
2373 slot->sl_flags &= ~NFSD4_SLOT_CACHED;
2376 slot->sl_flags |= NFSD4_SLOT_CACHED;
2378 base = resp->cstate.data_offset;
2379 slot->sl_datalen = buf->len - base;
2380 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
2381 WARN(1, "%s: sessions DRC could not cache compound\n",
2387 * Encode the replay sequence operation from the slot values.
2388 * If cachethis is FALSE encode the uncached rep error on the next
2389 * operation which sets resp->p and increments resp->opcnt for
2390 * nfs4svc_encode_compoundres.
2394 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2395 struct nfsd4_compoundres *resp)
2397 struct nfsd4_op *op;
2398 struct nfsd4_slot *slot = resp->cstate.slot;
2400 /* Encode the replayed sequence operation */
2401 op = &args->ops[resp->opcnt - 1];
2402 nfsd4_encode_operation(resp, op);
2404 if (slot->sl_flags & NFSD4_SLOT_CACHED)
2406 if (args->opcnt == 1) {
2408 * The original operation wasn't a solo sequence--we
2409 * always cache those--so this retry must not match the
2412 op->status = nfserr_seq_false_retry;
2414 op = &args->ops[resp->opcnt++];
2415 op->status = nfserr_retry_uncached_rep;
2416 nfsd4_encode_operation(resp, op);
2422 * The sequence operation is not cached because we can use the slot and
2426 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2427 struct nfsd4_sequence *seq)
2429 struct nfsd4_slot *slot = resp->cstate.slot;
2430 struct xdr_stream *xdr = &resp->xdr;
2434 dprintk("--> %s slot %p\n", __func__, slot);
2436 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
2440 p = xdr_reserve_space(xdr, slot->sl_datalen);
2443 return nfserr_serverfault;
2445 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2446 xdr_commit_encode(xdr);
2448 resp->opcnt = slot->sl_opcnt;
2449 return slot->sl_status;
2453 * Set the exchange_id flags returned by the server.
2456 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2458 #ifdef CONFIG_NFSD_PNFS
2459 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
2461 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
2464 /* Referrals are supported, Migration is not. */
2465 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2467 /* set the wire flags to return to client. */
2468 clid->flags = new->cl_exchange_flags;
2471 static bool client_has_openowners(struct nfs4_client *clp)
2473 struct nfs4_openowner *oo;
2475 list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
2476 if (!list_empty(&oo->oo_owner.so_stateids))
2482 static bool client_has_state(struct nfs4_client *clp)
2484 return client_has_openowners(clp)
2485 #ifdef CONFIG_NFSD_PNFS
2486 || !list_empty(&clp->cl_lo_states)
2488 || !list_empty(&clp->cl_delegations)
2489 || !list_empty(&clp->cl_sessions);
2493 nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2494 union nfsd4_op_u *u)
2496 struct nfsd4_exchange_id *exid = &u->exchange_id;
2497 struct nfs4_client *conf, *new;
2498 struct nfs4_client *unconf = NULL;
2500 char addr_str[INET6_ADDRSTRLEN];
2501 nfs4_verifier verf = exid->verifier;
2502 struct sockaddr *sa = svc_addr(rqstp);
2503 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
2504 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2506 rpc_ntop(sa, addr_str, sizeof(addr_str));
2507 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
2508 "ip_addr=%s flags %x, spa_how %d\n",
2509 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
2510 addr_str, exid->flags, exid->spa_how);
2512 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
2513 return nfserr_inval;
2515 new = create_client(exid->clname, rqstp, &verf);
2517 return nfserr_jukebox;
2519 switch (exid->spa_how) {
2521 exid->spo_must_enforce[0] = 0;
2522 exid->spo_must_enforce[1] = (
2523 1 << (OP_BIND_CONN_TO_SESSION - 32) |
2524 1 << (OP_EXCHANGE_ID - 32) |
2525 1 << (OP_CREATE_SESSION - 32) |
2526 1 << (OP_DESTROY_SESSION - 32) |
2527 1 << (OP_DESTROY_CLIENTID - 32));
2529 exid->spo_must_allow[0] &= (1 << (OP_CLOSE) |
2530 1 << (OP_OPEN_DOWNGRADE) |
2532 1 << (OP_DELEGRETURN));
2534 exid->spo_must_allow[1] &= (
2535 1 << (OP_TEST_STATEID - 32) |
2536 1 << (OP_FREE_STATEID - 32));
2537 if (!svc_rqst_integrity_protected(rqstp)) {
2538 status = nfserr_inval;
2542 * Sometimes userspace doesn't give us a principal.
2543 * Which is a bug, really. Anyway, we can't enforce
2544 * MACH_CRED in that case, better to give up now:
2546 if (!new->cl_cred.cr_principal &&
2547 !new->cl_cred.cr_raw_principal) {
2548 status = nfserr_serverfault;
2551 new->cl_mach_cred = true;
2554 default: /* checked by xdr code */
2557 status = nfserr_encr_alg_unsupp;
2561 /* Cases below refer to rfc 5661 section 18.35.4: */
2562 spin_lock(&nn->client_lock);
2563 conf = find_confirmed_client_by_name(&exid->clname, nn);
2565 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2566 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2569 if (!clp_used_exchangeid(conf)) { /* buggy client */
2570 status = nfserr_inval;
2573 if (!nfsd4_mach_creds_match(conf, rqstp)) {
2574 status = nfserr_wrong_cred;
2577 if (!creds_match) { /* case 9 */
2578 status = nfserr_perm;
2581 if (!verfs_match) { /* case 8 */
2582 status = nfserr_not_same;
2586 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
2589 if (!creds_match) { /* case 3 */
2590 if (client_has_state(conf)) {
2591 status = nfserr_clid_inuse;
2596 if (verfs_match) { /* case 2 */
2597 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
2600 /* case 5, client reboot */
2605 if (update) { /* case 7 */
2606 status = nfserr_noent;
2610 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
2611 if (unconf) /* case 4, possible retry or client restart */
2612 unhash_client_locked(unconf);
2614 /* case 1 (normal case) */
2617 status = mark_client_expired_locked(conf);
2621 new->cl_minorversion = cstate->minorversion;
2622 new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0];
2623 new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1];
2626 add_to_unconfirmed(new);
2629 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
2630 exid->clientid.cl_id = conf->cl_clientid.cl_id;
2632 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
2633 nfsd4_set_ex_flags(conf, exid);
2635 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
2636 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
2640 spin_unlock(&nn->client_lock);
2645 expire_client(unconf);
2650 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
2652 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2655 /* The slot is in use, and no response has been sent. */
2657 if (seqid == slot_seqid)
2658 return nfserr_jukebox;
2660 return nfserr_seq_misordered;
2662 /* Note unsigned 32-bit arithmetic handles wraparound: */
2663 if (likely(seqid == slot_seqid + 1))
2665 if (seqid == slot_seqid)
2666 return nfserr_replay_cache;
2667 return nfserr_seq_misordered;
2671 * Cache the create session result into the create session single DRC
2672 * slot cache by saving the xdr structure. sl_seqid has been set.
2673 * Do this for solo or embedded create session operations.
2676 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
2677 struct nfsd4_clid_slot *slot, __be32 nfserr)
2679 slot->sl_status = nfserr;
2680 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2684 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2685 struct nfsd4_clid_slot *slot)
2687 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2688 return slot->sl_status;
2691 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2692 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2693 1 + /* MIN tag is length with zero, only length */ \
2694 3 + /* version, opcount, opcode */ \
2695 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2696 /* seqid, slotID, slotID, cache */ \
2697 4 ) * sizeof(__be32))
2699 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2700 2 + /* verifier: AUTH_NULL, length 0 */\
2702 1 + /* MIN tag is length with zero, only length */ \
2703 3 + /* opcount, opcode, opstatus*/ \
2704 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2705 /* seqid, slotID, slotID, slotID, status */ \
2706 5 ) * sizeof(__be32))
2708 static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
2710 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2712 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2713 return nfserr_toosmall;
2714 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2715 return nfserr_toosmall;
2716 ca->headerpadsz = 0;
2717 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2718 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2719 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2720 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2721 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2722 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2724 * Note decreasing slot size below client's request may make it
2725 * difficult for client to function correctly, whereas
2726 * decreasing the number of slots will (just?) affect
2727 * performance. When short on memory we therefore prefer to
2728 * decrease number of slots instead of their size. Clients that
2729 * request larger slots than they need will get poor results:
2731 ca->maxreqs = nfsd4_get_drc_mem(ca);
2733 return nfserr_jukebox;
2739 * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
2740 * These are based on similar macros in linux/sunrpc/msg_prot.h .
2742 #define RPC_MAX_HEADER_WITH_AUTH_SYS \
2743 (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
2745 #define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
2746 (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
2748 #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2749 RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
2750 #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2751 RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
2754 static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2756 ca->headerpadsz = 0;
2758 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
2759 return nfserr_toosmall;
2760 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
2761 return nfserr_toosmall;
2762 ca->maxresp_cached = 0;
2764 return nfserr_toosmall;
2769 static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2771 switch (cbs->flavor) {
2777 * GSS case: the spec doesn't allow us to return this
2778 * error. But it also doesn't allow us not to support
2780 * I'd rather this fail hard than return some error the
2781 * client might think it can already handle:
2783 return nfserr_encr_alg_unsupp;
2788 nfsd4_create_session(struct svc_rqst *rqstp,
2789 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
2791 struct nfsd4_create_session *cr_ses = &u->create_session;
2792 struct sockaddr *sa = svc_addr(rqstp);
2793 struct nfs4_client *conf, *unconf;
2794 struct nfs4_client *old = NULL;
2795 struct nfsd4_session *new;
2796 struct nfsd4_conn *conn;
2797 struct nfsd4_clid_slot *cs_slot = NULL;
2799 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2801 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2802 return nfserr_inval;
2803 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2806 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
2809 status = check_backchannel_attrs(&cr_ses->back_channel);
2811 goto out_release_drc_mem;
2812 status = nfserr_jukebox;
2813 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
2815 goto out_release_drc_mem;
2816 conn = alloc_conn_from_crses(rqstp, cr_ses);
2818 goto out_free_session;
2820 spin_lock(&nn->client_lock);
2821 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
2822 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
2823 WARN_ON_ONCE(conf && unconf);
2826 status = nfserr_wrong_cred;
2827 if (!nfsd4_mach_creds_match(conf, rqstp))
2829 cs_slot = &conf->cl_cs_slot;
2830 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2832 if (status == nfserr_replay_cache)
2833 status = nfsd4_replay_create_session(cr_ses, cs_slot);
2836 } else if (unconf) {
2837 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
2838 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
2839 status = nfserr_clid_inuse;
2842 status = nfserr_wrong_cred;
2843 if (!nfsd4_mach_creds_match(unconf, rqstp))
2845 cs_slot = &unconf->cl_cs_slot;
2846 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2848 /* an unconfirmed replay returns misordered */
2849 status = nfserr_seq_misordered;
2852 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
2854 status = mark_client_expired_locked(old);
2860 move_to_confirmed(unconf);
2863 status = nfserr_stale_clientid;
2867 /* Persistent sessions are not supported */
2868 cr_ses->flags &= ~SESSION4_PERSIST;
2869 /* Upshifting from TCP to RDMA is not supported */
2870 cr_ses->flags &= ~SESSION4_RDMA;
2872 init_session(rqstp, new, conf, cr_ses);
2873 nfsd4_get_session_locked(new);
2875 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
2876 NFS4_MAX_SESSIONID_LEN);
2877 cs_slot->sl_seqid++;
2878 cr_ses->seqid = cs_slot->sl_seqid;
2880 /* cache solo and embedded create sessions under the client_lock */
2881 nfsd4_cache_create_session(cr_ses, cs_slot, status);
2882 spin_unlock(&nn->client_lock);
2883 /* init connection and backchannel */
2884 nfsd4_init_conn(rqstp, conn, new);
2885 nfsd4_put_session(new);
2890 spin_unlock(&nn->client_lock);
2895 __free_session(new);
2896 out_release_drc_mem:
2897 nfsd4_put_drc_mem(&cr_ses->fore_channel);
2901 static __be32 nfsd4_map_bcts_dir(u32 *dir)
2904 case NFS4_CDFC4_FORE:
2905 case NFS4_CDFC4_BACK:
2907 case NFS4_CDFC4_FORE_OR_BOTH:
2908 case NFS4_CDFC4_BACK_OR_BOTH:
2909 *dir = NFS4_CDFC4_BOTH;
2912 return nfserr_inval;
2915 __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp,
2916 struct nfsd4_compound_state *cstate,
2917 union nfsd4_op_u *u)
2919 struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl;
2920 struct nfsd4_session *session = cstate->session;
2921 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2924 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2927 spin_lock(&nn->client_lock);
2928 session->se_cb_prog = bc->bc_cb_program;
2929 session->se_cb_sec = bc->bc_cb_sec;
2930 spin_unlock(&nn->client_lock);
2932 nfsd4_probe_callback(session->se_client);
2937 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2938 struct nfsd4_compound_state *cstate,
2939 union nfsd4_op_u *u)
2941 struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session;
2943 struct nfsd4_conn *conn;
2944 struct nfsd4_session *session;
2945 struct net *net = SVC_NET(rqstp);
2946 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2948 if (!nfsd4_last_compound_op(rqstp))
2949 return nfserr_not_only_op;
2950 spin_lock(&nn->client_lock);
2951 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
2952 spin_unlock(&nn->client_lock);
2954 goto out_no_session;
2955 status = nfserr_wrong_cred;
2956 if (!nfsd4_mach_creds_match(session->se_client, rqstp))
2958 status = nfsd4_map_bcts_dir(&bcts->dir);
2961 conn = alloc_conn(rqstp, bcts->dir);
2962 status = nfserr_jukebox;
2965 nfsd4_init_conn(rqstp, conn, session);
2968 nfsd4_put_session(session);
2973 static bool nfsd4_compound_in_session(struct nfsd4_compound_state *cstate, struct nfs4_sessionid *sid)
2975 if (!cstate->session)
2977 return !memcmp(sid, &cstate->session->se_sessionid, sizeof(*sid));
2981 nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate,
2982 union nfsd4_op_u *u)
2984 struct nfs4_sessionid *sessionid = &u->destroy_session.sessionid;
2985 struct nfsd4_session *ses;
2987 int ref_held_by_me = 0;
2988 struct net *net = SVC_NET(r);
2989 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2991 status = nfserr_not_only_op;
2992 if (nfsd4_compound_in_session(cstate, sessionid)) {
2993 if (!nfsd4_last_compound_op(r))
2997 dump_sessionid(__func__, sessionid);
2998 spin_lock(&nn->client_lock);
2999 ses = find_in_sessionid_hashtbl(sessionid, net, &status);
3001 goto out_client_lock;
3002 status = nfserr_wrong_cred;
3003 if (!nfsd4_mach_creds_match(ses->se_client, r))
3004 goto out_put_session;
3005 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
3007 goto out_put_session;
3008 unhash_session(ses);
3009 spin_unlock(&nn->client_lock);
3011 nfsd4_probe_callback_sync(ses->se_client);
3013 spin_lock(&nn->client_lock);
3016 nfsd4_put_session_locked(ses);
3018 spin_unlock(&nn->client_lock);
3023 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
3025 struct nfsd4_conn *c;
3027 list_for_each_entry(c, &s->se_conns, cn_persession) {
3028 if (c->cn_xprt == xpt) {
3035 static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
3037 struct nfs4_client *clp = ses->se_client;
3038 struct nfsd4_conn *c;
3039 __be32 status = nfs_ok;
3042 spin_lock(&clp->cl_lock);
3043 c = __nfsd4_find_conn(new->cn_xprt, ses);
3046 status = nfserr_conn_not_bound_to_session;
3047 if (clp->cl_mach_cred)
3049 __nfsd4_hash_conn(new, ses);
3050 spin_unlock(&clp->cl_lock);
3051 ret = nfsd4_register_conn(new);
3053 /* oops; xprt is already down: */
3054 nfsd4_conn_lost(&new->cn_xpt_user);
3057 spin_unlock(&clp->cl_lock);
3062 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
3064 struct nfsd4_compoundargs *args = rqstp->rq_argp;
3066 return args->opcnt > session->se_fchannel.maxops;
3069 static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
3070 struct nfsd4_session *session)
3072 struct xdr_buf *xb = &rqstp->rq_arg;
3074 return xb->len > session->se_fchannel.maxreq_sz;
3077 static bool replay_matches_cache(struct svc_rqst *rqstp,
3078 struct nfsd4_sequence *seq, struct nfsd4_slot *slot)
3080 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
3082 if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) !=
3083 (bool)seq->cachethis)
3086 * If there's an error then the reply can have fewer ops than
3089 if (slot->sl_opcnt < argp->opcnt && !slot->sl_status)
3092 * But if we cached a reply with *more* ops than the call you're
3093 * sending us now, then this new call is clearly not really a
3094 * replay of the old one:
3096 if (slot->sl_opcnt > argp->opcnt)
3098 /* This is the only check explicitly called by spec: */
3099 if (!same_creds(&rqstp->rq_cred, &slot->sl_cred))
3102 * There may be more comparisons we could actually do, but the
3103 * spec doesn't require us to catch every case where the calls
3104 * don't match (that would require caching the call as well as
3105 * the reply), so we don't bother.
3111 nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3112 union nfsd4_op_u *u)
3114 struct nfsd4_sequence *seq = &u->sequence;
3115 struct nfsd4_compoundres *resp = rqstp->rq_resp;
3116 struct xdr_stream *xdr = &resp->xdr;
3117 struct nfsd4_session *session;
3118 struct nfs4_client *clp;
3119 struct nfsd4_slot *slot;
3120 struct nfsd4_conn *conn;
3123 struct net *net = SVC_NET(rqstp);
3124 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3126 if (resp->opcnt != 1)
3127 return nfserr_sequence_pos;
3130 * Will be either used or freed by nfsd4_sequence_check_conn
3133 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
3135 return nfserr_jukebox;
3137 spin_lock(&nn->client_lock);
3138 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
3140 goto out_no_session;
3141 clp = session->se_client;
3143 status = nfserr_too_many_ops;
3144 if (nfsd4_session_too_many_ops(rqstp, session))
3145 goto out_put_session;
3147 status = nfserr_req_too_big;
3148 if (nfsd4_request_too_big(rqstp, session))
3149 goto out_put_session;
3151 status = nfserr_badslot;
3152 if (seq->slotid >= session->se_fchannel.maxreqs)
3153 goto out_put_session;
3155 slot = session->se_slots[seq->slotid];
3156 dprintk("%s: slotid %d\n", __func__, seq->slotid);
3158 /* We do not negotiate the number of slots yet, so set the
3159 * maxslots to the session maxreqs which is used to encode
3160 * sr_highest_slotid and the sr_target_slot id to maxslots */
3161 seq->maxslots = session->se_fchannel.maxreqs;
3163 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
3164 slot->sl_flags & NFSD4_SLOT_INUSE);
3165 if (status == nfserr_replay_cache) {
3166 status = nfserr_seq_misordered;
3167 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
3168 goto out_put_session;
3169 status = nfserr_seq_false_retry;
3170 if (!replay_matches_cache(rqstp, seq, slot))
3171 goto out_put_session;
3172 cstate->slot = slot;
3173 cstate->session = session;
3175 /* Return the cached reply status and set cstate->status
3176 * for nfsd4_proc_compound processing */
3177 status = nfsd4_replay_cache_entry(resp, seq);
3178 cstate->status = nfserr_replay_cache;
3182 goto out_put_session;
3184 status = nfsd4_sequence_check_conn(conn, session);
3187 goto out_put_session;
3189 buflen = (seq->cachethis) ?
3190 session->se_fchannel.maxresp_cached :
3191 session->se_fchannel.maxresp_sz;
3192 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
3194 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
3195 goto out_put_session;
3196 svc_reserve(rqstp, buflen);
3199 /* Success! bump slot seqid */
3200 slot->sl_seqid = seq->seqid;
3201 slot->sl_flags |= NFSD4_SLOT_INUSE;
3203 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
3205 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
3207 cstate->slot = slot;
3208 cstate->session = session;
3212 switch (clp->cl_cb_state) {
3214 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
3216 case NFSD4_CB_FAULT:
3217 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
3220 seq->status_flags = 0;
3222 if (!list_empty(&clp->cl_revoked))
3223 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
3227 spin_unlock(&nn->client_lock);
3230 nfsd4_put_session_locked(session);
3231 goto out_no_session;
3235 nfsd4_sequence_done(struct nfsd4_compoundres *resp)
3237 struct nfsd4_compound_state *cs = &resp->cstate;
3239 if (nfsd4_has_session(cs)) {
3240 if (cs->status != nfserr_replay_cache) {
3241 nfsd4_store_cache_entry(resp);
3242 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
3244 /* Drop session reference that was taken in nfsd4_sequence() */
3245 nfsd4_put_session(cs->session);
3247 put_client_renew(cs->clp);
3251 nfsd4_destroy_clientid(struct svc_rqst *rqstp,
3252 struct nfsd4_compound_state *cstate,
3253 union nfsd4_op_u *u)
3255 struct nfsd4_destroy_clientid *dc = &u->destroy_clientid;
3256 struct nfs4_client *conf, *unconf;
3257 struct nfs4_client *clp = NULL;
3259 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3261 spin_lock(&nn->client_lock);
3262 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
3263 conf = find_confirmed_client(&dc->clientid, true, nn);
3264 WARN_ON_ONCE(conf && unconf);
3267 if (client_has_state(conf)) {
3268 status = nfserr_clientid_busy;
3271 status = mark_client_expired_locked(conf);
3278 status = nfserr_stale_clientid;
3281 if (!nfsd4_mach_creds_match(clp, rqstp)) {
3283 status = nfserr_wrong_cred;
3286 unhash_client_locked(clp);
3288 spin_unlock(&nn->client_lock);
3295 nfsd4_reclaim_complete(struct svc_rqst *rqstp,
3296 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
3298 struct nfsd4_reclaim_complete *rc = &u->reclaim_complete;
3301 if (rc->rca_one_fs) {
3302 if (!cstate->current_fh.fh_dentry)
3303 return nfserr_nofilehandle;
3305 * We don't take advantage of the rca_one_fs case.
3306 * That's OK, it's optional, we can safely ignore it.
3311 status = nfserr_complete_already;
3312 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
3313 &cstate->session->se_client->cl_flags))
3316 status = nfserr_stale_clientid;
3317 if (is_client_expired(cstate->session->se_client))
3319 * The following error isn't really legal.
3320 * But we only get here if the client just explicitly
3321 * destroyed the client. Surely it no longer cares what
3322 * error it gets back on an operation for the dead
3328 nfsd4_client_record_create(cstate->session->se_client);
3334 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3335 union nfsd4_op_u *u)
3337 struct nfsd4_setclientid *setclid = &u->setclientid;
3338 struct xdr_netobj clname = setclid->se_name;
3339 nfs4_verifier clverifier = setclid->se_verf;
3340 struct nfs4_client *conf, *new;
3341 struct nfs4_client *unconf = NULL;
3343 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3345 new = create_client(clname, rqstp, &clverifier);
3347 return nfserr_jukebox;
3348 /* Cases below refer to rfc 3530 section 14.2.33: */
3349 spin_lock(&nn->client_lock);
3350 conf = find_confirmed_client_by_name(&clname, nn);
3351 if (conf && client_has_state(conf)) {
3353 status = nfserr_clid_inuse;
3354 if (clp_used_exchangeid(conf))
3356 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
3357 char addr_str[INET6_ADDRSTRLEN];
3358 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
3360 dprintk("NFSD: setclientid: string in use by client "
3361 "at %s\n", addr_str);
3365 unconf = find_unconfirmed_client_by_name(&clname, nn);
3367 unhash_client_locked(unconf);
3368 if (conf && same_verf(&conf->cl_verifier, &clverifier)) {
3369 /* case 1: probable callback update */
3370 copy_clid(new, conf);
3371 gen_confirm(new, nn);
3372 } else /* case 4 (new client) or cases 2, 3 (client reboot): */
3374 new->cl_minorversion = 0;
3375 gen_callback(new, setclid, rqstp);
3376 add_to_unconfirmed(new);
3377 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
3378 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
3379 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
3383 spin_unlock(&nn->client_lock);
3387 expire_client(unconf);
3393 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
3394 struct nfsd4_compound_state *cstate,
3395 union nfsd4_op_u *u)
3397 struct nfsd4_setclientid_confirm *setclientid_confirm =
3398 &u->setclientid_confirm;
3399 struct nfs4_client *conf, *unconf;
3400 struct nfs4_client *old = NULL;
3401 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
3402 clientid_t * clid = &setclientid_confirm->sc_clientid;
3404 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3406 if (STALE_CLIENTID(clid, nn))
3407 return nfserr_stale_clientid;
3409 spin_lock(&nn->client_lock);
3410 conf = find_confirmed_client(clid, false, nn);
3411 unconf = find_unconfirmed_client(clid, false, nn);
3413 * We try hard to give out unique clientid's, so if we get an
3414 * attempt to confirm the same clientid with a different cred,
3415 * the client may be buggy; this should never happen.
3417 * Nevertheless, RFC 7530 recommends INUSE for this case:
3419 status = nfserr_clid_inuse;
3420 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3422 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3424 /* cases below refer to rfc 3530 section 14.2.34: */
3425 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
3426 if (conf && same_verf(&confirm, &conf->cl_confirm)) {
3427 /* case 2: probable retransmit */
3429 } else /* case 4: client hasn't noticed we rebooted yet? */
3430 status = nfserr_stale_clientid;
3434 if (conf) { /* case 1: callback update */
3436 unhash_client_locked(old);
3437 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
3438 } else { /* case 3: normal case; new or rebooted client */
3439 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3441 status = nfserr_clid_inuse;
3442 if (client_has_state(old)
3443 && !same_creds(&unconf->cl_cred,
3446 status = mark_client_expired_locked(old);
3452 move_to_confirmed(unconf);
3455 get_client_locked(conf);
3456 spin_unlock(&nn->client_lock);
3457 nfsd4_probe_callback(conf);
3458 spin_lock(&nn->client_lock);
3459 put_client_renew_locked(conf);
3461 spin_unlock(&nn->client_lock);
3467 static struct nfs4_file *nfsd4_alloc_file(void)
3469 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3472 /* OPEN Share state helper functions */
3473 static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
3474 struct nfs4_file *fp)
3476 lockdep_assert_held(&state_lock);
3478 refcount_set(&fp->fi_ref, 1);
3479 spin_lock_init(&fp->fi_lock);
3480 INIT_LIST_HEAD(&fp->fi_stateids);
3481 INIT_LIST_HEAD(&fp->fi_delegations);
3482 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
3483 fh_copy_shallow(&fp->fi_fhandle, fh);
3484 fp->fi_deleg_file = NULL;
3485 fp->fi_had_conflict = false;
3486 fp->fi_share_deny = 0;
3487 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3488 memset(fp->fi_access, 0, sizeof(fp->fi_access));
3489 #ifdef CONFIG_NFSD_PNFS
3490 INIT_LIST_HEAD(&fp->fi_lo_states);
3491 atomic_set(&fp->fi_lo_recalls, 0);
3493 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
3497 nfsd4_free_slabs(void)
3499 kmem_cache_destroy(client_slab);
3500 kmem_cache_destroy(openowner_slab);
3501 kmem_cache_destroy(lockowner_slab);
3502 kmem_cache_destroy(file_slab);
3503 kmem_cache_destroy(stateid_slab);
3504 kmem_cache_destroy(deleg_slab);
3505 kmem_cache_destroy(odstate_slab);
3509 nfsd4_init_slabs(void)
3511 client_slab = kmem_cache_create("nfsd4_clients",
3512 sizeof(struct nfs4_client), 0, 0, NULL);
3513 if (client_slab == NULL)
3515 openowner_slab = kmem_cache_create("nfsd4_openowners",
3516 sizeof(struct nfs4_openowner), 0, 0, NULL);
3517 if (openowner_slab == NULL)
3518 goto out_free_client_slab;
3519 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
3520 sizeof(struct nfs4_lockowner), 0, 0, NULL);
3521 if (lockowner_slab == NULL)
3522 goto out_free_openowner_slab;
3523 file_slab = kmem_cache_create("nfsd4_files",
3524 sizeof(struct nfs4_file), 0, 0, NULL);
3525 if (file_slab == NULL)
3526 goto out_free_lockowner_slab;
3527 stateid_slab = kmem_cache_create("nfsd4_stateids",
3528 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
3529 if (stateid_slab == NULL)
3530 goto out_free_file_slab;
3531 deleg_slab = kmem_cache_create("nfsd4_delegations",
3532 sizeof(struct nfs4_delegation), 0, 0, NULL);
3533 if (deleg_slab == NULL)
3534 goto out_free_stateid_slab;
3535 odstate_slab = kmem_cache_create("nfsd4_odstate",
3536 sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
3537 if (odstate_slab == NULL)
3538 goto out_free_deleg_slab;
3541 out_free_deleg_slab:
3542 kmem_cache_destroy(deleg_slab);
3543 out_free_stateid_slab:
3544 kmem_cache_destroy(stateid_slab);
3546 kmem_cache_destroy(file_slab);
3547 out_free_lockowner_slab:
3548 kmem_cache_destroy(lockowner_slab);
3549 out_free_openowner_slab:
3550 kmem_cache_destroy(openowner_slab);
3551 out_free_client_slab:
3552 kmem_cache_destroy(client_slab);
3554 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3558 static void init_nfs4_replay(struct nfs4_replay *rp)
3560 rp->rp_status = nfserr_serverfault;
3562 rp->rp_buf = rp->rp_ibuf;
3563 mutex_init(&rp->rp_mutex);
3566 static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3567 struct nfs4_stateowner *so)
3569 if (!nfsd4_has_session(cstate)) {
3570 mutex_lock(&so->so_replay.rp_mutex);
3571 cstate->replay_owner = nfs4_get_stateowner(so);
3575 void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3577 struct nfs4_stateowner *so = cstate->replay_owner;
3580 cstate->replay_owner = NULL;
3581 mutex_unlock(&so->so_replay.rp_mutex);
3582 nfs4_put_stateowner(so);
3586 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
3588 struct nfs4_stateowner *sop;
3590 sop = kmem_cache_alloc(slab, GFP_KERNEL);
3594 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3595 if (!sop->so_owner.data) {
3596 kmem_cache_free(slab, sop);
3599 sop->so_owner.len = owner->len;
3601 INIT_LIST_HEAD(&sop->so_stateids);
3602 sop->so_client = clp;
3603 init_nfs4_replay(&sop->so_replay);
3604 atomic_set(&sop->so_count, 1);
3608 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
3610 lockdep_assert_held(&clp->cl_lock);
3612 list_add(&oo->oo_owner.so_strhash,
3613 &clp->cl_ownerstr_hashtbl[strhashval]);
3614 list_add(&oo->oo_perclient, &clp->cl_openowners);
3617 static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3619 unhash_openowner_locked(openowner(so));
3622 static void nfs4_free_openowner(struct nfs4_stateowner *so)
3624 struct nfs4_openowner *oo = openowner(so);
3626 kmem_cache_free(openowner_slab, oo);
3629 static const struct nfs4_stateowner_operations openowner_ops = {
3630 .so_unhash = nfs4_unhash_openowner,
3631 .so_free = nfs4_free_openowner,
3634 static struct nfs4_ol_stateid *
3635 nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
3637 struct nfs4_ol_stateid *local, *ret = NULL;
3638 struct nfs4_openowner *oo = open->op_openowner;
3640 lockdep_assert_held(&fp->fi_lock);
3642 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
3643 /* ignore lock owners */
3644 if (local->st_stateowner->so_is_open_owner == 0)
3646 if (local->st_stateowner != &oo->oo_owner)
3648 if (local->st_stid.sc_type == NFS4_OPEN_STID) {
3650 refcount_inc(&ret->st_stid.sc_count);
3658 nfsd4_verify_open_stid(struct nfs4_stid *s)
3660 __be32 ret = nfs_ok;
3662 switch (s->sc_type) {
3666 case NFS4_CLOSED_STID:
3667 case NFS4_CLOSED_DELEG_STID:
3668 ret = nfserr_bad_stateid;
3670 case NFS4_REVOKED_DELEG_STID:
3671 ret = nfserr_deleg_revoked;
3676 /* Lock the stateid st_mutex, and deal with races with CLOSE */
3678 nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp)
3682 mutex_lock_nested(&stp->st_mutex, LOCK_STATEID_MUTEX);
3683 ret = nfsd4_verify_open_stid(&stp->st_stid);
3685 mutex_unlock(&stp->st_mutex);
3689 static struct nfs4_ol_stateid *
3690 nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
3692 struct nfs4_ol_stateid *stp;
3694 spin_lock(&fp->fi_lock);
3695 stp = nfsd4_find_existing_open(fp, open);
3696 spin_unlock(&fp->fi_lock);
3697 if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok)
3699 nfs4_put_stid(&stp->st_stid);
3704 static struct nfs4_openowner *
3705 alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
3706 struct nfsd4_compound_state *cstate)
3708 struct nfs4_client *clp = cstate->clp;
3709 struct nfs4_openowner *oo, *ret;
3711 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3714 oo->oo_owner.so_ops = &openowner_ops;
3715 oo->oo_owner.so_is_open_owner = 1;
3716 oo->oo_owner.so_seqid = open->op_seqid;
3718 if (nfsd4_has_session(cstate))
3719 oo->oo_flags |= NFS4_OO_CONFIRMED;
3721 oo->oo_last_closed_stid = NULL;
3722 INIT_LIST_HEAD(&oo->oo_close_lru);
3723 spin_lock(&clp->cl_lock);
3724 ret = find_openstateowner_str_locked(strhashval, open, clp);
3726 hash_openowner(oo, clp, strhashval);
3729 nfs4_free_stateowner(&oo->oo_owner);
3731 spin_unlock(&clp->cl_lock);
3735 static struct nfs4_ol_stateid *
3736 init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
3739 struct nfs4_openowner *oo = open->op_openowner;
3740 struct nfs4_ol_stateid *retstp = NULL;
3741 struct nfs4_ol_stateid *stp;
3744 /* We are moving these outside of the spinlocks to avoid the warnings */
3745 mutex_init(&stp->st_mutex);
3746 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
3749 spin_lock(&oo->oo_owner.so_client->cl_lock);
3750 spin_lock(&fp->fi_lock);
3752 retstp = nfsd4_find_existing_open(fp, open);
3756 open->op_stp = NULL;
3757 refcount_inc(&stp->st_stid.sc_count);
3758 stp->st_stid.sc_type = NFS4_OPEN_STID;
3759 INIT_LIST_HEAD(&stp->st_locks);
3760 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
3762 stp->st_stid.sc_file = fp;
3763 stp->st_access_bmap = 0;
3764 stp->st_deny_bmap = 0;
3765 stp->st_openstp = NULL;
3766 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
3767 list_add(&stp->st_perfile, &fp->fi_stateids);
3770 spin_unlock(&fp->fi_lock);
3771 spin_unlock(&oo->oo_owner.so_client->cl_lock);
3773 /* Handle races with CLOSE */
3774 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
3775 nfs4_put_stid(&retstp->st_stid);
3778 /* To keep mutex tracking happy */
3779 mutex_unlock(&stp->st_mutex);
3786 * In the 4.0 case we need to keep the owners around a little while to handle
3787 * CLOSE replay. We still do need to release any file access that is held by
3788 * them before returning however.
3791 move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
3793 struct nfs4_ol_stateid *last;
3794 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3795 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3798 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
3801 * We know that we hold one reference via nfsd4_close, and another
3802 * "persistent" reference for the client. If the refcount is higher
3803 * than 2, then there are still calls in progress that are using this
3804 * stateid. We can't put the sc_file reference until they are finished.
3805 * Wait for the refcount to drop to 2. Since it has been unhashed,
3806 * there should be no danger of the refcount going back up again at
3809 wait_event(close_wq, refcount_read(&s->st_stid.sc_count) == 2);
3811 release_all_access(s);
3812 if (s->st_stid.sc_file) {
3813 put_nfs4_file(s->st_stid.sc_file);
3814 s->st_stid.sc_file = NULL;
3817 spin_lock(&nn->client_lock);
3818 last = oo->oo_last_closed_stid;
3819 oo->oo_last_closed_stid = s;
3820 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
3821 oo->oo_time = get_seconds();
3822 spin_unlock(&nn->client_lock);
3824 nfs4_put_stid(&last->st_stid);
3827 /* search file_hashtbl[] for file */
3828 static struct nfs4_file *
3829 find_file_locked(struct knfsd_fh *fh, unsigned int hashval)
3831 struct nfs4_file *fp;
3833 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash) {
3834 if (fh_match(&fp->fi_fhandle, fh)) {
3835 if (refcount_inc_not_zero(&fp->fi_ref))
3843 find_file(struct knfsd_fh *fh)
3845 struct nfs4_file *fp;
3846 unsigned int hashval = file_hashval(fh);
3849 fp = find_file_locked(fh, hashval);
3854 static struct nfs4_file *
3855 find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
3857 struct nfs4_file *fp;
3858 unsigned int hashval = file_hashval(fh);
3861 fp = find_file_locked(fh, hashval);
3866 spin_lock(&state_lock);
3867 fp = find_file_locked(fh, hashval);
3868 if (likely(fp == NULL)) {
3869 nfsd4_init_file(fh, hashval, new);
3872 spin_unlock(&state_lock);
3878 * Called to check deny when READ with all zero stateid or
3879 * WRITE with all zero or all one stateid
3882 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3884 struct nfs4_file *fp;
3885 __be32 ret = nfs_ok;
3887 fp = find_file(¤t_fh->fh_handle);
3890 /* Check for conflicting share reservations */
3891 spin_lock(&fp->fi_lock);
3892 if (fp->fi_share_deny & deny_type)
3893 ret = nfserr_locked;
3894 spin_unlock(&fp->fi_lock);
3899 static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
3901 struct nfs4_delegation *dp = cb_to_delegation(cb);
3902 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3905 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
3908 * We can't do this in nfsd_break_deleg_cb because it is
3909 * already holding inode->i_lock.
3911 * If the dl_time != 0, then we know that it has already been
3912 * queued for a lease break. Don't queue it again.
3914 spin_lock(&state_lock);
3915 if (dp->dl_time == 0) {
3916 dp->dl_time = get_seconds();
3917 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
3919 spin_unlock(&state_lock);
3922 static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
3923 struct rpc_task *task)
3925 struct nfs4_delegation *dp = cb_to_delegation(cb);
3927 if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID)
3930 switch (task->tk_status) {
3934 case -NFS4ERR_BAD_STATEID:
3936 * Race: client probably got cb_recall before open reply
3937 * granting delegation.
3939 if (dp->dl_retries--) {
3940 rpc_delay(task, 2 * HZ);
3949 static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
3951 struct nfs4_delegation *dp = cb_to_delegation(cb);
3953 nfs4_put_stid(&dp->dl_stid);
3956 static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
3957 .prepare = nfsd4_cb_recall_prepare,
3958 .done = nfsd4_cb_recall_done,
3959 .release = nfsd4_cb_recall_release,
3962 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3965 * We're assuming the state code never drops its reference
3966 * without first removing the lease. Since we're in this lease
3967 * callback (and since the lease code is serialized by the
3968 * i_lock) we know the server hasn't removed the lease yet, and
3969 * we know it's safe to take a reference.
3971 refcount_inc(&dp->dl_stid.sc_count);
3972 nfsd4_run_cb(&dp->dl_recall);
3975 /* Called from break_lease() with i_lock held. */
3977 nfsd_break_deleg_cb(struct file_lock *fl)
3980 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
3981 struct nfs4_file *fp = dp->dl_stid.sc_file;
3984 * We don't want the locks code to timeout the lease for us;
3985 * we'll remove it ourself if a delegation isn't returned
3988 fl->fl_break_time = 0;
3990 spin_lock(&fp->fi_lock);
3991 fp->fi_had_conflict = true;
3992 nfsd_break_one_deleg(dp);
3993 spin_unlock(&fp->fi_lock);
3998 nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
3999 struct list_head *dispose)
4002 return lease_modify(onlist, arg, dispose);
4007 static const struct lock_manager_operations nfsd_lease_mng_ops = {
4008 .lm_break = nfsd_break_deleg_cb,
4009 .lm_change = nfsd_change_deleg_cb,
4012 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
4014 if (nfsd4_has_session(cstate))
4016 if (seqid == so->so_seqid - 1)
4017 return nfserr_replay_me;
4018 if (seqid == so->so_seqid)
4020 return nfserr_bad_seqid;
4023 static __be32 lookup_clientid(clientid_t *clid,
4024 struct nfsd4_compound_state *cstate,
4025 struct nfsd_net *nn)
4027 struct nfs4_client *found;
4030 found = cstate->clp;
4031 if (!same_clid(&found->cl_clientid, clid))
4032 return nfserr_stale_clientid;
4036 if (STALE_CLIENTID(clid, nn))
4037 return nfserr_stale_clientid;
4040 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
4041 * cached already then we know this is for is for v4.0 and "sessions"
4044 WARN_ON_ONCE(cstate->session);
4045 spin_lock(&nn->client_lock);
4046 found = find_confirmed_client(clid, false, nn);
4048 spin_unlock(&nn->client_lock);
4049 return nfserr_expired;
4051 atomic_inc(&found->cl_refcount);
4052 spin_unlock(&nn->client_lock);
4054 /* Cache the nfs4_client in cstate! */
4055 cstate->clp = found;
4060 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
4061 struct nfsd4_open *open, struct nfsd_net *nn)
4063 clientid_t *clientid = &open->op_clientid;
4064 struct nfs4_client *clp = NULL;
4065 unsigned int strhashval;
4066 struct nfs4_openowner *oo = NULL;
4069 if (STALE_CLIENTID(&open->op_clientid, nn))
4070 return nfserr_stale_clientid;
4072 * In case we need it later, after we've already created the
4073 * file and don't want to risk a further failure:
4075 open->op_file = nfsd4_alloc_file();
4076 if (open->op_file == NULL)
4077 return nfserr_jukebox;
4079 status = lookup_clientid(clientid, cstate, nn);
4084 strhashval = ownerstr_hashval(&open->op_owner);
4085 oo = find_openstateowner_str(strhashval, open, clp);
4086 open->op_openowner = oo;
4090 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4091 /* Replace unconfirmed owners without checking for replay. */
4092 release_openowner(oo);
4093 open->op_openowner = NULL;
4096 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
4101 oo = alloc_init_open_stateowner(strhashval, open, cstate);
4103 return nfserr_jukebox;
4104 open->op_openowner = oo;
4106 open->op_stp = nfs4_alloc_open_stateid(clp);
4108 return nfserr_jukebox;
4110 if (nfsd4_has_session(cstate) &&
4111 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
4112 open->op_odstate = alloc_clnt_odstate(clp);
4113 if (!open->op_odstate)
4114 return nfserr_jukebox;
4120 static inline __be32
4121 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
4123 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
4124 return nfserr_openmode;
4129 static int share_access_to_flags(u32 share_access)
4131 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
4134 static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
4136 struct nfs4_stid *ret;
4138 ret = find_stateid_by_type(cl, s,
4139 NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID);
4142 return delegstateid(ret);
4145 static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
4147 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
4148 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
4152 nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
4153 struct nfs4_delegation **dp)
4156 __be32 status = nfserr_bad_stateid;
4157 struct nfs4_delegation *deleg;
4159 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
4162 if (deleg->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) {
4163 nfs4_put_stid(&deleg->dl_stid);
4164 if (cl->cl_minorversion)
4165 status = nfserr_deleg_revoked;
4168 flags = share_access_to_flags(open->op_share_access);
4169 status = nfs4_check_delegmode(deleg, flags);
4171 nfs4_put_stid(&deleg->dl_stid);
4176 if (!nfsd4_is_deleg_cur(open))
4180 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
4184 static inline int nfs4_access_to_access(u32 nfs4_access)
4188 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
4189 flags |= NFSD_MAY_READ;
4190 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
4191 flags |= NFSD_MAY_WRITE;
4195 static inline __be32
4196 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
4197 struct nfsd4_open *open)
4199 struct iattr iattr = {
4200 .ia_valid = ATTR_SIZE,
4203 if (!open->op_truncate)
4205 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
4206 return nfserr_inval;
4207 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
4210 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
4211 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
4212 struct nfsd4_open *open)
4214 struct file *filp = NULL;
4216 int oflag = nfs4_access_to_omode(open->op_share_access);
4217 int access = nfs4_access_to_access(open->op_share_access);
4218 unsigned char old_access_bmap, old_deny_bmap;
4220 spin_lock(&fp->fi_lock);
4223 * Are we trying to set a deny mode that would conflict with
4226 status = nfs4_file_check_deny(fp, open->op_share_deny);
4227 if (status != nfs_ok) {
4228 spin_unlock(&fp->fi_lock);
4232 /* set access to the file */
4233 status = nfs4_file_get_access(fp, open->op_share_access);
4234 if (status != nfs_ok) {
4235 spin_unlock(&fp->fi_lock);
4239 /* Set access bits in stateid */
4240 old_access_bmap = stp->st_access_bmap;
4241 set_access(open->op_share_access, stp);
4243 /* Set new deny mask */
4244 old_deny_bmap = stp->st_deny_bmap;
4245 set_deny(open->op_share_deny, stp);
4246 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4248 if (!fp->fi_fds[oflag]) {
4249 spin_unlock(&fp->fi_lock);
4250 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
4252 goto out_put_access;
4253 spin_lock(&fp->fi_lock);
4254 if (!fp->fi_fds[oflag]) {
4255 fp->fi_fds[oflag] = filp;
4259 spin_unlock(&fp->fi_lock);
4263 status = nfsd4_truncate(rqstp, cur_fh, open);
4265 goto out_put_access;
4269 stp->st_access_bmap = old_access_bmap;
4270 nfs4_file_put_access(fp, open->op_share_access);
4271 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
4276 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)
4279 unsigned char old_deny_bmap = stp->st_deny_bmap;
4281 if (!test_access(open->op_share_access, stp))
4282 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
4284 /* test and set deny mode */
4285 spin_lock(&fp->fi_lock);
4286 status = nfs4_file_check_deny(fp, open->op_share_deny);
4287 if (status == nfs_ok) {
4288 set_deny(open->op_share_deny, stp);
4289 fp->fi_share_deny |=
4290 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4292 spin_unlock(&fp->fi_lock);
4294 if (status != nfs_ok)
4297 status = nfsd4_truncate(rqstp, cur_fh, open);
4298 if (status != nfs_ok)
4299 reset_union_bmap_deny(old_deny_bmap, stp);
4303 /* Should we give out recallable state?: */
4304 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
4306 if (clp->cl_cb_state == NFSD4_CB_UP)
4309 * In the sessions case, since we don't have to establish a
4310 * separate connection for callbacks, we assume it's OK
4311 * until we hear otherwise:
4313 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
4316 static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp,
4319 struct file_lock *fl;
4321 fl = locks_alloc_lock();
4324 fl->fl_lmops = &nfsd_lease_mng_ops;
4325 fl->fl_flags = FL_DELEG;
4326 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
4327 fl->fl_end = OFFSET_MAX;
4328 fl->fl_owner = (fl_owner_t)dp;
4329 fl->fl_pid = current->tgid;
4330 fl->fl_file = dp->dl_stid.sc_file->fi_deleg_file;
4334 static struct nfs4_delegation *
4335 nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
4336 struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate)
4339 struct nfs4_delegation *dp;
4341 struct file_lock *fl;
4344 * The fi_had_conflict and nfs_get_existing_delegation checks
4345 * here are just optimizations; we'll need to recheck them at
4348 if (fp->fi_had_conflict)
4349 return ERR_PTR(-EAGAIN);
4351 filp = find_readable_file(fp);
4353 /* We should always have a readable file here */
4355 return ERR_PTR(-EBADF);
4357 spin_lock(&state_lock);
4358 spin_lock(&fp->fi_lock);
4359 if (nfs4_delegation_exists(clp, fp))
4361 else if (!fp->fi_deleg_file) {
4362 fp->fi_deleg_file = filp;
4363 /* increment early to prevent fi_deleg_file from being
4365 fp->fi_delegees = 1;
4369 spin_unlock(&fp->fi_lock);
4370 spin_unlock(&state_lock);
4374 return ERR_PTR(status);
4377 dp = alloc_init_deleg(clp, fp, fh, odstate);
4381 fl = nfs4_alloc_init_lease(dp, NFS4_OPEN_DELEGATE_READ);
4383 goto out_clnt_odstate;
4385 status = vfs_setlease(fp->fi_deleg_file, fl->fl_type, &fl, NULL);
4387 locks_free_lock(fl);
4389 goto out_clnt_odstate;
4391 spin_lock(&state_lock);
4392 spin_lock(&fp->fi_lock);
4393 if (fp->fi_had_conflict)
4396 status = hash_delegation_locked(dp, fp);
4397 spin_unlock(&fp->fi_lock);
4398 spin_unlock(&state_lock);
4405 vfs_setlease(fp->fi_deleg_file, F_UNLCK, NULL, (void **)&dp);
4407 put_clnt_odstate(dp->dl_clnt_odstate);
4408 nfs4_put_stid(&dp->dl_stid);
4411 return ERR_PTR(status);
4414 static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
4416 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4417 if (status == -EAGAIN)
4418 open->op_why_no_deleg = WND4_CONTENTION;
4420 open->op_why_no_deleg = WND4_RESOURCE;
4421 switch (open->op_deleg_want) {
4422 case NFS4_SHARE_WANT_READ_DELEG:
4423 case NFS4_SHARE_WANT_WRITE_DELEG:
4424 case NFS4_SHARE_WANT_ANY_DELEG:
4426 case NFS4_SHARE_WANT_CANCEL:
4427 open->op_why_no_deleg = WND4_CANCELLED;
4429 case NFS4_SHARE_WANT_NO_DELEG:
4436 * Attempt to hand out a delegation.
4438 * Note we don't support write delegations, and won't until the vfs has
4439 * proper support for them.
4442 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
4443 struct nfs4_ol_stateid *stp)
4445 struct nfs4_delegation *dp;
4446 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
4447 struct nfs4_client *clp = stp->st_stid.sc_client;
4451 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
4452 open->op_recall = 0;
4453 switch (open->op_claim_type) {
4454 case NFS4_OPEN_CLAIM_PREVIOUS:
4456 open->op_recall = 1;
4457 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
4460 case NFS4_OPEN_CLAIM_NULL:
4461 case NFS4_OPEN_CLAIM_FH:
4463 * Let's not give out any delegations till everyone's
4464 * had the chance to reclaim theirs, *and* until
4465 * NLM locks have all been reclaimed:
4467 if (locks_in_grace(clp->net))
4469 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
4472 * Also, if the file was opened for write or
4473 * create, there's a good chance the client's
4474 * about to write to it, resulting in an
4475 * immediate recall (since we don't support
4476 * write delegations):
4478 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
4480 if (open->op_create == NFS4_OPEN_CREATE)
4486 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate);
4490 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
4492 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
4493 STATEID_VAL(&dp->dl_stid.sc_stateid));
4494 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
4495 nfs4_put_stid(&dp->dl_stid);
4498 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
4499 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
4500 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
4501 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
4502 open->op_recall = 1;
4505 /* 4.1 client asking for a delegation? */
4506 if (open->op_deleg_want)
4507 nfsd4_open_deleg_none_ext(open, status);
4511 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
4512 struct nfs4_delegation *dp)
4514 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
4515 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4516 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4517 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
4518 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
4519 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4520 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4521 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
4523 /* Otherwise the client must be confused wanting a delegation
4524 * it already has, therefore we don't return
4525 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
4530 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
4532 struct nfsd4_compoundres *resp = rqstp->rq_resp;
4533 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
4534 struct nfs4_file *fp = NULL;
4535 struct nfs4_ol_stateid *stp = NULL;
4536 struct nfs4_delegation *dp = NULL;
4538 bool new_stp = false;
4541 * Lookup file; if found, lookup stateid and check open request,
4542 * and check for delegations in the process of being recalled.
4543 * If not found, create the nfs4_file struct
4545 fp = find_or_add_file(open->op_file, ¤t_fh->fh_handle);
4546 if (fp != open->op_file) {
4547 status = nfs4_check_deleg(cl, open, &dp);
4550 stp = nfsd4_find_and_lock_existing_open(fp, open);
4552 open->op_file = NULL;
4553 status = nfserr_bad_stateid;
4554 if (nfsd4_is_deleg_cur(open))
4559 stp = init_open_stateid(fp, open);
4565 * OPEN the file, or upgrade an existing OPEN.
4566 * If truncate fails, the OPEN fails.
4568 * stp is already locked.
4571 /* Stateid was found, this is an OPEN upgrade */
4572 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
4574 mutex_unlock(&stp->st_mutex);
4578 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4580 stp->st_stid.sc_type = NFS4_CLOSED_STID;
4581 release_open_stateid(stp);
4582 mutex_unlock(&stp->st_mutex);
4586 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
4588 if (stp->st_clnt_odstate == open->op_odstate)
4589 open->op_odstate = NULL;
4592 nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
4593 mutex_unlock(&stp->st_mutex);
4595 if (nfsd4_has_session(&resp->cstate)) {
4596 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4597 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4598 open->op_why_no_deleg = WND4_NOT_WANTED;
4604 * Attempt to hand out a delegation. No error return, because the
4605 * OPEN succeeds even if we fail.
4607 nfs4_open_delegation(current_fh, open, stp);
4611 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
4612 STATEID_VAL(&stp->st_stid.sc_stateid));
4614 /* 4.1 client trying to upgrade/downgrade delegation? */
4615 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
4616 open->op_deleg_want)
4617 nfsd4_deleg_xgrade_none_ext(open, dp);
4621 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
4622 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
4624 * To finish the open response, we just need to set the rflags.
4626 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
4627 if (nfsd4_has_session(&resp->cstate))
4628 open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK;
4629 else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED))
4630 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
4633 nfs4_put_stid(&dp->dl_stid);
4635 nfs4_put_stid(&stp->st_stid);
4640 void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
4641 struct nfsd4_open *open)
4643 if (open->op_openowner) {
4644 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
4646 nfsd4_cstate_assign_replay(cstate, so);
4647 nfs4_put_stateowner(so);
4650 kmem_cache_free(file_slab, open->op_file);
4652 nfs4_put_stid(&open->op_stp->st_stid);
4653 if (open->op_odstate)
4654 kmem_cache_free(odstate_slab, open->op_odstate);
4658 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4659 union nfsd4_op_u *u)
4661 clientid_t *clid = &u->renew;
4662 struct nfs4_client *clp;
4664 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4666 dprintk("process_renew(%08x/%08x): starting\n",
4667 clid->cl_boot, clid->cl_id);
4668 status = lookup_clientid(clid, cstate, nn);
4672 status = nfserr_cb_path_down;
4673 if (!list_empty(&clp->cl_delegations)
4674 && clp->cl_cb_state != NFSD4_CB_UP)
4682 nfsd4_end_grace(struct nfsd_net *nn)
4684 /* do nothing if grace period already ended */
4685 if (nn->grace_ended)
4688 dprintk("NFSD: end of grace period\n");
4689 nn->grace_ended = true;
4691 * If the server goes down again right now, an NFSv4
4692 * client will still be allowed to reclaim after it comes back up,
4693 * even if it hasn't yet had a chance to reclaim state this time.
4696 nfsd4_record_grace_done(nn);
4698 * At this point, NFSv4 clients can still reclaim. But if the
4699 * server crashes, any that have not yet reclaimed will be out
4700 * of luck on the next boot.
4702 * (NFSv4.1+ clients are considered to have reclaimed once they
4703 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
4704 * have reclaimed after their first OPEN.)
4706 locks_end_grace(&nn->nfsd4_manager);
4708 * At this point, and once lockd and/or any other containers
4709 * exit their grace period, further reclaims will fail and
4710 * regular locking can resume.
4715 * If we've waited a lease period but there are still clients trying to
4716 * reclaim, wait a little longer to give them a chance to finish.
4718 static bool clients_still_reclaiming(struct nfsd_net *nn)
4720 unsigned long now = get_seconds();
4721 unsigned long double_grace_period_end = nn->boot_time +
4722 2 * nn->nfsd4_lease;
4724 if (!nn->somebody_reclaimed)
4726 nn->somebody_reclaimed = false;
4728 * If we've given them *two* lease times to reclaim, and they're
4729 * still not done, give up:
4731 if (time_after(now, double_grace_period_end))
4737 nfs4_laundromat(struct nfsd_net *nn)
4739 struct nfs4_client *clp;
4740 struct nfs4_openowner *oo;
4741 struct nfs4_delegation *dp;
4742 struct nfs4_ol_stateid *stp;
4743 struct nfsd4_blocked_lock *nbl;
4744 struct list_head *pos, *next, reaplist;
4745 time_t cutoff = get_seconds() - nn->nfsd4_lease;
4746 time_t t, new_timeo = nn->nfsd4_lease;
4748 dprintk("NFSD: laundromat service - starting\n");
4750 if (clients_still_reclaiming(nn)) {
4754 nfsd4_end_grace(nn);
4755 INIT_LIST_HEAD(&reaplist);
4756 spin_lock(&nn->client_lock);
4757 list_for_each_safe(pos, next, &nn->client_lru) {
4758 clp = list_entry(pos, struct nfs4_client, cl_lru);
4759 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4760 t = clp->cl_time - cutoff;
4761 new_timeo = min(new_timeo, t);
4764 if (mark_client_expired_locked(clp)) {
4765 dprintk("NFSD: client in use (clientid %08x)\n",
4766 clp->cl_clientid.cl_id);
4769 list_add(&clp->cl_lru, &reaplist);
4771 spin_unlock(&nn->client_lock);
4772 list_for_each_safe(pos, next, &reaplist) {
4773 clp = list_entry(pos, struct nfs4_client, cl_lru);
4774 dprintk("NFSD: purging unused client (clientid %08x)\n",
4775 clp->cl_clientid.cl_id);
4776 list_del_init(&clp->cl_lru);
4779 spin_lock(&state_lock);
4780 list_for_each_safe(pos, next, &nn->del_recall_lru) {
4781 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4782 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
4783 t = dp->dl_time - cutoff;
4784 new_timeo = min(new_timeo, t);
4787 WARN_ON(!unhash_delegation_locked(dp));
4788 list_add(&dp->dl_recall_lru, &reaplist);
4790 spin_unlock(&state_lock);
4791 while (!list_empty(&reaplist)) {
4792 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4794 list_del_init(&dp->dl_recall_lru);
4795 revoke_delegation(dp);
4798 spin_lock(&nn->client_lock);
4799 while (!list_empty(&nn->close_lru)) {
4800 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
4802 if (time_after((unsigned long)oo->oo_time,
4803 (unsigned long)cutoff)) {
4804 t = oo->oo_time - cutoff;
4805 new_timeo = min(new_timeo, t);
4808 list_del_init(&oo->oo_close_lru);
4809 stp = oo->oo_last_closed_stid;
4810 oo->oo_last_closed_stid = NULL;
4811 spin_unlock(&nn->client_lock);
4812 nfs4_put_stid(&stp->st_stid);
4813 spin_lock(&nn->client_lock);
4815 spin_unlock(&nn->client_lock);
4818 * It's possible for a client to try and acquire an already held lock
4819 * that is being held for a long time, and then lose interest in it.
4820 * So, we clean out any un-revisited request after a lease period
4821 * under the assumption that the client is no longer interested.
4823 * RFC5661, sec. 9.6 states that the client must not rely on getting
4824 * notifications and must continue to poll for locks, even when the
4825 * server supports them. Thus this shouldn't lead to clients blocking
4826 * indefinitely once the lock does become free.
4828 BUG_ON(!list_empty(&reaplist));
4829 spin_lock(&nn->blocked_locks_lock);
4830 while (!list_empty(&nn->blocked_locks_lru)) {
4831 nbl = list_first_entry(&nn->blocked_locks_lru,
4832 struct nfsd4_blocked_lock, nbl_lru);
4833 if (time_after((unsigned long)nbl->nbl_time,
4834 (unsigned long)cutoff)) {
4835 t = nbl->nbl_time - cutoff;
4836 new_timeo = min(new_timeo, t);
4839 list_move(&nbl->nbl_lru, &reaplist);
4840 list_del_init(&nbl->nbl_list);
4842 spin_unlock(&nn->blocked_locks_lock);
4844 while (!list_empty(&reaplist)) {
4845 nbl = list_first_entry(&reaplist,
4846 struct nfsd4_blocked_lock, nbl_lru);
4847 list_del_init(&nbl->nbl_lru);
4848 posix_unblock_lock(&nbl->nbl_lock);
4849 free_blocked_lock(nbl);
4852 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
4856 static struct workqueue_struct *laundry_wq;
4857 static void laundromat_main(struct work_struct *);
4860 laundromat_main(struct work_struct *laundry)
4863 struct delayed_work *dwork = to_delayed_work(laundry);
4864 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4867 t = nfs4_laundromat(nn);
4868 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
4869 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
4872 static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
4874 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
4875 return nfserr_bad_stateid;
4880 access_permit_read(struct nfs4_ol_stateid *stp)
4882 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4883 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4884 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
4888 access_permit_write(struct nfs4_ol_stateid *stp)
4890 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4891 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
4895 __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
4897 __be32 status = nfserr_openmode;
4899 /* For lock stateid's, we test the parent open, not the lock: */
4900 if (stp->st_openstp)
4901 stp = stp->st_openstp;
4902 if ((flags & WR_STATE) && !access_permit_write(stp))
4904 if ((flags & RD_STATE) && !access_permit_read(stp))
4911 static inline __be32
4912 check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
4914 if (ONE_STATEID(stateid) && (flags & RD_STATE))
4916 else if (opens_in_grace(net)) {
4917 /* Answer in remaining cases depends on existence of
4918 * conflicting state; so we must wait out the grace period. */
4919 return nfserr_grace;
4920 } else if (flags & WR_STATE)
4921 return nfs4_share_conflict(current_fh,
4922 NFS4_SHARE_DENY_WRITE);
4923 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4924 return nfs4_share_conflict(current_fh,
4925 NFS4_SHARE_DENY_READ);
4929 * Allow READ/WRITE during grace period on recovered state only for files
4930 * that are not able to provide mandatory locking.
4933 grace_disallows_io(struct net *net, struct inode *inode)
4935 return opens_in_grace(net) && mandatory_lock(inode);
4938 static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
4941 * When sessions are used the stateid generation number is ignored
4944 if (has_session && in->si_generation == 0)
4947 if (in->si_generation == ref->si_generation)
4950 /* If the client sends us a stateid from the future, it's buggy: */
4951 if (nfsd4_stateid_generation_after(in, ref))
4952 return nfserr_bad_stateid;
4954 * However, we could see a stateid from the past, even from a
4955 * non-buggy client. For example, if the client sends a lock
4956 * while some IO is outstanding, the lock may bump si_generation
4957 * while the IO is still in flight. The client could avoid that
4958 * situation by waiting for responses on all the IO requests,
4959 * but better performance may result in retrying IO that
4960 * receives an old_stateid error if requests are rarely
4961 * reordered in flight:
4963 return nfserr_old_stateid;
4966 static __be32 nfsd4_stid_check_stateid_generation(stateid_t *in, struct nfs4_stid *s, bool has_session)
4970 spin_lock(&s->sc_lock);
4971 ret = nfsd4_verify_open_stid(s);
4973 ret = check_stateid_generation(in, &s->sc_stateid, has_session);
4974 spin_unlock(&s->sc_lock);
4978 static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
4980 if (ols->st_stateowner->so_is_open_owner &&
4981 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
4982 return nfserr_bad_stateid;
4986 static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
4988 struct nfs4_stid *s;
4989 __be32 status = nfserr_bad_stateid;
4991 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
4992 CLOSE_STATEID(stateid))
4994 /* Client debugging aid. */
4995 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4996 char addr_str[INET6_ADDRSTRLEN];
4997 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4999 pr_warn_ratelimited("NFSD: client %s testing state ID "
5000 "with incorrect client ID\n", addr_str);
5003 spin_lock(&cl->cl_lock);
5004 s = find_stateid_locked(cl, stateid);
5007 status = nfsd4_stid_check_stateid_generation(stateid, s, 1);
5010 switch (s->sc_type) {
5011 case NFS4_DELEG_STID:
5014 case NFS4_REVOKED_DELEG_STID:
5015 status = nfserr_deleg_revoked;
5017 case NFS4_OPEN_STID:
5018 case NFS4_LOCK_STID:
5019 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
5022 printk("unknown stateid type %x\n", s->sc_type);
5024 case NFS4_CLOSED_STID:
5025 case NFS4_CLOSED_DELEG_STID:
5026 status = nfserr_bad_stateid;
5029 spin_unlock(&cl->cl_lock);
5034 nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
5035 stateid_t *stateid, unsigned char typemask,
5036 struct nfs4_stid **s, struct nfsd_net *nn)
5039 bool return_revoked = false;
5042 * only return revoked delegations if explicitly asked.
5043 * otherwise we report revoked or bad_stateid status.
5045 if (typemask & NFS4_REVOKED_DELEG_STID)
5046 return_revoked = true;
5047 else if (typemask & NFS4_DELEG_STID)
5048 typemask |= NFS4_REVOKED_DELEG_STID;
5050 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5051 CLOSE_STATEID(stateid))
5052 return nfserr_bad_stateid;
5053 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
5054 if (status == nfserr_stale_clientid) {
5055 if (cstate->session)
5056 return nfserr_bad_stateid;
5057 return nfserr_stale_stateid;
5061 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
5063 return nfserr_bad_stateid;
5064 if (((*s)->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) {
5066 if (cstate->minorversion)
5067 return nfserr_deleg_revoked;
5068 return nfserr_bad_stateid;
5073 static struct file *
5074 nfs4_find_file(struct nfs4_stid *s, int flags)
5079 switch (s->sc_type) {
5080 case NFS4_DELEG_STID:
5081 if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
5083 return get_file(s->sc_file->fi_deleg_file);
5084 case NFS4_OPEN_STID:
5085 case NFS4_LOCK_STID:
5086 if (flags & RD_STATE)
5087 return find_readable_file(s->sc_file);
5089 return find_writeable_file(s->sc_file);
5097 nfs4_check_olstateid(struct svc_fh *fhp, struct nfs4_ol_stateid *ols, int flags)
5101 status = nfsd4_check_openowner_confirmed(ols);
5104 return nfs4_check_openmode(ols, flags);
5108 nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
5109 struct file **filpp, bool *tmp_file, int flags)
5111 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
5115 file = nfs4_find_file(s, flags);
5117 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
5118 acc | NFSD_MAY_OWNER_OVERRIDE);
5126 status = nfsd_open(rqstp, fhp, S_IFREG, acc, filpp);
5138 * Checks for stateid operations
5141 nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
5142 struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
5143 stateid_t *stateid, int flags, struct file **filpp, bool *tmp_file)
5145 struct inode *ino = d_inode(fhp->fh_dentry);
5146 struct net *net = SVC_NET(rqstp);
5147 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5148 struct nfs4_stid *s = NULL;
5156 if (grace_disallows_io(net, ino))
5157 return nfserr_grace;
5159 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
5160 status = check_special_stateids(net, fhp, stateid, flags);
5164 status = nfsd4_lookup_stateid(cstate, stateid,
5165 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
5169 status = nfsd4_stid_check_stateid_generation(stateid, s,
5170 nfsd4_has_session(cstate));
5174 switch (s->sc_type) {
5175 case NFS4_DELEG_STID:
5176 status = nfs4_check_delegmode(delegstateid(s), flags);
5178 case NFS4_OPEN_STID:
5179 case NFS4_LOCK_STID:
5180 status = nfs4_check_olstateid(fhp, openlockstateid(s), flags);
5183 status = nfserr_bad_stateid;
5188 status = nfs4_check_fh(fhp, s);
5191 if (!status && filpp)
5192 status = nfs4_check_file(rqstp, fhp, s, filpp, tmp_file, flags);
5200 * Test if the stateid is valid
5203 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5204 union nfsd4_op_u *u)
5206 struct nfsd4_test_stateid *test_stateid = &u->test_stateid;
5207 struct nfsd4_test_stateid_id *stateid;
5208 struct nfs4_client *cl = cstate->session->se_client;
5210 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
5211 stateid->ts_id_status =
5212 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
5218 nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
5220 struct nfs4_ol_stateid *stp = openlockstateid(s);
5223 ret = nfsd4_lock_ol_stateid(stp);
5227 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5231 ret = nfserr_locks_held;
5232 if (check_for_locks(stp->st_stid.sc_file,
5233 lockowner(stp->st_stateowner)))
5236 release_lock_stateid(stp);
5240 mutex_unlock(&stp->st_mutex);
5247 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5248 union nfsd4_op_u *u)
5250 struct nfsd4_free_stateid *free_stateid = &u->free_stateid;
5251 stateid_t *stateid = &free_stateid->fr_stateid;
5252 struct nfs4_stid *s;
5253 struct nfs4_delegation *dp;
5254 struct nfs4_client *cl = cstate->session->se_client;
5255 __be32 ret = nfserr_bad_stateid;
5257 spin_lock(&cl->cl_lock);
5258 s = find_stateid_locked(cl, stateid);
5261 spin_lock(&s->sc_lock);
5262 switch (s->sc_type) {
5263 case NFS4_DELEG_STID:
5264 ret = nfserr_locks_held;
5266 case NFS4_OPEN_STID:
5267 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5270 ret = nfserr_locks_held;
5272 case NFS4_LOCK_STID:
5273 spin_unlock(&s->sc_lock);
5274 refcount_inc(&s->sc_count);
5275 spin_unlock(&cl->cl_lock);
5276 ret = nfsd4_free_lock_stateid(stateid, s);
5278 case NFS4_REVOKED_DELEG_STID:
5279 spin_unlock(&s->sc_lock);
5280 dp = delegstateid(s);
5281 list_del_init(&dp->dl_recall_lru);
5282 spin_unlock(&cl->cl_lock);
5286 /* Default falls through and returns nfserr_bad_stateid */
5288 spin_unlock(&s->sc_lock);
5290 spin_unlock(&cl->cl_lock);
5298 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
5299 RD_STATE : WR_STATE;
5302 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
5304 struct svc_fh *current_fh = &cstate->current_fh;
5305 struct nfs4_stateowner *sop = stp->st_stateowner;
5308 status = nfsd4_check_seqid(cstate, sop, seqid);
5311 status = nfsd4_lock_ol_stateid(stp);
5312 if (status != nfs_ok)
5314 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
5315 if (status == nfs_ok)
5316 status = nfs4_check_fh(current_fh, &stp->st_stid);
5317 if (status != nfs_ok)
5318 mutex_unlock(&stp->st_mutex);
5323 * Checks for sequence id mutating operations.
5326 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
5327 stateid_t *stateid, char typemask,
5328 struct nfs4_ol_stateid **stpp,
5329 struct nfsd_net *nn)
5332 struct nfs4_stid *s;
5333 struct nfs4_ol_stateid *stp = NULL;
5335 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
5336 seqid, STATEID_VAL(stateid));
5339 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
5342 stp = openlockstateid(s);
5343 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
5345 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
5349 nfs4_put_stid(&stp->st_stid);
5353 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
5354 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
5357 struct nfs4_openowner *oo;
5358 struct nfs4_ol_stateid *stp;
5360 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
5361 NFS4_OPEN_STID, &stp, nn);
5364 oo = openowner(stp->st_stateowner);
5365 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
5366 mutex_unlock(&stp->st_mutex);
5367 nfs4_put_stid(&stp->st_stid);
5368 return nfserr_bad_stateid;
5375 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5376 union nfsd4_op_u *u)
5378 struct nfsd4_open_confirm *oc = &u->open_confirm;
5380 struct nfs4_openowner *oo;
5381 struct nfs4_ol_stateid *stp;
5382 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5384 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
5385 cstate->current_fh.fh_dentry);
5387 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
5391 status = nfs4_preprocess_seqid_op(cstate,
5392 oc->oc_seqid, &oc->oc_req_stateid,
5393 NFS4_OPEN_STID, &stp, nn);
5396 oo = openowner(stp->st_stateowner);
5397 status = nfserr_bad_stateid;
5398 if (oo->oo_flags & NFS4_OO_CONFIRMED) {
5399 mutex_unlock(&stp->st_mutex);
5402 oo->oo_flags |= NFS4_OO_CONFIRMED;
5403 nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
5404 mutex_unlock(&stp->st_mutex);
5405 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
5406 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
5408 nfsd4_client_record_create(oo->oo_owner.so_client);
5411 nfs4_put_stid(&stp->st_stid);
5413 nfsd4_bump_seqid(cstate, status);
5417 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
5419 if (!test_access(access, stp))
5421 nfs4_file_put_access(stp->st_stid.sc_file, access);
5422 clear_access(access, stp);
5425 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
5427 switch (to_access) {
5428 case NFS4_SHARE_ACCESS_READ:
5429 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
5430 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
5432 case NFS4_SHARE_ACCESS_WRITE:
5433 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
5434 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
5436 case NFS4_SHARE_ACCESS_BOTH:
5444 nfsd4_open_downgrade(struct svc_rqst *rqstp,
5445 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
5447 struct nfsd4_open_downgrade *od = &u->open_downgrade;
5449 struct nfs4_ol_stateid *stp;
5450 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5452 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
5453 cstate->current_fh.fh_dentry);
5455 /* We don't yet support WANT bits: */
5456 if (od->od_deleg_want)
5457 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
5460 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
5461 &od->od_stateid, &stp, nn);
5464 status = nfserr_inval;
5465 if (!test_access(od->od_share_access, stp)) {
5466 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
5467 stp->st_access_bmap, od->od_share_access);
5470 if (!test_deny(od->od_share_deny, stp)) {
5471 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
5472 stp->st_deny_bmap, od->od_share_deny);
5475 nfs4_stateid_downgrade(stp, od->od_share_access);
5476 reset_union_bmap_deny(od->od_share_deny, stp);
5477 nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
5480 mutex_unlock(&stp->st_mutex);
5481 nfs4_put_stid(&stp->st_stid);
5483 nfsd4_bump_seqid(cstate, status);
5487 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
5489 struct nfs4_client *clp = s->st_stid.sc_client;
5491 LIST_HEAD(reaplist);
5493 spin_lock(&clp->cl_lock);
5494 unhashed = unhash_open_stateid(s, &reaplist);
5496 if (clp->cl_minorversion) {
5498 put_ol_stateid_locked(s, &reaplist);
5499 spin_unlock(&clp->cl_lock);
5500 free_ol_stateid_reaplist(&reaplist);
5502 spin_unlock(&clp->cl_lock);
5503 free_ol_stateid_reaplist(&reaplist);
5505 move_to_close_lru(s, clp->net);
5510 * nfs4_unlock_state() called after encode
5513 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5514 union nfsd4_op_u *u)
5516 struct nfsd4_close *close = &u->close;
5518 struct nfs4_ol_stateid *stp;
5519 struct net *net = SVC_NET(rqstp);
5520 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5522 dprintk("NFSD: nfsd4_close on file %pd\n",
5523 cstate->current_fh.fh_dentry);
5525 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
5527 NFS4_OPEN_STID|NFS4_CLOSED_STID,
5529 nfsd4_bump_seqid(cstate, status);
5533 stp->st_stid.sc_type = NFS4_CLOSED_STID;
5536 * Technically we don't _really_ have to increment or copy it, since
5537 * it should just be gone after this operation and we clobber the
5538 * copied value below, but we continue to do so here just to ensure
5539 * that racing ops see that there was a state change.
5541 nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
5543 nfsd4_close_open_stateid(stp);
5544 mutex_unlock(&stp->st_mutex);
5546 /* v4.1+ suggests that we send a special stateid in here, since the
5547 * clients should just ignore this anyway. Since this is not useful
5548 * for v4.0 clients either, we set it to the special close_stateid
5551 * See RFC5661 section 18.2.4, and RFC7530 section 16.2.5
5553 memcpy(&close->cl_stateid, &close_stateid, sizeof(close->cl_stateid));
5555 /* put reference from nfs4_preprocess_seqid_op */
5556 nfs4_put_stid(&stp->st_stid);
5562 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5563 union nfsd4_op_u *u)
5565 struct nfsd4_delegreturn *dr = &u->delegreturn;
5566 struct nfs4_delegation *dp;
5567 stateid_t *stateid = &dr->dr_stateid;
5568 struct nfs4_stid *s;
5570 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5572 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
5575 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
5578 dp = delegstateid(s);
5579 status = nfsd4_stid_check_stateid_generation(stateid, &dp->dl_stid, nfsd4_has_session(cstate));
5583 destroy_delegation(dp);
5585 nfs4_put_stid(&dp->dl_stid);
5591 end_offset(u64 start, u64 len)
5596 return end >= start ? end: NFS4_MAX_UINT64;
5599 /* last octet in a range */
5601 last_byte_offset(u64 start, u64 len)
5607 return end > start ? end - 1: NFS4_MAX_UINT64;
5611 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
5612 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
5613 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
5614 * locking, this prevents us from being completely protocol-compliant. The
5615 * real solution to this problem is to start using unsigned file offsets in
5616 * the VFS, but this is a very deep change!
5619 nfs4_transform_lock_offset(struct file_lock *lock)
5621 if (lock->fl_start < 0)
5622 lock->fl_start = OFFSET_MAX;
5623 if (lock->fl_end < 0)
5624 lock->fl_end = OFFSET_MAX;
5628 nfsd4_fl_get_owner(fl_owner_t owner)
5630 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
5632 nfs4_get_stateowner(&lo->lo_owner);
5637 nfsd4_fl_put_owner(fl_owner_t owner)
5639 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
5642 nfs4_put_stateowner(&lo->lo_owner);
5646 nfsd4_lm_notify(struct file_lock *fl)
5648 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
5649 struct net *net = lo->lo_owner.so_client->net;
5650 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5651 struct nfsd4_blocked_lock *nbl = container_of(fl,
5652 struct nfsd4_blocked_lock, nbl_lock);
5655 /* An empty list means that something else is going to be using it */
5656 spin_lock(&nn->blocked_locks_lock);
5657 if (!list_empty(&nbl->nbl_list)) {
5658 list_del_init(&nbl->nbl_list);
5659 list_del_init(&nbl->nbl_lru);
5662 spin_unlock(&nn->blocked_locks_lock);
5665 nfsd4_run_cb(&nbl->nbl_cb);
5668 static const struct lock_manager_operations nfsd_posix_mng_ops = {
5669 .lm_notify = nfsd4_lm_notify,
5670 .lm_get_owner = nfsd4_fl_get_owner,
5671 .lm_put_owner = nfsd4_fl_put_owner,
5675 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
5677 struct nfs4_lockowner *lo;
5679 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
5680 lo = (struct nfs4_lockowner *) fl->fl_owner;
5681 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
5682 lo->lo_owner.so_owner.len, GFP_KERNEL);
5683 if (!deny->ld_owner.data)
5684 /* We just don't care that much */
5686 deny->ld_owner.len = lo->lo_owner.so_owner.len;
5687 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
5690 deny->ld_owner.len = 0;
5691 deny->ld_owner.data = NULL;
5692 deny->ld_clientid.cl_boot = 0;
5693 deny->ld_clientid.cl_id = 0;
5695 deny->ld_start = fl->fl_start;
5696 deny->ld_length = NFS4_MAX_UINT64;
5697 if (fl->fl_end != NFS4_MAX_UINT64)
5698 deny->ld_length = fl->fl_end - fl->fl_start + 1;
5699 deny->ld_type = NFS4_READ_LT;
5700 if (fl->fl_type != F_RDLCK)
5701 deny->ld_type = NFS4_WRITE_LT;
5704 static struct nfs4_lockowner *
5705 find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
5707 unsigned int strhashval = ownerstr_hashval(owner);
5708 struct nfs4_stateowner *so;
5710 lockdep_assert_held(&clp->cl_lock);
5712 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
5714 if (so->so_is_open_owner)
5716 if (same_owner_str(so, owner))
5717 return lockowner(nfs4_get_stateowner(so));
5722 static struct nfs4_lockowner *
5723 find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
5725 struct nfs4_lockowner *lo;
5727 spin_lock(&clp->cl_lock);
5728 lo = find_lockowner_str_locked(clp, owner);
5729 spin_unlock(&clp->cl_lock);
5733 static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
5735 unhash_lockowner_locked(lockowner(sop));
5738 static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
5740 struct nfs4_lockowner *lo = lockowner(sop);
5742 kmem_cache_free(lockowner_slab, lo);
5745 static const struct nfs4_stateowner_operations lockowner_ops = {
5746 .so_unhash = nfs4_unhash_lockowner,
5747 .so_free = nfs4_free_lockowner,
5751 * Alloc a lock owner structure.
5752 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
5755 * strhashval = ownerstr_hashval
5757 static struct nfs4_lockowner *
5758 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
5759 struct nfs4_ol_stateid *open_stp,
5760 struct nfsd4_lock *lock)
5762 struct nfs4_lockowner *lo, *ret;
5764 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
5767 INIT_LIST_HEAD(&lo->lo_blocked);
5768 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
5769 lo->lo_owner.so_is_open_owner = 0;
5770 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
5771 lo->lo_owner.so_ops = &lockowner_ops;
5772 spin_lock(&clp->cl_lock);
5773 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
5775 list_add(&lo->lo_owner.so_strhash,
5776 &clp->cl_ownerstr_hashtbl[strhashval]);
5779 nfs4_free_stateowner(&lo->lo_owner);
5781 spin_unlock(&clp->cl_lock);
5785 static struct nfs4_ol_stateid *
5786 find_lock_stateid(const struct nfs4_lockowner *lo,
5787 const struct nfs4_ol_stateid *ost)
5789 struct nfs4_ol_stateid *lst;
5791 lockdep_assert_held(&ost->st_stid.sc_client->cl_lock);
5793 /* If ost is not hashed, ost->st_locks will not be valid */
5794 if (!nfs4_ol_stateid_unhashed(ost))
5795 list_for_each_entry(lst, &ost->st_locks, st_locks) {
5796 if (lst->st_stateowner == &lo->lo_owner) {
5797 refcount_inc(&lst->st_stid.sc_count);
5804 static struct nfs4_ol_stateid *
5805 init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
5806 struct nfs4_file *fp, struct inode *inode,
5807 struct nfs4_ol_stateid *open_stp)
5809 struct nfs4_client *clp = lo->lo_owner.so_client;
5810 struct nfs4_ol_stateid *retstp;
5812 mutex_init(&stp->st_mutex);
5813 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
5815 spin_lock(&clp->cl_lock);
5816 if (nfs4_ol_stateid_unhashed(open_stp))
5818 retstp = find_lock_stateid(lo, open_stp);
5821 refcount_inc(&stp->st_stid.sc_count);
5822 stp->st_stid.sc_type = NFS4_LOCK_STID;
5823 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
5825 stp->st_stid.sc_file = fp;
5826 stp->st_access_bmap = 0;
5827 stp->st_deny_bmap = open_stp->st_deny_bmap;
5828 stp->st_openstp = open_stp;
5829 spin_lock(&fp->fi_lock);
5830 list_add(&stp->st_locks, &open_stp->st_locks);
5831 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
5832 list_add(&stp->st_perfile, &fp->fi_stateids);
5833 spin_unlock(&fp->fi_lock);
5834 spin_unlock(&clp->cl_lock);
5837 spin_unlock(&clp->cl_lock);
5838 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
5839 nfs4_put_stid(&retstp->st_stid);
5842 /* To keep mutex tracking happy */
5843 mutex_unlock(&stp->st_mutex);
5846 spin_unlock(&clp->cl_lock);
5847 mutex_unlock(&stp->st_mutex);
5851 static struct nfs4_ol_stateid *
5852 find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5853 struct inode *inode, struct nfs4_ol_stateid *ost,
5856 struct nfs4_stid *ns = NULL;
5857 struct nfs4_ol_stateid *lst;
5858 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5859 struct nfs4_client *clp = oo->oo_owner.so_client;
5862 spin_lock(&clp->cl_lock);
5863 lst = find_lock_stateid(lo, ost);
5864 spin_unlock(&clp->cl_lock);
5866 if (nfsd4_lock_ol_stateid(lst) == nfs_ok)
5868 nfs4_put_stid(&lst->st_stid);
5870 ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid);
5874 lst = init_lock_stateid(openlockstateid(ns), lo, fi, inode, ost);
5875 if (lst == openlockstateid(ns))
5884 check_lock_length(u64 offset, u64 length)
5886 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
5887 (length > ~offset)));
5890 static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
5892 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
5894 lockdep_assert_held(&fp->fi_lock);
5896 if (test_access(access, lock_stp))
5898 __nfs4_file_get_access(fp, access);
5899 set_access(access, lock_stp);
5903 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5904 struct nfs4_ol_stateid *ost,
5905 struct nfsd4_lock *lock,
5906 struct nfs4_ol_stateid **plst, bool *new)
5909 struct nfs4_file *fi = ost->st_stid.sc_file;
5910 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5911 struct nfs4_client *cl = oo->oo_owner.so_client;
5912 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
5913 struct nfs4_lockowner *lo;
5914 struct nfs4_ol_stateid *lst;
5915 unsigned int strhashval;
5917 lo = find_lockowner_str(cl, &lock->lk_new_owner);
5919 strhashval = ownerstr_hashval(&lock->lk_new_owner);
5920 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5922 return nfserr_jukebox;
5924 /* with an existing lockowner, seqids must be the same */
5925 status = nfserr_bad_seqid;
5926 if (!cstate->minorversion &&
5927 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
5931 lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
5933 status = nfserr_jukebox;
5940 nfs4_put_stateowner(&lo->lo_owner);
5948 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5949 union nfsd4_op_u *u)
5951 struct nfsd4_lock *lock = &u->lock;
5952 struct nfs4_openowner *open_sop = NULL;
5953 struct nfs4_lockowner *lock_sop = NULL;
5954 struct nfs4_ol_stateid *lock_stp = NULL;
5955 struct nfs4_ol_stateid *open_stp = NULL;
5956 struct nfs4_file *fp;
5957 struct file *filp = NULL;
5958 struct nfsd4_blocked_lock *nbl = NULL;
5959 struct file_lock *file_lock = NULL;
5960 struct file_lock *conflock = NULL;
5965 unsigned char fl_type;
5966 unsigned int fl_flags = FL_POSIX;
5967 struct net *net = SVC_NET(rqstp);
5968 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5970 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5971 (long long) lock->lk_offset,
5972 (long long) lock->lk_length);
5974 if (check_lock_length(lock->lk_offset, lock->lk_length))
5975 return nfserr_inval;
5977 if ((status = fh_verify(rqstp, &cstate->current_fh,
5978 S_IFREG, NFSD_MAY_LOCK))) {
5979 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5983 if (lock->lk_is_new) {
5984 if (nfsd4_has_session(cstate))
5985 /* See rfc 5661 18.10.3: given clientid is ignored: */
5986 memcpy(&lock->lk_new_clientid,
5987 &cstate->session->se_client->cl_clientid,
5988 sizeof(clientid_t));
5990 status = nfserr_stale_clientid;
5991 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
5994 /* validate and update open stateid and open seqid */
5995 status = nfs4_preprocess_confirmed_seqid_op(cstate,
5996 lock->lk_new_open_seqid,
5997 &lock->lk_new_open_stateid,
6001 mutex_unlock(&open_stp->st_mutex);
6002 open_sop = openowner(open_stp->st_stateowner);
6003 status = nfserr_bad_stateid;
6004 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
6005 &lock->lk_new_clientid))
6007 status = lookup_or_create_lock_state(cstate, open_stp, lock,
6010 status = nfs4_preprocess_seqid_op(cstate,
6011 lock->lk_old_lock_seqid,
6012 &lock->lk_old_lock_stateid,
6013 NFS4_LOCK_STID, &lock_stp, nn);
6017 lock_sop = lockowner(lock_stp->st_stateowner);
6019 lkflg = setlkflg(lock->lk_type);
6020 status = nfs4_check_openmode(lock_stp, lkflg);
6024 status = nfserr_grace;
6025 if (locks_in_grace(net) && !lock->lk_reclaim)
6027 status = nfserr_no_grace;
6028 if (!locks_in_grace(net) && lock->lk_reclaim)
6031 fp = lock_stp->st_stid.sc_file;
6032 switch (lock->lk_type) {
6034 if (nfsd4_has_session(cstate))
6035 fl_flags |= FL_SLEEP;
6038 spin_lock(&fp->fi_lock);
6039 filp = find_readable_file_locked(fp);
6041 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
6042 spin_unlock(&fp->fi_lock);
6045 case NFS4_WRITEW_LT:
6046 if (nfsd4_has_session(cstate))
6047 fl_flags |= FL_SLEEP;
6050 spin_lock(&fp->fi_lock);
6051 filp = find_writeable_file_locked(fp);
6053 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
6054 spin_unlock(&fp->fi_lock);
6058 status = nfserr_inval;
6063 status = nfserr_openmode;
6067 nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
6069 dprintk("NFSD: %s: unable to allocate block!\n", __func__);
6070 status = nfserr_jukebox;
6074 file_lock = &nbl->nbl_lock;
6075 file_lock->fl_type = fl_type;
6076 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
6077 file_lock->fl_pid = current->tgid;
6078 file_lock->fl_file = filp;
6079 file_lock->fl_flags = fl_flags;
6080 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6081 file_lock->fl_start = lock->lk_offset;
6082 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
6083 nfs4_transform_lock_offset(file_lock);
6085 conflock = locks_alloc_lock();
6087 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6088 status = nfserr_jukebox;
6092 if (fl_flags & FL_SLEEP) {
6093 nbl->nbl_time = get_seconds();
6094 spin_lock(&nn->blocked_locks_lock);
6095 list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
6096 list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
6097 spin_unlock(&nn->blocked_locks_lock);
6100 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
6102 case 0: /* success! */
6103 nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
6105 if (lock->lk_reclaim)
6106 nn->somebody_reclaimed = true;
6108 case FILE_LOCK_DEFERRED:
6111 case -EAGAIN: /* conflock holds conflicting lock */
6112 status = nfserr_denied;
6113 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
6114 nfs4_set_lock_denied(conflock, &lock->lk_denied);
6117 status = nfserr_deadlock;
6120 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
6121 status = nfserrno(err);
6126 /* dequeue it if we queued it before */
6127 if (fl_flags & FL_SLEEP) {
6128 spin_lock(&nn->blocked_locks_lock);
6129 list_del_init(&nbl->nbl_list);
6130 list_del_init(&nbl->nbl_lru);
6131 spin_unlock(&nn->blocked_locks_lock);
6133 free_blocked_lock(nbl);
6138 /* Bump seqid manually if the 4.0 replay owner is openowner */
6139 if (cstate->replay_owner &&
6140 cstate->replay_owner != &lock_sop->lo_owner &&
6141 seqid_mutating_err(ntohl(status)))
6142 lock_sop->lo_owner.so_seqid++;
6145 * If this is a new, never-before-used stateid, and we are
6146 * returning an error, then just go ahead and release it.
6149 release_lock_stateid(lock_stp);
6151 mutex_unlock(&lock_stp->st_mutex);
6153 nfs4_put_stid(&lock_stp->st_stid);
6156 nfs4_put_stid(&open_stp->st_stid);
6157 nfsd4_bump_seqid(cstate, status);
6159 locks_free_lock(conflock);
6164 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
6165 * so we do a temporary open here just to get an open file to pass to
6166 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
6169 static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
6172 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
6174 err = nfserrno(vfs_test_lock(file, lock));
6184 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6185 union nfsd4_op_u *u)
6187 struct nfsd4_lockt *lockt = &u->lockt;
6188 struct file_lock *file_lock = NULL;
6189 struct nfs4_lockowner *lo = NULL;
6191 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6193 if (locks_in_grace(SVC_NET(rqstp)))
6194 return nfserr_grace;
6196 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
6197 return nfserr_inval;
6199 if (!nfsd4_has_session(cstate)) {
6200 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
6205 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
6208 file_lock = locks_alloc_lock();
6210 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6211 status = nfserr_jukebox;
6215 switch (lockt->lt_type) {
6218 file_lock->fl_type = F_RDLCK;
6221 case NFS4_WRITEW_LT:
6222 file_lock->fl_type = F_WRLCK;
6225 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
6226 status = nfserr_inval;
6230 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
6232 file_lock->fl_owner = (fl_owner_t)lo;
6233 file_lock->fl_pid = current->tgid;
6234 file_lock->fl_flags = FL_POSIX;
6236 file_lock->fl_start = lockt->lt_offset;
6237 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
6239 nfs4_transform_lock_offset(file_lock);
6241 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
6245 if (file_lock->fl_type != F_UNLCK) {
6246 status = nfserr_denied;
6247 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
6251 nfs4_put_stateowner(&lo->lo_owner);
6253 locks_free_lock(file_lock);
6258 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6259 union nfsd4_op_u *u)
6261 struct nfsd4_locku *locku = &u->locku;
6262 struct nfs4_ol_stateid *stp;
6263 struct file *filp = NULL;
6264 struct file_lock *file_lock = NULL;
6267 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6269 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
6270 (long long) locku->lu_offset,
6271 (long long) locku->lu_length);
6273 if (check_lock_length(locku->lu_offset, locku->lu_length))
6274 return nfserr_inval;
6276 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
6277 &locku->lu_stateid, NFS4_LOCK_STID,
6281 filp = find_any_file(stp->st_stid.sc_file);
6283 status = nfserr_lock_range;
6286 file_lock = locks_alloc_lock();
6288 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6289 status = nfserr_jukebox;
6293 file_lock->fl_type = F_UNLCK;
6294 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
6295 file_lock->fl_pid = current->tgid;
6296 file_lock->fl_file = filp;
6297 file_lock->fl_flags = FL_POSIX;
6298 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6299 file_lock->fl_start = locku->lu_offset;
6301 file_lock->fl_end = last_byte_offset(locku->lu_offset,
6303 nfs4_transform_lock_offset(file_lock);
6305 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
6307 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
6310 nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
6314 mutex_unlock(&stp->st_mutex);
6315 nfs4_put_stid(&stp->st_stid);
6317 nfsd4_bump_seqid(cstate, status);
6319 locks_free_lock(file_lock);
6323 status = nfserrno(err);
6329 * true: locks held by lockowner
6330 * false: no locks held by lockowner
6333 check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
6335 struct file_lock *fl;
6337 struct file *filp = find_any_file(fp);
6338 struct inode *inode;
6339 struct file_lock_context *flctx;
6342 /* Any valid lock stateid should have some sort of access */
6347 inode = locks_inode(filp);
6348 flctx = inode->i_flctx;
6350 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
6351 spin_lock(&flctx->flc_lock);
6352 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
6353 if (fl->fl_owner == (fl_owner_t)lowner) {
6358 spin_unlock(&flctx->flc_lock);
6365 nfsd4_release_lockowner(struct svc_rqst *rqstp,
6366 struct nfsd4_compound_state *cstate,
6367 union nfsd4_op_u *u)
6369 struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner;
6370 clientid_t *clid = &rlockowner->rl_clientid;
6371 struct nfs4_stateowner *sop;
6372 struct nfs4_lockowner *lo = NULL;
6373 struct nfs4_ol_stateid *stp;
6374 struct xdr_netobj *owner = &rlockowner->rl_owner;
6375 unsigned int hashval = ownerstr_hashval(owner);
6377 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6378 struct nfs4_client *clp;
6379 LIST_HEAD (reaplist);
6381 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
6382 clid->cl_boot, clid->cl_id);
6384 status = lookup_clientid(clid, cstate, nn);
6389 /* Find the matching lock stateowner */
6390 spin_lock(&clp->cl_lock);
6391 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
6394 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
6397 /* see if there are still any locks associated with it */
6398 lo = lockowner(sop);
6399 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
6400 if (check_for_locks(stp->st_stid.sc_file, lo)) {
6401 status = nfserr_locks_held;
6402 spin_unlock(&clp->cl_lock);
6407 nfs4_get_stateowner(sop);
6411 spin_unlock(&clp->cl_lock);
6415 unhash_lockowner_locked(lo);
6416 while (!list_empty(&lo->lo_owner.so_stateids)) {
6417 stp = list_first_entry(&lo->lo_owner.so_stateids,
6418 struct nfs4_ol_stateid,
6420 WARN_ON(!unhash_lock_stateid(stp));
6421 put_ol_stateid_locked(stp, &reaplist);
6423 spin_unlock(&clp->cl_lock);
6424 free_ol_stateid_reaplist(&reaplist);
6425 remove_blocked_locks(lo);
6426 nfs4_put_stateowner(&lo->lo_owner);
6431 static inline struct nfs4_client_reclaim *
6434 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
6438 nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
6440 struct nfs4_client_reclaim *crp;
6442 crp = nfsd4_find_reclaim_client(name, nn);
6443 return (crp && crp->cr_clp);
6447 * failure => all reset bets are off, nfserr_no_grace...
6449 struct nfs4_client_reclaim *
6450 nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
6452 unsigned int strhashval;
6453 struct nfs4_client_reclaim *crp;
6455 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
6456 crp = alloc_reclaim();
6458 strhashval = clientstr_hashval(name);
6459 INIT_LIST_HEAD(&crp->cr_strhash);
6460 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
6461 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
6463 nn->reclaim_str_hashtbl_size++;
6469 nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
6471 list_del(&crp->cr_strhash);
6473 nn->reclaim_str_hashtbl_size--;
6477 nfs4_release_reclaim(struct nfsd_net *nn)
6479 struct nfs4_client_reclaim *crp = NULL;
6482 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6483 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
6484 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
6485 struct nfs4_client_reclaim, cr_strhash);
6486 nfs4_remove_reclaim_record(crp, nn);
6489 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
6493 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
6494 struct nfs4_client_reclaim *
6495 nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
6497 unsigned int strhashval;
6498 struct nfs4_client_reclaim *crp = NULL;
6500 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
6502 strhashval = clientstr_hashval(recdir);
6503 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
6504 if (same_name(crp->cr_recdir, recdir)) {
6512 * Called from OPEN. Look for clientid in reclaim list.
6515 nfs4_check_open_reclaim(clientid_t *clid,
6516 struct nfsd4_compound_state *cstate,
6517 struct nfsd_net *nn)
6521 /* find clientid in conf_id_hashtbl */
6522 status = lookup_clientid(clid, cstate, nn);
6524 return nfserr_reclaim_bad;
6526 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
6527 return nfserr_no_grace;
6529 if (nfsd4_client_record_check(cstate->clp))
6530 return nfserr_reclaim_bad;
6535 #ifdef CONFIG_NFSD_FAULT_INJECTION
6537 put_client(struct nfs4_client *clp)
6539 atomic_dec(&clp->cl_refcount);
6542 static struct nfs4_client *
6543 nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
6545 struct nfs4_client *clp;
6546 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6549 if (!nfsd_netns_ready(nn))
6552 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6553 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
6560 nfsd_inject_print_clients(void)
6562 struct nfs4_client *clp;
6564 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6566 char buf[INET6_ADDRSTRLEN];
6568 if (!nfsd_netns_ready(nn))
6571 spin_lock(&nn->client_lock);
6572 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6573 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
6574 pr_info("NFS Client: %s\n", buf);
6577 spin_unlock(&nn->client_lock);
6583 nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
6586 struct nfs4_client *clp;
6587 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6590 if (!nfsd_netns_ready(nn))
6593 spin_lock(&nn->client_lock);
6594 clp = nfsd_find_client(addr, addr_size);
6596 if (mark_client_expired_locked(clp) == nfs_ok)
6601 spin_unlock(&nn->client_lock);
6610 nfsd_inject_forget_clients(u64 max)
6613 struct nfs4_client *clp, *next;
6614 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6616 LIST_HEAD(reaplist);
6618 if (!nfsd_netns_ready(nn))
6621 spin_lock(&nn->client_lock);
6622 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6623 if (mark_client_expired_locked(clp) == nfs_ok) {
6624 list_add(&clp->cl_lru, &reaplist);
6625 if (max != 0 && ++count >= max)
6629 spin_unlock(&nn->client_lock);
6631 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
6637 static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
6640 char buf[INET6_ADDRSTRLEN];
6641 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
6642 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
6646 nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
6647 struct list_head *collect)
6649 struct nfs4_client *clp = lst->st_stid.sc_client;
6650 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6656 lockdep_assert_held(&nn->client_lock);
6657 atomic_inc(&clp->cl_refcount);
6658 list_add(&lst->st_locks, collect);
6661 static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
6662 struct list_head *collect,
6663 bool (*func)(struct nfs4_ol_stateid *))
6665 struct nfs4_openowner *oop;
6666 struct nfs4_ol_stateid *stp, *st_next;
6667 struct nfs4_ol_stateid *lst, *lst_next;
6670 spin_lock(&clp->cl_lock);
6671 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
6672 list_for_each_entry_safe(stp, st_next,
6673 &oop->oo_owner.so_stateids, st_perstateowner) {
6674 list_for_each_entry_safe(lst, lst_next,
6675 &stp->st_locks, st_locks) {
6678 nfsd_inject_add_lock_to_list(lst,
6683 * Despite the fact that these functions deal
6684 * with 64-bit integers for "count", we must
6685 * ensure that it doesn't blow up the
6686 * clp->cl_refcount. Throw a warning if we
6687 * start to approach INT_MAX here.
6689 WARN_ON_ONCE(count == (INT_MAX / 2));
6696 spin_unlock(&clp->cl_lock);
6702 nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
6705 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
6709 nfsd_print_client_locks(struct nfs4_client *clp)
6711 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
6712 nfsd_print_count(clp, count, "locked files");
6717 nfsd_inject_print_locks(void)
6719 struct nfs4_client *clp;
6721 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6724 if (!nfsd_netns_ready(nn))
6727 spin_lock(&nn->client_lock);
6728 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6729 count += nfsd_print_client_locks(clp);
6730 spin_unlock(&nn->client_lock);
6736 nfsd_reap_locks(struct list_head *reaplist)
6738 struct nfs4_client *clp;
6739 struct nfs4_ol_stateid *stp, *next;
6741 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
6742 list_del_init(&stp->st_locks);
6743 clp = stp->st_stid.sc_client;
6744 nfs4_put_stid(&stp->st_stid);
6750 nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
6752 unsigned int count = 0;
6753 struct nfs4_client *clp;
6754 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6756 LIST_HEAD(reaplist);
6758 if (!nfsd_netns_ready(nn))
6761 spin_lock(&nn->client_lock);
6762 clp = nfsd_find_client(addr, addr_size);
6764 count = nfsd_collect_client_locks(clp, &reaplist, 0);
6765 spin_unlock(&nn->client_lock);
6766 nfsd_reap_locks(&reaplist);
6771 nfsd_inject_forget_locks(u64 max)
6774 struct nfs4_client *clp;
6775 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6777 LIST_HEAD(reaplist);
6779 if (!nfsd_netns_ready(nn))
6782 spin_lock(&nn->client_lock);
6783 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6784 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
6785 if (max != 0 && count >= max)
6788 spin_unlock(&nn->client_lock);
6789 nfsd_reap_locks(&reaplist);
6794 nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
6795 struct list_head *collect,
6796 void (*func)(struct nfs4_openowner *))
6798 struct nfs4_openowner *oop, *next;
6799 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6803 lockdep_assert_held(&nn->client_lock);
6805 spin_lock(&clp->cl_lock);
6806 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
6810 atomic_inc(&clp->cl_refcount);
6811 list_add(&oop->oo_perclient, collect);
6816 * Despite the fact that these functions deal with
6817 * 64-bit integers for "count", we must ensure that
6818 * it doesn't blow up the clp->cl_refcount. Throw a
6819 * warning if we start to approach INT_MAX here.
6821 WARN_ON_ONCE(count == (INT_MAX / 2));
6825 spin_unlock(&clp->cl_lock);
6831 nfsd_print_client_openowners(struct nfs4_client *clp)
6833 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
6835 nfsd_print_count(clp, count, "openowners");
6840 nfsd_collect_client_openowners(struct nfs4_client *clp,
6841 struct list_head *collect, u64 max)
6843 return nfsd_foreach_client_openowner(clp, max, collect,
6844 unhash_openowner_locked);
6848 nfsd_inject_print_openowners(void)
6850 struct nfs4_client *clp;
6852 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6855 if (!nfsd_netns_ready(nn))
6858 spin_lock(&nn->client_lock);
6859 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6860 count += nfsd_print_client_openowners(clp);
6861 spin_unlock(&nn->client_lock);
6867 nfsd_reap_openowners(struct list_head *reaplist)
6869 struct nfs4_client *clp;
6870 struct nfs4_openowner *oop, *next;
6872 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
6873 list_del_init(&oop->oo_perclient);
6874 clp = oop->oo_owner.so_client;
6875 release_openowner(oop);
6881 nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
6884 unsigned int count = 0;
6885 struct nfs4_client *clp;
6886 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6888 LIST_HEAD(reaplist);
6890 if (!nfsd_netns_ready(nn))
6893 spin_lock(&nn->client_lock);
6894 clp = nfsd_find_client(addr, addr_size);
6896 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
6897 spin_unlock(&nn->client_lock);
6898 nfsd_reap_openowners(&reaplist);
6903 nfsd_inject_forget_openowners(u64 max)
6906 struct nfs4_client *clp;
6907 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6909 LIST_HEAD(reaplist);
6911 if (!nfsd_netns_ready(nn))
6914 spin_lock(&nn->client_lock);
6915 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6916 count += nfsd_collect_client_openowners(clp, &reaplist,
6918 if (max != 0 && count >= max)
6921 spin_unlock(&nn->client_lock);
6922 nfsd_reap_openowners(&reaplist);
6926 static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
6927 struct list_head *victims)
6929 struct nfs4_delegation *dp, *next;
6930 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6934 lockdep_assert_held(&nn->client_lock);
6936 spin_lock(&state_lock);
6937 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
6940 * It's not safe to mess with delegations that have a
6941 * non-zero dl_time. They might have already been broken
6942 * and could be processed by the laundromat outside of
6943 * the state_lock. Just leave them be.
6945 if (dp->dl_time != 0)
6948 atomic_inc(&clp->cl_refcount);
6949 WARN_ON(!unhash_delegation_locked(dp));
6950 list_add(&dp->dl_recall_lru, victims);
6954 * Despite the fact that these functions deal with
6955 * 64-bit integers for "count", we must ensure that
6956 * it doesn't blow up the clp->cl_refcount. Throw a
6957 * warning if we start to approach INT_MAX here.
6959 WARN_ON_ONCE(count == (INT_MAX / 2));
6963 spin_unlock(&state_lock);
6968 nfsd_print_client_delegations(struct nfs4_client *clp)
6970 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
6972 nfsd_print_count(clp, count, "delegations");
6977 nfsd_inject_print_delegations(void)
6979 struct nfs4_client *clp;
6981 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6984 if (!nfsd_netns_ready(nn))
6987 spin_lock(&nn->client_lock);
6988 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6989 count += nfsd_print_client_delegations(clp);
6990 spin_unlock(&nn->client_lock);
6996 nfsd_forget_delegations(struct list_head *reaplist)
6998 struct nfs4_client *clp;
6999 struct nfs4_delegation *dp, *next;
7001 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
7002 list_del_init(&dp->dl_recall_lru);
7003 clp = dp->dl_stid.sc_client;
7004 revoke_delegation(dp);
7010 nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
7014 struct nfs4_client *clp;
7015 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7017 LIST_HEAD(reaplist);
7019 if (!nfsd_netns_ready(nn))
7022 spin_lock(&nn->client_lock);
7023 clp = nfsd_find_client(addr, addr_size);
7025 count = nfsd_find_all_delegations(clp, 0, &reaplist);
7026 spin_unlock(&nn->client_lock);
7028 nfsd_forget_delegations(&reaplist);
7033 nfsd_inject_forget_delegations(u64 max)
7036 struct nfs4_client *clp;
7037 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7039 LIST_HEAD(reaplist);
7041 if (!nfsd_netns_ready(nn))
7044 spin_lock(&nn->client_lock);
7045 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
7046 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
7047 if (max != 0 && count >= max)
7050 spin_unlock(&nn->client_lock);
7051 nfsd_forget_delegations(&reaplist);
7056 nfsd_recall_delegations(struct list_head *reaplist)
7058 struct nfs4_client *clp;
7059 struct nfs4_delegation *dp, *next;
7061 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
7062 list_del_init(&dp->dl_recall_lru);
7063 clp = dp->dl_stid.sc_client;
7065 * We skipped all entries that had a zero dl_time before,
7066 * so we can now reset the dl_time back to 0. If a delegation
7067 * break comes in now, then it won't make any difference since
7068 * we're recalling it either way.
7070 spin_lock(&state_lock);
7072 spin_unlock(&state_lock);
7073 nfsd_break_one_deleg(dp);
7079 nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
7083 struct nfs4_client *clp;
7084 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7086 LIST_HEAD(reaplist);
7088 if (!nfsd_netns_ready(nn))
7091 spin_lock(&nn->client_lock);
7092 clp = nfsd_find_client(addr, addr_size);
7094 count = nfsd_find_all_delegations(clp, 0, &reaplist);
7095 spin_unlock(&nn->client_lock);
7097 nfsd_recall_delegations(&reaplist);
7102 nfsd_inject_recall_delegations(u64 max)
7105 struct nfs4_client *clp, *next;
7106 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7108 LIST_HEAD(reaplist);
7110 if (!nfsd_netns_ready(nn))
7113 spin_lock(&nn->client_lock);
7114 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
7115 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
7116 if (max != 0 && ++count >= max)
7119 spin_unlock(&nn->client_lock);
7120 nfsd_recall_delegations(&reaplist);
7123 #endif /* CONFIG_NFSD_FAULT_INJECTION */
7126 * Since the lifetime of a delegation isn't limited to that of an open, a
7127 * client may quite reasonably hang on to a delegation as long as it has
7128 * the inode cached. This becomes an obvious problem the first time a
7129 * client's inode cache approaches the size of the server's total memory.
7131 * For now we avoid this problem by imposing a hard limit on the number
7132 * of delegations, which varies according to the server's memory size.
7135 set_max_delegations(void)
7138 * Allow at most 4 delegations per megabyte of RAM. Quick
7139 * estimates suggest that in the worst case (where every delegation
7140 * is for a different inode), a delegation could take about 1.5K,
7141 * giving a worst case usage of about 6% of memory.
7143 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
7146 static int nfs4_state_create_net(struct net *net)
7148 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7151 nn->conf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7152 sizeof(struct list_head),
7154 if (!nn->conf_id_hashtbl)
7156 nn->unconf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7157 sizeof(struct list_head),
7159 if (!nn->unconf_id_hashtbl)
7161 nn->sessionid_hashtbl = kmalloc_array(SESSION_HASH_SIZE,
7162 sizeof(struct list_head),
7164 if (!nn->sessionid_hashtbl)
7167 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7168 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
7169 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
7171 for (i = 0; i < SESSION_HASH_SIZE; i++)
7172 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
7173 nn->conf_name_tree = RB_ROOT;
7174 nn->unconf_name_tree = RB_ROOT;
7175 nn->boot_time = get_seconds();
7176 nn->grace_ended = false;
7177 nn->nfsd4_manager.block_opens = true;
7178 INIT_LIST_HEAD(&nn->nfsd4_manager.list);
7179 INIT_LIST_HEAD(&nn->client_lru);
7180 INIT_LIST_HEAD(&nn->close_lru);
7181 INIT_LIST_HEAD(&nn->del_recall_lru);
7182 spin_lock_init(&nn->client_lock);
7184 spin_lock_init(&nn->blocked_locks_lock);
7185 INIT_LIST_HEAD(&nn->blocked_locks_lru);
7187 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
7193 kfree(nn->unconf_id_hashtbl);
7195 kfree(nn->conf_id_hashtbl);
7201 nfs4_state_destroy_net(struct net *net)
7204 struct nfs4_client *clp = NULL;
7205 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7207 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7208 while (!list_empty(&nn->conf_id_hashtbl[i])) {
7209 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7210 destroy_client(clp);
7214 WARN_ON(!list_empty(&nn->blocked_locks_lru));
7216 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7217 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
7218 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7219 destroy_client(clp);
7223 kfree(nn->sessionid_hashtbl);
7224 kfree(nn->unconf_id_hashtbl);
7225 kfree(nn->conf_id_hashtbl);
7230 nfs4_state_start_net(struct net *net)
7232 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7235 ret = nfs4_state_create_net(net);
7238 locks_start_grace(net, &nn->nfsd4_manager);
7239 nfsd4_client_tracking_init(net);
7240 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %x)\n",
7241 nn->nfsd4_grace, net->ns.inum);
7242 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
7246 /* initialization to perform when the nfsd service is started: */
7249 nfs4_state_start(void)
7253 laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
7254 if (laundry_wq == NULL) {
7258 ret = nfsd4_create_callback_queue();
7260 goto out_free_laundry;
7262 set_max_delegations();
7266 destroy_workqueue(laundry_wq);
7272 nfs4_state_shutdown_net(struct net *net)
7274 struct nfs4_delegation *dp = NULL;
7275 struct list_head *pos, *next, reaplist;
7276 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7278 cancel_delayed_work_sync(&nn->laundromat_work);
7279 locks_end_grace(&nn->nfsd4_manager);
7281 INIT_LIST_HEAD(&reaplist);
7282 spin_lock(&state_lock);
7283 list_for_each_safe(pos, next, &nn->del_recall_lru) {
7284 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
7285 WARN_ON(!unhash_delegation_locked(dp));
7286 list_add(&dp->dl_recall_lru, &reaplist);
7288 spin_unlock(&state_lock);
7289 list_for_each_safe(pos, next, &reaplist) {
7290 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
7291 list_del_init(&dp->dl_recall_lru);
7292 destroy_unhashed_deleg(dp);
7295 nfsd4_client_tracking_exit(net);
7296 nfs4_state_destroy_net(net);
7300 nfs4_state_shutdown(void)
7302 destroy_workqueue(laundry_wq);
7303 nfsd4_destroy_callback_queue();
7307 get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7309 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
7310 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
7314 put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7316 if (cstate->minorversion) {
7317 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
7318 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
7323 clear_current_stateid(struct nfsd4_compound_state *cstate)
7325 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
7329 * functions to set current state id
7332 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate,
7333 union nfsd4_op_u *u)
7335 put_stateid(cstate, &u->open_downgrade.od_stateid);
7339 nfsd4_set_openstateid(struct nfsd4_compound_state *cstate,
7340 union nfsd4_op_u *u)
7342 put_stateid(cstate, &u->open.op_stateid);
7346 nfsd4_set_closestateid(struct nfsd4_compound_state *cstate,
7347 union nfsd4_op_u *u)
7349 put_stateid(cstate, &u->close.cl_stateid);
7353 nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate,
7354 union nfsd4_op_u *u)
7356 put_stateid(cstate, &u->lock.lk_resp_stateid);
7360 * functions to consume current state id
7364 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate,
7365 union nfsd4_op_u *u)
7367 get_stateid(cstate, &u->open_downgrade.od_stateid);
7371 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate,
7372 union nfsd4_op_u *u)
7374 get_stateid(cstate, &u->delegreturn.dr_stateid);
7378 nfsd4_get_freestateid(struct nfsd4_compound_state *cstate,
7379 union nfsd4_op_u *u)
7381 get_stateid(cstate, &u->free_stateid.fr_stateid);
7385 nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate,
7386 union nfsd4_op_u *u)
7388 get_stateid(cstate, &u->setattr.sa_stateid);
7392 nfsd4_get_closestateid(struct nfsd4_compound_state *cstate,
7393 union nfsd4_op_u *u)
7395 get_stateid(cstate, &u->close.cl_stateid);
7399 nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate,
7400 union nfsd4_op_u *u)
7402 get_stateid(cstate, &u->locku.lu_stateid);
7406 nfsd4_get_readstateid(struct nfsd4_compound_state *cstate,
7407 union nfsd4_op_u *u)
7409 get_stateid(cstate, &u->read.rd_stateid);
7413 nfsd4_get_writestateid(struct nfsd4_compound_state *cstate,
7414 union nfsd4_op_u *u)
7416 get_stateid(cstate, &u->write.wr_stateid);