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 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
788 /* Ensure that deleg break won't try to requeue it */
790 spin_lock(&fp->fi_lock);
791 list_del_init(&dp->dl_perclnt);
792 list_del_init(&dp->dl_recall_lru);
793 list_del_init(&dp->dl_perfile);
794 spin_unlock(&fp->fi_lock);
797 static void destroy_delegation(struct nfs4_delegation *dp)
799 spin_lock(&state_lock);
800 unhash_delegation_locked(dp);
801 spin_unlock(&state_lock);
802 put_clnt_odstate(dp->dl_clnt_odstate);
803 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
804 nfs4_put_stid(&dp->dl_stid);
807 static void revoke_delegation(struct nfs4_delegation *dp)
809 struct nfs4_client *clp = dp->dl_stid.sc_client;
811 WARN_ON(!list_empty(&dp->dl_recall_lru));
813 put_clnt_odstate(dp->dl_clnt_odstate);
814 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
816 if (clp->cl_minorversion == 0)
817 nfs4_put_stid(&dp->dl_stid);
819 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
820 spin_lock(&clp->cl_lock);
821 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
822 spin_unlock(&clp->cl_lock);
830 static unsigned int clientid_hashval(u32 id)
832 return id & CLIENT_HASH_MASK;
835 static unsigned int clientstr_hashval(const char *name)
837 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
841 * We store the NONE, READ, WRITE, and BOTH bits separately in the
842 * st_{access,deny}_bmap field of the stateid, in order to track not
843 * only what share bits are currently in force, but also what
844 * combinations of share bits previous opens have used. This allows us
845 * to enforce the recommendation of rfc 3530 14.2.19 that the server
846 * return an error if the client attempt to downgrade to a combination
847 * of share bits not explicable by closing some of its previous opens.
849 * XXX: This enforcement is actually incomplete, since we don't keep
850 * track of access/deny bit combinations; so, e.g., we allow:
852 * OPEN allow read, deny write
853 * OPEN allow both, deny none
854 * DOWNGRADE allow read, deny none
856 * which we should reject.
859 bmap_to_share_mode(unsigned long bmap) {
861 unsigned int access = 0;
863 for (i = 1; i < 4; i++) {
864 if (test_bit(i, &bmap))
870 /* set share access for a given stateid */
872 set_access(u32 access, struct nfs4_ol_stateid *stp)
874 unsigned char mask = 1 << access;
876 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
877 stp->st_access_bmap |= mask;
880 /* clear share access for a given stateid */
882 clear_access(u32 access, struct nfs4_ol_stateid *stp)
884 unsigned char mask = 1 << access;
886 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
887 stp->st_access_bmap &= ~mask;
890 /* test whether a given stateid has access */
892 test_access(u32 access, struct nfs4_ol_stateid *stp)
894 unsigned char mask = 1 << access;
896 return (bool)(stp->st_access_bmap & mask);
899 /* set share deny for a given stateid */
901 set_deny(u32 deny, struct nfs4_ol_stateid *stp)
903 unsigned char mask = 1 << deny;
905 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
906 stp->st_deny_bmap |= mask;
909 /* clear share deny for a given stateid */
911 clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
913 unsigned char mask = 1 << deny;
915 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
916 stp->st_deny_bmap &= ~mask;
919 /* test whether a given stateid is denying specific access */
921 test_deny(u32 deny, struct nfs4_ol_stateid *stp)
923 unsigned char mask = 1 << deny;
925 return (bool)(stp->st_deny_bmap & mask);
928 static int nfs4_access_to_omode(u32 access)
930 switch (access & NFS4_SHARE_ACCESS_BOTH) {
931 case NFS4_SHARE_ACCESS_READ:
933 case NFS4_SHARE_ACCESS_WRITE:
935 case NFS4_SHARE_ACCESS_BOTH:
943 * A stateid that had a deny mode associated with it is being released
944 * or downgraded. Recalculate the deny mode on the file.
947 recalculate_deny_mode(struct nfs4_file *fp)
949 struct nfs4_ol_stateid *stp;
951 spin_lock(&fp->fi_lock);
952 fp->fi_share_deny = 0;
953 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
954 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
955 spin_unlock(&fp->fi_lock);
959 reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
964 for (i = 1; i < 4; i++) {
965 if ((i & deny) != i) {
971 /* Recalculate per-file deny mode if there was a change */
973 recalculate_deny_mode(stp->st_stid.sc_file);
976 /* release all access and file references for a given stateid */
978 release_all_access(struct nfs4_ol_stateid *stp)
981 struct nfs4_file *fp = stp->st_stid.sc_file;
983 if (fp && stp->st_deny_bmap != 0)
984 recalculate_deny_mode(fp);
986 for (i = 1; i < 4; i++) {
987 if (test_access(i, stp))
988 nfs4_file_put_access(stp->st_stid.sc_file, i);
989 clear_access(i, stp);
993 static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
995 struct nfs4_client *clp = sop->so_client;
997 might_lock(&clp->cl_lock);
999 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
1001 sop->so_ops->so_unhash(sop);
1002 spin_unlock(&clp->cl_lock);
1003 kfree(sop->so_owner.data);
1004 sop->so_ops->so_free(sop);
1007 static void unhash_ol_stateid(struct nfs4_ol_stateid *stp)
1009 struct nfs4_file *fp = stp->st_stid.sc_file;
1011 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1013 spin_lock(&fp->fi_lock);
1014 list_del(&stp->st_perfile);
1015 spin_unlock(&fp->fi_lock);
1016 list_del(&stp->st_perstateowner);
1019 static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
1021 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1023 put_clnt_odstate(stp->st_clnt_odstate);
1024 release_all_access(stp);
1025 if (stp->st_stateowner)
1026 nfs4_put_stateowner(stp->st_stateowner);
1027 kmem_cache_free(stateid_slab, stid);
1030 static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
1032 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1033 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
1036 file = find_any_file(stp->st_stid.sc_file);
1038 filp_close(file, (fl_owner_t)lo);
1039 nfs4_free_ol_stateid(stid);
1043 * Put the persistent reference to an already unhashed generic stateid, while
1044 * holding the cl_lock. If it's the last reference, then put it onto the
1045 * reaplist for later destruction.
1047 static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1048 struct list_head *reaplist)
1050 struct nfs4_stid *s = &stp->st_stid;
1051 struct nfs4_client *clp = s->sc_client;
1053 lockdep_assert_held(&clp->cl_lock);
1055 WARN_ON_ONCE(!list_empty(&stp->st_locks));
1057 if (!atomic_dec_and_test(&s->sc_count)) {
1058 wake_up_all(&close_wq);
1062 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1063 list_add(&stp->st_locks, reaplist);
1066 static void unhash_lock_stateid(struct nfs4_ol_stateid *stp)
1068 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
1070 lockdep_assert_held(&oo->oo_owner.so_client->cl_lock);
1072 list_del_init(&stp->st_locks);
1073 unhash_ol_stateid(stp);
1074 nfs4_unhash_stid(&stp->st_stid);
1077 static void release_lock_stateid(struct nfs4_ol_stateid *stp)
1079 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
1081 spin_lock(&oo->oo_owner.so_client->cl_lock);
1082 unhash_lock_stateid(stp);
1083 spin_unlock(&oo->oo_owner.so_client->cl_lock);
1084 nfs4_put_stid(&stp->st_stid);
1087 static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
1089 struct nfs4_client *clp = lo->lo_owner.so_client;
1091 lockdep_assert_held(&clp->cl_lock);
1093 list_del_init(&lo->lo_owner.so_strhash);
1097 * Free a list of generic stateids that were collected earlier after being
1101 free_ol_stateid_reaplist(struct list_head *reaplist)
1103 struct nfs4_ol_stateid *stp;
1104 struct nfs4_file *fp;
1108 while (!list_empty(reaplist)) {
1109 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1111 list_del(&stp->st_locks);
1112 fp = stp->st_stid.sc_file;
1113 stp->st_stid.sc_free(&stp->st_stid);
1119 static void release_lockowner(struct nfs4_lockowner *lo)
1121 struct nfs4_client *clp = lo->lo_owner.so_client;
1122 struct nfs4_ol_stateid *stp;
1123 struct list_head reaplist;
1125 INIT_LIST_HEAD(&reaplist);
1127 spin_lock(&clp->cl_lock);
1128 unhash_lockowner_locked(lo);
1129 while (!list_empty(&lo->lo_owner.so_stateids)) {
1130 stp = list_first_entry(&lo->lo_owner.so_stateids,
1131 struct nfs4_ol_stateid, st_perstateowner);
1132 unhash_lock_stateid(stp);
1133 put_ol_stateid_locked(stp, &reaplist);
1135 spin_unlock(&clp->cl_lock);
1136 free_ol_stateid_reaplist(&reaplist);
1137 nfs4_put_stateowner(&lo->lo_owner);
1140 static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1141 struct list_head *reaplist)
1143 struct nfs4_ol_stateid *stp;
1145 while (!list_empty(&open_stp->st_locks)) {
1146 stp = list_entry(open_stp->st_locks.next,
1147 struct nfs4_ol_stateid, st_locks);
1148 unhash_lock_stateid(stp);
1149 put_ol_stateid_locked(stp, reaplist);
1153 static void unhash_open_stateid(struct nfs4_ol_stateid *stp,
1154 struct list_head *reaplist)
1156 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1158 unhash_ol_stateid(stp);
1159 release_open_stateid_locks(stp, reaplist);
1162 static void release_open_stateid(struct nfs4_ol_stateid *stp)
1164 LIST_HEAD(reaplist);
1166 spin_lock(&stp->st_stid.sc_client->cl_lock);
1167 unhash_open_stateid(stp, &reaplist);
1168 put_ol_stateid_locked(stp, &reaplist);
1169 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1170 free_ol_stateid_reaplist(&reaplist);
1173 static void unhash_openowner_locked(struct nfs4_openowner *oo)
1175 struct nfs4_client *clp = oo->oo_owner.so_client;
1177 lockdep_assert_held(&clp->cl_lock);
1179 list_del_init(&oo->oo_owner.so_strhash);
1180 list_del_init(&oo->oo_perclient);
1183 static void release_last_closed_stateid(struct nfs4_openowner *oo)
1185 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1187 struct nfs4_ol_stateid *s;
1189 spin_lock(&nn->client_lock);
1190 s = oo->oo_last_closed_stid;
1192 list_del_init(&oo->oo_close_lru);
1193 oo->oo_last_closed_stid = NULL;
1195 spin_unlock(&nn->client_lock);
1197 nfs4_put_stid(&s->st_stid);
1200 static void release_openowner(struct nfs4_openowner *oo)
1202 struct nfs4_ol_stateid *stp;
1203 struct nfs4_client *clp = oo->oo_owner.so_client;
1204 struct list_head reaplist;
1206 INIT_LIST_HEAD(&reaplist);
1208 spin_lock(&clp->cl_lock);
1209 unhash_openowner_locked(oo);
1210 while (!list_empty(&oo->oo_owner.so_stateids)) {
1211 stp = list_first_entry(&oo->oo_owner.so_stateids,
1212 struct nfs4_ol_stateid, st_perstateowner);
1213 unhash_open_stateid(stp, &reaplist);
1214 put_ol_stateid_locked(stp, &reaplist);
1216 spin_unlock(&clp->cl_lock);
1217 free_ol_stateid_reaplist(&reaplist);
1218 release_last_closed_stateid(oo);
1219 nfs4_put_stateowner(&oo->oo_owner);
1223 hash_sessionid(struct nfs4_sessionid *sessionid)
1225 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1227 return sid->sequence % SESSION_HASH_SIZE;
1230 #ifdef CONFIG_SUNRPC_DEBUG
1232 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1234 u32 *ptr = (u32 *)(&sessionid->data[0]);
1235 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1239 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1245 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1246 * won't be used for replay.
1248 void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1250 struct nfs4_stateowner *so = cstate->replay_owner;
1252 if (nfserr == nfserr_replay_me)
1255 if (!seqid_mutating_err(ntohl(nfserr))) {
1256 nfsd4_cstate_clear_replay(cstate);
1261 if (so->so_is_open_owner)
1262 release_last_closed_stateid(openowner(so));
1268 gen_sessionid(struct nfsd4_session *ses)
1270 struct nfs4_client *clp = ses->se_client;
1271 struct nfsd4_sessionid *sid;
1273 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1274 sid->clientid = clp->cl_clientid;
1275 sid->sequence = current_sessionid++;
1280 * The protocol defines ca_maxresponssize_cached to include the size of
1281 * the rpc header, but all we need to cache is the data starting after
1282 * the end of the initial SEQUENCE operation--the rest we regenerate
1283 * each time. Therefore we can advertise a ca_maxresponssize_cached
1284 * value that is the number of bytes in our cache plus a few additional
1285 * bytes. In order to stay on the safe side, and not promise more than
1286 * we can cache, those additional bytes must be the minimum possible: 24
1287 * bytes of rpc header (xid through accept state, with AUTH_NULL
1288 * verifier), 12 for the compound header (with zero-length tag), and 44
1289 * for the SEQUENCE op response:
1291 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1294 free_session_slots(struct nfsd4_session *ses)
1298 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1299 kfree(ses->se_slots[i]);
1303 * We don't actually need to cache the rpc and session headers, so we
1304 * can allocate a little less for each slot:
1306 static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
1310 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1313 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1314 return size + sizeof(struct nfsd4_slot);
1318 * XXX: If we run out of reserved DRC memory we could (up to a point)
1319 * re-negotiate active sessions and reduce their slot usage to make
1320 * room for new connections. For now we just fail the create session.
1322 static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
1324 u32 slotsize = slot_bytes(ca);
1325 u32 num = ca->maxreqs;
1328 spin_lock(&nfsd_drc_lock);
1329 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1330 nfsd_drc_max_mem - nfsd_drc_mem_used);
1331 num = min_t(int, num, avail / slotsize);
1332 nfsd_drc_mem_used += num * slotsize;
1333 spin_unlock(&nfsd_drc_lock);
1338 static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
1340 int slotsize = slot_bytes(ca);
1342 spin_lock(&nfsd_drc_lock);
1343 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
1344 spin_unlock(&nfsd_drc_lock);
1347 static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1348 struct nfsd4_channel_attrs *battrs)
1350 int numslots = fattrs->maxreqs;
1351 int slotsize = slot_bytes(fattrs);
1352 struct nfsd4_session *new;
1355 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
1356 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1357 mem = numslots * sizeof(struct nfsd4_slot *);
1359 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
1362 /* allocate each struct nfsd4_slot and data cache in one piece */
1363 for (i = 0; i < numslots; i++) {
1364 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
1365 if (!new->se_slots[i])
1369 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1370 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1375 kfree(new->se_slots[i]);
1380 static void free_conn(struct nfsd4_conn *c)
1382 svc_xprt_put(c->cn_xprt);
1386 static void nfsd4_conn_lost(struct svc_xpt_user *u)
1388 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1389 struct nfs4_client *clp = c->cn_session->se_client;
1391 spin_lock(&clp->cl_lock);
1392 if (!list_empty(&c->cn_persession)) {
1393 list_del(&c->cn_persession);
1396 nfsd4_probe_callback(clp);
1397 spin_unlock(&clp->cl_lock);
1400 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
1402 struct nfsd4_conn *conn;
1404 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1407 svc_xprt_get(rqstp->rq_xprt);
1408 conn->cn_xprt = rqstp->rq_xprt;
1409 conn->cn_flags = flags;
1410 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1414 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1416 conn->cn_session = ses;
1417 list_add(&conn->cn_persession, &ses->se_conns);
1420 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1422 struct nfs4_client *clp = ses->se_client;
1424 spin_lock(&clp->cl_lock);
1425 __nfsd4_hash_conn(conn, ses);
1426 spin_unlock(&clp->cl_lock);
1429 static int nfsd4_register_conn(struct nfsd4_conn *conn)
1431 conn->cn_xpt_user.callback = nfsd4_conn_lost;
1432 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
1435 static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
1439 nfsd4_hash_conn(conn, ses);
1440 ret = nfsd4_register_conn(conn);
1442 /* oops; xprt is already down: */
1443 nfsd4_conn_lost(&conn->cn_xpt_user);
1444 /* We may have gained or lost a callback channel: */
1445 nfsd4_probe_callback_sync(ses->se_client);
1448 static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
1450 u32 dir = NFS4_CDFC4_FORE;
1452 if (cses->flags & SESSION4_BACK_CHAN)
1453 dir |= NFS4_CDFC4_BACK;
1454 return alloc_conn(rqstp, dir);
1457 /* must be called under client_lock */
1458 static void nfsd4_del_conns(struct nfsd4_session *s)
1460 struct nfs4_client *clp = s->se_client;
1461 struct nfsd4_conn *c;
1463 spin_lock(&clp->cl_lock);
1464 while (!list_empty(&s->se_conns)) {
1465 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1466 list_del_init(&c->cn_persession);
1467 spin_unlock(&clp->cl_lock);
1469 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1472 spin_lock(&clp->cl_lock);
1474 spin_unlock(&clp->cl_lock);
1477 static void __free_session(struct nfsd4_session *ses)
1479 free_session_slots(ses);
1483 static void free_session(struct nfsd4_session *ses)
1485 nfsd4_del_conns(ses);
1486 nfsd4_put_drc_mem(&ses->se_fchannel);
1487 __free_session(ses);
1490 static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
1493 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1495 new->se_client = clp;
1498 INIT_LIST_HEAD(&new->se_conns);
1500 new->se_cb_seq_nr = 1;
1501 new->se_flags = cses->flags;
1502 new->se_cb_prog = cses->callback_prog;
1503 new->se_cb_sec = cses->cb_sec;
1504 atomic_set(&new->se_ref, 0);
1505 idx = hash_sessionid(&new->se_sessionid);
1506 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
1507 spin_lock(&clp->cl_lock);
1508 list_add(&new->se_perclnt, &clp->cl_sessions);
1509 spin_unlock(&clp->cl_lock);
1512 struct sockaddr *sa = svc_addr(rqstp);
1514 * This is a little silly; with sessions there's no real
1515 * use for the callback address. Use the peer address
1516 * as a reasonable default for now, but consider fixing
1517 * the rpc client not to require an address in the
1520 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1521 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
1525 /* caller must hold client_lock */
1526 static struct nfsd4_session *
1527 __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
1529 struct nfsd4_session *elem;
1531 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1533 lockdep_assert_held(&nn->client_lock);
1535 dump_sessionid(__func__, sessionid);
1536 idx = hash_sessionid(sessionid);
1537 /* Search in the appropriate list */
1538 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
1539 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1540 NFS4_MAX_SESSIONID_LEN)) {
1545 dprintk("%s: session not found\n", __func__);
1549 static struct nfsd4_session *
1550 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1553 struct nfsd4_session *session;
1554 __be32 status = nfserr_badsession;
1556 session = __find_in_sessionid_hashtbl(sessionid, net);
1559 status = nfsd4_get_session_locked(session);
1567 /* caller must hold client_lock */
1569 unhash_session(struct nfsd4_session *ses)
1571 struct nfs4_client *clp = ses->se_client;
1572 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1574 lockdep_assert_held(&nn->client_lock);
1576 list_del(&ses->se_hash);
1577 spin_lock(&ses->se_client->cl_lock);
1578 list_del(&ses->se_perclnt);
1579 spin_unlock(&ses->se_client->cl_lock);
1582 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1584 STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
1587 * We're assuming the clid was not given out from a boot
1588 * precisely 2^32 (about 136 years) before this one. That seems
1589 * a safe assumption:
1591 if (clid->cl_boot == (u32)nn->boot_time)
1593 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
1594 clid->cl_boot, clid->cl_id, nn->boot_time);
1599 * XXX Should we use a slab cache ?
1600 * This type of memory management is somewhat inefficient, but we use it
1601 * anyway since SETCLIENTID is not a common operation.
1603 static struct nfs4_client *alloc_client(struct xdr_netobj name)
1605 struct nfs4_client *clp;
1608 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1611 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
1612 if (clp->cl_name.data == NULL)
1614 clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
1615 OWNER_HASH_SIZE, GFP_KERNEL);
1616 if (!clp->cl_ownerstr_hashtbl)
1617 goto err_no_hashtbl;
1618 for (i = 0; i < OWNER_HASH_SIZE; i++)
1619 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
1620 clp->cl_name.len = name.len;
1621 INIT_LIST_HEAD(&clp->cl_sessions);
1622 idr_init(&clp->cl_stateids);
1623 atomic_set(&clp->cl_refcount, 0);
1624 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1625 INIT_LIST_HEAD(&clp->cl_idhash);
1626 INIT_LIST_HEAD(&clp->cl_openowners);
1627 INIT_LIST_HEAD(&clp->cl_delegations);
1628 INIT_LIST_HEAD(&clp->cl_lru);
1629 INIT_LIST_HEAD(&clp->cl_callbacks);
1630 INIT_LIST_HEAD(&clp->cl_revoked);
1631 #ifdef CONFIG_NFSD_PNFS
1632 INIT_LIST_HEAD(&clp->cl_lo_states);
1634 spin_lock_init(&clp->cl_lock);
1635 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1638 kfree(clp->cl_name.data);
1645 free_client(struct nfs4_client *clp)
1647 while (!list_empty(&clp->cl_sessions)) {
1648 struct nfsd4_session *ses;
1649 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1651 list_del(&ses->se_perclnt);
1652 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1655 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
1656 free_svc_cred(&clp->cl_cred);
1657 kfree(clp->cl_ownerstr_hashtbl);
1658 kfree(clp->cl_name.data);
1659 idr_destroy(&clp->cl_stateids);
1663 /* must be called under the client_lock */
1665 unhash_client_locked(struct nfs4_client *clp)
1667 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1668 struct nfsd4_session *ses;
1670 lockdep_assert_held(&nn->client_lock);
1672 /* Mark the client as expired! */
1674 /* Make it invisible */
1675 if (!list_empty(&clp->cl_idhash)) {
1676 list_del_init(&clp->cl_idhash);
1677 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1678 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1680 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1682 list_del_init(&clp->cl_lru);
1683 spin_lock(&clp->cl_lock);
1684 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1685 list_del_init(&ses->se_hash);
1686 spin_unlock(&clp->cl_lock);
1690 unhash_client(struct nfs4_client *clp)
1692 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1694 spin_lock(&nn->client_lock);
1695 unhash_client_locked(clp);
1696 spin_unlock(&nn->client_lock);
1699 static __be32 mark_client_expired_locked(struct nfs4_client *clp)
1701 if (atomic_read(&clp->cl_refcount))
1702 return nfserr_jukebox;
1703 unhash_client_locked(clp);
1708 __destroy_client(struct nfs4_client *clp)
1710 struct nfs4_openowner *oo;
1711 struct nfs4_delegation *dp;
1712 struct list_head reaplist;
1714 INIT_LIST_HEAD(&reaplist);
1715 spin_lock(&state_lock);
1716 while (!list_empty(&clp->cl_delegations)) {
1717 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
1718 unhash_delegation_locked(dp);
1719 list_add(&dp->dl_recall_lru, &reaplist);
1721 spin_unlock(&state_lock);
1722 while (!list_empty(&reaplist)) {
1723 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
1724 list_del_init(&dp->dl_recall_lru);
1725 put_clnt_odstate(dp->dl_clnt_odstate);
1726 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
1727 nfs4_put_stid(&dp->dl_stid);
1729 while (!list_empty(&clp->cl_revoked)) {
1730 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
1731 list_del_init(&dp->dl_recall_lru);
1732 nfs4_put_stid(&dp->dl_stid);
1734 while (!list_empty(&clp->cl_openowners)) {
1735 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1736 nfs4_get_stateowner(&oo->oo_owner);
1737 release_openowner(oo);
1739 nfsd4_return_all_client_layouts(clp);
1740 nfsd4_shutdown_callback(clp);
1741 if (clp->cl_cb_conn.cb_xprt)
1742 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
1747 destroy_client(struct nfs4_client *clp)
1750 __destroy_client(clp);
1753 static void expire_client(struct nfs4_client *clp)
1756 nfsd4_client_record_remove(clp);
1757 __destroy_client(clp);
1760 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1762 memcpy(target->cl_verifier.data, source->data,
1763 sizeof(target->cl_verifier.data));
1766 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1768 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1769 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1772 static int copy_cred(struct svc_cred *target, struct svc_cred *source)
1774 if (source->cr_principal) {
1775 target->cr_principal =
1776 kstrdup(source->cr_principal, GFP_KERNEL);
1777 if (target->cr_principal == NULL)
1780 target->cr_principal = NULL;
1781 target->cr_flavor = source->cr_flavor;
1782 target->cr_uid = source->cr_uid;
1783 target->cr_gid = source->cr_gid;
1784 target->cr_group_info = source->cr_group_info;
1785 get_group_info(target->cr_group_info);
1786 target->cr_gss_mech = source->cr_gss_mech;
1787 if (source->cr_gss_mech)
1788 gss_mech_get(source->cr_gss_mech);
1793 compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1795 if (o1->len < o2->len)
1797 if (o1->len > o2->len)
1799 return memcmp(o1->data, o2->data, o1->len);
1802 static int same_name(const char *n1, const char *n2)
1804 return 0 == memcmp(n1, n2, HEXDIR_LEN);
1808 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1810 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
1814 same_clid(clientid_t *cl1, clientid_t *cl2)
1816 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
1819 static bool groups_equal(struct group_info *g1, struct group_info *g2)
1823 if (g1->ngroups != g2->ngroups)
1825 for (i=0; i<g1->ngroups; i++)
1826 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
1832 * RFC 3530 language requires clid_inuse be returned when the
1833 * "principal" associated with a requests differs from that previously
1834 * used. We use uid, gid's, and gss principal string as our best
1835 * approximation. We also don't want to allow non-gss use of a client
1836 * established using gss: in theory cr_principal should catch that
1837 * change, but in practice cr_principal can be null even in the gss case
1838 * since gssd doesn't always pass down a principal string.
1840 static bool is_gss_cred(struct svc_cred *cr)
1842 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1843 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1848 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1850 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
1851 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1852 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
1853 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1855 if (cr1->cr_principal == cr2->cr_principal)
1857 if (!cr1->cr_principal || !cr2->cr_principal)
1859 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
1862 static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1864 struct svc_cred *cr = &rqstp->rq_cred;
1867 if (!cr->cr_gss_mech)
1869 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1870 return service == RPC_GSS_SVC_INTEGRITY ||
1871 service == RPC_GSS_SVC_PRIVACY;
1874 static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1876 struct svc_cred *cr = &rqstp->rq_cred;
1878 if (!cl->cl_mach_cred)
1880 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1882 if (!svc_rqst_integrity_protected(rqstp))
1884 if (!cr->cr_principal)
1886 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1889 static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
1894 * This is opaque to client, so no need to byte-swap. Use
1895 * __force to keep sparse happy
1897 verf[0] = (__force __be32)get_seconds();
1898 verf[1] = (__force __be32)nn->clientid_counter;
1899 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
1902 static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
1904 clp->cl_clientid.cl_boot = nn->boot_time;
1905 clp->cl_clientid.cl_id = nn->clientid_counter++;
1906 gen_confirm(clp, nn);
1909 static struct nfs4_stid *
1910 find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
1912 struct nfs4_stid *ret;
1914 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1915 if (!ret || !ret->sc_type)
1920 static struct nfs4_stid *
1921 find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
1923 struct nfs4_stid *s;
1925 spin_lock(&cl->cl_lock);
1926 s = find_stateid_locked(cl, t);
1928 if (typemask & s->sc_type)
1929 atomic_inc(&s->sc_count);
1933 spin_unlock(&cl->cl_lock);
1937 static struct nfs4_client *create_client(struct xdr_netobj name,
1938 struct svc_rqst *rqstp, nfs4_verifier *verf)
1940 struct nfs4_client *clp;
1941 struct sockaddr *sa = svc_addr(rqstp);
1943 struct net *net = SVC_NET(rqstp);
1945 clp = alloc_client(name);
1949 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1954 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
1955 clp->cl_time = get_seconds();
1956 clear_bit(0, &clp->cl_cb_slot_busy);
1957 copy_verf(clp, verf);
1958 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
1959 clp->cl_cb_session = NULL;
1965 add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1967 struct rb_node **new = &(root->rb_node), *parent = NULL;
1968 struct nfs4_client *clp;
1971 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1974 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1975 new = &((*new)->rb_left);
1977 new = &((*new)->rb_right);
1980 rb_link_node(&new_clp->cl_namenode, parent, new);
1981 rb_insert_color(&new_clp->cl_namenode, root);
1984 static struct nfs4_client *
1985 find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1988 struct rb_node *node = root->rb_node;
1989 struct nfs4_client *clp;
1992 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1993 cmp = compare_blob(&clp->cl_name, name);
1995 node = node->rb_left;
1997 node = node->rb_right;
2005 add_to_unconfirmed(struct nfs4_client *clp)
2007 unsigned int idhashval;
2008 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2010 lockdep_assert_held(&nn->client_lock);
2012 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2013 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
2014 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
2015 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
2016 renew_client_locked(clp);
2020 move_to_confirmed(struct nfs4_client *clp)
2022 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
2023 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2025 lockdep_assert_held(&nn->client_lock);
2027 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
2028 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
2029 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
2030 add_clp_to_name_tree(clp, &nn->conf_name_tree);
2031 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2032 renew_client_locked(clp);
2035 static struct nfs4_client *
2036 find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
2038 struct nfs4_client *clp;
2039 unsigned int idhashval = clientid_hashval(clid->cl_id);
2041 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
2042 if (same_clid(&clp->cl_clientid, clid)) {
2043 if ((bool)clp->cl_minorversion != sessions)
2045 renew_client_locked(clp);
2052 static struct nfs4_client *
2053 find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2055 struct list_head *tbl = nn->conf_id_hashtbl;
2057 lockdep_assert_held(&nn->client_lock);
2058 return find_client_in_id_table(tbl, clid, sessions);
2061 static struct nfs4_client *
2062 find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2064 struct list_head *tbl = nn->unconf_id_hashtbl;
2066 lockdep_assert_held(&nn->client_lock);
2067 return find_client_in_id_table(tbl, clid, sessions);
2070 static bool clp_used_exchangeid(struct nfs4_client *clp)
2072 return clp->cl_exchange_flags != 0;
2075 static struct nfs4_client *
2076 find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
2078 lockdep_assert_held(&nn->client_lock);
2079 return find_clp_in_name_tree(name, &nn->conf_name_tree);
2082 static struct nfs4_client *
2083 find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
2085 lockdep_assert_held(&nn->client_lock);
2086 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
2090 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
2092 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
2093 struct sockaddr *sa = svc_addr(rqstp);
2094 u32 scopeid = rpc_get_scope_id(sa);
2095 unsigned short expected_family;
2097 /* Currently, we only support tcp and tcp6 for the callback channel */
2098 if (se->se_callback_netid_len == 3 &&
2099 !memcmp(se->se_callback_netid_val, "tcp", 3))
2100 expected_family = AF_INET;
2101 else if (se->se_callback_netid_len == 4 &&
2102 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2103 expected_family = AF_INET6;
2107 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
2108 se->se_callback_addr_len,
2109 (struct sockaddr *)&conn->cb_addr,
2110 sizeof(conn->cb_addr));
2112 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
2115 if (conn->cb_addr.ss_family == AF_INET6)
2116 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
2118 conn->cb_prog = se->se_callback_prog;
2119 conn->cb_ident = se->se_callback_ident;
2120 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
2123 conn->cb_addr.ss_family = AF_UNSPEC;
2124 conn->cb_addrlen = 0;
2125 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
2126 "will not receive delegations\n",
2127 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2133 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
2136 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2138 struct xdr_buf *buf = resp->xdr.buf;
2139 struct nfsd4_slot *slot = resp->cstate.slot;
2142 dprintk("--> %s slot %p\n", __func__, slot);
2144 slot->sl_opcnt = resp->opcnt;
2145 slot->sl_status = resp->cstate.status;
2147 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
2148 if (nfsd4_not_cached(resp)) {
2149 slot->sl_datalen = 0;
2152 base = resp->cstate.data_offset;
2153 slot->sl_datalen = buf->len - base;
2154 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
2155 WARN("%s: sessions DRC could not cache compound\n", __func__);
2160 * Encode the replay sequence operation from the slot values.
2161 * If cachethis is FALSE encode the uncached rep error on the next
2162 * operation which sets resp->p and increments resp->opcnt for
2163 * nfs4svc_encode_compoundres.
2167 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2168 struct nfsd4_compoundres *resp)
2170 struct nfsd4_op *op;
2171 struct nfsd4_slot *slot = resp->cstate.slot;
2173 /* Encode the replayed sequence operation */
2174 op = &args->ops[resp->opcnt - 1];
2175 nfsd4_encode_operation(resp, op);
2177 /* Return nfserr_retry_uncached_rep in next operation. */
2178 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
2179 op = &args->ops[resp->opcnt++];
2180 op->status = nfserr_retry_uncached_rep;
2181 nfsd4_encode_operation(resp, op);
2187 * The sequence operation is not cached because we can use the slot and
2191 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2192 struct nfsd4_sequence *seq)
2194 struct nfsd4_slot *slot = resp->cstate.slot;
2195 struct xdr_stream *xdr = &resp->xdr;
2199 dprintk("--> %s slot %p\n", __func__, slot);
2201 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
2205 p = xdr_reserve_space(xdr, slot->sl_datalen);
2208 return nfserr_serverfault;
2210 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2211 xdr_commit_encode(xdr);
2213 resp->opcnt = slot->sl_opcnt;
2214 return slot->sl_status;
2218 * Set the exchange_id flags returned by the server.
2221 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2223 #ifdef CONFIG_NFSD_PNFS
2224 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
2226 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
2229 /* Referrals are supported, Migration is not. */
2230 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2232 /* set the wire flags to return to client. */
2233 clid->flags = new->cl_exchange_flags;
2236 static bool client_has_state(struct nfs4_client *clp)
2239 * Note clp->cl_openowners check isn't quite right: there's no
2240 * need to count owners without stateid's.
2242 * Also note we should probably be using this in 4.0 case too.
2244 return !list_empty(&clp->cl_openowners)
2245 || !list_empty(&clp->cl_delegations)
2246 || !list_empty(&clp->cl_sessions);
2250 nfsd4_exchange_id(struct svc_rqst *rqstp,
2251 struct nfsd4_compound_state *cstate,
2252 struct nfsd4_exchange_id *exid)
2254 struct nfs4_client *conf, *new;
2255 struct nfs4_client *unconf = NULL;
2257 char addr_str[INET6_ADDRSTRLEN];
2258 nfs4_verifier verf = exid->verifier;
2259 struct sockaddr *sa = svc_addr(rqstp);
2260 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
2261 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2263 rpc_ntop(sa, addr_str, sizeof(addr_str));
2264 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
2265 "ip_addr=%s flags %x, spa_how %d\n",
2266 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
2267 addr_str, exid->flags, exid->spa_how);
2269 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
2270 return nfserr_inval;
2272 switch (exid->spa_how) {
2274 if (!svc_rqst_integrity_protected(rqstp))
2275 return nfserr_inval;
2278 default: /* checked by xdr code */
2281 return nfserr_encr_alg_unsupp;
2284 new = create_client(exid->clname, rqstp, &verf);
2286 return nfserr_jukebox;
2288 /* Cases below refer to rfc 5661 section 18.35.4: */
2289 spin_lock(&nn->client_lock);
2290 conf = find_confirmed_client_by_name(&exid->clname, nn);
2292 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2293 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2296 if (!clp_used_exchangeid(conf)) { /* buggy client */
2297 status = nfserr_inval;
2300 if (!mach_creds_match(conf, rqstp)) {
2301 status = nfserr_wrong_cred;
2304 if (!creds_match) { /* case 9 */
2305 status = nfserr_perm;
2308 if (!verfs_match) { /* case 8 */
2309 status = nfserr_not_same;
2313 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
2316 if (!creds_match) { /* case 3 */
2317 if (client_has_state(conf)) {
2318 status = nfserr_clid_inuse;
2323 if (verfs_match) { /* case 2 */
2324 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
2327 /* case 5, client reboot */
2332 if (update) { /* case 7 */
2333 status = nfserr_noent;
2337 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
2338 if (unconf) /* case 4, possible retry or client restart */
2339 unhash_client_locked(unconf);
2341 /* case 1 (normal case) */
2344 status = mark_client_expired_locked(conf);
2348 new->cl_minorversion = cstate->minorversion;
2349 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
2352 add_to_unconfirmed(new);
2355 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
2356 exid->clientid.cl_id = conf->cl_clientid.cl_id;
2358 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
2359 nfsd4_set_ex_flags(conf, exid);
2361 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
2362 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
2366 spin_unlock(&nn->client_lock);
2370 expire_client(unconf);
2375 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
2377 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2380 /* The slot is in use, and no response has been sent. */
2382 if (seqid == slot_seqid)
2383 return nfserr_jukebox;
2385 return nfserr_seq_misordered;
2387 /* Note unsigned 32-bit arithmetic handles wraparound: */
2388 if (likely(seqid == slot_seqid + 1))
2390 if (seqid == slot_seqid)
2391 return nfserr_replay_cache;
2392 return nfserr_seq_misordered;
2396 * Cache the create session result into the create session single DRC
2397 * slot cache by saving the xdr structure. sl_seqid has been set.
2398 * Do this for solo or embedded create session operations.
2401 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
2402 struct nfsd4_clid_slot *slot, __be32 nfserr)
2404 slot->sl_status = nfserr;
2405 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2409 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2410 struct nfsd4_clid_slot *slot)
2412 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2413 return slot->sl_status;
2416 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2417 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2418 1 + /* MIN tag is length with zero, only length */ \
2419 3 + /* version, opcount, opcode */ \
2420 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2421 /* seqid, slotID, slotID, cache */ \
2422 4 ) * sizeof(__be32))
2424 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2425 2 + /* verifier: AUTH_NULL, length 0 */\
2427 1 + /* MIN tag is length with zero, only length */ \
2428 3 + /* opcount, opcode, opstatus*/ \
2429 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2430 /* seqid, slotID, slotID, slotID, status */ \
2431 5 ) * sizeof(__be32))
2433 static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
2435 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2437 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2438 return nfserr_toosmall;
2439 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2440 return nfserr_toosmall;
2441 ca->headerpadsz = 0;
2442 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2443 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2444 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2445 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2446 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2447 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2449 * Note decreasing slot size below client's request may make it
2450 * difficult for client to function correctly, whereas
2451 * decreasing the number of slots will (just?) affect
2452 * performance. When short on memory we therefore prefer to
2453 * decrease number of slots instead of their size. Clients that
2454 * request larger slots than they need will get poor results:
2456 ca->maxreqs = nfsd4_get_drc_mem(ca);
2458 return nfserr_jukebox;
2463 #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2464 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2465 #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2466 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2468 static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2470 ca->headerpadsz = 0;
2473 * These RPC_MAX_HEADER macros are overkill, especially since we
2474 * don't even do gss on the backchannel yet. But this is still
2475 * less than 1k. Tighten up this estimate in the unlikely event
2476 * it turns out to be a problem for some client:
2478 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
2479 return nfserr_toosmall;
2480 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
2481 return nfserr_toosmall;
2482 ca->maxresp_cached = 0;
2484 return nfserr_toosmall;
2489 static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2491 switch (cbs->flavor) {
2497 * GSS case: the spec doesn't allow us to return this
2498 * error. But it also doesn't allow us not to support
2500 * I'd rather this fail hard than return some error the
2501 * client might think it can already handle:
2503 return nfserr_encr_alg_unsupp;
2508 nfsd4_create_session(struct svc_rqst *rqstp,
2509 struct nfsd4_compound_state *cstate,
2510 struct nfsd4_create_session *cr_ses)
2512 struct sockaddr *sa = svc_addr(rqstp);
2513 struct nfs4_client *conf, *unconf;
2514 struct nfs4_client *old = NULL;
2515 struct nfsd4_session *new;
2516 struct nfsd4_conn *conn;
2517 struct nfsd4_clid_slot *cs_slot = NULL;
2519 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2521 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2522 return nfserr_inval;
2523 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2526 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
2529 status = check_backchannel_attrs(&cr_ses->back_channel);
2531 goto out_release_drc_mem;
2532 status = nfserr_jukebox;
2533 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
2535 goto out_release_drc_mem;
2536 conn = alloc_conn_from_crses(rqstp, cr_ses);
2538 goto out_free_session;
2540 spin_lock(&nn->client_lock);
2541 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
2542 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
2543 WARN_ON_ONCE(conf && unconf);
2546 status = nfserr_wrong_cred;
2547 if (!mach_creds_match(conf, rqstp))
2549 cs_slot = &conf->cl_cs_slot;
2550 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2551 if (status == nfserr_replay_cache) {
2552 status = nfsd4_replay_create_session(cr_ses, cs_slot);
2554 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
2555 status = nfserr_seq_misordered;
2558 } else if (unconf) {
2559 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
2560 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
2561 status = nfserr_clid_inuse;
2564 status = nfserr_wrong_cred;
2565 if (!mach_creds_match(unconf, rqstp))
2567 cs_slot = &unconf->cl_cs_slot;
2568 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2570 /* an unconfirmed replay returns misordered */
2571 status = nfserr_seq_misordered;
2574 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
2576 status = mark_client_expired_locked(old);
2582 move_to_confirmed(unconf);
2585 status = nfserr_stale_clientid;
2590 * We do not support RDMA or persistent sessions
2592 cr_ses->flags &= ~SESSION4_PERSIST;
2593 cr_ses->flags &= ~SESSION4_RDMA;
2595 init_session(rqstp, new, conf, cr_ses);
2596 nfsd4_get_session_locked(new);
2598 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
2599 NFS4_MAX_SESSIONID_LEN);
2600 cs_slot->sl_seqid++;
2601 cr_ses->seqid = cs_slot->sl_seqid;
2603 /* cache solo and embedded create sessions under the client_lock */
2604 nfsd4_cache_create_session(cr_ses, cs_slot, status);
2605 spin_unlock(&nn->client_lock);
2606 /* init connection and backchannel */
2607 nfsd4_init_conn(rqstp, conn, new);
2608 nfsd4_put_session(new);
2613 spin_unlock(&nn->client_lock);
2618 __free_session(new);
2619 out_release_drc_mem:
2620 nfsd4_put_drc_mem(&cr_ses->fore_channel);
2624 static __be32 nfsd4_map_bcts_dir(u32 *dir)
2627 case NFS4_CDFC4_FORE:
2628 case NFS4_CDFC4_BACK:
2630 case NFS4_CDFC4_FORE_OR_BOTH:
2631 case NFS4_CDFC4_BACK_OR_BOTH:
2632 *dir = NFS4_CDFC4_BOTH;
2635 return nfserr_inval;
2638 __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2640 struct nfsd4_session *session = cstate->session;
2641 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2644 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2647 spin_lock(&nn->client_lock);
2648 session->se_cb_prog = bc->bc_cb_program;
2649 session->se_cb_sec = bc->bc_cb_sec;
2650 spin_unlock(&nn->client_lock);
2652 nfsd4_probe_callback(session->se_client);
2657 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2658 struct nfsd4_compound_state *cstate,
2659 struct nfsd4_bind_conn_to_session *bcts)
2662 struct nfsd4_conn *conn;
2663 struct nfsd4_session *session;
2664 struct net *net = SVC_NET(rqstp);
2665 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2667 if (!nfsd4_last_compound_op(rqstp))
2668 return nfserr_not_only_op;
2669 spin_lock(&nn->client_lock);
2670 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
2671 spin_unlock(&nn->client_lock);
2673 goto out_no_session;
2674 status = nfserr_wrong_cred;
2675 if (!mach_creds_match(session->se_client, rqstp))
2677 status = nfsd4_map_bcts_dir(&bcts->dir);
2680 conn = alloc_conn(rqstp, bcts->dir);
2681 status = nfserr_jukebox;
2684 nfsd4_init_conn(rqstp, conn, session);
2687 nfsd4_put_session(session);
2692 static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2696 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2700 nfsd4_destroy_session(struct svc_rqst *r,
2701 struct nfsd4_compound_state *cstate,
2702 struct nfsd4_destroy_session *sessionid)
2704 struct nfsd4_session *ses;
2706 int ref_held_by_me = 0;
2707 struct net *net = SVC_NET(r);
2708 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2710 status = nfserr_not_only_op;
2711 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
2712 if (!nfsd4_last_compound_op(r))
2716 dump_sessionid(__func__, &sessionid->sessionid);
2717 spin_lock(&nn->client_lock);
2718 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
2720 goto out_client_lock;
2721 status = nfserr_wrong_cred;
2722 if (!mach_creds_match(ses->se_client, r))
2723 goto out_put_session;
2724 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
2726 goto out_put_session;
2727 unhash_session(ses);
2728 spin_unlock(&nn->client_lock);
2730 nfsd4_probe_callback_sync(ses->se_client);
2732 spin_lock(&nn->client_lock);
2735 nfsd4_put_session_locked(ses);
2737 spin_unlock(&nn->client_lock);
2742 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
2744 struct nfsd4_conn *c;
2746 list_for_each_entry(c, &s->se_conns, cn_persession) {
2747 if (c->cn_xprt == xpt) {
2754 static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
2756 struct nfs4_client *clp = ses->se_client;
2757 struct nfsd4_conn *c;
2758 __be32 status = nfs_ok;
2761 spin_lock(&clp->cl_lock);
2762 c = __nfsd4_find_conn(new->cn_xprt, ses);
2765 status = nfserr_conn_not_bound_to_session;
2766 if (clp->cl_mach_cred)
2768 __nfsd4_hash_conn(new, ses);
2769 spin_unlock(&clp->cl_lock);
2770 ret = nfsd4_register_conn(new);
2772 /* oops; xprt is already down: */
2773 nfsd4_conn_lost(&new->cn_xpt_user);
2776 spin_unlock(&clp->cl_lock);
2781 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2783 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2785 return args->opcnt > session->se_fchannel.maxops;
2788 static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2789 struct nfsd4_session *session)
2791 struct xdr_buf *xb = &rqstp->rq_arg;
2793 return xb->len > session->se_fchannel.maxreq_sz;
2797 nfsd4_sequence(struct svc_rqst *rqstp,
2798 struct nfsd4_compound_state *cstate,
2799 struct nfsd4_sequence *seq)
2801 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2802 struct xdr_stream *xdr = &resp->xdr;
2803 struct nfsd4_session *session;
2804 struct nfs4_client *clp;
2805 struct nfsd4_slot *slot;
2806 struct nfsd4_conn *conn;
2809 struct net *net = SVC_NET(rqstp);
2810 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2812 if (resp->opcnt != 1)
2813 return nfserr_sequence_pos;
2816 * Will be either used or freed by nfsd4_sequence_check_conn
2819 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2821 return nfserr_jukebox;
2823 spin_lock(&nn->client_lock);
2824 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
2826 goto out_no_session;
2827 clp = session->se_client;
2829 status = nfserr_too_many_ops;
2830 if (nfsd4_session_too_many_ops(rqstp, session))
2831 goto out_put_session;
2833 status = nfserr_req_too_big;
2834 if (nfsd4_request_too_big(rqstp, session))
2835 goto out_put_session;
2837 status = nfserr_badslot;
2838 if (seq->slotid >= session->se_fchannel.maxreqs)
2839 goto out_put_session;
2841 slot = session->se_slots[seq->slotid];
2842 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2844 /* We do not negotiate the number of slots yet, so set the
2845 * maxslots to the session maxreqs which is used to encode
2846 * sr_highest_slotid and the sr_target_slot id to maxslots */
2847 seq->maxslots = session->se_fchannel.maxreqs;
2849 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2850 slot->sl_flags & NFSD4_SLOT_INUSE);
2851 if (status == nfserr_replay_cache) {
2852 status = nfserr_seq_misordered;
2853 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
2854 goto out_put_session;
2855 cstate->slot = slot;
2856 cstate->session = session;
2858 /* Return the cached reply status and set cstate->status
2859 * for nfsd4_proc_compound processing */
2860 status = nfsd4_replay_cache_entry(resp, seq);
2861 cstate->status = nfserr_replay_cache;
2865 goto out_put_session;
2867 status = nfsd4_sequence_check_conn(conn, session);
2870 goto out_put_session;
2872 buflen = (seq->cachethis) ?
2873 session->se_fchannel.maxresp_cached :
2874 session->se_fchannel.maxresp_sz;
2875 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2877 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
2878 goto out_put_session;
2879 svc_reserve(rqstp, buflen);
2882 /* Success! bump slot seqid */
2883 slot->sl_seqid = seq->seqid;
2884 slot->sl_flags |= NFSD4_SLOT_INUSE;
2886 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
2888 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
2890 cstate->slot = slot;
2891 cstate->session = session;
2895 switch (clp->cl_cb_state) {
2897 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2899 case NFSD4_CB_FAULT:
2900 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2903 seq->status_flags = 0;
2905 if (!list_empty(&clp->cl_revoked))
2906 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
2910 spin_unlock(&nn->client_lock);
2913 nfsd4_put_session_locked(session);
2914 goto out_no_session;
2918 nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2920 struct nfsd4_compound_state *cs = &resp->cstate;
2922 if (nfsd4_has_session(cs)) {
2923 if (cs->status != nfserr_replay_cache) {
2924 nfsd4_store_cache_entry(resp);
2925 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2927 /* Drop session reference that was taken in nfsd4_sequence() */
2928 nfsd4_put_session(cs->session);
2930 put_client_renew(cs->clp);
2934 nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2936 struct nfs4_client *conf, *unconf;
2937 struct nfs4_client *clp = NULL;
2939 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2941 spin_lock(&nn->client_lock);
2942 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
2943 conf = find_confirmed_client(&dc->clientid, true, nn);
2944 WARN_ON_ONCE(conf && unconf);
2947 if (client_has_state(conf)) {
2948 status = nfserr_clientid_busy;
2951 status = mark_client_expired_locked(conf);
2958 status = nfserr_stale_clientid;
2961 if (!mach_creds_match(clp, rqstp)) {
2963 status = nfserr_wrong_cred;
2966 unhash_client_locked(clp);
2968 spin_unlock(&nn->client_lock);
2975 nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2979 if (rc->rca_one_fs) {
2980 if (!cstate->current_fh.fh_dentry)
2981 return nfserr_nofilehandle;
2983 * We don't take advantage of the rca_one_fs case.
2984 * That's OK, it's optional, we can safely ignore it.
2989 status = nfserr_complete_already;
2990 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2991 &cstate->session->se_client->cl_flags))
2994 status = nfserr_stale_clientid;
2995 if (is_client_expired(cstate->session->se_client))
2997 * The following error isn't really legal.
2998 * But we only get here if the client just explicitly
2999 * destroyed the client. Surely it no longer cares what
3000 * error it gets back on an operation for the dead
3006 nfsd4_client_record_create(cstate->session->se_client);
3012 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3013 struct nfsd4_setclientid *setclid)
3015 struct xdr_netobj clname = setclid->se_name;
3016 nfs4_verifier clverifier = setclid->se_verf;
3017 struct nfs4_client *conf, *new;
3018 struct nfs4_client *unconf = NULL;
3020 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3022 new = create_client(clname, rqstp, &clverifier);
3024 return nfserr_jukebox;
3025 /* Cases below refer to rfc 3530 section 14.2.33: */
3026 spin_lock(&nn->client_lock);
3027 conf = find_confirmed_client_by_name(&clname, nn);
3030 status = nfserr_clid_inuse;
3031 if (clp_used_exchangeid(conf))
3033 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
3034 char addr_str[INET6_ADDRSTRLEN];
3035 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
3037 dprintk("NFSD: setclientid: string in use by client "
3038 "at %s\n", addr_str);
3042 unconf = find_unconfirmed_client_by_name(&clname, nn);
3044 unhash_client_locked(unconf);
3045 if (conf && same_verf(&conf->cl_verifier, &clverifier))
3046 /* case 1: probable callback update */
3047 copy_clid(new, conf);
3048 else /* case 4 (new client) or cases 2, 3 (client reboot): */
3050 new->cl_minorversion = 0;
3051 gen_callback(new, setclid, rqstp);
3052 add_to_unconfirmed(new);
3053 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
3054 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
3055 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
3059 spin_unlock(&nn->client_lock);
3063 expire_client(unconf);
3069 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
3070 struct nfsd4_compound_state *cstate,
3071 struct nfsd4_setclientid_confirm *setclientid_confirm)
3073 struct nfs4_client *conf, *unconf;
3074 struct nfs4_client *old = NULL;
3075 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
3076 clientid_t * clid = &setclientid_confirm->sc_clientid;
3078 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3080 if (STALE_CLIENTID(clid, nn))
3081 return nfserr_stale_clientid;
3083 spin_lock(&nn->client_lock);
3084 conf = find_confirmed_client(clid, false, nn);
3085 unconf = find_unconfirmed_client(clid, false, nn);
3087 * We try hard to give out unique clientid's, so if we get an
3088 * attempt to confirm the same clientid with a different cred,
3089 * there's a bug somewhere. Let's charitably assume it's our
3092 status = nfserr_serverfault;
3093 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3095 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3097 /* cases below refer to rfc 3530 section 14.2.34: */
3098 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
3099 if (conf && !unconf) /* case 2: probable retransmit */
3101 else /* case 4: client hasn't noticed we rebooted yet? */
3102 status = nfserr_stale_clientid;
3106 if (conf) { /* case 1: callback update */
3108 unhash_client_locked(old);
3109 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
3110 } else { /* case 3: normal case; new or rebooted client */
3111 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3113 status = mark_client_expired_locked(old);
3119 move_to_confirmed(unconf);
3122 get_client_locked(conf);
3123 spin_unlock(&nn->client_lock);
3124 nfsd4_probe_callback(conf);
3125 spin_lock(&nn->client_lock);
3126 put_client_renew_locked(conf);
3128 spin_unlock(&nn->client_lock);
3134 static struct nfs4_file *nfsd4_alloc_file(void)
3136 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3139 /* OPEN Share state helper functions */
3140 static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
3141 struct nfs4_file *fp)
3143 lockdep_assert_held(&state_lock);
3145 atomic_set(&fp->fi_ref, 1);
3146 spin_lock_init(&fp->fi_lock);
3147 INIT_LIST_HEAD(&fp->fi_stateids);
3148 INIT_LIST_HEAD(&fp->fi_delegations);
3149 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
3150 fh_copy_shallow(&fp->fi_fhandle, fh);
3151 fp->fi_deleg_file = NULL;
3152 fp->fi_had_conflict = false;
3153 fp->fi_share_deny = 0;
3154 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3155 memset(fp->fi_access, 0, sizeof(fp->fi_access));
3156 #ifdef CONFIG_NFSD_PNFS
3157 INIT_LIST_HEAD(&fp->fi_lo_states);
3158 atomic_set(&fp->fi_lo_recalls, 0);
3160 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
3164 nfsd4_free_slabs(void)
3166 kmem_cache_destroy(odstate_slab);
3167 kmem_cache_destroy(openowner_slab);
3168 kmem_cache_destroy(lockowner_slab);
3169 kmem_cache_destroy(file_slab);
3170 kmem_cache_destroy(stateid_slab);
3171 kmem_cache_destroy(deleg_slab);
3175 nfsd4_init_slabs(void)
3177 openowner_slab = kmem_cache_create("nfsd4_openowners",
3178 sizeof(struct nfs4_openowner), 0, 0, NULL);
3179 if (openowner_slab == NULL)
3181 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
3182 sizeof(struct nfs4_lockowner), 0, 0, NULL);
3183 if (lockowner_slab == NULL)
3184 goto out_free_openowner_slab;
3185 file_slab = kmem_cache_create("nfsd4_files",
3186 sizeof(struct nfs4_file), 0, 0, NULL);
3187 if (file_slab == NULL)
3188 goto out_free_lockowner_slab;
3189 stateid_slab = kmem_cache_create("nfsd4_stateids",
3190 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
3191 if (stateid_slab == NULL)
3192 goto out_free_file_slab;
3193 deleg_slab = kmem_cache_create("nfsd4_delegations",
3194 sizeof(struct nfs4_delegation), 0, 0, NULL);
3195 if (deleg_slab == NULL)
3196 goto out_free_stateid_slab;
3197 odstate_slab = kmem_cache_create("nfsd4_odstate",
3198 sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
3199 if (odstate_slab == NULL)
3200 goto out_free_deleg_slab;
3203 out_free_deleg_slab:
3204 kmem_cache_destroy(deleg_slab);
3205 out_free_stateid_slab:
3206 kmem_cache_destroy(stateid_slab);
3208 kmem_cache_destroy(file_slab);
3209 out_free_lockowner_slab:
3210 kmem_cache_destroy(lockowner_slab);
3211 out_free_openowner_slab:
3212 kmem_cache_destroy(openowner_slab);
3214 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3218 static void init_nfs4_replay(struct nfs4_replay *rp)
3220 rp->rp_status = nfserr_serverfault;
3222 rp->rp_buf = rp->rp_ibuf;
3223 mutex_init(&rp->rp_mutex);
3226 static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3227 struct nfs4_stateowner *so)
3229 if (!nfsd4_has_session(cstate)) {
3230 mutex_lock(&so->so_replay.rp_mutex);
3231 cstate->replay_owner = nfs4_get_stateowner(so);
3235 void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3237 struct nfs4_stateowner *so = cstate->replay_owner;
3240 cstate->replay_owner = NULL;
3241 mutex_unlock(&so->so_replay.rp_mutex);
3242 nfs4_put_stateowner(so);
3246 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
3248 struct nfs4_stateowner *sop;
3250 sop = kmem_cache_alloc(slab, GFP_KERNEL);
3254 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3255 if (!sop->so_owner.data) {
3256 kmem_cache_free(slab, sop);
3259 sop->so_owner.len = owner->len;
3261 INIT_LIST_HEAD(&sop->so_stateids);
3262 sop->so_client = clp;
3263 init_nfs4_replay(&sop->so_replay);
3264 atomic_set(&sop->so_count, 1);
3268 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
3270 lockdep_assert_held(&clp->cl_lock);
3272 list_add(&oo->oo_owner.so_strhash,
3273 &clp->cl_ownerstr_hashtbl[strhashval]);
3274 list_add(&oo->oo_perclient, &clp->cl_openowners);
3277 static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3279 unhash_openowner_locked(openowner(so));
3282 static void nfs4_free_openowner(struct nfs4_stateowner *so)
3284 struct nfs4_openowner *oo = openowner(so);
3286 kmem_cache_free(openowner_slab, oo);
3289 static const struct nfs4_stateowner_operations openowner_ops = {
3290 .so_unhash = nfs4_unhash_openowner,
3291 .so_free = nfs4_free_openowner,
3294 static struct nfs4_openowner *
3295 alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
3296 struct nfsd4_compound_state *cstate)
3298 struct nfs4_client *clp = cstate->clp;
3299 struct nfs4_openowner *oo, *ret;
3301 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3304 oo->oo_owner.so_ops = &openowner_ops;
3305 oo->oo_owner.so_is_open_owner = 1;
3306 oo->oo_owner.so_seqid = open->op_seqid;
3308 if (nfsd4_has_session(cstate))
3309 oo->oo_flags |= NFS4_OO_CONFIRMED;
3311 oo->oo_last_closed_stid = NULL;
3312 INIT_LIST_HEAD(&oo->oo_close_lru);
3313 spin_lock(&clp->cl_lock);
3314 ret = find_openstateowner_str_locked(strhashval, open, clp);
3316 hash_openowner(oo, clp, strhashval);
3319 nfs4_free_openowner(&oo->oo_owner);
3320 spin_unlock(&clp->cl_lock);
3324 static void init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
3325 struct nfs4_openowner *oo = open->op_openowner;
3327 atomic_inc(&stp->st_stid.sc_count);
3328 stp->st_stid.sc_type = NFS4_OPEN_STID;
3329 INIT_LIST_HEAD(&stp->st_locks);
3330 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
3332 stp->st_stid.sc_file = fp;
3333 stp->st_access_bmap = 0;
3334 stp->st_deny_bmap = 0;
3335 stp->st_openstp = NULL;
3336 spin_lock(&oo->oo_owner.so_client->cl_lock);
3337 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
3338 spin_lock(&fp->fi_lock);
3339 list_add(&stp->st_perfile, &fp->fi_stateids);
3340 spin_unlock(&fp->fi_lock);
3341 spin_unlock(&oo->oo_owner.so_client->cl_lock);
3345 * In the 4.0 case we need to keep the owners around a little while to handle
3346 * CLOSE replay. We still do need to release any file access that is held by
3347 * them before returning however.
3350 move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
3352 struct nfs4_ol_stateid *last;
3353 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3354 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3357 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
3360 * We know that we hold one reference via nfsd4_close, and another
3361 * "persistent" reference for the client. If the refcount is higher
3362 * than 2, then there are still calls in progress that are using this
3363 * stateid. We can't put the sc_file reference until they are finished.
3364 * Wait for the refcount to drop to 2. Since it has been unhashed,
3365 * there should be no danger of the refcount going back up again at
3368 wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3370 release_all_access(s);
3371 if (s->st_stid.sc_file) {
3372 put_nfs4_file(s->st_stid.sc_file);
3373 s->st_stid.sc_file = NULL;
3376 spin_lock(&nn->client_lock);
3377 last = oo->oo_last_closed_stid;
3378 oo->oo_last_closed_stid = s;
3379 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
3380 oo->oo_time = get_seconds();
3381 spin_unlock(&nn->client_lock);
3383 nfs4_put_stid(&last->st_stid);
3386 /* search file_hashtbl[] for file */
3387 static struct nfs4_file *
3388 find_file_locked(struct knfsd_fh *fh, unsigned int hashval)
3390 struct nfs4_file *fp;
3392 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash) {
3393 if (fh_match(&fp->fi_fhandle, fh)) {
3394 if (atomic_inc_not_zero(&fp->fi_ref))
3402 find_file(struct knfsd_fh *fh)
3404 struct nfs4_file *fp;
3405 unsigned int hashval = file_hashval(fh);
3408 fp = find_file_locked(fh, hashval);
3413 static struct nfs4_file *
3414 find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
3416 struct nfs4_file *fp;
3417 unsigned int hashval = file_hashval(fh);
3420 fp = find_file_locked(fh, hashval);
3425 spin_lock(&state_lock);
3426 fp = find_file_locked(fh, hashval);
3427 if (likely(fp == NULL)) {
3428 nfsd4_init_file(fh, hashval, new);
3431 spin_unlock(&state_lock);
3437 * Called to check deny when READ with all zero stateid or
3438 * WRITE with all zero or all one stateid
3441 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3443 struct nfs4_file *fp;
3444 __be32 ret = nfs_ok;
3446 fp = find_file(¤t_fh->fh_handle);
3449 /* Check for conflicting share reservations */
3450 spin_lock(&fp->fi_lock);
3451 if (fp->fi_share_deny & deny_type)
3452 ret = nfserr_locked;
3453 spin_unlock(&fp->fi_lock);
3458 static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
3460 struct nfs4_delegation *dp = cb_to_delegation(cb);
3461 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3464 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
3467 * We can't do this in nfsd_break_deleg_cb because it is
3468 * already holding inode->i_lock.
3470 * If the dl_time != 0, then we know that it has already been
3471 * queued for a lease break. Don't queue it again.
3473 spin_lock(&state_lock);
3474 if (dp->dl_time == 0) {
3475 dp->dl_time = get_seconds();
3476 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
3478 spin_unlock(&state_lock);
3481 static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
3482 struct rpc_task *task)
3484 struct nfs4_delegation *dp = cb_to_delegation(cb);
3486 switch (task->tk_status) {
3490 case -NFS4ERR_BAD_STATEID:
3492 * Race: client probably got cb_recall before open reply
3493 * granting delegation.
3495 if (dp->dl_retries--) {
3496 rpc_delay(task, 2 * HZ);
3505 static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
3507 struct nfs4_delegation *dp = cb_to_delegation(cb);
3509 nfs4_put_stid(&dp->dl_stid);
3512 static struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
3513 .prepare = nfsd4_cb_recall_prepare,
3514 .done = nfsd4_cb_recall_done,
3515 .release = nfsd4_cb_recall_release,
3518 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3521 * We're assuming the state code never drops its reference
3522 * without first removing the lease. Since we're in this lease
3523 * callback (and since the lease code is serialized by the kernel
3524 * lock) we know the server hasn't removed the lease yet, we know
3525 * it's safe to take a reference.
3527 atomic_inc(&dp->dl_stid.sc_count);
3528 nfsd4_run_cb(&dp->dl_recall);
3531 /* Called from break_lease() with i_lock held. */
3533 nfsd_break_deleg_cb(struct file_lock *fl)
3536 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3537 struct nfs4_delegation *dp;
3540 WARN(1, "(%p)->fl_owner NULL\n", fl);
3543 if (fp->fi_had_conflict) {
3544 WARN(1, "duplicate break on %p\n", fp);
3548 * We don't want the locks code to timeout the lease for us;
3549 * we'll remove it ourself if a delegation isn't returned
3552 fl->fl_break_time = 0;
3554 spin_lock(&fp->fi_lock);
3555 fp->fi_had_conflict = true;
3557 * If there are no delegations on the list, then return true
3558 * so that the lease code will go ahead and delete it.
3560 if (list_empty(&fp->fi_delegations))
3563 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3564 nfsd_break_one_deleg(dp);
3565 spin_unlock(&fp->fi_lock);
3570 nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
3571 struct list_head *dispose)
3574 return lease_modify(onlist, arg, dispose);
3579 static const struct lock_manager_operations nfsd_lease_mng_ops = {
3580 .lm_break = nfsd_break_deleg_cb,
3581 .lm_change = nfsd_change_deleg_cb,
3584 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3586 if (nfsd4_has_session(cstate))
3588 if (seqid == so->so_seqid - 1)
3589 return nfserr_replay_me;
3590 if (seqid == so->so_seqid)
3592 return nfserr_bad_seqid;
3595 static __be32 lookup_clientid(clientid_t *clid,
3596 struct nfsd4_compound_state *cstate,
3597 struct nfsd_net *nn)
3599 struct nfs4_client *found;
3602 found = cstate->clp;
3603 if (!same_clid(&found->cl_clientid, clid))
3604 return nfserr_stale_clientid;
3608 if (STALE_CLIENTID(clid, nn))
3609 return nfserr_stale_clientid;
3612 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3613 * cached already then we know this is for is for v4.0 and "sessions"
3616 WARN_ON_ONCE(cstate->session);
3617 spin_lock(&nn->client_lock);
3618 found = find_confirmed_client(clid, false, nn);
3620 spin_unlock(&nn->client_lock);
3621 return nfserr_expired;
3623 atomic_inc(&found->cl_refcount);
3624 spin_unlock(&nn->client_lock);
3626 /* Cache the nfs4_client in cstate! */
3627 cstate->clp = found;
3632 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
3633 struct nfsd4_open *open, struct nfsd_net *nn)
3635 clientid_t *clientid = &open->op_clientid;
3636 struct nfs4_client *clp = NULL;
3637 unsigned int strhashval;
3638 struct nfs4_openowner *oo = NULL;
3641 if (STALE_CLIENTID(&open->op_clientid, nn))
3642 return nfserr_stale_clientid;
3644 * In case we need it later, after we've already created the
3645 * file and don't want to risk a further failure:
3647 open->op_file = nfsd4_alloc_file();
3648 if (open->op_file == NULL)
3649 return nfserr_jukebox;
3651 status = lookup_clientid(clientid, cstate, nn);
3656 strhashval = ownerstr_hashval(&open->op_owner);
3657 oo = find_openstateowner_str(strhashval, open, clp);
3658 open->op_openowner = oo;
3662 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
3663 /* Replace unconfirmed owners without checking for replay. */
3664 release_openowner(oo);
3665 open->op_openowner = NULL;
3668 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3673 oo = alloc_init_open_stateowner(strhashval, open, cstate);
3675 return nfserr_jukebox;
3676 open->op_openowner = oo;
3678 open->op_stp = nfs4_alloc_open_stateid(clp);
3680 return nfserr_jukebox;
3682 if (nfsd4_has_session(cstate) &&
3683 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
3684 open->op_odstate = alloc_clnt_odstate(clp);
3685 if (!open->op_odstate)
3686 return nfserr_jukebox;
3692 static inline __be32
3693 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3695 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3696 return nfserr_openmode;
3701 static int share_access_to_flags(u32 share_access)
3703 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3706 static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
3708 struct nfs4_stid *ret;
3710 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
3713 return delegstateid(ret);
3716 static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3718 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3719 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3723 nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
3724 struct nfs4_delegation **dp)
3727 __be32 status = nfserr_bad_stateid;
3728 struct nfs4_delegation *deleg;
3730 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3733 flags = share_access_to_flags(open->op_share_access);
3734 status = nfs4_check_delegmode(deleg, flags);
3736 nfs4_put_stid(&deleg->dl_stid);
3741 if (!nfsd4_is_deleg_cur(open))
3745 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
3749 static struct nfs4_ol_stateid *
3750 nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
3752 struct nfs4_ol_stateid *local, *ret = NULL;
3753 struct nfs4_openowner *oo = open->op_openowner;
3755 spin_lock(&fp->fi_lock);
3756 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
3757 /* ignore lock owners */
3758 if (local->st_stateowner->so_is_open_owner == 0)
3760 if (local->st_stateowner == &oo->oo_owner) {
3762 atomic_inc(&ret->st_stid.sc_count);
3766 spin_unlock(&fp->fi_lock);
3770 static inline int nfs4_access_to_access(u32 nfs4_access)
3774 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3775 flags |= NFSD_MAY_READ;
3776 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3777 flags |= NFSD_MAY_WRITE;
3781 static inline __be32
3782 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3783 struct nfsd4_open *open)
3785 struct iattr iattr = {
3786 .ia_valid = ATTR_SIZE,
3789 if (!open->op_truncate)
3791 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
3792 return nfserr_inval;
3793 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3796 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
3797 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3798 struct nfsd4_open *open)
3800 struct file *filp = NULL;
3802 int oflag = nfs4_access_to_omode(open->op_share_access);
3803 int access = nfs4_access_to_access(open->op_share_access);
3804 unsigned char old_access_bmap, old_deny_bmap;
3806 spin_lock(&fp->fi_lock);
3809 * Are we trying to set a deny mode that would conflict with
3812 status = nfs4_file_check_deny(fp, open->op_share_deny);
3813 if (status != nfs_ok) {
3814 spin_unlock(&fp->fi_lock);
3818 /* set access to the file */
3819 status = nfs4_file_get_access(fp, open->op_share_access);
3820 if (status != nfs_ok) {
3821 spin_unlock(&fp->fi_lock);
3825 /* Set access bits in stateid */
3826 old_access_bmap = stp->st_access_bmap;
3827 set_access(open->op_share_access, stp);
3829 /* Set new deny mask */
3830 old_deny_bmap = stp->st_deny_bmap;
3831 set_deny(open->op_share_deny, stp);
3832 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3834 if (!fp->fi_fds[oflag]) {
3835 spin_unlock(&fp->fi_lock);
3836 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
3838 goto out_put_access;
3839 spin_lock(&fp->fi_lock);
3840 if (!fp->fi_fds[oflag]) {
3841 fp->fi_fds[oflag] = filp;
3845 spin_unlock(&fp->fi_lock);
3849 status = nfsd4_truncate(rqstp, cur_fh, open);
3851 goto out_put_access;
3855 stp->st_access_bmap = old_access_bmap;
3856 nfs4_file_put_access(fp, open->op_share_access);
3857 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3862 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)
3865 unsigned char old_deny_bmap;
3867 if (!test_access(open->op_share_access, stp))
3868 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
3870 /* test and set deny mode */
3871 spin_lock(&fp->fi_lock);
3872 status = nfs4_file_check_deny(fp, open->op_share_deny);
3873 if (status == nfs_ok) {
3874 old_deny_bmap = stp->st_deny_bmap;
3875 set_deny(open->op_share_deny, stp);
3876 fp->fi_share_deny |=
3877 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3879 spin_unlock(&fp->fi_lock);
3881 if (status != nfs_ok)
3884 status = nfsd4_truncate(rqstp, cur_fh, open);
3885 if (status != nfs_ok)
3886 reset_union_bmap_deny(old_deny_bmap, stp);
3891 nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
3893 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
3896 /* Should we give out recallable state?: */
3897 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3899 if (clp->cl_cb_state == NFSD4_CB_UP)
3902 * In the sessions case, since we don't have to establish a
3903 * separate connection for callbacks, we assume it's OK
3904 * until we hear otherwise:
3906 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3909 static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
3911 struct file_lock *fl;
3913 fl = locks_alloc_lock();
3916 fl->fl_lmops = &nfsd_lease_mng_ops;
3917 fl->fl_flags = FL_DELEG;
3918 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3919 fl->fl_end = OFFSET_MAX;
3920 fl->fl_owner = (fl_owner_t)fp;
3921 fl->fl_pid = current->tgid;
3925 static int nfs4_setlease(struct nfs4_delegation *dp)
3927 struct nfs4_file *fp = dp->dl_stid.sc_file;
3928 struct file_lock *fl, *ret;
3932 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
3935 filp = find_readable_file(fp);
3937 /* We should always have a readable file here */
3943 status = vfs_setlease(filp, fl->fl_type, &fl, NULL);
3945 locks_free_lock(fl);
3948 spin_lock(&state_lock);
3949 spin_lock(&fp->fi_lock);
3950 /* Did the lease get broken before we took the lock? */
3952 if (fp->fi_had_conflict)
3955 if (fp->fi_deleg_file) {
3958 hash_delegation_locked(dp, fp);
3961 fp->fi_deleg_file = filp;
3962 fp->fi_delegees = 1;
3963 hash_delegation_locked(dp, fp);
3964 spin_unlock(&fp->fi_lock);
3965 spin_unlock(&state_lock);
3968 spin_unlock(&fp->fi_lock);
3969 spin_unlock(&state_lock);
3975 static struct nfs4_delegation *
3976 nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
3977 struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate)
3980 struct nfs4_delegation *dp;
3982 if (fp->fi_had_conflict)
3983 return ERR_PTR(-EAGAIN);
3985 dp = alloc_init_deleg(clp, fh, odstate);
3987 return ERR_PTR(-ENOMEM);
3990 spin_lock(&state_lock);
3991 spin_lock(&fp->fi_lock);
3992 dp->dl_stid.sc_file = fp;
3993 if (!fp->fi_deleg_file) {
3994 spin_unlock(&fp->fi_lock);
3995 spin_unlock(&state_lock);
3996 status = nfs4_setlease(dp);
3999 if (fp->fi_had_conflict) {
4004 hash_delegation_locked(dp, fp);
4007 spin_unlock(&fp->fi_lock);
4008 spin_unlock(&state_lock);
4011 put_clnt_odstate(dp->dl_clnt_odstate);
4012 nfs4_put_stid(&dp->dl_stid);
4013 return ERR_PTR(status);
4018 static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
4020 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4021 if (status == -EAGAIN)
4022 open->op_why_no_deleg = WND4_CONTENTION;
4024 open->op_why_no_deleg = WND4_RESOURCE;
4025 switch (open->op_deleg_want) {
4026 case NFS4_SHARE_WANT_READ_DELEG:
4027 case NFS4_SHARE_WANT_WRITE_DELEG:
4028 case NFS4_SHARE_WANT_ANY_DELEG:
4030 case NFS4_SHARE_WANT_CANCEL:
4031 open->op_why_no_deleg = WND4_CANCELLED;
4033 case NFS4_SHARE_WANT_NO_DELEG:
4040 * Attempt to hand out a delegation.
4042 * Note we don't support write delegations, and won't until the vfs has
4043 * proper support for them.
4046 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
4047 struct nfs4_ol_stateid *stp)
4049 struct nfs4_delegation *dp;
4050 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
4051 struct nfs4_client *clp = stp->st_stid.sc_client;
4055 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
4056 open->op_recall = 0;
4057 switch (open->op_claim_type) {
4058 case NFS4_OPEN_CLAIM_PREVIOUS:
4060 open->op_recall = 1;
4061 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
4064 case NFS4_OPEN_CLAIM_NULL:
4065 case NFS4_OPEN_CLAIM_FH:
4067 * Let's not give out any delegations till everyone's
4068 * had the chance to reclaim theirs....
4070 if (locks_in_grace(clp->net))
4072 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
4075 * Also, if the file was opened for write or
4076 * create, there's a good chance the client's
4077 * about to write to it, resulting in an
4078 * immediate recall (since we don't support
4079 * write delegations):
4081 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
4083 if (open->op_create == NFS4_OPEN_CREATE)
4089 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate);
4093 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
4095 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
4096 STATEID_VAL(&dp->dl_stid.sc_stateid));
4097 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
4098 nfs4_put_stid(&dp->dl_stid);
4101 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
4102 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
4103 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
4104 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
4105 open->op_recall = 1;
4108 /* 4.1 client asking for a delegation? */
4109 if (open->op_deleg_want)
4110 nfsd4_open_deleg_none_ext(open, status);
4114 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
4115 struct nfs4_delegation *dp)
4117 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
4118 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4119 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4120 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
4121 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
4122 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4123 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4124 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
4126 /* Otherwise the client must be confused wanting a delegation
4127 * it already has, therefore we don't return
4128 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
4133 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
4135 struct nfsd4_compoundres *resp = rqstp->rq_resp;
4136 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
4137 struct nfs4_file *fp = NULL;
4138 struct nfs4_ol_stateid *stp = NULL;
4139 struct nfs4_delegation *dp = NULL;
4143 * Lookup file; if found, lookup stateid and check open request,
4144 * and check for delegations in the process of being recalled.
4145 * If not found, create the nfs4_file struct
4147 fp = find_or_add_file(open->op_file, ¤t_fh->fh_handle);
4148 if (fp != open->op_file) {
4149 status = nfs4_check_deleg(cl, open, &dp);
4152 stp = nfsd4_find_existing_open(fp, open);
4154 open->op_file = NULL;
4155 status = nfserr_bad_stateid;
4156 if (nfsd4_is_deleg_cur(open))
4161 * OPEN the file, or upgrade an existing OPEN.
4162 * If truncate fails, the OPEN fails.
4165 /* Stateid was found, this is an OPEN upgrade */
4166 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
4171 open->op_stp = NULL;
4172 init_open_stateid(stp, fp, open);
4173 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4175 release_open_stateid(stp);
4179 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
4181 if (stp->st_clnt_odstate == open->op_odstate)
4182 open->op_odstate = NULL;
4184 update_stateid(&stp->st_stid.sc_stateid);
4185 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4187 if (nfsd4_has_session(&resp->cstate)) {
4188 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4189 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4190 open->op_why_no_deleg = WND4_NOT_WANTED;
4196 * Attempt to hand out a delegation. No error return, because the
4197 * OPEN succeeds even if we fail.
4199 nfs4_open_delegation(current_fh, open, stp);
4203 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
4204 STATEID_VAL(&stp->st_stid.sc_stateid));
4206 /* 4.1 client trying to upgrade/downgrade delegation? */
4207 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
4208 open->op_deleg_want)
4209 nfsd4_deleg_xgrade_none_ext(open, dp);
4213 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
4214 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
4216 * To finish the open response, we just need to set the rflags.
4218 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
4219 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
4220 !nfsd4_has_session(&resp->cstate))
4221 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
4223 nfs4_put_stid(&dp->dl_stid);
4225 nfs4_put_stid(&stp->st_stid);
4230 void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
4231 struct nfsd4_open *open)
4233 if (open->op_openowner) {
4234 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
4236 nfsd4_cstate_assign_replay(cstate, so);
4237 nfs4_put_stateowner(so);
4240 kmem_cache_free(file_slab, open->op_file);
4242 nfs4_put_stid(&open->op_stp->st_stid);
4243 if (open->op_odstate)
4244 kmem_cache_free(odstate_slab, open->op_odstate);
4248 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4251 struct nfs4_client *clp;
4253 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4255 dprintk("process_renew(%08x/%08x): starting\n",
4256 clid->cl_boot, clid->cl_id);
4257 status = lookup_clientid(clid, cstate, nn);
4261 status = nfserr_cb_path_down;
4262 if (!list_empty(&clp->cl_delegations)
4263 && clp->cl_cb_state != NFSD4_CB_UP)
4271 nfsd4_end_grace(struct nfsd_net *nn)
4273 /* do nothing if grace period already ended */
4274 if (nn->grace_ended)
4277 dprintk("NFSD: end of grace period\n");
4278 nn->grace_ended = true;
4280 * If the server goes down again right now, an NFSv4
4281 * client will still be allowed to reclaim after it comes back up,
4282 * even if it hasn't yet had a chance to reclaim state this time.
4285 nfsd4_record_grace_done(nn);
4287 * At this point, NFSv4 clients can still reclaim. But if the
4288 * server crashes, any that have not yet reclaimed will be out
4289 * of luck on the next boot.
4291 * (NFSv4.1+ clients are considered to have reclaimed once they
4292 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
4293 * have reclaimed after their first OPEN.)
4295 locks_end_grace(&nn->nfsd4_manager);
4297 * At this point, and once lockd and/or any other containers
4298 * exit their grace period, further reclaims will fail and
4299 * regular locking can resume.
4304 nfs4_laundromat(struct nfsd_net *nn)
4306 struct nfs4_client *clp;
4307 struct nfs4_openowner *oo;
4308 struct nfs4_delegation *dp;
4309 struct nfs4_ol_stateid *stp;
4310 struct list_head *pos, *next, reaplist;
4311 time_t cutoff = get_seconds() - nn->nfsd4_lease;
4312 time_t t, new_timeo = nn->nfsd4_lease;
4314 dprintk("NFSD: laundromat service - starting\n");
4315 nfsd4_end_grace(nn);
4316 INIT_LIST_HEAD(&reaplist);
4317 spin_lock(&nn->client_lock);
4318 list_for_each_safe(pos, next, &nn->client_lru) {
4319 clp = list_entry(pos, struct nfs4_client, cl_lru);
4320 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4321 t = clp->cl_time - cutoff;
4322 new_timeo = min(new_timeo, t);
4325 if (mark_client_expired_locked(clp)) {
4326 dprintk("NFSD: client in use (clientid %08x)\n",
4327 clp->cl_clientid.cl_id);
4330 list_add(&clp->cl_lru, &reaplist);
4332 spin_unlock(&nn->client_lock);
4333 list_for_each_safe(pos, next, &reaplist) {
4334 clp = list_entry(pos, struct nfs4_client, cl_lru);
4335 dprintk("NFSD: purging unused client (clientid %08x)\n",
4336 clp->cl_clientid.cl_id);
4337 list_del_init(&clp->cl_lru);
4340 spin_lock(&state_lock);
4341 list_for_each_safe(pos, next, &nn->del_recall_lru) {
4342 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4343 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
4345 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
4346 t = dp->dl_time - cutoff;
4347 new_timeo = min(new_timeo, t);
4350 unhash_delegation_locked(dp);
4351 list_add(&dp->dl_recall_lru, &reaplist);
4353 spin_unlock(&state_lock);
4354 while (!list_empty(&reaplist)) {
4355 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4357 list_del_init(&dp->dl_recall_lru);
4358 revoke_delegation(dp);
4361 spin_lock(&nn->client_lock);
4362 while (!list_empty(&nn->close_lru)) {
4363 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
4365 if (time_after((unsigned long)oo->oo_time,
4366 (unsigned long)cutoff)) {
4367 t = oo->oo_time - cutoff;
4368 new_timeo = min(new_timeo, t);
4371 list_del_init(&oo->oo_close_lru);
4372 stp = oo->oo_last_closed_stid;
4373 oo->oo_last_closed_stid = NULL;
4374 spin_unlock(&nn->client_lock);
4375 nfs4_put_stid(&stp->st_stid);
4376 spin_lock(&nn->client_lock);
4378 spin_unlock(&nn->client_lock);
4380 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
4384 static struct workqueue_struct *laundry_wq;
4385 static void laundromat_main(struct work_struct *);
4388 laundromat_main(struct work_struct *laundry)
4391 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
4393 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4396 t = nfs4_laundromat(nn);
4397 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
4398 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
4401 static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
4403 if (!fh_match(&fhp->fh_handle, &stp->st_stid.sc_file->fi_fhandle))
4404 return nfserr_bad_stateid;
4409 access_permit_read(struct nfs4_ol_stateid *stp)
4411 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4412 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4413 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
4417 access_permit_write(struct nfs4_ol_stateid *stp)
4419 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4420 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
4424 __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
4426 __be32 status = nfserr_openmode;
4428 /* For lock stateid's, we test the parent open, not the lock: */
4429 if (stp->st_openstp)
4430 stp = stp->st_openstp;
4431 if ((flags & WR_STATE) && !access_permit_write(stp))
4433 if ((flags & RD_STATE) && !access_permit_read(stp))
4440 static inline __be32
4441 check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
4443 if (ONE_STATEID(stateid) && (flags & RD_STATE))
4445 else if (locks_in_grace(net)) {
4446 /* Answer in remaining cases depends on existence of
4447 * conflicting state; so we must wait out the grace period. */
4448 return nfserr_grace;
4449 } else if (flags & WR_STATE)
4450 return nfs4_share_conflict(current_fh,
4451 NFS4_SHARE_DENY_WRITE);
4452 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4453 return nfs4_share_conflict(current_fh,
4454 NFS4_SHARE_DENY_READ);
4458 * Allow READ/WRITE during grace period on recovered state only for files
4459 * that are not able to provide mandatory locking.
4462 grace_disallows_io(struct net *net, struct inode *inode)
4464 return locks_in_grace(net) && mandatory_lock(inode);
4467 /* Returns true iff a is later than b: */
4468 static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4470 return (s32)(a->si_generation - b->si_generation) > 0;
4473 static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
4476 * When sessions are used the stateid generation number is ignored
4479 if (has_session && in->si_generation == 0)
4482 if (in->si_generation == ref->si_generation)
4485 /* If the client sends us a stateid from the future, it's buggy: */
4486 if (stateid_generation_after(in, ref))
4487 return nfserr_bad_stateid;
4489 * However, we could see a stateid from the past, even from a
4490 * non-buggy client. For example, if the client sends a lock
4491 * while some IO is outstanding, the lock may bump si_generation
4492 * while the IO is still in flight. The client could avoid that
4493 * situation by waiting for responses on all the IO requests,
4494 * but better performance may result in retrying IO that
4495 * receives an old_stateid error if requests are rarely
4496 * reordered in flight:
4498 return nfserr_old_stateid;
4501 static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
4503 if (ols->st_stateowner->so_is_open_owner &&
4504 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
4505 return nfserr_bad_stateid;
4509 static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
4511 struct nfs4_stid *s;
4512 __be32 status = nfserr_bad_stateid;
4514 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4516 /* Client debugging aid. */
4517 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4518 char addr_str[INET6_ADDRSTRLEN];
4519 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4521 pr_warn_ratelimited("NFSD: client %s testing state ID "
4522 "with incorrect client ID\n", addr_str);
4525 spin_lock(&cl->cl_lock);
4526 s = find_stateid_locked(cl, stateid);
4529 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
4532 switch (s->sc_type) {
4533 case NFS4_DELEG_STID:
4536 case NFS4_REVOKED_DELEG_STID:
4537 status = nfserr_deleg_revoked;
4539 case NFS4_OPEN_STID:
4540 case NFS4_LOCK_STID:
4541 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
4544 printk("unknown stateid type %x\n", s->sc_type);
4546 case NFS4_CLOSED_STID:
4547 case NFS4_CLOSED_DELEG_STID:
4548 status = nfserr_bad_stateid;
4551 spin_unlock(&cl->cl_lock);
4556 nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4557 stateid_t *stateid, unsigned char typemask,
4558 struct nfs4_stid **s, struct nfsd_net *nn)
4562 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4563 return nfserr_bad_stateid;
4564 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
4565 if (status == nfserr_stale_clientid) {
4566 if (cstate->session)
4567 return nfserr_bad_stateid;
4568 return nfserr_stale_stateid;
4572 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
4574 return nfserr_bad_stateid;
4579 * Checks for stateid operations
4582 nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
4583 stateid_t *stateid, int flags, struct file **filpp)
4585 struct nfs4_stid *s;
4586 struct nfs4_ol_stateid *stp = NULL;
4587 struct nfs4_delegation *dp = NULL;
4588 struct svc_fh *current_fh = &cstate->current_fh;
4589 struct inode *ino = d_inode(current_fh->fh_dentry);
4590 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4591 struct file *file = NULL;
4597 if (grace_disallows_io(net, ino))
4598 return nfserr_grace;
4600 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4601 return check_special_stateids(net, current_fh, stateid, flags);
4603 status = nfsd4_lookup_stateid(cstate, stateid,
4604 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
4608 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4611 switch (s->sc_type) {
4612 case NFS4_DELEG_STID:
4613 dp = delegstateid(s);
4614 status = nfs4_check_delegmode(dp, flags);
4618 file = dp->dl_stid.sc_file->fi_deleg_file;
4621 status = nfserr_serverfault;
4627 case NFS4_OPEN_STID:
4628 case NFS4_LOCK_STID:
4629 stp = openlockstateid(s);
4630 status = nfs4_check_fh(current_fh, stp);
4633 status = nfsd4_check_openowner_confirmed(stp);
4636 status = nfs4_check_openmode(stp, flags);
4640 struct nfs4_file *fp = stp->st_stid.sc_file;
4642 if (flags & RD_STATE)
4643 file = find_readable_file(fp);
4645 file = find_writeable_file(fp);
4649 status = nfserr_bad_stateid;
4661 * Test if the stateid is valid
4664 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4665 struct nfsd4_test_stateid *test_stateid)
4667 struct nfsd4_test_stateid_id *stateid;
4668 struct nfs4_client *cl = cstate->session->se_client;
4670 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
4671 stateid->ts_id_status =
4672 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
4678 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4679 struct nfsd4_free_stateid *free_stateid)
4681 stateid_t *stateid = &free_stateid->fr_stateid;
4682 struct nfs4_stid *s;
4683 struct nfs4_delegation *dp;
4684 struct nfs4_ol_stateid *stp;
4685 struct nfs4_client *cl = cstate->session->se_client;
4686 __be32 ret = nfserr_bad_stateid;
4688 spin_lock(&cl->cl_lock);
4689 s = find_stateid_locked(cl, stateid);
4692 switch (s->sc_type) {
4693 case NFS4_DELEG_STID:
4694 ret = nfserr_locks_held;
4696 case NFS4_OPEN_STID:
4697 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4700 ret = nfserr_locks_held;
4702 case NFS4_LOCK_STID:
4703 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4706 stp = openlockstateid(s);
4707 ret = nfserr_locks_held;
4708 if (check_for_locks(stp->st_stid.sc_file,
4709 lockowner(stp->st_stateowner)))
4711 unhash_lock_stateid(stp);
4712 spin_unlock(&cl->cl_lock);
4716 case NFS4_REVOKED_DELEG_STID:
4717 dp = delegstateid(s);
4718 list_del_init(&dp->dl_recall_lru);
4719 spin_unlock(&cl->cl_lock);
4723 /* Default falls through and returns nfserr_bad_stateid */
4726 spin_unlock(&cl->cl_lock);
4734 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4735 RD_STATE : WR_STATE;
4738 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
4740 struct svc_fh *current_fh = &cstate->current_fh;
4741 struct nfs4_stateowner *sop = stp->st_stateowner;
4744 status = nfsd4_check_seqid(cstate, sop, seqid);
4747 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4748 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
4750 * "Closed" stateid's exist *only* to return
4751 * nfserr_replay_me from the previous step, and
4752 * revoked delegations are kept only for free_stateid.
4754 return nfserr_bad_stateid;
4755 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4758 return nfs4_check_fh(current_fh, stp);
4762 * Checks for sequence id mutating operations.
4765 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4766 stateid_t *stateid, char typemask,
4767 struct nfs4_ol_stateid **stpp,
4768 struct nfsd_net *nn)
4771 struct nfs4_stid *s;
4772 struct nfs4_ol_stateid *stp = NULL;
4774 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4775 seqid, STATEID_VAL(stateid));
4778 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
4781 stp = openlockstateid(s);
4782 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
4784 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
4788 nfs4_put_stid(&stp->st_stid);
4792 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4793 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
4796 struct nfs4_openowner *oo;
4797 struct nfs4_ol_stateid *stp;
4799 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
4800 NFS4_OPEN_STID, &stp, nn);
4803 oo = openowner(stp->st_stateowner);
4804 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4805 nfs4_put_stid(&stp->st_stid);
4806 return nfserr_bad_stateid;
4813 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4814 struct nfsd4_open_confirm *oc)
4817 struct nfs4_openowner *oo;
4818 struct nfs4_ol_stateid *stp;
4819 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4821 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4822 cstate->current_fh.fh_dentry);
4824 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
4828 status = nfs4_preprocess_seqid_op(cstate,
4829 oc->oc_seqid, &oc->oc_req_stateid,
4830 NFS4_OPEN_STID, &stp, nn);
4833 oo = openowner(stp->st_stateowner);
4834 status = nfserr_bad_stateid;
4835 if (oo->oo_flags & NFS4_OO_CONFIRMED)
4837 oo->oo_flags |= NFS4_OO_CONFIRMED;
4838 update_stateid(&stp->st_stid.sc_stateid);
4839 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4840 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
4841 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
4843 nfsd4_client_record_create(oo->oo_owner.so_client);
4846 nfs4_put_stid(&stp->st_stid);
4848 nfsd4_bump_seqid(cstate, status);
4852 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
4854 if (!test_access(access, stp))
4856 nfs4_file_put_access(stp->st_stid.sc_file, access);
4857 clear_access(access, stp);
4860 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4862 switch (to_access) {
4863 case NFS4_SHARE_ACCESS_READ:
4864 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4865 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4867 case NFS4_SHARE_ACCESS_WRITE:
4868 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4869 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4871 case NFS4_SHARE_ACCESS_BOTH:
4879 nfsd4_open_downgrade(struct svc_rqst *rqstp,
4880 struct nfsd4_compound_state *cstate,
4881 struct nfsd4_open_downgrade *od)
4884 struct nfs4_ol_stateid *stp;
4885 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4887 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4888 cstate->current_fh.fh_dentry);
4890 /* We don't yet support WANT bits: */
4891 if (od->od_deleg_want)
4892 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4895 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
4896 &od->od_stateid, &stp, nn);
4899 status = nfserr_inval;
4900 if (!test_access(od->od_share_access, stp)) {
4901 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
4902 stp->st_access_bmap, od->od_share_access);
4905 if (!test_deny(od->od_share_deny, stp)) {
4906 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
4907 stp->st_deny_bmap, od->od_share_deny);
4910 nfs4_stateid_downgrade(stp, od->od_share_access);
4912 reset_union_bmap_deny(od->od_share_deny, stp);
4914 update_stateid(&stp->st_stid.sc_stateid);
4915 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4918 nfs4_put_stid(&stp->st_stid);
4920 nfsd4_bump_seqid(cstate, status);
4924 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4926 struct nfs4_client *clp = s->st_stid.sc_client;
4927 LIST_HEAD(reaplist);
4929 s->st_stid.sc_type = NFS4_CLOSED_STID;
4930 spin_lock(&clp->cl_lock);
4931 unhash_open_stateid(s, &reaplist);
4933 if (clp->cl_minorversion) {
4934 put_ol_stateid_locked(s, &reaplist);
4935 spin_unlock(&clp->cl_lock);
4936 free_ol_stateid_reaplist(&reaplist);
4938 spin_unlock(&clp->cl_lock);
4939 free_ol_stateid_reaplist(&reaplist);
4940 move_to_close_lru(s, clp->net);
4945 * nfs4_unlock_state() called after encode
4948 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4949 struct nfsd4_close *close)
4952 struct nfs4_ol_stateid *stp;
4953 struct net *net = SVC_NET(rqstp);
4954 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4956 dprintk("NFSD: nfsd4_close on file %pd\n",
4957 cstate->current_fh.fh_dentry);
4959 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4961 NFS4_OPEN_STID|NFS4_CLOSED_STID,
4963 nfsd4_bump_seqid(cstate, status);
4966 update_stateid(&stp->st_stid.sc_stateid);
4967 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4969 nfsd4_close_open_stateid(stp);
4971 /* put reference from nfs4_preprocess_seqid_op */
4972 nfs4_put_stid(&stp->st_stid);
4978 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4979 struct nfsd4_delegreturn *dr)
4981 struct nfs4_delegation *dp;
4982 stateid_t *stateid = &dr->dr_stateid;
4983 struct nfs4_stid *s;
4985 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4987 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
4990 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
4993 dp = delegstateid(s);
4994 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
4998 destroy_delegation(dp);
5000 nfs4_put_stid(&dp->dl_stid);
5006 #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
5009 end_offset(u64 start, u64 len)
5014 return end >= start ? end: NFS4_MAX_UINT64;
5017 /* last octet in a range */
5019 last_byte_offset(u64 start, u64 len)
5025 return end > start ? end - 1: NFS4_MAX_UINT64;
5029 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
5030 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
5031 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
5032 * locking, this prevents us from being completely protocol-compliant. The
5033 * real solution to this problem is to start using unsigned file offsets in
5034 * the VFS, but this is a very deep change!
5037 nfs4_transform_lock_offset(struct file_lock *lock)
5039 if (lock->fl_start < 0)
5040 lock->fl_start = OFFSET_MAX;
5041 if (lock->fl_end < 0)
5042 lock->fl_end = OFFSET_MAX;
5046 nfsd4_fl_get_owner(fl_owner_t owner)
5048 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
5050 nfs4_get_stateowner(&lo->lo_owner);
5055 nfsd4_fl_put_owner(fl_owner_t owner)
5057 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
5060 nfs4_put_stateowner(&lo->lo_owner);
5063 static const struct lock_manager_operations nfsd_posix_mng_ops = {
5064 .lm_get_owner = nfsd4_fl_get_owner,
5065 .lm_put_owner = nfsd4_fl_put_owner,
5069 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
5071 struct nfs4_lockowner *lo;
5073 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
5074 lo = (struct nfs4_lockowner *) fl->fl_owner;
5075 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
5076 lo->lo_owner.so_owner.len, GFP_KERNEL);
5077 if (!deny->ld_owner.data)
5078 /* We just don't care that much */
5080 deny->ld_owner.len = lo->lo_owner.so_owner.len;
5081 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
5084 deny->ld_owner.len = 0;
5085 deny->ld_owner.data = NULL;
5086 deny->ld_clientid.cl_boot = 0;
5087 deny->ld_clientid.cl_id = 0;
5089 deny->ld_start = fl->fl_start;
5090 deny->ld_length = NFS4_MAX_UINT64;
5091 if (fl->fl_end != NFS4_MAX_UINT64)
5092 deny->ld_length = fl->fl_end - fl->fl_start + 1;
5093 deny->ld_type = NFS4_READ_LT;
5094 if (fl->fl_type != F_RDLCK)
5095 deny->ld_type = NFS4_WRITE_LT;
5098 static struct nfs4_lockowner *
5099 find_lockowner_str_locked(clientid_t *clid, struct xdr_netobj *owner,
5100 struct nfs4_client *clp)
5102 unsigned int strhashval = ownerstr_hashval(owner);
5103 struct nfs4_stateowner *so;
5105 lockdep_assert_held(&clp->cl_lock);
5107 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
5109 if (so->so_is_open_owner)
5111 if (same_owner_str(so, owner))
5112 return lockowner(nfs4_get_stateowner(so));
5117 static struct nfs4_lockowner *
5118 find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
5119 struct nfs4_client *clp)
5121 struct nfs4_lockowner *lo;
5123 spin_lock(&clp->cl_lock);
5124 lo = find_lockowner_str_locked(clid, owner, clp);
5125 spin_unlock(&clp->cl_lock);
5129 static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
5131 unhash_lockowner_locked(lockowner(sop));
5134 static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
5136 struct nfs4_lockowner *lo = lockowner(sop);
5138 kmem_cache_free(lockowner_slab, lo);
5141 static const struct nfs4_stateowner_operations lockowner_ops = {
5142 .so_unhash = nfs4_unhash_lockowner,
5143 .so_free = nfs4_free_lockowner,
5147 * Alloc a lock owner structure.
5148 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
5151 * strhashval = ownerstr_hashval
5153 static struct nfs4_lockowner *
5154 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
5155 struct nfs4_ol_stateid *open_stp,
5156 struct nfsd4_lock *lock)
5158 struct nfs4_lockowner *lo, *ret;
5160 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
5163 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
5164 lo->lo_owner.so_is_open_owner = 0;
5165 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
5166 lo->lo_owner.so_ops = &lockowner_ops;
5167 spin_lock(&clp->cl_lock);
5168 ret = find_lockowner_str_locked(&clp->cl_clientid,
5169 &lock->lk_new_owner, clp);
5171 list_add(&lo->lo_owner.so_strhash,
5172 &clp->cl_ownerstr_hashtbl[strhashval]);
5175 nfs4_free_lockowner(&lo->lo_owner);
5176 spin_unlock(&clp->cl_lock);
5181 init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
5182 struct nfs4_file *fp, struct inode *inode,
5183 struct nfs4_ol_stateid *open_stp)
5185 struct nfs4_client *clp = lo->lo_owner.so_client;
5187 lockdep_assert_held(&clp->cl_lock);
5189 atomic_inc(&stp->st_stid.sc_count);
5190 stp->st_stid.sc_type = NFS4_LOCK_STID;
5191 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
5193 stp->st_stid.sc_file = fp;
5194 stp->st_stid.sc_free = nfs4_free_lock_stateid;
5195 stp->st_access_bmap = 0;
5196 stp->st_deny_bmap = open_stp->st_deny_bmap;
5197 stp->st_openstp = open_stp;
5198 list_add(&stp->st_locks, &open_stp->st_locks);
5199 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
5200 spin_lock(&fp->fi_lock);
5201 list_add(&stp->st_perfile, &fp->fi_stateids);
5202 spin_unlock(&fp->fi_lock);
5205 static struct nfs4_ol_stateid *
5206 find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5208 struct nfs4_ol_stateid *lst;
5209 struct nfs4_client *clp = lo->lo_owner.so_client;
5211 lockdep_assert_held(&clp->cl_lock);
5213 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
5214 if (lst->st_stid.sc_file == fp) {
5215 atomic_inc(&lst->st_stid.sc_count);
5222 static struct nfs4_ol_stateid *
5223 find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5224 struct inode *inode, struct nfs4_ol_stateid *ost,
5227 struct nfs4_stid *ns = NULL;
5228 struct nfs4_ol_stateid *lst;
5229 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5230 struct nfs4_client *clp = oo->oo_owner.so_client;
5232 spin_lock(&clp->cl_lock);
5233 lst = find_lock_stateid(lo, fi);
5235 spin_unlock(&clp->cl_lock);
5236 ns = nfs4_alloc_stid(clp, stateid_slab);
5240 spin_lock(&clp->cl_lock);
5241 lst = find_lock_stateid(lo, fi);
5243 lst = openlockstateid(ns);
5244 init_lock_stateid(lst, lo, fi, inode, ost);
5249 spin_unlock(&clp->cl_lock);
5256 check_lock_length(u64 offset, u64 length)
5258 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
5259 LOFF_OVERFLOW(offset, length)));
5262 static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
5264 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
5266 lockdep_assert_held(&fp->fi_lock);
5268 if (test_access(access, lock_stp))
5270 __nfs4_file_get_access(fp, access);
5271 set_access(access, lock_stp);
5275 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5276 struct nfs4_ol_stateid *ost,
5277 struct nfsd4_lock *lock,
5278 struct nfs4_ol_stateid **lst, bool *new)
5281 struct nfs4_file *fi = ost->st_stid.sc_file;
5282 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5283 struct nfs4_client *cl = oo->oo_owner.so_client;
5284 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
5285 struct nfs4_lockowner *lo;
5286 unsigned int strhashval;
5288 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, cl);
5290 strhashval = ownerstr_hashval(&lock->v.new.owner);
5291 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5293 return nfserr_jukebox;
5295 /* with an existing lockowner, seqids must be the same */
5296 status = nfserr_bad_seqid;
5297 if (!cstate->minorversion &&
5298 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
5302 *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
5304 status = nfserr_jukebox;
5309 nfs4_put_stateowner(&lo->lo_owner);
5317 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5318 struct nfsd4_lock *lock)
5320 struct nfs4_openowner *open_sop = NULL;
5321 struct nfs4_lockowner *lock_sop = NULL;
5322 struct nfs4_ol_stateid *lock_stp = NULL;
5323 struct nfs4_ol_stateid *open_stp = NULL;
5324 struct nfs4_file *fp;
5325 struct file *filp = NULL;
5326 struct file_lock *file_lock = NULL;
5327 struct file_lock *conflock = NULL;
5332 struct net *net = SVC_NET(rqstp);
5333 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5335 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5336 (long long) lock->lk_offset,
5337 (long long) lock->lk_length);
5339 if (check_lock_length(lock->lk_offset, lock->lk_length))
5340 return nfserr_inval;
5342 if ((status = fh_verify(rqstp, &cstate->current_fh,
5343 S_IFREG, NFSD_MAY_LOCK))) {
5344 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5348 if (lock->lk_is_new) {
5349 if (nfsd4_has_session(cstate))
5350 /* See rfc 5661 18.10.3: given clientid is ignored: */
5351 memcpy(&lock->v.new.clientid,
5352 &cstate->session->se_client->cl_clientid,
5353 sizeof(clientid_t));
5355 status = nfserr_stale_clientid;
5356 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
5359 /* validate and update open stateid and open seqid */
5360 status = nfs4_preprocess_confirmed_seqid_op(cstate,
5361 lock->lk_new_open_seqid,
5362 &lock->lk_new_open_stateid,
5366 open_sop = openowner(open_stp->st_stateowner);
5367 status = nfserr_bad_stateid;
5368 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
5369 &lock->v.new.clientid))
5371 status = lookup_or_create_lock_state(cstate, open_stp, lock,
5374 status = nfs4_preprocess_seqid_op(cstate,
5375 lock->lk_old_lock_seqid,
5376 &lock->lk_old_lock_stateid,
5377 NFS4_LOCK_STID, &lock_stp, nn);
5381 lock_sop = lockowner(lock_stp->st_stateowner);
5383 lkflg = setlkflg(lock->lk_type);
5384 status = nfs4_check_openmode(lock_stp, lkflg);
5388 status = nfserr_grace;
5389 if (locks_in_grace(net) && !lock->lk_reclaim)
5391 status = nfserr_no_grace;
5392 if (!locks_in_grace(net) && lock->lk_reclaim)
5395 file_lock = locks_alloc_lock();
5397 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5398 status = nfserr_jukebox;
5402 fp = lock_stp->st_stid.sc_file;
5403 switch (lock->lk_type) {
5406 spin_lock(&fp->fi_lock);
5407 filp = find_readable_file_locked(fp);
5409 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
5410 spin_unlock(&fp->fi_lock);
5411 file_lock->fl_type = F_RDLCK;
5414 case NFS4_WRITEW_LT:
5415 spin_lock(&fp->fi_lock);
5416 filp = find_writeable_file_locked(fp);
5418 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
5419 spin_unlock(&fp->fi_lock);
5420 file_lock->fl_type = F_WRLCK;
5423 status = nfserr_inval;
5427 status = nfserr_openmode;
5431 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
5432 file_lock->fl_pid = current->tgid;
5433 file_lock->fl_file = filp;
5434 file_lock->fl_flags = FL_POSIX;
5435 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5436 file_lock->fl_start = lock->lk_offset;
5437 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5438 nfs4_transform_lock_offset(file_lock);
5440 conflock = locks_alloc_lock();
5442 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5443 status = nfserr_jukebox;
5447 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
5449 case 0: /* success! */
5450 update_stateid(&lock_stp->st_stid.sc_stateid);
5451 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
5455 case (EAGAIN): /* conflock holds conflicting lock */
5456 status = nfserr_denied;
5457 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
5458 nfs4_set_lock_denied(conflock, &lock->lk_denied);
5461 status = nfserr_deadlock;
5464 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
5465 status = nfserrno(err);
5472 /* Bump seqid manually if the 4.0 replay owner is openowner */
5473 if (cstate->replay_owner &&
5474 cstate->replay_owner != &lock_sop->lo_owner &&
5475 seqid_mutating_err(ntohl(status)))
5476 lock_sop->lo_owner.so_seqid++;
5479 * If this is a new, never-before-used stateid, and we are
5480 * returning an error, then just go ahead and release it.
5483 release_lock_stateid(lock_stp);
5485 nfs4_put_stid(&lock_stp->st_stid);
5488 nfs4_put_stid(&open_stp->st_stid);
5489 nfsd4_bump_seqid(cstate, status);
5491 locks_free_lock(file_lock);
5493 locks_free_lock(conflock);
5498 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5499 * so we do a temporary open here just to get an open file to pass to
5500 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5503 static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
5506 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5508 err = nfserrno(vfs_test_lock(file, lock));
5518 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5519 struct nfsd4_lockt *lockt)
5521 struct file_lock *file_lock = NULL;
5522 struct nfs4_lockowner *lo = NULL;
5524 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5526 if (locks_in_grace(SVC_NET(rqstp)))
5527 return nfserr_grace;
5529 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5530 return nfserr_inval;
5532 if (!nfsd4_has_session(cstate)) {
5533 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
5538 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
5541 file_lock = locks_alloc_lock();
5543 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5544 status = nfserr_jukebox;
5548 switch (lockt->lt_type) {
5551 file_lock->fl_type = F_RDLCK;
5554 case NFS4_WRITEW_LT:
5555 file_lock->fl_type = F_WRLCK;
5558 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
5559 status = nfserr_inval;
5563 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner,
5566 file_lock->fl_owner = (fl_owner_t)lo;
5567 file_lock->fl_pid = current->tgid;
5568 file_lock->fl_flags = FL_POSIX;
5570 file_lock->fl_start = lockt->lt_offset;
5571 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
5573 nfs4_transform_lock_offset(file_lock);
5575 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
5579 if (file_lock->fl_type != F_UNLCK) {
5580 status = nfserr_denied;
5581 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
5585 nfs4_put_stateowner(&lo->lo_owner);
5587 locks_free_lock(file_lock);
5592 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5593 struct nfsd4_locku *locku)
5595 struct nfs4_ol_stateid *stp;
5596 struct file *filp = NULL;
5597 struct file_lock *file_lock = NULL;
5600 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5602 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5603 (long long) locku->lu_offset,
5604 (long long) locku->lu_length);
5606 if (check_lock_length(locku->lu_offset, locku->lu_length))
5607 return nfserr_inval;
5609 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
5610 &locku->lu_stateid, NFS4_LOCK_STID,
5614 filp = find_any_file(stp->st_stid.sc_file);
5616 status = nfserr_lock_range;
5619 file_lock = locks_alloc_lock();
5621 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5622 status = nfserr_jukebox;
5626 file_lock->fl_type = F_UNLCK;
5627 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
5628 file_lock->fl_pid = current->tgid;
5629 file_lock->fl_file = filp;
5630 file_lock->fl_flags = FL_POSIX;
5631 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5632 file_lock->fl_start = locku->lu_offset;
5634 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5636 nfs4_transform_lock_offset(file_lock);
5638 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
5640 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
5643 update_stateid(&stp->st_stid.sc_stateid);
5644 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
5648 nfs4_put_stid(&stp->st_stid);
5650 nfsd4_bump_seqid(cstate, status);
5652 locks_free_lock(file_lock);
5656 status = nfserrno(err);
5662 * true: locks held by lockowner
5663 * false: no locks held by lockowner
5666 check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
5668 struct file_lock *fl;
5670 struct file *filp = find_any_file(fp);
5671 struct inode *inode;
5672 struct file_lock_context *flctx;
5675 /* Any valid lock stateid should have some sort of access */
5680 inode = file_inode(filp);
5681 flctx = inode->i_flctx;
5683 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
5684 spin_lock(&flctx->flc_lock);
5685 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
5686 if (fl->fl_owner == (fl_owner_t)lowner) {
5691 spin_unlock(&flctx->flc_lock);
5698 nfsd4_release_lockowner(struct svc_rqst *rqstp,
5699 struct nfsd4_compound_state *cstate,
5700 struct nfsd4_release_lockowner *rlockowner)
5702 clientid_t *clid = &rlockowner->rl_clientid;
5703 struct nfs4_stateowner *sop;
5704 struct nfs4_lockowner *lo = NULL;
5705 struct nfs4_ol_stateid *stp;
5706 struct xdr_netobj *owner = &rlockowner->rl_owner;
5707 unsigned int hashval = ownerstr_hashval(owner);
5709 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5710 struct nfs4_client *clp;
5712 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5713 clid->cl_boot, clid->cl_id);
5715 status = lookup_clientid(clid, cstate, nn);
5720 /* Find the matching lock stateowner */
5721 spin_lock(&clp->cl_lock);
5722 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
5725 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
5728 /* see if there are still any locks associated with it */
5729 lo = lockowner(sop);
5730 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5731 if (check_for_locks(stp->st_stid.sc_file, lo)) {
5732 status = nfserr_locks_held;
5733 spin_unlock(&clp->cl_lock);
5738 nfs4_get_stateowner(sop);
5741 spin_unlock(&clp->cl_lock);
5743 release_lockowner(lo);
5747 static inline struct nfs4_client_reclaim *
5750 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
5754 nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
5756 struct nfs4_client_reclaim *crp;
5758 crp = nfsd4_find_reclaim_client(name, nn);
5759 return (crp && crp->cr_clp);
5763 * failure => all reset bets are off, nfserr_no_grace...
5765 struct nfs4_client_reclaim *
5766 nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
5768 unsigned int strhashval;
5769 struct nfs4_client_reclaim *crp;
5771 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5772 crp = alloc_reclaim();
5774 strhashval = clientstr_hashval(name);
5775 INIT_LIST_HEAD(&crp->cr_strhash);
5776 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
5777 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
5779 nn->reclaim_str_hashtbl_size++;
5785 nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
5787 list_del(&crp->cr_strhash);
5789 nn->reclaim_str_hashtbl_size--;
5793 nfs4_release_reclaim(struct nfsd_net *nn)
5795 struct nfs4_client_reclaim *crp = NULL;
5798 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5799 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5800 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
5801 struct nfs4_client_reclaim, cr_strhash);
5802 nfs4_remove_reclaim_record(crp, nn);
5805 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
5809 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
5810 struct nfs4_client_reclaim *
5811 nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
5813 unsigned int strhashval;
5814 struct nfs4_client_reclaim *crp = NULL;
5816 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
5818 strhashval = clientstr_hashval(recdir);
5819 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
5820 if (same_name(crp->cr_recdir, recdir)) {
5828 * Called from OPEN. Look for clientid in reclaim list.
5831 nfs4_check_open_reclaim(clientid_t *clid,
5832 struct nfsd4_compound_state *cstate,
5833 struct nfsd_net *nn)
5837 /* find clientid in conf_id_hashtbl */
5838 status = lookup_clientid(clid, cstate, nn);
5840 return nfserr_reclaim_bad;
5842 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
5843 return nfserr_no_grace;
5845 if (nfsd4_client_record_check(cstate->clp))
5846 return nfserr_reclaim_bad;
5851 #ifdef CONFIG_NFSD_FAULT_INJECTION
5853 put_client(struct nfs4_client *clp)
5855 atomic_dec(&clp->cl_refcount);
5858 static struct nfs4_client *
5859 nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5861 struct nfs4_client *clp;
5862 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5865 if (!nfsd_netns_ready(nn))
5868 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5869 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5876 nfsd_inject_print_clients(void)
5878 struct nfs4_client *clp;
5880 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5882 char buf[INET6_ADDRSTRLEN];
5884 if (!nfsd_netns_ready(nn))
5887 spin_lock(&nn->client_lock);
5888 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5889 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5890 pr_info("NFS Client: %s\n", buf);
5893 spin_unlock(&nn->client_lock);
5899 nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
5902 struct nfs4_client *clp;
5903 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5906 if (!nfsd_netns_ready(nn))
5909 spin_lock(&nn->client_lock);
5910 clp = nfsd_find_client(addr, addr_size);
5912 if (mark_client_expired_locked(clp) == nfs_ok)
5917 spin_unlock(&nn->client_lock);
5926 nfsd_inject_forget_clients(u64 max)
5929 struct nfs4_client *clp, *next;
5930 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5932 LIST_HEAD(reaplist);
5934 if (!nfsd_netns_ready(nn))
5937 spin_lock(&nn->client_lock);
5938 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
5939 if (mark_client_expired_locked(clp) == nfs_ok) {
5940 list_add(&clp->cl_lru, &reaplist);
5941 if (max != 0 && ++count >= max)
5945 spin_unlock(&nn->client_lock);
5947 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
5953 static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5956 char buf[INET6_ADDRSTRLEN];
5957 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5958 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5962 nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
5963 struct list_head *collect)
5965 struct nfs4_client *clp = lst->st_stid.sc_client;
5966 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5972 lockdep_assert_held(&nn->client_lock);
5973 atomic_inc(&clp->cl_refcount);
5974 list_add(&lst->st_locks, collect);
5977 static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
5978 struct list_head *collect,
5979 void (*func)(struct nfs4_ol_stateid *))
5981 struct nfs4_openowner *oop;
5982 struct nfs4_ol_stateid *stp, *st_next;
5983 struct nfs4_ol_stateid *lst, *lst_next;
5986 spin_lock(&clp->cl_lock);
5987 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
5988 list_for_each_entry_safe(stp, st_next,
5989 &oop->oo_owner.so_stateids, st_perstateowner) {
5990 list_for_each_entry_safe(lst, lst_next,
5991 &stp->st_locks, st_locks) {
5994 nfsd_inject_add_lock_to_list(lst,
5999 * Despite the fact that these functions deal
6000 * with 64-bit integers for "count", we must
6001 * ensure that it doesn't blow up the
6002 * clp->cl_refcount. Throw a warning if we
6003 * start to approach INT_MAX here.
6005 WARN_ON_ONCE(count == (INT_MAX / 2));
6012 spin_unlock(&clp->cl_lock);
6018 nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
6021 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
6025 nfsd_print_client_locks(struct nfs4_client *clp)
6027 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
6028 nfsd_print_count(clp, count, "locked files");
6033 nfsd_inject_print_locks(void)
6035 struct nfs4_client *clp;
6037 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6040 if (!nfsd_netns_ready(nn))
6043 spin_lock(&nn->client_lock);
6044 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6045 count += nfsd_print_client_locks(clp);
6046 spin_unlock(&nn->client_lock);
6052 nfsd_reap_locks(struct list_head *reaplist)
6054 struct nfs4_client *clp;
6055 struct nfs4_ol_stateid *stp, *next;
6057 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
6058 list_del_init(&stp->st_locks);
6059 clp = stp->st_stid.sc_client;
6060 nfs4_put_stid(&stp->st_stid);
6066 nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
6068 unsigned int count = 0;
6069 struct nfs4_client *clp;
6070 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6072 LIST_HEAD(reaplist);
6074 if (!nfsd_netns_ready(nn))
6077 spin_lock(&nn->client_lock);
6078 clp = nfsd_find_client(addr, addr_size);
6080 count = nfsd_collect_client_locks(clp, &reaplist, 0);
6081 spin_unlock(&nn->client_lock);
6082 nfsd_reap_locks(&reaplist);
6087 nfsd_inject_forget_locks(u64 max)
6090 struct nfs4_client *clp;
6091 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6093 LIST_HEAD(reaplist);
6095 if (!nfsd_netns_ready(nn))
6098 spin_lock(&nn->client_lock);
6099 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6100 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
6101 if (max != 0 && count >= max)
6104 spin_unlock(&nn->client_lock);
6105 nfsd_reap_locks(&reaplist);
6110 nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
6111 struct list_head *collect,
6112 void (*func)(struct nfs4_openowner *))
6114 struct nfs4_openowner *oop, *next;
6115 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6119 lockdep_assert_held(&nn->client_lock);
6121 spin_lock(&clp->cl_lock);
6122 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
6126 atomic_inc(&clp->cl_refcount);
6127 list_add(&oop->oo_perclient, collect);
6132 * Despite the fact that these functions deal with
6133 * 64-bit integers for "count", we must ensure that
6134 * it doesn't blow up the clp->cl_refcount. Throw a
6135 * warning if we start to approach INT_MAX here.
6137 WARN_ON_ONCE(count == (INT_MAX / 2));
6141 spin_unlock(&clp->cl_lock);
6147 nfsd_print_client_openowners(struct nfs4_client *clp)
6149 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
6151 nfsd_print_count(clp, count, "openowners");
6156 nfsd_collect_client_openowners(struct nfs4_client *clp,
6157 struct list_head *collect, u64 max)
6159 return nfsd_foreach_client_openowner(clp, max, collect,
6160 unhash_openowner_locked);
6164 nfsd_inject_print_openowners(void)
6166 struct nfs4_client *clp;
6168 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6171 if (!nfsd_netns_ready(nn))
6174 spin_lock(&nn->client_lock);
6175 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6176 count += nfsd_print_client_openowners(clp);
6177 spin_unlock(&nn->client_lock);
6183 nfsd_reap_openowners(struct list_head *reaplist)
6185 struct nfs4_client *clp;
6186 struct nfs4_openowner *oop, *next;
6188 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
6189 list_del_init(&oop->oo_perclient);
6190 clp = oop->oo_owner.so_client;
6191 release_openowner(oop);
6197 nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
6200 unsigned int count = 0;
6201 struct nfs4_client *clp;
6202 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6204 LIST_HEAD(reaplist);
6206 if (!nfsd_netns_ready(nn))
6209 spin_lock(&nn->client_lock);
6210 clp = nfsd_find_client(addr, addr_size);
6212 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
6213 spin_unlock(&nn->client_lock);
6214 nfsd_reap_openowners(&reaplist);
6219 nfsd_inject_forget_openowners(u64 max)
6222 struct nfs4_client *clp;
6223 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6225 LIST_HEAD(reaplist);
6227 if (!nfsd_netns_ready(nn))
6230 spin_lock(&nn->client_lock);
6231 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6232 count += nfsd_collect_client_openowners(clp, &reaplist,
6234 if (max != 0 && count >= max)
6237 spin_unlock(&nn->client_lock);
6238 nfsd_reap_openowners(&reaplist);
6242 static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
6243 struct list_head *victims)
6245 struct nfs4_delegation *dp, *next;
6246 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6250 lockdep_assert_held(&nn->client_lock);
6252 spin_lock(&state_lock);
6253 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
6256 * It's not safe to mess with delegations that have a
6257 * non-zero dl_time. They might have already been broken
6258 * and could be processed by the laundromat outside of
6259 * the state_lock. Just leave them be.
6261 if (dp->dl_time != 0)
6264 atomic_inc(&clp->cl_refcount);
6265 unhash_delegation_locked(dp);
6266 list_add(&dp->dl_recall_lru, victims);
6270 * Despite the fact that these functions deal with
6271 * 64-bit integers for "count", we must ensure that
6272 * it doesn't blow up the clp->cl_refcount. Throw a
6273 * warning if we start to approach INT_MAX here.
6275 WARN_ON_ONCE(count == (INT_MAX / 2));
6279 spin_unlock(&state_lock);
6284 nfsd_print_client_delegations(struct nfs4_client *clp)
6286 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
6288 nfsd_print_count(clp, count, "delegations");
6293 nfsd_inject_print_delegations(void)
6295 struct nfs4_client *clp;
6297 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6300 if (!nfsd_netns_ready(nn))
6303 spin_lock(&nn->client_lock);
6304 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6305 count += nfsd_print_client_delegations(clp);
6306 spin_unlock(&nn->client_lock);
6312 nfsd_forget_delegations(struct list_head *reaplist)
6314 struct nfs4_client *clp;
6315 struct nfs4_delegation *dp, *next;
6317 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6318 list_del_init(&dp->dl_recall_lru);
6319 clp = dp->dl_stid.sc_client;
6320 revoke_delegation(dp);
6326 nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
6330 struct nfs4_client *clp;
6331 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6333 LIST_HEAD(reaplist);
6335 if (!nfsd_netns_ready(nn))
6338 spin_lock(&nn->client_lock);
6339 clp = nfsd_find_client(addr, addr_size);
6341 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6342 spin_unlock(&nn->client_lock);
6344 nfsd_forget_delegations(&reaplist);
6349 nfsd_inject_forget_delegations(u64 max)
6352 struct nfs4_client *clp;
6353 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6355 LIST_HEAD(reaplist);
6357 if (!nfsd_netns_ready(nn))
6360 spin_lock(&nn->client_lock);
6361 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6362 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6363 if (max != 0 && count >= max)
6366 spin_unlock(&nn->client_lock);
6367 nfsd_forget_delegations(&reaplist);
6372 nfsd_recall_delegations(struct list_head *reaplist)
6374 struct nfs4_client *clp;
6375 struct nfs4_delegation *dp, *next;
6377 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6378 list_del_init(&dp->dl_recall_lru);
6379 clp = dp->dl_stid.sc_client;
6381 * We skipped all entries that had a zero dl_time before,
6382 * so we can now reset the dl_time back to 0. If a delegation
6383 * break comes in now, then it won't make any difference since
6384 * we're recalling it either way.
6386 spin_lock(&state_lock);
6388 spin_unlock(&state_lock);
6389 nfsd_break_one_deleg(dp);
6395 nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
6399 struct nfs4_client *clp;
6400 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6402 LIST_HEAD(reaplist);
6404 if (!nfsd_netns_ready(nn))
6407 spin_lock(&nn->client_lock);
6408 clp = nfsd_find_client(addr, addr_size);
6410 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6411 spin_unlock(&nn->client_lock);
6413 nfsd_recall_delegations(&reaplist);
6418 nfsd_inject_recall_delegations(u64 max)
6421 struct nfs4_client *clp, *next;
6422 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6424 LIST_HEAD(reaplist);
6426 if (!nfsd_netns_ready(nn))
6429 spin_lock(&nn->client_lock);
6430 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6431 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6432 if (max != 0 && ++count >= max)
6435 spin_unlock(&nn->client_lock);
6436 nfsd_recall_delegations(&reaplist);
6439 #endif /* CONFIG_NFSD_FAULT_INJECTION */
6442 * Since the lifetime of a delegation isn't limited to that of an open, a
6443 * client may quite reasonably hang on to a delegation as long as it has
6444 * the inode cached. This becomes an obvious problem the first time a
6445 * client's inode cache approaches the size of the server's total memory.
6447 * For now we avoid this problem by imposing a hard limit on the number
6448 * of delegations, which varies according to the server's memory size.
6451 set_max_delegations(void)
6454 * Allow at most 4 delegations per megabyte of RAM. Quick
6455 * estimates suggest that in the worst case (where every delegation
6456 * is for a different inode), a delegation could take about 1.5K,
6457 * giving a worst case usage of about 6% of memory.
6459 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
6462 static int nfs4_state_create_net(struct net *net)
6464 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6467 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6468 CLIENT_HASH_SIZE, GFP_KERNEL);
6469 if (!nn->conf_id_hashtbl)
6471 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6472 CLIENT_HASH_SIZE, GFP_KERNEL);
6473 if (!nn->unconf_id_hashtbl)
6475 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
6476 SESSION_HASH_SIZE, GFP_KERNEL);
6477 if (!nn->sessionid_hashtbl)
6480 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6481 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
6482 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
6484 for (i = 0; i < SESSION_HASH_SIZE; i++)
6485 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
6486 nn->conf_name_tree = RB_ROOT;
6487 nn->unconf_name_tree = RB_ROOT;
6488 INIT_LIST_HEAD(&nn->client_lru);
6489 INIT_LIST_HEAD(&nn->close_lru);
6490 INIT_LIST_HEAD(&nn->del_recall_lru);
6491 spin_lock_init(&nn->client_lock);
6493 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
6499 kfree(nn->unconf_id_hashtbl);
6501 kfree(nn->conf_id_hashtbl);
6507 nfs4_state_destroy_net(struct net *net)
6510 struct nfs4_client *clp = NULL;
6511 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6513 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6514 while (!list_empty(&nn->conf_id_hashtbl[i])) {
6515 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6516 destroy_client(clp);
6520 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6521 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
6522 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6523 destroy_client(clp);
6527 kfree(nn->sessionid_hashtbl);
6528 kfree(nn->unconf_id_hashtbl);
6529 kfree(nn->conf_id_hashtbl);
6534 nfs4_state_start_net(struct net *net)
6536 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6539 ret = nfs4_state_create_net(net);
6542 nn->boot_time = get_seconds();
6543 nn->grace_ended = false;
6544 locks_start_grace(net, &nn->nfsd4_manager);
6545 nfsd4_client_tracking_init(net);
6546 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
6547 nn->nfsd4_grace, net);
6548 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
6552 /* initialization to perform when the nfsd service is started: */
6555 nfs4_state_start(void)
6559 ret = set_callback_cred();
6562 laundry_wq = create_singlethread_workqueue("nfsd4");
6563 if (laundry_wq == NULL) {
6567 ret = nfsd4_create_callback_queue();
6569 goto out_free_laundry;
6571 set_max_delegations();
6576 destroy_workqueue(laundry_wq);
6582 nfs4_state_shutdown_net(struct net *net)
6584 struct nfs4_delegation *dp = NULL;
6585 struct list_head *pos, *next, reaplist;
6586 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6588 cancel_delayed_work_sync(&nn->laundromat_work);
6589 locks_end_grace(&nn->nfsd4_manager);
6591 INIT_LIST_HEAD(&reaplist);
6592 spin_lock(&state_lock);
6593 list_for_each_safe(pos, next, &nn->del_recall_lru) {
6594 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
6595 unhash_delegation_locked(dp);
6596 list_add(&dp->dl_recall_lru, &reaplist);
6598 spin_unlock(&state_lock);
6599 list_for_each_safe(pos, next, &reaplist) {
6600 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
6601 list_del_init(&dp->dl_recall_lru);
6602 put_clnt_odstate(dp->dl_clnt_odstate);
6603 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
6604 nfs4_put_stid(&dp->dl_stid);
6607 nfsd4_client_tracking_exit(net);
6608 nfs4_state_destroy_net(net);
6612 nfs4_state_shutdown(void)
6614 destroy_workqueue(laundry_wq);
6615 nfsd4_destroy_callback_queue();
6619 get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6621 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
6622 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
6626 put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6628 if (cstate->minorversion) {
6629 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
6630 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6635 clear_current_stateid(struct nfsd4_compound_state *cstate)
6637 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6641 * functions to set current state id
6644 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6646 put_stateid(cstate, &odp->od_stateid);
6650 nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
6652 put_stateid(cstate, &open->op_stateid);
6656 nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6658 put_stateid(cstate, &close->cl_stateid);
6662 nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
6664 put_stateid(cstate, &lock->lk_resp_stateid);
6668 * functions to consume current state id
6672 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6674 get_stateid(cstate, &odp->od_stateid);
6678 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
6680 get_stateid(cstate, &drp->dr_stateid);
6684 nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
6686 get_stateid(cstate, &fsp->fr_stateid);
6690 nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
6692 get_stateid(cstate, &setattr->sa_stateid);
6696 nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6698 get_stateid(cstate, &close->cl_stateid);
6702 nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
6704 get_stateid(cstate, &locku->lu_stateid);
6708 nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
6710 get_stateid(cstate, &read->rd_stateid);
6714 nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
6716 get_stateid(cstate, &write->wr_stateid);