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 int check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner);
77 /* Currently used for almost all code touching nfsv4 state: */
78 static DEFINE_MUTEX(client_mutex);
81 * Currently used for the del_recall_lru and file hash table. In an
82 * effort to decrease the scope of the client_mutex, this spinlock may
83 * eventually cover more:
85 static DEFINE_SPINLOCK(state_lock);
87 static struct kmem_cache *openowner_slab;
88 static struct kmem_cache *lockowner_slab;
89 static struct kmem_cache *file_slab;
90 static struct kmem_cache *stateid_slab;
91 static struct kmem_cache *deleg_slab;
96 mutex_lock(&client_mutex);
99 static void free_session(struct nfsd4_session *);
101 static bool is_session_dead(struct nfsd4_session *ses)
103 return ses->se_flags & NFS4_SESSION_DEAD;
106 static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
108 if (atomic_read(&ses->se_ref) > ref_held_by_me)
109 return nfserr_jukebox;
110 ses->se_flags |= NFS4_SESSION_DEAD;
115 nfs4_unlock_state(void)
117 mutex_unlock(&client_mutex);
120 static bool is_client_expired(struct nfs4_client *clp)
122 return clp->cl_time == 0;
125 static __be32 mark_client_expired_locked(struct nfs4_client *clp)
127 if (atomic_read(&clp->cl_refcount))
128 return nfserr_jukebox;
133 static __be32 mark_client_expired(struct nfs4_client *clp)
135 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
138 spin_lock(&nn->client_lock);
139 ret = mark_client_expired_locked(clp);
140 spin_unlock(&nn->client_lock);
144 static __be32 get_client_locked(struct nfs4_client *clp)
146 if (is_client_expired(clp))
147 return nfserr_expired;
148 atomic_inc(&clp->cl_refcount);
152 /* must be called under the client_lock */
154 renew_client_locked(struct nfs4_client *clp)
156 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
158 if (is_client_expired(clp)) {
160 printk("%s: client (clientid %08x/%08x) already expired\n",
162 clp->cl_clientid.cl_boot,
163 clp->cl_clientid.cl_id);
167 dprintk("renewing client (clientid %08x/%08x)\n",
168 clp->cl_clientid.cl_boot,
169 clp->cl_clientid.cl_id);
170 list_move_tail(&clp->cl_lru, &nn->client_lru);
171 clp->cl_time = get_seconds();
175 renew_client(struct nfs4_client *clp)
177 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
179 spin_lock(&nn->client_lock);
180 renew_client_locked(clp);
181 spin_unlock(&nn->client_lock);
184 static void put_client_renew_locked(struct nfs4_client *clp)
186 if (!atomic_dec_and_test(&clp->cl_refcount))
188 if (!is_client_expired(clp))
189 renew_client_locked(clp);
192 static void put_client_renew(struct nfs4_client *clp)
194 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
196 if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
198 if (!is_client_expired(clp))
199 renew_client_locked(clp);
200 spin_unlock(&nn->client_lock);
203 static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
207 if (is_session_dead(ses))
208 return nfserr_badsession;
209 status = get_client_locked(ses->se_client);
212 atomic_inc(&ses->se_ref);
216 static void nfsd4_put_session_locked(struct nfsd4_session *ses)
218 struct nfs4_client *clp = ses->se_client;
220 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
222 put_client_renew_locked(clp);
225 static void nfsd4_put_session(struct nfsd4_session *ses)
227 struct nfs4_client *clp = ses->se_client;
228 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
230 spin_lock(&nn->client_lock);
231 nfsd4_put_session_locked(ses);
232 spin_unlock(&nn->client_lock);
237 opaque_hashval(const void *ptr, int nbytes)
239 unsigned char *cptr = (unsigned char *) ptr;
249 static void nfsd4_free_file(struct nfs4_file *f)
251 kmem_cache_free(file_slab, f);
255 put_nfs4_file(struct nfs4_file *fi)
257 if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) {
258 hlist_del(&fi->fi_hash);
259 spin_unlock(&state_lock);
266 get_nfs4_file(struct nfs4_file *fi)
268 atomic_inc(&fi->fi_ref);
272 __nfs4_get_fd(struct nfs4_file *f, int oflag)
274 if (f->fi_fds[oflag])
275 return get_file(f->fi_fds[oflag]);
280 find_writeable_file_locked(struct nfs4_file *f)
284 lockdep_assert_held(&f->fi_lock);
286 ret = __nfs4_get_fd(f, O_WRONLY);
288 ret = __nfs4_get_fd(f, O_RDWR);
293 find_writeable_file(struct nfs4_file *f)
297 spin_lock(&f->fi_lock);
298 ret = find_writeable_file_locked(f);
299 spin_unlock(&f->fi_lock);
304 static struct file *find_readable_file_locked(struct nfs4_file *f)
308 lockdep_assert_held(&f->fi_lock);
310 ret = __nfs4_get_fd(f, O_RDONLY);
312 ret = __nfs4_get_fd(f, O_RDWR);
317 find_readable_file(struct nfs4_file *f)
321 spin_lock(&f->fi_lock);
322 ret = find_readable_file_locked(f);
323 spin_unlock(&f->fi_lock);
329 find_any_file(struct nfs4_file *f)
333 spin_lock(&f->fi_lock);
334 ret = __nfs4_get_fd(f, O_RDWR);
336 ret = __nfs4_get_fd(f, O_WRONLY);
338 ret = __nfs4_get_fd(f, O_RDONLY);
340 spin_unlock(&f->fi_lock);
344 static int num_delegations;
345 unsigned long max_delegations;
348 * Open owner state (share locks)
351 /* hash tables for lock and open owners */
352 #define OWNER_HASH_BITS 8
353 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
354 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
356 static unsigned int ownerstr_hashval(u32 clientid, struct xdr_netobj *ownername)
360 ret = opaque_hashval(ownername->data, ownername->len);
362 return ret & OWNER_HASH_MASK;
365 /* hash table for nfs4_file */
366 #define FILE_HASH_BITS 8
367 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
369 static unsigned int file_hashval(struct inode *ino)
371 /* XXX: why are we hashing on inode pointer, anyway? */
372 return hash_ptr(ino, FILE_HASH_BITS);
375 static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
378 __nfs4_file_get_access(struct nfs4_file *fp, u32 access)
380 lockdep_assert_held(&fp->fi_lock);
382 if (access & NFS4_SHARE_ACCESS_WRITE)
383 atomic_inc(&fp->fi_access[O_WRONLY]);
384 if (access & NFS4_SHARE_ACCESS_READ)
385 atomic_inc(&fp->fi_access[O_RDONLY]);
389 nfs4_file_get_access(struct nfs4_file *fp, u32 access)
391 lockdep_assert_held(&fp->fi_lock);
393 /* Does this access mode make sense? */
394 if (access & ~NFS4_SHARE_ACCESS_BOTH)
397 /* Does it conflict with a deny mode already set? */
398 if ((access & fp->fi_share_deny) != 0)
399 return nfserr_share_denied;
401 __nfs4_file_get_access(fp, access);
405 static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
407 /* Common case is that there is no deny mode. */
409 /* Does this deny mode make sense? */
410 if (deny & ~NFS4_SHARE_DENY_BOTH)
413 if ((deny & NFS4_SHARE_DENY_READ) &&
414 atomic_read(&fp->fi_access[O_RDONLY]))
415 return nfserr_share_denied;
417 if ((deny & NFS4_SHARE_DENY_WRITE) &&
418 atomic_read(&fp->fi_access[O_WRONLY]))
419 return nfserr_share_denied;
424 static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
426 might_lock(&fp->fi_lock);
428 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
429 struct file *f1 = NULL;
430 struct file *f2 = NULL;
432 swap(f1, fp->fi_fds[oflag]);
433 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
434 swap(f2, fp->fi_fds[O_RDWR]);
435 spin_unlock(&fp->fi_lock);
443 static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
445 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
447 if (access & NFS4_SHARE_ACCESS_WRITE)
448 __nfs4_file_put_access(fp, O_WRONLY);
449 if (access & NFS4_SHARE_ACCESS_READ)
450 __nfs4_file_put_access(fp, O_RDONLY);
453 static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct
456 struct idr *stateids = &cl->cl_stateids;
457 struct nfs4_stid *stid;
460 stid = kmem_cache_alloc(slab, GFP_KERNEL);
464 new_id = idr_alloc_cyclic(stateids, stid, 0, 0, GFP_KERNEL);
467 stid->sc_client = cl;
469 stid->sc_stateid.si_opaque.so_id = new_id;
470 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
471 /* Will be incremented before return to client: */
472 stid->sc_stateid.si_generation = 0;
475 * It shouldn't be a problem to reuse an opaque stateid value.
476 * I don't think it is for 4.1. But with 4.0 I worry that, for
477 * example, a stray write retransmission could be accepted by
478 * the server when it should have been rejected. Therefore,
479 * adopt a trick from the sctp code to attempt to maximize the
480 * amount of time until an id is reused, by ensuring they always
481 * "increase" (mod INT_MAX):
485 kmem_cache_free(slab, stid);
489 static struct nfs4_ol_stateid * nfs4_alloc_stateid(struct nfs4_client *clp)
491 return openlockstateid(nfs4_alloc_stid(clp, stateid_slab));
495 * When we recall a delegation, we should be careful not to hand it
496 * out again straight away.
497 * To ensure this we keep a pair of bloom filters ('new' and 'old')
498 * in which the filehandles of recalled delegations are "stored".
499 * If a filehandle appear in either filter, a delegation is blocked.
500 * When a delegation is recalled, the filehandle is stored in the "new"
502 * Every 30 seconds we swap the filters and clear the "new" one,
503 * unless both are empty of course.
505 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
506 * low 3 bytes as hash-table indices.
508 * 'state_lock', which is always held when block_delegations() is called,
509 * is used to manage concurrent access. Testing does not need the lock
510 * except when swapping the two filters.
512 static struct bloom_pair {
513 int entries, old_entries;
515 int new; /* index into 'set' */
516 DECLARE_BITMAP(set[2], 256);
517 } blocked_delegations;
519 static int delegation_blocked(struct knfsd_fh *fh)
522 struct bloom_pair *bd = &blocked_delegations;
524 if (bd->entries == 0)
526 if (seconds_since_boot() - bd->swap_time > 30) {
527 spin_lock(&state_lock);
528 if (seconds_since_boot() - bd->swap_time > 30) {
529 bd->entries -= bd->old_entries;
530 bd->old_entries = bd->entries;
531 memset(bd->set[bd->new], 0,
534 bd->swap_time = seconds_since_boot();
536 spin_unlock(&state_lock);
538 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
539 if (test_bit(hash&255, bd->set[0]) &&
540 test_bit((hash>>8)&255, bd->set[0]) &&
541 test_bit((hash>>16)&255, bd->set[0]))
544 if (test_bit(hash&255, bd->set[1]) &&
545 test_bit((hash>>8)&255, bd->set[1]) &&
546 test_bit((hash>>16)&255, bd->set[1]))
552 static void block_delegations(struct knfsd_fh *fh)
555 struct bloom_pair *bd = &blocked_delegations;
557 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
559 __set_bit(hash&255, bd->set[bd->new]);
560 __set_bit((hash>>8)&255, bd->set[bd->new]);
561 __set_bit((hash>>16)&255, bd->set[bd->new]);
562 if (bd->entries == 0)
563 bd->swap_time = seconds_since_boot();
567 static struct nfs4_delegation *
568 alloc_init_deleg(struct nfs4_client *clp, struct nfs4_ol_stateid *stp, struct svc_fh *current_fh)
570 struct nfs4_delegation *dp;
572 dprintk("NFSD alloc_init_deleg\n");
573 if (num_delegations > max_delegations)
575 if (delegation_blocked(¤t_fh->fh_handle))
577 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
581 * delegation seqid's are never incremented. The 4.1 special
582 * meaning of seqid 0 isn't meaningful, really, but let's avoid
583 * 0 anyway just for consistency and use 1:
585 dp->dl_stid.sc_stateid.si_generation = 1;
587 INIT_LIST_HEAD(&dp->dl_perfile);
588 INIT_LIST_HEAD(&dp->dl_perclnt);
589 INIT_LIST_HEAD(&dp->dl_recall_lru);
591 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
592 fh_copy_shallow(&dp->dl_fh, ¤t_fh->fh_handle);
594 atomic_set(&dp->dl_count, 1);
595 INIT_WORK(&dp->dl_recall.cb_work, nfsd4_do_callback_rpc);
599 static void remove_stid(struct nfs4_stid *s)
601 struct idr *stateids = &s->sc_client->cl_stateids;
603 idr_remove(stateids, s->sc_stateid.si_opaque.so_id);
606 static void nfs4_free_stid(struct kmem_cache *slab, struct nfs4_stid *s)
608 kmem_cache_free(slab, s);
612 nfs4_put_delegation(struct nfs4_delegation *dp)
614 if (atomic_dec_and_test(&dp->dl_count)) {
615 nfs4_free_stid(deleg_slab, &dp->dl_stid);
620 static void nfs4_put_deleg_lease(struct nfs4_file *fp)
624 if (atomic_dec_and_test(&fp->fi_delegees)) {
625 vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
627 fput(fp->fi_deleg_file);
628 fp->fi_deleg_file = NULL;
632 static void unhash_stid(struct nfs4_stid *s)
638 hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
640 lockdep_assert_held(&state_lock);
642 dp->dl_stid.sc_type = NFS4_DELEG_STID;
643 list_add(&dp->dl_perfile, &fp->fi_delegations);
644 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
647 /* Called under the state lock. */
649 unhash_delegation(struct nfs4_delegation *dp)
651 spin_lock(&state_lock);
652 list_del_init(&dp->dl_perclnt);
653 list_del_init(&dp->dl_perfile);
654 list_del_init(&dp->dl_recall_lru);
655 spin_unlock(&state_lock);
657 nfs4_put_deleg_lease(dp->dl_file);
658 put_nfs4_file(dp->dl_file);
665 static void destroy_revoked_delegation(struct nfs4_delegation *dp)
667 list_del_init(&dp->dl_recall_lru);
668 remove_stid(&dp->dl_stid);
669 nfs4_put_delegation(dp);
672 static void destroy_delegation(struct nfs4_delegation *dp)
674 unhash_delegation(dp);
675 remove_stid(&dp->dl_stid);
676 nfs4_put_delegation(dp);
679 static void revoke_delegation(struct nfs4_delegation *dp)
681 struct nfs4_client *clp = dp->dl_stid.sc_client;
683 if (clp->cl_minorversion == 0)
684 destroy_delegation(dp);
686 unhash_delegation(dp);
687 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
688 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
696 static unsigned int clientid_hashval(u32 id)
698 return id & CLIENT_HASH_MASK;
701 static unsigned int clientstr_hashval(const char *name)
703 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
707 * We store the NONE, READ, WRITE, and BOTH bits separately in the
708 * st_{access,deny}_bmap field of the stateid, in order to track not
709 * only what share bits are currently in force, but also what
710 * combinations of share bits previous opens have used. This allows us
711 * to enforce the recommendation of rfc 3530 14.2.19 that the server
712 * return an error if the client attempt to downgrade to a combination
713 * of share bits not explicable by closing some of its previous opens.
715 * XXX: This enforcement is actually incomplete, since we don't keep
716 * track of access/deny bit combinations; so, e.g., we allow:
718 * OPEN allow read, deny write
719 * OPEN allow both, deny none
720 * DOWNGRADE allow read, deny none
722 * which we should reject.
725 bmap_to_share_mode(unsigned long bmap) {
727 unsigned int access = 0;
729 for (i = 1; i < 4; i++) {
730 if (test_bit(i, &bmap))
736 /* set share access for a given stateid */
738 set_access(u32 access, struct nfs4_ol_stateid *stp)
740 unsigned char mask = 1 << access;
742 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
743 stp->st_access_bmap |= mask;
746 /* clear share access for a given stateid */
748 clear_access(u32 access, struct nfs4_ol_stateid *stp)
750 unsigned char mask = 1 << access;
752 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
753 stp->st_access_bmap &= ~mask;
756 /* test whether a given stateid has access */
758 test_access(u32 access, struct nfs4_ol_stateid *stp)
760 unsigned char mask = 1 << access;
762 return (bool)(stp->st_access_bmap & mask);
765 /* set share deny for a given stateid */
767 set_deny(u32 deny, struct nfs4_ol_stateid *stp)
769 unsigned char mask = 1 << deny;
771 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
772 stp->st_deny_bmap |= mask;
775 /* clear share deny for a given stateid */
777 clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
779 unsigned char mask = 1 << deny;
781 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
782 stp->st_deny_bmap &= ~mask;
785 /* test whether a given stateid is denying specific access */
787 test_deny(u32 deny, struct nfs4_ol_stateid *stp)
789 unsigned char mask = 1 << deny;
791 return (bool)(stp->st_deny_bmap & mask);
794 static int nfs4_access_to_omode(u32 access)
796 switch (access & NFS4_SHARE_ACCESS_BOTH) {
797 case NFS4_SHARE_ACCESS_READ:
799 case NFS4_SHARE_ACCESS_WRITE:
801 case NFS4_SHARE_ACCESS_BOTH:
809 * A stateid that had a deny mode associated with it is being released
810 * or downgraded. Recalculate the deny mode on the file.
813 recalculate_deny_mode(struct nfs4_file *fp)
815 struct nfs4_ol_stateid *stp;
817 spin_lock(&fp->fi_lock);
818 fp->fi_share_deny = 0;
819 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
820 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
821 spin_unlock(&fp->fi_lock);
825 reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
830 for (i = 1; i < 4; i++) {
831 if ((i & deny) != i) {
837 /* Recalculate per-file deny mode if there was a change */
839 recalculate_deny_mode(stp->st_file);
842 /* release all access and file references for a given stateid */
844 release_all_access(struct nfs4_ol_stateid *stp)
847 struct nfs4_file *fp = stp->st_file;
849 if (fp && stp->st_deny_bmap != 0)
850 recalculate_deny_mode(fp);
852 for (i = 1; i < 4; i++) {
853 if (test_access(i, stp))
854 nfs4_file_put_access(stp->st_file, i);
855 clear_access(i, stp);
859 static void unhash_generic_stateid(struct nfs4_ol_stateid *stp)
861 struct nfs4_file *fp = stp->st_file;
863 spin_lock(&fp->fi_lock);
864 list_del(&stp->st_perfile);
865 spin_unlock(&fp->fi_lock);
866 list_del(&stp->st_perstateowner);
869 static void close_generic_stateid(struct nfs4_ol_stateid *stp)
871 release_all_access(stp);
872 put_nfs4_file(stp->st_file);
876 static void free_generic_stateid(struct nfs4_ol_stateid *stp)
878 remove_stid(&stp->st_stid);
879 nfs4_free_stid(stateid_slab, &stp->st_stid);
882 static void __release_lock_stateid(struct nfs4_ol_stateid *stp)
886 list_del(&stp->st_locks);
887 unhash_generic_stateid(stp);
888 unhash_stid(&stp->st_stid);
889 file = find_any_file(stp->st_file);
891 filp_close(file, (fl_owner_t)lockowner(stp->st_stateowner));
892 close_generic_stateid(stp);
893 free_generic_stateid(stp);
896 static void unhash_lockowner(struct nfs4_lockowner *lo)
898 struct nfs4_ol_stateid *stp;
900 list_del(&lo->lo_owner.so_strhash);
901 while (!list_empty(&lo->lo_owner.so_stateids)) {
902 stp = list_first_entry(&lo->lo_owner.so_stateids,
903 struct nfs4_ol_stateid, st_perstateowner);
904 __release_lock_stateid(stp);
908 static void nfs4_free_lockowner(struct nfs4_lockowner *lo)
910 kfree(lo->lo_owner.so_owner.data);
911 kmem_cache_free(lockowner_slab, lo);
914 static void release_lockowner(struct nfs4_lockowner *lo)
916 unhash_lockowner(lo);
917 nfs4_free_lockowner(lo);
920 static void release_lockowner_if_empty(struct nfs4_lockowner *lo)
922 if (list_empty(&lo->lo_owner.so_stateids))
923 release_lockowner(lo);
926 static void release_lock_stateid(struct nfs4_ol_stateid *stp)
928 struct nfs4_lockowner *lo;
930 lo = lockowner(stp->st_stateowner);
931 __release_lock_stateid(stp);
932 release_lockowner_if_empty(lo);
935 static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp)
937 struct nfs4_ol_stateid *stp;
939 while (!list_empty(&open_stp->st_locks)) {
940 stp = list_entry(open_stp->st_locks.next,
941 struct nfs4_ol_stateid, st_locks);
942 release_lock_stateid(stp);
946 static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
948 unhash_generic_stateid(stp);
949 release_open_stateid_locks(stp);
950 close_generic_stateid(stp);
953 static void release_open_stateid(struct nfs4_ol_stateid *stp)
955 unhash_open_stateid(stp);
956 free_generic_stateid(stp);
959 static void unhash_openowner(struct nfs4_openowner *oo)
961 struct nfs4_ol_stateid *stp;
963 list_del(&oo->oo_owner.so_strhash);
964 list_del(&oo->oo_perclient);
965 while (!list_empty(&oo->oo_owner.so_stateids)) {
966 stp = list_first_entry(&oo->oo_owner.so_stateids,
967 struct nfs4_ol_stateid, st_perstateowner);
968 release_open_stateid(stp);
972 static void release_last_closed_stateid(struct nfs4_openowner *oo)
974 struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
977 free_generic_stateid(s);
978 oo->oo_last_closed_stid = NULL;
982 static void nfs4_free_openowner(struct nfs4_openowner *oo)
984 kfree(oo->oo_owner.so_owner.data);
985 kmem_cache_free(openowner_slab, oo);
988 static void release_openowner(struct nfs4_openowner *oo)
990 unhash_openowner(oo);
991 list_del(&oo->oo_close_lru);
992 release_last_closed_stateid(oo);
993 nfs4_free_openowner(oo);
997 hash_sessionid(struct nfs4_sessionid *sessionid)
999 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1001 return sid->sequence % SESSION_HASH_SIZE;
1006 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1008 u32 *ptr = (u32 *)(&sessionid->data[0]);
1009 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1013 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1019 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1020 * won't be used for replay.
1022 void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1024 struct nfs4_stateowner *so = cstate->replay_owner;
1026 if (nfserr == nfserr_replay_me)
1029 if (!seqid_mutating_err(ntohl(nfserr))) {
1030 cstate->replay_owner = NULL;
1035 if (so->so_is_open_owner)
1036 release_last_closed_stateid(openowner(so));
1042 gen_sessionid(struct nfsd4_session *ses)
1044 struct nfs4_client *clp = ses->se_client;
1045 struct nfsd4_sessionid *sid;
1047 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1048 sid->clientid = clp->cl_clientid;
1049 sid->sequence = current_sessionid++;
1054 * The protocol defines ca_maxresponssize_cached to include the size of
1055 * the rpc header, but all we need to cache is the data starting after
1056 * the end of the initial SEQUENCE operation--the rest we regenerate
1057 * each time. Therefore we can advertise a ca_maxresponssize_cached
1058 * value that is the number of bytes in our cache plus a few additional
1059 * bytes. In order to stay on the safe side, and not promise more than
1060 * we can cache, those additional bytes must be the minimum possible: 24
1061 * bytes of rpc header (xid through accept state, with AUTH_NULL
1062 * verifier), 12 for the compound header (with zero-length tag), and 44
1063 * for the SEQUENCE op response:
1065 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1068 free_session_slots(struct nfsd4_session *ses)
1072 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1073 kfree(ses->se_slots[i]);
1077 * We don't actually need to cache the rpc and session headers, so we
1078 * can allocate a little less for each slot:
1080 static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
1084 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1087 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1088 return size + sizeof(struct nfsd4_slot);
1092 * XXX: If we run out of reserved DRC memory we could (up to a point)
1093 * re-negotiate active sessions and reduce their slot usage to make
1094 * room for new connections. For now we just fail the create session.
1096 static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
1098 u32 slotsize = slot_bytes(ca);
1099 u32 num = ca->maxreqs;
1102 spin_lock(&nfsd_drc_lock);
1103 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1104 nfsd_drc_max_mem - nfsd_drc_mem_used);
1105 num = min_t(int, num, avail / slotsize);
1106 nfsd_drc_mem_used += num * slotsize;
1107 spin_unlock(&nfsd_drc_lock);
1112 static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
1114 int slotsize = slot_bytes(ca);
1116 spin_lock(&nfsd_drc_lock);
1117 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
1118 spin_unlock(&nfsd_drc_lock);
1121 static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1122 struct nfsd4_channel_attrs *battrs)
1124 int numslots = fattrs->maxreqs;
1125 int slotsize = slot_bytes(fattrs);
1126 struct nfsd4_session *new;
1129 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
1130 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1131 mem = numslots * sizeof(struct nfsd4_slot *);
1133 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
1136 /* allocate each struct nfsd4_slot and data cache in one piece */
1137 for (i = 0; i < numslots; i++) {
1138 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
1139 if (!new->se_slots[i])
1143 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1144 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1149 kfree(new->se_slots[i]);
1154 static void free_conn(struct nfsd4_conn *c)
1156 svc_xprt_put(c->cn_xprt);
1160 static void nfsd4_conn_lost(struct svc_xpt_user *u)
1162 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1163 struct nfs4_client *clp = c->cn_session->se_client;
1165 spin_lock(&clp->cl_lock);
1166 if (!list_empty(&c->cn_persession)) {
1167 list_del(&c->cn_persession);
1170 nfsd4_probe_callback(clp);
1171 spin_unlock(&clp->cl_lock);
1174 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
1176 struct nfsd4_conn *conn;
1178 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1181 svc_xprt_get(rqstp->rq_xprt);
1182 conn->cn_xprt = rqstp->rq_xprt;
1183 conn->cn_flags = flags;
1184 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1188 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1190 conn->cn_session = ses;
1191 list_add(&conn->cn_persession, &ses->se_conns);
1194 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1196 struct nfs4_client *clp = ses->se_client;
1198 spin_lock(&clp->cl_lock);
1199 __nfsd4_hash_conn(conn, ses);
1200 spin_unlock(&clp->cl_lock);
1203 static int nfsd4_register_conn(struct nfsd4_conn *conn)
1205 conn->cn_xpt_user.callback = nfsd4_conn_lost;
1206 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
1209 static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
1213 nfsd4_hash_conn(conn, ses);
1214 ret = nfsd4_register_conn(conn);
1216 /* oops; xprt is already down: */
1217 nfsd4_conn_lost(&conn->cn_xpt_user);
1218 if (conn->cn_flags & NFS4_CDFC4_BACK) {
1219 /* callback channel may be back up */
1220 nfsd4_probe_callback(ses->se_client);
1224 static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
1226 u32 dir = NFS4_CDFC4_FORE;
1228 if (cses->flags & SESSION4_BACK_CHAN)
1229 dir |= NFS4_CDFC4_BACK;
1230 return alloc_conn(rqstp, dir);
1233 /* must be called under client_lock */
1234 static void nfsd4_del_conns(struct nfsd4_session *s)
1236 struct nfs4_client *clp = s->se_client;
1237 struct nfsd4_conn *c;
1239 spin_lock(&clp->cl_lock);
1240 while (!list_empty(&s->se_conns)) {
1241 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1242 list_del_init(&c->cn_persession);
1243 spin_unlock(&clp->cl_lock);
1245 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1248 spin_lock(&clp->cl_lock);
1250 spin_unlock(&clp->cl_lock);
1253 static void __free_session(struct nfsd4_session *ses)
1255 free_session_slots(ses);
1259 static void free_session(struct nfsd4_session *ses)
1261 struct nfsd_net *nn = net_generic(ses->se_client->net, nfsd_net_id);
1263 lockdep_assert_held(&nn->client_lock);
1264 nfsd4_del_conns(ses);
1265 nfsd4_put_drc_mem(&ses->se_fchannel);
1266 __free_session(ses);
1269 static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
1272 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1274 new->se_client = clp;
1277 INIT_LIST_HEAD(&new->se_conns);
1279 new->se_cb_seq_nr = 1;
1280 new->se_flags = cses->flags;
1281 new->se_cb_prog = cses->callback_prog;
1282 new->se_cb_sec = cses->cb_sec;
1283 atomic_set(&new->se_ref, 0);
1284 idx = hash_sessionid(&new->se_sessionid);
1285 spin_lock(&nn->client_lock);
1286 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
1287 spin_lock(&clp->cl_lock);
1288 list_add(&new->se_perclnt, &clp->cl_sessions);
1289 spin_unlock(&clp->cl_lock);
1290 spin_unlock(&nn->client_lock);
1292 if (cses->flags & SESSION4_BACK_CHAN) {
1293 struct sockaddr *sa = svc_addr(rqstp);
1295 * This is a little silly; with sessions there's no real
1296 * use for the callback address. Use the peer address
1297 * as a reasonable default for now, but consider fixing
1298 * the rpc client not to require an address in the
1301 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1302 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
1306 /* caller must hold client_lock */
1307 static struct nfsd4_session *
1308 __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
1310 struct nfsd4_session *elem;
1312 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1314 dump_sessionid(__func__, sessionid);
1315 idx = hash_sessionid(sessionid);
1316 /* Search in the appropriate list */
1317 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
1318 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1319 NFS4_MAX_SESSIONID_LEN)) {
1324 dprintk("%s: session not found\n", __func__);
1328 static struct nfsd4_session *
1329 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1332 struct nfsd4_session *session;
1333 __be32 status = nfserr_badsession;
1335 session = __find_in_sessionid_hashtbl(sessionid, net);
1338 status = nfsd4_get_session_locked(session);
1346 /* caller must hold client_lock */
1348 unhash_session(struct nfsd4_session *ses)
1350 list_del(&ses->se_hash);
1351 spin_lock(&ses->se_client->cl_lock);
1352 list_del(&ses->se_perclnt);
1353 spin_unlock(&ses->se_client->cl_lock);
1356 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1358 STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
1360 if (clid->cl_boot == nn->boot_time)
1362 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
1363 clid->cl_boot, clid->cl_id, nn->boot_time);
1368 * XXX Should we use a slab cache ?
1369 * This type of memory management is somewhat inefficient, but we use it
1370 * anyway since SETCLIENTID is not a common operation.
1372 static struct nfs4_client *alloc_client(struct xdr_netobj name)
1374 struct nfs4_client *clp;
1376 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1379 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
1380 if (clp->cl_name.data == NULL) {
1384 clp->cl_name.len = name.len;
1385 INIT_LIST_HEAD(&clp->cl_sessions);
1386 idr_init(&clp->cl_stateids);
1387 atomic_set(&clp->cl_refcount, 0);
1388 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1389 INIT_LIST_HEAD(&clp->cl_idhash);
1390 INIT_LIST_HEAD(&clp->cl_openowners);
1391 INIT_LIST_HEAD(&clp->cl_delegations);
1392 INIT_LIST_HEAD(&clp->cl_lru);
1393 INIT_LIST_HEAD(&clp->cl_callbacks);
1394 INIT_LIST_HEAD(&clp->cl_revoked);
1395 spin_lock_init(&clp->cl_lock);
1396 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1401 free_client(struct nfs4_client *clp)
1403 struct nfsd_net __maybe_unused *nn = net_generic(clp->net, nfsd_net_id);
1405 lockdep_assert_held(&nn->client_lock);
1406 while (!list_empty(&clp->cl_sessions)) {
1407 struct nfsd4_session *ses;
1408 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1410 list_del(&ses->se_perclnt);
1411 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1414 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
1415 free_svc_cred(&clp->cl_cred);
1416 kfree(clp->cl_name.data);
1417 idr_destroy(&clp->cl_stateids);
1421 /* must be called under the client_lock */
1423 unhash_client_locked(struct nfs4_client *clp)
1425 struct nfsd4_session *ses;
1427 list_del(&clp->cl_lru);
1428 spin_lock(&clp->cl_lock);
1429 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1430 list_del_init(&ses->se_hash);
1431 spin_unlock(&clp->cl_lock);
1435 destroy_client(struct nfs4_client *clp)
1437 struct nfs4_openowner *oo;
1438 struct nfs4_delegation *dp;
1439 struct list_head reaplist;
1440 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1442 INIT_LIST_HEAD(&reaplist);
1443 spin_lock(&state_lock);
1444 while (!list_empty(&clp->cl_delegations)) {
1445 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
1446 list_del_init(&dp->dl_perclnt);
1447 /* Ensure that deleg break won't try to requeue it */
1449 list_move(&dp->dl_recall_lru, &reaplist);
1451 spin_unlock(&state_lock);
1452 while (!list_empty(&reaplist)) {
1453 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
1454 destroy_delegation(dp);
1456 list_splice_init(&clp->cl_revoked, &reaplist);
1457 while (!list_empty(&reaplist)) {
1458 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
1459 destroy_revoked_delegation(dp);
1461 while (!list_empty(&clp->cl_openowners)) {
1462 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1463 release_openowner(oo);
1465 nfsd4_shutdown_callback(clp);
1466 if (clp->cl_cb_conn.cb_xprt)
1467 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
1468 list_del(&clp->cl_idhash);
1469 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1470 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1472 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1473 spin_lock(&nn->client_lock);
1474 unhash_client_locked(clp);
1475 WARN_ON_ONCE(atomic_read(&clp->cl_refcount));
1477 spin_unlock(&nn->client_lock);
1480 static void expire_client(struct nfs4_client *clp)
1482 nfsd4_client_record_remove(clp);
1483 destroy_client(clp);
1486 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1488 memcpy(target->cl_verifier.data, source->data,
1489 sizeof(target->cl_verifier.data));
1492 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1494 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1495 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1498 static int copy_cred(struct svc_cred *target, struct svc_cred *source)
1500 if (source->cr_principal) {
1501 target->cr_principal =
1502 kstrdup(source->cr_principal, GFP_KERNEL);
1503 if (target->cr_principal == NULL)
1506 target->cr_principal = NULL;
1507 target->cr_flavor = source->cr_flavor;
1508 target->cr_uid = source->cr_uid;
1509 target->cr_gid = source->cr_gid;
1510 target->cr_group_info = source->cr_group_info;
1511 get_group_info(target->cr_group_info);
1512 target->cr_gss_mech = source->cr_gss_mech;
1513 if (source->cr_gss_mech)
1514 gss_mech_get(source->cr_gss_mech);
1519 compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1523 res = o1->len - o2->len;
1526 return (long long)memcmp(o1->data, o2->data, o1->len);
1529 static int same_name(const char *n1, const char *n2)
1531 return 0 == memcmp(n1, n2, HEXDIR_LEN);
1535 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1537 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
1541 same_clid(clientid_t *cl1, clientid_t *cl2)
1543 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
1546 static bool groups_equal(struct group_info *g1, struct group_info *g2)
1550 if (g1->ngroups != g2->ngroups)
1552 for (i=0; i<g1->ngroups; i++)
1553 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
1559 * RFC 3530 language requires clid_inuse be returned when the
1560 * "principal" associated with a requests differs from that previously
1561 * used. We use uid, gid's, and gss principal string as our best
1562 * approximation. We also don't want to allow non-gss use of a client
1563 * established using gss: in theory cr_principal should catch that
1564 * change, but in practice cr_principal can be null even in the gss case
1565 * since gssd doesn't always pass down a principal string.
1567 static bool is_gss_cred(struct svc_cred *cr)
1569 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1570 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1575 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1577 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
1578 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1579 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
1580 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1582 if (cr1->cr_principal == cr2->cr_principal)
1584 if (!cr1->cr_principal || !cr2->cr_principal)
1586 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
1589 static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1591 struct svc_cred *cr = &rqstp->rq_cred;
1594 if (!cr->cr_gss_mech)
1596 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1597 return service == RPC_GSS_SVC_INTEGRITY ||
1598 service == RPC_GSS_SVC_PRIVACY;
1601 static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1603 struct svc_cred *cr = &rqstp->rq_cred;
1605 if (!cl->cl_mach_cred)
1607 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1609 if (!svc_rqst_integrity_protected(rqstp))
1611 if (!cr->cr_principal)
1613 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1616 static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
1618 static u32 current_clientid = 1;
1620 clp->cl_clientid.cl_boot = nn->boot_time;
1621 clp->cl_clientid.cl_id = current_clientid++;
1624 static void gen_confirm(struct nfs4_client *clp)
1630 * This is opaque to client, so no need to byte-swap. Use
1631 * __force to keep sparse happy
1633 verf[0] = (__force __be32)get_seconds();
1634 verf[1] = (__force __be32)i++;
1635 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
1638 static struct nfs4_stid *find_stateid(struct nfs4_client *cl, stateid_t *t)
1640 struct nfs4_stid *ret;
1642 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1643 if (!ret || !ret->sc_type)
1648 static struct nfs4_stid *find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
1650 struct nfs4_stid *s;
1652 s = find_stateid(cl, t);
1655 if (typemask & s->sc_type)
1660 static struct nfs4_client *create_client(struct xdr_netobj name,
1661 struct svc_rqst *rqstp, nfs4_verifier *verf)
1663 struct nfs4_client *clp;
1664 struct sockaddr *sa = svc_addr(rqstp);
1666 struct net *net = SVC_NET(rqstp);
1667 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1669 clp = alloc_client(name);
1673 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1675 spin_lock(&nn->client_lock);
1677 spin_unlock(&nn->client_lock);
1680 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_do_callback_rpc);
1681 clp->cl_time = get_seconds();
1682 clear_bit(0, &clp->cl_cb_slot_busy);
1683 copy_verf(clp, verf);
1684 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
1686 clp->cl_cb_session = NULL;
1692 add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1694 struct rb_node **new = &(root->rb_node), *parent = NULL;
1695 struct nfs4_client *clp;
1698 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1701 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1702 new = &((*new)->rb_left);
1704 new = &((*new)->rb_right);
1707 rb_link_node(&new_clp->cl_namenode, parent, new);
1708 rb_insert_color(&new_clp->cl_namenode, root);
1711 static struct nfs4_client *
1712 find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1715 struct rb_node *node = root->rb_node;
1716 struct nfs4_client *clp;
1719 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1720 cmp = compare_blob(&clp->cl_name, name);
1722 node = node->rb_left;
1724 node = node->rb_right;
1732 add_to_unconfirmed(struct nfs4_client *clp)
1734 unsigned int idhashval;
1735 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1737 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
1738 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
1739 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1740 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
1745 move_to_confirmed(struct nfs4_client *clp)
1747 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1748 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1750 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
1751 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
1752 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1753 add_clp_to_name_tree(clp, &nn->conf_name_tree);
1754 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
1758 static struct nfs4_client *
1759 find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
1761 struct nfs4_client *clp;
1762 unsigned int idhashval = clientid_hashval(clid->cl_id);
1764 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
1765 if (same_clid(&clp->cl_clientid, clid)) {
1766 if ((bool)clp->cl_minorversion != sessions)
1775 static struct nfs4_client *
1776 find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1778 struct list_head *tbl = nn->conf_id_hashtbl;
1780 return find_client_in_id_table(tbl, clid, sessions);
1783 static struct nfs4_client *
1784 find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1786 struct list_head *tbl = nn->unconf_id_hashtbl;
1788 return find_client_in_id_table(tbl, clid, sessions);
1791 static bool clp_used_exchangeid(struct nfs4_client *clp)
1793 return clp->cl_exchange_flags != 0;
1796 static struct nfs4_client *
1797 find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
1799 return find_clp_in_name_tree(name, &nn->conf_name_tree);
1802 static struct nfs4_client *
1803 find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
1805 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
1809 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
1811 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
1812 struct sockaddr *sa = svc_addr(rqstp);
1813 u32 scopeid = rpc_get_scope_id(sa);
1814 unsigned short expected_family;
1816 /* Currently, we only support tcp and tcp6 for the callback channel */
1817 if (se->se_callback_netid_len == 3 &&
1818 !memcmp(se->se_callback_netid_val, "tcp", 3))
1819 expected_family = AF_INET;
1820 else if (se->se_callback_netid_len == 4 &&
1821 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1822 expected_family = AF_INET6;
1826 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
1827 se->se_callback_addr_len,
1828 (struct sockaddr *)&conn->cb_addr,
1829 sizeof(conn->cb_addr));
1831 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
1834 if (conn->cb_addr.ss_family == AF_INET6)
1835 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
1837 conn->cb_prog = se->se_callback_prog;
1838 conn->cb_ident = se->se_callback_ident;
1839 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
1842 conn->cb_addr.ss_family = AF_UNSPEC;
1843 conn->cb_addrlen = 0;
1844 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
1845 "will not receive delegations\n",
1846 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1852 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
1855 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1857 struct xdr_buf *buf = resp->xdr.buf;
1858 struct nfsd4_slot *slot = resp->cstate.slot;
1861 dprintk("--> %s slot %p\n", __func__, slot);
1863 slot->sl_opcnt = resp->opcnt;
1864 slot->sl_status = resp->cstate.status;
1866 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
1867 if (nfsd4_not_cached(resp)) {
1868 slot->sl_datalen = 0;
1871 base = resp->cstate.data_offset;
1872 slot->sl_datalen = buf->len - base;
1873 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
1874 WARN("%s: sessions DRC could not cache compound\n", __func__);
1879 * Encode the replay sequence operation from the slot values.
1880 * If cachethis is FALSE encode the uncached rep error on the next
1881 * operation which sets resp->p and increments resp->opcnt for
1882 * nfs4svc_encode_compoundres.
1886 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
1887 struct nfsd4_compoundres *resp)
1889 struct nfsd4_op *op;
1890 struct nfsd4_slot *slot = resp->cstate.slot;
1892 /* Encode the replayed sequence operation */
1893 op = &args->ops[resp->opcnt - 1];
1894 nfsd4_encode_operation(resp, op);
1896 /* Return nfserr_retry_uncached_rep in next operation. */
1897 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
1898 op = &args->ops[resp->opcnt++];
1899 op->status = nfserr_retry_uncached_rep;
1900 nfsd4_encode_operation(resp, op);
1906 * The sequence operation is not cached because we can use the slot and
1910 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
1911 struct nfsd4_sequence *seq)
1913 struct nfsd4_slot *slot = resp->cstate.slot;
1914 struct xdr_stream *xdr = &resp->xdr;
1918 dprintk("--> %s slot %p\n", __func__, slot);
1920 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
1924 p = xdr_reserve_space(xdr, slot->sl_datalen);
1927 return nfserr_serverfault;
1929 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
1930 xdr_commit_encode(xdr);
1932 resp->opcnt = slot->sl_opcnt;
1933 return slot->sl_status;
1937 * Set the exchange_id flags returned by the server.
1940 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1942 /* pNFS is not supported */
1943 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
1945 /* Referrals are supported, Migration is not. */
1946 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
1948 /* set the wire flags to return to client. */
1949 clid->flags = new->cl_exchange_flags;
1952 static bool client_has_state(struct nfs4_client *clp)
1955 * Note clp->cl_openowners check isn't quite right: there's no
1956 * need to count owners without stateid's.
1958 * Also note we should probably be using this in 4.0 case too.
1960 return !list_empty(&clp->cl_openowners)
1961 || !list_empty(&clp->cl_delegations)
1962 || !list_empty(&clp->cl_sessions);
1966 nfsd4_exchange_id(struct svc_rqst *rqstp,
1967 struct nfsd4_compound_state *cstate,
1968 struct nfsd4_exchange_id *exid)
1970 struct nfs4_client *unconf, *conf, *new;
1972 char addr_str[INET6_ADDRSTRLEN];
1973 nfs4_verifier verf = exid->verifier;
1974 struct sockaddr *sa = svc_addr(rqstp);
1975 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
1976 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1978 rpc_ntop(sa, addr_str, sizeof(addr_str));
1979 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
1980 "ip_addr=%s flags %x, spa_how %d\n",
1981 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
1982 addr_str, exid->flags, exid->spa_how);
1984 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
1985 return nfserr_inval;
1987 switch (exid->spa_how) {
1989 if (!svc_rqst_integrity_protected(rqstp))
1990 return nfserr_inval;
1993 default: /* checked by xdr code */
1996 return nfserr_encr_alg_unsupp;
1999 /* Cases below refer to rfc 5661 section 18.35.4: */
2001 conf = find_confirmed_client_by_name(&exid->clname, nn);
2003 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2004 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2007 if (!clp_used_exchangeid(conf)) { /* buggy client */
2008 status = nfserr_inval;
2011 if (!mach_creds_match(conf, rqstp)) {
2012 status = nfserr_wrong_cred;
2015 if (!creds_match) { /* case 9 */
2016 status = nfserr_perm;
2019 if (!verfs_match) { /* case 8 */
2020 status = nfserr_not_same;
2024 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
2028 if (!creds_match) { /* case 3 */
2029 if (client_has_state(conf)) {
2030 status = nfserr_clid_inuse;
2033 expire_client(conf);
2036 if (verfs_match) { /* case 2 */
2037 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
2041 /* case 5, client reboot */
2045 if (update) { /* case 7 */
2046 status = nfserr_noent;
2050 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
2051 if (unconf) /* case 4, possible retry or client restart */
2052 expire_client(unconf);
2054 /* case 1 (normal case) */
2056 new = create_client(exid->clname, rqstp, &verf);
2058 status = nfserr_jukebox;
2061 new->cl_minorversion = cstate->minorversion;
2062 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
2065 add_to_unconfirmed(new);
2067 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
2068 exid->clientid.cl_id = new->cl_clientid.cl_id;
2070 exid->seqid = new->cl_cs_slot.sl_seqid + 1;
2071 nfsd4_set_ex_flags(new, exid);
2073 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
2074 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
2078 nfs4_unlock_state();
2083 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
2085 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2088 /* The slot is in use, and no response has been sent. */
2090 if (seqid == slot_seqid)
2091 return nfserr_jukebox;
2093 return nfserr_seq_misordered;
2095 /* Note unsigned 32-bit arithmetic handles wraparound: */
2096 if (likely(seqid == slot_seqid + 1))
2098 if (seqid == slot_seqid)
2099 return nfserr_replay_cache;
2100 return nfserr_seq_misordered;
2104 * Cache the create session result into the create session single DRC
2105 * slot cache by saving the xdr structure. sl_seqid has been set.
2106 * Do this for solo or embedded create session operations.
2109 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
2110 struct nfsd4_clid_slot *slot, __be32 nfserr)
2112 slot->sl_status = nfserr;
2113 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2117 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2118 struct nfsd4_clid_slot *slot)
2120 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2121 return slot->sl_status;
2124 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2125 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2126 1 + /* MIN tag is length with zero, only length */ \
2127 3 + /* version, opcount, opcode */ \
2128 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2129 /* seqid, slotID, slotID, cache */ \
2130 4 ) * sizeof(__be32))
2132 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2133 2 + /* verifier: AUTH_NULL, length 0 */\
2135 1 + /* MIN tag is length with zero, only length */ \
2136 3 + /* opcount, opcode, opstatus*/ \
2137 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2138 /* seqid, slotID, slotID, slotID, status */ \
2139 5 ) * sizeof(__be32))
2141 static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
2143 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2145 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2146 return nfserr_toosmall;
2147 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2148 return nfserr_toosmall;
2149 ca->headerpadsz = 0;
2150 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2151 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2152 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2153 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2154 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2155 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2157 * Note decreasing slot size below client's request may make it
2158 * difficult for client to function correctly, whereas
2159 * decreasing the number of slots will (just?) affect
2160 * performance. When short on memory we therefore prefer to
2161 * decrease number of slots instead of their size. Clients that
2162 * request larger slots than they need will get poor results:
2164 ca->maxreqs = nfsd4_get_drc_mem(ca);
2166 return nfserr_jukebox;
2171 #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2172 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2173 #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2174 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2176 static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2178 ca->headerpadsz = 0;
2181 * These RPC_MAX_HEADER macros are overkill, especially since we
2182 * don't even do gss on the backchannel yet. But this is still
2183 * less than 1k. Tighten up this estimate in the unlikely event
2184 * it turns out to be a problem for some client:
2186 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
2187 return nfserr_toosmall;
2188 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
2189 return nfserr_toosmall;
2190 ca->maxresp_cached = 0;
2192 return nfserr_toosmall;
2197 static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2199 switch (cbs->flavor) {
2205 * GSS case: the spec doesn't allow us to return this
2206 * error. But it also doesn't allow us not to support
2208 * I'd rather this fail hard than return some error the
2209 * client might think it can already handle:
2211 return nfserr_encr_alg_unsupp;
2216 nfsd4_create_session(struct svc_rqst *rqstp,
2217 struct nfsd4_compound_state *cstate,
2218 struct nfsd4_create_session *cr_ses)
2220 struct sockaddr *sa = svc_addr(rqstp);
2221 struct nfs4_client *conf, *unconf;
2222 struct nfsd4_session *new;
2223 struct nfsd4_conn *conn;
2224 struct nfsd4_clid_slot *cs_slot = NULL;
2226 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2228 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2229 return nfserr_inval;
2230 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2233 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
2236 status = check_backchannel_attrs(&cr_ses->back_channel);
2238 goto out_release_drc_mem;
2239 status = nfserr_jukebox;
2240 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
2242 goto out_release_drc_mem;
2243 conn = alloc_conn_from_crses(rqstp, cr_ses);
2245 goto out_free_session;
2248 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
2249 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
2250 WARN_ON_ONCE(conf && unconf);
2253 status = nfserr_wrong_cred;
2254 if (!mach_creds_match(conf, rqstp))
2256 cs_slot = &conf->cl_cs_slot;
2257 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2258 if (status == nfserr_replay_cache) {
2259 status = nfsd4_replay_create_session(cr_ses, cs_slot);
2261 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
2262 status = nfserr_seq_misordered;
2265 } else if (unconf) {
2266 struct nfs4_client *old;
2267 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
2268 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
2269 status = nfserr_clid_inuse;
2272 status = nfserr_wrong_cred;
2273 if (!mach_creds_match(unconf, rqstp))
2275 cs_slot = &unconf->cl_cs_slot;
2276 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2278 /* an unconfirmed replay returns misordered */
2279 status = nfserr_seq_misordered;
2282 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
2284 status = mark_client_expired(old);
2289 move_to_confirmed(unconf);
2292 status = nfserr_stale_clientid;
2297 * We do not support RDMA or persistent sessions
2299 cr_ses->flags &= ~SESSION4_PERSIST;
2300 cr_ses->flags &= ~SESSION4_RDMA;
2302 init_session(rqstp, new, conf, cr_ses);
2303 nfsd4_init_conn(rqstp, conn, new);
2305 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
2306 NFS4_MAX_SESSIONID_LEN);
2307 cs_slot->sl_seqid++;
2308 cr_ses->seqid = cs_slot->sl_seqid;
2310 /* cache solo and embedded create sessions under the state lock */
2311 nfsd4_cache_create_session(cr_ses, cs_slot, status);
2312 nfs4_unlock_state();
2315 nfs4_unlock_state();
2318 __free_session(new);
2319 out_release_drc_mem:
2320 nfsd4_put_drc_mem(&cr_ses->fore_channel);
2324 static __be32 nfsd4_map_bcts_dir(u32 *dir)
2327 case NFS4_CDFC4_FORE:
2328 case NFS4_CDFC4_BACK:
2330 case NFS4_CDFC4_FORE_OR_BOTH:
2331 case NFS4_CDFC4_BACK_OR_BOTH:
2332 *dir = NFS4_CDFC4_BOTH;
2335 return nfserr_inval;
2338 __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2340 struct nfsd4_session *session = cstate->session;
2341 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2344 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2347 spin_lock(&nn->client_lock);
2348 session->se_cb_prog = bc->bc_cb_program;
2349 session->se_cb_sec = bc->bc_cb_sec;
2350 spin_unlock(&nn->client_lock);
2352 nfsd4_probe_callback(session->se_client);
2357 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2358 struct nfsd4_compound_state *cstate,
2359 struct nfsd4_bind_conn_to_session *bcts)
2362 struct nfsd4_conn *conn;
2363 struct nfsd4_session *session;
2364 struct net *net = SVC_NET(rqstp);
2365 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2367 if (!nfsd4_last_compound_op(rqstp))
2368 return nfserr_not_only_op;
2370 spin_lock(&nn->client_lock);
2371 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
2372 spin_unlock(&nn->client_lock);
2374 goto out_no_session;
2375 status = nfserr_wrong_cred;
2376 if (!mach_creds_match(session->se_client, rqstp))
2378 status = nfsd4_map_bcts_dir(&bcts->dir);
2381 conn = alloc_conn(rqstp, bcts->dir);
2382 status = nfserr_jukebox;
2385 nfsd4_init_conn(rqstp, conn, session);
2388 nfsd4_put_session(session);
2390 nfs4_unlock_state();
2394 static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2398 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2402 nfsd4_destroy_session(struct svc_rqst *r,
2403 struct nfsd4_compound_state *cstate,
2404 struct nfsd4_destroy_session *sessionid)
2406 struct nfsd4_session *ses;
2408 int ref_held_by_me = 0;
2409 struct net *net = SVC_NET(r);
2410 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2413 status = nfserr_not_only_op;
2414 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
2415 if (!nfsd4_last_compound_op(r))
2419 dump_sessionid(__func__, &sessionid->sessionid);
2420 spin_lock(&nn->client_lock);
2421 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
2423 goto out_client_lock;
2424 status = nfserr_wrong_cred;
2425 if (!mach_creds_match(ses->se_client, r))
2426 goto out_put_session;
2427 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
2429 goto out_put_session;
2430 unhash_session(ses);
2431 spin_unlock(&nn->client_lock);
2433 nfsd4_probe_callback_sync(ses->se_client);
2435 spin_lock(&nn->client_lock);
2438 nfsd4_put_session_locked(ses);
2440 spin_unlock(&nn->client_lock);
2442 nfs4_unlock_state();
2446 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
2448 struct nfsd4_conn *c;
2450 list_for_each_entry(c, &s->se_conns, cn_persession) {
2451 if (c->cn_xprt == xpt) {
2458 static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
2460 struct nfs4_client *clp = ses->se_client;
2461 struct nfsd4_conn *c;
2462 __be32 status = nfs_ok;
2465 spin_lock(&clp->cl_lock);
2466 c = __nfsd4_find_conn(new->cn_xprt, ses);
2469 status = nfserr_conn_not_bound_to_session;
2470 if (clp->cl_mach_cred)
2472 __nfsd4_hash_conn(new, ses);
2473 spin_unlock(&clp->cl_lock);
2474 ret = nfsd4_register_conn(new);
2476 /* oops; xprt is already down: */
2477 nfsd4_conn_lost(&new->cn_xpt_user);
2480 spin_unlock(&clp->cl_lock);
2485 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2487 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2489 return args->opcnt > session->se_fchannel.maxops;
2492 static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2493 struct nfsd4_session *session)
2495 struct xdr_buf *xb = &rqstp->rq_arg;
2497 return xb->len > session->se_fchannel.maxreq_sz;
2501 nfsd4_sequence(struct svc_rqst *rqstp,
2502 struct nfsd4_compound_state *cstate,
2503 struct nfsd4_sequence *seq)
2505 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2506 struct xdr_stream *xdr = &resp->xdr;
2507 struct nfsd4_session *session;
2508 struct nfs4_client *clp;
2509 struct nfsd4_slot *slot;
2510 struct nfsd4_conn *conn;
2513 struct net *net = SVC_NET(rqstp);
2514 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2516 if (resp->opcnt != 1)
2517 return nfserr_sequence_pos;
2520 * Will be either used or freed by nfsd4_sequence_check_conn
2523 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2525 return nfserr_jukebox;
2527 spin_lock(&nn->client_lock);
2528 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
2530 goto out_no_session;
2531 clp = session->se_client;
2533 status = nfserr_too_many_ops;
2534 if (nfsd4_session_too_many_ops(rqstp, session))
2535 goto out_put_session;
2537 status = nfserr_req_too_big;
2538 if (nfsd4_request_too_big(rqstp, session))
2539 goto out_put_session;
2541 status = nfserr_badslot;
2542 if (seq->slotid >= session->se_fchannel.maxreqs)
2543 goto out_put_session;
2545 slot = session->se_slots[seq->slotid];
2546 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2548 /* We do not negotiate the number of slots yet, so set the
2549 * maxslots to the session maxreqs which is used to encode
2550 * sr_highest_slotid and the sr_target_slot id to maxslots */
2551 seq->maxslots = session->se_fchannel.maxreqs;
2553 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2554 slot->sl_flags & NFSD4_SLOT_INUSE);
2555 if (status == nfserr_replay_cache) {
2556 status = nfserr_seq_misordered;
2557 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
2558 goto out_put_session;
2559 cstate->slot = slot;
2560 cstate->session = session;
2562 /* Return the cached reply status and set cstate->status
2563 * for nfsd4_proc_compound processing */
2564 status = nfsd4_replay_cache_entry(resp, seq);
2565 cstate->status = nfserr_replay_cache;
2569 goto out_put_session;
2571 status = nfsd4_sequence_check_conn(conn, session);
2574 goto out_put_session;
2576 buflen = (seq->cachethis) ?
2577 session->se_fchannel.maxresp_cached :
2578 session->se_fchannel.maxresp_sz;
2579 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2581 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
2582 goto out_put_session;
2583 svc_reserve(rqstp, buflen);
2586 /* Success! bump slot seqid */
2587 slot->sl_seqid = seq->seqid;
2588 slot->sl_flags |= NFSD4_SLOT_INUSE;
2590 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
2592 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
2594 cstate->slot = slot;
2595 cstate->session = session;
2599 switch (clp->cl_cb_state) {
2601 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2603 case NFSD4_CB_FAULT:
2604 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2607 seq->status_flags = 0;
2609 if (!list_empty(&clp->cl_revoked))
2610 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
2614 spin_unlock(&nn->client_lock);
2617 nfsd4_put_session_locked(session);
2618 goto out_no_session;
2622 nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2624 struct nfsd4_compound_state *cs = &resp->cstate;
2626 if (nfsd4_has_session(cs)) {
2627 if (cs->status != nfserr_replay_cache) {
2628 nfsd4_store_cache_entry(resp);
2629 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2631 /* Drop session reference that was taken in nfsd4_sequence() */
2632 nfsd4_put_session(cs->session);
2634 put_client_renew(cs->clp);
2638 nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2640 struct nfs4_client *conf, *unconf, *clp;
2642 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2645 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
2646 conf = find_confirmed_client(&dc->clientid, true, nn);
2647 WARN_ON_ONCE(conf && unconf);
2652 if (client_has_state(conf)) {
2653 status = nfserr_clientid_busy;
2659 status = nfserr_stale_clientid;
2662 if (!mach_creds_match(clp, rqstp)) {
2663 status = nfserr_wrong_cred;
2668 nfs4_unlock_state();
2673 nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2677 if (rc->rca_one_fs) {
2678 if (!cstate->current_fh.fh_dentry)
2679 return nfserr_nofilehandle;
2681 * We don't take advantage of the rca_one_fs case.
2682 * That's OK, it's optional, we can safely ignore it.
2688 status = nfserr_complete_already;
2689 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2690 &cstate->session->se_client->cl_flags))
2693 status = nfserr_stale_clientid;
2694 if (is_client_expired(cstate->session->se_client))
2696 * The following error isn't really legal.
2697 * But we only get here if the client just explicitly
2698 * destroyed the client. Surely it no longer cares what
2699 * error it gets back on an operation for the dead
2705 nfsd4_client_record_create(cstate->session->se_client);
2707 nfs4_unlock_state();
2712 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2713 struct nfsd4_setclientid *setclid)
2715 struct xdr_netobj clname = setclid->se_name;
2716 nfs4_verifier clverifier = setclid->se_verf;
2717 struct nfs4_client *conf, *unconf, *new;
2719 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2721 /* Cases below refer to rfc 3530 section 14.2.33: */
2723 conf = find_confirmed_client_by_name(&clname, nn);
2726 status = nfserr_clid_inuse;
2727 if (clp_used_exchangeid(conf))
2729 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
2730 char addr_str[INET6_ADDRSTRLEN];
2731 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2733 dprintk("NFSD: setclientid: string in use by client "
2734 "at %s\n", addr_str);
2738 unconf = find_unconfirmed_client_by_name(&clname, nn);
2740 expire_client(unconf);
2741 status = nfserr_jukebox;
2742 new = create_client(clname, rqstp, &clverifier);
2745 if (conf && same_verf(&conf->cl_verifier, &clverifier))
2746 /* case 1: probable callback update */
2747 copy_clid(new, conf);
2748 else /* case 4 (new client) or cases 2, 3 (client reboot): */
2750 new->cl_minorversion = 0;
2751 gen_callback(new, setclid, rqstp);
2752 add_to_unconfirmed(new);
2753 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2754 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2755 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2758 nfs4_unlock_state();
2764 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2765 struct nfsd4_compound_state *cstate,
2766 struct nfsd4_setclientid_confirm *setclientid_confirm)
2768 struct nfs4_client *conf, *unconf;
2769 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2770 clientid_t * clid = &setclientid_confirm->sc_clientid;
2772 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2774 if (STALE_CLIENTID(clid, nn))
2775 return nfserr_stale_clientid;
2778 conf = find_confirmed_client(clid, false, nn);
2779 unconf = find_unconfirmed_client(clid, false, nn);
2781 * We try hard to give out unique clientid's, so if we get an
2782 * attempt to confirm the same clientid with a different cred,
2783 * there's a bug somewhere. Let's charitably assume it's our
2786 status = nfserr_serverfault;
2787 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
2789 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
2791 /* cases below refer to rfc 3530 section 14.2.34: */
2792 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
2793 if (conf && !unconf) /* case 2: probable retransmit */
2795 else /* case 4: client hasn't noticed we rebooted yet? */
2796 status = nfserr_stale_clientid;
2800 if (conf) { /* case 1: callback update */
2801 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2802 nfsd4_probe_callback(conf);
2803 expire_client(unconf);
2804 } else { /* case 3: normal case; new or rebooted client */
2805 conf = find_confirmed_client_by_name(&unconf->cl_name, nn);
2807 status = mark_client_expired(conf);
2810 expire_client(conf);
2812 move_to_confirmed(unconf);
2813 nfsd4_probe_callback(unconf);
2816 nfs4_unlock_state();
2820 static struct nfs4_file *nfsd4_alloc_file(void)
2822 return kmem_cache_alloc(file_slab, GFP_KERNEL);
2825 /* OPEN Share state helper functions */
2826 static void nfsd4_init_file(struct nfs4_file *fp, struct inode *ino)
2828 unsigned int hashval = file_hashval(ino);
2830 lockdep_assert_held(&state_lock);
2832 atomic_set(&fp->fi_ref, 1);
2833 spin_lock_init(&fp->fi_lock);
2834 INIT_LIST_HEAD(&fp->fi_stateids);
2835 INIT_LIST_HEAD(&fp->fi_delegations);
2838 fp->fi_had_conflict = false;
2839 fp->fi_lease = NULL;
2840 fp->fi_share_deny = 0;
2841 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2842 memset(fp->fi_access, 0, sizeof(fp->fi_access));
2843 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
2847 nfsd4_free_slabs(void)
2849 kmem_cache_destroy(openowner_slab);
2850 kmem_cache_destroy(lockowner_slab);
2851 kmem_cache_destroy(file_slab);
2852 kmem_cache_destroy(stateid_slab);
2853 kmem_cache_destroy(deleg_slab);
2857 nfsd4_init_slabs(void)
2859 openowner_slab = kmem_cache_create("nfsd4_openowners",
2860 sizeof(struct nfs4_openowner), 0, 0, NULL);
2861 if (openowner_slab == NULL)
2863 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
2864 sizeof(struct nfs4_lockowner), 0, 0, NULL);
2865 if (lockowner_slab == NULL)
2866 goto out_free_openowner_slab;
2867 file_slab = kmem_cache_create("nfsd4_files",
2868 sizeof(struct nfs4_file), 0, 0, NULL);
2869 if (file_slab == NULL)
2870 goto out_free_lockowner_slab;
2871 stateid_slab = kmem_cache_create("nfsd4_stateids",
2872 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
2873 if (stateid_slab == NULL)
2874 goto out_free_file_slab;
2875 deleg_slab = kmem_cache_create("nfsd4_delegations",
2876 sizeof(struct nfs4_delegation), 0, 0, NULL);
2877 if (deleg_slab == NULL)
2878 goto out_free_stateid_slab;
2881 out_free_stateid_slab:
2882 kmem_cache_destroy(stateid_slab);
2884 kmem_cache_destroy(file_slab);
2885 out_free_lockowner_slab:
2886 kmem_cache_destroy(lockowner_slab);
2887 out_free_openowner_slab:
2888 kmem_cache_destroy(openowner_slab);
2890 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2894 static void init_nfs4_replay(struct nfs4_replay *rp)
2896 rp->rp_status = nfserr_serverfault;
2898 rp->rp_buf = rp->rp_ibuf;
2901 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
2903 struct nfs4_stateowner *sop;
2905 sop = kmem_cache_alloc(slab, GFP_KERNEL);
2909 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
2910 if (!sop->so_owner.data) {
2911 kmem_cache_free(slab, sop);
2914 sop->so_owner.len = owner->len;
2916 INIT_LIST_HEAD(&sop->so_stateids);
2917 sop->so_client = clp;
2918 init_nfs4_replay(&sop->so_replay);
2922 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
2924 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2926 list_add(&oo->oo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
2927 list_add(&oo->oo_perclient, &clp->cl_openowners);
2930 static struct nfs4_openowner *
2931 alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
2932 struct nfsd4_compound_state *cstate)
2934 struct nfs4_client *clp = cstate->clp;
2935 struct nfs4_openowner *oo;
2937 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
2940 oo->oo_owner.so_is_open_owner = 1;
2941 oo->oo_owner.so_seqid = open->op_seqid;
2942 oo->oo_flags = NFS4_OO_NEW;
2943 if (nfsd4_has_session(cstate))
2944 oo->oo_flags |= NFS4_OO_CONFIRMED;
2946 oo->oo_last_closed_stid = NULL;
2947 INIT_LIST_HEAD(&oo->oo_close_lru);
2948 hash_openowner(oo, clp, strhashval);
2952 static void init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
2953 struct nfs4_openowner *oo = open->op_openowner;
2955 stp->st_stid.sc_type = NFS4_OPEN_STID;
2956 INIT_LIST_HEAD(&stp->st_locks);
2957 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
2958 stp->st_stateowner = &oo->oo_owner;
2961 stp->st_access_bmap = 0;
2962 stp->st_deny_bmap = 0;
2963 set_access(open->op_share_access, stp);
2964 set_deny(open->op_share_deny, stp);
2965 stp->st_openstp = NULL;
2966 spin_lock(&fp->fi_lock);
2967 list_add(&stp->st_perfile, &fp->fi_stateids);
2968 spin_unlock(&fp->fi_lock);
2972 move_to_close_lru(struct nfs4_openowner *oo, struct net *net)
2974 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2976 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
2978 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
2979 oo->oo_time = get_seconds();
2983 same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
2986 return (sop->so_owner.len == owner->len) &&
2987 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
2988 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
2991 static struct nfs4_openowner *
2992 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
2993 bool sessions, struct nfsd_net *nn)
2995 struct nfs4_stateowner *so;
2996 struct nfs4_openowner *oo;
2997 struct nfs4_client *clp;
2999 list_for_each_entry(so, &nn->ownerstr_hashtbl[hashval], so_strhash) {
3000 if (!so->so_is_open_owner)
3002 if (same_owner_str(so, &open->op_owner, &open->op_clientid)) {
3004 clp = oo->oo_owner.so_client;
3005 if ((bool)clp->cl_minorversion != sessions)
3007 renew_client(oo->oo_owner.so_client);
3014 /* search file_hashtbl[] for file */
3015 static struct nfs4_file *
3016 find_file_locked(struct inode *ino)
3018 unsigned int hashval = file_hashval(ino);
3019 struct nfs4_file *fp;
3021 lockdep_assert_held(&state_lock);
3023 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
3024 if (fp->fi_inode == ino) {
3032 static struct nfs4_file *
3033 find_file(struct inode *ino)
3035 struct nfs4_file *fp;
3037 spin_lock(&state_lock);
3038 fp = find_file_locked(ino);
3039 spin_unlock(&state_lock);
3043 static struct nfs4_file *
3044 find_or_add_file(struct inode *ino, struct nfs4_file *new)
3046 struct nfs4_file *fp;
3048 spin_lock(&state_lock);
3049 fp = find_file_locked(ino);
3051 nfsd4_init_file(new, ino);
3054 spin_unlock(&state_lock);
3060 * Called to check deny when READ with all zero stateid or
3061 * WRITE with all zero or all one stateid
3064 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3066 struct inode *ino = current_fh->fh_dentry->d_inode;
3067 struct nfs4_file *fp;
3068 __be32 ret = nfs_ok;
3070 fp = find_file(ino);
3073 /* Check for conflicting share reservations */
3074 spin_lock(&fp->fi_lock);
3075 if (fp->fi_share_deny & deny_type)
3076 ret = nfserr_locked;
3077 spin_unlock(&fp->fi_lock);
3082 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3084 struct nfs4_client *clp = dp->dl_stid.sc_client;
3085 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
3087 lockdep_assert_held(&state_lock);
3088 /* We're assuming the state code never drops its reference
3089 * without first removing the lease. Since we're in this lease
3090 * callback (and since the lease code is serialized by the kernel
3091 * lock) we know the server hasn't removed the lease yet, we know
3092 * it's safe to take a reference: */
3093 atomic_inc(&dp->dl_count);
3096 * If the dl_time != 0, then we know that it has already been
3097 * queued for a lease break. Don't queue it again.
3099 if (dp->dl_time == 0) {
3100 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
3101 dp->dl_time = get_seconds();
3104 block_delegations(&dp->dl_fh);
3106 nfsd4_cb_recall(dp);
3109 /* Called from break_lease() with i_lock held. */
3110 static void nfsd_break_deleg_cb(struct file_lock *fl)
3112 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3113 struct nfs4_delegation *dp;
3116 WARN(1, "(%p)->fl_owner NULL\n", fl);
3119 if (fp->fi_had_conflict) {
3120 WARN(1, "duplicate break on %p\n", fp);
3124 * We don't want the locks code to timeout the lease for us;
3125 * we'll remove it ourself if a delegation isn't returned
3128 fl->fl_break_time = 0;
3130 spin_lock(&state_lock);
3131 fp->fi_had_conflict = true;
3132 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3133 nfsd_break_one_deleg(dp);
3134 spin_unlock(&state_lock);
3138 int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
3141 return lease_modify(onlist, arg);
3146 static const struct lock_manager_operations nfsd_lease_mng_ops = {
3147 .lm_break = nfsd_break_deleg_cb,
3148 .lm_change = nfsd_change_deleg_cb,
3151 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3153 if (nfsd4_has_session(cstate))
3155 if (seqid == so->so_seqid - 1)
3156 return nfserr_replay_me;
3157 if (seqid == so->so_seqid)
3159 return nfserr_bad_seqid;
3162 static __be32 lookup_clientid(clientid_t *clid,
3163 struct nfsd4_compound_state *cstate,
3164 struct nfsd_net *nn)
3166 struct nfs4_client *found;
3169 found = cstate->clp;
3170 if (!same_clid(&found->cl_clientid, clid))
3171 return nfserr_stale_clientid;
3175 if (STALE_CLIENTID(clid, nn))
3176 return nfserr_stale_clientid;
3179 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3180 * cached already then we know this is for is for v4.0 and "sessions"
3183 WARN_ON_ONCE(cstate->session);
3184 found = find_confirmed_client(clid, false, nn);
3186 return nfserr_expired;
3188 /* Cache the nfs4_client in cstate! */
3189 cstate->clp = found;
3190 atomic_inc(&found->cl_refcount);
3195 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
3196 struct nfsd4_open *open, struct nfsd_net *nn)
3198 clientid_t *clientid = &open->op_clientid;
3199 struct nfs4_client *clp = NULL;
3200 unsigned int strhashval;
3201 struct nfs4_openowner *oo = NULL;
3204 if (STALE_CLIENTID(&open->op_clientid, nn))
3205 return nfserr_stale_clientid;
3207 * In case we need it later, after we've already created the
3208 * file and don't want to risk a further failure:
3210 open->op_file = nfsd4_alloc_file();
3211 if (open->op_file == NULL)
3212 return nfserr_jukebox;
3214 status = lookup_clientid(clientid, cstate, nn);
3219 strhashval = ownerstr_hashval(clientid->cl_id, &open->op_owner);
3220 oo = find_openstateowner_str(strhashval, open, cstate->minorversion, nn);
3221 open->op_openowner = oo;
3225 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
3226 /* Replace unconfirmed owners without checking for replay. */
3227 release_openowner(oo);
3228 open->op_openowner = NULL;
3231 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3236 oo = alloc_init_open_stateowner(strhashval, open, cstate);
3238 return nfserr_jukebox;
3239 open->op_openowner = oo;
3241 open->op_stp = nfs4_alloc_stateid(clp);
3243 return nfserr_jukebox;
3247 static inline __be32
3248 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3250 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3251 return nfserr_openmode;
3256 static int share_access_to_flags(u32 share_access)
3258 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3261 static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
3263 struct nfs4_stid *ret;
3265 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
3268 return delegstateid(ret);
3271 static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3273 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3274 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3278 nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
3279 struct nfs4_delegation **dp)
3282 __be32 status = nfserr_bad_stateid;
3284 *dp = find_deleg_stateid(cl, &open->op_delegate_stateid);
3287 flags = share_access_to_flags(open->op_share_access);
3288 status = nfs4_check_delegmode(*dp, flags);
3292 if (!nfsd4_is_deleg_cur(open))
3296 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
3300 static struct nfs4_ol_stateid *
3301 nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
3303 struct nfs4_ol_stateid *local, *ret = NULL;
3304 struct nfs4_openowner *oo = open->op_openowner;
3306 spin_lock(&fp->fi_lock);
3307 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
3308 /* ignore lock owners */
3309 if (local->st_stateowner->so_is_open_owner == 0)
3311 if (local->st_stateowner == &oo->oo_owner) {
3316 spin_unlock(&fp->fi_lock);
3320 static inline int nfs4_access_to_access(u32 nfs4_access)
3324 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3325 flags |= NFSD_MAY_READ;
3326 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3327 flags |= NFSD_MAY_WRITE;
3331 static inline __be32
3332 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3333 struct nfsd4_open *open)
3335 struct iattr iattr = {
3336 .ia_valid = ATTR_SIZE,
3339 if (!open->op_truncate)
3341 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
3342 return nfserr_inval;
3343 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3346 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
3347 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3348 struct nfsd4_open *open)
3350 struct file *filp = NULL;
3352 int oflag = nfs4_access_to_omode(open->op_share_access);
3353 int access = nfs4_access_to_access(open->op_share_access);
3354 unsigned char old_access_bmap, old_deny_bmap;
3356 spin_lock(&fp->fi_lock);
3359 * Are we trying to set a deny mode that would conflict with
3362 status = nfs4_file_check_deny(fp, open->op_share_deny);
3363 if (status != nfs_ok) {
3364 spin_unlock(&fp->fi_lock);
3368 /* set access to the file */
3369 status = nfs4_file_get_access(fp, open->op_share_access);
3370 if (status != nfs_ok) {
3371 spin_unlock(&fp->fi_lock);
3375 /* Set access bits in stateid */
3376 old_access_bmap = stp->st_access_bmap;
3377 set_access(open->op_share_access, stp);
3379 /* Set new deny mask */
3380 old_deny_bmap = stp->st_deny_bmap;
3381 set_deny(open->op_share_deny, stp);
3382 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3384 if (!fp->fi_fds[oflag]) {
3385 spin_unlock(&fp->fi_lock);
3386 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
3388 goto out_put_access;
3389 spin_lock(&fp->fi_lock);
3390 if (!fp->fi_fds[oflag]) {
3391 fp->fi_fds[oflag] = filp;
3395 spin_unlock(&fp->fi_lock);
3399 status = nfsd4_truncate(rqstp, cur_fh, open);
3401 goto out_put_access;
3405 stp->st_access_bmap = old_access_bmap;
3406 nfs4_file_put_access(fp, open->op_share_access);
3407 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3412 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)
3415 unsigned char old_deny_bmap;
3417 if (!test_access(open->op_share_access, stp))
3418 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
3420 /* test and set deny mode */
3421 spin_lock(&fp->fi_lock);
3422 status = nfs4_file_check_deny(fp, open->op_share_deny);
3423 if (status == nfs_ok) {
3424 old_deny_bmap = stp->st_deny_bmap;
3425 set_deny(open->op_share_deny, stp);
3426 fp->fi_share_deny |=
3427 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3429 spin_unlock(&fp->fi_lock);
3431 if (status != nfs_ok)
3434 status = nfsd4_truncate(rqstp, cur_fh, open);
3435 if (status != nfs_ok)
3436 reset_union_bmap_deny(old_deny_bmap, stp);
3441 nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
3443 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
3446 /* Should we give out recallable state?: */
3447 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3449 if (clp->cl_cb_state == NFSD4_CB_UP)
3452 * In the sessions case, since we don't have to establish a
3453 * separate connection for callbacks, we assume it's OK
3454 * until we hear otherwise:
3456 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3459 static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp, int flag)
3461 struct file_lock *fl;
3463 fl = locks_alloc_lock();
3466 locks_init_lock(fl);
3467 fl->fl_lmops = &nfsd_lease_mng_ops;
3468 fl->fl_flags = FL_DELEG;
3469 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3470 fl->fl_end = OFFSET_MAX;
3471 fl->fl_owner = (fl_owner_t)(dp->dl_file);
3472 fl->fl_pid = current->tgid;
3476 static int nfs4_setlease(struct nfs4_delegation *dp)
3478 struct nfs4_file *fp = dp->dl_file;
3479 struct file_lock *fl;
3482 fl = nfs4_alloc_init_lease(dp, NFS4_OPEN_DELEGATE_READ);
3485 fl->fl_file = find_readable_file(fp);
3486 status = vfs_setlease(fl->fl_file, fl->fl_type, &fl);
3490 fp->fi_deleg_file = fl->fl_file;
3491 atomic_set(&fp->fi_delegees, 1);
3492 spin_lock(&state_lock);
3493 hash_delegation_locked(dp, fp);
3494 spin_unlock(&state_lock);
3499 locks_free_lock(fl);
3503 static int nfs4_set_delegation(struct nfs4_delegation *dp, struct nfs4_file *fp)
3505 if (fp->fi_had_conflict)
3510 return nfs4_setlease(dp);
3511 spin_lock(&state_lock);
3512 atomic_inc(&fp->fi_delegees);
3513 if (fp->fi_had_conflict) {
3514 spin_unlock(&state_lock);
3517 hash_delegation_locked(dp, fp);
3518 spin_unlock(&state_lock);
3522 static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3524 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3525 if (status == -EAGAIN)
3526 open->op_why_no_deleg = WND4_CONTENTION;
3528 open->op_why_no_deleg = WND4_RESOURCE;
3529 switch (open->op_deleg_want) {
3530 case NFS4_SHARE_WANT_READ_DELEG:
3531 case NFS4_SHARE_WANT_WRITE_DELEG:
3532 case NFS4_SHARE_WANT_ANY_DELEG:
3534 case NFS4_SHARE_WANT_CANCEL:
3535 open->op_why_no_deleg = WND4_CANCELLED;
3537 case NFS4_SHARE_WANT_NO_DELEG:
3544 * Attempt to hand out a delegation.
3546 * Note we don't support write delegations, and won't until the vfs has
3547 * proper support for them.
3550 nfs4_open_delegation(struct net *net, struct svc_fh *fh,
3551 struct nfsd4_open *open, struct nfs4_ol_stateid *stp)
3553 struct nfs4_delegation *dp;
3554 struct nfs4_openowner *oo = container_of(stp->st_stateowner, struct nfs4_openowner, oo_owner);
3558 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
3559 open->op_recall = 0;
3560 switch (open->op_claim_type) {
3561 case NFS4_OPEN_CLAIM_PREVIOUS:
3563 open->op_recall = 1;
3564 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3567 case NFS4_OPEN_CLAIM_NULL:
3568 case NFS4_OPEN_CLAIM_FH:
3570 * Let's not give out any delegations till everyone's
3571 * had the chance to reclaim theirs....
3573 if (locks_in_grace(net))
3575 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
3578 * Also, if the file was opened for write or
3579 * create, there's a good chance the client's
3580 * about to write to it, resulting in an
3581 * immediate recall (since we don't support
3582 * write delegations):
3584 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
3586 if (open->op_create == NFS4_OPEN_CREATE)
3592 dp = alloc_init_deleg(oo->oo_owner.so_client, stp, fh);
3595 status = nfs4_set_delegation(dp, stp->st_file);
3599 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
3601 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
3602 STATEID_VAL(&dp->dl_stid.sc_stateid));
3603 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
3606 destroy_delegation(dp);
3608 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3609 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
3610 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
3611 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
3612 open->op_recall = 1;
3615 /* 4.1 client asking for a delegation? */
3616 if (open->op_deleg_want)
3617 nfsd4_open_deleg_none_ext(open, status);
3621 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3622 struct nfs4_delegation *dp)
3624 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3625 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3626 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3627 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3628 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3629 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3630 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3631 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3633 /* Otherwise the client must be confused wanting a delegation
3634 * it already has, therefore we don't return
3635 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3640 * called with nfs4_lock_state() held.
3643 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3645 struct nfsd4_compoundres *resp = rqstp->rq_resp;
3646 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
3647 struct nfs4_file *fp = NULL;
3648 struct inode *ino = current_fh->fh_dentry->d_inode;
3649 struct nfs4_ol_stateid *stp = NULL;
3650 struct nfs4_delegation *dp = NULL;
3654 * Lookup file; if found, lookup stateid and check open request,
3655 * and check for delegations in the process of being recalled.
3656 * If not found, create the nfs4_file struct
3658 fp = find_or_add_file(ino, open->op_file);
3659 if (fp != open->op_file) {
3660 status = nfs4_check_deleg(cl, open, &dp);
3663 stp = nfsd4_find_existing_open(fp, open);
3665 open->op_file = NULL;
3666 status = nfserr_bad_stateid;
3667 if (nfsd4_is_deleg_cur(open))
3669 status = nfserr_jukebox;
3673 * OPEN the file, or upgrade an existing OPEN.
3674 * If truncate fails, the OPEN fails.
3677 /* Stateid was found, this is an OPEN upgrade */
3678 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
3683 open->op_stp = NULL;
3684 init_open_stateid(stp, fp, open);
3685 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
3687 release_open_stateid(stp);
3691 update_stateid(&stp->st_stid.sc_stateid);
3692 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
3694 if (nfsd4_has_session(&resp->cstate)) {
3695 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
3696 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3697 open->op_why_no_deleg = WND4_NOT_WANTED;
3703 * Attempt to hand out a delegation. No error return, because the
3704 * OPEN succeeds even if we fail.
3706 nfs4_open_delegation(SVC_NET(rqstp), current_fh, open, stp);
3710 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
3711 STATEID_VAL(&stp->st_stid.sc_stateid));
3713 /* 4.1 client trying to upgrade/downgrade delegation? */
3714 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
3715 open->op_deleg_want)
3716 nfsd4_deleg_xgrade_none_ext(open, dp);
3720 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
3721 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
3723 * To finish the open response, we just need to set the rflags.
3725 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
3726 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
3727 !nfsd4_has_session(&resp->cstate))
3728 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
3733 void nfsd4_cleanup_open_state(struct nfsd4_open *open, __be32 status)
3735 if (open->op_openowner) {
3736 struct nfs4_openowner *oo = open->op_openowner;
3738 if (!list_empty(&oo->oo_owner.so_stateids))
3739 list_del_init(&oo->oo_close_lru);
3740 if (oo->oo_flags & NFS4_OO_NEW) {
3742 release_openowner(oo);
3743 open->op_openowner = NULL;
3745 oo->oo_flags &= ~NFS4_OO_NEW;
3749 nfsd4_free_file(open->op_file);
3751 free_generic_stateid(open->op_stp);
3755 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3758 struct nfs4_client *clp;
3760 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3763 dprintk("process_renew(%08x/%08x): starting\n",
3764 clid->cl_boot, clid->cl_id);
3765 status = lookup_clientid(clid, cstate, nn);
3769 status = nfserr_cb_path_down;
3770 if (!list_empty(&clp->cl_delegations)
3771 && clp->cl_cb_state != NFSD4_CB_UP)
3775 nfs4_unlock_state();
3780 nfsd4_end_grace(struct nfsd_net *nn)
3782 /* do nothing if grace period already ended */
3783 if (nn->grace_ended)
3786 dprintk("NFSD: end of grace period\n");
3787 nn->grace_ended = true;
3788 nfsd4_record_grace_done(nn, nn->boot_time);
3789 locks_end_grace(&nn->nfsd4_manager);
3791 * Now that every NFSv4 client has had the chance to recover and
3792 * to see the (possibly new, possibly shorter) lease time, we
3793 * can safely set the next grace time to the current lease time:
3795 nn->nfsd4_grace = nn->nfsd4_lease;
3799 nfs4_laundromat(struct nfsd_net *nn)
3801 struct nfs4_client *clp;
3802 struct nfs4_openowner *oo;
3803 struct nfs4_delegation *dp;
3804 struct list_head *pos, *next, reaplist;
3805 time_t cutoff = get_seconds() - nn->nfsd4_lease;
3806 time_t t, new_timeo = nn->nfsd4_lease;
3810 dprintk("NFSD: laundromat service - starting\n");
3811 nfsd4_end_grace(nn);
3812 INIT_LIST_HEAD(&reaplist);
3813 spin_lock(&nn->client_lock);
3814 list_for_each_safe(pos, next, &nn->client_lru) {
3815 clp = list_entry(pos, struct nfs4_client, cl_lru);
3816 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
3817 t = clp->cl_time - cutoff;
3818 new_timeo = min(new_timeo, t);
3821 if (mark_client_expired_locked(clp)) {
3822 dprintk("NFSD: client in use (clientid %08x)\n",
3823 clp->cl_clientid.cl_id);
3826 list_move(&clp->cl_lru, &reaplist);
3828 spin_unlock(&nn->client_lock);
3829 list_for_each_safe(pos, next, &reaplist) {
3830 clp = list_entry(pos, struct nfs4_client, cl_lru);
3831 dprintk("NFSD: purging unused client (clientid %08x)\n",
3832 clp->cl_clientid.cl_id);
3835 spin_lock(&state_lock);
3836 list_for_each_safe(pos, next, &nn->del_recall_lru) {
3837 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3838 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
3840 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
3841 t = dp->dl_time - cutoff;
3842 new_timeo = min(new_timeo, t);
3845 list_move(&dp->dl_recall_lru, &reaplist);
3847 spin_unlock(&state_lock);
3848 list_for_each_safe(pos, next, &reaplist) {
3849 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3850 revoke_delegation(dp);
3852 list_for_each_safe(pos, next, &nn->close_lru) {
3853 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
3854 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
3855 t = oo->oo_time - cutoff;
3856 new_timeo = min(new_timeo, t);
3859 release_openowner(oo);
3861 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
3862 nfs4_unlock_state();
3866 static struct workqueue_struct *laundry_wq;
3867 static void laundromat_main(struct work_struct *);
3870 laundromat_main(struct work_struct *laundry)
3873 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
3875 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
3878 t = nfs4_laundromat(nn);
3879 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
3880 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
3883 static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
3885 if (fhp->fh_dentry->d_inode != stp->st_file->fi_inode)
3886 return nfserr_bad_stateid;
3891 access_permit_read(struct nfs4_ol_stateid *stp)
3893 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
3894 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
3895 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
3899 access_permit_write(struct nfs4_ol_stateid *stp)
3901 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
3902 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
3906 __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
3908 __be32 status = nfserr_openmode;
3910 /* For lock stateid's, we test the parent open, not the lock: */
3911 if (stp->st_openstp)
3912 stp = stp->st_openstp;
3913 if ((flags & WR_STATE) && !access_permit_write(stp))
3915 if ((flags & RD_STATE) && !access_permit_read(stp))
3922 static inline __be32
3923 check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
3925 if (ONE_STATEID(stateid) && (flags & RD_STATE))
3927 else if (locks_in_grace(net)) {
3928 /* Answer in remaining cases depends on existence of
3929 * conflicting state; so we must wait out the grace period. */
3930 return nfserr_grace;
3931 } else if (flags & WR_STATE)
3932 return nfs4_share_conflict(current_fh,
3933 NFS4_SHARE_DENY_WRITE);
3934 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
3935 return nfs4_share_conflict(current_fh,
3936 NFS4_SHARE_DENY_READ);
3940 * Allow READ/WRITE during grace period on recovered state only for files
3941 * that are not able to provide mandatory locking.
3944 grace_disallows_io(struct net *net, struct inode *inode)
3946 return locks_in_grace(net) && mandatory_lock(inode);
3949 /* Returns true iff a is later than b: */
3950 static bool stateid_generation_after(stateid_t *a, stateid_t *b)
3952 return (s32)(a->si_generation - b->si_generation) > 0;
3955 static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
3958 * When sessions are used the stateid generation number is ignored
3961 if (has_session && in->si_generation == 0)
3964 if (in->si_generation == ref->si_generation)
3967 /* If the client sends us a stateid from the future, it's buggy: */
3968 if (stateid_generation_after(in, ref))
3969 return nfserr_bad_stateid;
3971 * However, we could see a stateid from the past, even from a
3972 * non-buggy client. For example, if the client sends a lock
3973 * while some IO is outstanding, the lock may bump si_generation
3974 * while the IO is still in flight. The client could avoid that
3975 * situation by waiting for responses on all the IO requests,
3976 * but better performance may result in retrying IO that
3977 * receives an old_stateid error if requests are rarely
3978 * reordered in flight:
3980 return nfserr_old_stateid;
3983 static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
3985 struct nfs4_stid *s;
3986 struct nfs4_ol_stateid *ols;
3989 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3990 return nfserr_bad_stateid;
3991 /* Client debugging aid. */
3992 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
3993 char addr_str[INET6_ADDRSTRLEN];
3994 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
3996 pr_warn_ratelimited("NFSD: client %s testing state ID "
3997 "with incorrect client ID\n", addr_str);
3998 return nfserr_bad_stateid;
4000 s = find_stateid(cl, stateid);
4002 return nfserr_bad_stateid;
4003 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
4006 switch (s->sc_type) {
4007 case NFS4_DELEG_STID:
4009 case NFS4_REVOKED_DELEG_STID:
4010 return nfserr_deleg_revoked;
4011 case NFS4_OPEN_STID:
4012 case NFS4_LOCK_STID:
4013 ols = openlockstateid(s);
4014 if (ols->st_stateowner->so_is_open_owner
4015 && !(openowner(ols->st_stateowner)->oo_flags
4016 & NFS4_OO_CONFIRMED))
4017 return nfserr_bad_stateid;
4020 printk("unknown stateid type %x\n", s->sc_type);
4021 case NFS4_CLOSED_STID:
4022 return nfserr_bad_stateid;
4027 nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4028 stateid_t *stateid, unsigned char typemask,
4029 struct nfs4_stid **s, struct nfsd_net *nn)
4033 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4034 return nfserr_bad_stateid;
4035 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
4036 if (status == nfserr_stale_clientid) {
4037 if (cstate->session)
4038 return nfserr_bad_stateid;
4039 return nfserr_stale_stateid;
4043 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
4045 return nfserr_bad_stateid;
4050 * Checks for stateid operations
4053 nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
4054 stateid_t *stateid, int flags, struct file **filpp)
4056 struct nfs4_stid *s;
4057 struct nfs4_ol_stateid *stp = NULL;
4058 struct nfs4_delegation *dp = NULL;
4059 struct svc_fh *current_fh = &cstate->current_fh;
4060 struct inode *ino = current_fh->fh_dentry->d_inode;
4061 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4062 struct file *file = NULL;
4068 if (grace_disallows_io(net, ino))
4069 return nfserr_grace;
4071 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4072 return check_special_stateids(net, current_fh, stateid, flags);
4076 status = nfsd4_lookup_stateid(cstate, stateid,
4077 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
4081 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4084 switch (s->sc_type) {
4085 case NFS4_DELEG_STID:
4086 dp = delegstateid(s);
4087 status = nfs4_check_delegmode(dp, flags);
4091 file = dp->dl_file->fi_deleg_file;
4094 status = nfserr_serverfault;
4100 case NFS4_OPEN_STID:
4101 case NFS4_LOCK_STID:
4102 stp = openlockstateid(s);
4103 status = nfs4_check_fh(current_fh, stp);
4106 if (stp->st_stateowner->so_is_open_owner
4107 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
4109 status = nfs4_check_openmode(stp, flags);
4113 if (flags & RD_STATE)
4114 file = find_readable_file(stp->st_file);
4116 file = find_writeable_file(stp->st_file);
4120 status = nfserr_bad_stateid;
4127 nfs4_unlock_state();
4132 nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
4134 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
4136 if (check_for_locks(stp->st_file, lo))
4137 return nfserr_locks_held;
4138 release_lockowner_if_empty(lo);
4143 * Test if the stateid is valid
4146 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4147 struct nfsd4_test_stateid *test_stateid)
4149 struct nfsd4_test_stateid_id *stateid;
4150 struct nfs4_client *cl = cstate->session->se_client;
4153 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
4154 stateid->ts_id_status =
4155 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
4156 nfs4_unlock_state();
4162 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4163 struct nfsd4_free_stateid *free_stateid)
4165 stateid_t *stateid = &free_stateid->fr_stateid;
4166 struct nfs4_stid *s;
4167 struct nfs4_delegation *dp;
4168 struct nfs4_client *cl = cstate->session->se_client;
4169 __be32 ret = nfserr_bad_stateid;
4172 s = find_stateid(cl, stateid);
4175 switch (s->sc_type) {
4176 case NFS4_DELEG_STID:
4177 ret = nfserr_locks_held;
4179 case NFS4_OPEN_STID:
4180 case NFS4_LOCK_STID:
4181 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4184 if (s->sc_type == NFS4_LOCK_STID)
4185 ret = nfsd4_free_lock_stateid(openlockstateid(s));
4187 ret = nfserr_locks_held;
4189 case NFS4_REVOKED_DELEG_STID:
4190 dp = delegstateid(s);
4191 destroy_revoked_delegation(dp);
4195 ret = nfserr_bad_stateid;
4198 nfs4_unlock_state();
4205 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4206 RD_STATE : WR_STATE;
4209 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
4211 struct svc_fh *current_fh = &cstate->current_fh;
4212 struct nfs4_stateowner *sop = stp->st_stateowner;
4215 status = nfsd4_check_seqid(cstate, sop, seqid);
4218 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4219 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
4221 * "Closed" stateid's exist *only* to return
4222 * nfserr_replay_me from the previous step, and
4223 * revoked delegations are kept only for free_stateid.
4225 return nfserr_bad_stateid;
4226 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4229 return nfs4_check_fh(current_fh, stp);
4233 * Checks for sequence id mutating operations.
4236 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4237 stateid_t *stateid, char typemask,
4238 struct nfs4_ol_stateid **stpp,
4239 struct nfsd_net *nn)
4242 struct nfs4_stid *s;
4243 struct nfs4_ol_stateid *stp = NULL;
4245 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4246 seqid, STATEID_VAL(stateid));
4249 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
4252 stp = openlockstateid(s);
4253 if (!nfsd4_has_session(cstate))
4254 cstate->replay_owner = stp->st_stateowner;
4256 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
4262 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4263 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
4266 struct nfs4_openowner *oo;
4268 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
4269 NFS4_OPEN_STID, stpp, nn);
4272 oo = openowner((*stpp)->st_stateowner);
4273 if (!(oo->oo_flags & NFS4_OO_CONFIRMED))
4274 return nfserr_bad_stateid;
4279 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4280 struct nfsd4_open_confirm *oc)
4283 struct nfs4_openowner *oo;
4284 struct nfs4_ol_stateid *stp;
4285 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4287 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4288 cstate->current_fh.fh_dentry);
4290 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
4296 status = nfs4_preprocess_seqid_op(cstate,
4297 oc->oc_seqid, &oc->oc_req_stateid,
4298 NFS4_OPEN_STID, &stp, nn);
4301 oo = openowner(stp->st_stateowner);
4302 status = nfserr_bad_stateid;
4303 if (oo->oo_flags & NFS4_OO_CONFIRMED)
4305 oo->oo_flags |= NFS4_OO_CONFIRMED;
4306 update_stateid(&stp->st_stid.sc_stateid);
4307 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4308 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
4309 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
4311 nfsd4_client_record_create(oo->oo_owner.so_client);
4314 nfsd4_bump_seqid(cstate, status);
4315 if (!cstate->replay_owner)
4316 nfs4_unlock_state();
4320 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
4322 if (!test_access(access, stp))
4324 nfs4_file_put_access(stp->st_file, access);
4325 clear_access(access, stp);
4328 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4330 switch (to_access) {
4331 case NFS4_SHARE_ACCESS_READ:
4332 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4333 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4335 case NFS4_SHARE_ACCESS_WRITE:
4336 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4337 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4339 case NFS4_SHARE_ACCESS_BOTH:
4347 nfsd4_open_downgrade(struct svc_rqst *rqstp,
4348 struct nfsd4_compound_state *cstate,
4349 struct nfsd4_open_downgrade *od)
4352 struct nfs4_ol_stateid *stp;
4353 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4355 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4356 cstate->current_fh.fh_dentry);
4358 /* We don't yet support WANT bits: */
4359 if (od->od_deleg_want)
4360 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4364 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
4365 &od->od_stateid, &stp, nn);
4368 status = nfserr_inval;
4369 if (!test_access(od->od_share_access, stp)) {
4370 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
4371 stp->st_access_bmap, od->od_share_access);
4374 if (!test_deny(od->od_share_deny, stp)) {
4375 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
4376 stp->st_deny_bmap, od->od_share_deny);
4379 nfs4_stateid_downgrade(stp, od->od_share_access);
4381 reset_union_bmap_deny(od->od_share_deny, stp);
4383 update_stateid(&stp->st_stid.sc_stateid);
4384 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4387 nfsd4_bump_seqid(cstate, status);
4388 if (!cstate->replay_owner)
4389 nfs4_unlock_state();
4393 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4395 struct nfs4_client *clp = s->st_stid.sc_client;
4396 struct nfs4_openowner *oo = openowner(s->st_stateowner);
4398 s->st_stid.sc_type = NFS4_CLOSED_STID;
4399 unhash_open_stateid(s);
4401 if (clp->cl_minorversion) {
4402 free_generic_stateid(s);
4403 if (list_empty(&oo->oo_owner.so_stateids))
4404 release_openowner(oo);
4406 oo->oo_last_closed_stid = s;
4408 * In the 4.0 case we need to keep the owners around a
4409 * little while to handle CLOSE replay.
4411 if (list_empty(&oo->oo_owner.so_stateids))
4412 move_to_close_lru(oo, clp->net);
4417 * nfs4_unlock_state() called after encode
4420 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4421 struct nfsd4_close *close)
4424 struct nfs4_ol_stateid *stp;
4425 struct net *net = SVC_NET(rqstp);
4426 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4428 dprintk("NFSD: nfsd4_close on file %pd\n",
4429 cstate->current_fh.fh_dentry);
4432 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4434 NFS4_OPEN_STID|NFS4_CLOSED_STID,
4436 nfsd4_bump_seqid(cstate, status);
4439 update_stateid(&stp->st_stid.sc_stateid);
4440 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4442 nfsd4_close_open_stateid(stp);
4444 if (!cstate->replay_owner)
4445 nfs4_unlock_state();
4450 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4451 struct nfsd4_delegreturn *dr)
4453 struct nfs4_delegation *dp;
4454 stateid_t *stateid = &dr->dr_stateid;
4455 struct nfs4_stid *s;
4457 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4459 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
4463 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
4466 dp = delegstateid(s);
4467 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
4471 destroy_delegation(dp);
4473 nfs4_unlock_state();
4479 #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
4482 end_offset(u64 start, u64 len)
4487 return end >= start ? end: NFS4_MAX_UINT64;
4490 /* last octet in a range */
4492 last_byte_offset(u64 start, u64 len)
4498 return end > start ? end - 1: NFS4_MAX_UINT64;
4502 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4503 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4504 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4505 * locking, this prevents us from being completely protocol-compliant. The
4506 * real solution to this problem is to start using unsigned file offsets in
4507 * the VFS, but this is a very deep change!
4510 nfs4_transform_lock_offset(struct file_lock *lock)
4512 if (lock->fl_start < 0)
4513 lock->fl_start = OFFSET_MAX;
4514 if (lock->fl_end < 0)
4515 lock->fl_end = OFFSET_MAX;
4518 /* Hack!: For now, we're defining this just so we can use a pointer to it
4519 * as a unique cookie to identify our (NFSv4's) posix locks. */
4520 static const struct lock_manager_operations nfsd_posix_mng_ops = {
4524 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4526 struct nfs4_lockowner *lo;
4528 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
4529 lo = (struct nfs4_lockowner *) fl->fl_owner;
4530 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4531 lo->lo_owner.so_owner.len, GFP_KERNEL);
4532 if (!deny->ld_owner.data)
4533 /* We just don't care that much */
4535 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4536 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
4539 deny->ld_owner.len = 0;
4540 deny->ld_owner.data = NULL;
4541 deny->ld_clientid.cl_boot = 0;
4542 deny->ld_clientid.cl_id = 0;
4544 deny->ld_start = fl->fl_start;
4545 deny->ld_length = NFS4_MAX_UINT64;
4546 if (fl->fl_end != NFS4_MAX_UINT64)
4547 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4548 deny->ld_type = NFS4_READ_LT;
4549 if (fl->fl_type != F_RDLCK)
4550 deny->ld_type = NFS4_WRITE_LT;
4553 static struct nfs4_lockowner *
4554 find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
4555 struct nfsd_net *nn)
4557 unsigned int strhashval = ownerstr_hashval(clid->cl_id, owner);
4558 struct nfs4_stateowner *so;
4560 list_for_each_entry(so, &nn->ownerstr_hashtbl[strhashval], so_strhash) {
4561 if (so->so_is_open_owner)
4563 if (!same_owner_str(so, owner, clid))
4565 return lockowner(so);
4571 * Alloc a lock owner structure.
4572 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
4575 * strhashval = ownerstr_hashval
4577 static struct nfs4_lockowner *
4578 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_ol_stateid *open_stp, struct nfsd4_lock *lock) {
4579 struct nfs4_lockowner *lo;
4580 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
4582 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4585 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4586 lo->lo_owner.so_is_open_owner = 0;
4587 /* It is the openowner seqid that will be incremented in encode in the
4588 * case of new lockowners; so increment the lock seqid manually: */
4589 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1;
4590 list_add(&lo->lo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
4594 static struct nfs4_ol_stateid *
4595 alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp, struct nfs4_ol_stateid *open_stp)
4597 struct nfs4_ol_stateid *stp;
4598 struct nfs4_client *clp = lo->lo_owner.so_client;
4600 stp = nfs4_alloc_stateid(clp);
4603 stp->st_stid.sc_type = NFS4_LOCK_STID;
4604 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
4605 stp->st_stateowner = &lo->lo_owner;
4608 stp->st_access_bmap = 0;
4609 stp->st_deny_bmap = open_stp->st_deny_bmap;
4610 stp->st_openstp = open_stp;
4611 list_add(&stp->st_locks, &open_stp->st_locks);
4612 spin_lock(&fp->fi_lock);
4613 list_add(&stp->st_perfile, &fp->fi_stateids);
4614 spin_unlock(&fp->fi_lock);
4618 static struct nfs4_ol_stateid *
4619 find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
4621 struct nfs4_ol_stateid *lst;
4623 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
4624 if (lst->st_file == fp)
4632 check_lock_length(u64 offset, u64 length)
4634 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
4635 LOFF_OVERFLOW(offset, length)));
4638 static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
4640 struct nfs4_file *fp = lock_stp->st_file;
4642 lockdep_assert_held(&fp->fi_lock);
4644 if (test_access(access, lock_stp))
4646 __nfs4_file_get_access(fp, access);
4647 set_access(access, lock_stp);
4650 static __be32 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate, struct nfs4_ol_stateid *ost, struct nfsd4_lock *lock, struct nfs4_ol_stateid **lst, bool *new)
4652 struct nfs4_file *fi = ost->st_file;
4653 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
4654 struct nfs4_client *cl = oo->oo_owner.so_client;
4655 struct nfs4_lockowner *lo;
4656 unsigned int strhashval;
4657 struct nfsd_net *nn = net_generic(cl->net, nfsd_net_id);
4659 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, nn);
4661 strhashval = ownerstr_hashval(cl->cl_clientid.cl_id,
4662 &lock->v.new.owner);
4663 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
4665 return nfserr_jukebox;
4667 /* with an existing lockowner, seqids must be the same */
4668 if (!cstate->minorversion &&
4669 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
4670 return nfserr_bad_seqid;
4673 *lst = find_lock_stateid(lo, fi);
4675 *lst = alloc_init_lock_stateid(lo, fi, ost);
4677 release_lockowner_if_empty(lo);
4678 return nfserr_jukebox;
4689 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4690 struct nfsd4_lock *lock)
4692 struct nfs4_openowner *open_sop = NULL;
4693 struct nfs4_lockowner *lock_sop = NULL;
4694 struct nfs4_ol_stateid *lock_stp;
4695 struct nfs4_file *fp;
4696 struct file *filp = NULL;
4697 struct file_lock *file_lock = NULL;
4698 struct file_lock *conflock = NULL;
4700 bool new_state = false;
4703 struct net *net = SVC_NET(rqstp);
4704 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4706 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
4707 (long long) lock->lk_offset,
4708 (long long) lock->lk_length);
4710 if (check_lock_length(lock->lk_offset, lock->lk_length))
4711 return nfserr_inval;
4713 if ((status = fh_verify(rqstp, &cstate->current_fh,
4714 S_IFREG, NFSD_MAY_LOCK))) {
4715 dprintk("NFSD: nfsd4_lock: permission denied!\n");
4721 if (lock->lk_is_new) {
4722 struct nfs4_ol_stateid *open_stp = NULL;
4724 if (nfsd4_has_session(cstate))
4725 /* See rfc 5661 18.10.3: given clientid is ignored: */
4726 memcpy(&lock->v.new.clientid,
4727 &cstate->session->se_client->cl_clientid,
4728 sizeof(clientid_t));
4730 status = nfserr_stale_clientid;
4731 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
4734 /* validate and update open stateid and open seqid */
4735 status = nfs4_preprocess_confirmed_seqid_op(cstate,
4736 lock->lk_new_open_seqid,
4737 &lock->lk_new_open_stateid,
4741 open_sop = openowner(open_stp->st_stateowner);
4742 status = nfserr_bad_stateid;
4743 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
4744 &lock->v.new.clientid))
4746 status = lookup_or_create_lock_state(cstate, open_stp, lock,
4747 &lock_stp, &new_state);
4749 status = nfs4_preprocess_seqid_op(cstate,
4750 lock->lk_old_lock_seqid,
4751 &lock->lk_old_lock_stateid,
4752 NFS4_LOCK_STID, &lock_stp, nn);
4755 lock_sop = lockowner(lock_stp->st_stateowner);
4757 lkflg = setlkflg(lock->lk_type);
4758 status = nfs4_check_openmode(lock_stp, lkflg);
4762 status = nfserr_grace;
4763 if (locks_in_grace(net) && !lock->lk_reclaim)
4765 status = nfserr_no_grace;
4766 if (!locks_in_grace(net) && lock->lk_reclaim)
4769 file_lock = locks_alloc_lock();
4771 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4772 status = nfserr_jukebox;
4776 fp = lock_stp->st_file;
4777 locks_init_lock(file_lock);
4778 switch (lock->lk_type) {
4781 spin_lock(&fp->fi_lock);
4782 filp = find_readable_file_locked(fp);
4784 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
4785 spin_unlock(&fp->fi_lock);
4786 file_lock->fl_type = F_RDLCK;
4789 case NFS4_WRITEW_LT:
4790 spin_lock(&fp->fi_lock);
4791 filp = find_writeable_file_locked(fp);
4793 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
4794 spin_unlock(&fp->fi_lock);
4795 file_lock->fl_type = F_WRLCK;
4798 status = nfserr_inval;
4802 status = nfserr_openmode;
4805 file_lock->fl_owner = (fl_owner_t)lock_sop;
4806 file_lock->fl_pid = current->tgid;
4807 file_lock->fl_file = filp;
4808 file_lock->fl_flags = FL_POSIX;
4809 file_lock->fl_lmops = &nfsd_posix_mng_ops;
4810 file_lock->fl_start = lock->lk_offset;
4811 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
4812 nfs4_transform_lock_offset(file_lock);
4814 conflock = locks_alloc_lock();
4816 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4817 status = nfserr_jukebox;
4821 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
4823 case 0: /* success! */
4824 update_stateid(&lock_stp->st_stid.sc_stateid);
4825 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
4829 case (EAGAIN): /* conflock holds conflicting lock */
4830 status = nfserr_denied;
4831 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
4832 nfs4_set_lock_denied(conflock, &lock->lk_denied);
4835 status = nfserr_deadlock;
4838 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
4839 status = nfserrno(err);
4845 if (status && new_state)
4846 release_lock_stateid(lock_stp);
4847 nfsd4_bump_seqid(cstate, status);
4848 if (!cstate->replay_owner)
4849 nfs4_unlock_state();
4851 locks_free_lock(file_lock);
4853 locks_free_lock(conflock);
4858 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
4859 * so we do a temporary open here just to get an open file to pass to
4860 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
4863 static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
4866 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
4868 err = nfserrno(vfs_test_lock(file, lock));
4878 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4879 struct nfsd4_lockt *lockt)
4881 struct file_lock *file_lock = NULL;
4882 struct nfs4_lockowner *lo;
4884 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4886 if (locks_in_grace(SVC_NET(rqstp)))
4887 return nfserr_grace;
4889 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
4890 return nfserr_inval;
4894 if (!nfsd4_has_session(cstate)) {
4895 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
4900 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
4903 file_lock = locks_alloc_lock();
4905 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4906 status = nfserr_jukebox;
4909 locks_init_lock(file_lock);
4910 switch (lockt->lt_type) {
4913 file_lock->fl_type = F_RDLCK;
4916 case NFS4_WRITEW_LT:
4917 file_lock->fl_type = F_WRLCK;
4920 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
4921 status = nfserr_inval;
4925 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner, nn);
4927 file_lock->fl_owner = (fl_owner_t)lo;
4928 file_lock->fl_pid = current->tgid;
4929 file_lock->fl_flags = FL_POSIX;
4931 file_lock->fl_start = lockt->lt_offset;
4932 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
4934 nfs4_transform_lock_offset(file_lock);
4936 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
4940 if (file_lock->fl_type != F_UNLCK) {
4941 status = nfserr_denied;
4942 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
4945 nfs4_unlock_state();
4947 locks_free_lock(file_lock);
4952 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4953 struct nfsd4_locku *locku)
4955 struct nfs4_ol_stateid *stp;
4956 struct file *filp = NULL;
4957 struct file_lock *file_lock = NULL;
4960 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4962 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
4963 (long long) locku->lu_offset,
4964 (long long) locku->lu_length);
4966 if (check_lock_length(locku->lu_offset, locku->lu_length))
4967 return nfserr_inval;
4971 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
4972 &locku->lu_stateid, NFS4_LOCK_STID,
4976 filp = find_any_file(stp->st_file);
4978 status = nfserr_lock_range;
4981 file_lock = locks_alloc_lock();
4983 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4984 status = nfserr_jukebox;
4987 locks_init_lock(file_lock);
4988 file_lock->fl_type = F_UNLCK;
4989 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
4990 file_lock->fl_pid = current->tgid;
4991 file_lock->fl_file = filp;
4992 file_lock->fl_flags = FL_POSIX;
4993 file_lock->fl_lmops = &nfsd_posix_mng_ops;
4994 file_lock->fl_start = locku->lu_offset;
4996 file_lock->fl_end = last_byte_offset(locku->lu_offset,
4998 nfs4_transform_lock_offset(file_lock);
5000 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
5002 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
5005 update_stateid(&stp->st_stid.sc_stateid);
5006 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
5010 nfsd4_bump_seqid(cstate, status);
5011 if (!cstate->replay_owner)
5012 nfs4_unlock_state();
5014 locks_free_lock(file_lock);
5018 status = nfserrno(err);
5024 * 1: locks held by lockowner
5025 * 0: no locks held by lockowner
5028 check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner)
5030 struct file_lock **flpp;
5031 struct inode *inode = filp->fi_inode;
5034 spin_lock(&inode->i_lock);
5035 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
5036 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
5042 spin_unlock(&inode->i_lock);
5047 nfsd4_release_lockowner(struct svc_rqst *rqstp,
5048 struct nfsd4_compound_state *cstate,
5049 struct nfsd4_release_lockowner *rlockowner)
5051 clientid_t *clid = &rlockowner->rl_clientid;
5052 struct nfs4_stateowner *sop = NULL, *tmp;
5053 struct nfs4_lockowner *lo;
5054 struct nfs4_ol_stateid *stp;
5055 struct xdr_netobj *owner = &rlockowner->rl_owner;
5056 unsigned int hashval = ownerstr_hashval(clid->cl_id, owner);
5058 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5060 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5061 clid->cl_boot, clid->cl_id);
5065 status = lookup_clientid(clid, cstate, nn);
5069 status = nfserr_locks_held;
5071 /* Find the matching lock stateowner */
5072 list_for_each_entry(tmp, &nn->ownerstr_hashtbl[hashval], so_strhash) {
5073 if (tmp->so_is_open_owner)
5075 if (same_owner_str(tmp, owner, clid)) {
5081 /* No matching owner found, maybe a replay? Just declare victory... */
5087 lo = lockowner(sop);
5088 /* see if there are still any locks associated with it */
5089 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5090 if (check_for_locks(stp->st_file, lo))
5095 release_lockowner(lo);
5097 nfs4_unlock_state();
5101 static inline struct nfs4_client_reclaim *
5104 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
5108 nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
5110 struct nfs4_client_reclaim *crp;
5112 crp = nfsd4_find_reclaim_client(name, nn);
5113 return (crp && crp->cr_clp);
5117 * failure => all reset bets are off, nfserr_no_grace...
5119 struct nfs4_client_reclaim *
5120 nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
5122 unsigned int strhashval;
5123 struct nfs4_client_reclaim *crp;
5125 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5126 crp = alloc_reclaim();
5128 strhashval = clientstr_hashval(name);
5129 INIT_LIST_HEAD(&crp->cr_strhash);
5130 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
5131 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
5133 nn->reclaim_str_hashtbl_size++;
5139 nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
5141 list_del(&crp->cr_strhash);
5143 nn->reclaim_str_hashtbl_size--;
5147 nfs4_release_reclaim(struct nfsd_net *nn)
5149 struct nfs4_client_reclaim *crp = NULL;
5152 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5153 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5154 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
5155 struct nfs4_client_reclaim, cr_strhash);
5156 nfs4_remove_reclaim_record(crp, nn);
5159 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
5163 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
5164 struct nfs4_client_reclaim *
5165 nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
5167 unsigned int strhashval;
5168 struct nfs4_client_reclaim *crp = NULL;
5170 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
5172 strhashval = clientstr_hashval(recdir);
5173 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
5174 if (same_name(crp->cr_recdir, recdir)) {
5182 * Called from OPEN. Look for clientid in reclaim list.
5185 nfs4_check_open_reclaim(clientid_t *clid,
5186 struct nfsd4_compound_state *cstate,
5187 struct nfsd_net *nn)
5191 /* find clientid in conf_id_hashtbl */
5192 status = lookup_clientid(clid, cstate, nn);
5194 return nfserr_reclaim_bad;
5196 if (nfsd4_client_record_check(cstate->clp))
5197 return nfserr_reclaim_bad;
5202 #ifdef CONFIG_NFSD_FAULT_INJECTION
5204 u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
5206 if (mark_client_expired(clp))
5212 u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
5214 char buf[INET6_ADDRSTRLEN];
5215 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5216 printk(KERN_INFO "NFS Client: %s\n", buf);
5220 static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5223 char buf[INET6_ADDRSTRLEN];
5224 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5225 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5228 static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
5229 void (*func)(struct nfs4_ol_stateid *))
5231 struct nfs4_openowner *oop;
5232 struct nfs4_ol_stateid *stp, *st_next;
5233 struct nfs4_ol_stateid *lst, *lst_next;
5236 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
5237 list_for_each_entry_safe(stp, st_next,
5238 &oop->oo_owner.so_stateids, st_perstateowner) {
5239 list_for_each_entry_safe(lst, lst_next,
5240 &stp->st_locks, st_locks) {
5252 u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
5254 return nfsd_foreach_client_lock(clp, max, release_lock_stateid);
5257 u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
5259 u64 count = nfsd_foreach_client_lock(clp, max, NULL);
5260 nfsd_print_count(clp, count, "locked files");
5264 static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
5266 struct nfs4_openowner *oop, *next;
5269 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
5279 u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
5281 return nfsd_foreach_client_open(clp, max, release_openowner);
5284 u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
5286 u64 count = nfsd_foreach_client_open(clp, max, NULL);
5287 nfsd_print_count(clp, count, "open files");
5291 static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
5292 struct list_head *victims)
5294 struct nfs4_delegation *dp, *next;
5297 lockdep_assert_held(&state_lock);
5298 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
5301 * It's not safe to mess with delegations that have a
5302 * non-zero dl_time. They might have already been broken
5303 * and could be processed by the laundromat outside of
5304 * the state_lock. Just leave them be.
5306 if (dp->dl_time != 0)
5310 * Increment dl_time to ensure that delegation breaks
5311 * don't monkey with it now that we are.
5314 list_move(&dp->dl_recall_lru, victims);
5322 u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
5324 struct nfs4_delegation *dp, *next;
5328 spin_lock(&state_lock);
5329 count = nfsd_find_all_delegations(clp, max, &victims);
5330 spin_unlock(&state_lock);
5332 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru)
5333 revoke_delegation(dp);
5338 u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
5340 struct nfs4_delegation *dp;
5344 spin_lock(&state_lock);
5345 count = nfsd_find_all_delegations(clp, max, &victims);
5346 while (!list_empty(&victims)) {
5347 dp = list_first_entry(&victims, struct nfs4_delegation,
5349 list_del_init(&dp->dl_recall_lru);
5351 nfsd_break_one_deleg(dp);
5353 spin_unlock(&state_lock);
5358 u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
5362 spin_lock(&state_lock);
5363 count = nfsd_find_all_delegations(clp, max, NULL);
5364 spin_unlock(&state_lock);
5366 nfsd_print_count(clp, count, "delegations");
5370 u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
5372 struct nfs4_client *clp, *next;
5374 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
5376 if (!nfsd_netns_ready(nn))
5379 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
5380 count += func(clp, max - count);
5381 if ((max != 0) && (count >= max))
5388 struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5390 struct nfs4_client *clp;
5391 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
5393 if (!nfsd_netns_ready(nn))
5396 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5397 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5403 #endif /* CONFIG_NFSD_FAULT_INJECTION */
5406 * Since the lifetime of a delegation isn't limited to that of an open, a
5407 * client may quite reasonably hang on to a delegation as long as it has
5408 * the inode cached. This becomes an obvious problem the first time a
5409 * client's inode cache approaches the size of the server's total memory.
5411 * For now we avoid this problem by imposing a hard limit on the number
5412 * of delegations, which varies according to the server's memory size.
5415 set_max_delegations(void)
5418 * Allow at most 4 delegations per megabyte of RAM. Quick
5419 * estimates suggest that in the worst case (where every delegation
5420 * is for a different inode), a delegation could take about 1.5K,
5421 * giving a worst case usage of about 6% of memory.
5423 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
5426 static int nfs4_state_create_net(struct net *net)
5428 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5431 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5432 CLIENT_HASH_SIZE, GFP_KERNEL);
5433 if (!nn->conf_id_hashtbl)
5435 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5436 CLIENT_HASH_SIZE, GFP_KERNEL);
5437 if (!nn->unconf_id_hashtbl)
5439 nn->ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
5440 OWNER_HASH_SIZE, GFP_KERNEL);
5441 if (!nn->ownerstr_hashtbl)
5443 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
5444 SESSION_HASH_SIZE, GFP_KERNEL);
5445 if (!nn->sessionid_hashtbl)
5448 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5449 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
5450 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
5452 for (i = 0; i < OWNER_HASH_SIZE; i++)
5453 INIT_LIST_HEAD(&nn->ownerstr_hashtbl[i]);
5454 for (i = 0; i < SESSION_HASH_SIZE; i++)
5455 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
5456 nn->conf_name_tree = RB_ROOT;
5457 nn->unconf_name_tree = RB_ROOT;
5458 INIT_LIST_HEAD(&nn->client_lru);
5459 INIT_LIST_HEAD(&nn->close_lru);
5460 INIT_LIST_HEAD(&nn->del_recall_lru);
5461 spin_lock_init(&nn->client_lock);
5463 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
5469 kfree(nn->ownerstr_hashtbl);
5471 kfree(nn->unconf_id_hashtbl);
5473 kfree(nn->conf_id_hashtbl);
5479 nfs4_state_destroy_net(struct net *net)
5482 struct nfs4_client *clp = NULL;
5483 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5485 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5486 while (!list_empty(&nn->conf_id_hashtbl[i])) {
5487 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5488 destroy_client(clp);
5492 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5493 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
5494 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5495 destroy_client(clp);
5499 kfree(nn->sessionid_hashtbl);
5500 kfree(nn->ownerstr_hashtbl);
5501 kfree(nn->unconf_id_hashtbl);
5502 kfree(nn->conf_id_hashtbl);
5507 nfs4_state_start_net(struct net *net)
5509 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5512 ret = nfs4_state_create_net(net);
5515 nfsd4_client_tracking_init(net);
5516 nn->boot_time = get_seconds();
5517 locks_start_grace(net, &nn->nfsd4_manager);
5518 nn->grace_ended = false;
5519 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
5520 nn->nfsd4_grace, net);
5521 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
5525 /* initialization to perform when the nfsd service is started: */
5528 nfs4_state_start(void)
5532 ret = set_callback_cred();
5535 laundry_wq = create_singlethread_workqueue("nfsd4");
5536 if (laundry_wq == NULL) {
5540 ret = nfsd4_create_callback_queue();
5542 goto out_free_laundry;
5544 set_max_delegations();
5549 destroy_workqueue(laundry_wq);
5555 nfs4_state_shutdown_net(struct net *net)
5557 struct nfs4_delegation *dp = NULL;
5558 struct list_head *pos, *next, reaplist;
5559 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5561 cancel_delayed_work_sync(&nn->laundromat_work);
5562 locks_end_grace(&nn->nfsd4_manager);
5565 INIT_LIST_HEAD(&reaplist);
5566 spin_lock(&state_lock);
5567 list_for_each_safe(pos, next, &nn->del_recall_lru) {
5568 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
5569 list_move(&dp->dl_recall_lru, &reaplist);
5571 spin_unlock(&state_lock);
5572 list_for_each_safe(pos, next, &reaplist) {
5573 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
5574 destroy_delegation(dp);
5577 nfsd4_client_tracking_exit(net);
5578 nfs4_state_destroy_net(net);
5579 nfs4_unlock_state();
5583 nfs4_state_shutdown(void)
5585 destroy_workqueue(laundry_wq);
5586 nfsd4_destroy_callback_queue();
5590 get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5592 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
5593 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
5597 put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5599 if (cstate->minorversion) {
5600 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
5601 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
5606 clear_current_stateid(struct nfsd4_compound_state *cstate)
5608 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
5612 * functions to set current state id
5615 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5617 put_stateid(cstate, &odp->od_stateid);
5621 nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
5623 put_stateid(cstate, &open->op_stateid);
5627 nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5629 put_stateid(cstate, &close->cl_stateid);
5633 nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
5635 put_stateid(cstate, &lock->lk_resp_stateid);
5639 * functions to consume current state id
5643 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5645 get_stateid(cstate, &odp->od_stateid);
5649 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
5651 get_stateid(cstate, &drp->dr_stateid);
5655 nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
5657 get_stateid(cstate, &fsp->fr_stateid);
5661 nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
5663 get_stateid(cstate, &setattr->sa_stateid);
5667 nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5669 get_stateid(cstate, &close->cl_stateid);
5673 nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
5675 get_stateid(cstate, &locku->lu_stateid);
5679 nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
5681 get_stateid(cstate, &read->rd_stateid);
5685 nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
5687 get_stateid(cstate, &write->wr_stateid);