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 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 static int copy_cred(struct svc_cred *target, struct svc_cred *source)
1841 if (source->cr_principal) {
1842 target->cr_principal =
1843 kstrdup(source->cr_principal, GFP_KERNEL);
1844 if (target->cr_principal == NULL)
1847 target->cr_principal = NULL;
1848 target->cr_flavor = source->cr_flavor;
1849 target->cr_uid = source->cr_uid;
1850 target->cr_gid = source->cr_gid;
1851 target->cr_group_info = source->cr_group_info;
1852 get_group_info(target->cr_group_info);
1853 target->cr_gss_mech = source->cr_gss_mech;
1854 if (source->cr_gss_mech)
1855 gss_mech_get(source->cr_gss_mech);
1860 compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1862 if (o1->len < o2->len)
1864 if (o1->len > o2->len)
1866 return memcmp(o1->data, o2->data, o1->len);
1869 static int same_name(const char *n1, const char *n2)
1871 return 0 == memcmp(n1, n2, HEXDIR_LEN);
1875 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1877 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
1881 same_clid(clientid_t *cl1, clientid_t *cl2)
1883 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
1886 static bool groups_equal(struct group_info *g1, struct group_info *g2)
1890 if (g1->ngroups != g2->ngroups)
1892 for (i=0; i<g1->ngroups; i++)
1893 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
1899 * RFC 3530 language requires clid_inuse be returned when the
1900 * "principal" associated with a requests differs from that previously
1901 * used. We use uid, gid's, and gss principal string as our best
1902 * approximation. We also don't want to allow non-gss use of a client
1903 * established using gss: in theory cr_principal should catch that
1904 * change, but in practice cr_principal can be null even in the gss case
1905 * since gssd doesn't always pass down a principal string.
1907 static bool is_gss_cred(struct svc_cred *cr)
1909 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1910 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1915 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1917 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
1918 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1919 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
1920 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1922 if (cr1->cr_principal == cr2->cr_principal)
1924 if (!cr1->cr_principal || !cr2->cr_principal)
1926 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
1929 static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1931 struct svc_cred *cr = &rqstp->rq_cred;
1934 if (!cr->cr_gss_mech)
1936 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1937 return service == RPC_GSS_SVC_INTEGRITY ||
1938 service == RPC_GSS_SVC_PRIVACY;
1941 static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1943 struct svc_cred *cr = &rqstp->rq_cred;
1945 if (!cl->cl_mach_cred)
1947 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1949 if (!svc_rqst_integrity_protected(rqstp))
1951 if (!cr->cr_principal)
1953 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1956 static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
1961 * This is opaque to client, so no need to byte-swap. Use
1962 * __force to keep sparse happy
1964 verf[0] = (__force __be32)get_seconds();
1965 verf[1] = (__force __be32)nn->clverifier_counter++;
1966 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
1969 static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
1971 clp->cl_clientid.cl_boot = nn->boot_time;
1972 clp->cl_clientid.cl_id = nn->clientid_counter++;
1973 gen_confirm(clp, nn);
1976 static struct nfs4_stid *
1977 find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
1979 struct nfs4_stid *ret;
1981 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1982 if (!ret || !ret->sc_type)
1987 static struct nfs4_stid *
1988 find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
1990 struct nfs4_stid *s;
1992 spin_lock(&cl->cl_lock);
1993 s = find_stateid_locked(cl, t);
1995 if (typemask & s->sc_type)
1996 atomic_inc(&s->sc_count);
2000 spin_unlock(&cl->cl_lock);
2004 static struct nfs4_client *create_client(struct xdr_netobj name,
2005 struct svc_rqst *rqstp, nfs4_verifier *verf)
2007 struct nfs4_client *clp;
2008 struct sockaddr *sa = svc_addr(rqstp);
2010 struct net *net = SVC_NET(rqstp);
2012 clp = alloc_client(name);
2016 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
2021 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
2022 clp->cl_time = get_seconds();
2023 clear_bit(0, &clp->cl_cb_slot_busy);
2024 copy_verf(clp, verf);
2025 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
2026 clp->cl_cb_session = NULL;
2032 add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
2034 struct rb_node **new = &(root->rb_node), *parent = NULL;
2035 struct nfs4_client *clp;
2038 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
2041 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
2042 new = &((*new)->rb_left);
2044 new = &((*new)->rb_right);
2047 rb_link_node(&new_clp->cl_namenode, parent, new);
2048 rb_insert_color(&new_clp->cl_namenode, root);
2051 static struct nfs4_client *
2052 find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2055 struct rb_node *node = root->rb_node;
2056 struct nfs4_client *clp;
2059 clp = rb_entry(node, struct nfs4_client, cl_namenode);
2060 cmp = compare_blob(&clp->cl_name, name);
2062 node = node->rb_left;
2064 node = node->rb_right;
2072 add_to_unconfirmed(struct nfs4_client *clp)
2074 unsigned int idhashval;
2075 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2077 lockdep_assert_held(&nn->client_lock);
2079 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2080 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
2081 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
2082 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
2083 renew_client_locked(clp);
2087 move_to_confirmed(struct nfs4_client *clp)
2089 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
2090 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2092 lockdep_assert_held(&nn->client_lock);
2094 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
2095 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
2096 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
2097 add_clp_to_name_tree(clp, &nn->conf_name_tree);
2098 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2099 renew_client_locked(clp);
2102 static struct nfs4_client *
2103 find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
2105 struct nfs4_client *clp;
2106 unsigned int idhashval = clientid_hashval(clid->cl_id);
2108 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
2109 if (same_clid(&clp->cl_clientid, clid)) {
2110 if ((bool)clp->cl_minorversion != sessions)
2112 renew_client_locked(clp);
2119 static struct nfs4_client *
2120 find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2122 struct list_head *tbl = nn->conf_id_hashtbl;
2124 lockdep_assert_held(&nn->client_lock);
2125 return find_client_in_id_table(tbl, clid, sessions);
2128 static struct nfs4_client *
2129 find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2131 struct list_head *tbl = nn->unconf_id_hashtbl;
2133 lockdep_assert_held(&nn->client_lock);
2134 return find_client_in_id_table(tbl, clid, sessions);
2137 static bool clp_used_exchangeid(struct nfs4_client *clp)
2139 return clp->cl_exchange_flags != 0;
2142 static struct nfs4_client *
2143 find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
2145 lockdep_assert_held(&nn->client_lock);
2146 return find_clp_in_name_tree(name, &nn->conf_name_tree);
2149 static struct nfs4_client *
2150 find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
2152 lockdep_assert_held(&nn->client_lock);
2153 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
2157 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
2159 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
2160 struct sockaddr *sa = svc_addr(rqstp);
2161 u32 scopeid = rpc_get_scope_id(sa);
2162 unsigned short expected_family;
2164 /* Currently, we only support tcp and tcp6 for the callback channel */
2165 if (se->se_callback_netid_len == 3 &&
2166 !memcmp(se->se_callback_netid_val, "tcp", 3))
2167 expected_family = AF_INET;
2168 else if (se->se_callback_netid_len == 4 &&
2169 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2170 expected_family = AF_INET6;
2174 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
2175 se->se_callback_addr_len,
2176 (struct sockaddr *)&conn->cb_addr,
2177 sizeof(conn->cb_addr));
2179 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
2182 if (conn->cb_addr.ss_family == AF_INET6)
2183 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
2185 conn->cb_prog = se->se_callback_prog;
2186 conn->cb_ident = se->se_callback_ident;
2187 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
2190 conn->cb_addr.ss_family = AF_UNSPEC;
2191 conn->cb_addrlen = 0;
2192 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
2193 "will not receive delegations\n",
2194 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2200 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
2203 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2205 struct xdr_buf *buf = resp->xdr.buf;
2206 struct nfsd4_slot *slot = resp->cstate.slot;
2209 dprintk("--> %s slot %p\n", __func__, slot);
2211 slot->sl_opcnt = resp->opcnt;
2212 slot->sl_status = resp->cstate.status;
2214 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
2215 if (nfsd4_not_cached(resp)) {
2216 slot->sl_datalen = 0;
2219 base = resp->cstate.data_offset;
2220 slot->sl_datalen = buf->len - base;
2221 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
2222 WARN("%s: sessions DRC could not cache compound\n", __func__);
2227 * Encode the replay sequence operation from the slot values.
2228 * If cachethis is FALSE encode the uncached rep error on the next
2229 * operation which sets resp->p and increments resp->opcnt for
2230 * nfs4svc_encode_compoundres.
2234 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2235 struct nfsd4_compoundres *resp)
2237 struct nfsd4_op *op;
2238 struct nfsd4_slot *slot = resp->cstate.slot;
2240 /* Encode the replayed sequence operation */
2241 op = &args->ops[resp->opcnt - 1];
2242 nfsd4_encode_operation(resp, op);
2244 /* Return nfserr_retry_uncached_rep in next operation. */
2245 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
2246 op = &args->ops[resp->opcnt++];
2247 op->status = nfserr_retry_uncached_rep;
2248 nfsd4_encode_operation(resp, op);
2254 * The sequence operation is not cached because we can use the slot and
2258 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2259 struct nfsd4_sequence *seq)
2261 struct nfsd4_slot *slot = resp->cstate.slot;
2262 struct xdr_stream *xdr = &resp->xdr;
2266 dprintk("--> %s slot %p\n", __func__, slot);
2268 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
2272 p = xdr_reserve_space(xdr, slot->sl_datalen);
2275 return nfserr_serverfault;
2277 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2278 xdr_commit_encode(xdr);
2280 resp->opcnt = slot->sl_opcnt;
2281 return slot->sl_status;
2285 * Set the exchange_id flags returned by the server.
2288 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2290 #ifdef CONFIG_NFSD_PNFS
2291 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
2293 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
2296 /* Referrals are supported, Migration is not. */
2297 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2299 /* set the wire flags to return to client. */
2300 clid->flags = new->cl_exchange_flags;
2303 static bool client_has_openowners(struct nfs4_client *clp)
2305 struct nfs4_openowner *oo;
2307 list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
2308 if (!list_empty(&oo->oo_owner.so_stateids))
2314 static bool client_has_state(struct nfs4_client *clp)
2316 return client_has_openowners(clp)
2317 #ifdef CONFIG_NFSD_PNFS
2318 || !list_empty(&clp->cl_lo_states)
2320 || !list_empty(&clp->cl_delegations)
2321 || !list_empty(&clp->cl_sessions);
2325 nfsd4_exchange_id(struct svc_rqst *rqstp,
2326 struct nfsd4_compound_state *cstate,
2327 struct nfsd4_exchange_id *exid)
2329 struct nfs4_client *conf, *new;
2330 struct nfs4_client *unconf = NULL;
2332 char addr_str[INET6_ADDRSTRLEN];
2333 nfs4_verifier verf = exid->verifier;
2334 struct sockaddr *sa = svc_addr(rqstp);
2335 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
2336 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2338 rpc_ntop(sa, addr_str, sizeof(addr_str));
2339 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
2340 "ip_addr=%s flags %x, spa_how %d\n",
2341 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
2342 addr_str, exid->flags, exid->spa_how);
2344 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
2345 return nfserr_inval;
2347 switch (exid->spa_how) {
2349 if (!svc_rqst_integrity_protected(rqstp))
2350 return nfserr_inval;
2353 default: /* checked by xdr code */
2356 return nfserr_encr_alg_unsupp;
2359 new = create_client(exid->clname, rqstp, &verf);
2361 return nfserr_jukebox;
2363 /* Cases below refer to rfc 5661 section 18.35.4: */
2364 spin_lock(&nn->client_lock);
2365 conf = find_confirmed_client_by_name(&exid->clname, nn);
2367 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2368 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2371 if (!clp_used_exchangeid(conf)) { /* buggy client */
2372 status = nfserr_inval;
2375 if (!mach_creds_match(conf, rqstp)) {
2376 status = nfserr_wrong_cred;
2379 if (!creds_match) { /* case 9 */
2380 status = nfserr_perm;
2383 if (!verfs_match) { /* case 8 */
2384 status = nfserr_not_same;
2388 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
2391 if (!creds_match) { /* case 3 */
2392 if (client_has_state(conf)) {
2393 status = nfserr_clid_inuse;
2398 if (verfs_match) { /* case 2 */
2399 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
2402 /* case 5, client reboot */
2407 if (update) { /* case 7 */
2408 status = nfserr_noent;
2412 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
2413 if (unconf) /* case 4, possible retry or client restart */
2414 unhash_client_locked(unconf);
2416 /* case 1 (normal case) */
2419 status = mark_client_expired_locked(conf);
2423 new->cl_minorversion = cstate->minorversion;
2424 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
2427 add_to_unconfirmed(new);
2430 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
2431 exid->clientid.cl_id = conf->cl_clientid.cl_id;
2433 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
2434 nfsd4_set_ex_flags(conf, exid);
2436 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
2437 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
2441 spin_unlock(&nn->client_lock);
2445 expire_client(unconf);
2450 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
2452 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2455 /* The slot is in use, and no response has been sent. */
2457 if (seqid == slot_seqid)
2458 return nfserr_jukebox;
2460 return nfserr_seq_misordered;
2462 /* Note unsigned 32-bit arithmetic handles wraparound: */
2463 if (likely(seqid == slot_seqid + 1))
2465 if (seqid == slot_seqid)
2466 return nfserr_replay_cache;
2467 return nfserr_seq_misordered;
2471 * Cache the create session result into the create session single DRC
2472 * slot cache by saving the xdr structure. sl_seqid has been set.
2473 * Do this for solo or embedded create session operations.
2476 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
2477 struct nfsd4_clid_slot *slot, __be32 nfserr)
2479 slot->sl_status = nfserr;
2480 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2484 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2485 struct nfsd4_clid_slot *slot)
2487 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2488 return slot->sl_status;
2491 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2492 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2493 1 + /* MIN tag is length with zero, only length */ \
2494 3 + /* version, opcount, opcode */ \
2495 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2496 /* seqid, slotID, slotID, cache */ \
2497 4 ) * sizeof(__be32))
2499 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2500 2 + /* verifier: AUTH_NULL, length 0 */\
2502 1 + /* MIN tag is length with zero, only length */ \
2503 3 + /* opcount, opcode, opstatus*/ \
2504 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2505 /* seqid, slotID, slotID, slotID, status */ \
2506 5 ) * sizeof(__be32))
2508 static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
2510 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2512 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2513 return nfserr_toosmall;
2514 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2515 return nfserr_toosmall;
2516 ca->headerpadsz = 0;
2517 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2518 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2519 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2520 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2521 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2522 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2524 * Note decreasing slot size below client's request may make it
2525 * difficult for client to function correctly, whereas
2526 * decreasing the number of slots will (just?) affect
2527 * performance. When short on memory we therefore prefer to
2528 * decrease number of slots instead of their size. Clients that
2529 * request larger slots than they need will get poor results:
2531 ca->maxreqs = nfsd4_get_drc_mem(ca);
2533 return nfserr_jukebox;
2538 #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2539 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2540 #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2541 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2543 static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2545 ca->headerpadsz = 0;
2548 * These RPC_MAX_HEADER macros are overkill, especially since we
2549 * don't even do gss on the backchannel yet. But this is still
2550 * less than 1k. Tighten up this estimate in the unlikely event
2551 * it turns out to be a problem for some client:
2553 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
2554 return nfserr_toosmall;
2555 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
2556 return nfserr_toosmall;
2557 ca->maxresp_cached = 0;
2559 return nfserr_toosmall;
2564 static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2566 switch (cbs->flavor) {
2572 * GSS case: the spec doesn't allow us to return this
2573 * error. But it also doesn't allow us not to support
2575 * I'd rather this fail hard than return some error the
2576 * client might think it can already handle:
2578 return nfserr_encr_alg_unsupp;
2583 nfsd4_create_session(struct svc_rqst *rqstp,
2584 struct nfsd4_compound_state *cstate,
2585 struct nfsd4_create_session *cr_ses)
2587 struct sockaddr *sa = svc_addr(rqstp);
2588 struct nfs4_client *conf, *unconf;
2589 struct nfs4_client *old = NULL;
2590 struct nfsd4_session *new;
2591 struct nfsd4_conn *conn;
2592 struct nfsd4_clid_slot *cs_slot = NULL;
2594 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2596 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2597 return nfserr_inval;
2598 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2601 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
2604 status = check_backchannel_attrs(&cr_ses->back_channel);
2606 goto out_release_drc_mem;
2607 status = nfserr_jukebox;
2608 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
2610 goto out_release_drc_mem;
2611 conn = alloc_conn_from_crses(rqstp, cr_ses);
2613 goto out_free_session;
2615 spin_lock(&nn->client_lock);
2616 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
2617 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
2618 WARN_ON_ONCE(conf && unconf);
2621 status = nfserr_wrong_cred;
2622 if (!mach_creds_match(conf, rqstp))
2624 cs_slot = &conf->cl_cs_slot;
2625 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2627 if (status == nfserr_replay_cache)
2628 status = nfsd4_replay_create_session(cr_ses, cs_slot);
2631 } else if (unconf) {
2632 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
2633 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
2634 status = nfserr_clid_inuse;
2637 status = nfserr_wrong_cred;
2638 if (!mach_creds_match(unconf, rqstp))
2640 cs_slot = &unconf->cl_cs_slot;
2641 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2643 /* an unconfirmed replay returns misordered */
2644 status = nfserr_seq_misordered;
2647 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
2649 status = mark_client_expired_locked(old);
2655 move_to_confirmed(unconf);
2658 status = nfserr_stale_clientid;
2663 * We do not support RDMA or persistent sessions
2665 cr_ses->flags &= ~SESSION4_PERSIST;
2666 cr_ses->flags &= ~SESSION4_RDMA;
2668 init_session(rqstp, new, conf, cr_ses);
2669 nfsd4_get_session_locked(new);
2671 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
2672 NFS4_MAX_SESSIONID_LEN);
2673 cs_slot->sl_seqid++;
2674 cr_ses->seqid = cs_slot->sl_seqid;
2676 /* cache solo and embedded create sessions under the client_lock */
2677 nfsd4_cache_create_session(cr_ses, cs_slot, status);
2678 spin_unlock(&nn->client_lock);
2679 /* init connection and backchannel */
2680 nfsd4_init_conn(rqstp, conn, new);
2681 nfsd4_put_session(new);
2686 spin_unlock(&nn->client_lock);
2691 __free_session(new);
2692 out_release_drc_mem:
2693 nfsd4_put_drc_mem(&cr_ses->fore_channel);
2697 static __be32 nfsd4_map_bcts_dir(u32 *dir)
2700 case NFS4_CDFC4_FORE:
2701 case NFS4_CDFC4_BACK:
2703 case NFS4_CDFC4_FORE_OR_BOTH:
2704 case NFS4_CDFC4_BACK_OR_BOTH:
2705 *dir = NFS4_CDFC4_BOTH;
2708 return nfserr_inval;
2711 __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2713 struct nfsd4_session *session = cstate->session;
2714 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2717 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2720 spin_lock(&nn->client_lock);
2721 session->se_cb_prog = bc->bc_cb_program;
2722 session->se_cb_sec = bc->bc_cb_sec;
2723 spin_unlock(&nn->client_lock);
2725 nfsd4_probe_callback(session->se_client);
2730 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2731 struct nfsd4_compound_state *cstate,
2732 struct nfsd4_bind_conn_to_session *bcts)
2735 struct nfsd4_conn *conn;
2736 struct nfsd4_session *session;
2737 struct net *net = SVC_NET(rqstp);
2738 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2740 if (!nfsd4_last_compound_op(rqstp))
2741 return nfserr_not_only_op;
2742 spin_lock(&nn->client_lock);
2743 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
2744 spin_unlock(&nn->client_lock);
2746 goto out_no_session;
2747 status = nfserr_wrong_cred;
2748 if (!mach_creds_match(session->se_client, rqstp))
2750 status = nfsd4_map_bcts_dir(&bcts->dir);
2753 conn = alloc_conn(rqstp, bcts->dir);
2754 status = nfserr_jukebox;
2757 nfsd4_init_conn(rqstp, conn, session);
2760 nfsd4_put_session(session);
2765 static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2769 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2773 nfsd4_destroy_session(struct svc_rqst *r,
2774 struct nfsd4_compound_state *cstate,
2775 struct nfsd4_destroy_session *sessionid)
2777 struct nfsd4_session *ses;
2779 int ref_held_by_me = 0;
2780 struct net *net = SVC_NET(r);
2781 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2783 status = nfserr_not_only_op;
2784 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
2785 if (!nfsd4_last_compound_op(r))
2789 dump_sessionid(__func__, &sessionid->sessionid);
2790 spin_lock(&nn->client_lock);
2791 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
2793 goto out_client_lock;
2794 status = nfserr_wrong_cred;
2795 if (!mach_creds_match(ses->se_client, r))
2796 goto out_put_session;
2797 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
2799 goto out_put_session;
2800 unhash_session(ses);
2801 spin_unlock(&nn->client_lock);
2803 nfsd4_probe_callback_sync(ses->se_client);
2805 spin_lock(&nn->client_lock);
2808 nfsd4_put_session_locked(ses);
2810 spin_unlock(&nn->client_lock);
2815 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
2817 struct nfsd4_conn *c;
2819 list_for_each_entry(c, &s->se_conns, cn_persession) {
2820 if (c->cn_xprt == xpt) {
2827 static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
2829 struct nfs4_client *clp = ses->se_client;
2830 struct nfsd4_conn *c;
2831 __be32 status = nfs_ok;
2834 spin_lock(&clp->cl_lock);
2835 c = __nfsd4_find_conn(new->cn_xprt, ses);
2838 status = nfserr_conn_not_bound_to_session;
2839 if (clp->cl_mach_cred)
2841 __nfsd4_hash_conn(new, ses);
2842 spin_unlock(&clp->cl_lock);
2843 ret = nfsd4_register_conn(new);
2845 /* oops; xprt is already down: */
2846 nfsd4_conn_lost(&new->cn_xpt_user);
2849 spin_unlock(&clp->cl_lock);
2854 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2856 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2858 return args->opcnt > session->se_fchannel.maxops;
2861 static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2862 struct nfsd4_session *session)
2864 struct xdr_buf *xb = &rqstp->rq_arg;
2866 return xb->len > session->se_fchannel.maxreq_sz;
2870 nfsd4_sequence(struct svc_rqst *rqstp,
2871 struct nfsd4_compound_state *cstate,
2872 struct nfsd4_sequence *seq)
2874 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2875 struct xdr_stream *xdr = &resp->xdr;
2876 struct nfsd4_session *session;
2877 struct nfs4_client *clp;
2878 struct nfsd4_slot *slot;
2879 struct nfsd4_conn *conn;
2882 struct net *net = SVC_NET(rqstp);
2883 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2885 if (resp->opcnt != 1)
2886 return nfserr_sequence_pos;
2889 * Will be either used or freed by nfsd4_sequence_check_conn
2892 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2894 return nfserr_jukebox;
2896 spin_lock(&nn->client_lock);
2897 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
2899 goto out_no_session;
2900 clp = session->se_client;
2902 status = nfserr_too_many_ops;
2903 if (nfsd4_session_too_many_ops(rqstp, session))
2904 goto out_put_session;
2906 status = nfserr_req_too_big;
2907 if (nfsd4_request_too_big(rqstp, session))
2908 goto out_put_session;
2910 status = nfserr_badslot;
2911 if (seq->slotid >= session->se_fchannel.maxreqs)
2912 goto out_put_session;
2914 slot = session->se_slots[seq->slotid];
2915 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2917 /* We do not negotiate the number of slots yet, so set the
2918 * maxslots to the session maxreqs which is used to encode
2919 * sr_highest_slotid and the sr_target_slot id to maxslots */
2920 seq->maxslots = session->se_fchannel.maxreqs;
2922 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2923 slot->sl_flags & NFSD4_SLOT_INUSE);
2924 if (status == nfserr_replay_cache) {
2925 status = nfserr_seq_misordered;
2926 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
2927 goto out_put_session;
2928 cstate->slot = slot;
2929 cstate->session = session;
2931 /* Return the cached reply status and set cstate->status
2932 * for nfsd4_proc_compound processing */
2933 status = nfsd4_replay_cache_entry(resp, seq);
2934 cstate->status = nfserr_replay_cache;
2938 goto out_put_session;
2940 status = nfsd4_sequence_check_conn(conn, session);
2943 goto out_put_session;
2945 buflen = (seq->cachethis) ?
2946 session->se_fchannel.maxresp_cached :
2947 session->se_fchannel.maxresp_sz;
2948 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2950 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
2951 goto out_put_session;
2952 svc_reserve(rqstp, buflen);
2955 /* Success! bump slot seqid */
2956 slot->sl_seqid = seq->seqid;
2957 slot->sl_flags |= NFSD4_SLOT_INUSE;
2959 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
2961 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
2963 cstate->slot = slot;
2964 cstate->session = session;
2968 switch (clp->cl_cb_state) {
2970 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2972 case NFSD4_CB_FAULT:
2973 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2976 seq->status_flags = 0;
2978 if (!list_empty(&clp->cl_revoked))
2979 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
2983 spin_unlock(&nn->client_lock);
2986 nfsd4_put_session_locked(session);
2987 goto out_no_session;
2991 nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2993 struct nfsd4_compound_state *cs = &resp->cstate;
2995 if (nfsd4_has_session(cs)) {
2996 if (cs->status != nfserr_replay_cache) {
2997 nfsd4_store_cache_entry(resp);
2998 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
3000 /* Drop session reference that was taken in nfsd4_sequence() */
3001 nfsd4_put_session(cs->session);
3003 put_client_renew(cs->clp);
3007 nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
3009 struct nfs4_client *conf, *unconf;
3010 struct nfs4_client *clp = NULL;
3012 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3014 spin_lock(&nn->client_lock);
3015 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
3016 conf = find_confirmed_client(&dc->clientid, true, nn);
3017 WARN_ON_ONCE(conf && unconf);
3020 if (client_has_state(conf)) {
3021 status = nfserr_clientid_busy;
3024 status = mark_client_expired_locked(conf);
3031 status = nfserr_stale_clientid;
3034 if (!mach_creds_match(clp, rqstp)) {
3036 status = nfserr_wrong_cred;
3039 unhash_client_locked(clp);
3041 spin_unlock(&nn->client_lock);
3048 nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
3052 if (rc->rca_one_fs) {
3053 if (!cstate->current_fh.fh_dentry)
3054 return nfserr_nofilehandle;
3056 * We don't take advantage of the rca_one_fs case.
3057 * That's OK, it's optional, we can safely ignore it.
3062 status = nfserr_complete_already;
3063 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
3064 &cstate->session->se_client->cl_flags))
3067 status = nfserr_stale_clientid;
3068 if (is_client_expired(cstate->session->se_client))
3070 * The following error isn't really legal.
3071 * But we only get here if the client just explicitly
3072 * destroyed the client. Surely it no longer cares what
3073 * error it gets back on an operation for the dead
3079 nfsd4_client_record_create(cstate->session->se_client);
3085 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3086 struct nfsd4_setclientid *setclid)
3088 struct xdr_netobj clname = setclid->se_name;
3089 nfs4_verifier clverifier = setclid->se_verf;
3090 struct nfs4_client *conf, *new;
3091 struct nfs4_client *unconf = NULL;
3093 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3095 new = create_client(clname, rqstp, &clverifier);
3097 return nfserr_jukebox;
3098 /* Cases below refer to rfc 3530 section 14.2.33: */
3099 spin_lock(&nn->client_lock);
3100 conf = find_confirmed_client_by_name(&clname, nn);
3101 if (conf && client_has_state(conf)) {
3103 status = nfserr_clid_inuse;
3104 if (clp_used_exchangeid(conf))
3106 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
3107 char addr_str[INET6_ADDRSTRLEN];
3108 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
3110 dprintk("NFSD: setclientid: string in use by client "
3111 "at %s\n", addr_str);
3115 unconf = find_unconfirmed_client_by_name(&clname, nn);
3117 unhash_client_locked(unconf);
3118 if (conf && same_verf(&conf->cl_verifier, &clverifier)) {
3119 /* case 1: probable callback update */
3120 copy_clid(new, conf);
3121 gen_confirm(new, nn);
3122 } else /* case 4 (new client) or cases 2, 3 (client reboot): */
3124 new->cl_minorversion = 0;
3125 gen_callback(new, setclid, rqstp);
3126 add_to_unconfirmed(new);
3127 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
3128 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
3129 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
3133 spin_unlock(&nn->client_lock);
3137 expire_client(unconf);
3143 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
3144 struct nfsd4_compound_state *cstate,
3145 struct nfsd4_setclientid_confirm *setclientid_confirm)
3147 struct nfs4_client *conf, *unconf;
3148 struct nfs4_client *old = NULL;
3149 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
3150 clientid_t * clid = &setclientid_confirm->sc_clientid;
3152 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3154 if (STALE_CLIENTID(clid, nn))
3155 return nfserr_stale_clientid;
3157 spin_lock(&nn->client_lock);
3158 conf = find_confirmed_client(clid, false, nn);
3159 unconf = find_unconfirmed_client(clid, false, nn);
3161 * We try hard to give out unique clientid's, so if we get an
3162 * attempt to confirm the same clientid with a different cred,
3163 * the client may be buggy; this should never happen.
3165 * Nevertheless, RFC 7530 recommends INUSE for this case:
3167 status = nfserr_clid_inuse;
3168 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3170 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3172 /* cases below refer to rfc 3530 section 14.2.34: */
3173 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
3174 if (conf && !unconf) /* case 2: probable retransmit */
3176 else /* case 4: client hasn't noticed we rebooted yet? */
3177 status = nfserr_stale_clientid;
3181 if (conf) { /* case 1: callback update */
3183 unhash_client_locked(old);
3184 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
3185 } else { /* case 3: normal case; new or rebooted client */
3186 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3188 status = nfserr_clid_inuse;
3189 if (client_has_state(old)
3190 && !same_creds(&unconf->cl_cred,
3193 status = mark_client_expired_locked(old);
3199 move_to_confirmed(unconf);
3202 get_client_locked(conf);
3203 spin_unlock(&nn->client_lock);
3204 nfsd4_probe_callback(conf);
3205 spin_lock(&nn->client_lock);
3206 put_client_renew_locked(conf);
3208 spin_unlock(&nn->client_lock);
3214 static struct nfs4_file *nfsd4_alloc_file(void)
3216 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3219 /* OPEN Share state helper functions */
3220 static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
3221 struct nfs4_file *fp)
3223 lockdep_assert_held(&state_lock);
3225 atomic_set(&fp->fi_ref, 1);
3226 spin_lock_init(&fp->fi_lock);
3227 INIT_LIST_HEAD(&fp->fi_stateids);
3228 INIT_LIST_HEAD(&fp->fi_delegations);
3229 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
3230 fh_copy_shallow(&fp->fi_fhandle, fh);
3231 fp->fi_deleg_file = NULL;
3232 fp->fi_had_conflict = false;
3233 fp->fi_share_deny = 0;
3234 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3235 memset(fp->fi_access, 0, sizeof(fp->fi_access));
3236 #ifdef CONFIG_NFSD_PNFS
3237 INIT_LIST_HEAD(&fp->fi_lo_states);
3238 atomic_set(&fp->fi_lo_recalls, 0);
3240 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
3244 nfsd4_free_slabs(void)
3246 kmem_cache_destroy(odstate_slab);
3247 kmem_cache_destroy(openowner_slab);
3248 kmem_cache_destroy(lockowner_slab);
3249 kmem_cache_destroy(file_slab);
3250 kmem_cache_destroy(stateid_slab);
3251 kmem_cache_destroy(deleg_slab);
3255 nfsd4_init_slabs(void)
3257 openowner_slab = kmem_cache_create("nfsd4_openowners",
3258 sizeof(struct nfs4_openowner), 0, 0, NULL);
3259 if (openowner_slab == NULL)
3261 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
3262 sizeof(struct nfs4_lockowner), 0, 0, NULL);
3263 if (lockowner_slab == NULL)
3264 goto out_free_openowner_slab;
3265 file_slab = kmem_cache_create("nfsd4_files",
3266 sizeof(struct nfs4_file), 0, 0, NULL);
3267 if (file_slab == NULL)
3268 goto out_free_lockowner_slab;
3269 stateid_slab = kmem_cache_create("nfsd4_stateids",
3270 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
3271 if (stateid_slab == NULL)
3272 goto out_free_file_slab;
3273 deleg_slab = kmem_cache_create("nfsd4_delegations",
3274 sizeof(struct nfs4_delegation), 0, 0, NULL);
3275 if (deleg_slab == NULL)
3276 goto out_free_stateid_slab;
3277 odstate_slab = kmem_cache_create("nfsd4_odstate",
3278 sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
3279 if (odstate_slab == NULL)
3280 goto out_free_deleg_slab;
3283 out_free_deleg_slab:
3284 kmem_cache_destroy(deleg_slab);
3285 out_free_stateid_slab:
3286 kmem_cache_destroy(stateid_slab);
3288 kmem_cache_destroy(file_slab);
3289 out_free_lockowner_slab:
3290 kmem_cache_destroy(lockowner_slab);
3291 out_free_openowner_slab:
3292 kmem_cache_destroy(openowner_slab);
3294 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3298 static void init_nfs4_replay(struct nfs4_replay *rp)
3300 rp->rp_status = nfserr_serverfault;
3302 rp->rp_buf = rp->rp_ibuf;
3303 mutex_init(&rp->rp_mutex);
3306 static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3307 struct nfs4_stateowner *so)
3309 if (!nfsd4_has_session(cstate)) {
3310 mutex_lock(&so->so_replay.rp_mutex);
3311 cstate->replay_owner = nfs4_get_stateowner(so);
3315 void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3317 struct nfs4_stateowner *so = cstate->replay_owner;
3320 cstate->replay_owner = NULL;
3321 mutex_unlock(&so->so_replay.rp_mutex);
3322 nfs4_put_stateowner(so);
3326 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
3328 struct nfs4_stateowner *sop;
3330 sop = kmem_cache_alloc(slab, GFP_KERNEL);
3334 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3335 if (!sop->so_owner.data) {
3336 kmem_cache_free(slab, sop);
3339 sop->so_owner.len = owner->len;
3341 INIT_LIST_HEAD(&sop->so_stateids);
3342 sop->so_client = clp;
3343 init_nfs4_replay(&sop->so_replay);
3344 atomic_set(&sop->so_count, 1);
3348 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
3350 lockdep_assert_held(&clp->cl_lock);
3352 list_add(&oo->oo_owner.so_strhash,
3353 &clp->cl_ownerstr_hashtbl[strhashval]);
3354 list_add(&oo->oo_perclient, &clp->cl_openowners);
3357 static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3359 unhash_openowner_locked(openowner(so));
3362 static void nfs4_free_openowner(struct nfs4_stateowner *so)
3364 struct nfs4_openowner *oo = openowner(so);
3366 kmem_cache_free(openowner_slab, oo);
3369 static const struct nfs4_stateowner_operations openowner_ops = {
3370 .so_unhash = nfs4_unhash_openowner,
3371 .so_free = nfs4_free_openowner,
3374 static struct nfs4_ol_stateid *
3375 nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
3377 struct nfs4_ol_stateid *local, *ret = NULL;
3378 struct nfs4_openowner *oo = open->op_openowner;
3380 lockdep_assert_held(&fp->fi_lock);
3382 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
3383 /* ignore lock owners */
3384 if (local->st_stateowner->so_is_open_owner == 0)
3386 if (local->st_stateowner == &oo->oo_owner) {
3388 atomic_inc(&ret->st_stid.sc_count);
3395 static struct nfs4_openowner *
3396 alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
3397 struct nfsd4_compound_state *cstate)
3399 struct nfs4_client *clp = cstate->clp;
3400 struct nfs4_openowner *oo, *ret;
3402 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3405 oo->oo_owner.so_ops = &openowner_ops;
3406 oo->oo_owner.so_is_open_owner = 1;
3407 oo->oo_owner.so_seqid = open->op_seqid;
3409 if (nfsd4_has_session(cstate))
3410 oo->oo_flags |= NFS4_OO_CONFIRMED;
3412 oo->oo_last_closed_stid = NULL;
3413 INIT_LIST_HEAD(&oo->oo_close_lru);
3414 spin_lock(&clp->cl_lock);
3415 ret = find_openstateowner_str_locked(strhashval, open, clp);
3417 hash_openowner(oo, clp, strhashval);
3420 nfs4_free_stateowner(&oo->oo_owner);
3422 spin_unlock(&clp->cl_lock);
3426 static struct nfs4_ol_stateid *
3427 init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp,
3428 struct nfsd4_open *open)
3431 struct nfs4_openowner *oo = open->op_openowner;
3432 struct nfs4_ol_stateid *retstp = NULL;
3434 /* We are moving these outside of the spinlocks to avoid the warnings */
3435 mutex_init(&stp->st_mutex);
3436 mutex_lock(&stp->st_mutex);
3438 spin_lock(&oo->oo_owner.so_client->cl_lock);
3439 spin_lock(&fp->fi_lock);
3441 retstp = nfsd4_find_existing_open(fp, open);
3444 atomic_inc(&stp->st_stid.sc_count);
3445 stp->st_stid.sc_type = NFS4_OPEN_STID;
3446 INIT_LIST_HEAD(&stp->st_locks);
3447 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
3449 stp->st_stid.sc_file = fp;
3450 stp->st_access_bmap = 0;
3451 stp->st_deny_bmap = 0;
3452 stp->st_openstp = NULL;
3453 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
3454 list_add(&stp->st_perfile, &fp->fi_stateids);
3457 spin_unlock(&fp->fi_lock);
3458 spin_unlock(&oo->oo_owner.so_client->cl_lock);
3460 mutex_lock(&retstp->st_mutex);
3461 /* Not that we need to, just for neatness */
3462 mutex_unlock(&stp->st_mutex);
3468 * In the 4.0 case we need to keep the owners around a little while to handle
3469 * CLOSE replay. We still do need to release any file access that is held by
3470 * them before returning however.
3473 move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
3475 struct nfs4_ol_stateid *last;
3476 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3477 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3480 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
3483 * We know that we hold one reference via nfsd4_close, and another
3484 * "persistent" reference for the client. If the refcount is higher
3485 * than 2, then there are still calls in progress that are using this
3486 * stateid. We can't put the sc_file reference until they are finished.
3487 * Wait for the refcount to drop to 2. Since it has been unhashed,
3488 * there should be no danger of the refcount going back up again at
3491 wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3493 release_all_access(s);
3494 if (s->st_stid.sc_file) {
3495 put_nfs4_file(s->st_stid.sc_file);
3496 s->st_stid.sc_file = NULL;
3499 spin_lock(&nn->client_lock);
3500 last = oo->oo_last_closed_stid;
3501 oo->oo_last_closed_stid = s;
3502 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
3503 oo->oo_time = get_seconds();
3504 spin_unlock(&nn->client_lock);
3506 nfs4_put_stid(&last->st_stid);
3509 /* search file_hashtbl[] for file */
3510 static struct nfs4_file *
3511 find_file_locked(struct knfsd_fh *fh, unsigned int hashval)
3513 struct nfs4_file *fp;
3515 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash) {
3516 if (fh_match(&fp->fi_fhandle, fh)) {
3517 if (atomic_inc_not_zero(&fp->fi_ref))
3525 find_file(struct knfsd_fh *fh)
3527 struct nfs4_file *fp;
3528 unsigned int hashval = file_hashval(fh);
3531 fp = find_file_locked(fh, hashval);
3536 static struct nfs4_file *
3537 find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
3539 struct nfs4_file *fp;
3540 unsigned int hashval = file_hashval(fh);
3543 fp = find_file_locked(fh, hashval);
3548 spin_lock(&state_lock);
3549 fp = find_file_locked(fh, hashval);
3550 if (likely(fp == NULL)) {
3551 nfsd4_init_file(fh, hashval, new);
3554 spin_unlock(&state_lock);
3560 * Called to check deny when READ with all zero stateid or
3561 * WRITE with all zero or all one stateid
3564 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3566 struct nfs4_file *fp;
3567 __be32 ret = nfs_ok;
3569 fp = find_file(¤t_fh->fh_handle);
3572 /* Check for conflicting share reservations */
3573 spin_lock(&fp->fi_lock);
3574 if (fp->fi_share_deny & deny_type)
3575 ret = nfserr_locked;
3576 spin_unlock(&fp->fi_lock);
3581 static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
3583 struct nfs4_delegation *dp = cb_to_delegation(cb);
3584 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3587 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
3590 * We can't do this in nfsd_break_deleg_cb because it is
3591 * already holding inode->i_lock.
3593 * If the dl_time != 0, then we know that it has already been
3594 * queued for a lease break. Don't queue it again.
3596 spin_lock(&state_lock);
3597 if (dp->dl_time == 0) {
3598 dp->dl_time = get_seconds();
3599 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
3601 spin_unlock(&state_lock);
3604 static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
3605 struct rpc_task *task)
3607 struct nfs4_delegation *dp = cb_to_delegation(cb);
3609 if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID)
3612 switch (task->tk_status) {
3616 case -NFS4ERR_BAD_STATEID:
3618 * Race: client probably got cb_recall before open reply
3619 * granting delegation.
3621 if (dp->dl_retries--) {
3622 rpc_delay(task, 2 * HZ);
3631 static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
3633 struct nfs4_delegation *dp = cb_to_delegation(cb);
3635 nfs4_put_stid(&dp->dl_stid);
3638 static struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
3639 .prepare = nfsd4_cb_recall_prepare,
3640 .done = nfsd4_cb_recall_done,
3641 .release = nfsd4_cb_recall_release,
3644 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3647 * We're assuming the state code never drops its reference
3648 * without first removing the lease. Since we're in this lease
3649 * callback (and since the lease code is serialized by the kernel
3650 * lock) we know the server hasn't removed the lease yet, we know
3651 * it's safe to take a reference.
3653 atomic_inc(&dp->dl_stid.sc_count);
3654 nfsd4_run_cb(&dp->dl_recall);
3657 /* Called from break_lease() with i_lock held. */
3659 nfsd_break_deleg_cb(struct file_lock *fl)
3662 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3663 struct nfs4_delegation *dp;
3666 WARN(1, "(%p)->fl_owner NULL\n", fl);
3669 if (fp->fi_had_conflict) {
3670 WARN(1, "duplicate break on %p\n", fp);
3674 * We don't want the locks code to timeout the lease for us;
3675 * we'll remove it ourself if a delegation isn't returned
3678 fl->fl_break_time = 0;
3680 spin_lock(&fp->fi_lock);
3681 fp->fi_had_conflict = true;
3683 * If there are no delegations on the list, then return true
3684 * so that the lease code will go ahead and delete it.
3686 if (list_empty(&fp->fi_delegations))
3689 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3690 nfsd_break_one_deleg(dp);
3691 spin_unlock(&fp->fi_lock);
3696 nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
3697 struct list_head *dispose)
3700 return lease_modify(onlist, arg, dispose);
3705 static const struct lock_manager_operations nfsd_lease_mng_ops = {
3706 .lm_break = nfsd_break_deleg_cb,
3707 .lm_change = nfsd_change_deleg_cb,
3710 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3712 if (nfsd4_has_session(cstate))
3714 if (seqid == so->so_seqid - 1)
3715 return nfserr_replay_me;
3716 if (seqid == so->so_seqid)
3718 return nfserr_bad_seqid;
3721 static __be32 lookup_clientid(clientid_t *clid,
3722 struct nfsd4_compound_state *cstate,
3723 struct nfsd_net *nn)
3725 struct nfs4_client *found;
3728 found = cstate->clp;
3729 if (!same_clid(&found->cl_clientid, clid))
3730 return nfserr_stale_clientid;
3734 if (STALE_CLIENTID(clid, nn))
3735 return nfserr_stale_clientid;
3738 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3739 * cached already then we know this is for is for v4.0 and "sessions"
3742 WARN_ON_ONCE(cstate->session);
3743 spin_lock(&nn->client_lock);
3744 found = find_confirmed_client(clid, false, nn);
3746 spin_unlock(&nn->client_lock);
3747 return nfserr_expired;
3749 atomic_inc(&found->cl_refcount);
3750 spin_unlock(&nn->client_lock);
3752 /* Cache the nfs4_client in cstate! */
3753 cstate->clp = found;
3758 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
3759 struct nfsd4_open *open, struct nfsd_net *nn)
3761 clientid_t *clientid = &open->op_clientid;
3762 struct nfs4_client *clp = NULL;
3763 unsigned int strhashval;
3764 struct nfs4_openowner *oo = NULL;
3767 if (STALE_CLIENTID(&open->op_clientid, nn))
3768 return nfserr_stale_clientid;
3770 * In case we need it later, after we've already created the
3771 * file and don't want to risk a further failure:
3773 open->op_file = nfsd4_alloc_file();
3774 if (open->op_file == NULL)
3775 return nfserr_jukebox;
3777 status = lookup_clientid(clientid, cstate, nn);
3782 strhashval = ownerstr_hashval(&open->op_owner);
3783 oo = find_openstateowner_str(strhashval, open, clp);
3784 open->op_openowner = oo;
3788 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
3789 /* Replace unconfirmed owners without checking for replay. */
3790 release_openowner(oo);
3791 open->op_openowner = NULL;
3794 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3799 oo = alloc_init_open_stateowner(strhashval, open, cstate);
3801 return nfserr_jukebox;
3802 open->op_openowner = oo;
3804 open->op_stp = nfs4_alloc_open_stateid(clp);
3806 return nfserr_jukebox;
3808 if (nfsd4_has_session(cstate) &&
3809 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
3810 open->op_odstate = alloc_clnt_odstate(clp);
3811 if (!open->op_odstate)
3812 return nfserr_jukebox;
3818 static inline __be32
3819 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3821 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3822 return nfserr_openmode;
3827 static int share_access_to_flags(u32 share_access)
3829 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3832 static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
3834 struct nfs4_stid *ret;
3836 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
3839 return delegstateid(ret);
3842 static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3844 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3845 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3849 nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
3850 struct nfs4_delegation **dp)
3853 __be32 status = nfserr_bad_stateid;
3854 struct nfs4_delegation *deleg;
3856 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3859 flags = share_access_to_flags(open->op_share_access);
3860 status = nfs4_check_delegmode(deleg, flags);
3862 nfs4_put_stid(&deleg->dl_stid);
3867 if (!nfsd4_is_deleg_cur(open))
3871 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
3875 static inline int nfs4_access_to_access(u32 nfs4_access)
3879 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3880 flags |= NFSD_MAY_READ;
3881 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3882 flags |= NFSD_MAY_WRITE;
3886 static inline __be32
3887 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3888 struct nfsd4_open *open)
3890 struct iattr iattr = {
3891 .ia_valid = ATTR_SIZE,
3894 if (!open->op_truncate)
3896 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
3897 return nfserr_inval;
3898 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3901 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
3902 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3903 struct nfsd4_open *open)
3905 struct file *filp = NULL;
3907 int oflag = nfs4_access_to_omode(open->op_share_access);
3908 int access = nfs4_access_to_access(open->op_share_access);
3909 unsigned char old_access_bmap, old_deny_bmap;
3911 spin_lock(&fp->fi_lock);
3914 * Are we trying to set a deny mode that would conflict with
3917 status = nfs4_file_check_deny(fp, open->op_share_deny);
3918 if (status != nfs_ok) {
3919 spin_unlock(&fp->fi_lock);
3923 /* set access to the file */
3924 status = nfs4_file_get_access(fp, open->op_share_access);
3925 if (status != nfs_ok) {
3926 spin_unlock(&fp->fi_lock);
3930 /* Set access bits in stateid */
3931 old_access_bmap = stp->st_access_bmap;
3932 set_access(open->op_share_access, stp);
3934 /* Set new deny mask */
3935 old_deny_bmap = stp->st_deny_bmap;
3936 set_deny(open->op_share_deny, stp);
3937 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3939 if (!fp->fi_fds[oflag]) {
3940 spin_unlock(&fp->fi_lock);
3941 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
3943 goto out_put_access;
3944 spin_lock(&fp->fi_lock);
3945 if (!fp->fi_fds[oflag]) {
3946 fp->fi_fds[oflag] = filp;
3950 spin_unlock(&fp->fi_lock);
3954 status = nfsd4_truncate(rqstp, cur_fh, open);
3956 goto out_put_access;
3960 stp->st_access_bmap = old_access_bmap;
3961 nfs4_file_put_access(fp, open->op_share_access);
3962 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3967 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)
3970 unsigned char old_deny_bmap = stp->st_deny_bmap;
3972 if (!test_access(open->op_share_access, stp))
3973 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
3975 /* test and set deny mode */
3976 spin_lock(&fp->fi_lock);
3977 status = nfs4_file_check_deny(fp, open->op_share_deny);
3978 if (status == nfs_ok) {
3979 set_deny(open->op_share_deny, stp);
3980 fp->fi_share_deny |=
3981 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3983 spin_unlock(&fp->fi_lock);
3985 if (status != nfs_ok)
3988 status = nfsd4_truncate(rqstp, cur_fh, open);
3989 if (status != nfs_ok)
3990 reset_union_bmap_deny(old_deny_bmap, stp);
3994 /* Should we give out recallable state?: */
3995 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3997 if (clp->cl_cb_state == NFSD4_CB_UP)
4000 * In the sessions case, since we don't have to establish a
4001 * separate connection for callbacks, we assume it's OK
4002 * until we hear otherwise:
4004 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
4007 static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
4009 struct file_lock *fl;
4011 fl = locks_alloc_lock();
4014 fl->fl_lmops = &nfsd_lease_mng_ops;
4015 fl->fl_flags = FL_DELEG;
4016 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
4017 fl->fl_end = OFFSET_MAX;
4018 fl->fl_owner = (fl_owner_t)fp;
4019 fl->fl_pid = current->tgid;
4024 * nfs4_setlease - Obtain a delegation by requesting lease from vfs layer
4025 * @dp: a pointer to the nfs4_delegation we're adding.
4028 * On success: Return code will be 0 on success.
4030 * On error: -EAGAIN if there was an existing delegation.
4031 * nonzero if there is an error in other cases.
4035 static int nfs4_setlease(struct nfs4_delegation *dp)
4037 struct nfs4_file *fp = dp->dl_stid.sc_file;
4038 struct file_lock *fl;
4042 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
4045 filp = find_readable_file(fp);
4047 /* We should always have a readable file here */
4049 locks_free_lock(fl);
4053 status = vfs_setlease(filp, fl->fl_type, &fl, NULL);
4055 locks_free_lock(fl);
4058 spin_lock(&state_lock);
4059 spin_lock(&fp->fi_lock);
4060 /* Did the lease get broken before we took the lock? */
4062 if (fp->fi_had_conflict)
4065 if (fp->fi_deleg_file) {
4066 status = hash_delegation_locked(dp, fp);
4069 fp->fi_deleg_file = filp;
4070 fp->fi_delegees = 0;
4071 status = hash_delegation_locked(dp, fp);
4072 spin_unlock(&fp->fi_lock);
4073 spin_unlock(&state_lock);
4075 /* Should never happen, this is a new fi_deleg_file */
4081 spin_unlock(&fp->fi_lock);
4082 spin_unlock(&state_lock);
4088 static struct nfs4_delegation *
4089 nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
4090 struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate)
4093 struct nfs4_delegation *dp;
4095 if (fp->fi_had_conflict)
4096 return ERR_PTR(-EAGAIN);
4098 spin_lock(&state_lock);
4099 spin_lock(&fp->fi_lock);
4100 status = nfs4_get_existing_delegation(clp, fp);
4101 spin_unlock(&fp->fi_lock);
4102 spin_unlock(&state_lock);
4105 return ERR_PTR(status);
4107 dp = alloc_init_deleg(clp, fh, odstate);
4109 return ERR_PTR(-ENOMEM);
4112 spin_lock(&state_lock);
4113 spin_lock(&fp->fi_lock);
4114 dp->dl_stid.sc_file = fp;
4115 if (!fp->fi_deleg_file) {
4116 spin_unlock(&fp->fi_lock);
4117 spin_unlock(&state_lock);
4118 status = nfs4_setlease(dp);
4121 if (fp->fi_had_conflict) {
4125 status = hash_delegation_locked(dp, fp);
4127 spin_unlock(&fp->fi_lock);
4128 spin_unlock(&state_lock);
4131 put_clnt_odstate(dp->dl_clnt_odstate);
4132 nfs4_put_stid(&dp->dl_stid);
4133 return ERR_PTR(status);
4138 static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
4140 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4141 if (status == -EAGAIN)
4142 open->op_why_no_deleg = WND4_CONTENTION;
4144 open->op_why_no_deleg = WND4_RESOURCE;
4145 switch (open->op_deleg_want) {
4146 case NFS4_SHARE_WANT_READ_DELEG:
4147 case NFS4_SHARE_WANT_WRITE_DELEG:
4148 case NFS4_SHARE_WANT_ANY_DELEG:
4150 case NFS4_SHARE_WANT_CANCEL:
4151 open->op_why_no_deleg = WND4_CANCELLED;
4153 case NFS4_SHARE_WANT_NO_DELEG:
4160 * Attempt to hand out a delegation.
4162 * Note we don't support write delegations, and won't until the vfs has
4163 * proper support for them.
4166 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
4167 struct nfs4_ol_stateid *stp)
4169 struct nfs4_delegation *dp;
4170 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
4171 struct nfs4_client *clp = stp->st_stid.sc_client;
4175 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
4176 open->op_recall = 0;
4177 switch (open->op_claim_type) {
4178 case NFS4_OPEN_CLAIM_PREVIOUS:
4180 open->op_recall = 1;
4181 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
4184 case NFS4_OPEN_CLAIM_NULL:
4185 case NFS4_OPEN_CLAIM_FH:
4187 * Let's not give out any delegations till everyone's
4188 * had the chance to reclaim theirs, *and* until
4189 * NLM locks have all been reclaimed:
4191 if (locks_in_grace(clp->net))
4193 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
4196 * Also, if the file was opened for write or
4197 * create, there's a good chance the client's
4198 * about to write to it, resulting in an
4199 * immediate recall (since we don't support
4200 * write delegations):
4202 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
4204 if (open->op_create == NFS4_OPEN_CREATE)
4210 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate);
4214 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
4216 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
4217 STATEID_VAL(&dp->dl_stid.sc_stateid));
4218 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
4219 nfs4_put_stid(&dp->dl_stid);
4222 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
4223 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
4224 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
4225 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
4226 open->op_recall = 1;
4229 /* 4.1 client asking for a delegation? */
4230 if (open->op_deleg_want)
4231 nfsd4_open_deleg_none_ext(open, status);
4235 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
4236 struct nfs4_delegation *dp)
4238 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
4239 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4240 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4241 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
4242 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
4243 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4244 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4245 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
4247 /* Otherwise the client must be confused wanting a delegation
4248 * it already has, therefore we don't return
4249 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
4254 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
4256 struct nfsd4_compoundres *resp = rqstp->rq_resp;
4257 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
4258 struct nfs4_file *fp = NULL;
4259 struct nfs4_ol_stateid *stp = NULL;
4260 struct nfs4_ol_stateid *swapstp = NULL;
4261 struct nfs4_delegation *dp = NULL;
4265 * Lookup file; if found, lookup stateid and check open request,
4266 * and check for delegations in the process of being recalled.
4267 * If not found, create the nfs4_file struct
4269 fp = find_or_add_file(open->op_file, ¤t_fh->fh_handle);
4270 if (fp != open->op_file) {
4271 status = nfs4_check_deleg(cl, open, &dp);
4274 spin_lock(&fp->fi_lock);
4275 stp = nfsd4_find_existing_open(fp, open);
4276 spin_unlock(&fp->fi_lock);
4278 open->op_file = NULL;
4279 status = nfserr_bad_stateid;
4280 if (nfsd4_is_deleg_cur(open))
4285 * OPEN the file, or upgrade an existing OPEN.
4286 * If truncate fails, the OPEN fails.
4289 /* Stateid was found, this is an OPEN upgrade */
4290 mutex_lock(&stp->st_mutex);
4291 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
4293 mutex_unlock(&stp->st_mutex);
4298 open->op_stp = NULL;
4300 * init_open_stateid() either returns a locked stateid
4301 * it found, or initializes and locks the new one we passed in
4303 swapstp = init_open_stateid(stp, fp, open);
4305 nfs4_put_stid(&stp->st_stid);
4307 status = nfs4_upgrade_open(rqstp, fp, current_fh,
4310 mutex_unlock(&stp->st_mutex);
4315 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4317 mutex_unlock(&stp->st_mutex);
4318 release_open_stateid(stp);
4322 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
4324 if (stp->st_clnt_odstate == open->op_odstate)
4325 open->op_odstate = NULL;
4328 nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
4329 mutex_unlock(&stp->st_mutex);
4331 if (nfsd4_has_session(&resp->cstate)) {
4332 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4333 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4334 open->op_why_no_deleg = WND4_NOT_WANTED;
4340 * Attempt to hand out a delegation. No error return, because the
4341 * OPEN succeeds even if we fail.
4343 nfs4_open_delegation(current_fh, open, stp);
4347 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
4348 STATEID_VAL(&stp->st_stid.sc_stateid));
4350 /* 4.1 client trying to upgrade/downgrade delegation? */
4351 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
4352 open->op_deleg_want)
4353 nfsd4_deleg_xgrade_none_ext(open, dp);
4357 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
4358 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
4360 * To finish the open response, we just need to set the rflags.
4362 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
4363 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
4364 !nfsd4_has_session(&resp->cstate))
4365 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
4367 nfs4_put_stid(&dp->dl_stid);
4369 nfs4_put_stid(&stp->st_stid);
4374 void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
4375 struct nfsd4_open *open)
4377 if (open->op_openowner) {
4378 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
4380 nfsd4_cstate_assign_replay(cstate, so);
4381 nfs4_put_stateowner(so);
4384 kmem_cache_free(file_slab, open->op_file);
4386 nfs4_put_stid(&open->op_stp->st_stid);
4387 if (open->op_odstate)
4388 kmem_cache_free(odstate_slab, open->op_odstate);
4392 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4395 struct nfs4_client *clp;
4397 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4399 dprintk("process_renew(%08x/%08x): starting\n",
4400 clid->cl_boot, clid->cl_id);
4401 status = lookup_clientid(clid, cstate, nn);
4405 status = nfserr_cb_path_down;
4406 if (!list_empty(&clp->cl_delegations)
4407 && clp->cl_cb_state != NFSD4_CB_UP)
4415 nfsd4_end_grace(struct nfsd_net *nn)
4417 /* do nothing if grace period already ended */
4418 if (nn->grace_ended)
4421 dprintk("NFSD: end of grace period\n");
4422 nn->grace_ended = true;
4424 * If the server goes down again right now, an NFSv4
4425 * client will still be allowed to reclaim after it comes back up,
4426 * even if it hasn't yet had a chance to reclaim state this time.
4429 nfsd4_record_grace_done(nn);
4431 * At this point, NFSv4 clients can still reclaim. But if the
4432 * server crashes, any that have not yet reclaimed will be out
4433 * of luck on the next boot.
4435 * (NFSv4.1+ clients are considered to have reclaimed once they
4436 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
4437 * have reclaimed after their first OPEN.)
4439 locks_end_grace(&nn->nfsd4_manager);
4441 * At this point, and once lockd and/or any other containers
4442 * exit their grace period, further reclaims will fail and
4443 * regular locking can resume.
4448 nfs4_laundromat(struct nfsd_net *nn)
4450 struct nfs4_client *clp;
4451 struct nfs4_openowner *oo;
4452 struct nfs4_delegation *dp;
4453 struct nfs4_ol_stateid *stp;
4454 struct list_head *pos, *next, reaplist;
4455 time_t cutoff = get_seconds() - nn->nfsd4_lease;
4456 time_t t, new_timeo = nn->nfsd4_lease;
4458 dprintk("NFSD: laundromat service - starting\n");
4459 nfsd4_end_grace(nn);
4460 INIT_LIST_HEAD(&reaplist);
4461 spin_lock(&nn->client_lock);
4462 list_for_each_safe(pos, next, &nn->client_lru) {
4463 clp = list_entry(pos, struct nfs4_client, cl_lru);
4464 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4465 t = clp->cl_time - cutoff;
4466 new_timeo = min(new_timeo, t);
4469 if (mark_client_expired_locked(clp)) {
4470 dprintk("NFSD: client in use (clientid %08x)\n",
4471 clp->cl_clientid.cl_id);
4474 list_add(&clp->cl_lru, &reaplist);
4476 spin_unlock(&nn->client_lock);
4477 list_for_each_safe(pos, next, &reaplist) {
4478 clp = list_entry(pos, struct nfs4_client, cl_lru);
4479 dprintk("NFSD: purging unused client (clientid %08x)\n",
4480 clp->cl_clientid.cl_id);
4481 list_del_init(&clp->cl_lru);
4484 spin_lock(&state_lock);
4485 list_for_each_safe(pos, next, &nn->del_recall_lru) {
4486 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4487 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
4488 t = dp->dl_time - cutoff;
4489 new_timeo = min(new_timeo, t);
4492 WARN_ON(!unhash_delegation_locked(dp));
4493 list_add(&dp->dl_recall_lru, &reaplist);
4495 spin_unlock(&state_lock);
4496 while (!list_empty(&reaplist)) {
4497 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4499 list_del_init(&dp->dl_recall_lru);
4500 revoke_delegation(dp);
4503 spin_lock(&nn->client_lock);
4504 while (!list_empty(&nn->close_lru)) {
4505 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
4507 if (time_after((unsigned long)oo->oo_time,
4508 (unsigned long)cutoff)) {
4509 t = oo->oo_time - cutoff;
4510 new_timeo = min(new_timeo, t);
4513 list_del_init(&oo->oo_close_lru);
4514 stp = oo->oo_last_closed_stid;
4515 oo->oo_last_closed_stid = NULL;
4516 spin_unlock(&nn->client_lock);
4517 nfs4_put_stid(&stp->st_stid);
4518 spin_lock(&nn->client_lock);
4520 spin_unlock(&nn->client_lock);
4522 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
4526 static struct workqueue_struct *laundry_wq;
4527 static void laundromat_main(struct work_struct *);
4530 laundromat_main(struct work_struct *laundry)
4533 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
4535 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4538 t = nfs4_laundromat(nn);
4539 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
4540 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
4543 static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
4545 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
4546 return nfserr_bad_stateid;
4551 access_permit_read(struct nfs4_ol_stateid *stp)
4553 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4554 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4555 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
4559 access_permit_write(struct nfs4_ol_stateid *stp)
4561 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4562 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
4566 __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
4568 __be32 status = nfserr_openmode;
4570 /* For lock stateid's, we test the parent open, not the lock: */
4571 if (stp->st_openstp)
4572 stp = stp->st_openstp;
4573 if ((flags & WR_STATE) && !access_permit_write(stp))
4575 if ((flags & RD_STATE) && !access_permit_read(stp))
4582 static inline __be32
4583 check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
4585 if (ONE_STATEID(stateid) && (flags & RD_STATE))
4587 else if (opens_in_grace(net)) {
4588 /* Answer in remaining cases depends on existence of
4589 * conflicting state; so we must wait out the grace period. */
4590 return nfserr_grace;
4591 } else if (flags & WR_STATE)
4592 return nfs4_share_conflict(current_fh,
4593 NFS4_SHARE_DENY_WRITE);
4594 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4595 return nfs4_share_conflict(current_fh,
4596 NFS4_SHARE_DENY_READ);
4600 * Allow READ/WRITE during grace period on recovered state only for files
4601 * that are not able to provide mandatory locking.
4604 grace_disallows_io(struct net *net, struct inode *inode)
4606 return opens_in_grace(net) && mandatory_lock(inode);
4609 /* Returns true iff a is later than b: */
4610 static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4612 return (s32)(a->si_generation - b->si_generation) > 0;
4615 static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
4618 * When sessions are used the stateid generation number is ignored
4621 if (has_session && in->si_generation == 0)
4624 if (in->si_generation == ref->si_generation)
4627 /* If the client sends us a stateid from the future, it's buggy: */
4628 if (stateid_generation_after(in, ref))
4629 return nfserr_bad_stateid;
4631 * However, we could see a stateid from the past, even from a
4632 * non-buggy client. For example, if the client sends a lock
4633 * while some IO is outstanding, the lock may bump si_generation
4634 * while the IO is still in flight. The client could avoid that
4635 * situation by waiting for responses on all the IO requests,
4636 * but better performance may result in retrying IO that
4637 * receives an old_stateid error if requests are rarely
4638 * reordered in flight:
4640 return nfserr_old_stateid;
4643 static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
4645 if (ols->st_stateowner->so_is_open_owner &&
4646 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
4647 return nfserr_bad_stateid;
4651 static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
4653 struct nfs4_stid *s;
4654 __be32 status = nfserr_bad_stateid;
4656 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4658 /* Client debugging aid. */
4659 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4660 char addr_str[INET6_ADDRSTRLEN];
4661 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4663 pr_warn_ratelimited("NFSD: client %s testing state ID "
4664 "with incorrect client ID\n", addr_str);
4667 spin_lock(&cl->cl_lock);
4668 s = find_stateid_locked(cl, stateid);
4671 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
4674 switch (s->sc_type) {
4675 case NFS4_DELEG_STID:
4678 case NFS4_REVOKED_DELEG_STID:
4679 status = nfserr_deleg_revoked;
4681 case NFS4_OPEN_STID:
4682 case NFS4_LOCK_STID:
4683 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
4686 printk("unknown stateid type %x\n", s->sc_type);
4688 case NFS4_CLOSED_STID:
4689 case NFS4_CLOSED_DELEG_STID:
4690 status = nfserr_bad_stateid;
4693 spin_unlock(&cl->cl_lock);
4698 nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4699 stateid_t *stateid, unsigned char typemask,
4700 struct nfs4_stid **s, struct nfsd_net *nn)
4704 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4705 return nfserr_bad_stateid;
4706 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
4707 if (status == nfserr_stale_clientid) {
4708 if (cstate->session)
4709 return nfserr_bad_stateid;
4710 return nfserr_stale_stateid;
4714 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
4716 return nfserr_bad_stateid;
4720 static struct file *
4721 nfs4_find_file(struct nfs4_stid *s, int flags)
4726 switch (s->sc_type) {
4727 case NFS4_DELEG_STID:
4728 if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
4730 return get_file(s->sc_file->fi_deleg_file);
4731 case NFS4_OPEN_STID:
4732 case NFS4_LOCK_STID:
4733 if (flags & RD_STATE)
4734 return find_readable_file(s->sc_file);
4736 return find_writeable_file(s->sc_file);
4744 nfs4_check_olstateid(struct svc_fh *fhp, struct nfs4_ol_stateid *ols, int flags)
4748 status = nfsd4_check_openowner_confirmed(ols);
4751 return nfs4_check_openmode(ols, flags);
4755 nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
4756 struct file **filpp, bool *tmp_file, int flags)
4758 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
4762 file = nfs4_find_file(s, flags);
4764 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
4765 acc | NFSD_MAY_OWNER_OVERRIDE);
4773 status = nfsd_open(rqstp, fhp, S_IFREG, acc, filpp);
4785 * Checks for stateid operations
4788 nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
4789 struct nfsd4_compound_state *cstate, stateid_t *stateid,
4790 int flags, struct file **filpp, bool *tmp_file)
4792 struct svc_fh *fhp = &cstate->current_fh;
4793 struct inode *ino = d_inode(fhp->fh_dentry);
4794 struct net *net = SVC_NET(rqstp);
4795 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4796 struct nfs4_stid *s = NULL;
4804 if (grace_disallows_io(net, ino))
4805 return nfserr_grace;
4807 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
4808 status = check_special_stateids(net, fhp, stateid, flags);
4812 status = nfsd4_lookup_stateid(cstate, stateid,
4813 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
4817 status = check_stateid_generation(stateid, &s->sc_stateid,
4818 nfsd4_has_session(cstate));
4822 switch (s->sc_type) {
4823 case NFS4_DELEG_STID:
4824 status = nfs4_check_delegmode(delegstateid(s), flags);
4826 case NFS4_OPEN_STID:
4827 case NFS4_LOCK_STID:
4828 status = nfs4_check_olstateid(fhp, openlockstateid(s), flags);
4831 status = nfserr_bad_stateid;
4836 status = nfs4_check_fh(fhp, s);
4839 if (!status && filpp)
4840 status = nfs4_check_file(rqstp, fhp, s, filpp, tmp_file, flags);
4848 * Test if the stateid is valid
4851 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4852 struct nfsd4_test_stateid *test_stateid)
4854 struct nfsd4_test_stateid_id *stateid;
4855 struct nfs4_client *cl = cstate->session->se_client;
4857 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
4858 stateid->ts_id_status =
4859 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
4865 nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
4867 struct nfs4_ol_stateid *stp = openlockstateid(s);
4870 mutex_lock(&stp->st_mutex);
4872 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4876 ret = nfserr_locks_held;
4877 if (check_for_locks(stp->st_stid.sc_file,
4878 lockowner(stp->st_stateowner)))
4881 release_lock_stateid(stp);
4885 mutex_unlock(&stp->st_mutex);
4891 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4892 struct nfsd4_free_stateid *free_stateid)
4894 stateid_t *stateid = &free_stateid->fr_stateid;
4895 struct nfs4_stid *s;
4896 struct nfs4_delegation *dp;
4897 struct nfs4_client *cl = cstate->session->se_client;
4898 __be32 ret = nfserr_bad_stateid;
4900 spin_lock(&cl->cl_lock);
4901 s = find_stateid_locked(cl, stateid);
4904 switch (s->sc_type) {
4905 case NFS4_DELEG_STID:
4906 ret = nfserr_locks_held;
4908 case NFS4_OPEN_STID:
4909 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4912 ret = nfserr_locks_held;
4914 case NFS4_LOCK_STID:
4915 atomic_inc(&s->sc_count);
4916 spin_unlock(&cl->cl_lock);
4917 ret = nfsd4_free_lock_stateid(stateid, s);
4919 case NFS4_REVOKED_DELEG_STID:
4920 dp = delegstateid(s);
4921 list_del_init(&dp->dl_recall_lru);
4922 spin_unlock(&cl->cl_lock);
4926 /* Default falls through and returns nfserr_bad_stateid */
4929 spin_unlock(&cl->cl_lock);
4937 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4938 RD_STATE : WR_STATE;
4941 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
4943 struct svc_fh *current_fh = &cstate->current_fh;
4944 struct nfs4_stateowner *sop = stp->st_stateowner;
4947 status = nfsd4_check_seqid(cstate, sop, seqid);
4950 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4951 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
4953 * "Closed" stateid's exist *only* to return
4954 * nfserr_replay_me from the previous step, and
4955 * revoked delegations are kept only for free_stateid.
4957 return nfserr_bad_stateid;
4958 mutex_lock(&stp->st_mutex);
4959 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4960 if (status == nfs_ok)
4961 status = nfs4_check_fh(current_fh, &stp->st_stid);
4962 if (status != nfs_ok)
4963 mutex_unlock(&stp->st_mutex);
4968 * Checks for sequence id mutating operations.
4971 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4972 stateid_t *stateid, char typemask,
4973 struct nfs4_ol_stateid **stpp,
4974 struct nfsd_net *nn)
4977 struct nfs4_stid *s;
4978 struct nfs4_ol_stateid *stp = NULL;
4980 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4981 seqid, STATEID_VAL(stateid));
4984 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
4987 stp = openlockstateid(s);
4988 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
4990 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
4994 nfs4_put_stid(&stp->st_stid);
4998 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4999 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
5002 struct nfs4_openowner *oo;
5003 struct nfs4_ol_stateid *stp;
5005 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
5006 NFS4_OPEN_STID, &stp, nn);
5009 oo = openowner(stp->st_stateowner);
5010 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
5011 mutex_unlock(&stp->st_mutex);
5012 nfs4_put_stid(&stp->st_stid);
5013 return nfserr_bad_stateid;
5020 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5021 struct nfsd4_open_confirm *oc)
5024 struct nfs4_openowner *oo;
5025 struct nfs4_ol_stateid *stp;
5026 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5028 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
5029 cstate->current_fh.fh_dentry);
5031 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
5035 status = nfs4_preprocess_seqid_op(cstate,
5036 oc->oc_seqid, &oc->oc_req_stateid,
5037 NFS4_OPEN_STID, &stp, nn);
5040 oo = openowner(stp->st_stateowner);
5041 status = nfserr_bad_stateid;
5042 if (oo->oo_flags & NFS4_OO_CONFIRMED) {
5043 mutex_unlock(&stp->st_mutex);
5046 oo->oo_flags |= NFS4_OO_CONFIRMED;
5047 nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
5048 mutex_unlock(&stp->st_mutex);
5049 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
5050 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
5052 nfsd4_client_record_create(oo->oo_owner.so_client);
5055 nfs4_put_stid(&stp->st_stid);
5057 nfsd4_bump_seqid(cstate, status);
5061 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
5063 if (!test_access(access, stp))
5065 nfs4_file_put_access(stp->st_stid.sc_file, access);
5066 clear_access(access, stp);
5069 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
5071 switch (to_access) {
5072 case NFS4_SHARE_ACCESS_READ:
5073 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
5074 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
5076 case NFS4_SHARE_ACCESS_WRITE:
5077 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
5078 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
5080 case NFS4_SHARE_ACCESS_BOTH:
5088 nfsd4_open_downgrade(struct svc_rqst *rqstp,
5089 struct nfsd4_compound_state *cstate,
5090 struct nfsd4_open_downgrade *od)
5093 struct nfs4_ol_stateid *stp;
5094 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5096 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
5097 cstate->current_fh.fh_dentry);
5099 /* We don't yet support WANT bits: */
5100 if (od->od_deleg_want)
5101 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
5104 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
5105 &od->od_stateid, &stp, nn);
5108 status = nfserr_inval;
5109 if (!test_access(od->od_share_access, stp)) {
5110 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
5111 stp->st_access_bmap, od->od_share_access);
5114 if (!test_deny(od->od_share_deny, stp)) {
5115 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
5116 stp->st_deny_bmap, od->od_share_deny);
5119 nfs4_stateid_downgrade(stp, od->od_share_access);
5120 reset_union_bmap_deny(od->od_share_deny, stp);
5121 nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
5124 mutex_unlock(&stp->st_mutex);
5125 nfs4_put_stid(&stp->st_stid);
5127 nfsd4_bump_seqid(cstate, status);
5131 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
5133 struct nfs4_client *clp = s->st_stid.sc_client;
5135 LIST_HEAD(reaplist);
5137 s->st_stid.sc_type = NFS4_CLOSED_STID;
5138 spin_lock(&clp->cl_lock);
5139 unhashed = unhash_open_stateid(s, &reaplist);
5141 if (clp->cl_minorversion) {
5143 put_ol_stateid_locked(s, &reaplist);
5144 spin_unlock(&clp->cl_lock);
5145 free_ol_stateid_reaplist(&reaplist);
5147 spin_unlock(&clp->cl_lock);
5148 free_ol_stateid_reaplist(&reaplist);
5150 move_to_close_lru(s, clp->net);
5155 * nfs4_unlock_state() called after encode
5158 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5159 struct nfsd4_close *close)
5162 struct nfs4_ol_stateid *stp;
5163 struct net *net = SVC_NET(rqstp);
5164 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5166 dprintk("NFSD: nfsd4_close on file %pd\n",
5167 cstate->current_fh.fh_dentry);
5169 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
5171 NFS4_OPEN_STID|NFS4_CLOSED_STID,
5173 nfsd4_bump_seqid(cstate, status);
5176 nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
5177 mutex_unlock(&stp->st_mutex);
5179 nfsd4_close_open_stateid(stp);
5181 /* put reference from nfs4_preprocess_seqid_op */
5182 nfs4_put_stid(&stp->st_stid);
5188 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5189 struct nfsd4_delegreturn *dr)
5191 struct nfs4_delegation *dp;
5192 stateid_t *stateid = &dr->dr_stateid;
5193 struct nfs4_stid *s;
5195 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5197 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
5200 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
5203 dp = delegstateid(s);
5204 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
5208 destroy_delegation(dp);
5210 nfs4_put_stid(&dp->dl_stid);
5216 end_offset(u64 start, u64 len)
5221 return end >= start ? end: NFS4_MAX_UINT64;
5224 /* last octet in a range */
5226 last_byte_offset(u64 start, u64 len)
5232 return end > start ? end - 1: NFS4_MAX_UINT64;
5236 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
5237 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
5238 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
5239 * locking, this prevents us from being completely protocol-compliant. The
5240 * real solution to this problem is to start using unsigned file offsets in
5241 * the VFS, but this is a very deep change!
5244 nfs4_transform_lock_offset(struct file_lock *lock)
5246 if (lock->fl_start < 0)
5247 lock->fl_start = OFFSET_MAX;
5248 if (lock->fl_end < 0)
5249 lock->fl_end = OFFSET_MAX;
5253 nfsd4_fl_get_owner(fl_owner_t owner)
5255 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
5257 nfs4_get_stateowner(&lo->lo_owner);
5262 nfsd4_fl_put_owner(fl_owner_t owner)
5264 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
5267 nfs4_put_stateowner(&lo->lo_owner);
5270 static const struct lock_manager_operations nfsd_posix_mng_ops = {
5271 .lm_get_owner = nfsd4_fl_get_owner,
5272 .lm_put_owner = nfsd4_fl_put_owner,
5276 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
5278 struct nfs4_lockowner *lo;
5280 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
5281 lo = (struct nfs4_lockowner *) fl->fl_owner;
5282 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
5283 lo->lo_owner.so_owner.len, GFP_KERNEL);
5284 if (!deny->ld_owner.data)
5285 /* We just don't care that much */
5287 deny->ld_owner.len = lo->lo_owner.so_owner.len;
5288 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
5291 deny->ld_owner.len = 0;
5292 deny->ld_owner.data = NULL;
5293 deny->ld_clientid.cl_boot = 0;
5294 deny->ld_clientid.cl_id = 0;
5296 deny->ld_start = fl->fl_start;
5297 deny->ld_length = NFS4_MAX_UINT64;
5298 if (fl->fl_end != NFS4_MAX_UINT64)
5299 deny->ld_length = fl->fl_end - fl->fl_start + 1;
5300 deny->ld_type = NFS4_READ_LT;
5301 if (fl->fl_type != F_RDLCK)
5302 deny->ld_type = NFS4_WRITE_LT;
5305 static struct nfs4_lockowner *
5306 find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
5308 unsigned int strhashval = ownerstr_hashval(owner);
5309 struct nfs4_stateowner *so;
5311 lockdep_assert_held(&clp->cl_lock);
5313 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
5315 if (so->so_is_open_owner)
5317 if (same_owner_str(so, owner))
5318 return lockowner(nfs4_get_stateowner(so));
5323 static struct nfs4_lockowner *
5324 find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
5326 struct nfs4_lockowner *lo;
5328 spin_lock(&clp->cl_lock);
5329 lo = find_lockowner_str_locked(clp, owner);
5330 spin_unlock(&clp->cl_lock);
5334 static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
5336 unhash_lockowner_locked(lockowner(sop));
5339 static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
5341 struct nfs4_lockowner *lo = lockowner(sop);
5343 kmem_cache_free(lockowner_slab, lo);
5346 static const struct nfs4_stateowner_operations lockowner_ops = {
5347 .so_unhash = nfs4_unhash_lockowner,
5348 .so_free = nfs4_free_lockowner,
5352 * Alloc a lock owner structure.
5353 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
5356 * strhashval = ownerstr_hashval
5358 static struct nfs4_lockowner *
5359 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
5360 struct nfs4_ol_stateid *open_stp,
5361 struct nfsd4_lock *lock)
5363 struct nfs4_lockowner *lo, *ret;
5365 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
5368 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
5369 lo->lo_owner.so_is_open_owner = 0;
5370 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
5371 lo->lo_owner.so_ops = &lockowner_ops;
5372 spin_lock(&clp->cl_lock);
5373 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
5375 list_add(&lo->lo_owner.so_strhash,
5376 &clp->cl_ownerstr_hashtbl[strhashval]);
5379 nfs4_free_stateowner(&lo->lo_owner);
5381 spin_unlock(&clp->cl_lock);
5386 init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
5387 struct nfs4_file *fp, struct inode *inode,
5388 struct nfs4_ol_stateid *open_stp)
5390 struct nfs4_client *clp = lo->lo_owner.so_client;
5392 lockdep_assert_held(&clp->cl_lock);
5394 atomic_inc(&stp->st_stid.sc_count);
5395 stp->st_stid.sc_type = NFS4_LOCK_STID;
5396 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
5398 stp->st_stid.sc_file = fp;
5399 stp->st_stid.sc_free = nfs4_free_lock_stateid;
5400 stp->st_access_bmap = 0;
5401 stp->st_deny_bmap = open_stp->st_deny_bmap;
5402 stp->st_openstp = open_stp;
5403 mutex_init(&stp->st_mutex);
5404 list_add(&stp->st_locks, &open_stp->st_locks);
5405 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
5406 spin_lock(&fp->fi_lock);
5407 list_add(&stp->st_perfile, &fp->fi_stateids);
5408 spin_unlock(&fp->fi_lock);
5411 static struct nfs4_ol_stateid *
5412 find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5414 struct nfs4_ol_stateid *lst;
5415 struct nfs4_client *clp = lo->lo_owner.so_client;
5417 lockdep_assert_held(&clp->cl_lock);
5419 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
5420 if (lst->st_stid.sc_file == fp) {
5421 atomic_inc(&lst->st_stid.sc_count);
5428 static struct nfs4_ol_stateid *
5429 find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5430 struct inode *inode, struct nfs4_ol_stateid *ost,
5433 struct nfs4_stid *ns = NULL;
5434 struct nfs4_ol_stateid *lst;
5435 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5436 struct nfs4_client *clp = oo->oo_owner.so_client;
5438 spin_lock(&clp->cl_lock);
5439 lst = find_lock_stateid(lo, fi);
5441 spin_unlock(&clp->cl_lock);
5442 ns = nfs4_alloc_stid(clp, stateid_slab);
5446 spin_lock(&clp->cl_lock);
5447 lst = find_lock_stateid(lo, fi);
5449 lst = openlockstateid(ns);
5450 init_lock_stateid(lst, lo, fi, inode, ost);
5455 spin_unlock(&clp->cl_lock);
5462 check_lock_length(u64 offset, u64 length)
5464 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
5465 (length > ~offset)));
5468 static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
5470 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
5472 lockdep_assert_held(&fp->fi_lock);
5474 if (test_access(access, lock_stp))
5476 __nfs4_file_get_access(fp, access);
5477 set_access(access, lock_stp);
5481 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5482 struct nfs4_ol_stateid *ost,
5483 struct nfsd4_lock *lock,
5484 struct nfs4_ol_stateid **plst, bool *new)
5487 struct nfs4_file *fi = ost->st_stid.sc_file;
5488 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5489 struct nfs4_client *cl = oo->oo_owner.so_client;
5490 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
5491 struct nfs4_lockowner *lo;
5492 struct nfs4_ol_stateid *lst;
5493 unsigned int strhashval;
5496 lo = find_lockowner_str(cl, &lock->lk_new_owner);
5498 strhashval = ownerstr_hashval(&lock->lk_new_owner);
5499 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5501 return nfserr_jukebox;
5503 /* with an existing lockowner, seqids must be the same */
5504 status = nfserr_bad_seqid;
5505 if (!cstate->minorversion &&
5506 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
5511 lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
5513 status = nfserr_jukebox;
5517 mutex_lock(&lst->st_mutex);
5519 /* See if it's still hashed to avoid race with FREE_STATEID */
5520 spin_lock(&cl->cl_lock);
5521 hashed = !list_empty(&lst->st_perfile);
5522 spin_unlock(&cl->cl_lock);
5525 mutex_unlock(&lst->st_mutex);
5526 nfs4_put_stid(&lst->st_stid);
5532 nfs4_put_stateowner(&lo->lo_owner);
5540 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5541 struct nfsd4_lock *lock)
5543 struct nfs4_openowner *open_sop = NULL;
5544 struct nfs4_lockowner *lock_sop = NULL;
5545 struct nfs4_ol_stateid *lock_stp = NULL;
5546 struct nfs4_ol_stateid *open_stp = NULL;
5547 struct nfs4_file *fp;
5548 struct file *filp = NULL;
5549 struct file_lock *file_lock = NULL;
5550 struct file_lock *conflock = NULL;
5555 struct net *net = SVC_NET(rqstp);
5556 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5558 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5559 (long long) lock->lk_offset,
5560 (long long) lock->lk_length);
5562 if (check_lock_length(lock->lk_offset, lock->lk_length))
5563 return nfserr_inval;
5565 if ((status = fh_verify(rqstp, &cstate->current_fh,
5566 S_IFREG, NFSD_MAY_LOCK))) {
5567 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5571 if (lock->lk_is_new) {
5572 if (nfsd4_has_session(cstate))
5573 /* See rfc 5661 18.10.3: given clientid is ignored: */
5574 memcpy(&lock->lk_new_clientid,
5575 &cstate->session->se_client->cl_clientid,
5576 sizeof(clientid_t));
5578 status = nfserr_stale_clientid;
5579 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
5582 /* validate and update open stateid and open seqid */
5583 status = nfs4_preprocess_confirmed_seqid_op(cstate,
5584 lock->lk_new_open_seqid,
5585 &lock->lk_new_open_stateid,
5589 mutex_unlock(&open_stp->st_mutex);
5590 open_sop = openowner(open_stp->st_stateowner);
5591 status = nfserr_bad_stateid;
5592 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
5593 &lock->lk_new_clientid))
5595 status = lookup_or_create_lock_state(cstate, open_stp, lock,
5598 status = nfs4_preprocess_seqid_op(cstate,
5599 lock->lk_old_lock_seqid,
5600 &lock->lk_old_lock_stateid,
5601 NFS4_LOCK_STID, &lock_stp, nn);
5605 lock_sop = lockowner(lock_stp->st_stateowner);
5607 lkflg = setlkflg(lock->lk_type);
5608 status = nfs4_check_openmode(lock_stp, lkflg);
5612 status = nfserr_grace;
5613 if (locks_in_grace(net) && !lock->lk_reclaim)
5615 status = nfserr_no_grace;
5616 if (!locks_in_grace(net) && lock->lk_reclaim)
5619 file_lock = locks_alloc_lock();
5621 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5622 status = nfserr_jukebox;
5626 fp = lock_stp->st_stid.sc_file;
5627 switch (lock->lk_type) {
5630 spin_lock(&fp->fi_lock);
5631 filp = find_readable_file_locked(fp);
5633 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
5634 spin_unlock(&fp->fi_lock);
5635 file_lock->fl_type = F_RDLCK;
5638 case NFS4_WRITEW_LT:
5639 spin_lock(&fp->fi_lock);
5640 filp = find_writeable_file_locked(fp);
5642 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
5643 spin_unlock(&fp->fi_lock);
5644 file_lock->fl_type = F_WRLCK;
5647 status = nfserr_inval;
5651 status = nfserr_openmode;
5655 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
5656 file_lock->fl_pid = current->tgid;
5657 file_lock->fl_file = filp;
5658 file_lock->fl_flags = FL_POSIX;
5659 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5660 file_lock->fl_start = lock->lk_offset;
5661 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5662 nfs4_transform_lock_offset(file_lock);
5664 conflock = locks_alloc_lock();
5666 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5667 status = nfserr_jukebox;
5671 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
5673 case 0: /* success! */
5674 nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
5677 case (EAGAIN): /* conflock holds conflicting lock */
5678 status = nfserr_denied;
5679 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
5680 nfs4_set_lock_denied(conflock, &lock->lk_denied);
5683 status = nfserr_deadlock;
5686 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
5687 status = nfserrno(err);
5694 /* Bump seqid manually if the 4.0 replay owner is openowner */
5695 if (cstate->replay_owner &&
5696 cstate->replay_owner != &lock_sop->lo_owner &&
5697 seqid_mutating_err(ntohl(status)))
5698 lock_sop->lo_owner.so_seqid++;
5700 mutex_unlock(&lock_stp->st_mutex);
5703 * If this is a new, never-before-used stateid, and we are
5704 * returning an error, then just go ahead and release it.
5707 release_lock_stateid(lock_stp);
5709 nfs4_put_stid(&lock_stp->st_stid);
5712 nfs4_put_stid(&open_stp->st_stid);
5713 nfsd4_bump_seqid(cstate, status);
5715 locks_free_lock(file_lock);
5717 locks_free_lock(conflock);
5722 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5723 * so we do a temporary open here just to get an open file to pass to
5724 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5727 static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
5730 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5732 err = nfserrno(vfs_test_lock(file, lock));
5742 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5743 struct nfsd4_lockt *lockt)
5745 struct file_lock *file_lock = NULL;
5746 struct nfs4_lockowner *lo = NULL;
5748 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5750 if (locks_in_grace(SVC_NET(rqstp)))
5751 return nfserr_grace;
5753 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5754 return nfserr_inval;
5756 if (!nfsd4_has_session(cstate)) {
5757 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
5762 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
5765 file_lock = locks_alloc_lock();
5767 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5768 status = nfserr_jukebox;
5772 switch (lockt->lt_type) {
5775 file_lock->fl_type = F_RDLCK;
5778 case NFS4_WRITEW_LT:
5779 file_lock->fl_type = F_WRLCK;
5782 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
5783 status = nfserr_inval;
5787 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
5789 file_lock->fl_owner = (fl_owner_t)lo;
5790 file_lock->fl_pid = current->tgid;
5791 file_lock->fl_flags = FL_POSIX;
5793 file_lock->fl_start = lockt->lt_offset;
5794 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
5796 nfs4_transform_lock_offset(file_lock);
5798 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
5802 if (file_lock->fl_type != F_UNLCK) {
5803 status = nfserr_denied;
5804 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
5808 nfs4_put_stateowner(&lo->lo_owner);
5810 locks_free_lock(file_lock);
5815 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5816 struct nfsd4_locku *locku)
5818 struct nfs4_ol_stateid *stp;
5819 struct file *filp = NULL;
5820 struct file_lock *file_lock = NULL;
5823 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5825 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5826 (long long) locku->lu_offset,
5827 (long long) locku->lu_length);
5829 if (check_lock_length(locku->lu_offset, locku->lu_length))
5830 return nfserr_inval;
5832 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
5833 &locku->lu_stateid, NFS4_LOCK_STID,
5837 filp = find_any_file(stp->st_stid.sc_file);
5839 status = nfserr_lock_range;
5842 file_lock = locks_alloc_lock();
5844 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5845 status = nfserr_jukebox;
5849 file_lock->fl_type = F_UNLCK;
5850 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
5851 file_lock->fl_pid = current->tgid;
5852 file_lock->fl_file = filp;
5853 file_lock->fl_flags = FL_POSIX;
5854 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5855 file_lock->fl_start = locku->lu_offset;
5857 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5859 nfs4_transform_lock_offset(file_lock);
5861 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
5863 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
5866 nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
5870 mutex_unlock(&stp->st_mutex);
5871 nfs4_put_stid(&stp->st_stid);
5873 nfsd4_bump_seqid(cstate, status);
5875 locks_free_lock(file_lock);
5879 status = nfserrno(err);
5885 * true: locks held by lockowner
5886 * false: no locks held by lockowner
5889 check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
5891 struct file_lock *fl;
5893 struct file *filp = find_any_file(fp);
5894 struct inode *inode;
5895 struct file_lock_context *flctx;
5898 /* Any valid lock stateid should have some sort of access */
5903 inode = file_inode(filp);
5904 flctx = inode->i_flctx;
5906 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
5907 spin_lock(&flctx->flc_lock);
5908 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
5909 if (fl->fl_owner == (fl_owner_t)lowner) {
5914 spin_unlock(&flctx->flc_lock);
5921 nfsd4_release_lockowner(struct svc_rqst *rqstp,
5922 struct nfsd4_compound_state *cstate,
5923 struct nfsd4_release_lockowner *rlockowner)
5925 clientid_t *clid = &rlockowner->rl_clientid;
5926 struct nfs4_stateowner *sop;
5927 struct nfs4_lockowner *lo = NULL;
5928 struct nfs4_ol_stateid *stp;
5929 struct xdr_netobj *owner = &rlockowner->rl_owner;
5930 unsigned int hashval = ownerstr_hashval(owner);
5932 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5933 struct nfs4_client *clp;
5934 LIST_HEAD (reaplist);
5936 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5937 clid->cl_boot, clid->cl_id);
5939 status = lookup_clientid(clid, cstate, nn);
5944 /* Find the matching lock stateowner */
5945 spin_lock(&clp->cl_lock);
5946 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
5949 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
5952 /* see if there are still any locks associated with it */
5953 lo = lockowner(sop);
5954 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5955 if (check_for_locks(stp->st_stid.sc_file, lo)) {
5956 status = nfserr_locks_held;
5957 spin_unlock(&clp->cl_lock);
5962 nfs4_get_stateowner(sop);
5966 spin_unlock(&clp->cl_lock);
5970 unhash_lockowner_locked(lo);
5971 while (!list_empty(&lo->lo_owner.so_stateids)) {
5972 stp = list_first_entry(&lo->lo_owner.so_stateids,
5973 struct nfs4_ol_stateid,
5975 WARN_ON(!unhash_lock_stateid(stp));
5976 put_ol_stateid_locked(stp, &reaplist);
5978 spin_unlock(&clp->cl_lock);
5979 free_ol_stateid_reaplist(&reaplist);
5980 nfs4_put_stateowner(&lo->lo_owner);
5985 static inline struct nfs4_client_reclaim *
5988 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
5992 nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
5994 struct nfs4_client_reclaim *crp;
5996 crp = nfsd4_find_reclaim_client(name, nn);
5997 return (crp && crp->cr_clp);
6001 * failure => all reset bets are off, nfserr_no_grace...
6003 struct nfs4_client_reclaim *
6004 nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
6006 unsigned int strhashval;
6007 struct nfs4_client_reclaim *crp;
6009 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
6010 crp = alloc_reclaim();
6012 strhashval = clientstr_hashval(name);
6013 INIT_LIST_HEAD(&crp->cr_strhash);
6014 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
6015 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
6017 nn->reclaim_str_hashtbl_size++;
6023 nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
6025 list_del(&crp->cr_strhash);
6027 nn->reclaim_str_hashtbl_size--;
6031 nfs4_release_reclaim(struct nfsd_net *nn)
6033 struct nfs4_client_reclaim *crp = NULL;
6036 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6037 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
6038 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
6039 struct nfs4_client_reclaim, cr_strhash);
6040 nfs4_remove_reclaim_record(crp, nn);
6043 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
6047 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
6048 struct nfs4_client_reclaim *
6049 nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
6051 unsigned int strhashval;
6052 struct nfs4_client_reclaim *crp = NULL;
6054 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
6056 strhashval = clientstr_hashval(recdir);
6057 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
6058 if (same_name(crp->cr_recdir, recdir)) {
6066 * Called from OPEN. Look for clientid in reclaim list.
6069 nfs4_check_open_reclaim(clientid_t *clid,
6070 struct nfsd4_compound_state *cstate,
6071 struct nfsd_net *nn)
6075 /* find clientid in conf_id_hashtbl */
6076 status = lookup_clientid(clid, cstate, nn);
6078 return nfserr_reclaim_bad;
6080 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
6081 return nfserr_no_grace;
6083 if (nfsd4_client_record_check(cstate->clp))
6084 return nfserr_reclaim_bad;
6089 #ifdef CONFIG_NFSD_FAULT_INJECTION
6091 put_client(struct nfs4_client *clp)
6093 atomic_dec(&clp->cl_refcount);
6096 static struct nfs4_client *
6097 nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
6099 struct nfs4_client *clp;
6100 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6103 if (!nfsd_netns_ready(nn))
6106 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6107 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
6114 nfsd_inject_print_clients(void)
6116 struct nfs4_client *clp;
6118 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6120 char buf[INET6_ADDRSTRLEN];
6122 if (!nfsd_netns_ready(nn))
6125 spin_lock(&nn->client_lock);
6126 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6127 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
6128 pr_info("NFS Client: %s\n", buf);
6131 spin_unlock(&nn->client_lock);
6137 nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
6140 struct nfs4_client *clp;
6141 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6144 if (!nfsd_netns_ready(nn))
6147 spin_lock(&nn->client_lock);
6148 clp = nfsd_find_client(addr, addr_size);
6150 if (mark_client_expired_locked(clp) == nfs_ok)
6155 spin_unlock(&nn->client_lock);
6164 nfsd_inject_forget_clients(u64 max)
6167 struct nfs4_client *clp, *next;
6168 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6170 LIST_HEAD(reaplist);
6172 if (!nfsd_netns_ready(nn))
6175 spin_lock(&nn->client_lock);
6176 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6177 if (mark_client_expired_locked(clp) == nfs_ok) {
6178 list_add(&clp->cl_lru, &reaplist);
6179 if (max != 0 && ++count >= max)
6183 spin_unlock(&nn->client_lock);
6185 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
6191 static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
6194 char buf[INET6_ADDRSTRLEN];
6195 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
6196 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
6200 nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
6201 struct list_head *collect)
6203 struct nfs4_client *clp = lst->st_stid.sc_client;
6204 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6210 lockdep_assert_held(&nn->client_lock);
6211 atomic_inc(&clp->cl_refcount);
6212 list_add(&lst->st_locks, collect);
6215 static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
6216 struct list_head *collect,
6217 bool (*func)(struct nfs4_ol_stateid *))
6219 struct nfs4_openowner *oop;
6220 struct nfs4_ol_stateid *stp, *st_next;
6221 struct nfs4_ol_stateid *lst, *lst_next;
6224 spin_lock(&clp->cl_lock);
6225 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
6226 list_for_each_entry_safe(stp, st_next,
6227 &oop->oo_owner.so_stateids, st_perstateowner) {
6228 list_for_each_entry_safe(lst, lst_next,
6229 &stp->st_locks, st_locks) {
6232 nfsd_inject_add_lock_to_list(lst,
6237 * Despite the fact that these functions deal
6238 * with 64-bit integers for "count", we must
6239 * ensure that it doesn't blow up the
6240 * clp->cl_refcount. Throw a warning if we
6241 * start to approach INT_MAX here.
6243 WARN_ON_ONCE(count == (INT_MAX / 2));
6250 spin_unlock(&clp->cl_lock);
6256 nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
6259 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
6263 nfsd_print_client_locks(struct nfs4_client *clp)
6265 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
6266 nfsd_print_count(clp, count, "locked files");
6271 nfsd_inject_print_locks(void)
6273 struct nfs4_client *clp;
6275 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6278 if (!nfsd_netns_ready(nn))
6281 spin_lock(&nn->client_lock);
6282 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6283 count += nfsd_print_client_locks(clp);
6284 spin_unlock(&nn->client_lock);
6290 nfsd_reap_locks(struct list_head *reaplist)
6292 struct nfs4_client *clp;
6293 struct nfs4_ol_stateid *stp, *next;
6295 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
6296 list_del_init(&stp->st_locks);
6297 clp = stp->st_stid.sc_client;
6298 nfs4_put_stid(&stp->st_stid);
6304 nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
6306 unsigned int count = 0;
6307 struct nfs4_client *clp;
6308 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6310 LIST_HEAD(reaplist);
6312 if (!nfsd_netns_ready(nn))
6315 spin_lock(&nn->client_lock);
6316 clp = nfsd_find_client(addr, addr_size);
6318 count = nfsd_collect_client_locks(clp, &reaplist, 0);
6319 spin_unlock(&nn->client_lock);
6320 nfsd_reap_locks(&reaplist);
6325 nfsd_inject_forget_locks(u64 max)
6328 struct nfs4_client *clp;
6329 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6331 LIST_HEAD(reaplist);
6333 if (!nfsd_netns_ready(nn))
6336 spin_lock(&nn->client_lock);
6337 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6338 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
6339 if (max != 0 && count >= max)
6342 spin_unlock(&nn->client_lock);
6343 nfsd_reap_locks(&reaplist);
6348 nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
6349 struct list_head *collect,
6350 void (*func)(struct nfs4_openowner *))
6352 struct nfs4_openowner *oop, *next;
6353 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6357 lockdep_assert_held(&nn->client_lock);
6359 spin_lock(&clp->cl_lock);
6360 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
6364 atomic_inc(&clp->cl_refcount);
6365 list_add(&oop->oo_perclient, collect);
6370 * Despite the fact that these functions deal with
6371 * 64-bit integers for "count", we must ensure that
6372 * it doesn't blow up the clp->cl_refcount. Throw a
6373 * warning if we start to approach INT_MAX here.
6375 WARN_ON_ONCE(count == (INT_MAX / 2));
6379 spin_unlock(&clp->cl_lock);
6385 nfsd_print_client_openowners(struct nfs4_client *clp)
6387 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
6389 nfsd_print_count(clp, count, "openowners");
6394 nfsd_collect_client_openowners(struct nfs4_client *clp,
6395 struct list_head *collect, u64 max)
6397 return nfsd_foreach_client_openowner(clp, max, collect,
6398 unhash_openowner_locked);
6402 nfsd_inject_print_openowners(void)
6404 struct nfs4_client *clp;
6406 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6409 if (!nfsd_netns_ready(nn))
6412 spin_lock(&nn->client_lock);
6413 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6414 count += nfsd_print_client_openowners(clp);
6415 spin_unlock(&nn->client_lock);
6421 nfsd_reap_openowners(struct list_head *reaplist)
6423 struct nfs4_client *clp;
6424 struct nfs4_openowner *oop, *next;
6426 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
6427 list_del_init(&oop->oo_perclient);
6428 clp = oop->oo_owner.so_client;
6429 release_openowner(oop);
6435 nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
6438 unsigned int count = 0;
6439 struct nfs4_client *clp;
6440 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6442 LIST_HEAD(reaplist);
6444 if (!nfsd_netns_ready(nn))
6447 spin_lock(&nn->client_lock);
6448 clp = nfsd_find_client(addr, addr_size);
6450 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
6451 spin_unlock(&nn->client_lock);
6452 nfsd_reap_openowners(&reaplist);
6457 nfsd_inject_forget_openowners(u64 max)
6460 struct nfs4_client *clp;
6461 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6463 LIST_HEAD(reaplist);
6465 if (!nfsd_netns_ready(nn))
6468 spin_lock(&nn->client_lock);
6469 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6470 count += nfsd_collect_client_openowners(clp, &reaplist,
6472 if (max != 0 && count >= max)
6475 spin_unlock(&nn->client_lock);
6476 nfsd_reap_openowners(&reaplist);
6480 static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
6481 struct list_head *victims)
6483 struct nfs4_delegation *dp, *next;
6484 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6488 lockdep_assert_held(&nn->client_lock);
6490 spin_lock(&state_lock);
6491 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
6494 * It's not safe to mess with delegations that have a
6495 * non-zero dl_time. They might have already been broken
6496 * and could be processed by the laundromat outside of
6497 * the state_lock. Just leave them be.
6499 if (dp->dl_time != 0)
6502 atomic_inc(&clp->cl_refcount);
6503 WARN_ON(!unhash_delegation_locked(dp));
6504 list_add(&dp->dl_recall_lru, victims);
6508 * Despite the fact that these functions deal with
6509 * 64-bit integers for "count", we must ensure that
6510 * it doesn't blow up the clp->cl_refcount. Throw a
6511 * warning if we start to approach INT_MAX here.
6513 WARN_ON_ONCE(count == (INT_MAX / 2));
6517 spin_unlock(&state_lock);
6522 nfsd_print_client_delegations(struct nfs4_client *clp)
6524 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
6526 nfsd_print_count(clp, count, "delegations");
6531 nfsd_inject_print_delegations(void)
6533 struct nfs4_client *clp;
6535 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6538 if (!nfsd_netns_ready(nn))
6541 spin_lock(&nn->client_lock);
6542 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6543 count += nfsd_print_client_delegations(clp);
6544 spin_unlock(&nn->client_lock);
6550 nfsd_forget_delegations(struct list_head *reaplist)
6552 struct nfs4_client *clp;
6553 struct nfs4_delegation *dp, *next;
6555 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6556 list_del_init(&dp->dl_recall_lru);
6557 clp = dp->dl_stid.sc_client;
6558 revoke_delegation(dp);
6564 nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
6568 struct nfs4_client *clp;
6569 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6571 LIST_HEAD(reaplist);
6573 if (!nfsd_netns_ready(nn))
6576 spin_lock(&nn->client_lock);
6577 clp = nfsd_find_client(addr, addr_size);
6579 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6580 spin_unlock(&nn->client_lock);
6582 nfsd_forget_delegations(&reaplist);
6587 nfsd_inject_forget_delegations(u64 max)
6590 struct nfs4_client *clp;
6591 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6593 LIST_HEAD(reaplist);
6595 if (!nfsd_netns_ready(nn))
6598 spin_lock(&nn->client_lock);
6599 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6600 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6601 if (max != 0 && count >= max)
6604 spin_unlock(&nn->client_lock);
6605 nfsd_forget_delegations(&reaplist);
6610 nfsd_recall_delegations(struct list_head *reaplist)
6612 struct nfs4_client *clp;
6613 struct nfs4_delegation *dp, *next;
6615 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6616 list_del_init(&dp->dl_recall_lru);
6617 clp = dp->dl_stid.sc_client;
6619 * We skipped all entries that had a zero dl_time before,
6620 * so we can now reset the dl_time back to 0. If a delegation
6621 * break comes in now, then it won't make any difference since
6622 * we're recalling it either way.
6624 spin_lock(&state_lock);
6626 spin_unlock(&state_lock);
6627 nfsd_break_one_deleg(dp);
6633 nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
6637 struct nfs4_client *clp;
6638 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6640 LIST_HEAD(reaplist);
6642 if (!nfsd_netns_ready(nn))
6645 spin_lock(&nn->client_lock);
6646 clp = nfsd_find_client(addr, addr_size);
6648 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6649 spin_unlock(&nn->client_lock);
6651 nfsd_recall_delegations(&reaplist);
6656 nfsd_inject_recall_delegations(u64 max)
6659 struct nfs4_client *clp, *next;
6660 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6662 LIST_HEAD(reaplist);
6664 if (!nfsd_netns_ready(nn))
6667 spin_lock(&nn->client_lock);
6668 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6669 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6670 if (max != 0 && ++count >= max)
6673 spin_unlock(&nn->client_lock);
6674 nfsd_recall_delegations(&reaplist);
6677 #endif /* CONFIG_NFSD_FAULT_INJECTION */
6680 * Since the lifetime of a delegation isn't limited to that of an open, a
6681 * client may quite reasonably hang on to a delegation as long as it has
6682 * the inode cached. This becomes an obvious problem the first time a
6683 * client's inode cache approaches the size of the server's total memory.
6685 * For now we avoid this problem by imposing a hard limit on the number
6686 * of delegations, which varies according to the server's memory size.
6689 set_max_delegations(void)
6692 * Allow at most 4 delegations per megabyte of RAM. Quick
6693 * estimates suggest that in the worst case (where every delegation
6694 * is for a different inode), a delegation could take about 1.5K,
6695 * giving a worst case usage of about 6% of memory.
6697 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
6700 static int nfs4_state_create_net(struct net *net)
6702 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6705 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6706 CLIENT_HASH_SIZE, GFP_KERNEL);
6707 if (!nn->conf_id_hashtbl)
6709 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6710 CLIENT_HASH_SIZE, GFP_KERNEL);
6711 if (!nn->unconf_id_hashtbl)
6713 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
6714 SESSION_HASH_SIZE, GFP_KERNEL);
6715 if (!nn->sessionid_hashtbl)
6718 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6719 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
6720 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
6722 for (i = 0; i < SESSION_HASH_SIZE; i++)
6723 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
6724 nn->conf_name_tree = RB_ROOT;
6725 nn->unconf_name_tree = RB_ROOT;
6726 INIT_LIST_HEAD(&nn->client_lru);
6727 INIT_LIST_HEAD(&nn->close_lru);
6728 INIT_LIST_HEAD(&nn->del_recall_lru);
6729 spin_lock_init(&nn->client_lock);
6731 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
6737 kfree(nn->unconf_id_hashtbl);
6739 kfree(nn->conf_id_hashtbl);
6745 nfs4_state_destroy_net(struct net *net)
6748 struct nfs4_client *clp = NULL;
6749 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6751 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6752 while (!list_empty(&nn->conf_id_hashtbl[i])) {
6753 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6754 destroy_client(clp);
6758 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6759 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
6760 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6761 destroy_client(clp);
6765 kfree(nn->sessionid_hashtbl);
6766 kfree(nn->unconf_id_hashtbl);
6767 kfree(nn->conf_id_hashtbl);
6772 nfs4_state_start_net(struct net *net)
6774 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6777 ret = nfs4_state_create_net(net);
6780 nn->boot_time = get_seconds();
6781 nn->grace_ended = false;
6782 nn->nfsd4_manager.block_opens = true;
6783 locks_start_grace(net, &nn->nfsd4_manager);
6784 nfsd4_client_tracking_init(net);
6785 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
6786 nn->nfsd4_grace, net);
6787 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
6791 /* initialization to perform when the nfsd service is started: */
6794 nfs4_state_start(void)
6798 ret = set_callback_cred();
6801 laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
6802 if (laundry_wq == NULL) {
6806 ret = nfsd4_create_callback_queue();
6808 goto out_free_laundry;
6810 set_max_delegations();
6815 destroy_workqueue(laundry_wq);
6821 nfs4_state_shutdown_net(struct net *net)
6823 struct nfs4_delegation *dp = NULL;
6824 struct list_head *pos, *next, reaplist;
6825 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6827 cancel_delayed_work_sync(&nn->laundromat_work);
6828 locks_end_grace(&nn->nfsd4_manager);
6830 INIT_LIST_HEAD(&reaplist);
6831 spin_lock(&state_lock);
6832 list_for_each_safe(pos, next, &nn->del_recall_lru) {
6833 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
6834 WARN_ON(!unhash_delegation_locked(dp));
6835 list_add(&dp->dl_recall_lru, &reaplist);
6837 spin_unlock(&state_lock);
6838 list_for_each_safe(pos, next, &reaplist) {
6839 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
6840 list_del_init(&dp->dl_recall_lru);
6841 put_clnt_odstate(dp->dl_clnt_odstate);
6842 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
6843 nfs4_put_stid(&dp->dl_stid);
6846 nfsd4_client_tracking_exit(net);
6847 nfs4_state_destroy_net(net);
6851 nfs4_state_shutdown(void)
6853 destroy_workqueue(laundry_wq);
6854 nfsd4_destroy_callback_queue();
6858 get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6860 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
6861 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
6865 put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6867 if (cstate->minorversion) {
6868 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
6869 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6874 clear_current_stateid(struct nfsd4_compound_state *cstate)
6876 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6880 * functions to set current state id
6883 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6885 put_stateid(cstate, &odp->od_stateid);
6889 nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
6891 put_stateid(cstate, &open->op_stateid);
6895 nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6897 put_stateid(cstate, &close->cl_stateid);
6901 nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
6903 put_stateid(cstate, &lock->lk_resp_stateid);
6907 * functions to consume current state id
6911 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6913 get_stateid(cstate, &odp->od_stateid);
6917 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
6919 get_stateid(cstate, &drp->dr_stateid);
6923 nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
6925 get_stateid(cstate, &fsp->fr_stateid);
6929 nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
6931 get_stateid(cstate, &setattr->sa_stateid);
6935 nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6937 get_stateid(cstate, &close->cl_stateid);
6941 nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
6943 get_stateid(cstate, &locku->lu_stateid);
6947 nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
6949 get_stateid(cstate, &read->rd_stateid);
6953 nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
6955 get_stateid(cstate, &write->wr_stateid);