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 nfs4_stid *stid;
473 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
477 idr_preload(GFP_KERNEL);
478 spin_lock(&cl->cl_lock);
479 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 0, 0, GFP_NOWAIT);
480 spin_unlock(&cl->cl_lock);
484 stid->sc_client = cl;
485 stid->sc_stateid.si_opaque.so_id = new_id;
486 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
487 /* Will be incremented before return to client: */
488 atomic_set(&stid->sc_count, 1);
491 * It shouldn't be a problem to reuse an opaque stateid value.
492 * I don't think it is for 4.1. But with 4.0 I worry that, for
493 * example, a stray write retransmission could be accepted by
494 * the server when it should have been rejected. Therefore,
495 * adopt a trick from the sctp code to attempt to maximize the
496 * amount of time until an id is reused, by ensuring they always
497 * "increase" (mod INT_MAX):
501 kmem_cache_free(slab, stid);
505 static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
507 struct nfs4_stid *stid;
508 struct nfs4_ol_stateid *stp;
510 stid = nfs4_alloc_stid(clp, stateid_slab);
514 stp = openlockstateid(stid);
515 stp->st_stid.sc_free = nfs4_free_ol_stateid;
519 static void nfs4_free_deleg(struct nfs4_stid *stid)
521 kmem_cache_free(deleg_slab, stid);
522 atomic_long_dec(&num_delegations);
526 * When we recall a delegation, we should be careful not to hand it
527 * out again straight away.
528 * To ensure this we keep a pair of bloom filters ('new' and 'old')
529 * in which the filehandles of recalled delegations are "stored".
530 * If a filehandle appear in either filter, a delegation is blocked.
531 * When a delegation is recalled, the filehandle is stored in the "new"
533 * Every 30 seconds we swap the filters and clear the "new" one,
534 * unless both are empty of course.
536 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
537 * low 3 bytes as hash-table indices.
539 * 'blocked_delegations_lock', which is always taken in block_delegations(),
540 * is used to manage concurrent access. Testing does not need the lock
541 * except when swapping the two filters.
543 static DEFINE_SPINLOCK(blocked_delegations_lock);
544 static struct bloom_pair {
545 int entries, old_entries;
547 int new; /* index into 'set' */
548 DECLARE_BITMAP(set[2], 256);
549 } blocked_delegations;
551 static int delegation_blocked(struct knfsd_fh *fh)
554 struct bloom_pair *bd = &blocked_delegations;
556 if (bd->entries == 0)
558 if (seconds_since_boot() - bd->swap_time > 30) {
559 spin_lock(&blocked_delegations_lock);
560 if (seconds_since_boot() - bd->swap_time > 30) {
561 bd->entries -= bd->old_entries;
562 bd->old_entries = bd->entries;
563 memset(bd->set[bd->new], 0,
566 bd->swap_time = seconds_since_boot();
568 spin_unlock(&blocked_delegations_lock);
570 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
571 if (test_bit(hash&255, bd->set[0]) &&
572 test_bit((hash>>8)&255, bd->set[0]) &&
573 test_bit((hash>>16)&255, bd->set[0]))
576 if (test_bit(hash&255, bd->set[1]) &&
577 test_bit((hash>>8)&255, bd->set[1]) &&
578 test_bit((hash>>16)&255, bd->set[1]))
584 static void block_delegations(struct knfsd_fh *fh)
587 struct bloom_pair *bd = &blocked_delegations;
589 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
591 spin_lock(&blocked_delegations_lock);
592 __set_bit(hash&255, bd->set[bd->new]);
593 __set_bit((hash>>8)&255, bd->set[bd->new]);
594 __set_bit((hash>>16)&255, bd->set[bd->new]);
595 if (bd->entries == 0)
596 bd->swap_time = seconds_since_boot();
598 spin_unlock(&blocked_delegations_lock);
601 static struct nfs4_delegation *
602 alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh)
604 struct nfs4_delegation *dp;
607 dprintk("NFSD alloc_init_deleg\n");
608 n = atomic_long_inc_return(&num_delegations);
609 if (n < 0 || n > max_delegations)
611 if (delegation_blocked(¤t_fh->fh_handle))
613 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
617 dp->dl_stid.sc_free = nfs4_free_deleg;
619 * delegation seqid's are never incremented. The 4.1 special
620 * meaning of seqid 0 isn't meaningful, really, but let's avoid
621 * 0 anyway just for consistency and use 1:
623 dp->dl_stid.sc_stateid.si_generation = 1;
624 INIT_LIST_HEAD(&dp->dl_perfile);
625 INIT_LIST_HEAD(&dp->dl_perclnt);
626 INIT_LIST_HEAD(&dp->dl_recall_lru);
627 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
628 INIT_WORK(&dp->dl_recall.cb_work, nfsd4_run_cb_recall);
631 atomic_long_dec(&num_delegations);
636 nfs4_put_stid(struct nfs4_stid *s)
638 struct nfs4_file *fp = s->sc_file;
639 struct nfs4_client *clp = s->sc_client;
641 might_lock(&clp->cl_lock);
643 if (!atomic_dec_and_lock(&s->sc_count, &clp->cl_lock))
645 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
646 spin_unlock(&clp->cl_lock);
652 static void nfs4_put_deleg_lease(struct nfs4_file *fp)
654 lockdep_assert_held(&state_lock);
658 if (atomic_dec_and_test(&fp->fi_delegees)) {
659 vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
661 fput(fp->fi_deleg_file);
662 fp->fi_deleg_file = NULL;
666 static void unhash_stid(struct nfs4_stid *s)
672 hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
674 lockdep_assert_held(&state_lock);
675 lockdep_assert_held(&fp->fi_lock);
677 dp->dl_stid.sc_type = NFS4_DELEG_STID;
678 list_add(&dp->dl_perfile, &fp->fi_delegations);
679 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
683 unhash_delegation_locked(struct nfs4_delegation *dp)
685 struct nfs4_file *fp = dp->dl_stid.sc_file;
687 lockdep_assert_held(&state_lock);
689 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
690 /* Ensure that deleg break won't try to requeue it */
692 spin_lock(&fp->fi_lock);
693 list_del_init(&dp->dl_perclnt);
694 list_del_init(&dp->dl_recall_lru);
695 list_del_init(&dp->dl_perfile);
696 spin_unlock(&fp->fi_lock);
698 nfs4_put_deleg_lease(fp);
701 static void destroy_delegation(struct nfs4_delegation *dp)
703 spin_lock(&state_lock);
704 unhash_delegation_locked(dp);
705 spin_unlock(&state_lock);
706 nfs4_put_stid(&dp->dl_stid);
709 static void revoke_delegation(struct nfs4_delegation *dp)
711 struct nfs4_client *clp = dp->dl_stid.sc_client;
713 WARN_ON(!list_empty(&dp->dl_recall_lru));
715 if (clp->cl_minorversion == 0)
716 nfs4_put_stid(&dp->dl_stid);
718 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
719 spin_lock(&clp->cl_lock);
720 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
721 spin_unlock(&clp->cl_lock);
729 static unsigned int clientid_hashval(u32 id)
731 return id & CLIENT_HASH_MASK;
734 static unsigned int clientstr_hashval(const char *name)
736 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
740 * We store the NONE, READ, WRITE, and BOTH bits separately in the
741 * st_{access,deny}_bmap field of the stateid, in order to track not
742 * only what share bits are currently in force, but also what
743 * combinations of share bits previous opens have used. This allows us
744 * to enforce the recommendation of rfc 3530 14.2.19 that the server
745 * return an error if the client attempt to downgrade to a combination
746 * of share bits not explicable by closing some of its previous opens.
748 * XXX: This enforcement is actually incomplete, since we don't keep
749 * track of access/deny bit combinations; so, e.g., we allow:
751 * OPEN allow read, deny write
752 * OPEN allow both, deny none
753 * DOWNGRADE allow read, deny none
755 * which we should reject.
758 bmap_to_share_mode(unsigned long bmap) {
760 unsigned int access = 0;
762 for (i = 1; i < 4; i++) {
763 if (test_bit(i, &bmap))
769 /* set share access for a given stateid */
771 set_access(u32 access, struct nfs4_ol_stateid *stp)
773 unsigned char mask = 1 << access;
775 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
776 stp->st_access_bmap |= mask;
779 /* clear share access for a given stateid */
781 clear_access(u32 access, struct nfs4_ol_stateid *stp)
783 unsigned char mask = 1 << access;
785 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
786 stp->st_access_bmap &= ~mask;
789 /* test whether a given stateid has access */
791 test_access(u32 access, struct nfs4_ol_stateid *stp)
793 unsigned char mask = 1 << access;
795 return (bool)(stp->st_access_bmap & mask);
798 /* set share deny for a given stateid */
800 set_deny(u32 deny, struct nfs4_ol_stateid *stp)
802 unsigned char mask = 1 << deny;
804 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
805 stp->st_deny_bmap |= mask;
808 /* clear share deny for a given stateid */
810 clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
812 unsigned char mask = 1 << deny;
814 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
815 stp->st_deny_bmap &= ~mask;
818 /* test whether a given stateid is denying specific access */
820 test_deny(u32 deny, struct nfs4_ol_stateid *stp)
822 unsigned char mask = 1 << deny;
824 return (bool)(stp->st_deny_bmap & mask);
827 static int nfs4_access_to_omode(u32 access)
829 switch (access & NFS4_SHARE_ACCESS_BOTH) {
830 case NFS4_SHARE_ACCESS_READ:
832 case NFS4_SHARE_ACCESS_WRITE:
834 case NFS4_SHARE_ACCESS_BOTH:
842 * A stateid that had a deny mode associated with it is being released
843 * or downgraded. Recalculate the deny mode on the file.
846 recalculate_deny_mode(struct nfs4_file *fp)
848 struct nfs4_ol_stateid *stp;
850 spin_lock(&fp->fi_lock);
851 fp->fi_share_deny = 0;
852 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
853 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
854 spin_unlock(&fp->fi_lock);
858 reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
863 for (i = 1; i < 4; i++) {
864 if ((i & deny) != i) {
870 /* Recalculate per-file deny mode if there was a change */
872 recalculate_deny_mode(stp->st_stid.sc_file);
875 /* release all access and file references for a given stateid */
877 release_all_access(struct nfs4_ol_stateid *stp)
880 struct nfs4_file *fp = stp->st_stid.sc_file;
882 if (fp && stp->st_deny_bmap != 0)
883 recalculate_deny_mode(fp);
885 for (i = 1; i < 4; i++) {
886 if (test_access(i, stp))
887 nfs4_file_put_access(stp->st_stid.sc_file, i);
888 clear_access(i, stp);
892 static void unhash_generic_stateid(struct nfs4_ol_stateid *stp)
894 struct nfs4_file *fp = stp->st_stid.sc_file;
896 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
898 spin_lock(&fp->fi_lock);
899 list_del(&stp->st_perfile);
900 spin_unlock(&fp->fi_lock);
901 list_del(&stp->st_perstateowner);
904 static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
906 struct nfs4_ol_stateid *stp = openlockstateid(stid);
908 release_all_access(stp);
909 kmem_cache_free(stateid_slab, stid);
912 static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
914 struct nfs4_ol_stateid *stp = openlockstateid(stid);
915 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
918 file = find_any_file(stp->st_stid.sc_file);
920 filp_close(file, (fl_owner_t)lo);
921 nfs4_free_ol_stateid(stid);
924 static void __release_lock_stateid(struct nfs4_ol_stateid *stp)
926 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
928 spin_lock(&oo->oo_owner.so_client->cl_lock);
929 list_del(&stp->st_locks);
930 unhash_generic_stateid(stp);
931 unhash_stid(&stp->st_stid);
932 spin_unlock(&oo->oo_owner.so_client->cl_lock);
933 nfs4_put_stid(&stp->st_stid);
936 static void unhash_lockowner(struct nfs4_lockowner *lo)
938 struct nfs4_ol_stateid *stp;
940 list_del(&lo->lo_owner.so_strhash);
941 while (!list_empty(&lo->lo_owner.so_stateids)) {
942 stp = list_first_entry(&lo->lo_owner.so_stateids,
943 struct nfs4_ol_stateid, st_perstateowner);
944 __release_lock_stateid(stp);
948 static void nfs4_free_lockowner(struct nfs4_lockowner *lo)
950 kfree(lo->lo_owner.so_owner.data);
951 kmem_cache_free(lockowner_slab, lo);
954 static void release_lockowner(struct nfs4_lockowner *lo)
956 unhash_lockowner(lo);
957 nfs4_free_lockowner(lo);
960 static void release_lockowner_if_empty(struct nfs4_lockowner *lo)
962 if (list_empty(&lo->lo_owner.so_stateids))
963 release_lockowner(lo);
966 static void release_lock_stateid(struct nfs4_ol_stateid *stp)
968 struct nfs4_lockowner *lo;
970 lo = lockowner(stp->st_stateowner);
971 __release_lock_stateid(stp);
972 release_lockowner_if_empty(lo);
975 static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp)
976 __releases(&open_stp->st_stateowner->so_client->cl_lock)
977 __acquires(&open_stp->st_stateowner->so_client->cl_lock)
979 struct nfs4_ol_stateid *stp;
981 while (!list_empty(&open_stp->st_locks)) {
982 stp = list_entry(open_stp->st_locks.next,
983 struct nfs4_ol_stateid, st_locks);
984 spin_unlock(&open_stp->st_stateowner->so_client->cl_lock);
985 release_lock_stateid(stp);
986 spin_lock(&open_stp->st_stateowner->so_client->cl_lock);
990 static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
992 spin_lock(&stp->st_stateowner->so_client->cl_lock);
993 unhash_generic_stateid(stp);
994 release_open_stateid_locks(stp);
995 spin_unlock(&stp->st_stateowner->so_client->cl_lock);
998 static void release_open_stateid(struct nfs4_ol_stateid *stp)
1000 unhash_open_stateid(stp);
1001 nfs4_put_stid(&stp->st_stid);
1004 static void unhash_openowner(struct nfs4_openowner *oo)
1006 struct nfs4_ol_stateid *stp;
1008 list_del(&oo->oo_owner.so_strhash);
1009 list_del(&oo->oo_perclient);
1010 while (!list_empty(&oo->oo_owner.so_stateids)) {
1011 stp = list_first_entry(&oo->oo_owner.so_stateids,
1012 struct nfs4_ol_stateid, st_perstateowner);
1013 release_open_stateid(stp);
1017 static void release_last_closed_stateid(struct nfs4_openowner *oo)
1019 struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
1022 nfs4_put_stid(&s->st_stid);
1023 oo->oo_last_closed_stid = NULL;
1027 static void nfs4_free_openowner(struct nfs4_openowner *oo)
1029 kfree(oo->oo_owner.so_owner.data);
1030 kmem_cache_free(openowner_slab, oo);
1033 static void release_openowner(struct nfs4_openowner *oo)
1035 unhash_openowner(oo);
1036 list_del(&oo->oo_close_lru);
1037 release_last_closed_stateid(oo);
1038 nfs4_free_openowner(oo);
1042 hash_sessionid(struct nfs4_sessionid *sessionid)
1044 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1046 return sid->sequence % SESSION_HASH_SIZE;
1051 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1053 u32 *ptr = (u32 *)(&sessionid->data[0]);
1054 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1058 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1064 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1065 * won't be used for replay.
1067 void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1069 struct nfs4_stateowner *so = cstate->replay_owner;
1071 if (nfserr == nfserr_replay_me)
1074 if (!seqid_mutating_err(ntohl(nfserr))) {
1075 cstate->replay_owner = NULL;
1080 if (so->so_is_open_owner)
1081 release_last_closed_stateid(openowner(so));
1087 gen_sessionid(struct nfsd4_session *ses)
1089 struct nfs4_client *clp = ses->se_client;
1090 struct nfsd4_sessionid *sid;
1092 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1093 sid->clientid = clp->cl_clientid;
1094 sid->sequence = current_sessionid++;
1099 * The protocol defines ca_maxresponssize_cached to include the size of
1100 * the rpc header, but all we need to cache is the data starting after
1101 * the end of the initial SEQUENCE operation--the rest we regenerate
1102 * each time. Therefore we can advertise a ca_maxresponssize_cached
1103 * value that is the number of bytes in our cache plus a few additional
1104 * bytes. In order to stay on the safe side, and not promise more than
1105 * we can cache, those additional bytes must be the minimum possible: 24
1106 * bytes of rpc header (xid through accept state, with AUTH_NULL
1107 * verifier), 12 for the compound header (with zero-length tag), and 44
1108 * for the SEQUENCE op response:
1110 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1113 free_session_slots(struct nfsd4_session *ses)
1117 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1118 kfree(ses->se_slots[i]);
1122 * We don't actually need to cache the rpc and session headers, so we
1123 * can allocate a little less for each slot:
1125 static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
1129 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1132 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1133 return size + sizeof(struct nfsd4_slot);
1137 * XXX: If we run out of reserved DRC memory we could (up to a point)
1138 * re-negotiate active sessions and reduce their slot usage to make
1139 * room for new connections. For now we just fail the create session.
1141 static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
1143 u32 slotsize = slot_bytes(ca);
1144 u32 num = ca->maxreqs;
1147 spin_lock(&nfsd_drc_lock);
1148 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1149 nfsd_drc_max_mem - nfsd_drc_mem_used);
1150 num = min_t(int, num, avail / slotsize);
1151 nfsd_drc_mem_used += num * slotsize;
1152 spin_unlock(&nfsd_drc_lock);
1157 static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
1159 int slotsize = slot_bytes(ca);
1161 spin_lock(&nfsd_drc_lock);
1162 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
1163 spin_unlock(&nfsd_drc_lock);
1166 static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1167 struct nfsd4_channel_attrs *battrs)
1169 int numslots = fattrs->maxreqs;
1170 int slotsize = slot_bytes(fattrs);
1171 struct nfsd4_session *new;
1174 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
1175 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1176 mem = numslots * sizeof(struct nfsd4_slot *);
1178 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
1181 /* allocate each struct nfsd4_slot and data cache in one piece */
1182 for (i = 0; i < numslots; i++) {
1183 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
1184 if (!new->se_slots[i])
1188 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1189 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1194 kfree(new->se_slots[i]);
1199 static void free_conn(struct nfsd4_conn *c)
1201 svc_xprt_put(c->cn_xprt);
1205 static void nfsd4_conn_lost(struct svc_xpt_user *u)
1207 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1208 struct nfs4_client *clp = c->cn_session->se_client;
1210 spin_lock(&clp->cl_lock);
1211 if (!list_empty(&c->cn_persession)) {
1212 list_del(&c->cn_persession);
1215 nfsd4_probe_callback(clp);
1216 spin_unlock(&clp->cl_lock);
1219 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
1221 struct nfsd4_conn *conn;
1223 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1226 svc_xprt_get(rqstp->rq_xprt);
1227 conn->cn_xprt = rqstp->rq_xprt;
1228 conn->cn_flags = flags;
1229 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1233 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1235 conn->cn_session = ses;
1236 list_add(&conn->cn_persession, &ses->se_conns);
1239 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1241 struct nfs4_client *clp = ses->se_client;
1243 spin_lock(&clp->cl_lock);
1244 __nfsd4_hash_conn(conn, ses);
1245 spin_unlock(&clp->cl_lock);
1248 static int nfsd4_register_conn(struct nfsd4_conn *conn)
1250 conn->cn_xpt_user.callback = nfsd4_conn_lost;
1251 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
1254 static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
1258 nfsd4_hash_conn(conn, ses);
1259 ret = nfsd4_register_conn(conn);
1261 /* oops; xprt is already down: */
1262 nfsd4_conn_lost(&conn->cn_xpt_user);
1263 /* We may have gained or lost a callback channel: */
1264 nfsd4_probe_callback_sync(ses->se_client);
1267 static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
1269 u32 dir = NFS4_CDFC4_FORE;
1271 if (cses->flags & SESSION4_BACK_CHAN)
1272 dir |= NFS4_CDFC4_BACK;
1273 return alloc_conn(rqstp, dir);
1276 /* must be called under client_lock */
1277 static void nfsd4_del_conns(struct nfsd4_session *s)
1279 struct nfs4_client *clp = s->se_client;
1280 struct nfsd4_conn *c;
1282 spin_lock(&clp->cl_lock);
1283 while (!list_empty(&s->se_conns)) {
1284 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1285 list_del_init(&c->cn_persession);
1286 spin_unlock(&clp->cl_lock);
1288 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1291 spin_lock(&clp->cl_lock);
1293 spin_unlock(&clp->cl_lock);
1296 static void __free_session(struct nfsd4_session *ses)
1298 free_session_slots(ses);
1302 static void free_session(struct nfsd4_session *ses)
1304 struct nfsd_net *nn = net_generic(ses->se_client->net, nfsd_net_id);
1306 lockdep_assert_held(&nn->client_lock);
1307 nfsd4_del_conns(ses);
1308 nfsd4_put_drc_mem(&ses->se_fchannel);
1309 __free_session(ses);
1312 static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
1315 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1317 new->se_client = clp;
1320 INIT_LIST_HEAD(&new->se_conns);
1322 new->se_cb_seq_nr = 1;
1323 new->se_flags = cses->flags;
1324 new->se_cb_prog = cses->callback_prog;
1325 new->se_cb_sec = cses->cb_sec;
1326 atomic_set(&new->se_ref, 0);
1327 idx = hash_sessionid(&new->se_sessionid);
1328 spin_lock(&nn->client_lock);
1329 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
1330 spin_lock(&clp->cl_lock);
1331 list_add(&new->se_perclnt, &clp->cl_sessions);
1332 spin_unlock(&clp->cl_lock);
1333 spin_unlock(&nn->client_lock);
1335 if (cses->flags & SESSION4_BACK_CHAN) {
1336 struct sockaddr *sa = svc_addr(rqstp);
1338 * This is a little silly; with sessions there's no real
1339 * use for the callback address. Use the peer address
1340 * as a reasonable default for now, but consider fixing
1341 * the rpc client not to require an address in the
1344 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1345 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
1349 /* caller must hold client_lock */
1350 static struct nfsd4_session *
1351 __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
1353 struct nfsd4_session *elem;
1355 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1357 dump_sessionid(__func__, sessionid);
1358 idx = hash_sessionid(sessionid);
1359 /* Search in the appropriate list */
1360 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
1361 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1362 NFS4_MAX_SESSIONID_LEN)) {
1367 dprintk("%s: session not found\n", __func__);
1371 static struct nfsd4_session *
1372 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1375 struct nfsd4_session *session;
1376 __be32 status = nfserr_badsession;
1378 session = __find_in_sessionid_hashtbl(sessionid, net);
1381 status = nfsd4_get_session_locked(session);
1389 /* caller must hold client_lock */
1391 unhash_session(struct nfsd4_session *ses)
1393 list_del(&ses->se_hash);
1394 spin_lock(&ses->se_client->cl_lock);
1395 list_del(&ses->se_perclnt);
1396 spin_unlock(&ses->se_client->cl_lock);
1399 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1401 STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
1403 if (clid->cl_boot == nn->boot_time)
1405 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
1406 clid->cl_boot, clid->cl_id, nn->boot_time);
1411 * XXX Should we use a slab cache ?
1412 * This type of memory management is somewhat inefficient, but we use it
1413 * anyway since SETCLIENTID is not a common operation.
1415 static struct nfs4_client *alloc_client(struct xdr_netobj name)
1417 struct nfs4_client *clp;
1419 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1422 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
1423 if (clp->cl_name.data == NULL) {
1427 clp->cl_name.len = name.len;
1428 INIT_LIST_HEAD(&clp->cl_sessions);
1429 idr_init(&clp->cl_stateids);
1430 atomic_set(&clp->cl_refcount, 0);
1431 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1432 INIT_LIST_HEAD(&clp->cl_idhash);
1433 INIT_LIST_HEAD(&clp->cl_openowners);
1434 INIT_LIST_HEAD(&clp->cl_delegations);
1435 INIT_LIST_HEAD(&clp->cl_lru);
1436 INIT_LIST_HEAD(&clp->cl_callbacks);
1437 INIT_LIST_HEAD(&clp->cl_revoked);
1438 spin_lock_init(&clp->cl_lock);
1439 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1444 free_client(struct nfs4_client *clp)
1446 struct nfsd_net __maybe_unused *nn = net_generic(clp->net, nfsd_net_id);
1448 lockdep_assert_held(&nn->client_lock);
1449 while (!list_empty(&clp->cl_sessions)) {
1450 struct nfsd4_session *ses;
1451 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1453 list_del(&ses->se_perclnt);
1454 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1457 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
1458 free_svc_cred(&clp->cl_cred);
1459 kfree(clp->cl_name.data);
1460 idr_destroy(&clp->cl_stateids);
1464 /* must be called under the client_lock */
1466 unhash_client_locked(struct nfs4_client *clp)
1468 struct nfsd4_session *ses;
1470 list_del(&clp->cl_lru);
1471 spin_lock(&clp->cl_lock);
1472 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1473 list_del_init(&ses->se_hash);
1474 spin_unlock(&clp->cl_lock);
1478 destroy_client(struct nfs4_client *clp)
1480 struct nfs4_openowner *oo;
1481 struct nfs4_delegation *dp;
1482 struct list_head reaplist;
1483 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1485 INIT_LIST_HEAD(&reaplist);
1486 spin_lock(&state_lock);
1487 while (!list_empty(&clp->cl_delegations)) {
1488 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
1489 unhash_delegation_locked(dp);
1490 list_add(&dp->dl_recall_lru, &reaplist);
1492 spin_unlock(&state_lock);
1493 while (!list_empty(&reaplist)) {
1494 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
1495 list_del_init(&dp->dl_recall_lru);
1496 nfs4_put_stid(&dp->dl_stid);
1498 while (!list_empty(&clp->cl_revoked)) {
1499 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
1500 list_del_init(&dp->dl_recall_lru);
1501 nfs4_put_stid(&dp->dl_stid);
1503 while (!list_empty(&clp->cl_openowners)) {
1504 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1505 release_openowner(oo);
1507 nfsd4_shutdown_callback(clp);
1508 if (clp->cl_cb_conn.cb_xprt)
1509 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
1510 list_del(&clp->cl_idhash);
1511 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1512 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1514 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1515 spin_lock(&nn->client_lock);
1516 unhash_client_locked(clp);
1517 WARN_ON_ONCE(atomic_read(&clp->cl_refcount));
1519 spin_unlock(&nn->client_lock);
1522 static void expire_client(struct nfs4_client *clp)
1524 nfsd4_client_record_remove(clp);
1525 destroy_client(clp);
1528 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1530 memcpy(target->cl_verifier.data, source->data,
1531 sizeof(target->cl_verifier.data));
1534 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1536 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1537 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1540 static int copy_cred(struct svc_cred *target, struct svc_cred *source)
1542 if (source->cr_principal) {
1543 target->cr_principal =
1544 kstrdup(source->cr_principal, GFP_KERNEL);
1545 if (target->cr_principal == NULL)
1548 target->cr_principal = NULL;
1549 target->cr_flavor = source->cr_flavor;
1550 target->cr_uid = source->cr_uid;
1551 target->cr_gid = source->cr_gid;
1552 target->cr_group_info = source->cr_group_info;
1553 get_group_info(target->cr_group_info);
1554 target->cr_gss_mech = source->cr_gss_mech;
1555 if (source->cr_gss_mech)
1556 gss_mech_get(source->cr_gss_mech);
1561 compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1565 res = o1->len - o2->len;
1568 return (long long)memcmp(o1->data, o2->data, o1->len);
1571 static int same_name(const char *n1, const char *n2)
1573 return 0 == memcmp(n1, n2, HEXDIR_LEN);
1577 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1579 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
1583 same_clid(clientid_t *cl1, clientid_t *cl2)
1585 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
1588 static bool groups_equal(struct group_info *g1, struct group_info *g2)
1592 if (g1->ngroups != g2->ngroups)
1594 for (i=0; i<g1->ngroups; i++)
1595 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
1601 * RFC 3530 language requires clid_inuse be returned when the
1602 * "principal" associated with a requests differs from that previously
1603 * used. We use uid, gid's, and gss principal string as our best
1604 * approximation. We also don't want to allow non-gss use of a client
1605 * established using gss: in theory cr_principal should catch that
1606 * change, but in practice cr_principal can be null even in the gss case
1607 * since gssd doesn't always pass down a principal string.
1609 static bool is_gss_cred(struct svc_cred *cr)
1611 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1612 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1617 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1619 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
1620 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1621 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
1622 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1624 if (cr1->cr_principal == cr2->cr_principal)
1626 if (!cr1->cr_principal || !cr2->cr_principal)
1628 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
1631 static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1633 struct svc_cred *cr = &rqstp->rq_cred;
1636 if (!cr->cr_gss_mech)
1638 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1639 return service == RPC_GSS_SVC_INTEGRITY ||
1640 service == RPC_GSS_SVC_PRIVACY;
1643 static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1645 struct svc_cred *cr = &rqstp->rq_cred;
1647 if (!cl->cl_mach_cred)
1649 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1651 if (!svc_rqst_integrity_protected(rqstp))
1653 if (!cr->cr_principal)
1655 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1658 static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
1660 static u32 current_clientid = 1;
1662 clp->cl_clientid.cl_boot = nn->boot_time;
1663 clp->cl_clientid.cl_id = current_clientid++;
1666 static void gen_confirm(struct nfs4_client *clp)
1672 * This is opaque to client, so no need to byte-swap. Use
1673 * __force to keep sparse happy
1675 verf[0] = (__force __be32)get_seconds();
1676 verf[1] = (__force __be32)i++;
1677 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
1680 static struct nfs4_stid *
1681 find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
1683 struct nfs4_stid *ret;
1685 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1686 if (!ret || !ret->sc_type)
1691 static struct nfs4_stid *
1692 find_stateid(struct nfs4_client *cl, stateid_t *t)
1694 struct nfs4_stid *ret;
1696 spin_lock(&cl->cl_lock);
1697 ret = find_stateid_locked(cl, t);
1698 spin_unlock(&cl->cl_lock);
1702 static struct nfs4_stid *
1703 find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
1705 struct nfs4_stid *s;
1707 spin_lock(&cl->cl_lock);
1708 s = find_stateid_locked(cl, t);
1709 if (s != NULL && !(typemask & s->sc_type))
1711 spin_unlock(&cl->cl_lock);
1715 static struct nfs4_client *create_client(struct xdr_netobj name,
1716 struct svc_rqst *rqstp, nfs4_verifier *verf)
1718 struct nfs4_client *clp;
1719 struct sockaddr *sa = svc_addr(rqstp);
1721 struct net *net = SVC_NET(rqstp);
1722 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1724 clp = alloc_client(name);
1728 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1730 spin_lock(&nn->client_lock);
1732 spin_unlock(&nn->client_lock);
1735 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_run_cb_null);
1736 clp->cl_time = get_seconds();
1737 clear_bit(0, &clp->cl_cb_slot_busy);
1738 copy_verf(clp, verf);
1739 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
1741 clp->cl_cb_session = NULL;
1747 add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1749 struct rb_node **new = &(root->rb_node), *parent = NULL;
1750 struct nfs4_client *clp;
1753 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1756 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1757 new = &((*new)->rb_left);
1759 new = &((*new)->rb_right);
1762 rb_link_node(&new_clp->cl_namenode, parent, new);
1763 rb_insert_color(&new_clp->cl_namenode, root);
1766 static struct nfs4_client *
1767 find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1770 struct rb_node *node = root->rb_node;
1771 struct nfs4_client *clp;
1774 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1775 cmp = compare_blob(&clp->cl_name, name);
1777 node = node->rb_left;
1779 node = node->rb_right;
1787 add_to_unconfirmed(struct nfs4_client *clp)
1789 unsigned int idhashval;
1790 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1792 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
1793 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
1794 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1795 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
1800 move_to_confirmed(struct nfs4_client *clp)
1802 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1803 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1805 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
1806 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
1807 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1808 add_clp_to_name_tree(clp, &nn->conf_name_tree);
1809 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
1813 static struct nfs4_client *
1814 find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
1816 struct nfs4_client *clp;
1817 unsigned int idhashval = clientid_hashval(clid->cl_id);
1819 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
1820 if (same_clid(&clp->cl_clientid, clid)) {
1821 if ((bool)clp->cl_minorversion != sessions)
1830 static struct nfs4_client *
1831 find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1833 struct list_head *tbl = nn->conf_id_hashtbl;
1835 return find_client_in_id_table(tbl, clid, sessions);
1838 static struct nfs4_client *
1839 find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1841 struct list_head *tbl = nn->unconf_id_hashtbl;
1843 return find_client_in_id_table(tbl, clid, sessions);
1846 static bool clp_used_exchangeid(struct nfs4_client *clp)
1848 return clp->cl_exchange_flags != 0;
1851 static struct nfs4_client *
1852 find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
1854 return find_clp_in_name_tree(name, &nn->conf_name_tree);
1857 static struct nfs4_client *
1858 find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
1860 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
1864 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
1866 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
1867 struct sockaddr *sa = svc_addr(rqstp);
1868 u32 scopeid = rpc_get_scope_id(sa);
1869 unsigned short expected_family;
1871 /* Currently, we only support tcp and tcp6 for the callback channel */
1872 if (se->se_callback_netid_len == 3 &&
1873 !memcmp(se->se_callback_netid_val, "tcp", 3))
1874 expected_family = AF_INET;
1875 else if (se->se_callback_netid_len == 4 &&
1876 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1877 expected_family = AF_INET6;
1881 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
1882 se->se_callback_addr_len,
1883 (struct sockaddr *)&conn->cb_addr,
1884 sizeof(conn->cb_addr));
1886 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
1889 if (conn->cb_addr.ss_family == AF_INET6)
1890 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
1892 conn->cb_prog = se->se_callback_prog;
1893 conn->cb_ident = se->se_callback_ident;
1894 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
1897 conn->cb_addr.ss_family = AF_UNSPEC;
1898 conn->cb_addrlen = 0;
1899 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
1900 "will not receive delegations\n",
1901 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1907 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
1910 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1912 struct xdr_buf *buf = resp->xdr.buf;
1913 struct nfsd4_slot *slot = resp->cstate.slot;
1916 dprintk("--> %s slot %p\n", __func__, slot);
1918 slot->sl_opcnt = resp->opcnt;
1919 slot->sl_status = resp->cstate.status;
1921 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
1922 if (nfsd4_not_cached(resp)) {
1923 slot->sl_datalen = 0;
1926 base = resp->cstate.data_offset;
1927 slot->sl_datalen = buf->len - base;
1928 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
1929 WARN("%s: sessions DRC could not cache compound\n", __func__);
1934 * Encode the replay sequence operation from the slot values.
1935 * If cachethis is FALSE encode the uncached rep error on the next
1936 * operation which sets resp->p and increments resp->opcnt for
1937 * nfs4svc_encode_compoundres.
1941 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
1942 struct nfsd4_compoundres *resp)
1944 struct nfsd4_op *op;
1945 struct nfsd4_slot *slot = resp->cstate.slot;
1947 /* Encode the replayed sequence operation */
1948 op = &args->ops[resp->opcnt - 1];
1949 nfsd4_encode_operation(resp, op);
1951 /* Return nfserr_retry_uncached_rep in next operation. */
1952 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
1953 op = &args->ops[resp->opcnt++];
1954 op->status = nfserr_retry_uncached_rep;
1955 nfsd4_encode_operation(resp, op);
1961 * The sequence operation is not cached because we can use the slot and
1965 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
1966 struct nfsd4_sequence *seq)
1968 struct nfsd4_slot *slot = resp->cstate.slot;
1969 struct xdr_stream *xdr = &resp->xdr;
1973 dprintk("--> %s slot %p\n", __func__, slot);
1975 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
1979 p = xdr_reserve_space(xdr, slot->sl_datalen);
1982 return nfserr_serverfault;
1984 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
1985 xdr_commit_encode(xdr);
1987 resp->opcnt = slot->sl_opcnt;
1988 return slot->sl_status;
1992 * Set the exchange_id flags returned by the server.
1995 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1997 /* pNFS is not supported */
1998 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
2000 /* Referrals are supported, Migration is not. */
2001 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2003 /* set the wire flags to return to client. */
2004 clid->flags = new->cl_exchange_flags;
2007 static bool client_has_state(struct nfs4_client *clp)
2010 * Note clp->cl_openowners check isn't quite right: there's no
2011 * need to count owners without stateid's.
2013 * Also note we should probably be using this in 4.0 case too.
2015 return !list_empty(&clp->cl_openowners)
2016 || !list_empty(&clp->cl_delegations)
2017 || !list_empty(&clp->cl_sessions);
2021 nfsd4_exchange_id(struct svc_rqst *rqstp,
2022 struct nfsd4_compound_state *cstate,
2023 struct nfsd4_exchange_id *exid)
2025 struct nfs4_client *unconf, *conf, *new;
2027 char addr_str[INET6_ADDRSTRLEN];
2028 nfs4_verifier verf = exid->verifier;
2029 struct sockaddr *sa = svc_addr(rqstp);
2030 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
2031 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2033 rpc_ntop(sa, addr_str, sizeof(addr_str));
2034 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
2035 "ip_addr=%s flags %x, spa_how %d\n",
2036 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
2037 addr_str, exid->flags, exid->spa_how);
2039 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
2040 return nfserr_inval;
2042 switch (exid->spa_how) {
2044 if (!svc_rqst_integrity_protected(rqstp))
2045 return nfserr_inval;
2048 default: /* checked by xdr code */
2051 return nfserr_encr_alg_unsupp;
2054 /* Cases below refer to rfc 5661 section 18.35.4: */
2056 conf = find_confirmed_client_by_name(&exid->clname, nn);
2058 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2059 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2062 if (!clp_used_exchangeid(conf)) { /* buggy client */
2063 status = nfserr_inval;
2066 if (!mach_creds_match(conf, rqstp)) {
2067 status = nfserr_wrong_cred;
2070 if (!creds_match) { /* case 9 */
2071 status = nfserr_perm;
2074 if (!verfs_match) { /* case 8 */
2075 status = nfserr_not_same;
2079 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
2083 if (!creds_match) { /* case 3 */
2084 if (client_has_state(conf)) {
2085 status = nfserr_clid_inuse;
2088 expire_client(conf);
2091 if (verfs_match) { /* case 2 */
2092 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
2096 /* case 5, client reboot */
2100 if (update) { /* case 7 */
2101 status = nfserr_noent;
2105 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
2106 if (unconf) /* case 4, possible retry or client restart */
2107 expire_client(unconf);
2109 /* case 1 (normal case) */
2111 new = create_client(exid->clname, rqstp, &verf);
2113 status = nfserr_jukebox;
2116 new->cl_minorversion = cstate->minorversion;
2117 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
2120 add_to_unconfirmed(new);
2122 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
2123 exid->clientid.cl_id = new->cl_clientid.cl_id;
2125 exid->seqid = new->cl_cs_slot.sl_seqid + 1;
2126 nfsd4_set_ex_flags(new, exid);
2128 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
2129 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
2133 nfs4_unlock_state();
2138 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
2140 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2143 /* The slot is in use, and no response has been sent. */
2145 if (seqid == slot_seqid)
2146 return nfserr_jukebox;
2148 return nfserr_seq_misordered;
2150 /* Note unsigned 32-bit arithmetic handles wraparound: */
2151 if (likely(seqid == slot_seqid + 1))
2153 if (seqid == slot_seqid)
2154 return nfserr_replay_cache;
2155 return nfserr_seq_misordered;
2159 * Cache the create session result into the create session single DRC
2160 * slot cache by saving the xdr structure. sl_seqid has been set.
2161 * Do this for solo or embedded create session operations.
2164 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
2165 struct nfsd4_clid_slot *slot, __be32 nfserr)
2167 slot->sl_status = nfserr;
2168 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2172 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2173 struct nfsd4_clid_slot *slot)
2175 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2176 return slot->sl_status;
2179 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2180 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2181 1 + /* MIN tag is length with zero, only length */ \
2182 3 + /* version, opcount, opcode */ \
2183 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2184 /* seqid, slotID, slotID, cache */ \
2185 4 ) * sizeof(__be32))
2187 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2188 2 + /* verifier: AUTH_NULL, length 0 */\
2190 1 + /* MIN tag is length with zero, only length */ \
2191 3 + /* opcount, opcode, opstatus*/ \
2192 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2193 /* seqid, slotID, slotID, slotID, status */ \
2194 5 ) * sizeof(__be32))
2196 static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
2198 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2200 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2201 return nfserr_toosmall;
2202 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2203 return nfserr_toosmall;
2204 ca->headerpadsz = 0;
2205 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2206 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2207 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2208 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2209 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2210 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2212 * Note decreasing slot size below client's request may make it
2213 * difficult for client to function correctly, whereas
2214 * decreasing the number of slots will (just?) affect
2215 * performance. When short on memory we therefore prefer to
2216 * decrease number of slots instead of their size. Clients that
2217 * request larger slots than they need will get poor results:
2219 ca->maxreqs = nfsd4_get_drc_mem(ca);
2221 return nfserr_jukebox;
2226 #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2227 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2228 #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2229 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2231 static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2233 ca->headerpadsz = 0;
2236 * These RPC_MAX_HEADER macros are overkill, especially since we
2237 * don't even do gss on the backchannel yet. But this is still
2238 * less than 1k. Tighten up this estimate in the unlikely event
2239 * it turns out to be a problem for some client:
2241 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
2242 return nfserr_toosmall;
2243 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
2244 return nfserr_toosmall;
2245 ca->maxresp_cached = 0;
2247 return nfserr_toosmall;
2252 static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2254 switch (cbs->flavor) {
2260 * GSS case: the spec doesn't allow us to return this
2261 * error. But it also doesn't allow us not to support
2263 * I'd rather this fail hard than return some error the
2264 * client might think it can already handle:
2266 return nfserr_encr_alg_unsupp;
2271 nfsd4_create_session(struct svc_rqst *rqstp,
2272 struct nfsd4_compound_state *cstate,
2273 struct nfsd4_create_session *cr_ses)
2275 struct sockaddr *sa = svc_addr(rqstp);
2276 struct nfs4_client *conf, *unconf;
2277 struct nfsd4_session *new;
2278 struct nfsd4_conn *conn;
2279 struct nfsd4_clid_slot *cs_slot = NULL;
2281 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2283 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2284 return nfserr_inval;
2285 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2288 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
2291 status = check_backchannel_attrs(&cr_ses->back_channel);
2293 goto out_release_drc_mem;
2294 status = nfserr_jukebox;
2295 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
2297 goto out_release_drc_mem;
2298 conn = alloc_conn_from_crses(rqstp, cr_ses);
2300 goto out_free_session;
2303 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
2304 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
2305 WARN_ON_ONCE(conf && unconf);
2308 status = nfserr_wrong_cred;
2309 if (!mach_creds_match(conf, rqstp))
2311 cs_slot = &conf->cl_cs_slot;
2312 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2313 if (status == nfserr_replay_cache) {
2314 status = nfsd4_replay_create_session(cr_ses, cs_slot);
2316 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
2317 status = nfserr_seq_misordered;
2320 } else if (unconf) {
2321 struct nfs4_client *old;
2322 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
2323 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
2324 status = nfserr_clid_inuse;
2327 status = nfserr_wrong_cred;
2328 if (!mach_creds_match(unconf, rqstp))
2330 cs_slot = &unconf->cl_cs_slot;
2331 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2333 /* an unconfirmed replay returns misordered */
2334 status = nfserr_seq_misordered;
2337 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
2339 status = mark_client_expired(old);
2344 move_to_confirmed(unconf);
2347 status = nfserr_stale_clientid;
2352 * We do not support RDMA or persistent sessions
2354 cr_ses->flags &= ~SESSION4_PERSIST;
2355 cr_ses->flags &= ~SESSION4_RDMA;
2357 init_session(rqstp, new, conf, cr_ses);
2358 nfsd4_init_conn(rqstp, conn, new);
2360 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
2361 NFS4_MAX_SESSIONID_LEN);
2362 cs_slot->sl_seqid++;
2363 cr_ses->seqid = cs_slot->sl_seqid;
2365 /* cache solo and embedded create sessions under the state lock */
2366 nfsd4_cache_create_session(cr_ses, cs_slot, status);
2367 nfs4_unlock_state();
2370 nfs4_unlock_state();
2373 __free_session(new);
2374 out_release_drc_mem:
2375 nfsd4_put_drc_mem(&cr_ses->fore_channel);
2379 static __be32 nfsd4_map_bcts_dir(u32 *dir)
2382 case NFS4_CDFC4_FORE:
2383 case NFS4_CDFC4_BACK:
2385 case NFS4_CDFC4_FORE_OR_BOTH:
2386 case NFS4_CDFC4_BACK_OR_BOTH:
2387 *dir = NFS4_CDFC4_BOTH;
2390 return nfserr_inval;
2393 __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2395 struct nfsd4_session *session = cstate->session;
2396 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2399 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2402 spin_lock(&nn->client_lock);
2403 session->se_cb_prog = bc->bc_cb_program;
2404 session->se_cb_sec = bc->bc_cb_sec;
2405 spin_unlock(&nn->client_lock);
2407 nfsd4_probe_callback(session->se_client);
2412 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2413 struct nfsd4_compound_state *cstate,
2414 struct nfsd4_bind_conn_to_session *bcts)
2417 struct nfsd4_conn *conn;
2418 struct nfsd4_session *session;
2419 struct net *net = SVC_NET(rqstp);
2420 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2422 if (!nfsd4_last_compound_op(rqstp))
2423 return nfserr_not_only_op;
2425 spin_lock(&nn->client_lock);
2426 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
2427 spin_unlock(&nn->client_lock);
2429 goto out_no_session;
2430 status = nfserr_wrong_cred;
2431 if (!mach_creds_match(session->se_client, rqstp))
2433 status = nfsd4_map_bcts_dir(&bcts->dir);
2436 conn = alloc_conn(rqstp, bcts->dir);
2437 status = nfserr_jukebox;
2440 nfsd4_init_conn(rqstp, conn, session);
2443 nfsd4_put_session(session);
2445 nfs4_unlock_state();
2449 static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2453 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2457 nfsd4_destroy_session(struct svc_rqst *r,
2458 struct nfsd4_compound_state *cstate,
2459 struct nfsd4_destroy_session *sessionid)
2461 struct nfsd4_session *ses;
2463 int ref_held_by_me = 0;
2464 struct net *net = SVC_NET(r);
2465 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2468 status = nfserr_not_only_op;
2469 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
2470 if (!nfsd4_last_compound_op(r))
2474 dump_sessionid(__func__, &sessionid->sessionid);
2475 spin_lock(&nn->client_lock);
2476 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
2478 goto out_client_lock;
2479 status = nfserr_wrong_cred;
2480 if (!mach_creds_match(ses->se_client, r))
2481 goto out_put_session;
2482 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
2484 goto out_put_session;
2485 unhash_session(ses);
2486 spin_unlock(&nn->client_lock);
2488 nfsd4_probe_callback_sync(ses->se_client);
2490 spin_lock(&nn->client_lock);
2493 nfsd4_put_session_locked(ses);
2495 spin_unlock(&nn->client_lock);
2497 nfs4_unlock_state();
2501 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
2503 struct nfsd4_conn *c;
2505 list_for_each_entry(c, &s->se_conns, cn_persession) {
2506 if (c->cn_xprt == xpt) {
2513 static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
2515 struct nfs4_client *clp = ses->se_client;
2516 struct nfsd4_conn *c;
2517 __be32 status = nfs_ok;
2520 spin_lock(&clp->cl_lock);
2521 c = __nfsd4_find_conn(new->cn_xprt, ses);
2524 status = nfserr_conn_not_bound_to_session;
2525 if (clp->cl_mach_cred)
2527 __nfsd4_hash_conn(new, ses);
2528 spin_unlock(&clp->cl_lock);
2529 ret = nfsd4_register_conn(new);
2531 /* oops; xprt is already down: */
2532 nfsd4_conn_lost(&new->cn_xpt_user);
2535 spin_unlock(&clp->cl_lock);
2540 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2542 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2544 return args->opcnt > session->se_fchannel.maxops;
2547 static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2548 struct nfsd4_session *session)
2550 struct xdr_buf *xb = &rqstp->rq_arg;
2552 return xb->len > session->se_fchannel.maxreq_sz;
2556 nfsd4_sequence(struct svc_rqst *rqstp,
2557 struct nfsd4_compound_state *cstate,
2558 struct nfsd4_sequence *seq)
2560 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2561 struct xdr_stream *xdr = &resp->xdr;
2562 struct nfsd4_session *session;
2563 struct nfs4_client *clp;
2564 struct nfsd4_slot *slot;
2565 struct nfsd4_conn *conn;
2568 struct net *net = SVC_NET(rqstp);
2569 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2571 if (resp->opcnt != 1)
2572 return nfserr_sequence_pos;
2575 * Will be either used or freed by nfsd4_sequence_check_conn
2578 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2580 return nfserr_jukebox;
2582 spin_lock(&nn->client_lock);
2583 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
2585 goto out_no_session;
2586 clp = session->se_client;
2588 status = nfserr_too_many_ops;
2589 if (nfsd4_session_too_many_ops(rqstp, session))
2590 goto out_put_session;
2592 status = nfserr_req_too_big;
2593 if (nfsd4_request_too_big(rqstp, session))
2594 goto out_put_session;
2596 status = nfserr_badslot;
2597 if (seq->slotid >= session->se_fchannel.maxreqs)
2598 goto out_put_session;
2600 slot = session->se_slots[seq->slotid];
2601 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2603 /* We do not negotiate the number of slots yet, so set the
2604 * maxslots to the session maxreqs which is used to encode
2605 * sr_highest_slotid and the sr_target_slot id to maxslots */
2606 seq->maxslots = session->se_fchannel.maxreqs;
2608 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2609 slot->sl_flags & NFSD4_SLOT_INUSE);
2610 if (status == nfserr_replay_cache) {
2611 status = nfserr_seq_misordered;
2612 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
2613 goto out_put_session;
2614 cstate->slot = slot;
2615 cstate->session = session;
2617 /* Return the cached reply status and set cstate->status
2618 * for nfsd4_proc_compound processing */
2619 status = nfsd4_replay_cache_entry(resp, seq);
2620 cstate->status = nfserr_replay_cache;
2624 goto out_put_session;
2626 status = nfsd4_sequence_check_conn(conn, session);
2629 goto out_put_session;
2631 buflen = (seq->cachethis) ?
2632 session->se_fchannel.maxresp_cached :
2633 session->se_fchannel.maxresp_sz;
2634 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2636 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
2637 goto out_put_session;
2638 svc_reserve(rqstp, buflen);
2641 /* Success! bump slot seqid */
2642 slot->sl_seqid = seq->seqid;
2643 slot->sl_flags |= NFSD4_SLOT_INUSE;
2645 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
2647 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
2649 cstate->slot = slot;
2650 cstate->session = session;
2654 switch (clp->cl_cb_state) {
2656 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2658 case NFSD4_CB_FAULT:
2659 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2662 seq->status_flags = 0;
2664 if (!list_empty(&clp->cl_revoked))
2665 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
2669 spin_unlock(&nn->client_lock);
2672 nfsd4_put_session_locked(session);
2673 goto out_no_session;
2677 nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2679 struct nfsd4_compound_state *cs = &resp->cstate;
2681 if (nfsd4_has_session(cs)) {
2682 if (cs->status != nfserr_replay_cache) {
2683 nfsd4_store_cache_entry(resp);
2684 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2686 /* Drop session reference that was taken in nfsd4_sequence() */
2687 nfsd4_put_session(cs->session);
2689 put_client_renew(cs->clp);
2693 nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2695 struct nfs4_client *conf, *unconf, *clp;
2697 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2700 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
2701 conf = find_confirmed_client(&dc->clientid, true, nn);
2702 WARN_ON_ONCE(conf && unconf);
2707 if (client_has_state(conf)) {
2708 status = nfserr_clientid_busy;
2714 status = nfserr_stale_clientid;
2717 if (!mach_creds_match(clp, rqstp)) {
2718 status = nfserr_wrong_cred;
2723 nfs4_unlock_state();
2728 nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2732 if (rc->rca_one_fs) {
2733 if (!cstate->current_fh.fh_dentry)
2734 return nfserr_nofilehandle;
2736 * We don't take advantage of the rca_one_fs case.
2737 * That's OK, it's optional, we can safely ignore it.
2743 status = nfserr_complete_already;
2744 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2745 &cstate->session->se_client->cl_flags))
2748 status = nfserr_stale_clientid;
2749 if (is_client_expired(cstate->session->se_client))
2751 * The following error isn't really legal.
2752 * But we only get here if the client just explicitly
2753 * destroyed the client. Surely it no longer cares what
2754 * error it gets back on an operation for the dead
2760 nfsd4_client_record_create(cstate->session->se_client);
2762 nfs4_unlock_state();
2767 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2768 struct nfsd4_setclientid *setclid)
2770 struct xdr_netobj clname = setclid->se_name;
2771 nfs4_verifier clverifier = setclid->se_verf;
2772 struct nfs4_client *conf, *unconf, *new;
2774 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2776 /* Cases below refer to rfc 3530 section 14.2.33: */
2778 conf = find_confirmed_client_by_name(&clname, nn);
2781 status = nfserr_clid_inuse;
2782 if (clp_used_exchangeid(conf))
2784 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
2785 char addr_str[INET6_ADDRSTRLEN];
2786 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2788 dprintk("NFSD: setclientid: string in use by client "
2789 "at %s\n", addr_str);
2793 unconf = find_unconfirmed_client_by_name(&clname, nn);
2795 expire_client(unconf);
2796 status = nfserr_jukebox;
2797 new = create_client(clname, rqstp, &clverifier);
2800 if (conf && same_verf(&conf->cl_verifier, &clverifier))
2801 /* case 1: probable callback update */
2802 copy_clid(new, conf);
2803 else /* case 4 (new client) or cases 2, 3 (client reboot): */
2805 new->cl_minorversion = 0;
2806 gen_callback(new, setclid, rqstp);
2807 add_to_unconfirmed(new);
2808 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2809 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2810 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2813 nfs4_unlock_state();
2819 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2820 struct nfsd4_compound_state *cstate,
2821 struct nfsd4_setclientid_confirm *setclientid_confirm)
2823 struct nfs4_client *conf, *unconf;
2824 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2825 clientid_t * clid = &setclientid_confirm->sc_clientid;
2827 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2829 if (STALE_CLIENTID(clid, nn))
2830 return nfserr_stale_clientid;
2833 conf = find_confirmed_client(clid, false, nn);
2834 unconf = find_unconfirmed_client(clid, false, nn);
2836 * We try hard to give out unique clientid's, so if we get an
2837 * attempt to confirm the same clientid with a different cred,
2838 * there's a bug somewhere. Let's charitably assume it's our
2841 status = nfserr_serverfault;
2842 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
2844 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
2846 /* cases below refer to rfc 3530 section 14.2.34: */
2847 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
2848 if (conf && !unconf) /* case 2: probable retransmit */
2850 else /* case 4: client hasn't noticed we rebooted yet? */
2851 status = nfserr_stale_clientid;
2855 if (conf) { /* case 1: callback update */
2856 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2857 nfsd4_probe_callback(conf);
2858 expire_client(unconf);
2859 } else { /* case 3: normal case; new or rebooted client */
2860 conf = find_confirmed_client_by_name(&unconf->cl_name, nn);
2862 status = mark_client_expired(conf);
2865 expire_client(conf);
2867 move_to_confirmed(unconf);
2868 nfsd4_probe_callback(unconf);
2871 nfs4_unlock_state();
2875 static struct nfs4_file *nfsd4_alloc_file(void)
2877 return kmem_cache_alloc(file_slab, GFP_KERNEL);
2880 /* OPEN Share state helper functions */
2881 static void nfsd4_init_file(struct nfs4_file *fp, struct knfsd_fh *fh)
2883 unsigned int hashval = file_hashval(fh);
2885 lockdep_assert_held(&state_lock);
2887 atomic_set(&fp->fi_ref, 1);
2888 spin_lock_init(&fp->fi_lock);
2889 INIT_LIST_HEAD(&fp->fi_stateids);
2890 INIT_LIST_HEAD(&fp->fi_delegations);
2891 fh_copy_shallow(&fp->fi_fhandle, fh);
2892 fp->fi_had_conflict = false;
2893 fp->fi_lease = NULL;
2894 fp->fi_share_deny = 0;
2895 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2896 memset(fp->fi_access, 0, sizeof(fp->fi_access));
2897 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
2901 nfsd4_free_slabs(void)
2903 kmem_cache_destroy(openowner_slab);
2904 kmem_cache_destroy(lockowner_slab);
2905 kmem_cache_destroy(file_slab);
2906 kmem_cache_destroy(stateid_slab);
2907 kmem_cache_destroy(deleg_slab);
2911 nfsd4_init_slabs(void)
2913 openowner_slab = kmem_cache_create("nfsd4_openowners",
2914 sizeof(struct nfs4_openowner), 0, 0, NULL);
2915 if (openowner_slab == NULL)
2917 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
2918 sizeof(struct nfs4_lockowner), 0, 0, NULL);
2919 if (lockowner_slab == NULL)
2920 goto out_free_openowner_slab;
2921 file_slab = kmem_cache_create("nfsd4_files",
2922 sizeof(struct nfs4_file), 0, 0, NULL);
2923 if (file_slab == NULL)
2924 goto out_free_lockowner_slab;
2925 stateid_slab = kmem_cache_create("nfsd4_stateids",
2926 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
2927 if (stateid_slab == NULL)
2928 goto out_free_file_slab;
2929 deleg_slab = kmem_cache_create("nfsd4_delegations",
2930 sizeof(struct nfs4_delegation), 0, 0, NULL);
2931 if (deleg_slab == NULL)
2932 goto out_free_stateid_slab;
2935 out_free_stateid_slab:
2936 kmem_cache_destroy(stateid_slab);
2938 kmem_cache_destroy(file_slab);
2939 out_free_lockowner_slab:
2940 kmem_cache_destroy(lockowner_slab);
2941 out_free_openowner_slab:
2942 kmem_cache_destroy(openowner_slab);
2944 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2948 static void init_nfs4_replay(struct nfs4_replay *rp)
2950 rp->rp_status = nfserr_serverfault;
2952 rp->rp_buf = rp->rp_ibuf;
2955 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
2957 struct nfs4_stateowner *sop;
2959 sop = kmem_cache_alloc(slab, GFP_KERNEL);
2963 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
2964 if (!sop->so_owner.data) {
2965 kmem_cache_free(slab, sop);
2968 sop->so_owner.len = owner->len;
2970 INIT_LIST_HEAD(&sop->so_stateids);
2971 sop->so_client = clp;
2972 init_nfs4_replay(&sop->so_replay);
2976 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
2978 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2980 list_add(&oo->oo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
2981 list_add(&oo->oo_perclient, &clp->cl_openowners);
2984 static struct nfs4_openowner *
2985 alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
2986 struct nfsd4_compound_state *cstate)
2988 struct nfs4_client *clp = cstate->clp;
2989 struct nfs4_openowner *oo;
2991 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
2994 oo->oo_owner.so_is_open_owner = 1;
2995 oo->oo_owner.so_seqid = open->op_seqid;
2996 oo->oo_flags = NFS4_OO_NEW;
2997 if (nfsd4_has_session(cstate))
2998 oo->oo_flags |= NFS4_OO_CONFIRMED;
3000 oo->oo_last_closed_stid = NULL;
3001 INIT_LIST_HEAD(&oo->oo_close_lru);
3002 hash_openowner(oo, clp, strhashval);
3006 static void init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
3007 struct nfs4_openowner *oo = open->op_openowner;
3009 stp->st_stid.sc_type = NFS4_OPEN_STID;
3010 INIT_LIST_HEAD(&stp->st_locks);
3011 stp->st_stateowner = &oo->oo_owner;
3013 stp->st_stid.sc_file = fp;
3014 stp->st_access_bmap = 0;
3015 stp->st_deny_bmap = 0;
3016 stp->st_openstp = NULL;
3017 spin_lock(&oo->oo_owner.so_client->cl_lock);
3018 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
3019 spin_lock(&fp->fi_lock);
3020 list_add(&stp->st_perfile, &fp->fi_stateids);
3021 spin_unlock(&fp->fi_lock);
3022 spin_unlock(&oo->oo_owner.so_client->cl_lock);
3026 move_to_close_lru(struct nfs4_openowner *oo, struct net *net)
3028 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3030 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
3032 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
3033 oo->oo_time = get_seconds();
3037 same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
3040 return (sop->so_owner.len == owner->len) &&
3041 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
3042 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
3045 static struct nfs4_openowner *
3046 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
3047 bool sessions, struct nfsd_net *nn)
3049 struct nfs4_stateowner *so;
3050 struct nfs4_openowner *oo;
3051 struct nfs4_client *clp;
3053 list_for_each_entry(so, &nn->ownerstr_hashtbl[hashval], so_strhash) {
3054 if (!so->so_is_open_owner)
3056 if (same_owner_str(so, &open->op_owner, &open->op_clientid)) {
3058 clp = oo->oo_owner.so_client;
3059 if ((bool)clp->cl_minorversion != sessions)
3061 renew_client(oo->oo_owner.so_client);
3068 /* search file_hashtbl[] for file */
3069 static struct nfs4_file *
3070 find_file_locked(struct knfsd_fh *fh)
3072 unsigned int hashval = file_hashval(fh);
3073 struct nfs4_file *fp;
3075 lockdep_assert_held(&state_lock);
3077 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
3078 if (nfsd_fh_match(&fp->fi_fhandle, fh)) {
3086 static struct nfs4_file *
3087 find_file(struct knfsd_fh *fh)
3089 struct nfs4_file *fp;
3091 spin_lock(&state_lock);
3092 fp = find_file_locked(fh);
3093 spin_unlock(&state_lock);
3097 static struct nfs4_file *
3098 find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
3100 struct nfs4_file *fp;
3102 spin_lock(&state_lock);
3103 fp = find_file_locked(fh);
3105 nfsd4_init_file(new, fh);
3108 spin_unlock(&state_lock);
3114 * Called to check deny when READ with all zero stateid or
3115 * WRITE with all zero or all one stateid
3118 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3120 struct nfs4_file *fp;
3121 __be32 ret = nfs_ok;
3123 fp = find_file(¤t_fh->fh_handle);
3126 /* Check for conflicting share reservations */
3127 spin_lock(&fp->fi_lock);
3128 if (fp->fi_share_deny & deny_type)
3129 ret = nfserr_locked;
3130 spin_unlock(&fp->fi_lock);
3135 void nfsd4_prepare_cb_recall(struct nfs4_delegation *dp)
3137 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3140 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
3143 * We can't do this in nfsd_break_deleg_cb because it is
3144 * already holding inode->i_lock.
3146 * If the dl_time != 0, then we know that it has already been
3147 * queued for a lease break. Don't queue it again.
3149 spin_lock(&state_lock);
3150 if (dp->dl_time == 0) {
3151 dp->dl_time = get_seconds();
3152 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
3154 spin_unlock(&state_lock);
3157 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3160 * We're assuming the state code never drops its reference
3161 * without first removing the lease. Since we're in this lease
3162 * callback (and since the lease code is serialized by the kernel
3163 * lock) we know the server hasn't removed the lease yet, we know
3164 * it's safe to take a reference.
3166 atomic_inc(&dp->dl_stid.sc_count);
3167 nfsd4_cb_recall(dp);
3170 /* Called from break_lease() with i_lock held. */
3171 static void nfsd_break_deleg_cb(struct file_lock *fl)
3173 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3174 struct nfs4_delegation *dp;
3177 WARN(1, "(%p)->fl_owner NULL\n", fl);
3180 if (fp->fi_had_conflict) {
3181 WARN(1, "duplicate break on %p\n", fp);
3185 * We don't want the locks code to timeout the lease for us;
3186 * we'll remove it ourself if a delegation isn't returned
3189 fl->fl_break_time = 0;
3191 spin_lock(&fp->fi_lock);
3192 fp->fi_had_conflict = true;
3194 * If there are no delegations on the list, then we can't count on this
3195 * lease ever being cleaned up. Set the fl_break_time to jiffies so that
3196 * time_out_leases will do it ASAP. The fact that fi_had_conflict is now
3197 * true should keep any new delegations from being hashed.
3199 if (list_empty(&fp->fi_delegations))
3200 fl->fl_break_time = jiffies;
3202 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3203 nfsd_break_one_deleg(dp);
3204 spin_unlock(&fp->fi_lock);
3208 int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
3211 return lease_modify(onlist, arg);
3216 static const struct lock_manager_operations nfsd_lease_mng_ops = {
3217 .lm_break = nfsd_break_deleg_cb,
3218 .lm_change = nfsd_change_deleg_cb,
3221 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3223 if (nfsd4_has_session(cstate))
3225 if (seqid == so->so_seqid - 1)
3226 return nfserr_replay_me;
3227 if (seqid == so->so_seqid)
3229 return nfserr_bad_seqid;
3232 static __be32 lookup_clientid(clientid_t *clid,
3233 struct nfsd4_compound_state *cstate,
3234 struct nfsd_net *nn)
3236 struct nfs4_client *found;
3239 found = cstate->clp;
3240 if (!same_clid(&found->cl_clientid, clid))
3241 return nfserr_stale_clientid;
3245 if (STALE_CLIENTID(clid, nn))
3246 return nfserr_stale_clientid;
3249 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3250 * cached already then we know this is for is for v4.0 and "sessions"
3253 WARN_ON_ONCE(cstate->session);
3254 found = find_confirmed_client(clid, false, nn);
3256 return nfserr_expired;
3258 /* Cache the nfs4_client in cstate! */
3259 cstate->clp = found;
3260 atomic_inc(&found->cl_refcount);
3265 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
3266 struct nfsd4_open *open, struct nfsd_net *nn)
3268 clientid_t *clientid = &open->op_clientid;
3269 struct nfs4_client *clp = NULL;
3270 unsigned int strhashval;
3271 struct nfs4_openowner *oo = NULL;
3274 if (STALE_CLIENTID(&open->op_clientid, nn))
3275 return nfserr_stale_clientid;
3277 * In case we need it later, after we've already created the
3278 * file and don't want to risk a further failure:
3280 open->op_file = nfsd4_alloc_file();
3281 if (open->op_file == NULL)
3282 return nfserr_jukebox;
3284 status = lookup_clientid(clientid, cstate, nn);
3289 strhashval = ownerstr_hashval(clientid->cl_id, &open->op_owner);
3290 oo = find_openstateowner_str(strhashval, open, cstate->minorversion, nn);
3291 open->op_openowner = oo;
3295 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
3296 /* Replace unconfirmed owners without checking for replay. */
3297 release_openowner(oo);
3298 open->op_openowner = NULL;
3301 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3306 oo = alloc_init_open_stateowner(strhashval, open, cstate);
3308 return nfserr_jukebox;
3309 open->op_openowner = oo;
3311 open->op_stp = nfs4_alloc_open_stateid(clp);
3313 return nfserr_jukebox;
3317 static inline __be32
3318 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3320 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3321 return nfserr_openmode;
3326 static int share_access_to_flags(u32 share_access)
3328 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3331 static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
3333 struct nfs4_stid *ret;
3335 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
3338 return delegstateid(ret);
3341 static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3343 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3344 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3348 nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
3349 struct nfs4_delegation **dp)
3352 __be32 status = nfserr_bad_stateid;
3354 *dp = find_deleg_stateid(cl, &open->op_delegate_stateid);
3357 flags = share_access_to_flags(open->op_share_access);
3358 status = nfs4_check_delegmode(*dp, flags);
3362 if (!nfsd4_is_deleg_cur(open))
3366 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
3370 static struct nfs4_ol_stateid *
3371 nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
3373 struct nfs4_ol_stateid *local, *ret = NULL;
3374 struct nfs4_openowner *oo = open->op_openowner;
3376 spin_lock(&fp->fi_lock);
3377 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
3378 /* ignore lock owners */
3379 if (local->st_stateowner->so_is_open_owner == 0)
3381 if (local->st_stateowner == &oo->oo_owner) {
3386 spin_unlock(&fp->fi_lock);
3390 static inline int nfs4_access_to_access(u32 nfs4_access)
3394 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3395 flags |= NFSD_MAY_READ;
3396 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3397 flags |= NFSD_MAY_WRITE;
3401 static inline __be32
3402 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3403 struct nfsd4_open *open)
3405 struct iattr iattr = {
3406 .ia_valid = ATTR_SIZE,
3409 if (!open->op_truncate)
3411 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
3412 return nfserr_inval;
3413 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3416 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
3417 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3418 struct nfsd4_open *open)
3420 struct file *filp = NULL;
3422 int oflag = nfs4_access_to_omode(open->op_share_access);
3423 int access = nfs4_access_to_access(open->op_share_access);
3424 unsigned char old_access_bmap, old_deny_bmap;
3426 spin_lock(&fp->fi_lock);
3429 * Are we trying to set a deny mode that would conflict with
3432 status = nfs4_file_check_deny(fp, open->op_share_deny);
3433 if (status != nfs_ok) {
3434 spin_unlock(&fp->fi_lock);
3438 /* set access to the file */
3439 status = nfs4_file_get_access(fp, open->op_share_access);
3440 if (status != nfs_ok) {
3441 spin_unlock(&fp->fi_lock);
3445 /* Set access bits in stateid */
3446 old_access_bmap = stp->st_access_bmap;
3447 set_access(open->op_share_access, stp);
3449 /* Set new deny mask */
3450 old_deny_bmap = stp->st_deny_bmap;
3451 set_deny(open->op_share_deny, stp);
3452 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3454 if (!fp->fi_fds[oflag]) {
3455 spin_unlock(&fp->fi_lock);
3456 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
3458 goto out_put_access;
3459 spin_lock(&fp->fi_lock);
3460 if (!fp->fi_fds[oflag]) {
3461 fp->fi_fds[oflag] = filp;
3465 spin_unlock(&fp->fi_lock);
3469 status = nfsd4_truncate(rqstp, cur_fh, open);
3471 goto out_put_access;
3475 stp->st_access_bmap = old_access_bmap;
3476 nfs4_file_put_access(fp, open->op_share_access);
3477 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3482 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)
3485 unsigned char old_deny_bmap;
3487 if (!test_access(open->op_share_access, stp))
3488 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
3490 /* test and set deny mode */
3491 spin_lock(&fp->fi_lock);
3492 status = nfs4_file_check_deny(fp, open->op_share_deny);
3493 if (status == nfs_ok) {
3494 old_deny_bmap = stp->st_deny_bmap;
3495 set_deny(open->op_share_deny, stp);
3496 fp->fi_share_deny |=
3497 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3499 spin_unlock(&fp->fi_lock);
3501 if (status != nfs_ok)
3504 status = nfsd4_truncate(rqstp, cur_fh, open);
3505 if (status != nfs_ok)
3506 reset_union_bmap_deny(old_deny_bmap, stp);
3511 nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
3513 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
3516 /* Should we give out recallable state?: */
3517 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3519 if (clp->cl_cb_state == NFSD4_CB_UP)
3522 * In the sessions case, since we don't have to establish a
3523 * separate connection for callbacks, we assume it's OK
3524 * until we hear otherwise:
3526 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3529 static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
3531 struct file_lock *fl;
3533 fl = locks_alloc_lock();
3536 locks_init_lock(fl);
3537 fl->fl_lmops = &nfsd_lease_mng_ops;
3538 fl->fl_flags = FL_DELEG;
3539 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3540 fl->fl_end = OFFSET_MAX;
3541 fl->fl_owner = (fl_owner_t)fp;
3542 fl->fl_pid = current->tgid;
3546 static int nfs4_setlease(struct nfs4_delegation *dp)
3548 struct nfs4_file *fp = dp->dl_stid.sc_file;
3549 struct file_lock *fl;
3553 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
3556 filp = find_readable_file(fp);
3558 /* We should always have a readable file here */
3563 status = vfs_setlease(filp, fl->fl_type, &fl);
3565 locks_free_lock(fl);
3568 spin_lock(&state_lock);
3569 spin_lock(&fp->fi_lock);
3570 /* Did the lease get broken before we took the lock? */
3572 if (fp->fi_had_conflict)
3577 atomic_inc(&fp->fi_delegees);
3578 hash_delegation_locked(dp, fp);
3582 fp->fi_deleg_file = filp;
3583 atomic_set(&fp->fi_delegees, 1);
3584 hash_delegation_locked(dp, fp);
3585 spin_unlock(&fp->fi_lock);
3586 spin_unlock(&state_lock);
3589 spin_unlock(&fp->fi_lock);
3590 spin_unlock(&state_lock);
3596 static struct nfs4_delegation *
3597 nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
3598 struct nfs4_file *fp)
3601 struct nfs4_delegation *dp;
3603 if (fp->fi_had_conflict)
3604 return ERR_PTR(-EAGAIN);
3606 dp = alloc_init_deleg(clp, fh);
3608 return ERR_PTR(-ENOMEM);
3611 spin_lock(&state_lock);
3612 spin_lock(&fp->fi_lock);
3613 dp->dl_stid.sc_file = fp;
3614 if (!fp->fi_lease) {
3615 spin_unlock(&fp->fi_lock);
3616 spin_unlock(&state_lock);
3617 status = nfs4_setlease(dp);
3620 atomic_inc(&fp->fi_delegees);
3621 if (fp->fi_had_conflict) {
3625 hash_delegation_locked(dp, fp);
3628 spin_unlock(&fp->fi_lock);
3629 spin_unlock(&state_lock);
3632 nfs4_put_stid(&dp->dl_stid);
3633 return ERR_PTR(status);
3638 static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3640 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3641 if (status == -EAGAIN)
3642 open->op_why_no_deleg = WND4_CONTENTION;
3644 open->op_why_no_deleg = WND4_RESOURCE;
3645 switch (open->op_deleg_want) {
3646 case NFS4_SHARE_WANT_READ_DELEG:
3647 case NFS4_SHARE_WANT_WRITE_DELEG:
3648 case NFS4_SHARE_WANT_ANY_DELEG:
3650 case NFS4_SHARE_WANT_CANCEL:
3651 open->op_why_no_deleg = WND4_CANCELLED;
3653 case NFS4_SHARE_WANT_NO_DELEG:
3660 * Attempt to hand out a delegation.
3662 * Note we don't support write delegations, and won't until the vfs has
3663 * proper support for them.
3666 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
3667 struct nfs4_ol_stateid *stp)
3669 struct nfs4_delegation *dp;
3670 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
3671 struct nfs4_client *clp = stp->st_stid.sc_client;
3675 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
3676 open->op_recall = 0;
3677 switch (open->op_claim_type) {
3678 case NFS4_OPEN_CLAIM_PREVIOUS:
3680 open->op_recall = 1;
3681 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3684 case NFS4_OPEN_CLAIM_NULL:
3685 case NFS4_OPEN_CLAIM_FH:
3687 * Let's not give out any delegations till everyone's
3688 * had the chance to reclaim theirs....
3690 if (locks_in_grace(clp->net))
3692 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
3695 * Also, if the file was opened for write or
3696 * create, there's a good chance the client's
3697 * about to write to it, resulting in an
3698 * immediate recall (since we don't support
3699 * write delegations):
3701 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
3703 if (open->op_create == NFS4_OPEN_CREATE)
3709 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file);
3713 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
3715 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
3716 STATEID_VAL(&dp->dl_stid.sc_stateid));
3717 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
3720 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3721 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
3722 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
3723 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
3724 open->op_recall = 1;
3727 /* 4.1 client asking for a delegation? */
3728 if (open->op_deleg_want)
3729 nfsd4_open_deleg_none_ext(open, status);
3733 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3734 struct nfs4_delegation *dp)
3736 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3737 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3738 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3739 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3740 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3741 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3742 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3743 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3745 /* Otherwise the client must be confused wanting a delegation
3746 * it already has, therefore we don't return
3747 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3752 * called with nfs4_lock_state() held.
3755 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3757 struct nfsd4_compoundres *resp = rqstp->rq_resp;
3758 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
3759 struct nfs4_file *fp = NULL;
3760 struct nfs4_ol_stateid *stp = NULL;
3761 struct nfs4_delegation *dp = NULL;
3765 * Lookup file; if found, lookup stateid and check open request,
3766 * and check for delegations in the process of being recalled.
3767 * If not found, create the nfs4_file struct
3769 fp = find_or_add_file(open->op_file, ¤t_fh->fh_handle);
3770 if (fp != open->op_file) {
3771 status = nfs4_check_deleg(cl, open, &dp);
3774 stp = nfsd4_find_existing_open(fp, open);
3776 open->op_file = NULL;
3777 status = nfserr_bad_stateid;
3778 if (nfsd4_is_deleg_cur(open))
3780 status = nfserr_jukebox;
3784 * OPEN the file, or upgrade an existing OPEN.
3785 * If truncate fails, the OPEN fails.
3788 /* Stateid was found, this is an OPEN upgrade */
3789 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
3794 open->op_stp = NULL;
3795 init_open_stateid(stp, fp, open);
3796 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
3798 release_open_stateid(stp);
3802 update_stateid(&stp->st_stid.sc_stateid);
3803 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
3805 if (nfsd4_has_session(&resp->cstate)) {
3806 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
3807 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3808 open->op_why_no_deleg = WND4_NOT_WANTED;
3814 * Attempt to hand out a delegation. No error return, because the
3815 * OPEN succeeds even if we fail.
3817 nfs4_open_delegation(current_fh, open, stp);
3821 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
3822 STATEID_VAL(&stp->st_stid.sc_stateid));
3824 /* 4.1 client trying to upgrade/downgrade delegation? */
3825 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
3826 open->op_deleg_want)
3827 nfsd4_deleg_xgrade_none_ext(open, dp);
3831 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
3832 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
3834 * To finish the open response, we just need to set the rflags.
3836 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
3837 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
3838 !nfsd4_has_session(&resp->cstate))
3839 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
3844 void nfsd4_cleanup_open_state(struct nfsd4_open *open, __be32 status)
3846 if (open->op_openowner) {
3847 struct nfs4_openowner *oo = open->op_openowner;
3849 if (!list_empty(&oo->oo_owner.so_stateids))
3850 list_del_init(&oo->oo_close_lru);
3851 if (oo->oo_flags & NFS4_OO_NEW) {
3853 release_openowner(oo);
3854 open->op_openowner = NULL;
3856 oo->oo_flags &= ~NFS4_OO_NEW;
3860 nfsd4_free_file(open->op_file);
3862 nfs4_put_stid(&open->op_stp->st_stid);
3866 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3869 struct nfs4_client *clp;
3871 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3874 dprintk("process_renew(%08x/%08x): starting\n",
3875 clid->cl_boot, clid->cl_id);
3876 status = lookup_clientid(clid, cstate, nn);
3880 status = nfserr_cb_path_down;
3881 if (!list_empty(&clp->cl_delegations)
3882 && clp->cl_cb_state != NFSD4_CB_UP)
3886 nfs4_unlock_state();
3891 nfsd4_end_grace(struct nfsd_net *nn)
3893 /* do nothing if grace period already ended */
3894 if (nn->grace_ended)
3897 dprintk("NFSD: end of grace period\n");
3898 nn->grace_ended = true;
3899 nfsd4_record_grace_done(nn, nn->boot_time);
3900 locks_end_grace(&nn->nfsd4_manager);
3902 * Now that every NFSv4 client has had the chance to recover and
3903 * to see the (possibly new, possibly shorter) lease time, we
3904 * can safely set the next grace time to the current lease time:
3906 nn->nfsd4_grace = nn->nfsd4_lease;
3910 nfs4_laundromat(struct nfsd_net *nn)
3912 struct nfs4_client *clp;
3913 struct nfs4_openowner *oo;
3914 struct nfs4_delegation *dp;
3915 struct list_head *pos, *next, reaplist;
3916 time_t cutoff = get_seconds() - nn->nfsd4_lease;
3917 time_t t, new_timeo = nn->nfsd4_lease;
3921 dprintk("NFSD: laundromat service - starting\n");
3922 nfsd4_end_grace(nn);
3923 INIT_LIST_HEAD(&reaplist);
3924 spin_lock(&nn->client_lock);
3925 list_for_each_safe(pos, next, &nn->client_lru) {
3926 clp = list_entry(pos, struct nfs4_client, cl_lru);
3927 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
3928 t = clp->cl_time - cutoff;
3929 new_timeo = min(new_timeo, t);
3932 if (mark_client_expired_locked(clp)) {
3933 dprintk("NFSD: client in use (clientid %08x)\n",
3934 clp->cl_clientid.cl_id);
3937 list_move(&clp->cl_lru, &reaplist);
3939 spin_unlock(&nn->client_lock);
3940 list_for_each_safe(pos, next, &reaplist) {
3941 clp = list_entry(pos, struct nfs4_client, cl_lru);
3942 dprintk("NFSD: purging unused client (clientid %08x)\n",
3943 clp->cl_clientid.cl_id);
3946 spin_lock(&state_lock);
3947 list_for_each_safe(pos, next, &nn->del_recall_lru) {
3948 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3949 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
3951 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
3952 t = dp->dl_time - cutoff;
3953 new_timeo = min(new_timeo, t);
3956 unhash_delegation_locked(dp);
3957 list_add(&dp->dl_recall_lru, &reaplist);
3959 spin_unlock(&state_lock);
3960 while (!list_empty(&reaplist)) {
3961 dp = list_first_entry(&reaplist, struct nfs4_delegation,
3963 list_del_init(&dp->dl_recall_lru);
3964 revoke_delegation(dp);
3966 list_for_each_safe(pos, next, &nn->close_lru) {
3967 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
3968 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
3969 t = oo->oo_time - cutoff;
3970 new_timeo = min(new_timeo, t);
3973 release_openowner(oo);
3975 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
3976 nfs4_unlock_state();
3980 static struct workqueue_struct *laundry_wq;
3981 static void laundromat_main(struct work_struct *);
3984 laundromat_main(struct work_struct *laundry)
3987 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
3989 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
3992 t = nfs4_laundromat(nn);
3993 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
3994 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
3997 static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
3999 if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_stid.sc_file->fi_fhandle))
4000 return nfserr_bad_stateid;
4005 access_permit_read(struct nfs4_ol_stateid *stp)
4007 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4008 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4009 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
4013 access_permit_write(struct nfs4_ol_stateid *stp)
4015 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4016 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
4020 __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
4022 __be32 status = nfserr_openmode;
4024 /* For lock stateid's, we test the parent open, not the lock: */
4025 if (stp->st_openstp)
4026 stp = stp->st_openstp;
4027 if ((flags & WR_STATE) && !access_permit_write(stp))
4029 if ((flags & RD_STATE) && !access_permit_read(stp))
4036 static inline __be32
4037 check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
4039 if (ONE_STATEID(stateid) && (flags & RD_STATE))
4041 else if (locks_in_grace(net)) {
4042 /* Answer in remaining cases depends on existence of
4043 * conflicting state; so we must wait out the grace period. */
4044 return nfserr_grace;
4045 } else if (flags & WR_STATE)
4046 return nfs4_share_conflict(current_fh,
4047 NFS4_SHARE_DENY_WRITE);
4048 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4049 return nfs4_share_conflict(current_fh,
4050 NFS4_SHARE_DENY_READ);
4054 * Allow READ/WRITE during grace period on recovered state only for files
4055 * that are not able to provide mandatory locking.
4058 grace_disallows_io(struct net *net, struct inode *inode)
4060 return locks_in_grace(net) && mandatory_lock(inode);
4063 /* Returns true iff a is later than b: */
4064 static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4066 return (s32)(a->si_generation - b->si_generation) > 0;
4069 static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
4072 * When sessions are used the stateid generation number is ignored
4075 if (has_session && in->si_generation == 0)
4078 if (in->si_generation == ref->si_generation)
4081 /* If the client sends us a stateid from the future, it's buggy: */
4082 if (stateid_generation_after(in, ref))
4083 return nfserr_bad_stateid;
4085 * However, we could see a stateid from the past, even from a
4086 * non-buggy client. For example, if the client sends a lock
4087 * while some IO is outstanding, the lock may bump si_generation
4088 * while the IO is still in flight. The client could avoid that
4089 * situation by waiting for responses on all the IO requests,
4090 * but better performance may result in retrying IO that
4091 * receives an old_stateid error if requests are rarely
4092 * reordered in flight:
4094 return nfserr_old_stateid;
4097 static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
4099 struct nfs4_stid *s;
4100 struct nfs4_ol_stateid *ols;
4103 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4104 return nfserr_bad_stateid;
4105 /* Client debugging aid. */
4106 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4107 char addr_str[INET6_ADDRSTRLEN];
4108 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4110 pr_warn_ratelimited("NFSD: client %s testing state ID "
4111 "with incorrect client ID\n", addr_str);
4112 return nfserr_bad_stateid;
4114 s = find_stateid(cl, stateid);
4116 return nfserr_bad_stateid;
4117 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
4120 switch (s->sc_type) {
4121 case NFS4_DELEG_STID:
4123 case NFS4_REVOKED_DELEG_STID:
4124 return nfserr_deleg_revoked;
4125 case NFS4_OPEN_STID:
4126 case NFS4_LOCK_STID:
4127 ols = openlockstateid(s);
4128 if (ols->st_stateowner->so_is_open_owner
4129 && !(openowner(ols->st_stateowner)->oo_flags
4130 & NFS4_OO_CONFIRMED))
4131 return nfserr_bad_stateid;
4134 printk("unknown stateid type %x\n", s->sc_type);
4136 case NFS4_CLOSED_STID:
4137 case NFS4_CLOSED_DELEG_STID:
4138 return nfserr_bad_stateid;
4143 nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4144 stateid_t *stateid, unsigned char typemask,
4145 struct nfs4_stid **s, struct nfsd_net *nn)
4149 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4150 return nfserr_bad_stateid;
4151 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
4152 if (status == nfserr_stale_clientid) {
4153 if (cstate->session)
4154 return nfserr_bad_stateid;
4155 return nfserr_stale_stateid;
4159 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
4161 return nfserr_bad_stateid;
4166 * Checks for stateid operations
4169 nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
4170 stateid_t *stateid, int flags, struct file **filpp)
4172 struct nfs4_stid *s;
4173 struct nfs4_ol_stateid *stp = NULL;
4174 struct nfs4_delegation *dp = NULL;
4175 struct svc_fh *current_fh = &cstate->current_fh;
4176 struct inode *ino = current_fh->fh_dentry->d_inode;
4177 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4178 struct file *file = NULL;
4184 if (grace_disallows_io(net, ino))
4185 return nfserr_grace;
4187 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4188 return check_special_stateids(net, current_fh, stateid, flags);
4192 status = nfsd4_lookup_stateid(cstate, stateid,
4193 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
4197 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4200 switch (s->sc_type) {
4201 case NFS4_DELEG_STID:
4202 dp = delegstateid(s);
4203 status = nfs4_check_delegmode(dp, flags);
4207 file = dp->dl_stid.sc_file->fi_deleg_file;
4210 status = nfserr_serverfault;
4216 case NFS4_OPEN_STID:
4217 case NFS4_LOCK_STID:
4218 stp = openlockstateid(s);
4219 status = nfs4_check_fh(current_fh, stp);
4222 if (stp->st_stateowner->so_is_open_owner
4223 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
4225 status = nfs4_check_openmode(stp, flags);
4229 struct nfs4_file *fp = stp->st_stid.sc_file;
4231 if (flags & RD_STATE)
4232 file = find_readable_file(fp);
4234 file = find_writeable_file(fp);
4238 status = nfserr_bad_stateid;
4245 nfs4_unlock_state();
4250 nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
4252 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
4254 if (check_for_locks(stp->st_stid.sc_file, lo))
4255 return nfserr_locks_held;
4256 release_lockowner_if_empty(lo);
4261 * Test if the stateid is valid
4264 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4265 struct nfsd4_test_stateid *test_stateid)
4267 struct nfsd4_test_stateid_id *stateid;
4268 struct nfs4_client *cl = cstate->session->se_client;
4271 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
4272 stateid->ts_id_status =
4273 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
4274 nfs4_unlock_state();
4280 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4281 struct nfsd4_free_stateid *free_stateid)
4283 stateid_t *stateid = &free_stateid->fr_stateid;
4284 struct nfs4_stid *s;
4285 struct nfs4_delegation *dp;
4286 struct nfs4_client *cl = cstate->session->se_client;
4287 __be32 ret = nfserr_bad_stateid;
4290 s = find_stateid(cl, stateid);
4293 switch (s->sc_type) {
4294 case NFS4_DELEG_STID:
4295 ret = nfserr_locks_held;
4297 case NFS4_OPEN_STID:
4298 case NFS4_LOCK_STID:
4299 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4302 if (s->sc_type == NFS4_LOCK_STID)
4303 ret = nfsd4_free_lock_stateid(openlockstateid(s));
4305 ret = nfserr_locks_held;
4307 case NFS4_REVOKED_DELEG_STID:
4308 dp = delegstateid(s);
4309 spin_lock(&cl->cl_lock);
4310 list_del_init(&dp->dl_recall_lru);
4311 spin_unlock(&cl->cl_lock);
4316 ret = nfserr_bad_stateid;
4319 nfs4_unlock_state();
4326 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4327 RD_STATE : WR_STATE;
4330 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
4332 struct svc_fh *current_fh = &cstate->current_fh;
4333 struct nfs4_stateowner *sop = stp->st_stateowner;
4336 status = nfsd4_check_seqid(cstate, sop, seqid);
4339 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4340 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
4342 * "Closed" stateid's exist *only* to return
4343 * nfserr_replay_me from the previous step, and
4344 * revoked delegations are kept only for free_stateid.
4346 return nfserr_bad_stateid;
4347 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4350 return nfs4_check_fh(current_fh, stp);
4354 * Checks for sequence id mutating operations.
4357 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4358 stateid_t *stateid, char typemask,
4359 struct nfs4_ol_stateid **stpp,
4360 struct nfsd_net *nn)
4363 struct nfs4_stid *s;
4364 struct nfs4_ol_stateid *stp = NULL;
4366 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4367 seqid, STATEID_VAL(stateid));
4370 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
4373 stp = openlockstateid(s);
4374 if (!nfsd4_has_session(cstate))
4375 cstate->replay_owner = stp->st_stateowner;
4377 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
4383 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4384 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
4387 struct nfs4_openowner *oo;
4389 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
4390 NFS4_OPEN_STID, stpp, nn);
4393 oo = openowner((*stpp)->st_stateowner);
4394 if (!(oo->oo_flags & NFS4_OO_CONFIRMED))
4395 return nfserr_bad_stateid;
4400 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4401 struct nfsd4_open_confirm *oc)
4404 struct nfs4_openowner *oo;
4405 struct nfs4_ol_stateid *stp;
4406 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4408 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4409 cstate->current_fh.fh_dentry);
4411 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
4417 status = nfs4_preprocess_seqid_op(cstate,
4418 oc->oc_seqid, &oc->oc_req_stateid,
4419 NFS4_OPEN_STID, &stp, nn);
4422 oo = openowner(stp->st_stateowner);
4423 status = nfserr_bad_stateid;
4424 if (oo->oo_flags & NFS4_OO_CONFIRMED)
4426 oo->oo_flags |= NFS4_OO_CONFIRMED;
4427 update_stateid(&stp->st_stid.sc_stateid);
4428 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4429 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
4430 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
4432 nfsd4_client_record_create(oo->oo_owner.so_client);
4435 nfsd4_bump_seqid(cstate, status);
4436 if (!cstate->replay_owner)
4437 nfs4_unlock_state();
4441 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
4443 if (!test_access(access, stp))
4445 nfs4_file_put_access(stp->st_stid.sc_file, access);
4446 clear_access(access, stp);
4449 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4451 switch (to_access) {
4452 case NFS4_SHARE_ACCESS_READ:
4453 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4454 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4456 case NFS4_SHARE_ACCESS_WRITE:
4457 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4458 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4460 case NFS4_SHARE_ACCESS_BOTH:
4468 nfsd4_open_downgrade(struct svc_rqst *rqstp,
4469 struct nfsd4_compound_state *cstate,
4470 struct nfsd4_open_downgrade *od)
4473 struct nfs4_ol_stateid *stp;
4474 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4476 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4477 cstate->current_fh.fh_dentry);
4479 /* We don't yet support WANT bits: */
4480 if (od->od_deleg_want)
4481 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4485 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
4486 &od->od_stateid, &stp, nn);
4489 status = nfserr_inval;
4490 if (!test_access(od->od_share_access, stp)) {
4491 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
4492 stp->st_access_bmap, od->od_share_access);
4495 if (!test_deny(od->od_share_deny, stp)) {
4496 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
4497 stp->st_deny_bmap, od->od_share_deny);
4500 nfs4_stateid_downgrade(stp, od->od_share_access);
4502 reset_union_bmap_deny(od->od_share_deny, stp);
4504 update_stateid(&stp->st_stid.sc_stateid);
4505 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4508 nfsd4_bump_seqid(cstate, status);
4509 if (!cstate->replay_owner)
4510 nfs4_unlock_state();
4514 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4516 struct nfs4_client *clp = s->st_stid.sc_client;
4517 struct nfs4_openowner *oo = openowner(s->st_stateowner);
4519 s->st_stid.sc_type = NFS4_CLOSED_STID;
4520 unhash_open_stateid(s);
4522 if (clp->cl_minorversion) {
4523 if (list_empty(&oo->oo_owner.so_stateids))
4524 release_openowner(oo);
4525 nfs4_put_stid(&s->st_stid);
4528 * In the 4.0 case we need to keep the owners around a
4529 * little while to handle CLOSE replay. We still do need
4530 * to release any file access that is held by them
4531 * before returning however.
4533 release_all_access(s);
4534 if (s->st_stid.sc_file) {
4535 put_nfs4_file(s->st_stid.sc_file);
4536 s->st_stid.sc_file = NULL;
4538 oo->oo_last_closed_stid = s;
4539 if (list_empty(&oo->oo_owner.so_stateids))
4540 move_to_close_lru(oo, clp->net);
4545 * nfs4_unlock_state() called after encode
4548 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4549 struct nfsd4_close *close)
4552 struct nfs4_ol_stateid *stp;
4553 struct net *net = SVC_NET(rqstp);
4554 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4556 dprintk("NFSD: nfsd4_close on file %pd\n",
4557 cstate->current_fh.fh_dentry);
4560 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4562 NFS4_OPEN_STID|NFS4_CLOSED_STID,
4564 nfsd4_bump_seqid(cstate, status);
4567 update_stateid(&stp->st_stid.sc_stateid);
4568 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4570 nfsd4_close_open_stateid(stp);
4572 if (!cstate->replay_owner)
4573 nfs4_unlock_state();
4578 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4579 struct nfsd4_delegreturn *dr)
4581 struct nfs4_delegation *dp;
4582 stateid_t *stateid = &dr->dr_stateid;
4583 struct nfs4_stid *s;
4585 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4587 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
4591 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
4594 dp = delegstateid(s);
4595 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
4599 destroy_delegation(dp);
4601 nfs4_unlock_state();
4607 #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
4610 end_offset(u64 start, u64 len)
4615 return end >= start ? end: NFS4_MAX_UINT64;
4618 /* last octet in a range */
4620 last_byte_offset(u64 start, u64 len)
4626 return end > start ? end - 1: NFS4_MAX_UINT64;
4630 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4631 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4632 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4633 * locking, this prevents us from being completely protocol-compliant. The
4634 * real solution to this problem is to start using unsigned file offsets in
4635 * the VFS, but this is a very deep change!
4638 nfs4_transform_lock_offset(struct file_lock *lock)
4640 if (lock->fl_start < 0)
4641 lock->fl_start = OFFSET_MAX;
4642 if (lock->fl_end < 0)
4643 lock->fl_end = OFFSET_MAX;
4646 /* Hack!: For now, we're defining this just so we can use a pointer to it
4647 * as a unique cookie to identify our (NFSv4's) posix locks. */
4648 static const struct lock_manager_operations nfsd_posix_mng_ops = {
4652 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4654 struct nfs4_lockowner *lo;
4656 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
4657 lo = (struct nfs4_lockowner *) fl->fl_owner;
4658 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4659 lo->lo_owner.so_owner.len, GFP_KERNEL);
4660 if (!deny->ld_owner.data)
4661 /* We just don't care that much */
4663 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4664 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
4667 deny->ld_owner.len = 0;
4668 deny->ld_owner.data = NULL;
4669 deny->ld_clientid.cl_boot = 0;
4670 deny->ld_clientid.cl_id = 0;
4672 deny->ld_start = fl->fl_start;
4673 deny->ld_length = NFS4_MAX_UINT64;
4674 if (fl->fl_end != NFS4_MAX_UINT64)
4675 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4676 deny->ld_type = NFS4_READ_LT;
4677 if (fl->fl_type != F_RDLCK)
4678 deny->ld_type = NFS4_WRITE_LT;
4681 static struct nfs4_lockowner *
4682 find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
4683 struct nfsd_net *nn)
4685 unsigned int strhashval = ownerstr_hashval(clid->cl_id, owner);
4686 struct nfs4_stateowner *so;
4688 list_for_each_entry(so, &nn->ownerstr_hashtbl[strhashval], so_strhash) {
4689 if (so->so_is_open_owner)
4691 if (!same_owner_str(so, owner, clid))
4693 return lockowner(so);
4699 * Alloc a lock owner structure.
4700 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
4703 * strhashval = ownerstr_hashval
4705 static struct nfs4_lockowner *
4706 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_ol_stateid *open_stp, struct nfsd4_lock *lock) {
4707 struct nfs4_lockowner *lo;
4708 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
4710 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4713 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4714 lo->lo_owner.so_is_open_owner = 0;
4715 /* It is the openowner seqid that will be incremented in encode in the
4716 * case of new lockowners; so increment the lock seqid manually: */
4717 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1;
4718 list_add(&lo->lo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
4723 init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
4724 struct nfs4_file *fp, struct inode *inode,
4725 struct nfs4_ol_stateid *open_stp)
4727 struct nfs4_client *clp = lo->lo_owner.so_client;
4729 lockdep_assert_held(&clp->cl_lock);
4731 stp->st_stid.sc_type = NFS4_LOCK_STID;
4732 stp->st_stateowner = &lo->lo_owner;
4734 stp->st_stid.sc_file = fp;
4735 stp->st_stid.sc_free = nfs4_free_lock_stateid;
4736 stp->st_access_bmap = 0;
4737 stp->st_deny_bmap = open_stp->st_deny_bmap;
4738 stp->st_openstp = open_stp;
4739 list_add(&stp->st_locks, &open_stp->st_locks);
4740 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
4741 spin_lock(&fp->fi_lock);
4742 list_add(&stp->st_perfile, &fp->fi_stateids);
4743 spin_unlock(&fp->fi_lock);
4746 static struct nfs4_ol_stateid *
4747 find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
4749 struct nfs4_ol_stateid *lst;
4750 struct nfs4_client *clp = lo->lo_owner.so_client;
4752 lockdep_assert_held(&clp->cl_lock);
4754 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
4755 if (lst->st_stid.sc_file == fp)
4761 static struct nfs4_ol_stateid *
4762 find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
4763 struct inode *inode, struct nfs4_ol_stateid *ost,
4766 struct nfs4_stid *ns = NULL;
4767 struct nfs4_ol_stateid *lst;
4768 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
4769 struct nfs4_client *clp = oo->oo_owner.so_client;
4771 spin_lock(&clp->cl_lock);
4772 lst = find_lock_stateid(lo, fi);
4774 spin_unlock(&clp->cl_lock);
4775 ns = nfs4_alloc_stid(clp, stateid_slab);
4779 spin_lock(&clp->cl_lock);
4780 lst = find_lock_stateid(lo, fi);
4782 lst = openlockstateid(ns);
4783 init_lock_stateid(lst, lo, fi, inode, ost);
4788 spin_unlock(&clp->cl_lock);
4795 check_lock_length(u64 offset, u64 length)
4797 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
4798 LOFF_OVERFLOW(offset, length)));
4801 static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
4803 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
4805 lockdep_assert_held(&fp->fi_lock);
4807 if (test_access(access, lock_stp))
4809 __nfs4_file_get_access(fp, access);
4810 set_access(access, lock_stp);
4814 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
4815 struct nfs4_ol_stateid *ost,
4816 struct nfsd4_lock *lock,
4817 struct nfs4_ol_stateid **lst, bool *new)
4819 struct nfs4_file *fi = ost->st_stid.sc_file;
4820 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
4821 struct nfs4_client *cl = oo->oo_owner.so_client;
4822 struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
4823 struct nfs4_lockowner *lo;
4824 unsigned int strhashval;
4825 struct nfsd_net *nn = net_generic(cl->net, nfsd_net_id);
4827 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, nn);
4829 strhashval = ownerstr_hashval(cl->cl_clientid.cl_id,
4830 &lock->v.new.owner);
4831 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
4833 return nfserr_jukebox;
4835 /* with an existing lockowner, seqids must be the same */
4836 if (!cstate->minorversion &&
4837 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
4838 return nfserr_bad_seqid;
4841 *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
4843 release_lockowner_if_empty(lo);
4844 return nfserr_jukebox;
4853 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4854 struct nfsd4_lock *lock)
4856 struct nfs4_openowner *open_sop = NULL;
4857 struct nfs4_lockowner *lock_sop = NULL;
4858 struct nfs4_ol_stateid *lock_stp;
4859 struct nfs4_file *fp;
4860 struct file *filp = NULL;
4861 struct file_lock *file_lock = NULL;
4862 struct file_lock *conflock = NULL;
4864 bool new_state = false;
4867 struct net *net = SVC_NET(rqstp);
4868 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4870 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
4871 (long long) lock->lk_offset,
4872 (long long) lock->lk_length);
4874 if (check_lock_length(lock->lk_offset, lock->lk_length))
4875 return nfserr_inval;
4877 if ((status = fh_verify(rqstp, &cstate->current_fh,
4878 S_IFREG, NFSD_MAY_LOCK))) {
4879 dprintk("NFSD: nfsd4_lock: permission denied!\n");
4885 if (lock->lk_is_new) {
4886 struct nfs4_ol_stateid *open_stp = NULL;
4888 if (nfsd4_has_session(cstate))
4889 /* See rfc 5661 18.10.3: given clientid is ignored: */
4890 memcpy(&lock->v.new.clientid,
4891 &cstate->session->se_client->cl_clientid,
4892 sizeof(clientid_t));
4894 status = nfserr_stale_clientid;
4895 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
4898 /* validate and update open stateid and open seqid */
4899 status = nfs4_preprocess_confirmed_seqid_op(cstate,
4900 lock->lk_new_open_seqid,
4901 &lock->lk_new_open_stateid,
4905 open_sop = openowner(open_stp->st_stateowner);
4906 status = nfserr_bad_stateid;
4907 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
4908 &lock->v.new.clientid))
4910 status = lookup_or_create_lock_state(cstate, open_stp, lock,
4911 &lock_stp, &new_state);
4913 status = nfs4_preprocess_seqid_op(cstate,
4914 lock->lk_old_lock_seqid,
4915 &lock->lk_old_lock_stateid,
4916 NFS4_LOCK_STID, &lock_stp, nn);
4919 lock_sop = lockowner(lock_stp->st_stateowner);
4921 lkflg = setlkflg(lock->lk_type);
4922 status = nfs4_check_openmode(lock_stp, lkflg);
4926 status = nfserr_grace;
4927 if (locks_in_grace(net) && !lock->lk_reclaim)
4929 status = nfserr_no_grace;
4930 if (!locks_in_grace(net) && lock->lk_reclaim)
4933 file_lock = locks_alloc_lock();
4935 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4936 status = nfserr_jukebox;
4940 fp = lock_stp->st_stid.sc_file;
4941 locks_init_lock(file_lock);
4942 switch (lock->lk_type) {
4945 spin_lock(&fp->fi_lock);
4946 filp = find_readable_file_locked(fp);
4948 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
4949 spin_unlock(&fp->fi_lock);
4950 file_lock->fl_type = F_RDLCK;
4953 case NFS4_WRITEW_LT:
4954 spin_lock(&fp->fi_lock);
4955 filp = find_writeable_file_locked(fp);
4957 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
4958 spin_unlock(&fp->fi_lock);
4959 file_lock->fl_type = F_WRLCK;
4962 status = nfserr_inval;
4966 status = nfserr_openmode;
4969 file_lock->fl_owner = (fl_owner_t)lock_sop;
4970 file_lock->fl_pid = current->tgid;
4971 file_lock->fl_file = filp;
4972 file_lock->fl_flags = FL_POSIX;
4973 file_lock->fl_lmops = &nfsd_posix_mng_ops;
4974 file_lock->fl_start = lock->lk_offset;
4975 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
4976 nfs4_transform_lock_offset(file_lock);
4978 conflock = locks_alloc_lock();
4980 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4981 status = nfserr_jukebox;
4985 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
4987 case 0: /* success! */
4988 update_stateid(&lock_stp->st_stid.sc_stateid);
4989 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
4993 case (EAGAIN): /* conflock holds conflicting lock */
4994 status = nfserr_denied;
4995 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
4996 nfs4_set_lock_denied(conflock, &lock->lk_denied);
4999 status = nfserr_deadlock;
5002 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
5003 status = nfserrno(err);
5009 if (status && new_state)
5010 release_lock_stateid(lock_stp);
5011 nfsd4_bump_seqid(cstate, status);
5012 if (!cstate->replay_owner)
5013 nfs4_unlock_state();
5015 locks_free_lock(file_lock);
5017 locks_free_lock(conflock);
5022 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5023 * so we do a temporary open here just to get an open file to pass to
5024 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5027 static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
5030 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5032 err = nfserrno(vfs_test_lock(file, lock));
5042 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5043 struct nfsd4_lockt *lockt)
5045 struct file_lock *file_lock = NULL;
5046 struct nfs4_lockowner *lo;
5048 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5050 if (locks_in_grace(SVC_NET(rqstp)))
5051 return nfserr_grace;
5053 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5054 return nfserr_inval;
5058 if (!nfsd4_has_session(cstate)) {
5059 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
5064 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
5067 file_lock = locks_alloc_lock();
5069 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5070 status = nfserr_jukebox;
5073 locks_init_lock(file_lock);
5074 switch (lockt->lt_type) {
5077 file_lock->fl_type = F_RDLCK;
5080 case NFS4_WRITEW_LT:
5081 file_lock->fl_type = F_WRLCK;
5084 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
5085 status = nfserr_inval;
5089 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner, nn);
5091 file_lock->fl_owner = (fl_owner_t)lo;
5092 file_lock->fl_pid = current->tgid;
5093 file_lock->fl_flags = FL_POSIX;
5095 file_lock->fl_start = lockt->lt_offset;
5096 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
5098 nfs4_transform_lock_offset(file_lock);
5100 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
5104 if (file_lock->fl_type != F_UNLCK) {
5105 status = nfserr_denied;
5106 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
5109 nfs4_unlock_state();
5111 locks_free_lock(file_lock);
5116 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5117 struct nfsd4_locku *locku)
5119 struct nfs4_ol_stateid *stp;
5120 struct file *filp = NULL;
5121 struct file_lock *file_lock = NULL;
5124 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5126 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5127 (long long) locku->lu_offset,
5128 (long long) locku->lu_length);
5130 if (check_lock_length(locku->lu_offset, locku->lu_length))
5131 return nfserr_inval;
5135 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
5136 &locku->lu_stateid, NFS4_LOCK_STID,
5140 filp = find_any_file(stp->st_stid.sc_file);
5142 status = nfserr_lock_range;
5145 file_lock = locks_alloc_lock();
5147 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5148 status = nfserr_jukebox;
5151 locks_init_lock(file_lock);
5152 file_lock->fl_type = F_UNLCK;
5153 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
5154 file_lock->fl_pid = current->tgid;
5155 file_lock->fl_file = filp;
5156 file_lock->fl_flags = FL_POSIX;
5157 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5158 file_lock->fl_start = locku->lu_offset;
5160 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5162 nfs4_transform_lock_offset(file_lock);
5164 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
5166 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
5169 update_stateid(&stp->st_stid.sc_stateid);
5170 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
5174 nfsd4_bump_seqid(cstate, status);
5175 if (!cstate->replay_owner)
5176 nfs4_unlock_state();
5178 locks_free_lock(file_lock);
5182 status = nfserrno(err);
5188 * true: locks held by lockowner
5189 * false: no locks held by lockowner
5192 check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
5194 struct file_lock **flpp;
5196 struct file *filp = find_any_file(fp);
5197 struct inode *inode;
5200 /* Any valid lock stateid should have some sort of access */
5205 inode = file_inode(filp);
5207 spin_lock(&inode->i_lock);
5208 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
5209 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
5214 spin_unlock(&inode->i_lock);
5220 nfsd4_release_lockowner(struct svc_rqst *rqstp,
5221 struct nfsd4_compound_state *cstate,
5222 struct nfsd4_release_lockowner *rlockowner)
5224 clientid_t *clid = &rlockowner->rl_clientid;
5225 struct nfs4_stateowner *sop = NULL, *tmp;
5226 struct nfs4_lockowner *lo;
5227 struct nfs4_ol_stateid *stp;
5228 struct xdr_netobj *owner = &rlockowner->rl_owner;
5229 unsigned int hashval = ownerstr_hashval(clid->cl_id, owner);
5231 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5233 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5234 clid->cl_boot, clid->cl_id);
5238 status = lookup_clientid(clid, cstate, nn);
5242 status = nfserr_locks_held;
5244 /* Find the matching lock stateowner */
5245 list_for_each_entry(tmp, &nn->ownerstr_hashtbl[hashval], so_strhash) {
5246 if (tmp->so_is_open_owner)
5248 if (same_owner_str(tmp, owner, clid)) {
5254 /* No matching owner found, maybe a replay? Just declare victory... */
5260 lo = lockowner(sop);
5261 /* see if there are still any locks associated with it */
5262 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5263 if (check_for_locks(stp->st_stid.sc_file, lo))
5268 release_lockowner(lo);
5270 nfs4_unlock_state();
5274 static inline struct nfs4_client_reclaim *
5277 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
5281 nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
5283 struct nfs4_client_reclaim *crp;
5285 crp = nfsd4_find_reclaim_client(name, nn);
5286 return (crp && crp->cr_clp);
5290 * failure => all reset bets are off, nfserr_no_grace...
5292 struct nfs4_client_reclaim *
5293 nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
5295 unsigned int strhashval;
5296 struct nfs4_client_reclaim *crp;
5298 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5299 crp = alloc_reclaim();
5301 strhashval = clientstr_hashval(name);
5302 INIT_LIST_HEAD(&crp->cr_strhash);
5303 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
5304 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
5306 nn->reclaim_str_hashtbl_size++;
5312 nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
5314 list_del(&crp->cr_strhash);
5316 nn->reclaim_str_hashtbl_size--;
5320 nfs4_release_reclaim(struct nfsd_net *nn)
5322 struct nfs4_client_reclaim *crp = NULL;
5325 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5326 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5327 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
5328 struct nfs4_client_reclaim, cr_strhash);
5329 nfs4_remove_reclaim_record(crp, nn);
5332 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
5336 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
5337 struct nfs4_client_reclaim *
5338 nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
5340 unsigned int strhashval;
5341 struct nfs4_client_reclaim *crp = NULL;
5343 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
5345 strhashval = clientstr_hashval(recdir);
5346 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
5347 if (same_name(crp->cr_recdir, recdir)) {
5355 * Called from OPEN. Look for clientid in reclaim list.
5358 nfs4_check_open_reclaim(clientid_t *clid,
5359 struct nfsd4_compound_state *cstate,
5360 struct nfsd_net *nn)
5364 /* find clientid in conf_id_hashtbl */
5365 status = lookup_clientid(clid, cstate, nn);
5367 return nfserr_reclaim_bad;
5369 if (nfsd4_client_record_check(cstate->clp))
5370 return nfserr_reclaim_bad;
5375 #ifdef CONFIG_NFSD_FAULT_INJECTION
5377 u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
5379 if (mark_client_expired(clp))
5385 u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
5387 char buf[INET6_ADDRSTRLEN];
5388 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5389 printk(KERN_INFO "NFS Client: %s\n", buf);
5393 static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5396 char buf[INET6_ADDRSTRLEN];
5397 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5398 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5401 static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
5402 void (*func)(struct nfs4_ol_stateid *))
5404 struct nfs4_openowner *oop;
5405 struct nfs4_ol_stateid *stp, *st_next;
5406 struct nfs4_ol_stateid *lst, *lst_next;
5409 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
5410 list_for_each_entry_safe(stp, st_next,
5411 &oop->oo_owner.so_stateids, st_perstateowner) {
5412 list_for_each_entry_safe(lst, lst_next,
5413 &stp->st_locks, st_locks) {
5425 u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
5427 return nfsd_foreach_client_lock(clp, max, release_lock_stateid);
5430 u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
5432 u64 count = nfsd_foreach_client_lock(clp, max, NULL);
5433 nfsd_print_count(clp, count, "locked files");
5437 static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
5439 struct nfs4_openowner *oop, *next;
5442 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
5452 u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
5454 return nfsd_foreach_client_open(clp, max, release_openowner);
5457 u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
5459 u64 count = nfsd_foreach_client_open(clp, max, NULL);
5460 nfsd_print_count(clp, count, "open files");
5464 static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
5465 struct list_head *victims)
5467 struct nfs4_delegation *dp, *next;
5470 lockdep_assert_held(&state_lock);
5471 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
5474 * It's not safe to mess with delegations that have a
5475 * non-zero dl_time. They might have already been broken
5476 * and could be processed by the laundromat outside of
5477 * the state_lock. Just leave them be.
5479 if (dp->dl_time != 0)
5482 unhash_delegation_locked(dp);
5483 list_add(&dp->dl_recall_lru, victims);
5491 u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
5493 struct nfs4_delegation *dp, *next;
5497 spin_lock(&state_lock);
5498 count = nfsd_find_all_delegations(clp, max, &victims);
5499 spin_unlock(&state_lock);
5501 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru) {
5502 list_del_init(&dp->dl_recall_lru);
5503 revoke_delegation(dp);
5509 u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
5511 struct nfs4_delegation *dp;
5515 spin_lock(&state_lock);
5516 count = nfsd_find_all_delegations(clp, max, &victims);
5517 while (!list_empty(&victims)) {
5518 dp = list_first_entry(&victims, struct nfs4_delegation,
5520 list_del_init(&dp->dl_recall_lru);
5522 nfsd_break_one_deleg(dp);
5524 spin_unlock(&state_lock);
5529 u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
5533 spin_lock(&state_lock);
5534 count = nfsd_find_all_delegations(clp, max, NULL);
5535 spin_unlock(&state_lock);
5537 nfsd_print_count(clp, count, "delegations");
5541 u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
5543 struct nfs4_client *clp, *next;
5545 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
5547 if (!nfsd_netns_ready(nn))
5550 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
5551 count += func(clp, max - count);
5552 if ((max != 0) && (count >= max))
5559 struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5561 struct nfs4_client *clp;
5562 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
5564 if (!nfsd_netns_ready(nn))
5567 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5568 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5574 #endif /* CONFIG_NFSD_FAULT_INJECTION */
5577 * Since the lifetime of a delegation isn't limited to that of an open, a
5578 * client may quite reasonably hang on to a delegation as long as it has
5579 * the inode cached. This becomes an obvious problem the first time a
5580 * client's inode cache approaches the size of the server's total memory.
5582 * For now we avoid this problem by imposing a hard limit on the number
5583 * of delegations, which varies according to the server's memory size.
5586 set_max_delegations(void)
5589 * Allow at most 4 delegations per megabyte of RAM. Quick
5590 * estimates suggest that in the worst case (where every delegation
5591 * is for a different inode), a delegation could take about 1.5K,
5592 * giving a worst case usage of about 6% of memory.
5594 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
5597 static int nfs4_state_create_net(struct net *net)
5599 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5602 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5603 CLIENT_HASH_SIZE, GFP_KERNEL);
5604 if (!nn->conf_id_hashtbl)
5606 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5607 CLIENT_HASH_SIZE, GFP_KERNEL);
5608 if (!nn->unconf_id_hashtbl)
5610 nn->ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
5611 OWNER_HASH_SIZE, GFP_KERNEL);
5612 if (!nn->ownerstr_hashtbl)
5614 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
5615 SESSION_HASH_SIZE, GFP_KERNEL);
5616 if (!nn->sessionid_hashtbl)
5619 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5620 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
5621 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
5623 for (i = 0; i < OWNER_HASH_SIZE; i++)
5624 INIT_LIST_HEAD(&nn->ownerstr_hashtbl[i]);
5625 for (i = 0; i < SESSION_HASH_SIZE; i++)
5626 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
5627 nn->conf_name_tree = RB_ROOT;
5628 nn->unconf_name_tree = RB_ROOT;
5629 INIT_LIST_HEAD(&nn->client_lru);
5630 INIT_LIST_HEAD(&nn->close_lru);
5631 INIT_LIST_HEAD(&nn->del_recall_lru);
5632 spin_lock_init(&nn->client_lock);
5634 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
5640 kfree(nn->ownerstr_hashtbl);
5642 kfree(nn->unconf_id_hashtbl);
5644 kfree(nn->conf_id_hashtbl);
5650 nfs4_state_destroy_net(struct net *net)
5653 struct nfs4_client *clp = NULL;
5654 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5656 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5657 while (!list_empty(&nn->conf_id_hashtbl[i])) {
5658 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5659 destroy_client(clp);
5663 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5664 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
5665 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5666 destroy_client(clp);
5670 kfree(nn->sessionid_hashtbl);
5671 kfree(nn->ownerstr_hashtbl);
5672 kfree(nn->unconf_id_hashtbl);
5673 kfree(nn->conf_id_hashtbl);
5678 nfs4_state_start_net(struct net *net)
5680 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5683 ret = nfs4_state_create_net(net);
5686 nfsd4_client_tracking_init(net);
5687 nn->boot_time = get_seconds();
5688 locks_start_grace(net, &nn->nfsd4_manager);
5689 nn->grace_ended = false;
5690 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
5691 nn->nfsd4_grace, net);
5692 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
5696 /* initialization to perform when the nfsd service is started: */
5699 nfs4_state_start(void)
5703 ret = set_callback_cred();
5706 laundry_wq = create_singlethread_workqueue("nfsd4");
5707 if (laundry_wq == NULL) {
5711 ret = nfsd4_create_callback_queue();
5713 goto out_free_laundry;
5715 set_max_delegations();
5720 destroy_workqueue(laundry_wq);
5726 nfs4_state_shutdown_net(struct net *net)
5728 struct nfs4_delegation *dp = NULL;
5729 struct list_head *pos, *next, reaplist;
5730 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5732 cancel_delayed_work_sync(&nn->laundromat_work);
5733 locks_end_grace(&nn->nfsd4_manager);
5736 INIT_LIST_HEAD(&reaplist);
5737 spin_lock(&state_lock);
5738 list_for_each_safe(pos, next, &nn->del_recall_lru) {
5739 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
5740 unhash_delegation_locked(dp);
5741 list_add(&dp->dl_recall_lru, &reaplist);
5743 spin_unlock(&state_lock);
5744 list_for_each_safe(pos, next, &reaplist) {
5745 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
5746 list_del_init(&dp->dl_recall_lru);
5747 nfs4_put_stid(&dp->dl_stid);
5750 nfsd4_client_tracking_exit(net);
5751 nfs4_state_destroy_net(net);
5752 nfs4_unlock_state();
5756 nfs4_state_shutdown(void)
5758 destroy_workqueue(laundry_wq);
5759 nfsd4_destroy_callback_queue();
5763 get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5765 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
5766 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
5770 put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5772 if (cstate->minorversion) {
5773 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
5774 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
5779 clear_current_stateid(struct nfsd4_compound_state *cstate)
5781 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
5785 * functions to set current state id
5788 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5790 put_stateid(cstate, &odp->od_stateid);
5794 nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
5796 put_stateid(cstate, &open->op_stateid);
5800 nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5802 put_stateid(cstate, &close->cl_stateid);
5806 nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
5808 put_stateid(cstate, &lock->lk_resp_stateid);
5812 * functions to consume current state id
5816 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5818 get_stateid(cstate, &odp->od_stateid);
5822 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
5824 get_stateid(cstate, &drp->dr_stateid);
5828 nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
5830 get_stateid(cstate, &fsp->fr_stateid);
5834 nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
5836 get_stateid(cstate, &setattr->sa_stateid);
5840 nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5842 get_stateid(cstate, &close->cl_stateid);
5846 nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
5848 get_stateid(cstate, &locku->lu_stateid);
5852 nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
5854 get_stateid(cstate, &read->rd_stateid);
5858 nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
5860 get_stateid(cstate, &write->wr_stateid);