2 * Copyright (c) 2001 The Regents of the University of Michigan.
5 * Kendrick Smith <kmsmith@umich.edu>
6 * Andy Adamson <kandros@umich.edu>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <linux/file.h>
37 #include <linux/slab.h>
38 #include <linux/namei.h>
39 #include <linux/swap.h>
40 #include <linux/pagemap.h>
41 #include <linux/ratelimit.h>
42 #include <linux/sunrpc/svcauth_gss.h>
43 #include <linux/sunrpc/addr.h>
44 #include <linux/jhash.h>
48 #include "current_stateid.h"
53 #define NFSDDBG_FACILITY NFSDDBG_PROC
55 #define all_ones {{~0,~0},~0}
56 static const stateid_t one_stateid = {
58 .si_opaque = all_ones,
60 static const stateid_t zero_stateid = {
63 static const stateid_t currentstateid = {
67 static u64 current_sessionid = 1;
69 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
70 #define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
71 #define CURRENT_STATEID(stateid) (!memcmp((stateid), ¤tstateid, sizeof(stateid_t)))
73 /* forward declarations */
74 static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
75 static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
80 * Currently used for the del_recall_lru and file hash table. In an
81 * effort to decrease the scope of the client_mutex, this spinlock may
82 * eventually cover more:
84 static DEFINE_SPINLOCK(state_lock);
87 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
88 * the refcount on the open stateid to drop.
90 static DECLARE_WAIT_QUEUE_HEAD(close_wq);
92 static struct kmem_cache *openowner_slab;
93 static struct kmem_cache *lockowner_slab;
94 static struct kmem_cache *file_slab;
95 static struct kmem_cache *stateid_slab;
96 static struct kmem_cache *deleg_slab;
97 static struct kmem_cache *odstate_slab;
99 static void free_session(struct nfsd4_session *);
101 static struct nfsd4_callback_ops nfsd4_cb_recall_ops;
103 static bool is_session_dead(struct nfsd4_session *ses)
105 return ses->se_flags & NFS4_SESSION_DEAD;
108 static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
110 if (atomic_read(&ses->se_ref) > ref_held_by_me)
111 return nfserr_jukebox;
112 ses->se_flags |= NFS4_SESSION_DEAD;
116 static bool is_client_expired(struct nfs4_client *clp)
118 return clp->cl_time == 0;
121 static __be32 get_client_locked(struct nfs4_client *clp)
123 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
125 lockdep_assert_held(&nn->client_lock);
127 if (is_client_expired(clp))
128 return nfserr_expired;
129 atomic_inc(&clp->cl_refcount);
133 /* must be called under the client_lock */
135 renew_client_locked(struct nfs4_client *clp)
137 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
139 if (is_client_expired(clp)) {
141 printk("%s: client (clientid %08x/%08x) already expired\n",
143 clp->cl_clientid.cl_boot,
144 clp->cl_clientid.cl_id);
148 dprintk("renewing client (clientid %08x/%08x)\n",
149 clp->cl_clientid.cl_boot,
150 clp->cl_clientid.cl_id);
151 list_move_tail(&clp->cl_lru, &nn->client_lru);
152 clp->cl_time = get_seconds();
155 static void put_client_renew_locked(struct nfs4_client *clp)
157 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
159 lockdep_assert_held(&nn->client_lock);
161 if (!atomic_dec_and_test(&clp->cl_refcount))
163 if (!is_client_expired(clp))
164 renew_client_locked(clp);
167 static void put_client_renew(struct nfs4_client *clp)
169 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
171 if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
173 if (!is_client_expired(clp))
174 renew_client_locked(clp);
175 spin_unlock(&nn->client_lock);
178 static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
182 if (is_session_dead(ses))
183 return nfserr_badsession;
184 status = get_client_locked(ses->se_client);
187 atomic_inc(&ses->se_ref);
191 static void nfsd4_put_session_locked(struct nfsd4_session *ses)
193 struct nfs4_client *clp = ses->se_client;
194 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
196 lockdep_assert_held(&nn->client_lock);
198 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
200 put_client_renew_locked(clp);
203 static void nfsd4_put_session(struct nfsd4_session *ses)
205 struct nfs4_client *clp = ses->se_client;
206 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
208 spin_lock(&nn->client_lock);
209 nfsd4_put_session_locked(ses);
210 spin_unlock(&nn->client_lock);
213 static inline struct nfs4_stateowner *
214 nfs4_get_stateowner(struct nfs4_stateowner *sop)
216 atomic_inc(&sop->so_count);
221 same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
223 return (sop->so_owner.len == owner->len) &&
224 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
227 static struct nfs4_openowner *
228 find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
229 struct nfs4_client *clp)
231 struct nfs4_stateowner *so;
233 lockdep_assert_held(&clp->cl_lock);
235 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
237 if (!so->so_is_open_owner)
239 if (same_owner_str(so, &open->op_owner))
240 return openowner(nfs4_get_stateowner(so));
245 static struct nfs4_openowner *
246 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
247 struct nfs4_client *clp)
249 struct nfs4_openowner *oo;
251 spin_lock(&clp->cl_lock);
252 oo = find_openstateowner_str_locked(hashval, open, clp);
253 spin_unlock(&clp->cl_lock);
258 opaque_hashval(const void *ptr, int nbytes)
260 unsigned char *cptr = (unsigned char *) ptr;
270 static void nfsd4_free_file_rcu(struct rcu_head *rcu)
272 struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
274 kmem_cache_free(file_slab, fp);
278 put_nfs4_file(struct nfs4_file *fi)
280 might_lock(&state_lock);
282 if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) {
283 hlist_del_rcu(&fi->fi_hash);
284 spin_unlock(&state_lock);
285 WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
286 WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
287 call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
292 __nfs4_get_fd(struct nfs4_file *f, int oflag)
294 if (f->fi_fds[oflag])
295 return get_file(f->fi_fds[oflag]);
300 find_writeable_file_locked(struct nfs4_file *f)
304 lockdep_assert_held(&f->fi_lock);
306 ret = __nfs4_get_fd(f, O_WRONLY);
308 ret = __nfs4_get_fd(f, O_RDWR);
313 find_writeable_file(struct nfs4_file *f)
317 spin_lock(&f->fi_lock);
318 ret = find_writeable_file_locked(f);
319 spin_unlock(&f->fi_lock);
324 static struct file *find_readable_file_locked(struct nfs4_file *f)
328 lockdep_assert_held(&f->fi_lock);
330 ret = __nfs4_get_fd(f, O_RDONLY);
332 ret = __nfs4_get_fd(f, O_RDWR);
337 find_readable_file(struct nfs4_file *f)
341 spin_lock(&f->fi_lock);
342 ret = find_readable_file_locked(f);
343 spin_unlock(&f->fi_lock);
349 find_any_file(struct nfs4_file *f)
353 spin_lock(&f->fi_lock);
354 ret = __nfs4_get_fd(f, O_RDWR);
356 ret = __nfs4_get_fd(f, O_WRONLY);
358 ret = __nfs4_get_fd(f, O_RDONLY);
360 spin_unlock(&f->fi_lock);
364 static atomic_long_t num_delegations;
365 unsigned long max_delegations;
368 * Open owner state (share locks)
371 /* hash tables for lock and open owners */
372 #define OWNER_HASH_BITS 8
373 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
374 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
376 static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
380 ret = opaque_hashval(ownername->data, ownername->len);
381 return ret & OWNER_HASH_MASK;
384 /* hash table for nfs4_file */
385 #define FILE_HASH_BITS 8
386 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
388 static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
390 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
393 static unsigned int file_hashval(struct knfsd_fh *fh)
395 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
398 static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
401 __nfs4_file_get_access(struct nfs4_file *fp, u32 access)
403 lockdep_assert_held(&fp->fi_lock);
405 if (access & NFS4_SHARE_ACCESS_WRITE)
406 atomic_inc(&fp->fi_access[O_WRONLY]);
407 if (access & NFS4_SHARE_ACCESS_READ)
408 atomic_inc(&fp->fi_access[O_RDONLY]);
412 nfs4_file_get_access(struct nfs4_file *fp, u32 access)
414 lockdep_assert_held(&fp->fi_lock);
416 /* Does this access mode make sense? */
417 if (access & ~NFS4_SHARE_ACCESS_BOTH)
420 /* Does it conflict with a deny mode already set? */
421 if ((access & fp->fi_share_deny) != 0)
422 return nfserr_share_denied;
424 __nfs4_file_get_access(fp, access);
428 static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
430 /* Common case is that there is no deny mode. */
432 /* Does this deny mode make sense? */
433 if (deny & ~NFS4_SHARE_DENY_BOTH)
436 if ((deny & NFS4_SHARE_DENY_READ) &&
437 atomic_read(&fp->fi_access[O_RDONLY]))
438 return nfserr_share_denied;
440 if ((deny & NFS4_SHARE_DENY_WRITE) &&
441 atomic_read(&fp->fi_access[O_WRONLY]))
442 return nfserr_share_denied;
447 static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
449 might_lock(&fp->fi_lock);
451 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
452 struct file *f1 = NULL;
453 struct file *f2 = NULL;
455 swap(f1, fp->fi_fds[oflag]);
456 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
457 swap(f2, fp->fi_fds[O_RDWR]);
458 spin_unlock(&fp->fi_lock);
466 static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
468 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
470 if (access & NFS4_SHARE_ACCESS_WRITE)
471 __nfs4_file_put_access(fp, O_WRONLY);
472 if (access & NFS4_SHARE_ACCESS_READ)
473 __nfs4_file_put_access(fp, O_RDONLY);
477 * Allocate a new open/delegation state counter. This is needed for
478 * pNFS for proper return on close semantics.
480 * Note that we only allocate it for pNFS-enabled exports, otherwise
481 * all pointers to struct nfs4_clnt_odstate are always NULL.
483 static struct nfs4_clnt_odstate *
484 alloc_clnt_odstate(struct nfs4_client *clp)
486 struct nfs4_clnt_odstate *co;
488 co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
491 atomic_set(&co->co_odcount, 1);
497 hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
499 struct nfs4_file *fp = co->co_file;
501 lockdep_assert_held(&fp->fi_lock);
502 list_add(&co->co_perfile, &fp->fi_clnt_odstate);
506 get_clnt_odstate(struct nfs4_clnt_odstate *co)
509 atomic_inc(&co->co_odcount);
513 put_clnt_odstate(struct nfs4_clnt_odstate *co)
515 struct nfs4_file *fp;
521 if (atomic_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
522 list_del(&co->co_perfile);
523 spin_unlock(&fp->fi_lock);
525 nfsd4_return_all_file_layouts(co->co_client, fp);
526 kmem_cache_free(odstate_slab, co);
530 static struct nfs4_clnt_odstate *
531 find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
533 struct nfs4_clnt_odstate *co;
534 struct nfs4_client *cl;
541 spin_lock(&fp->fi_lock);
542 list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
543 if (co->co_client == cl) {
544 get_clnt_odstate(co);
550 hash_clnt_odstate_locked(new);
552 spin_unlock(&fp->fi_lock);
556 struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl,
557 struct kmem_cache *slab)
559 struct nfs4_stid *stid;
562 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
566 idr_preload(GFP_KERNEL);
567 spin_lock(&cl->cl_lock);
568 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 0, 0, GFP_NOWAIT);
569 spin_unlock(&cl->cl_lock);
573 stid->sc_client = cl;
574 stid->sc_stateid.si_opaque.so_id = new_id;
575 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
576 /* Will be incremented before return to client: */
577 atomic_set(&stid->sc_count, 1);
580 * It shouldn't be a problem to reuse an opaque stateid value.
581 * I don't think it is for 4.1. But with 4.0 I worry that, for
582 * example, a stray write retransmission could be accepted by
583 * the server when it should have been rejected. Therefore,
584 * adopt a trick from the sctp code to attempt to maximize the
585 * amount of time until an id is reused, by ensuring they always
586 * "increase" (mod INT_MAX):
590 kmem_cache_free(slab, stid);
594 static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
596 struct nfs4_stid *stid;
597 struct nfs4_ol_stateid *stp;
599 stid = nfs4_alloc_stid(clp, stateid_slab);
603 stp = openlockstateid(stid);
604 stp->st_stid.sc_free = nfs4_free_ol_stateid;
608 static void nfs4_free_deleg(struct nfs4_stid *stid)
610 kmem_cache_free(deleg_slab, stid);
611 atomic_long_dec(&num_delegations);
615 * When we recall a delegation, we should be careful not to hand it
616 * out again straight away.
617 * To ensure this we keep a pair of bloom filters ('new' and 'old')
618 * in which the filehandles of recalled delegations are "stored".
619 * If a filehandle appear in either filter, a delegation is blocked.
620 * When a delegation is recalled, the filehandle is stored in the "new"
622 * Every 30 seconds we swap the filters and clear the "new" one,
623 * unless both are empty of course.
625 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
626 * low 3 bytes as hash-table indices.
628 * 'blocked_delegations_lock', which is always taken in block_delegations(),
629 * is used to manage concurrent access. Testing does not need the lock
630 * except when swapping the two filters.
632 static DEFINE_SPINLOCK(blocked_delegations_lock);
633 static struct bloom_pair {
634 int entries, old_entries;
636 int new; /* index into 'set' */
637 DECLARE_BITMAP(set[2], 256);
638 } blocked_delegations;
640 static int delegation_blocked(struct knfsd_fh *fh)
643 struct bloom_pair *bd = &blocked_delegations;
645 if (bd->entries == 0)
647 if (seconds_since_boot() - bd->swap_time > 30) {
648 spin_lock(&blocked_delegations_lock);
649 if (seconds_since_boot() - bd->swap_time > 30) {
650 bd->entries -= bd->old_entries;
651 bd->old_entries = bd->entries;
652 memset(bd->set[bd->new], 0,
655 bd->swap_time = seconds_since_boot();
657 spin_unlock(&blocked_delegations_lock);
659 hash = jhash(&fh->fh_base, fh->fh_size, 0);
660 if (test_bit(hash&255, bd->set[0]) &&
661 test_bit((hash>>8)&255, bd->set[0]) &&
662 test_bit((hash>>16)&255, bd->set[0]))
665 if (test_bit(hash&255, bd->set[1]) &&
666 test_bit((hash>>8)&255, bd->set[1]) &&
667 test_bit((hash>>16)&255, bd->set[1]))
673 static void block_delegations(struct knfsd_fh *fh)
676 struct bloom_pair *bd = &blocked_delegations;
678 hash = jhash(&fh->fh_base, fh->fh_size, 0);
680 spin_lock(&blocked_delegations_lock);
681 __set_bit(hash&255, bd->set[bd->new]);
682 __set_bit((hash>>8)&255, bd->set[bd->new]);
683 __set_bit((hash>>16)&255, bd->set[bd->new]);
684 if (bd->entries == 0)
685 bd->swap_time = seconds_since_boot();
687 spin_unlock(&blocked_delegations_lock);
690 static struct nfs4_delegation *
691 alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh,
692 struct nfs4_clnt_odstate *odstate)
694 struct nfs4_delegation *dp;
697 dprintk("NFSD alloc_init_deleg\n");
698 n = atomic_long_inc_return(&num_delegations);
699 if (n < 0 || n > max_delegations)
701 if (delegation_blocked(¤t_fh->fh_handle))
703 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
707 dp->dl_stid.sc_free = nfs4_free_deleg;
709 * delegation seqid's are never incremented. The 4.1 special
710 * meaning of seqid 0 isn't meaningful, really, but let's avoid
711 * 0 anyway just for consistency and use 1:
713 dp->dl_stid.sc_stateid.si_generation = 1;
714 INIT_LIST_HEAD(&dp->dl_perfile);
715 INIT_LIST_HEAD(&dp->dl_perclnt);
716 INIT_LIST_HEAD(&dp->dl_recall_lru);
717 dp->dl_clnt_odstate = odstate;
718 get_clnt_odstate(odstate);
719 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
721 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
722 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
725 atomic_long_dec(&num_delegations);
730 nfs4_put_stid(struct nfs4_stid *s)
732 struct nfs4_file *fp = s->sc_file;
733 struct nfs4_client *clp = s->sc_client;
735 might_lock(&clp->cl_lock);
737 if (!atomic_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
738 wake_up_all(&close_wq);
741 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
742 spin_unlock(&clp->cl_lock);
748 static void nfs4_put_deleg_lease(struct nfs4_file *fp)
750 struct file *filp = NULL;
752 spin_lock(&fp->fi_lock);
753 if (fp->fi_deleg_file && --fp->fi_delegees == 0)
754 swap(filp, fp->fi_deleg_file);
755 spin_unlock(&fp->fi_lock);
758 vfs_setlease(filp, F_UNLCK, NULL, (void **)&fp);
763 void nfs4_unhash_stid(struct nfs4_stid *s)
769 hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
771 lockdep_assert_held(&state_lock);
772 lockdep_assert_held(&fp->fi_lock);
774 atomic_inc(&dp->dl_stid.sc_count);
775 dp->dl_stid.sc_type = NFS4_DELEG_STID;
776 list_add(&dp->dl_perfile, &fp->fi_delegations);
777 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
781 unhash_delegation_locked(struct nfs4_delegation *dp)
783 struct nfs4_file *fp = dp->dl_stid.sc_file;
785 lockdep_assert_held(&state_lock);
787 if (list_empty(&dp->dl_perfile))
790 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
791 /* Ensure that deleg break won't try to requeue it */
793 spin_lock(&fp->fi_lock);
794 list_del_init(&dp->dl_perclnt);
795 list_del_init(&dp->dl_recall_lru);
796 list_del_init(&dp->dl_perfile);
797 spin_unlock(&fp->fi_lock);
801 static void destroy_delegation(struct nfs4_delegation *dp)
805 spin_lock(&state_lock);
806 unhashed = unhash_delegation_locked(dp);
807 spin_unlock(&state_lock);
809 put_clnt_odstate(dp->dl_clnt_odstate);
810 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
811 nfs4_put_stid(&dp->dl_stid);
815 static void revoke_delegation(struct nfs4_delegation *dp)
817 struct nfs4_client *clp = dp->dl_stid.sc_client;
819 WARN_ON(!list_empty(&dp->dl_recall_lru));
821 put_clnt_odstate(dp->dl_clnt_odstate);
822 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
824 if (clp->cl_minorversion == 0)
825 nfs4_put_stid(&dp->dl_stid);
827 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
828 spin_lock(&clp->cl_lock);
829 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
830 spin_unlock(&clp->cl_lock);
838 static unsigned int clientid_hashval(u32 id)
840 return id & CLIENT_HASH_MASK;
843 static unsigned int clientstr_hashval(const char *name)
845 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
849 * We store the NONE, READ, WRITE, and BOTH bits separately in the
850 * st_{access,deny}_bmap field of the stateid, in order to track not
851 * only what share bits are currently in force, but also what
852 * combinations of share bits previous opens have used. This allows us
853 * to enforce the recommendation of rfc 3530 14.2.19 that the server
854 * return an error if the client attempt to downgrade to a combination
855 * of share bits not explicable by closing some of its previous opens.
857 * XXX: This enforcement is actually incomplete, since we don't keep
858 * track of access/deny bit combinations; so, e.g., we allow:
860 * OPEN allow read, deny write
861 * OPEN allow both, deny none
862 * DOWNGRADE allow read, deny none
864 * which we should reject.
867 bmap_to_share_mode(unsigned long bmap) {
869 unsigned int access = 0;
871 for (i = 1; i < 4; i++) {
872 if (test_bit(i, &bmap))
878 /* set share access for a given stateid */
880 set_access(u32 access, struct nfs4_ol_stateid *stp)
882 unsigned char mask = 1 << access;
884 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
885 stp->st_access_bmap |= mask;
888 /* clear share access for a given stateid */
890 clear_access(u32 access, struct nfs4_ol_stateid *stp)
892 unsigned char mask = 1 << access;
894 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
895 stp->st_access_bmap &= ~mask;
898 /* test whether a given stateid has access */
900 test_access(u32 access, struct nfs4_ol_stateid *stp)
902 unsigned char mask = 1 << access;
904 return (bool)(stp->st_access_bmap & mask);
907 /* set share deny for a given stateid */
909 set_deny(u32 deny, struct nfs4_ol_stateid *stp)
911 unsigned char mask = 1 << deny;
913 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
914 stp->st_deny_bmap |= mask;
917 /* clear share deny for a given stateid */
919 clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
921 unsigned char mask = 1 << deny;
923 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
924 stp->st_deny_bmap &= ~mask;
927 /* test whether a given stateid is denying specific access */
929 test_deny(u32 deny, struct nfs4_ol_stateid *stp)
931 unsigned char mask = 1 << deny;
933 return (bool)(stp->st_deny_bmap & mask);
936 static int nfs4_access_to_omode(u32 access)
938 switch (access & NFS4_SHARE_ACCESS_BOTH) {
939 case NFS4_SHARE_ACCESS_READ:
941 case NFS4_SHARE_ACCESS_WRITE:
943 case NFS4_SHARE_ACCESS_BOTH:
951 * A stateid that had a deny mode associated with it is being released
952 * or downgraded. Recalculate the deny mode on the file.
955 recalculate_deny_mode(struct nfs4_file *fp)
957 struct nfs4_ol_stateid *stp;
959 spin_lock(&fp->fi_lock);
960 fp->fi_share_deny = 0;
961 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
962 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
963 spin_unlock(&fp->fi_lock);
967 reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
972 for (i = 1; i < 4; i++) {
973 if ((i & deny) != i) {
979 /* Recalculate per-file deny mode if there was a change */
981 recalculate_deny_mode(stp->st_stid.sc_file);
984 /* release all access and file references for a given stateid */
986 release_all_access(struct nfs4_ol_stateid *stp)
989 struct nfs4_file *fp = stp->st_stid.sc_file;
991 if (fp && stp->st_deny_bmap != 0)
992 recalculate_deny_mode(fp);
994 for (i = 1; i < 4; i++) {
995 if (test_access(i, stp))
996 nfs4_file_put_access(stp->st_stid.sc_file, i);
997 clear_access(i, stp);
1001 static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1003 kfree(sop->so_owner.data);
1004 sop->so_ops->so_free(sop);
1007 static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1009 struct nfs4_client *clp = sop->so_client;
1011 might_lock(&clp->cl_lock);
1013 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
1015 sop->so_ops->so_unhash(sop);
1016 spin_unlock(&clp->cl_lock);
1017 nfs4_free_stateowner(sop);
1020 static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
1022 struct nfs4_file *fp = stp->st_stid.sc_file;
1024 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1026 if (list_empty(&stp->st_perfile))
1029 spin_lock(&fp->fi_lock);
1030 list_del_init(&stp->st_perfile);
1031 spin_unlock(&fp->fi_lock);
1032 list_del(&stp->st_perstateowner);
1036 static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
1038 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1040 put_clnt_odstate(stp->st_clnt_odstate);
1041 release_all_access(stp);
1042 if (stp->st_stateowner)
1043 nfs4_put_stateowner(stp->st_stateowner);
1044 kmem_cache_free(stateid_slab, stid);
1047 static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
1049 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1050 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
1053 file = find_any_file(stp->st_stid.sc_file);
1055 filp_close(file, (fl_owner_t)lo);
1056 nfs4_free_ol_stateid(stid);
1060 * Put the persistent reference to an already unhashed generic stateid, while
1061 * holding the cl_lock. If it's the last reference, then put it onto the
1062 * reaplist for later destruction.
1064 static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1065 struct list_head *reaplist)
1067 struct nfs4_stid *s = &stp->st_stid;
1068 struct nfs4_client *clp = s->sc_client;
1070 lockdep_assert_held(&clp->cl_lock);
1072 WARN_ON_ONCE(!list_empty(&stp->st_locks));
1074 if (!atomic_dec_and_test(&s->sc_count)) {
1075 wake_up_all(&close_wq);
1079 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1080 list_add(&stp->st_locks, reaplist);
1083 static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
1085 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
1087 lockdep_assert_held(&oo->oo_owner.so_client->cl_lock);
1089 list_del_init(&stp->st_locks);
1090 nfs4_unhash_stid(&stp->st_stid);
1091 return unhash_ol_stateid(stp);
1094 static void release_lock_stateid(struct nfs4_ol_stateid *stp)
1096 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
1099 spin_lock(&oo->oo_owner.so_client->cl_lock);
1100 unhashed = unhash_lock_stateid(stp);
1101 spin_unlock(&oo->oo_owner.so_client->cl_lock);
1103 nfs4_put_stid(&stp->st_stid);
1106 static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
1108 struct nfs4_client *clp = lo->lo_owner.so_client;
1110 lockdep_assert_held(&clp->cl_lock);
1112 list_del_init(&lo->lo_owner.so_strhash);
1116 * Free a list of generic stateids that were collected earlier after being
1120 free_ol_stateid_reaplist(struct list_head *reaplist)
1122 struct nfs4_ol_stateid *stp;
1123 struct nfs4_file *fp;
1127 while (!list_empty(reaplist)) {
1128 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1130 list_del(&stp->st_locks);
1131 fp = stp->st_stid.sc_file;
1132 stp->st_stid.sc_free(&stp->st_stid);
1138 static void release_lockowner(struct nfs4_lockowner *lo)
1140 struct nfs4_client *clp = lo->lo_owner.so_client;
1141 struct nfs4_ol_stateid *stp;
1142 struct list_head reaplist;
1144 INIT_LIST_HEAD(&reaplist);
1146 spin_lock(&clp->cl_lock);
1147 unhash_lockowner_locked(lo);
1148 while (!list_empty(&lo->lo_owner.so_stateids)) {
1149 stp = list_first_entry(&lo->lo_owner.so_stateids,
1150 struct nfs4_ol_stateid, st_perstateowner);
1151 WARN_ON(!unhash_lock_stateid(stp));
1152 put_ol_stateid_locked(stp, &reaplist);
1154 spin_unlock(&clp->cl_lock);
1155 free_ol_stateid_reaplist(&reaplist);
1156 nfs4_put_stateowner(&lo->lo_owner);
1159 static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1160 struct list_head *reaplist)
1162 struct nfs4_ol_stateid *stp;
1164 lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1166 while (!list_empty(&open_stp->st_locks)) {
1167 stp = list_entry(open_stp->st_locks.next,
1168 struct nfs4_ol_stateid, st_locks);
1169 WARN_ON(!unhash_lock_stateid(stp));
1170 put_ol_stateid_locked(stp, reaplist);
1174 static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
1175 struct list_head *reaplist)
1179 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1181 unhashed = unhash_ol_stateid(stp);
1182 release_open_stateid_locks(stp, reaplist);
1186 static void release_open_stateid(struct nfs4_ol_stateid *stp)
1188 LIST_HEAD(reaplist);
1190 spin_lock(&stp->st_stid.sc_client->cl_lock);
1191 if (unhash_open_stateid(stp, &reaplist))
1192 put_ol_stateid_locked(stp, &reaplist);
1193 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1194 free_ol_stateid_reaplist(&reaplist);
1197 static void unhash_openowner_locked(struct nfs4_openowner *oo)
1199 struct nfs4_client *clp = oo->oo_owner.so_client;
1201 lockdep_assert_held(&clp->cl_lock);
1203 list_del_init(&oo->oo_owner.so_strhash);
1204 list_del_init(&oo->oo_perclient);
1207 static void release_last_closed_stateid(struct nfs4_openowner *oo)
1209 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1211 struct nfs4_ol_stateid *s;
1213 spin_lock(&nn->client_lock);
1214 s = oo->oo_last_closed_stid;
1216 list_del_init(&oo->oo_close_lru);
1217 oo->oo_last_closed_stid = NULL;
1219 spin_unlock(&nn->client_lock);
1221 nfs4_put_stid(&s->st_stid);
1224 static void release_openowner(struct nfs4_openowner *oo)
1226 struct nfs4_ol_stateid *stp;
1227 struct nfs4_client *clp = oo->oo_owner.so_client;
1228 struct list_head reaplist;
1230 INIT_LIST_HEAD(&reaplist);
1232 spin_lock(&clp->cl_lock);
1233 unhash_openowner_locked(oo);
1234 while (!list_empty(&oo->oo_owner.so_stateids)) {
1235 stp = list_first_entry(&oo->oo_owner.so_stateids,
1236 struct nfs4_ol_stateid, st_perstateowner);
1237 if (unhash_open_stateid(stp, &reaplist))
1238 put_ol_stateid_locked(stp, &reaplist);
1240 spin_unlock(&clp->cl_lock);
1241 free_ol_stateid_reaplist(&reaplist);
1242 release_last_closed_stateid(oo);
1243 nfs4_put_stateowner(&oo->oo_owner);
1247 hash_sessionid(struct nfs4_sessionid *sessionid)
1249 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1251 return sid->sequence % SESSION_HASH_SIZE;
1254 #ifdef CONFIG_SUNRPC_DEBUG
1256 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1258 u32 *ptr = (u32 *)(&sessionid->data[0]);
1259 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1263 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1269 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1270 * won't be used for replay.
1272 void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1274 struct nfs4_stateowner *so = cstate->replay_owner;
1276 if (nfserr == nfserr_replay_me)
1279 if (!seqid_mutating_err(ntohl(nfserr))) {
1280 nfsd4_cstate_clear_replay(cstate);
1285 if (so->so_is_open_owner)
1286 release_last_closed_stateid(openowner(so));
1292 gen_sessionid(struct nfsd4_session *ses)
1294 struct nfs4_client *clp = ses->se_client;
1295 struct nfsd4_sessionid *sid;
1297 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1298 sid->clientid = clp->cl_clientid;
1299 sid->sequence = current_sessionid++;
1304 * The protocol defines ca_maxresponssize_cached to include the size of
1305 * the rpc header, but all we need to cache is the data starting after
1306 * the end of the initial SEQUENCE operation--the rest we regenerate
1307 * each time. Therefore we can advertise a ca_maxresponssize_cached
1308 * value that is the number of bytes in our cache plus a few additional
1309 * bytes. In order to stay on the safe side, and not promise more than
1310 * we can cache, those additional bytes must be the minimum possible: 24
1311 * bytes of rpc header (xid through accept state, with AUTH_NULL
1312 * verifier), 12 for the compound header (with zero-length tag), and 44
1313 * for the SEQUENCE op response:
1315 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1318 free_session_slots(struct nfsd4_session *ses)
1322 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1323 kfree(ses->se_slots[i]);
1327 * We don't actually need to cache the rpc and session headers, so we
1328 * can allocate a little less for each slot:
1330 static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
1334 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1337 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1338 return size + sizeof(struct nfsd4_slot);
1342 * XXX: If we run out of reserved DRC memory we could (up to a point)
1343 * re-negotiate active sessions and reduce their slot usage to make
1344 * room for new connections. For now we just fail the create session.
1346 static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
1348 u32 slotsize = slot_bytes(ca);
1349 u32 num = ca->maxreqs;
1352 spin_lock(&nfsd_drc_lock);
1353 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1354 nfsd_drc_max_mem - nfsd_drc_mem_used);
1355 num = min_t(int, num, avail / slotsize);
1356 nfsd_drc_mem_used += num * slotsize;
1357 spin_unlock(&nfsd_drc_lock);
1362 static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
1364 int slotsize = slot_bytes(ca);
1366 spin_lock(&nfsd_drc_lock);
1367 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
1368 spin_unlock(&nfsd_drc_lock);
1371 static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1372 struct nfsd4_channel_attrs *battrs)
1374 int numslots = fattrs->maxreqs;
1375 int slotsize = slot_bytes(fattrs);
1376 struct nfsd4_session *new;
1379 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
1380 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1381 mem = numslots * sizeof(struct nfsd4_slot *);
1383 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
1386 /* allocate each struct nfsd4_slot and data cache in one piece */
1387 for (i = 0; i < numslots; i++) {
1388 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
1389 if (!new->se_slots[i])
1393 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1394 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1399 kfree(new->se_slots[i]);
1404 static void free_conn(struct nfsd4_conn *c)
1406 svc_xprt_put(c->cn_xprt);
1410 static void nfsd4_conn_lost(struct svc_xpt_user *u)
1412 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1413 struct nfs4_client *clp = c->cn_session->se_client;
1415 spin_lock(&clp->cl_lock);
1416 if (!list_empty(&c->cn_persession)) {
1417 list_del(&c->cn_persession);
1420 nfsd4_probe_callback(clp);
1421 spin_unlock(&clp->cl_lock);
1424 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
1426 struct nfsd4_conn *conn;
1428 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1431 svc_xprt_get(rqstp->rq_xprt);
1432 conn->cn_xprt = rqstp->rq_xprt;
1433 conn->cn_flags = flags;
1434 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1438 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1440 conn->cn_session = ses;
1441 list_add(&conn->cn_persession, &ses->se_conns);
1444 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1446 struct nfs4_client *clp = ses->se_client;
1448 spin_lock(&clp->cl_lock);
1449 __nfsd4_hash_conn(conn, ses);
1450 spin_unlock(&clp->cl_lock);
1453 static int nfsd4_register_conn(struct nfsd4_conn *conn)
1455 conn->cn_xpt_user.callback = nfsd4_conn_lost;
1456 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
1459 static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
1463 nfsd4_hash_conn(conn, ses);
1464 ret = nfsd4_register_conn(conn);
1466 /* oops; xprt is already down: */
1467 nfsd4_conn_lost(&conn->cn_xpt_user);
1468 /* We may have gained or lost a callback channel: */
1469 nfsd4_probe_callback_sync(ses->se_client);
1472 static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
1474 u32 dir = NFS4_CDFC4_FORE;
1476 if (cses->flags & SESSION4_BACK_CHAN)
1477 dir |= NFS4_CDFC4_BACK;
1478 return alloc_conn(rqstp, dir);
1481 /* must be called under client_lock */
1482 static void nfsd4_del_conns(struct nfsd4_session *s)
1484 struct nfs4_client *clp = s->se_client;
1485 struct nfsd4_conn *c;
1487 spin_lock(&clp->cl_lock);
1488 while (!list_empty(&s->se_conns)) {
1489 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1490 list_del_init(&c->cn_persession);
1491 spin_unlock(&clp->cl_lock);
1493 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1496 spin_lock(&clp->cl_lock);
1498 spin_unlock(&clp->cl_lock);
1501 static void __free_session(struct nfsd4_session *ses)
1503 free_session_slots(ses);
1507 static void free_session(struct nfsd4_session *ses)
1509 nfsd4_del_conns(ses);
1510 nfsd4_put_drc_mem(&ses->se_fchannel);
1511 __free_session(ses);
1514 static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
1517 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1519 new->se_client = clp;
1522 INIT_LIST_HEAD(&new->se_conns);
1524 new->se_cb_seq_nr = 1;
1525 new->se_flags = cses->flags;
1526 new->se_cb_prog = cses->callback_prog;
1527 new->se_cb_sec = cses->cb_sec;
1528 atomic_set(&new->se_ref, 0);
1529 idx = hash_sessionid(&new->se_sessionid);
1530 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
1531 spin_lock(&clp->cl_lock);
1532 list_add(&new->se_perclnt, &clp->cl_sessions);
1533 spin_unlock(&clp->cl_lock);
1536 struct sockaddr *sa = svc_addr(rqstp);
1538 * This is a little silly; with sessions there's no real
1539 * use for the callback address. Use the peer address
1540 * as a reasonable default for now, but consider fixing
1541 * the rpc client not to require an address in the
1544 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1545 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
1549 /* caller must hold client_lock */
1550 static struct nfsd4_session *
1551 __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
1553 struct nfsd4_session *elem;
1555 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1557 lockdep_assert_held(&nn->client_lock);
1559 dump_sessionid(__func__, sessionid);
1560 idx = hash_sessionid(sessionid);
1561 /* Search in the appropriate list */
1562 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
1563 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1564 NFS4_MAX_SESSIONID_LEN)) {
1569 dprintk("%s: session not found\n", __func__);
1573 static struct nfsd4_session *
1574 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1577 struct nfsd4_session *session;
1578 __be32 status = nfserr_badsession;
1580 session = __find_in_sessionid_hashtbl(sessionid, net);
1583 status = nfsd4_get_session_locked(session);
1591 /* caller must hold client_lock */
1593 unhash_session(struct nfsd4_session *ses)
1595 struct nfs4_client *clp = ses->se_client;
1596 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1598 lockdep_assert_held(&nn->client_lock);
1600 list_del(&ses->se_hash);
1601 spin_lock(&ses->se_client->cl_lock);
1602 list_del(&ses->se_perclnt);
1603 spin_unlock(&ses->se_client->cl_lock);
1606 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1608 STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
1611 * We're assuming the clid was not given out from a boot
1612 * precisely 2^32 (about 136 years) before this one. That seems
1613 * a safe assumption:
1615 if (clid->cl_boot == (u32)nn->boot_time)
1617 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
1618 clid->cl_boot, clid->cl_id, nn->boot_time);
1623 * XXX Should we use a slab cache ?
1624 * This type of memory management is somewhat inefficient, but we use it
1625 * anyway since SETCLIENTID is not a common operation.
1627 static struct nfs4_client *alloc_client(struct xdr_netobj name)
1629 struct nfs4_client *clp;
1632 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1635 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
1636 if (clp->cl_name.data == NULL)
1638 clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
1639 OWNER_HASH_SIZE, GFP_KERNEL);
1640 if (!clp->cl_ownerstr_hashtbl)
1641 goto err_no_hashtbl;
1642 for (i = 0; i < OWNER_HASH_SIZE; i++)
1643 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
1644 clp->cl_name.len = name.len;
1645 INIT_LIST_HEAD(&clp->cl_sessions);
1646 idr_init(&clp->cl_stateids);
1647 atomic_set(&clp->cl_refcount, 0);
1648 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1649 INIT_LIST_HEAD(&clp->cl_idhash);
1650 INIT_LIST_HEAD(&clp->cl_openowners);
1651 INIT_LIST_HEAD(&clp->cl_delegations);
1652 INIT_LIST_HEAD(&clp->cl_lru);
1653 INIT_LIST_HEAD(&clp->cl_revoked);
1654 #ifdef CONFIG_NFSD_PNFS
1655 INIT_LIST_HEAD(&clp->cl_lo_states);
1657 spin_lock_init(&clp->cl_lock);
1658 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1661 kfree(clp->cl_name.data);
1668 free_client(struct nfs4_client *clp)
1670 while (!list_empty(&clp->cl_sessions)) {
1671 struct nfsd4_session *ses;
1672 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1674 list_del(&ses->se_perclnt);
1675 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1678 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
1679 free_svc_cred(&clp->cl_cred);
1680 kfree(clp->cl_ownerstr_hashtbl);
1681 kfree(clp->cl_name.data);
1682 idr_destroy(&clp->cl_stateids);
1686 /* must be called under the client_lock */
1688 unhash_client_locked(struct nfs4_client *clp)
1690 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1691 struct nfsd4_session *ses;
1693 lockdep_assert_held(&nn->client_lock);
1695 /* Mark the client as expired! */
1697 /* Make it invisible */
1698 if (!list_empty(&clp->cl_idhash)) {
1699 list_del_init(&clp->cl_idhash);
1700 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1701 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1703 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1705 list_del_init(&clp->cl_lru);
1706 spin_lock(&clp->cl_lock);
1707 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1708 list_del_init(&ses->se_hash);
1709 spin_unlock(&clp->cl_lock);
1713 unhash_client(struct nfs4_client *clp)
1715 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1717 spin_lock(&nn->client_lock);
1718 unhash_client_locked(clp);
1719 spin_unlock(&nn->client_lock);
1722 static __be32 mark_client_expired_locked(struct nfs4_client *clp)
1724 if (atomic_read(&clp->cl_refcount))
1725 return nfserr_jukebox;
1726 unhash_client_locked(clp);
1731 __destroy_client(struct nfs4_client *clp)
1733 struct nfs4_openowner *oo;
1734 struct nfs4_delegation *dp;
1735 struct list_head reaplist;
1737 INIT_LIST_HEAD(&reaplist);
1738 spin_lock(&state_lock);
1739 while (!list_empty(&clp->cl_delegations)) {
1740 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
1741 WARN_ON(!unhash_delegation_locked(dp));
1742 list_add(&dp->dl_recall_lru, &reaplist);
1744 spin_unlock(&state_lock);
1745 while (!list_empty(&reaplist)) {
1746 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
1747 list_del_init(&dp->dl_recall_lru);
1748 put_clnt_odstate(dp->dl_clnt_odstate);
1749 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
1750 nfs4_put_stid(&dp->dl_stid);
1752 while (!list_empty(&clp->cl_revoked)) {
1753 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
1754 list_del_init(&dp->dl_recall_lru);
1755 nfs4_put_stid(&dp->dl_stid);
1757 while (!list_empty(&clp->cl_openowners)) {
1758 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1759 nfs4_get_stateowner(&oo->oo_owner);
1760 release_openowner(oo);
1762 nfsd4_return_all_client_layouts(clp);
1763 nfsd4_shutdown_callback(clp);
1764 if (clp->cl_cb_conn.cb_xprt)
1765 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
1770 destroy_client(struct nfs4_client *clp)
1773 __destroy_client(clp);
1776 static void expire_client(struct nfs4_client *clp)
1779 nfsd4_client_record_remove(clp);
1780 __destroy_client(clp);
1783 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1785 memcpy(target->cl_verifier.data, source->data,
1786 sizeof(target->cl_verifier.data));
1789 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1791 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1792 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1795 static int copy_cred(struct svc_cred *target, struct svc_cred *source)
1797 if (source->cr_principal) {
1798 target->cr_principal =
1799 kstrdup(source->cr_principal, GFP_KERNEL);
1800 if (target->cr_principal == NULL)
1803 target->cr_principal = NULL;
1804 target->cr_flavor = source->cr_flavor;
1805 target->cr_uid = source->cr_uid;
1806 target->cr_gid = source->cr_gid;
1807 target->cr_group_info = source->cr_group_info;
1808 get_group_info(target->cr_group_info);
1809 target->cr_gss_mech = source->cr_gss_mech;
1810 if (source->cr_gss_mech)
1811 gss_mech_get(source->cr_gss_mech);
1816 compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1818 if (o1->len < o2->len)
1820 if (o1->len > o2->len)
1822 return memcmp(o1->data, o2->data, o1->len);
1825 static int same_name(const char *n1, const char *n2)
1827 return 0 == memcmp(n1, n2, HEXDIR_LEN);
1831 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1833 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
1837 same_clid(clientid_t *cl1, clientid_t *cl2)
1839 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
1842 static bool groups_equal(struct group_info *g1, struct group_info *g2)
1846 if (g1->ngroups != g2->ngroups)
1848 for (i=0; i<g1->ngroups; i++)
1849 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
1855 * RFC 3530 language requires clid_inuse be returned when the
1856 * "principal" associated with a requests differs from that previously
1857 * used. We use uid, gid's, and gss principal string as our best
1858 * approximation. We also don't want to allow non-gss use of a client
1859 * established using gss: in theory cr_principal should catch that
1860 * change, but in practice cr_principal can be null even in the gss case
1861 * since gssd doesn't always pass down a principal string.
1863 static bool is_gss_cred(struct svc_cred *cr)
1865 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1866 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1871 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1873 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
1874 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1875 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
1876 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1878 if (cr1->cr_principal == cr2->cr_principal)
1880 if (!cr1->cr_principal || !cr2->cr_principal)
1882 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
1885 static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1887 struct svc_cred *cr = &rqstp->rq_cred;
1890 if (!cr->cr_gss_mech)
1892 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1893 return service == RPC_GSS_SVC_INTEGRITY ||
1894 service == RPC_GSS_SVC_PRIVACY;
1897 static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1899 struct svc_cred *cr = &rqstp->rq_cred;
1901 if (!cl->cl_mach_cred)
1903 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1905 if (!svc_rqst_integrity_protected(rqstp))
1907 if (!cr->cr_principal)
1909 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1912 static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
1917 * This is opaque to client, so no need to byte-swap. Use
1918 * __force to keep sparse happy
1920 verf[0] = (__force __be32)get_seconds();
1921 verf[1] = (__force __be32)nn->clverifier_counter++;
1922 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
1925 static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
1927 clp->cl_clientid.cl_boot = nn->boot_time;
1928 clp->cl_clientid.cl_id = nn->clientid_counter++;
1929 gen_confirm(clp, nn);
1932 static struct nfs4_stid *
1933 find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
1935 struct nfs4_stid *ret;
1937 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1938 if (!ret || !ret->sc_type)
1943 static struct nfs4_stid *
1944 find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
1946 struct nfs4_stid *s;
1948 spin_lock(&cl->cl_lock);
1949 s = find_stateid_locked(cl, t);
1951 if (typemask & s->sc_type)
1952 atomic_inc(&s->sc_count);
1956 spin_unlock(&cl->cl_lock);
1960 static struct nfs4_client *create_client(struct xdr_netobj name,
1961 struct svc_rqst *rqstp, nfs4_verifier *verf)
1963 struct nfs4_client *clp;
1964 struct sockaddr *sa = svc_addr(rqstp);
1966 struct net *net = SVC_NET(rqstp);
1968 clp = alloc_client(name);
1972 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1977 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
1978 clp->cl_time = get_seconds();
1979 clear_bit(0, &clp->cl_cb_slot_busy);
1980 copy_verf(clp, verf);
1981 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
1982 clp->cl_cb_session = NULL;
1988 add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1990 struct rb_node **new = &(root->rb_node), *parent = NULL;
1991 struct nfs4_client *clp;
1994 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1997 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1998 new = &((*new)->rb_left);
2000 new = &((*new)->rb_right);
2003 rb_link_node(&new_clp->cl_namenode, parent, new);
2004 rb_insert_color(&new_clp->cl_namenode, root);
2007 static struct nfs4_client *
2008 find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2011 struct rb_node *node = root->rb_node;
2012 struct nfs4_client *clp;
2015 clp = rb_entry(node, struct nfs4_client, cl_namenode);
2016 cmp = compare_blob(&clp->cl_name, name);
2018 node = node->rb_left;
2020 node = node->rb_right;
2028 add_to_unconfirmed(struct nfs4_client *clp)
2030 unsigned int idhashval;
2031 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2033 lockdep_assert_held(&nn->client_lock);
2035 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2036 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
2037 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
2038 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
2039 renew_client_locked(clp);
2043 move_to_confirmed(struct nfs4_client *clp)
2045 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
2046 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2048 lockdep_assert_held(&nn->client_lock);
2050 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
2051 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
2052 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
2053 add_clp_to_name_tree(clp, &nn->conf_name_tree);
2054 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2055 renew_client_locked(clp);
2058 static struct nfs4_client *
2059 find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
2061 struct nfs4_client *clp;
2062 unsigned int idhashval = clientid_hashval(clid->cl_id);
2064 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
2065 if (same_clid(&clp->cl_clientid, clid)) {
2066 if ((bool)clp->cl_minorversion != sessions)
2068 renew_client_locked(clp);
2075 static struct nfs4_client *
2076 find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2078 struct list_head *tbl = nn->conf_id_hashtbl;
2080 lockdep_assert_held(&nn->client_lock);
2081 return find_client_in_id_table(tbl, clid, sessions);
2084 static struct nfs4_client *
2085 find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2087 struct list_head *tbl = nn->unconf_id_hashtbl;
2089 lockdep_assert_held(&nn->client_lock);
2090 return find_client_in_id_table(tbl, clid, sessions);
2093 static bool clp_used_exchangeid(struct nfs4_client *clp)
2095 return clp->cl_exchange_flags != 0;
2098 static struct nfs4_client *
2099 find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
2101 lockdep_assert_held(&nn->client_lock);
2102 return find_clp_in_name_tree(name, &nn->conf_name_tree);
2105 static struct nfs4_client *
2106 find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
2108 lockdep_assert_held(&nn->client_lock);
2109 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
2113 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
2115 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
2116 struct sockaddr *sa = svc_addr(rqstp);
2117 u32 scopeid = rpc_get_scope_id(sa);
2118 unsigned short expected_family;
2120 /* Currently, we only support tcp and tcp6 for the callback channel */
2121 if (se->se_callback_netid_len == 3 &&
2122 !memcmp(se->se_callback_netid_val, "tcp", 3))
2123 expected_family = AF_INET;
2124 else if (se->se_callback_netid_len == 4 &&
2125 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2126 expected_family = AF_INET6;
2130 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
2131 se->se_callback_addr_len,
2132 (struct sockaddr *)&conn->cb_addr,
2133 sizeof(conn->cb_addr));
2135 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
2138 if (conn->cb_addr.ss_family == AF_INET6)
2139 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
2141 conn->cb_prog = se->se_callback_prog;
2142 conn->cb_ident = se->se_callback_ident;
2143 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
2146 conn->cb_addr.ss_family = AF_UNSPEC;
2147 conn->cb_addrlen = 0;
2148 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
2149 "will not receive delegations\n",
2150 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2156 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
2159 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2161 struct xdr_buf *buf = resp->xdr.buf;
2162 struct nfsd4_slot *slot = resp->cstate.slot;
2165 dprintk("--> %s slot %p\n", __func__, slot);
2167 slot->sl_opcnt = resp->opcnt;
2168 slot->sl_status = resp->cstate.status;
2170 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
2171 if (nfsd4_not_cached(resp)) {
2172 slot->sl_datalen = 0;
2175 base = resp->cstate.data_offset;
2176 slot->sl_datalen = buf->len - base;
2177 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
2178 WARN("%s: sessions DRC could not cache compound\n", __func__);
2183 * Encode the replay sequence operation from the slot values.
2184 * If cachethis is FALSE encode the uncached rep error on the next
2185 * operation which sets resp->p and increments resp->opcnt for
2186 * nfs4svc_encode_compoundres.
2190 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2191 struct nfsd4_compoundres *resp)
2193 struct nfsd4_op *op;
2194 struct nfsd4_slot *slot = resp->cstate.slot;
2196 /* Encode the replayed sequence operation */
2197 op = &args->ops[resp->opcnt - 1];
2198 nfsd4_encode_operation(resp, op);
2200 /* Return nfserr_retry_uncached_rep in next operation. */
2201 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
2202 op = &args->ops[resp->opcnt++];
2203 op->status = nfserr_retry_uncached_rep;
2204 nfsd4_encode_operation(resp, op);
2210 * The sequence operation is not cached because we can use the slot and
2214 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2215 struct nfsd4_sequence *seq)
2217 struct nfsd4_slot *slot = resp->cstate.slot;
2218 struct xdr_stream *xdr = &resp->xdr;
2222 dprintk("--> %s slot %p\n", __func__, slot);
2224 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
2228 p = xdr_reserve_space(xdr, slot->sl_datalen);
2231 return nfserr_serverfault;
2233 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2234 xdr_commit_encode(xdr);
2236 resp->opcnt = slot->sl_opcnt;
2237 return slot->sl_status;
2241 * Set the exchange_id flags returned by the server.
2244 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2246 #ifdef CONFIG_NFSD_PNFS
2247 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
2249 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
2252 /* Referrals are supported, Migration is not. */
2253 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2255 /* set the wire flags to return to client. */
2256 clid->flags = new->cl_exchange_flags;
2259 static bool client_has_openowners(struct nfs4_client *clp)
2261 struct nfs4_openowner *oo;
2263 list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
2264 if (!list_empty(&oo->oo_owner.so_stateids))
2270 static bool client_has_state(struct nfs4_client *clp)
2272 return client_has_openowners(clp)
2273 #ifdef CONFIG_NFSD_PNFS
2274 || !list_empty(&clp->cl_lo_states)
2276 || !list_empty(&clp->cl_delegations)
2277 || !list_empty(&clp->cl_sessions);
2281 nfsd4_exchange_id(struct svc_rqst *rqstp,
2282 struct nfsd4_compound_state *cstate,
2283 struct nfsd4_exchange_id *exid)
2285 struct nfs4_client *conf, *new;
2286 struct nfs4_client *unconf = NULL;
2288 char addr_str[INET6_ADDRSTRLEN];
2289 nfs4_verifier verf = exid->verifier;
2290 struct sockaddr *sa = svc_addr(rqstp);
2291 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
2292 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2294 rpc_ntop(sa, addr_str, sizeof(addr_str));
2295 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
2296 "ip_addr=%s flags %x, spa_how %d\n",
2297 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
2298 addr_str, exid->flags, exid->spa_how);
2300 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
2301 return nfserr_inval;
2303 switch (exid->spa_how) {
2305 if (!svc_rqst_integrity_protected(rqstp))
2306 return nfserr_inval;
2309 default: /* checked by xdr code */
2312 return nfserr_encr_alg_unsupp;
2315 new = create_client(exid->clname, rqstp, &verf);
2317 return nfserr_jukebox;
2319 /* Cases below refer to rfc 5661 section 18.35.4: */
2320 spin_lock(&nn->client_lock);
2321 conf = find_confirmed_client_by_name(&exid->clname, nn);
2323 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2324 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2327 if (!clp_used_exchangeid(conf)) { /* buggy client */
2328 status = nfserr_inval;
2331 if (!mach_creds_match(conf, rqstp)) {
2332 status = nfserr_wrong_cred;
2335 if (!creds_match) { /* case 9 */
2336 status = nfserr_perm;
2339 if (!verfs_match) { /* case 8 */
2340 status = nfserr_not_same;
2344 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
2347 if (!creds_match) { /* case 3 */
2348 if (client_has_state(conf)) {
2349 status = nfserr_clid_inuse;
2354 if (verfs_match) { /* case 2 */
2355 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
2358 /* case 5, client reboot */
2363 if (update) { /* case 7 */
2364 status = nfserr_noent;
2368 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
2369 if (unconf) /* case 4, possible retry or client restart */
2370 unhash_client_locked(unconf);
2372 /* case 1 (normal case) */
2375 status = mark_client_expired_locked(conf);
2379 new->cl_minorversion = cstate->minorversion;
2380 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
2383 add_to_unconfirmed(new);
2386 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
2387 exid->clientid.cl_id = conf->cl_clientid.cl_id;
2389 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
2390 nfsd4_set_ex_flags(conf, exid);
2392 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
2393 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
2397 spin_unlock(&nn->client_lock);
2401 expire_client(unconf);
2406 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
2408 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2411 /* The slot is in use, and no response has been sent. */
2413 if (seqid == slot_seqid)
2414 return nfserr_jukebox;
2416 return nfserr_seq_misordered;
2418 /* Note unsigned 32-bit arithmetic handles wraparound: */
2419 if (likely(seqid == slot_seqid + 1))
2421 if (seqid == slot_seqid)
2422 return nfserr_replay_cache;
2423 return nfserr_seq_misordered;
2427 * Cache the create session result into the create session single DRC
2428 * slot cache by saving the xdr structure. sl_seqid has been set.
2429 * Do this for solo or embedded create session operations.
2432 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
2433 struct nfsd4_clid_slot *slot, __be32 nfserr)
2435 slot->sl_status = nfserr;
2436 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2440 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2441 struct nfsd4_clid_slot *slot)
2443 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2444 return slot->sl_status;
2447 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2448 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2449 1 + /* MIN tag is length with zero, only length */ \
2450 3 + /* version, opcount, opcode */ \
2451 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2452 /* seqid, slotID, slotID, cache */ \
2453 4 ) * sizeof(__be32))
2455 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2456 2 + /* verifier: AUTH_NULL, length 0 */\
2458 1 + /* MIN tag is length with zero, only length */ \
2459 3 + /* opcount, opcode, opstatus*/ \
2460 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2461 /* seqid, slotID, slotID, slotID, status */ \
2462 5 ) * sizeof(__be32))
2464 static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
2466 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2468 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2469 return nfserr_toosmall;
2470 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2471 return nfserr_toosmall;
2472 ca->headerpadsz = 0;
2473 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2474 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2475 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2476 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2477 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2478 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2480 * Note decreasing slot size below client's request may make it
2481 * difficult for client to function correctly, whereas
2482 * decreasing the number of slots will (just?) affect
2483 * performance. When short on memory we therefore prefer to
2484 * decrease number of slots instead of their size. Clients that
2485 * request larger slots than they need will get poor results:
2487 ca->maxreqs = nfsd4_get_drc_mem(ca);
2489 return nfserr_jukebox;
2494 #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2495 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2496 #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2497 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2499 static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2501 ca->headerpadsz = 0;
2504 * These RPC_MAX_HEADER macros are overkill, especially since we
2505 * don't even do gss on the backchannel yet. But this is still
2506 * less than 1k. Tighten up this estimate in the unlikely event
2507 * it turns out to be a problem for some client:
2509 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
2510 return nfserr_toosmall;
2511 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
2512 return nfserr_toosmall;
2513 ca->maxresp_cached = 0;
2515 return nfserr_toosmall;
2520 static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2522 switch (cbs->flavor) {
2528 * GSS case: the spec doesn't allow us to return this
2529 * error. But it also doesn't allow us not to support
2531 * I'd rather this fail hard than return some error the
2532 * client might think it can already handle:
2534 return nfserr_encr_alg_unsupp;
2539 nfsd4_create_session(struct svc_rqst *rqstp,
2540 struct nfsd4_compound_state *cstate,
2541 struct nfsd4_create_session *cr_ses)
2543 struct sockaddr *sa = svc_addr(rqstp);
2544 struct nfs4_client *conf, *unconf;
2545 struct nfs4_client *old = NULL;
2546 struct nfsd4_session *new;
2547 struct nfsd4_conn *conn;
2548 struct nfsd4_clid_slot *cs_slot = NULL;
2550 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2552 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2553 return nfserr_inval;
2554 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2557 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
2560 status = check_backchannel_attrs(&cr_ses->back_channel);
2562 goto out_release_drc_mem;
2563 status = nfserr_jukebox;
2564 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
2566 goto out_release_drc_mem;
2567 conn = alloc_conn_from_crses(rqstp, cr_ses);
2569 goto out_free_session;
2571 spin_lock(&nn->client_lock);
2572 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
2573 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
2574 WARN_ON_ONCE(conf && unconf);
2577 status = nfserr_wrong_cred;
2578 if (!mach_creds_match(conf, rqstp))
2580 cs_slot = &conf->cl_cs_slot;
2581 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2583 if (status == nfserr_replay_cache)
2584 status = nfsd4_replay_create_session(cr_ses, cs_slot);
2587 } else if (unconf) {
2588 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
2589 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
2590 status = nfserr_clid_inuse;
2593 status = nfserr_wrong_cred;
2594 if (!mach_creds_match(unconf, rqstp))
2596 cs_slot = &unconf->cl_cs_slot;
2597 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2599 /* an unconfirmed replay returns misordered */
2600 status = nfserr_seq_misordered;
2603 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
2605 status = mark_client_expired_locked(old);
2611 move_to_confirmed(unconf);
2614 status = nfserr_stale_clientid;
2619 * We do not support RDMA or persistent sessions
2621 cr_ses->flags &= ~SESSION4_PERSIST;
2622 cr_ses->flags &= ~SESSION4_RDMA;
2624 init_session(rqstp, new, conf, cr_ses);
2625 nfsd4_get_session_locked(new);
2627 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
2628 NFS4_MAX_SESSIONID_LEN);
2629 cs_slot->sl_seqid++;
2630 cr_ses->seqid = cs_slot->sl_seqid;
2632 /* cache solo and embedded create sessions under the client_lock */
2633 nfsd4_cache_create_session(cr_ses, cs_slot, status);
2634 spin_unlock(&nn->client_lock);
2635 /* init connection and backchannel */
2636 nfsd4_init_conn(rqstp, conn, new);
2637 nfsd4_put_session(new);
2642 spin_unlock(&nn->client_lock);
2647 __free_session(new);
2648 out_release_drc_mem:
2649 nfsd4_put_drc_mem(&cr_ses->fore_channel);
2653 static __be32 nfsd4_map_bcts_dir(u32 *dir)
2656 case NFS4_CDFC4_FORE:
2657 case NFS4_CDFC4_BACK:
2659 case NFS4_CDFC4_FORE_OR_BOTH:
2660 case NFS4_CDFC4_BACK_OR_BOTH:
2661 *dir = NFS4_CDFC4_BOTH;
2664 return nfserr_inval;
2667 __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2669 struct nfsd4_session *session = cstate->session;
2670 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2673 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2676 spin_lock(&nn->client_lock);
2677 session->se_cb_prog = bc->bc_cb_program;
2678 session->se_cb_sec = bc->bc_cb_sec;
2679 spin_unlock(&nn->client_lock);
2681 nfsd4_probe_callback(session->se_client);
2686 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2687 struct nfsd4_compound_state *cstate,
2688 struct nfsd4_bind_conn_to_session *bcts)
2691 struct nfsd4_conn *conn;
2692 struct nfsd4_session *session;
2693 struct net *net = SVC_NET(rqstp);
2694 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2696 if (!nfsd4_last_compound_op(rqstp))
2697 return nfserr_not_only_op;
2698 spin_lock(&nn->client_lock);
2699 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
2700 spin_unlock(&nn->client_lock);
2702 goto out_no_session;
2703 status = nfserr_wrong_cred;
2704 if (!mach_creds_match(session->se_client, rqstp))
2706 status = nfsd4_map_bcts_dir(&bcts->dir);
2709 conn = alloc_conn(rqstp, bcts->dir);
2710 status = nfserr_jukebox;
2713 nfsd4_init_conn(rqstp, conn, session);
2716 nfsd4_put_session(session);
2721 static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2725 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2729 nfsd4_destroy_session(struct svc_rqst *r,
2730 struct nfsd4_compound_state *cstate,
2731 struct nfsd4_destroy_session *sessionid)
2733 struct nfsd4_session *ses;
2735 int ref_held_by_me = 0;
2736 struct net *net = SVC_NET(r);
2737 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2739 status = nfserr_not_only_op;
2740 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
2741 if (!nfsd4_last_compound_op(r))
2745 dump_sessionid(__func__, &sessionid->sessionid);
2746 spin_lock(&nn->client_lock);
2747 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
2749 goto out_client_lock;
2750 status = nfserr_wrong_cred;
2751 if (!mach_creds_match(ses->se_client, r))
2752 goto out_put_session;
2753 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
2755 goto out_put_session;
2756 unhash_session(ses);
2757 spin_unlock(&nn->client_lock);
2759 nfsd4_probe_callback_sync(ses->se_client);
2761 spin_lock(&nn->client_lock);
2764 nfsd4_put_session_locked(ses);
2766 spin_unlock(&nn->client_lock);
2771 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
2773 struct nfsd4_conn *c;
2775 list_for_each_entry(c, &s->se_conns, cn_persession) {
2776 if (c->cn_xprt == xpt) {
2783 static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
2785 struct nfs4_client *clp = ses->se_client;
2786 struct nfsd4_conn *c;
2787 __be32 status = nfs_ok;
2790 spin_lock(&clp->cl_lock);
2791 c = __nfsd4_find_conn(new->cn_xprt, ses);
2794 status = nfserr_conn_not_bound_to_session;
2795 if (clp->cl_mach_cred)
2797 __nfsd4_hash_conn(new, ses);
2798 spin_unlock(&clp->cl_lock);
2799 ret = nfsd4_register_conn(new);
2801 /* oops; xprt is already down: */
2802 nfsd4_conn_lost(&new->cn_xpt_user);
2805 spin_unlock(&clp->cl_lock);
2810 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2812 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2814 return args->opcnt > session->se_fchannel.maxops;
2817 static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2818 struct nfsd4_session *session)
2820 struct xdr_buf *xb = &rqstp->rq_arg;
2822 return xb->len > session->se_fchannel.maxreq_sz;
2826 nfsd4_sequence(struct svc_rqst *rqstp,
2827 struct nfsd4_compound_state *cstate,
2828 struct nfsd4_sequence *seq)
2830 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2831 struct xdr_stream *xdr = &resp->xdr;
2832 struct nfsd4_session *session;
2833 struct nfs4_client *clp;
2834 struct nfsd4_slot *slot;
2835 struct nfsd4_conn *conn;
2838 struct net *net = SVC_NET(rqstp);
2839 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2841 if (resp->opcnt != 1)
2842 return nfserr_sequence_pos;
2845 * Will be either used or freed by nfsd4_sequence_check_conn
2848 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2850 return nfserr_jukebox;
2852 spin_lock(&nn->client_lock);
2853 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
2855 goto out_no_session;
2856 clp = session->se_client;
2858 status = nfserr_too_many_ops;
2859 if (nfsd4_session_too_many_ops(rqstp, session))
2860 goto out_put_session;
2862 status = nfserr_req_too_big;
2863 if (nfsd4_request_too_big(rqstp, session))
2864 goto out_put_session;
2866 status = nfserr_badslot;
2867 if (seq->slotid >= session->se_fchannel.maxreqs)
2868 goto out_put_session;
2870 slot = session->se_slots[seq->slotid];
2871 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2873 /* We do not negotiate the number of slots yet, so set the
2874 * maxslots to the session maxreqs which is used to encode
2875 * sr_highest_slotid and the sr_target_slot id to maxslots */
2876 seq->maxslots = session->se_fchannel.maxreqs;
2878 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2879 slot->sl_flags & NFSD4_SLOT_INUSE);
2880 if (status == nfserr_replay_cache) {
2881 status = nfserr_seq_misordered;
2882 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
2883 goto out_put_session;
2884 cstate->slot = slot;
2885 cstate->session = session;
2887 /* Return the cached reply status and set cstate->status
2888 * for nfsd4_proc_compound processing */
2889 status = nfsd4_replay_cache_entry(resp, seq);
2890 cstate->status = nfserr_replay_cache;
2894 goto out_put_session;
2896 status = nfsd4_sequence_check_conn(conn, session);
2899 goto out_put_session;
2901 buflen = (seq->cachethis) ?
2902 session->se_fchannel.maxresp_cached :
2903 session->se_fchannel.maxresp_sz;
2904 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2906 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
2907 goto out_put_session;
2908 svc_reserve(rqstp, buflen);
2911 /* Success! bump slot seqid */
2912 slot->sl_seqid = seq->seqid;
2913 slot->sl_flags |= NFSD4_SLOT_INUSE;
2915 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
2917 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
2919 cstate->slot = slot;
2920 cstate->session = session;
2924 switch (clp->cl_cb_state) {
2926 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2928 case NFSD4_CB_FAULT:
2929 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2932 seq->status_flags = 0;
2934 if (!list_empty(&clp->cl_revoked))
2935 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
2939 spin_unlock(&nn->client_lock);
2942 nfsd4_put_session_locked(session);
2943 goto out_no_session;
2947 nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2949 struct nfsd4_compound_state *cs = &resp->cstate;
2951 if (nfsd4_has_session(cs)) {
2952 if (cs->status != nfserr_replay_cache) {
2953 nfsd4_store_cache_entry(resp);
2954 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2956 /* Drop session reference that was taken in nfsd4_sequence() */
2957 nfsd4_put_session(cs->session);
2959 put_client_renew(cs->clp);
2963 nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2965 struct nfs4_client *conf, *unconf;
2966 struct nfs4_client *clp = NULL;
2968 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2970 spin_lock(&nn->client_lock);
2971 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
2972 conf = find_confirmed_client(&dc->clientid, true, nn);
2973 WARN_ON_ONCE(conf && unconf);
2976 if (client_has_state(conf)) {
2977 status = nfserr_clientid_busy;
2980 status = mark_client_expired_locked(conf);
2987 status = nfserr_stale_clientid;
2990 if (!mach_creds_match(clp, rqstp)) {
2992 status = nfserr_wrong_cred;
2995 unhash_client_locked(clp);
2997 spin_unlock(&nn->client_lock);
3004 nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
3008 if (rc->rca_one_fs) {
3009 if (!cstate->current_fh.fh_dentry)
3010 return nfserr_nofilehandle;
3012 * We don't take advantage of the rca_one_fs case.
3013 * That's OK, it's optional, we can safely ignore it.
3018 status = nfserr_complete_already;
3019 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
3020 &cstate->session->se_client->cl_flags))
3023 status = nfserr_stale_clientid;
3024 if (is_client_expired(cstate->session->se_client))
3026 * The following error isn't really legal.
3027 * But we only get here if the client just explicitly
3028 * destroyed the client. Surely it no longer cares what
3029 * error it gets back on an operation for the dead
3035 nfsd4_client_record_create(cstate->session->se_client);
3041 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3042 struct nfsd4_setclientid *setclid)
3044 struct xdr_netobj clname = setclid->se_name;
3045 nfs4_verifier clverifier = setclid->se_verf;
3046 struct nfs4_client *conf, *new;
3047 struct nfs4_client *unconf = NULL;
3049 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3051 new = create_client(clname, rqstp, &clverifier);
3053 return nfserr_jukebox;
3054 /* Cases below refer to rfc 3530 section 14.2.33: */
3055 spin_lock(&nn->client_lock);
3056 conf = find_confirmed_client_by_name(&clname, nn);
3057 if (conf && client_has_state(conf)) {
3059 status = nfserr_clid_inuse;
3060 if (clp_used_exchangeid(conf))
3062 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
3063 char addr_str[INET6_ADDRSTRLEN];
3064 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
3066 dprintk("NFSD: setclientid: string in use by client "
3067 "at %s\n", addr_str);
3071 unconf = find_unconfirmed_client_by_name(&clname, nn);
3073 unhash_client_locked(unconf);
3074 if (conf && same_verf(&conf->cl_verifier, &clverifier)) {
3075 /* case 1: probable callback update */
3076 copy_clid(new, conf);
3077 gen_confirm(new, nn);
3078 } else /* case 4 (new client) or cases 2, 3 (client reboot): */
3080 new->cl_minorversion = 0;
3081 gen_callback(new, setclid, rqstp);
3082 add_to_unconfirmed(new);
3083 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
3084 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
3085 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
3089 spin_unlock(&nn->client_lock);
3093 expire_client(unconf);
3099 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
3100 struct nfsd4_compound_state *cstate,
3101 struct nfsd4_setclientid_confirm *setclientid_confirm)
3103 struct nfs4_client *conf, *unconf;
3104 struct nfs4_client *old = NULL;
3105 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
3106 clientid_t * clid = &setclientid_confirm->sc_clientid;
3108 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3110 if (STALE_CLIENTID(clid, nn))
3111 return nfserr_stale_clientid;
3113 spin_lock(&nn->client_lock);
3114 conf = find_confirmed_client(clid, false, nn);
3115 unconf = find_unconfirmed_client(clid, false, nn);
3117 * We try hard to give out unique clientid's, so if we get an
3118 * attempt to confirm the same clientid with a different cred,
3119 * the client may be buggy; this should never happen.
3121 * Nevertheless, RFC 7530 recommends INUSE for this case:
3123 status = nfserr_clid_inuse;
3124 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3126 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3128 /* cases below refer to rfc 3530 section 14.2.34: */
3129 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
3130 if (conf && !unconf) /* case 2: probable retransmit */
3132 else /* case 4: client hasn't noticed we rebooted yet? */
3133 status = nfserr_stale_clientid;
3137 if (conf) { /* case 1: callback update */
3139 unhash_client_locked(old);
3140 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
3141 } else { /* case 3: normal case; new or rebooted client */
3142 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3144 status = nfserr_clid_inuse;
3145 if (client_has_state(old)
3146 && !same_creds(&unconf->cl_cred,
3149 status = mark_client_expired_locked(old);
3155 move_to_confirmed(unconf);
3158 get_client_locked(conf);
3159 spin_unlock(&nn->client_lock);
3160 nfsd4_probe_callback(conf);
3161 spin_lock(&nn->client_lock);
3162 put_client_renew_locked(conf);
3164 spin_unlock(&nn->client_lock);
3170 static struct nfs4_file *nfsd4_alloc_file(void)
3172 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3175 /* OPEN Share state helper functions */
3176 static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
3177 struct nfs4_file *fp)
3179 lockdep_assert_held(&state_lock);
3181 atomic_set(&fp->fi_ref, 1);
3182 spin_lock_init(&fp->fi_lock);
3183 INIT_LIST_HEAD(&fp->fi_stateids);
3184 INIT_LIST_HEAD(&fp->fi_delegations);
3185 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
3186 fh_copy_shallow(&fp->fi_fhandle, fh);
3187 fp->fi_deleg_file = NULL;
3188 fp->fi_had_conflict = false;
3189 fp->fi_share_deny = 0;
3190 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3191 memset(fp->fi_access, 0, sizeof(fp->fi_access));
3192 #ifdef CONFIG_NFSD_PNFS
3193 INIT_LIST_HEAD(&fp->fi_lo_states);
3194 atomic_set(&fp->fi_lo_recalls, 0);
3196 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
3200 nfsd4_free_slabs(void)
3202 kmem_cache_destroy(odstate_slab);
3203 kmem_cache_destroy(openowner_slab);
3204 kmem_cache_destroy(lockowner_slab);
3205 kmem_cache_destroy(file_slab);
3206 kmem_cache_destroy(stateid_slab);
3207 kmem_cache_destroy(deleg_slab);
3211 nfsd4_init_slabs(void)
3213 openowner_slab = kmem_cache_create("nfsd4_openowners",
3214 sizeof(struct nfs4_openowner), 0, 0, NULL);
3215 if (openowner_slab == NULL)
3217 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
3218 sizeof(struct nfs4_lockowner), 0, 0, NULL);
3219 if (lockowner_slab == NULL)
3220 goto out_free_openowner_slab;
3221 file_slab = kmem_cache_create("nfsd4_files",
3222 sizeof(struct nfs4_file), 0, 0, NULL);
3223 if (file_slab == NULL)
3224 goto out_free_lockowner_slab;
3225 stateid_slab = kmem_cache_create("nfsd4_stateids",
3226 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
3227 if (stateid_slab == NULL)
3228 goto out_free_file_slab;
3229 deleg_slab = kmem_cache_create("nfsd4_delegations",
3230 sizeof(struct nfs4_delegation), 0, 0, NULL);
3231 if (deleg_slab == NULL)
3232 goto out_free_stateid_slab;
3233 odstate_slab = kmem_cache_create("nfsd4_odstate",
3234 sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
3235 if (odstate_slab == NULL)
3236 goto out_free_deleg_slab;
3239 out_free_deleg_slab:
3240 kmem_cache_destroy(deleg_slab);
3241 out_free_stateid_slab:
3242 kmem_cache_destroy(stateid_slab);
3244 kmem_cache_destroy(file_slab);
3245 out_free_lockowner_slab:
3246 kmem_cache_destroy(lockowner_slab);
3247 out_free_openowner_slab:
3248 kmem_cache_destroy(openowner_slab);
3250 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3254 static void init_nfs4_replay(struct nfs4_replay *rp)
3256 rp->rp_status = nfserr_serverfault;
3258 rp->rp_buf = rp->rp_ibuf;
3259 mutex_init(&rp->rp_mutex);
3262 static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3263 struct nfs4_stateowner *so)
3265 if (!nfsd4_has_session(cstate)) {
3266 mutex_lock(&so->so_replay.rp_mutex);
3267 cstate->replay_owner = nfs4_get_stateowner(so);
3271 void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3273 struct nfs4_stateowner *so = cstate->replay_owner;
3276 cstate->replay_owner = NULL;
3277 mutex_unlock(&so->so_replay.rp_mutex);
3278 nfs4_put_stateowner(so);
3282 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
3284 struct nfs4_stateowner *sop;
3286 sop = kmem_cache_alloc(slab, GFP_KERNEL);
3290 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3291 if (!sop->so_owner.data) {
3292 kmem_cache_free(slab, sop);
3295 sop->so_owner.len = owner->len;
3297 INIT_LIST_HEAD(&sop->so_stateids);
3298 sop->so_client = clp;
3299 init_nfs4_replay(&sop->so_replay);
3300 atomic_set(&sop->so_count, 1);
3304 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
3306 lockdep_assert_held(&clp->cl_lock);
3308 list_add(&oo->oo_owner.so_strhash,
3309 &clp->cl_ownerstr_hashtbl[strhashval]);
3310 list_add(&oo->oo_perclient, &clp->cl_openowners);
3313 static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3315 unhash_openowner_locked(openowner(so));
3318 static void nfs4_free_openowner(struct nfs4_stateowner *so)
3320 struct nfs4_openowner *oo = openowner(so);
3322 kmem_cache_free(openowner_slab, oo);
3325 static const struct nfs4_stateowner_operations openowner_ops = {
3326 .so_unhash = nfs4_unhash_openowner,
3327 .so_free = nfs4_free_openowner,
3330 static struct nfs4_openowner *
3331 alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
3332 struct nfsd4_compound_state *cstate)
3334 struct nfs4_client *clp = cstate->clp;
3335 struct nfs4_openowner *oo, *ret;
3337 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3340 oo->oo_owner.so_ops = &openowner_ops;
3341 oo->oo_owner.so_is_open_owner = 1;
3342 oo->oo_owner.so_seqid = open->op_seqid;
3344 if (nfsd4_has_session(cstate))
3345 oo->oo_flags |= NFS4_OO_CONFIRMED;
3347 oo->oo_last_closed_stid = NULL;
3348 INIT_LIST_HEAD(&oo->oo_close_lru);
3349 spin_lock(&clp->cl_lock);
3350 ret = find_openstateowner_str_locked(strhashval, open, clp);
3352 hash_openowner(oo, clp, strhashval);
3355 nfs4_free_stateowner(&oo->oo_owner);
3357 spin_unlock(&clp->cl_lock);
3361 static void init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
3362 struct nfs4_openowner *oo = open->op_openowner;
3364 atomic_inc(&stp->st_stid.sc_count);
3365 stp->st_stid.sc_type = NFS4_OPEN_STID;
3366 INIT_LIST_HEAD(&stp->st_locks);
3367 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
3369 stp->st_stid.sc_file = fp;
3370 stp->st_access_bmap = 0;
3371 stp->st_deny_bmap = 0;
3372 stp->st_openstp = NULL;
3373 init_rwsem(&stp->st_rwsem);
3374 spin_lock(&oo->oo_owner.so_client->cl_lock);
3375 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
3376 spin_lock(&fp->fi_lock);
3377 list_add(&stp->st_perfile, &fp->fi_stateids);
3378 spin_unlock(&fp->fi_lock);
3379 spin_unlock(&oo->oo_owner.so_client->cl_lock);
3383 * In the 4.0 case we need to keep the owners around a little while to handle
3384 * CLOSE replay. We still do need to release any file access that is held by
3385 * them before returning however.
3388 move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
3390 struct nfs4_ol_stateid *last;
3391 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3392 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3395 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
3398 * We know that we hold one reference via nfsd4_close, and another
3399 * "persistent" reference for the client. If the refcount is higher
3400 * than 2, then there are still calls in progress that are using this
3401 * stateid. We can't put the sc_file reference until they are finished.
3402 * Wait for the refcount to drop to 2. Since it has been unhashed,
3403 * there should be no danger of the refcount going back up again at
3406 wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3408 release_all_access(s);
3409 if (s->st_stid.sc_file) {
3410 put_nfs4_file(s->st_stid.sc_file);
3411 s->st_stid.sc_file = NULL;
3414 spin_lock(&nn->client_lock);
3415 last = oo->oo_last_closed_stid;
3416 oo->oo_last_closed_stid = s;
3417 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
3418 oo->oo_time = get_seconds();
3419 spin_unlock(&nn->client_lock);
3421 nfs4_put_stid(&last->st_stid);
3424 /* search file_hashtbl[] for file */
3425 static struct nfs4_file *
3426 find_file_locked(struct knfsd_fh *fh, unsigned int hashval)
3428 struct nfs4_file *fp;
3430 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash) {
3431 if (fh_match(&fp->fi_fhandle, fh)) {
3432 if (atomic_inc_not_zero(&fp->fi_ref))
3440 find_file(struct knfsd_fh *fh)
3442 struct nfs4_file *fp;
3443 unsigned int hashval = file_hashval(fh);
3446 fp = find_file_locked(fh, hashval);
3451 static struct nfs4_file *
3452 find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
3454 struct nfs4_file *fp;
3455 unsigned int hashval = file_hashval(fh);
3458 fp = find_file_locked(fh, hashval);
3463 spin_lock(&state_lock);
3464 fp = find_file_locked(fh, hashval);
3465 if (likely(fp == NULL)) {
3466 nfsd4_init_file(fh, hashval, new);
3469 spin_unlock(&state_lock);
3475 * Called to check deny when READ with all zero stateid or
3476 * WRITE with all zero or all one stateid
3479 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3481 struct nfs4_file *fp;
3482 __be32 ret = nfs_ok;
3484 fp = find_file(¤t_fh->fh_handle);
3487 /* Check for conflicting share reservations */
3488 spin_lock(&fp->fi_lock);
3489 if (fp->fi_share_deny & deny_type)
3490 ret = nfserr_locked;
3491 spin_unlock(&fp->fi_lock);
3496 static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
3498 struct nfs4_delegation *dp = cb_to_delegation(cb);
3499 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3502 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
3505 * We can't do this in nfsd_break_deleg_cb because it is
3506 * already holding inode->i_lock.
3508 * If the dl_time != 0, then we know that it has already been
3509 * queued for a lease break. Don't queue it again.
3511 spin_lock(&state_lock);
3512 if (dp->dl_time == 0) {
3513 dp->dl_time = get_seconds();
3514 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
3516 spin_unlock(&state_lock);
3519 static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
3520 struct rpc_task *task)
3522 struct nfs4_delegation *dp = cb_to_delegation(cb);
3524 if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID)
3527 switch (task->tk_status) {
3531 case -NFS4ERR_BAD_STATEID:
3533 * Race: client probably got cb_recall before open reply
3534 * granting delegation.
3536 if (dp->dl_retries--) {
3537 rpc_delay(task, 2 * HZ);
3546 static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
3548 struct nfs4_delegation *dp = cb_to_delegation(cb);
3550 nfs4_put_stid(&dp->dl_stid);
3553 static struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
3554 .prepare = nfsd4_cb_recall_prepare,
3555 .done = nfsd4_cb_recall_done,
3556 .release = nfsd4_cb_recall_release,
3559 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3562 * We're assuming the state code never drops its reference
3563 * without first removing the lease. Since we're in this lease
3564 * callback (and since the lease code is serialized by the kernel
3565 * lock) we know the server hasn't removed the lease yet, we know
3566 * it's safe to take a reference.
3568 atomic_inc(&dp->dl_stid.sc_count);
3569 nfsd4_run_cb(&dp->dl_recall);
3572 /* Called from break_lease() with i_lock held. */
3574 nfsd_break_deleg_cb(struct file_lock *fl)
3577 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3578 struct nfs4_delegation *dp;
3581 WARN(1, "(%p)->fl_owner NULL\n", fl);
3584 if (fp->fi_had_conflict) {
3585 WARN(1, "duplicate break on %p\n", fp);
3589 * We don't want the locks code to timeout the lease for us;
3590 * we'll remove it ourself if a delegation isn't returned
3593 fl->fl_break_time = 0;
3595 spin_lock(&fp->fi_lock);
3596 fp->fi_had_conflict = true;
3598 * If there are no delegations on the list, then return true
3599 * so that the lease code will go ahead and delete it.
3601 if (list_empty(&fp->fi_delegations))
3604 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3605 nfsd_break_one_deleg(dp);
3606 spin_unlock(&fp->fi_lock);
3611 nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
3612 struct list_head *dispose)
3615 return lease_modify(onlist, arg, dispose);
3620 static const struct lock_manager_operations nfsd_lease_mng_ops = {
3621 .lm_break = nfsd_break_deleg_cb,
3622 .lm_change = nfsd_change_deleg_cb,
3625 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3627 if (nfsd4_has_session(cstate))
3629 if (seqid == so->so_seqid - 1)
3630 return nfserr_replay_me;
3631 if (seqid == so->so_seqid)
3633 return nfserr_bad_seqid;
3636 static __be32 lookup_clientid(clientid_t *clid,
3637 struct nfsd4_compound_state *cstate,
3638 struct nfsd_net *nn)
3640 struct nfs4_client *found;
3643 found = cstate->clp;
3644 if (!same_clid(&found->cl_clientid, clid))
3645 return nfserr_stale_clientid;
3649 if (STALE_CLIENTID(clid, nn))
3650 return nfserr_stale_clientid;
3653 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3654 * cached already then we know this is for is for v4.0 and "sessions"
3657 WARN_ON_ONCE(cstate->session);
3658 spin_lock(&nn->client_lock);
3659 found = find_confirmed_client(clid, false, nn);
3661 spin_unlock(&nn->client_lock);
3662 return nfserr_expired;
3664 atomic_inc(&found->cl_refcount);
3665 spin_unlock(&nn->client_lock);
3667 /* Cache the nfs4_client in cstate! */
3668 cstate->clp = found;
3673 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
3674 struct nfsd4_open *open, struct nfsd_net *nn)
3676 clientid_t *clientid = &open->op_clientid;
3677 struct nfs4_client *clp = NULL;
3678 unsigned int strhashval;
3679 struct nfs4_openowner *oo = NULL;
3682 if (STALE_CLIENTID(&open->op_clientid, nn))
3683 return nfserr_stale_clientid;
3685 * In case we need it later, after we've already created the
3686 * file and don't want to risk a further failure:
3688 open->op_file = nfsd4_alloc_file();
3689 if (open->op_file == NULL)
3690 return nfserr_jukebox;
3692 status = lookup_clientid(clientid, cstate, nn);
3697 strhashval = ownerstr_hashval(&open->op_owner);
3698 oo = find_openstateowner_str(strhashval, open, clp);
3699 open->op_openowner = oo;
3703 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
3704 /* Replace unconfirmed owners without checking for replay. */
3705 release_openowner(oo);
3706 open->op_openowner = NULL;
3709 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3714 oo = alloc_init_open_stateowner(strhashval, open, cstate);
3716 return nfserr_jukebox;
3717 open->op_openowner = oo;
3719 open->op_stp = nfs4_alloc_open_stateid(clp);
3721 return nfserr_jukebox;
3723 if (nfsd4_has_session(cstate) &&
3724 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
3725 open->op_odstate = alloc_clnt_odstate(clp);
3726 if (!open->op_odstate)
3727 return nfserr_jukebox;
3733 static inline __be32
3734 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3736 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3737 return nfserr_openmode;
3742 static int share_access_to_flags(u32 share_access)
3744 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3747 static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
3749 struct nfs4_stid *ret;
3751 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
3754 return delegstateid(ret);
3757 static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3759 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3760 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3764 nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
3765 struct nfs4_delegation **dp)
3768 __be32 status = nfserr_bad_stateid;
3769 struct nfs4_delegation *deleg;
3771 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3774 flags = share_access_to_flags(open->op_share_access);
3775 status = nfs4_check_delegmode(deleg, flags);
3777 nfs4_put_stid(&deleg->dl_stid);
3782 if (!nfsd4_is_deleg_cur(open))
3786 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
3790 static struct nfs4_ol_stateid *
3791 nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
3793 struct nfs4_ol_stateid *local, *ret = NULL;
3794 struct nfs4_openowner *oo = open->op_openowner;
3796 spin_lock(&fp->fi_lock);
3797 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
3798 /* ignore lock owners */
3799 if (local->st_stateowner->so_is_open_owner == 0)
3801 if (local->st_stateowner == &oo->oo_owner) {
3803 atomic_inc(&ret->st_stid.sc_count);
3807 spin_unlock(&fp->fi_lock);
3811 static inline int nfs4_access_to_access(u32 nfs4_access)
3815 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3816 flags |= NFSD_MAY_READ;
3817 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3818 flags |= NFSD_MAY_WRITE;
3822 static inline __be32
3823 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3824 struct nfsd4_open *open)
3826 struct iattr iattr = {
3827 .ia_valid = ATTR_SIZE,
3830 if (!open->op_truncate)
3832 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
3833 return nfserr_inval;
3834 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3837 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
3838 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3839 struct nfsd4_open *open)
3841 struct file *filp = NULL;
3843 int oflag = nfs4_access_to_omode(open->op_share_access);
3844 int access = nfs4_access_to_access(open->op_share_access);
3845 unsigned char old_access_bmap, old_deny_bmap;
3847 spin_lock(&fp->fi_lock);
3850 * Are we trying to set a deny mode that would conflict with
3853 status = nfs4_file_check_deny(fp, open->op_share_deny);
3854 if (status != nfs_ok) {
3855 spin_unlock(&fp->fi_lock);
3859 /* set access to the file */
3860 status = nfs4_file_get_access(fp, open->op_share_access);
3861 if (status != nfs_ok) {
3862 spin_unlock(&fp->fi_lock);
3866 /* Set access bits in stateid */
3867 old_access_bmap = stp->st_access_bmap;
3868 set_access(open->op_share_access, stp);
3870 /* Set new deny mask */
3871 old_deny_bmap = stp->st_deny_bmap;
3872 set_deny(open->op_share_deny, stp);
3873 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3875 if (!fp->fi_fds[oflag]) {
3876 spin_unlock(&fp->fi_lock);
3877 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
3879 goto out_put_access;
3880 spin_lock(&fp->fi_lock);
3881 if (!fp->fi_fds[oflag]) {
3882 fp->fi_fds[oflag] = filp;
3886 spin_unlock(&fp->fi_lock);
3890 status = nfsd4_truncate(rqstp, cur_fh, open);
3892 goto out_put_access;
3896 stp->st_access_bmap = old_access_bmap;
3897 nfs4_file_put_access(fp, open->op_share_access);
3898 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3903 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)
3906 unsigned char old_deny_bmap = stp->st_deny_bmap;
3908 if (!test_access(open->op_share_access, stp))
3909 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
3911 /* test and set deny mode */
3912 spin_lock(&fp->fi_lock);
3913 status = nfs4_file_check_deny(fp, open->op_share_deny);
3914 if (status == nfs_ok) {
3915 set_deny(open->op_share_deny, stp);
3916 fp->fi_share_deny |=
3917 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3919 spin_unlock(&fp->fi_lock);
3921 if (status != nfs_ok)
3924 status = nfsd4_truncate(rqstp, cur_fh, open);
3925 if (status != nfs_ok)
3926 reset_union_bmap_deny(old_deny_bmap, stp);
3930 /* Should we give out recallable state?: */
3931 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3933 if (clp->cl_cb_state == NFSD4_CB_UP)
3936 * In the sessions case, since we don't have to establish a
3937 * separate connection for callbacks, we assume it's OK
3938 * until we hear otherwise:
3940 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3943 static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
3945 struct file_lock *fl;
3947 fl = locks_alloc_lock();
3950 fl->fl_lmops = &nfsd_lease_mng_ops;
3951 fl->fl_flags = FL_DELEG;
3952 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3953 fl->fl_end = OFFSET_MAX;
3954 fl->fl_owner = (fl_owner_t)fp;
3955 fl->fl_pid = current->tgid;
3959 static int nfs4_setlease(struct nfs4_delegation *dp)
3961 struct nfs4_file *fp = dp->dl_stid.sc_file;
3962 struct file_lock *fl;
3966 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
3969 filp = find_readable_file(fp);
3971 /* We should always have a readable file here */
3973 locks_free_lock(fl);
3977 status = vfs_setlease(filp, fl->fl_type, &fl, NULL);
3979 locks_free_lock(fl);
3982 spin_lock(&state_lock);
3983 spin_lock(&fp->fi_lock);
3984 /* Did the lease get broken before we took the lock? */
3986 if (fp->fi_had_conflict)
3989 if (fp->fi_deleg_file) {
3992 hash_delegation_locked(dp, fp);
3995 fp->fi_deleg_file = filp;
3996 fp->fi_delegees = 1;
3997 hash_delegation_locked(dp, fp);
3998 spin_unlock(&fp->fi_lock);
3999 spin_unlock(&state_lock);
4002 spin_unlock(&fp->fi_lock);
4003 spin_unlock(&state_lock);
4009 static struct nfs4_delegation *
4010 nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
4011 struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate)
4014 struct nfs4_delegation *dp;
4016 if (fp->fi_had_conflict)
4017 return ERR_PTR(-EAGAIN);
4019 dp = alloc_init_deleg(clp, fh, odstate);
4021 return ERR_PTR(-ENOMEM);
4024 spin_lock(&state_lock);
4025 spin_lock(&fp->fi_lock);
4026 dp->dl_stid.sc_file = fp;
4027 if (!fp->fi_deleg_file) {
4028 spin_unlock(&fp->fi_lock);
4029 spin_unlock(&state_lock);
4030 status = nfs4_setlease(dp);
4033 if (fp->fi_had_conflict) {
4038 hash_delegation_locked(dp, fp);
4041 spin_unlock(&fp->fi_lock);
4042 spin_unlock(&state_lock);
4045 put_clnt_odstate(dp->dl_clnt_odstate);
4046 nfs4_put_stid(&dp->dl_stid);
4047 return ERR_PTR(status);
4052 static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
4054 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4055 if (status == -EAGAIN)
4056 open->op_why_no_deleg = WND4_CONTENTION;
4058 open->op_why_no_deleg = WND4_RESOURCE;
4059 switch (open->op_deleg_want) {
4060 case NFS4_SHARE_WANT_READ_DELEG:
4061 case NFS4_SHARE_WANT_WRITE_DELEG:
4062 case NFS4_SHARE_WANT_ANY_DELEG:
4064 case NFS4_SHARE_WANT_CANCEL:
4065 open->op_why_no_deleg = WND4_CANCELLED;
4067 case NFS4_SHARE_WANT_NO_DELEG:
4074 * Attempt to hand out a delegation.
4076 * Note we don't support write delegations, and won't until the vfs has
4077 * proper support for them.
4080 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
4081 struct nfs4_ol_stateid *stp)
4083 struct nfs4_delegation *dp;
4084 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
4085 struct nfs4_client *clp = stp->st_stid.sc_client;
4089 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
4090 open->op_recall = 0;
4091 switch (open->op_claim_type) {
4092 case NFS4_OPEN_CLAIM_PREVIOUS:
4094 open->op_recall = 1;
4095 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
4098 case NFS4_OPEN_CLAIM_NULL:
4099 case NFS4_OPEN_CLAIM_FH:
4101 * Let's not give out any delegations till everyone's
4102 * had the chance to reclaim theirs, *and* until
4103 * NLM locks have all been reclaimed:
4105 if (locks_in_grace(clp->net))
4107 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
4110 * Also, if the file was opened for write or
4111 * create, there's a good chance the client's
4112 * about to write to it, resulting in an
4113 * immediate recall (since we don't support
4114 * write delegations):
4116 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
4118 if (open->op_create == NFS4_OPEN_CREATE)
4124 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate);
4128 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
4130 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
4131 STATEID_VAL(&dp->dl_stid.sc_stateid));
4132 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
4133 nfs4_put_stid(&dp->dl_stid);
4136 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
4137 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
4138 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
4139 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
4140 open->op_recall = 1;
4143 /* 4.1 client asking for a delegation? */
4144 if (open->op_deleg_want)
4145 nfsd4_open_deleg_none_ext(open, status);
4149 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
4150 struct nfs4_delegation *dp)
4152 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
4153 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4154 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4155 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
4156 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
4157 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4158 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4159 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
4161 /* Otherwise the client must be confused wanting a delegation
4162 * it already has, therefore we don't return
4163 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
4168 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
4170 struct nfsd4_compoundres *resp = rqstp->rq_resp;
4171 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
4172 struct nfs4_file *fp = NULL;
4173 struct nfs4_ol_stateid *stp = NULL;
4174 struct nfs4_delegation *dp = NULL;
4178 * Lookup file; if found, lookup stateid and check open request,
4179 * and check for delegations in the process of being recalled.
4180 * If not found, create the nfs4_file struct
4182 fp = find_or_add_file(open->op_file, ¤t_fh->fh_handle);
4183 if (fp != open->op_file) {
4184 status = nfs4_check_deleg(cl, open, &dp);
4187 stp = nfsd4_find_existing_open(fp, open);
4189 open->op_file = NULL;
4190 status = nfserr_bad_stateid;
4191 if (nfsd4_is_deleg_cur(open))
4196 * OPEN the file, or upgrade an existing OPEN.
4197 * If truncate fails, the OPEN fails.
4200 /* Stateid was found, this is an OPEN upgrade */
4201 down_read(&stp->st_rwsem);
4202 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
4204 up_read(&stp->st_rwsem);
4209 open->op_stp = NULL;
4210 init_open_stateid(stp, fp, open);
4211 down_read(&stp->st_rwsem);
4212 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4214 up_read(&stp->st_rwsem);
4215 release_open_stateid(stp);
4219 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
4221 if (stp->st_clnt_odstate == open->op_odstate)
4222 open->op_odstate = NULL;
4224 update_stateid(&stp->st_stid.sc_stateid);
4225 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4226 up_read(&stp->st_rwsem);
4228 if (nfsd4_has_session(&resp->cstate)) {
4229 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4230 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4231 open->op_why_no_deleg = WND4_NOT_WANTED;
4237 * Attempt to hand out a delegation. No error return, because the
4238 * OPEN succeeds even if we fail.
4240 nfs4_open_delegation(current_fh, open, stp);
4244 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
4245 STATEID_VAL(&stp->st_stid.sc_stateid));
4247 /* 4.1 client trying to upgrade/downgrade delegation? */
4248 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
4249 open->op_deleg_want)
4250 nfsd4_deleg_xgrade_none_ext(open, dp);
4254 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
4255 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
4257 * To finish the open response, we just need to set the rflags.
4259 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
4260 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
4261 !nfsd4_has_session(&resp->cstate))
4262 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
4264 nfs4_put_stid(&dp->dl_stid);
4266 nfs4_put_stid(&stp->st_stid);
4271 void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
4272 struct nfsd4_open *open)
4274 if (open->op_openowner) {
4275 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
4277 nfsd4_cstate_assign_replay(cstate, so);
4278 nfs4_put_stateowner(so);
4281 kmem_cache_free(file_slab, open->op_file);
4283 nfs4_put_stid(&open->op_stp->st_stid);
4284 if (open->op_odstate)
4285 kmem_cache_free(odstate_slab, open->op_odstate);
4289 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4292 struct nfs4_client *clp;
4294 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4296 dprintk("process_renew(%08x/%08x): starting\n",
4297 clid->cl_boot, clid->cl_id);
4298 status = lookup_clientid(clid, cstate, nn);
4302 status = nfserr_cb_path_down;
4303 if (!list_empty(&clp->cl_delegations)
4304 && clp->cl_cb_state != NFSD4_CB_UP)
4312 nfsd4_end_grace(struct nfsd_net *nn)
4314 /* do nothing if grace period already ended */
4315 if (nn->grace_ended)
4318 dprintk("NFSD: end of grace period\n");
4319 nn->grace_ended = true;
4321 * If the server goes down again right now, an NFSv4
4322 * client will still be allowed to reclaim after it comes back up,
4323 * even if it hasn't yet had a chance to reclaim state this time.
4326 nfsd4_record_grace_done(nn);
4328 * At this point, NFSv4 clients can still reclaim. But if the
4329 * server crashes, any that have not yet reclaimed will be out
4330 * of luck on the next boot.
4332 * (NFSv4.1+ clients are considered to have reclaimed once they
4333 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
4334 * have reclaimed after their first OPEN.)
4336 locks_end_grace(&nn->nfsd4_manager);
4338 * At this point, and once lockd and/or any other containers
4339 * exit their grace period, further reclaims will fail and
4340 * regular locking can resume.
4345 nfs4_laundromat(struct nfsd_net *nn)
4347 struct nfs4_client *clp;
4348 struct nfs4_openowner *oo;
4349 struct nfs4_delegation *dp;
4350 struct nfs4_ol_stateid *stp;
4351 struct list_head *pos, *next, reaplist;
4352 time_t cutoff = get_seconds() - nn->nfsd4_lease;
4353 time_t t, new_timeo = nn->nfsd4_lease;
4355 dprintk("NFSD: laundromat service - starting\n");
4356 nfsd4_end_grace(nn);
4357 INIT_LIST_HEAD(&reaplist);
4358 spin_lock(&nn->client_lock);
4359 list_for_each_safe(pos, next, &nn->client_lru) {
4360 clp = list_entry(pos, struct nfs4_client, cl_lru);
4361 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4362 t = clp->cl_time - cutoff;
4363 new_timeo = min(new_timeo, t);
4366 if (mark_client_expired_locked(clp)) {
4367 dprintk("NFSD: client in use (clientid %08x)\n",
4368 clp->cl_clientid.cl_id);
4371 list_add(&clp->cl_lru, &reaplist);
4373 spin_unlock(&nn->client_lock);
4374 list_for_each_safe(pos, next, &reaplist) {
4375 clp = list_entry(pos, struct nfs4_client, cl_lru);
4376 dprintk("NFSD: purging unused client (clientid %08x)\n",
4377 clp->cl_clientid.cl_id);
4378 list_del_init(&clp->cl_lru);
4381 spin_lock(&state_lock);
4382 list_for_each_safe(pos, next, &nn->del_recall_lru) {
4383 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4384 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
4385 t = dp->dl_time - cutoff;
4386 new_timeo = min(new_timeo, t);
4389 WARN_ON(!unhash_delegation_locked(dp));
4390 list_add(&dp->dl_recall_lru, &reaplist);
4392 spin_unlock(&state_lock);
4393 while (!list_empty(&reaplist)) {
4394 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4396 list_del_init(&dp->dl_recall_lru);
4397 revoke_delegation(dp);
4400 spin_lock(&nn->client_lock);
4401 while (!list_empty(&nn->close_lru)) {
4402 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
4404 if (time_after((unsigned long)oo->oo_time,
4405 (unsigned long)cutoff)) {
4406 t = oo->oo_time - cutoff;
4407 new_timeo = min(new_timeo, t);
4410 list_del_init(&oo->oo_close_lru);
4411 stp = oo->oo_last_closed_stid;
4412 oo->oo_last_closed_stid = NULL;
4413 spin_unlock(&nn->client_lock);
4414 nfs4_put_stid(&stp->st_stid);
4415 spin_lock(&nn->client_lock);
4417 spin_unlock(&nn->client_lock);
4419 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
4423 static struct workqueue_struct *laundry_wq;
4424 static void laundromat_main(struct work_struct *);
4427 laundromat_main(struct work_struct *laundry)
4430 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
4432 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4435 t = nfs4_laundromat(nn);
4436 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
4437 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
4440 static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
4442 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
4443 return nfserr_bad_stateid;
4448 access_permit_read(struct nfs4_ol_stateid *stp)
4450 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4451 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4452 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
4456 access_permit_write(struct nfs4_ol_stateid *stp)
4458 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4459 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
4463 __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
4465 __be32 status = nfserr_openmode;
4467 /* For lock stateid's, we test the parent open, not the lock: */
4468 if (stp->st_openstp)
4469 stp = stp->st_openstp;
4470 if ((flags & WR_STATE) && !access_permit_write(stp))
4472 if ((flags & RD_STATE) && !access_permit_read(stp))
4479 static inline __be32
4480 check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
4482 if (ONE_STATEID(stateid) && (flags & RD_STATE))
4484 else if (opens_in_grace(net)) {
4485 /* Answer in remaining cases depends on existence of
4486 * conflicting state; so we must wait out the grace period. */
4487 return nfserr_grace;
4488 } else if (flags & WR_STATE)
4489 return nfs4_share_conflict(current_fh,
4490 NFS4_SHARE_DENY_WRITE);
4491 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4492 return nfs4_share_conflict(current_fh,
4493 NFS4_SHARE_DENY_READ);
4497 * Allow READ/WRITE during grace period on recovered state only for files
4498 * that are not able to provide mandatory locking.
4501 grace_disallows_io(struct net *net, struct inode *inode)
4503 return opens_in_grace(net) && mandatory_lock(inode);
4506 /* Returns true iff a is later than b: */
4507 static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4509 return (s32)(a->si_generation - b->si_generation) > 0;
4512 static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
4515 * When sessions are used the stateid generation number is ignored
4518 if (has_session && in->si_generation == 0)
4521 if (in->si_generation == ref->si_generation)
4524 /* If the client sends us a stateid from the future, it's buggy: */
4525 if (stateid_generation_after(in, ref))
4526 return nfserr_bad_stateid;
4528 * However, we could see a stateid from the past, even from a
4529 * non-buggy client. For example, if the client sends a lock
4530 * while some IO is outstanding, the lock may bump si_generation
4531 * while the IO is still in flight. The client could avoid that
4532 * situation by waiting for responses on all the IO requests,
4533 * but better performance may result in retrying IO that
4534 * receives an old_stateid error if requests are rarely
4535 * reordered in flight:
4537 return nfserr_old_stateid;
4540 static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
4542 if (ols->st_stateowner->so_is_open_owner &&
4543 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
4544 return nfserr_bad_stateid;
4548 static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
4550 struct nfs4_stid *s;
4551 __be32 status = nfserr_bad_stateid;
4553 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4555 /* Client debugging aid. */
4556 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4557 char addr_str[INET6_ADDRSTRLEN];
4558 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4560 pr_warn_ratelimited("NFSD: client %s testing state ID "
4561 "with incorrect client ID\n", addr_str);
4564 spin_lock(&cl->cl_lock);
4565 s = find_stateid_locked(cl, stateid);
4568 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
4571 switch (s->sc_type) {
4572 case NFS4_DELEG_STID:
4575 case NFS4_REVOKED_DELEG_STID:
4576 status = nfserr_deleg_revoked;
4578 case NFS4_OPEN_STID:
4579 case NFS4_LOCK_STID:
4580 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
4583 printk("unknown stateid type %x\n", s->sc_type);
4585 case NFS4_CLOSED_STID:
4586 case NFS4_CLOSED_DELEG_STID:
4587 status = nfserr_bad_stateid;
4590 spin_unlock(&cl->cl_lock);
4595 nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4596 stateid_t *stateid, unsigned char typemask,
4597 struct nfs4_stid **s, struct nfsd_net *nn)
4601 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4602 return nfserr_bad_stateid;
4603 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
4604 if (status == nfserr_stale_clientid) {
4605 if (cstate->session)
4606 return nfserr_bad_stateid;
4607 return nfserr_stale_stateid;
4611 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
4613 return nfserr_bad_stateid;
4617 static struct file *
4618 nfs4_find_file(struct nfs4_stid *s, int flags)
4623 switch (s->sc_type) {
4624 case NFS4_DELEG_STID:
4625 if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
4627 return get_file(s->sc_file->fi_deleg_file);
4628 case NFS4_OPEN_STID:
4629 case NFS4_LOCK_STID:
4630 if (flags & RD_STATE)
4631 return find_readable_file(s->sc_file);
4633 return find_writeable_file(s->sc_file);
4641 nfs4_check_olstateid(struct svc_fh *fhp, struct nfs4_ol_stateid *ols, int flags)
4645 status = nfsd4_check_openowner_confirmed(ols);
4648 return nfs4_check_openmode(ols, flags);
4652 nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
4653 struct file **filpp, bool *tmp_file, int flags)
4655 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
4659 file = nfs4_find_file(s, flags);
4661 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
4662 acc | NFSD_MAY_OWNER_OVERRIDE);
4670 status = nfsd_open(rqstp, fhp, S_IFREG, acc, filpp);
4682 * Checks for stateid operations
4685 nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
4686 struct nfsd4_compound_state *cstate, stateid_t *stateid,
4687 int flags, struct file **filpp, bool *tmp_file)
4689 struct svc_fh *fhp = &cstate->current_fh;
4690 struct inode *ino = d_inode(fhp->fh_dentry);
4691 struct net *net = SVC_NET(rqstp);
4692 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4693 struct nfs4_stid *s = NULL;
4701 if (grace_disallows_io(net, ino))
4702 return nfserr_grace;
4704 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
4705 status = check_special_stateids(net, fhp, stateid, flags);
4709 status = nfsd4_lookup_stateid(cstate, stateid,
4710 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
4714 status = check_stateid_generation(stateid, &s->sc_stateid,
4715 nfsd4_has_session(cstate));
4719 switch (s->sc_type) {
4720 case NFS4_DELEG_STID:
4721 status = nfs4_check_delegmode(delegstateid(s), flags);
4723 case NFS4_OPEN_STID:
4724 case NFS4_LOCK_STID:
4725 status = nfs4_check_olstateid(fhp, openlockstateid(s), flags);
4728 status = nfserr_bad_stateid;
4733 status = nfs4_check_fh(fhp, s);
4736 if (!status && filpp)
4737 status = nfs4_check_file(rqstp, fhp, s, filpp, tmp_file, flags);
4745 * Test if the stateid is valid
4748 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4749 struct nfsd4_test_stateid *test_stateid)
4751 struct nfsd4_test_stateid_id *stateid;
4752 struct nfs4_client *cl = cstate->session->se_client;
4754 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
4755 stateid->ts_id_status =
4756 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
4762 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4763 struct nfsd4_free_stateid *free_stateid)
4765 stateid_t *stateid = &free_stateid->fr_stateid;
4766 struct nfs4_stid *s;
4767 struct nfs4_delegation *dp;
4768 struct nfs4_ol_stateid *stp;
4769 struct nfs4_client *cl = cstate->session->se_client;
4770 __be32 ret = nfserr_bad_stateid;
4772 spin_lock(&cl->cl_lock);
4773 s = find_stateid_locked(cl, stateid);
4776 switch (s->sc_type) {
4777 case NFS4_DELEG_STID:
4778 ret = nfserr_locks_held;
4780 case NFS4_OPEN_STID:
4781 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4784 ret = nfserr_locks_held;
4786 case NFS4_LOCK_STID:
4787 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4790 stp = openlockstateid(s);
4791 ret = nfserr_locks_held;
4792 if (check_for_locks(stp->st_stid.sc_file,
4793 lockowner(stp->st_stateowner)))
4795 WARN_ON(!unhash_lock_stateid(stp));
4796 spin_unlock(&cl->cl_lock);
4800 case NFS4_REVOKED_DELEG_STID:
4801 dp = delegstateid(s);
4802 list_del_init(&dp->dl_recall_lru);
4803 spin_unlock(&cl->cl_lock);
4807 /* Default falls through and returns nfserr_bad_stateid */
4810 spin_unlock(&cl->cl_lock);
4818 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4819 RD_STATE : WR_STATE;
4822 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
4824 struct svc_fh *current_fh = &cstate->current_fh;
4825 struct nfs4_stateowner *sop = stp->st_stateowner;
4828 status = nfsd4_check_seqid(cstate, sop, seqid);
4831 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4832 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
4834 * "Closed" stateid's exist *only* to return
4835 * nfserr_replay_me from the previous step, and
4836 * revoked delegations are kept only for free_stateid.
4838 return nfserr_bad_stateid;
4839 down_write(&stp->st_rwsem);
4840 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4841 if (status == nfs_ok)
4842 status = nfs4_check_fh(current_fh, &stp->st_stid);
4843 if (status != nfs_ok)
4844 up_write(&stp->st_rwsem);
4849 * Checks for sequence id mutating operations.
4852 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4853 stateid_t *stateid, char typemask,
4854 struct nfs4_ol_stateid **stpp,
4855 struct nfsd_net *nn)
4858 struct nfs4_stid *s;
4859 struct nfs4_ol_stateid *stp = NULL;
4861 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4862 seqid, STATEID_VAL(stateid));
4865 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
4868 stp = openlockstateid(s);
4869 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
4871 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
4875 nfs4_put_stid(&stp->st_stid);
4879 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4880 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
4883 struct nfs4_openowner *oo;
4884 struct nfs4_ol_stateid *stp;
4886 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
4887 NFS4_OPEN_STID, &stp, nn);
4890 oo = openowner(stp->st_stateowner);
4891 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4892 up_write(&stp->st_rwsem);
4893 nfs4_put_stid(&stp->st_stid);
4894 return nfserr_bad_stateid;
4901 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4902 struct nfsd4_open_confirm *oc)
4905 struct nfs4_openowner *oo;
4906 struct nfs4_ol_stateid *stp;
4907 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4909 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4910 cstate->current_fh.fh_dentry);
4912 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
4916 status = nfs4_preprocess_seqid_op(cstate,
4917 oc->oc_seqid, &oc->oc_req_stateid,
4918 NFS4_OPEN_STID, &stp, nn);
4921 oo = openowner(stp->st_stateowner);
4922 status = nfserr_bad_stateid;
4923 if (oo->oo_flags & NFS4_OO_CONFIRMED) {
4924 up_write(&stp->st_rwsem);
4927 oo->oo_flags |= NFS4_OO_CONFIRMED;
4928 update_stateid(&stp->st_stid.sc_stateid);
4929 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4930 up_write(&stp->st_rwsem);
4931 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
4932 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
4934 nfsd4_client_record_create(oo->oo_owner.so_client);
4937 nfs4_put_stid(&stp->st_stid);
4939 nfsd4_bump_seqid(cstate, status);
4943 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
4945 if (!test_access(access, stp))
4947 nfs4_file_put_access(stp->st_stid.sc_file, access);
4948 clear_access(access, stp);
4951 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4953 switch (to_access) {
4954 case NFS4_SHARE_ACCESS_READ:
4955 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4956 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4958 case NFS4_SHARE_ACCESS_WRITE:
4959 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4960 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4962 case NFS4_SHARE_ACCESS_BOTH:
4970 nfsd4_open_downgrade(struct svc_rqst *rqstp,
4971 struct nfsd4_compound_state *cstate,
4972 struct nfsd4_open_downgrade *od)
4975 struct nfs4_ol_stateid *stp;
4976 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4978 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4979 cstate->current_fh.fh_dentry);
4981 /* We don't yet support WANT bits: */
4982 if (od->od_deleg_want)
4983 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4986 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
4987 &od->od_stateid, &stp, nn);
4990 status = nfserr_inval;
4991 if (!test_access(od->od_share_access, stp)) {
4992 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
4993 stp->st_access_bmap, od->od_share_access);
4996 if (!test_deny(od->od_share_deny, stp)) {
4997 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
4998 stp->st_deny_bmap, od->od_share_deny);
5001 nfs4_stateid_downgrade(stp, od->od_share_access);
5003 reset_union_bmap_deny(od->od_share_deny, stp);
5005 update_stateid(&stp->st_stid.sc_stateid);
5006 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
5009 up_write(&stp->st_rwsem);
5010 nfs4_put_stid(&stp->st_stid);
5012 nfsd4_bump_seqid(cstate, status);
5016 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
5018 struct nfs4_client *clp = s->st_stid.sc_client;
5020 LIST_HEAD(reaplist);
5022 s->st_stid.sc_type = NFS4_CLOSED_STID;
5023 spin_lock(&clp->cl_lock);
5024 unhashed = unhash_open_stateid(s, &reaplist);
5026 if (clp->cl_minorversion) {
5028 put_ol_stateid_locked(s, &reaplist);
5029 spin_unlock(&clp->cl_lock);
5030 free_ol_stateid_reaplist(&reaplist);
5032 spin_unlock(&clp->cl_lock);
5033 free_ol_stateid_reaplist(&reaplist);
5035 move_to_close_lru(s, clp->net);
5040 * nfs4_unlock_state() called after encode
5043 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5044 struct nfsd4_close *close)
5047 struct nfs4_ol_stateid *stp;
5048 struct net *net = SVC_NET(rqstp);
5049 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5051 dprintk("NFSD: nfsd4_close on file %pd\n",
5052 cstate->current_fh.fh_dentry);
5054 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
5056 NFS4_OPEN_STID|NFS4_CLOSED_STID,
5058 nfsd4_bump_seqid(cstate, status);
5061 update_stateid(&stp->st_stid.sc_stateid);
5062 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
5063 up_write(&stp->st_rwsem);
5065 nfsd4_close_open_stateid(stp);
5067 /* put reference from nfs4_preprocess_seqid_op */
5068 nfs4_put_stid(&stp->st_stid);
5074 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5075 struct nfsd4_delegreturn *dr)
5077 struct nfs4_delegation *dp;
5078 stateid_t *stateid = &dr->dr_stateid;
5079 struct nfs4_stid *s;
5081 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5083 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
5086 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
5089 dp = delegstateid(s);
5090 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
5094 destroy_delegation(dp);
5096 nfs4_put_stid(&dp->dl_stid);
5102 end_offset(u64 start, u64 len)
5107 return end >= start ? end: NFS4_MAX_UINT64;
5110 /* last octet in a range */
5112 last_byte_offset(u64 start, u64 len)
5118 return end > start ? end - 1: NFS4_MAX_UINT64;
5122 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
5123 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
5124 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
5125 * locking, this prevents us from being completely protocol-compliant. The
5126 * real solution to this problem is to start using unsigned file offsets in
5127 * the VFS, but this is a very deep change!
5130 nfs4_transform_lock_offset(struct file_lock *lock)
5132 if (lock->fl_start < 0)
5133 lock->fl_start = OFFSET_MAX;
5134 if (lock->fl_end < 0)
5135 lock->fl_end = OFFSET_MAX;
5139 nfsd4_fl_get_owner(fl_owner_t owner)
5141 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
5143 nfs4_get_stateowner(&lo->lo_owner);
5148 nfsd4_fl_put_owner(fl_owner_t owner)
5150 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
5153 nfs4_put_stateowner(&lo->lo_owner);
5156 static const struct lock_manager_operations nfsd_posix_mng_ops = {
5157 .lm_get_owner = nfsd4_fl_get_owner,
5158 .lm_put_owner = nfsd4_fl_put_owner,
5162 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
5164 struct nfs4_lockowner *lo;
5166 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
5167 lo = (struct nfs4_lockowner *) fl->fl_owner;
5168 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
5169 lo->lo_owner.so_owner.len, GFP_KERNEL);
5170 if (!deny->ld_owner.data)
5171 /* We just don't care that much */
5173 deny->ld_owner.len = lo->lo_owner.so_owner.len;
5174 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
5177 deny->ld_owner.len = 0;
5178 deny->ld_owner.data = NULL;
5179 deny->ld_clientid.cl_boot = 0;
5180 deny->ld_clientid.cl_id = 0;
5182 deny->ld_start = fl->fl_start;
5183 deny->ld_length = NFS4_MAX_UINT64;
5184 if (fl->fl_end != NFS4_MAX_UINT64)
5185 deny->ld_length = fl->fl_end - fl->fl_start + 1;
5186 deny->ld_type = NFS4_READ_LT;
5187 if (fl->fl_type != F_RDLCK)
5188 deny->ld_type = NFS4_WRITE_LT;
5191 static struct nfs4_lockowner *
5192 find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
5194 unsigned int strhashval = ownerstr_hashval(owner);
5195 struct nfs4_stateowner *so;
5197 lockdep_assert_held(&clp->cl_lock);
5199 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
5201 if (so->so_is_open_owner)
5203 if (same_owner_str(so, owner))
5204 return lockowner(nfs4_get_stateowner(so));
5209 static struct nfs4_lockowner *
5210 find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
5212 struct nfs4_lockowner *lo;
5214 spin_lock(&clp->cl_lock);
5215 lo = find_lockowner_str_locked(clp, owner);
5216 spin_unlock(&clp->cl_lock);
5220 static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
5222 unhash_lockowner_locked(lockowner(sop));
5225 static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
5227 struct nfs4_lockowner *lo = lockowner(sop);
5229 kmem_cache_free(lockowner_slab, lo);
5232 static const struct nfs4_stateowner_operations lockowner_ops = {
5233 .so_unhash = nfs4_unhash_lockowner,
5234 .so_free = nfs4_free_lockowner,
5238 * Alloc a lock owner structure.
5239 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
5242 * strhashval = ownerstr_hashval
5244 static struct nfs4_lockowner *
5245 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
5246 struct nfs4_ol_stateid *open_stp,
5247 struct nfsd4_lock *lock)
5249 struct nfs4_lockowner *lo, *ret;
5251 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
5254 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
5255 lo->lo_owner.so_is_open_owner = 0;
5256 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
5257 lo->lo_owner.so_ops = &lockowner_ops;
5258 spin_lock(&clp->cl_lock);
5259 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
5261 list_add(&lo->lo_owner.so_strhash,
5262 &clp->cl_ownerstr_hashtbl[strhashval]);
5265 nfs4_free_stateowner(&lo->lo_owner);
5267 spin_unlock(&clp->cl_lock);
5272 init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
5273 struct nfs4_file *fp, struct inode *inode,
5274 struct nfs4_ol_stateid *open_stp)
5276 struct nfs4_client *clp = lo->lo_owner.so_client;
5278 lockdep_assert_held(&clp->cl_lock);
5280 atomic_inc(&stp->st_stid.sc_count);
5281 stp->st_stid.sc_type = NFS4_LOCK_STID;
5282 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
5284 stp->st_stid.sc_file = fp;
5285 stp->st_stid.sc_free = nfs4_free_lock_stateid;
5286 stp->st_access_bmap = 0;
5287 stp->st_deny_bmap = open_stp->st_deny_bmap;
5288 stp->st_openstp = open_stp;
5289 init_rwsem(&stp->st_rwsem);
5290 list_add(&stp->st_locks, &open_stp->st_locks);
5291 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
5292 spin_lock(&fp->fi_lock);
5293 list_add(&stp->st_perfile, &fp->fi_stateids);
5294 spin_unlock(&fp->fi_lock);
5297 static struct nfs4_ol_stateid *
5298 find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5300 struct nfs4_ol_stateid *lst;
5301 struct nfs4_client *clp = lo->lo_owner.so_client;
5303 lockdep_assert_held(&clp->cl_lock);
5305 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
5306 if (lst->st_stid.sc_file == fp) {
5307 atomic_inc(&lst->st_stid.sc_count);
5314 static struct nfs4_ol_stateid *
5315 find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5316 struct inode *inode, struct nfs4_ol_stateid *ost,
5319 struct nfs4_stid *ns = NULL;
5320 struct nfs4_ol_stateid *lst;
5321 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5322 struct nfs4_client *clp = oo->oo_owner.so_client;
5324 spin_lock(&clp->cl_lock);
5325 lst = find_lock_stateid(lo, fi);
5327 spin_unlock(&clp->cl_lock);
5328 ns = nfs4_alloc_stid(clp, stateid_slab);
5332 spin_lock(&clp->cl_lock);
5333 lst = find_lock_stateid(lo, fi);
5335 lst = openlockstateid(ns);
5336 init_lock_stateid(lst, lo, fi, inode, ost);
5341 spin_unlock(&clp->cl_lock);
5348 check_lock_length(u64 offset, u64 length)
5350 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
5351 (length > ~offset)));
5354 static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
5356 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
5358 lockdep_assert_held(&fp->fi_lock);
5360 if (test_access(access, lock_stp))
5362 __nfs4_file_get_access(fp, access);
5363 set_access(access, lock_stp);
5367 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5368 struct nfs4_ol_stateid *ost,
5369 struct nfsd4_lock *lock,
5370 struct nfs4_ol_stateid **lst, bool *new)
5373 struct nfs4_file *fi = ost->st_stid.sc_file;
5374 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5375 struct nfs4_client *cl = oo->oo_owner.so_client;
5376 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
5377 struct nfs4_lockowner *lo;
5378 unsigned int strhashval;
5380 lo = find_lockowner_str(cl, &lock->lk_new_owner);
5382 strhashval = ownerstr_hashval(&lock->lk_new_owner);
5383 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5385 return nfserr_jukebox;
5387 /* with an existing lockowner, seqids must be the same */
5388 status = nfserr_bad_seqid;
5389 if (!cstate->minorversion &&
5390 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
5394 *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
5396 status = nfserr_jukebox;
5401 nfs4_put_stateowner(&lo->lo_owner);
5409 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5410 struct nfsd4_lock *lock)
5412 struct nfs4_openowner *open_sop = NULL;
5413 struct nfs4_lockowner *lock_sop = NULL;
5414 struct nfs4_ol_stateid *lock_stp = NULL;
5415 struct nfs4_ol_stateid *open_stp = NULL;
5416 struct nfs4_file *fp;
5417 struct file *filp = NULL;
5418 struct file_lock *file_lock = NULL;
5419 struct file_lock *conflock = NULL;
5424 struct net *net = SVC_NET(rqstp);
5425 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5427 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5428 (long long) lock->lk_offset,
5429 (long long) lock->lk_length);
5431 if (check_lock_length(lock->lk_offset, lock->lk_length))
5432 return nfserr_inval;
5434 if ((status = fh_verify(rqstp, &cstate->current_fh,
5435 S_IFREG, NFSD_MAY_LOCK))) {
5436 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5440 if (lock->lk_is_new) {
5441 if (nfsd4_has_session(cstate))
5442 /* See rfc 5661 18.10.3: given clientid is ignored: */
5443 memcpy(&lock->lk_new_clientid,
5444 &cstate->session->se_client->cl_clientid,
5445 sizeof(clientid_t));
5447 status = nfserr_stale_clientid;
5448 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
5451 /* validate and update open stateid and open seqid */
5452 status = nfs4_preprocess_confirmed_seqid_op(cstate,
5453 lock->lk_new_open_seqid,
5454 &lock->lk_new_open_stateid,
5458 up_write(&open_stp->st_rwsem);
5459 open_sop = openowner(open_stp->st_stateowner);
5460 status = nfserr_bad_stateid;
5461 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
5462 &lock->lk_new_clientid))
5464 status = lookup_or_create_lock_state(cstate, open_stp, lock,
5466 if (status == nfs_ok)
5467 down_write(&lock_stp->st_rwsem);
5469 status = nfs4_preprocess_seqid_op(cstate,
5470 lock->lk_old_lock_seqid,
5471 &lock->lk_old_lock_stateid,
5472 NFS4_LOCK_STID, &lock_stp, nn);
5476 lock_sop = lockowner(lock_stp->st_stateowner);
5478 lkflg = setlkflg(lock->lk_type);
5479 status = nfs4_check_openmode(lock_stp, lkflg);
5483 status = nfserr_grace;
5484 if (locks_in_grace(net) && !lock->lk_reclaim)
5486 status = nfserr_no_grace;
5487 if (!locks_in_grace(net) && lock->lk_reclaim)
5490 file_lock = locks_alloc_lock();
5492 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5493 status = nfserr_jukebox;
5497 fp = lock_stp->st_stid.sc_file;
5498 switch (lock->lk_type) {
5501 spin_lock(&fp->fi_lock);
5502 filp = find_readable_file_locked(fp);
5504 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
5505 spin_unlock(&fp->fi_lock);
5506 file_lock->fl_type = F_RDLCK;
5509 case NFS4_WRITEW_LT:
5510 spin_lock(&fp->fi_lock);
5511 filp = find_writeable_file_locked(fp);
5513 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
5514 spin_unlock(&fp->fi_lock);
5515 file_lock->fl_type = F_WRLCK;
5518 status = nfserr_inval;
5522 status = nfserr_openmode;
5526 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
5527 file_lock->fl_pid = current->tgid;
5528 file_lock->fl_file = filp;
5529 file_lock->fl_flags = FL_POSIX;
5530 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5531 file_lock->fl_start = lock->lk_offset;
5532 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5533 nfs4_transform_lock_offset(file_lock);
5535 conflock = locks_alloc_lock();
5537 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5538 status = nfserr_jukebox;
5542 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
5544 case 0: /* success! */
5545 update_stateid(&lock_stp->st_stid.sc_stateid);
5546 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
5550 case (EAGAIN): /* conflock holds conflicting lock */
5551 status = nfserr_denied;
5552 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
5553 nfs4_set_lock_denied(conflock, &lock->lk_denied);
5556 status = nfserr_deadlock;
5559 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
5560 status = nfserrno(err);
5567 /* Bump seqid manually if the 4.0 replay owner is openowner */
5568 if (cstate->replay_owner &&
5569 cstate->replay_owner != &lock_sop->lo_owner &&
5570 seqid_mutating_err(ntohl(status)))
5571 lock_sop->lo_owner.so_seqid++;
5573 up_write(&lock_stp->st_rwsem);
5576 * If this is a new, never-before-used stateid, and we are
5577 * returning an error, then just go ahead and release it.
5580 release_lock_stateid(lock_stp);
5582 nfs4_put_stid(&lock_stp->st_stid);
5585 nfs4_put_stid(&open_stp->st_stid);
5586 nfsd4_bump_seqid(cstate, status);
5588 locks_free_lock(file_lock);
5590 locks_free_lock(conflock);
5595 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5596 * so we do a temporary open here just to get an open file to pass to
5597 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5600 static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
5603 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5605 err = nfserrno(vfs_test_lock(file, lock));
5615 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5616 struct nfsd4_lockt *lockt)
5618 struct file_lock *file_lock = NULL;
5619 struct nfs4_lockowner *lo = NULL;
5621 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5623 if (locks_in_grace(SVC_NET(rqstp)))
5624 return nfserr_grace;
5626 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5627 return nfserr_inval;
5629 if (!nfsd4_has_session(cstate)) {
5630 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
5635 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
5638 file_lock = locks_alloc_lock();
5640 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5641 status = nfserr_jukebox;
5645 switch (lockt->lt_type) {
5648 file_lock->fl_type = F_RDLCK;
5651 case NFS4_WRITEW_LT:
5652 file_lock->fl_type = F_WRLCK;
5655 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
5656 status = nfserr_inval;
5660 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
5662 file_lock->fl_owner = (fl_owner_t)lo;
5663 file_lock->fl_pid = current->tgid;
5664 file_lock->fl_flags = FL_POSIX;
5666 file_lock->fl_start = lockt->lt_offset;
5667 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
5669 nfs4_transform_lock_offset(file_lock);
5671 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
5675 if (file_lock->fl_type != F_UNLCK) {
5676 status = nfserr_denied;
5677 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
5681 nfs4_put_stateowner(&lo->lo_owner);
5683 locks_free_lock(file_lock);
5688 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5689 struct nfsd4_locku *locku)
5691 struct nfs4_ol_stateid *stp;
5692 struct file *filp = NULL;
5693 struct file_lock *file_lock = NULL;
5696 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5698 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5699 (long long) locku->lu_offset,
5700 (long long) locku->lu_length);
5702 if (check_lock_length(locku->lu_offset, locku->lu_length))
5703 return nfserr_inval;
5705 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
5706 &locku->lu_stateid, NFS4_LOCK_STID,
5710 filp = find_any_file(stp->st_stid.sc_file);
5712 status = nfserr_lock_range;
5715 file_lock = locks_alloc_lock();
5717 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5718 status = nfserr_jukebox;
5722 file_lock->fl_type = F_UNLCK;
5723 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
5724 file_lock->fl_pid = current->tgid;
5725 file_lock->fl_file = filp;
5726 file_lock->fl_flags = FL_POSIX;
5727 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5728 file_lock->fl_start = locku->lu_offset;
5730 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5732 nfs4_transform_lock_offset(file_lock);
5734 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
5736 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
5739 update_stateid(&stp->st_stid.sc_stateid);
5740 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
5744 up_write(&stp->st_rwsem);
5745 nfs4_put_stid(&stp->st_stid);
5747 nfsd4_bump_seqid(cstate, status);
5749 locks_free_lock(file_lock);
5753 status = nfserrno(err);
5759 * true: locks held by lockowner
5760 * false: no locks held by lockowner
5763 check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
5765 struct file_lock *fl;
5767 struct file *filp = find_any_file(fp);
5768 struct inode *inode;
5769 struct file_lock_context *flctx;
5772 /* Any valid lock stateid should have some sort of access */
5777 inode = file_inode(filp);
5778 flctx = inode->i_flctx;
5780 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
5781 spin_lock(&flctx->flc_lock);
5782 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
5783 if (fl->fl_owner == (fl_owner_t)lowner) {
5788 spin_unlock(&flctx->flc_lock);
5795 nfsd4_release_lockowner(struct svc_rqst *rqstp,
5796 struct nfsd4_compound_state *cstate,
5797 struct nfsd4_release_lockowner *rlockowner)
5799 clientid_t *clid = &rlockowner->rl_clientid;
5800 struct nfs4_stateowner *sop;
5801 struct nfs4_lockowner *lo = NULL;
5802 struct nfs4_ol_stateid *stp;
5803 struct xdr_netobj *owner = &rlockowner->rl_owner;
5804 unsigned int hashval = ownerstr_hashval(owner);
5806 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5807 struct nfs4_client *clp;
5809 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5810 clid->cl_boot, clid->cl_id);
5812 status = lookup_clientid(clid, cstate, nn);
5817 /* Find the matching lock stateowner */
5818 spin_lock(&clp->cl_lock);
5819 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
5822 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
5825 /* see if there are still any locks associated with it */
5826 lo = lockowner(sop);
5827 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5828 if (check_for_locks(stp->st_stid.sc_file, lo)) {
5829 status = nfserr_locks_held;
5830 spin_unlock(&clp->cl_lock);
5835 nfs4_get_stateowner(sop);
5838 spin_unlock(&clp->cl_lock);
5840 release_lockowner(lo);
5844 static inline struct nfs4_client_reclaim *
5847 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
5851 nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
5853 struct nfs4_client_reclaim *crp;
5855 crp = nfsd4_find_reclaim_client(name, nn);
5856 return (crp && crp->cr_clp);
5860 * failure => all reset bets are off, nfserr_no_grace...
5862 struct nfs4_client_reclaim *
5863 nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
5865 unsigned int strhashval;
5866 struct nfs4_client_reclaim *crp;
5868 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5869 crp = alloc_reclaim();
5871 strhashval = clientstr_hashval(name);
5872 INIT_LIST_HEAD(&crp->cr_strhash);
5873 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
5874 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
5876 nn->reclaim_str_hashtbl_size++;
5882 nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
5884 list_del(&crp->cr_strhash);
5886 nn->reclaim_str_hashtbl_size--;
5890 nfs4_release_reclaim(struct nfsd_net *nn)
5892 struct nfs4_client_reclaim *crp = NULL;
5895 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5896 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5897 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
5898 struct nfs4_client_reclaim, cr_strhash);
5899 nfs4_remove_reclaim_record(crp, nn);
5902 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
5906 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
5907 struct nfs4_client_reclaim *
5908 nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
5910 unsigned int strhashval;
5911 struct nfs4_client_reclaim *crp = NULL;
5913 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
5915 strhashval = clientstr_hashval(recdir);
5916 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
5917 if (same_name(crp->cr_recdir, recdir)) {
5925 * Called from OPEN. Look for clientid in reclaim list.
5928 nfs4_check_open_reclaim(clientid_t *clid,
5929 struct nfsd4_compound_state *cstate,
5930 struct nfsd_net *nn)
5934 /* find clientid in conf_id_hashtbl */
5935 status = lookup_clientid(clid, cstate, nn);
5937 return nfserr_reclaim_bad;
5939 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
5940 return nfserr_no_grace;
5942 if (nfsd4_client_record_check(cstate->clp))
5943 return nfserr_reclaim_bad;
5948 #ifdef CONFIG_NFSD_FAULT_INJECTION
5950 put_client(struct nfs4_client *clp)
5952 atomic_dec(&clp->cl_refcount);
5955 static struct nfs4_client *
5956 nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5958 struct nfs4_client *clp;
5959 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5962 if (!nfsd_netns_ready(nn))
5965 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5966 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5973 nfsd_inject_print_clients(void)
5975 struct nfs4_client *clp;
5977 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5979 char buf[INET6_ADDRSTRLEN];
5981 if (!nfsd_netns_ready(nn))
5984 spin_lock(&nn->client_lock);
5985 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5986 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5987 pr_info("NFS Client: %s\n", buf);
5990 spin_unlock(&nn->client_lock);
5996 nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
5999 struct nfs4_client *clp;
6000 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6003 if (!nfsd_netns_ready(nn))
6006 spin_lock(&nn->client_lock);
6007 clp = nfsd_find_client(addr, addr_size);
6009 if (mark_client_expired_locked(clp) == nfs_ok)
6014 spin_unlock(&nn->client_lock);
6023 nfsd_inject_forget_clients(u64 max)
6026 struct nfs4_client *clp, *next;
6027 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6029 LIST_HEAD(reaplist);
6031 if (!nfsd_netns_ready(nn))
6034 spin_lock(&nn->client_lock);
6035 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6036 if (mark_client_expired_locked(clp) == nfs_ok) {
6037 list_add(&clp->cl_lru, &reaplist);
6038 if (max != 0 && ++count >= max)
6042 spin_unlock(&nn->client_lock);
6044 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
6050 static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
6053 char buf[INET6_ADDRSTRLEN];
6054 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
6055 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
6059 nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
6060 struct list_head *collect)
6062 struct nfs4_client *clp = lst->st_stid.sc_client;
6063 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6069 lockdep_assert_held(&nn->client_lock);
6070 atomic_inc(&clp->cl_refcount);
6071 list_add(&lst->st_locks, collect);
6074 static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
6075 struct list_head *collect,
6076 bool (*func)(struct nfs4_ol_stateid *))
6078 struct nfs4_openowner *oop;
6079 struct nfs4_ol_stateid *stp, *st_next;
6080 struct nfs4_ol_stateid *lst, *lst_next;
6083 spin_lock(&clp->cl_lock);
6084 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
6085 list_for_each_entry_safe(stp, st_next,
6086 &oop->oo_owner.so_stateids, st_perstateowner) {
6087 list_for_each_entry_safe(lst, lst_next,
6088 &stp->st_locks, st_locks) {
6091 nfsd_inject_add_lock_to_list(lst,
6096 * Despite the fact that these functions deal
6097 * with 64-bit integers for "count", we must
6098 * ensure that it doesn't blow up the
6099 * clp->cl_refcount. Throw a warning if we
6100 * start to approach INT_MAX here.
6102 WARN_ON_ONCE(count == (INT_MAX / 2));
6109 spin_unlock(&clp->cl_lock);
6115 nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
6118 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
6122 nfsd_print_client_locks(struct nfs4_client *clp)
6124 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
6125 nfsd_print_count(clp, count, "locked files");
6130 nfsd_inject_print_locks(void)
6132 struct nfs4_client *clp;
6134 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6137 if (!nfsd_netns_ready(nn))
6140 spin_lock(&nn->client_lock);
6141 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6142 count += nfsd_print_client_locks(clp);
6143 spin_unlock(&nn->client_lock);
6149 nfsd_reap_locks(struct list_head *reaplist)
6151 struct nfs4_client *clp;
6152 struct nfs4_ol_stateid *stp, *next;
6154 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
6155 list_del_init(&stp->st_locks);
6156 clp = stp->st_stid.sc_client;
6157 nfs4_put_stid(&stp->st_stid);
6163 nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
6165 unsigned int count = 0;
6166 struct nfs4_client *clp;
6167 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6169 LIST_HEAD(reaplist);
6171 if (!nfsd_netns_ready(nn))
6174 spin_lock(&nn->client_lock);
6175 clp = nfsd_find_client(addr, addr_size);
6177 count = nfsd_collect_client_locks(clp, &reaplist, 0);
6178 spin_unlock(&nn->client_lock);
6179 nfsd_reap_locks(&reaplist);
6184 nfsd_inject_forget_locks(u64 max)
6187 struct nfs4_client *clp;
6188 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6190 LIST_HEAD(reaplist);
6192 if (!nfsd_netns_ready(nn))
6195 spin_lock(&nn->client_lock);
6196 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6197 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
6198 if (max != 0 && count >= max)
6201 spin_unlock(&nn->client_lock);
6202 nfsd_reap_locks(&reaplist);
6207 nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
6208 struct list_head *collect,
6209 void (*func)(struct nfs4_openowner *))
6211 struct nfs4_openowner *oop, *next;
6212 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6216 lockdep_assert_held(&nn->client_lock);
6218 spin_lock(&clp->cl_lock);
6219 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
6223 atomic_inc(&clp->cl_refcount);
6224 list_add(&oop->oo_perclient, collect);
6229 * Despite the fact that these functions deal with
6230 * 64-bit integers for "count", we must ensure that
6231 * it doesn't blow up the clp->cl_refcount. Throw a
6232 * warning if we start to approach INT_MAX here.
6234 WARN_ON_ONCE(count == (INT_MAX / 2));
6238 spin_unlock(&clp->cl_lock);
6244 nfsd_print_client_openowners(struct nfs4_client *clp)
6246 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
6248 nfsd_print_count(clp, count, "openowners");
6253 nfsd_collect_client_openowners(struct nfs4_client *clp,
6254 struct list_head *collect, u64 max)
6256 return nfsd_foreach_client_openowner(clp, max, collect,
6257 unhash_openowner_locked);
6261 nfsd_inject_print_openowners(void)
6263 struct nfs4_client *clp;
6265 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6268 if (!nfsd_netns_ready(nn))
6271 spin_lock(&nn->client_lock);
6272 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6273 count += nfsd_print_client_openowners(clp);
6274 spin_unlock(&nn->client_lock);
6280 nfsd_reap_openowners(struct list_head *reaplist)
6282 struct nfs4_client *clp;
6283 struct nfs4_openowner *oop, *next;
6285 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
6286 list_del_init(&oop->oo_perclient);
6287 clp = oop->oo_owner.so_client;
6288 release_openowner(oop);
6294 nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
6297 unsigned int count = 0;
6298 struct nfs4_client *clp;
6299 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6301 LIST_HEAD(reaplist);
6303 if (!nfsd_netns_ready(nn))
6306 spin_lock(&nn->client_lock);
6307 clp = nfsd_find_client(addr, addr_size);
6309 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
6310 spin_unlock(&nn->client_lock);
6311 nfsd_reap_openowners(&reaplist);
6316 nfsd_inject_forget_openowners(u64 max)
6319 struct nfs4_client *clp;
6320 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6322 LIST_HEAD(reaplist);
6324 if (!nfsd_netns_ready(nn))
6327 spin_lock(&nn->client_lock);
6328 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6329 count += nfsd_collect_client_openowners(clp, &reaplist,
6331 if (max != 0 && count >= max)
6334 spin_unlock(&nn->client_lock);
6335 nfsd_reap_openowners(&reaplist);
6339 static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
6340 struct list_head *victims)
6342 struct nfs4_delegation *dp, *next;
6343 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6347 lockdep_assert_held(&nn->client_lock);
6349 spin_lock(&state_lock);
6350 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
6353 * It's not safe to mess with delegations that have a
6354 * non-zero dl_time. They might have already been broken
6355 * and could be processed by the laundromat outside of
6356 * the state_lock. Just leave them be.
6358 if (dp->dl_time != 0)
6361 atomic_inc(&clp->cl_refcount);
6362 WARN_ON(!unhash_delegation_locked(dp));
6363 list_add(&dp->dl_recall_lru, victims);
6367 * Despite the fact that these functions deal with
6368 * 64-bit integers for "count", we must ensure that
6369 * it doesn't blow up the clp->cl_refcount. Throw a
6370 * warning if we start to approach INT_MAX here.
6372 WARN_ON_ONCE(count == (INT_MAX / 2));
6376 spin_unlock(&state_lock);
6381 nfsd_print_client_delegations(struct nfs4_client *clp)
6383 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
6385 nfsd_print_count(clp, count, "delegations");
6390 nfsd_inject_print_delegations(void)
6392 struct nfs4_client *clp;
6394 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6397 if (!nfsd_netns_ready(nn))
6400 spin_lock(&nn->client_lock);
6401 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6402 count += nfsd_print_client_delegations(clp);
6403 spin_unlock(&nn->client_lock);
6409 nfsd_forget_delegations(struct list_head *reaplist)
6411 struct nfs4_client *clp;
6412 struct nfs4_delegation *dp, *next;
6414 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6415 list_del_init(&dp->dl_recall_lru);
6416 clp = dp->dl_stid.sc_client;
6417 revoke_delegation(dp);
6423 nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
6427 struct nfs4_client *clp;
6428 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6430 LIST_HEAD(reaplist);
6432 if (!nfsd_netns_ready(nn))
6435 spin_lock(&nn->client_lock);
6436 clp = nfsd_find_client(addr, addr_size);
6438 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6439 spin_unlock(&nn->client_lock);
6441 nfsd_forget_delegations(&reaplist);
6446 nfsd_inject_forget_delegations(u64 max)
6449 struct nfs4_client *clp;
6450 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6452 LIST_HEAD(reaplist);
6454 if (!nfsd_netns_ready(nn))
6457 spin_lock(&nn->client_lock);
6458 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6459 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6460 if (max != 0 && count >= max)
6463 spin_unlock(&nn->client_lock);
6464 nfsd_forget_delegations(&reaplist);
6469 nfsd_recall_delegations(struct list_head *reaplist)
6471 struct nfs4_client *clp;
6472 struct nfs4_delegation *dp, *next;
6474 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6475 list_del_init(&dp->dl_recall_lru);
6476 clp = dp->dl_stid.sc_client;
6478 * We skipped all entries that had a zero dl_time before,
6479 * so we can now reset the dl_time back to 0. If a delegation
6480 * break comes in now, then it won't make any difference since
6481 * we're recalling it either way.
6483 spin_lock(&state_lock);
6485 spin_unlock(&state_lock);
6486 nfsd_break_one_deleg(dp);
6492 nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
6496 struct nfs4_client *clp;
6497 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6499 LIST_HEAD(reaplist);
6501 if (!nfsd_netns_ready(nn))
6504 spin_lock(&nn->client_lock);
6505 clp = nfsd_find_client(addr, addr_size);
6507 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6508 spin_unlock(&nn->client_lock);
6510 nfsd_recall_delegations(&reaplist);
6515 nfsd_inject_recall_delegations(u64 max)
6518 struct nfs4_client *clp, *next;
6519 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6521 LIST_HEAD(reaplist);
6523 if (!nfsd_netns_ready(nn))
6526 spin_lock(&nn->client_lock);
6527 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6528 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6529 if (max != 0 && ++count >= max)
6532 spin_unlock(&nn->client_lock);
6533 nfsd_recall_delegations(&reaplist);
6536 #endif /* CONFIG_NFSD_FAULT_INJECTION */
6539 * Since the lifetime of a delegation isn't limited to that of an open, a
6540 * client may quite reasonably hang on to a delegation as long as it has
6541 * the inode cached. This becomes an obvious problem the first time a
6542 * client's inode cache approaches the size of the server's total memory.
6544 * For now we avoid this problem by imposing a hard limit on the number
6545 * of delegations, which varies according to the server's memory size.
6548 set_max_delegations(void)
6551 * Allow at most 4 delegations per megabyte of RAM. Quick
6552 * estimates suggest that in the worst case (where every delegation
6553 * is for a different inode), a delegation could take about 1.5K,
6554 * giving a worst case usage of about 6% of memory.
6556 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
6559 static int nfs4_state_create_net(struct net *net)
6561 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6564 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6565 CLIENT_HASH_SIZE, GFP_KERNEL);
6566 if (!nn->conf_id_hashtbl)
6568 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6569 CLIENT_HASH_SIZE, GFP_KERNEL);
6570 if (!nn->unconf_id_hashtbl)
6572 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
6573 SESSION_HASH_SIZE, GFP_KERNEL);
6574 if (!nn->sessionid_hashtbl)
6577 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6578 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
6579 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
6581 for (i = 0; i < SESSION_HASH_SIZE; i++)
6582 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
6583 nn->conf_name_tree = RB_ROOT;
6584 nn->unconf_name_tree = RB_ROOT;
6585 INIT_LIST_HEAD(&nn->client_lru);
6586 INIT_LIST_HEAD(&nn->close_lru);
6587 INIT_LIST_HEAD(&nn->del_recall_lru);
6588 spin_lock_init(&nn->client_lock);
6590 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
6596 kfree(nn->unconf_id_hashtbl);
6598 kfree(nn->conf_id_hashtbl);
6604 nfs4_state_destroy_net(struct net *net)
6607 struct nfs4_client *clp = NULL;
6608 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6610 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6611 while (!list_empty(&nn->conf_id_hashtbl[i])) {
6612 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6613 destroy_client(clp);
6617 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6618 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
6619 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6620 destroy_client(clp);
6624 kfree(nn->sessionid_hashtbl);
6625 kfree(nn->unconf_id_hashtbl);
6626 kfree(nn->conf_id_hashtbl);
6631 nfs4_state_start_net(struct net *net)
6633 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6636 ret = nfs4_state_create_net(net);
6639 nn->boot_time = get_seconds();
6640 nn->grace_ended = false;
6641 nn->nfsd4_manager.block_opens = true;
6642 locks_start_grace(net, &nn->nfsd4_manager);
6643 nfsd4_client_tracking_init(net);
6644 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
6645 nn->nfsd4_grace, net);
6646 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
6650 /* initialization to perform when the nfsd service is started: */
6653 nfs4_state_start(void)
6657 ret = set_callback_cred();
6660 laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
6661 if (laundry_wq == NULL) {
6665 ret = nfsd4_create_callback_queue();
6667 goto out_free_laundry;
6669 set_max_delegations();
6674 destroy_workqueue(laundry_wq);
6680 nfs4_state_shutdown_net(struct net *net)
6682 struct nfs4_delegation *dp = NULL;
6683 struct list_head *pos, *next, reaplist;
6684 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6686 cancel_delayed_work_sync(&nn->laundromat_work);
6687 locks_end_grace(&nn->nfsd4_manager);
6689 INIT_LIST_HEAD(&reaplist);
6690 spin_lock(&state_lock);
6691 list_for_each_safe(pos, next, &nn->del_recall_lru) {
6692 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
6693 WARN_ON(!unhash_delegation_locked(dp));
6694 list_add(&dp->dl_recall_lru, &reaplist);
6696 spin_unlock(&state_lock);
6697 list_for_each_safe(pos, next, &reaplist) {
6698 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
6699 list_del_init(&dp->dl_recall_lru);
6700 put_clnt_odstate(dp->dl_clnt_odstate);
6701 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
6702 nfs4_put_stid(&dp->dl_stid);
6705 nfsd4_client_tracking_exit(net);
6706 nfs4_state_destroy_net(net);
6710 nfs4_state_shutdown(void)
6712 destroy_workqueue(laundry_wq);
6713 nfsd4_destroy_callback_queue();
6717 get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6719 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
6720 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
6724 put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6726 if (cstate->minorversion) {
6727 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
6728 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6733 clear_current_stateid(struct nfsd4_compound_state *cstate)
6735 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6739 * functions to set current state id
6742 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6744 put_stateid(cstate, &odp->od_stateid);
6748 nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
6750 put_stateid(cstate, &open->op_stateid);
6754 nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6756 put_stateid(cstate, &close->cl_stateid);
6760 nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
6762 put_stateid(cstate, &lock->lk_resp_stateid);
6766 * functions to consume current state id
6770 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6772 get_stateid(cstate, &odp->od_stateid);
6776 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
6778 get_stateid(cstate, &drp->dr_stateid);
6782 nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
6784 get_stateid(cstate, &fsp->fr_stateid);
6788 nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
6790 get_stateid(cstate, &setattr->sa_stateid);
6794 nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6796 get_stateid(cstate, &close->cl_stateid);
6800 nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
6802 get_stateid(cstate, &locku->lu_stateid);
6806 nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
6808 get_stateid(cstate, &read->rd_stateid);
6812 nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
6814 get_stateid(cstate, &write->wr_stateid);