2 * Copyright (c) 2001 The Regents of the University of Michigan.
5 * Kendrick Smith <kmsmith@umich.edu>
6 * Andy Adamson <kandros@umich.edu>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <linux/file.h>
37 #include <linux/slab.h>
38 #include <linux/namei.h>
39 #include <linux/swap.h>
40 #include <linux/pagemap.h>
41 #include <linux/ratelimit.h>
42 #include <linux/sunrpc/svcauth_gss.h>
43 #include <linux/sunrpc/addr.h>
44 #include <linux/hash.h>
48 #include "current_stateid.h"
52 #define NFSDDBG_FACILITY NFSDDBG_PROC
54 #define all_ones {{~0,~0},~0}
55 static const stateid_t one_stateid = {
57 .si_opaque = all_ones,
59 static const stateid_t zero_stateid = {
62 static const stateid_t currentstateid = {
66 static u64 current_sessionid = 1;
68 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
69 #define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
70 #define CURRENT_STATEID(stateid) (!memcmp((stateid), ¤tstateid, sizeof(stateid_t)))
72 /* forward declarations */
73 static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
74 static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
78 /* Currently used for almost all code touching nfsv4 state: */
79 static DEFINE_MUTEX(client_mutex);
82 * Currently used for the del_recall_lru and file hash table. In an
83 * effort to decrease the scope of the client_mutex, this spinlock may
84 * eventually cover more:
86 static DEFINE_SPINLOCK(state_lock);
88 static struct kmem_cache *openowner_slab;
89 static struct kmem_cache *lockowner_slab;
90 static struct kmem_cache *file_slab;
91 static struct kmem_cache *stateid_slab;
92 static struct kmem_cache *deleg_slab;
97 mutex_lock(&client_mutex);
100 static void free_session(struct nfsd4_session *);
102 static bool is_session_dead(struct nfsd4_session *ses)
104 return ses->se_flags & NFS4_SESSION_DEAD;
107 static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
109 if (atomic_read(&ses->se_ref) > ref_held_by_me)
110 return nfserr_jukebox;
111 ses->se_flags |= NFS4_SESSION_DEAD;
116 nfs4_unlock_state(void)
118 mutex_unlock(&client_mutex);
121 static bool is_client_expired(struct nfs4_client *clp)
123 return clp->cl_time == 0;
126 static __be32 mark_client_expired_locked(struct nfs4_client *clp)
128 if (atomic_read(&clp->cl_refcount))
129 return nfserr_jukebox;
134 static __be32 mark_client_expired(struct nfs4_client *clp)
136 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
139 spin_lock(&nn->client_lock);
140 ret = mark_client_expired_locked(clp);
141 spin_unlock(&nn->client_lock);
145 static __be32 get_client_locked(struct nfs4_client *clp)
147 if (is_client_expired(clp))
148 return nfserr_expired;
149 atomic_inc(&clp->cl_refcount);
153 /* must be called under the client_lock */
155 renew_client_locked(struct nfs4_client *clp)
157 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
159 if (is_client_expired(clp)) {
161 printk("%s: client (clientid %08x/%08x) already expired\n",
163 clp->cl_clientid.cl_boot,
164 clp->cl_clientid.cl_id);
168 dprintk("renewing client (clientid %08x/%08x)\n",
169 clp->cl_clientid.cl_boot,
170 clp->cl_clientid.cl_id);
171 list_move_tail(&clp->cl_lru, &nn->client_lru);
172 clp->cl_time = get_seconds();
176 renew_client(struct nfs4_client *clp)
178 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
180 spin_lock(&nn->client_lock);
181 renew_client_locked(clp);
182 spin_unlock(&nn->client_lock);
185 static void put_client_renew_locked(struct nfs4_client *clp)
187 if (!atomic_dec_and_test(&clp->cl_refcount))
189 if (!is_client_expired(clp))
190 renew_client_locked(clp);
193 static void put_client_renew(struct nfs4_client *clp)
195 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
197 if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
199 if (!is_client_expired(clp))
200 renew_client_locked(clp);
201 spin_unlock(&nn->client_lock);
204 static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
208 if (is_session_dead(ses))
209 return nfserr_badsession;
210 status = get_client_locked(ses->se_client);
213 atomic_inc(&ses->se_ref);
217 static void nfsd4_put_session_locked(struct nfsd4_session *ses)
219 struct nfs4_client *clp = ses->se_client;
221 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
223 put_client_renew_locked(clp);
226 static void nfsd4_put_session(struct nfsd4_session *ses)
228 struct nfs4_client *clp = ses->se_client;
229 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
231 spin_lock(&nn->client_lock);
232 nfsd4_put_session_locked(ses);
233 spin_unlock(&nn->client_lock);
238 opaque_hashval(const void *ptr, int nbytes)
240 unsigned char *cptr = (unsigned char *) ptr;
250 static void nfsd4_free_file(struct nfs4_file *f)
252 kmem_cache_free(file_slab, f);
256 put_nfs4_file(struct nfs4_file *fi)
258 might_lock(&state_lock);
260 if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) {
261 hlist_del(&fi->fi_hash);
262 spin_unlock(&state_lock);
268 get_nfs4_file(struct nfs4_file *fi)
270 atomic_inc(&fi->fi_ref);
274 __nfs4_get_fd(struct nfs4_file *f, int oflag)
276 if (f->fi_fds[oflag])
277 return get_file(f->fi_fds[oflag]);
282 find_writeable_file_locked(struct nfs4_file *f)
286 lockdep_assert_held(&f->fi_lock);
288 ret = __nfs4_get_fd(f, O_WRONLY);
290 ret = __nfs4_get_fd(f, O_RDWR);
295 find_writeable_file(struct nfs4_file *f)
299 spin_lock(&f->fi_lock);
300 ret = find_writeable_file_locked(f);
301 spin_unlock(&f->fi_lock);
306 static struct file *find_readable_file_locked(struct nfs4_file *f)
310 lockdep_assert_held(&f->fi_lock);
312 ret = __nfs4_get_fd(f, O_RDONLY);
314 ret = __nfs4_get_fd(f, O_RDWR);
319 find_readable_file(struct nfs4_file *f)
323 spin_lock(&f->fi_lock);
324 ret = find_readable_file_locked(f);
325 spin_unlock(&f->fi_lock);
331 find_any_file(struct nfs4_file *f)
335 spin_lock(&f->fi_lock);
336 ret = __nfs4_get_fd(f, O_RDWR);
338 ret = __nfs4_get_fd(f, O_WRONLY);
340 ret = __nfs4_get_fd(f, O_RDONLY);
342 spin_unlock(&f->fi_lock);
346 static atomic_long_t num_delegations;
347 unsigned long max_delegations;
350 * Open owner state (share locks)
353 /* hash tables for lock and open owners */
354 #define OWNER_HASH_BITS 8
355 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
356 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
358 static unsigned int ownerstr_hashval(u32 clientid, struct xdr_netobj *ownername)
362 ret = opaque_hashval(ownername->data, ownername->len);
364 return ret & OWNER_HASH_MASK;
367 /* hash table for nfs4_file */
368 #define FILE_HASH_BITS 8
369 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
371 static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
373 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
376 static unsigned int file_hashval(struct knfsd_fh *fh)
378 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
381 static bool nfsd_fh_match(struct knfsd_fh *fh1, struct knfsd_fh *fh2)
383 return fh1->fh_size == fh2->fh_size &&
384 !memcmp(fh1->fh_base.fh_pad,
389 static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
392 __nfs4_file_get_access(struct nfs4_file *fp, u32 access)
394 lockdep_assert_held(&fp->fi_lock);
396 if (access & NFS4_SHARE_ACCESS_WRITE)
397 atomic_inc(&fp->fi_access[O_WRONLY]);
398 if (access & NFS4_SHARE_ACCESS_READ)
399 atomic_inc(&fp->fi_access[O_RDONLY]);
403 nfs4_file_get_access(struct nfs4_file *fp, u32 access)
405 lockdep_assert_held(&fp->fi_lock);
407 /* Does this access mode make sense? */
408 if (access & ~NFS4_SHARE_ACCESS_BOTH)
411 /* Does it conflict with a deny mode already set? */
412 if ((access & fp->fi_share_deny) != 0)
413 return nfserr_share_denied;
415 __nfs4_file_get_access(fp, access);
419 static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
421 /* Common case is that there is no deny mode. */
423 /* Does this deny mode make sense? */
424 if (deny & ~NFS4_SHARE_DENY_BOTH)
427 if ((deny & NFS4_SHARE_DENY_READ) &&
428 atomic_read(&fp->fi_access[O_RDONLY]))
429 return nfserr_share_denied;
431 if ((deny & NFS4_SHARE_DENY_WRITE) &&
432 atomic_read(&fp->fi_access[O_WRONLY]))
433 return nfserr_share_denied;
438 static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
440 might_lock(&fp->fi_lock);
442 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
443 struct file *f1 = NULL;
444 struct file *f2 = NULL;
446 swap(f1, fp->fi_fds[oflag]);
447 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
448 swap(f2, fp->fi_fds[O_RDWR]);
449 spin_unlock(&fp->fi_lock);
457 static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
459 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
461 if (access & NFS4_SHARE_ACCESS_WRITE)
462 __nfs4_file_put_access(fp, O_WRONLY);
463 if (access & NFS4_SHARE_ACCESS_READ)
464 __nfs4_file_put_access(fp, O_RDONLY);
467 static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl,
468 struct kmem_cache *slab)
470 struct idr *stateids = &cl->cl_stateids;
471 struct nfs4_stid *stid;
474 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
478 new_id = idr_alloc_cyclic(stateids, stid, 0, 0, GFP_KERNEL);
481 stid->sc_client = cl;
482 stid->sc_stateid.si_opaque.so_id = new_id;
483 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
484 /* Will be incremented before return to client: */
485 atomic_set(&stid->sc_count, 1);
488 * It shouldn't be a problem to reuse an opaque stateid value.
489 * I don't think it is for 4.1. But with 4.0 I worry that, for
490 * example, a stray write retransmission could be accepted by
491 * the server when it should have been rejected. Therefore,
492 * adopt a trick from the sctp code to attempt to maximize the
493 * amount of time until an id is reused, by ensuring they always
494 * "increase" (mod INT_MAX):
498 kmem_cache_free(slab, stid);
502 static struct nfs4_ol_stateid * nfs4_alloc_stateid(struct nfs4_client *clp)
504 struct nfs4_stid *stid;
505 struct nfs4_ol_stateid *stp;
507 stid = nfs4_alloc_stid(clp, stateid_slab);
511 stp = openlockstateid(stid);
512 stp->st_stid.sc_free = nfs4_free_ol_stateid;
516 static void nfs4_free_deleg(struct nfs4_stid *stid)
518 kmem_cache_free(deleg_slab, stid);
519 atomic_long_dec(&num_delegations);
523 * When we recall a delegation, we should be careful not to hand it
524 * out again straight away.
525 * To ensure this we keep a pair of bloom filters ('new' and 'old')
526 * in which the filehandles of recalled delegations are "stored".
527 * If a filehandle appear in either filter, a delegation is blocked.
528 * When a delegation is recalled, the filehandle is stored in the "new"
530 * Every 30 seconds we swap the filters and clear the "new" one,
531 * unless both are empty of course.
533 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
534 * low 3 bytes as hash-table indices.
536 * 'blocked_delegations_lock', which is always taken in block_delegations(),
537 * is used to manage concurrent access. Testing does not need the lock
538 * except when swapping the two filters.
540 static DEFINE_SPINLOCK(blocked_delegations_lock);
541 static struct bloom_pair {
542 int entries, old_entries;
544 int new; /* index into 'set' */
545 DECLARE_BITMAP(set[2], 256);
546 } blocked_delegations;
548 static int delegation_blocked(struct knfsd_fh *fh)
551 struct bloom_pair *bd = &blocked_delegations;
553 if (bd->entries == 0)
555 if (seconds_since_boot() - bd->swap_time > 30) {
556 spin_lock(&blocked_delegations_lock);
557 if (seconds_since_boot() - bd->swap_time > 30) {
558 bd->entries -= bd->old_entries;
559 bd->old_entries = bd->entries;
560 memset(bd->set[bd->new], 0,
563 bd->swap_time = seconds_since_boot();
565 spin_unlock(&blocked_delegations_lock);
567 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
568 if (test_bit(hash&255, bd->set[0]) &&
569 test_bit((hash>>8)&255, bd->set[0]) &&
570 test_bit((hash>>16)&255, bd->set[0]))
573 if (test_bit(hash&255, bd->set[1]) &&
574 test_bit((hash>>8)&255, bd->set[1]) &&
575 test_bit((hash>>16)&255, bd->set[1]))
581 static void block_delegations(struct knfsd_fh *fh)
584 struct bloom_pair *bd = &blocked_delegations;
586 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
588 spin_lock(&blocked_delegations_lock);
589 __set_bit(hash&255, bd->set[bd->new]);
590 __set_bit((hash>>8)&255, bd->set[bd->new]);
591 __set_bit((hash>>16)&255, bd->set[bd->new]);
592 if (bd->entries == 0)
593 bd->swap_time = seconds_since_boot();
595 spin_unlock(&blocked_delegations_lock);
598 static struct nfs4_delegation *
599 alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh)
601 struct nfs4_delegation *dp;
604 dprintk("NFSD alloc_init_deleg\n");
605 n = atomic_long_inc_return(&num_delegations);
606 if (n < 0 || n > max_delegations)
608 if (delegation_blocked(¤t_fh->fh_handle))
610 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
614 dp->dl_stid.sc_free = nfs4_free_deleg;
616 * delegation seqid's are never incremented. The 4.1 special
617 * meaning of seqid 0 isn't meaningful, really, but let's avoid
618 * 0 anyway just for consistency and use 1:
620 dp->dl_stid.sc_stateid.si_generation = 1;
621 INIT_LIST_HEAD(&dp->dl_perfile);
622 INIT_LIST_HEAD(&dp->dl_perclnt);
623 INIT_LIST_HEAD(&dp->dl_recall_lru);
624 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
625 INIT_WORK(&dp->dl_recall.cb_work, nfsd4_run_cb_recall);
628 atomic_long_dec(&num_delegations);
633 nfs4_put_stid(struct nfs4_stid *s)
635 struct nfs4_file *fp = s->sc_file;
636 struct nfs4_client *clp = s->sc_client;
638 if (!atomic_dec_and_test(&s->sc_count))
640 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
646 static void nfs4_put_deleg_lease(struct nfs4_file *fp)
648 lockdep_assert_held(&state_lock);
652 if (atomic_dec_and_test(&fp->fi_delegees)) {
653 vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
655 fput(fp->fi_deleg_file);
656 fp->fi_deleg_file = NULL;
660 static void unhash_stid(struct nfs4_stid *s)
666 hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
668 lockdep_assert_held(&state_lock);
669 lockdep_assert_held(&fp->fi_lock);
671 dp->dl_stid.sc_type = NFS4_DELEG_STID;
672 list_add(&dp->dl_perfile, &fp->fi_delegations);
673 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
677 unhash_delegation_locked(struct nfs4_delegation *dp)
679 struct nfs4_file *fp = dp->dl_stid.sc_file;
681 lockdep_assert_held(&state_lock);
683 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
684 /* Ensure that deleg break won't try to requeue it */
686 spin_lock(&fp->fi_lock);
687 list_del_init(&dp->dl_perclnt);
688 list_del_init(&dp->dl_recall_lru);
689 list_del_init(&dp->dl_perfile);
690 spin_unlock(&fp->fi_lock);
692 nfs4_put_deleg_lease(fp);
695 static void destroy_delegation(struct nfs4_delegation *dp)
697 spin_lock(&state_lock);
698 unhash_delegation_locked(dp);
699 spin_unlock(&state_lock);
700 nfs4_put_stid(&dp->dl_stid);
703 static void revoke_delegation(struct nfs4_delegation *dp)
705 struct nfs4_client *clp = dp->dl_stid.sc_client;
707 WARN_ON(!list_empty(&dp->dl_recall_lru));
709 if (clp->cl_minorversion == 0)
710 nfs4_put_stid(&dp->dl_stid);
712 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
713 spin_lock(&clp->cl_lock);
714 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
715 spin_unlock(&clp->cl_lock);
723 static unsigned int clientid_hashval(u32 id)
725 return id & CLIENT_HASH_MASK;
728 static unsigned int clientstr_hashval(const char *name)
730 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
734 * We store the NONE, READ, WRITE, and BOTH bits separately in the
735 * st_{access,deny}_bmap field of the stateid, in order to track not
736 * only what share bits are currently in force, but also what
737 * combinations of share bits previous opens have used. This allows us
738 * to enforce the recommendation of rfc 3530 14.2.19 that the server
739 * return an error if the client attempt to downgrade to a combination
740 * of share bits not explicable by closing some of its previous opens.
742 * XXX: This enforcement is actually incomplete, since we don't keep
743 * track of access/deny bit combinations; so, e.g., we allow:
745 * OPEN allow read, deny write
746 * OPEN allow both, deny none
747 * DOWNGRADE allow read, deny none
749 * which we should reject.
752 bmap_to_share_mode(unsigned long bmap) {
754 unsigned int access = 0;
756 for (i = 1; i < 4; i++) {
757 if (test_bit(i, &bmap))
763 /* set share access for a given stateid */
765 set_access(u32 access, struct nfs4_ol_stateid *stp)
767 unsigned char mask = 1 << access;
769 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
770 stp->st_access_bmap |= mask;
773 /* clear share access for a given stateid */
775 clear_access(u32 access, struct nfs4_ol_stateid *stp)
777 unsigned char mask = 1 << access;
779 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
780 stp->st_access_bmap &= ~mask;
783 /* test whether a given stateid has access */
785 test_access(u32 access, struct nfs4_ol_stateid *stp)
787 unsigned char mask = 1 << access;
789 return (bool)(stp->st_access_bmap & mask);
792 /* set share deny for a given stateid */
794 set_deny(u32 deny, struct nfs4_ol_stateid *stp)
796 unsigned char mask = 1 << deny;
798 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
799 stp->st_deny_bmap |= mask;
802 /* clear share deny for a given stateid */
804 clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
806 unsigned char mask = 1 << deny;
808 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
809 stp->st_deny_bmap &= ~mask;
812 /* test whether a given stateid is denying specific access */
814 test_deny(u32 deny, struct nfs4_ol_stateid *stp)
816 unsigned char mask = 1 << deny;
818 return (bool)(stp->st_deny_bmap & mask);
821 static int nfs4_access_to_omode(u32 access)
823 switch (access & NFS4_SHARE_ACCESS_BOTH) {
824 case NFS4_SHARE_ACCESS_READ:
826 case NFS4_SHARE_ACCESS_WRITE:
828 case NFS4_SHARE_ACCESS_BOTH:
836 * A stateid that had a deny mode associated with it is being released
837 * or downgraded. Recalculate the deny mode on the file.
840 recalculate_deny_mode(struct nfs4_file *fp)
842 struct nfs4_ol_stateid *stp;
844 spin_lock(&fp->fi_lock);
845 fp->fi_share_deny = 0;
846 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
847 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
848 spin_unlock(&fp->fi_lock);
852 reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
857 for (i = 1; i < 4; i++) {
858 if ((i & deny) != i) {
864 /* Recalculate per-file deny mode if there was a change */
866 recalculate_deny_mode(stp->st_stid.sc_file);
869 /* release all access and file references for a given stateid */
871 release_all_access(struct nfs4_ol_stateid *stp)
874 struct nfs4_file *fp = stp->st_stid.sc_file;
876 if (fp && stp->st_deny_bmap != 0)
877 recalculate_deny_mode(fp);
879 for (i = 1; i < 4; i++) {
880 if (test_access(i, stp))
881 nfs4_file_put_access(stp->st_stid.sc_file, i);
882 clear_access(i, stp);
886 static void unhash_generic_stateid(struct nfs4_ol_stateid *stp)
888 struct nfs4_file *fp = stp->st_stid.sc_file;
890 spin_lock(&fp->fi_lock);
891 list_del(&stp->st_perfile);
892 spin_unlock(&fp->fi_lock);
893 list_del(&stp->st_perstateowner);
896 static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
898 struct nfs4_ol_stateid *stp = openlockstateid(stid);
900 release_all_access(stp);
901 kmem_cache_free(stateid_slab, stid);
904 static void __release_lock_stateid(struct nfs4_ol_stateid *stp)
908 list_del(&stp->st_locks);
909 unhash_generic_stateid(stp);
910 unhash_stid(&stp->st_stid);
911 file = find_any_file(stp->st_stid.sc_file);
913 filp_close(file, (fl_owner_t)lockowner(stp->st_stateowner));
914 nfs4_put_stid(&stp->st_stid);
917 static void unhash_lockowner(struct nfs4_lockowner *lo)
919 struct nfs4_ol_stateid *stp;
921 list_del(&lo->lo_owner.so_strhash);
922 while (!list_empty(&lo->lo_owner.so_stateids)) {
923 stp = list_first_entry(&lo->lo_owner.so_stateids,
924 struct nfs4_ol_stateid, st_perstateowner);
925 __release_lock_stateid(stp);
929 static void nfs4_free_lockowner(struct nfs4_lockowner *lo)
931 kfree(lo->lo_owner.so_owner.data);
932 kmem_cache_free(lockowner_slab, lo);
935 static void release_lockowner(struct nfs4_lockowner *lo)
937 unhash_lockowner(lo);
938 nfs4_free_lockowner(lo);
941 static void release_lockowner_if_empty(struct nfs4_lockowner *lo)
943 if (list_empty(&lo->lo_owner.so_stateids))
944 release_lockowner(lo);
947 static void release_lock_stateid(struct nfs4_ol_stateid *stp)
949 struct nfs4_lockowner *lo;
951 lo = lockowner(stp->st_stateowner);
952 __release_lock_stateid(stp);
953 release_lockowner_if_empty(lo);
956 static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp)
958 struct nfs4_ol_stateid *stp;
960 while (!list_empty(&open_stp->st_locks)) {
961 stp = list_entry(open_stp->st_locks.next,
962 struct nfs4_ol_stateid, st_locks);
963 release_lock_stateid(stp);
967 static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
969 unhash_generic_stateid(stp);
970 release_open_stateid_locks(stp);
973 static void release_open_stateid(struct nfs4_ol_stateid *stp)
975 unhash_open_stateid(stp);
976 nfs4_put_stid(&stp->st_stid);
979 static void unhash_openowner(struct nfs4_openowner *oo)
981 struct nfs4_ol_stateid *stp;
983 list_del(&oo->oo_owner.so_strhash);
984 list_del(&oo->oo_perclient);
985 while (!list_empty(&oo->oo_owner.so_stateids)) {
986 stp = list_first_entry(&oo->oo_owner.so_stateids,
987 struct nfs4_ol_stateid, st_perstateowner);
988 release_open_stateid(stp);
992 static void release_last_closed_stateid(struct nfs4_openowner *oo)
994 struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
997 nfs4_put_stid(&s->st_stid);
998 oo->oo_last_closed_stid = NULL;
1002 static void nfs4_free_openowner(struct nfs4_openowner *oo)
1004 kfree(oo->oo_owner.so_owner.data);
1005 kmem_cache_free(openowner_slab, oo);
1008 static void release_openowner(struct nfs4_openowner *oo)
1010 unhash_openowner(oo);
1011 list_del(&oo->oo_close_lru);
1012 release_last_closed_stateid(oo);
1013 nfs4_free_openowner(oo);
1017 hash_sessionid(struct nfs4_sessionid *sessionid)
1019 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1021 return sid->sequence % SESSION_HASH_SIZE;
1026 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1028 u32 *ptr = (u32 *)(&sessionid->data[0]);
1029 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1033 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1039 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1040 * won't be used for replay.
1042 void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1044 struct nfs4_stateowner *so = cstate->replay_owner;
1046 if (nfserr == nfserr_replay_me)
1049 if (!seqid_mutating_err(ntohl(nfserr))) {
1050 cstate->replay_owner = NULL;
1055 if (so->so_is_open_owner)
1056 release_last_closed_stateid(openowner(so));
1062 gen_sessionid(struct nfsd4_session *ses)
1064 struct nfs4_client *clp = ses->se_client;
1065 struct nfsd4_sessionid *sid;
1067 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1068 sid->clientid = clp->cl_clientid;
1069 sid->sequence = current_sessionid++;
1074 * The protocol defines ca_maxresponssize_cached to include the size of
1075 * the rpc header, but all we need to cache is the data starting after
1076 * the end of the initial SEQUENCE operation--the rest we regenerate
1077 * each time. Therefore we can advertise a ca_maxresponssize_cached
1078 * value that is the number of bytes in our cache plus a few additional
1079 * bytes. In order to stay on the safe side, and not promise more than
1080 * we can cache, those additional bytes must be the minimum possible: 24
1081 * bytes of rpc header (xid through accept state, with AUTH_NULL
1082 * verifier), 12 for the compound header (with zero-length tag), and 44
1083 * for the SEQUENCE op response:
1085 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1088 free_session_slots(struct nfsd4_session *ses)
1092 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1093 kfree(ses->se_slots[i]);
1097 * We don't actually need to cache the rpc and session headers, so we
1098 * can allocate a little less for each slot:
1100 static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
1104 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1107 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1108 return size + sizeof(struct nfsd4_slot);
1112 * XXX: If we run out of reserved DRC memory we could (up to a point)
1113 * re-negotiate active sessions and reduce their slot usage to make
1114 * room for new connections. For now we just fail the create session.
1116 static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
1118 u32 slotsize = slot_bytes(ca);
1119 u32 num = ca->maxreqs;
1122 spin_lock(&nfsd_drc_lock);
1123 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1124 nfsd_drc_max_mem - nfsd_drc_mem_used);
1125 num = min_t(int, num, avail / slotsize);
1126 nfsd_drc_mem_used += num * slotsize;
1127 spin_unlock(&nfsd_drc_lock);
1132 static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
1134 int slotsize = slot_bytes(ca);
1136 spin_lock(&nfsd_drc_lock);
1137 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
1138 spin_unlock(&nfsd_drc_lock);
1141 static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1142 struct nfsd4_channel_attrs *battrs)
1144 int numslots = fattrs->maxreqs;
1145 int slotsize = slot_bytes(fattrs);
1146 struct nfsd4_session *new;
1149 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
1150 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1151 mem = numslots * sizeof(struct nfsd4_slot *);
1153 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
1156 /* allocate each struct nfsd4_slot and data cache in one piece */
1157 for (i = 0; i < numslots; i++) {
1158 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
1159 if (!new->se_slots[i])
1163 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1164 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1169 kfree(new->se_slots[i]);
1174 static void free_conn(struct nfsd4_conn *c)
1176 svc_xprt_put(c->cn_xprt);
1180 static void nfsd4_conn_lost(struct svc_xpt_user *u)
1182 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1183 struct nfs4_client *clp = c->cn_session->se_client;
1185 spin_lock(&clp->cl_lock);
1186 if (!list_empty(&c->cn_persession)) {
1187 list_del(&c->cn_persession);
1190 nfsd4_probe_callback(clp);
1191 spin_unlock(&clp->cl_lock);
1194 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
1196 struct nfsd4_conn *conn;
1198 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1201 svc_xprt_get(rqstp->rq_xprt);
1202 conn->cn_xprt = rqstp->rq_xprt;
1203 conn->cn_flags = flags;
1204 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1208 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1210 conn->cn_session = ses;
1211 list_add(&conn->cn_persession, &ses->se_conns);
1214 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1216 struct nfs4_client *clp = ses->se_client;
1218 spin_lock(&clp->cl_lock);
1219 __nfsd4_hash_conn(conn, ses);
1220 spin_unlock(&clp->cl_lock);
1223 static int nfsd4_register_conn(struct nfsd4_conn *conn)
1225 conn->cn_xpt_user.callback = nfsd4_conn_lost;
1226 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
1229 static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
1233 nfsd4_hash_conn(conn, ses);
1234 ret = nfsd4_register_conn(conn);
1236 /* oops; xprt is already down: */
1237 nfsd4_conn_lost(&conn->cn_xpt_user);
1238 /* We may have gained or lost a callback channel: */
1239 nfsd4_probe_callback_sync(ses->se_client);
1242 static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
1244 u32 dir = NFS4_CDFC4_FORE;
1246 if (cses->flags & SESSION4_BACK_CHAN)
1247 dir |= NFS4_CDFC4_BACK;
1248 return alloc_conn(rqstp, dir);
1251 /* must be called under client_lock */
1252 static void nfsd4_del_conns(struct nfsd4_session *s)
1254 struct nfs4_client *clp = s->se_client;
1255 struct nfsd4_conn *c;
1257 spin_lock(&clp->cl_lock);
1258 while (!list_empty(&s->se_conns)) {
1259 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1260 list_del_init(&c->cn_persession);
1261 spin_unlock(&clp->cl_lock);
1263 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1266 spin_lock(&clp->cl_lock);
1268 spin_unlock(&clp->cl_lock);
1271 static void __free_session(struct nfsd4_session *ses)
1273 free_session_slots(ses);
1277 static void free_session(struct nfsd4_session *ses)
1279 struct nfsd_net *nn = net_generic(ses->se_client->net, nfsd_net_id);
1281 lockdep_assert_held(&nn->client_lock);
1282 nfsd4_del_conns(ses);
1283 nfsd4_put_drc_mem(&ses->se_fchannel);
1284 __free_session(ses);
1287 static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
1290 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1292 new->se_client = clp;
1295 INIT_LIST_HEAD(&new->se_conns);
1297 new->se_cb_seq_nr = 1;
1298 new->se_flags = cses->flags;
1299 new->se_cb_prog = cses->callback_prog;
1300 new->se_cb_sec = cses->cb_sec;
1301 atomic_set(&new->se_ref, 0);
1302 idx = hash_sessionid(&new->se_sessionid);
1303 spin_lock(&nn->client_lock);
1304 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
1305 spin_lock(&clp->cl_lock);
1306 list_add(&new->se_perclnt, &clp->cl_sessions);
1307 spin_unlock(&clp->cl_lock);
1308 spin_unlock(&nn->client_lock);
1310 if (cses->flags & SESSION4_BACK_CHAN) {
1311 struct sockaddr *sa = svc_addr(rqstp);
1313 * This is a little silly; with sessions there's no real
1314 * use for the callback address. Use the peer address
1315 * as a reasonable default for now, but consider fixing
1316 * the rpc client not to require an address in the
1319 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1320 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
1324 /* caller must hold client_lock */
1325 static struct nfsd4_session *
1326 __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
1328 struct nfsd4_session *elem;
1330 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1332 dump_sessionid(__func__, sessionid);
1333 idx = hash_sessionid(sessionid);
1334 /* Search in the appropriate list */
1335 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
1336 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1337 NFS4_MAX_SESSIONID_LEN)) {
1342 dprintk("%s: session not found\n", __func__);
1346 static struct nfsd4_session *
1347 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1350 struct nfsd4_session *session;
1351 __be32 status = nfserr_badsession;
1353 session = __find_in_sessionid_hashtbl(sessionid, net);
1356 status = nfsd4_get_session_locked(session);
1364 /* caller must hold client_lock */
1366 unhash_session(struct nfsd4_session *ses)
1368 list_del(&ses->se_hash);
1369 spin_lock(&ses->se_client->cl_lock);
1370 list_del(&ses->se_perclnt);
1371 spin_unlock(&ses->se_client->cl_lock);
1374 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1376 STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
1378 if (clid->cl_boot == nn->boot_time)
1380 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
1381 clid->cl_boot, clid->cl_id, nn->boot_time);
1386 * XXX Should we use a slab cache ?
1387 * This type of memory management is somewhat inefficient, but we use it
1388 * anyway since SETCLIENTID is not a common operation.
1390 static struct nfs4_client *alloc_client(struct xdr_netobj name)
1392 struct nfs4_client *clp;
1394 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1397 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
1398 if (clp->cl_name.data == NULL) {
1402 clp->cl_name.len = name.len;
1403 INIT_LIST_HEAD(&clp->cl_sessions);
1404 idr_init(&clp->cl_stateids);
1405 atomic_set(&clp->cl_refcount, 0);
1406 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1407 INIT_LIST_HEAD(&clp->cl_idhash);
1408 INIT_LIST_HEAD(&clp->cl_openowners);
1409 INIT_LIST_HEAD(&clp->cl_delegations);
1410 INIT_LIST_HEAD(&clp->cl_lru);
1411 INIT_LIST_HEAD(&clp->cl_callbacks);
1412 INIT_LIST_HEAD(&clp->cl_revoked);
1413 spin_lock_init(&clp->cl_lock);
1414 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1419 free_client(struct nfs4_client *clp)
1421 struct nfsd_net __maybe_unused *nn = net_generic(clp->net, nfsd_net_id);
1423 lockdep_assert_held(&nn->client_lock);
1424 while (!list_empty(&clp->cl_sessions)) {
1425 struct nfsd4_session *ses;
1426 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1428 list_del(&ses->se_perclnt);
1429 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1432 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
1433 free_svc_cred(&clp->cl_cred);
1434 kfree(clp->cl_name.data);
1435 idr_destroy(&clp->cl_stateids);
1439 /* must be called under the client_lock */
1441 unhash_client_locked(struct nfs4_client *clp)
1443 struct nfsd4_session *ses;
1445 list_del(&clp->cl_lru);
1446 spin_lock(&clp->cl_lock);
1447 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1448 list_del_init(&ses->se_hash);
1449 spin_unlock(&clp->cl_lock);
1453 destroy_client(struct nfs4_client *clp)
1455 struct nfs4_openowner *oo;
1456 struct nfs4_delegation *dp;
1457 struct list_head reaplist;
1458 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1460 INIT_LIST_HEAD(&reaplist);
1461 spin_lock(&state_lock);
1462 while (!list_empty(&clp->cl_delegations)) {
1463 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
1464 unhash_delegation_locked(dp);
1465 list_add(&dp->dl_recall_lru, &reaplist);
1467 spin_unlock(&state_lock);
1468 while (!list_empty(&reaplist)) {
1469 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
1470 list_del_init(&dp->dl_recall_lru);
1471 nfs4_put_stid(&dp->dl_stid);
1473 while (!list_empty(&clp->cl_revoked)) {
1474 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
1475 list_del_init(&dp->dl_recall_lru);
1476 nfs4_put_stid(&dp->dl_stid);
1478 while (!list_empty(&clp->cl_openowners)) {
1479 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1480 release_openowner(oo);
1482 nfsd4_shutdown_callback(clp);
1483 if (clp->cl_cb_conn.cb_xprt)
1484 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
1485 list_del(&clp->cl_idhash);
1486 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1487 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1489 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1490 spin_lock(&nn->client_lock);
1491 unhash_client_locked(clp);
1492 WARN_ON_ONCE(atomic_read(&clp->cl_refcount));
1494 spin_unlock(&nn->client_lock);
1497 static void expire_client(struct nfs4_client *clp)
1499 nfsd4_client_record_remove(clp);
1500 destroy_client(clp);
1503 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1505 memcpy(target->cl_verifier.data, source->data,
1506 sizeof(target->cl_verifier.data));
1509 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1511 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1512 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1515 static int copy_cred(struct svc_cred *target, struct svc_cred *source)
1517 if (source->cr_principal) {
1518 target->cr_principal =
1519 kstrdup(source->cr_principal, GFP_KERNEL);
1520 if (target->cr_principal == NULL)
1523 target->cr_principal = NULL;
1524 target->cr_flavor = source->cr_flavor;
1525 target->cr_uid = source->cr_uid;
1526 target->cr_gid = source->cr_gid;
1527 target->cr_group_info = source->cr_group_info;
1528 get_group_info(target->cr_group_info);
1529 target->cr_gss_mech = source->cr_gss_mech;
1530 if (source->cr_gss_mech)
1531 gss_mech_get(source->cr_gss_mech);
1536 compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1540 res = o1->len - o2->len;
1543 return (long long)memcmp(o1->data, o2->data, o1->len);
1546 static int same_name(const char *n1, const char *n2)
1548 return 0 == memcmp(n1, n2, HEXDIR_LEN);
1552 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1554 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
1558 same_clid(clientid_t *cl1, clientid_t *cl2)
1560 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
1563 static bool groups_equal(struct group_info *g1, struct group_info *g2)
1567 if (g1->ngroups != g2->ngroups)
1569 for (i=0; i<g1->ngroups; i++)
1570 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
1576 * RFC 3530 language requires clid_inuse be returned when the
1577 * "principal" associated with a requests differs from that previously
1578 * used. We use uid, gid's, and gss principal string as our best
1579 * approximation. We also don't want to allow non-gss use of a client
1580 * established using gss: in theory cr_principal should catch that
1581 * change, but in practice cr_principal can be null even in the gss case
1582 * since gssd doesn't always pass down a principal string.
1584 static bool is_gss_cred(struct svc_cred *cr)
1586 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1587 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1592 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1594 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
1595 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1596 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
1597 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1599 if (cr1->cr_principal == cr2->cr_principal)
1601 if (!cr1->cr_principal || !cr2->cr_principal)
1603 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
1606 static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1608 struct svc_cred *cr = &rqstp->rq_cred;
1611 if (!cr->cr_gss_mech)
1613 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1614 return service == RPC_GSS_SVC_INTEGRITY ||
1615 service == RPC_GSS_SVC_PRIVACY;
1618 static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1620 struct svc_cred *cr = &rqstp->rq_cred;
1622 if (!cl->cl_mach_cred)
1624 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1626 if (!svc_rqst_integrity_protected(rqstp))
1628 if (!cr->cr_principal)
1630 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1633 static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
1635 static u32 current_clientid = 1;
1637 clp->cl_clientid.cl_boot = nn->boot_time;
1638 clp->cl_clientid.cl_id = current_clientid++;
1641 static void gen_confirm(struct nfs4_client *clp)
1647 * This is opaque to client, so no need to byte-swap. Use
1648 * __force to keep sparse happy
1650 verf[0] = (__force __be32)get_seconds();
1651 verf[1] = (__force __be32)i++;
1652 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
1655 static struct nfs4_stid *find_stateid(struct nfs4_client *cl, stateid_t *t)
1657 struct nfs4_stid *ret;
1659 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1660 if (!ret || !ret->sc_type)
1665 static struct nfs4_stid *find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
1667 struct nfs4_stid *s;
1669 s = find_stateid(cl, t);
1672 if (typemask & s->sc_type)
1677 static struct nfs4_client *create_client(struct xdr_netobj name,
1678 struct svc_rqst *rqstp, nfs4_verifier *verf)
1680 struct nfs4_client *clp;
1681 struct sockaddr *sa = svc_addr(rqstp);
1683 struct net *net = SVC_NET(rqstp);
1684 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1686 clp = alloc_client(name);
1690 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1692 spin_lock(&nn->client_lock);
1694 spin_unlock(&nn->client_lock);
1697 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_run_cb_null);
1698 clp->cl_time = get_seconds();
1699 clear_bit(0, &clp->cl_cb_slot_busy);
1700 copy_verf(clp, verf);
1701 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
1703 clp->cl_cb_session = NULL;
1709 add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1711 struct rb_node **new = &(root->rb_node), *parent = NULL;
1712 struct nfs4_client *clp;
1715 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1718 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1719 new = &((*new)->rb_left);
1721 new = &((*new)->rb_right);
1724 rb_link_node(&new_clp->cl_namenode, parent, new);
1725 rb_insert_color(&new_clp->cl_namenode, root);
1728 static struct nfs4_client *
1729 find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1732 struct rb_node *node = root->rb_node;
1733 struct nfs4_client *clp;
1736 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1737 cmp = compare_blob(&clp->cl_name, name);
1739 node = node->rb_left;
1741 node = node->rb_right;
1749 add_to_unconfirmed(struct nfs4_client *clp)
1751 unsigned int idhashval;
1752 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1754 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
1755 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
1756 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1757 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
1762 move_to_confirmed(struct nfs4_client *clp)
1764 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1765 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1767 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
1768 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
1769 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1770 add_clp_to_name_tree(clp, &nn->conf_name_tree);
1771 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
1775 static struct nfs4_client *
1776 find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
1778 struct nfs4_client *clp;
1779 unsigned int idhashval = clientid_hashval(clid->cl_id);
1781 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
1782 if (same_clid(&clp->cl_clientid, clid)) {
1783 if ((bool)clp->cl_minorversion != sessions)
1792 static struct nfs4_client *
1793 find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1795 struct list_head *tbl = nn->conf_id_hashtbl;
1797 return find_client_in_id_table(tbl, clid, sessions);
1800 static struct nfs4_client *
1801 find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1803 struct list_head *tbl = nn->unconf_id_hashtbl;
1805 return find_client_in_id_table(tbl, clid, sessions);
1808 static bool clp_used_exchangeid(struct nfs4_client *clp)
1810 return clp->cl_exchange_flags != 0;
1813 static struct nfs4_client *
1814 find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
1816 return find_clp_in_name_tree(name, &nn->conf_name_tree);
1819 static struct nfs4_client *
1820 find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
1822 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
1826 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
1828 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
1829 struct sockaddr *sa = svc_addr(rqstp);
1830 u32 scopeid = rpc_get_scope_id(sa);
1831 unsigned short expected_family;
1833 /* Currently, we only support tcp and tcp6 for the callback channel */
1834 if (se->se_callback_netid_len == 3 &&
1835 !memcmp(se->se_callback_netid_val, "tcp", 3))
1836 expected_family = AF_INET;
1837 else if (se->se_callback_netid_len == 4 &&
1838 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1839 expected_family = AF_INET6;
1843 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
1844 se->se_callback_addr_len,
1845 (struct sockaddr *)&conn->cb_addr,
1846 sizeof(conn->cb_addr));
1848 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
1851 if (conn->cb_addr.ss_family == AF_INET6)
1852 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
1854 conn->cb_prog = se->se_callback_prog;
1855 conn->cb_ident = se->se_callback_ident;
1856 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
1859 conn->cb_addr.ss_family = AF_UNSPEC;
1860 conn->cb_addrlen = 0;
1861 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
1862 "will not receive delegations\n",
1863 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1869 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
1872 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1874 struct xdr_buf *buf = resp->xdr.buf;
1875 struct nfsd4_slot *slot = resp->cstate.slot;
1878 dprintk("--> %s slot %p\n", __func__, slot);
1880 slot->sl_opcnt = resp->opcnt;
1881 slot->sl_status = resp->cstate.status;
1883 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
1884 if (nfsd4_not_cached(resp)) {
1885 slot->sl_datalen = 0;
1888 base = resp->cstate.data_offset;
1889 slot->sl_datalen = buf->len - base;
1890 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
1891 WARN("%s: sessions DRC could not cache compound\n", __func__);
1896 * Encode the replay sequence operation from the slot values.
1897 * If cachethis is FALSE encode the uncached rep error on the next
1898 * operation which sets resp->p and increments resp->opcnt for
1899 * nfs4svc_encode_compoundres.
1903 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
1904 struct nfsd4_compoundres *resp)
1906 struct nfsd4_op *op;
1907 struct nfsd4_slot *slot = resp->cstate.slot;
1909 /* Encode the replayed sequence operation */
1910 op = &args->ops[resp->opcnt - 1];
1911 nfsd4_encode_operation(resp, op);
1913 /* Return nfserr_retry_uncached_rep in next operation. */
1914 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
1915 op = &args->ops[resp->opcnt++];
1916 op->status = nfserr_retry_uncached_rep;
1917 nfsd4_encode_operation(resp, op);
1923 * The sequence operation is not cached because we can use the slot and
1927 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
1928 struct nfsd4_sequence *seq)
1930 struct nfsd4_slot *slot = resp->cstate.slot;
1931 struct xdr_stream *xdr = &resp->xdr;
1935 dprintk("--> %s slot %p\n", __func__, slot);
1937 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
1941 p = xdr_reserve_space(xdr, slot->sl_datalen);
1944 return nfserr_serverfault;
1946 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
1947 xdr_commit_encode(xdr);
1949 resp->opcnt = slot->sl_opcnt;
1950 return slot->sl_status;
1954 * Set the exchange_id flags returned by the server.
1957 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1959 /* pNFS is not supported */
1960 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
1962 /* Referrals are supported, Migration is not. */
1963 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
1965 /* set the wire flags to return to client. */
1966 clid->flags = new->cl_exchange_flags;
1969 static bool client_has_state(struct nfs4_client *clp)
1972 * Note clp->cl_openowners check isn't quite right: there's no
1973 * need to count owners without stateid's.
1975 * Also note we should probably be using this in 4.0 case too.
1977 return !list_empty(&clp->cl_openowners)
1978 || !list_empty(&clp->cl_delegations)
1979 || !list_empty(&clp->cl_sessions);
1983 nfsd4_exchange_id(struct svc_rqst *rqstp,
1984 struct nfsd4_compound_state *cstate,
1985 struct nfsd4_exchange_id *exid)
1987 struct nfs4_client *unconf, *conf, *new;
1989 char addr_str[INET6_ADDRSTRLEN];
1990 nfs4_verifier verf = exid->verifier;
1991 struct sockaddr *sa = svc_addr(rqstp);
1992 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
1993 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1995 rpc_ntop(sa, addr_str, sizeof(addr_str));
1996 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
1997 "ip_addr=%s flags %x, spa_how %d\n",
1998 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
1999 addr_str, exid->flags, exid->spa_how);
2001 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
2002 return nfserr_inval;
2004 switch (exid->spa_how) {
2006 if (!svc_rqst_integrity_protected(rqstp))
2007 return nfserr_inval;
2010 default: /* checked by xdr code */
2013 return nfserr_encr_alg_unsupp;
2016 /* Cases below refer to rfc 5661 section 18.35.4: */
2018 conf = find_confirmed_client_by_name(&exid->clname, nn);
2020 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2021 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2024 if (!clp_used_exchangeid(conf)) { /* buggy client */
2025 status = nfserr_inval;
2028 if (!mach_creds_match(conf, rqstp)) {
2029 status = nfserr_wrong_cred;
2032 if (!creds_match) { /* case 9 */
2033 status = nfserr_perm;
2036 if (!verfs_match) { /* case 8 */
2037 status = nfserr_not_same;
2041 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
2045 if (!creds_match) { /* case 3 */
2046 if (client_has_state(conf)) {
2047 status = nfserr_clid_inuse;
2050 expire_client(conf);
2053 if (verfs_match) { /* case 2 */
2054 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
2058 /* case 5, client reboot */
2062 if (update) { /* case 7 */
2063 status = nfserr_noent;
2067 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
2068 if (unconf) /* case 4, possible retry or client restart */
2069 expire_client(unconf);
2071 /* case 1 (normal case) */
2073 new = create_client(exid->clname, rqstp, &verf);
2075 status = nfserr_jukebox;
2078 new->cl_minorversion = cstate->minorversion;
2079 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
2082 add_to_unconfirmed(new);
2084 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
2085 exid->clientid.cl_id = new->cl_clientid.cl_id;
2087 exid->seqid = new->cl_cs_slot.sl_seqid + 1;
2088 nfsd4_set_ex_flags(new, exid);
2090 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
2091 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
2095 nfs4_unlock_state();
2100 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
2102 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2105 /* The slot is in use, and no response has been sent. */
2107 if (seqid == slot_seqid)
2108 return nfserr_jukebox;
2110 return nfserr_seq_misordered;
2112 /* Note unsigned 32-bit arithmetic handles wraparound: */
2113 if (likely(seqid == slot_seqid + 1))
2115 if (seqid == slot_seqid)
2116 return nfserr_replay_cache;
2117 return nfserr_seq_misordered;
2121 * Cache the create session result into the create session single DRC
2122 * slot cache by saving the xdr structure. sl_seqid has been set.
2123 * Do this for solo or embedded create session operations.
2126 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
2127 struct nfsd4_clid_slot *slot, __be32 nfserr)
2129 slot->sl_status = nfserr;
2130 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2134 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2135 struct nfsd4_clid_slot *slot)
2137 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2138 return slot->sl_status;
2141 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2142 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2143 1 + /* MIN tag is length with zero, only length */ \
2144 3 + /* version, opcount, opcode */ \
2145 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2146 /* seqid, slotID, slotID, cache */ \
2147 4 ) * sizeof(__be32))
2149 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2150 2 + /* verifier: AUTH_NULL, length 0 */\
2152 1 + /* MIN tag is length with zero, only length */ \
2153 3 + /* opcount, opcode, opstatus*/ \
2154 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2155 /* seqid, slotID, slotID, slotID, status */ \
2156 5 ) * sizeof(__be32))
2158 static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
2160 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2162 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2163 return nfserr_toosmall;
2164 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2165 return nfserr_toosmall;
2166 ca->headerpadsz = 0;
2167 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2168 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2169 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2170 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2171 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2172 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2174 * Note decreasing slot size below client's request may make it
2175 * difficult for client to function correctly, whereas
2176 * decreasing the number of slots will (just?) affect
2177 * performance. When short on memory we therefore prefer to
2178 * decrease number of slots instead of their size. Clients that
2179 * request larger slots than they need will get poor results:
2181 ca->maxreqs = nfsd4_get_drc_mem(ca);
2183 return nfserr_jukebox;
2188 #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2189 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2190 #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2191 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2193 static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2195 ca->headerpadsz = 0;
2198 * These RPC_MAX_HEADER macros are overkill, especially since we
2199 * don't even do gss on the backchannel yet. But this is still
2200 * less than 1k. Tighten up this estimate in the unlikely event
2201 * it turns out to be a problem for some client:
2203 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
2204 return nfserr_toosmall;
2205 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
2206 return nfserr_toosmall;
2207 ca->maxresp_cached = 0;
2209 return nfserr_toosmall;
2214 static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2216 switch (cbs->flavor) {
2222 * GSS case: the spec doesn't allow us to return this
2223 * error. But it also doesn't allow us not to support
2225 * I'd rather this fail hard than return some error the
2226 * client might think it can already handle:
2228 return nfserr_encr_alg_unsupp;
2233 nfsd4_create_session(struct svc_rqst *rqstp,
2234 struct nfsd4_compound_state *cstate,
2235 struct nfsd4_create_session *cr_ses)
2237 struct sockaddr *sa = svc_addr(rqstp);
2238 struct nfs4_client *conf, *unconf;
2239 struct nfsd4_session *new;
2240 struct nfsd4_conn *conn;
2241 struct nfsd4_clid_slot *cs_slot = NULL;
2243 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2245 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2246 return nfserr_inval;
2247 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2250 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
2253 status = check_backchannel_attrs(&cr_ses->back_channel);
2255 goto out_release_drc_mem;
2256 status = nfserr_jukebox;
2257 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
2259 goto out_release_drc_mem;
2260 conn = alloc_conn_from_crses(rqstp, cr_ses);
2262 goto out_free_session;
2265 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
2266 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
2267 WARN_ON_ONCE(conf && unconf);
2270 status = nfserr_wrong_cred;
2271 if (!mach_creds_match(conf, rqstp))
2273 cs_slot = &conf->cl_cs_slot;
2274 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2275 if (status == nfserr_replay_cache) {
2276 status = nfsd4_replay_create_session(cr_ses, cs_slot);
2278 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
2279 status = nfserr_seq_misordered;
2282 } else if (unconf) {
2283 struct nfs4_client *old;
2284 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
2285 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
2286 status = nfserr_clid_inuse;
2289 status = nfserr_wrong_cred;
2290 if (!mach_creds_match(unconf, rqstp))
2292 cs_slot = &unconf->cl_cs_slot;
2293 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2295 /* an unconfirmed replay returns misordered */
2296 status = nfserr_seq_misordered;
2299 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
2301 status = mark_client_expired(old);
2306 move_to_confirmed(unconf);
2309 status = nfserr_stale_clientid;
2314 * We do not support RDMA or persistent sessions
2316 cr_ses->flags &= ~SESSION4_PERSIST;
2317 cr_ses->flags &= ~SESSION4_RDMA;
2319 init_session(rqstp, new, conf, cr_ses);
2320 nfsd4_init_conn(rqstp, conn, new);
2322 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
2323 NFS4_MAX_SESSIONID_LEN);
2324 cs_slot->sl_seqid++;
2325 cr_ses->seqid = cs_slot->sl_seqid;
2327 /* cache solo and embedded create sessions under the state lock */
2328 nfsd4_cache_create_session(cr_ses, cs_slot, status);
2329 nfs4_unlock_state();
2332 nfs4_unlock_state();
2335 __free_session(new);
2336 out_release_drc_mem:
2337 nfsd4_put_drc_mem(&cr_ses->fore_channel);
2341 static __be32 nfsd4_map_bcts_dir(u32 *dir)
2344 case NFS4_CDFC4_FORE:
2345 case NFS4_CDFC4_BACK:
2347 case NFS4_CDFC4_FORE_OR_BOTH:
2348 case NFS4_CDFC4_BACK_OR_BOTH:
2349 *dir = NFS4_CDFC4_BOTH;
2352 return nfserr_inval;
2355 __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2357 struct nfsd4_session *session = cstate->session;
2358 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2361 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2364 spin_lock(&nn->client_lock);
2365 session->se_cb_prog = bc->bc_cb_program;
2366 session->se_cb_sec = bc->bc_cb_sec;
2367 spin_unlock(&nn->client_lock);
2369 nfsd4_probe_callback(session->se_client);
2374 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2375 struct nfsd4_compound_state *cstate,
2376 struct nfsd4_bind_conn_to_session *bcts)
2379 struct nfsd4_conn *conn;
2380 struct nfsd4_session *session;
2381 struct net *net = SVC_NET(rqstp);
2382 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2384 if (!nfsd4_last_compound_op(rqstp))
2385 return nfserr_not_only_op;
2387 spin_lock(&nn->client_lock);
2388 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
2389 spin_unlock(&nn->client_lock);
2391 goto out_no_session;
2392 status = nfserr_wrong_cred;
2393 if (!mach_creds_match(session->se_client, rqstp))
2395 status = nfsd4_map_bcts_dir(&bcts->dir);
2398 conn = alloc_conn(rqstp, bcts->dir);
2399 status = nfserr_jukebox;
2402 nfsd4_init_conn(rqstp, conn, session);
2405 nfsd4_put_session(session);
2407 nfs4_unlock_state();
2411 static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2415 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2419 nfsd4_destroy_session(struct svc_rqst *r,
2420 struct nfsd4_compound_state *cstate,
2421 struct nfsd4_destroy_session *sessionid)
2423 struct nfsd4_session *ses;
2425 int ref_held_by_me = 0;
2426 struct net *net = SVC_NET(r);
2427 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2430 status = nfserr_not_only_op;
2431 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
2432 if (!nfsd4_last_compound_op(r))
2436 dump_sessionid(__func__, &sessionid->sessionid);
2437 spin_lock(&nn->client_lock);
2438 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
2440 goto out_client_lock;
2441 status = nfserr_wrong_cred;
2442 if (!mach_creds_match(ses->se_client, r))
2443 goto out_put_session;
2444 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
2446 goto out_put_session;
2447 unhash_session(ses);
2448 spin_unlock(&nn->client_lock);
2450 nfsd4_probe_callback_sync(ses->se_client);
2452 spin_lock(&nn->client_lock);
2455 nfsd4_put_session_locked(ses);
2457 spin_unlock(&nn->client_lock);
2459 nfs4_unlock_state();
2463 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
2465 struct nfsd4_conn *c;
2467 list_for_each_entry(c, &s->se_conns, cn_persession) {
2468 if (c->cn_xprt == xpt) {
2475 static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
2477 struct nfs4_client *clp = ses->se_client;
2478 struct nfsd4_conn *c;
2479 __be32 status = nfs_ok;
2482 spin_lock(&clp->cl_lock);
2483 c = __nfsd4_find_conn(new->cn_xprt, ses);
2486 status = nfserr_conn_not_bound_to_session;
2487 if (clp->cl_mach_cred)
2489 __nfsd4_hash_conn(new, ses);
2490 spin_unlock(&clp->cl_lock);
2491 ret = nfsd4_register_conn(new);
2493 /* oops; xprt is already down: */
2494 nfsd4_conn_lost(&new->cn_xpt_user);
2497 spin_unlock(&clp->cl_lock);
2502 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2504 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2506 return args->opcnt > session->se_fchannel.maxops;
2509 static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2510 struct nfsd4_session *session)
2512 struct xdr_buf *xb = &rqstp->rq_arg;
2514 return xb->len > session->se_fchannel.maxreq_sz;
2518 nfsd4_sequence(struct svc_rqst *rqstp,
2519 struct nfsd4_compound_state *cstate,
2520 struct nfsd4_sequence *seq)
2522 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2523 struct xdr_stream *xdr = &resp->xdr;
2524 struct nfsd4_session *session;
2525 struct nfs4_client *clp;
2526 struct nfsd4_slot *slot;
2527 struct nfsd4_conn *conn;
2530 struct net *net = SVC_NET(rqstp);
2531 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2533 if (resp->opcnt != 1)
2534 return nfserr_sequence_pos;
2537 * Will be either used or freed by nfsd4_sequence_check_conn
2540 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2542 return nfserr_jukebox;
2544 spin_lock(&nn->client_lock);
2545 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
2547 goto out_no_session;
2548 clp = session->se_client;
2550 status = nfserr_too_many_ops;
2551 if (nfsd4_session_too_many_ops(rqstp, session))
2552 goto out_put_session;
2554 status = nfserr_req_too_big;
2555 if (nfsd4_request_too_big(rqstp, session))
2556 goto out_put_session;
2558 status = nfserr_badslot;
2559 if (seq->slotid >= session->se_fchannel.maxreqs)
2560 goto out_put_session;
2562 slot = session->se_slots[seq->slotid];
2563 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2565 /* We do not negotiate the number of slots yet, so set the
2566 * maxslots to the session maxreqs which is used to encode
2567 * sr_highest_slotid and the sr_target_slot id to maxslots */
2568 seq->maxslots = session->se_fchannel.maxreqs;
2570 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2571 slot->sl_flags & NFSD4_SLOT_INUSE);
2572 if (status == nfserr_replay_cache) {
2573 status = nfserr_seq_misordered;
2574 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
2575 goto out_put_session;
2576 cstate->slot = slot;
2577 cstate->session = session;
2579 /* Return the cached reply status and set cstate->status
2580 * for nfsd4_proc_compound processing */
2581 status = nfsd4_replay_cache_entry(resp, seq);
2582 cstate->status = nfserr_replay_cache;
2586 goto out_put_session;
2588 status = nfsd4_sequence_check_conn(conn, session);
2591 goto out_put_session;
2593 buflen = (seq->cachethis) ?
2594 session->se_fchannel.maxresp_cached :
2595 session->se_fchannel.maxresp_sz;
2596 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2598 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
2599 goto out_put_session;
2600 svc_reserve(rqstp, buflen);
2603 /* Success! bump slot seqid */
2604 slot->sl_seqid = seq->seqid;
2605 slot->sl_flags |= NFSD4_SLOT_INUSE;
2607 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
2609 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
2611 cstate->slot = slot;
2612 cstate->session = session;
2616 switch (clp->cl_cb_state) {
2618 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2620 case NFSD4_CB_FAULT:
2621 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2624 seq->status_flags = 0;
2626 if (!list_empty(&clp->cl_revoked))
2627 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
2631 spin_unlock(&nn->client_lock);
2634 nfsd4_put_session_locked(session);
2635 goto out_no_session;
2639 nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2641 struct nfsd4_compound_state *cs = &resp->cstate;
2643 if (nfsd4_has_session(cs)) {
2644 if (cs->status != nfserr_replay_cache) {
2645 nfsd4_store_cache_entry(resp);
2646 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2648 /* Drop session reference that was taken in nfsd4_sequence() */
2649 nfsd4_put_session(cs->session);
2651 put_client_renew(cs->clp);
2655 nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2657 struct nfs4_client *conf, *unconf, *clp;
2659 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2662 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
2663 conf = find_confirmed_client(&dc->clientid, true, nn);
2664 WARN_ON_ONCE(conf && unconf);
2669 if (client_has_state(conf)) {
2670 status = nfserr_clientid_busy;
2676 status = nfserr_stale_clientid;
2679 if (!mach_creds_match(clp, rqstp)) {
2680 status = nfserr_wrong_cred;
2685 nfs4_unlock_state();
2690 nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2694 if (rc->rca_one_fs) {
2695 if (!cstate->current_fh.fh_dentry)
2696 return nfserr_nofilehandle;
2698 * We don't take advantage of the rca_one_fs case.
2699 * That's OK, it's optional, we can safely ignore it.
2705 status = nfserr_complete_already;
2706 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2707 &cstate->session->se_client->cl_flags))
2710 status = nfserr_stale_clientid;
2711 if (is_client_expired(cstate->session->se_client))
2713 * The following error isn't really legal.
2714 * But we only get here if the client just explicitly
2715 * destroyed the client. Surely it no longer cares what
2716 * error it gets back on an operation for the dead
2722 nfsd4_client_record_create(cstate->session->se_client);
2724 nfs4_unlock_state();
2729 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2730 struct nfsd4_setclientid *setclid)
2732 struct xdr_netobj clname = setclid->se_name;
2733 nfs4_verifier clverifier = setclid->se_verf;
2734 struct nfs4_client *conf, *unconf, *new;
2736 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2738 /* Cases below refer to rfc 3530 section 14.2.33: */
2740 conf = find_confirmed_client_by_name(&clname, nn);
2743 status = nfserr_clid_inuse;
2744 if (clp_used_exchangeid(conf))
2746 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
2747 char addr_str[INET6_ADDRSTRLEN];
2748 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2750 dprintk("NFSD: setclientid: string in use by client "
2751 "at %s\n", addr_str);
2755 unconf = find_unconfirmed_client_by_name(&clname, nn);
2757 expire_client(unconf);
2758 status = nfserr_jukebox;
2759 new = create_client(clname, rqstp, &clverifier);
2762 if (conf && same_verf(&conf->cl_verifier, &clverifier))
2763 /* case 1: probable callback update */
2764 copy_clid(new, conf);
2765 else /* case 4 (new client) or cases 2, 3 (client reboot): */
2767 new->cl_minorversion = 0;
2768 gen_callback(new, setclid, rqstp);
2769 add_to_unconfirmed(new);
2770 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2771 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2772 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2775 nfs4_unlock_state();
2781 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2782 struct nfsd4_compound_state *cstate,
2783 struct nfsd4_setclientid_confirm *setclientid_confirm)
2785 struct nfs4_client *conf, *unconf;
2786 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2787 clientid_t * clid = &setclientid_confirm->sc_clientid;
2789 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2791 if (STALE_CLIENTID(clid, nn))
2792 return nfserr_stale_clientid;
2795 conf = find_confirmed_client(clid, false, nn);
2796 unconf = find_unconfirmed_client(clid, false, nn);
2798 * We try hard to give out unique clientid's, so if we get an
2799 * attempt to confirm the same clientid with a different cred,
2800 * there's a bug somewhere. Let's charitably assume it's our
2803 status = nfserr_serverfault;
2804 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
2806 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
2808 /* cases below refer to rfc 3530 section 14.2.34: */
2809 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
2810 if (conf && !unconf) /* case 2: probable retransmit */
2812 else /* case 4: client hasn't noticed we rebooted yet? */
2813 status = nfserr_stale_clientid;
2817 if (conf) { /* case 1: callback update */
2818 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2819 nfsd4_probe_callback(conf);
2820 expire_client(unconf);
2821 } else { /* case 3: normal case; new or rebooted client */
2822 conf = find_confirmed_client_by_name(&unconf->cl_name, nn);
2824 status = mark_client_expired(conf);
2827 expire_client(conf);
2829 move_to_confirmed(unconf);
2830 nfsd4_probe_callback(unconf);
2833 nfs4_unlock_state();
2837 static struct nfs4_file *nfsd4_alloc_file(void)
2839 return kmem_cache_alloc(file_slab, GFP_KERNEL);
2842 /* OPEN Share state helper functions */
2843 static void nfsd4_init_file(struct nfs4_file *fp, struct knfsd_fh *fh)
2845 unsigned int hashval = file_hashval(fh);
2847 lockdep_assert_held(&state_lock);
2849 atomic_set(&fp->fi_ref, 1);
2850 spin_lock_init(&fp->fi_lock);
2851 INIT_LIST_HEAD(&fp->fi_stateids);
2852 INIT_LIST_HEAD(&fp->fi_delegations);
2853 fh_copy_shallow(&fp->fi_fhandle, fh);
2854 fp->fi_had_conflict = false;
2855 fp->fi_lease = NULL;
2856 fp->fi_share_deny = 0;
2857 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2858 memset(fp->fi_access, 0, sizeof(fp->fi_access));
2859 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
2863 nfsd4_free_slabs(void)
2865 kmem_cache_destroy(openowner_slab);
2866 kmem_cache_destroy(lockowner_slab);
2867 kmem_cache_destroy(file_slab);
2868 kmem_cache_destroy(stateid_slab);
2869 kmem_cache_destroy(deleg_slab);
2873 nfsd4_init_slabs(void)
2875 openowner_slab = kmem_cache_create("nfsd4_openowners",
2876 sizeof(struct nfs4_openowner), 0, 0, NULL);
2877 if (openowner_slab == NULL)
2879 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
2880 sizeof(struct nfs4_lockowner), 0, 0, NULL);
2881 if (lockowner_slab == NULL)
2882 goto out_free_openowner_slab;
2883 file_slab = kmem_cache_create("nfsd4_files",
2884 sizeof(struct nfs4_file), 0, 0, NULL);
2885 if (file_slab == NULL)
2886 goto out_free_lockowner_slab;
2887 stateid_slab = kmem_cache_create("nfsd4_stateids",
2888 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
2889 if (stateid_slab == NULL)
2890 goto out_free_file_slab;
2891 deleg_slab = kmem_cache_create("nfsd4_delegations",
2892 sizeof(struct nfs4_delegation), 0, 0, NULL);
2893 if (deleg_slab == NULL)
2894 goto out_free_stateid_slab;
2897 out_free_stateid_slab:
2898 kmem_cache_destroy(stateid_slab);
2900 kmem_cache_destroy(file_slab);
2901 out_free_lockowner_slab:
2902 kmem_cache_destroy(lockowner_slab);
2903 out_free_openowner_slab:
2904 kmem_cache_destroy(openowner_slab);
2906 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2910 static void init_nfs4_replay(struct nfs4_replay *rp)
2912 rp->rp_status = nfserr_serverfault;
2914 rp->rp_buf = rp->rp_ibuf;
2917 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
2919 struct nfs4_stateowner *sop;
2921 sop = kmem_cache_alloc(slab, GFP_KERNEL);
2925 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
2926 if (!sop->so_owner.data) {
2927 kmem_cache_free(slab, sop);
2930 sop->so_owner.len = owner->len;
2932 INIT_LIST_HEAD(&sop->so_stateids);
2933 sop->so_client = clp;
2934 init_nfs4_replay(&sop->so_replay);
2938 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
2940 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2942 list_add(&oo->oo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
2943 list_add(&oo->oo_perclient, &clp->cl_openowners);
2946 static struct nfs4_openowner *
2947 alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
2948 struct nfsd4_compound_state *cstate)
2950 struct nfs4_client *clp = cstate->clp;
2951 struct nfs4_openowner *oo;
2953 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
2956 oo->oo_owner.so_is_open_owner = 1;
2957 oo->oo_owner.so_seqid = open->op_seqid;
2958 oo->oo_flags = NFS4_OO_NEW;
2959 if (nfsd4_has_session(cstate))
2960 oo->oo_flags |= NFS4_OO_CONFIRMED;
2962 oo->oo_last_closed_stid = NULL;
2963 INIT_LIST_HEAD(&oo->oo_close_lru);
2964 hash_openowner(oo, clp, strhashval);
2968 static void init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
2969 struct nfs4_openowner *oo = open->op_openowner;
2971 stp->st_stid.sc_type = NFS4_OPEN_STID;
2972 INIT_LIST_HEAD(&stp->st_locks);
2973 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
2974 stp->st_stateowner = &oo->oo_owner;
2976 stp->st_stid.sc_file = fp;
2977 stp->st_access_bmap = 0;
2978 stp->st_deny_bmap = 0;
2979 stp->st_openstp = NULL;
2980 spin_lock(&fp->fi_lock);
2981 list_add(&stp->st_perfile, &fp->fi_stateids);
2982 spin_unlock(&fp->fi_lock);
2986 move_to_close_lru(struct nfs4_openowner *oo, struct net *net)
2988 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2990 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
2992 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
2993 oo->oo_time = get_seconds();
2997 same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
3000 return (sop->so_owner.len == owner->len) &&
3001 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
3002 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
3005 static struct nfs4_openowner *
3006 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
3007 bool sessions, struct nfsd_net *nn)
3009 struct nfs4_stateowner *so;
3010 struct nfs4_openowner *oo;
3011 struct nfs4_client *clp;
3013 list_for_each_entry(so, &nn->ownerstr_hashtbl[hashval], so_strhash) {
3014 if (!so->so_is_open_owner)
3016 if (same_owner_str(so, &open->op_owner, &open->op_clientid)) {
3018 clp = oo->oo_owner.so_client;
3019 if ((bool)clp->cl_minorversion != sessions)
3021 renew_client(oo->oo_owner.so_client);
3028 /* search file_hashtbl[] for file */
3029 static struct nfs4_file *
3030 find_file_locked(struct knfsd_fh *fh)
3032 unsigned int hashval = file_hashval(fh);
3033 struct nfs4_file *fp;
3035 lockdep_assert_held(&state_lock);
3037 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
3038 if (nfsd_fh_match(&fp->fi_fhandle, fh)) {
3046 static struct nfs4_file *
3047 find_file(struct knfsd_fh *fh)
3049 struct nfs4_file *fp;
3051 spin_lock(&state_lock);
3052 fp = find_file_locked(fh);
3053 spin_unlock(&state_lock);
3057 static struct nfs4_file *
3058 find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
3060 struct nfs4_file *fp;
3062 spin_lock(&state_lock);
3063 fp = find_file_locked(fh);
3065 nfsd4_init_file(new, fh);
3068 spin_unlock(&state_lock);
3074 * Called to check deny when READ with all zero stateid or
3075 * WRITE with all zero or all one stateid
3078 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3080 struct nfs4_file *fp;
3081 __be32 ret = nfs_ok;
3083 fp = find_file(¤t_fh->fh_handle);
3086 /* Check for conflicting share reservations */
3087 spin_lock(&fp->fi_lock);
3088 if (fp->fi_share_deny & deny_type)
3089 ret = nfserr_locked;
3090 spin_unlock(&fp->fi_lock);
3095 void nfsd4_prepare_cb_recall(struct nfs4_delegation *dp)
3097 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3100 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
3103 * We can't do this in nfsd_break_deleg_cb because it is
3104 * already holding inode->i_lock.
3106 * If the dl_time != 0, then we know that it has already been
3107 * queued for a lease break. Don't queue it again.
3109 spin_lock(&state_lock);
3110 if (dp->dl_time == 0) {
3111 dp->dl_time = get_seconds();
3112 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
3114 spin_unlock(&state_lock);
3117 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3120 * We're assuming the state code never drops its reference
3121 * without first removing the lease. Since we're in this lease
3122 * callback (and since the lease code is serialized by the kernel
3123 * lock) we know the server hasn't removed the lease yet, we know
3124 * it's safe to take a reference.
3126 atomic_inc(&dp->dl_stid.sc_count);
3127 nfsd4_cb_recall(dp);
3130 /* Called from break_lease() with i_lock held. */
3131 static void nfsd_break_deleg_cb(struct file_lock *fl)
3133 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3134 struct nfs4_delegation *dp;
3137 WARN(1, "(%p)->fl_owner NULL\n", fl);
3140 if (fp->fi_had_conflict) {
3141 WARN(1, "duplicate break on %p\n", fp);
3145 * We don't want the locks code to timeout the lease for us;
3146 * we'll remove it ourself if a delegation isn't returned
3149 fl->fl_break_time = 0;
3151 spin_lock(&fp->fi_lock);
3152 fp->fi_had_conflict = true;
3154 * If there are no delegations on the list, then we can't count on this
3155 * lease ever being cleaned up. Set the fl_break_time to jiffies so that
3156 * time_out_leases will do it ASAP. The fact that fi_had_conflict is now
3157 * true should keep any new delegations from being hashed.
3159 if (list_empty(&fp->fi_delegations))
3160 fl->fl_break_time = jiffies;
3162 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3163 nfsd_break_one_deleg(dp);
3164 spin_unlock(&fp->fi_lock);
3168 int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
3171 return lease_modify(onlist, arg);
3176 static const struct lock_manager_operations nfsd_lease_mng_ops = {
3177 .lm_break = nfsd_break_deleg_cb,
3178 .lm_change = nfsd_change_deleg_cb,
3181 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3183 if (nfsd4_has_session(cstate))
3185 if (seqid == so->so_seqid - 1)
3186 return nfserr_replay_me;
3187 if (seqid == so->so_seqid)
3189 return nfserr_bad_seqid;
3192 static __be32 lookup_clientid(clientid_t *clid,
3193 struct nfsd4_compound_state *cstate,
3194 struct nfsd_net *nn)
3196 struct nfs4_client *found;
3199 found = cstate->clp;
3200 if (!same_clid(&found->cl_clientid, clid))
3201 return nfserr_stale_clientid;
3205 if (STALE_CLIENTID(clid, nn))
3206 return nfserr_stale_clientid;
3209 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3210 * cached already then we know this is for is for v4.0 and "sessions"
3213 WARN_ON_ONCE(cstate->session);
3214 found = find_confirmed_client(clid, false, nn);
3216 return nfserr_expired;
3218 /* Cache the nfs4_client in cstate! */
3219 cstate->clp = found;
3220 atomic_inc(&found->cl_refcount);
3225 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
3226 struct nfsd4_open *open, struct nfsd_net *nn)
3228 clientid_t *clientid = &open->op_clientid;
3229 struct nfs4_client *clp = NULL;
3230 unsigned int strhashval;
3231 struct nfs4_openowner *oo = NULL;
3234 if (STALE_CLIENTID(&open->op_clientid, nn))
3235 return nfserr_stale_clientid;
3237 * In case we need it later, after we've already created the
3238 * file and don't want to risk a further failure:
3240 open->op_file = nfsd4_alloc_file();
3241 if (open->op_file == NULL)
3242 return nfserr_jukebox;
3244 status = lookup_clientid(clientid, cstate, nn);
3249 strhashval = ownerstr_hashval(clientid->cl_id, &open->op_owner);
3250 oo = find_openstateowner_str(strhashval, open, cstate->minorversion, nn);
3251 open->op_openowner = oo;
3255 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
3256 /* Replace unconfirmed owners without checking for replay. */
3257 release_openowner(oo);
3258 open->op_openowner = NULL;
3261 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3266 oo = alloc_init_open_stateowner(strhashval, open, cstate);
3268 return nfserr_jukebox;
3269 open->op_openowner = oo;
3271 open->op_stp = nfs4_alloc_stateid(clp);
3273 return nfserr_jukebox;
3277 static inline __be32
3278 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3280 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3281 return nfserr_openmode;
3286 static int share_access_to_flags(u32 share_access)
3288 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3291 static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
3293 struct nfs4_stid *ret;
3295 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
3298 return delegstateid(ret);
3301 static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3303 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3304 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3308 nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
3309 struct nfs4_delegation **dp)
3312 __be32 status = nfserr_bad_stateid;
3314 *dp = find_deleg_stateid(cl, &open->op_delegate_stateid);
3317 flags = share_access_to_flags(open->op_share_access);
3318 status = nfs4_check_delegmode(*dp, flags);
3322 if (!nfsd4_is_deleg_cur(open))
3326 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
3330 static struct nfs4_ol_stateid *
3331 nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
3333 struct nfs4_ol_stateid *local, *ret = NULL;
3334 struct nfs4_openowner *oo = open->op_openowner;
3336 spin_lock(&fp->fi_lock);
3337 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
3338 /* ignore lock owners */
3339 if (local->st_stateowner->so_is_open_owner == 0)
3341 if (local->st_stateowner == &oo->oo_owner) {
3346 spin_unlock(&fp->fi_lock);
3350 static inline int nfs4_access_to_access(u32 nfs4_access)
3354 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3355 flags |= NFSD_MAY_READ;
3356 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3357 flags |= NFSD_MAY_WRITE;
3361 static inline __be32
3362 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3363 struct nfsd4_open *open)
3365 struct iattr iattr = {
3366 .ia_valid = ATTR_SIZE,
3369 if (!open->op_truncate)
3371 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
3372 return nfserr_inval;
3373 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3376 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
3377 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3378 struct nfsd4_open *open)
3380 struct file *filp = NULL;
3382 int oflag = nfs4_access_to_omode(open->op_share_access);
3383 int access = nfs4_access_to_access(open->op_share_access);
3384 unsigned char old_access_bmap, old_deny_bmap;
3386 spin_lock(&fp->fi_lock);
3389 * Are we trying to set a deny mode that would conflict with
3392 status = nfs4_file_check_deny(fp, open->op_share_deny);
3393 if (status != nfs_ok) {
3394 spin_unlock(&fp->fi_lock);
3398 /* set access to the file */
3399 status = nfs4_file_get_access(fp, open->op_share_access);
3400 if (status != nfs_ok) {
3401 spin_unlock(&fp->fi_lock);
3405 /* Set access bits in stateid */
3406 old_access_bmap = stp->st_access_bmap;
3407 set_access(open->op_share_access, stp);
3409 /* Set new deny mask */
3410 old_deny_bmap = stp->st_deny_bmap;
3411 set_deny(open->op_share_deny, stp);
3412 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3414 if (!fp->fi_fds[oflag]) {
3415 spin_unlock(&fp->fi_lock);
3416 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
3418 goto out_put_access;
3419 spin_lock(&fp->fi_lock);
3420 if (!fp->fi_fds[oflag]) {
3421 fp->fi_fds[oflag] = filp;
3425 spin_unlock(&fp->fi_lock);
3429 status = nfsd4_truncate(rqstp, cur_fh, open);
3431 goto out_put_access;
3435 stp->st_access_bmap = old_access_bmap;
3436 nfs4_file_put_access(fp, open->op_share_access);
3437 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3442 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)
3445 unsigned char old_deny_bmap;
3447 if (!test_access(open->op_share_access, stp))
3448 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
3450 /* test and set deny mode */
3451 spin_lock(&fp->fi_lock);
3452 status = nfs4_file_check_deny(fp, open->op_share_deny);
3453 if (status == nfs_ok) {
3454 old_deny_bmap = stp->st_deny_bmap;
3455 set_deny(open->op_share_deny, stp);
3456 fp->fi_share_deny |=
3457 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3459 spin_unlock(&fp->fi_lock);
3461 if (status != nfs_ok)
3464 status = nfsd4_truncate(rqstp, cur_fh, open);
3465 if (status != nfs_ok)
3466 reset_union_bmap_deny(old_deny_bmap, stp);
3471 nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
3473 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
3476 /* Should we give out recallable state?: */
3477 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3479 if (clp->cl_cb_state == NFSD4_CB_UP)
3482 * In the sessions case, since we don't have to establish a
3483 * separate connection for callbacks, we assume it's OK
3484 * until we hear otherwise:
3486 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3489 static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
3491 struct file_lock *fl;
3493 fl = locks_alloc_lock();
3496 locks_init_lock(fl);
3497 fl->fl_lmops = &nfsd_lease_mng_ops;
3498 fl->fl_flags = FL_DELEG;
3499 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3500 fl->fl_end = OFFSET_MAX;
3501 fl->fl_owner = (fl_owner_t)fp;
3502 fl->fl_pid = current->tgid;
3506 static int nfs4_setlease(struct nfs4_delegation *dp)
3508 struct nfs4_file *fp = dp->dl_stid.sc_file;
3509 struct file_lock *fl;
3513 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
3516 filp = find_readable_file(fp);
3518 /* We should always have a readable file here */
3523 status = vfs_setlease(filp, fl->fl_type, &fl);
3525 locks_free_lock(fl);
3528 spin_lock(&state_lock);
3529 spin_lock(&fp->fi_lock);
3530 /* Did the lease get broken before we took the lock? */
3532 if (fp->fi_had_conflict)
3537 atomic_inc(&fp->fi_delegees);
3538 hash_delegation_locked(dp, fp);
3542 fp->fi_deleg_file = filp;
3543 atomic_set(&fp->fi_delegees, 1);
3544 hash_delegation_locked(dp, fp);
3545 spin_unlock(&fp->fi_lock);
3546 spin_unlock(&state_lock);
3549 spin_unlock(&fp->fi_lock);
3550 spin_unlock(&state_lock);
3556 static struct nfs4_delegation *
3557 nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
3558 struct nfs4_file *fp)
3561 struct nfs4_delegation *dp;
3563 if (fp->fi_had_conflict)
3564 return ERR_PTR(-EAGAIN);
3566 dp = alloc_init_deleg(clp, fh);
3568 return ERR_PTR(-ENOMEM);
3571 spin_lock(&state_lock);
3572 spin_lock(&fp->fi_lock);
3573 dp->dl_stid.sc_file = fp;
3574 if (!fp->fi_lease) {
3575 spin_unlock(&fp->fi_lock);
3576 spin_unlock(&state_lock);
3577 status = nfs4_setlease(dp);
3580 atomic_inc(&fp->fi_delegees);
3581 if (fp->fi_had_conflict) {
3585 hash_delegation_locked(dp, fp);
3588 spin_unlock(&fp->fi_lock);
3589 spin_unlock(&state_lock);
3592 nfs4_put_stid(&dp->dl_stid);
3593 return ERR_PTR(status);
3598 static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3600 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3601 if (status == -EAGAIN)
3602 open->op_why_no_deleg = WND4_CONTENTION;
3604 open->op_why_no_deleg = WND4_RESOURCE;
3605 switch (open->op_deleg_want) {
3606 case NFS4_SHARE_WANT_READ_DELEG:
3607 case NFS4_SHARE_WANT_WRITE_DELEG:
3608 case NFS4_SHARE_WANT_ANY_DELEG:
3610 case NFS4_SHARE_WANT_CANCEL:
3611 open->op_why_no_deleg = WND4_CANCELLED;
3613 case NFS4_SHARE_WANT_NO_DELEG:
3620 * Attempt to hand out a delegation.
3622 * Note we don't support write delegations, and won't until the vfs has
3623 * proper support for them.
3626 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
3627 struct nfs4_ol_stateid *stp)
3629 struct nfs4_delegation *dp;
3630 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
3631 struct nfs4_client *clp = stp->st_stid.sc_client;
3635 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
3636 open->op_recall = 0;
3637 switch (open->op_claim_type) {
3638 case NFS4_OPEN_CLAIM_PREVIOUS:
3640 open->op_recall = 1;
3641 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3644 case NFS4_OPEN_CLAIM_NULL:
3645 case NFS4_OPEN_CLAIM_FH:
3647 * Let's not give out any delegations till everyone's
3648 * had the chance to reclaim theirs....
3650 if (locks_in_grace(clp->net))
3652 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
3655 * Also, if the file was opened for write or
3656 * create, there's a good chance the client's
3657 * about to write to it, resulting in an
3658 * immediate recall (since we don't support
3659 * write delegations):
3661 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
3663 if (open->op_create == NFS4_OPEN_CREATE)
3669 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file);
3673 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
3675 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
3676 STATEID_VAL(&dp->dl_stid.sc_stateid));
3677 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
3680 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3681 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
3682 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
3683 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
3684 open->op_recall = 1;
3687 /* 4.1 client asking for a delegation? */
3688 if (open->op_deleg_want)
3689 nfsd4_open_deleg_none_ext(open, status);
3693 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3694 struct nfs4_delegation *dp)
3696 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3697 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3698 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3699 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3700 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3701 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3702 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3703 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3705 /* Otherwise the client must be confused wanting a delegation
3706 * it already has, therefore we don't return
3707 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3712 * called with nfs4_lock_state() held.
3715 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3717 struct nfsd4_compoundres *resp = rqstp->rq_resp;
3718 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
3719 struct nfs4_file *fp = NULL;
3720 struct nfs4_ol_stateid *stp = NULL;
3721 struct nfs4_delegation *dp = NULL;
3725 * Lookup file; if found, lookup stateid and check open request,
3726 * and check for delegations in the process of being recalled.
3727 * If not found, create the nfs4_file struct
3729 fp = find_or_add_file(open->op_file, ¤t_fh->fh_handle);
3730 if (fp != open->op_file) {
3731 status = nfs4_check_deleg(cl, open, &dp);
3734 stp = nfsd4_find_existing_open(fp, open);
3736 open->op_file = NULL;
3737 status = nfserr_bad_stateid;
3738 if (nfsd4_is_deleg_cur(open))
3740 status = nfserr_jukebox;
3744 * OPEN the file, or upgrade an existing OPEN.
3745 * If truncate fails, the OPEN fails.
3748 /* Stateid was found, this is an OPEN upgrade */
3749 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
3754 open->op_stp = NULL;
3755 init_open_stateid(stp, fp, open);
3756 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
3758 release_open_stateid(stp);
3762 update_stateid(&stp->st_stid.sc_stateid);
3763 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
3765 if (nfsd4_has_session(&resp->cstate)) {
3766 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
3767 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3768 open->op_why_no_deleg = WND4_NOT_WANTED;
3774 * Attempt to hand out a delegation. No error return, because the
3775 * OPEN succeeds even if we fail.
3777 nfs4_open_delegation(current_fh, open, stp);
3781 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
3782 STATEID_VAL(&stp->st_stid.sc_stateid));
3784 /* 4.1 client trying to upgrade/downgrade delegation? */
3785 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
3786 open->op_deleg_want)
3787 nfsd4_deleg_xgrade_none_ext(open, dp);
3791 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
3792 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
3794 * To finish the open response, we just need to set the rflags.
3796 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
3797 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
3798 !nfsd4_has_session(&resp->cstate))
3799 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
3804 void nfsd4_cleanup_open_state(struct nfsd4_open *open, __be32 status)
3806 if (open->op_openowner) {
3807 struct nfs4_openowner *oo = open->op_openowner;
3809 if (!list_empty(&oo->oo_owner.so_stateids))
3810 list_del_init(&oo->oo_close_lru);
3811 if (oo->oo_flags & NFS4_OO_NEW) {
3813 release_openowner(oo);
3814 open->op_openowner = NULL;
3816 oo->oo_flags &= ~NFS4_OO_NEW;
3820 nfsd4_free_file(open->op_file);
3822 nfs4_put_stid(&open->op_stp->st_stid);
3826 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3829 struct nfs4_client *clp;
3831 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3834 dprintk("process_renew(%08x/%08x): starting\n",
3835 clid->cl_boot, clid->cl_id);
3836 status = lookup_clientid(clid, cstate, nn);
3840 status = nfserr_cb_path_down;
3841 if (!list_empty(&clp->cl_delegations)
3842 && clp->cl_cb_state != NFSD4_CB_UP)
3846 nfs4_unlock_state();
3851 nfsd4_end_grace(struct nfsd_net *nn)
3853 /* do nothing if grace period already ended */
3854 if (nn->grace_ended)
3857 dprintk("NFSD: end of grace period\n");
3858 nn->grace_ended = true;
3859 nfsd4_record_grace_done(nn, nn->boot_time);
3860 locks_end_grace(&nn->nfsd4_manager);
3862 * Now that every NFSv4 client has had the chance to recover and
3863 * to see the (possibly new, possibly shorter) lease time, we
3864 * can safely set the next grace time to the current lease time:
3866 nn->nfsd4_grace = nn->nfsd4_lease;
3870 nfs4_laundromat(struct nfsd_net *nn)
3872 struct nfs4_client *clp;
3873 struct nfs4_openowner *oo;
3874 struct nfs4_delegation *dp;
3875 struct list_head *pos, *next, reaplist;
3876 time_t cutoff = get_seconds() - nn->nfsd4_lease;
3877 time_t t, new_timeo = nn->nfsd4_lease;
3881 dprintk("NFSD: laundromat service - starting\n");
3882 nfsd4_end_grace(nn);
3883 INIT_LIST_HEAD(&reaplist);
3884 spin_lock(&nn->client_lock);
3885 list_for_each_safe(pos, next, &nn->client_lru) {
3886 clp = list_entry(pos, struct nfs4_client, cl_lru);
3887 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
3888 t = clp->cl_time - cutoff;
3889 new_timeo = min(new_timeo, t);
3892 if (mark_client_expired_locked(clp)) {
3893 dprintk("NFSD: client in use (clientid %08x)\n",
3894 clp->cl_clientid.cl_id);
3897 list_move(&clp->cl_lru, &reaplist);
3899 spin_unlock(&nn->client_lock);
3900 list_for_each_safe(pos, next, &reaplist) {
3901 clp = list_entry(pos, struct nfs4_client, cl_lru);
3902 dprintk("NFSD: purging unused client (clientid %08x)\n",
3903 clp->cl_clientid.cl_id);
3906 spin_lock(&state_lock);
3907 list_for_each_safe(pos, next, &nn->del_recall_lru) {
3908 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3909 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
3911 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
3912 t = dp->dl_time - cutoff;
3913 new_timeo = min(new_timeo, t);
3916 unhash_delegation_locked(dp);
3917 list_add(&dp->dl_recall_lru, &reaplist);
3919 spin_unlock(&state_lock);
3920 while (!list_empty(&reaplist)) {
3921 dp = list_first_entry(&reaplist, struct nfs4_delegation,
3923 list_del_init(&dp->dl_recall_lru);
3924 revoke_delegation(dp);
3926 list_for_each_safe(pos, next, &nn->close_lru) {
3927 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
3928 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
3929 t = oo->oo_time - cutoff;
3930 new_timeo = min(new_timeo, t);
3933 release_openowner(oo);
3935 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
3936 nfs4_unlock_state();
3940 static struct workqueue_struct *laundry_wq;
3941 static void laundromat_main(struct work_struct *);
3944 laundromat_main(struct work_struct *laundry)
3947 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
3949 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
3952 t = nfs4_laundromat(nn);
3953 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
3954 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
3957 static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
3959 if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_stid.sc_file->fi_fhandle))
3960 return nfserr_bad_stateid;
3965 access_permit_read(struct nfs4_ol_stateid *stp)
3967 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
3968 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
3969 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
3973 access_permit_write(struct nfs4_ol_stateid *stp)
3975 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
3976 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
3980 __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
3982 __be32 status = nfserr_openmode;
3984 /* For lock stateid's, we test the parent open, not the lock: */
3985 if (stp->st_openstp)
3986 stp = stp->st_openstp;
3987 if ((flags & WR_STATE) && !access_permit_write(stp))
3989 if ((flags & RD_STATE) && !access_permit_read(stp))
3996 static inline __be32
3997 check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
3999 if (ONE_STATEID(stateid) && (flags & RD_STATE))
4001 else if (locks_in_grace(net)) {
4002 /* Answer in remaining cases depends on existence of
4003 * conflicting state; so we must wait out the grace period. */
4004 return nfserr_grace;
4005 } else if (flags & WR_STATE)
4006 return nfs4_share_conflict(current_fh,
4007 NFS4_SHARE_DENY_WRITE);
4008 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4009 return nfs4_share_conflict(current_fh,
4010 NFS4_SHARE_DENY_READ);
4014 * Allow READ/WRITE during grace period on recovered state only for files
4015 * that are not able to provide mandatory locking.
4018 grace_disallows_io(struct net *net, struct inode *inode)
4020 return locks_in_grace(net) && mandatory_lock(inode);
4023 /* Returns true iff a is later than b: */
4024 static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4026 return (s32)(a->si_generation - b->si_generation) > 0;
4029 static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
4032 * When sessions are used the stateid generation number is ignored
4035 if (has_session && in->si_generation == 0)
4038 if (in->si_generation == ref->si_generation)
4041 /* If the client sends us a stateid from the future, it's buggy: */
4042 if (stateid_generation_after(in, ref))
4043 return nfserr_bad_stateid;
4045 * However, we could see a stateid from the past, even from a
4046 * non-buggy client. For example, if the client sends a lock
4047 * while some IO is outstanding, the lock may bump si_generation
4048 * while the IO is still in flight. The client could avoid that
4049 * situation by waiting for responses on all the IO requests,
4050 * but better performance may result in retrying IO that
4051 * receives an old_stateid error if requests are rarely
4052 * reordered in flight:
4054 return nfserr_old_stateid;
4057 static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
4059 struct nfs4_stid *s;
4060 struct nfs4_ol_stateid *ols;
4063 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4064 return nfserr_bad_stateid;
4065 /* Client debugging aid. */
4066 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4067 char addr_str[INET6_ADDRSTRLEN];
4068 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4070 pr_warn_ratelimited("NFSD: client %s testing state ID "
4071 "with incorrect client ID\n", addr_str);
4072 return nfserr_bad_stateid;
4074 s = find_stateid(cl, stateid);
4076 return nfserr_bad_stateid;
4077 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
4080 switch (s->sc_type) {
4081 case NFS4_DELEG_STID:
4083 case NFS4_REVOKED_DELEG_STID:
4084 return nfserr_deleg_revoked;
4085 case NFS4_OPEN_STID:
4086 case NFS4_LOCK_STID:
4087 ols = openlockstateid(s);
4088 if (ols->st_stateowner->so_is_open_owner
4089 && !(openowner(ols->st_stateowner)->oo_flags
4090 & NFS4_OO_CONFIRMED))
4091 return nfserr_bad_stateid;
4094 printk("unknown stateid type %x\n", s->sc_type);
4096 case NFS4_CLOSED_STID:
4097 case NFS4_CLOSED_DELEG_STID:
4098 return nfserr_bad_stateid;
4103 nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4104 stateid_t *stateid, unsigned char typemask,
4105 struct nfs4_stid **s, struct nfsd_net *nn)
4109 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4110 return nfserr_bad_stateid;
4111 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
4112 if (status == nfserr_stale_clientid) {
4113 if (cstate->session)
4114 return nfserr_bad_stateid;
4115 return nfserr_stale_stateid;
4119 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
4121 return nfserr_bad_stateid;
4126 * Checks for stateid operations
4129 nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
4130 stateid_t *stateid, int flags, struct file **filpp)
4132 struct nfs4_stid *s;
4133 struct nfs4_ol_stateid *stp = NULL;
4134 struct nfs4_delegation *dp = NULL;
4135 struct svc_fh *current_fh = &cstate->current_fh;
4136 struct inode *ino = current_fh->fh_dentry->d_inode;
4137 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4138 struct file *file = NULL;
4144 if (grace_disallows_io(net, ino))
4145 return nfserr_grace;
4147 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4148 return check_special_stateids(net, current_fh, stateid, flags);
4152 status = nfsd4_lookup_stateid(cstate, stateid,
4153 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
4157 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4160 switch (s->sc_type) {
4161 case NFS4_DELEG_STID:
4162 dp = delegstateid(s);
4163 status = nfs4_check_delegmode(dp, flags);
4167 file = dp->dl_stid.sc_file->fi_deleg_file;
4170 status = nfserr_serverfault;
4176 case NFS4_OPEN_STID:
4177 case NFS4_LOCK_STID:
4178 stp = openlockstateid(s);
4179 status = nfs4_check_fh(current_fh, stp);
4182 if (stp->st_stateowner->so_is_open_owner
4183 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
4185 status = nfs4_check_openmode(stp, flags);
4189 struct nfs4_file *fp = stp->st_stid.sc_file;
4191 if (flags & RD_STATE)
4192 file = find_readable_file(fp);
4194 file = find_writeable_file(fp);
4198 status = nfserr_bad_stateid;
4205 nfs4_unlock_state();
4210 nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
4212 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
4214 if (check_for_locks(stp->st_stid.sc_file, lo))
4215 return nfserr_locks_held;
4216 release_lockowner_if_empty(lo);
4221 * Test if the stateid is valid
4224 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4225 struct nfsd4_test_stateid *test_stateid)
4227 struct nfsd4_test_stateid_id *stateid;
4228 struct nfs4_client *cl = cstate->session->se_client;
4231 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
4232 stateid->ts_id_status =
4233 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
4234 nfs4_unlock_state();
4240 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4241 struct nfsd4_free_stateid *free_stateid)
4243 stateid_t *stateid = &free_stateid->fr_stateid;
4244 struct nfs4_stid *s;
4245 struct nfs4_delegation *dp;
4246 struct nfs4_client *cl = cstate->session->se_client;
4247 __be32 ret = nfserr_bad_stateid;
4250 s = find_stateid(cl, stateid);
4253 switch (s->sc_type) {
4254 case NFS4_DELEG_STID:
4255 ret = nfserr_locks_held;
4257 case NFS4_OPEN_STID:
4258 case NFS4_LOCK_STID:
4259 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4262 if (s->sc_type == NFS4_LOCK_STID)
4263 ret = nfsd4_free_lock_stateid(openlockstateid(s));
4265 ret = nfserr_locks_held;
4267 case NFS4_REVOKED_DELEG_STID:
4268 dp = delegstateid(s);
4269 spin_lock(&cl->cl_lock);
4270 list_del_init(&dp->dl_recall_lru);
4271 spin_unlock(&cl->cl_lock);
4276 ret = nfserr_bad_stateid;
4279 nfs4_unlock_state();
4286 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4287 RD_STATE : WR_STATE;
4290 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
4292 struct svc_fh *current_fh = &cstate->current_fh;
4293 struct nfs4_stateowner *sop = stp->st_stateowner;
4296 status = nfsd4_check_seqid(cstate, sop, seqid);
4299 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4300 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
4302 * "Closed" stateid's exist *only* to return
4303 * nfserr_replay_me from the previous step, and
4304 * revoked delegations are kept only for free_stateid.
4306 return nfserr_bad_stateid;
4307 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4310 return nfs4_check_fh(current_fh, stp);
4314 * Checks for sequence id mutating operations.
4317 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4318 stateid_t *stateid, char typemask,
4319 struct nfs4_ol_stateid **stpp,
4320 struct nfsd_net *nn)
4323 struct nfs4_stid *s;
4324 struct nfs4_ol_stateid *stp = NULL;
4326 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4327 seqid, STATEID_VAL(stateid));
4330 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
4333 stp = openlockstateid(s);
4334 if (!nfsd4_has_session(cstate))
4335 cstate->replay_owner = stp->st_stateowner;
4337 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
4343 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4344 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
4347 struct nfs4_openowner *oo;
4349 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
4350 NFS4_OPEN_STID, stpp, nn);
4353 oo = openowner((*stpp)->st_stateowner);
4354 if (!(oo->oo_flags & NFS4_OO_CONFIRMED))
4355 return nfserr_bad_stateid;
4360 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4361 struct nfsd4_open_confirm *oc)
4364 struct nfs4_openowner *oo;
4365 struct nfs4_ol_stateid *stp;
4366 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4368 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4369 cstate->current_fh.fh_dentry);
4371 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
4377 status = nfs4_preprocess_seqid_op(cstate,
4378 oc->oc_seqid, &oc->oc_req_stateid,
4379 NFS4_OPEN_STID, &stp, nn);
4382 oo = openowner(stp->st_stateowner);
4383 status = nfserr_bad_stateid;
4384 if (oo->oo_flags & NFS4_OO_CONFIRMED)
4386 oo->oo_flags |= NFS4_OO_CONFIRMED;
4387 update_stateid(&stp->st_stid.sc_stateid);
4388 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4389 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
4390 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
4392 nfsd4_client_record_create(oo->oo_owner.so_client);
4395 nfsd4_bump_seqid(cstate, status);
4396 if (!cstate->replay_owner)
4397 nfs4_unlock_state();
4401 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
4403 if (!test_access(access, stp))
4405 nfs4_file_put_access(stp->st_stid.sc_file, access);
4406 clear_access(access, stp);
4409 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4411 switch (to_access) {
4412 case NFS4_SHARE_ACCESS_READ:
4413 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4414 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4416 case NFS4_SHARE_ACCESS_WRITE:
4417 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4418 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4420 case NFS4_SHARE_ACCESS_BOTH:
4428 nfsd4_open_downgrade(struct svc_rqst *rqstp,
4429 struct nfsd4_compound_state *cstate,
4430 struct nfsd4_open_downgrade *od)
4433 struct nfs4_ol_stateid *stp;
4434 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4436 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4437 cstate->current_fh.fh_dentry);
4439 /* We don't yet support WANT bits: */
4440 if (od->od_deleg_want)
4441 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4445 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
4446 &od->od_stateid, &stp, nn);
4449 status = nfserr_inval;
4450 if (!test_access(od->od_share_access, stp)) {
4451 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
4452 stp->st_access_bmap, od->od_share_access);
4455 if (!test_deny(od->od_share_deny, stp)) {
4456 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
4457 stp->st_deny_bmap, od->od_share_deny);
4460 nfs4_stateid_downgrade(stp, od->od_share_access);
4462 reset_union_bmap_deny(od->od_share_deny, stp);
4464 update_stateid(&stp->st_stid.sc_stateid);
4465 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4468 nfsd4_bump_seqid(cstate, status);
4469 if (!cstate->replay_owner)
4470 nfs4_unlock_state();
4474 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4476 struct nfs4_client *clp = s->st_stid.sc_client;
4477 struct nfs4_openowner *oo = openowner(s->st_stateowner);
4479 s->st_stid.sc_type = NFS4_CLOSED_STID;
4480 unhash_open_stateid(s);
4482 if (clp->cl_minorversion) {
4483 if (list_empty(&oo->oo_owner.so_stateids))
4484 release_openowner(oo);
4485 nfs4_put_stid(&s->st_stid);
4488 * In the 4.0 case we need to keep the owners around a
4489 * little while to handle CLOSE replay. We still do need
4490 * to release any file access that is held by them
4491 * before returning however.
4493 release_all_access(s);
4494 if (s->st_stid.sc_file) {
4495 put_nfs4_file(s->st_stid.sc_file);
4496 s->st_stid.sc_file = NULL;
4498 oo->oo_last_closed_stid = s;
4499 if (list_empty(&oo->oo_owner.so_stateids))
4500 move_to_close_lru(oo, clp->net);
4505 * nfs4_unlock_state() called after encode
4508 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4509 struct nfsd4_close *close)
4512 struct nfs4_ol_stateid *stp;
4513 struct net *net = SVC_NET(rqstp);
4514 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4516 dprintk("NFSD: nfsd4_close on file %pd\n",
4517 cstate->current_fh.fh_dentry);
4520 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4522 NFS4_OPEN_STID|NFS4_CLOSED_STID,
4524 nfsd4_bump_seqid(cstate, status);
4527 update_stateid(&stp->st_stid.sc_stateid);
4528 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4530 nfsd4_close_open_stateid(stp);
4532 if (!cstate->replay_owner)
4533 nfs4_unlock_state();
4538 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4539 struct nfsd4_delegreturn *dr)
4541 struct nfs4_delegation *dp;
4542 stateid_t *stateid = &dr->dr_stateid;
4543 struct nfs4_stid *s;
4545 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4547 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
4551 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
4554 dp = delegstateid(s);
4555 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
4559 destroy_delegation(dp);
4561 nfs4_unlock_state();
4567 #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
4570 end_offset(u64 start, u64 len)
4575 return end >= start ? end: NFS4_MAX_UINT64;
4578 /* last octet in a range */
4580 last_byte_offset(u64 start, u64 len)
4586 return end > start ? end - 1: NFS4_MAX_UINT64;
4590 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4591 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4592 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4593 * locking, this prevents us from being completely protocol-compliant. The
4594 * real solution to this problem is to start using unsigned file offsets in
4595 * the VFS, but this is a very deep change!
4598 nfs4_transform_lock_offset(struct file_lock *lock)
4600 if (lock->fl_start < 0)
4601 lock->fl_start = OFFSET_MAX;
4602 if (lock->fl_end < 0)
4603 lock->fl_end = OFFSET_MAX;
4606 /* Hack!: For now, we're defining this just so we can use a pointer to it
4607 * as a unique cookie to identify our (NFSv4's) posix locks. */
4608 static const struct lock_manager_operations nfsd_posix_mng_ops = {
4612 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4614 struct nfs4_lockowner *lo;
4616 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
4617 lo = (struct nfs4_lockowner *) fl->fl_owner;
4618 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4619 lo->lo_owner.so_owner.len, GFP_KERNEL);
4620 if (!deny->ld_owner.data)
4621 /* We just don't care that much */
4623 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4624 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
4627 deny->ld_owner.len = 0;
4628 deny->ld_owner.data = NULL;
4629 deny->ld_clientid.cl_boot = 0;
4630 deny->ld_clientid.cl_id = 0;
4632 deny->ld_start = fl->fl_start;
4633 deny->ld_length = NFS4_MAX_UINT64;
4634 if (fl->fl_end != NFS4_MAX_UINT64)
4635 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4636 deny->ld_type = NFS4_READ_LT;
4637 if (fl->fl_type != F_RDLCK)
4638 deny->ld_type = NFS4_WRITE_LT;
4641 static struct nfs4_lockowner *
4642 find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
4643 struct nfsd_net *nn)
4645 unsigned int strhashval = ownerstr_hashval(clid->cl_id, owner);
4646 struct nfs4_stateowner *so;
4648 list_for_each_entry(so, &nn->ownerstr_hashtbl[strhashval], so_strhash) {
4649 if (so->so_is_open_owner)
4651 if (!same_owner_str(so, owner, clid))
4653 return lockowner(so);
4659 * Alloc a lock owner structure.
4660 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
4663 * strhashval = ownerstr_hashval
4665 static struct nfs4_lockowner *
4666 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_ol_stateid *open_stp, struct nfsd4_lock *lock) {
4667 struct nfs4_lockowner *lo;
4668 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
4670 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4673 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4674 lo->lo_owner.so_is_open_owner = 0;
4675 /* It is the openowner seqid that will be incremented in encode in the
4676 * case of new lockowners; so increment the lock seqid manually: */
4677 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1;
4678 list_add(&lo->lo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
4682 static struct nfs4_ol_stateid *
4683 alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp,
4684 struct inode *inode,
4685 struct nfs4_ol_stateid *open_stp)
4687 struct nfs4_ol_stateid *stp;
4688 struct nfs4_client *clp = lo->lo_owner.so_client;
4690 stp = nfs4_alloc_stateid(clp);
4693 stp->st_stid.sc_type = NFS4_LOCK_STID;
4694 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
4695 stp->st_stateowner = &lo->lo_owner;
4697 stp->st_stid.sc_file = fp;
4698 stp->st_access_bmap = 0;
4699 stp->st_deny_bmap = open_stp->st_deny_bmap;
4700 stp->st_openstp = open_stp;
4701 list_add(&stp->st_locks, &open_stp->st_locks);
4702 spin_lock(&fp->fi_lock);
4703 list_add(&stp->st_perfile, &fp->fi_stateids);
4704 spin_unlock(&fp->fi_lock);
4708 static struct nfs4_ol_stateid *
4709 find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
4711 struct nfs4_ol_stateid *lst;
4713 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
4714 if (lst->st_stid.sc_file == fp)
4722 check_lock_length(u64 offset, u64 length)
4724 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
4725 LOFF_OVERFLOW(offset, length)));
4728 static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
4730 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
4732 lockdep_assert_held(&fp->fi_lock);
4734 if (test_access(access, lock_stp))
4736 __nfs4_file_get_access(fp, access);
4737 set_access(access, lock_stp);
4740 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)
4742 struct nfs4_file *fi = ost->st_stid.sc_file;
4743 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
4744 struct nfs4_client *cl = oo->oo_owner.so_client;
4745 struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
4746 struct nfs4_lockowner *lo;
4747 unsigned int strhashval;
4748 struct nfsd_net *nn = net_generic(cl->net, nfsd_net_id);
4750 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, nn);
4752 strhashval = ownerstr_hashval(cl->cl_clientid.cl_id,
4753 &lock->v.new.owner);
4754 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
4756 return nfserr_jukebox;
4758 /* with an existing lockowner, seqids must be the same */
4759 if (!cstate->minorversion &&
4760 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
4761 return nfserr_bad_seqid;
4764 *lst = find_lock_stateid(lo, fi);
4766 *lst = alloc_init_lock_stateid(lo, fi, inode, ost);
4768 release_lockowner_if_empty(lo);
4769 return nfserr_jukebox;
4780 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4781 struct nfsd4_lock *lock)
4783 struct nfs4_openowner *open_sop = NULL;
4784 struct nfs4_lockowner *lock_sop = NULL;
4785 struct nfs4_ol_stateid *lock_stp;
4786 struct nfs4_file *fp;
4787 struct file *filp = NULL;
4788 struct file_lock *file_lock = NULL;
4789 struct file_lock *conflock = NULL;
4791 bool new_state = false;
4794 struct net *net = SVC_NET(rqstp);
4795 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4797 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
4798 (long long) lock->lk_offset,
4799 (long long) lock->lk_length);
4801 if (check_lock_length(lock->lk_offset, lock->lk_length))
4802 return nfserr_inval;
4804 if ((status = fh_verify(rqstp, &cstate->current_fh,
4805 S_IFREG, NFSD_MAY_LOCK))) {
4806 dprintk("NFSD: nfsd4_lock: permission denied!\n");
4812 if (lock->lk_is_new) {
4813 struct nfs4_ol_stateid *open_stp = NULL;
4815 if (nfsd4_has_session(cstate))
4816 /* See rfc 5661 18.10.3: given clientid is ignored: */
4817 memcpy(&lock->v.new.clientid,
4818 &cstate->session->se_client->cl_clientid,
4819 sizeof(clientid_t));
4821 status = nfserr_stale_clientid;
4822 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
4825 /* validate and update open stateid and open seqid */
4826 status = nfs4_preprocess_confirmed_seqid_op(cstate,
4827 lock->lk_new_open_seqid,
4828 &lock->lk_new_open_stateid,
4832 open_sop = openowner(open_stp->st_stateowner);
4833 status = nfserr_bad_stateid;
4834 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
4835 &lock->v.new.clientid))
4837 status = lookup_or_create_lock_state(cstate, open_stp, lock,
4838 &lock_stp, &new_state);
4840 status = nfs4_preprocess_seqid_op(cstate,
4841 lock->lk_old_lock_seqid,
4842 &lock->lk_old_lock_stateid,
4843 NFS4_LOCK_STID, &lock_stp, nn);
4846 lock_sop = lockowner(lock_stp->st_stateowner);
4848 lkflg = setlkflg(lock->lk_type);
4849 status = nfs4_check_openmode(lock_stp, lkflg);
4853 status = nfserr_grace;
4854 if (locks_in_grace(net) && !lock->lk_reclaim)
4856 status = nfserr_no_grace;
4857 if (!locks_in_grace(net) && lock->lk_reclaim)
4860 file_lock = locks_alloc_lock();
4862 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4863 status = nfserr_jukebox;
4867 fp = lock_stp->st_stid.sc_file;
4868 locks_init_lock(file_lock);
4869 switch (lock->lk_type) {
4872 spin_lock(&fp->fi_lock);
4873 filp = find_readable_file_locked(fp);
4875 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
4876 spin_unlock(&fp->fi_lock);
4877 file_lock->fl_type = F_RDLCK;
4880 case NFS4_WRITEW_LT:
4881 spin_lock(&fp->fi_lock);
4882 filp = find_writeable_file_locked(fp);
4884 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
4885 spin_unlock(&fp->fi_lock);
4886 file_lock->fl_type = F_WRLCK;
4889 status = nfserr_inval;
4893 status = nfserr_openmode;
4896 file_lock->fl_owner = (fl_owner_t)lock_sop;
4897 file_lock->fl_pid = current->tgid;
4898 file_lock->fl_file = filp;
4899 file_lock->fl_flags = FL_POSIX;
4900 file_lock->fl_lmops = &nfsd_posix_mng_ops;
4901 file_lock->fl_start = lock->lk_offset;
4902 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
4903 nfs4_transform_lock_offset(file_lock);
4905 conflock = locks_alloc_lock();
4907 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4908 status = nfserr_jukebox;
4912 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
4914 case 0: /* success! */
4915 update_stateid(&lock_stp->st_stid.sc_stateid);
4916 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
4920 case (EAGAIN): /* conflock holds conflicting lock */
4921 status = nfserr_denied;
4922 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
4923 nfs4_set_lock_denied(conflock, &lock->lk_denied);
4926 status = nfserr_deadlock;
4929 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
4930 status = nfserrno(err);
4936 if (status && new_state)
4937 release_lock_stateid(lock_stp);
4938 nfsd4_bump_seqid(cstate, status);
4939 if (!cstate->replay_owner)
4940 nfs4_unlock_state();
4942 locks_free_lock(file_lock);
4944 locks_free_lock(conflock);
4949 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
4950 * so we do a temporary open here just to get an open file to pass to
4951 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
4954 static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
4957 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
4959 err = nfserrno(vfs_test_lock(file, lock));
4969 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4970 struct nfsd4_lockt *lockt)
4972 struct file_lock *file_lock = NULL;
4973 struct nfs4_lockowner *lo;
4975 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4977 if (locks_in_grace(SVC_NET(rqstp)))
4978 return nfserr_grace;
4980 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
4981 return nfserr_inval;
4985 if (!nfsd4_has_session(cstate)) {
4986 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
4991 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
4994 file_lock = locks_alloc_lock();
4996 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4997 status = nfserr_jukebox;
5000 locks_init_lock(file_lock);
5001 switch (lockt->lt_type) {
5004 file_lock->fl_type = F_RDLCK;
5007 case NFS4_WRITEW_LT:
5008 file_lock->fl_type = F_WRLCK;
5011 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
5012 status = nfserr_inval;
5016 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner, nn);
5018 file_lock->fl_owner = (fl_owner_t)lo;
5019 file_lock->fl_pid = current->tgid;
5020 file_lock->fl_flags = FL_POSIX;
5022 file_lock->fl_start = lockt->lt_offset;
5023 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
5025 nfs4_transform_lock_offset(file_lock);
5027 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
5031 if (file_lock->fl_type != F_UNLCK) {
5032 status = nfserr_denied;
5033 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
5036 nfs4_unlock_state();
5038 locks_free_lock(file_lock);
5043 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5044 struct nfsd4_locku *locku)
5046 struct nfs4_ol_stateid *stp;
5047 struct file *filp = NULL;
5048 struct file_lock *file_lock = NULL;
5051 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5053 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5054 (long long) locku->lu_offset,
5055 (long long) locku->lu_length);
5057 if (check_lock_length(locku->lu_offset, locku->lu_length))
5058 return nfserr_inval;
5062 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
5063 &locku->lu_stateid, NFS4_LOCK_STID,
5067 filp = find_any_file(stp->st_stid.sc_file);
5069 status = nfserr_lock_range;
5072 file_lock = locks_alloc_lock();
5074 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5075 status = nfserr_jukebox;
5078 locks_init_lock(file_lock);
5079 file_lock->fl_type = F_UNLCK;
5080 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
5081 file_lock->fl_pid = current->tgid;
5082 file_lock->fl_file = filp;
5083 file_lock->fl_flags = FL_POSIX;
5084 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5085 file_lock->fl_start = locku->lu_offset;
5087 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5089 nfs4_transform_lock_offset(file_lock);
5091 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
5093 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
5096 update_stateid(&stp->st_stid.sc_stateid);
5097 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
5101 nfsd4_bump_seqid(cstate, status);
5102 if (!cstate->replay_owner)
5103 nfs4_unlock_state();
5105 locks_free_lock(file_lock);
5109 status = nfserrno(err);
5115 * true: locks held by lockowner
5116 * false: no locks held by lockowner
5119 check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
5121 struct file_lock **flpp;
5123 struct file *filp = find_any_file(fp);
5124 struct inode *inode;
5127 /* Any valid lock stateid should have some sort of access */
5132 inode = file_inode(filp);
5134 spin_lock(&inode->i_lock);
5135 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
5136 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
5141 spin_unlock(&inode->i_lock);
5147 nfsd4_release_lockowner(struct svc_rqst *rqstp,
5148 struct nfsd4_compound_state *cstate,
5149 struct nfsd4_release_lockowner *rlockowner)
5151 clientid_t *clid = &rlockowner->rl_clientid;
5152 struct nfs4_stateowner *sop = NULL, *tmp;
5153 struct nfs4_lockowner *lo;
5154 struct nfs4_ol_stateid *stp;
5155 struct xdr_netobj *owner = &rlockowner->rl_owner;
5156 unsigned int hashval = ownerstr_hashval(clid->cl_id, owner);
5158 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5160 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5161 clid->cl_boot, clid->cl_id);
5165 status = lookup_clientid(clid, cstate, nn);
5169 status = nfserr_locks_held;
5171 /* Find the matching lock stateowner */
5172 list_for_each_entry(tmp, &nn->ownerstr_hashtbl[hashval], so_strhash) {
5173 if (tmp->so_is_open_owner)
5175 if (same_owner_str(tmp, owner, clid)) {
5181 /* No matching owner found, maybe a replay? Just declare victory... */
5187 lo = lockowner(sop);
5188 /* see if there are still any locks associated with it */
5189 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5190 if (check_for_locks(stp->st_stid.sc_file, lo))
5195 release_lockowner(lo);
5197 nfs4_unlock_state();
5201 static inline struct nfs4_client_reclaim *
5204 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
5208 nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
5210 struct nfs4_client_reclaim *crp;
5212 crp = nfsd4_find_reclaim_client(name, nn);
5213 return (crp && crp->cr_clp);
5217 * failure => all reset bets are off, nfserr_no_grace...
5219 struct nfs4_client_reclaim *
5220 nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
5222 unsigned int strhashval;
5223 struct nfs4_client_reclaim *crp;
5225 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5226 crp = alloc_reclaim();
5228 strhashval = clientstr_hashval(name);
5229 INIT_LIST_HEAD(&crp->cr_strhash);
5230 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
5231 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
5233 nn->reclaim_str_hashtbl_size++;
5239 nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
5241 list_del(&crp->cr_strhash);
5243 nn->reclaim_str_hashtbl_size--;
5247 nfs4_release_reclaim(struct nfsd_net *nn)
5249 struct nfs4_client_reclaim *crp = NULL;
5252 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5253 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5254 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
5255 struct nfs4_client_reclaim, cr_strhash);
5256 nfs4_remove_reclaim_record(crp, nn);
5259 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
5263 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
5264 struct nfs4_client_reclaim *
5265 nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
5267 unsigned int strhashval;
5268 struct nfs4_client_reclaim *crp = NULL;
5270 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
5272 strhashval = clientstr_hashval(recdir);
5273 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
5274 if (same_name(crp->cr_recdir, recdir)) {
5282 * Called from OPEN. Look for clientid in reclaim list.
5285 nfs4_check_open_reclaim(clientid_t *clid,
5286 struct nfsd4_compound_state *cstate,
5287 struct nfsd_net *nn)
5291 /* find clientid in conf_id_hashtbl */
5292 status = lookup_clientid(clid, cstate, nn);
5294 return nfserr_reclaim_bad;
5296 if (nfsd4_client_record_check(cstate->clp))
5297 return nfserr_reclaim_bad;
5302 #ifdef CONFIG_NFSD_FAULT_INJECTION
5304 u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
5306 if (mark_client_expired(clp))
5312 u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
5314 char buf[INET6_ADDRSTRLEN];
5315 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5316 printk(KERN_INFO "NFS Client: %s\n", buf);
5320 static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5323 char buf[INET6_ADDRSTRLEN];
5324 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5325 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5328 static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
5329 void (*func)(struct nfs4_ol_stateid *))
5331 struct nfs4_openowner *oop;
5332 struct nfs4_ol_stateid *stp, *st_next;
5333 struct nfs4_ol_stateid *lst, *lst_next;
5336 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
5337 list_for_each_entry_safe(stp, st_next,
5338 &oop->oo_owner.so_stateids, st_perstateowner) {
5339 list_for_each_entry_safe(lst, lst_next,
5340 &stp->st_locks, st_locks) {
5352 u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
5354 return nfsd_foreach_client_lock(clp, max, release_lock_stateid);
5357 u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
5359 u64 count = nfsd_foreach_client_lock(clp, max, NULL);
5360 nfsd_print_count(clp, count, "locked files");
5364 static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
5366 struct nfs4_openowner *oop, *next;
5369 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
5379 u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
5381 return nfsd_foreach_client_open(clp, max, release_openowner);
5384 u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
5386 u64 count = nfsd_foreach_client_open(clp, max, NULL);
5387 nfsd_print_count(clp, count, "open files");
5391 static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
5392 struct list_head *victims)
5394 struct nfs4_delegation *dp, *next;
5397 lockdep_assert_held(&state_lock);
5398 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
5401 * It's not safe to mess with delegations that have a
5402 * non-zero dl_time. They might have already been broken
5403 * and could be processed by the laundromat outside of
5404 * the state_lock. Just leave them be.
5406 if (dp->dl_time != 0)
5409 unhash_delegation_locked(dp);
5410 list_add(&dp->dl_recall_lru, victims);
5418 u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
5420 struct nfs4_delegation *dp, *next;
5424 spin_lock(&state_lock);
5425 count = nfsd_find_all_delegations(clp, max, &victims);
5426 spin_unlock(&state_lock);
5428 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru) {
5429 list_del_init(&dp->dl_recall_lru);
5430 revoke_delegation(dp);
5436 u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
5438 struct nfs4_delegation *dp;
5442 spin_lock(&state_lock);
5443 count = nfsd_find_all_delegations(clp, max, &victims);
5444 while (!list_empty(&victims)) {
5445 dp = list_first_entry(&victims, struct nfs4_delegation,
5447 list_del_init(&dp->dl_recall_lru);
5449 nfsd_break_one_deleg(dp);
5451 spin_unlock(&state_lock);
5456 u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
5460 spin_lock(&state_lock);
5461 count = nfsd_find_all_delegations(clp, max, NULL);
5462 spin_unlock(&state_lock);
5464 nfsd_print_count(clp, count, "delegations");
5468 u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
5470 struct nfs4_client *clp, *next;
5472 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
5474 if (!nfsd_netns_ready(nn))
5477 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
5478 count += func(clp, max - count);
5479 if ((max != 0) && (count >= max))
5486 struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5488 struct nfs4_client *clp;
5489 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
5491 if (!nfsd_netns_ready(nn))
5494 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5495 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5501 #endif /* CONFIG_NFSD_FAULT_INJECTION */
5504 * Since the lifetime of a delegation isn't limited to that of an open, a
5505 * client may quite reasonably hang on to a delegation as long as it has
5506 * the inode cached. This becomes an obvious problem the first time a
5507 * client's inode cache approaches the size of the server's total memory.
5509 * For now we avoid this problem by imposing a hard limit on the number
5510 * of delegations, which varies according to the server's memory size.
5513 set_max_delegations(void)
5516 * Allow at most 4 delegations per megabyte of RAM. Quick
5517 * estimates suggest that in the worst case (where every delegation
5518 * is for a different inode), a delegation could take about 1.5K,
5519 * giving a worst case usage of about 6% of memory.
5521 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
5524 static int nfs4_state_create_net(struct net *net)
5526 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5529 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5530 CLIENT_HASH_SIZE, GFP_KERNEL);
5531 if (!nn->conf_id_hashtbl)
5533 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5534 CLIENT_HASH_SIZE, GFP_KERNEL);
5535 if (!nn->unconf_id_hashtbl)
5537 nn->ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
5538 OWNER_HASH_SIZE, GFP_KERNEL);
5539 if (!nn->ownerstr_hashtbl)
5541 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
5542 SESSION_HASH_SIZE, GFP_KERNEL);
5543 if (!nn->sessionid_hashtbl)
5546 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5547 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
5548 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
5550 for (i = 0; i < OWNER_HASH_SIZE; i++)
5551 INIT_LIST_HEAD(&nn->ownerstr_hashtbl[i]);
5552 for (i = 0; i < SESSION_HASH_SIZE; i++)
5553 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
5554 nn->conf_name_tree = RB_ROOT;
5555 nn->unconf_name_tree = RB_ROOT;
5556 INIT_LIST_HEAD(&nn->client_lru);
5557 INIT_LIST_HEAD(&nn->close_lru);
5558 INIT_LIST_HEAD(&nn->del_recall_lru);
5559 spin_lock_init(&nn->client_lock);
5561 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
5567 kfree(nn->ownerstr_hashtbl);
5569 kfree(nn->unconf_id_hashtbl);
5571 kfree(nn->conf_id_hashtbl);
5577 nfs4_state_destroy_net(struct net *net)
5580 struct nfs4_client *clp = NULL;
5581 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5583 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5584 while (!list_empty(&nn->conf_id_hashtbl[i])) {
5585 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5586 destroy_client(clp);
5590 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5591 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
5592 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5593 destroy_client(clp);
5597 kfree(nn->sessionid_hashtbl);
5598 kfree(nn->ownerstr_hashtbl);
5599 kfree(nn->unconf_id_hashtbl);
5600 kfree(nn->conf_id_hashtbl);
5605 nfs4_state_start_net(struct net *net)
5607 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5610 ret = nfs4_state_create_net(net);
5613 nfsd4_client_tracking_init(net);
5614 nn->boot_time = get_seconds();
5615 locks_start_grace(net, &nn->nfsd4_manager);
5616 nn->grace_ended = false;
5617 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
5618 nn->nfsd4_grace, net);
5619 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
5623 /* initialization to perform when the nfsd service is started: */
5626 nfs4_state_start(void)
5630 ret = set_callback_cred();
5633 laundry_wq = create_singlethread_workqueue("nfsd4");
5634 if (laundry_wq == NULL) {
5638 ret = nfsd4_create_callback_queue();
5640 goto out_free_laundry;
5642 set_max_delegations();
5647 destroy_workqueue(laundry_wq);
5653 nfs4_state_shutdown_net(struct net *net)
5655 struct nfs4_delegation *dp = NULL;
5656 struct list_head *pos, *next, reaplist;
5657 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5659 cancel_delayed_work_sync(&nn->laundromat_work);
5660 locks_end_grace(&nn->nfsd4_manager);
5663 INIT_LIST_HEAD(&reaplist);
5664 spin_lock(&state_lock);
5665 list_for_each_safe(pos, next, &nn->del_recall_lru) {
5666 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
5667 unhash_delegation_locked(dp);
5668 list_add(&dp->dl_recall_lru, &reaplist);
5670 spin_unlock(&state_lock);
5671 list_for_each_safe(pos, next, &reaplist) {
5672 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
5673 list_del_init(&dp->dl_recall_lru);
5674 nfs4_put_stid(&dp->dl_stid);
5677 nfsd4_client_tracking_exit(net);
5678 nfs4_state_destroy_net(net);
5679 nfs4_unlock_state();
5683 nfs4_state_shutdown(void)
5685 destroy_workqueue(laundry_wq);
5686 nfsd4_destroy_callback_queue();
5690 get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5692 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
5693 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
5697 put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5699 if (cstate->minorversion) {
5700 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
5701 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
5706 clear_current_stateid(struct nfsd4_compound_state *cstate)
5708 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
5712 * functions to set current state id
5715 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5717 put_stateid(cstate, &odp->od_stateid);
5721 nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
5723 put_stateid(cstate, &open->op_stateid);
5727 nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5729 put_stateid(cstate, &close->cl_stateid);
5733 nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
5735 put_stateid(cstate, &lock->lk_resp_stateid);
5739 * functions to consume current state id
5743 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5745 get_stateid(cstate, &odp->od_stateid);
5749 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
5751 get_stateid(cstate, &drp->dr_stateid);
5755 nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
5757 get_stateid(cstate, &fsp->fr_stateid);
5761 nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
5763 get_stateid(cstate, &setattr->sa_stateid);
5767 nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5769 get_stateid(cstate, &close->cl_stateid);
5773 nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
5775 get_stateid(cstate, &locku->lu_stateid);
5779 nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
5781 get_stateid(cstate, &read->rd_stateid);
5785 nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
5787 get_stateid(cstate, &write->wr_stateid);