2 * Copyright (c) 2001 The Regents of the University of Michigan.
5 * Kendrick Smith <kmsmith@umich.edu>
6 * Andy Adamson <kandros@umich.edu>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <linux/file.h>
37 #include <linux/slab.h>
38 #include <linux/namei.h>
39 #include <linux/swap.h>
40 #include <linux/pagemap.h>
41 #include <linux/ratelimit.h>
42 #include <linux/sunrpc/svcauth_gss.h>
43 #include <linux/sunrpc/addr.h>
44 #include <linux/jhash.h>
48 #include "current_stateid.h"
53 #define NFSDDBG_FACILITY NFSDDBG_PROC
55 #define all_ones {{~0,~0},~0}
56 static const stateid_t one_stateid = {
58 .si_opaque = all_ones,
60 static const stateid_t zero_stateid = {
63 static const stateid_t currentstateid = {
66 static const stateid_t close_stateid = {
67 .si_generation = 0xffffffffU,
70 static u64 current_sessionid = 1;
72 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
73 #define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
74 #define CURRENT_STATEID(stateid) (!memcmp((stateid), ¤tstateid, sizeof(stateid_t)))
76 /* forward declarations */
77 static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
78 static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
83 * Currently used for the del_recall_lru and file hash table. In an
84 * effort to decrease the scope of the client_mutex, this spinlock may
85 * eventually cover more:
87 static DEFINE_SPINLOCK(state_lock);
90 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
91 * the refcount on the open stateid to drop.
93 static DECLARE_WAIT_QUEUE_HEAD(close_wq);
95 static struct kmem_cache *openowner_slab;
96 static struct kmem_cache *lockowner_slab;
97 static struct kmem_cache *file_slab;
98 static struct kmem_cache *stateid_slab;
99 static struct kmem_cache *deleg_slab;
100 static struct kmem_cache *odstate_slab;
102 static void free_session(struct nfsd4_session *);
104 static struct nfsd4_callback_ops nfsd4_cb_recall_ops;
106 static bool is_session_dead(struct nfsd4_session *ses)
108 return ses->se_flags & NFS4_SESSION_DEAD;
111 static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
113 if (atomic_read(&ses->se_ref) > ref_held_by_me)
114 return nfserr_jukebox;
115 ses->se_flags |= NFS4_SESSION_DEAD;
119 static bool is_client_expired(struct nfs4_client *clp)
121 return clp->cl_time == 0;
124 static __be32 get_client_locked(struct nfs4_client *clp)
126 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
128 lockdep_assert_held(&nn->client_lock);
130 if (is_client_expired(clp))
131 return nfserr_expired;
132 atomic_inc(&clp->cl_refcount);
136 /* must be called under the client_lock */
138 renew_client_locked(struct nfs4_client *clp)
140 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
142 if (is_client_expired(clp)) {
144 printk("%s: client (clientid %08x/%08x) already expired\n",
146 clp->cl_clientid.cl_boot,
147 clp->cl_clientid.cl_id);
151 dprintk("renewing client (clientid %08x/%08x)\n",
152 clp->cl_clientid.cl_boot,
153 clp->cl_clientid.cl_id);
154 list_move_tail(&clp->cl_lru, &nn->client_lru);
155 clp->cl_time = get_seconds();
158 static void put_client_renew_locked(struct nfs4_client *clp)
160 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
162 lockdep_assert_held(&nn->client_lock);
164 if (!atomic_dec_and_test(&clp->cl_refcount))
166 if (!is_client_expired(clp))
167 renew_client_locked(clp);
170 static void put_client_renew(struct nfs4_client *clp)
172 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
174 if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
176 if (!is_client_expired(clp))
177 renew_client_locked(clp);
178 spin_unlock(&nn->client_lock);
181 static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
185 if (is_session_dead(ses))
186 return nfserr_badsession;
187 status = get_client_locked(ses->se_client);
190 atomic_inc(&ses->se_ref);
194 static void nfsd4_put_session_locked(struct nfsd4_session *ses)
196 struct nfs4_client *clp = ses->se_client;
197 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
199 lockdep_assert_held(&nn->client_lock);
201 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
203 put_client_renew_locked(clp);
206 static void nfsd4_put_session(struct nfsd4_session *ses)
208 struct nfs4_client *clp = ses->se_client;
209 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
211 spin_lock(&nn->client_lock);
212 nfsd4_put_session_locked(ses);
213 spin_unlock(&nn->client_lock);
216 static inline struct nfs4_stateowner *
217 nfs4_get_stateowner(struct nfs4_stateowner *sop)
219 atomic_inc(&sop->so_count);
224 same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
226 return (sop->so_owner.len == owner->len) &&
227 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
230 static struct nfs4_openowner *
231 find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
232 struct nfs4_client *clp)
234 struct nfs4_stateowner *so;
236 lockdep_assert_held(&clp->cl_lock);
238 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
240 if (!so->so_is_open_owner)
242 if (same_owner_str(so, &open->op_owner))
243 return openowner(nfs4_get_stateowner(so));
248 static struct nfs4_openowner *
249 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
250 struct nfs4_client *clp)
252 struct nfs4_openowner *oo;
254 spin_lock(&clp->cl_lock);
255 oo = find_openstateowner_str_locked(hashval, open, clp);
256 spin_unlock(&clp->cl_lock);
261 opaque_hashval(const void *ptr, int nbytes)
263 unsigned char *cptr = (unsigned char *) ptr;
273 static void nfsd4_free_file_rcu(struct rcu_head *rcu)
275 struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
277 kmem_cache_free(file_slab, fp);
281 put_nfs4_file(struct nfs4_file *fi)
283 might_lock(&state_lock);
285 if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) {
286 hlist_del_rcu(&fi->fi_hash);
287 spin_unlock(&state_lock);
288 WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
289 WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
290 call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
295 __nfs4_get_fd(struct nfs4_file *f, int oflag)
297 if (f->fi_fds[oflag])
298 return get_file(f->fi_fds[oflag]);
303 find_writeable_file_locked(struct nfs4_file *f)
307 lockdep_assert_held(&f->fi_lock);
309 ret = __nfs4_get_fd(f, O_WRONLY);
311 ret = __nfs4_get_fd(f, O_RDWR);
316 find_writeable_file(struct nfs4_file *f)
320 spin_lock(&f->fi_lock);
321 ret = find_writeable_file_locked(f);
322 spin_unlock(&f->fi_lock);
327 static struct file *find_readable_file_locked(struct nfs4_file *f)
331 lockdep_assert_held(&f->fi_lock);
333 ret = __nfs4_get_fd(f, O_RDONLY);
335 ret = __nfs4_get_fd(f, O_RDWR);
340 find_readable_file(struct nfs4_file *f)
344 spin_lock(&f->fi_lock);
345 ret = find_readable_file_locked(f);
346 spin_unlock(&f->fi_lock);
352 find_any_file(struct nfs4_file *f)
356 spin_lock(&f->fi_lock);
357 ret = __nfs4_get_fd(f, O_RDWR);
359 ret = __nfs4_get_fd(f, O_WRONLY);
361 ret = __nfs4_get_fd(f, O_RDONLY);
363 spin_unlock(&f->fi_lock);
367 static atomic_long_t num_delegations;
368 unsigned long max_delegations;
371 * Open owner state (share locks)
374 /* hash tables for lock and open owners */
375 #define OWNER_HASH_BITS 8
376 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
377 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
379 static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
383 ret = opaque_hashval(ownername->data, ownername->len);
384 return ret & OWNER_HASH_MASK;
387 /* hash table for nfs4_file */
388 #define FILE_HASH_BITS 8
389 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
391 static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
393 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
396 static unsigned int file_hashval(struct knfsd_fh *fh)
398 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
401 static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
404 __nfs4_file_get_access(struct nfs4_file *fp, u32 access)
406 lockdep_assert_held(&fp->fi_lock);
408 if (access & NFS4_SHARE_ACCESS_WRITE)
409 atomic_inc(&fp->fi_access[O_WRONLY]);
410 if (access & NFS4_SHARE_ACCESS_READ)
411 atomic_inc(&fp->fi_access[O_RDONLY]);
415 nfs4_file_get_access(struct nfs4_file *fp, u32 access)
417 lockdep_assert_held(&fp->fi_lock);
419 /* Does this access mode make sense? */
420 if (access & ~NFS4_SHARE_ACCESS_BOTH)
423 /* Does it conflict with a deny mode already set? */
424 if ((access & fp->fi_share_deny) != 0)
425 return nfserr_share_denied;
427 __nfs4_file_get_access(fp, access);
431 static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
433 /* Common case is that there is no deny mode. */
435 /* Does this deny mode make sense? */
436 if (deny & ~NFS4_SHARE_DENY_BOTH)
439 if ((deny & NFS4_SHARE_DENY_READ) &&
440 atomic_read(&fp->fi_access[O_RDONLY]))
441 return nfserr_share_denied;
443 if ((deny & NFS4_SHARE_DENY_WRITE) &&
444 atomic_read(&fp->fi_access[O_WRONLY]))
445 return nfserr_share_denied;
450 static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
452 might_lock(&fp->fi_lock);
454 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
455 struct file *f1 = NULL;
456 struct file *f2 = NULL;
458 swap(f1, fp->fi_fds[oflag]);
459 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
460 swap(f2, fp->fi_fds[O_RDWR]);
461 spin_unlock(&fp->fi_lock);
469 static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
471 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
473 if (access & NFS4_SHARE_ACCESS_WRITE)
474 __nfs4_file_put_access(fp, O_WRONLY);
475 if (access & NFS4_SHARE_ACCESS_READ)
476 __nfs4_file_put_access(fp, O_RDONLY);
480 * Allocate a new open/delegation state counter. This is needed for
481 * pNFS for proper return on close semantics.
483 * Note that we only allocate it for pNFS-enabled exports, otherwise
484 * all pointers to struct nfs4_clnt_odstate are always NULL.
486 static struct nfs4_clnt_odstate *
487 alloc_clnt_odstate(struct nfs4_client *clp)
489 struct nfs4_clnt_odstate *co;
491 co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
494 atomic_set(&co->co_odcount, 1);
500 hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
502 struct nfs4_file *fp = co->co_file;
504 lockdep_assert_held(&fp->fi_lock);
505 list_add(&co->co_perfile, &fp->fi_clnt_odstate);
509 get_clnt_odstate(struct nfs4_clnt_odstate *co)
512 atomic_inc(&co->co_odcount);
516 put_clnt_odstate(struct nfs4_clnt_odstate *co)
518 struct nfs4_file *fp;
524 if (atomic_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
525 list_del(&co->co_perfile);
526 spin_unlock(&fp->fi_lock);
528 nfsd4_return_all_file_layouts(co->co_client, fp);
529 kmem_cache_free(odstate_slab, co);
533 static struct nfs4_clnt_odstate *
534 find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
536 struct nfs4_clnt_odstate *co;
537 struct nfs4_client *cl;
544 spin_lock(&fp->fi_lock);
545 list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
546 if (co->co_client == cl) {
547 get_clnt_odstate(co);
553 hash_clnt_odstate_locked(new);
555 spin_unlock(&fp->fi_lock);
559 struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab,
560 void (*sc_free)(struct nfs4_stid *))
562 struct nfs4_stid *stid;
565 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
569 idr_preload(GFP_KERNEL);
570 spin_lock(&cl->cl_lock);
571 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 0, 0, GFP_NOWAIT);
572 spin_unlock(&cl->cl_lock);
577 stid->sc_free = sc_free;
578 stid->sc_client = cl;
579 stid->sc_stateid.si_opaque.so_id = new_id;
580 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
581 /* Will be incremented before return to client: */
582 atomic_set(&stid->sc_count, 1);
583 spin_lock_init(&stid->sc_lock);
586 * It shouldn't be a problem to reuse an opaque stateid value.
587 * I don't think it is for 4.1. But with 4.0 I worry that, for
588 * example, a stray write retransmission could be accepted by
589 * the server when it should have been rejected. Therefore,
590 * adopt a trick from the sctp code to attempt to maximize the
591 * amount of time until an id is reused, by ensuring they always
592 * "increase" (mod INT_MAX):
596 kmem_cache_free(slab, stid);
600 static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
602 struct nfs4_stid *stid;
604 stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid);
608 return openlockstateid(stid);
611 static void nfs4_free_deleg(struct nfs4_stid *stid)
613 kmem_cache_free(deleg_slab, stid);
614 atomic_long_dec(&num_delegations);
618 * When we recall a delegation, we should be careful not to hand it
619 * out again straight away.
620 * To ensure this we keep a pair of bloom filters ('new' and 'old')
621 * in which the filehandles of recalled delegations are "stored".
622 * If a filehandle appear in either filter, a delegation is blocked.
623 * When a delegation is recalled, the filehandle is stored in the "new"
625 * Every 30 seconds we swap the filters and clear the "new" one,
626 * unless both are empty of course.
628 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
629 * low 3 bytes as hash-table indices.
631 * 'blocked_delegations_lock', which is always taken in block_delegations(),
632 * is used to manage concurrent access. Testing does not need the lock
633 * except when swapping the two filters.
635 static DEFINE_SPINLOCK(blocked_delegations_lock);
636 static struct bloom_pair {
637 int entries, old_entries;
639 int new; /* index into 'set' */
640 DECLARE_BITMAP(set[2], 256);
641 } blocked_delegations;
643 static int delegation_blocked(struct knfsd_fh *fh)
646 struct bloom_pair *bd = &blocked_delegations;
648 if (bd->entries == 0)
650 if (seconds_since_boot() - bd->swap_time > 30) {
651 spin_lock(&blocked_delegations_lock);
652 if (seconds_since_boot() - bd->swap_time > 30) {
653 bd->entries -= bd->old_entries;
654 bd->old_entries = bd->entries;
655 memset(bd->set[bd->new], 0,
658 bd->swap_time = seconds_since_boot();
660 spin_unlock(&blocked_delegations_lock);
662 hash = jhash(&fh->fh_base, fh->fh_size, 0);
663 if (test_bit(hash&255, bd->set[0]) &&
664 test_bit((hash>>8)&255, bd->set[0]) &&
665 test_bit((hash>>16)&255, bd->set[0]))
668 if (test_bit(hash&255, bd->set[1]) &&
669 test_bit((hash>>8)&255, bd->set[1]) &&
670 test_bit((hash>>16)&255, bd->set[1]))
676 static void block_delegations(struct knfsd_fh *fh)
679 struct bloom_pair *bd = &blocked_delegations;
681 hash = jhash(&fh->fh_base, fh->fh_size, 0);
683 spin_lock(&blocked_delegations_lock);
684 __set_bit(hash&255, bd->set[bd->new]);
685 __set_bit((hash>>8)&255, bd->set[bd->new]);
686 __set_bit((hash>>16)&255, bd->set[bd->new]);
687 if (bd->entries == 0)
688 bd->swap_time = seconds_since_boot();
690 spin_unlock(&blocked_delegations_lock);
693 static struct nfs4_delegation *
694 alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh,
695 struct nfs4_clnt_odstate *odstate)
697 struct nfs4_delegation *dp;
700 dprintk("NFSD alloc_init_deleg\n");
701 n = atomic_long_inc_return(&num_delegations);
702 if (n < 0 || n > max_delegations)
704 if (delegation_blocked(¤t_fh->fh_handle))
706 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg));
711 * delegation seqid's are never incremented. The 4.1 special
712 * meaning of seqid 0 isn't meaningful, really, but let's avoid
713 * 0 anyway just for consistency and use 1:
715 dp->dl_stid.sc_stateid.si_generation = 1;
716 INIT_LIST_HEAD(&dp->dl_perfile);
717 INIT_LIST_HEAD(&dp->dl_perclnt);
718 INIT_LIST_HEAD(&dp->dl_recall_lru);
719 dp->dl_clnt_odstate = odstate;
720 get_clnt_odstate(odstate);
721 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
723 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
724 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
727 atomic_long_dec(&num_delegations);
732 nfs4_put_stid(struct nfs4_stid *s)
734 struct nfs4_file *fp = s->sc_file;
735 struct nfs4_client *clp = s->sc_client;
737 might_lock(&clp->cl_lock);
739 if (!atomic_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
740 wake_up_all(&close_wq);
743 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
744 spin_unlock(&clp->cl_lock);
751 nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
753 stateid_t *src = &stid->sc_stateid;
755 spin_lock(&stid->sc_lock);
756 if (unlikely(++src->si_generation == 0))
757 src->si_generation = 1;
758 memcpy(dst, src, sizeof(*dst));
759 spin_unlock(&stid->sc_lock);
762 static void nfs4_put_deleg_lease(struct nfs4_file *fp)
764 struct file *filp = NULL;
766 spin_lock(&fp->fi_lock);
767 if (fp->fi_deleg_file && --fp->fi_delegees == 0)
768 swap(filp, fp->fi_deleg_file);
769 spin_unlock(&fp->fi_lock);
772 vfs_setlease(filp, F_UNLCK, NULL, (void **)&fp);
777 void nfs4_unhash_stid(struct nfs4_stid *s)
783 * nfs4_get_existing_delegation - Discover if this delegation already exists
784 * @clp: a pointer to the nfs4_client we're granting a delegation to
785 * @fp: a pointer to the nfs4_file we're granting a delegation on
788 * On success: NULL if an existing delegation was not found.
790 * On error: -EAGAIN if one was previously granted to this nfs4_client
791 * for this nfs4_file.
796 nfs4_get_existing_delegation(struct nfs4_client *clp, struct nfs4_file *fp)
798 struct nfs4_delegation *searchdp = NULL;
799 struct nfs4_client *searchclp = NULL;
801 lockdep_assert_held(&state_lock);
802 lockdep_assert_held(&fp->fi_lock);
804 list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
805 searchclp = searchdp->dl_stid.sc_client;
806 if (clp == searchclp) {
814 * hash_delegation_locked - Add a delegation to the appropriate lists
815 * @dp: a pointer to the nfs4_delegation we are adding.
816 * @fp: a pointer to the nfs4_file we're granting a delegation on
819 * On success: NULL if the delegation was successfully hashed.
821 * On error: -EAGAIN if one was previously granted to this
822 * nfs4_client for this nfs4_file. Delegation is not hashed.
827 hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
830 struct nfs4_client *clp = dp->dl_stid.sc_client;
832 lockdep_assert_held(&state_lock);
833 lockdep_assert_held(&fp->fi_lock);
835 status = nfs4_get_existing_delegation(clp, fp);
839 atomic_inc(&dp->dl_stid.sc_count);
840 dp->dl_stid.sc_type = NFS4_DELEG_STID;
841 list_add(&dp->dl_perfile, &fp->fi_delegations);
842 list_add(&dp->dl_perclnt, &clp->cl_delegations);
847 unhash_delegation_locked(struct nfs4_delegation *dp)
849 struct nfs4_file *fp = dp->dl_stid.sc_file;
851 lockdep_assert_held(&state_lock);
853 if (list_empty(&dp->dl_perfile))
856 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
857 /* Ensure that deleg break won't try to requeue it */
859 spin_lock(&fp->fi_lock);
860 list_del_init(&dp->dl_perclnt);
861 list_del_init(&dp->dl_recall_lru);
862 list_del_init(&dp->dl_perfile);
863 spin_unlock(&fp->fi_lock);
867 static void destroy_delegation(struct nfs4_delegation *dp)
871 spin_lock(&state_lock);
872 unhashed = unhash_delegation_locked(dp);
873 spin_unlock(&state_lock);
875 put_clnt_odstate(dp->dl_clnt_odstate);
876 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
877 nfs4_put_stid(&dp->dl_stid);
881 static void revoke_delegation(struct nfs4_delegation *dp)
883 struct nfs4_client *clp = dp->dl_stid.sc_client;
885 WARN_ON(!list_empty(&dp->dl_recall_lru));
887 put_clnt_odstate(dp->dl_clnt_odstate);
888 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
890 if (clp->cl_minorversion == 0)
891 nfs4_put_stid(&dp->dl_stid);
893 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
894 spin_lock(&clp->cl_lock);
895 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
896 spin_unlock(&clp->cl_lock);
904 static unsigned int clientid_hashval(u32 id)
906 return id & CLIENT_HASH_MASK;
909 static unsigned int clientstr_hashval(const char *name)
911 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
915 * We store the NONE, READ, WRITE, and BOTH bits separately in the
916 * st_{access,deny}_bmap field of the stateid, in order to track not
917 * only what share bits are currently in force, but also what
918 * combinations of share bits previous opens have used. This allows us
919 * to enforce the recommendation of rfc 3530 14.2.19 that the server
920 * return an error if the client attempt to downgrade to a combination
921 * of share bits not explicable by closing some of its previous opens.
923 * XXX: This enforcement is actually incomplete, since we don't keep
924 * track of access/deny bit combinations; so, e.g., we allow:
926 * OPEN allow read, deny write
927 * OPEN allow both, deny none
928 * DOWNGRADE allow read, deny none
930 * which we should reject.
933 bmap_to_share_mode(unsigned long bmap) {
935 unsigned int access = 0;
937 for (i = 1; i < 4; i++) {
938 if (test_bit(i, &bmap))
944 /* set share access for a given stateid */
946 set_access(u32 access, struct nfs4_ol_stateid *stp)
948 unsigned char mask = 1 << access;
950 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
951 stp->st_access_bmap |= mask;
954 /* clear share access for a given stateid */
956 clear_access(u32 access, struct nfs4_ol_stateid *stp)
958 unsigned char mask = 1 << access;
960 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
961 stp->st_access_bmap &= ~mask;
964 /* test whether a given stateid has access */
966 test_access(u32 access, struct nfs4_ol_stateid *stp)
968 unsigned char mask = 1 << access;
970 return (bool)(stp->st_access_bmap & mask);
973 /* set share deny for a given stateid */
975 set_deny(u32 deny, struct nfs4_ol_stateid *stp)
977 unsigned char mask = 1 << deny;
979 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
980 stp->st_deny_bmap |= mask;
983 /* clear share deny for a given stateid */
985 clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
987 unsigned char mask = 1 << deny;
989 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
990 stp->st_deny_bmap &= ~mask;
993 /* test whether a given stateid is denying specific access */
995 test_deny(u32 deny, struct nfs4_ol_stateid *stp)
997 unsigned char mask = 1 << deny;
999 return (bool)(stp->st_deny_bmap & mask);
1002 static int nfs4_access_to_omode(u32 access)
1004 switch (access & NFS4_SHARE_ACCESS_BOTH) {
1005 case NFS4_SHARE_ACCESS_READ:
1007 case NFS4_SHARE_ACCESS_WRITE:
1009 case NFS4_SHARE_ACCESS_BOTH:
1017 * A stateid that had a deny mode associated with it is being released
1018 * or downgraded. Recalculate the deny mode on the file.
1021 recalculate_deny_mode(struct nfs4_file *fp)
1023 struct nfs4_ol_stateid *stp;
1025 spin_lock(&fp->fi_lock);
1026 fp->fi_share_deny = 0;
1027 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
1028 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
1029 spin_unlock(&fp->fi_lock);
1033 reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
1036 bool change = false;
1038 for (i = 1; i < 4; i++) {
1039 if ((i & deny) != i) {
1045 /* Recalculate per-file deny mode if there was a change */
1047 recalculate_deny_mode(stp->st_stid.sc_file);
1050 /* release all access and file references for a given stateid */
1052 release_all_access(struct nfs4_ol_stateid *stp)
1055 struct nfs4_file *fp = stp->st_stid.sc_file;
1057 if (fp && stp->st_deny_bmap != 0)
1058 recalculate_deny_mode(fp);
1060 for (i = 1; i < 4; i++) {
1061 if (test_access(i, stp))
1062 nfs4_file_put_access(stp->st_stid.sc_file, i);
1063 clear_access(i, stp);
1067 static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1069 kfree(sop->so_owner.data);
1070 sop->so_ops->so_free(sop);
1073 static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1075 struct nfs4_client *clp = sop->so_client;
1077 might_lock(&clp->cl_lock);
1079 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
1081 sop->so_ops->so_unhash(sop);
1082 spin_unlock(&clp->cl_lock);
1083 nfs4_free_stateowner(sop);
1086 static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
1088 struct nfs4_file *fp = stp->st_stid.sc_file;
1090 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1092 if (list_empty(&stp->st_perfile))
1095 spin_lock(&fp->fi_lock);
1096 list_del_init(&stp->st_perfile);
1097 spin_unlock(&fp->fi_lock);
1098 list_del(&stp->st_perstateowner);
1102 static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
1104 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1106 put_clnt_odstate(stp->st_clnt_odstate);
1107 release_all_access(stp);
1108 if (stp->st_stateowner)
1109 nfs4_put_stateowner(stp->st_stateowner);
1110 kmem_cache_free(stateid_slab, stid);
1113 static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
1115 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1116 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
1119 file = find_any_file(stp->st_stid.sc_file);
1121 filp_close(file, (fl_owner_t)lo);
1122 nfs4_free_ol_stateid(stid);
1126 * Put the persistent reference to an already unhashed generic stateid, while
1127 * holding the cl_lock. If it's the last reference, then put it onto the
1128 * reaplist for later destruction.
1130 static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1131 struct list_head *reaplist)
1133 struct nfs4_stid *s = &stp->st_stid;
1134 struct nfs4_client *clp = s->sc_client;
1136 lockdep_assert_held(&clp->cl_lock);
1138 WARN_ON_ONCE(!list_empty(&stp->st_locks));
1140 if (!atomic_dec_and_test(&s->sc_count)) {
1141 wake_up_all(&close_wq);
1145 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1146 list_add(&stp->st_locks, reaplist);
1149 static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
1151 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1153 list_del_init(&stp->st_locks);
1154 nfs4_unhash_stid(&stp->st_stid);
1155 return unhash_ol_stateid(stp);
1158 static void release_lock_stateid(struct nfs4_ol_stateid *stp)
1160 struct nfs4_client *clp = stp->st_stid.sc_client;
1163 spin_lock(&clp->cl_lock);
1164 unhashed = unhash_lock_stateid(stp);
1165 spin_unlock(&clp->cl_lock);
1167 nfs4_put_stid(&stp->st_stid);
1170 static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
1172 struct nfs4_client *clp = lo->lo_owner.so_client;
1174 lockdep_assert_held(&clp->cl_lock);
1176 list_del_init(&lo->lo_owner.so_strhash);
1180 * Free a list of generic stateids that were collected earlier after being
1184 free_ol_stateid_reaplist(struct list_head *reaplist)
1186 struct nfs4_ol_stateid *stp;
1187 struct nfs4_file *fp;
1191 while (!list_empty(reaplist)) {
1192 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1194 list_del(&stp->st_locks);
1195 fp = stp->st_stid.sc_file;
1196 stp->st_stid.sc_free(&stp->st_stid);
1202 static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1203 struct list_head *reaplist)
1205 struct nfs4_ol_stateid *stp;
1207 lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1209 while (!list_empty(&open_stp->st_locks)) {
1210 stp = list_entry(open_stp->st_locks.next,
1211 struct nfs4_ol_stateid, st_locks);
1212 WARN_ON(!unhash_lock_stateid(stp));
1213 put_ol_stateid_locked(stp, reaplist);
1217 static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
1218 struct list_head *reaplist)
1222 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1224 unhashed = unhash_ol_stateid(stp);
1225 release_open_stateid_locks(stp, reaplist);
1229 static void release_open_stateid(struct nfs4_ol_stateid *stp)
1231 LIST_HEAD(reaplist);
1233 spin_lock(&stp->st_stid.sc_client->cl_lock);
1234 if (unhash_open_stateid(stp, &reaplist))
1235 put_ol_stateid_locked(stp, &reaplist);
1236 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1237 free_ol_stateid_reaplist(&reaplist);
1240 static void unhash_openowner_locked(struct nfs4_openowner *oo)
1242 struct nfs4_client *clp = oo->oo_owner.so_client;
1244 lockdep_assert_held(&clp->cl_lock);
1246 list_del_init(&oo->oo_owner.so_strhash);
1247 list_del_init(&oo->oo_perclient);
1250 static void release_last_closed_stateid(struct nfs4_openowner *oo)
1252 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1254 struct nfs4_ol_stateid *s;
1256 spin_lock(&nn->client_lock);
1257 s = oo->oo_last_closed_stid;
1259 list_del_init(&oo->oo_close_lru);
1260 oo->oo_last_closed_stid = NULL;
1262 spin_unlock(&nn->client_lock);
1264 nfs4_put_stid(&s->st_stid);
1267 static void release_openowner(struct nfs4_openowner *oo)
1269 struct nfs4_ol_stateid *stp;
1270 struct nfs4_client *clp = oo->oo_owner.so_client;
1271 struct list_head reaplist;
1273 INIT_LIST_HEAD(&reaplist);
1275 spin_lock(&clp->cl_lock);
1276 unhash_openowner_locked(oo);
1277 while (!list_empty(&oo->oo_owner.so_stateids)) {
1278 stp = list_first_entry(&oo->oo_owner.so_stateids,
1279 struct nfs4_ol_stateid, st_perstateowner);
1280 if (unhash_open_stateid(stp, &reaplist))
1281 put_ol_stateid_locked(stp, &reaplist);
1283 spin_unlock(&clp->cl_lock);
1284 free_ol_stateid_reaplist(&reaplist);
1285 release_last_closed_stateid(oo);
1286 nfs4_put_stateowner(&oo->oo_owner);
1290 hash_sessionid(struct nfs4_sessionid *sessionid)
1292 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1294 return sid->sequence % SESSION_HASH_SIZE;
1297 #ifdef CONFIG_SUNRPC_DEBUG
1299 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1301 u32 *ptr = (u32 *)(&sessionid->data[0]);
1302 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1306 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1312 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1313 * won't be used for replay.
1315 void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1317 struct nfs4_stateowner *so = cstate->replay_owner;
1319 if (nfserr == nfserr_replay_me)
1322 if (!seqid_mutating_err(ntohl(nfserr))) {
1323 nfsd4_cstate_clear_replay(cstate);
1328 if (so->so_is_open_owner)
1329 release_last_closed_stateid(openowner(so));
1335 gen_sessionid(struct nfsd4_session *ses)
1337 struct nfs4_client *clp = ses->se_client;
1338 struct nfsd4_sessionid *sid;
1340 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1341 sid->clientid = clp->cl_clientid;
1342 sid->sequence = current_sessionid++;
1347 * The protocol defines ca_maxresponssize_cached to include the size of
1348 * the rpc header, but all we need to cache is the data starting after
1349 * the end of the initial SEQUENCE operation--the rest we regenerate
1350 * each time. Therefore we can advertise a ca_maxresponssize_cached
1351 * value that is the number of bytes in our cache plus a few additional
1352 * bytes. In order to stay on the safe side, and not promise more than
1353 * we can cache, those additional bytes must be the minimum possible: 24
1354 * bytes of rpc header (xid through accept state, with AUTH_NULL
1355 * verifier), 12 for the compound header (with zero-length tag), and 44
1356 * for the SEQUENCE op response:
1358 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1361 free_session_slots(struct nfsd4_session *ses)
1365 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1366 kfree(ses->se_slots[i]);
1370 * We don't actually need to cache the rpc and session headers, so we
1371 * can allocate a little less for each slot:
1373 static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
1377 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1380 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1381 return size + sizeof(struct nfsd4_slot);
1385 * XXX: If we run out of reserved DRC memory we could (up to a point)
1386 * re-negotiate active sessions and reduce their slot usage to make
1387 * room for new connections. For now we just fail the create session.
1389 static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
1391 u32 slotsize = slot_bytes(ca);
1392 u32 num = ca->maxreqs;
1395 spin_lock(&nfsd_drc_lock);
1396 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1397 nfsd_drc_max_mem - nfsd_drc_mem_used);
1398 num = min_t(int, num, avail / slotsize);
1399 nfsd_drc_mem_used += num * slotsize;
1400 spin_unlock(&nfsd_drc_lock);
1405 static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
1407 int slotsize = slot_bytes(ca);
1409 spin_lock(&nfsd_drc_lock);
1410 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
1411 spin_unlock(&nfsd_drc_lock);
1414 static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1415 struct nfsd4_channel_attrs *battrs)
1417 int numslots = fattrs->maxreqs;
1418 int slotsize = slot_bytes(fattrs);
1419 struct nfsd4_session *new;
1422 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
1423 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1424 mem = numslots * sizeof(struct nfsd4_slot *);
1426 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
1429 /* allocate each struct nfsd4_slot and data cache in one piece */
1430 for (i = 0; i < numslots; i++) {
1431 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
1432 if (!new->se_slots[i])
1436 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1437 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1442 kfree(new->se_slots[i]);
1447 static void free_conn(struct nfsd4_conn *c)
1449 svc_xprt_put(c->cn_xprt);
1453 static void nfsd4_conn_lost(struct svc_xpt_user *u)
1455 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1456 struct nfs4_client *clp = c->cn_session->se_client;
1458 spin_lock(&clp->cl_lock);
1459 if (!list_empty(&c->cn_persession)) {
1460 list_del(&c->cn_persession);
1463 nfsd4_probe_callback(clp);
1464 spin_unlock(&clp->cl_lock);
1467 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
1469 struct nfsd4_conn *conn;
1471 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1474 svc_xprt_get(rqstp->rq_xprt);
1475 conn->cn_xprt = rqstp->rq_xprt;
1476 conn->cn_flags = flags;
1477 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1481 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1483 conn->cn_session = ses;
1484 list_add(&conn->cn_persession, &ses->se_conns);
1487 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1489 struct nfs4_client *clp = ses->se_client;
1491 spin_lock(&clp->cl_lock);
1492 __nfsd4_hash_conn(conn, ses);
1493 spin_unlock(&clp->cl_lock);
1496 static int nfsd4_register_conn(struct nfsd4_conn *conn)
1498 conn->cn_xpt_user.callback = nfsd4_conn_lost;
1499 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
1502 static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
1506 nfsd4_hash_conn(conn, ses);
1507 ret = nfsd4_register_conn(conn);
1509 /* oops; xprt is already down: */
1510 nfsd4_conn_lost(&conn->cn_xpt_user);
1511 /* We may have gained or lost a callback channel: */
1512 nfsd4_probe_callback_sync(ses->se_client);
1515 static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
1517 u32 dir = NFS4_CDFC4_FORE;
1519 if (cses->flags & SESSION4_BACK_CHAN)
1520 dir |= NFS4_CDFC4_BACK;
1521 return alloc_conn(rqstp, dir);
1524 /* must be called under client_lock */
1525 static void nfsd4_del_conns(struct nfsd4_session *s)
1527 struct nfs4_client *clp = s->se_client;
1528 struct nfsd4_conn *c;
1530 spin_lock(&clp->cl_lock);
1531 while (!list_empty(&s->se_conns)) {
1532 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1533 list_del_init(&c->cn_persession);
1534 spin_unlock(&clp->cl_lock);
1536 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1539 spin_lock(&clp->cl_lock);
1541 spin_unlock(&clp->cl_lock);
1544 static void __free_session(struct nfsd4_session *ses)
1546 free_session_slots(ses);
1550 static void free_session(struct nfsd4_session *ses)
1552 nfsd4_del_conns(ses);
1553 nfsd4_put_drc_mem(&ses->se_fchannel);
1554 __free_session(ses);
1557 static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
1560 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1562 new->se_client = clp;
1565 INIT_LIST_HEAD(&new->se_conns);
1567 new->se_cb_seq_nr = 1;
1568 new->se_flags = cses->flags;
1569 new->se_cb_prog = cses->callback_prog;
1570 new->se_cb_sec = cses->cb_sec;
1571 atomic_set(&new->se_ref, 0);
1572 idx = hash_sessionid(&new->se_sessionid);
1573 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
1574 spin_lock(&clp->cl_lock);
1575 list_add(&new->se_perclnt, &clp->cl_sessions);
1576 spin_unlock(&clp->cl_lock);
1579 struct sockaddr *sa = svc_addr(rqstp);
1581 * This is a little silly; with sessions there's no real
1582 * use for the callback address. Use the peer address
1583 * as a reasonable default for now, but consider fixing
1584 * the rpc client not to require an address in the
1587 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1588 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
1592 /* caller must hold client_lock */
1593 static struct nfsd4_session *
1594 __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
1596 struct nfsd4_session *elem;
1598 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1600 lockdep_assert_held(&nn->client_lock);
1602 dump_sessionid(__func__, sessionid);
1603 idx = hash_sessionid(sessionid);
1604 /* Search in the appropriate list */
1605 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
1606 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1607 NFS4_MAX_SESSIONID_LEN)) {
1612 dprintk("%s: session not found\n", __func__);
1616 static struct nfsd4_session *
1617 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1620 struct nfsd4_session *session;
1621 __be32 status = nfserr_badsession;
1623 session = __find_in_sessionid_hashtbl(sessionid, net);
1626 status = nfsd4_get_session_locked(session);
1634 /* caller must hold client_lock */
1636 unhash_session(struct nfsd4_session *ses)
1638 struct nfs4_client *clp = ses->se_client;
1639 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1641 lockdep_assert_held(&nn->client_lock);
1643 list_del(&ses->se_hash);
1644 spin_lock(&ses->se_client->cl_lock);
1645 list_del(&ses->se_perclnt);
1646 spin_unlock(&ses->se_client->cl_lock);
1649 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1651 STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
1654 * We're assuming the clid was not given out from a boot
1655 * precisely 2^32 (about 136 years) before this one. That seems
1656 * a safe assumption:
1658 if (clid->cl_boot == (u32)nn->boot_time)
1660 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
1661 clid->cl_boot, clid->cl_id, nn->boot_time);
1666 * XXX Should we use a slab cache ?
1667 * This type of memory management is somewhat inefficient, but we use it
1668 * anyway since SETCLIENTID is not a common operation.
1670 static struct nfs4_client *alloc_client(struct xdr_netobj name)
1672 struct nfs4_client *clp;
1675 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1678 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
1679 if (clp->cl_name.data == NULL)
1681 clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
1682 OWNER_HASH_SIZE, GFP_KERNEL);
1683 if (!clp->cl_ownerstr_hashtbl)
1684 goto err_no_hashtbl;
1685 for (i = 0; i < OWNER_HASH_SIZE; i++)
1686 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
1687 clp->cl_name.len = name.len;
1688 INIT_LIST_HEAD(&clp->cl_sessions);
1689 idr_init(&clp->cl_stateids);
1690 atomic_set(&clp->cl_refcount, 0);
1691 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1692 INIT_LIST_HEAD(&clp->cl_idhash);
1693 INIT_LIST_HEAD(&clp->cl_openowners);
1694 INIT_LIST_HEAD(&clp->cl_delegations);
1695 INIT_LIST_HEAD(&clp->cl_lru);
1696 INIT_LIST_HEAD(&clp->cl_revoked);
1697 #ifdef CONFIG_NFSD_PNFS
1698 INIT_LIST_HEAD(&clp->cl_lo_states);
1700 spin_lock_init(&clp->cl_lock);
1701 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1704 kfree(clp->cl_name.data);
1711 free_client(struct nfs4_client *clp)
1713 while (!list_empty(&clp->cl_sessions)) {
1714 struct nfsd4_session *ses;
1715 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1717 list_del(&ses->se_perclnt);
1718 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1721 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
1722 free_svc_cred(&clp->cl_cred);
1723 kfree(clp->cl_ownerstr_hashtbl);
1724 kfree(clp->cl_name.data);
1725 idr_destroy(&clp->cl_stateids);
1729 /* must be called under the client_lock */
1731 unhash_client_locked(struct nfs4_client *clp)
1733 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1734 struct nfsd4_session *ses;
1736 lockdep_assert_held(&nn->client_lock);
1738 /* Mark the client as expired! */
1740 /* Make it invisible */
1741 if (!list_empty(&clp->cl_idhash)) {
1742 list_del_init(&clp->cl_idhash);
1743 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1744 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1746 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1748 list_del_init(&clp->cl_lru);
1749 spin_lock(&clp->cl_lock);
1750 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1751 list_del_init(&ses->se_hash);
1752 spin_unlock(&clp->cl_lock);
1756 unhash_client(struct nfs4_client *clp)
1758 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1760 spin_lock(&nn->client_lock);
1761 unhash_client_locked(clp);
1762 spin_unlock(&nn->client_lock);
1765 static __be32 mark_client_expired_locked(struct nfs4_client *clp)
1767 if (atomic_read(&clp->cl_refcount))
1768 return nfserr_jukebox;
1769 unhash_client_locked(clp);
1774 __destroy_client(struct nfs4_client *clp)
1776 struct nfs4_openowner *oo;
1777 struct nfs4_delegation *dp;
1778 struct list_head reaplist;
1780 INIT_LIST_HEAD(&reaplist);
1781 spin_lock(&state_lock);
1782 while (!list_empty(&clp->cl_delegations)) {
1783 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
1784 WARN_ON(!unhash_delegation_locked(dp));
1785 list_add(&dp->dl_recall_lru, &reaplist);
1787 spin_unlock(&state_lock);
1788 while (!list_empty(&reaplist)) {
1789 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
1790 list_del_init(&dp->dl_recall_lru);
1791 put_clnt_odstate(dp->dl_clnt_odstate);
1792 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
1793 nfs4_put_stid(&dp->dl_stid);
1795 while (!list_empty(&clp->cl_revoked)) {
1796 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
1797 list_del_init(&dp->dl_recall_lru);
1798 nfs4_put_stid(&dp->dl_stid);
1800 while (!list_empty(&clp->cl_openowners)) {
1801 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1802 nfs4_get_stateowner(&oo->oo_owner);
1803 release_openowner(oo);
1805 nfsd4_return_all_client_layouts(clp);
1806 nfsd4_shutdown_callback(clp);
1807 if (clp->cl_cb_conn.cb_xprt)
1808 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
1813 destroy_client(struct nfs4_client *clp)
1816 __destroy_client(clp);
1819 static void expire_client(struct nfs4_client *clp)
1822 nfsd4_client_record_remove(clp);
1823 __destroy_client(clp);
1826 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1828 memcpy(target->cl_verifier.data, source->data,
1829 sizeof(target->cl_verifier.data));
1832 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1834 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1835 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1838 static int copy_cred(struct svc_cred *target, struct svc_cred *source)
1840 if (source->cr_principal) {
1841 target->cr_principal =
1842 kstrdup(source->cr_principal, GFP_KERNEL);
1843 if (target->cr_principal == NULL)
1846 target->cr_principal = NULL;
1847 target->cr_flavor = source->cr_flavor;
1848 target->cr_uid = source->cr_uid;
1849 target->cr_gid = source->cr_gid;
1850 target->cr_group_info = source->cr_group_info;
1851 get_group_info(target->cr_group_info);
1852 target->cr_gss_mech = source->cr_gss_mech;
1853 if (source->cr_gss_mech)
1854 gss_mech_get(source->cr_gss_mech);
1859 compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1861 if (o1->len < o2->len)
1863 if (o1->len > o2->len)
1865 return memcmp(o1->data, o2->data, o1->len);
1868 static int same_name(const char *n1, const char *n2)
1870 return 0 == memcmp(n1, n2, HEXDIR_LEN);
1874 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1876 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
1880 same_clid(clientid_t *cl1, clientid_t *cl2)
1882 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
1885 static bool groups_equal(struct group_info *g1, struct group_info *g2)
1889 if (g1->ngroups != g2->ngroups)
1891 for (i=0; i<g1->ngroups; i++)
1892 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
1898 * RFC 3530 language requires clid_inuse be returned when the
1899 * "principal" associated with a requests differs from that previously
1900 * used. We use uid, gid's, and gss principal string as our best
1901 * approximation. We also don't want to allow non-gss use of a client
1902 * established using gss: in theory cr_principal should catch that
1903 * change, but in practice cr_principal can be null even in the gss case
1904 * since gssd doesn't always pass down a principal string.
1906 static bool is_gss_cred(struct svc_cred *cr)
1908 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1909 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1914 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1916 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
1917 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1918 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
1919 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1921 if (cr1->cr_principal == cr2->cr_principal)
1923 if (!cr1->cr_principal || !cr2->cr_principal)
1925 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
1928 static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1930 struct svc_cred *cr = &rqstp->rq_cred;
1933 if (!cr->cr_gss_mech)
1935 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1936 return service == RPC_GSS_SVC_INTEGRITY ||
1937 service == RPC_GSS_SVC_PRIVACY;
1940 static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1942 struct svc_cred *cr = &rqstp->rq_cred;
1944 if (!cl->cl_mach_cred)
1946 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1948 if (!svc_rqst_integrity_protected(rqstp))
1950 if (!cr->cr_principal)
1952 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1955 static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
1960 * This is opaque to client, so no need to byte-swap. Use
1961 * __force to keep sparse happy
1963 verf[0] = (__force __be32)get_seconds();
1964 verf[1] = (__force __be32)nn->clverifier_counter++;
1965 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
1968 static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
1970 clp->cl_clientid.cl_boot = nn->boot_time;
1971 clp->cl_clientid.cl_id = nn->clientid_counter++;
1972 gen_confirm(clp, nn);
1975 static struct nfs4_stid *
1976 find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
1978 struct nfs4_stid *ret;
1980 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1981 if (!ret || !ret->sc_type)
1986 static struct nfs4_stid *
1987 find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
1989 struct nfs4_stid *s;
1991 spin_lock(&cl->cl_lock);
1992 s = find_stateid_locked(cl, t);
1994 if (typemask & s->sc_type)
1995 atomic_inc(&s->sc_count);
1999 spin_unlock(&cl->cl_lock);
2003 static struct nfs4_client *create_client(struct xdr_netobj name,
2004 struct svc_rqst *rqstp, nfs4_verifier *verf)
2006 struct nfs4_client *clp;
2007 struct sockaddr *sa = svc_addr(rqstp);
2009 struct net *net = SVC_NET(rqstp);
2011 clp = alloc_client(name);
2015 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
2020 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
2021 clp->cl_time = get_seconds();
2022 clear_bit(0, &clp->cl_cb_slot_busy);
2023 copy_verf(clp, verf);
2024 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
2025 clp->cl_cb_session = NULL;
2031 add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
2033 struct rb_node **new = &(root->rb_node), *parent = NULL;
2034 struct nfs4_client *clp;
2037 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
2040 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
2041 new = &((*new)->rb_left);
2043 new = &((*new)->rb_right);
2046 rb_link_node(&new_clp->cl_namenode, parent, new);
2047 rb_insert_color(&new_clp->cl_namenode, root);
2050 static struct nfs4_client *
2051 find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2054 struct rb_node *node = root->rb_node;
2055 struct nfs4_client *clp;
2058 clp = rb_entry(node, struct nfs4_client, cl_namenode);
2059 cmp = compare_blob(&clp->cl_name, name);
2061 node = node->rb_left;
2063 node = node->rb_right;
2071 add_to_unconfirmed(struct nfs4_client *clp)
2073 unsigned int idhashval;
2074 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2076 lockdep_assert_held(&nn->client_lock);
2078 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2079 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
2080 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
2081 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
2082 renew_client_locked(clp);
2086 move_to_confirmed(struct nfs4_client *clp)
2088 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
2089 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2091 lockdep_assert_held(&nn->client_lock);
2093 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
2094 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
2095 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
2096 add_clp_to_name_tree(clp, &nn->conf_name_tree);
2097 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2098 renew_client_locked(clp);
2101 static struct nfs4_client *
2102 find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
2104 struct nfs4_client *clp;
2105 unsigned int idhashval = clientid_hashval(clid->cl_id);
2107 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
2108 if (same_clid(&clp->cl_clientid, clid)) {
2109 if ((bool)clp->cl_minorversion != sessions)
2111 renew_client_locked(clp);
2118 static struct nfs4_client *
2119 find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2121 struct list_head *tbl = nn->conf_id_hashtbl;
2123 lockdep_assert_held(&nn->client_lock);
2124 return find_client_in_id_table(tbl, clid, sessions);
2127 static struct nfs4_client *
2128 find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2130 struct list_head *tbl = nn->unconf_id_hashtbl;
2132 lockdep_assert_held(&nn->client_lock);
2133 return find_client_in_id_table(tbl, clid, sessions);
2136 static bool clp_used_exchangeid(struct nfs4_client *clp)
2138 return clp->cl_exchange_flags != 0;
2141 static struct nfs4_client *
2142 find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
2144 lockdep_assert_held(&nn->client_lock);
2145 return find_clp_in_name_tree(name, &nn->conf_name_tree);
2148 static struct nfs4_client *
2149 find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
2151 lockdep_assert_held(&nn->client_lock);
2152 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
2156 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
2158 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
2159 struct sockaddr *sa = svc_addr(rqstp);
2160 u32 scopeid = rpc_get_scope_id(sa);
2161 unsigned short expected_family;
2163 /* Currently, we only support tcp and tcp6 for the callback channel */
2164 if (se->se_callback_netid_len == 3 &&
2165 !memcmp(se->se_callback_netid_val, "tcp", 3))
2166 expected_family = AF_INET;
2167 else if (se->se_callback_netid_len == 4 &&
2168 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2169 expected_family = AF_INET6;
2173 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
2174 se->se_callback_addr_len,
2175 (struct sockaddr *)&conn->cb_addr,
2176 sizeof(conn->cb_addr));
2178 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
2181 if (conn->cb_addr.ss_family == AF_INET6)
2182 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
2184 conn->cb_prog = se->se_callback_prog;
2185 conn->cb_ident = se->se_callback_ident;
2186 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
2189 conn->cb_addr.ss_family = AF_UNSPEC;
2190 conn->cb_addrlen = 0;
2191 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
2192 "will not receive delegations\n",
2193 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2199 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
2202 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2204 struct xdr_buf *buf = resp->xdr.buf;
2205 struct nfsd4_slot *slot = resp->cstate.slot;
2208 dprintk("--> %s slot %p\n", __func__, slot);
2210 slot->sl_opcnt = resp->opcnt;
2211 slot->sl_status = resp->cstate.status;
2213 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
2214 if (nfsd4_not_cached(resp)) {
2215 slot->sl_datalen = 0;
2218 base = resp->cstate.data_offset;
2219 slot->sl_datalen = buf->len - base;
2220 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
2221 WARN("%s: sessions DRC could not cache compound\n", __func__);
2226 * Encode the replay sequence operation from the slot values.
2227 * If cachethis is FALSE encode the uncached rep error on the next
2228 * operation which sets resp->p and increments resp->opcnt for
2229 * nfs4svc_encode_compoundres.
2233 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2234 struct nfsd4_compoundres *resp)
2236 struct nfsd4_op *op;
2237 struct nfsd4_slot *slot = resp->cstate.slot;
2239 /* Encode the replayed sequence operation */
2240 op = &args->ops[resp->opcnt - 1];
2241 nfsd4_encode_operation(resp, op);
2243 /* Return nfserr_retry_uncached_rep in next operation. */
2244 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
2245 op = &args->ops[resp->opcnt++];
2246 op->status = nfserr_retry_uncached_rep;
2247 nfsd4_encode_operation(resp, op);
2253 * The sequence operation is not cached because we can use the slot and
2257 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2258 struct nfsd4_sequence *seq)
2260 struct nfsd4_slot *slot = resp->cstate.slot;
2261 struct xdr_stream *xdr = &resp->xdr;
2265 dprintk("--> %s slot %p\n", __func__, slot);
2267 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
2271 p = xdr_reserve_space(xdr, slot->sl_datalen);
2274 return nfserr_serverfault;
2276 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2277 xdr_commit_encode(xdr);
2279 resp->opcnt = slot->sl_opcnt;
2280 return slot->sl_status;
2284 * Set the exchange_id flags returned by the server.
2287 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2289 #ifdef CONFIG_NFSD_PNFS
2290 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
2292 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
2295 /* Referrals are supported, Migration is not. */
2296 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2298 /* set the wire flags to return to client. */
2299 clid->flags = new->cl_exchange_flags;
2302 static bool client_has_openowners(struct nfs4_client *clp)
2304 struct nfs4_openowner *oo;
2306 list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
2307 if (!list_empty(&oo->oo_owner.so_stateids))
2313 static bool client_has_state(struct nfs4_client *clp)
2315 return client_has_openowners(clp)
2316 #ifdef CONFIG_NFSD_PNFS
2317 || !list_empty(&clp->cl_lo_states)
2319 || !list_empty(&clp->cl_delegations)
2320 || !list_empty(&clp->cl_sessions);
2324 nfsd4_exchange_id(struct svc_rqst *rqstp,
2325 struct nfsd4_compound_state *cstate,
2326 struct nfsd4_exchange_id *exid)
2328 struct nfs4_client *conf, *new;
2329 struct nfs4_client *unconf = NULL;
2331 char addr_str[INET6_ADDRSTRLEN];
2332 nfs4_verifier verf = exid->verifier;
2333 struct sockaddr *sa = svc_addr(rqstp);
2334 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
2335 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2337 rpc_ntop(sa, addr_str, sizeof(addr_str));
2338 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
2339 "ip_addr=%s flags %x, spa_how %d\n",
2340 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
2341 addr_str, exid->flags, exid->spa_how);
2343 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
2344 return nfserr_inval;
2346 switch (exid->spa_how) {
2348 if (!svc_rqst_integrity_protected(rqstp))
2349 return nfserr_inval;
2352 default: /* checked by xdr code */
2355 return nfserr_encr_alg_unsupp;
2358 new = create_client(exid->clname, rqstp, &verf);
2360 return nfserr_jukebox;
2362 /* Cases below refer to rfc 5661 section 18.35.4: */
2363 spin_lock(&nn->client_lock);
2364 conf = find_confirmed_client_by_name(&exid->clname, nn);
2366 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2367 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2370 if (!clp_used_exchangeid(conf)) { /* buggy client */
2371 status = nfserr_inval;
2374 if (!mach_creds_match(conf, rqstp)) {
2375 status = nfserr_wrong_cred;
2378 if (!creds_match) { /* case 9 */
2379 status = nfserr_perm;
2382 if (!verfs_match) { /* case 8 */
2383 status = nfserr_not_same;
2387 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
2390 if (!creds_match) { /* case 3 */
2391 if (client_has_state(conf)) {
2392 status = nfserr_clid_inuse;
2397 if (verfs_match) { /* case 2 */
2398 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
2401 /* case 5, client reboot */
2406 if (update) { /* case 7 */
2407 status = nfserr_noent;
2411 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
2412 if (unconf) /* case 4, possible retry or client restart */
2413 unhash_client_locked(unconf);
2415 /* case 1 (normal case) */
2418 status = mark_client_expired_locked(conf);
2422 new->cl_minorversion = cstate->minorversion;
2423 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
2426 add_to_unconfirmed(new);
2429 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
2430 exid->clientid.cl_id = conf->cl_clientid.cl_id;
2432 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
2433 nfsd4_set_ex_flags(conf, exid);
2435 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
2436 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
2440 spin_unlock(&nn->client_lock);
2444 expire_client(unconf);
2449 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
2451 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2454 /* The slot is in use, and no response has been sent. */
2456 if (seqid == slot_seqid)
2457 return nfserr_jukebox;
2459 return nfserr_seq_misordered;
2461 /* Note unsigned 32-bit arithmetic handles wraparound: */
2462 if (likely(seqid == slot_seqid + 1))
2464 if (seqid == slot_seqid)
2465 return nfserr_replay_cache;
2466 return nfserr_seq_misordered;
2470 * Cache the create session result into the create session single DRC
2471 * slot cache by saving the xdr structure. sl_seqid has been set.
2472 * Do this for solo or embedded create session operations.
2475 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
2476 struct nfsd4_clid_slot *slot, __be32 nfserr)
2478 slot->sl_status = nfserr;
2479 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2483 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2484 struct nfsd4_clid_slot *slot)
2486 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2487 return slot->sl_status;
2490 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2491 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2492 1 + /* MIN tag is length with zero, only length */ \
2493 3 + /* version, opcount, opcode */ \
2494 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2495 /* seqid, slotID, slotID, cache */ \
2496 4 ) * sizeof(__be32))
2498 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2499 2 + /* verifier: AUTH_NULL, length 0 */\
2501 1 + /* MIN tag is length with zero, only length */ \
2502 3 + /* opcount, opcode, opstatus*/ \
2503 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2504 /* seqid, slotID, slotID, slotID, status */ \
2505 5 ) * sizeof(__be32))
2507 static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
2509 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2511 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2512 return nfserr_toosmall;
2513 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2514 return nfserr_toosmall;
2515 ca->headerpadsz = 0;
2516 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2517 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2518 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2519 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2520 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2521 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2523 * Note decreasing slot size below client's request may make it
2524 * difficult for client to function correctly, whereas
2525 * decreasing the number of slots will (just?) affect
2526 * performance. When short on memory we therefore prefer to
2527 * decrease number of slots instead of their size. Clients that
2528 * request larger slots than they need will get poor results:
2530 ca->maxreqs = nfsd4_get_drc_mem(ca);
2532 return nfserr_jukebox;
2537 #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2538 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2539 #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2540 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2542 static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2544 ca->headerpadsz = 0;
2547 * These RPC_MAX_HEADER macros are overkill, especially since we
2548 * don't even do gss on the backchannel yet. But this is still
2549 * less than 1k. Tighten up this estimate in the unlikely event
2550 * it turns out to be a problem for some client:
2552 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
2553 return nfserr_toosmall;
2554 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
2555 return nfserr_toosmall;
2556 ca->maxresp_cached = 0;
2558 return nfserr_toosmall;
2563 static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2565 switch (cbs->flavor) {
2571 * GSS case: the spec doesn't allow us to return this
2572 * error. But it also doesn't allow us not to support
2574 * I'd rather this fail hard than return some error the
2575 * client might think it can already handle:
2577 return nfserr_encr_alg_unsupp;
2582 nfsd4_create_session(struct svc_rqst *rqstp,
2583 struct nfsd4_compound_state *cstate,
2584 struct nfsd4_create_session *cr_ses)
2586 struct sockaddr *sa = svc_addr(rqstp);
2587 struct nfs4_client *conf, *unconf;
2588 struct nfs4_client *old = NULL;
2589 struct nfsd4_session *new;
2590 struct nfsd4_conn *conn;
2591 struct nfsd4_clid_slot *cs_slot = NULL;
2593 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2595 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2596 return nfserr_inval;
2597 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2600 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
2603 status = check_backchannel_attrs(&cr_ses->back_channel);
2605 goto out_release_drc_mem;
2606 status = nfserr_jukebox;
2607 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
2609 goto out_release_drc_mem;
2610 conn = alloc_conn_from_crses(rqstp, cr_ses);
2612 goto out_free_session;
2614 spin_lock(&nn->client_lock);
2615 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
2616 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
2617 WARN_ON_ONCE(conf && unconf);
2620 status = nfserr_wrong_cred;
2621 if (!mach_creds_match(conf, rqstp))
2623 cs_slot = &conf->cl_cs_slot;
2624 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2626 if (status == nfserr_replay_cache)
2627 status = nfsd4_replay_create_session(cr_ses, cs_slot);
2630 } else if (unconf) {
2631 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
2632 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
2633 status = nfserr_clid_inuse;
2636 status = nfserr_wrong_cred;
2637 if (!mach_creds_match(unconf, rqstp))
2639 cs_slot = &unconf->cl_cs_slot;
2640 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2642 /* an unconfirmed replay returns misordered */
2643 status = nfserr_seq_misordered;
2646 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
2648 status = mark_client_expired_locked(old);
2654 move_to_confirmed(unconf);
2657 status = nfserr_stale_clientid;
2662 * We do not support RDMA or persistent sessions
2664 cr_ses->flags &= ~SESSION4_PERSIST;
2665 cr_ses->flags &= ~SESSION4_RDMA;
2667 init_session(rqstp, new, conf, cr_ses);
2668 nfsd4_get_session_locked(new);
2670 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
2671 NFS4_MAX_SESSIONID_LEN);
2672 cs_slot->sl_seqid++;
2673 cr_ses->seqid = cs_slot->sl_seqid;
2675 /* cache solo and embedded create sessions under the client_lock */
2676 nfsd4_cache_create_session(cr_ses, cs_slot, status);
2677 spin_unlock(&nn->client_lock);
2678 /* init connection and backchannel */
2679 nfsd4_init_conn(rqstp, conn, new);
2680 nfsd4_put_session(new);
2685 spin_unlock(&nn->client_lock);
2690 __free_session(new);
2691 out_release_drc_mem:
2692 nfsd4_put_drc_mem(&cr_ses->fore_channel);
2696 static __be32 nfsd4_map_bcts_dir(u32 *dir)
2699 case NFS4_CDFC4_FORE:
2700 case NFS4_CDFC4_BACK:
2702 case NFS4_CDFC4_FORE_OR_BOTH:
2703 case NFS4_CDFC4_BACK_OR_BOTH:
2704 *dir = NFS4_CDFC4_BOTH;
2707 return nfserr_inval;
2710 __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2712 struct nfsd4_session *session = cstate->session;
2713 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2716 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2719 spin_lock(&nn->client_lock);
2720 session->se_cb_prog = bc->bc_cb_program;
2721 session->se_cb_sec = bc->bc_cb_sec;
2722 spin_unlock(&nn->client_lock);
2724 nfsd4_probe_callback(session->se_client);
2729 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2730 struct nfsd4_compound_state *cstate,
2731 struct nfsd4_bind_conn_to_session *bcts)
2734 struct nfsd4_conn *conn;
2735 struct nfsd4_session *session;
2736 struct net *net = SVC_NET(rqstp);
2737 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2739 if (!nfsd4_last_compound_op(rqstp))
2740 return nfserr_not_only_op;
2741 spin_lock(&nn->client_lock);
2742 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
2743 spin_unlock(&nn->client_lock);
2745 goto out_no_session;
2746 status = nfserr_wrong_cred;
2747 if (!mach_creds_match(session->se_client, rqstp))
2749 status = nfsd4_map_bcts_dir(&bcts->dir);
2752 conn = alloc_conn(rqstp, bcts->dir);
2753 status = nfserr_jukebox;
2756 nfsd4_init_conn(rqstp, conn, session);
2759 nfsd4_put_session(session);
2764 static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2768 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2772 nfsd4_destroy_session(struct svc_rqst *r,
2773 struct nfsd4_compound_state *cstate,
2774 struct nfsd4_destroy_session *sessionid)
2776 struct nfsd4_session *ses;
2778 int ref_held_by_me = 0;
2779 struct net *net = SVC_NET(r);
2780 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2782 status = nfserr_not_only_op;
2783 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
2784 if (!nfsd4_last_compound_op(r))
2788 dump_sessionid(__func__, &sessionid->sessionid);
2789 spin_lock(&nn->client_lock);
2790 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
2792 goto out_client_lock;
2793 status = nfserr_wrong_cred;
2794 if (!mach_creds_match(ses->se_client, r))
2795 goto out_put_session;
2796 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
2798 goto out_put_session;
2799 unhash_session(ses);
2800 spin_unlock(&nn->client_lock);
2802 nfsd4_probe_callback_sync(ses->se_client);
2804 spin_lock(&nn->client_lock);
2807 nfsd4_put_session_locked(ses);
2809 spin_unlock(&nn->client_lock);
2814 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
2816 struct nfsd4_conn *c;
2818 list_for_each_entry(c, &s->se_conns, cn_persession) {
2819 if (c->cn_xprt == xpt) {
2826 static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
2828 struct nfs4_client *clp = ses->se_client;
2829 struct nfsd4_conn *c;
2830 __be32 status = nfs_ok;
2833 spin_lock(&clp->cl_lock);
2834 c = __nfsd4_find_conn(new->cn_xprt, ses);
2837 status = nfserr_conn_not_bound_to_session;
2838 if (clp->cl_mach_cred)
2840 __nfsd4_hash_conn(new, ses);
2841 spin_unlock(&clp->cl_lock);
2842 ret = nfsd4_register_conn(new);
2844 /* oops; xprt is already down: */
2845 nfsd4_conn_lost(&new->cn_xpt_user);
2848 spin_unlock(&clp->cl_lock);
2853 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2855 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2857 return args->opcnt > session->se_fchannel.maxops;
2860 static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2861 struct nfsd4_session *session)
2863 struct xdr_buf *xb = &rqstp->rq_arg;
2865 return xb->len > session->se_fchannel.maxreq_sz;
2869 nfsd4_sequence(struct svc_rqst *rqstp,
2870 struct nfsd4_compound_state *cstate,
2871 struct nfsd4_sequence *seq)
2873 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2874 struct xdr_stream *xdr = &resp->xdr;
2875 struct nfsd4_session *session;
2876 struct nfs4_client *clp;
2877 struct nfsd4_slot *slot;
2878 struct nfsd4_conn *conn;
2881 struct net *net = SVC_NET(rqstp);
2882 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2884 if (resp->opcnt != 1)
2885 return nfserr_sequence_pos;
2888 * Will be either used or freed by nfsd4_sequence_check_conn
2891 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2893 return nfserr_jukebox;
2895 spin_lock(&nn->client_lock);
2896 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
2898 goto out_no_session;
2899 clp = session->se_client;
2901 status = nfserr_too_many_ops;
2902 if (nfsd4_session_too_many_ops(rqstp, session))
2903 goto out_put_session;
2905 status = nfserr_req_too_big;
2906 if (nfsd4_request_too_big(rqstp, session))
2907 goto out_put_session;
2909 status = nfserr_badslot;
2910 if (seq->slotid >= session->se_fchannel.maxreqs)
2911 goto out_put_session;
2913 slot = session->se_slots[seq->slotid];
2914 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2916 /* We do not negotiate the number of slots yet, so set the
2917 * maxslots to the session maxreqs which is used to encode
2918 * sr_highest_slotid and the sr_target_slot id to maxslots */
2919 seq->maxslots = session->se_fchannel.maxreqs;
2921 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2922 slot->sl_flags & NFSD4_SLOT_INUSE);
2923 if (status == nfserr_replay_cache) {
2924 status = nfserr_seq_misordered;
2925 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
2926 goto out_put_session;
2927 cstate->slot = slot;
2928 cstate->session = session;
2930 /* Return the cached reply status and set cstate->status
2931 * for nfsd4_proc_compound processing */
2932 status = nfsd4_replay_cache_entry(resp, seq);
2933 cstate->status = nfserr_replay_cache;
2937 goto out_put_session;
2939 status = nfsd4_sequence_check_conn(conn, session);
2942 goto out_put_session;
2944 buflen = (seq->cachethis) ?
2945 session->se_fchannel.maxresp_cached :
2946 session->se_fchannel.maxresp_sz;
2947 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2949 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
2950 goto out_put_session;
2951 svc_reserve(rqstp, buflen);
2954 /* Success! bump slot seqid */
2955 slot->sl_seqid = seq->seqid;
2956 slot->sl_flags |= NFSD4_SLOT_INUSE;
2958 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
2960 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
2962 cstate->slot = slot;
2963 cstate->session = session;
2967 switch (clp->cl_cb_state) {
2969 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2971 case NFSD4_CB_FAULT:
2972 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2975 seq->status_flags = 0;
2977 if (!list_empty(&clp->cl_revoked))
2978 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
2982 spin_unlock(&nn->client_lock);
2985 nfsd4_put_session_locked(session);
2986 goto out_no_session;
2990 nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2992 struct nfsd4_compound_state *cs = &resp->cstate;
2994 if (nfsd4_has_session(cs)) {
2995 if (cs->status != nfserr_replay_cache) {
2996 nfsd4_store_cache_entry(resp);
2997 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2999 /* Drop session reference that was taken in nfsd4_sequence() */
3000 nfsd4_put_session(cs->session);
3002 put_client_renew(cs->clp);
3006 nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
3008 struct nfs4_client *conf, *unconf;
3009 struct nfs4_client *clp = NULL;
3011 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3013 spin_lock(&nn->client_lock);
3014 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
3015 conf = find_confirmed_client(&dc->clientid, true, nn);
3016 WARN_ON_ONCE(conf && unconf);
3019 if (client_has_state(conf)) {
3020 status = nfserr_clientid_busy;
3023 status = mark_client_expired_locked(conf);
3030 status = nfserr_stale_clientid;
3033 if (!mach_creds_match(clp, rqstp)) {
3035 status = nfserr_wrong_cred;
3038 unhash_client_locked(clp);
3040 spin_unlock(&nn->client_lock);
3047 nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
3051 if (rc->rca_one_fs) {
3052 if (!cstate->current_fh.fh_dentry)
3053 return nfserr_nofilehandle;
3055 * We don't take advantage of the rca_one_fs case.
3056 * That's OK, it's optional, we can safely ignore it.
3061 status = nfserr_complete_already;
3062 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
3063 &cstate->session->se_client->cl_flags))
3066 status = nfserr_stale_clientid;
3067 if (is_client_expired(cstate->session->se_client))
3069 * The following error isn't really legal.
3070 * But we only get here if the client just explicitly
3071 * destroyed the client. Surely it no longer cares what
3072 * error it gets back on an operation for the dead
3078 nfsd4_client_record_create(cstate->session->se_client);
3084 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3085 struct nfsd4_setclientid *setclid)
3087 struct xdr_netobj clname = setclid->se_name;
3088 nfs4_verifier clverifier = setclid->se_verf;
3089 struct nfs4_client *conf, *new;
3090 struct nfs4_client *unconf = NULL;
3092 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3094 new = create_client(clname, rqstp, &clverifier);
3096 return nfserr_jukebox;
3097 /* Cases below refer to rfc 3530 section 14.2.33: */
3098 spin_lock(&nn->client_lock);
3099 conf = find_confirmed_client_by_name(&clname, nn);
3100 if (conf && client_has_state(conf)) {
3102 status = nfserr_clid_inuse;
3103 if (clp_used_exchangeid(conf))
3105 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
3106 char addr_str[INET6_ADDRSTRLEN];
3107 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
3109 dprintk("NFSD: setclientid: string in use by client "
3110 "at %s\n", addr_str);
3114 unconf = find_unconfirmed_client_by_name(&clname, nn);
3116 unhash_client_locked(unconf);
3117 if (conf && same_verf(&conf->cl_verifier, &clverifier)) {
3118 /* case 1: probable callback update */
3119 copy_clid(new, conf);
3120 gen_confirm(new, nn);
3121 } else /* case 4 (new client) or cases 2, 3 (client reboot): */
3123 new->cl_minorversion = 0;
3124 gen_callback(new, setclid, rqstp);
3125 add_to_unconfirmed(new);
3126 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
3127 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
3128 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
3132 spin_unlock(&nn->client_lock);
3136 expire_client(unconf);
3142 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
3143 struct nfsd4_compound_state *cstate,
3144 struct nfsd4_setclientid_confirm *setclientid_confirm)
3146 struct nfs4_client *conf, *unconf;
3147 struct nfs4_client *old = NULL;
3148 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
3149 clientid_t * clid = &setclientid_confirm->sc_clientid;
3151 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3153 if (STALE_CLIENTID(clid, nn))
3154 return nfserr_stale_clientid;
3156 spin_lock(&nn->client_lock);
3157 conf = find_confirmed_client(clid, false, nn);
3158 unconf = find_unconfirmed_client(clid, false, nn);
3160 * We try hard to give out unique clientid's, so if we get an
3161 * attempt to confirm the same clientid with a different cred,
3162 * the client may be buggy; this should never happen.
3164 * Nevertheless, RFC 7530 recommends INUSE for this case:
3166 status = nfserr_clid_inuse;
3167 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3169 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3171 /* cases below refer to rfc 3530 section 14.2.34: */
3172 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
3173 if (conf && !unconf) /* case 2: probable retransmit */
3175 else /* case 4: client hasn't noticed we rebooted yet? */
3176 status = nfserr_stale_clientid;
3180 if (conf) { /* case 1: callback update */
3182 unhash_client_locked(old);
3183 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
3184 } else { /* case 3: normal case; new or rebooted client */
3185 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3187 status = nfserr_clid_inuse;
3188 if (client_has_state(old)
3189 && !same_creds(&unconf->cl_cred,
3192 status = mark_client_expired_locked(old);
3198 move_to_confirmed(unconf);
3201 get_client_locked(conf);
3202 spin_unlock(&nn->client_lock);
3203 nfsd4_probe_callback(conf);
3204 spin_lock(&nn->client_lock);
3205 put_client_renew_locked(conf);
3207 spin_unlock(&nn->client_lock);
3213 static struct nfs4_file *nfsd4_alloc_file(void)
3215 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3218 /* OPEN Share state helper functions */
3219 static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
3220 struct nfs4_file *fp)
3222 lockdep_assert_held(&state_lock);
3224 atomic_set(&fp->fi_ref, 1);
3225 spin_lock_init(&fp->fi_lock);
3226 INIT_LIST_HEAD(&fp->fi_stateids);
3227 INIT_LIST_HEAD(&fp->fi_delegations);
3228 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
3229 fh_copy_shallow(&fp->fi_fhandle, fh);
3230 fp->fi_deleg_file = NULL;
3231 fp->fi_had_conflict = false;
3232 fp->fi_share_deny = 0;
3233 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3234 memset(fp->fi_access, 0, sizeof(fp->fi_access));
3235 #ifdef CONFIG_NFSD_PNFS
3236 INIT_LIST_HEAD(&fp->fi_lo_states);
3237 atomic_set(&fp->fi_lo_recalls, 0);
3239 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
3243 nfsd4_free_slabs(void)
3245 kmem_cache_destroy(odstate_slab);
3246 kmem_cache_destroy(openowner_slab);
3247 kmem_cache_destroy(lockowner_slab);
3248 kmem_cache_destroy(file_slab);
3249 kmem_cache_destroy(stateid_slab);
3250 kmem_cache_destroy(deleg_slab);
3254 nfsd4_init_slabs(void)
3256 openowner_slab = kmem_cache_create("nfsd4_openowners",
3257 sizeof(struct nfs4_openowner), 0, 0, NULL);
3258 if (openowner_slab == NULL)
3260 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
3261 sizeof(struct nfs4_lockowner), 0, 0, NULL);
3262 if (lockowner_slab == NULL)
3263 goto out_free_openowner_slab;
3264 file_slab = kmem_cache_create("nfsd4_files",
3265 sizeof(struct nfs4_file), 0, 0, NULL);
3266 if (file_slab == NULL)
3267 goto out_free_lockowner_slab;
3268 stateid_slab = kmem_cache_create("nfsd4_stateids",
3269 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
3270 if (stateid_slab == NULL)
3271 goto out_free_file_slab;
3272 deleg_slab = kmem_cache_create("nfsd4_delegations",
3273 sizeof(struct nfs4_delegation), 0, 0, NULL);
3274 if (deleg_slab == NULL)
3275 goto out_free_stateid_slab;
3276 odstate_slab = kmem_cache_create("nfsd4_odstate",
3277 sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
3278 if (odstate_slab == NULL)
3279 goto out_free_deleg_slab;
3282 out_free_deleg_slab:
3283 kmem_cache_destroy(deleg_slab);
3284 out_free_stateid_slab:
3285 kmem_cache_destroy(stateid_slab);
3287 kmem_cache_destroy(file_slab);
3288 out_free_lockowner_slab:
3289 kmem_cache_destroy(lockowner_slab);
3290 out_free_openowner_slab:
3291 kmem_cache_destroy(openowner_slab);
3293 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3297 static void init_nfs4_replay(struct nfs4_replay *rp)
3299 rp->rp_status = nfserr_serverfault;
3301 rp->rp_buf = rp->rp_ibuf;
3302 mutex_init(&rp->rp_mutex);
3305 static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3306 struct nfs4_stateowner *so)
3308 if (!nfsd4_has_session(cstate)) {
3309 mutex_lock(&so->so_replay.rp_mutex);
3310 cstate->replay_owner = nfs4_get_stateowner(so);
3314 void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3316 struct nfs4_stateowner *so = cstate->replay_owner;
3319 cstate->replay_owner = NULL;
3320 mutex_unlock(&so->so_replay.rp_mutex);
3321 nfs4_put_stateowner(so);
3325 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
3327 struct nfs4_stateowner *sop;
3329 sop = kmem_cache_alloc(slab, GFP_KERNEL);
3333 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3334 if (!sop->so_owner.data) {
3335 kmem_cache_free(slab, sop);
3338 sop->so_owner.len = owner->len;
3340 INIT_LIST_HEAD(&sop->so_stateids);
3341 sop->so_client = clp;
3342 init_nfs4_replay(&sop->so_replay);
3343 atomic_set(&sop->so_count, 1);
3347 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
3349 lockdep_assert_held(&clp->cl_lock);
3351 list_add(&oo->oo_owner.so_strhash,
3352 &clp->cl_ownerstr_hashtbl[strhashval]);
3353 list_add(&oo->oo_perclient, &clp->cl_openowners);
3356 static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3358 unhash_openowner_locked(openowner(so));
3361 static void nfs4_free_openowner(struct nfs4_stateowner *so)
3363 struct nfs4_openowner *oo = openowner(so);
3365 kmem_cache_free(openowner_slab, oo);
3368 static const struct nfs4_stateowner_operations openowner_ops = {
3369 .so_unhash = nfs4_unhash_openowner,
3370 .so_free = nfs4_free_openowner,
3373 static struct nfs4_ol_stateid *
3374 nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
3376 struct nfs4_ol_stateid *local, *ret = NULL;
3377 struct nfs4_openowner *oo = open->op_openowner;
3379 lockdep_assert_held(&fp->fi_lock);
3381 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
3382 /* ignore lock owners */
3383 if (local->st_stateowner->so_is_open_owner == 0)
3385 if (local->st_stateowner != &oo->oo_owner)
3387 if (local->st_stid.sc_type == NFS4_OPEN_STID) {
3389 atomic_inc(&ret->st_stid.sc_count);
3397 nfsd4_verify_open_stid(struct nfs4_stid *s)
3399 __be32 ret = nfs_ok;
3401 switch (s->sc_type) {
3404 case NFS4_CLOSED_STID:
3405 case NFS4_CLOSED_DELEG_STID:
3406 ret = nfserr_bad_stateid;
3408 case NFS4_REVOKED_DELEG_STID:
3409 ret = nfserr_deleg_revoked;
3414 /* Lock the stateid st_mutex, and deal with races with CLOSE */
3416 nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp)
3420 mutex_lock(&stp->st_mutex);
3421 ret = nfsd4_verify_open_stid(&stp->st_stid);
3423 mutex_unlock(&stp->st_mutex);
3427 static struct nfs4_ol_stateid *
3428 nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
3430 struct nfs4_ol_stateid *stp;
3432 spin_lock(&fp->fi_lock);
3433 stp = nfsd4_find_existing_open(fp, open);
3434 spin_unlock(&fp->fi_lock);
3435 if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok)
3437 nfs4_put_stid(&stp->st_stid);
3442 static struct nfs4_openowner *
3443 alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
3444 struct nfsd4_compound_state *cstate)
3446 struct nfs4_client *clp = cstate->clp;
3447 struct nfs4_openowner *oo, *ret;
3449 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3452 oo->oo_owner.so_ops = &openowner_ops;
3453 oo->oo_owner.so_is_open_owner = 1;
3454 oo->oo_owner.so_seqid = open->op_seqid;
3456 if (nfsd4_has_session(cstate))
3457 oo->oo_flags |= NFS4_OO_CONFIRMED;
3459 oo->oo_last_closed_stid = NULL;
3460 INIT_LIST_HEAD(&oo->oo_close_lru);
3461 spin_lock(&clp->cl_lock);
3462 ret = find_openstateowner_str_locked(strhashval, open, clp);
3464 hash_openowner(oo, clp, strhashval);
3467 nfs4_free_stateowner(&oo->oo_owner);
3469 spin_unlock(&clp->cl_lock);
3473 static struct nfs4_ol_stateid *
3474 init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
3477 struct nfs4_openowner *oo = open->op_openowner;
3478 struct nfs4_ol_stateid *retstp = NULL;
3479 struct nfs4_ol_stateid *stp;
3482 /* We are moving these outside of the spinlocks to avoid the warnings */
3483 mutex_init(&stp->st_mutex);
3484 mutex_lock(&stp->st_mutex);
3487 spin_lock(&oo->oo_owner.so_client->cl_lock);
3488 spin_lock(&fp->fi_lock);
3490 retstp = nfsd4_find_existing_open(fp, open);
3494 open->op_stp = NULL;
3495 atomic_inc(&stp->st_stid.sc_count);
3496 stp->st_stid.sc_type = NFS4_OPEN_STID;
3497 INIT_LIST_HEAD(&stp->st_locks);
3498 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
3500 stp->st_stid.sc_file = fp;
3501 stp->st_access_bmap = 0;
3502 stp->st_deny_bmap = 0;
3503 stp->st_openstp = NULL;
3504 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
3505 list_add(&stp->st_perfile, &fp->fi_stateids);
3508 spin_unlock(&fp->fi_lock);
3509 spin_unlock(&oo->oo_owner.so_client->cl_lock);
3511 /* Handle races with CLOSE */
3512 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
3513 nfs4_put_stid(&retstp->st_stid);
3516 /* To keep mutex tracking happy */
3517 mutex_unlock(&stp->st_mutex);
3524 * In the 4.0 case we need to keep the owners around a little while to handle
3525 * CLOSE replay. We still do need to release any file access that is held by
3526 * them before returning however.
3529 move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
3531 struct nfs4_ol_stateid *last;
3532 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3533 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3536 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
3539 * We know that we hold one reference via nfsd4_close, and another
3540 * "persistent" reference for the client. If the refcount is higher
3541 * than 2, then there are still calls in progress that are using this
3542 * stateid. We can't put the sc_file reference until they are finished.
3543 * Wait for the refcount to drop to 2. Since it has been unhashed,
3544 * there should be no danger of the refcount going back up again at
3547 wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3549 release_all_access(s);
3550 if (s->st_stid.sc_file) {
3551 put_nfs4_file(s->st_stid.sc_file);
3552 s->st_stid.sc_file = NULL;
3555 spin_lock(&nn->client_lock);
3556 last = oo->oo_last_closed_stid;
3557 oo->oo_last_closed_stid = s;
3558 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
3559 oo->oo_time = get_seconds();
3560 spin_unlock(&nn->client_lock);
3562 nfs4_put_stid(&last->st_stid);
3565 /* search file_hashtbl[] for file */
3566 static struct nfs4_file *
3567 find_file_locked(struct knfsd_fh *fh, unsigned int hashval)
3569 struct nfs4_file *fp;
3571 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash) {
3572 if (fh_match(&fp->fi_fhandle, fh)) {
3573 if (atomic_inc_not_zero(&fp->fi_ref))
3581 find_file(struct knfsd_fh *fh)
3583 struct nfs4_file *fp;
3584 unsigned int hashval = file_hashval(fh);
3587 fp = find_file_locked(fh, hashval);
3592 static struct nfs4_file *
3593 find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
3595 struct nfs4_file *fp;
3596 unsigned int hashval = file_hashval(fh);
3599 fp = find_file_locked(fh, hashval);
3604 spin_lock(&state_lock);
3605 fp = find_file_locked(fh, hashval);
3606 if (likely(fp == NULL)) {
3607 nfsd4_init_file(fh, hashval, new);
3610 spin_unlock(&state_lock);
3616 * Called to check deny when READ with all zero stateid or
3617 * WRITE with all zero or all one stateid
3620 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3622 struct nfs4_file *fp;
3623 __be32 ret = nfs_ok;
3625 fp = find_file(¤t_fh->fh_handle);
3628 /* Check for conflicting share reservations */
3629 spin_lock(&fp->fi_lock);
3630 if (fp->fi_share_deny & deny_type)
3631 ret = nfserr_locked;
3632 spin_unlock(&fp->fi_lock);
3637 static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
3639 struct nfs4_delegation *dp = cb_to_delegation(cb);
3640 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3643 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
3646 * We can't do this in nfsd_break_deleg_cb because it is
3647 * already holding inode->i_lock.
3649 * If the dl_time != 0, then we know that it has already been
3650 * queued for a lease break. Don't queue it again.
3652 spin_lock(&state_lock);
3653 if (dp->dl_time == 0) {
3654 dp->dl_time = get_seconds();
3655 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
3657 spin_unlock(&state_lock);
3660 static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
3661 struct rpc_task *task)
3663 struct nfs4_delegation *dp = cb_to_delegation(cb);
3665 if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID)
3668 switch (task->tk_status) {
3672 case -NFS4ERR_BAD_STATEID:
3674 * Race: client probably got cb_recall before open reply
3675 * granting delegation.
3677 if (dp->dl_retries--) {
3678 rpc_delay(task, 2 * HZ);
3687 static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
3689 struct nfs4_delegation *dp = cb_to_delegation(cb);
3691 nfs4_put_stid(&dp->dl_stid);
3694 static struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
3695 .prepare = nfsd4_cb_recall_prepare,
3696 .done = nfsd4_cb_recall_done,
3697 .release = nfsd4_cb_recall_release,
3700 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3703 * We're assuming the state code never drops its reference
3704 * without first removing the lease. Since we're in this lease
3705 * callback (and since the lease code is serialized by the kernel
3706 * lock) we know the server hasn't removed the lease yet, we know
3707 * it's safe to take a reference.
3709 atomic_inc(&dp->dl_stid.sc_count);
3710 nfsd4_run_cb(&dp->dl_recall);
3713 /* Called from break_lease() with i_lock held. */
3715 nfsd_break_deleg_cb(struct file_lock *fl)
3718 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3719 struct nfs4_delegation *dp;
3722 WARN(1, "(%p)->fl_owner NULL\n", fl);
3725 if (fp->fi_had_conflict) {
3726 WARN(1, "duplicate break on %p\n", fp);
3730 * We don't want the locks code to timeout the lease for us;
3731 * we'll remove it ourself if a delegation isn't returned
3734 fl->fl_break_time = 0;
3736 spin_lock(&fp->fi_lock);
3737 fp->fi_had_conflict = true;
3739 * If there are no delegations on the list, then return true
3740 * so that the lease code will go ahead and delete it.
3742 if (list_empty(&fp->fi_delegations))
3745 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3746 nfsd_break_one_deleg(dp);
3747 spin_unlock(&fp->fi_lock);
3752 nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
3753 struct list_head *dispose)
3756 return lease_modify(onlist, arg, dispose);
3761 static const struct lock_manager_operations nfsd_lease_mng_ops = {
3762 .lm_break = nfsd_break_deleg_cb,
3763 .lm_change = nfsd_change_deleg_cb,
3766 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3768 if (nfsd4_has_session(cstate))
3770 if (seqid == so->so_seqid - 1)
3771 return nfserr_replay_me;
3772 if (seqid == so->so_seqid)
3774 return nfserr_bad_seqid;
3777 static __be32 lookup_clientid(clientid_t *clid,
3778 struct nfsd4_compound_state *cstate,
3779 struct nfsd_net *nn)
3781 struct nfs4_client *found;
3784 found = cstate->clp;
3785 if (!same_clid(&found->cl_clientid, clid))
3786 return nfserr_stale_clientid;
3790 if (STALE_CLIENTID(clid, nn))
3791 return nfserr_stale_clientid;
3794 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3795 * cached already then we know this is for is for v4.0 and "sessions"
3798 WARN_ON_ONCE(cstate->session);
3799 spin_lock(&nn->client_lock);
3800 found = find_confirmed_client(clid, false, nn);
3802 spin_unlock(&nn->client_lock);
3803 return nfserr_expired;
3805 atomic_inc(&found->cl_refcount);
3806 spin_unlock(&nn->client_lock);
3808 /* Cache the nfs4_client in cstate! */
3809 cstate->clp = found;
3814 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
3815 struct nfsd4_open *open, struct nfsd_net *nn)
3817 clientid_t *clientid = &open->op_clientid;
3818 struct nfs4_client *clp = NULL;
3819 unsigned int strhashval;
3820 struct nfs4_openowner *oo = NULL;
3823 if (STALE_CLIENTID(&open->op_clientid, nn))
3824 return nfserr_stale_clientid;
3826 * In case we need it later, after we've already created the
3827 * file and don't want to risk a further failure:
3829 open->op_file = nfsd4_alloc_file();
3830 if (open->op_file == NULL)
3831 return nfserr_jukebox;
3833 status = lookup_clientid(clientid, cstate, nn);
3838 strhashval = ownerstr_hashval(&open->op_owner);
3839 oo = find_openstateowner_str(strhashval, open, clp);
3840 open->op_openowner = oo;
3844 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
3845 /* Replace unconfirmed owners without checking for replay. */
3846 release_openowner(oo);
3847 open->op_openowner = NULL;
3850 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3855 oo = alloc_init_open_stateowner(strhashval, open, cstate);
3857 return nfserr_jukebox;
3858 open->op_openowner = oo;
3860 open->op_stp = nfs4_alloc_open_stateid(clp);
3862 return nfserr_jukebox;
3864 if (nfsd4_has_session(cstate) &&
3865 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
3866 open->op_odstate = alloc_clnt_odstate(clp);
3867 if (!open->op_odstate)
3868 return nfserr_jukebox;
3874 static inline __be32
3875 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3877 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3878 return nfserr_openmode;
3883 static int share_access_to_flags(u32 share_access)
3885 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3888 static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
3890 struct nfs4_stid *ret;
3892 ret = find_stateid_by_type(cl, s,
3893 NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID);
3896 return delegstateid(ret);
3899 static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3901 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3902 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3906 nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
3907 struct nfs4_delegation **dp)
3910 __be32 status = nfserr_bad_stateid;
3911 struct nfs4_delegation *deleg;
3913 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3916 if (deleg->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) {
3917 nfs4_put_stid(&deleg->dl_stid);
3918 if (cl->cl_minorversion)
3919 status = nfserr_deleg_revoked;
3922 flags = share_access_to_flags(open->op_share_access);
3923 status = nfs4_check_delegmode(deleg, flags);
3925 nfs4_put_stid(&deleg->dl_stid);
3930 if (!nfsd4_is_deleg_cur(open))
3934 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
3938 static inline int nfs4_access_to_access(u32 nfs4_access)
3942 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3943 flags |= NFSD_MAY_READ;
3944 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3945 flags |= NFSD_MAY_WRITE;
3949 static inline __be32
3950 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3951 struct nfsd4_open *open)
3953 struct iattr iattr = {
3954 .ia_valid = ATTR_SIZE,
3957 if (!open->op_truncate)
3959 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
3960 return nfserr_inval;
3961 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3964 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
3965 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3966 struct nfsd4_open *open)
3968 struct file *filp = NULL;
3970 int oflag = nfs4_access_to_omode(open->op_share_access);
3971 int access = nfs4_access_to_access(open->op_share_access);
3972 unsigned char old_access_bmap, old_deny_bmap;
3974 spin_lock(&fp->fi_lock);
3977 * Are we trying to set a deny mode that would conflict with
3980 status = nfs4_file_check_deny(fp, open->op_share_deny);
3981 if (status != nfs_ok) {
3982 spin_unlock(&fp->fi_lock);
3986 /* set access to the file */
3987 status = nfs4_file_get_access(fp, open->op_share_access);
3988 if (status != nfs_ok) {
3989 spin_unlock(&fp->fi_lock);
3993 /* Set access bits in stateid */
3994 old_access_bmap = stp->st_access_bmap;
3995 set_access(open->op_share_access, stp);
3997 /* Set new deny mask */
3998 old_deny_bmap = stp->st_deny_bmap;
3999 set_deny(open->op_share_deny, stp);
4000 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4002 if (!fp->fi_fds[oflag]) {
4003 spin_unlock(&fp->fi_lock);
4004 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
4006 goto out_put_access;
4007 spin_lock(&fp->fi_lock);
4008 if (!fp->fi_fds[oflag]) {
4009 fp->fi_fds[oflag] = filp;
4013 spin_unlock(&fp->fi_lock);
4017 status = nfsd4_truncate(rqstp, cur_fh, open);
4019 goto out_put_access;
4023 stp->st_access_bmap = old_access_bmap;
4024 nfs4_file_put_access(fp, open->op_share_access);
4025 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
4030 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)
4033 unsigned char old_deny_bmap = stp->st_deny_bmap;
4035 if (!test_access(open->op_share_access, stp))
4036 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
4038 /* test and set deny mode */
4039 spin_lock(&fp->fi_lock);
4040 status = nfs4_file_check_deny(fp, open->op_share_deny);
4041 if (status == nfs_ok) {
4042 set_deny(open->op_share_deny, stp);
4043 fp->fi_share_deny |=
4044 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4046 spin_unlock(&fp->fi_lock);
4048 if (status != nfs_ok)
4051 status = nfsd4_truncate(rqstp, cur_fh, open);
4052 if (status != nfs_ok)
4053 reset_union_bmap_deny(old_deny_bmap, stp);
4057 /* Should we give out recallable state?: */
4058 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
4060 if (clp->cl_cb_state == NFSD4_CB_UP)
4063 * In the sessions case, since we don't have to establish a
4064 * separate connection for callbacks, we assume it's OK
4065 * until we hear otherwise:
4067 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
4070 static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
4072 struct file_lock *fl;
4074 fl = locks_alloc_lock();
4077 fl->fl_lmops = &nfsd_lease_mng_ops;
4078 fl->fl_flags = FL_DELEG;
4079 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
4080 fl->fl_end = OFFSET_MAX;
4081 fl->fl_owner = (fl_owner_t)fp;
4082 fl->fl_pid = current->tgid;
4087 * nfs4_setlease - Obtain a delegation by requesting lease from vfs layer
4088 * @dp: a pointer to the nfs4_delegation we're adding.
4091 * On success: Return code will be 0 on success.
4093 * On error: -EAGAIN if there was an existing delegation.
4094 * nonzero if there is an error in other cases.
4098 static int nfs4_setlease(struct nfs4_delegation *dp)
4100 struct nfs4_file *fp = dp->dl_stid.sc_file;
4101 struct file_lock *fl;
4105 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
4108 filp = find_readable_file(fp);
4110 /* We should always have a readable file here */
4112 locks_free_lock(fl);
4116 status = vfs_setlease(filp, fl->fl_type, &fl, NULL);
4118 locks_free_lock(fl);
4121 spin_lock(&state_lock);
4122 spin_lock(&fp->fi_lock);
4123 /* Did the lease get broken before we took the lock? */
4125 if (fp->fi_had_conflict)
4128 if (fp->fi_deleg_file) {
4129 status = hash_delegation_locked(dp, fp);
4132 fp->fi_deleg_file = filp;
4133 fp->fi_delegees = 0;
4134 status = hash_delegation_locked(dp, fp);
4135 spin_unlock(&fp->fi_lock);
4136 spin_unlock(&state_lock);
4138 /* Should never happen, this is a new fi_deleg_file */
4144 spin_unlock(&fp->fi_lock);
4145 spin_unlock(&state_lock);
4151 static struct nfs4_delegation *
4152 nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
4153 struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate)
4156 struct nfs4_delegation *dp;
4158 if (fp->fi_had_conflict)
4159 return ERR_PTR(-EAGAIN);
4161 spin_lock(&state_lock);
4162 spin_lock(&fp->fi_lock);
4163 status = nfs4_get_existing_delegation(clp, fp);
4164 spin_unlock(&fp->fi_lock);
4165 spin_unlock(&state_lock);
4168 return ERR_PTR(status);
4170 dp = alloc_init_deleg(clp, fh, odstate);
4172 return ERR_PTR(-ENOMEM);
4175 spin_lock(&state_lock);
4176 spin_lock(&fp->fi_lock);
4177 dp->dl_stid.sc_file = fp;
4178 if (!fp->fi_deleg_file) {
4179 spin_unlock(&fp->fi_lock);
4180 spin_unlock(&state_lock);
4181 status = nfs4_setlease(dp);
4184 if (fp->fi_had_conflict) {
4188 status = hash_delegation_locked(dp, fp);
4190 spin_unlock(&fp->fi_lock);
4191 spin_unlock(&state_lock);
4194 put_clnt_odstate(dp->dl_clnt_odstate);
4195 nfs4_put_stid(&dp->dl_stid);
4196 return ERR_PTR(status);
4201 static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
4203 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4204 if (status == -EAGAIN)
4205 open->op_why_no_deleg = WND4_CONTENTION;
4207 open->op_why_no_deleg = WND4_RESOURCE;
4208 switch (open->op_deleg_want) {
4209 case NFS4_SHARE_WANT_READ_DELEG:
4210 case NFS4_SHARE_WANT_WRITE_DELEG:
4211 case NFS4_SHARE_WANT_ANY_DELEG:
4213 case NFS4_SHARE_WANT_CANCEL:
4214 open->op_why_no_deleg = WND4_CANCELLED;
4216 case NFS4_SHARE_WANT_NO_DELEG:
4223 * Attempt to hand out a delegation.
4225 * Note we don't support write delegations, and won't until the vfs has
4226 * proper support for them.
4229 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
4230 struct nfs4_ol_stateid *stp)
4232 struct nfs4_delegation *dp;
4233 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
4234 struct nfs4_client *clp = stp->st_stid.sc_client;
4238 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
4239 open->op_recall = 0;
4240 switch (open->op_claim_type) {
4241 case NFS4_OPEN_CLAIM_PREVIOUS:
4243 open->op_recall = 1;
4244 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
4247 case NFS4_OPEN_CLAIM_NULL:
4248 case NFS4_OPEN_CLAIM_FH:
4250 * Let's not give out any delegations till everyone's
4251 * had the chance to reclaim theirs, *and* until
4252 * NLM locks have all been reclaimed:
4254 if (locks_in_grace(clp->net))
4256 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
4259 * Also, if the file was opened for write or
4260 * create, there's a good chance the client's
4261 * about to write to it, resulting in an
4262 * immediate recall (since we don't support
4263 * write delegations):
4265 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
4267 if (open->op_create == NFS4_OPEN_CREATE)
4273 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate);
4277 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
4279 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
4280 STATEID_VAL(&dp->dl_stid.sc_stateid));
4281 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
4282 nfs4_put_stid(&dp->dl_stid);
4285 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
4286 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
4287 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
4288 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
4289 open->op_recall = 1;
4292 /* 4.1 client asking for a delegation? */
4293 if (open->op_deleg_want)
4294 nfsd4_open_deleg_none_ext(open, status);
4298 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
4299 struct nfs4_delegation *dp)
4301 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
4302 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4303 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4304 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
4305 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
4306 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4307 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4308 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
4310 /* Otherwise the client must be confused wanting a delegation
4311 * it already has, therefore we don't return
4312 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
4317 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
4319 struct nfsd4_compoundres *resp = rqstp->rq_resp;
4320 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
4321 struct nfs4_file *fp = NULL;
4322 struct nfs4_ol_stateid *stp = NULL;
4323 struct nfs4_delegation *dp = NULL;
4325 bool new_stp = false;
4328 * Lookup file; if found, lookup stateid and check open request,
4329 * and check for delegations in the process of being recalled.
4330 * If not found, create the nfs4_file struct
4332 fp = find_or_add_file(open->op_file, ¤t_fh->fh_handle);
4333 if (fp != open->op_file) {
4334 status = nfs4_check_deleg(cl, open, &dp);
4337 stp = nfsd4_find_and_lock_existing_open(fp, open);
4339 open->op_file = NULL;
4340 status = nfserr_bad_stateid;
4341 if (nfsd4_is_deleg_cur(open))
4346 stp = init_open_stateid(fp, open);
4352 * OPEN the file, or upgrade an existing OPEN.
4353 * If truncate fails, the OPEN fails.
4355 * stp is already locked.
4358 /* Stateid was found, this is an OPEN upgrade */
4359 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
4361 mutex_unlock(&stp->st_mutex);
4365 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4367 stp->st_stid.sc_type = NFS4_CLOSED_STID;
4368 release_open_stateid(stp);
4369 mutex_unlock(&stp->st_mutex);
4373 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
4375 if (stp->st_clnt_odstate == open->op_odstate)
4376 open->op_odstate = NULL;
4379 nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
4380 mutex_unlock(&stp->st_mutex);
4382 if (nfsd4_has_session(&resp->cstate)) {
4383 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4384 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4385 open->op_why_no_deleg = WND4_NOT_WANTED;
4391 * Attempt to hand out a delegation. No error return, because the
4392 * OPEN succeeds even if we fail.
4394 nfs4_open_delegation(current_fh, open, stp);
4398 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
4399 STATEID_VAL(&stp->st_stid.sc_stateid));
4401 /* 4.1 client trying to upgrade/downgrade delegation? */
4402 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
4403 open->op_deleg_want)
4404 nfsd4_deleg_xgrade_none_ext(open, dp);
4408 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
4409 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
4411 * To finish the open response, we just need to set the rflags.
4413 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
4414 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
4415 !nfsd4_has_session(&resp->cstate))
4416 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
4418 nfs4_put_stid(&dp->dl_stid);
4420 nfs4_put_stid(&stp->st_stid);
4425 void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
4426 struct nfsd4_open *open)
4428 if (open->op_openowner) {
4429 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
4431 nfsd4_cstate_assign_replay(cstate, so);
4432 nfs4_put_stateowner(so);
4435 kmem_cache_free(file_slab, open->op_file);
4437 nfs4_put_stid(&open->op_stp->st_stid);
4438 if (open->op_odstate)
4439 kmem_cache_free(odstate_slab, open->op_odstate);
4443 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4446 struct nfs4_client *clp;
4448 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4450 dprintk("process_renew(%08x/%08x): starting\n",
4451 clid->cl_boot, clid->cl_id);
4452 status = lookup_clientid(clid, cstate, nn);
4456 status = nfserr_cb_path_down;
4457 if (!list_empty(&clp->cl_delegations)
4458 && clp->cl_cb_state != NFSD4_CB_UP)
4466 nfsd4_end_grace(struct nfsd_net *nn)
4468 /* do nothing if grace period already ended */
4469 if (nn->grace_ended)
4472 dprintk("NFSD: end of grace period\n");
4473 nn->grace_ended = true;
4475 * If the server goes down again right now, an NFSv4
4476 * client will still be allowed to reclaim after it comes back up,
4477 * even if it hasn't yet had a chance to reclaim state this time.
4480 nfsd4_record_grace_done(nn);
4482 * At this point, NFSv4 clients can still reclaim. But if the
4483 * server crashes, any that have not yet reclaimed will be out
4484 * of luck on the next boot.
4486 * (NFSv4.1+ clients are considered to have reclaimed once they
4487 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
4488 * have reclaimed after their first OPEN.)
4490 locks_end_grace(&nn->nfsd4_manager);
4492 * At this point, and once lockd and/or any other containers
4493 * exit their grace period, further reclaims will fail and
4494 * regular locking can resume.
4499 nfs4_laundromat(struct nfsd_net *nn)
4501 struct nfs4_client *clp;
4502 struct nfs4_openowner *oo;
4503 struct nfs4_delegation *dp;
4504 struct nfs4_ol_stateid *stp;
4505 struct list_head *pos, *next, reaplist;
4506 time_t cutoff = get_seconds() - nn->nfsd4_lease;
4507 time_t t, new_timeo = nn->nfsd4_lease;
4509 dprintk("NFSD: laundromat service - starting\n");
4510 nfsd4_end_grace(nn);
4511 INIT_LIST_HEAD(&reaplist);
4512 spin_lock(&nn->client_lock);
4513 list_for_each_safe(pos, next, &nn->client_lru) {
4514 clp = list_entry(pos, struct nfs4_client, cl_lru);
4515 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4516 t = clp->cl_time - cutoff;
4517 new_timeo = min(new_timeo, t);
4520 if (mark_client_expired_locked(clp)) {
4521 dprintk("NFSD: client in use (clientid %08x)\n",
4522 clp->cl_clientid.cl_id);
4525 list_add(&clp->cl_lru, &reaplist);
4527 spin_unlock(&nn->client_lock);
4528 list_for_each_safe(pos, next, &reaplist) {
4529 clp = list_entry(pos, struct nfs4_client, cl_lru);
4530 dprintk("NFSD: purging unused client (clientid %08x)\n",
4531 clp->cl_clientid.cl_id);
4532 list_del_init(&clp->cl_lru);
4535 spin_lock(&state_lock);
4536 list_for_each_safe(pos, next, &nn->del_recall_lru) {
4537 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4538 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
4539 t = dp->dl_time - cutoff;
4540 new_timeo = min(new_timeo, t);
4543 WARN_ON(!unhash_delegation_locked(dp));
4544 list_add(&dp->dl_recall_lru, &reaplist);
4546 spin_unlock(&state_lock);
4547 while (!list_empty(&reaplist)) {
4548 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4550 list_del_init(&dp->dl_recall_lru);
4551 revoke_delegation(dp);
4554 spin_lock(&nn->client_lock);
4555 while (!list_empty(&nn->close_lru)) {
4556 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
4558 if (time_after((unsigned long)oo->oo_time,
4559 (unsigned long)cutoff)) {
4560 t = oo->oo_time - cutoff;
4561 new_timeo = min(new_timeo, t);
4564 list_del_init(&oo->oo_close_lru);
4565 stp = oo->oo_last_closed_stid;
4566 oo->oo_last_closed_stid = NULL;
4567 spin_unlock(&nn->client_lock);
4568 nfs4_put_stid(&stp->st_stid);
4569 spin_lock(&nn->client_lock);
4571 spin_unlock(&nn->client_lock);
4573 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
4577 static struct workqueue_struct *laundry_wq;
4578 static void laundromat_main(struct work_struct *);
4581 laundromat_main(struct work_struct *laundry)
4584 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
4586 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4589 t = nfs4_laundromat(nn);
4590 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
4591 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
4594 static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
4596 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
4597 return nfserr_bad_stateid;
4602 access_permit_read(struct nfs4_ol_stateid *stp)
4604 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4605 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4606 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
4610 access_permit_write(struct nfs4_ol_stateid *stp)
4612 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4613 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
4617 __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
4619 __be32 status = nfserr_openmode;
4621 /* For lock stateid's, we test the parent open, not the lock: */
4622 if (stp->st_openstp)
4623 stp = stp->st_openstp;
4624 if ((flags & WR_STATE) && !access_permit_write(stp))
4626 if ((flags & RD_STATE) && !access_permit_read(stp))
4633 static inline __be32
4634 check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
4636 if (ONE_STATEID(stateid) && (flags & RD_STATE))
4638 else if (opens_in_grace(net)) {
4639 /* Answer in remaining cases depends on existence of
4640 * conflicting state; so we must wait out the grace period. */
4641 return nfserr_grace;
4642 } else if (flags & WR_STATE)
4643 return nfs4_share_conflict(current_fh,
4644 NFS4_SHARE_DENY_WRITE);
4645 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4646 return nfs4_share_conflict(current_fh,
4647 NFS4_SHARE_DENY_READ);
4651 * Allow READ/WRITE during grace period on recovered state only for files
4652 * that are not able to provide mandatory locking.
4655 grace_disallows_io(struct net *net, struct inode *inode)
4657 return opens_in_grace(net) && mandatory_lock(inode);
4660 /* Returns true iff a is later than b: */
4661 static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4663 return (s32)(a->si_generation - b->si_generation) > 0;
4666 static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
4669 * When sessions are used the stateid generation number is ignored
4672 if (has_session && in->si_generation == 0)
4675 if (in->si_generation == ref->si_generation)
4678 /* If the client sends us a stateid from the future, it's buggy: */
4679 if (stateid_generation_after(in, ref))
4680 return nfserr_bad_stateid;
4682 * However, we could see a stateid from the past, even from a
4683 * non-buggy client. For example, if the client sends a lock
4684 * while some IO is outstanding, the lock may bump si_generation
4685 * while the IO is still in flight. The client could avoid that
4686 * situation by waiting for responses on all the IO requests,
4687 * but better performance may result in retrying IO that
4688 * receives an old_stateid error if requests are rarely
4689 * reordered in flight:
4691 return nfserr_old_stateid;
4694 static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
4696 if (ols->st_stateowner->so_is_open_owner &&
4697 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
4698 return nfserr_bad_stateid;
4702 static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
4704 struct nfs4_stid *s;
4705 __be32 status = nfserr_bad_stateid;
4707 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4709 /* Client debugging aid. */
4710 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4711 char addr_str[INET6_ADDRSTRLEN];
4712 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4714 pr_warn_ratelimited("NFSD: client %s testing state ID "
4715 "with incorrect client ID\n", addr_str);
4718 spin_lock(&cl->cl_lock);
4719 s = find_stateid_locked(cl, stateid);
4722 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
4725 switch (s->sc_type) {
4726 case NFS4_DELEG_STID:
4729 case NFS4_REVOKED_DELEG_STID:
4730 status = nfserr_deleg_revoked;
4732 case NFS4_OPEN_STID:
4733 case NFS4_LOCK_STID:
4734 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
4737 printk("unknown stateid type %x\n", s->sc_type);
4739 case NFS4_CLOSED_STID:
4740 case NFS4_CLOSED_DELEG_STID:
4741 status = nfserr_bad_stateid;
4744 spin_unlock(&cl->cl_lock);
4749 nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4750 stateid_t *stateid, unsigned char typemask,
4751 struct nfs4_stid **s, struct nfsd_net *nn)
4754 bool return_revoked = false;
4757 * only return revoked delegations if explicitly asked.
4758 * otherwise we report revoked or bad_stateid status.
4760 if (typemask & NFS4_REVOKED_DELEG_STID)
4761 return_revoked = true;
4762 else if (typemask & NFS4_DELEG_STID)
4763 typemask |= NFS4_REVOKED_DELEG_STID;
4765 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4766 return nfserr_bad_stateid;
4767 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
4768 if (status == nfserr_stale_clientid) {
4769 if (cstate->session)
4770 return nfserr_bad_stateid;
4771 return nfserr_stale_stateid;
4775 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
4777 return nfserr_bad_stateid;
4778 if (((*s)->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) {
4780 if (cstate->minorversion)
4781 return nfserr_deleg_revoked;
4782 return nfserr_bad_stateid;
4787 static struct file *
4788 nfs4_find_file(struct nfs4_stid *s, int flags)
4793 switch (s->sc_type) {
4794 case NFS4_DELEG_STID:
4795 if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
4797 return get_file(s->sc_file->fi_deleg_file);
4798 case NFS4_OPEN_STID:
4799 case NFS4_LOCK_STID:
4800 if (flags & RD_STATE)
4801 return find_readable_file(s->sc_file);
4803 return find_writeable_file(s->sc_file);
4811 nfs4_check_olstateid(struct svc_fh *fhp, struct nfs4_ol_stateid *ols, int flags)
4815 status = nfsd4_check_openowner_confirmed(ols);
4818 return nfs4_check_openmode(ols, flags);
4822 nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
4823 struct file **filpp, bool *tmp_file, int flags)
4825 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
4829 file = nfs4_find_file(s, flags);
4831 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
4832 acc | NFSD_MAY_OWNER_OVERRIDE);
4840 status = nfsd_open(rqstp, fhp, S_IFREG, acc, filpp);
4852 * Checks for stateid operations
4855 nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
4856 struct nfsd4_compound_state *cstate, stateid_t *stateid,
4857 int flags, struct file **filpp, bool *tmp_file)
4859 struct svc_fh *fhp = &cstate->current_fh;
4860 struct inode *ino = d_inode(fhp->fh_dentry);
4861 struct net *net = SVC_NET(rqstp);
4862 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4863 struct nfs4_stid *s = NULL;
4871 if (grace_disallows_io(net, ino))
4872 return nfserr_grace;
4874 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
4875 status = check_special_stateids(net, fhp, stateid, flags);
4879 status = nfsd4_lookup_stateid(cstate, stateid,
4880 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
4884 status = check_stateid_generation(stateid, &s->sc_stateid,
4885 nfsd4_has_session(cstate));
4889 switch (s->sc_type) {
4890 case NFS4_DELEG_STID:
4891 status = nfs4_check_delegmode(delegstateid(s), flags);
4893 case NFS4_OPEN_STID:
4894 case NFS4_LOCK_STID:
4895 status = nfs4_check_olstateid(fhp, openlockstateid(s), flags);
4898 status = nfserr_bad_stateid;
4903 status = nfs4_check_fh(fhp, s);
4906 if (!status && filpp)
4907 status = nfs4_check_file(rqstp, fhp, s, filpp, tmp_file, flags);
4915 * Test if the stateid is valid
4918 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4919 struct nfsd4_test_stateid *test_stateid)
4921 struct nfsd4_test_stateid_id *stateid;
4922 struct nfs4_client *cl = cstate->session->se_client;
4924 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
4925 stateid->ts_id_status =
4926 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
4932 nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
4934 struct nfs4_ol_stateid *stp = openlockstateid(s);
4937 mutex_lock(&stp->st_mutex);
4939 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4943 ret = nfserr_locks_held;
4944 if (check_for_locks(stp->st_stid.sc_file,
4945 lockowner(stp->st_stateowner)))
4948 release_lock_stateid(stp);
4952 mutex_unlock(&stp->st_mutex);
4958 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4959 struct nfsd4_free_stateid *free_stateid)
4961 stateid_t *stateid = &free_stateid->fr_stateid;
4962 struct nfs4_stid *s;
4963 struct nfs4_delegation *dp;
4964 struct nfs4_client *cl = cstate->session->se_client;
4965 __be32 ret = nfserr_bad_stateid;
4967 spin_lock(&cl->cl_lock);
4968 s = find_stateid_locked(cl, stateid);
4971 switch (s->sc_type) {
4972 case NFS4_DELEG_STID:
4973 ret = nfserr_locks_held;
4975 case NFS4_OPEN_STID:
4976 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4979 ret = nfserr_locks_held;
4981 case NFS4_LOCK_STID:
4982 atomic_inc(&s->sc_count);
4983 spin_unlock(&cl->cl_lock);
4984 ret = nfsd4_free_lock_stateid(stateid, s);
4986 case NFS4_REVOKED_DELEG_STID:
4987 dp = delegstateid(s);
4988 list_del_init(&dp->dl_recall_lru);
4989 spin_unlock(&cl->cl_lock);
4993 /* Default falls through and returns nfserr_bad_stateid */
4996 spin_unlock(&cl->cl_lock);
5004 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
5005 RD_STATE : WR_STATE;
5008 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
5010 struct svc_fh *current_fh = &cstate->current_fh;
5011 struct nfs4_stateowner *sop = stp->st_stateowner;
5014 status = nfsd4_check_seqid(cstate, sop, seqid);
5017 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
5018 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
5020 * "Closed" stateid's exist *only* to return
5021 * nfserr_replay_me from the previous step, and
5022 * revoked delegations are kept only for free_stateid.
5024 return nfserr_bad_stateid;
5025 mutex_lock(&stp->st_mutex);
5026 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
5027 if (status == nfs_ok)
5028 status = nfs4_check_fh(current_fh, &stp->st_stid);
5029 if (status != nfs_ok)
5030 mutex_unlock(&stp->st_mutex);
5035 * Checks for sequence id mutating operations.
5038 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
5039 stateid_t *stateid, char typemask,
5040 struct nfs4_ol_stateid **stpp,
5041 struct nfsd_net *nn)
5044 struct nfs4_stid *s;
5045 struct nfs4_ol_stateid *stp = NULL;
5047 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
5048 seqid, STATEID_VAL(stateid));
5051 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
5054 stp = openlockstateid(s);
5055 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
5057 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
5061 nfs4_put_stid(&stp->st_stid);
5065 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
5066 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
5069 struct nfs4_openowner *oo;
5070 struct nfs4_ol_stateid *stp;
5072 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
5073 NFS4_OPEN_STID, &stp, nn);
5076 oo = openowner(stp->st_stateowner);
5077 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
5078 mutex_unlock(&stp->st_mutex);
5079 nfs4_put_stid(&stp->st_stid);
5080 return nfserr_bad_stateid;
5087 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5088 struct nfsd4_open_confirm *oc)
5091 struct nfs4_openowner *oo;
5092 struct nfs4_ol_stateid *stp;
5093 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5095 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
5096 cstate->current_fh.fh_dentry);
5098 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
5102 status = nfs4_preprocess_seqid_op(cstate,
5103 oc->oc_seqid, &oc->oc_req_stateid,
5104 NFS4_OPEN_STID, &stp, nn);
5107 oo = openowner(stp->st_stateowner);
5108 status = nfserr_bad_stateid;
5109 if (oo->oo_flags & NFS4_OO_CONFIRMED) {
5110 mutex_unlock(&stp->st_mutex);
5113 oo->oo_flags |= NFS4_OO_CONFIRMED;
5114 nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
5115 mutex_unlock(&stp->st_mutex);
5116 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
5117 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
5119 nfsd4_client_record_create(oo->oo_owner.so_client);
5122 nfs4_put_stid(&stp->st_stid);
5124 nfsd4_bump_seqid(cstate, status);
5128 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
5130 if (!test_access(access, stp))
5132 nfs4_file_put_access(stp->st_stid.sc_file, access);
5133 clear_access(access, stp);
5136 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
5138 switch (to_access) {
5139 case NFS4_SHARE_ACCESS_READ:
5140 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
5141 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
5143 case NFS4_SHARE_ACCESS_WRITE:
5144 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
5145 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
5147 case NFS4_SHARE_ACCESS_BOTH:
5155 nfsd4_open_downgrade(struct svc_rqst *rqstp,
5156 struct nfsd4_compound_state *cstate,
5157 struct nfsd4_open_downgrade *od)
5160 struct nfs4_ol_stateid *stp;
5161 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5163 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
5164 cstate->current_fh.fh_dentry);
5166 /* We don't yet support WANT bits: */
5167 if (od->od_deleg_want)
5168 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
5171 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
5172 &od->od_stateid, &stp, nn);
5175 status = nfserr_inval;
5176 if (!test_access(od->od_share_access, stp)) {
5177 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
5178 stp->st_access_bmap, od->od_share_access);
5181 if (!test_deny(od->od_share_deny, stp)) {
5182 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
5183 stp->st_deny_bmap, od->od_share_deny);
5186 nfs4_stateid_downgrade(stp, od->od_share_access);
5187 reset_union_bmap_deny(od->od_share_deny, stp);
5188 nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
5191 mutex_unlock(&stp->st_mutex);
5192 nfs4_put_stid(&stp->st_stid);
5194 nfsd4_bump_seqid(cstate, status);
5198 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
5200 struct nfs4_client *clp = s->st_stid.sc_client;
5202 LIST_HEAD(reaplist);
5204 spin_lock(&clp->cl_lock);
5205 unhashed = unhash_open_stateid(s, &reaplist);
5207 if (clp->cl_minorversion) {
5209 put_ol_stateid_locked(s, &reaplist);
5210 spin_unlock(&clp->cl_lock);
5211 free_ol_stateid_reaplist(&reaplist);
5213 spin_unlock(&clp->cl_lock);
5214 free_ol_stateid_reaplist(&reaplist);
5216 move_to_close_lru(s, clp->net);
5221 * nfs4_unlock_state() called after encode
5224 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5225 struct nfsd4_close *close)
5228 struct nfs4_ol_stateid *stp;
5229 struct net *net = SVC_NET(rqstp);
5230 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5232 dprintk("NFSD: nfsd4_close on file %pd\n",
5233 cstate->current_fh.fh_dentry);
5235 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
5237 NFS4_OPEN_STID|NFS4_CLOSED_STID,
5239 nfsd4_bump_seqid(cstate, status);
5243 stp->st_stid.sc_type = NFS4_CLOSED_STID;
5244 nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
5246 nfsd4_close_open_stateid(stp);
5247 mutex_unlock(&stp->st_mutex);
5249 /* See RFC5661 sectionm 18.2.4 */
5250 if (stp->st_stid.sc_client->cl_minorversion)
5251 memcpy(&close->cl_stateid, &close_stateid,
5252 sizeof(close->cl_stateid));
5254 /* put reference from nfs4_preprocess_seqid_op */
5255 nfs4_put_stid(&stp->st_stid);
5261 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5262 struct nfsd4_delegreturn *dr)
5264 struct nfs4_delegation *dp;
5265 stateid_t *stateid = &dr->dr_stateid;
5266 struct nfs4_stid *s;
5268 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5270 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
5273 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
5276 dp = delegstateid(s);
5277 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
5281 destroy_delegation(dp);
5283 nfs4_put_stid(&dp->dl_stid);
5289 end_offset(u64 start, u64 len)
5294 return end >= start ? end: NFS4_MAX_UINT64;
5297 /* last octet in a range */
5299 last_byte_offset(u64 start, u64 len)
5305 return end > start ? end - 1: NFS4_MAX_UINT64;
5309 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
5310 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
5311 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
5312 * locking, this prevents us from being completely protocol-compliant. The
5313 * real solution to this problem is to start using unsigned file offsets in
5314 * the VFS, but this is a very deep change!
5317 nfs4_transform_lock_offset(struct file_lock *lock)
5319 if (lock->fl_start < 0)
5320 lock->fl_start = OFFSET_MAX;
5321 if (lock->fl_end < 0)
5322 lock->fl_end = OFFSET_MAX;
5326 nfsd4_fl_get_owner(fl_owner_t owner)
5328 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
5330 nfs4_get_stateowner(&lo->lo_owner);
5335 nfsd4_fl_put_owner(fl_owner_t owner)
5337 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
5340 nfs4_put_stateowner(&lo->lo_owner);
5343 static const struct lock_manager_operations nfsd_posix_mng_ops = {
5344 .lm_get_owner = nfsd4_fl_get_owner,
5345 .lm_put_owner = nfsd4_fl_put_owner,
5349 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
5351 struct nfs4_lockowner *lo;
5353 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
5354 lo = (struct nfs4_lockowner *) fl->fl_owner;
5355 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
5356 lo->lo_owner.so_owner.len, GFP_KERNEL);
5357 if (!deny->ld_owner.data)
5358 /* We just don't care that much */
5360 deny->ld_owner.len = lo->lo_owner.so_owner.len;
5361 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
5364 deny->ld_owner.len = 0;
5365 deny->ld_owner.data = NULL;
5366 deny->ld_clientid.cl_boot = 0;
5367 deny->ld_clientid.cl_id = 0;
5369 deny->ld_start = fl->fl_start;
5370 deny->ld_length = NFS4_MAX_UINT64;
5371 if (fl->fl_end != NFS4_MAX_UINT64)
5372 deny->ld_length = fl->fl_end - fl->fl_start + 1;
5373 deny->ld_type = NFS4_READ_LT;
5374 if (fl->fl_type != F_RDLCK)
5375 deny->ld_type = NFS4_WRITE_LT;
5378 static struct nfs4_lockowner *
5379 find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
5381 unsigned int strhashval = ownerstr_hashval(owner);
5382 struct nfs4_stateowner *so;
5384 lockdep_assert_held(&clp->cl_lock);
5386 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
5388 if (so->so_is_open_owner)
5390 if (same_owner_str(so, owner))
5391 return lockowner(nfs4_get_stateowner(so));
5396 static struct nfs4_lockowner *
5397 find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
5399 struct nfs4_lockowner *lo;
5401 spin_lock(&clp->cl_lock);
5402 lo = find_lockowner_str_locked(clp, owner);
5403 spin_unlock(&clp->cl_lock);
5407 static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
5409 unhash_lockowner_locked(lockowner(sop));
5412 static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
5414 struct nfs4_lockowner *lo = lockowner(sop);
5416 kmem_cache_free(lockowner_slab, lo);
5419 static const struct nfs4_stateowner_operations lockowner_ops = {
5420 .so_unhash = nfs4_unhash_lockowner,
5421 .so_free = nfs4_free_lockowner,
5425 * Alloc a lock owner structure.
5426 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
5429 * strhashval = ownerstr_hashval
5431 static struct nfs4_lockowner *
5432 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
5433 struct nfs4_ol_stateid *open_stp,
5434 struct nfsd4_lock *lock)
5436 struct nfs4_lockowner *lo, *ret;
5438 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
5441 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
5442 lo->lo_owner.so_is_open_owner = 0;
5443 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
5444 lo->lo_owner.so_ops = &lockowner_ops;
5445 spin_lock(&clp->cl_lock);
5446 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
5448 list_add(&lo->lo_owner.so_strhash,
5449 &clp->cl_ownerstr_hashtbl[strhashval]);
5452 nfs4_free_stateowner(&lo->lo_owner);
5454 spin_unlock(&clp->cl_lock);
5459 init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
5460 struct nfs4_file *fp, struct inode *inode,
5461 struct nfs4_ol_stateid *open_stp)
5463 struct nfs4_client *clp = lo->lo_owner.so_client;
5465 lockdep_assert_held(&clp->cl_lock);
5467 atomic_inc(&stp->st_stid.sc_count);
5468 stp->st_stid.sc_type = NFS4_LOCK_STID;
5469 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
5471 stp->st_stid.sc_file = fp;
5472 stp->st_access_bmap = 0;
5473 stp->st_deny_bmap = open_stp->st_deny_bmap;
5474 stp->st_openstp = open_stp;
5475 mutex_init(&stp->st_mutex);
5476 list_add(&stp->st_locks, &open_stp->st_locks);
5477 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
5478 spin_lock(&fp->fi_lock);
5479 list_add(&stp->st_perfile, &fp->fi_stateids);
5480 spin_unlock(&fp->fi_lock);
5483 static struct nfs4_ol_stateid *
5484 find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5486 struct nfs4_ol_stateid *lst;
5487 struct nfs4_client *clp = lo->lo_owner.so_client;
5489 lockdep_assert_held(&clp->cl_lock);
5491 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
5492 if (lst->st_stid.sc_file == fp) {
5493 atomic_inc(&lst->st_stid.sc_count);
5500 static struct nfs4_ol_stateid *
5501 find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5502 struct inode *inode, struct nfs4_ol_stateid *ost,
5505 struct nfs4_stid *ns = NULL;
5506 struct nfs4_ol_stateid *lst;
5507 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5508 struct nfs4_client *clp = oo->oo_owner.so_client;
5510 spin_lock(&clp->cl_lock);
5511 lst = find_lock_stateid(lo, fi);
5513 spin_unlock(&clp->cl_lock);
5514 ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid);
5518 spin_lock(&clp->cl_lock);
5519 lst = find_lock_stateid(lo, fi);
5521 lst = openlockstateid(ns);
5522 init_lock_stateid(lst, lo, fi, inode, ost);
5527 spin_unlock(&clp->cl_lock);
5534 check_lock_length(u64 offset, u64 length)
5536 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
5537 (length > ~offset)));
5540 static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
5542 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
5544 lockdep_assert_held(&fp->fi_lock);
5546 if (test_access(access, lock_stp))
5548 __nfs4_file_get_access(fp, access);
5549 set_access(access, lock_stp);
5553 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5554 struct nfs4_ol_stateid *ost,
5555 struct nfsd4_lock *lock,
5556 struct nfs4_ol_stateid **plst, bool *new)
5559 struct nfs4_file *fi = ost->st_stid.sc_file;
5560 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5561 struct nfs4_client *cl = oo->oo_owner.so_client;
5562 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
5563 struct nfs4_lockowner *lo;
5564 struct nfs4_ol_stateid *lst;
5565 unsigned int strhashval;
5568 lo = find_lockowner_str(cl, &lock->lk_new_owner);
5570 strhashval = ownerstr_hashval(&lock->lk_new_owner);
5571 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5573 return nfserr_jukebox;
5575 /* with an existing lockowner, seqids must be the same */
5576 status = nfserr_bad_seqid;
5577 if (!cstate->minorversion &&
5578 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
5583 lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
5585 status = nfserr_jukebox;
5589 mutex_lock(&lst->st_mutex);
5591 /* See if it's still hashed to avoid race with FREE_STATEID */
5592 spin_lock(&cl->cl_lock);
5593 hashed = !list_empty(&lst->st_perfile);
5594 spin_unlock(&cl->cl_lock);
5597 mutex_unlock(&lst->st_mutex);
5598 nfs4_put_stid(&lst->st_stid);
5604 nfs4_put_stateowner(&lo->lo_owner);
5612 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5613 struct nfsd4_lock *lock)
5615 struct nfs4_openowner *open_sop = NULL;
5616 struct nfs4_lockowner *lock_sop = NULL;
5617 struct nfs4_ol_stateid *lock_stp = NULL;
5618 struct nfs4_ol_stateid *open_stp = NULL;
5619 struct nfs4_file *fp;
5620 struct file *filp = NULL;
5621 struct file_lock *file_lock = NULL;
5622 struct file_lock *conflock = NULL;
5627 struct net *net = SVC_NET(rqstp);
5628 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5630 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5631 (long long) lock->lk_offset,
5632 (long long) lock->lk_length);
5634 if (check_lock_length(lock->lk_offset, lock->lk_length))
5635 return nfserr_inval;
5637 if ((status = fh_verify(rqstp, &cstate->current_fh,
5638 S_IFREG, NFSD_MAY_LOCK))) {
5639 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5643 if (lock->lk_is_new) {
5644 if (nfsd4_has_session(cstate))
5645 /* See rfc 5661 18.10.3: given clientid is ignored: */
5646 memcpy(&lock->lk_new_clientid,
5647 &cstate->session->se_client->cl_clientid,
5648 sizeof(clientid_t));
5650 status = nfserr_stale_clientid;
5651 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
5654 /* validate and update open stateid and open seqid */
5655 status = nfs4_preprocess_confirmed_seqid_op(cstate,
5656 lock->lk_new_open_seqid,
5657 &lock->lk_new_open_stateid,
5661 mutex_unlock(&open_stp->st_mutex);
5662 open_sop = openowner(open_stp->st_stateowner);
5663 status = nfserr_bad_stateid;
5664 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
5665 &lock->lk_new_clientid))
5667 status = lookup_or_create_lock_state(cstate, open_stp, lock,
5670 status = nfs4_preprocess_seqid_op(cstate,
5671 lock->lk_old_lock_seqid,
5672 &lock->lk_old_lock_stateid,
5673 NFS4_LOCK_STID, &lock_stp, nn);
5677 lock_sop = lockowner(lock_stp->st_stateowner);
5679 lkflg = setlkflg(lock->lk_type);
5680 status = nfs4_check_openmode(lock_stp, lkflg);
5684 status = nfserr_grace;
5685 if (locks_in_grace(net) && !lock->lk_reclaim)
5687 status = nfserr_no_grace;
5688 if (!locks_in_grace(net) && lock->lk_reclaim)
5691 file_lock = locks_alloc_lock();
5693 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5694 status = nfserr_jukebox;
5698 fp = lock_stp->st_stid.sc_file;
5699 switch (lock->lk_type) {
5702 spin_lock(&fp->fi_lock);
5703 filp = find_readable_file_locked(fp);
5705 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
5706 spin_unlock(&fp->fi_lock);
5707 file_lock->fl_type = F_RDLCK;
5710 case NFS4_WRITEW_LT:
5711 spin_lock(&fp->fi_lock);
5712 filp = find_writeable_file_locked(fp);
5714 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
5715 spin_unlock(&fp->fi_lock);
5716 file_lock->fl_type = F_WRLCK;
5719 status = nfserr_inval;
5723 status = nfserr_openmode;
5727 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
5728 file_lock->fl_pid = current->tgid;
5729 file_lock->fl_file = filp;
5730 file_lock->fl_flags = FL_POSIX;
5731 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5732 file_lock->fl_start = lock->lk_offset;
5733 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5734 nfs4_transform_lock_offset(file_lock);
5736 conflock = locks_alloc_lock();
5738 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5739 status = nfserr_jukebox;
5743 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
5745 case 0: /* success! */
5746 nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
5749 case (EAGAIN): /* conflock holds conflicting lock */
5750 status = nfserr_denied;
5751 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
5752 nfs4_set_lock_denied(conflock, &lock->lk_denied);
5755 status = nfserr_deadlock;
5758 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
5759 status = nfserrno(err);
5766 /* Bump seqid manually if the 4.0 replay owner is openowner */
5767 if (cstate->replay_owner &&
5768 cstate->replay_owner != &lock_sop->lo_owner &&
5769 seqid_mutating_err(ntohl(status)))
5770 lock_sop->lo_owner.so_seqid++;
5772 mutex_unlock(&lock_stp->st_mutex);
5775 * If this is a new, never-before-used stateid, and we are
5776 * returning an error, then just go ahead and release it.
5779 release_lock_stateid(lock_stp);
5781 nfs4_put_stid(&lock_stp->st_stid);
5784 nfs4_put_stid(&open_stp->st_stid);
5785 nfsd4_bump_seqid(cstate, status);
5787 locks_free_lock(file_lock);
5789 locks_free_lock(conflock);
5794 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5795 * so we do a temporary open here just to get an open file to pass to
5796 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5799 static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
5802 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5804 err = nfserrno(vfs_test_lock(file, lock));
5814 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5815 struct nfsd4_lockt *lockt)
5817 struct file_lock *file_lock = NULL;
5818 struct nfs4_lockowner *lo = NULL;
5820 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5822 if (locks_in_grace(SVC_NET(rqstp)))
5823 return nfserr_grace;
5825 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5826 return nfserr_inval;
5828 if (!nfsd4_has_session(cstate)) {
5829 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
5834 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
5837 file_lock = locks_alloc_lock();
5839 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5840 status = nfserr_jukebox;
5844 switch (lockt->lt_type) {
5847 file_lock->fl_type = F_RDLCK;
5850 case NFS4_WRITEW_LT:
5851 file_lock->fl_type = F_WRLCK;
5854 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
5855 status = nfserr_inval;
5859 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
5861 file_lock->fl_owner = (fl_owner_t)lo;
5862 file_lock->fl_pid = current->tgid;
5863 file_lock->fl_flags = FL_POSIX;
5865 file_lock->fl_start = lockt->lt_offset;
5866 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
5868 nfs4_transform_lock_offset(file_lock);
5870 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
5874 if (file_lock->fl_type != F_UNLCK) {
5875 status = nfserr_denied;
5876 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
5880 nfs4_put_stateowner(&lo->lo_owner);
5882 locks_free_lock(file_lock);
5887 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5888 struct nfsd4_locku *locku)
5890 struct nfs4_ol_stateid *stp;
5891 struct file *filp = NULL;
5892 struct file_lock *file_lock = NULL;
5895 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5897 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5898 (long long) locku->lu_offset,
5899 (long long) locku->lu_length);
5901 if (check_lock_length(locku->lu_offset, locku->lu_length))
5902 return nfserr_inval;
5904 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
5905 &locku->lu_stateid, NFS4_LOCK_STID,
5909 filp = find_any_file(stp->st_stid.sc_file);
5911 status = nfserr_lock_range;
5914 file_lock = locks_alloc_lock();
5916 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5917 status = nfserr_jukebox;
5921 file_lock->fl_type = F_UNLCK;
5922 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
5923 file_lock->fl_pid = current->tgid;
5924 file_lock->fl_file = filp;
5925 file_lock->fl_flags = FL_POSIX;
5926 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5927 file_lock->fl_start = locku->lu_offset;
5929 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5931 nfs4_transform_lock_offset(file_lock);
5933 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
5935 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
5938 nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
5942 mutex_unlock(&stp->st_mutex);
5943 nfs4_put_stid(&stp->st_stid);
5945 nfsd4_bump_seqid(cstate, status);
5947 locks_free_lock(file_lock);
5951 status = nfserrno(err);
5957 * true: locks held by lockowner
5958 * false: no locks held by lockowner
5961 check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
5963 struct file_lock *fl;
5965 struct file *filp = find_any_file(fp);
5966 struct inode *inode;
5967 struct file_lock_context *flctx;
5970 /* Any valid lock stateid should have some sort of access */
5975 inode = file_inode(filp);
5976 flctx = inode->i_flctx;
5978 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
5979 spin_lock(&flctx->flc_lock);
5980 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
5981 if (fl->fl_owner == (fl_owner_t)lowner) {
5986 spin_unlock(&flctx->flc_lock);
5993 nfsd4_release_lockowner(struct svc_rqst *rqstp,
5994 struct nfsd4_compound_state *cstate,
5995 struct nfsd4_release_lockowner *rlockowner)
5997 clientid_t *clid = &rlockowner->rl_clientid;
5998 struct nfs4_stateowner *sop;
5999 struct nfs4_lockowner *lo = NULL;
6000 struct nfs4_ol_stateid *stp;
6001 struct xdr_netobj *owner = &rlockowner->rl_owner;
6002 unsigned int hashval = ownerstr_hashval(owner);
6004 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6005 struct nfs4_client *clp;
6006 LIST_HEAD (reaplist);
6008 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
6009 clid->cl_boot, clid->cl_id);
6011 status = lookup_clientid(clid, cstate, nn);
6016 /* Find the matching lock stateowner */
6017 spin_lock(&clp->cl_lock);
6018 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
6021 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
6024 /* see if there are still any locks associated with it */
6025 lo = lockowner(sop);
6026 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
6027 if (check_for_locks(stp->st_stid.sc_file, lo)) {
6028 status = nfserr_locks_held;
6029 spin_unlock(&clp->cl_lock);
6034 nfs4_get_stateowner(sop);
6038 spin_unlock(&clp->cl_lock);
6042 unhash_lockowner_locked(lo);
6043 while (!list_empty(&lo->lo_owner.so_stateids)) {
6044 stp = list_first_entry(&lo->lo_owner.so_stateids,
6045 struct nfs4_ol_stateid,
6047 WARN_ON(!unhash_lock_stateid(stp));
6048 put_ol_stateid_locked(stp, &reaplist);
6050 spin_unlock(&clp->cl_lock);
6051 free_ol_stateid_reaplist(&reaplist);
6052 nfs4_put_stateowner(&lo->lo_owner);
6057 static inline struct nfs4_client_reclaim *
6060 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
6064 nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
6066 struct nfs4_client_reclaim *crp;
6068 crp = nfsd4_find_reclaim_client(name, nn);
6069 return (crp && crp->cr_clp);
6073 * failure => all reset bets are off, nfserr_no_grace...
6075 struct nfs4_client_reclaim *
6076 nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
6078 unsigned int strhashval;
6079 struct nfs4_client_reclaim *crp;
6081 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
6082 crp = alloc_reclaim();
6084 strhashval = clientstr_hashval(name);
6085 INIT_LIST_HEAD(&crp->cr_strhash);
6086 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
6087 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
6089 nn->reclaim_str_hashtbl_size++;
6095 nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
6097 list_del(&crp->cr_strhash);
6099 nn->reclaim_str_hashtbl_size--;
6103 nfs4_release_reclaim(struct nfsd_net *nn)
6105 struct nfs4_client_reclaim *crp = NULL;
6108 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6109 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
6110 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
6111 struct nfs4_client_reclaim, cr_strhash);
6112 nfs4_remove_reclaim_record(crp, nn);
6115 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
6119 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
6120 struct nfs4_client_reclaim *
6121 nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
6123 unsigned int strhashval;
6124 struct nfs4_client_reclaim *crp = NULL;
6126 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
6128 strhashval = clientstr_hashval(recdir);
6129 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
6130 if (same_name(crp->cr_recdir, recdir)) {
6138 * Called from OPEN. Look for clientid in reclaim list.
6141 nfs4_check_open_reclaim(clientid_t *clid,
6142 struct nfsd4_compound_state *cstate,
6143 struct nfsd_net *nn)
6147 /* find clientid in conf_id_hashtbl */
6148 status = lookup_clientid(clid, cstate, nn);
6150 return nfserr_reclaim_bad;
6152 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
6153 return nfserr_no_grace;
6155 if (nfsd4_client_record_check(cstate->clp))
6156 return nfserr_reclaim_bad;
6161 #ifdef CONFIG_NFSD_FAULT_INJECTION
6163 put_client(struct nfs4_client *clp)
6165 atomic_dec(&clp->cl_refcount);
6168 static struct nfs4_client *
6169 nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
6171 struct nfs4_client *clp;
6172 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6175 if (!nfsd_netns_ready(nn))
6178 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6179 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
6186 nfsd_inject_print_clients(void)
6188 struct nfs4_client *clp;
6190 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6192 char buf[INET6_ADDRSTRLEN];
6194 if (!nfsd_netns_ready(nn))
6197 spin_lock(&nn->client_lock);
6198 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6199 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
6200 pr_info("NFS Client: %s\n", buf);
6203 spin_unlock(&nn->client_lock);
6209 nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
6212 struct nfs4_client *clp;
6213 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6216 if (!nfsd_netns_ready(nn))
6219 spin_lock(&nn->client_lock);
6220 clp = nfsd_find_client(addr, addr_size);
6222 if (mark_client_expired_locked(clp) == nfs_ok)
6227 spin_unlock(&nn->client_lock);
6236 nfsd_inject_forget_clients(u64 max)
6239 struct nfs4_client *clp, *next;
6240 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6242 LIST_HEAD(reaplist);
6244 if (!nfsd_netns_ready(nn))
6247 spin_lock(&nn->client_lock);
6248 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6249 if (mark_client_expired_locked(clp) == nfs_ok) {
6250 list_add(&clp->cl_lru, &reaplist);
6251 if (max != 0 && ++count >= max)
6255 spin_unlock(&nn->client_lock);
6257 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
6263 static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
6266 char buf[INET6_ADDRSTRLEN];
6267 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
6268 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
6272 nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
6273 struct list_head *collect)
6275 struct nfs4_client *clp = lst->st_stid.sc_client;
6276 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6282 lockdep_assert_held(&nn->client_lock);
6283 atomic_inc(&clp->cl_refcount);
6284 list_add(&lst->st_locks, collect);
6287 static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
6288 struct list_head *collect,
6289 bool (*func)(struct nfs4_ol_stateid *))
6291 struct nfs4_openowner *oop;
6292 struct nfs4_ol_stateid *stp, *st_next;
6293 struct nfs4_ol_stateid *lst, *lst_next;
6296 spin_lock(&clp->cl_lock);
6297 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
6298 list_for_each_entry_safe(stp, st_next,
6299 &oop->oo_owner.so_stateids, st_perstateowner) {
6300 list_for_each_entry_safe(lst, lst_next,
6301 &stp->st_locks, st_locks) {
6304 nfsd_inject_add_lock_to_list(lst,
6309 * Despite the fact that these functions deal
6310 * with 64-bit integers for "count", we must
6311 * ensure that it doesn't blow up the
6312 * clp->cl_refcount. Throw a warning if we
6313 * start to approach INT_MAX here.
6315 WARN_ON_ONCE(count == (INT_MAX / 2));
6322 spin_unlock(&clp->cl_lock);
6328 nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
6331 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
6335 nfsd_print_client_locks(struct nfs4_client *clp)
6337 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
6338 nfsd_print_count(clp, count, "locked files");
6343 nfsd_inject_print_locks(void)
6345 struct nfs4_client *clp;
6347 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6350 if (!nfsd_netns_ready(nn))
6353 spin_lock(&nn->client_lock);
6354 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6355 count += nfsd_print_client_locks(clp);
6356 spin_unlock(&nn->client_lock);
6362 nfsd_reap_locks(struct list_head *reaplist)
6364 struct nfs4_client *clp;
6365 struct nfs4_ol_stateid *stp, *next;
6367 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
6368 list_del_init(&stp->st_locks);
6369 clp = stp->st_stid.sc_client;
6370 nfs4_put_stid(&stp->st_stid);
6376 nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
6378 unsigned int count = 0;
6379 struct nfs4_client *clp;
6380 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6382 LIST_HEAD(reaplist);
6384 if (!nfsd_netns_ready(nn))
6387 spin_lock(&nn->client_lock);
6388 clp = nfsd_find_client(addr, addr_size);
6390 count = nfsd_collect_client_locks(clp, &reaplist, 0);
6391 spin_unlock(&nn->client_lock);
6392 nfsd_reap_locks(&reaplist);
6397 nfsd_inject_forget_locks(u64 max)
6400 struct nfs4_client *clp;
6401 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6403 LIST_HEAD(reaplist);
6405 if (!nfsd_netns_ready(nn))
6408 spin_lock(&nn->client_lock);
6409 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6410 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
6411 if (max != 0 && count >= max)
6414 spin_unlock(&nn->client_lock);
6415 nfsd_reap_locks(&reaplist);
6420 nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
6421 struct list_head *collect,
6422 void (*func)(struct nfs4_openowner *))
6424 struct nfs4_openowner *oop, *next;
6425 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6429 lockdep_assert_held(&nn->client_lock);
6431 spin_lock(&clp->cl_lock);
6432 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
6436 atomic_inc(&clp->cl_refcount);
6437 list_add(&oop->oo_perclient, collect);
6442 * Despite the fact that these functions deal with
6443 * 64-bit integers for "count", we must ensure that
6444 * it doesn't blow up the clp->cl_refcount. Throw a
6445 * warning if we start to approach INT_MAX here.
6447 WARN_ON_ONCE(count == (INT_MAX / 2));
6451 spin_unlock(&clp->cl_lock);
6457 nfsd_print_client_openowners(struct nfs4_client *clp)
6459 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
6461 nfsd_print_count(clp, count, "openowners");
6466 nfsd_collect_client_openowners(struct nfs4_client *clp,
6467 struct list_head *collect, u64 max)
6469 return nfsd_foreach_client_openowner(clp, max, collect,
6470 unhash_openowner_locked);
6474 nfsd_inject_print_openowners(void)
6476 struct nfs4_client *clp;
6478 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6481 if (!nfsd_netns_ready(nn))
6484 spin_lock(&nn->client_lock);
6485 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6486 count += nfsd_print_client_openowners(clp);
6487 spin_unlock(&nn->client_lock);
6493 nfsd_reap_openowners(struct list_head *reaplist)
6495 struct nfs4_client *clp;
6496 struct nfs4_openowner *oop, *next;
6498 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
6499 list_del_init(&oop->oo_perclient);
6500 clp = oop->oo_owner.so_client;
6501 release_openowner(oop);
6507 nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
6510 unsigned int count = 0;
6511 struct nfs4_client *clp;
6512 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6514 LIST_HEAD(reaplist);
6516 if (!nfsd_netns_ready(nn))
6519 spin_lock(&nn->client_lock);
6520 clp = nfsd_find_client(addr, addr_size);
6522 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
6523 spin_unlock(&nn->client_lock);
6524 nfsd_reap_openowners(&reaplist);
6529 nfsd_inject_forget_openowners(u64 max)
6532 struct nfs4_client *clp;
6533 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6535 LIST_HEAD(reaplist);
6537 if (!nfsd_netns_ready(nn))
6540 spin_lock(&nn->client_lock);
6541 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6542 count += nfsd_collect_client_openowners(clp, &reaplist,
6544 if (max != 0 && count >= max)
6547 spin_unlock(&nn->client_lock);
6548 nfsd_reap_openowners(&reaplist);
6552 static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
6553 struct list_head *victims)
6555 struct nfs4_delegation *dp, *next;
6556 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6560 lockdep_assert_held(&nn->client_lock);
6562 spin_lock(&state_lock);
6563 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
6566 * It's not safe to mess with delegations that have a
6567 * non-zero dl_time. They might have already been broken
6568 * and could be processed by the laundromat outside of
6569 * the state_lock. Just leave them be.
6571 if (dp->dl_time != 0)
6574 atomic_inc(&clp->cl_refcount);
6575 WARN_ON(!unhash_delegation_locked(dp));
6576 list_add(&dp->dl_recall_lru, victims);
6580 * Despite the fact that these functions deal with
6581 * 64-bit integers for "count", we must ensure that
6582 * it doesn't blow up the clp->cl_refcount. Throw a
6583 * warning if we start to approach INT_MAX here.
6585 WARN_ON_ONCE(count == (INT_MAX / 2));
6589 spin_unlock(&state_lock);
6594 nfsd_print_client_delegations(struct nfs4_client *clp)
6596 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
6598 nfsd_print_count(clp, count, "delegations");
6603 nfsd_inject_print_delegations(void)
6605 struct nfs4_client *clp;
6607 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6610 if (!nfsd_netns_ready(nn))
6613 spin_lock(&nn->client_lock);
6614 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6615 count += nfsd_print_client_delegations(clp);
6616 spin_unlock(&nn->client_lock);
6622 nfsd_forget_delegations(struct list_head *reaplist)
6624 struct nfs4_client *clp;
6625 struct nfs4_delegation *dp, *next;
6627 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6628 list_del_init(&dp->dl_recall_lru);
6629 clp = dp->dl_stid.sc_client;
6630 revoke_delegation(dp);
6636 nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
6640 struct nfs4_client *clp;
6641 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6643 LIST_HEAD(reaplist);
6645 if (!nfsd_netns_ready(nn))
6648 spin_lock(&nn->client_lock);
6649 clp = nfsd_find_client(addr, addr_size);
6651 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6652 spin_unlock(&nn->client_lock);
6654 nfsd_forget_delegations(&reaplist);
6659 nfsd_inject_forget_delegations(u64 max)
6662 struct nfs4_client *clp;
6663 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6665 LIST_HEAD(reaplist);
6667 if (!nfsd_netns_ready(nn))
6670 spin_lock(&nn->client_lock);
6671 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6672 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6673 if (max != 0 && count >= max)
6676 spin_unlock(&nn->client_lock);
6677 nfsd_forget_delegations(&reaplist);
6682 nfsd_recall_delegations(struct list_head *reaplist)
6684 struct nfs4_client *clp;
6685 struct nfs4_delegation *dp, *next;
6687 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6688 list_del_init(&dp->dl_recall_lru);
6689 clp = dp->dl_stid.sc_client;
6691 * We skipped all entries that had a zero dl_time before,
6692 * so we can now reset the dl_time back to 0. If a delegation
6693 * break comes in now, then it won't make any difference since
6694 * we're recalling it either way.
6696 spin_lock(&state_lock);
6698 spin_unlock(&state_lock);
6699 nfsd_break_one_deleg(dp);
6705 nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
6709 struct nfs4_client *clp;
6710 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6712 LIST_HEAD(reaplist);
6714 if (!nfsd_netns_ready(nn))
6717 spin_lock(&nn->client_lock);
6718 clp = nfsd_find_client(addr, addr_size);
6720 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6721 spin_unlock(&nn->client_lock);
6723 nfsd_recall_delegations(&reaplist);
6728 nfsd_inject_recall_delegations(u64 max)
6731 struct nfs4_client *clp, *next;
6732 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6734 LIST_HEAD(reaplist);
6736 if (!nfsd_netns_ready(nn))
6739 spin_lock(&nn->client_lock);
6740 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6741 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6742 if (max != 0 && ++count >= max)
6745 spin_unlock(&nn->client_lock);
6746 nfsd_recall_delegations(&reaplist);
6749 #endif /* CONFIG_NFSD_FAULT_INJECTION */
6752 * Since the lifetime of a delegation isn't limited to that of an open, a
6753 * client may quite reasonably hang on to a delegation as long as it has
6754 * the inode cached. This becomes an obvious problem the first time a
6755 * client's inode cache approaches the size of the server's total memory.
6757 * For now we avoid this problem by imposing a hard limit on the number
6758 * of delegations, which varies according to the server's memory size.
6761 set_max_delegations(void)
6764 * Allow at most 4 delegations per megabyte of RAM. Quick
6765 * estimates suggest that in the worst case (where every delegation
6766 * is for a different inode), a delegation could take about 1.5K,
6767 * giving a worst case usage of about 6% of memory.
6769 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
6772 static int nfs4_state_create_net(struct net *net)
6774 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6777 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6778 CLIENT_HASH_SIZE, GFP_KERNEL);
6779 if (!nn->conf_id_hashtbl)
6781 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6782 CLIENT_HASH_SIZE, GFP_KERNEL);
6783 if (!nn->unconf_id_hashtbl)
6785 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
6786 SESSION_HASH_SIZE, GFP_KERNEL);
6787 if (!nn->sessionid_hashtbl)
6790 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6791 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
6792 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
6794 for (i = 0; i < SESSION_HASH_SIZE; i++)
6795 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
6796 nn->conf_name_tree = RB_ROOT;
6797 nn->unconf_name_tree = RB_ROOT;
6798 INIT_LIST_HEAD(&nn->client_lru);
6799 INIT_LIST_HEAD(&nn->close_lru);
6800 INIT_LIST_HEAD(&nn->del_recall_lru);
6801 spin_lock_init(&nn->client_lock);
6803 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
6809 kfree(nn->unconf_id_hashtbl);
6811 kfree(nn->conf_id_hashtbl);
6817 nfs4_state_destroy_net(struct net *net)
6820 struct nfs4_client *clp = NULL;
6821 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6823 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6824 while (!list_empty(&nn->conf_id_hashtbl[i])) {
6825 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6826 destroy_client(clp);
6830 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6831 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
6832 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6833 destroy_client(clp);
6837 kfree(nn->sessionid_hashtbl);
6838 kfree(nn->unconf_id_hashtbl);
6839 kfree(nn->conf_id_hashtbl);
6844 nfs4_state_start_net(struct net *net)
6846 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6849 ret = nfs4_state_create_net(net);
6852 nn->boot_time = get_seconds();
6853 nn->grace_ended = false;
6854 nn->nfsd4_manager.block_opens = true;
6855 locks_start_grace(net, &nn->nfsd4_manager);
6856 nfsd4_client_tracking_init(net);
6857 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
6858 nn->nfsd4_grace, net);
6859 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
6863 /* initialization to perform when the nfsd service is started: */
6866 nfs4_state_start(void)
6870 ret = set_callback_cred();
6874 laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
6875 if (laundry_wq == NULL) {
6877 goto out_cleanup_cred;
6879 ret = nfsd4_create_callback_queue();
6881 goto out_free_laundry;
6883 set_max_delegations();
6887 destroy_workqueue(laundry_wq);
6889 cleanup_callback_cred();
6894 nfs4_state_shutdown_net(struct net *net)
6896 struct nfs4_delegation *dp = NULL;
6897 struct list_head *pos, *next, reaplist;
6898 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6900 cancel_delayed_work_sync(&nn->laundromat_work);
6901 locks_end_grace(&nn->nfsd4_manager);
6903 INIT_LIST_HEAD(&reaplist);
6904 spin_lock(&state_lock);
6905 list_for_each_safe(pos, next, &nn->del_recall_lru) {
6906 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
6907 WARN_ON(!unhash_delegation_locked(dp));
6908 list_add(&dp->dl_recall_lru, &reaplist);
6910 spin_unlock(&state_lock);
6911 list_for_each_safe(pos, next, &reaplist) {
6912 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
6913 list_del_init(&dp->dl_recall_lru);
6914 put_clnt_odstate(dp->dl_clnt_odstate);
6915 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
6916 nfs4_put_stid(&dp->dl_stid);
6919 nfsd4_client_tracking_exit(net);
6920 nfs4_state_destroy_net(net);
6924 nfs4_state_shutdown(void)
6926 destroy_workqueue(laundry_wq);
6927 nfsd4_destroy_callback_queue();
6928 cleanup_callback_cred();
6932 get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6934 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
6935 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
6939 put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6941 if (cstate->minorversion) {
6942 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
6943 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6948 clear_current_stateid(struct nfsd4_compound_state *cstate)
6950 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6954 * functions to set current state id
6957 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6959 put_stateid(cstate, &odp->od_stateid);
6963 nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
6965 put_stateid(cstate, &open->op_stateid);
6969 nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6971 put_stateid(cstate, &close->cl_stateid);
6975 nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
6977 put_stateid(cstate, &lock->lk_resp_stateid);
6981 * functions to consume current state id
6985 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6987 get_stateid(cstate, &odp->od_stateid);
6991 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
6993 get_stateid(cstate, &drp->dr_stateid);
6997 nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
6999 get_stateid(cstate, &fsp->fr_stateid);
7003 nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
7005 get_stateid(cstate, &setattr->sa_stateid);
7009 nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
7011 get_stateid(cstate, &close->cl_stateid);
7015 nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
7017 get_stateid(cstate, &locku->lu_stateid);
7021 nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
7023 get_stateid(cstate, &read->rd_stateid);
7027 nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
7029 get_stateid(cstate, &write->wr_stateid);