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 const struct nfsd4_callback_ops nfsd4_cb_recall_ops;
102 static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops;
104 static bool is_session_dead(struct nfsd4_session *ses)
106 return ses->se_flags & NFS4_SESSION_DEAD;
109 static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
111 if (atomic_read(&ses->se_ref) > ref_held_by_me)
112 return nfserr_jukebox;
113 ses->se_flags |= NFS4_SESSION_DEAD;
117 static bool is_client_expired(struct nfs4_client *clp)
119 return clp->cl_time == 0;
122 static __be32 get_client_locked(struct nfs4_client *clp)
124 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
126 lockdep_assert_held(&nn->client_lock);
128 if (is_client_expired(clp))
129 return nfserr_expired;
130 atomic_inc(&clp->cl_refcount);
134 /* must be called under the client_lock */
136 renew_client_locked(struct nfs4_client *clp)
138 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
140 if (is_client_expired(clp)) {
142 printk("%s: client (clientid %08x/%08x) already expired\n",
144 clp->cl_clientid.cl_boot,
145 clp->cl_clientid.cl_id);
149 dprintk("renewing client (clientid %08x/%08x)\n",
150 clp->cl_clientid.cl_boot,
151 clp->cl_clientid.cl_id);
152 list_move_tail(&clp->cl_lru, &nn->client_lru);
153 clp->cl_time = get_seconds();
156 static void put_client_renew_locked(struct nfs4_client *clp)
158 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
160 lockdep_assert_held(&nn->client_lock);
162 if (!atomic_dec_and_test(&clp->cl_refcount))
164 if (!is_client_expired(clp))
165 renew_client_locked(clp);
168 static void put_client_renew(struct nfs4_client *clp)
170 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
172 if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
174 if (!is_client_expired(clp))
175 renew_client_locked(clp);
176 spin_unlock(&nn->client_lock);
179 static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
183 if (is_session_dead(ses))
184 return nfserr_badsession;
185 status = get_client_locked(ses->se_client);
188 atomic_inc(&ses->se_ref);
192 static void nfsd4_put_session_locked(struct nfsd4_session *ses)
194 struct nfs4_client *clp = ses->se_client;
195 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
197 lockdep_assert_held(&nn->client_lock);
199 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
201 put_client_renew_locked(clp);
204 static void nfsd4_put_session(struct nfsd4_session *ses)
206 struct nfs4_client *clp = ses->se_client;
207 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
209 spin_lock(&nn->client_lock);
210 nfsd4_put_session_locked(ses);
211 spin_unlock(&nn->client_lock);
214 static struct nfsd4_blocked_lock *
215 find_blocked_lock(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
218 struct nfsd4_blocked_lock *cur, *found = NULL;
220 spin_lock(&nn->client_lock);
221 list_for_each_entry(cur, &lo->lo_blocked, nbl_list) {
222 if (fh_match(fh, &cur->nbl_fh)) {
223 list_del_init(&cur->nbl_list);
228 spin_unlock(&nn->client_lock);
230 posix_unblock_lock(&found->nbl_lock);
234 static struct nfsd4_blocked_lock *
235 find_or_allocate_block(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
238 struct nfsd4_blocked_lock *nbl;
240 nbl = find_blocked_lock(lo, fh, nn);
242 nbl= kmalloc(sizeof(*nbl), GFP_KERNEL);
244 fh_copy_shallow(&nbl->nbl_fh, fh);
245 locks_init_lock(&nbl->nbl_lock);
246 nfsd4_init_cb(&nbl->nbl_cb, lo->lo_owner.so_client,
247 &nfsd4_cb_notify_lock_ops,
248 NFSPROC4_CLNT_CB_NOTIFY_LOCK);
255 free_blocked_lock(struct nfsd4_blocked_lock *nbl)
257 locks_release_private(&nbl->nbl_lock);
262 nfsd4_cb_notify_lock_done(struct nfsd4_callback *cb, struct rpc_task *task)
265 * Since this is just an optimization, we don't try very hard if it
266 * turns out not to succeed. We'll requeue it on NFS4ERR_DELAY, and
267 * just quit trying on anything else.
269 switch (task->tk_status) {
271 rpc_delay(task, 1 * HZ);
279 nfsd4_cb_notify_lock_release(struct nfsd4_callback *cb)
281 struct nfsd4_blocked_lock *nbl = container_of(cb,
282 struct nfsd4_blocked_lock, nbl_cb);
284 free_blocked_lock(nbl);
287 static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops = {
288 .done = nfsd4_cb_notify_lock_done,
289 .release = nfsd4_cb_notify_lock_release,
292 static inline struct nfs4_stateowner *
293 nfs4_get_stateowner(struct nfs4_stateowner *sop)
295 atomic_inc(&sop->so_count);
300 same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
302 return (sop->so_owner.len == owner->len) &&
303 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
306 static struct nfs4_openowner *
307 find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
308 struct nfs4_client *clp)
310 struct nfs4_stateowner *so;
312 lockdep_assert_held(&clp->cl_lock);
314 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
316 if (!so->so_is_open_owner)
318 if (same_owner_str(so, &open->op_owner))
319 return openowner(nfs4_get_stateowner(so));
324 static struct nfs4_openowner *
325 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
326 struct nfs4_client *clp)
328 struct nfs4_openowner *oo;
330 spin_lock(&clp->cl_lock);
331 oo = find_openstateowner_str_locked(hashval, open, clp);
332 spin_unlock(&clp->cl_lock);
337 opaque_hashval(const void *ptr, int nbytes)
339 unsigned char *cptr = (unsigned char *) ptr;
349 static void nfsd4_free_file_rcu(struct rcu_head *rcu)
351 struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
353 kmem_cache_free(file_slab, fp);
357 put_nfs4_file(struct nfs4_file *fi)
359 might_lock(&state_lock);
361 if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) {
362 hlist_del_rcu(&fi->fi_hash);
363 spin_unlock(&state_lock);
364 WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
365 WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
366 call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
371 __nfs4_get_fd(struct nfs4_file *f, int oflag)
373 if (f->fi_fds[oflag])
374 return get_file(f->fi_fds[oflag]);
379 find_writeable_file_locked(struct nfs4_file *f)
383 lockdep_assert_held(&f->fi_lock);
385 ret = __nfs4_get_fd(f, O_WRONLY);
387 ret = __nfs4_get_fd(f, O_RDWR);
392 find_writeable_file(struct nfs4_file *f)
396 spin_lock(&f->fi_lock);
397 ret = find_writeable_file_locked(f);
398 spin_unlock(&f->fi_lock);
403 static struct file *find_readable_file_locked(struct nfs4_file *f)
407 lockdep_assert_held(&f->fi_lock);
409 ret = __nfs4_get_fd(f, O_RDONLY);
411 ret = __nfs4_get_fd(f, O_RDWR);
416 find_readable_file(struct nfs4_file *f)
420 spin_lock(&f->fi_lock);
421 ret = find_readable_file_locked(f);
422 spin_unlock(&f->fi_lock);
428 find_any_file(struct nfs4_file *f)
432 spin_lock(&f->fi_lock);
433 ret = __nfs4_get_fd(f, O_RDWR);
435 ret = __nfs4_get_fd(f, O_WRONLY);
437 ret = __nfs4_get_fd(f, O_RDONLY);
439 spin_unlock(&f->fi_lock);
443 static atomic_long_t num_delegations;
444 unsigned long max_delegations;
447 * Open owner state (share locks)
450 /* hash tables for lock and open owners */
451 #define OWNER_HASH_BITS 8
452 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
453 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
455 static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
459 ret = opaque_hashval(ownername->data, ownername->len);
460 return ret & OWNER_HASH_MASK;
463 /* hash table for nfs4_file */
464 #define FILE_HASH_BITS 8
465 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
467 static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
469 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
472 static unsigned int file_hashval(struct knfsd_fh *fh)
474 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
477 static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
480 __nfs4_file_get_access(struct nfs4_file *fp, u32 access)
482 lockdep_assert_held(&fp->fi_lock);
484 if (access & NFS4_SHARE_ACCESS_WRITE)
485 atomic_inc(&fp->fi_access[O_WRONLY]);
486 if (access & NFS4_SHARE_ACCESS_READ)
487 atomic_inc(&fp->fi_access[O_RDONLY]);
491 nfs4_file_get_access(struct nfs4_file *fp, u32 access)
493 lockdep_assert_held(&fp->fi_lock);
495 /* Does this access mode make sense? */
496 if (access & ~NFS4_SHARE_ACCESS_BOTH)
499 /* Does it conflict with a deny mode already set? */
500 if ((access & fp->fi_share_deny) != 0)
501 return nfserr_share_denied;
503 __nfs4_file_get_access(fp, access);
507 static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
509 /* Common case is that there is no deny mode. */
511 /* Does this deny mode make sense? */
512 if (deny & ~NFS4_SHARE_DENY_BOTH)
515 if ((deny & NFS4_SHARE_DENY_READ) &&
516 atomic_read(&fp->fi_access[O_RDONLY]))
517 return nfserr_share_denied;
519 if ((deny & NFS4_SHARE_DENY_WRITE) &&
520 atomic_read(&fp->fi_access[O_WRONLY]))
521 return nfserr_share_denied;
526 static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
528 might_lock(&fp->fi_lock);
530 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
531 struct file *f1 = NULL;
532 struct file *f2 = NULL;
534 swap(f1, fp->fi_fds[oflag]);
535 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
536 swap(f2, fp->fi_fds[O_RDWR]);
537 spin_unlock(&fp->fi_lock);
545 static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
547 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
549 if (access & NFS4_SHARE_ACCESS_WRITE)
550 __nfs4_file_put_access(fp, O_WRONLY);
551 if (access & NFS4_SHARE_ACCESS_READ)
552 __nfs4_file_put_access(fp, O_RDONLY);
556 * Allocate a new open/delegation state counter. This is needed for
557 * pNFS for proper return on close semantics.
559 * Note that we only allocate it for pNFS-enabled exports, otherwise
560 * all pointers to struct nfs4_clnt_odstate are always NULL.
562 static struct nfs4_clnt_odstate *
563 alloc_clnt_odstate(struct nfs4_client *clp)
565 struct nfs4_clnt_odstate *co;
567 co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
570 atomic_set(&co->co_odcount, 1);
576 hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
578 struct nfs4_file *fp = co->co_file;
580 lockdep_assert_held(&fp->fi_lock);
581 list_add(&co->co_perfile, &fp->fi_clnt_odstate);
585 get_clnt_odstate(struct nfs4_clnt_odstate *co)
588 atomic_inc(&co->co_odcount);
592 put_clnt_odstate(struct nfs4_clnt_odstate *co)
594 struct nfs4_file *fp;
600 if (atomic_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
601 list_del(&co->co_perfile);
602 spin_unlock(&fp->fi_lock);
604 nfsd4_return_all_file_layouts(co->co_client, fp);
605 kmem_cache_free(odstate_slab, co);
609 static struct nfs4_clnt_odstate *
610 find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
612 struct nfs4_clnt_odstate *co;
613 struct nfs4_client *cl;
620 spin_lock(&fp->fi_lock);
621 list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
622 if (co->co_client == cl) {
623 get_clnt_odstate(co);
629 hash_clnt_odstate_locked(new);
631 spin_unlock(&fp->fi_lock);
635 struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl,
636 struct kmem_cache *slab)
638 struct nfs4_stid *stid;
641 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
645 idr_preload(GFP_KERNEL);
646 spin_lock(&cl->cl_lock);
647 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 0, 0, GFP_NOWAIT);
648 spin_unlock(&cl->cl_lock);
652 stid->sc_client = cl;
653 stid->sc_stateid.si_opaque.so_id = new_id;
654 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
655 /* Will be incremented before return to client: */
656 atomic_set(&stid->sc_count, 1);
657 spin_lock_init(&stid->sc_lock);
660 * It shouldn't be a problem to reuse an opaque stateid value.
661 * I don't think it is for 4.1. But with 4.0 I worry that, for
662 * example, a stray write retransmission could be accepted by
663 * the server when it should have been rejected. Therefore,
664 * adopt a trick from the sctp code to attempt to maximize the
665 * amount of time until an id is reused, by ensuring they always
666 * "increase" (mod INT_MAX):
670 kmem_cache_free(slab, stid);
674 static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
676 struct nfs4_stid *stid;
677 struct nfs4_ol_stateid *stp;
679 stid = nfs4_alloc_stid(clp, stateid_slab);
683 stp = openlockstateid(stid);
684 stp->st_stid.sc_free = nfs4_free_ol_stateid;
688 static void nfs4_free_deleg(struct nfs4_stid *stid)
690 kmem_cache_free(deleg_slab, stid);
691 atomic_long_dec(&num_delegations);
695 * When we recall a delegation, we should be careful not to hand it
696 * out again straight away.
697 * To ensure this we keep a pair of bloom filters ('new' and 'old')
698 * in which the filehandles of recalled delegations are "stored".
699 * If a filehandle appear in either filter, a delegation is blocked.
700 * When a delegation is recalled, the filehandle is stored in the "new"
702 * Every 30 seconds we swap the filters and clear the "new" one,
703 * unless both are empty of course.
705 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
706 * low 3 bytes as hash-table indices.
708 * 'blocked_delegations_lock', which is always taken in block_delegations(),
709 * is used to manage concurrent access. Testing does not need the lock
710 * except when swapping the two filters.
712 static DEFINE_SPINLOCK(blocked_delegations_lock);
713 static struct bloom_pair {
714 int entries, old_entries;
716 int new; /* index into 'set' */
717 DECLARE_BITMAP(set[2], 256);
718 } blocked_delegations;
720 static int delegation_blocked(struct knfsd_fh *fh)
723 struct bloom_pair *bd = &blocked_delegations;
725 if (bd->entries == 0)
727 if (seconds_since_boot() - bd->swap_time > 30) {
728 spin_lock(&blocked_delegations_lock);
729 if (seconds_since_boot() - bd->swap_time > 30) {
730 bd->entries -= bd->old_entries;
731 bd->old_entries = bd->entries;
732 memset(bd->set[bd->new], 0,
735 bd->swap_time = seconds_since_boot();
737 spin_unlock(&blocked_delegations_lock);
739 hash = jhash(&fh->fh_base, fh->fh_size, 0);
740 if (test_bit(hash&255, bd->set[0]) &&
741 test_bit((hash>>8)&255, bd->set[0]) &&
742 test_bit((hash>>16)&255, bd->set[0]))
745 if (test_bit(hash&255, bd->set[1]) &&
746 test_bit((hash>>8)&255, bd->set[1]) &&
747 test_bit((hash>>16)&255, bd->set[1]))
753 static void block_delegations(struct knfsd_fh *fh)
756 struct bloom_pair *bd = &blocked_delegations;
758 hash = jhash(&fh->fh_base, fh->fh_size, 0);
760 spin_lock(&blocked_delegations_lock);
761 __set_bit(hash&255, bd->set[bd->new]);
762 __set_bit((hash>>8)&255, bd->set[bd->new]);
763 __set_bit((hash>>16)&255, bd->set[bd->new]);
764 if (bd->entries == 0)
765 bd->swap_time = seconds_since_boot();
767 spin_unlock(&blocked_delegations_lock);
770 static struct nfs4_delegation *
771 alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh,
772 struct nfs4_clnt_odstate *odstate)
774 struct nfs4_delegation *dp;
777 dprintk("NFSD alloc_init_deleg\n");
778 n = atomic_long_inc_return(&num_delegations);
779 if (n < 0 || n > max_delegations)
781 if (delegation_blocked(¤t_fh->fh_handle))
783 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
787 dp->dl_stid.sc_free = nfs4_free_deleg;
789 * delegation seqid's are never incremented. The 4.1 special
790 * meaning of seqid 0 isn't meaningful, really, but let's avoid
791 * 0 anyway just for consistency and use 1:
793 dp->dl_stid.sc_stateid.si_generation = 1;
794 INIT_LIST_HEAD(&dp->dl_perfile);
795 INIT_LIST_HEAD(&dp->dl_perclnt);
796 INIT_LIST_HEAD(&dp->dl_recall_lru);
797 dp->dl_clnt_odstate = odstate;
798 get_clnt_odstate(odstate);
799 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
801 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
802 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
805 atomic_long_dec(&num_delegations);
810 nfs4_put_stid(struct nfs4_stid *s)
812 struct nfs4_file *fp = s->sc_file;
813 struct nfs4_client *clp = s->sc_client;
815 might_lock(&clp->cl_lock);
817 if (!atomic_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
818 wake_up_all(&close_wq);
821 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
822 spin_unlock(&clp->cl_lock);
829 nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
831 stateid_t *src = &stid->sc_stateid;
833 spin_lock(&stid->sc_lock);
834 if (unlikely(++src->si_generation == 0))
835 src->si_generation = 1;
836 memcpy(dst, src, sizeof(*dst));
837 spin_unlock(&stid->sc_lock);
840 static void nfs4_put_deleg_lease(struct nfs4_file *fp)
842 struct file *filp = NULL;
844 spin_lock(&fp->fi_lock);
845 if (fp->fi_deleg_file && --fp->fi_delegees == 0)
846 swap(filp, fp->fi_deleg_file);
847 spin_unlock(&fp->fi_lock);
850 vfs_setlease(filp, F_UNLCK, NULL, (void **)&fp);
855 void nfs4_unhash_stid(struct nfs4_stid *s)
861 * nfs4_get_existing_delegation - Discover if this delegation already exists
862 * @clp: a pointer to the nfs4_client we're granting a delegation to
863 * @fp: a pointer to the nfs4_file we're granting a delegation on
866 * On success: NULL if an existing delegation was not found.
868 * On error: -EAGAIN if one was previously granted to this nfs4_client
869 * for this nfs4_file.
874 nfs4_get_existing_delegation(struct nfs4_client *clp, struct nfs4_file *fp)
876 struct nfs4_delegation *searchdp = NULL;
877 struct nfs4_client *searchclp = NULL;
879 lockdep_assert_held(&state_lock);
880 lockdep_assert_held(&fp->fi_lock);
882 list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
883 searchclp = searchdp->dl_stid.sc_client;
884 if (clp == searchclp) {
892 * hash_delegation_locked - Add a delegation to the appropriate lists
893 * @dp: a pointer to the nfs4_delegation we are adding.
894 * @fp: a pointer to the nfs4_file we're granting a delegation on
897 * On success: NULL if the delegation was successfully hashed.
899 * On error: -EAGAIN if one was previously granted to this
900 * nfs4_client for this nfs4_file. Delegation is not hashed.
905 hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
908 struct nfs4_client *clp = dp->dl_stid.sc_client;
910 lockdep_assert_held(&state_lock);
911 lockdep_assert_held(&fp->fi_lock);
913 status = nfs4_get_existing_delegation(clp, fp);
917 atomic_inc(&dp->dl_stid.sc_count);
918 dp->dl_stid.sc_type = NFS4_DELEG_STID;
919 list_add(&dp->dl_perfile, &fp->fi_delegations);
920 list_add(&dp->dl_perclnt, &clp->cl_delegations);
925 unhash_delegation_locked(struct nfs4_delegation *dp)
927 struct nfs4_file *fp = dp->dl_stid.sc_file;
929 lockdep_assert_held(&state_lock);
931 if (list_empty(&dp->dl_perfile))
934 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
935 /* Ensure that deleg break won't try to requeue it */
937 spin_lock(&fp->fi_lock);
938 list_del_init(&dp->dl_perclnt);
939 list_del_init(&dp->dl_recall_lru);
940 list_del_init(&dp->dl_perfile);
941 spin_unlock(&fp->fi_lock);
945 static void destroy_delegation(struct nfs4_delegation *dp)
949 spin_lock(&state_lock);
950 unhashed = unhash_delegation_locked(dp);
951 spin_unlock(&state_lock);
953 put_clnt_odstate(dp->dl_clnt_odstate);
954 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
955 nfs4_put_stid(&dp->dl_stid);
959 static void revoke_delegation(struct nfs4_delegation *dp)
961 struct nfs4_client *clp = dp->dl_stid.sc_client;
963 WARN_ON(!list_empty(&dp->dl_recall_lru));
965 put_clnt_odstate(dp->dl_clnt_odstate);
966 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
968 if (clp->cl_minorversion == 0)
969 nfs4_put_stid(&dp->dl_stid);
971 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
972 spin_lock(&clp->cl_lock);
973 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
974 spin_unlock(&clp->cl_lock);
982 static unsigned int clientid_hashval(u32 id)
984 return id & CLIENT_HASH_MASK;
987 static unsigned int clientstr_hashval(const char *name)
989 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
993 * We store the NONE, READ, WRITE, and BOTH bits separately in the
994 * st_{access,deny}_bmap field of the stateid, in order to track not
995 * only what share bits are currently in force, but also what
996 * combinations of share bits previous opens have used. This allows us
997 * to enforce the recommendation of rfc 3530 14.2.19 that the server
998 * return an error if the client attempt to downgrade to a combination
999 * of share bits not explicable by closing some of its previous opens.
1001 * XXX: This enforcement is actually incomplete, since we don't keep
1002 * track of access/deny bit combinations; so, e.g., we allow:
1004 * OPEN allow read, deny write
1005 * OPEN allow both, deny none
1006 * DOWNGRADE allow read, deny none
1008 * which we should reject.
1011 bmap_to_share_mode(unsigned long bmap) {
1013 unsigned int access = 0;
1015 for (i = 1; i < 4; i++) {
1016 if (test_bit(i, &bmap))
1022 /* set share access for a given stateid */
1024 set_access(u32 access, struct nfs4_ol_stateid *stp)
1026 unsigned char mask = 1 << access;
1028 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1029 stp->st_access_bmap |= mask;
1032 /* clear share access for a given stateid */
1034 clear_access(u32 access, struct nfs4_ol_stateid *stp)
1036 unsigned char mask = 1 << access;
1038 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1039 stp->st_access_bmap &= ~mask;
1042 /* test whether a given stateid has access */
1044 test_access(u32 access, struct nfs4_ol_stateid *stp)
1046 unsigned char mask = 1 << access;
1048 return (bool)(stp->st_access_bmap & mask);
1051 /* set share deny for a given stateid */
1053 set_deny(u32 deny, struct nfs4_ol_stateid *stp)
1055 unsigned char mask = 1 << deny;
1057 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1058 stp->st_deny_bmap |= mask;
1061 /* clear share deny for a given stateid */
1063 clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
1065 unsigned char mask = 1 << deny;
1067 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1068 stp->st_deny_bmap &= ~mask;
1071 /* test whether a given stateid is denying specific access */
1073 test_deny(u32 deny, struct nfs4_ol_stateid *stp)
1075 unsigned char mask = 1 << deny;
1077 return (bool)(stp->st_deny_bmap & mask);
1080 static int nfs4_access_to_omode(u32 access)
1082 switch (access & NFS4_SHARE_ACCESS_BOTH) {
1083 case NFS4_SHARE_ACCESS_READ:
1085 case NFS4_SHARE_ACCESS_WRITE:
1087 case NFS4_SHARE_ACCESS_BOTH:
1095 * A stateid that had a deny mode associated with it is being released
1096 * or downgraded. Recalculate the deny mode on the file.
1099 recalculate_deny_mode(struct nfs4_file *fp)
1101 struct nfs4_ol_stateid *stp;
1103 spin_lock(&fp->fi_lock);
1104 fp->fi_share_deny = 0;
1105 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
1106 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
1107 spin_unlock(&fp->fi_lock);
1111 reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
1114 bool change = false;
1116 for (i = 1; i < 4; i++) {
1117 if ((i & deny) != i) {
1123 /* Recalculate per-file deny mode if there was a change */
1125 recalculate_deny_mode(stp->st_stid.sc_file);
1128 /* release all access and file references for a given stateid */
1130 release_all_access(struct nfs4_ol_stateid *stp)
1133 struct nfs4_file *fp = stp->st_stid.sc_file;
1135 if (fp && stp->st_deny_bmap != 0)
1136 recalculate_deny_mode(fp);
1138 for (i = 1; i < 4; i++) {
1139 if (test_access(i, stp))
1140 nfs4_file_put_access(stp->st_stid.sc_file, i);
1141 clear_access(i, stp);
1145 static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1147 kfree(sop->so_owner.data);
1148 sop->so_ops->so_free(sop);
1151 static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1153 struct nfs4_client *clp = sop->so_client;
1155 might_lock(&clp->cl_lock);
1157 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
1159 sop->so_ops->so_unhash(sop);
1160 spin_unlock(&clp->cl_lock);
1161 nfs4_free_stateowner(sop);
1164 static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
1166 struct nfs4_file *fp = stp->st_stid.sc_file;
1168 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1170 if (list_empty(&stp->st_perfile))
1173 spin_lock(&fp->fi_lock);
1174 list_del_init(&stp->st_perfile);
1175 spin_unlock(&fp->fi_lock);
1176 list_del(&stp->st_perstateowner);
1180 static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
1182 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1184 put_clnt_odstate(stp->st_clnt_odstate);
1185 release_all_access(stp);
1186 if (stp->st_stateowner)
1187 nfs4_put_stateowner(stp->st_stateowner);
1188 kmem_cache_free(stateid_slab, stid);
1191 static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
1193 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1194 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
1197 file = find_any_file(stp->st_stid.sc_file);
1199 filp_close(file, (fl_owner_t)lo);
1200 nfs4_free_ol_stateid(stid);
1204 * Put the persistent reference to an already unhashed generic stateid, while
1205 * holding the cl_lock. If it's the last reference, then put it onto the
1206 * reaplist for later destruction.
1208 static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1209 struct list_head *reaplist)
1211 struct nfs4_stid *s = &stp->st_stid;
1212 struct nfs4_client *clp = s->sc_client;
1214 lockdep_assert_held(&clp->cl_lock);
1216 WARN_ON_ONCE(!list_empty(&stp->st_locks));
1218 if (!atomic_dec_and_test(&s->sc_count)) {
1219 wake_up_all(&close_wq);
1223 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1224 list_add(&stp->st_locks, reaplist);
1227 static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
1229 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
1231 lockdep_assert_held(&oo->oo_owner.so_client->cl_lock);
1233 list_del_init(&stp->st_locks);
1234 nfs4_unhash_stid(&stp->st_stid);
1235 return unhash_ol_stateid(stp);
1238 static void release_lock_stateid(struct nfs4_ol_stateid *stp)
1240 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
1243 spin_lock(&oo->oo_owner.so_client->cl_lock);
1244 unhashed = unhash_lock_stateid(stp);
1245 spin_unlock(&oo->oo_owner.so_client->cl_lock);
1247 nfs4_put_stid(&stp->st_stid);
1250 static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
1252 struct nfs4_client *clp = lo->lo_owner.so_client;
1254 lockdep_assert_held(&clp->cl_lock);
1256 list_del_init(&lo->lo_owner.so_strhash);
1260 * Free a list of generic stateids that were collected earlier after being
1264 free_ol_stateid_reaplist(struct list_head *reaplist)
1266 struct nfs4_ol_stateid *stp;
1267 struct nfs4_file *fp;
1271 while (!list_empty(reaplist)) {
1272 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1274 list_del(&stp->st_locks);
1275 fp = stp->st_stid.sc_file;
1276 stp->st_stid.sc_free(&stp->st_stid);
1282 static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1283 struct list_head *reaplist)
1285 struct nfs4_ol_stateid *stp;
1287 lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1289 while (!list_empty(&open_stp->st_locks)) {
1290 stp = list_entry(open_stp->st_locks.next,
1291 struct nfs4_ol_stateid, st_locks);
1292 WARN_ON(!unhash_lock_stateid(stp));
1293 put_ol_stateid_locked(stp, reaplist);
1297 static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
1298 struct list_head *reaplist)
1302 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1304 unhashed = unhash_ol_stateid(stp);
1305 release_open_stateid_locks(stp, reaplist);
1309 static void release_open_stateid(struct nfs4_ol_stateid *stp)
1311 LIST_HEAD(reaplist);
1313 spin_lock(&stp->st_stid.sc_client->cl_lock);
1314 if (unhash_open_stateid(stp, &reaplist))
1315 put_ol_stateid_locked(stp, &reaplist);
1316 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1317 free_ol_stateid_reaplist(&reaplist);
1320 static void unhash_openowner_locked(struct nfs4_openowner *oo)
1322 struct nfs4_client *clp = oo->oo_owner.so_client;
1324 lockdep_assert_held(&clp->cl_lock);
1326 list_del_init(&oo->oo_owner.so_strhash);
1327 list_del_init(&oo->oo_perclient);
1330 static void release_last_closed_stateid(struct nfs4_openowner *oo)
1332 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1334 struct nfs4_ol_stateid *s;
1336 spin_lock(&nn->client_lock);
1337 s = oo->oo_last_closed_stid;
1339 list_del_init(&oo->oo_close_lru);
1340 oo->oo_last_closed_stid = NULL;
1342 spin_unlock(&nn->client_lock);
1344 nfs4_put_stid(&s->st_stid);
1347 static void release_openowner(struct nfs4_openowner *oo)
1349 struct nfs4_ol_stateid *stp;
1350 struct nfs4_client *clp = oo->oo_owner.so_client;
1351 struct list_head reaplist;
1353 INIT_LIST_HEAD(&reaplist);
1355 spin_lock(&clp->cl_lock);
1356 unhash_openowner_locked(oo);
1357 while (!list_empty(&oo->oo_owner.so_stateids)) {
1358 stp = list_first_entry(&oo->oo_owner.so_stateids,
1359 struct nfs4_ol_stateid, st_perstateowner);
1360 if (unhash_open_stateid(stp, &reaplist))
1361 put_ol_stateid_locked(stp, &reaplist);
1363 spin_unlock(&clp->cl_lock);
1364 free_ol_stateid_reaplist(&reaplist);
1365 release_last_closed_stateid(oo);
1366 nfs4_put_stateowner(&oo->oo_owner);
1370 hash_sessionid(struct nfs4_sessionid *sessionid)
1372 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1374 return sid->sequence % SESSION_HASH_SIZE;
1377 #ifdef CONFIG_SUNRPC_DEBUG
1379 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1381 u32 *ptr = (u32 *)(&sessionid->data[0]);
1382 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1386 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1392 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1393 * won't be used for replay.
1395 void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1397 struct nfs4_stateowner *so = cstate->replay_owner;
1399 if (nfserr == nfserr_replay_me)
1402 if (!seqid_mutating_err(ntohl(nfserr))) {
1403 nfsd4_cstate_clear_replay(cstate);
1408 if (so->so_is_open_owner)
1409 release_last_closed_stateid(openowner(so));
1415 gen_sessionid(struct nfsd4_session *ses)
1417 struct nfs4_client *clp = ses->se_client;
1418 struct nfsd4_sessionid *sid;
1420 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1421 sid->clientid = clp->cl_clientid;
1422 sid->sequence = current_sessionid++;
1427 * The protocol defines ca_maxresponssize_cached to include the size of
1428 * the rpc header, but all we need to cache is the data starting after
1429 * the end of the initial SEQUENCE operation--the rest we regenerate
1430 * each time. Therefore we can advertise a ca_maxresponssize_cached
1431 * value that is the number of bytes in our cache plus a few additional
1432 * bytes. In order to stay on the safe side, and not promise more than
1433 * we can cache, those additional bytes must be the minimum possible: 24
1434 * bytes of rpc header (xid through accept state, with AUTH_NULL
1435 * verifier), 12 for the compound header (with zero-length tag), and 44
1436 * for the SEQUENCE op response:
1438 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1441 free_session_slots(struct nfsd4_session *ses)
1445 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1446 kfree(ses->se_slots[i]);
1450 * We don't actually need to cache the rpc and session headers, so we
1451 * can allocate a little less for each slot:
1453 static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
1457 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1460 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1461 return size + sizeof(struct nfsd4_slot);
1465 * XXX: If we run out of reserved DRC memory we could (up to a point)
1466 * re-negotiate active sessions and reduce their slot usage to make
1467 * room for new connections. For now we just fail the create session.
1469 static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
1471 u32 slotsize = slot_bytes(ca);
1472 u32 num = ca->maxreqs;
1475 spin_lock(&nfsd_drc_lock);
1476 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1477 nfsd_drc_max_mem - nfsd_drc_mem_used);
1478 num = min_t(int, num, avail / slotsize);
1479 nfsd_drc_mem_used += num * slotsize;
1480 spin_unlock(&nfsd_drc_lock);
1485 static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
1487 int slotsize = slot_bytes(ca);
1489 spin_lock(&nfsd_drc_lock);
1490 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
1491 spin_unlock(&nfsd_drc_lock);
1494 static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1495 struct nfsd4_channel_attrs *battrs)
1497 int numslots = fattrs->maxreqs;
1498 int slotsize = slot_bytes(fattrs);
1499 struct nfsd4_session *new;
1502 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
1503 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1504 mem = numslots * sizeof(struct nfsd4_slot *);
1506 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
1509 /* allocate each struct nfsd4_slot and data cache in one piece */
1510 for (i = 0; i < numslots; i++) {
1511 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
1512 if (!new->se_slots[i])
1516 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1517 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1522 kfree(new->se_slots[i]);
1527 static void free_conn(struct nfsd4_conn *c)
1529 svc_xprt_put(c->cn_xprt);
1533 static void nfsd4_conn_lost(struct svc_xpt_user *u)
1535 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1536 struct nfs4_client *clp = c->cn_session->se_client;
1538 spin_lock(&clp->cl_lock);
1539 if (!list_empty(&c->cn_persession)) {
1540 list_del(&c->cn_persession);
1543 nfsd4_probe_callback(clp);
1544 spin_unlock(&clp->cl_lock);
1547 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
1549 struct nfsd4_conn *conn;
1551 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1554 svc_xprt_get(rqstp->rq_xprt);
1555 conn->cn_xprt = rqstp->rq_xprt;
1556 conn->cn_flags = flags;
1557 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1561 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1563 conn->cn_session = ses;
1564 list_add(&conn->cn_persession, &ses->se_conns);
1567 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1569 struct nfs4_client *clp = ses->se_client;
1571 spin_lock(&clp->cl_lock);
1572 __nfsd4_hash_conn(conn, ses);
1573 spin_unlock(&clp->cl_lock);
1576 static int nfsd4_register_conn(struct nfsd4_conn *conn)
1578 conn->cn_xpt_user.callback = nfsd4_conn_lost;
1579 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
1582 static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
1586 nfsd4_hash_conn(conn, ses);
1587 ret = nfsd4_register_conn(conn);
1589 /* oops; xprt is already down: */
1590 nfsd4_conn_lost(&conn->cn_xpt_user);
1591 /* We may have gained or lost a callback channel: */
1592 nfsd4_probe_callback_sync(ses->se_client);
1595 static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
1597 u32 dir = NFS4_CDFC4_FORE;
1599 if (cses->flags & SESSION4_BACK_CHAN)
1600 dir |= NFS4_CDFC4_BACK;
1601 return alloc_conn(rqstp, dir);
1604 /* must be called under client_lock */
1605 static void nfsd4_del_conns(struct nfsd4_session *s)
1607 struct nfs4_client *clp = s->se_client;
1608 struct nfsd4_conn *c;
1610 spin_lock(&clp->cl_lock);
1611 while (!list_empty(&s->se_conns)) {
1612 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1613 list_del_init(&c->cn_persession);
1614 spin_unlock(&clp->cl_lock);
1616 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1619 spin_lock(&clp->cl_lock);
1621 spin_unlock(&clp->cl_lock);
1624 static void __free_session(struct nfsd4_session *ses)
1626 free_session_slots(ses);
1630 static void free_session(struct nfsd4_session *ses)
1632 nfsd4_del_conns(ses);
1633 nfsd4_put_drc_mem(&ses->se_fchannel);
1634 __free_session(ses);
1637 static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
1640 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1642 new->se_client = clp;
1645 INIT_LIST_HEAD(&new->se_conns);
1647 new->se_cb_seq_nr = 1;
1648 new->se_flags = cses->flags;
1649 new->se_cb_prog = cses->callback_prog;
1650 new->se_cb_sec = cses->cb_sec;
1651 atomic_set(&new->se_ref, 0);
1652 idx = hash_sessionid(&new->se_sessionid);
1653 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
1654 spin_lock(&clp->cl_lock);
1655 list_add(&new->se_perclnt, &clp->cl_sessions);
1656 spin_unlock(&clp->cl_lock);
1659 struct sockaddr *sa = svc_addr(rqstp);
1661 * This is a little silly; with sessions there's no real
1662 * use for the callback address. Use the peer address
1663 * as a reasonable default for now, but consider fixing
1664 * the rpc client not to require an address in the
1667 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1668 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
1672 /* caller must hold client_lock */
1673 static struct nfsd4_session *
1674 __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
1676 struct nfsd4_session *elem;
1678 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1680 lockdep_assert_held(&nn->client_lock);
1682 dump_sessionid(__func__, sessionid);
1683 idx = hash_sessionid(sessionid);
1684 /* Search in the appropriate list */
1685 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
1686 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1687 NFS4_MAX_SESSIONID_LEN)) {
1692 dprintk("%s: session not found\n", __func__);
1696 static struct nfsd4_session *
1697 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1700 struct nfsd4_session *session;
1701 __be32 status = nfserr_badsession;
1703 session = __find_in_sessionid_hashtbl(sessionid, net);
1706 status = nfsd4_get_session_locked(session);
1714 /* caller must hold client_lock */
1716 unhash_session(struct nfsd4_session *ses)
1718 struct nfs4_client *clp = ses->se_client;
1719 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1721 lockdep_assert_held(&nn->client_lock);
1723 list_del(&ses->se_hash);
1724 spin_lock(&ses->se_client->cl_lock);
1725 list_del(&ses->se_perclnt);
1726 spin_unlock(&ses->se_client->cl_lock);
1729 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1731 STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
1734 * We're assuming the clid was not given out from a boot
1735 * precisely 2^32 (about 136 years) before this one. That seems
1736 * a safe assumption:
1738 if (clid->cl_boot == (u32)nn->boot_time)
1740 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
1741 clid->cl_boot, clid->cl_id, nn->boot_time);
1746 * XXX Should we use a slab cache ?
1747 * This type of memory management is somewhat inefficient, but we use it
1748 * anyway since SETCLIENTID is not a common operation.
1750 static struct nfs4_client *alloc_client(struct xdr_netobj name)
1752 struct nfs4_client *clp;
1755 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1758 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
1759 if (clp->cl_name.data == NULL)
1761 clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
1762 OWNER_HASH_SIZE, GFP_KERNEL);
1763 if (!clp->cl_ownerstr_hashtbl)
1764 goto err_no_hashtbl;
1765 for (i = 0; i < OWNER_HASH_SIZE; i++)
1766 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
1767 clp->cl_name.len = name.len;
1768 INIT_LIST_HEAD(&clp->cl_sessions);
1769 idr_init(&clp->cl_stateids);
1770 atomic_set(&clp->cl_refcount, 0);
1771 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1772 INIT_LIST_HEAD(&clp->cl_idhash);
1773 INIT_LIST_HEAD(&clp->cl_openowners);
1774 INIT_LIST_HEAD(&clp->cl_delegations);
1775 INIT_LIST_HEAD(&clp->cl_lru);
1776 INIT_LIST_HEAD(&clp->cl_revoked);
1777 #ifdef CONFIG_NFSD_PNFS
1778 INIT_LIST_HEAD(&clp->cl_lo_states);
1780 spin_lock_init(&clp->cl_lock);
1781 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1784 kfree(clp->cl_name.data);
1791 free_client(struct nfs4_client *clp)
1793 while (!list_empty(&clp->cl_sessions)) {
1794 struct nfsd4_session *ses;
1795 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1797 list_del(&ses->se_perclnt);
1798 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1801 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
1802 free_svc_cred(&clp->cl_cred);
1803 kfree(clp->cl_ownerstr_hashtbl);
1804 kfree(clp->cl_name.data);
1805 idr_destroy(&clp->cl_stateids);
1809 /* must be called under the client_lock */
1811 unhash_client_locked(struct nfs4_client *clp)
1813 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1814 struct nfsd4_session *ses;
1816 lockdep_assert_held(&nn->client_lock);
1818 /* Mark the client as expired! */
1820 /* Make it invisible */
1821 if (!list_empty(&clp->cl_idhash)) {
1822 list_del_init(&clp->cl_idhash);
1823 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1824 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1826 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1828 list_del_init(&clp->cl_lru);
1829 spin_lock(&clp->cl_lock);
1830 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1831 list_del_init(&ses->se_hash);
1832 spin_unlock(&clp->cl_lock);
1836 unhash_client(struct nfs4_client *clp)
1838 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1840 spin_lock(&nn->client_lock);
1841 unhash_client_locked(clp);
1842 spin_unlock(&nn->client_lock);
1845 static __be32 mark_client_expired_locked(struct nfs4_client *clp)
1847 if (atomic_read(&clp->cl_refcount))
1848 return nfserr_jukebox;
1849 unhash_client_locked(clp);
1854 __destroy_client(struct nfs4_client *clp)
1856 struct nfs4_openowner *oo;
1857 struct nfs4_delegation *dp;
1858 struct list_head reaplist;
1860 INIT_LIST_HEAD(&reaplist);
1861 spin_lock(&state_lock);
1862 while (!list_empty(&clp->cl_delegations)) {
1863 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
1864 WARN_ON(!unhash_delegation_locked(dp));
1865 list_add(&dp->dl_recall_lru, &reaplist);
1867 spin_unlock(&state_lock);
1868 while (!list_empty(&reaplist)) {
1869 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
1870 list_del_init(&dp->dl_recall_lru);
1871 put_clnt_odstate(dp->dl_clnt_odstate);
1872 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
1873 nfs4_put_stid(&dp->dl_stid);
1875 while (!list_empty(&clp->cl_revoked)) {
1876 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
1877 list_del_init(&dp->dl_recall_lru);
1878 nfs4_put_stid(&dp->dl_stid);
1880 while (!list_empty(&clp->cl_openowners)) {
1881 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1882 nfs4_get_stateowner(&oo->oo_owner);
1883 release_openowner(oo);
1885 nfsd4_return_all_client_layouts(clp);
1886 nfsd4_shutdown_callback(clp);
1887 if (clp->cl_cb_conn.cb_xprt)
1888 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
1893 destroy_client(struct nfs4_client *clp)
1896 __destroy_client(clp);
1899 static void expire_client(struct nfs4_client *clp)
1902 nfsd4_client_record_remove(clp);
1903 __destroy_client(clp);
1906 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1908 memcpy(target->cl_verifier.data, source->data,
1909 sizeof(target->cl_verifier.data));
1912 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1914 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1915 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1918 int strdup_if_nonnull(char **target, char *source)
1921 *target = kstrdup(source, GFP_KERNEL);
1929 static int copy_cred(struct svc_cred *target, struct svc_cred *source)
1933 ret = strdup_if_nonnull(&target->cr_principal, source->cr_principal);
1936 ret = strdup_if_nonnull(&target->cr_raw_principal,
1937 source->cr_raw_principal);
1940 target->cr_flavor = source->cr_flavor;
1941 target->cr_uid = source->cr_uid;
1942 target->cr_gid = source->cr_gid;
1943 target->cr_group_info = source->cr_group_info;
1944 get_group_info(target->cr_group_info);
1945 target->cr_gss_mech = source->cr_gss_mech;
1946 if (source->cr_gss_mech)
1947 gss_mech_get(source->cr_gss_mech);
1952 compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1954 if (o1->len < o2->len)
1956 if (o1->len > o2->len)
1958 return memcmp(o1->data, o2->data, o1->len);
1961 static int same_name(const char *n1, const char *n2)
1963 return 0 == memcmp(n1, n2, HEXDIR_LEN);
1967 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1969 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
1973 same_clid(clientid_t *cl1, clientid_t *cl2)
1975 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
1978 static bool groups_equal(struct group_info *g1, struct group_info *g2)
1982 if (g1->ngroups != g2->ngroups)
1984 for (i=0; i<g1->ngroups; i++)
1985 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
1991 * RFC 3530 language requires clid_inuse be returned when the
1992 * "principal" associated with a requests differs from that previously
1993 * used. We use uid, gid's, and gss principal string as our best
1994 * approximation. We also don't want to allow non-gss use of a client
1995 * established using gss: in theory cr_principal should catch that
1996 * change, but in practice cr_principal can be null even in the gss case
1997 * since gssd doesn't always pass down a principal string.
1999 static bool is_gss_cred(struct svc_cred *cr)
2001 /* Is cr_flavor one of the gss "pseudoflavors"?: */
2002 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
2007 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
2009 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
2010 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
2011 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
2012 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
2014 if (cr1->cr_principal == cr2->cr_principal)
2016 if (!cr1->cr_principal || !cr2->cr_principal)
2018 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
2021 static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
2023 struct svc_cred *cr = &rqstp->rq_cred;
2026 if (!cr->cr_gss_mech)
2028 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
2029 return service == RPC_GSS_SVC_INTEGRITY ||
2030 service == RPC_GSS_SVC_PRIVACY;
2033 bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
2035 struct svc_cred *cr = &rqstp->rq_cred;
2037 if (!cl->cl_mach_cred)
2039 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
2041 if (!svc_rqst_integrity_protected(rqstp))
2043 if (cl->cl_cred.cr_raw_principal)
2044 return 0 == strcmp(cl->cl_cred.cr_raw_principal,
2045 cr->cr_raw_principal);
2046 if (!cr->cr_principal)
2048 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
2051 static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
2056 * This is opaque to client, so no need to byte-swap. Use
2057 * __force to keep sparse happy
2059 verf[0] = (__force __be32)get_seconds();
2060 verf[1] = (__force __be32)nn->clverifier_counter++;
2061 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
2064 static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
2066 clp->cl_clientid.cl_boot = nn->boot_time;
2067 clp->cl_clientid.cl_id = nn->clientid_counter++;
2068 gen_confirm(clp, nn);
2071 static struct nfs4_stid *
2072 find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
2074 struct nfs4_stid *ret;
2076 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
2077 if (!ret || !ret->sc_type)
2082 static struct nfs4_stid *
2083 find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
2085 struct nfs4_stid *s;
2087 spin_lock(&cl->cl_lock);
2088 s = find_stateid_locked(cl, t);
2090 if (typemask & s->sc_type)
2091 atomic_inc(&s->sc_count);
2095 spin_unlock(&cl->cl_lock);
2099 static struct nfs4_client *create_client(struct xdr_netobj name,
2100 struct svc_rqst *rqstp, nfs4_verifier *verf)
2102 struct nfs4_client *clp;
2103 struct sockaddr *sa = svc_addr(rqstp);
2105 struct net *net = SVC_NET(rqstp);
2107 clp = alloc_client(name);
2111 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
2116 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
2117 clp->cl_time = get_seconds();
2118 clear_bit(0, &clp->cl_cb_slot_busy);
2119 copy_verf(clp, verf);
2120 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
2121 clp->cl_cb_session = NULL;
2127 add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
2129 struct rb_node **new = &(root->rb_node), *parent = NULL;
2130 struct nfs4_client *clp;
2133 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
2136 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
2137 new = &((*new)->rb_left);
2139 new = &((*new)->rb_right);
2142 rb_link_node(&new_clp->cl_namenode, parent, new);
2143 rb_insert_color(&new_clp->cl_namenode, root);
2146 static struct nfs4_client *
2147 find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2150 struct rb_node *node = root->rb_node;
2151 struct nfs4_client *clp;
2154 clp = rb_entry(node, struct nfs4_client, cl_namenode);
2155 cmp = compare_blob(&clp->cl_name, name);
2157 node = node->rb_left;
2159 node = node->rb_right;
2167 add_to_unconfirmed(struct nfs4_client *clp)
2169 unsigned int idhashval;
2170 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2172 lockdep_assert_held(&nn->client_lock);
2174 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2175 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
2176 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
2177 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
2178 renew_client_locked(clp);
2182 move_to_confirmed(struct nfs4_client *clp)
2184 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
2185 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2187 lockdep_assert_held(&nn->client_lock);
2189 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
2190 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
2191 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
2192 add_clp_to_name_tree(clp, &nn->conf_name_tree);
2193 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2194 renew_client_locked(clp);
2197 static struct nfs4_client *
2198 find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
2200 struct nfs4_client *clp;
2201 unsigned int idhashval = clientid_hashval(clid->cl_id);
2203 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
2204 if (same_clid(&clp->cl_clientid, clid)) {
2205 if ((bool)clp->cl_minorversion != sessions)
2207 renew_client_locked(clp);
2214 static struct nfs4_client *
2215 find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2217 struct list_head *tbl = nn->conf_id_hashtbl;
2219 lockdep_assert_held(&nn->client_lock);
2220 return find_client_in_id_table(tbl, clid, sessions);
2223 static struct nfs4_client *
2224 find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2226 struct list_head *tbl = nn->unconf_id_hashtbl;
2228 lockdep_assert_held(&nn->client_lock);
2229 return find_client_in_id_table(tbl, clid, sessions);
2232 static bool clp_used_exchangeid(struct nfs4_client *clp)
2234 return clp->cl_exchange_flags != 0;
2237 static struct nfs4_client *
2238 find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
2240 lockdep_assert_held(&nn->client_lock);
2241 return find_clp_in_name_tree(name, &nn->conf_name_tree);
2244 static struct nfs4_client *
2245 find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
2247 lockdep_assert_held(&nn->client_lock);
2248 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
2252 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
2254 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
2255 struct sockaddr *sa = svc_addr(rqstp);
2256 u32 scopeid = rpc_get_scope_id(sa);
2257 unsigned short expected_family;
2259 /* Currently, we only support tcp and tcp6 for the callback channel */
2260 if (se->se_callback_netid_len == 3 &&
2261 !memcmp(se->se_callback_netid_val, "tcp", 3))
2262 expected_family = AF_INET;
2263 else if (se->se_callback_netid_len == 4 &&
2264 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2265 expected_family = AF_INET6;
2269 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
2270 se->se_callback_addr_len,
2271 (struct sockaddr *)&conn->cb_addr,
2272 sizeof(conn->cb_addr));
2274 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
2277 if (conn->cb_addr.ss_family == AF_INET6)
2278 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
2280 conn->cb_prog = se->se_callback_prog;
2281 conn->cb_ident = se->se_callback_ident;
2282 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
2285 conn->cb_addr.ss_family = AF_UNSPEC;
2286 conn->cb_addrlen = 0;
2287 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
2288 "will not receive delegations\n",
2289 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2295 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
2298 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2300 struct xdr_buf *buf = resp->xdr.buf;
2301 struct nfsd4_slot *slot = resp->cstate.slot;
2304 dprintk("--> %s slot %p\n", __func__, slot);
2306 slot->sl_opcnt = resp->opcnt;
2307 slot->sl_status = resp->cstate.status;
2309 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
2310 if (nfsd4_not_cached(resp)) {
2311 slot->sl_datalen = 0;
2314 base = resp->cstate.data_offset;
2315 slot->sl_datalen = buf->len - base;
2316 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
2317 WARN(1, "%s: sessions DRC could not cache compound\n",
2323 * Encode the replay sequence operation from the slot values.
2324 * If cachethis is FALSE encode the uncached rep error on the next
2325 * operation which sets resp->p and increments resp->opcnt for
2326 * nfs4svc_encode_compoundres.
2330 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2331 struct nfsd4_compoundres *resp)
2333 struct nfsd4_op *op;
2334 struct nfsd4_slot *slot = resp->cstate.slot;
2336 /* Encode the replayed sequence operation */
2337 op = &args->ops[resp->opcnt - 1];
2338 nfsd4_encode_operation(resp, op);
2340 /* Return nfserr_retry_uncached_rep in next operation. */
2341 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
2342 op = &args->ops[resp->opcnt++];
2343 op->status = nfserr_retry_uncached_rep;
2344 nfsd4_encode_operation(resp, op);
2350 * The sequence operation is not cached because we can use the slot and
2354 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2355 struct nfsd4_sequence *seq)
2357 struct nfsd4_slot *slot = resp->cstate.slot;
2358 struct xdr_stream *xdr = &resp->xdr;
2362 dprintk("--> %s slot %p\n", __func__, slot);
2364 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
2368 p = xdr_reserve_space(xdr, slot->sl_datalen);
2371 return nfserr_serverfault;
2373 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2374 xdr_commit_encode(xdr);
2376 resp->opcnt = slot->sl_opcnt;
2377 return slot->sl_status;
2381 * Set the exchange_id flags returned by the server.
2384 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2386 #ifdef CONFIG_NFSD_PNFS
2387 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
2389 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
2392 /* Referrals are supported, Migration is not. */
2393 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2395 /* set the wire flags to return to client. */
2396 clid->flags = new->cl_exchange_flags;
2399 static bool client_has_openowners(struct nfs4_client *clp)
2401 struct nfs4_openowner *oo;
2403 list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
2404 if (!list_empty(&oo->oo_owner.so_stateids))
2410 static bool client_has_state(struct nfs4_client *clp)
2412 return client_has_openowners(clp)
2413 #ifdef CONFIG_NFSD_PNFS
2414 || !list_empty(&clp->cl_lo_states)
2416 || !list_empty(&clp->cl_delegations)
2417 || !list_empty(&clp->cl_sessions);
2421 nfsd4_exchange_id(struct svc_rqst *rqstp,
2422 struct nfsd4_compound_state *cstate,
2423 struct nfsd4_exchange_id *exid)
2425 struct nfs4_client *conf, *new;
2426 struct nfs4_client *unconf = NULL;
2428 char addr_str[INET6_ADDRSTRLEN];
2429 nfs4_verifier verf = exid->verifier;
2430 struct sockaddr *sa = svc_addr(rqstp);
2431 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
2432 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2434 rpc_ntop(sa, addr_str, sizeof(addr_str));
2435 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
2436 "ip_addr=%s flags %x, spa_how %d\n",
2437 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
2438 addr_str, exid->flags, exid->spa_how);
2440 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
2441 return nfserr_inval;
2443 new = create_client(exid->clname, rqstp, &verf);
2445 return nfserr_jukebox;
2447 switch (exid->spa_how) {
2449 exid->spo_must_enforce[0] = 0;
2450 exid->spo_must_enforce[1] = (
2451 1 << (OP_BIND_CONN_TO_SESSION - 32) |
2452 1 << (OP_EXCHANGE_ID - 32) |
2453 1 << (OP_CREATE_SESSION - 32) |
2454 1 << (OP_DESTROY_SESSION - 32) |
2455 1 << (OP_DESTROY_CLIENTID - 32));
2457 exid->spo_must_allow[0] &= (1 << (OP_CLOSE) |
2458 1 << (OP_OPEN_DOWNGRADE) |
2460 1 << (OP_DELEGRETURN));
2462 exid->spo_must_allow[1] &= (
2463 1 << (OP_TEST_STATEID - 32) |
2464 1 << (OP_FREE_STATEID - 32));
2465 if (!svc_rqst_integrity_protected(rqstp)) {
2466 status = nfserr_inval;
2470 * Sometimes userspace doesn't give us a principal.
2471 * Which is a bug, really. Anyway, we can't enforce
2472 * MACH_CRED in that case, better to give up now:
2474 if (!new->cl_cred.cr_principal &&
2475 !new->cl_cred.cr_raw_principal) {
2476 status = nfserr_serverfault;
2479 new->cl_mach_cred = true;
2482 default: /* checked by xdr code */
2485 status = nfserr_encr_alg_unsupp;
2489 /* Cases below refer to rfc 5661 section 18.35.4: */
2490 spin_lock(&nn->client_lock);
2491 conf = find_confirmed_client_by_name(&exid->clname, nn);
2493 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2494 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2497 if (!clp_used_exchangeid(conf)) { /* buggy client */
2498 status = nfserr_inval;
2501 if (!nfsd4_mach_creds_match(conf, rqstp)) {
2502 status = nfserr_wrong_cred;
2505 if (!creds_match) { /* case 9 */
2506 status = nfserr_perm;
2509 if (!verfs_match) { /* case 8 */
2510 status = nfserr_not_same;
2514 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
2517 if (!creds_match) { /* case 3 */
2518 if (client_has_state(conf)) {
2519 status = nfserr_clid_inuse;
2524 if (verfs_match) { /* case 2 */
2525 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
2528 /* case 5, client reboot */
2533 if (update) { /* case 7 */
2534 status = nfserr_noent;
2538 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
2539 if (unconf) /* case 4, possible retry or client restart */
2540 unhash_client_locked(unconf);
2542 /* case 1 (normal case) */
2545 status = mark_client_expired_locked(conf);
2549 new->cl_minorversion = cstate->minorversion;
2550 new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0];
2551 new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1];
2554 add_to_unconfirmed(new);
2557 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
2558 exid->clientid.cl_id = conf->cl_clientid.cl_id;
2560 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
2561 nfsd4_set_ex_flags(conf, exid);
2563 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
2564 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
2568 spin_unlock(&nn->client_lock);
2573 expire_client(unconf);
2578 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
2580 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2583 /* The slot is in use, and no response has been sent. */
2585 if (seqid == slot_seqid)
2586 return nfserr_jukebox;
2588 return nfserr_seq_misordered;
2590 /* Note unsigned 32-bit arithmetic handles wraparound: */
2591 if (likely(seqid == slot_seqid + 1))
2593 if (seqid == slot_seqid)
2594 return nfserr_replay_cache;
2595 return nfserr_seq_misordered;
2599 * Cache the create session result into the create session single DRC
2600 * slot cache by saving the xdr structure. sl_seqid has been set.
2601 * Do this for solo or embedded create session operations.
2604 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
2605 struct nfsd4_clid_slot *slot, __be32 nfserr)
2607 slot->sl_status = nfserr;
2608 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2612 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2613 struct nfsd4_clid_slot *slot)
2615 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2616 return slot->sl_status;
2619 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2620 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2621 1 + /* MIN tag is length with zero, only length */ \
2622 3 + /* version, opcount, opcode */ \
2623 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2624 /* seqid, slotID, slotID, cache */ \
2625 4 ) * sizeof(__be32))
2627 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2628 2 + /* verifier: AUTH_NULL, length 0 */\
2630 1 + /* MIN tag is length with zero, only length */ \
2631 3 + /* opcount, opcode, opstatus*/ \
2632 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2633 /* seqid, slotID, slotID, slotID, status */ \
2634 5 ) * sizeof(__be32))
2636 static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
2638 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2640 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2641 return nfserr_toosmall;
2642 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2643 return nfserr_toosmall;
2644 ca->headerpadsz = 0;
2645 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2646 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2647 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2648 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2649 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2650 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2652 * Note decreasing slot size below client's request may make it
2653 * difficult for client to function correctly, whereas
2654 * decreasing the number of slots will (just?) affect
2655 * performance. When short on memory we therefore prefer to
2656 * decrease number of slots instead of their size. Clients that
2657 * request larger slots than they need will get poor results:
2659 ca->maxreqs = nfsd4_get_drc_mem(ca);
2661 return nfserr_jukebox;
2667 * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
2668 * These are based on similar macros in linux/sunrpc/msg_prot.h .
2670 #define RPC_MAX_HEADER_WITH_AUTH_SYS \
2671 (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
2673 #define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
2674 (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
2676 #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2677 RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
2678 #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2679 RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
2682 static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2684 ca->headerpadsz = 0;
2686 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
2687 return nfserr_toosmall;
2688 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
2689 return nfserr_toosmall;
2690 ca->maxresp_cached = 0;
2692 return nfserr_toosmall;
2697 static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2699 switch (cbs->flavor) {
2705 * GSS case: the spec doesn't allow us to return this
2706 * error. But it also doesn't allow us not to support
2708 * I'd rather this fail hard than return some error the
2709 * client might think it can already handle:
2711 return nfserr_encr_alg_unsupp;
2716 nfsd4_create_session(struct svc_rqst *rqstp,
2717 struct nfsd4_compound_state *cstate,
2718 struct nfsd4_create_session *cr_ses)
2720 struct sockaddr *sa = svc_addr(rqstp);
2721 struct nfs4_client *conf, *unconf;
2722 struct nfs4_client *old = NULL;
2723 struct nfsd4_session *new;
2724 struct nfsd4_conn *conn;
2725 struct nfsd4_clid_slot *cs_slot = NULL;
2727 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2729 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2730 return nfserr_inval;
2731 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2734 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
2737 status = check_backchannel_attrs(&cr_ses->back_channel);
2739 goto out_release_drc_mem;
2740 status = nfserr_jukebox;
2741 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
2743 goto out_release_drc_mem;
2744 conn = alloc_conn_from_crses(rqstp, cr_ses);
2746 goto out_free_session;
2748 spin_lock(&nn->client_lock);
2749 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
2750 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
2751 WARN_ON_ONCE(conf && unconf);
2754 status = nfserr_wrong_cred;
2755 if (!nfsd4_mach_creds_match(conf, rqstp))
2757 cs_slot = &conf->cl_cs_slot;
2758 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2760 if (status == nfserr_replay_cache)
2761 status = nfsd4_replay_create_session(cr_ses, cs_slot);
2764 } else if (unconf) {
2765 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
2766 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
2767 status = nfserr_clid_inuse;
2770 status = nfserr_wrong_cred;
2771 if (!nfsd4_mach_creds_match(unconf, rqstp))
2773 cs_slot = &unconf->cl_cs_slot;
2774 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2776 /* an unconfirmed replay returns misordered */
2777 status = nfserr_seq_misordered;
2780 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
2782 status = mark_client_expired_locked(old);
2788 move_to_confirmed(unconf);
2791 status = nfserr_stale_clientid;
2795 /* Persistent sessions are not supported */
2796 cr_ses->flags &= ~SESSION4_PERSIST;
2797 /* Upshifting from TCP to RDMA is not supported */
2798 cr_ses->flags &= ~SESSION4_RDMA;
2800 init_session(rqstp, new, conf, cr_ses);
2801 nfsd4_get_session_locked(new);
2803 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
2804 NFS4_MAX_SESSIONID_LEN);
2805 cs_slot->sl_seqid++;
2806 cr_ses->seqid = cs_slot->sl_seqid;
2808 /* cache solo and embedded create sessions under the client_lock */
2809 nfsd4_cache_create_session(cr_ses, cs_slot, status);
2810 spin_unlock(&nn->client_lock);
2811 /* init connection and backchannel */
2812 nfsd4_init_conn(rqstp, conn, new);
2813 nfsd4_put_session(new);
2818 spin_unlock(&nn->client_lock);
2823 __free_session(new);
2824 out_release_drc_mem:
2825 nfsd4_put_drc_mem(&cr_ses->fore_channel);
2829 static __be32 nfsd4_map_bcts_dir(u32 *dir)
2832 case NFS4_CDFC4_FORE:
2833 case NFS4_CDFC4_BACK:
2835 case NFS4_CDFC4_FORE_OR_BOTH:
2836 case NFS4_CDFC4_BACK_OR_BOTH:
2837 *dir = NFS4_CDFC4_BOTH;
2840 return nfserr_inval;
2843 __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2845 struct nfsd4_session *session = cstate->session;
2846 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2849 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2852 spin_lock(&nn->client_lock);
2853 session->se_cb_prog = bc->bc_cb_program;
2854 session->se_cb_sec = bc->bc_cb_sec;
2855 spin_unlock(&nn->client_lock);
2857 nfsd4_probe_callback(session->se_client);
2862 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2863 struct nfsd4_compound_state *cstate,
2864 struct nfsd4_bind_conn_to_session *bcts)
2867 struct nfsd4_conn *conn;
2868 struct nfsd4_session *session;
2869 struct net *net = SVC_NET(rqstp);
2870 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2872 if (!nfsd4_last_compound_op(rqstp))
2873 return nfserr_not_only_op;
2874 spin_lock(&nn->client_lock);
2875 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
2876 spin_unlock(&nn->client_lock);
2878 goto out_no_session;
2879 status = nfserr_wrong_cred;
2880 if (!nfsd4_mach_creds_match(session->se_client, rqstp))
2882 status = nfsd4_map_bcts_dir(&bcts->dir);
2885 conn = alloc_conn(rqstp, bcts->dir);
2886 status = nfserr_jukebox;
2889 nfsd4_init_conn(rqstp, conn, session);
2892 nfsd4_put_session(session);
2897 static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2901 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2905 nfsd4_destroy_session(struct svc_rqst *r,
2906 struct nfsd4_compound_state *cstate,
2907 struct nfsd4_destroy_session *sessionid)
2909 struct nfsd4_session *ses;
2911 int ref_held_by_me = 0;
2912 struct net *net = SVC_NET(r);
2913 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2915 status = nfserr_not_only_op;
2916 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
2917 if (!nfsd4_last_compound_op(r))
2921 dump_sessionid(__func__, &sessionid->sessionid);
2922 spin_lock(&nn->client_lock);
2923 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
2925 goto out_client_lock;
2926 status = nfserr_wrong_cred;
2927 if (!nfsd4_mach_creds_match(ses->se_client, r))
2928 goto out_put_session;
2929 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
2931 goto out_put_session;
2932 unhash_session(ses);
2933 spin_unlock(&nn->client_lock);
2935 nfsd4_probe_callback_sync(ses->se_client);
2937 spin_lock(&nn->client_lock);
2940 nfsd4_put_session_locked(ses);
2942 spin_unlock(&nn->client_lock);
2947 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
2949 struct nfsd4_conn *c;
2951 list_for_each_entry(c, &s->se_conns, cn_persession) {
2952 if (c->cn_xprt == xpt) {
2959 static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
2961 struct nfs4_client *clp = ses->se_client;
2962 struct nfsd4_conn *c;
2963 __be32 status = nfs_ok;
2966 spin_lock(&clp->cl_lock);
2967 c = __nfsd4_find_conn(new->cn_xprt, ses);
2970 status = nfserr_conn_not_bound_to_session;
2971 if (clp->cl_mach_cred)
2973 __nfsd4_hash_conn(new, ses);
2974 spin_unlock(&clp->cl_lock);
2975 ret = nfsd4_register_conn(new);
2977 /* oops; xprt is already down: */
2978 nfsd4_conn_lost(&new->cn_xpt_user);
2981 spin_unlock(&clp->cl_lock);
2986 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2988 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2990 return args->opcnt > session->se_fchannel.maxops;
2993 static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2994 struct nfsd4_session *session)
2996 struct xdr_buf *xb = &rqstp->rq_arg;
2998 return xb->len > session->se_fchannel.maxreq_sz;
3002 nfsd4_sequence(struct svc_rqst *rqstp,
3003 struct nfsd4_compound_state *cstate,
3004 struct nfsd4_sequence *seq)
3006 struct nfsd4_compoundres *resp = rqstp->rq_resp;
3007 struct xdr_stream *xdr = &resp->xdr;
3008 struct nfsd4_session *session;
3009 struct nfs4_client *clp;
3010 struct nfsd4_slot *slot;
3011 struct nfsd4_conn *conn;
3014 struct net *net = SVC_NET(rqstp);
3015 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3017 if (resp->opcnt != 1)
3018 return nfserr_sequence_pos;
3021 * Will be either used or freed by nfsd4_sequence_check_conn
3024 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
3026 return nfserr_jukebox;
3028 spin_lock(&nn->client_lock);
3029 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
3031 goto out_no_session;
3032 clp = session->se_client;
3034 status = nfserr_too_many_ops;
3035 if (nfsd4_session_too_many_ops(rqstp, session))
3036 goto out_put_session;
3038 status = nfserr_req_too_big;
3039 if (nfsd4_request_too_big(rqstp, session))
3040 goto out_put_session;
3042 status = nfserr_badslot;
3043 if (seq->slotid >= session->se_fchannel.maxreqs)
3044 goto out_put_session;
3046 slot = session->se_slots[seq->slotid];
3047 dprintk("%s: slotid %d\n", __func__, seq->slotid);
3049 /* We do not negotiate the number of slots yet, so set the
3050 * maxslots to the session maxreqs which is used to encode
3051 * sr_highest_slotid and the sr_target_slot id to maxslots */
3052 seq->maxslots = session->se_fchannel.maxreqs;
3054 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
3055 slot->sl_flags & NFSD4_SLOT_INUSE);
3056 if (status == nfserr_replay_cache) {
3057 status = nfserr_seq_misordered;
3058 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
3059 goto out_put_session;
3060 cstate->slot = slot;
3061 cstate->session = session;
3063 /* Return the cached reply status and set cstate->status
3064 * for nfsd4_proc_compound processing */
3065 status = nfsd4_replay_cache_entry(resp, seq);
3066 cstate->status = nfserr_replay_cache;
3070 goto out_put_session;
3072 status = nfsd4_sequence_check_conn(conn, session);
3075 goto out_put_session;
3077 buflen = (seq->cachethis) ?
3078 session->se_fchannel.maxresp_cached :
3079 session->se_fchannel.maxresp_sz;
3080 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
3082 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
3083 goto out_put_session;
3084 svc_reserve(rqstp, buflen);
3087 /* Success! bump slot seqid */
3088 slot->sl_seqid = seq->seqid;
3089 slot->sl_flags |= NFSD4_SLOT_INUSE;
3091 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
3093 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
3095 cstate->slot = slot;
3096 cstate->session = session;
3100 switch (clp->cl_cb_state) {
3102 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
3104 case NFSD4_CB_FAULT:
3105 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
3108 seq->status_flags = 0;
3110 if (!list_empty(&clp->cl_revoked))
3111 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
3115 spin_unlock(&nn->client_lock);
3118 nfsd4_put_session_locked(session);
3119 goto out_no_session;
3123 nfsd4_sequence_done(struct nfsd4_compoundres *resp)
3125 struct nfsd4_compound_state *cs = &resp->cstate;
3127 if (nfsd4_has_session(cs)) {
3128 if (cs->status != nfserr_replay_cache) {
3129 nfsd4_store_cache_entry(resp);
3130 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
3132 /* Drop session reference that was taken in nfsd4_sequence() */
3133 nfsd4_put_session(cs->session);
3135 put_client_renew(cs->clp);
3139 nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
3141 struct nfs4_client *conf, *unconf;
3142 struct nfs4_client *clp = NULL;
3144 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3146 spin_lock(&nn->client_lock);
3147 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
3148 conf = find_confirmed_client(&dc->clientid, true, nn);
3149 WARN_ON_ONCE(conf && unconf);
3152 if (client_has_state(conf)) {
3153 status = nfserr_clientid_busy;
3156 status = mark_client_expired_locked(conf);
3163 status = nfserr_stale_clientid;
3166 if (!nfsd4_mach_creds_match(clp, rqstp)) {
3168 status = nfserr_wrong_cred;
3171 unhash_client_locked(clp);
3173 spin_unlock(&nn->client_lock);
3180 nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
3184 if (rc->rca_one_fs) {
3185 if (!cstate->current_fh.fh_dentry)
3186 return nfserr_nofilehandle;
3188 * We don't take advantage of the rca_one_fs case.
3189 * That's OK, it's optional, we can safely ignore it.
3194 status = nfserr_complete_already;
3195 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
3196 &cstate->session->se_client->cl_flags))
3199 status = nfserr_stale_clientid;
3200 if (is_client_expired(cstate->session->se_client))
3202 * The following error isn't really legal.
3203 * But we only get here if the client just explicitly
3204 * destroyed the client. Surely it no longer cares what
3205 * error it gets back on an operation for the dead
3211 nfsd4_client_record_create(cstate->session->se_client);
3217 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3218 struct nfsd4_setclientid *setclid)
3220 struct xdr_netobj clname = setclid->se_name;
3221 nfs4_verifier clverifier = setclid->se_verf;
3222 struct nfs4_client *conf, *new;
3223 struct nfs4_client *unconf = NULL;
3225 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3227 new = create_client(clname, rqstp, &clverifier);
3229 return nfserr_jukebox;
3230 /* Cases below refer to rfc 3530 section 14.2.33: */
3231 spin_lock(&nn->client_lock);
3232 conf = find_confirmed_client_by_name(&clname, nn);
3233 if (conf && client_has_state(conf)) {
3235 status = nfserr_clid_inuse;
3236 if (clp_used_exchangeid(conf))
3238 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
3239 char addr_str[INET6_ADDRSTRLEN];
3240 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
3242 dprintk("NFSD: setclientid: string in use by client "
3243 "at %s\n", addr_str);
3247 unconf = find_unconfirmed_client_by_name(&clname, nn);
3249 unhash_client_locked(unconf);
3250 if (conf && same_verf(&conf->cl_verifier, &clverifier)) {
3251 /* case 1: probable callback update */
3252 copy_clid(new, conf);
3253 gen_confirm(new, nn);
3254 } else /* case 4 (new client) or cases 2, 3 (client reboot): */
3256 new->cl_minorversion = 0;
3257 gen_callback(new, setclid, rqstp);
3258 add_to_unconfirmed(new);
3259 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
3260 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
3261 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
3265 spin_unlock(&nn->client_lock);
3269 expire_client(unconf);
3275 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
3276 struct nfsd4_compound_state *cstate,
3277 struct nfsd4_setclientid_confirm *setclientid_confirm)
3279 struct nfs4_client *conf, *unconf;
3280 struct nfs4_client *old = NULL;
3281 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
3282 clientid_t * clid = &setclientid_confirm->sc_clientid;
3284 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3286 if (STALE_CLIENTID(clid, nn))
3287 return nfserr_stale_clientid;
3289 spin_lock(&nn->client_lock);
3290 conf = find_confirmed_client(clid, false, nn);
3291 unconf = find_unconfirmed_client(clid, false, nn);
3293 * We try hard to give out unique clientid's, so if we get an
3294 * attempt to confirm the same clientid with a different cred,
3295 * the client may be buggy; this should never happen.
3297 * Nevertheless, RFC 7530 recommends INUSE for this case:
3299 status = nfserr_clid_inuse;
3300 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3302 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3304 /* cases below refer to rfc 3530 section 14.2.34: */
3305 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
3306 if (conf && !unconf) /* case 2: probable retransmit */
3308 else /* case 4: client hasn't noticed we rebooted yet? */
3309 status = nfserr_stale_clientid;
3313 if (conf) { /* case 1: callback update */
3315 unhash_client_locked(old);
3316 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
3317 } else { /* case 3: normal case; new or rebooted client */
3318 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3320 status = nfserr_clid_inuse;
3321 if (client_has_state(old)
3322 && !same_creds(&unconf->cl_cred,
3325 status = mark_client_expired_locked(old);
3331 move_to_confirmed(unconf);
3334 get_client_locked(conf);
3335 spin_unlock(&nn->client_lock);
3336 nfsd4_probe_callback(conf);
3337 spin_lock(&nn->client_lock);
3338 put_client_renew_locked(conf);
3340 spin_unlock(&nn->client_lock);
3346 static struct nfs4_file *nfsd4_alloc_file(void)
3348 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3351 /* OPEN Share state helper functions */
3352 static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
3353 struct nfs4_file *fp)
3355 lockdep_assert_held(&state_lock);
3357 atomic_set(&fp->fi_ref, 1);
3358 spin_lock_init(&fp->fi_lock);
3359 INIT_LIST_HEAD(&fp->fi_stateids);
3360 INIT_LIST_HEAD(&fp->fi_delegations);
3361 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
3362 fh_copy_shallow(&fp->fi_fhandle, fh);
3363 fp->fi_deleg_file = NULL;
3364 fp->fi_had_conflict = false;
3365 fp->fi_share_deny = 0;
3366 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3367 memset(fp->fi_access, 0, sizeof(fp->fi_access));
3368 #ifdef CONFIG_NFSD_PNFS
3369 INIT_LIST_HEAD(&fp->fi_lo_states);
3370 atomic_set(&fp->fi_lo_recalls, 0);
3372 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
3376 nfsd4_free_slabs(void)
3378 kmem_cache_destroy(odstate_slab);
3379 kmem_cache_destroy(openowner_slab);
3380 kmem_cache_destroy(lockowner_slab);
3381 kmem_cache_destroy(file_slab);
3382 kmem_cache_destroy(stateid_slab);
3383 kmem_cache_destroy(deleg_slab);
3387 nfsd4_init_slabs(void)
3389 openowner_slab = kmem_cache_create("nfsd4_openowners",
3390 sizeof(struct nfs4_openowner), 0, 0, NULL);
3391 if (openowner_slab == NULL)
3393 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
3394 sizeof(struct nfs4_lockowner), 0, 0, NULL);
3395 if (lockowner_slab == NULL)
3396 goto out_free_openowner_slab;
3397 file_slab = kmem_cache_create("nfsd4_files",
3398 sizeof(struct nfs4_file), 0, 0, NULL);
3399 if (file_slab == NULL)
3400 goto out_free_lockowner_slab;
3401 stateid_slab = kmem_cache_create("nfsd4_stateids",
3402 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
3403 if (stateid_slab == NULL)
3404 goto out_free_file_slab;
3405 deleg_slab = kmem_cache_create("nfsd4_delegations",
3406 sizeof(struct nfs4_delegation), 0, 0, NULL);
3407 if (deleg_slab == NULL)
3408 goto out_free_stateid_slab;
3409 odstate_slab = kmem_cache_create("nfsd4_odstate",
3410 sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
3411 if (odstate_slab == NULL)
3412 goto out_free_deleg_slab;
3415 out_free_deleg_slab:
3416 kmem_cache_destroy(deleg_slab);
3417 out_free_stateid_slab:
3418 kmem_cache_destroy(stateid_slab);
3420 kmem_cache_destroy(file_slab);
3421 out_free_lockowner_slab:
3422 kmem_cache_destroy(lockowner_slab);
3423 out_free_openowner_slab:
3424 kmem_cache_destroy(openowner_slab);
3426 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3430 static void init_nfs4_replay(struct nfs4_replay *rp)
3432 rp->rp_status = nfserr_serverfault;
3434 rp->rp_buf = rp->rp_ibuf;
3435 mutex_init(&rp->rp_mutex);
3438 static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3439 struct nfs4_stateowner *so)
3441 if (!nfsd4_has_session(cstate)) {
3442 mutex_lock(&so->so_replay.rp_mutex);
3443 cstate->replay_owner = nfs4_get_stateowner(so);
3447 void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3449 struct nfs4_stateowner *so = cstate->replay_owner;
3452 cstate->replay_owner = NULL;
3453 mutex_unlock(&so->so_replay.rp_mutex);
3454 nfs4_put_stateowner(so);
3458 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
3460 struct nfs4_stateowner *sop;
3462 sop = kmem_cache_alloc(slab, GFP_KERNEL);
3466 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3467 if (!sop->so_owner.data) {
3468 kmem_cache_free(slab, sop);
3471 sop->so_owner.len = owner->len;
3473 INIT_LIST_HEAD(&sop->so_stateids);
3474 sop->so_client = clp;
3475 init_nfs4_replay(&sop->so_replay);
3476 atomic_set(&sop->so_count, 1);
3480 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
3482 lockdep_assert_held(&clp->cl_lock);
3484 list_add(&oo->oo_owner.so_strhash,
3485 &clp->cl_ownerstr_hashtbl[strhashval]);
3486 list_add(&oo->oo_perclient, &clp->cl_openowners);
3489 static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3491 unhash_openowner_locked(openowner(so));
3494 static void nfs4_free_openowner(struct nfs4_stateowner *so)
3496 struct nfs4_openowner *oo = openowner(so);
3498 kmem_cache_free(openowner_slab, oo);
3501 static const struct nfs4_stateowner_operations openowner_ops = {
3502 .so_unhash = nfs4_unhash_openowner,
3503 .so_free = nfs4_free_openowner,
3506 static struct nfs4_ol_stateid *
3507 nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
3509 struct nfs4_ol_stateid *local, *ret = NULL;
3510 struct nfs4_openowner *oo = open->op_openowner;
3512 lockdep_assert_held(&fp->fi_lock);
3514 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
3515 /* ignore lock owners */
3516 if (local->st_stateowner->so_is_open_owner == 0)
3518 if (local->st_stateowner == &oo->oo_owner) {
3520 atomic_inc(&ret->st_stid.sc_count);
3527 static struct nfs4_openowner *
3528 alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
3529 struct nfsd4_compound_state *cstate)
3531 struct nfs4_client *clp = cstate->clp;
3532 struct nfs4_openowner *oo, *ret;
3534 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3537 oo->oo_owner.so_ops = &openowner_ops;
3538 oo->oo_owner.so_is_open_owner = 1;
3539 oo->oo_owner.so_seqid = open->op_seqid;
3541 if (nfsd4_has_session(cstate))
3542 oo->oo_flags |= NFS4_OO_CONFIRMED;
3544 oo->oo_last_closed_stid = NULL;
3545 INIT_LIST_HEAD(&oo->oo_close_lru);
3546 spin_lock(&clp->cl_lock);
3547 ret = find_openstateowner_str_locked(strhashval, open, clp);
3549 hash_openowner(oo, clp, strhashval);
3552 nfs4_free_stateowner(&oo->oo_owner);
3554 spin_unlock(&clp->cl_lock);
3558 static struct nfs4_ol_stateid *
3559 init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
3562 struct nfs4_openowner *oo = open->op_openowner;
3563 struct nfs4_ol_stateid *retstp = NULL;
3564 struct nfs4_ol_stateid *stp;
3567 /* We are moving these outside of the spinlocks to avoid the warnings */
3568 mutex_init(&stp->st_mutex);
3569 mutex_lock(&stp->st_mutex);
3571 spin_lock(&oo->oo_owner.so_client->cl_lock);
3572 spin_lock(&fp->fi_lock);
3574 retstp = nfsd4_find_existing_open(fp, open);
3578 open->op_stp = NULL;
3579 atomic_inc(&stp->st_stid.sc_count);
3580 stp->st_stid.sc_type = NFS4_OPEN_STID;
3581 INIT_LIST_HEAD(&stp->st_locks);
3582 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
3584 stp->st_stid.sc_file = fp;
3585 stp->st_access_bmap = 0;
3586 stp->st_deny_bmap = 0;
3587 stp->st_openstp = NULL;
3588 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
3589 list_add(&stp->st_perfile, &fp->fi_stateids);
3592 spin_unlock(&fp->fi_lock);
3593 spin_unlock(&oo->oo_owner.so_client->cl_lock);
3595 mutex_lock(&retstp->st_mutex);
3596 /* To keep mutex tracking happy */
3597 mutex_unlock(&stp->st_mutex);
3604 * In the 4.0 case we need to keep the owners around a little while to handle
3605 * CLOSE replay. We still do need to release any file access that is held by
3606 * them before returning however.
3609 move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
3611 struct nfs4_ol_stateid *last;
3612 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3613 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3616 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
3619 * We know that we hold one reference via nfsd4_close, and another
3620 * "persistent" reference for the client. If the refcount is higher
3621 * than 2, then there are still calls in progress that are using this
3622 * stateid. We can't put the sc_file reference until they are finished.
3623 * Wait for the refcount to drop to 2. Since it has been unhashed,
3624 * there should be no danger of the refcount going back up again at
3627 wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3629 release_all_access(s);
3630 if (s->st_stid.sc_file) {
3631 put_nfs4_file(s->st_stid.sc_file);
3632 s->st_stid.sc_file = NULL;
3635 spin_lock(&nn->client_lock);
3636 last = oo->oo_last_closed_stid;
3637 oo->oo_last_closed_stid = s;
3638 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
3639 oo->oo_time = get_seconds();
3640 spin_unlock(&nn->client_lock);
3642 nfs4_put_stid(&last->st_stid);
3645 /* search file_hashtbl[] for file */
3646 static struct nfs4_file *
3647 find_file_locked(struct knfsd_fh *fh, unsigned int hashval)
3649 struct nfs4_file *fp;
3651 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash) {
3652 if (fh_match(&fp->fi_fhandle, fh)) {
3653 if (atomic_inc_not_zero(&fp->fi_ref))
3661 find_file(struct knfsd_fh *fh)
3663 struct nfs4_file *fp;
3664 unsigned int hashval = file_hashval(fh);
3667 fp = find_file_locked(fh, hashval);
3672 static struct nfs4_file *
3673 find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
3675 struct nfs4_file *fp;
3676 unsigned int hashval = file_hashval(fh);
3679 fp = find_file_locked(fh, hashval);
3684 spin_lock(&state_lock);
3685 fp = find_file_locked(fh, hashval);
3686 if (likely(fp == NULL)) {
3687 nfsd4_init_file(fh, hashval, new);
3690 spin_unlock(&state_lock);
3696 * Called to check deny when READ with all zero stateid or
3697 * WRITE with all zero or all one stateid
3700 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3702 struct nfs4_file *fp;
3703 __be32 ret = nfs_ok;
3705 fp = find_file(¤t_fh->fh_handle);
3708 /* Check for conflicting share reservations */
3709 spin_lock(&fp->fi_lock);
3710 if (fp->fi_share_deny & deny_type)
3711 ret = nfserr_locked;
3712 spin_unlock(&fp->fi_lock);
3717 static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
3719 struct nfs4_delegation *dp = cb_to_delegation(cb);
3720 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3723 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
3726 * We can't do this in nfsd_break_deleg_cb because it is
3727 * already holding inode->i_lock.
3729 * If the dl_time != 0, then we know that it has already been
3730 * queued for a lease break. Don't queue it again.
3732 spin_lock(&state_lock);
3733 if (dp->dl_time == 0) {
3734 dp->dl_time = get_seconds();
3735 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
3737 spin_unlock(&state_lock);
3740 static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
3741 struct rpc_task *task)
3743 struct nfs4_delegation *dp = cb_to_delegation(cb);
3745 if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID)
3748 switch (task->tk_status) {
3752 case -NFS4ERR_BAD_STATEID:
3754 * Race: client probably got cb_recall before open reply
3755 * granting delegation.
3757 if (dp->dl_retries--) {
3758 rpc_delay(task, 2 * HZ);
3767 static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
3769 struct nfs4_delegation *dp = cb_to_delegation(cb);
3771 nfs4_put_stid(&dp->dl_stid);
3774 static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
3775 .prepare = nfsd4_cb_recall_prepare,
3776 .done = nfsd4_cb_recall_done,
3777 .release = nfsd4_cb_recall_release,
3780 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3783 * We're assuming the state code never drops its reference
3784 * without first removing the lease. Since we're in this lease
3785 * callback (and since the lease code is serialized by the kernel
3786 * lock) we know the server hasn't removed the lease yet, we know
3787 * it's safe to take a reference.
3789 atomic_inc(&dp->dl_stid.sc_count);
3790 nfsd4_run_cb(&dp->dl_recall);
3793 /* Called from break_lease() with i_lock held. */
3795 nfsd_break_deleg_cb(struct file_lock *fl)
3798 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3799 struct nfs4_delegation *dp;
3802 WARN(1, "(%p)->fl_owner NULL\n", fl);
3805 if (fp->fi_had_conflict) {
3806 WARN(1, "duplicate break on %p\n", fp);
3810 * We don't want the locks code to timeout the lease for us;
3811 * we'll remove it ourself if a delegation isn't returned
3814 fl->fl_break_time = 0;
3816 spin_lock(&fp->fi_lock);
3817 fp->fi_had_conflict = true;
3819 * If there are no delegations on the list, then return true
3820 * so that the lease code will go ahead and delete it.
3822 if (list_empty(&fp->fi_delegations))
3825 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3826 nfsd_break_one_deleg(dp);
3827 spin_unlock(&fp->fi_lock);
3832 nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
3833 struct list_head *dispose)
3836 return lease_modify(onlist, arg, dispose);
3841 static const struct lock_manager_operations nfsd_lease_mng_ops = {
3842 .lm_break = nfsd_break_deleg_cb,
3843 .lm_change = nfsd_change_deleg_cb,
3846 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3848 if (nfsd4_has_session(cstate))
3850 if (seqid == so->so_seqid - 1)
3851 return nfserr_replay_me;
3852 if (seqid == so->so_seqid)
3854 return nfserr_bad_seqid;
3857 static __be32 lookup_clientid(clientid_t *clid,
3858 struct nfsd4_compound_state *cstate,
3859 struct nfsd_net *nn)
3861 struct nfs4_client *found;
3864 found = cstate->clp;
3865 if (!same_clid(&found->cl_clientid, clid))
3866 return nfserr_stale_clientid;
3870 if (STALE_CLIENTID(clid, nn))
3871 return nfserr_stale_clientid;
3874 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3875 * cached already then we know this is for is for v4.0 and "sessions"
3878 WARN_ON_ONCE(cstate->session);
3879 spin_lock(&nn->client_lock);
3880 found = find_confirmed_client(clid, false, nn);
3882 spin_unlock(&nn->client_lock);
3883 return nfserr_expired;
3885 atomic_inc(&found->cl_refcount);
3886 spin_unlock(&nn->client_lock);
3888 /* Cache the nfs4_client in cstate! */
3889 cstate->clp = found;
3894 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
3895 struct nfsd4_open *open, struct nfsd_net *nn)
3897 clientid_t *clientid = &open->op_clientid;
3898 struct nfs4_client *clp = NULL;
3899 unsigned int strhashval;
3900 struct nfs4_openowner *oo = NULL;
3903 if (STALE_CLIENTID(&open->op_clientid, nn))
3904 return nfserr_stale_clientid;
3906 * In case we need it later, after we've already created the
3907 * file and don't want to risk a further failure:
3909 open->op_file = nfsd4_alloc_file();
3910 if (open->op_file == NULL)
3911 return nfserr_jukebox;
3913 status = lookup_clientid(clientid, cstate, nn);
3918 strhashval = ownerstr_hashval(&open->op_owner);
3919 oo = find_openstateowner_str(strhashval, open, clp);
3920 open->op_openowner = oo;
3924 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
3925 /* Replace unconfirmed owners without checking for replay. */
3926 release_openowner(oo);
3927 open->op_openowner = NULL;
3930 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3935 oo = alloc_init_open_stateowner(strhashval, open, cstate);
3937 return nfserr_jukebox;
3938 open->op_openowner = oo;
3940 open->op_stp = nfs4_alloc_open_stateid(clp);
3942 return nfserr_jukebox;
3944 if (nfsd4_has_session(cstate) &&
3945 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
3946 open->op_odstate = alloc_clnt_odstate(clp);
3947 if (!open->op_odstate)
3948 return nfserr_jukebox;
3954 static inline __be32
3955 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3957 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3958 return nfserr_openmode;
3963 static int share_access_to_flags(u32 share_access)
3965 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3968 static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
3970 struct nfs4_stid *ret;
3972 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
3975 return delegstateid(ret);
3978 static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3980 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3981 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3985 nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
3986 struct nfs4_delegation **dp)
3989 __be32 status = nfserr_bad_stateid;
3990 struct nfs4_delegation *deleg;
3992 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3995 flags = share_access_to_flags(open->op_share_access);
3996 status = nfs4_check_delegmode(deleg, flags);
3998 nfs4_put_stid(&deleg->dl_stid);
4003 if (!nfsd4_is_deleg_cur(open))
4007 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
4011 static inline int nfs4_access_to_access(u32 nfs4_access)
4015 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
4016 flags |= NFSD_MAY_READ;
4017 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
4018 flags |= NFSD_MAY_WRITE;
4022 static inline __be32
4023 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
4024 struct nfsd4_open *open)
4026 struct iattr iattr = {
4027 .ia_valid = ATTR_SIZE,
4030 if (!open->op_truncate)
4032 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
4033 return nfserr_inval;
4034 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
4037 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
4038 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
4039 struct nfsd4_open *open)
4041 struct file *filp = NULL;
4043 int oflag = nfs4_access_to_omode(open->op_share_access);
4044 int access = nfs4_access_to_access(open->op_share_access);
4045 unsigned char old_access_bmap, old_deny_bmap;
4047 spin_lock(&fp->fi_lock);
4050 * Are we trying to set a deny mode that would conflict with
4053 status = nfs4_file_check_deny(fp, open->op_share_deny);
4054 if (status != nfs_ok) {
4055 spin_unlock(&fp->fi_lock);
4059 /* set access to the file */
4060 status = nfs4_file_get_access(fp, open->op_share_access);
4061 if (status != nfs_ok) {
4062 spin_unlock(&fp->fi_lock);
4066 /* Set access bits in stateid */
4067 old_access_bmap = stp->st_access_bmap;
4068 set_access(open->op_share_access, stp);
4070 /* Set new deny mask */
4071 old_deny_bmap = stp->st_deny_bmap;
4072 set_deny(open->op_share_deny, stp);
4073 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4075 if (!fp->fi_fds[oflag]) {
4076 spin_unlock(&fp->fi_lock);
4077 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
4079 goto out_put_access;
4080 spin_lock(&fp->fi_lock);
4081 if (!fp->fi_fds[oflag]) {
4082 fp->fi_fds[oflag] = filp;
4086 spin_unlock(&fp->fi_lock);
4090 status = nfsd4_truncate(rqstp, cur_fh, open);
4092 goto out_put_access;
4096 stp->st_access_bmap = old_access_bmap;
4097 nfs4_file_put_access(fp, open->op_share_access);
4098 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
4103 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)
4106 unsigned char old_deny_bmap = stp->st_deny_bmap;
4108 if (!test_access(open->op_share_access, stp))
4109 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
4111 /* test and set deny mode */
4112 spin_lock(&fp->fi_lock);
4113 status = nfs4_file_check_deny(fp, open->op_share_deny);
4114 if (status == nfs_ok) {
4115 set_deny(open->op_share_deny, stp);
4116 fp->fi_share_deny |=
4117 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4119 spin_unlock(&fp->fi_lock);
4121 if (status != nfs_ok)
4124 status = nfsd4_truncate(rqstp, cur_fh, open);
4125 if (status != nfs_ok)
4126 reset_union_bmap_deny(old_deny_bmap, stp);
4130 /* Should we give out recallable state?: */
4131 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
4133 if (clp->cl_cb_state == NFSD4_CB_UP)
4136 * In the sessions case, since we don't have to establish a
4137 * separate connection for callbacks, we assume it's OK
4138 * until we hear otherwise:
4140 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
4143 static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
4145 struct file_lock *fl;
4147 fl = locks_alloc_lock();
4150 fl->fl_lmops = &nfsd_lease_mng_ops;
4151 fl->fl_flags = FL_DELEG;
4152 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
4153 fl->fl_end = OFFSET_MAX;
4154 fl->fl_owner = (fl_owner_t)fp;
4155 fl->fl_pid = current->tgid;
4160 * nfs4_setlease - Obtain a delegation by requesting lease from vfs layer
4161 * @dp: a pointer to the nfs4_delegation we're adding.
4164 * On success: Return code will be 0 on success.
4166 * On error: -EAGAIN if there was an existing delegation.
4167 * nonzero if there is an error in other cases.
4171 static int nfs4_setlease(struct nfs4_delegation *dp)
4173 struct nfs4_file *fp = dp->dl_stid.sc_file;
4174 struct file_lock *fl;
4178 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
4181 filp = find_readable_file(fp);
4183 /* We should always have a readable file here */
4185 locks_free_lock(fl);
4189 status = vfs_setlease(filp, fl->fl_type, &fl, NULL);
4191 locks_free_lock(fl);
4194 spin_lock(&state_lock);
4195 spin_lock(&fp->fi_lock);
4196 /* Did the lease get broken before we took the lock? */
4198 if (fp->fi_had_conflict)
4201 if (fp->fi_deleg_file) {
4202 status = hash_delegation_locked(dp, fp);
4205 fp->fi_deleg_file = filp;
4206 fp->fi_delegees = 0;
4207 status = hash_delegation_locked(dp, fp);
4208 spin_unlock(&fp->fi_lock);
4209 spin_unlock(&state_lock);
4211 /* Should never happen, this is a new fi_deleg_file */
4217 spin_unlock(&fp->fi_lock);
4218 spin_unlock(&state_lock);
4224 static struct nfs4_delegation *
4225 nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
4226 struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate)
4229 struct nfs4_delegation *dp;
4231 if (fp->fi_had_conflict)
4232 return ERR_PTR(-EAGAIN);
4234 spin_lock(&state_lock);
4235 spin_lock(&fp->fi_lock);
4236 status = nfs4_get_existing_delegation(clp, fp);
4237 spin_unlock(&fp->fi_lock);
4238 spin_unlock(&state_lock);
4241 return ERR_PTR(status);
4243 dp = alloc_init_deleg(clp, fh, odstate);
4245 return ERR_PTR(-ENOMEM);
4248 spin_lock(&state_lock);
4249 spin_lock(&fp->fi_lock);
4250 dp->dl_stid.sc_file = fp;
4251 if (!fp->fi_deleg_file) {
4252 spin_unlock(&fp->fi_lock);
4253 spin_unlock(&state_lock);
4254 status = nfs4_setlease(dp);
4257 if (fp->fi_had_conflict) {
4261 status = hash_delegation_locked(dp, fp);
4263 spin_unlock(&fp->fi_lock);
4264 spin_unlock(&state_lock);
4267 put_clnt_odstate(dp->dl_clnt_odstate);
4268 nfs4_put_stid(&dp->dl_stid);
4269 return ERR_PTR(status);
4274 static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
4276 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4277 if (status == -EAGAIN)
4278 open->op_why_no_deleg = WND4_CONTENTION;
4280 open->op_why_no_deleg = WND4_RESOURCE;
4281 switch (open->op_deleg_want) {
4282 case NFS4_SHARE_WANT_READ_DELEG:
4283 case NFS4_SHARE_WANT_WRITE_DELEG:
4284 case NFS4_SHARE_WANT_ANY_DELEG:
4286 case NFS4_SHARE_WANT_CANCEL:
4287 open->op_why_no_deleg = WND4_CANCELLED;
4289 case NFS4_SHARE_WANT_NO_DELEG:
4296 * Attempt to hand out a delegation.
4298 * Note we don't support write delegations, and won't until the vfs has
4299 * proper support for them.
4302 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
4303 struct nfs4_ol_stateid *stp)
4305 struct nfs4_delegation *dp;
4306 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
4307 struct nfs4_client *clp = stp->st_stid.sc_client;
4311 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
4312 open->op_recall = 0;
4313 switch (open->op_claim_type) {
4314 case NFS4_OPEN_CLAIM_PREVIOUS:
4316 open->op_recall = 1;
4317 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
4320 case NFS4_OPEN_CLAIM_NULL:
4321 case NFS4_OPEN_CLAIM_FH:
4323 * Let's not give out any delegations till everyone's
4324 * had the chance to reclaim theirs, *and* until
4325 * NLM locks have all been reclaimed:
4327 if (locks_in_grace(clp->net))
4329 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
4332 * Also, if the file was opened for write or
4333 * create, there's a good chance the client's
4334 * about to write to it, resulting in an
4335 * immediate recall (since we don't support
4336 * write delegations):
4338 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
4340 if (open->op_create == NFS4_OPEN_CREATE)
4346 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate);
4350 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
4352 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
4353 STATEID_VAL(&dp->dl_stid.sc_stateid));
4354 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
4355 nfs4_put_stid(&dp->dl_stid);
4358 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
4359 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
4360 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
4361 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
4362 open->op_recall = 1;
4365 /* 4.1 client asking for a delegation? */
4366 if (open->op_deleg_want)
4367 nfsd4_open_deleg_none_ext(open, status);
4371 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
4372 struct nfs4_delegation *dp)
4374 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
4375 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4376 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4377 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
4378 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
4379 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4380 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4381 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
4383 /* Otherwise the client must be confused wanting a delegation
4384 * it already has, therefore we don't return
4385 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
4390 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
4392 struct nfsd4_compoundres *resp = rqstp->rq_resp;
4393 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
4394 struct nfs4_file *fp = NULL;
4395 struct nfs4_ol_stateid *stp = NULL;
4396 struct nfs4_delegation *dp = NULL;
4400 * Lookup file; if found, lookup stateid and check open request,
4401 * and check for delegations in the process of being recalled.
4402 * If not found, create the nfs4_file struct
4404 fp = find_or_add_file(open->op_file, ¤t_fh->fh_handle);
4405 if (fp != open->op_file) {
4406 status = nfs4_check_deleg(cl, open, &dp);
4409 spin_lock(&fp->fi_lock);
4410 stp = nfsd4_find_existing_open(fp, open);
4411 spin_unlock(&fp->fi_lock);
4413 open->op_file = NULL;
4414 status = nfserr_bad_stateid;
4415 if (nfsd4_is_deleg_cur(open))
4420 * OPEN the file, or upgrade an existing OPEN.
4421 * If truncate fails, the OPEN fails.
4424 /* Stateid was found, this is an OPEN upgrade */
4425 mutex_lock(&stp->st_mutex);
4426 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
4428 mutex_unlock(&stp->st_mutex);
4432 /* stp is returned locked. */
4433 stp = init_open_stateid(fp, open);
4434 /* See if we lost the race to some other thread */
4435 if (stp->st_access_bmap != 0) {
4436 status = nfs4_upgrade_open(rqstp, fp, current_fh,
4439 mutex_unlock(&stp->st_mutex);
4444 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4446 mutex_unlock(&stp->st_mutex);
4447 release_open_stateid(stp);
4451 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
4453 if (stp->st_clnt_odstate == open->op_odstate)
4454 open->op_odstate = NULL;
4457 nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
4458 mutex_unlock(&stp->st_mutex);
4460 if (nfsd4_has_session(&resp->cstate)) {
4461 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4462 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4463 open->op_why_no_deleg = WND4_NOT_WANTED;
4469 * Attempt to hand out a delegation. No error return, because the
4470 * OPEN succeeds even if we fail.
4472 nfs4_open_delegation(current_fh, open, stp);
4476 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
4477 STATEID_VAL(&stp->st_stid.sc_stateid));
4479 /* 4.1 client trying to upgrade/downgrade delegation? */
4480 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
4481 open->op_deleg_want)
4482 nfsd4_deleg_xgrade_none_ext(open, dp);
4486 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
4487 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
4489 * To finish the open response, we just need to set the rflags.
4491 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
4492 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
4493 !nfsd4_has_session(&resp->cstate))
4494 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
4496 nfs4_put_stid(&dp->dl_stid);
4498 nfs4_put_stid(&stp->st_stid);
4503 void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
4504 struct nfsd4_open *open)
4506 if (open->op_openowner) {
4507 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
4509 nfsd4_cstate_assign_replay(cstate, so);
4510 nfs4_put_stateowner(so);
4513 kmem_cache_free(file_slab, open->op_file);
4515 nfs4_put_stid(&open->op_stp->st_stid);
4516 if (open->op_odstate)
4517 kmem_cache_free(odstate_slab, open->op_odstate);
4521 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4524 struct nfs4_client *clp;
4526 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4528 dprintk("process_renew(%08x/%08x): starting\n",
4529 clid->cl_boot, clid->cl_id);
4530 status = lookup_clientid(clid, cstate, nn);
4534 status = nfserr_cb_path_down;
4535 if (!list_empty(&clp->cl_delegations)
4536 && clp->cl_cb_state != NFSD4_CB_UP)
4544 nfsd4_end_grace(struct nfsd_net *nn)
4546 /* do nothing if grace period already ended */
4547 if (nn->grace_ended)
4550 dprintk("NFSD: end of grace period\n");
4551 nn->grace_ended = true;
4553 * If the server goes down again right now, an NFSv4
4554 * client will still be allowed to reclaim after it comes back up,
4555 * even if it hasn't yet had a chance to reclaim state this time.
4558 nfsd4_record_grace_done(nn);
4560 * At this point, NFSv4 clients can still reclaim. But if the
4561 * server crashes, any that have not yet reclaimed will be out
4562 * of luck on the next boot.
4564 * (NFSv4.1+ clients are considered to have reclaimed once they
4565 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
4566 * have reclaimed after their first OPEN.)
4568 locks_end_grace(&nn->nfsd4_manager);
4570 * At this point, and once lockd and/or any other containers
4571 * exit their grace period, further reclaims will fail and
4572 * regular locking can resume.
4577 nfs4_laundromat(struct nfsd_net *nn)
4579 struct nfs4_client *clp;
4580 struct nfs4_openowner *oo;
4581 struct nfs4_delegation *dp;
4582 struct nfs4_ol_stateid *stp;
4583 struct list_head *pos, *next, reaplist;
4584 time_t cutoff = get_seconds() - nn->nfsd4_lease;
4585 time_t t, new_timeo = nn->nfsd4_lease;
4587 dprintk("NFSD: laundromat service - starting\n");
4588 nfsd4_end_grace(nn);
4589 INIT_LIST_HEAD(&reaplist);
4590 spin_lock(&nn->client_lock);
4591 list_for_each_safe(pos, next, &nn->client_lru) {
4592 clp = list_entry(pos, struct nfs4_client, cl_lru);
4593 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4594 t = clp->cl_time - cutoff;
4595 new_timeo = min(new_timeo, t);
4598 if (mark_client_expired_locked(clp)) {
4599 dprintk("NFSD: client in use (clientid %08x)\n",
4600 clp->cl_clientid.cl_id);
4603 list_add(&clp->cl_lru, &reaplist);
4605 spin_unlock(&nn->client_lock);
4606 list_for_each_safe(pos, next, &reaplist) {
4607 clp = list_entry(pos, struct nfs4_client, cl_lru);
4608 dprintk("NFSD: purging unused client (clientid %08x)\n",
4609 clp->cl_clientid.cl_id);
4610 list_del_init(&clp->cl_lru);
4613 spin_lock(&state_lock);
4614 list_for_each_safe(pos, next, &nn->del_recall_lru) {
4615 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4616 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
4617 t = dp->dl_time - cutoff;
4618 new_timeo = min(new_timeo, t);
4621 WARN_ON(!unhash_delegation_locked(dp));
4622 list_add(&dp->dl_recall_lru, &reaplist);
4624 spin_unlock(&state_lock);
4625 while (!list_empty(&reaplist)) {
4626 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4628 list_del_init(&dp->dl_recall_lru);
4629 revoke_delegation(dp);
4632 spin_lock(&nn->client_lock);
4633 while (!list_empty(&nn->close_lru)) {
4634 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
4636 if (time_after((unsigned long)oo->oo_time,
4637 (unsigned long)cutoff)) {
4638 t = oo->oo_time - cutoff;
4639 new_timeo = min(new_timeo, t);
4642 list_del_init(&oo->oo_close_lru);
4643 stp = oo->oo_last_closed_stid;
4644 oo->oo_last_closed_stid = NULL;
4645 spin_unlock(&nn->client_lock);
4646 nfs4_put_stid(&stp->st_stid);
4647 spin_lock(&nn->client_lock);
4649 spin_unlock(&nn->client_lock);
4651 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
4655 static struct workqueue_struct *laundry_wq;
4656 static void laundromat_main(struct work_struct *);
4659 laundromat_main(struct work_struct *laundry)
4662 struct delayed_work *dwork = to_delayed_work(laundry);
4663 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4666 t = nfs4_laundromat(nn);
4667 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
4668 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
4671 static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
4673 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
4674 return nfserr_bad_stateid;
4679 access_permit_read(struct nfs4_ol_stateid *stp)
4681 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4682 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4683 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
4687 access_permit_write(struct nfs4_ol_stateid *stp)
4689 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4690 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
4694 __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
4696 __be32 status = nfserr_openmode;
4698 /* For lock stateid's, we test the parent open, not the lock: */
4699 if (stp->st_openstp)
4700 stp = stp->st_openstp;
4701 if ((flags & WR_STATE) && !access_permit_write(stp))
4703 if ((flags & RD_STATE) && !access_permit_read(stp))
4710 static inline __be32
4711 check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
4713 if (ONE_STATEID(stateid) && (flags & RD_STATE))
4715 else if (opens_in_grace(net)) {
4716 /* Answer in remaining cases depends on existence of
4717 * conflicting state; so we must wait out the grace period. */
4718 return nfserr_grace;
4719 } else if (flags & WR_STATE)
4720 return nfs4_share_conflict(current_fh,
4721 NFS4_SHARE_DENY_WRITE);
4722 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4723 return nfs4_share_conflict(current_fh,
4724 NFS4_SHARE_DENY_READ);
4728 * Allow READ/WRITE during grace period on recovered state only for files
4729 * that are not able to provide mandatory locking.
4732 grace_disallows_io(struct net *net, struct inode *inode)
4734 return opens_in_grace(net) && mandatory_lock(inode);
4737 static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
4740 * When sessions are used the stateid generation number is ignored
4743 if (has_session && in->si_generation == 0)
4746 if (in->si_generation == ref->si_generation)
4749 /* If the client sends us a stateid from the future, it's buggy: */
4750 if (nfsd4_stateid_generation_after(in, ref))
4751 return nfserr_bad_stateid;
4753 * However, we could see a stateid from the past, even from a
4754 * non-buggy client. For example, if the client sends a lock
4755 * while some IO is outstanding, the lock may bump si_generation
4756 * while the IO is still in flight. The client could avoid that
4757 * situation by waiting for responses on all the IO requests,
4758 * but better performance may result in retrying IO that
4759 * receives an old_stateid error if requests are rarely
4760 * reordered in flight:
4762 return nfserr_old_stateid;
4765 static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
4767 if (ols->st_stateowner->so_is_open_owner &&
4768 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
4769 return nfserr_bad_stateid;
4773 static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
4775 struct nfs4_stid *s;
4776 __be32 status = nfserr_bad_stateid;
4778 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4780 /* Client debugging aid. */
4781 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4782 char addr_str[INET6_ADDRSTRLEN];
4783 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4785 pr_warn_ratelimited("NFSD: client %s testing state ID "
4786 "with incorrect client ID\n", addr_str);
4789 spin_lock(&cl->cl_lock);
4790 s = find_stateid_locked(cl, stateid);
4793 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
4796 switch (s->sc_type) {
4797 case NFS4_DELEG_STID:
4800 case NFS4_REVOKED_DELEG_STID:
4801 status = nfserr_deleg_revoked;
4803 case NFS4_OPEN_STID:
4804 case NFS4_LOCK_STID:
4805 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
4808 printk("unknown stateid type %x\n", s->sc_type);
4810 case NFS4_CLOSED_STID:
4811 case NFS4_CLOSED_DELEG_STID:
4812 status = nfserr_bad_stateid;
4815 spin_unlock(&cl->cl_lock);
4820 nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4821 stateid_t *stateid, unsigned char typemask,
4822 struct nfs4_stid **s, struct nfsd_net *nn)
4826 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4827 return nfserr_bad_stateid;
4828 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
4829 if (status == nfserr_stale_clientid) {
4830 if (cstate->session)
4831 return nfserr_bad_stateid;
4832 return nfserr_stale_stateid;
4836 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
4838 return nfserr_bad_stateid;
4842 static struct file *
4843 nfs4_find_file(struct nfs4_stid *s, int flags)
4848 switch (s->sc_type) {
4849 case NFS4_DELEG_STID:
4850 if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
4852 return get_file(s->sc_file->fi_deleg_file);
4853 case NFS4_OPEN_STID:
4854 case NFS4_LOCK_STID:
4855 if (flags & RD_STATE)
4856 return find_readable_file(s->sc_file);
4858 return find_writeable_file(s->sc_file);
4866 nfs4_check_olstateid(struct svc_fh *fhp, struct nfs4_ol_stateid *ols, int flags)
4870 status = nfsd4_check_openowner_confirmed(ols);
4873 return nfs4_check_openmode(ols, flags);
4877 nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
4878 struct file **filpp, bool *tmp_file, int flags)
4880 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
4884 file = nfs4_find_file(s, flags);
4886 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
4887 acc | NFSD_MAY_OWNER_OVERRIDE);
4895 status = nfsd_open(rqstp, fhp, S_IFREG, acc, filpp);
4907 * Checks for stateid operations
4910 nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
4911 struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
4912 stateid_t *stateid, int flags, struct file **filpp, bool *tmp_file)
4914 struct inode *ino = d_inode(fhp->fh_dentry);
4915 struct net *net = SVC_NET(rqstp);
4916 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4917 struct nfs4_stid *s = NULL;
4925 if (grace_disallows_io(net, ino))
4926 return nfserr_grace;
4928 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
4929 status = check_special_stateids(net, fhp, stateid, flags);
4933 status = nfsd4_lookup_stateid(cstate, stateid,
4934 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
4938 status = check_stateid_generation(stateid, &s->sc_stateid,
4939 nfsd4_has_session(cstate));
4943 switch (s->sc_type) {
4944 case NFS4_DELEG_STID:
4945 status = nfs4_check_delegmode(delegstateid(s), flags);
4947 case NFS4_OPEN_STID:
4948 case NFS4_LOCK_STID:
4949 status = nfs4_check_olstateid(fhp, openlockstateid(s), flags);
4952 status = nfserr_bad_stateid;
4957 status = nfs4_check_fh(fhp, s);
4960 if (!status && filpp)
4961 status = nfs4_check_file(rqstp, fhp, s, filpp, tmp_file, flags);
4969 * Test if the stateid is valid
4972 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4973 struct nfsd4_test_stateid *test_stateid)
4975 struct nfsd4_test_stateid_id *stateid;
4976 struct nfs4_client *cl = cstate->session->se_client;
4978 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
4979 stateid->ts_id_status =
4980 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
4986 nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
4988 struct nfs4_ol_stateid *stp = openlockstateid(s);
4991 mutex_lock(&stp->st_mutex);
4993 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4997 ret = nfserr_locks_held;
4998 if (check_for_locks(stp->st_stid.sc_file,
4999 lockowner(stp->st_stateowner)))
5002 release_lock_stateid(stp);
5006 mutex_unlock(&stp->st_mutex);
5012 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5013 struct nfsd4_free_stateid *free_stateid)
5015 stateid_t *stateid = &free_stateid->fr_stateid;
5016 struct nfs4_stid *s;
5017 struct nfs4_delegation *dp;
5018 struct nfs4_client *cl = cstate->session->se_client;
5019 __be32 ret = nfserr_bad_stateid;
5021 spin_lock(&cl->cl_lock);
5022 s = find_stateid_locked(cl, stateid);
5025 switch (s->sc_type) {
5026 case NFS4_DELEG_STID:
5027 ret = nfserr_locks_held;
5029 case NFS4_OPEN_STID:
5030 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5033 ret = nfserr_locks_held;
5035 case NFS4_LOCK_STID:
5036 atomic_inc(&s->sc_count);
5037 spin_unlock(&cl->cl_lock);
5038 ret = nfsd4_free_lock_stateid(stateid, s);
5040 case NFS4_REVOKED_DELEG_STID:
5041 dp = delegstateid(s);
5042 list_del_init(&dp->dl_recall_lru);
5043 spin_unlock(&cl->cl_lock);
5047 /* Default falls through and returns nfserr_bad_stateid */
5050 spin_unlock(&cl->cl_lock);
5058 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
5059 RD_STATE : WR_STATE;
5062 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
5064 struct svc_fh *current_fh = &cstate->current_fh;
5065 struct nfs4_stateowner *sop = stp->st_stateowner;
5068 status = nfsd4_check_seqid(cstate, sop, seqid);
5071 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
5072 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
5074 * "Closed" stateid's exist *only* to return
5075 * nfserr_replay_me from the previous step, and
5076 * revoked delegations are kept only for free_stateid.
5078 return nfserr_bad_stateid;
5079 mutex_lock(&stp->st_mutex);
5080 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
5081 if (status == nfs_ok)
5082 status = nfs4_check_fh(current_fh, &stp->st_stid);
5083 if (status != nfs_ok)
5084 mutex_unlock(&stp->st_mutex);
5089 * Checks for sequence id mutating operations.
5092 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
5093 stateid_t *stateid, char typemask,
5094 struct nfs4_ol_stateid **stpp,
5095 struct nfsd_net *nn)
5098 struct nfs4_stid *s;
5099 struct nfs4_ol_stateid *stp = NULL;
5101 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
5102 seqid, STATEID_VAL(stateid));
5105 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
5108 stp = openlockstateid(s);
5109 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
5111 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
5115 nfs4_put_stid(&stp->st_stid);
5119 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
5120 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
5123 struct nfs4_openowner *oo;
5124 struct nfs4_ol_stateid *stp;
5126 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
5127 NFS4_OPEN_STID, &stp, nn);
5130 oo = openowner(stp->st_stateowner);
5131 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
5132 mutex_unlock(&stp->st_mutex);
5133 nfs4_put_stid(&stp->st_stid);
5134 return nfserr_bad_stateid;
5141 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5142 struct nfsd4_open_confirm *oc)
5145 struct nfs4_openowner *oo;
5146 struct nfs4_ol_stateid *stp;
5147 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5149 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
5150 cstate->current_fh.fh_dentry);
5152 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
5156 status = nfs4_preprocess_seqid_op(cstate,
5157 oc->oc_seqid, &oc->oc_req_stateid,
5158 NFS4_OPEN_STID, &stp, nn);
5161 oo = openowner(stp->st_stateowner);
5162 status = nfserr_bad_stateid;
5163 if (oo->oo_flags & NFS4_OO_CONFIRMED) {
5164 mutex_unlock(&stp->st_mutex);
5167 oo->oo_flags |= NFS4_OO_CONFIRMED;
5168 nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
5169 mutex_unlock(&stp->st_mutex);
5170 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
5171 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
5173 nfsd4_client_record_create(oo->oo_owner.so_client);
5176 nfs4_put_stid(&stp->st_stid);
5178 nfsd4_bump_seqid(cstate, status);
5182 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
5184 if (!test_access(access, stp))
5186 nfs4_file_put_access(stp->st_stid.sc_file, access);
5187 clear_access(access, stp);
5190 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
5192 switch (to_access) {
5193 case NFS4_SHARE_ACCESS_READ:
5194 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
5195 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
5197 case NFS4_SHARE_ACCESS_WRITE:
5198 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
5199 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
5201 case NFS4_SHARE_ACCESS_BOTH:
5209 nfsd4_open_downgrade(struct svc_rqst *rqstp,
5210 struct nfsd4_compound_state *cstate,
5211 struct nfsd4_open_downgrade *od)
5214 struct nfs4_ol_stateid *stp;
5215 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5217 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
5218 cstate->current_fh.fh_dentry);
5220 /* We don't yet support WANT bits: */
5221 if (od->od_deleg_want)
5222 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
5225 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
5226 &od->od_stateid, &stp, nn);
5229 status = nfserr_inval;
5230 if (!test_access(od->od_share_access, stp)) {
5231 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
5232 stp->st_access_bmap, od->od_share_access);
5235 if (!test_deny(od->od_share_deny, stp)) {
5236 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
5237 stp->st_deny_bmap, od->od_share_deny);
5240 nfs4_stateid_downgrade(stp, od->od_share_access);
5241 reset_union_bmap_deny(od->od_share_deny, stp);
5242 nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
5245 mutex_unlock(&stp->st_mutex);
5246 nfs4_put_stid(&stp->st_stid);
5248 nfsd4_bump_seqid(cstate, status);
5252 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
5254 struct nfs4_client *clp = s->st_stid.sc_client;
5256 LIST_HEAD(reaplist);
5258 s->st_stid.sc_type = NFS4_CLOSED_STID;
5259 spin_lock(&clp->cl_lock);
5260 unhashed = unhash_open_stateid(s, &reaplist);
5262 if (clp->cl_minorversion) {
5264 put_ol_stateid_locked(s, &reaplist);
5265 spin_unlock(&clp->cl_lock);
5266 free_ol_stateid_reaplist(&reaplist);
5268 spin_unlock(&clp->cl_lock);
5269 free_ol_stateid_reaplist(&reaplist);
5271 move_to_close_lru(s, clp->net);
5276 * nfs4_unlock_state() called after encode
5279 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5280 struct nfsd4_close *close)
5283 struct nfs4_ol_stateid *stp;
5284 struct net *net = SVC_NET(rqstp);
5285 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5287 dprintk("NFSD: nfsd4_close on file %pd\n",
5288 cstate->current_fh.fh_dentry);
5290 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
5292 NFS4_OPEN_STID|NFS4_CLOSED_STID,
5294 nfsd4_bump_seqid(cstate, status);
5297 nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
5298 mutex_unlock(&stp->st_mutex);
5300 nfsd4_close_open_stateid(stp);
5302 /* put reference from nfs4_preprocess_seqid_op */
5303 nfs4_put_stid(&stp->st_stid);
5309 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5310 struct nfsd4_delegreturn *dr)
5312 struct nfs4_delegation *dp;
5313 stateid_t *stateid = &dr->dr_stateid;
5314 struct nfs4_stid *s;
5316 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5318 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
5321 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
5324 dp = delegstateid(s);
5325 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
5329 destroy_delegation(dp);
5331 nfs4_put_stid(&dp->dl_stid);
5337 end_offset(u64 start, u64 len)
5342 return end >= start ? end: NFS4_MAX_UINT64;
5345 /* last octet in a range */
5347 last_byte_offset(u64 start, u64 len)
5353 return end > start ? end - 1: NFS4_MAX_UINT64;
5357 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
5358 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
5359 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
5360 * locking, this prevents us from being completely protocol-compliant. The
5361 * real solution to this problem is to start using unsigned file offsets in
5362 * the VFS, but this is a very deep change!
5365 nfs4_transform_lock_offset(struct file_lock *lock)
5367 if (lock->fl_start < 0)
5368 lock->fl_start = OFFSET_MAX;
5369 if (lock->fl_end < 0)
5370 lock->fl_end = OFFSET_MAX;
5374 nfsd4_fl_get_owner(fl_owner_t owner)
5376 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
5378 nfs4_get_stateowner(&lo->lo_owner);
5383 nfsd4_fl_put_owner(fl_owner_t owner)
5385 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
5388 nfs4_put_stateowner(&lo->lo_owner);
5392 nfsd4_lm_notify(struct file_lock *fl)
5394 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
5395 struct net *net = lo->lo_owner.so_client->net;
5396 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5397 struct nfsd4_blocked_lock *nbl = container_of(fl,
5398 struct nfsd4_blocked_lock, nbl_lock);
5401 spin_lock(&nn->client_lock);
5402 if (!list_empty(&nbl->nbl_list)) {
5403 list_del_init(&nbl->nbl_list);
5406 spin_unlock(&nn->client_lock);
5409 nfsd4_run_cb(&nbl->nbl_cb);
5412 static const struct lock_manager_operations nfsd_posix_mng_ops = {
5413 .lm_notify = nfsd4_lm_notify,
5414 .lm_get_owner = nfsd4_fl_get_owner,
5415 .lm_put_owner = nfsd4_fl_put_owner,
5419 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
5421 struct nfs4_lockowner *lo;
5423 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
5424 lo = (struct nfs4_lockowner *) fl->fl_owner;
5425 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
5426 lo->lo_owner.so_owner.len, GFP_KERNEL);
5427 if (!deny->ld_owner.data)
5428 /* We just don't care that much */
5430 deny->ld_owner.len = lo->lo_owner.so_owner.len;
5431 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
5434 deny->ld_owner.len = 0;
5435 deny->ld_owner.data = NULL;
5436 deny->ld_clientid.cl_boot = 0;
5437 deny->ld_clientid.cl_id = 0;
5439 deny->ld_start = fl->fl_start;
5440 deny->ld_length = NFS4_MAX_UINT64;
5441 if (fl->fl_end != NFS4_MAX_UINT64)
5442 deny->ld_length = fl->fl_end - fl->fl_start + 1;
5443 deny->ld_type = NFS4_READ_LT;
5444 if (fl->fl_type != F_RDLCK)
5445 deny->ld_type = NFS4_WRITE_LT;
5448 static struct nfs4_lockowner *
5449 find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
5451 unsigned int strhashval = ownerstr_hashval(owner);
5452 struct nfs4_stateowner *so;
5454 lockdep_assert_held(&clp->cl_lock);
5456 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
5458 if (so->so_is_open_owner)
5460 if (same_owner_str(so, owner))
5461 return lockowner(nfs4_get_stateowner(so));
5466 static struct nfs4_lockowner *
5467 find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
5469 struct nfs4_lockowner *lo;
5471 spin_lock(&clp->cl_lock);
5472 lo = find_lockowner_str_locked(clp, owner);
5473 spin_unlock(&clp->cl_lock);
5477 static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
5479 unhash_lockowner_locked(lockowner(sop));
5482 static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
5484 struct nfs4_lockowner *lo = lockowner(sop);
5486 kmem_cache_free(lockowner_slab, lo);
5489 static const struct nfs4_stateowner_operations lockowner_ops = {
5490 .so_unhash = nfs4_unhash_lockowner,
5491 .so_free = nfs4_free_lockowner,
5495 * Alloc a lock owner structure.
5496 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
5499 * strhashval = ownerstr_hashval
5501 static struct nfs4_lockowner *
5502 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
5503 struct nfs4_ol_stateid *open_stp,
5504 struct nfsd4_lock *lock)
5506 struct nfs4_lockowner *lo, *ret;
5508 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
5511 INIT_LIST_HEAD(&lo->lo_blocked);
5512 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
5513 lo->lo_owner.so_is_open_owner = 0;
5514 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
5515 lo->lo_owner.so_ops = &lockowner_ops;
5516 spin_lock(&clp->cl_lock);
5517 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
5519 list_add(&lo->lo_owner.so_strhash,
5520 &clp->cl_ownerstr_hashtbl[strhashval]);
5523 nfs4_free_stateowner(&lo->lo_owner);
5525 spin_unlock(&clp->cl_lock);
5530 init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
5531 struct nfs4_file *fp, struct inode *inode,
5532 struct nfs4_ol_stateid *open_stp)
5534 struct nfs4_client *clp = lo->lo_owner.so_client;
5536 lockdep_assert_held(&clp->cl_lock);
5538 atomic_inc(&stp->st_stid.sc_count);
5539 stp->st_stid.sc_type = NFS4_LOCK_STID;
5540 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
5542 stp->st_stid.sc_file = fp;
5543 stp->st_stid.sc_free = nfs4_free_lock_stateid;
5544 stp->st_access_bmap = 0;
5545 stp->st_deny_bmap = open_stp->st_deny_bmap;
5546 stp->st_openstp = open_stp;
5547 mutex_init(&stp->st_mutex);
5548 list_add(&stp->st_locks, &open_stp->st_locks);
5549 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
5550 spin_lock(&fp->fi_lock);
5551 list_add(&stp->st_perfile, &fp->fi_stateids);
5552 spin_unlock(&fp->fi_lock);
5555 static struct nfs4_ol_stateid *
5556 find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5558 struct nfs4_ol_stateid *lst;
5559 struct nfs4_client *clp = lo->lo_owner.so_client;
5561 lockdep_assert_held(&clp->cl_lock);
5563 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
5564 if (lst->st_stid.sc_file == fp) {
5565 atomic_inc(&lst->st_stid.sc_count);
5572 static struct nfs4_ol_stateid *
5573 find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5574 struct inode *inode, struct nfs4_ol_stateid *ost,
5577 struct nfs4_stid *ns = NULL;
5578 struct nfs4_ol_stateid *lst;
5579 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5580 struct nfs4_client *clp = oo->oo_owner.so_client;
5582 spin_lock(&clp->cl_lock);
5583 lst = find_lock_stateid(lo, fi);
5585 spin_unlock(&clp->cl_lock);
5586 ns = nfs4_alloc_stid(clp, stateid_slab);
5590 spin_lock(&clp->cl_lock);
5591 lst = find_lock_stateid(lo, fi);
5593 lst = openlockstateid(ns);
5594 init_lock_stateid(lst, lo, fi, inode, ost);
5599 spin_unlock(&clp->cl_lock);
5606 check_lock_length(u64 offset, u64 length)
5608 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
5609 (length > ~offset)));
5612 static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
5614 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
5616 lockdep_assert_held(&fp->fi_lock);
5618 if (test_access(access, lock_stp))
5620 __nfs4_file_get_access(fp, access);
5621 set_access(access, lock_stp);
5625 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5626 struct nfs4_ol_stateid *ost,
5627 struct nfsd4_lock *lock,
5628 struct nfs4_ol_stateid **plst, bool *new)
5631 struct nfs4_file *fi = ost->st_stid.sc_file;
5632 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5633 struct nfs4_client *cl = oo->oo_owner.so_client;
5634 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
5635 struct nfs4_lockowner *lo;
5636 struct nfs4_ol_stateid *lst;
5637 unsigned int strhashval;
5640 lo = find_lockowner_str(cl, &lock->lk_new_owner);
5642 strhashval = ownerstr_hashval(&lock->lk_new_owner);
5643 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5645 return nfserr_jukebox;
5647 /* with an existing lockowner, seqids must be the same */
5648 status = nfserr_bad_seqid;
5649 if (!cstate->minorversion &&
5650 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
5655 lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
5657 status = nfserr_jukebox;
5661 mutex_lock(&lst->st_mutex);
5663 /* See if it's still hashed to avoid race with FREE_STATEID */
5664 spin_lock(&cl->cl_lock);
5665 hashed = !list_empty(&lst->st_perfile);
5666 spin_unlock(&cl->cl_lock);
5669 mutex_unlock(&lst->st_mutex);
5670 nfs4_put_stid(&lst->st_stid);
5676 nfs4_put_stateowner(&lo->lo_owner);
5684 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5685 struct nfsd4_lock *lock)
5687 struct nfs4_openowner *open_sop = NULL;
5688 struct nfs4_lockowner *lock_sop = NULL;
5689 struct nfs4_ol_stateid *lock_stp = NULL;
5690 struct nfs4_ol_stateid *open_stp = NULL;
5691 struct nfs4_file *fp;
5692 struct file *filp = NULL;
5693 struct nfsd4_blocked_lock *nbl = NULL;
5694 struct file_lock *file_lock = NULL;
5695 struct file_lock *conflock = NULL;
5700 unsigned char fl_type;
5701 unsigned int fl_flags = FL_POSIX;
5702 struct net *net = SVC_NET(rqstp);
5703 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5705 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5706 (long long) lock->lk_offset,
5707 (long long) lock->lk_length);
5709 if (check_lock_length(lock->lk_offset, lock->lk_length))
5710 return nfserr_inval;
5712 if ((status = fh_verify(rqstp, &cstate->current_fh,
5713 S_IFREG, NFSD_MAY_LOCK))) {
5714 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5718 if (lock->lk_is_new) {
5719 if (nfsd4_has_session(cstate))
5720 /* See rfc 5661 18.10.3: given clientid is ignored: */
5721 memcpy(&lock->lk_new_clientid,
5722 &cstate->session->se_client->cl_clientid,
5723 sizeof(clientid_t));
5725 status = nfserr_stale_clientid;
5726 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
5729 /* validate and update open stateid and open seqid */
5730 status = nfs4_preprocess_confirmed_seqid_op(cstate,
5731 lock->lk_new_open_seqid,
5732 &lock->lk_new_open_stateid,
5736 mutex_unlock(&open_stp->st_mutex);
5737 open_sop = openowner(open_stp->st_stateowner);
5738 status = nfserr_bad_stateid;
5739 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
5740 &lock->lk_new_clientid))
5742 status = lookup_or_create_lock_state(cstate, open_stp, lock,
5745 status = nfs4_preprocess_seqid_op(cstate,
5746 lock->lk_old_lock_seqid,
5747 &lock->lk_old_lock_stateid,
5748 NFS4_LOCK_STID, &lock_stp, nn);
5752 lock_sop = lockowner(lock_stp->st_stateowner);
5754 lkflg = setlkflg(lock->lk_type);
5755 status = nfs4_check_openmode(lock_stp, lkflg);
5759 status = nfserr_grace;
5760 if (locks_in_grace(net) && !lock->lk_reclaim)
5762 status = nfserr_no_grace;
5763 if (!locks_in_grace(net) && lock->lk_reclaim)
5766 fp = lock_stp->st_stid.sc_file;
5767 switch (lock->lk_type) {
5769 if (nfsd4_has_session(cstate))
5770 fl_flags |= FL_SLEEP;
5773 spin_lock(&fp->fi_lock);
5774 filp = find_readable_file_locked(fp);
5776 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
5777 spin_unlock(&fp->fi_lock);
5780 case NFS4_WRITEW_LT:
5781 if (nfsd4_has_session(cstate))
5782 fl_flags |= FL_SLEEP;
5785 spin_lock(&fp->fi_lock);
5786 filp = find_writeable_file_locked(fp);
5788 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
5789 spin_unlock(&fp->fi_lock);
5793 status = nfserr_inval;
5798 status = nfserr_openmode;
5802 nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
5804 dprintk("NFSD: %s: unable to allocate block!\n", __func__);
5805 status = nfserr_jukebox;
5809 file_lock = &nbl->nbl_lock;
5810 file_lock->fl_type = fl_type;
5811 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
5812 file_lock->fl_pid = current->tgid;
5813 file_lock->fl_file = filp;
5814 file_lock->fl_flags = fl_flags;
5815 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5816 file_lock->fl_start = lock->lk_offset;
5817 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5818 nfs4_transform_lock_offset(file_lock);
5820 conflock = locks_alloc_lock();
5822 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5823 status = nfserr_jukebox;
5827 if (fl_flags & FL_SLEEP) {
5828 spin_lock(&nn->client_lock);
5829 list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
5830 spin_unlock(&nn->client_lock);
5833 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
5835 case 0: /* success! */
5836 nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
5839 case FILE_LOCK_DEFERRED:
5842 case -EAGAIN: /* conflock holds conflicting lock */
5843 status = nfserr_denied;
5844 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
5845 nfs4_set_lock_denied(conflock, &lock->lk_denied);
5848 status = nfserr_deadlock;
5851 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
5852 status = nfserrno(err);
5857 /* dequeue it if we queued it before */
5858 if (fl_flags & FL_SLEEP) {
5859 spin_lock(&nn->client_lock);
5860 list_del_init(&nbl->nbl_list);
5861 spin_unlock(&nn->client_lock);
5863 free_blocked_lock(nbl);
5868 /* Bump seqid manually if the 4.0 replay owner is openowner */
5869 if (cstate->replay_owner &&
5870 cstate->replay_owner != &lock_sop->lo_owner &&
5871 seqid_mutating_err(ntohl(status)))
5872 lock_sop->lo_owner.so_seqid++;
5874 mutex_unlock(&lock_stp->st_mutex);
5877 * If this is a new, never-before-used stateid, and we are
5878 * returning an error, then just go ahead and release it.
5881 release_lock_stateid(lock_stp);
5883 nfs4_put_stid(&lock_stp->st_stid);
5886 nfs4_put_stid(&open_stp->st_stid);
5887 nfsd4_bump_seqid(cstate, status);
5889 locks_free_lock(conflock);
5894 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5895 * so we do a temporary open here just to get an open file to pass to
5896 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5899 static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
5902 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5904 err = nfserrno(vfs_test_lock(file, lock));
5914 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5915 struct nfsd4_lockt *lockt)
5917 struct file_lock *file_lock = NULL;
5918 struct nfs4_lockowner *lo = NULL;
5920 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5922 if (locks_in_grace(SVC_NET(rqstp)))
5923 return nfserr_grace;
5925 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5926 return nfserr_inval;
5928 if (!nfsd4_has_session(cstate)) {
5929 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
5934 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
5937 file_lock = locks_alloc_lock();
5939 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5940 status = nfserr_jukebox;
5944 switch (lockt->lt_type) {
5947 file_lock->fl_type = F_RDLCK;
5950 case NFS4_WRITEW_LT:
5951 file_lock->fl_type = F_WRLCK;
5954 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
5955 status = nfserr_inval;
5959 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
5961 file_lock->fl_owner = (fl_owner_t)lo;
5962 file_lock->fl_pid = current->tgid;
5963 file_lock->fl_flags = FL_POSIX;
5965 file_lock->fl_start = lockt->lt_offset;
5966 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
5968 nfs4_transform_lock_offset(file_lock);
5970 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
5974 if (file_lock->fl_type != F_UNLCK) {
5975 status = nfserr_denied;
5976 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
5980 nfs4_put_stateowner(&lo->lo_owner);
5982 locks_free_lock(file_lock);
5987 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5988 struct nfsd4_locku *locku)
5990 struct nfs4_ol_stateid *stp;
5991 struct file *filp = NULL;
5992 struct file_lock *file_lock = NULL;
5995 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5997 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5998 (long long) locku->lu_offset,
5999 (long long) locku->lu_length);
6001 if (check_lock_length(locku->lu_offset, locku->lu_length))
6002 return nfserr_inval;
6004 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
6005 &locku->lu_stateid, NFS4_LOCK_STID,
6009 filp = find_any_file(stp->st_stid.sc_file);
6011 status = nfserr_lock_range;
6014 file_lock = locks_alloc_lock();
6016 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6017 status = nfserr_jukebox;
6021 file_lock->fl_type = F_UNLCK;
6022 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
6023 file_lock->fl_pid = current->tgid;
6024 file_lock->fl_file = filp;
6025 file_lock->fl_flags = FL_POSIX;
6026 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6027 file_lock->fl_start = locku->lu_offset;
6029 file_lock->fl_end = last_byte_offset(locku->lu_offset,
6031 nfs4_transform_lock_offset(file_lock);
6033 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
6035 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
6038 nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
6042 mutex_unlock(&stp->st_mutex);
6043 nfs4_put_stid(&stp->st_stid);
6045 nfsd4_bump_seqid(cstate, status);
6047 locks_free_lock(file_lock);
6051 status = nfserrno(err);
6057 * true: locks held by lockowner
6058 * false: no locks held by lockowner
6061 check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
6063 struct file_lock *fl;
6065 struct file *filp = find_any_file(fp);
6066 struct inode *inode;
6067 struct file_lock_context *flctx;
6070 /* Any valid lock stateid should have some sort of access */
6075 inode = file_inode(filp);
6076 flctx = inode->i_flctx;
6078 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
6079 spin_lock(&flctx->flc_lock);
6080 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
6081 if (fl->fl_owner == (fl_owner_t)lowner) {
6086 spin_unlock(&flctx->flc_lock);
6093 nfsd4_release_lockowner(struct svc_rqst *rqstp,
6094 struct nfsd4_compound_state *cstate,
6095 struct nfsd4_release_lockowner *rlockowner)
6097 clientid_t *clid = &rlockowner->rl_clientid;
6098 struct nfs4_stateowner *sop;
6099 struct nfs4_lockowner *lo = NULL;
6100 struct nfs4_ol_stateid *stp;
6101 struct xdr_netobj *owner = &rlockowner->rl_owner;
6102 unsigned int hashval = ownerstr_hashval(owner);
6104 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6105 struct nfs4_client *clp;
6106 LIST_HEAD (reaplist);
6108 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
6109 clid->cl_boot, clid->cl_id);
6111 status = lookup_clientid(clid, cstate, nn);
6116 /* Find the matching lock stateowner */
6117 spin_lock(&clp->cl_lock);
6118 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
6121 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
6124 /* see if there are still any locks associated with it */
6125 lo = lockowner(sop);
6126 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
6127 if (check_for_locks(stp->st_stid.sc_file, lo)) {
6128 status = nfserr_locks_held;
6129 spin_unlock(&clp->cl_lock);
6134 nfs4_get_stateowner(sop);
6138 spin_unlock(&clp->cl_lock);
6142 unhash_lockowner_locked(lo);
6143 while (!list_empty(&lo->lo_owner.so_stateids)) {
6144 stp = list_first_entry(&lo->lo_owner.so_stateids,
6145 struct nfs4_ol_stateid,
6147 WARN_ON(!unhash_lock_stateid(stp));
6148 put_ol_stateid_locked(stp, &reaplist);
6150 spin_unlock(&clp->cl_lock);
6151 free_ol_stateid_reaplist(&reaplist);
6152 nfs4_put_stateowner(&lo->lo_owner);
6157 static inline struct nfs4_client_reclaim *
6160 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
6164 nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
6166 struct nfs4_client_reclaim *crp;
6168 crp = nfsd4_find_reclaim_client(name, nn);
6169 return (crp && crp->cr_clp);
6173 * failure => all reset bets are off, nfserr_no_grace...
6175 struct nfs4_client_reclaim *
6176 nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
6178 unsigned int strhashval;
6179 struct nfs4_client_reclaim *crp;
6181 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
6182 crp = alloc_reclaim();
6184 strhashval = clientstr_hashval(name);
6185 INIT_LIST_HEAD(&crp->cr_strhash);
6186 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
6187 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
6189 nn->reclaim_str_hashtbl_size++;
6195 nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
6197 list_del(&crp->cr_strhash);
6199 nn->reclaim_str_hashtbl_size--;
6203 nfs4_release_reclaim(struct nfsd_net *nn)
6205 struct nfs4_client_reclaim *crp = NULL;
6208 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6209 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
6210 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
6211 struct nfs4_client_reclaim, cr_strhash);
6212 nfs4_remove_reclaim_record(crp, nn);
6215 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
6219 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
6220 struct nfs4_client_reclaim *
6221 nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
6223 unsigned int strhashval;
6224 struct nfs4_client_reclaim *crp = NULL;
6226 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
6228 strhashval = clientstr_hashval(recdir);
6229 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
6230 if (same_name(crp->cr_recdir, recdir)) {
6238 * Called from OPEN. Look for clientid in reclaim list.
6241 nfs4_check_open_reclaim(clientid_t *clid,
6242 struct nfsd4_compound_state *cstate,
6243 struct nfsd_net *nn)
6247 /* find clientid in conf_id_hashtbl */
6248 status = lookup_clientid(clid, cstate, nn);
6250 return nfserr_reclaim_bad;
6252 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
6253 return nfserr_no_grace;
6255 if (nfsd4_client_record_check(cstate->clp))
6256 return nfserr_reclaim_bad;
6261 #ifdef CONFIG_NFSD_FAULT_INJECTION
6263 put_client(struct nfs4_client *clp)
6265 atomic_dec(&clp->cl_refcount);
6268 static struct nfs4_client *
6269 nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
6271 struct nfs4_client *clp;
6272 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6275 if (!nfsd_netns_ready(nn))
6278 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6279 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
6286 nfsd_inject_print_clients(void)
6288 struct nfs4_client *clp;
6290 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6292 char buf[INET6_ADDRSTRLEN];
6294 if (!nfsd_netns_ready(nn))
6297 spin_lock(&nn->client_lock);
6298 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6299 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
6300 pr_info("NFS Client: %s\n", buf);
6303 spin_unlock(&nn->client_lock);
6309 nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
6312 struct nfs4_client *clp;
6313 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6316 if (!nfsd_netns_ready(nn))
6319 spin_lock(&nn->client_lock);
6320 clp = nfsd_find_client(addr, addr_size);
6322 if (mark_client_expired_locked(clp) == nfs_ok)
6327 spin_unlock(&nn->client_lock);
6336 nfsd_inject_forget_clients(u64 max)
6339 struct nfs4_client *clp, *next;
6340 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6342 LIST_HEAD(reaplist);
6344 if (!nfsd_netns_ready(nn))
6347 spin_lock(&nn->client_lock);
6348 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6349 if (mark_client_expired_locked(clp) == nfs_ok) {
6350 list_add(&clp->cl_lru, &reaplist);
6351 if (max != 0 && ++count >= max)
6355 spin_unlock(&nn->client_lock);
6357 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
6363 static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
6366 char buf[INET6_ADDRSTRLEN];
6367 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
6368 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
6372 nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
6373 struct list_head *collect)
6375 struct nfs4_client *clp = lst->st_stid.sc_client;
6376 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6382 lockdep_assert_held(&nn->client_lock);
6383 atomic_inc(&clp->cl_refcount);
6384 list_add(&lst->st_locks, collect);
6387 static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
6388 struct list_head *collect,
6389 bool (*func)(struct nfs4_ol_stateid *))
6391 struct nfs4_openowner *oop;
6392 struct nfs4_ol_stateid *stp, *st_next;
6393 struct nfs4_ol_stateid *lst, *lst_next;
6396 spin_lock(&clp->cl_lock);
6397 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
6398 list_for_each_entry_safe(stp, st_next,
6399 &oop->oo_owner.so_stateids, st_perstateowner) {
6400 list_for_each_entry_safe(lst, lst_next,
6401 &stp->st_locks, st_locks) {
6404 nfsd_inject_add_lock_to_list(lst,
6409 * Despite the fact that these functions deal
6410 * with 64-bit integers for "count", we must
6411 * ensure that it doesn't blow up the
6412 * clp->cl_refcount. Throw a warning if we
6413 * start to approach INT_MAX here.
6415 WARN_ON_ONCE(count == (INT_MAX / 2));
6422 spin_unlock(&clp->cl_lock);
6428 nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
6431 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
6435 nfsd_print_client_locks(struct nfs4_client *clp)
6437 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
6438 nfsd_print_count(clp, count, "locked files");
6443 nfsd_inject_print_locks(void)
6445 struct nfs4_client *clp;
6447 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6450 if (!nfsd_netns_ready(nn))
6453 spin_lock(&nn->client_lock);
6454 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6455 count += nfsd_print_client_locks(clp);
6456 spin_unlock(&nn->client_lock);
6462 nfsd_reap_locks(struct list_head *reaplist)
6464 struct nfs4_client *clp;
6465 struct nfs4_ol_stateid *stp, *next;
6467 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
6468 list_del_init(&stp->st_locks);
6469 clp = stp->st_stid.sc_client;
6470 nfs4_put_stid(&stp->st_stid);
6476 nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
6478 unsigned int count = 0;
6479 struct nfs4_client *clp;
6480 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6482 LIST_HEAD(reaplist);
6484 if (!nfsd_netns_ready(nn))
6487 spin_lock(&nn->client_lock);
6488 clp = nfsd_find_client(addr, addr_size);
6490 count = nfsd_collect_client_locks(clp, &reaplist, 0);
6491 spin_unlock(&nn->client_lock);
6492 nfsd_reap_locks(&reaplist);
6497 nfsd_inject_forget_locks(u64 max)
6500 struct nfs4_client *clp;
6501 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6503 LIST_HEAD(reaplist);
6505 if (!nfsd_netns_ready(nn))
6508 spin_lock(&nn->client_lock);
6509 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6510 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
6511 if (max != 0 && count >= max)
6514 spin_unlock(&nn->client_lock);
6515 nfsd_reap_locks(&reaplist);
6520 nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
6521 struct list_head *collect,
6522 void (*func)(struct nfs4_openowner *))
6524 struct nfs4_openowner *oop, *next;
6525 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6529 lockdep_assert_held(&nn->client_lock);
6531 spin_lock(&clp->cl_lock);
6532 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
6536 atomic_inc(&clp->cl_refcount);
6537 list_add(&oop->oo_perclient, collect);
6542 * Despite the fact that these functions deal with
6543 * 64-bit integers for "count", we must ensure that
6544 * it doesn't blow up the clp->cl_refcount. Throw a
6545 * warning if we start to approach INT_MAX here.
6547 WARN_ON_ONCE(count == (INT_MAX / 2));
6551 spin_unlock(&clp->cl_lock);
6557 nfsd_print_client_openowners(struct nfs4_client *clp)
6559 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
6561 nfsd_print_count(clp, count, "openowners");
6566 nfsd_collect_client_openowners(struct nfs4_client *clp,
6567 struct list_head *collect, u64 max)
6569 return nfsd_foreach_client_openowner(clp, max, collect,
6570 unhash_openowner_locked);
6574 nfsd_inject_print_openowners(void)
6576 struct nfs4_client *clp;
6578 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6581 if (!nfsd_netns_ready(nn))
6584 spin_lock(&nn->client_lock);
6585 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6586 count += nfsd_print_client_openowners(clp);
6587 spin_unlock(&nn->client_lock);
6593 nfsd_reap_openowners(struct list_head *reaplist)
6595 struct nfs4_client *clp;
6596 struct nfs4_openowner *oop, *next;
6598 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
6599 list_del_init(&oop->oo_perclient);
6600 clp = oop->oo_owner.so_client;
6601 release_openowner(oop);
6607 nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
6610 unsigned int count = 0;
6611 struct nfs4_client *clp;
6612 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6614 LIST_HEAD(reaplist);
6616 if (!nfsd_netns_ready(nn))
6619 spin_lock(&nn->client_lock);
6620 clp = nfsd_find_client(addr, addr_size);
6622 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
6623 spin_unlock(&nn->client_lock);
6624 nfsd_reap_openowners(&reaplist);
6629 nfsd_inject_forget_openowners(u64 max)
6632 struct nfs4_client *clp;
6633 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6635 LIST_HEAD(reaplist);
6637 if (!nfsd_netns_ready(nn))
6640 spin_lock(&nn->client_lock);
6641 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6642 count += nfsd_collect_client_openowners(clp, &reaplist,
6644 if (max != 0 && count >= max)
6647 spin_unlock(&nn->client_lock);
6648 nfsd_reap_openowners(&reaplist);
6652 static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
6653 struct list_head *victims)
6655 struct nfs4_delegation *dp, *next;
6656 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6660 lockdep_assert_held(&nn->client_lock);
6662 spin_lock(&state_lock);
6663 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
6666 * It's not safe to mess with delegations that have a
6667 * non-zero dl_time. They might have already been broken
6668 * and could be processed by the laundromat outside of
6669 * the state_lock. Just leave them be.
6671 if (dp->dl_time != 0)
6674 atomic_inc(&clp->cl_refcount);
6675 WARN_ON(!unhash_delegation_locked(dp));
6676 list_add(&dp->dl_recall_lru, victims);
6680 * Despite the fact that these functions deal with
6681 * 64-bit integers for "count", we must ensure that
6682 * it doesn't blow up the clp->cl_refcount. Throw a
6683 * warning if we start to approach INT_MAX here.
6685 WARN_ON_ONCE(count == (INT_MAX / 2));
6689 spin_unlock(&state_lock);
6694 nfsd_print_client_delegations(struct nfs4_client *clp)
6696 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
6698 nfsd_print_count(clp, count, "delegations");
6703 nfsd_inject_print_delegations(void)
6705 struct nfs4_client *clp;
6707 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6710 if (!nfsd_netns_ready(nn))
6713 spin_lock(&nn->client_lock);
6714 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6715 count += nfsd_print_client_delegations(clp);
6716 spin_unlock(&nn->client_lock);
6722 nfsd_forget_delegations(struct list_head *reaplist)
6724 struct nfs4_client *clp;
6725 struct nfs4_delegation *dp, *next;
6727 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6728 list_del_init(&dp->dl_recall_lru);
6729 clp = dp->dl_stid.sc_client;
6730 revoke_delegation(dp);
6736 nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
6740 struct nfs4_client *clp;
6741 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6743 LIST_HEAD(reaplist);
6745 if (!nfsd_netns_ready(nn))
6748 spin_lock(&nn->client_lock);
6749 clp = nfsd_find_client(addr, addr_size);
6751 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6752 spin_unlock(&nn->client_lock);
6754 nfsd_forget_delegations(&reaplist);
6759 nfsd_inject_forget_delegations(u64 max)
6762 struct nfs4_client *clp;
6763 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6765 LIST_HEAD(reaplist);
6767 if (!nfsd_netns_ready(nn))
6770 spin_lock(&nn->client_lock);
6771 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6772 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6773 if (max != 0 && count >= max)
6776 spin_unlock(&nn->client_lock);
6777 nfsd_forget_delegations(&reaplist);
6782 nfsd_recall_delegations(struct list_head *reaplist)
6784 struct nfs4_client *clp;
6785 struct nfs4_delegation *dp, *next;
6787 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6788 list_del_init(&dp->dl_recall_lru);
6789 clp = dp->dl_stid.sc_client;
6791 * We skipped all entries that had a zero dl_time before,
6792 * so we can now reset the dl_time back to 0. If a delegation
6793 * break comes in now, then it won't make any difference since
6794 * we're recalling it either way.
6796 spin_lock(&state_lock);
6798 spin_unlock(&state_lock);
6799 nfsd_break_one_deleg(dp);
6805 nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
6809 struct nfs4_client *clp;
6810 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6812 LIST_HEAD(reaplist);
6814 if (!nfsd_netns_ready(nn))
6817 spin_lock(&nn->client_lock);
6818 clp = nfsd_find_client(addr, addr_size);
6820 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6821 spin_unlock(&nn->client_lock);
6823 nfsd_recall_delegations(&reaplist);
6828 nfsd_inject_recall_delegations(u64 max)
6831 struct nfs4_client *clp, *next;
6832 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6834 LIST_HEAD(reaplist);
6836 if (!nfsd_netns_ready(nn))
6839 spin_lock(&nn->client_lock);
6840 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6841 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6842 if (max != 0 && ++count >= max)
6845 spin_unlock(&nn->client_lock);
6846 nfsd_recall_delegations(&reaplist);
6849 #endif /* CONFIG_NFSD_FAULT_INJECTION */
6852 * Since the lifetime of a delegation isn't limited to that of an open, a
6853 * client may quite reasonably hang on to a delegation as long as it has
6854 * the inode cached. This becomes an obvious problem the first time a
6855 * client's inode cache approaches the size of the server's total memory.
6857 * For now we avoid this problem by imposing a hard limit on the number
6858 * of delegations, which varies according to the server's memory size.
6861 set_max_delegations(void)
6864 * Allow at most 4 delegations per megabyte of RAM. Quick
6865 * estimates suggest that in the worst case (where every delegation
6866 * is for a different inode), a delegation could take about 1.5K,
6867 * giving a worst case usage of about 6% of memory.
6869 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
6872 static int nfs4_state_create_net(struct net *net)
6874 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6877 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6878 CLIENT_HASH_SIZE, GFP_KERNEL);
6879 if (!nn->conf_id_hashtbl)
6881 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6882 CLIENT_HASH_SIZE, GFP_KERNEL);
6883 if (!nn->unconf_id_hashtbl)
6885 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
6886 SESSION_HASH_SIZE, GFP_KERNEL);
6887 if (!nn->sessionid_hashtbl)
6890 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6891 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
6892 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
6894 for (i = 0; i < SESSION_HASH_SIZE; i++)
6895 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
6896 nn->conf_name_tree = RB_ROOT;
6897 nn->unconf_name_tree = RB_ROOT;
6898 INIT_LIST_HEAD(&nn->client_lru);
6899 INIT_LIST_HEAD(&nn->close_lru);
6900 INIT_LIST_HEAD(&nn->del_recall_lru);
6901 spin_lock_init(&nn->client_lock);
6903 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
6909 kfree(nn->unconf_id_hashtbl);
6911 kfree(nn->conf_id_hashtbl);
6917 nfs4_state_destroy_net(struct net *net)
6920 struct nfs4_client *clp = NULL;
6921 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6923 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6924 while (!list_empty(&nn->conf_id_hashtbl[i])) {
6925 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6926 destroy_client(clp);
6930 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6931 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
6932 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6933 destroy_client(clp);
6937 kfree(nn->sessionid_hashtbl);
6938 kfree(nn->unconf_id_hashtbl);
6939 kfree(nn->conf_id_hashtbl);
6944 nfs4_state_start_net(struct net *net)
6946 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6949 ret = nfs4_state_create_net(net);
6952 nn->boot_time = get_seconds();
6953 nn->grace_ended = false;
6954 nn->nfsd4_manager.block_opens = true;
6955 locks_start_grace(net, &nn->nfsd4_manager);
6956 nfsd4_client_tracking_init(net);
6957 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
6958 nn->nfsd4_grace, net);
6959 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
6963 /* initialization to perform when the nfsd service is started: */
6966 nfs4_state_start(void)
6970 ret = set_callback_cred();
6973 laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
6974 if (laundry_wq == NULL) {
6978 ret = nfsd4_create_callback_queue();
6980 goto out_free_laundry;
6982 set_max_delegations();
6987 destroy_workqueue(laundry_wq);
6993 nfs4_state_shutdown_net(struct net *net)
6995 struct nfs4_delegation *dp = NULL;
6996 struct list_head *pos, *next, reaplist;
6997 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6999 cancel_delayed_work_sync(&nn->laundromat_work);
7000 locks_end_grace(&nn->nfsd4_manager);
7002 INIT_LIST_HEAD(&reaplist);
7003 spin_lock(&state_lock);
7004 list_for_each_safe(pos, next, &nn->del_recall_lru) {
7005 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
7006 WARN_ON(!unhash_delegation_locked(dp));
7007 list_add(&dp->dl_recall_lru, &reaplist);
7009 spin_unlock(&state_lock);
7010 list_for_each_safe(pos, next, &reaplist) {
7011 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
7012 list_del_init(&dp->dl_recall_lru);
7013 put_clnt_odstate(dp->dl_clnt_odstate);
7014 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
7015 nfs4_put_stid(&dp->dl_stid);
7018 nfsd4_client_tracking_exit(net);
7019 nfs4_state_destroy_net(net);
7023 nfs4_state_shutdown(void)
7025 destroy_workqueue(laundry_wq);
7026 nfsd4_destroy_callback_queue();
7030 get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7032 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
7033 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
7037 put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7039 if (cstate->minorversion) {
7040 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
7041 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
7046 clear_current_stateid(struct nfsd4_compound_state *cstate)
7048 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
7052 * functions to set current state id
7055 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
7057 put_stateid(cstate, &odp->od_stateid);
7061 nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
7063 put_stateid(cstate, &open->op_stateid);
7067 nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
7069 put_stateid(cstate, &close->cl_stateid);
7073 nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
7075 put_stateid(cstate, &lock->lk_resp_stateid);
7079 * functions to consume current state id
7083 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
7085 get_stateid(cstate, &odp->od_stateid);
7089 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
7091 get_stateid(cstate, &drp->dr_stateid);
7095 nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
7097 get_stateid(cstate, &fsp->fr_stateid);
7101 nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
7103 get_stateid(cstate, &setattr->sa_stateid);
7107 nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
7109 get_stateid(cstate, &close->cl_stateid);
7113 nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
7115 get_stateid(cstate, &locku->lu_stateid);
7119 nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
7121 get_stateid(cstate, &read->rd_stateid);
7125 nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
7127 get_stateid(cstate, &write->wr_stateid);