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);
91 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
92 * the refcount on the open stateid to drop.
94 static DECLARE_WAIT_QUEUE_HEAD(close_wq);
96 static struct kmem_cache *openowner_slab;
97 static struct kmem_cache *lockowner_slab;
98 static struct kmem_cache *file_slab;
99 static struct kmem_cache *stateid_slab;
100 static struct kmem_cache *deleg_slab;
101 static struct kmem_cache *odstate_slab;
103 static void free_session(struct nfsd4_session *);
105 static const struct nfsd4_callback_ops nfsd4_cb_recall_ops;
106 static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops;
108 static bool is_session_dead(struct nfsd4_session *ses)
110 return ses->se_flags & NFS4_SESSION_DEAD;
113 static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
115 if (atomic_read(&ses->se_ref) > ref_held_by_me)
116 return nfserr_jukebox;
117 ses->se_flags |= NFS4_SESSION_DEAD;
121 static bool is_client_expired(struct nfs4_client *clp)
123 return clp->cl_time == 0;
126 static __be32 get_client_locked(struct nfs4_client *clp)
128 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
130 lockdep_assert_held(&nn->client_lock);
132 if (is_client_expired(clp))
133 return nfserr_expired;
134 atomic_inc(&clp->cl_refcount);
138 /* must be called under the client_lock */
140 renew_client_locked(struct nfs4_client *clp)
142 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
144 if (is_client_expired(clp)) {
146 printk("%s: client (clientid %08x/%08x) already expired\n",
148 clp->cl_clientid.cl_boot,
149 clp->cl_clientid.cl_id);
153 dprintk("renewing client (clientid %08x/%08x)\n",
154 clp->cl_clientid.cl_boot,
155 clp->cl_clientid.cl_id);
156 list_move_tail(&clp->cl_lru, &nn->client_lru);
157 clp->cl_time = get_seconds();
160 static void put_client_renew_locked(struct nfs4_client *clp)
162 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
164 lockdep_assert_held(&nn->client_lock);
166 if (!atomic_dec_and_test(&clp->cl_refcount))
168 if (!is_client_expired(clp))
169 renew_client_locked(clp);
172 static void put_client_renew(struct nfs4_client *clp)
174 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
176 if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
178 if (!is_client_expired(clp))
179 renew_client_locked(clp);
180 spin_unlock(&nn->client_lock);
183 static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
187 if (is_session_dead(ses))
188 return nfserr_badsession;
189 status = get_client_locked(ses->se_client);
192 atomic_inc(&ses->se_ref);
196 static void nfsd4_put_session_locked(struct nfsd4_session *ses)
198 struct nfs4_client *clp = ses->se_client;
199 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
201 lockdep_assert_held(&nn->client_lock);
203 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
205 put_client_renew_locked(clp);
208 static void nfsd4_put_session(struct nfsd4_session *ses)
210 struct nfs4_client *clp = ses->se_client;
211 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
213 spin_lock(&nn->client_lock);
214 nfsd4_put_session_locked(ses);
215 spin_unlock(&nn->client_lock);
218 static struct nfsd4_blocked_lock *
219 find_blocked_lock(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
222 struct nfsd4_blocked_lock *cur, *found = NULL;
224 spin_lock(&nn->blocked_locks_lock);
225 list_for_each_entry(cur, &lo->lo_blocked, nbl_list) {
226 if (fh_match(fh, &cur->nbl_fh)) {
227 list_del_init(&cur->nbl_list);
228 list_del_init(&cur->nbl_lru);
233 spin_unlock(&nn->blocked_locks_lock);
235 posix_unblock_lock(&found->nbl_lock);
239 static struct nfsd4_blocked_lock *
240 find_or_allocate_block(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
243 struct nfsd4_blocked_lock *nbl;
245 nbl = find_blocked_lock(lo, fh, nn);
247 nbl= kmalloc(sizeof(*nbl), GFP_KERNEL);
249 INIT_LIST_HEAD(&nbl->nbl_list);
250 INIT_LIST_HEAD(&nbl->nbl_lru);
251 fh_copy_shallow(&nbl->nbl_fh, fh);
252 locks_init_lock(&nbl->nbl_lock);
253 nfsd4_init_cb(&nbl->nbl_cb, lo->lo_owner.so_client,
254 &nfsd4_cb_notify_lock_ops,
255 NFSPROC4_CLNT_CB_NOTIFY_LOCK);
262 free_blocked_lock(struct nfsd4_blocked_lock *nbl)
264 locks_release_private(&nbl->nbl_lock);
269 remove_blocked_locks(struct nfs4_lockowner *lo)
271 struct nfs4_client *clp = lo->lo_owner.so_client;
272 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
273 struct nfsd4_blocked_lock *nbl;
276 /* Dequeue all blocked locks */
277 spin_lock(&nn->blocked_locks_lock);
278 while (!list_empty(&lo->lo_blocked)) {
279 nbl = list_first_entry(&lo->lo_blocked,
280 struct nfsd4_blocked_lock,
282 list_del_init(&nbl->nbl_list);
283 list_move(&nbl->nbl_lru, &reaplist);
285 spin_unlock(&nn->blocked_locks_lock);
288 while (!list_empty(&reaplist)) {
289 nbl = list_first_entry(&reaplist, struct nfsd4_blocked_lock,
291 list_del_init(&nbl->nbl_lru);
292 posix_unblock_lock(&nbl->nbl_lock);
293 free_blocked_lock(nbl);
298 nfsd4_cb_notify_lock_done(struct nfsd4_callback *cb, struct rpc_task *task)
301 * Since this is just an optimization, we don't try very hard if it
302 * turns out not to succeed. We'll requeue it on NFS4ERR_DELAY, and
303 * just quit trying on anything else.
305 switch (task->tk_status) {
307 rpc_delay(task, 1 * HZ);
315 nfsd4_cb_notify_lock_release(struct nfsd4_callback *cb)
317 struct nfsd4_blocked_lock *nbl = container_of(cb,
318 struct nfsd4_blocked_lock, nbl_cb);
320 free_blocked_lock(nbl);
323 static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops = {
324 .done = nfsd4_cb_notify_lock_done,
325 .release = nfsd4_cb_notify_lock_release,
328 static inline struct nfs4_stateowner *
329 nfs4_get_stateowner(struct nfs4_stateowner *sop)
331 atomic_inc(&sop->so_count);
336 same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
338 return (sop->so_owner.len == owner->len) &&
339 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
342 static struct nfs4_openowner *
343 find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
344 struct nfs4_client *clp)
346 struct nfs4_stateowner *so;
348 lockdep_assert_held(&clp->cl_lock);
350 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
352 if (!so->so_is_open_owner)
354 if (same_owner_str(so, &open->op_owner))
355 return openowner(nfs4_get_stateowner(so));
360 static struct nfs4_openowner *
361 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
362 struct nfs4_client *clp)
364 struct nfs4_openowner *oo;
366 spin_lock(&clp->cl_lock);
367 oo = find_openstateowner_str_locked(hashval, open, clp);
368 spin_unlock(&clp->cl_lock);
373 opaque_hashval(const void *ptr, int nbytes)
375 unsigned char *cptr = (unsigned char *) ptr;
385 static void nfsd4_free_file_rcu(struct rcu_head *rcu)
387 struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
389 kmem_cache_free(file_slab, fp);
393 put_nfs4_file(struct nfs4_file *fi)
395 might_lock(&state_lock);
397 if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) {
398 hlist_del_rcu(&fi->fi_hash);
399 spin_unlock(&state_lock);
400 WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
401 WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
402 call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
407 __nfs4_get_fd(struct nfs4_file *f, int oflag)
409 if (f->fi_fds[oflag])
410 return get_file(f->fi_fds[oflag]);
415 find_writeable_file_locked(struct nfs4_file *f)
419 lockdep_assert_held(&f->fi_lock);
421 ret = __nfs4_get_fd(f, O_WRONLY);
423 ret = __nfs4_get_fd(f, O_RDWR);
428 find_writeable_file(struct nfs4_file *f)
432 spin_lock(&f->fi_lock);
433 ret = find_writeable_file_locked(f);
434 spin_unlock(&f->fi_lock);
439 static struct file *find_readable_file_locked(struct nfs4_file *f)
443 lockdep_assert_held(&f->fi_lock);
445 ret = __nfs4_get_fd(f, O_RDONLY);
447 ret = __nfs4_get_fd(f, O_RDWR);
452 find_readable_file(struct nfs4_file *f)
456 spin_lock(&f->fi_lock);
457 ret = find_readable_file_locked(f);
458 spin_unlock(&f->fi_lock);
464 find_any_file(struct nfs4_file *f)
468 spin_lock(&f->fi_lock);
469 ret = __nfs4_get_fd(f, O_RDWR);
471 ret = __nfs4_get_fd(f, O_WRONLY);
473 ret = __nfs4_get_fd(f, O_RDONLY);
475 spin_unlock(&f->fi_lock);
479 static atomic_long_t num_delegations;
480 unsigned long max_delegations;
483 * Open owner state (share locks)
486 /* hash tables for lock and open owners */
487 #define OWNER_HASH_BITS 8
488 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
489 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
491 static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
495 ret = opaque_hashval(ownername->data, ownername->len);
496 return ret & OWNER_HASH_MASK;
499 /* hash table for nfs4_file */
500 #define FILE_HASH_BITS 8
501 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
503 static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
505 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
508 static unsigned int file_hashval(struct knfsd_fh *fh)
510 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
513 static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
516 __nfs4_file_get_access(struct nfs4_file *fp, u32 access)
518 lockdep_assert_held(&fp->fi_lock);
520 if (access & NFS4_SHARE_ACCESS_WRITE)
521 atomic_inc(&fp->fi_access[O_WRONLY]);
522 if (access & NFS4_SHARE_ACCESS_READ)
523 atomic_inc(&fp->fi_access[O_RDONLY]);
527 nfs4_file_get_access(struct nfs4_file *fp, u32 access)
529 lockdep_assert_held(&fp->fi_lock);
531 /* Does this access mode make sense? */
532 if (access & ~NFS4_SHARE_ACCESS_BOTH)
535 /* Does it conflict with a deny mode already set? */
536 if ((access & fp->fi_share_deny) != 0)
537 return nfserr_share_denied;
539 __nfs4_file_get_access(fp, access);
543 static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
545 /* Common case is that there is no deny mode. */
547 /* Does this deny mode make sense? */
548 if (deny & ~NFS4_SHARE_DENY_BOTH)
551 if ((deny & NFS4_SHARE_DENY_READ) &&
552 atomic_read(&fp->fi_access[O_RDONLY]))
553 return nfserr_share_denied;
555 if ((deny & NFS4_SHARE_DENY_WRITE) &&
556 atomic_read(&fp->fi_access[O_WRONLY]))
557 return nfserr_share_denied;
562 static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
564 might_lock(&fp->fi_lock);
566 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
567 struct file *f1 = NULL;
568 struct file *f2 = NULL;
570 swap(f1, fp->fi_fds[oflag]);
571 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
572 swap(f2, fp->fi_fds[O_RDWR]);
573 spin_unlock(&fp->fi_lock);
581 static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
583 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
585 if (access & NFS4_SHARE_ACCESS_WRITE)
586 __nfs4_file_put_access(fp, O_WRONLY);
587 if (access & NFS4_SHARE_ACCESS_READ)
588 __nfs4_file_put_access(fp, O_RDONLY);
592 * Allocate a new open/delegation state counter. This is needed for
593 * pNFS for proper return on close semantics.
595 * Note that we only allocate it for pNFS-enabled exports, otherwise
596 * all pointers to struct nfs4_clnt_odstate are always NULL.
598 static struct nfs4_clnt_odstate *
599 alloc_clnt_odstate(struct nfs4_client *clp)
601 struct nfs4_clnt_odstate *co;
603 co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
606 atomic_set(&co->co_odcount, 1);
612 hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
614 struct nfs4_file *fp = co->co_file;
616 lockdep_assert_held(&fp->fi_lock);
617 list_add(&co->co_perfile, &fp->fi_clnt_odstate);
621 get_clnt_odstate(struct nfs4_clnt_odstate *co)
624 atomic_inc(&co->co_odcount);
628 put_clnt_odstate(struct nfs4_clnt_odstate *co)
630 struct nfs4_file *fp;
636 if (atomic_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
637 list_del(&co->co_perfile);
638 spin_unlock(&fp->fi_lock);
640 nfsd4_return_all_file_layouts(co->co_client, fp);
641 kmem_cache_free(odstate_slab, co);
645 static struct nfs4_clnt_odstate *
646 find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
648 struct nfs4_clnt_odstate *co;
649 struct nfs4_client *cl;
656 spin_lock(&fp->fi_lock);
657 list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
658 if (co->co_client == cl) {
659 get_clnt_odstate(co);
665 hash_clnt_odstate_locked(new);
667 spin_unlock(&fp->fi_lock);
671 struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab,
672 void (*sc_free)(struct nfs4_stid *))
674 struct nfs4_stid *stid;
677 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
681 idr_preload(GFP_KERNEL);
682 spin_lock(&cl->cl_lock);
683 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 0, 0, GFP_NOWAIT);
684 spin_unlock(&cl->cl_lock);
689 stid->sc_free = sc_free;
690 stid->sc_client = cl;
691 stid->sc_stateid.si_opaque.so_id = new_id;
692 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
693 /* Will be incremented before return to client: */
694 atomic_set(&stid->sc_count, 1);
695 spin_lock_init(&stid->sc_lock);
698 * It shouldn't be a problem to reuse an opaque stateid value.
699 * I don't think it is for 4.1. But with 4.0 I worry that, for
700 * example, a stray write retransmission could be accepted by
701 * the server when it should have been rejected. Therefore,
702 * adopt a trick from the sctp code to attempt to maximize the
703 * amount of time until an id is reused, by ensuring they always
704 * "increase" (mod INT_MAX):
708 kmem_cache_free(slab, stid);
712 static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
714 struct nfs4_stid *stid;
716 stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid);
720 return openlockstateid(stid);
723 static void nfs4_free_deleg(struct nfs4_stid *stid)
725 kmem_cache_free(deleg_slab, stid);
726 atomic_long_dec(&num_delegations);
730 * When we recall a delegation, we should be careful not to hand it
731 * out again straight away.
732 * To ensure this we keep a pair of bloom filters ('new' and 'old')
733 * in which the filehandles of recalled delegations are "stored".
734 * If a filehandle appear in either filter, a delegation is blocked.
735 * When a delegation is recalled, the filehandle is stored in the "new"
737 * Every 30 seconds we swap the filters and clear the "new" one,
738 * unless both are empty of course.
740 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
741 * low 3 bytes as hash-table indices.
743 * 'blocked_delegations_lock', which is always taken in block_delegations(),
744 * is used to manage concurrent access. Testing does not need the lock
745 * except when swapping the two filters.
747 static DEFINE_SPINLOCK(blocked_delegations_lock);
748 static struct bloom_pair {
749 int entries, old_entries;
751 int new; /* index into 'set' */
752 DECLARE_BITMAP(set[2], 256);
753 } blocked_delegations;
755 static int delegation_blocked(struct knfsd_fh *fh)
758 struct bloom_pair *bd = &blocked_delegations;
760 if (bd->entries == 0)
762 if (seconds_since_boot() - bd->swap_time > 30) {
763 spin_lock(&blocked_delegations_lock);
764 if (seconds_since_boot() - bd->swap_time > 30) {
765 bd->entries -= bd->old_entries;
766 bd->old_entries = bd->entries;
767 memset(bd->set[bd->new], 0,
770 bd->swap_time = seconds_since_boot();
772 spin_unlock(&blocked_delegations_lock);
774 hash = jhash(&fh->fh_base, fh->fh_size, 0);
775 if (test_bit(hash&255, bd->set[0]) &&
776 test_bit((hash>>8)&255, bd->set[0]) &&
777 test_bit((hash>>16)&255, bd->set[0]))
780 if (test_bit(hash&255, bd->set[1]) &&
781 test_bit((hash>>8)&255, bd->set[1]) &&
782 test_bit((hash>>16)&255, bd->set[1]))
788 static void block_delegations(struct knfsd_fh *fh)
791 struct bloom_pair *bd = &blocked_delegations;
793 hash = jhash(&fh->fh_base, fh->fh_size, 0);
795 spin_lock(&blocked_delegations_lock);
796 __set_bit(hash&255, bd->set[bd->new]);
797 __set_bit((hash>>8)&255, bd->set[bd->new]);
798 __set_bit((hash>>16)&255, bd->set[bd->new]);
799 if (bd->entries == 0)
800 bd->swap_time = seconds_since_boot();
802 spin_unlock(&blocked_delegations_lock);
805 static struct nfs4_delegation *
806 alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh,
807 struct nfs4_clnt_odstate *odstate)
809 struct nfs4_delegation *dp;
812 dprintk("NFSD alloc_init_deleg\n");
813 n = atomic_long_inc_return(&num_delegations);
814 if (n < 0 || n > max_delegations)
816 if (delegation_blocked(¤t_fh->fh_handle))
818 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg));
823 * delegation seqid's are never incremented. The 4.1 special
824 * meaning of seqid 0 isn't meaningful, really, but let's avoid
825 * 0 anyway just for consistency and use 1:
827 dp->dl_stid.sc_stateid.si_generation = 1;
828 INIT_LIST_HEAD(&dp->dl_perfile);
829 INIT_LIST_HEAD(&dp->dl_perclnt);
830 INIT_LIST_HEAD(&dp->dl_recall_lru);
831 dp->dl_clnt_odstate = odstate;
832 get_clnt_odstate(odstate);
833 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
835 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
836 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
839 atomic_long_dec(&num_delegations);
844 nfs4_put_stid(struct nfs4_stid *s)
846 struct nfs4_file *fp = s->sc_file;
847 struct nfs4_client *clp = s->sc_client;
849 might_lock(&clp->cl_lock);
851 if (!atomic_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
852 wake_up_all(&close_wq);
855 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
856 spin_unlock(&clp->cl_lock);
863 nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
865 stateid_t *src = &stid->sc_stateid;
867 spin_lock(&stid->sc_lock);
868 if (unlikely(++src->si_generation == 0))
869 src->si_generation = 1;
870 memcpy(dst, src, sizeof(*dst));
871 spin_unlock(&stid->sc_lock);
874 static void nfs4_put_deleg_lease(struct nfs4_file *fp)
876 struct file *filp = NULL;
878 spin_lock(&fp->fi_lock);
879 if (fp->fi_deleg_file && --fp->fi_delegees == 0)
880 swap(filp, fp->fi_deleg_file);
881 spin_unlock(&fp->fi_lock);
884 vfs_setlease(filp, F_UNLCK, NULL, (void **)&fp);
889 void nfs4_unhash_stid(struct nfs4_stid *s)
895 * nfs4_get_existing_delegation - Discover if this delegation already exists
896 * @clp: a pointer to the nfs4_client we're granting a delegation to
897 * @fp: a pointer to the nfs4_file we're granting a delegation on
900 * On success: NULL if an existing delegation was not found.
902 * On error: -EAGAIN if one was previously granted to this nfs4_client
903 * for this nfs4_file.
908 nfs4_get_existing_delegation(struct nfs4_client *clp, struct nfs4_file *fp)
910 struct nfs4_delegation *searchdp = NULL;
911 struct nfs4_client *searchclp = NULL;
913 lockdep_assert_held(&state_lock);
914 lockdep_assert_held(&fp->fi_lock);
916 list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
917 searchclp = searchdp->dl_stid.sc_client;
918 if (clp == searchclp) {
926 * hash_delegation_locked - Add a delegation to the appropriate lists
927 * @dp: a pointer to the nfs4_delegation we are adding.
928 * @fp: a pointer to the nfs4_file we're granting a delegation on
931 * On success: NULL if the delegation was successfully hashed.
933 * On error: -EAGAIN if one was previously granted to this
934 * nfs4_client for this nfs4_file. Delegation is not hashed.
939 hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
942 struct nfs4_client *clp = dp->dl_stid.sc_client;
944 lockdep_assert_held(&state_lock);
945 lockdep_assert_held(&fp->fi_lock);
947 status = nfs4_get_existing_delegation(clp, fp);
951 atomic_inc(&dp->dl_stid.sc_count);
952 dp->dl_stid.sc_type = NFS4_DELEG_STID;
953 list_add(&dp->dl_perfile, &fp->fi_delegations);
954 list_add(&dp->dl_perclnt, &clp->cl_delegations);
958 static bool delegation_hashed(struct nfs4_delegation *dp)
960 return !(list_empty(&dp->dl_perfile));
964 unhash_delegation_locked(struct nfs4_delegation *dp)
966 struct nfs4_file *fp = dp->dl_stid.sc_file;
968 lockdep_assert_held(&state_lock);
970 if (!delegation_hashed(dp))
973 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
974 /* Ensure that deleg break won't try to requeue it */
976 spin_lock(&fp->fi_lock);
977 list_del_init(&dp->dl_perclnt);
978 list_del_init(&dp->dl_recall_lru);
979 list_del_init(&dp->dl_perfile);
980 spin_unlock(&fp->fi_lock);
984 static void destroy_delegation(struct nfs4_delegation *dp)
988 spin_lock(&state_lock);
989 unhashed = unhash_delegation_locked(dp);
990 spin_unlock(&state_lock);
992 put_clnt_odstate(dp->dl_clnt_odstate);
993 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
994 nfs4_put_stid(&dp->dl_stid);
998 static void revoke_delegation(struct nfs4_delegation *dp)
1000 struct nfs4_client *clp = dp->dl_stid.sc_client;
1002 WARN_ON(!list_empty(&dp->dl_recall_lru));
1004 put_clnt_odstate(dp->dl_clnt_odstate);
1005 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
1007 if (clp->cl_minorversion == 0)
1008 nfs4_put_stid(&dp->dl_stid);
1010 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
1011 spin_lock(&clp->cl_lock);
1012 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
1013 spin_unlock(&clp->cl_lock);
1021 static unsigned int clientid_hashval(u32 id)
1023 return id & CLIENT_HASH_MASK;
1026 static unsigned int clientstr_hashval(const char *name)
1028 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
1032 * We store the NONE, READ, WRITE, and BOTH bits separately in the
1033 * st_{access,deny}_bmap field of the stateid, in order to track not
1034 * only what share bits are currently in force, but also what
1035 * combinations of share bits previous opens have used. This allows us
1036 * to enforce the recommendation of rfc 3530 14.2.19 that the server
1037 * return an error if the client attempt to downgrade to a combination
1038 * of share bits not explicable by closing some of its previous opens.
1040 * XXX: This enforcement is actually incomplete, since we don't keep
1041 * track of access/deny bit combinations; so, e.g., we allow:
1043 * OPEN allow read, deny write
1044 * OPEN allow both, deny none
1045 * DOWNGRADE allow read, deny none
1047 * which we should reject.
1050 bmap_to_share_mode(unsigned long bmap) {
1052 unsigned int access = 0;
1054 for (i = 1; i < 4; i++) {
1055 if (test_bit(i, &bmap))
1061 /* set share access for a given stateid */
1063 set_access(u32 access, struct nfs4_ol_stateid *stp)
1065 unsigned char mask = 1 << access;
1067 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1068 stp->st_access_bmap |= mask;
1071 /* clear share access for a given stateid */
1073 clear_access(u32 access, struct nfs4_ol_stateid *stp)
1075 unsigned char mask = 1 << access;
1077 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1078 stp->st_access_bmap &= ~mask;
1081 /* test whether a given stateid has access */
1083 test_access(u32 access, struct nfs4_ol_stateid *stp)
1085 unsigned char mask = 1 << access;
1087 return (bool)(stp->st_access_bmap & mask);
1090 /* set share deny for a given stateid */
1092 set_deny(u32 deny, struct nfs4_ol_stateid *stp)
1094 unsigned char mask = 1 << deny;
1096 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1097 stp->st_deny_bmap |= mask;
1100 /* clear share deny for a given stateid */
1102 clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
1104 unsigned char mask = 1 << deny;
1106 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1107 stp->st_deny_bmap &= ~mask;
1110 /* test whether a given stateid is denying specific access */
1112 test_deny(u32 deny, struct nfs4_ol_stateid *stp)
1114 unsigned char mask = 1 << deny;
1116 return (bool)(stp->st_deny_bmap & mask);
1119 static int nfs4_access_to_omode(u32 access)
1121 switch (access & NFS4_SHARE_ACCESS_BOTH) {
1122 case NFS4_SHARE_ACCESS_READ:
1124 case NFS4_SHARE_ACCESS_WRITE:
1126 case NFS4_SHARE_ACCESS_BOTH:
1134 * A stateid that had a deny mode associated with it is being released
1135 * or downgraded. Recalculate the deny mode on the file.
1138 recalculate_deny_mode(struct nfs4_file *fp)
1140 struct nfs4_ol_stateid *stp;
1142 spin_lock(&fp->fi_lock);
1143 fp->fi_share_deny = 0;
1144 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
1145 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
1146 spin_unlock(&fp->fi_lock);
1150 reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
1153 bool change = false;
1155 for (i = 1; i < 4; i++) {
1156 if ((i & deny) != i) {
1162 /* Recalculate per-file deny mode if there was a change */
1164 recalculate_deny_mode(stp->st_stid.sc_file);
1167 /* release all access and file references for a given stateid */
1169 release_all_access(struct nfs4_ol_stateid *stp)
1172 struct nfs4_file *fp = stp->st_stid.sc_file;
1174 if (fp && stp->st_deny_bmap != 0)
1175 recalculate_deny_mode(fp);
1177 for (i = 1; i < 4; i++) {
1178 if (test_access(i, stp))
1179 nfs4_file_put_access(stp->st_stid.sc_file, i);
1180 clear_access(i, stp);
1184 static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1186 kfree(sop->so_owner.data);
1187 sop->so_ops->so_free(sop);
1190 static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1192 struct nfs4_client *clp = sop->so_client;
1194 might_lock(&clp->cl_lock);
1196 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
1198 sop->so_ops->so_unhash(sop);
1199 spin_unlock(&clp->cl_lock);
1200 nfs4_free_stateowner(sop);
1203 static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
1205 struct nfs4_file *fp = stp->st_stid.sc_file;
1207 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1209 if (list_empty(&stp->st_perfile))
1212 spin_lock(&fp->fi_lock);
1213 list_del_init(&stp->st_perfile);
1214 spin_unlock(&fp->fi_lock);
1215 list_del(&stp->st_perstateowner);
1219 static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
1221 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1223 put_clnt_odstate(stp->st_clnt_odstate);
1224 release_all_access(stp);
1225 if (stp->st_stateowner)
1226 nfs4_put_stateowner(stp->st_stateowner);
1227 kmem_cache_free(stateid_slab, stid);
1230 static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
1232 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1233 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
1236 file = find_any_file(stp->st_stid.sc_file);
1238 filp_close(file, (fl_owner_t)lo);
1239 nfs4_free_ol_stateid(stid);
1243 * Put the persistent reference to an already unhashed generic stateid, while
1244 * holding the cl_lock. If it's the last reference, then put it onto the
1245 * reaplist for later destruction.
1247 static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1248 struct list_head *reaplist)
1250 struct nfs4_stid *s = &stp->st_stid;
1251 struct nfs4_client *clp = s->sc_client;
1253 lockdep_assert_held(&clp->cl_lock);
1255 WARN_ON_ONCE(!list_empty(&stp->st_locks));
1257 if (!atomic_dec_and_test(&s->sc_count)) {
1258 wake_up_all(&close_wq);
1262 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1263 list_add(&stp->st_locks, reaplist);
1266 static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
1268 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1270 list_del_init(&stp->st_locks);
1271 nfs4_unhash_stid(&stp->st_stid);
1272 return unhash_ol_stateid(stp);
1275 static void release_lock_stateid(struct nfs4_ol_stateid *stp)
1277 struct nfs4_client *clp = stp->st_stid.sc_client;
1280 spin_lock(&clp->cl_lock);
1281 unhashed = unhash_lock_stateid(stp);
1282 spin_unlock(&clp->cl_lock);
1284 nfs4_put_stid(&stp->st_stid);
1287 static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
1289 struct nfs4_client *clp = lo->lo_owner.so_client;
1291 lockdep_assert_held(&clp->cl_lock);
1293 list_del_init(&lo->lo_owner.so_strhash);
1297 * Free a list of generic stateids that were collected earlier after being
1301 free_ol_stateid_reaplist(struct list_head *reaplist)
1303 struct nfs4_ol_stateid *stp;
1304 struct nfs4_file *fp;
1308 while (!list_empty(reaplist)) {
1309 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1311 list_del(&stp->st_locks);
1312 fp = stp->st_stid.sc_file;
1313 stp->st_stid.sc_free(&stp->st_stid);
1319 static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1320 struct list_head *reaplist)
1322 struct nfs4_ol_stateid *stp;
1324 lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1326 while (!list_empty(&open_stp->st_locks)) {
1327 stp = list_entry(open_stp->st_locks.next,
1328 struct nfs4_ol_stateid, st_locks);
1329 WARN_ON(!unhash_lock_stateid(stp));
1330 put_ol_stateid_locked(stp, reaplist);
1334 static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
1335 struct list_head *reaplist)
1339 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1341 unhashed = unhash_ol_stateid(stp);
1342 release_open_stateid_locks(stp, reaplist);
1346 static void release_open_stateid(struct nfs4_ol_stateid *stp)
1348 LIST_HEAD(reaplist);
1350 spin_lock(&stp->st_stid.sc_client->cl_lock);
1351 if (unhash_open_stateid(stp, &reaplist))
1352 put_ol_stateid_locked(stp, &reaplist);
1353 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1354 free_ol_stateid_reaplist(&reaplist);
1357 static void unhash_openowner_locked(struct nfs4_openowner *oo)
1359 struct nfs4_client *clp = oo->oo_owner.so_client;
1361 lockdep_assert_held(&clp->cl_lock);
1363 list_del_init(&oo->oo_owner.so_strhash);
1364 list_del_init(&oo->oo_perclient);
1367 static void release_last_closed_stateid(struct nfs4_openowner *oo)
1369 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1371 struct nfs4_ol_stateid *s;
1373 spin_lock(&nn->client_lock);
1374 s = oo->oo_last_closed_stid;
1376 list_del_init(&oo->oo_close_lru);
1377 oo->oo_last_closed_stid = NULL;
1379 spin_unlock(&nn->client_lock);
1381 nfs4_put_stid(&s->st_stid);
1384 static void release_openowner(struct nfs4_openowner *oo)
1386 struct nfs4_ol_stateid *stp;
1387 struct nfs4_client *clp = oo->oo_owner.so_client;
1388 struct list_head reaplist;
1390 INIT_LIST_HEAD(&reaplist);
1392 spin_lock(&clp->cl_lock);
1393 unhash_openowner_locked(oo);
1394 while (!list_empty(&oo->oo_owner.so_stateids)) {
1395 stp = list_first_entry(&oo->oo_owner.so_stateids,
1396 struct nfs4_ol_stateid, st_perstateowner);
1397 if (unhash_open_stateid(stp, &reaplist))
1398 put_ol_stateid_locked(stp, &reaplist);
1400 spin_unlock(&clp->cl_lock);
1401 free_ol_stateid_reaplist(&reaplist);
1402 release_last_closed_stateid(oo);
1403 nfs4_put_stateowner(&oo->oo_owner);
1407 hash_sessionid(struct nfs4_sessionid *sessionid)
1409 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1411 return sid->sequence % SESSION_HASH_SIZE;
1414 #ifdef CONFIG_SUNRPC_DEBUG
1416 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1418 u32 *ptr = (u32 *)(&sessionid->data[0]);
1419 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1423 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1429 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1430 * won't be used for replay.
1432 void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1434 struct nfs4_stateowner *so = cstate->replay_owner;
1436 if (nfserr == nfserr_replay_me)
1439 if (!seqid_mutating_err(ntohl(nfserr))) {
1440 nfsd4_cstate_clear_replay(cstate);
1445 if (so->so_is_open_owner)
1446 release_last_closed_stateid(openowner(so));
1452 gen_sessionid(struct nfsd4_session *ses)
1454 struct nfs4_client *clp = ses->se_client;
1455 struct nfsd4_sessionid *sid;
1457 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1458 sid->clientid = clp->cl_clientid;
1459 sid->sequence = current_sessionid++;
1464 * The protocol defines ca_maxresponssize_cached to include the size of
1465 * the rpc header, but all we need to cache is the data starting after
1466 * the end of the initial SEQUENCE operation--the rest we regenerate
1467 * each time. Therefore we can advertise a ca_maxresponssize_cached
1468 * value that is the number of bytes in our cache plus a few additional
1469 * bytes. In order to stay on the safe side, and not promise more than
1470 * we can cache, those additional bytes must be the minimum possible: 24
1471 * bytes of rpc header (xid through accept state, with AUTH_NULL
1472 * verifier), 12 for the compound header (with zero-length tag), and 44
1473 * for the SEQUENCE op response:
1475 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1478 free_session_slots(struct nfsd4_session *ses)
1482 for (i = 0; i < ses->se_fchannel.maxreqs; i++) {
1483 free_svc_cred(&ses->se_slots[i]->sl_cred);
1484 kfree(ses->se_slots[i]);
1489 * We don't actually need to cache the rpc and session headers, so we
1490 * can allocate a little less for each slot:
1492 static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
1496 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1499 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1500 return size + sizeof(struct nfsd4_slot);
1504 * XXX: If we run out of reserved DRC memory we could (up to a point)
1505 * re-negotiate active sessions and reduce their slot usage to make
1506 * room for new connections. For now we just fail the create session.
1508 static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
1510 u32 slotsize = slot_bytes(ca);
1511 u32 num = ca->maxreqs;
1512 unsigned long avail, total_avail;
1514 spin_lock(&nfsd_drc_lock);
1515 total_avail = nfsd_drc_max_mem - nfsd_drc_mem_used;
1516 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, total_avail);
1518 * Never use more than a third of the remaining memory,
1519 * unless it's the only way to give this client a slot:
1521 avail = clamp_t(unsigned long, avail, slotsize, total_avail/3);
1522 num = min_t(int, num, avail / slotsize);
1523 nfsd_drc_mem_used += num * slotsize;
1524 spin_unlock(&nfsd_drc_lock);
1529 static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
1531 int slotsize = slot_bytes(ca);
1533 spin_lock(&nfsd_drc_lock);
1534 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
1535 spin_unlock(&nfsd_drc_lock);
1538 static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1539 struct nfsd4_channel_attrs *battrs)
1541 int numslots = fattrs->maxreqs;
1542 int slotsize = slot_bytes(fattrs);
1543 struct nfsd4_session *new;
1546 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
1547 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1548 mem = numslots * sizeof(struct nfsd4_slot *);
1550 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
1553 /* allocate each struct nfsd4_slot and data cache in one piece */
1554 for (i = 0; i < numslots; i++) {
1555 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
1556 if (!new->se_slots[i])
1560 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1561 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1566 kfree(new->se_slots[i]);
1571 static void free_conn(struct nfsd4_conn *c)
1573 svc_xprt_put(c->cn_xprt);
1577 static void nfsd4_conn_lost(struct svc_xpt_user *u)
1579 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1580 struct nfs4_client *clp = c->cn_session->se_client;
1582 spin_lock(&clp->cl_lock);
1583 if (!list_empty(&c->cn_persession)) {
1584 list_del(&c->cn_persession);
1587 nfsd4_probe_callback(clp);
1588 spin_unlock(&clp->cl_lock);
1591 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
1593 struct nfsd4_conn *conn;
1595 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1598 svc_xprt_get(rqstp->rq_xprt);
1599 conn->cn_xprt = rqstp->rq_xprt;
1600 conn->cn_flags = flags;
1601 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1605 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1607 conn->cn_session = ses;
1608 list_add(&conn->cn_persession, &ses->se_conns);
1611 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1613 struct nfs4_client *clp = ses->se_client;
1615 spin_lock(&clp->cl_lock);
1616 __nfsd4_hash_conn(conn, ses);
1617 spin_unlock(&clp->cl_lock);
1620 static int nfsd4_register_conn(struct nfsd4_conn *conn)
1622 conn->cn_xpt_user.callback = nfsd4_conn_lost;
1623 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
1626 static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
1630 nfsd4_hash_conn(conn, ses);
1631 ret = nfsd4_register_conn(conn);
1633 /* oops; xprt is already down: */
1634 nfsd4_conn_lost(&conn->cn_xpt_user);
1635 /* We may have gained or lost a callback channel: */
1636 nfsd4_probe_callback_sync(ses->se_client);
1639 static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
1641 u32 dir = NFS4_CDFC4_FORE;
1643 if (cses->flags & SESSION4_BACK_CHAN)
1644 dir |= NFS4_CDFC4_BACK;
1645 return alloc_conn(rqstp, dir);
1648 /* must be called under client_lock */
1649 static void nfsd4_del_conns(struct nfsd4_session *s)
1651 struct nfs4_client *clp = s->se_client;
1652 struct nfsd4_conn *c;
1654 spin_lock(&clp->cl_lock);
1655 while (!list_empty(&s->se_conns)) {
1656 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1657 list_del_init(&c->cn_persession);
1658 spin_unlock(&clp->cl_lock);
1660 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1663 spin_lock(&clp->cl_lock);
1665 spin_unlock(&clp->cl_lock);
1668 static void __free_session(struct nfsd4_session *ses)
1670 free_session_slots(ses);
1674 static void free_session(struct nfsd4_session *ses)
1676 nfsd4_del_conns(ses);
1677 nfsd4_put_drc_mem(&ses->se_fchannel);
1678 __free_session(ses);
1681 static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
1684 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1686 new->se_client = clp;
1689 INIT_LIST_HEAD(&new->se_conns);
1691 new->se_cb_seq_nr = 1;
1692 new->se_flags = cses->flags;
1693 new->se_cb_prog = cses->callback_prog;
1694 new->se_cb_sec = cses->cb_sec;
1695 atomic_set(&new->se_ref, 0);
1696 idx = hash_sessionid(&new->se_sessionid);
1697 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
1698 spin_lock(&clp->cl_lock);
1699 list_add(&new->se_perclnt, &clp->cl_sessions);
1700 spin_unlock(&clp->cl_lock);
1703 struct sockaddr *sa = svc_addr(rqstp);
1705 * This is a little silly; with sessions there's no real
1706 * use for the callback address. Use the peer address
1707 * as a reasonable default for now, but consider fixing
1708 * the rpc client not to require an address in the
1711 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1712 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
1716 /* caller must hold client_lock */
1717 static struct nfsd4_session *
1718 __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
1720 struct nfsd4_session *elem;
1722 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1724 lockdep_assert_held(&nn->client_lock);
1726 dump_sessionid(__func__, sessionid);
1727 idx = hash_sessionid(sessionid);
1728 /* Search in the appropriate list */
1729 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
1730 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1731 NFS4_MAX_SESSIONID_LEN)) {
1736 dprintk("%s: session not found\n", __func__);
1740 static struct nfsd4_session *
1741 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1744 struct nfsd4_session *session;
1745 __be32 status = nfserr_badsession;
1747 session = __find_in_sessionid_hashtbl(sessionid, net);
1750 status = nfsd4_get_session_locked(session);
1758 /* caller must hold client_lock */
1760 unhash_session(struct nfsd4_session *ses)
1762 struct nfs4_client *clp = ses->se_client;
1763 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1765 lockdep_assert_held(&nn->client_lock);
1767 list_del(&ses->se_hash);
1768 spin_lock(&ses->se_client->cl_lock);
1769 list_del(&ses->se_perclnt);
1770 spin_unlock(&ses->se_client->cl_lock);
1773 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1775 STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
1778 * We're assuming the clid was not given out from a boot
1779 * precisely 2^32 (about 136 years) before this one. That seems
1780 * a safe assumption:
1782 if (clid->cl_boot == (u32)nn->boot_time)
1784 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
1785 clid->cl_boot, clid->cl_id, nn->boot_time);
1790 * XXX Should we use a slab cache ?
1791 * This type of memory management is somewhat inefficient, but we use it
1792 * anyway since SETCLIENTID is not a common operation.
1794 static struct nfs4_client *alloc_client(struct xdr_netobj name)
1796 struct nfs4_client *clp;
1799 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1802 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
1803 if (clp->cl_name.data == NULL)
1805 clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
1806 OWNER_HASH_SIZE, GFP_KERNEL);
1807 if (!clp->cl_ownerstr_hashtbl)
1808 goto err_no_hashtbl;
1809 for (i = 0; i < OWNER_HASH_SIZE; i++)
1810 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
1811 clp->cl_name.len = name.len;
1812 INIT_LIST_HEAD(&clp->cl_sessions);
1813 idr_init(&clp->cl_stateids);
1814 atomic_set(&clp->cl_refcount, 0);
1815 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1816 INIT_LIST_HEAD(&clp->cl_idhash);
1817 INIT_LIST_HEAD(&clp->cl_openowners);
1818 INIT_LIST_HEAD(&clp->cl_delegations);
1819 INIT_LIST_HEAD(&clp->cl_lru);
1820 INIT_LIST_HEAD(&clp->cl_revoked);
1821 #ifdef CONFIG_NFSD_PNFS
1822 INIT_LIST_HEAD(&clp->cl_lo_states);
1824 spin_lock_init(&clp->cl_lock);
1825 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1828 kfree(clp->cl_name.data);
1835 free_client(struct nfs4_client *clp)
1837 while (!list_empty(&clp->cl_sessions)) {
1838 struct nfsd4_session *ses;
1839 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1841 list_del(&ses->se_perclnt);
1842 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1845 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
1846 free_svc_cred(&clp->cl_cred);
1847 kfree(clp->cl_ownerstr_hashtbl);
1848 kfree(clp->cl_name.data);
1849 idr_destroy(&clp->cl_stateids);
1853 /* must be called under the client_lock */
1855 unhash_client_locked(struct nfs4_client *clp)
1857 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1858 struct nfsd4_session *ses;
1860 lockdep_assert_held(&nn->client_lock);
1862 /* Mark the client as expired! */
1864 /* Make it invisible */
1865 if (!list_empty(&clp->cl_idhash)) {
1866 list_del_init(&clp->cl_idhash);
1867 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1868 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1870 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1872 list_del_init(&clp->cl_lru);
1873 spin_lock(&clp->cl_lock);
1874 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1875 list_del_init(&ses->se_hash);
1876 spin_unlock(&clp->cl_lock);
1880 unhash_client(struct nfs4_client *clp)
1882 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1884 spin_lock(&nn->client_lock);
1885 unhash_client_locked(clp);
1886 spin_unlock(&nn->client_lock);
1889 static __be32 mark_client_expired_locked(struct nfs4_client *clp)
1891 if (atomic_read(&clp->cl_refcount))
1892 return nfserr_jukebox;
1893 unhash_client_locked(clp);
1898 __destroy_client(struct nfs4_client *clp)
1901 struct nfs4_openowner *oo;
1902 struct nfs4_delegation *dp;
1903 struct list_head reaplist;
1905 INIT_LIST_HEAD(&reaplist);
1906 spin_lock(&state_lock);
1907 while (!list_empty(&clp->cl_delegations)) {
1908 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
1909 WARN_ON(!unhash_delegation_locked(dp));
1910 list_add(&dp->dl_recall_lru, &reaplist);
1912 spin_unlock(&state_lock);
1913 while (!list_empty(&reaplist)) {
1914 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
1915 list_del_init(&dp->dl_recall_lru);
1916 put_clnt_odstate(dp->dl_clnt_odstate);
1917 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
1918 nfs4_put_stid(&dp->dl_stid);
1920 while (!list_empty(&clp->cl_revoked)) {
1921 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
1922 list_del_init(&dp->dl_recall_lru);
1923 nfs4_put_stid(&dp->dl_stid);
1925 while (!list_empty(&clp->cl_openowners)) {
1926 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1927 nfs4_get_stateowner(&oo->oo_owner);
1928 release_openowner(oo);
1930 for (i = 0; i < OWNER_HASH_SIZE; i++) {
1931 struct nfs4_stateowner *so, *tmp;
1933 list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i],
1935 /* Should be no openowners at this point */
1936 WARN_ON_ONCE(so->so_is_open_owner);
1937 remove_blocked_locks(lockowner(so));
1940 nfsd4_return_all_client_layouts(clp);
1941 nfsd4_shutdown_callback(clp);
1942 if (clp->cl_cb_conn.cb_xprt)
1943 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
1948 destroy_client(struct nfs4_client *clp)
1951 __destroy_client(clp);
1954 static void expire_client(struct nfs4_client *clp)
1957 nfsd4_client_record_remove(clp);
1958 __destroy_client(clp);
1961 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1963 memcpy(target->cl_verifier.data, source->data,
1964 sizeof(target->cl_verifier.data));
1967 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1969 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1970 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1973 static int copy_cred(struct svc_cred *target, struct svc_cred *source)
1975 target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL);
1976 target->cr_raw_principal = kstrdup(source->cr_raw_principal,
1978 if ((source->cr_principal && ! target->cr_principal) ||
1979 (source->cr_raw_principal && ! target->cr_raw_principal))
1982 target->cr_flavor = source->cr_flavor;
1983 target->cr_uid = source->cr_uid;
1984 target->cr_gid = source->cr_gid;
1985 target->cr_group_info = source->cr_group_info;
1986 get_group_info(target->cr_group_info);
1987 target->cr_gss_mech = source->cr_gss_mech;
1988 if (source->cr_gss_mech)
1989 gss_mech_get(source->cr_gss_mech);
1994 compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1996 if (o1->len < o2->len)
1998 if (o1->len > o2->len)
2000 return memcmp(o1->data, o2->data, o1->len);
2003 static int same_name(const char *n1, const char *n2)
2005 return 0 == memcmp(n1, n2, HEXDIR_LEN);
2009 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
2011 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
2015 same_clid(clientid_t *cl1, clientid_t *cl2)
2017 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
2020 static bool groups_equal(struct group_info *g1, struct group_info *g2)
2024 if (g1->ngroups != g2->ngroups)
2026 for (i=0; i<g1->ngroups; i++)
2027 if (!gid_eq(g1->gid[i], g2->gid[i]))
2033 * RFC 3530 language requires clid_inuse be returned when the
2034 * "principal" associated with a requests differs from that previously
2035 * used. We use uid, gid's, and gss principal string as our best
2036 * approximation. We also don't want to allow non-gss use of a client
2037 * established using gss: in theory cr_principal should catch that
2038 * change, but in practice cr_principal can be null even in the gss case
2039 * since gssd doesn't always pass down a principal string.
2041 static bool is_gss_cred(struct svc_cred *cr)
2043 /* Is cr_flavor one of the gss "pseudoflavors"?: */
2044 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
2049 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
2051 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
2052 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
2053 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
2054 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
2056 if (cr1->cr_principal == cr2->cr_principal)
2058 if (!cr1->cr_principal || !cr2->cr_principal)
2060 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
2063 static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
2065 struct svc_cred *cr = &rqstp->rq_cred;
2068 if (!cr->cr_gss_mech)
2070 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
2071 return service == RPC_GSS_SVC_INTEGRITY ||
2072 service == RPC_GSS_SVC_PRIVACY;
2075 bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
2077 struct svc_cred *cr = &rqstp->rq_cred;
2079 if (!cl->cl_mach_cred)
2081 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
2083 if (!svc_rqst_integrity_protected(rqstp))
2085 if (cl->cl_cred.cr_raw_principal)
2086 return 0 == strcmp(cl->cl_cred.cr_raw_principal,
2087 cr->cr_raw_principal);
2088 if (!cr->cr_principal)
2090 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
2093 static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
2098 * This is opaque to client, so no need to byte-swap. Use
2099 * __force to keep sparse happy
2101 verf[0] = (__force __be32)get_seconds();
2102 verf[1] = (__force __be32)nn->clverifier_counter++;
2103 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
2106 static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
2108 clp->cl_clientid.cl_boot = nn->boot_time;
2109 clp->cl_clientid.cl_id = nn->clientid_counter++;
2110 gen_confirm(clp, nn);
2113 static struct nfs4_stid *
2114 find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
2116 struct nfs4_stid *ret;
2118 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
2119 if (!ret || !ret->sc_type)
2124 static struct nfs4_stid *
2125 find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
2127 struct nfs4_stid *s;
2129 spin_lock(&cl->cl_lock);
2130 s = find_stateid_locked(cl, t);
2132 if (typemask & s->sc_type)
2133 atomic_inc(&s->sc_count);
2137 spin_unlock(&cl->cl_lock);
2141 static struct nfs4_client *create_client(struct xdr_netobj name,
2142 struct svc_rqst *rqstp, nfs4_verifier *verf)
2144 struct nfs4_client *clp;
2145 struct sockaddr *sa = svc_addr(rqstp);
2147 struct net *net = SVC_NET(rqstp);
2149 clp = alloc_client(name);
2153 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
2158 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
2159 clp->cl_time = get_seconds();
2160 clear_bit(0, &clp->cl_cb_slot_busy);
2161 copy_verf(clp, verf);
2162 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
2163 clp->cl_cb_session = NULL;
2169 add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
2171 struct rb_node **new = &(root->rb_node), *parent = NULL;
2172 struct nfs4_client *clp;
2175 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
2178 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
2179 new = &((*new)->rb_left);
2181 new = &((*new)->rb_right);
2184 rb_link_node(&new_clp->cl_namenode, parent, new);
2185 rb_insert_color(&new_clp->cl_namenode, root);
2188 static struct nfs4_client *
2189 find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2192 struct rb_node *node = root->rb_node;
2193 struct nfs4_client *clp;
2196 clp = rb_entry(node, struct nfs4_client, cl_namenode);
2197 cmp = compare_blob(&clp->cl_name, name);
2199 node = node->rb_left;
2201 node = node->rb_right;
2209 add_to_unconfirmed(struct nfs4_client *clp)
2211 unsigned int idhashval;
2212 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2214 lockdep_assert_held(&nn->client_lock);
2216 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2217 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
2218 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
2219 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
2220 renew_client_locked(clp);
2224 move_to_confirmed(struct nfs4_client *clp)
2226 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
2227 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2229 lockdep_assert_held(&nn->client_lock);
2231 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
2232 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
2233 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
2234 add_clp_to_name_tree(clp, &nn->conf_name_tree);
2235 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2236 renew_client_locked(clp);
2239 static struct nfs4_client *
2240 find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
2242 struct nfs4_client *clp;
2243 unsigned int idhashval = clientid_hashval(clid->cl_id);
2245 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
2246 if (same_clid(&clp->cl_clientid, clid)) {
2247 if ((bool)clp->cl_minorversion != sessions)
2249 renew_client_locked(clp);
2256 static struct nfs4_client *
2257 find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2259 struct list_head *tbl = nn->conf_id_hashtbl;
2261 lockdep_assert_held(&nn->client_lock);
2262 return find_client_in_id_table(tbl, clid, sessions);
2265 static struct nfs4_client *
2266 find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2268 struct list_head *tbl = nn->unconf_id_hashtbl;
2270 lockdep_assert_held(&nn->client_lock);
2271 return find_client_in_id_table(tbl, clid, sessions);
2274 static bool clp_used_exchangeid(struct nfs4_client *clp)
2276 return clp->cl_exchange_flags != 0;
2279 static struct nfs4_client *
2280 find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
2282 lockdep_assert_held(&nn->client_lock);
2283 return find_clp_in_name_tree(name, &nn->conf_name_tree);
2286 static struct nfs4_client *
2287 find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
2289 lockdep_assert_held(&nn->client_lock);
2290 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
2294 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
2296 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
2297 struct sockaddr *sa = svc_addr(rqstp);
2298 u32 scopeid = rpc_get_scope_id(sa);
2299 unsigned short expected_family;
2301 /* Currently, we only support tcp and tcp6 for the callback channel */
2302 if (se->se_callback_netid_len == 3 &&
2303 !memcmp(se->se_callback_netid_val, "tcp", 3))
2304 expected_family = AF_INET;
2305 else if (se->se_callback_netid_len == 4 &&
2306 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2307 expected_family = AF_INET6;
2311 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
2312 se->se_callback_addr_len,
2313 (struct sockaddr *)&conn->cb_addr,
2314 sizeof(conn->cb_addr));
2316 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
2319 if (conn->cb_addr.ss_family == AF_INET6)
2320 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
2322 conn->cb_prog = se->se_callback_prog;
2323 conn->cb_ident = se->se_callback_ident;
2324 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
2327 conn->cb_addr.ss_family = AF_UNSPEC;
2328 conn->cb_addrlen = 0;
2329 dprintk("NFSD: this client (clientid %08x/%08x) "
2330 "will not receive delegations\n",
2331 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2337 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
2340 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2342 struct xdr_buf *buf = resp->xdr.buf;
2343 struct nfsd4_slot *slot = resp->cstate.slot;
2346 dprintk("--> %s slot %p\n", __func__, slot);
2348 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
2349 slot->sl_opcnt = resp->opcnt;
2350 slot->sl_status = resp->cstate.status;
2351 free_svc_cred(&slot->sl_cred);
2352 copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred);
2354 if (!nfsd4_cache_this(resp)) {
2355 slot->sl_flags &= ~NFSD4_SLOT_CACHED;
2358 slot->sl_flags |= NFSD4_SLOT_CACHED;
2360 base = resp->cstate.data_offset;
2361 slot->sl_datalen = buf->len - base;
2362 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
2363 WARN(1, "%s: sessions DRC could not cache compound\n",
2369 * Encode the replay sequence operation from the slot values.
2370 * If cachethis is FALSE encode the uncached rep error on the next
2371 * operation which sets resp->p and increments resp->opcnt for
2372 * nfs4svc_encode_compoundres.
2376 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2377 struct nfsd4_compoundres *resp)
2379 struct nfsd4_op *op;
2380 struct nfsd4_slot *slot = resp->cstate.slot;
2382 /* Encode the replayed sequence operation */
2383 op = &args->ops[resp->opcnt - 1];
2384 nfsd4_encode_operation(resp, op);
2386 if (slot->sl_flags & NFSD4_SLOT_CACHED)
2388 if (args->opcnt == 1) {
2390 * The original operation wasn't a solo sequence--we
2391 * always cache those--so this retry must not match the
2394 op->status = nfserr_seq_false_retry;
2396 op = &args->ops[resp->opcnt++];
2397 op->status = nfserr_retry_uncached_rep;
2398 nfsd4_encode_operation(resp, op);
2404 * The sequence operation is not cached because we can use the slot and
2408 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2409 struct nfsd4_sequence *seq)
2411 struct nfsd4_slot *slot = resp->cstate.slot;
2412 struct xdr_stream *xdr = &resp->xdr;
2416 dprintk("--> %s slot %p\n", __func__, slot);
2418 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
2422 p = xdr_reserve_space(xdr, slot->sl_datalen);
2425 return nfserr_serverfault;
2427 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2428 xdr_commit_encode(xdr);
2430 resp->opcnt = slot->sl_opcnt;
2431 return slot->sl_status;
2435 * Set the exchange_id flags returned by the server.
2438 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2440 #ifdef CONFIG_NFSD_PNFS
2441 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
2443 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
2446 /* Referrals are supported, Migration is not. */
2447 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2449 /* set the wire flags to return to client. */
2450 clid->flags = new->cl_exchange_flags;
2453 static bool client_has_openowners(struct nfs4_client *clp)
2455 struct nfs4_openowner *oo;
2457 list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
2458 if (!list_empty(&oo->oo_owner.so_stateids))
2464 static bool client_has_state(struct nfs4_client *clp)
2466 return client_has_openowners(clp)
2467 #ifdef CONFIG_NFSD_PNFS
2468 || !list_empty(&clp->cl_lo_states)
2470 || !list_empty(&clp->cl_delegations)
2471 || !list_empty(&clp->cl_sessions);
2475 nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2476 union nfsd4_op_u *u)
2478 struct nfsd4_exchange_id *exid = &u->exchange_id;
2479 struct nfs4_client *conf, *new;
2480 struct nfs4_client *unconf = NULL;
2482 char addr_str[INET6_ADDRSTRLEN];
2483 nfs4_verifier verf = exid->verifier;
2484 struct sockaddr *sa = svc_addr(rqstp);
2485 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
2486 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2488 rpc_ntop(sa, addr_str, sizeof(addr_str));
2489 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
2490 "ip_addr=%s flags %x, spa_how %d\n",
2491 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
2492 addr_str, exid->flags, exid->spa_how);
2494 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
2495 return nfserr_inval;
2497 new = create_client(exid->clname, rqstp, &verf);
2499 return nfserr_jukebox;
2501 switch (exid->spa_how) {
2503 exid->spo_must_enforce[0] = 0;
2504 exid->spo_must_enforce[1] = (
2505 1 << (OP_BIND_CONN_TO_SESSION - 32) |
2506 1 << (OP_EXCHANGE_ID - 32) |
2507 1 << (OP_CREATE_SESSION - 32) |
2508 1 << (OP_DESTROY_SESSION - 32) |
2509 1 << (OP_DESTROY_CLIENTID - 32));
2511 exid->spo_must_allow[0] &= (1 << (OP_CLOSE) |
2512 1 << (OP_OPEN_DOWNGRADE) |
2514 1 << (OP_DELEGRETURN));
2516 exid->spo_must_allow[1] &= (
2517 1 << (OP_TEST_STATEID - 32) |
2518 1 << (OP_FREE_STATEID - 32));
2519 if (!svc_rqst_integrity_protected(rqstp)) {
2520 status = nfserr_inval;
2524 * Sometimes userspace doesn't give us a principal.
2525 * Which is a bug, really. Anyway, we can't enforce
2526 * MACH_CRED in that case, better to give up now:
2528 if (!new->cl_cred.cr_principal &&
2529 !new->cl_cred.cr_raw_principal) {
2530 status = nfserr_serverfault;
2533 new->cl_mach_cred = true;
2536 default: /* checked by xdr code */
2539 status = nfserr_encr_alg_unsupp;
2543 /* Cases below refer to rfc 5661 section 18.35.4: */
2544 spin_lock(&nn->client_lock);
2545 conf = find_confirmed_client_by_name(&exid->clname, nn);
2547 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2548 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2551 if (!clp_used_exchangeid(conf)) { /* buggy client */
2552 status = nfserr_inval;
2555 if (!nfsd4_mach_creds_match(conf, rqstp)) {
2556 status = nfserr_wrong_cred;
2559 if (!creds_match) { /* case 9 */
2560 status = nfserr_perm;
2563 if (!verfs_match) { /* case 8 */
2564 status = nfserr_not_same;
2568 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
2571 if (!creds_match) { /* case 3 */
2572 if (client_has_state(conf)) {
2573 status = nfserr_clid_inuse;
2578 if (verfs_match) { /* case 2 */
2579 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
2582 /* case 5, client reboot */
2587 if (update) { /* case 7 */
2588 status = nfserr_noent;
2592 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
2593 if (unconf) /* case 4, possible retry or client restart */
2594 unhash_client_locked(unconf);
2596 /* case 1 (normal case) */
2599 status = mark_client_expired_locked(conf);
2603 new->cl_minorversion = cstate->minorversion;
2604 new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0];
2605 new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1];
2608 add_to_unconfirmed(new);
2611 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
2612 exid->clientid.cl_id = conf->cl_clientid.cl_id;
2614 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
2615 nfsd4_set_ex_flags(conf, exid);
2617 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
2618 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
2622 spin_unlock(&nn->client_lock);
2627 expire_client(unconf);
2632 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
2634 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2637 /* The slot is in use, and no response has been sent. */
2639 if (seqid == slot_seqid)
2640 return nfserr_jukebox;
2642 return nfserr_seq_misordered;
2644 /* Note unsigned 32-bit arithmetic handles wraparound: */
2645 if (likely(seqid == slot_seqid + 1))
2647 if (seqid == slot_seqid)
2648 return nfserr_replay_cache;
2649 return nfserr_seq_misordered;
2653 * Cache the create session result into the create session single DRC
2654 * slot cache by saving the xdr structure. sl_seqid has been set.
2655 * Do this for solo or embedded create session operations.
2658 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
2659 struct nfsd4_clid_slot *slot, __be32 nfserr)
2661 slot->sl_status = nfserr;
2662 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2666 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2667 struct nfsd4_clid_slot *slot)
2669 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2670 return slot->sl_status;
2673 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2674 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2675 1 + /* MIN tag is length with zero, only length */ \
2676 3 + /* version, opcount, opcode */ \
2677 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2678 /* seqid, slotID, slotID, cache */ \
2679 4 ) * sizeof(__be32))
2681 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2682 2 + /* verifier: AUTH_NULL, length 0 */\
2684 1 + /* MIN tag is length with zero, only length */ \
2685 3 + /* opcount, opcode, opstatus*/ \
2686 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2687 /* seqid, slotID, slotID, slotID, status */ \
2688 5 ) * sizeof(__be32))
2690 static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
2692 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2694 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2695 return nfserr_toosmall;
2696 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2697 return nfserr_toosmall;
2698 ca->headerpadsz = 0;
2699 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2700 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2701 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2702 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2703 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2704 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2706 * Note decreasing slot size below client's request may make it
2707 * difficult for client to function correctly, whereas
2708 * decreasing the number of slots will (just?) affect
2709 * performance. When short on memory we therefore prefer to
2710 * decrease number of slots instead of their size. Clients that
2711 * request larger slots than they need will get poor results:
2713 ca->maxreqs = nfsd4_get_drc_mem(ca);
2715 return nfserr_jukebox;
2721 * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
2722 * These are based on similar macros in linux/sunrpc/msg_prot.h .
2724 #define RPC_MAX_HEADER_WITH_AUTH_SYS \
2725 (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
2727 #define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
2728 (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
2730 #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2731 RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
2732 #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2733 RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
2736 static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2738 ca->headerpadsz = 0;
2740 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
2741 return nfserr_toosmall;
2742 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
2743 return nfserr_toosmall;
2744 ca->maxresp_cached = 0;
2746 return nfserr_toosmall;
2751 static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2753 switch (cbs->flavor) {
2759 * GSS case: the spec doesn't allow us to return this
2760 * error. But it also doesn't allow us not to support
2762 * I'd rather this fail hard than return some error the
2763 * client might think it can already handle:
2765 return nfserr_encr_alg_unsupp;
2770 nfsd4_create_session(struct svc_rqst *rqstp,
2771 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
2773 struct nfsd4_create_session *cr_ses = &u->create_session;
2774 struct sockaddr *sa = svc_addr(rqstp);
2775 struct nfs4_client *conf, *unconf;
2776 struct nfs4_client *old = NULL;
2777 struct nfsd4_session *new;
2778 struct nfsd4_conn *conn;
2779 struct nfsd4_clid_slot *cs_slot = NULL;
2781 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2783 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2784 return nfserr_inval;
2785 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2788 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
2791 status = check_backchannel_attrs(&cr_ses->back_channel);
2793 goto out_release_drc_mem;
2794 status = nfserr_jukebox;
2795 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
2797 goto out_release_drc_mem;
2798 conn = alloc_conn_from_crses(rqstp, cr_ses);
2800 goto out_free_session;
2802 spin_lock(&nn->client_lock);
2803 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
2804 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
2805 WARN_ON_ONCE(conf && unconf);
2808 status = nfserr_wrong_cred;
2809 if (!nfsd4_mach_creds_match(conf, rqstp))
2811 cs_slot = &conf->cl_cs_slot;
2812 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2814 if (status == nfserr_replay_cache)
2815 status = nfsd4_replay_create_session(cr_ses, cs_slot);
2818 } else if (unconf) {
2819 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
2820 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
2821 status = nfserr_clid_inuse;
2824 status = nfserr_wrong_cred;
2825 if (!nfsd4_mach_creds_match(unconf, rqstp))
2827 cs_slot = &unconf->cl_cs_slot;
2828 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2830 /* an unconfirmed replay returns misordered */
2831 status = nfserr_seq_misordered;
2834 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
2836 status = mark_client_expired_locked(old);
2842 move_to_confirmed(unconf);
2845 status = nfserr_stale_clientid;
2849 /* Persistent sessions are not supported */
2850 cr_ses->flags &= ~SESSION4_PERSIST;
2851 /* Upshifting from TCP to RDMA is not supported */
2852 cr_ses->flags &= ~SESSION4_RDMA;
2854 init_session(rqstp, new, conf, cr_ses);
2855 nfsd4_get_session_locked(new);
2857 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
2858 NFS4_MAX_SESSIONID_LEN);
2859 cs_slot->sl_seqid++;
2860 cr_ses->seqid = cs_slot->sl_seqid;
2862 /* cache solo and embedded create sessions under the client_lock */
2863 nfsd4_cache_create_session(cr_ses, cs_slot, status);
2864 spin_unlock(&nn->client_lock);
2865 /* init connection and backchannel */
2866 nfsd4_init_conn(rqstp, conn, new);
2867 nfsd4_put_session(new);
2872 spin_unlock(&nn->client_lock);
2877 __free_session(new);
2878 out_release_drc_mem:
2879 nfsd4_put_drc_mem(&cr_ses->fore_channel);
2883 static __be32 nfsd4_map_bcts_dir(u32 *dir)
2886 case NFS4_CDFC4_FORE:
2887 case NFS4_CDFC4_BACK:
2889 case NFS4_CDFC4_FORE_OR_BOTH:
2890 case NFS4_CDFC4_BACK_OR_BOTH:
2891 *dir = NFS4_CDFC4_BOTH;
2894 return nfserr_inval;
2897 __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp,
2898 struct nfsd4_compound_state *cstate,
2899 union nfsd4_op_u *u)
2901 struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl;
2902 struct nfsd4_session *session = cstate->session;
2903 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2906 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2909 spin_lock(&nn->client_lock);
2910 session->se_cb_prog = bc->bc_cb_program;
2911 session->se_cb_sec = bc->bc_cb_sec;
2912 spin_unlock(&nn->client_lock);
2914 nfsd4_probe_callback(session->se_client);
2919 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2920 struct nfsd4_compound_state *cstate,
2921 union nfsd4_op_u *u)
2923 struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session;
2925 struct nfsd4_conn *conn;
2926 struct nfsd4_session *session;
2927 struct net *net = SVC_NET(rqstp);
2928 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2930 if (!nfsd4_last_compound_op(rqstp))
2931 return nfserr_not_only_op;
2932 spin_lock(&nn->client_lock);
2933 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
2934 spin_unlock(&nn->client_lock);
2936 goto out_no_session;
2937 status = nfserr_wrong_cred;
2938 if (!nfsd4_mach_creds_match(session->se_client, rqstp))
2940 status = nfsd4_map_bcts_dir(&bcts->dir);
2943 conn = alloc_conn(rqstp, bcts->dir);
2944 status = nfserr_jukebox;
2947 nfsd4_init_conn(rqstp, conn, session);
2950 nfsd4_put_session(session);
2955 static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2959 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2963 nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate,
2964 union nfsd4_op_u *u)
2966 struct nfsd4_destroy_session *sessionid = &u->destroy_session;
2967 struct nfsd4_session *ses;
2969 int ref_held_by_me = 0;
2970 struct net *net = SVC_NET(r);
2971 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2973 status = nfserr_not_only_op;
2974 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
2975 if (!nfsd4_last_compound_op(r))
2979 dump_sessionid(__func__, &sessionid->sessionid);
2980 spin_lock(&nn->client_lock);
2981 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
2983 goto out_client_lock;
2984 status = nfserr_wrong_cred;
2985 if (!nfsd4_mach_creds_match(ses->se_client, r))
2986 goto out_put_session;
2987 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
2989 goto out_put_session;
2990 unhash_session(ses);
2991 spin_unlock(&nn->client_lock);
2993 nfsd4_probe_callback_sync(ses->se_client);
2995 spin_lock(&nn->client_lock);
2998 nfsd4_put_session_locked(ses);
3000 spin_unlock(&nn->client_lock);
3005 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
3007 struct nfsd4_conn *c;
3009 list_for_each_entry(c, &s->se_conns, cn_persession) {
3010 if (c->cn_xprt == xpt) {
3017 static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
3019 struct nfs4_client *clp = ses->se_client;
3020 struct nfsd4_conn *c;
3021 __be32 status = nfs_ok;
3024 spin_lock(&clp->cl_lock);
3025 c = __nfsd4_find_conn(new->cn_xprt, ses);
3028 status = nfserr_conn_not_bound_to_session;
3029 if (clp->cl_mach_cred)
3031 __nfsd4_hash_conn(new, ses);
3032 spin_unlock(&clp->cl_lock);
3033 ret = nfsd4_register_conn(new);
3035 /* oops; xprt is already down: */
3036 nfsd4_conn_lost(&new->cn_xpt_user);
3039 spin_unlock(&clp->cl_lock);
3044 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
3046 struct nfsd4_compoundargs *args = rqstp->rq_argp;
3048 return args->opcnt > session->se_fchannel.maxops;
3051 static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
3052 struct nfsd4_session *session)
3054 struct xdr_buf *xb = &rqstp->rq_arg;
3056 return xb->len > session->se_fchannel.maxreq_sz;
3059 static bool replay_matches_cache(struct svc_rqst *rqstp,
3060 struct nfsd4_sequence *seq, struct nfsd4_slot *slot)
3062 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
3064 if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) !=
3065 (bool)seq->cachethis)
3068 * If there's an error then the reply can have fewer ops than
3071 if (slot->sl_opcnt < argp->opcnt && !slot->sl_status)
3074 * But if we cached a reply with *more* ops than the call you're
3075 * sending us now, then this new call is clearly not really a
3076 * replay of the old one:
3078 if (slot->sl_opcnt > argp->opcnt)
3080 /* This is the only check explicitly called by spec: */
3081 if (!same_creds(&rqstp->rq_cred, &slot->sl_cred))
3084 * There may be more comparisons we could actually do, but the
3085 * spec doesn't require us to catch every case where the calls
3086 * don't match (that would require caching the call as well as
3087 * the reply), so we don't bother.
3093 nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3094 union nfsd4_op_u *u)
3096 struct nfsd4_sequence *seq = &u->sequence;
3097 struct nfsd4_compoundres *resp = rqstp->rq_resp;
3098 struct xdr_stream *xdr = &resp->xdr;
3099 struct nfsd4_session *session;
3100 struct nfs4_client *clp;
3101 struct nfsd4_slot *slot;
3102 struct nfsd4_conn *conn;
3105 struct net *net = SVC_NET(rqstp);
3106 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3108 if (resp->opcnt != 1)
3109 return nfserr_sequence_pos;
3112 * Will be either used or freed by nfsd4_sequence_check_conn
3115 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
3117 return nfserr_jukebox;
3119 spin_lock(&nn->client_lock);
3120 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
3122 goto out_no_session;
3123 clp = session->se_client;
3125 status = nfserr_too_many_ops;
3126 if (nfsd4_session_too_many_ops(rqstp, session))
3127 goto out_put_session;
3129 status = nfserr_req_too_big;
3130 if (nfsd4_request_too_big(rqstp, session))
3131 goto out_put_session;
3133 status = nfserr_badslot;
3134 if (seq->slotid >= session->se_fchannel.maxreqs)
3135 goto out_put_session;
3137 slot = session->se_slots[seq->slotid];
3138 dprintk("%s: slotid %d\n", __func__, seq->slotid);
3140 /* We do not negotiate the number of slots yet, so set the
3141 * maxslots to the session maxreqs which is used to encode
3142 * sr_highest_slotid and the sr_target_slot id to maxslots */
3143 seq->maxslots = session->se_fchannel.maxreqs;
3145 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
3146 slot->sl_flags & NFSD4_SLOT_INUSE);
3147 if (status == nfserr_replay_cache) {
3148 status = nfserr_seq_misordered;
3149 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
3150 goto out_put_session;
3151 status = nfserr_seq_false_retry;
3152 if (!replay_matches_cache(rqstp, seq, slot))
3153 goto out_put_session;
3154 cstate->slot = slot;
3155 cstate->session = session;
3157 /* Return the cached reply status and set cstate->status
3158 * for nfsd4_proc_compound processing */
3159 status = nfsd4_replay_cache_entry(resp, seq);
3160 cstate->status = nfserr_replay_cache;
3164 goto out_put_session;
3166 status = nfsd4_sequence_check_conn(conn, session);
3169 goto out_put_session;
3171 buflen = (seq->cachethis) ?
3172 session->se_fchannel.maxresp_cached :
3173 session->se_fchannel.maxresp_sz;
3174 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
3176 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
3177 goto out_put_session;
3178 svc_reserve(rqstp, buflen);
3181 /* Success! bump slot seqid */
3182 slot->sl_seqid = seq->seqid;
3183 slot->sl_flags |= NFSD4_SLOT_INUSE;
3185 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
3187 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
3189 cstate->slot = slot;
3190 cstate->session = session;
3194 switch (clp->cl_cb_state) {
3196 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
3198 case NFSD4_CB_FAULT:
3199 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
3202 seq->status_flags = 0;
3204 if (!list_empty(&clp->cl_revoked))
3205 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
3209 spin_unlock(&nn->client_lock);
3212 nfsd4_put_session_locked(session);
3213 goto out_no_session;
3217 nfsd4_sequence_done(struct nfsd4_compoundres *resp)
3219 struct nfsd4_compound_state *cs = &resp->cstate;
3221 if (nfsd4_has_session(cs)) {
3222 if (cs->status != nfserr_replay_cache) {
3223 nfsd4_store_cache_entry(resp);
3224 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
3226 /* Drop session reference that was taken in nfsd4_sequence() */
3227 nfsd4_put_session(cs->session);
3229 put_client_renew(cs->clp);
3233 nfsd4_destroy_clientid(struct svc_rqst *rqstp,
3234 struct nfsd4_compound_state *cstate,
3235 union nfsd4_op_u *u)
3237 struct nfsd4_destroy_clientid *dc = &u->destroy_clientid;
3238 struct nfs4_client *conf, *unconf;
3239 struct nfs4_client *clp = NULL;
3241 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3243 spin_lock(&nn->client_lock);
3244 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
3245 conf = find_confirmed_client(&dc->clientid, true, nn);
3246 WARN_ON_ONCE(conf && unconf);
3249 if (client_has_state(conf)) {
3250 status = nfserr_clientid_busy;
3253 status = mark_client_expired_locked(conf);
3260 status = nfserr_stale_clientid;
3263 if (!nfsd4_mach_creds_match(clp, rqstp)) {
3265 status = nfserr_wrong_cred;
3268 unhash_client_locked(clp);
3270 spin_unlock(&nn->client_lock);
3277 nfsd4_reclaim_complete(struct svc_rqst *rqstp,
3278 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
3280 struct nfsd4_reclaim_complete *rc = &u->reclaim_complete;
3283 if (rc->rca_one_fs) {
3284 if (!cstate->current_fh.fh_dentry)
3285 return nfserr_nofilehandle;
3287 * We don't take advantage of the rca_one_fs case.
3288 * That's OK, it's optional, we can safely ignore it.
3293 status = nfserr_complete_already;
3294 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
3295 &cstate->session->se_client->cl_flags))
3298 status = nfserr_stale_clientid;
3299 if (is_client_expired(cstate->session->se_client))
3301 * The following error isn't really legal.
3302 * But we only get here if the client just explicitly
3303 * destroyed the client. Surely it no longer cares what
3304 * error it gets back on an operation for the dead
3310 nfsd4_client_record_create(cstate->session->se_client);
3316 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3317 union nfsd4_op_u *u)
3319 struct nfsd4_setclientid *setclid = &u->setclientid;
3320 struct xdr_netobj clname = setclid->se_name;
3321 nfs4_verifier clverifier = setclid->se_verf;
3322 struct nfs4_client *conf, *new;
3323 struct nfs4_client *unconf = NULL;
3325 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3327 new = create_client(clname, rqstp, &clverifier);
3329 return nfserr_jukebox;
3330 /* Cases below refer to rfc 3530 section 14.2.33: */
3331 spin_lock(&nn->client_lock);
3332 conf = find_confirmed_client_by_name(&clname, nn);
3333 if (conf && client_has_state(conf)) {
3335 status = nfserr_clid_inuse;
3336 if (clp_used_exchangeid(conf))
3338 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
3339 char addr_str[INET6_ADDRSTRLEN];
3340 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
3342 dprintk("NFSD: setclientid: string in use by client "
3343 "at %s\n", addr_str);
3347 unconf = find_unconfirmed_client_by_name(&clname, nn);
3349 unhash_client_locked(unconf);
3350 if (conf && same_verf(&conf->cl_verifier, &clverifier)) {
3351 /* case 1: probable callback update */
3352 copy_clid(new, conf);
3353 gen_confirm(new, nn);
3354 } else /* case 4 (new client) or cases 2, 3 (client reboot): */
3356 new->cl_minorversion = 0;
3357 gen_callback(new, setclid, rqstp);
3358 add_to_unconfirmed(new);
3359 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
3360 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
3361 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
3365 spin_unlock(&nn->client_lock);
3369 expire_client(unconf);
3375 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
3376 struct nfsd4_compound_state *cstate,
3377 union nfsd4_op_u *u)
3379 struct nfsd4_setclientid_confirm *setclientid_confirm =
3380 &u->setclientid_confirm;
3381 struct nfs4_client *conf, *unconf;
3382 struct nfs4_client *old = NULL;
3383 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
3384 clientid_t * clid = &setclientid_confirm->sc_clientid;
3386 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3388 if (STALE_CLIENTID(clid, nn))
3389 return nfserr_stale_clientid;
3391 spin_lock(&nn->client_lock);
3392 conf = find_confirmed_client(clid, false, nn);
3393 unconf = find_unconfirmed_client(clid, false, nn);
3395 * We try hard to give out unique clientid's, so if we get an
3396 * attempt to confirm the same clientid with a different cred,
3397 * the client may be buggy; this should never happen.
3399 * Nevertheless, RFC 7530 recommends INUSE for this case:
3401 status = nfserr_clid_inuse;
3402 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3404 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3406 /* cases below refer to rfc 3530 section 14.2.34: */
3407 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
3408 if (conf && same_verf(&confirm, &conf->cl_confirm)) {
3409 /* case 2: probable retransmit */
3411 } else /* case 4: client hasn't noticed we rebooted yet? */
3412 status = nfserr_stale_clientid;
3416 if (conf) { /* case 1: callback update */
3418 unhash_client_locked(old);
3419 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
3420 } else { /* case 3: normal case; new or rebooted client */
3421 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3423 status = nfserr_clid_inuse;
3424 if (client_has_state(old)
3425 && !same_creds(&unconf->cl_cred,
3430 status = mark_client_expired_locked(old);
3436 move_to_confirmed(unconf);
3439 get_client_locked(conf);
3440 spin_unlock(&nn->client_lock);
3441 nfsd4_probe_callback(conf);
3442 spin_lock(&nn->client_lock);
3443 put_client_renew_locked(conf);
3445 spin_unlock(&nn->client_lock);
3451 static struct nfs4_file *nfsd4_alloc_file(void)
3453 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3456 /* OPEN Share state helper functions */
3457 static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
3458 struct nfs4_file *fp)
3460 lockdep_assert_held(&state_lock);
3462 atomic_set(&fp->fi_ref, 1);
3463 spin_lock_init(&fp->fi_lock);
3464 INIT_LIST_HEAD(&fp->fi_stateids);
3465 INIT_LIST_HEAD(&fp->fi_delegations);
3466 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
3467 fh_copy_shallow(&fp->fi_fhandle, fh);
3468 fp->fi_deleg_file = NULL;
3469 fp->fi_had_conflict = false;
3470 fp->fi_share_deny = 0;
3471 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3472 memset(fp->fi_access, 0, sizeof(fp->fi_access));
3473 #ifdef CONFIG_NFSD_PNFS
3474 INIT_LIST_HEAD(&fp->fi_lo_states);
3475 atomic_set(&fp->fi_lo_recalls, 0);
3477 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
3481 nfsd4_free_slabs(void)
3483 kmem_cache_destroy(odstate_slab);
3484 kmem_cache_destroy(openowner_slab);
3485 kmem_cache_destroy(lockowner_slab);
3486 kmem_cache_destroy(file_slab);
3487 kmem_cache_destroy(stateid_slab);
3488 kmem_cache_destroy(deleg_slab);
3492 nfsd4_init_slabs(void)
3494 openowner_slab = kmem_cache_create("nfsd4_openowners",
3495 sizeof(struct nfs4_openowner), 0, 0, NULL);
3496 if (openowner_slab == NULL)
3498 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
3499 sizeof(struct nfs4_lockowner), 0, 0, NULL);
3500 if (lockowner_slab == NULL)
3501 goto out_free_openowner_slab;
3502 file_slab = kmem_cache_create("nfsd4_files",
3503 sizeof(struct nfs4_file), 0, 0, NULL);
3504 if (file_slab == NULL)
3505 goto out_free_lockowner_slab;
3506 stateid_slab = kmem_cache_create("nfsd4_stateids",
3507 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
3508 if (stateid_slab == NULL)
3509 goto out_free_file_slab;
3510 deleg_slab = kmem_cache_create("nfsd4_delegations",
3511 sizeof(struct nfs4_delegation), 0, 0, NULL);
3512 if (deleg_slab == NULL)
3513 goto out_free_stateid_slab;
3514 odstate_slab = kmem_cache_create("nfsd4_odstate",
3515 sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
3516 if (odstate_slab == NULL)
3517 goto out_free_deleg_slab;
3520 out_free_deleg_slab:
3521 kmem_cache_destroy(deleg_slab);
3522 out_free_stateid_slab:
3523 kmem_cache_destroy(stateid_slab);
3525 kmem_cache_destroy(file_slab);
3526 out_free_lockowner_slab:
3527 kmem_cache_destroy(lockowner_slab);
3528 out_free_openowner_slab:
3529 kmem_cache_destroy(openowner_slab);
3531 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3535 static void init_nfs4_replay(struct nfs4_replay *rp)
3537 rp->rp_status = nfserr_serverfault;
3539 rp->rp_buf = rp->rp_ibuf;
3540 mutex_init(&rp->rp_mutex);
3543 static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3544 struct nfs4_stateowner *so)
3546 if (!nfsd4_has_session(cstate)) {
3547 mutex_lock(&so->so_replay.rp_mutex);
3548 cstate->replay_owner = nfs4_get_stateowner(so);
3552 void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3554 struct nfs4_stateowner *so = cstate->replay_owner;
3557 cstate->replay_owner = NULL;
3558 mutex_unlock(&so->so_replay.rp_mutex);
3559 nfs4_put_stateowner(so);
3563 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
3565 struct nfs4_stateowner *sop;
3567 sop = kmem_cache_alloc(slab, GFP_KERNEL);
3571 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3572 if (!sop->so_owner.data) {
3573 kmem_cache_free(slab, sop);
3576 sop->so_owner.len = owner->len;
3578 INIT_LIST_HEAD(&sop->so_stateids);
3579 sop->so_client = clp;
3580 init_nfs4_replay(&sop->so_replay);
3581 atomic_set(&sop->so_count, 1);
3585 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
3587 lockdep_assert_held(&clp->cl_lock);
3589 list_add(&oo->oo_owner.so_strhash,
3590 &clp->cl_ownerstr_hashtbl[strhashval]);
3591 list_add(&oo->oo_perclient, &clp->cl_openowners);
3594 static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3596 unhash_openowner_locked(openowner(so));
3599 static void nfs4_free_openowner(struct nfs4_stateowner *so)
3601 struct nfs4_openowner *oo = openowner(so);
3603 kmem_cache_free(openowner_slab, oo);
3606 static const struct nfs4_stateowner_operations openowner_ops = {
3607 .so_unhash = nfs4_unhash_openowner,
3608 .so_free = nfs4_free_openowner,
3611 static struct nfs4_ol_stateid *
3612 nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
3614 struct nfs4_ol_stateid *local, *ret = NULL;
3615 struct nfs4_openowner *oo = open->op_openowner;
3617 lockdep_assert_held(&fp->fi_lock);
3619 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
3620 /* ignore lock owners */
3621 if (local->st_stateowner->so_is_open_owner == 0)
3623 if (local->st_stateowner != &oo->oo_owner)
3625 if (local->st_stid.sc_type == NFS4_OPEN_STID) {
3627 atomic_inc(&ret->st_stid.sc_count);
3635 nfsd4_verify_open_stid(struct nfs4_stid *s)
3637 __be32 ret = nfs_ok;
3639 switch (s->sc_type) {
3642 case NFS4_CLOSED_STID:
3643 case NFS4_CLOSED_DELEG_STID:
3644 ret = nfserr_bad_stateid;
3646 case NFS4_REVOKED_DELEG_STID:
3647 ret = nfserr_deleg_revoked;
3652 /* Lock the stateid st_mutex, and deal with races with CLOSE */
3654 nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp)
3658 mutex_lock(&stp->st_mutex);
3659 ret = nfsd4_verify_open_stid(&stp->st_stid);
3661 mutex_unlock(&stp->st_mutex);
3665 static struct nfs4_ol_stateid *
3666 nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
3668 struct nfs4_ol_stateid *stp;
3670 spin_lock(&fp->fi_lock);
3671 stp = nfsd4_find_existing_open(fp, open);
3672 spin_unlock(&fp->fi_lock);
3673 if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok)
3675 nfs4_put_stid(&stp->st_stid);
3680 static struct nfs4_openowner *
3681 alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
3682 struct nfsd4_compound_state *cstate)
3684 struct nfs4_client *clp = cstate->clp;
3685 struct nfs4_openowner *oo, *ret;
3687 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3690 oo->oo_owner.so_ops = &openowner_ops;
3691 oo->oo_owner.so_is_open_owner = 1;
3692 oo->oo_owner.so_seqid = open->op_seqid;
3694 if (nfsd4_has_session(cstate))
3695 oo->oo_flags |= NFS4_OO_CONFIRMED;
3697 oo->oo_last_closed_stid = NULL;
3698 INIT_LIST_HEAD(&oo->oo_close_lru);
3699 spin_lock(&clp->cl_lock);
3700 ret = find_openstateowner_str_locked(strhashval, open, clp);
3702 hash_openowner(oo, clp, strhashval);
3705 nfs4_free_stateowner(&oo->oo_owner);
3707 spin_unlock(&clp->cl_lock);
3711 static struct nfs4_ol_stateid *
3712 init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
3715 struct nfs4_openowner *oo = open->op_openowner;
3716 struct nfs4_ol_stateid *retstp = NULL;
3717 struct nfs4_ol_stateid *stp;
3720 /* We are moving these outside of the spinlocks to avoid the warnings */
3721 mutex_init(&stp->st_mutex);
3722 mutex_lock(&stp->st_mutex);
3725 spin_lock(&oo->oo_owner.so_client->cl_lock);
3726 spin_lock(&fp->fi_lock);
3728 retstp = nfsd4_find_existing_open(fp, open);
3732 open->op_stp = NULL;
3733 atomic_inc(&stp->st_stid.sc_count);
3734 stp->st_stid.sc_type = NFS4_OPEN_STID;
3735 INIT_LIST_HEAD(&stp->st_locks);
3736 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
3738 stp->st_stid.sc_file = fp;
3739 stp->st_access_bmap = 0;
3740 stp->st_deny_bmap = 0;
3741 stp->st_openstp = NULL;
3742 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
3743 list_add(&stp->st_perfile, &fp->fi_stateids);
3746 spin_unlock(&fp->fi_lock);
3747 spin_unlock(&oo->oo_owner.so_client->cl_lock);
3749 /* Handle races with CLOSE */
3750 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
3751 nfs4_put_stid(&retstp->st_stid);
3754 /* To keep mutex tracking happy */
3755 mutex_unlock(&stp->st_mutex);
3762 * In the 4.0 case we need to keep the owners around a little while to handle
3763 * CLOSE replay. We still do need to release any file access that is held by
3764 * them before returning however.
3767 move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
3769 struct nfs4_ol_stateid *last;
3770 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3771 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3774 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
3777 * We know that we hold one reference via nfsd4_close, and another
3778 * "persistent" reference for the client. If the refcount is higher
3779 * than 2, then there are still calls in progress that are using this
3780 * stateid. We can't put the sc_file reference until they are finished.
3781 * Wait for the refcount to drop to 2. Since it has been unhashed,
3782 * there should be no danger of the refcount going back up again at
3785 wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3787 release_all_access(s);
3788 if (s->st_stid.sc_file) {
3789 put_nfs4_file(s->st_stid.sc_file);
3790 s->st_stid.sc_file = NULL;
3793 spin_lock(&nn->client_lock);
3794 last = oo->oo_last_closed_stid;
3795 oo->oo_last_closed_stid = s;
3796 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
3797 oo->oo_time = get_seconds();
3798 spin_unlock(&nn->client_lock);
3800 nfs4_put_stid(&last->st_stid);
3803 /* search file_hashtbl[] for file */
3804 static struct nfs4_file *
3805 find_file_locked(struct knfsd_fh *fh, unsigned int hashval)
3807 struct nfs4_file *fp;
3809 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash) {
3810 if (fh_match(&fp->fi_fhandle, fh)) {
3811 if (atomic_inc_not_zero(&fp->fi_ref))
3819 find_file(struct knfsd_fh *fh)
3821 struct nfs4_file *fp;
3822 unsigned int hashval = file_hashval(fh);
3825 fp = find_file_locked(fh, hashval);
3830 static struct nfs4_file *
3831 find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
3833 struct nfs4_file *fp;
3834 unsigned int hashval = file_hashval(fh);
3837 fp = find_file_locked(fh, hashval);
3842 spin_lock(&state_lock);
3843 fp = find_file_locked(fh, hashval);
3844 if (likely(fp == NULL)) {
3845 nfsd4_init_file(fh, hashval, new);
3848 spin_unlock(&state_lock);
3854 * Called to check deny when READ with all zero stateid or
3855 * WRITE with all zero or all one stateid
3858 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3860 struct nfs4_file *fp;
3861 __be32 ret = nfs_ok;
3863 fp = find_file(¤t_fh->fh_handle);
3866 /* Check for conflicting share reservations */
3867 spin_lock(&fp->fi_lock);
3868 if (fp->fi_share_deny & deny_type)
3869 ret = nfserr_locked;
3870 spin_unlock(&fp->fi_lock);
3875 static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
3877 struct nfs4_delegation *dp = cb_to_delegation(cb);
3878 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3881 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
3884 * We can't do this in nfsd_break_deleg_cb because it is
3885 * already holding inode->i_lock.
3887 * If the dl_time != 0, then we know that it has already been
3888 * queued for a lease break. Don't queue it again.
3890 spin_lock(&state_lock);
3891 if (delegation_hashed(dp) && dp->dl_time == 0) {
3892 dp->dl_time = get_seconds();
3893 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
3895 spin_unlock(&state_lock);
3898 static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
3899 struct rpc_task *task)
3901 struct nfs4_delegation *dp = cb_to_delegation(cb);
3903 if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID)
3906 switch (task->tk_status) {
3910 case -NFS4ERR_BAD_STATEID:
3912 * Race: client probably got cb_recall before open reply
3913 * granting delegation.
3915 if (dp->dl_retries--) {
3916 rpc_delay(task, 2 * HZ);
3925 static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
3927 struct nfs4_delegation *dp = cb_to_delegation(cb);
3929 nfs4_put_stid(&dp->dl_stid);
3932 static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
3933 .prepare = nfsd4_cb_recall_prepare,
3934 .done = nfsd4_cb_recall_done,
3935 .release = nfsd4_cb_recall_release,
3938 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3941 * We're assuming the state code never drops its reference
3942 * without first removing the lease. Since we're in this lease
3943 * callback (and since the lease code is serialized by the kernel
3944 * lock) we know the server hasn't removed the lease yet, we know
3945 * it's safe to take a reference.
3947 atomic_inc(&dp->dl_stid.sc_count);
3948 nfsd4_run_cb(&dp->dl_recall);
3951 /* Called from break_lease() with i_lock held. */
3953 nfsd_break_deleg_cb(struct file_lock *fl)
3956 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3957 struct nfs4_delegation *dp;
3960 WARN(1, "(%p)->fl_owner NULL\n", fl);
3963 if (fp->fi_had_conflict) {
3964 WARN(1, "duplicate break on %p\n", fp);
3968 * We don't want the locks code to timeout the lease for us;
3969 * we'll remove it ourself if a delegation isn't returned
3972 fl->fl_break_time = 0;
3974 spin_lock(&fp->fi_lock);
3975 fp->fi_had_conflict = true;
3977 * If there are no delegations on the list, then return true
3978 * so that the lease code will go ahead and delete it.
3980 if (list_empty(&fp->fi_delegations))
3983 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3984 nfsd_break_one_deleg(dp);
3985 spin_unlock(&fp->fi_lock);
3990 nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
3991 struct list_head *dispose)
3994 return lease_modify(onlist, arg, dispose);
3999 static const struct lock_manager_operations nfsd_lease_mng_ops = {
4000 .lm_break = nfsd_break_deleg_cb,
4001 .lm_change = nfsd_change_deleg_cb,
4004 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
4006 if (nfsd4_has_session(cstate))
4008 if (seqid == so->so_seqid - 1)
4009 return nfserr_replay_me;
4010 if (seqid == so->so_seqid)
4012 return nfserr_bad_seqid;
4015 static __be32 lookup_clientid(clientid_t *clid,
4016 struct nfsd4_compound_state *cstate,
4017 struct nfsd_net *nn)
4019 struct nfs4_client *found;
4022 found = cstate->clp;
4023 if (!same_clid(&found->cl_clientid, clid))
4024 return nfserr_stale_clientid;
4028 if (STALE_CLIENTID(clid, nn))
4029 return nfserr_stale_clientid;
4032 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
4033 * cached already then we know this is for is for v4.0 and "sessions"
4036 WARN_ON_ONCE(cstate->session);
4037 spin_lock(&nn->client_lock);
4038 found = find_confirmed_client(clid, false, nn);
4040 spin_unlock(&nn->client_lock);
4041 return nfserr_expired;
4043 atomic_inc(&found->cl_refcount);
4044 spin_unlock(&nn->client_lock);
4046 /* Cache the nfs4_client in cstate! */
4047 cstate->clp = found;
4052 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
4053 struct nfsd4_open *open, struct nfsd_net *nn)
4055 clientid_t *clientid = &open->op_clientid;
4056 struct nfs4_client *clp = NULL;
4057 unsigned int strhashval;
4058 struct nfs4_openowner *oo = NULL;
4061 if (STALE_CLIENTID(&open->op_clientid, nn))
4062 return nfserr_stale_clientid;
4064 * In case we need it later, after we've already created the
4065 * file and don't want to risk a further failure:
4067 open->op_file = nfsd4_alloc_file();
4068 if (open->op_file == NULL)
4069 return nfserr_jukebox;
4071 status = lookup_clientid(clientid, cstate, nn);
4076 strhashval = ownerstr_hashval(&open->op_owner);
4077 oo = find_openstateowner_str(strhashval, open, clp);
4078 open->op_openowner = oo;
4082 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4083 /* Replace unconfirmed owners without checking for replay. */
4084 release_openowner(oo);
4085 open->op_openowner = NULL;
4088 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
4093 oo = alloc_init_open_stateowner(strhashval, open, cstate);
4095 return nfserr_jukebox;
4096 open->op_openowner = oo;
4098 open->op_stp = nfs4_alloc_open_stateid(clp);
4100 return nfserr_jukebox;
4102 if (nfsd4_has_session(cstate) &&
4103 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
4104 open->op_odstate = alloc_clnt_odstate(clp);
4105 if (!open->op_odstate)
4106 return nfserr_jukebox;
4112 static inline __be32
4113 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
4115 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
4116 return nfserr_openmode;
4121 static int share_access_to_flags(u32 share_access)
4123 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
4126 static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
4128 struct nfs4_stid *ret;
4130 ret = find_stateid_by_type(cl, s,
4131 NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID);
4134 return delegstateid(ret);
4137 static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
4139 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
4140 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
4144 nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
4145 struct nfs4_delegation **dp)
4148 __be32 status = nfserr_bad_stateid;
4149 struct nfs4_delegation *deleg;
4151 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
4154 if (deleg->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) {
4155 nfs4_put_stid(&deleg->dl_stid);
4156 if (cl->cl_minorversion)
4157 status = nfserr_deleg_revoked;
4160 flags = share_access_to_flags(open->op_share_access);
4161 status = nfs4_check_delegmode(deleg, flags);
4163 nfs4_put_stid(&deleg->dl_stid);
4168 if (!nfsd4_is_deleg_cur(open))
4172 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
4176 static inline int nfs4_access_to_access(u32 nfs4_access)
4180 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
4181 flags |= NFSD_MAY_READ;
4182 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
4183 flags |= NFSD_MAY_WRITE;
4187 static inline __be32
4188 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
4189 struct nfsd4_open *open)
4191 struct iattr iattr = {
4192 .ia_valid = ATTR_SIZE,
4195 if (!open->op_truncate)
4197 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
4198 return nfserr_inval;
4199 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
4202 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
4203 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
4204 struct nfsd4_open *open)
4206 struct file *filp = NULL;
4208 int oflag = nfs4_access_to_omode(open->op_share_access);
4209 int access = nfs4_access_to_access(open->op_share_access);
4210 unsigned char old_access_bmap, old_deny_bmap;
4212 spin_lock(&fp->fi_lock);
4215 * Are we trying to set a deny mode that would conflict with
4218 status = nfs4_file_check_deny(fp, open->op_share_deny);
4219 if (status != nfs_ok) {
4220 spin_unlock(&fp->fi_lock);
4224 /* set access to the file */
4225 status = nfs4_file_get_access(fp, open->op_share_access);
4226 if (status != nfs_ok) {
4227 spin_unlock(&fp->fi_lock);
4231 /* Set access bits in stateid */
4232 old_access_bmap = stp->st_access_bmap;
4233 set_access(open->op_share_access, stp);
4235 /* Set new deny mask */
4236 old_deny_bmap = stp->st_deny_bmap;
4237 set_deny(open->op_share_deny, stp);
4238 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4240 if (!fp->fi_fds[oflag]) {
4241 spin_unlock(&fp->fi_lock);
4242 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
4244 goto out_put_access;
4245 spin_lock(&fp->fi_lock);
4246 if (!fp->fi_fds[oflag]) {
4247 fp->fi_fds[oflag] = filp;
4251 spin_unlock(&fp->fi_lock);
4255 status = nfsd4_truncate(rqstp, cur_fh, open);
4257 goto out_put_access;
4261 stp->st_access_bmap = old_access_bmap;
4262 nfs4_file_put_access(fp, open->op_share_access);
4263 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
4268 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)
4271 unsigned char old_deny_bmap = stp->st_deny_bmap;
4273 if (!test_access(open->op_share_access, stp))
4274 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
4276 /* test and set deny mode */
4277 spin_lock(&fp->fi_lock);
4278 status = nfs4_file_check_deny(fp, open->op_share_deny);
4279 if (status == nfs_ok) {
4280 set_deny(open->op_share_deny, stp);
4281 fp->fi_share_deny |=
4282 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4284 spin_unlock(&fp->fi_lock);
4286 if (status != nfs_ok)
4289 status = nfsd4_truncate(rqstp, cur_fh, open);
4290 if (status != nfs_ok)
4291 reset_union_bmap_deny(old_deny_bmap, stp);
4295 /* Should we give out recallable state?: */
4296 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
4298 if (clp->cl_cb_state == NFSD4_CB_UP)
4301 * In the sessions case, since we don't have to establish a
4302 * separate connection for callbacks, we assume it's OK
4303 * until we hear otherwise:
4305 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
4308 static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
4310 struct file_lock *fl;
4312 fl = locks_alloc_lock();
4315 fl->fl_lmops = &nfsd_lease_mng_ops;
4316 fl->fl_flags = FL_DELEG;
4317 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
4318 fl->fl_end = OFFSET_MAX;
4319 fl->fl_owner = (fl_owner_t)fp;
4320 fl->fl_pid = current->tgid;
4325 * nfs4_setlease - Obtain a delegation by requesting lease from vfs layer
4326 * @dp: a pointer to the nfs4_delegation we're adding.
4329 * On success: Return code will be 0 on success.
4331 * On error: -EAGAIN if there was an existing delegation.
4332 * nonzero if there is an error in other cases.
4336 static int nfs4_setlease(struct nfs4_delegation *dp)
4338 struct nfs4_file *fp = dp->dl_stid.sc_file;
4339 struct file_lock *fl;
4343 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
4346 filp = find_readable_file(fp);
4348 /* We should always have a readable file here */
4350 locks_free_lock(fl);
4354 status = vfs_setlease(filp, fl->fl_type, &fl, NULL);
4356 locks_free_lock(fl);
4359 spin_lock(&state_lock);
4360 spin_lock(&fp->fi_lock);
4361 /* Did the lease get broken before we took the lock? */
4363 if (fp->fi_had_conflict)
4366 if (fp->fi_deleg_file) {
4367 status = hash_delegation_locked(dp, fp);
4370 fp->fi_deleg_file = filp;
4371 fp->fi_delegees = 0;
4372 status = hash_delegation_locked(dp, fp);
4373 spin_unlock(&fp->fi_lock);
4374 spin_unlock(&state_lock);
4376 /* Should never happen, this is a new fi_deleg_file */
4382 spin_unlock(&fp->fi_lock);
4383 spin_unlock(&state_lock);
4389 static struct nfs4_delegation *
4390 nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
4391 struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate)
4394 struct nfs4_delegation *dp;
4396 if (fp->fi_had_conflict)
4397 return ERR_PTR(-EAGAIN);
4399 spin_lock(&state_lock);
4400 spin_lock(&fp->fi_lock);
4401 status = nfs4_get_existing_delegation(clp, fp);
4402 spin_unlock(&fp->fi_lock);
4403 spin_unlock(&state_lock);
4406 return ERR_PTR(status);
4408 dp = alloc_init_deleg(clp, fh, odstate);
4410 return ERR_PTR(-ENOMEM);
4413 spin_lock(&state_lock);
4414 spin_lock(&fp->fi_lock);
4415 dp->dl_stid.sc_file = fp;
4416 if (!fp->fi_deleg_file) {
4417 spin_unlock(&fp->fi_lock);
4418 spin_unlock(&state_lock);
4419 status = nfs4_setlease(dp);
4422 if (fp->fi_had_conflict) {
4426 status = hash_delegation_locked(dp, fp);
4428 spin_unlock(&fp->fi_lock);
4429 spin_unlock(&state_lock);
4432 put_clnt_odstate(dp->dl_clnt_odstate);
4433 nfs4_put_stid(&dp->dl_stid);
4434 return ERR_PTR(status);
4439 static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
4441 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4442 if (status == -EAGAIN)
4443 open->op_why_no_deleg = WND4_CONTENTION;
4445 open->op_why_no_deleg = WND4_RESOURCE;
4446 switch (open->op_deleg_want) {
4447 case NFS4_SHARE_WANT_READ_DELEG:
4448 case NFS4_SHARE_WANT_WRITE_DELEG:
4449 case NFS4_SHARE_WANT_ANY_DELEG:
4451 case NFS4_SHARE_WANT_CANCEL:
4452 open->op_why_no_deleg = WND4_CANCELLED;
4454 case NFS4_SHARE_WANT_NO_DELEG:
4461 * Attempt to hand out a delegation.
4463 * Note we don't support write delegations, and won't until the vfs has
4464 * proper support for them.
4467 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
4468 struct nfs4_ol_stateid *stp)
4470 struct nfs4_delegation *dp;
4471 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
4472 struct nfs4_client *clp = stp->st_stid.sc_client;
4476 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
4477 open->op_recall = 0;
4478 switch (open->op_claim_type) {
4479 case NFS4_OPEN_CLAIM_PREVIOUS:
4481 open->op_recall = 1;
4482 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
4485 case NFS4_OPEN_CLAIM_NULL:
4486 case NFS4_OPEN_CLAIM_FH:
4488 * Let's not give out any delegations till everyone's
4489 * had the chance to reclaim theirs, *and* until
4490 * NLM locks have all been reclaimed:
4492 if (locks_in_grace(clp->net))
4494 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
4497 * Also, if the file was opened for write or
4498 * create, there's a good chance the client's
4499 * about to write to it, resulting in an
4500 * immediate recall (since we don't support
4501 * write delegations):
4503 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
4505 if (open->op_create == NFS4_OPEN_CREATE)
4511 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate);
4515 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
4517 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
4518 STATEID_VAL(&dp->dl_stid.sc_stateid));
4519 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
4520 nfs4_put_stid(&dp->dl_stid);
4523 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
4524 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
4525 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
4526 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
4527 open->op_recall = 1;
4530 /* 4.1 client asking for a delegation? */
4531 if (open->op_deleg_want)
4532 nfsd4_open_deleg_none_ext(open, status);
4536 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
4537 struct nfs4_delegation *dp)
4539 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
4540 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4541 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4542 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
4543 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
4544 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4545 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4546 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
4548 /* Otherwise the client must be confused wanting a delegation
4549 * it already has, therefore we don't return
4550 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
4555 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
4557 struct nfsd4_compoundres *resp = rqstp->rq_resp;
4558 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
4559 struct nfs4_file *fp = NULL;
4560 struct nfs4_ol_stateid *stp = NULL;
4561 struct nfs4_delegation *dp = NULL;
4563 bool new_stp = false;
4566 * Lookup file; if found, lookup stateid and check open request,
4567 * and check for delegations in the process of being recalled.
4568 * If not found, create the nfs4_file struct
4570 fp = find_or_add_file(open->op_file, ¤t_fh->fh_handle);
4571 if (fp != open->op_file) {
4572 status = nfs4_check_deleg(cl, open, &dp);
4575 stp = nfsd4_find_and_lock_existing_open(fp, open);
4577 open->op_file = NULL;
4578 status = nfserr_bad_stateid;
4579 if (nfsd4_is_deleg_cur(open))
4584 stp = init_open_stateid(fp, open);
4590 * OPEN the file, or upgrade an existing OPEN.
4591 * If truncate fails, the OPEN fails.
4593 * stp is already locked.
4596 /* Stateid was found, this is an OPEN upgrade */
4597 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
4599 mutex_unlock(&stp->st_mutex);
4603 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4605 stp->st_stid.sc_type = NFS4_CLOSED_STID;
4606 release_open_stateid(stp);
4607 mutex_unlock(&stp->st_mutex);
4611 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
4613 if (stp->st_clnt_odstate == open->op_odstate)
4614 open->op_odstate = NULL;
4617 nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
4618 mutex_unlock(&stp->st_mutex);
4620 if (nfsd4_has_session(&resp->cstate)) {
4621 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4622 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4623 open->op_why_no_deleg = WND4_NOT_WANTED;
4629 * Attempt to hand out a delegation. No error return, because the
4630 * OPEN succeeds even if we fail.
4632 nfs4_open_delegation(current_fh, open, stp);
4636 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
4637 STATEID_VAL(&stp->st_stid.sc_stateid));
4639 /* 4.1 client trying to upgrade/downgrade delegation? */
4640 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
4641 open->op_deleg_want)
4642 nfsd4_deleg_xgrade_none_ext(open, dp);
4646 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
4647 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
4649 * To finish the open response, we just need to set the rflags.
4651 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
4652 if (nfsd4_has_session(&resp->cstate))
4653 open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK;
4654 else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED))
4655 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
4658 nfs4_put_stid(&dp->dl_stid);
4660 nfs4_put_stid(&stp->st_stid);
4665 void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
4666 struct nfsd4_open *open)
4668 if (open->op_openowner) {
4669 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
4671 nfsd4_cstate_assign_replay(cstate, so);
4672 nfs4_put_stateowner(so);
4675 kmem_cache_free(file_slab, open->op_file);
4677 nfs4_put_stid(&open->op_stp->st_stid);
4678 if (open->op_odstate)
4679 kmem_cache_free(odstate_slab, open->op_odstate);
4683 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4684 union nfsd4_op_u *u)
4686 clientid_t *clid = &u->renew;
4687 struct nfs4_client *clp;
4689 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4691 dprintk("process_renew(%08x/%08x): starting\n",
4692 clid->cl_boot, clid->cl_id);
4693 status = lookup_clientid(clid, cstate, nn);
4697 status = nfserr_cb_path_down;
4698 if (!list_empty(&clp->cl_delegations)
4699 && clp->cl_cb_state != NFSD4_CB_UP)
4707 nfsd4_end_grace(struct nfsd_net *nn)
4709 /* do nothing if grace period already ended */
4710 if (nn->grace_ended)
4713 dprintk("NFSD: end of grace period\n");
4714 nn->grace_ended = true;
4716 * If the server goes down again right now, an NFSv4
4717 * client will still be allowed to reclaim after it comes back up,
4718 * even if it hasn't yet had a chance to reclaim state this time.
4721 nfsd4_record_grace_done(nn);
4723 * At this point, NFSv4 clients can still reclaim. But if the
4724 * server crashes, any that have not yet reclaimed will be out
4725 * of luck on the next boot.
4727 * (NFSv4.1+ clients are considered to have reclaimed once they
4728 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
4729 * have reclaimed after their first OPEN.)
4731 locks_end_grace(&nn->nfsd4_manager);
4733 * At this point, and once lockd and/or any other containers
4734 * exit their grace period, further reclaims will fail and
4735 * regular locking can resume.
4740 nfs4_laundromat(struct nfsd_net *nn)
4742 struct nfs4_client *clp;
4743 struct nfs4_openowner *oo;
4744 struct nfs4_delegation *dp;
4745 struct nfs4_ol_stateid *stp;
4746 struct nfsd4_blocked_lock *nbl;
4747 struct list_head *pos, *next, reaplist;
4748 time_t cutoff = get_seconds() - nn->nfsd4_lease;
4749 time_t t, new_timeo = nn->nfsd4_lease;
4751 dprintk("NFSD: laundromat service - starting\n");
4752 nfsd4_end_grace(nn);
4753 INIT_LIST_HEAD(&reaplist);
4754 spin_lock(&nn->client_lock);
4755 list_for_each_safe(pos, next, &nn->client_lru) {
4756 clp = list_entry(pos, struct nfs4_client, cl_lru);
4757 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4758 t = clp->cl_time - cutoff;
4759 new_timeo = min(new_timeo, t);
4762 if (mark_client_expired_locked(clp)) {
4763 dprintk("NFSD: client in use (clientid %08x)\n",
4764 clp->cl_clientid.cl_id);
4767 list_add(&clp->cl_lru, &reaplist);
4769 spin_unlock(&nn->client_lock);
4770 list_for_each_safe(pos, next, &reaplist) {
4771 clp = list_entry(pos, struct nfs4_client, cl_lru);
4772 dprintk("NFSD: purging unused client (clientid %08x)\n",
4773 clp->cl_clientid.cl_id);
4774 list_del_init(&clp->cl_lru);
4777 spin_lock(&state_lock);
4778 list_for_each_safe(pos, next, &nn->del_recall_lru) {
4779 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4780 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
4781 t = dp->dl_time - cutoff;
4782 new_timeo = min(new_timeo, t);
4785 WARN_ON(!unhash_delegation_locked(dp));
4786 list_add(&dp->dl_recall_lru, &reaplist);
4788 spin_unlock(&state_lock);
4789 while (!list_empty(&reaplist)) {
4790 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4792 list_del_init(&dp->dl_recall_lru);
4793 revoke_delegation(dp);
4796 spin_lock(&nn->client_lock);
4797 while (!list_empty(&nn->close_lru)) {
4798 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
4800 if (time_after((unsigned long)oo->oo_time,
4801 (unsigned long)cutoff)) {
4802 t = oo->oo_time - cutoff;
4803 new_timeo = min(new_timeo, t);
4806 list_del_init(&oo->oo_close_lru);
4807 stp = oo->oo_last_closed_stid;
4808 oo->oo_last_closed_stid = NULL;
4809 spin_unlock(&nn->client_lock);
4810 nfs4_put_stid(&stp->st_stid);
4811 spin_lock(&nn->client_lock);
4813 spin_unlock(&nn->client_lock);
4816 * It's possible for a client to try and acquire an already held lock
4817 * that is being held for a long time, and then lose interest in it.
4818 * So, we clean out any un-revisited request after a lease period
4819 * under the assumption that the client is no longer interested.
4821 * RFC5661, sec. 9.6 states that the client must not rely on getting
4822 * notifications and must continue to poll for locks, even when the
4823 * server supports them. Thus this shouldn't lead to clients blocking
4824 * indefinitely once the lock does become free.
4826 BUG_ON(!list_empty(&reaplist));
4827 spin_lock(&nn->blocked_locks_lock);
4828 while (!list_empty(&nn->blocked_locks_lru)) {
4829 nbl = list_first_entry(&nn->blocked_locks_lru,
4830 struct nfsd4_blocked_lock, nbl_lru);
4831 if (time_after((unsigned long)nbl->nbl_time,
4832 (unsigned long)cutoff)) {
4833 t = nbl->nbl_time - cutoff;
4834 new_timeo = min(new_timeo, t);
4837 list_move(&nbl->nbl_lru, &reaplist);
4838 list_del_init(&nbl->nbl_list);
4840 spin_unlock(&nn->blocked_locks_lock);
4842 while (!list_empty(&reaplist)) {
4843 nbl = list_first_entry(&reaplist,
4844 struct nfsd4_blocked_lock, nbl_lru);
4845 list_del_init(&nbl->nbl_lru);
4846 posix_unblock_lock(&nbl->nbl_lock);
4847 free_blocked_lock(nbl);
4850 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
4854 static struct workqueue_struct *laundry_wq;
4855 static void laundromat_main(struct work_struct *);
4858 laundromat_main(struct work_struct *laundry)
4861 struct delayed_work *dwork = to_delayed_work(laundry);
4862 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4865 t = nfs4_laundromat(nn);
4866 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
4867 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
4870 static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
4872 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
4873 return nfserr_bad_stateid;
4878 access_permit_read(struct nfs4_ol_stateid *stp)
4880 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4881 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4882 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
4886 access_permit_write(struct nfs4_ol_stateid *stp)
4888 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4889 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
4893 __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
4895 __be32 status = nfserr_openmode;
4897 /* For lock stateid's, we test the parent open, not the lock: */
4898 if (stp->st_openstp)
4899 stp = stp->st_openstp;
4900 if ((flags & WR_STATE) && !access_permit_write(stp))
4902 if ((flags & RD_STATE) && !access_permit_read(stp))
4909 static inline __be32
4910 check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
4912 if (ONE_STATEID(stateid) && (flags & RD_STATE))
4914 else if (opens_in_grace(net)) {
4915 /* Answer in remaining cases depends on existence of
4916 * conflicting state; so we must wait out the grace period. */
4917 return nfserr_grace;
4918 } else if (flags & WR_STATE)
4919 return nfs4_share_conflict(current_fh,
4920 NFS4_SHARE_DENY_WRITE);
4921 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4922 return nfs4_share_conflict(current_fh,
4923 NFS4_SHARE_DENY_READ);
4927 * Allow READ/WRITE during grace period on recovered state only for files
4928 * that are not able to provide mandatory locking.
4931 grace_disallows_io(struct net *net, struct inode *inode)
4933 return opens_in_grace(net) && mandatory_lock(inode);
4936 static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
4939 * When sessions are used the stateid generation number is ignored
4942 if (has_session && in->si_generation == 0)
4945 if (in->si_generation == ref->si_generation)
4948 /* If the client sends us a stateid from the future, it's buggy: */
4949 if (nfsd4_stateid_generation_after(in, ref))
4950 return nfserr_bad_stateid;
4952 * However, we could see a stateid from the past, even from a
4953 * non-buggy client. For example, if the client sends a lock
4954 * while some IO is outstanding, the lock may bump si_generation
4955 * while the IO is still in flight. The client could avoid that
4956 * situation by waiting for responses on all the IO requests,
4957 * but better performance may result in retrying IO that
4958 * receives an old_stateid error if requests are rarely
4959 * reordered in flight:
4961 return nfserr_old_stateid;
4964 static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
4966 if (ols->st_stateowner->so_is_open_owner &&
4967 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
4968 return nfserr_bad_stateid;
4972 static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
4974 struct nfs4_stid *s;
4975 __be32 status = nfserr_bad_stateid;
4977 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
4978 CLOSE_STATEID(stateid))
4980 /* Client debugging aid. */
4981 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4982 char addr_str[INET6_ADDRSTRLEN];
4983 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4985 pr_warn_ratelimited("NFSD: client %s testing state ID "
4986 "with incorrect client ID\n", addr_str);
4989 spin_lock(&cl->cl_lock);
4990 s = find_stateid_locked(cl, stateid);
4993 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
4996 switch (s->sc_type) {
4997 case NFS4_DELEG_STID:
5000 case NFS4_REVOKED_DELEG_STID:
5001 status = nfserr_deleg_revoked;
5003 case NFS4_OPEN_STID:
5004 case NFS4_LOCK_STID:
5005 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
5008 printk("unknown stateid type %x\n", s->sc_type);
5010 case NFS4_CLOSED_STID:
5011 case NFS4_CLOSED_DELEG_STID:
5012 status = nfserr_bad_stateid;
5015 spin_unlock(&cl->cl_lock);
5020 nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
5021 stateid_t *stateid, unsigned char typemask,
5022 struct nfs4_stid **s, struct nfsd_net *nn)
5025 bool return_revoked = false;
5028 * only return revoked delegations if explicitly asked.
5029 * otherwise we report revoked or bad_stateid status.
5031 if (typemask & NFS4_REVOKED_DELEG_STID)
5032 return_revoked = true;
5033 else if (typemask & NFS4_DELEG_STID)
5034 typemask |= NFS4_REVOKED_DELEG_STID;
5036 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5037 CLOSE_STATEID(stateid))
5038 return nfserr_bad_stateid;
5039 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
5040 if (status == nfserr_stale_clientid) {
5041 if (cstate->session)
5042 return nfserr_bad_stateid;
5043 return nfserr_stale_stateid;
5047 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
5049 return nfserr_bad_stateid;
5050 if (((*s)->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) {
5052 if (cstate->minorversion)
5053 return nfserr_deleg_revoked;
5054 return nfserr_bad_stateid;
5059 static struct file *
5060 nfs4_find_file(struct nfs4_stid *s, int flags)
5065 switch (s->sc_type) {
5066 case NFS4_DELEG_STID:
5067 if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
5069 return get_file(s->sc_file->fi_deleg_file);
5070 case NFS4_OPEN_STID:
5071 case NFS4_LOCK_STID:
5072 if (flags & RD_STATE)
5073 return find_readable_file(s->sc_file);
5075 return find_writeable_file(s->sc_file);
5083 nfs4_check_olstateid(struct svc_fh *fhp, struct nfs4_ol_stateid *ols, int flags)
5087 status = nfsd4_check_openowner_confirmed(ols);
5090 return nfs4_check_openmode(ols, flags);
5094 nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
5095 struct file **filpp, bool *tmp_file, int flags)
5097 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
5101 file = nfs4_find_file(s, flags);
5103 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
5104 acc | NFSD_MAY_OWNER_OVERRIDE);
5112 status = nfsd_open(rqstp, fhp, S_IFREG, acc, filpp);
5124 * Checks for stateid operations
5127 nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
5128 struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
5129 stateid_t *stateid, int flags, struct file **filpp, bool *tmp_file)
5131 struct inode *ino = d_inode(fhp->fh_dentry);
5132 struct net *net = SVC_NET(rqstp);
5133 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5134 struct nfs4_stid *s = NULL;
5142 if (grace_disallows_io(net, ino))
5143 return nfserr_grace;
5145 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
5146 status = check_special_stateids(net, fhp, stateid, flags);
5150 status = nfsd4_lookup_stateid(cstate, stateid,
5151 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
5155 status = check_stateid_generation(stateid, &s->sc_stateid,
5156 nfsd4_has_session(cstate));
5160 switch (s->sc_type) {
5161 case NFS4_DELEG_STID:
5162 status = nfs4_check_delegmode(delegstateid(s), flags);
5164 case NFS4_OPEN_STID:
5165 case NFS4_LOCK_STID:
5166 status = nfs4_check_olstateid(fhp, openlockstateid(s), flags);
5169 status = nfserr_bad_stateid;
5174 status = nfs4_check_fh(fhp, s);
5177 if (!status && filpp)
5178 status = nfs4_check_file(rqstp, fhp, s, filpp, tmp_file, flags);
5186 * Test if the stateid is valid
5189 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5190 union nfsd4_op_u *u)
5192 struct nfsd4_test_stateid *test_stateid = &u->test_stateid;
5193 struct nfsd4_test_stateid_id *stateid;
5194 struct nfs4_client *cl = cstate->session->se_client;
5196 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
5197 stateid->ts_id_status =
5198 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
5204 nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
5206 struct nfs4_ol_stateid *stp = openlockstateid(s);
5209 mutex_lock(&stp->st_mutex);
5211 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5215 ret = nfserr_locks_held;
5216 if (check_for_locks(stp->st_stid.sc_file,
5217 lockowner(stp->st_stateowner)))
5220 release_lock_stateid(stp);
5224 mutex_unlock(&stp->st_mutex);
5230 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5231 union nfsd4_op_u *u)
5233 struct nfsd4_free_stateid *free_stateid = &u->free_stateid;
5234 stateid_t *stateid = &free_stateid->fr_stateid;
5235 struct nfs4_stid *s;
5236 struct nfs4_delegation *dp;
5237 struct nfs4_client *cl = cstate->session->se_client;
5238 __be32 ret = nfserr_bad_stateid;
5240 spin_lock(&cl->cl_lock);
5241 s = find_stateid_locked(cl, stateid);
5244 switch (s->sc_type) {
5245 case NFS4_DELEG_STID:
5246 ret = nfserr_locks_held;
5248 case NFS4_OPEN_STID:
5249 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5252 ret = nfserr_locks_held;
5254 case NFS4_LOCK_STID:
5255 atomic_inc(&s->sc_count);
5256 spin_unlock(&cl->cl_lock);
5257 ret = nfsd4_free_lock_stateid(stateid, s);
5259 case NFS4_REVOKED_DELEG_STID:
5260 dp = delegstateid(s);
5261 list_del_init(&dp->dl_recall_lru);
5262 spin_unlock(&cl->cl_lock);
5266 /* Default falls through and returns nfserr_bad_stateid */
5269 spin_unlock(&cl->cl_lock);
5277 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
5278 RD_STATE : WR_STATE;
5281 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
5283 struct svc_fh *current_fh = &cstate->current_fh;
5284 struct nfs4_stateowner *sop = stp->st_stateowner;
5287 status = nfsd4_check_seqid(cstate, sop, seqid);
5290 status = nfsd4_lock_ol_stateid(stp);
5291 if (status != nfs_ok)
5293 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
5294 if (status == nfs_ok)
5295 status = nfs4_check_fh(current_fh, &stp->st_stid);
5296 if (status != nfs_ok)
5297 mutex_unlock(&stp->st_mutex);
5302 * Checks for sequence id mutating operations.
5305 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
5306 stateid_t *stateid, char typemask,
5307 struct nfs4_ol_stateid **stpp,
5308 struct nfsd_net *nn)
5311 struct nfs4_stid *s;
5312 struct nfs4_ol_stateid *stp = NULL;
5314 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
5315 seqid, STATEID_VAL(stateid));
5318 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
5321 stp = openlockstateid(s);
5322 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
5324 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
5328 nfs4_put_stid(&stp->st_stid);
5332 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
5333 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
5336 struct nfs4_openowner *oo;
5337 struct nfs4_ol_stateid *stp;
5339 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
5340 NFS4_OPEN_STID, &stp, nn);
5343 oo = openowner(stp->st_stateowner);
5344 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
5345 mutex_unlock(&stp->st_mutex);
5346 nfs4_put_stid(&stp->st_stid);
5347 return nfserr_bad_stateid;
5354 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5355 union nfsd4_op_u *u)
5357 struct nfsd4_open_confirm *oc = &u->open_confirm;
5359 struct nfs4_openowner *oo;
5360 struct nfs4_ol_stateid *stp;
5361 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5363 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
5364 cstate->current_fh.fh_dentry);
5366 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
5370 status = nfs4_preprocess_seqid_op(cstate,
5371 oc->oc_seqid, &oc->oc_req_stateid,
5372 NFS4_OPEN_STID, &stp, nn);
5375 oo = openowner(stp->st_stateowner);
5376 status = nfserr_bad_stateid;
5377 if (oo->oo_flags & NFS4_OO_CONFIRMED) {
5378 mutex_unlock(&stp->st_mutex);
5381 oo->oo_flags |= NFS4_OO_CONFIRMED;
5382 nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
5383 mutex_unlock(&stp->st_mutex);
5384 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
5385 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
5387 nfsd4_client_record_create(oo->oo_owner.so_client);
5390 nfs4_put_stid(&stp->st_stid);
5392 nfsd4_bump_seqid(cstate, status);
5396 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
5398 if (!test_access(access, stp))
5400 nfs4_file_put_access(stp->st_stid.sc_file, access);
5401 clear_access(access, stp);
5404 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
5406 switch (to_access) {
5407 case NFS4_SHARE_ACCESS_READ:
5408 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
5409 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
5411 case NFS4_SHARE_ACCESS_WRITE:
5412 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
5413 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
5415 case NFS4_SHARE_ACCESS_BOTH:
5423 nfsd4_open_downgrade(struct svc_rqst *rqstp,
5424 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
5426 struct nfsd4_open_downgrade *od = &u->open_downgrade;
5428 struct nfs4_ol_stateid *stp;
5429 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5431 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
5432 cstate->current_fh.fh_dentry);
5434 /* We don't yet support WANT bits: */
5435 if (od->od_deleg_want)
5436 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
5439 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
5440 &od->od_stateid, &stp, nn);
5443 status = nfserr_inval;
5444 if (!test_access(od->od_share_access, stp)) {
5445 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
5446 stp->st_access_bmap, od->od_share_access);
5449 if (!test_deny(od->od_share_deny, stp)) {
5450 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
5451 stp->st_deny_bmap, od->od_share_deny);
5454 nfs4_stateid_downgrade(stp, od->od_share_access);
5455 reset_union_bmap_deny(od->od_share_deny, stp);
5456 nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
5459 mutex_unlock(&stp->st_mutex);
5460 nfs4_put_stid(&stp->st_stid);
5462 nfsd4_bump_seqid(cstate, status);
5466 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
5468 struct nfs4_client *clp = s->st_stid.sc_client;
5470 LIST_HEAD(reaplist);
5472 spin_lock(&clp->cl_lock);
5473 unhashed = unhash_open_stateid(s, &reaplist);
5475 if (clp->cl_minorversion) {
5477 put_ol_stateid_locked(s, &reaplist);
5478 spin_unlock(&clp->cl_lock);
5479 free_ol_stateid_reaplist(&reaplist);
5481 spin_unlock(&clp->cl_lock);
5482 free_ol_stateid_reaplist(&reaplist);
5484 move_to_close_lru(s, clp->net);
5489 * nfs4_unlock_state() called after encode
5492 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5493 union nfsd4_op_u *u)
5495 struct nfsd4_close *close = &u->close;
5497 struct nfs4_ol_stateid *stp;
5498 struct net *net = SVC_NET(rqstp);
5499 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5501 dprintk("NFSD: nfsd4_close on file %pd\n",
5502 cstate->current_fh.fh_dentry);
5504 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
5506 NFS4_OPEN_STID|NFS4_CLOSED_STID,
5508 nfsd4_bump_seqid(cstate, status);
5512 stp->st_stid.sc_type = NFS4_CLOSED_STID;
5513 nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
5515 nfsd4_close_open_stateid(stp);
5516 mutex_unlock(&stp->st_mutex);
5518 /* See RFC5661 sectionm 18.2.4 */
5519 if (stp->st_stid.sc_client->cl_minorversion)
5520 memcpy(&close->cl_stateid, &close_stateid,
5521 sizeof(close->cl_stateid));
5523 /* put reference from nfs4_preprocess_seqid_op */
5524 nfs4_put_stid(&stp->st_stid);
5530 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5531 union nfsd4_op_u *u)
5533 struct nfsd4_delegreturn *dr = &u->delegreturn;
5534 struct nfs4_delegation *dp;
5535 stateid_t *stateid = &dr->dr_stateid;
5536 struct nfs4_stid *s;
5538 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5540 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
5543 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
5546 dp = delegstateid(s);
5547 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
5551 destroy_delegation(dp);
5553 nfs4_put_stid(&dp->dl_stid);
5559 end_offset(u64 start, u64 len)
5564 return end >= start ? end: NFS4_MAX_UINT64;
5567 /* last octet in a range */
5569 last_byte_offset(u64 start, u64 len)
5575 return end > start ? end - 1: NFS4_MAX_UINT64;
5579 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
5580 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
5581 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
5582 * locking, this prevents us from being completely protocol-compliant. The
5583 * real solution to this problem is to start using unsigned file offsets in
5584 * the VFS, but this is a very deep change!
5587 nfs4_transform_lock_offset(struct file_lock *lock)
5589 if (lock->fl_start < 0)
5590 lock->fl_start = OFFSET_MAX;
5591 if (lock->fl_end < 0)
5592 lock->fl_end = OFFSET_MAX;
5596 nfsd4_fl_get_owner(fl_owner_t owner)
5598 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
5600 nfs4_get_stateowner(&lo->lo_owner);
5605 nfsd4_fl_put_owner(fl_owner_t owner)
5607 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
5610 nfs4_put_stateowner(&lo->lo_owner);
5614 nfsd4_lm_notify(struct file_lock *fl)
5616 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
5617 struct net *net = lo->lo_owner.so_client->net;
5618 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5619 struct nfsd4_blocked_lock *nbl = container_of(fl,
5620 struct nfsd4_blocked_lock, nbl_lock);
5623 /* An empty list means that something else is going to be using it */
5624 spin_lock(&nn->blocked_locks_lock);
5625 if (!list_empty(&nbl->nbl_list)) {
5626 list_del_init(&nbl->nbl_list);
5627 list_del_init(&nbl->nbl_lru);
5630 spin_unlock(&nn->blocked_locks_lock);
5633 nfsd4_run_cb(&nbl->nbl_cb);
5636 static const struct lock_manager_operations nfsd_posix_mng_ops = {
5637 .lm_notify = nfsd4_lm_notify,
5638 .lm_get_owner = nfsd4_fl_get_owner,
5639 .lm_put_owner = nfsd4_fl_put_owner,
5643 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
5645 struct nfs4_lockowner *lo;
5647 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
5648 lo = (struct nfs4_lockowner *) fl->fl_owner;
5649 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
5650 lo->lo_owner.so_owner.len, GFP_KERNEL);
5651 if (!deny->ld_owner.data)
5652 /* We just don't care that much */
5654 deny->ld_owner.len = lo->lo_owner.so_owner.len;
5655 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
5658 deny->ld_owner.len = 0;
5659 deny->ld_owner.data = NULL;
5660 deny->ld_clientid.cl_boot = 0;
5661 deny->ld_clientid.cl_id = 0;
5663 deny->ld_start = fl->fl_start;
5664 deny->ld_length = NFS4_MAX_UINT64;
5665 if (fl->fl_end != NFS4_MAX_UINT64)
5666 deny->ld_length = fl->fl_end - fl->fl_start + 1;
5667 deny->ld_type = NFS4_READ_LT;
5668 if (fl->fl_type != F_RDLCK)
5669 deny->ld_type = NFS4_WRITE_LT;
5672 static struct nfs4_lockowner *
5673 find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
5675 unsigned int strhashval = ownerstr_hashval(owner);
5676 struct nfs4_stateowner *so;
5678 lockdep_assert_held(&clp->cl_lock);
5680 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
5682 if (so->so_is_open_owner)
5684 if (same_owner_str(so, owner))
5685 return lockowner(nfs4_get_stateowner(so));
5690 static struct nfs4_lockowner *
5691 find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
5693 struct nfs4_lockowner *lo;
5695 spin_lock(&clp->cl_lock);
5696 lo = find_lockowner_str_locked(clp, owner);
5697 spin_unlock(&clp->cl_lock);
5701 static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
5703 unhash_lockowner_locked(lockowner(sop));
5706 static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
5708 struct nfs4_lockowner *lo = lockowner(sop);
5710 kmem_cache_free(lockowner_slab, lo);
5713 static const struct nfs4_stateowner_operations lockowner_ops = {
5714 .so_unhash = nfs4_unhash_lockowner,
5715 .so_free = nfs4_free_lockowner,
5719 * Alloc a lock owner structure.
5720 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
5723 * strhashval = ownerstr_hashval
5725 static struct nfs4_lockowner *
5726 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
5727 struct nfs4_ol_stateid *open_stp,
5728 struct nfsd4_lock *lock)
5730 struct nfs4_lockowner *lo, *ret;
5732 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
5735 INIT_LIST_HEAD(&lo->lo_blocked);
5736 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
5737 lo->lo_owner.so_is_open_owner = 0;
5738 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
5739 lo->lo_owner.so_ops = &lockowner_ops;
5740 spin_lock(&clp->cl_lock);
5741 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
5743 list_add(&lo->lo_owner.so_strhash,
5744 &clp->cl_ownerstr_hashtbl[strhashval]);
5747 nfs4_free_stateowner(&lo->lo_owner);
5749 spin_unlock(&clp->cl_lock);
5754 init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
5755 struct nfs4_file *fp, struct inode *inode,
5756 struct nfs4_ol_stateid *open_stp)
5758 struct nfs4_client *clp = lo->lo_owner.so_client;
5760 lockdep_assert_held(&clp->cl_lock);
5762 atomic_inc(&stp->st_stid.sc_count);
5763 stp->st_stid.sc_type = NFS4_LOCK_STID;
5764 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
5766 stp->st_stid.sc_file = fp;
5767 stp->st_access_bmap = 0;
5768 stp->st_deny_bmap = open_stp->st_deny_bmap;
5769 stp->st_openstp = open_stp;
5770 mutex_init(&stp->st_mutex);
5771 list_add(&stp->st_locks, &open_stp->st_locks);
5772 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
5773 spin_lock(&fp->fi_lock);
5774 list_add(&stp->st_perfile, &fp->fi_stateids);
5775 spin_unlock(&fp->fi_lock);
5778 static struct nfs4_ol_stateid *
5779 find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5781 struct nfs4_ol_stateid *lst;
5782 struct nfs4_client *clp = lo->lo_owner.so_client;
5784 lockdep_assert_held(&clp->cl_lock);
5786 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
5787 if (lst->st_stid.sc_file == fp) {
5788 atomic_inc(&lst->st_stid.sc_count);
5795 static struct nfs4_ol_stateid *
5796 find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5797 struct inode *inode, struct nfs4_ol_stateid *ost,
5800 struct nfs4_stid *ns = NULL;
5801 struct nfs4_ol_stateid *lst;
5802 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5803 struct nfs4_client *clp = oo->oo_owner.so_client;
5805 spin_lock(&clp->cl_lock);
5806 lst = find_lock_stateid(lo, fi);
5808 spin_unlock(&clp->cl_lock);
5809 ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid);
5813 spin_lock(&clp->cl_lock);
5814 lst = find_lock_stateid(lo, fi);
5816 lst = openlockstateid(ns);
5817 init_lock_stateid(lst, lo, fi, inode, ost);
5822 spin_unlock(&clp->cl_lock);
5829 check_lock_length(u64 offset, u64 length)
5831 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
5832 (length > ~offset)));
5835 static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
5837 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
5839 lockdep_assert_held(&fp->fi_lock);
5841 if (test_access(access, lock_stp))
5843 __nfs4_file_get_access(fp, access);
5844 set_access(access, lock_stp);
5848 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5849 struct nfs4_ol_stateid *ost,
5850 struct nfsd4_lock *lock,
5851 struct nfs4_ol_stateid **plst, bool *new)
5854 struct nfs4_file *fi = ost->st_stid.sc_file;
5855 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5856 struct nfs4_client *cl = oo->oo_owner.so_client;
5857 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
5858 struct nfs4_lockowner *lo;
5859 struct nfs4_ol_stateid *lst;
5860 unsigned int strhashval;
5863 lo = find_lockowner_str(cl, &lock->lk_new_owner);
5865 strhashval = ownerstr_hashval(&lock->lk_new_owner);
5866 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5868 return nfserr_jukebox;
5870 /* with an existing lockowner, seqids must be the same */
5871 status = nfserr_bad_seqid;
5872 if (!cstate->minorversion &&
5873 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
5878 lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
5880 status = nfserr_jukebox;
5884 mutex_lock(&lst->st_mutex);
5886 /* See if it's still hashed to avoid race with FREE_STATEID */
5887 spin_lock(&cl->cl_lock);
5888 hashed = !list_empty(&lst->st_perfile);
5889 spin_unlock(&cl->cl_lock);
5892 mutex_unlock(&lst->st_mutex);
5893 nfs4_put_stid(&lst->st_stid);
5899 nfs4_put_stateowner(&lo->lo_owner);
5907 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5908 union nfsd4_op_u *u)
5910 struct nfsd4_lock *lock = &u->lock;
5911 struct nfs4_openowner *open_sop = NULL;
5912 struct nfs4_lockowner *lock_sop = NULL;
5913 struct nfs4_ol_stateid *lock_stp = NULL;
5914 struct nfs4_ol_stateid *open_stp = NULL;
5915 struct nfs4_file *fp;
5916 struct file *filp = NULL;
5917 struct nfsd4_blocked_lock *nbl = NULL;
5918 struct file_lock *file_lock = NULL;
5919 struct file_lock *conflock = NULL;
5924 unsigned char fl_type;
5925 unsigned int fl_flags = FL_POSIX;
5926 struct net *net = SVC_NET(rqstp);
5927 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5929 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5930 (long long) lock->lk_offset,
5931 (long long) lock->lk_length);
5933 if (check_lock_length(lock->lk_offset, lock->lk_length))
5934 return nfserr_inval;
5936 if ((status = fh_verify(rqstp, &cstate->current_fh,
5937 S_IFREG, NFSD_MAY_LOCK))) {
5938 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5942 if (lock->lk_is_new) {
5943 if (nfsd4_has_session(cstate))
5944 /* See rfc 5661 18.10.3: given clientid is ignored: */
5945 memcpy(&lock->lk_new_clientid,
5946 &cstate->session->se_client->cl_clientid,
5947 sizeof(clientid_t));
5949 status = nfserr_stale_clientid;
5950 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
5953 /* validate and update open stateid and open seqid */
5954 status = nfs4_preprocess_confirmed_seqid_op(cstate,
5955 lock->lk_new_open_seqid,
5956 &lock->lk_new_open_stateid,
5960 mutex_unlock(&open_stp->st_mutex);
5961 open_sop = openowner(open_stp->st_stateowner);
5962 status = nfserr_bad_stateid;
5963 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
5964 &lock->lk_new_clientid))
5966 status = lookup_or_create_lock_state(cstate, open_stp, lock,
5969 status = nfs4_preprocess_seqid_op(cstate,
5970 lock->lk_old_lock_seqid,
5971 &lock->lk_old_lock_stateid,
5972 NFS4_LOCK_STID, &lock_stp, nn);
5976 lock_sop = lockowner(lock_stp->st_stateowner);
5978 lkflg = setlkflg(lock->lk_type);
5979 status = nfs4_check_openmode(lock_stp, lkflg);
5983 status = nfserr_grace;
5984 if (locks_in_grace(net) && !lock->lk_reclaim)
5986 status = nfserr_no_grace;
5987 if (!locks_in_grace(net) && lock->lk_reclaim)
5990 fp = lock_stp->st_stid.sc_file;
5991 switch (lock->lk_type) {
5993 if (nfsd4_has_session(cstate))
5994 fl_flags |= FL_SLEEP;
5997 spin_lock(&fp->fi_lock);
5998 filp = find_readable_file_locked(fp);
6000 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
6001 spin_unlock(&fp->fi_lock);
6004 case NFS4_WRITEW_LT:
6005 if (nfsd4_has_session(cstate))
6006 fl_flags |= FL_SLEEP;
6009 spin_lock(&fp->fi_lock);
6010 filp = find_writeable_file_locked(fp);
6012 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
6013 spin_unlock(&fp->fi_lock);
6017 status = nfserr_inval;
6022 status = nfserr_openmode;
6026 nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
6028 dprintk("NFSD: %s: unable to allocate block!\n", __func__);
6029 status = nfserr_jukebox;
6033 file_lock = &nbl->nbl_lock;
6034 file_lock->fl_type = fl_type;
6035 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
6036 file_lock->fl_pid = current->tgid;
6037 file_lock->fl_file = filp;
6038 file_lock->fl_flags = fl_flags;
6039 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6040 file_lock->fl_start = lock->lk_offset;
6041 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
6042 nfs4_transform_lock_offset(file_lock);
6044 conflock = locks_alloc_lock();
6046 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6047 status = nfserr_jukebox;
6051 if (fl_flags & FL_SLEEP) {
6052 nbl->nbl_time = get_seconds();
6053 spin_lock(&nn->blocked_locks_lock);
6054 list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
6055 list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
6056 spin_unlock(&nn->blocked_locks_lock);
6059 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
6061 case 0: /* success! */
6062 nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
6065 case FILE_LOCK_DEFERRED:
6068 case -EAGAIN: /* conflock holds conflicting lock */
6069 status = nfserr_denied;
6070 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
6071 nfs4_set_lock_denied(conflock, &lock->lk_denied);
6074 status = nfserr_deadlock;
6077 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
6078 status = nfserrno(err);
6083 /* dequeue it if we queued it before */
6084 if (fl_flags & FL_SLEEP) {
6085 spin_lock(&nn->blocked_locks_lock);
6086 list_del_init(&nbl->nbl_list);
6087 list_del_init(&nbl->nbl_lru);
6088 spin_unlock(&nn->blocked_locks_lock);
6090 free_blocked_lock(nbl);
6095 /* Bump seqid manually if the 4.0 replay owner is openowner */
6096 if (cstate->replay_owner &&
6097 cstate->replay_owner != &lock_sop->lo_owner &&
6098 seqid_mutating_err(ntohl(status)))
6099 lock_sop->lo_owner.so_seqid++;
6101 mutex_unlock(&lock_stp->st_mutex);
6104 * If this is a new, never-before-used stateid, and we are
6105 * returning an error, then just go ahead and release it.
6108 release_lock_stateid(lock_stp);
6110 nfs4_put_stid(&lock_stp->st_stid);
6113 nfs4_put_stid(&open_stp->st_stid);
6114 nfsd4_bump_seqid(cstate, status);
6116 locks_free_lock(conflock);
6121 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
6122 * so we do a temporary open here just to get an open file to pass to
6123 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
6126 static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
6129 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
6131 err = nfserrno(vfs_test_lock(file, lock));
6141 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6142 union nfsd4_op_u *u)
6144 struct nfsd4_lockt *lockt = &u->lockt;
6145 struct file_lock *file_lock = NULL;
6146 struct nfs4_lockowner *lo = NULL;
6148 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6150 if (locks_in_grace(SVC_NET(rqstp)))
6151 return nfserr_grace;
6153 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
6154 return nfserr_inval;
6156 if (!nfsd4_has_session(cstate)) {
6157 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
6162 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
6165 file_lock = locks_alloc_lock();
6167 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6168 status = nfserr_jukebox;
6172 switch (lockt->lt_type) {
6175 file_lock->fl_type = F_RDLCK;
6178 case NFS4_WRITEW_LT:
6179 file_lock->fl_type = F_WRLCK;
6182 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
6183 status = nfserr_inval;
6187 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
6189 file_lock->fl_owner = (fl_owner_t)lo;
6190 file_lock->fl_pid = current->tgid;
6191 file_lock->fl_flags = FL_POSIX;
6193 file_lock->fl_start = lockt->lt_offset;
6194 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
6196 nfs4_transform_lock_offset(file_lock);
6198 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
6202 if (file_lock->fl_type != F_UNLCK) {
6203 status = nfserr_denied;
6204 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
6208 nfs4_put_stateowner(&lo->lo_owner);
6210 locks_free_lock(file_lock);
6215 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6216 union nfsd4_op_u *u)
6218 struct nfsd4_locku *locku = &u->locku;
6219 struct nfs4_ol_stateid *stp;
6220 struct file *filp = NULL;
6221 struct file_lock *file_lock = NULL;
6224 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6226 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
6227 (long long) locku->lu_offset,
6228 (long long) locku->lu_length);
6230 if (check_lock_length(locku->lu_offset, locku->lu_length))
6231 return nfserr_inval;
6233 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
6234 &locku->lu_stateid, NFS4_LOCK_STID,
6238 filp = find_any_file(stp->st_stid.sc_file);
6240 status = nfserr_lock_range;
6243 file_lock = locks_alloc_lock();
6245 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6246 status = nfserr_jukebox;
6250 file_lock->fl_type = F_UNLCK;
6251 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
6252 file_lock->fl_pid = current->tgid;
6253 file_lock->fl_file = filp;
6254 file_lock->fl_flags = FL_POSIX;
6255 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6256 file_lock->fl_start = locku->lu_offset;
6258 file_lock->fl_end = last_byte_offset(locku->lu_offset,
6260 nfs4_transform_lock_offset(file_lock);
6262 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
6264 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
6267 nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
6271 mutex_unlock(&stp->st_mutex);
6272 nfs4_put_stid(&stp->st_stid);
6274 nfsd4_bump_seqid(cstate, status);
6276 locks_free_lock(file_lock);
6280 status = nfserrno(err);
6286 * true: locks held by lockowner
6287 * false: no locks held by lockowner
6290 check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
6292 struct file_lock *fl;
6294 struct file *filp = find_any_file(fp);
6295 struct inode *inode;
6296 struct file_lock_context *flctx;
6299 /* Any valid lock stateid should have some sort of access */
6304 inode = file_inode(filp);
6305 flctx = inode->i_flctx;
6307 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
6308 spin_lock(&flctx->flc_lock);
6309 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
6310 if (fl->fl_owner == (fl_owner_t)lowner) {
6315 spin_unlock(&flctx->flc_lock);
6322 nfsd4_release_lockowner(struct svc_rqst *rqstp,
6323 struct nfsd4_compound_state *cstate,
6324 union nfsd4_op_u *u)
6326 struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner;
6327 clientid_t *clid = &rlockowner->rl_clientid;
6328 struct nfs4_stateowner *sop;
6329 struct nfs4_lockowner *lo = NULL;
6330 struct nfs4_ol_stateid *stp;
6331 struct xdr_netobj *owner = &rlockowner->rl_owner;
6332 unsigned int hashval = ownerstr_hashval(owner);
6334 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6335 struct nfs4_client *clp;
6336 LIST_HEAD (reaplist);
6338 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
6339 clid->cl_boot, clid->cl_id);
6341 status = lookup_clientid(clid, cstate, nn);
6346 /* Find the matching lock stateowner */
6347 spin_lock(&clp->cl_lock);
6348 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
6351 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
6354 if (atomic_read(&sop->so_count) != 1) {
6355 spin_unlock(&clp->cl_lock);
6356 return nfserr_locks_held;
6359 lo = lockowner(sop);
6360 nfs4_get_stateowner(sop);
6364 spin_unlock(&clp->cl_lock);
6368 unhash_lockowner_locked(lo);
6369 while (!list_empty(&lo->lo_owner.so_stateids)) {
6370 stp = list_first_entry(&lo->lo_owner.so_stateids,
6371 struct nfs4_ol_stateid,
6373 WARN_ON(!unhash_lock_stateid(stp));
6374 put_ol_stateid_locked(stp, &reaplist);
6376 spin_unlock(&clp->cl_lock);
6377 free_ol_stateid_reaplist(&reaplist);
6378 remove_blocked_locks(lo);
6379 nfs4_put_stateowner(&lo->lo_owner);
6384 static inline struct nfs4_client_reclaim *
6387 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
6391 nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
6393 struct nfs4_client_reclaim *crp;
6395 crp = nfsd4_find_reclaim_client(name, nn);
6396 return (crp && crp->cr_clp);
6400 * failure => all reset bets are off, nfserr_no_grace...
6402 struct nfs4_client_reclaim *
6403 nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
6405 unsigned int strhashval;
6406 struct nfs4_client_reclaim *crp;
6408 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
6409 crp = alloc_reclaim();
6411 strhashval = clientstr_hashval(name);
6412 INIT_LIST_HEAD(&crp->cr_strhash);
6413 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
6414 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
6416 nn->reclaim_str_hashtbl_size++;
6422 nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
6424 list_del(&crp->cr_strhash);
6426 nn->reclaim_str_hashtbl_size--;
6430 nfs4_release_reclaim(struct nfsd_net *nn)
6432 struct nfs4_client_reclaim *crp = NULL;
6435 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6436 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
6437 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
6438 struct nfs4_client_reclaim, cr_strhash);
6439 nfs4_remove_reclaim_record(crp, nn);
6442 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
6446 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
6447 struct nfs4_client_reclaim *
6448 nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
6450 unsigned int strhashval;
6451 struct nfs4_client_reclaim *crp = NULL;
6453 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
6455 strhashval = clientstr_hashval(recdir);
6456 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
6457 if (same_name(crp->cr_recdir, recdir)) {
6465 * Called from OPEN. Look for clientid in reclaim list.
6468 nfs4_check_open_reclaim(clientid_t *clid,
6469 struct nfsd4_compound_state *cstate,
6470 struct nfsd_net *nn)
6474 /* find clientid in conf_id_hashtbl */
6475 status = lookup_clientid(clid, cstate, nn);
6477 return nfserr_reclaim_bad;
6479 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
6480 return nfserr_no_grace;
6482 if (nfsd4_client_record_check(cstate->clp))
6483 return nfserr_reclaim_bad;
6488 #ifdef CONFIG_NFSD_FAULT_INJECTION
6490 put_client(struct nfs4_client *clp)
6492 atomic_dec(&clp->cl_refcount);
6495 static struct nfs4_client *
6496 nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
6498 struct nfs4_client *clp;
6499 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6502 if (!nfsd_netns_ready(nn))
6505 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6506 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
6513 nfsd_inject_print_clients(void)
6515 struct nfs4_client *clp;
6517 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6519 char buf[INET6_ADDRSTRLEN];
6521 if (!nfsd_netns_ready(nn))
6524 spin_lock(&nn->client_lock);
6525 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6526 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
6527 pr_info("NFS Client: %s\n", buf);
6530 spin_unlock(&nn->client_lock);
6536 nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
6539 struct nfs4_client *clp;
6540 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6543 if (!nfsd_netns_ready(nn))
6546 spin_lock(&nn->client_lock);
6547 clp = nfsd_find_client(addr, addr_size);
6549 if (mark_client_expired_locked(clp) == nfs_ok)
6554 spin_unlock(&nn->client_lock);
6563 nfsd_inject_forget_clients(u64 max)
6566 struct nfs4_client *clp, *next;
6567 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6569 LIST_HEAD(reaplist);
6571 if (!nfsd_netns_ready(nn))
6574 spin_lock(&nn->client_lock);
6575 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6576 if (mark_client_expired_locked(clp) == nfs_ok) {
6577 list_add(&clp->cl_lru, &reaplist);
6578 if (max != 0 && ++count >= max)
6582 spin_unlock(&nn->client_lock);
6584 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
6590 static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
6593 char buf[INET6_ADDRSTRLEN];
6594 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
6595 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
6599 nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
6600 struct list_head *collect)
6602 struct nfs4_client *clp = lst->st_stid.sc_client;
6603 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6609 lockdep_assert_held(&nn->client_lock);
6610 atomic_inc(&clp->cl_refcount);
6611 list_add(&lst->st_locks, collect);
6614 static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
6615 struct list_head *collect,
6616 bool (*func)(struct nfs4_ol_stateid *))
6618 struct nfs4_openowner *oop;
6619 struct nfs4_ol_stateid *stp, *st_next;
6620 struct nfs4_ol_stateid *lst, *lst_next;
6623 spin_lock(&clp->cl_lock);
6624 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
6625 list_for_each_entry_safe(stp, st_next,
6626 &oop->oo_owner.so_stateids, st_perstateowner) {
6627 list_for_each_entry_safe(lst, lst_next,
6628 &stp->st_locks, st_locks) {
6631 nfsd_inject_add_lock_to_list(lst,
6636 * Despite the fact that these functions deal
6637 * with 64-bit integers for "count", we must
6638 * ensure that it doesn't blow up the
6639 * clp->cl_refcount. Throw a warning if we
6640 * start to approach INT_MAX here.
6642 WARN_ON_ONCE(count == (INT_MAX / 2));
6649 spin_unlock(&clp->cl_lock);
6655 nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
6658 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
6662 nfsd_print_client_locks(struct nfs4_client *clp)
6664 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
6665 nfsd_print_count(clp, count, "locked files");
6670 nfsd_inject_print_locks(void)
6672 struct nfs4_client *clp;
6674 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6677 if (!nfsd_netns_ready(nn))
6680 spin_lock(&nn->client_lock);
6681 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6682 count += nfsd_print_client_locks(clp);
6683 spin_unlock(&nn->client_lock);
6689 nfsd_reap_locks(struct list_head *reaplist)
6691 struct nfs4_client *clp;
6692 struct nfs4_ol_stateid *stp, *next;
6694 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
6695 list_del_init(&stp->st_locks);
6696 clp = stp->st_stid.sc_client;
6697 nfs4_put_stid(&stp->st_stid);
6703 nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
6705 unsigned int count = 0;
6706 struct nfs4_client *clp;
6707 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6709 LIST_HEAD(reaplist);
6711 if (!nfsd_netns_ready(nn))
6714 spin_lock(&nn->client_lock);
6715 clp = nfsd_find_client(addr, addr_size);
6717 count = nfsd_collect_client_locks(clp, &reaplist, 0);
6718 spin_unlock(&nn->client_lock);
6719 nfsd_reap_locks(&reaplist);
6724 nfsd_inject_forget_locks(u64 max)
6727 struct nfs4_client *clp;
6728 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6730 LIST_HEAD(reaplist);
6732 if (!nfsd_netns_ready(nn))
6735 spin_lock(&nn->client_lock);
6736 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6737 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
6738 if (max != 0 && count >= max)
6741 spin_unlock(&nn->client_lock);
6742 nfsd_reap_locks(&reaplist);
6747 nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
6748 struct list_head *collect,
6749 void (*func)(struct nfs4_openowner *))
6751 struct nfs4_openowner *oop, *next;
6752 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6756 lockdep_assert_held(&nn->client_lock);
6758 spin_lock(&clp->cl_lock);
6759 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
6763 atomic_inc(&clp->cl_refcount);
6764 list_add(&oop->oo_perclient, collect);
6769 * Despite the fact that these functions deal with
6770 * 64-bit integers for "count", we must ensure that
6771 * it doesn't blow up the clp->cl_refcount. Throw a
6772 * warning if we start to approach INT_MAX here.
6774 WARN_ON_ONCE(count == (INT_MAX / 2));
6778 spin_unlock(&clp->cl_lock);
6784 nfsd_print_client_openowners(struct nfs4_client *clp)
6786 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
6788 nfsd_print_count(clp, count, "openowners");
6793 nfsd_collect_client_openowners(struct nfs4_client *clp,
6794 struct list_head *collect, u64 max)
6796 return nfsd_foreach_client_openowner(clp, max, collect,
6797 unhash_openowner_locked);
6801 nfsd_inject_print_openowners(void)
6803 struct nfs4_client *clp;
6805 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6808 if (!nfsd_netns_ready(nn))
6811 spin_lock(&nn->client_lock);
6812 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6813 count += nfsd_print_client_openowners(clp);
6814 spin_unlock(&nn->client_lock);
6820 nfsd_reap_openowners(struct list_head *reaplist)
6822 struct nfs4_client *clp;
6823 struct nfs4_openowner *oop, *next;
6825 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
6826 list_del_init(&oop->oo_perclient);
6827 clp = oop->oo_owner.so_client;
6828 release_openowner(oop);
6834 nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
6837 unsigned int count = 0;
6838 struct nfs4_client *clp;
6839 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6841 LIST_HEAD(reaplist);
6843 if (!nfsd_netns_ready(nn))
6846 spin_lock(&nn->client_lock);
6847 clp = nfsd_find_client(addr, addr_size);
6849 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
6850 spin_unlock(&nn->client_lock);
6851 nfsd_reap_openowners(&reaplist);
6856 nfsd_inject_forget_openowners(u64 max)
6859 struct nfs4_client *clp;
6860 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6862 LIST_HEAD(reaplist);
6864 if (!nfsd_netns_ready(nn))
6867 spin_lock(&nn->client_lock);
6868 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6869 count += nfsd_collect_client_openowners(clp, &reaplist,
6871 if (max != 0 && count >= max)
6874 spin_unlock(&nn->client_lock);
6875 nfsd_reap_openowners(&reaplist);
6879 static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
6880 struct list_head *victims)
6882 struct nfs4_delegation *dp, *next;
6883 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6887 lockdep_assert_held(&nn->client_lock);
6889 spin_lock(&state_lock);
6890 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
6893 * It's not safe to mess with delegations that have a
6894 * non-zero dl_time. They might have already been broken
6895 * and could be processed by the laundromat outside of
6896 * the state_lock. Just leave them be.
6898 if (dp->dl_time != 0)
6901 atomic_inc(&clp->cl_refcount);
6902 WARN_ON(!unhash_delegation_locked(dp));
6903 list_add(&dp->dl_recall_lru, victims);
6907 * Despite the fact that these functions deal with
6908 * 64-bit integers for "count", we must ensure that
6909 * it doesn't blow up the clp->cl_refcount. Throw a
6910 * warning if we start to approach INT_MAX here.
6912 WARN_ON_ONCE(count == (INT_MAX / 2));
6916 spin_unlock(&state_lock);
6921 nfsd_print_client_delegations(struct nfs4_client *clp)
6923 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
6925 nfsd_print_count(clp, count, "delegations");
6930 nfsd_inject_print_delegations(void)
6932 struct nfs4_client *clp;
6934 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6937 if (!nfsd_netns_ready(nn))
6940 spin_lock(&nn->client_lock);
6941 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6942 count += nfsd_print_client_delegations(clp);
6943 spin_unlock(&nn->client_lock);
6949 nfsd_forget_delegations(struct list_head *reaplist)
6951 struct nfs4_client *clp;
6952 struct nfs4_delegation *dp, *next;
6954 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6955 list_del_init(&dp->dl_recall_lru);
6956 clp = dp->dl_stid.sc_client;
6957 revoke_delegation(dp);
6963 nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
6967 struct nfs4_client *clp;
6968 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6970 LIST_HEAD(reaplist);
6972 if (!nfsd_netns_ready(nn))
6975 spin_lock(&nn->client_lock);
6976 clp = nfsd_find_client(addr, addr_size);
6978 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6979 spin_unlock(&nn->client_lock);
6981 nfsd_forget_delegations(&reaplist);
6986 nfsd_inject_forget_delegations(u64 max)
6989 struct nfs4_client *clp;
6990 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6992 LIST_HEAD(reaplist);
6994 if (!nfsd_netns_ready(nn))
6997 spin_lock(&nn->client_lock);
6998 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6999 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
7000 if (max != 0 && count >= max)
7003 spin_unlock(&nn->client_lock);
7004 nfsd_forget_delegations(&reaplist);
7009 nfsd_recall_delegations(struct list_head *reaplist)
7011 struct nfs4_client *clp;
7012 struct nfs4_delegation *dp, *next;
7014 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
7015 list_del_init(&dp->dl_recall_lru);
7016 clp = dp->dl_stid.sc_client;
7018 * We skipped all entries that had a zero dl_time before,
7019 * so we can now reset the dl_time back to 0. If a delegation
7020 * break comes in now, then it won't make any difference since
7021 * we're recalling it either way.
7023 spin_lock(&state_lock);
7025 spin_unlock(&state_lock);
7026 nfsd_break_one_deleg(dp);
7032 nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
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 clp = nfsd_find_client(addr, addr_size);
7047 count = nfsd_find_all_delegations(clp, 0, &reaplist);
7048 spin_unlock(&nn->client_lock);
7050 nfsd_recall_delegations(&reaplist);
7055 nfsd_inject_recall_delegations(u64 max)
7058 struct nfs4_client *clp, *next;
7059 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7061 LIST_HEAD(reaplist);
7063 if (!nfsd_netns_ready(nn))
7066 spin_lock(&nn->client_lock);
7067 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
7068 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
7069 if (max != 0 && ++count >= max)
7072 spin_unlock(&nn->client_lock);
7073 nfsd_recall_delegations(&reaplist);
7076 #endif /* CONFIG_NFSD_FAULT_INJECTION */
7079 * Since the lifetime of a delegation isn't limited to that of an open, a
7080 * client may quite reasonably hang on to a delegation as long as it has
7081 * the inode cached. This becomes an obvious problem the first time a
7082 * client's inode cache approaches the size of the server's total memory.
7084 * For now we avoid this problem by imposing a hard limit on the number
7085 * of delegations, which varies according to the server's memory size.
7088 set_max_delegations(void)
7091 * Allow at most 4 delegations per megabyte of RAM. Quick
7092 * estimates suggest that in the worst case (where every delegation
7093 * is for a different inode), a delegation could take about 1.5K,
7094 * giving a worst case usage of about 6% of memory.
7096 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
7099 static int nfs4_state_create_net(struct net *net)
7101 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7104 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
7105 CLIENT_HASH_SIZE, GFP_KERNEL);
7106 if (!nn->conf_id_hashtbl)
7108 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
7109 CLIENT_HASH_SIZE, GFP_KERNEL);
7110 if (!nn->unconf_id_hashtbl)
7112 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
7113 SESSION_HASH_SIZE, GFP_KERNEL);
7114 if (!nn->sessionid_hashtbl)
7117 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7118 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
7119 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
7121 for (i = 0; i < SESSION_HASH_SIZE; i++)
7122 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
7123 nn->conf_name_tree = RB_ROOT;
7124 nn->unconf_name_tree = RB_ROOT;
7125 nn->boot_time = get_seconds();
7126 nn->grace_ended = false;
7127 nn->nfsd4_manager.block_opens = true;
7128 INIT_LIST_HEAD(&nn->nfsd4_manager.list);
7129 INIT_LIST_HEAD(&nn->client_lru);
7130 INIT_LIST_HEAD(&nn->close_lru);
7131 INIT_LIST_HEAD(&nn->del_recall_lru);
7132 spin_lock_init(&nn->client_lock);
7134 spin_lock_init(&nn->blocked_locks_lock);
7135 INIT_LIST_HEAD(&nn->blocked_locks_lru);
7137 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
7143 kfree(nn->unconf_id_hashtbl);
7145 kfree(nn->conf_id_hashtbl);
7151 nfs4_state_destroy_net(struct net *net)
7154 struct nfs4_client *clp = NULL;
7155 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7157 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7158 while (!list_empty(&nn->conf_id_hashtbl[i])) {
7159 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7160 destroy_client(clp);
7164 WARN_ON(!list_empty(&nn->blocked_locks_lru));
7166 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7167 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
7168 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7169 destroy_client(clp);
7173 kfree(nn->sessionid_hashtbl);
7174 kfree(nn->unconf_id_hashtbl);
7175 kfree(nn->conf_id_hashtbl);
7180 nfs4_state_start_net(struct net *net)
7182 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7185 ret = nfs4_state_create_net(net);
7188 locks_start_grace(net, &nn->nfsd4_manager);
7189 nfsd4_client_tracking_init(net);
7190 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
7191 nn->nfsd4_grace, net);
7192 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
7196 /* initialization to perform when the nfsd service is started: */
7199 nfs4_state_start(void)
7203 ret = set_callback_cred();
7207 laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
7208 if (laundry_wq == NULL) {
7210 goto out_cleanup_cred;
7212 ret = nfsd4_create_callback_queue();
7214 goto out_free_laundry;
7216 set_max_delegations();
7220 destroy_workqueue(laundry_wq);
7222 cleanup_callback_cred();
7227 nfs4_state_shutdown_net(struct net *net)
7229 struct nfs4_delegation *dp = NULL;
7230 struct list_head *pos, *next, reaplist;
7231 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7233 cancel_delayed_work_sync(&nn->laundromat_work);
7234 locks_end_grace(&nn->nfsd4_manager);
7236 INIT_LIST_HEAD(&reaplist);
7237 spin_lock(&state_lock);
7238 list_for_each_safe(pos, next, &nn->del_recall_lru) {
7239 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
7240 WARN_ON(!unhash_delegation_locked(dp));
7241 list_add(&dp->dl_recall_lru, &reaplist);
7243 spin_unlock(&state_lock);
7244 list_for_each_safe(pos, next, &reaplist) {
7245 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
7246 list_del_init(&dp->dl_recall_lru);
7247 put_clnt_odstate(dp->dl_clnt_odstate);
7248 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
7249 nfs4_put_stid(&dp->dl_stid);
7252 nfsd4_client_tracking_exit(net);
7253 nfs4_state_destroy_net(net);
7257 nfs4_state_shutdown(void)
7259 destroy_workqueue(laundry_wq);
7260 nfsd4_destroy_callback_queue();
7261 cleanup_callback_cred();
7265 get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7267 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
7268 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
7272 put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7274 if (cstate->minorversion) {
7275 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
7276 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
7281 clear_current_stateid(struct nfsd4_compound_state *cstate)
7283 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
7287 * functions to set current state id
7290 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate,
7291 union nfsd4_op_u *u)
7293 put_stateid(cstate, &u->open_downgrade.od_stateid);
7297 nfsd4_set_openstateid(struct nfsd4_compound_state *cstate,
7298 union nfsd4_op_u *u)
7300 put_stateid(cstate, &u->open.op_stateid);
7304 nfsd4_set_closestateid(struct nfsd4_compound_state *cstate,
7305 union nfsd4_op_u *u)
7307 put_stateid(cstate, &u->close.cl_stateid);
7311 nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate,
7312 union nfsd4_op_u *u)
7314 put_stateid(cstate, &u->lock.lk_resp_stateid);
7318 * functions to consume current state id
7322 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate,
7323 union nfsd4_op_u *u)
7325 get_stateid(cstate, &u->open_downgrade.od_stateid);
7329 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate,
7330 union nfsd4_op_u *u)
7332 get_stateid(cstate, &u->delegreturn.dr_stateid);
7336 nfsd4_get_freestateid(struct nfsd4_compound_state *cstate,
7337 union nfsd4_op_u *u)
7339 get_stateid(cstate, &u->free_stateid.fr_stateid);
7343 nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate,
7344 union nfsd4_op_u *u)
7346 get_stateid(cstate, &u->setattr.sa_stateid);
7350 nfsd4_get_closestateid(struct nfsd4_compound_state *cstate,
7351 union nfsd4_op_u *u)
7353 get_stateid(cstate, &u->close.cl_stateid);
7357 nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate,
7358 union nfsd4_op_u *u)
7360 get_stateid(cstate, &u->locku.lu_stateid);
7364 nfsd4_get_readstateid(struct nfsd4_compound_state *cstate,
7365 union nfsd4_op_u *u)
7367 get_stateid(cstate, &u->read.rd_stateid);
7371 nfsd4_get_writestateid(struct nfsd4_compound_state *cstate,
7372 union nfsd4_op_u *u)
7374 get_stateid(cstate, &u->write.wr_stateid);