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/hash.h>
48 #include "current_stateid.h"
52 #define NFSDDBG_FACILITY NFSDDBG_PROC
54 #define all_ones {{~0,~0},~0}
55 static const stateid_t one_stateid = {
57 .si_opaque = all_ones,
59 static const stateid_t zero_stateid = {
62 static const stateid_t currentstateid = {
66 static u64 current_sessionid = 1;
68 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
69 #define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
70 #define CURRENT_STATEID(stateid) (!memcmp((stateid), ¤tstateid, sizeof(stateid_t)))
72 /* forward declarations */
73 static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
74 static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
78 /* Currently used for almost all code touching nfsv4 state: */
79 static DEFINE_MUTEX(client_mutex);
82 * Currently used for the del_recall_lru and file hash table. In an
83 * effort to decrease the scope of the client_mutex, this spinlock may
84 * eventually cover more:
86 static DEFINE_SPINLOCK(state_lock);
89 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
90 * the refcount on the open stateid to drop.
92 static DECLARE_WAIT_QUEUE_HEAD(close_wq);
94 static struct kmem_cache *openowner_slab;
95 static struct kmem_cache *lockowner_slab;
96 static struct kmem_cache *file_slab;
97 static struct kmem_cache *stateid_slab;
98 static struct kmem_cache *deleg_slab;
101 nfs4_lock_state(void)
103 mutex_lock(&client_mutex);
106 static void free_session(struct nfsd4_session *);
108 static bool is_session_dead(struct nfsd4_session *ses)
110 return ses->se_flags & NFS4_SESSION_DEAD;
113 static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
115 if (atomic_read(&ses->se_ref) > ref_held_by_me)
116 return nfserr_jukebox;
117 ses->se_flags |= NFS4_SESSION_DEAD;
122 nfs4_unlock_state(void)
124 mutex_unlock(&client_mutex);
127 static bool is_client_expired(struct nfs4_client *clp)
129 return clp->cl_time == 0;
132 static __be32 get_client_locked(struct nfs4_client *clp)
134 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
136 lockdep_assert_held(&nn->client_lock);
138 if (is_client_expired(clp))
139 return nfserr_expired;
140 atomic_inc(&clp->cl_refcount);
144 /* must be called under the client_lock */
146 renew_client_locked(struct nfs4_client *clp)
148 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
150 if (is_client_expired(clp)) {
152 printk("%s: client (clientid %08x/%08x) already expired\n",
154 clp->cl_clientid.cl_boot,
155 clp->cl_clientid.cl_id);
159 dprintk("renewing client (clientid %08x/%08x)\n",
160 clp->cl_clientid.cl_boot,
161 clp->cl_clientid.cl_id);
162 list_move_tail(&clp->cl_lru, &nn->client_lru);
163 clp->cl_time = get_seconds();
167 renew_client(struct nfs4_client *clp)
169 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
171 spin_lock(&nn->client_lock);
172 renew_client_locked(clp);
173 spin_unlock(&nn->client_lock);
176 static void put_client_renew_locked(struct nfs4_client *clp)
178 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
180 lockdep_assert_held(&nn->client_lock);
182 if (!atomic_dec_and_test(&clp->cl_refcount))
184 if (!is_client_expired(clp))
185 renew_client_locked(clp);
188 static void put_client_renew(struct nfs4_client *clp)
190 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
192 if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
194 if (!is_client_expired(clp))
195 renew_client_locked(clp);
196 spin_unlock(&nn->client_lock);
199 static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
203 if (is_session_dead(ses))
204 return nfserr_badsession;
205 status = get_client_locked(ses->se_client);
208 atomic_inc(&ses->se_ref);
212 static void nfsd4_put_session_locked(struct nfsd4_session *ses)
214 struct nfs4_client *clp = ses->se_client;
215 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
217 lockdep_assert_held(&nn->client_lock);
219 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
221 put_client_renew_locked(clp);
224 static void nfsd4_put_session(struct nfsd4_session *ses)
226 struct nfs4_client *clp = ses->se_client;
227 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
229 spin_lock(&nn->client_lock);
230 nfsd4_put_session_locked(ses);
231 spin_unlock(&nn->client_lock);
235 same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
237 return (sop->so_owner.len == owner->len) &&
238 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
241 static struct nfs4_openowner *
242 find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
243 struct nfs4_client *clp)
245 struct nfs4_stateowner *so;
247 lockdep_assert_held(&clp->cl_lock);
249 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
251 if (!so->so_is_open_owner)
253 if (same_owner_str(so, &open->op_owner)) {
254 atomic_inc(&so->so_count);
255 return openowner(so);
261 static struct nfs4_openowner *
262 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
263 struct nfs4_client *clp)
265 struct nfs4_openowner *oo;
267 spin_lock(&clp->cl_lock);
268 oo = find_openstateowner_str_locked(hashval, open, clp);
269 spin_unlock(&clp->cl_lock);
274 opaque_hashval(const void *ptr, int nbytes)
276 unsigned char *cptr = (unsigned char *) ptr;
286 static void nfsd4_free_file(struct nfs4_file *f)
288 kmem_cache_free(file_slab, f);
292 put_nfs4_file(struct nfs4_file *fi)
294 might_lock(&state_lock);
296 if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) {
297 hlist_del(&fi->fi_hash);
298 spin_unlock(&state_lock);
304 get_nfs4_file(struct nfs4_file *fi)
306 atomic_inc(&fi->fi_ref);
310 __nfs4_get_fd(struct nfs4_file *f, int oflag)
312 if (f->fi_fds[oflag])
313 return get_file(f->fi_fds[oflag]);
318 find_writeable_file_locked(struct nfs4_file *f)
322 lockdep_assert_held(&f->fi_lock);
324 ret = __nfs4_get_fd(f, O_WRONLY);
326 ret = __nfs4_get_fd(f, O_RDWR);
331 find_writeable_file(struct nfs4_file *f)
335 spin_lock(&f->fi_lock);
336 ret = find_writeable_file_locked(f);
337 spin_unlock(&f->fi_lock);
342 static struct file *find_readable_file_locked(struct nfs4_file *f)
346 lockdep_assert_held(&f->fi_lock);
348 ret = __nfs4_get_fd(f, O_RDONLY);
350 ret = __nfs4_get_fd(f, O_RDWR);
355 find_readable_file(struct nfs4_file *f)
359 spin_lock(&f->fi_lock);
360 ret = find_readable_file_locked(f);
361 spin_unlock(&f->fi_lock);
367 find_any_file(struct nfs4_file *f)
371 spin_lock(&f->fi_lock);
372 ret = __nfs4_get_fd(f, O_RDWR);
374 ret = __nfs4_get_fd(f, O_WRONLY);
376 ret = __nfs4_get_fd(f, O_RDONLY);
378 spin_unlock(&f->fi_lock);
382 static atomic_long_t num_delegations;
383 unsigned long max_delegations;
386 * Open owner state (share locks)
389 /* hash tables for lock and open owners */
390 #define OWNER_HASH_BITS 8
391 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
392 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
394 static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
398 ret = opaque_hashval(ownername->data, ownername->len);
399 return ret & OWNER_HASH_MASK;
402 /* hash table for nfs4_file */
403 #define FILE_HASH_BITS 8
404 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
406 static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
408 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
411 static unsigned int file_hashval(struct knfsd_fh *fh)
413 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
416 static bool nfsd_fh_match(struct knfsd_fh *fh1, struct knfsd_fh *fh2)
418 return fh1->fh_size == fh2->fh_size &&
419 !memcmp(fh1->fh_base.fh_pad,
424 static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
427 __nfs4_file_get_access(struct nfs4_file *fp, u32 access)
429 lockdep_assert_held(&fp->fi_lock);
431 if (access & NFS4_SHARE_ACCESS_WRITE)
432 atomic_inc(&fp->fi_access[O_WRONLY]);
433 if (access & NFS4_SHARE_ACCESS_READ)
434 atomic_inc(&fp->fi_access[O_RDONLY]);
438 nfs4_file_get_access(struct nfs4_file *fp, u32 access)
440 lockdep_assert_held(&fp->fi_lock);
442 /* Does this access mode make sense? */
443 if (access & ~NFS4_SHARE_ACCESS_BOTH)
446 /* Does it conflict with a deny mode already set? */
447 if ((access & fp->fi_share_deny) != 0)
448 return nfserr_share_denied;
450 __nfs4_file_get_access(fp, access);
454 static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
456 /* Common case is that there is no deny mode. */
458 /* Does this deny mode make sense? */
459 if (deny & ~NFS4_SHARE_DENY_BOTH)
462 if ((deny & NFS4_SHARE_DENY_READ) &&
463 atomic_read(&fp->fi_access[O_RDONLY]))
464 return nfserr_share_denied;
466 if ((deny & NFS4_SHARE_DENY_WRITE) &&
467 atomic_read(&fp->fi_access[O_WRONLY]))
468 return nfserr_share_denied;
473 static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
475 might_lock(&fp->fi_lock);
477 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
478 struct file *f1 = NULL;
479 struct file *f2 = NULL;
481 swap(f1, fp->fi_fds[oflag]);
482 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
483 swap(f2, fp->fi_fds[O_RDWR]);
484 spin_unlock(&fp->fi_lock);
492 static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
494 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
496 if (access & NFS4_SHARE_ACCESS_WRITE)
497 __nfs4_file_put_access(fp, O_WRONLY);
498 if (access & NFS4_SHARE_ACCESS_READ)
499 __nfs4_file_put_access(fp, O_RDONLY);
502 static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl,
503 struct kmem_cache *slab)
505 struct nfs4_stid *stid;
508 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
512 idr_preload(GFP_KERNEL);
513 spin_lock(&cl->cl_lock);
514 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 0, 0, GFP_NOWAIT);
515 spin_unlock(&cl->cl_lock);
519 stid->sc_client = cl;
520 stid->sc_stateid.si_opaque.so_id = new_id;
521 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
522 /* Will be incremented before return to client: */
523 atomic_set(&stid->sc_count, 1);
526 * It shouldn't be a problem to reuse an opaque stateid value.
527 * I don't think it is for 4.1. But with 4.0 I worry that, for
528 * example, a stray write retransmission could be accepted by
529 * the server when it should have been rejected. Therefore,
530 * adopt a trick from the sctp code to attempt to maximize the
531 * amount of time until an id is reused, by ensuring they always
532 * "increase" (mod INT_MAX):
536 kmem_cache_free(slab, stid);
540 static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
542 struct nfs4_stid *stid;
543 struct nfs4_ol_stateid *stp;
545 stid = nfs4_alloc_stid(clp, stateid_slab);
549 stp = openlockstateid(stid);
550 stp->st_stid.sc_free = nfs4_free_ol_stateid;
554 static void nfs4_free_deleg(struct nfs4_stid *stid)
556 kmem_cache_free(deleg_slab, stid);
557 atomic_long_dec(&num_delegations);
561 * When we recall a delegation, we should be careful not to hand it
562 * out again straight away.
563 * To ensure this we keep a pair of bloom filters ('new' and 'old')
564 * in which the filehandles of recalled delegations are "stored".
565 * If a filehandle appear in either filter, a delegation is blocked.
566 * When a delegation is recalled, the filehandle is stored in the "new"
568 * Every 30 seconds we swap the filters and clear the "new" one,
569 * unless both are empty of course.
571 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
572 * low 3 bytes as hash-table indices.
574 * 'blocked_delegations_lock', which is always taken in block_delegations(),
575 * is used to manage concurrent access. Testing does not need the lock
576 * except when swapping the two filters.
578 static DEFINE_SPINLOCK(blocked_delegations_lock);
579 static struct bloom_pair {
580 int entries, old_entries;
582 int new; /* index into 'set' */
583 DECLARE_BITMAP(set[2], 256);
584 } blocked_delegations;
586 static int delegation_blocked(struct knfsd_fh *fh)
589 struct bloom_pair *bd = &blocked_delegations;
591 if (bd->entries == 0)
593 if (seconds_since_boot() - bd->swap_time > 30) {
594 spin_lock(&blocked_delegations_lock);
595 if (seconds_since_boot() - bd->swap_time > 30) {
596 bd->entries -= bd->old_entries;
597 bd->old_entries = bd->entries;
598 memset(bd->set[bd->new], 0,
601 bd->swap_time = seconds_since_boot();
603 spin_unlock(&blocked_delegations_lock);
605 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
606 if (test_bit(hash&255, bd->set[0]) &&
607 test_bit((hash>>8)&255, bd->set[0]) &&
608 test_bit((hash>>16)&255, bd->set[0]))
611 if (test_bit(hash&255, bd->set[1]) &&
612 test_bit((hash>>8)&255, bd->set[1]) &&
613 test_bit((hash>>16)&255, bd->set[1]))
619 static void block_delegations(struct knfsd_fh *fh)
622 struct bloom_pair *bd = &blocked_delegations;
624 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
626 spin_lock(&blocked_delegations_lock);
627 __set_bit(hash&255, bd->set[bd->new]);
628 __set_bit((hash>>8)&255, bd->set[bd->new]);
629 __set_bit((hash>>16)&255, bd->set[bd->new]);
630 if (bd->entries == 0)
631 bd->swap_time = seconds_since_boot();
633 spin_unlock(&blocked_delegations_lock);
636 static struct nfs4_delegation *
637 alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh)
639 struct nfs4_delegation *dp;
642 dprintk("NFSD alloc_init_deleg\n");
643 n = atomic_long_inc_return(&num_delegations);
644 if (n < 0 || n > max_delegations)
646 if (delegation_blocked(¤t_fh->fh_handle))
648 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
652 dp->dl_stid.sc_free = nfs4_free_deleg;
654 * delegation seqid's are never incremented. The 4.1 special
655 * meaning of seqid 0 isn't meaningful, really, but let's avoid
656 * 0 anyway just for consistency and use 1:
658 dp->dl_stid.sc_stateid.si_generation = 1;
659 INIT_LIST_HEAD(&dp->dl_perfile);
660 INIT_LIST_HEAD(&dp->dl_perclnt);
661 INIT_LIST_HEAD(&dp->dl_recall_lru);
662 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
663 INIT_WORK(&dp->dl_recall.cb_work, nfsd4_run_cb_recall);
666 atomic_long_dec(&num_delegations);
671 nfs4_put_stid(struct nfs4_stid *s)
673 struct nfs4_file *fp = s->sc_file;
674 struct nfs4_client *clp = s->sc_client;
676 might_lock(&clp->cl_lock);
678 if (!atomic_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
679 wake_up_all(&close_wq);
682 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
683 spin_unlock(&clp->cl_lock);
689 static void nfs4_put_deleg_lease(struct nfs4_file *fp)
691 lockdep_assert_held(&state_lock);
695 if (atomic_dec_and_test(&fp->fi_delegees)) {
696 vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
698 fput(fp->fi_deleg_file);
699 fp->fi_deleg_file = NULL;
703 static void unhash_stid(struct nfs4_stid *s)
709 hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
711 lockdep_assert_held(&state_lock);
712 lockdep_assert_held(&fp->fi_lock);
714 atomic_inc(&dp->dl_stid.sc_count);
715 dp->dl_stid.sc_type = NFS4_DELEG_STID;
716 list_add(&dp->dl_perfile, &fp->fi_delegations);
717 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
721 unhash_delegation_locked(struct nfs4_delegation *dp)
723 struct nfs4_file *fp = dp->dl_stid.sc_file;
725 lockdep_assert_held(&state_lock);
727 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
728 /* Ensure that deleg break won't try to requeue it */
730 spin_lock(&fp->fi_lock);
731 list_del_init(&dp->dl_perclnt);
732 list_del_init(&dp->dl_recall_lru);
733 list_del_init(&dp->dl_perfile);
734 spin_unlock(&fp->fi_lock);
736 nfs4_put_deleg_lease(fp);
739 static void destroy_delegation(struct nfs4_delegation *dp)
741 spin_lock(&state_lock);
742 unhash_delegation_locked(dp);
743 spin_unlock(&state_lock);
744 nfs4_put_stid(&dp->dl_stid);
747 static void revoke_delegation(struct nfs4_delegation *dp)
749 struct nfs4_client *clp = dp->dl_stid.sc_client;
751 WARN_ON(!list_empty(&dp->dl_recall_lru));
753 if (clp->cl_minorversion == 0)
754 nfs4_put_stid(&dp->dl_stid);
756 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
757 spin_lock(&clp->cl_lock);
758 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
759 spin_unlock(&clp->cl_lock);
767 static unsigned int clientid_hashval(u32 id)
769 return id & CLIENT_HASH_MASK;
772 static unsigned int clientstr_hashval(const char *name)
774 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
778 * We store the NONE, READ, WRITE, and BOTH bits separately in the
779 * st_{access,deny}_bmap field of the stateid, in order to track not
780 * only what share bits are currently in force, but also what
781 * combinations of share bits previous opens have used. This allows us
782 * to enforce the recommendation of rfc 3530 14.2.19 that the server
783 * return an error if the client attempt to downgrade to a combination
784 * of share bits not explicable by closing some of its previous opens.
786 * XXX: This enforcement is actually incomplete, since we don't keep
787 * track of access/deny bit combinations; so, e.g., we allow:
789 * OPEN allow read, deny write
790 * OPEN allow both, deny none
791 * DOWNGRADE allow read, deny none
793 * which we should reject.
796 bmap_to_share_mode(unsigned long bmap) {
798 unsigned int access = 0;
800 for (i = 1; i < 4; i++) {
801 if (test_bit(i, &bmap))
807 /* set share access for a given stateid */
809 set_access(u32 access, struct nfs4_ol_stateid *stp)
811 unsigned char mask = 1 << access;
813 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
814 stp->st_access_bmap |= mask;
817 /* clear share access for a given stateid */
819 clear_access(u32 access, struct nfs4_ol_stateid *stp)
821 unsigned char mask = 1 << access;
823 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
824 stp->st_access_bmap &= ~mask;
827 /* test whether a given stateid has access */
829 test_access(u32 access, struct nfs4_ol_stateid *stp)
831 unsigned char mask = 1 << access;
833 return (bool)(stp->st_access_bmap & mask);
836 /* set share deny for a given stateid */
838 set_deny(u32 deny, struct nfs4_ol_stateid *stp)
840 unsigned char mask = 1 << deny;
842 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
843 stp->st_deny_bmap |= mask;
846 /* clear share deny for a given stateid */
848 clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
850 unsigned char mask = 1 << deny;
852 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
853 stp->st_deny_bmap &= ~mask;
856 /* test whether a given stateid is denying specific access */
858 test_deny(u32 deny, struct nfs4_ol_stateid *stp)
860 unsigned char mask = 1 << deny;
862 return (bool)(stp->st_deny_bmap & mask);
865 static int nfs4_access_to_omode(u32 access)
867 switch (access & NFS4_SHARE_ACCESS_BOTH) {
868 case NFS4_SHARE_ACCESS_READ:
870 case NFS4_SHARE_ACCESS_WRITE:
872 case NFS4_SHARE_ACCESS_BOTH:
880 * A stateid that had a deny mode associated with it is being released
881 * or downgraded. Recalculate the deny mode on the file.
884 recalculate_deny_mode(struct nfs4_file *fp)
886 struct nfs4_ol_stateid *stp;
888 spin_lock(&fp->fi_lock);
889 fp->fi_share_deny = 0;
890 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
891 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
892 spin_unlock(&fp->fi_lock);
896 reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
901 for (i = 1; i < 4; i++) {
902 if ((i & deny) != i) {
908 /* Recalculate per-file deny mode if there was a change */
910 recalculate_deny_mode(stp->st_stid.sc_file);
913 /* release all access and file references for a given stateid */
915 release_all_access(struct nfs4_ol_stateid *stp)
918 struct nfs4_file *fp = stp->st_stid.sc_file;
920 if (fp && stp->st_deny_bmap != 0)
921 recalculate_deny_mode(fp);
923 for (i = 1; i < 4; i++) {
924 if (test_access(i, stp))
925 nfs4_file_put_access(stp->st_stid.sc_file, i);
926 clear_access(i, stp);
930 static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
932 struct nfs4_client *clp = sop->so_client;
934 might_lock(&clp->cl_lock);
936 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
938 sop->so_ops->so_unhash(sop);
939 spin_unlock(&clp->cl_lock);
940 kfree(sop->so_owner.data);
941 sop->so_ops->so_free(sop);
944 static void unhash_ol_stateid(struct nfs4_ol_stateid *stp)
946 struct nfs4_file *fp = stp->st_stid.sc_file;
948 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
950 spin_lock(&fp->fi_lock);
951 list_del(&stp->st_perfile);
952 spin_unlock(&fp->fi_lock);
953 list_del(&stp->st_perstateowner);
956 static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
958 struct nfs4_ol_stateid *stp = openlockstateid(stid);
960 release_all_access(stp);
961 if (stp->st_stateowner)
962 nfs4_put_stateowner(stp->st_stateowner);
963 kmem_cache_free(stateid_slab, stid);
966 static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
968 struct nfs4_ol_stateid *stp = openlockstateid(stid);
969 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
972 file = find_any_file(stp->st_stid.sc_file);
974 filp_close(file, (fl_owner_t)lo);
975 nfs4_free_ol_stateid(stid);
979 * Put the persistent reference to an already unhashed generic stateid, while
980 * holding the cl_lock. If it's the last reference, then put it onto the
981 * reaplist for later destruction.
983 static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
984 struct list_head *reaplist)
986 struct nfs4_stid *s = &stp->st_stid;
987 struct nfs4_client *clp = s->sc_client;
989 lockdep_assert_held(&clp->cl_lock);
991 WARN_ON_ONCE(!list_empty(&stp->st_locks));
993 if (!atomic_dec_and_test(&s->sc_count)) {
994 wake_up_all(&close_wq);
998 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
999 list_add(&stp->st_locks, reaplist);
1002 static void unhash_lock_stateid(struct nfs4_ol_stateid *stp)
1004 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
1006 lockdep_assert_held(&oo->oo_owner.so_client->cl_lock);
1008 list_del_init(&stp->st_locks);
1009 unhash_ol_stateid(stp);
1010 unhash_stid(&stp->st_stid);
1013 static void release_lock_stateid(struct nfs4_ol_stateid *stp)
1015 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
1017 spin_lock(&oo->oo_owner.so_client->cl_lock);
1018 unhash_lock_stateid(stp);
1019 spin_unlock(&oo->oo_owner.so_client->cl_lock);
1020 nfs4_put_stid(&stp->st_stid);
1023 static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
1025 struct nfs4_client *clp = lo->lo_owner.so_client;
1027 lockdep_assert_held(&clp->cl_lock);
1029 list_del_init(&lo->lo_owner.so_strhash);
1033 * Free a list of generic stateids that were collected earlier after being
1037 free_ol_stateid_reaplist(struct list_head *reaplist)
1039 struct nfs4_ol_stateid *stp;
1040 struct nfs4_file *fp;
1044 while (!list_empty(reaplist)) {
1045 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1047 list_del(&stp->st_locks);
1048 fp = stp->st_stid.sc_file;
1049 stp->st_stid.sc_free(&stp->st_stid);
1055 static void release_lockowner(struct nfs4_lockowner *lo)
1057 struct nfs4_client *clp = lo->lo_owner.so_client;
1058 struct nfs4_ol_stateid *stp;
1059 struct list_head reaplist;
1061 INIT_LIST_HEAD(&reaplist);
1063 spin_lock(&clp->cl_lock);
1064 unhash_lockowner_locked(lo);
1065 while (!list_empty(&lo->lo_owner.so_stateids)) {
1066 stp = list_first_entry(&lo->lo_owner.so_stateids,
1067 struct nfs4_ol_stateid, st_perstateowner);
1068 unhash_lock_stateid(stp);
1069 put_ol_stateid_locked(stp, &reaplist);
1071 spin_unlock(&clp->cl_lock);
1072 free_ol_stateid_reaplist(&reaplist);
1073 nfs4_put_stateowner(&lo->lo_owner);
1076 static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1077 struct list_head *reaplist)
1079 struct nfs4_ol_stateid *stp;
1081 while (!list_empty(&open_stp->st_locks)) {
1082 stp = list_entry(open_stp->st_locks.next,
1083 struct nfs4_ol_stateid, st_locks);
1084 unhash_lock_stateid(stp);
1085 put_ol_stateid_locked(stp, reaplist);
1089 static void unhash_open_stateid(struct nfs4_ol_stateid *stp,
1090 struct list_head *reaplist)
1092 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1094 unhash_ol_stateid(stp);
1095 release_open_stateid_locks(stp, reaplist);
1098 static void release_open_stateid(struct nfs4_ol_stateid *stp)
1100 LIST_HEAD(reaplist);
1102 spin_lock(&stp->st_stid.sc_client->cl_lock);
1103 unhash_open_stateid(stp, &reaplist);
1104 put_ol_stateid_locked(stp, &reaplist);
1105 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1106 free_ol_stateid_reaplist(&reaplist);
1109 static void unhash_openowner_locked(struct nfs4_openowner *oo)
1111 struct nfs4_client *clp = oo->oo_owner.so_client;
1113 lockdep_assert_held(&clp->cl_lock);
1115 list_del_init(&oo->oo_owner.so_strhash);
1116 list_del_init(&oo->oo_perclient);
1119 static void release_last_closed_stateid(struct nfs4_openowner *oo)
1121 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1123 struct nfs4_ol_stateid *s;
1125 spin_lock(&nn->client_lock);
1126 s = oo->oo_last_closed_stid;
1128 list_del_init(&oo->oo_close_lru);
1129 oo->oo_last_closed_stid = NULL;
1131 spin_unlock(&nn->client_lock);
1133 nfs4_put_stid(&s->st_stid);
1136 static void release_openowner(struct nfs4_openowner *oo)
1138 struct nfs4_ol_stateid *stp;
1139 struct nfs4_client *clp = oo->oo_owner.so_client;
1140 struct list_head reaplist;
1142 INIT_LIST_HEAD(&reaplist);
1144 spin_lock(&clp->cl_lock);
1145 unhash_openowner_locked(oo);
1146 while (!list_empty(&oo->oo_owner.so_stateids)) {
1147 stp = list_first_entry(&oo->oo_owner.so_stateids,
1148 struct nfs4_ol_stateid, st_perstateowner);
1149 unhash_open_stateid(stp, &reaplist);
1150 put_ol_stateid_locked(stp, &reaplist);
1152 spin_unlock(&clp->cl_lock);
1153 free_ol_stateid_reaplist(&reaplist);
1154 release_last_closed_stateid(oo);
1155 nfs4_put_stateowner(&oo->oo_owner);
1159 hash_sessionid(struct nfs4_sessionid *sessionid)
1161 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1163 return sid->sequence % SESSION_HASH_SIZE;
1168 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1170 u32 *ptr = (u32 *)(&sessionid->data[0]);
1171 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1175 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1181 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1182 * won't be used for replay.
1184 void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1186 struct nfs4_stateowner *so = cstate->replay_owner;
1188 if (nfserr == nfserr_replay_me)
1191 if (!seqid_mutating_err(ntohl(nfserr))) {
1192 nfsd4_cstate_clear_replay(cstate);
1197 if (so->so_is_open_owner)
1198 release_last_closed_stateid(openowner(so));
1204 gen_sessionid(struct nfsd4_session *ses)
1206 struct nfs4_client *clp = ses->se_client;
1207 struct nfsd4_sessionid *sid;
1209 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1210 sid->clientid = clp->cl_clientid;
1211 sid->sequence = current_sessionid++;
1216 * The protocol defines ca_maxresponssize_cached to include the size of
1217 * the rpc header, but all we need to cache is the data starting after
1218 * the end of the initial SEQUENCE operation--the rest we regenerate
1219 * each time. Therefore we can advertise a ca_maxresponssize_cached
1220 * value that is the number of bytes in our cache plus a few additional
1221 * bytes. In order to stay on the safe side, and not promise more than
1222 * we can cache, those additional bytes must be the minimum possible: 24
1223 * bytes of rpc header (xid through accept state, with AUTH_NULL
1224 * verifier), 12 for the compound header (with zero-length tag), and 44
1225 * for the SEQUENCE op response:
1227 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1230 free_session_slots(struct nfsd4_session *ses)
1234 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1235 kfree(ses->se_slots[i]);
1239 * We don't actually need to cache the rpc and session headers, so we
1240 * can allocate a little less for each slot:
1242 static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
1246 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1249 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1250 return size + sizeof(struct nfsd4_slot);
1254 * XXX: If we run out of reserved DRC memory we could (up to a point)
1255 * re-negotiate active sessions and reduce their slot usage to make
1256 * room for new connections. For now we just fail the create session.
1258 static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
1260 u32 slotsize = slot_bytes(ca);
1261 u32 num = ca->maxreqs;
1264 spin_lock(&nfsd_drc_lock);
1265 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1266 nfsd_drc_max_mem - nfsd_drc_mem_used);
1267 num = min_t(int, num, avail / slotsize);
1268 nfsd_drc_mem_used += num * slotsize;
1269 spin_unlock(&nfsd_drc_lock);
1274 static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
1276 int slotsize = slot_bytes(ca);
1278 spin_lock(&nfsd_drc_lock);
1279 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
1280 spin_unlock(&nfsd_drc_lock);
1283 static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1284 struct nfsd4_channel_attrs *battrs)
1286 int numslots = fattrs->maxreqs;
1287 int slotsize = slot_bytes(fattrs);
1288 struct nfsd4_session *new;
1291 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
1292 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1293 mem = numslots * sizeof(struct nfsd4_slot *);
1295 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
1298 /* allocate each struct nfsd4_slot and data cache in one piece */
1299 for (i = 0; i < numslots; i++) {
1300 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
1301 if (!new->se_slots[i])
1305 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1306 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1311 kfree(new->se_slots[i]);
1316 static void free_conn(struct nfsd4_conn *c)
1318 svc_xprt_put(c->cn_xprt);
1322 static void nfsd4_conn_lost(struct svc_xpt_user *u)
1324 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1325 struct nfs4_client *clp = c->cn_session->se_client;
1327 spin_lock(&clp->cl_lock);
1328 if (!list_empty(&c->cn_persession)) {
1329 list_del(&c->cn_persession);
1332 nfsd4_probe_callback(clp);
1333 spin_unlock(&clp->cl_lock);
1336 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
1338 struct nfsd4_conn *conn;
1340 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1343 svc_xprt_get(rqstp->rq_xprt);
1344 conn->cn_xprt = rqstp->rq_xprt;
1345 conn->cn_flags = flags;
1346 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1350 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1352 conn->cn_session = ses;
1353 list_add(&conn->cn_persession, &ses->se_conns);
1356 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1358 struct nfs4_client *clp = ses->se_client;
1360 spin_lock(&clp->cl_lock);
1361 __nfsd4_hash_conn(conn, ses);
1362 spin_unlock(&clp->cl_lock);
1365 static int nfsd4_register_conn(struct nfsd4_conn *conn)
1367 conn->cn_xpt_user.callback = nfsd4_conn_lost;
1368 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
1371 static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
1375 nfsd4_hash_conn(conn, ses);
1376 ret = nfsd4_register_conn(conn);
1378 /* oops; xprt is already down: */
1379 nfsd4_conn_lost(&conn->cn_xpt_user);
1380 /* We may have gained or lost a callback channel: */
1381 nfsd4_probe_callback_sync(ses->se_client);
1384 static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
1386 u32 dir = NFS4_CDFC4_FORE;
1388 if (cses->flags & SESSION4_BACK_CHAN)
1389 dir |= NFS4_CDFC4_BACK;
1390 return alloc_conn(rqstp, dir);
1393 /* must be called under client_lock */
1394 static void nfsd4_del_conns(struct nfsd4_session *s)
1396 struct nfs4_client *clp = s->se_client;
1397 struct nfsd4_conn *c;
1399 spin_lock(&clp->cl_lock);
1400 while (!list_empty(&s->se_conns)) {
1401 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1402 list_del_init(&c->cn_persession);
1403 spin_unlock(&clp->cl_lock);
1405 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1408 spin_lock(&clp->cl_lock);
1410 spin_unlock(&clp->cl_lock);
1413 static void __free_session(struct nfsd4_session *ses)
1415 free_session_slots(ses);
1419 static void free_session(struct nfsd4_session *ses)
1421 nfsd4_del_conns(ses);
1422 nfsd4_put_drc_mem(&ses->se_fchannel);
1423 __free_session(ses);
1426 static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
1429 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1431 new->se_client = clp;
1434 INIT_LIST_HEAD(&new->se_conns);
1436 new->se_cb_seq_nr = 1;
1437 new->se_flags = cses->flags;
1438 new->se_cb_prog = cses->callback_prog;
1439 new->se_cb_sec = cses->cb_sec;
1440 atomic_set(&new->se_ref, 0);
1441 idx = hash_sessionid(&new->se_sessionid);
1442 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
1443 spin_lock(&clp->cl_lock);
1444 list_add(&new->se_perclnt, &clp->cl_sessions);
1445 spin_unlock(&clp->cl_lock);
1447 if (cses->flags & SESSION4_BACK_CHAN) {
1448 struct sockaddr *sa = svc_addr(rqstp);
1450 * This is a little silly; with sessions there's no real
1451 * use for the callback address. Use the peer address
1452 * as a reasonable default for now, but consider fixing
1453 * the rpc client not to require an address in the
1456 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1457 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
1461 /* caller must hold client_lock */
1462 static struct nfsd4_session *
1463 __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
1465 struct nfsd4_session *elem;
1467 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1469 lockdep_assert_held(&nn->client_lock);
1471 dump_sessionid(__func__, sessionid);
1472 idx = hash_sessionid(sessionid);
1473 /* Search in the appropriate list */
1474 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
1475 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1476 NFS4_MAX_SESSIONID_LEN)) {
1481 dprintk("%s: session not found\n", __func__);
1485 static struct nfsd4_session *
1486 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1489 struct nfsd4_session *session;
1490 __be32 status = nfserr_badsession;
1492 session = __find_in_sessionid_hashtbl(sessionid, net);
1495 status = nfsd4_get_session_locked(session);
1503 /* caller must hold client_lock */
1505 unhash_session(struct nfsd4_session *ses)
1507 struct nfs4_client *clp = ses->se_client;
1508 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1510 lockdep_assert_held(&nn->client_lock);
1512 list_del(&ses->se_hash);
1513 spin_lock(&ses->se_client->cl_lock);
1514 list_del(&ses->se_perclnt);
1515 spin_unlock(&ses->se_client->cl_lock);
1518 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1520 STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
1522 if (clid->cl_boot == nn->boot_time)
1524 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
1525 clid->cl_boot, clid->cl_id, nn->boot_time);
1530 * XXX Should we use a slab cache ?
1531 * This type of memory management is somewhat inefficient, but we use it
1532 * anyway since SETCLIENTID is not a common operation.
1534 static struct nfs4_client *alloc_client(struct xdr_netobj name)
1536 struct nfs4_client *clp;
1539 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1542 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
1543 if (clp->cl_name.data == NULL)
1545 clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
1546 OWNER_HASH_SIZE, GFP_KERNEL);
1547 if (!clp->cl_ownerstr_hashtbl)
1548 goto err_no_hashtbl;
1549 for (i = 0; i < OWNER_HASH_SIZE; i++)
1550 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
1551 clp->cl_name.len = name.len;
1552 INIT_LIST_HEAD(&clp->cl_sessions);
1553 idr_init(&clp->cl_stateids);
1554 atomic_set(&clp->cl_refcount, 0);
1555 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1556 INIT_LIST_HEAD(&clp->cl_idhash);
1557 INIT_LIST_HEAD(&clp->cl_openowners);
1558 INIT_LIST_HEAD(&clp->cl_delegations);
1559 INIT_LIST_HEAD(&clp->cl_lru);
1560 INIT_LIST_HEAD(&clp->cl_callbacks);
1561 INIT_LIST_HEAD(&clp->cl_revoked);
1562 spin_lock_init(&clp->cl_lock);
1563 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1566 kfree(clp->cl_name.data);
1573 free_client(struct nfs4_client *clp)
1575 while (!list_empty(&clp->cl_sessions)) {
1576 struct nfsd4_session *ses;
1577 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1579 list_del(&ses->se_perclnt);
1580 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1583 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
1584 free_svc_cred(&clp->cl_cred);
1585 kfree(clp->cl_ownerstr_hashtbl);
1586 kfree(clp->cl_name.data);
1587 idr_destroy(&clp->cl_stateids);
1591 /* must be called under the client_lock */
1593 unhash_client_locked(struct nfs4_client *clp)
1595 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1596 struct nfsd4_session *ses;
1598 lockdep_assert_held(&nn->client_lock);
1600 /* Mark the client as expired! */
1602 /* Make it invisible */
1603 if (!list_empty(&clp->cl_idhash)) {
1604 list_del_init(&clp->cl_idhash);
1605 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1606 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1608 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1610 list_del_init(&clp->cl_lru);
1611 spin_lock(&clp->cl_lock);
1612 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1613 list_del_init(&ses->se_hash);
1614 spin_unlock(&clp->cl_lock);
1618 unhash_client(struct nfs4_client *clp)
1620 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1622 spin_lock(&nn->client_lock);
1623 unhash_client_locked(clp);
1624 spin_unlock(&nn->client_lock);
1627 static __be32 mark_client_expired_locked(struct nfs4_client *clp)
1629 if (atomic_read(&clp->cl_refcount))
1630 return nfserr_jukebox;
1631 unhash_client_locked(clp);
1636 __destroy_client(struct nfs4_client *clp)
1638 struct nfs4_openowner *oo;
1639 struct nfs4_delegation *dp;
1640 struct list_head reaplist;
1642 INIT_LIST_HEAD(&reaplist);
1643 spin_lock(&state_lock);
1644 while (!list_empty(&clp->cl_delegations)) {
1645 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
1646 unhash_delegation_locked(dp);
1647 list_add(&dp->dl_recall_lru, &reaplist);
1649 spin_unlock(&state_lock);
1650 while (!list_empty(&reaplist)) {
1651 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
1652 list_del_init(&dp->dl_recall_lru);
1653 nfs4_put_stid(&dp->dl_stid);
1655 while (!list_empty(&clp->cl_revoked)) {
1656 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
1657 list_del_init(&dp->dl_recall_lru);
1658 nfs4_put_stid(&dp->dl_stid);
1660 while (!list_empty(&clp->cl_openowners)) {
1661 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1662 atomic_inc(&oo->oo_owner.so_count);
1663 release_openowner(oo);
1665 nfsd4_shutdown_callback(clp);
1666 if (clp->cl_cb_conn.cb_xprt)
1667 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
1672 destroy_client(struct nfs4_client *clp)
1675 __destroy_client(clp);
1678 static void expire_client(struct nfs4_client *clp)
1681 nfsd4_client_record_remove(clp);
1682 __destroy_client(clp);
1685 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1687 memcpy(target->cl_verifier.data, source->data,
1688 sizeof(target->cl_verifier.data));
1691 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1693 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1694 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1697 static int copy_cred(struct svc_cred *target, struct svc_cred *source)
1699 if (source->cr_principal) {
1700 target->cr_principal =
1701 kstrdup(source->cr_principal, GFP_KERNEL);
1702 if (target->cr_principal == NULL)
1705 target->cr_principal = NULL;
1706 target->cr_flavor = source->cr_flavor;
1707 target->cr_uid = source->cr_uid;
1708 target->cr_gid = source->cr_gid;
1709 target->cr_group_info = source->cr_group_info;
1710 get_group_info(target->cr_group_info);
1711 target->cr_gss_mech = source->cr_gss_mech;
1712 if (source->cr_gss_mech)
1713 gss_mech_get(source->cr_gss_mech);
1718 compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1722 res = o1->len - o2->len;
1725 return (long long)memcmp(o1->data, o2->data, o1->len);
1728 static int same_name(const char *n1, const char *n2)
1730 return 0 == memcmp(n1, n2, HEXDIR_LEN);
1734 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1736 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
1740 same_clid(clientid_t *cl1, clientid_t *cl2)
1742 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
1745 static bool groups_equal(struct group_info *g1, struct group_info *g2)
1749 if (g1->ngroups != g2->ngroups)
1751 for (i=0; i<g1->ngroups; i++)
1752 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
1758 * RFC 3530 language requires clid_inuse be returned when the
1759 * "principal" associated with a requests differs from that previously
1760 * used. We use uid, gid's, and gss principal string as our best
1761 * approximation. We also don't want to allow non-gss use of a client
1762 * established using gss: in theory cr_principal should catch that
1763 * change, but in practice cr_principal can be null even in the gss case
1764 * since gssd doesn't always pass down a principal string.
1766 static bool is_gss_cred(struct svc_cred *cr)
1768 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1769 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1774 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1776 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
1777 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1778 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
1779 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1781 if (cr1->cr_principal == cr2->cr_principal)
1783 if (!cr1->cr_principal || !cr2->cr_principal)
1785 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
1788 static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1790 struct svc_cred *cr = &rqstp->rq_cred;
1793 if (!cr->cr_gss_mech)
1795 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1796 return service == RPC_GSS_SVC_INTEGRITY ||
1797 service == RPC_GSS_SVC_PRIVACY;
1800 static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1802 struct svc_cred *cr = &rqstp->rq_cred;
1804 if (!cl->cl_mach_cred)
1806 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1808 if (!svc_rqst_integrity_protected(rqstp))
1810 if (!cr->cr_principal)
1812 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1815 static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
1820 * This is opaque to client, so no need to byte-swap. Use
1821 * __force to keep sparse happy
1823 verf[0] = (__force __be32)get_seconds();
1824 verf[1] = (__force __be32)nn->clientid_counter;
1825 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
1828 static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
1830 clp->cl_clientid.cl_boot = nn->boot_time;
1831 clp->cl_clientid.cl_id = nn->clientid_counter++;
1832 gen_confirm(clp, nn);
1835 static struct nfs4_stid *
1836 find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
1838 struct nfs4_stid *ret;
1840 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1841 if (!ret || !ret->sc_type)
1846 static struct nfs4_stid *
1847 find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
1849 struct nfs4_stid *s;
1851 spin_lock(&cl->cl_lock);
1852 s = find_stateid_locked(cl, t);
1854 if (typemask & s->sc_type)
1855 atomic_inc(&s->sc_count);
1859 spin_unlock(&cl->cl_lock);
1863 static struct nfs4_client *create_client(struct xdr_netobj name,
1864 struct svc_rqst *rqstp, nfs4_verifier *verf)
1866 struct nfs4_client *clp;
1867 struct sockaddr *sa = svc_addr(rqstp);
1869 struct net *net = SVC_NET(rqstp);
1871 clp = alloc_client(name);
1875 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1880 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_run_cb_null);
1881 clp->cl_time = get_seconds();
1882 clear_bit(0, &clp->cl_cb_slot_busy);
1883 copy_verf(clp, verf);
1884 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
1885 clp->cl_cb_session = NULL;
1891 add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1893 struct rb_node **new = &(root->rb_node), *parent = NULL;
1894 struct nfs4_client *clp;
1897 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1900 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1901 new = &((*new)->rb_left);
1903 new = &((*new)->rb_right);
1906 rb_link_node(&new_clp->cl_namenode, parent, new);
1907 rb_insert_color(&new_clp->cl_namenode, root);
1910 static struct nfs4_client *
1911 find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1914 struct rb_node *node = root->rb_node;
1915 struct nfs4_client *clp;
1918 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1919 cmp = compare_blob(&clp->cl_name, name);
1921 node = node->rb_left;
1923 node = node->rb_right;
1931 add_to_unconfirmed(struct nfs4_client *clp)
1933 unsigned int idhashval;
1934 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1936 lockdep_assert_held(&nn->client_lock);
1938 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
1939 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
1940 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1941 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
1942 renew_client_locked(clp);
1946 move_to_confirmed(struct nfs4_client *clp)
1948 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1949 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1951 lockdep_assert_held(&nn->client_lock);
1953 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
1954 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
1955 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1956 add_clp_to_name_tree(clp, &nn->conf_name_tree);
1957 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
1958 renew_client_locked(clp);
1961 static struct nfs4_client *
1962 find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
1964 struct nfs4_client *clp;
1965 unsigned int idhashval = clientid_hashval(clid->cl_id);
1967 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
1968 if (same_clid(&clp->cl_clientid, clid)) {
1969 if ((bool)clp->cl_minorversion != sessions)
1971 renew_client_locked(clp);
1978 static struct nfs4_client *
1979 find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1981 struct list_head *tbl = nn->conf_id_hashtbl;
1983 lockdep_assert_held(&nn->client_lock);
1984 return find_client_in_id_table(tbl, clid, sessions);
1987 static struct nfs4_client *
1988 find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1990 struct list_head *tbl = nn->unconf_id_hashtbl;
1992 lockdep_assert_held(&nn->client_lock);
1993 return find_client_in_id_table(tbl, clid, sessions);
1996 static bool clp_used_exchangeid(struct nfs4_client *clp)
1998 return clp->cl_exchange_flags != 0;
2001 static struct nfs4_client *
2002 find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
2004 lockdep_assert_held(&nn->client_lock);
2005 return find_clp_in_name_tree(name, &nn->conf_name_tree);
2008 static struct nfs4_client *
2009 find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
2011 lockdep_assert_held(&nn->client_lock);
2012 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
2016 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
2018 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
2019 struct sockaddr *sa = svc_addr(rqstp);
2020 u32 scopeid = rpc_get_scope_id(sa);
2021 unsigned short expected_family;
2023 /* Currently, we only support tcp and tcp6 for the callback channel */
2024 if (se->se_callback_netid_len == 3 &&
2025 !memcmp(se->se_callback_netid_val, "tcp", 3))
2026 expected_family = AF_INET;
2027 else if (se->se_callback_netid_len == 4 &&
2028 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2029 expected_family = AF_INET6;
2033 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
2034 se->se_callback_addr_len,
2035 (struct sockaddr *)&conn->cb_addr,
2036 sizeof(conn->cb_addr));
2038 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
2041 if (conn->cb_addr.ss_family == AF_INET6)
2042 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
2044 conn->cb_prog = se->se_callback_prog;
2045 conn->cb_ident = se->se_callback_ident;
2046 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
2049 conn->cb_addr.ss_family = AF_UNSPEC;
2050 conn->cb_addrlen = 0;
2051 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
2052 "will not receive delegations\n",
2053 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2059 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
2062 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2064 struct xdr_buf *buf = resp->xdr.buf;
2065 struct nfsd4_slot *slot = resp->cstate.slot;
2068 dprintk("--> %s slot %p\n", __func__, slot);
2070 slot->sl_opcnt = resp->opcnt;
2071 slot->sl_status = resp->cstate.status;
2073 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
2074 if (nfsd4_not_cached(resp)) {
2075 slot->sl_datalen = 0;
2078 base = resp->cstate.data_offset;
2079 slot->sl_datalen = buf->len - base;
2080 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
2081 WARN("%s: sessions DRC could not cache compound\n", __func__);
2086 * Encode the replay sequence operation from the slot values.
2087 * If cachethis is FALSE encode the uncached rep error on the next
2088 * operation which sets resp->p and increments resp->opcnt for
2089 * nfs4svc_encode_compoundres.
2093 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2094 struct nfsd4_compoundres *resp)
2096 struct nfsd4_op *op;
2097 struct nfsd4_slot *slot = resp->cstate.slot;
2099 /* Encode the replayed sequence operation */
2100 op = &args->ops[resp->opcnt - 1];
2101 nfsd4_encode_operation(resp, op);
2103 /* Return nfserr_retry_uncached_rep in next operation. */
2104 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
2105 op = &args->ops[resp->opcnt++];
2106 op->status = nfserr_retry_uncached_rep;
2107 nfsd4_encode_operation(resp, op);
2113 * The sequence operation is not cached because we can use the slot and
2117 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2118 struct nfsd4_sequence *seq)
2120 struct nfsd4_slot *slot = resp->cstate.slot;
2121 struct xdr_stream *xdr = &resp->xdr;
2125 dprintk("--> %s slot %p\n", __func__, slot);
2127 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
2131 p = xdr_reserve_space(xdr, slot->sl_datalen);
2134 return nfserr_serverfault;
2136 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2137 xdr_commit_encode(xdr);
2139 resp->opcnt = slot->sl_opcnt;
2140 return slot->sl_status;
2144 * Set the exchange_id flags returned by the server.
2147 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2149 /* pNFS is not supported */
2150 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
2152 /* Referrals are supported, Migration is not. */
2153 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2155 /* set the wire flags to return to client. */
2156 clid->flags = new->cl_exchange_flags;
2159 static bool client_has_state(struct nfs4_client *clp)
2162 * Note clp->cl_openowners check isn't quite right: there's no
2163 * need to count owners without stateid's.
2165 * Also note we should probably be using this in 4.0 case too.
2167 return !list_empty(&clp->cl_openowners)
2168 || !list_empty(&clp->cl_delegations)
2169 || !list_empty(&clp->cl_sessions);
2173 nfsd4_exchange_id(struct svc_rqst *rqstp,
2174 struct nfsd4_compound_state *cstate,
2175 struct nfsd4_exchange_id *exid)
2177 struct nfs4_client *conf, *new;
2178 struct nfs4_client *unconf = NULL;
2180 char addr_str[INET6_ADDRSTRLEN];
2181 nfs4_verifier verf = exid->verifier;
2182 struct sockaddr *sa = svc_addr(rqstp);
2183 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
2184 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2186 rpc_ntop(sa, addr_str, sizeof(addr_str));
2187 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
2188 "ip_addr=%s flags %x, spa_how %d\n",
2189 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
2190 addr_str, exid->flags, exid->spa_how);
2192 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
2193 return nfserr_inval;
2195 switch (exid->spa_how) {
2197 if (!svc_rqst_integrity_protected(rqstp))
2198 return nfserr_inval;
2201 default: /* checked by xdr code */
2204 return nfserr_encr_alg_unsupp;
2207 new = create_client(exid->clname, rqstp, &verf);
2209 return nfserr_jukebox;
2211 /* Cases below refer to rfc 5661 section 18.35.4: */
2212 spin_lock(&nn->client_lock);
2213 conf = find_confirmed_client_by_name(&exid->clname, nn);
2215 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2216 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2219 if (!clp_used_exchangeid(conf)) { /* buggy client */
2220 status = nfserr_inval;
2223 if (!mach_creds_match(conf, rqstp)) {
2224 status = nfserr_wrong_cred;
2227 if (!creds_match) { /* case 9 */
2228 status = nfserr_perm;
2231 if (!verfs_match) { /* case 8 */
2232 status = nfserr_not_same;
2236 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
2239 if (!creds_match) { /* case 3 */
2240 if (client_has_state(conf)) {
2241 status = nfserr_clid_inuse;
2246 if (verfs_match) { /* case 2 */
2247 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
2250 /* case 5, client reboot */
2255 if (update) { /* case 7 */
2256 status = nfserr_noent;
2260 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
2261 if (unconf) /* case 4, possible retry or client restart */
2262 unhash_client_locked(unconf);
2264 /* case 1 (normal case) */
2267 status = mark_client_expired_locked(conf);
2271 new->cl_minorversion = cstate->minorversion;
2272 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
2275 add_to_unconfirmed(new);
2278 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
2279 exid->clientid.cl_id = conf->cl_clientid.cl_id;
2281 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
2282 nfsd4_set_ex_flags(conf, exid);
2284 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
2285 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
2289 spin_unlock(&nn->client_lock);
2293 expire_client(unconf);
2298 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
2300 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2303 /* The slot is in use, and no response has been sent. */
2305 if (seqid == slot_seqid)
2306 return nfserr_jukebox;
2308 return nfserr_seq_misordered;
2310 /* Note unsigned 32-bit arithmetic handles wraparound: */
2311 if (likely(seqid == slot_seqid + 1))
2313 if (seqid == slot_seqid)
2314 return nfserr_replay_cache;
2315 return nfserr_seq_misordered;
2319 * Cache the create session result into the create session single DRC
2320 * slot cache by saving the xdr structure. sl_seqid has been set.
2321 * Do this for solo or embedded create session operations.
2324 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
2325 struct nfsd4_clid_slot *slot, __be32 nfserr)
2327 slot->sl_status = nfserr;
2328 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2332 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2333 struct nfsd4_clid_slot *slot)
2335 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2336 return slot->sl_status;
2339 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2340 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2341 1 + /* MIN tag is length with zero, only length */ \
2342 3 + /* version, opcount, opcode */ \
2343 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2344 /* seqid, slotID, slotID, cache */ \
2345 4 ) * sizeof(__be32))
2347 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2348 2 + /* verifier: AUTH_NULL, length 0 */\
2350 1 + /* MIN tag is length with zero, only length */ \
2351 3 + /* opcount, opcode, opstatus*/ \
2352 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2353 /* seqid, slotID, slotID, slotID, status */ \
2354 5 ) * sizeof(__be32))
2356 static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
2358 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2360 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2361 return nfserr_toosmall;
2362 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2363 return nfserr_toosmall;
2364 ca->headerpadsz = 0;
2365 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2366 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2367 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2368 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2369 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2370 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2372 * Note decreasing slot size below client's request may make it
2373 * difficult for client to function correctly, whereas
2374 * decreasing the number of slots will (just?) affect
2375 * performance. When short on memory we therefore prefer to
2376 * decrease number of slots instead of their size. Clients that
2377 * request larger slots than they need will get poor results:
2379 ca->maxreqs = nfsd4_get_drc_mem(ca);
2381 return nfserr_jukebox;
2386 #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2387 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2388 #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2389 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2391 static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2393 ca->headerpadsz = 0;
2396 * These RPC_MAX_HEADER macros are overkill, especially since we
2397 * don't even do gss on the backchannel yet. But this is still
2398 * less than 1k. Tighten up this estimate in the unlikely event
2399 * it turns out to be a problem for some client:
2401 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
2402 return nfserr_toosmall;
2403 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
2404 return nfserr_toosmall;
2405 ca->maxresp_cached = 0;
2407 return nfserr_toosmall;
2412 static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2414 switch (cbs->flavor) {
2420 * GSS case: the spec doesn't allow us to return this
2421 * error. But it also doesn't allow us not to support
2423 * I'd rather this fail hard than return some error the
2424 * client might think it can already handle:
2426 return nfserr_encr_alg_unsupp;
2431 nfsd4_create_session(struct svc_rqst *rqstp,
2432 struct nfsd4_compound_state *cstate,
2433 struct nfsd4_create_session *cr_ses)
2435 struct sockaddr *sa = svc_addr(rqstp);
2436 struct nfs4_client *conf, *unconf;
2437 struct nfs4_client *old = NULL;
2438 struct nfsd4_session *new;
2439 struct nfsd4_conn *conn;
2440 struct nfsd4_clid_slot *cs_slot = NULL;
2442 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2444 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2445 return nfserr_inval;
2446 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2449 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
2452 status = check_backchannel_attrs(&cr_ses->back_channel);
2454 goto out_release_drc_mem;
2455 status = nfserr_jukebox;
2456 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
2458 goto out_release_drc_mem;
2459 conn = alloc_conn_from_crses(rqstp, cr_ses);
2461 goto out_free_session;
2463 spin_lock(&nn->client_lock);
2464 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
2465 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
2466 WARN_ON_ONCE(conf && unconf);
2469 status = nfserr_wrong_cred;
2470 if (!mach_creds_match(conf, rqstp))
2472 cs_slot = &conf->cl_cs_slot;
2473 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2474 if (status == nfserr_replay_cache) {
2475 status = nfsd4_replay_create_session(cr_ses, cs_slot);
2477 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
2478 status = nfserr_seq_misordered;
2481 } else if (unconf) {
2482 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
2483 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
2484 status = nfserr_clid_inuse;
2487 status = nfserr_wrong_cred;
2488 if (!mach_creds_match(unconf, rqstp))
2490 cs_slot = &unconf->cl_cs_slot;
2491 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2493 /* an unconfirmed replay returns misordered */
2494 status = nfserr_seq_misordered;
2497 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
2499 status = mark_client_expired_locked(old);
2505 move_to_confirmed(unconf);
2508 status = nfserr_stale_clientid;
2513 * We do not support RDMA or persistent sessions
2515 cr_ses->flags &= ~SESSION4_PERSIST;
2516 cr_ses->flags &= ~SESSION4_RDMA;
2518 init_session(rqstp, new, conf, cr_ses);
2519 nfsd4_get_session_locked(new);
2521 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
2522 NFS4_MAX_SESSIONID_LEN);
2523 cs_slot->sl_seqid++;
2524 cr_ses->seqid = cs_slot->sl_seqid;
2526 /* cache solo and embedded create sessions under the client_lock */
2527 nfsd4_cache_create_session(cr_ses, cs_slot, status);
2528 spin_unlock(&nn->client_lock);
2529 /* init connection and backchannel */
2530 nfsd4_init_conn(rqstp, conn, new);
2531 nfsd4_put_session(new);
2536 spin_unlock(&nn->client_lock);
2541 __free_session(new);
2542 out_release_drc_mem:
2543 nfsd4_put_drc_mem(&cr_ses->fore_channel);
2547 static __be32 nfsd4_map_bcts_dir(u32 *dir)
2550 case NFS4_CDFC4_FORE:
2551 case NFS4_CDFC4_BACK:
2553 case NFS4_CDFC4_FORE_OR_BOTH:
2554 case NFS4_CDFC4_BACK_OR_BOTH:
2555 *dir = NFS4_CDFC4_BOTH;
2558 return nfserr_inval;
2561 __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2563 struct nfsd4_session *session = cstate->session;
2564 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2567 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2570 spin_lock(&nn->client_lock);
2571 session->se_cb_prog = bc->bc_cb_program;
2572 session->se_cb_sec = bc->bc_cb_sec;
2573 spin_unlock(&nn->client_lock);
2575 nfsd4_probe_callback(session->se_client);
2580 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2581 struct nfsd4_compound_state *cstate,
2582 struct nfsd4_bind_conn_to_session *bcts)
2585 struct nfsd4_conn *conn;
2586 struct nfsd4_session *session;
2587 struct net *net = SVC_NET(rqstp);
2588 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2590 if (!nfsd4_last_compound_op(rqstp))
2591 return nfserr_not_only_op;
2592 spin_lock(&nn->client_lock);
2593 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
2594 spin_unlock(&nn->client_lock);
2596 goto out_no_session;
2597 status = nfserr_wrong_cred;
2598 if (!mach_creds_match(session->se_client, rqstp))
2600 status = nfsd4_map_bcts_dir(&bcts->dir);
2603 conn = alloc_conn(rqstp, bcts->dir);
2604 status = nfserr_jukebox;
2607 nfsd4_init_conn(rqstp, conn, session);
2610 nfsd4_put_session(session);
2615 static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2619 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2623 nfsd4_destroy_session(struct svc_rqst *r,
2624 struct nfsd4_compound_state *cstate,
2625 struct nfsd4_destroy_session *sessionid)
2627 struct nfsd4_session *ses;
2629 int ref_held_by_me = 0;
2630 struct net *net = SVC_NET(r);
2631 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2633 status = nfserr_not_only_op;
2634 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
2635 if (!nfsd4_last_compound_op(r))
2639 dump_sessionid(__func__, &sessionid->sessionid);
2640 spin_lock(&nn->client_lock);
2641 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
2643 goto out_client_lock;
2644 status = nfserr_wrong_cred;
2645 if (!mach_creds_match(ses->se_client, r))
2646 goto out_put_session;
2647 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
2649 goto out_put_session;
2650 unhash_session(ses);
2651 spin_unlock(&nn->client_lock);
2653 nfsd4_probe_callback_sync(ses->se_client);
2655 spin_lock(&nn->client_lock);
2658 nfsd4_put_session_locked(ses);
2660 spin_unlock(&nn->client_lock);
2665 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
2667 struct nfsd4_conn *c;
2669 list_for_each_entry(c, &s->se_conns, cn_persession) {
2670 if (c->cn_xprt == xpt) {
2677 static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
2679 struct nfs4_client *clp = ses->se_client;
2680 struct nfsd4_conn *c;
2681 __be32 status = nfs_ok;
2684 spin_lock(&clp->cl_lock);
2685 c = __nfsd4_find_conn(new->cn_xprt, ses);
2688 status = nfserr_conn_not_bound_to_session;
2689 if (clp->cl_mach_cred)
2691 __nfsd4_hash_conn(new, ses);
2692 spin_unlock(&clp->cl_lock);
2693 ret = nfsd4_register_conn(new);
2695 /* oops; xprt is already down: */
2696 nfsd4_conn_lost(&new->cn_xpt_user);
2699 spin_unlock(&clp->cl_lock);
2704 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2706 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2708 return args->opcnt > session->se_fchannel.maxops;
2711 static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2712 struct nfsd4_session *session)
2714 struct xdr_buf *xb = &rqstp->rq_arg;
2716 return xb->len > session->se_fchannel.maxreq_sz;
2720 nfsd4_sequence(struct svc_rqst *rqstp,
2721 struct nfsd4_compound_state *cstate,
2722 struct nfsd4_sequence *seq)
2724 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2725 struct xdr_stream *xdr = &resp->xdr;
2726 struct nfsd4_session *session;
2727 struct nfs4_client *clp;
2728 struct nfsd4_slot *slot;
2729 struct nfsd4_conn *conn;
2732 struct net *net = SVC_NET(rqstp);
2733 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2735 if (resp->opcnt != 1)
2736 return nfserr_sequence_pos;
2739 * Will be either used or freed by nfsd4_sequence_check_conn
2742 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2744 return nfserr_jukebox;
2746 spin_lock(&nn->client_lock);
2747 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
2749 goto out_no_session;
2750 clp = session->se_client;
2752 status = nfserr_too_many_ops;
2753 if (nfsd4_session_too_many_ops(rqstp, session))
2754 goto out_put_session;
2756 status = nfserr_req_too_big;
2757 if (nfsd4_request_too_big(rqstp, session))
2758 goto out_put_session;
2760 status = nfserr_badslot;
2761 if (seq->slotid >= session->se_fchannel.maxreqs)
2762 goto out_put_session;
2764 slot = session->se_slots[seq->slotid];
2765 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2767 /* We do not negotiate the number of slots yet, so set the
2768 * maxslots to the session maxreqs which is used to encode
2769 * sr_highest_slotid and the sr_target_slot id to maxslots */
2770 seq->maxslots = session->se_fchannel.maxreqs;
2772 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2773 slot->sl_flags & NFSD4_SLOT_INUSE);
2774 if (status == nfserr_replay_cache) {
2775 status = nfserr_seq_misordered;
2776 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
2777 goto out_put_session;
2778 cstate->slot = slot;
2779 cstate->session = session;
2781 /* Return the cached reply status and set cstate->status
2782 * for nfsd4_proc_compound processing */
2783 status = nfsd4_replay_cache_entry(resp, seq);
2784 cstate->status = nfserr_replay_cache;
2788 goto out_put_session;
2790 status = nfsd4_sequence_check_conn(conn, session);
2793 goto out_put_session;
2795 buflen = (seq->cachethis) ?
2796 session->se_fchannel.maxresp_cached :
2797 session->se_fchannel.maxresp_sz;
2798 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2800 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
2801 goto out_put_session;
2802 svc_reserve(rqstp, buflen);
2805 /* Success! bump slot seqid */
2806 slot->sl_seqid = seq->seqid;
2807 slot->sl_flags |= NFSD4_SLOT_INUSE;
2809 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
2811 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
2813 cstate->slot = slot;
2814 cstate->session = session;
2818 switch (clp->cl_cb_state) {
2820 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2822 case NFSD4_CB_FAULT:
2823 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2826 seq->status_flags = 0;
2828 if (!list_empty(&clp->cl_revoked))
2829 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
2833 spin_unlock(&nn->client_lock);
2836 nfsd4_put_session_locked(session);
2837 goto out_no_session;
2841 nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2843 struct nfsd4_compound_state *cs = &resp->cstate;
2845 if (nfsd4_has_session(cs)) {
2846 if (cs->status != nfserr_replay_cache) {
2847 nfsd4_store_cache_entry(resp);
2848 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2850 /* Drop session reference that was taken in nfsd4_sequence() */
2851 nfsd4_put_session(cs->session);
2853 put_client_renew(cs->clp);
2857 nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2859 struct nfs4_client *conf, *unconf;
2860 struct nfs4_client *clp = NULL;
2862 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2864 spin_lock(&nn->client_lock);
2865 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
2866 conf = find_confirmed_client(&dc->clientid, true, nn);
2867 WARN_ON_ONCE(conf && unconf);
2870 if (client_has_state(conf)) {
2871 status = nfserr_clientid_busy;
2874 status = mark_client_expired_locked(conf);
2881 status = nfserr_stale_clientid;
2884 if (!mach_creds_match(clp, rqstp)) {
2886 status = nfserr_wrong_cred;
2889 unhash_client_locked(clp);
2891 spin_unlock(&nn->client_lock);
2898 nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2902 if (rc->rca_one_fs) {
2903 if (!cstate->current_fh.fh_dentry)
2904 return nfserr_nofilehandle;
2906 * We don't take advantage of the rca_one_fs case.
2907 * That's OK, it's optional, we can safely ignore it.
2912 status = nfserr_complete_already;
2913 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2914 &cstate->session->se_client->cl_flags))
2917 status = nfserr_stale_clientid;
2918 if (is_client_expired(cstate->session->se_client))
2920 * The following error isn't really legal.
2921 * But we only get here if the client just explicitly
2922 * destroyed the client. Surely it no longer cares what
2923 * error it gets back on an operation for the dead
2929 nfsd4_client_record_create(cstate->session->se_client);
2935 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2936 struct nfsd4_setclientid *setclid)
2938 struct xdr_netobj clname = setclid->se_name;
2939 nfs4_verifier clverifier = setclid->se_verf;
2940 struct nfs4_client *conf, *new;
2941 struct nfs4_client *unconf = NULL;
2943 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2945 new = create_client(clname, rqstp, &clverifier);
2947 return nfserr_jukebox;
2948 /* Cases below refer to rfc 3530 section 14.2.33: */
2949 spin_lock(&nn->client_lock);
2950 conf = find_confirmed_client_by_name(&clname, nn);
2953 status = nfserr_clid_inuse;
2954 if (clp_used_exchangeid(conf))
2956 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
2957 char addr_str[INET6_ADDRSTRLEN];
2958 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2960 dprintk("NFSD: setclientid: string in use by client "
2961 "at %s\n", addr_str);
2965 unconf = find_unconfirmed_client_by_name(&clname, nn);
2967 unhash_client_locked(unconf);
2968 if (conf && same_verf(&conf->cl_verifier, &clverifier))
2969 /* case 1: probable callback update */
2970 copy_clid(new, conf);
2971 else /* case 4 (new client) or cases 2, 3 (client reboot): */
2973 new->cl_minorversion = 0;
2974 gen_callback(new, setclid, rqstp);
2975 add_to_unconfirmed(new);
2976 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2977 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2978 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2982 spin_unlock(&nn->client_lock);
2986 expire_client(unconf);
2992 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2993 struct nfsd4_compound_state *cstate,
2994 struct nfsd4_setclientid_confirm *setclientid_confirm)
2996 struct nfs4_client *conf, *unconf;
2997 struct nfs4_client *old = NULL;
2998 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2999 clientid_t * clid = &setclientid_confirm->sc_clientid;
3001 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3003 if (STALE_CLIENTID(clid, nn))
3004 return nfserr_stale_clientid;
3006 spin_lock(&nn->client_lock);
3007 conf = find_confirmed_client(clid, false, nn);
3008 unconf = find_unconfirmed_client(clid, false, nn);
3010 * We try hard to give out unique clientid's, so if we get an
3011 * attempt to confirm the same clientid with a different cred,
3012 * there's a bug somewhere. Let's charitably assume it's our
3015 status = nfserr_serverfault;
3016 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3018 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3020 /* cases below refer to rfc 3530 section 14.2.34: */
3021 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
3022 if (conf && !unconf) /* case 2: probable retransmit */
3024 else /* case 4: client hasn't noticed we rebooted yet? */
3025 status = nfserr_stale_clientid;
3029 if (conf) { /* case 1: callback update */
3031 unhash_client_locked(old);
3032 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
3033 } else { /* case 3: normal case; new or rebooted client */
3034 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3036 status = mark_client_expired_locked(old);
3042 move_to_confirmed(unconf);
3045 get_client_locked(conf);
3046 spin_unlock(&nn->client_lock);
3047 nfsd4_probe_callback(conf);
3048 spin_lock(&nn->client_lock);
3049 put_client_renew_locked(conf);
3051 spin_unlock(&nn->client_lock);
3057 static struct nfs4_file *nfsd4_alloc_file(void)
3059 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3062 /* OPEN Share state helper functions */
3063 static void nfsd4_init_file(struct nfs4_file *fp, struct knfsd_fh *fh)
3065 unsigned int hashval = file_hashval(fh);
3067 lockdep_assert_held(&state_lock);
3069 atomic_set(&fp->fi_ref, 1);
3070 spin_lock_init(&fp->fi_lock);
3071 INIT_LIST_HEAD(&fp->fi_stateids);
3072 INIT_LIST_HEAD(&fp->fi_delegations);
3073 fh_copy_shallow(&fp->fi_fhandle, fh);
3074 fp->fi_had_conflict = false;
3075 fp->fi_lease = NULL;
3076 fp->fi_share_deny = 0;
3077 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3078 memset(fp->fi_access, 0, sizeof(fp->fi_access));
3079 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
3083 nfsd4_free_slabs(void)
3085 kmem_cache_destroy(openowner_slab);
3086 kmem_cache_destroy(lockowner_slab);
3087 kmem_cache_destroy(file_slab);
3088 kmem_cache_destroy(stateid_slab);
3089 kmem_cache_destroy(deleg_slab);
3093 nfsd4_init_slabs(void)
3095 openowner_slab = kmem_cache_create("nfsd4_openowners",
3096 sizeof(struct nfs4_openowner), 0, 0, NULL);
3097 if (openowner_slab == NULL)
3099 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
3100 sizeof(struct nfs4_lockowner), 0, 0, NULL);
3101 if (lockowner_slab == NULL)
3102 goto out_free_openowner_slab;
3103 file_slab = kmem_cache_create("nfsd4_files",
3104 sizeof(struct nfs4_file), 0, 0, NULL);
3105 if (file_slab == NULL)
3106 goto out_free_lockowner_slab;
3107 stateid_slab = kmem_cache_create("nfsd4_stateids",
3108 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
3109 if (stateid_slab == NULL)
3110 goto out_free_file_slab;
3111 deleg_slab = kmem_cache_create("nfsd4_delegations",
3112 sizeof(struct nfs4_delegation), 0, 0, NULL);
3113 if (deleg_slab == NULL)
3114 goto out_free_stateid_slab;
3117 out_free_stateid_slab:
3118 kmem_cache_destroy(stateid_slab);
3120 kmem_cache_destroy(file_slab);
3121 out_free_lockowner_slab:
3122 kmem_cache_destroy(lockowner_slab);
3123 out_free_openowner_slab:
3124 kmem_cache_destroy(openowner_slab);
3126 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3130 static void init_nfs4_replay(struct nfs4_replay *rp)
3132 rp->rp_status = nfserr_serverfault;
3134 rp->rp_buf = rp->rp_ibuf;
3135 mutex_init(&rp->rp_mutex);
3138 static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3139 struct nfs4_stateowner *so)
3141 if (!nfsd4_has_session(cstate)) {
3142 mutex_lock(&so->so_replay.rp_mutex);
3143 cstate->replay_owner = so;
3144 atomic_inc(&so->so_count);
3148 void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3150 struct nfs4_stateowner *so = cstate->replay_owner;
3153 cstate->replay_owner = NULL;
3154 mutex_unlock(&so->so_replay.rp_mutex);
3155 nfs4_put_stateowner(so);
3159 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
3161 struct nfs4_stateowner *sop;
3163 sop = kmem_cache_alloc(slab, GFP_KERNEL);
3167 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3168 if (!sop->so_owner.data) {
3169 kmem_cache_free(slab, sop);
3172 sop->so_owner.len = owner->len;
3174 INIT_LIST_HEAD(&sop->so_stateids);
3175 sop->so_client = clp;
3176 init_nfs4_replay(&sop->so_replay);
3177 atomic_set(&sop->so_count, 1);
3181 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
3183 lockdep_assert_held(&clp->cl_lock);
3185 list_add(&oo->oo_owner.so_strhash,
3186 &clp->cl_ownerstr_hashtbl[strhashval]);
3187 list_add(&oo->oo_perclient, &clp->cl_openowners);
3190 static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3192 unhash_openowner_locked(openowner(so));
3195 static void nfs4_free_openowner(struct nfs4_stateowner *so)
3197 struct nfs4_openowner *oo = openowner(so);
3199 kmem_cache_free(openowner_slab, oo);
3202 static const struct nfs4_stateowner_operations openowner_ops = {
3203 .so_unhash = nfs4_unhash_openowner,
3204 .so_free = nfs4_free_openowner,
3207 static struct nfs4_openowner *
3208 alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
3209 struct nfsd4_compound_state *cstate)
3211 struct nfs4_client *clp = cstate->clp;
3212 struct nfs4_openowner *oo, *ret;
3214 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3217 oo->oo_owner.so_ops = &openowner_ops;
3218 oo->oo_owner.so_is_open_owner = 1;
3219 oo->oo_owner.so_seqid = open->op_seqid;
3221 if (nfsd4_has_session(cstate))
3222 oo->oo_flags |= NFS4_OO_CONFIRMED;
3224 oo->oo_last_closed_stid = NULL;
3225 INIT_LIST_HEAD(&oo->oo_close_lru);
3226 spin_lock(&clp->cl_lock);
3227 ret = find_openstateowner_str_locked(strhashval, open, clp);
3229 hash_openowner(oo, clp, strhashval);
3232 nfs4_free_openowner(&oo->oo_owner);
3233 spin_unlock(&clp->cl_lock);
3237 static void init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
3238 struct nfs4_openowner *oo = open->op_openowner;
3240 atomic_inc(&stp->st_stid.sc_count);
3241 stp->st_stid.sc_type = NFS4_OPEN_STID;
3242 INIT_LIST_HEAD(&stp->st_locks);
3243 stp->st_stateowner = &oo->oo_owner;
3244 atomic_inc(&stp->st_stateowner->so_count);
3246 stp->st_stid.sc_file = fp;
3247 stp->st_access_bmap = 0;
3248 stp->st_deny_bmap = 0;
3249 stp->st_openstp = NULL;
3250 spin_lock(&oo->oo_owner.so_client->cl_lock);
3251 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
3252 spin_lock(&fp->fi_lock);
3253 list_add(&stp->st_perfile, &fp->fi_stateids);
3254 spin_unlock(&fp->fi_lock);
3255 spin_unlock(&oo->oo_owner.so_client->cl_lock);
3259 * In the 4.0 case we need to keep the owners around a little while to handle
3260 * CLOSE replay. We still do need to release any file access that is held by
3261 * them before returning however.
3264 move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
3266 struct nfs4_ol_stateid *last;
3267 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3268 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3271 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
3274 * We know that we hold one reference via nfsd4_close, and another
3275 * "persistent" reference for the client. If the refcount is higher
3276 * than 2, then there are still calls in progress that are using this
3277 * stateid. We can't put the sc_file reference until they are finished.
3278 * Wait for the refcount to drop to 2. Since it has been unhashed,
3279 * there should be no danger of the refcount going back up again at
3282 wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3284 release_all_access(s);
3285 if (s->st_stid.sc_file) {
3286 put_nfs4_file(s->st_stid.sc_file);
3287 s->st_stid.sc_file = NULL;
3290 spin_lock(&nn->client_lock);
3291 last = oo->oo_last_closed_stid;
3292 oo->oo_last_closed_stid = s;
3293 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
3294 oo->oo_time = get_seconds();
3295 spin_unlock(&nn->client_lock);
3297 nfs4_put_stid(&last->st_stid);
3300 /* search file_hashtbl[] for file */
3301 static struct nfs4_file *
3302 find_file_locked(struct knfsd_fh *fh)
3304 unsigned int hashval = file_hashval(fh);
3305 struct nfs4_file *fp;
3307 lockdep_assert_held(&state_lock);
3309 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
3310 if (nfsd_fh_match(&fp->fi_fhandle, fh)) {
3318 static struct nfs4_file *
3319 find_file(struct knfsd_fh *fh)
3321 struct nfs4_file *fp;
3323 spin_lock(&state_lock);
3324 fp = find_file_locked(fh);
3325 spin_unlock(&state_lock);
3329 static struct nfs4_file *
3330 find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
3332 struct nfs4_file *fp;
3334 spin_lock(&state_lock);
3335 fp = find_file_locked(fh);
3337 nfsd4_init_file(new, fh);
3340 spin_unlock(&state_lock);
3346 * Called to check deny when READ with all zero stateid or
3347 * WRITE with all zero or all one stateid
3350 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3352 struct nfs4_file *fp;
3353 __be32 ret = nfs_ok;
3355 fp = find_file(¤t_fh->fh_handle);
3358 /* Check for conflicting share reservations */
3359 spin_lock(&fp->fi_lock);
3360 if (fp->fi_share_deny & deny_type)
3361 ret = nfserr_locked;
3362 spin_unlock(&fp->fi_lock);
3367 void nfsd4_prepare_cb_recall(struct nfs4_delegation *dp)
3369 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3372 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
3375 * We can't do this in nfsd_break_deleg_cb because it is
3376 * already holding inode->i_lock.
3378 * If the dl_time != 0, then we know that it has already been
3379 * queued for a lease break. Don't queue it again.
3381 spin_lock(&state_lock);
3382 if (dp->dl_time == 0) {
3383 dp->dl_time = get_seconds();
3384 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
3386 spin_unlock(&state_lock);
3389 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3392 * We're assuming the state code never drops its reference
3393 * without first removing the lease. Since we're in this lease
3394 * callback (and since the lease code is serialized by the kernel
3395 * lock) we know the server hasn't removed the lease yet, we know
3396 * it's safe to take a reference.
3398 atomic_inc(&dp->dl_stid.sc_count);
3399 nfsd4_cb_recall(dp);
3402 /* Called from break_lease() with i_lock held. */
3403 static void nfsd_break_deleg_cb(struct file_lock *fl)
3405 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3406 struct nfs4_delegation *dp;
3409 WARN(1, "(%p)->fl_owner NULL\n", fl);
3412 if (fp->fi_had_conflict) {
3413 WARN(1, "duplicate break on %p\n", fp);
3417 * We don't want the locks code to timeout the lease for us;
3418 * we'll remove it ourself if a delegation isn't returned
3421 fl->fl_break_time = 0;
3423 spin_lock(&fp->fi_lock);
3424 fp->fi_had_conflict = true;
3426 * If there are no delegations on the list, then we can't count on this
3427 * lease ever being cleaned up. Set the fl_break_time to jiffies so that
3428 * time_out_leases will do it ASAP. The fact that fi_had_conflict is now
3429 * true should keep any new delegations from being hashed.
3431 if (list_empty(&fp->fi_delegations))
3432 fl->fl_break_time = jiffies;
3434 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3435 nfsd_break_one_deleg(dp);
3436 spin_unlock(&fp->fi_lock);
3440 int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
3443 return lease_modify(onlist, arg);
3448 static const struct lock_manager_operations nfsd_lease_mng_ops = {
3449 .lm_break = nfsd_break_deleg_cb,
3450 .lm_change = nfsd_change_deleg_cb,
3453 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3455 if (nfsd4_has_session(cstate))
3457 if (seqid == so->so_seqid - 1)
3458 return nfserr_replay_me;
3459 if (seqid == so->so_seqid)
3461 return nfserr_bad_seqid;
3464 static __be32 lookup_clientid(clientid_t *clid,
3465 struct nfsd4_compound_state *cstate,
3466 struct nfsd_net *nn)
3468 struct nfs4_client *found;
3471 found = cstate->clp;
3472 if (!same_clid(&found->cl_clientid, clid))
3473 return nfserr_stale_clientid;
3477 if (STALE_CLIENTID(clid, nn))
3478 return nfserr_stale_clientid;
3481 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3482 * cached already then we know this is for is for v4.0 and "sessions"
3485 WARN_ON_ONCE(cstate->session);
3486 spin_lock(&nn->client_lock);
3487 found = find_confirmed_client(clid, false, nn);
3489 spin_unlock(&nn->client_lock);
3490 return nfserr_expired;
3492 atomic_inc(&found->cl_refcount);
3493 spin_unlock(&nn->client_lock);
3495 /* Cache the nfs4_client in cstate! */
3496 cstate->clp = found;
3501 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
3502 struct nfsd4_open *open, struct nfsd_net *nn)
3504 clientid_t *clientid = &open->op_clientid;
3505 struct nfs4_client *clp = NULL;
3506 unsigned int strhashval;
3507 struct nfs4_openowner *oo = NULL;
3510 if (STALE_CLIENTID(&open->op_clientid, nn))
3511 return nfserr_stale_clientid;
3513 * In case we need it later, after we've already created the
3514 * file and don't want to risk a further failure:
3516 open->op_file = nfsd4_alloc_file();
3517 if (open->op_file == NULL)
3518 return nfserr_jukebox;
3520 status = lookup_clientid(clientid, cstate, nn);
3525 strhashval = ownerstr_hashval(&open->op_owner);
3526 oo = find_openstateowner_str(strhashval, open, clp);
3527 open->op_openowner = oo;
3531 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
3532 /* Replace unconfirmed owners without checking for replay. */
3533 release_openowner(oo);
3534 open->op_openowner = NULL;
3537 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3542 oo = alloc_init_open_stateowner(strhashval, open, cstate);
3544 return nfserr_jukebox;
3545 open->op_openowner = oo;
3547 open->op_stp = nfs4_alloc_open_stateid(clp);
3549 return nfserr_jukebox;
3553 static inline __be32
3554 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3556 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3557 return nfserr_openmode;
3562 static int share_access_to_flags(u32 share_access)
3564 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3567 static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
3569 struct nfs4_stid *ret;
3571 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
3574 return delegstateid(ret);
3577 static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3579 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3580 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3584 nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
3585 struct nfs4_delegation **dp)
3588 __be32 status = nfserr_bad_stateid;
3589 struct nfs4_delegation *deleg;
3591 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3594 flags = share_access_to_flags(open->op_share_access);
3595 status = nfs4_check_delegmode(deleg, flags);
3597 nfs4_put_stid(&deleg->dl_stid);
3602 if (!nfsd4_is_deleg_cur(open))
3606 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
3610 static struct nfs4_ol_stateid *
3611 nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
3613 struct nfs4_ol_stateid *local, *ret = NULL;
3614 struct nfs4_openowner *oo = open->op_openowner;
3616 spin_lock(&fp->fi_lock);
3617 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
3618 /* ignore lock owners */
3619 if (local->st_stateowner->so_is_open_owner == 0)
3621 if (local->st_stateowner == &oo->oo_owner) {
3623 atomic_inc(&ret->st_stid.sc_count);
3627 spin_unlock(&fp->fi_lock);
3631 static inline int nfs4_access_to_access(u32 nfs4_access)
3635 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3636 flags |= NFSD_MAY_READ;
3637 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3638 flags |= NFSD_MAY_WRITE;
3642 static inline __be32
3643 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3644 struct nfsd4_open *open)
3646 struct iattr iattr = {
3647 .ia_valid = ATTR_SIZE,
3650 if (!open->op_truncate)
3652 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
3653 return nfserr_inval;
3654 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3657 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
3658 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3659 struct nfsd4_open *open)
3661 struct file *filp = NULL;
3663 int oflag = nfs4_access_to_omode(open->op_share_access);
3664 int access = nfs4_access_to_access(open->op_share_access);
3665 unsigned char old_access_bmap, old_deny_bmap;
3667 spin_lock(&fp->fi_lock);
3670 * Are we trying to set a deny mode that would conflict with
3673 status = nfs4_file_check_deny(fp, open->op_share_deny);
3674 if (status != nfs_ok) {
3675 spin_unlock(&fp->fi_lock);
3679 /* set access to the file */
3680 status = nfs4_file_get_access(fp, open->op_share_access);
3681 if (status != nfs_ok) {
3682 spin_unlock(&fp->fi_lock);
3686 /* Set access bits in stateid */
3687 old_access_bmap = stp->st_access_bmap;
3688 set_access(open->op_share_access, stp);
3690 /* Set new deny mask */
3691 old_deny_bmap = stp->st_deny_bmap;
3692 set_deny(open->op_share_deny, stp);
3693 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3695 if (!fp->fi_fds[oflag]) {
3696 spin_unlock(&fp->fi_lock);
3697 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
3699 goto out_put_access;
3700 spin_lock(&fp->fi_lock);
3701 if (!fp->fi_fds[oflag]) {
3702 fp->fi_fds[oflag] = filp;
3706 spin_unlock(&fp->fi_lock);
3710 status = nfsd4_truncate(rqstp, cur_fh, open);
3712 goto out_put_access;
3716 stp->st_access_bmap = old_access_bmap;
3717 nfs4_file_put_access(fp, open->op_share_access);
3718 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3723 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)
3726 unsigned char old_deny_bmap;
3728 if (!test_access(open->op_share_access, stp))
3729 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
3731 /* test and set deny mode */
3732 spin_lock(&fp->fi_lock);
3733 status = nfs4_file_check_deny(fp, open->op_share_deny);
3734 if (status == nfs_ok) {
3735 old_deny_bmap = stp->st_deny_bmap;
3736 set_deny(open->op_share_deny, stp);
3737 fp->fi_share_deny |=
3738 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3740 spin_unlock(&fp->fi_lock);
3742 if (status != nfs_ok)
3745 status = nfsd4_truncate(rqstp, cur_fh, open);
3746 if (status != nfs_ok)
3747 reset_union_bmap_deny(old_deny_bmap, stp);
3752 nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
3754 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
3757 /* Should we give out recallable state?: */
3758 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3760 if (clp->cl_cb_state == NFSD4_CB_UP)
3763 * In the sessions case, since we don't have to establish a
3764 * separate connection for callbacks, we assume it's OK
3765 * until we hear otherwise:
3767 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3770 static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
3772 struct file_lock *fl;
3774 fl = locks_alloc_lock();
3777 locks_init_lock(fl);
3778 fl->fl_lmops = &nfsd_lease_mng_ops;
3779 fl->fl_flags = FL_DELEG;
3780 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3781 fl->fl_end = OFFSET_MAX;
3782 fl->fl_owner = (fl_owner_t)fp;
3783 fl->fl_pid = current->tgid;
3787 static int nfs4_setlease(struct nfs4_delegation *dp)
3789 struct nfs4_file *fp = dp->dl_stid.sc_file;
3790 struct file_lock *fl;
3794 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
3797 filp = find_readable_file(fp);
3799 /* We should always have a readable file here */
3804 status = vfs_setlease(filp, fl->fl_type, &fl);
3806 locks_free_lock(fl);
3809 spin_lock(&state_lock);
3810 spin_lock(&fp->fi_lock);
3811 /* Did the lease get broken before we took the lock? */
3813 if (fp->fi_had_conflict)
3818 atomic_inc(&fp->fi_delegees);
3819 hash_delegation_locked(dp, fp);
3823 fp->fi_deleg_file = filp;
3824 atomic_set(&fp->fi_delegees, 1);
3825 hash_delegation_locked(dp, fp);
3826 spin_unlock(&fp->fi_lock);
3827 spin_unlock(&state_lock);
3830 spin_unlock(&fp->fi_lock);
3831 spin_unlock(&state_lock);
3837 static struct nfs4_delegation *
3838 nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
3839 struct nfs4_file *fp)
3842 struct nfs4_delegation *dp;
3844 if (fp->fi_had_conflict)
3845 return ERR_PTR(-EAGAIN);
3847 dp = alloc_init_deleg(clp, fh);
3849 return ERR_PTR(-ENOMEM);
3852 spin_lock(&state_lock);
3853 spin_lock(&fp->fi_lock);
3854 dp->dl_stid.sc_file = fp;
3855 if (!fp->fi_lease) {
3856 spin_unlock(&fp->fi_lock);
3857 spin_unlock(&state_lock);
3858 status = nfs4_setlease(dp);
3861 atomic_inc(&fp->fi_delegees);
3862 if (fp->fi_had_conflict) {
3866 hash_delegation_locked(dp, fp);
3869 spin_unlock(&fp->fi_lock);
3870 spin_unlock(&state_lock);
3873 nfs4_put_stid(&dp->dl_stid);
3874 return ERR_PTR(status);
3879 static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3881 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3882 if (status == -EAGAIN)
3883 open->op_why_no_deleg = WND4_CONTENTION;
3885 open->op_why_no_deleg = WND4_RESOURCE;
3886 switch (open->op_deleg_want) {
3887 case NFS4_SHARE_WANT_READ_DELEG:
3888 case NFS4_SHARE_WANT_WRITE_DELEG:
3889 case NFS4_SHARE_WANT_ANY_DELEG:
3891 case NFS4_SHARE_WANT_CANCEL:
3892 open->op_why_no_deleg = WND4_CANCELLED;
3894 case NFS4_SHARE_WANT_NO_DELEG:
3901 * Attempt to hand out a delegation.
3903 * Note we don't support write delegations, and won't until the vfs has
3904 * proper support for them.
3907 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
3908 struct nfs4_ol_stateid *stp)
3910 struct nfs4_delegation *dp;
3911 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
3912 struct nfs4_client *clp = stp->st_stid.sc_client;
3916 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
3917 open->op_recall = 0;
3918 switch (open->op_claim_type) {
3919 case NFS4_OPEN_CLAIM_PREVIOUS:
3921 open->op_recall = 1;
3922 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3925 case NFS4_OPEN_CLAIM_NULL:
3926 case NFS4_OPEN_CLAIM_FH:
3928 * Let's not give out any delegations till everyone's
3929 * had the chance to reclaim theirs....
3931 if (locks_in_grace(clp->net))
3933 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
3936 * Also, if the file was opened for write or
3937 * create, there's a good chance the client's
3938 * about to write to it, resulting in an
3939 * immediate recall (since we don't support
3940 * write delegations):
3942 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
3944 if (open->op_create == NFS4_OPEN_CREATE)
3950 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file);
3954 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
3956 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
3957 STATEID_VAL(&dp->dl_stid.sc_stateid));
3958 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
3959 nfs4_put_stid(&dp->dl_stid);
3962 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3963 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
3964 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
3965 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
3966 open->op_recall = 1;
3969 /* 4.1 client asking for a delegation? */
3970 if (open->op_deleg_want)
3971 nfsd4_open_deleg_none_ext(open, status);
3975 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3976 struct nfs4_delegation *dp)
3978 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3979 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3980 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3981 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3982 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3983 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3984 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3985 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3987 /* Otherwise the client must be confused wanting a delegation
3988 * it already has, therefore we don't return
3989 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3994 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3996 struct nfsd4_compoundres *resp = rqstp->rq_resp;
3997 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
3998 struct nfs4_file *fp = NULL;
3999 struct nfs4_ol_stateid *stp = NULL;
4000 struct nfs4_delegation *dp = NULL;
4004 * Lookup file; if found, lookup stateid and check open request,
4005 * and check for delegations in the process of being recalled.
4006 * If not found, create the nfs4_file struct
4008 fp = find_or_add_file(open->op_file, ¤t_fh->fh_handle);
4009 if (fp != open->op_file) {
4010 status = nfs4_check_deleg(cl, open, &dp);
4013 stp = nfsd4_find_existing_open(fp, open);
4015 open->op_file = NULL;
4016 status = nfserr_bad_stateid;
4017 if (nfsd4_is_deleg_cur(open))
4019 status = nfserr_jukebox;
4023 * OPEN the file, or upgrade an existing OPEN.
4024 * If truncate fails, the OPEN fails.
4027 /* Stateid was found, this is an OPEN upgrade */
4028 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
4033 open->op_stp = NULL;
4034 init_open_stateid(stp, fp, open);
4035 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4037 release_open_stateid(stp);
4041 update_stateid(&stp->st_stid.sc_stateid);
4042 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4044 if (nfsd4_has_session(&resp->cstate)) {
4045 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4046 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4047 open->op_why_no_deleg = WND4_NOT_WANTED;
4053 * Attempt to hand out a delegation. No error return, because the
4054 * OPEN succeeds even if we fail.
4056 nfs4_open_delegation(current_fh, open, stp);
4060 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
4061 STATEID_VAL(&stp->st_stid.sc_stateid));
4063 /* 4.1 client trying to upgrade/downgrade delegation? */
4064 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
4065 open->op_deleg_want)
4066 nfsd4_deleg_xgrade_none_ext(open, dp);
4070 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
4071 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
4073 * To finish the open response, we just need to set the rflags.
4075 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
4076 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
4077 !nfsd4_has_session(&resp->cstate))
4078 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
4080 nfs4_put_stid(&dp->dl_stid);
4082 nfs4_put_stid(&stp->st_stid);
4087 void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
4088 struct nfsd4_open *open, __be32 status)
4090 if (open->op_openowner) {
4091 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
4093 nfsd4_cstate_assign_replay(cstate, so);
4094 nfs4_put_stateowner(so);
4097 nfsd4_free_file(open->op_file);
4099 nfs4_put_stid(&open->op_stp->st_stid);
4103 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4106 struct nfs4_client *clp;
4108 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4110 dprintk("process_renew(%08x/%08x): starting\n",
4111 clid->cl_boot, clid->cl_id);
4112 status = lookup_clientid(clid, cstate, nn);
4116 status = nfserr_cb_path_down;
4117 if (!list_empty(&clp->cl_delegations)
4118 && clp->cl_cb_state != NFSD4_CB_UP)
4126 nfsd4_end_grace(struct nfsd_net *nn)
4128 /* do nothing if grace period already ended */
4129 if (nn->grace_ended)
4132 dprintk("NFSD: end of grace period\n");
4133 nn->grace_ended = true;
4134 nfsd4_record_grace_done(nn, nn->boot_time);
4135 locks_end_grace(&nn->nfsd4_manager);
4137 * Now that every NFSv4 client has had the chance to recover and
4138 * to see the (possibly new, possibly shorter) lease time, we
4139 * can safely set the next grace time to the current lease time:
4141 nn->nfsd4_grace = nn->nfsd4_lease;
4145 nfs4_laundromat(struct nfsd_net *nn)
4147 struct nfs4_client *clp;
4148 struct nfs4_openowner *oo;
4149 struct nfs4_delegation *dp;
4150 struct nfs4_ol_stateid *stp;
4151 struct list_head *pos, *next, reaplist;
4152 time_t cutoff = get_seconds() - nn->nfsd4_lease;
4153 time_t t, new_timeo = nn->nfsd4_lease;
4155 dprintk("NFSD: laundromat service - starting\n");
4156 nfsd4_end_grace(nn);
4157 INIT_LIST_HEAD(&reaplist);
4158 spin_lock(&nn->client_lock);
4159 list_for_each_safe(pos, next, &nn->client_lru) {
4160 clp = list_entry(pos, struct nfs4_client, cl_lru);
4161 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4162 t = clp->cl_time - cutoff;
4163 new_timeo = min(new_timeo, t);
4166 if (mark_client_expired_locked(clp)) {
4167 dprintk("NFSD: client in use (clientid %08x)\n",
4168 clp->cl_clientid.cl_id);
4171 list_add(&clp->cl_lru, &reaplist);
4173 spin_unlock(&nn->client_lock);
4174 list_for_each_safe(pos, next, &reaplist) {
4175 clp = list_entry(pos, struct nfs4_client, cl_lru);
4176 dprintk("NFSD: purging unused client (clientid %08x)\n",
4177 clp->cl_clientid.cl_id);
4178 list_del_init(&clp->cl_lru);
4181 spin_lock(&state_lock);
4182 list_for_each_safe(pos, next, &nn->del_recall_lru) {
4183 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4184 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
4186 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
4187 t = dp->dl_time - cutoff;
4188 new_timeo = min(new_timeo, t);
4191 unhash_delegation_locked(dp);
4192 list_add(&dp->dl_recall_lru, &reaplist);
4194 spin_unlock(&state_lock);
4195 while (!list_empty(&reaplist)) {
4196 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4198 list_del_init(&dp->dl_recall_lru);
4199 revoke_delegation(dp);
4202 spin_lock(&nn->client_lock);
4203 while (!list_empty(&nn->close_lru)) {
4204 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
4206 if (time_after((unsigned long)oo->oo_time,
4207 (unsigned long)cutoff)) {
4208 t = oo->oo_time - cutoff;
4209 new_timeo = min(new_timeo, t);
4212 list_del_init(&oo->oo_close_lru);
4213 stp = oo->oo_last_closed_stid;
4214 oo->oo_last_closed_stid = NULL;
4215 spin_unlock(&nn->client_lock);
4216 nfs4_put_stid(&stp->st_stid);
4217 spin_lock(&nn->client_lock);
4219 spin_unlock(&nn->client_lock);
4221 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
4225 static struct workqueue_struct *laundry_wq;
4226 static void laundromat_main(struct work_struct *);
4229 laundromat_main(struct work_struct *laundry)
4232 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
4234 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4237 t = nfs4_laundromat(nn);
4238 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
4239 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
4242 static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
4244 if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_stid.sc_file->fi_fhandle))
4245 return nfserr_bad_stateid;
4250 access_permit_read(struct nfs4_ol_stateid *stp)
4252 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4253 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4254 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
4258 access_permit_write(struct nfs4_ol_stateid *stp)
4260 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4261 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
4265 __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
4267 __be32 status = nfserr_openmode;
4269 /* For lock stateid's, we test the parent open, not the lock: */
4270 if (stp->st_openstp)
4271 stp = stp->st_openstp;
4272 if ((flags & WR_STATE) && !access_permit_write(stp))
4274 if ((flags & RD_STATE) && !access_permit_read(stp))
4281 static inline __be32
4282 check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
4284 if (ONE_STATEID(stateid) && (flags & RD_STATE))
4286 else if (locks_in_grace(net)) {
4287 /* Answer in remaining cases depends on existence of
4288 * conflicting state; so we must wait out the grace period. */
4289 return nfserr_grace;
4290 } else if (flags & WR_STATE)
4291 return nfs4_share_conflict(current_fh,
4292 NFS4_SHARE_DENY_WRITE);
4293 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4294 return nfs4_share_conflict(current_fh,
4295 NFS4_SHARE_DENY_READ);
4299 * Allow READ/WRITE during grace period on recovered state only for files
4300 * that are not able to provide mandatory locking.
4303 grace_disallows_io(struct net *net, struct inode *inode)
4305 return locks_in_grace(net) && mandatory_lock(inode);
4308 /* Returns true iff a is later than b: */
4309 static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4311 return (s32)(a->si_generation - b->si_generation) > 0;
4314 static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
4317 * When sessions are used the stateid generation number is ignored
4320 if (has_session && in->si_generation == 0)
4323 if (in->si_generation == ref->si_generation)
4326 /* If the client sends us a stateid from the future, it's buggy: */
4327 if (stateid_generation_after(in, ref))
4328 return nfserr_bad_stateid;
4330 * However, we could see a stateid from the past, even from a
4331 * non-buggy client. For example, if the client sends a lock
4332 * while some IO is outstanding, the lock may bump si_generation
4333 * while the IO is still in flight. The client could avoid that
4334 * situation by waiting for responses on all the IO requests,
4335 * but better performance may result in retrying IO that
4336 * receives an old_stateid error if requests are rarely
4337 * reordered in flight:
4339 return nfserr_old_stateid;
4342 static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
4344 struct nfs4_stid *s;
4345 struct nfs4_ol_stateid *ols;
4346 __be32 status = nfserr_bad_stateid;
4348 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4350 /* Client debugging aid. */
4351 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4352 char addr_str[INET6_ADDRSTRLEN];
4353 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4355 pr_warn_ratelimited("NFSD: client %s testing state ID "
4356 "with incorrect client ID\n", addr_str);
4359 spin_lock(&cl->cl_lock);
4360 s = find_stateid_locked(cl, stateid);
4363 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
4366 switch (s->sc_type) {
4367 case NFS4_DELEG_STID:
4370 case NFS4_REVOKED_DELEG_STID:
4371 status = nfserr_deleg_revoked;
4373 case NFS4_OPEN_STID:
4374 case NFS4_LOCK_STID:
4375 ols = openlockstateid(s);
4376 if (ols->st_stateowner->so_is_open_owner
4377 && !(openowner(ols->st_stateowner)->oo_flags
4378 & NFS4_OO_CONFIRMED))
4379 status = nfserr_bad_stateid;
4384 printk("unknown stateid type %x\n", s->sc_type);
4386 case NFS4_CLOSED_STID:
4387 case NFS4_CLOSED_DELEG_STID:
4388 status = nfserr_bad_stateid;
4391 spin_unlock(&cl->cl_lock);
4396 nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4397 stateid_t *stateid, unsigned char typemask,
4398 struct nfs4_stid **s, struct nfsd_net *nn)
4402 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4403 return nfserr_bad_stateid;
4404 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
4405 if (status == nfserr_stale_clientid) {
4406 if (cstate->session)
4407 return nfserr_bad_stateid;
4408 return nfserr_stale_stateid;
4412 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
4414 return nfserr_bad_stateid;
4419 * Checks for stateid operations
4422 nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
4423 stateid_t *stateid, int flags, struct file **filpp)
4425 struct nfs4_stid *s;
4426 struct nfs4_ol_stateid *stp = NULL;
4427 struct nfs4_delegation *dp = NULL;
4428 struct svc_fh *current_fh = &cstate->current_fh;
4429 struct inode *ino = current_fh->fh_dentry->d_inode;
4430 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4431 struct file *file = NULL;
4437 if (grace_disallows_io(net, ino))
4438 return nfserr_grace;
4440 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4441 return check_special_stateids(net, current_fh, stateid, flags);
4443 status = nfsd4_lookup_stateid(cstate, stateid,
4444 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
4448 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4451 switch (s->sc_type) {
4452 case NFS4_DELEG_STID:
4453 dp = delegstateid(s);
4454 status = nfs4_check_delegmode(dp, flags);
4458 file = dp->dl_stid.sc_file->fi_deleg_file;
4461 status = nfserr_serverfault;
4467 case NFS4_OPEN_STID:
4468 case NFS4_LOCK_STID:
4469 stp = openlockstateid(s);
4470 status = nfs4_check_fh(current_fh, stp);
4473 if (stp->st_stateowner->so_is_open_owner
4474 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
4476 status = nfs4_check_openmode(stp, flags);
4480 struct nfs4_file *fp = stp->st_stid.sc_file;
4482 if (flags & RD_STATE)
4483 file = find_readable_file(fp);
4485 file = find_writeable_file(fp);
4489 status = nfserr_bad_stateid;
4501 * Test if the stateid is valid
4504 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4505 struct nfsd4_test_stateid *test_stateid)
4507 struct nfsd4_test_stateid_id *stateid;
4508 struct nfs4_client *cl = cstate->session->se_client;
4510 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
4511 stateid->ts_id_status =
4512 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
4518 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4519 struct nfsd4_free_stateid *free_stateid)
4521 stateid_t *stateid = &free_stateid->fr_stateid;
4522 struct nfs4_stid *s;
4523 struct nfs4_delegation *dp;
4524 struct nfs4_ol_stateid *stp;
4525 struct nfs4_client *cl = cstate->session->se_client;
4526 __be32 ret = nfserr_bad_stateid;
4528 spin_lock(&cl->cl_lock);
4529 s = find_stateid_locked(cl, stateid);
4532 switch (s->sc_type) {
4533 case NFS4_DELEG_STID:
4534 ret = nfserr_locks_held;
4536 case NFS4_OPEN_STID:
4537 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4540 ret = nfserr_locks_held;
4542 case NFS4_LOCK_STID:
4543 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4546 stp = openlockstateid(s);
4547 ret = nfserr_locks_held;
4548 if (check_for_locks(stp->st_stid.sc_file,
4549 lockowner(stp->st_stateowner)))
4551 unhash_lock_stateid(stp);
4552 spin_unlock(&cl->cl_lock);
4556 case NFS4_REVOKED_DELEG_STID:
4557 dp = delegstateid(s);
4558 list_del_init(&dp->dl_recall_lru);
4559 spin_unlock(&cl->cl_lock);
4563 /* Default falls through and returns nfserr_bad_stateid */
4566 spin_unlock(&cl->cl_lock);
4574 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4575 RD_STATE : WR_STATE;
4578 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
4580 struct svc_fh *current_fh = &cstate->current_fh;
4581 struct nfs4_stateowner *sop = stp->st_stateowner;
4584 status = nfsd4_check_seqid(cstate, sop, seqid);
4587 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4588 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
4590 * "Closed" stateid's exist *only* to return
4591 * nfserr_replay_me from the previous step, and
4592 * revoked delegations are kept only for free_stateid.
4594 return nfserr_bad_stateid;
4595 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4598 return nfs4_check_fh(current_fh, stp);
4602 * Checks for sequence id mutating operations.
4605 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4606 stateid_t *stateid, char typemask,
4607 struct nfs4_ol_stateid **stpp,
4608 struct nfsd_net *nn)
4611 struct nfs4_stid *s;
4612 struct nfs4_ol_stateid *stp = NULL;
4614 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4615 seqid, STATEID_VAL(stateid));
4618 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
4621 stp = openlockstateid(s);
4622 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
4624 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
4628 nfs4_put_stid(&stp->st_stid);
4632 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4633 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
4636 struct nfs4_openowner *oo;
4637 struct nfs4_ol_stateid *stp;
4639 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
4640 NFS4_OPEN_STID, &stp, nn);
4643 oo = openowner(stp->st_stateowner);
4644 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4645 nfs4_put_stid(&stp->st_stid);
4646 return nfserr_bad_stateid;
4653 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4654 struct nfsd4_open_confirm *oc)
4657 struct nfs4_openowner *oo;
4658 struct nfs4_ol_stateid *stp;
4659 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4661 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4662 cstate->current_fh.fh_dentry);
4664 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
4668 status = nfs4_preprocess_seqid_op(cstate,
4669 oc->oc_seqid, &oc->oc_req_stateid,
4670 NFS4_OPEN_STID, &stp, nn);
4673 oo = openowner(stp->st_stateowner);
4674 status = nfserr_bad_stateid;
4675 if (oo->oo_flags & NFS4_OO_CONFIRMED)
4677 oo->oo_flags |= NFS4_OO_CONFIRMED;
4678 update_stateid(&stp->st_stid.sc_stateid);
4679 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4680 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
4681 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
4683 nfsd4_client_record_create(oo->oo_owner.so_client);
4686 nfs4_put_stid(&stp->st_stid);
4688 nfsd4_bump_seqid(cstate, status);
4692 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
4694 if (!test_access(access, stp))
4696 nfs4_file_put_access(stp->st_stid.sc_file, access);
4697 clear_access(access, stp);
4700 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4702 switch (to_access) {
4703 case NFS4_SHARE_ACCESS_READ:
4704 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4705 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4707 case NFS4_SHARE_ACCESS_WRITE:
4708 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4709 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4711 case NFS4_SHARE_ACCESS_BOTH:
4719 nfsd4_open_downgrade(struct svc_rqst *rqstp,
4720 struct nfsd4_compound_state *cstate,
4721 struct nfsd4_open_downgrade *od)
4724 struct nfs4_ol_stateid *stp;
4725 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4727 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4728 cstate->current_fh.fh_dentry);
4730 /* We don't yet support WANT bits: */
4731 if (od->od_deleg_want)
4732 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4735 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
4736 &od->od_stateid, &stp, nn);
4739 status = nfserr_inval;
4740 if (!test_access(od->od_share_access, stp)) {
4741 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
4742 stp->st_access_bmap, od->od_share_access);
4745 if (!test_deny(od->od_share_deny, stp)) {
4746 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
4747 stp->st_deny_bmap, od->od_share_deny);
4750 nfs4_stateid_downgrade(stp, od->od_share_access);
4752 reset_union_bmap_deny(od->od_share_deny, stp);
4754 update_stateid(&stp->st_stid.sc_stateid);
4755 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4758 nfs4_put_stid(&stp->st_stid);
4760 nfsd4_bump_seqid(cstate, status);
4764 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4766 struct nfs4_client *clp = s->st_stid.sc_client;
4767 LIST_HEAD(reaplist);
4769 s->st_stid.sc_type = NFS4_CLOSED_STID;
4770 spin_lock(&clp->cl_lock);
4771 unhash_open_stateid(s, &reaplist);
4773 if (clp->cl_minorversion) {
4774 put_ol_stateid_locked(s, &reaplist);
4775 spin_unlock(&clp->cl_lock);
4776 free_ol_stateid_reaplist(&reaplist);
4778 spin_unlock(&clp->cl_lock);
4779 free_ol_stateid_reaplist(&reaplist);
4780 move_to_close_lru(s, clp->net);
4785 * nfs4_unlock_state() called after encode
4788 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4789 struct nfsd4_close *close)
4792 struct nfs4_ol_stateid *stp;
4793 struct net *net = SVC_NET(rqstp);
4794 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4796 dprintk("NFSD: nfsd4_close on file %pd\n",
4797 cstate->current_fh.fh_dentry);
4799 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4801 NFS4_OPEN_STID|NFS4_CLOSED_STID,
4803 nfsd4_bump_seqid(cstate, status);
4806 update_stateid(&stp->st_stid.sc_stateid);
4807 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4809 nfsd4_close_open_stateid(stp);
4811 /* put reference from nfs4_preprocess_seqid_op */
4812 nfs4_put_stid(&stp->st_stid);
4818 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4819 struct nfsd4_delegreturn *dr)
4821 struct nfs4_delegation *dp;
4822 stateid_t *stateid = &dr->dr_stateid;
4823 struct nfs4_stid *s;
4825 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4827 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
4830 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
4833 dp = delegstateid(s);
4834 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
4838 destroy_delegation(dp);
4840 nfs4_put_stid(&dp->dl_stid);
4846 #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
4849 end_offset(u64 start, u64 len)
4854 return end >= start ? end: NFS4_MAX_UINT64;
4857 /* last octet in a range */
4859 last_byte_offset(u64 start, u64 len)
4865 return end > start ? end - 1: NFS4_MAX_UINT64;
4869 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4870 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4871 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4872 * locking, this prevents us from being completely protocol-compliant. The
4873 * real solution to this problem is to start using unsigned file offsets in
4874 * the VFS, but this is a very deep change!
4877 nfs4_transform_lock_offset(struct file_lock *lock)
4879 if (lock->fl_start < 0)
4880 lock->fl_start = OFFSET_MAX;
4881 if (lock->fl_end < 0)
4882 lock->fl_end = OFFSET_MAX;
4885 /* Hack!: For now, we're defining this just so we can use a pointer to it
4886 * as a unique cookie to identify our (NFSv4's) posix locks. */
4887 static const struct lock_manager_operations nfsd_posix_mng_ops = {
4891 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4893 struct nfs4_lockowner *lo;
4895 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
4896 lo = (struct nfs4_lockowner *) fl->fl_owner;
4897 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4898 lo->lo_owner.so_owner.len, GFP_KERNEL);
4899 if (!deny->ld_owner.data)
4900 /* We just don't care that much */
4902 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4903 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
4906 deny->ld_owner.len = 0;
4907 deny->ld_owner.data = NULL;
4908 deny->ld_clientid.cl_boot = 0;
4909 deny->ld_clientid.cl_id = 0;
4911 deny->ld_start = fl->fl_start;
4912 deny->ld_length = NFS4_MAX_UINT64;
4913 if (fl->fl_end != NFS4_MAX_UINT64)
4914 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4915 deny->ld_type = NFS4_READ_LT;
4916 if (fl->fl_type != F_RDLCK)
4917 deny->ld_type = NFS4_WRITE_LT;
4920 static struct nfs4_lockowner *
4921 find_lockowner_str_locked(clientid_t *clid, struct xdr_netobj *owner,
4922 struct nfs4_client *clp)
4924 unsigned int strhashval = ownerstr_hashval(owner);
4925 struct nfs4_stateowner *so;
4927 lockdep_assert_held(&clp->cl_lock);
4929 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
4931 if (so->so_is_open_owner)
4933 if (!same_owner_str(so, owner))
4935 atomic_inc(&so->so_count);
4936 return lockowner(so);
4941 static struct nfs4_lockowner *
4942 find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
4943 struct nfs4_client *clp)
4945 struct nfs4_lockowner *lo;
4947 spin_lock(&clp->cl_lock);
4948 lo = find_lockowner_str_locked(clid, owner, clp);
4949 spin_unlock(&clp->cl_lock);
4953 static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
4955 unhash_lockowner_locked(lockowner(sop));
4958 static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
4960 struct nfs4_lockowner *lo = lockowner(sop);
4962 kmem_cache_free(lockowner_slab, lo);
4965 static const struct nfs4_stateowner_operations lockowner_ops = {
4966 .so_unhash = nfs4_unhash_lockowner,
4967 .so_free = nfs4_free_lockowner,
4971 * Alloc a lock owner structure.
4972 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
4975 * strhashval = ownerstr_hashval
4977 static struct nfs4_lockowner *
4978 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
4979 struct nfs4_ol_stateid *open_stp,
4980 struct nfsd4_lock *lock)
4982 struct nfs4_lockowner *lo, *ret;
4984 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4987 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4988 lo->lo_owner.so_is_open_owner = 0;
4989 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
4990 lo->lo_owner.so_ops = &lockowner_ops;
4991 spin_lock(&clp->cl_lock);
4992 ret = find_lockowner_str_locked(&clp->cl_clientid,
4993 &lock->lk_new_owner, clp);
4995 list_add(&lo->lo_owner.so_strhash,
4996 &clp->cl_ownerstr_hashtbl[strhashval]);
4999 nfs4_free_lockowner(&lo->lo_owner);
5000 spin_unlock(&clp->cl_lock);
5005 init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
5006 struct nfs4_file *fp, struct inode *inode,
5007 struct nfs4_ol_stateid *open_stp)
5009 struct nfs4_client *clp = lo->lo_owner.so_client;
5011 lockdep_assert_held(&clp->cl_lock);
5013 atomic_inc(&stp->st_stid.sc_count);
5014 stp->st_stid.sc_type = NFS4_LOCK_STID;
5015 stp->st_stateowner = &lo->lo_owner;
5016 atomic_inc(&lo->lo_owner.so_count);
5018 stp->st_stid.sc_file = fp;
5019 stp->st_stid.sc_free = nfs4_free_lock_stateid;
5020 stp->st_access_bmap = 0;
5021 stp->st_deny_bmap = open_stp->st_deny_bmap;
5022 stp->st_openstp = open_stp;
5023 list_add(&stp->st_locks, &open_stp->st_locks);
5024 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
5025 spin_lock(&fp->fi_lock);
5026 list_add(&stp->st_perfile, &fp->fi_stateids);
5027 spin_unlock(&fp->fi_lock);
5030 static struct nfs4_ol_stateid *
5031 find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5033 struct nfs4_ol_stateid *lst;
5034 struct nfs4_client *clp = lo->lo_owner.so_client;
5036 lockdep_assert_held(&clp->cl_lock);
5038 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
5039 if (lst->st_stid.sc_file == fp) {
5040 atomic_inc(&lst->st_stid.sc_count);
5047 static struct nfs4_ol_stateid *
5048 find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5049 struct inode *inode, struct nfs4_ol_stateid *ost,
5052 struct nfs4_stid *ns = NULL;
5053 struct nfs4_ol_stateid *lst;
5054 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5055 struct nfs4_client *clp = oo->oo_owner.so_client;
5057 spin_lock(&clp->cl_lock);
5058 lst = find_lock_stateid(lo, fi);
5060 spin_unlock(&clp->cl_lock);
5061 ns = nfs4_alloc_stid(clp, stateid_slab);
5065 spin_lock(&clp->cl_lock);
5066 lst = find_lock_stateid(lo, fi);
5068 lst = openlockstateid(ns);
5069 init_lock_stateid(lst, lo, fi, inode, ost);
5074 spin_unlock(&clp->cl_lock);
5081 check_lock_length(u64 offset, u64 length)
5083 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
5084 LOFF_OVERFLOW(offset, length)));
5087 static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
5089 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
5091 lockdep_assert_held(&fp->fi_lock);
5093 if (test_access(access, lock_stp))
5095 __nfs4_file_get_access(fp, access);
5096 set_access(access, lock_stp);
5100 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5101 struct nfs4_ol_stateid *ost,
5102 struct nfsd4_lock *lock,
5103 struct nfs4_ol_stateid **lst, bool *new)
5106 struct nfs4_file *fi = ost->st_stid.sc_file;
5107 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5108 struct nfs4_client *cl = oo->oo_owner.so_client;
5109 struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
5110 struct nfs4_lockowner *lo;
5111 unsigned int strhashval;
5113 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, cl);
5115 strhashval = ownerstr_hashval(&lock->v.new.owner);
5116 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5118 return nfserr_jukebox;
5120 /* with an existing lockowner, seqids must be the same */
5121 status = nfserr_bad_seqid;
5122 if (!cstate->minorversion &&
5123 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
5127 *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
5129 status = nfserr_jukebox;
5134 nfs4_put_stateowner(&lo->lo_owner);
5142 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5143 struct nfsd4_lock *lock)
5145 struct nfs4_openowner *open_sop = NULL;
5146 struct nfs4_lockowner *lock_sop = NULL;
5147 struct nfs4_ol_stateid *lock_stp = NULL;
5148 struct nfs4_ol_stateid *open_stp = NULL;
5149 struct nfs4_file *fp;
5150 struct file *filp = NULL;
5151 struct file_lock *file_lock = NULL;
5152 struct file_lock *conflock = NULL;
5157 struct net *net = SVC_NET(rqstp);
5158 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5160 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5161 (long long) lock->lk_offset,
5162 (long long) lock->lk_length);
5164 if (check_lock_length(lock->lk_offset, lock->lk_length))
5165 return nfserr_inval;
5167 if ((status = fh_verify(rqstp, &cstate->current_fh,
5168 S_IFREG, NFSD_MAY_LOCK))) {
5169 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5173 if (lock->lk_is_new) {
5174 if (nfsd4_has_session(cstate))
5175 /* See rfc 5661 18.10.3: given clientid is ignored: */
5176 memcpy(&lock->v.new.clientid,
5177 &cstate->session->se_client->cl_clientid,
5178 sizeof(clientid_t));
5180 status = nfserr_stale_clientid;
5181 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
5184 /* validate and update open stateid and open seqid */
5185 status = nfs4_preprocess_confirmed_seqid_op(cstate,
5186 lock->lk_new_open_seqid,
5187 &lock->lk_new_open_stateid,
5191 open_sop = openowner(open_stp->st_stateowner);
5192 status = nfserr_bad_stateid;
5193 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
5194 &lock->v.new.clientid))
5196 status = lookup_or_create_lock_state(cstate, open_stp, lock,
5199 status = nfs4_preprocess_seqid_op(cstate,
5200 lock->lk_old_lock_seqid,
5201 &lock->lk_old_lock_stateid,
5202 NFS4_LOCK_STID, &lock_stp, nn);
5206 lock_sop = lockowner(lock_stp->st_stateowner);
5208 lkflg = setlkflg(lock->lk_type);
5209 status = nfs4_check_openmode(lock_stp, lkflg);
5213 status = nfserr_grace;
5214 if (locks_in_grace(net) && !lock->lk_reclaim)
5216 status = nfserr_no_grace;
5217 if (!locks_in_grace(net) && lock->lk_reclaim)
5220 file_lock = locks_alloc_lock();
5222 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5223 status = nfserr_jukebox;
5227 fp = lock_stp->st_stid.sc_file;
5228 locks_init_lock(file_lock);
5229 switch (lock->lk_type) {
5232 spin_lock(&fp->fi_lock);
5233 filp = find_readable_file_locked(fp);
5235 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
5236 spin_unlock(&fp->fi_lock);
5237 file_lock->fl_type = F_RDLCK;
5240 case NFS4_WRITEW_LT:
5241 spin_lock(&fp->fi_lock);
5242 filp = find_writeable_file_locked(fp);
5244 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
5245 spin_unlock(&fp->fi_lock);
5246 file_lock->fl_type = F_WRLCK;
5249 status = nfserr_inval;
5253 status = nfserr_openmode;
5256 file_lock->fl_owner = (fl_owner_t)lock_sop;
5257 file_lock->fl_pid = current->tgid;
5258 file_lock->fl_file = filp;
5259 file_lock->fl_flags = FL_POSIX;
5260 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5261 file_lock->fl_start = lock->lk_offset;
5262 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5263 nfs4_transform_lock_offset(file_lock);
5265 conflock = locks_alloc_lock();
5267 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5268 status = nfserr_jukebox;
5272 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
5274 case 0: /* success! */
5275 update_stateid(&lock_stp->st_stid.sc_stateid);
5276 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
5280 case (EAGAIN): /* conflock holds conflicting lock */
5281 status = nfserr_denied;
5282 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
5283 nfs4_set_lock_denied(conflock, &lock->lk_denied);
5286 status = nfserr_deadlock;
5289 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
5290 status = nfserrno(err);
5297 /* Bump seqid manually if the 4.0 replay owner is openowner */
5298 if (cstate->replay_owner &&
5299 cstate->replay_owner != &lock_sop->lo_owner &&
5300 seqid_mutating_err(ntohl(status)))
5301 lock_sop->lo_owner.so_seqid++;
5304 * If this is a new, never-before-used stateid, and we are
5305 * returning an error, then just go ahead and release it.
5308 release_lock_stateid(lock_stp);
5310 nfs4_put_stid(&lock_stp->st_stid);
5313 nfs4_put_stid(&open_stp->st_stid);
5314 nfsd4_bump_seqid(cstate, status);
5316 locks_free_lock(file_lock);
5318 locks_free_lock(conflock);
5323 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5324 * so we do a temporary open here just to get an open file to pass to
5325 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5328 static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
5331 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5333 err = nfserrno(vfs_test_lock(file, lock));
5343 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5344 struct nfsd4_lockt *lockt)
5346 struct file_lock *file_lock = NULL;
5347 struct nfs4_lockowner *lo = NULL;
5349 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5351 if (locks_in_grace(SVC_NET(rqstp)))
5352 return nfserr_grace;
5354 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5355 return nfserr_inval;
5357 if (!nfsd4_has_session(cstate)) {
5358 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
5363 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
5366 file_lock = locks_alloc_lock();
5368 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5369 status = nfserr_jukebox;
5372 locks_init_lock(file_lock);
5373 switch (lockt->lt_type) {
5376 file_lock->fl_type = F_RDLCK;
5379 case NFS4_WRITEW_LT:
5380 file_lock->fl_type = F_WRLCK;
5383 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
5384 status = nfserr_inval;
5388 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner,
5391 file_lock->fl_owner = (fl_owner_t)lo;
5392 file_lock->fl_pid = current->tgid;
5393 file_lock->fl_flags = FL_POSIX;
5395 file_lock->fl_start = lockt->lt_offset;
5396 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
5398 nfs4_transform_lock_offset(file_lock);
5400 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
5404 if (file_lock->fl_type != F_UNLCK) {
5405 status = nfserr_denied;
5406 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
5410 nfs4_put_stateowner(&lo->lo_owner);
5412 locks_free_lock(file_lock);
5417 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5418 struct nfsd4_locku *locku)
5420 struct nfs4_ol_stateid *stp;
5421 struct file *filp = NULL;
5422 struct file_lock *file_lock = NULL;
5425 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5427 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5428 (long long) locku->lu_offset,
5429 (long long) locku->lu_length);
5431 if (check_lock_length(locku->lu_offset, locku->lu_length))
5432 return nfserr_inval;
5434 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
5435 &locku->lu_stateid, NFS4_LOCK_STID,
5439 filp = find_any_file(stp->st_stid.sc_file);
5441 status = nfserr_lock_range;
5444 file_lock = locks_alloc_lock();
5446 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5447 status = nfserr_jukebox;
5450 locks_init_lock(file_lock);
5451 file_lock->fl_type = F_UNLCK;
5452 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
5453 file_lock->fl_pid = current->tgid;
5454 file_lock->fl_file = filp;
5455 file_lock->fl_flags = FL_POSIX;
5456 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5457 file_lock->fl_start = locku->lu_offset;
5459 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5461 nfs4_transform_lock_offset(file_lock);
5463 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
5465 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
5468 update_stateid(&stp->st_stid.sc_stateid);
5469 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
5473 nfs4_put_stid(&stp->st_stid);
5475 nfsd4_bump_seqid(cstate, status);
5477 locks_free_lock(file_lock);
5481 status = nfserrno(err);
5487 * true: locks held by lockowner
5488 * false: no locks held by lockowner
5491 check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
5493 struct file_lock **flpp;
5495 struct file *filp = find_any_file(fp);
5496 struct inode *inode;
5499 /* Any valid lock stateid should have some sort of access */
5504 inode = file_inode(filp);
5506 spin_lock(&inode->i_lock);
5507 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
5508 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
5513 spin_unlock(&inode->i_lock);
5519 nfsd4_release_lockowner(struct svc_rqst *rqstp,
5520 struct nfsd4_compound_state *cstate,
5521 struct nfsd4_release_lockowner *rlockowner)
5523 clientid_t *clid = &rlockowner->rl_clientid;
5524 struct nfs4_stateowner *sop;
5525 struct nfs4_lockowner *lo = NULL;
5526 struct nfs4_ol_stateid *stp;
5527 struct xdr_netobj *owner = &rlockowner->rl_owner;
5528 unsigned int hashval = ownerstr_hashval(owner);
5530 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5531 struct nfs4_client *clp;
5533 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5534 clid->cl_boot, clid->cl_id);
5536 status = lookup_clientid(clid, cstate, nn);
5541 /* Find the matching lock stateowner */
5542 spin_lock(&clp->cl_lock);
5543 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
5546 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
5549 /* see if there are still any locks associated with it */
5550 lo = lockowner(sop);
5551 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5552 if (check_for_locks(stp->st_stid.sc_file, lo)) {
5553 status = nfserr_locks_held;
5554 spin_unlock(&clp->cl_lock);
5559 atomic_inc(&sop->so_count);
5562 spin_unlock(&clp->cl_lock);
5564 release_lockowner(lo);
5568 static inline struct nfs4_client_reclaim *
5571 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
5575 nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
5577 struct nfs4_client_reclaim *crp;
5579 crp = nfsd4_find_reclaim_client(name, nn);
5580 return (crp && crp->cr_clp);
5584 * failure => all reset bets are off, nfserr_no_grace...
5586 struct nfs4_client_reclaim *
5587 nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
5589 unsigned int strhashval;
5590 struct nfs4_client_reclaim *crp;
5592 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5593 crp = alloc_reclaim();
5595 strhashval = clientstr_hashval(name);
5596 INIT_LIST_HEAD(&crp->cr_strhash);
5597 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
5598 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
5600 nn->reclaim_str_hashtbl_size++;
5606 nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
5608 list_del(&crp->cr_strhash);
5610 nn->reclaim_str_hashtbl_size--;
5614 nfs4_release_reclaim(struct nfsd_net *nn)
5616 struct nfs4_client_reclaim *crp = NULL;
5619 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5620 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5621 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
5622 struct nfs4_client_reclaim, cr_strhash);
5623 nfs4_remove_reclaim_record(crp, nn);
5626 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
5630 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
5631 struct nfs4_client_reclaim *
5632 nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
5634 unsigned int strhashval;
5635 struct nfs4_client_reclaim *crp = NULL;
5637 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
5639 strhashval = clientstr_hashval(recdir);
5640 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
5641 if (same_name(crp->cr_recdir, recdir)) {
5649 * Called from OPEN. Look for clientid in reclaim list.
5652 nfs4_check_open_reclaim(clientid_t *clid,
5653 struct nfsd4_compound_state *cstate,
5654 struct nfsd_net *nn)
5658 /* find clientid in conf_id_hashtbl */
5659 status = lookup_clientid(clid, cstate, nn);
5661 return nfserr_reclaim_bad;
5663 if (nfsd4_client_record_check(cstate->clp))
5664 return nfserr_reclaim_bad;
5669 #ifdef CONFIG_NFSD_FAULT_INJECTION
5671 put_client(struct nfs4_client *clp)
5673 atomic_dec(&clp->cl_refcount);
5676 static struct nfs4_client *
5677 nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5679 struct nfs4_client *clp;
5680 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5683 if (!nfsd_netns_ready(nn))
5686 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5687 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5694 nfsd_inject_print_clients(void)
5696 struct nfs4_client *clp;
5698 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5700 char buf[INET6_ADDRSTRLEN];
5702 if (!nfsd_netns_ready(nn))
5705 spin_lock(&nn->client_lock);
5706 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5707 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5708 pr_info("NFS Client: %s\n", buf);
5711 spin_unlock(&nn->client_lock);
5717 nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
5720 struct nfs4_client *clp;
5721 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5724 if (!nfsd_netns_ready(nn))
5727 spin_lock(&nn->client_lock);
5728 clp = nfsd_find_client(addr, addr_size);
5730 if (mark_client_expired_locked(clp) == nfs_ok)
5735 spin_unlock(&nn->client_lock);
5744 nfsd_inject_forget_clients(u64 max)
5747 struct nfs4_client *clp, *next;
5748 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5750 LIST_HEAD(reaplist);
5752 if (!nfsd_netns_ready(nn))
5755 spin_lock(&nn->client_lock);
5756 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
5757 if (mark_client_expired_locked(clp) == nfs_ok) {
5758 list_add(&clp->cl_lru, &reaplist);
5759 if (max != 0 && ++count >= max)
5763 spin_unlock(&nn->client_lock);
5765 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
5771 static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5774 char buf[INET6_ADDRSTRLEN];
5775 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5776 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5780 nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
5781 struct list_head *collect)
5783 struct nfs4_client *clp = lst->st_stid.sc_client;
5784 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5790 lockdep_assert_held(&nn->client_lock);
5791 atomic_inc(&clp->cl_refcount);
5792 list_add(&lst->st_locks, collect);
5795 static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
5796 struct list_head *collect,
5797 void (*func)(struct nfs4_ol_stateid *))
5799 struct nfs4_openowner *oop;
5800 struct nfs4_ol_stateid *stp, *st_next;
5801 struct nfs4_ol_stateid *lst, *lst_next;
5804 spin_lock(&clp->cl_lock);
5805 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
5806 list_for_each_entry_safe(stp, st_next,
5807 &oop->oo_owner.so_stateids, st_perstateowner) {
5808 list_for_each_entry_safe(lst, lst_next,
5809 &stp->st_locks, st_locks) {
5812 nfsd_inject_add_lock_to_list(lst,
5817 * Despite the fact that these functions deal
5818 * with 64-bit integers for "count", we must
5819 * ensure that it doesn't blow up the
5820 * clp->cl_refcount. Throw a warning if we
5821 * start to approach INT_MAX here.
5823 WARN_ON_ONCE(count == (INT_MAX / 2));
5830 spin_unlock(&clp->cl_lock);
5836 nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
5839 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
5843 nfsd_print_client_locks(struct nfs4_client *clp)
5845 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
5846 nfsd_print_count(clp, count, "locked files");
5851 nfsd_inject_print_locks(void)
5853 struct nfs4_client *clp;
5855 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5858 if (!nfsd_netns_ready(nn))
5861 spin_lock(&nn->client_lock);
5862 list_for_each_entry(clp, &nn->client_lru, cl_lru)
5863 count += nfsd_print_client_locks(clp);
5864 spin_unlock(&nn->client_lock);
5870 nfsd_reap_locks(struct list_head *reaplist)
5872 struct nfs4_client *clp;
5873 struct nfs4_ol_stateid *stp, *next;
5875 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
5876 list_del_init(&stp->st_locks);
5877 clp = stp->st_stid.sc_client;
5878 nfs4_put_stid(&stp->st_stid);
5884 nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
5886 unsigned int count = 0;
5887 struct nfs4_client *clp;
5888 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5890 LIST_HEAD(reaplist);
5892 if (!nfsd_netns_ready(nn))
5895 spin_lock(&nn->client_lock);
5896 clp = nfsd_find_client(addr, addr_size);
5898 count = nfsd_collect_client_locks(clp, &reaplist, 0);
5899 spin_unlock(&nn->client_lock);
5900 nfsd_reap_locks(&reaplist);
5905 nfsd_inject_forget_locks(u64 max)
5908 struct nfs4_client *clp;
5909 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5911 LIST_HEAD(reaplist);
5913 if (!nfsd_netns_ready(nn))
5916 spin_lock(&nn->client_lock);
5917 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5918 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
5919 if (max != 0 && count >= max)
5922 spin_unlock(&nn->client_lock);
5923 nfsd_reap_locks(&reaplist);
5928 nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
5929 struct list_head *collect,
5930 void (*func)(struct nfs4_openowner *))
5932 struct nfs4_openowner *oop, *next;
5933 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5937 lockdep_assert_held(&nn->client_lock);
5939 spin_lock(&clp->cl_lock);
5940 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
5944 atomic_inc(&clp->cl_refcount);
5945 list_add(&oop->oo_perclient, collect);
5950 * Despite the fact that these functions deal with
5951 * 64-bit integers for "count", we must ensure that
5952 * it doesn't blow up the clp->cl_refcount. Throw a
5953 * warning if we start to approach INT_MAX here.
5955 WARN_ON_ONCE(count == (INT_MAX / 2));
5959 spin_unlock(&clp->cl_lock);
5965 nfsd_print_client_openowners(struct nfs4_client *clp)
5967 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
5969 nfsd_print_count(clp, count, "openowners");
5974 nfsd_collect_client_openowners(struct nfs4_client *clp,
5975 struct list_head *collect, u64 max)
5977 return nfsd_foreach_client_openowner(clp, max, collect,
5978 unhash_openowner_locked);
5982 nfsd_inject_print_openowners(void)
5984 struct nfs4_client *clp;
5986 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5989 if (!nfsd_netns_ready(nn))
5992 spin_lock(&nn->client_lock);
5993 list_for_each_entry(clp, &nn->client_lru, cl_lru)
5994 count += nfsd_print_client_openowners(clp);
5995 spin_unlock(&nn->client_lock);
6001 nfsd_reap_openowners(struct list_head *reaplist)
6003 struct nfs4_client *clp;
6004 struct nfs4_openowner *oop, *next;
6006 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
6007 list_del_init(&oop->oo_perclient);
6008 clp = oop->oo_owner.so_client;
6009 release_openowner(oop);
6015 nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
6018 unsigned int count = 0;
6019 struct nfs4_client *clp;
6020 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6022 LIST_HEAD(reaplist);
6024 if (!nfsd_netns_ready(nn))
6027 spin_lock(&nn->client_lock);
6028 clp = nfsd_find_client(addr, addr_size);
6030 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
6031 spin_unlock(&nn->client_lock);
6032 nfsd_reap_openowners(&reaplist);
6037 nfsd_inject_forget_openowners(u64 max)
6040 struct nfs4_client *clp;
6041 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6043 LIST_HEAD(reaplist);
6045 if (!nfsd_netns_ready(nn))
6048 spin_lock(&nn->client_lock);
6049 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6050 count += nfsd_collect_client_openowners(clp, &reaplist,
6052 if (max != 0 && count >= max)
6055 spin_unlock(&nn->client_lock);
6056 nfsd_reap_openowners(&reaplist);
6060 static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
6061 struct list_head *victims)
6063 struct nfs4_delegation *dp, *next;
6064 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6068 lockdep_assert_held(&nn->client_lock);
6070 spin_lock(&state_lock);
6071 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
6074 * It's not safe to mess with delegations that have a
6075 * non-zero dl_time. They might have already been broken
6076 * and could be processed by the laundromat outside of
6077 * the state_lock. Just leave them be.
6079 if (dp->dl_time != 0)
6082 atomic_inc(&clp->cl_refcount);
6083 unhash_delegation_locked(dp);
6084 list_add(&dp->dl_recall_lru, victims);
6088 * Despite the fact that these functions deal with
6089 * 64-bit integers for "count", we must ensure that
6090 * it doesn't blow up the clp->cl_refcount. Throw a
6091 * warning if we start to approach INT_MAX here.
6093 WARN_ON_ONCE(count == (INT_MAX / 2));
6097 spin_unlock(&state_lock);
6102 nfsd_print_client_delegations(struct nfs4_client *clp)
6104 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
6106 nfsd_print_count(clp, count, "delegations");
6111 nfsd_inject_print_delegations(void)
6113 struct nfs4_client *clp;
6115 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6118 if (!nfsd_netns_ready(nn))
6121 spin_lock(&nn->client_lock);
6122 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6123 count += nfsd_print_client_delegations(clp);
6124 spin_unlock(&nn->client_lock);
6130 nfsd_forget_delegations(struct list_head *reaplist)
6132 struct nfs4_client *clp;
6133 struct nfs4_delegation *dp, *next;
6135 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6136 list_del_init(&dp->dl_recall_lru);
6137 clp = dp->dl_stid.sc_client;
6138 revoke_delegation(dp);
6144 nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
6148 struct nfs4_client *clp;
6149 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6151 LIST_HEAD(reaplist);
6153 if (!nfsd_netns_ready(nn))
6156 spin_lock(&nn->client_lock);
6157 clp = nfsd_find_client(addr, addr_size);
6159 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6160 spin_unlock(&nn->client_lock);
6162 nfsd_forget_delegations(&reaplist);
6167 nfsd_inject_forget_delegations(u64 max)
6170 struct nfs4_client *clp;
6171 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6173 LIST_HEAD(reaplist);
6175 if (!nfsd_netns_ready(nn))
6178 spin_lock(&nn->client_lock);
6179 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6180 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6181 if (max != 0 && count >= max)
6184 spin_unlock(&nn->client_lock);
6185 nfsd_forget_delegations(&reaplist);
6190 nfsd_recall_delegations(struct list_head *reaplist)
6192 struct nfs4_client *clp;
6193 struct nfs4_delegation *dp, *next;
6195 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6196 list_del_init(&dp->dl_recall_lru);
6197 clp = dp->dl_stid.sc_client;
6199 * We skipped all entries that had a zero dl_time before,
6200 * so we can now reset the dl_time back to 0. If a delegation
6201 * break comes in now, then it won't make any difference since
6202 * we're recalling it either way.
6204 spin_lock(&state_lock);
6206 spin_unlock(&state_lock);
6207 nfsd_break_one_deleg(dp);
6213 nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
6217 struct nfs4_client *clp;
6218 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6220 LIST_HEAD(reaplist);
6222 if (!nfsd_netns_ready(nn))
6225 spin_lock(&nn->client_lock);
6226 clp = nfsd_find_client(addr, addr_size);
6228 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6229 spin_unlock(&nn->client_lock);
6231 nfsd_recall_delegations(&reaplist);
6236 nfsd_inject_recall_delegations(u64 max)
6239 struct nfs4_client *clp, *next;
6240 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6242 LIST_HEAD(reaplist);
6244 if (!nfsd_netns_ready(nn))
6247 spin_lock(&nn->client_lock);
6248 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6249 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6250 if (max != 0 && ++count >= max)
6253 spin_unlock(&nn->client_lock);
6254 nfsd_recall_delegations(&reaplist);
6257 #endif /* CONFIG_NFSD_FAULT_INJECTION */
6260 * Since the lifetime of a delegation isn't limited to that of an open, a
6261 * client may quite reasonably hang on to a delegation as long as it has
6262 * the inode cached. This becomes an obvious problem the first time a
6263 * client's inode cache approaches the size of the server's total memory.
6265 * For now we avoid this problem by imposing a hard limit on the number
6266 * of delegations, which varies according to the server's memory size.
6269 set_max_delegations(void)
6272 * Allow at most 4 delegations per megabyte of RAM. Quick
6273 * estimates suggest that in the worst case (where every delegation
6274 * is for a different inode), a delegation could take about 1.5K,
6275 * giving a worst case usage of about 6% of memory.
6277 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
6280 static int nfs4_state_create_net(struct net *net)
6282 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6285 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6286 CLIENT_HASH_SIZE, GFP_KERNEL);
6287 if (!nn->conf_id_hashtbl)
6289 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6290 CLIENT_HASH_SIZE, GFP_KERNEL);
6291 if (!nn->unconf_id_hashtbl)
6293 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
6294 SESSION_HASH_SIZE, GFP_KERNEL);
6295 if (!nn->sessionid_hashtbl)
6298 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6299 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
6300 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
6302 for (i = 0; i < SESSION_HASH_SIZE; i++)
6303 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
6304 nn->conf_name_tree = RB_ROOT;
6305 nn->unconf_name_tree = RB_ROOT;
6306 INIT_LIST_HEAD(&nn->client_lru);
6307 INIT_LIST_HEAD(&nn->close_lru);
6308 INIT_LIST_HEAD(&nn->del_recall_lru);
6309 spin_lock_init(&nn->client_lock);
6311 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
6317 kfree(nn->unconf_id_hashtbl);
6319 kfree(nn->conf_id_hashtbl);
6325 nfs4_state_destroy_net(struct net *net)
6328 struct nfs4_client *clp = NULL;
6329 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6331 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6332 while (!list_empty(&nn->conf_id_hashtbl[i])) {
6333 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6334 destroy_client(clp);
6338 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6339 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
6340 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6341 destroy_client(clp);
6345 kfree(nn->sessionid_hashtbl);
6346 kfree(nn->unconf_id_hashtbl);
6347 kfree(nn->conf_id_hashtbl);
6352 nfs4_state_start_net(struct net *net)
6354 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6357 ret = nfs4_state_create_net(net);
6360 nfsd4_client_tracking_init(net);
6361 nn->boot_time = get_seconds();
6362 locks_start_grace(net, &nn->nfsd4_manager);
6363 nn->grace_ended = false;
6364 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
6365 nn->nfsd4_grace, net);
6366 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
6370 /* initialization to perform when the nfsd service is started: */
6373 nfs4_state_start(void)
6377 ret = set_callback_cred();
6380 laundry_wq = create_singlethread_workqueue("nfsd4");
6381 if (laundry_wq == NULL) {
6385 ret = nfsd4_create_callback_queue();
6387 goto out_free_laundry;
6389 set_max_delegations();
6394 destroy_workqueue(laundry_wq);
6400 nfs4_state_shutdown_net(struct net *net)
6402 struct nfs4_delegation *dp = NULL;
6403 struct list_head *pos, *next, reaplist;
6404 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6406 cancel_delayed_work_sync(&nn->laundromat_work);
6407 locks_end_grace(&nn->nfsd4_manager);
6410 INIT_LIST_HEAD(&reaplist);
6411 spin_lock(&state_lock);
6412 list_for_each_safe(pos, next, &nn->del_recall_lru) {
6413 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
6414 unhash_delegation_locked(dp);
6415 list_add(&dp->dl_recall_lru, &reaplist);
6417 spin_unlock(&state_lock);
6418 list_for_each_safe(pos, next, &reaplist) {
6419 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
6420 list_del_init(&dp->dl_recall_lru);
6421 nfs4_put_stid(&dp->dl_stid);
6424 nfsd4_client_tracking_exit(net);
6425 nfs4_state_destroy_net(net);
6426 nfs4_unlock_state();
6430 nfs4_state_shutdown(void)
6432 destroy_workqueue(laundry_wq);
6433 nfsd4_destroy_callback_queue();
6437 get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6439 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
6440 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
6444 put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6446 if (cstate->minorversion) {
6447 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
6448 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6453 clear_current_stateid(struct nfsd4_compound_state *cstate)
6455 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6459 * functions to set current state id
6462 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6464 put_stateid(cstate, &odp->od_stateid);
6468 nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
6470 put_stateid(cstate, &open->op_stateid);
6474 nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6476 put_stateid(cstate, &close->cl_stateid);
6480 nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
6482 put_stateid(cstate, &lock->lk_resp_stateid);
6486 * functions to consume current state id
6490 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6492 get_stateid(cstate, &odp->od_stateid);
6496 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
6498 get_stateid(cstate, &drp->dr_stateid);
6502 nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
6504 get_stateid(cstate, &fsp->fr_stateid);
6508 nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
6510 get_stateid(cstate, &setattr->sa_stateid);
6514 nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6516 get_stateid(cstate, &close->cl_stateid);
6520 nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
6522 get_stateid(cstate, &locku->lu_stateid);
6526 nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
6528 get_stateid(cstate, &read->rd_stateid);
6532 nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
6534 get_stateid(cstate, &write->wr_stateid);