2 * Copyright (c) 2001 The Regents of the University of Michigan.
5 * Kendrick Smith <kmsmith@umich.edu>
6 * Andy Adamson <kandros@umich.edu>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <linux/file.h>
37 #include <linux/slab.h>
38 #include <linux/namei.h>
39 #include <linux/swap.h>
40 #include <linux/pagemap.h>
41 #include <linux/ratelimit.h>
42 #include <linux/sunrpc/svcauth_gss.h>
43 #include <linux/sunrpc/addr.h>
44 #include <linux/jhash.h>
48 #include "current_stateid.h"
53 #define NFSDDBG_FACILITY NFSDDBG_PROC
55 #define all_ones {{~0,~0},~0}
56 static const stateid_t one_stateid = {
58 .si_opaque = all_ones,
60 static const stateid_t zero_stateid = {
63 static const stateid_t currentstateid = {
67 static u64 current_sessionid = 1;
69 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
70 #define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
71 #define CURRENT_STATEID(stateid) (!memcmp((stateid), ¤tstateid, sizeof(stateid_t)))
73 /* forward declarations */
74 static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
75 static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
80 * Currently used for the del_recall_lru and file hash table. In an
81 * effort to decrease the scope of the client_mutex, this spinlock may
82 * eventually cover more:
84 static DEFINE_SPINLOCK(state_lock);
87 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
88 * the refcount on the open stateid to drop.
90 static DECLARE_WAIT_QUEUE_HEAD(close_wq);
92 static struct kmem_cache *openowner_slab;
93 static struct kmem_cache *lockowner_slab;
94 static struct kmem_cache *file_slab;
95 static struct kmem_cache *stateid_slab;
96 static struct kmem_cache *deleg_slab;
97 static struct kmem_cache *odstate_slab;
99 static void free_session(struct nfsd4_session *);
101 static struct nfsd4_callback_ops nfsd4_cb_recall_ops;
103 static bool is_session_dead(struct nfsd4_session *ses)
105 return ses->se_flags & NFS4_SESSION_DEAD;
108 static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
110 if (atomic_read(&ses->se_ref) > ref_held_by_me)
111 return nfserr_jukebox;
112 ses->se_flags |= NFS4_SESSION_DEAD;
116 static bool is_client_expired(struct nfs4_client *clp)
118 return clp->cl_time == 0;
121 static __be32 get_client_locked(struct nfs4_client *clp)
123 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
125 lockdep_assert_held(&nn->client_lock);
127 if (is_client_expired(clp))
128 return nfserr_expired;
129 atomic_inc(&clp->cl_refcount);
133 /* must be called under the client_lock */
135 renew_client_locked(struct nfs4_client *clp)
137 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
139 if (is_client_expired(clp)) {
141 printk("%s: client (clientid %08x/%08x) already expired\n",
143 clp->cl_clientid.cl_boot,
144 clp->cl_clientid.cl_id);
148 dprintk("renewing client (clientid %08x/%08x)\n",
149 clp->cl_clientid.cl_boot,
150 clp->cl_clientid.cl_id);
151 list_move_tail(&clp->cl_lru, &nn->client_lru);
152 clp->cl_time = get_seconds();
155 static void put_client_renew_locked(struct nfs4_client *clp)
157 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
159 lockdep_assert_held(&nn->client_lock);
161 if (!atomic_dec_and_test(&clp->cl_refcount))
163 if (!is_client_expired(clp))
164 renew_client_locked(clp);
167 static void put_client_renew(struct nfs4_client *clp)
169 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
171 if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
173 if (!is_client_expired(clp))
174 renew_client_locked(clp);
175 spin_unlock(&nn->client_lock);
178 static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
182 if (is_session_dead(ses))
183 return nfserr_badsession;
184 status = get_client_locked(ses->se_client);
187 atomic_inc(&ses->se_ref);
191 static void nfsd4_put_session_locked(struct nfsd4_session *ses)
193 struct nfs4_client *clp = ses->se_client;
194 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
196 lockdep_assert_held(&nn->client_lock);
198 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
200 put_client_renew_locked(clp);
203 static void nfsd4_put_session(struct nfsd4_session *ses)
205 struct nfs4_client *clp = ses->se_client;
206 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
208 spin_lock(&nn->client_lock);
209 nfsd4_put_session_locked(ses);
210 spin_unlock(&nn->client_lock);
213 static inline struct nfs4_stateowner *
214 nfs4_get_stateowner(struct nfs4_stateowner *sop)
216 atomic_inc(&sop->so_count);
221 same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
223 return (sop->so_owner.len == owner->len) &&
224 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
227 static struct nfs4_openowner *
228 find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
229 struct nfs4_client *clp)
231 struct nfs4_stateowner *so;
233 lockdep_assert_held(&clp->cl_lock);
235 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
237 if (!so->so_is_open_owner)
239 if (same_owner_str(so, &open->op_owner))
240 return openowner(nfs4_get_stateowner(so));
245 static struct nfs4_openowner *
246 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
247 struct nfs4_client *clp)
249 struct nfs4_openowner *oo;
251 spin_lock(&clp->cl_lock);
252 oo = find_openstateowner_str_locked(hashval, open, clp);
253 spin_unlock(&clp->cl_lock);
258 opaque_hashval(const void *ptr, int nbytes)
260 unsigned char *cptr = (unsigned char *) ptr;
270 static void nfsd4_free_file_rcu(struct rcu_head *rcu)
272 struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
274 kmem_cache_free(file_slab, fp);
278 put_nfs4_file(struct nfs4_file *fi)
280 might_lock(&state_lock);
282 if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) {
283 hlist_del_rcu(&fi->fi_hash);
284 spin_unlock(&state_lock);
285 WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
286 WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
287 call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
292 __nfs4_get_fd(struct nfs4_file *f, int oflag)
294 if (f->fi_fds[oflag])
295 return get_file(f->fi_fds[oflag]);
300 find_writeable_file_locked(struct nfs4_file *f)
304 lockdep_assert_held(&f->fi_lock);
306 ret = __nfs4_get_fd(f, O_WRONLY);
308 ret = __nfs4_get_fd(f, O_RDWR);
313 find_writeable_file(struct nfs4_file *f)
317 spin_lock(&f->fi_lock);
318 ret = find_writeable_file_locked(f);
319 spin_unlock(&f->fi_lock);
324 static struct file *find_readable_file_locked(struct nfs4_file *f)
328 lockdep_assert_held(&f->fi_lock);
330 ret = __nfs4_get_fd(f, O_RDONLY);
332 ret = __nfs4_get_fd(f, O_RDWR);
337 find_readable_file(struct nfs4_file *f)
341 spin_lock(&f->fi_lock);
342 ret = find_readable_file_locked(f);
343 spin_unlock(&f->fi_lock);
349 find_any_file(struct nfs4_file *f)
353 spin_lock(&f->fi_lock);
354 ret = __nfs4_get_fd(f, O_RDWR);
356 ret = __nfs4_get_fd(f, O_WRONLY);
358 ret = __nfs4_get_fd(f, O_RDONLY);
360 spin_unlock(&f->fi_lock);
364 static atomic_long_t num_delegations;
365 unsigned long max_delegations;
368 * Open owner state (share locks)
371 /* hash tables for lock and open owners */
372 #define OWNER_HASH_BITS 8
373 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
374 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
376 static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
380 ret = opaque_hashval(ownername->data, ownername->len);
381 return ret & OWNER_HASH_MASK;
384 /* hash table for nfs4_file */
385 #define FILE_HASH_BITS 8
386 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
388 static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
390 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
393 static unsigned int file_hashval(struct knfsd_fh *fh)
395 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
398 static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
401 __nfs4_file_get_access(struct nfs4_file *fp, u32 access)
403 lockdep_assert_held(&fp->fi_lock);
405 if (access & NFS4_SHARE_ACCESS_WRITE)
406 atomic_inc(&fp->fi_access[O_WRONLY]);
407 if (access & NFS4_SHARE_ACCESS_READ)
408 atomic_inc(&fp->fi_access[O_RDONLY]);
412 nfs4_file_get_access(struct nfs4_file *fp, u32 access)
414 lockdep_assert_held(&fp->fi_lock);
416 /* Does this access mode make sense? */
417 if (access & ~NFS4_SHARE_ACCESS_BOTH)
420 /* Does it conflict with a deny mode already set? */
421 if ((access & fp->fi_share_deny) != 0)
422 return nfserr_share_denied;
424 __nfs4_file_get_access(fp, access);
428 static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
430 /* Common case is that there is no deny mode. */
432 /* Does this deny mode make sense? */
433 if (deny & ~NFS4_SHARE_DENY_BOTH)
436 if ((deny & NFS4_SHARE_DENY_READ) &&
437 atomic_read(&fp->fi_access[O_RDONLY]))
438 return nfserr_share_denied;
440 if ((deny & NFS4_SHARE_DENY_WRITE) &&
441 atomic_read(&fp->fi_access[O_WRONLY]))
442 return nfserr_share_denied;
447 static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
449 might_lock(&fp->fi_lock);
451 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
452 struct file *f1 = NULL;
453 struct file *f2 = NULL;
455 swap(f1, fp->fi_fds[oflag]);
456 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
457 swap(f2, fp->fi_fds[O_RDWR]);
458 spin_unlock(&fp->fi_lock);
466 static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
468 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
470 if (access & NFS4_SHARE_ACCESS_WRITE)
471 __nfs4_file_put_access(fp, O_WRONLY);
472 if (access & NFS4_SHARE_ACCESS_READ)
473 __nfs4_file_put_access(fp, O_RDONLY);
477 * Allocate a new open/delegation state counter. This is needed for
478 * pNFS for proper return on close semantics.
480 * Note that we only allocate it for pNFS-enabled exports, otherwise
481 * all pointers to struct nfs4_clnt_odstate are always NULL.
483 static struct nfs4_clnt_odstate *
484 alloc_clnt_odstate(struct nfs4_client *clp)
486 struct nfs4_clnt_odstate *co;
488 co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
491 atomic_set(&co->co_odcount, 1);
497 hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
499 struct nfs4_file *fp = co->co_file;
501 lockdep_assert_held(&fp->fi_lock);
502 list_add(&co->co_perfile, &fp->fi_clnt_odstate);
506 get_clnt_odstate(struct nfs4_clnt_odstate *co)
509 atomic_inc(&co->co_odcount);
513 put_clnt_odstate(struct nfs4_clnt_odstate *co)
515 struct nfs4_file *fp;
521 if (atomic_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
522 list_del(&co->co_perfile);
523 spin_unlock(&fp->fi_lock);
525 nfsd4_return_all_file_layouts(co->co_client, fp);
526 kmem_cache_free(odstate_slab, co);
530 static struct nfs4_clnt_odstate *
531 find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
533 struct nfs4_clnt_odstate *co;
534 struct nfs4_client *cl;
541 spin_lock(&fp->fi_lock);
542 list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
543 if (co->co_client == cl) {
544 get_clnt_odstate(co);
550 hash_clnt_odstate_locked(new);
552 spin_unlock(&fp->fi_lock);
556 struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl,
557 struct kmem_cache *slab)
559 struct nfs4_stid *stid;
562 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
566 idr_preload(GFP_KERNEL);
567 spin_lock(&cl->cl_lock);
568 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 0, 0, GFP_NOWAIT);
569 spin_unlock(&cl->cl_lock);
573 stid->sc_client = cl;
574 stid->sc_stateid.si_opaque.so_id = new_id;
575 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
576 /* Will be incremented before return to client: */
577 atomic_set(&stid->sc_count, 1);
580 * It shouldn't be a problem to reuse an opaque stateid value.
581 * I don't think it is for 4.1. But with 4.0 I worry that, for
582 * example, a stray write retransmission could be accepted by
583 * the server when it should have been rejected. Therefore,
584 * adopt a trick from the sctp code to attempt to maximize the
585 * amount of time until an id is reused, by ensuring they always
586 * "increase" (mod INT_MAX):
590 kmem_cache_free(slab, stid);
594 static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
596 struct nfs4_stid *stid;
597 struct nfs4_ol_stateid *stp;
599 stid = nfs4_alloc_stid(clp, stateid_slab);
603 stp = openlockstateid(stid);
604 stp->st_stid.sc_free = nfs4_free_ol_stateid;
608 static void nfs4_free_deleg(struct nfs4_stid *stid)
610 kmem_cache_free(deleg_slab, stid);
611 atomic_long_dec(&num_delegations);
615 * When we recall a delegation, we should be careful not to hand it
616 * out again straight away.
617 * To ensure this we keep a pair of bloom filters ('new' and 'old')
618 * in which the filehandles of recalled delegations are "stored".
619 * If a filehandle appear in either filter, a delegation is blocked.
620 * When a delegation is recalled, the filehandle is stored in the "new"
622 * Every 30 seconds we swap the filters and clear the "new" one,
623 * unless both are empty of course.
625 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
626 * low 3 bytes as hash-table indices.
628 * 'blocked_delegations_lock', which is always taken in block_delegations(),
629 * is used to manage concurrent access. Testing does not need the lock
630 * except when swapping the two filters.
632 static DEFINE_SPINLOCK(blocked_delegations_lock);
633 static struct bloom_pair {
634 int entries, old_entries;
636 int new; /* index into 'set' */
637 DECLARE_BITMAP(set[2], 256);
638 } blocked_delegations;
640 static int delegation_blocked(struct knfsd_fh *fh)
643 struct bloom_pair *bd = &blocked_delegations;
645 if (bd->entries == 0)
647 if (seconds_since_boot() - bd->swap_time > 30) {
648 spin_lock(&blocked_delegations_lock);
649 if (seconds_since_boot() - bd->swap_time > 30) {
650 bd->entries -= bd->old_entries;
651 bd->old_entries = bd->entries;
652 memset(bd->set[bd->new], 0,
655 bd->swap_time = seconds_since_boot();
657 spin_unlock(&blocked_delegations_lock);
659 hash = jhash(&fh->fh_base, fh->fh_size, 0);
660 if (test_bit(hash&255, bd->set[0]) &&
661 test_bit((hash>>8)&255, bd->set[0]) &&
662 test_bit((hash>>16)&255, bd->set[0]))
665 if (test_bit(hash&255, bd->set[1]) &&
666 test_bit((hash>>8)&255, bd->set[1]) &&
667 test_bit((hash>>16)&255, bd->set[1]))
673 static void block_delegations(struct knfsd_fh *fh)
676 struct bloom_pair *bd = &blocked_delegations;
678 hash = jhash(&fh->fh_base, fh->fh_size, 0);
680 spin_lock(&blocked_delegations_lock);
681 __set_bit(hash&255, bd->set[bd->new]);
682 __set_bit((hash>>8)&255, bd->set[bd->new]);
683 __set_bit((hash>>16)&255, bd->set[bd->new]);
684 if (bd->entries == 0)
685 bd->swap_time = seconds_since_boot();
687 spin_unlock(&blocked_delegations_lock);
690 static struct nfs4_delegation *
691 alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh,
692 struct nfs4_clnt_odstate *odstate)
694 struct nfs4_delegation *dp;
697 dprintk("NFSD alloc_init_deleg\n");
698 n = atomic_long_inc_return(&num_delegations);
699 if (n < 0 || n > max_delegations)
701 if (delegation_blocked(¤t_fh->fh_handle))
703 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
707 dp->dl_stid.sc_free = nfs4_free_deleg;
709 * delegation seqid's are never incremented. The 4.1 special
710 * meaning of seqid 0 isn't meaningful, really, but let's avoid
711 * 0 anyway just for consistency and use 1:
713 dp->dl_stid.sc_stateid.si_generation = 1;
714 INIT_LIST_HEAD(&dp->dl_perfile);
715 INIT_LIST_HEAD(&dp->dl_perclnt);
716 INIT_LIST_HEAD(&dp->dl_recall_lru);
717 dp->dl_clnt_odstate = odstate;
718 get_clnt_odstate(odstate);
719 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
721 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
722 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
725 atomic_long_dec(&num_delegations);
730 nfs4_put_stid(struct nfs4_stid *s)
732 struct nfs4_file *fp = s->sc_file;
733 struct nfs4_client *clp = s->sc_client;
735 might_lock(&clp->cl_lock);
737 if (!atomic_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
738 wake_up_all(&close_wq);
741 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
742 spin_unlock(&clp->cl_lock);
748 static void nfs4_put_deleg_lease(struct nfs4_file *fp)
750 struct file *filp = NULL;
752 spin_lock(&fp->fi_lock);
753 if (fp->fi_deleg_file && --fp->fi_delegees == 0)
754 swap(filp, fp->fi_deleg_file);
755 spin_unlock(&fp->fi_lock);
758 vfs_setlease(filp, F_UNLCK, NULL, (void **)&fp);
763 void nfs4_unhash_stid(struct nfs4_stid *s)
769 hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
771 lockdep_assert_held(&state_lock);
772 lockdep_assert_held(&fp->fi_lock);
774 atomic_inc(&dp->dl_stid.sc_count);
775 dp->dl_stid.sc_type = NFS4_DELEG_STID;
776 list_add(&dp->dl_perfile, &fp->fi_delegations);
777 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
781 unhash_delegation_locked(struct nfs4_delegation *dp)
783 struct nfs4_file *fp = dp->dl_stid.sc_file;
785 lockdep_assert_held(&state_lock);
787 if (list_empty(&dp->dl_perfile))
790 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
791 /* Ensure that deleg break won't try to requeue it */
793 spin_lock(&fp->fi_lock);
794 list_del_init(&dp->dl_perclnt);
795 list_del_init(&dp->dl_recall_lru);
796 list_del_init(&dp->dl_perfile);
797 spin_unlock(&fp->fi_lock);
801 static void destroy_delegation(struct nfs4_delegation *dp)
805 spin_lock(&state_lock);
806 unhashed = unhash_delegation_locked(dp);
807 spin_unlock(&state_lock);
809 put_clnt_odstate(dp->dl_clnt_odstate);
810 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
811 nfs4_put_stid(&dp->dl_stid);
815 static void revoke_delegation(struct nfs4_delegation *dp)
817 struct nfs4_client *clp = dp->dl_stid.sc_client;
819 WARN_ON(!list_empty(&dp->dl_recall_lru));
821 put_clnt_odstate(dp->dl_clnt_odstate);
822 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
824 if (clp->cl_minorversion == 0)
825 nfs4_put_stid(&dp->dl_stid);
827 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
828 spin_lock(&clp->cl_lock);
829 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
830 spin_unlock(&clp->cl_lock);
838 static unsigned int clientid_hashval(u32 id)
840 return id & CLIENT_HASH_MASK;
843 static unsigned int clientstr_hashval(const char *name)
845 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
849 * We store the NONE, READ, WRITE, and BOTH bits separately in the
850 * st_{access,deny}_bmap field of the stateid, in order to track not
851 * only what share bits are currently in force, but also what
852 * combinations of share bits previous opens have used. This allows us
853 * to enforce the recommendation of rfc 3530 14.2.19 that the server
854 * return an error if the client attempt to downgrade to a combination
855 * of share bits not explicable by closing some of its previous opens.
857 * XXX: This enforcement is actually incomplete, since we don't keep
858 * track of access/deny bit combinations; so, e.g., we allow:
860 * OPEN allow read, deny write
861 * OPEN allow both, deny none
862 * DOWNGRADE allow read, deny none
864 * which we should reject.
867 bmap_to_share_mode(unsigned long bmap) {
869 unsigned int access = 0;
871 for (i = 1; i < 4; i++) {
872 if (test_bit(i, &bmap))
878 /* set share access for a given stateid */
880 set_access(u32 access, struct nfs4_ol_stateid *stp)
882 unsigned char mask = 1 << access;
884 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
885 stp->st_access_bmap |= mask;
888 /* clear share access for a given stateid */
890 clear_access(u32 access, struct nfs4_ol_stateid *stp)
892 unsigned char mask = 1 << access;
894 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
895 stp->st_access_bmap &= ~mask;
898 /* test whether a given stateid has access */
900 test_access(u32 access, struct nfs4_ol_stateid *stp)
902 unsigned char mask = 1 << access;
904 return (bool)(stp->st_access_bmap & mask);
907 /* set share deny for a given stateid */
909 set_deny(u32 deny, struct nfs4_ol_stateid *stp)
911 unsigned char mask = 1 << deny;
913 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
914 stp->st_deny_bmap |= mask;
917 /* clear share deny for a given stateid */
919 clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
921 unsigned char mask = 1 << deny;
923 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
924 stp->st_deny_bmap &= ~mask;
927 /* test whether a given stateid is denying specific access */
929 test_deny(u32 deny, struct nfs4_ol_stateid *stp)
931 unsigned char mask = 1 << deny;
933 return (bool)(stp->st_deny_bmap & mask);
936 static int nfs4_access_to_omode(u32 access)
938 switch (access & NFS4_SHARE_ACCESS_BOTH) {
939 case NFS4_SHARE_ACCESS_READ:
941 case NFS4_SHARE_ACCESS_WRITE:
943 case NFS4_SHARE_ACCESS_BOTH:
951 * A stateid that had a deny mode associated with it is being released
952 * or downgraded. Recalculate the deny mode on the file.
955 recalculate_deny_mode(struct nfs4_file *fp)
957 struct nfs4_ol_stateid *stp;
959 spin_lock(&fp->fi_lock);
960 fp->fi_share_deny = 0;
961 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
962 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
963 spin_unlock(&fp->fi_lock);
967 reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
972 for (i = 1; i < 4; i++) {
973 if ((i & deny) != i) {
979 /* Recalculate per-file deny mode if there was a change */
981 recalculate_deny_mode(stp->st_stid.sc_file);
984 /* release all access and file references for a given stateid */
986 release_all_access(struct nfs4_ol_stateid *stp)
989 struct nfs4_file *fp = stp->st_stid.sc_file;
991 if (fp && stp->st_deny_bmap != 0)
992 recalculate_deny_mode(fp);
994 for (i = 1; i < 4; i++) {
995 if (test_access(i, stp))
996 nfs4_file_put_access(stp->st_stid.sc_file, i);
997 clear_access(i, stp);
1001 static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1003 struct nfs4_client *clp = sop->so_client;
1005 might_lock(&clp->cl_lock);
1007 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
1009 sop->so_ops->so_unhash(sop);
1010 spin_unlock(&clp->cl_lock);
1011 kfree(sop->so_owner.data);
1012 sop->so_ops->so_free(sop);
1015 static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
1017 struct nfs4_file *fp = stp->st_stid.sc_file;
1019 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1021 if (list_empty(&stp->st_perfile))
1024 spin_lock(&fp->fi_lock);
1025 list_del_init(&stp->st_perfile);
1026 spin_unlock(&fp->fi_lock);
1027 list_del(&stp->st_perstateowner);
1031 static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
1033 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1035 put_clnt_odstate(stp->st_clnt_odstate);
1036 release_all_access(stp);
1037 if (stp->st_stateowner)
1038 nfs4_put_stateowner(stp->st_stateowner);
1039 kmem_cache_free(stateid_slab, stid);
1042 static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
1044 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1045 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
1048 file = find_any_file(stp->st_stid.sc_file);
1050 filp_close(file, (fl_owner_t)lo);
1051 nfs4_free_ol_stateid(stid);
1055 * Put the persistent reference to an already unhashed generic stateid, while
1056 * holding the cl_lock. If it's the last reference, then put it onto the
1057 * reaplist for later destruction.
1059 static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1060 struct list_head *reaplist)
1062 struct nfs4_stid *s = &stp->st_stid;
1063 struct nfs4_client *clp = s->sc_client;
1065 lockdep_assert_held(&clp->cl_lock);
1067 WARN_ON_ONCE(!list_empty(&stp->st_locks));
1069 if (!atomic_dec_and_test(&s->sc_count)) {
1070 wake_up_all(&close_wq);
1074 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1075 list_add(&stp->st_locks, reaplist);
1078 static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
1080 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
1082 lockdep_assert_held(&oo->oo_owner.so_client->cl_lock);
1084 list_del_init(&stp->st_locks);
1085 nfs4_unhash_stid(&stp->st_stid);
1086 return unhash_ol_stateid(stp);
1089 static void release_lock_stateid(struct nfs4_ol_stateid *stp)
1091 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
1094 spin_lock(&oo->oo_owner.so_client->cl_lock);
1095 unhashed = unhash_lock_stateid(stp);
1096 spin_unlock(&oo->oo_owner.so_client->cl_lock);
1098 nfs4_put_stid(&stp->st_stid);
1101 static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
1103 struct nfs4_client *clp = lo->lo_owner.so_client;
1105 lockdep_assert_held(&clp->cl_lock);
1107 list_del_init(&lo->lo_owner.so_strhash);
1111 * Free a list of generic stateids that were collected earlier after being
1115 free_ol_stateid_reaplist(struct list_head *reaplist)
1117 struct nfs4_ol_stateid *stp;
1118 struct nfs4_file *fp;
1122 while (!list_empty(reaplist)) {
1123 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1125 list_del(&stp->st_locks);
1126 fp = stp->st_stid.sc_file;
1127 stp->st_stid.sc_free(&stp->st_stid);
1133 static void release_lockowner(struct nfs4_lockowner *lo)
1135 struct nfs4_client *clp = lo->lo_owner.so_client;
1136 struct nfs4_ol_stateid *stp;
1137 struct list_head reaplist;
1139 INIT_LIST_HEAD(&reaplist);
1141 spin_lock(&clp->cl_lock);
1142 unhash_lockowner_locked(lo);
1143 while (!list_empty(&lo->lo_owner.so_stateids)) {
1144 stp = list_first_entry(&lo->lo_owner.so_stateids,
1145 struct nfs4_ol_stateid, st_perstateowner);
1146 WARN_ON(!unhash_lock_stateid(stp));
1147 put_ol_stateid_locked(stp, &reaplist);
1149 spin_unlock(&clp->cl_lock);
1150 free_ol_stateid_reaplist(&reaplist);
1151 nfs4_put_stateowner(&lo->lo_owner);
1154 static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1155 struct list_head *reaplist)
1157 struct nfs4_ol_stateid *stp;
1159 lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1161 while (!list_empty(&open_stp->st_locks)) {
1162 stp = list_entry(open_stp->st_locks.next,
1163 struct nfs4_ol_stateid, st_locks);
1164 WARN_ON(!unhash_lock_stateid(stp));
1165 put_ol_stateid_locked(stp, reaplist);
1169 static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
1170 struct list_head *reaplist)
1174 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1176 unhashed = unhash_ol_stateid(stp);
1177 release_open_stateid_locks(stp, reaplist);
1181 static void release_open_stateid(struct nfs4_ol_stateid *stp)
1183 LIST_HEAD(reaplist);
1185 spin_lock(&stp->st_stid.sc_client->cl_lock);
1186 if (unhash_open_stateid(stp, &reaplist))
1187 put_ol_stateid_locked(stp, &reaplist);
1188 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1189 free_ol_stateid_reaplist(&reaplist);
1192 static void unhash_openowner_locked(struct nfs4_openowner *oo)
1194 struct nfs4_client *clp = oo->oo_owner.so_client;
1196 lockdep_assert_held(&clp->cl_lock);
1198 list_del_init(&oo->oo_owner.so_strhash);
1199 list_del_init(&oo->oo_perclient);
1202 static void release_last_closed_stateid(struct nfs4_openowner *oo)
1204 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1206 struct nfs4_ol_stateid *s;
1208 spin_lock(&nn->client_lock);
1209 s = oo->oo_last_closed_stid;
1211 list_del_init(&oo->oo_close_lru);
1212 oo->oo_last_closed_stid = NULL;
1214 spin_unlock(&nn->client_lock);
1216 nfs4_put_stid(&s->st_stid);
1219 static void release_openowner(struct nfs4_openowner *oo)
1221 struct nfs4_ol_stateid *stp;
1222 struct nfs4_client *clp = oo->oo_owner.so_client;
1223 struct list_head reaplist;
1225 INIT_LIST_HEAD(&reaplist);
1227 spin_lock(&clp->cl_lock);
1228 unhash_openowner_locked(oo);
1229 while (!list_empty(&oo->oo_owner.so_stateids)) {
1230 stp = list_first_entry(&oo->oo_owner.so_stateids,
1231 struct nfs4_ol_stateid, st_perstateowner);
1232 if (unhash_open_stateid(stp, &reaplist))
1233 put_ol_stateid_locked(stp, &reaplist);
1235 spin_unlock(&clp->cl_lock);
1236 free_ol_stateid_reaplist(&reaplist);
1237 release_last_closed_stateid(oo);
1238 nfs4_put_stateowner(&oo->oo_owner);
1242 hash_sessionid(struct nfs4_sessionid *sessionid)
1244 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1246 return sid->sequence % SESSION_HASH_SIZE;
1249 #ifdef CONFIG_SUNRPC_DEBUG
1251 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1253 u32 *ptr = (u32 *)(&sessionid->data[0]);
1254 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1258 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1264 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1265 * won't be used for replay.
1267 void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1269 struct nfs4_stateowner *so = cstate->replay_owner;
1271 if (nfserr == nfserr_replay_me)
1274 if (!seqid_mutating_err(ntohl(nfserr))) {
1275 nfsd4_cstate_clear_replay(cstate);
1280 if (so->so_is_open_owner)
1281 release_last_closed_stateid(openowner(so));
1287 gen_sessionid(struct nfsd4_session *ses)
1289 struct nfs4_client *clp = ses->se_client;
1290 struct nfsd4_sessionid *sid;
1292 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1293 sid->clientid = clp->cl_clientid;
1294 sid->sequence = current_sessionid++;
1299 * The protocol defines ca_maxresponssize_cached to include the size of
1300 * the rpc header, but all we need to cache is the data starting after
1301 * the end of the initial SEQUENCE operation--the rest we regenerate
1302 * each time. Therefore we can advertise a ca_maxresponssize_cached
1303 * value that is the number of bytes in our cache plus a few additional
1304 * bytes. In order to stay on the safe side, and not promise more than
1305 * we can cache, those additional bytes must be the minimum possible: 24
1306 * bytes of rpc header (xid through accept state, with AUTH_NULL
1307 * verifier), 12 for the compound header (with zero-length tag), and 44
1308 * for the SEQUENCE op response:
1310 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1313 free_session_slots(struct nfsd4_session *ses)
1317 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1318 kfree(ses->se_slots[i]);
1322 * We don't actually need to cache the rpc and session headers, so we
1323 * can allocate a little less for each slot:
1325 static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
1329 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1332 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1333 return size + sizeof(struct nfsd4_slot);
1337 * XXX: If we run out of reserved DRC memory we could (up to a point)
1338 * re-negotiate active sessions and reduce their slot usage to make
1339 * room for new connections. For now we just fail the create session.
1341 static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
1343 u32 slotsize = slot_bytes(ca);
1344 u32 num = ca->maxreqs;
1347 spin_lock(&nfsd_drc_lock);
1348 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1349 nfsd_drc_max_mem - nfsd_drc_mem_used);
1350 num = min_t(int, num, avail / slotsize);
1351 nfsd_drc_mem_used += num * slotsize;
1352 spin_unlock(&nfsd_drc_lock);
1357 static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
1359 int slotsize = slot_bytes(ca);
1361 spin_lock(&nfsd_drc_lock);
1362 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
1363 spin_unlock(&nfsd_drc_lock);
1366 static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1367 struct nfsd4_channel_attrs *battrs)
1369 int numslots = fattrs->maxreqs;
1370 int slotsize = slot_bytes(fattrs);
1371 struct nfsd4_session *new;
1374 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
1375 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1376 mem = numslots * sizeof(struct nfsd4_slot *);
1378 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
1381 /* allocate each struct nfsd4_slot and data cache in one piece */
1382 for (i = 0; i < numslots; i++) {
1383 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
1384 if (!new->se_slots[i])
1388 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1389 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1394 kfree(new->se_slots[i]);
1399 static void free_conn(struct nfsd4_conn *c)
1401 svc_xprt_put(c->cn_xprt);
1405 static void nfsd4_conn_lost(struct svc_xpt_user *u)
1407 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1408 struct nfs4_client *clp = c->cn_session->se_client;
1410 spin_lock(&clp->cl_lock);
1411 if (!list_empty(&c->cn_persession)) {
1412 list_del(&c->cn_persession);
1415 nfsd4_probe_callback(clp);
1416 spin_unlock(&clp->cl_lock);
1419 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
1421 struct nfsd4_conn *conn;
1423 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1426 svc_xprt_get(rqstp->rq_xprt);
1427 conn->cn_xprt = rqstp->rq_xprt;
1428 conn->cn_flags = flags;
1429 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1433 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1435 conn->cn_session = ses;
1436 list_add(&conn->cn_persession, &ses->se_conns);
1439 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1441 struct nfs4_client *clp = ses->se_client;
1443 spin_lock(&clp->cl_lock);
1444 __nfsd4_hash_conn(conn, ses);
1445 spin_unlock(&clp->cl_lock);
1448 static int nfsd4_register_conn(struct nfsd4_conn *conn)
1450 conn->cn_xpt_user.callback = nfsd4_conn_lost;
1451 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
1454 static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
1458 nfsd4_hash_conn(conn, ses);
1459 ret = nfsd4_register_conn(conn);
1461 /* oops; xprt is already down: */
1462 nfsd4_conn_lost(&conn->cn_xpt_user);
1463 /* We may have gained or lost a callback channel: */
1464 nfsd4_probe_callback_sync(ses->se_client);
1467 static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
1469 u32 dir = NFS4_CDFC4_FORE;
1471 if (cses->flags & SESSION4_BACK_CHAN)
1472 dir |= NFS4_CDFC4_BACK;
1473 return alloc_conn(rqstp, dir);
1476 /* must be called under client_lock */
1477 static void nfsd4_del_conns(struct nfsd4_session *s)
1479 struct nfs4_client *clp = s->se_client;
1480 struct nfsd4_conn *c;
1482 spin_lock(&clp->cl_lock);
1483 while (!list_empty(&s->se_conns)) {
1484 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1485 list_del_init(&c->cn_persession);
1486 spin_unlock(&clp->cl_lock);
1488 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1491 spin_lock(&clp->cl_lock);
1493 spin_unlock(&clp->cl_lock);
1496 static void __free_session(struct nfsd4_session *ses)
1498 free_session_slots(ses);
1502 static void free_session(struct nfsd4_session *ses)
1504 nfsd4_del_conns(ses);
1505 nfsd4_put_drc_mem(&ses->se_fchannel);
1506 __free_session(ses);
1509 static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
1512 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1514 new->se_client = clp;
1517 INIT_LIST_HEAD(&new->se_conns);
1519 new->se_cb_seq_nr = 1;
1520 new->se_flags = cses->flags;
1521 new->se_cb_prog = cses->callback_prog;
1522 new->se_cb_sec = cses->cb_sec;
1523 atomic_set(&new->se_ref, 0);
1524 idx = hash_sessionid(&new->se_sessionid);
1525 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
1526 spin_lock(&clp->cl_lock);
1527 list_add(&new->se_perclnt, &clp->cl_sessions);
1528 spin_unlock(&clp->cl_lock);
1531 struct sockaddr *sa = svc_addr(rqstp);
1533 * This is a little silly; with sessions there's no real
1534 * use for the callback address. Use the peer address
1535 * as a reasonable default for now, but consider fixing
1536 * the rpc client not to require an address in the
1539 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1540 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
1544 /* caller must hold client_lock */
1545 static struct nfsd4_session *
1546 __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
1548 struct nfsd4_session *elem;
1550 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1552 lockdep_assert_held(&nn->client_lock);
1554 dump_sessionid(__func__, sessionid);
1555 idx = hash_sessionid(sessionid);
1556 /* Search in the appropriate list */
1557 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
1558 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1559 NFS4_MAX_SESSIONID_LEN)) {
1564 dprintk("%s: session not found\n", __func__);
1568 static struct nfsd4_session *
1569 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1572 struct nfsd4_session *session;
1573 __be32 status = nfserr_badsession;
1575 session = __find_in_sessionid_hashtbl(sessionid, net);
1578 status = nfsd4_get_session_locked(session);
1586 /* caller must hold client_lock */
1588 unhash_session(struct nfsd4_session *ses)
1590 struct nfs4_client *clp = ses->se_client;
1591 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1593 lockdep_assert_held(&nn->client_lock);
1595 list_del(&ses->se_hash);
1596 spin_lock(&ses->se_client->cl_lock);
1597 list_del(&ses->se_perclnt);
1598 spin_unlock(&ses->se_client->cl_lock);
1601 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1603 STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
1606 * We're assuming the clid was not given out from a boot
1607 * precisely 2^32 (about 136 years) before this one. That seems
1608 * a safe assumption:
1610 if (clid->cl_boot == (u32)nn->boot_time)
1612 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
1613 clid->cl_boot, clid->cl_id, nn->boot_time);
1618 * XXX Should we use a slab cache ?
1619 * This type of memory management is somewhat inefficient, but we use it
1620 * anyway since SETCLIENTID is not a common operation.
1622 static struct nfs4_client *alloc_client(struct xdr_netobj name)
1624 struct nfs4_client *clp;
1627 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1630 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
1631 if (clp->cl_name.data == NULL)
1633 clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
1634 OWNER_HASH_SIZE, GFP_KERNEL);
1635 if (!clp->cl_ownerstr_hashtbl)
1636 goto err_no_hashtbl;
1637 for (i = 0; i < OWNER_HASH_SIZE; i++)
1638 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
1639 clp->cl_name.len = name.len;
1640 INIT_LIST_HEAD(&clp->cl_sessions);
1641 idr_init(&clp->cl_stateids);
1642 atomic_set(&clp->cl_refcount, 0);
1643 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1644 INIT_LIST_HEAD(&clp->cl_idhash);
1645 INIT_LIST_HEAD(&clp->cl_openowners);
1646 INIT_LIST_HEAD(&clp->cl_delegations);
1647 INIT_LIST_HEAD(&clp->cl_lru);
1648 INIT_LIST_HEAD(&clp->cl_revoked);
1649 #ifdef CONFIG_NFSD_PNFS
1650 INIT_LIST_HEAD(&clp->cl_lo_states);
1652 spin_lock_init(&clp->cl_lock);
1653 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1656 kfree(clp->cl_name.data);
1663 free_client(struct nfs4_client *clp)
1665 while (!list_empty(&clp->cl_sessions)) {
1666 struct nfsd4_session *ses;
1667 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1669 list_del(&ses->se_perclnt);
1670 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1673 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
1674 free_svc_cred(&clp->cl_cred);
1675 kfree(clp->cl_ownerstr_hashtbl);
1676 kfree(clp->cl_name.data);
1677 idr_destroy(&clp->cl_stateids);
1681 /* must be called under the client_lock */
1683 unhash_client_locked(struct nfs4_client *clp)
1685 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1686 struct nfsd4_session *ses;
1688 lockdep_assert_held(&nn->client_lock);
1690 /* Mark the client as expired! */
1692 /* Make it invisible */
1693 if (!list_empty(&clp->cl_idhash)) {
1694 list_del_init(&clp->cl_idhash);
1695 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1696 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1698 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1700 list_del_init(&clp->cl_lru);
1701 spin_lock(&clp->cl_lock);
1702 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1703 list_del_init(&ses->se_hash);
1704 spin_unlock(&clp->cl_lock);
1708 unhash_client(struct nfs4_client *clp)
1710 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1712 spin_lock(&nn->client_lock);
1713 unhash_client_locked(clp);
1714 spin_unlock(&nn->client_lock);
1717 static __be32 mark_client_expired_locked(struct nfs4_client *clp)
1719 if (atomic_read(&clp->cl_refcount))
1720 return nfserr_jukebox;
1721 unhash_client_locked(clp);
1726 __destroy_client(struct nfs4_client *clp)
1728 struct nfs4_openowner *oo;
1729 struct nfs4_delegation *dp;
1730 struct list_head reaplist;
1732 INIT_LIST_HEAD(&reaplist);
1733 spin_lock(&state_lock);
1734 while (!list_empty(&clp->cl_delegations)) {
1735 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
1736 WARN_ON(!unhash_delegation_locked(dp));
1737 list_add(&dp->dl_recall_lru, &reaplist);
1739 spin_unlock(&state_lock);
1740 while (!list_empty(&reaplist)) {
1741 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
1742 list_del_init(&dp->dl_recall_lru);
1743 put_clnt_odstate(dp->dl_clnt_odstate);
1744 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
1745 nfs4_put_stid(&dp->dl_stid);
1747 while (!list_empty(&clp->cl_revoked)) {
1748 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
1749 list_del_init(&dp->dl_recall_lru);
1750 nfs4_put_stid(&dp->dl_stid);
1752 while (!list_empty(&clp->cl_openowners)) {
1753 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1754 nfs4_get_stateowner(&oo->oo_owner);
1755 release_openowner(oo);
1757 nfsd4_return_all_client_layouts(clp);
1758 nfsd4_shutdown_callback(clp);
1759 if (clp->cl_cb_conn.cb_xprt)
1760 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
1765 destroy_client(struct nfs4_client *clp)
1768 __destroy_client(clp);
1771 static void expire_client(struct nfs4_client *clp)
1774 nfsd4_client_record_remove(clp);
1775 __destroy_client(clp);
1778 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1780 memcpy(target->cl_verifier.data, source->data,
1781 sizeof(target->cl_verifier.data));
1784 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1786 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1787 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1790 static int copy_cred(struct svc_cred *target, struct svc_cred *source)
1792 if (source->cr_principal) {
1793 target->cr_principal =
1794 kstrdup(source->cr_principal, GFP_KERNEL);
1795 if (target->cr_principal == NULL)
1798 target->cr_principal = NULL;
1799 target->cr_flavor = source->cr_flavor;
1800 target->cr_uid = source->cr_uid;
1801 target->cr_gid = source->cr_gid;
1802 target->cr_group_info = source->cr_group_info;
1803 get_group_info(target->cr_group_info);
1804 target->cr_gss_mech = source->cr_gss_mech;
1805 if (source->cr_gss_mech)
1806 gss_mech_get(source->cr_gss_mech);
1811 compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1813 if (o1->len < o2->len)
1815 if (o1->len > o2->len)
1817 return memcmp(o1->data, o2->data, o1->len);
1820 static int same_name(const char *n1, const char *n2)
1822 return 0 == memcmp(n1, n2, HEXDIR_LEN);
1826 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1828 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
1832 same_clid(clientid_t *cl1, clientid_t *cl2)
1834 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
1837 static bool groups_equal(struct group_info *g1, struct group_info *g2)
1841 if (g1->ngroups != g2->ngroups)
1843 for (i=0; i<g1->ngroups; i++)
1844 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
1850 * RFC 3530 language requires clid_inuse be returned when the
1851 * "principal" associated with a requests differs from that previously
1852 * used. We use uid, gid's, and gss principal string as our best
1853 * approximation. We also don't want to allow non-gss use of a client
1854 * established using gss: in theory cr_principal should catch that
1855 * change, but in practice cr_principal can be null even in the gss case
1856 * since gssd doesn't always pass down a principal string.
1858 static bool is_gss_cred(struct svc_cred *cr)
1860 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1861 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1866 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1868 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
1869 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1870 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
1871 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1873 if (cr1->cr_principal == cr2->cr_principal)
1875 if (!cr1->cr_principal || !cr2->cr_principal)
1877 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
1880 static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1882 struct svc_cred *cr = &rqstp->rq_cred;
1885 if (!cr->cr_gss_mech)
1887 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1888 return service == RPC_GSS_SVC_INTEGRITY ||
1889 service == RPC_GSS_SVC_PRIVACY;
1892 static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1894 struct svc_cred *cr = &rqstp->rq_cred;
1896 if (!cl->cl_mach_cred)
1898 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1900 if (!svc_rqst_integrity_protected(rqstp))
1902 if (!cr->cr_principal)
1904 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1907 static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
1912 * This is opaque to client, so no need to byte-swap. Use
1913 * __force to keep sparse happy
1915 verf[0] = (__force __be32)get_seconds();
1916 verf[1] = (__force __be32)nn->clientid_counter;
1917 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
1920 static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
1922 clp->cl_clientid.cl_boot = nn->boot_time;
1923 clp->cl_clientid.cl_id = nn->clientid_counter++;
1924 gen_confirm(clp, nn);
1927 static struct nfs4_stid *
1928 find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
1930 struct nfs4_stid *ret;
1932 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1933 if (!ret || !ret->sc_type)
1938 static struct nfs4_stid *
1939 find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
1941 struct nfs4_stid *s;
1943 spin_lock(&cl->cl_lock);
1944 s = find_stateid_locked(cl, t);
1946 if (typemask & s->sc_type)
1947 atomic_inc(&s->sc_count);
1951 spin_unlock(&cl->cl_lock);
1955 static struct nfs4_client *create_client(struct xdr_netobj name,
1956 struct svc_rqst *rqstp, nfs4_verifier *verf)
1958 struct nfs4_client *clp;
1959 struct sockaddr *sa = svc_addr(rqstp);
1961 struct net *net = SVC_NET(rqstp);
1963 clp = alloc_client(name);
1967 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1972 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
1973 clp->cl_time = get_seconds();
1974 clear_bit(0, &clp->cl_cb_slot_busy);
1975 copy_verf(clp, verf);
1976 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
1977 clp->cl_cb_session = NULL;
1983 add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1985 struct rb_node **new = &(root->rb_node), *parent = NULL;
1986 struct nfs4_client *clp;
1989 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1992 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1993 new = &((*new)->rb_left);
1995 new = &((*new)->rb_right);
1998 rb_link_node(&new_clp->cl_namenode, parent, new);
1999 rb_insert_color(&new_clp->cl_namenode, root);
2002 static struct nfs4_client *
2003 find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2006 struct rb_node *node = root->rb_node;
2007 struct nfs4_client *clp;
2010 clp = rb_entry(node, struct nfs4_client, cl_namenode);
2011 cmp = compare_blob(&clp->cl_name, name);
2013 node = node->rb_left;
2015 node = node->rb_right;
2023 add_to_unconfirmed(struct nfs4_client *clp)
2025 unsigned int idhashval;
2026 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2028 lockdep_assert_held(&nn->client_lock);
2030 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2031 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
2032 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
2033 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
2034 renew_client_locked(clp);
2038 move_to_confirmed(struct nfs4_client *clp)
2040 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
2041 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2043 lockdep_assert_held(&nn->client_lock);
2045 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
2046 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
2047 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
2048 add_clp_to_name_tree(clp, &nn->conf_name_tree);
2049 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2050 renew_client_locked(clp);
2053 static struct nfs4_client *
2054 find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
2056 struct nfs4_client *clp;
2057 unsigned int idhashval = clientid_hashval(clid->cl_id);
2059 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
2060 if (same_clid(&clp->cl_clientid, clid)) {
2061 if ((bool)clp->cl_minorversion != sessions)
2063 renew_client_locked(clp);
2070 static struct nfs4_client *
2071 find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2073 struct list_head *tbl = nn->conf_id_hashtbl;
2075 lockdep_assert_held(&nn->client_lock);
2076 return find_client_in_id_table(tbl, clid, sessions);
2079 static struct nfs4_client *
2080 find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2082 struct list_head *tbl = nn->unconf_id_hashtbl;
2084 lockdep_assert_held(&nn->client_lock);
2085 return find_client_in_id_table(tbl, clid, sessions);
2088 static bool clp_used_exchangeid(struct nfs4_client *clp)
2090 return clp->cl_exchange_flags != 0;
2093 static struct nfs4_client *
2094 find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
2096 lockdep_assert_held(&nn->client_lock);
2097 return find_clp_in_name_tree(name, &nn->conf_name_tree);
2100 static struct nfs4_client *
2101 find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
2103 lockdep_assert_held(&nn->client_lock);
2104 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
2108 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
2110 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
2111 struct sockaddr *sa = svc_addr(rqstp);
2112 u32 scopeid = rpc_get_scope_id(sa);
2113 unsigned short expected_family;
2115 /* Currently, we only support tcp and tcp6 for the callback channel */
2116 if (se->se_callback_netid_len == 3 &&
2117 !memcmp(se->se_callback_netid_val, "tcp", 3))
2118 expected_family = AF_INET;
2119 else if (se->se_callback_netid_len == 4 &&
2120 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2121 expected_family = AF_INET6;
2125 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
2126 se->se_callback_addr_len,
2127 (struct sockaddr *)&conn->cb_addr,
2128 sizeof(conn->cb_addr));
2130 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
2133 if (conn->cb_addr.ss_family == AF_INET6)
2134 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
2136 conn->cb_prog = se->se_callback_prog;
2137 conn->cb_ident = se->se_callback_ident;
2138 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
2141 conn->cb_addr.ss_family = AF_UNSPEC;
2142 conn->cb_addrlen = 0;
2143 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
2144 "will not receive delegations\n",
2145 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2151 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
2154 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2156 struct xdr_buf *buf = resp->xdr.buf;
2157 struct nfsd4_slot *slot = resp->cstate.slot;
2160 dprintk("--> %s slot %p\n", __func__, slot);
2162 slot->sl_opcnt = resp->opcnt;
2163 slot->sl_status = resp->cstate.status;
2165 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
2166 if (nfsd4_not_cached(resp)) {
2167 slot->sl_datalen = 0;
2170 base = resp->cstate.data_offset;
2171 slot->sl_datalen = buf->len - base;
2172 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
2173 WARN("%s: sessions DRC could not cache compound\n", __func__);
2178 * Encode the replay sequence operation from the slot values.
2179 * If cachethis is FALSE encode the uncached rep error on the next
2180 * operation which sets resp->p and increments resp->opcnt for
2181 * nfs4svc_encode_compoundres.
2185 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2186 struct nfsd4_compoundres *resp)
2188 struct nfsd4_op *op;
2189 struct nfsd4_slot *slot = resp->cstate.slot;
2191 /* Encode the replayed sequence operation */
2192 op = &args->ops[resp->opcnt - 1];
2193 nfsd4_encode_operation(resp, op);
2195 /* Return nfserr_retry_uncached_rep in next operation. */
2196 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
2197 op = &args->ops[resp->opcnt++];
2198 op->status = nfserr_retry_uncached_rep;
2199 nfsd4_encode_operation(resp, op);
2205 * The sequence operation is not cached because we can use the slot and
2209 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2210 struct nfsd4_sequence *seq)
2212 struct nfsd4_slot *slot = resp->cstate.slot;
2213 struct xdr_stream *xdr = &resp->xdr;
2217 dprintk("--> %s slot %p\n", __func__, slot);
2219 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
2223 p = xdr_reserve_space(xdr, slot->sl_datalen);
2226 return nfserr_serverfault;
2228 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2229 xdr_commit_encode(xdr);
2231 resp->opcnt = slot->sl_opcnt;
2232 return slot->sl_status;
2236 * Set the exchange_id flags returned by the server.
2239 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2241 #ifdef CONFIG_NFSD_PNFS
2242 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
2244 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
2247 /* Referrals are supported, Migration is not. */
2248 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2250 /* set the wire flags to return to client. */
2251 clid->flags = new->cl_exchange_flags;
2254 static bool client_has_state(struct nfs4_client *clp)
2257 * Note clp->cl_openowners check isn't quite right: there's no
2258 * need to count owners without stateid's.
2260 * Also note we should probably be using this in 4.0 case too.
2262 return !list_empty(&clp->cl_openowners)
2263 || !list_empty(&clp->cl_delegations)
2264 || !list_empty(&clp->cl_sessions);
2268 nfsd4_exchange_id(struct svc_rqst *rqstp,
2269 struct nfsd4_compound_state *cstate,
2270 struct nfsd4_exchange_id *exid)
2272 struct nfs4_client *conf, *new;
2273 struct nfs4_client *unconf = NULL;
2275 char addr_str[INET6_ADDRSTRLEN];
2276 nfs4_verifier verf = exid->verifier;
2277 struct sockaddr *sa = svc_addr(rqstp);
2278 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
2279 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2281 rpc_ntop(sa, addr_str, sizeof(addr_str));
2282 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
2283 "ip_addr=%s flags %x, spa_how %d\n",
2284 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
2285 addr_str, exid->flags, exid->spa_how);
2287 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
2288 return nfserr_inval;
2290 switch (exid->spa_how) {
2292 if (!svc_rqst_integrity_protected(rqstp))
2293 return nfserr_inval;
2296 default: /* checked by xdr code */
2299 return nfserr_encr_alg_unsupp;
2302 new = create_client(exid->clname, rqstp, &verf);
2304 return nfserr_jukebox;
2306 /* Cases below refer to rfc 5661 section 18.35.4: */
2307 spin_lock(&nn->client_lock);
2308 conf = find_confirmed_client_by_name(&exid->clname, nn);
2310 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2311 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2314 if (!clp_used_exchangeid(conf)) { /* buggy client */
2315 status = nfserr_inval;
2318 if (!mach_creds_match(conf, rqstp)) {
2319 status = nfserr_wrong_cred;
2322 if (!creds_match) { /* case 9 */
2323 status = nfserr_perm;
2326 if (!verfs_match) { /* case 8 */
2327 status = nfserr_not_same;
2331 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
2334 if (!creds_match) { /* case 3 */
2335 if (client_has_state(conf)) {
2336 status = nfserr_clid_inuse;
2341 if (verfs_match) { /* case 2 */
2342 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
2345 /* case 5, client reboot */
2350 if (update) { /* case 7 */
2351 status = nfserr_noent;
2355 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
2356 if (unconf) /* case 4, possible retry or client restart */
2357 unhash_client_locked(unconf);
2359 /* case 1 (normal case) */
2362 status = mark_client_expired_locked(conf);
2366 new->cl_minorversion = cstate->minorversion;
2367 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
2370 add_to_unconfirmed(new);
2373 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
2374 exid->clientid.cl_id = conf->cl_clientid.cl_id;
2376 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
2377 nfsd4_set_ex_flags(conf, exid);
2379 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
2380 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
2384 spin_unlock(&nn->client_lock);
2388 expire_client(unconf);
2393 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
2395 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2398 /* The slot is in use, and no response has been sent. */
2400 if (seqid == slot_seqid)
2401 return nfserr_jukebox;
2403 return nfserr_seq_misordered;
2405 /* Note unsigned 32-bit arithmetic handles wraparound: */
2406 if (likely(seqid == slot_seqid + 1))
2408 if (seqid == slot_seqid)
2409 return nfserr_replay_cache;
2410 return nfserr_seq_misordered;
2414 * Cache the create session result into the create session single DRC
2415 * slot cache by saving the xdr structure. sl_seqid has been set.
2416 * Do this for solo or embedded create session operations.
2419 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
2420 struct nfsd4_clid_slot *slot, __be32 nfserr)
2422 slot->sl_status = nfserr;
2423 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2427 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2428 struct nfsd4_clid_slot *slot)
2430 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2431 return slot->sl_status;
2434 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2435 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2436 1 + /* MIN tag is length with zero, only length */ \
2437 3 + /* version, opcount, opcode */ \
2438 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2439 /* seqid, slotID, slotID, cache */ \
2440 4 ) * sizeof(__be32))
2442 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2443 2 + /* verifier: AUTH_NULL, length 0 */\
2445 1 + /* MIN tag is length with zero, only length */ \
2446 3 + /* opcount, opcode, opstatus*/ \
2447 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2448 /* seqid, slotID, slotID, slotID, status */ \
2449 5 ) * sizeof(__be32))
2451 static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
2453 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2455 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2456 return nfserr_toosmall;
2457 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2458 return nfserr_toosmall;
2459 ca->headerpadsz = 0;
2460 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2461 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2462 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2463 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2464 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2465 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2467 * Note decreasing slot size below client's request may make it
2468 * difficult for client to function correctly, whereas
2469 * decreasing the number of slots will (just?) affect
2470 * performance. When short on memory we therefore prefer to
2471 * decrease number of slots instead of their size. Clients that
2472 * request larger slots than they need will get poor results:
2474 ca->maxreqs = nfsd4_get_drc_mem(ca);
2476 return nfserr_jukebox;
2481 #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2482 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2483 #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2484 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2486 static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2488 ca->headerpadsz = 0;
2491 * These RPC_MAX_HEADER macros are overkill, especially since we
2492 * don't even do gss on the backchannel yet. But this is still
2493 * less than 1k. Tighten up this estimate in the unlikely event
2494 * it turns out to be a problem for some client:
2496 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
2497 return nfserr_toosmall;
2498 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
2499 return nfserr_toosmall;
2500 ca->maxresp_cached = 0;
2502 return nfserr_toosmall;
2507 static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2509 switch (cbs->flavor) {
2515 * GSS case: the spec doesn't allow us to return this
2516 * error. But it also doesn't allow us not to support
2518 * I'd rather this fail hard than return some error the
2519 * client might think it can already handle:
2521 return nfserr_encr_alg_unsupp;
2526 nfsd4_create_session(struct svc_rqst *rqstp,
2527 struct nfsd4_compound_state *cstate,
2528 struct nfsd4_create_session *cr_ses)
2530 struct sockaddr *sa = svc_addr(rqstp);
2531 struct nfs4_client *conf, *unconf;
2532 struct nfs4_client *old = NULL;
2533 struct nfsd4_session *new;
2534 struct nfsd4_conn *conn;
2535 struct nfsd4_clid_slot *cs_slot = NULL;
2537 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2539 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2540 return nfserr_inval;
2541 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2544 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
2547 status = check_backchannel_attrs(&cr_ses->back_channel);
2549 goto out_release_drc_mem;
2550 status = nfserr_jukebox;
2551 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
2553 goto out_release_drc_mem;
2554 conn = alloc_conn_from_crses(rqstp, cr_ses);
2556 goto out_free_session;
2558 spin_lock(&nn->client_lock);
2559 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
2560 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
2561 WARN_ON_ONCE(conf && unconf);
2564 status = nfserr_wrong_cred;
2565 if (!mach_creds_match(conf, rqstp))
2567 cs_slot = &conf->cl_cs_slot;
2568 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2569 if (status == nfserr_replay_cache) {
2570 status = nfsd4_replay_create_session(cr_ses, cs_slot);
2572 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
2573 status = nfserr_seq_misordered;
2576 } else if (unconf) {
2577 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
2578 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
2579 status = nfserr_clid_inuse;
2582 status = nfserr_wrong_cred;
2583 if (!mach_creds_match(unconf, rqstp))
2585 cs_slot = &unconf->cl_cs_slot;
2586 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2588 /* an unconfirmed replay returns misordered */
2589 status = nfserr_seq_misordered;
2592 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
2594 status = mark_client_expired_locked(old);
2600 move_to_confirmed(unconf);
2603 status = nfserr_stale_clientid;
2608 * We do not support RDMA or persistent sessions
2610 cr_ses->flags &= ~SESSION4_PERSIST;
2611 cr_ses->flags &= ~SESSION4_RDMA;
2613 init_session(rqstp, new, conf, cr_ses);
2614 nfsd4_get_session_locked(new);
2616 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
2617 NFS4_MAX_SESSIONID_LEN);
2618 cs_slot->sl_seqid++;
2619 cr_ses->seqid = cs_slot->sl_seqid;
2621 /* cache solo and embedded create sessions under the client_lock */
2622 nfsd4_cache_create_session(cr_ses, cs_slot, status);
2623 spin_unlock(&nn->client_lock);
2624 /* init connection and backchannel */
2625 nfsd4_init_conn(rqstp, conn, new);
2626 nfsd4_put_session(new);
2631 spin_unlock(&nn->client_lock);
2636 __free_session(new);
2637 out_release_drc_mem:
2638 nfsd4_put_drc_mem(&cr_ses->fore_channel);
2642 static __be32 nfsd4_map_bcts_dir(u32 *dir)
2645 case NFS4_CDFC4_FORE:
2646 case NFS4_CDFC4_BACK:
2648 case NFS4_CDFC4_FORE_OR_BOTH:
2649 case NFS4_CDFC4_BACK_OR_BOTH:
2650 *dir = NFS4_CDFC4_BOTH;
2653 return nfserr_inval;
2656 __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2658 struct nfsd4_session *session = cstate->session;
2659 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2662 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2665 spin_lock(&nn->client_lock);
2666 session->se_cb_prog = bc->bc_cb_program;
2667 session->se_cb_sec = bc->bc_cb_sec;
2668 spin_unlock(&nn->client_lock);
2670 nfsd4_probe_callback(session->se_client);
2675 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2676 struct nfsd4_compound_state *cstate,
2677 struct nfsd4_bind_conn_to_session *bcts)
2680 struct nfsd4_conn *conn;
2681 struct nfsd4_session *session;
2682 struct net *net = SVC_NET(rqstp);
2683 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2685 if (!nfsd4_last_compound_op(rqstp))
2686 return nfserr_not_only_op;
2687 spin_lock(&nn->client_lock);
2688 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
2689 spin_unlock(&nn->client_lock);
2691 goto out_no_session;
2692 status = nfserr_wrong_cred;
2693 if (!mach_creds_match(session->se_client, rqstp))
2695 status = nfsd4_map_bcts_dir(&bcts->dir);
2698 conn = alloc_conn(rqstp, bcts->dir);
2699 status = nfserr_jukebox;
2702 nfsd4_init_conn(rqstp, conn, session);
2705 nfsd4_put_session(session);
2710 static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2714 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2718 nfsd4_destroy_session(struct svc_rqst *r,
2719 struct nfsd4_compound_state *cstate,
2720 struct nfsd4_destroy_session *sessionid)
2722 struct nfsd4_session *ses;
2724 int ref_held_by_me = 0;
2725 struct net *net = SVC_NET(r);
2726 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2728 status = nfserr_not_only_op;
2729 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
2730 if (!nfsd4_last_compound_op(r))
2734 dump_sessionid(__func__, &sessionid->sessionid);
2735 spin_lock(&nn->client_lock);
2736 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
2738 goto out_client_lock;
2739 status = nfserr_wrong_cred;
2740 if (!mach_creds_match(ses->se_client, r))
2741 goto out_put_session;
2742 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
2744 goto out_put_session;
2745 unhash_session(ses);
2746 spin_unlock(&nn->client_lock);
2748 nfsd4_probe_callback_sync(ses->se_client);
2750 spin_lock(&nn->client_lock);
2753 nfsd4_put_session_locked(ses);
2755 spin_unlock(&nn->client_lock);
2760 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
2762 struct nfsd4_conn *c;
2764 list_for_each_entry(c, &s->se_conns, cn_persession) {
2765 if (c->cn_xprt == xpt) {
2772 static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
2774 struct nfs4_client *clp = ses->se_client;
2775 struct nfsd4_conn *c;
2776 __be32 status = nfs_ok;
2779 spin_lock(&clp->cl_lock);
2780 c = __nfsd4_find_conn(new->cn_xprt, ses);
2783 status = nfserr_conn_not_bound_to_session;
2784 if (clp->cl_mach_cred)
2786 __nfsd4_hash_conn(new, ses);
2787 spin_unlock(&clp->cl_lock);
2788 ret = nfsd4_register_conn(new);
2790 /* oops; xprt is already down: */
2791 nfsd4_conn_lost(&new->cn_xpt_user);
2794 spin_unlock(&clp->cl_lock);
2799 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2801 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2803 return args->opcnt > session->se_fchannel.maxops;
2806 static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2807 struct nfsd4_session *session)
2809 struct xdr_buf *xb = &rqstp->rq_arg;
2811 return xb->len > session->se_fchannel.maxreq_sz;
2815 nfsd4_sequence(struct svc_rqst *rqstp,
2816 struct nfsd4_compound_state *cstate,
2817 struct nfsd4_sequence *seq)
2819 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2820 struct xdr_stream *xdr = &resp->xdr;
2821 struct nfsd4_session *session;
2822 struct nfs4_client *clp;
2823 struct nfsd4_slot *slot;
2824 struct nfsd4_conn *conn;
2827 struct net *net = SVC_NET(rqstp);
2828 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2830 if (resp->opcnt != 1)
2831 return nfserr_sequence_pos;
2834 * Will be either used or freed by nfsd4_sequence_check_conn
2837 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2839 return nfserr_jukebox;
2841 spin_lock(&nn->client_lock);
2842 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
2844 goto out_no_session;
2845 clp = session->se_client;
2847 status = nfserr_too_many_ops;
2848 if (nfsd4_session_too_many_ops(rqstp, session))
2849 goto out_put_session;
2851 status = nfserr_req_too_big;
2852 if (nfsd4_request_too_big(rqstp, session))
2853 goto out_put_session;
2855 status = nfserr_badslot;
2856 if (seq->slotid >= session->se_fchannel.maxreqs)
2857 goto out_put_session;
2859 slot = session->se_slots[seq->slotid];
2860 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2862 /* We do not negotiate the number of slots yet, so set the
2863 * maxslots to the session maxreqs which is used to encode
2864 * sr_highest_slotid and the sr_target_slot id to maxslots */
2865 seq->maxslots = session->se_fchannel.maxreqs;
2867 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2868 slot->sl_flags & NFSD4_SLOT_INUSE);
2869 if (status == nfserr_replay_cache) {
2870 status = nfserr_seq_misordered;
2871 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
2872 goto out_put_session;
2873 cstate->slot = slot;
2874 cstate->session = session;
2876 /* Return the cached reply status and set cstate->status
2877 * for nfsd4_proc_compound processing */
2878 status = nfsd4_replay_cache_entry(resp, seq);
2879 cstate->status = nfserr_replay_cache;
2883 goto out_put_session;
2885 status = nfsd4_sequence_check_conn(conn, session);
2888 goto out_put_session;
2890 buflen = (seq->cachethis) ?
2891 session->se_fchannel.maxresp_cached :
2892 session->se_fchannel.maxresp_sz;
2893 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2895 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
2896 goto out_put_session;
2897 svc_reserve(rqstp, buflen);
2900 /* Success! bump slot seqid */
2901 slot->sl_seqid = seq->seqid;
2902 slot->sl_flags |= NFSD4_SLOT_INUSE;
2904 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
2906 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
2908 cstate->slot = slot;
2909 cstate->session = session;
2913 switch (clp->cl_cb_state) {
2915 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2917 case NFSD4_CB_FAULT:
2918 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2921 seq->status_flags = 0;
2923 if (!list_empty(&clp->cl_revoked))
2924 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
2928 spin_unlock(&nn->client_lock);
2931 nfsd4_put_session_locked(session);
2932 goto out_no_session;
2936 nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2938 struct nfsd4_compound_state *cs = &resp->cstate;
2940 if (nfsd4_has_session(cs)) {
2941 if (cs->status != nfserr_replay_cache) {
2942 nfsd4_store_cache_entry(resp);
2943 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2945 /* Drop session reference that was taken in nfsd4_sequence() */
2946 nfsd4_put_session(cs->session);
2948 put_client_renew(cs->clp);
2952 nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2954 struct nfs4_client *conf, *unconf;
2955 struct nfs4_client *clp = NULL;
2957 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2959 spin_lock(&nn->client_lock);
2960 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
2961 conf = find_confirmed_client(&dc->clientid, true, nn);
2962 WARN_ON_ONCE(conf && unconf);
2965 if (client_has_state(conf)) {
2966 status = nfserr_clientid_busy;
2969 status = mark_client_expired_locked(conf);
2976 status = nfserr_stale_clientid;
2979 if (!mach_creds_match(clp, rqstp)) {
2981 status = nfserr_wrong_cred;
2984 unhash_client_locked(clp);
2986 spin_unlock(&nn->client_lock);
2993 nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2997 if (rc->rca_one_fs) {
2998 if (!cstate->current_fh.fh_dentry)
2999 return nfserr_nofilehandle;
3001 * We don't take advantage of the rca_one_fs case.
3002 * That's OK, it's optional, we can safely ignore it.
3007 status = nfserr_complete_already;
3008 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
3009 &cstate->session->se_client->cl_flags))
3012 status = nfserr_stale_clientid;
3013 if (is_client_expired(cstate->session->se_client))
3015 * The following error isn't really legal.
3016 * But we only get here if the client just explicitly
3017 * destroyed the client. Surely it no longer cares what
3018 * error it gets back on an operation for the dead
3024 nfsd4_client_record_create(cstate->session->se_client);
3030 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3031 struct nfsd4_setclientid *setclid)
3033 struct xdr_netobj clname = setclid->se_name;
3034 nfs4_verifier clverifier = setclid->se_verf;
3035 struct nfs4_client *conf, *new;
3036 struct nfs4_client *unconf = NULL;
3038 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3040 new = create_client(clname, rqstp, &clverifier);
3042 return nfserr_jukebox;
3043 /* Cases below refer to rfc 3530 section 14.2.33: */
3044 spin_lock(&nn->client_lock);
3045 conf = find_confirmed_client_by_name(&clname, nn);
3048 status = nfserr_clid_inuse;
3049 if (clp_used_exchangeid(conf))
3051 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
3052 char addr_str[INET6_ADDRSTRLEN];
3053 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
3055 dprintk("NFSD: setclientid: string in use by client "
3056 "at %s\n", addr_str);
3060 unconf = find_unconfirmed_client_by_name(&clname, nn);
3062 unhash_client_locked(unconf);
3063 if (conf && same_verf(&conf->cl_verifier, &clverifier))
3064 /* case 1: probable callback update */
3065 copy_clid(new, conf);
3066 else /* case 4 (new client) or cases 2, 3 (client reboot): */
3068 new->cl_minorversion = 0;
3069 gen_callback(new, setclid, rqstp);
3070 add_to_unconfirmed(new);
3071 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
3072 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
3073 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
3077 spin_unlock(&nn->client_lock);
3081 expire_client(unconf);
3087 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
3088 struct nfsd4_compound_state *cstate,
3089 struct nfsd4_setclientid_confirm *setclientid_confirm)
3091 struct nfs4_client *conf, *unconf;
3092 struct nfs4_client *old = NULL;
3093 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
3094 clientid_t * clid = &setclientid_confirm->sc_clientid;
3096 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3098 if (STALE_CLIENTID(clid, nn))
3099 return nfserr_stale_clientid;
3101 spin_lock(&nn->client_lock);
3102 conf = find_confirmed_client(clid, false, nn);
3103 unconf = find_unconfirmed_client(clid, false, nn);
3105 * We try hard to give out unique clientid's, so if we get an
3106 * attempt to confirm the same clientid with a different cred,
3107 * there's a bug somewhere. Let's charitably assume it's our
3110 status = nfserr_serverfault;
3111 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3113 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3115 /* cases below refer to rfc 3530 section 14.2.34: */
3116 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
3117 if (conf && !unconf) /* case 2: probable retransmit */
3119 else /* case 4: client hasn't noticed we rebooted yet? */
3120 status = nfserr_stale_clientid;
3124 if (conf) { /* case 1: callback update */
3126 unhash_client_locked(old);
3127 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
3128 } else { /* case 3: normal case; new or rebooted client */
3129 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3131 status = mark_client_expired_locked(old);
3137 move_to_confirmed(unconf);
3140 get_client_locked(conf);
3141 spin_unlock(&nn->client_lock);
3142 nfsd4_probe_callback(conf);
3143 spin_lock(&nn->client_lock);
3144 put_client_renew_locked(conf);
3146 spin_unlock(&nn->client_lock);
3152 static struct nfs4_file *nfsd4_alloc_file(void)
3154 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3157 /* OPEN Share state helper functions */
3158 static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
3159 struct nfs4_file *fp)
3161 lockdep_assert_held(&state_lock);
3163 atomic_set(&fp->fi_ref, 1);
3164 spin_lock_init(&fp->fi_lock);
3165 INIT_LIST_HEAD(&fp->fi_stateids);
3166 INIT_LIST_HEAD(&fp->fi_delegations);
3167 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
3168 fh_copy_shallow(&fp->fi_fhandle, fh);
3169 fp->fi_deleg_file = NULL;
3170 fp->fi_had_conflict = false;
3171 fp->fi_share_deny = 0;
3172 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3173 memset(fp->fi_access, 0, sizeof(fp->fi_access));
3174 #ifdef CONFIG_NFSD_PNFS
3175 INIT_LIST_HEAD(&fp->fi_lo_states);
3176 atomic_set(&fp->fi_lo_recalls, 0);
3178 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
3182 nfsd4_free_slabs(void)
3184 kmem_cache_destroy(odstate_slab);
3185 kmem_cache_destroy(openowner_slab);
3186 kmem_cache_destroy(lockowner_slab);
3187 kmem_cache_destroy(file_slab);
3188 kmem_cache_destroy(stateid_slab);
3189 kmem_cache_destroy(deleg_slab);
3193 nfsd4_init_slabs(void)
3195 openowner_slab = kmem_cache_create("nfsd4_openowners",
3196 sizeof(struct nfs4_openowner), 0, 0, NULL);
3197 if (openowner_slab == NULL)
3199 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
3200 sizeof(struct nfs4_lockowner), 0, 0, NULL);
3201 if (lockowner_slab == NULL)
3202 goto out_free_openowner_slab;
3203 file_slab = kmem_cache_create("nfsd4_files",
3204 sizeof(struct nfs4_file), 0, 0, NULL);
3205 if (file_slab == NULL)
3206 goto out_free_lockowner_slab;
3207 stateid_slab = kmem_cache_create("nfsd4_stateids",
3208 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
3209 if (stateid_slab == NULL)
3210 goto out_free_file_slab;
3211 deleg_slab = kmem_cache_create("nfsd4_delegations",
3212 sizeof(struct nfs4_delegation), 0, 0, NULL);
3213 if (deleg_slab == NULL)
3214 goto out_free_stateid_slab;
3215 odstate_slab = kmem_cache_create("nfsd4_odstate",
3216 sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
3217 if (odstate_slab == NULL)
3218 goto out_free_deleg_slab;
3221 out_free_deleg_slab:
3222 kmem_cache_destroy(deleg_slab);
3223 out_free_stateid_slab:
3224 kmem_cache_destroy(stateid_slab);
3226 kmem_cache_destroy(file_slab);
3227 out_free_lockowner_slab:
3228 kmem_cache_destroy(lockowner_slab);
3229 out_free_openowner_slab:
3230 kmem_cache_destroy(openowner_slab);
3232 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3236 static void init_nfs4_replay(struct nfs4_replay *rp)
3238 rp->rp_status = nfserr_serverfault;
3240 rp->rp_buf = rp->rp_ibuf;
3241 mutex_init(&rp->rp_mutex);
3244 static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3245 struct nfs4_stateowner *so)
3247 if (!nfsd4_has_session(cstate)) {
3248 mutex_lock(&so->so_replay.rp_mutex);
3249 cstate->replay_owner = nfs4_get_stateowner(so);
3253 void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3255 struct nfs4_stateowner *so = cstate->replay_owner;
3258 cstate->replay_owner = NULL;
3259 mutex_unlock(&so->so_replay.rp_mutex);
3260 nfs4_put_stateowner(so);
3264 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
3266 struct nfs4_stateowner *sop;
3268 sop = kmem_cache_alloc(slab, GFP_KERNEL);
3272 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3273 if (!sop->so_owner.data) {
3274 kmem_cache_free(slab, sop);
3277 sop->so_owner.len = owner->len;
3279 INIT_LIST_HEAD(&sop->so_stateids);
3280 sop->so_client = clp;
3281 init_nfs4_replay(&sop->so_replay);
3282 atomic_set(&sop->so_count, 1);
3286 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
3288 lockdep_assert_held(&clp->cl_lock);
3290 list_add(&oo->oo_owner.so_strhash,
3291 &clp->cl_ownerstr_hashtbl[strhashval]);
3292 list_add(&oo->oo_perclient, &clp->cl_openowners);
3295 static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3297 unhash_openowner_locked(openowner(so));
3300 static void nfs4_free_openowner(struct nfs4_stateowner *so)
3302 struct nfs4_openowner *oo = openowner(so);
3304 kmem_cache_free(openowner_slab, oo);
3307 static const struct nfs4_stateowner_operations openowner_ops = {
3308 .so_unhash = nfs4_unhash_openowner,
3309 .so_free = nfs4_free_openowner,
3312 static struct nfs4_openowner *
3313 alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
3314 struct nfsd4_compound_state *cstate)
3316 struct nfs4_client *clp = cstate->clp;
3317 struct nfs4_openowner *oo, *ret;
3319 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3322 oo->oo_owner.so_ops = &openowner_ops;
3323 oo->oo_owner.so_is_open_owner = 1;
3324 oo->oo_owner.so_seqid = open->op_seqid;
3326 if (nfsd4_has_session(cstate))
3327 oo->oo_flags |= NFS4_OO_CONFIRMED;
3329 oo->oo_last_closed_stid = NULL;
3330 INIT_LIST_HEAD(&oo->oo_close_lru);
3331 spin_lock(&clp->cl_lock);
3332 ret = find_openstateowner_str_locked(strhashval, open, clp);
3334 hash_openowner(oo, clp, strhashval);
3337 nfs4_free_openowner(&oo->oo_owner);
3338 spin_unlock(&clp->cl_lock);
3342 static void init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
3343 struct nfs4_openowner *oo = open->op_openowner;
3345 atomic_inc(&stp->st_stid.sc_count);
3346 stp->st_stid.sc_type = NFS4_OPEN_STID;
3347 INIT_LIST_HEAD(&stp->st_locks);
3348 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
3350 stp->st_stid.sc_file = fp;
3351 stp->st_access_bmap = 0;
3352 stp->st_deny_bmap = 0;
3353 stp->st_openstp = NULL;
3354 spin_lock(&oo->oo_owner.so_client->cl_lock);
3355 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
3356 spin_lock(&fp->fi_lock);
3357 list_add(&stp->st_perfile, &fp->fi_stateids);
3358 spin_unlock(&fp->fi_lock);
3359 spin_unlock(&oo->oo_owner.so_client->cl_lock);
3363 * In the 4.0 case we need to keep the owners around a little while to handle
3364 * CLOSE replay. We still do need to release any file access that is held by
3365 * them before returning however.
3368 move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
3370 struct nfs4_ol_stateid *last;
3371 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3372 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3375 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
3378 * We know that we hold one reference via nfsd4_close, and another
3379 * "persistent" reference for the client. If the refcount is higher
3380 * than 2, then there are still calls in progress that are using this
3381 * stateid. We can't put the sc_file reference until they are finished.
3382 * Wait for the refcount to drop to 2. Since it has been unhashed,
3383 * there should be no danger of the refcount going back up again at
3386 wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3388 release_all_access(s);
3389 if (s->st_stid.sc_file) {
3390 put_nfs4_file(s->st_stid.sc_file);
3391 s->st_stid.sc_file = NULL;
3394 spin_lock(&nn->client_lock);
3395 last = oo->oo_last_closed_stid;
3396 oo->oo_last_closed_stid = s;
3397 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
3398 oo->oo_time = get_seconds();
3399 spin_unlock(&nn->client_lock);
3401 nfs4_put_stid(&last->st_stid);
3404 /* search file_hashtbl[] for file */
3405 static struct nfs4_file *
3406 find_file_locked(struct knfsd_fh *fh, unsigned int hashval)
3408 struct nfs4_file *fp;
3410 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash) {
3411 if (fh_match(&fp->fi_fhandle, fh)) {
3412 if (atomic_inc_not_zero(&fp->fi_ref))
3420 find_file(struct knfsd_fh *fh)
3422 struct nfs4_file *fp;
3423 unsigned int hashval = file_hashval(fh);
3426 fp = find_file_locked(fh, hashval);
3431 static struct nfs4_file *
3432 find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
3434 struct nfs4_file *fp;
3435 unsigned int hashval = file_hashval(fh);
3438 fp = find_file_locked(fh, hashval);
3443 spin_lock(&state_lock);
3444 fp = find_file_locked(fh, hashval);
3445 if (likely(fp == NULL)) {
3446 nfsd4_init_file(fh, hashval, new);
3449 spin_unlock(&state_lock);
3455 * Called to check deny when READ with all zero stateid or
3456 * WRITE with all zero or all one stateid
3459 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3461 struct nfs4_file *fp;
3462 __be32 ret = nfs_ok;
3464 fp = find_file(¤t_fh->fh_handle);
3467 /* Check for conflicting share reservations */
3468 spin_lock(&fp->fi_lock);
3469 if (fp->fi_share_deny & deny_type)
3470 ret = nfserr_locked;
3471 spin_unlock(&fp->fi_lock);
3476 static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
3478 struct nfs4_delegation *dp = cb_to_delegation(cb);
3479 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3482 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
3485 * We can't do this in nfsd_break_deleg_cb because it is
3486 * already holding inode->i_lock.
3488 * If the dl_time != 0, then we know that it has already been
3489 * queued for a lease break. Don't queue it again.
3491 spin_lock(&state_lock);
3492 if (dp->dl_time == 0) {
3493 dp->dl_time = get_seconds();
3494 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
3496 spin_unlock(&state_lock);
3499 static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
3500 struct rpc_task *task)
3502 struct nfs4_delegation *dp = cb_to_delegation(cb);
3504 switch (task->tk_status) {
3508 case -NFS4ERR_BAD_STATEID:
3510 * Race: client probably got cb_recall before open reply
3511 * granting delegation.
3513 if (dp->dl_retries--) {
3514 rpc_delay(task, 2 * HZ);
3523 static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
3525 struct nfs4_delegation *dp = cb_to_delegation(cb);
3527 nfs4_put_stid(&dp->dl_stid);
3530 static struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
3531 .prepare = nfsd4_cb_recall_prepare,
3532 .done = nfsd4_cb_recall_done,
3533 .release = nfsd4_cb_recall_release,
3536 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3539 * We're assuming the state code never drops its reference
3540 * without first removing the lease. Since we're in this lease
3541 * callback (and since the lease code is serialized by the kernel
3542 * lock) we know the server hasn't removed the lease yet, we know
3543 * it's safe to take a reference.
3545 atomic_inc(&dp->dl_stid.sc_count);
3546 nfsd4_run_cb(&dp->dl_recall);
3549 /* Called from break_lease() with i_lock held. */
3551 nfsd_break_deleg_cb(struct file_lock *fl)
3554 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3555 struct nfs4_delegation *dp;
3558 WARN(1, "(%p)->fl_owner NULL\n", fl);
3561 if (fp->fi_had_conflict) {
3562 WARN(1, "duplicate break on %p\n", fp);
3566 * We don't want the locks code to timeout the lease for us;
3567 * we'll remove it ourself if a delegation isn't returned
3570 fl->fl_break_time = 0;
3572 spin_lock(&fp->fi_lock);
3573 fp->fi_had_conflict = true;
3575 * If there are no delegations on the list, then return true
3576 * so that the lease code will go ahead and delete it.
3578 if (list_empty(&fp->fi_delegations))
3581 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3582 nfsd_break_one_deleg(dp);
3583 spin_unlock(&fp->fi_lock);
3588 nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
3589 struct list_head *dispose)
3592 return lease_modify(onlist, arg, dispose);
3597 static const struct lock_manager_operations nfsd_lease_mng_ops = {
3598 .lm_break = nfsd_break_deleg_cb,
3599 .lm_change = nfsd_change_deleg_cb,
3602 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3604 if (nfsd4_has_session(cstate))
3606 if (seqid == so->so_seqid - 1)
3607 return nfserr_replay_me;
3608 if (seqid == so->so_seqid)
3610 return nfserr_bad_seqid;
3613 static __be32 lookup_clientid(clientid_t *clid,
3614 struct nfsd4_compound_state *cstate,
3615 struct nfsd_net *nn)
3617 struct nfs4_client *found;
3620 found = cstate->clp;
3621 if (!same_clid(&found->cl_clientid, clid))
3622 return nfserr_stale_clientid;
3626 if (STALE_CLIENTID(clid, nn))
3627 return nfserr_stale_clientid;
3630 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3631 * cached already then we know this is for is for v4.0 and "sessions"
3634 WARN_ON_ONCE(cstate->session);
3635 spin_lock(&nn->client_lock);
3636 found = find_confirmed_client(clid, false, nn);
3638 spin_unlock(&nn->client_lock);
3639 return nfserr_expired;
3641 atomic_inc(&found->cl_refcount);
3642 spin_unlock(&nn->client_lock);
3644 /* Cache the nfs4_client in cstate! */
3645 cstate->clp = found;
3650 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
3651 struct nfsd4_open *open, struct nfsd_net *nn)
3653 clientid_t *clientid = &open->op_clientid;
3654 struct nfs4_client *clp = NULL;
3655 unsigned int strhashval;
3656 struct nfs4_openowner *oo = NULL;
3659 if (STALE_CLIENTID(&open->op_clientid, nn))
3660 return nfserr_stale_clientid;
3662 * In case we need it later, after we've already created the
3663 * file and don't want to risk a further failure:
3665 open->op_file = nfsd4_alloc_file();
3666 if (open->op_file == NULL)
3667 return nfserr_jukebox;
3669 status = lookup_clientid(clientid, cstate, nn);
3674 strhashval = ownerstr_hashval(&open->op_owner);
3675 oo = find_openstateowner_str(strhashval, open, clp);
3676 open->op_openowner = oo;
3680 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
3681 /* Replace unconfirmed owners without checking for replay. */
3682 release_openowner(oo);
3683 open->op_openowner = NULL;
3686 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3691 oo = alloc_init_open_stateowner(strhashval, open, cstate);
3693 return nfserr_jukebox;
3694 open->op_openowner = oo;
3696 open->op_stp = nfs4_alloc_open_stateid(clp);
3698 return nfserr_jukebox;
3700 if (nfsd4_has_session(cstate) &&
3701 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
3702 open->op_odstate = alloc_clnt_odstate(clp);
3703 if (!open->op_odstate)
3704 return nfserr_jukebox;
3710 static inline __be32
3711 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3713 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3714 return nfserr_openmode;
3719 static int share_access_to_flags(u32 share_access)
3721 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3724 static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
3726 struct nfs4_stid *ret;
3728 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
3731 return delegstateid(ret);
3734 static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3736 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3737 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3741 nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
3742 struct nfs4_delegation **dp)
3745 __be32 status = nfserr_bad_stateid;
3746 struct nfs4_delegation *deleg;
3748 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3751 flags = share_access_to_flags(open->op_share_access);
3752 status = nfs4_check_delegmode(deleg, flags);
3754 nfs4_put_stid(&deleg->dl_stid);
3759 if (!nfsd4_is_deleg_cur(open))
3763 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
3767 static struct nfs4_ol_stateid *
3768 nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
3770 struct nfs4_ol_stateid *local, *ret = NULL;
3771 struct nfs4_openowner *oo = open->op_openowner;
3773 spin_lock(&fp->fi_lock);
3774 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
3775 /* ignore lock owners */
3776 if (local->st_stateowner->so_is_open_owner == 0)
3778 if (local->st_stateowner == &oo->oo_owner) {
3780 atomic_inc(&ret->st_stid.sc_count);
3784 spin_unlock(&fp->fi_lock);
3788 static inline int nfs4_access_to_access(u32 nfs4_access)
3792 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3793 flags |= NFSD_MAY_READ;
3794 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3795 flags |= NFSD_MAY_WRITE;
3799 static inline __be32
3800 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3801 struct nfsd4_open *open)
3803 struct iattr iattr = {
3804 .ia_valid = ATTR_SIZE,
3807 if (!open->op_truncate)
3809 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
3810 return nfserr_inval;
3811 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3814 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
3815 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3816 struct nfsd4_open *open)
3818 struct file *filp = NULL;
3820 int oflag = nfs4_access_to_omode(open->op_share_access);
3821 int access = nfs4_access_to_access(open->op_share_access);
3822 unsigned char old_access_bmap, old_deny_bmap;
3824 spin_lock(&fp->fi_lock);
3827 * Are we trying to set a deny mode that would conflict with
3830 status = nfs4_file_check_deny(fp, open->op_share_deny);
3831 if (status != nfs_ok) {
3832 spin_unlock(&fp->fi_lock);
3836 /* set access to the file */
3837 status = nfs4_file_get_access(fp, open->op_share_access);
3838 if (status != nfs_ok) {
3839 spin_unlock(&fp->fi_lock);
3843 /* Set access bits in stateid */
3844 old_access_bmap = stp->st_access_bmap;
3845 set_access(open->op_share_access, stp);
3847 /* Set new deny mask */
3848 old_deny_bmap = stp->st_deny_bmap;
3849 set_deny(open->op_share_deny, stp);
3850 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3852 if (!fp->fi_fds[oflag]) {
3853 spin_unlock(&fp->fi_lock);
3854 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
3856 goto out_put_access;
3857 spin_lock(&fp->fi_lock);
3858 if (!fp->fi_fds[oflag]) {
3859 fp->fi_fds[oflag] = filp;
3863 spin_unlock(&fp->fi_lock);
3867 status = nfsd4_truncate(rqstp, cur_fh, open);
3869 goto out_put_access;
3873 stp->st_access_bmap = old_access_bmap;
3874 nfs4_file_put_access(fp, open->op_share_access);
3875 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3880 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)
3883 unsigned char old_deny_bmap;
3885 if (!test_access(open->op_share_access, stp))
3886 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
3888 /* test and set deny mode */
3889 spin_lock(&fp->fi_lock);
3890 status = nfs4_file_check_deny(fp, open->op_share_deny);
3891 if (status == nfs_ok) {
3892 old_deny_bmap = stp->st_deny_bmap;
3893 set_deny(open->op_share_deny, stp);
3894 fp->fi_share_deny |=
3895 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3897 spin_unlock(&fp->fi_lock);
3899 if (status != nfs_ok)
3902 status = nfsd4_truncate(rqstp, cur_fh, open);
3903 if (status != nfs_ok)
3904 reset_union_bmap_deny(old_deny_bmap, stp);
3909 nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
3911 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
3914 /* Should we give out recallable state?: */
3915 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3917 if (clp->cl_cb_state == NFSD4_CB_UP)
3920 * In the sessions case, since we don't have to establish a
3921 * separate connection for callbacks, we assume it's OK
3922 * until we hear otherwise:
3924 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3927 static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
3929 struct file_lock *fl;
3931 fl = locks_alloc_lock();
3934 fl->fl_lmops = &nfsd_lease_mng_ops;
3935 fl->fl_flags = FL_DELEG;
3936 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3937 fl->fl_end = OFFSET_MAX;
3938 fl->fl_owner = (fl_owner_t)fp;
3939 fl->fl_pid = current->tgid;
3943 static int nfs4_setlease(struct nfs4_delegation *dp)
3945 struct nfs4_file *fp = dp->dl_stid.sc_file;
3946 struct file_lock *fl, *ret;
3950 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
3953 filp = find_readable_file(fp);
3955 /* We should always have a readable file here */
3961 status = vfs_setlease(filp, fl->fl_type, &fl, NULL);
3963 locks_free_lock(fl);
3966 spin_lock(&state_lock);
3967 spin_lock(&fp->fi_lock);
3968 /* Did the lease get broken before we took the lock? */
3970 if (fp->fi_had_conflict)
3973 if (fp->fi_deleg_file) {
3976 hash_delegation_locked(dp, fp);
3979 fp->fi_deleg_file = filp;
3980 fp->fi_delegees = 1;
3981 hash_delegation_locked(dp, fp);
3982 spin_unlock(&fp->fi_lock);
3983 spin_unlock(&state_lock);
3986 spin_unlock(&fp->fi_lock);
3987 spin_unlock(&state_lock);
3993 static struct nfs4_delegation *
3994 nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
3995 struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate)
3998 struct nfs4_delegation *dp;
4000 if (fp->fi_had_conflict)
4001 return ERR_PTR(-EAGAIN);
4003 dp = alloc_init_deleg(clp, fh, odstate);
4005 return ERR_PTR(-ENOMEM);
4008 spin_lock(&state_lock);
4009 spin_lock(&fp->fi_lock);
4010 dp->dl_stid.sc_file = fp;
4011 if (!fp->fi_deleg_file) {
4012 spin_unlock(&fp->fi_lock);
4013 spin_unlock(&state_lock);
4014 status = nfs4_setlease(dp);
4017 if (fp->fi_had_conflict) {
4022 hash_delegation_locked(dp, fp);
4025 spin_unlock(&fp->fi_lock);
4026 spin_unlock(&state_lock);
4029 put_clnt_odstate(dp->dl_clnt_odstate);
4030 nfs4_put_stid(&dp->dl_stid);
4031 return ERR_PTR(status);
4036 static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
4038 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4039 if (status == -EAGAIN)
4040 open->op_why_no_deleg = WND4_CONTENTION;
4042 open->op_why_no_deleg = WND4_RESOURCE;
4043 switch (open->op_deleg_want) {
4044 case NFS4_SHARE_WANT_READ_DELEG:
4045 case NFS4_SHARE_WANT_WRITE_DELEG:
4046 case NFS4_SHARE_WANT_ANY_DELEG:
4048 case NFS4_SHARE_WANT_CANCEL:
4049 open->op_why_no_deleg = WND4_CANCELLED;
4051 case NFS4_SHARE_WANT_NO_DELEG:
4058 * Attempt to hand out a delegation.
4060 * Note we don't support write delegations, and won't until the vfs has
4061 * proper support for them.
4064 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
4065 struct nfs4_ol_stateid *stp)
4067 struct nfs4_delegation *dp;
4068 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
4069 struct nfs4_client *clp = stp->st_stid.sc_client;
4073 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
4074 open->op_recall = 0;
4075 switch (open->op_claim_type) {
4076 case NFS4_OPEN_CLAIM_PREVIOUS:
4078 open->op_recall = 1;
4079 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
4082 case NFS4_OPEN_CLAIM_NULL:
4083 case NFS4_OPEN_CLAIM_FH:
4085 * Let's not give out any delegations till everyone's
4086 * had the chance to reclaim theirs....
4088 if (locks_in_grace(clp->net))
4090 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
4093 * Also, if the file was opened for write or
4094 * create, there's a good chance the client's
4095 * about to write to it, resulting in an
4096 * immediate recall (since we don't support
4097 * write delegations):
4099 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
4101 if (open->op_create == NFS4_OPEN_CREATE)
4107 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate);
4111 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
4113 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
4114 STATEID_VAL(&dp->dl_stid.sc_stateid));
4115 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
4116 nfs4_put_stid(&dp->dl_stid);
4119 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
4120 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
4121 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
4122 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
4123 open->op_recall = 1;
4126 /* 4.1 client asking for a delegation? */
4127 if (open->op_deleg_want)
4128 nfsd4_open_deleg_none_ext(open, status);
4132 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
4133 struct nfs4_delegation *dp)
4135 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
4136 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4137 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4138 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
4139 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
4140 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4141 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4142 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
4144 /* Otherwise the client must be confused wanting a delegation
4145 * it already has, therefore we don't return
4146 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
4151 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
4153 struct nfsd4_compoundres *resp = rqstp->rq_resp;
4154 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
4155 struct nfs4_file *fp = NULL;
4156 struct nfs4_ol_stateid *stp = NULL;
4157 struct nfs4_delegation *dp = NULL;
4161 * Lookup file; if found, lookup stateid and check open request,
4162 * and check for delegations in the process of being recalled.
4163 * If not found, create the nfs4_file struct
4165 fp = find_or_add_file(open->op_file, ¤t_fh->fh_handle);
4166 if (fp != open->op_file) {
4167 status = nfs4_check_deleg(cl, open, &dp);
4170 stp = nfsd4_find_existing_open(fp, open);
4172 open->op_file = NULL;
4173 status = nfserr_bad_stateid;
4174 if (nfsd4_is_deleg_cur(open))
4179 * OPEN the file, or upgrade an existing OPEN.
4180 * If truncate fails, the OPEN fails.
4183 /* Stateid was found, this is an OPEN upgrade */
4184 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
4189 open->op_stp = NULL;
4190 init_open_stateid(stp, fp, open);
4191 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4193 release_open_stateid(stp);
4197 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
4199 if (stp->st_clnt_odstate == open->op_odstate)
4200 open->op_odstate = NULL;
4202 update_stateid(&stp->st_stid.sc_stateid);
4203 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4205 if (nfsd4_has_session(&resp->cstate)) {
4206 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4207 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4208 open->op_why_no_deleg = WND4_NOT_WANTED;
4214 * Attempt to hand out a delegation. No error return, because the
4215 * OPEN succeeds even if we fail.
4217 nfs4_open_delegation(current_fh, open, stp);
4221 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
4222 STATEID_VAL(&stp->st_stid.sc_stateid));
4224 /* 4.1 client trying to upgrade/downgrade delegation? */
4225 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
4226 open->op_deleg_want)
4227 nfsd4_deleg_xgrade_none_ext(open, dp);
4231 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
4232 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
4234 * To finish the open response, we just need to set the rflags.
4236 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
4237 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
4238 !nfsd4_has_session(&resp->cstate))
4239 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
4241 nfs4_put_stid(&dp->dl_stid);
4243 nfs4_put_stid(&stp->st_stid);
4248 void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
4249 struct nfsd4_open *open)
4251 if (open->op_openowner) {
4252 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
4254 nfsd4_cstate_assign_replay(cstate, so);
4255 nfs4_put_stateowner(so);
4258 kmem_cache_free(file_slab, open->op_file);
4260 nfs4_put_stid(&open->op_stp->st_stid);
4261 if (open->op_odstate)
4262 kmem_cache_free(odstate_slab, open->op_odstate);
4266 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4269 struct nfs4_client *clp;
4271 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4273 dprintk("process_renew(%08x/%08x): starting\n",
4274 clid->cl_boot, clid->cl_id);
4275 status = lookup_clientid(clid, cstate, nn);
4279 status = nfserr_cb_path_down;
4280 if (!list_empty(&clp->cl_delegations)
4281 && clp->cl_cb_state != NFSD4_CB_UP)
4289 nfsd4_end_grace(struct nfsd_net *nn)
4291 /* do nothing if grace period already ended */
4292 if (nn->grace_ended)
4295 dprintk("NFSD: end of grace period\n");
4296 nn->grace_ended = true;
4298 * If the server goes down again right now, an NFSv4
4299 * client will still be allowed to reclaim after it comes back up,
4300 * even if it hasn't yet had a chance to reclaim state this time.
4303 nfsd4_record_grace_done(nn);
4305 * At this point, NFSv4 clients can still reclaim. But if the
4306 * server crashes, any that have not yet reclaimed will be out
4307 * of luck on the next boot.
4309 * (NFSv4.1+ clients are considered to have reclaimed once they
4310 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
4311 * have reclaimed after their first OPEN.)
4313 locks_end_grace(&nn->nfsd4_manager);
4315 * At this point, and once lockd and/or any other containers
4316 * exit their grace period, further reclaims will fail and
4317 * regular locking can resume.
4322 nfs4_laundromat(struct nfsd_net *nn)
4324 struct nfs4_client *clp;
4325 struct nfs4_openowner *oo;
4326 struct nfs4_delegation *dp;
4327 struct nfs4_ol_stateid *stp;
4328 struct list_head *pos, *next, reaplist;
4329 time_t cutoff = get_seconds() - nn->nfsd4_lease;
4330 time_t t, new_timeo = nn->nfsd4_lease;
4332 dprintk("NFSD: laundromat service - starting\n");
4333 nfsd4_end_grace(nn);
4334 INIT_LIST_HEAD(&reaplist);
4335 spin_lock(&nn->client_lock);
4336 list_for_each_safe(pos, next, &nn->client_lru) {
4337 clp = list_entry(pos, struct nfs4_client, cl_lru);
4338 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4339 t = clp->cl_time - cutoff;
4340 new_timeo = min(new_timeo, t);
4343 if (mark_client_expired_locked(clp)) {
4344 dprintk("NFSD: client in use (clientid %08x)\n",
4345 clp->cl_clientid.cl_id);
4348 list_add(&clp->cl_lru, &reaplist);
4350 spin_unlock(&nn->client_lock);
4351 list_for_each_safe(pos, next, &reaplist) {
4352 clp = list_entry(pos, struct nfs4_client, cl_lru);
4353 dprintk("NFSD: purging unused client (clientid %08x)\n",
4354 clp->cl_clientid.cl_id);
4355 list_del_init(&clp->cl_lru);
4358 spin_lock(&state_lock);
4359 list_for_each_safe(pos, next, &nn->del_recall_lru) {
4360 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4361 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
4363 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
4364 t = dp->dl_time - cutoff;
4365 new_timeo = min(new_timeo, t);
4368 WARN_ON(!unhash_delegation_locked(dp));
4369 list_add(&dp->dl_recall_lru, &reaplist);
4371 spin_unlock(&state_lock);
4372 while (!list_empty(&reaplist)) {
4373 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4375 list_del_init(&dp->dl_recall_lru);
4376 revoke_delegation(dp);
4379 spin_lock(&nn->client_lock);
4380 while (!list_empty(&nn->close_lru)) {
4381 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
4383 if (time_after((unsigned long)oo->oo_time,
4384 (unsigned long)cutoff)) {
4385 t = oo->oo_time - cutoff;
4386 new_timeo = min(new_timeo, t);
4389 list_del_init(&oo->oo_close_lru);
4390 stp = oo->oo_last_closed_stid;
4391 oo->oo_last_closed_stid = NULL;
4392 spin_unlock(&nn->client_lock);
4393 nfs4_put_stid(&stp->st_stid);
4394 spin_lock(&nn->client_lock);
4396 spin_unlock(&nn->client_lock);
4398 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
4402 static struct workqueue_struct *laundry_wq;
4403 static void laundromat_main(struct work_struct *);
4406 laundromat_main(struct work_struct *laundry)
4409 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
4411 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4414 t = nfs4_laundromat(nn);
4415 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
4416 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
4419 static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
4421 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
4422 return nfserr_bad_stateid;
4427 access_permit_read(struct nfs4_ol_stateid *stp)
4429 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4430 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4431 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
4435 access_permit_write(struct nfs4_ol_stateid *stp)
4437 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4438 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
4442 __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
4444 __be32 status = nfserr_openmode;
4446 /* For lock stateid's, we test the parent open, not the lock: */
4447 if (stp->st_openstp)
4448 stp = stp->st_openstp;
4449 if ((flags & WR_STATE) && !access_permit_write(stp))
4451 if ((flags & RD_STATE) && !access_permit_read(stp))
4458 static inline __be32
4459 check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
4461 if (ONE_STATEID(stateid) && (flags & RD_STATE))
4463 else if (locks_in_grace(net)) {
4464 /* Answer in remaining cases depends on existence of
4465 * conflicting state; so we must wait out the grace period. */
4466 return nfserr_grace;
4467 } else if (flags & WR_STATE)
4468 return nfs4_share_conflict(current_fh,
4469 NFS4_SHARE_DENY_WRITE);
4470 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4471 return nfs4_share_conflict(current_fh,
4472 NFS4_SHARE_DENY_READ);
4476 * Allow READ/WRITE during grace period on recovered state only for files
4477 * that are not able to provide mandatory locking.
4480 grace_disallows_io(struct net *net, struct inode *inode)
4482 return locks_in_grace(net) && mandatory_lock(inode);
4485 /* Returns true iff a is later than b: */
4486 static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4488 return (s32)(a->si_generation - b->si_generation) > 0;
4491 static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
4494 * When sessions are used the stateid generation number is ignored
4497 if (has_session && in->si_generation == 0)
4500 if (in->si_generation == ref->si_generation)
4503 /* If the client sends us a stateid from the future, it's buggy: */
4504 if (stateid_generation_after(in, ref))
4505 return nfserr_bad_stateid;
4507 * However, we could see a stateid from the past, even from a
4508 * non-buggy client. For example, if the client sends a lock
4509 * while some IO is outstanding, the lock may bump si_generation
4510 * while the IO is still in flight. The client could avoid that
4511 * situation by waiting for responses on all the IO requests,
4512 * but better performance may result in retrying IO that
4513 * receives an old_stateid error if requests are rarely
4514 * reordered in flight:
4516 return nfserr_old_stateid;
4519 static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
4521 if (ols->st_stateowner->so_is_open_owner &&
4522 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
4523 return nfserr_bad_stateid;
4527 static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
4529 struct nfs4_stid *s;
4530 __be32 status = nfserr_bad_stateid;
4532 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4534 /* Client debugging aid. */
4535 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4536 char addr_str[INET6_ADDRSTRLEN];
4537 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4539 pr_warn_ratelimited("NFSD: client %s testing state ID "
4540 "with incorrect client ID\n", addr_str);
4543 spin_lock(&cl->cl_lock);
4544 s = find_stateid_locked(cl, stateid);
4547 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
4550 switch (s->sc_type) {
4551 case NFS4_DELEG_STID:
4554 case NFS4_REVOKED_DELEG_STID:
4555 status = nfserr_deleg_revoked;
4557 case NFS4_OPEN_STID:
4558 case NFS4_LOCK_STID:
4559 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
4562 printk("unknown stateid type %x\n", s->sc_type);
4564 case NFS4_CLOSED_STID:
4565 case NFS4_CLOSED_DELEG_STID:
4566 status = nfserr_bad_stateid;
4569 spin_unlock(&cl->cl_lock);
4574 nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4575 stateid_t *stateid, unsigned char typemask,
4576 struct nfs4_stid **s, struct nfsd_net *nn)
4580 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4581 return nfserr_bad_stateid;
4582 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
4583 if (status == nfserr_stale_clientid) {
4584 if (cstate->session)
4585 return nfserr_bad_stateid;
4586 return nfserr_stale_stateid;
4590 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
4592 return nfserr_bad_stateid;
4596 static struct file *
4597 nfs4_find_file(struct nfs4_stid *s, int flags)
4599 switch (s->sc_type) {
4600 case NFS4_DELEG_STID:
4601 if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
4603 return get_file(s->sc_file->fi_deleg_file);
4604 case NFS4_OPEN_STID:
4605 case NFS4_LOCK_STID:
4606 if (flags & RD_STATE)
4607 return find_readable_file(s->sc_file);
4609 return find_writeable_file(s->sc_file);
4617 nfs4_check_olstateid(struct svc_fh *fhp, struct nfs4_ol_stateid *ols, int flags)
4621 status = nfsd4_check_openowner_confirmed(ols);
4624 return nfs4_check_openmode(ols, flags);
4628 * Checks for stateid operations
4631 nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
4632 stateid_t *stateid, int flags, struct file **filpp)
4634 struct svc_fh *fhp = &cstate->current_fh;
4635 struct inode *ino = d_inode(fhp->fh_dentry);
4636 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4637 struct nfs4_stid *s;
4643 if (grace_disallows_io(net, ino))
4644 return nfserr_grace;
4646 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4647 return check_special_stateids(net, fhp, stateid, flags);
4649 status = nfsd4_lookup_stateid(cstate, stateid,
4650 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
4654 status = check_stateid_generation(stateid, &s->sc_stateid,
4655 nfsd4_has_session(cstate));
4659 switch (s->sc_type) {
4660 case NFS4_DELEG_STID:
4661 status = nfs4_check_delegmode(delegstateid(s), flags);
4663 case NFS4_OPEN_STID:
4664 case NFS4_LOCK_STID:
4665 status = nfs4_check_olstateid(fhp, openlockstateid(s), flags);
4668 status = nfserr_bad_stateid;
4673 status = nfs4_check_fh(fhp, s);
4675 if (!status && filpp) {
4676 *filpp = nfs4_find_file(s, flags);
4678 status = nfserr_serverfault;
4686 * Test if the stateid is valid
4689 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4690 struct nfsd4_test_stateid *test_stateid)
4692 struct nfsd4_test_stateid_id *stateid;
4693 struct nfs4_client *cl = cstate->session->se_client;
4695 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
4696 stateid->ts_id_status =
4697 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
4703 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4704 struct nfsd4_free_stateid *free_stateid)
4706 stateid_t *stateid = &free_stateid->fr_stateid;
4707 struct nfs4_stid *s;
4708 struct nfs4_delegation *dp;
4709 struct nfs4_ol_stateid *stp;
4710 struct nfs4_client *cl = cstate->session->se_client;
4711 __be32 ret = nfserr_bad_stateid;
4713 spin_lock(&cl->cl_lock);
4714 s = find_stateid_locked(cl, stateid);
4717 switch (s->sc_type) {
4718 case NFS4_DELEG_STID:
4719 ret = nfserr_locks_held;
4721 case NFS4_OPEN_STID:
4722 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4725 ret = nfserr_locks_held;
4727 case NFS4_LOCK_STID:
4728 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4731 stp = openlockstateid(s);
4732 ret = nfserr_locks_held;
4733 if (check_for_locks(stp->st_stid.sc_file,
4734 lockowner(stp->st_stateowner)))
4736 WARN_ON(!unhash_lock_stateid(stp));
4737 spin_unlock(&cl->cl_lock);
4741 case NFS4_REVOKED_DELEG_STID:
4742 dp = delegstateid(s);
4743 list_del_init(&dp->dl_recall_lru);
4744 spin_unlock(&cl->cl_lock);
4748 /* Default falls through and returns nfserr_bad_stateid */
4751 spin_unlock(&cl->cl_lock);
4759 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4760 RD_STATE : WR_STATE;
4763 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
4765 struct svc_fh *current_fh = &cstate->current_fh;
4766 struct nfs4_stateowner *sop = stp->st_stateowner;
4769 status = nfsd4_check_seqid(cstate, sop, seqid);
4772 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4773 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
4775 * "Closed" stateid's exist *only* to return
4776 * nfserr_replay_me from the previous step, and
4777 * revoked delegations are kept only for free_stateid.
4779 return nfserr_bad_stateid;
4780 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4783 return nfs4_check_fh(current_fh, &stp->st_stid);
4787 * Checks for sequence id mutating operations.
4790 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4791 stateid_t *stateid, char typemask,
4792 struct nfs4_ol_stateid **stpp,
4793 struct nfsd_net *nn)
4796 struct nfs4_stid *s;
4797 struct nfs4_ol_stateid *stp = NULL;
4799 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4800 seqid, STATEID_VAL(stateid));
4803 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
4806 stp = openlockstateid(s);
4807 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
4809 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
4813 nfs4_put_stid(&stp->st_stid);
4817 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4818 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
4821 struct nfs4_openowner *oo;
4822 struct nfs4_ol_stateid *stp;
4824 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
4825 NFS4_OPEN_STID, &stp, nn);
4828 oo = openowner(stp->st_stateowner);
4829 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4830 nfs4_put_stid(&stp->st_stid);
4831 return nfserr_bad_stateid;
4838 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4839 struct nfsd4_open_confirm *oc)
4842 struct nfs4_openowner *oo;
4843 struct nfs4_ol_stateid *stp;
4844 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4846 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4847 cstate->current_fh.fh_dentry);
4849 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
4853 status = nfs4_preprocess_seqid_op(cstate,
4854 oc->oc_seqid, &oc->oc_req_stateid,
4855 NFS4_OPEN_STID, &stp, nn);
4858 oo = openowner(stp->st_stateowner);
4859 status = nfserr_bad_stateid;
4860 if (oo->oo_flags & NFS4_OO_CONFIRMED)
4862 oo->oo_flags |= NFS4_OO_CONFIRMED;
4863 update_stateid(&stp->st_stid.sc_stateid);
4864 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4865 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
4866 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
4868 nfsd4_client_record_create(oo->oo_owner.so_client);
4871 nfs4_put_stid(&stp->st_stid);
4873 nfsd4_bump_seqid(cstate, status);
4877 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
4879 if (!test_access(access, stp))
4881 nfs4_file_put_access(stp->st_stid.sc_file, access);
4882 clear_access(access, stp);
4885 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4887 switch (to_access) {
4888 case NFS4_SHARE_ACCESS_READ:
4889 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4890 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4892 case NFS4_SHARE_ACCESS_WRITE:
4893 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4894 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4896 case NFS4_SHARE_ACCESS_BOTH:
4904 nfsd4_open_downgrade(struct svc_rqst *rqstp,
4905 struct nfsd4_compound_state *cstate,
4906 struct nfsd4_open_downgrade *od)
4909 struct nfs4_ol_stateid *stp;
4910 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4912 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4913 cstate->current_fh.fh_dentry);
4915 /* We don't yet support WANT bits: */
4916 if (od->od_deleg_want)
4917 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4920 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
4921 &od->od_stateid, &stp, nn);
4924 status = nfserr_inval;
4925 if (!test_access(od->od_share_access, stp)) {
4926 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
4927 stp->st_access_bmap, od->od_share_access);
4930 if (!test_deny(od->od_share_deny, stp)) {
4931 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
4932 stp->st_deny_bmap, od->od_share_deny);
4935 nfs4_stateid_downgrade(stp, od->od_share_access);
4937 reset_union_bmap_deny(od->od_share_deny, stp);
4939 update_stateid(&stp->st_stid.sc_stateid);
4940 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4943 nfs4_put_stid(&stp->st_stid);
4945 nfsd4_bump_seqid(cstate, status);
4949 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4951 struct nfs4_client *clp = s->st_stid.sc_client;
4953 LIST_HEAD(reaplist);
4955 s->st_stid.sc_type = NFS4_CLOSED_STID;
4956 spin_lock(&clp->cl_lock);
4957 unhashed = unhash_open_stateid(s, &reaplist);
4959 if (clp->cl_minorversion) {
4961 put_ol_stateid_locked(s, &reaplist);
4962 spin_unlock(&clp->cl_lock);
4963 free_ol_stateid_reaplist(&reaplist);
4965 spin_unlock(&clp->cl_lock);
4966 free_ol_stateid_reaplist(&reaplist);
4968 move_to_close_lru(s, clp->net);
4973 * nfs4_unlock_state() called after encode
4976 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4977 struct nfsd4_close *close)
4980 struct nfs4_ol_stateid *stp;
4981 struct net *net = SVC_NET(rqstp);
4982 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4984 dprintk("NFSD: nfsd4_close on file %pd\n",
4985 cstate->current_fh.fh_dentry);
4987 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4989 NFS4_OPEN_STID|NFS4_CLOSED_STID,
4991 nfsd4_bump_seqid(cstate, status);
4994 update_stateid(&stp->st_stid.sc_stateid);
4995 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4997 nfsd4_close_open_stateid(stp);
4999 /* put reference from nfs4_preprocess_seqid_op */
5000 nfs4_put_stid(&stp->st_stid);
5006 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5007 struct nfsd4_delegreturn *dr)
5009 struct nfs4_delegation *dp;
5010 stateid_t *stateid = &dr->dr_stateid;
5011 struct nfs4_stid *s;
5013 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5015 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
5018 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
5021 dp = delegstateid(s);
5022 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
5026 destroy_delegation(dp);
5028 nfs4_put_stid(&dp->dl_stid);
5034 #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
5037 end_offset(u64 start, u64 len)
5042 return end >= start ? end: NFS4_MAX_UINT64;
5045 /* last octet in a range */
5047 last_byte_offset(u64 start, u64 len)
5053 return end > start ? end - 1: NFS4_MAX_UINT64;
5057 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
5058 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
5059 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
5060 * locking, this prevents us from being completely protocol-compliant. The
5061 * real solution to this problem is to start using unsigned file offsets in
5062 * the VFS, but this is a very deep change!
5065 nfs4_transform_lock_offset(struct file_lock *lock)
5067 if (lock->fl_start < 0)
5068 lock->fl_start = OFFSET_MAX;
5069 if (lock->fl_end < 0)
5070 lock->fl_end = OFFSET_MAX;
5074 nfsd4_fl_get_owner(fl_owner_t owner)
5076 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
5078 nfs4_get_stateowner(&lo->lo_owner);
5083 nfsd4_fl_put_owner(fl_owner_t owner)
5085 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
5088 nfs4_put_stateowner(&lo->lo_owner);
5091 static const struct lock_manager_operations nfsd_posix_mng_ops = {
5092 .lm_get_owner = nfsd4_fl_get_owner,
5093 .lm_put_owner = nfsd4_fl_put_owner,
5097 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
5099 struct nfs4_lockowner *lo;
5101 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
5102 lo = (struct nfs4_lockowner *) fl->fl_owner;
5103 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
5104 lo->lo_owner.so_owner.len, GFP_KERNEL);
5105 if (!deny->ld_owner.data)
5106 /* We just don't care that much */
5108 deny->ld_owner.len = lo->lo_owner.so_owner.len;
5109 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
5112 deny->ld_owner.len = 0;
5113 deny->ld_owner.data = NULL;
5114 deny->ld_clientid.cl_boot = 0;
5115 deny->ld_clientid.cl_id = 0;
5117 deny->ld_start = fl->fl_start;
5118 deny->ld_length = NFS4_MAX_UINT64;
5119 if (fl->fl_end != NFS4_MAX_UINT64)
5120 deny->ld_length = fl->fl_end - fl->fl_start + 1;
5121 deny->ld_type = NFS4_READ_LT;
5122 if (fl->fl_type != F_RDLCK)
5123 deny->ld_type = NFS4_WRITE_LT;
5126 static struct nfs4_lockowner *
5127 find_lockowner_str_locked(clientid_t *clid, struct xdr_netobj *owner,
5128 struct nfs4_client *clp)
5130 unsigned int strhashval = ownerstr_hashval(owner);
5131 struct nfs4_stateowner *so;
5133 lockdep_assert_held(&clp->cl_lock);
5135 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
5137 if (so->so_is_open_owner)
5139 if (same_owner_str(so, owner))
5140 return lockowner(nfs4_get_stateowner(so));
5145 static struct nfs4_lockowner *
5146 find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
5147 struct nfs4_client *clp)
5149 struct nfs4_lockowner *lo;
5151 spin_lock(&clp->cl_lock);
5152 lo = find_lockowner_str_locked(clid, owner, clp);
5153 spin_unlock(&clp->cl_lock);
5157 static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
5159 unhash_lockowner_locked(lockowner(sop));
5162 static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
5164 struct nfs4_lockowner *lo = lockowner(sop);
5166 kmem_cache_free(lockowner_slab, lo);
5169 static const struct nfs4_stateowner_operations lockowner_ops = {
5170 .so_unhash = nfs4_unhash_lockowner,
5171 .so_free = nfs4_free_lockowner,
5175 * Alloc a lock owner structure.
5176 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
5179 * strhashval = ownerstr_hashval
5181 static struct nfs4_lockowner *
5182 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
5183 struct nfs4_ol_stateid *open_stp,
5184 struct nfsd4_lock *lock)
5186 struct nfs4_lockowner *lo, *ret;
5188 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
5191 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
5192 lo->lo_owner.so_is_open_owner = 0;
5193 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
5194 lo->lo_owner.so_ops = &lockowner_ops;
5195 spin_lock(&clp->cl_lock);
5196 ret = find_lockowner_str_locked(&clp->cl_clientid,
5197 &lock->lk_new_owner, clp);
5199 list_add(&lo->lo_owner.so_strhash,
5200 &clp->cl_ownerstr_hashtbl[strhashval]);
5203 nfs4_free_lockowner(&lo->lo_owner);
5204 spin_unlock(&clp->cl_lock);
5209 init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
5210 struct nfs4_file *fp, struct inode *inode,
5211 struct nfs4_ol_stateid *open_stp)
5213 struct nfs4_client *clp = lo->lo_owner.so_client;
5215 lockdep_assert_held(&clp->cl_lock);
5217 atomic_inc(&stp->st_stid.sc_count);
5218 stp->st_stid.sc_type = NFS4_LOCK_STID;
5219 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
5221 stp->st_stid.sc_file = fp;
5222 stp->st_stid.sc_free = nfs4_free_lock_stateid;
5223 stp->st_access_bmap = 0;
5224 stp->st_deny_bmap = open_stp->st_deny_bmap;
5225 stp->st_openstp = open_stp;
5226 list_add(&stp->st_locks, &open_stp->st_locks);
5227 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
5228 spin_lock(&fp->fi_lock);
5229 list_add(&stp->st_perfile, &fp->fi_stateids);
5230 spin_unlock(&fp->fi_lock);
5233 static struct nfs4_ol_stateid *
5234 find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5236 struct nfs4_ol_stateid *lst;
5237 struct nfs4_client *clp = lo->lo_owner.so_client;
5239 lockdep_assert_held(&clp->cl_lock);
5241 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
5242 if (lst->st_stid.sc_file == fp) {
5243 atomic_inc(&lst->st_stid.sc_count);
5250 static struct nfs4_ol_stateid *
5251 find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5252 struct inode *inode, struct nfs4_ol_stateid *ost,
5255 struct nfs4_stid *ns = NULL;
5256 struct nfs4_ol_stateid *lst;
5257 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5258 struct nfs4_client *clp = oo->oo_owner.so_client;
5260 spin_lock(&clp->cl_lock);
5261 lst = find_lock_stateid(lo, fi);
5263 spin_unlock(&clp->cl_lock);
5264 ns = nfs4_alloc_stid(clp, stateid_slab);
5268 spin_lock(&clp->cl_lock);
5269 lst = find_lock_stateid(lo, fi);
5271 lst = openlockstateid(ns);
5272 init_lock_stateid(lst, lo, fi, inode, ost);
5277 spin_unlock(&clp->cl_lock);
5284 check_lock_length(u64 offset, u64 length)
5286 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
5287 LOFF_OVERFLOW(offset, length)));
5290 static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
5292 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
5294 lockdep_assert_held(&fp->fi_lock);
5296 if (test_access(access, lock_stp))
5298 __nfs4_file_get_access(fp, access);
5299 set_access(access, lock_stp);
5303 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5304 struct nfs4_ol_stateid *ost,
5305 struct nfsd4_lock *lock,
5306 struct nfs4_ol_stateid **lst, bool *new)
5309 struct nfs4_file *fi = ost->st_stid.sc_file;
5310 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5311 struct nfs4_client *cl = oo->oo_owner.so_client;
5312 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
5313 struct nfs4_lockowner *lo;
5314 unsigned int strhashval;
5316 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, cl);
5318 strhashval = ownerstr_hashval(&lock->v.new.owner);
5319 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5321 return nfserr_jukebox;
5323 /* with an existing lockowner, seqids must be the same */
5324 status = nfserr_bad_seqid;
5325 if (!cstate->minorversion &&
5326 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
5330 *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
5332 status = nfserr_jukebox;
5337 nfs4_put_stateowner(&lo->lo_owner);
5345 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5346 struct nfsd4_lock *lock)
5348 struct nfs4_openowner *open_sop = NULL;
5349 struct nfs4_lockowner *lock_sop = NULL;
5350 struct nfs4_ol_stateid *lock_stp = NULL;
5351 struct nfs4_ol_stateid *open_stp = NULL;
5352 struct nfs4_file *fp;
5353 struct file *filp = NULL;
5354 struct file_lock *file_lock = NULL;
5355 struct file_lock *conflock = NULL;
5360 struct net *net = SVC_NET(rqstp);
5361 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5363 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5364 (long long) lock->lk_offset,
5365 (long long) lock->lk_length);
5367 if (check_lock_length(lock->lk_offset, lock->lk_length))
5368 return nfserr_inval;
5370 if ((status = fh_verify(rqstp, &cstate->current_fh,
5371 S_IFREG, NFSD_MAY_LOCK))) {
5372 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5376 if (lock->lk_is_new) {
5377 if (nfsd4_has_session(cstate))
5378 /* See rfc 5661 18.10.3: given clientid is ignored: */
5379 memcpy(&lock->v.new.clientid,
5380 &cstate->session->se_client->cl_clientid,
5381 sizeof(clientid_t));
5383 status = nfserr_stale_clientid;
5384 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
5387 /* validate and update open stateid and open seqid */
5388 status = nfs4_preprocess_confirmed_seqid_op(cstate,
5389 lock->lk_new_open_seqid,
5390 &lock->lk_new_open_stateid,
5394 open_sop = openowner(open_stp->st_stateowner);
5395 status = nfserr_bad_stateid;
5396 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
5397 &lock->v.new.clientid))
5399 status = lookup_or_create_lock_state(cstate, open_stp, lock,
5402 status = nfs4_preprocess_seqid_op(cstate,
5403 lock->lk_old_lock_seqid,
5404 &lock->lk_old_lock_stateid,
5405 NFS4_LOCK_STID, &lock_stp, nn);
5409 lock_sop = lockowner(lock_stp->st_stateowner);
5411 lkflg = setlkflg(lock->lk_type);
5412 status = nfs4_check_openmode(lock_stp, lkflg);
5416 status = nfserr_grace;
5417 if (locks_in_grace(net) && !lock->lk_reclaim)
5419 status = nfserr_no_grace;
5420 if (!locks_in_grace(net) && lock->lk_reclaim)
5423 file_lock = locks_alloc_lock();
5425 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5426 status = nfserr_jukebox;
5430 fp = lock_stp->st_stid.sc_file;
5431 switch (lock->lk_type) {
5434 spin_lock(&fp->fi_lock);
5435 filp = find_readable_file_locked(fp);
5437 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
5438 spin_unlock(&fp->fi_lock);
5439 file_lock->fl_type = F_RDLCK;
5442 case NFS4_WRITEW_LT:
5443 spin_lock(&fp->fi_lock);
5444 filp = find_writeable_file_locked(fp);
5446 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
5447 spin_unlock(&fp->fi_lock);
5448 file_lock->fl_type = F_WRLCK;
5451 status = nfserr_inval;
5455 status = nfserr_openmode;
5459 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
5460 file_lock->fl_pid = current->tgid;
5461 file_lock->fl_file = filp;
5462 file_lock->fl_flags = FL_POSIX;
5463 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5464 file_lock->fl_start = lock->lk_offset;
5465 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5466 nfs4_transform_lock_offset(file_lock);
5468 conflock = locks_alloc_lock();
5470 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5471 status = nfserr_jukebox;
5475 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
5477 case 0: /* success! */
5478 update_stateid(&lock_stp->st_stid.sc_stateid);
5479 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
5483 case (EAGAIN): /* conflock holds conflicting lock */
5484 status = nfserr_denied;
5485 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
5486 nfs4_set_lock_denied(conflock, &lock->lk_denied);
5489 status = nfserr_deadlock;
5492 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
5493 status = nfserrno(err);
5500 /* Bump seqid manually if the 4.0 replay owner is openowner */
5501 if (cstate->replay_owner &&
5502 cstate->replay_owner != &lock_sop->lo_owner &&
5503 seqid_mutating_err(ntohl(status)))
5504 lock_sop->lo_owner.so_seqid++;
5507 * If this is a new, never-before-used stateid, and we are
5508 * returning an error, then just go ahead and release it.
5511 release_lock_stateid(lock_stp);
5513 nfs4_put_stid(&lock_stp->st_stid);
5516 nfs4_put_stid(&open_stp->st_stid);
5517 nfsd4_bump_seqid(cstate, status);
5519 locks_free_lock(file_lock);
5521 locks_free_lock(conflock);
5526 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5527 * so we do a temporary open here just to get an open file to pass to
5528 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5531 static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
5534 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5536 err = nfserrno(vfs_test_lock(file, lock));
5546 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5547 struct nfsd4_lockt *lockt)
5549 struct file_lock *file_lock = NULL;
5550 struct nfs4_lockowner *lo = NULL;
5552 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5554 if (locks_in_grace(SVC_NET(rqstp)))
5555 return nfserr_grace;
5557 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5558 return nfserr_inval;
5560 if (!nfsd4_has_session(cstate)) {
5561 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
5566 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
5569 file_lock = locks_alloc_lock();
5571 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5572 status = nfserr_jukebox;
5576 switch (lockt->lt_type) {
5579 file_lock->fl_type = F_RDLCK;
5582 case NFS4_WRITEW_LT:
5583 file_lock->fl_type = F_WRLCK;
5586 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
5587 status = nfserr_inval;
5591 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner,
5594 file_lock->fl_owner = (fl_owner_t)lo;
5595 file_lock->fl_pid = current->tgid;
5596 file_lock->fl_flags = FL_POSIX;
5598 file_lock->fl_start = lockt->lt_offset;
5599 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
5601 nfs4_transform_lock_offset(file_lock);
5603 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
5607 if (file_lock->fl_type != F_UNLCK) {
5608 status = nfserr_denied;
5609 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
5613 nfs4_put_stateowner(&lo->lo_owner);
5615 locks_free_lock(file_lock);
5620 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5621 struct nfsd4_locku *locku)
5623 struct nfs4_ol_stateid *stp;
5624 struct file *filp = NULL;
5625 struct file_lock *file_lock = NULL;
5628 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5630 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5631 (long long) locku->lu_offset,
5632 (long long) locku->lu_length);
5634 if (check_lock_length(locku->lu_offset, locku->lu_length))
5635 return nfserr_inval;
5637 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
5638 &locku->lu_stateid, NFS4_LOCK_STID,
5642 filp = find_any_file(stp->st_stid.sc_file);
5644 status = nfserr_lock_range;
5647 file_lock = locks_alloc_lock();
5649 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5650 status = nfserr_jukebox;
5654 file_lock->fl_type = F_UNLCK;
5655 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
5656 file_lock->fl_pid = current->tgid;
5657 file_lock->fl_file = filp;
5658 file_lock->fl_flags = FL_POSIX;
5659 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5660 file_lock->fl_start = locku->lu_offset;
5662 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5664 nfs4_transform_lock_offset(file_lock);
5666 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
5668 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
5671 update_stateid(&stp->st_stid.sc_stateid);
5672 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
5676 nfs4_put_stid(&stp->st_stid);
5678 nfsd4_bump_seqid(cstate, status);
5680 locks_free_lock(file_lock);
5684 status = nfserrno(err);
5690 * true: locks held by lockowner
5691 * false: no locks held by lockowner
5694 check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
5696 struct file_lock *fl;
5698 struct file *filp = find_any_file(fp);
5699 struct inode *inode;
5700 struct file_lock_context *flctx;
5703 /* Any valid lock stateid should have some sort of access */
5708 inode = file_inode(filp);
5709 flctx = inode->i_flctx;
5711 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
5712 spin_lock(&flctx->flc_lock);
5713 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
5714 if (fl->fl_owner == (fl_owner_t)lowner) {
5719 spin_unlock(&flctx->flc_lock);
5726 nfsd4_release_lockowner(struct svc_rqst *rqstp,
5727 struct nfsd4_compound_state *cstate,
5728 struct nfsd4_release_lockowner *rlockowner)
5730 clientid_t *clid = &rlockowner->rl_clientid;
5731 struct nfs4_stateowner *sop;
5732 struct nfs4_lockowner *lo = NULL;
5733 struct nfs4_ol_stateid *stp;
5734 struct xdr_netobj *owner = &rlockowner->rl_owner;
5735 unsigned int hashval = ownerstr_hashval(owner);
5737 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5738 struct nfs4_client *clp;
5740 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5741 clid->cl_boot, clid->cl_id);
5743 status = lookup_clientid(clid, cstate, nn);
5748 /* Find the matching lock stateowner */
5749 spin_lock(&clp->cl_lock);
5750 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
5753 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
5756 /* see if there are still any locks associated with it */
5757 lo = lockowner(sop);
5758 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5759 if (check_for_locks(stp->st_stid.sc_file, lo)) {
5760 status = nfserr_locks_held;
5761 spin_unlock(&clp->cl_lock);
5766 nfs4_get_stateowner(sop);
5769 spin_unlock(&clp->cl_lock);
5771 release_lockowner(lo);
5775 static inline struct nfs4_client_reclaim *
5778 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
5782 nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
5784 struct nfs4_client_reclaim *crp;
5786 crp = nfsd4_find_reclaim_client(name, nn);
5787 return (crp && crp->cr_clp);
5791 * failure => all reset bets are off, nfserr_no_grace...
5793 struct nfs4_client_reclaim *
5794 nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
5796 unsigned int strhashval;
5797 struct nfs4_client_reclaim *crp;
5799 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5800 crp = alloc_reclaim();
5802 strhashval = clientstr_hashval(name);
5803 INIT_LIST_HEAD(&crp->cr_strhash);
5804 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
5805 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
5807 nn->reclaim_str_hashtbl_size++;
5813 nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
5815 list_del(&crp->cr_strhash);
5817 nn->reclaim_str_hashtbl_size--;
5821 nfs4_release_reclaim(struct nfsd_net *nn)
5823 struct nfs4_client_reclaim *crp = NULL;
5826 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5827 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5828 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
5829 struct nfs4_client_reclaim, cr_strhash);
5830 nfs4_remove_reclaim_record(crp, nn);
5833 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
5837 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
5838 struct nfs4_client_reclaim *
5839 nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
5841 unsigned int strhashval;
5842 struct nfs4_client_reclaim *crp = NULL;
5844 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
5846 strhashval = clientstr_hashval(recdir);
5847 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
5848 if (same_name(crp->cr_recdir, recdir)) {
5856 * Called from OPEN. Look for clientid in reclaim list.
5859 nfs4_check_open_reclaim(clientid_t *clid,
5860 struct nfsd4_compound_state *cstate,
5861 struct nfsd_net *nn)
5865 /* find clientid in conf_id_hashtbl */
5866 status = lookup_clientid(clid, cstate, nn);
5868 return nfserr_reclaim_bad;
5870 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
5871 return nfserr_no_grace;
5873 if (nfsd4_client_record_check(cstate->clp))
5874 return nfserr_reclaim_bad;
5879 #ifdef CONFIG_NFSD_FAULT_INJECTION
5881 put_client(struct nfs4_client *clp)
5883 atomic_dec(&clp->cl_refcount);
5886 static struct nfs4_client *
5887 nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5889 struct nfs4_client *clp;
5890 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5893 if (!nfsd_netns_ready(nn))
5896 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5897 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5904 nfsd_inject_print_clients(void)
5906 struct nfs4_client *clp;
5908 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5910 char buf[INET6_ADDRSTRLEN];
5912 if (!nfsd_netns_ready(nn))
5915 spin_lock(&nn->client_lock);
5916 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5917 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5918 pr_info("NFS Client: %s\n", buf);
5921 spin_unlock(&nn->client_lock);
5927 nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
5930 struct nfs4_client *clp;
5931 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5934 if (!nfsd_netns_ready(nn))
5937 spin_lock(&nn->client_lock);
5938 clp = nfsd_find_client(addr, addr_size);
5940 if (mark_client_expired_locked(clp) == nfs_ok)
5945 spin_unlock(&nn->client_lock);
5954 nfsd_inject_forget_clients(u64 max)
5957 struct nfs4_client *clp, *next;
5958 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5960 LIST_HEAD(reaplist);
5962 if (!nfsd_netns_ready(nn))
5965 spin_lock(&nn->client_lock);
5966 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
5967 if (mark_client_expired_locked(clp) == nfs_ok) {
5968 list_add(&clp->cl_lru, &reaplist);
5969 if (max != 0 && ++count >= max)
5973 spin_unlock(&nn->client_lock);
5975 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
5981 static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5984 char buf[INET6_ADDRSTRLEN];
5985 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5986 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5990 nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
5991 struct list_head *collect)
5993 struct nfs4_client *clp = lst->st_stid.sc_client;
5994 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6000 lockdep_assert_held(&nn->client_lock);
6001 atomic_inc(&clp->cl_refcount);
6002 list_add(&lst->st_locks, collect);
6005 static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
6006 struct list_head *collect,
6007 bool (*func)(struct nfs4_ol_stateid *))
6009 struct nfs4_openowner *oop;
6010 struct nfs4_ol_stateid *stp, *st_next;
6011 struct nfs4_ol_stateid *lst, *lst_next;
6014 spin_lock(&clp->cl_lock);
6015 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
6016 list_for_each_entry_safe(stp, st_next,
6017 &oop->oo_owner.so_stateids, st_perstateowner) {
6018 list_for_each_entry_safe(lst, lst_next,
6019 &stp->st_locks, st_locks) {
6022 nfsd_inject_add_lock_to_list(lst,
6027 * Despite the fact that these functions deal
6028 * with 64-bit integers for "count", we must
6029 * ensure that it doesn't blow up the
6030 * clp->cl_refcount. Throw a warning if we
6031 * start to approach INT_MAX here.
6033 WARN_ON_ONCE(count == (INT_MAX / 2));
6040 spin_unlock(&clp->cl_lock);
6046 nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
6049 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
6053 nfsd_print_client_locks(struct nfs4_client *clp)
6055 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
6056 nfsd_print_count(clp, count, "locked files");
6061 nfsd_inject_print_locks(void)
6063 struct nfs4_client *clp;
6065 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6068 if (!nfsd_netns_ready(nn))
6071 spin_lock(&nn->client_lock);
6072 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6073 count += nfsd_print_client_locks(clp);
6074 spin_unlock(&nn->client_lock);
6080 nfsd_reap_locks(struct list_head *reaplist)
6082 struct nfs4_client *clp;
6083 struct nfs4_ol_stateid *stp, *next;
6085 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
6086 list_del_init(&stp->st_locks);
6087 clp = stp->st_stid.sc_client;
6088 nfs4_put_stid(&stp->st_stid);
6094 nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
6096 unsigned int count = 0;
6097 struct nfs4_client *clp;
6098 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6100 LIST_HEAD(reaplist);
6102 if (!nfsd_netns_ready(nn))
6105 spin_lock(&nn->client_lock);
6106 clp = nfsd_find_client(addr, addr_size);
6108 count = nfsd_collect_client_locks(clp, &reaplist, 0);
6109 spin_unlock(&nn->client_lock);
6110 nfsd_reap_locks(&reaplist);
6115 nfsd_inject_forget_locks(u64 max)
6118 struct nfs4_client *clp;
6119 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6121 LIST_HEAD(reaplist);
6123 if (!nfsd_netns_ready(nn))
6126 spin_lock(&nn->client_lock);
6127 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6128 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
6129 if (max != 0 && count >= max)
6132 spin_unlock(&nn->client_lock);
6133 nfsd_reap_locks(&reaplist);
6138 nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
6139 struct list_head *collect,
6140 void (*func)(struct nfs4_openowner *))
6142 struct nfs4_openowner *oop, *next;
6143 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6147 lockdep_assert_held(&nn->client_lock);
6149 spin_lock(&clp->cl_lock);
6150 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
6154 atomic_inc(&clp->cl_refcount);
6155 list_add(&oop->oo_perclient, collect);
6160 * Despite the fact that these functions deal with
6161 * 64-bit integers for "count", we must ensure that
6162 * it doesn't blow up the clp->cl_refcount. Throw a
6163 * warning if we start to approach INT_MAX here.
6165 WARN_ON_ONCE(count == (INT_MAX / 2));
6169 spin_unlock(&clp->cl_lock);
6175 nfsd_print_client_openowners(struct nfs4_client *clp)
6177 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
6179 nfsd_print_count(clp, count, "openowners");
6184 nfsd_collect_client_openowners(struct nfs4_client *clp,
6185 struct list_head *collect, u64 max)
6187 return nfsd_foreach_client_openowner(clp, max, collect,
6188 unhash_openowner_locked);
6192 nfsd_inject_print_openowners(void)
6194 struct nfs4_client *clp;
6196 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6199 if (!nfsd_netns_ready(nn))
6202 spin_lock(&nn->client_lock);
6203 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6204 count += nfsd_print_client_openowners(clp);
6205 spin_unlock(&nn->client_lock);
6211 nfsd_reap_openowners(struct list_head *reaplist)
6213 struct nfs4_client *clp;
6214 struct nfs4_openowner *oop, *next;
6216 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
6217 list_del_init(&oop->oo_perclient);
6218 clp = oop->oo_owner.so_client;
6219 release_openowner(oop);
6225 nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
6228 unsigned int count = 0;
6229 struct nfs4_client *clp;
6230 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6232 LIST_HEAD(reaplist);
6234 if (!nfsd_netns_ready(nn))
6237 spin_lock(&nn->client_lock);
6238 clp = nfsd_find_client(addr, addr_size);
6240 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
6241 spin_unlock(&nn->client_lock);
6242 nfsd_reap_openowners(&reaplist);
6247 nfsd_inject_forget_openowners(u64 max)
6250 struct nfs4_client *clp;
6251 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6253 LIST_HEAD(reaplist);
6255 if (!nfsd_netns_ready(nn))
6258 spin_lock(&nn->client_lock);
6259 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6260 count += nfsd_collect_client_openowners(clp, &reaplist,
6262 if (max != 0 && count >= max)
6265 spin_unlock(&nn->client_lock);
6266 nfsd_reap_openowners(&reaplist);
6270 static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
6271 struct list_head *victims)
6273 struct nfs4_delegation *dp, *next;
6274 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6278 lockdep_assert_held(&nn->client_lock);
6280 spin_lock(&state_lock);
6281 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
6284 * It's not safe to mess with delegations that have a
6285 * non-zero dl_time. They might have already been broken
6286 * and could be processed by the laundromat outside of
6287 * the state_lock. Just leave them be.
6289 if (dp->dl_time != 0)
6292 atomic_inc(&clp->cl_refcount);
6293 WARN_ON(!unhash_delegation_locked(dp));
6294 list_add(&dp->dl_recall_lru, victims);
6298 * Despite the fact that these functions deal with
6299 * 64-bit integers for "count", we must ensure that
6300 * it doesn't blow up the clp->cl_refcount. Throw a
6301 * warning if we start to approach INT_MAX here.
6303 WARN_ON_ONCE(count == (INT_MAX / 2));
6307 spin_unlock(&state_lock);
6312 nfsd_print_client_delegations(struct nfs4_client *clp)
6314 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
6316 nfsd_print_count(clp, count, "delegations");
6321 nfsd_inject_print_delegations(void)
6323 struct nfs4_client *clp;
6325 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6328 if (!nfsd_netns_ready(nn))
6331 spin_lock(&nn->client_lock);
6332 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6333 count += nfsd_print_client_delegations(clp);
6334 spin_unlock(&nn->client_lock);
6340 nfsd_forget_delegations(struct list_head *reaplist)
6342 struct nfs4_client *clp;
6343 struct nfs4_delegation *dp, *next;
6345 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6346 list_del_init(&dp->dl_recall_lru);
6347 clp = dp->dl_stid.sc_client;
6348 revoke_delegation(dp);
6354 nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
6358 struct nfs4_client *clp;
6359 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6361 LIST_HEAD(reaplist);
6363 if (!nfsd_netns_ready(nn))
6366 spin_lock(&nn->client_lock);
6367 clp = nfsd_find_client(addr, addr_size);
6369 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6370 spin_unlock(&nn->client_lock);
6372 nfsd_forget_delegations(&reaplist);
6377 nfsd_inject_forget_delegations(u64 max)
6380 struct nfs4_client *clp;
6381 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6383 LIST_HEAD(reaplist);
6385 if (!nfsd_netns_ready(nn))
6388 spin_lock(&nn->client_lock);
6389 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6390 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6391 if (max != 0 && count >= max)
6394 spin_unlock(&nn->client_lock);
6395 nfsd_forget_delegations(&reaplist);
6400 nfsd_recall_delegations(struct list_head *reaplist)
6402 struct nfs4_client *clp;
6403 struct nfs4_delegation *dp, *next;
6405 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6406 list_del_init(&dp->dl_recall_lru);
6407 clp = dp->dl_stid.sc_client;
6409 * We skipped all entries that had a zero dl_time before,
6410 * so we can now reset the dl_time back to 0. If a delegation
6411 * break comes in now, then it won't make any difference since
6412 * we're recalling it either way.
6414 spin_lock(&state_lock);
6416 spin_unlock(&state_lock);
6417 nfsd_break_one_deleg(dp);
6423 nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
6427 struct nfs4_client *clp;
6428 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6430 LIST_HEAD(reaplist);
6432 if (!nfsd_netns_ready(nn))
6435 spin_lock(&nn->client_lock);
6436 clp = nfsd_find_client(addr, addr_size);
6438 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6439 spin_unlock(&nn->client_lock);
6441 nfsd_recall_delegations(&reaplist);
6446 nfsd_inject_recall_delegations(u64 max)
6449 struct nfs4_client *clp, *next;
6450 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6452 LIST_HEAD(reaplist);
6454 if (!nfsd_netns_ready(nn))
6457 spin_lock(&nn->client_lock);
6458 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6459 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6460 if (max != 0 && ++count >= max)
6463 spin_unlock(&nn->client_lock);
6464 nfsd_recall_delegations(&reaplist);
6467 #endif /* CONFIG_NFSD_FAULT_INJECTION */
6470 * Since the lifetime of a delegation isn't limited to that of an open, a
6471 * client may quite reasonably hang on to a delegation as long as it has
6472 * the inode cached. This becomes an obvious problem the first time a
6473 * client's inode cache approaches the size of the server's total memory.
6475 * For now we avoid this problem by imposing a hard limit on the number
6476 * of delegations, which varies according to the server's memory size.
6479 set_max_delegations(void)
6482 * Allow at most 4 delegations per megabyte of RAM. Quick
6483 * estimates suggest that in the worst case (where every delegation
6484 * is for a different inode), a delegation could take about 1.5K,
6485 * giving a worst case usage of about 6% of memory.
6487 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
6490 static int nfs4_state_create_net(struct net *net)
6492 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6495 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6496 CLIENT_HASH_SIZE, GFP_KERNEL);
6497 if (!nn->conf_id_hashtbl)
6499 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6500 CLIENT_HASH_SIZE, GFP_KERNEL);
6501 if (!nn->unconf_id_hashtbl)
6503 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
6504 SESSION_HASH_SIZE, GFP_KERNEL);
6505 if (!nn->sessionid_hashtbl)
6508 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6509 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
6510 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
6512 for (i = 0; i < SESSION_HASH_SIZE; i++)
6513 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
6514 nn->conf_name_tree = RB_ROOT;
6515 nn->unconf_name_tree = RB_ROOT;
6516 INIT_LIST_HEAD(&nn->client_lru);
6517 INIT_LIST_HEAD(&nn->close_lru);
6518 INIT_LIST_HEAD(&nn->del_recall_lru);
6519 spin_lock_init(&nn->client_lock);
6521 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
6527 kfree(nn->unconf_id_hashtbl);
6529 kfree(nn->conf_id_hashtbl);
6535 nfs4_state_destroy_net(struct net *net)
6538 struct nfs4_client *clp = NULL;
6539 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6541 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6542 while (!list_empty(&nn->conf_id_hashtbl[i])) {
6543 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6544 destroy_client(clp);
6548 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6549 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
6550 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6551 destroy_client(clp);
6555 kfree(nn->sessionid_hashtbl);
6556 kfree(nn->unconf_id_hashtbl);
6557 kfree(nn->conf_id_hashtbl);
6562 nfs4_state_start_net(struct net *net)
6564 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6567 ret = nfs4_state_create_net(net);
6570 nn->boot_time = get_seconds();
6571 nn->grace_ended = false;
6572 locks_start_grace(net, &nn->nfsd4_manager);
6573 nfsd4_client_tracking_init(net);
6574 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
6575 nn->nfsd4_grace, net);
6576 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
6580 /* initialization to perform when the nfsd service is started: */
6583 nfs4_state_start(void)
6587 ret = set_callback_cred();
6590 laundry_wq = create_singlethread_workqueue("nfsd4");
6591 if (laundry_wq == NULL) {
6595 ret = nfsd4_create_callback_queue();
6597 goto out_free_laundry;
6599 set_max_delegations();
6604 destroy_workqueue(laundry_wq);
6610 nfs4_state_shutdown_net(struct net *net)
6612 struct nfs4_delegation *dp = NULL;
6613 struct list_head *pos, *next, reaplist;
6614 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6616 cancel_delayed_work_sync(&nn->laundromat_work);
6617 locks_end_grace(&nn->nfsd4_manager);
6619 INIT_LIST_HEAD(&reaplist);
6620 spin_lock(&state_lock);
6621 list_for_each_safe(pos, next, &nn->del_recall_lru) {
6622 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
6623 WARN_ON(!unhash_delegation_locked(dp));
6624 list_add(&dp->dl_recall_lru, &reaplist);
6626 spin_unlock(&state_lock);
6627 list_for_each_safe(pos, next, &reaplist) {
6628 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
6629 list_del_init(&dp->dl_recall_lru);
6630 put_clnt_odstate(dp->dl_clnt_odstate);
6631 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
6632 nfs4_put_stid(&dp->dl_stid);
6635 nfsd4_client_tracking_exit(net);
6636 nfs4_state_destroy_net(net);
6640 nfs4_state_shutdown(void)
6642 destroy_workqueue(laundry_wq);
6643 nfsd4_destroy_callback_queue();
6647 get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6649 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
6650 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
6654 put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6656 if (cstate->minorversion) {
6657 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
6658 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6663 clear_current_stateid(struct nfsd4_compound_state *cstate)
6665 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6669 * functions to set current state id
6672 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6674 put_stateid(cstate, &odp->od_stateid);
6678 nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
6680 put_stateid(cstate, &open->op_stateid);
6684 nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6686 put_stateid(cstate, &close->cl_stateid);
6690 nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
6692 put_stateid(cstate, &lock->lk_resp_stateid);
6696 * functions to consume current state id
6700 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6702 get_stateid(cstate, &odp->od_stateid);
6706 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
6708 get_stateid(cstate, &drp->dr_stateid);
6712 nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
6714 get_stateid(cstate, &fsp->fr_stateid);
6718 nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
6720 get_stateid(cstate, &setattr->sa_stateid);
6724 nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6726 get_stateid(cstate, &close->cl_stateid);
6730 nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
6732 get_stateid(cstate, &locku->lu_stateid);
6736 nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
6738 get_stateid(cstate, &read->rd_stateid);
6742 nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
6744 get_stateid(cstate, &write->wr_stateid);