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 atomic_inc(&dp->dl_stid.sc_count);
678 dp->dl_stid.sc_type = NFS4_DELEG_STID;
679 list_add(&dp->dl_perfile, &fp->fi_delegations);
680 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
684 unhash_delegation_locked(struct nfs4_delegation *dp)
686 struct nfs4_file *fp = dp->dl_stid.sc_file;
688 lockdep_assert_held(&state_lock);
690 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
691 /* Ensure that deleg break won't try to requeue it */
693 spin_lock(&fp->fi_lock);
694 list_del_init(&dp->dl_perclnt);
695 list_del_init(&dp->dl_recall_lru);
696 list_del_init(&dp->dl_perfile);
697 spin_unlock(&fp->fi_lock);
699 nfs4_put_deleg_lease(fp);
702 static void destroy_delegation(struct nfs4_delegation *dp)
704 spin_lock(&state_lock);
705 unhash_delegation_locked(dp);
706 spin_unlock(&state_lock);
707 nfs4_put_stid(&dp->dl_stid);
710 static void revoke_delegation(struct nfs4_delegation *dp)
712 struct nfs4_client *clp = dp->dl_stid.sc_client;
714 WARN_ON(!list_empty(&dp->dl_recall_lru));
716 if (clp->cl_minorversion == 0)
717 nfs4_put_stid(&dp->dl_stid);
719 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
720 spin_lock(&clp->cl_lock);
721 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
722 spin_unlock(&clp->cl_lock);
730 static unsigned int clientid_hashval(u32 id)
732 return id & CLIENT_HASH_MASK;
735 static unsigned int clientstr_hashval(const char *name)
737 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
741 * We store the NONE, READ, WRITE, and BOTH bits separately in the
742 * st_{access,deny}_bmap field of the stateid, in order to track not
743 * only what share bits are currently in force, but also what
744 * combinations of share bits previous opens have used. This allows us
745 * to enforce the recommendation of rfc 3530 14.2.19 that the server
746 * return an error if the client attempt to downgrade to a combination
747 * of share bits not explicable by closing some of its previous opens.
749 * XXX: This enforcement is actually incomplete, since we don't keep
750 * track of access/deny bit combinations; so, e.g., we allow:
752 * OPEN allow read, deny write
753 * OPEN allow both, deny none
754 * DOWNGRADE allow read, deny none
756 * which we should reject.
759 bmap_to_share_mode(unsigned long bmap) {
761 unsigned int access = 0;
763 for (i = 1; i < 4; i++) {
764 if (test_bit(i, &bmap))
770 /* set share access for a given stateid */
772 set_access(u32 access, struct nfs4_ol_stateid *stp)
774 unsigned char mask = 1 << access;
776 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
777 stp->st_access_bmap |= mask;
780 /* clear share access for a given stateid */
782 clear_access(u32 access, struct nfs4_ol_stateid *stp)
784 unsigned char mask = 1 << access;
786 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
787 stp->st_access_bmap &= ~mask;
790 /* test whether a given stateid has access */
792 test_access(u32 access, struct nfs4_ol_stateid *stp)
794 unsigned char mask = 1 << access;
796 return (bool)(stp->st_access_bmap & mask);
799 /* set share deny for a given stateid */
801 set_deny(u32 deny, struct nfs4_ol_stateid *stp)
803 unsigned char mask = 1 << deny;
805 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
806 stp->st_deny_bmap |= mask;
809 /* clear share deny for a given stateid */
811 clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
813 unsigned char mask = 1 << deny;
815 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
816 stp->st_deny_bmap &= ~mask;
819 /* test whether a given stateid is denying specific access */
821 test_deny(u32 deny, struct nfs4_ol_stateid *stp)
823 unsigned char mask = 1 << deny;
825 return (bool)(stp->st_deny_bmap & mask);
828 static int nfs4_access_to_omode(u32 access)
830 switch (access & NFS4_SHARE_ACCESS_BOTH) {
831 case NFS4_SHARE_ACCESS_READ:
833 case NFS4_SHARE_ACCESS_WRITE:
835 case NFS4_SHARE_ACCESS_BOTH:
843 * A stateid that had a deny mode associated with it is being released
844 * or downgraded. Recalculate the deny mode on the file.
847 recalculate_deny_mode(struct nfs4_file *fp)
849 struct nfs4_ol_stateid *stp;
851 spin_lock(&fp->fi_lock);
852 fp->fi_share_deny = 0;
853 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
854 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
855 spin_unlock(&fp->fi_lock);
859 reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
864 for (i = 1; i < 4; i++) {
865 if ((i & deny) != i) {
871 /* Recalculate per-file deny mode if there was a change */
873 recalculate_deny_mode(stp->st_stid.sc_file);
876 /* release all access and file references for a given stateid */
878 release_all_access(struct nfs4_ol_stateid *stp)
881 struct nfs4_file *fp = stp->st_stid.sc_file;
883 if (fp && stp->st_deny_bmap != 0)
884 recalculate_deny_mode(fp);
886 for (i = 1; i < 4; i++) {
887 if (test_access(i, stp))
888 nfs4_file_put_access(stp->st_stid.sc_file, i);
889 clear_access(i, stp);
893 static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
895 if (!atomic_dec_and_test(&sop->so_count))
897 sop->so_ops->so_unhash(sop);
898 kfree(sop->so_owner.data);
899 sop->so_ops->so_free(sop);
902 static void unhash_generic_stateid(struct nfs4_ol_stateid *stp)
904 struct nfs4_file *fp = stp->st_stid.sc_file;
906 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
908 spin_lock(&fp->fi_lock);
909 list_del(&stp->st_perfile);
910 spin_unlock(&fp->fi_lock);
911 list_del(&stp->st_perstateowner);
914 static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
916 struct nfs4_ol_stateid *stp = openlockstateid(stid);
918 release_all_access(stp);
919 kmem_cache_free(stateid_slab, stid);
922 static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
924 struct nfs4_ol_stateid *stp = openlockstateid(stid);
925 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
928 file = find_any_file(stp->st_stid.sc_file);
930 filp_close(file, (fl_owner_t)lo);
931 if (stp->st_stateowner)
932 nfs4_put_stateowner(stp->st_stateowner);
933 nfs4_free_ol_stateid(stid);
936 static void release_lock_stateid(struct nfs4_ol_stateid *stp)
938 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
940 spin_lock(&oo->oo_owner.so_client->cl_lock);
941 list_del(&stp->st_locks);
942 unhash_generic_stateid(stp);
943 unhash_stid(&stp->st_stid);
944 spin_unlock(&oo->oo_owner.so_client->cl_lock);
945 nfs4_put_stid(&stp->st_stid);
948 static void unhash_lockowner(struct nfs4_lockowner *lo)
950 list_del_init(&lo->lo_owner.so_strhash);
953 static void release_lockowner_stateids(struct nfs4_lockowner *lo)
955 struct nfs4_ol_stateid *stp;
957 while (!list_empty(&lo->lo_owner.so_stateids)) {
958 stp = list_first_entry(&lo->lo_owner.so_stateids,
959 struct nfs4_ol_stateid, st_perstateowner);
960 release_lock_stateid(stp);
964 static void release_lockowner(struct nfs4_lockowner *lo)
966 unhash_lockowner(lo);
967 release_lockowner_stateids(lo);
968 nfs4_put_stateowner(&lo->lo_owner);
971 static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp)
972 __releases(&open_stp->st_stateowner->so_client->cl_lock)
973 __acquires(&open_stp->st_stateowner->so_client->cl_lock)
975 struct nfs4_ol_stateid *stp;
977 while (!list_empty(&open_stp->st_locks)) {
978 stp = list_entry(open_stp->st_locks.next,
979 struct nfs4_ol_stateid, st_locks);
980 spin_unlock(&open_stp->st_stateowner->so_client->cl_lock);
981 release_lock_stateid(stp);
982 spin_lock(&open_stp->st_stateowner->so_client->cl_lock);
986 static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
988 spin_lock(&stp->st_stateowner->so_client->cl_lock);
989 unhash_generic_stateid(stp);
990 release_open_stateid_locks(stp);
991 spin_unlock(&stp->st_stateowner->so_client->cl_lock);
994 static void release_open_stateid(struct nfs4_ol_stateid *stp)
996 unhash_open_stateid(stp);
997 nfs4_put_stid(&stp->st_stid);
1000 static void unhash_openowner(struct nfs4_openowner *oo)
1002 list_del_init(&oo->oo_owner.so_strhash);
1003 list_del_init(&oo->oo_perclient);
1006 static void release_last_closed_stateid(struct nfs4_openowner *oo)
1008 struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
1011 nfs4_put_stid(&s->st_stid);
1012 oo->oo_last_closed_stid = NULL;
1016 static void release_openowner_stateids(struct nfs4_openowner *oo)
1018 struct nfs4_ol_stateid *stp;
1020 while (!list_empty(&oo->oo_owner.so_stateids)) {
1021 stp = list_first_entry(&oo->oo_owner.so_stateids,
1022 struct nfs4_ol_stateid, st_perstateowner);
1023 release_open_stateid(stp);
1027 static void release_openowner(struct nfs4_openowner *oo)
1029 unhash_openowner(oo);
1030 release_openowner_stateids(oo);
1031 list_del(&oo->oo_close_lru);
1032 release_last_closed_stateid(oo);
1033 nfs4_put_stateowner(&oo->oo_owner);
1037 hash_sessionid(struct nfs4_sessionid *sessionid)
1039 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1041 return sid->sequence % SESSION_HASH_SIZE;
1046 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1048 u32 *ptr = (u32 *)(&sessionid->data[0]);
1049 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1053 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1059 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1060 * won't be used for replay.
1062 void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1064 struct nfs4_stateowner *so = cstate->replay_owner;
1066 if (nfserr == nfserr_replay_me)
1069 if (!seqid_mutating_err(ntohl(nfserr))) {
1070 nfsd4_cstate_clear_replay(cstate);
1075 if (so->so_is_open_owner)
1076 release_last_closed_stateid(openowner(so));
1082 gen_sessionid(struct nfsd4_session *ses)
1084 struct nfs4_client *clp = ses->se_client;
1085 struct nfsd4_sessionid *sid;
1087 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1088 sid->clientid = clp->cl_clientid;
1089 sid->sequence = current_sessionid++;
1094 * The protocol defines ca_maxresponssize_cached to include the size of
1095 * the rpc header, but all we need to cache is the data starting after
1096 * the end of the initial SEQUENCE operation--the rest we regenerate
1097 * each time. Therefore we can advertise a ca_maxresponssize_cached
1098 * value that is the number of bytes in our cache plus a few additional
1099 * bytes. In order to stay on the safe side, and not promise more than
1100 * we can cache, those additional bytes must be the minimum possible: 24
1101 * bytes of rpc header (xid through accept state, with AUTH_NULL
1102 * verifier), 12 for the compound header (with zero-length tag), and 44
1103 * for the SEQUENCE op response:
1105 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1108 free_session_slots(struct nfsd4_session *ses)
1112 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1113 kfree(ses->se_slots[i]);
1117 * We don't actually need to cache the rpc and session headers, so we
1118 * can allocate a little less for each slot:
1120 static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
1124 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1127 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1128 return size + sizeof(struct nfsd4_slot);
1132 * XXX: If we run out of reserved DRC memory we could (up to a point)
1133 * re-negotiate active sessions and reduce their slot usage to make
1134 * room for new connections. For now we just fail the create session.
1136 static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
1138 u32 slotsize = slot_bytes(ca);
1139 u32 num = ca->maxreqs;
1142 spin_lock(&nfsd_drc_lock);
1143 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1144 nfsd_drc_max_mem - nfsd_drc_mem_used);
1145 num = min_t(int, num, avail / slotsize);
1146 nfsd_drc_mem_used += num * slotsize;
1147 spin_unlock(&nfsd_drc_lock);
1152 static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
1154 int slotsize = slot_bytes(ca);
1156 spin_lock(&nfsd_drc_lock);
1157 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
1158 spin_unlock(&nfsd_drc_lock);
1161 static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1162 struct nfsd4_channel_attrs *battrs)
1164 int numslots = fattrs->maxreqs;
1165 int slotsize = slot_bytes(fattrs);
1166 struct nfsd4_session *new;
1169 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
1170 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1171 mem = numslots * sizeof(struct nfsd4_slot *);
1173 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
1176 /* allocate each struct nfsd4_slot and data cache in one piece */
1177 for (i = 0; i < numslots; i++) {
1178 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
1179 if (!new->se_slots[i])
1183 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1184 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1189 kfree(new->se_slots[i]);
1194 static void free_conn(struct nfsd4_conn *c)
1196 svc_xprt_put(c->cn_xprt);
1200 static void nfsd4_conn_lost(struct svc_xpt_user *u)
1202 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1203 struct nfs4_client *clp = c->cn_session->se_client;
1205 spin_lock(&clp->cl_lock);
1206 if (!list_empty(&c->cn_persession)) {
1207 list_del(&c->cn_persession);
1210 nfsd4_probe_callback(clp);
1211 spin_unlock(&clp->cl_lock);
1214 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
1216 struct nfsd4_conn *conn;
1218 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1221 svc_xprt_get(rqstp->rq_xprt);
1222 conn->cn_xprt = rqstp->rq_xprt;
1223 conn->cn_flags = flags;
1224 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1228 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1230 conn->cn_session = ses;
1231 list_add(&conn->cn_persession, &ses->se_conns);
1234 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1236 struct nfs4_client *clp = ses->se_client;
1238 spin_lock(&clp->cl_lock);
1239 __nfsd4_hash_conn(conn, ses);
1240 spin_unlock(&clp->cl_lock);
1243 static int nfsd4_register_conn(struct nfsd4_conn *conn)
1245 conn->cn_xpt_user.callback = nfsd4_conn_lost;
1246 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
1249 static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
1253 nfsd4_hash_conn(conn, ses);
1254 ret = nfsd4_register_conn(conn);
1256 /* oops; xprt is already down: */
1257 nfsd4_conn_lost(&conn->cn_xpt_user);
1258 /* We may have gained or lost a callback channel: */
1259 nfsd4_probe_callback_sync(ses->se_client);
1262 static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
1264 u32 dir = NFS4_CDFC4_FORE;
1266 if (cses->flags & SESSION4_BACK_CHAN)
1267 dir |= NFS4_CDFC4_BACK;
1268 return alloc_conn(rqstp, dir);
1271 /* must be called under client_lock */
1272 static void nfsd4_del_conns(struct nfsd4_session *s)
1274 struct nfs4_client *clp = s->se_client;
1275 struct nfsd4_conn *c;
1277 spin_lock(&clp->cl_lock);
1278 while (!list_empty(&s->se_conns)) {
1279 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1280 list_del_init(&c->cn_persession);
1281 spin_unlock(&clp->cl_lock);
1283 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1286 spin_lock(&clp->cl_lock);
1288 spin_unlock(&clp->cl_lock);
1291 static void __free_session(struct nfsd4_session *ses)
1293 free_session_slots(ses);
1297 static void free_session(struct nfsd4_session *ses)
1299 struct nfsd_net *nn = net_generic(ses->se_client->net, nfsd_net_id);
1301 lockdep_assert_held(&nn->client_lock);
1302 nfsd4_del_conns(ses);
1303 nfsd4_put_drc_mem(&ses->se_fchannel);
1304 __free_session(ses);
1307 static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
1310 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1312 new->se_client = clp;
1315 INIT_LIST_HEAD(&new->se_conns);
1317 new->se_cb_seq_nr = 1;
1318 new->se_flags = cses->flags;
1319 new->se_cb_prog = cses->callback_prog;
1320 new->se_cb_sec = cses->cb_sec;
1321 atomic_set(&new->se_ref, 0);
1322 idx = hash_sessionid(&new->se_sessionid);
1323 spin_lock(&nn->client_lock);
1324 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
1325 spin_lock(&clp->cl_lock);
1326 list_add(&new->se_perclnt, &clp->cl_sessions);
1327 spin_unlock(&clp->cl_lock);
1328 spin_unlock(&nn->client_lock);
1330 if (cses->flags & SESSION4_BACK_CHAN) {
1331 struct sockaddr *sa = svc_addr(rqstp);
1333 * This is a little silly; with sessions there's no real
1334 * use for the callback address. Use the peer address
1335 * as a reasonable default for now, but consider fixing
1336 * the rpc client not to require an address in the
1339 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1340 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
1344 /* caller must hold client_lock */
1345 static struct nfsd4_session *
1346 __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
1348 struct nfsd4_session *elem;
1350 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1352 dump_sessionid(__func__, sessionid);
1353 idx = hash_sessionid(sessionid);
1354 /* Search in the appropriate list */
1355 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
1356 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1357 NFS4_MAX_SESSIONID_LEN)) {
1362 dprintk("%s: session not found\n", __func__);
1366 static struct nfsd4_session *
1367 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1370 struct nfsd4_session *session;
1371 __be32 status = nfserr_badsession;
1373 session = __find_in_sessionid_hashtbl(sessionid, net);
1376 status = nfsd4_get_session_locked(session);
1384 /* caller must hold client_lock */
1386 unhash_session(struct nfsd4_session *ses)
1388 list_del(&ses->se_hash);
1389 spin_lock(&ses->se_client->cl_lock);
1390 list_del(&ses->se_perclnt);
1391 spin_unlock(&ses->se_client->cl_lock);
1394 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1396 STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
1398 if (clid->cl_boot == nn->boot_time)
1400 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
1401 clid->cl_boot, clid->cl_id, nn->boot_time);
1406 * XXX Should we use a slab cache ?
1407 * This type of memory management is somewhat inefficient, but we use it
1408 * anyway since SETCLIENTID is not a common operation.
1410 static struct nfs4_client *alloc_client(struct xdr_netobj name)
1412 struct nfs4_client *clp;
1414 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1417 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
1418 if (clp->cl_name.data == NULL) {
1422 clp->cl_name.len = name.len;
1423 INIT_LIST_HEAD(&clp->cl_sessions);
1424 idr_init(&clp->cl_stateids);
1425 atomic_set(&clp->cl_refcount, 0);
1426 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1427 INIT_LIST_HEAD(&clp->cl_idhash);
1428 INIT_LIST_HEAD(&clp->cl_openowners);
1429 INIT_LIST_HEAD(&clp->cl_delegations);
1430 INIT_LIST_HEAD(&clp->cl_lru);
1431 INIT_LIST_HEAD(&clp->cl_callbacks);
1432 INIT_LIST_HEAD(&clp->cl_revoked);
1433 spin_lock_init(&clp->cl_lock);
1434 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1439 free_client(struct nfs4_client *clp)
1441 struct nfsd_net __maybe_unused *nn = net_generic(clp->net, nfsd_net_id);
1443 lockdep_assert_held(&nn->client_lock);
1444 while (!list_empty(&clp->cl_sessions)) {
1445 struct nfsd4_session *ses;
1446 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1448 list_del(&ses->se_perclnt);
1449 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1452 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
1453 free_svc_cred(&clp->cl_cred);
1454 kfree(clp->cl_name.data);
1455 idr_destroy(&clp->cl_stateids);
1459 /* must be called under the client_lock */
1461 unhash_client_locked(struct nfs4_client *clp)
1463 struct nfsd4_session *ses;
1465 list_del(&clp->cl_lru);
1466 spin_lock(&clp->cl_lock);
1467 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1468 list_del_init(&ses->se_hash);
1469 spin_unlock(&clp->cl_lock);
1473 destroy_client(struct nfs4_client *clp)
1475 struct nfs4_openowner *oo;
1476 struct nfs4_delegation *dp;
1477 struct list_head reaplist;
1478 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1480 INIT_LIST_HEAD(&reaplist);
1481 spin_lock(&state_lock);
1482 while (!list_empty(&clp->cl_delegations)) {
1483 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
1484 unhash_delegation_locked(dp);
1485 list_add(&dp->dl_recall_lru, &reaplist);
1487 spin_unlock(&state_lock);
1488 while (!list_empty(&reaplist)) {
1489 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
1490 list_del_init(&dp->dl_recall_lru);
1491 nfs4_put_stid(&dp->dl_stid);
1493 while (!list_empty(&clp->cl_revoked)) {
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_openowners)) {
1499 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1500 release_openowner(oo);
1502 nfsd4_shutdown_callback(clp);
1503 if (clp->cl_cb_conn.cb_xprt)
1504 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
1505 list_del(&clp->cl_idhash);
1506 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1507 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1509 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1510 spin_lock(&nn->client_lock);
1511 unhash_client_locked(clp);
1512 WARN_ON_ONCE(atomic_read(&clp->cl_refcount));
1514 spin_unlock(&nn->client_lock);
1517 static void expire_client(struct nfs4_client *clp)
1519 nfsd4_client_record_remove(clp);
1520 destroy_client(clp);
1523 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1525 memcpy(target->cl_verifier.data, source->data,
1526 sizeof(target->cl_verifier.data));
1529 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1531 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1532 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1535 static int copy_cred(struct svc_cred *target, struct svc_cred *source)
1537 if (source->cr_principal) {
1538 target->cr_principal =
1539 kstrdup(source->cr_principal, GFP_KERNEL);
1540 if (target->cr_principal == NULL)
1543 target->cr_principal = NULL;
1544 target->cr_flavor = source->cr_flavor;
1545 target->cr_uid = source->cr_uid;
1546 target->cr_gid = source->cr_gid;
1547 target->cr_group_info = source->cr_group_info;
1548 get_group_info(target->cr_group_info);
1549 target->cr_gss_mech = source->cr_gss_mech;
1550 if (source->cr_gss_mech)
1551 gss_mech_get(source->cr_gss_mech);
1556 compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1560 res = o1->len - o2->len;
1563 return (long long)memcmp(o1->data, o2->data, o1->len);
1566 static int same_name(const char *n1, const char *n2)
1568 return 0 == memcmp(n1, n2, HEXDIR_LEN);
1572 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1574 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
1578 same_clid(clientid_t *cl1, clientid_t *cl2)
1580 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
1583 static bool groups_equal(struct group_info *g1, struct group_info *g2)
1587 if (g1->ngroups != g2->ngroups)
1589 for (i=0; i<g1->ngroups; i++)
1590 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
1596 * RFC 3530 language requires clid_inuse be returned when the
1597 * "principal" associated with a requests differs from that previously
1598 * used. We use uid, gid's, and gss principal string as our best
1599 * approximation. We also don't want to allow non-gss use of a client
1600 * established using gss: in theory cr_principal should catch that
1601 * change, but in practice cr_principal can be null even in the gss case
1602 * since gssd doesn't always pass down a principal string.
1604 static bool is_gss_cred(struct svc_cred *cr)
1606 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1607 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1612 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1614 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
1615 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1616 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
1617 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1619 if (cr1->cr_principal == cr2->cr_principal)
1621 if (!cr1->cr_principal || !cr2->cr_principal)
1623 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
1626 static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1628 struct svc_cred *cr = &rqstp->rq_cred;
1631 if (!cr->cr_gss_mech)
1633 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1634 return service == RPC_GSS_SVC_INTEGRITY ||
1635 service == RPC_GSS_SVC_PRIVACY;
1638 static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1640 struct svc_cred *cr = &rqstp->rq_cred;
1642 if (!cl->cl_mach_cred)
1644 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1646 if (!svc_rqst_integrity_protected(rqstp))
1648 if (!cr->cr_principal)
1650 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1653 static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
1655 static u32 current_clientid = 1;
1657 clp->cl_clientid.cl_boot = nn->boot_time;
1658 clp->cl_clientid.cl_id = current_clientid++;
1661 static void gen_confirm(struct nfs4_client *clp)
1667 * This is opaque to client, so no need to byte-swap. Use
1668 * __force to keep sparse happy
1670 verf[0] = (__force __be32)get_seconds();
1671 verf[1] = (__force __be32)i++;
1672 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
1675 static struct nfs4_stid *
1676 find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
1678 struct nfs4_stid *ret;
1680 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1681 if (!ret || !ret->sc_type)
1686 static struct nfs4_stid *
1687 find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
1689 struct nfs4_stid *s;
1691 spin_lock(&cl->cl_lock);
1692 s = find_stateid_locked(cl, t);
1694 if (typemask & s->sc_type)
1695 atomic_inc(&s->sc_count);
1699 spin_unlock(&cl->cl_lock);
1703 static struct nfs4_client *create_client(struct xdr_netobj name,
1704 struct svc_rqst *rqstp, nfs4_verifier *verf)
1706 struct nfs4_client *clp;
1707 struct sockaddr *sa = svc_addr(rqstp);
1709 struct net *net = SVC_NET(rqstp);
1710 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1712 clp = alloc_client(name);
1716 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1718 spin_lock(&nn->client_lock);
1720 spin_unlock(&nn->client_lock);
1723 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_run_cb_null);
1724 clp->cl_time = get_seconds();
1725 clear_bit(0, &clp->cl_cb_slot_busy);
1726 copy_verf(clp, verf);
1727 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
1729 clp->cl_cb_session = NULL;
1735 add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1737 struct rb_node **new = &(root->rb_node), *parent = NULL;
1738 struct nfs4_client *clp;
1741 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1744 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1745 new = &((*new)->rb_left);
1747 new = &((*new)->rb_right);
1750 rb_link_node(&new_clp->cl_namenode, parent, new);
1751 rb_insert_color(&new_clp->cl_namenode, root);
1754 static struct nfs4_client *
1755 find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1758 struct rb_node *node = root->rb_node;
1759 struct nfs4_client *clp;
1762 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1763 cmp = compare_blob(&clp->cl_name, name);
1765 node = node->rb_left;
1767 node = node->rb_right;
1775 add_to_unconfirmed(struct nfs4_client *clp)
1777 unsigned int idhashval;
1778 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1780 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
1781 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
1782 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1783 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
1788 move_to_confirmed(struct nfs4_client *clp)
1790 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1791 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1793 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
1794 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
1795 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1796 add_clp_to_name_tree(clp, &nn->conf_name_tree);
1797 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
1801 static struct nfs4_client *
1802 find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
1804 struct nfs4_client *clp;
1805 unsigned int idhashval = clientid_hashval(clid->cl_id);
1807 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
1808 if (same_clid(&clp->cl_clientid, clid)) {
1809 if ((bool)clp->cl_minorversion != sessions)
1818 static struct nfs4_client *
1819 find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1821 struct list_head *tbl = nn->conf_id_hashtbl;
1823 return find_client_in_id_table(tbl, clid, sessions);
1826 static struct nfs4_client *
1827 find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1829 struct list_head *tbl = nn->unconf_id_hashtbl;
1831 return find_client_in_id_table(tbl, clid, sessions);
1834 static bool clp_used_exchangeid(struct nfs4_client *clp)
1836 return clp->cl_exchange_flags != 0;
1839 static struct nfs4_client *
1840 find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
1842 return find_clp_in_name_tree(name, &nn->conf_name_tree);
1845 static struct nfs4_client *
1846 find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
1848 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
1852 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
1854 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
1855 struct sockaddr *sa = svc_addr(rqstp);
1856 u32 scopeid = rpc_get_scope_id(sa);
1857 unsigned short expected_family;
1859 /* Currently, we only support tcp and tcp6 for the callback channel */
1860 if (se->se_callback_netid_len == 3 &&
1861 !memcmp(se->se_callback_netid_val, "tcp", 3))
1862 expected_family = AF_INET;
1863 else if (se->se_callback_netid_len == 4 &&
1864 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1865 expected_family = AF_INET6;
1869 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
1870 se->se_callback_addr_len,
1871 (struct sockaddr *)&conn->cb_addr,
1872 sizeof(conn->cb_addr));
1874 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
1877 if (conn->cb_addr.ss_family == AF_INET6)
1878 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
1880 conn->cb_prog = se->se_callback_prog;
1881 conn->cb_ident = se->se_callback_ident;
1882 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
1885 conn->cb_addr.ss_family = AF_UNSPEC;
1886 conn->cb_addrlen = 0;
1887 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
1888 "will not receive delegations\n",
1889 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1895 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
1898 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1900 struct xdr_buf *buf = resp->xdr.buf;
1901 struct nfsd4_slot *slot = resp->cstate.slot;
1904 dprintk("--> %s slot %p\n", __func__, slot);
1906 slot->sl_opcnt = resp->opcnt;
1907 slot->sl_status = resp->cstate.status;
1909 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
1910 if (nfsd4_not_cached(resp)) {
1911 slot->sl_datalen = 0;
1914 base = resp->cstate.data_offset;
1915 slot->sl_datalen = buf->len - base;
1916 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
1917 WARN("%s: sessions DRC could not cache compound\n", __func__);
1922 * Encode the replay sequence operation from the slot values.
1923 * If cachethis is FALSE encode the uncached rep error on the next
1924 * operation which sets resp->p and increments resp->opcnt for
1925 * nfs4svc_encode_compoundres.
1929 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
1930 struct nfsd4_compoundres *resp)
1932 struct nfsd4_op *op;
1933 struct nfsd4_slot *slot = resp->cstate.slot;
1935 /* Encode the replayed sequence operation */
1936 op = &args->ops[resp->opcnt - 1];
1937 nfsd4_encode_operation(resp, op);
1939 /* Return nfserr_retry_uncached_rep in next operation. */
1940 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
1941 op = &args->ops[resp->opcnt++];
1942 op->status = nfserr_retry_uncached_rep;
1943 nfsd4_encode_operation(resp, op);
1949 * The sequence operation is not cached because we can use the slot and
1953 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
1954 struct nfsd4_sequence *seq)
1956 struct nfsd4_slot *slot = resp->cstate.slot;
1957 struct xdr_stream *xdr = &resp->xdr;
1961 dprintk("--> %s slot %p\n", __func__, slot);
1963 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
1967 p = xdr_reserve_space(xdr, slot->sl_datalen);
1970 return nfserr_serverfault;
1972 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
1973 xdr_commit_encode(xdr);
1975 resp->opcnt = slot->sl_opcnt;
1976 return slot->sl_status;
1980 * Set the exchange_id flags returned by the server.
1983 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1985 /* pNFS is not supported */
1986 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
1988 /* Referrals are supported, Migration is not. */
1989 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
1991 /* set the wire flags to return to client. */
1992 clid->flags = new->cl_exchange_flags;
1995 static bool client_has_state(struct nfs4_client *clp)
1998 * Note clp->cl_openowners check isn't quite right: there's no
1999 * need to count owners without stateid's.
2001 * Also note we should probably be using this in 4.0 case too.
2003 return !list_empty(&clp->cl_openowners)
2004 || !list_empty(&clp->cl_delegations)
2005 || !list_empty(&clp->cl_sessions);
2009 nfsd4_exchange_id(struct svc_rqst *rqstp,
2010 struct nfsd4_compound_state *cstate,
2011 struct nfsd4_exchange_id *exid)
2013 struct nfs4_client *unconf, *conf, *new;
2015 char addr_str[INET6_ADDRSTRLEN];
2016 nfs4_verifier verf = exid->verifier;
2017 struct sockaddr *sa = svc_addr(rqstp);
2018 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
2019 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2021 rpc_ntop(sa, addr_str, sizeof(addr_str));
2022 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
2023 "ip_addr=%s flags %x, spa_how %d\n",
2024 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
2025 addr_str, exid->flags, exid->spa_how);
2027 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
2028 return nfserr_inval;
2030 switch (exid->spa_how) {
2032 if (!svc_rqst_integrity_protected(rqstp))
2033 return nfserr_inval;
2036 default: /* checked by xdr code */
2039 return nfserr_encr_alg_unsupp;
2042 /* Cases below refer to rfc 5661 section 18.35.4: */
2044 conf = find_confirmed_client_by_name(&exid->clname, nn);
2046 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2047 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2050 if (!clp_used_exchangeid(conf)) { /* buggy client */
2051 status = nfserr_inval;
2054 if (!mach_creds_match(conf, rqstp)) {
2055 status = nfserr_wrong_cred;
2058 if (!creds_match) { /* case 9 */
2059 status = nfserr_perm;
2062 if (!verfs_match) { /* case 8 */
2063 status = nfserr_not_same;
2067 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
2071 if (!creds_match) { /* case 3 */
2072 if (client_has_state(conf)) {
2073 status = nfserr_clid_inuse;
2076 expire_client(conf);
2079 if (verfs_match) { /* case 2 */
2080 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
2084 /* case 5, client reboot */
2088 if (update) { /* case 7 */
2089 status = nfserr_noent;
2093 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
2094 if (unconf) /* case 4, possible retry or client restart */
2095 expire_client(unconf);
2097 /* case 1 (normal case) */
2099 new = create_client(exid->clname, rqstp, &verf);
2101 status = nfserr_jukebox;
2104 new->cl_minorversion = cstate->minorversion;
2105 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
2108 add_to_unconfirmed(new);
2110 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
2111 exid->clientid.cl_id = new->cl_clientid.cl_id;
2113 exid->seqid = new->cl_cs_slot.sl_seqid + 1;
2114 nfsd4_set_ex_flags(new, exid);
2116 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
2117 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
2121 nfs4_unlock_state();
2126 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
2128 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2131 /* The slot is in use, and no response has been sent. */
2133 if (seqid == slot_seqid)
2134 return nfserr_jukebox;
2136 return nfserr_seq_misordered;
2138 /* Note unsigned 32-bit arithmetic handles wraparound: */
2139 if (likely(seqid == slot_seqid + 1))
2141 if (seqid == slot_seqid)
2142 return nfserr_replay_cache;
2143 return nfserr_seq_misordered;
2147 * Cache the create session result into the create session single DRC
2148 * slot cache by saving the xdr structure. sl_seqid has been set.
2149 * Do this for solo or embedded create session operations.
2152 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
2153 struct nfsd4_clid_slot *slot, __be32 nfserr)
2155 slot->sl_status = nfserr;
2156 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2160 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2161 struct nfsd4_clid_slot *slot)
2163 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2164 return slot->sl_status;
2167 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2168 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2169 1 + /* MIN tag is length with zero, only length */ \
2170 3 + /* version, opcount, opcode */ \
2171 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2172 /* seqid, slotID, slotID, cache */ \
2173 4 ) * sizeof(__be32))
2175 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2176 2 + /* verifier: AUTH_NULL, length 0 */\
2178 1 + /* MIN tag is length with zero, only length */ \
2179 3 + /* opcount, opcode, opstatus*/ \
2180 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2181 /* seqid, slotID, slotID, slotID, status */ \
2182 5 ) * sizeof(__be32))
2184 static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
2186 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2188 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2189 return nfserr_toosmall;
2190 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2191 return nfserr_toosmall;
2192 ca->headerpadsz = 0;
2193 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2194 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2195 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2196 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2197 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2198 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2200 * Note decreasing slot size below client's request may make it
2201 * difficult for client to function correctly, whereas
2202 * decreasing the number of slots will (just?) affect
2203 * performance. When short on memory we therefore prefer to
2204 * decrease number of slots instead of their size. Clients that
2205 * request larger slots than they need will get poor results:
2207 ca->maxreqs = nfsd4_get_drc_mem(ca);
2209 return nfserr_jukebox;
2214 #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2215 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2216 #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2217 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2219 static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2221 ca->headerpadsz = 0;
2224 * These RPC_MAX_HEADER macros are overkill, especially since we
2225 * don't even do gss on the backchannel yet. But this is still
2226 * less than 1k. Tighten up this estimate in the unlikely event
2227 * it turns out to be a problem for some client:
2229 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
2230 return nfserr_toosmall;
2231 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
2232 return nfserr_toosmall;
2233 ca->maxresp_cached = 0;
2235 return nfserr_toosmall;
2240 static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2242 switch (cbs->flavor) {
2248 * GSS case: the spec doesn't allow us to return this
2249 * error. But it also doesn't allow us not to support
2251 * I'd rather this fail hard than return some error the
2252 * client might think it can already handle:
2254 return nfserr_encr_alg_unsupp;
2259 nfsd4_create_session(struct svc_rqst *rqstp,
2260 struct nfsd4_compound_state *cstate,
2261 struct nfsd4_create_session *cr_ses)
2263 struct sockaddr *sa = svc_addr(rqstp);
2264 struct nfs4_client *conf, *unconf;
2265 struct nfsd4_session *new;
2266 struct nfsd4_conn *conn;
2267 struct nfsd4_clid_slot *cs_slot = NULL;
2269 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2271 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2272 return nfserr_inval;
2273 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2276 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
2279 status = check_backchannel_attrs(&cr_ses->back_channel);
2281 goto out_release_drc_mem;
2282 status = nfserr_jukebox;
2283 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
2285 goto out_release_drc_mem;
2286 conn = alloc_conn_from_crses(rqstp, cr_ses);
2288 goto out_free_session;
2291 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
2292 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
2293 WARN_ON_ONCE(conf && unconf);
2296 status = nfserr_wrong_cred;
2297 if (!mach_creds_match(conf, rqstp))
2299 cs_slot = &conf->cl_cs_slot;
2300 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2301 if (status == nfserr_replay_cache) {
2302 status = nfsd4_replay_create_session(cr_ses, cs_slot);
2304 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
2305 status = nfserr_seq_misordered;
2308 } else if (unconf) {
2309 struct nfs4_client *old;
2310 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
2311 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
2312 status = nfserr_clid_inuse;
2315 status = nfserr_wrong_cred;
2316 if (!mach_creds_match(unconf, rqstp))
2318 cs_slot = &unconf->cl_cs_slot;
2319 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2321 /* an unconfirmed replay returns misordered */
2322 status = nfserr_seq_misordered;
2325 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
2327 status = mark_client_expired(old);
2332 move_to_confirmed(unconf);
2335 status = nfserr_stale_clientid;
2340 * We do not support RDMA or persistent sessions
2342 cr_ses->flags &= ~SESSION4_PERSIST;
2343 cr_ses->flags &= ~SESSION4_RDMA;
2345 init_session(rqstp, new, conf, cr_ses);
2346 nfsd4_init_conn(rqstp, conn, new);
2348 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
2349 NFS4_MAX_SESSIONID_LEN);
2350 cs_slot->sl_seqid++;
2351 cr_ses->seqid = cs_slot->sl_seqid;
2353 /* cache solo and embedded create sessions under the state lock */
2354 nfsd4_cache_create_session(cr_ses, cs_slot, status);
2355 nfs4_unlock_state();
2358 nfs4_unlock_state();
2361 __free_session(new);
2362 out_release_drc_mem:
2363 nfsd4_put_drc_mem(&cr_ses->fore_channel);
2367 static __be32 nfsd4_map_bcts_dir(u32 *dir)
2370 case NFS4_CDFC4_FORE:
2371 case NFS4_CDFC4_BACK:
2373 case NFS4_CDFC4_FORE_OR_BOTH:
2374 case NFS4_CDFC4_BACK_OR_BOTH:
2375 *dir = NFS4_CDFC4_BOTH;
2378 return nfserr_inval;
2381 __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2383 struct nfsd4_session *session = cstate->session;
2384 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2387 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2390 spin_lock(&nn->client_lock);
2391 session->se_cb_prog = bc->bc_cb_program;
2392 session->se_cb_sec = bc->bc_cb_sec;
2393 spin_unlock(&nn->client_lock);
2395 nfsd4_probe_callback(session->se_client);
2400 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2401 struct nfsd4_compound_state *cstate,
2402 struct nfsd4_bind_conn_to_session *bcts)
2405 struct nfsd4_conn *conn;
2406 struct nfsd4_session *session;
2407 struct net *net = SVC_NET(rqstp);
2408 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2410 if (!nfsd4_last_compound_op(rqstp))
2411 return nfserr_not_only_op;
2413 spin_lock(&nn->client_lock);
2414 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
2415 spin_unlock(&nn->client_lock);
2417 goto out_no_session;
2418 status = nfserr_wrong_cred;
2419 if (!mach_creds_match(session->se_client, rqstp))
2421 status = nfsd4_map_bcts_dir(&bcts->dir);
2424 conn = alloc_conn(rqstp, bcts->dir);
2425 status = nfserr_jukebox;
2428 nfsd4_init_conn(rqstp, conn, session);
2431 nfsd4_put_session(session);
2433 nfs4_unlock_state();
2437 static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2441 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2445 nfsd4_destroy_session(struct svc_rqst *r,
2446 struct nfsd4_compound_state *cstate,
2447 struct nfsd4_destroy_session *sessionid)
2449 struct nfsd4_session *ses;
2451 int ref_held_by_me = 0;
2452 struct net *net = SVC_NET(r);
2453 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2456 status = nfserr_not_only_op;
2457 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
2458 if (!nfsd4_last_compound_op(r))
2462 dump_sessionid(__func__, &sessionid->sessionid);
2463 spin_lock(&nn->client_lock);
2464 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
2466 goto out_client_lock;
2467 status = nfserr_wrong_cred;
2468 if (!mach_creds_match(ses->se_client, r))
2469 goto out_put_session;
2470 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
2472 goto out_put_session;
2473 unhash_session(ses);
2474 spin_unlock(&nn->client_lock);
2476 nfsd4_probe_callback_sync(ses->se_client);
2478 spin_lock(&nn->client_lock);
2481 nfsd4_put_session_locked(ses);
2483 spin_unlock(&nn->client_lock);
2485 nfs4_unlock_state();
2489 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
2491 struct nfsd4_conn *c;
2493 list_for_each_entry(c, &s->se_conns, cn_persession) {
2494 if (c->cn_xprt == xpt) {
2501 static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
2503 struct nfs4_client *clp = ses->se_client;
2504 struct nfsd4_conn *c;
2505 __be32 status = nfs_ok;
2508 spin_lock(&clp->cl_lock);
2509 c = __nfsd4_find_conn(new->cn_xprt, ses);
2512 status = nfserr_conn_not_bound_to_session;
2513 if (clp->cl_mach_cred)
2515 __nfsd4_hash_conn(new, ses);
2516 spin_unlock(&clp->cl_lock);
2517 ret = nfsd4_register_conn(new);
2519 /* oops; xprt is already down: */
2520 nfsd4_conn_lost(&new->cn_xpt_user);
2523 spin_unlock(&clp->cl_lock);
2528 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2530 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2532 return args->opcnt > session->se_fchannel.maxops;
2535 static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2536 struct nfsd4_session *session)
2538 struct xdr_buf *xb = &rqstp->rq_arg;
2540 return xb->len > session->se_fchannel.maxreq_sz;
2544 nfsd4_sequence(struct svc_rqst *rqstp,
2545 struct nfsd4_compound_state *cstate,
2546 struct nfsd4_sequence *seq)
2548 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2549 struct xdr_stream *xdr = &resp->xdr;
2550 struct nfsd4_session *session;
2551 struct nfs4_client *clp;
2552 struct nfsd4_slot *slot;
2553 struct nfsd4_conn *conn;
2556 struct net *net = SVC_NET(rqstp);
2557 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2559 if (resp->opcnt != 1)
2560 return nfserr_sequence_pos;
2563 * Will be either used or freed by nfsd4_sequence_check_conn
2566 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2568 return nfserr_jukebox;
2570 spin_lock(&nn->client_lock);
2571 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
2573 goto out_no_session;
2574 clp = session->se_client;
2576 status = nfserr_too_many_ops;
2577 if (nfsd4_session_too_many_ops(rqstp, session))
2578 goto out_put_session;
2580 status = nfserr_req_too_big;
2581 if (nfsd4_request_too_big(rqstp, session))
2582 goto out_put_session;
2584 status = nfserr_badslot;
2585 if (seq->slotid >= session->se_fchannel.maxreqs)
2586 goto out_put_session;
2588 slot = session->se_slots[seq->slotid];
2589 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2591 /* We do not negotiate the number of slots yet, so set the
2592 * maxslots to the session maxreqs which is used to encode
2593 * sr_highest_slotid and the sr_target_slot id to maxslots */
2594 seq->maxslots = session->se_fchannel.maxreqs;
2596 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2597 slot->sl_flags & NFSD4_SLOT_INUSE);
2598 if (status == nfserr_replay_cache) {
2599 status = nfserr_seq_misordered;
2600 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
2601 goto out_put_session;
2602 cstate->slot = slot;
2603 cstate->session = session;
2605 /* Return the cached reply status and set cstate->status
2606 * for nfsd4_proc_compound processing */
2607 status = nfsd4_replay_cache_entry(resp, seq);
2608 cstate->status = nfserr_replay_cache;
2612 goto out_put_session;
2614 status = nfsd4_sequence_check_conn(conn, session);
2617 goto out_put_session;
2619 buflen = (seq->cachethis) ?
2620 session->se_fchannel.maxresp_cached :
2621 session->se_fchannel.maxresp_sz;
2622 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2624 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
2625 goto out_put_session;
2626 svc_reserve(rqstp, buflen);
2629 /* Success! bump slot seqid */
2630 slot->sl_seqid = seq->seqid;
2631 slot->sl_flags |= NFSD4_SLOT_INUSE;
2633 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
2635 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
2637 cstate->slot = slot;
2638 cstate->session = session;
2642 switch (clp->cl_cb_state) {
2644 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2646 case NFSD4_CB_FAULT:
2647 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2650 seq->status_flags = 0;
2652 if (!list_empty(&clp->cl_revoked))
2653 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
2657 spin_unlock(&nn->client_lock);
2660 nfsd4_put_session_locked(session);
2661 goto out_no_session;
2665 nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2667 struct nfsd4_compound_state *cs = &resp->cstate;
2669 if (nfsd4_has_session(cs)) {
2670 if (cs->status != nfserr_replay_cache) {
2671 nfsd4_store_cache_entry(resp);
2672 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2674 /* Drop session reference that was taken in nfsd4_sequence() */
2675 nfsd4_put_session(cs->session);
2677 put_client_renew(cs->clp);
2681 nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2683 struct nfs4_client *conf, *unconf, *clp;
2685 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2688 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
2689 conf = find_confirmed_client(&dc->clientid, true, nn);
2690 WARN_ON_ONCE(conf && unconf);
2695 if (client_has_state(conf)) {
2696 status = nfserr_clientid_busy;
2702 status = nfserr_stale_clientid;
2705 if (!mach_creds_match(clp, rqstp)) {
2706 status = nfserr_wrong_cred;
2711 nfs4_unlock_state();
2716 nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2720 if (rc->rca_one_fs) {
2721 if (!cstate->current_fh.fh_dentry)
2722 return nfserr_nofilehandle;
2724 * We don't take advantage of the rca_one_fs case.
2725 * That's OK, it's optional, we can safely ignore it.
2731 status = nfserr_complete_already;
2732 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2733 &cstate->session->se_client->cl_flags))
2736 status = nfserr_stale_clientid;
2737 if (is_client_expired(cstate->session->se_client))
2739 * The following error isn't really legal.
2740 * But we only get here if the client just explicitly
2741 * destroyed the client. Surely it no longer cares what
2742 * error it gets back on an operation for the dead
2748 nfsd4_client_record_create(cstate->session->se_client);
2750 nfs4_unlock_state();
2755 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2756 struct nfsd4_setclientid *setclid)
2758 struct xdr_netobj clname = setclid->se_name;
2759 nfs4_verifier clverifier = setclid->se_verf;
2760 struct nfs4_client *conf, *unconf, *new;
2762 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2764 /* Cases below refer to rfc 3530 section 14.2.33: */
2766 conf = find_confirmed_client_by_name(&clname, nn);
2769 status = nfserr_clid_inuse;
2770 if (clp_used_exchangeid(conf))
2772 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
2773 char addr_str[INET6_ADDRSTRLEN];
2774 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2776 dprintk("NFSD: setclientid: string in use by client "
2777 "at %s\n", addr_str);
2781 unconf = find_unconfirmed_client_by_name(&clname, nn);
2783 expire_client(unconf);
2784 status = nfserr_jukebox;
2785 new = create_client(clname, rqstp, &clverifier);
2788 if (conf && same_verf(&conf->cl_verifier, &clverifier))
2789 /* case 1: probable callback update */
2790 copy_clid(new, conf);
2791 else /* case 4 (new client) or cases 2, 3 (client reboot): */
2793 new->cl_minorversion = 0;
2794 gen_callback(new, setclid, rqstp);
2795 add_to_unconfirmed(new);
2796 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2797 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2798 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2801 nfs4_unlock_state();
2807 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2808 struct nfsd4_compound_state *cstate,
2809 struct nfsd4_setclientid_confirm *setclientid_confirm)
2811 struct nfs4_client *conf, *unconf;
2812 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2813 clientid_t * clid = &setclientid_confirm->sc_clientid;
2815 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2817 if (STALE_CLIENTID(clid, nn))
2818 return nfserr_stale_clientid;
2821 conf = find_confirmed_client(clid, false, nn);
2822 unconf = find_unconfirmed_client(clid, false, nn);
2824 * We try hard to give out unique clientid's, so if we get an
2825 * attempt to confirm the same clientid with a different cred,
2826 * there's a bug somewhere. Let's charitably assume it's our
2829 status = nfserr_serverfault;
2830 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
2832 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
2834 /* cases below refer to rfc 3530 section 14.2.34: */
2835 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
2836 if (conf && !unconf) /* case 2: probable retransmit */
2838 else /* case 4: client hasn't noticed we rebooted yet? */
2839 status = nfserr_stale_clientid;
2843 if (conf) { /* case 1: callback update */
2844 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2845 nfsd4_probe_callback(conf);
2846 expire_client(unconf);
2847 } else { /* case 3: normal case; new or rebooted client */
2848 conf = find_confirmed_client_by_name(&unconf->cl_name, nn);
2850 status = mark_client_expired(conf);
2853 expire_client(conf);
2855 move_to_confirmed(unconf);
2856 nfsd4_probe_callback(unconf);
2859 nfs4_unlock_state();
2863 static struct nfs4_file *nfsd4_alloc_file(void)
2865 return kmem_cache_alloc(file_slab, GFP_KERNEL);
2868 /* OPEN Share state helper functions */
2869 static void nfsd4_init_file(struct nfs4_file *fp, struct knfsd_fh *fh)
2871 unsigned int hashval = file_hashval(fh);
2873 lockdep_assert_held(&state_lock);
2875 atomic_set(&fp->fi_ref, 1);
2876 spin_lock_init(&fp->fi_lock);
2877 INIT_LIST_HEAD(&fp->fi_stateids);
2878 INIT_LIST_HEAD(&fp->fi_delegations);
2879 fh_copy_shallow(&fp->fi_fhandle, fh);
2880 fp->fi_had_conflict = false;
2881 fp->fi_lease = NULL;
2882 fp->fi_share_deny = 0;
2883 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2884 memset(fp->fi_access, 0, sizeof(fp->fi_access));
2885 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
2889 nfsd4_free_slabs(void)
2891 kmem_cache_destroy(openowner_slab);
2892 kmem_cache_destroy(lockowner_slab);
2893 kmem_cache_destroy(file_slab);
2894 kmem_cache_destroy(stateid_slab);
2895 kmem_cache_destroy(deleg_slab);
2899 nfsd4_init_slabs(void)
2901 openowner_slab = kmem_cache_create("nfsd4_openowners",
2902 sizeof(struct nfs4_openowner), 0, 0, NULL);
2903 if (openowner_slab == NULL)
2905 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
2906 sizeof(struct nfs4_lockowner), 0, 0, NULL);
2907 if (lockowner_slab == NULL)
2908 goto out_free_openowner_slab;
2909 file_slab = kmem_cache_create("nfsd4_files",
2910 sizeof(struct nfs4_file), 0, 0, NULL);
2911 if (file_slab == NULL)
2912 goto out_free_lockowner_slab;
2913 stateid_slab = kmem_cache_create("nfsd4_stateids",
2914 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
2915 if (stateid_slab == NULL)
2916 goto out_free_file_slab;
2917 deleg_slab = kmem_cache_create("nfsd4_delegations",
2918 sizeof(struct nfs4_delegation), 0, 0, NULL);
2919 if (deleg_slab == NULL)
2920 goto out_free_stateid_slab;
2923 out_free_stateid_slab:
2924 kmem_cache_destroy(stateid_slab);
2926 kmem_cache_destroy(file_slab);
2927 out_free_lockowner_slab:
2928 kmem_cache_destroy(lockowner_slab);
2929 out_free_openowner_slab:
2930 kmem_cache_destroy(openowner_slab);
2932 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2936 static void init_nfs4_replay(struct nfs4_replay *rp)
2938 rp->rp_status = nfserr_serverfault;
2940 rp->rp_buf = rp->rp_ibuf;
2941 mutex_init(&rp->rp_mutex);
2944 static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
2945 struct nfs4_stateowner *so)
2947 if (!nfsd4_has_session(cstate)) {
2948 mutex_lock(&so->so_replay.rp_mutex);
2949 cstate->replay_owner = so;
2950 atomic_inc(&so->so_count);
2954 void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
2956 struct nfs4_stateowner *so = cstate->replay_owner;
2959 cstate->replay_owner = NULL;
2960 mutex_unlock(&so->so_replay.rp_mutex);
2961 nfs4_put_stateowner(so);
2965 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
2967 struct nfs4_stateowner *sop;
2969 sop = kmem_cache_alloc(slab, GFP_KERNEL);
2973 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
2974 if (!sop->so_owner.data) {
2975 kmem_cache_free(slab, sop);
2978 sop->so_owner.len = owner->len;
2980 INIT_LIST_HEAD(&sop->so_stateids);
2981 sop->so_client = clp;
2982 init_nfs4_replay(&sop->so_replay);
2983 atomic_set(&sop->so_count, 1);
2987 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
2989 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2991 list_add(&oo->oo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
2992 list_add(&oo->oo_perclient, &clp->cl_openowners);
2995 static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
2997 struct nfs4_openowner *oo = openowner(so);
2999 unhash_openowner(oo);
3002 static void nfs4_free_openowner(struct nfs4_stateowner *so)
3004 struct nfs4_openowner *oo = openowner(so);
3006 kmem_cache_free(openowner_slab, oo);
3009 static const struct nfs4_stateowner_operations openowner_ops = {
3010 .so_unhash = nfs4_unhash_openowner,
3011 .so_free = nfs4_free_openowner,
3014 static struct nfs4_openowner *
3015 alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
3016 struct nfsd4_compound_state *cstate)
3018 struct nfs4_client *clp = cstate->clp;
3019 struct nfs4_openowner *oo;
3021 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3024 oo->oo_owner.so_ops = &openowner_ops;
3025 oo->oo_owner.so_is_open_owner = 1;
3026 oo->oo_owner.so_seqid = open->op_seqid;
3027 oo->oo_flags = NFS4_OO_NEW;
3028 if (nfsd4_has_session(cstate))
3029 oo->oo_flags |= NFS4_OO_CONFIRMED;
3031 oo->oo_last_closed_stid = NULL;
3032 INIT_LIST_HEAD(&oo->oo_close_lru);
3033 hash_openowner(oo, clp, strhashval);
3037 static void init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
3038 struct nfs4_openowner *oo = open->op_openowner;
3040 atomic_inc(&stp->st_stid.sc_count);
3041 stp->st_stid.sc_type = NFS4_OPEN_STID;
3042 INIT_LIST_HEAD(&stp->st_locks);
3043 stp->st_stateowner = &oo->oo_owner;
3045 stp->st_stid.sc_file = fp;
3046 stp->st_access_bmap = 0;
3047 stp->st_deny_bmap = 0;
3048 stp->st_openstp = NULL;
3049 spin_lock(&oo->oo_owner.so_client->cl_lock);
3050 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
3051 spin_lock(&fp->fi_lock);
3052 list_add(&stp->st_perfile, &fp->fi_stateids);
3053 spin_unlock(&fp->fi_lock);
3054 spin_unlock(&oo->oo_owner.so_client->cl_lock);
3058 move_to_close_lru(struct nfs4_openowner *oo, struct net *net)
3060 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3062 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
3064 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
3065 oo->oo_time = get_seconds();
3069 same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
3072 return (sop->so_owner.len == owner->len) &&
3073 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
3074 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
3077 static struct nfs4_openowner *
3078 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
3079 bool sessions, struct nfsd_net *nn)
3081 struct nfs4_stateowner *so;
3082 struct nfs4_openowner *oo;
3083 struct nfs4_client *clp;
3085 list_for_each_entry(so, &nn->ownerstr_hashtbl[hashval], so_strhash) {
3086 if (!so->so_is_open_owner)
3088 if (same_owner_str(so, &open->op_owner, &open->op_clientid)) {
3090 clp = oo->oo_owner.so_client;
3091 if ((bool)clp->cl_minorversion != sessions)
3093 renew_client(oo->oo_owner.so_client);
3100 /* search file_hashtbl[] for file */
3101 static struct nfs4_file *
3102 find_file_locked(struct knfsd_fh *fh)
3104 unsigned int hashval = file_hashval(fh);
3105 struct nfs4_file *fp;
3107 lockdep_assert_held(&state_lock);
3109 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
3110 if (nfsd_fh_match(&fp->fi_fhandle, fh)) {
3118 static struct nfs4_file *
3119 find_file(struct knfsd_fh *fh)
3121 struct nfs4_file *fp;
3123 spin_lock(&state_lock);
3124 fp = find_file_locked(fh);
3125 spin_unlock(&state_lock);
3129 static struct nfs4_file *
3130 find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
3132 struct nfs4_file *fp;
3134 spin_lock(&state_lock);
3135 fp = find_file_locked(fh);
3137 nfsd4_init_file(new, fh);
3140 spin_unlock(&state_lock);
3146 * Called to check deny when READ with all zero stateid or
3147 * WRITE with all zero or all one stateid
3150 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3152 struct nfs4_file *fp;
3153 __be32 ret = nfs_ok;
3155 fp = find_file(¤t_fh->fh_handle);
3158 /* Check for conflicting share reservations */
3159 spin_lock(&fp->fi_lock);
3160 if (fp->fi_share_deny & deny_type)
3161 ret = nfserr_locked;
3162 spin_unlock(&fp->fi_lock);
3167 void nfsd4_prepare_cb_recall(struct nfs4_delegation *dp)
3169 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3172 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
3175 * We can't do this in nfsd_break_deleg_cb because it is
3176 * already holding inode->i_lock.
3178 * If the dl_time != 0, then we know that it has already been
3179 * queued for a lease break. Don't queue it again.
3181 spin_lock(&state_lock);
3182 if (dp->dl_time == 0) {
3183 dp->dl_time = get_seconds();
3184 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
3186 spin_unlock(&state_lock);
3189 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3192 * We're assuming the state code never drops its reference
3193 * without first removing the lease. Since we're in this lease
3194 * callback (and since the lease code is serialized by the kernel
3195 * lock) we know the server hasn't removed the lease yet, we know
3196 * it's safe to take a reference.
3198 atomic_inc(&dp->dl_stid.sc_count);
3199 nfsd4_cb_recall(dp);
3202 /* Called from break_lease() with i_lock held. */
3203 static void nfsd_break_deleg_cb(struct file_lock *fl)
3205 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3206 struct nfs4_delegation *dp;
3209 WARN(1, "(%p)->fl_owner NULL\n", fl);
3212 if (fp->fi_had_conflict) {
3213 WARN(1, "duplicate break on %p\n", fp);
3217 * We don't want the locks code to timeout the lease for us;
3218 * we'll remove it ourself if a delegation isn't returned
3221 fl->fl_break_time = 0;
3223 spin_lock(&fp->fi_lock);
3224 fp->fi_had_conflict = true;
3226 * If there are no delegations on the list, then we can't count on this
3227 * lease ever being cleaned up. Set the fl_break_time to jiffies so that
3228 * time_out_leases will do it ASAP. The fact that fi_had_conflict is now
3229 * true should keep any new delegations from being hashed.
3231 if (list_empty(&fp->fi_delegations))
3232 fl->fl_break_time = jiffies;
3234 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3235 nfsd_break_one_deleg(dp);
3236 spin_unlock(&fp->fi_lock);
3240 int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
3243 return lease_modify(onlist, arg);
3248 static const struct lock_manager_operations nfsd_lease_mng_ops = {
3249 .lm_break = nfsd_break_deleg_cb,
3250 .lm_change = nfsd_change_deleg_cb,
3253 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3255 if (nfsd4_has_session(cstate))
3257 if (seqid == so->so_seqid - 1)
3258 return nfserr_replay_me;
3259 if (seqid == so->so_seqid)
3261 return nfserr_bad_seqid;
3264 static __be32 lookup_clientid(clientid_t *clid,
3265 struct nfsd4_compound_state *cstate,
3266 struct nfsd_net *nn)
3268 struct nfs4_client *found;
3271 found = cstate->clp;
3272 if (!same_clid(&found->cl_clientid, clid))
3273 return nfserr_stale_clientid;
3277 if (STALE_CLIENTID(clid, nn))
3278 return nfserr_stale_clientid;
3281 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3282 * cached already then we know this is for is for v4.0 and "sessions"
3285 WARN_ON_ONCE(cstate->session);
3286 found = find_confirmed_client(clid, false, nn);
3288 return nfserr_expired;
3290 /* Cache the nfs4_client in cstate! */
3291 cstate->clp = found;
3292 atomic_inc(&found->cl_refcount);
3297 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
3298 struct nfsd4_open *open, struct nfsd_net *nn)
3300 clientid_t *clientid = &open->op_clientid;
3301 struct nfs4_client *clp = NULL;
3302 unsigned int strhashval;
3303 struct nfs4_openowner *oo = NULL;
3306 if (STALE_CLIENTID(&open->op_clientid, nn))
3307 return nfserr_stale_clientid;
3309 * In case we need it later, after we've already created the
3310 * file and don't want to risk a further failure:
3312 open->op_file = nfsd4_alloc_file();
3313 if (open->op_file == NULL)
3314 return nfserr_jukebox;
3316 status = lookup_clientid(clientid, cstate, nn);
3321 strhashval = ownerstr_hashval(clientid->cl_id, &open->op_owner);
3322 oo = find_openstateowner_str(strhashval, open, cstate->minorversion, nn);
3323 open->op_openowner = oo;
3327 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
3328 /* Replace unconfirmed owners without checking for replay. */
3329 release_openowner(oo);
3330 open->op_openowner = NULL;
3333 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3338 oo = alloc_init_open_stateowner(strhashval, open, cstate);
3340 return nfserr_jukebox;
3341 open->op_openowner = oo;
3343 open->op_stp = nfs4_alloc_open_stateid(clp);
3345 return nfserr_jukebox;
3349 static inline __be32
3350 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3352 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3353 return nfserr_openmode;
3358 static int share_access_to_flags(u32 share_access)
3360 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3363 static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
3365 struct nfs4_stid *ret;
3367 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
3370 return delegstateid(ret);
3373 static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3375 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3376 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3380 nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
3381 struct nfs4_delegation **dp)
3384 __be32 status = nfserr_bad_stateid;
3385 struct nfs4_delegation *deleg;
3387 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3390 flags = share_access_to_flags(open->op_share_access);
3391 status = nfs4_check_delegmode(deleg, flags);
3393 nfs4_put_stid(&deleg->dl_stid);
3398 if (!nfsd4_is_deleg_cur(open))
3402 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
3406 static struct nfs4_ol_stateid *
3407 nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
3409 struct nfs4_ol_stateid *local, *ret = NULL;
3410 struct nfs4_openowner *oo = open->op_openowner;
3412 spin_lock(&fp->fi_lock);
3413 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
3414 /* ignore lock owners */
3415 if (local->st_stateowner->so_is_open_owner == 0)
3417 if (local->st_stateowner == &oo->oo_owner) {
3419 atomic_inc(&ret->st_stid.sc_count);
3423 spin_unlock(&fp->fi_lock);
3427 static inline int nfs4_access_to_access(u32 nfs4_access)
3431 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3432 flags |= NFSD_MAY_READ;
3433 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3434 flags |= NFSD_MAY_WRITE;
3438 static inline __be32
3439 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3440 struct nfsd4_open *open)
3442 struct iattr iattr = {
3443 .ia_valid = ATTR_SIZE,
3446 if (!open->op_truncate)
3448 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
3449 return nfserr_inval;
3450 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3453 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
3454 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3455 struct nfsd4_open *open)
3457 struct file *filp = NULL;
3459 int oflag = nfs4_access_to_omode(open->op_share_access);
3460 int access = nfs4_access_to_access(open->op_share_access);
3461 unsigned char old_access_bmap, old_deny_bmap;
3463 spin_lock(&fp->fi_lock);
3466 * Are we trying to set a deny mode that would conflict with
3469 status = nfs4_file_check_deny(fp, open->op_share_deny);
3470 if (status != nfs_ok) {
3471 spin_unlock(&fp->fi_lock);
3475 /* set access to the file */
3476 status = nfs4_file_get_access(fp, open->op_share_access);
3477 if (status != nfs_ok) {
3478 spin_unlock(&fp->fi_lock);
3482 /* Set access bits in stateid */
3483 old_access_bmap = stp->st_access_bmap;
3484 set_access(open->op_share_access, stp);
3486 /* Set new deny mask */
3487 old_deny_bmap = stp->st_deny_bmap;
3488 set_deny(open->op_share_deny, stp);
3489 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3491 if (!fp->fi_fds[oflag]) {
3492 spin_unlock(&fp->fi_lock);
3493 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
3495 goto out_put_access;
3496 spin_lock(&fp->fi_lock);
3497 if (!fp->fi_fds[oflag]) {
3498 fp->fi_fds[oflag] = filp;
3502 spin_unlock(&fp->fi_lock);
3506 status = nfsd4_truncate(rqstp, cur_fh, open);
3508 goto out_put_access;
3512 stp->st_access_bmap = old_access_bmap;
3513 nfs4_file_put_access(fp, open->op_share_access);
3514 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3519 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)
3522 unsigned char old_deny_bmap;
3524 if (!test_access(open->op_share_access, stp))
3525 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
3527 /* test and set deny mode */
3528 spin_lock(&fp->fi_lock);
3529 status = nfs4_file_check_deny(fp, open->op_share_deny);
3530 if (status == nfs_ok) {
3531 old_deny_bmap = stp->st_deny_bmap;
3532 set_deny(open->op_share_deny, stp);
3533 fp->fi_share_deny |=
3534 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3536 spin_unlock(&fp->fi_lock);
3538 if (status != nfs_ok)
3541 status = nfsd4_truncate(rqstp, cur_fh, open);
3542 if (status != nfs_ok)
3543 reset_union_bmap_deny(old_deny_bmap, stp);
3548 nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
3550 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
3553 /* Should we give out recallable state?: */
3554 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3556 if (clp->cl_cb_state == NFSD4_CB_UP)
3559 * In the sessions case, since we don't have to establish a
3560 * separate connection for callbacks, we assume it's OK
3561 * until we hear otherwise:
3563 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3566 static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
3568 struct file_lock *fl;
3570 fl = locks_alloc_lock();
3573 locks_init_lock(fl);
3574 fl->fl_lmops = &nfsd_lease_mng_ops;
3575 fl->fl_flags = FL_DELEG;
3576 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3577 fl->fl_end = OFFSET_MAX;
3578 fl->fl_owner = (fl_owner_t)fp;
3579 fl->fl_pid = current->tgid;
3583 static int nfs4_setlease(struct nfs4_delegation *dp)
3585 struct nfs4_file *fp = dp->dl_stid.sc_file;
3586 struct file_lock *fl;
3590 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
3593 filp = find_readable_file(fp);
3595 /* We should always have a readable file here */
3600 status = vfs_setlease(filp, fl->fl_type, &fl);
3602 locks_free_lock(fl);
3605 spin_lock(&state_lock);
3606 spin_lock(&fp->fi_lock);
3607 /* Did the lease get broken before we took the lock? */
3609 if (fp->fi_had_conflict)
3614 atomic_inc(&fp->fi_delegees);
3615 hash_delegation_locked(dp, fp);
3619 fp->fi_deleg_file = filp;
3620 atomic_set(&fp->fi_delegees, 1);
3621 hash_delegation_locked(dp, fp);
3622 spin_unlock(&fp->fi_lock);
3623 spin_unlock(&state_lock);
3626 spin_unlock(&fp->fi_lock);
3627 spin_unlock(&state_lock);
3633 static struct nfs4_delegation *
3634 nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
3635 struct nfs4_file *fp)
3638 struct nfs4_delegation *dp;
3640 if (fp->fi_had_conflict)
3641 return ERR_PTR(-EAGAIN);
3643 dp = alloc_init_deleg(clp, fh);
3645 return ERR_PTR(-ENOMEM);
3648 spin_lock(&state_lock);
3649 spin_lock(&fp->fi_lock);
3650 dp->dl_stid.sc_file = fp;
3651 if (!fp->fi_lease) {
3652 spin_unlock(&fp->fi_lock);
3653 spin_unlock(&state_lock);
3654 status = nfs4_setlease(dp);
3657 atomic_inc(&fp->fi_delegees);
3658 if (fp->fi_had_conflict) {
3662 hash_delegation_locked(dp, fp);
3665 spin_unlock(&fp->fi_lock);
3666 spin_unlock(&state_lock);
3669 nfs4_put_stid(&dp->dl_stid);
3670 return ERR_PTR(status);
3675 static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3677 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3678 if (status == -EAGAIN)
3679 open->op_why_no_deleg = WND4_CONTENTION;
3681 open->op_why_no_deleg = WND4_RESOURCE;
3682 switch (open->op_deleg_want) {
3683 case NFS4_SHARE_WANT_READ_DELEG:
3684 case NFS4_SHARE_WANT_WRITE_DELEG:
3685 case NFS4_SHARE_WANT_ANY_DELEG:
3687 case NFS4_SHARE_WANT_CANCEL:
3688 open->op_why_no_deleg = WND4_CANCELLED;
3690 case NFS4_SHARE_WANT_NO_DELEG:
3697 * Attempt to hand out a delegation.
3699 * Note we don't support write delegations, and won't until the vfs has
3700 * proper support for them.
3703 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
3704 struct nfs4_ol_stateid *stp)
3706 struct nfs4_delegation *dp;
3707 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
3708 struct nfs4_client *clp = stp->st_stid.sc_client;
3712 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
3713 open->op_recall = 0;
3714 switch (open->op_claim_type) {
3715 case NFS4_OPEN_CLAIM_PREVIOUS:
3717 open->op_recall = 1;
3718 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3721 case NFS4_OPEN_CLAIM_NULL:
3722 case NFS4_OPEN_CLAIM_FH:
3724 * Let's not give out any delegations till everyone's
3725 * had the chance to reclaim theirs....
3727 if (locks_in_grace(clp->net))
3729 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
3732 * Also, if the file was opened for write or
3733 * create, there's a good chance the client's
3734 * about to write to it, resulting in an
3735 * immediate recall (since we don't support
3736 * write delegations):
3738 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
3740 if (open->op_create == NFS4_OPEN_CREATE)
3746 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file);
3750 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
3752 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
3753 STATEID_VAL(&dp->dl_stid.sc_stateid));
3754 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
3755 nfs4_put_stid(&dp->dl_stid);
3758 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3759 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
3760 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
3761 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
3762 open->op_recall = 1;
3765 /* 4.1 client asking for a delegation? */
3766 if (open->op_deleg_want)
3767 nfsd4_open_deleg_none_ext(open, status);
3771 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3772 struct nfs4_delegation *dp)
3774 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3775 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3776 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3777 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3778 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3779 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3780 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3781 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3783 /* Otherwise the client must be confused wanting a delegation
3784 * it already has, therefore we don't return
3785 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3790 * called with nfs4_lock_state() held.
3793 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3795 struct nfsd4_compoundres *resp = rqstp->rq_resp;
3796 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
3797 struct nfs4_file *fp = NULL;
3798 struct nfs4_ol_stateid *stp = NULL;
3799 struct nfs4_delegation *dp = NULL;
3803 * Lookup file; if found, lookup stateid and check open request,
3804 * and check for delegations in the process of being recalled.
3805 * If not found, create the nfs4_file struct
3807 fp = find_or_add_file(open->op_file, ¤t_fh->fh_handle);
3808 if (fp != open->op_file) {
3809 status = nfs4_check_deleg(cl, open, &dp);
3812 stp = nfsd4_find_existing_open(fp, open);
3814 open->op_file = NULL;
3815 status = nfserr_bad_stateid;
3816 if (nfsd4_is_deleg_cur(open))
3818 status = nfserr_jukebox;
3822 * OPEN the file, or upgrade an existing OPEN.
3823 * If truncate fails, the OPEN fails.
3826 /* Stateid was found, this is an OPEN upgrade */
3827 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
3832 open->op_stp = NULL;
3833 init_open_stateid(stp, fp, open);
3834 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
3836 release_open_stateid(stp);
3840 update_stateid(&stp->st_stid.sc_stateid);
3841 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
3843 if (nfsd4_has_session(&resp->cstate)) {
3844 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
3845 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3846 open->op_why_no_deleg = WND4_NOT_WANTED;
3852 * Attempt to hand out a delegation. No error return, because the
3853 * OPEN succeeds even if we fail.
3855 nfs4_open_delegation(current_fh, open, stp);
3859 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
3860 STATEID_VAL(&stp->st_stid.sc_stateid));
3862 /* 4.1 client trying to upgrade/downgrade delegation? */
3863 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
3864 open->op_deleg_want)
3865 nfsd4_deleg_xgrade_none_ext(open, dp);
3869 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
3870 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
3872 * To finish the open response, we just need to set the rflags.
3874 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
3875 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
3876 !nfsd4_has_session(&resp->cstate))
3877 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
3879 nfs4_put_stid(&dp->dl_stid);
3881 nfs4_put_stid(&stp->st_stid);
3886 void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
3887 struct nfsd4_open *open, __be32 status)
3889 if (open->op_openowner) {
3890 struct nfs4_openowner *oo = open->op_openowner;
3892 if (!list_empty(&oo->oo_owner.so_stateids))
3893 list_del_init(&oo->oo_close_lru);
3894 if (oo->oo_flags & NFS4_OO_NEW) {
3896 release_openowner(oo);
3897 open->op_openowner = NULL;
3899 oo->oo_flags &= ~NFS4_OO_NEW;
3901 if (open->op_openowner)
3902 nfsd4_cstate_assign_replay(cstate, &oo->oo_owner);
3905 nfsd4_free_file(open->op_file);
3907 nfs4_put_stid(&open->op_stp->st_stid);
3911 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3914 struct nfs4_client *clp;
3916 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3919 dprintk("process_renew(%08x/%08x): starting\n",
3920 clid->cl_boot, clid->cl_id);
3921 status = lookup_clientid(clid, cstate, nn);
3925 status = nfserr_cb_path_down;
3926 if (!list_empty(&clp->cl_delegations)
3927 && clp->cl_cb_state != NFSD4_CB_UP)
3931 nfs4_unlock_state();
3936 nfsd4_end_grace(struct nfsd_net *nn)
3938 /* do nothing if grace period already ended */
3939 if (nn->grace_ended)
3942 dprintk("NFSD: end of grace period\n");
3943 nn->grace_ended = true;
3944 nfsd4_record_grace_done(nn, nn->boot_time);
3945 locks_end_grace(&nn->nfsd4_manager);
3947 * Now that every NFSv4 client has had the chance to recover and
3948 * to see the (possibly new, possibly shorter) lease time, we
3949 * can safely set the next grace time to the current lease time:
3951 nn->nfsd4_grace = nn->nfsd4_lease;
3955 nfs4_laundromat(struct nfsd_net *nn)
3957 struct nfs4_client *clp;
3958 struct nfs4_openowner *oo;
3959 struct nfs4_delegation *dp;
3960 struct list_head *pos, *next, reaplist;
3961 time_t cutoff = get_seconds() - nn->nfsd4_lease;
3962 time_t t, new_timeo = nn->nfsd4_lease;
3966 dprintk("NFSD: laundromat service - starting\n");
3967 nfsd4_end_grace(nn);
3968 INIT_LIST_HEAD(&reaplist);
3969 spin_lock(&nn->client_lock);
3970 list_for_each_safe(pos, next, &nn->client_lru) {
3971 clp = list_entry(pos, struct nfs4_client, cl_lru);
3972 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
3973 t = clp->cl_time - cutoff;
3974 new_timeo = min(new_timeo, t);
3977 if (mark_client_expired_locked(clp)) {
3978 dprintk("NFSD: client in use (clientid %08x)\n",
3979 clp->cl_clientid.cl_id);
3982 list_move(&clp->cl_lru, &reaplist);
3984 spin_unlock(&nn->client_lock);
3985 list_for_each_safe(pos, next, &reaplist) {
3986 clp = list_entry(pos, struct nfs4_client, cl_lru);
3987 dprintk("NFSD: purging unused client (clientid %08x)\n",
3988 clp->cl_clientid.cl_id);
3991 spin_lock(&state_lock);
3992 list_for_each_safe(pos, next, &nn->del_recall_lru) {
3993 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3994 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
3996 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
3997 t = dp->dl_time - cutoff;
3998 new_timeo = min(new_timeo, t);
4001 unhash_delegation_locked(dp);
4002 list_add(&dp->dl_recall_lru, &reaplist);
4004 spin_unlock(&state_lock);
4005 while (!list_empty(&reaplist)) {
4006 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4008 list_del_init(&dp->dl_recall_lru);
4009 revoke_delegation(dp);
4011 list_for_each_safe(pos, next, &nn->close_lru) {
4012 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
4013 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
4014 t = oo->oo_time - cutoff;
4015 new_timeo = min(new_timeo, t);
4018 release_openowner(oo);
4020 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
4021 nfs4_unlock_state();
4025 static struct workqueue_struct *laundry_wq;
4026 static void laundromat_main(struct work_struct *);
4029 laundromat_main(struct work_struct *laundry)
4032 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
4034 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4037 t = nfs4_laundromat(nn);
4038 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
4039 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
4042 static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
4044 if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_stid.sc_file->fi_fhandle))
4045 return nfserr_bad_stateid;
4050 access_permit_read(struct nfs4_ol_stateid *stp)
4052 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4053 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4054 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
4058 access_permit_write(struct nfs4_ol_stateid *stp)
4060 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4061 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
4065 __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
4067 __be32 status = nfserr_openmode;
4069 /* For lock stateid's, we test the parent open, not the lock: */
4070 if (stp->st_openstp)
4071 stp = stp->st_openstp;
4072 if ((flags & WR_STATE) && !access_permit_write(stp))
4074 if ((flags & RD_STATE) && !access_permit_read(stp))
4081 static inline __be32
4082 check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
4084 if (ONE_STATEID(stateid) && (flags & RD_STATE))
4086 else if (locks_in_grace(net)) {
4087 /* Answer in remaining cases depends on existence of
4088 * conflicting state; so we must wait out the grace period. */
4089 return nfserr_grace;
4090 } else if (flags & WR_STATE)
4091 return nfs4_share_conflict(current_fh,
4092 NFS4_SHARE_DENY_WRITE);
4093 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4094 return nfs4_share_conflict(current_fh,
4095 NFS4_SHARE_DENY_READ);
4099 * Allow READ/WRITE during grace period on recovered state only for files
4100 * that are not able to provide mandatory locking.
4103 grace_disallows_io(struct net *net, struct inode *inode)
4105 return locks_in_grace(net) && mandatory_lock(inode);
4108 /* Returns true iff a is later than b: */
4109 static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4111 return (s32)(a->si_generation - b->si_generation) > 0;
4114 static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
4117 * When sessions are used the stateid generation number is ignored
4120 if (has_session && in->si_generation == 0)
4123 if (in->si_generation == ref->si_generation)
4126 /* If the client sends us a stateid from the future, it's buggy: */
4127 if (stateid_generation_after(in, ref))
4128 return nfserr_bad_stateid;
4130 * However, we could see a stateid from the past, even from a
4131 * non-buggy client. For example, if the client sends a lock
4132 * while some IO is outstanding, the lock may bump si_generation
4133 * while the IO is still in flight. The client could avoid that
4134 * situation by waiting for responses on all the IO requests,
4135 * but better performance may result in retrying IO that
4136 * receives an old_stateid error if requests are rarely
4137 * reordered in flight:
4139 return nfserr_old_stateid;
4142 static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
4144 struct nfs4_stid *s;
4145 struct nfs4_ol_stateid *ols;
4146 __be32 status = nfserr_bad_stateid;
4148 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4150 /* Client debugging aid. */
4151 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4152 char addr_str[INET6_ADDRSTRLEN];
4153 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4155 pr_warn_ratelimited("NFSD: client %s testing state ID "
4156 "with incorrect client ID\n", addr_str);
4159 spin_lock(&cl->cl_lock);
4160 s = find_stateid_locked(cl, stateid);
4163 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
4166 switch (s->sc_type) {
4167 case NFS4_DELEG_STID:
4170 case NFS4_REVOKED_DELEG_STID:
4171 status = nfserr_deleg_revoked;
4173 case NFS4_OPEN_STID:
4174 case NFS4_LOCK_STID:
4175 ols = openlockstateid(s);
4176 if (ols->st_stateowner->so_is_open_owner
4177 && !(openowner(ols->st_stateowner)->oo_flags
4178 & NFS4_OO_CONFIRMED))
4179 status = nfserr_bad_stateid;
4184 printk("unknown stateid type %x\n", s->sc_type);
4186 case NFS4_CLOSED_STID:
4187 case NFS4_CLOSED_DELEG_STID:
4188 status = nfserr_bad_stateid;
4191 spin_unlock(&cl->cl_lock);
4196 nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4197 stateid_t *stateid, unsigned char typemask,
4198 struct nfs4_stid **s, struct nfsd_net *nn)
4202 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4203 return nfserr_bad_stateid;
4204 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
4205 if (status == nfserr_stale_clientid) {
4206 if (cstate->session)
4207 return nfserr_bad_stateid;
4208 return nfserr_stale_stateid;
4212 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
4214 return nfserr_bad_stateid;
4219 * Checks for stateid operations
4222 nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
4223 stateid_t *stateid, int flags, struct file **filpp)
4225 struct nfs4_stid *s;
4226 struct nfs4_ol_stateid *stp = NULL;
4227 struct nfs4_delegation *dp = NULL;
4228 struct svc_fh *current_fh = &cstate->current_fh;
4229 struct inode *ino = current_fh->fh_dentry->d_inode;
4230 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4231 struct file *file = NULL;
4237 if (grace_disallows_io(net, ino))
4238 return nfserr_grace;
4240 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4241 return check_special_stateids(net, current_fh, stateid, flags);
4245 status = nfsd4_lookup_stateid(cstate, stateid,
4246 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
4250 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4253 switch (s->sc_type) {
4254 case NFS4_DELEG_STID:
4255 dp = delegstateid(s);
4256 status = nfs4_check_delegmode(dp, flags);
4260 file = dp->dl_stid.sc_file->fi_deleg_file;
4263 status = nfserr_serverfault;
4269 case NFS4_OPEN_STID:
4270 case NFS4_LOCK_STID:
4271 stp = openlockstateid(s);
4272 status = nfs4_check_fh(current_fh, stp);
4275 if (stp->st_stateowner->so_is_open_owner
4276 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
4278 status = nfs4_check_openmode(stp, flags);
4282 struct nfs4_file *fp = stp->st_stid.sc_file;
4284 if (flags & RD_STATE)
4285 file = find_readable_file(fp);
4287 file = find_writeable_file(fp);
4291 status = nfserr_bad_stateid;
4300 nfs4_unlock_state();
4305 nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
4307 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
4309 if (check_for_locks(stp->st_stid.sc_file, lo))
4310 return nfserr_locks_held;
4311 release_lock_stateid(stp);
4316 * Test if the stateid is valid
4319 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4320 struct nfsd4_test_stateid *test_stateid)
4322 struct nfsd4_test_stateid_id *stateid;
4323 struct nfs4_client *cl = cstate->session->se_client;
4326 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
4327 stateid->ts_id_status =
4328 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
4329 nfs4_unlock_state();
4335 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4336 struct nfsd4_free_stateid *free_stateid)
4338 stateid_t *stateid = &free_stateid->fr_stateid;
4339 struct nfs4_stid *s;
4340 struct nfs4_delegation *dp;
4341 struct nfs4_client *cl = cstate->session->se_client;
4342 __be32 ret = nfserr_bad_stateid;
4345 spin_lock(&cl->cl_lock);
4346 s = find_stateid_locked(cl, stateid);
4349 switch (s->sc_type) {
4350 case NFS4_DELEG_STID:
4351 ret = nfserr_locks_held;
4353 case NFS4_OPEN_STID:
4354 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4357 ret = nfserr_locks_held;
4359 case NFS4_LOCK_STID:
4360 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4363 spin_unlock(&cl->cl_lock);
4364 ret = nfsd4_free_lock_stateid(openlockstateid(s));
4366 case NFS4_REVOKED_DELEG_STID:
4367 dp = delegstateid(s);
4368 list_del_init(&dp->dl_recall_lru);
4369 spin_unlock(&cl->cl_lock);
4373 /* Default falls through and returns nfserr_bad_stateid */
4376 spin_unlock(&cl->cl_lock);
4378 nfs4_unlock_state();
4385 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4386 RD_STATE : WR_STATE;
4389 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
4391 struct svc_fh *current_fh = &cstate->current_fh;
4392 struct nfs4_stateowner *sop = stp->st_stateowner;
4395 status = nfsd4_check_seqid(cstate, sop, seqid);
4398 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4399 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
4401 * "Closed" stateid's exist *only* to return
4402 * nfserr_replay_me from the previous step, and
4403 * revoked delegations are kept only for free_stateid.
4405 return nfserr_bad_stateid;
4406 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4409 return nfs4_check_fh(current_fh, stp);
4413 * Checks for sequence id mutating operations.
4416 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4417 stateid_t *stateid, char typemask,
4418 struct nfs4_ol_stateid **stpp,
4419 struct nfsd_net *nn)
4422 struct nfs4_stid *s;
4423 struct nfs4_ol_stateid *stp = NULL;
4425 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4426 seqid, STATEID_VAL(stateid));
4429 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
4432 stp = openlockstateid(s);
4433 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
4435 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
4439 nfs4_put_stid(&stp->st_stid);
4443 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4444 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
4447 struct nfs4_openowner *oo;
4448 struct nfs4_ol_stateid *stp;
4450 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
4451 NFS4_OPEN_STID, &stp, nn);
4454 oo = openowner(stp->st_stateowner);
4455 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4456 nfs4_put_stid(&stp->st_stid);
4457 return nfserr_bad_stateid;
4464 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4465 struct nfsd4_open_confirm *oc)
4468 struct nfs4_openowner *oo;
4469 struct nfs4_ol_stateid *stp;
4470 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4472 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4473 cstate->current_fh.fh_dentry);
4475 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
4481 status = nfs4_preprocess_seqid_op(cstate,
4482 oc->oc_seqid, &oc->oc_req_stateid,
4483 NFS4_OPEN_STID, &stp, nn);
4486 oo = openowner(stp->st_stateowner);
4487 status = nfserr_bad_stateid;
4488 if (oo->oo_flags & NFS4_OO_CONFIRMED)
4490 oo->oo_flags |= NFS4_OO_CONFIRMED;
4491 update_stateid(&stp->st_stid.sc_stateid);
4492 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4493 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
4494 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
4496 nfsd4_client_record_create(oo->oo_owner.so_client);
4499 nfs4_put_stid(&stp->st_stid);
4501 nfsd4_bump_seqid(cstate, status);
4502 nfs4_unlock_state();
4506 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
4508 if (!test_access(access, stp))
4510 nfs4_file_put_access(stp->st_stid.sc_file, access);
4511 clear_access(access, stp);
4514 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4516 switch (to_access) {
4517 case NFS4_SHARE_ACCESS_READ:
4518 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4519 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4521 case NFS4_SHARE_ACCESS_WRITE:
4522 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4523 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4525 case NFS4_SHARE_ACCESS_BOTH:
4533 nfsd4_open_downgrade(struct svc_rqst *rqstp,
4534 struct nfsd4_compound_state *cstate,
4535 struct nfsd4_open_downgrade *od)
4538 struct nfs4_ol_stateid *stp;
4539 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4541 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4542 cstate->current_fh.fh_dentry);
4544 /* We don't yet support WANT bits: */
4545 if (od->od_deleg_want)
4546 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4550 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
4551 &od->od_stateid, &stp, nn);
4554 status = nfserr_inval;
4555 if (!test_access(od->od_share_access, stp)) {
4556 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
4557 stp->st_access_bmap, od->od_share_access);
4560 if (!test_deny(od->od_share_deny, stp)) {
4561 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
4562 stp->st_deny_bmap, od->od_share_deny);
4565 nfs4_stateid_downgrade(stp, od->od_share_access);
4567 reset_union_bmap_deny(od->od_share_deny, stp);
4569 update_stateid(&stp->st_stid.sc_stateid);
4570 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4573 nfs4_put_stid(&stp->st_stid);
4575 nfsd4_bump_seqid(cstate, status);
4576 nfs4_unlock_state();
4580 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4582 struct nfs4_client *clp = s->st_stid.sc_client;
4583 struct nfs4_openowner *oo = openowner(s->st_stateowner);
4585 s->st_stid.sc_type = NFS4_CLOSED_STID;
4586 unhash_open_stateid(s);
4588 if (clp->cl_minorversion) {
4589 if (list_empty(&oo->oo_owner.so_stateids))
4590 release_openowner(oo);
4591 nfs4_put_stid(&s->st_stid);
4594 * In the 4.0 case we need to keep the owners around a
4595 * little while to handle CLOSE replay. We still do need
4596 * to release any file access that is held by them
4597 * before returning however.
4599 release_all_access(s);
4600 if (s->st_stid.sc_file) {
4601 put_nfs4_file(s->st_stid.sc_file);
4602 s->st_stid.sc_file = NULL;
4604 oo->oo_last_closed_stid = s;
4605 if (list_empty(&oo->oo_owner.so_stateids))
4606 move_to_close_lru(oo, clp->net);
4611 * nfs4_unlock_state() called after encode
4614 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4615 struct nfsd4_close *close)
4618 struct nfs4_ol_stateid *stp;
4619 struct net *net = SVC_NET(rqstp);
4620 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4622 dprintk("NFSD: nfsd4_close on file %pd\n",
4623 cstate->current_fh.fh_dentry);
4626 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4628 NFS4_OPEN_STID|NFS4_CLOSED_STID,
4630 nfsd4_bump_seqid(cstate, status);
4633 update_stateid(&stp->st_stid.sc_stateid);
4634 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4636 nfsd4_close_open_stateid(stp);
4638 /* put reference from nfs4_preprocess_seqid_op */
4639 nfs4_put_stid(&stp->st_stid);
4641 nfs4_unlock_state();
4646 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4647 struct nfsd4_delegreturn *dr)
4649 struct nfs4_delegation *dp;
4650 stateid_t *stateid = &dr->dr_stateid;
4651 struct nfs4_stid *s;
4653 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4655 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
4659 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
4662 dp = delegstateid(s);
4663 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
4667 destroy_delegation(dp);
4669 nfs4_put_stid(&dp->dl_stid);
4671 nfs4_unlock_state();
4677 #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
4680 end_offset(u64 start, u64 len)
4685 return end >= start ? end: NFS4_MAX_UINT64;
4688 /* last octet in a range */
4690 last_byte_offset(u64 start, u64 len)
4696 return end > start ? end - 1: NFS4_MAX_UINT64;
4700 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4701 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4702 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4703 * locking, this prevents us from being completely protocol-compliant. The
4704 * real solution to this problem is to start using unsigned file offsets in
4705 * the VFS, but this is a very deep change!
4708 nfs4_transform_lock_offset(struct file_lock *lock)
4710 if (lock->fl_start < 0)
4711 lock->fl_start = OFFSET_MAX;
4712 if (lock->fl_end < 0)
4713 lock->fl_end = OFFSET_MAX;
4716 /* Hack!: For now, we're defining this just so we can use a pointer to it
4717 * as a unique cookie to identify our (NFSv4's) posix locks. */
4718 static const struct lock_manager_operations nfsd_posix_mng_ops = {
4722 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4724 struct nfs4_lockowner *lo;
4726 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
4727 lo = (struct nfs4_lockowner *) fl->fl_owner;
4728 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4729 lo->lo_owner.so_owner.len, GFP_KERNEL);
4730 if (!deny->ld_owner.data)
4731 /* We just don't care that much */
4733 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4734 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
4737 deny->ld_owner.len = 0;
4738 deny->ld_owner.data = NULL;
4739 deny->ld_clientid.cl_boot = 0;
4740 deny->ld_clientid.cl_id = 0;
4742 deny->ld_start = fl->fl_start;
4743 deny->ld_length = NFS4_MAX_UINT64;
4744 if (fl->fl_end != NFS4_MAX_UINT64)
4745 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4746 deny->ld_type = NFS4_READ_LT;
4747 if (fl->fl_type != F_RDLCK)
4748 deny->ld_type = NFS4_WRITE_LT;
4751 static struct nfs4_lockowner *
4752 find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
4753 struct nfsd_net *nn)
4755 unsigned int strhashval = ownerstr_hashval(clid->cl_id, owner);
4756 struct nfs4_stateowner *so;
4758 list_for_each_entry(so, &nn->ownerstr_hashtbl[strhashval], so_strhash) {
4759 if (so->so_is_open_owner)
4761 if (!same_owner_str(so, owner, clid))
4763 atomic_inc(&so->so_count);
4764 return lockowner(so);
4769 static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
4771 unhash_lockowner(lockowner(sop));
4774 static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
4776 struct nfs4_lockowner *lo = lockowner(sop);
4778 kmem_cache_free(lockowner_slab, lo);
4781 static const struct nfs4_stateowner_operations lockowner_ops = {
4782 .so_unhash = nfs4_unhash_lockowner,
4783 .so_free = nfs4_free_lockowner,
4787 * Alloc a lock owner structure.
4788 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
4791 * strhashval = ownerstr_hashval
4793 static struct nfs4_lockowner *
4794 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_ol_stateid *open_stp, struct nfsd4_lock *lock) {
4795 struct nfs4_lockowner *lo;
4796 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
4798 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4801 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4802 lo->lo_owner.so_is_open_owner = 0;
4803 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
4804 lo->lo_owner.so_ops = &lockowner_ops;
4805 list_add(&lo->lo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
4810 init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
4811 struct nfs4_file *fp, struct inode *inode,
4812 struct nfs4_ol_stateid *open_stp)
4814 struct nfs4_client *clp = lo->lo_owner.so_client;
4816 lockdep_assert_held(&clp->cl_lock);
4818 atomic_inc(&stp->st_stid.sc_count);
4819 stp->st_stid.sc_type = NFS4_LOCK_STID;
4820 stp->st_stateowner = &lo->lo_owner;
4821 atomic_inc(&lo->lo_owner.so_count);
4823 stp->st_stid.sc_file = fp;
4824 stp->st_stid.sc_free = nfs4_free_lock_stateid;
4825 stp->st_access_bmap = 0;
4826 stp->st_deny_bmap = open_stp->st_deny_bmap;
4827 stp->st_openstp = open_stp;
4828 list_add(&stp->st_locks, &open_stp->st_locks);
4829 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
4830 spin_lock(&fp->fi_lock);
4831 list_add(&stp->st_perfile, &fp->fi_stateids);
4832 spin_unlock(&fp->fi_lock);
4835 static struct nfs4_ol_stateid *
4836 find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
4838 struct nfs4_ol_stateid *lst;
4839 struct nfs4_client *clp = lo->lo_owner.so_client;
4841 lockdep_assert_held(&clp->cl_lock);
4843 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
4844 if (lst->st_stid.sc_file == fp) {
4845 atomic_inc(&lst->st_stid.sc_count);
4852 static struct nfs4_ol_stateid *
4853 find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
4854 struct inode *inode, struct nfs4_ol_stateid *ost,
4857 struct nfs4_stid *ns = NULL;
4858 struct nfs4_ol_stateid *lst;
4859 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
4860 struct nfs4_client *clp = oo->oo_owner.so_client;
4862 spin_lock(&clp->cl_lock);
4863 lst = find_lock_stateid(lo, fi);
4865 spin_unlock(&clp->cl_lock);
4866 ns = nfs4_alloc_stid(clp, stateid_slab);
4870 spin_lock(&clp->cl_lock);
4871 lst = find_lock_stateid(lo, fi);
4873 lst = openlockstateid(ns);
4874 init_lock_stateid(lst, lo, fi, inode, ost);
4879 spin_unlock(&clp->cl_lock);
4886 check_lock_length(u64 offset, u64 length)
4888 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
4889 LOFF_OVERFLOW(offset, length)));
4892 static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
4894 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
4896 lockdep_assert_held(&fp->fi_lock);
4898 if (test_access(access, lock_stp))
4900 __nfs4_file_get_access(fp, access);
4901 set_access(access, lock_stp);
4905 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
4906 struct nfs4_ol_stateid *ost,
4907 struct nfsd4_lock *lock,
4908 struct nfs4_ol_stateid **lst, bool *new)
4911 struct nfs4_file *fi = ost->st_stid.sc_file;
4912 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
4913 struct nfs4_client *cl = oo->oo_owner.so_client;
4914 struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
4915 struct nfs4_lockowner *lo;
4916 unsigned int strhashval;
4917 struct nfsd_net *nn = net_generic(cl->net, nfsd_net_id);
4919 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, nn);
4921 strhashval = ownerstr_hashval(cl->cl_clientid.cl_id,
4922 &lock->v.new.owner);
4923 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
4925 return nfserr_jukebox;
4927 /* with an existing lockowner, seqids must be the same */
4928 status = nfserr_bad_seqid;
4929 if (!cstate->minorversion &&
4930 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
4934 *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
4936 status = nfserr_jukebox;
4941 nfs4_put_stateowner(&lo->lo_owner);
4949 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4950 struct nfsd4_lock *lock)
4952 struct nfs4_openowner *open_sop = NULL;
4953 struct nfs4_lockowner *lock_sop = NULL;
4954 struct nfs4_ol_stateid *lock_stp = NULL;
4955 struct nfs4_ol_stateid *open_stp = NULL;
4956 struct nfs4_file *fp;
4957 struct file *filp = NULL;
4958 struct file_lock *file_lock = NULL;
4959 struct file_lock *conflock = NULL;
4964 struct net *net = SVC_NET(rqstp);
4965 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4967 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
4968 (long long) lock->lk_offset,
4969 (long long) lock->lk_length);
4971 if (check_lock_length(lock->lk_offset, lock->lk_length))
4972 return nfserr_inval;
4974 if ((status = fh_verify(rqstp, &cstate->current_fh,
4975 S_IFREG, NFSD_MAY_LOCK))) {
4976 dprintk("NFSD: nfsd4_lock: permission denied!\n");
4982 if (lock->lk_is_new) {
4983 if (nfsd4_has_session(cstate))
4984 /* See rfc 5661 18.10.3: given clientid is ignored: */
4985 memcpy(&lock->v.new.clientid,
4986 &cstate->session->se_client->cl_clientid,
4987 sizeof(clientid_t));
4989 status = nfserr_stale_clientid;
4990 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
4993 /* validate and update open stateid and open seqid */
4994 status = nfs4_preprocess_confirmed_seqid_op(cstate,
4995 lock->lk_new_open_seqid,
4996 &lock->lk_new_open_stateid,
5000 open_sop = openowner(open_stp->st_stateowner);
5001 status = nfserr_bad_stateid;
5002 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
5003 &lock->v.new.clientid))
5005 status = lookup_or_create_lock_state(cstate, open_stp, lock,
5008 status = nfs4_preprocess_seqid_op(cstate,
5009 lock->lk_old_lock_seqid,
5010 &lock->lk_old_lock_stateid,
5011 NFS4_LOCK_STID, &lock_stp, nn);
5015 lock_sop = lockowner(lock_stp->st_stateowner);
5017 lkflg = setlkflg(lock->lk_type);
5018 status = nfs4_check_openmode(lock_stp, lkflg);
5022 status = nfserr_grace;
5023 if (locks_in_grace(net) && !lock->lk_reclaim)
5025 status = nfserr_no_grace;
5026 if (!locks_in_grace(net) && lock->lk_reclaim)
5029 file_lock = locks_alloc_lock();
5031 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5032 status = nfserr_jukebox;
5036 fp = lock_stp->st_stid.sc_file;
5037 locks_init_lock(file_lock);
5038 switch (lock->lk_type) {
5041 spin_lock(&fp->fi_lock);
5042 filp = find_readable_file_locked(fp);
5044 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
5045 spin_unlock(&fp->fi_lock);
5046 file_lock->fl_type = F_RDLCK;
5049 case NFS4_WRITEW_LT:
5050 spin_lock(&fp->fi_lock);
5051 filp = find_writeable_file_locked(fp);
5053 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
5054 spin_unlock(&fp->fi_lock);
5055 file_lock->fl_type = F_WRLCK;
5058 status = nfserr_inval;
5062 status = nfserr_openmode;
5065 file_lock->fl_owner = (fl_owner_t)lock_sop;
5066 file_lock->fl_pid = current->tgid;
5067 file_lock->fl_file = filp;
5068 file_lock->fl_flags = FL_POSIX;
5069 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5070 file_lock->fl_start = lock->lk_offset;
5071 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5072 nfs4_transform_lock_offset(file_lock);
5074 conflock = locks_alloc_lock();
5076 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5077 status = nfserr_jukebox;
5081 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
5083 case 0: /* success! */
5084 update_stateid(&lock_stp->st_stid.sc_stateid);
5085 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
5089 case (EAGAIN): /* conflock holds conflicting lock */
5090 status = nfserr_denied;
5091 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
5092 nfs4_set_lock_denied(conflock, &lock->lk_denied);
5095 status = nfserr_deadlock;
5098 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
5099 status = nfserrno(err);
5106 /* Bump seqid manually if the 4.0 replay owner is openowner */
5107 if (cstate->replay_owner &&
5108 cstate->replay_owner != &lock_sop->lo_owner &&
5109 seqid_mutating_err(ntohl(status)))
5110 lock_sop->lo_owner.so_seqid++;
5113 * If this is a new, never-before-used stateid, and we are
5114 * returning an error, then just go ahead and release it.
5117 release_lock_stateid(lock_stp);
5119 nfs4_put_stid(&lock_stp->st_stid);
5122 nfs4_put_stid(&open_stp->st_stid);
5123 nfsd4_bump_seqid(cstate, status);
5124 nfs4_unlock_state();
5126 locks_free_lock(file_lock);
5128 locks_free_lock(conflock);
5133 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5134 * so we do a temporary open here just to get an open file to pass to
5135 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5138 static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
5141 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5143 err = nfserrno(vfs_test_lock(file, lock));
5153 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5154 struct nfsd4_lockt *lockt)
5156 struct file_lock *file_lock = NULL;
5157 struct nfs4_lockowner *lo = NULL;
5159 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5161 if (locks_in_grace(SVC_NET(rqstp)))
5162 return nfserr_grace;
5164 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5165 return nfserr_inval;
5169 if (!nfsd4_has_session(cstate)) {
5170 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
5175 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
5178 file_lock = locks_alloc_lock();
5180 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5181 status = nfserr_jukebox;
5184 locks_init_lock(file_lock);
5185 switch (lockt->lt_type) {
5188 file_lock->fl_type = F_RDLCK;
5191 case NFS4_WRITEW_LT:
5192 file_lock->fl_type = F_WRLCK;
5195 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
5196 status = nfserr_inval;
5200 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner, nn);
5202 file_lock->fl_owner = (fl_owner_t)lo;
5203 file_lock->fl_pid = current->tgid;
5204 file_lock->fl_flags = FL_POSIX;
5206 file_lock->fl_start = lockt->lt_offset;
5207 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
5209 nfs4_transform_lock_offset(file_lock);
5211 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
5215 if (file_lock->fl_type != F_UNLCK) {
5216 status = nfserr_denied;
5217 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
5221 nfs4_put_stateowner(&lo->lo_owner);
5222 nfs4_unlock_state();
5224 locks_free_lock(file_lock);
5229 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5230 struct nfsd4_locku *locku)
5232 struct nfs4_ol_stateid *stp;
5233 struct file *filp = NULL;
5234 struct file_lock *file_lock = NULL;
5237 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5239 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5240 (long long) locku->lu_offset,
5241 (long long) locku->lu_length);
5243 if (check_lock_length(locku->lu_offset, locku->lu_length))
5244 return nfserr_inval;
5248 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
5249 &locku->lu_stateid, NFS4_LOCK_STID,
5253 filp = find_any_file(stp->st_stid.sc_file);
5255 status = nfserr_lock_range;
5258 file_lock = locks_alloc_lock();
5260 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5261 status = nfserr_jukebox;
5264 locks_init_lock(file_lock);
5265 file_lock->fl_type = F_UNLCK;
5266 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
5267 file_lock->fl_pid = current->tgid;
5268 file_lock->fl_file = filp;
5269 file_lock->fl_flags = FL_POSIX;
5270 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5271 file_lock->fl_start = locku->lu_offset;
5273 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5275 nfs4_transform_lock_offset(file_lock);
5277 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
5279 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
5282 update_stateid(&stp->st_stid.sc_stateid);
5283 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
5287 nfs4_put_stid(&stp->st_stid);
5289 nfsd4_bump_seqid(cstate, status);
5290 nfs4_unlock_state();
5292 locks_free_lock(file_lock);
5296 status = nfserrno(err);
5302 * true: locks held by lockowner
5303 * false: no locks held by lockowner
5306 check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
5308 struct file_lock **flpp;
5310 struct file *filp = find_any_file(fp);
5311 struct inode *inode;
5314 /* Any valid lock stateid should have some sort of access */
5319 inode = file_inode(filp);
5321 spin_lock(&inode->i_lock);
5322 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
5323 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
5328 spin_unlock(&inode->i_lock);
5334 nfsd4_release_lockowner(struct svc_rqst *rqstp,
5335 struct nfsd4_compound_state *cstate,
5336 struct nfsd4_release_lockowner *rlockowner)
5338 clientid_t *clid = &rlockowner->rl_clientid;
5339 struct nfs4_stateowner *sop = NULL, *tmp;
5340 struct nfs4_lockowner *lo;
5341 struct nfs4_ol_stateid *stp;
5342 struct xdr_netobj *owner = &rlockowner->rl_owner;
5343 unsigned int hashval = ownerstr_hashval(clid->cl_id, owner);
5345 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5347 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5348 clid->cl_boot, clid->cl_id);
5352 status = lookup_clientid(clid, cstate, nn);
5356 status = nfserr_locks_held;
5358 /* Find the matching lock stateowner */
5359 list_for_each_entry(tmp, &nn->ownerstr_hashtbl[hashval], so_strhash) {
5360 if (tmp->so_is_open_owner)
5362 if (same_owner_str(tmp, owner, clid)) {
5364 atomic_inc(&sop->so_count);
5369 /* No matching owner found, maybe a replay? Just declare victory... */
5375 lo = lockowner(sop);
5376 /* see if there are still any locks associated with it */
5377 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5378 if (check_for_locks(stp->st_stid.sc_file, lo)) {
5379 nfs4_put_stateowner(sop);
5385 release_lockowner(lo);
5387 nfs4_unlock_state();
5391 static inline struct nfs4_client_reclaim *
5394 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
5398 nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
5400 struct nfs4_client_reclaim *crp;
5402 crp = nfsd4_find_reclaim_client(name, nn);
5403 return (crp && crp->cr_clp);
5407 * failure => all reset bets are off, nfserr_no_grace...
5409 struct nfs4_client_reclaim *
5410 nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
5412 unsigned int strhashval;
5413 struct nfs4_client_reclaim *crp;
5415 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5416 crp = alloc_reclaim();
5418 strhashval = clientstr_hashval(name);
5419 INIT_LIST_HEAD(&crp->cr_strhash);
5420 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
5421 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
5423 nn->reclaim_str_hashtbl_size++;
5429 nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
5431 list_del(&crp->cr_strhash);
5433 nn->reclaim_str_hashtbl_size--;
5437 nfs4_release_reclaim(struct nfsd_net *nn)
5439 struct nfs4_client_reclaim *crp = NULL;
5442 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5443 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5444 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
5445 struct nfs4_client_reclaim, cr_strhash);
5446 nfs4_remove_reclaim_record(crp, nn);
5449 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
5453 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
5454 struct nfs4_client_reclaim *
5455 nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
5457 unsigned int strhashval;
5458 struct nfs4_client_reclaim *crp = NULL;
5460 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
5462 strhashval = clientstr_hashval(recdir);
5463 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
5464 if (same_name(crp->cr_recdir, recdir)) {
5472 * Called from OPEN. Look for clientid in reclaim list.
5475 nfs4_check_open_reclaim(clientid_t *clid,
5476 struct nfsd4_compound_state *cstate,
5477 struct nfsd_net *nn)
5481 /* find clientid in conf_id_hashtbl */
5482 status = lookup_clientid(clid, cstate, nn);
5484 return nfserr_reclaim_bad;
5486 if (nfsd4_client_record_check(cstate->clp))
5487 return nfserr_reclaim_bad;
5492 #ifdef CONFIG_NFSD_FAULT_INJECTION
5494 u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
5496 if (mark_client_expired(clp))
5502 u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
5504 char buf[INET6_ADDRSTRLEN];
5505 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5506 printk(KERN_INFO "NFS Client: %s\n", buf);
5510 static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5513 char buf[INET6_ADDRSTRLEN];
5514 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5515 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5518 static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
5519 void (*func)(struct nfs4_ol_stateid *))
5521 struct nfs4_openowner *oop;
5522 struct nfs4_ol_stateid *stp, *st_next;
5523 struct nfs4_ol_stateid *lst, *lst_next;
5526 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
5527 list_for_each_entry_safe(stp, st_next,
5528 &oop->oo_owner.so_stateids, st_perstateowner) {
5529 list_for_each_entry_safe(lst, lst_next,
5530 &stp->st_locks, st_locks) {
5542 u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
5544 return nfsd_foreach_client_lock(clp, max, release_lock_stateid);
5547 u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
5549 u64 count = nfsd_foreach_client_lock(clp, max, NULL);
5550 nfsd_print_count(clp, count, "locked files");
5554 static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
5556 struct nfs4_openowner *oop, *next;
5559 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
5569 u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
5571 return nfsd_foreach_client_open(clp, max, release_openowner);
5574 u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
5576 u64 count = nfsd_foreach_client_open(clp, max, NULL);
5577 nfsd_print_count(clp, count, "open files");
5581 static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
5582 struct list_head *victims)
5584 struct nfs4_delegation *dp, *next;
5587 lockdep_assert_held(&state_lock);
5588 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
5591 * It's not safe to mess with delegations that have a
5592 * non-zero dl_time. They might have already been broken
5593 * and could be processed by the laundromat outside of
5594 * the state_lock. Just leave them be.
5596 if (dp->dl_time != 0)
5599 unhash_delegation_locked(dp);
5600 list_add(&dp->dl_recall_lru, victims);
5608 u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
5610 struct nfs4_delegation *dp, *next;
5614 spin_lock(&state_lock);
5615 count = nfsd_find_all_delegations(clp, max, &victims);
5616 spin_unlock(&state_lock);
5618 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru) {
5619 list_del_init(&dp->dl_recall_lru);
5620 revoke_delegation(dp);
5626 u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
5628 struct nfs4_delegation *dp;
5632 spin_lock(&state_lock);
5633 count = nfsd_find_all_delegations(clp, max, &victims);
5634 while (!list_empty(&victims)) {
5635 dp = list_first_entry(&victims, struct nfs4_delegation,
5637 list_del_init(&dp->dl_recall_lru);
5639 nfsd_break_one_deleg(dp);
5641 spin_unlock(&state_lock);
5646 u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
5650 spin_lock(&state_lock);
5651 count = nfsd_find_all_delegations(clp, max, NULL);
5652 spin_unlock(&state_lock);
5654 nfsd_print_count(clp, count, "delegations");
5658 u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
5660 struct nfs4_client *clp, *next;
5662 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
5664 if (!nfsd_netns_ready(nn))
5667 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
5668 count += func(clp, max - count);
5669 if ((max != 0) && (count >= max))
5676 struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5678 struct nfs4_client *clp;
5679 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
5681 if (!nfsd_netns_ready(nn))
5684 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5685 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5691 #endif /* CONFIG_NFSD_FAULT_INJECTION */
5694 * Since the lifetime of a delegation isn't limited to that of an open, a
5695 * client may quite reasonably hang on to a delegation as long as it has
5696 * the inode cached. This becomes an obvious problem the first time a
5697 * client's inode cache approaches the size of the server's total memory.
5699 * For now we avoid this problem by imposing a hard limit on the number
5700 * of delegations, which varies according to the server's memory size.
5703 set_max_delegations(void)
5706 * Allow at most 4 delegations per megabyte of RAM. Quick
5707 * estimates suggest that in the worst case (where every delegation
5708 * is for a different inode), a delegation could take about 1.5K,
5709 * giving a worst case usage of about 6% of memory.
5711 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
5714 static int nfs4_state_create_net(struct net *net)
5716 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5719 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5720 CLIENT_HASH_SIZE, GFP_KERNEL);
5721 if (!nn->conf_id_hashtbl)
5723 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5724 CLIENT_HASH_SIZE, GFP_KERNEL);
5725 if (!nn->unconf_id_hashtbl)
5727 nn->ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
5728 OWNER_HASH_SIZE, GFP_KERNEL);
5729 if (!nn->ownerstr_hashtbl)
5731 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
5732 SESSION_HASH_SIZE, GFP_KERNEL);
5733 if (!nn->sessionid_hashtbl)
5736 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5737 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
5738 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
5740 for (i = 0; i < OWNER_HASH_SIZE; i++)
5741 INIT_LIST_HEAD(&nn->ownerstr_hashtbl[i]);
5742 for (i = 0; i < SESSION_HASH_SIZE; i++)
5743 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
5744 nn->conf_name_tree = RB_ROOT;
5745 nn->unconf_name_tree = RB_ROOT;
5746 INIT_LIST_HEAD(&nn->client_lru);
5747 INIT_LIST_HEAD(&nn->close_lru);
5748 INIT_LIST_HEAD(&nn->del_recall_lru);
5749 spin_lock_init(&nn->client_lock);
5751 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
5757 kfree(nn->ownerstr_hashtbl);
5759 kfree(nn->unconf_id_hashtbl);
5761 kfree(nn->conf_id_hashtbl);
5767 nfs4_state_destroy_net(struct net *net)
5770 struct nfs4_client *clp = NULL;
5771 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5773 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5774 while (!list_empty(&nn->conf_id_hashtbl[i])) {
5775 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5776 destroy_client(clp);
5780 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5781 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
5782 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5783 destroy_client(clp);
5787 kfree(nn->sessionid_hashtbl);
5788 kfree(nn->ownerstr_hashtbl);
5789 kfree(nn->unconf_id_hashtbl);
5790 kfree(nn->conf_id_hashtbl);
5795 nfs4_state_start_net(struct net *net)
5797 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5800 ret = nfs4_state_create_net(net);
5803 nfsd4_client_tracking_init(net);
5804 nn->boot_time = get_seconds();
5805 locks_start_grace(net, &nn->nfsd4_manager);
5806 nn->grace_ended = false;
5807 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
5808 nn->nfsd4_grace, net);
5809 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
5813 /* initialization to perform when the nfsd service is started: */
5816 nfs4_state_start(void)
5820 ret = set_callback_cred();
5823 laundry_wq = create_singlethread_workqueue("nfsd4");
5824 if (laundry_wq == NULL) {
5828 ret = nfsd4_create_callback_queue();
5830 goto out_free_laundry;
5832 set_max_delegations();
5837 destroy_workqueue(laundry_wq);
5843 nfs4_state_shutdown_net(struct net *net)
5845 struct nfs4_delegation *dp = NULL;
5846 struct list_head *pos, *next, reaplist;
5847 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5849 cancel_delayed_work_sync(&nn->laundromat_work);
5850 locks_end_grace(&nn->nfsd4_manager);
5853 INIT_LIST_HEAD(&reaplist);
5854 spin_lock(&state_lock);
5855 list_for_each_safe(pos, next, &nn->del_recall_lru) {
5856 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
5857 unhash_delegation_locked(dp);
5858 list_add(&dp->dl_recall_lru, &reaplist);
5860 spin_unlock(&state_lock);
5861 list_for_each_safe(pos, next, &reaplist) {
5862 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
5863 list_del_init(&dp->dl_recall_lru);
5864 nfs4_put_stid(&dp->dl_stid);
5867 nfsd4_client_tracking_exit(net);
5868 nfs4_state_destroy_net(net);
5869 nfs4_unlock_state();
5873 nfs4_state_shutdown(void)
5875 destroy_workqueue(laundry_wq);
5876 nfsd4_destroy_callback_queue();
5880 get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5882 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
5883 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
5887 put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5889 if (cstate->minorversion) {
5890 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
5891 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
5896 clear_current_stateid(struct nfsd4_compound_state *cstate)
5898 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
5902 * functions to set current state id
5905 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5907 put_stateid(cstate, &odp->od_stateid);
5911 nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
5913 put_stateid(cstate, &open->op_stateid);
5917 nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5919 put_stateid(cstate, &close->cl_stateid);
5923 nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
5925 put_stateid(cstate, &lock->lk_resp_stateid);
5929 * functions to consume current state id
5933 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5935 get_stateid(cstate, &odp->od_stateid);
5939 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
5941 get_stateid(cstate, &drp->dr_stateid);
5945 nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
5947 get_stateid(cstate, &fsp->fr_stateid);
5951 nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
5953 get_stateid(cstate, &setattr->sa_stateid);
5957 nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5959 get_stateid(cstate, &close->cl_stateid);
5963 nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
5965 get_stateid(cstate, &locku->lu_stateid);
5969 nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
5971 get_stateid(cstate, &read->rd_stateid);
5975 nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
5977 get_stateid(cstate, &write->wr_stateid);