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 = {
67 static u64 current_sessionid = 1;
69 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
70 #define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
71 #define CURRENT_STATEID(stateid) (!memcmp((stateid), ¤tstateid, sizeof(stateid_t)))
73 /* forward declarations */
74 static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
75 static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
80 * Currently used for the del_recall_lru and file hash table. In an
81 * effort to decrease the scope of the client_mutex, this spinlock may
82 * eventually cover more:
84 static DEFINE_SPINLOCK(state_lock);
87 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
88 * the refcount on the open stateid to drop.
90 static DECLARE_WAIT_QUEUE_HEAD(close_wq);
92 static struct kmem_cache *openowner_slab;
93 static struct kmem_cache *lockowner_slab;
94 static struct kmem_cache *file_slab;
95 static struct kmem_cache *stateid_slab;
96 static struct kmem_cache *deleg_slab;
97 static struct kmem_cache *odstate_slab;
99 static void free_session(struct nfsd4_session *);
101 static const struct nfsd4_callback_ops nfsd4_cb_recall_ops;
103 static bool is_session_dead(struct nfsd4_session *ses)
105 return ses->se_flags & NFS4_SESSION_DEAD;
108 static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
110 if (atomic_read(&ses->se_ref) > ref_held_by_me)
111 return nfserr_jukebox;
112 ses->se_flags |= NFS4_SESSION_DEAD;
116 static bool is_client_expired(struct nfs4_client *clp)
118 return clp->cl_time == 0;
121 static __be32 get_client_locked(struct nfs4_client *clp)
123 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
125 lockdep_assert_held(&nn->client_lock);
127 if (is_client_expired(clp))
128 return nfserr_expired;
129 atomic_inc(&clp->cl_refcount);
133 /* must be called under the client_lock */
135 renew_client_locked(struct nfs4_client *clp)
137 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
139 if (is_client_expired(clp)) {
141 printk("%s: client (clientid %08x/%08x) already expired\n",
143 clp->cl_clientid.cl_boot,
144 clp->cl_clientid.cl_id);
148 dprintk("renewing client (clientid %08x/%08x)\n",
149 clp->cl_clientid.cl_boot,
150 clp->cl_clientid.cl_id);
151 list_move_tail(&clp->cl_lru, &nn->client_lru);
152 clp->cl_time = get_seconds();
155 static void put_client_renew_locked(struct nfs4_client *clp)
157 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
159 lockdep_assert_held(&nn->client_lock);
161 if (!atomic_dec_and_test(&clp->cl_refcount))
163 if (!is_client_expired(clp))
164 renew_client_locked(clp);
167 static void put_client_renew(struct nfs4_client *clp)
169 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
171 if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
173 if (!is_client_expired(clp))
174 renew_client_locked(clp);
175 spin_unlock(&nn->client_lock);
178 static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
182 if (is_session_dead(ses))
183 return nfserr_badsession;
184 status = get_client_locked(ses->se_client);
187 atomic_inc(&ses->se_ref);
191 static void nfsd4_put_session_locked(struct nfsd4_session *ses)
193 struct nfs4_client *clp = ses->se_client;
194 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
196 lockdep_assert_held(&nn->client_lock);
198 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
200 put_client_renew_locked(clp);
203 static void nfsd4_put_session(struct nfsd4_session *ses)
205 struct nfs4_client *clp = ses->se_client;
206 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
208 spin_lock(&nn->client_lock);
209 nfsd4_put_session_locked(ses);
210 spin_unlock(&nn->client_lock);
213 static inline struct nfs4_stateowner *
214 nfs4_get_stateowner(struct nfs4_stateowner *sop)
216 atomic_inc(&sop->so_count);
221 same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
223 return (sop->so_owner.len == owner->len) &&
224 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
227 static struct nfs4_openowner *
228 find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
229 struct nfs4_client *clp)
231 struct nfs4_stateowner *so;
233 lockdep_assert_held(&clp->cl_lock);
235 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
237 if (!so->so_is_open_owner)
239 if (same_owner_str(so, &open->op_owner))
240 return openowner(nfs4_get_stateowner(so));
245 static struct nfs4_openowner *
246 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
247 struct nfs4_client *clp)
249 struct nfs4_openowner *oo;
251 spin_lock(&clp->cl_lock);
252 oo = find_openstateowner_str_locked(hashval, open, clp);
253 spin_unlock(&clp->cl_lock);
258 opaque_hashval(const void *ptr, int nbytes)
260 unsigned char *cptr = (unsigned char *) ptr;
270 static void nfsd4_free_file_rcu(struct rcu_head *rcu)
272 struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
274 kmem_cache_free(file_slab, fp);
278 put_nfs4_file(struct nfs4_file *fi)
280 might_lock(&state_lock);
282 if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) {
283 hlist_del_rcu(&fi->fi_hash);
284 spin_unlock(&state_lock);
285 WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
286 WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
287 call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
292 __nfs4_get_fd(struct nfs4_file *f, int oflag)
294 if (f->fi_fds[oflag])
295 return get_file(f->fi_fds[oflag]);
300 find_writeable_file_locked(struct nfs4_file *f)
304 lockdep_assert_held(&f->fi_lock);
306 ret = __nfs4_get_fd(f, O_WRONLY);
308 ret = __nfs4_get_fd(f, O_RDWR);
313 find_writeable_file(struct nfs4_file *f)
317 spin_lock(&f->fi_lock);
318 ret = find_writeable_file_locked(f);
319 spin_unlock(&f->fi_lock);
324 static struct file *find_readable_file_locked(struct nfs4_file *f)
328 lockdep_assert_held(&f->fi_lock);
330 ret = __nfs4_get_fd(f, O_RDONLY);
332 ret = __nfs4_get_fd(f, O_RDWR);
337 find_readable_file(struct nfs4_file *f)
341 spin_lock(&f->fi_lock);
342 ret = find_readable_file_locked(f);
343 spin_unlock(&f->fi_lock);
349 find_any_file(struct nfs4_file *f)
353 spin_lock(&f->fi_lock);
354 ret = __nfs4_get_fd(f, O_RDWR);
356 ret = __nfs4_get_fd(f, O_WRONLY);
358 ret = __nfs4_get_fd(f, O_RDONLY);
360 spin_unlock(&f->fi_lock);
364 static atomic_long_t num_delegations;
365 unsigned long max_delegations;
368 * Open owner state (share locks)
371 /* hash tables for lock and open owners */
372 #define OWNER_HASH_BITS 8
373 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
374 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
376 static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
380 ret = opaque_hashval(ownername->data, ownername->len);
381 return ret & OWNER_HASH_MASK;
384 /* hash table for nfs4_file */
385 #define FILE_HASH_BITS 8
386 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
388 static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
390 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
393 static unsigned int file_hashval(struct knfsd_fh *fh)
395 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
398 static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
401 __nfs4_file_get_access(struct nfs4_file *fp, u32 access)
403 lockdep_assert_held(&fp->fi_lock);
405 if (access & NFS4_SHARE_ACCESS_WRITE)
406 atomic_inc(&fp->fi_access[O_WRONLY]);
407 if (access & NFS4_SHARE_ACCESS_READ)
408 atomic_inc(&fp->fi_access[O_RDONLY]);
412 nfs4_file_get_access(struct nfs4_file *fp, u32 access)
414 lockdep_assert_held(&fp->fi_lock);
416 /* Does this access mode make sense? */
417 if (access & ~NFS4_SHARE_ACCESS_BOTH)
420 /* Does it conflict with a deny mode already set? */
421 if ((access & fp->fi_share_deny) != 0)
422 return nfserr_share_denied;
424 __nfs4_file_get_access(fp, access);
428 static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
430 /* Common case is that there is no deny mode. */
432 /* Does this deny mode make sense? */
433 if (deny & ~NFS4_SHARE_DENY_BOTH)
436 if ((deny & NFS4_SHARE_DENY_READ) &&
437 atomic_read(&fp->fi_access[O_RDONLY]))
438 return nfserr_share_denied;
440 if ((deny & NFS4_SHARE_DENY_WRITE) &&
441 atomic_read(&fp->fi_access[O_WRONLY]))
442 return nfserr_share_denied;
447 static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
449 might_lock(&fp->fi_lock);
451 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
452 struct file *f1 = NULL;
453 struct file *f2 = NULL;
455 swap(f1, fp->fi_fds[oflag]);
456 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
457 swap(f2, fp->fi_fds[O_RDWR]);
458 spin_unlock(&fp->fi_lock);
466 static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
468 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
470 if (access & NFS4_SHARE_ACCESS_WRITE)
471 __nfs4_file_put_access(fp, O_WRONLY);
472 if (access & NFS4_SHARE_ACCESS_READ)
473 __nfs4_file_put_access(fp, O_RDONLY);
477 * Allocate a new open/delegation state counter. This is needed for
478 * pNFS for proper return on close semantics.
480 * Note that we only allocate it for pNFS-enabled exports, otherwise
481 * all pointers to struct nfs4_clnt_odstate are always NULL.
483 static struct nfs4_clnt_odstate *
484 alloc_clnt_odstate(struct nfs4_client *clp)
486 struct nfs4_clnt_odstate *co;
488 co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
491 atomic_set(&co->co_odcount, 1);
497 hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
499 struct nfs4_file *fp = co->co_file;
501 lockdep_assert_held(&fp->fi_lock);
502 list_add(&co->co_perfile, &fp->fi_clnt_odstate);
506 get_clnt_odstate(struct nfs4_clnt_odstate *co)
509 atomic_inc(&co->co_odcount);
513 put_clnt_odstate(struct nfs4_clnt_odstate *co)
515 struct nfs4_file *fp;
521 if (atomic_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
522 list_del(&co->co_perfile);
523 spin_unlock(&fp->fi_lock);
525 nfsd4_return_all_file_layouts(co->co_client, fp);
526 kmem_cache_free(odstate_slab, co);
530 static struct nfs4_clnt_odstate *
531 find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
533 struct nfs4_clnt_odstate *co;
534 struct nfs4_client *cl;
541 spin_lock(&fp->fi_lock);
542 list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
543 if (co->co_client == cl) {
544 get_clnt_odstate(co);
550 hash_clnt_odstate_locked(new);
552 spin_unlock(&fp->fi_lock);
556 struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl,
557 struct kmem_cache *slab)
559 struct nfs4_stid *stid;
562 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
566 idr_preload(GFP_KERNEL);
567 spin_lock(&cl->cl_lock);
568 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 0, 0, GFP_NOWAIT);
569 spin_unlock(&cl->cl_lock);
573 stid->sc_client = cl;
574 stid->sc_stateid.si_opaque.so_id = new_id;
575 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
576 /* Will be incremented before return to client: */
577 atomic_set(&stid->sc_count, 1);
578 spin_lock_init(&stid->sc_lock);
581 * It shouldn't be a problem to reuse an opaque stateid value.
582 * I don't think it is for 4.1. But with 4.0 I worry that, for
583 * example, a stray write retransmission could be accepted by
584 * the server when it should have been rejected. Therefore,
585 * adopt a trick from the sctp code to attempt to maximize the
586 * amount of time until an id is reused, by ensuring they always
587 * "increase" (mod INT_MAX):
591 kmem_cache_free(slab, stid);
595 static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
597 struct nfs4_stid *stid;
598 struct nfs4_ol_stateid *stp;
600 stid = nfs4_alloc_stid(clp, stateid_slab);
604 stp = openlockstateid(stid);
605 stp->st_stid.sc_free = nfs4_free_ol_stateid;
609 static void nfs4_free_deleg(struct nfs4_stid *stid)
611 kmem_cache_free(deleg_slab, stid);
612 atomic_long_dec(&num_delegations);
616 * When we recall a delegation, we should be careful not to hand it
617 * out again straight away.
618 * To ensure this we keep a pair of bloom filters ('new' and 'old')
619 * in which the filehandles of recalled delegations are "stored".
620 * If a filehandle appear in either filter, a delegation is blocked.
621 * When a delegation is recalled, the filehandle is stored in the "new"
623 * Every 30 seconds we swap the filters and clear the "new" one,
624 * unless both are empty of course.
626 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
627 * low 3 bytes as hash-table indices.
629 * 'blocked_delegations_lock', which is always taken in block_delegations(),
630 * is used to manage concurrent access. Testing does not need the lock
631 * except when swapping the two filters.
633 static DEFINE_SPINLOCK(blocked_delegations_lock);
634 static struct bloom_pair {
635 int entries, old_entries;
637 int new; /* index into 'set' */
638 DECLARE_BITMAP(set[2], 256);
639 } blocked_delegations;
641 static int delegation_blocked(struct knfsd_fh *fh)
644 struct bloom_pair *bd = &blocked_delegations;
646 if (bd->entries == 0)
648 if (seconds_since_boot() - bd->swap_time > 30) {
649 spin_lock(&blocked_delegations_lock);
650 if (seconds_since_boot() - bd->swap_time > 30) {
651 bd->entries -= bd->old_entries;
652 bd->old_entries = bd->entries;
653 memset(bd->set[bd->new], 0,
656 bd->swap_time = seconds_since_boot();
658 spin_unlock(&blocked_delegations_lock);
660 hash = jhash(&fh->fh_base, fh->fh_size, 0);
661 if (test_bit(hash&255, bd->set[0]) &&
662 test_bit((hash>>8)&255, bd->set[0]) &&
663 test_bit((hash>>16)&255, bd->set[0]))
666 if (test_bit(hash&255, bd->set[1]) &&
667 test_bit((hash>>8)&255, bd->set[1]) &&
668 test_bit((hash>>16)&255, bd->set[1]))
674 static void block_delegations(struct knfsd_fh *fh)
677 struct bloom_pair *bd = &blocked_delegations;
679 hash = jhash(&fh->fh_base, fh->fh_size, 0);
681 spin_lock(&blocked_delegations_lock);
682 __set_bit(hash&255, bd->set[bd->new]);
683 __set_bit((hash>>8)&255, bd->set[bd->new]);
684 __set_bit((hash>>16)&255, bd->set[bd->new]);
685 if (bd->entries == 0)
686 bd->swap_time = seconds_since_boot();
688 spin_unlock(&blocked_delegations_lock);
691 static struct nfs4_delegation *
692 alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh,
693 struct nfs4_clnt_odstate *odstate)
695 struct nfs4_delegation *dp;
698 dprintk("NFSD alloc_init_deleg\n");
699 n = atomic_long_inc_return(&num_delegations);
700 if (n < 0 || n > max_delegations)
702 if (delegation_blocked(¤t_fh->fh_handle))
704 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
708 dp->dl_stid.sc_free = nfs4_free_deleg;
710 * delegation seqid's are never incremented. The 4.1 special
711 * meaning of seqid 0 isn't meaningful, really, but let's avoid
712 * 0 anyway just for consistency and use 1:
714 dp->dl_stid.sc_stateid.si_generation = 1;
715 INIT_LIST_HEAD(&dp->dl_perfile);
716 INIT_LIST_HEAD(&dp->dl_perclnt);
717 INIT_LIST_HEAD(&dp->dl_recall_lru);
718 dp->dl_clnt_odstate = odstate;
719 get_clnt_odstate(odstate);
720 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
722 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
723 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
726 atomic_long_dec(&num_delegations);
731 nfs4_put_stid(struct nfs4_stid *s)
733 struct nfs4_file *fp = s->sc_file;
734 struct nfs4_client *clp = s->sc_client;
736 might_lock(&clp->cl_lock);
738 if (!atomic_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
739 wake_up_all(&close_wq);
742 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
743 spin_unlock(&clp->cl_lock);
750 nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
752 stateid_t *src = &stid->sc_stateid;
754 spin_lock(&stid->sc_lock);
755 if (unlikely(++src->si_generation == 0))
756 src->si_generation = 1;
757 memcpy(dst, src, sizeof(*dst));
758 spin_unlock(&stid->sc_lock);
761 static void nfs4_put_deleg_lease(struct nfs4_file *fp)
763 struct file *filp = NULL;
765 spin_lock(&fp->fi_lock);
766 if (fp->fi_deleg_file && --fp->fi_delegees == 0)
767 swap(filp, fp->fi_deleg_file);
768 spin_unlock(&fp->fi_lock);
771 vfs_setlease(filp, F_UNLCK, NULL, (void **)&fp);
776 void nfs4_unhash_stid(struct nfs4_stid *s)
782 * nfs4_get_existing_delegation - Discover if this delegation already exists
783 * @clp: a pointer to the nfs4_client we're granting a delegation to
784 * @fp: a pointer to the nfs4_file we're granting a delegation on
787 * On success: NULL if an existing delegation was not found.
789 * On error: -EAGAIN if one was previously granted to this nfs4_client
790 * for this nfs4_file.
795 nfs4_get_existing_delegation(struct nfs4_client *clp, struct nfs4_file *fp)
797 struct nfs4_delegation *searchdp = NULL;
798 struct nfs4_client *searchclp = NULL;
800 lockdep_assert_held(&state_lock);
801 lockdep_assert_held(&fp->fi_lock);
803 list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
804 searchclp = searchdp->dl_stid.sc_client;
805 if (clp == searchclp) {
813 * hash_delegation_locked - Add a delegation to the appropriate lists
814 * @dp: a pointer to the nfs4_delegation we are adding.
815 * @fp: a pointer to the nfs4_file we're granting a delegation on
818 * On success: NULL if the delegation was successfully hashed.
820 * On error: -EAGAIN if one was previously granted to this
821 * nfs4_client for this nfs4_file. Delegation is not hashed.
826 hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
829 struct nfs4_client *clp = dp->dl_stid.sc_client;
831 lockdep_assert_held(&state_lock);
832 lockdep_assert_held(&fp->fi_lock);
834 status = nfs4_get_existing_delegation(clp, fp);
838 atomic_inc(&dp->dl_stid.sc_count);
839 dp->dl_stid.sc_type = NFS4_DELEG_STID;
840 list_add(&dp->dl_perfile, &fp->fi_delegations);
841 list_add(&dp->dl_perclnt, &clp->cl_delegations);
846 unhash_delegation_locked(struct nfs4_delegation *dp)
848 struct nfs4_file *fp = dp->dl_stid.sc_file;
850 lockdep_assert_held(&state_lock);
852 if (list_empty(&dp->dl_perfile))
855 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
856 /* Ensure that deleg break won't try to requeue it */
858 spin_lock(&fp->fi_lock);
859 list_del_init(&dp->dl_perclnt);
860 list_del_init(&dp->dl_recall_lru);
861 list_del_init(&dp->dl_perfile);
862 spin_unlock(&fp->fi_lock);
866 static void destroy_delegation(struct nfs4_delegation *dp)
870 spin_lock(&state_lock);
871 unhashed = unhash_delegation_locked(dp);
872 spin_unlock(&state_lock);
874 put_clnt_odstate(dp->dl_clnt_odstate);
875 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
876 nfs4_put_stid(&dp->dl_stid);
880 static void revoke_delegation(struct nfs4_delegation *dp)
882 struct nfs4_client *clp = dp->dl_stid.sc_client;
884 WARN_ON(!list_empty(&dp->dl_recall_lru));
886 put_clnt_odstate(dp->dl_clnt_odstate);
887 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
889 if (clp->cl_minorversion == 0)
890 nfs4_put_stid(&dp->dl_stid);
892 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
893 spin_lock(&clp->cl_lock);
894 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
895 spin_unlock(&clp->cl_lock);
903 static unsigned int clientid_hashval(u32 id)
905 return id & CLIENT_HASH_MASK;
908 static unsigned int clientstr_hashval(const char *name)
910 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
914 * We store the NONE, READ, WRITE, and BOTH bits separately in the
915 * st_{access,deny}_bmap field of the stateid, in order to track not
916 * only what share bits are currently in force, but also what
917 * combinations of share bits previous opens have used. This allows us
918 * to enforce the recommendation of rfc 3530 14.2.19 that the server
919 * return an error if the client attempt to downgrade to a combination
920 * of share bits not explicable by closing some of its previous opens.
922 * XXX: This enforcement is actually incomplete, since we don't keep
923 * track of access/deny bit combinations; so, e.g., we allow:
925 * OPEN allow read, deny write
926 * OPEN allow both, deny none
927 * DOWNGRADE allow read, deny none
929 * which we should reject.
932 bmap_to_share_mode(unsigned long bmap) {
934 unsigned int access = 0;
936 for (i = 1; i < 4; i++) {
937 if (test_bit(i, &bmap))
943 /* set share access for a given stateid */
945 set_access(u32 access, struct nfs4_ol_stateid *stp)
947 unsigned char mask = 1 << access;
949 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
950 stp->st_access_bmap |= mask;
953 /* clear share access for a given stateid */
955 clear_access(u32 access, struct nfs4_ol_stateid *stp)
957 unsigned char mask = 1 << access;
959 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
960 stp->st_access_bmap &= ~mask;
963 /* test whether a given stateid has access */
965 test_access(u32 access, struct nfs4_ol_stateid *stp)
967 unsigned char mask = 1 << access;
969 return (bool)(stp->st_access_bmap & mask);
972 /* set share deny for a given stateid */
974 set_deny(u32 deny, struct nfs4_ol_stateid *stp)
976 unsigned char mask = 1 << deny;
978 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
979 stp->st_deny_bmap |= mask;
982 /* clear share deny for a given stateid */
984 clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
986 unsigned char mask = 1 << deny;
988 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
989 stp->st_deny_bmap &= ~mask;
992 /* test whether a given stateid is denying specific access */
994 test_deny(u32 deny, struct nfs4_ol_stateid *stp)
996 unsigned char mask = 1 << deny;
998 return (bool)(stp->st_deny_bmap & mask);
1001 static int nfs4_access_to_omode(u32 access)
1003 switch (access & NFS4_SHARE_ACCESS_BOTH) {
1004 case NFS4_SHARE_ACCESS_READ:
1006 case NFS4_SHARE_ACCESS_WRITE:
1008 case NFS4_SHARE_ACCESS_BOTH:
1016 * A stateid that had a deny mode associated with it is being released
1017 * or downgraded. Recalculate the deny mode on the file.
1020 recalculate_deny_mode(struct nfs4_file *fp)
1022 struct nfs4_ol_stateid *stp;
1024 spin_lock(&fp->fi_lock);
1025 fp->fi_share_deny = 0;
1026 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
1027 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
1028 spin_unlock(&fp->fi_lock);
1032 reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
1035 bool change = false;
1037 for (i = 1; i < 4; i++) {
1038 if ((i & deny) != i) {
1044 /* Recalculate per-file deny mode if there was a change */
1046 recalculate_deny_mode(stp->st_stid.sc_file);
1049 /* release all access and file references for a given stateid */
1051 release_all_access(struct nfs4_ol_stateid *stp)
1054 struct nfs4_file *fp = stp->st_stid.sc_file;
1056 if (fp && stp->st_deny_bmap != 0)
1057 recalculate_deny_mode(fp);
1059 for (i = 1; i < 4; i++) {
1060 if (test_access(i, stp))
1061 nfs4_file_put_access(stp->st_stid.sc_file, i);
1062 clear_access(i, stp);
1066 static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1068 kfree(sop->so_owner.data);
1069 sop->so_ops->so_free(sop);
1072 static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1074 struct nfs4_client *clp = sop->so_client;
1076 might_lock(&clp->cl_lock);
1078 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
1080 sop->so_ops->so_unhash(sop);
1081 spin_unlock(&clp->cl_lock);
1082 nfs4_free_stateowner(sop);
1085 static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
1087 struct nfs4_file *fp = stp->st_stid.sc_file;
1089 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1091 if (list_empty(&stp->st_perfile))
1094 spin_lock(&fp->fi_lock);
1095 list_del_init(&stp->st_perfile);
1096 spin_unlock(&fp->fi_lock);
1097 list_del(&stp->st_perstateowner);
1101 static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
1103 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1105 put_clnt_odstate(stp->st_clnt_odstate);
1106 release_all_access(stp);
1107 if (stp->st_stateowner)
1108 nfs4_put_stateowner(stp->st_stateowner);
1109 kmem_cache_free(stateid_slab, stid);
1112 static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
1114 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1115 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
1118 file = find_any_file(stp->st_stid.sc_file);
1120 filp_close(file, (fl_owner_t)lo);
1121 nfs4_free_ol_stateid(stid);
1125 * Put the persistent reference to an already unhashed generic stateid, while
1126 * holding the cl_lock. If it's the last reference, then put it onto the
1127 * reaplist for later destruction.
1129 static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1130 struct list_head *reaplist)
1132 struct nfs4_stid *s = &stp->st_stid;
1133 struct nfs4_client *clp = s->sc_client;
1135 lockdep_assert_held(&clp->cl_lock);
1137 WARN_ON_ONCE(!list_empty(&stp->st_locks));
1139 if (!atomic_dec_and_test(&s->sc_count)) {
1140 wake_up_all(&close_wq);
1144 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1145 list_add(&stp->st_locks, reaplist);
1148 static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
1150 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
1152 lockdep_assert_held(&oo->oo_owner.so_client->cl_lock);
1154 list_del_init(&stp->st_locks);
1155 nfs4_unhash_stid(&stp->st_stid);
1156 return unhash_ol_stateid(stp);
1159 static void release_lock_stateid(struct nfs4_ol_stateid *stp)
1161 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
1164 spin_lock(&oo->oo_owner.so_client->cl_lock);
1165 unhashed = unhash_lock_stateid(stp);
1166 spin_unlock(&oo->oo_owner.so_client->cl_lock);
1168 nfs4_put_stid(&stp->st_stid);
1171 static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
1173 struct nfs4_client *clp = lo->lo_owner.so_client;
1175 lockdep_assert_held(&clp->cl_lock);
1177 list_del_init(&lo->lo_owner.so_strhash);
1181 * Free a list of generic stateids that were collected earlier after being
1185 free_ol_stateid_reaplist(struct list_head *reaplist)
1187 struct nfs4_ol_stateid *stp;
1188 struct nfs4_file *fp;
1192 while (!list_empty(reaplist)) {
1193 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1195 list_del(&stp->st_locks);
1196 fp = stp->st_stid.sc_file;
1197 stp->st_stid.sc_free(&stp->st_stid);
1203 static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1204 struct list_head *reaplist)
1206 struct nfs4_ol_stateid *stp;
1208 lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1210 while (!list_empty(&open_stp->st_locks)) {
1211 stp = list_entry(open_stp->st_locks.next,
1212 struct nfs4_ol_stateid, st_locks);
1213 WARN_ON(!unhash_lock_stateid(stp));
1214 put_ol_stateid_locked(stp, reaplist);
1218 static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
1219 struct list_head *reaplist)
1223 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1225 unhashed = unhash_ol_stateid(stp);
1226 release_open_stateid_locks(stp, reaplist);
1230 static void release_open_stateid(struct nfs4_ol_stateid *stp)
1232 LIST_HEAD(reaplist);
1234 spin_lock(&stp->st_stid.sc_client->cl_lock);
1235 if (unhash_open_stateid(stp, &reaplist))
1236 put_ol_stateid_locked(stp, &reaplist);
1237 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1238 free_ol_stateid_reaplist(&reaplist);
1241 static void unhash_openowner_locked(struct nfs4_openowner *oo)
1243 struct nfs4_client *clp = oo->oo_owner.so_client;
1245 lockdep_assert_held(&clp->cl_lock);
1247 list_del_init(&oo->oo_owner.so_strhash);
1248 list_del_init(&oo->oo_perclient);
1251 static void release_last_closed_stateid(struct nfs4_openowner *oo)
1253 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1255 struct nfs4_ol_stateid *s;
1257 spin_lock(&nn->client_lock);
1258 s = oo->oo_last_closed_stid;
1260 list_del_init(&oo->oo_close_lru);
1261 oo->oo_last_closed_stid = NULL;
1263 spin_unlock(&nn->client_lock);
1265 nfs4_put_stid(&s->st_stid);
1268 static void release_openowner(struct nfs4_openowner *oo)
1270 struct nfs4_ol_stateid *stp;
1271 struct nfs4_client *clp = oo->oo_owner.so_client;
1272 struct list_head reaplist;
1274 INIT_LIST_HEAD(&reaplist);
1276 spin_lock(&clp->cl_lock);
1277 unhash_openowner_locked(oo);
1278 while (!list_empty(&oo->oo_owner.so_stateids)) {
1279 stp = list_first_entry(&oo->oo_owner.so_stateids,
1280 struct nfs4_ol_stateid, st_perstateowner);
1281 if (unhash_open_stateid(stp, &reaplist))
1282 put_ol_stateid_locked(stp, &reaplist);
1284 spin_unlock(&clp->cl_lock);
1285 free_ol_stateid_reaplist(&reaplist);
1286 release_last_closed_stateid(oo);
1287 nfs4_put_stateowner(&oo->oo_owner);
1291 hash_sessionid(struct nfs4_sessionid *sessionid)
1293 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1295 return sid->sequence % SESSION_HASH_SIZE;
1298 #ifdef CONFIG_SUNRPC_DEBUG
1300 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1302 u32 *ptr = (u32 *)(&sessionid->data[0]);
1303 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1307 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1313 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1314 * won't be used for replay.
1316 void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1318 struct nfs4_stateowner *so = cstate->replay_owner;
1320 if (nfserr == nfserr_replay_me)
1323 if (!seqid_mutating_err(ntohl(nfserr))) {
1324 nfsd4_cstate_clear_replay(cstate);
1329 if (so->so_is_open_owner)
1330 release_last_closed_stateid(openowner(so));
1336 gen_sessionid(struct nfsd4_session *ses)
1338 struct nfs4_client *clp = ses->se_client;
1339 struct nfsd4_sessionid *sid;
1341 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1342 sid->clientid = clp->cl_clientid;
1343 sid->sequence = current_sessionid++;
1348 * The protocol defines ca_maxresponssize_cached to include the size of
1349 * the rpc header, but all we need to cache is the data starting after
1350 * the end of the initial SEQUENCE operation--the rest we regenerate
1351 * each time. Therefore we can advertise a ca_maxresponssize_cached
1352 * value that is the number of bytes in our cache plus a few additional
1353 * bytes. In order to stay on the safe side, and not promise more than
1354 * we can cache, those additional bytes must be the minimum possible: 24
1355 * bytes of rpc header (xid through accept state, with AUTH_NULL
1356 * verifier), 12 for the compound header (with zero-length tag), and 44
1357 * for the SEQUENCE op response:
1359 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1362 free_session_slots(struct nfsd4_session *ses)
1366 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1367 kfree(ses->se_slots[i]);
1371 * We don't actually need to cache the rpc and session headers, so we
1372 * can allocate a little less for each slot:
1374 static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
1378 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1381 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1382 return size + sizeof(struct nfsd4_slot);
1386 * XXX: If we run out of reserved DRC memory we could (up to a point)
1387 * re-negotiate active sessions and reduce their slot usage to make
1388 * room for new connections. For now we just fail the create session.
1390 static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
1392 u32 slotsize = slot_bytes(ca);
1393 u32 num = ca->maxreqs;
1396 spin_lock(&nfsd_drc_lock);
1397 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1398 nfsd_drc_max_mem - nfsd_drc_mem_used);
1399 num = min_t(int, num, avail / slotsize);
1400 nfsd_drc_mem_used += num * slotsize;
1401 spin_unlock(&nfsd_drc_lock);
1406 static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
1408 int slotsize = slot_bytes(ca);
1410 spin_lock(&nfsd_drc_lock);
1411 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
1412 spin_unlock(&nfsd_drc_lock);
1415 static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1416 struct nfsd4_channel_attrs *battrs)
1418 int numslots = fattrs->maxreqs;
1419 int slotsize = slot_bytes(fattrs);
1420 struct nfsd4_session *new;
1423 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
1424 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1425 mem = numslots * sizeof(struct nfsd4_slot *);
1427 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
1430 /* allocate each struct nfsd4_slot and data cache in one piece */
1431 for (i = 0; i < numslots; i++) {
1432 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
1433 if (!new->se_slots[i])
1437 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1438 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1443 kfree(new->se_slots[i]);
1448 static void free_conn(struct nfsd4_conn *c)
1450 svc_xprt_put(c->cn_xprt);
1454 static void nfsd4_conn_lost(struct svc_xpt_user *u)
1456 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1457 struct nfs4_client *clp = c->cn_session->se_client;
1459 spin_lock(&clp->cl_lock);
1460 if (!list_empty(&c->cn_persession)) {
1461 list_del(&c->cn_persession);
1464 nfsd4_probe_callback(clp);
1465 spin_unlock(&clp->cl_lock);
1468 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
1470 struct nfsd4_conn *conn;
1472 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1475 svc_xprt_get(rqstp->rq_xprt);
1476 conn->cn_xprt = rqstp->rq_xprt;
1477 conn->cn_flags = flags;
1478 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1482 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1484 conn->cn_session = ses;
1485 list_add(&conn->cn_persession, &ses->se_conns);
1488 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1490 struct nfs4_client *clp = ses->se_client;
1492 spin_lock(&clp->cl_lock);
1493 __nfsd4_hash_conn(conn, ses);
1494 spin_unlock(&clp->cl_lock);
1497 static int nfsd4_register_conn(struct nfsd4_conn *conn)
1499 conn->cn_xpt_user.callback = nfsd4_conn_lost;
1500 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
1503 static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
1507 nfsd4_hash_conn(conn, ses);
1508 ret = nfsd4_register_conn(conn);
1510 /* oops; xprt is already down: */
1511 nfsd4_conn_lost(&conn->cn_xpt_user);
1512 /* We may have gained or lost a callback channel: */
1513 nfsd4_probe_callback_sync(ses->se_client);
1516 static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
1518 u32 dir = NFS4_CDFC4_FORE;
1520 if (cses->flags & SESSION4_BACK_CHAN)
1521 dir |= NFS4_CDFC4_BACK;
1522 return alloc_conn(rqstp, dir);
1525 /* must be called under client_lock */
1526 static void nfsd4_del_conns(struct nfsd4_session *s)
1528 struct nfs4_client *clp = s->se_client;
1529 struct nfsd4_conn *c;
1531 spin_lock(&clp->cl_lock);
1532 while (!list_empty(&s->se_conns)) {
1533 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1534 list_del_init(&c->cn_persession);
1535 spin_unlock(&clp->cl_lock);
1537 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1540 spin_lock(&clp->cl_lock);
1542 spin_unlock(&clp->cl_lock);
1545 static void __free_session(struct nfsd4_session *ses)
1547 free_session_slots(ses);
1551 static void free_session(struct nfsd4_session *ses)
1553 nfsd4_del_conns(ses);
1554 nfsd4_put_drc_mem(&ses->se_fchannel);
1555 __free_session(ses);
1558 static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
1561 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1563 new->se_client = clp;
1566 INIT_LIST_HEAD(&new->se_conns);
1568 new->se_cb_seq_nr = 1;
1569 new->se_flags = cses->flags;
1570 new->se_cb_prog = cses->callback_prog;
1571 new->se_cb_sec = cses->cb_sec;
1572 atomic_set(&new->se_ref, 0);
1573 idx = hash_sessionid(&new->se_sessionid);
1574 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
1575 spin_lock(&clp->cl_lock);
1576 list_add(&new->se_perclnt, &clp->cl_sessions);
1577 spin_unlock(&clp->cl_lock);
1580 struct sockaddr *sa = svc_addr(rqstp);
1582 * This is a little silly; with sessions there's no real
1583 * use for the callback address. Use the peer address
1584 * as a reasonable default for now, but consider fixing
1585 * the rpc client not to require an address in the
1588 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1589 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
1593 /* caller must hold client_lock */
1594 static struct nfsd4_session *
1595 __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
1597 struct nfsd4_session *elem;
1599 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1601 lockdep_assert_held(&nn->client_lock);
1603 dump_sessionid(__func__, sessionid);
1604 idx = hash_sessionid(sessionid);
1605 /* Search in the appropriate list */
1606 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
1607 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1608 NFS4_MAX_SESSIONID_LEN)) {
1613 dprintk("%s: session not found\n", __func__);
1617 static struct nfsd4_session *
1618 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1621 struct nfsd4_session *session;
1622 __be32 status = nfserr_badsession;
1624 session = __find_in_sessionid_hashtbl(sessionid, net);
1627 status = nfsd4_get_session_locked(session);
1635 /* caller must hold client_lock */
1637 unhash_session(struct nfsd4_session *ses)
1639 struct nfs4_client *clp = ses->se_client;
1640 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1642 lockdep_assert_held(&nn->client_lock);
1644 list_del(&ses->se_hash);
1645 spin_lock(&ses->se_client->cl_lock);
1646 list_del(&ses->se_perclnt);
1647 spin_unlock(&ses->se_client->cl_lock);
1650 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1652 STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
1655 * We're assuming the clid was not given out from a boot
1656 * precisely 2^32 (about 136 years) before this one. That seems
1657 * a safe assumption:
1659 if (clid->cl_boot == (u32)nn->boot_time)
1661 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
1662 clid->cl_boot, clid->cl_id, nn->boot_time);
1667 * XXX Should we use a slab cache ?
1668 * This type of memory management is somewhat inefficient, but we use it
1669 * anyway since SETCLIENTID is not a common operation.
1671 static struct nfs4_client *alloc_client(struct xdr_netobj name)
1673 struct nfs4_client *clp;
1676 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1679 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
1680 if (clp->cl_name.data == NULL)
1682 clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
1683 OWNER_HASH_SIZE, GFP_KERNEL);
1684 if (!clp->cl_ownerstr_hashtbl)
1685 goto err_no_hashtbl;
1686 for (i = 0; i < OWNER_HASH_SIZE; i++)
1687 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
1688 clp->cl_name.len = name.len;
1689 INIT_LIST_HEAD(&clp->cl_sessions);
1690 idr_init(&clp->cl_stateids);
1691 atomic_set(&clp->cl_refcount, 0);
1692 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1693 INIT_LIST_HEAD(&clp->cl_idhash);
1694 INIT_LIST_HEAD(&clp->cl_openowners);
1695 INIT_LIST_HEAD(&clp->cl_delegations);
1696 INIT_LIST_HEAD(&clp->cl_lru);
1697 INIT_LIST_HEAD(&clp->cl_revoked);
1698 #ifdef CONFIG_NFSD_PNFS
1699 INIT_LIST_HEAD(&clp->cl_lo_states);
1701 spin_lock_init(&clp->cl_lock);
1702 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1705 kfree(clp->cl_name.data);
1712 free_client(struct nfs4_client *clp)
1714 while (!list_empty(&clp->cl_sessions)) {
1715 struct nfsd4_session *ses;
1716 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1718 list_del(&ses->se_perclnt);
1719 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1722 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
1723 free_svc_cred(&clp->cl_cred);
1724 kfree(clp->cl_ownerstr_hashtbl);
1725 kfree(clp->cl_name.data);
1726 idr_destroy(&clp->cl_stateids);
1730 /* must be called under the client_lock */
1732 unhash_client_locked(struct nfs4_client *clp)
1734 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1735 struct nfsd4_session *ses;
1737 lockdep_assert_held(&nn->client_lock);
1739 /* Mark the client as expired! */
1741 /* Make it invisible */
1742 if (!list_empty(&clp->cl_idhash)) {
1743 list_del_init(&clp->cl_idhash);
1744 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1745 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1747 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1749 list_del_init(&clp->cl_lru);
1750 spin_lock(&clp->cl_lock);
1751 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1752 list_del_init(&ses->se_hash);
1753 spin_unlock(&clp->cl_lock);
1757 unhash_client(struct nfs4_client *clp)
1759 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1761 spin_lock(&nn->client_lock);
1762 unhash_client_locked(clp);
1763 spin_unlock(&nn->client_lock);
1766 static __be32 mark_client_expired_locked(struct nfs4_client *clp)
1768 if (atomic_read(&clp->cl_refcount))
1769 return nfserr_jukebox;
1770 unhash_client_locked(clp);
1775 __destroy_client(struct nfs4_client *clp)
1777 struct nfs4_openowner *oo;
1778 struct nfs4_delegation *dp;
1779 struct list_head reaplist;
1781 INIT_LIST_HEAD(&reaplist);
1782 spin_lock(&state_lock);
1783 while (!list_empty(&clp->cl_delegations)) {
1784 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
1785 WARN_ON(!unhash_delegation_locked(dp));
1786 list_add(&dp->dl_recall_lru, &reaplist);
1788 spin_unlock(&state_lock);
1789 while (!list_empty(&reaplist)) {
1790 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
1791 list_del_init(&dp->dl_recall_lru);
1792 put_clnt_odstate(dp->dl_clnt_odstate);
1793 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
1794 nfs4_put_stid(&dp->dl_stid);
1796 while (!list_empty(&clp->cl_revoked)) {
1797 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
1798 list_del_init(&dp->dl_recall_lru);
1799 nfs4_put_stid(&dp->dl_stid);
1801 while (!list_empty(&clp->cl_openowners)) {
1802 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1803 nfs4_get_stateowner(&oo->oo_owner);
1804 release_openowner(oo);
1806 nfsd4_return_all_client_layouts(clp);
1807 nfsd4_shutdown_callback(clp);
1808 if (clp->cl_cb_conn.cb_xprt)
1809 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
1814 destroy_client(struct nfs4_client *clp)
1817 __destroy_client(clp);
1820 static void expire_client(struct nfs4_client *clp)
1823 nfsd4_client_record_remove(clp);
1824 __destroy_client(clp);
1827 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1829 memcpy(target->cl_verifier.data, source->data,
1830 sizeof(target->cl_verifier.data));
1833 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1835 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1836 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1839 int strdup_if_nonnull(char **target, char *source)
1842 *target = kstrdup(source, GFP_KERNEL);
1850 static int copy_cred(struct svc_cred *target, struct svc_cred *source)
1854 ret = strdup_if_nonnull(&target->cr_principal, source->cr_principal);
1857 ret = strdup_if_nonnull(&target->cr_raw_principal,
1858 source->cr_raw_principal);
1861 target->cr_flavor = source->cr_flavor;
1862 target->cr_uid = source->cr_uid;
1863 target->cr_gid = source->cr_gid;
1864 target->cr_group_info = source->cr_group_info;
1865 get_group_info(target->cr_group_info);
1866 target->cr_gss_mech = source->cr_gss_mech;
1867 if (source->cr_gss_mech)
1868 gss_mech_get(source->cr_gss_mech);
1873 compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1875 if (o1->len < o2->len)
1877 if (o1->len > o2->len)
1879 return memcmp(o1->data, o2->data, o1->len);
1882 static int same_name(const char *n1, const char *n2)
1884 return 0 == memcmp(n1, n2, HEXDIR_LEN);
1888 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1890 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
1894 same_clid(clientid_t *cl1, clientid_t *cl2)
1896 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
1899 static bool groups_equal(struct group_info *g1, struct group_info *g2)
1903 if (g1->ngroups != g2->ngroups)
1905 for (i=0; i<g1->ngroups; i++)
1906 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
1912 * RFC 3530 language requires clid_inuse be returned when the
1913 * "principal" associated with a requests differs from that previously
1914 * used. We use uid, gid's, and gss principal string as our best
1915 * approximation. We also don't want to allow non-gss use of a client
1916 * established using gss: in theory cr_principal should catch that
1917 * change, but in practice cr_principal can be null even in the gss case
1918 * since gssd doesn't always pass down a principal string.
1920 static bool is_gss_cred(struct svc_cred *cr)
1922 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1923 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1928 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1930 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
1931 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1932 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
1933 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1935 if (cr1->cr_principal == cr2->cr_principal)
1937 if (!cr1->cr_principal || !cr2->cr_principal)
1939 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
1942 static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1944 struct svc_cred *cr = &rqstp->rq_cred;
1947 if (!cr->cr_gss_mech)
1949 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1950 return service == RPC_GSS_SVC_INTEGRITY ||
1951 service == RPC_GSS_SVC_PRIVACY;
1954 bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1956 struct svc_cred *cr = &rqstp->rq_cred;
1958 if (!cl->cl_mach_cred)
1960 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1962 if (!svc_rqst_integrity_protected(rqstp))
1964 if (cl->cl_cred.cr_raw_principal)
1965 return 0 == strcmp(cl->cl_cred.cr_raw_principal,
1966 cr->cr_raw_principal);
1967 if (!cr->cr_principal)
1969 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1972 static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
1977 * This is opaque to client, so no need to byte-swap. Use
1978 * __force to keep sparse happy
1980 verf[0] = (__force __be32)get_seconds();
1981 verf[1] = (__force __be32)nn->clverifier_counter++;
1982 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
1985 static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
1987 clp->cl_clientid.cl_boot = nn->boot_time;
1988 clp->cl_clientid.cl_id = nn->clientid_counter++;
1989 gen_confirm(clp, nn);
1992 static struct nfs4_stid *
1993 find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
1995 struct nfs4_stid *ret;
1997 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1998 if (!ret || !ret->sc_type)
2003 static struct nfs4_stid *
2004 find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
2006 struct nfs4_stid *s;
2008 spin_lock(&cl->cl_lock);
2009 s = find_stateid_locked(cl, t);
2011 if (typemask & s->sc_type)
2012 atomic_inc(&s->sc_count);
2016 spin_unlock(&cl->cl_lock);
2020 static struct nfs4_client *create_client(struct xdr_netobj name,
2021 struct svc_rqst *rqstp, nfs4_verifier *verf)
2023 struct nfs4_client *clp;
2024 struct sockaddr *sa = svc_addr(rqstp);
2026 struct net *net = SVC_NET(rqstp);
2028 clp = alloc_client(name);
2032 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
2037 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
2038 clp->cl_time = get_seconds();
2039 clear_bit(0, &clp->cl_cb_slot_busy);
2040 copy_verf(clp, verf);
2041 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
2042 clp->cl_cb_session = NULL;
2048 add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
2050 struct rb_node **new = &(root->rb_node), *parent = NULL;
2051 struct nfs4_client *clp;
2054 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
2057 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
2058 new = &((*new)->rb_left);
2060 new = &((*new)->rb_right);
2063 rb_link_node(&new_clp->cl_namenode, parent, new);
2064 rb_insert_color(&new_clp->cl_namenode, root);
2067 static struct nfs4_client *
2068 find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2071 struct rb_node *node = root->rb_node;
2072 struct nfs4_client *clp;
2075 clp = rb_entry(node, struct nfs4_client, cl_namenode);
2076 cmp = compare_blob(&clp->cl_name, name);
2078 node = node->rb_left;
2080 node = node->rb_right;
2088 add_to_unconfirmed(struct nfs4_client *clp)
2090 unsigned int idhashval;
2091 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2093 lockdep_assert_held(&nn->client_lock);
2095 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2096 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
2097 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
2098 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
2099 renew_client_locked(clp);
2103 move_to_confirmed(struct nfs4_client *clp)
2105 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
2106 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2108 lockdep_assert_held(&nn->client_lock);
2110 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
2111 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
2112 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
2113 add_clp_to_name_tree(clp, &nn->conf_name_tree);
2114 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2115 renew_client_locked(clp);
2118 static struct nfs4_client *
2119 find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
2121 struct nfs4_client *clp;
2122 unsigned int idhashval = clientid_hashval(clid->cl_id);
2124 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
2125 if (same_clid(&clp->cl_clientid, clid)) {
2126 if ((bool)clp->cl_minorversion != sessions)
2128 renew_client_locked(clp);
2135 static struct nfs4_client *
2136 find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2138 struct list_head *tbl = nn->conf_id_hashtbl;
2140 lockdep_assert_held(&nn->client_lock);
2141 return find_client_in_id_table(tbl, clid, sessions);
2144 static struct nfs4_client *
2145 find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2147 struct list_head *tbl = nn->unconf_id_hashtbl;
2149 lockdep_assert_held(&nn->client_lock);
2150 return find_client_in_id_table(tbl, clid, sessions);
2153 static bool clp_used_exchangeid(struct nfs4_client *clp)
2155 return clp->cl_exchange_flags != 0;
2158 static struct nfs4_client *
2159 find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
2161 lockdep_assert_held(&nn->client_lock);
2162 return find_clp_in_name_tree(name, &nn->conf_name_tree);
2165 static struct nfs4_client *
2166 find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
2168 lockdep_assert_held(&nn->client_lock);
2169 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
2173 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
2175 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
2176 struct sockaddr *sa = svc_addr(rqstp);
2177 u32 scopeid = rpc_get_scope_id(sa);
2178 unsigned short expected_family;
2180 /* Currently, we only support tcp and tcp6 for the callback channel */
2181 if (se->se_callback_netid_len == 3 &&
2182 !memcmp(se->se_callback_netid_val, "tcp", 3))
2183 expected_family = AF_INET;
2184 else if (se->se_callback_netid_len == 4 &&
2185 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2186 expected_family = AF_INET6;
2190 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
2191 se->se_callback_addr_len,
2192 (struct sockaddr *)&conn->cb_addr,
2193 sizeof(conn->cb_addr));
2195 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
2198 if (conn->cb_addr.ss_family == AF_INET6)
2199 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
2201 conn->cb_prog = se->se_callback_prog;
2202 conn->cb_ident = se->se_callback_ident;
2203 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
2206 conn->cb_addr.ss_family = AF_UNSPEC;
2207 conn->cb_addrlen = 0;
2208 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
2209 "will not receive delegations\n",
2210 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2216 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
2219 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2221 struct xdr_buf *buf = resp->xdr.buf;
2222 struct nfsd4_slot *slot = resp->cstate.slot;
2225 dprintk("--> %s slot %p\n", __func__, slot);
2227 slot->sl_opcnt = resp->opcnt;
2228 slot->sl_status = resp->cstate.status;
2230 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
2231 if (nfsd4_not_cached(resp)) {
2232 slot->sl_datalen = 0;
2235 base = resp->cstate.data_offset;
2236 slot->sl_datalen = buf->len - base;
2237 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
2238 WARN(1, "%s: sessions DRC could not cache compound\n",
2244 * Encode the replay sequence operation from the slot values.
2245 * If cachethis is FALSE encode the uncached rep error on the next
2246 * operation which sets resp->p and increments resp->opcnt for
2247 * nfs4svc_encode_compoundres.
2251 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2252 struct nfsd4_compoundres *resp)
2254 struct nfsd4_op *op;
2255 struct nfsd4_slot *slot = resp->cstate.slot;
2257 /* Encode the replayed sequence operation */
2258 op = &args->ops[resp->opcnt - 1];
2259 nfsd4_encode_operation(resp, op);
2261 /* Return nfserr_retry_uncached_rep in next operation. */
2262 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
2263 op = &args->ops[resp->opcnt++];
2264 op->status = nfserr_retry_uncached_rep;
2265 nfsd4_encode_operation(resp, op);
2271 * The sequence operation is not cached because we can use the slot and
2275 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2276 struct nfsd4_sequence *seq)
2278 struct nfsd4_slot *slot = resp->cstate.slot;
2279 struct xdr_stream *xdr = &resp->xdr;
2283 dprintk("--> %s slot %p\n", __func__, slot);
2285 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
2289 p = xdr_reserve_space(xdr, slot->sl_datalen);
2292 return nfserr_serverfault;
2294 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2295 xdr_commit_encode(xdr);
2297 resp->opcnt = slot->sl_opcnt;
2298 return slot->sl_status;
2302 * Set the exchange_id flags returned by the server.
2305 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2307 #ifdef CONFIG_NFSD_PNFS
2308 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
2310 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
2313 /* Referrals are supported, Migration is not. */
2314 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2316 /* set the wire flags to return to client. */
2317 clid->flags = new->cl_exchange_flags;
2320 static bool client_has_openowners(struct nfs4_client *clp)
2322 struct nfs4_openowner *oo;
2324 list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
2325 if (!list_empty(&oo->oo_owner.so_stateids))
2331 static bool client_has_state(struct nfs4_client *clp)
2333 return client_has_openowners(clp)
2334 #ifdef CONFIG_NFSD_PNFS
2335 || !list_empty(&clp->cl_lo_states)
2337 || !list_empty(&clp->cl_delegations)
2338 || !list_empty(&clp->cl_sessions);
2342 nfsd4_exchange_id(struct svc_rqst *rqstp,
2343 struct nfsd4_compound_state *cstate,
2344 struct nfsd4_exchange_id *exid)
2346 struct nfs4_client *conf, *new;
2347 struct nfs4_client *unconf = NULL;
2349 char addr_str[INET6_ADDRSTRLEN];
2350 nfs4_verifier verf = exid->verifier;
2351 struct sockaddr *sa = svc_addr(rqstp);
2352 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
2353 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2355 rpc_ntop(sa, addr_str, sizeof(addr_str));
2356 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
2357 "ip_addr=%s flags %x, spa_how %d\n",
2358 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
2359 addr_str, exid->flags, exid->spa_how);
2361 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
2362 return nfserr_inval;
2364 new = create_client(exid->clname, rqstp, &verf);
2366 return nfserr_jukebox;
2368 switch (exid->spa_how) {
2370 exid->spo_must_enforce[0] = 0;
2371 exid->spo_must_enforce[1] = (
2372 1 << (OP_BIND_CONN_TO_SESSION - 32) |
2373 1 << (OP_EXCHANGE_ID - 32) |
2374 1 << (OP_CREATE_SESSION - 32) |
2375 1 << (OP_DESTROY_SESSION - 32) |
2376 1 << (OP_DESTROY_CLIENTID - 32));
2378 exid->spo_must_allow[0] &= (1 << (OP_CLOSE) |
2379 1 << (OP_OPEN_DOWNGRADE) |
2381 1 << (OP_DELEGRETURN));
2383 exid->spo_must_allow[1] &= (
2384 1 << (OP_TEST_STATEID - 32) |
2385 1 << (OP_FREE_STATEID - 32));
2386 if (!svc_rqst_integrity_protected(rqstp)) {
2387 status = nfserr_inval;
2391 * Sometimes userspace doesn't give us a principal.
2392 * Which is a bug, really. Anyway, we can't enforce
2393 * MACH_CRED in that case, better to give up now:
2395 if (!new->cl_cred.cr_principal &&
2396 !new->cl_cred.cr_raw_principal) {
2397 status = nfserr_serverfault;
2400 new->cl_mach_cred = true;
2403 default: /* checked by xdr code */
2406 status = nfserr_encr_alg_unsupp;
2410 /* Cases below refer to rfc 5661 section 18.35.4: */
2411 spin_lock(&nn->client_lock);
2412 conf = find_confirmed_client_by_name(&exid->clname, nn);
2414 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2415 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2418 if (!clp_used_exchangeid(conf)) { /* buggy client */
2419 status = nfserr_inval;
2422 if (!nfsd4_mach_creds_match(conf, rqstp)) {
2423 status = nfserr_wrong_cred;
2426 if (!creds_match) { /* case 9 */
2427 status = nfserr_perm;
2430 if (!verfs_match) { /* case 8 */
2431 status = nfserr_not_same;
2435 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
2438 if (!creds_match) { /* case 3 */
2439 if (client_has_state(conf)) {
2440 status = nfserr_clid_inuse;
2445 if (verfs_match) { /* case 2 */
2446 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
2449 /* case 5, client reboot */
2454 if (update) { /* case 7 */
2455 status = nfserr_noent;
2459 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
2460 if (unconf) /* case 4, possible retry or client restart */
2461 unhash_client_locked(unconf);
2463 /* case 1 (normal case) */
2466 status = mark_client_expired_locked(conf);
2470 new->cl_minorversion = cstate->minorversion;
2471 new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0];
2472 new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1];
2475 add_to_unconfirmed(new);
2478 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
2479 exid->clientid.cl_id = conf->cl_clientid.cl_id;
2481 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
2482 nfsd4_set_ex_flags(conf, exid);
2484 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
2485 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
2489 spin_unlock(&nn->client_lock);
2494 expire_client(unconf);
2499 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
2501 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2504 /* The slot is in use, and no response has been sent. */
2506 if (seqid == slot_seqid)
2507 return nfserr_jukebox;
2509 return nfserr_seq_misordered;
2511 /* Note unsigned 32-bit arithmetic handles wraparound: */
2512 if (likely(seqid == slot_seqid + 1))
2514 if (seqid == slot_seqid)
2515 return nfserr_replay_cache;
2516 return nfserr_seq_misordered;
2520 * Cache the create session result into the create session single DRC
2521 * slot cache by saving the xdr structure. sl_seqid has been set.
2522 * Do this for solo or embedded create session operations.
2525 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
2526 struct nfsd4_clid_slot *slot, __be32 nfserr)
2528 slot->sl_status = nfserr;
2529 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2533 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2534 struct nfsd4_clid_slot *slot)
2536 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2537 return slot->sl_status;
2540 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2541 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2542 1 + /* MIN tag is length with zero, only length */ \
2543 3 + /* version, opcount, opcode */ \
2544 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2545 /* seqid, slotID, slotID, cache */ \
2546 4 ) * sizeof(__be32))
2548 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2549 2 + /* verifier: AUTH_NULL, length 0 */\
2551 1 + /* MIN tag is length with zero, only length */ \
2552 3 + /* opcount, opcode, opstatus*/ \
2553 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2554 /* seqid, slotID, slotID, slotID, status */ \
2555 5 ) * sizeof(__be32))
2557 static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
2559 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2561 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2562 return nfserr_toosmall;
2563 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2564 return nfserr_toosmall;
2565 ca->headerpadsz = 0;
2566 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2567 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2568 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2569 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2570 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2571 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2573 * Note decreasing slot size below client's request may make it
2574 * difficult for client to function correctly, whereas
2575 * decreasing the number of slots will (just?) affect
2576 * performance. When short on memory we therefore prefer to
2577 * decrease number of slots instead of their size. Clients that
2578 * request larger slots than they need will get poor results:
2580 ca->maxreqs = nfsd4_get_drc_mem(ca);
2582 return nfserr_jukebox;
2588 * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
2589 * These are based on similar macros in linux/sunrpc/msg_prot.h .
2591 #define RPC_MAX_HEADER_WITH_AUTH_SYS \
2592 (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
2594 #define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
2595 (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
2597 #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2598 RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
2599 #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2600 RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
2603 static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2605 ca->headerpadsz = 0;
2607 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
2608 return nfserr_toosmall;
2609 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
2610 return nfserr_toosmall;
2611 ca->maxresp_cached = 0;
2613 return nfserr_toosmall;
2618 static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2620 switch (cbs->flavor) {
2626 * GSS case: the spec doesn't allow us to return this
2627 * error. But it also doesn't allow us not to support
2629 * I'd rather this fail hard than return some error the
2630 * client might think it can already handle:
2632 return nfserr_encr_alg_unsupp;
2637 nfsd4_create_session(struct svc_rqst *rqstp,
2638 struct nfsd4_compound_state *cstate,
2639 struct nfsd4_create_session *cr_ses)
2641 struct sockaddr *sa = svc_addr(rqstp);
2642 struct nfs4_client *conf, *unconf;
2643 struct nfs4_client *old = NULL;
2644 struct nfsd4_session *new;
2645 struct nfsd4_conn *conn;
2646 struct nfsd4_clid_slot *cs_slot = NULL;
2648 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2650 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2651 return nfserr_inval;
2652 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2655 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
2658 status = check_backchannel_attrs(&cr_ses->back_channel);
2660 goto out_release_drc_mem;
2661 status = nfserr_jukebox;
2662 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
2664 goto out_release_drc_mem;
2665 conn = alloc_conn_from_crses(rqstp, cr_ses);
2667 goto out_free_session;
2669 spin_lock(&nn->client_lock);
2670 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
2671 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
2672 WARN_ON_ONCE(conf && unconf);
2675 status = nfserr_wrong_cred;
2676 if (!nfsd4_mach_creds_match(conf, rqstp))
2678 cs_slot = &conf->cl_cs_slot;
2679 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2681 if (status == nfserr_replay_cache)
2682 status = nfsd4_replay_create_session(cr_ses, cs_slot);
2685 } else if (unconf) {
2686 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
2687 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
2688 status = nfserr_clid_inuse;
2691 status = nfserr_wrong_cred;
2692 if (!nfsd4_mach_creds_match(unconf, rqstp))
2694 cs_slot = &unconf->cl_cs_slot;
2695 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2697 /* an unconfirmed replay returns misordered */
2698 status = nfserr_seq_misordered;
2701 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
2703 status = mark_client_expired_locked(old);
2709 move_to_confirmed(unconf);
2712 status = nfserr_stale_clientid;
2716 /* Persistent sessions are not supported */
2717 cr_ses->flags &= ~SESSION4_PERSIST;
2718 /* Upshifting from TCP to RDMA is not supported */
2719 cr_ses->flags &= ~SESSION4_RDMA;
2721 init_session(rqstp, new, conf, cr_ses);
2722 nfsd4_get_session_locked(new);
2724 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
2725 NFS4_MAX_SESSIONID_LEN);
2726 cs_slot->sl_seqid++;
2727 cr_ses->seqid = cs_slot->sl_seqid;
2729 /* cache solo and embedded create sessions under the client_lock */
2730 nfsd4_cache_create_session(cr_ses, cs_slot, status);
2731 spin_unlock(&nn->client_lock);
2732 /* init connection and backchannel */
2733 nfsd4_init_conn(rqstp, conn, new);
2734 nfsd4_put_session(new);
2739 spin_unlock(&nn->client_lock);
2744 __free_session(new);
2745 out_release_drc_mem:
2746 nfsd4_put_drc_mem(&cr_ses->fore_channel);
2750 static __be32 nfsd4_map_bcts_dir(u32 *dir)
2753 case NFS4_CDFC4_FORE:
2754 case NFS4_CDFC4_BACK:
2756 case NFS4_CDFC4_FORE_OR_BOTH:
2757 case NFS4_CDFC4_BACK_OR_BOTH:
2758 *dir = NFS4_CDFC4_BOTH;
2761 return nfserr_inval;
2764 __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2766 struct nfsd4_session *session = cstate->session;
2767 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2770 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2773 spin_lock(&nn->client_lock);
2774 session->se_cb_prog = bc->bc_cb_program;
2775 session->se_cb_sec = bc->bc_cb_sec;
2776 spin_unlock(&nn->client_lock);
2778 nfsd4_probe_callback(session->se_client);
2783 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2784 struct nfsd4_compound_state *cstate,
2785 struct nfsd4_bind_conn_to_session *bcts)
2788 struct nfsd4_conn *conn;
2789 struct nfsd4_session *session;
2790 struct net *net = SVC_NET(rqstp);
2791 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2793 if (!nfsd4_last_compound_op(rqstp))
2794 return nfserr_not_only_op;
2795 spin_lock(&nn->client_lock);
2796 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
2797 spin_unlock(&nn->client_lock);
2799 goto out_no_session;
2800 status = nfserr_wrong_cred;
2801 if (!nfsd4_mach_creds_match(session->se_client, rqstp))
2803 status = nfsd4_map_bcts_dir(&bcts->dir);
2806 conn = alloc_conn(rqstp, bcts->dir);
2807 status = nfserr_jukebox;
2810 nfsd4_init_conn(rqstp, conn, session);
2813 nfsd4_put_session(session);
2818 static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2822 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2826 nfsd4_destroy_session(struct svc_rqst *r,
2827 struct nfsd4_compound_state *cstate,
2828 struct nfsd4_destroy_session *sessionid)
2830 struct nfsd4_session *ses;
2832 int ref_held_by_me = 0;
2833 struct net *net = SVC_NET(r);
2834 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2836 status = nfserr_not_only_op;
2837 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
2838 if (!nfsd4_last_compound_op(r))
2842 dump_sessionid(__func__, &sessionid->sessionid);
2843 spin_lock(&nn->client_lock);
2844 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
2846 goto out_client_lock;
2847 status = nfserr_wrong_cred;
2848 if (!nfsd4_mach_creds_match(ses->se_client, r))
2849 goto out_put_session;
2850 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
2852 goto out_put_session;
2853 unhash_session(ses);
2854 spin_unlock(&nn->client_lock);
2856 nfsd4_probe_callback_sync(ses->se_client);
2858 spin_lock(&nn->client_lock);
2861 nfsd4_put_session_locked(ses);
2863 spin_unlock(&nn->client_lock);
2868 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
2870 struct nfsd4_conn *c;
2872 list_for_each_entry(c, &s->se_conns, cn_persession) {
2873 if (c->cn_xprt == xpt) {
2880 static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
2882 struct nfs4_client *clp = ses->se_client;
2883 struct nfsd4_conn *c;
2884 __be32 status = nfs_ok;
2887 spin_lock(&clp->cl_lock);
2888 c = __nfsd4_find_conn(new->cn_xprt, ses);
2891 status = nfserr_conn_not_bound_to_session;
2892 if (clp->cl_mach_cred)
2894 __nfsd4_hash_conn(new, ses);
2895 spin_unlock(&clp->cl_lock);
2896 ret = nfsd4_register_conn(new);
2898 /* oops; xprt is already down: */
2899 nfsd4_conn_lost(&new->cn_xpt_user);
2902 spin_unlock(&clp->cl_lock);
2907 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2909 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2911 return args->opcnt > session->se_fchannel.maxops;
2914 static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2915 struct nfsd4_session *session)
2917 struct xdr_buf *xb = &rqstp->rq_arg;
2919 return xb->len > session->se_fchannel.maxreq_sz;
2923 nfsd4_sequence(struct svc_rqst *rqstp,
2924 struct nfsd4_compound_state *cstate,
2925 struct nfsd4_sequence *seq)
2927 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2928 struct xdr_stream *xdr = &resp->xdr;
2929 struct nfsd4_session *session;
2930 struct nfs4_client *clp;
2931 struct nfsd4_slot *slot;
2932 struct nfsd4_conn *conn;
2935 struct net *net = SVC_NET(rqstp);
2936 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2938 if (resp->opcnt != 1)
2939 return nfserr_sequence_pos;
2942 * Will be either used or freed by nfsd4_sequence_check_conn
2945 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2947 return nfserr_jukebox;
2949 spin_lock(&nn->client_lock);
2950 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
2952 goto out_no_session;
2953 clp = session->se_client;
2955 status = nfserr_too_many_ops;
2956 if (nfsd4_session_too_many_ops(rqstp, session))
2957 goto out_put_session;
2959 status = nfserr_req_too_big;
2960 if (nfsd4_request_too_big(rqstp, session))
2961 goto out_put_session;
2963 status = nfserr_badslot;
2964 if (seq->slotid >= session->se_fchannel.maxreqs)
2965 goto out_put_session;
2967 slot = session->se_slots[seq->slotid];
2968 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2970 /* We do not negotiate the number of slots yet, so set the
2971 * maxslots to the session maxreqs which is used to encode
2972 * sr_highest_slotid and the sr_target_slot id to maxslots */
2973 seq->maxslots = session->se_fchannel.maxreqs;
2975 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2976 slot->sl_flags & NFSD4_SLOT_INUSE);
2977 if (status == nfserr_replay_cache) {
2978 status = nfserr_seq_misordered;
2979 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
2980 goto out_put_session;
2981 cstate->slot = slot;
2982 cstate->session = session;
2984 /* Return the cached reply status and set cstate->status
2985 * for nfsd4_proc_compound processing */
2986 status = nfsd4_replay_cache_entry(resp, seq);
2987 cstate->status = nfserr_replay_cache;
2991 goto out_put_session;
2993 status = nfsd4_sequence_check_conn(conn, session);
2996 goto out_put_session;
2998 buflen = (seq->cachethis) ?
2999 session->se_fchannel.maxresp_cached :
3000 session->se_fchannel.maxresp_sz;
3001 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
3003 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
3004 goto out_put_session;
3005 svc_reserve(rqstp, buflen);
3008 /* Success! bump slot seqid */
3009 slot->sl_seqid = seq->seqid;
3010 slot->sl_flags |= NFSD4_SLOT_INUSE;
3012 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
3014 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
3016 cstate->slot = slot;
3017 cstate->session = session;
3021 switch (clp->cl_cb_state) {
3023 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
3025 case NFSD4_CB_FAULT:
3026 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
3029 seq->status_flags = 0;
3031 if (!list_empty(&clp->cl_revoked))
3032 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
3036 spin_unlock(&nn->client_lock);
3039 nfsd4_put_session_locked(session);
3040 goto out_no_session;
3044 nfsd4_sequence_done(struct nfsd4_compoundres *resp)
3046 struct nfsd4_compound_state *cs = &resp->cstate;
3048 if (nfsd4_has_session(cs)) {
3049 if (cs->status != nfserr_replay_cache) {
3050 nfsd4_store_cache_entry(resp);
3051 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
3053 /* Drop session reference that was taken in nfsd4_sequence() */
3054 nfsd4_put_session(cs->session);
3056 put_client_renew(cs->clp);
3060 nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
3062 struct nfs4_client *conf, *unconf;
3063 struct nfs4_client *clp = NULL;
3065 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3067 spin_lock(&nn->client_lock);
3068 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
3069 conf = find_confirmed_client(&dc->clientid, true, nn);
3070 WARN_ON_ONCE(conf && unconf);
3073 if (client_has_state(conf)) {
3074 status = nfserr_clientid_busy;
3077 status = mark_client_expired_locked(conf);
3084 status = nfserr_stale_clientid;
3087 if (!nfsd4_mach_creds_match(clp, rqstp)) {
3089 status = nfserr_wrong_cred;
3092 unhash_client_locked(clp);
3094 spin_unlock(&nn->client_lock);
3101 nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
3105 if (rc->rca_one_fs) {
3106 if (!cstate->current_fh.fh_dentry)
3107 return nfserr_nofilehandle;
3109 * We don't take advantage of the rca_one_fs case.
3110 * That's OK, it's optional, we can safely ignore it.
3115 status = nfserr_complete_already;
3116 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
3117 &cstate->session->se_client->cl_flags))
3120 status = nfserr_stale_clientid;
3121 if (is_client_expired(cstate->session->se_client))
3123 * The following error isn't really legal.
3124 * But we only get here if the client just explicitly
3125 * destroyed the client. Surely it no longer cares what
3126 * error it gets back on an operation for the dead
3132 nfsd4_client_record_create(cstate->session->se_client);
3138 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3139 struct nfsd4_setclientid *setclid)
3141 struct xdr_netobj clname = setclid->se_name;
3142 nfs4_verifier clverifier = setclid->se_verf;
3143 struct nfs4_client *conf, *new;
3144 struct nfs4_client *unconf = NULL;
3146 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3148 new = create_client(clname, rqstp, &clverifier);
3150 return nfserr_jukebox;
3151 /* Cases below refer to rfc 3530 section 14.2.33: */
3152 spin_lock(&nn->client_lock);
3153 conf = find_confirmed_client_by_name(&clname, nn);
3154 if (conf && client_has_state(conf)) {
3156 status = nfserr_clid_inuse;
3157 if (clp_used_exchangeid(conf))
3159 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
3160 char addr_str[INET6_ADDRSTRLEN];
3161 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
3163 dprintk("NFSD: setclientid: string in use by client "
3164 "at %s\n", addr_str);
3168 unconf = find_unconfirmed_client_by_name(&clname, nn);
3170 unhash_client_locked(unconf);
3171 if (conf && same_verf(&conf->cl_verifier, &clverifier)) {
3172 /* case 1: probable callback update */
3173 copy_clid(new, conf);
3174 gen_confirm(new, nn);
3175 } else /* case 4 (new client) or cases 2, 3 (client reboot): */
3177 new->cl_minorversion = 0;
3178 gen_callback(new, setclid, rqstp);
3179 add_to_unconfirmed(new);
3180 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
3181 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
3182 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
3186 spin_unlock(&nn->client_lock);
3190 expire_client(unconf);
3196 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
3197 struct nfsd4_compound_state *cstate,
3198 struct nfsd4_setclientid_confirm *setclientid_confirm)
3200 struct nfs4_client *conf, *unconf;
3201 struct nfs4_client *old = NULL;
3202 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
3203 clientid_t * clid = &setclientid_confirm->sc_clientid;
3205 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3207 if (STALE_CLIENTID(clid, nn))
3208 return nfserr_stale_clientid;
3210 spin_lock(&nn->client_lock);
3211 conf = find_confirmed_client(clid, false, nn);
3212 unconf = find_unconfirmed_client(clid, false, nn);
3214 * We try hard to give out unique clientid's, so if we get an
3215 * attempt to confirm the same clientid with a different cred,
3216 * the client may be buggy; this should never happen.
3218 * Nevertheless, RFC 7530 recommends INUSE for this case:
3220 status = nfserr_clid_inuse;
3221 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3223 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3225 /* cases below refer to rfc 3530 section 14.2.34: */
3226 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
3227 if (conf && !unconf) /* case 2: probable retransmit */
3229 else /* case 4: client hasn't noticed we rebooted yet? */
3230 status = nfserr_stale_clientid;
3234 if (conf) { /* case 1: callback update */
3236 unhash_client_locked(old);
3237 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
3238 } else { /* case 3: normal case; new or rebooted client */
3239 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3241 status = nfserr_clid_inuse;
3242 if (client_has_state(old)
3243 && !same_creds(&unconf->cl_cred,
3246 status = mark_client_expired_locked(old);
3252 move_to_confirmed(unconf);
3255 get_client_locked(conf);
3256 spin_unlock(&nn->client_lock);
3257 nfsd4_probe_callback(conf);
3258 spin_lock(&nn->client_lock);
3259 put_client_renew_locked(conf);
3261 spin_unlock(&nn->client_lock);
3267 static struct nfs4_file *nfsd4_alloc_file(void)
3269 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3272 /* OPEN Share state helper functions */
3273 static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
3274 struct nfs4_file *fp)
3276 lockdep_assert_held(&state_lock);
3278 atomic_set(&fp->fi_ref, 1);
3279 spin_lock_init(&fp->fi_lock);
3280 INIT_LIST_HEAD(&fp->fi_stateids);
3281 INIT_LIST_HEAD(&fp->fi_delegations);
3282 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
3283 fh_copy_shallow(&fp->fi_fhandle, fh);
3284 fp->fi_deleg_file = NULL;
3285 fp->fi_had_conflict = false;
3286 fp->fi_share_deny = 0;
3287 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3288 memset(fp->fi_access, 0, sizeof(fp->fi_access));
3289 #ifdef CONFIG_NFSD_PNFS
3290 INIT_LIST_HEAD(&fp->fi_lo_states);
3291 atomic_set(&fp->fi_lo_recalls, 0);
3293 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
3297 nfsd4_free_slabs(void)
3299 kmem_cache_destroy(odstate_slab);
3300 kmem_cache_destroy(openowner_slab);
3301 kmem_cache_destroy(lockowner_slab);
3302 kmem_cache_destroy(file_slab);
3303 kmem_cache_destroy(stateid_slab);
3304 kmem_cache_destroy(deleg_slab);
3308 nfsd4_init_slabs(void)
3310 openowner_slab = kmem_cache_create("nfsd4_openowners",
3311 sizeof(struct nfs4_openowner), 0, 0, NULL);
3312 if (openowner_slab == NULL)
3314 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
3315 sizeof(struct nfs4_lockowner), 0, 0, NULL);
3316 if (lockowner_slab == NULL)
3317 goto out_free_openowner_slab;
3318 file_slab = kmem_cache_create("nfsd4_files",
3319 sizeof(struct nfs4_file), 0, 0, NULL);
3320 if (file_slab == NULL)
3321 goto out_free_lockowner_slab;
3322 stateid_slab = kmem_cache_create("nfsd4_stateids",
3323 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
3324 if (stateid_slab == NULL)
3325 goto out_free_file_slab;
3326 deleg_slab = kmem_cache_create("nfsd4_delegations",
3327 sizeof(struct nfs4_delegation), 0, 0, NULL);
3328 if (deleg_slab == NULL)
3329 goto out_free_stateid_slab;
3330 odstate_slab = kmem_cache_create("nfsd4_odstate",
3331 sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
3332 if (odstate_slab == NULL)
3333 goto out_free_deleg_slab;
3336 out_free_deleg_slab:
3337 kmem_cache_destroy(deleg_slab);
3338 out_free_stateid_slab:
3339 kmem_cache_destroy(stateid_slab);
3341 kmem_cache_destroy(file_slab);
3342 out_free_lockowner_slab:
3343 kmem_cache_destroy(lockowner_slab);
3344 out_free_openowner_slab:
3345 kmem_cache_destroy(openowner_slab);
3347 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3351 static void init_nfs4_replay(struct nfs4_replay *rp)
3353 rp->rp_status = nfserr_serverfault;
3355 rp->rp_buf = rp->rp_ibuf;
3356 mutex_init(&rp->rp_mutex);
3359 static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3360 struct nfs4_stateowner *so)
3362 if (!nfsd4_has_session(cstate)) {
3363 mutex_lock(&so->so_replay.rp_mutex);
3364 cstate->replay_owner = nfs4_get_stateowner(so);
3368 void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3370 struct nfs4_stateowner *so = cstate->replay_owner;
3373 cstate->replay_owner = NULL;
3374 mutex_unlock(&so->so_replay.rp_mutex);
3375 nfs4_put_stateowner(so);
3379 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
3381 struct nfs4_stateowner *sop;
3383 sop = kmem_cache_alloc(slab, GFP_KERNEL);
3387 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3388 if (!sop->so_owner.data) {
3389 kmem_cache_free(slab, sop);
3392 sop->so_owner.len = owner->len;
3394 INIT_LIST_HEAD(&sop->so_stateids);
3395 sop->so_client = clp;
3396 init_nfs4_replay(&sop->so_replay);
3397 atomic_set(&sop->so_count, 1);
3401 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
3403 lockdep_assert_held(&clp->cl_lock);
3405 list_add(&oo->oo_owner.so_strhash,
3406 &clp->cl_ownerstr_hashtbl[strhashval]);
3407 list_add(&oo->oo_perclient, &clp->cl_openowners);
3410 static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3412 unhash_openowner_locked(openowner(so));
3415 static void nfs4_free_openowner(struct nfs4_stateowner *so)
3417 struct nfs4_openowner *oo = openowner(so);
3419 kmem_cache_free(openowner_slab, oo);
3422 static const struct nfs4_stateowner_operations openowner_ops = {
3423 .so_unhash = nfs4_unhash_openowner,
3424 .so_free = nfs4_free_openowner,
3427 static struct nfs4_ol_stateid *
3428 nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
3430 struct nfs4_ol_stateid *local, *ret = NULL;
3431 struct nfs4_openowner *oo = open->op_openowner;
3433 lockdep_assert_held(&fp->fi_lock);
3435 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
3436 /* ignore lock owners */
3437 if (local->st_stateowner->so_is_open_owner == 0)
3439 if (local->st_stateowner == &oo->oo_owner) {
3441 atomic_inc(&ret->st_stid.sc_count);
3448 static struct nfs4_openowner *
3449 alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
3450 struct nfsd4_compound_state *cstate)
3452 struct nfs4_client *clp = cstate->clp;
3453 struct nfs4_openowner *oo, *ret;
3455 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3458 oo->oo_owner.so_ops = &openowner_ops;
3459 oo->oo_owner.so_is_open_owner = 1;
3460 oo->oo_owner.so_seqid = open->op_seqid;
3462 if (nfsd4_has_session(cstate))
3463 oo->oo_flags |= NFS4_OO_CONFIRMED;
3465 oo->oo_last_closed_stid = NULL;
3466 INIT_LIST_HEAD(&oo->oo_close_lru);
3467 spin_lock(&clp->cl_lock);
3468 ret = find_openstateowner_str_locked(strhashval, open, clp);
3470 hash_openowner(oo, clp, strhashval);
3473 nfs4_free_stateowner(&oo->oo_owner);
3475 spin_unlock(&clp->cl_lock);
3479 static struct nfs4_ol_stateid *
3480 init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
3483 struct nfs4_openowner *oo = open->op_openowner;
3484 struct nfs4_ol_stateid *retstp = NULL;
3485 struct nfs4_ol_stateid *stp;
3488 /* We are moving these outside of the spinlocks to avoid the warnings */
3489 mutex_init(&stp->st_mutex);
3490 mutex_lock(&stp->st_mutex);
3492 spin_lock(&oo->oo_owner.so_client->cl_lock);
3493 spin_lock(&fp->fi_lock);
3495 retstp = nfsd4_find_existing_open(fp, open);
3499 open->op_stp = NULL;
3500 atomic_inc(&stp->st_stid.sc_count);
3501 stp->st_stid.sc_type = NFS4_OPEN_STID;
3502 INIT_LIST_HEAD(&stp->st_locks);
3503 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
3505 stp->st_stid.sc_file = fp;
3506 stp->st_access_bmap = 0;
3507 stp->st_deny_bmap = 0;
3508 stp->st_openstp = NULL;
3509 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
3510 list_add(&stp->st_perfile, &fp->fi_stateids);
3513 spin_unlock(&fp->fi_lock);
3514 spin_unlock(&oo->oo_owner.so_client->cl_lock);
3516 mutex_lock(&retstp->st_mutex);
3517 /* To keep mutex tracking happy */
3518 mutex_unlock(&stp->st_mutex);
3525 * In the 4.0 case we need to keep the owners around a little while to handle
3526 * CLOSE replay. We still do need to release any file access that is held by
3527 * them before returning however.
3530 move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
3532 struct nfs4_ol_stateid *last;
3533 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3534 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3537 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
3540 * We know that we hold one reference via nfsd4_close, and another
3541 * "persistent" reference for the client. If the refcount is higher
3542 * than 2, then there are still calls in progress that are using this
3543 * stateid. We can't put the sc_file reference until they are finished.
3544 * Wait for the refcount to drop to 2. Since it has been unhashed,
3545 * there should be no danger of the refcount going back up again at
3548 wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3550 release_all_access(s);
3551 if (s->st_stid.sc_file) {
3552 put_nfs4_file(s->st_stid.sc_file);
3553 s->st_stid.sc_file = NULL;
3556 spin_lock(&nn->client_lock);
3557 last = oo->oo_last_closed_stid;
3558 oo->oo_last_closed_stid = s;
3559 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
3560 oo->oo_time = get_seconds();
3561 spin_unlock(&nn->client_lock);
3563 nfs4_put_stid(&last->st_stid);
3566 /* search file_hashtbl[] for file */
3567 static struct nfs4_file *
3568 find_file_locked(struct knfsd_fh *fh, unsigned int hashval)
3570 struct nfs4_file *fp;
3572 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash) {
3573 if (fh_match(&fp->fi_fhandle, fh)) {
3574 if (atomic_inc_not_zero(&fp->fi_ref))
3582 find_file(struct knfsd_fh *fh)
3584 struct nfs4_file *fp;
3585 unsigned int hashval = file_hashval(fh);
3588 fp = find_file_locked(fh, hashval);
3593 static struct nfs4_file *
3594 find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
3596 struct nfs4_file *fp;
3597 unsigned int hashval = file_hashval(fh);
3600 fp = find_file_locked(fh, hashval);
3605 spin_lock(&state_lock);
3606 fp = find_file_locked(fh, hashval);
3607 if (likely(fp == NULL)) {
3608 nfsd4_init_file(fh, hashval, new);
3611 spin_unlock(&state_lock);
3617 * Called to check deny when READ with all zero stateid or
3618 * WRITE with all zero or all one stateid
3621 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3623 struct nfs4_file *fp;
3624 __be32 ret = nfs_ok;
3626 fp = find_file(¤t_fh->fh_handle);
3629 /* Check for conflicting share reservations */
3630 spin_lock(&fp->fi_lock);
3631 if (fp->fi_share_deny & deny_type)
3632 ret = nfserr_locked;
3633 spin_unlock(&fp->fi_lock);
3638 static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
3640 struct nfs4_delegation *dp = cb_to_delegation(cb);
3641 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3644 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
3647 * We can't do this in nfsd_break_deleg_cb because it is
3648 * already holding inode->i_lock.
3650 * If the dl_time != 0, then we know that it has already been
3651 * queued for a lease break. Don't queue it again.
3653 spin_lock(&state_lock);
3654 if (dp->dl_time == 0) {
3655 dp->dl_time = get_seconds();
3656 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
3658 spin_unlock(&state_lock);
3661 static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
3662 struct rpc_task *task)
3664 struct nfs4_delegation *dp = cb_to_delegation(cb);
3666 if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID)
3669 switch (task->tk_status) {
3673 case -NFS4ERR_BAD_STATEID:
3675 * Race: client probably got cb_recall before open reply
3676 * granting delegation.
3678 if (dp->dl_retries--) {
3679 rpc_delay(task, 2 * HZ);
3688 static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
3690 struct nfs4_delegation *dp = cb_to_delegation(cb);
3692 nfs4_put_stid(&dp->dl_stid);
3695 static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
3696 .prepare = nfsd4_cb_recall_prepare,
3697 .done = nfsd4_cb_recall_done,
3698 .release = nfsd4_cb_recall_release,
3701 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3704 * We're assuming the state code never drops its reference
3705 * without first removing the lease. Since we're in this lease
3706 * callback (and since the lease code is serialized by the kernel
3707 * lock) we know the server hasn't removed the lease yet, we know
3708 * it's safe to take a reference.
3710 atomic_inc(&dp->dl_stid.sc_count);
3711 nfsd4_run_cb(&dp->dl_recall);
3714 /* Called from break_lease() with i_lock held. */
3716 nfsd_break_deleg_cb(struct file_lock *fl)
3719 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3720 struct nfs4_delegation *dp;
3723 WARN(1, "(%p)->fl_owner NULL\n", fl);
3726 if (fp->fi_had_conflict) {
3727 WARN(1, "duplicate break on %p\n", fp);
3731 * We don't want the locks code to timeout the lease for us;
3732 * we'll remove it ourself if a delegation isn't returned
3735 fl->fl_break_time = 0;
3737 spin_lock(&fp->fi_lock);
3738 fp->fi_had_conflict = true;
3740 * If there are no delegations on the list, then return true
3741 * so that the lease code will go ahead and delete it.
3743 if (list_empty(&fp->fi_delegations))
3746 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3747 nfsd_break_one_deleg(dp);
3748 spin_unlock(&fp->fi_lock);
3753 nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
3754 struct list_head *dispose)
3757 return lease_modify(onlist, arg, dispose);
3762 static const struct lock_manager_operations nfsd_lease_mng_ops = {
3763 .lm_break = nfsd_break_deleg_cb,
3764 .lm_change = nfsd_change_deleg_cb,
3767 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3769 if (nfsd4_has_session(cstate))
3771 if (seqid == so->so_seqid - 1)
3772 return nfserr_replay_me;
3773 if (seqid == so->so_seqid)
3775 return nfserr_bad_seqid;
3778 static __be32 lookup_clientid(clientid_t *clid,
3779 struct nfsd4_compound_state *cstate,
3780 struct nfsd_net *nn)
3782 struct nfs4_client *found;
3785 found = cstate->clp;
3786 if (!same_clid(&found->cl_clientid, clid))
3787 return nfserr_stale_clientid;
3791 if (STALE_CLIENTID(clid, nn))
3792 return nfserr_stale_clientid;
3795 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3796 * cached already then we know this is for is for v4.0 and "sessions"
3799 WARN_ON_ONCE(cstate->session);
3800 spin_lock(&nn->client_lock);
3801 found = find_confirmed_client(clid, false, nn);
3803 spin_unlock(&nn->client_lock);
3804 return nfserr_expired;
3806 atomic_inc(&found->cl_refcount);
3807 spin_unlock(&nn->client_lock);
3809 /* Cache the nfs4_client in cstate! */
3810 cstate->clp = found;
3815 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
3816 struct nfsd4_open *open, struct nfsd_net *nn)
3818 clientid_t *clientid = &open->op_clientid;
3819 struct nfs4_client *clp = NULL;
3820 unsigned int strhashval;
3821 struct nfs4_openowner *oo = NULL;
3824 if (STALE_CLIENTID(&open->op_clientid, nn))
3825 return nfserr_stale_clientid;
3827 * In case we need it later, after we've already created the
3828 * file and don't want to risk a further failure:
3830 open->op_file = nfsd4_alloc_file();
3831 if (open->op_file == NULL)
3832 return nfserr_jukebox;
3834 status = lookup_clientid(clientid, cstate, nn);
3839 strhashval = ownerstr_hashval(&open->op_owner);
3840 oo = find_openstateowner_str(strhashval, open, clp);
3841 open->op_openowner = oo;
3845 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
3846 /* Replace unconfirmed owners without checking for replay. */
3847 release_openowner(oo);
3848 open->op_openowner = NULL;
3851 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3856 oo = alloc_init_open_stateowner(strhashval, open, cstate);
3858 return nfserr_jukebox;
3859 open->op_openowner = oo;
3861 open->op_stp = nfs4_alloc_open_stateid(clp);
3863 return nfserr_jukebox;
3865 if (nfsd4_has_session(cstate) &&
3866 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
3867 open->op_odstate = alloc_clnt_odstate(clp);
3868 if (!open->op_odstate)
3869 return nfserr_jukebox;
3875 static inline __be32
3876 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3878 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3879 return nfserr_openmode;
3884 static int share_access_to_flags(u32 share_access)
3886 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3889 static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
3891 struct nfs4_stid *ret;
3893 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
3896 return delegstateid(ret);
3899 static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3901 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3902 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3906 nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
3907 struct nfs4_delegation **dp)
3910 __be32 status = nfserr_bad_stateid;
3911 struct nfs4_delegation *deleg;
3913 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3916 flags = share_access_to_flags(open->op_share_access);
3917 status = nfs4_check_delegmode(deleg, flags);
3919 nfs4_put_stid(&deleg->dl_stid);
3924 if (!nfsd4_is_deleg_cur(open))
3928 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
3932 static inline int nfs4_access_to_access(u32 nfs4_access)
3936 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3937 flags |= NFSD_MAY_READ;
3938 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3939 flags |= NFSD_MAY_WRITE;
3943 static inline __be32
3944 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3945 struct nfsd4_open *open)
3947 struct iattr iattr = {
3948 .ia_valid = ATTR_SIZE,
3951 if (!open->op_truncate)
3953 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
3954 return nfserr_inval;
3955 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3958 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
3959 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3960 struct nfsd4_open *open)
3962 struct file *filp = NULL;
3964 int oflag = nfs4_access_to_omode(open->op_share_access);
3965 int access = nfs4_access_to_access(open->op_share_access);
3966 unsigned char old_access_bmap, old_deny_bmap;
3968 spin_lock(&fp->fi_lock);
3971 * Are we trying to set a deny mode that would conflict with
3974 status = nfs4_file_check_deny(fp, open->op_share_deny);
3975 if (status != nfs_ok) {
3976 spin_unlock(&fp->fi_lock);
3980 /* set access to the file */
3981 status = nfs4_file_get_access(fp, open->op_share_access);
3982 if (status != nfs_ok) {
3983 spin_unlock(&fp->fi_lock);
3987 /* Set access bits in stateid */
3988 old_access_bmap = stp->st_access_bmap;
3989 set_access(open->op_share_access, stp);
3991 /* Set new deny mask */
3992 old_deny_bmap = stp->st_deny_bmap;
3993 set_deny(open->op_share_deny, stp);
3994 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3996 if (!fp->fi_fds[oflag]) {
3997 spin_unlock(&fp->fi_lock);
3998 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
4000 goto out_put_access;
4001 spin_lock(&fp->fi_lock);
4002 if (!fp->fi_fds[oflag]) {
4003 fp->fi_fds[oflag] = filp;
4007 spin_unlock(&fp->fi_lock);
4011 status = nfsd4_truncate(rqstp, cur_fh, open);
4013 goto out_put_access;
4017 stp->st_access_bmap = old_access_bmap;
4018 nfs4_file_put_access(fp, open->op_share_access);
4019 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
4024 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)
4027 unsigned char old_deny_bmap = stp->st_deny_bmap;
4029 if (!test_access(open->op_share_access, stp))
4030 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
4032 /* test and set deny mode */
4033 spin_lock(&fp->fi_lock);
4034 status = nfs4_file_check_deny(fp, open->op_share_deny);
4035 if (status == nfs_ok) {
4036 set_deny(open->op_share_deny, stp);
4037 fp->fi_share_deny |=
4038 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4040 spin_unlock(&fp->fi_lock);
4042 if (status != nfs_ok)
4045 status = nfsd4_truncate(rqstp, cur_fh, open);
4046 if (status != nfs_ok)
4047 reset_union_bmap_deny(old_deny_bmap, stp);
4051 /* Should we give out recallable state?: */
4052 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
4054 if (clp->cl_cb_state == NFSD4_CB_UP)
4057 * In the sessions case, since we don't have to establish a
4058 * separate connection for callbacks, we assume it's OK
4059 * until we hear otherwise:
4061 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
4064 static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
4066 struct file_lock *fl;
4068 fl = locks_alloc_lock();
4071 fl->fl_lmops = &nfsd_lease_mng_ops;
4072 fl->fl_flags = FL_DELEG;
4073 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
4074 fl->fl_end = OFFSET_MAX;
4075 fl->fl_owner = (fl_owner_t)fp;
4076 fl->fl_pid = current->tgid;
4081 * nfs4_setlease - Obtain a delegation by requesting lease from vfs layer
4082 * @dp: a pointer to the nfs4_delegation we're adding.
4085 * On success: Return code will be 0 on success.
4087 * On error: -EAGAIN if there was an existing delegation.
4088 * nonzero if there is an error in other cases.
4092 static int nfs4_setlease(struct nfs4_delegation *dp)
4094 struct nfs4_file *fp = dp->dl_stid.sc_file;
4095 struct file_lock *fl;
4099 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
4102 filp = find_readable_file(fp);
4104 /* We should always have a readable file here */
4106 locks_free_lock(fl);
4110 status = vfs_setlease(filp, fl->fl_type, &fl, NULL);
4112 locks_free_lock(fl);
4115 spin_lock(&state_lock);
4116 spin_lock(&fp->fi_lock);
4117 /* Did the lease get broken before we took the lock? */
4119 if (fp->fi_had_conflict)
4122 if (fp->fi_deleg_file) {
4123 status = hash_delegation_locked(dp, fp);
4126 fp->fi_deleg_file = filp;
4127 fp->fi_delegees = 0;
4128 status = hash_delegation_locked(dp, fp);
4129 spin_unlock(&fp->fi_lock);
4130 spin_unlock(&state_lock);
4132 /* Should never happen, this is a new fi_deleg_file */
4138 spin_unlock(&fp->fi_lock);
4139 spin_unlock(&state_lock);
4145 static struct nfs4_delegation *
4146 nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
4147 struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate)
4150 struct nfs4_delegation *dp;
4152 if (fp->fi_had_conflict)
4153 return ERR_PTR(-EAGAIN);
4155 spin_lock(&state_lock);
4156 spin_lock(&fp->fi_lock);
4157 status = nfs4_get_existing_delegation(clp, fp);
4158 spin_unlock(&fp->fi_lock);
4159 spin_unlock(&state_lock);
4162 return ERR_PTR(status);
4164 dp = alloc_init_deleg(clp, fh, odstate);
4166 return ERR_PTR(-ENOMEM);
4169 spin_lock(&state_lock);
4170 spin_lock(&fp->fi_lock);
4171 dp->dl_stid.sc_file = fp;
4172 if (!fp->fi_deleg_file) {
4173 spin_unlock(&fp->fi_lock);
4174 spin_unlock(&state_lock);
4175 status = nfs4_setlease(dp);
4178 if (fp->fi_had_conflict) {
4182 status = hash_delegation_locked(dp, fp);
4184 spin_unlock(&fp->fi_lock);
4185 spin_unlock(&state_lock);
4188 put_clnt_odstate(dp->dl_clnt_odstate);
4189 nfs4_put_stid(&dp->dl_stid);
4190 return ERR_PTR(status);
4195 static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
4197 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4198 if (status == -EAGAIN)
4199 open->op_why_no_deleg = WND4_CONTENTION;
4201 open->op_why_no_deleg = WND4_RESOURCE;
4202 switch (open->op_deleg_want) {
4203 case NFS4_SHARE_WANT_READ_DELEG:
4204 case NFS4_SHARE_WANT_WRITE_DELEG:
4205 case NFS4_SHARE_WANT_ANY_DELEG:
4207 case NFS4_SHARE_WANT_CANCEL:
4208 open->op_why_no_deleg = WND4_CANCELLED;
4210 case NFS4_SHARE_WANT_NO_DELEG:
4217 * Attempt to hand out a delegation.
4219 * Note we don't support write delegations, and won't until the vfs has
4220 * proper support for them.
4223 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
4224 struct nfs4_ol_stateid *stp)
4226 struct nfs4_delegation *dp;
4227 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
4228 struct nfs4_client *clp = stp->st_stid.sc_client;
4232 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
4233 open->op_recall = 0;
4234 switch (open->op_claim_type) {
4235 case NFS4_OPEN_CLAIM_PREVIOUS:
4237 open->op_recall = 1;
4238 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
4241 case NFS4_OPEN_CLAIM_NULL:
4242 case NFS4_OPEN_CLAIM_FH:
4244 * Let's not give out any delegations till everyone's
4245 * had the chance to reclaim theirs, *and* until
4246 * NLM locks have all been reclaimed:
4248 if (locks_in_grace(clp->net))
4250 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
4253 * Also, if the file was opened for write or
4254 * create, there's a good chance the client's
4255 * about to write to it, resulting in an
4256 * immediate recall (since we don't support
4257 * write delegations):
4259 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
4261 if (open->op_create == NFS4_OPEN_CREATE)
4267 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate);
4271 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
4273 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
4274 STATEID_VAL(&dp->dl_stid.sc_stateid));
4275 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
4276 nfs4_put_stid(&dp->dl_stid);
4279 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
4280 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
4281 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
4282 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
4283 open->op_recall = 1;
4286 /* 4.1 client asking for a delegation? */
4287 if (open->op_deleg_want)
4288 nfsd4_open_deleg_none_ext(open, status);
4292 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
4293 struct nfs4_delegation *dp)
4295 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
4296 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4297 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4298 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
4299 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
4300 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4301 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4302 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
4304 /* Otherwise the client must be confused wanting a delegation
4305 * it already has, therefore we don't return
4306 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
4311 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
4313 struct nfsd4_compoundres *resp = rqstp->rq_resp;
4314 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
4315 struct nfs4_file *fp = NULL;
4316 struct nfs4_ol_stateid *stp = NULL;
4317 struct nfs4_delegation *dp = NULL;
4321 * Lookup file; if found, lookup stateid and check open request,
4322 * and check for delegations in the process of being recalled.
4323 * If not found, create the nfs4_file struct
4325 fp = find_or_add_file(open->op_file, ¤t_fh->fh_handle);
4326 if (fp != open->op_file) {
4327 status = nfs4_check_deleg(cl, open, &dp);
4330 spin_lock(&fp->fi_lock);
4331 stp = nfsd4_find_existing_open(fp, open);
4332 spin_unlock(&fp->fi_lock);
4334 open->op_file = NULL;
4335 status = nfserr_bad_stateid;
4336 if (nfsd4_is_deleg_cur(open))
4341 * OPEN the file, or upgrade an existing OPEN.
4342 * If truncate fails, the OPEN fails.
4345 /* Stateid was found, this is an OPEN upgrade */
4346 mutex_lock(&stp->st_mutex);
4347 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
4349 mutex_unlock(&stp->st_mutex);
4353 /* stp is returned locked. */
4354 stp = init_open_stateid(fp, open);
4355 /* See if we lost the race to some other thread */
4356 if (stp->st_access_bmap != 0) {
4357 status = nfs4_upgrade_open(rqstp, fp, current_fh,
4360 mutex_unlock(&stp->st_mutex);
4365 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4367 mutex_unlock(&stp->st_mutex);
4368 release_open_stateid(stp);
4372 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
4374 if (stp->st_clnt_odstate == open->op_odstate)
4375 open->op_odstate = NULL;
4378 nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
4379 mutex_unlock(&stp->st_mutex);
4381 if (nfsd4_has_session(&resp->cstate)) {
4382 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4383 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4384 open->op_why_no_deleg = WND4_NOT_WANTED;
4390 * Attempt to hand out a delegation. No error return, because the
4391 * OPEN succeeds even if we fail.
4393 nfs4_open_delegation(current_fh, open, stp);
4397 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
4398 STATEID_VAL(&stp->st_stid.sc_stateid));
4400 /* 4.1 client trying to upgrade/downgrade delegation? */
4401 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
4402 open->op_deleg_want)
4403 nfsd4_deleg_xgrade_none_ext(open, dp);
4407 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
4408 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
4410 * To finish the open response, we just need to set the rflags.
4412 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
4413 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
4414 !nfsd4_has_session(&resp->cstate))
4415 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
4417 nfs4_put_stid(&dp->dl_stid);
4419 nfs4_put_stid(&stp->st_stid);
4424 void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
4425 struct nfsd4_open *open)
4427 if (open->op_openowner) {
4428 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
4430 nfsd4_cstate_assign_replay(cstate, so);
4431 nfs4_put_stateowner(so);
4434 kmem_cache_free(file_slab, open->op_file);
4436 nfs4_put_stid(&open->op_stp->st_stid);
4437 if (open->op_odstate)
4438 kmem_cache_free(odstate_slab, open->op_odstate);
4442 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4445 struct nfs4_client *clp;
4447 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4449 dprintk("process_renew(%08x/%08x): starting\n",
4450 clid->cl_boot, clid->cl_id);
4451 status = lookup_clientid(clid, cstate, nn);
4455 status = nfserr_cb_path_down;
4456 if (!list_empty(&clp->cl_delegations)
4457 && clp->cl_cb_state != NFSD4_CB_UP)
4465 nfsd4_end_grace(struct nfsd_net *nn)
4467 /* do nothing if grace period already ended */
4468 if (nn->grace_ended)
4471 dprintk("NFSD: end of grace period\n");
4472 nn->grace_ended = true;
4474 * If the server goes down again right now, an NFSv4
4475 * client will still be allowed to reclaim after it comes back up,
4476 * even if it hasn't yet had a chance to reclaim state this time.
4479 nfsd4_record_grace_done(nn);
4481 * At this point, NFSv4 clients can still reclaim. But if the
4482 * server crashes, any that have not yet reclaimed will be out
4483 * of luck on the next boot.
4485 * (NFSv4.1+ clients are considered to have reclaimed once they
4486 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
4487 * have reclaimed after their first OPEN.)
4489 locks_end_grace(&nn->nfsd4_manager);
4491 * At this point, and once lockd and/or any other containers
4492 * exit their grace period, further reclaims will fail and
4493 * regular locking can resume.
4498 nfs4_laundromat(struct nfsd_net *nn)
4500 struct nfs4_client *clp;
4501 struct nfs4_openowner *oo;
4502 struct nfs4_delegation *dp;
4503 struct nfs4_ol_stateid *stp;
4504 struct list_head *pos, *next, reaplist;
4505 time_t cutoff = get_seconds() - nn->nfsd4_lease;
4506 time_t t, new_timeo = nn->nfsd4_lease;
4508 dprintk("NFSD: laundromat service - starting\n");
4509 nfsd4_end_grace(nn);
4510 INIT_LIST_HEAD(&reaplist);
4511 spin_lock(&nn->client_lock);
4512 list_for_each_safe(pos, next, &nn->client_lru) {
4513 clp = list_entry(pos, struct nfs4_client, cl_lru);
4514 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4515 t = clp->cl_time - cutoff;
4516 new_timeo = min(new_timeo, t);
4519 if (mark_client_expired_locked(clp)) {
4520 dprintk("NFSD: client in use (clientid %08x)\n",
4521 clp->cl_clientid.cl_id);
4524 list_add(&clp->cl_lru, &reaplist);
4526 spin_unlock(&nn->client_lock);
4527 list_for_each_safe(pos, next, &reaplist) {
4528 clp = list_entry(pos, struct nfs4_client, cl_lru);
4529 dprintk("NFSD: purging unused client (clientid %08x)\n",
4530 clp->cl_clientid.cl_id);
4531 list_del_init(&clp->cl_lru);
4534 spin_lock(&state_lock);
4535 list_for_each_safe(pos, next, &nn->del_recall_lru) {
4536 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4537 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
4538 t = dp->dl_time - cutoff;
4539 new_timeo = min(new_timeo, t);
4542 WARN_ON(!unhash_delegation_locked(dp));
4543 list_add(&dp->dl_recall_lru, &reaplist);
4545 spin_unlock(&state_lock);
4546 while (!list_empty(&reaplist)) {
4547 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4549 list_del_init(&dp->dl_recall_lru);
4550 revoke_delegation(dp);
4553 spin_lock(&nn->client_lock);
4554 while (!list_empty(&nn->close_lru)) {
4555 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
4557 if (time_after((unsigned long)oo->oo_time,
4558 (unsigned long)cutoff)) {
4559 t = oo->oo_time - cutoff;
4560 new_timeo = min(new_timeo, t);
4563 list_del_init(&oo->oo_close_lru);
4564 stp = oo->oo_last_closed_stid;
4565 oo->oo_last_closed_stid = NULL;
4566 spin_unlock(&nn->client_lock);
4567 nfs4_put_stid(&stp->st_stid);
4568 spin_lock(&nn->client_lock);
4570 spin_unlock(&nn->client_lock);
4572 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
4576 static struct workqueue_struct *laundry_wq;
4577 static void laundromat_main(struct work_struct *);
4580 laundromat_main(struct work_struct *laundry)
4583 struct delayed_work *dwork = to_delayed_work(laundry);
4584 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4587 t = nfs4_laundromat(nn);
4588 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
4589 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
4592 static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
4594 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
4595 return nfserr_bad_stateid;
4600 access_permit_read(struct nfs4_ol_stateid *stp)
4602 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4603 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4604 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
4608 access_permit_write(struct nfs4_ol_stateid *stp)
4610 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4611 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
4615 __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
4617 __be32 status = nfserr_openmode;
4619 /* For lock stateid's, we test the parent open, not the lock: */
4620 if (stp->st_openstp)
4621 stp = stp->st_openstp;
4622 if ((flags & WR_STATE) && !access_permit_write(stp))
4624 if ((flags & RD_STATE) && !access_permit_read(stp))
4631 static inline __be32
4632 check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
4634 if (ONE_STATEID(stateid) && (flags & RD_STATE))
4636 else if (opens_in_grace(net)) {
4637 /* Answer in remaining cases depends on existence of
4638 * conflicting state; so we must wait out the grace period. */
4639 return nfserr_grace;
4640 } else if (flags & WR_STATE)
4641 return nfs4_share_conflict(current_fh,
4642 NFS4_SHARE_DENY_WRITE);
4643 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4644 return nfs4_share_conflict(current_fh,
4645 NFS4_SHARE_DENY_READ);
4649 * Allow READ/WRITE during grace period on recovered state only for files
4650 * that are not able to provide mandatory locking.
4653 grace_disallows_io(struct net *net, struct inode *inode)
4655 return opens_in_grace(net) && mandatory_lock(inode);
4658 static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
4661 * When sessions are used the stateid generation number is ignored
4664 if (has_session && in->si_generation == 0)
4667 if (in->si_generation == ref->si_generation)
4670 /* If the client sends us a stateid from the future, it's buggy: */
4671 if (nfsd4_stateid_generation_after(in, ref))
4672 return nfserr_bad_stateid;
4674 * However, we could see a stateid from the past, even from a
4675 * non-buggy client. For example, if the client sends a lock
4676 * while some IO is outstanding, the lock may bump si_generation
4677 * while the IO is still in flight. The client could avoid that
4678 * situation by waiting for responses on all the IO requests,
4679 * but better performance may result in retrying IO that
4680 * receives an old_stateid error if requests are rarely
4681 * reordered in flight:
4683 return nfserr_old_stateid;
4686 static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
4688 if (ols->st_stateowner->so_is_open_owner &&
4689 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
4690 return nfserr_bad_stateid;
4694 static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
4696 struct nfs4_stid *s;
4697 __be32 status = nfserr_bad_stateid;
4699 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4701 /* Client debugging aid. */
4702 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4703 char addr_str[INET6_ADDRSTRLEN];
4704 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4706 pr_warn_ratelimited("NFSD: client %s testing state ID "
4707 "with incorrect client ID\n", addr_str);
4710 spin_lock(&cl->cl_lock);
4711 s = find_stateid_locked(cl, stateid);
4714 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
4717 switch (s->sc_type) {
4718 case NFS4_DELEG_STID:
4721 case NFS4_REVOKED_DELEG_STID:
4722 status = nfserr_deleg_revoked;
4724 case NFS4_OPEN_STID:
4725 case NFS4_LOCK_STID:
4726 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
4729 printk("unknown stateid type %x\n", s->sc_type);
4731 case NFS4_CLOSED_STID:
4732 case NFS4_CLOSED_DELEG_STID:
4733 status = nfserr_bad_stateid;
4736 spin_unlock(&cl->cl_lock);
4741 nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4742 stateid_t *stateid, unsigned char typemask,
4743 struct nfs4_stid **s, struct nfsd_net *nn)
4747 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4748 return nfserr_bad_stateid;
4749 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
4750 if (status == nfserr_stale_clientid) {
4751 if (cstate->session)
4752 return nfserr_bad_stateid;
4753 return nfserr_stale_stateid;
4757 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
4759 return nfserr_bad_stateid;
4763 static struct file *
4764 nfs4_find_file(struct nfs4_stid *s, int flags)
4769 switch (s->sc_type) {
4770 case NFS4_DELEG_STID:
4771 if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
4773 return get_file(s->sc_file->fi_deleg_file);
4774 case NFS4_OPEN_STID:
4775 case NFS4_LOCK_STID:
4776 if (flags & RD_STATE)
4777 return find_readable_file(s->sc_file);
4779 return find_writeable_file(s->sc_file);
4787 nfs4_check_olstateid(struct svc_fh *fhp, struct nfs4_ol_stateid *ols, int flags)
4791 status = nfsd4_check_openowner_confirmed(ols);
4794 return nfs4_check_openmode(ols, flags);
4798 nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
4799 struct file **filpp, bool *tmp_file, int flags)
4801 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
4805 file = nfs4_find_file(s, flags);
4807 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
4808 acc | NFSD_MAY_OWNER_OVERRIDE);
4816 status = nfsd_open(rqstp, fhp, S_IFREG, acc, filpp);
4828 * Checks for stateid operations
4831 nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
4832 struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
4833 stateid_t *stateid, int flags, struct file **filpp, bool *tmp_file)
4835 struct inode *ino = d_inode(fhp->fh_dentry);
4836 struct net *net = SVC_NET(rqstp);
4837 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4838 struct nfs4_stid *s = NULL;
4846 if (grace_disallows_io(net, ino))
4847 return nfserr_grace;
4849 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
4850 status = check_special_stateids(net, fhp, stateid, flags);
4854 status = nfsd4_lookup_stateid(cstate, stateid,
4855 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
4859 status = check_stateid_generation(stateid, &s->sc_stateid,
4860 nfsd4_has_session(cstate));
4864 switch (s->sc_type) {
4865 case NFS4_DELEG_STID:
4866 status = nfs4_check_delegmode(delegstateid(s), flags);
4868 case NFS4_OPEN_STID:
4869 case NFS4_LOCK_STID:
4870 status = nfs4_check_olstateid(fhp, openlockstateid(s), flags);
4873 status = nfserr_bad_stateid;
4878 status = nfs4_check_fh(fhp, s);
4881 if (!status && filpp)
4882 status = nfs4_check_file(rqstp, fhp, s, filpp, tmp_file, flags);
4890 * Test if the stateid is valid
4893 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4894 struct nfsd4_test_stateid *test_stateid)
4896 struct nfsd4_test_stateid_id *stateid;
4897 struct nfs4_client *cl = cstate->session->se_client;
4899 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
4900 stateid->ts_id_status =
4901 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
4907 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4908 struct nfsd4_free_stateid *free_stateid)
4910 stateid_t *stateid = &free_stateid->fr_stateid;
4911 struct nfs4_stid *s;
4912 struct nfs4_delegation *dp;
4913 struct nfs4_ol_stateid *stp;
4914 struct nfs4_client *cl = cstate->session->se_client;
4915 __be32 ret = nfserr_bad_stateid;
4917 spin_lock(&cl->cl_lock);
4918 s = find_stateid_locked(cl, stateid);
4921 switch (s->sc_type) {
4922 case NFS4_DELEG_STID:
4923 ret = nfserr_locks_held;
4925 case NFS4_OPEN_STID:
4926 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4929 ret = nfserr_locks_held;
4931 case NFS4_LOCK_STID:
4932 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4935 stp = openlockstateid(s);
4936 ret = nfserr_locks_held;
4937 if (check_for_locks(stp->st_stid.sc_file,
4938 lockowner(stp->st_stateowner)))
4940 WARN_ON(!unhash_lock_stateid(stp));
4941 spin_unlock(&cl->cl_lock);
4945 case NFS4_REVOKED_DELEG_STID:
4946 dp = delegstateid(s);
4947 list_del_init(&dp->dl_recall_lru);
4948 spin_unlock(&cl->cl_lock);
4952 /* Default falls through and returns nfserr_bad_stateid */
4955 spin_unlock(&cl->cl_lock);
4963 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4964 RD_STATE : WR_STATE;
4967 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
4969 struct svc_fh *current_fh = &cstate->current_fh;
4970 struct nfs4_stateowner *sop = stp->st_stateowner;
4973 status = nfsd4_check_seqid(cstate, sop, seqid);
4976 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4977 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
4979 * "Closed" stateid's exist *only* to return
4980 * nfserr_replay_me from the previous step, and
4981 * revoked delegations are kept only for free_stateid.
4983 return nfserr_bad_stateid;
4984 mutex_lock(&stp->st_mutex);
4985 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4986 if (status == nfs_ok)
4987 status = nfs4_check_fh(current_fh, &stp->st_stid);
4988 if (status != nfs_ok)
4989 mutex_unlock(&stp->st_mutex);
4994 * Checks for sequence id mutating operations.
4997 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4998 stateid_t *stateid, char typemask,
4999 struct nfs4_ol_stateid **stpp,
5000 struct nfsd_net *nn)
5003 struct nfs4_stid *s;
5004 struct nfs4_ol_stateid *stp = NULL;
5006 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
5007 seqid, STATEID_VAL(stateid));
5010 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
5013 stp = openlockstateid(s);
5014 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
5016 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
5020 nfs4_put_stid(&stp->st_stid);
5024 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
5025 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
5028 struct nfs4_openowner *oo;
5029 struct nfs4_ol_stateid *stp;
5031 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
5032 NFS4_OPEN_STID, &stp, nn);
5035 oo = openowner(stp->st_stateowner);
5036 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
5037 mutex_unlock(&stp->st_mutex);
5038 nfs4_put_stid(&stp->st_stid);
5039 return nfserr_bad_stateid;
5046 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5047 struct nfsd4_open_confirm *oc)
5050 struct nfs4_openowner *oo;
5051 struct nfs4_ol_stateid *stp;
5052 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5054 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
5055 cstate->current_fh.fh_dentry);
5057 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
5061 status = nfs4_preprocess_seqid_op(cstate,
5062 oc->oc_seqid, &oc->oc_req_stateid,
5063 NFS4_OPEN_STID, &stp, nn);
5066 oo = openowner(stp->st_stateowner);
5067 status = nfserr_bad_stateid;
5068 if (oo->oo_flags & NFS4_OO_CONFIRMED) {
5069 mutex_unlock(&stp->st_mutex);
5072 oo->oo_flags |= NFS4_OO_CONFIRMED;
5073 nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
5074 mutex_unlock(&stp->st_mutex);
5075 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
5076 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
5078 nfsd4_client_record_create(oo->oo_owner.so_client);
5081 nfs4_put_stid(&stp->st_stid);
5083 nfsd4_bump_seqid(cstate, status);
5087 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
5089 if (!test_access(access, stp))
5091 nfs4_file_put_access(stp->st_stid.sc_file, access);
5092 clear_access(access, stp);
5095 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
5097 switch (to_access) {
5098 case NFS4_SHARE_ACCESS_READ:
5099 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
5100 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
5102 case NFS4_SHARE_ACCESS_WRITE:
5103 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
5104 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
5106 case NFS4_SHARE_ACCESS_BOTH:
5114 nfsd4_open_downgrade(struct svc_rqst *rqstp,
5115 struct nfsd4_compound_state *cstate,
5116 struct nfsd4_open_downgrade *od)
5119 struct nfs4_ol_stateid *stp;
5120 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5122 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
5123 cstate->current_fh.fh_dentry);
5125 /* We don't yet support WANT bits: */
5126 if (od->od_deleg_want)
5127 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
5130 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
5131 &od->od_stateid, &stp, nn);
5134 status = nfserr_inval;
5135 if (!test_access(od->od_share_access, stp)) {
5136 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
5137 stp->st_access_bmap, od->od_share_access);
5140 if (!test_deny(od->od_share_deny, stp)) {
5141 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
5142 stp->st_deny_bmap, od->od_share_deny);
5145 nfs4_stateid_downgrade(stp, od->od_share_access);
5146 reset_union_bmap_deny(od->od_share_deny, stp);
5147 nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
5150 mutex_unlock(&stp->st_mutex);
5151 nfs4_put_stid(&stp->st_stid);
5153 nfsd4_bump_seqid(cstate, status);
5157 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
5159 struct nfs4_client *clp = s->st_stid.sc_client;
5161 LIST_HEAD(reaplist);
5163 s->st_stid.sc_type = NFS4_CLOSED_STID;
5164 spin_lock(&clp->cl_lock);
5165 unhashed = unhash_open_stateid(s, &reaplist);
5167 if (clp->cl_minorversion) {
5169 put_ol_stateid_locked(s, &reaplist);
5170 spin_unlock(&clp->cl_lock);
5171 free_ol_stateid_reaplist(&reaplist);
5173 spin_unlock(&clp->cl_lock);
5174 free_ol_stateid_reaplist(&reaplist);
5176 move_to_close_lru(s, clp->net);
5181 * nfs4_unlock_state() called after encode
5184 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5185 struct nfsd4_close *close)
5188 struct nfs4_ol_stateid *stp;
5189 struct net *net = SVC_NET(rqstp);
5190 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5192 dprintk("NFSD: nfsd4_close on file %pd\n",
5193 cstate->current_fh.fh_dentry);
5195 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
5197 NFS4_OPEN_STID|NFS4_CLOSED_STID,
5199 nfsd4_bump_seqid(cstate, status);
5202 nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
5203 mutex_unlock(&stp->st_mutex);
5205 nfsd4_close_open_stateid(stp);
5207 /* put reference from nfs4_preprocess_seqid_op */
5208 nfs4_put_stid(&stp->st_stid);
5214 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5215 struct nfsd4_delegreturn *dr)
5217 struct nfs4_delegation *dp;
5218 stateid_t *stateid = &dr->dr_stateid;
5219 struct nfs4_stid *s;
5221 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5223 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
5226 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
5229 dp = delegstateid(s);
5230 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
5234 destroy_delegation(dp);
5236 nfs4_put_stid(&dp->dl_stid);
5242 end_offset(u64 start, u64 len)
5247 return end >= start ? end: NFS4_MAX_UINT64;
5250 /* last octet in a range */
5252 last_byte_offset(u64 start, u64 len)
5258 return end > start ? end - 1: NFS4_MAX_UINT64;
5262 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
5263 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
5264 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
5265 * locking, this prevents us from being completely protocol-compliant. The
5266 * real solution to this problem is to start using unsigned file offsets in
5267 * the VFS, but this is a very deep change!
5270 nfs4_transform_lock_offset(struct file_lock *lock)
5272 if (lock->fl_start < 0)
5273 lock->fl_start = OFFSET_MAX;
5274 if (lock->fl_end < 0)
5275 lock->fl_end = OFFSET_MAX;
5279 nfsd4_fl_get_owner(fl_owner_t owner)
5281 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
5283 nfs4_get_stateowner(&lo->lo_owner);
5288 nfsd4_fl_put_owner(fl_owner_t owner)
5290 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
5293 nfs4_put_stateowner(&lo->lo_owner);
5296 static const struct lock_manager_operations nfsd_posix_mng_ops = {
5297 .lm_get_owner = nfsd4_fl_get_owner,
5298 .lm_put_owner = nfsd4_fl_put_owner,
5302 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
5304 struct nfs4_lockowner *lo;
5306 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
5307 lo = (struct nfs4_lockowner *) fl->fl_owner;
5308 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
5309 lo->lo_owner.so_owner.len, GFP_KERNEL);
5310 if (!deny->ld_owner.data)
5311 /* We just don't care that much */
5313 deny->ld_owner.len = lo->lo_owner.so_owner.len;
5314 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
5317 deny->ld_owner.len = 0;
5318 deny->ld_owner.data = NULL;
5319 deny->ld_clientid.cl_boot = 0;
5320 deny->ld_clientid.cl_id = 0;
5322 deny->ld_start = fl->fl_start;
5323 deny->ld_length = NFS4_MAX_UINT64;
5324 if (fl->fl_end != NFS4_MAX_UINT64)
5325 deny->ld_length = fl->fl_end - fl->fl_start + 1;
5326 deny->ld_type = NFS4_READ_LT;
5327 if (fl->fl_type != F_RDLCK)
5328 deny->ld_type = NFS4_WRITE_LT;
5331 static struct nfs4_lockowner *
5332 find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
5334 unsigned int strhashval = ownerstr_hashval(owner);
5335 struct nfs4_stateowner *so;
5337 lockdep_assert_held(&clp->cl_lock);
5339 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
5341 if (so->so_is_open_owner)
5343 if (same_owner_str(so, owner))
5344 return lockowner(nfs4_get_stateowner(so));
5349 static struct nfs4_lockowner *
5350 find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
5352 struct nfs4_lockowner *lo;
5354 spin_lock(&clp->cl_lock);
5355 lo = find_lockowner_str_locked(clp, owner);
5356 spin_unlock(&clp->cl_lock);
5360 static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
5362 unhash_lockowner_locked(lockowner(sop));
5365 static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
5367 struct nfs4_lockowner *lo = lockowner(sop);
5369 kmem_cache_free(lockowner_slab, lo);
5372 static const struct nfs4_stateowner_operations lockowner_ops = {
5373 .so_unhash = nfs4_unhash_lockowner,
5374 .so_free = nfs4_free_lockowner,
5378 * Alloc a lock owner structure.
5379 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
5382 * strhashval = ownerstr_hashval
5384 static struct nfs4_lockowner *
5385 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
5386 struct nfs4_ol_stateid *open_stp,
5387 struct nfsd4_lock *lock)
5389 struct nfs4_lockowner *lo, *ret;
5391 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
5394 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
5395 lo->lo_owner.so_is_open_owner = 0;
5396 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
5397 lo->lo_owner.so_ops = &lockowner_ops;
5398 spin_lock(&clp->cl_lock);
5399 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
5401 list_add(&lo->lo_owner.so_strhash,
5402 &clp->cl_ownerstr_hashtbl[strhashval]);
5405 nfs4_free_stateowner(&lo->lo_owner);
5407 spin_unlock(&clp->cl_lock);
5412 init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
5413 struct nfs4_file *fp, struct inode *inode,
5414 struct nfs4_ol_stateid *open_stp)
5416 struct nfs4_client *clp = lo->lo_owner.so_client;
5418 lockdep_assert_held(&clp->cl_lock);
5420 atomic_inc(&stp->st_stid.sc_count);
5421 stp->st_stid.sc_type = NFS4_LOCK_STID;
5422 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
5424 stp->st_stid.sc_file = fp;
5425 stp->st_stid.sc_free = nfs4_free_lock_stateid;
5426 stp->st_access_bmap = 0;
5427 stp->st_deny_bmap = open_stp->st_deny_bmap;
5428 stp->st_openstp = open_stp;
5429 mutex_init(&stp->st_mutex);
5430 list_add(&stp->st_locks, &open_stp->st_locks);
5431 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
5432 spin_lock(&fp->fi_lock);
5433 list_add(&stp->st_perfile, &fp->fi_stateids);
5434 spin_unlock(&fp->fi_lock);
5437 static struct nfs4_ol_stateid *
5438 find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5440 struct nfs4_ol_stateid *lst;
5441 struct nfs4_client *clp = lo->lo_owner.so_client;
5443 lockdep_assert_held(&clp->cl_lock);
5445 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
5446 if (lst->st_stid.sc_file == fp) {
5447 atomic_inc(&lst->st_stid.sc_count);
5454 static struct nfs4_ol_stateid *
5455 find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5456 struct inode *inode, struct nfs4_ol_stateid *ost,
5459 struct nfs4_stid *ns = NULL;
5460 struct nfs4_ol_stateid *lst;
5461 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5462 struct nfs4_client *clp = oo->oo_owner.so_client;
5464 spin_lock(&clp->cl_lock);
5465 lst = find_lock_stateid(lo, fi);
5467 spin_unlock(&clp->cl_lock);
5468 ns = nfs4_alloc_stid(clp, stateid_slab);
5472 spin_lock(&clp->cl_lock);
5473 lst = find_lock_stateid(lo, fi);
5475 lst = openlockstateid(ns);
5476 init_lock_stateid(lst, lo, fi, inode, ost);
5481 spin_unlock(&clp->cl_lock);
5488 check_lock_length(u64 offset, u64 length)
5490 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
5491 (length > ~offset)));
5494 static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
5496 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
5498 lockdep_assert_held(&fp->fi_lock);
5500 if (test_access(access, lock_stp))
5502 __nfs4_file_get_access(fp, access);
5503 set_access(access, lock_stp);
5507 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5508 struct nfs4_ol_stateid *ost,
5509 struct nfsd4_lock *lock,
5510 struct nfs4_ol_stateid **lst, bool *new)
5513 struct nfs4_file *fi = ost->st_stid.sc_file;
5514 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5515 struct nfs4_client *cl = oo->oo_owner.so_client;
5516 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
5517 struct nfs4_lockowner *lo;
5518 unsigned int strhashval;
5520 lo = find_lockowner_str(cl, &lock->lk_new_owner);
5522 strhashval = ownerstr_hashval(&lock->lk_new_owner);
5523 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5525 return nfserr_jukebox;
5527 /* with an existing lockowner, seqids must be the same */
5528 status = nfserr_bad_seqid;
5529 if (!cstate->minorversion &&
5530 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
5534 *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
5536 status = nfserr_jukebox;
5541 nfs4_put_stateowner(&lo->lo_owner);
5549 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5550 struct nfsd4_lock *lock)
5552 struct nfs4_openowner *open_sop = NULL;
5553 struct nfs4_lockowner *lock_sop = NULL;
5554 struct nfs4_ol_stateid *lock_stp = NULL;
5555 struct nfs4_ol_stateid *open_stp = NULL;
5556 struct nfs4_file *fp;
5557 struct file *filp = NULL;
5558 struct file_lock *file_lock = NULL;
5559 struct file_lock *conflock = NULL;
5564 struct net *net = SVC_NET(rqstp);
5565 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5567 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5568 (long long) lock->lk_offset,
5569 (long long) lock->lk_length);
5571 if (check_lock_length(lock->lk_offset, lock->lk_length))
5572 return nfserr_inval;
5574 if ((status = fh_verify(rqstp, &cstate->current_fh,
5575 S_IFREG, NFSD_MAY_LOCK))) {
5576 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5580 if (lock->lk_is_new) {
5581 if (nfsd4_has_session(cstate))
5582 /* See rfc 5661 18.10.3: given clientid is ignored: */
5583 memcpy(&lock->lk_new_clientid,
5584 &cstate->session->se_client->cl_clientid,
5585 sizeof(clientid_t));
5587 status = nfserr_stale_clientid;
5588 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
5591 /* validate and update open stateid and open seqid */
5592 status = nfs4_preprocess_confirmed_seqid_op(cstate,
5593 lock->lk_new_open_seqid,
5594 &lock->lk_new_open_stateid,
5598 mutex_unlock(&open_stp->st_mutex);
5599 open_sop = openowner(open_stp->st_stateowner);
5600 status = nfserr_bad_stateid;
5601 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
5602 &lock->lk_new_clientid))
5604 status = lookup_or_create_lock_state(cstate, open_stp, lock,
5606 if (status == nfs_ok)
5607 mutex_lock(&lock_stp->st_mutex);
5609 status = nfs4_preprocess_seqid_op(cstate,
5610 lock->lk_old_lock_seqid,
5611 &lock->lk_old_lock_stateid,
5612 NFS4_LOCK_STID, &lock_stp, nn);
5616 lock_sop = lockowner(lock_stp->st_stateowner);
5618 lkflg = setlkflg(lock->lk_type);
5619 status = nfs4_check_openmode(lock_stp, lkflg);
5623 status = nfserr_grace;
5624 if (locks_in_grace(net) && !lock->lk_reclaim)
5626 status = nfserr_no_grace;
5627 if (!locks_in_grace(net) && lock->lk_reclaim)
5630 file_lock = locks_alloc_lock();
5632 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5633 status = nfserr_jukebox;
5637 fp = lock_stp->st_stid.sc_file;
5638 switch (lock->lk_type) {
5641 spin_lock(&fp->fi_lock);
5642 filp = find_readable_file_locked(fp);
5644 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
5645 spin_unlock(&fp->fi_lock);
5646 file_lock->fl_type = F_RDLCK;
5649 case NFS4_WRITEW_LT:
5650 spin_lock(&fp->fi_lock);
5651 filp = find_writeable_file_locked(fp);
5653 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
5654 spin_unlock(&fp->fi_lock);
5655 file_lock->fl_type = F_WRLCK;
5658 status = nfserr_inval;
5662 status = nfserr_openmode;
5666 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
5667 file_lock->fl_pid = current->tgid;
5668 file_lock->fl_file = filp;
5669 file_lock->fl_flags = FL_POSIX;
5670 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5671 file_lock->fl_start = lock->lk_offset;
5672 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5673 nfs4_transform_lock_offset(file_lock);
5675 conflock = locks_alloc_lock();
5677 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5678 status = nfserr_jukebox;
5682 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
5684 case 0: /* success! */
5685 nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
5688 case (EAGAIN): /* conflock holds conflicting lock */
5689 status = nfserr_denied;
5690 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
5691 nfs4_set_lock_denied(conflock, &lock->lk_denied);
5694 status = nfserr_deadlock;
5697 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
5698 status = nfserrno(err);
5705 /* Bump seqid manually if the 4.0 replay owner is openowner */
5706 if (cstate->replay_owner &&
5707 cstate->replay_owner != &lock_sop->lo_owner &&
5708 seqid_mutating_err(ntohl(status)))
5709 lock_sop->lo_owner.so_seqid++;
5711 mutex_unlock(&lock_stp->st_mutex);
5714 * If this is a new, never-before-used stateid, and we are
5715 * returning an error, then just go ahead and release it.
5718 release_lock_stateid(lock_stp);
5720 nfs4_put_stid(&lock_stp->st_stid);
5723 nfs4_put_stid(&open_stp->st_stid);
5724 nfsd4_bump_seqid(cstate, status);
5726 locks_free_lock(file_lock);
5728 locks_free_lock(conflock);
5733 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5734 * so we do a temporary open here just to get an open file to pass to
5735 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5738 static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
5741 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5743 err = nfserrno(vfs_test_lock(file, lock));
5753 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5754 struct nfsd4_lockt *lockt)
5756 struct file_lock *file_lock = NULL;
5757 struct nfs4_lockowner *lo = NULL;
5759 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5761 if (locks_in_grace(SVC_NET(rqstp)))
5762 return nfserr_grace;
5764 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5765 return nfserr_inval;
5767 if (!nfsd4_has_session(cstate)) {
5768 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
5773 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
5776 file_lock = locks_alloc_lock();
5778 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5779 status = nfserr_jukebox;
5783 switch (lockt->lt_type) {
5786 file_lock->fl_type = F_RDLCK;
5789 case NFS4_WRITEW_LT:
5790 file_lock->fl_type = F_WRLCK;
5793 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
5794 status = nfserr_inval;
5798 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
5800 file_lock->fl_owner = (fl_owner_t)lo;
5801 file_lock->fl_pid = current->tgid;
5802 file_lock->fl_flags = FL_POSIX;
5804 file_lock->fl_start = lockt->lt_offset;
5805 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
5807 nfs4_transform_lock_offset(file_lock);
5809 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
5813 if (file_lock->fl_type != F_UNLCK) {
5814 status = nfserr_denied;
5815 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
5819 nfs4_put_stateowner(&lo->lo_owner);
5821 locks_free_lock(file_lock);
5826 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5827 struct nfsd4_locku *locku)
5829 struct nfs4_ol_stateid *stp;
5830 struct file *filp = NULL;
5831 struct file_lock *file_lock = NULL;
5834 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5836 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5837 (long long) locku->lu_offset,
5838 (long long) locku->lu_length);
5840 if (check_lock_length(locku->lu_offset, locku->lu_length))
5841 return nfserr_inval;
5843 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
5844 &locku->lu_stateid, NFS4_LOCK_STID,
5848 filp = find_any_file(stp->st_stid.sc_file);
5850 status = nfserr_lock_range;
5853 file_lock = locks_alloc_lock();
5855 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5856 status = nfserr_jukebox;
5860 file_lock->fl_type = F_UNLCK;
5861 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
5862 file_lock->fl_pid = current->tgid;
5863 file_lock->fl_file = filp;
5864 file_lock->fl_flags = FL_POSIX;
5865 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5866 file_lock->fl_start = locku->lu_offset;
5868 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5870 nfs4_transform_lock_offset(file_lock);
5872 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
5874 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
5877 nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
5881 mutex_unlock(&stp->st_mutex);
5882 nfs4_put_stid(&stp->st_stid);
5884 nfsd4_bump_seqid(cstate, status);
5886 locks_free_lock(file_lock);
5890 status = nfserrno(err);
5896 * true: locks held by lockowner
5897 * false: no locks held by lockowner
5900 check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
5902 struct file_lock *fl;
5904 struct file *filp = find_any_file(fp);
5905 struct inode *inode;
5906 struct file_lock_context *flctx;
5909 /* Any valid lock stateid should have some sort of access */
5914 inode = file_inode(filp);
5915 flctx = inode->i_flctx;
5917 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
5918 spin_lock(&flctx->flc_lock);
5919 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
5920 if (fl->fl_owner == (fl_owner_t)lowner) {
5925 spin_unlock(&flctx->flc_lock);
5932 nfsd4_release_lockowner(struct svc_rqst *rqstp,
5933 struct nfsd4_compound_state *cstate,
5934 struct nfsd4_release_lockowner *rlockowner)
5936 clientid_t *clid = &rlockowner->rl_clientid;
5937 struct nfs4_stateowner *sop;
5938 struct nfs4_lockowner *lo = NULL;
5939 struct nfs4_ol_stateid *stp;
5940 struct xdr_netobj *owner = &rlockowner->rl_owner;
5941 unsigned int hashval = ownerstr_hashval(owner);
5943 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5944 struct nfs4_client *clp;
5945 LIST_HEAD (reaplist);
5947 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5948 clid->cl_boot, clid->cl_id);
5950 status = lookup_clientid(clid, cstate, nn);
5955 /* Find the matching lock stateowner */
5956 spin_lock(&clp->cl_lock);
5957 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
5960 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
5963 /* see if there are still any locks associated with it */
5964 lo = lockowner(sop);
5965 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5966 if (check_for_locks(stp->st_stid.sc_file, lo)) {
5967 status = nfserr_locks_held;
5968 spin_unlock(&clp->cl_lock);
5973 nfs4_get_stateowner(sop);
5977 spin_unlock(&clp->cl_lock);
5981 unhash_lockowner_locked(lo);
5982 while (!list_empty(&lo->lo_owner.so_stateids)) {
5983 stp = list_first_entry(&lo->lo_owner.so_stateids,
5984 struct nfs4_ol_stateid,
5986 WARN_ON(!unhash_lock_stateid(stp));
5987 put_ol_stateid_locked(stp, &reaplist);
5989 spin_unlock(&clp->cl_lock);
5990 free_ol_stateid_reaplist(&reaplist);
5991 nfs4_put_stateowner(&lo->lo_owner);
5996 static inline struct nfs4_client_reclaim *
5999 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
6003 nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
6005 struct nfs4_client_reclaim *crp;
6007 crp = nfsd4_find_reclaim_client(name, nn);
6008 return (crp && crp->cr_clp);
6012 * failure => all reset bets are off, nfserr_no_grace...
6014 struct nfs4_client_reclaim *
6015 nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
6017 unsigned int strhashval;
6018 struct nfs4_client_reclaim *crp;
6020 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
6021 crp = alloc_reclaim();
6023 strhashval = clientstr_hashval(name);
6024 INIT_LIST_HEAD(&crp->cr_strhash);
6025 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
6026 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
6028 nn->reclaim_str_hashtbl_size++;
6034 nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
6036 list_del(&crp->cr_strhash);
6038 nn->reclaim_str_hashtbl_size--;
6042 nfs4_release_reclaim(struct nfsd_net *nn)
6044 struct nfs4_client_reclaim *crp = NULL;
6047 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6048 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
6049 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
6050 struct nfs4_client_reclaim, cr_strhash);
6051 nfs4_remove_reclaim_record(crp, nn);
6054 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
6058 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
6059 struct nfs4_client_reclaim *
6060 nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
6062 unsigned int strhashval;
6063 struct nfs4_client_reclaim *crp = NULL;
6065 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
6067 strhashval = clientstr_hashval(recdir);
6068 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
6069 if (same_name(crp->cr_recdir, recdir)) {
6077 * Called from OPEN. Look for clientid in reclaim list.
6080 nfs4_check_open_reclaim(clientid_t *clid,
6081 struct nfsd4_compound_state *cstate,
6082 struct nfsd_net *nn)
6086 /* find clientid in conf_id_hashtbl */
6087 status = lookup_clientid(clid, cstate, nn);
6089 return nfserr_reclaim_bad;
6091 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
6092 return nfserr_no_grace;
6094 if (nfsd4_client_record_check(cstate->clp))
6095 return nfserr_reclaim_bad;
6100 #ifdef CONFIG_NFSD_FAULT_INJECTION
6102 put_client(struct nfs4_client *clp)
6104 atomic_dec(&clp->cl_refcount);
6107 static struct nfs4_client *
6108 nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
6110 struct nfs4_client *clp;
6111 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6114 if (!nfsd_netns_ready(nn))
6117 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6118 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
6125 nfsd_inject_print_clients(void)
6127 struct nfs4_client *clp;
6129 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6131 char buf[INET6_ADDRSTRLEN];
6133 if (!nfsd_netns_ready(nn))
6136 spin_lock(&nn->client_lock);
6137 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6138 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
6139 pr_info("NFS Client: %s\n", buf);
6142 spin_unlock(&nn->client_lock);
6148 nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
6151 struct nfs4_client *clp;
6152 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6155 if (!nfsd_netns_ready(nn))
6158 spin_lock(&nn->client_lock);
6159 clp = nfsd_find_client(addr, addr_size);
6161 if (mark_client_expired_locked(clp) == nfs_ok)
6166 spin_unlock(&nn->client_lock);
6175 nfsd_inject_forget_clients(u64 max)
6178 struct nfs4_client *clp, *next;
6179 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6181 LIST_HEAD(reaplist);
6183 if (!nfsd_netns_ready(nn))
6186 spin_lock(&nn->client_lock);
6187 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6188 if (mark_client_expired_locked(clp) == nfs_ok) {
6189 list_add(&clp->cl_lru, &reaplist);
6190 if (max != 0 && ++count >= max)
6194 spin_unlock(&nn->client_lock);
6196 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
6202 static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
6205 char buf[INET6_ADDRSTRLEN];
6206 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
6207 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
6211 nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
6212 struct list_head *collect)
6214 struct nfs4_client *clp = lst->st_stid.sc_client;
6215 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6221 lockdep_assert_held(&nn->client_lock);
6222 atomic_inc(&clp->cl_refcount);
6223 list_add(&lst->st_locks, collect);
6226 static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
6227 struct list_head *collect,
6228 bool (*func)(struct nfs4_ol_stateid *))
6230 struct nfs4_openowner *oop;
6231 struct nfs4_ol_stateid *stp, *st_next;
6232 struct nfs4_ol_stateid *lst, *lst_next;
6235 spin_lock(&clp->cl_lock);
6236 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
6237 list_for_each_entry_safe(stp, st_next,
6238 &oop->oo_owner.so_stateids, st_perstateowner) {
6239 list_for_each_entry_safe(lst, lst_next,
6240 &stp->st_locks, st_locks) {
6243 nfsd_inject_add_lock_to_list(lst,
6248 * Despite the fact that these functions deal
6249 * with 64-bit integers for "count", we must
6250 * ensure that it doesn't blow up the
6251 * clp->cl_refcount. Throw a warning if we
6252 * start to approach INT_MAX here.
6254 WARN_ON_ONCE(count == (INT_MAX / 2));
6261 spin_unlock(&clp->cl_lock);
6267 nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
6270 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
6274 nfsd_print_client_locks(struct nfs4_client *clp)
6276 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
6277 nfsd_print_count(clp, count, "locked files");
6282 nfsd_inject_print_locks(void)
6284 struct nfs4_client *clp;
6286 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6289 if (!nfsd_netns_ready(nn))
6292 spin_lock(&nn->client_lock);
6293 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6294 count += nfsd_print_client_locks(clp);
6295 spin_unlock(&nn->client_lock);
6301 nfsd_reap_locks(struct list_head *reaplist)
6303 struct nfs4_client *clp;
6304 struct nfs4_ol_stateid *stp, *next;
6306 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
6307 list_del_init(&stp->st_locks);
6308 clp = stp->st_stid.sc_client;
6309 nfs4_put_stid(&stp->st_stid);
6315 nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
6317 unsigned int count = 0;
6318 struct nfs4_client *clp;
6319 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6321 LIST_HEAD(reaplist);
6323 if (!nfsd_netns_ready(nn))
6326 spin_lock(&nn->client_lock);
6327 clp = nfsd_find_client(addr, addr_size);
6329 count = nfsd_collect_client_locks(clp, &reaplist, 0);
6330 spin_unlock(&nn->client_lock);
6331 nfsd_reap_locks(&reaplist);
6336 nfsd_inject_forget_locks(u64 max)
6339 struct nfs4_client *clp;
6340 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6342 LIST_HEAD(reaplist);
6344 if (!nfsd_netns_ready(nn))
6347 spin_lock(&nn->client_lock);
6348 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6349 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
6350 if (max != 0 && count >= max)
6353 spin_unlock(&nn->client_lock);
6354 nfsd_reap_locks(&reaplist);
6359 nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
6360 struct list_head *collect,
6361 void (*func)(struct nfs4_openowner *))
6363 struct nfs4_openowner *oop, *next;
6364 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6368 lockdep_assert_held(&nn->client_lock);
6370 spin_lock(&clp->cl_lock);
6371 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
6375 atomic_inc(&clp->cl_refcount);
6376 list_add(&oop->oo_perclient, collect);
6381 * Despite the fact that these functions deal with
6382 * 64-bit integers for "count", we must ensure that
6383 * it doesn't blow up the clp->cl_refcount. Throw a
6384 * warning if we start to approach INT_MAX here.
6386 WARN_ON_ONCE(count == (INT_MAX / 2));
6390 spin_unlock(&clp->cl_lock);
6396 nfsd_print_client_openowners(struct nfs4_client *clp)
6398 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
6400 nfsd_print_count(clp, count, "openowners");
6405 nfsd_collect_client_openowners(struct nfs4_client *clp,
6406 struct list_head *collect, u64 max)
6408 return nfsd_foreach_client_openowner(clp, max, collect,
6409 unhash_openowner_locked);
6413 nfsd_inject_print_openowners(void)
6415 struct nfs4_client *clp;
6417 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6420 if (!nfsd_netns_ready(nn))
6423 spin_lock(&nn->client_lock);
6424 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6425 count += nfsd_print_client_openowners(clp);
6426 spin_unlock(&nn->client_lock);
6432 nfsd_reap_openowners(struct list_head *reaplist)
6434 struct nfs4_client *clp;
6435 struct nfs4_openowner *oop, *next;
6437 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
6438 list_del_init(&oop->oo_perclient);
6439 clp = oop->oo_owner.so_client;
6440 release_openowner(oop);
6446 nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
6449 unsigned int count = 0;
6450 struct nfs4_client *clp;
6451 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6453 LIST_HEAD(reaplist);
6455 if (!nfsd_netns_ready(nn))
6458 spin_lock(&nn->client_lock);
6459 clp = nfsd_find_client(addr, addr_size);
6461 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
6462 spin_unlock(&nn->client_lock);
6463 nfsd_reap_openowners(&reaplist);
6468 nfsd_inject_forget_openowners(u64 max)
6471 struct nfs4_client *clp;
6472 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6474 LIST_HEAD(reaplist);
6476 if (!nfsd_netns_ready(nn))
6479 spin_lock(&nn->client_lock);
6480 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6481 count += nfsd_collect_client_openowners(clp, &reaplist,
6483 if (max != 0 && count >= max)
6486 spin_unlock(&nn->client_lock);
6487 nfsd_reap_openowners(&reaplist);
6491 static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
6492 struct list_head *victims)
6494 struct nfs4_delegation *dp, *next;
6495 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6499 lockdep_assert_held(&nn->client_lock);
6501 spin_lock(&state_lock);
6502 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
6505 * It's not safe to mess with delegations that have a
6506 * non-zero dl_time. They might have already been broken
6507 * and could be processed by the laundromat outside of
6508 * the state_lock. Just leave them be.
6510 if (dp->dl_time != 0)
6513 atomic_inc(&clp->cl_refcount);
6514 WARN_ON(!unhash_delegation_locked(dp));
6515 list_add(&dp->dl_recall_lru, victims);
6519 * Despite the fact that these functions deal with
6520 * 64-bit integers for "count", we must ensure that
6521 * it doesn't blow up the clp->cl_refcount. Throw a
6522 * warning if we start to approach INT_MAX here.
6524 WARN_ON_ONCE(count == (INT_MAX / 2));
6528 spin_unlock(&state_lock);
6533 nfsd_print_client_delegations(struct nfs4_client *clp)
6535 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
6537 nfsd_print_count(clp, count, "delegations");
6542 nfsd_inject_print_delegations(void)
6544 struct nfs4_client *clp;
6546 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6549 if (!nfsd_netns_ready(nn))
6552 spin_lock(&nn->client_lock);
6553 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6554 count += nfsd_print_client_delegations(clp);
6555 spin_unlock(&nn->client_lock);
6561 nfsd_forget_delegations(struct list_head *reaplist)
6563 struct nfs4_client *clp;
6564 struct nfs4_delegation *dp, *next;
6566 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6567 list_del_init(&dp->dl_recall_lru);
6568 clp = dp->dl_stid.sc_client;
6569 revoke_delegation(dp);
6575 nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
6579 struct nfs4_client *clp;
6580 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6582 LIST_HEAD(reaplist);
6584 if (!nfsd_netns_ready(nn))
6587 spin_lock(&nn->client_lock);
6588 clp = nfsd_find_client(addr, addr_size);
6590 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6591 spin_unlock(&nn->client_lock);
6593 nfsd_forget_delegations(&reaplist);
6598 nfsd_inject_forget_delegations(u64 max)
6601 struct nfs4_client *clp;
6602 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6604 LIST_HEAD(reaplist);
6606 if (!nfsd_netns_ready(nn))
6609 spin_lock(&nn->client_lock);
6610 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6611 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6612 if (max != 0 && count >= max)
6615 spin_unlock(&nn->client_lock);
6616 nfsd_forget_delegations(&reaplist);
6621 nfsd_recall_delegations(struct list_head *reaplist)
6623 struct nfs4_client *clp;
6624 struct nfs4_delegation *dp, *next;
6626 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6627 list_del_init(&dp->dl_recall_lru);
6628 clp = dp->dl_stid.sc_client;
6630 * We skipped all entries that had a zero dl_time before,
6631 * so we can now reset the dl_time back to 0. If a delegation
6632 * break comes in now, then it won't make any difference since
6633 * we're recalling it either way.
6635 spin_lock(&state_lock);
6637 spin_unlock(&state_lock);
6638 nfsd_break_one_deleg(dp);
6644 nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
6648 struct nfs4_client *clp;
6649 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6651 LIST_HEAD(reaplist);
6653 if (!nfsd_netns_ready(nn))
6656 spin_lock(&nn->client_lock);
6657 clp = nfsd_find_client(addr, addr_size);
6659 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6660 spin_unlock(&nn->client_lock);
6662 nfsd_recall_delegations(&reaplist);
6667 nfsd_inject_recall_delegations(u64 max)
6670 struct nfs4_client *clp, *next;
6671 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6673 LIST_HEAD(reaplist);
6675 if (!nfsd_netns_ready(nn))
6678 spin_lock(&nn->client_lock);
6679 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6680 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6681 if (max != 0 && ++count >= max)
6684 spin_unlock(&nn->client_lock);
6685 nfsd_recall_delegations(&reaplist);
6688 #endif /* CONFIG_NFSD_FAULT_INJECTION */
6691 * Since the lifetime of a delegation isn't limited to that of an open, a
6692 * client may quite reasonably hang on to a delegation as long as it has
6693 * the inode cached. This becomes an obvious problem the first time a
6694 * client's inode cache approaches the size of the server's total memory.
6696 * For now we avoid this problem by imposing a hard limit on the number
6697 * of delegations, which varies according to the server's memory size.
6700 set_max_delegations(void)
6703 * Allow at most 4 delegations per megabyte of RAM. Quick
6704 * estimates suggest that in the worst case (where every delegation
6705 * is for a different inode), a delegation could take about 1.5K,
6706 * giving a worst case usage of about 6% of memory.
6708 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
6711 static int nfs4_state_create_net(struct net *net)
6713 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6716 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6717 CLIENT_HASH_SIZE, GFP_KERNEL);
6718 if (!nn->conf_id_hashtbl)
6720 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6721 CLIENT_HASH_SIZE, GFP_KERNEL);
6722 if (!nn->unconf_id_hashtbl)
6724 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
6725 SESSION_HASH_SIZE, GFP_KERNEL);
6726 if (!nn->sessionid_hashtbl)
6729 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6730 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
6731 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
6733 for (i = 0; i < SESSION_HASH_SIZE; i++)
6734 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
6735 nn->conf_name_tree = RB_ROOT;
6736 nn->unconf_name_tree = RB_ROOT;
6737 INIT_LIST_HEAD(&nn->client_lru);
6738 INIT_LIST_HEAD(&nn->close_lru);
6739 INIT_LIST_HEAD(&nn->del_recall_lru);
6740 spin_lock_init(&nn->client_lock);
6742 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
6748 kfree(nn->unconf_id_hashtbl);
6750 kfree(nn->conf_id_hashtbl);
6756 nfs4_state_destroy_net(struct net *net)
6759 struct nfs4_client *clp = NULL;
6760 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6762 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6763 while (!list_empty(&nn->conf_id_hashtbl[i])) {
6764 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6765 destroy_client(clp);
6769 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6770 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
6771 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6772 destroy_client(clp);
6776 kfree(nn->sessionid_hashtbl);
6777 kfree(nn->unconf_id_hashtbl);
6778 kfree(nn->conf_id_hashtbl);
6783 nfs4_state_start_net(struct net *net)
6785 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6788 ret = nfs4_state_create_net(net);
6791 nn->boot_time = get_seconds();
6792 nn->grace_ended = false;
6793 nn->nfsd4_manager.block_opens = true;
6794 locks_start_grace(net, &nn->nfsd4_manager);
6795 nfsd4_client_tracking_init(net);
6796 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
6797 nn->nfsd4_grace, net);
6798 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
6802 /* initialization to perform when the nfsd service is started: */
6805 nfs4_state_start(void)
6809 ret = set_callback_cred();
6812 laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
6813 if (laundry_wq == NULL) {
6817 ret = nfsd4_create_callback_queue();
6819 goto out_free_laundry;
6821 set_max_delegations();
6826 destroy_workqueue(laundry_wq);
6832 nfs4_state_shutdown_net(struct net *net)
6834 struct nfs4_delegation *dp = NULL;
6835 struct list_head *pos, *next, reaplist;
6836 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6838 cancel_delayed_work_sync(&nn->laundromat_work);
6839 locks_end_grace(&nn->nfsd4_manager);
6841 INIT_LIST_HEAD(&reaplist);
6842 spin_lock(&state_lock);
6843 list_for_each_safe(pos, next, &nn->del_recall_lru) {
6844 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
6845 WARN_ON(!unhash_delegation_locked(dp));
6846 list_add(&dp->dl_recall_lru, &reaplist);
6848 spin_unlock(&state_lock);
6849 list_for_each_safe(pos, next, &reaplist) {
6850 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
6851 list_del_init(&dp->dl_recall_lru);
6852 put_clnt_odstate(dp->dl_clnt_odstate);
6853 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
6854 nfs4_put_stid(&dp->dl_stid);
6857 nfsd4_client_tracking_exit(net);
6858 nfs4_state_destroy_net(net);
6862 nfs4_state_shutdown(void)
6864 destroy_workqueue(laundry_wq);
6865 nfsd4_destroy_callback_queue();
6869 get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6871 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
6872 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
6876 put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6878 if (cstate->minorversion) {
6879 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
6880 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6885 clear_current_stateid(struct nfsd4_compound_state *cstate)
6887 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6891 * functions to set current state id
6894 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6896 put_stateid(cstate, &odp->od_stateid);
6900 nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
6902 put_stateid(cstate, &open->op_stateid);
6906 nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6908 put_stateid(cstate, &close->cl_stateid);
6912 nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
6914 put_stateid(cstate, &lock->lk_resp_stateid);
6918 * functions to consume current state id
6922 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6924 get_stateid(cstate, &odp->od_stateid);
6928 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
6930 get_stateid(cstate, &drp->dr_stateid);
6934 nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
6936 get_stateid(cstate, &fsp->fr_stateid);
6940 nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
6942 get_stateid(cstate, &setattr->sa_stateid);
6946 nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6948 get_stateid(cstate, &close->cl_stateid);
6952 nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
6954 get_stateid(cstate, &locku->lu_stateid);
6958 nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
6960 get_stateid(cstate, &read->rd_stateid);
6964 nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
6966 get_stateid(cstate, &write->wr_stateid);